vmx.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __KVM_X86_VMX_H
  3. #define __KVM_X86_VMX_H
  4. #include <linux/kvm_host.h>
  5. #include <asm/kvm.h>
  6. #include <asm/intel_pt.h>
  7. #include <asm/perf_event.h>
  8. #include "capabilities.h"
  9. #include "../kvm_cache_regs.h"
  10. #include "posted_intr.h"
  11. #include "vmcs.h"
  12. #include "vmx_ops.h"
  13. #include "../cpuid.h"
  14. #include "run_flags.h"
  15. #define MSR_TYPE_R 1
  16. #define MSR_TYPE_W 2
  17. #define MSR_TYPE_RW 3
  18. #define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
  19. #ifdef CONFIG_X86_64
  20. #define MAX_NR_USER_RETURN_MSRS 7
  21. #else
  22. #define MAX_NR_USER_RETURN_MSRS 4
  23. #endif
  24. #define MAX_NR_LOADSTORE_MSRS 8
  25. struct vmx_msrs {
  26. unsigned int nr;
  27. struct vmx_msr_entry val[MAX_NR_LOADSTORE_MSRS];
  28. };
  29. struct vmx_uret_msr {
  30. bool load_into_hardware;
  31. u64 data;
  32. u64 mask;
  33. };
  34. enum segment_cache_field {
  35. SEG_FIELD_SEL = 0,
  36. SEG_FIELD_BASE = 1,
  37. SEG_FIELD_LIMIT = 2,
  38. SEG_FIELD_AR = 3,
  39. SEG_FIELD_NR = 4
  40. };
  41. #define RTIT_ADDR_RANGE 4
  42. struct pt_ctx {
  43. u64 ctl;
  44. u64 status;
  45. u64 output_base;
  46. u64 output_mask;
  47. u64 cr3_match;
  48. u64 addr_a[RTIT_ADDR_RANGE];
  49. u64 addr_b[RTIT_ADDR_RANGE];
  50. };
  51. struct pt_desc {
  52. u64 ctl_bitmask;
  53. u32 num_address_ranges;
  54. u32 caps[PT_CPUID_REGS_NUM * PT_CPUID_LEAVES];
  55. struct pt_ctx host;
  56. struct pt_ctx guest;
  57. };
  58. union vmx_exit_reason {
  59. struct {
  60. u32 basic : 16;
  61. u32 reserved16 : 1;
  62. u32 reserved17 : 1;
  63. u32 reserved18 : 1;
  64. u32 reserved19 : 1;
  65. u32 reserved20 : 1;
  66. u32 reserved21 : 1;
  67. u32 reserved22 : 1;
  68. u32 reserved23 : 1;
  69. u32 reserved24 : 1;
  70. u32 reserved25 : 1;
  71. u32 bus_lock_detected : 1;
  72. u32 enclave_mode : 1;
  73. u32 smi_pending_mtf : 1;
  74. u32 smi_from_vmx_root : 1;
  75. u32 reserved30 : 1;
  76. u32 failed_vmentry : 1;
  77. };
  78. u32 full;
  79. };
  80. static inline bool intel_pmu_has_perf_global_ctrl(struct kvm_pmu *pmu)
  81. {
  82. /*
  83. * Architecturally, Intel's SDM states that IA32_PERF_GLOBAL_CTRL is
  84. * supported if "CPUID.0AH: EAX[7:0] > 0", i.e. if the PMU version is
  85. * greater than zero. However, KVM only exposes and emulates the MSR
  86. * to/for the guest if the guest PMU supports at least "Architectural
  87. * Performance Monitoring Version 2".
  88. */
  89. return pmu->version > 1;
  90. }
  91. struct lbr_desc {
  92. /* Basic info about guest LBR records. */
  93. struct x86_pmu_lbr records;
  94. /*
  95. * Emulate LBR feature via passthrough LBR registers when the
  96. * per-vcpu guest LBR event is scheduled on the current pcpu.
  97. *
  98. * The records may be inaccurate if the host reclaims the LBR.
  99. */
  100. struct perf_event *event;
  101. /* True if LBRs are marked as not intercepted in the MSR bitmap */
  102. bool msr_passthrough;
  103. };
  104. /*
  105. * The nested_vmx structure is part of vcpu_vmx, and holds information we need
  106. * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
  107. */
  108. struct nested_vmx {
  109. /* Has the level1 guest done vmxon? */
  110. bool vmxon;
  111. gpa_t vmxon_ptr;
  112. bool pml_full;
  113. /* The guest-physical address of the current VMCS L1 keeps for L2 */
  114. gpa_t current_vmptr;
  115. /*
  116. * Cache of the guest's VMCS, existing outside of guest memory.
  117. * Loaded from guest memory during VMPTRLD. Flushed to guest
  118. * memory during VMCLEAR and VMPTRLD.
  119. */
  120. struct vmcs12 *cached_vmcs12;
  121. /*
  122. * Cache of the guest's shadow VMCS, existing outside of guest
  123. * memory. Loaded from guest memory during VM entry. Flushed
  124. * to guest memory during VM exit.
  125. */
  126. struct vmcs12 *cached_shadow_vmcs12;
  127. /*
  128. * GPA to HVA cache for accessing vmcs12->vmcs_link_pointer
  129. */
  130. struct gfn_to_hva_cache shadow_vmcs12_cache;
  131. /*
  132. * GPA to HVA cache for VMCS12
  133. */
  134. struct gfn_to_hva_cache vmcs12_cache;
  135. /*
  136. * Indicates if the shadow vmcs or enlightened vmcs must be updated
  137. * with the data held by struct vmcs12.
  138. */
  139. bool need_vmcs12_to_shadow_sync;
  140. bool dirty_vmcs12;
  141. /*
  142. * Indicates whether MSR bitmap for L2 needs to be rebuilt due to
  143. * changes in MSR bitmap for L1 or switching to a different L2. Note,
  144. * this flag can only be used reliably in conjunction with a paravirt L1
  145. * which informs L0 whether any changes to MSR bitmap for L2 were done
  146. * on its side.
  147. */
  148. bool force_msr_bitmap_recalc;
  149. /*
  150. * Indicates lazily loaded guest state has not yet been decached from
  151. * vmcs02.
  152. */
  153. bool need_sync_vmcs02_to_vmcs12_rare;
  154. /*
  155. * vmcs02 has been initialized, i.e. state that is constant for
  156. * vmcs02 has been written to the backing VMCS. Initialization
  157. * is delayed until L1 actually attempts to run a nested VM.
  158. */
  159. bool vmcs02_initialized;
  160. bool change_vmcs01_virtual_apic_mode;
  161. bool reload_vmcs01_apic_access_page;
  162. bool update_vmcs01_cpu_dirty_logging;
  163. bool update_vmcs01_apicv_status;
  164. /*
  165. * Enlightened VMCS has been enabled. It does not mean that L1 has to
  166. * use it. However, VMX features available to L1 will be limited based
  167. * on what the enlightened VMCS supports.
  168. */
  169. bool enlightened_vmcs_enabled;
  170. /* L2 must run next, and mustn't decide to exit to L1. */
  171. bool nested_run_pending;
  172. /* Pending MTF VM-exit into L1. */
  173. bool mtf_pending;
  174. struct loaded_vmcs vmcs02;
  175. /*
  176. * Guest pages referred to in the vmcs02 with host-physical
  177. * pointers, so we must keep them pinned while L2 runs.
  178. */
  179. struct kvm_host_map apic_access_page_map;
  180. struct kvm_host_map virtual_apic_map;
  181. struct kvm_host_map pi_desc_map;
  182. struct kvm_host_map msr_bitmap_map;
  183. struct pi_desc *pi_desc;
  184. bool pi_pending;
  185. u16 posted_intr_nv;
  186. struct hrtimer preemption_timer;
  187. u64 preemption_timer_deadline;
  188. bool has_preemption_timer_deadline;
  189. bool preemption_timer_expired;
  190. /*
  191. * Used to snapshot MSRs that are conditionally loaded on VM-Enter in
  192. * order to propagate the guest's pre-VM-Enter value into vmcs02. For
  193. * emulation of VMLAUNCH/VMRESUME, the snapshot will be of L1's value.
  194. * For KVM_SET_NESTED_STATE, the snapshot is of L2's value, _if_
  195. * userspace restores MSRs before nested state. If userspace restores
  196. * MSRs after nested state, the snapshot holds garbage, but KVM can't
  197. * detect that, and the garbage value in vmcs02 will be overwritten by
  198. * MSR restoration in any case.
  199. */
  200. u64 pre_vmenter_debugctl;
  201. u64 pre_vmenter_bndcfgs;
  202. /* to migrate it to L1 if L2 writes to L1's CR8 directly */
  203. int l1_tpr_threshold;
  204. u16 vpid02;
  205. u16 last_vpid;
  206. struct nested_vmx_msrs msrs;
  207. /* SMM related state */
  208. struct {
  209. /* in VMX operation on SMM entry? */
  210. bool vmxon;
  211. /* in guest mode on SMM entry? */
  212. bool guest_mode;
  213. } smm;
  214. gpa_t hv_evmcs_vmptr;
  215. struct kvm_host_map hv_evmcs_map;
  216. struct hv_enlightened_vmcs *hv_evmcs;
  217. };
  218. struct vcpu_vmx {
  219. struct kvm_vcpu vcpu;
  220. u8 fail;
  221. u8 x2apic_msr_bitmap_mode;
  222. /*
  223. * If true, host state has been stored in vmx->loaded_vmcs for
  224. * the CPU registers that only need to be switched when transitioning
  225. * to/from the kernel, and the registers have been loaded with guest
  226. * values. If false, host state is loaded in the CPU registers
  227. * and vmx->loaded_vmcs->host_state is invalid.
  228. */
  229. bool guest_state_loaded;
  230. unsigned long exit_qualification;
  231. u32 exit_intr_info;
  232. u32 idt_vectoring_info;
  233. ulong rflags;
  234. /*
  235. * User return MSRs are always emulated when enabled in the guest, but
  236. * only loaded into hardware when necessary, e.g. SYSCALL #UDs outside
  237. * of 64-bit mode or if EFER.SCE=1, thus the SYSCALL MSRs don't need to
  238. * be loaded into hardware if those conditions aren't met.
  239. */
  240. struct vmx_uret_msr guest_uret_msrs[MAX_NR_USER_RETURN_MSRS];
  241. bool guest_uret_msrs_loaded;
  242. #ifdef CONFIG_X86_64
  243. u64 msr_host_kernel_gs_base;
  244. u64 msr_guest_kernel_gs_base;
  245. #endif
  246. u64 spec_ctrl;
  247. u32 msr_ia32_umwait_control;
  248. /*
  249. * loaded_vmcs points to the VMCS currently used in this vcpu. For a
  250. * non-nested (L1) guest, it always points to vmcs01. For a nested
  251. * guest (L2), it points to a different VMCS.
  252. */
  253. struct loaded_vmcs vmcs01;
  254. struct loaded_vmcs *loaded_vmcs;
  255. struct msr_autoload {
  256. struct vmx_msrs guest;
  257. struct vmx_msrs host;
  258. } msr_autoload;
  259. struct msr_autostore {
  260. struct vmx_msrs guest;
  261. } msr_autostore;
  262. struct {
  263. int vm86_active;
  264. ulong save_rflags;
  265. struct kvm_segment segs[8];
  266. } rmode;
  267. struct {
  268. u32 bitmask; /* 4 bits per segment (1 bit per field) */
  269. struct kvm_save_segment {
  270. u16 selector;
  271. unsigned long base;
  272. u32 limit;
  273. u32 ar;
  274. } seg[8];
  275. } segment_cache;
  276. int vpid;
  277. bool emulation_required;
  278. union vmx_exit_reason exit_reason;
  279. /* Posted interrupt descriptor */
  280. struct pi_desc pi_desc;
  281. /* Used if this vCPU is waiting for PI notification wakeup. */
  282. struct list_head pi_wakeup_list;
  283. /* Support for a guest hypervisor (nested VMX) */
  284. struct nested_vmx nested;
  285. /* Dynamic PLE window. */
  286. unsigned int ple_window;
  287. bool ple_window_dirty;
  288. bool req_immediate_exit;
  289. /* Support for PML */
  290. #define PML_ENTITY_NUM 512
  291. struct page *pml_pg;
  292. /* apic deadline value in host tsc */
  293. u64 hv_deadline_tsc;
  294. unsigned long host_debugctlmsr;
  295. /*
  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.
  299. */
  300. u64 msr_ia32_feature_control;
  301. u64 msr_ia32_feature_control_valid_bits;
  302. /* SGX Launch Control public key hash */
  303. u64 msr_ia32_sgxlepubkeyhash[4];
  304. u64 msr_ia32_mcu_opt_ctrl;
  305. bool disable_fb_clear;
  306. struct pt_desc pt_desc;
  307. struct lbr_desc lbr_desc;
  308. /* Save desired MSR intercept (read: pass-through) state */
  309. #define MAX_POSSIBLE_PASSTHROUGH_MSRS 15
  310. struct {
  311. DECLARE_BITMAP(read, MAX_POSSIBLE_PASSTHROUGH_MSRS);
  312. DECLARE_BITMAP(write, MAX_POSSIBLE_PASSTHROUGH_MSRS);
  313. } shadow_msr_intercept;
  314. };
  315. struct kvm_vmx {
  316. struct kvm kvm;
  317. unsigned int tss_addr;
  318. bool ept_identity_pagetable_done;
  319. gpa_t ept_identity_map_addr;
  320. /* Posted Interrupt Descriptor (PID) table for IPI virtualization */
  321. u64 *pid_table;
  322. };
  323. bool nested_vmx_allowed(struct kvm_vcpu *vcpu);
  324. void vmx_vcpu_load_vmcs(struct kvm_vcpu *vcpu, int cpu,
  325. struct loaded_vmcs *buddy);
  326. int allocate_vpid(void);
  327. void free_vpid(int vpid);
  328. void vmx_set_constant_host_state(struct vcpu_vmx *vmx);
  329. void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu);
  330. void vmx_set_host_fs_gs(struct vmcs_host_state *host, u16 fs_sel, u16 gs_sel,
  331. unsigned long fs_base, unsigned long gs_base);
  332. int vmx_get_cpl(struct kvm_vcpu *vcpu);
  333. bool vmx_emulation_required(struct kvm_vcpu *vcpu);
  334. unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu);
  335. void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags);
  336. u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu);
  337. void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask);
  338. int vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer);
  339. void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
  340. void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
  341. void set_cr4_guest_host_mask(struct vcpu_vmx *vmx);
  342. void ept_save_pdptrs(struct kvm_vcpu *vcpu);
  343. void vmx_get_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg);
  344. void __vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg);
  345. u64 construct_eptp(struct kvm_vcpu *vcpu, hpa_t root_hpa, int root_level);
  346. bool vmx_guest_inject_ac(struct kvm_vcpu *vcpu);
  347. void vmx_update_exception_bitmap(struct kvm_vcpu *vcpu);
  348. bool vmx_nmi_blocked(struct kvm_vcpu *vcpu);
  349. bool vmx_interrupt_blocked(struct kvm_vcpu *vcpu);
  350. bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
  351. void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
  352. void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu);
  353. struct vmx_uret_msr *vmx_find_uret_msr(struct vcpu_vmx *vmx, u32 msr);
  354. void pt_update_intercept_for_msr(struct kvm_vcpu *vcpu);
  355. void vmx_update_host_rsp(struct vcpu_vmx *vmx, unsigned long host_rsp);
  356. void vmx_spec_ctrl_restore_host(struct vcpu_vmx *vmx, unsigned int flags);
  357. unsigned int __vmx_vcpu_run_flags(struct vcpu_vmx *vmx);
  358. bool __vmx_vcpu_run(struct vcpu_vmx *vmx, unsigned long *regs,
  359. unsigned int flags);
  360. int vmx_find_loadstore_msr_slot(struct vmx_msrs *m, u32 msr);
  361. void vmx_ept_load_pdptrs(struct kvm_vcpu *vcpu);
  362. void vmx_disable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type);
  363. void vmx_enable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type);
  364. u64 vmx_get_l2_tsc_offset(struct kvm_vcpu *vcpu);
  365. u64 vmx_get_l2_tsc_multiplier(struct kvm_vcpu *vcpu);
  366. static inline void vmx_set_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr,
  367. int type, bool value)
  368. {
  369. if (value)
  370. vmx_enable_intercept_for_msr(vcpu, msr, type);
  371. else
  372. vmx_disable_intercept_for_msr(vcpu, msr, type);
  373. }
  374. void vmx_update_cpu_dirty_logging(struct kvm_vcpu *vcpu);
  375. /*
  376. * Note, early Intel manuals have the write-low and read-high bitmap offsets
  377. * the wrong way round. The bitmaps control MSRs 0x00000000-0x00001fff and
  378. * 0xc0000000-0xc0001fff. The former (low) uses bytes 0-0x3ff for reads and
  379. * 0x800-0xbff for writes. The latter (high) uses 0x400-0x7ff for reads and
  380. * 0xc00-0xfff for writes. MSRs not covered by either of the ranges always
  381. * VM-Exit.
  382. */
  383. #define __BUILD_VMX_MSR_BITMAP_HELPER(rtype, action, bitop, access, base) \
  384. static inline rtype vmx_##action##_msr_bitmap_##access(unsigned long *bitmap, \
  385. u32 msr) \
  386. { \
  387. int f = sizeof(unsigned long); \
  388. \
  389. if (msr <= 0x1fff) \
  390. return bitop##_bit(msr, bitmap + base / f); \
  391. else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) \
  392. return bitop##_bit(msr & 0x1fff, bitmap + (base + 0x400) / f); \
  393. return (rtype)true; \
  394. }
  395. #define BUILD_VMX_MSR_BITMAP_HELPERS(ret_type, action, bitop) \
  396. __BUILD_VMX_MSR_BITMAP_HELPER(ret_type, action, bitop, read, 0x0) \
  397. __BUILD_VMX_MSR_BITMAP_HELPER(ret_type, action, bitop, write, 0x800)
  398. BUILD_VMX_MSR_BITMAP_HELPERS(bool, test, test)
  399. BUILD_VMX_MSR_BITMAP_HELPERS(void, clear, __clear)
  400. BUILD_VMX_MSR_BITMAP_HELPERS(void, set, __set)
  401. static inline u8 vmx_get_rvi(void)
  402. {
  403. return vmcs_read16(GUEST_INTR_STATUS) & 0xff;
  404. }
  405. #define __KVM_REQUIRED_VMX_VM_ENTRY_CONTROLS \
  406. (VM_ENTRY_LOAD_DEBUG_CONTROLS)
  407. #ifdef CONFIG_X86_64
  408. #define KVM_REQUIRED_VMX_VM_ENTRY_CONTROLS \
  409. (__KVM_REQUIRED_VMX_VM_ENTRY_CONTROLS | \
  410. VM_ENTRY_IA32E_MODE)
  411. #else
  412. #define KVM_REQUIRED_VMX_VM_ENTRY_CONTROLS \
  413. __KVM_REQUIRED_VMX_VM_ENTRY_CONTROLS
  414. #endif
  415. #define KVM_OPTIONAL_VMX_VM_ENTRY_CONTROLS \
  416. (VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL | \
  417. VM_ENTRY_LOAD_IA32_PAT | \
  418. VM_ENTRY_LOAD_IA32_EFER | \
  419. VM_ENTRY_LOAD_BNDCFGS | \
  420. VM_ENTRY_PT_CONCEAL_PIP | \
  421. VM_ENTRY_LOAD_IA32_RTIT_CTL)
  422. #define __KVM_REQUIRED_VMX_VM_EXIT_CONTROLS \
  423. (VM_EXIT_SAVE_DEBUG_CONTROLS | \
  424. VM_EXIT_ACK_INTR_ON_EXIT)
  425. #ifdef CONFIG_X86_64
  426. #define KVM_REQUIRED_VMX_VM_EXIT_CONTROLS \
  427. (__KVM_REQUIRED_VMX_VM_EXIT_CONTROLS | \
  428. VM_EXIT_HOST_ADDR_SPACE_SIZE)
  429. #else
  430. #define KVM_REQUIRED_VMX_VM_EXIT_CONTROLS \
  431. __KVM_REQUIRED_VMX_VM_EXIT_CONTROLS
  432. #endif
  433. #define KVM_OPTIONAL_VMX_VM_EXIT_CONTROLS \
  434. (VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL | \
  435. VM_EXIT_SAVE_IA32_PAT | \
  436. VM_EXIT_LOAD_IA32_PAT | \
  437. VM_EXIT_SAVE_IA32_EFER | \
  438. VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | \
  439. VM_EXIT_LOAD_IA32_EFER | \
  440. VM_EXIT_CLEAR_BNDCFGS | \
  441. VM_EXIT_PT_CONCEAL_PIP | \
  442. VM_EXIT_CLEAR_IA32_RTIT_CTL)
  443. #define KVM_REQUIRED_VMX_PIN_BASED_VM_EXEC_CONTROL \
  444. (PIN_BASED_EXT_INTR_MASK | \
  445. PIN_BASED_NMI_EXITING)
  446. #define KVM_OPTIONAL_VMX_PIN_BASED_VM_EXEC_CONTROL \
  447. (PIN_BASED_VIRTUAL_NMIS | \
  448. PIN_BASED_POSTED_INTR | \
  449. PIN_BASED_VMX_PREEMPTION_TIMER)
  450. #define __KVM_REQUIRED_VMX_CPU_BASED_VM_EXEC_CONTROL \
  451. (CPU_BASED_HLT_EXITING | \
  452. CPU_BASED_CR3_LOAD_EXITING | \
  453. CPU_BASED_CR3_STORE_EXITING | \
  454. CPU_BASED_UNCOND_IO_EXITING | \
  455. CPU_BASED_MOV_DR_EXITING | \
  456. CPU_BASED_USE_TSC_OFFSETTING | \
  457. CPU_BASED_MWAIT_EXITING | \
  458. CPU_BASED_MONITOR_EXITING | \
  459. CPU_BASED_INVLPG_EXITING | \
  460. CPU_BASED_RDPMC_EXITING | \
  461. CPU_BASED_INTR_WINDOW_EXITING)
  462. #ifdef CONFIG_X86_64
  463. #define KVM_REQUIRED_VMX_CPU_BASED_VM_EXEC_CONTROL \
  464. (__KVM_REQUIRED_VMX_CPU_BASED_VM_EXEC_CONTROL | \
  465. CPU_BASED_CR8_LOAD_EXITING | \
  466. CPU_BASED_CR8_STORE_EXITING)
  467. #else
  468. #define KVM_REQUIRED_VMX_CPU_BASED_VM_EXEC_CONTROL \
  469. __KVM_REQUIRED_VMX_CPU_BASED_VM_EXEC_CONTROL
  470. #endif
  471. #define KVM_OPTIONAL_VMX_CPU_BASED_VM_EXEC_CONTROL \
  472. (CPU_BASED_RDTSC_EXITING | \
  473. CPU_BASED_TPR_SHADOW | \
  474. CPU_BASED_USE_IO_BITMAPS | \
  475. CPU_BASED_MONITOR_TRAP_FLAG | \
  476. CPU_BASED_USE_MSR_BITMAPS | \
  477. CPU_BASED_NMI_WINDOW_EXITING | \
  478. CPU_BASED_PAUSE_EXITING | \
  479. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS | \
  480. CPU_BASED_ACTIVATE_TERTIARY_CONTROLS)
  481. #define KVM_REQUIRED_VMX_SECONDARY_VM_EXEC_CONTROL 0
  482. #define KVM_OPTIONAL_VMX_SECONDARY_VM_EXEC_CONTROL \
  483. (SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | \
  484. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | \
  485. SECONDARY_EXEC_WBINVD_EXITING | \
  486. SECONDARY_EXEC_ENABLE_VPID | \
  487. SECONDARY_EXEC_ENABLE_EPT | \
  488. SECONDARY_EXEC_UNRESTRICTED_GUEST | \
  489. SECONDARY_EXEC_PAUSE_LOOP_EXITING | \
  490. SECONDARY_EXEC_DESC | \
  491. SECONDARY_EXEC_ENABLE_RDTSCP | \
  492. SECONDARY_EXEC_ENABLE_INVPCID | \
  493. SECONDARY_EXEC_APIC_REGISTER_VIRT | \
  494. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | \
  495. SECONDARY_EXEC_SHADOW_VMCS | \
  496. SECONDARY_EXEC_XSAVES | \
  497. SECONDARY_EXEC_RDSEED_EXITING | \
  498. SECONDARY_EXEC_RDRAND_EXITING | \
  499. SECONDARY_EXEC_ENABLE_PML | \
  500. SECONDARY_EXEC_TSC_SCALING | \
  501. SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE | \
  502. SECONDARY_EXEC_PT_USE_GPA | \
  503. SECONDARY_EXEC_PT_CONCEAL_VMX | \
  504. SECONDARY_EXEC_ENABLE_VMFUNC | \
  505. SECONDARY_EXEC_BUS_LOCK_DETECTION | \
  506. SECONDARY_EXEC_NOTIFY_VM_EXITING | \
  507. SECONDARY_EXEC_ENCLS_EXITING)
  508. #define KVM_REQUIRED_VMX_TERTIARY_VM_EXEC_CONTROL 0
  509. #define KVM_OPTIONAL_VMX_TERTIARY_VM_EXEC_CONTROL \
  510. (TERTIARY_EXEC_IPI_VIRT)
  511. #define BUILD_CONTROLS_SHADOW(lname, uname, bits) \
  512. static inline void lname##_controls_set(struct vcpu_vmx *vmx, u##bits val) \
  513. { \
  514. if (vmx->loaded_vmcs->controls_shadow.lname != val) { \
  515. vmcs_write##bits(uname, val); \
  516. vmx->loaded_vmcs->controls_shadow.lname = val; \
  517. } \
  518. } \
  519. static inline u##bits __##lname##_controls_get(struct loaded_vmcs *vmcs) \
  520. { \
  521. return vmcs->controls_shadow.lname; \
  522. } \
  523. static inline u##bits lname##_controls_get(struct vcpu_vmx *vmx) \
  524. { \
  525. return __##lname##_controls_get(vmx->loaded_vmcs); \
  526. } \
  527. static __always_inline void lname##_controls_setbit(struct vcpu_vmx *vmx, u##bits val) \
  528. { \
  529. BUILD_BUG_ON(!(val & (KVM_REQUIRED_VMX_##uname | KVM_OPTIONAL_VMX_##uname))); \
  530. lname##_controls_set(vmx, lname##_controls_get(vmx) | val); \
  531. } \
  532. static __always_inline void lname##_controls_clearbit(struct vcpu_vmx *vmx, u##bits val) \
  533. { \
  534. BUILD_BUG_ON(!(val & (KVM_REQUIRED_VMX_##uname | KVM_OPTIONAL_VMX_##uname))); \
  535. lname##_controls_set(vmx, lname##_controls_get(vmx) & ~val); \
  536. }
  537. BUILD_CONTROLS_SHADOW(vm_entry, VM_ENTRY_CONTROLS, 32)
  538. BUILD_CONTROLS_SHADOW(vm_exit, VM_EXIT_CONTROLS, 32)
  539. BUILD_CONTROLS_SHADOW(pin, PIN_BASED_VM_EXEC_CONTROL, 32)
  540. BUILD_CONTROLS_SHADOW(exec, CPU_BASED_VM_EXEC_CONTROL, 32)
  541. BUILD_CONTROLS_SHADOW(secondary_exec, SECONDARY_VM_EXEC_CONTROL, 32)
  542. BUILD_CONTROLS_SHADOW(tertiary_exec, TERTIARY_VM_EXEC_CONTROL, 64)
  543. /*
  544. * VMX_REGS_LAZY_LOAD_SET - The set of registers that will be updated in the
  545. * cache on demand. Other registers not listed here are synced to
  546. * the cache immediately after VM-Exit.
  547. */
  548. #define VMX_REGS_LAZY_LOAD_SET ((1 << VCPU_REGS_RIP) | \
  549. (1 << VCPU_REGS_RSP) | \
  550. (1 << VCPU_EXREG_RFLAGS) | \
  551. (1 << VCPU_EXREG_PDPTR) | \
  552. (1 << VCPU_EXREG_SEGMENTS) | \
  553. (1 << VCPU_EXREG_CR0) | \
  554. (1 << VCPU_EXREG_CR3) | \
  555. (1 << VCPU_EXREG_CR4) | \
  556. (1 << VCPU_EXREG_EXIT_INFO_1) | \
  557. (1 << VCPU_EXREG_EXIT_INFO_2))
  558. static inline unsigned long vmx_l1_guest_owned_cr0_bits(void)
  559. {
  560. unsigned long bits = KVM_POSSIBLE_CR0_GUEST_BITS;
  561. /*
  562. * CR0.WP needs to be intercepted when KVM is shadowing legacy paging
  563. * in order to construct shadow PTEs with the correct protections.
  564. * Note! CR0.WP technically can be passed through to the guest if
  565. * paging is disabled, but checking CR0.PG would generate a cyclical
  566. * dependency of sorts due to forcing the caller to ensure CR0 holds
  567. * the correct value prior to determining which CR0 bits can be owned
  568. * by L1. Keep it simple and limit the optimization to EPT.
  569. */
  570. if (!enable_ept)
  571. bits &= ~X86_CR0_WP;
  572. return bits;
  573. }
  574. static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
  575. {
  576. return container_of(kvm, struct kvm_vmx, kvm);
  577. }
  578. static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
  579. {
  580. return container_of(vcpu, struct vcpu_vmx, vcpu);
  581. }
  582. static inline struct lbr_desc *vcpu_to_lbr_desc(struct kvm_vcpu *vcpu)
  583. {
  584. return &to_vmx(vcpu)->lbr_desc;
  585. }
  586. static inline struct x86_pmu_lbr *vcpu_to_lbr_records(struct kvm_vcpu *vcpu)
  587. {
  588. return &vcpu_to_lbr_desc(vcpu)->records;
  589. }
  590. static inline bool intel_pmu_lbr_is_enabled(struct kvm_vcpu *vcpu)
  591. {
  592. return !!vcpu_to_lbr_records(vcpu)->nr;
  593. }
  594. void intel_pmu_cross_mapped_check(struct kvm_pmu *pmu);
  595. int intel_pmu_create_guest_lbr_event(struct kvm_vcpu *vcpu);
  596. void vmx_passthrough_lbr_msrs(struct kvm_vcpu *vcpu);
  597. static inline unsigned long vmx_get_exit_qual(struct kvm_vcpu *vcpu)
  598. {
  599. struct vcpu_vmx *vmx = to_vmx(vcpu);
  600. if (!kvm_register_is_available(vcpu, VCPU_EXREG_EXIT_INFO_1)) {
  601. kvm_register_mark_available(vcpu, VCPU_EXREG_EXIT_INFO_1);
  602. vmx->exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  603. }
  604. return vmx->exit_qualification;
  605. }
  606. static inline u32 vmx_get_intr_info(struct kvm_vcpu *vcpu)
  607. {
  608. struct vcpu_vmx *vmx = to_vmx(vcpu);
  609. if (!kvm_register_is_available(vcpu, VCPU_EXREG_EXIT_INFO_2)) {
  610. kvm_register_mark_available(vcpu, VCPU_EXREG_EXIT_INFO_2);
  611. vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  612. }
  613. return vmx->exit_intr_info;
  614. }
  615. struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu, gfp_t flags);
  616. void free_vmcs(struct vmcs *vmcs);
  617. int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs);
  618. void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs);
  619. void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs);
  620. static inline struct vmcs *alloc_vmcs(bool shadow)
  621. {
  622. return alloc_vmcs_cpu(shadow, raw_smp_processor_id(),
  623. GFP_KERNEL_ACCOUNT);
  624. }
  625. static inline bool vmx_has_waitpkg(struct vcpu_vmx *vmx)
  626. {
  627. return secondary_exec_controls_get(vmx) &
  628. SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE;
  629. }
  630. static inline bool vmx_need_pf_intercept(struct kvm_vcpu *vcpu)
  631. {
  632. if (!enable_ept)
  633. return true;
  634. return allow_smaller_maxphyaddr && cpuid_maxphyaddr(vcpu) < boot_cpu_data.x86_phys_bits;
  635. }
  636. static inline bool is_unrestricted_guest(struct kvm_vcpu *vcpu)
  637. {
  638. return enable_unrestricted_guest && (!is_guest_mode(vcpu) ||
  639. (secondary_exec_controls_get(to_vmx(vcpu)) &
  640. SECONDARY_EXEC_UNRESTRICTED_GUEST));
  641. }
  642. bool __vmx_guest_state_valid(struct kvm_vcpu *vcpu);
  643. static inline bool vmx_guest_state_valid(struct kvm_vcpu *vcpu)
  644. {
  645. return is_unrestricted_guest(vcpu) || __vmx_guest_state_valid(vcpu);
  646. }
  647. void dump_vmcs(struct kvm_vcpu *vcpu);
  648. static inline int vmx_get_instr_info_reg2(u32 vmx_instr_info)
  649. {
  650. return (vmx_instr_info >> 28) & 0xf;
  651. }
  652. static inline bool vmx_can_use_ipiv(struct kvm_vcpu *vcpu)
  653. {
  654. return lapic_in_kernel(vcpu) && enable_ipiv;
  655. }
  656. static inline bool guest_cpuid_has_evmcs(struct kvm_vcpu *vcpu)
  657. {
  658. /*
  659. * eVMCS is exposed to the guest if Hyper-V is enabled in CPUID and
  660. * eVMCS has been explicitly enabled by userspace.
  661. */
  662. return vcpu->arch.hyperv_enabled &&
  663. to_vmx(vcpu)->nested.enlightened_vmcs_enabled;
  664. }
  665. #endif /* __KVM_X86_VMX_H */