blob: 64fe6c91435f05b5ef23e200008f4e955e7db6bd [file] [log] [blame]
Sean Christopherson8373d252018-12-03 13:53:08 -08001/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef __KVM_X86_VMX_H
3#define __KVM_X86_VMX_H
4
5#include <linux/kvm_host.h>
6
7#include <asm/kvm.h>
Chao Pengf99e3da2018-10-24 16:05:10 +08008#include <asm/intel_pt.h>
Sean Christopherson8373d252018-12-03 13:53:08 -08009
10#include "capabilities.h"
Sean Christophersone5d03de2020-04-15 13:34:51 -070011#include "kvm_cache_regs.h"
Xiaoyao Li8888cdd2020-09-23 11:31:11 -070012#include "posted_intr.h"
Sean Christopherson8373d252018-12-03 13:53:08 -080013#include "vmcs.h"
Sean Christopherson5a085322020-09-23 11:31:12 -070014#include "vmx_ops.h"
Mohammed Gamal1dbf5d682020-07-10 17:48:09 +020015#include "cpuid.h"
Sean Christopherson8373d252018-12-03 13:53:08 -080016
Sean Christophersoncf3646e2018-12-03 13:53:15 -080017extern const u32 vmx_msr_index[];
Sean Christophersoncf3646e2018-12-03 13:53:15 -080018
Sean Christopherson8373d252018-12-03 13:53:08 -080019#define MSR_TYPE_R 1
20#define MSR_TYPE_W 2
21#define MSR_TYPE_RW 3
22
23#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
24
Jim Mattson7d737102019-12-03 16:24:42 -080025#ifdef CONFIG_X86_64
Sean Christophersoneb3db1b2020-09-23 11:03:58 -070026#define MAX_NR_USER_RETURN_MSRS 7
Jim Mattson7d737102019-12-03 16:24:42 -080027#else
Sean Christophersoneb3db1b2020-09-23 11:03:58 -070028#define MAX_NR_USER_RETURN_MSRS 4
Jim Mattson7d737102019-12-03 16:24:42 -080029#endif
30
Sean Christophersonce833b22020-09-23 11:03:56 -070031#define MAX_NR_LOADSTORE_MSRS 8
Sean Christopherson8373d252018-12-03 13:53:08 -080032
33struct vmx_msrs {
34 unsigned int nr;
Sean Christophersonce833b22020-09-23 11:03:56 -070035 struct vmx_msr_entry val[MAX_NR_LOADSTORE_MSRS];
Sean Christopherson8373d252018-12-03 13:53:08 -080036};
37
Sean Christophersoneb3db1b2020-09-23 11:03:58 -070038struct vmx_uret_msr {
Sean Christopherson802145c2020-09-23 11:04:09 -070039 unsigned int slot; /* The MSR's slot in kvm_user_return_msrs. */
Sean Christopherson8373d252018-12-03 13:53:08 -080040 u64 data;
41 u64 mask;
42};
43
44enum segment_cache_field {
45 SEG_FIELD_SEL = 0,
46 SEG_FIELD_BASE = 1,
47 SEG_FIELD_LIMIT = 2,
48 SEG_FIELD_AR = 3,
49
50 SEG_FIELD_NR = 4
51};
52
Chao Peng2ef444f2018-10-24 16:05:12 +080053#define RTIT_ADDR_RANGE 4
54
55struct pt_ctx {
56 u64 ctl;
57 u64 status;
58 u64 output_base;
59 u64 output_mask;
60 u64 cr3_match;
61 u64 addr_a[RTIT_ADDR_RANGE];
62 u64 addr_b[RTIT_ADDR_RANGE];
63};
64
65struct pt_desc {
66 u64 ctl_bitmask;
67 u32 addr_range;
68 u32 caps[PT_CPUID_REGS_NUM * PT_CPUID_LEAVES];
69 struct pt_ctx host;
70 struct pt_ctx guest;
71};
Sean Christopherson8373d252018-12-03 13:53:08 -080072
73/*
74 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
75 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
76 */
77struct nested_vmx {
78 /* Has the level1 guest done vmxon? */
79 bool vmxon;
80 gpa_t vmxon_ptr;
81 bool pml_full;
82
83 /* The guest-physical address of the current VMCS L1 keeps for L2 */
84 gpa_t current_vmptr;
85 /*
86 * Cache of the guest's VMCS, existing outside of guest memory.
87 * Loaded from guest memory during VMPTRLD. Flushed to guest
88 * memory during VMCLEAR and VMPTRLD.
89 */
90 struct vmcs12 *cached_vmcs12;
91 /*
92 * Cache of the guest's shadow VMCS, existing outside of guest
93 * memory. Loaded from guest memory during VM entry. Flushed
94 * to guest memory during VM exit.
95 */
96 struct vmcs12 *cached_shadow_vmcs12;
Sean Christopherson7952d762019-05-07 08:36:29 -070097
Sean Christopherson8373d252018-12-03 13:53:08 -080098 /*
99 * Indicates if the shadow vmcs or enlightened vmcs must be updated
100 * with the data held by struct vmcs12.
101 */
Sean Christopherson3731905ef2019-05-07 08:36:27 -0700102 bool need_vmcs12_to_shadow_sync;
Sean Christopherson8373d252018-12-03 13:53:08 -0800103 bool dirty_vmcs12;
104
105 /*
Sean Christopherson7952d762019-05-07 08:36:29 -0700106 * Indicates lazily loaded guest state has not yet been decached from
107 * vmcs02.
108 */
109 bool need_sync_vmcs02_to_vmcs12_rare;
110
111 /*
Sean Christopherson8373d252018-12-03 13:53:08 -0800112 * vmcs02 has been initialized, i.e. state that is constant for
113 * vmcs02 has been written to the backing VMCS. Initialization
114 * is delayed until L1 actually attempts to run a nested VM.
115 */
116 bool vmcs02_initialized;
117
118 bool change_vmcs01_virtual_apic_mode;
Sean Christopherson1196cb92020-03-20 14:28:23 -0700119 bool reload_vmcs01_apic_access_page;
Sean Christopherson8373d252018-12-03 13:53:08 -0800120
121 /*
122 * Enlightened VMCS has been enabled. It does not mean that L1 has to
123 * use it. However, VMX features available to L1 will be limited based
124 * on what the enlightened VMCS supports.
125 */
126 bool enlightened_vmcs_enabled;
127
128 /* L2 must run next, and mustn't decide to exit to L1. */
129 bool nested_run_pending;
130
Oliver Upton5ef8acb2020-02-07 02:36:07 -0800131 /* Pending MTF VM-exit into L1. */
132 bool mtf_pending;
133
Sean Christopherson8373d252018-12-03 13:53:08 -0800134 struct loaded_vmcs vmcs02;
135
136 /*
137 * Guest pages referred to in the vmcs02 with host-physical
138 * pointers, so we must keep them pinned while L2 runs.
139 */
140 struct page *apic_access_page;
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +0100141 struct kvm_host_map virtual_apic_map;
KarimAllah Ahmed3278e042019-01-31 21:24:38 +0100142 struct kvm_host_map pi_desc_map;
KarimAllah Ahmed31f0b6c2019-01-31 21:24:36 +0100143
144 struct kvm_host_map msr_bitmap_map;
145
Sean Christopherson8373d252018-12-03 13:53:08 -0800146 struct pi_desc *pi_desc;
147 bool pi_pending;
148 u16 posted_intr_nv;
149
150 struct hrtimer preemption_timer;
Peter Shier850448f2020-05-26 14:51:06 -0700151 u64 preemption_timer_deadline;
152 bool has_preemption_timer_deadline;
Sean Christopherson8373d252018-12-03 13:53:08 -0800153 bool preemption_timer_expired;
154
155 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
156 u64 vmcs01_debugctl;
157 u64 vmcs01_guest_bndcfgs;
158
Liran Alon02d496cf2019-11-11 14:30:55 +0200159 /* to migrate it to L1 if L2 writes to L1's CR8 directly */
160 int l1_tpr_threshold;
161
Sean Christopherson8373d252018-12-03 13:53:08 -0800162 u16 vpid02;
163 u16 last_vpid;
164
165 struct nested_vmx_msrs msrs;
166
167 /* SMM related state */
168 struct {
169 /* in VMX operation on SMM entry? */
170 bool vmxon;
171 /* in guest mode on SMM entry? */
172 bool guest_mode;
173 } smm;
174
175 gpa_t hv_evmcs_vmptr;
KarimAllah Ahmeddee9c042019-01-31 21:24:42 +0100176 struct kvm_host_map hv_evmcs_map;
Sean Christopherson8373d252018-12-03 13:53:08 -0800177 struct hv_enlightened_vmcs *hv_evmcs;
178};
179
180struct vcpu_vmx {
181 struct kvm_vcpu vcpu;
Sean Christopherson8373d252018-12-03 13:53:08 -0800182 u8 fail;
183 u8 msr_bitmap_mode;
Paolo Bonzinib464f57e2019-06-07 19:00:14 +0200184
185 /*
186 * If true, host state has been stored in vmx->loaded_vmcs for
187 * the CPU registers that only need to be switched when transitioning
188 * to/from the kernel, and the registers have been loaded with guest
189 * values. If false, host state is loaded in the CPU registers
190 * and vmx->loaded_vmcs->host_state is invalid.
191 */
192 bool guest_state_loaded;
193
Sean Christopherson5addc232020-04-15 13:34:53 -0700194 unsigned long exit_qualification;
Sean Christopherson8373d252018-12-03 13:53:08 -0800195 u32 exit_intr_info;
196 u32 idt_vectoring_info;
197 ulong rflags;
Sean Christopherson70f932e2019-05-07 12:17:54 -0700198
Sean Christophersoneb3db1b2020-09-23 11:03:58 -0700199 struct vmx_uret_msr guest_uret_msrs[MAX_NR_USER_RETURN_MSRS];
Sean Christophersonfbc18002020-09-23 11:03:59 -0700200 int nr_uret_msrs;
Sean Christophersone9bb1ae2020-09-23 11:04:00 -0700201 int nr_active_uret_msrs;
Sean Christopherson658ece82020-09-23 11:04:01 -0700202 bool guest_uret_msrs_loaded;
Sean Christopherson8373d252018-12-03 13:53:08 -0800203#ifdef CONFIG_X86_64
204 u64 msr_host_kernel_gs_base;
205 u64 msr_guest_kernel_gs_base;
206#endif
207
Sean Christopherson8373d252018-12-03 13:53:08 -0800208 u64 spec_ctrl;
Tao Xu6e3ba4a2019-07-16 14:55:50 +0800209 u32 msr_ia32_umwait_control;
Sean Christopherson8373d252018-12-03 13:53:08 -0800210
Sean Christopherson8373d252018-12-03 13:53:08 -0800211 u32 secondary_exec_control;
212
213 /*
214 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
215 * non-nested (L1) guest, it always points to vmcs01. For a nested
Paolo Bonzinib464f57e2019-06-07 19:00:14 +0200216 * guest (L2), it points to a different VMCS.
Sean Christopherson8373d252018-12-03 13:53:08 -0800217 */
218 struct loaded_vmcs vmcs01;
219 struct loaded_vmcs *loaded_vmcs;
Sean Christophersonc9afc582019-01-25 07:41:05 -0800220
Sean Christopherson8373d252018-12-03 13:53:08 -0800221 struct msr_autoload {
222 struct vmx_msrs guest;
223 struct vmx_msrs host;
224 } msr_autoload;
225
Aaron Lewis662f1d12019-11-07 21:14:39 -0800226 struct msr_autostore {
227 struct vmx_msrs guest;
228 } msr_autostore;
229
Sean Christopherson8373d252018-12-03 13:53:08 -0800230 struct {
231 int vm86_active;
232 ulong save_rflags;
233 struct kvm_segment segs[8];
234 } rmode;
235 struct {
236 u32 bitmask; /* 4 bits per segment (1 bit per field) */
237 struct kvm_save_segment {
238 u16 selector;
239 unsigned long base;
240 u32 limit;
241 u32 ar;
242 } seg[8];
243 } segment_cache;
244 int vpid;
245 bool emulation_required;
246
247 u32 exit_reason;
248
249 /* Posted interrupt descriptor */
250 struct pi_desc pi_desc;
251
252 /* Support for a guest hypervisor (nested VMX) */
253 struct nested_vmx nested;
254
255 /* Dynamic PLE window. */
Peter Xuc5c5d6f2019-09-06 10:17:21 +0800256 unsigned int ple_window;
Sean Christopherson8373d252018-12-03 13:53:08 -0800257 bool ple_window_dirty;
258
259 bool req_immediate_exit;
260
261 /* Support for PML */
262#define PML_ENTITY_NUM 512
263 struct page *pml_pg;
264
265 /* apic deadline value in host tsc */
266 u64 hv_deadline_tsc;
267
268 u64 current_tsc_ratio;
269
Sean Christopherson8373d252018-12-03 13:53:08 -0800270 unsigned long host_debugctlmsr;
271
272 /*
273 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
Sean Christopherson32ad73d2019-12-20 20:44:55 -0800274 * msr_ia32_feature_control. FEAT_CTL_LOCKED is always included
Sean Christopherson8373d252018-12-03 13:53:08 -0800275 * in msr_ia32_feature_control_valid_bits.
276 */
277 u64 msr_ia32_feature_control;
278 u64 msr_ia32_feature_control_valid_bits;
279 u64 ept_pointer;
Chao Peng2ef444f2018-10-24 16:05:12 +0800280
281 struct pt_desc pt_desc;
Sean Christopherson8373d252018-12-03 13:53:08 -0800282};
283
284enum ept_pointers_status {
285 EPT_POINTERS_CHECK = 0,
286 EPT_POINTERS_MATCH = 1,
287 EPT_POINTERS_MISMATCH = 2
288};
289
290struct kvm_vmx {
291 struct kvm kvm;
292
293 unsigned int tss_addr;
294 bool ept_identity_pagetable_done;
295 gpa_t ept_identity_map_addr;
296
297 enum ept_pointers_status ept_pointers_match;
298 spinlock_t ept_pointer_lock;
299};
300
Sean Christopherson7c97fcb2018-12-03 13:53:17 -0800301bool nested_vmx_allowed(struct kvm_vcpu *vcpu);
Sean Christopherson5c911be2020-05-01 09:31:17 -0700302void vmx_vcpu_load_vmcs(struct kvm_vcpu *vcpu, int cpu,
303 struct loaded_vmcs *buddy);
Sean Christopherson97b7ead2018-12-03 13:53:16 -0800304int allocate_vpid(void);
305void free_vpid(int vpid);
306void vmx_set_constant_host_state(struct vcpu_vmx *vmx);
307void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu);
Sean Christopherson13b964a2019-05-07 09:06:31 -0700308void vmx_set_host_fs_gs(struct vmcs_host_state *host, u16 fs_sel, u16 gs_sel,
309 unsigned long fs_base, unsigned long gs_base);
Sean Christopherson97b7ead2018-12-03 13:53:16 -0800310int vmx_get_cpl(struct kvm_vcpu *vcpu);
311unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu);
312void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags);
313u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu);
314void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask);
315void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer);
316void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
Sean Christopherson97b7ead2018-12-03 13:53:16 -0800317int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
318void set_cr4_guest_host_mask(struct vcpu_vmx *vmx);
319void ept_save_pdptrs(struct kvm_vcpu *vcpu);
320void vmx_get_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg);
321void vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg);
Sean Christopherson2a40b902020-07-15 20:41:18 -0700322u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa,
323 int root_level);
Sean Christopherson2ba44932020-09-23 11:44:48 -0700324
Sean Christopherson97b7ead2018-12-03 13:53:16 -0800325void update_exception_bitmap(struct kvm_vcpu *vcpu);
326void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Sean Christopherson1b660b62020-04-22 19:25:44 -0700327bool vmx_nmi_blocked(struct kvm_vcpu *vcpu);
328bool vmx_interrupt_blocked(struct kvm_vcpu *vcpu);
Sean Christopherson97b7ead2018-12-03 13:53:16 -0800329bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
330void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
331void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu);
Sean Christophersond85a8032020-09-23 11:04:06 -0700332struct vmx_uret_msr *vmx_find_uret_msr(struct vcpu_vmx *vmx, u32 msr);
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200333void pt_update_intercept_for_msr(struct kvm_vcpu *vcpu);
Yi Wang4d259962019-05-20 12:27:47 +0800334void vmx_update_host_rsp(struct vcpu_vmx *vmx, unsigned long host_rsp);
Sean Christophersona128a932020-09-23 11:03:57 -0700335int vmx_find_loadstore_msr_slot(struct vmx_msrs *m, u32 msr);
Peter Shier43fea4e2020-08-20 16:05:45 -0700336void vmx_ept_load_pdptrs(struct kvm_vcpu *vcpu);
Sean Christopherson97b7ead2018-12-03 13:53:16 -0800337
Sean Christopherson89b0c9f2018-12-03 13:53:07 -0800338static inline u8 vmx_get_rvi(void)
339{
340 return vmcs_read16(GUEST_INTR_STATUS) & 0xff;
341}
342
Sean Christopherson70f932e2019-05-07 12:17:54 -0700343#define BUILD_CONTROLS_SHADOW(lname, uname) \
Sean Christopherson70f932e2019-05-07 12:17:54 -0700344static inline void lname##_controls_set(struct vcpu_vmx *vmx, u32 val) \
345{ \
Sean Christopherson09e226c2019-05-07 12:17:58 -0700346 if (vmx->loaded_vmcs->controls_shadow.lname != val) { \
347 vmcs_write32(uname, val); \
348 vmx->loaded_vmcs->controls_shadow.lname = val; \
349 } \
Sean Christopherson70f932e2019-05-07 12:17:54 -0700350} \
351static inline u32 lname##_controls_get(struct vcpu_vmx *vmx) \
352{ \
Sean Christopherson09e226c2019-05-07 12:17:58 -0700353 return vmx->loaded_vmcs->controls_shadow.lname; \
Sean Christopherson70f932e2019-05-07 12:17:54 -0700354} \
355static inline void lname##_controls_setbit(struct vcpu_vmx *vmx, u32 val) \
356{ \
357 lname##_controls_set(vmx, lname##_controls_get(vmx) | val); \
358} \
359static inline void lname##_controls_clearbit(struct vcpu_vmx *vmx, u32 val) \
360{ \
361 lname##_controls_set(vmx, lname##_controls_get(vmx) & ~val); \
Sean Christopherson89b0c9f2018-12-03 13:53:07 -0800362}
Sean Christopherson70f932e2019-05-07 12:17:54 -0700363BUILD_CONTROLS_SHADOW(vm_entry, VM_ENTRY_CONTROLS)
364BUILD_CONTROLS_SHADOW(vm_exit, VM_EXIT_CONTROLS)
Sean Christophersonc5f2c762019-05-07 12:17:55 -0700365BUILD_CONTROLS_SHADOW(pin, PIN_BASED_VM_EXEC_CONTROL)
Sean Christopherson2183f562019-05-07 12:17:56 -0700366BUILD_CONTROLS_SHADOW(exec, CPU_BASED_VM_EXEC_CONTROL)
Sean Christophersonfe7f895d2019-05-07 12:17:57 -0700367BUILD_CONTROLS_SHADOW(secondary_exec, SECONDARY_VM_EXEC_CONTROL)
Sean Christopherson89b0c9f2018-12-03 13:53:07 -0800368
Sean Christophersone5d03de2020-04-15 13:34:51 -0700369static inline void vmx_register_cache_reset(struct kvm_vcpu *vcpu)
370{
371 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
372 | (1 << VCPU_EXREG_RFLAGS)
373 | (1 << VCPU_EXREG_PDPTR)
374 | (1 << VCPU_EXREG_SEGMENTS)
Sean Christophersonbd31fe42020-05-01 21:32:31 -0700375 | (1 << VCPU_EXREG_CR0)
Sean Christopherson5addc232020-04-15 13:34:53 -0700376 | (1 << VCPU_EXREG_CR3)
Sean Christophersonf98c1e72020-05-01 21:32:30 -0700377 | (1 << VCPU_EXREG_CR4)
Sean Christopherson87915852020-04-15 13:34:54 -0700378 | (1 << VCPU_EXREG_EXIT_INFO_1)
379 | (1 << VCPU_EXREG_EXIT_INFO_2));
Sean Christophersone5d03de2020-04-15 13:34:51 -0700380 vcpu->arch.regs_dirty = 0;
381}
382
Sean Christopherson8373d252018-12-03 13:53:08 -0800383static inline u32 vmx_vmentry_ctrl(void)
384{
Chao Pengf99e3da2018-10-24 16:05:10 +0800385 u32 vmentry_ctrl = vmcs_config.vmentry_ctrl;
Sean Christopherson2ef76192020-03-02 15:56:22 -0800386 if (vmx_pt_mode_is_system())
Yu Zhangd9293592019-01-31 11:26:39 +0800387 vmentry_ctrl &= ~(VM_ENTRY_PT_CONCEAL_PIP |
388 VM_ENTRY_LOAD_IA32_RTIT_CTL);
Sean Christopherson8373d252018-12-03 13:53:08 -0800389 /* Loading of EFER and PERF_GLOBAL_CTRL are toggled dynamically */
Chao Pengf99e3da2018-10-24 16:05:10 +0800390 return vmentry_ctrl &
Sean Christopherson8373d252018-12-03 13:53:08 -0800391 ~(VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL | VM_ENTRY_LOAD_IA32_EFER);
392}
393
394static inline u32 vmx_vmexit_ctrl(void)
395{
Chao Pengf99e3da2018-10-24 16:05:10 +0800396 u32 vmexit_ctrl = vmcs_config.vmexit_ctrl;
Sean Christopherson2ef76192020-03-02 15:56:22 -0800397 if (vmx_pt_mode_is_system())
Yu Zhangd9293592019-01-31 11:26:39 +0800398 vmexit_ctrl &= ~(VM_EXIT_PT_CONCEAL_PIP |
399 VM_EXIT_CLEAR_IA32_RTIT_CTL);
Sean Christopherson8373d252018-12-03 13:53:08 -0800400 /* Loading of EFER and PERF_GLOBAL_CTRL are toggled dynamically */
Yu Zhangd9293592019-01-31 11:26:39 +0800401 return vmexit_ctrl &
Sean Christopherson8373d252018-12-03 13:53:08 -0800402 ~(VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL | VM_EXIT_LOAD_IA32_EFER);
403}
404
405u32 vmx_exec_control(struct vcpu_vmx *vmx);
Sean Christophersonc075c3e2019-05-07 12:17:53 -0700406u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx);
Sean Christopherson8373d252018-12-03 13:53:08 -0800407
408static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
409{
410 return container_of(kvm, struct kvm_vmx, kvm);
411}
412
413static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
414{
415 return container_of(vcpu, struct vcpu_vmx, vcpu);
416}
417
Sean Christopherson5addc232020-04-15 13:34:53 -0700418static inline unsigned long vmx_get_exit_qual(struct kvm_vcpu *vcpu)
419{
420 struct vcpu_vmx *vmx = to_vmx(vcpu);
421
422 if (!kvm_register_is_available(vcpu, VCPU_EXREG_EXIT_INFO_1)) {
423 kvm_register_mark_available(vcpu, VCPU_EXREG_EXIT_INFO_1);
424 vmx->exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
425 }
426 return vmx->exit_qualification;
427}
428
Sean Christopherson87915852020-04-15 13:34:54 -0700429static inline u32 vmx_get_intr_info(struct kvm_vcpu *vcpu)
430{
431 struct vcpu_vmx *vmx = to_vmx(vcpu);
432
433 if (!kvm_register_is_available(vcpu, VCPU_EXREG_EXIT_INFO_2)) {
434 kvm_register_mark_available(vcpu, VCPU_EXREG_EXIT_INFO_2);
435 vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
436 }
437 return vmx->exit_intr_info;
438}
439
Ben Gardon41836832019-02-11 11:02:52 -0800440struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu, gfp_t flags);
Sean Christopherson89b0c9f2018-12-03 13:53:07 -0800441void free_vmcs(struct vmcs *vmcs);
442int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs);
443void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs);
Sean Christopherson89b0c9f2018-12-03 13:53:07 -0800444void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs);
445
446static inline struct vmcs *alloc_vmcs(bool shadow)
447{
Ben Gardon41836832019-02-11 11:02:52 -0800448 return alloc_vmcs_cpu(shadow, raw_smp_processor_id(),
449 GFP_KERNEL_ACCOUNT);
Sean Christopherson89b0c9f2018-12-03 13:53:07 -0800450}
451
Sean Christopherson89b0c9f2018-12-03 13:53:07 -0800452static inline void decache_tsc_multiplier(struct vcpu_vmx *vmx)
453{
454 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
455 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
456}
457
Tao Xu6e3ba4a2019-07-16 14:55:50 +0800458static inline bool vmx_has_waitpkg(struct vcpu_vmx *vmx)
459{
460 return vmx->secondary_exec_control &
461 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE;
462}
463
Paolo Bonzinia0c13432020-07-10 17:48:08 +0200464static inline bool vmx_need_pf_intercept(struct kvm_vcpu *vcpu)
465{
Mohammed Gamal1dbf5d682020-07-10 17:48:09 +0200466 return !enable_ept || cpuid_maxphyaddr(vcpu) < boot_cpu_data.x86_phys_bits;
Paolo Bonzinia0c13432020-07-10 17:48:08 +0200467}
468
Krish Sadhukhanbddd82d2020-09-21 08:10:25 +0000469static inline bool is_unrestricted_guest(struct kvm_vcpu *vcpu)
470{
471 return enable_unrestricted_guest && (!is_guest_mode(vcpu) ||
472 (secondary_exec_controls_get(to_vmx(vcpu)) &
473 SECONDARY_EXEC_UNRESTRICTED_GUEST));
474}
475
Sean Christopherson2ba44932020-09-23 11:44:48 -0700476bool __vmx_guest_state_valid(struct kvm_vcpu *vcpu);
477static inline bool vmx_guest_state_valid(struct kvm_vcpu *vcpu)
478{
479 return is_unrestricted_guest(vcpu) || __vmx_guest_state_valid(vcpu);
480}
481
Paolo Bonzini69090812019-04-15 15:16:17 +0200482void dump_vmcs(void);
483
Sean Christopherson8373d252018-12-03 13:53:08 -0800484#endif /* __KVM_X86_VMX_H */