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