1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __KVM_X86_VMX_H
3 #define __KVM_X86_VMX_H
5 #include <linux/kvm_host.h>
8 #include <asm/intel_pt.h>
10 #include "capabilities.h"
14 extern const u32 vmx_msr_index[];
17 extern u32 get_umwait_control_msr(void);
23 #define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
25 #define NR_AUTOLOAD_MSRS 8
29 struct vmx_msr_entry val[NR_AUTOLOAD_MSRS];
32 struct shared_msr_entry {
38 enum segment_cache_field {
47 /* Posted-Interrupt Descriptor */
49 u32 pir[8]; /* Posted interrupt requested */
52 /* bit 256 - Outstanding Notification */
54 /* bit 257 - Suppress Notification */
56 /* bit 271:258 - Reserved */
58 /* bit 279:272 - Notification Vector */
60 /* bit 287:280 - Reserved */
62 /* bit 319:288 - Notification Destination */
70 #define RTIT_ADDR_RANGE 4
78 u64 addr_a[RTIT_ADDR_RANGE];
79 u64 addr_b[RTIT_ADDR_RANGE];
85 u32 caps[PT_CPUID_REGS_NUM * PT_CPUID_LEAVES];
91 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
92 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
95 /* Has the level1 guest done vmxon? */
100 /* The guest-physical address of the current VMCS L1 keeps for L2 */
103 * Cache of the guest's VMCS, existing outside of guest memory.
104 * Loaded from guest memory during VMPTRLD. Flushed to guest
105 * memory during VMCLEAR and VMPTRLD.
107 struct vmcs12 *cached_vmcs12;
109 * Cache of the guest's shadow VMCS, existing outside of guest
110 * memory. Loaded from guest memory during VM entry. Flushed
111 * to guest memory during VM exit.
113 struct vmcs12 *cached_shadow_vmcs12;
116 * Indicates if the shadow vmcs or enlightened vmcs must be updated
117 * with the data held by struct vmcs12.
119 bool need_vmcs12_to_shadow_sync;
123 * Indicates lazily loaded guest state has not yet been decached from
126 bool need_sync_vmcs02_to_vmcs12_rare;
129 * vmcs02 has been initialized, i.e. state that is constant for
130 * vmcs02 has been written to the backing VMCS. Initialization
131 * is delayed until L1 actually attempts to run a nested VM.
133 bool vmcs02_initialized;
135 bool change_vmcs01_virtual_apic_mode;
138 * Enlightened VMCS has been enabled. It does not mean that L1 has to
139 * use it. However, VMX features available to L1 will be limited based
140 * on what the enlightened VMCS supports.
142 bool enlightened_vmcs_enabled;
144 /* L2 must run next, and mustn't decide to exit to L1. */
145 bool nested_run_pending;
147 struct loaded_vmcs vmcs02;
150 * Guest pages referred to in the vmcs02 with host-physical
151 * pointers, so we must keep them pinned while L2 runs.
153 struct page *apic_access_page;
154 struct kvm_host_map virtual_apic_map;
155 struct kvm_host_map pi_desc_map;
157 struct kvm_host_map msr_bitmap_map;
159 struct pi_desc *pi_desc;
163 struct hrtimer preemption_timer;
164 bool preemption_timer_expired;
166 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
168 u64 vmcs01_guest_bndcfgs;
173 struct nested_vmx_msrs msrs;
175 /* SMM related state */
177 /* in VMX operation on SMM entry? */
179 /* in guest mode on SMM entry? */
183 gpa_t hv_evmcs_vmptr;
184 struct kvm_host_map hv_evmcs_map;
185 struct hv_enlightened_vmcs *hv_evmcs;
189 struct kvm_vcpu vcpu;
194 * If true, host state has been stored in vmx->loaded_vmcs for
195 * the CPU registers that only need to be switched when transitioning
196 * to/from the kernel, and the registers have been loaded with guest
197 * values. If false, host state is loaded in the CPU registers
198 * and vmx->loaded_vmcs->host_state is invalid.
200 bool guest_state_loaded;
203 u32 idt_vectoring_info;
206 struct shared_msr_entry *guest_msrs;
209 bool guest_msrs_ready;
211 u64 msr_host_kernel_gs_base;
212 u64 msr_guest_kernel_gs_base;
216 u32 msr_ia32_umwait_control;
218 u32 secondary_exec_control;
221 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
222 * non-nested (L1) guest, it always points to vmcs01. For a nested
223 * guest (L2), it points to a different VMCS.
225 struct loaded_vmcs vmcs01;
226 struct loaded_vmcs *loaded_vmcs;
228 struct msr_autoload {
229 struct vmx_msrs guest;
230 struct vmx_msrs host;
236 struct kvm_segment segs[8];
239 u32 bitmask; /* 4 bits per segment (1 bit per field) */
240 struct kvm_save_segment {
248 bool emulation_required;
252 /* Posted interrupt descriptor */
253 struct pi_desc pi_desc;
255 /* Support for a guest hypervisor (nested VMX) */
256 struct nested_vmx nested;
258 /* Dynamic PLE window. */
259 unsigned int ple_window;
260 bool ple_window_dirty;
262 bool req_immediate_exit;
264 /* Support for PML */
265 #define PML_ENTITY_NUM 512
268 /* apic deadline value in host tsc */
271 u64 current_tsc_ratio;
275 unsigned long host_debugctlmsr;
278 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
279 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
280 * in msr_ia32_feature_control_valid_bits.
282 u64 msr_ia32_feature_control;
283 u64 msr_ia32_feature_control_valid_bits;
286 struct pt_desc pt_desc;
289 enum ept_pointers_status {
290 EPT_POINTERS_CHECK = 0,
291 EPT_POINTERS_MATCH = 1,
292 EPT_POINTERS_MISMATCH = 2
298 unsigned int tss_addr;
299 bool ept_identity_pagetable_done;
300 gpa_t ept_identity_map_addr;
302 enum ept_pointers_status ept_pointers_match;
303 spinlock_t ept_pointer_lock;
306 bool nested_vmx_allowed(struct kvm_vcpu *vcpu);
307 void vmx_vcpu_load_vmcs(struct kvm_vcpu *vcpu, int cpu);
308 void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
309 int allocate_vpid(void);
310 void free_vpid(int vpid);
311 void vmx_set_constant_host_state(struct vcpu_vmx *vmx);
312 void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu);
313 void vmx_set_host_fs_gs(struct vmcs_host_state *host, u16 fs_sel, u16 gs_sel,
314 unsigned long fs_base, unsigned long gs_base);
315 int vmx_get_cpl(struct kvm_vcpu *vcpu);
316 unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu);
317 void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags);
318 u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu);
319 void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask);
320 void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer);
321 void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
322 void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3);
323 int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
324 void set_cr4_guest_host_mask(struct vcpu_vmx *vmx);
325 void ept_save_pdptrs(struct kvm_vcpu *vcpu);
326 void vmx_get_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg);
327 void vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg);
328 u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
329 void update_exception_bitmap(struct kvm_vcpu *vcpu);
330 void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
331 bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
332 void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
333 void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu);
334 struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr);
335 void pt_update_intercept_for_msr(struct vcpu_vmx *vmx);
336 void vmx_update_host_rsp(struct vcpu_vmx *vmx, unsigned long host_rsp);
338 #define POSTED_INTR_ON 0
339 #define POSTED_INTR_SN 1
341 static inline bool pi_test_and_set_on(struct pi_desc *pi_desc)
343 return test_and_set_bit(POSTED_INTR_ON,
344 (unsigned long *)&pi_desc->control);
347 static inline bool pi_test_and_clear_on(struct pi_desc *pi_desc)
349 return test_and_clear_bit(POSTED_INTR_ON,
350 (unsigned long *)&pi_desc->control);
353 static inline int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
355 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
358 static inline void pi_set_sn(struct pi_desc *pi_desc)
360 set_bit(POSTED_INTR_SN,
361 (unsigned long *)&pi_desc->control);
364 static inline void pi_set_on(struct pi_desc *pi_desc)
366 set_bit(POSTED_INTR_ON,
367 (unsigned long *)&pi_desc->control);
370 static inline void pi_clear_on(struct pi_desc *pi_desc)
372 clear_bit(POSTED_INTR_ON,
373 (unsigned long *)&pi_desc->control);
376 static inline int pi_test_on(struct pi_desc *pi_desc)
378 return test_bit(POSTED_INTR_ON,
379 (unsigned long *)&pi_desc->control);
382 static inline int pi_test_sn(struct pi_desc *pi_desc)
384 return test_bit(POSTED_INTR_SN,
385 (unsigned long *)&pi_desc->control);
388 static inline u8 vmx_get_rvi(void)
390 return vmcs_read16(GUEST_INTR_STATUS) & 0xff;
393 #define BUILD_CONTROLS_SHADOW(lname, uname) \
394 static inline void lname##_controls_set(struct vcpu_vmx *vmx, u32 val) \
396 if (vmx->loaded_vmcs->controls_shadow.lname != val) { \
397 vmcs_write32(uname, val); \
398 vmx->loaded_vmcs->controls_shadow.lname = val; \
401 static inline u32 lname##_controls_get(struct vcpu_vmx *vmx) \
403 return vmx->loaded_vmcs->controls_shadow.lname; \
405 static inline void lname##_controls_setbit(struct vcpu_vmx *vmx, u32 val) \
407 lname##_controls_set(vmx, lname##_controls_get(vmx) | val); \
409 static inline void lname##_controls_clearbit(struct vcpu_vmx *vmx, u32 val) \
411 lname##_controls_set(vmx, lname##_controls_get(vmx) & ~val); \
413 BUILD_CONTROLS_SHADOW(vm_entry, VM_ENTRY_CONTROLS)
414 BUILD_CONTROLS_SHADOW(vm_exit, VM_EXIT_CONTROLS)
415 BUILD_CONTROLS_SHADOW(pin, PIN_BASED_VM_EXEC_CONTROL)
416 BUILD_CONTROLS_SHADOW(exec, CPU_BASED_VM_EXEC_CONTROL)
417 BUILD_CONTROLS_SHADOW(secondary_exec, SECONDARY_VM_EXEC_CONTROL)
419 static inline void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
421 vmx->segment_cache.bitmask = 0;
424 static inline u32 vmx_vmentry_ctrl(void)
426 u32 vmentry_ctrl = vmcs_config.vmentry_ctrl;
427 if (pt_mode == PT_MODE_SYSTEM)
428 vmentry_ctrl &= ~(VM_ENTRY_PT_CONCEAL_PIP |
429 VM_ENTRY_LOAD_IA32_RTIT_CTL);
430 /* Loading of EFER and PERF_GLOBAL_CTRL are toggled dynamically */
431 return vmentry_ctrl &
432 ~(VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL | VM_ENTRY_LOAD_IA32_EFER);
435 static inline u32 vmx_vmexit_ctrl(void)
437 u32 vmexit_ctrl = vmcs_config.vmexit_ctrl;
438 if (pt_mode == PT_MODE_SYSTEM)
439 vmexit_ctrl &= ~(VM_EXIT_PT_CONCEAL_PIP |
440 VM_EXIT_CLEAR_IA32_RTIT_CTL);
441 /* Loading of EFER and PERF_GLOBAL_CTRL are toggled dynamically */
443 ~(VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL | VM_EXIT_LOAD_IA32_EFER);
446 u32 vmx_exec_control(struct vcpu_vmx *vmx);
447 u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx);
449 static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
451 return container_of(kvm, struct kvm_vmx, kvm);
454 static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
456 return container_of(vcpu, struct vcpu_vmx, vcpu);
459 static inline struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
461 return &(to_vmx(vcpu)->pi_desc);
464 struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu, gfp_t flags);
465 void free_vmcs(struct vmcs *vmcs);
466 int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs);
467 void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs);
468 void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs);
469 void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs);
471 static inline struct vmcs *alloc_vmcs(bool shadow)
473 return alloc_vmcs_cpu(shadow, raw_smp_processor_id(),
477 u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
479 static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
482 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
483 if (!VALID_PAGE(vcpu->arch.mmu->root_hpa))
485 ept_sync_context(construct_eptp(vcpu,
486 vcpu->arch.mmu->root_hpa));
488 vpid_sync_context(vpid);
492 static inline void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
494 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
497 static inline void decache_tsc_multiplier(struct vcpu_vmx *vmx)
499 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
500 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
503 static inline bool vmx_has_waitpkg(struct vcpu_vmx *vmx)
505 return vmx->secondary_exec_control &
506 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE;
509 void dump_vmcs(void);
511 #endif /* __KVM_X86_VMX_H */