blob: c92349e2f621e16493b7870711c9b71e143a4b31 [file] [log] [blame]
Sean Christopherson55d23752018-12-03 13:53:18 -08001// SPDX-License-Identifier: GPL-2.0
2
3#include <linux/frame.h>
4#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"
13#include "trace.h"
14#include "x86.h"
15
16static bool __read_mostly enable_shadow_vmcs = 1;
17module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
18
19static bool __read_mostly nested_early_check = 0;
20module_param(nested_early_check, bool, S_IRUGO);
21
Sean Christopherson55d23752018-12-03 13:53:18 -080022/*
23 * Hyper-V requires all of these, so mark them as supported even though
24 * they are just treated the same as all-context.
25 */
26#define VMX_VPID_EXTENT_SUPPORTED_MASK \
27 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
28 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
29 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
30 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
31
32#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
33
34enum {
35 VMX_VMREAD_BITMAP,
36 VMX_VMWRITE_BITMAP,
37 VMX_BITMAP_NR
38};
39static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
40
41#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
42#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
43
44static u16 shadow_read_only_fields[] = {
45#define SHADOW_FIELD_RO(x) x,
46#include "vmcs_shadow_fields.h"
47};
48static int max_shadow_read_only_fields =
49 ARRAY_SIZE(shadow_read_only_fields);
50
51static u16 shadow_read_write_fields[] = {
52#define SHADOW_FIELD_RW(x) x,
53#include "vmcs_shadow_fields.h"
54};
55static int max_shadow_read_write_fields =
56 ARRAY_SIZE(shadow_read_write_fields);
57
Yi Wang8997f652019-01-21 15:27:05 +080058static void init_vmcs_shadow_fields(void)
Sean Christopherson55d23752018-12-03 13:53:18 -080059{
60 int i, j;
61
62 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
63 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
64
65 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
66 u16 field = shadow_read_only_fields[i];
67
68 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
69 (i + 1 == max_shadow_read_only_fields ||
70 shadow_read_only_fields[i + 1] != field + 1))
71 pr_err("Missing field from shadow_read_only_field %x\n",
72 field + 1);
73
74 clear_bit(field, vmx_vmread_bitmap);
75#ifdef CONFIG_X86_64
76 if (field & 1)
77 continue;
78#endif
79 if (j < i)
80 shadow_read_only_fields[j] = field;
81 j++;
82 }
83 max_shadow_read_only_fields = j;
84
85 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
86 u16 field = shadow_read_write_fields[i];
87
88 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
89 (i + 1 == max_shadow_read_write_fields ||
90 shadow_read_write_fields[i + 1] != field + 1))
91 pr_err("Missing field from shadow_read_write_field %x\n",
92 field + 1);
93
94 /*
95 * PML and the preemption timer can be emulated, but the
96 * processor cannot vmwrite to fields that don't exist
97 * on bare metal.
98 */
99 switch (field) {
100 case GUEST_PML_INDEX:
101 if (!cpu_has_vmx_pml())
102 continue;
103 break;
104 case VMX_PREEMPTION_TIMER_VALUE:
105 if (!cpu_has_vmx_preemption_timer())
106 continue;
107 break;
108 case GUEST_INTR_STATUS:
109 if (!cpu_has_vmx_apicv())
110 continue;
111 break;
112 default:
113 break;
114 }
115
116 clear_bit(field, vmx_vmwrite_bitmap);
117 clear_bit(field, vmx_vmread_bitmap);
118#ifdef CONFIG_X86_64
119 if (field & 1)
120 continue;
121#endif
122 if (j < i)
123 shadow_read_write_fields[j] = field;
124 j++;
125 }
126 max_shadow_read_write_fields = j;
127}
128
129/*
130 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
131 * set the success or error code of an emulated VMX instruction (as specified
132 * by Vol 2B, VMX Instruction Reference, "Conventions"), and skip the emulated
133 * instruction.
134 */
135static int nested_vmx_succeed(struct kvm_vcpu *vcpu)
136{
137 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
138 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
139 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
140 return kvm_skip_emulated_instruction(vcpu);
141}
142
143static int nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
144{
145 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
146 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
147 X86_EFLAGS_SF | X86_EFLAGS_OF))
148 | X86_EFLAGS_CF);
149 return kvm_skip_emulated_instruction(vcpu);
150}
151
152static int nested_vmx_failValid(struct kvm_vcpu *vcpu,
153 u32 vm_instruction_error)
154{
155 struct vcpu_vmx *vmx = to_vmx(vcpu);
156
157 /*
158 * failValid writes the error number to the current VMCS, which
159 * can't be done if there isn't a current VMCS.
160 */
161 if (vmx->nested.current_vmptr == -1ull && !vmx->nested.hv_evmcs)
162 return nested_vmx_failInvalid(vcpu);
163
164 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
165 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
166 X86_EFLAGS_SF | X86_EFLAGS_OF))
167 | X86_EFLAGS_ZF);
168 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
169 /*
170 * We don't need to force a shadow sync because
171 * VM_INSTRUCTION_ERROR is not shadowed
172 */
173 return kvm_skip_emulated_instruction(vcpu);
174}
175
176static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
177{
178 /* TODO: not to reset guest simply here. */
179 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
180 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
181}
182
183static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
184{
185 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
186 vmcs_write64(VMCS_LINK_POINTER, -1ull);
187}
188
189static inline void nested_release_evmcs(struct kvm_vcpu *vcpu)
190{
191 struct vcpu_vmx *vmx = to_vmx(vcpu);
192
193 if (!vmx->nested.hv_evmcs)
194 return;
195
KarimAllah Ahmeddee9c042019-01-31 21:24:42 +0100196 kvm_vcpu_unmap(vcpu, &vmx->nested.hv_evmcs_map, true);
Sean Christopherson55d23752018-12-03 13:53:18 -0800197 vmx->nested.hv_evmcs_vmptr = -1ull;
Sean Christopherson55d23752018-12-03 13:53:18 -0800198 vmx->nested.hv_evmcs = NULL;
199}
200
201/*
202 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
203 * just stops using VMX.
204 */
205static void free_nested(struct kvm_vcpu *vcpu)
206{
207 struct vcpu_vmx *vmx = to_vmx(vcpu);
208
209 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
210 return;
211
212 vmx->nested.vmxon = false;
213 vmx->nested.smm.vmxon = false;
214 free_vpid(vmx->nested.vpid02);
215 vmx->nested.posted_intr_nv = -1;
216 vmx->nested.current_vmptr = -1ull;
217 if (enable_shadow_vmcs) {
218 vmx_disable_shadow_vmcs(vmx);
219 vmcs_clear(vmx->vmcs01.shadow_vmcs);
220 free_vmcs(vmx->vmcs01.shadow_vmcs);
221 vmx->vmcs01.shadow_vmcs = NULL;
222 }
223 kfree(vmx->nested.cached_vmcs12);
224 kfree(vmx->nested.cached_shadow_vmcs12);
225 /* Unpin physical memory we referred to in the vmcs02 */
226 if (vmx->nested.apic_access_page) {
227 kvm_release_page_dirty(vmx->nested.apic_access_page);
228 vmx->nested.apic_access_page = NULL;
229 }
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +0100230 kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map, true);
KarimAllah Ahmed3278e042019-01-31 21:24:38 +0100231 kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map, true);
232 vmx->nested.pi_desc = NULL;
Sean Christopherson55d23752018-12-03 13:53:18 -0800233
234 kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
235
236 nested_release_evmcs(vcpu);
237
238 free_loaded_vmcs(&vmx->nested.vmcs02);
239}
240
241static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
242{
243 struct vcpu_vmx *vmx = to_vmx(vcpu);
244 int cpu;
245
246 if (vmx->loaded_vmcs == vmcs)
247 return;
248
249 cpu = get_cpu();
250 vmx_vcpu_put(vcpu);
251 vmx->loaded_vmcs = vmcs;
252 vmx_vcpu_load(vcpu, cpu);
253 put_cpu();
254
255 vm_entry_controls_reset_shadow(vmx);
256 vm_exit_controls_reset_shadow(vmx);
257 vmx_segment_cache_clear(vmx);
258}
259
260/*
261 * Ensure that the current vmcs of the logical processor is the
262 * vmcs01 of the vcpu before calling free_nested().
263 */
264void nested_vmx_free_vcpu(struct kvm_vcpu *vcpu)
265{
266 vcpu_load(vcpu);
Paolo Bonzinib4b65b52019-01-29 19:12:35 +0100267 vmx_leave_nested(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -0800268 vmx_switch_vmcs(vcpu, &to_vmx(vcpu)->vmcs01);
269 free_nested(vcpu);
270 vcpu_put(vcpu);
271}
272
273static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
274 struct x86_exception *fault)
275{
276 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
277 struct vcpu_vmx *vmx = to_vmx(vcpu);
278 u32 exit_reason;
279 unsigned long exit_qualification = vcpu->arch.exit_qualification;
280
281 if (vmx->nested.pml_full) {
282 exit_reason = EXIT_REASON_PML_FULL;
283 vmx->nested.pml_full = false;
284 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
285 } else if (fault->error_code & PFERR_RSVD_MASK)
286 exit_reason = EXIT_REASON_EPT_MISCONFIG;
287 else
288 exit_reason = EXIT_REASON_EPT_VIOLATION;
289
290 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
291 vmcs12->guest_physical_address = fault->address;
292}
293
294static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
295{
296 WARN_ON(mmu_is_nested(vcpu));
297
298 vcpu->arch.mmu = &vcpu->arch.guest_mmu;
299 kvm_init_shadow_ept_mmu(vcpu,
300 to_vmx(vcpu)->nested.msrs.ept_caps &
301 VMX_EPT_EXECUTE_ONLY_BIT,
302 nested_ept_ad_enabled(vcpu),
303 nested_ept_get_cr3(vcpu));
304 vcpu->arch.mmu->set_cr3 = vmx_set_cr3;
305 vcpu->arch.mmu->get_cr3 = nested_ept_get_cr3;
306 vcpu->arch.mmu->inject_page_fault = nested_ept_inject_page_fault;
307 vcpu->arch.mmu->get_pdptr = kvm_pdptr_read;
308
309 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
310}
311
312static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
313{
314 vcpu->arch.mmu = &vcpu->arch.root_mmu;
315 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
316}
317
318static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
319 u16 error_code)
320{
321 bool inequality, bit;
322
323 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
324 inequality =
325 (error_code & vmcs12->page_fault_error_code_mask) !=
326 vmcs12->page_fault_error_code_match;
327 return inequality ^ bit;
328}
329
330
331/*
332 * KVM wants to inject page-faults which it got to the guest. This function
333 * checks whether in a nested guest, we need to inject them to L1 or L2.
334 */
335static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
336{
337 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
338 unsigned int nr = vcpu->arch.exception.nr;
339 bool has_payload = vcpu->arch.exception.has_payload;
340 unsigned long payload = vcpu->arch.exception.payload;
341
342 if (nr == PF_VECTOR) {
343 if (vcpu->arch.exception.nested_apf) {
344 *exit_qual = vcpu->arch.apf.nested_apf_token;
345 return 1;
346 }
347 if (nested_vmx_is_page_fault_vmexit(vmcs12,
348 vcpu->arch.exception.error_code)) {
349 *exit_qual = has_payload ? payload : vcpu->arch.cr2;
350 return 1;
351 }
352 } else if (vmcs12->exception_bitmap & (1u << nr)) {
353 if (nr == DB_VECTOR) {
354 if (!has_payload) {
355 payload = vcpu->arch.dr6;
356 payload &= ~(DR6_FIXED_1 | DR6_BT);
357 payload ^= DR6_RTM;
358 }
359 *exit_qual = payload;
360 } else
361 *exit_qual = 0;
362 return 1;
363 }
364
365 return 0;
366}
367
368
369static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
370 struct x86_exception *fault)
371{
372 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
373
374 WARN_ON(!is_guest_mode(vcpu));
375
376 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
377 !to_vmx(vcpu)->nested.nested_run_pending) {
378 vmcs12->vm_exit_intr_error_code = fault->error_code;
379 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
380 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
381 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
382 fault->address);
383 } else {
384 kvm_inject_page_fault(vcpu, fault);
385 }
386}
387
388static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
389{
390 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
391}
392
393static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
394 struct vmcs12 *vmcs12)
395{
396 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
397 return 0;
398
399 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
400 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
401 return -EINVAL;
402
403 return 0;
404}
405
406static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
407 struct vmcs12 *vmcs12)
408{
409 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
410 return 0;
411
412 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
413 return -EINVAL;
414
415 return 0;
416}
417
418static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
419 struct vmcs12 *vmcs12)
420{
421 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
422 return 0;
423
424 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
425 return -EINVAL;
426
427 return 0;
428}
429
430/*
431 * Check if MSR is intercepted for L01 MSR bitmap.
432 */
433static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
434{
435 unsigned long *msr_bitmap;
436 int f = sizeof(unsigned long);
437
438 if (!cpu_has_vmx_msr_bitmap())
439 return true;
440
441 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
442
443 if (msr <= 0x1fff) {
444 return !!test_bit(msr, msr_bitmap + 0x800 / f);
445 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
446 msr &= 0x1fff;
447 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
448 }
449
450 return true;
451}
452
453/*
454 * If a msr is allowed by L0, we should check whether it is allowed by L1.
455 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
456 */
457static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
458 unsigned long *msr_bitmap_nested,
459 u32 msr, int type)
460{
461 int f = sizeof(unsigned long);
462
463 /*
464 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
465 * have the write-low and read-high bitmap offsets the wrong way round.
466 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
467 */
468 if (msr <= 0x1fff) {
469 if (type & MSR_TYPE_R &&
470 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
471 /* read-low */
472 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
473
474 if (type & MSR_TYPE_W &&
475 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
476 /* write-low */
477 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
478
479 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
480 msr &= 0x1fff;
481 if (type & MSR_TYPE_R &&
482 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
483 /* read-high */
484 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
485
486 if (type & MSR_TYPE_W &&
487 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
488 /* write-high */
489 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
490
491 }
492}
493
Marc Orracff7842019-04-01 23:55:59 -0700494static inline void enable_x2apic_msr_intercepts(unsigned long *msr_bitmap) {
495 int msr;
496
497 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
498 unsigned word = msr / BITS_PER_LONG;
499
500 msr_bitmap[word] = ~0;
501 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
502 }
503}
504
Sean Christopherson55d23752018-12-03 13:53:18 -0800505/*
506 * Merge L0's and L1's MSR bitmap, return false to indicate that
507 * we do not use the hardware.
508 */
509static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
510 struct vmcs12 *vmcs12)
511{
512 int msr;
Sean Christopherson55d23752018-12-03 13:53:18 -0800513 unsigned long *msr_bitmap_l1;
514 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
KarimAllah Ahmed31f0b6c2019-01-31 21:24:36 +0100515 struct kvm_host_map *map = &to_vmx(vcpu)->nested.msr_bitmap_map;
Sean Christopherson55d23752018-12-03 13:53:18 -0800516
517 /* Nothing to do if the MSR bitmap is not in use. */
518 if (!cpu_has_vmx_msr_bitmap() ||
519 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
520 return false;
521
KarimAllah Ahmed31f0b6c2019-01-31 21:24:36 +0100522 if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->msr_bitmap), map))
Sean Christopherson55d23752018-12-03 13:53:18 -0800523 return false;
524
KarimAllah Ahmed31f0b6c2019-01-31 21:24:36 +0100525 msr_bitmap_l1 = (unsigned long *)map->hva;
Sean Christopherson55d23752018-12-03 13:53:18 -0800526
Marc Orracff7842019-04-01 23:55:59 -0700527 /*
528 * To keep the control flow simple, pay eight 8-byte writes (sixteen
529 * 4-byte writes on 32-bit systems) up front to enable intercepts for
530 * the x2APIC MSR range and selectively disable them below.
531 */
532 enable_x2apic_msr_intercepts(msr_bitmap_l0);
Sean Christopherson55d23752018-12-03 13:53:18 -0800533
Marc Orracff7842019-04-01 23:55:59 -0700534 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
535 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
536 /*
537 * L0 need not intercept reads for MSRs between 0x800
538 * and 0x8ff, it just lets the processor take the value
539 * from the virtual-APIC page; take those 256 bits
540 * directly from the L1 bitmap.
541 */
542 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
543 unsigned word = msr / BITS_PER_LONG;
544
545 msr_bitmap_l0[word] = msr_bitmap_l1[word];
546 }
547 }
548
Sean Christopherson55d23752018-12-03 13:53:18 -0800549 nested_vmx_disable_intercept_for_msr(
550 msr_bitmap_l1, msr_bitmap_l0,
Marc Orracff7842019-04-01 23:55:59 -0700551 X2APIC_MSR(APIC_TASKPRI),
Marc Orrc73f4c92019-04-01 23:56:00 -0700552 MSR_TYPE_R | MSR_TYPE_W);
Marc Orracff7842019-04-01 23:55:59 -0700553
554 if (nested_cpu_has_vid(vmcs12)) {
555 nested_vmx_disable_intercept_for_msr(
556 msr_bitmap_l1, msr_bitmap_l0,
557 X2APIC_MSR(APIC_EOI),
558 MSR_TYPE_W);
559 nested_vmx_disable_intercept_for_msr(
560 msr_bitmap_l1, msr_bitmap_l0,
561 X2APIC_MSR(APIC_SELF_IPI),
562 MSR_TYPE_W);
563 }
Sean Christopherson55d23752018-12-03 13:53:18 -0800564 }
565
Sean Christophersond69129b2019-05-08 07:32:15 -0700566 /* KVM unconditionally exposes the FS/GS base MSRs to L1. */
567 nested_vmx_disable_intercept_for_msr(msr_bitmap_l1, msr_bitmap_l0,
568 MSR_FS_BASE, MSR_TYPE_RW);
569
570 nested_vmx_disable_intercept_for_msr(msr_bitmap_l1, msr_bitmap_l0,
571 MSR_GS_BASE, MSR_TYPE_RW);
572
573 nested_vmx_disable_intercept_for_msr(msr_bitmap_l1, msr_bitmap_l0,
574 MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
575
576 /*
577 * Checking the L0->L1 bitmap is trying to verify two things:
578 *
579 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
580 * ensures that we do not accidentally generate an L02 MSR bitmap
581 * from the L12 MSR bitmap that is too permissive.
582 * 2. That L1 or L2s have actually used the MSR. This avoids
583 * unnecessarily merging of the bitmap if the MSR is unused. This
584 * works properly because we only update the L01 MSR bitmap lazily.
585 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
586 * updated to reflect this when L1 (or its L2s) actually write to
587 * the MSR.
588 */
589 if (!msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL))
Sean Christopherson55d23752018-12-03 13:53:18 -0800590 nested_vmx_disable_intercept_for_msr(
591 msr_bitmap_l1, msr_bitmap_l0,
592 MSR_IA32_SPEC_CTRL,
593 MSR_TYPE_R | MSR_TYPE_W);
594
Sean Christophersond69129b2019-05-08 07:32:15 -0700595 if (!msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD))
Sean Christopherson55d23752018-12-03 13:53:18 -0800596 nested_vmx_disable_intercept_for_msr(
597 msr_bitmap_l1, msr_bitmap_l0,
598 MSR_IA32_PRED_CMD,
599 MSR_TYPE_W);
600
KarimAllah Ahmed31f0b6c2019-01-31 21:24:36 +0100601 kvm_vcpu_unmap(vcpu, &to_vmx(vcpu)->nested.msr_bitmap_map, false);
Sean Christopherson55d23752018-12-03 13:53:18 -0800602
603 return true;
604}
605
606static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
607 struct vmcs12 *vmcs12)
608{
KarimAllah Ahmed88925302019-01-31 21:24:41 +0100609 struct kvm_host_map map;
Sean Christopherson55d23752018-12-03 13:53:18 -0800610 struct vmcs12 *shadow;
Sean Christopherson55d23752018-12-03 13:53:18 -0800611
612 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
613 vmcs12->vmcs_link_pointer == -1ull)
614 return;
615
616 shadow = get_shadow_vmcs12(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -0800617
KarimAllah Ahmed88925302019-01-31 21:24:41 +0100618 if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->vmcs_link_pointer), &map))
619 return;
Sean Christopherson55d23752018-12-03 13:53:18 -0800620
KarimAllah Ahmed88925302019-01-31 21:24:41 +0100621 memcpy(shadow, map.hva, VMCS12_SIZE);
622 kvm_vcpu_unmap(vcpu, &map, false);
Sean Christopherson55d23752018-12-03 13:53:18 -0800623}
624
625static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
626 struct vmcs12 *vmcs12)
627{
628 struct vcpu_vmx *vmx = to_vmx(vcpu);
629
630 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
631 vmcs12->vmcs_link_pointer == -1ull)
632 return;
633
634 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
635 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
636}
637
638/*
639 * In nested virtualization, check if L1 has set
640 * VM_EXIT_ACK_INTR_ON_EXIT
641 */
642static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
643{
644 return get_vmcs12(vcpu)->vm_exit_controls &
645 VM_EXIT_ACK_INTR_ON_EXIT;
646}
647
648static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
649{
650 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
651}
652
653static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
654 struct vmcs12 *vmcs12)
655{
656 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
657 !page_address_valid(vcpu, vmcs12->apic_access_addr))
658 return -EINVAL;
659 else
660 return 0;
661}
662
663static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
664 struct vmcs12 *vmcs12)
665{
666 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
667 !nested_cpu_has_apic_reg_virt(vmcs12) &&
668 !nested_cpu_has_vid(vmcs12) &&
669 !nested_cpu_has_posted_intr(vmcs12))
670 return 0;
671
672 /*
673 * If virtualize x2apic mode is enabled,
674 * virtualize apic access must be disabled.
675 */
676 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
677 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
678 return -EINVAL;
679
680 /*
681 * If virtual interrupt delivery is enabled,
682 * we must exit on external interrupts.
683 */
684 if (nested_cpu_has_vid(vmcs12) &&
685 !nested_exit_on_intr(vcpu))
686 return -EINVAL;
687
688 /*
689 * bits 15:8 should be zero in posted_intr_nv,
690 * the descriptor address has been already checked
691 * in nested_get_vmcs12_pages.
692 *
693 * bits 5:0 of posted_intr_desc_addr should be zero.
694 */
695 if (nested_cpu_has_posted_intr(vmcs12) &&
696 (!nested_cpu_has_vid(vmcs12) ||
697 !nested_exit_intr_ack_set(vcpu) ||
698 (vmcs12->posted_intr_nv & 0xff00) ||
699 (vmcs12->posted_intr_desc_addr & 0x3f) ||
700 (vmcs12->posted_intr_desc_addr >> cpuid_maxphyaddr(vcpu))))
701 return -EINVAL;
702
703 /* tpr shadow is needed by all apicv features. */
704 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
705 return -EINVAL;
706
707 return 0;
708}
709
710static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
Sean Christophersonf9b245e2018-12-12 13:30:08 -0500711 u32 count, u64 addr)
Sean Christopherson55d23752018-12-03 13:53:18 -0800712{
Sean Christopherson55d23752018-12-03 13:53:18 -0800713 int maxphyaddr;
Sean Christopherson55d23752018-12-03 13:53:18 -0800714
Sean Christopherson55d23752018-12-03 13:53:18 -0800715 if (count == 0)
716 return 0;
717 maxphyaddr = cpuid_maxphyaddr(vcpu);
718 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
Sean Christophersonf9b245e2018-12-12 13:30:08 -0500719 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr)
Sean Christopherson55d23752018-12-03 13:53:18 -0800720 return -EINVAL;
Sean Christophersonf9b245e2018-12-12 13:30:08 -0500721
Sean Christopherson55d23752018-12-03 13:53:18 -0800722 return 0;
723}
724
Krish Sadhukhan61446ba2018-12-12 13:30:09 -0500725static int nested_vmx_check_exit_msr_switch_controls(struct kvm_vcpu *vcpu,
726 struct vmcs12 *vmcs12)
Sean Christopherson55d23752018-12-03 13:53:18 -0800727{
Sean Christophersonf9b245e2018-12-12 13:30:08 -0500728 if (nested_vmx_check_msr_switch(vcpu, vmcs12->vm_exit_msr_load_count,
729 vmcs12->vm_exit_msr_load_addr) ||
730 nested_vmx_check_msr_switch(vcpu, vmcs12->vm_exit_msr_store_count,
Krish Sadhukhan61446ba2018-12-12 13:30:09 -0500731 vmcs12->vm_exit_msr_store_addr))
Sean Christopherson55d23752018-12-03 13:53:18 -0800732 return -EINVAL;
Sean Christophersonf9b245e2018-12-12 13:30:08 -0500733
Sean Christopherson55d23752018-12-03 13:53:18 -0800734 return 0;
735}
736
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -0500737static int nested_vmx_check_entry_msr_switch_controls(struct kvm_vcpu *vcpu,
738 struct vmcs12 *vmcs12)
Krish Sadhukhan61446ba2018-12-12 13:30:09 -0500739{
740 if (nested_vmx_check_msr_switch(vcpu, vmcs12->vm_entry_msr_load_count,
741 vmcs12->vm_entry_msr_load_addr))
742 return -EINVAL;
743
744 return 0;
745}
746
Sean Christopherson55d23752018-12-03 13:53:18 -0800747static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
748 struct vmcs12 *vmcs12)
749{
750 if (!nested_cpu_has_pml(vmcs12))
751 return 0;
752
753 if (!nested_cpu_has_ept(vmcs12) ||
754 !page_address_valid(vcpu, vmcs12->pml_address))
755 return -EINVAL;
756
757 return 0;
758}
759
760static int nested_vmx_check_unrestricted_guest_controls(struct kvm_vcpu *vcpu,
761 struct vmcs12 *vmcs12)
762{
763 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST) &&
764 !nested_cpu_has_ept(vmcs12))
765 return -EINVAL;
766 return 0;
767}
768
769static int nested_vmx_check_mode_based_ept_exec_controls(struct kvm_vcpu *vcpu,
770 struct vmcs12 *vmcs12)
771{
772 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_MODE_BASED_EPT_EXEC) &&
773 !nested_cpu_has_ept(vmcs12))
774 return -EINVAL;
775 return 0;
776}
777
778static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
779 struct vmcs12 *vmcs12)
780{
781 if (!nested_cpu_has_shadow_vmcs(vmcs12))
782 return 0;
783
784 if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) ||
785 !page_address_valid(vcpu, vmcs12->vmwrite_bitmap))
786 return -EINVAL;
787
788 return 0;
789}
790
791static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
792 struct vmx_msr_entry *e)
793{
794 /* x2APIC MSR accesses are not allowed */
795 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
796 return -EINVAL;
797 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
798 e->index == MSR_IA32_UCODE_REV)
799 return -EINVAL;
800 if (e->reserved != 0)
801 return -EINVAL;
802 return 0;
803}
804
805static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
806 struct vmx_msr_entry *e)
807{
808 if (e->index == MSR_FS_BASE ||
809 e->index == MSR_GS_BASE ||
810 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
811 nested_vmx_msr_check_common(vcpu, e))
812 return -EINVAL;
813 return 0;
814}
815
816static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
817 struct vmx_msr_entry *e)
818{
819 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
820 nested_vmx_msr_check_common(vcpu, e))
821 return -EINVAL;
822 return 0;
823}
824
825/*
826 * Load guest's/host's msr at nested entry/exit.
827 * return 0 for success, entry index for failure.
828 */
829static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
830{
831 u32 i;
832 struct vmx_msr_entry e;
833 struct msr_data msr;
834
835 msr.host_initiated = false;
836 for (i = 0; i < count; i++) {
837 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
838 &e, sizeof(e))) {
839 pr_debug_ratelimited(
840 "%s cannot read MSR entry (%u, 0x%08llx)\n",
841 __func__, i, gpa + i * sizeof(e));
842 goto fail;
843 }
844 if (nested_vmx_load_msr_check(vcpu, &e)) {
845 pr_debug_ratelimited(
846 "%s check failed (%u, 0x%x, 0x%x)\n",
847 __func__, i, e.index, e.reserved);
848 goto fail;
849 }
850 msr.index = e.index;
851 msr.data = e.value;
852 if (kvm_set_msr(vcpu, &msr)) {
853 pr_debug_ratelimited(
854 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
855 __func__, i, e.index, e.value);
856 goto fail;
857 }
858 }
859 return 0;
860fail:
861 return i + 1;
862}
863
864static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
865{
866 u32 i;
867 struct vmx_msr_entry e;
868
869 for (i = 0; i < count; i++) {
870 struct msr_data msr_info;
871 if (kvm_vcpu_read_guest(vcpu,
872 gpa + i * sizeof(e),
873 &e, 2 * sizeof(u32))) {
874 pr_debug_ratelimited(
875 "%s cannot read MSR entry (%u, 0x%08llx)\n",
876 __func__, i, gpa + i * sizeof(e));
877 return -EINVAL;
878 }
879 if (nested_vmx_store_msr_check(vcpu, &e)) {
880 pr_debug_ratelimited(
881 "%s check failed (%u, 0x%x, 0x%x)\n",
882 __func__, i, e.index, e.reserved);
883 return -EINVAL;
884 }
885 msr_info.host_initiated = false;
886 msr_info.index = e.index;
887 if (kvm_get_msr(vcpu, &msr_info)) {
888 pr_debug_ratelimited(
889 "%s cannot read MSR (%u, 0x%x)\n",
890 __func__, i, e.index);
891 return -EINVAL;
892 }
893 if (kvm_vcpu_write_guest(vcpu,
894 gpa + i * sizeof(e) +
895 offsetof(struct vmx_msr_entry, value),
896 &msr_info.data, sizeof(msr_info.data))) {
897 pr_debug_ratelimited(
898 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
899 __func__, i, e.index, msr_info.data);
900 return -EINVAL;
901 }
902 }
903 return 0;
904}
905
906static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
907{
908 unsigned long invalid_mask;
909
910 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
911 return (val & invalid_mask) == 0;
912}
913
914/*
915 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
916 * emulating VM entry into a guest with EPT enabled.
917 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
918 * is assigned to entry_failure_code on failure.
919 */
920static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
921 u32 *entry_failure_code)
922{
923 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
924 if (!nested_cr3_valid(vcpu, cr3)) {
925 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christophersonc80add02019-04-11 12:18:09 -0700926 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -0800927 }
928
929 /*
930 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
931 * must not be dereferenced.
932 */
933 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
934 !nested_ept) {
935 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
936 *entry_failure_code = ENTRY_FAIL_PDPTE;
Sean Christophersonc80add02019-04-11 12:18:09 -0700937 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -0800938 }
939 }
940 }
941
942 if (!nested_ept)
943 kvm_mmu_new_cr3(vcpu, cr3, false);
944
945 vcpu->arch.cr3 = cr3;
946 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
947
948 kvm_init_mmu(vcpu, false);
949
950 return 0;
951}
952
953/*
954 * Returns if KVM is able to config CPU to tag TLB entries
955 * populated by L2 differently than TLB entries populated
956 * by L1.
957 *
958 * If L1 uses EPT, then TLB entries are tagged with different EPTP.
959 *
960 * If L1 uses VPID and we allocated a vpid02, TLB entries are tagged
961 * with different VPID (L1 entries are tagged with vmx->vpid
962 * while L2 entries are tagged with vmx->nested.vpid02).
963 */
964static bool nested_has_guest_tlb_tag(struct kvm_vcpu *vcpu)
965{
966 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
967
968 return nested_cpu_has_ept(vmcs12) ||
969 (nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02);
970}
971
972static u16 nested_get_vpid02(struct kvm_vcpu *vcpu)
973{
974 struct vcpu_vmx *vmx = to_vmx(vcpu);
975
976 return vmx->nested.vpid02 ? vmx->nested.vpid02 : vmx->vpid;
977}
978
979
980static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
981{
982 return fixed_bits_valid(control, low, high);
983}
984
985static inline u64 vmx_control_msr(u32 low, u32 high)
986{
987 return low | ((u64)high << 32);
988}
989
990static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
991{
992 superset &= mask;
993 subset &= mask;
994
995 return (superset | subset) == superset;
996}
997
998static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
999{
1000 const u64 feature_and_reserved =
1001 /* feature (except bit 48; see below) */
1002 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
1003 /* reserved */
1004 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
1005 u64 vmx_basic = vmx->nested.msrs.basic;
1006
1007 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
1008 return -EINVAL;
1009
1010 /*
1011 * KVM does not emulate a version of VMX that constrains physical
1012 * addresses of VMX structures (e.g. VMCS) to 32-bits.
1013 */
1014 if (data & BIT_ULL(48))
1015 return -EINVAL;
1016
1017 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
1018 vmx_basic_vmcs_revision_id(data))
1019 return -EINVAL;
1020
1021 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
1022 return -EINVAL;
1023
1024 vmx->nested.msrs.basic = data;
1025 return 0;
1026}
1027
1028static int
1029vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
1030{
1031 u64 supported;
1032 u32 *lowp, *highp;
1033
1034 switch (msr_index) {
1035 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
1036 lowp = &vmx->nested.msrs.pinbased_ctls_low;
1037 highp = &vmx->nested.msrs.pinbased_ctls_high;
1038 break;
1039 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
1040 lowp = &vmx->nested.msrs.procbased_ctls_low;
1041 highp = &vmx->nested.msrs.procbased_ctls_high;
1042 break;
1043 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
1044 lowp = &vmx->nested.msrs.exit_ctls_low;
1045 highp = &vmx->nested.msrs.exit_ctls_high;
1046 break;
1047 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
1048 lowp = &vmx->nested.msrs.entry_ctls_low;
1049 highp = &vmx->nested.msrs.entry_ctls_high;
1050 break;
1051 case MSR_IA32_VMX_PROCBASED_CTLS2:
1052 lowp = &vmx->nested.msrs.secondary_ctls_low;
1053 highp = &vmx->nested.msrs.secondary_ctls_high;
1054 break;
1055 default:
1056 BUG();
1057 }
1058
1059 supported = vmx_control_msr(*lowp, *highp);
1060
1061 /* Check must-be-1 bits are still 1. */
1062 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
1063 return -EINVAL;
1064
1065 /* Check must-be-0 bits are still 0. */
1066 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
1067 return -EINVAL;
1068
1069 *lowp = data;
1070 *highp = data >> 32;
1071 return 0;
1072}
1073
1074static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
1075{
1076 const u64 feature_and_reserved_bits =
1077 /* feature */
1078 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
1079 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
1080 /* reserved */
1081 GENMASK_ULL(13, 9) | BIT_ULL(31);
1082 u64 vmx_misc;
1083
1084 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
1085 vmx->nested.msrs.misc_high);
1086
1087 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
1088 return -EINVAL;
1089
1090 if ((vmx->nested.msrs.pinbased_ctls_high &
1091 PIN_BASED_VMX_PREEMPTION_TIMER) &&
1092 vmx_misc_preemption_timer_rate(data) !=
1093 vmx_misc_preemption_timer_rate(vmx_misc))
1094 return -EINVAL;
1095
1096 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
1097 return -EINVAL;
1098
1099 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
1100 return -EINVAL;
1101
1102 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
1103 return -EINVAL;
1104
1105 vmx->nested.msrs.misc_low = data;
1106 vmx->nested.msrs.misc_high = data >> 32;
1107
1108 /*
1109 * If L1 has read-only VM-exit information fields, use the
1110 * less permissive vmx_vmwrite_bitmap to specify write
1111 * permissions for the shadow VMCS.
1112 */
1113 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
1114 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
1115
1116 return 0;
1117}
1118
1119static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
1120{
1121 u64 vmx_ept_vpid_cap;
1122
1123 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
1124 vmx->nested.msrs.vpid_caps);
1125
1126 /* Every bit is either reserved or a feature bit. */
1127 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
1128 return -EINVAL;
1129
1130 vmx->nested.msrs.ept_caps = data;
1131 vmx->nested.msrs.vpid_caps = data >> 32;
1132 return 0;
1133}
1134
1135static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
1136{
1137 u64 *msr;
1138
1139 switch (msr_index) {
1140 case MSR_IA32_VMX_CR0_FIXED0:
1141 msr = &vmx->nested.msrs.cr0_fixed0;
1142 break;
1143 case MSR_IA32_VMX_CR4_FIXED0:
1144 msr = &vmx->nested.msrs.cr4_fixed0;
1145 break;
1146 default:
1147 BUG();
1148 }
1149
1150 /*
1151 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
1152 * must be 1 in the restored value.
1153 */
1154 if (!is_bitwise_subset(data, *msr, -1ULL))
1155 return -EINVAL;
1156
1157 *msr = data;
1158 return 0;
1159}
1160
1161/*
1162 * Called when userspace is restoring VMX MSRs.
1163 *
1164 * Returns 0 on success, non-0 otherwise.
1165 */
1166int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1167{
1168 struct vcpu_vmx *vmx = to_vmx(vcpu);
1169
1170 /*
1171 * Don't allow changes to the VMX capability MSRs while the vCPU
1172 * is in VMX operation.
1173 */
1174 if (vmx->nested.vmxon)
1175 return -EBUSY;
1176
1177 switch (msr_index) {
1178 case MSR_IA32_VMX_BASIC:
1179 return vmx_restore_vmx_basic(vmx, data);
1180 case MSR_IA32_VMX_PINBASED_CTLS:
1181 case MSR_IA32_VMX_PROCBASED_CTLS:
1182 case MSR_IA32_VMX_EXIT_CTLS:
1183 case MSR_IA32_VMX_ENTRY_CTLS:
1184 /*
1185 * The "non-true" VMX capability MSRs are generated from the
1186 * "true" MSRs, so we do not support restoring them directly.
1187 *
1188 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
1189 * should restore the "true" MSRs with the must-be-1 bits
1190 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
1191 * DEFAULT SETTINGS".
1192 */
1193 return -EINVAL;
1194 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
1195 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
1196 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
1197 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
1198 case MSR_IA32_VMX_PROCBASED_CTLS2:
1199 return vmx_restore_control_msr(vmx, msr_index, data);
1200 case MSR_IA32_VMX_MISC:
1201 return vmx_restore_vmx_misc(vmx, data);
1202 case MSR_IA32_VMX_CR0_FIXED0:
1203 case MSR_IA32_VMX_CR4_FIXED0:
1204 return vmx_restore_fixed0_msr(vmx, msr_index, data);
1205 case MSR_IA32_VMX_CR0_FIXED1:
1206 case MSR_IA32_VMX_CR4_FIXED1:
1207 /*
1208 * These MSRs are generated based on the vCPU's CPUID, so we
1209 * do not support restoring them directly.
1210 */
1211 return -EINVAL;
1212 case MSR_IA32_VMX_EPT_VPID_CAP:
1213 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
1214 case MSR_IA32_VMX_VMCS_ENUM:
1215 vmx->nested.msrs.vmcs_enum = data;
1216 return 0;
1217 default:
1218 /*
1219 * The rest of the VMX capability MSRs do not support restore.
1220 */
1221 return -EINVAL;
1222 }
1223}
1224
1225/* Returns 0 on success, non-0 otherwise. */
1226int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
1227{
1228 switch (msr_index) {
1229 case MSR_IA32_VMX_BASIC:
1230 *pdata = msrs->basic;
1231 break;
1232 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
1233 case MSR_IA32_VMX_PINBASED_CTLS:
1234 *pdata = vmx_control_msr(
1235 msrs->pinbased_ctls_low,
1236 msrs->pinbased_ctls_high);
1237 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
1238 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
1239 break;
1240 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
1241 case MSR_IA32_VMX_PROCBASED_CTLS:
1242 *pdata = vmx_control_msr(
1243 msrs->procbased_ctls_low,
1244 msrs->procbased_ctls_high);
1245 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
1246 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
1247 break;
1248 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
1249 case MSR_IA32_VMX_EXIT_CTLS:
1250 *pdata = vmx_control_msr(
1251 msrs->exit_ctls_low,
1252 msrs->exit_ctls_high);
1253 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
1254 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
1255 break;
1256 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
1257 case MSR_IA32_VMX_ENTRY_CTLS:
1258 *pdata = vmx_control_msr(
1259 msrs->entry_ctls_low,
1260 msrs->entry_ctls_high);
1261 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
1262 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
1263 break;
1264 case MSR_IA32_VMX_MISC:
1265 *pdata = vmx_control_msr(
1266 msrs->misc_low,
1267 msrs->misc_high);
1268 break;
1269 case MSR_IA32_VMX_CR0_FIXED0:
1270 *pdata = msrs->cr0_fixed0;
1271 break;
1272 case MSR_IA32_VMX_CR0_FIXED1:
1273 *pdata = msrs->cr0_fixed1;
1274 break;
1275 case MSR_IA32_VMX_CR4_FIXED0:
1276 *pdata = msrs->cr4_fixed0;
1277 break;
1278 case MSR_IA32_VMX_CR4_FIXED1:
1279 *pdata = msrs->cr4_fixed1;
1280 break;
1281 case MSR_IA32_VMX_VMCS_ENUM:
1282 *pdata = msrs->vmcs_enum;
1283 break;
1284 case MSR_IA32_VMX_PROCBASED_CTLS2:
1285 *pdata = vmx_control_msr(
1286 msrs->secondary_ctls_low,
1287 msrs->secondary_ctls_high);
1288 break;
1289 case MSR_IA32_VMX_EPT_VPID_CAP:
1290 *pdata = msrs->ept_caps |
1291 ((u64)msrs->vpid_caps << 32);
1292 break;
1293 case MSR_IA32_VMX_VMFUNC:
1294 *pdata = msrs->vmfunc_controls;
1295 break;
1296 default:
1297 return 1;
1298 }
1299
1300 return 0;
1301}
1302
1303/*
1304 * Copy the writable VMCS shadow fields back to the VMCS12, in case
1305 * they have been modified by the L1 guest. Note that the "read-only"
1306 * VM-exit information fields are actually writable if the vCPU is
1307 * configured to support "VMWRITE to any supported field in the VMCS."
1308 */
1309static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
1310{
1311 const u16 *fields[] = {
1312 shadow_read_write_fields,
1313 shadow_read_only_fields
1314 };
1315 const int max_fields[] = {
1316 max_shadow_read_write_fields,
1317 max_shadow_read_only_fields
1318 };
1319 int i, q;
1320 unsigned long field;
1321 u64 field_value;
1322 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
1323
1324 preempt_disable();
1325
1326 vmcs_load(shadow_vmcs);
1327
1328 for (q = 0; q < ARRAY_SIZE(fields); q++) {
1329 for (i = 0; i < max_fields[q]; i++) {
1330 field = fields[q][i];
1331 field_value = __vmcs_readl(field);
1332 vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value);
1333 }
1334 /*
1335 * Skip the VM-exit information fields if they are read-only.
1336 */
1337 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
1338 break;
1339 }
1340
1341 vmcs_clear(shadow_vmcs);
1342 vmcs_load(vmx->loaded_vmcs->vmcs);
1343
1344 preempt_enable();
1345}
1346
1347static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
1348{
1349 const u16 *fields[] = {
1350 shadow_read_write_fields,
1351 shadow_read_only_fields
1352 };
1353 const int max_fields[] = {
1354 max_shadow_read_write_fields,
1355 max_shadow_read_only_fields
1356 };
1357 int i, q;
1358 unsigned long field;
1359 u64 field_value = 0;
1360 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
1361
1362 vmcs_load(shadow_vmcs);
1363
1364 for (q = 0; q < ARRAY_SIZE(fields); q++) {
1365 for (i = 0; i < max_fields[q]; i++) {
1366 field = fields[q][i];
1367 vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value);
1368 __vmcs_writel(field, field_value);
1369 }
1370 }
1371
1372 vmcs_clear(shadow_vmcs);
1373 vmcs_load(vmx->loaded_vmcs->vmcs);
1374}
1375
1376static int copy_enlightened_to_vmcs12(struct vcpu_vmx *vmx)
1377{
1378 struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12;
1379 struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
1380
1381 /* HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE */
1382 vmcs12->tpr_threshold = evmcs->tpr_threshold;
1383 vmcs12->guest_rip = evmcs->guest_rip;
1384
1385 if (unlikely(!(evmcs->hv_clean_fields &
1386 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC))) {
1387 vmcs12->guest_rsp = evmcs->guest_rsp;
1388 vmcs12->guest_rflags = evmcs->guest_rflags;
1389 vmcs12->guest_interruptibility_info =
1390 evmcs->guest_interruptibility_info;
1391 }
1392
1393 if (unlikely(!(evmcs->hv_clean_fields &
1394 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC))) {
1395 vmcs12->cpu_based_vm_exec_control =
1396 evmcs->cpu_based_vm_exec_control;
1397 }
1398
1399 if (unlikely(!(evmcs->hv_clean_fields &
1400 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC))) {
1401 vmcs12->exception_bitmap = evmcs->exception_bitmap;
1402 }
1403
1404 if (unlikely(!(evmcs->hv_clean_fields &
1405 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_ENTRY))) {
1406 vmcs12->vm_entry_controls = evmcs->vm_entry_controls;
1407 }
1408
1409 if (unlikely(!(evmcs->hv_clean_fields &
1410 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT))) {
1411 vmcs12->vm_entry_intr_info_field =
1412 evmcs->vm_entry_intr_info_field;
1413 vmcs12->vm_entry_exception_error_code =
1414 evmcs->vm_entry_exception_error_code;
1415 vmcs12->vm_entry_instruction_len =
1416 evmcs->vm_entry_instruction_len;
1417 }
1418
1419 if (unlikely(!(evmcs->hv_clean_fields &
1420 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1))) {
1421 vmcs12->host_ia32_pat = evmcs->host_ia32_pat;
1422 vmcs12->host_ia32_efer = evmcs->host_ia32_efer;
1423 vmcs12->host_cr0 = evmcs->host_cr0;
1424 vmcs12->host_cr3 = evmcs->host_cr3;
1425 vmcs12->host_cr4 = evmcs->host_cr4;
1426 vmcs12->host_ia32_sysenter_esp = evmcs->host_ia32_sysenter_esp;
1427 vmcs12->host_ia32_sysenter_eip = evmcs->host_ia32_sysenter_eip;
1428 vmcs12->host_rip = evmcs->host_rip;
1429 vmcs12->host_ia32_sysenter_cs = evmcs->host_ia32_sysenter_cs;
1430 vmcs12->host_es_selector = evmcs->host_es_selector;
1431 vmcs12->host_cs_selector = evmcs->host_cs_selector;
1432 vmcs12->host_ss_selector = evmcs->host_ss_selector;
1433 vmcs12->host_ds_selector = evmcs->host_ds_selector;
1434 vmcs12->host_fs_selector = evmcs->host_fs_selector;
1435 vmcs12->host_gs_selector = evmcs->host_gs_selector;
1436 vmcs12->host_tr_selector = evmcs->host_tr_selector;
1437 }
1438
1439 if (unlikely(!(evmcs->hv_clean_fields &
1440 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1))) {
1441 vmcs12->pin_based_vm_exec_control =
1442 evmcs->pin_based_vm_exec_control;
1443 vmcs12->vm_exit_controls = evmcs->vm_exit_controls;
1444 vmcs12->secondary_vm_exec_control =
1445 evmcs->secondary_vm_exec_control;
1446 }
1447
1448 if (unlikely(!(evmcs->hv_clean_fields &
1449 HV_VMX_ENLIGHTENED_CLEAN_FIELD_IO_BITMAP))) {
1450 vmcs12->io_bitmap_a = evmcs->io_bitmap_a;
1451 vmcs12->io_bitmap_b = evmcs->io_bitmap_b;
1452 }
1453
1454 if (unlikely(!(evmcs->hv_clean_fields &
1455 HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP))) {
1456 vmcs12->msr_bitmap = evmcs->msr_bitmap;
1457 }
1458
1459 if (unlikely(!(evmcs->hv_clean_fields &
1460 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2))) {
1461 vmcs12->guest_es_base = evmcs->guest_es_base;
1462 vmcs12->guest_cs_base = evmcs->guest_cs_base;
1463 vmcs12->guest_ss_base = evmcs->guest_ss_base;
1464 vmcs12->guest_ds_base = evmcs->guest_ds_base;
1465 vmcs12->guest_fs_base = evmcs->guest_fs_base;
1466 vmcs12->guest_gs_base = evmcs->guest_gs_base;
1467 vmcs12->guest_ldtr_base = evmcs->guest_ldtr_base;
1468 vmcs12->guest_tr_base = evmcs->guest_tr_base;
1469 vmcs12->guest_gdtr_base = evmcs->guest_gdtr_base;
1470 vmcs12->guest_idtr_base = evmcs->guest_idtr_base;
1471 vmcs12->guest_es_limit = evmcs->guest_es_limit;
1472 vmcs12->guest_cs_limit = evmcs->guest_cs_limit;
1473 vmcs12->guest_ss_limit = evmcs->guest_ss_limit;
1474 vmcs12->guest_ds_limit = evmcs->guest_ds_limit;
1475 vmcs12->guest_fs_limit = evmcs->guest_fs_limit;
1476 vmcs12->guest_gs_limit = evmcs->guest_gs_limit;
1477 vmcs12->guest_ldtr_limit = evmcs->guest_ldtr_limit;
1478 vmcs12->guest_tr_limit = evmcs->guest_tr_limit;
1479 vmcs12->guest_gdtr_limit = evmcs->guest_gdtr_limit;
1480 vmcs12->guest_idtr_limit = evmcs->guest_idtr_limit;
1481 vmcs12->guest_es_ar_bytes = evmcs->guest_es_ar_bytes;
1482 vmcs12->guest_cs_ar_bytes = evmcs->guest_cs_ar_bytes;
1483 vmcs12->guest_ss_ar_bytes = evmcs->guest_ss_ar_bytes;
1484 vmcs12->guest_ds_ar_bytes = evmcs->guest_ds_ar_bytes;
1485 vmcs12->guest_fs_ar_bytes = evmcs->guest_fs_ar_bytes;
1486 vmcs12->guest_gs_ar_bytes = evmcs->guest_gs_ar_bytes;
1487 vmcs12->guest_ldtr_ar_bytes = evmcs->guest_ldtr_ar_bytes;
1488 vmcs12->guest_tr_ar_bytes = evmcs->guest_tr_ar_bytes;
1489 vmcs12->guest_es_selector = evmcs->guest_es_selector;
1490 vmcs12->guest_cs_selector = evmcs->guest_cs_selector;
1491 vmcs12->guest_ss_selector = evmcs->guest_ss_selector;
1492 vmcs12->guest_ds_selector = evmcs->guest_ds_selector;
1493 vmcs12->guest_fs_selector = evmcs->guest_fs_selector;
1494 vmcs12->guest_gs_selector = evmcs->guest_gs_selector;
1495 vmcs12->guest_ldtr_selector = evmcs->guest_ldtr_selector;
1496 vmcs12->guest_tr_selector = evmcs->guest_tr_selector;
1497 }
1498
1499 if (unlikely(!(evmcs->hv_clean_fields &
1500 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2))) {
1501 vmcs12->tsc_offset = evmcs->tsc_offset;
1502 vmcs12->virtual_apic_page_addr = evmcs->virtual_apic_page_addr;
1503 vmcs12->xss_exit_bitmap = evmcs->xss_exit_bitmap;
1504 }
1505
1506 if (unlikely(!(evmcs->hv_clean_fields &
1507 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR))) {
1508 vmcs12->cr0_guest_host_mask = evmcs->cr0_guest_host_mask;
1509 vmcs12->cr4_guest_host_mask = evmcs->cr4_guest_host_mask;
1510 vmcs12->cr0_read_shadow = evmcs->cr0_read_shadow;
1511 vmcs12->cr4_read_shadow = evmcs->cr4_read_shadow;
1512 vmcs12->guest_cr0 = evmcs->guest_cr0;
1513 vmcs12->guest_cr3 = evmcs->guest_cr3;
1514 vmcs12->guest_cr4 = evmcs->guest_cr4;
1515 vmcs12->guest_dr7 = evmcs->guest_dr7;
1516 }
1517
1518 if (unlikely(!(evmcs->hv_clean_fields &
1519 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER))) {
1520 vmcs12->host_fs_base = evmcs->host_fs_base;
1521 vmcs12->host_gs_base = evmcs->host_gs_base;
1522 vmcs12->host_tr_base = evmcs->host_tr_base;
1523 vmcs12->host_gdtr_base = evmcs->host_gdtr_base;
1524 vmcs12->host_idtr_base = evmcs->host_idtr_base;
1525 vmcs12->host_rsp = evmcs->host_rsp;
1526 }
1527
1528 if (unlikely(!(evmcs->hv_clean_fields &
1529 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_XLAT))) {
1530 vmcs12->ept_pointer = evmcs->ept_pointer;
1531 vmcs12->virtual_processor_id = evmcs->virtual_processor_id;
1532 }
1533
1534 if (unlikely(!(evmcs->hv_clean_fields &
1535 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1))) {
1536 vmcs12->vmcs_link_pointer = evmcs->vmcs_link_pointer;
1537 vmcs12->guest_ia32_debugctl = evmcs->guest_ia32_debugctl;
1538 vmcs12->guest_ia32_pat = evmcs->guest_ia32_pat;
1539 vmcs12->guest_ia32_efer = evmcs->guest_ia32_efer;
1540 vmcs12->guest_pdptr0 = evmcs->guest_pdptr0;
1541 vmcs12->guest_pdptr1 = evmcs->guest_pdptr1;
1542 vmcs12->guest_pdptr2 = evmcs->guest_pdptr2;
1543 vmcs12->guest_pdptr3 = evmcs->guest_pdptr3;
1544 vmcs12->guest_pending_dbg_exceptions =
1545 evmcs->guest_pending_dbg_exceptions;
1546 vmcs12->guest_sysenter_esp = evmcs->guest_sysenter_esp;
1547 vmcs12->guest_sysenter_eip = evmcs->guest_sysenter_eip;
1548 vmcs12->guest_bndcfgs = evmcs->guest_bndcfgs;
1549 vmcs12->guest_activity_state = evmcs->guest_activity_state;
1550 vmcs12->guest_sysenter_cs = evmcs->guest_sysenter_cs;
1551 }
1552
1553 /*
1554 * Not used?
1555 * vmcs12->vm_exit_msr_store_addr = evmcs->vm_exit_msr_store_addr;
1556 * vmcs12->vm_exit_msr_load_addr = evmcs->vm_exit_msr_load_addr;
1557 * vmcs12->vm_entry_msr_load_addr = evmcs->vm_entry_msr_load_addr;
1558 * vmcs12->cr3_target_value0 = evmcs->cr3_target_value0;
1559 * vmcs12->cr3_target_value1 = evmcs->cr3_target_value1;
1560 * vmcs12->cr3_target_value2 = evmcs->cr3_target_value2;
1561 * vmcs12->cr3_target_value3 = evmcs->cr3_target_value3;
1562 * vmcs12->page_fault_error_code_mask =
1563 * evmcs->page_fault_error_code_mask;
1564 * vmcs12->page_fault_error_code_match =
1565 * evmcs->page_fault_error_code_match;
1566 * vmcs12->cr3_target_count = evmcs->cr3_target_count;
1567 * vmcs12->vm_exit_msr_store_count = evmcs->vm_exit_msr_store_count;
1568 * vmcs12->vm_exit_msr_load_count = evmcs->vm_exit_msr_load_count;
1569 * vmcs12->vm_entry_msr_load_count = evmcs->vm_entry_msr_load_count;
1570 */
1571
1572 /*
1573 * Read only fields:
1574 * vmcs12->guest_physical_address = evmcs->guest_physical_address;
1575 * vmcs12->vm_instruction_error = evmcs->vm_instruction_error;
1576 * vmcs12->vm_exit_reason = evmcs->vm_exit_reason;
1577 * vmcs12->vm_exit_intr_info = evmcs->vm_exit_intr_info;
1578 * vmcs12->vm_exit_intr_error_code = evmcs->vm_exit_intr_error_code;
1579 * vmcs12->idt_vectoring_info_field = evmcs->idt_vectoring_info_field;
1580 * vmcs12->idt_vectoring_error_code = evmcs->idt_vectoring_error_code;
1581 * vmcs12->vm_exit_instruction_len = evmcs->vm_exit_instruction_len;
1582 * vmcs12->vmx_instruction_info = evmcs->vmx_instruction_info;
1583 * vmcs12->exit_qualification = evmcs->exit_qualification;
1584 * vmcs12->guest_linear_address = evmcs->guest_linear_address;
1585 *
1586 * Not present in struct vmcs12:
1587 * vmcs12->exit_io_instruction_ecx = evmcs->exit_io_instruction_ecx;
1588 * vmcs12->exit_io_instruction_esi = evmcs->exit_io_instruction_esi;
1589 * vmcs12->exit_io_instruction_edi = evmcs->exit_io_instruction_edi;
1590 * vmcs12->exit_io_instruction_eip = evmcs->exit_io_instruction_eip;
1591 */
1592
1593 return 0;
1594}
1595
1596static int copy_vmcs12_to_enlightened(struct vcpu_vmx *vmx)
1597{
1598 struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12;
1599 struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
1600
1601 /*
1602 * Should not be changed by KVM:
1603 *
1604 * evmcs->host_es_selector = vmcs12->host_es_selector;
1605 * evmcs->host_cs_selector = vmcs12->host_cs_selector;
1606 * evmcs->host_ss_selector = vmcs12->host_ss_selector;
1607 * evmcs->host_ds_selector = vmcs12->host_ds_selector;
1608 * evmcs->host_fs_selector = vmcs12->host_fs_selector;
1609 * evmcs->host_gs_selector = vmcs12->host_gs_selector;
1610 * evmcs->host_tr_selector = vmcs12->host_tr_selector;
1611 * evmcs->host_ia32_pat = vmcs12->host_ia32_pat;
1612 * evmcs->host_ia32_efer = vmcs12->host_ia32_efer;
1613 * evmcs->host_cr0 = vmcs12->host_cr0;
1614 * evmcs->host_cr3 = vmcs12->host_cr3;
1615 * evmcs->host_cr4 = vmcs12->host_cr4;
1616 * evmcs->host_ia32_sysenter_esp = vmcs12->host_ia32_sysenter_esp;
1617 * evmcs->host_ia32_sysenter_eip = vmcs12->host_ia32_sysenter_eip;
1618 * evmcs->host_rip = vmcs12->host_rip;
1619 * evmcs->host_ia32_sysenter_cs = vmcs12->host_ia32_sysenter_cs;
1620 * evmcs->host_fs_base = vmcs12->host_fs_base;
1621 * evmcs->host_gs_base = vmcs12->host_gs_base;
1622 * evmcs->host_tr_base = vmcs12->host_tr_base;
1623 * evmcs->host_gdtr_base = vmcs12->host_gdtr_base;
1624 * evmcs->host_idtr_base = vmcs12->host_idtr_base;
1625 * evmcs->host_rsp = vmcs12->host_rsp;
1626 * sync_vmcs12() doesn't read these:
1627 * evmcs->io_bitmap_a = vmcs12->io_bitmap_a;
1628 * evmcs->io_bitmap_b = vmcs12->io_bitmap_b;
1629 * evmcs->msr_bitmap = vmcs12->msr_bitmap;
1630 * evmcs->ept_pointer = vmcs12->ept_pointer;
1631 * evmcs->xss_exit_bitmap = vmcs12->xss_exit_bitmap;
1632 * evmcs->vm_exit_msr_store_addr = vmcs12->vm_exit_msr_store_addr;
1633 * evmcs->vm_exit_msr_load_addr = vmcs12->vm_exit_msr_load_addr;
1634 * evmcs->vm_entry_msr_load_addr = vmcs12->vm_entry_msr_load_addr;
1635 * evmcs->cr3_target_value0 = vmcs12->cr3_target_value0;
1636 * evmcs->cr3_target_value1 = vmcs12->cr3_target_value1;
1637 * evmcs->cr3_target_value2 = vmcs12->cr3_target_value2;
1638 * evmcs->cr3_target_value3 = vmcs12->cr3_target_value3;
1639 * evmcs->tpr_threshold = vmcs12->tpr_threshold;
1640 * evmcs->virtual_processor_id = vmcs12->virtual_processor_id;
1641 * evmcs->exception_bitmap = vmcs12->exception_bitmap;
1642 * evmcs->vmcs_link_pointer = vmcs12->vmcs_link_pointer;
1643 * evmcs->pin_based_vm_exec_control = vmcs12->pin_based_vm_exec_control;
1644 * evmcs->vm_exit_controls = vmcs12->vm_exit_controls;
1645 * evmcs->secondary_vm_exec_control = vmcs12->secondary_vm_exec_control;
1646 * evmcs->page_fault_error_code_mask =
1647 * vmcs12->page_fault_error_code_mask;
1648 * evmcs->page_fault_error_code_match =
1649 * vmcs12->page_fault_error_code_match;
1650 * evmcs->cr3_target_count = vmcs12->cr3_target_count;
1651 * evmcs->virtual_apic_page_addr = vmcs12->virtual_apic_page_addr;
1652 * evmcs->tsc_offset = vmcs12->tsc_offset;
1653 * evmcs->guest_ia32_debugctl = vmcs12->guest_ia32_debugctl;
1654 * evmcs->cr0_guest_host_mask = vmcs12->cr0_guest_host_mask;
1655 * evmcs->cr4_guest_host_mask = vmcs12->cr4_guest_host_mask;
1656 * evmcs->cr0_read_shadow = vmcs12->cr0_read_shadow;
1657 * evmcs->cr4_read_shadow = vmcs12->cr4_read_shadow;
1658 * evmcs->vm_exit_msr_store_count = vmcs12->vm_exit_msr_store_count;
1659 * evmcs->vm_exit_msr_load_count = vmcs12->vm_exit_msr_load_count;
1660 * evmcs->vm_entry_msr_load_count = vmcs12->vm_entry_msr_load_count;
1661 *
1662 * Not present in struct vmcs12:
1663 * evmcs->exit_io_instruction_ecx = vmcs12->exit_io_instruction_ecx;
1664 * evmcs->exit_io_instruction_esi = vmcs12->exit_io_instruction_esi;
1665 * evmcs->exit_io_instruction_edi = vmcs12->exit_io_instruction_edi;
1666 * evmcs->exit_io_instruction_eip = vmcs12->exit_io_instruction_eip;
1667 */
1668
1669 evmcs->guest_es_selector = vmcs12->guest_es_selector;
1670 evmcs->guest_cs_selector = vmcs12->guest_cs_selector;
1671 evmcs->guest_ss_selector = vmcs12->guest_ss_selector;
1672 evmcs->guest_ds_selector = vmcs12->guest_ds_selector;
1673 evmcs->guest_fs_selector = vmcs12->guest_fs_selector;
1674 evmcs->guest_gs_selector = vmcs12->guest_gs_selector;
1675 evmcs->guest_ldtr_selector = vmcs12->guest_ldtr_selector;
1676 evmcs->guest_tr_selector = vmcs12->guest_tr_selector;
1677
1678 evmcs->guest_es_limit = vmcs12->guest_es_limit;
1679 evmcs->guest_cs_limit = vmcs12->guest_cs_limit;
1680 evmcs->guest_ss_limit = vmcs12->guest_ss_limit;
1681 evmcs->guest_ds_limit = vmcs12->guest_ds_limit;
1682 evmcs->guest_fs_limit = vmcs12->guest_fs_limit;
1683 evmcs->guest_gs_limit = vmcs12->guest_gs_limit;
1684 evmcs->guest_ldtr_limit = vmcs12->guest_ldtr_limit;
1685 evmcs->guest_tr_limit = vmcs12->guest_tr_limit;
1686 evmcs->guest_gdtr_limit = vmcs12->guest_gdtr_limit;
1687 evmcs->guest_idtr_limit = vmcs12->guest_idtr_limit;
1688
1689 evmcs->guest_es_ar_bytes = vmcs12->guest_es_ar_bytes;
1690 evmcs->guest_cs_ar_bytes = vmcs12->guest_cs_ar_bytes;
1691 evmcs->guest_ss_ar_bytes = vmcs12->guest_ss_ar_bytes;
1692 evmcs->guest_ds_ar_bytes = vmcs12->guest_ds_ar_bytes;
1693 evmcs->guest_fs_ar_bytes = vmcs12->guest_fs_ar_bytes;
1694 evmcs->guest_gs_ar_bytes = vmcs12->guest_gs_ar_bytes;
1695 evmcs->guest_ldtr_ar_bytes = vmcs12->guest_ldtr_ar_bytes;
1696 evmcs->guest_tr_ar_bytes = vmcs12->guest_tr_ar_bytes;
1697
1698 evmcs->guest_es_base = vmcs12->guest_es_base;
1699 evmcs->guest_cs_base = vmcs12->guest_cs_base;
1700 evmcs->guest_ss_base = vmcs12->guest_ss_base;
1701 evmcs->guest_ds_base = vmcs12->guest_ds_base;
1702 evmcs->guest_fs_base = vmcs12->guest_fs_base;
1703 evmcs->guest_gs_base = vmcs12->guest_gs_base;
1704 evmcs->guest_ldtr_base = vmcs12->guest_ldtr_base;
1705 evmcs->guest_tr_base = vmcs12->guest_tr_base;
1706 evmcs->guest_gdtr_base = vmcs12->guest_gdtr_base;
1707 evmcs->guest_idtr_base = vmcs12->guest_idtr_base;
1708
1709 evmcs->guest_ia32_pat = vmcs12->guest_ia32_pat;
1710 evmcs->guest_ia32_efer = vmcs12->guest_ia32_efer;
1711
1712 evmcs->guest_pdptr0 = vmcs12->guest_pdptr0;
1713 evmcs->guest_pdptr1 = vmcs12->guest_pdptr1;
1714 evmcs->guest_pdptr2 = vmcs12->guest_pdptr2;
1715 evmcs->guest_pdptr3 = vmcs12->guest_pdptr3;
1716
1717 evmcs->guest_pending_dbg_exceptions =
1718 vmcs12->guest_pending_dbg_exceptions;
1719 evmcs->guest_sysenter_esp = vmcs12->guest_sysenter_esp;
1720 evmcs->guest_sysenter_eip = vmcs12->guest_sysenter_eip;
1721
1722 evmcs->guest_activity_state = vmcs12->guest_activity_state;
1723 evmcs->guest_sysenter_cs = vmcs12->guest_sysenter_cs;
1724
1725 evmcs->guest_cr0 = vmcs12->guest_cr0;
1726 evmcs->guest_cr3 = vmcs12->guest_cr3;
1727 evmcs->guest_cr4 = vmcs12->guest_cr4;
1728 evmcs->guest_dr7 = vmcs12->guest_dr7;
1729
1730 evmcs->guest_physical_address = vmcs12->guest_physical_address;
1731
1732 evmcs->vm_instruction_error = vmcs12->vm_instruction_error;
1733 evmcs->vm_exit_reason = vmcs12->vm_exit_reason;
1734 evmcs->vm_exit_intr_info = vmcs12->vm_exit_intr_info;
1735 evmcs->vm_exit_intr_error_code = vmcs12->vm_exit_intr_error_code;
1736 evmcs->idt_vectoring_info_field = vmcs12->idt_vectoring_info_field;
1737 evmcs->idt_vectoring_error_code = vmcs12->idt_vectoring_error_code;
1738 evmcs->vm_exit_instruction_len = vmcs12->vm_exit_instruction_len;
1739 evmcs->vmx_instruction_info = vmcs12->vmx_instruction_info;
1740
1741 evmcs->exit_qualification = vmcs12->exit_qualification;
1742
1743 evmcs->guest_linear_address = vmcs12->guest_linear_address;
1744 evmcs->guest_rsp = vmcs12->guest_rsp;
1745 evmcs->guest_rflags = vmcs12->guest_rflags;
1746
1747 evmcs->guest_interruptibility_info =
1748 vmcs12->guest_interruptibility_info;
1749 evmcs->cpu_based_vm_exec_control = vmcs12->cpu_based_vm_exec_control;
1750 evmcs->vm_entry_controls = vmcs12->vm_entry_controls;
1751 evmcs->vm_entry_intr_info_field = vmcs12->vm_entry_intr_info_field;
1752 evmcs->vm_entry_exception_error_code =
1753 vmcs12->vm_entry_exception_error_code;
1754 evmcs->vm_entry_instruction_len = vmcs12->vm_entry_instruction_len;
1755
1756 evmcs->guest_rip = vmcs12->guest_rip;
1757
1758 evmcs->guest_bndcfgs = vmcs12->guest_bndcfgs;
1759
1760 return 0;
1761}
1762
1763/*
1764 * This is an equivalent of the nested hypervisor executing the vmptrld
1765 * instruction.
1766 */
1767static int nested_vmx_handle_enlightened_vmptrld(struct kvm_vcpu *vcpu,
1768 bool from_launch)
1769{
1770 struct vcpu_vmx *vmx = to_vmx(vcpu);
1771 struct hv_vp_assist_page assist_page;
1772
1773 if (likely(!vmx->nested.enlightened_vmcs_enabled))
1774 return 1;
1775
1776 if (unlikely(!kvm_hv_get_assist_page(vcpu, &assist_page)))
1777 return 1;
1778
1779 if (unlikely(!assist_page.enlighten_vmentry))
1780 return 1;
1781
1782 if (unlikely(assist_page.current_nested_vmcs !=
1783 vmx->nested.hv_evmcs_vmptr)) {
1784
1785 if (!vmx->nested.hv_evmcs)
1786 vmx->nested.current_vmptr = -1ull;
1787
1788 nested_release_evmcs(vcpu);
1789
KarimAllah Ahmeddee9c042019-01-31 21:24:42 +01001790 if (kvm_vcpu_map(vcpu, gpa_to_gfn(assist_page.current_nested_vmcs),
1791 &vmx->nested.hv_evmcs_map))
Sean Christopherson55d23752018-12-03 13:53:18 -08001792 return 0;
1793
KarimAllah Ahmeddee9c042019-01-31 21:24:42 +01001794 vmx->nested.hv_evmcs = vmx->nested.hv_evmcs_map.hva;
Sean Christopherson55d23752018-12-03 13:53:18 -08001795
1796 /*
1797 * Currently, KVM only supports eVMCS version 1
1798 * (== KVM_EVMCS_VERSION) and thus we expect guest to set this
1799 * value to first u32 field of eVMCS which should specify eVMCS
1800 * VersionNumber.
1801 *
1802 * Guest should be aware of supported eVMCS versions by host by
1803 * examining CPUID.0x4000000A.EAX[0:15]. Host userspace VMM is
1804 * expected to set this CPUID leaf according to the value
1805 * returned in vmcs_version from nested_enable_evmcs().
1806 *
1807 * However, it turns out that Microsoft Hyper-V fails to comply
1808 * to their own invented interface: When Hyper-V use eVMCS, it
1809 * just sets first u32 field of eVMCS to revision_id specified
1810 * in MSR_IA32_VMX_BASIC. Instead of used eVMCS version number
1811 * which is one of the supported versions specified in
1812 * CPUID.0x4000000A.EAX[0:15].
1813 *
1814 * To overcome Hyper-V bug, we accept here either a supported
1815 * eVMCS version or VMCS12 revision_id as valid values for first
1816 * u32 field of eVMCS.
1817 */
1818 if ((vmx->nested.hv_evmcs->revision_id != KVM_EVMCS_VERSION) &&
1819 (vmx->nested.hv_evmcs->revision_id != VMCS12_REVISION)) {
1820 nested_release_evmcs(vcpu);
1821 return 0;
1822 }
1823
1824 vmx->nested.dirty_vmcs12 = true;
1825 /*
1826 * As we keep L2 state for one guest only 'hv_clean_fields' mask
1827 * can't be used when we switch between them. Reset it here for
1828 * simplicity.
1829 */
1830 vmx->nested.hv_evmcs->hv_clean_fields &=
1831 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
1832 vmx->nested.hv_evmcs_vmptr = assist_page.current_nested_vmcs;
1833
1834 /*
1835 * Unlike normal vmcs12, enlightened vmcs12 is not fully
1836 * reloaded from guest's memory (read only fields, fields not
1837 * present in struct hv_enlightened_vmcs, ...). Make sure there
1838 * are no leftovers.
1839 */
1840 if (from_launch) {
1841 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1842 memset(vmcs12, 0, sizeof(*vmcs12));
1843 vmcs12->hdr.revision_id = VMCS12_REVISION;
1844 }
1845
1846 }
1847 return 1;
1848}
1849
1850void nested_sync_from_vmcs12(struct kvm_vcpu *vcpu)
1851{
1852 struct vcpu_vmx *vmx = to_vmx(vcpu);
1853
1854 /*
1855 * hv_evmcs may end up being not mapped after migration (when
1856 * L2 was running), map it here to make sure vmcs12 changes are
1857 * properly reflected.
1858 */
1859 if (vmx->nested.enlightened_vmcs_enabled && !vmx->nested.hv_evmcs)
1860 nested_vmx_handle_enlightened_vmptrld(vcpu, false);
1861
1862 if (vmx->nested.hv_evmcs) {
1863 copy_vmcs12_to_enlightened(vmx);
1864 /* All fields are clean */
1865 vmx->nested.hv_evmcs->hv_clean_fields |=
1866 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
1867 } else {
1868 copy_vmcs12_to_shadow(vmx);
1869 }
1870
1871 vmx->nested.need_vmcs12_sync = false;
1872}
1873
1874static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
1875{
1876 struct vcpu_vmx *vmx =
1877 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
1878
1879 vmx->nested.preemption_timer_expired = true;
1880 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
1881 kvm_vcpu_kick(&vmx->vcpu);
1882
1883 return HRTIMER_NORESTART;
1884}
1885
1886static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
1887{
1888 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
1889 struct vcpu_vmx *vmx = to_vmx(vcpu);
1890
1891 /*
1892 * A timer value of zero is architecturally guaranteed to cause
1893 * a VMExit prior to executing any instructions in the guest.
1894 */
1895 if (preemption_timeout == 0) {
1896 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
1897 return;
1898 }
1899
1900 if (vcpu->arch.virtual_tsc_khz == 0)
1901 return;
1902
1903 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
1904 preemption_timeout *= 1000000;
1905 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
1906 hrtimer_start(&vmx->nested.preemption_timer,
1907 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
1908}
1909
1910static u64 nested_vmx_calc_efer(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
1911{
1912 if (vmx->nested.nested_run_pending &&
1913 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
1914 return vmcs12->guest_ia32_efer;
1915 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
1916 return vmx->vcpu.arch.efer | (EFER_LMA | EFER_LME);
1917 else
1918 return vmx->vcpu.arch.efer & ~(EFER_LMA | EFER_LME);
1919}
1920
1921static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx)
1922{
1923 /*
1924 * If vmcs02 hasn't been initialized, set the constant vmcs02 state
1925 * according to L0's settings (vmcs12 is irrelevant here). Host
1926 * fields that come from L0 and are not constant, e.g. HOST_CR3,
1927 * will be set as needed prior to VMLAUNCH/VMRESUME.
1928 */
1929 if (vmx->nested.vmcs02_initialized)
1930 return;
1931 vmx->nested.vmcs02_initialized = true;
1932
1933 /*
1934 * We don't care what the EPTP value is we just need to guarantee
1935 * it's valid so we don't get a false positive when doing early
1936 * consistency checks.
1937 */
1938 if (enable_ept && nested_early_check)
1939 vmcs_write64(EPT_POINTER, construct_eptp(&vmx->vcpu, 0));
1940
1941 /* All VMFUNCs are currently emulated through L0 vmexits. */
1942 if (cpu_has_vmx_vmfunc())
1943 vmcs_write64(VM_FUNCTION_CONTROL, 0);
1944
1945 if (cpu_has_vmx_posted_intr())
1946 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
1947
1948 if (cpu_has_vmx_msr_bitmap())
1949 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
1950
1951 if (enable_pml)
1952 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
1953
1954 /*
1955 * Set the MSR load/store lists to match L0's settings. Only the
1956 * addresses are constant (for vmcs02), the counts can change based
1957 * on L2's behavior, e.g. switching to/from long mode.
1958 */
1959 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
1960 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
1961 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
1962
1963 vmx_set_constant_host_state(vmx);
1964}
1965
1966static void prepare_vmcs02_early_full(struct vcpu_vmx *vmx,
1967 struct vmcs12 *vmcs12)
1968{
1969 prepare_vmcs02_constant_state(vmx);
1970
1971 vmcs_write64(VMCS_LINK_POINTER, -1ull);
1972
1973 if (enable_vpid) {
1974 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
1975 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
1976 else
1977 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
1978 }
1979}
1980
1981static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
1982{
1983 u32 exec_control, vmcs12_exec_ctrl;
1984 u64 guest_efer = nested_vmx_calc_efer(vmx, vmcs12);
1985
1986 if (vmx->nested.dirty_vmcs12 || vmx->nested.hv_evmcs)
1987 prepare_vmcs02_early_full(vmx, vmcs12);
1988
1989 /*
Sean Christopherson55d23752018-12-03 13:53:18 -08001990 * PIN CONTROLS
1991 */
1992 exec_control = vmcs12->pin_based_vm_exec_control;
1993
1994 /* Preemption timer setting is computed directly in vmx_vcpu_run. */
1995 exec_control |= vmcs_config.pin_based_exec_ctrl;
1996 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
1997 vmx->loaded_vmcs->hv_timer_armed = false;
1998
1999 /* Posted interrupts setting is only taken from vmcs12. */
2000 if (nested_cpu_has_posted_intr(vmcs12)) {
2001 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
2002 vmx->nested.pi_pending = false;
2003 } else {
2004 exec_control &= ~PIN_BASED_POSTED_INTR;
2005 }
2006 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
2007
2008 /*
2009 * EXEC CONTROLS
2010 */
2011 exec_control = vmx_exec_control(vmx); /* L0's desires */
2012 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
2013 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
2014 exec_control &= ~CPU_BASED_TPR_SHADOW;
2015 exec_control |= vmcs12->cpu_based_vm_exec_control;
2016
2017 /*
2018 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
2019 * nested_get_vmcs12_pages can't fix it up, the illegal value
2020 * will result in a VM entry failure.
2021 */
2022 if (exec_control & CPU_BASED_TPR_SHADOW) {
2023 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
2024 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
2025 } else {
2026#ifdef CONFIG_X86_64
2027 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
2028 CPU_BASED_CR8_STORE_EXITING;
2029#endif
2030 }
2031
2032 /*
2033 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
2034 * for I/O port accesses.
2035 */
2036 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
2037 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
2038 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
2039
2040 /*
2041 * SECONDARY EXEC CONTROLS
2042 */
2043 if (cpu_has_secondary_exec_ctrls()) {
2044 exec_control = vmx->secondary_exec_control;
2045
2046 /* Take the following fields only from vmcs12 */
2047 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
2048 SECONDARY_EXEC_ENABLE_INVPCID |
2049 SECONDARY_EXEC_RDTSCP |
2050 SECONDARY_EXEC_XSAVES |
2051 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
2052 SECONDARY_EXEC_APIC_REGISTER_VIRT |
2053 SECONDARY_EXEC_ENABLE_VMFUNC);
2054 if (nested_cpu_has(vmcs12,
2055 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
2056 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
2057 ~SECONDARY_EXEC_ENABLE_PML;
2058 exec_control |= vmcs12_exec_ctrl;
2059 }
2060
2061 /* VMCS shadowing for L2 is emulated for now */
2062 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
2063
2064 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
2065 vmcs_write16(GUEST_INTR_STATUS,
2066 vmcs12->guest_intr_status);
2067
2068 /*
2069 * Write an illegal value to APIC_ACCESS_ADDR. Later,
2070 * nested_get_vmcs12_pages will either fix it up or
2071 * remove the VM execution control.
2072 */
2073 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
2074 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
2075
2076 if (exec_control & SECONDARY_EXEC_ENCLS_EXITING)
2077 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
2078
2079 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
2080 }
2081
2082 /*
2083 * ENTRY CONTROLS
2084 *
2085 * vmcs12's VM_{ENTRY,EXIT}_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE
2086 * are emulated by vmx_set_efer() in prepare_vmcs02(), but speculate
2087 * on the related bits (if supported by the CPU) in the hope that
2088 * we can avoid VMWrites during vmx_set_efer().
2089 */
2090 exec_control = (vmcs12->vm_entry_controls | vmx_vmentry_ctrl()) &
2091 ~VM_ENTRY_IA32E_MODE & ~VM_ENTRY_LOAD_IA32_EFER;
2092 if (cpu_has_load_ia32_efer()) {
2093 if (guest_efer & EFER_LMA)
2094 exec_control |= VM_ENTRY_IA32E_MODE;
2095 if (guest_efer != host_efer)
2096 exec_control |= VM_ENTRY_LOAD_IA32_EFER;
2097 }
2098 vm_entry_controls_init(vmx, exec_control);
2099
2100 /*
2101 * EXIT CONTROLS
2102 *
2103 * L2->L1 exit controls are emulated - the hardware exit is to L0 so
2104 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
2105 * bits may be modified by vmx_set_efer() in prepare_vmcs02().
2106 */
2107 exec_control = vmx_vmexit_ctrl();
2108 if (cpu_has_load_ia32_efer() && guest_efer != host_efer)
2109 exec_control |= VM_EXIT_LOAD_IA32_EFER;
2110 vm_exit_controls_init(vmx, exec_control);
2111
2112 /*
2113 * Conceptually we want to copy the PML address and index from
2114 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
2115 * since we always flush the log on each vmexit and never change
2116 * the PML address (once set), this happens to be equivalent to
2117 * simply resetting the index in vmcs02.
2118 */
2119 if (enable_pml)
2120 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
2121
2122 /*
2123 * Interrupt/Exception Fields
2124 */
2125 if (vmx->nested.nested_run_pending) {
2126 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2127 vmcs12->vm_entry_intr_info_field);
2128 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
2129 vmcs12->vm_entry_exception_error_code);
2130 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2131 vmcs12->vm_entry_instruction_len);
2132 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
2133 vmcs12->guest_interruptibility_info);
2134 vmx->loaded_vmcs->nmi_known_unmasked =
2135 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
2136 } else {
2137 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
2138 }
2139}
2140
2141static void prepare_vmcs02_full(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
2142{
2143 struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs;
2144
2145 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
2146 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2)) {
2147 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
2148 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
2149 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
2150 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
2151 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
2152 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
2153 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
2154 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
2155 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
2156 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
2157 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
2158 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
2159 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
2160 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
2161 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
2162 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
2163 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
2164 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
2165 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
2166 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
2167 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
2168 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
2169 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
2170 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
2171 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
2172 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
2173 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
2174 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
2175 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
2176 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
2177 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
2178 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
2179 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
2180 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
2181 }
2182
2183 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
2184 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1)) {
2185 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
2186 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
2187 vmcs12->guest_pending_dbg_exceptions);
2188 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
2189 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
2190
2191 /*
2192 * L1 may access the L2's PDPTR, so save them to construct
2193 * vmcs12
2194 */
2195 if (enable_ept) {
2196 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
2197 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
2198 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
2199 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
2200 }
2201 }
2202
2203 if (nested_cpu_has_xsaves(vmcs12))
2204 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
2205
2206 /*
2207 * Whether page-faults are trapped is determined by a combination of
2208 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
2209 * If enable_ept, L0 doesn't care about page faults and we should
2210 * set all of these to L1's desires. However, if !enable_ept, L0 does
2211 * care about (at least some) page faults, and because it is not easy
2212 * (if at all possible?) to merge L0 and L1's desires, we simply ask
2213 * to exit on each and every L2 page fault. This is done by setting
2214 * MASK=MATCH=0 and (see below) EB.PF=1.
2215 * Note that below we don't need special code to set EB.PF beyond the
2216 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
2217 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
2218 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
2219 */
2220 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
2221 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
2222 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
2223 enable_ept ? vmcs12->page_fault_error_code_match : 0);
2224
2225 if (cpu_has_vmx_apicv()) {
2226 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
2227 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
2228 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
2229 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
2230 }
2231
2232 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
2233 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
2234
2235 set_cr4_guest_host_mask(vmx);
2236
2237 if (kvm_mpx_supported()) {
2238 if (vmx->nested.nested_run_pending &&
2239 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
2240 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
2241 else
2242 vmcs_write64(GUEST_BNDCFGS, vmx->nested.vmcs01_guest_bndcfgs);
2243 }
2244}
2245
2246/*
2247 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
2248 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
2249 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
2250 * guest in a way that will both be appropriate to L1's requests, and our
2251 * needs. In addition to modifying the active vmcs (which is vmcs02), this
2252 * function also has additional necessary side-effects, like setting various
2253 * vcpu->arch fields.
2254 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
2255 * is assigned to entry_failure_code on failure.
2256 */
2257static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
2258 u32 *entry_failure_code)
2259{
2260 struct vcpu_vmx *vmx = to_vmx(vcpu);
2261 struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs;
2262
2263 if (vmx->nested.dirty_vmcs12 || vmx->nested.hv_evmcs) {
2264 prepare_vmcs02_full(vmx, vmcs12);
2265 vmx->nested.dirty_vmcs12 = false;
2266 }
2267
2268 /*
2269 * First, the fields that are shadowed. This must be kept in sync
2270 * with vmcs_shadow_fields.h.
2271 */
2272 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
2273 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2)) {
2274 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
2275 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
2276 }
2277
2278 if (vmx->nested.nested_run_pending &&
2279 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
2280 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
2281 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
2282 } else {
2283 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
2284 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
2285 }
2286 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
2287
Sean Christopherson55d23752018-12-03 13:53:18 -08002288 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
2289 * bitwise-or of what L1 wants to trap for L2, and what we want to
2290 * trap. Note that CR0.TS also needs updating - we do this later.
2291 */
2292 update_exception_bitmap(vcpu);
2293 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
2294 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
2295
2296 if (vmx->nested.nested_run_pending &&
2297 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
2298 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
2299 vcpu->arch.pat = vmcs12->guest_ia32_pat;
2300 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2301 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
2302 }
2303
2304 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
2305
2306 if (kvm_has_tsc_control)
2307 decache_tsc_multiplier(vmx);
2308
2309 if (enable_vpid) {
2310 /*
2311 * There is no direct mapping between vpid02 and vpid12, the
2312 * vpid02 is per-vCPU for L0 and reused while the value of
2313 * vpid12 is changed w/ one invvpid during nested vmentry.
2314 * The vpid12 is allocated by L1 for L2, so it will not
2315 * influence global bitmap(for vpid01 and vpid02 allocation)
2316 * even if spawn a lot of nested vCPUs.
2317 */
2318 if (nested_cpu_has_vpid(vmcs12) && nested_has_guest_tlb_tag(vcpu)) {
2319 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
2320 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
2321 __vmx_flush_tlb(vcpu, nested_get_vpid02(vcpu), false);
2322 }
2323 } else {
2324 /*
2325 * If L1 use EPT, then L0 needs to execute INVEPT on
2326 * EPTP02 instead of EPTP01. Therefore, delay TLB
2327 * flush until vmcs02->eptp is fully updated by
2328 * KVM_REQ_LOAD_CR3. Note that this assumes
2329 * KVM_REQ_TLB_FLUSH is evaluated after
2330 * KVM_REQ_LOAD_CR3 in vcpu_enter_guest().
2331 */
2332 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
2333 }
2334 }
2335
2336 if (nested_cpu_has_ept(vmcs12))
2337 nested_ept_init_mmu_context(vcpu);
2338 else if (nested_cpu_has2(vmcs12,
2339 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
2340 vmx_flush_tlb(vcpu, true);
2341
2342 /*
2343 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
2344 * bits which we consider mandatory enabled.
2345 * The CR0_READ_SHADOW is what L2 should have expected to read given
2346 * the specifications by L1; It's not enough to take
2347 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
2348 * have more bits than L1 expected.
2349 */
2350 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
2351 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
2352
2353 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
2354 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
2355
2356 vcpu->arch.efer = nested_vmx_calc_efer(vmx, vmcs12);
2357 /* Note: may modify VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
2358 vmx_set_efer(vcpu, vcpu->arch.efer);
2359
2360 /*
2361 * Guest state is invalid and unrestricted guest is disabled,
2362 * which means L1 attempted VMEntry to L2 with invalid state.
2363 * Fail the VMEntry.
2364 */
2365 if (vmx->emulation_required) {
2366 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christophersonc80add02019-04-11 12:18:09 -07002367 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002368 }
2369
2370 /* Shadow page tables on either EPT or shadow page tables. */
2371 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
2372 entry_failure_code))
Sean Christophersonc80add02019-04-11 12:18:09 -07002373 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002374
2375 if (!enable_ept)
2376 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
2377
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02002378 kvm_rsp_write(vcpu, vmcs12->guest_rsp);
2379 kvm_rip_write(vcpu, vmcs12->guest_rip);
Sean Christopherson55d23752018-12-03 13:53:18 -08002380 return 0;
2381}
2382
2383static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
2384{
2385 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
2386 nested_cpu_has_virtual_nmis(vmcs12))
2387 return -EINVAL;
2388
2389 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
2390 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
2391 return -EINVAL;
2392
2393 return 0;
2394}
2395
2396static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
2397{
2398 struct vcpu_vmx *vmx = to_vmx(vcpu);
2399 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2400
2401 /* Check for memory type validity */
2402 switch (address & VMX_EPTP_MT_MASK) {
2403 case VMX_EPTP_MT_UC:
2404 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
2405 return false;
2406 break;
2407 case VMX_EPTP_MT_WB:
2408 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
2409 return false;
2410 break;
2411 default:
2412 return false;
2413 }
2414
2415 /* only 4 levels page-walk length are valid */
2416 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
2417 return false;
2418
2419 /* Reserved bits should not be set */
2420 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
2421 return false;
2422
2423 /* AD, if set, should be supported */
2424 if (address & VMX_EPTP_AD_ENABLE_BIT) {
2425 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
2426 return false;
2427 }
2428
2429 return true;
2430}
2431
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002432/*
2433 * Checks related to VM-Execution Control Fields
2434 */
2435static int nested_check_vm_execution_controls(struct kvm_vcpu *vcpu,
2436 struct vmcs12 *vmcs12)
2437{
2438 struct vcpu_vmx *vmx = to_vmx(vcpu);
2439
2440 if (!vmx_control_verify(vmcs12->pin_based_vm_exec_control,
2441 vmx->nested.msrs.pinbased_ctls_low,
2442 vmx->nested.msrs.pinbased_ctls_high) ||
2443 !vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
2444 vmx->nested.msrs.procbased_ctls_low,
2445 vmx->nested.msrs.procbased_ctls_high))
2446 return -EINVAL;
2447
2448 if (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
2449 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
2450 vmx->nested.msrs.secondary_ctls_low,
2451 vmx->nested.msrs.secondary_ctls_high))
2452 return -EINVAL;
2453
2454 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu) ||
2455 nested_vmx_check_io_bitmap_controls(vcpu, vmcs12) ||
2456 nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12) ||
2457 nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12) ||
2458 nested_vmx_check_apic_access_controls(vcpu, vmcs12) ||
2459 nested_vmx_check_apicv_controls(vcpu, vmcs12) ||
2460 nested_vmx_check_nmi_controls(vmcs12) ||
2461 nested_vmx_check_pml_controls(vcpu, vmcs12) ||
2462 nested_vmx_check_unrestricted_guest_controls(vcpu, vmcs12) ||
2463 nested_vmx_check_mode_based_ept_exec_controls(vcpu, vmcs12) ||
2464 nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12) ||
2465 (nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id))
2466 return -EINVAL;
2467
Sean Christophersonbc441212019-02-12 16:42:23 -08002468 if (!nested_cpu_has_preemption_timer(vmcs12) &&
2469 nested_cpu_has_save_preemption_timer(vmcs12))
2470 return -EINVAL;
2471
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002472 if (nested_cpu_has_ept(vmcs12) &&
2473 !valid_ept_address(vcpu, vmcs12->ept_pointer))
2474 return -EINVAL;
2475
2476 if (nested_cpu_has_vmfunc(vmcs12)) {
2477 if (vmcs12->vm_function_control &
2478 ~vmx->nested.msrs.vmfunc_controls)
2479 return -EINVAL;
2480
2481 if (nested_cpu_has_eptp_switching(vmcs12)) {
2482 if (!nested_cpu_has_ept(vmcs12) ||
2483 !page_address_valid(vcpu, vmcs12->eptp_list_address))
2484 return -EINVAL;
2485 }
2486 }
2487
2488 return 0;
2489}
2490
Krish Sadhukhan61446ba2018-12-12 13:30:09 -05002491/*
2492 * Checks related to VM-Exit Control Fields
2493 */
2494static int nested_check_vm_exit_controls(struct kvm_vcpu *vcpu,
2495 struct vmcs12 *vmcs12)
2496{
2497 struct vcpu_vmx *vmx = to_vmx(vcpu);
2498
2499 if (!vmx_control_verify(vmcs12->vm_exit_controls,
2500 vmx->nested.msrs.exit_ctls_low,
2501 vmx->nested.msrs.exit_ctls_high) ||
2502 nested_vmx_check_exit_msr_switch_controls(vcpu, vmcs12))
2503 return -EINVAL;
2504
2505 return 0;
2506}
2507
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002508/*
2509 * Checks related to VM-Entry Control Fields
2510 */
2511static int nested_check_vm_entry_controls(struct kvm_vcpu *vcpu,
2512 struct vmcs12 *vmcs12)
Sean Christopherson55d23752018-12-03 13:53:18 -08002513{
2514 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08002515
Krish Sadhukhan61446ba2018-12-12 13:30:09 -05002516 if (!vmx_control_verify(vmcs12->vm_entry_controls,
Sean Christopherson55d23752018-12-03 13:53:18 -08002517 vmx->nested.msrs.entry_ctls_low,
2518 vmx->nested.msrs.entry_ctls_high))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002519 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002520
2521 /*
2522 * From the Intel SDM, volume 3:
2523 * Fields relevant to VM-entry event injection must be set properly.
2524 * These fields are the VM-entry interruption-information field, the
2525 * VM-entry exception error code, and the VM-entry instruction length.
2526 */
2527 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
2528 u32 intr_info = vmcs12->vm_entry_intr_info_field;
2529 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
2530 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
2531 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
2532 bool should_have_error_code;
2533 bool urg = nested_cpu_has2(vmcs12,
2534 SECONDARY_EXEC_UNRESTRICTED_GUEST);
2535 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
2536
2537 /* VM-entry interruption-info field: interruption type */
2538 if (intr_type == INTR_TYPE_RESERVED ||
2539 (intr_type == INTR_TYPE_OTHER_EVENT &&
2540 !nested_cpu_supports_monitor_trap_flag(vcpu)))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002541 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002542
2543 /* VM-entry interruption-info field: vector */
2544 if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
2545 (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
2546 (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002547 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002548
2549 /* VM-entry interruption-info field: deliver error code */
2550 should_have_error_code =
2551 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
2552 x86_exception_has_error_code(vector);
2553 if (has_error_code != should_have_error_code)
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002554 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002555
2556 /* VM-entry exception error code */
2557 if (has_error_code &&
2558 vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002559 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002560
2561 /* VM-entry interruption-info field: reserved bits */
2562 if (intr_info & INTR_INFO_RESVD_BITS_MASK)
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002563 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002564
2565 /* VM-entry instruction length */
2566 switch (intr_type) {
2567 case INTR_TYPE_SOFT_EXCEPTION:
2568 case INTR_TYPE_SOFT_INTR:
2569 case INTR_TYPE_PRIV_SW_EXCEPTION:
2570 if ((vmcs12->vm_entry_instruction_len > 15) ||
2571 (vmcs12->vm_entry_instruction_len == 0 &&
2572 !nested_cpu_has_zero_length_injection(vcpu)))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002573 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002574 }
2575 }
2576
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002577 if (nested_vmx_check_entry_msr_switch_controls(vcpu, vmcs12))
2578 return -EINVAL;
2579
2580 return 0;
2581}
2582
Sean Christopherson5478ba32019-04-11 12:18:06 -07002583static int nested_vmx_check_controls(struct kvm_vcpu *vcpu,
2584 struct vmcs12 *vmcs12)
2585{
2586 if (nested_check_vm_execution_controls(vcpu, vmcs12) ||
2587 nested_check_vm_exit_controls(vcpu, vmcs12) ||
2588 nested_check_vm_entry_controls(vcpu, vmcs12))
Paolo Bonzini98d9e852019-04-12 10:19:57 +02002589 return -EINVAL;
Sean Christopherson5478ba32019-04-11 12:18:06 -07002590
2591 return 0;
2592}
2593
Paolo Bonzini98d9e852019-04-12 10:19:57 +02002594static int nested_vmx_check_host_state(struct kvm_vcpu *vcpu,
2595 struct vmcs12 *vmcs12)
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002596{
2597 bool ia32e;
2598
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002599 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
2600 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
2601 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
Krish Sadhukhan254b2f32018-12-12 13:30:11 -05002602 return -EINVAL;
Krish Sadhukhan711eff32019-02-07 14:05:30 -05002603
2604 if (is_noncanonical_address(vmcs12->host_ia32_sysenter_esp, vcpu) ||
2605 is_noncanonical_address(vmcs12->host_ia32_sysenter_eip, vcpu))
2606 return -EINVAL;
2607
Krish Sadhukhanf6b0db1f2019-04-08 17:35:11 -04002608 if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) &&
2609 !kvm_pat_valid(vmcs12->host_ia32_pat))
2610 return -EINVAL;
2611
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002612 /*
2613 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
2614 * IA32_EFER MSR must be 0 in the field for that register. In addition,
2615 * the values of the LMA and LME bits in the field must each be that of
2616 * the host address-space size VM-exit control.
2617 */
2618 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
2619 ia32e = (vmcs12->vm_exit_controls &
2620 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
2621 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
2622 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
2623 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
Krish Sadhukhan254b2f32018-12-12 13:30:11 -05002624 return -EINVAL;
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002625 }
2626
Sean Christopherson55d23752018-12-03 13:53:18 -08002627 return 0;
2628}
2629
2630static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
2631 struct vmcs12 *vmcs12)
2632{
KarimAllah Ahmed88925302019-01-31 21:24:41 +01002633 int r = 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08002634 struct vmcs12 *shadow;
KarimAllah Ahmed88925302019-01-31 21:24:41 +01002635 struct kvm_host_map map;
Sean Christopherson55d23752018-12-03 13:53:18 -08002636
2637 if (vmcs12->vmcs_link_pointer == -1ull)
2638 return 0;
2639
2640 if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))
2641 return -EINVAL;
2642
KarimAllah Ahmed88925302019-01-31 21:24:41 +01002643 if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->vmcs_link_pointer), &map))
Sean Christopherson55d23752018-12-03 13:53:18 -08002644 return -EINVAL;
2645
KarimAllah Ahmed88925302019-01-31 21:24:41 +01002646 shadow = map.hva;
2647
Sean Christopherson55d23752018-12-03 13:53:18 -08002648 if (shadow->hdr.revision_id != VMCS12_REVISION ||
2649 shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
2650 r = -EINVAL;
KarimAllah Ahmed88925302019-01-31 21:24:41 +01002651
2652 kvm_vcpu_unmap(vcpu, &map, false);
Sean Christopherson55d23752018-12-03 13:53:18 -08002653 return r;
2654}
2655
Sean Christopherson55d23752018-12-03 13:53:18 -08002656/*
2657 * Checks related to Guest Non-register State
2658 */
2659static int nested_check_guest_non_reg_state(struct vmcs12 *vmcs12)
2660{
2661 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
2662 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
2663 return -EINVAL;
2664
2665 return 0;
2666}
2667
Sean Christopherson5478ba32019-04-11 12:18:06 -07002668static int nested_vmx_check_guest_state(struct kvm_vcpu *vcpu,
2669 struct vmcs12 *vmcs12,
2670 u32 *exit_qual)
Sean Christopherson55d23752018-12-03 13:53:18 -08002671{
2672 bool ia32e;
2673
2674 *exit_qual = ENTRY_FAIL_DEFAULT;
2675
2676 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
2677 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
Sean Christophersonc80add02019-04-11 12:18:09 -07002678 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002679
Krish Sadhukhande2bc2b2019-04-08 17:35:12 -04002680 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) &&
2681 !kvm_pat_valid(vmcs12->guest_ia32_pat))
Sean Christophersonc80add02019-04-11 12:18:09 -07002682 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002683
2684 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
2685 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
Sean Christophersonc80add02019-04-11 12:18:09 -07002686 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002687 }
2688
2689 /*
2690 * If the load IA32_EFER VM-entry control is 1, the following checks
2691 * are performed on the field for the IA32_EFER MSR:
2692 * - Bits reserved in the IA32_EFER MSR must be 0.
2693 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
2694 * the IA-32e mode guest VM-exit control. It must also be identical
2695 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
2696 * CR0.PG) is 1.
2697 */
2698 if (to_vmx(vcpu)->nested.nested_run_pending &&
2699 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
2700 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
2701 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
2702 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
2703 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
2704 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
Sean Christophersonc80add02019-04-11 12:18:09 -07002705 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002706 }
2707
2708 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
Sean Christophersonc80add02019-04-11 12:18:09 -07002709 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
2710 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
2711 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002712
Sean Christopherson9c3e9222019-04-11 12:18:05 -07002713 if (nested_check_guest_non_reg_state(vmcs12))
Sean Christophersonc80add02019-04-11 12:18:09 -07002714 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002715
2716 return 0;
2717}
2718
Sean Christopherson453eafb2018-12-20 12:25:17 -08002719static int nested_vmx_check_vmentry_hw(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08002720{
2721 struct vcpu_vmx *vmx = to_vmx(vcpu);
2722 unsigned long cr3, cr4;
Sean Christophersonf1727b42019-01-25 07:40:58 -08002723 bool vm_fail;
Sean Christopherson55d23752018-12-03 13:53:18 -08002724
2725 if (!nested_early_check)
2726 return 0;
2727
2728 if (vmx->msr_autoload.host.nr)
2729 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
2730 if (vmx->msr_autoload.guest.nr)
2731 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
2732
2733 preempt_disable();
2734
2735 vmx_prepare_switch_to_guest(vcpu);
2736
2737 /*
2738 * Induce a consistency check VMExit by clearing bit 1 in GUEST_RFLAGS,
2739 * which is reserved to '1' by hardware. GUEST_RFLAGS is guaranteed to
2740 * be written (by preparve_vmcs02()) before the "real" VMEnter, i.e.
2741 * there is no need to preserve other bits or save/restore the field.
2742 */
2743 vmcs_writel(GUEST_RFLAGS, 0);
2744
Sean Christopherson55d23752018-12-03 13:53:18 -08002745 cr3 = __get_current_cr3_fast();
2746 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
2747 vmcs_writel(HOST_CR3, cr3);
2748 vmx->loaded_vmcs->host_state.cr3 = cr3;
2749 }
2750
2751 cr4 = cr4_read_shadow();
2752 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
2753 vmcs_writel(HOST_CR4, cr4);
2754 vmx->loaded_vmcs->host_state.cr4 = cr4;
2755 }
2756
Sean Christopherson55d23752018-12-03 13:53:18 -08002757 asm(
Sean Christopherson453eafb2018-12-20 12:25:17 -08002758 "sub $%c[wordsize], %%" _ASM_SP "\n\t" /* temporarily adjust RSP for CALL */
Sean Christopherson5a878162019-01-25 07:41:02 -08002759 "cmp %%" _ASM_SP ", %c[host_state_rsp](%[loaded_vmcs]) \n\t"
2760 "je 1f \n\t"
Sean Christophersonfbda0fd2019-01-25 07:41:01 -08002761 __ex("vmwrite %%" _ASM_SP ", %[HOST_RSP]") "\n\t"
Sean Christopherson5a878162019-01-25 07:41:02 -08002762 "mov %%" _ASM_SP ", %c[host_state_rsp](%[loaded_vmcs]) \n\t"
2763 "1: \n\t"
Sean Christopherson453eafb2018-12-20 12:25:17 -08002764 "add $%c[wordsize], %%" _ASM_SP "\n\t" /* un-adjust RSP */
Sean Christopherson55d23752018-12-03 13:53:18 -08002765
2766 /* Check if vmlaunch or vmresume is needed */
Sean Christopherson74dfa272019-01-25 07:41:00 -08002767 "cmpb $0, %c[launched](%[loaded_vmcs])\n\t"
Sean Christopherson453eafb2018-12-20 12:25:17 -08002768
Sean Christophersonf1727b42019-01-25 07:40:58 -08002769 /*
2770 * VMLAUNCH and VMRESUME clear RFLAGS.{CF,ZF} on VM-Exit, set
2771 * RFLAGS.CF on VM-Fail Invalid and set RFLAGS.ZF on VM-Fail
2772 * Valid. vmx_vmenter() directly "returns" RFLAGS, and so the
Sean Christophersonbbc0b822019-01-25 07:40:59 -08002773 * results of VM-Enter is captured via CC_{SET,OUT} to vm_fail.
Sean Christophersonf1727b42019-01-25 07:40:58 -08002774 */
Sean Christopherson453eafb2018-12-20 12:25:17 -08002775 "call vmx_vmenter\n\t"
2776
Sean Christophersonbbc0b822019-01-25 07:40:59 -08002777 CC_SET(be)
2778 : ASM_CALL_CONSTRAINT, CC_OUT(be) (vm_fail)
Sean Christopherson5a878162019-01-25 07:41:02 -08002779 : [HOST_RSP]"r"((unsigned long)HOST_RSP),
Sean Christopherson74dfa272019-01-25 07:41:00 -08002780 [loaded_vmcs]"r"(vmx->loaded_vmcs),
2781 [launched]"i"(offsetof(struct loaded_vmcs, launched)),
Sean Christopherson5a878162019-01-25 07:41:02 -08002782 [host_state_rsp]"i"(offsetof(struct loaded_vmcs, host_state.rsp)),
Sean Christopherson453eafb2018-12-20 12:25:17 -08002783 [wordsize]"i"(sizeof(ulong))
Jan Beulich5a253552019-05-27 02:45:44 -06002784 : "memory"
Sean Christopherson55d23752018-12-03 13:53:18 -08002785 );
2786
Sean Christopherson55d23752018-12-03 13:53:18 -08002787 if (vmx->msr_autoload.host.nr)
2788 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
2789 if (vmx->msr_autoload.guest.nr)
2790 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
2791
Sean Christophersonf1727b42019-01-25 07:40:58 -08002792 if (vm_fail) {
Wanpeng Li541e8862019-05-17 16:49:50 +08002793 preempt_enable();
Sean Christopherson55d23752018-12-03 13:53:18 -08002794 WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) !=
2795 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Sean Christopherson55d23752018-12-03 13:53:18 -08002796 return 1;
2797 }
2798
2799 /*
2800 * VMExit clears RFLAGS.IF and DR7, even on a consistency check.
2801 */
2802 local_irq_enable();
2803 if (hw_breakpoint_active())
2804 set_debugreg(__this_cpu_read(cpu_dr7), 7);
Wanpeng Li541e8862019-05-17 16:49:50 +08002805 preempt_enable();
Sean Christopherson55d23752018-12-03 13:53:18 -08002806
2807 /*
2808 * A non-failing VMEntry means we somehow entered guest mode with
2809 * an illegal RIP, and that's just the tip of the iceberg. There
2810 * is no telling what memory has been modified or what state has
2811 * been exposed to unknown code. Hitting this all but guarantees
2812 * a (very critical) hardware issue.
2813 */
2814 WARN_ON(!(vmcs_read32(VM_EXIT_REASON) &
2815 VMX_EXIT_REASONS_FAILED_VMENTRY));
2816
2817 return 0;
2818}
Sean Christopherson55d23752018-12-03 13:53:18 -08002819
2820static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
2821 struct vmcs12 *vmcs12);
2822
2823static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
2824{
2825 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2826 struct vcpu_vmx *vmx = to_vmx(vcpu);
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01002827 struct kvm_host_map *map;
Sean Christopherson55d23752018-12-03 13:53:18 -08002828 struct page *page;
2829 u64 hpa;
2830
2831 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
2832 /*
2833 * Translate L1 physical address to host physical
2834 * address for vmcs02. Keep the page pinned, so this
2835 * physical address remains valid. We keep a reference
2836 * to it so we can release it later.
2837 */
2838 if (vmx->nested.apic_access_page) { /* shouldn't happen */
2839 kvm_release_page_dirty(vmx->nested.apic_access_page);
2840 vmx->nested.apic_access_page = NULL;
2841 }
2842 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
2843 /*
2844 * If translation failed, no matter: This feature asks
2845 * to exit when accessing the given address, and if it
2846 * can never be accessed, this feature won't do
2847 * anything anyway.
2848 */
2849 if (!is_error_page(page)) {
2850 vmx->nested.apic_access_page = page;
2851 hpa = page_to_phys(vmx->nested.apic_access_page);
2852 vmcs_write64(APIC_ACCESS_ADDR, hpa);
2853 } else {
2854 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
2855 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
2856 }
2857 }
2858
2859 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01002860 map = &vmx->nested.virtual_apic_map;
Sean Christopherson55d23752018-12-03 13:53:18 -08002861
2862 /*
2863 * If translation failed, VM entry will fail because
2864 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
Sean Christopherson55d23752018-12-03 13:53:18 -08002865 */
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01002866 if (!kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->virtual_apic_page_addr), map)) {
2867 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, pfn_to_hpa(map->pfn));
Paolo Bonzini69090812019-04-15 15:16:17 +02002868 } else if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING) &&
2869 nested_cpu_has(vmcs12, CPU_BASED_CR8_STORE_EXITING) &&
2870 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
2871 /*
2872 * The processor will never use the TPR shadow, simply
2873 * clear the bit from the execution control. Such a
2874 * configuration is useless, but it happens in tests.
2875 * For any other configuration, failing the vm entry is
2876 * _not_ what the processor does but it's basically the
2877 * only possibility we have.
2878 */
2879 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
2880 CPU_BASED_TPR_SHADOW);
2881 } else {
2882 printk("bad virtual-APIC page address\n");
2883 dump_vmcs();
Sean Christopherson55d23752018-12-03 13:53:18 -08002884 }
2885 }
2886
2887 if (nested_cpu_has_posted_intr(vmcs12)) {
KarimAllah Ahmed3278e042019-01-31 21:24:38 +01002888 map = &vmx->nested.pi_desc_map;
2889
2890 if (!kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->posted_intr_desc_addr), map)) {
2891 vmx->nested.pi_desc =
2892 (struct pi_desc *)(((void *)map->hva) +
2893 offset_in_page(vmcs12->posted_intr_desc_addr));
2894 vmcs_write64(POSTED_INTR_DESC_ADDR,
2895 pfn_to_hpa(map->pfn) + offset_in_page(vmcs12->posted_intr_desc_addr));
Sean Christopherson55d23752018-12-03 13:53:18 -08002896 }
Sean Christopherson55d23752018-12-03 13:53:18 -08002897 }
2898 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
2899 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
2900 CPU_BASED_USE_MSR_BITMAPS);
2901 else
2902 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
2903 CPU_BASED_USE_MSR_BITMAPS);
2904}
2905
2906/*
2907 * Intel's VMX Instruction Reference specifies a common set of prerequisites
2908 * for running VMX instructions (except VMXON, whose prerequisites are
2909 * slightly different). It also specifies what exception to inject otherwise.
2910 * Note that many of these exceptions have priority over VM exits, so they
2911 * don't have to be checked again here.
2912 */
2913static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
2914{
2915 if (!to_vmx(vcpu)->nested.vmxon) {
2916 kvm_queue_exception(vcpu, UD_VECTOR);
2917 return 0;
2918 }
2919
2920 if (vmx_get_cpl(vcpu)) {
2921 kvm_inject_gp(vcpu, 0);
2922 return 0;
2923 }
2924
2925 return 1;
2926}
2927
2928static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu)
2929{
2930 u8 rvi = vmx_get_rvi();
2931 u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI);
2932
2933 return ((rvi & 0xf0) > (vppr & 0xf0));
2934}
2935
2936static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
2937 struct vmcs12 *vmcs12);
2938
2939/*
2940 * If from_vmentry is false, this is being called from state restore (either RSM
2941 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
2942+ *
2943+ * Returns:
2944+ * 0 - success, i.e. proceed with actual VMEnter
2945+ * 1 - consistency check VMExit
2946+ * -1 - consistency check VMFail
2947 */
2948int nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry)
2949{
2950 struct vcpu_vmx *vmx = to_vmx(vcpu);
2951 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2952 bool evaluate_pending_interrupts;
2953 u32 exit_reason = EXIT_REASON_INVALID_STATE;
2954 u32 exit_qual;
2955
2956 evaluate_pending_interrupts = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
2957 (CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_VIRTUAL_NMI_PENDING);
2958 if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu))
2959 evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu);
2960
2961 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
2962 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
2963 if (kvm_mpx_supported() &&
2964 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
2965 vmx->nested.vmcs01_guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
2966
2967 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
2968
2969 prepare_vmcs02_early(vmx, vmcs12);
2970
2971 if (from_vmentry) {
2972 nested_get_vmcs12_pages(vcpu);
2973
2974 if (nested_vmx_check_vmentry_hw(vcpu)) {
2975 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
2976 return -1;
2977 }
2978
Sean Christopherson5478ba32019-04-11 12:18:06 -07002979 if (nested_vmx_check_guest_state(vcpu, vmcs12, &exit_qual))
Sean Christopherson55d23752018-12-03 13:53:18 -08002980 goto vmentry_fail_vmexit;
2981 }
2982
2983 enter_guest_mode(vcpu);
2984 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
2985 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
2986
2987 if (prepare_vmcs02(vcpu, vmcs12, &exit_qual))
2988 goto vmentry_fail_vmexit_guest_mode;
2989
2990 if (from_vmentry) {
2991 exit_reason = EXIT_REASON_MSR_LOAD_FAIL;
2992 exit_qual = nested_vmx_load_msr(vcpu,
2993 vmcs12->vm_entry_msr_load_addr,
2994 vmcs12->vm_entry_msr_load_count);
2995 if (exit_qual)
2996 goto vmentry_fail_vmexit_guest_mode;
2997 } else {
2998 /*
2999 * The MMU is not initialized to point at the right entities yet and
3000 * "get pages" would need to read data from the guest (i.e. we will
3001 * need to perform gpa to hpa translation). Request a call
3002 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
3003 * have already been set at vmentry time and should not be reset.
3004 */
3005 kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
3006 }
3007
3008 /*
3009 * If L1 had a pending IRQ/NMI until it executed
3010 * VMLAUNCH/VMRESUME which wasn't delivered because it was
3011 * disallowed (e.g. interrupts disabled), L0 needs to
3012 * evaluate if this pending event should cause an exit from L2
3013 * to L1 or delivered directly to L2 (e.g. In case L1 don't
3014 * intercept EXTERNAL_INTERRUPT).
3015 *
3016 * Usually this would be handled by the processor noticing an
3017 * IRQ/NMI window request, or checking RVI during evaluation of
3018 * pending virtual interrupts. However, this setting was done
3019 * on VMCS01 and now VMCS02 is active instead. Thus, we force L0
3020 * to perform pending event evaluation by requesting a KVM_REQ_EVENT.
3021 */
3022 if (unlikely(evaluate_pending_interrupts))
3023 kvm_make_request(KVM_REQ_EVENT, vcpu);
3024
3025 /*
Paolo Bonzini359a6c32019-01-29 19:14:46 +01003026 * Do not start the preemption timer hrtimer until after we know
3027 * we are successful, so that only nested_vmx_vmexit needs to cancel
3028 * the timer.
3029 */
3030 vmx->nested.preemption_timer_expired = false;
3031 if (nested_cpu_has_preemption_timer(vmcs12))
3032 vmx_start_preemption_timer(vcpu);
3033
3034 /*
Sean Christopherson55d23752018-12-03 13:53:18 -08003035 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
3036 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
3037 * returned as far as L1 is concerned. It will only return (and set
3038 * the success flag) when L2 exits (see nested_vmx_vmexit()).
3039 */
3040 return 0;
3041
3042 /*
3043 * A failed consistency check that leads to a VMExit during L1's
3044 * VMEnter to L2 is a variation of a normal VMexit, as explained in
3045 * 26.7 "VM-entry failures during or after loading guest state".
3046 */
3047vmentry_fail_vmexit_guest_mode:
3048 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
3049 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
3050 leave_guest_mode(vcpu);
3051
3052vmentry_fail_vmexit:
3053 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
3054
3055 if (!from_vmentry)
3056 return 1;
3057
3058 load_vmcs12_host_state(vcpu, vmcs12);
3059 vmcs12->vm_exit_reason = exit_reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
3060 vmcs12->exit_qualification = exit_qual;
3061 if (enable_shadow_vmcs || vmx->nested.hv_evmcs)
3062 vmx->nested.need_vmcs12_sync = true;
3063 return 1;
3064}
3065
3066/*
3067 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
3068 * for running an L2 nested guest.
3069 */
3070static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
3071{
3072 struct vmcs12 *vmcs12;
3073 struct vcpu_vmx *vmx = to_vmx(vcpu);
3074 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
3075 int ret;
3076
3077 if (!nested_vmx_check_permission(vcpu))
3078 return 1;
3079
3080 if (!nested_vmx_handle_enlightened_vmptrld(vcpu, true))
3081 return 1;
3082
3083 if (!vmx->nested.hv_evmcs && vmx->nested.current_vmptr == -1ull)
3084 return nested_vmx_failInvalid(vcpu);
3085
3086 vmcs12 = get_vmcs12(vcpu);
3087
3088 /*
3089 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
3090 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
3091 * rather than RFLAGS.ZF, and no error number is stored to the
3092 * VM-instruction error field.
3093 */
3094 if (vmcs12->hdr.shadow_vmcs)
3095 return nested_vmx_failInvalid(vcpu);
3096
3097 if (vmx->nested.hv_evmcs) {
3098 copy_enlightened_to_vmcs12(vmx);
3099 /* Enlightened VMCS doesn't have launch state */
3100 vmcs12->launch_state = !launch;
3101 } else if (enable_shadow_vmcs) {
3102 copy_shadow_to_vmcs12(vmx);
3103 }
3104
3105 /*
3106 * The nested entry process starts with enforcing various prerequisites
3107 * on vmcs12 as required by the Intel SDM, and act appropriately when
3108 * they fail: As the SDM explains, some conditions should cause the
3109 * instruction to fail, while others will cause the instruction to seem
3110 * to succeed, but return an EXIT_REASON_INVALID_STATE.
3111 * To speed up the normal (success) code path, we should avoid checking
3112 * for misconfigurations which will anyway be caught by the processor
3113 * when using the merged vmcs02.
3114 */
3115 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS)
3116 return nested_vmx_failValid(vcpu,
3117 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
3118
3119 if (vmcs12->launch_state == launch)
3120 return nested_vmx_failValid(vcpu,
3121 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
3122 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
3123
Paolo Bonzini98d9e852019-04-12 10:19:57 +02003124 if (nested_vmx_check_controls(vcpu, vmcs12))
3125 return nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Sean Christopherson5478ba32019-04-11 12:18:06 -07003126
Paolo Bonzini98d9e852019-04-12 10:19:57 +02003127 if (nested_vmx_check_host_state(vcpu, vmcs12))
3128 return nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
Sean Christopherson55d23752018-12-03 13:53:18 -08003129
3130 /*
3131 * We're finally done with prerequisite checking, and can start with
3132 * the nested entry.
3133 */
3134 vmx->nested.nested_run_pending = 1;
3135 ret = nested_vmx_enter_non_root_mode(vcpu, true);
3136 vmx->nested.nested_run_pending = !ret;
3137 if (ret > 0)
3138 return 1;
3139 else if (ret)
3140 return nested_vmx_failValid(vcpu,
3141 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
3142
3143 /* Hide L1D cache contents from the nested guest. */
3144 vmx->vcpu.arch.l1tf_flush_l1d = true;
3145
3146 /*
3147 * Must happen outside of nested_vmx_enter_non_root_mode() as it will
3148 * also be used as part of restoring nVMX state for
3149 * snapshot restore (migration).
3150 *
3151 * In this flow, it is assumed that vmcs12 cache was
3152 * trasferred as part of captured nVMX state and should
3153 * therefore not be read from guest memory (which may not
3154 * exist on destination host yet).
3155 */
3156 nested_cache_shadow_vmcs12(vcpu, vmcs12);
3157
3158 /*
Jim Mattson9ebdfe52018-11-26 11:22:32 -08003159 * If we're entering a halted L2 vcpu and the L2 vcpu won't be
3160 * awakened by event injection or by an NMI-window VM-exit or
3161 * by an interrupt-window VM-exit, halt the vcpu.
Sean Christopherson55d23752018-12-03 13:53:18 -08003162 */
3163 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson9ebdfe52018-11-26 11:22:32 -08003164 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) &&
3165 !(vmcs12->cpu_based_vm_exec_control & CPU_BASED_VIRTUAL_NMI_PENDING) &&
3166 !((vmcs12->cpu_based_vm_exec_control & CPU_BASED_VIRTUAL_INTR_PENDING) &&
3167 (vmcs12->guest_rflags & X86_EFLAGS_IF))) {
Sean Christopherson55d23752018-12-03 13:53:18 -08003168 vmx->nested.nested_run_pending = 0;
3169 return kvm_vcpu_halt(vcpu);
3170 }
3171 return 1;
3172}
3173
3174/*
3175 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
3176 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
3177 * This function returns the new value we should put in vmcs12.guest_cr0.
3178 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
3179 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
3180 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
3181 * didn't trap the bit, because if L1 did, so would L0).
3182 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
3183 * been modified by L2, and L1 knows it. So just leave the old value of
3184 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
3185 * isn't relevant, because if L0 traps this bit it can set it to anything.
3186 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
3187 * changed these bits, and therefore they need to be updated, but L0
3188 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
3189 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
3190 */
3191static inline unsigned long
3192vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
3193{
3194 return
3195 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
3196 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
3197 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
3198 vcpu->arch.cr0_guest_owned_bits));
3199}
3200
3201static inline unsigned long
3202vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
3203{
3204 return
3205 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
3206 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
3207 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
3208 vcpu->arch.cr4_guest_owned_bits));
3209}
3210
3211static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
3212 struct vmcs12 *vmcs12)
3213{
3214 u32 idt_vectoring;
3215 unsigned int nr;
3216
3217 if (vcpu->arch.exception.injected) {
3218 nr = vcpu->arch.exception.nr;
3219 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
3220
3221 if (kvm_exception_is_soft(nr)) {
3222 vmcs12->vm_exit_instruction_len =
3223 vcpu->arch.event_exit_inst_len;
3224 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
3225 } else
3226 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
3227
3228 if (vcpu->arch.exception.has_error_code) {
3229 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
3230 vmcs12->idt_vectoring_error_code =
3231 vcpu->arch.exception.error_code;
3232 }
3233
3234 vmcs12->idt_vectoring_info_field = idt_vectoring;
3235 } else if (vcpu->arch.nmi_injected) {
3236 vmcs12->idt_vectoring_info_field =
3237 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
3238 } else if (vcpu->arch.interrupt.injected) {
3239 nr = vcpu->arch.interrupt.nr;
3240 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
3241
3242 if (vcpu->arch.interrupt.soft) {
3243 idt_vectoring |= INTR_TYPE_SOFT_INTR;
3244 vmcs12->vm_entry_instruction_len =
3245 vcpu->arch.event_exit_inst_len;
3246 } else
3247 idt_vectoring |= INTR_TYPE_EXT_INTR;
3248
3249 vmcs12->idt_vectoring_info_field = idt_vectoring;
3250 }
3251}
3252
3253
3254static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
3255{
3256 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3257 gfn_t gfn;
3258
3259 /*
3260 * Don't need to mark the APIC access page dirty; it is never
3261 * written to by the CPU during APIC virtualization.
3262 */
3263
3264 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
3265 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
3266 kvm_vcpu_mark_page_dirty(vcpu, gfn);
3267 }
3268
3269 if (nested_cpu_has_posted_intr(vmcs12)) {
3270 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
3271 kvm_vcpu_mark_page_dirty(vcpu, gfn);
3272 }
3273}
3274
3275static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
3276{
3277 struct vcpu_vmx *vmx = to_vmx(vcpu);
3278 int max_irr;
3279 void *vapic_page;
3280 u16 status;
3281
3282 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
3283 return;
3284
3285 vmx->nested.pi_pending = false;
3286 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
3287 return;
3288
3289 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
3290 if (max_irr != 256) {
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003291 vapic_page = vmx->nested.virtual_apic_map.hva;
3292 if (!vapic_page)
3293 return;
3294
Sean Christopherson55d23752018-12-03 13:53:18 -08003295 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
3296 vapic_page, &max_irr);
Sean Christopherson55d23752018-12-03 13:53:18 -08003297 status = vmcs_read16(GUEST_INTR_STATUS);
3298 if ((u8)max_irr > ((u8)status & 0xff)) {
3299 status &= ~0xff;
3300 status |= (u8)max_irr;
3301 vmcs_write16(GUEST_INTR_STATUS, status);
3302 }
3303 }
3304
3305 nested_mark_vmcs12_pages_dirty(vcpu);
3306}
3307
3308static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3309 unsigned long exit_qual)
3310{
3311 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3312 unsigned int nr = vcpu->arch.exception.nr;
3313 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3314
3315 if (vcpu->arch.exception.has_error_code) {
3316 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3317 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3318 }
3319
3320 if (kvm_exception_is_soft(nr))
3321 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3322 else
3323 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3324
3325 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3326 vmx_get_nmi_mask(vcpu))
3327 intr_info |= INTR_INFO_UNBLOCK_NMI;
3328
3329 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3330}
3331
3332static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
3333{
3334 struct vcpu_vmx *vmx = to_vmx(vcpu);
3335 unsigned long exit_qual;
3336 bool block_nested_events =
3337 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
3338
3339 if (vcpu->arch.exception.pending &&
3340 nested_vmx_check_exception(vcpu, &exit_qual)) {
3341 if (block_nested_events)
3342 return -EBUSY;
3343 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
3344 return 0;
3345 }
3346
3347 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
3348 vmx->nested.preemption_timer_expired) {
3349 if (block_nested_events)
3350 return -EBUSY;
3351 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
3352 return 0;
3353 }
3354
3355 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
3356 if (block_nested_events)
3357 return -EBUSY;
3358 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
3359 NMI_VECTOR | INTR_TYPE_NMI_INTR |
3360 INTR_INFO_VALID_MASK, 0);
3361 /*
3362 * The NMI-triggered VM exit counts as injection:
3363 * clear this one and block further NMIs.
3364 */
3365 vcpu->arch.nmi_pending = 0;
3366 vmx_set_nmi_mask(vcpu, true);
3367 return 0;
3368 }
3369
3370 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
3371 nested_exit_on_intr(vcpu)) {
3372 if (block_nested_events)
3373 return -EBUSY;
3374 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
3375 return 0;
3376 }
3377
3378 vmx_complete_nested_posted_interrupt(vcpu);
3379 return 0;
3380}
3381
3382static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
3383{
3384 ktime_t remaining =
3385 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
3386 u64 value;
3387
3388 if (ktime_to_ns(remaining) <= 0)
3389 return 0;
3390
3391 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
3392 do_div(value, 1000000);
3393 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
3394}
3395
3396/*
3397 * Update the guest state fields of vmcs12 to reflect changes that
3398 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
3399 * VM-entry controls is also updated, since this is really a guest
3400 * state bit.)
3401 */
3402static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
3403{
3404 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
3405 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
3406
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02003407 vmcs12->guest_rsp = kvm_rsp_read(vcpu);
3408 vmcs12->guest_rip = kvm_rip_read(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08003409 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
3410
3411 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
3412 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
3413 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
3414 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
3415 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
3416 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
3417 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
3418 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
3419 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
3420 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
3421 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
3422 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
3423 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
3424 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
3425 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
3426 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
3427 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
3428 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
3429 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
3430 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
3431 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
3432 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
3433 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
3434 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
3435 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
3436 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
3437 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
3438 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
3439 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
3440 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
3441 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
3442 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
3443 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
3444 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
3445 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
3446 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
3447
3448 vmcs12->guest_interruptibility_info =
3449 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3450 vmcs12->guest_pending_dbg_exceptions =
3451 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
3452 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
3453 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
3454 else
3455 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
3456
Paolo Bonzinib4b65b52019-01-29 19:12:35 +01003457 if (nested_cpu_has_preemption_timer(vmcs12) &&
3458 vmcs12->vm_exit_controls & VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
Sean Christopherson55d23752018-12-03 13:53:18 -08003459 vmcs12->vmx_preemption_timer_value =
3460 vmx_get_preemption_timer_value(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08003461
3462 /*
3463 * In some cases (usually, nested EPT), L2 is allowed to change its
3464 * own CR3 without exiting. If it has changed it, we must keep it.
3465 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
3466 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
3467 *
3468 * Additionally, restore L2's PDPTR to vmcs12.
3469 */
3470 if (enable_ept) {
3471 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
3472 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
3473 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
3474 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
3475 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
3476 }
3477
3478 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
3479
3480 if (nested_cpu_has_vid(vmcs12))
3481 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
3482
3483 vmcs12->vm_entry_controls =
3484 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
3485 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
3486
3487 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
3488 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
3489 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
3490 }
3491
3492 /* TODO: These cannot have changed unless we have MSR bitmaps and
3493 * the relevant bit asks not to trap the change */
3494 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
3495 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
3496 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
3497 vmcs12->guest_ia32_efer = vcpu->arch.efer;
3498 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
3499 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
3500 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
3501 if (kvm_mpx_supported())
3502 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
3503}
3504
3505/*
3506 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
3507 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
3508 * and this function updates it to reflect the changes to the guest state while
3509 * L2 was running (and perhaps made some exits which were handled directly by L0
3510 * without going back to L1), and to reflect the exit reason.
3511 * Note that we do not have to copy here all VMCS fields, just those that
3512 * could have changed by the L2 guest or the exit - i.e., the guest-state and
3513 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
3514 * which already writes to vmcs12 directly.
3515 */
3516static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
3517 u32 exit_reason, u32 exit_intr_info,
3518 unsigned long exit_qualification)
3519{
3520 /* update guest state fields: */
3521 sync_vmcs12(vcpu, vmcs12);
3522
3523 /* update exit information fields: */
3524
3525 vmcs12->vm_exit_reason = exit_reason;
3526 vmcs12->exit_qualification = exit_qualification;
3527 vmcs12->vm_exit_intr_info = exit_intr_info;
3528
3529 vmcs12->idt_vectoring_info_field = 0;
3530 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
3531 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
3532
3533 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
3534 vmcs12->launch_state = 1;
3535
3536 /* vm_entry_intr_info_field is cleared on exit. Emulate this
3537 * instead of reading the real value. */
3538 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
3539
3540 /*
3541 * Transfer the event that L0 or L1 may wanted to inject into
3542 * L2 to IDT_VECTORING_INFO_FIELD.
3543 */
3544 vmcs12_save_pending_event(vcpu, vmcs12);
Krish Sadhukhana0d4f802018-12-04 19:00:13 -05003545
3546 /*
3547 * According to spec, there's no need to store the guest's
3548 * MSRs if the exit is due to a VM-entry failure that occurs
3549 * during or after loading the guest state. Since this exit
3550 * does not fall in that category, we need to save the MSRs.
3551 */
3552 if (nested_vmx_store_msr(vcpu,
3553 vmcs12->vm_exit_msr_store_addr,
3554 vmcs12->vm_exit_msr_store_count))
3555 nested_vmx_abort(vcpu,
3556 VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Sean Christopherson55d23752018-12-03 13:53:18 -08003557 }
3558
3559 /*
3560 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
3561 * preserved above and would only end up incorrectly in L1.
3562 */
3563 vcpu->arch.nmi_injected = false;
3564 kvm_clear_exception_queue(vcpu);
3565 kvm_clear_interrupt_queue(vcpu);
3566}
3567
3568/*
3569 * A part of what we need to when the nested L2 guest exits and we want to
3570 * run its L1 parent, is to reset L1's guest state to the host state specified
3571 * in vmcs12.
3572 * This function is to be called not only on normal nested exit, but also on
3573 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
3574 * Failures During or After Loading Guest State").
3575 * This function should be called when the active VMCS is L1's (vmcs01).
3576 */
3577static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
3578 struct vmcs12 *vmcs12)
3579{
3580 struct kvm_segment seg;
3581 u32 entry_failure_code;
3582
3583 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
3584 vcpu->arch.efer = vmcs12->host_ia32_efer;
3585 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
3586 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
3587 else
3588 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
3589 vmx_set_efer(vcpu, vcpu->arch.efer);
3590
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02003591 kvm_rsp_write(vcpu, vmcs12->host_rsp);
3592 kvm_rip_write(vcpu, vmcs12->host_rip);
Sean Christopherson55d23752018-12-03 13:53:18 -08003593 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
3594 vmx_set_interrupt_shadow(vcpu, 0);
3595
3596 /*
3597 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
3598 * actually changed, because vmx_set_cr0 refers to efer set above.
3599 *
3600 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
3601 * (KVM doesn't change it);
3602 */
3603 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
3604 vmx_set_cr0(vcpu, vmcs12->host_cr0);
3605
3606 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
3607 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
3608 vmx_set_cr4(vcpu, vmcs12->host_cr4);
3609
3610 nested_ept_uninit_mmu_context(vcpu);
3611
3612 /*
3613 * Only PDPTE load can fail as the value of cr3 was checked on entry and
3614 * couldn't have changed.
3615 */
3616 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
3617 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
3618
3619 if (!enable_ept)
3620 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
3621
3622 /*
3623 * If vmcs01 doesn't use VPID, CPU flushes TLB on every
3624 * VMEntry/VMExit. Thus, no need to flush TLB.
3625 *
3626 * If vmcs12 doesn't use VPID, L1 expects TLB to be
3627 * flushed on every VMEntry/VMExit.
3628 *
3629 * Otherwise, we can preserve TLB entries as long as we are
3630 * able to tag L1 TLB entries differently than L2 TLB entries.
3631 *
3632 * If vmcs12 uses EPT, we need to execute this flush on EPTP01
3633 * and therefore we request the TLB flush to happen only after VMCS EPTP
3634 * has been set by KVM_REQ_LOAD_CR3.
3635 */
3636 if (enable_vpid &&
3637 (!nested_cpu_has_vpid(vmcs12) || !nested_has_guest_tlb_tag(vcpu))) {
3638 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
3639 }
3640
3641 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
3642 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
3643 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
3644 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
3645 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
3646 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
3647 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
3648
3649 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
3650 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
3651 vmcs_write64(GUEST_BNDCFGS, 0);
3652
3653 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
3654 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
3655 vcpu->arch.pat = vmcs12->host_ia32_pat;
3656 }
3657 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
3658 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
3659 vmcs12->host_ia32_perf_global_ctrl);
3660
3661 /* Set L1 segment info according to Intel SDM
3662 27.5.2 Loading Host Segment and Descriptor-Table Registers */
3663 seg = (struct kvm_segment) {
3664 .base = 0,
3665 .limit = 0xFFFFFFFF,
3666 .selector = vmcs12->host_cs_selector,
3667 .type = 11,
3668 .present = 1,
3669 .s = 1,
3670 .g = 1
3671 };
3672 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
3673 seg.l = 1;
3674 else
3675 seg.db = 1;
3676 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
3677 seg = (struct kvm_segment) {
3678 .base = 0,
3679 .limit = 0xFFFFFFFF,
3680 .type = 3,
3681 .present = 1,
3682 .s = 1,
3683 .db = 1,
3684 .g = 1
3685 };
3686 seg.selector = vmcs12->host_ds_selector;
3687 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
3688 seg.selector = vmcs12->host_es_selector;
3689 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
3690 seg.selector = vmcs12->host_ss_selector;
3691 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
3692 seg.selector = vmcs12->host_fs_selector;
3693 seg.base = vmcs12->host_fs_base;
3694 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
3695 seg.selector = vmcs12->host_gs_selector;
3696 seg.base = vmcs12->host_gs_base;
3697 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
3698 seg = (struct kvm_segment) {
3699 .base = vmcs12->host_tr_base,
3700 .limit = 0x67,
3701 .selector = vmcs12->host_tr_selector,
3702 .type = 11,
3703 .present = 1
3704 };
3705 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
3706
3707 kvm_set_dr(vcpu, 7, 0x400);
3708 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
3709
3710 if (cpu_has_vmx_msr_bitmap())
3711 vmx_update_msr_bitmap(vcpu);
3712
3713 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
3714 vmcs12->vm_exit_msr_load_count))
3715 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
3716}
3717
3718static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx)
3719{
3720 struct shared_msr_entry *efer_msr;
3721 unsigned int i;
3722
3723 if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER)
3724 return vmcs_read64(GUEST_IA32_EFER);
3725
3726 if (cpu_has_load_ia32_efer())
3727 return host_efer;
3728
3729 for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) {
3730 if (vmx->msr_autoload.guest.val[i].index == MSR_EFER)
3731 return vmx->msr_autoload.guest.val[i].value;
3732 }
3733
3734 efer_msr = find_msr_entry(vmx, MSR_EFER);
3735 if (efer_msr)
3736 return efer_msr->data;
3737
3738 return host_efer;
3739}
3740
3741static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu)
3742{
3743 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3744 struct vcpu_vmx *vmx = to_vmx(vcpu);
3745 struct vmx_msr_entry g, h;
3746 struct msr_data msr;
3747 gpa_t gpa;
3748 u32 i, j;
3749
3750 vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT);
3751
3752 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
3753 /*
3754 * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set
3755 * as vmcs01.GUEST_DR7 contains a userspace defined value
3756 * and vcpu->arch.dr7 is not squirreled away before the
3757 * nested VMENTER (not worth adding a variable in nested_vmx).
3758 */
3759 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
3760 kvm_set_dr(vcpu, 7, DR7_FIXED_1);
3761 else
3762 WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7)));
3763 }
3764
3765 /*
3766 * Note that calling vmx_set_{efer,cr0,cr4} is important as they
3767 * handle a variety of side effects to KVM's software model.
3768 */
3769 vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx));
3770
3771 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
3772 vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW));
3773
3774 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
3775 vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW));
3776
3777 nested_ept_uninit_mmu_context(vcpu);
Paolo Bonzini2b279242019-04-15 15:57:19 +02003778
3779 /*
3780 * This is only valid if EPT is in use, otherwise the vmcs01 GUEST_CR3
3781 * points to shadow pages! Fortunately we only get here after a WARN_ON
3782 * if EPT is disabled, so a VMabort is perfectly fine.
3783 */
3784 if (enable_ept) {
3785 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
3786 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
3787 } else {
3788 nested_vmx_abort(vcpu, VMX_ABORT_VMCS_CORRUPTED);
3789 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003790
3791 /*
3792 * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs
3793 * from vmcs01 (if necessary). The PDPTRs are not loaded on
3794 * VMFail, like everything else we just need to ensure our
3795 * software model is up-to-date.
3796 */
3797 ept_save_pdptrs(vcpu);
3798
3799 kvm_mmu_reset_context(vcpu);
3800
3801 if (cpu_has_vmx_msr_bitmap())
3802 vmx_update_msr_bitmap(vcpu);
3803
3804 /*
3805 * This nasty bit of open coding is a compromise between blindly
3806 * loading L1's MSRs using the exit load lists (incorrect emulation
3807 * of VMFail), leaving the nested VM's MSRs in the software model
3808 * (incorrect behavior) and snapshotting the modified MSRs (too
3809 * expensive since the lists are unbound by hardware). For each
3810 * MSR that was (prematurely) loaded from the nested VMEntry load
3811 * list, reload it from the exit load list if it exists and differs
3812 * from the guest value. The intent is to stuff host state as
3813 * silently as possible, not to fully process the exit load list.
3814 */
3815 msr.host_initiated = false;
3816 for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) {
3817 gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g));
3818 if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) {
3819 pr_debug_ratelimited(
3820 "%s read MSR index failed (%u, 0x%08llx)\n",
3821 __func__, i, gpa);
3822 goto vmabort;
3823 }
3824
3825 for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) {
3826 gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h));
3827 if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) {
3828 pr_debug_ratelimited(
3829 "%s read MSR failed (%u, 0x%08llx)\n",
3830 __func__, j, gpa);
3831 goto vmabort;
3832 }
3833 if (h.index != g.index)
3834 continue;
3835 if (h.value == g.value)
3836 break;
3837
3838 if (nested_vmx_load_msr_check(vcpu, &h)) {
3839 pr_debug_ratelimited(
3840 "%s check failed (%u, 0x%x, 0x%x)\n",
3841 __func__, j, h.index, h.reserved);
3842 goto vmabort;
3843 }
3844
3845 msr.index = h.index;
3846 msr.data = h.value;
3847 if (kvm_set_msr(vcpu, &msr)) {
3848 pr_debug_ratelimited(
3849 "%s WRMSR failed (%u, 0x%x, 0x%llx)\n",
3850 __func__, j, h.index, h.value);
3851 goto vmabort;
3852 }
3853 }
3854 }
3855
3856 return;
3857
3858vmabort:
3859 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
3860}
3861
3862/*
3863 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
3864 * and modify vmcs12 to make it see what it would expect to see there if
3865 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
3866 */
3867void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
3868 u32 exit_intr_info, unsigned long exit_qualification)
3869{
3870 struct vcpu_vmx *vmx = to_vmx(vcpu);
3871 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3872
3873 /* trying to cancel vmlaunch/vmresume is a bug */
3874 WARN_ON_ONCE(vmx->nested.nested_run_pending);
3875
3876 leave_guest_mode(vcpu);
3877
Paolo Bonzinib4b65b52019-01-29 19:12:35 +01003878 if (nested_cpu_has_preemption_timer(vmcs12))
3879 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
3880
Sean Christopherson55d23752018-12-03 13:53:18 -08003881 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
3882 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
3883
3884 if (likely(!vmx->fail)) {
3885 if (exit_reason == -1)
3886 sync_vmcs12(vcpu, vmcs12);
3887 else
3888 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
3889 exit_qualification);
3890
3891 /*
3892 * Must happen outside of sync_vmcs12() as it will
3893 * also be used to capture vmcs12 cache as part of
3894 * capturing nVMX state for snapshot (migration).
3895 *
3896 * Otherwise, this flush will dirty guest memory at a
3897 * point it is already assumed by user-space to be
3898 * immutable.
3899 */
3900 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08003901 } else {
3902 /*
3903 * The only expected VM-instruction error is "VM entry with
3904 * invalid control field(s)." Anything else indicates a
3905 * problem with L0. And we should never get here with a
3906 * VMFail of any type if early consistency checks are enabled.
3907 */
3908 WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) !=
3909 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
3910 WARN_ON_ONCE(nested_early_check);
3911 }
3912
3913 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
3914
3915 /* Update any VMCS fields that might have changed while L2 ran */
3916 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
3917 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
3918 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
3919
3920 if (kvm_has_tsc_control)
3921 decache_tsc_multiplier(vmx);
3922
3923 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
3924 vmx->nested.change_vmcs01_virtual_apic_mode = false;
3925 vmx_set_virtual_apic_mode(vcpu);
3926 } else if (!nested_cpu_has_ept(vmcs12) &&
3927 nested_cpu_has2(vmcs12,
3928 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
3929 vmx_flush_tlb(vcpu, true);
3930 }
3931
Sean Christopherson55d23752018-12-03 13:53:18 -08003932 /* Unpin physical memory we referred to in vmcs02 */
3933 if (vmx->nested.apic_access_page) {
3934 kvm_release_page_dirty(vmx->nested.apic_access_page);
3935 vmx->nested.apic_access_page = NULL;
3936 }
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003937 kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map, true);
KarimAllah Ahmed3278e042019-01-31 21:24:38 +01003938 kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map, true);
3939 vmx->nested.pi_desc = NULL;
Sean Christopherson55d23752018-12-03 13:53:18 -08003940
3941 /*
3942 * We are now running in L2, mmu_notifier will force to reload the
3943 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
3944 */
3945 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
3946
3947 if ((exit_reason != -1) && (enable_shadow_vmcs || vmx->nested.hv_evmcs))
3948 vmx->nested.need_vmcs12_sync = true;
3949
3950 /* in case we halted in L2 */
3951 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
3952
3953 if (likely(!vmx->fail)) {
3954 /*
3955 * TODO: SDM says that with acknowledge interrupt on
3956 * exit, bit 31 of the VM-exit interrupt information
3957 * (valid interrupt) is always set to 1 on
3958 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
3959 * need kvm_cpu_has_interrupt(). See the commit
3960 * message for details.
3961 */
3962 if (nested_exit_intr_ack_set(vcpu) &&
3963 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
3964 kvm_cpu_has_interrupt(vcpu)) {
3965 int irq = kvm_cpu_get_interrupt(vcpu);
3966 WARN_ON(irq < 0);
3967 vmcs12->vm_exit_intr_info = irq |
3968 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
3969 }
3970
3971 if (exit_reason != -1)
3972 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
3973 vmcs12->exit_qualification,
3974 vmcs12->idt_vectoring_info_field,
3975 vmcs12->vm_exit_intr_info,
3976 vmcs12->vm_exit_intr_error_code,
3977 KVM_ISA_VMX);
3978
3979 load_vmcs12_host_state(vcpu, vmcs12);
3980
3981 return;
3982 }
3983
3984 /*
3985 * After an early L2 VM-entry failure, we're now back
3986 * in L1 which thinks it just finished a VMLAUNCH or
3987 * VMRESUME instruction, so we need to set the failure
3988 * flag and the VM-instruction error field of the VMCS
3989 * accordingly, and skip the emulated instruction.
3990 */
3991 (void)nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
3992
3993 /*
3994 * Restore L1's host state to KVM's software model. We're here
3995 * because a consistency check was caught by hardware, which
3996 * means some amount of guest state has been propagated to KVM's
3997 * model and needs to be unwound to the host's state.
3998 */
3999 nested_vmx_restore_host_state(vcpu);
4000
4001 vmx->fail = 0;
4002}
4003
4004/*
4005 * Decode the memory-address operand of a vmx instruction, as recorded on an
4006 * exit caused by such an instruction (run by a guest hypervisor).
4007 * On success, returns 0. When the operand is invalid, returns 1 and throws
4008 * #UD or #GP.
4009 */
4010int get_vmx_mem_address(struct kvm_vcpu *vcpu, unsigned long exit_qualification,
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004011 u32 vmx_instruction_info, bool wr, int len, gva_t *ret)
Sean Christopherson55d23752018-12-03 13:53:18 -08004012{
4013 gva_t off;
4014 bool exn;
4015 struct kvm_segment s;
4016
4017 /*
4018 * According to Vol. 3B, "Information for VM Exits Due to Instruction
4019 * Execution", on an exit, vmx_instruction_info holds most of the
4020 * addressing components of the operand. Only the displacement part
4021 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
4022 * For how an actual address is calculated from all these components,
4023 * refer to Vol. 1, "Operand Addressing".
4024 */
4025 int scaling = vmx_instruction_info & 3;
4026 int addr_size = (vmx_instruction_info >> 7) & 7;
4027 bool is_reg = vmx_instruction_info & (1u << 10);
4028 int seg_reg = (vmx_instruction_info >> 15) & 7;
4029 int index_reg = (vmx_instruction_info >> 18) & 0xf;
4030 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
4031 int base_reg = (vmx_instruction_info >> 23) & 0xf;
4032 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
4033
4034 if (is_reg) {
4035 kvm_queue_exception(vcpu, UD_VECTOR);
4036 return 1;
4037 }
4038
4039 /* Addr = segment_base + offset */
4040 /* offset = base + [index * scale] + displacement */
4041 off = exit_qualification; /* holds the displacement */
Sean Christopherson946c5222019-01-23 14:39:23 -08004042 if (addr_size == 1)
4043 off = (gva_t)sign_extend64(off, 31);
4044 else if (addr_size == 0)
4045 off = (gva_t)sign_extend64(off, 15);
Sean Christopherson55d23752018-12-03 13:53:18 -08004046 if (base_is_valid)
4047 off += kvm_register_read(vcpu, base_reg);
4048 if (index_is_valid)
4049 off += kvm_register_read(vcpu, index_reg)<<scaling;
4050 vmx_get_segment(vcpu, &s, seg_reg);
Sean Christopherson55d23752018-12-03 13:53:18 -08004051
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004052 /*
4053 * The effective address, i.e. @off, of a memory operand is truncated
4054 * based on the address size of the instruction. Note that this is
4055 * the *effective address*, i.e. the address prior to accounting for
4056 * the segment's base.
4057 */
Sean Christopherson55d23752018-12-03 13:53:18 -08004058 if (addr_size == 1) /* 32 bit */
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004059 off &= 0xffffffff;
4060 else if (addr_size == 0) /* 16 bit */
4061 off &= 0xffff;
Sean Christopherson55d23752018-12-03 13:53:18 -08004062
4063 /* Checks for #GP/#SS exceptions. */
4064 exn = false;
4065 if (is_long_mode(vcpu)) {
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004066 /*
4067 * The virtual/linear address is never truncated in 64-bit
4068 * mode, e.g. a 32-bit address size can yield a 64-bit virtual
4069 * address when using FS/GS with a non-zero base.
4070 */
4071 *ret = s.base + off;
4072
Sean Christopherson55d23752018-12-03 13:53:18 -08004073 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
4074 * non-canonical form. This is the only check on the memory
4075 * destination for long mode!
4076 */
4077 exn = is_noncanonical_address(*ret, vcpu);
Paolo Bonzinie0dfacb2019-01-30 17:25:38 +01004078 } else {
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004079 /*
4080 * When not in long mode, the virtual/linear address is
4081 * unconditionally truncated to 32 bits regardless of the
4082 * address size.
4083 */
4084 *ret = (s.base + off) & 0xffffffff;
4085
Sean Christopherson55d23752018-12-03 13:53:18 -08004086 /* Protected mode: apply checks for segment validity in the
4087 * following order:
4088 * - segment type check (#GP(0) may be thrown)
4089 * - usability check (#GP(0)/#SS(0))
4090 * - limit check (#GP(0)/#SS(0))
4091 */
4092 if (wr)
4093 /* #GP(0) if the destination operand is located in a
4094 * read-only data segment or any code segment.
4095 */
4096 exn = ((s.type & 0xa) == 0 || (s.type & 8));
4097 else
4098 /* #GP(0) if the source operand is located in an
4099 * execute-only code segment
4100 */
4101 exn = ((s.type & 0xa) == 8);
4102 if (exn) {
4103 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
4104 return 1;
4105 }
4106 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
4107 */
4108 exn = (s.unusable != 0);
Sean Christopherson34333cc2019-01-23 14:39:25 -08004109
4110 /*
4111 * Protected mode: #GP(0)/#SS(0) if the memory operand is
4112 * outside the segment limit. All CPUs that support VMX ignore
4113 * limit checks for flat segments, i.e. segments with base==0,
4114 * limit==0xffffffff and of type expand-up data or code.
Sean Christopherson55d23752018-12-03 13:53:18 -08004115 */
Sean Christopherson34333cc2019-01-23 14:39:25 -08004116 if (!(s.base == 0 && s.limit == 0xffffffff &&
4117 ((s.type & 8) || !(s.type & 4))))
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004118 exn = exn || ((u64)off + len - 1 > s.limit);
Sean Christopherson55d23752018-12-03 13:53:18 -08004119 }
4120 if (exn) {
4121 kvm_queue_exception_e(vcpu,
4122 seg_reg == VCPU_SREG_SS ?
4123 SS_VECTOR : GP_VECTOR,
4124 0);
4125 return 1;
4126 }
4127
4128 return 0;
4129}
4130
4131static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
4132{
4133 gva_t gva;
4134 struct x86_exception e;
4135
4136 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004137 vmcs_read32(VMX_INSTRUCTION_INFO), false,
4138 sizeof(*vmpointer), &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08004139 return 1;
4140
4141 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
4142 kvm_inject_page_fault(vcpu, &e);
4143 return 1;
4144 }
4145
4146 return 0;
4147}
4148
4149/*
4150 * Allocate a shadow VMCS and associate it with the currently loaded
4151 * VMCS, unless such a shadow VMCS already exists. The newly allocated
4152 * VMCS is also VMCLEARed, so that it is ready for use.
4153 */
4154static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
4155{
4156 struct vcpu_vmx *vmx = to_vmx(vcpu);
4157 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
4158
4159 /*
4160 * We should allocate a shadow vmcs for vmcs01 only when L1
4161 * executes VMXON and free it when L1 executes VMXOFF.
4162 * As it is invalid to execute VMXON twice, we shouldn't reach
4163 * here when vmcs01 already have an allocated shadow vmcs.
4164 */
4165 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
4166
4167 if (!loaded_vmcs->shadow_vmcs) {
4168 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
4169 if (loaded_vmcs->shadow_vmcs)
4170 vmcs_clear(loaded_vmcs->shadow_vmcs);
4171 }
4172 return loaded_vmcs->shadow_vmcs;
4173}
4174
4175static int enter_vmx_operation(struct kvm_vcpu *vcpu)
4176{
4177 struct vcpu_vmx *vmx = to_vmx(vcpu);
4178 int r;
4179
4180 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
4181 if (r < 0)
4182 goto out_vmcs02;
4183
Ben Gardon41836832019-02-11 11:02:52 -08004184 vmx->nested.cached_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT);
Sean Christopherson55d23752018-12-03 13:53:18 -08004185 if (!vmx->nested.cached_vmcs12)
4186 goto out_cached_vmcs12;
4187
Ben Gardon41836832019-02-11 11:02:52 -08004188 vmx->nested.cached_shadow_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT);
Sean Christopherson55d23752018-12-03 13:53:18 -08004189 if (!vmx->nested.cached_shadow_vmcs12)
4190 goto out_cached_shadow_vmcs12;
4191
4192 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
4193 goto out_shadow_vmcs;
4194
4195 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
4196 HRTIMER_MODE_REL_PINNED);
4197 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
4198
4199 vmx->nested.vpid02 = allocate_vpid();
4200
4201 vmx->nested.vmcs02_initialized = false;
4202 vmx->nested.vmxon = true;
Luwei Kangee85dec2018-10-24 16:05:16 +08004203
4204 if (pt_mode == PT_MODE_HOST_GUEST) {
4205 vmx->pt_desc.guest.ctl = 0;
4206 pt_update_intercept_for_msr(vmx);
4207 }
4208
Sean Christopherson55d23752018-12-03 13:53:18 -08004209 return 0;
4210
4211out_shadow_vmcs:
4212 kfree(vmx->nested.cached_shadow_vmcs12);
4213
4214out_cached_shadow_vmcs12:
4215 kfree(vmx->nested.cached_vmcs12);
4216
4217out_cached_vmcs12:
4218 free_loaded_vmcs(&vmx->nested.vmcs02);
4219
4220out_vmcs02:
4221 return -ENOMEM;
4222}
4223
4224/*
4225 * Emulate the VMXON instruction.
4226 * Currently, we just remember that VMX is active, and do not save or even
4227 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
4228 * do not currently need to store anything in that guest-allocated memory
4229 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
4230 * argument is different from the VMXON pointer (which the spec says they do).
4231 */
4232static int handle_vmon(struct kvm_vcpu *vcpu)
4233{
4234 int ret;
4235 gpa_t vmptr;
KarimAllah Ahmed2e408932019-01-31 21:24:31 +01004236 uint32_t revision;
Sean Christopherson55d23752018-12-03 13:53:18 -08004237 struct vcpu_vmx *vmx = to_vmx(vcpu);
4238 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
4239 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4240
4241 /*
4242 * The Intel VMX Instruction Reference lists a bunch of bits that are
4243 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
4244 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
4245 * Otherwise, we should fail with #UD. But most faulting conditions
4246 * have already been checked by hardware, prior to the VM-exit for
4247 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
4248 * that bit set to 1 in non-root mode.
4249 */
4250 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
4251 kvm_queue_exception(vcpu, UD_VECTOR);
4252 return 1;
4253 }
4254
4255 /* CPL=0 must be checked manually. */
4256 if (vmx_get_cpl(vcpu)) {
4257 kvm_inject_gp(vcpu, 0);
4258 return 1;
4259 }
4260
4261 if (vmx->nested.vmxon)
4262 return nested_vmx_failValid(vcpu,
4263 VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
4264
4265 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
4266 != VMXON_NEEDED_FEATURES) {
4267 kvm_inject_gp(vcpu, 0);
4268 return 1;
4269 }
4270
4271 if (nested_vmx_get_vmptr(vcpu, &vmptr))
4272 return 1;
4273
4274 /*
4275 * SDM 3: 24.11.5
4276 * The first 4 bytes of VMXON region contain the supported
4277 * VMCS revision identifier
4278 *
4279 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
4280 * which replaces physical address width with 32
4281 */
KarimAllah Ahmede0bf2662019-01-31 21:24:43 +01004282 if (!page_address_valid(vcpu, vmptr))
Sean Christopherson55d23752018-12-03 13:53:18 -08004283 return nested_vmx_failInvalid(vcpu);
4284
KarimAllah Ahmed2e408932019-01-31 21:24:31 +01004285 if (kvm_read_guest(vcpu->kvm, vmptr, &revision, sizeof(revision)) ||
4286 revision != VMCS12_REVISION)
Sean Christopherson55d23752018-12-03 13:53:18 -08004287 return nested_vmx_failInvalid(vcpu);
4288
Sean Christopherson55d23752018-12-03 13:53:18 -08004289 vmx->nested.vmxon_ptr = vmptr;
4290 ret = enter_vmx_operation(vcpu);
4291 if (ret)
4292 return ret;
4293
4294 return nested_vmx_succeed(vcpu);
4295}
4296
4297static inline void nested_release_vmcs12(struct kvm_vcpu *vcpu)
4298{
4299 struct vcpu_vmx *vmx = to_vmx(vcpu);
4300
4301 if (vmx->nested.current_vmptr == -1ull)
4302 return;
4303
4304 if (enable_shadow_vmcs) {
4305 /* copy to memory all shadowed fields in case
4306 they were modified */
4307 copy_shadow_to_vmcs12(vmx);
4308 vmx->nested.need_vmcs12_sync = false;
4309 vmx_disable_shadow_vmcs(vmx);
4310 }
4311 vmx->nested.posted_intr_nv = -1;
4312
4313 /* Flush VMCS12 to guest memory */
4314 kvm_vcpu_write_guest_page(vcpu,
4315 vmx->nested.current_vmptr >> PAGE_SHIFT,
4316 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
4317
4318 kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
4319
4320 vmx->nested.current_vmptr = -1ull;
4321}
4322
4323/* Emulate the VMXOFF instruction */
4324static int handle_vmoff(struct kvm_vcpu *vcpu)
4325{
4326 if (!nested_vmx_check_permission(vcpu))
4327 return 1;
4328 free_nested(vcpu);
4329 return nested_vmx_succeed(vcpu);
4330}
4331
4332/* Emulate the VMCLEAR instruction */
4333static int handle_vmclear(struct kvm_vcpu *vcpu)
4334{
4335 struct vcpu_vmx *vmx = to_vmx(vcpu);
4336 u32 zero = 0;
4337 gpa_t vmptr;
4338
4339 if (!nested_vmx_check_permission(vcpu))
4340 return 1;
4341
4342 if (nested_vmx_get_vmptr(vcpu, &vmptr))
4343 return 1;
4344
KarimAllah Ahmede0bf2662019-01-31 21:24:43 +01004345 if (!page_address_valid(vcpu, vmptr))
Sean Christopherson55d23752018-12-03 13:53:18 -08004346 return nested_vmx_failValid(vcpu,
4347 VMXERR_VMCLEAR_INVALID_ADDRESS);
4348
4349 if (vmptr == vmx->nested.vmxon_ptr)
4350 return nested_vmx_failValid(vcpu,
4351 VMXERR_VMCLEAR_VMXON_POINTER);
4352
KarimAllah Ahmeddee9c042019-01-31 21:24:42 +01004353 if (vmx->nested.hv_evmcs_map.hva) {
Sean Christopherson55d23752018-12-03 13:53:18 -08004354 if (vmptr == vmx->nested.hv_evmcs_vmptr)
4355 nested_release_evmcs(vcpu);
4356 } else {
4357 if (vmptr == vmx->nested.current_vmptr)
4358 nested_release_vmcs12(vcpu);
4359
4360 kvm_vcpu_write_guest(vcpu,
4361 vmptr + offsetof(struct vmcs12,
4362 launch_state),
4363 &zero, sizeof(zero));
4364 }
4365
4366 return nested_vmx_succeed(vcpu);
4367}
4368
4369static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
4370
4371/* Emulate the VMLAUNCH instruction */
4372static int handle_vmlaunch(struct kvm_vcpu *vcpu)
4373{
4374 return nested_vmx_run(vcpu, true);
4375}
4376
4377/* Emulate the VMRESUME instruction */
4378static int handle_vmresume(struct kvm_vcpu *vcpu)
4379{
4380
4381 return nested_vmx_run(vcpu, false);
4382}
4383
4384static int handle_vmread(struct kvm_vcpu *vcpu)
4385{
4386 unsigned long field;
4387 u64 field_value;
4388 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4389 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004390 int len;
Sean Christopherson55d23752018-12-03 13:53:18 -08004391 gva_t gva = 0;
4392 struct vmcs12 *vmcs12;
4393
4394 if (!nested_vmx_check_permission(vcpu))
4395 return 1;
4396
4397 if (to_vmx(vcpu)->nested.current_vmptr == -1ull)
4398 return nested_vmx_failInvalid(vcpu);
4399
4400 if (!is_guest_mode(vcpu))
4401 vmcs12 = get_vmcs12(vcpu);
4402 else {
4403 /*
4404 * When vmcs->vmcs_link_pointer is -1ull, any VMREAD
4405 * to shadowed-field sets the ALU flags for VMfailInvalid.
4406 */
4407 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull)
4408 return nested_vmx_failInvalid(vcpu);
4409 vmcs12 = get_shadow_vmcs12(vcpu);
4410 }
4411
4412 /* Decode instruction info and find the field to read */
4413 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
4414 /* Read the field, zero-extended to a u64 field_value */
4415 if (vmcs12_read_any(vmcs12, field, &field_value) < 0)
4416 return nested_vmx_failValid(vcpu,
4417 VMXERR_UNSUPPORTED_VMCS_COMPONENT);
4418
4419 /*
4420 * Now copy part of this value to register or memory, as requested.
4421 * Note that the number of bits actually copied is 32 or 64 depending
4422 * on the guest's mode (32 or 64 bit), not on the given field's length.
4423 */
4424 if (vmx_instruction_info & (1u << 10)) {
4425 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
4426 field_value);
4427 } else {
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004428 len = is_64_bit_mode(vcpu) ? 8 : 4;
Sean Christopherson55d23752018-12-03 13:53:18 -08004429 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004430 vmx_instruction_info, true, len, &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08004431 return 1;
4432 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004433 kvm_write_guest_virt_system(vcpu, gva, &field_value, len, NULL);
Sean Christopherson55d23752018-12-03 13:53:18 -08004434 }
4435
4436 return nested_vmx_succeed(vcpu);
4437}
4438
4439
4440static int handle_vmwrite(struct kvm_vcpu *vcpu)
4441{
4442 unsigned long field;
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004443 int len;
Sean Christopherson55d23752018-12-03 13:53:18 -08004444 gva_t gva;
4445 struct vcpu_vmx *vmx = to_vmx(vcpu);
4446 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4447 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
4448
4449 /* The value to write might be 32 or 64 bits, depending on L1's long
4450 * mode, and eventually we need to write that into a field of several
4451 * possible lengths. The code below first zero-extends the value to 64
4452 * bit (field_value), and then copies only the appropriate number of
4453 * bits into the vmcs12 field.
4454 */
4455 u64 field_value = 0;
4456 struct x86_exception e;
4457 struct vmcs12 *vmcs12;
4458
4459 if (!nested_vmx_check_permission(vcpu))
4460 return 1;
4461
4462 if (vmx->nested.current_vmptr == -1ull)
4463 return nested_vmx_failInvalid(vcpu);
4464
4465 if (vmx_instruction_info & (1u << 10))
4466 field_value = kvm_register_readl(vcpu,
4467 (((vmx_instruction_info) >> 3) & 0xf));
4468 else {
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004469 len = is_64_bit_mode(vcpu) ? 8 : 4;
Sean Christopherson55d23752018-12-03 13:53:18 -08004470 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004471 vmx_instruction_info, false, len, &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08004472 return 1;
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004473 if (kvm_read_guest_virt(vcpu, gva, &field_value, len, &e)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08004474 kvm_inject_page_fault(vcpu, &e);
4475 return 1;
4476 }
4477 }
4478
4479
4480 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
4481 /*
4482 * If the vCPU supports "VMWRITE to any supported field in the
4483 * VMCS," then the "read-only" fields are actually read/write.
4484 */
4485 if (vmcs_field_readonly(field) &&
4486 !nested_cpu_has_vmwrite_any_field(vcpu))
4487 return nested_vmx_failValid(vcpu,
4488 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
4489
4490 if (!is_guest_mode(vcpu))
4491 vmcs12 = get_vmcs12(vcpu);
4492 else {
4493 /*
4494 * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE
4495 * to shadowed-field sets the ALU flags for VMfailInvalid.
4496 */
4497 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull)
4498 return nested_vmx_failInvalid(vcpu);
4499 vmcs12 = get_shadow_vmcs12(vcpu);
4500 }
4501
4502 if (vmcs12_write_any(vmcs12, field, field_value) < 0)
4503 return nested_vmx_failValid(vcpu,
4504 VMXERR_UNSUPPORTED_VMCS_COMPONENT);
4505
4506 /*
4507 * Do not track vmcs12 dirty-state if in guest-mode
4508 * as we actually dirty shadow vmcs12 instead of vmcs12.
4509 */
4510 if (!is_guest_mode(vcpu)) {
4511 switch (field) {
4512#define SHADOW_FIELD_RW(x) case x:
4513#include "vmcs_shadow_fields.h"
4514 /*
4515 * The fields that can be updated by L1 without a vmexit are
4516 * always updated in the vmcs02, the others go down the slow
4517 * path of prepare_vmcs02.
4518 */
4519 break;
4520 default:
4521 vmx->nested.dirty_vmcs12 = true;
4522 break;
4523 }
4524 }
4525
4526 return nested_vmx_succeed(vcpu);
4527}
4528
4529static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
4530{
4531 vmx->nested.current_vmptr = vmptr;
4532 if (enable_shadow_vmcs) {
4533 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
4534 SECONDARY_EXEC_SHADOW_VMCS);
4535 vmcs_write64(VMCS_LINK_POINTER,
4536 __pa(vmx->vmcs01.shadow_vmcs));
4537 vmx->nested.need_vmcs12_sync = true;
4538 }
4539 vmx->nested.dirty_vmcs12 = true;
4540}
4541
4542/* Emulate the VMPTRLD instruction */
4543static int handle_vmptrld(struct kvm_vcpu *vcpu)
4544{
4545 struct vcpu_vmx *vmx = to_vmx(vcpu);
4546 gpa_t vmptr;
4547
4548 if (!nested_vmx_check_permission(vcpu))
4549 return 1;
4550
4551 if (nested_vmx_get_vmptr(vcpu, &vmptr))
4552 return 1;
4553
KarimAllah Ahmede0bf2662019-01-31 21:24:43 +01004554 if (!page_address_valid(vcpu, vmptr))
Sean Christopherson55d23752018-12-03 13:53:18 -08004555 return nested_vmx_failValid(vcpu,
4556 VMXERR_VMPTRLD_INVALID_ADDRESS);
4557
4558 if (vmptr == vmx->nested.vmxon_ptr)
4559 return nested_vmx_failValid(vcpu,
4560 VMXERR_VMPTRLD_VMXON_POINTER);
4561
4562 /* Forbid normal VMPTRLD if Enlightened version was used */
4563 if (vmx->nested.hv_evmcs)
4564 return 1;
4565
4566 if (vmx->nested.current_vmptr != vmptr) {
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01004567 struct kvm_host_map map;
Sean Christopherson55d23752018-12-03 13:53:18 -08004568 struct vmcs12 *new_vmcs12;
Sean Christopherson55d23752018-12-03 13:53:18 -08004569
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01004570 if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmptr), &map)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08004571 /*
4572 * Reads from an unbacked page return all 1s,
4573 * which means that the 32 bits located at the
4574 * given physical address won't match the required
4575 * VMCS12_REVISION identifier.
4576 */
Vitaly Kuznetsov826c1362019-01-09 18:22:56 +01004577 return nested_vmx_failValid(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08004578 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Sean Christopherson55d23752018-12-03 13:53:18 -08004579 }
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01004580
4581 new_vmcs12 = map.hva;
4582
Sean Christopherson55d23752018-12-03 13:53:18 -08004583 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
4584 (new_vmcs12->hdr.shadow_vmcs &&
4585 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01004586 kvm_vcpu_unmap(vcpu, &map, false);
Sean Christopherson55d23752018-12-03 13:53:18 -08004587 return nested_vmx_failValid(vcpu,
4588 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
4589 }
4590
4591 nested_release_vmcs12(vcpu);
4592
4593 /*
4594 * Load VMCS12 from guest memory since it is not already
4595 * cached.
4596 */
4597 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01004598 kvm_vcpu_unmap(vcpu, &map, false);
Sean Christopherson55d23752018-12-03 13:53:18 -08004599
4600 set_current_vmptr(vmx, vmptr);
4601 }
4602
4603 return nested_vmx_succeed(vcpu);
4604}
4605
4606/* Emulate the VMPTRST instruction */
4607static int handle_vmptrst(struct kvm_vcpu *vcpu)
4608{
4609 unsigned long exit_qual = vmcs_readl(EXIT_QUALIFICATION);
4610 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
4611 gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr;
4612 struct x86_exception e;
4613 gva_t gva;
4614
4615 if (!nested_vmx_check_permission(vcpu))
4616 return 1;
4617
4618 if (unlikely(to_vmx(vcpu)->nested.hv_evmcs))
4619 return 1;
4620
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004621 if (get_vmx_mem_address(vcpu, exit_qual, instr_info,
4622 true, sizeof(gpa_t), &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08004623 return 1;
4624 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
4625 if (kvm_write_guest_virt_system(vcpu, gva, (void *)&current_vmptr,
4626 sizeof(gpa_t), &e)) {
4627 kvm_inject_page_fault(vcpu, &e);
4628 return 1;
4629 }
4630 return nested_vmx_succeed(vcpu);
4631}
4632
4633/* Emulate the INVEPT instruction */
4634static int handle_invept(struct kvm_vcpu *vcpu)
4635{
4636 struct vcpu_vmx *vmx = to_vmx(vcpu);
4637 u32 vmx_instruction_info, types;
4638 unsigned long type;
4639 gva_t gva;
4640 struct x86_exception e;
4641 struct {
4642 u64 eptp, gpa;
4643 } operand;
4644
4645 if (!(vmx->nested.msrs.secondary_ctls_high &
4646 SECONDARY_EXEC_ENABLE_EPT) ||
4647 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
4648 kvm_queue_exception(vcpu, UD_VECTOR);
4649 return 1;
4650 }
4651
4652 if (!nested_vmx_check_permission(vcpu))
4653 return 1;
4654
4655 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
4656 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
4657
4658 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
4659
4660 if (type >= 32 || !(types & (1 << type)))
4661 return nested_vmx_failValid(vcpu,
4662 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
4663
4664 /* According to the Intel VMX instruction reference, the memory
4665 * operand is read even if it isn't needed (e.g., for type==global)
4666 */
4667 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004668 vmx_instruction_info, false, sizeof(operand), &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08004669 return 1;
4670 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
4671 kvm_inject_page_fault(vcpu, &e);
4672 return 1;
4673 }
4674
4675 switch (type) {
4676 case VMX_EPT_EXTENT_GLOBAL:
4677 /*
4678 * TODO: track mappings and invalidate
4679 * single context requests appropriately
4680 */
4681 case VMX_EPT_EXTENT_CONTEXT:
4682 kvm_mmu_sync_roots(vcpu);
4683 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
4684 break;
4685 default:
4686 BUG_ON(1);
4687 break;
4688 }
4689
4690 return nested_vmx_succeed(vcpu);
4691}
4692
4693static int handle_invvpid(struct kvm_vcpu *vcpu)
4694{
4695 struct vcpu_vmx *vmx = to_vmx(vcpu);
4696 u32 vmx_instruction_info;
4697 unsigned long type, types;
4698 gva_t gva;
4699 struct x86_exception e;
4700 struct {
4701 u64 vpid;
4702 u64 gla;
4703 } operand;
4704 u16 vpid02;
4705
4706 if (!(vmx->nested.msrs.secondary_ctls_high &
4707 SECONDARY_EXEC_ENABLE_VPID) ||
4708 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
4709 kvm_queue_exception(vcpu, UD_VECTOR);
4710 return 1;
4711 }
4712
4713 if (!nested_vmx_check_permission(vcpu))
4714 return 1;
4715
4716 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
4717 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
4718
4719 types = (vmx->nested.msrs.vpid_caps &
4720 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
4721
4722 if (type >= 32 || !(types & (1 << type)))
4723 return nested_vmx_failValid(vcpu,
4724 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
4725
4726 /* according to the intel vmx instruction reference, the memory
4727 * operand is read even if it isn't needed (e.g., for type==global)
4728 */
4729 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004730 vmx_instruction_info, false, sizeof(operand), &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08004731 return 1;
4732 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
4733 kvm_inject_page_fault(vcpu, &e);
4734 return 1;
4735 }
4736 if (operand.vpid >> 16)
4737 return nested_vmx_failValid(vcpu,
4738 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
4739
4740 vpid02 = nested_get_vpid02(vcpu);
4741 switch (type) {
4742 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
4743 if (!operand.vpid ||
4744 is_noncanonical_address(operand.gla, vcpu))
4745 return nested_vmx_failValid(vcpu,
4746 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
4747 if (cpu_has_vmx_invvpid_individual_addr()) {
4748 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
4749 vpid02, operand.gla);
4750 } else
4751 __vmx_flush_tlb(vcpu, vpid02, false);
4752 break;
4753 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
4754 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
4755 if (!operand.vpid)
4756 return nested_vmx_failValid(vcpu,
4757 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
4758 __vmx_flush_tlb(vcpu, vpid02, false);
4759 break;
4760 case VMX_VPID_EXTENT_ALL_CONTEXT:
4761 __vmx_flush_tlb(vcpu, vpid02, false);
4762 break;
4763 default:
4764 WARN_ON_ONCE(1);
4765 return kvm_skip_emulated_instruction(vcpu);
4766 }
4767
4768 return nested_vmx_succeed(vcpu);
4769}
4770
4771static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
4772 struct vmcs12 *vmcs12)
4773{
Sean Christopherson2b3eaf82019-04-30 10:36:19 -07004774 u32 index = kvm_rcx_read(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08004775 u64 address;
4776 bool accessed_dirty;
4777 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4778
4779 if (!nested_cpu_has_eptp_switching(vmcs12) ||
4780 !nested_cpu_has_ept(vmcs12))
4781 return 1;
4782
4783 if (index >= VMFUNC_EPTP_ENTRIES)
4784 return 1;
4785
4786
4787 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
4788 &address, index * 8, 8))
4789 return 1;
4790
4791 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
4792
4793 /*
4794 * If the (L2) guest does a vmfunc to the currently
4795 * active ept pointer, we don't have to do anything else
4796 */
4797 if (vmcs12->ept_pointer != address) {
4798 if (!valid_ept_address(vcpu, address))
4799 return 1;
4800
4801 kvm_mmu_unload(vcpu);
4802 mmu->ept_ad = accessed_dirty;
4803 mmu->mmu_role.base.ad_disabled = !accessed_dirty;
4804 vmcs12->ept_pointer = address;
4805 /*
4806 * TODO: Check what's the correct approach in case
4807 * mmu reload fails. Currently, we just let the next
4808 * reload potentially fail
4809 */
4810 kvm_mmu_reload(vcpu);
4811 }
4812
4813 return 0;
4814}
4815
4816static int handle_vmfunc(struct kvm_vcpu *vcpu)
4817{
4818 struct vcpu_vmx *vmx = to_vmx(vcpu);
4819 struct vmcs12 *vmcs12;
Sean Christopherson2b3eaf82019-04-30 10:36:19 -07004820 u32 function = kvm_rax_read(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08004821
4822 /*
4823 * VMFUNC is only supported for nested guests, but we always enable the
4824 * secondary control for simplicity; for non-nested mode, fake that we
4825 * didn't by injecting #UD.
4826 */
4827 if (!is_guest_mode(vcpu)) {
4828 kvm_queue_exception(vcpu, UD_VECTOR);
4829 return 1;
4830 }
4831
4832 vmcs12 = get_vmcs12(vcpu);
4833 if ((vmcs12->vm_function_control & (1 << function)) == 0)
4834 goto fail;
4835
4836 switch (function) {
4837 case 0:
4838 if (nested_vmx_eptp_switching(vcpu, vmcs12))
4839 goto fail;
4840 break;
4841 default:
4842 goto fail;
4843 }
4844 return kvm_skip_emulated_instruction(vcpu);
4845
4846fail:
4847 nested_vmx_vmexit(vcpu, vmx->exit_reason,
4848 vmcs_read32(VM_EXIT_INTR_INFO),
4849 vmcs_readl(EXIT_QUALIFICATION));
4850 return 1;
4851}
4852
4853
4854static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
4855 struct vmcs12 *vmcs12)
4856{
4857 unsigned long exit_qualification;
4858 gpa_t bitmap, last_bitmap;
4859 unsigned int port;
4860 int size;
4861 u8 b;
4862
4863 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
4864 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
4865
4866 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4867
4868 port = exit_qualification >> 16;
4869 size = (exit_qualification & 7) + 1;
4870
4871 last_bitmap = (gpa_t)-1;
4872 b = -1;
4873
4874 while (size > 0) {
4875 if (port < 0x8000)
4876 bitmap = vmcs12->io_bitmap_a;
4877 else if (port < 0x10000)
4878 bitmap = vmcs12->io_bitmap_b;
4879 else
4880 return true;
4881 bitmap += (port & 0x7fff) / 8;
4882
4883 if (last_bitmap != bitmap)
4884 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
4885 return true;
4886 if (b & (1 << (port & 7)))
4887 return true;
4888
4889 port++;
4890 size--;
4891 last_bitmap = bitmap;
4892 }
4893
4894 return false;
4895}
4896
4897/*
4898 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
4899 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
4900 * disinterest in the current event (read or write a specific MSR) by using an
4901 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
4902 */
4903static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
4904 struct vmcs12 *vmcs12, u32 exit_reason)
4905{
Sean Christopherson2b3eaf82019-04-30 10:36:19 -07004906 u32 msr_index = kvm_rcx_read(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08004907 gpa_t bitmap;
4908
4909 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
4910 return true;
4911
4912 /*
4913 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
4914 * for the four combinations of read/write and low/high MSR numbers.
4915 * First we need to figure out which of the four to use:
4916 */
4917 bitmap = vmcs12->msr_bitmap;
4918 if (exit_reason == EXIT_REASON_MSR_WRITE)
4919 bitmap += 2048;
4920 if (msr_index >= 0xc0000000) {
4921 msr_index -= 0xc0000000;
4922 bitmap += 1024;
4923 }
4924
4925 /* Then read the msr_index'th bit from this bitmap: */
4926 if (msr_index < 1024*8) {
4927 unsigned char b;
4928 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
4929 return true;
4930 return 1 & (b >> (msr_index & 7));
4931 } else
4932 return true; /* let L1 handle the wrong parameter */
4933}
4934
4935/*
4936 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
4937 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
4938 * intercept (via guest_host_mask etc.) the current event.
4939 */
4940static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
4941 struct vmcs12 *vmcs12)
4942{
4943 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4944 int cr = exit_qualification & 15;
4945 int reg;
4946 unsigned long val;
4947
4948 switch ((exit_qualification >> 4) & 3) {
4949 case 0: /* mov to cr */
4950 reg = (exit_qualification >> 8) & 15;
4951 val = kvm_register_readl(vcpu, reg);
4952 switch (cr) {
4953 case 0:
4954 if (vmcs12->cr0_guest_host_mask &
4955 (val ^ vmcs12->cr0_read_shadow))
4956 return true;
4957 break;
4958 case 3:
4959 if ((vmcs12->cr3_target_count >= 1 &&
4960 vmcs12->cr3_target_value0 == val) ||
4961 (vmcs12->cr3_target_count >= 2 &&
4962 vmcs12->cr3_target_value1 == val) ||
4963 (vmcs12->cr3_target_count >= 3 &&
4964 vmcs12->cr3_target_value2 == val) ||
4965 (vmcs12->cr3_target_count >= 4 &&
4966 vmcs12->cr3_target_value3 == val))
4967 return false;
4968 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
4969 return true;
4970 break;
4971 case 4:
4972 if (vmcs12->cr4_guest_host_mask &
4973 (vmcs12->cr4_read_shadow ^ val))
4974 return true;
4975 break;
4976 case 8:
4977 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
4978 return true;
4979 break;
4980 }
4981 break;
4982 case 2: /* clts */
4983 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
4984 (vmcs12->cr0_read_shadow & X86_CR0_TS))
4985 return true;
4986 break;
4987 case 1: /* mov from cr */
4988 switch (cr) {
4989 case 3:
4990 if (vmcs12->cpu_based_vm_exec_control &
4991 CPU_BASED_CR3_STORE_EXITING)
4992 return true;
4993 break;
4994 case 8:
4995 if (vmcs12->cpu_based_vm_exec_control &
4996 CPU_BASED_CR8_STORE_EXITING)
4997 return true;
4998 break;
4999 }
5000 break;
5001 case 3: /* lmsw */
5002 /*
5003 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
5004 * cr0. Other attempted changes are ignored, with no exit.
5005 */
5006 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
5007 if (vmcs12->cr0_guest_host_mask & 0xe &
5008 (val ^ vmcs12->cr0_read_shadow))
5009 return true;
5010 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
5011 !(vmcs12->cr0_read_shadow & 0x1) &&
5012 (val & 0x1))
5013 return true;
5014 break;
5015 }
5016 return false;
5017}
5018
5019static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
5020 struct vmcs12 *vmcs12, gpa_t bitmap)
5021{
5022 u32 vmx_instruction_info;
5023 unsigned long field;
5024 u8 b;
5025
5026 if (!nested_cpu_has_shadow_vmcs(vmcs12))
5027 return true;
5028
5029 /* Decode instruction info and find the field to access */
5030 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5031 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
5032
5033 /* Out-of-range fields always cause a VM exit from L2 to L1 */
5034 if (field >> 15)
5035 return true;
5036
5037 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
5038 return true;
5039
5040 return 1 & (b >> (field & 7));
5041}
5042
5043/*
5044 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
5045 * should handle it ourselves in L0 (and then continue L2). Only call this
5046 * when in is_guest_mode (L2).
5047 */
5048bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
5049{
5050 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
5051 struct vcpu_vmx *vmx = to_vmx(vcpu);
5052 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5053
5054 if (vmx->nested.nested_run_pending)
5055 return false;
5056
5057 if (unlikely(vmx->fail)) {
5058 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
5059 vmcs_read32(VM_INSTRUCTION_ERROR));
5060 return true;
5061 }
5062
5063 /*
5064 * The host physical addresses of some pages of guest memory
5065 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
5066 * Page). The CPU may write to these pages via their host
5067 * physical address while L2 is running, bypassing any
5068 * address-translation-based dirty tracking (e.g. EPT write
5069 * protection).
5070 *
5071 * Mark them dirty on every exit from L2 to prevent them from
5072 * getting out of sync with dirty tracking.
5073 */
5074 nested_mark_vmcs12_pages_dirty(vcpu);
5075
5076 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
5077 vmcs_readl(EXIT_QUALIFICATION),
5078 vmx->idt_vectoring_info,
5079 intr_info,
5080 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
5081 KVM_ISA_VMX);
5082
5083 switch (exit_reason) {
5084 case EXIT_REASON_EXCEPTION_NMI:
5085 if (is_nmi(intr_info))
5086 return false;
5087 else if (is_page_fault(intr_info))
5088 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
5089 else if (is_debug(intr_info) &&
5090 vcpu->guest_debug &
5091 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5092 return false;
5093 else if (is_breakpoint(intr_info) &&
5094 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
5095 return false;
5096 return vmcs12->exception_bitmap &
5097 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
5098 case EXIT_REASON_EXTERNAL_INTERRUPT:
5099 return false;
5100 case EXIT_REASON_TRIPLE_FAULT:
5101 return true;
5102 case EXIT_REASON_PENDING_INTERRUPT:
5103 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
5104 case EXIT_REASON_NMI_WINDOW:
5105 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
5106 case EXIT_REASON_TASK_SWITCH:
5107 return true;
5108 case EXIT_REASON_CPUID:
5109 return true;
5110 case EXIT_REASON_HLT:
5111 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
5112 case EXIT_REASON_INVD:
5113 return true;
5114 case EXIT_REASON_INVLPG:
5115 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
5116 case EXIT_REASON_RDPMC:
5117 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
5118 case EXIT_REASON_RDRAND:
5119 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
5120 case EXIT_REASON_RDSEED:
5121 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
5122 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
5123 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
5124 case EXIT_REASON_VMREAD:
5125 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
5126 vmcs12->vmread_bitmap);
5127 case EXIT_REASON_VMWRITE:
5128 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
5129 vmcs12->vmwrite_bitmap);
5130 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
5131 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
5132 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
5133 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
5134 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
5135 /*
5136 * VMX instructions trap unconditionally. This allows L1 to
5137 * emulate them for its L2 guest, i.e., allows 3-level nesting!
5138 */
5139 return true;
5140 case EXIT_REASON_CR_ACCESS:
5141 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
5142 case EXIT_REASON_DR_ACCESS:
5143 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
5144 case EXIT_REASON_IO_INSTRUCTION:
5145 return nested_vmx_exit_handled_io(vcpu, vmcs12);
5146 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
5147 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
5148 case EXIT_REASON_MSR_READ:
5149 case EXIT_REASON_MSR_WRITE:
5150 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
5151 case EXIT_REASON_INVALID_STATE:
5152 return true;
5153 case EXIT_REASON_MWAIT_INSTRUCTION:
5154 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
5155 case EXIT_REASON_MONITOR_TRAP_FLAG:
5156 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
5157 case EXIT_REASON_MONITOR_INSTRUCTION:
5158 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
5159 case EXIT_REASON_PAUSE_INSTRUCTION:
5160 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
5161 nested_cpu_has2(vmcs12,
5162 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
5163 case EXIT_REASON_MCE_DURING_VMENTRY:
5164 return false;
5165 case EXIT_REASON_TPR_BELOW_THRESHOLD:
5166 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
5167 case EXIT_REASON_APIC_ACCESS:
5168 case EXIT_REASON_APIC_WRITE:
5169 case EXIT_REASON_EOI_INDUCED:
5170 /*
5171 * The controls for "virtualize APIC accesses," "APIC-
5172 * register virtualization," and "virtual-interrupt
5173 * delivery" only come from vmcs12.
5174 */
5175 return true;
5176 case EXIT_REASON_EPT_VIOLATION:
5177 /*
5178 * L0 always deals with the EPT violation. If nested EPT is
5179 * used, and the nested mmu code discovers that the address is
5180 * missing in the guest EPT table (EPT12), the EPT violation
5181 * will be injected with nested_ept_inject_page_fault()
5182 */
5183 return false;
5184 case EXIT_REASON_EPT_MISCONFIG:
5185 /*
5186 * L2 never uses directly L1's EPT, but rather L0's own EPT
5187 * table (shadow on EPT) or a merged EPT table that L0 built
5188 * (EPT on EPT). So any problems with the structure of the
5189 * table is L0's fault.
5190 */
5191 return false;
5192 case EXIT_REASON_INVPCID:
5193 return
5194 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
5195 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
5196 case EXIT_REASON_WBINVD:
5197 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
5198 case EXIT_REASON_XSETBV:
5199 return true;
5200 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
5201 /*
5202 * This should never happen, since it is not possible to
5203 * set XSS to a non-zero value---neither in L1 nor in L2.
5204 * If if it were, XSS would have to be checked against
5205 * the XSS exit bitmap in vmcs12.
5206 */
5207 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
5208 case EXIT_REASON_PREEMPTION_TIMER:
5209 return false;
5210 case EXIT_REASON_PML_FULL:
5211 /* We emulate PML support to L1. */
5212 return false;
5213 case EXIT_REASON_VMFUNC:
5214 /* VM functions are emulated through L2->L0 vmexits. */
5215 return false;
5216 case EXIT_REASON_ENCLS:
5217 /* SGX is never exposed to L1 */
5218 return false;
5219 default:
5220 return true;
5221 }
5222}
5223
5224
5225static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
5226 struct kvm_nested_state __user *user_kvm_nested_state,
5227 u32 user_data_size)
5228{
5229 struct vcpu_vmx *vmx;
5230 struct vmcs12 *vmcs12;
5231 struct kvm_nested_state kvm_state = {
5232 .flags = 0,
5233 .format = 0,
5234 .size = sizeof(kvm_state),
5235 .vmx.vmxon_pa = -1ull,
5236 .vmx.vmcs_pa = -1ull,
5237 };
5238
5239 if (!vcpu)
5240 return kvm_state.size + 2 * VMCS12_SIZE;
5241
5242 vmx = to_vmx(vcpu);
5243 vmcs12 = get_vmcs12(vcpu);
5244
5245 if (nested_vmx_allowed(vcpu) && vmx->nested.enlightened_vmcs_enabled)
5246 kvm_state.flags |= KVM_STATE_NESTED_EVMCS;
5247
5248 if (nested_vmx_allowed(vcpu) &&
5249 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
5250 kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
5251 kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
5252
5253 if (vmx_has_valid_vmcs12(vcpu)) {
5254 kvm_state.size += VMCS12_SIZE;
5255
5256 if (is_guest_mode(vcpu) &&
5257 nested_cpu_has_shadow_vmcs(vmcs12) &&
5258 vmcs12->vmcs_link_pointer != -1ull)
5259 kvm_state.size += VMCS12_SIZE;
5260 }
5261
5262 if (vmx->nested.smm.vmxon)
5263 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
5264
5265 if (vmx->nested.smm.guest_mode)
5266 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
5267
5268 if (is_guest_mode(vcpu)) {
5269 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
5270
5271 if (vmx->nested.nested_run_pending)
5272 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
5273 }
5274 }
5275
5276 if (user_data_size < kvm_state.size)
5277 goto out;
5278
5279 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
5280 return -EFAULT;
5281
5282 if (!vmx_has_valid_vmcs12(vcpu))
5283 goto out;
5284
5285 /*
5286 * When running L2, the authoritative vmcs12 state is in the
5287 * vmcs02. When running L1, the authoritative vmcs12 state is
5288 * in the shadow or enlightened vmcs linked to vmcs01, unless
5289 * need_vmcs12_sync is set, in which case, the authoritative
5290 * vmcs12 state is in the vmcs12 already.
5291 */
5292 if (is_guest_mode(vcpu)) {
5293 sync_vmcs12(vcpu, vmcs12);
5294 } else if (!vmx->nested.need_vmcs12_sync) {
5295 if (vmx->nested.hv_evmcs)
5296 copy_enlightened_to_vmcs12(vmx);
5297 else if (enable_shadow_vmcs)
5298 copy_shadow_to_vmcs12(vmx);
5299 }
5300
Tom Roeder3a33d032019-01-24 13:48:20 -08005301 /*
5302 * Copy over the full allocated size of vmcs12 rather than just the size
5303 * of the struct.
5304 */
5305 if (copy_to_user(user_kvm_nested_state->data, vmcs12, VMCS12_SIZE))
Sean Christopherson55d23752018-12-03 13:53:18 -08005306 return -EFAULT;
5307
5308 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
5309 vmcs12->vmcs_link_pointer != -1ull) {
5310 if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE,
Tom Roeder3a33d032019-01-24 13:48:20 -08005311 get_shadow_vmcs12(vcpu), VMCS12_SIZE))
Sean Christopherson55d23752018-12-03 13:53:18 -08005312 return -EFAULT;
5313 }
5314
5315out:
5316 return kvm_state.size;
5317}
5318
5319/*
5320 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
5321 */
5322void vmx_leave_nested(struct kvm_vcpu *vcpu)
5323{
5324 if (is_guest_mode(vcpu)) {
5325 to_vmx(vcpu)->nested.nested_run_pending = 0;
5326 nested_vmx_vmexit(vcpu, -1, 0, 0);
5327 }
5328 free_nested(vcpu);
5329}
5330
5331static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
5332 struct kvm_nested_state __user *user_kvm_nested_state,
5333 struct kvm_nested_state *kvm_state)
5334{
5335 struct vcpu_vmx *vmx = to_vmx(vcpu);
5336 struct vmcs12 *vmcs12;
5337 u32 exit_qual;
5338 int ret;
5339
5340 if (kvm_state->format != 0)
5341 return -EINVAL;
5342
Sean Christopherson55d23752018-12-03 13:53:18 -08005343 if (!nested_vmx_allowed(vcpu))
5344 return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
5345
5346 if (kvm_state->vmx.vmxon_pa == -1ull) {
5347 if (kvm_state->vmx.smm.flags)
5348 return -EINVAL;
5349
5350 if (kvm_state->vmx.vmcs_pa != -1ull)
5351 return -EINVAL;
5352
5353 vmx_leave_nested(vcpu);
5354 return 0;
5355 }
5356
5357 if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
5358 return -EINVAL;
5359
5360 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
5361 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
5362 return -EINVAL;
5363
5364 if (kvm_state->vmx.smm.flags &
5365 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
5366 return -EINVAL;
5367
5368 /*
5369 * SMM temporarily disables VMX, so we cannot be in guest mode,
5370 * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags
5371 * must be zero.
5372 */
5373 if (is_smm(vcpu) ? kvm_state->flags : kvm_state->vmx.smm.flags)
5374 return -EINVAL;
5375
5376 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
5377 !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
5378 return -EINVAL;
5379
5380 vmx_leave_nested(vcpu);
5381 if (kvm_state->vmx.vmxon_pa == -1ull)
5382 return 0;
5383
Aaron Lewis332d0792019-05-02 11:31:33 -07005384 if (kvm_state->flags & KVM_STATE_NESTED_EVMCS)
5385 nested_enable_evmcs(vcpu, NULL);
5386
Sean Christopherson55d23752018-12-03 13:53:18 -08005387 vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
5388 ret = enter_vmx_operation(vcpu);
5389 if (ret)
5390 return ret;
5391
5392 /* Empty 'VMXON' state is permitted */
Jim Mattsone8ab8d22019-01-17 11:55:58 -08005393 if (kvm_state->size < sizeof(*kvm_state) + sizeof(*vmcs12))
Sean Christopherson55d23752018-12-03 13:53:18 -08005394 return 0;
5395
5396 if (kvm_state->vmx.vmcs_pa != -1ull) {
5397 if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
5398 !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
5399 return -EINVAL;
5400
5401 set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
5402 } else if (kvm_state->flags & KVM_STATE_NESTED_EVMCS) {
5403 /*
5404 * Sync eVMCS upon entry as we may not have
5405 * HV_X64_MSR_VP_ASSIST_PAGE set up yet.
5406 */
5407 vmx->nested.need_vmcs12_sync = true;
5408 } else {
5409 return -EINVAL;
5410 }
5411
5412 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
5413 vmx->nested.smm.vmxon = true;
5414 vmx->nested.vmxon = false;
5415
5416 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
5417 vmx->nested.smm.guest_mode = true;
5418 }
5419
5420 vmcs12 = get_vmcs12(vcpu);
5421 if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
5422 return -EFAULT;
5423
5424 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
5425 return -EINVAL;
5426
5427 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
5428 return 0;
5429
Sean Christopherson21be4ca2019-05-08 11:04:32 -07005430 vmx->nested.nested_run_pending =
5431 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
5432
5433 ret = -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08005434 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
5435 vmcs12->vmcs_link_pointer != -1ull) {
5436 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
5437
Paolo Bonzinidb809272019-05-20 11:55:36 +02005438 if (kvm_state->size < sizeof(*kvm_state) + VMCS12_SIZE + sizeof(*vmcs12))
Sean Christopherson21be4ca2019-05-08 11:04:32 -07005439 goto error_guest_mode;
Sean Christopherson55d23752018-12-03 13:53:18 -08005440
5441 if (copy_from_user(shadow_vmcs12,
5442 user_kvm_nested_state->data + VMCS12_SIZE,
Sean Christopherson21be4ca2019-05-08 11:04:32 -07005443 sizeof(*vmcs12))) {
5444 ret = -EFAULT;
5445 goto error_guest_mode;
5446 }
Sean Christopherson55d23752018-12-03 13:53:18 -08005447
5448 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
5449 !shadow_vmcs12->hdr.shadow_vmcs)
Sean Christopherson21be4ca2019-05-08 11:04:32 -07005450 goto error_guest_mode;
Sean Christopherson55d23752018-12-03 13:53:18 -08005451 }
5452
Sean Christopherson5478ba32019-04-11 12:18:06 -07005453 if (nested_vmx_check_controls(vcpu, vmcs12) ||
5454 nested_vmx_check_host_state(vcpu, vmcs12) ||
5455 nested_vmx_check_guest_state(vcpu, vmcs12, &exit_qual))
Sean Christopherson21be4ca2019-05-08 11:04:32 -07005456 goto error_guest_mode;
Sean Christopherson55d23752018-12-03 13:53:18 -08005457
5458 vmx->nested.dirty_vmcs12 = true;
5459 ret = nested_vmx_enter_non_root_mode(vcpu, false);
Sean Christopherson21be4ca2019-05-08 11:04:32 -07005460 if (ret)
5461 goto error_guest_mode;
Sean Christopherson55d23752018-12-03 13:53:18 -08005462
5463 return 0;
Sean Christopherson21be4ca2019-05-08 11:04:32 -07005464
5465error_guest_mode:
5466 vmx->nested.nested_run_pending = 0;
5467 return ret;
Sean Christopherson55d23752018-12-03 13:53:18 -08005468}
5469
5470void nested_vmx_vcpu_setup(void)
5471{
5472 if (enable_shadow_vmcs) {
5473 /*
5474 * At vCPU creation, "VMWRITE to any supported field
5475 * in the VMCS" is supported, so use the more
5476 * permissive vmx_vmread_bitmap to specify both read
5477 * and write permissions for the shadow VMCS.
5478 */
5479 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
5480 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
5481 }
5482}
5483
5484/*
5485 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
5486 * returned for the various VMX controls MSRs when nested VMX is enabled.
5487 * The same values should also be used to verify that vmcs12 control fields are
5488 * valid during nested entry from L1 to L2.
5489 * Each of these control msrs has a low and high 32-bit half: A low bit is on
5490 * if the corresponding bit in the (32-bit) control field *must* be on, and a
5491 * bit in the high half is on if the corresponding bit in the control field
5492 * may be on. See also vmx_control_verify().
5493 */
5494void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, u32 ept_caps,
5495 bool apicv)
5496{
5497 /*
5498 * Note that as a general rule, the high half of the MSRs (bits in
5499 * the control fields which may be 1) should be initialized by the
5500 * intersection of the underlying hardware's MSR (i.e., features which
5501 * can be supported) and the list of features we want to expose -
5502 * because they are known to be properly supported in our code.
5503 * Also, usually, the low half of the MSRs (bits which must be 1) can
5504 * be set to 0, meaning that L1 may turn off any of these bits. The
5505 * reason is that if one of these bits is necessary, it will appear
5506 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
5507 * fields of vmcs01 and vmcs02, will turn these bits off - and
5508 * nested_vmx_exit_reflected() will not pass related exits to L1.
5509 * These rules have exceptions below.
5510 */
5511
5512 /* pin-based controls */
5513 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
5514 msrs->pinbased_ctls_low,
5515 msrs->pinbased_ctls_high);
5516 msrs->pinbased_ctls_low |=
5517 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
5518 msrs->pinbased_ctls_high &=
5519 PIN_BASED_EXT_INTR_MASK |
5520 PIN_BASED_NMI_EXITING |
5521 PIN_BASED_VIRTUAL_NMIS |
5522 (apicv ? PIN_BASED_POSTED_INTR : 0);
5523 msrs->pinbased_ctls_high |=
5524 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
5525 PIN_BASED_VMX_PREEMPTION_TIMER;
5526
5527 /* exit controls */
5528 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
5529 msrs->exit_ctls_low,
5530 msrs->exit_ctls_high);
5531 msrs->exit_ctls_low =
5532 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
5533
5534 msrs->exit_ctls_high &=
5535#ifdef CONFIG_X86_64
5536 VM_EXIT_HOST_ADDR_SPACE_SIZE |
5537#endif
5538 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
5539 msrs->exit_ctls_high |=
5540 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
5541 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
5542 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
5543
5544 /* We support free control of debug control saving. */
5545 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
5546
5547 /* entry controls */
5548 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
5549 msrs->entry_ctls_low,
5550 msrs->entry_ctls_high);
5551 msrs->entry_ctls_low =
5552 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
5553 msrs->entry_ctls_high &=
5554#ifdef CONFIG_X86_64
5555 VM_ENTRY_IA32E_MODE |
5556#endif
5557 VM_ENTRY_LOAD_IA32_PAT;
5558 msrs->entry_ctls_high |=
5559 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
5560
5561 /* We support free control of debug control loading. */
5562 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
5563
5564 /* cpu-based controls */
5565 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
5566 msrs->procbased_ctls_low,
5567 msrs->procbased_ctls_high);
5568 msrs->procbased_ctls_low =
5569 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
5570 msrs->procbased_ctls_high &=
5571 CPU_BASED_VIRTUAL_INTR_PENDING |
5572 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
5573 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
5574 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
5575 CPU_BASED_CR3_STORE_EXITING |
5576#ifdef CONFIG_X86_64
5577 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
5578#endif
5579 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
5580 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
5581 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
5582 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
5583 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
5584 /*
5585 * We can allow some features even when not supported by the
5586 * hardware. For example, L1 can specify an MSR bitmap - and we
5587 * can use it to avoid exits to L1 - even when L0 runs L2
5588 * without MSR bitmaps.
5589 */
5590 msrs->procbased_ctls_high |=
5591 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
5592 CPU_BASED_USE_MSR_BITMAPS;
5593
5594 /* We support free control of CR3 access interception. */
5595 msrs->procbased_ctls_low &=
5596 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
5597
5598 /*
5599 * secondary cpu-based controls. Do not include those that
5600 * depend on CPUID bits, they are added later by vmx_cpuid_update.
5601 */
Vitaly Kuznetsov6b1971c2019-02-07 11:42:14 +01005602 if (msrs->procbased_ctls_high & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)
5603 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
5604 msrs->secondary_ctls_low,
5605 msrs->secondary_ctls_high);
5606
Sean Christopherson55d23752018-12-03 13:53:18 -08005607 msrs->secondary_ctls_low = 0;
5608 msrs->secondary_ctls_high &=
5609 SECONDARY_EXEC_DESC |
5610 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
5611 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5612 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
5613 SECONDARY_EXEC_WBINVD_EXITING;
5614
5615 /*
5616 * We can emulate "VMCS shadowing," even if the hardware
5617 * doesn't support it.
5618 */
5619 msrs->secondary_ctls_high |=
5620 SECONDARY_EXEC_SHADOW_VMCS;
5621
5622 if (enable_ept) {
5623 /* nested EPT: emulate EPT also to L1 */
5624 msrs->secondary_ctls_high |=
5625 SECONDARY_EXEC_ENABLE_EPT;
5626 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
5627 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
5628 if (cpu_has_vmx_ept_execute_only())
5629 msrs->ept_caps |=
5630 VMX_EPT_EXECUTE_ONLY_BIT;
5631 msrs->ept_caps &= ept_caps;
5632 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
5633 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
5634 VMX_EPT_1GB_PAGE_BIT;
5635 if (enable_ept_ad_bits) {
5636 msrs->secondary_ctls_high |=
5637 SECONDARY_EXEC_ENABLE_PML;
5638 msrs->ept_caps |= VMX_EPT_AD_BIT;
5639 }
5640 }
5641
5642 if (cpu_has_vmx_vmfunc()) {
5643 msrs->secondary_ctls_high |=
5644 SECONDARY_EXEC_ENABLE_VMFUNC;
5645 /*
5646 * Advertise EPTP switching unconditionally
5647 * since we emulate it
5648 */
5649 if (enable_ept)
5650 msrs->vmfunc_controls =
5651 VMX_VMFUNC_EPTP_SWITCHING;
5652 }
5653
5654 /*
5655 * Old versions of KVM use the single-context version without
5656 * checking for support, so declare that it is supported even
5657 * though it is treated as global context. The alternative is
5658 * not failing the single-context invvpid, and it is worse.
5659 */
5660 if (enable_vpid) {
5661 msrs->secondary_ctls_high |=
5662 SECONDARY_EXEC_ENABLE_VPID;
5663 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
5664 VMX_VPID_EXTENT_SUPPORTED_MASK;
5665 }
5666
5667 if (enable_unrestricted_guest)
5668 msrs->secondary_ctls_high |=
5669 SECONDARY_EXEC_UNRESTRICTED_GUEST;
5670
5671 if (flexpriority_enabled)
5672 msrs->secondary_ctls_high |=
5673 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
5674
5675 /* miscellaneous data */
5676 rdmsr(MSR_IA32_VMX_MISC,
5677 msrs->misc_low,
5678 msrs->misc_high);
5679 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
5680 msrs->misc_low |=
5681 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
5682 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
5683 VMX_MISC_ACTIVITY_HLT;
5684 msrs->misc_high = 0;
5685
5686 /*
5687 * This MSR reports some information about VMX support. We
5688 * should return information about the VMX we emulate for the
5689 * guest, and the VMCS structure we give it - not about the
5690 * VMX support of the underlying hardware.
5691 */
5692 msrs->basic =
5693 VMCS12_REVISION |
5694 VMX_BASIC_TRUE_CTLS |
5695 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
5696 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
5697
5698 if (cpu_has_vmx_basic_inout())
5699 msrs->basic |= VMX_BASIC_INOUT;
5700
5701 /*
5702 * These MSRs specify bits which the guest must keep fixed on
5703 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
5704 * We picked the standard core2 setting.
5705 */
5706#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
5707#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
5708 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
5709 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
5710
5711 /* These MSRs specify bits which the guest must keep fixed off. */
5712 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
5713 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
5714
5715 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
5716 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
5717}
5718
5719void nested_vmx_hardware_unsetup(void)
5720{
5721 int i;
5722
5723 if (enable_shadow_vmcs) {
5724 for (i = 0; i < VMX_BITMAP_NR; i++)
5725 free_page((unsigned long)vmx_bitmap[i]);
5726 }
5727}
5728
5729__init int nested_vmx_hardware_setup(int (*exit_handlers[])(struct kvm_vcpu *))
5730{
5731 int i;
5732
Paolo Bonzini2b279242019-04-15 15:57:19 +02005733 /*
5734 * Without EPT it is not possible to restore L1's CR3 and PDPTR on
5735 * VMfail, because they are not available in vmcs01. Just always
5736 * use hardware checks.
5737 */
5738 if (!enable_ept)
5739 nested_early_check = 1;
5740
Sean Christopherson55d23752018-12-03 13:53:18 -08005741 if (!cpu_has_vmx_shadow_vmcs())
5742 enable_shadow_vmcs = 0;
5743 if (enable_shadow_vmcs) {
5744 for (i = 0; i < VMX_BITMAP_NR; i++) {
Ben Gardon41836832019-02-11 11:02:52 -08005745 /*
5746 * The vmx_bitmap is not tied to a VM and so should
5747 * not be charged to a memcg.
5748 */
Sean Christopherson55d23752018-12-03 13:53:18 -08005749 vmx_bitmap[i] = (unsigned long *)
5750 __get_free_page(GFP_KERNEL);
5751 if (!vmx_bitmap[i]) {
5752 nested_vmx_hardware_unsetup();
5753 return -ENOMEM;
5754 }
5755 }
5756
5757 init_vmcs_shadow_fields();
5758 }
5759
5760 exit_handlers[EXIT_REASON_VMCLEAR] = handle_vmclear,
5761 exit_handlers[EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
5762 exit_handlers[EXIT_REASON_VMPTRLD] = handle_vmptrld,
5763 exit_handlers[EXIT_REASON_VMPTRST] = handle_vmptrst,
5764 exit_handlers[EXIT_REASON_VMREAD] = handle_vmread,
5765 exit_handlers[EXIT_REASON_VMRESUME] = handle_vmresume,
5766 exit_handlers[EXIT_REASON_VMWRITE] = handle_vmwrite,
5767 exit_handlers[EXIT_REASON_VMOFF] = handle_vmoff,
5768 exit_handlers[EXIT_REASON_VMON] = handle_vmon,
5769 exit_handlers[EXIT_REASON_INVEPT] = handle_invept,
5770 exit_handlers[EXIT_REASON_INVVPID] = handle_invvpid,
5771 exit_handlers[EXIT_REASON_VMFUNC] = handle_vmfunc,
5772
5773 kvm_x86_ops->check_nested_events = vmx_check_nested_events;
5774 kvm_x86_ops->get_nested_state = vmx_get_nested_state;
5775 kvm_x86_ops->set_nested_state = vmx_set_nested_state;
5776 kvm_x86_ops->get_vmcs12_pages = nested_get_vmcs12_pages,
5777 kvm_x86_ops->nested_enable_evmcs = nested_enable_evmcs;
Vitaly Kuznetsove2e871a2018-12-10 18:21:55 +01005778 kvm_x86_ops->nested_get_evmcs_version = nested_get_evmcs_version;
Sean Christopherson55d23752018-12-03 13:53:18 -08005779
5780 return 0;
5781}