1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __KVM_X86_VMX_CAPS_H
3 #define __KVM_X86_VMX_CAPS_H
9 extern bool __read_mostly enable_vpid;
10 extern bool __read_mostly flexpriority_enabled;
11 extern bool __read_mostly enable_ept;
12 extern bool __read_mostly enable_unrestricted_guest;
13 extern bool __read_mostly enable_ept_ad_bits;
14 extern bool __read_mostly enable_pml;
15 extern bool __read_mostly enable_apicv;
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)
23 struct nested_vmx_msrs {
25 * We only store the "true" versions of the VMX capability MSRs. We
26 * generate the "non-true" versions by setting the must-be-1 bits
27 * according to the SDM.
29 u32 procbased_ctls_low;
30 u32 procbased_ctls_high;
31 u32 secondary_ctls_low;
32 u32 secondary_ctls_high;
33 u32 pinbased_ctls_low;
34 u32 pinbased_ctls_high;
57 u32 pin_based_exec_ctrl;
58 u32 cpu_based_exec_ctrl;
59 u32 cpu_based_2nd_exec_ctrl;
62 struct nested_vmx_msrs nested;
64 extern struct vmcs_config vmcs_config;
66 struct vmx_capability {
70 extern struct vmx_capability vmx_capability;
72 static inline bool cpu_has_vmx_basic_inout(void)
74 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
77 static inline bool cpu_has_virtual_nmis(void)
79 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
82 static inline bool cpu_has_vmx_preemption_timer(void)
84 return vmcs_config.pin_based_exec_ctrl &
85 PIN_BASED_VMX_PREEMPTION_TIMER;
88 static inline bool cpu_has_vmx_posted_intr(void)
90 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
91 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
94 static inline bool cpu_has_load_ia32_efer(void)
96 return (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_EFER) &&
97 (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_EFER);
100 static inline bool cpu_has_load_perf_global_ctrl(void)
102 return (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) &&
103 (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
106 static inline bool cpu_has_vmx_mpx(void)
108 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
109 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
112 static inline bool cpu_has_vmx_tpr_shadow(void)
114 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
117 static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
119 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
122 static inline bool cpu_has_vmx_msr_bitmap(void)
124 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
127 static inline bool cpu_has_secondary_exec_ctrls(void)
129 return vmcs_config.cpu_based_exec_ctrl &
130 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
133 static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
135 return vmcs_config.cpu_based_2nd_exec_ctrl &
136 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
139 static inline bool cpu_has_vmx_ept(void)
141 return vmcs_config.cpu_based_2nd_exec_ctrl &
142 SECONDARY_EXEC_ENABLE_EPT;
145 static inline bool vmx_umip_emulated(void)
147 return vmcs_config.cpu_based_2nd_exec_ctrl &
151 static inline bool cpu_has_vmx_rdtscp(void)
153 return vmcs_config.cpu_based_2nd_exec_ctrl &
154 SECONDARY_EXEC_RDTSCP;
157 static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
159 return vmcs_config.cpu_based_2nd_exec_ctrl &
160 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
163 static inline bool cpu_has_vmx_vpid(void)
165 return vmcs_config.cpu_based_2nd_exec_ctrl &
166 SECONDARY_EXEC_ENABLE_VPID;
169 static inline bool cpu_has_vmx_wbinvd_exit(void)
171 return vmcs_config.cpu_based_2nd_exec_ctrl &
172 SECONDARY_EXEC_WBINVD_EXITING;
175 static inline bool cpu_has_vmx_unrestricted_guest(void)
177 return vmcs_config.cpu_based_2nd_exec_ctrl &
178 SECONDARY_EXEC_UNRESTRICTED_GUEST;
181 static inline bool cpu_has_vmx_apic_register_virt(void)
183 return vmcs_config.cpu_based_2nd_exec_ctrl &
184 SECONDARY_EXEC_APIC_REGISTER_VIRT;
187 static inline bool cpu_has_vmx_virtual_intr_delivery(void)
189 return vmcs_config.cpu_based_2nd_exec_ctrl &
190 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
193 static inline bool cpu_has_vmx_ple(void)
195 return vmcs_config.cpu_based_2nd_exec_ctrl &
196 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
199 static inline bool vmx_rdrand_supported(void)
201 return vmcs_config.cpu_based_2nd_exec_ctrl &
202 SECONDARY_EXEC_RDRAND_EXITING;
205 static inline bool cpu_has_vmx_invpcid(void)
207 return vmcs_config.cpu_based_2nd_exec_ctrl &
208 SECONDARY_EXEC_ENABLE_INVPCID;
211 static inline bool cpu_has_vmx_vmfunc(void)
213 return vmcs_config.cpu_based_2nd_exec_ctrl &
214 SECONDARY_EXEC_ENABLE_VMFUNC;
217 static inline bool cpu_has_vmx_shadow_vmcs(void)
221 /* check if the cpu supports writing r/o exit information fields */
222 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
223 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
226 return vmcs_config.cpu_based_2nd_exec_ctrl &
227 SECONDARY_EXEC_SHADOW_VMCS;
230 static inline bool cpu_has_vmx_encls_vmexit(void)
232 return vmcs_config.cpu_based_2nd_exec_ctrl &
233 SECONDARY_EXEC_ENCLS_EXITING;
236 static inline bool vmx_rdseed_supported(void)
238 return vmcs_config.cpu_based_2nd_exec_ctrl &
239 SECONDARY_EXEC_RDSEED_EXITING;
242 static inline bool cpu_has_vmx_pml(void)
244 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
247 static inline bool vmx_xsaves_supported(void)
249 return vmcs_config.cpu_based_2nd_exec_ctrl &
250 SECONDARY_EXEC_XSAVES;
253 static inline bool vmx_waitpkg_supported(void)
255 return vmcs_config.cpu_based_2nd_exec_ctrl &
256 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE;
259 static inline bool cpu_has_vmx_tsc_scaling(void)
261 return vmcs_config.cpu_based_2nd_exec_ctrl &
262 SECONDARY_EXEC_TSC_SCALING;
265 static inline bool cpu_has_vmx_apicv(void)
267 return cpu_has_vmx_apic_register_virt() &&
268 cpu_has_vmx_virtual_intr_delivery() &&
269 cpu_has_vmx_posted_intr();
272 static inline bool cpu_has_vmx_flexpriority(void)
274 return cpu_has_vmx_tpr_shadow() &&
275 cpu_has_vmx_virtualize_apic_accesses();
278 static inline bool cpu_has_vmx_ept_execute_only(void)
280 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
283 static inline bool cpu_has_vmx_ept_4levels(void)
285 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
288 static inline bool cpu_has_vmx_ept_5levels(void)
290 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
293 static inline bool cpu_has_vmx_ept_mt_wb(void)
295 return vmx_capability.ept & VMX_EPTP_WB_BIT;
298 static inline bool cpu_has_vmx_ept_2m_page(void)
300 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
303 static inline bool cpu_has_vmx_ept_1g_page(void)
305 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
308 static inline bool cpu_has_vmx_ept_ad_bits(void)
310 return vmx_capability.ept & VMX_EPT_AD_BIT;
313 static inline bool cpu_has_vmx_invept_context(void)
315 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
318 static inline bool cpu_has_vmx_invept_global(void)
320 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
323 static inline bool cpu_has_vmx_invvpid(void)
325 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
328 static inline bool cpu_has_vmx_invvpid_individual_addr(void)
330 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
333 static inline bool cpu_has_vmx_invvpid_single(void)
335 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
338 static inline bool cpu_has_vmx_invvpid_global(void)
340 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
343 static inline bool cpu_has_vmx_intel_pt(void)
347 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
348 return (vmx_msr & MSR_IA32_VMX_MISC_INTEL_PT) &&
349 (vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_PT_USE_GPA) &&
350 (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_IA32_RTIT_CTL) &&
351 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_RTIT_CTL);
355 * Processor Trace can operate in one of three modes:
356 * a. system-wide: trace both host/guest and output to host buffer
357 * b. host-only: only trace host and output to host buffer
358 * c. host-guest: trace host and guest simultaneously and output to their
361 * KVM currently only supports (a) and (c).
363 static inline bool vmx_pt_mode_is_system(void)
365 return pt_mode == PT_MODE_SYSTEM;
367 static inline bool vmx_pt_mode_is_host_guest(void)
369 return pt_mode == PT_MODE_HOST_GUEST;
372 static inline u64 vmx_get_perf_capabilities(void)
375 * Since counters are virtualized, KVM would support full
376 * width counting unconditionally, even if the host lacks it.
378 return PMU_CAP_FW_WRITES;
381 #endif /* __KVM_X86_VMX_CAPS_H */