KVM: VMX: Expose various getters and setters to nested VMX
[linux-block.git] / arch / x86 / kvm / vmx / vmx.h
CommitLineData
8373d25d
SC
1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef __KVM_X86_VMX_H
3#define __KVM_X86_VMX_H
4
5#include <linux/kvm_host.h>
6
7#include <asm/kvm.h>
8
9#include "capabilities.h"
89b0c9f5 10#include "ops.h"
8373d25d
SC
11#include "vmcs.h"
12
cf3646eb
SC
13extern const u32 vmx_msr_index[];
14extern const ulong vmx_return;
15extern u64 host_efer;
16
8373d25d
SC
17#define MSR_TYPE_R 1
18#define MSR_TYPE_W 2
19#define MSR_TYPE_RW 3
20
21#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
22
23#define NR_AUTOLOAD_MSRS 8
24
25struct vmx_msrs {
26 unsigned int nr;
27 struct vmx_msr_entry val[NR_AUTOLOAD_MSRS];
28};
29
30struct shared_msr_entry {
31 unsigned index;
32 u64 data;
33 u64 mask;
34};
35
36enum segment_cache_field {
37 SEG_FIELD_SEL = 0,
38 SEG_FIELD_BASE = 1,
39 SEG_FIELD_LIMIT = 2,
40 SEG_FIELD_AR = 3,
41
42 SEG_FIELD_NR = 4
43};
44
45/* Posted-Interrupt Descriptor */
46struct pi_desc {
47 u32 pir[8]; /* Posted interrupt requested */
48 union {
49 struct {
50 /* bit 256 - Outstanding Notification */
51 u16 on : 1,
52 /* bit 257 - Suppress Notification */
53 sn : 1,
54 /* bit 271:258 - Reserved */
55 rsvd_1 : 14;
56 /* bit 279:272 - Notification Vector */
57 u8 nv;
58 /* bit 287:280 - Reserved */
59 u8 rsvd_2;
60 /* bit 319:288 - Notification Destination */
61 u32 ndst;
62 };
63 u64 control;
64 };
65 u32 rsvd[6];
66} __aligned(64);
67
68
69/*
70 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
71 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
72 */
73struct nested_vmx {
74 /* Has the level1 guest done vmxon? */
75 bool vmxon;
76 gpa_t vmxon_ptr;
77 bool pml_full;
78
79 /* The guest-physical address of the current VMCS L1 keeps for L2 */
80 gpa_t current_vmptr;
81 /*
82 * Cache of the guest's VMCS, existing outside of guest memory.
83 * Loaded from guest memory during VMPTRLD. Flushed to guest
84 * memory during VMCLEAR and VMPTRLD.
85 */
86 struct vmcs12 *cached_vmcs12;
87 /*
88 * Cache of the guest's shadow VMCS, existing outside of guest
89 * memory. Loaded from guest memory during VM entry. Flushed
90 * to guest memory during VM exit.
91 */
92 struct vmcs12 *cached_shadow_vmcs12;
93 /*
94 * Indicates if the shadow vmcs or enlightened vmcs must be updated
95 * with the data held by struct vmcs12.
96 */
97 bool need_vmcs12_sync;
98 bool dirty_vmcs12;
99
100 /*
101 * vmcs02 has been initialized, i.e. state that is constant for
102 * vmcs02 has been written to the backing VMCS. Initialization
103 * is delayed until L1 actually attempts to run a nested VM.
104 */
105 bool vmcs02_initialized;
106
107 bool change_vmcs01_virtual_apic_mode;
108
109 /*
110 * Enlightened VMCS has been enabled. It does not mean that L1 has to
111 * use it. However, VMX features available to L1 will be limited based
112 * on what the enlightened VMCS supports.
113 */
114 bool enlightened_vmcs_enabled;
115
116 /* L2 must run next, and mustn't decide to exit to L1. */
117 bool nested_run_pending;
118
119 struct loaded_vmcs vmcs02;
120
121 /*
122 * Guest pages referred to in the vmcs02 with host-physical
123 * pointers, so we must keep them pinned while L2 runs.
124 */
125 struct page *apic_access_page;
126 struct page *virtual_apic_page;
127 struct page *pi_desc_page;
128 struct pi_desc *pi_desc;
129 bool pi_pending;
130 u16 posted_intr_nv;
131
132 struct hrtimer preemption_timer;
133 bool preemption_timer_expired;
134
135 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
136 u64 vmcs01_debugctl;
137 u64 vmcs01_guest_bndcfgs;
138
139 u16 vpid02;
140 u16 last_vpid;
141
142 struct nested_vmx_msrs msrs;
143
144 /* SMM related state */
145 struct {
146 /* in VMX operation on SMM entry? */
147 bool vmxon;
148 /* in guest mode on SMM entry? */
149 bool guest_mode;
150 } smm;
151
152 gpa_t hv_evmcs_vmptr;
153 struct page *hv_evmcs_page;
154 struct hv_enlightened_vmcs *hv_evmcs;
155};
156
157struct vcpu_vmx {
158 struct kvm_vcpu vcpu;
159 unsigned long host_rsp;
160 u8 fail;
161 u8 msr_bitmap_mode;
162 u32 exit_intr_info;
163 u32 idt_vectoring_info;
164 ulong rflags;
165 struct shared_msr_entry *guest_msrs;
166 int nmsrs;
167 int save_nmsrs;
168 bool guest_msrs_dirty;
169 unsigned long host_idt_base;
170#ifdef CONFIG_X86_64
171 u64 msr_host_kernel_gs_base;
172 u64 msr_guest_kernel_gs_base;
173#endif
174
175 u64 arch_capabilities;
176 u64 spec_ctrl;
177
178 u32 vm_entry_controls_shadow;
179 u32 vm_exit_controls_shadow;
180 u32 secondary_exec_control;
181
182 /*
183 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
184 * non-nested (L1) guest, it always points to vmcs01. For a nested
185 * guest (L2), it points to a different VMCS. loaded_cpu_state points
186 * to the VMCS whose state is loaded into the CPU registers that only
187 * need to be switched when transitioning to/from the kernel; a NULL
188 * value indicates that host state is loaded.
189 */
190 struct loaded_vmcs vmcs01;
191 struct loaded_vmcs *loaded_vmcs;
192 struct loaded_vmcs *loaded_cpu_state;
193 bool __launched; /* temporary, used in vmx_vcpu_run */
194 struct msr_autoload {
195 struct vmx_msrs guest;
196 struct vmx_msrs host;
197 } msr_autoload;
198
199 struct {
200 int vm86_active;
201 ulong save_rflags;
202 struct kvm_segment segs[8];
203 } rmode;
204 struct {
205 u32 bitmask; /* 4 bits per segment (1 bit per field) */
206 struct kvm_save_segment {
207 u16 selector;
208 unsigned long base;
209 u32 limit;
210 u32 ar;
211 } seg[8];
212 } segment_cache;
213 int vpid;
214 bool emulation_required;
215
216 u32 exit_reason;
217
218 /* Posted interrupt descriptor */
219 struct pi_desc pi_desc;
220
221 /* Support for a guest hypervisor (nested VMX) */
222 struct nested_vmx nested;
223
224 /* Dynamic PLE window. */
225 int ple_window;
226 bool ple_window_dirty;
227
228 bool req_immediate_exit;
229
230 /* Support for PML */
231#define PML_ENTITY_NUM 512
232 struct page *pml_pg;
233
234 /* apic deadline value in host tsc */
235 u64 hv_deadline_tsc;
236
237 u64 current_tsc_ratio;
238
239 u32 host_pkru;
240
241 unsigned long host_debugctlmsr;
242
243 /*
244 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
245 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
246 * in msr_ia32_feature_control_valid_bits.
247 */
248 u64 msr_ia32_feature_control;
249 u64 msr_ia32_feature_control_valid_bits;
250 u64 ept_pointer;
251};
252
253enum ept_pointers_status {
254 EPT_POINTERS_CHECK = 0,
255 EPT_POINTERS_MATCH = 1,
256 EPT_POINTERS_MISMATCH = 2
257};
258
259struct kvm_vmx {
260 struct kvm kvm;
261
262 unsigned int tss_addr;
263 bool ept_identity_pagetable_done;
264 gpa_t ept_identity_map_addr;
265
266 enum ept_pointers_status ept_pointers_match;
267 spinlock_t ept_pointer_lock;
268};
269
97b7ead3
SC
270void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
271void vmx_vcpu_put(struct kvm_vcpu *vcpu);
272int allocate_vpid(void);
273void free_vpid(int vpid);
274void vmx_set_constant_host_state(struct vcpu_vmx *vmx);
275void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu);
276int vmx_get_cpl(struct kvm_vcpu *vcpu);
277unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu);
278void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags);
279u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu);
280void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask);
281void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer);
282void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
283void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3);
284int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
285void set_cr4_guest_host_mask(struct vcpu_vmx *vmx);
286void ept_save_pdptrs(struct kvm_vcpu *vcpu);
287void vmx_get_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg);
288void vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg);
289u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
290void update_exception_bitmap(struct kvm_vcpu *vcpu);
291void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
292bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
293void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
294void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu);
295struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr);
296
8373d25d
SC
297#define POSTED_INTR_ON 0
298#define POSTED_INTR_SN 1
299
300static inline bool pi_test_and_set_on(struct pi_desc *pi_desc)
301{
302 return test_and_set_bit(POSTED_INTR_ON,
303 (unsigned long *)&pi_desc->control);
304}
305
306static inline bool pi_test_and_clear_on(struct pi_desc *pi_desc)
307{
308 return test_and_clear_bit(POSTED_INTR_ON,
309 (unsigned long *)&pi_desc->control);
310}
311
312static inline int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
313{
314 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
315}
316
317static inline void pi_clear_sn(struct pi_desc *pi_desc)
318{
319 return clear_bit(POSTED_INTR_SN,
320 (unsigned long *)&pi_desc->control);
321}
322
323static inline void pi_set_sn(struct pi_desc *pi_desc)
324{
325 return set_bit(POSTED_INTR_SN,
326 (unsigned long *)&pi_desc->control);
327}
328
329static inline void pi_clear_on(struct pi_desc *pi_desc)
330{
331 clear_bit(POSTED_INTR_ON,
332 (unsigned long *)&pi_desc->control);
333}
334
335static inline int pi_test_on(struct pi_desc *pi_desc)
336{
337 return test_bit(POSTED_INTR_ON,
338 (unsigned long *)&pi_desc->control);
339}
340
341static inline int pi_test_sn(struct pi_desc *pi_desc)
342{
343 return test_bit(POSTED_INTR_SN,
344 (unsigned long *)&pi_desc->control);
345}
346
89b0c9f5
SC
347static inline u8 vmx_get_rvi(void)
348{
349 return vmcs_read16(GUEST_INTR_STATUS) & 0xff;
350}
351
352static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
353{
354 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
355}
356
357static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
358{
359 vmcs_write32(VM_ENTRY_CONTROLS, val);
360 vmx->vm_entry_controls_shadow = val;
361}
362
363static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
364{
365 if (vmx->vm_entry_controls_shadow != val)
366 vm_entry_controls_init(vmx, val);
367}
368
369static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
370{
371 return vmx->vm_entry_controls_shadow;
372}
373
374static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
375{
376 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
377}
378
379static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
380{
381 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
382}
383
384static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
385{
386 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
387}
388
389static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
390{
391 vmcs_write32(VM_EXIT_CONTROLS, val);
392 vmx->vm_exit_controls_shadow = val;
393}
394
395static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
396{
397 if (vmx->vm_exit_controls_shadow != val)
398 vm_exit_controls_init(vmx, val);
399}
400
401static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
402{
403 return vmx->vm_exit_controls_shadow;
404}
405
406static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
407{
408 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
409}
410
411static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
412{
413 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
414}
415
8373d25d
SC
416static inline void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
417{
418 vmx->segment_cache.bitmask = 0;
419}
420
421static inline u32 vmx_vmentry_ctrl(void)
422{
423 /* Loading of EFER and PERF_GLOBAL_CTRL are toggled dynamically */
424 return vmcs_config.vmentry_ctrl &
425 ~(VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL | VM_ENTRY_LOAD_IA32_EFER);
426}
427
428static inline u32 vmx_vmexit_ctrl(void)
429{
430 /* Loading of EFER and PERF_GLOBAL_CTRL are toggled dynamically */
431 return vmcs_config.vmexit_ctrl &
432 ~(VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL | VM_EXIT_LOAD_IA32_EFER);
433}
434
435u32 vmx_exec_control(struct vcpu_vmx *vmx);
436
437static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
438{
439 return container_of(kvm, struct kvm_vmx, kvm);
440}
441
442static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
443{
444 return container_of(vcpu, struct vcpu_vmx, vcpu);
445}
446
447static inline struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
448{
449 return &(to_vmx(vcpu)->pi_desc);
450}
451
89b0c9f5
SC
452struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu);
453void free_vmcs(struct vmcs *vmcs);
454int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs);
455void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs);
456void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs);
457void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs);
458
459static inline struct vmcs *alloc_vmcs(bool shadow)
460{
461 return alloc_vmcs_cpu(shadow, raw_smp_processor_id());
462}
463
464u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
465
466static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
467 bool invalidate_gpa)
468{
469 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
470 if (!VALID_PAGE(vcpu->arch.mmu->root_hpa))
471 return;
472 ept_sync_context(construct_eptp(vcpu,
473 vcpu->arch.mmu->root_hpa));
474 } else {
475 vpid_sync_context(vpid);
476 }
477}
478
479static inline void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
480{
481 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
482}
483
484static inline void decache_tsc_multiplier(struct vcpu_vmx *vmx)
485{
486 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
487 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
488}
489
8373d25d 490#endif /* __KVM_X86_VMX_H */