1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __KVM_X86_VMX_CAPS_H
3 #define __KVM_X86_VMX_CAPS_H
12 extern bool __read_mostly enable_vpid;
13 extern bool __read_mostly flexpriority_enabled;
14 extern bool __read_mostly enable_ept;
15 extern bool __read_mostly enable_unrestricted_guest;
16 extern bool __read_mostly enable_ept_ad_bits;
17 extern bool __read_mostly enable_pml;
18 extern bool __read_mostly enable_ipiv;
19 extern int __read_mostly pt_mode;
21 #define PT_MODE_SYSTEM 0
22 #define PT_MODE_HOST_GUEST 1
24 #define PMU_CAP_FW_WRITES (1ULL << 13)
25 #define PMU_CAP_LBR_FMT 0x3f
27 struct nested_vmx_msrs {
29 * We only store the "true" versions of the VMX capability MSRs. We
30 * generate the "non-true" versions by setting the must-be-1 bits
31 * according to the SDM.
33 u32 procbased_ctls_low;
34 u32 procbased_ctls_high;
35 u32 secondary_ctls_low;
36 u32 secondary_ctls_high;
37 u32 pinbased_ctls_low;
38 u32 pinbased_ctls_high;
60 u32 pin_based_exec_ctrl;
61 u32 cpu_based_exec_ctrl;
62 u32 cpu_based_2nd_exec_ctrl;
63 u64 cpu_based_3rd_exec_ctrl;
67 struct nested_vmx_msrs nested;
69 extern struct vmcs_config vmcs_config __ro_after_init;
71 struct vmx_capability {
75 extern struct vmx_capability vmx_capability __ro_after_init;
77 static inline bool cpu_has_vmx_basic_inout(void)
79 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
82 static inline bool cpu_has_virtual_nmis(void)
84 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS &&
85 vmcs_config.cpu_based_exec_ctrl & CPU_BASED_NMI_WINDOW_EXITING;
88 static inline bool cpu_has_vmx_preemption_timer(void)
90 return vmcs_config.pin_based_exec_ctrl &
91 PIN_BASED_VMX_PREEMPTION_TIMER;
94 static inline bool cpu_has_vmx_posted_intr(void)
96 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
99 static inline bool cpu_has_load_ia32_efer(void)
101 return vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_EFER;
104 static inline bool cpu_has_load_perf_global_ctrl(void)
106 return vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
109 static inline bool cpu_has_vmx_mpx(void)
111 return 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_tertiary_exec_ctrls(void)
137 return vmcs_config.cpu_based_exec_ctrl &
138 CPU_BASED_ACTIVATE_TERTIARY_CONTROLS;
141 static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
143 return vmcs_config.cpu_based_2nd_exec_ctrl &
144 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
147 static inline bool cpu_has_vmx_ept(void)
149 return vmcs_config.cpu_based_2nd_exec_ctrl &
150 SECONDARY_EXEC_ENABLE_EPT;
153 static inline bool vmx_umip_emulated(void)
155 return vmcs_config.cpu_based_2nd_exec_ctrl &
159 static inline bool cpu_has_vmx_rdtscp(void)
161 return vmcs_config.cpu_based_2nd_exec_ctrl &
162 SECONDARY_EXEC_ENABLE_RDTSCP;
165 static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
167 return vmcs_config.cpu_based_2nd_exec_ctrl &
168 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
171 static inline bool cpu_has_vmx_vpid(void)
173 return vmcs_config.cpu_based_2nd_exec_ctrl &
174 SECONDARY_EXEC_ENABLE_VPID;
177 static inline bool cpu_has_vmx_wbinvd_exit(void)
179 return vmcs_config.cpu_based_2nd_exec_ctrl &
180 SECONDARY_EXEC_WBINVD_EXITING;
183 static inline bool cpu_has_vmx_unrestricted_guest(void)
185 return vmcs_config.cpu_based_2nd_exec_ctrl &
186 SECONDARY_EXEC_UNRESTRICTED_GUEST;
189 static inline bool cpu_has_vmx_apic_register_virt(void)
191 return vmcs_config.cpu_based_2nd_exec_ctrl &
192 SECONDARY_EXEC_APIC_REGISTER_VIRT;
195 static inline bool cpu_has_vmx_virtual_intr_delivery(void)
197 return vmcs_config.cpu_based_2nd_exec_ctrl &
198 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
201 static inline bool cpu_has_vmx_ple(void)
203 return vmcs_config.cpu_based_2nd_exec_ctrl &
204 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
207 static inline bool cpu_has_vmx_rdrand(void)
209 return vmcs_config.cpu_based_2nd_exec_ctrl &
210 SECONDARY_EXEC_RDRAND_EXITING;
213 static inline bool cpu_has_vmx_invpcid(void)
215 return vmcs_config.cpu_based_2nd_exec_ctrl &
216 SECONDARY_EXEC_ENABLE_INVPCID;
219 static inline bool cpu_has_vmx_vmfunc(void)
221 return vmcs_config.cpu_based_2nd_exec_ctrl &
222 SECONDARY_EXEC_ENABLE_VMFUNC;
225 static inline bool cpu_has_vmx_shadow_vmcs(void)
227 /* check if the cpu supports writing r/o exit information fields */
228 if (!(vmcs_config.misc & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
231 return vmcs_config.cpu_based_2nd_exec_ctrl &
232 SECONDARY_EXEC_SHADOW_VMCS;
235 static inline bool cpu_has_vmx_encls_vmexit(void)
237 return vmcs_config.cpu_based_2nd_exec_ctrl &
238 SECONDARY_EXEC_ENCLS_EXITING;
241 static inline bool cpu_has_vmx_rdseed(void)
243 return vmcs_config.cpu_based_2nd_exec_ctrl &
244 SECONDARY_EXEC_RDSEED_EXITING;
247 static inline bool cpu_has_vmx_pml(void)
249 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
252 static inline bool cpu_has_vmx_xsaves(void)
254 return vmcs_config.cpu_based_2nd_exec_ctrl &
255 SECONDARY_EXEC_XSAVES;
258 static inline bool cpu_has_vmx_waitpkg(void)
260 return vmcs_config.cpu_based_2nd_exec_ctrl &
261 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE;
264 static inline bool cpu_has_vmx_tsc_scaling(void)
266 return vmcs_config.cpu_based_2nd_exec_ctrl &
267 SECONDARY_EXEC_TSC_SCALING;
270 static inline bool cpu_has_vmx_bus_lock_detection(void)
272 return vmcs_config.cpu_based_2nd_exec_ctrl &
273 SECONDARY_EXEC_BUS_LOCK_DETECTION;
276 static inline bool cpu_has_vmx_apicv(void)
278 return cpu_has_vmx_apic_register_virt() &&
279 cpu_has_vmx_virtual_intr_delivery() &&
280 cpu_has_vmx_posted_intr();
283 static inline bool cpu_has_vmx_ipiv(void)
285 return vmcs_config.cpu_based_3rd_exec_ctrl & TERTIARY_EXEC_IPI_VIRT;
288 static inline bool cpu_has_vmx_flexpriority(void)
290 return cpu_has_vmx_tpr_shadow() &&
291 cpu_has_vmx_virtualize_apic_accesses();
294 static inline bool cpu_has_vmx_ept_execute_only(void)
296 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
299 static inline bool cpu_has_vmx_ept_4levels(void)
301 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
304 static inline bool cpu_has_vmx_ept_5levels(void)
306 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
309 static inline bool cpu_has_vmx_ept_mt_wb(void)
311 return vmx_capability.ept & VMX_EPTP_WB_BIT;
314 static inline bool cpu_has_vmx_ept_2m_page(void)
316 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
319 static inline bool cpu_has_vmx_ept_1g_page(void)
321 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
324 static inline int ept_caps_to_lpage_level(u32 ept_caps)
326 if (ept_caps & VMX_EPT_1GB_PAGE_BIT)
328 if (ept_caps & VMX_EPT_2MB_PAGE_BIT)
333 static inline bool cpu_has_vmx_ept_ad_bits(void)
335 return vmx_capability.ept & VMX_EPT_AD_BIT;
338 static inline bool cpu_has_vmx_invept_context(void)
340 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
343 static inline bool cpu_has_vmx_invept_global(void)
345 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
348 static inline bool cpu_has_vmx_invvpid(void)
350 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
353 static inline bool cpu_has_vmx_invvpid_individual_addr(void)
355 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
358 static inline bool cpu_has_vmx_invvpid_single(void)
360 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
363 static inline bool cpu_has_vmx_invvpid_global(void)
365 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
368 static inline bool cpu_has_vmx_intel_pt(void)
370 return (vmcs_config.misc & MSR_IA32_VMX_MISC_INTEL_PT) &&
371 (vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_PT_USE_GPA) &&
372 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_RTIT_CTL);
376 * Processor Trace can operate in one of three modes:
377 * a. system-wide: trace both host/guest and output to host buffer
378 * b. host-only: only trace host and output to host buffer
379 * c. host-guest: trace host and guest simultaneously and output to their
382 * KVM currently only supports (a) and (c).
384 static inline bool vmx_pt_mode_is_system(void)
386 return pt_mode == PT_MODE_SYSTEM;
388 static inline bool vmx_pt_mode_is_host_guest(void)
390 return pt_mode == PT_MODE_HOST_GUEST;
393 static inline bool vmx_pebs_supported(void)
395 return boot_cpu_has(X86_FEATURE_PEBS) && kvm_pmu_cap.pebs_ept;
398 static inline bool cpu_has_notify_vmexit(void)
400 return vmcs_config.cpu_based_2nd_exec_ctrl &
401 SECONDARY_EXEC_NOTIFY_VM_EXITING;
404 #endif /* __KVM_X86_VMX_CAPS_H */