1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __KVM_X86_VMX_CAPS_H
3 #define __KVM_X86_VMX_CAPS_H
10 extern bool __read_mostly enable_vpid;
11 extern bool __read_mostly flexpriority_enabled;
12 extern bool __read_mostly enable_ept;
13 extern bool __read_mostly enable_unrestricted_guest;
14 extern bool __read_mostly enable_ept_ad_bits;
15 extern bool __read_mostly enable_pml;
16 extern int __read_mostly pt_mode;
18 #define PT_MODE_SYSTEM 0
19 #define PT_MODE_HOST_GUEST 1
21 #define PMU_CAP_FW_WRITES (1ULL << 13)
22 #define PMU_CAP_LBR_FMT 0x3f
24 #define DEBUGCTLMSR_LBR_MASK (DEBUGCTLMSR_LBR | DEBUGCTLMSR_FREEZE_LBRS_ON_PMI)
26 struct nested_vmx_msrs {
28 * We only store the "true" versions of the VMX capability MSRs. We
29 * generate the "non-true" versions by setting the must-be-1 bits
30 * according to the SDM.
32 u32 procbased_ctls_low;
33 u32 procbased_ctls_high;
34 u32 secondary_ctls_low;
35 u32 secondary_ctls_high;
36 u32 pinbased_ctls_low;
37 u32 pinbased_ctls_high;
60 u32 pin_based_exec_ctrl;
61 u32 cpu_based_exec_ctrl;
62 u32 cpu_based_2nd_exec_ctrl;
65 struct nested_vmx_msrs nested;
67 extern struct vmcs_config vmcs_config;
69 struct vmx_capability {
73 extern struct vmx_capability vmx_capability;
75 static inline bool cpu_has_vmx_basic_inout(void)
77 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
80 static inline bool cpu_has_virtual_nmis(void)
82 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
85 static inline bool cpu_has_vmx_preemption_timer(void)
87 return vmcs_config.pin_based_exec_ctrl &
88 PIN_BASED_VMX_PREEMPTION_TIMER;
91 static inline bool cpu_has_vmx_posted_intr(void)
93 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
96 static inline bool cpu_has_load_ia32_efer(void)
98 return (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_EFER) &&
99 (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_EFER);
102 static inline bool cpu_has_load_perf_global_ctrl(void)
104 return (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) &&
105 (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
108 static inline bool cpu_has_vmx_mpx(void)
110 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
111 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
114 static inline bool cpu_has_vmx_tpr_shadow(void)
116 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
119 static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
121 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
124 static inline bool cpu_has_vmx_msr_bitmap(void)
126 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
129 static inline bool cpu_has_secondary_exec_ctrls(void)
131 return vmcs_config.cpu_based_exec_ctrl &
132 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
135 static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
137 return vmcs_config.cpu_based_2nd_exec_ctrl &
138 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
141 static inline bool cpu_has_vmx_ept(void)
143 return vmcs_config.cpu_based_2nd_exec_ctrl &
144 SECONDARY_EXEC_ENABLE_EPT;
147 static inline bool vmx_umip_emulated(void)
149 return vmcs_config.cpu_based_2nd_exec_ctrl &
153 static inline bool cpu_has_vmx_rdtscp(void)
155 return vmcs_config.cpu_based_2nd_exec_ctrl &
156 SECONDARY_EXEC_ENABLE_RDTSCP;
159 static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
161 return vmcs_config.cpu_based_2nd_exec_ctrl &
162 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
165 static inline bool cpu_has_vmx_vpid(void)
167 return vmcs_config.cpu_based_2nd_exec_ctrl &
168 SECONDARY_EXEC_ENABLE_VPID;
171 static inline bool cpu_has_vmx_wbinvd_exit(void)
173 return vmcs_config.cpu_based_2nd_exec_ctrl &
174 SECONDARY_EXEC_WBINVD_EXITING;
177 static inline bool cpu_has_vmx_unrestricted_guest(void)
179 return vmcs_config.cpu_based_2nd_exec_ctrl &
180 SECONDARY_EXEC_UNRESTRICTED_GUEST;
183 static inline bool cpu_has_vmx_apic_register_virt(void)
185 return vmcs_config.cpu_based_2nd_exec_ctrl &
186 SECONDARY_EXEC_APIC_REGISTER_VIRT;
189 static inline bool cpu_has_vmx_virtual_intr_delivery(void)
191 return vmcs_config.cpu_based_2nd_exec_ctrl &
192 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
195 static inline bool cpu_has_vmx_ple(void)
197 return vmcs_config.cpu_based_2nd_exec_ctrl &
198 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
201 static inline bool cpu_has_vmx_rdrand(void)
203 return vmcs_config.cpu_based_2nd_exec_ctrl &
204 SECONDARY_EXEC_RDRAND_EXITING;
207 static inline bool cpu_has_vmx_invpcid(void)
209 return vmcs_config.cpu_based_2nd_exec_ctrl &
210 SECONDARY_EXEC_ENABLE_INVPCID;
213 static inline bool cpu_has_vmx_vmfunc(void)
215 return vmcs_config.cpu_based_2nd_exec_ctrl &
216 SECONDARY_EXEC_ENABLE_VMFUNC;
219 static inline bool cpu_has_vmx_shadow_vmcs(void)
223 /* check if the cpu supports writing r/o exit information fields */
224 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
225 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
228 return vmcs_config.cpu_based_2nd_exec_ctrl &
229 SECONDARY_EXEC_SHADOW_VMCS;
232 static inline bool cpu_has_vmx_encls_vmexit(void)
234 return vmcs_config.cpu_based_2nd_exec_ctrl &
235 SECONDARY_EXEC_ENCLS_EXITING;
238 static inline bool cpu_has_vmx_rdseed(void)
240 return vmcs_config.cpu_based_2nd_exec_ctrl &
241 SECONDARY_EXEC_RDSEED_EXITING;
244 static inline bool cpu_has_vmx_pml(void)
246 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
249 static inline bool cpu_has_vmx_xsaves(void)
251 return vmcs_config.cpu_based_2nd_exec_ctrl &
252 SECONDARY_EXEC_XSAVES;
255 static inline bool cpu_has_vmx_waitpkg(void)
257 return vmcs_config.cpu_based_2nd_exec_ctrl &
258 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE;
261 static inline bool cpu_has_vmx_tsc_scaling(void)
263 return vmcs_config.cpu_based_2nd_exec_ctrl &
264 SECONDARY_EXEC_TSC_SCALING;
267 static inline bool cpu_has_vmx_bus_lock_detection(void)
269 return vmcs_config.cpu_based_2nd_exec_ctrl &
270 SECONDARY_EXEC_BUS_LOCK_DETECTION;
273 static inline bool cpu_has_vmx_apicv(void)
275 return cpu_has_vmx_apic_register_virt() &&
276 cpu_has_vmx_virtual_intr_delivery() &&
277 cpu_has_vmx_posted_intr();
280 static inline bool cpu_has_vmx_flexpriority(void)
282 return cpu_has_vmx_tpr_shadow() &&
283 cpu_has_vmx_virtualize_apic_accesses();
286 static inline bool cpu_has_vmx_ept_execute_only(void)
288 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
291 static inline bool cpu_has_vmx_ept_4levels(void)
293 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
296 static inline bool cpu_has_vmx_ept_5levels(void)
298 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
301 static inline bool cpu_has_vmx_ept_mt_wb(void)
303 return vmx_capability.ept & VMX_EPTP_WB_BIT;
306 static inline bool cpu_has_vmx_ept_2m_page(void)
308 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
311 static inline bool cpu_has_vmx_ept_1g_page(void)
313 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
316 static inline int ept_caps_to_lpage_level(u32 ept_caps)
318 if (ept_caps & VMX_EPT_1GB_PAGE_BIT)
320 if (ept_caps & VMX_EPT_2MB_PAGE_BIT)
325 static inline bool cpu_has_vmx_ept_ad_bits(void)
327 return vmx_capability.ept & VMX_EPT_AD_BIT;
330 static inline bool cpu_has_vmx_invept_context(void)
332 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
335 static inline bool cpu_has_vmx_invept_global(void)
337 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
340 static inline bool cpu_has_vmx_invvpid(void)
342 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
345 static inline bool cpu_has_vmx_invvpid_individual_addr(void)
347 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
350 static inline bool cpu_has_vmx_invvpid_single(void)
352 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
355 static inline bool cpu_has_vmx_invvpid_global(void)
357 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
360 static inline bool cpu_has_vmx_intel_pt(void)
364 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
365 return (vmx_msr & MSR_IA32_VMX_MISC_INTEL_PT) &&
366 (vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_PT_USE_GPA) &&
367 (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_IA32_RTIT_CTL) &&
368 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_RTIT_CTL);
372 * Processor Trace can operate in one of three modes:
373 * a. system-wide: trace both host/guest and output to host buffer
374 * b. host-only: only trace host and output to host buffer
375 * c. host-guest: trace host and guest simultaneously and output to their
378 * KVM currently only supports (a) and (c).
380 static inline bool vmx_pt_mode_is_system(void)
382 return pt_mode == PT_MODE_SYSTEM;
384 static inline bool vmx_pt_mode_is_host_guest(void)
386 return pt_mode == PT_MODE_HOST_GUEST;
389 static inline u64 vmx_get_perf_capabilities(void)
396 if (boot_cpu_has(X86_FEATURE_PDCM))
397 rdmsrl(MSR_IA32_PERF_CAPABILITIES, perf_cap);
399 perf_cap &= PMU_CAP_LBR_FMT;
402 * Since counters are virtualized, KVM would support full
403 * width counting unconditionally, even if the host lacks it.
405 return PMU_CAP_FW_WRITES | perf_cap;
408 static inline u64 vmx_supported_debugctl(void)
412 if (boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT))
413 debugctl |= DEBUGCTLMSR_BUS_LOCK_DETECT;
415 if (vmx_get_perf_capabilities() & PMU_CAP_LBR_FMT)
416 debugctl |= DEBUGCTLMSR_LBR_MASK;
421 #endif /* __KVM_X86_VMX_CAPS_H */