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