capabilities.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __KVM_X86_VMX_CAPS_H
  3. #define __KVM_X86_VMX_CAPS_H
  4. #include <asm/vmx.h>
  5. #include "../lapic.h"
  6. #include "../x86.h"
  7. #include "../pmu.h"
  8. #include "../cpuid.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_ipiv;
  16. extern int __read_mostly pt_mode;
  17. #define PT_MODE_SYSTEM 0
  18. #define PT_MODE_HOST_GUEST 1
  19. #define PMU_CAP_FW_WRITES (1ULL << 13)
  20. #define PMU_CAP_LBR_FMT 0x3f
  21. struct nested_vmx_msrs {
  22. /*
  23. * We only store the "true" versions of the VMX capability MSRs. We
  24. * generate the "non-true" versions by setting the must-be-1 bits
  25. * according to the SDM.
  26. */
  27. u32 procbased_ctls_low;
  28. u32 procbased_ctls_high;
  29. u32 secondary_ctls_low;
  30. u32 secondary_ctls_high;
  31. u32 pinbased_ctls_low;
  32. u32 pinbased_ctls_high;
  33. u32 exit_ctls_low;
  34. u32 exit_ctls_high;
  35. u32 entry_ctls_low;
  36. u32 entry_ctls_high;
  37. u32 misc_low;
  38. u32 misc_high;
  39. u32 ept_caps;
  40. u32 vpid_caps;
  41. u64 basic;
  42. u64 cr0_fixed0;
  43. u64 cr0_fixed1;
  44. u64 cr4_fixed0;
  45. u64 cr4_fixed1;
  46. u64 vmcs_enum;
  47. u64 vmfunc_controls;
  48. };
  49. struct vmcs_config {
  50. int size;
  51. u32 basic_cap;
  52. u32 revision_id;
  53. u32 pin_based_exec_ctrl;
  54. u32 cpu_based_exec_ctrl;
  55. u32 cpu_based_2nd_exec_ctrl;
  56. u64 cpu_based_3rd_exec_ctrl;
  57. u32 vmexit_ctrl;
  58. u32 vmentry_ctrl;
  59. u64 misc;
  60. struct nested_vmx_msrs nested;
  61. };
  62. extern struct vmcs_config vmcs_config;
  63. struct vmx_capability {
  64. u32 ept;
  65. u32 vpid;
  66. };
  67. extern struct vmx_capability vmx_capability;
  68. static inline bool cpu_has_vmx_basic_inout(void)
  69. {
  70. return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
  71. }
  72. static inline bool cpu_has_virtual_nmis(void)
  73. {
  74. return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS &&
  75. vmcs_config.cpu_based_exec_ctrl & CPU_BASED_NMI_WINDOW_EXITING;
  76. }
  77. static inline bool cpu_has_vmx_preemption_timer(void)
  78. {
  79. return vmcs_config.pin_based_exec_ctrl &
  80. PIN_BASED_VMX_PREEMPTION_TIMER;
  81. }
  82. static inline bool cpu_has_vmx_posted_intr(void)
  83. {
  84. return vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
  85. }
  86. static inline bool cpu_has_load_ia32_efer(void)
  87. {
  88. return vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_EFER;
  89. }
  90. static inline bool cpu_has_load_perf_global_ctrl(void)
  91. {
  92. return vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
  93. }
  94. static inline bool cpu_has_vmx_mpx(void)
  95. {
  96. return vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS;
  97. }
  98. static inline bool cpu_has_vmx_tpr_shadow(void)
  99. {
  100. return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
  101. }
  102. static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
  103. {
  104. return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
  105. }
  106. static inline bool cpu_has_vmx_msr_bitmap(void)
  107. {
  108. return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
  109. }
  110. static inline bool cpu_has_secondary_exec_ctrls(void)
  111. {
  112. return vmcs_config.cpu_based_exec_ctrl &
  113. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  114. }
  115. static inline bool cpu_has_tertiary_exec_ctrls(void)
  116. {
  117. return vmcs_config.cpu_based_exec_ctrl &
  118. CPU_BASED_ACTIVATE_TERTIARY_CONTROLS;
  119. }
  120. static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
  121. {
  122. return vmcs_config.cpu_based_2nd_exec_ctrl &
  123. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  124. }
  125. static inline bool cpu_has_vmx_ept(void)
  126. {
  127. return vmcs_config.cpu_based_2nd_exec_ctrl &
  128. SECONDARY_EXEC_ENABLE_EPT;
  129. }
  130. static inline bool vmx_umip_emulated(void)
  131. {
  132. return vmcs_config.cpu_based_2nd_exec_ctrl &
  133. SECONDARY_EXEC_DESC;
  134. }
  135. static inline bool cpu_has_vmx_rdtscp(void)
  136. {
  137. return vmcs_config.cpu_based_2nd_exec_ctrl &
  138. SECONDARY_EXEC_ENABLE_RDTSCP;
  139. }
  140. static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
  141. {
  142. return vmcs_config.cpu_based_2nd_exec_ctrl &
  143. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  144. }
  145. static inline bool cpu_has_vmx_vpid(void)
  146. {
  147. return vmcs_config.cpu_based_2nd_exec_ctrl &
  148. SECONDARY_EXEC_ENABLE_VPID;
  149. }
  150. static inline bool cpu_has_vmx_wbinvd_exit(void)
  151. {
  152. return vmcs_config.cpu_based_2nd_exec_ctrl &
  153. SECONDARY_EXEC_WBINVD_EXITING;
  154. }
  155. static inline bool cpu_has_vmx_unrestricted_guest(void)
  156. {
  157. return vmcs_config.cpu_based_2nd_exec_ctrl &
  158. SECONDARY_EXEC_UNRESTRICTED_GUEST;
  159. }
  160. static inline bool cpu_has_vmx_apic_register_virt(void)
  161. {
  162. return vmcs_config.cpu_based_2nd_exec_ctrl &
  163. SECONDARY_EXEC_APIC_REGISTER_VIRT;
  164. }
  165. static inline bool cpu_has_vmx_virtual_intr_delivery(void)
  166. {
  167. return vmcs_config.cpu_based_2nd_exec_ctrl &
  168. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
  169. }
  170. static inline bool cpu_has_vmx_ple(void)
  171. {
  172. return vmcs_config.cpu_based_2nd_exec_ctrl &
  173. SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  174. }
  175. static inline bool cpu_has_vmx_rdrand(void)
  176. {
  177. return vmcs_config.cpu_based_2nd_exec_ctrl &
  178. SECONDARY_EXEC_RDRAND_EXITING;
  179. }
  180. static inline bool cpu_has_vmx_invpcid(void)
  181. {
  182. return vmcs_config.cpu_based_2nd_exec_ctrl &
  183. SECONDARY_EXEC_ENABLE_INVPCID;
  184. }
  185. static inline bool cpu_has_vmx_vmfunc(void)
  186. {
  187. return vmcs_config.cpu_based_2nd_exec_ctrl &
  188. SECONDARY_EXEC_ENABLE_VMFUNC;
  189. }
  190. static inline bool cpu_has_vmx_shadow_vmcs(void)
  191. {
  192. /* check if the cpu supports writing r/o exit information fields */
  193. if (!(vmcs_config.misc & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
  194. return false;
  195. return vmcs_config.cpu_based_2nd_exec_ctrl &
  196. SECONDARY_EXEC_SHADOW_VMCS;
  197. }
  198. static inline bool cpu_has_vmx_encls_vmexit(void)
  199. {
  200. return vmcs_config.cpu_based_2nd_exec_ctrl &
  201. SECONDARY_EXEC_ENCLS_EXITING;
  202. }
  203. static inline bool cpu_has_vmx_rdseed(void)
  204. {
  205. return vmcs_config.cpu_based_2nd_exec_ctrl &
  206. SECONDARY_EXEC_RDSEED_EXITING;
  207. }
  208. static inline bool cpu_has_vmx_pml(void)
  209. {
  210. return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
  211. }
  212. static inline bool cpu_has_vmx_xsaves(void)
  213. {
  214. return vmcs_config.cpu_based_2nd_exec_ctrl &
  215. SECONDARY_EXEC_XSAVES;
  216. }
  217. static inline bool cpu_has_vmx_waitpkg(void)
  218. {
  219. return vmcs_config.cpu_based_2nd_exec_ctrl &
  220. SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE;
  221. }
  222. static inline bool cpu_has_vmx_tsc_scaling(void)
  223. {
  224. return vmcs_config.cpu_based_2nd_exec_ctrl &
  225. SECONDARY_EXEC_TSC_SCALING;
  226. }
  227. static inline bool cpu_has_vmx_bus_lock_detection(void)
  228. {
  229. return vmcs_config.cpu_based_2nd_exec_ctrl &
  230. SECONDARY_EXEC_BUS_LOCK_DETECTION;
  231. }
  232. static inline bool cpu_has_vmx_apicv(void)
  233. {
  234. return cpu_has_vmx_apic_register_virt() &&
  235. cpu_has_vmx_virtual_intr_delivery() &&
  236. cpu_has_vmx_posted_intr();
  237. }
  238. static inline bool cpu_has_vmx_ipiv(void)
  239. {
  240. return vmcs_config.cpu_based_3rd_exec_ctrl & TERTIARY_EXEC_IPI_VIRT;
  241. }
  242. static inline bool cpu_has_vmx_flexpriority(void)
  243. {
  244. return cpu_has_vmx_tpr_shadow() &&
  245. cpu_has_vmx_virtualize_apic_accesses();
  246. }
  247. static inline bool cpu_has_vmx_ept_execute_only(void)
  248. {
  249. return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
  250. }
  251. static inline bool cpu_has_vmx_ept_4levels(void)
  252. {
  253. return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
  254. }
  255. static inline bool cpu_has_vmx_ept_5levels(void)
  256. {
  257. return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
  258. }
  259. static inline bool cpu_has_vmx_ept_mt_wb(void)
  260. {
  261. return vmx_capability.ept & VMX_EPTP_WB_BIT;
  262. }
  263. static inline bool cpu_has_vmx_ept_2m_page(void)
  264. {
  265. return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
  266. }
  267. static inline bool cpu_has_vmx_ept_1g_page(void)
  268. {
  269. return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
  270. }
  271. static inline int ept_caps_to_lpage_level(u32 ept_caps)
  272. {
  273. if (ept_caps & VMX_EPT_1GB_PAGE_BIT)
  274. return PG_LEVEL_1G;
  275. if (ept_caps & VMX_EPT_2MB_PAGE_BIT)
  276. return PG_LEVEL_2M;
  277. return PG_LEVEL_4K;
  278. }
  279. static inline bool cpu_has_vmx_ept_ad_bits(void)
  280. {
  281. return vmx_capability.ept & VMX_EPT_AD_BIT;
  282. }
  283. static inline bool cpu_has_vmx_invept_context(void)
  284. {
  285. return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
  286. }
  287. static inline bool cpu_has_vmx_invept_global(void)
  288. {
  289. return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
  290. }
  291. static inline bool cpu_has_vmx_invvpid(void)
  292. {
  293. return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
  294. }
  295. static inline bool cpu_has_vmx_invvpid_individual_addr(void)
  296. {
  297. return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
  298. }
  299. static inline bool cpu_has_vmx_invvpid_single(void)
  300. {
  301. return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
  302. }
  303. static inline bool cpu_has_vmx_invvpid_global(void)
  304. {
  305. return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
  306. }
  307. static inline bool cpu_has_vmx_intel_pt(void)
  308. {
  309. return (vmcs_config.misc & MSR_IA32_VMX_MISC_INTEL_PT) &&
  310. (vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_PT_USE_GPA) &&
  311. (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_RTIT_CTL);
  312. }
  313. /*
  314. * Processor Trace can operate in one of three modes:
  315. * a. system-wide: trace both host/guest and output to host buffer
  316. * b. host-only: only trace host and output to host buffer
  317. * c. host-guest: trace host and guest simultaneously and output to their
  318. * respective buffer
  319. *
  320. * KVM currently only supports (a) and (c).
  321. */
  322. static inline bool vmx_pt_mode_is_system(void)
  323. {
  324. return pt_mode == PT_MODE_SYSTEM;
  325. }
  326. static inline bool vmx_pt_mode_is_host_guest(void)
  327. {
  328. return pt_mode == PT_MODE_HOST_GUEST;
  329. }
  330. static inline bool vmx_pebs_supported(void)
  331. {
  332. return boot_cpu_has(X86_FEATURE_PEBS) && kvm_pmu_cap.pebs_ept;
  333. }
  334. static inline bool cpu_has_notify_vmexit(void)
  335. {
  336. return vmcs_config.cpu_based_2nd_exec_ctrl &
  337. SECONDARY_EXEC_NOTIFY_VM_EXITING;
  338. }
  339. #endif /* __KVM_X86_VMX_CAPS_H */