blob: 13a4accca348fa1d1bcbc3b3e142bfbec43a600c [file] [log] [blame]
Sean Christopherson55d23752018-12-03 13:53:18 -08001// SPDX-License-Identifier: GPL-2.0
2
Julien Thierry00089c02020-09-04 16:30:25 +01003#include <linux/objtool.h>
Sean Christopherson55d23752018-12-03 13:53:18 -08004#include <linux/percpu.h>
5
6#include <asm/debugreg.h>
7#include <asm/mmu_context.h>
8
9#include "cpuid.h"
10#include "hyperv.h"
11#include "mmu.h"
12#include "nested.h"
Oliver Uptonbfc6ad62019-11-13 16:17:16 -080013#include "pmu.h"
Sean Christopherson72add912021-04-12 16:21:42 +120014#include "sgx.h"
Sean Christopherson55d23752018-12-03 13:53:18 -080015#include "trace.h"
Uros Bizjak150f17b2020-12-30 16:26:57 -080016#include "vmx.h"
Sean Christopherson55d23752018-12-03 13:53:18 -080017#include "x86.h"
18
19static bool __read_mostly enable_shadow_vmcs = 1;
20module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
21
22static bool __read_mostly nested_early_check = 0;
23module_param(nested_early_check, bool, S_IRUGO);
24
Sean Christopherson648fc8a2021-02-03 16:01:16 -080025#define CC KVM_NESTED_VMENTER_CONSISTENCY_CHECK
Sean Christopherson5497b952019-07-11 08:58:29 -070026
Sean Christopherson55d23752018-12-03 13:53:18 -080027/*
28 * Hyper-V requires all of these, so mark them as supported even though
29 * they are just treated the same as all-context.
30 */
31#define VMX_VPID_EXTENT_SUPPORTED_MASK \
32 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
33 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
34 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
35 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
36
37#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
38
39enum {
40 VMX_VMREAD_BITMAP,
41 VMX_VMWRITE_BITMAP,
42 VMX_BITMAP_NR
43};
44static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
45
46#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
47#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
48
Sean Christopherson1c6f0b42019-05-07 08:36:25 -070049struct shadow_vmcs_field {
50 u16 encoding;
51 u16 offset;
52};
53static struct shadow_vmcs_field shadow_read_only_fields[] = {
54#define SHADOW_FIELD_RO(x, y) { x, offsetof(struct vmcs12, y) },
Sean Christopherson55d23752018-12-03 13:53:18 -080055#include "vmcs_shadow_fields.h"
56};
57static int max_shadow_read_only_fields =
58 ARRAY_SIZE(shadow_read_only_fields);
59
Sean Christopherson1c6f0b42019-05-07 08:36:25 -070060static struct shadow_vmcs_field shadow_read_write_fields[] = {
61#define SHADOW_FIELD_RW(x, y) { x, offsetof(struct vmcs12, y) },
Sean Christopherson55d23752018-12-03 13:53:18 -080062#include "vmcs_shadow_fields.h"
63};
64static int max_shadow_read_write_fields =
65 ARRAY_SIZE(shadow_read_write_fields);
66
Yi Wang8997f652019-01-21 15:27:05 +080067static void init_vmcs_shadow_fields(void)
Sean Christopherson55d23752018-12-03 13:53:18 -080068{
69 int i, j;
70
71 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
72 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
73
74 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
Sean Christopherson1c6f0b42019-05-07 08:36:25 -070075 struct shadow_vmcs_field entry = shadow_read_only_fields[i];
76 u16 field = entry.encoding;
Sean Christopherson55d23752018-12-03 13:53:18 -080077
78 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
79 (i + 1 == max_shadow_read_only_fields ||
Sean Christopherson1c6f0b42019-05-07 08:36:25 -070080 shadow_read_only_fields[i + 1].encoding != field + 1))
Sean Christopherson55d23752018-12-03 13:53:18 -080081 pr_err("Missing field from shadow_read_only_field %x\n",
82 field + 1);
83
84 clear_bit(field, vmx_vmread_bitmap);
Sean Christopherson55d23752018-12-03 13:53:18 -080085 if (field & 1)
Sean Christopherson1c6f0b42019-05-07 08:36:25 -070086#ifdef CONFIG_X86_64
Sean Christopherson55d23752018-12-03 13:53:18 -080087 continue;
Sean Christopherson1c6f0b42019-05-07 08:36:25 -070088#else
89 entry.offset += sizeof(u32);
Sean Christopherson55d23752018-12-03 13:53:18 -080090#endif
Sean Christopherson1c6f0b42019-05-07 08:36:25 -070091 shadow_read_only_fields[j++] = entry;
Sean Christopherson55d23752018-12-03 13:53:18 -080092 }
93 max_shadow_read_only_fields = j;
94
95 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Sean Christopherson1c6f0b42019-05-07 08:36:25 -070096 struct shadow_vmcs_field entry = shadow_read_write_fields[i];
97 u16 field = entry.encoding;
Sean Christopherson55d23752018-12-03 13:53:18 -080098
99 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
100 (i + 1 == max_shadow_read_write_fields ||
Sean Christopherson1c6f0b42019-05-07 08:36:25 -0700101 shadow_read_write_fields[i + 1].encoding != field + 1))
Sean Christopherson55d23752018-12-03 13:53:18 -0800102 pr_err("Missing field from shadow_read_write_field %x\n",
103 field + 1);
104
Sean Christophersonb6437802019-05-07 08:36:24 -0700105 WARN_ONCE(field >= GUEST_ES_AR_BYTES &&
106 field <= GUEST_TR_AR_BYTES,
Sean Christopherson1c6f0b42019-05-07 08:36:25 -0700107 "Update vmcs12_write_any() to drop reserved bits from AR_BYTES");
Sean Christophersonb6437802019-05-07 08:36:24 -0700108
Sean Christopherson55d23752018-12-03 13:53:18 -0800109 /*
110 * PML and the preemption timer can be emulated, but the
111 * processor cannot vmwrite to fields that don't exist
112 * on bare metal.
113 */
114 switch (field) {
115 case GUEST_PML_INDEX:
116 if (!cpu_has_vmx_pml())
117 continue;
118 break;
119 case VMX_PREEMPTION_TIMER_VALUE:
120 if (!cpu_has_vmx_preemption_timer())
121 continue;
122 break;
123 case GUEST_INTR_STATUS:
124 if (!cpu_has_vmx_apicv())
125 continue;
126 break;
127 default:
128 break;
129 }
130
131 clear_bit(field, vmx_vmwrite_bitmap);
132 clear_bit(field, vmx_vmread_bitmap);
Sean Christopherson55d23752018-12-03 13:53:18 -0800133 if (field & 1)
Sean Christopherson1c6f0b42019-05-07 08:36:25 -0700134#ifdef CONFIG_X86_64
Sean Christopherson55d23752018-12-03 13:53:18 -0800135 continue;
Sean Christopherson1c6f0b42019-05-07 08:36:25 -0700136#else
137 entry.offset += sizeof(u32);
Sean Christopherson55d23752018-12-03 13:53:18 -0800138#endif
Sean Christopherson1c6f0b42019-05-07 08:36:25 -0700139 shadow_read_write_fields[j++] = entry;
Sean Christopherson55d23752018-12-03 13:53:18 -0800140 }
141 max_shadow_read_write_fields = j;
142}
143
144/*
145 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
146 * set the success or error code of an emulated VMX instruction (as specified
147 * by Vol 2B, VMX Instruction Reference, "Conventions"), and skip the emulated
148 * instruction.
149 */
150static int nested_vmx_succeed(struct kvm_vcpu *vcpu)
151{
152 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
153 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
154 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
155 return kvm_skip_emulated_instruction(vcpu);
156}
157
158static int nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
159{
160 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
161 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
162 X86_EFLAGS_SF | X86_EFLAGS_OF))
163 | X86_EFLAGS_CF);
164 return kvm_skip_emulated_instruction(vcpu);
165}
166
167static int nested_vmx_failValid(struct kvm_vcpu *vcpu,
168 u32 vm_instruction_error)
169{
Sean Christopherson55d23752018-12-03 13:53:18 -0800170 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
171 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
172 X86_EFLAGS_SF | X86_EFLAGS_OF))
173 | X86_EFLAGS_ZF);
174 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
175 /*
Vitaly Kuznetsovb7685cf2021-05-26 15:20:23 +0200176 * We don't need to force sync to shadow VMCS because
177 * VM_INSTRUCTION_ERROR is not shadowed. Enlightened VMCS 'shadows' all
178 * fields and thus must be synced.
Sean Christopherson55d23752018-12-03 13:53:18 -0800179 */
Vitaly Kuznetsovb7685cf2021-05-26 15:20:23 +0200180 if (to_vmx(vcpu)->nested.hv_evmcs_vmptr != EVMPTR_INVALID)
181 to_vmx(vcpu)->nested.need_vmcs12_to_shadow_sync = true;
182
Sean Christopherson55d23752018-12-03 13:53:18 -0800183 return kvm_skip_emulated_instruction(vcpu);
184}
185
Sean Christophersonb2656e42020-06-08 18:56:07 -0700186static int nested_vmx_fail(struct kvm_vcpu *vcpu, u32 vm_instruction_error)
187{
188 struct vcpu_vmx *vmx = to_vmx(vcpu);
189
190 /*
191 * failValid writes the error number to the current VMCS, which
192 * can't be done if there isn't a current VMCS.
193 */
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +0200194 if (vmx->nested.current_vmptr == -1ull &&
195 !evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
Sean Christophersonb2656e42020-06-08 18:56:07 -0700196 return nested_vmx_failInvalid(vcpu);
197
198 return nested_vmx_failValid(vcpu, vm_instruction_error);
199}
200
Sean Christopherson55d23752018-12-03 13:53:18 -0800201static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
202{
203 /* TODO: not to reset guest simply here. */
204 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
205 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
206}
207
Marc Orrf0b51052019-09-17 11:50:57 -0700208static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
209{
210 return fixed_bits_valid(control, low, high);
211}
212
213static inline u64 vmx_control_msr(u32 low, u32 high)
214{
215 return low | ((u64)high << 32);
216}
217
Sean Christopherson55d23752018-12-03 13:53:18 -0800218static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
219{
Sean Christophersonfe7f895d2019-05-07 12:17:57 -0700220 secondary_exec_controls_clearbit(vmx, SECONDARY_EXEC_SHADOW_VMCS);
Sean Christopherson55d23752018-12-03 13:53:18 -0800221 vmcs_write64(VMCS_LINK_POINTER, -1ull);
Paolo Bonzini88dddc12019-07-19 18:41:10 +0200222 vmx->nested.need_vmcs12_to_shadow_sync = false;
Sean Christopherson55d23752018-12-03 13:53:18 -0800223}
224
225static inline void nested_release_evmcs(struct kvm_vcpu *vcpu)
226{
227 struct vcpu_vmx *vmx = to_vmx(vcpu);
228
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +0200229 if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) {
230 kvm_vcpu_unmap(vcpu, &vmx->nested.hv_evmcs_map, true);
231 vmx->nested.hv_evmcs = NULL;
232 }
Sean Christopherson55d23752018-12-03 13:53:18 -0800233
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +0200234 vmx->nested.hv_evmcs_vmptr = EVMPTR_INVALID;
Sean Christopherson55d23752018-12-03 13:53:18 -0800235}
236
Sean Christophersonc61ca2f2020-09-23 11:44:49 -0700237static void vmx_sync_vmcs_host_state(struct vcpu_vmx *vmx,
238 struct loaded_vmcs *prev)
239{
240 struct vmcs_host_state *dest, *src;
241
242 if (unlikely(!vmx->guest_state_loaded))
243 return;
244
245 src = &prev->host_state;
246 dest = &vmx->loaded_vmcs->host_state;
247
248 vmx_set_host_fs_gs(dest, src->fs_sel, src->gs_sel, src->fs_base, src->gs_base);
249 dest->ldt_sel = src->ldt_sel;
250#ifdef CONFIG_X86_64
251 dest->ds_sel = src->ds_sel;
252 dest->es_sel = src->es_sel;
253#endif
254}
255
256static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
257{
258 struct vcpu_vmx *vmx = to_vmx(vcpu);
259 struct loaded_vmcs *prev;
260 int cpu;
261
Sean Christopherson138534a2020-09-23 11:44:52 -0700262 if (WARN_ON_ONCE(vmx->loaded_vmcs == vmcs))
Sean Christophersonc61ca2f2020-09-23 11:44:49 -0700263 return;
264
265 cpu = get_cpu();
266 prev = vmx->loaded_vmcs;
267 vmx->loaded_vmcs = vmcs;
268 vmx_vcpu_load_vmcs(vcpu, cpu, prev);
269 vmx_sync_vmcs_host_state(vmx, prev);
270 put_cpu();
271
272 vmx_register_cache_reset(vcpu);
273}
274
Sean Christopherson55d23752018-12-03 13:53:18 -0800275/*
276 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
277 * just stops using VMX.
278 */
279static void free_nested(struct kvm_vcpu *vcpu)
280{
281 struct vcpu_vmx *vmx = to_vmx(vcpu);
282
Sean Christophersondf82a242020-09-23 11:44:50 -0700283 if (WARN_ON_ONCE(vmx->loaded_vmcs != &vmx->vmcs01))
284 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
285
Sean Christopherson55d23752018-12-03 13:53:18 -0800286 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
287 return;
288
Paolo Bonzini729c15c2020-09-22 06:53:57 -0400289 kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
Jan Kiszkacf645272019-07-21 13:52:18 +0200290
Sean Christopherson55d23752018-12-03 13:53:18 -0800291 vmx->nested.vmxon = false;
292 vmx->nested.smm.vmxon = false;
293 free_vpid(vmx->nested.vpid02);
294 vmx->nested.posted_intr_nv = -1;
295 vmx->nested.current_vmptr = -1ull;
296 if (enable_shadow_vmcs) {
297 vmx_disable_shadow_vmcs(vmx);
298 vmcs_clear(vmx->vmcs01.shadow_vmcs);
299 free_vmcs(vmx->vmcs01.shadow_vmcs);
300 vmx->vmcs01.shadow_vmcs = NULL;
301 }
302 kfree(vmx->nested.cached_vmcs12);
Jan Kiszkac6bf2ae2019-07-21 16:01:36 +0200303 vmx->nested.cached_vmcs12 = NULL;
Sean Christopherson55d23752018-12-03 13:53:18 -0800304 kfree(vmx->nested.cached_shadow_vmcs12);
Jan Kiszkac6bf2ae2019-07-21 16:01:36 +0200305 vmx->nested.cached_shadow_vmcs12 = NULL;
Sean Christopherson55d23752018-12-03 13:53:18 -0800306 /* Unpin physical memory we referred to in the vmcs02 */
307 if (vmx->nested.apic_access_page) {
Liran Alonb11494b2019-11-21 00:31:47 +0200308 kvm_release_page_clean(vmx->nested.apic_access_page);
Sean Christopherson55d23752018-12-03 13:53:18 -0800309 vmx->nested.apic_access_page = NULL;
310 }
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +0100311 kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map, true);
KarimAllah Ahmed3278e042019-01-31 21:24:38 +0100312 kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map, true);
313 vmx->nested.pi_desc = NULL;
Sean Christopherson55d23752018-12-03 13:53:18 -0800314
315 kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
316
317 nested_release_evmcs(vcpu);
318
319 free_loaded_vmcs(&vmx->nested.vmcs02);
320}
321
Sean Christopherson55d23752018-12-03 13:53:18 -0800322/*
323 * Ensure that the current vmcs of the logical processor is the
324 * vmcs01 of the vcpu before calling free_nested().
325 */
326void nested_vmx_free_vcpu(struct kvm_vcpu *vcpu)
327{
328 vcpu_load(vcpu);
Paolo Bonzinib4b65b52019-01-29 19:12:35 +0100329 vmx_leave_nested(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -0800330 vcpu_put(vcpu);
331}
332
333static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
334 struct x86_exception *fault)
335{
336 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
337 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson4dcefa32020-04-15 10:55:18 -0700338 u32 vm_exit_reason;
Sean Christopherson55d23752018-12-03 13:53:18 -0800339 unsigned long exit_qualification = vcpu->arch.exit_qualification;
340
341 if (vmx->nested.pml_full) {
Sean Christopherson4dcefa32020-04-15 10:55:18 -0700342 vm_exit_reason = EXIT_REASON_PML_FULL;
Sean Christopherson55d23752018-12-03 13:53:18 -0800343 vmx->nested.pml_full = false;
344 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
345 } else if (fault->error_code & PFERR_RSVD_MASK)
Sean Christopherson4dcefa32020-04-15 10:55:18 -0700346 vm_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Sean Christopherson55d23752018-12-03 13:53:18 -0800347 else
Sean Christopherson4dcefa32020-04-15 10:55:18 -0700348 vm_exit_reason = EXIT_REASON_EPT_VIOLATION;
Sean Christopherson55d23752018-12-03 13:53:18 -0800349
Sean Christopherson4dcefa32020-04-15 10:55:18 -0700350 nested_vmx_vmexit(vcpu, vm_exit_reason, 0, exit_qualification);
Sean Christopherson55d23752018-12-03 13:53:18 -0800351 vmcs12->guest_physical_address = fault->address;
352}
353
354static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
355{
356 WARN_ON(mmu_is_nested(vcpu));
357
358 vcpu->arch.mmu = &vcpu->arch.guest_mmu;
359 kvm_init_shadow_ept_mmu(vcpu,
360 to_vmx(vcpu)->nested.msrs.ept_caps &
361 VMX_EPT_EXECUTE_ONLY_BIT,
362 nested_ept_ad_enabled(vcpu),
Sean Christophersonac69dfa2020-03-02 18:02:37 -0800363 nested_ept_get_eptp(vcpu));
Sean Christophersond8dd54e2020-03-02 18:02:39 -0800364 vcpu->arch.mmu->get_guest_pgd = nested_ept_get_eptp;
Sean Christopherson55d23752018-12-03 13:53:18 -0800365 vcpu->arch.mmu->inject_page_fault = nested_ept_inject_page_fault;
366 vcpu->arch.mmu->get_pdptr = kvm_pdptr_read;
367
368 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
369}
370
371static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
372{
373 vcpu->arch.mmu = &vcpu->arch.root_mmu;
374 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
375}
376
377static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
378 u16 error_code)
379{
380 bool inequality, bit;
381
382 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
383 inequality =
384 (error_code & vmcs12->page_fault_error_code_mask) !=
385 vmcs12->page_fault_error_code_match;
386 return inequality ^ bit;
387}
388
389
390/*
391 * KVM wants to inject page-faults which it got to the guest. This function
392 * checks whether in a nested guest, we need to inject them to L1 or L2.
393 */
394static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
395{
396 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
397 unsigned int nr = vcpu->arch.exception.nr;
398 bool has_payload = vcpu->arch.exception.has_payload;
399 unsigned long payload = vcpu->arch.exception.payload;
400
401 if (nr == PF_VECTOR) {
402 if (vcpu->arch.exception.nested_apf) {
403 *exit_qual = vcpu->arch.apf.nested_apf_token;
404 return 1;
405 }
406 if (nested_vmx_is_page_fault_vmexit(vmcs12,
407 vcpu->arch.exception.error_code)) {
408 *exit_qual = has_payload ? payload : vcpu->arch.cr2;
409 return 1;
410 }
411 } else if (vmcs12->exception_bitmap & (1u << nr)) {
412 if (nr == DB_VECTOR) {
413 if (!has_payload) {
414 payload = vcpu->arch.dr6;
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +0800415 payload &= ~DR6_BT;
416 payload ^= DR6_ACTIVE_LOW;
Sean Christopherson55d23752018-12-03 13:53:18 -0800417 }
418 *exit_qual = payload;
419 } else
420 *exit_qual = 0;
421 return 1;
422 }
423
424 return 0;
425}
426
427
428static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
429 struct x86_exception *fault)
430{
431 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
432
433 WARN_ON(!is_guest_mode(vcpu));
434
435 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
436 !to_vmx(vcpu)->nested.nested_run_pending) {
437 vmcs12->vm_exit_intr_error_code = fault->error_code;
438 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
439 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
440 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
441 fault->address);
442 } else {
443 kvm_inject_page_fault(vcpu, fault);
444 }
445}
446
Sean Christopherson55d23752018-12-03 13:53:18 -0800447static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
448 struct vmcs12 *vmcs12)
449{
450 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
451 return 0;
452
Sean Christopherson5497b952019-07-11 08:58:29 -0700453 if (CC(!page_address_valid(vcpu, vmcs12->io_bitmap_a)) ||
454 CC(!page_address_valid(vcpu, vmcs12->io_bitmap_b)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800455 return -EINVAL;
456
457 return 0;
458}
459
460static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
461 struct vmcs12 *vmcs12)
462{
463 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
464 return 0;
465
Sean Christopherson5497b952019-07-11 08:58:29 -0700466 if (CC(!page_address_valid(vcpu, vmcs12->msr_bitmap)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800467 return -EINVAL;
468
469 return 0;
470}
471
472static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
473 struct vmcs12 *vmcs12)
474{
475 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
476 return 0;
477
Sean Christopherson5497b952019-07-11 08:58:29 -0700478 if (CC(!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800479 return -EINVAL;
480
481 return 0;
482}
483
484/*
485 * Check if MSR is intercepted for L01 MSR bitmap.
486 */
487static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
488{
489 unsigned long *msr_bitmap;
490 int f = sizeof(unsigned long);
491
492 if (!cpu_has_vmx_msr_bitmap())
493 return true;
494
495 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
496
497 if (msr <= 0x1fff) {
498 return !!test_bit(msr, msr_bitmap + 0x800 / f);
499 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
500 msr &= 0x1fff;
501 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
502 }
503
504 return true;
505}
506
507/*
508 * If a msr is allowed by L0, we should check whether it is allowed by L1.
509 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
510 */
511static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
512 unsigned long *msr_bitmap_nested,
513 u32 msr, int type)
514{
515 int f = sizeof(unsigned long);
516
517 /*
518 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
519 * have the write-low and read-high bitmap offsets the wrong way round.
520 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
521 */
522 if (msr <= 0x1fff) {
523 if (type & MSR_TYPE_R &&
524 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
525 /* read-low */
526 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
527
528 if (type & MSR_TYPE_W &&
529 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
530 /* write-low */
531 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
532
533 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
534 msr &= 0x1fff;
535 if (type & MSR_TYPE_R &&
536 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
537 /* read-high */
538 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
539
540 if (type & MSR_TYPE_W &&
541 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
542 /* write-high */
543 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
544
545 }
546}
547
Miaohe Linffdbd502020-02-07 23:22:45 +0800548static inline void enable_x2apic_msr_intercepts(unsigned long *msr_bitmap)
549{
Marc Orracff7842019-04-01 23:55:59 -0700550 int msr;
551
552 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
553 unsigned word = msr / BITS_PER_LONG;
554
555 msr_bitmap[word] = ~0;
556 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
557 }
558}
559
Sean Christopherson55d23752018-12-03 13:53:18 -0800560/*
561 * Merge L0's and L1's MSR bitmap, return false to indicate that
562 * we do not use the hardware.
563 */
564static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
565 struct vmcs12 *vmcs12)
566{
567 int msr;
Sean Christopherson55d23752018-12-03 13:53:18 -0800568 unsigned long *msr_bitmap_l1;
569 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
KarimAllah Ahmed31f0b6c2019-01-31 21:24:36 +0100570 struct kvm_host_map *map = &to_vmx(vcpu)->nested.msr_bitmap_map;
Sean Christopherson55d23752018-12-03 13:53:18 -0800571
572 /* Nothing to do if the MSR bitmap is not in use. */
573 if (!cpu_has_vmx_msr_bitmap() ||
574 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
575 return false;
576
KarimAllah Ahmed31f0b6c2019-01-31 21:24:36 +0100577 if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->msr_bitmap), map))
Sean Christopherson55d23752018-12-03 13:53:18 -0800578 return false;
579
KarimAllah Ahmed31f0b6c2019-01-31 21:24:36 +0100580 msr_bitmap_l1 = (unsigned long *)map->hva;
Sean Christopherson55d23752018-12-03 13:53:18 -0800581
Marc Orracff7842019-04-01 23:55:59 -0700582 /*
583 * To keep the control flow simple, pay eight 8-byte writes (sixteen
584 * 4-byte writes on 32-bit systems) up front to enable intercepts for
585 * the x2APIC MSR range and selectively disable them below.
586 */
587 enable_x2apic_msr_intercepts(msr_bitmap_l0);
Sean Christopherson55d23752018-12-03 13:53:18 -0800588
Marc Orracff7842019-04-01 23:55:59 -0700589 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
590 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
591 /*
592 * L0 need not intercept reads for MSRs between 0x800
593 * and 0x8ff, it just lets the processor take the value
594 * from the virtual-APIC page; take those 256 bits
595 * directly from the L1 bitmap.
596 */
597 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
598 unsigned word = msr / BITS_PER_LONG;
599
600 msr_bitmap_l0[word] = msr_bitmap_l1[word];
601 }
602 }
603
Sean Christopherson55d23752018-12-03 13:53:18 -0800604 nested_vmx_disable_intercept_for_msr(
605 msr_bitmap_l1, msr_bitmap_l0,
Marc Orracff7842019-04-01 23:55:59 -0700606 X2APIC_MSR(APIC_TASKPRI),
Marc Orrc73f4c92019-04-01 23:56:00 -0700607 MSR_TYPE_R | MSR_TYPE_W);
Marc Orracff7842019-04-01 23:55:59 -0700608
609 if (nested_cpu_has_vid(vmcs12)) {
610 nested_vmx_disable_intercept_for_msr(
611 msr_bitmap_l1, msr_bitmap_l0,
612 X2APIC_MSR(APIC_EOI),
613 MSR_TYPE_W);
614 nested_vmx_disable_intercept_for_msr(
615 msr_bitmap_l1, msr_bitmap_l0,
616 X2APIC_MSR(APIC_SELF_IPI),
617 MSR_TYPE_W);
618 }
Sean Christopherson55d23752018-12-03 13:53:18 -0800619 }
620
Sean Christophersond69129b2019-05-08 07:32:15 -0700621 /* KVM unconditionally exposes the FS/GS base MSRs to L1. */
Sean Christophersondbdd0962021-04-21 19:38:31 -0700622#ifdef CONFIG_X86_64
Sean Christophersond69129b2019-05-08 07:32:15 -0700623 nested_vmx_disable_intercept_for_msr(msr_bitmap_l1, msr_bitmap_l0,
624 MSR_FS_BASE, MSR_TYPE_RW);
625
626 nested_vmx_disable_intercept_for_msr(msr_bitmap_l1, msr_bitmap_l0,
627 MSR_GS_BASE, MSR_TYPE_RW);
628
629 nested_vmx_disable_intercept_for_msr(msr_bitmap_l1, msr_bitmap_l0,
630 MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
Sean Christophersondbdd0962021-04-21 19:38:31 -0700631#endif
Sean Christophersond69129b2019-05-08 07:32:15 -0700632
633 /*
634 * Checking the L0->L1 bitmap is trying to verify two things:
635 *
636 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
637 * ensures that we do not accidentally generate an L02 MSR bitmap
638 * from the L12 MSR bitmap that is too permissive.
639 * 2. That L1 or L2s have actually used the MSR. This avoids
640 * unnecessarily merging of the bitmap if the MSR is unused. This
641 * works properly because we only update the L01 MSR bitmap lazily.
642 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
643 * updated to reflect this when L1 (or its L2s) actually write to
644 * the MSR.
645 */
646 if (!msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL))
Sean Christopherson55d23752018-12-03 13:53:18 -0800647 nested_vmx_disable_intercept_for_msr(
648 msr_bitmap_l1, msr_bitmap_l0,
649 MSR_IA32_SPEC_CTRL,
650 MSR_TYPE_R | MSR_TYPE_W);
651
Sean Christophersond69129b2019-05-08 07:32:15 -0700652 if (!msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD))
Sean Christopherson55d23752018-12-03 13:53:18 -0800653 nested_vmx_disable_intercept_for_msr(
654 msr_bitmap_l1, msr_bitmap_l0,
655 MSR_IA32_PRED_CMD,
656 MSR_TYPE_W);
657
KarimAllah Ahmed31f0b6c2019-01-31 21:24:36 +0100658 kvm_vcpu_unmap(vcpu, &to_vmx(vcpu)->nested.msr_bitmap_map, false);
Sean Christopherson55d23752018-12-03 13:53:18 -0800659
660 return true;
661}
662
663static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
664 struct vmcs12 *vmcs12)
665{
KarimAllah Ahmed88925302019-01-31 21:24:41 +0100666 struct kvm_host_map map;
Sean Christopherson55d23752018-12-03 13:53:18 -0800667 struct vmcs12 *shadow;
Sean Christopherson55d23752018-12-03 13:53:18 -0800668
669 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
670 vmcs12->vmcs_link_pointer == -1ull)
671 return;
672
673 shadow = get_shadow_vmcs12(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -0800674
KarimAllah Ahmed88925302019-01-31 21:24:41 +0100675 if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->vmcs_link_pointer), &map))
676 return;
Sean Christopherson55d23752018-12-03 13:53:18 -0800677
KarimAllah Ahmed88925302019-01-31 21:24:41 +0100678 memcpy(shadow, map.hva, VMCS12_SIZE);
679 kvm_vcpu_unmap(vcpu, &map, false);
Sean Christopherson55d23752018-12-03 13:53:18 -0800680}
681
682static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
683 struct vmcs12 *vmcs12)
684{
685 struct vcpu_vmx *vmx = to_vmx(vcpu);
686
687 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
688 vmcs12->vmcs_link_pointer == -1ull)
689 return;
690
691 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
692 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
693}
694
695/*
696 * In nested virtualization, check if L1 has set
697 * VM_EXIT_ACK_INTR_ON_EXIT
698 */
699static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
700{
701 return get_vmcs12(vcpu)->vm_exit_controls &
702 VM_EXIT_ACK_INTR_ON_EXIT;
703}
704
Sean Christopherson55d23752018-12-03 13:53:18 -0800705static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
706 struct vmcs12 *vmcs12)
707{
708 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
Sean Christopherson5497b952019-07-11 08:58:29 -0700709 CC(!page_address_valid(vcpu, vmcs12->apic_access_addr)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800710 return -EINVAL;
711 else
712 return 0;
713}
714
715static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
716 struct vmcs12 *vmcs12)
717{
718 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
719 !nested_cpu_has_apic_reg_virt(vmcs12) &&
720 !nested_cpu_has_vid(vmcs12) &&
721 !nested_cpu_has_posted_intr(vmcs12))
722 return 0;
723
724 /*
725 * If virtualize x2apic mode is enabled,
726 * virtualize apic access must be disabled.
727 */
Sean Christopherson5497b952019-07-11 08:58:29 -0700728 if (CC(nested_cpu_has_virt_x2apic_mode(vmcs12) &&
729 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800730 return -EINVAL;
731
732 /*
733 * If virtual interrupt delivery is enabled,
734 * we must exit on external interrupts.
735 */
Sean Christopherson5497b952019-07-11 08:58:29 -0700736 if (CC(nested_cpu_has_vid(vmcs12) && !nested_exit_on_intr(vcpu)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800737 return -EINVAL;
738
739 /*
740 * bits 15:8 should be zero in posted_intr_nv,
741 * the descriptor address has been already checked
742 * in nested_get_vmcs12_pages.
743 *
744 * bits 5:0 of posted_intr_desc_addr should be zero.
745 */
746 if (nested_cpu_has_posted_intr(vmcs12) &&
Sean Christopherson5497b952019-07-11 08:58:29 -0700747 (CC(!nested_cpu_has_vid(vmcs12)) ||
748 CC(!nested_exit_intr_ack_set(vcpu)) ||
749 CC((vmcs12->posted_intr_nv & 0xff00)) ||
Sean Christopherson636e8b72021-02-03 16:01:10 -0800750 CC(!kvm_vcpu_is_legal_aligned_gpa(vcpu, vmcs12->posted_intr_desc_addr, 64))))
Sean Christopherson55d23752018-12-03 13:53:18 -0800751 return -EINVAL;
752
753 /* tpr shadow is needed by all apicv features. */
Sean Christopherson5497b952019-07-11 08:58:29 -0700754 if (CC(!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800755 return -EINVAL;
756
757 return 0;
758}
759
760static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
Sean Christophersonf9b245e2018-12-12 13:30:08 -0500761 u32 count, u64 addr)
Sean Christopherson55d23752018-12-03 13:53:18 -0800762{
Sean Christopherson55d23752018-12-03 13:53:18 -0800763 if (count == 0)
764 return 0;
Sean Christopherson636e8b72021-02-03 16:01:10 -0800765
766 if (!kvm_vcpu_is_legal_aligned_gpa(vcpu, addr, 16) ||
767 !kvm_vcpu_is_legal_gpa(vcpu, (addr + count * sizeof(struct vmx_msr_entry) - 1)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800768 return -EINVAL;
Sean Christophersonf9b245e2018-12-12 13:30:08 -0500769
Sean Christopherson55d23752018-12-03 13:53:18 -0800770 return 0;
771}
772
Krish Sadhukhan61446ba2018-12-12 13:30:09 -0500773static int nested_vmx_check_exit_msr_switch_controls(struct kvm_vcpu *vcpu,
774 struct vmcs12 *vmcs12)
Sean Christopherson55d23752018-12-03 13:53:18 -0800775{
Sean Christopherson5497b952019-07-11 08:58:29 -0700776 if (CC(nested_vmx_check_msr_switch(vcpu,
777 vmcs12->vm_exit_msr_load_count,
778 vmcs12->vm_exit_msr_load_addr)) ||
779 CC(nested_vmx_check_msr_switch(vcpu,
780 vmcs12->vm_exit_msr_store_count,
781 vmcs12->vm_exit_msr_store_addr)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800782 return -EINVAL;
Sean Christophersonf9b245e2018-12-12 13:30:08 -0500783
Sean Christopherson55d23752018-12-03 13:53:18 -0800784 return 0;
785}
786
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -0500787static int nested_vmx_check_entry_msr_switch_controls(struct kvm_vcpu *vcpu,
788 struct vmcs12 *vmcs12)
Krish Sadhukhan61446ba2018-12-12 13:30:09 -0500789{
Sean Christopherson5497b952019-07-11 08:58:29 -0700790 if (CC(nested_vmx_check_msr_switch(vcpu,
791 vmcs12->vm_entry_msr_load_count,
792 vmcs12->vm_entry_msr_load_addr)))
Krish Sadhukhan61446ba2018-12-12 13:30:09 -0500793 return -EINVAL;
794
795 return 0;
796}
797
Sean Christopherson55d23752018-12-03 13:53:18 -0800798static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
799 struct vmcs12 *vmcs12)
800{
801 if (!nested_cpu_has_pml(vmcs12))
802 return 0;
803
Sean Christopherson5497b952019-07-11 08:58:29 -0700804 if (CC(!nested_cpu_has_ept(vmcs12)) ||
805 CC(!page_address_valid(vcpu, vmcs12->pml_address)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800806 return -EINVAL;
807
808 return 0;
809}
810
811static int nested_vmx_check_unrestricted_guest_controls(struct kvm_vcpu *vcpu,
812 struct vmcs12 *vmcs12)
813{
Sean Christopherson5497b952019-07-11 08:58:29 -0700814 if (CC(nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST) &&
815 !nested_cpu_has_ept(vmcs12)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800816 return -EINVAL;
817 return 0;
818}
819
820static int nested_vmx_check_mode_based_ept_exec_controls(struct kvm_vcpu *vcpu,
821 struct vmcs12 *vmcs12)
822{
Sean Christopherson5497b952019-07-11 08:58:29 -0700823 if (CC(nested_cpu_has2(vmcs12, SECONDARY_EXEC_MODE_BASED_EPT_EXEC) &&
824 !nested_cpu_has_ept(vmcs12)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800825 return -EINVAL;
826 return 0;
827}
828
829static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
830 struct vmcs12 *vmcs12)
831{
832 if (!nested_cpu_has_shadow_vmcs(vmcs12))
833 return 0;
834
Sean Christopherson5497b952019-07-11 08:58:29 -0700835 if (CC(!page_address_valid(vcpu, vmcs12->vmread_bitmap)) ||
836 CC(!page_address_valid(vcpu, vmcs12->vmwrite_bitmap)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800837 return -EINVAL;
838
839 return 0;
840}
841
842static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
843 struct vmx_msr_entry *e)
844{
845 /* x2APIC MSR accesses are not allowed */
Sean Christopherson5497b952019-07-11 08:58:29 -0700846 if (CC(vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8))
Sean Christopherson55d23752018-12-03 13:53:18 -0800847 return -EINVAL;
Sean Christopherson5497b952019-07-11 08:58:29 -0700848 if (CC(e->index == MSR_IA32_UCODE_WRITE) || /* SDM Table 35-2 */
849 CC(e->index == MSR_IA32_UCODE_REV))
Sean Christopherson55d23752018-12-03 13:53:18 -0800850 return -EINVAL;
Sean Christopherson5497b952019-07-11 08:58:29 -0700851 if (CC(e->reserved != 0))
Sean Christopherson55d23752018-12-03 13:53:18 -0800852 return -EINVAL;
853 return 0;
854}
855
856static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
857 struct vmx_msr_entry *e)
858{
Sean Christopherson5497b952019-07-11 08:58:29 -0700859 if (CC(e->index == MSR_FS_BASE) ||
860 CC(e->index == MSR_GS_BASE) ||
861 CC(e->index == MSR_IA32_SMM_MONITOR_CTL) || /* SMM is not supported */
Sean Christopherson55d23752018-12-03 13:53:18 -0800862 nested_vmx_msr_check_common(vcpu, e))
863 return -EINVAL;
864 return 0;
865}
866
867static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
868 struct vmx_msr_entry *e)
869{
Sean Christopherson5497b952019-07-11 08:58:29 -0700870 if (CC(e->index == MSR_IA32_SMBASE) || /* SMM is not supported */
Sean Christopherson55d23752018-12-03 13:53:18 -0800871 nested_vmx_msr_check_common(vcpu, e))
872 return -EINVAL;
873 return 0;
874}
875
Marc Orrf0b51052019-09-17 11:50:57 -0700876static u32 nested_vmx_max_atomic_switch_msrs(struct kvm_vcpu *vcpu)
877{
878 struct vcpu_vmx *vmx = to_vmx(vcpu);
879 u64 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
880 vmx->nested.msrs.misc_high);
881
882 return (vmx_misc_max_msr(vmx_misc) + 1) * VMX_MISC_MSR_LIST_MULTIPLIER;
883}
884
Sean Christopherson55d23752018-12-03 13:53:18 -0800885/*
886 * Load guest's/host's msr at nested entry/exit.
887 * return 0 for success, entry index for failure.
Marc Orrf0b51052019-09-17 11:50:57 -0700888 *
889 * One of the failure modes for MSR load/store is when a list exceeds the
890 * virtual hardware's capacity. To maintain compatibility with hardware inasmuch
891 * as possible, process all valid entries before failing rather than precheck
892 * for a capacity violation.
Sean Christopherson55d23752018-12-03 13:53:18 -0800893 */
894static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
895{
896 u32 i;
897 struct vmx_msr_entry e;
Marc Orrf0b51052019-09-17 11:50:57 -0700898 u32 max_msr_list_size = nested_vmx_max_atomic_switch_msrs(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -0800899
Sean Christopherson55d23752018-12-03 13:53:18 -0800900 for (i = 0; i < count; i++) {
Marc Orrf0b51052019-09-17 11:50:57 -0700901 if (unlikely(i >= max_msr_list_size))
902 goto fail;
903
Sean Christopherson55d23752018-12-03 13:53:18 -0800904 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
905 &e, sizeof(e))) {
906 pr_debug_ratelimited(
907 "%s cannot read MSR entry (%u, 0x%08llx)\n",
908 __func__, i, gpa + i * sizeof(e));
909 goto fail;
910 }
911 if (nested_vmx_load_msr_check(vcpu, &e)) {
912 pr_debug_ratelimited(
913 "%s check failed (%u, 0x%x, 0x%x)\n",
914 __func__, i, e.index, e.reserved);
915 goto fail;
916 }
Sean Christophersonf20935d2019-09-05 14:22:54 -0700917 if (kvm_set_msr(vcpu, e.index, e.value)) {
Sean Christopherson55d23752018-12-03 13:53:18 -0800918 pr_debug_ratelimited(
919 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
920 __func__, i, e.index, e.value);
921 goto fail;
922 }
923 }
924 return 0;
925fail:
Sean Christopherson68cda402020-05-11 15:05:29 -0700926 /* Note, max_msr_list_size is at most 4096, i.e. this can't wrap. */
Sean Christopherson55d23752018-12-03 13:53:18 -0800927 return i + 1;
928}
929
Aaron Lewis662f1d12019-11-07 21:14:39 -0800930static bool nested_vmx_get_vmexit_msr_value(struct kvm_vcpu *vcpu,
931 u32 msr_index,
932 u64 *data)
933{
934 struct vcpu_vmx *vmx = to_vmx(vcpu);
935
936 /*
937 * If the L0 hypervisor stored a more accurate value for the TSC that
938 * does not include the time taken for emulation of the L2->L1
939 * VM-exit in L0, use the more accurate value.
940 */
941 if (msr_index == MSR_IA32_TSC) {
Sean Christophersona128a932020-09-23 11:03:57 -0700942 int i = vmx_find_loadstore_msr_slot(&vmx->msr_autostore.guest,
943 MSR_IA32_TSC);
Aaron Lewis662f1d12019-11-07 21:14:39 -0800944
Sean Christophersona128a932020-09-23 11:03:57 -0700945 if (i >= 0) {
946 u64 val = vmx->msr_autostore.guest.val[i].value;
Aaron Lewis662f1d12019-11-07 21:14:39 -0800947
948 *data = kvm_read_l1_tsc(vcpu, val);
949 return true;
950 }
951 }
952
953 if (kvm_get_msr(vcpu, msr_index, data)) {
954 pr_debug_ratelimited("%s cannot read MSR (0x%x)\n", __func__,
955 msr_index);
956 return false;
957 }
958 return true;
959}
960
Aaron Lewis365d3d52019-11-07 21:14:36 -0800961static bool read_and_check_msr_entry(struct kvm_vcpu *vcpu, u64 gpa, int i,
962 struct vmx_msr_entry *e)
963{
964 if (kvm_vcpu_read_guest(vcpu,
965 gpa + i * sizeof(*e),
966 e, 2 * sizeof(u32))) {
967 pr_debug_ratelimited(
968 "%s cannot read MSR entry (%u, 0x%08llx)\n",
969 __func__, i, gpa + i * sizeof(*e));
970 return false;
971 }
972 if (nested_vmx_store_msr_check(vcpu, e)) {
973 pr_debug_ratelimited(
974 "%s check failed (%u, 0x%x, 0x%x)\n",
975 __func__, i, e->index, e->reserved);
976 return false;
977 }
978 return true;
979}
980
Sean Christopherson55d23752018-12-03 13:53:18 -0800981static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
982{
Sean Christophersonf20935d2019-09-05 14:22:54 -0700983 u64 data;
Sean Christopherson55d23752018-12-03 13:53:18 -0800984 u32 i;
985 struct vmx_msr_entry e;
Marc Orrf0b51052019-09-17 11:50:57 -0700986 u32 max_msr_list_size = nested_vmx_max_atomic_switch_msrs(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -0800987
988 for (i = 0; i < count; i++) {
Marc Orrf0b51052019-09-17 11:50:57 -0700989 if (unlikely(i >= max_msr_list_size))
990 return -EINVAL;
991
Aaron Lewis365d3d52019-11-07 21:14:36 -0800992 if (!read_and_check_msr_entry(vcpu, gpa, i, &e))
Sean Christopherson55d23752018-12-03 13:53:18 -0800993 return -EINVAL;
Aaron Lewis365d3d52019-11-07 21:14:36 -0800994
Aaron Lewis662f1d12019-11-07 21:14:39 -0800995 if (!nested_vmx_get_vmexit_msr_value(vcpu, e.index, &data))
Sean Christopherson55d23752018-12-03 13:53:18 -0800996 return -EINVAL;
Aaron Lewis662f1d12019-11-07 21:14:39 -0800997
Sean Christopherson55d23752018-12-03 13:53:18 -0800998 if (kvm_vcpu_write_guest(vcpu,
999 gpa + i * sizeof(e) +
1000 offsetof(struct vmx_msr_entry, value),
Sean Christophersonf20935d2019-09-05 14:22:54 -07001001 &data, sizeof(data))) {
Sean Christopherson55d23752018-12-03 13:53:18 -08001002 pr_debug_ratelimited(
1003 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Sean Christophersonf20935d2019-09-05 14:22:54 -07001004 __func__, i, e.index, data);
Sean Christopherson55d23752018-12-03 13:53:18 -08001005 return -EINVAL;
1006 }
1007 }
1008 return 0;
1009}
1010
Aaron Lewis662f1d12019-11-07 21:14:39 -08001011static bool nested_msr_store_list_has_msr(struct kvm_vcpu *vcpu, u32 msr_index)
1012{
1013 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1014 u32 count = vmcs12->vm_exit_msr_store_count;
1015 u64 gpa = vmcs12->vm_exit_msr_store_addr;
1016 struct vmx_msr_entry e;
1017 u32 i;
1018
1019 for (i = 0; i < count; i++) {
1020 if (!read_and_check_msr_entry(vcpu, gpa, i, &e))
1021 return false;
1022
1023 if (e.index == msr_index)
1024 return true;
1025 }
1026 return false;
1027}
1028
1029static void prepare_vmx_msr_autostore_list(struct kvm_vcpu *vcpu,
1030 u32 msr_index)
1031{
1032 struct vcpu_vmx *vmx = to_vmx(vcpu);
1033 struct vmx_msrs *autostore = &vmx->msr_autostore.guest;
1034 bool in_vmcs12_store_list;
Sean Christophersona128a932020-09-23 11:03:57 -07001035 int msr_autostore_slot;
Aaron Lewis662f1d12019-11-07 21:14:39 -08001036 bool in_autostore_list;
1037 int last;
1038
Sean Christophersona128a932020-09-23 11:03:57 -07001039 msr_autostore_slot = vmx_find_loadstore_msr_slot(autostore, msr_index);
1040 in_autostore_list = msr_autostore_slot >= 0;
Aaron Lewis662f1d12019-11-07 21:14:39 -08001041 in_vmcs12_store_list = nested_msr_store_list_has_msr(vcpu, msr_index);
1042
1043 if (in_vmcs12_store_list && !in_autostore_list) {
Sean Christophersonce833b22020-09-23 11:03:56 -07001044 if (autostore->nr == MAX_NR_LOADSTORE_MSRS) {
Aaron Lewis662f1d12019-11-07 21:14:39 -08001045 /*
1046 * Emulated VMEntry does not fail here. Instead a less
1047 * accurate value will be returned by
1048 * nested_vmx_get_vmexit_msr_value() using kvm_get_msr()
1049 * instead of reading the value from the vmcs02 VMExit
1050 * MSR-store area.
1051 */
1052 pr_warn_ratelimited(
1053 "Not enough msr entries in msr_autostore. Can't add msr %x\n",
1054 msr_index);
1055 return;
1056 }
1057 last = autostore->nr++;
1058 autostore->val[last].index = msr_index;
1059 } else if (!in_vmcs12_store_list && in_autostore_list) {
1060 last = --autostore->nr;
Sean Christophersona128a932020-09-23 11:03:57 -07001061 autostore->val[msr_autostore_slot] = autostore->val[last];
Aaron Lewis662f1d12019-11-07 21:14:39 -08001062 }
1063}
1064
Sean Christopherson55d23752018-12-03 13:53:18 -08001065/*
Sean Christophersonea79a752020-02-04 07:32:59 -08001066 * Load guest's/host's cr3 at nested entry/exit. @nested_ept is true if we are
1067 * emulating VM-Entry into a guest with EPT enabled. On failure, the expected
1068 * Exit Qualification (for a VM-Entry consistency check VM-Exit) is assigned to
1069 * @entry_failure_code.
Sean Christopherson55d23752018-12-03 13:53:18 -08001070 */
Maxim Levitsky0f857222021-06-07 12:02:00 +03001071static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,
1072 bool nested_ept, bool reload_pdptrs,
Sean Christopherson68cda402020-05-11 15:05:29 -07001073 enum vm_entry_failure_code *entry_failure_code)
Sean Christopherson55d23752018-12-03 13:53:18 -08001074{
Sean Christopherson636e8b72021-02-03 16:01:10 -08001075 if (CC(kvm_vcpu_is_illegal_gpa(vcpu, cr3))) {
Sean Christopherson0cc69202020-05-01 21:32:26 -07001076 *entry_failure_code = ENTRY_FAIL_DEFAULT;
1077 return -EINVAL;
1078 }
Sean Christopherson55d23752018-12-03 13:53:18 -08001079
Sean Christopherson0cc69202020-05-01 21:32:26 -07001080 /*
1081 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
1082 * must not be dereferenced.
1083 */
Maxim Levitsky0f857222021-06-07 12:02:00 +03001084 if (reload_pdptrs && !nested_ept && is_pae_paging(vcpu) &&
Sean Christophersonbcb72d02021-06-07 12:01:56 +03001085 CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))) {
1086 *entry_failure_code = ENTRY_FAIL_PDPTE;
1087 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08001088 }
1089
Sean Christopherson50a41792021-06-09 16:42:28 -07001090 if (!nested_ept)
Sean Christophersonb5129102021-06-09 16:42:27 -07001091 kvm_mmu_new_pgd(vcpu, cr3);
Sean Christopherson07ffaf32021-06-09 16:42:21 -07001092
Sean Christopherson55d23752018-12-03 13:53:18 -08001093 vcpu->arch.cr3 = cr3;
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07001094 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Sean Christopherson55d23752018-12-03 13:53:18 -08001095
1096 kvm_init_mmu(vcpu, false);
1097
1098 return 0;
1099}
1100
1101/*
1102 * Returns if KVM is able to config CPU to tag TLB entries
1103 * populated by L2 differently than TLB entries populated
1104 * by L1.
1105 *
Liran Alon992edea2019-11-20 14:24:52 +02001106 * If L0 uses EPT, L1 and L2 run with different EPTP because
1107 * guest_mode is part of kvm_mmu_page_role. Thus, TLB entries
1108 * are tagged with different EPTP.
Sean Christopherson55d23752018-12-03 13:53:18 -08001109 *
1110 * If L1 uses VPID and we allocated a vpid02, TLB entries are tagged
1111 * with different VPID (L1 entries are tagged with vmx->vpid
1112 * while L2 entries are tagged with vmx->nested.vpid02).
1113 */
1114static bool nested_has_guest_tlb_tag(struct kvm_vcpu *vcpu)
1115{
1116 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1117
Liran Alon992edea2019-11-20 14:24:52 +02001118 return enable_ept ||
Sean Christopherson55d23752018-12-03 13:53:18 -08001119 (nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02);
1120}
1121
Sean Christopherson50b265a2020-03-20 14:28:19 -07001122static void nested_vmx_transition_tlb_flush(struct kvm_vcpu *vcpu,
1123 struct vmcs12 *vmcs12,
1124 bool is_vmenter)
1125{
1126 struct vcpu_vmx *vmx = to_vmx(vcpu);
1127
1128 /*
Sean Christopherson50a41792021-06-09 16:42:28 -07001129 * If vmcs12 doesn't use VPID, L1 expects linear and combined mappings
1130 * for *all* contexts to be flushed on VM-Enter/VM-Exit, i.e. it's a
1131 * full TLB flush from the guest's perspective. This is required even
1132 * if VPID is disabled in the host as KVM may need to synchronize the
1133 * MMU in response to the guest TLB flush.
1134 *
1135 * Note, using TLB_FLUSH_GUEST is correct even if nested EPT is in use.
1136 * EPT is a special snowflake, as guest-physical mappings aren't
1137 * flushed on VPID invalidations, including VM-Enter or VM-Exit with
1138 * VPID disabled. As a result, KVM _never_ needs to sync nEPT
1139 * entries on VM-Enter because L1 can't rely on VM-Enter to flush
1140 * those mappings.
Sean Christopherson50b265a2020-03-20 14:28:19 -07001141 */
Sean Christopherson50a41792021-06-09 16:42:28 -07001142 if (!nested_cpu_has_vpid(vmcs12)) {
1143 kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu);
Sean Christopherson50b265a2020-03-20 14:28:19 -07001144 return;
Sean Christopherson50a41792021-06-09 16:42:28 -07001145 }
1146
1147 /* L2 should never have a VPID if VPID is disabled. */
1148 WARN_ON(!enable_vpid);
Sean Christopherson50b265a2020-03-20 14:28:19 -07001149
1150 /*
Sean Christopherson50b265a2020-03-20 14:28:19 -07001151 * If VPID is enabled and used by vmc12, but L2 does not have a unique
1152 * TLB tag (ASID), i.e. EPT is disabled and KVM was unable to allocate
Sean Christophersonc51e1ff2020-03-20 14:28:22 -07001153 * a VPID for L2, flush the current context as the effective ASID is
1154 * common to both L1 and L2.
Sean Christopherson50b265a2020-03-20 14:28:19 -07001155 *
1156 * Defer the flush so that it runs after vmcs02.EPTP has been set by
1157 * KVM_REQ_LOAD_MMU_PGD (if nested EPT is enabled) and to avoid
1158 * redundant flushes further down the nested pipeline.
1159 *
1160 * If a TLB flush isn't required due to any of the above, and vpid12 is
1161 * changing then the new "virtual" VPID (vpid12) will reuse the same
Sean Christopherson50a41792021-06-09 16:42:28 -07001162 * "real" VPID (vpid02), and so needs to be flushed. There's no direct
Sean Christopherson50b265a2020-03-20 14:28:19 -07001163 * mapping between vpid02 and vpid12, vpid02 is per-vCPU and reused for
Sean Christopherson50a41792021-06-09 16:42:28 -07001164 * all nested vCPUs. Remember, a flush on VM-Enter does not invalidate
1165 * guest-physical mappings, so there is no need to sync the nEPT MMU.
Sean Christopherson50b265a2020-03-20 14:28:19 -07001166 */
Sean Christopherson50a41792021-06-09 16:42:28 -07001167 if (!nested_has_guest_tlb_tag(vcpu)) {
Sean Christophersonc51e1ff2020-03-20 14:28:22 -07001168 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
Sean Christopherson50b265a2020-03-20 14:28:19 -07001169 } else if (is_vmenter &&
1170 vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
1171 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
1172 vpid_sync_context(nested_get_vpid02(vcpu));
1173 }
1174}
1175
Sean Christopherson55d23752018-12-03 13:53:18 -08001176static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
1177{
1178 superset &= mask;
1179 subset &= mask;
1180
1181 return (superset | subset) == superset;
1182}
1183
1184static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
1185{
1186 const u64 feature_and_reserved =
1187 /* feature (except bit 48; see below) */
1188 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
1189 /* reserved */
1190 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
1191 u64 vmx_basic = vmx->nested.msrs.basic;
1192
1193 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
1194 return -EINVAL;
1195
1196 /*
1197 * KVM does not emulate a version of VMX that constrains physical
1198 * addresses of VMX structures (e.g. VMCS) to 32-bits.
1199 */
1200 if (data & BIT_ULL(48))
1201 return -EINVAL;
1202
1203 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
1204 vmx_basic_vmcs_revision_id(data))
1205 return -EINVAL;
1206
1207 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
1208 return -EINVAL;
1209
1210 vmx->nested.msrs.basic = data;
1211 return 0;
1212}
1213
1214static int
1215vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
1216{
1217 u64 supported;
1218 u32 *lowp, *highp;
1219
1220 switch (msr_index) {
1221 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
1222 lowp = &vmx->nested.msrs.pinbased_ctls_low;
1223 highp = &vmx->nested.msrs.pinbased_ctls_high;
1224 break;
1225 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
1226 lowp = &vmx->nested.msrs.procbased_ctls_low;
1227 highp = &vmx->nested.msrs.procbased_ctls_high;
1228 break;
1229 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
1230 lowp = &vmx->nested.msrs.exit_ctls_low;
1231 highp = &vmx->nested.msrs.exit_ctls_high;
1232 break;
1233 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
1234 lowp = &vmx->nested.msrs.entry_ctls_low;
1235 highp = &vmx->nested.msrs.entry_ctls_high;
1236 break;
1237 case MSR_IA32_VMX_PROCBASED_CTLS2:
1238 lowp = &vmx->nested.msrs.secondary_ctls_low;
1239 highp = &vmx->nested.msrs.secondary_ctls_high;
1240 break;
1241 default:
1242 BUG();
1243 }
1244
1245 supported = vmx_control_msr(*lowp, *highp);
1246
1247 /* Check must-be-1 bits are still 1. */
1248 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
1249 return -EINVAL;
1250
1251 /* Check must-be-0 bits are still 0. */
1252 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
1253 return -EINVAL;
1254
1255 *lowp = data;
1256 *highp = data >> 32;
1257 return 0;
1258}
1259
1260static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
1261{
1262 const u64 feature_and_reserved_bits =
1263 /* feature */
1264 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
1265 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
1266 /* reserved */
1267 GENMASK_ULL(13, 9) | BIT_ULL(31);
1268 u64 vmx_misc;
1269
1270 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
1271 vmx->nested.msrs.misc_high);
1272
1273 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
1274 return -EINVAL;
1275
1276 if ((vmx->nested.msrs.pinbased_ctls_high &
1277 PIN_BASED_VMX_PREEMPTION_TIMER) &&
1278 vmx_misc_preemption_timer_rate(data) !=
1279 vmx_misc_preemption_timer_rate(vmx_misc))
1280 return -EINVAL;
1281
1282 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
1283 return -EINVAL;
1284
1285 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
1286 return -EINVAL;
1287
1288 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
1289 return -EINVAL;
1290
1291 vmx->nested.msrs.misc_low = data;
1292 vmx->nested.msrs.misc_high = data >> 32;
1293
Sean Christopherson55d23752018-12-03 13:53:18 -08001294 return 0;
1295}
1296
1297static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
1298{
1299 u64 vmx_ept_vpid_cap;
1300
1301 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
1302 vmx->nested.msrs.vpid_caps);
1303
1304 /* Every bit is either reserved or a feature bit. */
1305 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
1306 return -EINVAL;
1307
1308 vmx->nested.msrs.ept_caps = data;
1309 vmx->nested.msrs.vpid_caps = data >> 32;
1310 return 0;
1311}
1312
1313static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
1314{
1315 u64 *msr;
1316
1317 switch (msr_index) {
1318 case MSR_IA32_VMX_CR0_FIXED0:
1319 msr = &vmx->nested.msrs.cr0_fixed0;
1320 break;
1321 case MSR_IA32_VMX_CR4_FIXED0:
1322 msr = &vmx->nested.msrs.cr4_fixed0;
1323 break;
1324 default:
1325 BUG();
1326 }
1327
1328 /*
1329 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
1330 * must be 1 in the restored value.
1331 */
1332 if (!is_bitwise_subset(data, *msr, -1ULL))
1333 return -EINVAL;
1334
1335 *msr = data;
1336 return 0;
1337}
1338
1339/*
1340 * Called when userspace is restoring VMX MSRs.
1341 *
1342 * Returns 0 on success, non-0 otherwise.
1343 */
1344int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1345{
1346 struct vcpu_vmx *vmx = to_vmx(vcpu);
1347
1348 /*
1349 * Don't allow changes to the VMX capability MSRs while the vCPU
1350 * is in VMX operation.
1351 */
1352 if (vmx->nested.vmxon)
1353 return -EBUSY;
1354
1355 switch (msr_index) {
1356 case MSR_IA32_VMX_BASIC:
1357 return vmx_restore_vmx_basic(vmx, data);
1358 case MSR_IA32_VMX_PINBASED_CTLS:
1359 case MSR_IA32_VMX_PROCBASED_CTLS:
1360 case MSR_IA32_VMX_EXIT_CTLS:
1361 case MSR_IA32_VMX_ENTRY_CTLS:
1362 /*
1363 * The "non-true" VMX capability MSRs are generated from the
1364 * "true" MSRs, so we do not support restoring them directly.
1365 *
1366 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
1367 * should restore the "true" MSRs with the must-be-1 bits
1368 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
1369 * DEFAULT SETTINGS".
1370 */
1371 return -EINVAL;
1372 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
1373 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
1374 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
1375 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
1376 case MSR_IA32_VMX_PROCBASED_CTLS2:
1377 return vmx_restore_control_msr(vmx, msr_index, data);
1378 case MSR_IA32_VMX_MISC:
1379 return vmx_restore_vmx_misc(vmx, data);
1380 case MSR_IA32_VMX_CR0_FIXED0:
1381 case MSR_IA32_VMX_CR4_FIXED0:
1382 return vmx_restore_fixed0_msr(vmx, msr_index, data);
1383 case MSR_IA32_VMX_CR0_FIXED1:
1384 case MSR_IA32_VMX_CR4_FIXED1:
1385 /*
1386 * These MSRs are generated based on the vCPU's CPUID, so we
1387 * do not support restoring them directly.
1388 */
1389 return -EINVAL;
1390 case MSR_IA32_VMX_EPT_VPID_CAP:
1391 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
1392 case MSR_IA32_VMX_VMCS_ENUM:
1393 vmx->nested.msrs.vmcs_enum = data;
1394 return 0;
Paolo Bonzinie8a70bd2019-07-02 14:40:40 +02001395 case MSR_IA32_VMX_VMFUNC:
1396 if (data & ~vmx->nested.msrs.vmfunc_controls)
1397 return -EINVAL;
1398 vmx->nested.msrs.vmfunc_controls = data;
1399 return 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08001400 default:
1401 /*
1402 * The rest of the VMX capability MSRs do not support restore.
1403 */
1404 return -EINVAL;
1405 }
1406}
1407
1408/* Returns 0 on success, non-0 otherwise. */
1409int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
1410{
1411 switch (msr_index) {
1412 case MSR_IA32_VMX_BASIC:
1413 *pdata = msrs->basic;
1414 break;
1415 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
1416 case MSR_IA32_VMX_PINBASED_CTLS:
1417 *pdata = vmx_control_msr(
1418 msrs->pinbased_ctls_low,
1419 msrs->pinbased_ctls_high);
1420 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
1421 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
1422 break;
1423 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
1424 case MSR_IA32_VMX_PROCBASED_CTLS:
1425 *pdata = vmx_control_msr(
1426 msrs->procbased_ctls_low,
1427 msrs->procbased_ctls_high);
1428 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
1429 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
1430 break;
1431 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
1432 case MSR_IA32_VMX_EXIT_CTLS:
1433 *pdata = vmx_control_msr(
1434 msrs->exit_ctls_low,
1435 msrs->exit_ctls_high);
1436 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
1437 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
1438 break;
1439 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
1440 case MSR_IA32_VMX_ENTRY_CTLS:
1441 *pdata = vmx_control_msr(
1442 msrs->entry_ctls_low,
1443 msrs->entry_ctls_high);
1444 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
1445 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
1446 break;
1447 case MSR_IA32_VMX_MISC:
1448 *pdata = vmx_control_msr(
1449 msrs->misc_low,
1450 msrs->misc_high);
1451 break;
1452 case MSR_IA32_VMX_CR0_FIXED0:
1453 *pdata = msrs->cr0_fixed0;
1454 break;
1455 case MSR_IA32_VMX_CR0_FIXED1:
1456 *pdata = msrs->cr0_fixed1;
1457 break;
1458 case MSR_IA32_VMX_CR4_FIXED0:
1459 *pdata = msrs->cr4_fixed0;
1460 break;
1461 case MSR_IA32_VMX_CR4_FIXED1:
1462 *pdata = msrs->cr4_fixed1;
1463 break;
1464 case MSR_IA32_VMX_VMCS_ENUM:
1465 *pdata = msrs->vmcs_enum;
1466 break;
1467 case MSR_IA32_VMX_PROCBASED_CTLS2:
1468 *pdata = vmx_control_msr(
1469 msrs->secondary_ctls_low,
1470 msrs->secondary_ctls_high);
1471 break;
1472 case MSR_IA32_VMX_EPT_VPID_CAP:
1473 *pdata = msrs->ept_caps |
1474 ((u64)msrs->vpid_caps << 32);
1475 break;
1476 case MSR_IA32_VMX_VMFUNC:
1477 *pdata = msrs->vmfunc_controls;
1478 break;
1479 default:
1480 return 1;
1481 }
1482
1483 return 0;
1484}
1485
1486/*
Sean Christophersonfadcead2019-05-07 08:36:23 -07001487 * Copy the writable VMCS shadow fields back to the VMCS12, in case they have
1488 * been modified by the L1 guest. Note, "writable" in this context means
1489 * "writable by the guest", i.e. tagged SHADOW_FIELD_RW; the set of
1490 * fields tagged SHADOW_FIELD_RO may or may not align with the "read-only"
1491 * VM-exit information fields (which are actually writable if the vCPU is
1492 * configured to support "VMWRITE to any supported field in the VMCS").
Sean Christopherson55d23752018-12-03 13:53:18 -08001493 */
1494static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
1495{
Sean Christopherson55d23752018-12-03 13:53:18 -08001496 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Sean Christophersonfadcead2019-05-07 08:36:23 -07001497 struct vmcs12 *vmcs12 = get_vmcs12(&vmx->vcpu);
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07001498 struct shadow_vmcs_field field;
1499 unsigned long val;
Sean Christophersonfadcead2019-05-07 08:36:23 -07001500 int i;
Sean Christopherson55d23752018-12-03 13:53:18 -08001501
Paolo Bonzini88dddc12019-07-19 18:41:10 +02001502 if (WARN_ON(!shadow_vmcs))
1503 return;
1504
Sean Christopherson55d23752018-12-03 13:53:18 -08001505 preempt_disable();
1506
1507 vmcs_load(shadow_vmcs);
1508
Sean Christophersonfadcead2019-05-07 08:36:23 -07001509 for (i = 0; i < max_shadow_read_write_fields; i++) {
1510 field = shadow_read_write_fields[i];
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07001511 val = __vmcs_readl(field.encoding);
1512 vmcs12_write_any(vmcs12, field.encoding, field.offset, val);
Sean Christopherson55d23752018-12-03 13:53:18 -08001513 }
1514
1515 vmcs_clear(shadow_vmcs);
1516 vmcs_load(vmx->loaded_vmcs->vmcs);
1517
1518 preempt_enable();
1519}
1520
1521static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
1522{
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07001523 const struct shadow_vmcs_field *fields[] = {
Sean Christopherson55d23752018-12-03 13:53:18 -08001524 shadow_read_write_fields,
1525 shadow_read_only_fields
1526 };
1527 const int max_fields[] = {
1528 max_shadow_read_write_fields,
1529 max_shadow_read_only_fields
1530 };
Sean Christopherson55d23752018-12-03 13:53:18 -08001531 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07001532 struct vmcs12 *vmcs12 = get_vmcs12(&vmx->vcpu);
1533 struct shadow_vmcs_field field;
1534 unsigned long val;
1535 int i, q;
Sean Christopherson55d23752018-12-03 13:53:18 -08001536
Paolo Bonzini88dddc12019-07-19 18:41:10 +02001537 if (WARN_ON(!shadow_vmcs))
1538 return;
1539
Sean Christopherson55d23752018-12-03 13:53:18 -08001540 vmcs_load(shadow_vmcs);
1541
1542 for (q = 0; q < ARRAY_SIZE(fields); q++) {
1543 for (i = 0; i < max_fields[q]; i++) {
1544 field = fields[q][i];
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07001545 val = vmcs12_read_any(vmcs12, field.encoding,
1546 field.offset);
1547 __vmcs_writel(field.encoding, val);
Sean Christopherson55d23752018-12-03 13:53:18 -08001548 }
1549 }
1550
1551 vmcs_clear(shadow_vmcs);
1552 vmcs_load(vmx->loaded_vmcs->vmcs);
1553}
1554
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001555static void copy_enlightened_to_vmcs12(struct vcpu_vmx *vmx, u32 hv_clean_fields)
Sean Christopherson55d23752018-12-03 13:53:18 -08001556{
1557 struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12;
1558 struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
1559
1560 /* HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE */
1561 vmcs12->tpr_threshold = evmcs->tpr_threshold;
1562 vmcs12->guest_rip = evmcs->guest_rip;
1563
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001564 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001565 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC))) {
1566 vmcs12->guest_rsp = evmcs->guest_rsp;
1567 vmcs12->guest_rflags = evmcs->guest_rflags;
1568 vmcs12->guest_interruptibility_info =
1569 evmcs->guest_interruptibility_info;
1570 }
1571
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001572 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001573 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC))) {
1574 vmcs12->cpu_based_vm_exec_control =
1575 evmcs->cpu_based_vm_exec_control;
1576 }
1577
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001578 if (unlikely(!(hv_clean_fields &
Vitaly Kuznetsovf9bc5222019-06-13 13:35:02 +02001579 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EXCPN))) {
Sean Christopherson55d23752018-12-03 13:53:18 -08001580 vmcs12->exception_bitmap = evmcs->exception_bitmap;
1581 }
1582
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001583 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001584 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_ENTRY))) {
1585 vmcs12->vm_entry_controls = evmcs->vm_entry_controls;
1586 }
1587
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001588 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001589 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT))) {
1590 vmcs12->vm_entry_intr_info_field =
1591 evmcs->vm_entry_intr_info_field;
1592 vmcs12->vm_entry_exception_error_code =
1593 evmcs->vm_entry_exception_error_code;
1594 vmcs12->vm_entry_instruction_len =
1595 evmcs->vm_entry_instruction_len;
1596 }
1597
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001598 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001599 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1))) {
1600 vmcs12->host_ia32_pat = evmcs->host_ia32_pat;
1601 vmcs12->host_ia32_efer = evmcs->host_ia32_efer;
1602 vmcs12->host_cr0 = evmcs->host_cr0;
1603 vmcs12->host_cr3 = evmcs->host_cr3;
1604 vmcs12->host_cr4 = evmcs->host_cr4;
1605 vmcs12->host_ia32_sysenter_esp = evmcs->host_ia32_sysenter_esp;
1606 vmcs12->host_ia32_sysenter_eip = evmcs->host_ia32_sysenter_eip;
1607 vmcs12->host_rip = evmcs->host_rip;
1608 vmcs12->host_ia32_sysenter_cs = evmcs->host_ia32_sysenter_cs;
1609 vmcs12->host_es_selector = evmcs->host_es_selector;
1610 vmcs12->host_cs_selector = evmcs->host_cs_selector;
1611 vmcs12->host_ss_selector = evmcs->host_ss_selector;
1612 vmcs12->host_ds_selector = evmcs->host_ds_selector;
1613 vmcs12->host_fs_selector = evmcs->host_fs_selector;
1614 vmcs12->host_gs_selector = evmcs->host_gs_selector;
1615 vmcs12->host_tr_selector = evmcs->host_tr_selector;
1616 }
1617
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001618 if (unlikely(!(hv_clean_fields &
Vitaly Kuznetsovf9bc5222019-06-13 13:35:02 +02001619 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1))) {
Sean Christopherson55d23752018-12-03 13:53:18 -08001620 vmcs12->pin_based_vm_exec_control =
1621 evmcs->pin_based_vm_exec_control;
1622 vmcs12->vm_exit_controls = evmcs->vm_exit_controls;
1623 vmcs12->secondary_vm_exec_control =
1624 evmcs->secondary_vm_exec_control;
1625 }
1626
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001627 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001628 HV_VMX_ENLIGHTENED_CLEAN_FIELD_IO_BITMAP))) {
1629 vmcs12->io_bitmap_a = evmcs->io_bitmap_a;
1630 vmcs12->io_bitmap_b = evmcs->io_bitmap_b;
1631 }
1632
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001633 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001634 HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP))) {
1635 vmcs12->msr_bitmap = evmcs->msr_bitmap;
1636 }
1637
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001638 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001639 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2))) {
1640 vmcs12->guest_es_base = evmcs->guest_es_base;
1641 vmcs12->guest_cs_base = evmcs->guest_cs_base;
1642 vmcs12->guest_ss_base = evmcs->guest_ss_base;
1643 vmcs12->guest_ds_base = evmcs->guest_ds_base;
1644 vmcs12->guest_fs_base = evmcs->guest_fs_base;
1645 vmcs12->guest_gs_base = evmcs->guest_gs_base;
1646 vmcs12->guest_ldtr_base = evmcs->guest_ldtr_base;
1647 vmcs12->guest_tr_base = evmcs->guest_tr_base;
1648 vmcs12->guest_gdtr_base = evmcs->guest_gdtr_base;
1649 vmcs12->guest_idtr_base = evmcs->guest_idtr_base;
1650 vmcs12->guest_es_limit = evmcs->guest_es_limit;
1651 vmcs12->guest_cs_limit = evmcs->guest_cs_limit;
1652 vmcs12->guest_ss_limit = evmcs->guest_ss_limit;
1653 vmcs12->guest_ds_limit = evmcs->guest_ds_limit;
1654 vmcs12->guest_fs_limit = evmcs->guest_fs_limit;
1655 vmcs12->guest_gs_limit = evmcs->guest_gs_limit;
1656 vmcs12->guest_ldtr_limit = evmcs->guest_ldtr_limit;
1657 vmcs12->guest_tr_limit = evmcs->guest_tr_limit;
1658 vmcs12->guest_gdtr_limit = evmcs->guest_gdtr_limit;
1659 vmcs12->guest_idtr_limit = evmcs->guest_idtr_limit;
1660 vmcs12->guest_es_ar_bytes = evmcs->guest_es_ar_bytes;
1661 vmcs12->guest_cs_ar_bytes = evmcs->guest_cs_ar_bytes;
1662 vmcs12->guest_ss_ar_bytes = evmcs->guest_ss_ar_bytes;
1663 vmcs12->guest_ds_ar_bytes = evmcs->guest_ds_ar_bytes;
1664 vmcs12->guest_fs_ar_bytes = evmcs->guest_fs_ar_bytes;
1665 vmcs12->guest_gs_ar_bytes = evmcs->guest_gs_ar_bytes;
1666 vmcs12->guest_ldtr_ar_bytes = evmcs->guest_ldtr_ar_bytes;
1667 vmcs12->guest_tr_ar_bytes = evmcs->guest_tr_ar_bytes;
1668 vmcs12->guest_es_selector = evmcs->guest_es_selector;
1669 vmcs12->guest_cs_selector = evmcs->guest_cs_selector;
1670 vmcs12->guest_ss_selector = evmcs->guest_ss_selector;
1671 vmcs12->guest_ds_selector = evmcs->guest_ds_selector;
1672 vmcs12->guest_fs_selector = evmcs->guest_fs_selector;
1673 vmcs12->guest_gs_selector = evmcs->guest_gs_selector;
1674 vmcs12->guest_ldtr_selector = evmcs->guest_ldtr_selector;
1675 vmcs12->guest_tr_selector = evmcs->guest_tr_selector;
1676 }
1677
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001678 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001679 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2))) {
1680 vmcs12->tsc_offset = evmcs->tsc_offset;
1681 vmcs12->virtual_apic_page_addr = evmcs->virtual_apic_page_addr;
1682 vmcs12->xss_exit_bitmap = evmcs->xss_exit_bitmap;
1683 }
1684
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001685 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001686 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR))) {
1687 vmcs12->cr0_guest_host_mask = evmcs->cr0_guest_host_mask;
1688 vmcs12->cr4_guest_host_mask = evmcs->cr4_guest_host_mask;
1689 vmcs12->cr0_read_shadow = evmcs->cr0_read_shadow;
1690 vmcs12->cr4_read_shadow = evmcs->cr4_read_shadow;
1691 vmcs12->guest_cr0 = evmcs->guest_cr0;
1692 vmcs12->guest_cr3 = evmcs->guest_cr3;
1693 vmcs12->guest_cr4 = evmcs->guest_cr4;
1694 vmcs12->guest_dr7 = evmcs->guest_dr7;
1695 }
1696
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001697 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001698 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER))) {
1699 vmcs12->host_fs_base = evmcs->host_fs_base;
1700 vmcs12->host_gs_base = evmcs->host_gs_base;
1701 vmcs12->host_tr_base = evmcs->host_tr_base;
1702 vmcs12->host_gdtr_base = evmcs->host_gdtr_base;
1703 vmcs12->host_idtr_base = evmcs->host_idtr_base;
1704 vmcs12->host_rsp = evmcs->host_rsp;
1705 }
1706
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001707 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001708 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_XLAT))) {
1709 vmcs12->ept_pointer = evmcs->ept_pointer;
1710 vmcs12->virtual_processor_id = evmcs->virtual_processor_id;
1711 }
1712
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001713 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001714 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1))) {
1715 vmcs12->vmcs_link_pointer = evmcs->vmcs_link_pointer;
1716 vmcs12->guest_ia32_debugctl = evmcs->guest_ia32_debugctl;
1717 vmcs12->guest_ia32_pat = evmcs->guest_ia32_pat;
1718 vmcs12->guest_ia32_efer = evmcs->guest_ia32_efer;
1719 vmcs12->guest_pdptr0 = evmcs->guest_pdptr0;
1720 vmcs12->guest_pdptr1 = evmcs->guest_pdptr1;
1721 vmcs12->guest_pdptr2 = evmcs->guest_pdptr2;
1722 vmcs12->guest_pdptr3 = evmcs->guest_pdptr3;
1723 vmcs12->guest_pending_dbg_exceptions =
1724 evmcs->guest_pending_dbg_exceptions;
1725 vmcs12->guest_sysenter_esp = evmcs->guest_sysenter_esp;
1726 vmcs12->guest_sysenter_eip = evmcs->guest_sysenter_eip;
1727 vmcs12->guest_bndcfgs = evmcs->guest_bndcfgs;
1728 vmcs12->guest_activity_state = evmcs->guest_activity_state;
1729 vmcs12->guest_sysenter_cs = evmcs->guest_sysenter_cs;
1730 }
1731
1732 /*
1733 * Not used?
1734 * vmcs12->vm_exit_msr_store_addr = evmcs->vm_exit_msr_store_addr;
1735 * vmcs12->vm_exit_msr_load_addr = evmcs->vm_exit_msr_load_addr;
1736 * vmcs12->vm_entry_msr_load_addr = evmcs->vm_entry_msr_load_addr;
Sean Christopherson55d23752018-12-03 13:53:18 -08001737 * vmcs12->page_fault_error_code_mask =
1738 * evmcs->page_fault_error_code_mask;
1739 * vmcs12->page_fault_error_code_match =
1740 * evmcs->page_fault_error_code_match;
1741 * vmcs12->cr3_target_count = evmcs->cr3_target_count;
1742 * vmcs12->vm_exit_msr_store_count = evmcs->vm_exit_msr_store_count;
1743 * vmcs12->vm_exit_msr_load_count = evmcs->vm_exit_msr_load_count;
1744 * vmcs12->vm_entry_msr_load_count = evmcs->vm_entry_msr_load_count;
1745 */
1746
1747 /*
1748 * Read only fields:
1749 * vmcs12->guest_physical_address = evmcs->guest_physical_address;
1750 * vmcs12->vm_instruction_error = evmcs->vm_instruction_error;
1751 * vmcs12->vm_exit_reason = evmcs->vm_exit_reason;
1752 * vmcs12->vm_exit_intr_info = evmcs->vm_exit_intr_info;
1753 * vmcs12->vm_exit_intr_error_code = evmcs->vm_exit_intr_error_code;
1754 * vmcs12->idt_vectoring_info_field = evmcs->idt_vectoring_info_field;
1755 * vmcs12->idt_vectoring_error_code = evmcs->idt_vectoring_error_code;
1756 * vmcs12->vm_exit_instruction_len = evmcs->vm_exit_instruction_len;
1757 * vmcs12->vmx_instruction_info = evmcs->vmx_instruction_info;
1758 * vmcs12->exit_qualification = evmcs->exit_qualification;
1759 * vmcs12->guest_linear_address = evmcs->guest_linear_address;
1760 *
1761 * Not present in struct vmcs12:
1762 * vmcs12->exit_io_instruction_ecx = evmcs->exit_io_instruction_ecx;
1763 * vmcs12->exit_io_instruction_esi = evmcs->exit_io_instruction_esi;
1764 * vmcs12->exit_io_instruction_edi = evmcs->exit_io_instruction_edi;
1765 * vmcs12->exit_io_instruction_eip = evmcs->exit_io_instruction_eip;
1766 */
1767
Vitaly Kuznetsov25641ca2021-05-26 15:20:19 +02001768 return;
Sean Christopherson55d23752018-12-03 13:53:18 -08001769}
1770
Vitaly Kuznetsov25641ca2021-05-26 15:20:19 +02001771static void copy_vmcs12_to_enlightened(struct vcpu_vmx *vmx)
Sean Christopherson55d23752018-12-03 13:53:18 -08001772{
1773 struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12;
1774 struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
1775
1776 /*
1777 * Should not be changed by KVM:
1778 *
1779 * evmcs->host_es_selector = vmcs12->host_es_selector;
1780 * evmcs->host_cs_selector = vmcs12->host_cs_selector;
1781 * evmcs->host_ss_selector = vmcs12->host_ss_selector;
1782 * evmcs->host_ds_selector = vmcs12->host_ds_selector;
1783 * evmcs->host_fs_selector = vmcs12->host_fs_selector;
1784 * evmcs->host_gs_selector = vmcs12->host_gs_selector;
1785 * evmcs->host_tr_selector = vmcs12->host_tr_selector;
1786 * evmcs->host_ia32_pat = vmcs12->host_ia32_pat;
1787 * evmcs->host_ia32_efer = vmcs12->host_ia32_efer;
1788 * evmcs->host_cr0 = vmcs12->host_cr0;
1789 * evmcs->host_cr3 = vmcs12->host_cr3;
1790 * evmcs->host_cr4 = vmcs12->host_cr4;
1791 * evmcs->host_ia32_sysenter_esp = vmcs12->host_ia32_sysenter_esp;
1792 * evmcs->host_ia32_sysenter_eip = vmcs12->host_ia32_sysenter_eip;
1793 * evmcs->host_rip = vmcs12->host_rip;
1794 * evmcs->host_ia32_sysenter_cs = vmcs12->host_ia32_sysenter_cs;
1795 * evmcs->host_fs_base = vmcs12->host_fs_base;
1796 * evmcs->host_gs_base = vmcs12->host_gs_base;
1797 * evmcs->host_tr_base = vmcs12->host_tr_base;
1798 * evmcs->host_gdtr_base = vmcs12->host_gdtr_base;
1799 * evmcs->host_idtr_base = vmcs12->host_idtr_base;
1800 * evmcs->host_rsp = vmcs12->host_rsp;
Sean Christopherson3731905ef2019-05-07 08:36:27 -07001801 * sync_vmcs02_to_vmcs12() doesn't read these:
Sean Christopherson55d23752018-12-03 13:53:18 -08001802 * evmcs->io_bitmap_a = vmcs12->io_bitmap_a;
1803 * evmcs->io_bitmap_b = vmcs12->io_bitmap_b;
1804 * evmcs->msr_bitmap = vmcs12->msr_bitmap;
1805 * evmcs->ept_pointer = vmcs12->ept_pointer;
1806 * evmcs->xss_exit_bitmap = vmcs12->xss_exit_bitmap;
1807 * evmcs->vm_exit_msr_store_addr = vmcs12->vm_exit_msr_store_addr;
1808 * evmcs->vm_exit_msr_load_addr = vmcs12->vm_exit_msr_load_addr;
1809 * evmcs->vm_entry_msr_load_addr = vmcs12->vm_entry_msr_load_addr;
Sean Christopherson55d23752018-12-03 13:53:18 -08001810 * evmcs->tpr_threshold = vmcs12->tpr_threshold;
1811 * evmcs->virtual_processor_id = vmcs12->virtual_processor_id;
1812 * evmcs->exception_bitmap = vmcs12->exception_bitmap;
1813 * evmcs->vmcs_link_pointer = vmcs12->vmcs_link_pointer;
1814 * evmcs->pin_based_vm_exec_control = vmcs12->pin_based_vm_exec_control;
1815 * evmcs->vm_exit_controls = vmcs12->vm_exit_controls;
1816 * evmcs->secondary_vm_exec_control = vmcs12->secondary_vm_exec_control;
1817 * evmcs->page_fault_error_code_mask =
1818 * vmcs12->page_fault_error_code_mask;
1819 * evmcs->page_fault_error_code_match =
1820 * vmcs12->page_fault_error_code_match;
1821 * evmcs->cr3_target_count = vmcs12->cr3_target_count;
1822 * evmcs->virtual_apic_page_addr = vmcs12->virtual_apic_page_addr;
1823 * evmcs->tsc_offset = vmcs12->tsc_offset;
1824 * evmcs->guest_ia32_debugctl = vmcs12->guest_ia32_debugctl;
1825 * evmcs->cr0_guest_host_mask = vmcs12->cr0_guest_host_mask;
1826 * evmcs->cr4_guest_host_mask = vmcs12->cr4_guest_host_mask;
1827 * evmcs->cr0_read_shadow = vmcs12->cr0_read_shadow;
1828 * evmcs->cr4_read_shadow = vmcs12->cr4_read_shadow;
1829 * evmcs->vm_exit_msr_store_count = vmcs12->vm_exit_msr_store_count;
1830 * evmcs->vm_exit_msr_load_count = vmcs12->vm_exit_msr_load_count;
1831 * evmcs->vm_entry_msr_load_count = vmcs12->vm_entry_msr_load_count;
1832 *
1833 * Not present in struct vmcs12:
1834 * evmcs->exit_io_instruction_ecx = vmcs12->exit_io_instruction_ecx;
1835 * evmcs->exit_io_instruction_esi = vmcs12->exit_io_instruction_esi;
1836 * evmcs->exit_io_instruction_edi = vmcs12->exit_io_instruction_edi;
1837 * evmcs->exit_io_instruction_eip = vmcs12->exit_io_instruction_eip;
1838 */
1839
1840 evmcs->guest_es_selector = vmcs12->guest_es_selector;
1841 evmcs->guest_cs_selector = vmcs12->guest_cs_selector;
1842 evmcs->guest_ss_selector = vmcs12->guest_ss_selector;
1843 evmcs->guest_ds_selector = vmcs12->guest_ds_selector;
1844 evmcs->guest_fs_selector = vmcs12->guest_fs_selector;
1845 evmcs->guest_gs_selector = vmcs12->guest_gs_selector;
1846 evmcs->guest_ldtr_selector = vmcs12->guest_ldtr_selector;
1847 evmcs->guest_tr_selector = vmcs12->guest_tr_selector;
1848
1849 evmcs->guest_es_limit = vmcs12->guest_es_limit;
1850 evmcs->guest_cs_limit = vmcs12->guest_cs_limit;
1851 evmcs->guest_ss_limit = vmcs12->guest_ss_limit;
1852 evmcs->guest_ds_limit = vmcs12->guest_ds_limit;
1853 evmcs->guest_fs_limit = vmcs12->guest_fs_limit;
1854 evmcs->guest_gs_limit = vmcs12->guest_gs_limit;
1855 evmcs->guest_ldtr_limit = vmcs12->guest_ldtr_limit;
1856 evmcs->guest_tr_limit = vmcs12->guest_tr_limit;
1857 evmcs->guest_gdtr_limit = vmcs12->guest_gdtr_limit;
1858 evmcs->guest_idtr_limit = vmcs12->guest_idtr_limit;
1859
1860 evmcs->guest_es_ar_bytes = vmcs12->guest_es_ar_bytes;
1861 evmcs->guest_cs_ar_bytes = vmcs12->guest_cs_ar_bytes;
1862 evmcs->guest_ss_ar_bytes = vmcs12->guest_ss_ar_bytes;
1863 evmcs->guest_ds_ar_bytes = vmcs12->guest_ds_ar_bytes;
1864 evmcs->guest_fs_ar_bytes = vmcs12->guest_fs_ar_bytes;
1865 evmcs->guest_gs_ar_bytes = vmcs12->guest_gs_ar_bytes;
1866 evmcs->guest_ldtr_ar_bytes = vmcs12->guest_ldtr_ar_bytes;
1867 evmcs->guest_tr_ar_bytes = vmcs12->guest_tr_ar_bytes;
1868
1869 evmcs->guest_es_base = vmcs12->guest_es_base;
1870 evmcs->guest_cs_base = vmcs12->guest_cs_base;
1871 evmcs->guest_ss_base = vmcs12->guest_ss_base;
1872 evmcs->guest_ds_base = vmcs12->guest_ds_base;
1873 evmcs->guest_fs_base = vmcs12->guest_fs_base;
1874 evmcs->guest_gs_base = vmcs12->guest_gs_base;
1875 evmcs->guest_ldtr_base = vmcs12->guest_ldtr_base;
1876 evmcs->guest_tr_base = vmcs12->guest_tr_base;
1877 evmcs->guest_gdtr_base = vmcs12->guest_gdtr_base;
1878 evmcs->guest_idtr_base = vmcs12->guest_idtr_base;
1879
1880 evmcs->guest_ia32_pat = vmcs12->guest_ia32_pat;
1881 evmcs->guest_ia32_efer = vmcs12->guest_ia32_efer;
1882
1883 evmcs->guest_pdptr0 = vmcs12->guest_pdptr0;
1884 evmcs->guest_pdptr1 = vmcs12->guest_pdptr1;
1885 evmcs->guest_pdptr2 = vmcs12->guest_pdptr2;
1886 evmcs->guest_pdptr3 = vmcs12->guest_pdptr3;
1887
1888 evmcs->guest_pending_dbg_exceptions =
1889 vmcs12->guest_pending_dbg_exceptions;
1890 evmcs->guest_sysenter_esp = vmcs12->guest_sysenter_esp;
1891 evmcs->guest_sysenter_eip = vmcs12->guest_sysenter_eip;
1892
1893 evmcs->guest_activity_state = vmcs12->guest_activity_state;
1894 evmcs->guest_sysenter_cs = vmcs12->guest_sysenter_cs;
1895
1896 evmcs->guest_cr0 = vmcs12->guest_cr0;
1897 evmcs->guest_cr3 = vmcs12->guest_cr3;
1898 evmcs->guest_cr4 = vmcs12->guest_cr4;
1899 evmcs->guest_dr7 = vmcs12->guest_dr7;
1900
1901 evmcs->guest_physical_address = vmcs12->guest_physical_address;
1902
1903 evmcs->vm_instruction_error = vmcs12->vm_instruction_error;
1904 evmcs->vm_exit_reason = vmcs12->vm_exit_reason;
1905 evmcs->vm_exit_intr_info = vmcs12->vm_exit_intr_info;
1906 evmcs->vm_exit_intr_error_code = vmcs12->vm_exit_intr_error_code;
1907 evmcs->idt_vectoring_info_field = vmcs12->idt_vectoring_info_field;
1908 evmcs->idt_vectoring_error_code = vmcs12->idt_vectoring_error_code;
1909 evmcs->vm_exit_instruction_len = vmcs12->vm_exit_instruction_len;
1910 evmcs->vmx_instruction_info = vmcs12->vmx_instruction_info;
1911
1912 evmcs->exit_qualification = vmcs12->exit_qualification;
1913
1914 evmcs->guest_linear_address = vmcs12->guest_linear_address;
1915 evmcs->guest_rsp = vmcs12->guest_rsp;
1916 evmcs->guest_rflags = vmcs12->guest_rflags;
1917
1918 evmcs->guest_interruptibility_info =
1919 vmcs12->guest_interruptibility_info;
1920 evmcs->cpu_based_vm_exec_control = vmcs12->cpu_based_vm_exec_control;
1921 evmcs->vm_entry_controls = vmcs12->vm_entry_controls;
1922 evmcs->vm_entry_intr_info_field = vmcs12->vm_entry_intr_info_field;
1923 evmcs->vm_entry_exception_error_code =
1924 vmcs12->vm_entry_exception_error_code;
1925 evmcs->vm_entry_instruction_len = vmcs12->vm_entry_instruction_len;
1926
1927 evmcs->guest_rip = vmcs12->guest_rip;
1928
1929 evmcs->guest_bndcfgs = vmcs12->guest_bndcfgs;
1930
Vitaly Kuznetsov25641ca2021-05-26 15:20:19 +02001931 return;
Sean Christopherson55d23752018-12-03 13:53:18 -08001932}
1933
1934/*
1935 * This is an equivalent of the nested hypervisor executing the vmptrld
1936 * instruction.
1937 */
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01001938static enum nested_evmptrld_status nested_vmx_handle_enlightened_vmptrld(
1939 struct kvm_vcpu *vcpu, bool from_launch)
Sean Christopherson55d23752018-12-03 13:53:18 -08001940{
1941 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsova21a39c2019-06-28 13:23:32 +02001942 bool evmcs_gpa_changed = false;
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02001943 u64 evmcs_gpa;
Sean Christopherson55d23752018-12-03 13:53:18 -08001944
1945 if (likely(!vmx->nested.enlightened_vmcs_enabled))
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01001946 return EVMPTRLD_DISABLED;
Sean Christopherson55d23752018-12-03 13:53:18 -08001947
Vitaly Kuznetsov02761712021-05-26 15:20:18 +02001948 if (!nested_enlightened_vmentry(vcpu, &evmcs_gpa)) {
1949 nested_release_evmcs(vcpu);
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01001950 return EVMPTRLD_DISABLED;
Vitaly Kuznetsov02761712021-05-26 15:20:18 +02001951 }
Sean Christopherson55d23752018-12-03 13:53:18 -08001952
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02001953 if (unlikely(evmcs_gpa != vmx->nested.hv_evmcs_vmptr)) {
1954 vmx->nested.current_vmptr = -1ull;
Sean Christopherson55d23752018-12-03 13:53:18 -08001955
1956 nested_release_evmcs(vcpu);
1957
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02001958 if (kvm_vcpu_map(vcpu, gpa_to_gfn(evmcs_gpa),
KarimAllah Ahmeddee9c042019-01-31 21:24:42 +01001959 &vmx->nested.hv_evmcs_map))
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01001960 return EVMPTRLD_ERROR;
Sean Christopherson55d23752018-12-03 13:53:18 -08001961
KarimAllah Ahmeddee9c042019-01-31 21:24:42 +01001962 vmx->nested.hv_evmcs = vmx->nested.hv_evmcs_map.hva;
Sean Christopherson55d23752018-12-03 13:53:18 -08001963
1964 /*
1965 * Currently, KVM only supports eVMCS version 1
1966 * (== KVM_EVMCS_VERSION) and thus we expect guest to set this
1967 * value to first u32 field of eVMCS which should specify eVMCS
1968 * VersionNumber.
1969 *
1970 * Guest should be aware of supported eVMCS versions by host by
1971 * examining CPUID.0x4000000A.EAX[0:15]. Host userspace VMM is
1972 * expected to set this CPUID leaf according to the value
1973 * returned in vmcs_version from nested_enable_evmcs().
1974 *
1975 * However, it turns out that Microsoft Hyper-V fails to comply
1976 * to their own invented interface: When Hyper-V use eVMCS, it
1977 * just sets first u32 field of eVMCS to revision_id specified
1978 * in MSR_IA32_VMX_BASIC. Instead of used eVMCS version number
1979 * which is one of the supported versions specified in
1980 * CPUID.0x4000000A.EAX[0:15].
1981 *
1982 * To overcome Hyper-V bug, we accept here either a supported
1983 * eVMCS version or VMCS12 revision_id as valid values for first
1984 * u32 field of eVMCS.
1985 */
1986 if ((vmx->nested.hv_evmcs->revision_id != KVM_EVMCS_VERSION) &&
1987 (vmx->nested.hv_evmcs->revision_id != VMCS12_REVISION)) {
1988 nested_release_evmcs(vcpu);
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01001989 return EVMPTRLD_VMFAIL;
Sean Christopherson55d23752018-12-03 13:53:18 -08001990 }
1991
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02001992 vmx->nested.hv_evmcs_vmptr = evmcs_gpa;
Sean Christopherson55d23752018-12-03 13:53:18 -08001993
Vitaly Kuznetsova21a39c2019-06-28 13:23:32 +02001994 evmcs_gpa_changed = true;
Sean Christopherson55d23752018-12-03 13:53:18 -08001995 /*
1996 * Unlike normal vmcs12, enlightened vmcs12 is not fully
1997 * reloaded from guest's memory (read only fields, fields not
1998 * present in struct hv_enlightened_vmcs, ...). Make sure there
1999 * are no leftovers.
2000 */
2001 if (from_launch) {
2002 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2003 memset(vmcs12, 0, sizeof(*vmcs12));
2004 vmcs12->hdr.revision_id = VMCS12_REVISION;
2005 }
2006
2007 }
Vitaly Kuznetsova21a39c2019-06-28 13:23:32 +02002008
2009 /*
Miaohe Linffdbd502020-02-07 23:22:45 +08002010 * Clean fields data can't be used on VMLAUNCH and when we switch
Vitaly Kuznetsova21a39c2019-06-28 13:23:32 +02002011 * between different L2 guests as KVM keeps a single VMCS12 per L1.
2012 */
2013 if (from_launch || evmcs_gpa_changed)
2014 vmx->nested.hv_evmcs->hv_clean_fields &=
2015 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
2016
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01002017 return EVMPTRLD_SUCCEEDED;
Sean Christopherson55d23752018-12-03 13:53:18 -08002018}
2019
Sean Christopherson3731905ef2019-05-07 08:36:27 -07002020void nested_sync_vmcs12_to_shadow(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08002021{
2022 struct vcpu_vmx *vmx = to_vmx(vcpu);
2023
Vitaly Kuznetsovdc313382021-05-26 15:20:24 +02002024 if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
Sean Christopherson55d23752018-12-03 13:53:18 -08002025 copy_vmcs12_to_enlightened(vmx);
Vitaly Kuznetsovdc313382021-05-26 15:20:24 +02002026 else
Sean Christopherson55d23752018-12-03 13:53:18 -08002027 copy_vmcs12_to_shadow(vmx);
Sean Christopherson55d23752018-12-03 13:53:18 -08002028
Sean Christopherson3731905ef2019-05-07 08:36:27 -07002029 vmx->nested.need_vmcs12_to_shadow_sync = false;
Sean Christopherson55d23752018-12-03 13:53:18 -08002030}
2031
2032static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
2033{
2034 struct vcpu_vmx *vmx =
2035 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
2036
2037 vmx->nested.preemption_timer_expired = true;
2038 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
2039 kvm_vcpu_kick(&vmx->vcpu);
2040
2041 return HRTIMER_NORESTART;
2042}
2043
Peter Shier850448f2020-05-26 14:51:06 -07002044static u64 vmx_calc_preemption_timer_value(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08002045{
Peter Shier850448f2020-05-26 14:51:06 -07002046 struct vcpu_vmx *vmx = to_vmx(vcpu);
2047 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Peter Shier850448f2020-05-26 14:51:06 -07002048
2049 u64 l1_scaled_tsc = kvm_read_l1_tsc(vcpu, rdtsc()) >>
2050 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
2051
2052 if (!vmx->nested.has_preemption_timer_deadline) {
Makarand Sonare8d7fbf02020-05-26 14:51:07 -07002053 vmx->nested.preemption_timer_deadline =
2054 vmcs12->vmx_preemption_timer_value + l1_scaled_tsc;
Peter Shier850448f2020-05-26 14:51:06 -07002055 vmx->nested.has_preemption_timer_deadline = true;
Makarand Sonare8d7fbf02020-05-26 14:51:07 -07002056 }
2057 return vmx->nested.preemption_timer_deadline - l1_scaled_tsc;
Peter Shier850448f2020-05-26 14:51:06 -07002058}
2059
2060static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu,
2061 u64 preemption_timeout)
2062{
Sean Christopherson55d23752018-12-03 13:53:18 -08002063 struct vcpu_vmx *vmx = to_vmx(vcpu);
2064
2065 /*
2066 * A timer value of zero is architecturally guaranteed to cause
2067 * a VMExit prior to executing any instructions in the guest.
2068 */
2069 if (preemption_timeout == 0) {
2070 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
2071 return;
2072 }
2073
2074 if (vcpu->arch.virtual_tsc_khz == 0)
2075 return;
2076
2077 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
2078 preemption_timeout *= 1000000;
2079 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
2080 hrtimer_start(&vmx->nested.preemption_timer,
Jim Mattsonada00982020-05-08 13:36:42 -07002081 ktime_add_ns(ktime_get(), preemption_timeout),
2082 HRTIMER_MODE_ABS_PINNED);
Sean Christopherson55d23752018-12-03 13:53:18 -08002083}
2084
2085static u64 nested_vmx_calc_efer(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
2086{
2087 if (vmx->nested.nested_run_pending &&
2088 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
2089 return vmcs12->guest_ia32_efer;
2090 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
2091 return vmx->vcpu.arch.efer | (EFER_LMA | EFER_LME);
2092 else
2093 return vmx->vcpu.arch.efer & ~(EFER_LMA | EFER_LME);
2094}
2095
2096static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx)
2097{
2098 /*
2099 * If vmcs02 hasn't been initialized, set the constant vmcs02 state
2100 * according to L0's settings (vmcs12 is irrelevant here). Host
2101 * fields that come from L0 and are not constant, e.g. HOST_CR3,
2102 * will be set as needed prior to VMLAUNCH/VMRESUME.
2103 */
2104 if (vmx->nested.vmcs02_initialized)
2105 return;
2106 vmx->nested.vmcs02_initialized = true;
2107
2108 /*
2109 * We don't care what the EPTP value is we just need to guarantee
2110 * it's valid so we don't get a false positive when doing early
2111 * consistency checks.
2112 */
2113 if (enable_ept && nested_early_check)
Sean Christopherson2a40b902020-07-15 20:41:18 -07002114 vmcs_write64(EPT_POINTER,
2115 construct_eptp(&vmx->vcpu, 0, PT64_ROOT_4LEVEL));
Sean Christopherson55d23752018-12-03 13:53:18 -08002116
2117 /* All VMFUNCs are currently emulated through L0 vmexits. */
2118 if (cpu_has_vmx_vmfunc())
2119 vmcs_write64(VM_FUNCTION_CONTROL, 0);
2120
2121 if (cpu_has_vmx_posted_intr())
2122 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
2123
2124 if (cpu_has_vmx_msr_bitmap())
2125 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
2126
Sean Christopherson4d6c9892019-05-07 09:06:30 -07002127 /*
Sean Christophersonc3bb9a22021-02-12 16:50:07 -08002128 * PML is emulated for L2, but never enabled in hardware as the MMU
2129 * handles A/D emulation. Disabling PML for L2 also avoids having to
2130 * deal with filtering out L2 GPAs from the buffer.
Sean Christopherson4d6c9892019-05-07 09:06:30 -07002131 */
2132 if (enable_pml) {
Sean Christophersonc3bb9a22021-02-12 16:50:07 -08002133 vmcs_write64(PML_ADDRESS, 0);
2134 vmcs_write16(GUEST_PML_INDEX, -1);
Sean Christopherson4d6c9892019-05-07 09:06:30 -07002135 }
Sean Christopherson55d23752018-12-03 13:53:18 -08002136
Sean Christophersonc538d572019-05-07 09:06:29 -07002137 if (cpu_has_vmx_encls_vmexit())
2138 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
Sean Christopherson55d23752018-12-03 13:53:18 -08002139
2140 /*
2141 * Set the MSR load/store lists to match L0's settings. Only the
2142 * addresses are constant (for vmcs02), the counts can change based
2143 * on L2's behavior, e.g. switching to/from long mode.
2144 */
Aaron Lewis662f1d12019-11-07 21:14:39 -08002145 vmcs_write64(VM_EXIT_MSR_STORE_ADDR, __pa(vmx->msr_autostore.guest.val));
Sean Christopherson55d23752018-12-03 13:53:18 -08002146 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
2147 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
2148
2149 vmx_set_constant_host_state(vmx);
2150}
2151
Paolo Bonzinib1346ab2019-06-06 17:24:00 +02002152static void prepare_vmcs02_early_rare(struct vcpu_vmx *vmx,
Sean Christopherson55d23752018-12-03 13:53:18 -08002153 struct vmcs12 *vmcs12)
2154{
2155 prepare_vmcs02_constant_state(vmx);
2156
2157 vmcs_write64(VMCS_LINK_POINTER, -1ull);
2158
2159 if (enable_vpid) {
2160 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
2161 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
2162 else
2163 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
2164 }
2165}
2166
2167static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
2168{
Sean Christophersonc3bb9a22021-02-12 16:50:07 -08002169 u32 exec_control;
Sean Christopherson55d23752018-12-03 13:53:18 -08002170 u64 guest_efer = nested_vmx_calc_efer(vmx, vmcs12);
2171
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02002172 if (vmx->nested.dirty_vmcs12 || evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
Paolo Bonzinib1346ab2019-06-06 17:24:00 +02002173 prepare_vmcs02_early_rare(vmx, vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08002174
2175 /*
Sean Christopherson55d23752018-12-03 13:53:18 -08002176 * PIN CONTROLS
2177 */
Sean Christophersonc075c3e2019-05-07 12:17:53 -07002178 exec_control = vmx_pin_based_exec_ctrl(vmx);
Sean Christopherson804939e2019-05-07 12:18:05 -07002179 exec_control |= (vmcs12->pin_based_vm_exec_control &
2180 ~PIN_BASED_VMX_PREEMPTION_TIMER);
Sean Christopherson55d23752018-12-03 13:53:18 -08002181
2182 /* Posted interrupts setting is only taken from vmcs12. */
2183 if (nested_cpu_has_posted_intr(vmcs12)) {
2184 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
2185 vmx->nested.pi_pending = false;
2186 } else {
2187 exec_control &= ~PIN_BASED_POSTED_INTR;
2188 }
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002189 pin_controls_set(vmx, exec_control);
Sean Christopherson55d23752018-12-03 13:53:18 -08002190
2191 /*
2192 * EXEC CONTROLS
2193 */
2194 exec_control = vmx_exec_control(vmx); /* L0's desires */
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08002195 exec_control &= ~CPU_BASED_INTR_WINDOW_EXITING;
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08002196 exec_control &= ~CPU_BASED_NMI_WINDOW_EXITING;
Sean Christopherson55d23752018-12-03 13:53:18 -08002197 exec_control &= ~CPU_BASED_TPR_SHADOW;
2198 exec_control |= vmcs12->cpu_based_vm_exec_control;
2199
Liran Alon02d496cf2019-11-11 14:30:55 +02002200 vmx->nested.l1_tpr_threshold = -1;
Sean Christophersonca2f5462019-05-07 09:06:33 -07002201 if (exec_control & CPU_BASED_TPR_SHADOW)
Sean Christopherson55d23752018-12-03 13:53:18 -08002202 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Sean Christopherson55d23752018-12-03 13:53:18 -08002203#ifdef CONFIG_X86_64
Sean Christophersonca2f5462019-05-07 09:06:33 -07002204 else
Sean Christopherson55d23752018-12-03 13:53:18 -08002205 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
2206 CPU_BASED_CR8_STORE_EXITING;
2207#endif
Sean Christopherson55d23752018-12-03 13:53:18 -08002208
2209 /*
2210 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
2211 * for I/O port accesses.
2212 */
Sean Christopherson55d23752018-12-03 13:53:18 -08002213 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
Sean Christophersonde0286b2019-05-07 12:18:01 -07002214 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
2215
2216 /*
2217 * This bit will be computed in nested_get_vmcs12_pages, because
2218 * we do not have access to L1's MSR bitmap yet. For now, keep
2219 * the same bit as before, hoping to avoid multiple VMWRITEs that
2220 * only set/clear this bit.
2221 */
2222 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
2223 exec_control |= exec_controls_get(vmx) & CPU_BASED_USE_MSR_BITMAPS;
2224
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002225 exec_controls_set(vmx, exec_control);
Sean Christopherson55d23752018-12-03 13:53:18 -08002226
2227 /*
2228 * SECONDARY EXEC CONTROLS
2229 */
2230 if (cpu_has_secondary_exec_ctrls()) {
2231 exec_control = vmx->secondary_exec_control;
2232
2233 /* Take the following fields only from vmcs12 */
2234 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
2235 SECONDARY_EXEC_ENABLE_INVPCID |
Sean Christopherson7f3603b2020-09-23 09:50:47 -07002236 SECONDARY_EXEC_ENABLE_RDTSCP |
Sean Christopherson55d23752018-12-03 13:53:18 -08002237 SECONDARY_EXEC_XSAVES |
Tao Xue69e72fa2019-07-16 14:55:49 +08002238 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE |
Sean Christopherson55d23752018-12-03 13:53:18 -08002239 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
2240 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Ilias Stamatisd041b5e2021-05-26 19:44:17 +01002241 SECONDARY_EXEC_ENABLE_VMFUNC |
2242 SECONDARY_EXEC_TSC_SCALING);
Sean Christopherson55d23752018-12-03 13:53:18 -08002243 if (nested_cpu_has(vmcs12,
Sean Christophersonc3bb9a22021-02-12 16:50:07 -08002244 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
2245 exec_control |= vmcs12->secondary_vm_exec_control;
2246
2247 /* PML is emulated and never enabled in hardware for L2. */
2248 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Sean Christopherson55d23752018-12-03 13:53:18 -08002249
2250 /* VMCS shadowing for L2 is emulated for now */
2251 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
2252
Sean Christopherson469debd2019-05-07 12:18:02 -07002253 /*
2254 * Preset *DT exiting when emulating UMIP, so that vmx_set_cr4()
2255 * will not have to rewrite the controls just for this bit.
2256 */
2257 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated() &&
2258 (vmcs12->guest_cr4 & X86_CR4_UMIP))
2259 exec_control |= SECONDARY_EXEC_DESC;
2260
Sean Christopherson55d23752018-12-03 13:53:18 -08002261 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
2262 vmcs_write16(GUEST_INTR_STATUS,
2263 vmcs12->guest_intr_status);
2264
Krish Sadhukhanbddd82d2020-09-21 08:10:25 +00002265 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
2266 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
2267
Sean Christopherson72add912021-04-12 16:21:42 +12002268 if (exec_control & SECONDARY_EXEC_ENCLS_EXITING)
2269 vmx_write_encls_bitmap(&vmx->vcpu, vmcs12);
2270
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002271 secondary_exec_controls_set(vmx, exec_control);
Sean Christopherson55d23752018-12-03 13:53:18 -08002272 }
2273
2274 /*
2275 * ENTRY CONTROLS
2276 *
2277 * vmcs12's VM_{ENTRY,EXIT}_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE
2278 * are emulated by vmx_set_efer() in prepare_vmcs02(), but speculate
2279 * on the related bits (if supported by the CPU) in the hope that
2280 * we can avoid VMWrites during vmx_set_efer().
2281 */
2282 exec_control = (vmcs12->vm_entry_controls | vmx_vmentry_ctrl()) &
2283 ~VM_ENTRY_IA32E_MODE & ~VM_ENTRY_LOAD_IA32_EFER;
2284 if (cpu_has_load_ia32_efer()) {
2285 if (guest_efer & EFER_LMA)
2286 exec_control |= VM_ENTRY_IA32E_MODE;
2287 if (guest_efer != host_efer)
2288 exec_control |= VM_ENTRY_LOAD_IA32_EFER;
2289 }
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002290 vm_entry_controls_set(vmx, exec_control);
Sean Christopherson55d23752018-12-03 13:53:18 -08002291
2292 /*
2293 * EXIT CONTROLS
2294 *
2295 * L2->L1 exit controls are emulated - the hardware exit is to L0 so
2296 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
2297 * bits may be modified by vmx_set_efer() in prepare_vmcs02().
2298 */
2299 exec_control = vmx_vmexit_ctrl();
2300 if (cpu_has_load_ia32_efer() && guest_efer != host_efer)
2301 exec_control |= VM_EXIT_LOAD_IA32_EFER;
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002302 vm_exit_controls_set(vmx, exec_control);
Sean Christopherson55d23752018-12-03 13:53:18 -08002303
2304 /*
2305 * Interrupt/Exception Fields
2306 */
2307 if (vmx->nested.nested_run_pending) {
2308 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2309 vmcs12->vm_entry_intr_info_field);
2310 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
2311 vmcs12->vm_entry_exception_error_code);
2312 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2313 vmcs12->vm_entry_instruction_len);
2314 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
2315 vmcs12->guest_interruptibility_info);
2316 vmx->loaded_vmcs->nmi_known_unmasked =
2317 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
2318 } else {
2319 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
2320 }
2321}
2322
Paolo Bonzinib1346ab2019-06-06 17:24:00 +02002323static void prepare_vmcs02_rare(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
Sean Christopherson55d23752018-12-03 13:53:18 -08002324{
2325 struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs;
2326
2327 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
2328 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2)) {
2329 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
2330 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
2331 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
2332 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
2333 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
2334 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
2335 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
2336 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
2337 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
2338 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
2339 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
2340 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
2341 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
2342 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
2343 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
2344 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
2345 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
2346 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07002347 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
2348 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
Sean Christopherson55d23752018-12-03 13:53:18 -08002349 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
2350 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
2351 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
2352 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
2353 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
2354 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
2355 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
2356 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
2357 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
2358 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
2359 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
2360 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
2361 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
2362 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
2363 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
2364 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Sean Christophersonfc387d82020-09-23 11:44:46 -07002365
2366 vmx->segment_cache.bitmask = 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08002367 }
2368
2369 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
2370 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1)) {
2371 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
2372 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
2373 vmcs12->guest_pending_dbg_exceptions);
2374 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
2375 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
2376
2377 /*
2378 * L1 may access the L2's PDPTR, so save them to construct
2379 * vmcs12
2380 */
2381 if (enable_ept) {
2382 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
2383 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
2384 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
2385 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
2386 }
Sean Christophersonc27e5b02019-05-07 09:06:39 -07002387
2388 if (kvm_mpx_supported() && vmx->nested.nested_run_pending &&
2389 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
2390 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
Sean Christopherson55d23752018-12-03 13:53:18 -08002391 }
2392
2393 if (nested_cpu_has_xsaves(vmcs12))
2394 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
2395
2396 /*
2397 * Whether page-faults are trapped is determined by a combination of
Paolo Bonzinia0c13432020-07-10 17:48:08 +02002398 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF. If L0
2399 * doesn't care about page faults then we should set all of these to
2400 * L1's desires. However, if L0 does care about (some) page faults, it
2401 * is not easy (if at all possible?) to merge L0 and L1's desires, we
2402 * simply ask to exit on each and every L2 page fault. This is done by
2403 * setting MASK=MATCH=0 and (see below) EB.PF=1.
Sean Christopherson55d23752018-12-03 13:53:18 -08002404 * Note that below we don't need special code to set EB.PF beyond the
2405 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
2406 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
2407 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
2408 */
Paolo Bonzinia0c13432020-07-10 17:48:08 +02002409 if (vmx_need_pf_intercept(&vmx->vcpu)) {
2410 /*
2411 * TODO: if both L0 and L1 need the same MASK and MATCH,
2412 * go ahead and use it?
2413 */
2414 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
2415 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
2416 } else {
2417 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, vmcs12->page_fault_error_code_mask);
2418 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, vmcs12->page_fault_error_code_match);
2419 }
Sean Christopherson55d23752018-12-03 13:53:18 -08002420
2421 if (cpu_has_vmx_apicv()) {
2422 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
2423 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
2424 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
2425 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
2426 }
2427
Aaron Lewis662f1d12019-11-07 21:14:39 -08002428 /*
2429 * Make sure the msr_autostore list is up to date before we set the
2430 * count in the vmcs02.
2431 */
2432 prepare_vmx_msr_autostore_list(&vmx->vcpu, MSR_IA32_TSC);
2433
2434 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, vmx->msr_autostore.guest.nr);
Sean Christopherson55d23752018-12-03 13:53:18 -08002435 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
2436 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
2437
2438 set_cr4_guest_host_mask(vmx);
Sean Christopherson55d23752018-12-03 13:53:18 -08002439}
2440
2441/*
2442 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
2443 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
2444 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
2445 * guest in a way that will both be appropriate to L1's requests, and our
2446 * needs. In addition to modifying the active vmcs (which is vmcs02), this
2447 * function also has additional necessary side-effects, like setting various
2448 * vcpu->arch fields.
2449 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
2450 * is assigned to entry_failure_code on failure.
2451 */
2452static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Maxim Levitsky0f857222021-06-07 12:02:00 +03002453 bool from_vmentry,
Sean Christopherson68cda402020-05-11 15:05:29 -07002454 enum vm_entry_failure_code *entry_failure_code)
Sean Christopherson55d23752018-12-03 13:53:18 -08002455{
2456 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersonc7554efc2019-05-07 09:06:40 -07002457 bool load_guest_pdptrs_vmcs12 = false;
Sean Christopherson55d23752018-12-03 13:53:18 -08002458
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02002459 if (vmx->nested.dirty_vmcs12 || evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) {
Paolo Bonzinib1346ab2019-06-06 17:24:00 +02002460 prepare_vmcs02_rare(vmx, vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08002461 vmx->nested.dirty_vmcs12 = false;
Sean Christopherson55d23752018-12-03 13:53:18 -08002462
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02002463 load_guest_pdptrs_vmcs12 = !evmptr_is_valid(vmx->nested.hv_evmcs_vmptr) ||
2464 !(vmx->nested.hv_evmcs->hv_clean_fields &
Sean Christophersonc7554efc2019-05-07 09:06:40 -07002465 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1);
Sean Christopherson55d23752018-12-03 13:53:18 -08002466 }
2467
2468 if (vmx->nested.nested_run_pending &&
2469 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
2470 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
2471 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
2472 } else {
2473 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
2474 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
2475 }
Sean Christopherson3b013a22019-05-07 09:06:28 -07002476 if (kvm_mpx_supported() && (!vmx->nested.nested_run_pending ||
2477 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)))
2478 vmcs_write64(GUEST_BNDCFGS, vmx->nested.vmcs01_guest_bndcfgs);
Sean Christopherson55d23752018-12-03 13:53:18 -08002479 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
2480
Sean Christopherson55d23752018-12-03 13:53:18 -08002481 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
2482 * bitwise-or of what L1 wants to trap for L2, and what we want to
2483 * trap. Note that CR0.TS also needs updating - we do this later.
2484 */
Jason Baronb6a7cc32021-01-14 22:27:54 -05002485 vmx_update_exception_bitmap(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08002486 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
2487 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
2488
2489 if (vmx->nested.nested_run_pending &&
2490 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
2491 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
2492 vcpu->arch.pat = vmcs12->guest_ia32_pat;
2493 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2494 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
2495 }
2496
Ilias Stamatisd041b5e2021-05-26 19:44:17 +01002497 vcpu->arch.tsc_offset = kvm_calc_nested_tsc_offset(
2498 vcpu->arch.l1_tsc_offset,
2499 vmx_get_l2_tsc_offset(vcpu),
2500 vmx_get_l2_tsc_multiplier(vcpu));
2501
2502 vcpu->arch.tsc_scaling_ratio = kvm_calc_nested_tsc_multiplier(
2503 vcpu->arch.l1_tsc_scaling_ratio,
2504 vmx_get_l2_tsc_multiplier(vcpu));
2505
Sean Christopherson55d23752018-12-03 13:53:18 -08002506 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Sean Christopherson55d23752018-12-03 13:53:18 -08002507 if (kvm_has_tsc_control)
Ilias Stamatis1ab92872021-06-07 11:54:38 +01002508 vmcs_write64(TSC_MULTIPLIER, vcpu->arch.tsc_scaling_ratio);
Sean Christopherson55d23752018-12-03 13:53:18 -08002509
Sean Christopherson50b265a2020-03-20 14:28:19 -07002510 nested_vmx_transition_tlb_flush(vcpu, vmcs12, true);
Sean Christopherson55d23752018-12-03 13:53:18 -08002511
2512 if (nested_cpu_has_ept(vmcs12))
2513 nested_ept_init_mmu_context(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08002514
2515 /*
2516 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
2517 * bits which we consider mandatory enabled.
2518 * The CR0_READ_SHADOW is what L2 should have expected to read given
2519 * the specifications by L1; It's not enough to take
2520 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
2521 * have more bits than L1 expected.
2522 */
2523 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
2524 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
2525
2526 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
2527 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
2528
2529 vcpu->arch.efer = nested_vmx_calc_efer(vmx, vmcs12);
2530 /* Note: may modify VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
2531 vmx_set_efer(vcpu, vcpu->arch.efer);
2532
2533 /*
2534 * Guest state is invalid and unrestricted guest is disabled,
2535 * which means L1 attempted VMEntry to L2 with invalid state.
2536 * Fail the VMEntry.
2537 */
Sean Christopherson2ba44932020-09-23 11:44:48 -07002538 if (CC(!vmx_guest_state_valid(vcpu))) {
Sean Christopherson55d23752018-12-03 13:53:18 -08002539 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christophersonc80add02019-04-11 12:18:09 -07002540 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002541 }
2542
2543 /* Shadow page tables on either EPT or shadow page tables. */
2544 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Maxim Levitsky0f857222021-06-07 12:02:00 +03002545 from_vmentry, entry_failure_code))
Sean Christophersonc80add02019-04-11 12:18:09 -07002546 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002547
Sean Christopherson04f11ef2019-09-27 14:45:16 -07002548 /*
2549 * Immediately write vmcs02.GUEST_CR3. It will be propagated to vmcs12
2550 * on nested VM-Exit, which can occur without actually running L2 and
Paolo Bonzini727a7e22020-03-05 03:52:50 -05002551 * thus without hitting vmx_load_mmu_pgd(), e.g. if L1 is entering L2 with
Sean Christopherson04f11ef2019-09-27 14:45:16 -07002552 * vmcs12.GUEST_ACTIVITYSTATE=HLT, in which case KVM will intercept the
2553 * transition to HLT instead of running L2.
2554 */
2555 if (enable_ept)
2556 vmcs_writel(GUEST_CR3, vmcs12->guest_cr3);
2557
Sean Christophersonc7554efc2019-05-07 09:06:40 -07002558 /* Late preparation of GUEST_PDPTRs now that EFER and CRs are set. */
2559 if (load_guest_pdptrs_vmcs12 && nested_cpu_has_ept(vmcs12) &&
2560 is_pae_paging(vcpu)) {
2561 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
2562 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
2563 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
2564 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
2565 }
2566
Sean Christopherson55d23752018-12-03 13:53:18 -08002567 if (!enable_ept)
2568 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
2569
Oliver Upton71f73472019-11-13 16:17:19 -08002570 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) &&
Oliver Uptond1968422019-12-13 16:33:58 -08002571 WARN_ON_ONCE(kvm_set_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL,
2572 vmcs12->guest_ia32_perf_global_ctrl)))
Oliver Upton71f73472019-11-13 16:17:19 -08002573 return -EINVAL;
2574
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02002575 kvm_rsp_write(vcpu, vmcs12->guest_rsp);
2576 kvm_rip_write(vcpu, vmcs12->guest_rip);
Vitaly Kuznetsovdc313382021-05-26 15:20:24 +02002577
2578 /*
2579 * It was observed that genuine Hyper-V running in L1 doesn't reset
2580 * 'hv_clean_fields' by itself, it only sets the corresponding dirty
2581 * bits when it changes a field in eVMCS. Mark all fields as clean
2582 * here.
2583 */
2584 if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
2585 vmx->nested.hv_evmcs->hv_clean_fields |=
2586 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
2587
Sean Christopherson55d23752018-12-03 13:53:18 -08002588 return 0;
2589}
2590
2591static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
2592{
Sean Christopherson5497b952019-07-11 08:58:29 -07002593 if (CC(!nested_cpu_has_nmi_exiting(vmcs12) &&
2594 nested_cpu_has_virtual_nmis(vmcs12)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002595 return -EINVAL;
2596
Sean Christopherson5497b952019-07-11 08:58:29 -07002597 if (CC(!nested_cpu_has_virtual_nmis(vmcs12) &&
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08002598 nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002599 return -EINVAL;
2600
2601 return 0;
2602}
2603
Sean Christophersonac6389a2020-03-02 18:02:38 -08002604static bool nested_vmx_check_eptp(struct kvm_vcpu *vcpu, u64 new_eptp)
Sean Christopherson55d23752018-12-03 13:53:18 -08002605{
2606 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08002607
2608 /* Check for memory type validity */
Sean Christophersonac6389a2020-03-02 18:02:38 -08002609 switch (new_eptp & VMX_EPTP_MT_MASK) {
Sean Christopherson55d23752018-12-03 13:53:18 -08002610 case VMX_EPTP_MT_UC:
Sean Christopherson5497b952019-07-11 08:58:29 -07002611 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002612 return false;
2613 break;
2614 case VMX_EPTP_MT_WB:
Sean Christopherson5497b952019-07-11 08:58:29 -07002615 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002616 return false;
2617 break;
2618 default:
2619 return false;
2620 }
2621
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08002622 /* Page-walk levels validity. */
Sean Christophersonac6389a2020-03-02 18:02:38 -08002623 switch (new_eptp & VMX_EPTP_PWL_MASK) {
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08002624 case VMX_EPTP_PWL_5:
2625 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_PAGE_WALK_5_BIT)))
2626 return false;
2627 break;
2628 case VMX_EPTP_PWL_4:
2629 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_PAGE_WALK_4_BIT)))
2630 return false;
2631 break;
2632 default:
Sean Christopherson55d23752018-12-03 13:53:18 -08002633 return false;
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08002634 }
Sean Christopherson55d23752018-12-03 13:53:18 -08002635
2636 /* Reserved bits should not be set */
Sean Christopherson636e8b72021-02-03 16:01:10 -08002637 if (CC(kvm_vcpu_is_illegal_gpa(vcpu, new_eptp) || ((new_eptp >> 7) & 0x1f)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002638 return false;
2639
2640 /* AD, if set, should be supported */
Sean Christophersonac6389a2020-03-02 18:02:38 -08002641 if (new_eptp & VMX_EPTP_AD_ENABLE_BIT) {
Sean Christopherson5497b952019-07-11 08:58:29 -07002642 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002643 return false;
2644 }
2645
2646 return true;
2647}
2648
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002649/*
2650 * Checks related to VM-Execution Control Fields
2651 */
2652static int nested_check_vm_execution_controls(struct kvm_vcpu *vcpu,
2653 struct vmcs12 *vmcs12)
2654{
2655 struct vcpu_vmx *vmx = to_vmx(vcpu);
2656
Sean Christopherson5497b952019-07-11 08:58:29 -07002657 if (CC(!vmx_control_verify(vmcs12->pin_based_vm_exec_control,
2658 vmx->nested.msrs.pinbased_ctls_low,
2659 vmx->nested.msrs.pinbased_ctls_high)) ||
2660 CC(!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
2661 vmx->nested.msrs.procbased_ctls_low,
2662 vmx->nested.msrs.procbased_ctls_high)))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002663 return -EINVAL;
2664
2665 if (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
Sean Christopherson5497b952019-07-11 08:58:29 -07002666 CC(!vmx_control_verify(vmcs12->secondary_vm_exec_control,
2667 vmx->nested.msrs.secondary_ctls_low,
2668 vmx->nested.msrs.secondary_ctls_high)))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002669 return -EINVAL;
2670
Sean Christopherson5497b952019-07-11 08:58:29 -07002671 if (CC(vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu)) ||
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002672 nested_vmx_check_io_bitmap_controls(vcpu, vmcs12) ||
2673 nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12) ||
2674 nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12) ||
2675 nested_vmx_check_apic_access_controls(vcpu, vmcs12) ||
2676 nested_vmx_check_apicv_controls(vcpu, vmcs12) ||
2677 nested_vmx_check_nmi_controls(vmcs12) ||
2678 nested_vmx_check_pml_controls(vcpu, vmcs12) ||
2679 nested_vmx_check_unrestricted_guest_controls(vcpu, vmcs12) ||
2680 nested_vmx_check_mode_based_ept_exec_controls(vcpu, vmcs12) ||
2681 nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12) ||
Sean Christopherson5497b952019-07-11 08:58:29 -07002682 CC(nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002683 return -EINVAL;
2684
Sean Christophersonbc441212019-02-12 16:42:23 -08002685 if (!nested_cpu_has_preemption_timer(vmcs12) &&
2686 nested_cpu_has_save_preemption_timer(vmcs12))
2687 return -EINVAL;
2688
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002689 if (nested_cpu_has_ept(vmcs12) &&
Sean Christophersonac6389a2020-03-02 18:02:38 -08002690 CC(!nested_vmx_check_eptp(vcpu, vmcs12->ept_pointer)))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002691 return -EINVAL;
2692
2693 if (nested_cpu_has_vmfunc(vmcs12)) {
Sean Christopherson5497b952019-07-11 08:58:29 -07002694 if (CC(vmcs12->vm_function_control &
2695 ~vmx->nested.msrs.vmfunc_controls))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002696 return -EINVAL;
2697
2698 if (nested_cpu_has_eptp_switching(vmcs12)) {
Sean Christopherson5497b952019-07-11 08:58:29 -07002699 if (CC(!nested_cpu_has_ept(vmcs12)) ||
2700 CC(!page_address_valid(vcpu, vmcs12->eptp_list_address)))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002701 return -EINVAL;
2702 }
2703 }
2704
2705 return 0;
2706}
2707
Krish Sadhukhan61446ba2018-12-12 13:30:09 -05002708/*
2709 * Checks related to VM-Exit Control Fields
2710 */
2711static int nested_check_vm_exit_controls(struct kvm_vcpu *vcpu,
2712 struct vmcs12 *vmcs12)
2713{
2714 struct vcpu_vmx *vmx = to_vmx(vcpu);
2715
Sean Christopherson5497b952019-07-11 08:58:29 -07002716 if (CC(!vmx_control_verify(vmcs12->vm_exit_controls,
2717 vmx->nested.msrs.exit_ctls_low,
2718 vmx->nested.msrs.exit_ctls_high)) ||
2719 CC(nested_vmx_check_exit_msr_switch_controls(vcpu, vmcs12)))
Krish Sadhukhan61446ba2018-12-12 13:30:09 -05002720 return -EINVAL;
2721
2722 return 0;
2723}
2724
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002725/*
2726 * Checks related to VM-Entry Control Fields
2727 */
2728static int nested_check_vm_entry_controls(struct kvm_vcpu *vcpu,
2729 struct vmcs12 *vmcs12)
Sean Christopherson55d23752018-12-03 13:53:18 -08002730{
2731 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08002732
Sean Christopherson5497b952019-07-11 08:58:29 -07002733 if (CC(!vmx_control_verify(vmcs12->vm_entry_controls,
2734 vmx->nested.msrs.entry_ctls_low,
2735 vmx->nested.msrs.entry_ctls_high)))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002736 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002737
2738 /*
2739 * From the Intel SDM, volume 3:
2740 * Fields relevant to VM-entry event injection must be set properly.
2741 * These fields are the VM-entry interruption-information field, the
2742 * VM-entry exception error code, and the VM-entry instruction length.
2743 */
2744 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
2745 u32 intr_info = vmcs12->vm_entry_intr_info_field;
2746 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
2747 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
2748 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
2749 bool should_have_error_code;
2750 bool urg = nested_cpu_has2(vmcs12,
2751 SECONDARY_EXEC_UNRESTRICTED_GUEST);
2752 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
2753
2754 /* VM-entry interruption-info field: interruption type */
Sean Christopherson5497b952019-07-11 08:58:29 -07002755 if (CC(intr_type == INTR_TYPE_RESERVED) ||
2756 CC(intr_type == INTR_TYPE_OTHER_EVENT &&
2757 !nested_cpu_supports_monitor_trap_flag(vcpu)))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002758 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002759
2760 /* VM-entry interruption-info field: vector */
Sean Christopherson5497b952019-07-11 08:58:29 -07002761 if (CC(intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
2762 CC(intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
2763 CC(intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002764 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002765
2766 /* VM-entry interruption-info field: deliver error code */
2767 should_have_error_code =
2768 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
2769 x86_exception_has_error_code(vector);
Sean Christopherson5497b952019-07-11 08:58:29 -07002770 if (CC(has_error_code != should_have_error_code))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002771 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002772
2773 /* VM-entry exception error code */
Sean Christopherson5497b952019-07-11 08:58:29 -07002774 if (CC(has_error_code &&
Sean Christopherson567926c2019-10-01 09:21:23 -07002775 vmcs12->vm_entry_exception_error_code & GENMASK(31, 16)))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002776 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002777
2778 /* VM-entry interruption-info field: reserved bits */
Sean Christopherson5497b952019-07-11 08:58:29 -07002779 if (CC(intr_info & INTR_INFO_RESVD_BITS_MASK))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002780 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002781
2782 /* VM-entry instruction length */
2783 switch (intr_type) {
2784 case INTR_TYPE_SOFT_EXCEPTION:
2785 case INTR_TYPE_SOFT_INTR:
2786 case INTR_TYPE_PRIV_SW_EXCEPTION:
Sean Christopherson5497b952019-07-11 08:58:29 -07002787 if (CC(vmcs12->vm_entry_instruction_len > 15) ||
2788 CC(vmcs12->vm_entry_instruction_len == 0 &&
2789 CC(!nested_cpu_has_zero_length_injection(vcpu))))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002790 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002791 }
2792 }
2793
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002794 if (nested_vmx_check_entry_msr_switch_controls(vcpu, vmcs12))
2795 return -EINVAL;
2796
2797 return 0;
2798}
2799
Sean Christopherson5478ba32019-04-11 12:18:06 -07002800static int nested_vmx_check_controls(struct kvm_vcpu *vcpu,
2801 struct vmcs12 *vmcs12)
2802{
2803 if (nested_check_vm_execution_controls(vcpu, vmcs12) ||
2804 nested_check_vm_exit_controls(vcpu, vmcs12) ||
2805 nested_check_vm_entry_controls(vcpu, vmcs12))
Paolo Bonzini98d9e852019-04-12 10:19:57 +02002806 return -EINVAL;
Sean Christopherson5478ba32019-04-11 12:18:06 -07002807
Vitaly Kuznetsova8350232020-02-05 13:30:34 +01002808 if (to_vmx(vcpu)->nested.enlightened_vmcs_enabled)
2809 return nested_evmcs_check_controls(vmcs12);
2810
Sean Christopherson5478ba32019-04-11 12:18:06 -07002811 return 0;
2812}
2813
Paolo Bonzini98d9e852019-04-12 10:19:57 +02002814static int nested_vmx_check_host_state(struct kvm_vcpu *vcpu,
2815 struct vmcs12 *vmcs12)
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002816{
2817 bool ia32e;
2818
Sean Christopherson5497b952019-07-11 08:58:29 -07002819 if (CC(!nested_host_cr0_valid(vcpu, vmcs12->host_cr0)) ||
2820 CC(!nested_host_cr4_valid(vcpu, vmcs12->host_cr4)) ||
Sean Christopherson636e8b72021-02-03 16:01:10 -08002821 CC(kvm_vcpu_is_illegal_gpa(vcpu, vmcs12->host_cr3)))
Krish Sadhukhan254b2f32018-12-12 13:30:11 -05002822 return -EINVAL;
Krish Sadhukhan711eff32019-02-07 14:05:30 -05002823
Sean Christopherson5497b952019-07-11 08:58:29 -07002824 if (CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_esp, vcpu)) ||
2825 CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_eip, vcpu)))
Krish Sadhukhan711eff32019-02-07 14:05:30 -05002826 return -EINVAL;
2827
Krish Sadhukhanf6b0db1f2019-04-08 17:35:11 -04002828 if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) &&
Sean Christopherson5497b952019-07-11 08:58:29 -07002829 CC(!kvm_pat_valid(vmcs12->host_ia32_pat)))
Krish Sadhukhanf6b0db1f2019-04-08 17:35:11 -04002830 return -EINVAL;
2831
Oliver Uptonc547cb62019-11-13 16:17:17 -08002832 if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) &&
2833 CC(!kvm_valid_perf_global_ctrl(vcpu_to_pmu(vcpu),
2834 vmcs12->host_ia32_perf_global_ctrl)))
2835 return -EINVAL;
2836
Paolo Bonzinifd3edd42019-09-25 18:33:53 +02002837#ifdef CONFIG_X86_64
2838 ia32e = !!(vcpu->arch.efer & EFER_LMA);
2839#else
2840 ia32e = false;
2841#endif
2842
2843 if (ia32e) {
2844 if (CC(!(vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)) ||
2845 CC(!(vmcs12->host_cr4 & X86_CR4_PAE)))
2846 return -EINVAL;
2847 } else {
2848 if (CC(vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) ||
2849 CC(vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) ||
2850 CC(vmcs12->host_cr4 & X86_CR4_PCIDE) ||
2851 CC((vmcs12->host_rip) >> 32))
2852 return -EINVAL;
2853 }
Krish Sadhukhan1ef23e12019-07-03 19:54:35 -04002854
Sean Christopherson5497b952019-07-11 08:58:29 -07002855 if (CC(vmcs12->host_cs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2856 CC(vmcs12->host_ss_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2857 CC(vmcs12->host_ds_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2858 CC(vmcs12->host_es_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2859 CC(vmcs12->host_fs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2860 CC(vmcs12->host_gs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2861 CC(vmcs12->host_tr_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2862 CC(vmcs12->host_cs_selector == 0) ||
2863 CC(vmcs12->host_tr_selector == 0) ||
2864 CC(vmcs12->host_ss_selector == 0 && !ia32e))
Krish Sadhukhan1ef23e12019-07-03 19:54:35 -04002865 return -EINVAL;
2866
Sean Christopherson5497b952019-07-11 08:58:29 -07002867 if (CC(is_noncanonical_address(vmcs12->host_fs_base, vcpu)) ||
2868 CC(is_noncanonical_address(vmcs12->host_gs_base, vcpu)) ||
2869 CC(is_noncanonical_address(vmcs12->host_gdtr_base, vcpu)) ||
2870 CC(is_noncanonical_address(vmcs12->host_idtr_base, vcpu)) ||
Paolo Bonzinifd3edd42019-09-25 18:33:53 +02002871 CC(is_noncanonical_address(vmcs12->host_tr_base, vcpu)) ||
2872 CC(is_noncanonical_address(vmcs12->host_rip, vcpu)))
Krish Sadhukhan58450382019-08-09 12:26:19 -07002873 return -EINVAL;
Krish Sadhukhan1ef23e12019-07-03 19:54:35 -04002874
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002875 /*
2876 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
2877 * IA32_EFER MSR must be 0 in the field for that register. In addition,
2878 * the values of the LMA and LME bits in the field must each be that of
2879 * the host address-space size VM-exit control.
2880 */
2881 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
Sean Christopherson5497b952019-07-11 08:58:29 -07002882 if (CC(!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer)) ||
2883 CC(ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA)) ||
2884 CC(ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)))
Krish Sadhukhan254b2f32018-12-12 13:30:11 -05002885 return -EINVAL;
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002886 }
2887
Sean Christopherson55d23752018-12-03 13:53:18 -08002888 return 0;
2889}
2890
2891static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
2892 struct vmcs12 *vmcs12)
2893{
KarimAllah Ahmed88925302019-01-31 21:24:41 +01002894 int r = 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08002895 struct vmcs12 *shadow;
KarimAllah Ahmed88925302019-01-31 21:24:41 +01002896 struct kvm_host_map map;
Sean Christopherson55d23752018-12-03 13:53:18 -08002897
2898 if (vmcs12->vmcs_link_pointer == -1ull)
2899 return 0;
2900
Sean Christopherson5497b952019-07-11 08:58:29 -07002901 if (CC(!page_address_valid(vcpu, vmcs12->vmcs_link_pointer)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002902 return -EINVAL;
2903
Sean Christopherson5497b952019-07-11 08:58:29 -07002904 if (CC(kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->vmcs_link_pointer), &map)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002905 return -EINVAL;
2906
KarimAllah Ahmed88925302019-01-31 21:24:41 +01002907 shadow = map.hva;
2908
Sean Christopherson5497b952019-07-11 08:58:29 -07002909 if (CC(shadow->hdr.revision_id != VMCS12_REVISION) ||
2910 CC(shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002911 r = -EINVAL;
KarimAllah Ahmed88925302019-01-31 21:24:41 +01002912
2913 kvm_vcpu_unmap(vcpu, &map, false);
Sean Christopherson55d23752018-12-03 13:53:18 -08002914 return r;
2915}
2916
Sean Christopherson55d23752018-12-03 13:53:18 -08002917/*
2918 * Checks related to Guest Non-register State
2919 */
2920static int nested_check_guest_non_reg_state(struct vmcs12 *vmcs12)
2921{
Sean Christopherson5497b952019-07-11 08:58:29 -07002922 if (CC(vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
Yadong Qibf0cd882020-11-06 14:51:22 +08002923 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT &&
2924 vmcs12->guest_activity_state != GUEST_ACTIVITY_WAIT_SIPI))
Sean Christopherson55d23752018-12-03 13:53:18 -08002925 return -EINVAL;
2926
2927 return 0;
2928}
2929
Sean Christopherson5478ba32019-04-11 12:18:06 -07002930static int nested_vmx_check_guest_state(struct kvm_vcpu *vcpu,
2931 struct vmcs12 *vmcs12,
Sean Christopherson68cda402020-05-11 15:05:29 -07002932 enum vm_entry_failure_code *entry_failure_code)
Sean Christopherson55d23752018-12-03 13:53:18 -08002933{
2934 bool ia32e;
2935
Sean Christopherson68cda402020-05-11 15:05:29 -07002936 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson55d23752018-12-03 13:53:18 -08002937
Sean Christopherson5497b952019-07-11 08:58:29 -07002938 if (CC(!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0)) ||
2939 CC(!nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4)))
Sean Christophersonc80add02019-04-11 12:18:09 -07002940 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002941
Krish Sadhukhanb91991b2020-01-15 19:54:32 -05002942 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) &&
2943 CC(!kvm_dr7_valid(vmcs12->guest_dr7)))
2944 return -EINVAL;
2945
Krish Sadhukhande2bc2b2019-04-08 17:35:12 -04002946 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) &&
Sean Christopherson5497b952019-07-11 08:58:29 -07002947 CC(!kvm_pat_valid(vmcs12->guest_ia32_pat)))
Sean Christophersonc80add02019-04-11 12:18:09 -07002948 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002949
2950 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
Sean Christopherson68cda402020-05-11 15:05:29 -07002951 *entry_failure_code = ENTRY_FAIL_VMCS_LINK_PTR;
Sean Christophersonc80add02019-04-11 12:18:09 -07002952 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002953 }
2954
Oliver Uptonbfc6ad62019-11-13 16:17:16 -08002955 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) &&
2956 CC(!kvm_valid_perf_global_ctrl(vcpu_to_pmu(vcpu),
2957 vmcs12->guest_ia32_perf_global_ctrl)))
2958 return -EINVAL;
2959
Sean Christopherson55d23752018-12-03 13:53:18 -08002960 /*
2961 * If the load IA32_EFER VM-entry control is 1, the following checks
2962 * are performed on the field for the IA32_EFER MSR:
2963 * - Bits reserved in the IA32_EFER MSR must be 0.
2964 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
2965 * the IA-32e mode guest VM-exit control. It must also be identical
2966 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
2967 * CR0.PG) is 1.
2968 */
2969 if (to_vmx(vcpu)->nested.nested_run_pending &&
2970 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
2971 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
Sean Christopherson5497b952019-07-11 08:58:29 -07002972 if (CC(!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer)) ||
2973 CC(ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA)) ||
2974 CC(((vmcs12->guest_cr0 & X86_CR0_PG) &&
2975 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))))
Sean Christophersonc80add02019-04-11 12:18:09 -07002976 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002977 }
2978
2979 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
Sean Christopherson5497b952019-07-11 08:58:29 -07002980 (CC(is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu)) ||
2981 CC((vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD))))
Sean Christophersonc80add02019-04-11 12:18:09 -07002982 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002983
Sean Christopherson9c3e9222019-04-11 12:18:05 -07002984 if (nested_check_guest_non_reg_state(vmcs12))
Sean Christophersonc80add02019-04-11 12:18:09 -07002985 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002986
2987 return 0;
2988}
2989
Sean Christopherson453eafb2018-12-20 12:25:17 -08002990static int nested_vmx_check_vmentry_hw(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08002991{
2992 struct vcpu_vmx *vmx = to_vmx(vcpu);
2993 unsigned long cr3, cr4;
Sean Christophersonf1727b42019-01-25 07:40:58 -08002994 bool vm_fail;
Sean Christopherson55d23752018-12-03 13:53:18 -08002995
2996 if (!nested_early_check)
2997 return 0;
2998
2999 if (vmx->msr_autoload.host.nr)
3000 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
3001 if (vmx->msr_autoload.guest.nr)
3002 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
3003
3004 preempt_disable();
3005
3006 vmx_prepare_switch_to_guest(vcpu);
3007
3008 /*
3009 * Induce a consistency check VMExit by clearing bit 1 in GUEST_RFLAGS,
3010 * which is reserved to '1' by hardware. GUEST_RFLAGS is guaranteed to
Miaohe Lin49f933d2020-02-27 11:20:54 +08003011 * be written (by prepare_vmcs02()) before the "real" VMEnter, i.e.
Sean Christopherson55d23752018-12-03 13:53:18 -08003012 * there is no need to preserve other bits or save/restore the field.
3013 */
3014 vmcs_writel(GUEST_RFLAGS, 0);
3015
Sean Christopherson55d23752018-12-03 13:53:18 -08003016 cr3 = __get_current_cr3_fast();
3017 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
3018 vmcs_writel(HOST_CR3, cr3);
3019 vmx->loaded_vmcs->host_state.cr3 = cr3;
3020 }
3021
3022 cr4 = cr4_read_shadow();
3023 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
3024 vmcs_writel(HOST_CR4, cr4);
3025 vmx->loaded_vmcs->host_state.cr4 = cr4;
3026 }
3027
Uros Bizjak150f17b2020-12-30 16:26:57 -08003028 vm_fail = __vmx_vcpu_run(vmx, (unsigned long *)&vcpu->arch.regs,
3029 vmx->loaded_vmcs->launched);
Sean Christopherson55d23752018-12-03 13:53:18 -08003030
Sean Christopherson55d23752018-12-03 13:53:18 -08003031 if (vmx->msr_autoload.host.nr)
3032 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
3033 if (vmx->msr_autoload.guest.nr)
3034 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
3035
Sean Christophersonf1727b42019-01-25 07:40:58 -08003036 if (vm_fail) {
Sean Christopherson380e0052019-07-11 08:58:30 -07003037 u32 error = vmcs_read32(VM_INSTRUCTION_ERROR);
3038
Wanpeng Li541e8862019-05-17 16:49:50 +08003039 preempt_enable();
Sean Christopherson380e0052019-07-11 08:58:30 -07003040
3041 trace_kvm_nested_vmenter_failed(
3042 "early hardware check VM-instruction error: ", error);
3043 WARN_ON_ONCE(error != VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Sean Christopherson55d23752018-12-03 13:53:18 -08003044 return 1;
3045 }
3046
3047 /*
3048 * VMExit clears RFLAGS.IF and DR7, even on a consistency check.
3049 */
Sean Christopherson55d23752018-12-03 13:53:18 -08003050 if (hw_breakpoint_active())
3051 set_debugreg(__this_cpu_read(cpu_dr7), 7);
Peter Zijlstra84b6a342020-05-29 23:27:36 +02003052 local_irq_enable();
Wanpeng Li541e8862019-05-17 16:49:50 +08003053 preempt_enable();
Sean Christopherson55d23752018-12-03 13:53:18 -08003054
3055 /*
3056 * A non-failing VMEntry means we somehow entered guest mode with
3057 * an illegal RIP, and that's just the tip of the iceberg. There
3058 * is no telling what memory has been modified or what state has
3059 * been exposed to unknown code. Hitting this all but guarantees
3060 * a (very critical) hardware issue.
3061 */
3062 WARN_ON(!(vmcs_read32(VM_EXIT_REASON) &
3063 VMX_EXIT_REASONS_FAILED_VMENTRY));
3064
3065 return 0;
3066}
Sean Christopherson55d23752018-12-03 13:53:18 -08003067
Paolo Bonzini9a78e152021-01-08 11:43:08 -05003068static bool nested_get_evmcs_page(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08003069{
Sean Christopherson55d23752018-12-03 13:53:18 -08003070 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08003071
Vitaly Kuznetsove942dbf2020-03-09 16:52:12 +01003072 /*
3073 * hv_evmcs may end up being not mapped after migration (when
3074 * L2 was running), map it here to make sure vmcs12 changes are
3075 * properly reflected.
3076 */
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02003077 if (vmx->nested.enlightened_vmcs_enabled &&
Vitaly Kuznetsov27849962021-05-26 15:20:20 +02003078 vmx->nested.hv_evmcs_vmptr == EVMPTR_MAP_PENDING) {
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003079 enum nested_evmptrld_status evmptrld_status =
3080 nested_vmx_handle_enlightened_vmptrld(vcpu, false);
3081
3082 if (evmptrld_status == EVMPTRLD_VMFAIL ||
Vitaly Kuznetsovf5c7e842021-05-03 17:08:51 +02003083 evmptrld_status == EVMPTRLD_ERROR)
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003084 return false;
Vitaly Kuznetsov8629b622021-05-26 15:20:25 +02003085
3086 /*
3087 * Post migration VMCS12 always provides the most actual
3088 * information, copy it to eVMCS upon entry.
3089 */
3090 vmx->nested.need_vmcs12_to_shadow_sync = true;
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003091 }
Vitaly Kuznetsove942dbf2020-03-09 16:52:12 +01003092
Paolo Bonzini9a78e152021-01-08 11:43:08 -05003093 return true;
3094}
3095
3096static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
3097{
3098 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3099 struct vcpu_vmx *vmx = to_vmx(vcpu);
3100 struct kvm_host_map *map;
3101 struct page *page;
3102 u64 hpa;
3103
Maxim Levitsky158a48e2021-06-07 12:02:03 +03003104 if (!vcpu->arch.pdptrs_from_userspace &&
3105 !nested_cpu_has_ept(vmcs12) && is_pae_paging(vcpu)) {
Maxim Levitsky0f857222021-06-07 12:02:00 +03003106 /*
3107 * Reload the guest's PDPTRs since after a migration
3108 * the guest CR3 might be restored prior to setting the nested
3109 * state which can lead to a load of wrong PDPTRs.
3110 */
3111 if (CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, vcpu->arch.cr3)))
3112 return false;
3113 }
3114
3115
Sean Christopherson55d23752018-12-03 13:53:18 -08003116 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
3117 /*
3118 * Translate L1 physical address to host physical
3119 * address for vmcs02. Keep the page pinned, so this
3120 * physical address remains valid. We keep a reference
3121 * to it so we can release it later.
3122 */
3123 if (vmx->nested.apic_access_page) { /* shouldn't happen */
Liran Alonb11494b2019-11-21 00:31:47 +02003124 kvm_release_page_clean(vmx->nested.apic_access_page);
Sean Christopherson55d23752018-12-03 13:53:18 -08003125 vmx->nested.apic_access_page = NULL;
3126 }
3127 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Sean Christopherson55d23752018-12-03 13:53:18 -08003128 if (!is_error_page(page)) {
3129 vmx->nested.apic_access_page = page;
3130 hpa = page_to_phys(vmx->nested.apic_access_page);
3131 vmcs_write64(APIC_ACCESS_ADDR, hpa);
3132 } else {
Jim Mattson671ddc72019-10-15 10:44:05 -07003133 pr_debug_ratelimited("%s: no backing 'struct page' for APIC-access address in vmcs12\n",
3134 __func__);
3135 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3136 vcpu->run->internal.suberror =
3137 KVM_INTERNAL_ERROR_EMULATION;
3138 vcpu->run->internal.ndata = 0;
3139 return false;
Sean Christopherson55d23752018-12-03 13:53:18 -08003140 }
3141 }
3142
3143 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003144 map = &vmx->nested.virtual_apic_map;
Sean Christopherson55d23752018-12-03 13:53:18 -08003145
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003146 if (!kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->virtual_apic_page_addr), map)) {
3147 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, pfn_to_hpa(map->pfn));
Paolo Bonzini69090812019-04-15 15:16:17 +02003148 } else if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING) &&
3149 nested_cpu_has(vmcs12, CPU_BASED_CR8_STORE_EXITING) &&
3150 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
3151 /*
3152 * The processor will never use the TPR shadow, simply
3153 * clear the bit from the execution control. Such a
3154 * configuration is useless, but it happens in tests.
3155 * For any other configuration, failing the vm entry is
3156 * _not_ what the processor does but it's basically the
3157 * only possibility we have.
3158 */
Sean Christopherson2183f562019-05-07 12:17:56 -07003159 exec_controls_clearbit(vmx, CPU_BASED_TPR_SHADOW);
Paolo Bonzini69090812019-04-15 15:16:17 +02003160 } else {
Sean Christophersonca2f5462019-05-07 09:06:33 -07003161 /*
3162 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR to
3163 * force VM-Entry to fail.
3164 */
3165 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Sean Christopherson55d23752018-12-03 13:53:18 -08003166 }
3167 }
3168
3169 if (nested_cpu_has_posted_intr(vmcs12)) {
KarimAllah Ahmed3278e042019-01-31 21:24:38 +01003170 map = &vmx->nested.pi_desc_map;
3171
3172 if (!kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->posted_intr_desc_addr), map)) {
3173 vmx->nested.pi_desc =
3174 (struct pi_desc *)(((void *)map->hva) +
3175 offset_in_page(vmcs12->posted_intr_desc_addr));
3176 vmcs_write64(POSTED_INTR_DESC_ADDR,
3177 pfn_to_hpa(map->pfn) + offset_in_page(vmcs12->posted_intr_desc_addr));
Jim Mattson966eefb2021-06-04 10:26:06 -07003178 } else {
3179 /*
3180 * Defer the KVM_INTERNAL_EXIT until KVM tries to
3181 * access the contents of the VMCS12 posted interrupt
3182 * descriptor. (Note that KVM may do this when it
3183 * should not, per the architectural specification.)
3184 */
3185 vmx->nested.pi_desc = NULL;
3186 pin_controls_clearbit(vmx, PIN_BASED_POSTED_INTR);
Sean Christopherson55d23752018-12-03 13:53:18 -08003187 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003188 }
3189 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
Sean Christopherson2183f562019-05-07 12:17:56 -07003190 exec_controls_setbit(vmx, CPU_BASED_USE_MSR_BITMAPS);
Sean Christopherson55d23752018-12-03 13:53:18 -08003191 else
Sean Christopherson2183f562019-05-07 12:17:56 -07003192 exec_controls_clearbit(vmx, CPU_BASED_USE_MSR_BITMAPS);
Paolo Bonzini9a78e152021-01-08 11:43:08 -05003193
3194 return true;
3195}
3196
3197static bool vmx_get_nested_state_pages(struct kvm_vcpu *vcpu)
3198{
Vitaly Kuznetsovf5c7e842021-05-03 17:08:51 +02003199 if (!nested_get_evmcs_page(vcpu)) {
3200 pr_debug_ratelimited("%s: enlightened vmptrld failed\n",
3201 __func__);
3202 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3203 vcpu->run->internal.suberror =
3204 KVM_INTERNAL_ERROR_EMULATION;
3205 vcpu->run->internal.ndata = 0;
3206
Paolo Bonzini9a78e152021-01-08 11:43:08 -05003207 return false;
Vitaly Kuznetsovf5c7e842021-05-03 17:08:51 +02003208 }
Paolo Bonzini9a78e152021-01-08 11:43:08 -05003209
3210 if (is_guest_mode(vcpu) && !nested_get_vmcs12_pages(vcpu))
3211 return false;
3212
Jim Mattson671ddc72019-10-15 10:44:05 -07003213 return true;
Sean Christopherson55d23752018-12-03 13:53:18 -08003214}
3215
Sean Christopherson02f5fb22020-06-22 14:58:32 -07003216static int nested_vmx_write_pml_buffer(struct kvm_vcpu *vcpu, gpa_t gpa)
3217{
3218 struct vmcs12 *vmcs12;
3219 struct vcpu_vmx *vmx = to_vmx(vcpu);
3220 gpa_t dst;
3221
3222 if (WARN_ON_ONCE(!is_guest_mode(vcpu)))
3223 return 0;
3224
3225 if (WARN_ON_ONCE(vmx->nested.pml_full))
3226 return 1;
3227
3228 /*
3229 * Check if PML is enabled for the nested guest. Whether eptp bit 6 is
3230 * set is already checked as part of A/D emulation.
3231 */
3232 vmcs12 = get_vmcs12(vcpu);
3233 if (!nested_cpu_has_pml(vmcs12))
3234 return 0;
3235
3236 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
3237 vmx->nested.pml_full = true;
3238 return 1;
3239 }
3240
3241 gpa &= ~0xFFFull;
3242 dst = vmcs12->pml_address + sizeof(u64) * vmcs12->guest_pml_index;
3243
3244 if (kvm_write_guest_page(vcpu->kvm, gpa_to_gfn(dst), &gpa,
3245 offset_in_page(dst), sizeof(gpa)))
3246 return 0;
3247
3248 vmcs12->guest_pml_index--;
3249
3250 return 0;
3251}
3252
Sean Christopherson55d23752018-12-03 13:53:18 -08003253/*
3254 * Intel's VMX Instruction Reference specifies a common set of prerequisites
3255 * for running VMX instructions (except VMXON, whose prerequisites are
3256 * slightly different). It also specifies what exception to inject otherwise.
3257 * Note that many of these exceptions have priority over VM exits, so they
3258 * don't have to be checked again here.
3259 */
3260static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
3261{
3262 if (!to_vmx(vcpu)->nested.vmxon) {
3263 kvm_queue_exception(vcpu, UD_VECTOR);
3264 return 0;
3265 }
3266
3267 if (vmx_get_cpl(vcpu)) {
3268 kvm_inject_gp(vcpu, 0);
3269 return 0;
3270 }
3271
3272 return 1;
3273}
3274
3275static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu)
3276{
3277 u8 rvi = vmx_get_rvi();
3278 u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI);
3279
3280 return ((rvi & 0xf0) > (vppr & 0xf0));
3281}
3282
3283static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
3284 struct vmcs12 *vmcs12);
3285
3286/*
3287 * If from_vmentry is false, this is being called from state restore (either RSM
3288 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
Jim Mattson671ddc72019-10-15 10:44:05 -07003289 *
3290 * Returns:
Miaohe Lin463bfee2020-02-14 10:44:05 +08003291 * NVMX_VMENTRY_SUCCESS: Entered VMX non-root mode
3292 * NVMX_VMENTRY_VMFAIL: Consistency check VMFail
3293 * NVMX_VMENTRY_VMEXIT: Consistency check VMExit
3294 * NVMX_VMENTRY_KVM_INTERNAL_ERROR: KVM internal error
Sean Christopherson55d23752018-12-03 13:53:18 -08003295 */
Jim Mattson671ddc72019-10-15 10:44:05 -07003296enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu,
3297 bool from_vmentry)
Sean Christopherson55d23752018-12-03 13:53:18 -08003298{
3299 struct vcpu_vmx *vmx = to_vmx(vcpu);
3300 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Sean Christopherson68cda402020-05-11 15:05:29 -07003301 enum vm_entry_failure_code entry_failure_code;
Sean Christopherson55d23752018-12-03 13:53:18 -08003302 bool evaluate_pending_interrupts;
Sean Christopherson8e533242020-11-06 17:03:12 +08003303 union vmx_exit_reason exit_reason = {
3304 .basic = EXIT_REASON_INVALID_STATE,
3305 .failed_vmentry = 1,
3306 };
3307 u32 failed_index;
Sean Christopherson55d23752018-12-03 13:53:18 -08003308
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07003309 if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu))
3310 kvm_vcpu_flush_tlb_current(vcpu);
3311
Sean Christopherson2183f562019-05-07 12:17:56 -07003312 evaluate_pending_interrupts = exec_controls_get(vmx) &
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08003313 (CPU_BASED_INTR_WINDOW_EXITING | CPU_BASED_NMI_WINDOW_EXITING);
Sean Christopherson55d23752018-12-03 13:53:18 -08003314 if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu))
3315 evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu);
3316
3317 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
3318 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
3319 if (kvm_mpx_supported() &&
3320 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
3321 vmx->nested.vmcs01_guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
3322
Sean Christophersonf087a022019-06-07 11:55:34 -07003323 /*
3324 * Overwrite vmcs01.GUEST_CR3 with L1's CR3 if EPT is disabled *and*
3325 * nested early checks are disabled. In the event of a "late" VM-Fail,
3326 * i.e. a VM-Fail detected by hardware but not KVM, KVM must unwind its
3327 * software model to the pre-VMEntry host state. When EPT is disabled,
3328 * GUEST_CR3 holds KVM's shadow CR3, not L1's "real" CR3, which causes
3329 * nested_vmx_restore_host_state() to corrupt vcpu->arch.cr3. Stuffing
3330 * vmcs01.GUEST_CR3 results in the unwind naturally setting arch.cr3 to
3331 * the correct value. Smashing vmcs01.GUEST_CR3 is safe because nested
3332 * VM-Exits, and the unwind, reset KVM's MMU, i.e. vmcs01.GUEST_CR3 is
3333 * guaranteed to be overwritten with a shadow CR3 prior to re-entering
3334 * L1. Don't stuff vmcs01.GUEST_CR3 when using nested early checks as
3335 * KVM modifies vcpu->arch.cr3 if and only if the early hardware checks
3336 * pass, and early VM-Fails do not reset KVM's MMU, i.e. the VM-Fail
3337 * path would need to manually save/restore vmcs01.GUEST_CR3.
3338 */
3339 if (!enable_ept && !nested_early_check)
3340 vmcs_writel(GUEST_CR3, vcpu->arch.cr3);
3341
Sean Christopherson55d23752018-12-03 13:53:18 -08003342 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
3343
3344 prepare_vmcs02_early(vmx, vmcs12);
3345
3346 if (from_vmentry) {
Sean Christophersonb89d5ad2020-09-23 11:44:47 -07003347 if (unlikely(!nested_get_vmcs12_pages(vcpu))) {
3348 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson671ddc72019-10-15 10:44:05 -07003349 return NVMX_VMENTRY_KVM_INTERNAL_ERROR;
Sean Christophersonb89d5ad2020-09-23 11:44:47 -07003350 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003351
3352 if (nested_vmx_check_vmentry_hw(vcpu)) {
3353 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson671ddc72019-10-15 10:44:05 -07003354 return NVMX_VMENTRY_VMFAIL;
Sean Christopherson55d23752018-12-03 13:53:18 -08003355 }
3356
Sean Christopherson68cda402020-05-11 15:05:29 -07003357 if (nested_vmx_check_guest_state(vcpu, vmcs12,
3358 &entry_failure_code)) {
Sean Christopherson8e533242020-11-06 17:03:12 +08003359 exit_reason.basic = EXIT_REASON_INVALID_STATE;
Sean Christopherson68cda402020-05-11 15:05:29 -07003360 vmcs12->exit_qualification = entry_failure_code;
Sean Christopherson55d23752018-12-03 13:53:18 -08003361 goto vmentry_fail_vmexit;
Sean Christopherson68cda402020-05-11 15:05:29 -07003362 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003363 }
3364
3365 enter_guest_mode(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08003366
Maxim Levitsky0f857222021-06-07 12:02:00 +03003367 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &entry_failure_code)) {
Sean Christopherson8e533242020-11-06 17:03:12 +08003368 exit_reason.basic = EXIT_REASON_INVALID_STATE;
Sean Christopherson68cda402020-05-11 15:05:29 -07003369 vmcs12->exit_qualification = entry_failure_code;
Sean Christopherson55d23752018-12-03 13:53:18 -08003370 goto vmentry_fail_vmexit_guest_mode;
Sean Christopherson68cda402020-05-11 15:05:29 -07003371 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003372
3373 if (from_vmentry) {
Sean Christopherson68cda402020-05-11 15:05:29 -07003374 failed_index = nested_vmx_load_msr(vcpu,
3375 vmcs12->vm_entry_msr_load_addr,
3376 vmcs12->vm_entry_msr_load_count);
3377 if (failed_index) {
Sean Christopherson8e533242020-11-06 17:03:12 +08003378 exit_reason.basic = EXIT_REASON_MSR_LOAD_FAIL;
Sean Christopherson68cda402020-05-11 15:05:29 -07003379 vmcs12->exit_qualification = failed_index;
Sean Christopherson55d23752018-12-03 13:53:18 -08003380 goto vmentry_fail_vmexit_guest_mode;
Sean Christopherson68cda402020-05-11 15:05:29 -07003381 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003382 } else {
3383 /*
3384 * The MMU is not initialized to point at the right entities yet and
3385 * "get pages" would need to read data from the guest (i.e. we will
3386 * need to perform gpa to hpa translation). Request a call
3387 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
3388 * have already been set at vmentry time and should not be reset.
3389 */
Paolo Bonzini729c15c2020-09-22 06:53:57 -04003390 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08003391 }
3392
3393 /*
3394 * If L1 had a pending IRQ/NMI until it executed
3395 * VMLAUNCH/VMRESUME which wasn't delivered because it was
3396 * disallowed (e.g. interrupts disabled), L0 needs to
3397 * evaluate if this pending event should cause an exit from L2
3398 * to L1 or delivered directly to L2 (e.g. In case L1 don't
3399 * intercept EXTERNAL_INTERRUPT).
3400 *
3401 * Usually this would be handled by the processor noticing an
3402 * IRQ/NMI window request, or checking RVI during evaluation of
3403 * pending virtual interrupts. However, this setting was done
3404 * on VMCS01 and now VMCS02 is active instead. Thus, we force L0
3405 * to perform pending event evaluation by requesting a KVM_REQ_EVENT.
3406 */
3407 if (unlikely(evaluate_pending_interrupts))
3408 kvm_make_request(KVM_REQ_EVENT, vcpu);
3409
3410 /*
Paolo Bonzini359a6c32019-01-29 19:14:46 +01003411 * Do not start the preemption timer hrtimer until after we know
3412 * we are successful, so that only nested_vmx_vmexit needs to cancel
3413 * the timer.
3414 */
3415 vmx->nested.preemption_timer_expired = false;
Peter Shier850448f2020-05-26 14:51:06 -07003416 if (nested_cpu_has_preemption_timer(vmcs12)) {
3417 u64 timer_value = vmx_calc_preemption_timer_value(vcpu);
3418 vmx_start_preemption_timer(vcpu, timer_value);
3419 }
Paolo Bonzini359a6c32019-01-29 19:14:46 +01003420
3421 /*
Sean Christopherson55d23752018-12-03 13:53:18 -08003422 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
3423 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
3424 * returned as far as L1 is concerned. It will only return (and set
3425 * the success flag) when L2 exits (see nested_vmx_vmexit()).
3426 */
Jim Mattson671ddc72019-10-15 10:44:05 -07003427 return NVMX_VMENTRY_SUCCESS;
Sean Christopherson55d23752018-12-03 13:53:18 -08003428
3429 /*
3430 * A failed consistency check that leads to a VMExit during L1's
3431 * VMEnter to L2 is a variation of a normal VMexit, as explained in
3432 * 26.7 "VM-entry failures during or after loading guest state".
3433 */
3434vmentry_fail_vmexit_guest_mode:
Xiaoyao Li5e3d3942019-12-06 16:45:26 +08003435 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING)
Sean Christopherson55d23752018-12-03 13:53:18 -08003436 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
3437 leave_guest_mode(vcpu);
3438
3439vmentry_fail_vmexit:
3440 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
3441
3442 if (!from_vmentry)
Jim Mattson671ddc72019-10-15 10:44:05 -07003443 return NVMX_VMENTRY_VMEXIT;
Sean Christopherson55d23752018-12-03 13:53:18 -08003444
3445 load_vmcs12_host_state(vcpu, vmcs12);
Sean Christopherson8e533242020-11-06 17:03:12 +08003446 vmcs12->vm_exit_reason = exit_reason.full;
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02003447 if (enable_shadow_vmcs || evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
Sean Christopherson3731905ef2019-05-07 08:36:27 -07003448 vmx->nested.need_vmcs12_to_shadow_sync = true;
Jim Mattson671ddc72019-10-15 10:44:05 -07003449 return NVMX_VMENTRY_VMEXIT;
Sean Christopherson55d23752018-12-03 13:53:18 -08003450}
3451
3452/*
3453 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
3454 * for running an L2 nested guest.
3455 */
3456static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
3457{
3458 struct vmcs12 *vmcs12;
Jim Mattson671ddc72019-10-15 10:44:05 -07003459 enum nvmx_vmentry_status status;
Sean Christopherson55d23752018-12-03 13:53:18 -08003460 struct vcpu_vmx *vmx = to_vmx(vcpu);
3461 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003462 enum nested_evmptrld_status evmptrld_status;
Sean Christopherson55d23752018-12-03 13:53:18 -08003463
3464 if (!nested_vmx_check_permission(vcpu))
3465 return 1;
3466
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003467 evmptrld_status = nested_vmx_handle_enlightened_vmptrld(vcpu, launch);
3468 if (evmptrld_status == EVMPTRLD_ERROR) {
3469 kvm_queue_exception(vcpu, UD_VECTOR);
Sean Christopherson55d23752018-12-03 13:53:18 -08003470 return 1;
Sean Christophersonfc595f32020-08-12 11:06:15 -07003471 } else if (CC(evmptrld_status == EVMPTRLD_VMFAIL)) {
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003472 return nested_vmx_failInvalid(vcpu);
3473 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003474
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02003475 if (CC(!evmptr_is_valid(vmx->nested.hv_evmcs_vmptr) &&
3476 vmx->nested.current_vmptr == -1ull))
Sean Christopherson55d23752018-12-03 13:53:18 -08003477 return nested_vmx_failInvalid(vcpu);
3478
3479 vmcs12 = get_vmcs12(vcpu);
3480
3481 /*
3482 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
3483 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
3484 * rather than RFLAGS.ZF, and no error number is stored to the
3485 * VM-instruction error field.
3486 */
Sean Christophersonfc595f32020-08-12 11:06:15 -07003487 if (CC(vmcs12->hdr.shadow_vmcs))
Sean Christopherson55d23752018-12-03 13:53:18 -08003488 return nested_vmx_failInvalid(vcpu);
3489
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02003490 if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) {
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02003491 copy_enlightened_to_vmcs12(vmx, vmx->nested.hv_evmcs->hv_clean_fields);
Sean Christopherson55d23752018-12-03 13:53:18 -08003492 /* Enlightened VMCS doesn't have launch state */
3493 vmcs12->launch_state = !launch;
3494 } else if (enable_shadow_vmcs) {
3495 copy_shadow_to_vmcs12(vmx);
3496 }
3497
3498 /*
3499 * The nested entry process starts with enforcing various prerequisites
3500 * on vmcs12 as required by the Intel SDM, and act appropriately when
3501 * they fail: As the SDM explains, some conditions should cause the
3502 * instruction to fail, while others will cause the instruction to seem
3503 * to succeed, but return an EXIT_REASON_INVALID_STATE.
3504 * To speed up the normal (success) code path, we should avoid checking
3505 * for misconfigurations which will anyway be caught by the processor
3506 * when using the merged vmcs02.
3507 */
Sean Christophersonfc595f32020-08-12 11:06:15 -07003508 if (CC(interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS))
Sean Christophersonb2656e42020-06-08 18:56:07 -07003509 return nested_vmx_fail(vcpu, VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
Sean Christopherson55d23752018-12-03 13:53:18 -08003510
Sean Christophersonfc595f32020-08-12 11:06:15 -07003511 if (CC(vmcs12->launch_state == launch))
Sean Christophersonb2656e42020-06-08 18:56:07 -07003512 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08003513 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
3514 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
3515
Paolo Bonzini98d9e852019-04-12 10:19:57 +02003516 if (nested_vmx_check_controls(vcpu, vmcs12))
Sean Christophersonb2656e42020-06-08 18:56:07 -07003517 return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Sean Christopherson5478ba32019-04-11 12:18:06 -07003518
Paolo Bonzini98d9e852019-04-12 10:19:57 +02003519 if (nested_vmx_check_host_state(vcpu, vmcs12))
Sean Christophersonb2656e42020-06-08 18:56:07 -07003520 return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
Sean Christopherson55d23752018-12-03 13:53:18 -08003521
3522 /*
3523 * We're finally done with prerequisite checking, and can start with
3524 * the nested entry.
3525 */
3526 vmx->nested.nested_run_pending = 1;
Peter Shier850448f2020-05-26 14:51:06 -07003527 vmx->nested.has_preemption_timer_deadline = false;
Jim Mattson671ddc72019-10-15 10:44:05 -07003528 status = nested_vmx_enter_non_root_mode(vcpu, true);
3529 if (unlikely(status != NVMX_VMENTRY_SUCCESS))
3530 goto vmentry_failed;
Sean Christopherson55d23752018-12-03 13:53:18 -08003531
Sean Christopherson25bb2cf2020-08-12 10:51:29 -07003532 /* Emulate processing of posted interrupts on VM-Enter. */
3533 if (nested_cpu_has_posted_intr(vmcs12) &&
3534 kvm_apic_has_interrupt(vcpu) == vmx->nested.posted_intr_nv) {
3535 vmx->nested.pi_pending = true;
3536 kvm_make_request(KVM_REQ_EVENT, vcpu);
3537 kvm_apic_clear_irr(vcpu, vmx->nested.posted_intr_nv);
3538 }
3539
Sean Christopherson55d23752018-12-03 13:53:18 -08003540 /* Hide L1D cache contents from the nested guest. */
3541 vmx->vcpu.arch.l1tf_flush_l1d = true;
3542
3543 /*
3544 * Must happen outside of nested_vmx_enter_non_root_mode() as it will
3545 * also be used as part of restoring nVMX state for
3546 * snapshot restore (migration).
3547 *
3548 * In this flow, it is assumed that vmcs12 cache was
Ingo Molnar163b0992021-03-21 22:28:53 +01003549 * transferred as part of captured nVMX state and should
Sean Christopherson55d23752018-12-03 13:53:18 -08003550 * therefore not be read from guest memory (which may not
3551 * exist on destination host yet).
3552 */
3553 nested_cache_shadow_vmcs12(vcpu, vmcs12);
3554
Yadong Qibf0cd882020-11-06 14:51:22 +08003555 switch (vmcs12->guest_activity_state) {
3556 case GUEST_ACTIVITY_HLT:
3557 /*
3558 * If we're entering a halted L2 vcpu and the L2 vcpu won't be
3559 * awakened by event injection or by an NMI-window VM-exit or
3560 * by an interrupt-window VM-exit, halt the vcpu.
3561 */
3562 if (!(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) &&
3563 !nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING) &&
3564 !(nested_cpu_has(vmcs12, CPU_BASED_INTR_WINDOW_EXITING) &&
3565 (vmcs12->guest_rflags & X86_EFLAGS_IF))) {
3566 vmx->nested.nested_run_pending = 0;
3567 return kvm_vcpu_halt(vcpu);
3568 }
3569 break;
3570 case GUEST_ACTIVITY_WAIT_SIPI:
Sean Christopherson55d23752018-12-03 13:53:18 -08003571 vmx->nested.nested_run_pending = 0;
Yadong Qibf0cd882020-11-06 14:51:22 +08003572 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
3573 break;
3574 default:
3575 break;
Sean Christopherson55d23752018-12-03 13:53:18 -08003576 }
Yadong Qibf0cd882020-11-06 14:51:22 +08003577
Sean Christopherson55d23752018-12-03 13:53:18 -08003578 return 1;
Jim Mattson671ddc72019-10-15 10:44:05 -07003579
3580vmentry_failed:
3581 vmx->nested.nested_run_pending = 0;
3582 if (status == NVMX_VMENTRY_KVM_INTERNAL_ERROR)
3583 return 0;
3584 if (status == NVMX_VMENTRY_VMEXIT)
3585 return 1;
3586 WARN_ON_ONCE(status != NVMX_VMENTRY_VMFAIL);
Sean Christophersonb2656e42020-06-08 18:56:07 -07003587 return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Sean Christopherson55d23752018-12-03 13:53:18 -08003588}
3589
3590/*
3591 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
Miaohe Lin67b0ae42019-12-11 14:26:22 +08003592 * because L2 may have changed some cr0 bits directly (CR0_GUEST_HOST_MASK).
Sean Christopherson55d23752018-12-03 13:53:18 -08003593 * This function returns the new value we should put in vmcs12.guest_cr0.
3594 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
3595 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
3596 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
3597 * didn't trap the bit, because if L1 did, so would L0).
3598 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
3599 * been modified by L2, and L1 knows it. So just leave the old value of
3600 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
3601 * isn't relevant, because if L0 traps this bit it can set it to anything.
3602 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
3603 * changed these bits, and therefore they need to be updated, but L0
3604 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
3605 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
3606 */
3607static inline unsigned long
3608vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
3609{
3610 return
3611 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
3612 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
3613 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
3614 vcpu->arch.cr0_guest_owned_bits));
3615}
3616
3617static inline unsigned long
3618vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
3619{
3620 return
3621 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
3622 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
3623 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
3624 vcpu->arch.cr4_guest_owned_bits));
3625}
3626
3627static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
3628 struct vmcs12 *vmcs12)
3629{
3630 u32 idt_vectoring;
3631 unsigned int nr;
3632
3633 if (vcpu->arch.exception.injected) {
3634 nr = vcpu->arch.exception.nr;
3635 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
3636
3637 if (kvm_exception_is_soft(nr)) {
3638 vmcs12->vm_exit_instruction_len =
3639 vcpu->arch.event_exit_inst_len;
3640 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
3641 } else
3642 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
3643
3644 if (vcpu->arch.exception.has_error_code) {
3645 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
3646 vmcs12->idt_vectoring_error_code =
3647 vcpu->arch.exception.error_code;
3648 }
3649
3650 vmcs12->idt_vectoring_info_field = idt_vectoring;
3651 } else if (vcpu->arch.nmi_injected) {
3652 vmcs12->idt_vectoring_info_field =
3653 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
3654 } else if (vcpu->arch.interrupt.injected) {
3655 nr = vcpu->arch.interrupt.nr;
3656 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
3657
3658 if (vcpu->arch.interrupt.soft) {
3659 idt_vectoring |= INTR_TYPE_SOFT_INTR;
3660 vmcs12->vm_entry_instruction_len =
3661 vcpu->arch.event_exit_inst_len;
3662 } else
3663 idt_vectoring |= INTR_TYPE_EXT_INTR;
3664
3665 vmcs12->idt_vectoring_info_field = idt_vectoring;
3666 }
3667}
3668
3669
Paolo Bonzini96b100c2020-03-17 18:32:50 +01003670void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08003671{
3672 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3673 gfn_t gfn;
3674
3675 /*
3676 * Don't need to mark the APIC access page dirty; it is never
3677 * written to by the CPU during APIC virtualization.
3678 */
3679
3680 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
3681 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
3682 kvm_vcpu_mark_page_dirty(vcpu, gfn);
3683 }
3684
3685 if (nested_cpu_has_posted_intr(vmcs12)) {
3686 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
3687 kvm_vcpu_mark_page_dirty(vcpu, gfn);
3688 }
3689}
3690
Jim Mattson650293c2021-06-04 10:26:02 -07003691static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08003692{
3693 struct vcpu_vmx *vmx = to_vmx(vcpu);
3694 int max_irr;
3695 void *vapic_page;
3696 u16 status;
3697
Jim Mattson966eefb2021-06-04 10:26:06 -07003698 if (!vmx->nested.pi_pending)
Jim Mattson650293c2021-06-04 10:26:02 -07003699 return 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08003700
Jim Mattson966eefb2021-06-04 10:26:06 -07003701 if (!vmx->nested.pi_desc)
3702 goto mmio_needed;
3703
Sean Christopherson55d23752018-12-03 13:53:18 -08003704 vmx->nested.pi_pending = false;
Jim Mattson966eefb2021-06-04 10:26:06 -07003705
Sean Christopherson55d23752018-12-03 13:53:18 -08003706 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
Jim Mattson650293c2021-06-04 10:26:02 -07003707 return 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08003708
3709 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
3710 if (max_irr != 256) {
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003711 vapic_page = vmx->nested.virtual_apic_map.hva;
3712 if (!vapic_page)
Jim Mattson0fe998b2021-06-04 10:26:05 -07003713 goto mmio_needed;
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003714
Sean Christopherson55d23752018-12-03 13:53:18 -08003715 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
3716 vapic_page, &max_irr);
Sean Christopherson55d23752018-12-03 13:53:18 -08003717 status = vmcs_read16(GUEST_INTR_STATUS);
3718 if ((u8)max_irr > ((u8)status & 0xff)) {
3719 status &= ~0xff;
3720 status |= (u8)max_irr;
3721 vmcs_write16(GUEST_INTR_STATUS, status);
3722 }
3723 }
3724
3725 nested_mark_vmcs12_pages_dirty(vcpu);
Jim Mattson650293c2021-06-04 10:26:02 -07003726 return 0;
Jim Mattson0fe998b2021-06-04 10:26:05 -07003727
3728mmio_needed:
3729 kvm_handle_memory_failure(vcpu, X86EMUL_IO_NEEDED, NULL);
3730 return -ENXIO;
Sean Christopherson55d23752018-12-03 13:53:18 -08003731}
3732
3733static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3734 unsigned long exit_qual)
3735{
3736 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3737 unsigned int nr = vcpu->arch.exception.nr;
3738 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3739
3740 if (vcpu->arch.exception.has_error_code) {
3741 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3742 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3743 }
3744
3745 if (kvm_exception_is_soft(nr))
3746 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3747 else
3748 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3749
3750 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3751 vmx_get_nmi_mask(vcpu))
3752 intr_info |= INTR_INFO_UNBLOCK_NMI;
3753
3754 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3755}
3756
Oliver Upton684c0422020-02-07 02:36:05 -08003757/*
3758 * Returns true if a debug trap is pending delivery.
3759 *
3760 * In KVM, debug traps bear an exception payload. As such, the class of a #DB
3761 * exception may be inferred from the presence of an exception payload.
3762 */
3763static inline bool vmx_pending_dbg_trap(struct kvm_vcpu *vcpu)
3764{
3765 return vcpu->arch.exception.pending &&
3766 vcpu->arch.exception.nr == DB_VECTOR &&
3767 vcpu->arch.exception.payload;
3768}
3769
3770/*
3771 * Certain VM-exits set the 'pending debug exceptions' field to indicate a
3772 * recognized #DB (data or single-step) that has yet to be delivered. Since KVM
3773 * represents these debug traps with a payload that is said to be compatible
3774 * with the 'pending debug exceptions' field, write the payload to the VMCS
3775 * field if a VM-exit is delivered before the debug trap.
3776 */
3777static void nested_vmx_update_pending_dbg(struct kvm_vcpu *vcpu)
3778{
3779 if (vmx_pending_dbg_trap(vcpu))
3780 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
3781 vcpu->arch.exception.payload);
3782}
3783
Sean Christophersond2060bd2020-04-22 19:25:39 -07003784static bool nested_vmx_preemption_timer_pending(struct kvm_vcpu *vcpu)
3785{
3786 return nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
3787 to_vmx(vcpu)->nested.preemption_timer_expired;
3788}
3789
Sean Christophersona1c77ab2020-03-02 22:27:35 -08003790static int vmx_check_nested_events(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08003791{
3792 struct vcpu_vmx *vmx = to_vmx(vcpu);
3793 unsigned long exit_qual;
3794 bool block_nested_events =
3795 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003796 bool mtf_pending = vmx->nested.mtf_pending;
Liran Alon4b9852f2019-08-26 13:24:49 +03003797 struct kvm_lapic *apic = vcpu->arch.apic;
3798
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003799 /*
3800 * Clear the MTF state. If a higher priority VM-exit is delivered first,
3801 * this state is discarded.
3802 */
Oliver Upton5c8beb42020-04-06 20:12:37 +00003803 if (!block_nested_events)
3804 vmx->nested.mtf_pending = false;
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003805
Liran Alon4b9852f2019-08-26 13:24:49 +03003806 if (lapic_in_kernel(vcpu) &&
3807 test_bit(KVM_APIC_INIT, &apic->pending_events)) {
3808 if (block_nested_events)
3809 return -EBUSY;
Oliver Upton684c0422020-02-07 02:36:05 -08003810 nested_vmx_update_pending_dbg(vcpu);
Liran Alone64a8502019-11-11 14:16:05 +02003811 clear_bit(KVM_APIC_INIT, &apic->pending_events);
Yadong Qibf0cd882020-11-06 14:51:22 +08003812 if (vcpu->arch.mp_state != KVM_MP_STATE_INIT_RECEIVED)
3813 nested_vmx_vmexit(vcpu, EXIT_REASON_INIT_SIGNAL, 0, 0);
3814 return 0;
3815 }
3816
3817 if (lapic_in_kernel(vcpu) &&
3818 test_bit(KVM_APIC_SIPI, &apic->pending_events)) {
3819 if (block_nested_events)
3820 return -EBUSY;
3821
3822 clear_bit(KVM_APIC_SIPI, &apic->pending_events);
3823 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED)
3824 nested_vmx_vmexit(vcpu, EXIT_REASON_SIPI_SIGNAL, 0,
3825 apic->sipi_vector & 0xFFUL);
Liran Alon4b9852f2019-08-26 13:24:49 +03003826 return 0;
3827 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003828
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003829 /*
3830 * Process any exceptions that are not debug traps before MTF.
Maxim Levitsky4020da32021-04-01 17:38:14 +03003831 *
3832 * Note that only a pending nested run can block a pending exception.
3833 * Otherwise an injected NMI/interrupt should either be
3834 * lost or delivered to the nested hypervisor in the IDT_VECTORING_INFO,
3835 * while delivering the pending exception.
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003836 */
Maxim Levitsky4020da32021-04-01 17:38:14 +03003837
Sean Christopherson6ce347a2020-04-22 19:25:38 -07003838 if (vcpu->arch.exception.pending && !vmx_pending_dbg_trap(vcpu)) {
Maxim Levitsky4020da32021-04-01 17:38:14 +03003839 if (vmx->nested.nested_run_pending)
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003840 return -EBUSY;
Sean Christopherson6ce347a2020-04-22 19:25:38 -07003841 if (!nested_vmx_check_exception(vcpu, &exit_qual))
3842 goto no_vmexit;
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003843 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
3844 return 0;
3845 }
3846
3847 if (mtf_pending) {
3848 if (block_nested_events)
3849 return -EBUSY;
3850 nested_vmx_update_pending_dbg(vcpu);
3851 nested_vmx_vmexit(vcpu, EXIT_REASON_MONITOR_TRAP_FLAG, 0, 0);
3852 return 0;
3853 }
3854
Sean Christopherson6ce347a2020-04-22 19:25:38 -07003855 if (vcpu->arch.exception.pending) {
Maxim Levitsky4020da32021-04-01 17:38:14 +03003856 if (vmx->nested.nested_run_pending)
Sean Christopherson55d23752018-12-03 13:53:18 -08003857 return -EBUSY;
Sean Christopherson6ce347a2020-04-22 19:25:38 -07003858 if (!nested_vmx_check_exception(vcpu, &exit_qual))
3859 goto no_vmexit;
Sean Christopherson55d23752018-12-03 13:53:18 -08003860 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
3861 return 0;
3862 }
3863
Sean Christophersond2060bd2020-04-22 19:25:39 -07003864 if (nested_vmx_preemption_timer_pending(vcpu)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08003865 if (block_nested_events)
3866 return -EBUSY;
3867 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
3868 return 0;
3869 }
3870
Sean Christopherson1cd2f0b2020-04-22 19:25:46 -07003871 if (vcpu->arch.smi_pending && !is_smm(vcpu)) {
3872 if (block_nested_events)
3873 return -EBUSY;
3874 goto no_vmexit;
3875 }
3876
Sean Christopherson15ff0b42020-04-22 19:25:45 -07003877 if (vcpu->arch.nmi_pending && !vmx_nmi_blocked(vcpu)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08003878 if (block_nested_events)
3879 return -EBUSY;
Sean Christopherson15ff0b42020-04-22 19:25:45 -07003880 if (!nested_exit_on_nmi(vcpu))
3881 goto no_vmexit;
3882
Sean Christopherson55d23752018-12-03 13:53:18 -08003883 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
3884 NMI_VECTOR | INTR_TYPE_NMI_INTR |
3885 INTR_INFO_VALID_MASK, 0);
3886 /*
3887 * The NMI-triggered VM exit counts as injection:
3888 * clear this one and block further NMIs.
3889 */
3890 vcpu->arch.nmi_pending = 0;
3891 vmx_set_nmi_mask(vcpu, true);
3892 return 0;
3893 }
3894
Sean Christopherson15ff0b42020-04-22 19:25:45 -07003895 if (kvm_cpu_has_interrupt(vcpu) && !vmx_interrupt_blocked(vcpu)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08003896 if (block_nested_events)
3897 return -EBUSY;
Sean Christopherson15ff0b42020-04-22 19:25:45 -07003898 if (!nested_exit_on_intr(vcpu))
3899 goto no_vmexit;
Sean Christopherson55d23752018-12-03 13:53:18 -08003900 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
3901 return 0;
3902 }
3903
Sean Christopherson6ce347a2020-04-22 19:25:38 -07003904no_vmexit:
Jim Mattson650293c2021-06-04 10:26:02 -07003905 return vmx_complete_nested_posted_interrupt(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08003906}
3907
3908static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
3909{
3910 ktime_t remaining =
3911 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
3912 u64 value;
3913
3914 if (ktime_to_ns(remaining) <= 0)
3915 return 0;
3916
3917 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
3918 do_div(value, 1000000);
3919 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
3920}
3921
Sean Christopherson7952d762019-05-07 08:36:29 -07003922static bool is_vmcs12_ext_field(unsigned long field)
Sean Christopherson55d23752018-12-03 13:53:18 -08003923{
Sean Christopherson7952d762019-05-07 08:36:29 -07003924 switch (field) {
3925 case GUEST_ES_SELECTOR:
3926 case GUEST_CS_SELECTOR:
3927 case GUEST_SS_SELECTOR:
3928 case GUEST_DS_SELECTOR:
3929 case GUEST_FS_SELECTOR:
3930 case GUEST_GS_SELECTOR:
3931 case GUEST_LDTR_SELECTOR:
3932 case GUEST_TR_SELECTOR:
3933 case GUEST_ES_LIMIT:
3934 case GUEST_CS_LIMIT:
3935 case GUEST_SS_LIMIT:
3936 case GUEST_DS_LIMIT:
3937 case GUEST_FS_LIMIT:
3938 case GUEST_GS_LIMIT:
3939 case GUEST_LDTR_LIMIT:
3940 case GUEST_TR_LIMIT:
3941 case GUEST_GDTR_LIMIT:
3942 case GUEST_IDTR_LIMIT:
3943 case GUEST_ES_AR_BYTES:
3944 case GUEST_DS_AR_BYTES:
3945 case GUEST_FS_AR_BYTES:
3946 case GUEST_GS_AR_BYTES:
3947 case GUEST_LDTR_AR_BYTES:
3948 case GUEST_TR_AR_BYTES:
3949 case GUEST_ES_BASE:
3950 case GUEST_CS_BASE:
3951 case GUEST_SS_BASE:
3952 case GUEST_DS_BASE:
3953 case GUEST_FS_BASE:
3954 case GUEST_GS_BASE:
3955 case GUEST_LDTR_BASE:
3956 case GUEST_TR_BASE:
3957 case GUEST_GDTR_BASE:
3958 case GUEST_IDTR_BASE:
3959 case GUEST_PENDING_DBG_EXCEPTIONS:
3960 case GUEST_BNDCFGS:
3961 return true;
3962 default:
3963 break;
3964 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003965
Sean Christopherson7952d762019-05-07 08:36:29 -07003966 return false;
3967}
3968
3969static void sync_vmcs02_to_vmcs12_rare(struct kvm_vcpu *vcpu,
3970 struct vmcs12 *vmcs12)
3971{
3972 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08003973
3974 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
3975 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
3976 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
3977 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
3978 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
3979 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
3980 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
3981 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
3982 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
3983 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
3984 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
3985 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
3986 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
3987 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
3988 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
3989 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
3990 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
3991 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
3992 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
Sean Christopherson55d23752018-12-03 13:53:18 -08003993 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
3994 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
3995 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
3996 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
3997 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
3998 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
3999 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
4000 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
4001 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
4002 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
4003 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
4004 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
4005 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
4006 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
4007 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
Sean Christopherson7952d762019-05-07 08:36:29 -07004008 vmcs12->guest_pending_dbg_exceptions =
4009 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
4010 if (kvm_mpx_supported())
4011 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
4012
4013 vmx->nested.need_sync_vmcs02_to_vmcs12_rare = false;
4014}
4015
4016static void copy_vmcs02_to_vmcs12_rare(struct kvm_vcpu *vcpu,
4017 struct vmcs12 *vmcs12)
4018{
4019 struct vcpu_vmx *vmx = to_vmx(vcpu);
4020 int cpu;
4021
4022 if (!vmx->nested.need_sync_vmcs02_to_vmcs12_rare)
4023 return;
4024
4025
4026 WARN_ON_ONCE(vmx->loaded_vmcs != &vmx->vmcs01);
4027
4028 cpu = get_cpu();
4029 vmx->loaded_vmcs = &vmx->nested.vmcs02;
Sean Christopherson1af1bb02020-05-06 16:58:50 -07004030 vmx_vcpu_load_vmcs(vcpu, cpu, &vmx->vmcs01);
Sean Christopherson7952d762019-05-07 08:36:29 -07004031
4032 sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
4033
4034 vmx->loaded_vmcs = &vmx->vmcs01;
Sean Christopherson1af1bb02020-05-06 16:58:50 -07004035 vmx_vcpu_load_vmcs(vcpu, cpu, &vmx->nested.vmcs02);
Sean Christopherson7952d762019-05-07 08:36:29 -07004036 put_cpu();
4037}
4038
4039/*
4040 * Update the guest state fields of vmcs12 to reflect changes that
4041 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
4042 * VM-entry controls is also updated, since this is really a guest
4043 * state bit.)
4044 */
4045static void sync_vmcs02_to_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
4046{
4047 struct vcpu_vmx *vmx = to_vmx(vcpu);
4048
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02004049 if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
Sean Christopherson7952d762019-05-07 08:36:29 -07004050 sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
4051
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02004052 vmx->nested.need_sync_vmcs02_to_vmcs12_rare =
4053 !evmptr_is_valid(vmx->nested.hv_evmcs_vmptr);
Sean Christopherson7952d762019-05-07 08:36:29 -07004054
4055 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
4056 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
4057
4058 vmcs12->guest_rsp = kvm_rsp_read(vcpu);
4059 vmcs12->guest_rip = kvm_rip_read(vcpu);
4060 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
4061
4062 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
4063 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
Sean Christopherson55d23752018-12-03 13:53:18 -08004064
4065 vmcs12->guest_interruptibility_info =
4066 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
Sean Christopherson7952d762019-05-07 08:36:29 -07004067
Sean Christopherson55d23752018-12-03 13:53:18 -08004068 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
4069 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
Yadong Qibf0cd882020-11-06 14:51:22 +08004070 else if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED)
4071 vmcs12->guest_activity_state = GUEST_ACTIVITY_WAIT_SIPI;
Sean Christopherson55d23752018-12-03 13:53:18 -08004072 else
4073 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
4074
Paolo Bonzinib4b65b52019-01-29 19:12:35 +01004075 if (nested_cpu_has_preemption_timer(vmcs12) &&
Peter Shier850448f2020-05-26 14:51:06 -07004076 vmcs12->vm_exit_controls & VM_EXIT_SAVE_VMX_PREEMPTION_TIMER &&
4077 !vmx->nested.nested_run_pending)
4078 vmcs12->vmx_preemption_timer_value =
4079 vmx_get_preemption_timer_value(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08004080
4081 /*
4082 * In some cases (usually, nested EPT), L2 is allowed to change its
4083 * own CR3 without exiting. If it has changed it, we must keep it.
4084 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
4085 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
4086 *
4087 * Additionally, restore L2's PDPTR to vmcs12.
4088 */
4089 if (enable_ept) {
4090 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Sean Christophersonc7554efc2019-05-07 09:06:40 -07004091 if (nested_cpu_has_ept(vmcs12) && is_pae_paging(vcpu)) {
4092 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
4093 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
4094 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
4095 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
4096 }
Sean Christopherson55d23752018-12-03 13:53:18 -08004097 }
4098
4099 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
4100
4101 if (nested_cpu_has_vid(vmcs12))
4102 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
4103
4104 vmcs12->vm_entry_controls =
4105 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
4106 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
4107
Sean Christopherson699a1ac2019-05-07 09:06:37 -07004108 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS)
Sean Christopherson55d23752018-12-03 13:53:18 -08004109 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
Sean Christopherson55d23752018-12-03 13:53:18 -08004110
Sean Christopherson55d23752018-12-03 13:53:18 -08004111 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
4112 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Sean Christopherson55d23752018-12-03 13:53:18 -08004113}
4114
4115/*
4116 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
4117 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
4118 * and this function updates it to reflect the changes to the guest state while
4119 * L2 was running (and perhaps made some exits which were handled directly by L0
4120 * without going back to L1), and to reflect the exit reason.
4121 * Note that we do not have to copy here all VMCS fields, just those that
4122 * could have changed by the L2 guest or the exit - i.e., the guest-state and
4123 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
4124 * which already writes to vmcs12 directly.
4125 */
4126static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004127 u32 vm_exit_reason, u32 exit_intr_info,
Sean Christopherson55d23752018-12-03 13:53:18 -08004128 unsigned long exit_qualification)
4129{
Sean Christopherson55d23752018-12-03 13:53:18 -08004130 /* update exit information fields: */
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004131 vmcs12->vm_exit_reason = vm_exit_reason;
Sean Christopherson3c0c2ad2021-04-12 16:21:37 +12004132 if (to_vmx(vcpu)->exit_reason.enclave_mode)
4133 vmcs12->vm_exit_reason |= VMX_EXIT_REASONS_SGX_ENCLAVE_MODE;
Sean Christopherson55d23752018-12-03 13:53:18 -08004134 vmcs12->exit_qualification = exit_qualification;
4135 vmcs12->vm_exit_intr_info = exit_intr_info;
4136
4137 vmcs12->idt_vectoring_info_field = 0;
4138 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
4139 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
4140
4141 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
4142 vmcs12->launch_state = 1;
4143
4144 /* vm_entry_intr_info_field is cleared on exit. Emulate this
4145 * instead of reading the real value. */
4146 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
4147
4148 /*
4149 * Transfer the event that L0 or L1 may wanted to inject into
4150 * L2 to IDT_VECTORING_INFO_FIELD.
4151 */
4152 vmcs12_save_pending_event(vcpu, vmcs12);
Krish Sadhukhana0d4f802018-12-04 19:00:13 -05004153
4154 /*
4155 * According to spec, there's no need to store the guest's
4156 * MSRs if the exit is due to a VM-entry failure that occurs
4157 * during or after loading the guest state. Since this exit
4158 * does not fall in that category, we need to save the MSRs.
4159 */
4160 if (nested_vmx_store_msr(vcpu,
4161 vmcs12->vm_exit_msr_store_addr,
4162 vmcs12->vm_exit_msr_store_count))
4163 nested_vmx_abort(vcpu,
4164 VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Sean Christopherson55d23752018-12-03 13:53:18 -08004165 }
4166
4167 /*
4168 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
4169 * preserved above and would only end up incorrectly in L1.
4170 */
4171 vcpu->arch.nmi_injected = false;
4172 kvm_clear_exception_queue(vcpu);
4173 kvm_clear_interrupt_queue(vcpu);
4174}
4175
4176/*
4177 * A part of what we need to when the nested L2 guest exits and we want to
4178 * run its L1 parent, is to reset L1's guest state to the host state specified
4179 * in vmcs12.
4180 * This function is to be called not only on normal nested exit, but also on
4181 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
4182 * Failures During or After Loading Guest State").
4183 * This function should be called when the active VMCS is L1's (vmcs01).
4184 */
4185static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
4186 struct vmcs12 *vmcs12)
4187{
Sean Christopherson68cda402020-05-11 15:05:29 -07004188 enum vm_entry_failure_code ignored;
Sean Christopherson55d23752018-12-03 13:53:18 -08004189 struct kvm_segment seg;
Sean Christopherson55d23752018-12-03 13:53:18 -08004190
4191 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
4192 vcpu->arch.efer = vmcs12->host_ia32_efer;
4193 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
4194 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
4195 else
4196 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
4197 vmx_set_efer(vcpu, vcpu->arch.efer);
4198
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02004199 kvm_rsp_write(vcpu, vmcs12->host_rsp);
4200 kvm_rip_write(vcpu, vmcs12->host_rip);
Sean Christopherson55d23752018-12-03 13:53:18 -08004201 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
4202 vmx_set_interrupt_shadow(vcpu, 0);
4203
4204 /*
4205 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
4206 * actually changed, because vmx_set_cr0 refers to efer set above.
4207 *
4208 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
4209 * (KVM doesn't change it);
4210 */
Sean Christophersonfa71e952020-07-02 21:04:22 -07004211 vcpu->arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS;
Sean Christopherson55d23752018-12-03 13:53:18 -08004212 vmx_set_cr0(vcpu, vmcs12->host_cr0);
4213
4214 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
4215 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
4216 vmx_set_cr4(vcpu, vmcs12->host_cr4);
4217
4218 nested_ept_uninit_mmu_context(vcpu);
4219
4220 /*
4221 * Only PDPTE load can fail as the value of cr3 was checked on entry and
4222 * couldn't have changed.
4223 */
Maxim Levitsky0f857222021-06-07 12:02:00 +03004224 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, true, &ignored))
Sean Christopherson55d23752018-12-03 13:53:18 -08004225 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
4226
Sean Christopherson50b265a2020-03-20 14:28:19 -07004227 nested_vmx_transition_tlb_flush(vcpu, vmcs12, false);
Sean Christopherson55d23752018-12-03 13:53:18 -08004228
4229 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
4230 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
4231 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
4232 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
4233 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
4234 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
4235 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
4236
4237 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
4238 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
4239 vmcs_write64(GUEST_BNDCFGS, 0);
4240
4241 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
4242 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
4243 vcpu->arch.pat = vmcs12->host_ia32_pat;
4244 }
4245 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Oliver Uptond1968422019-12-13 16:33:58 -08004246 WARN_ON_ONCE(kvm_set_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL,
4247 vmcs12->host_ia32_perf_global_ctrl));
Sean Christopherson55d23752018-12-03 13:53:18 -08004248
4249 /* Set L1 segment info according to Intel SDM
4250 27.5.2 Loading Host Segment and Descriptor-Table Registers */
4251 seg = (struct kvm_segment) {
4252 .base = 0,
4253 .limit = 0xFFFFFFFF,
4254 .selector = vmcs12->host_cs_selector,
4255 .type = 11,
4256 .present = 1,
4257 .s = 1,
4258 .g = 1
4259 };
4260 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
4261 seg.l = 1;
4262 else
4263 seg.db = 1;
4264 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
4265 seg = (struct kvm_segment) {
4266 .base = 0,
4267 .limit = 0xFFFFFFFF,
4268 .type = 3,
4269 .present = 1,
4270 .s = 1,
4271 .db = 1,
4272 .g = 1
4273 };
4274 seg.selector = vmcs12->host_ds_selector;
4275 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
4276 seg.selector = vmcs12->host_es_selector;
4277 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
4278 seg.selector = vmcs12->host_ss_selector;
4279 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
4280 seg.selector = vmcs12->host_fs_selector;
4281 seg.base = vmcs12->host_fs_base;
4282 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
4283 seg.selector = vmcs12->host_gs_selector;
4284 seg.base = vmcs12->host_gs_base;
4285 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
4286 seg = (struct kvm_segment) {
4287 .base = vmcs12->host_tr_base,
4288 .limit = 0x67,
4289 .selector = vmcs12->host_tr_selector,
4290 .type = 11,
4291 .present = 1
4292 };
4293 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
4294
4295 kvm_set_dr(vcpu, 7, 0x400);
4296 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
4297
4298 if (cpu_has_vmx_msr_bitmap())
4299 vmx_update_msr_bitmap(vcpu);
4300
4301 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
4302 vmcs12->vm_exit_msr_load_count))
4303 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
4304}
4305
4306static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx)
4307{
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07004308 struct vmx_uret_msr *efer_msr;
Sean Christopherson55d23752018-12-03 13:53:18 -08004309 unsigned int i;
4310
4311 if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER)
4312 return vmcs_read64(GUEST_IA32_EFER);
4313
4314 if (cpu_has_load_ia32_efer())
4315 return host_efer;
4316
4317 for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) {
4318 if (vmx->msr_autoload.guest.val[i].index == MSR_EFER)
4319 return vmx->msr_autoload.guest.val[i].value;
4320 }
4321
Sean Christophersond85a8032020-09-23 11:04:06 -07004322 efer_msr = vmx_find_uret_msr(vmx, MSR_EFER);
Sean Christopherson55d23752018-12-03 13:53:18 -08004323 if (efer_msr)
4324 return efer_msr->data;
4325
4326 return host_efer;
4327}
4328
4329static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu)
4330{
4331 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4332 struct vcpu_vmx *vmx = to_vmx(vcpu);
4333 struct vmx_msr_entry g, h;
Sean Christopherson55d23752018-12-03 13:53:18 -08004334 gpa_t gpa;
4335 u32 i, j;
4336
4337 vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT);
4338
4339 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
4340 /*
4341 * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set
4342 * as vmcs01.GUEST_DR7 contains a userspace defined value
4343 * and vcpu->arch.dr7 is not squirreled away before the
4344 * nested VMENTER (not worth adding a variable in nested_vmx).
4345 */
4346 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
4347 kvm_set_dr(vcpu, 7, DR7_FIXED_1);
4348 else
4349 WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7)));
4350 }
4351
4352 /*
4353 * Note that calling vmx_set_{efer,cr0,cr4} is important as they
4354 * handle a variety of side effects to KVM's software model.
4355 */
4356 vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx));
4357
Sean Christophersonfa71e952020-07-02 21:04:22 -07004358 vcpu->arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS;
Sean Christopherson55d23752018-12-03 13:53:18 -08004359 vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW));
4360
4361 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
4362 vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW));
4363
4364 nested_ept_uninit_mmu_context(vcpu);
Sean Christophersonf087a022019-06-07 11:55:34 -07004365 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07004366 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Sean Christopherson55d23752018-12-03 13:53:18 -08004367
4368 /*
4369 * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs
4370 * from vmcs01 (if necessary). The PDPTRs are not loaded on
4371 * VMFail, like everything else we just need to ensure our
4372 * software model is up-to-date.
4373 */
Sean Christopherson9932b492020-04-15 13:34:50 -07004374 if (enable_ept && is_pae_paging(vcpu))
Sean Christophersonf087a022019-06-07 11:55:34 -07004375 ept_save_pdptrs(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08004376
4377 kvm_mmu_reset_context(vcpu);
4378
4379 if (cpu_has_vmx_msr_bitmap())
4380 vmx_update_msr_bitmap(vcpu);
4381
4382 /*
4383 * This nasty bit of open coding is a compromise between blindly
4384 * loading L1's MSRs using the exit load lists (incorrect emulation
4385 * of VMFail), leaving the nested VM's MSRs in the software model
4386 * (incorrect behavior) and snapshotting the modified MSRs (too
4387 * expensive since the lists are unbound by hardware). For each
4388 * MSR that was (prematurely) loaded from the nested VMEntry load
4389 * list, reload it from the exit load list if it exists and differs
4390 * from the guest value. The intent is to stuff host state as
4391 * silently as possible, not to fully process the exit load list.
4392 */
Sean Christopherson55d23752018-12-03 13:53:18 -08004393 for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) {
4394 gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g));
4395 if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) {
4396 pr_debug_ratelimited(
4397 "%s read MSR index failed (%u, 0x%08llx)\n",
4398 __func__, i, gpa);
4399 goto vmabort;
4400 }
4401
4402 for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) {
4403 gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h));
4404 if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) {
4405 pr_debug_ratelimited(
4406 "%s read MSR failed (%u, 0x%08llx)\n",
4407 __func__, j, gpa);
4408 goto vmabort;
4409 }
4410 if (h.index != g.index)
4411 continue;
4412 if (h.value == g.value)
4413 break;
4414
4415 if (nested_vmx_load_msr_check(vcpu, &h)) {
4416 pr_debug_ratelimited(
4417 "%s check failed (%u, 0x%x, 0x%x)\n",
4418 __func__, j, h.index, h.reserved);
4419 goto vmabort;
4420 }
4421
Sean Christophersonf20935d2019-09-05 14:22:54 -07004422 if (kvm_set_msr(vcpu, h.index, h.value)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08004423 pr_debug_ratelimited(
4424 "%s WRMSR failed (%u, 0x%x, 0x%llx)\n",
4425 __func__, j, h.index, h.value);
4426 goto vmabort;
4427 }
4428 }
4429 }
4430
4431 return;
4432
4433vmabort:
4434 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
4435}
4436
4437/*
4438 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
4439 * and modify vmcs12 to make it see what it would expect to see there if
4440 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
4441 */
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004442void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason,
Sean Christopherson55d23752018-12-03 13:53:18 -08004443 u32 exit_intr_info, unsigned long exit_qualification)
4444{
4445 struct vcpu_vmx *vmx = to_vmx(vcpu);
4446 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4447
4448 /* trying to cancel vmlaunch/vmresume is a bug */
4449 WARN_ON_ONCE(vmx->nested.nested_run_pending);
4450
Sean Christophersoncb6a32c2021-03-02 09:45:14 -08004451 /* Similarly, triple faults in L2 should never escape. */
4452 WARN_ON_ONCE(kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu));
4453
Vitaly Kuznetsovf5c7e842021-05-03 17:08:51 +02004454 if (kvm_check_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu)) {
4455 /*
4456 * KVM_REQ_GET_NESTED_STATE_PAGES is also used to map
4457 * Enlightened VMCS after migration and we still need to
4458 * do that when something is forcing L2->L1 exit prior to
4459 * the first L2 run.
4460 */
4461 (void)nested_get_evmcs_page(vcpu);
4462 }
Maxim Levitskyf2c7ef32021-01-07 11:38:51 +02004463
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07004464 /* Service the TLB flush request for L2 before switching to L1. */
4465 if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu))
4466 kvm_vcpu_flush_tlb_current(vcpu);
4467
Peter Shier43fea4e2020-08-20 16:05:45 -07004468 /*
4469 * VCPU_EXREG_PDPTR will be clobbered in arch/x86/kvm/vmx/vmx.h between
4470 * now and the new vmentry. Ensure that the VMCS02 PDPTR fields are
4471 * up-to-date before switching to L1.
4472 */
4473 if (enable_ept && is_pae_paging(vcpu))
4474 vmx_ept_load_pdptrs(vcpu);
4475
Sean Christopherson55d23752018-12-03 13:53:18 -08004476 leave_guest_mode(vcpu);
4477
Paolo Bonzinib4b65b52019-01-29 19:12:35 +01004478 if (nested_cpu_has_preemption_timer(vmcs12))
4479 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
4480
Ilias Stamatisd041b5e2021-05-26 19:44:17 +01004481 if (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETTING)) {
4482 vcpu->arch.tsc_offset = vcpu->arch.l1_tsc_offset;
4483 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_TSC_SCALING))
4484 vcpu->arch.tsc_scaling_ratio = vcpu->arch.l1_tsc_scaling_ratio;
4485 }
Sean Christopherson55d23752018-12-03 13:53:18 -08004486
4487 if (likely(!vmx->fail)) {
Sean Christopherson3731905ef2019-05-07 08:36:27 -07004488 sync_vmcs02_to_vmcs12(vcpu, vmcs12);
Sean Christophersonf4f83162019-05-07 08:36:26 -07004489
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004490 if (vm_exit_reason != -1)
4491 prepare_vmcs12(vcpu, vmcs12, vm_exit_reason,
4492 exit_intr_info, exit_qualification);
Sean Christopherson55d23752018-12-03 13:53:18 -08004493
4494 /*
Sean Christopherson3731905ef2019-05-07 08:36:27 -07004495 * Must happen outside of sync_vmcs02_to_vmcs12() as it will
Sean Christopherson55d23752018-12-03 13:53:18 -08004496 * also be used to capture vmcs12 cache as part of
4497 * capturing nVMX state for snapshot (migration).
4498 *
4499 * Otherwise, this flush will dirty guest memory at a
4500 * point it is already assumed by user-space to be
4501 * immutable.
4502 */
4503 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08004504 } else {
4505 /*
4506 * The only expected VM-instruction error is "VM entry with
4507 * invalid control field(s)." Anything else indicates a
4508 * problem with L0. And we should never get here with a
4509 * VMFail of any type if early consistency checks are enabled.
4510 */
4511 WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) !=
4512 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
4513 WARN_ON_ONCE(nested_early_check);
4514 }
4515
4516 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
4517
4518 /* Update any VMCS fields that might have changed while L2 ran */
4519 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
4520 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
4521 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Ilias Stamatis1ab92872021-06-07 11:54:38 +01004522 if (kvm_has_tsc_control)
4523 vmcs_write64(TSC_MULTIPLIER, vcpu->arch.tsc_scaling_ratio);
4524
Liran Alon02d496cf2019-11-11 14:30:55 +02004525 if (vmx->nested.l1_tpr_threshold != -1)
4526 vmcs_write32(TPR_THRESHOLD, vmx->nested.l1_tpr_threshold);
Sean Christopherson55d23752018-12-03 13:53:18 -08004527
Sean Christopherson55d23752018-12-03 13:53:18 -08004528 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
4529 vmx->nested.change_vmcs01_virtual_apic_mode = false;
4530 vmx_set_virtual_apic_mode(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08004531 }
4532
Makarand Sonarea85863c2021-02-12 16:50:12 -08004533 if (vmx->nested.update_vmcs01_cpu_dirty_logging) {
4534 vmx->nested.update_vmcs01_cpu_dirty_logging = false;
4535 vmx_update_cpu_dirty_logging(vcpu);
4536 }
4537
Sean Christopherson55d23752018-12-03 13:53:18 -08004538 /* Unpin physical memory we referred to in vmcs02 */
4539 if (vmx->nested.apic_access_page) {
Liran Alonb11494b2019-11-21 00:31:47 +02004540 kvm_release_page_clean(vmx->nested.apic_access_page);
Sean Christopherson55d23752018-12-03 13:53:18 -08004541 vmx->nested.apic_access_page = NULL;
4542 }
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01004543 kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map, true);
KarimAllah Ahmed3278e042019-01-31 21:24:38 +01004544 kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map, true);
4545 vmx->nested.pi_desc = NULL;
Sean Christopherson55d23752018-12-03 13:53:18 -08004546
Sean Christopherson1196cb92020-03-20 14:28:23 -07004547 if (vmx->nested.reload_vmcs01_apic_access_page) {
4548 vmx->nested.reload_vmcs01_apic_access_page = false;
4549 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
4550 }
Sean Christopherson55d23752018-12-03 13:53:18 -08004551
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004552 if ((vm_exit_reason != -1) &&
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02004553 (enable_shadow_vmcs || evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)))
Sean Christopherson3731905ef2019-05-07 08:36:27 -07004554 vmx->nested.need_vmcs12_to_shadow_sync = true;
Sean Christopherson55d23752018-12-03 13:53:18 -08004555
4556 /* in case we halted in L2 */
4557 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
4558
4559 if (likely(!vmx->fail)) {
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004560 if ((u16)vm_exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
Sean Christophersona1c77ab2020-03-02 22:27:35 -08004561 nested_exit_intr_ack_set(vcpu)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08004562 int irq = kvm_cpu_get_interrupt(vcpu);
4563 WARN_ON(irq < 0);
4564 vmcs12->vm_exit_intr_info = irq |
4565 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
4566 }
4567
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004568 if (vm_exit_reason != -1)
Sean Christopherson55d23752018-12-03 13:53:18 -08004569 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
4570 vmcs12->exit_qualification,
4571 vmcs12->idt_vectoring_info_field,
4572 vmcs12->vm_exit_intr_info,
4573 vmcs12->vm_exit_intr_error_code,
4574 KVM_ISA_VMX);
4575
4576 load_vmcs12_host_state(vcpu, vmcs12);
4577
4578 return;
4579 }
4580
4581 /*
4582 * After an early L2 VM-entry failure, we're now back
4583 * in L1 which thinks it just finished a VMLAUNCH or
4584 * VMRESUME instruction, so we need to set the failure
4585 * flag and the VM-instruction error field of the VMCS
4586 * accordingly, and skip the emulated instruction.
4587 */
Sean Christophersonb2656e42020-06-08 18:56:07 -07004588 (void)nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Sean Christopherson55d23752018-12-03 13:53:18 -08004589
4590 /*
4591 * Restore L1's host state to KVM's software model. We're here
4592 * because a consistency check was caught by hardware, which
4593 * means some amount of guest state has been propagated to KVM's
4594 * model and needs to be unwound to the host's state.
4595 */
4596 nested_vmx_restore_host_state(vcpu);
4597
4598 vmx->fail = 0;
4599}
4600
Sean Christophersoncb6a32c2021-03-02 09:45:14 -08004601static void nested_vmx_triple_fault(struct kvm_vcpu *vcpu)
4602{
4603 nested_vmx_vmexit(vcpu, EXIT_REASON_TRIPLE_FAULT, 0, 0);
4604}
4605
Sean Christopherson55d23752018-12-03 13:53:18 -08004606/*
4607 * Decode the memory-address operand of a vmx instruction, as recorded on an
4608 * exit caused by such an instruction (run by a guest hypervisor).
4609 * On success, returns 0. When the operand is invalid, returns 1 and throws
Miaohe Lin49f933d2020-02-27 11:20:54 +08004610 * #UD, #GP, or #SS.
Sean Christopherson55d23752018-12-03 13:53:18 -08004611 */
4612int get_vmx_mem_address(struct kvm_vcpu *vcpu, unsigned long exit_qualification,
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004613 u32 vmx_instruction_info, bool wr, int len, gva_t *ret)
Sean Christopherson55d23752018-12-03 13:53:18 -08004614{
4615 gva_t off;
4616 bool exn;
4617 struct kvm_segment s;
4618
4619 /*
4620 * According to Vol. 3B, "Information for VM Exits Due to Instruction
4621 * Execution", on an exit, vmx_instruction_info holds most of the
4622 * addressing components of the operand. Only the displacement part
4623 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
4624 * For how an actual address is calculated from all these components,
4625 * refer to Vol. 1, "Operand Addressing".
4626 */
4627 int scaling = vmx_instruction_info & 3;
4628 int addr_size = (vmx_instruction_info >> 7) & 7;
4629 bool is_reg = vmx_instruction_info & (1u << 10);
4630 int seg_reg = (vmx_instruction_info >> 15) & 7;
4631 int index_reg = (vmx_instruction_info >> 18) & 0xf;
4632 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
4633 int base_reg = (vmx_instruction_info >> 23) & 0xf;
4634 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
4635
4636 if (is_reg) {
4637 kvm_queue_exception(vcpu, UD_VECTOR);
4638 return 1;
4639 }
4640
4641 /* Addr = segment_base + offset */
4642 /* offset = base + [index * scale] + displacement */
4643 off = exit_qualification; /* holds the displacement */
Sean Christopherson946c5222019-01-23 14:39:23 -08004644 if (addr_size == 1)
4645 off = (gva_t)sign_extend64(off, 31);
4646 else if (addr_size == 0)
4647 off = (gva_t)sign_extend64(off, 15);
Sean Christopherson55d23752018-12-03 13:53:18 -08004648 if (base_is_valid)
4649 off += kvm_register_read(vcpu, base_reg);
4650 if (index_is_valid)
Miaohe Line6302692020-02-15 10:44:22 +08004651 off += kvm_register_read(vcpu, index_reg) << scaling;
Sean Christopherson55d23752018-12-03 13:53:18 -08004652 vmx_get_segment(vcpu, &s, seg_reg);
Sean Christopherson55d23752018-12-03 13:53:18 -08004653
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004654 /*
4655 * The effective address, i.e. @off, of a memory operand is truncated
4656 * based on the address size of the instruction. Note that this is
4657 * the *effective address*, i.e. the address prior to accounting for
4658 * the segment's base.
4659 */
Sean Christopherson55d23752018-12-03 13:53:18 -08004660 if (addr_size == 1) /* 32 bit */
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004661 off &= 0xffffffff;
4662 else if (addr_size == 0) /* 16 bit */
4663 off &= 0xffff;
Sean Christopherson55d23752018-12-03 13:53:18 -08004664
4665 /* Checks for #GP/#SS exceptions. */
4666 exn = false;
4667 if (is_long_mode(vcpu)) {
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004668 /*
4669 * The virtual/linear address is never truncated in 64-bit
4670 * mode, e.g. a 32-bit address size can yield a 64-bit virtual
4671 * address when using FS/GS with a non-zero base.
4672 */
Liran Alon6694e482019-07-15 18:47:44 +03004673 if (seg_reg == VCPU_SREG_FS || seg_reg == VCPU_SREG_GS)
4674 *ret = s.base + off;
4675 else
4676 *ret = off;
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004677
Sean Christopherson55d23752018-12-03 13:53:18 -08004678 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
4679 * non-canonical form. This is the only check on the memory
4680 * destination for long mode!
4681 */
4682 exn = is_noncanonical_address(*ret, vcpu);
Paolo Bonzinie0dfacb2019-01-30 17:25:38 +01004683 } else {
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004684 /*
4685 * When not in long mode, the virtual/linear address is
4686 * unconditionally truncated to 32 bits regardless of the
4687 * address size.
4688 */
4689 *ret = (s.base + off) & 0xffffffff;
4690
Sean Christopherson55d23752018-12-03 13:53:18 -08004691 /* Protected mode: apply checks for segment validity in the
4692 * following order:
4693 * - segment type check (#GP(0) may be thrown)
4694 * - usability check (#GP(0)/#SS(0))
4695 * - limit check (#GP(0)/#SS(0))
4696 */
4697 if (wr)
4698 /* #GP(0) if the destination operand is located in a
4699 * read-only data segment or any code segment.
4700 */
4701 exn = ((s.type & 0xa) == 0 || (s.type & 8));
4702 else
4703 /* #GP(0) if the source operand is located in an
4704 * execute-only code segment
4705 */
4706 exn = ((s.type & 0xa) == 8);
4707 if (exn) {
4708 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
4709 return 1;
4710 }
4711 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
4712 */
4713 exn = (s.unusable != 0);
Sean Christopherson34333cc2019-01-23 14:39:25 -08004714
4715 /*
4716 * Protected mode: #GP(0)/#SS(0) if the memory operand is
4717 * outside the segment limit. All CPUs that support VMX ignore
4718 * limit checks for flat segments, i.e. segments with base==0,
4719 * limit==0xffffffff and of type expand-up data or code.
Sean Christopherson55d23752018-12-03 13:53:18 -08004720 */
Sean Christopherson34333cc2019-01-23 14:39:25 -08004721 if (!(s.base == 0 && s.limit == 0xffffffff &&
4722 ((s.type & 8) || !(s.type & 4))))
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004723 exn = exn || ((u64)off + len - 1 > s.limit);
Sean Christopherson55d23752018-12-03 13:53:18 -08004724 }
4725 if (exn) {
4726 kvm_queue_exception_e(vcpu,
4727 seg_reg == VCPU_SREG_SS ?
4728 SS_VECTOR : GP_VECTOR,
4729 0);
4730 return 1;
4731 }
4732
4733 return 0;
4734}
4735
Oliver Upton03a8871a2019-11-13 16:17:20 -08004736void nested_vmx_pmu_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
4737{
4738 struct vcpu_vmx *vmx;
4739
4740 if (!nested_vmx_allowed(vcpu))
4741 return;
4742
4743 vmx = to_vmx(vcpu);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004744 if (kvm_x86_ops.pmu_ops->is_valid_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL)) {
Oliver Upton03a8871a2019-11-13 16:17:20 -08004745 vmx->nested.msrs.entry_ctls_high |=
4746 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
4747 vmx->nested.msrs.exit_ctls_high |=
4748 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
4749 } else {
4750 vmx->nested.msrs.entry_ctls_high &=
4751 ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
4752 vmx->nested.msrs.exit_ctls_high &=
Chenyi Qiangc6b177a2020-08-28 16:56:21 +08004753 ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
Oliver Upton03a8871a2019-11-13 16:17:20 -08004754 }
4755}
4756
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004757static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer,
4758 int *ret)
Sean Christopherson55d23752018-12-03 13:53:18 -08004759{
4760 gva_t gva;
4761 struct x86_exception e;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004762 int r;
Sean Christopherson55d23752018-12-03 13:53:18 -08004763
Sean Christopherson5addc232020-04-15 13:34:53 -07004764 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004765 vmcs_read32(VMX_INSTRUCTION_INFO), false,
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004766 sizeof(*vmpointer), &gva)) {
4767 *ret = 1;
4768 return -EINVAL;
4769 }
Sean Christopherson55d23752018-12-03 13:53:18 -08004770
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004771 r = kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e);
4772 if (r != X86EMUL_CONTINUE) {
Babu Moger3f3393b2020-09-11 14:29:05 -05004773 *ret = kvm_handle_memory_failure(vcpu, r, &e);
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004774 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08004775 }
4776
4777 return 0;
4778}
4779
4780/*
4781 * Allocate a shadow VMCS and associate it with the currently loaded
4782 * VMCS, unless such a shadow VMCS already exists. The newly allocated
4783 * VMCS is also VMCLEARed, so that it is ready for use.
4784 */
4785static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
4786{
4787 struct vcpu_vmx *vmx = to_vmx(vcpu);
4788 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
4789
4790 /*
4791 * We should allocate a shadow vmcs for vmcs01 only when L1
4792 * executes VMXON and free it when L1 executes VMXOFF.
4793 * As it is invalid to execute VMXON twice, we shouldn't reach
4794 * here when vmcs01 already have an allocated shadow vmcs.
4795 */
4796 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
4797
4798 if (!loaded_vmcs->shadow_vmcs) {
4799 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
4800 if (loaded_vmcs->shadow_vmcs)
4801 vmcs_clear(loaded_vmcs->shadow_vmcs);
4802 }
4803 return loaded_vmcs->shadow_vmcs;
4804}
4805
4806static int enter_vmx_operation(struct kvm_vcpu *vcpu)
4807{
4808 struct vcpu_vmx *vmx = to_vmx(vcpu);
4809 int r;
4810
4811 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
4812 if (r < 0)
4813 goto out_vmcs02;
4814
Ben Gardon41836832019-02-11 11:02:52 -08004815 vmx->nested.cached_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT);
Sean Christopherson55d23752018-12-03 13:53:18 -08004816 if (!vmx->nested.cached_vmcs12)
4817 goto out_cached_vmcs12;
4818
Ben Gardon41836832019-02-11 11:02:52 -08004819 vmx->nested.cached_shadow_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT);
Sean Christopherson55d23752018-12-03 13:53:18 -08004820 if (!vmx->nested.cached_shadow_vmcs12)
4821 goto out_cached_shadow_vmcs12;
4822
4823 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
4824 goto out_shadow_vmcs;
4825
4826 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
Jim Mattsonada00982020-05-08 13:36:42 -07004827 HRTIMER_MODE_ABS_PINNED);
Sean Christopherson55d23752018-12-03 13:53:18 -08004828 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
4829
4830 vmx->nested.vpid02 = allocate_vpid();
4831
4832 vmx->nested.vmcs02_initialized = false;
4833 vmx->nested.vmxon = true;
Luwei Kangee85dec2018-10-24 16:05:16 +08004834
Sean Christopherson2ef76192020-03-02 15:56:22 -08004835 if (vmx_pt_mode_is_host_guest()) {
Luwei Kangee85dec2018-10-24 16:05:16 +08004836 vmx->pt_desc.guest.ctl = 0;
Aaron Lewis476c9bd2020-09-25 16:34:18 +02004837 pt_update_intercept_for_msr(vcpu);
Luwei Kangee85dec2018-10-24 16:05:16 +08004838 }
4839
Sean Christopherson55d23752018-12-03 13:53:18 -08004840 return 0;
4841
4842out_shadow_vmcs:
4843 kfree(vmx->nested.cached_shadow_vmcs12);
4844
4845out_cached_shadow_vmcs12:
4846 kfree(vmx->nested.cached_vmcs12);
4847
4848out_cached_vmcs12:
4849 free_loaded_vmcs(&vmx->nested.vmcs02);
4850
4851out_vmcs02:
4852 return -ENOMEM;
4853}
4854
4855/*
4856 * Emulate the VMXON instruction.
4857 * Currently, we just remember that VMX is active, and do not save or even
4858 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
4859 * do not currently need to store anything in that guest-allocated memory
4860 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
4861 * argument is different from the VMXON pointer (which the spec says they do).
4862 */
4863static int handle_vmon(struct kvm_vcpu *vcpu)
4864{
4865 int ret;
4866 gpa_t vmptr;
KarimAllah Ahmed2e408932019-01-31 21:24:31 +01004867 uint32_t revision;
Sean Christopherson55d23752018-12-03 13:53:18 -08004868 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson32ad73d2019-12-20 20:44:55 -08004869 const u64 VMXON_NEEDED_FEATURES = FEAT_CTL_LOCKED
4870 | FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX;
Sean Christopherson55d23752018-12-03 13:53:18 -08004871
4872 /*
4873 * The Intel VMX Instruction Reference lists a bunch of bits that are
4874 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
Sean Christophersonc2fe3cd2020-10-06 18:44:15 -07004875 * 1 (see vmx_is_valid_cr4() for when we allow the guest to set this).
Sean Christopherson55d23752018-12-03 13:53:18 -08004876 * Otherwise, we should fail with #UD. But most faulting conditions
4877 * have already been checked by hardware, prior to the VM-exit for
4878 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
4879 * that bit set to 1 in non-root mode.
4880 */
4881 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
4882 kvm_queue_exception(vcpu, UD_VECTOR);
4883 return 1;
4884 }
4885
4886 /* CPL=0 must be checked manually. */
4887 if (vmx_get_cpl(vcpu)) {
4888 kvm_inject_gp(vcpu, 0);
4889 return 1;
4890 }
4891
4892 if (vmx->nested.vmxon)
Sean Christophersonb2656e42020-06-08 18:56:07 -07004893 return nested_vmx_fail(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Sean Christopherson55d23752018-12-03 13:53:18 -08004894
4895 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
4896 != VMXON_NEEDED_FEATURES) {
4897 kvm_inject_gp(vcpu, 0);
4898 return 1;
4899 }
4900
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004901 if (nested_vmx_get_vmptr(vcpu, &vmptr, &ret))
4902 return ret;
Sean Christopherson55d23752018-12-03 13:53:18 -08004903
4904 /*
4905 * SDM 3: 24.11.5
4906 * The first 4 bytes of VMXON region contain the supported
4907 * VMCS revision identifier
4908 *
4909 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
4910 * which replaces physical address width with 32
4911 */
KarimAllah Ahmede0bf2662019-01-31 21:24:43 +01004912 if (!page_address_valid(vcpu, vmptr))
Sean Christopherson55d23752018-12-03 13:53:18 -08004913 return nested_vmx_failInvalid(vcpu);
4914
KarimAllah Ahmed2e408932019-01-31 21:24:31 +01004915 if (kvm_read_guest(vcpu->kvm, vmptr, &revision, sizeof(revision)) ||
4916 revision != VMCS12_REVISION)
Sean Christopherson55d23752018-12-03 13:53:18 -08004917 return nested_vmx_failInvalid(vcpu);
4918
Sean Christopherson55d23752018-12-03 13:53:18 -08004919 vmx->nested.vmxon_ptr = vmptr;
4920 ret = enter_vmx_operation(vcpu);
4921 if (ret)
4922 return ret;
4923
4924 return nested_vmx_succeed(vcpu);
4925}
4926
4927static inline void nested_release_vmcs12(struct kvm_vcpu *vcpu)
4928{
4929 struct vcpu_vmx *vmx = to_vmx(vcpu);
4930
4931 if (vmx->nested.current_vmptr == -1ull)
4932 return;
4933
Sean Christopherson7952d762019-05-07 08:36:29 -07004934 copy_vmcs02_to_vmcs12_rare(vcpu, get_vmcs12(vcpu));
4935
Sean Christopherson55d23752018-12-03 13:53:18 -08004936 if (enable_shadow_vmcs) {
4937 /* copy to memory all shadowed fields in case
4938 they were modified */
4939 copy_shadow_to_vmcs12(vmx);
Sean Christopherson55d23752018-12-03 13:53:18 -08004940 vmx_disable_shadow_vmcs(vmx);
4941 }
4942 vmx->nested.posted_intr_nv = -1;
4943
4944 /* Flush VMCS12 to guest memory */
4945 kvm_vcpu_write_guest_page(vcpu,
4946 vmx->nested.current_vmptr >> PAGE_SHIFT,
4947 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
4948
4949 kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
4950
4951 vmx->nested.current_vmptr = -1ull;
4952}
4953
4954/* Emulate the VMXOFF instruction */
4955static int handle_vmoff(struct kvm_vcpu *vcpu)
4956{
4957 if (!nested_vmx_check_permission(vcpu))
4958 return 1;
Liran Alon4b9852f2019-08-26 13:24:49 +03004959
Sean Christopherson55d23752018-12-03 13:53:18 -08004960 free_nested(vcpu);
Liran Alon4b9852f2019-08-26 13:24:49 +03004961
4962 /* Process a latched INIT during time CPU was in VMX operation */
4963 kvm_make_request(KVM_REQ_EVENT, vcpu);
4964
Sean Christopherson55d23752018-12-03 13:53:18 -08004965 return nested_vmx_succeed(vcpu);
4966}
4967
4968/* Emulate the VMCLEAR instruction */
4969static int handle_vmclear(struct kvm_vcpu *vcpu)
4970{
4971 struct vcpu_vmx *vmx = to_vmx(vcpu);
4972 u32 zero = 0;
4973 gpa_t vmptr;
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02004974 u64 evmcs_gpa;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004975 int r;
Sean Christopherson55d23752018-12-03 13:53:18 -08004976
4977 if (!nested_vmx_check_permission(vcpu))
4978 return 1;
4979
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004980 if (nested_vmx_get_vmptr(vcpu, &vmptr, &r))
4981 return r;
Sean Christopherson55d23752018-12-03 13:53:18 -08004982
KarimAllah Ahmede0bf2662019-01-31 21:24:43 +01004983 if (!page_address_valid(vcpu, vmptr))
Sean Christophersonb2656e42020-06-08 18:56:07 -07004984 return nested_vmx_fail(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
Sean Christopherson55d23752018-12-03 13:53:18 -08004985
4986 if (vmptr == vmx->nested.vmxon_ptr)
Sean Christophersonb2656e42020-06-08 18:56:07 -07004987 return nested_vmx_fail(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
Sean Christopherson55d23752018-12-03 13:53:18 -08004988
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02004989 /*
4990 * When Enlightened VMEntry is enabled on the calling CPU we treat
4991 * memory area pointer by vmptr as Enlightened VMCS (as there's no good
4992 * way to distinguish it from VMCS12) and we must not corrupt it by
4993 * writing to the non-existent 'launch_state' field. The area doesn't
4994 * have to be the currently active EVMCS on the calling CPU and there's
4995 * nothing KVM has to do to transition it from 'active' to 'non-active'
4996 * state. It is possible that the area will stay mapped as
4997 * vmx->nested.hv_evmcs but this shouldn't be a problem.
4998 */
4999 if (likely(!vmx->nested.enlightened_vmcs_enabled ||
5000 !nested_enlightened_vmentry(vcpu, &evmcs_gpa))) {
Sean Christopherson55d23752018-12-03 13:53:18 -08005001 if (vmptr == vmx->nested.current_vmptr)
5002 nested_release_vmcs12(vcpu);
5003
5004 kvm_vcpu_write_guest(vcpu,
5005 vmptr + offsetof(struct vmcs12,
5006 launch_state),
5007 &zero, sizeof(zero));
Vitaly Kuznetsov3b19b812021-05-26 15:20:21 +02005008 } else if (vmx->nested.hv_evmcs && vmptr == vmx->nested.hv_evmcs_vmptr) {
5009 nested_release_evmcs(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005010 }
5011
5012 return nested_vmx_succeed(vcpu);
5013}
5014
Sean Christopherson55d23752018-12-03 13:53:18 -08005015/* Emulate the VMLAUNCH instruction */
5016static int handle_vmlaunch(struct kvm_vcpu *vcpu)
5017{
5018 return nested_vmx_run(vcpu, true);
5019}
5020
5021/* Emulate the VMRESUME instruction */
5022static int handle_vmresume(struct kvm_vcpu *vcpu)
5023{
5024
5025 return nested_vmx_run(vcpu, false);
5026}
5027
5028static int handle_vmread(struct kvm_vcpu *vcpu)
5029{
Jim Mattsondd2d6042019-12-06 15:46:35 -08005030 struct vmcs12 *vmcs12 = is_guest_mode(vcpu) ? get_shadow_vmcs12(vcpu)
5031 : get_vmcs12(vcpu);
Sean Christopherson5addc232020-04-15 13:34:53 -07005032 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005033 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5034 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzinif7eea632019-09-14 00:26:27 +02005035 struct x86_exception e;
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005036 unsigned long field;
5037 u64 value;
5038 gva_t gva = 0;
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07005039 short offset;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005040 int len, r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005041
5042 if (!nested_vmx_check_permission(vcpu))
5043 return 1;
5044
Jim Mattsondd2d6042019-12-06 15:46:35 -08005045 /*
5046 * In VMX non-root operation, when the VMCS-link pointer is -1ull,
5047 * any VMREAD sets the ALU flags for VMfailInvalid.
5048 */
5049 if (vmx->nested.current_vmptr == -1ull ||
5050 (is_guest_mode(vcpu) &&
5051 get_vmcs12(vcpu)->vmcs_link_pointer == -1ull))
Sean Christopherson55d23752018-12-03 13:53:18 -08005052 return nested_vmx_failInvalid(vcpu);
5053
Sean Christopherson55d23752018-12-03 13:53:18 -08005054 /* Decode instruction info and find the field to read */
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005055 field = kvm_register_read(vcpu, (((instr_info) >> 28) & 0xf));
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07005056
5057 offset = vmcs_field_to_offset(field);
5058 if (offset < 0)
Sean Christophersonb2656e42020-06-08 18:56:07 -07005059 return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Sean Christopherson55d23752018-12-03 13:53:18 -08005060
Sean Christopherson7952d762019-05-07 08:36:29 -07005061 if (!is_guest_mode(vcpu) && is_vmcs12_ext_field(field))
5062 copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
5063
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005064 /* Read the field, zero-extended to a u64 value */
5065 value = vmcs12_read_any(vmcs12, field, offset);
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07005066
Sean Christopherson55d23752018-12-03 13:53:18 -08005067 /*
5068 * Now copy part of this value to register or memory, as requested.
5069 * Note that the number of bits actually copied is 32 or 64 depending
5070 * on the guest's mode (32 or 64 bit), not on the given field's length.
5071 */
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005072 if (instr_info & BIT(10)) {
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005073 kvm_register_write(vcpu, (((instr_info) >> 3) & 0xf), value);
Sean Christopherson55d23752018-12-03 13:53:18 -08005074 } else {
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005075 len = is_64_bit_mode(vcpu) ? 8 : 4;
Sean Christopherson55d23752018-12-03 13:53:18 -08005076 if (get_vmx_mem_address(vcpu, exit_qualification,
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005077 instr_info, true, len, &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08005078 return 1;
5079 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005080 r = kvm_write_guest_virt_system(vcpu, gva, &value, len, &e);
5081 if (r != X86EMUL_CONTINUE)
Babu Moger3f3393b2020-09-11 14:29:05 -05005082 return kvm_handle_memory_failure(vcpu, r, &e);
Sean Christopherson55d23752018-12-03 13:53:18 -08005083 }
5084
5085 return nested_vmx_succeed(vcpu);
5086}
5087
Sean Christophersone2174292019-05-07 08:36:28 -07005088static bool is_shadow_field_rw(unsigned long field)
5089{
5090 switch (field) {
5091#define SHADOW_FIELD_RW(x, y) case x:
5092#include "vmcs_shadow_fields.h"
5093 return true;
5094 default:
5095 break;
5096 }
5097 return false;
5098}
5099
5100static bool is_shadow_field_ro(unsigned long field)
5101{
5102 switch (field) {
5103#define SHADOW_FIELD_RO(x, y) case x:
5104#include "vmcs_shadow_fields.h"
5105 return true;
5106 default:
5107 break;
5108 }
5109 return false;
5110}
Sean Christopherson55d23752018-12-03 13:53:18 -08005111
5112static int handle_vmwrite(struct kvm_vcpu *vcpu)
5113{
Jim Mattsondd2d6042019-12-06 15:46:35 -08005114 struct vmcs12 *vmcs12 = is_guest_mode(vcpu) ? get_shadow_vmcs12(vcpu)
5115 : get_vmcs12(vcpu);
Sean Christopherson5addc232020-04-15 13:34:53 -07005116 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005117 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5118 struct vcpu_vmx *vmx = to_vmx(vcpu);
5119 struct x86_exception e;
5120 unsigned long field;
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07005121 short offset;
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005122 gva_t gva;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005123 int len, r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005124
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005125 /*
5126 * The value to write might be 32 or 64 bits, depending on L1's long
Sean Christopherson55d23752018-12-03 13:53:18 -08005127 * mode, and eventually we need to write that into a field of several
5128 * possible lengths. The code below first zero-extends the value to 64
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005129 * bit (value), and then copies only the appropriate number of
Sean Christopherson55d23752018-12-03 13:53:18 -08005130 * bits into the vmcs12 field.
5131 */
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005132 u64 value = 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08005133
5134 if (!nested_vmx_check_permission(vcpu))
5135 return 1;
5136
Jim Mattsondd2d6042019-12-06 15:46:35 -08005137 /*
5138 * In VMX non-root operation, when the VMCS-link pointer is -1ull,
5139 * any VMWRITE sets the ALU flags for VMfailInvalid.
5140 */
5141 if (vmx->nested.current_vmptr == -1ull ||
5142 (is_guest_mode(vcpu) &&
5143 get_vmcs12(vcpu)->vmcs_link_pointer == -1ull))
Sean Christopherson55d23752018-12-03 13:53:18 -08005144 return nested_vmx_failInvalid(vcpu);
5145
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005146 if (instr_info & BIT(10))
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005147 value = kvm_register_read(vcpu, (((instr_info) >> 3) & 0xf));
Sean Christopherson55d23752018-12-03 13:53:18 -08005148 else {
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005149 len = is_64_bit_mode(vcpu) ? 8 : 4;
Sean Christopherson55d23752018-12-03 13:53:18 -08005150 if (get_vmx_mem_address(vcpu, exit_qualification,
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005151 instr_info, false, len, &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08005152 return 1;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005153 r = kvm_read_guest_virt(vcpu, gva, &value, len, &e);
5154 if (r != X86EMUL_CONTINUE)
Babu Moger3f3393b2020-09-11 14:29:05 -05005155 return kvm_handle_memory_failure(vcpu, r, &e);
Sean Christopherson55d23752018-12-03 13:53:18 -08005156 }
5157
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005158 field = kvm_register_read(vcpu, (((instr_info) >> 28) & 0xf));
Sean Christopherson55d23752018-12-03 13:53:18 -08005159
Jim Mattson693e02c2019-12-06 15:46:36 -08005160 offset = vmcs_field_to_offset(field);
5161 if (offset < 0)
Sean Christophersonb2656e42020-06-08 18:56:07 -07005162 return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Jim Mattson693e02c2019-12-06 15:46:36 -08005163
Sean Christopherson55d23752018-12-03 13:53:18 -08005164 /*
5165 * If the vCPU supports "VMWRITE to any supported field in the
5166 * VMCS," then the "read-only" fields are actually read/write.
5167 */
5168 if (vmcs_field_readonly(field) &&
5169 !nested_cpu_has_vmwrite_any_field(vcpu))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005170 return nested_vmx_fail(vcpu, VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Sean Christopherson55d23752018-12-03 13:53:18 -08005171
Jim Mattsondd2d6042019-12-06 15:46:35 -08005172 /*
5173 * Ensure vmcs12 is up-to-date before any VMWRITE that dirties
5174 * vmcs12, else we may crush a field or consume a stale value.
5175 */
5176 if (!is_guest_mode(vcpu) && !is_shadow_field_rw(field))
5177 copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08005178
5179 /*
Sean Christophersonb6437802019-05-07 08:36:24 -07005180 * Some Intel CPUs intentionally drop the reserved bits of the AR byte
5181 * fields on VMWRITE. Emulate this behavior to ensure consistent KVM
5182 * behavior regardless of the underlying hardware, e.g. if an AR_BYTE
5183 * field is intercepted for VMWRITE but not VMREAD (in L1), then VMREAD
5184 * from L1 will return a different value than VMREAD from L2 (L1 sees
5185 * the stripped down value, L2 sees the full value as stored by KVM).
Sean Christopherson55d23752018-12-03 13:53:18 -08005186 */
Sean Christophersonb6437802019-05-07 08:36:24 -07005187 if (field >= GUEST_ES_AR_BYTES && field <= GUEST_TR_AR_BYTES)
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005188 value &= 0x1f0ff;
Sean Christophersonb6437802019-05-07 08:36:24 -07005189
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005190 vmcs12_write_any(vmcs12, field, offset, value);
Sean Christopherson55d23752018-12-03 13:53:18 -08005191
5192 /*
Sean Christophersone2174292019-05-07 08:36:28 -07005193 * Do not track vmcs12 dirty-state if in guest-mode as we actually
5194 * dirty shadow vmcs12 instead of vmcs12. Fields that can be updated
5195 * by L1 without a vmexit are always updated in the vmcs02, i.e. don't
5196 * "dirty" vmcs12, all others go down the prepare_vmcs02() slow path.
Sean Christopherson55d23752018-12-03 13:53:18 -08005197 */
Sean Christophersone2174292019-05-07 08:36:28 -07005198 if (!is_guest_mode(vcpu) && !is_shadow_field_rw(field)) {
5199 /*
5200 * L1 can read these fields without exiting, ensure the
5201 * shadow VMCS is up-to-date.
5202 */
5203 if (enable_shadow_vmcs && is_shadow_field_ro(field)) {
5204 preempt_disable();
5205 vmcs_load(vmx->vmcs01.shadow_vmcs);
Sean Christophersonfadcead2019-05-07 08:36:23 -07005206
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005207 __vmcs_writel(field, value);
Sean Christophersonfadcead2019-05-07 08:36:23 -07005208
Sean Christophersone2174292019-05-07 08:36:28 -07005209 vmcs_clear(vmx->vmcs01.shadow_vmcs);
5210 vmcs_load(vmx->loaded_vmcs->vmcs);
5211 preempt_enable();
Sean Christopherson55d23752018-12-03 13:53:18 -08005212 }
Sean Christophersone2174292019-05-07 08:36:28 -07005213 vmx->nested.dirty_vmcs12 = true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005214 }
5215
5216 return nested_vmx_succeed(vcpu);
5217}
5218
5219static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
5220{
5221 vmx->nested.current_vmptr = vmptr;
5222 if (enable_shadow_vmcs) {
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07005223 secondary_exec_controls_setbit(vmx, SECONDARY_EXEC_SHADOW_VMCS);
Sean Christopherson55d23752018-12-03 13:53:18 -08005224 vmcs_write64(VMCS_LINK_POINTER,
5225 __pa(vmx->vmcs01.shadow_vmcs));
Sean Christopherson3731905ef2019-05-07 08:36:27 -07005226 vmx->nested.need_vmcs12_to_shadow_sync = true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005227 }
5228 vmx->nested.dirty_vmcs12 = true;
5229}
5230
5231/* Emulate the VMPTRLD instruction */
5232static int handle_vmptrld(struct kvm_vcpu *vcpu)
5233{
5234 struct vcpu_vmx *vmx = to_vmx(vcpu);
5235 gpa_t vmptr;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005236 int r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005237
5238 if (!nested_vmx_check_permission(vcpu))
5239 return 1;
5240
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005241 if (nested_vmx_get_vmptr(vcpu, &vmptr, &r))
5242 return r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005243
KarimAllah Ahmede0bf2662019-01-31 21:24:43 +01005244 if (!page_address_valid(vcpu, vmptr))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005245 return nested_vmx_fail(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
Sean Christopherson55d23752018-12-03 13:53:18 -08005246
5247 if (vmptr == vmx->nested.vmxon_ptr)
Sean Christophersonb2656e42020-06-08 18:56:07 -07005248 return nested_vmx_fail(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
Sean Christopherson55d23752018-12-03 13:53:18 -08005249
5250 /* Forbid normal VMPTRLD if Enlightened version was used */
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02005251 if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
Sean Christopherson55d23752018-12-03 13:53:18 -08005252 return 1;
5253
5254 if (vmx->nested.current_vmptr != vmptr) {
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01005255 struct kvm_host_map map;
Sean Christopherson55d23752018-12-03 13:53:18 -08005256 struct vmcs12 *new_vmcs12;
Sean Christopherson55d23752018-12-03 13:53:18 -08005257
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01005258 if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmptr), &map)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08005259 /*
5260 * Reads from an unbacked page return all 1s,
5261 * which means that the 32 bits located at the
5262 * given physical address won't match the required
5263 * VMCS12_REVISION identifier.
5264 */
Sean Christophersonb2656e42020-06-08 18:56:07 -07005265 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005266 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Sean Christopherson55d23752018-12-03 13:53:18 -08005267 }
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01005268
5269 new_vmcs12 = map.hva;
5270
Sean Christopherson55d23752018-12-03 13:53:18 -08005271 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
5272 (new_vmcs12->hdr.shadow_vmcs &&
5273 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01005274 kvm_vcpu_unmap(vcpu, &map, false);
Sean Christophersonb2656e42020-06-08 18:56:07 -07005275 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005276 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
5277 }
5278
5279 nested_release_vmcs12(vcpu);
5280
5281 /*
5282 * Load VMCS12 from guest memory since it is not already
5283 * cached.
5284 */
5285 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01005286 kvm_vcpu_unmap(vcpu, &map, false);
Sean Christopherson55d23752018-12-03 13:53:18 -08005287
5288 set_current_vmptr(vmx, vmptr);
5289 }
5290
5291 return nested_vmx_succeed(vcpu);
5292}
5293
5294/* Emulate the VMPTRST instruction */
5295static int handle_vmptrst(struct kvm_vcpu *vcpu)
5296{
Sean Christopherson5addc232020-04-15 13:34:53 -07005297 unsigned long exit_qual = vmx_get_exit_qual(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005298 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5299 gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr;
5300 struct x86_exception e;
5301 gva_t gva;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005302 int r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005303
5304 if (!nested_vmx_check_permission(vcpu))
5305 return 1;
5306
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02005307 if (unlikely(evmptr_is_valid(to_vmx(vcpu)->nested.hv_evmcs_vmptr)))
Sean Christopherson55d23752018-12-03 13:53:18 -08005308 return 1;
5309
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005310 if (get_vmx_mem_address(vcpu, exit_qual, instr_info,
5311 true, sizeof(gpa_t), &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08005312 return 1;
5313 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005314 r = kvm_write_guest_virt_system(vcpu, gva, (void *)&current_vmptr,
5315 sizeof(gpa_t), &e);
5316 if (r != X86EMUL_CONTINUE)
Babu Moger3f3393b2020-09-11 14:29:05 -05005317 return kvm_handle_memory_failure(vcpu, r, &e);
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005318
Sean Christopherson55d23752018-12-03 13:53:18 -08005319 return nested_vmx_succeed(vcpu);
5320}
5321
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005322#define EPTP_PA_MASK GENMASK_ULL(51, 12)
5323
5324static bool nested_ept_root_matches(hpa_t root_hpa, u64 root_eptp, u64 eptp)
5325{
5326 return VALID_PAGE(root_hpa) &&
5327 ((root_eptp & EPTP_PA_MASK) == (eptp & EPTP_PA_MASK));
5328}
5329
Sean Christopherson55d23752018-12-03 13:53:18 -08005330/* Emulate the INVEPT instruction */
5331static int handle_invept(struct kvm_vcpu *vcpu)
5332{
5333 struct vcpu_vmx *vmx = to_vmx(vcpu);
5334 u32 vmx_instruction_info, types;
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005335 unsigned long type, roots_to_free;
5336 struct kvm_mmu *mmu;
Sean Christopherson55d23752018-12-03 13:53:18 -08005337 gva_t gva;
5338 struct x86_exception e;
5339 struct {
5340 u64 eptp, gpa;
5341 } operand;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005342 int i, r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005343
5344 if (!(vmx->nested.msrs.secondary_ctls_high &
5345 SECONDARY_EXEC_ENABLE_EPT) ||
5346 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
5347 kvm_queue_exception(vcpu, UD_VECTOR);
5348 return 1;
5349 }
5350
5351 if (!nested_vmx_check_permission(vcpu))
5352 return 1;
5353
5354 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005355 type = kvm_register_read(vcpu, (vmx_instruction_info >> 28) & 0xf);
Sean Christopherson55d23752018-12-03 13:53:18 -08005356
5357 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
5358
5359 if (type >= 32 || !(types & (1 << type)))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005360 return nested_vmx_fail(vcpu, VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Sean Christopherson55d23752018-12-03 13:53:18 -08005361
5362 /* According to the Intel VMX instruction reference, the memory
5363 * operand is read even if it isn't needed (e.g., for type==global)
5364 */
Sean Christopherson5addc232020-04-15 13:34:53 -07005365 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005366 vmx_instruction_info, false, sizeof(operand), &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08005367 return 1;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005368 r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e);
5369 if (r != X86EMUL_CONTINUE)
Babu Moger3f3393b2020-09-11 14:29:05 -05005370 return kvm_handle_memory_failure(vcpu, r, &e);
Sean Christopherson55d23752018-12-03 13:53:18 -08005371
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005372 /*
5373 * Nested EPT roots are always held through guest_mmu,
5374 * not root_mmu.
5375 */
5376 mmu = &vcpu->arch.guest_mmu;
5377
Sean Christopherson55d23752018-12-03 13:53:18 -08005378 switch (type) {
Sean Christopherson55d23752018-12-03 13:53:18 -08005379 case VMX_EPT_EXTENT_CONTEXT:
Sean Christophersoneed00302020-03-20 14:27:58 -07005380 if (!nested_vmx_check_eptp(vcpu, operand.eptp))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005381 return nested_vmx_fail(vcpu,
Sean Christophersoneed00302020-03-20 14:27:58 -07005382 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Sean Christophersonf8aa7e32020-03-20 14:27:59 -07005383
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005384 roots_to_free = 0;
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07005385 if (nested_ept_root_matches(mmu->root_hpa, mmu->root_pgd,
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005386 operand.eptp))
5387 roots_to_free |= KVM_MMU_ROOT_CURRENT;
5388
5389 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) {
5390 if (nested_ept_root_matches(mmu->prev_roots[i].hpa,
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07005391 mmu->prev_roots[i].pgd,
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005392 operand.eptp))
5393 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
5394 }
5395 break;
Sean Christophersoneed00302020-03-20 14:27:58 -07005396 case VMX_EPT_EXTENT_GLOBAL:
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005397 roots_to_free = KVM_MMU_ROOTS_ALL;
Sean Christopherson55d23752018-12-03 13:53:18 -08005398 break;
5399 default:
Sean Christophersonf9336e32020-05-04 08:35:06 -07005400 BUG();
Sean Christopherson55d23752018-12-03 13:53:18 -08005401 break;
5402 }
5403
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005404 if (roots_to_free)
5405 kvm_mmu_free_roots(vcpu, mmu, roots_to_free);
5406
Sean Christopherson55d23752018-12-03 13:53:18 -08005407 return nested_vmx_succeed(vcpu);
5408}
5409
5410static int handle_invvpid(struct kvm_vcpu *vcpu)
5411{
5412 struct vcpu_vmx *vmx = to_vmx(vcpu);
5413 u32 vmx_instruction_info;
5414 unsigned long type, types;
5415 gva_t gva;
5416 struct x86_exception e;
5417 struct {
5418 u64 vpid;
5419 u64 gla;
5420 } operand;
5421 u16 vpid02;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005422 int r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005423
5424 if (!(vmx->nested.msrs.secondary_ctls_high &
5425 SECONDARY_EXEC_ENABLE_VPID) ||
5426 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
5427 kvm_queue_exception(vcpu, UD_VECTOR);
5428 return 1;
5429 }
5430
5431 if (!nested_vmx_check_permission(vcpu))
5432 return 1;
5433
5434 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005435 type = kvm_register_read(vcpu, (vmx_instruction_info >> 28) & 0xf);
Sean Christopherson55d23752018-12-03 13:53:18 -08005436
5437 types = (vmx->nested.msrs.vpid_caps &
5438 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
5439
5440 if (type >= 32 || !(types & (1 << type)))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005441 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005442 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
5443
5444 /* according to the intel vmx instruction reference, the memory
5445 * operand is read even if it isn't needed (e.g., for type==global)
5446 */
Sean Christopherson5addc232020-04-15 13:34:53 -07005447 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005448 vmx_instruction_info, false, sizeof(operand), &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08005449 return 1;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005450 r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e);
5451 if (r != X86EMUL_CONTINUE)
Babu Moger3f3393b2020-09-11 14:29:05 -05005452 return kvm_handle_memory_failure(vcpu, r, &e);
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005453
Sean Christopherson55d23752018-12-03 13:53:18 -08005454 if (operand.vpid >> 16)
Sean Christophersonb2656e42020-06-08 18:56:07 -07005455 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005456 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
5457
5458 vpid02 = nested_get_vpid02(vcpu);
5459 switch (type) {
5460 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
5461 if (!operand.vpid ||
5462 is_noncanonical_address(operand.gla, vcpu))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005463 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005464 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Sean Christophersonbc41d0c2020-03-20 14:28:09 -07005465 vpid_sync_vcpu_addr(vpid02, operand.gla);
Sean Christopherson55d23752018-12-03 13:53:18 -08005466 break;
5467 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
5468 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
5469 if (!operand.vpid)
Sean Christophersonb2656e42020-06-08 18:56:07 -07005470 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005471 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Sean Christopherson446ace42020-03-20 14:28:05 -07005472 vpid_sync_context(vpid02);
Sean Christopherson55d23752018-12-03 13:53:18 -08005473 break;
5474 case VMX_VPID_EXTENT_ALL_CONTEXT:
Sean Christopherson446ace42020-03-20 14:28:05 -07005475 vpid_sync_context(vpid02);
Sean Christopherson55d23752018-12-03 13:53:18 -08005476 break;
5477 default:
5478 WARN_ON_ONCE(1);
5479 return kvm_skip_emulated_instruction(vcpu);
5480 }
5481
Junaid Shahidd6e3f832020-03-20 14:28:00 -07005482 /*
5483 * Sync the shadow page tables if EPT is disabled, L1 is invalidating
Sean Christopherson25b62c62021-06-09 16:42:29 -07005484 * linear mappings for L2 (tagged with L2's VPID). Free all guest
5485 * roots as VPIDs are not tracked in the MMU role.
Junaid Shahidd6e3f832020-03-20 14:28:00 -07005486 *
5487 * Note, this operates on root_mmu, not guest_mmu, as L1 and L2 share
5488 * an MMU when EPT is disabled.
5489 *
5490 * TODO: sync only the affected SPTEs for INVDIVIDUAL_ADDR.
5491 */
5492 if (!enable_ept)
Sean Christopherson25b62c62021-06-09 16:42:29 -07005493 kvm_mmu_free_guest_mode_roots(vcpu, &vcpu->arch.root_mmu);
Junaid Shahidd6e3f832020-03-20 14:28:00 -07005494
Sean Christopherson55d23752018-12-03 13:53:18 -08005495 return nested_vmx_succeed(vcpu);
5496}
5497
5498static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
5499 struct vmcs12 *vmcs12)
5500{
Sean Christopherson2b3eaf82019-04-30 10:36:19 -07005501 u32 index = kvm_rcx_read(vcpu);
Sean Christophersonac6389a2020-03-02 18:02:38 -08005502 u64 new_eptp;
Sean Christopherson55d23752018-12-03 13:53:18 -08005503
5504 if (!nested_cpu_has_eptp_switching(vmcs12) ||
5505 !nested_cpu_has_ept(vmcs12))
5506 return 1;
5507
5508 if (index >= VMFUNC_EPTP_ENTRIES)
5509 return 1;
5510
Sean Christopherson55d23752018-12-03 13:53:18 -08005511 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
Sean Christophersonac6389a2020-03-02 18:02:38 -08005512 &new_eptp, index * 8, 8))
Sean Christopherson55d23752018-12-03 13:53:18 -08005513 return 1;
5514
Sean Christopherson55d23752018-12-03 13:53:18 -08005515 /*
5516 * If the (L2) guest does a vmfunc to the currently
5517 * active ept pointer, we don't have to do anything else
5518 */
Sean Christophersonac6389a2020-03-02 18:02:38 -08005519 if (vmcs12->ept_pointer != new_eptp) {
5520 if (!nested_vmx_check_eptp(vcpu, new_eptp))
Sean Christopherson55d23752018-12-03 13:53:18 -08005521 return 1;
5522
Sean Christophersonac6389a2020-03-02 18:02:38 -08005523 vmcs12->ept_pointer = new_eptp;
Sean Christophersonc805f5d2021-03-04 17:10:57 -08005524
5525 kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005526 }
5527
5528 return 0;
5529}
5530
5531static int handle_vmfunc(struct kvm_vcpu *vcpu)
5532{
5533 struct vcpu_vmx *vmx = to_vmx(vcpu);
5534 struct vmcs12 *vmcs12;
Sean Christopherson2b3eaf82019-04-30 10:36:19 -07005535 u32 function = kvm_rax_read(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005536
5537 /*
5538 * VMFUNC is only supported for nested guests, but we always enable the
5539 * secondary control for simplicity; for non-nested mode, fake that we
5540 * didn't by injecting #UD.
5541 */
5542 if (!is_guest_mode(vcpu)) {
5543 kvm_queue_exception(vcpu, UD_VECTOR);
5544 return 1;
5545 }
5546
5547 vmcs12 = get_vmcs12(vcpu);
Sean Christopherson0e752252021-06-09 16:42:22 -07005548 if (!(vmcs12->vm_function_control & BIT_ULL(function)))
Sean Christopherson55d23752018-12-03 13:53:18 -08005549 goto fail;
5550
5551 switch (function) {
5552 case 0:
5553 if (nested_vmx_eptp_switching(vcpu, vmcs12))
5554 goto fail;
5555 break;
5556 default:
5557 goto fail;
5558 }
5559 return kvm_skip_emulated_instruction(vcpu);
5560
5561fail:
Sean Christopherson8e533242020-11-06 17:03:12 +08005562 /*
5563 * This is effectively a reflected VM-Exit, as opposed to a synthesized
5564 * nested VM-Exit. Pass the original exit reason, i.e. don't hardcode
5565 * EXIT_REASON_VMFUNC as the exit reason.
5566 */
5567 nested_vmx_vmexit(vcpu, vmx->exit_reason.full,
Sean Christopherson87915852020-04-15 13:34:54 -07005568 vmx_get_intr_info(vcpu),
Sean Christopherson5addc232020-04-15 13:34:53 -07005569 vmx_get_exit_qual(vcpu));
Sean Christopherson55d23752018-12-03 13:53:18 -08005570 return 1;
5571}
5572
Oliver Uptone71237d2020-02-04 15:26:30 -08005573/*
5574 * Return true if an IO instruction with the specified port and size should cause
5575 * a VM-exit into L1.
5576 */
5577bool nested_vmx_check_io_bitmaps(struct kvm_vcpu *vcpu, unsigned int port,
5578 int size)
Sean Christopherson55d23752018-12-03 13:53:18 -08005579{
Oliver Uptone71237d2020-02-04 15:26:30 -08005580 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005581 gpa_t bitmap, last_bitmap;
Sean Christopherson55d23752018-12-03 13:53:18 -08005582 u8 b;
5583
Sean Christopherson55d23752018-12-03 13:53:18 -08005584 last_bitmap = (gpa_t)-1;
5585 b = -1;
5586
5587 while (size > 0) {
5588 if (port < 0x8000)
5589 bitmap = vmcs12->io_bitmap_a;
5590 else if (port < 0x10000)
5591 bitmap = vmcs12->io_bitmap_b;
5592 else
5593 return true;
5594 bitmap += (port & 0x7fff) / 8;
5595
5596 if (last_bitmap != bitmap)
5597 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
5598 return true;
5599 if (b & (1 << (port & 7)))
5600 return true;
5601
5602 port++;
5603 size--;
5604 last_bitmap = bitmap;
5605 }
5606
5607 return false;
5608}
5609
Oliver Uptone71237d2020-02-04 15:26:30 -08005610static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
5611 struct vmcs12 *vmcs12)
5612{
5613 unsigned long exit_qualification;
Oliver Upton35a57132020-02-04 15:26:31 -08005614 unsigned short port;
Oliver Uptone71237d2020-02-04 15:26:30 -08005615 int size;
5616
5617 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
5618 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
5619
Sean Christopherson5addc232020-04-15 13:34:53 -07005620 exit_qualification = vmx_get_exit_qual(vcpu);
Oliver Uptone71237d2020-02-04 15:26:30 -08005621
5622 port = exit_qualification >> 16;
5623 size = (exit_qualification & 7) + 1;
5624
5625 return nested_vmx_check_io_bitmaps(vcpu, port, size);
5626}
5627
Sean Christopherson55d23752018-12-03 13:53:18 -08005628/*
Miaohe Lin463bfee2020-02-14 10:44:05 +08005629 * Return 1 if we should exit from L2 to L1 to handle an MSR access,
Sean Christopherson55d23752018-12-03 13:53:18 -08005630 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
5631 * disinterest in the current event (read or write a specific MSR) by using an
5632 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
5633 */
5634static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
Sean Christopherson8e533242020-11-06 17:03:12 +08005635 struct vmcs12 *vmcs12,
5636 union vmx_exit_reason exit_reason)
Sean Christopherson55d23752018-12-03 13:53:18 -08005637{
Sean Christopherson2b3eaf82019-04-30 10:36:19 -07005638 u32 msr_index = kvm_rcx_read(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005639 gpa_t bitmap;
5640
5641 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
5642 return true;
5643
5644 /*
5645 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
5646 * for the four combinations of read/write and low/high MSR numbers.
5647 * First we need to figure out which of the four to use:
5648 */
5649 bitmap = vmcs12->msr_bitmap;
Sean Christopherson8e533242020-11-06 17:03:12 +08005650 if (exit_reason.basic == EXIT_REASON_MSR_WRITE)
Sean Christopherson55d23752018-12-03 13:53:18 -08005651 bitmap += 2048;
5652 if (msr_index >= 0xc0000000) {
5653 msr_index -= 0xc0000000;
5654 bitmap += 1024;
5655 }
5656
5657 /* Then read the msr_index'th bit from this bitmap: */
5658 if (msr_index < 1024*8) {
5659 unsigned char b;
5660 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
5661 return true;
5662 return 1 & (b >> (msr_index & 7));
5663 } else
5664 return true; /* let L1 handle the wrong parameter */
5665}
5666
5667/*
5668 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
5669 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
5670 * intercept (via guest_host_mask etc.) the current event.
5671 */
5672static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
5673 struct vmcs12 *vmcs12)
5674{
Sean Christopherson5addc232020-04-15 13:34:53 -07005675 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005676 int cr = exit_qualification & 15;
5677 int reg;
5678 unsigned long val;
5679
5680 switch ((exit_qualification >> 4) & 3) {
5681 case 0: /* mov to cr */
5682 reg = (exit_qualification >> 8) & 15;
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005683 val = kvm_register_read(vcpu, reg);
Sean Christopherson55d23752018-12-03 13:53:18 -08005684 switch (cr) {
5685 case 0:
5686 if (vmcs12->cr0_guest_host_mask &
5687 (val ^ vmcs12->cr0_read_shadow))
5688 return true;
5689 break;
5690 case 3:
Sean Christopherson55d23752018-12-03 13:53:18 -08005691 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
5692 return true;
5693 break;
5694 case 4:
5695 if (vmcs12->cr4_guest_host_mask &
5696 (vmcs12->cr4_read_shadow ^ val))
5697 return true;
5698 break;
5699 case 8:
5700 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
5701 return true;
5702 break;
5703 }
5704 break;
5705 case 2: /* clts */
5706 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
5707 (vmcs12->cr0_read_shadow & X86_CR0_TS))
5708 return true;
5709 break;
5710 case 1: /* mov from cr */
5711 switch (cr) {
5712 case 3:
5713 if (vmcs12->cpu_based_vm_exec_control &
5714 CPU_BASED_CR3_STORE_EXITING)
5715 return true;
5716 break;
5717 case 8:
5718 if (vmcs12->cpu_based_vm_exec_control &
5719 CPU_BASED_CR8_STORE_EXITING)
5720 return true;
5721 break;
5722 }
5723 break;
5724 case 3: /* lmsw */
5725 /*
5726 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
5727 * cr0. Other attempted changes are ignored, with no exit.
5728 */
5729 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
5730 if (vmcs12->cr0_guest_host_mask & 0xe &
5731 (val ^ vmcs12->cr0_read_shadow))
5732 return true;
5733 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
5734 !(vmcs12->cr0_read_shadow & 0x1) &&
5735 (val & 0x1))
5736 return true;
5737 break;
5738 }
5739 return false;
5740}
5741
Sean Christopherson72add912021-04-12 16:21:42 +12005742static bool nested_vmx_exit_handled_encls(struct kvm_vcpu *vcpu,
5743 struct vmcs12 *vmcs12)
5744{
5745 u32 encls_leaf;
5746
5747 if (!guest_cpuid_has(vcpu, X86_FEATURE_SGX) ||
5748 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENCLS_EXITING))
5749 return false;
5750
5751 encls_leaf = kvm_rax_read(vcpu);
5752 if (encls_leaf > 62)
5753 encls_leaf = 63;
5754 return vmcs12->encls_exiting_bitmap & BIT_ULL(encls_leaf);
5755}
5756
Sean Christopherson55d23752018-12-03 13:53:18 -08005757static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
5758 struct vmcs12 *vmcs12, gpa_t bitmap)
5759{
5760 u32 vmx_instruction_info;
5761 unsigned long field;
5762 u8 b;
5763
5764 if (!nested_cpu_has_shadow_vmcs(vmcs12))
5765 return true;
5766
5767 /* Decode instruction info and find the field to access */
5768 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5769 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
5770
5771 /* Out-of-range fields always cause a VM exit from L2 to L1 */
5772 if (field >> 15)
5773 return true;
5774
5775 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
5776 return true;
5777
5778 return 1 & (b >> (field & 7));
5779}
5780
Oliver Uptonb045ae92020-04-14 22:47:45 +00005781static bool nested_vmx_exit_handled_mtf(struct vmcs12 *vmcs12)
5782{
5783 u32 entry_intr_info = vmcs12->vm_entry_intr_info_field;
5784
5785 if (nested_cpu_has_mtf(vmcs12))
5786 return true;
5787
5788 /*
5789 * An MTF VM-exit may be injected into the guest by setting the
5790 * interruption-type to 7 (other event) and the vector field to 0. Such
5791 * is the case regardless of the 'monitor trap flag' VM-execution
5792 * control.
5793 */
5794 return entry_intr_info == (INTR_INFO_VALID_MASK
5795 | INTR_TYPE_OTHER_EVENT);
5796}
5797
Sean Christopherson55d23752018-12-03 13:53:18 -08005798/*
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005799 * Return true if L0 wants to handle an exit from L2 regardless of whether or not
5800 * L1 wants the exit. Only call this when in is_guest_mode (L2).
Sean Christopherson55d23752018-12-03 13:53:18 -08005801 */
Sean Christopherson8e533242020-11-06 17:03:12 +08005802static bool nested_vmx_l0_wants_exit(struct kvm_vcpu *vcpu,
5803 union vmx_exit_reason exit_reason)
Sean Christopherson55d23752018-12-03 13:53:18 -08005804{
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005805 u32 intr_info;
5806
Sean Christopherson8e533242020-11-06 17:03:12 +08005807 switch ((u16)exit_reason.basic) {
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005808 case EXIT_REASON_EXCEPTION_NMI:
Sean Christopherson87915852020-04-15 13:34:54 -07005809 intr_info = vmx_get_intr_info(vcpu);
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005810 if (is_nmi(intr_info))
5811 return true;
5812 else if (is_page_fault(intr_info))
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +02005813 return vcpu->arch.apf.host_apf_flags || !enable_ept;
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005814 else if (is_debug(intr_info) &&
5815 vcpu->guest_debug &
5816 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5817 return true;
5818 else if (is_breakpoint(intr_info) &&
5819 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
5820 return true;
5821 return false;
5822 case EXIT_REASON_EXTERNAL_INTERRUPT:
5823 return true;
5824 case EXIT_REASON_MCE_DURING_VMENTRY:
5825 return true;
5826 case EXIT_REASON_EPT_VIOLATION:
5827 /*
5828 * L0 always deals with the EPT violation. If nested EPT is
5829 * used, and the nested mmu code discovers that the address is
5830 * missing in the guest EPT table (EPT12), the EPT violation
5831 * will be injected with nested_ept_inject_page_fault()
5832 */
5833 return true;
5834 case EXIT_REASON_EPT_MISCONFIG:
5835 /*
5836 * L2 never uses directly L1's EPT, but rather L0's own EPT
5837 * table (shadow on EPT) or a merged EPT table that L0 built
5838 * (EPT on EPT). So any problems with the structure of the
5839 * table is L0's fault.
5840 */
5841 return true;
5842 case EXIT_REASON_PREEMPTION_TIMER:
5843 return true;
5844 case EXIT_REASON_PML_FULL:
Sean Christophersonc3bb9a22021-02-12 16:50:07 -08005845 /*
5846 * PML is emulated for an L1 VMM and should never be enabled in
5847 * vmcs02, always "handle" PML_FULL by exiting to userspace.
5848 */
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005849 return true;
5850 case EXIT_REASON_VMFUNC:
5851 /* VM functions are emulated through L2->L0 vmexits. */
5852 return true;
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005853 default:
5854 break;
5855 }
5856 return false;
5857}
5858
5859/*
5860 * Return 1 if L1 wants to intercept an exit from L2. Only call this when in
5861 * is_guest_mode (L2).
5862 */
Sean Christopherson8e533242020-11-06 17:03:12 +08005863static bool nested_vmx_l1_wants_exit(struct kvm_vcpu *vcpu,
5864 union vmx_exit_reason exit_reason)
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005865{
Sean Christopherson55d23752018-12-03 13:53:18 -08005866 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Sean Christopherson9bd4af22020-04-21 00:53:27 -07005867 u32 intr_info;
Sean Christopherson55d23752018-12-03 13:53:18 -08005868
Sean Christopherson8e533242020-11-06 17:03:12 +08005869 switch ((u16)exit_reason.basic) {
Sean Christopherson55d23752018-12-03 13:53:18 -08005870 case EXIT_REASON_EXCEPTION_NMI:
Sean Christopherson87915852020-04-15 13:34:54 -07005871 intr_info = vmx_get_intr_info(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005872 if (is_nmi(intr_info))
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005873 return true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005874 else if (is_page_fault(intr_info))
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005875 return true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005876 return vmcs12->exception_bitmap &
5877 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
5878 case EXIT_REASON_EXTERNAL_INTERRUPT:
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005879 return nested_exit_on_intr(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005880 case EXIT_REASON_TRIPLE_FAULT:
5881 return true;
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08005882 case EXIT_REASON_INTERRUPT_WINDOW:
5883 return nested_cpu_has(vmcs12, CPU_BASED_INTR_WINDOW_EXITING);
Sean Christopherson55d23752018-12-03 13:53:18 -08005884 case EXIT_REASON_NMI_WINDOW:
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08005885 return nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING);
Sean Christopherson55d23752018-12-03 13:53:18 -08005886 case EXIT_REASON_TASK_SWITCH:
5887 return true;
5888 case EXIT_REASON_CPUID:
5889 return true;
5890 case EXIT_REASON_HLT:
5891 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
5892 case EXIT_REASON_INVD:
5893 return true;
5894 case EXIT_REASON_INVLPG:
5895 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
5896 case EXIT_REASON_RDPMC:
5897 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
5898 case EXIT_REASON_RDRAND:
5899 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
5900 case EXIT_REASON_RDSEED:
5901 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
5902 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
5903 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
5904 case EXIT_REASON_VMREAD:
5905 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
5906 vmcs12->vmread_bitmap);
5907 case EXIT_REASON_VMWRITE:
5908 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
5909 vmcs12->vmwrite_bitmap);
5910 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
5911 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
5912 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
5913 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
5914 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
5915 /*
5916 * VMX instructions trap unconditionally. This allows L1 to
5917 * emulate them for its L2 guest, i.e., allows 3-level nesting!
5918 */
5919 return true;
5920 case EXIT_REASON_CR_ACCESS:
5921 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
5922 case EXIT_REASON_DR_ACCESS:
5923 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
5924 case EXIT_REASON_IO_INSTRUCTION:
5925 return nested_vmx_exit_handled_io(vcpu, vmcs12);
5926 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
5927 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
5928 case EXIT_REASON_MSR_READ:
5929 case EXIT_REASON_MSR_WRITE:
5930 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
5931 case EXIT_REASON_INVALID_STATE:
5932 return true;
5933 case EXIT_REASON_MWAIT_INSTRUCTION:
5934 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
5935 case EXIT_REASON_MONITOR_TRAP_FLAG:
Oliver Uptonb045ae92020-04-14 22:47:45 +00005936 return nested_vmx_exit_handled_mtf(vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08005937 case EXIT_REASON_MONITOR_INSTRUCTION:
5938 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
5939 case EXIT_REASON_PAUSE_INSTRUCTION:
5940 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
5941 nested_cpu_has2(vmcs12,
5942 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
5943 case EXIT_REASON_MCE_DURING_VMENTRY:
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005944 return true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005945 case EXIT_REASON_TPR_BELOW_THRESHOLD:
5946 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
5947 case EXIT_REASON_APIC_ACCESS:
5948 case EXIT_REASON_APIC_WRITE:
5949 case EXIT_REASON_EOI_INDUCED:
5950 /*
5951 * The controls for "virtualize APIC accesses," "APIC-
5952 * register virtualization," and "virtual-interrupt
5953 * delivery" only come from vmcs12.
5954 */
5955 return true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005956 case EXIT_REASON_INVPCID:
5957 return
5958 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
5959 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
5960 case EXIT_REASON_WBINVD:
5961 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
5962 case EXIT_REASON_XSETBV:
5963 return true;
5964 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
5965 /*
5966 * This should never happen, since it is not possible to
5967 * set XSS to a non-zero value---neither in L1 nor in L2.
5968 * If if it were, XSS would have to be checked against
5969 * the XSS exit bitmap in vmcs12.
5970 */
5971 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Tao Xubf653b72019-07-16 14:55:51 +08005972 case EXIT_REASON_UMWAIT:
5973 case EXIT_REASON_TPAUSE:
5974 return nested_cpu_has2(vmcs12,
5975 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE);
Sean Christopherson72add912021-04-12 16:21:42 +12005976 case EXIT_REASON_ENCLS:
5977 return nested_vmx_exit_handled_encls(vcpu, vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08005978 default:
5979 return true;
5980 }
5981}
5982
Sean Christopherson7b7bd872020-04-15 10:55:11 -07005983/*
5984 * Conditionally reflect a VM-Exit into L1. Returns %true if the VM-Exit was
5985 * reflected into L1.
5986 */
Sean Christophersonf47baae2020-04-15 10:55:16 -07005987bool nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu)
Sean Christopherson7b7bd872020-04-15 10:55:11 -07005988{
Sean Christophersonfbdd5022020-04-15 10:55:12 -07005989 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson8e533242020-11-06 17:03:12 +08005990 union vmx_exit_reason exit_reason = vmx->exit_reason;
Sean Christopherson87796552020-04-22 17:11:27 -07005991 unsigned long exit_qual;
5992 u32 exit_intr_info;
Sean Christophersonfbdd5022020-04-15 10:55:12 -07005993
5994 WARN_ON_ONCE(vmx->nested.nested_run_pending);
5995
5996 /*
5997 * Late nested VM-Fail shares the same flow as nested VM-Exit since KVM
5998 * has already loaded L2's state.
5999 */
6000 if (unlikely(vmx->fail)) {
6001 trace_kvm_nested_vmenter_failed(
6002 "hardware VM-instruction error: ",
6003 vmcs_read32(VM_INSTRUCTION_ERROR));
6004 exit_intr_info = 0;
6005 exit_qual = 0;
6006 goto reflect_vmexit;
6007 }
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006008
Sean Christopherson8e533242020-11-06 17:03:12 +08006009 trace_kvm_nested_vmexit(exit_reason.full, vcpu, KVM_ISA_VMX);
Sean Christopherson236871b2020-04-15 10:55:13 -07006010
Sean Christopherson2c1f3322020-04-15 10:55:14 -07006011 /* If L0 (KVM) wants the exit, it trumps L1's desires. */
6012 if (nested_vmx_l0_wants_exit(vcpu, exit_reason))
6013 return false;
6014
6015 /* If L1 doesn't want the exit, handle it in L0. */
6016 if (!nested_vmx_l1_wants_exit(vcpu, exit_reason))
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006017 return false;
6018
6019 /*
Sean Christopherson1d283062020-04-15 10:55:15 -07006020 * vmcs.VM_EXIT_INTR_INFO is only valid for EXCEPTION_NMI exits. For
6021 * EXTERNAL_INTERRUPT, the value for vmcs12->vm_exit_intr_info would
6022 * need to be synthesized by querying the in-kernel LAPIC, but external
6023 * interrupts are never reflected to L1 so it's a non-issue.
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006024 */
Sean Christopherson02f19652020-09-23 13:13:49 -07006025 exit_intr_info = vmx_get_intr_info(vcpu);
Sean Christophersonf315f2b2020-09-23 13:13:45 -07006026 if (is_exception_with_error_code(exit_intr_info)) {
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006027 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6028
6029 vmcs12->vm_exit_intr_error_code =
6030 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
6031 }
Sean Christopherson02f19652020-09-23 13:13:49 -07006032 exit_qual = vmx_get_exit_qual(vcpu);
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006033
Sean Christophersonfbdd5022020-04-15 10:55:12 -07006034reflect_vmexit:
Sean Christopherson8e533242020-11-06 17:03:12 +08006035 nested_vmx_vmexit(vcpu, exit_reason.full, exit_intr_info, exit_qual);
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006036 return true;
6037}
Sean Christopherson55d23752018-12-03 13:53:18 -08006038
6039static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
6040 struct kvm_nested_state __user *user_kvm_nested_state,
6041 u32 user_data_size)
6042{
6043 struct vcpu_vmx *vmx;
6044 struct vmcs12 *vmcs12;
6045 struct kvm_nested_state kvm_state = {
6046 .flags = 0,
Liran Alon6ca00df2019-06-16 15:03:10 +03006047 .format = KVM_STATE_NESTED_FORMAT_VMX,
Sean Christopherson55d23752018-12-03 13:53:18 -08006048 .size = sizeof(kvm_state),
Peter Shier850448f2020-05-26 14:51:06 -07006049 .hdr.vmx.flags = 0,
Liran Alon6ca00df2019-06-16 15:03:10 +03006050 .hdr.vmx.vmxon_pa = -1ull,
6051 .hdr.vmx.vmcs12_pa = -1ull,
Peter Shier850448f2020-05-26 14:51:06 -07006052 .hdr.vmx.preemption_timer_deadline = 0,
Sean Christopherson55d23752018-12-03 13:53:18 -08006053 };
Liran Alon6ca00df2019-06-16 15:03:10 +03006054 struct kvm_vmx_nested_state_data __user *user_vmx_nested_state =
6055 &user_kvm_nested_state->data.vmx[0];
Sean Christopherson55d23752018-12-03 13:53:18 -08006056
6057 if (!vcpu)
Liran Alon6ca00df2019-06-16 15:03:10 +03006058 return kvm_state.size + sizeof(*user_vmx_nested_state);
Sean Christopherson55d23752018-12-03 13:53:18 -08006059
6060 vmx = to_vmx(vcpu);
6061 vmcs12 = get_vmcs12(vcpu);
6062
Sean Christopherson55d23752018-12-03 13:53:18 -08006063 if (nested_vmx_allowed(vcpu) &&
6064 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
Liran Alon6ca00df2019-06-16 15:03:10 +03006065 kvm_state.hdr.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
6066 kvm_state.hdr.vmx.vmcs12_pa = vmx->nested.current_vmptr;
Sean Christopherson55d23752018-12-03 13:53:18 -08006067
6068 if (vmx_has_valid_vmcs12(vcpu)) {
Liran Alon6ca00df2019-06-16 15:03:10 +03006069 kvm_state.size += sizeof(user_vmx_nested_state->vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08006070
Vitaly Kuznetsov27849962021-05-26 15:20:20 +02006071 /* 'hv_evmcs_vmptr' can also be EVMPTR_MAP_PENDING here */
6072 if (vmx->nested.hv_evmcs_vmptr != EVMPTR_INVALID)
Liran Alon323d73a2019-06-26 16:09:27 +03006073 kvm_state.flags |= KVM_STATE_NESTED_EVMCS;
6074
Sean Christopherson55d23752018-12-03 13:53:18 -08006075 if (is_guest_mode(vcpu) &&
6076 nested_cpu_has_shadow_vmcs(vmcs12) &&
6077 vmcs12->vmcs_link_pointer != -1ull)
Liran Alon6ca00df2019-06-16 15:03:10 +03006078 kvm_state.size += sizeof(user_vmx_nested_state->shadow_vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08006079 }
6080
6081 if (vmx->nested.smm.vmxon)
Liran Alon6ca00df2019-06-16 15:03:10 +03006082 kvm_state.hdr.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
Sean Christopherson55d23752018-12-03 13:53:18 -08006083
6084 if (vmx->nested.smm.guest_mode)
Liran Alon6ca00df2019-06-16 15:03:10 +03006085 kvm_state.hdr.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
Sean Christopherson55d23752018-12-03 13:53:18 -08006086
6087 if (is_guest_mode(vcpu)) {
6088 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
6089
6090 if (vmx->nested.nested_run_pending)
6091 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
Oliver Upton5ef8acb2020-02-07 02:36:07 -08006092
6093 if (vmx->nested.mtf_pending)
6094 kvm_state.flags |= KVM_STATE_NESTED_MTF_PENDING;
Peter Shier850448f2020-05-26 14:51:06 -07006095
6096 if (nested_cpu_has_preemption_timer(vmcs12) &&
6097 vmx->nested.has_preemption_timer_deadline) {
6098 kvm_state.hdr.vmx.flags |=
6099 KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE;
6100 kvm_state.hdr.vmx.preemption_timer_deadline =
6101 vmx->nested.preemption_timer_deadline;
6102 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006103 }
6104 }
6105
6106 if (user_data_size < kvm_state.size)
6107 goto out;
6108
6109 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
6110 return -EFAULT;
6111
6112 if (!vmx_has_valid_vmcs12(vcpu))
6113 goto out;
6114
6115 /*
6116 * When running L2, the authoritative vmcs12 state is in the
6117 * vmcs02. When running L1, the authoritative vmcs12 state is
6118 * in the shadow or enlightened vmcs linked to vmcs01, unless
Sean Christopherson3731905ef2019-05-07 08:36:27 -07006119 * need_vmcs12_to_shadow_sync is set, in which case, the authoritative
Sean Christopherson55d23752018-12-03 13:53:18 -08006120 * vmcs12 state is in the vmcs12 already.
6121 */
6122 if (is_guest_mode(vcpu)) {
Sean Christopherson3731905ef2019-05-07 08:36:27 -07006123 sync_vmcs02_to_vmcs12(vcpu, vmcs12);
Sean Christopherson7952d762019-05-07 08:36:29 -07006124 sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
Maxim Levitskyd51e1d32021-01-14 22:54:47 +02006125 } else {
6126 copy_vmcs02_to_vmcs12_rare(vcpu, get_vmcs12(vcpu));
6127 if (!vmx->nested.need_vmcs12_to_shadow_sync) {
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02006128 if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02006129 /*
6130 * L1 hypervisor is not obliged to keep eVMCS
6131 * clean fields data always up-to-date while
6132 * not in guest mode, 'hv_clean_fields' is only
6133 * supposed to be actual upon vmentry so we need
6134 * to ignore it here and do full copy.
6135 */
6136 copy_enlightened_to_vmcs12(vmx, 0);
Maxim Levitskyd51e1d32021-01-14 22:54:47 +02006137 else if (enable_shadow_vmcs)
6138 copy_shadow_to_vmcs12(vmx);
6139 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006140 }
6141
Liran Alon6ca00df2019-06-16 15:03:10 +03006142 BUILD_BUG_ON(sizeof(user_vmx_nested_state->vmcs12) < VMCS12_SIZE);
6143 BUILD_BUG_ON(sizeof(user_vmx_nested_state->shadow_vmcs12) < VMCS12_SIZE);
6144
Tom Roeder3a33d032019-01-24 13:48:20 -08006145 /*
6146 * Copy over the full allocated size of vmcs12 rather than just the size
6147 * of the struct.
6148 */
Liran Alon6ca00df2019-06-16 15:03:10 +03006149 if (copy_to_user(user_vmx_nested_state->vmcs12, vmcs12, VMCS12_SIZE))
Sean Christopherson55d23752018-12-03 13:53:18 -08006150 return -EFAULT;
6151
6152 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
6153 vmcs12->vmcs_link_pointer != -1ull) {
Liran Alon6ca00df2019-06-16 15:03:10 +03006154 if (copy_to_user(user_vmx_nested_state->shadow_vmcs12,
Tom Roeder3a33d032019-01-24 13:48:20 -08006155 get_shadow_vmcs12(vcpu), VMCS12_SIZE))
Sean Christopherson55d23752018-12-03 13:53:18 -08006156 return -EFAULT;
6157 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006158out:
6159 return kvm_state.size;
6160}
6161
6162/*
6163 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
6164 */
6165void vmx_leave_nested(struct kvm_vcpu *vcpu)
6166{
6167 if (is_guest_mode(vcpu)) {
6168 to_vmx(vcpu)->nested.nested_run_pending = 0;
6169 nested_vmx_vmexit(vcpu, -1, 0, 0);
6170 }
6171 free_nested(vcpu);
6172}
6173
6174static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
6175 struct kvm_nested_state __user *user_kvm_nested_state,
6176 struct kvm_nested_state *kvm_state)
6177{
6178 struct vcpu_vmx *vmx = to_vmx(vcpu);
6179 struct vmcs12 *vmcs12;
Sean Christopherson68cda402020-05-11 15:05:29 -07006180 enum vm_entry_failure_code ignored;
Liran Alon6ca00df2019-06-16 15:03:10 +03006181 struct kvm_vmx_nested_state_data __user *user_vmx_nested_state =
6182 &user_kvm_nested_state->data.vmx[0];
Sean Christopherson55d23752018-12-03 13:53:18 -08006183 int ret;
6184
Liran Alon6ca00df2019-06-16 15:03:10 +03006185 if (kvm_state->format != KVM_STATE_NESTED_FORMAT_VMX)
Sean Christopherson55d23752018-12-03 13:53:18 -08006186 return -EINVAL;
6187
Liran Alon6ca00df2019-06-16 15:03:10 +03006188 if (kvm_state->hdr.vmx.vmxon_pa == -1ull) {
6189 if (kvm_state->hdr.vmx.smm.flags)
Sean Christopherson55d23752018-12-03 13:53:18 -08006190 return -EINVAL;
6191
Liran Alon6ca00df2019-06-16 15:03:10 +03006192 if (kvm_state->hdr.vmx.vmcs12_pa != -1ull)
Sean Christopherson55d23752018-12-03 13:53:18 -08006193 return -EINVAL;
6194
Liran Alon323d73a2019-06-26 16:09:27 +03006195 /*
6196 * KVM_STATE_NESTED_EVMCS used to signal that KVM should
6197 * enable eVMCS capability on vCPU. However, since then
6198 * code was changed such that flag signals vmcs12 should
6199 * be copied into eVMCS in guest memory.
6200 *
6201 * To preserve backwards compatability, allow user
6202 * to set this flag even when there is no VMXON region.
6203 */
Paolo Bonzini9fd58872019-06-19 16:52:27 +02006204 if (kvm_state->flags & ~KVM_STATE_NESTED_EVMCS)
6205 return -EINVAL;
6206 } else {
6207 if (!nested_vmx_allowed(vcpu))
6208 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08006209
Paolo Bonzini9fd58872019-06-19 16:52:27 +02006210 if (!page_address_valid(vcpu, kvm_state->hdr.vmx.vmxon_pa))
6211 return -EINVAL;
Liran Alon323d73a2019-06-26 16:09:27 +03006212 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006213
Liran Alon6ca00df2019-06-16 15:03:10 +03006214 if ((kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
Sean Christopherson55d23752018-12-03 13:53:18 -08006215 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
6216 return -EINVAL;
6217
Liran Alon6ca00df2019-06-16 15:03:10 +03006218 if (kvm_state->hdr.vmx.smm.flags &
Sean Christopherson55d23752018-12-03 13:53:18 -08006219 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
6220 return -EINVAL;
6221
Paolo Bonzini5e105c82020-07-27 08:55:09 -04006222 if (kvm_state->hdr.vmx.flags & ~KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE)
6223 return -EINVAL;
6224
Sean Christopherson55d23752018-12-03 13:53:18 -08006225 /*
6226 * SMM temporarily disables VMX, so we cannot be in guest mode,
6227 * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags
6228 * must be zero.
6229 */
Liran Alon65b712f12019-06-25 14:26:42 +03006230 if (is_smm(vcpu) ?
6231 (kvm_state->flags &
6232 (KVM_STATE_NESTED_GUEST_MODE | KVM_STATE_NESTED_RUN_PENDING))
6233 : kvm_state->hdr.vmx.smm.flags)
Sean Christopherson55d23752018-12-03 13:53:18 -08006234 return -EINVAL;
6235
Liran Alon6ca00df2019-06-16 15:03:10 +03006236 if ((kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
6237 !(kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
Sean Christopherson55d23752018-12-03 13:53:18 -08006238 return -EINVAL;
6239
Liran Alon323d73a2019-06-26 16:09:27 +03006240 if ((kvm_state->flags & KVM_STATE_NESTED_EVMCS) &&
6241 (!nested_vmx_allowed(vcpu) || !vmx->nested.enlightened_vmcs_enabled))
Paolo Bonzini9fd58872019-06-19 16:52:27 +02006242 return -EINVAL;
6243
Liran Alon323d73a2019-06-26 16:09:27 +03006244 vmx_leave_nested(vcpu);
Paolo Bonzini9fd58872019-06-19 16:52:27 +02006245
Liran Alon6ca00df2019-06-16 15:03:10 +03006246 if (kvm_state->hdr.vmx.vmxon_pa == -1ull)
Sean Christopherson55d23752018-12-03 13:53:18 -08006247 return 0;
6248
Liran Alon6ca00df2019-06-16 15:03:10 +03006249 vmx->nested.vmxon_ptr = kvm_state->hdr.vmx.vmxon_pa;
Sean Christopherson55d23752018-12-03 13:53:18 -08006250 ret = enter_vmx_operation(vcpu);
6251 if (ret)
6252 return ret;
6253
Paolo Bonzini0f02bd02020-07-27 09:00:37 -04006254 /* Empty 'VMXON' state is permitted if no VMCS loaded */
6255 if (kvm_state->size < sizeof(*kvm_state) + sizeof(*vmcs12)) {
6256 /* See vmx_has_valid_vmcs12. */
6257 if ((kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE) ||
6258 (kvm_state->flags & KVM_STATE_NESTED_EVMCS) ||
6259 (kvm_state->hdr.vmx.vmcs12_pa != -1ull))
6260 return -EINVAL;
6261 else
6262 return 0;
6263 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006264
Liran Alon6ca00df2019-06-16 15:03:10 +03006265 if (kvm_state->hdr.vmx.vmcs12_pa != -1ull) {
6266 if (kvm_state->hdr.vmx.vmcs12_pa == kvm_state->hdr.vmx.vmxon_pa ||
6267 !page_address_valid(vcpu, kvm_state->hdr.vmx.vmcs12_pa))
Sean Christopherson55d23752018-12-03 13:53:18 -08006268 return -EINVAL;
6269
Liran Alon6ca00df2019-06-16 15:03:10 +03006270 set_current_vmptr(vmx, kvm_state->hdr.vmx.vmcs12_pa);
Sean Christopherson55d23752018-12-03 13:53:18 -08006271 } else if (kvm_state->flags & KVM_STATE_NESTED_EVMCS) {
6272 /*
Vitaly Kuznetsove942dbf2020-03-09 16:52:12 +01006273 * nested_vmx_handle_enlightened_vmptrld() cannot be called
6274 * directly from here as HV_X64_MSR_VP_ASSIST_PAGE may not be
6275 * restored yet. EVMCS will be mapped from
6276 * nested_get_vmcs12_pages().
Sean Christopherson55d23752018-12-03 13:53:18 -08006277 */
Vitaly Kuznetsov27849962021-05-26 15:20:20 +02006278 vmx->nested.hv_evmcs_vmptr = EVMPTR_MAP_PENDING;
Paolo Bonzini729c15c2020-09-22 06:53:57 -04006279 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08006280 } else {
6281 return -EINVAL;
6282 }
6283
Liran Alon6ca00df2019-06-16 15:03:10 +03006284 if (kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
Sean Christopherson55d23752018-12-03 13:53:18 -08006285 vmx->nested.smm.vmxon = true;
6286 vmx->nested.vmxon = false;
6287
Liran Alon6ca00df2019-06-16 15:03:10 +03006288 if (kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
Sean Christopherson55d23752018-12-03 13:53:18 -08006289 vmx->nested.smm.guest_mode = true;
6290 }
6291
6292 vmcs12 = get_vmcs12(vcpu);
Liran Alon6ca00df2019-06-16 15:03:10 +03006293 if (copy_from_user(vmcs12, user_vmx_nested_state->vmcs12, sizeof(*vmcs12)))
Sean Christopherson55d23752018-12-03 13:53:18 -08006294 return -EFAULT;
6295
6296 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
6297 return -EINVAL;
6298
6299 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
6300 return 0;
6301
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006302 vmx->nested.nested_run_pending =
6303 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
6304
Oliver Upton5ef8acb2020-02-07 02:36:07 -08006305 vmx->nested.mtf_pending =
6306 !!(kvm_state->flags & KVM_STATE_NESTED_MTF_PENDING);
6307
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006308 ret = -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08006309 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
6310 vmcs12->vmcs_link_pointer != -1ull) {
6311 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
6312
Liran Alon6ca00df2019-06-16 15:03:10 +03006313 if (kvm_state->size <
6314 sizeof(*kvm_state) +
6315 sizeof(user_vmx_nested_state->vmcs12) + sizeof(*shadow_vmcs12))
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006316 goto error_guest_mode;
Sean Christopherson55d23752018-12-03 13:53:18 -08006317
6318 if (copy_from_user(shadow_vmcs12,
Liran Alon6ca00df2019-06-16 15:03:10 +03006319 user_vmx_nested_state->shadow_vmcs12,
6320 sizeof(*shadow_vmcs12))) {
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006321 ret = -EFAULT;
6322 goto error_guest_mode;
6323 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006324
6325 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
6326 !shadow_vmcs12->hdr.shadow_vmcs)
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006327 goto error_guest_mode;
Sean Christopherson55d23752018-12-03 13:53:18 -08006328 }
6329
Paolo Bonzini83d31e52020-07-09 13:12:09 -04006330 vmx->nested.has_preemption_timer_deadline = false;
Peter Shier850448f2020-05-26 14:51:06 -07006331 if (kvm_state->hdr.vmx.flags & KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE) {
6332 vmx->nested.has_preemption_timer_deadline = true;
6333 vmx->nested.preemption_timer_deadline =
6334 kvm_state->hdr.vmx.preemption_timer_deadline;
6335 }
6336
Sean Christopherson5478ba32019-04-11 12:18:06 -07006337 if (nested_vmx_check_controls(vcpu, vmcs12) ||
6338 nested_vmx_check_host_state(vcpu, vmcs12) ||
Sean Christopherson68cda402020-05-11 15:05:29 -07006339 nested_vmx_check_guest_state(vcpu, vmcs12, &ignored))
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006340 goto error_guest_mode;
Sean Christopherson55d23752018-12-03 13:53:18 -08006341
6342 vmx->nested.dirty_vmcs12 = true;
6343 ret = nested_vmx_enter_non_root_mode(vcpu, false);
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006344 if (ret)
6345 goto error_guest_mode;
Sean Christopherson55d23752018-12-03 13:53:18 -08006346
6347 return 0;
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006348
6349error_guest_mode:
6350 vmx->nested.nested_run_pending = 0;
6351 return ret;
Sean Christopherson55d23752018-12-03 13:53:18 -08006352}
6353
Xiaoyao Li1b842922019-10-20 17:11:01 +08006354void nested_vmx_set_vmcs_shadowing_bitmap(void)
Sean Christopherson55d23752018-12-03 13:53:18 -08006355{
6356 if (enable_shadow_vmcs) {
Sean Christopherson55d23752018-12-03 13:53:18 -08006357 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Sean Christophersonfadcead2019-05-07 08:36:23 -07006358 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
Sean Christopherson55d23752018-12-03 13:53:18 -08006359 }
6360}
6361
6362/*
6363 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
6364 * returned for the various VMX controls MSRs when nested VMX is enabled.
6365 * The same values should also be used to verify that vmcs12 control fields are
6366 * valid during nested entry from L1 to L2.
6367 * Each of these control msrs has a low and high 32-bit half: A low bit is on
6368 * if the corresponding bit in the (32-bit) control field *must* be on, and a
6369 * bit in the high half is on if the corresponding bit in the control field
6370 * may be on. See also vmx_control_verify().
6371 */
Vitaly Kuznetsova4443262020-02-20 18:22:04 +01006372void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, u32 ept_caps)
Sean Christopherson55d23752018-12-03 13:53:18 -08006373{
6374 /*
6375 * Note that as a general rule, the high half of the MSRs (bits in
6376 * the control fields which may be 1) should be initialized by the
6377 * intersection of the underlying hardware's MSR (i.e., features which
6378 * can be supported) and the list of features we want to expose -
6379 * because they are known to be properly supported in our code.
6380 * Also, usually, the low half of the MSRs (bits which must be 1) can
6381 * be set to 0, meaning that L1 may turn off any of these bits. The
6382 * reason is that if one of these bits is necessary, it will appear
6383 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
6384 * fields of vmcs01 and vmcs02, will turn these bits off - and
Sean Christopherson2c1f3322020-04-15 10:55:14 -07006385 * nested_vmx_l1_wants_exit() will not pass related exits to L1.
Sean Christopherson55d23752018-12-03 13:53:18 -08006386 * These rules have exceptions below.
6387 */
6388
6389 /* pin-based controls */
6390 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
6391 msrs->pinbased_ctls_low,
6392 msrs->pinbased_ctls_high);
6393 msrs->pinbased_ctls_low |=
6394 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
6395 msrs->pinbased_ctls_high &=
6396 PIN_BASED_EXT_INTR_MASK |
6397 PIN_BASED_NMI_EXITING |
6398 PIN_BASED_VIRTUAL_NMIS |
Vitaly Kuznetsova4443262020-02-20 18:22:04 +01006399 (enable_apicv ? PIN_BASED_POSTED_INTR : 0);
Sean Christopherson55d23752018-12-03 13:53:18 -08006400 msrs->pinbased_ctls_high |=
6401 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
6402 PIN_BASED_VMX_PREEMPTION_TIMER;
6403
6404 /* exit controls */
6405 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
6406 msrs->exit_ctls_low,
6407 msrs->exit_ctls_high);
6408 msrs->exit_ctls_low =
6409 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
6410
6411 msrs->exit_ctls_high &=
6412#ifdef CONFIG_X86_64
6413 VM_EXIT_HOST_ADDR_SPACE_SIZE |
6414#endif
Chenyi Qiangefc83132020-08-28 16:56:18 +08006415 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT |
6416 VM_EXIT_CLEAR_BNDCFGS | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
Sean Christopherson55d23752018-12-03 13:53:18 -08006417 msrs->exit_ctls_high |=
6418 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
6419 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
6420 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
6421
6422 /* We support free control of debug control saving. */
6423 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
6424
6425 /* entry controls */
6426 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
6427 msrs->entry_ctls_low,
6428 msrs->entry_ctls_high);
6429 msrs->entry_ctls_low =
6430 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
6431 msrs->entry_ctls_high &=
6432#ifdef CONFIG_X86_64
6433 VM_ENTRY_IA32E_MODE |
6434#endif
Chenyi Qiangefc83132020-08-28 16:56:18 +08006435 VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS |
6436 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
Sean Christopherson55d23752018-12-03 13:53:18 -08006437 msrs->entry_ctls_high |=
6438 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
6439
6440 /* We support free control of debug control loading. */
6441 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
6442
6443 /* cpu-based controls */
6444 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
6445 msrs->procbased_ctls_low,
6446 msrs->procbased_ctls_high);
6447 msrs->procbased_ctls_low =
6448 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
6449 msrs->procbased_ctls_high &=
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08006450 CPU_BASED_INTR_WINDOW_EXITING |
Xiaoyao Li5e3d3942019-12-06 16:45:26 +08006451 CPU_BASED_NMI_WINDOW_EXITING | CPU_BASED_USE_TSC_OFFSETTING |
Sean Christopherson55d23752018-12-03 13:53:18 -08006452 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
6453 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
6454 CPU_BASED_CR3_STORE_EXITING |
6455#ifdef CONFIG_X86_64
6456 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
6457#endif
6458 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
6459 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
6460 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
6461 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
6462 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
6463 /*
6464 * We can allow some features even when not supported by the
6465 * hardware. For example, L1 can specify an MSR bitmap - and we
6466 * can use it to avoid exits to L1 - even when L0 runs L2
6467 * without MSR bitmaps.
6468 */
6469 msrs->procbased_ctls_high |=
6470 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
6471 CPU_BASED_USE_MSR_BITMAPS;
6472
6473 /* We support free control of CR3 access interception. */
6474 msrs->procbased_ctls_low &=
6475 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
6476
6477 /*
6478 * secondary cpu-based controls. Do not include those that
Xiaoyao Li7c1b7612020-07-09 12:34:25 +08006479 * depend on CPUID bits, they are added later by
6480 * vmx_vcpu_after_set_cpuid.
Sean Christopherson55d23752018-12-03 13:53:18 -08006481 */
Vitaly Kuznetsov6b1971c2019-02-07 11:42:14 +01006482 if (msrs->procbased_ctls_high & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)
6483 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
6484 msrs->secondary_ctls_low,
6485 msrs->secondary_ctls_high);
6486
Sean Christopherson55d23752018-12-03 13:53:18 -08006487 msrs->secondary_ctls_low = 0;
6488 msrs->secondary_ctls_high &=
6489 SECONDARY_EXEC_DESC |
Sean Christopherson7f3603b2020-09-23 09:50:47 -07006490 SECONDARY_EXEC_ENABLE_RDTSCP |
Sean Christopherson55d23752018-12-03 13:53:18 -08006491 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini6defc592019-07-02 14:39:29 +02006492 SECONDARY_EXEC_WBINVD_EXITING |
Sean Christopherson55d23752018-12-03 13:53:18 -08006493 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6494 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini6defc592019-07-02 14:39:29 +02006495 SECONDARY_EXEC_RDRAND_EXITING |
6496 SECONDARY_EXEC_ENABLE_INVPCID |
6497 SECONDARY_EXEC_RDSEED_EXITING |
Ilias Stamatisd041b5e2021-05-26 19:44:17 +01006498 SECONDARY_EXEC_XSAVES |
6499 SECONDARY_EXEC_TSC_SCALING;
Sean Christopherson55d23752018-12-03 13:53:18 -08006500
6501 /*
6502 * We can emulate "VMCS shadowing," even if the hardware
6503 * doesn't support it.
6504 */
6505 msrs->secondary_ctls_high |=
6506 SECONDARY_EXEC_SHADOW_VMCS;
6507
6508 if (enable_ept) {
6509 /* nested EPT: emulate EPT also to L1 */
6510 msrs->secondary_ctls_high |=
6511 SECONDARY_EXEC_ENABLE_EPT;
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08006512 msrs->ept_caps =
6513 VMX_EPT_PAGE_WALK_4_BIT |
6514 VMX_EPT_PAGE_WALK_5_BIT |
6515 VMX_EPTP_WB_BIT |
Sean Christopherson96d47012020-03-02 18:02:40 -08006516 VMX_EPT_INVEPT_BIT |
6517 VMX_EPT_EXECUTE_ONLY_BIT;
6518
Sean Christopherson55d23752018-12-03 13:53:18 -08006519 msrs->ept_caps &= ept_caps;
6520 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
6521 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
6522 VMX_EPT_1GB_PAGE_BIT;
6523 if (enable_ept_ad_bits) {
6524 msrs->secondary_ctls_high |=
6525 SECONDARY_EXEC_ENABLE_PML;
6526 msrs->ept_caps |= VMX_EPT_AD_BIT;
6527 }
6528 }
6529
6530 if (cpu_has_vmx_vmfunc()) {
6531 msrs->secondary_ctls_high |=
6532 SECONDARY_EXEC_ENABLE_VMFUNC;
6533 /*
6534 * Advertise EPTP switching unconditionally
6535 * since we emulate it
6536 */
6537 if (enable_ept)
6538 msrs->vmfunc_controls =
6539 VMX_VMFUNC_EPTP_SWITCHING;
6540 }
6541
6542 /*
6543 * Old versions of KVM use the single-context version without
6544 * checking for support, so declare that it is supported even
6545 * though it is treated as global context. The alternative is
6546 * not failing the single-context invvpid, and it is worse.
6547 */
6548 if (enable_vpid) {
6549 msrs->secondary_ctls_high |=
6550 SECONDARY_EXEC_ENABLE_VPID;
6551 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
6552 VMX_VPID_EXTENT_SUPPORTED_MASK;
6553 }
6554
6555 if (enable_unrestricted_guest)
6556 msrs->secondary_ctls_high |=
6557 SECONDARY_EXEC_UNRESTRICTED_GUEST;
6558
6559 if (flexpriority_enabled)
6560 msrs->secondary_ctls_high |=
6561 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6562
Sean Christopherson72add912021-04-12 16:21:42 +12006563 if (enable_sgx)
6564 msrs->secondary_ctls_high |= SECONDARY_EXEC_ENCLS_EXITING;
6565
Sean Christopherson55d23752018-12-03 13:53:18 -08006566 /* miscellaneous data */
6567 rdmsr(MSR_IA32_VMX_MISC,
6568 msrs->misc_low,
6569 msrs->misc_high);
6570 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
6571 msrs->misc_low |=
6572 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
6573 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Yadong Qibf0cd882020-11-06 14:51:22 +08006574 VMX_MISC_ACTIVITY_HLT |
6575 VMX_MISC_ACTIVITY_WAIT_SIPI;
Sean Christopherson55d23752018-12-03 13:53:18 -08006576 msrs->misc_high = 0;
6577
6578 /*
6579 * This MSR reports some information about VMX support. We
6580 * should return information about the VMX we emulate for the
6581 * guest, and the VMCS structure we give it - not about the
6582 * VMX support of the underlying hardware.
6583 */
6584 msrs->basic =
6585 VMCS12_REVISION |
6586 VMX_BASIC_TRUE_CTLS |
6587 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
6588 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
6589
6590 if (cpu_has_vmx_basic_inout())
6591 msrs->basic |= VMX_BASIC_INOUT;
6592
6593 /*
6594 * These MSRs specify bits which the guest must keep fixed on
6595 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
6596 * We picked the standard core2 setting.
6597 */
6598#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
6599#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
6600 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
6601 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
6602
6603 /* These MSRs specify bits which the guest must keep fixed off. */
6604 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
6605 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
6606
6607 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
6608 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
6609}
6610
6611void nested_vmx_hardware_unsetup(void)
6612{
6613 int i;
6614
6615 if (enable_shadow_vmcs) {
6616 for (i = 0; i < VMX_BITMAP_NR; i++)
6617 free_page((unsigned long)vmx_bitmap[i]);
6618 }
6619}
6620
Sean Christopherson6c1c6e52020-05-06 13:46:53 -07006621__init int nested_vmx_hardware_setup(int (*exit_handlers[])(struct kvm_vcpu *))
Sean Christopherson55d23752018-12-03 13:53:18 -08006622{
6623 int i;
6624
6625 if (!cpu_has_vmx_shadow_vmcs())
6626 enable_shadow_vmcs = 0;
6627 if (enable_shadow_vmcs) {
6628 for (i = 0; i < VMX_BITMAP_NR; i++) {
Ben Gardon41836832019-02-11 11:02:52 -08006629 /*
6630 * The vmx_bitmap is not tied to a VM and so should
6631 * not be charged to a memcg.
6632 */
Sean Christopherson55d23752018-12-03 13:53:18 -08006633 vmx_bitmap[i] = (unsigned long *)
6634 __get_free_page(GFP_KERNEL);
6635 if (!vmx_bitmap[i]) {
6636 nested_vmx_hardware_unsetup();
6637 return -ENOMEM;
6638 }
6639 }
6640
6641 init_vmcs_shadow_fields();
6642 }
6643
Liran Aloncc877672019-11-18 21:11:21 +02006644 exit_handlers[EXIT_REASON_VMCLEAR] = handle_vmclear;
6645 exit_handlers[EXIT_REASON_VMLAUNCH] = handle_vmlaunch;
6646 exit_handlers[EXIT_REASON_VMPTRLD] = handle_vmptrld;
6647 exit_handlers[EXIT_REASON_VMPTRST] = handle_vmptrst;
6648 exit_handlers[EXIT_REASON_VMREAD] = handle_vmread;
6649 exit_handlers[EXIT_REASON_VMRESUME] = handle_vmresume;
6650 exit_handlers[EXIT_REASON_VMWRITE] = handle_vmwrite;
6651 exit_handlers[EXIT_REASON_VMOFF] = handle_vmoff;
6652 exit_handlers[EXIT_REASON_VMON] = handle_vmon;
6653 exit_handlers[EXIT_REASON_INVEPT] = handle_invept;
6654 exit_handlers[EXIT_REASON_INVVPID] = handle_invvpid;
6655 exit_handlers[EXIT_REASON_VMFUNC] = handle_vmfunc;
Sean Christopherson55d23752018-12-03 13:53:18 -08006656
Sean Christopherson55d23752018-12-03 13:53:18 -08006657 return 0;
6658}
Paolo Bonzini33b22172020-04-17 10:24:18 -04006659
6660struct kvm_x86_nested_ops vmx_nested_ops = {
6661 .check_events = vmx_check_nested_events,
Sean Christophersond2060bd2020-04-22 19:25:39 -07006662 .hv_timer_pending = nested_vmx_preemption_timer_pending,
Sean Christophersoncb6a32c2021-03-02 09:45:14 -08006663 .triple_fault = nested_vmx_triple_fault,
Paolo Bonzini33b22172020-04-17 10:24:18 -04006664 .get_state = vmx_get_nested_state,
6665 .set_state = vmx_set_nested_state,
Paolo Bonzini9a78e152021-01-08 11:43:08 -05006666 .get_nested_state_pages = vmx_get_nested_state_pages,
Sean Christopherson02f5fb22020-06-22 14:58:32 -07006667 .write_log_dirty = nested_vmx_write_pml_buffer,
Paolo Bonzini33b22172020-04-17 10:24:18 -04006668 .enable_evmcs = nested_enable_evmcs,
6669 .get_evmcs_version = nested_get_evmcs_version,
6670};