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"
11 #include "kvm_cache_regs.h"
12 #include "posted_intr.h"
17 extern const u32 vmx_msr_index[];
23 #define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
26 #define MAX_NR_USER_RETURN_MSRS 7
28 #define MAX_NR_USER_RETURN_MSRS 4
31 #define MAX_NR_LOADSTORE_MSRS 8
35 struct vmx_msr_entry val[MAX_NR_LOADSTORE_MSRS];
39 unsigned int slot; /* The MSR's slot in kvm_user_return_msrs. */
44 enum segment_cache_field {
53 #define RTIT_ADDR_RANGE 4
61 u64 addr_a[RTIT_ADDR_RANGE];
62 u64 addr_b[RTIT_ADDR_RANGE];
68 u32 caps[PT_CPUID_REGS_NUM * PT_CPUID_LEAVES];
73 union vmx_exit_reason {
86 u32 bus_lock_detected : 1;
88 u32 smi_pending_mtf : 1;
89 u32 smi_from_vmx_root : 1;
91 u32 failed_vmentry : 1;
97 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
98 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
101 /* Has the level1 guest done vmxon? */
106 /* The guest-physical address of the current VMCS L1 keeps for L2 */
109 * Cache of the guest's VMCS, existing outside of guest memory.
110 * Loaded from guest memory during VMPTRLD. Flushed to guest
111 * memory during VMCLEAR and VMPTRLD.
113 struct vmcs12 *cached_vmcs12;
115 * Cache of the guest's shadow VMCS, existing outside of guest
116 * memory. Loaded from guest memory during VM entry. Flushed
117 * to guest memory during VM exit.
119 struct vmcs12 *cached_shadow_vmcs12;
122 * Indicates if the shadow vmcs or enlightened vmcs must be updated
123 * with the data held by struct vmcs12.
125 bool need_vmcs12_to_shadow_sync;
129 * Indicates lazily loaded guest state has not yet been decached from
132 bool need_sync_vmcs02_to_vmcs12_rare;
135 * vmcs02 has been initialized, i.e. state that is constant for
136 * vmcs02 has been written to the backing VMCS. Initialization
137 * is delayed until L1 actually attempts to run a nested VM.
139 bool vmcs02_initialized;
141 bool change_vmcs01_virtual_apic_mode;
142 bool reload_vmcs01_apic_access_page;
145 * Enlightened VMCS has been enabled. It does not mean that L1 has to
146 * use it. However, VMX features available to L1 will be limited based
147 * on what the enlightened VMCS supports.
149 bool enlightened_vmcs_enabled;
151 /* L2 must run next, and mustn't decide to exit to L1. */
152 bool nested_run_pending;
154 /* Pending MTF VM-exit into L1. */
157 struct loaded_vmcs vmcs02;
160 * Guest pages referred to in the vmcs02 with host-physical
161 * pointers, so we must keep them pinned while L2 runs.
163 struct page *apic_access_page;
164 struct kvm_host_map virtual_apic_map;
165 struct kvm_host_map pi_desc_map;
167 struct kvm_host_map msr_bitmap_map;
169 struct pi_desc *pi_desc;
173 struct hrtimer preemption_timer;
174 u64 preemption_timer_deadline;
175 bool has_preemption_timer_deadline;
176 bool preemption_timer_expired;
178 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
180 u64 vmcs01_guest_bndcfgs;
182 /* to migrate it to L1 if L2 writes to L1's CR8 directly */
183 int l1_tpr_threshold;
188 struct nested_vmx_msrs msrs;
190 /* SMM related state */
192 /* in VMX operation on SMM entry? */
194 /* in guest mode on SMM entry? */
198 gpa_t hv_evmcs_vmptr;
199 struct kvm_host_map hv_evmcs_map;
200 struct hv_enlightened_vmcs *hv_evmcs;
204 struct kvm_vcpu vcpu;
209 * If true, host state has been stored in vmx->loaded_vmcs for
210 * the CPU registers that only need to be switched when transitioning
211 * to/from the kernel, and the registers have been loaded with guest
212 * values. If false, host state is loaded in the CPU registers
213 * and vmx->loaded_vmcs->host_state is invalid.
215 bool guest_state_loaded;
217 unsigned long exit_qualification;
219 u32 idt_vectoring_info;
222 struct vmx_uret_msr guest_uret_msrs[MAX_NR_USER_RETURN_MSRS];
224 int nr_active_uret_msrs;
225 bool guest_uret_msrs_loaded;
227 u64 msr_host_kernel_gs_base;
228 u64 msr_guest_kernel_gs_base;
232 u32 msr_ia32_umwait_control;
234 u32 secondary_exec_control;
237 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
238 * non-nested (L1) guest, it always points to vmcs01. For a nested
239 * guest (L2), it points to a different VMCS.
241 struct loaded_vmcs vmcs01;
242 struct loaded_vmcs *loaded_vmcs;
244 struct msr_autoload {
245 struct vmx_msrs guest;
246 struct vmx_msrs host;
249 struct msr_autostore {
250 struct vmx_msrs guest;
256 struct kvm_segment segs[8];
259 u32 bitmask; /* 4 bits per segment (1 bit per field) */
260 struct kvm_save_segment {
268 bool emulation_required;
270 union vmx_exit_reason exit_reason;
272 /* Posted interrupt descriptor */
273 struct pi_desc pi_desc;
275 /* Support for a guest hypervisor (nested VMX) */
276 struct nested_vmx nested;
278 /* Dynamic PLE window. */
279 unsigned int ple_window;
280 bool ple_window_dirty;
282 bool req_immediate_exit;
284 /* Support for PML */
285 #define PML_ENTITY_NUM 512
288 /* apic deadline value in host tsc */
291 u64 current_tsc_ratio;
293 unsigned long host_debugctlmsr;
296 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
297 * msr_ia32_feature_control. FEAT_CTL_LOCKED is always included
298 * in msr_ia32_feature_control_valid_bits.
300 u64 msr_ia32_feature_control;
301 u64 msr_ia32_feature_control_valid_bits;
304 struct pt_desc pt_desc;
306 /* Save desired MSR intercept (read: pass-through) state */
307 #define MAX_POSSIBLE_PASSTHROUGH_MSRS 13
309 DECLARE_BITMAP(read, MAX_POSSIBLE_PASSTHROUGH_MSRS);
310 DECLARE_BITMAP(write, MAX_POSSIBLE_PASSTHROUGH_MSRS);
311 } shadow_msr_intercept;
314 enum ept_pointers_status {
315 EPT_POINTERS_CHECK = 0,
316 EPT_POINTERS_MATCH = 1,
317 EPT_POINTERS_MISMATCH = 2
323 unsigned int tss_addr;
324 bool ept_identity_pagetable_done;
325 gpa_t ept_identity_map_addr;
327 enum ept_pointers_status ept_pointers_match;
328 spinlock_t ept_pointer_lock;
331 bool nested_vmx_allowed(struct kvm_vcpu *vcpu);
332 void vmx_vcpu_load_vmcs(struct kvm_vcpu *vcpu, int cpu,
333 struct loaded_vmcs *buddy);
334 int allocate_vpid(void);
335 void free_vpid(int vpid);
336 void vmx_set_constant_host_state(struct vcpu_vmx *vmx);
337 void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu);
338 void vmx_set_host_fs_gs(struct vmcs_host_state *host, u16 fs_sel, u16 gs_sel,
339 unsigned long fs_base, unsigned long gs_base);
340 int vmx_get_cpl(struct kvm_vcpu *vcpu);
341 unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu);
342 void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags);
343 u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu);
344 void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask);
345 int vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer);
346 void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
347 void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
348 void set_cr4_guest_host_mask(struct vcpu_vmx *vmx);
349 void ept_save_pdptrs(struct kvm_vcpu *vcpu);
350 void vmx_get_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg);
351 void vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg);
352 u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa,
355 void update_exception_bitmap(struct kvm_vcpu *vcpu);
356 void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
357 bool vmx_nmi_blocked(struct kvm_vcpu *vcpu);
358 bool vmx_interrupt_blocked(struct kvm_vcpu *vcpu);
359 bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
360 void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
361 void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu);
362 struct vmx_uret_msr *vmx_find_uret_msr(struct vcpu_vmx *vmx, u32 msr);
363 void pt_update_intercept_for_msr(struct kvm_vcpu *vcpu);
364 void vmx_update_host_rsp(struct vcpu_vmx *vmx, unsigned long host_rsp);
365 int vmx_find_loadstore_msr_slot(struct vmx_msrs *m, u32 msr);
366 void vmx_ept_load_pdptrs(struct kvm_vcpu *vcpu);
367 void vmx_set_intercept_for_msr(struct kvm_vcpu *vcpu,
368 u32 msr, int type, bool value);
370 static inline u8 vmx_get_rvi(void)
372 return vmcs_read16(GUEST_INTR_STATUS) & 0xff;
375 #define BUILD_CONTROLS_SHADOW(lname, uname) \
376 static inline void lname##_controls_set(struct vcpu_vmx *vmx, u32 val) \
378 if (vmx->loaded_vmcs->controls_shadow.lname != val) { \
379 vmcs_write32(uname, val); \
380 vmx->loaded_vmcs->controls_shadow.lname = val; \
383 static inline u32 lname##_controls_get(struct vcpu_vmx *vmx) \
385 return vmx->loaded_vmcs->controls_shadow.lname; \
387 static inline void lname##_controls_setbit(struct vcpu_vmx *vmx, u32 val) \
389 lname##_controls_set(vmx, lname##_controls_get(vmx) | val); \
391 static inline void lname##_controls_clearbit(struct vcpu_vmx *vmx, u32 val) \
393 lname##_controls_set(vmx, lname##_controls_get(vmx) & ~val); \
395 BUILD_CONTROLS_SHADOW(vm_entry, VM_ENTRY_CONTROLS)
396 BUILD_CONTROLS_SHADOW(vm_exit, VM_EXIT_CONTROLS)
397 BUILD_CONTROLS_SHADOW(pin, PIN_BASED_VM_EXEC_CONTROL)
398 BUILD_CONTROLS_SHADOW(exec, CPU_BASED_VM_EXEC_CONTROL)
399 BUILD_CONTROLS_SHADOW(secondary_exec, SECONDARY_VM_EXEC_CONTROL)
401 static inline void vmx_register_cache_reset(struct kvm_vcpu *vcpu)
403 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
404 | (1 << VCPU_EXREG_RFLAGS)
405 | (1 << VCPU_EXREG_PDPTR)
406 | (1 << VCPU_EXREG_SEGMENTS)
407 | (1 << VCPU_EXREG_CR0)
408 | (1 << VCPU_EXREG_CR3)
409 | (1 << VCPU_EXREG_CR4)
410 | (1 << VCPU_EXREG_EXIT_INFO_1)
411 | (1 << VCPU_EXREG_EXIT_INFO_2));
412 vcpu->arch.regs_dirty = 0;
415 static inline u32 vmx_vmentry_ctrl(void)
417 u32 vmentry_ctrl = vmcs_config.vmentry_ctrl;
418 if (vmx_pt_mode_is_system())
419 vmentry_ctrl &= ~(VM_ENTRY_PT_CONCEAL_PIP |
420 VM_ENTRY_LOAD_IA32_RTIT_CTL);
421 /* Loading of EFER and PERF_GLOBAL_CTRL are toggled dynamically */
422 return vmentry_ctrl &
423 ~(VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL | VM_ENTRY_LOAD_IA32_EFER);
426 static inline u32 vmx_vmexit_ctrl(void)
428 u32 vmexit_ctrl = vmcs_config.vmexit_ctrl;
429 if (vmx_pt_mode_is_system())
430 vmexit_ctrl &= ~(VM_EXIT_PT_CONCEAL_PIP |
431 VM_EXIT_CLEAR_IA32_RTIT_CTL);
432 /* Loading of EFER and PERF_GLOBAL_CTRL are toggled dynamically */
434 ~(VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL | VM_EXIT_LOAD_IA32_EFER);
437 u32 vmx_exec_control(struct vcpu_vmx *vmx);
438 u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx);
440 static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
442 return container_of(kvm, struct kvm_vmx, kvm);
445 static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
447 return container_of(vcpu, struct vcpu_vmx, vcpu);
450 static inline unsigned long vmx_get_exit_qual(struct kvm_vcpu *vcpu)
452 struct vcpu_vmx *vmx = to_vmx(vcpu);
454 if (!kvm_register_is_available(vcpu, VCPU_EXREG_EXIT_INFO_1)) {
455 kvm_register_mark_available(vcpu, VCPU_EXREG_EXIT_INFO_1);
456 vmx->exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
458 return vmx->exit_qualification;
461 static inline u32 vmx_get_intr_info(struct kvm_vcpu *vcpu)
463 struct vcpu_vmx *vmx = to_vmx(vcpu);
465 if (!kvm_register_is_available(vcpu, VCPU_EXREG_EXIT_INFO_2)) {
466 kvm_register_mark_available(vcpu, VCPU_EXREG_EXIT_INFO_2);
467 vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
469 return vmx->exit_intr_info;
472 struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu, gfp_t flags);
473 void free_vmcs(struct vmcs *vmcs);
474 int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs);
475 void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs);
476 void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs);
478 static inline struct vmcs *alloc_vmcs(bool shadow)
480 return alloc_vmcs_cpu(shadow, raw_smp_processor_id(),
484 static inline void decache_tsc_multiplier(struct vcpu_vmx *vmx)
486 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
487 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
490 static inline bool vmx_has_waitpkg(struct vcpu_vmx *vmx)
492 return vmx->secondary_exec_control &
493 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE;
496 static inline bool vmx_need_pf_intercept(struct kvm_vcpu *vcpu)
501 return allow_smaller_maxphyaddr && cpuid_maxphyaddr(vcpu) < boot_cpu_data.x86_phys_bits;
504 static inline bool is_unrestricted_guest(struct kvm_vcpu *vcpu)
506 return enable_unrestricted_guest && (!is_guest_mode(vcpu) ||
507 (secondary_exec_controls_get(to_vmx(vcpu)) &
508 SECONDARY_EXEC_UNRESTRICTED_GUEST));
511 bool __vmx_guest_state_valid(struct kvm_vcpu *vcpu);
512 static inline bool vmx_guest_state_valid(struct kvm_vcpu *vcpu)
514 return is_unrestricted_guest(vcpu) || __vmx_guest_state_valid(vcpu);
517 void dump_vmcs(void);
519 #endif /* __KVM_X86_VMX_H */