blob: 0c2389d0fdafe23c9e90cf4e05e893e100739f8e [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0
2
Olivier Deprez157378f2022-04-04 15:47:50 +02003#include <linux/objtool.h>
David Brazdil0f672f62019-12-10 10:32:29 +00004#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"
Olivier Deprez157378f2022-04-04 15:47:50 +020013#include "pmu.h"
David Brazdil0f672f62019-12-10 10:32:29 +000014#include "trace.h"
15#include "x86.h"
16
17static bool __read_mostly enable_shadow_vmcs = 1;
18module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
19
20static bool __read_mostly nested_early_check = 0;
21module_param(nested_early_check, bool, S_IRUGO);
22
23#define CC(consistency_check) \
24({ \
25 bool failed = (consistency_check); \
26 if (failed) \
27 trace_kvm_nested_vmenter_failed(#consistency_check, 0); \
28 failed; \
29})
30
31/*
32 * Hyper-V requires all of these, so mark them as supported even though
33 * they are just treated the same as all-context.
34 */
35#define VMX_VPID_EXTENT_SUPPORTED_MASK \
36 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
37 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
38 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
39 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
40
41#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
42
43enum {
44 VMX_VMREAD_BITMAP,
45 VMX_VMWRITE_BITMAP,
46 VMX_BITMAP_NR
47};
48static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
49
50#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
51#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
52
53struct shadow_vmcs_field {
54 u16 encoding;
55 u16 offset;
56};
57static struct shadow_vmcs_field shadow_read_only_fields[] = {
58#define SHADOW_FIELD_RO(x, y) { x, offsetof(struct vmcs12, y) },
59#include "vmcs_shadow_fields.h"
60};
61static int max_shadow_read_only_fields =
62 ARRAY_SIZE(shadow_read_only_fields);
63
64static struct shadow_vmcs_field shadow_read_write_fields[] = {
65#define SHADOW_FIELD_RW(x, y) { x, offsetof(struct vmcs12, y) },
66#include "vmcs_shadow_fields.h"
67};
68static int max_shadow_read_write_fields =
69 ARRAY_SIZE(shadow_read_write_fields);
70
71static void init_vmcs_shadow_fields(void)
72{
73 int i, j;
74
75 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
76 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
77
78 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
79 struct shadow_vmcs_field entry = shadow_read_only_fields[i];
80 u16 field = entry.encoding;
81
82 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
83 (i + 1 == max_shadow_read_only_fields ||
84 shadow_read_only_fields[i + 1].encoding != field + 1))
85 pr_err("Missing field from shadow_read_only_field %x\n",
86 field + 1);
87
88 clear_bit(field, vmx_vmread_bitmap);
89 if (field & 1)
90#ifdef CONFIG_X86_64
91 continue;
92#else
93 entry.offset += sizeof(u32);
94#endif
95 shadow_read_only_fields[j++] = entry;
96 }
97 max_shadow_read_only_fields = j;
98
99 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
100 struct shadow_vmcs_field entry = shadow_read_write_fields[i];
101 u16 field = entry.encoding;
102
103 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
104 (i + 1 == max_shadow_read_write_fields ||
105 shadow_read_write_fields[i + 1].encoding != field + 1))
106 pr_err("Missing field from shadow_read_write_field %x\n",
107 field + 1);
108
109 WARN_ONCE(field >= GUEST_ES_AR_BYTES &&
110 field <= GUEST_TR_AR_BYTES,
111 "Update vmcs12_write_any() to drop reserved bits from AR_BYTES");
112
113 /*
114 * PML and the preemption timer can be emulated, but the
115 * processor cannot vmwrite to fields that don't exist
116 * on bare metal.
117 */
118 switch (field) {
119 case GUEST_PML_INDEX:
120 if (!cpu_has_vmx_pml())
121 continue;
122 break;
123 case VMX_PREEMPTION_TIMER_VALUE:
124 if (!cpu_has_vmx_preemption_timer())
125 continue;
126 break;
127 case GUEST_INTR_STATUS:
128 if (!cpu_has_vmx_apicv())
129 continue;
130 break;
131 default:
132 break;
133 }
134
135 clear_bit(field, vmx_vmwrite_bitmap);
136 clear_bit(field, vmx_vmread_bitmap);
137 if (field & 1)
138#ifdef CONFIG_X86_64
139 continue;
140#else
141 entry.offset += sizeof(u32);
142#endif
143 shadow_read_write_fields[j++] = entry;
144 }
145 max_shadow_read_write_fields = j;
146}
147
148/*
149 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
150 * set the success or error code of an emulated VMX instruction (as specified
151 * by Vol 2B, VMX Instruction Reference, "Conventions"), and skip the emulated
152 * instruction.
153 */
154static int nested_vmx_succeed(struct kvm_vcpu *vcpu)
155{
156 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
157 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
158 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
159 return kvm_skip_emulated_instruction(vcpu);
160}
161
162static int nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
163{
164 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
165 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
166 X86_EFLAGS_SF | X86_EFLAGS_OF))
167 | X86_EFLAGS_CF);
168 return kvm_skip_emulated_instruction(vcpu);
169}
170
171static int nested_vmx_failValid(struct kvm_vcpu *vcpu,
172 u32 vm_instruction_error)
173{
David Brazdil0f672f62019-12-10 10:32:29 +0000174 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
175 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
176 X86_EFLAGS_SF | X86_EFLAGS_OF))
177 | X86_EFLAGS_ZF);
178 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
179 /*
180 * We don't need to force a shadow sync because
181 * VM_INSTRUCTION_ERROR is not shadowed
182 */
183 return kvm_skip_emulated_instruction(vcpu);
184}
185
Olivier Deprez157378f2022-04-04 15:47:50 +0200186static int nested_vmx_fail(struct kvm_vcpu *vcpu, u32 vm_instruction_error)
187{
188 struct vcpu_vmx *vmx = to_vmx(vcpu);
189
190 /*
191 * failValid writes the error number to the current VMCS, which
192 * can't be done if there isn't a current VMCS.
193 */
194 if (vmx->nested.current_vmptr == -1ull && !vmx->nested.hv_evmcs)
195 return nested_vmx_failInvalid(vcpu);
196
197 return nested_vmx_failValid(vcpu, vm_instruction_error);
198}
199
David Brazdil0f672f62019-12-10 10:32:29 +0000200static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
201{
202 /* TODO: not to reset guest simply here. */
203 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
204 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
205}
206
207static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
208{
209 return fixed_bits_valid(control, low, high);
210}
211
212static inline u64 vmx_control_msr(u32 low, u32 high)
213{
214 return low | ((u64)high << 32);
215}
216
217static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
218{
219 secondary_exec_controls_clearbit(vmx, SECONDARY_EXEC_SHADOW_VMCS);
220 vmcs_write64(VMCS_LINK_POINTER, -1ull);
221 vmx->nested.need_vmcs12_to_shadow_sync = false;
222}
223
224static inline void nested_release_evmcs(struct kvm_vcpu *vcpu)
225{
226 struct vcpu_vmx *vmx = to_vmx(vcpu);
227
228 if (!vmx->nested.hv_evmcs)
229 return;
230
231 kvm_vcpu_unmap(vcpu, &vmx->nested.hv_evmcs_map, true);
Olivier Deprez0e641232021-09-23 10:07:05 +0200232 vmx->nested.hv_evmcs_vmptr = 0;
David Brazdil0f672f62019-12-10 10:32:29 +0000233 vmx->nested.hv_evmcs = NULL;
234}
235
David Brazdil0f672f62019-12-10 10:32:29 +0000236static void vmx_sync_vmcs_host_state(struct vcpu_vmx *vmx,
237 struct loaded_vmcs *prev)
238{
239 struct vmcs_host_state *dest, *src;
240
241 if (unlikely(!vmx->guest_state_loaded))
242 return;
243
244 src = &prev->host_state;
245 dest = &vmx->loaded_vmcs->host_state;
246
247 vmx_set_host_fs_gs(dest, src->fs_sel, src->gs_sel, src->fs_base, src->gs_base);
248 dest->ldt_sel = src->ldt_sel;
249#ifdef CONFIG_X86_64
250 dest->ds_sel = src->ds_sel;
251 dest->es_sel = src->es_sel;
252#endif
253}
254
255static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
256{
257 struct vcpu_vmx *vmx = to_vmx(vcpu);
258 struct loaded_vmcs *prev;
259 int cpu;
260
Olivier Deprez157378f2022-04-04 15:47:50 +0200261 if (WARN_ON_ONCE(vmx->loaded_vmcs == vmcs))
David Brazdil0f672f62019-12-10 10:32:29 +0000262 return;
263
264 cpu = get_cpu();
265 prev = vmx->loaded_vmcs;
266 vmx->loaded_vmcs = vmcs;
Olivier Deprez0e641232021-09-23 10:07:05 +0200267 vmx_vcpu_load_vmcs(vcpu, cpu, prev);
David Brazdil0f672f62019-12-10 10:32:29 +0000268 vmx_sync_vmcs_host_state(vmx, prev);
269 put_cpu();
270
Olivier Deprez157378f2022-04-04 15:47:50 +0200271 vmx_register_cache_reset(vcpu);
272}
273
274/*
275 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
276 * just stops using VMX.
277 */
278static void free_nested(struct kvm_vcpu *vcpu)
279{
280 struct vcpu_vmx *vmx = to_vmx(vcpu);
281
282 if (WARN_ON_ONCE(vmx->loaded_vmcs != &vmx->vmcs01))
283 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
284
285 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
286 return;
287
288 kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
289
290 vmx->nested.vmxon = false;
291 vmx->nested.smm.vmxon = false;
292 free_vpid(vmx->nested.vpid02);
293 vmx->nested.posted_intr_nv = -1;
294 vmx->nested.current_vmptr = -1ull;
295 if (enable_shadow_vmcs) {
296 vmx_disable_shadow_vmcs(vmx);
297 vmcs_clear(vmx->vmcs01.shadow_vmcs);
298 free_vmcs(vmx->vmcs01.shadow_vmcs);
299 vmx->vmcs01.shadow_vmcs = NULL;
300 }
301 kfree(vmx->nested.cached_vmcs12);
302 vmx->nested.cached_vmcs12 = NULL;
303 kfree(vmx->nested.cached_shadow_vmcs12);
304 vmx->nested.cached_shadow_vmcs12 = NULL;
305 /* Unpin physical memory we referred to in the vmcs02 */
306 if (vmx->nested.apic_access_page) {
307 kvm_release_page_clean(vmx->nested.apic_access_page);
308 vmx->nested.apic_access_page = NULL;
309 }
310 kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map, true);
311 kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map, true);
312 vmx->nested.pi_desc = NULL;
313
314 kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
315
316 nested_release_evmcs(vcpu);
317
318 free_loaded_vmcs(&vmx->nested.vmcs02);
David Brazdil0f672f62019-12-10 10:32:29 +0000319}
320
321/*
322 * Ensure that the current vmcs of the logical processor is the
323 * vmcs01 of the vcpu before calling free_nested().
324 */
325void nested_vmx_free_vcpu(struct kvm_vcpu *vcpu)
326{
327 vcpu_load(vcpu);
328 vmx_leave_nested(vcpu);
David Brazdil0f672f62019-12-10 10:32:29 +0000329 vcpu_put(vcpu);
330}
331
332static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
333 struct x86_exception *fault)
334{
335 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
336 struct vcpu_vmx *vmx = to_vmx(vcpu);
Olivier Deprez157378f2022-04-04 15:47:50 +0200337 u32 vm_exit_reason;
David Brazdil0f672f62019-12-10 10:32:29 +0000338 unsigned long exit_qualification = vcpu->arch.exit_qualification;
339
340 if (vmx->nested.pml_full) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200341 vm_exit_reason = EXIT_REASON_PML_FULL;
David Brazdil0f672f62019-12-10 10:32:29 +0000342 vmx->nested.pml_full = false;
343 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
344 } else if (fault->error_code & PFERR_RSVD_MASK)
Olivier Deprez157378f2022-04-04 15:47:50 +0200345 vm_exit_reason = EXIT_REASON_EPT_MISCONFIG;
David Brazdil0f672f62019-12-10 10:32:29 +0000346 else
Olivier Deprez157378f2022-04-04 15:47:50 +0200347 vm_exit_reason = EXIT_REASON_EPT_VIOLATION;
David Brazdil0f672f62019-12-10 10:32:29 +0000348
Olivier Deprez157378f2022-04-04 15:47:50 +0200349 nested_vmx_vmexit(vcpu, vm_exit_reason, 0, exit_qualification);
David Brazdil0f672f62019-12-10 10:32:29 +0000350 vmcs12->guest_physical_address = fault->address;
351}
352
353static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
354{
355 WARN_ON(mmu_is_nested(vcpu));
356
357 vcpu->arch.mmu = &vcpu->arch.guest_mmu;
358 kvm_init_shadow_ept_mmu(vcpu,
359 to_vmx(vcpu)->nested.msrs.ept_caps &
360 VMX_EPT_EXECUTE_ONLY_BIT,
361 nested_ept_ad_enabled(vcpu),
Olivier Deprez157378f2022-04-04 15:47:50 +0200362 nested_ept_get_eptp(vcpu));
363 vcpu->arch.mmu->get_guest_pgd = nested_ept_get_eptp;
David Brazdil0f672f62019-12-10 10:32:29 +0000364 vcpu->arch.mmu->inject_page_fault = nested_ept_inject_page_fault;
365 vcpu->arch.mmu->get_pdptr = kvm_pdptr_read;
366
367 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
368}
369
370static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
371{
372 vcpu->arch.mmu = &vcpu->arch.root_mmu;
373 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
374}
375
376static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
377 u16 error_code)
378{
379 bool inequality, bit;
380
381 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
382 inequality =
383 (error_code & vmcs12->page_fault_error_code_mask) !=
384 vmcs12->page_fault_error_code_match;
385 return inequality ^ bit;
386}
387
388
389/*
390 * KVM wants to inject page-faults which it got to the guest. This function
391 * checks whether in a nested guest, we need to inject them to L1 or L2.
392 */
393static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
394{
395 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
396 unsigned int nr = vcpu->arch.exception.nr;
397 bool has_payload = vcpu->arch.exception.has_payload;
398 unsigned long payload = vcpu->arch.exception.payload;
399
400 if (nr == PF_VECTOR) {
401 if (vcpu->arch.exception.nested_apf) {
402 *exit_qual = vcpu->arch.apf.nested_apf_token;
403 return 1;
404 }
405 if (nested_vmx_is_page_fault_vmexit(vmcs12,
406 vcpu->arch.exception.error_code)) {
407 *exit_qual = has_payload ? payload : vcpu->arch.cr2;
408 return 1;
409 }
410 } else if (vmcs12->exception_bitmap & (1u << nr)) {
411 if (nr == DB_VECTOR) {
412 if (!has_payload) {
413 payload = vcpu->arch.dr6;
414 payload &= ~(DR6_FIXED_1 | DR6_BT);
415 payload ^= DR6_RTM;
416 }
417 *exit_qual = payload;
418 } else
419 *exit_qual = 0;
420 return 1;
421 }
422
423 return 0;
424}
425
426
427static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
428 struct x86_exception *fault)
429{
430 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
431
432 WARN_ON(!is_guest_mode(vcpu));
433
434 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
435 !to_vmx(vcpu)->nested.nested_run_pending) {
436 vmcs12->vm_exit_intr_error_code = fault->error_code;
437 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
438 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
439 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
440 fault->address);
441 } else {
442 kvm_inject_page_fault(vcpu, fault);
443 }
444}
445
David Brazdil0f672f62019-12-10 10:32:29 +0000446static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
447 struct vmcs12 *vmcs12)
448{
449 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
450 return 0;
451
452 if (CC(!page_address_valid(vcpu, vmcs12->io_bitmap_a)) ||
453 CC(!page_address_valid(vcpu, vmcs12->io_bitmap_b)))
454 return -EINVAL;
455
456 return 0;
457}
458
459static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
460 struct vmcs12 *vmcs12)
461{
462 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
463 return 0;
464
465 if (CC(!page_address_valid(vcpu, vmcs12->msr_bitmap)))
466 return -EINVAL;
467
468 return 0;
469}
470
471static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
472 struct vmcs12 *vmcs12)
473{
474 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
475 return 0;
476
477 if (CC(!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr)))
478 return -EINVAL;
479
480 return 0;
481}
482
483/*
484 * Check if MSR is intercepted for L01 MSR bitmap.
485 */
486static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
487{
488 unsigned long *msr_bitmap;
489 int f = sizeof(unsigned long);
490
491 if (!cpu_has_vmx_msr_bitmap())
492 return true;
493
494 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
495
496 if (msr <= 0x1fff) {
497 return !!test_bit(msr, msr_bitmap + 0x800 / f);
498 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
499 msr &= 0x1fff;
500 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
501 }
502
503 return true;
504}
505
506/*
507 * If a msr is allowed by L0, we should check whether it is allowed by L1.
508 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
509 */
510static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
511 unsigned long *msr_bitmap_nested,
512 u32 msr, int type)
513{
514 int f = sizeof(unsigned long);
515
516 /*
517 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
518 * have the write-low and read-high bitmap offsets the wrong way round.
519 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
520 */
521 if (msr <= 0x1fff) {
522 if (type & MSR_TYPE_R &&
523 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
524 /* read-low */
525 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
526
527 if (type & MSR_TYPE_W &&
528 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
529 /* write-low */
530 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
531
532 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
533 msr &= 0x1fff;
534 if (type & MSR_TYPE_R &&
535 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
536 /* read-high */
537 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
538
539 if (type & MSR_TYPE_W &&
540 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
541 /* write-high */
542 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
543
544 }
545}
546
Olivier Deprez157378f2022-04-04 15:47:50 +0200547static inline void enable_x2apic_msr_intercepts(unsigned long *msr_bitmap)
548{
David Brazdil0f672f62019-12-10 10:32:29 +0000549 int msr;
550
551 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
552 unsigned word = msr / BITS_PER_LONG;
553
554 msr_bitmap[word] = ~0;
555 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
556 }
557}
558
559/*
560 * Merge L0's and L1's MSR bitmap, return false to indicate that
561 * we do not use the hardware.
562 */
563static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
564 struct vmcs12 *vmcs12)
565{
566 int msr;
567 unsigned long *msr_bitmap_l1;
568 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
569 struct kvm_host_map *map = &to_vmx(vcpu)->nested.msr_bitmap_map;
570
571 /* Nothing to do if the MSR bitmap is not in use. */
572 if (!cpu_has_vmx_msr_bitmap() ||
573 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
574 return false;
575
576 if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->msr_bitmap), map))
577 return false;
578
579 msr_bitmap_l1 = (unsigned long *)map->hva;
580
581 /*
582 * To keep the control flow simple, pay eight 8-byte writes (sixteen
583 * 4-byte writes on 32-bit systems) up front to enable intercepts for
584 * the x2APIC MSR range and selectively disable them below.
585 */
586 enable_x2apic_msr_intercepts(msr_bitmap_l0);
587
588 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
589 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
590 /*
591 * L0 need not intercept reads for MSRs between 0x800
592 * and 0x8ff, it just lets the processor take the value
593 * from the virtual-APIC page; take those 256 bits
594 * directly from the L1 bitmap.
595 */
596 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
597 unsigned word = msr / BITS_PER_LONG;
598
599 msr_bitmap_l0[word] = msr_bitmap_l1[word];
600 }
601 }
602
603 nested_vmx_disable_intercept_for_msr(
604 msr_bitmap_l1, msr_bitmap_l0,
605 X2APIC_MSR(APIC_TASKPRI),
606 MSR_TYPE_R | MSR_TYPE_W);
607
608 if (nested_cpu_has_vid(vmcs12)) {
609 nested_vmx_disable_intercept_for_msr(
610 msr_bitmap_l1, msr_bitmap_l0,
611 X2APIC_MSR(APIC_EOI),
612 MSR_TYPE_W);
613 nested_vmx_disable_intercept_for_msr(
614 msr_bitmap_l1, msr_bitmap_l0,
615 X2APIC_MSR(APIC_SELF_IPI),
616 MSR_TYPE_W);
617 }
618 }
619
620 /* KVM unconditionally exposes the FS/GS base MSRs to L1. */
Olivier Deprez157378f2022-04-04 15:47:50 +0200621#ifdef CONFIG_X86_64
David Brazdil0f672f62019-12-10 10:32:29 +0000622 nested_vmx_disable_intercept_for_msr(msr_bitmap_l1, msr_bitmap_l0,
623 MSR_FS_BASE, MSR_TYPE_RW);
624
625 nested_vmx_disable_intercept_for_msr(msr_bitmap_l1, msr_bitmap_l0,
626 MSR_GS_BASE, MSR_TYPE_RW);
627
628 nested_vmx_disable_intercept_for_msr(msr_bitmap_l1, msr_bitmap_l0,
629 MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
Olivier Deprez157378f2022-04-04 15:47:50 +0200630#endif
David Brazdil0f672f62019-12-10 10:32:29 +0000631
632 /*
633 * Checking the L0->L1 bitmap is trying to verify two things:
634 *
635 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
636 * ensures that we do not accidentally generate an L02 MSR bitmap
637 * from the L12 MSR bitmap that is too permissive.
638 * 2. That L1 or L2s have actually used the MSR. This avoids
639 * unnecessarily merging of the bitmap if the MSR is unused. This
640 * works properly because we only update the L01 MSR bitmap lazily.
641 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
642 * updated to reflect this when L1 (or its L2s) actually write to
643 * the MSR.
644 */
645 if (!msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL))
646 nested_vmx_disable_intercept_for_msr(
647 msr_bitmap_l1, msr_bitmap_l0,
648 MSR_IA32_SPEC_CTRL,
649 MSR_TYPE_R | MSR_TYPE_W);
650
651 if (!msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD))
652 nested_vmx_disable_intercept_for_msr(
653 msr_bitmap_l1, msr_bitmap_l0,
654 MSR_IA32_PRED_CMD,
655 MSR_TYPE_W);
656
657 kvm_vcpu_unmap(vcpu, &to_vmx(vcpu)->nested.msr_bitmap_map, false);
658
659 return true;
660}
661
662static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
663 struct vmcs12 *vmcs12)
664{
665 struct kvm_host_map map;
666 struct vmcs12 *shadow;
667
668 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
669 vmcs12->vmcs_link_pointer == -1ull)
670 return;
671
672 shadow = get_shadow_vmcs12(vcpu);
673
674 if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->vmcs_link_pointer), &map))
675 return;
676
677 memcpy(shadow, map.hva, VMCS12_SIZE);
678 kvm_vcpu_unmap(vcpu, &map, false);
679}
680
681static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
682 struct vmcs12 *vmcs12)
683{
684 struct vcpu_vmx *vmx = to_vmx(vcpu);
685
686 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
687 vmcs12->vmcs_link_pointer == -1ull)
688 return;
689
690 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
691 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
692}
693
694/*
695 * In nested virtualization, check if L1 has set
696 * VM_EXIT_ACK_INTR_ON_EXIT
697 */
698static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
699{
700 return get_vmcs12(vcpu)->vm_exit_controls &
701 VM_EXIT_ACK_INTR_ON_EXIT;
702}
703
David Brazdil0f672f62019-12-10 10:32:29 +0000704static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
705 struct vmcs12 *vmcs12)
706{
707 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
708 CC(!page_address_valid(vcpu, vmcs12->apic_access_addr)))
709 return -EINVAL;
710 else
711 return 0;
712}
713
714static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
715 struct vmcs12 *vmcs12)
716{
717 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
718 !nested_cpu_has_apic_reg_virt(vmcs12) &&
719 !nested_cpu_has_vid(vmcs12) &&
720 !nested_cpu_has_posted_intr(vmcs12))
721 return 0;
722
723 /*
724 * If virtualize x2apic mode is enabled,
725 * virtualize apic access must be disabled.
726 */
727 if (CC(nested_cpu_has_virt_x2apic_mode(vmcs12) &&
728 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)))
729 return -EINVAL;
730
731 /*
732 * If virtual interrupt delivery is enabled,
733 * we must exit on external interrupts.
734 */
735 if (CC(nested_cpu_has_vid(vmcs12) && !nested_exit_on_intr(vcpu)))
736 return -EINVAL;
737
738 /*
739 * bits 15:8 should be zero in posted_intr_nv,
740 * the descriptor address has been already checked
741 * in nested_get_vmcs12_pages.
742 *
743 * bits 5:0 of posted_intr_desc_addr should be zero.
744 */
745 if (nested_cpu_has_posted_intr(vmcs12) &&
746 (CC(!nested_cpu_has_vid(vmcs12)) ||
747 CC(!nested_exit_intr_ack_set(vcpu)) ||
748 CC((vmcs12->posted_intr_nv & 0xff00)) ||
749 CC((vmcs12->posted_intr_desc_addr & 0x3f)) ||
750 CC((vmcs12->posted_intr_desc_addr >> cpuid_maxphyaddr(vcpu)))))
751 return -EINVAL;
752
753 /* tpr shadow is needed by all apicv features. */
754 if (CC(!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)))
755 return -EINVAL;
756
757 return 0;
758}
759
760static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
761 u32 count, u64 addr)
762{
763 int maxphyaddr;
764
765 if (count == 0)
766 return 0;
767 maxphyaddr = cpuid_maxphyaddr(vcpu);
768 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
769 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr)
770 return -EINVAL;
771
772 return 0;
773}
774
775static int nested_vmx_check_exit_msr_switch_controls(struct kvm_vcpu *vcpu,
776 struct vmcs12 *vmcs12)
777{
778 if (CC(nested_vmx_check_msr_switch(vcpu,
779 vmcs12->vm_exit_msr_load_count,
780 vmcs12->vm_exit_msr_load_addr)) ||
781 CC(nested_vmx_check_msr_switch(vcpu,
782 vmcs12->vm_exit_msr_store_count,
783 vmcs12->vm_exit_msr_store_addr)))
784 return -EINVAL;
785
786 return 0;
787}
788
789static int nested_vmx_check_entry_msr_switch_controls(struct kvm_vcpu *vcpu,
790 struct vmcs12 *vmcs12)
791{
792 if (CC(nested_vmx_check_msr_switch(vcpu,
793 vmcs12->vm_entry_msr_load_count,
794 vmcs12->vm_entry_msr_load_addr)))
795 return -EINVAL;
796
797 return 0;
798}
799
800static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
801 struct vmcs12 *vmcs12)
802{
803 if (!nested_cpu_has_pml(vmcs12))
804 return 0;
805
806 if (CC(!nested_cpu_has_ept(vmcs12)) ||
807 CC(!page_address_valid(vcpu, vmcs12->pml_address)))
808 return -EINVAL;
809
810 return 0;
811}
812
813static int nested_vmx_check_unrestricted_guest_controls(struct kvm_vcpu *vcpu,
814 struct vmcs12 *vmcs12)
815{
816 if (CC(nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST) &&
817 !nested_cpu_has_ept(vmcs12)))
818 return -EINVAL;
819 return 0;
820}
821
822static int nested_vmx_check_mode_based_ept_exec_controls(struct kvm_vcpu *vcpu,
823 struct vmcs12 *vmcs12)
824{
825 if (CC(nested_cpu_has2(vmcs12, SECONDARY_EXEC_MODE_BASED_EPT_EXEC) &&
826 !nested_cpu_has_ept(vmcs12)))
827 return -EINVAL;
828 return 0;
829}
830
831static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
832 struct vmcs12 *vmcs12)
833{
834 if (!nested_cpu_has_shadow_vmcs(vmcs12))
835 return 0;
836
837 if (CC(!page_address_valid(vcpu, vmcs12->vmread_bitmap)) ||
838 CC(!page_address_valid(vcpu, vmcs12->vmwrite_bitmap)))
839 return -EINVAL;
840
841 return 0;
842}
843
844static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
845 struct vmx_msr_entry *e)
846{
847 /* x2APIC MSR accesses are not allowed */
848 if (CC(vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8))
849 return -EINVAL;
850 if (CC(e->index == MSR_IA32_UCODE_WRITE) || /* SDM Table 35-2 */
851 CC(e->index == MSR_IA32_UCODE_REV))
852 return -EINVAL;
853 if (CC(e->reserved != 0))
854 return -EINVAL;
855 return 0;
856}
857
858static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
859 struct vmx_msr_entry *e)
860{
861 if (CC(e->index == MSR_FS_BASE) ||
862 CC(e->index == MSR_GS_BASE) ||
863 CC(e->index == MSR_IA32_SMM_MONITOR_CTL) || /* SMM is not supported */
864 nested_vmx_msr_check_common(vcpu, e))
865 return -EINVAL;
866 return 0;
867}
868
869static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
870 struct vmx_msr_entry *e)
871{
872 if (CC(e->index == MSR_IA32_SMBASE) || /* SMM is not supported */
873 nested_vmx_msr_check_common(vcpu, e))
874 return -EINVAL;
875 return 0;
876}
877
878static u32 nested_vmx_max_atomic_switch_msrs(struct kvm_vcpu *vcpu)
879{
880 struct vcpu_vmx *vmx = to_vmx(vcpu);
881 u64 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
882 vmx->nested.msrs.misc_high);
883
884 return (vmx_misc_max_msr(vmx_misc) + 1) * VMX_MISC_MSR_LIST_MULTIPLIER;
885}
886
887/*
888 * Load guest's/host's msr at nested entry/exit.
889 * return 0 for success, entry index for failure.
890 *
891 * One of the failure modes for MSR load/store is when a list exceeds the
892 * virtual hardware's capacity. To maintain compatibility with hardware inasmuch
893 * as possible, process all valid entries before failing rather than precheck
894 * for a capacity violation.
895 */
896static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
897{
898 u32 i;
899 struct vmx_msr_entry e;
900 u32 max_msr_list_size = nested_vmx_max_atomic_switch_msrs(vcpu);
901
902 for (i = 0; i < count; i++) {
903 if (unlikely(i >= max_msr_list_size))
904 goto fail;
905
906 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
907 &e, sizeof(e))) {
908 pr_debug_ratelimited(
909 "%s cannot read MSR entry (%u, 0x%08llx)\n",
910 __func__, i, gpa + i * sizeof(e));
911 goto fail;
912 }
913 if (nested_vmx_load_msr_check(vcpu, &e)) {
914 pr_debug_ratelimited(
915 "%s check failed (%u, 0x%x, 0x%x)\n",
916 __func__, i, e.index, e.reserved);
917 goto fail;
918 }
919 if (kvm_set_msr(vcpu, e.index, e.value)) {
920 pr_debug_ratelimited(
921 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
922 __func__, i, e.index, e.value);
923 goto fail;
924 }
925 }
926 return 0;
927fail:
Olivier Deprez157378f2022-04-04 15:47:50 +0200928 /* Note, max_msr_list_size is at most 4096, i.e. this can't wrap. */
David Brazdil0f672f62019-12-10 10:32:29 +0000929 return i + 1;
930}
931
Olivier Deprez157378f2022-04-04 15:47:50 +0200932static bool nested_vmx_get_vmexit_msr_value(struct kvm_vcpu *vcpu,
933 u32 msr_index,
934 u64 *data)
935{
936 struct vcpu_vmx *vmx = to_vmx(vcpu);
937
938 /*
939 * If the L0 hypervisor stored a more accurate value for the TSC that
940 * does not include the time taken for emulation of the L2->L1
941 * VM-exit in L0, use the more accurate value.
942 */
943 if (msr_index == MSR_IA32_TSC) {
944 int i = vmx_find_loadstore_msr_slot(&vmx->msr_autostore.guest,
945 MSR_IA32_TSC);
946
947 if (i >= 0) {
948 u64 val = vmx->msr_autostore.guest.val[i].value;
949
950 *data = kvm_read_l1_tsc(vcpu, val);
951 return true;
952 }
953 }
954
955 if (kvm_get_msr(vcpu, msr_index, data)) {
956 pr_debug_ratelimited("%s cannot read MSR (0x%x)\n", __func__,
957 msr_index);
958 return false;
959 }
960 return true;
961}
962
963static bool read_and_check_msr_entry(struct kvm_vcpu *vcpu, u64 gpa, int i,
964 struct vmx_msr_entry *e)
965{
966 if (kvm_vcpu_read_guest(vcpu,
967 gpa + i * sizeof(*e),
968 e, 2 * sizeof(u32))) {
969 pr_debug_ratelimited(
970 "%s cannot read MSR entry (%u, 0x%08llx)\n",
971 __func__, i, gpa + i * sizeof(*e));
972 return false;
973 }
974 if (nested_vmx_store_msr_check(vcpu, e)) {
975 pr_debug_ratelimited(
976 "%s check failed (%u, 0x%x, 0x%x)\n",
977 __func__, i, e->index, e->reserved);
978 return false;
979 }
980 return true;
981}
982
David Brazdil0f672f62019-12-10 10:32:29 +0000983static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
984{
985 u64 data;
986 u32 i;
987 struct vmx_msr_entry e;
988 u32 max_msr_list_size = nested_vmx_max_atomic_switch_msrs(vcpu);
989
990 for (i = 0; i < count; i++) {
991 if (unlikely(i >= max_msr_list_size))
992 return -EINVAL;
993
Olivier Deprez157378f2022-04-04 15:47:50 +0200994 if (!read_and_check_msr_entry(vcpu, gpa, i, &e))
David Brazdil0f672f62019-12-10 10:32:29 +0000995 return -EINVAL;
Olivier Deprez157378f2022-04-04 15:47:50 +0200996
997 if (!nested_vmx_get_vmexit_msr_value(vcpu, e.index, &data))
David Brazdil0f672f62019-12-10 10:32:29 +0000998 return -EINVAL;
Olivier Deprez157378f2022-04-04 15:47:50 +0200999
David Brazdil0f672f62019-12-10 10:32:29 +00001000 if (kvm_vcpu_write_guest(vcpu,
1001 gpa + i * sizeof(e) +
1002 offsetof(struct vmx_msr_entry, value),
1003 &data, sizeof(data))) {
1004 pr_debug_ratelimited(
1005 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
1006 __func__, i, e.index, data);
1007 return -EINVAL;
1008 }
1009 }
1010 return 0;
1011}
1012
Olivier Deprez157378f2022-04-04 15:47:50 +02001013static bool nested_msr_store_list_has_msr(struct kvm_vcpu *vcpu, u32 msr_index)
1014{
1015 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1016 u32 count = vmcs12->vm_exit_msr_store_count;
1017 u64 gpa = vmcs12->vm_exit_msr_store_addr;
1018 struct vmx_msr_entry e;
1019 u32 i;
1020
1021 for (i = 0; i < count; i++) {
1022 if (!read_and_check_msr_entry(vcpu, gpa, i, &e))
1023 return false;
1024
1025 if (e.index == msr_index)
1026 return true;
1027 }
1028 return false;
1029}
1030
1031static void prepare_vmx_msr_autostore_list(struct kvm_vcpu *vcpu,
1032 u32 msr_index)
1033{
1034 struct vcpu_vmx *vmx = to_vmx(vcpu);
1035 struct vmx_msrs *autostore = &vmx->msr_autostore.guest;
1036 bool in_vmcs12_store_list;
1037 int msr_autostore_slot;
1038 bool in_autostore_list;
1039 int last;
1040
1041 msr_autostore_slot = vmx_find_loadstore_msr_slot(autostore, msr_index);
1042 in_autostore_list = msr_autostore_slot >= 0;
1043 in_vmcs12_store_list = nested_msr_store_list_has_msr(vcpu, msr_index);
1044
1045 if (in_vmcs12_store_list && !in_autostore_list) {
1046 if (autostore->nr == MAX_NR_LOADSTORE_MSRS) {
1047 /*
1048 * Emulated VMEntry does not fail here. Instead a less
1049 * accurate value will be returned by
1050 * nested_vmx_get_vmexit_msr_value() using kvm_get_msr()
1051 * instead of reading the value from the vmcs02 VMExit
1052 * MSR-store area.
1053 */
1054 pr_warn_ratelimited(
1055 "Not enough msr entries in msr_autostore. Can't add msr %x\n",
1056 msr_index);
1057 return;
1058 }
1059 last = autostore->nr++;
1060 autostore->val[last].index = msr_index;
1061 } else if (!in_vmcs12_store_list && in_autostore_list) {
1062 last = --autostore->nr;
1063 autostore->val[msr_autostore_slot] = autostore->val[last];
1064 }
1065}
1066
David Brazdil0f672f62019-12-10 10:32:29 +00001067static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
1068{
1069 unsigned long invalid_mask;
1070
1071 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
1072 return (val & invalid_mask) == 0;
1073}
1074
1075/*
Olivier Deprez157378f2022-04-04 15:47:50 +02001076 * Returns true if the MMU needs to be sync'd on nested VM-Enter/VM-Exit.
1077 * tl;dr: the MMU needs a sync if L0 is using shadow paging and L1 didn't
1078 * enable VPID for L2 (implying it expects a TLB flush on VMX transitions).
1079 * Here's why.
1080 *
1081 * If EPT is enabled by L0 a sync is never needed:
1082 * - if it is disabled by L1, then L0 is not shadowing L1 or L2 PTEs, there
1083 * cannot be unsync'd SPTEs for either L1 or L2.
1084 *
1085 * - if it is also enabled by L1, then L0 doesn't need to sync on VM-Enter
1086 * VM-Enter as VM-Enter isn't required to invalidate guest-physical mappings
1087 * (irrespective of VPID), i.e. L1 can't rely on the (virtual) CPU to flush
1088 * stale guest-physical mappings for L2 from the TLB. And as above, L0 isn't
1089 * shadowing L1 PTEs so there are no unsync'd SPTEs to sync on VM-Exit.
1090 *
1091 * If EPT is disabled by L0:
1092 * - if VPID is enabled by L1 (for L2), the situation is similar to when L1
1093 * enables EPT: L0 doesn't need to sync as VM-Enter and VM-Exit aren't
1094 * required to invalidate linear mappings (EPT is disabled so there are
1095 * no combined or guest-physical mappings), i.e. L1 can't rely on the
1096 * (virtual) CPU to flush stale linear mappings for either L2 or itself (L1).
1097 *
1098 * - however if VPID is disabled by L1, then a sync is needed as L1 expects all
1099 * linear mappings (EPT is disabled so there are no combined or guest-physical
1100 * mappings) to be invalidated on both VM-Enter and VM-Exit.
1101 *
1102 * Note, this logic is subtly different than nested_has_guest_tlb_tag(), which
1103 * additionally checks that L2 has been assigned a VPID (when EPT is disabled).
1104 * Whether or not L2 has been assigned a VPID by L0 is irrelevant with respect
1105 * to L1's expectations, e.g. L0 needs to invalidate hardware TLB entries if L2
1106 * doesn't have a unique VPID to prevent reusing L1's entries (assuming L1 has
1107 * been assigned a VPID), but L0 doesn't need to do a MMU sync because L1
1108 * doesn't expect stale (virtual) TLB entries to be flushed, i.e. L1 doesn't
1109 * know that L0 will flush the TLB and so L1 will do INVVPID as needed to flush
1110 * stale TLB entries, at which point L0 will sync L2's MMU.
1111 */
1112static bool nested_vmx_transition_mmu_sync(struct kvm_vcpu *vcpu)
1113{
1114 return !enable_ept && !nested_cpu_has_vpid(get_vmcs12(vcpu));
1115}
1116
1117/*
1118 * Load guest's/host's cr3 at nested entry/exit. @nested_ept is true if we are
1119 * emulating VM-Entry into a guest with EPT enabled. On failure, the expected
1120 * Exit Qualification (for a VM-Entry consistency check VM-Exit) is assigned to
1121 * @entry_failure_code.
David Brazdil0f672f62019-12-10 10:32:29 +00001122 */
1123static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Olivier Deprez157378f2022-04-04 15:47:50 +02001124 enum vm_entry_failure_code *entry_failure_code)
David Brazdil0f672f62019-12-10 10:32:29 +00001125{
Olivier Deprez157378f2022-04-04 15:47:50 +02001126 if (CC(!nested_cr3_valid(vcpu, cr3))) {
1127 *entry_failure_code = ENTRY_FAIL_DEFAULT;
1128 return -EINVAL;
1129 }
David Brazdil0f672f62019-12-10 10:32:29 +00001130
Olivier Deprez157378f2022-04-04 15:47:50 +02001131 /*
1132 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
1133 * must not be dereferenced.
1134 */
1135 if (!nested_ept && is_pae_paging(vcpu) &&
1136 (cr3 != kvm_read_cr3(vcpu) || pdptrs_changed(vcpu))) {
1137 if (CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))) {
1138 *entry_failure_code = ENTRY_FAIL_PDPTE;
1139 return -EINVAL;
David Brazdil0f672f62019-12-10 10:32:29 +00001140 }
1141 }
1142
Olivier Deprez157378f2022-04-04 15:47:50 +02001143 /*
1144 * Unconditionally skip the TLB flush on fast CR3 switch, all TLB
1145 * flushes are handled by nested_vmx_transition_tlb_flush().
1146 */
1147 if (!nested_ept) {
1148 kvm_mmu_new_pgd(vcpu, cr3, true, true);
1149
1150 /*
1151 * A TLB flush on VM-Enter/VM-Exit flushes all linear mappings
1152 * across all PCIDs, i.e. all PGDs need to be synchronized.
1153 * See nested_vmx_transition_mmu_sync() for more details.
1154 */
1155 if (nested_vmx_transition_mmu_sync(vcpu))
1156 kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu);
1157 }
David Brazdil0f672f62019-12-10 10:32:29 +00001158
1159 vcpu->arch.cr3 = cr3;
Olivier Deprez157378f2022-04-04 15:47:50 +02001160 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
David Brazdil0f672f62019-12-10 10:32:29 +00001161
1162 kvm_init_mmu(vcpu, false);
1163
1164 return 0;
1165}
1166
1167/*
1168 * Returns if KVM is able to config CPU to tag TLB entries
1169 * populated by L2 differently than TLB entries populated
1170 * by L1.
1171 *
Olivier Deprez157378f2022-04-04 15:47:50 +02001172 * If L0 uses EPT, L1 and L2 run with different EPTP because
1173 * guest_mode is part of kvm_mmu_page_role. Thus, TLB entries
1174 * are tagged with different EPTP.
David Brazdil0f672f62019-12-10 10:32:29 +00001175 *
1176 * If L1 uses VPID and we allocated a vpid02, TLB entries are tagged
1177 * with different VPID (L1 entries are tagged with vmx->vpid
1178 * while L2 entries are tagged with vmx->nested.vpid02).
1179 */
1180static bool nested_has_guest_tlb_tag(struct kvm_vcpu *vcpu)
1181{
1182 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1183
Olivier Deprez157378f2022-04-04 15:47:50 +02001184 return enable_ept ||
David Brazdil0f672f62019-12-10 10:32:29 +00001185 (nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02);
1186}
1187
Olivier Deprez157378f2022-04-04 15:47:50 +02001188static void nested_vmx_transition_tlb_flush(struct kvm_vcpu *vcpu,
1189 struct vmcs12 *vmcs12,
1190 bool is_vmenter)
David Brazdil0f672f62019-12-10 10:32:29 +00001191{
1192 struct vcpu_vmx *vmx = to_vmx(vcpu);
1193
Olivier Deprez157378f2022-04-04 15:47:50 +02001194 /*
1195 * If VPID is disabled, linear and combined mappings are flushed on
1196 * VM-Enter/VM-Exit, and guest-physical mappings are valid only for
1197 * their associated EPTP.
1198 */
1199 if (!enable_vpid)
1200 return;
1201
1202 /*
1203 * If vmcs12 doesn't use VPID, L1 expects linear and combined mappings
1204 * for *all* contexts to be flushed on VM-Enter/VM-Exit.
1205 *
1206 * If VPID is enabled and used by vmc12, but L2 does not have a unique
1207 * TLB tag (ASID), i.e. EPT is disabled and KVM was unable to allocate
1208 * a VPID for L2, flush the current context as the effective ASID is
1209 * common to both L1 and L2.
1210 *
1211 * Defer the flush so that it runs after vmcs02.EPTP has been set by
1212 * KVM_REQ_LOAD_MMU_PGD (if nested EPT is enabled) and to avoid
1213 * redundant flushes further down the nested pipeline.
1214 *
1215 * If a TLB flush isn't required due to any of the above, and vpid12 is
1216 * changing then the new "virtual" VPID (vpid12) will reuse the same
1217 * "real" VPID (vpid02), and so needs to be sync'd. There is no direct
1218 * mapping between vpid02 and vpid12, vpid02 is per-vCPU and reused for
1219 * all nested vCPUs.
1220 */
1221 if (!nested_cpu_has_vpid(vmcs12)) {
1222 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
1223 } else if (!nested_has_guest_tlb_tag(vcpu)) {
1224 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
1225 } else if (is_vmenter &&
1226 vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
1227 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
1228 vpid_sync_context(nested_get_vpid02(vcpu));
1229 }
David Brazdil0f672f62019-12-10 10:32:29 +00001230}
1231
1232static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
1233{
1234 superset &= mask;
1235 subset &= mask;
1236
1237 return (superset | subset) == superset;
1238}
1239
1240static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
1241{
1242 const u64 feature_and_reserved =
1243 /* feature (except bit 48; see below) */
1244 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
1245 /* reserved */
1246 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
1247 u64 vmx_basic = vmx->nested.msrs.basic;
1248
1249 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
1250 return -EINVAL;
1251
1252 /*
1253 * KVM does not emulate a version of VMX that constrains physical
1254 * addresses of VMX structures (e.g. VMCS) to 32-bits.
1255 */
1256 if (data & BIT_ULL(48))
1257 return -EINVAL;
1258
1259 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
1260 vmx_basic_vmcs_revision_id(data))
1261 return -EINVAL;
1262
1263 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
1264 return -EINVAL;
1265
1266 vmx->nested.msrs.basic = data;
1267 return 0;
1268}
1269
1270static int
1271vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
1272{
1273 u64 supported;
1274 u32 *lowp, *highp;
1275
1276 switch (msr_index) {
1277 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
1278 lowp = &vmx->nested.msrs.pinbased_ctls_low;
1279 highp = &vmx->nested.msrs.pinbased_ctls_high;
1280 break;
1281 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
1282 lowp = &vmx->nested.msrs.procbased_ctls_low;
1283 highp = &vmx->nested.msrs.procbased_ctls_high;
1284 break;
1285 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
1286 lowp = &vmx->nested.msrs.exit_ctls_low;
1287 highp = &vmx->nested.msrs.exit_ctls_high;
1288 break;
1289 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
1290 lowp = &vmx->nested.msrs.entry_ctls_low;
1291 highp = &vmx->nested.msrs.entry_ctls_high;
1292 break;
1293 case MSR_IA32_VMX_PROCBASED_CTLS2:
1294 lowp = &vmx->nested.msrs.secondary_ctls_low;
1295 highp = &vmx->nested.msrs.secondary_ctls_high;
1296 break;
1297 default:
1298 BUG();
1299 }
1300
1301 supported = vmx_control_msr(*lowp, *highp);
1302
1303 /* Check must-be-1 bits are still 1. */
1304 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
1305 return -EINVAL;
1306
1307 /* Check must-be-0 bits are still 0. */
1308 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
1309 return -EINVAL;
1310
1311 *lowp = data;
1312 *highp = data >> 32;
1313 return 0;
1314}
1315
1316static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
1317{
1318 const u64 feature_and_reserved_bits =
1319 /* feature */
1320 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
1321 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
1322 /* reserved */
1323 GENMASK_ULL(13, 9) | BIT_ULL(31);
1324 u64 vmx_misc;
1325
1326 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
1327 vmx->nested.msrs.misc_high);
1328
1329 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
1330 return -EINVAL;
1331
1332 if ((vmx->nested.msrs.pinbased_ctls_high &
1333 PIN_BASED_VMX_PREEMPTION_TIMER) &&
1334 vmx_misc_preemption_timer_rate(data) !=
1335 vmx_misc_preemption_timer_rate(vmx_misc))
1336 return -EINVAL;
1337
1338 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
1339 return -EINVAL;
1340
1341 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
1342 return -EINVAL;
1343
1344 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
1345 return -EINVAL;
1346
1347 vmx->nested.msrs.misc_low = data;
1348 vmx->nested.msrs.misc_high = data >> 32;
1349
1350 return 0;
1351}
1352
1353static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
1354{
1355 u64 vmx_ept_vpid_cap;
1356
1357 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
1358 vmx->nested.msrs.vpid_caps);
1359
1360 /* Every bit is either reserved or a feature bit. */
1361 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
1362 return -EINVAL;
1363
1364 vmx->nested.msrs.ept_caps = data;
1365 vmx->nested.msrs.vpid_caps = data >> 32;
1366 return 0;
1367}
1368
1369static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
1370{
1371 u64 *msr;
1372
1373 switch (msr_index) {
1374 case MSR_IA32_VMX_CR0_FIXED0:
1375 msr = &vmx->nested.msrs.cr0_fixed0;
1376 break;
1377 case MSR_IA32_VMX_CR4_FIXED0:
1378 msr = &vmx->nested.msrs.cr4_fixed0;
1379 break;
1380 default:
1381 BUG();
1382 }
1383
1384 /*
1385 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
1386 * must be 1 in the restored value.
1387 */
1388 if (!is_bitwise_subset(data, *msr, -1ULL))
1389 return -EINVAL;
1390
1391 *msr = data;
1392 return 0;
1393}
1394
1395/*
1396 * Called when userspace is restoring VMX MSRs.
1397 *
1398 * Returns 0 on success, non-0 otherwise.
1399 */
1400int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1401{
1402 struct vcpu_vmx *vmx = to_vmx(vcpu);
1403
1404 /*
1405 * Don't allow changes to the VMX capability MSRs while the vCPU
1406 * is in VMX operation.
1407 */
1408 if (vmx->nested.vmxon)
1409 return -EBUSY;
1410
1411 switch (msr_index) {
1412 case MSR_IA32_VMX_BASIC:
1413 return vmx_restore_vmx_basic(vmx, data);
1414 case MSR_IA32_VMX_PINBASED_CTLS:
1415 case MSR_IA32_VMX_PROCBASED_CTLS:
1416 case MSR_IA32_VMX_EXIT_CTLS:
1417 case MSR_IA32_VMX_ENTRY_CTLS:
1418 /*
1419 * The "non-true" VMX capability MSRs are generated from the
1420 * "true" MSRs, so we do not support restoring them directly.
1421 *
1422 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
1423 * should restore the "true" MSRs with the must-be-1 bits
1424 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
1425 * DEFAULT SETTINGS".
1426 */
1427 return -EINVAL;
1428 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
1429 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
1430 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
1431 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
1432 case MSR_IA32_VMX_PROCBASED_CTLS2:
1433 return vmx_restore_control_msr(vmx, msr_index, data);
1434 case MSR_IA32_VMX_MISC:
1435 return vmx_restore_vmx_misc(vmx, data);
1436 case MSR_IA32_VMX_CR0_FIXED0:
1437 case MSR_IA32_VMX_CR4_FIXED0:
1438 return vmx_restore_fixed0_msr(vmx, msr_index, data);
1439 case MSR_IA32_VMX_CR0_FIXED1:
1440 case MSR_IA32_VMX_CR4_FIXED1:
1441 /*
1442 * These MSRs are generated based on the vCPU's CPUID, so we
1443 * do not support restoring them directly.
1444 */
1445 return -EINVAL;
1446 case MSR_IA32_VMX_EPT_VPID_CAP:
1447 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
1448 case MSR_IA32_VMX_VMCS_ENUM:
1449 vmx->nested.msrs.vmcs_enum = data;
1450 return 0;
1451 case MSR_IA32_VMX_VMFUNC:
1452 if (data & ~vmx->nested.msrs.vmfunc_controls)
1453 return -EINVAL;
1454 vmx->nested.msrs.vmfunc_controls = data;
1455 return 0;
1456 default:
1457 /*
1458 * The rest of the VMX capability MSRs do not support restore.
1459 */
1460 return -EINVAL;
1461 }
1462}
1463
1464/* Returns 0 on success, non-0 otherwise. */
1465int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
1466{
1467 switch (msr_index) {
1468 case MSR_IA32_VMX_BASIC:
1469 *pdata = msrs->basic;
1470 break;
1471 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
1472 case MSR_IA32_VMX_PINBASED_CTLS:
1473 *pdata = vmx_control_msr(
1474 msrs->pinbased_ctls_low,
1475 msrs->pinbased_ctls_high);
1476 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
1477 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
1478 break;
1479 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
1480 case MSR_IA32_VMX_PROCBASED_CTLS:
1481 *pdata = vmx_control_msr(
1482 msrs->procbased_ctls_low,
1483 msrs->procbased_ctls_high);
1484 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
1485 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
1486 break;
1487 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
1488 case MSR_IA32_VMX_EXIT_CTLS:
1489 *pdata = vmx_control_msr(
1490 msrs->exit_ctls_low,
1491 msrs->exit_ctls_high);
1492 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
1493 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
1494 break;
1495 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
1496 case MSR_IA32_VMX_ENTRY_CTLS:
1497 *pdata = vmx_control_msr(
1498 msrs->entry_ctls_low,
1499 msrs->entry_ctls_high);
1500 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
1501 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
1502 break;
1503 case MSR_IA32_VMX_MISC:
1504 *pdata = vmx_control_msr(
1505 msrs->misc_low,
1506 msrs->misc_high);
1507 break;
1508 case MSR_IA32_VMX_CR0_FIXED0:
1509 *pdata = msrs->cr0_fixed0;
1510 break;
1511 case MSR_IA32_VMX_CR0_FIXED1:
1512 *pdata = msrs->cr0_fixed1;
1513 break;
1514 case MSR_IA32_VMX_CR4_FIXED0:
1515 *pdata = msrs->cr4_fixed0;
1516 break;
1517 case MSR_IA32_VMX_CR4_FIXED1:
1518 *pdata = msrs->cr4_fixed1;
1519 break;
1520 case MSR_IA32_VMX_VMCS_ENUM:
1521 *pdata = msrs->vmcs_enum;
1522 break;
1523 case MSR_IA32_VMX_PROCBASED_CTLS2:
1524 *pdata = vmx_control_msr(
1525 msrs->secondary_ctls_low,
1526 msrs->secondary_ctls_high);
1527 break;
1528 case MSR_IA32_VMX_EPT_VPID_CAP:
1529 *pdata = msrs->ept_caps |
1530 ((u64)msrs->vpid_caps << 32);
1531 break;
1532 case MSR_IA32_VMX_VMFUNC:
1533 *pdata = msrs->vmfunc_controls;
1534 break;
1535 default:
1536 return 1;
1537 }
1538
1539 return 0;
1540}
1541
1542/*
1543 * Copy the writable VMCS shadow fields back to the VMCS12, in case they have
1544 * been modified by the L1 guest. Note, "writable" in this context means
1545 * "writable by the guest", i.e. tagged SHADOW_FIELD_RW; the set of
1546 * fields tagged SHADOW_FIELD_RO may or may not align with the "read-only"
1547 * VM-exit information fields (which are actually writable if the vCPU is
1548 * configured to support "VMWRITE to any supported field in the VMCS").
1549 */
1550static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
1551{
1552 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
1553 struct vmcs12 *vmcs12 = get_vmcs12(&vmx->vcpu);
1554 struct shadow_vmcs_field field;
1555 unsigned long val;
1556 int i;
1557
1558 if (WARN_ON(!shadow_vmcs))
1559 return;
1560
1561 preempt_disable();
1562
1563 vmcs_load(shadow_vmcs);
1564
1565 for (i = 0; i < max_shadow_read_write_fields; i++) {
1566 field = shadow_read_write_fields[i];
1567 val = __vmcs_readl(field.encoding);
1568 vmcs12_write_any(vmcs12, field.encoding, field.offset, val);
1569 }
1570
1571 vmcs_clear(shadow_vmcs);
1572 vmcs_load(vmx->loaded_vmcs->vmcs);
1573
1574 preempt_enable();
1575}
1576
1577static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
1578{
1579 const struct shadow_vmcs_field *fields[] = {
1580 shadow_read_write_fields,
1581 shadow_read_only_fields
1582 };
1583 const int max_fields[] = {
1584 max_shadow_read_write_fields,
1585 max_shadow_read_only_fields
1586 };
1587 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
1588 struct vmcs12 *vmcs12 = get_vmcs12(&vmx->vcpu);
1589 struct shadow_vmcs_field field;
1590 unsigned long val;
1591 int i, q;
1592
1593 if (WARN_ON(!shadow_vmcs))
1594 return;
1595
1596 vmcs_load(shadow_vmcs);
1597
1598 for (q = 0; q < ARRAY_SIZE(fields); q++) {
1599 for (i = 0; i < max_fields[q]; i++) {
1600 field = fields[q][i];
1601 val = vmcs12_read_any(vmcs12, field.encoding,
1602 field.offset);
1603 __vmcs_writel(field.encoding, val);
1604 }
1605 }
1606
1607 vmcs_clear(shadow_vmcs);
1608 vmcs_load(vmx->loaded_vmcs->vmcs);
1609}
1610
1611static int copy_enlightened_to_vmcs12(struct vcpu_vmx *vmx)
1612{
1613 struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12;
1614 struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
1615
1616 /* HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE */
1617 vmcs12->tpr_threshold = evmcs->tpr_threshold;
1618 vmcs12->guest_rip = evmcs->guest_rip;
1619
1620 if (unlikely(!(evmcs->hv_clean_fields &
1621 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC))) {
1622 vmcs12->guest_rsp = evmcs->guest_rsp;
1623 vmcs12->guest_rflags = evmcs->guest_rflags;
1624 vmcs12->guest_interruptibility_info =
1625 evmcs->guest_interruptibility_info;
1626 }
1627
1628 if (unlikely(!(evmcs->hv_clean_fields &
1629 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC))) {
1630 vmcs12->cpu_based_vm_exec_control =
1631 evmcs->cpu_based_vm_exec_control;
1632 }
1633
1634 if (unlikely(!(evmcs->hv_clean_fields &
1635 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EXCPN))) {
1636 vmcs12->exception_bitmap = evmcs->exception_bitmap;
1637 }
1638
1639 if (unlikely(!(evmcs->hv_clean_fields &
1640 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_ENTRY))) {
1641 vmcs12->vm_entry_controls = evmcs->vm_entry_controls;
1642 }
1643
1644 if (unlikely(!(evmcs->hv_clean_fields &
1645 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT))) {
1646 vmcs12->vm_entry_intr_info_field =
1647 evmcs->vm_entry_intr_info_field;
1648 vmcs12->vm_entry_exception_error_code =
1649 evmcs->vm_entry_exception_error_code;
1650 vmcs12->vm_entry_instruction_len =
1651 evmcs->vm_entry_instruction_len;
1652 }
1653
1654 if (unlikely(!(evmcs->hv_clean_fields &
1655 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1))) {
1656 vmcs12->host_ia32_pat = evmcs->host_ia32_pat;
1657 vmcs12->host_ia32_efer = evmcs->host_ia32_efer;
1658 vmcs12->host_cr0 = evmcs->host_cr0;
1659 vmcs12->host_cr3 = evmcs->host_cr3;
1660 vmcs12->host_cr4 = evmcs->host_cr4;
1661 vmcs12->host_ia32_sysenter_esp = evmcs->host_ia32_sysenter_esp;
1662 vmcs12->host_ia32_sysenter_eip = evmcs->host_ia32_sysenter_eip;
1663 vmcs12->host_rip = evmcs->host_rip;
1664 vmcs12->host_ia32_sysenter_cs = evmcs->host_ia32_sysenter_cs;
1665 vmcs12->host_es_selector = evmcs->host_es_selector;
1666 vmcs12->host_cs_selector = evmcs->host_cs_selector;
1667 vmcs12->host_ss_selector = evmcs->host_ss_selector;
1668 vmcs12->host_ds_selector = evmcs->host_ds_selector;
1669 vmcs12->host_fs_selector = evmcs->host_fs_selector;
1670 vmcs12->host_gs_selector = evmcs->host_gs_selector;
1671 vmcs12->host_tr_selector = evmcs->host_tr_selector;
1672 }
1673
1674 if (unlikely(!(evmcs->hv_clean_fields &
1675 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1))) {
1676 vmcs12->pin_based_vm_exec_control =
1677 evmcs->pin_based_vm_exec_control;
1678 vmcs12->vm_exit_controls = evmcs->vm_exit_controls;
1679 vmcs12->secondary_vm_exec_control =
1680 evmcs->secondary_vm_exec_control;
1681 }
1682
1683 if (unlikely(!(evmcs->hv_clean_fields &
1684 HV_VMX_ENLIGHTENED_CLEAN_FIELD_IO_BITMAP))) {
1685 vmcs12->io_bitmap_a = evmcs->io_bitmap_a;
1686 vmcs12->io_bitmap_b = evmcs->io_bitmap_b;
1687 }
1688
1689 if (unlikely(!(evmcs->hv_clean_fields &
1690 HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP))) {
1691 vmcs12->msr_bitmap = evmcs->msr_bitmap;
1692 }
1693
1694 if (unlikely(!(evmcs->hv_clean_fields &
1695 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2))) {
1696 vmcs12->guest_es_base = evmcs->guest_es_base;
1697 vmcs12->guest_cs_base = evmcs->guest_cs_base;
1698 vmcs12->guest_ss_base = evmcs->guest_ss_base;
1699 vmcs12->guest_ds_base = evmcs->guest_ds_base;
1700 vmcs12->guest_fs_base = evmcs->guest_fs_base;
1701 vmcs12->guest_gs_base = evmcs->guest_gs_base;
1702 vmcs12->guest_ldtr_base = evmcs->guest_ldtr_base;
1703 vmcs12->guest_tr_base = evmcs->guest_tr_base;
1704 vmcs12->guest_gdtr_base = evmcs->guest_gdtr_base;
1705 vmcs12->guest_idtr_base = evmcs->guest_idtr_base;
1706 vmcs12->guest_es_limit = evmcs->guest_es_limit;
1707 vmcs12->guest_cs_limit = evmcs->guest_cs_limit;
1708 vmcs12->guest_ss_limit = evmcs->guest_ss_limit;
1709 vmcs12->guest_ds_limit = evmcs->guest_ds_limit;
1710 vmcs12->guest_fs_limit = evmcs->guest_fs_limit;
1711 vmcs12->guest_gs_limit = evmcs->guest_gs_limit;
1712 vmcs12->guest_ldtr_limit = evmcs->guest_ldtr_limit;
1713 vmcs12->guest_tr_limit = evmcs->guest_tr_limit;
1714 vmcs12->guest_gdtr_limit = evmcs->guest_gdtr_limit;
1715 vmcs12->guest_idtr_limit = evmcs->guest_idtr_limit;
1716 vmcs12->guest_es_ar_bytes = evmcs->guest_es_ar_bytes;
1717 vmcs12->guest_cs_ar_bytes = evmcs->guest_cs_ar_bytes;
1718 vmcs12->guest_ss_ar_bytes = evmcs->guest_ss_ar_bytes;
1719 vmcs12->guest_ds_ar_bytes = evmcs->guest_ds_ar_bytes;
1720 vmcs12->guest_fs_ar_bytes = evmcs->guest_fs_ar_bytes;
1721 vmcs12->guest_gs_ar_bytes = evmcs->guest_gs_ar_bytes;
1722 vmcs12->guest_ldtr_ar_bytes = evmcs->guest_ldtr_ar_bytes;
1723 vmcs12->guest_tr_ar_bytes = evmcs->guest_tr_ar_bytes;
1724 vmcs12->guest_es_selector = evmcs->guest_es_selector;
1725 vmcs12->guest_cs_selector = evmcs->guest_cs_selector;
1726 vmcs12->guest_ss_selector = evmcs->guest_ss_selector;
1727 vmcs12->guest_ds_selector = evmcs->guest_ds_selector;
1728 vmcs12->guest_fs_selector = evmcs->guest_fs_selector;
1729 vmcs12->guest_gs_selector = evmcs->guest_gs_selector;
1730 vmcs12->guest_ldtr_selector = evmcs->guest_ldtr_selector;
1731 vmcs12->guest_tr_selector = evmcs->guest_tr_selector;
1732 }
1733
1734 if (unlikely(!(evmcs->hv_clean_fields &
1735 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2))) {
1736 vmcs12->tsc_offset = evmcs->tsc_offset;
1737 vmcs12->virtual_apic_page_addr = evmcs->virtual_apic_page_addr;
1738 vmcs12->xss_exit_bitmap = evmcs->xss_exit_bitmap;
1739 }
1740
1741 if (unlikely(!(evmcs->hv_clean_fields &
1742 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR))) {
1743 vmcs12->cr0_guest_host_mask = evmcs->cr0_guest_host_mask;
1744 vmcs12->cr4_guest_host_mask = evmcs->cr4_guest_host_mask;
1745 vmcs12->cr0_read_shadow = evmcs->cr0_read_shadow;
1746 vmcs12->cr4_read_shadow = evmcs->cr4_read_shadow;
1747 vmcs12->guest_cr0 = evmcs->guest_cr0;
1748 vmcs12->guest_cr3 = evmcs->guest_cr3;
1749 vmcs12->guest_cr4 = evmcs->guest_cr4;
1750 vmcs12->guest_dr7 = evmcs->guest_dr7;
1751 }
1752
1753 if (unlikely(!(evmcs->hv_clean_fields &
1754 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER))) {
1755 vmcs12->host_fs_base = evmcs->host_fs_base;
1756 vmcs12->host_gs_base = evmcs->host_gs_base;
1757 vmcs12->host_tr_base = evmcs->host_tr_base;
1758 vmcs12->host_gdtr_base = evmcs->host_gdtr_base;
1759 vmcs12->host_idtr_base = evmcs->host_idtr_base;
1760 vmcs12->host_rsp = evmcs->host_rsp;
1761 }
1762
1763 if (unlikely(!(evmcs->hv_clean_fields &
1764 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_XLAT))) {
1765 vmcs12->ept_pointer = evmcs->ept_pointer;
1766 vmcs12->virtual_processor_id = evmcs->virtual_processor_id;
1767 }
1768
1769 if (unlikely(!(evmcs->hv_clean_fields &
1770 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1))) {
1771 vmcs12->vmcs_link_pointer = evmcs->vmcs_link_pointer;
1772 vmcs12->guest_ia32_debugctl = evmcs->guest_ia32_debugctl;
1773 vmcs12->guest_ia32_pat = evmcs->guest_ia32_pat;
1774 vmcs12->guest_ia32_efer = evmcs->guest_ia32_efer;
1775 vmcs12->guest_pdptr0 = evmcs->guest_pdptr0;
1776 vmcs12->guest_pdptr1 = evmcs->guest_pdptr1;
1777 vmcs12->guest_pdptr2 = evmcs->guest_pdptr2;
1778 vmcs12->guest_pdptr3 = evmcs->guest_pdptr3;
1779 vmcs12->guest_pending_dbg_exceptions =
1780 evmcs->guest_pending_dbg_exceptions;
1781 vmcs12->guest_sysenter_esp = evmcs->guest_sysenter_esp;
1782 vmcs12->guest_sysenter_eip = evmcs->guest_sysenter_eip;
1783 vmcs12->guest_bndcfgs = evmcs->guest_bndcfgs;
1784 vmcs12->guest_activity_state = evmcs->guest_activity_state;
1785 vmcs12->guest_sysenter_cs = evmcs->guest_sysenter_cs;
1786 }
1787
1788 /*
1789 * Not used?
1790 * vmcs12->vm_exit_msr_store_addr = evmcs->vm_exit_msr_store_addr;
1791 * vmcs12->vm_exit_msr_load_addr = evmcs->vm_exit_msr_load_addr;
1792 * vmcs12->vm_entry_msr_load_addr = evmcs->vm_entry_msr_load_addr;
David Brazdil0f672f62019-12-10 10:32:29 +00001793 * vmcs12->page_fault_error_code_mask =
1794 * evmcs->page_fault_error_code_mask;
1795 * vmcs12->page_fault_error_code_match =
1796 * evmcs->page_fault_error_code_match;
1797 * vmcs12->cr3_target_count = evmcs->cr3_target_count;
1798 * vmcs12->vm_exit_msr_store_count = evmcs->vm_exit_msr_store_count;
1799 * vmcs12->vm_exit_msr_load_count = evmcs->vm_exit_msr_load_count;
1800 * vmcs12->vm_entry_msr_load_count = evmcs->vm_entry_msr_load_count;
1801 */
1802
1803 /*
1804 * Read only fields:
1805 * vmcs12->guest_physical_address = evmcs->guest_physical_address;
1806 * vmcs12->vm_instruction_error = evmcs->vm_instruction_error;
1807 * vmcs12->vm_exit_reason = evmcs->vm_exit_reason;
1808 * vmcs12->vm_exit_intr_info = evmcs->vm_exit_intr_info;
1809 * vmcs12->vm_exit_intr_error_code = evmcs->vm_exit_intr_error_code;
1810 * vmcs12->idt_vectoring_info_field = evmcs->idt_vectoring_info_field;
1811 * vmcs12->idt_vectoring_error_code = evmcs->idt_vectoring_error_code;
1812 * vmcs12->vm_exit_instruction_len = evmcs->vm_exit_instruction_len;
1813 * vmcs12->vmx_instruction_info = evmcs->vmx_instruction_info;
1814 * vmcs12->exit_qualification = evmcs->exit_qualification;
1815 * vmcs12->guest_linear_address = evmcs->guest_linear_address;
1816 *
1817 * Not present in struct vmcs12:
1818 * vmcs12->exit_io_instruction_ecx = evmcs->exit_io_instruction_ecx;
1819 * vmcs12->exit_io_instruction_esi = evmcs->exit_io_instruction_esi;
1820 * vmcs12->exit_io_instruction_edi = evmcs->exit_io_instruction_edi;
1821 * vmcs12->exit_io_instruction_eip = evmcs->exit_io_instruction_eip;
1822 */
1823
1824 return 0;
1825}
1826
1827static int copy_vmcs12_to_enlightened(struct vcpu_vmx *vmx)
1828{
1829 struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12;
1830 struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
1831
1832 /*
1833 * Should not be changed by KVM:
1834 *
1835 * evmcs->host_es_selector = vmcs12->host_es_selector;
1836 * evmcs->host_cs_selector = vmcs12->host_cs_selector;
1837 * evmcs->host_ss_selector = vmcs12->host_ss_selector;
1838 * evmcs->host_ds_selector = vmcs12->host_ds_selector;
1839 * evmcs->host_fs_selector = vmcs12->host_fs_selector;
1840 * evmcs->host_gs_selector = vmcs12->host_gs_selector;
1841 * evmcs->host_tr_selector = vmcs12->host_tr_selector;
1842 * evmcs->host_ia32_pat = vmcs12->host_ia32_pat;
1843 * evmcs->host_ia32_efer = vmcs12->host_ia32_efer;
1844 * evmcs->host_cr0 = vmcs12->host_cr0;
1845 * evmcs->host_cr3 = vmcs12->host_cr3;
1846 * evmcs->host_cr4 = vmcs12->host_cr4;
1847 * evmcs->host_ia32_sysenter_esp = vmcs12->host_ia32_sysenter_esp;
1848 * evmcs->host_ia32_sysenter_eip = vmcs12->host_ia32_sysenter_eip;
1849 * evmcs->host_rip = vmcs12->host_rip;
1850 * evmcs->host_ia32_sysenter_cs = vmcs12->host_ia32_sysenter_cs;
1851 * evmcs->host_fs_base = vmcs12->host_fs_base;
1852 * evmcs->host_gs_base = vmcs12->host_gs_base;
1853 * evmcs->host_tr_base = vmcs12->host_tr_base;
1854 * evmcs->host_gdtr_base = vmcs12->host_gdtr_base;
1855 * evmcs->host_idtr_base = vmcs12->host_idtr_base;
1856 * evmcs->host_rsp = vmcs12->host_rsp;
1857 * sync_vmcs02_to_vmcs12() doesn't read these:
1858 * evmcs->io_bitmap_a = vmcs12->io_bitmap_a;
1859 * evmcs->io_bitmap_b = vmcs12->io_bitmap_b;
1860 * evmcs->msr_bitmap = vmcs12->msr_bitmap;
1861 * evmcs->ept_pointer = vmcs12->ept_pointer;
1862 * evmcs->xss_exit_bitmap = vmcs12->xss_exit_bitmap;
1863 * evmcs->vm_exit_msr_store_addr = vmcs12->vm_exit_msr_store_addr;
1864 * evmcs->vm_exit_msr_load_addr = vmcs12->vm_exit_msr_load_addr;
1865 * evmcs->vm_entry_msr_load_addr = vmcs12->vm_entry_msr_load_addr;
David Brazdil0f672f62019-12-10 10:32:29 +00001866 * evmcs->tpr_threshold = vmcs12->tpr_threshold;
1867 * evmcs->virtual_processor_id = vmcs12->virtual_processor_id;
1868 * evmcs->exception_bitmap = vmcs12->exception_bitmap;
1869 * evmcs->vmcs_link_pointer = vmcs12->vmcs_link_pointer;
1870 * evmcs->pin_based_vm_exec_control = vmcs12->pin_based_vm_exec_control;
1871 * evmcs->vm_exit_controls = vmcs12->vm_exit_controls;
1872 * evmcs->secondary_vm_exec_control = vmcs12->secondary_vm_exec_control;
1873 * evmcs->page_fault_error_code_mask =
1874 * vmcs12->page_fault_error_code_mask;
1875 * evmcs->page_fault_error_code_match =
1876 * vmcs12->page_fault_error_code_match;
1877 * evmcs->cr3_target_count = vmcs12->cr3_target_count;
1878 * evmcs->virtual_apic_page_addr = vmcs12->virtual_apic_page_addr;
1879 * evmcs->tsc_offset = vmcs12->tsc_offset;
1880 * evmcs->guest_ia32_debugctl = vmcs12->guest_ia32_debugctl;
1881 * evmcs->cr0_guest_host_mask = vmcs12->cr0_guest_host_mask;
1882 * evmcs->cr4_guest_host_mask = vmcs12->cr4_guest_host_mask;
1883 * evmcs->cr0_read_shadow = vmcs12->cr0_read_shadow;
1884 * evmcs->cr4_read_shadow = vmcs12->cr4_read_shadow;
1885 * evmcs->vm_exit_msr_store_count = vmcs12->vm_exit_msr_store_count;
1886 * evmcs->vm_exit_msr_load_count = vmcs12->vm_exit_msr_load_count;
1887 * evmcs->vm_entry_msr_load_count = vmcs12->vm_entry_msr_load_count;
1888 *
1889 * Not present in struct vmcs12:
1890 * evmcs->exit_io_instruction_ecx = vmcs12->exit_io_instruction_ecx;
1891 * evmcs->exit_io_instruction_esi = vmcs12->exit_io_instruction_esi;
1892 * evmcs->exit_io_instruction_edi = vmcs12->exit_io_instruction_edi;
1893 * evmcs->exit_io_instruction_eip = vmcs12->exit_io_instruction_eip;
1894 */
1895
1896 evmcs->guest_es_selector = vmcs12->guest_es_selector;
1897 evmcs->guest_cs_selector = vmcs12->guest_cs_selector;
1898 evmcs->guest_ss_selector = vmcs12->guest_ss_selector;
1899 evmcs->guest_ds_selector = vmcs12->guest_ds_selector;
1900 evmcs->guest_fs_selector = vmcs12->guest_fs_selector;
1901 evmcs->guest_gs_selector = vmcs12->guest_gs_selector;
1902 evmcs->guest_ldtr_selector = vmcs12->guest_ldtr_selector;
1903 evmcs->guest_tr_selector = vmcs12->guest_tr_selector;
1904
1905 evmcs->guest_es_limit = vmcs12->guest_es_limit;
1906 evmcs->guest_cs_limit = vmcs12->guest_cs_limit;
1907 evmcs->guest_ss_limit = vmcs12->guest_ss_limit;
1908 evmcs->guest_ds_limit = vmcs12->guest_ds_limit;
1909 evmcs->guest_fs_limit = vmcs12->guest_fs_limit;
1910 evmcs->guest_gs_limit = vmcs12->guest_gs_limit;
1911 evmcs->guest_ldtr_limit = vmcs12->guest_ldtr_limit;
1912 evmcs->guest_tr_limit = vmcs12->guest_tr_limit;
1913 evmcs->guest_gdtr_limit = vmcs12->guest_gdtr_limit;
1914 evmcs->guest_idtr_limit = vmcs12->guest_idtr_limit;
1915
1916 evmcs->guest_es_ar_bytes = vmcs12->guest_es_ar_bytes;
1917 evmcs->guest_cs_ar_bytes = vmcs12->guest_cs_ar_bytes;
1918 evmcs->guest_ss_ar_bytes = vmcs12->guest_ss_ar_bytes;
1919 evmcs->guest_ds_ar_bytes = vmcs12->guest_ds_ar_bytes;
1920 evmcs->guest_fs_ar_bytes = vmcs12->guest_fs_ar_bytes;
1921 evmcs->guest_gs_ar_bytes = vmcs12->guest_gs_ar_bytes;
1922 evmcs->guest_ldtr_ar_bytes = vmcs12->guest_ldtr_ar_bytes;
1923 evmcs->guest_tr_ar_bytes = vmcs12->guest_tr_ar_bytes;
1924
1925 evmcs->guest_es_base = vmcs12->guest_es_base;
1926 evmcs->guest_cs_base = vmcs12->guest_cs_base;
1927 evmcs->guest_ss_base = vmcs12->guest_ss_base;
1928 evmcs->guest_ds_base = vmcs12->guest_ds_base;
1929 evmcs->guest_fs_base = vmcs12->guest_fs_base;
1930 evmcs->guest_gs_base = vmcs12->guest_gs_base;
1931 evmcs->guest_ldtr_base = vmcs12->guest_ldtr_base;
1932 evmcs->guest_tr_base = vmcs12->guest_tr_base;
1933 evmcs->guest_gdtr_base = vmcs12->guest_gdtr_base;
1934 evmcs->guest_idtr_base = vmcs12->guest_idtr_base;
1935
1936 evmcs->guest_ia32_pat = vmcs12->guest_ia32_pat;
1937 evmcs->guest_ia32_efer = vmcs12->guest_ia32_efer;
1938
1939 evmcs->guest_pdptr0 = vmcs12->guest_pdptr0;
1940 evmcs->guest_pdptr1 = vmcs12->guest_pdptr1;
1941 evmcs->guest_pdptr2 = vmcs12->guest_pdptr2;
1942 evmcs->guest_pdptr3 = vmcs12->guest_pdptr3;
1943
1944 evmcs->guest_pending_dbg_exceptions =
1945 vmcs12->guest_pending_dbg_exceptions;
1946 evmcs->guest_sysenter_esp = vmcs12->guest_sysenter_esp;
1947 evmcs->guest_sysenter_eip = vmcs12->guest_sysenter_eip;
1948
1949 evmcs->guest_activity_state = vmcs12->guest_activity_state;
1950 evmcs->guest_sysenter_cs = vmcs12->guest_sysenter_cs;
1951
1952 evmcs->guest_cr0 = vmcs12->guest_cr0;
1953 evmcs->guest_cr3 = vmcs12->guest_cr3;
1954 evmcs->guest_cr4 = vmcs12->guest_cr4;
1955 evmcs->guest_dr7 = vmcs12->guest_dr7;
1956
1957 evmcs->guest_physical_address = vmcs12->guest_physical_address;
1958
1959 evmcs->vm_instruction_error = vmcs12->vm_instruction_error;
1960 evmcs->vm_exit_reason = vmcs12->vm_exit_reason;
1961 evmcs->vm_exit_intr_info = vmcs12->vm_exit_intr_info;
1962 evmcs->vm_exit_intr_error_code = vmcs12->vm_exit_intr_error_code;
1963 evmcs->idt_vectoring_info_field = vmcs12->idt_vectoring_info_field;
1964 evmcs->idt_vectoring_error_code = vmcs12->idt_vectoring_error_code;
1965 evmcs->vm_exit_instruction_len = vmcs12->vm_exit_instruction_len;
1966 evmcs->vmx_instruction_info = vmcs12->vmx_instruction_info;
1967
1968 evmcs->exit_qualification = vmcs12->exit_qualification;
1969
1970 evmcs->guest_linear_address = vmcs12->guest_linear_address;
1971 evmcs->guest_rsp = vmcs12->guest_rsp;
1972 evmcs->guest_rflags = vmcs12->guest_rflags;
1973
1974 evmcs->guest_interruptibility_info =
1975 vmcs12->guest_interruptibility_info;
1976 evmcs->cpu_based_vm_exec_control = vmcs12->cpu_based_vm_exec_control;
1977 evmcs->vm_entry_controls = vmcs12->vm_entry_controls;
1978 evmcs->vm_entry_intr_info_field = vmcs12->vm_entry_intr_info_field;
1979 evmcs->vm_entry_exception_error_code =
1980 vmcs12->vm_entry_exception_error_code;
1981 evmcs->vm_entry_instruction_len = vmcs12->vm_entry_instruction_len;
1982
1983 evmcs->guest_rip = vmcs12->guest_rip;
1984
1985 evmcs->guest_bndcfgs = vmcs12->guest_bndcfgs;
1986
1987 return 0;
1988}
1989
1990/*
1991 * This is an equivalent of the nested hypervisor executing the vmptrld
1992 * instruction.
1993 */
Olivier Deprez157378f2022-04-04 15:47:50 +02001994static enum nested_evmptrld_status nested_vmx_handle_enlightened_vmptrld(
1995 struct kvm_vcpu *vcpu, bool from_launch)
David Brazdil0f672f62019-12-10 10:32:29 +00001996{
1997 struct vcpu_vmx *vmx = to_vmx(vcpu);
1998 bool evmcs_gpa_changed = false;
1999 u64 evmcs_gpa;
2000
2001 if (likely(!vmx->nested.enlightened_vmcs_enabled))
Olivier Deprez157378f2022-04-04 15:47:50 +02002002 return EVMPTRLD_DISABLED;
David Brazdil0f672f62019-12-10 10:32:29 +00002003
2004 if (!nested_enlightened_vmentry(vcpu, &evmcs_gpa))
Olivier Deprez157378f2022-04-04 15:47:50 +02002005 return EVMPTRLD_DISABLED;
David Brazdil0f672f62019-12-10 10:32:29 +00002006
Olivier Deprez0e641232021-09-23 10:07:05 +02002007 if (unlikely(!vmx->nested.hv_evmcs ||
2008 evmcs_gpa != vmx->nested.hv_evmcs_vmptr)) {
David Brazdil0f672f62019-12-10 10:32:29 +00002009 if (!vmx->nested.hv_evmcs)
2010 vmx->nested.current_vmptr = -1ull;
2011
2012 nested_release_evmcs(vcpu);
2013
2014 if (kvm_vcpu_map(vcpu, gpa_to_gfn(evmcs_gpa),
2015 &vmx->nested.hv_evmcs_map))
Olivier Deprez157378f2022-04-04 15:47:50 +02002016 return EVMPTRLD_ERROR;
David Brazdil0f672f62019-12-10 10:32:29 +00002017
2018 vmx->nested.hv_evmcs = vmx->nested.hv_evmcs_map.hva;
2019
2020 /*
2021 * Currently, KVM only supports eVMCS version 1
2022 * (== KVM_EVMCS_VERSION) and thus we expect guest to set this
2023 * value to first u32 field of eVMCS which should specify eVMCS
2024 * VersionNumber.
2025 *
2026 * Guest should be aware of supported eVMCS versions by host by
2027 * examining CPUID.0x4000000A.EAX[0:15]. Host userspace VMM is
2028 * expected to set this CPUID leaf according to the value
2029 * returned in vmcs_version from nested_enable_evmcs().
2030 *
2031 * However, it turns out that Microsoft Hyper-V fails to comply
2032 * to their own invented interface: When Hyper-V use eVMCS, it
2033 * just sets first u32 field of eVMCS to revision_id specified
2034 * in MSR_IA32_VMX_BASIC. Instead of used eVMCS version number
2035 * which is one of the supported versions specified in
2036 * CPUID.0x4000000A.EAX[0:15].
2037 *
2038 * To overcome Hyper-V bug, we accept here either a supported
2039 * eVMCS version or VMCS12 revision_id as valid values for first
2040 * u32 field of eVMCS.
2041 */
2042 if ((vmx->nested.hv_evmcs->revision_id != KVM_EVMCS_VERSION) &&
2043 (vmx->nested.hv_evmcs->revision_id != VMCS12_REVISION)) {
2044 nested_release_evmcs(vcpu);
Olivier Deprez157378f2022-04-04 15:47:50 +02002045 return EVMPTRLD_VMFAIL;
David Brazdil0f672f62019-12-10 10:32:29 +00002046 }
2047
2048 vmx->nested.dirty_vmcs12 = true;
2049 vmx->nested.hv_evmcs_vmptr = evmcs_gpa;
2050
2051 evmcs_gpa_changed = true;
2052 /*
2053 * Unlike normal vmcs12, enlightened vmcs12 is not fully
2054 * reloaded from guest's memory (read only fields, fields not
2055 * present in struct hv_enlightened_vmcs, ...). Make sure there
2056 * are no leftovers.
2057 */
2058 if (from_launch) {
2059 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2060 memset(vmcs12, 0, sizeof(*vmcs12));
2061 vmcs12->hdr.revision_id = VMCS12_REVISION;
2062 }
2063
2064 }
2065
2066 /*
Olivier Deprez157378f2022-04-04 15:47:50 +02002067 * Clean fields data can't be used on VMLAUNCH and when we switch
David Brazdil0f672f62019-12-10 10:32:29 +00002068 * between different L2 guests as KVM keeps a single VMCS12 per L1.
2069 */
2070 if (from_launch || evmcs_gpa_changed)
2071 vmx->nested.hv_evmcs->hv_clean_fields &=
2072 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
2073
Olivier Deprez157378f2022-04-04 15:47:50 +02002074 return EVMPTRLD_SUCCEEDED;
David Brazdil0f672f62019-12-10 10:32:29 +00002075}
2076
2077void nested_sync_vmcs12_to_shadow(struct kvm_vcpu *vcpu)
2078{
2079 struct vcpu_vmx *vmx = to_vmx(vcpu);
2080
David Brazdil0f672f62019-12-10 10:32:29 +00002081 if (vmx->nested.hv_evmcs) {
2082 copy_vmcs12_to_enlightened(vmx);
2083 /* All fields are clean */
2084 vmx->nested.hv_evmcs->hv_clean_fields |=
2085 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
2086 } else {
2087 copy_vmcs12_to_shadow(vmx);
2088 }
2089
2090 vmx->nested.need_vmcs12_to_shadow_sync = false;
2091}
2092
2093static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
2094{
2095 struct vcpu_vmx *vmx =
2096 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
2097
2098 vmx->nested.preemption_timer_expired = true;
2099 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
2100 kvm_vcpu_kick(&vmx->vcpu);
2101
2102 return HRTIMER_NORESTART;
2103}
2104
Olivier Deprez157378f2022-04-04 15:47:50 +02002105static u64 vmx_calc_preemption_timer_value(struct kvm_vcpu *vcpu)
David Brazdil0f672f62019-12-10 10:32:29 +00002106{
Olivier Deprez157378f2022-04-04 15:47:50 +02002107 struct vcpu_vmx *vmx = to_vmx(vcpu);
2108 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2109
2110 u64 l1_scaled_tsc = kvm_read_l1_tsc(vcpu, rdtsc()) >>
2111 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
2112
2113 if (!vmx->nested.has_preemption_timer_deadline) {
2114 vmx->nested.preemption_timer_deadline =
2115 vmcs12->vmx_preemption_timer_value + l1_scaled_tsc;
2116 vmx->nested.has_preemption_timer_deadline = true;
2117 }
2118 return vmx->nested.preemption_timer_deadline - l1_scaled_tsc;
2119}
2120
2121static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu,
2122 u64 preemption_timeout)
2123{
David Brazdil0f672f62019-12-10 10:32:29 +00002124 struct vcpu_vmx *vmx = to_vmx(vcpu);
2125
2126 /*
2127 * A timer value of zero is architecturally guaranteed to cause
2128 * a VMExit prior to executing any instructions in the guest.
2129 */
2130 if (preemption_timeout == 0) {
2131 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
2132 return;
2133 }
2134
2135 if (vcpu->arch.virtual_tsc_khz == 0)
2136 return;
2137
2138 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
2139 preemption_timeout *= 1000000;
2140 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
2141 hrtimer_start(&vmx->nested.preemption_timer,
Olivier Deprez157378f2022-04-04 15:47:50 +02002142 ktime_add_ns(ktime_get(), preemption_timeout),
2143 HRTIMER_MODE_ABS_PINNED);
David Brazdil0f672f62019-12-10 10:32:29 +00002144}
2145
2146static u64 nested_vmx_calc_efer(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
2147{
2148 if (vmx->nested.nested_run_pending &&
2149 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
2150 return vmcs12->guest_ia32_efer;
2151 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
2152 return vmx->vcpu.arch.efer | (EFER_LMA | EFER_LME);
2153 else
2154 return vmx->vcpu.arch.efer & ~(EFER_LMA | EFER_LME);
2155}
2156
2157static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx)
2158{
2159 /*
2160 * If vmcs02 hasn't been initialized, set the constant vmcs02 state
2161 * according to L0's settings (vmcs12 is irrelevant here). Host
2162 * fields that come from L0 and are not constant, e.g. HOST_CR3,
2163 * will be set as needed prior to VMLAUNCH/VMRESUME.
2164 */
2165 if (vmx->nested.vmcs02_initialized)
2166 return;
2167 vmx->nested.vmcs02_initialized = true;
2168
2169 /*
2170 * We don't care what the EPTP value is we just need to guarantee
2171 * it's valid so we don't get a false positive when doing early
2172 * consistency checks.
2173 */
2174 if (enable_ept && nested_early_check)
Olivier Deprez157378f2022-04-04 15:47:50 +02002175 vmcs_write64(EPT_POINTER,
2176 construct_eptp(&vmx->vcpu, 0, PT64_ROOT_4LEVEL));
David Brazdil0f672f62019-12-10 10:32:29 +00002177
2178 /* All VMFUNCs are currently emulated through L0 vmexits. */
2179 if (cpu_has_vmx_vmfunc())
2180 vmcs_write64(VM_FUNCTION_CONTROL, 0);
2181
2182 if (cpu_has_vmx_posted_intr())
2183 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
2184
2185 if (cpu_has_vmx_msr_bitmap())
2186 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
2187
2188 /*
2189 * The PML address never changes, so it is constant in vmcs02.
2190 * Conceptually we want to copy the PML index from vmcs01 here,
2191 * and then back to vmcs01 on nested vmexit. But since we flush
2192 * the log and reset GUEST_PML_INDEX on each vmexit, the PML
2193 * index is also effectively constant in vmcs02.
2194 */
2195 if (enable_pml) {
2196 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
2197 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
2198 }
2199
2200 if (cpu_has_vmx_encls_vmexit())
2201 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
2202
2203 /*
2204 * Set the MSR load/store lists to match L0's settings. Only the
2205 * addresses are constant (for vmcs02), the counts can change based
2206 * on L2's behavior, e.g. switching to/from long mode.
2207 */
Olivier Deprez157378f2022-04-04 15:47:50 +02002208 vmcs_write64(VM_EXIT_MSR_STORE_ADDR, __pa(vmx->msr_autostore.guest.val));
David Brazdil0f672f62019-12-10 10:32:29 +00002209 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
2210 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
2211
2212 vmx_set_constant_host_state(vmx);
2213}
2214
2215static void prepare_vmcs02_early_rare(struct vcpu_vmx *vmx,
2216 struct vmcs12 *vmcs12)
2217{
2218 prepare_vmcs02_constant_state(vmx);
2219
2220 vmcs_write64(VMCS_LINK_POINTER, -1ull);
2221
2222 if (enable_vpid) {
2223 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
2224 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
2225 else
2226 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
2227 }
2228}
2229
2230static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
2231{
2232 u32 exec_control, vmcs12_exec_ctrl;
2233 u64 guest_efer = nested_vmx_calc_efer(vmx, vmcs12);
2234
2235 if (vmx->nested.dirty_vmcs12 || vmx->nested.hv_evmcs)
2236 prepare_vmcs02_early_rare(vmx, vmcs12);
2237
2238 /*
2239 * PIN CONTROLS
2240 */
2241 exec_control = vmx_pin_based_exec_ctrl(vmx);
2242 exec_control |= (vmcs12->pin_based_vm_exec_control &
2243 ~PIN_BASED_VMX_PREEMPTION_TIMER);
2244
2245 /* Posted interrupts setting is only taken from vmcs12. */
Olivier Deprez0e641232021-09-23 10:07:05 +02002246 vmx->nested.pi_pending = false;
2247 if (nested_cpu_has_posted_intr(vmcs12))
David Brazdil0f672f62019-12-10 10:32:29 +00002248 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
Olivier Deprez0e641232021-09-23 10:07:05 +02002249 else
David Brazdil0f672f62019-12-10 10:32:29 +00002250 exec_control &= ~PIN_BASED_POSTED_INTR;
David Brazdil0f672f62019-12-10 10:32:29 +00002251 pin_controls_set(vmx, exec_control);
2252
2253 /*
2254 * EXEC CONTROLS
2255 */
2256 exec_control = vmx_exec_control(vmx); /* L0's desires */
Olivier Deprez157378f2022-04-04 15:47:50 +02002257 exec_control &= ~CPU_BASED_INTR_WINDOW_EXITING;
2258 exec_control &= ~CPU_BASED_NMI_WINDOW_EXITING;
David Brazdil0f672f62019-12-10 10:32:29 +00002259 exec_control &= ~CPU_BASED_TPR_SHADOW;
2260 exec_control |= vmcs12->cpu_based_vm_exec_control;
2261
Olivier Deprez157378f2022-04-04 15:47:50 +02002262 vmx->nested.l1_tpr_threshold = -1;
David Brazdil0f672f62019-12-10 10:32:29 +00002263 if (exec_control & CPU_BASED_TPR_SHADOW)
2264 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
2265#ifdef CONFIG_X86_64
2266 else
2267 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
2268 CPU_BASED_CR8_STORE_EXITING;
2269#endif
2270
2271 /*
2272 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
2273 * for I/O port accesses.
2274 */
2275 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
2276 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
2277
2278 /*
2279 * This bit will be computed in nested_get_vmcs12_pages, because
2280 * we do not have access to L1's MSR bitmap yet. For now, keep
2281 * the same bit as before, hoping to avoid multiple VMWRITEs that
2282 * only set/clear this bit.
2283 */
2284 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
2285 exec_control |= exec_controls_get(vmx) & CPU_BASED_USE_MSR_BITMAPS;
2286
2287 exec_controls_set(vmx, exec_control);
2288
2289 /*
2290 * SECONDARY EXEC CONTROLS
2291 */
2292 if (cpu_has_secondary_exec_ctrls()) {
2293 exec_control = vmx->secondary_exec_control;
2294
2295 /* Take the following fields only from vmcs12 */
2296 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
2297 SECONDARY_EXEC_ENABLE_INVPCID |
Olivier Deprez157378f2022-04-04 15:47:50 +02002298 SECONDARY_EXEC_ENABLE_RDTSCP |
David Brazdil0f672f62019-12-10 10:32:29 +00002299 SECONDARY_EXEC_XSAVES |
2300 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE |
2301 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
2302 SECONDARY_EXEC_APIC_REGISTER_VIRT |
2303 SECONDARY_EXEC_ENABLE_VMFUNC);
2304 if (nested_cpu_has(vmcs12,
2305 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
2306 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
2307 ~SECONDARY_EXEC_ENABLE_PML;
2308 exec_control |= vmcs12_exec_ctrl;
2309 }
2310
2311 /* VMCS shadowing for L2 is emulated for now */
2312 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
2313
2314 /*
2315 * Preset *DT exiting when emulating UMIP, so that vmx_set_cr4()
2316 * will not have to rewrite the controls just for this bit.
2317 */
2318 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated() &&
2319 (vmcs12->guest_cr4 & X86_CR4_UMIP))
2320 exec_control |= SECONDARY_EXEC_DESC;
2321
2322 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
2323 vmcs_write16(GUEST_INTR_STATUS,
2324 vmcs12->guest_intr_status);
2325
Olivier Deprez157378f2022-04-04 15:47:50 +02002326 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
2327 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
2328
David Brazdil0f672f62019-12-10 10:32:29 +00002329 secondary_exec_controls_set(vmx, exec_control);
2330 }
2331
2332 /*
2333 * ENTRY CONTROLS
2334 *
2335 * vmcs12's VM_{ENTRY,EXIT}_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE
2336 * are emulated by vmx_set_efer() in prepare_vmcs02(), but speculate
2337 * on the related bits (if supported by the CPU) in the hope that
2338 * we can avoid VMWrites during vmx_set_efer().
2339 */
2340 exec_control = (vmcs12->vm_entry_controls | vmx_vmentry_ctrl()) &
2341 ~VM_ENTRY_IA32E_MODE & ~VM_ENTRY_LOAD_IA32_EFER;
2342 if (cpu_has_load_ia32_efer()) {
2343 if (guest_efer & EFER_LMA)
2344 exec_control |= VM_ENTRY_IA32E_MODE;
2345 if (guest_efer != host_efer)
2346 exec_control |= VM_ENTRY_LOAD_IA32_EFER;
2347 }
2348 vm_entry_controls_set(vmx, exec_control);
2349
2350 /*
2351 * EXIT CONTROLS
2352 *
2353 * L2->L1 exit controls are emulated - the hardware exit is to L0 so
2354 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
2355 * bits may be modified by vmx_set_efer() in prepare_vmcs02().
2356 */
2357 exec_control = vmx_vmexit_ctrl();
2358 if (cpu_has_load_ia32_efer() && guest_efer != host_efer)
2359 exec_control |= VM_EXIT_LOAD_IA32_EFER;
2360 vm_exit_controls_set(vmx, exec_control);
2361
2362 /*
2363 * Interrupt/Exception Fields
2364 */
2365 if (vmx->nested.nested_run_pending) {
2366 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2367 vmcs12->vm_entry_intr_info_field);
2368 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
2369 vmcs12->vm_entry_exception_error_code);
2370 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2371 vmcs12->vm_entry_instruction_len);
2372 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
2373 vmcs12->guest_interruptibility_info);
2374 vmx->loaded_vmcs->nmi_known_unmasked =
2375 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
2376 } else {
2377 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
2378 }
2379}
2380
2381static void prepare_vmcs02_rare(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
2382{
2383 struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs;
2384
2385 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
2386 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2)) {
2387 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
2388 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
2389 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
2390 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
2391 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
2392 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
2393 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
2394 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
2395 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
2396 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
2397 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
2398 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
2399 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
2400 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
2401 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
2402 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
2403 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
2404 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
2405 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
2406 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
2407 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
2408 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
2409 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
2410 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
2411 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
2412 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
2413 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
2414 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
2415 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
2416 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
2417 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
2418 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
2419 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
2420 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
2421 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
2422 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Olivier Deprez0e641232021-09-23 10:07:05 +02002423
2424 vmx->segment_cache.bitmask = 0;
David Brazdil0f672f62019-12-10 10:32:29 +00002425 }
2426
2427 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
2428 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1)) {
2429 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
2430 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
2431 vmcs12->guest_pending_dbg_exceptions);
2432 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
2433 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
2434
2435 /*
2436 * L1 may access the L2's PDPTR, so save them to construct
2437 * vmcs12
2438 */
2439 if (enable_ept) {
2440 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
2441 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
2442 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
2443 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
2444 }
2445
2446 if (kvm_mpx_supported() && vmx->nested.nested_run_pending &&
2447 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
2448 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
2449 }
2450
2451 if (nested_cpu_has_xsaves(vmcs12))
2452 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
2453
2454 /*
2455 * Whether page-faults are trapped is determined by a combination of
Olivier Deprez157378f2022-04-04 15:47:50 +02002456 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF. If L0
2457 * doesn't care about page faults then we should set all of these to
2458 * L1's desires. However, if L0 does care about (some) page faults, it
2459 * is not easy (if at all possible?) to merge L0 and L1's desires, we
2460 * simply ask to exit on each and every L2 page fault. This is done by
2461 * setting MASK=MATCH=0 and (see below) EB.PF=1.
David Brazdil0f672f62019-12-10 10:32:29 +00002462 * Note that below we don't need special code to set EB.PF beyond the
2463 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
2464 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
2465 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
2466 */
Olivier Deprez157378f2022-04-04 15:47:50 +02002467 if (vmx_need_pf_intercept(&vmx->vcpu)) {
2468 /*
2469 * TODO: if both L0 and L1 need the same MASK and MATCH,
2470 * go ahead and use it?
2471 */
2472 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
2473 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
2474 } else {
2475 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, vmcs12->page_fault_error_code_mask);
2476 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, vmcs12->page_fault_error_code_match);
2477 }
David Brazdil0f672f62019-12-10 10:32:29 +00002478
2479 if (cpu_has_vmx_apicv()) {
2480 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
2481 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
2482 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
2483 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
2484 }
2485
Olivier Deprez157378f2022-04-04 15:47:50 +02002486 /*
2487 * Make sure the msr_autostore list is up to date before we set the
2488 * count in the vmcs02.
2489 */
2490 prepare_vmx_msr_autostore_list(&vmx->vcpu, MSR_IA32_TSC);
2491
2492 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, vmx->msr_autostore.guest.nr);
David Brazdil0f672f62019-12-10 10:32:29 +00002493 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
2494 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
2495
2496 set_cr4_guest_host_mask(vmx);
2497}
2498
2499/*
2500 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
2501 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
2502 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
2503 * guest in a way that will both be appropriate to L1's requests, and our
2504 * needs. In addition to modifying the active vmcs (which is vmcs02), this
2505 * function also has additional necessary side-effects, like setting various
2506 * vcpu->arch fields.
2507 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
2508 * is assigned to entry_failure_code on failure.
2509 */
2510static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Olivier Deprez157378f2022-04-04 15:47:50 +02002511 enum vm_entry_failure_code *entry_failure_code)
David Brazdil0f672f62019-12-10 10:32:29 +00002512{
2513 struct vcpu_vmx *vmx = to_vmx(vcpu);
2514 struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs;
2515 bool load_guest_pdptrs_vmcs12 = false;
2516
2517 if (vmx->nested.dirty_vmcs12 || hv_evmcs) {
2518 prepare_vmcs02_rare(vmx, vmcs12);
2519 vmx->nested.dirty_vmcs12 = false;
2520
2521 load_guest_pdptrs_vmcs12 = !hv_evmcs ||
2522 !(hv_evmcs->hv_clean_fields &
2523 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1);
2524 }
2525
2526 if (vmx->nested.nested_run_pending &&
2527 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
2528 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
2529 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
2530 } else {
2531 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
2532 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
2533 }
2534 if (kvm_mpx_supported() && (!vmx->nested.nested_run_pending ||
2535 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)))
2536 vmcs_write64(GUEST_BNDCFGS, vmx->nested.vmcs01_guest_bndcfgs);
2537 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
2538
2539 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
2540 * bitwise-or of what L1 wants to trap for L2, and what we want to
2541 * trap. Note that CR0.TS also needs updating - we do this later.
2542 */
2543 update_exception_bitmap(vcpu);
2544 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
2545 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
2546
2547 if (vmx->nested.nested_run_pending &&
2548 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
2549 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
2550 vcpu->arch.pat = vmcs12->guest_ia32_pat;
2551 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2552 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
2553 }
2554
2555 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
2556
2557 if (kvm_has_tsc_control)
2558 decache_tsc_multiplier(vmx);
2559
Olivier Deprez157378f2022-04-04 15:47:50 +02002560 nested_vmx_transition_tlb_flush(vcpu, vmcs12, true);
David Brazdil0f672f62019-12-10 10:32:29 +00002561
2562 if (nested_cpu_has_ept(vmcs12))
2563 nested_ept_init_mmu_context(vcpu);
David Brazdil0f672f62019-12-10 10:32:29 +00002564
2565 /*
2566 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
2567 * bits which we consider mandatory enabled.
2568 * The CR0_READ_SHADOW is what L2 should have expected to read given
2569 * the specifications by L1; It's not enough to take
2570 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
2571 * have more bits than L1 expected.
2572 */
2573 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
2574 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
2575
2576 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
2577 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
2578
2579 vcpu->arch.efer = nested_vmx_calc_efer(vmx, vmcs12);
2580 /* Note: may modify VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
2581 vmx_set_efer(vcpu, vcpu->arch.efer);
2582
2583 /*
2584 * Guest state is invalid and unrestricted guest is disabled,
2585 * which means L1 attempted VMEntry to L2 with invalid state.
2586 * Fail the VMEntry.
2587 */
Olivier Deprez157378f2022-04-04 15:47:50 +02002588 if (CC(!vmx_guest_state_valid(vcpu))) {
David Brazdil0f672f62019-12-10 10:32:29 +00002589 *entry_failure_code = ENTRY_FAIL_DEFAULT;
2590 return -EINVAL;
2591 }
2592
2593 /* Shadow page tables on either EPT or shadow page tables. */
2594 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
2595 entry_failure_code))
2596 return -EINVAL;
2597
Olivier Deprez0e641232021-09-23 10:07:05 +02002598 /*
2599 * Immediately write vmcs02.GUEST_CR3. It will be propagated to vmcs12
2600 * on nested VM-Exit, which can occur without actually running L2 and
Olivier Deprez157378f2022-04-04 15:47:50 +02002601 * thus without hitting vmx_load_mmu_pgd(), e.g. if L1 is entering L2 with
Olivier Deprez0e641232021-09-23 10:07:05 +02002602 * vmcs12.GUEST_ACTIVITYSTATE=HLT, in which case KVM will intercept the
2603 * transition to HLT instead of running L2.
2604 */
2605 if (enable_ept)
2606 vmcs_writel(GUEST_CR3, vmcs12->guest_cr3);
2607
David Brazdil0f672f62019-12-10 10:32:29 +00002608 /* Late preparation of GUEST_PDPTRs now that EFER and CRs are set. */
2609 if (load_guest_pdptrs_vmcs12 && nested_cpu_has_ept(vmcs12) &&
2610 is_pae_paging(vcpu)) {
2611 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
2612 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
2613 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
2614 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
2615 }
2616
2617 if (!enable_ept)
2618 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
2619
Olivier Deprez157378f2022-04-04 15:47:50 +02002620 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) &&
2621 WARN_ON_ONCE(kvm_set_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL,
2622 vmcs12->guest_ia32_perf_global_ctrl))) {
2623 *entry_failure_code = ENTRY_FAIL_DEFAULT;
2624 return -EINVAL;
2625 }
2626
David Brazdil0f672f62019-12-10 10:32:29 +00002627 kvm_rsp_write(vcpu, vmcs12->guest_rsp);
2628 kvm_rip_write(vcpu, vmcs12->guest_rip);
2629 return 0;
2630}
2631
2632static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
2633{
2634 if (CC(!nested_cpu_has_nmi_exiting(vmcs12) &&
2635 nested_cpu_has_virtual_nmis(vmcs12)))
2636 return -EINVAL;
2637
2638 if (CC(!nested_cpu_has_virtual_nmis(vmcs12) &&
Olivier Deprez157378f2022-04-04 15:47:50 +02002639 nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING)))
David Brazdil0f672f62019-12-10 10:32:29 +00002640 return -EINVAL;
2641
2642 return 0;
2643}
2644
Olivier Deprez157378f2022-04-04 15:47:50 +02002645static bool nested_vmx_check_eptp(struct kvm_vcpu *vcpu, u64 new_eptp)
David Brazdil0f672f62019-12-10 10:32:29 +00002646{
2647 struct vcpu_vmx *vmx = to_vmx(vcpu);
2648 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2649
2650 /* Check for memory type validity */
Olivier Deprez157378f2022-04-04 15:47:50 +02002651 switch (new_eptp & VMX_EPTP_MT_MASK) {
David Brazdil0f672f62019-12-10 10:32:29 +00002652 case VMX_EPTP_MT_UC:
2653 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT)))
2654 return false;
2655 break;
2656 case VMX_EPTP_MT_WB:
2657 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT)))
2658 return false;
2659 break;
2660 default:
2661 return false;
2662 }
2663
Olivier Deprez157378f2022-04-04 15:47:50 +02002664 /* Page-walk levels validity. */
2665 switch (new_eptp & VMX_EPTP_PWL_MASK) {
2666 case VMX_EPTP_PWL_5:
2667 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_PAGE_WALK_5_BIT)))
2668 return false;
2669 break;
2670 case VMX_EPTP_PWL_4:
2671 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_PAGE_WALK_4_BIT)))
2672 return false;
2673 break;
2674 default:
David Brazdil0f672f62019-12-10 10:32:29 +00002675 return false;
Olivier Deprez157378f2022-04-04 15:47:50 +02002676 }
David Brazdil0f672f62019-12-10 10:32:29 +00002677
2678 /* Reserved bits should not be set */
Olivier Deprez157378f2022-04-04 15:47:50 +02002679 if (CC(new_eptp >> maxphyaddr || ((new_eptp >> 7) & 0x1f)))
David Brazdil0f672f62019-12-10 10:32:29 +00002680 return false;
2681
2682 /* AD, if set, should be supported */
Olivier Deprez157378f2022-04-04 15:47:50 +02002683 if (new_eptp & VMX_EPTP_AD_ENABLE_BIT) {
David Brazdil0f672f62019-12-10 10:32:29 +00002684 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT)))
2685 return false;
2686 }
2687
2688 return true;
2689}
2690
2691/*
2692 * Checks related to VM-Execution Control Fields
2693 */
2694static int nested_check_vm_execution_controls(struct kvm_vcpu *vcpu,
2695 struct vmcs12 *vmcs12)
2696{
2697 struct vcpu_vmx *vmx = to_vmx(vcpu);
2698
2699 if (CC(!vmx_control_verify(vmcs12->pin_based_vm_exec_control,
2700 vmx->nested.msrs.pinbased_ctls_low,
2701 vmx->nested.msrs.pinbased_ctls_high)) ||
2702 CC(!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
2703 vmx->nested.msrs.procbased_ctls_low,
2704 vmx->nested.msrs.procbased_ctls_high)))
2705 return -EINVAL;
2706
2707 if (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
2708 CC(!vmx_control_verify(vmcs12->secondary_vm_exec_control,
2709 vmx->nested.msrs.secondary_ctls_low,
2710 vmx->nested.msrs.secondary_ctls_high)))
2711 return -EINVAL;
2712
2713 if (CC(vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu)) ||
2714 nested_vmx_check_io_bitmap_controls(vcpu, vmcs12) ||
2715 nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12) ||
2716 nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12) ||
2717 nested_vmx_check_apic_access_controls(vcpu, vmcs12) ||
2718 nested_vmx_check_apicv_controls(vcpu, vmcs12) ||
2719 nested_vmx_check_nmi_controls(vmcs12) ||
2720 nested_vmx_check_pml_controls(vcpu, vmcs12) ||
2721 nested_vmx_check_unrestricted_guest_controls(vcpu, vmcs12) ||
2722 nested_vmx_check_mode_based_ept_exec_controls(vcpu, vmcs12) ||
2723 nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12) ||
2724 CC(nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id))
2725 return -EINVAL;
2726
2727 if (!nested_cpu_has_preemption_timer(vmcs12) &&
2728 nested_cpu_has_save_preemption_timer(vmcs12))
2729 return -EINVAL;
2730
2731 if (nested_cpu_has_ept(vmcs12) &&
Olivier Deprez157378f2022-04-04 15:47:50 +02002732 CC(!nested_vmx_check_eptp(vcpu, vmcs12->ept_pointer)))
David Brazdil0f672f62019-12-10 10:32:29 +00002733 return -EINVAL;
2734
2735 if (nested_cpu_has_vmfunc(vmcs12)) {
2736 if (CC(vmcs12->vm_function_control &
2737 ~vmx->nested.msrs.vmfunc_controls))
2738 return -EINVAL;
2739
2740 if (nested_cpu_has_eptp_switching(vmcs12)) {
2741 if (CC(!nested_cpu_has_ept(vmcs12)) ||
2742 CC(!page_address_valid(vcpu, vmcs12->eptp_list_address)))
2743 return -EINVAL;
2744 }
2745 }
2746
2747 return 0;
2748}
2749
2750/*
2751 * Checks related to VM-Exit Control Fields
2752 */
2753static int nested_check_vm_exit_controls(struct kvm_vcpu *vcpu,
2754 struct vmcs12 *vmcs12)
2755{
2756 struct vcpu_vmx *vmx = to_vmx(vcpu);
2757
2758 if (CC(!vmx_control_verify(vmcs12->vm_exit_controls,
2759 vmx->nested.msrs.exit_ctls_low,
2760 vmx->nested.msrs.exit_ctls_high)) ||
2761 CC(nested_vmx_check_exit_msr_switch_controls(vcpu, vmcs12)))
2762 return -EINVAL;
2763
2764 return 0;
2765}
2766
2767/*
2768 * Checks related to VM-Entry Control Fields
2769 */
2770static int nested_check_vm_entry_controls(struct kvm_vcpu *vcpu,
2771 struct vmcs12 *vmcs12)
2772{
2773 struct vcpu_vmx *vmx = to_vmx(vcpu);
2774
2775 if (CC(!vmx_control_verify(vmcs12->vm_entry_controls,
2776 vmx->nested.msrs.entry_ctls_low,
2777 vmx->nested.msrs.entry_ctls_high)))
2778 return -EINVAL;
2779
2780 /*
2781 * From the Intel SDM, volume 3:
2782 * Fields relevant to VM-entry event injection must be set properly.
2783 * These fields are the VM-entry interruption-information field, the
2784 * VM-entry exception error code, and the VM-entry instruction length.
2785 */
2786 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
2787 u32 intr_info = vmcs12->vm_entry_intr_info_field;
2788 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
2789 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
2790 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
2791 bool should_have_error_code;
2792 bool urg = nested_cpu_has2(vmcs12,
2793 SECONDARY_EXEC_UNRESTRICTED_GUEST);
2794 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
2795
2796 /* VM-entry interruption-info field: interruption type */
2797 if (CC(intr_type == INTR_TYPE_RESERVED) ||
2798 CC(intr_type == INTR_TYPE_OTHER_EVENT &&
2799 !nested_cpu_supports_monitor_trap_flag(vcpu)))
2800 return -EINVAL;
2801
2802 /* VM-entry interruption-info field: vector */
2803 if (CC(intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
2804 CC(intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
2805 CC(intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
2806 return -EINVAL;
2807
2808 /* VM-entry interruption-info field: deliver error code */
2809 should_have_error_code =
2810 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
2811 x86_exception_has_error_code(vector);
2812 if (CC(has_error_code != should_have_error_code))
2813 return -EINVAL;
2814
2815 /* VM-entry exception error code */
2816 if (CC(has_error_code &&
2817 vmcs12->vm_entry_exception_error_code & GENMASK(31, 16)))
2818 return -EINVAL;
2819
2820 /* VM-entry interruption-info field: reserved bits */
2821 if (CC(intr_info & INTR_INFO_RESVD_BITS_MASK))
2822 return -EINVAL;
2823
2824 /* VM-entry instruction length */
2825 switch (intr_type) {
2826 case INTR_TYPE_SOFT_EXCEPTION:
2827 case INTR_TYPE_SOFT_INTR:
2828 case INTR_TYPE_PRIV_SW_EXCEPTION:
2829 if (CC(vmcs12->vm_entry_instruction_len > 15) ||
2830 CC(vmcs12->vm_entry_instruction_len == 0 &&
2831 CC(!nested_cpu_has_zero_length_injection(vcpu))))
2832 return -EINVAL;
2833 }
2834 }
2835
2836 if (nested_vmx_check_entry_msr_switch_controls(vcpu, vmcs12))
2837 return -EINVAL;
2838
2839 return 0;
2840}
2841
2842static int nested_vmx_check_controls(struct kvm_vcpu *vcpu,
2843 struct vmcs12 *vmcs12)
2844{
2845 if (nested_check_vm_execution_controls(vcpu, vmcs12) ||
2846 nested_check_vm_exit_controls(vcpu, vmcs12) ||
2847 nested_check_vm_entry_controls(vcpu, vmcs12))
2848 return -EINVAL;
2849
Olivier Deprez157378f2022-04-04 15:47:50 +02002850 if (to_vmx(vcpu)->nested.enlightened_vmcs_enabled)
2851 return nested_evmcs_check_controls(vmcs12);
2852
2853 return 0;
2854}
2855
2856static int nested_vmx_check_address_space_size(struct kvm_vcpu *vcpu,
2857 struct vmcs12 *vmcs12)
2858{
2859#ifdef CONFIG_X86_64
2860 if (CC(!!(vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) !=
2861 !!(vcpu->arch.efer & EFER_LMA)))
2862 return -EINVAL;
2863#endif
David Brazdil0f672f62019-12-10 10:32:29 +00002864 return 0;
2865}
2866
2867static int nested_vmx_check_host_state(struct kvm_vcpu *vcpu,
2868 struct vmcs12 *vmcs12)
2869{
2870 bool ia32e;
2871
2872 if (CC(!nested_host_cr0_valid(vcpu, vmcs12->host_cr0)) ||
2873 CC(!nested_host_cr4_valid(vcpu, vmcs12->host_cr4)) ||
2874 CC(!nested_cr3_valid(vcpu, vmcs12->host_cr3)))
2875 return -EINVAL;
2876
2877 if (CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_esp, vcpu)) ||
2878 CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_eip, vcpu)))
2879 return -EINVAL;
2880
2881 if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) &&
2882 CC(!kvm_pat_valid(vmcs12->host_ia32_pat)))
2883 return -EINVAL;
2884
Olivier Deprez157378f2022-04-04 15:47:50 +02002885 if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) &&
2886 CC(!kvm_valid_perf_global_ctrl(vcpu_to_pmu(vcpu),
2887 vmcs12->host_ia32_perf_global_ctrl)))
2888 return -EINVAL;
2889
David Brazdil0f672f62019-12-10 10:32:29 +00002890#ifdef CONFIG_X86_64
Olivier Deprez157378f2022-04-04 15:47:50 +02002891 ia32e = !!(vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE);
David Brazdil0f672f62019-12-10 10:32:29 +00002892#else
2893 ia32e = false;
2894#endif
2895
2896 if (ia32e) {
Olivier Deprez157378f2022-04-04 15:47:50 +02002897 if (CC(!(vmcs12->host_cr4 & X86_CR4_PAE)))
David Brazdil0f672f62019-12-10 10:32:29 +00002898 return -EINVAL;
2899 } else {
Olivier Deprez157378f2022-04-04 15:47:50 +02002900 if (CC(vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) ||
David Brazdil0f672f62019-12-10 10:32:29 +00002901 CC(vmcs12->host_cr4 & X86_CR4_PCIDE) ||
2902 CC((vmcs12->host_rip) >> 32))
2903 return -EINVAL;
2904 }
2905
2906 if (CC(vmcs12->host_cs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2907 CC(vmcs12->host_ss_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2908 CC(vmcs12->host_ds_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2909 CC(vmcs12->host_es_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2910 CC(vmcs12->host_fs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2911 CC(vmcs12->host_gs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2912 CC(vmcs12->host_tr_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2913 CC(vmcs12->host_cs_selector == 0) ||
2914 CC(vmcs12->host_tr_selector == 0) ||
2915 CC(vmcs12->host_ss_selector == 0 && !ia32e))
2916 return -EINVAL;
2917
David Brazdil0f672f62019-12-10 10:32:29 +00002918 if (CC(is_noncanonical_address(vmcs12->host_fs_base, vcpu)) ||
2919 CC(is_noncanonical_address(vmcs12->host_gs_base, vcpu)) ||
2920 CC(is_noncanonical_address(vmcs12->host_gdtr_base, vcpu)) ||
2921 CC(is_noncanonical_address(vmcs12->host_idtr_base, vcpu)) ||
2922 CC(is_noncanonical_address(vmcs12->host_tr_base, vcpu)) ||
2923 CC(is_noncanonical_address(vmcs12->host_rip, vcpu)))
2924 return -EINVAL;
David Brazdil0f672f62019-12-10 10:32:29 +00002925
2926 /*
2927 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
2928 * IA32_EFER MSR must be 0 in the field for that register. In addition,
2929 * the values of the LMA and LME bits in the field must each be that of
2930 * the host address-space size VM-exit control.
2931 */
2932 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
2933 if (CC(!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer)) ||
2934 CC(ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA)) ||
2935 CC(ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)))
2936 return -EINVAL;
2937 }
2938
2939 return 0;
2940}
2941
2942static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
2943 struct vmcs12 *vmcs12)
2944{
2945 int r = 0;
2946 struct vmcs12 *shadow;
2947 struct kvm_host_map map;
2948
2949 if (vmcs12->vmcs_link_pointer == -1ull)
2950 return 0;
2951
2952 if (CC(!page_address_valid(vcpu, vmcs12->vmcs_link_pointer)))
2953 return -EINVAL;
2954
2955 if (CC(kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->vmcs_link_pointer), &map)))
2956 return -EINVAL;
2957
2958 shadow = map.hva;
2959
2960 if (CC(shadow->hdr.revision_id != VMCS12_REVISION) ||
2961 CC(shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12)))
2962 r = -EINVAL;
2963
2964 kvm_vcpu_unmap(vcpu, &map, false);
2965 return r;
2966}
2967
2968/*
2969 * Checks related to Guest Non-register State
2970 */
2971static int nested_check_guest_non_reg_state(struct vmcs12 *vmcs12)
2972{
2973 if (CC(vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
2974 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT))
2975 return -EINVAL;
2976
2977 return 0;
2978}
2979
2980static int nested_vmx_check_guest_state(struct kvm_vcpu *vcpu,
2981 struct vmcs12 *vmcs12,
Olivier Deprez157378f2022-04-04 15:47:50 +02002982 enum vm_entry_failure_code *entry_failure_code)
David Brazdil0f672f62019-12-10 10:32:29 +00002983{
2984 bool ia32e;
2985
Olivier Deprez157378f2022-04-04 15:47:50 +02002986 *entry_failure_code = ENTRY_FAIL_DEFAULT;
David Brazdil0f672f62019-12-10 10:32:29 +00002987
2988 if (CC(!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0)) ||
2989 CC(!nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4)))
2990 return -EINVAL;
2991
Olivier Deprez157378f2022-04-04 15:47:50 +02002992 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) &&
2993 CC(!kvm_dr7_valid(vmcs12->guest_dr7)))
2994 return -EINVAL;
2995
David Brazdil0f672f62019-12-10 10:32:29 +00002996 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) &&
2997 CC(!kvm_pat_valid(vmcs12->guest_ia32_pat)))
2998 return -EINVAL;
2999
3000 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
Olivier Deprez157378f2022-04-04 15:47:50 +02003001 *entry_failure_code = ENTRY_FAIL_VMCS_LINK_PTR;
David Brazdil0f672f62019-12-10 10:32:29 +00003002 return -EINVAL;
3003 }
3004
Olivier Deprez157378f2022-04-04 15:47:50 +02003005 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) &&
3006 CC(!kvm_valid_perf_global_ctrl(vcpu_to_pmu(vcpu),
3007 vmcs12->guest_ia32_perf_global_ctrl)))
3008 return -EINVAL;
3009
David Brazdil0f672f62019-12-10 10:32:29 +00003010 /*
3011 * If the load IA32_EFER VM-entry control is 1, the following checks
3012 * are performed on the field for the IA32_EFER MSR:
3013 * - Bits reserved in the IA32_EFER MSR must be 0.
3014 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
3015 * the IA-32e mode guest VM-exit control. It must also be identical
3016 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
3017 * CR0.PG) is 1.
3018 */
3019 if (to_vmx(vcpu)->nested.nested_run_pending &&
3020 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
3021 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
3022 if (CC(!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer)) ||
3023 CC(ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA)) ||
3024 CC(((vmcs12->guest_cr0 & X86_CR0_PG) &&
3025 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))))
3026 return -EINVAL;
3027 }
3028
3029 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
3030 (CC(is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu)) ||
3031 CC((vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD))))
3032 return -EINVAL;
3033
3034 if (nested_check_guest_non_reg_state(vmcs12))
3035 return -EINVAL;
3036
3037 return 0;
3038}
3039
3040static int nested_vmx_check_vmentry_hw(struct kvm_vcpu *vcpu)
3041{
3042 struct vcpu_vmx *vmx = to_vmx(vcpu);
3043 unsigned long cr3, cr4;
3044 bool vm_fail;
3045
3046 if (!nested_early_check)
3047 return 0;
3048
3049 if (vmx->msr_autoload.host.nr)
3050 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
3051 if (vmx->msr_autoload.guest.nr)
3052 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
3053
3054 preempt_disable();
3055
3056 vmx_prepare_switch_to_guest(vcpu);
3057
3058 /*
3059 * Induce a consistency check VMExit by clearing bit 1 in GUEST_RFLAGS,
3060 * which is reserved to '1' by hardware. GUEST_RFLAGS is guaranteed to
Olivier Deprez157378f2022-04-04 15:47:50 +02003061 * be written (by prepare_vmcs02()) before the "real" VMEnter, i.e.
David Brazdil0f672f62019-12-10 10:32:29 +00003062 * there is no need to preserve other bits or save/restore the field.
3063 */
3064 vmcs_writel(GUEST_RFLAGS, 0);
3065
3066 cr3 = __get_current_cr3_fast();
3067 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
3068 vmcs_writel(HOST_CR3, cr3);
3069 vmx->loaded_vmcs->host_state.cr3 = cr3;
3070 }
3071
3072 cr4 = cr4_read_shadow();
3073 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
3074 vmcs_writel(HOST_CR4, cr4);
3075 vmx->loaded_vmcs->host_state.cr4 = cr4;
3076 }
3077
3078 asm(
3079 "sub $%c[wordsize], %%" _ASM_SP "\n\t" /* temporarily adjust RSP for CALL */
3080 "cmp %%" _ASM_SP ", %c[host_state_rsp](%[loaded_vmcs]) \n\t"
3081 "je 1f \n\t"
3082 __ex("vmwrite %%" _ASM_SP ", %[HOST_RSP]") "\n\t"
3083 "mov %%" _ASM_SP ", %c[host_state_rsp](%[loaded_vmcs]) \n\t"
3084 "1: \n\t"
3085 "add $%c[wordsize], %%" _ASM_SP "\n\t" /* un-adjust RSP */
3086
3087 /* Check if vmlaunch or vmresume is needed */
3088 "cmpb $0, %c[launched](%[loaded_vmcs])\n\t"
3089
3090 /*
3091 * VMLAUNCH and VMRESUME clear RFLAGS.{CF,ZF} on VM-Exit, set
3092 * RFLAGS.CF on VM-Fail Invalid and set RFLAGS.ZF on VM-Fail
3093 * Valid. vmx_vmenter() directly "returns" RFLAGS, and so the
3094 * results of VM-Enter is captured via CC_{SET,OUT} to vm_fail.
3095 */
3096 "call vmx_vmenter\n\t"
3097
3098 CC_SET(be)
3099 : ASM_CALL_CONSTRAINT, CC_OUT(be) (vm_fail)
3100 : [HOST_RSP]"r"((unsigned long)HOST_RSP),
3101 [loaded_vmcs]"r"(vmx->loaded_vmcs),
3102 [launched]"i"(offsetof(struct loaded_vmcs, launched)),
3103 [host_state_rsp]"i"(offsetof(struct loaded_vmcs, host_state.rsp)),
3104 [wordsize]"i"(sizeof(ulong))
3105 : "memory"
3106 );
3107
3108 if (vmx->msr_autoload.host.nr)
3109 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
3110 if (vmx->msr_autoload.guest.nr)
3111 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
3112
3113 if (vm_fail) {
3114 u32 error = vmcs_read32(VM_INSTRUCTION_ERROR);
3115
3116 preempt_enable();
3117
3118 trace_kvm_nested_vmenter_failed(
3119 "early hardware check VM-instruction error: ", error);
3120 WARN_ON_ONCE(error != VMXERR_ENTRY_INVALID_CONTROL_FIELD);
3121 return 1;
3122 }
3123
3124 /*
3125 * VMExit clears RFLAGS.IF and DR7, even on a consistency check.
3126 */
David Brazdil0f672f62019-12-10 10:32:29 +00003127 if (hw_breakpoint_active())
3128 set_debugreg(__this_cpu_read(cpu_dr7), 7);
Olivier Deprez157378f2022-04-04 15:47:50 +02003129 local_irq_enable();
David Brazdil0f672f62019-12-10 10:32:29 +00003130 preempt_enable();
3131
3132 /*
3133 * A non-failing VMEntry means we somehow entered guest mode with
3134 * an illegal RIP, and that's just the tip of the iceberg. There
3135 * is no telling what memory has been modified or what state has
3136 * been exposed to unknown code. Hitting this all but guarantees
3137 * a (very critical) hardware issue.
3138 */
3139 WARN_ON(!(vmcs_read32(VM_EXIT_REASON) &
3140 VMX_EXIT_REASONS_FAILED_VMENTRY));
3141
3142 return 0;
3143}
3144
Olivier Deprez157378f2022-04-04 15:47:50 +02003145static bool nested_get_evmcs_page(struct kvm_vcpu *vcpu)
3146{
3147 struct vcpu_vmx *vmx = to_vmx(vcpu);
3148
3149 /*
3150 * hv_evmcs may end up being not mapped after migration (when
3151 * L2 was running), map it here to make sure vmcs12 changes are
3152 * properly reflected.
3153 */
3154 if (vmx->nested.enlightened_vmcs_enabled && !vmx->nested.hv_evmcs) {
3155 enum nested_evmptrld_status evmptrld_status =
3156 nested_vmx_handle_enlightened_vmptrld(vcpu, false);
3157
3158 if (evmptrld_status == EVMPTRLD_VMFAIL ||
3159 evmptrld_status == EVMPTRLD_ERROR)
3160 return false;
3161 }
3162
3163 return true;
3164}
David Brazdil0f672f62019-12-10 10:32:29 +00003165
3166static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
3167{
3168 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3169 struct vcpu_vmx *vmx = to_vmx(vcpu);
3170 struct kvm_host_map *map;
3171 struct page *page;
3172 u64 hpa;
3173
3174 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
3175 /*
3176 * Translate L1 physical address to host physical
3177 * address for vmcs02. Keep the page pinned, so this
3178 * physical address remains valid. We keep a reference
3179 * to it so we can release it later.
3180 */
3181 if (vmx->nested.apic_access_page) { /* shouldn't happen */
Olivier Deprez157378f2022-04-04 15:47:50 +02003182 kvm_release_page_clean(vmx->nested.apic_access_page);
David Brazdil0f672f62019-12-10 10:32:29 +00003183 vmx->nested.apic_access_page = NULL;
3184 }
3185 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
3186 if (!is_error_page(page)) {
3187 vmx->nested.apic_access_page = page;
3188 hpa = page_to_phys(vmx->nested.apic_access_page);
3189 vmcs_write64(APIC_ACCESS_ADDR, hpa);
3190 } else {
3191 pr_debug_ratelimited("%s: no backing 'struct page' for APIC-access address in vmcs12\n",
3192 __func__);
3193 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3194 vcpu->run->internal.suberror =
3195 KVM_INTERNAL_ERROR_EMULATION;
3196 vcpu->run->internal.ndata = 0;
3197 return false;
3198 }
3199 }
3200
3201 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
3202 map = &vmx->nested.virtual_apic_map;
3203
3204 if (!kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->virtual_apic_page_addr), map)) {
3205 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, pfn_to_hpa(map->pfn));
3206 } else if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING) &&
3207 nested_cpu_has(vmcs12, CPU_BASED_CR8_STORE_EXITING) &&
3208 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
3209 /*
3210 * The processor will never use the TPR shadow, simply
3211 * clear the bit from the execution control. Such a
3212 * configuration is useless, but it happens in tests.
3213 * For any other configuration, failing the vm entry is
3214 * _not_ what the processor does but it's basically the
3215 * only possibility we have.
3216 */
3217 exec_controls_clearbit(vmx, CPU_BASED_TPR_SHADOW);
3218 } else {
3219 /*
3220 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR to
3221 * force VM-Entry to fail.
3222 */
3223 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
3224 }
3225 }
3226
3227 if (nested_cpu_has_posted_intr(vmcs12)) {
3228 map = &vmx->nested.pi_desc_map;
3229
3230 if (!kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->posted_intr_desc_addr), map)) {
3231 vmx->nested.pi_desc =
3232 (struct pi_desc *)(((void *)map->hva) +
3233 offset_in_page(vmcs12->posted_intr_desc_addr));
3234 vmcs_write64(POSTED_INTR_DESC_ADDR,
3235 pfn_to_hpa(map->pfn) + offset_in_page(vmcs12->posted_intr_desc_addr));
3236 }
3237 }
3238 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
3239 exec_controls_setbit(vmx, CPU_BASED_USE_MSR_BITMAPS);
3240 else
3241 exec_controls_clearbit(vmx, CPU_BASED_USE_MSR_BITMAPS);
Olivier Deprez157378f2022-04-04 15:47:50 +02003242
David Brazdil0f672f62019-12-10 10:32:29 +00003243 return true;
3244}
3245
Olivier Deprez157378f2022-04-04 15:47:50 +02003246static bool vmx_get_nested_state_pages(struct kvm_vcpu *vcpu)
3247{
3248 if (!nested_get_evmcs_page(vcpu)) {
3249 pr_debug_ratelimited("%s: enlightened vmptrld failed\n",
3250 __func__);
3251 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3252 vcpu->run->internal.suberror =
3253 KVM_INTERNAL_ERROR_EMULATION;
3254 vcpu->run->internal.ndata = 0;
3255
3256 return false;
3257 }
3258
3259 if (is_guest_mode(vcpu) && !nested_get_vmcs12_pages(vcpu))
3260 return false;
3261
3262 return true;
3263}
3264
3265static int nested_vmx_write_pml_buffer(struct kvm_vcpu *vcpu, gpa_t gpa)
3266{
3267 struct vmcs12 *vmcs12;
3268 struct vcpu_vmx *vmx = to_vmx(vcpu);
3269 gpa_t dst;
3270
3271 if (WARN_ON_ONCE(!is_guest_mode(vcpu)))
3272 return 0;
3273
3274 if (WARN_ON_ONCE(vmx->nested.pml_full))
3275 return 1;
3276
3277 /*
3278 * Check if PML is enabled for the nested guest. Whether eptp bit 6 is
3279 * set is already checked as part of A/D emulation.
3280 */
3281 vmcs12 = get_vmcs12(vcpu);
3282 if (!nested_cpu_has_pml(vmcs12))
3283 return 0;
3284
3285 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
3286 vmx->nested.pml_full = true;
3287 return 1;
3288 }
3289
3290 gpa &= ~0xFFFull;
3291 dst = vmcs12->pml_address + sizeof(u64) * vmcs12->guest_pml_index;
3292
3293 if (kvm_write_guest_page(vcpu->kvm, gpa_to_gfn(dst), &gpa,
3294 offset_in_page(dst), sizeof(gpa)))
3295 return 0;
3296
3297 vmcs12->guest_pml_index--;
3298
3299 return 0;
3300}
3301
David Brazdil0f672f62019-12-10 10:32:29 +00003302/*
3303 * Intel's VMX Instruction Reference specifies a common set of prerequisites
3304 * for running VMX instructions (except VMXON, whose prerequisites are
3305 * slightly different). It also specifies what exception to inject otherwise.
3306 * Note that many of these exceptions have priority over VM exits, so they
3307 * don't have to be checked again here.
3308 */
3309static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
3310{
3311 if (!to_vmx(vcpu)->nested.vmxon) {
3312 kvm_queue_exception(vcpu, UD_VECTOR);
3313 return 0;
3314 }
3315
3316 if (vmx_get_cpl(vcpu)) {
3317 kvm_inject_gp(vcpu, 0);
3318 return 0;
3319 }
3320
3321 return 1;
3322}
3323
3324static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu)
3325{
3326 u8 rvi = vmx_get_rvi();
3327 u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI);
3328
3329 return ((rvi & 0xf0) > (vppr & 0xf0));
3330}
3331
3332static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
3333 struct vmcs12 *vmcs12);
3334
3335/*
3336 * If from_vmentry is false, this is being called from state restore (either RSM
3337 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
3338 *
3339 * Returns:
Olivier Deprez157378f2022-04-04 15:47:50 +02003340 * NVMX_VMENTRY_SUCCESS: Entered VMX non-root mode
3341 * NVMX_VMENTRY_VMFAIL: Consistency check VMFail
3342 * NVMX_VMENTRY_VMEXIT: Consistency check VMExit
3343 * NVMX_VMENTRY_KVM_INTERNAL_ERROR: KVM internal error
David Brazdil0f672f62019-12-10 10:32:29 +00003344 */
3345enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu,
3346 bool from_vmentry)
3347{
3348 struct vcpu_vmx *vmx = to_vmx(vcpu);
3349 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Olivier Deprez157378f2022-04-04 15:47:50 +02003350 enum vm_entry_failure_code entry_failure_code;
David Brazdil0f672f62019-12-10 10:32:29 +00003351 bool evaluate_pending_interrupts;
Olivier Deprez157378f2022-04-04 15:47:50 +02003352 union vmx_exit_reason exit_reason = {
3353 .basic = EXIT_REASON_INVALID_STATE,
3354 .failed_vmentry = 1,
3355 };
3356 u32 failed_index;
3357
3358 if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu))
3359 kvm_vcpu_flush_tlb_current(vcpu);
David Brazdil0f672f62019-12-10 10:32:29 +00003360
3361 evaluate_pending_interrupts = exec_controls_get(vmx) &
Olivier Deprez157378f2022-04-04 15:47:50 +02003362 (CPU_BASED_INTR_WINDOW_EXITING | CPU_BASED_NMI_WINDOW_EXITING);
David Brazdil0f672f62019-12-10 10:32:29 +00003363 if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu))
3364 evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu);
3365
3366 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
3367 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
3368 if (kvm_mpx_supported() &&
3369 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
3370 vmx->nested.vmcs01_guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
3371
3372 /*
3373 * Overwrite vmcs01.GUEST_CR3 with L1's CR3 if EPT is disabled *and*
3374 * nested early checks are disabled. In the event of a "late" VM-Fail,
3375 * i.e. a VM-Fail detected by hardware but not KVM, KVM must unwind its
3376 * software model to the pre-VMEntry host state. When EPT is disabled,
3377 * GUEST_CR3 holds KVM's shadow CR3, not L1's "real" CR3, which causes
3378 * nested_vmx_restore_host_state() to corrupt vcpu->arch.cr3. Stuffing
3379 * vmcs01.GUEST_CR3 results in the unwind naturally setting arch.cr3 to
3380 * the correct value. Smashing vmcs01.GUEST_CR3 is safe because nested
3381 * VM-Exits, and the unwind, reset KVM's MMU, i.e. vmcs01.GUEST_CR3 is
3382 * guaranteed to be overwritten with a shadow CR3 prior to re-entering
3383 * L1. Don't stuff vmcs01.GUEST_CR3 when using nested early checks as
3384 * KVM modifies vcpu->arch.cr3 if and only if the early hardware checks
3385 * pass, and early VM-Fails do not reset KVM's MMU, i.e. the VM-Fail
3386 * path would need to manually save/restore vmcs01.GUEST_CR3.
3387 */
3388 if (!enable_ept && !nested_early_check)
3389 vmcs_writel(GUEST_CR3, vcpu->arch.cr3);
3390
3391 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
3392
3393 prepare_vmcs02_early(vmx, vmcs12);
3394
3395 if (from_vmentry) {
Olivier Deprez0e641232021-09-23 10:07:05 +02003396 if (unlikely(!nested_get_vmcs12_pages(vcpu))) {
3397 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
David Brazdil0f672f62019-12-10 10:32:29 +00003398 return NVMX_VMENTRY_KVM_INTERNAL_ERROR;
Olivier Deprez0e641232021-09-23 10:07:05 +02003399 }
David Brazdil0f672f62019-12-10 10:32:29 +00003400
3401 if (nested_vmx_check_vmentry_hw(vcpu)) {
3402 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
3403 return NVMX_VMENTRY_VMFAIL;
3404 }
3405
Olivier Deprez157378f2022-04-04 15:47:50 +02003406 if (nested_vmx_check_guest_state(vcpu, vmcs12,
3407 &entry_failure_code)) {
3408 exit_reason.basic = EXIT_REASON_INVALID_STATE;
3409 vmcs12->exit_qualification = entry_failure_code;
David Brazdil0f672f62019-12-10 10:32:29 +00003410 goto vmentry_fail_vmexit;
Olivier Deprez157378f2022-04-04 15:47:50 +02003411 }
David Brazdil0f672f62019-12-10 10:32:29 +00003412 }
3413
3414 enter_guest_mode(vcpu);
Olivier Deprez157378f2022-04-04 15:47:50 +02003415 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING)
David Brazdil0f672f62019-12-10 10:32:29 +00003416 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
3417
Olivier Deprez157378f2022-04-04 15:47:50 +02003418 if (prepare_vmcs02(vcpu, vmcs12, &entry_failure_code)) {
3419 exit_reason.basic = EXIT_REASON_INVALID_STATE;
3420 vmcs12->exit_qualification = entry_failure_code;
David Brazdil0f672f62019-12-10 10:32:29 +00003421 goto vmentry_fail_vmexit_guest_mode;
Olivier Deprez157378f2022-04-04 15:47:50 +02003422 }
David Brazdil0f672f62019-12-10 10:32:29 +00003423
3424 if (from_vmentry) {
Olivier Deprez157378f2022-04-04 15:47:50 +02003425 failed_index = nested_vmx_load_msr(vcpu,
3426 vmcs12->vm_entry_msr_load_addr,
3427 vmcs12->vm_entry_msr_load_count);
3428 if (failed_index) {
3429 exit_reason.basic = EXIT_REASON_MSR_LOAD_FAIL;
3430 vmcs12->exit_qualification = failed_index;
David Brazdil0f672f62019-12-10 10:32:29 +00003431 goto vmentry_fail_vmexit_guest_mode;
Olivier Deprez157378f2022-04-04 15:47:50 +02003432 }
David Brazdil0f672f62019-12-10 10:32:29 +00003433 } else {
3434 /*
3435 * The MMU is not initialized to point at the right entities yet and
3436 * "get pages" would need to read data from the guest (i.e. we will
3437 * need to perform gpa to hpa translation). Request a call
3438 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
3439 * have already been set at vmentry time and should not be reset.
3440 */
Olivier Deprez157378f2022-04-04 15:47:50 +02003441 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
David Brazdil0f672f62019-12-10 10:32:29 +00003442 }
3443
3444 /*
3445 * If L1 had a pending IRQ/NMI until it executed
3446 * VMLAUNCH/VMRESUME which wasn't delivered because it was
3447 * disallowed (e.g. interrupts disabled), L0 needs to
3448 * evaluate if this pending event should cause an exit from L2
3449 * to L1 or delivered directly to L2 (e.g. In case L1 don't
3450 * intercept EXTERNAL_INTERRUPT).
3451 *
3452 * Usually this would be handled by the processor noticing an
3453 * IRQ/NMI window request, or checking RVI during evaluation of
3454 * pending virtual interrupts. However, this setting was done
3455 * on VMCS01 and now VMCS02 is active instead. Thus, we force L0
3456 * to perform pending event evaluation by requesting a KVM_REQ_EVENT.
3457 */
3458 if (unlikely(evaluate_pending_interrupts))
3459 kvm_make_request(KVM_REQ_EVENT, vcpu);
3460
3461 /*
3462 * Do not start the preemption timer hrtimer until after we know
3463 * we are successful, so that only nested_vmx_vmexit needs to cancel
3464 * the timer.
3465 */
3466 vmx->nested.preemption_timer_expired = false;
Olivier Deprez157378f2022-04-04 15:47:50 +02003467 if (nested_cpu_has_preemption_timer(vmcs12)) {
3468 u64 timer_value = vmx_calc_preemption_timer_value(vcpu);
3469 vmx_start_preemption_timer(vcpu, timer_value);
3470 }
David Brazdil0f672f62019-12-10 10:32:29 +00003471
3472 /*
3473 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
3474 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
3475 * returned as far as L1 is concerned. It will only return (and set
3476 * the success flag) when L2 exits (see nested_vmx_vmexit()).
3477 */
3478 return NVMX_VMENTRY_SUCCESS;
3479
3480 /*
3481 * A failed consistency check that leads to a VMExit during L1's
3482 * VMEnter to L2 is a variation of a normal VMexit, as explained in
3483 * 26.7 "VM-entry failures during or after loading guest state".
3484 */
3485vmentry_fail_vmexit_guest_mode:
Olivier Deprez157378f2022-04-04 15:47:50 +02003486 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING)
David Brazdil0f672f62019-12-10 10:32:29 +00003487 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
3488 leave_guest_mode(vcpu);
3489
3490vmentry_fail_vmexit:
3491 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
3492
3493 if (!from_vmentry)
3494 return NVMX_VMENTRY_VMEXIT;
3495
3496 load_vmcs12_host_state(vcpu, vmcs12);
Olivier Deprez157378f2022-04-04 15:47:50 +02003497 vmcs12->vm_exit_reason = exit_reason.full;
David Brazdil0f672f62019-12-10 10:32:29 +00003498 if (enable_shadow_vmcs || vmx->nested.hv_evmcs)
3499 vmx->nested.need_vmcs12_to_shadow_sync = true;
3500 return NVMX_VMENTRY_VMEXIT;
3501}
3502
3503/*
3504 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
3505 * for running an L2 nested guest.
3506 */
3507static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
3508{
3509 struct vmcs12 *vmcs12;
3510 enum nvmx_vmentry_status status;
3511 struct vcpu_vmx *vmx = to_vmx(vcpu);
3512 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Olivier Deprez157378f2022-04-04 15:47:50 +02003513 enum nested_evmptrld_status evmptrld_status;
David Brazdil0f672f62019-12-10 10:32:29 +00003514
3515 if (!nested_vmx_check_permission(vcpu))
3516 return 1;
3517
Olivier Deprez157378f2022-04-04 15:47:50 +02003518 evmptrld_status = nested_vmx_handle_enlightened_vmptrld(vcpu, launch);
3519 if (evmptrld_status == EVMPTRLD_ERROR) {
3520 kvm_queue_exception(vcpu, UD_VECTOR);
David Brazdil0f672f62019-12-10 10:32:29 +00003521 return 1;
Olivier Deprez157378f2022-04-04 15:47:50 +02003522 } else if (CC(evmptrld_status == EVMPTRLD_VMFAIL)) {
3523 return nested_vmx_failInvalid(vcpu);
3524 }
David Brazdil0f672f62019-12-10 10:32:29 +00003525
Olivier Deprez157378f2022-04-04 15:47:50 +02003526 if (CC(!vmx->nested.hv_evmcs && vmx->nested.current_vmptr == -1ull))
David Brazdil0f672f62019-12-10 10:32:29 +00003527 return nested_vmx_failInvalid(vcpu);
3528
3529 vmcs12 = get_vmcs12(vcpu);
3530
3531 /*
3532 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
3533 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
3534 * rather than RFLAGS.ZF, and no error number is stored to the
3535 * VM-instruction error field.
3536 */
Olivier Deprez157378f2022-04-04 15:47:50 +02003537 if (CC(vmcs12->hdr.shadow_vmcs))
David Brazdil0f672f62019-12-10 10:32:29 +00003538 return nested_vmx_failInvalid(vcpu);
3539
3540 if (vmx->nested.hv_evmcs) {
3541 copy_enlightened_to_vmcs12(vmx);
3542 /* Enlightened VMCS doesn't have launch state */
3543 vmcs12->launch_state = !launch;
3544 } else if (enable_shadow_vmcs) {
3545 copy_shadow_to_vmcs12(vmx);
3546 }
3547
3548 /*
3549 * The nested entry process starts with enforcing various prerequisites
3550 * on vmcs12 as required by the Intel SDM, and act appropriately when
3551 * they fail: As the SDM explains, some conditions should cause the
3552 * instruction to fail, while others will cause the instruction to seem
3553 * to succeed, but return an EXIT_REASON_INVALID_STATE.
3554 * To speed up the normal (success) code path, we should avoid checking
3555 * for misconfigurations which will anyway be caught by the processor
3556 * when using the merged vmcs02.
3557 */
Olivier Deprez157378f2022-04-04 15:47:50 +02003558 if (CC(interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS))
3559 return nested_vmx_fail(vcpu, VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
David Brazdil0f672f62019-12-10 10:32:29 +00003560
Olivier Deprez157378f2022-04-04 15:47:50 +02003561 if (CC(vmcs12->launch_state == launch))
3562 return nested_vmx_fail(vcpu,
David Brazdil0f672f62019-12-10 10:32:29 +00003563 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
3564 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
3565
3566 if (nested_vmx_check_controls(vcpu, vmcs12))
Olivier Deprez157378f2022-04-04 15:47:50 +02003567 return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
3568
3569 if (nested_vmx_check_address_space_size(vcpu, vmcs12))
3570 return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
David Brazdil0f672f62019-12-10 10:32:29 +00003571
3572 if (nested_vmx_check_host_state(vcpu, vmcs12))
Olivier Deprez157378f2022-04-04 15:47:50 +02003573 return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
David Brazdil0f672f62019-12-10 10:32:29 +00003574
3575 /*
3576 * We're finally done with prerequisite checking, and can start with
3577 * the nested entry.
3578 */
3579 vmx->nested.nested_run_pending = 1;
Olivier Deprez157378f2022-04-04 15:47:50 +02003580 vmx->nested.has_preemption_timer_deadline = false;
David Brazdil0f672f62019-12-10 10:32:29 +00003581 status = nested_vmx_enter_non_root_mode(vcpu, true);
3582 if (unlikely(status != NVMX_VMENTRY_SUCCESS))
3583 goto vmentry_failed;
3584
Olivier Deprez157378f2022-04-04 15:47:50 +02003585 /* Emulate processing of posted interrupts on VM-Enter. */
3586 if (nested_cpu_has_posted_intr(vmcs12) &&
3587 kvm_apic_has_interrupt(vcpu) == vmx->nested.posted_intr_nv) {
3588 vmx->nested.pi_pending = true;
3589 kvm_make_request(KVM_REQ_EVENT, vcpu);
3590 kvm_apic_clear_irr(vcpu, vmx->nested.posted_intr_nv);
3591 }
3592
David Brazdil0f672f62019-12-10 10:32:29 +00003593 /* Hide L1D cache contents from the nested guest. */
3594 vmx->vcpu.arch.l1tf_flush_l1d = true;
3595
3596 /*
3597 * Must happen outside of nested_vmx_enter_non_root_mode() as it will
3598 * also be used as part of restoring nVMX state for
3599 * snapshot restore (migration).
3600 *
3601 * In this flow, it is assumed that vmcs12 cache was
3602 * trasferred as part of captured nVMX state and should
3603 * therefore not be read from guest memory (which may not
3604 * exist on destination host yet).
3605 */
3606 nested_cache_shadow_vmcs12(vcpu, vmcs12);
3607
3608 /*
3609 * If we're entering a halted L2 vcpu and the L2 vcpu won't be
3610 * awakened by event injection or by an NMI-window VM-exit or
3611 * by an interrupt-window VM-exit, halt the vcpu.
3612 */
3613 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
3614 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) &&
Olivier Deprez157378f2022-04-04 15:47:50 +02003615 !(vmcs12->cpu_based_vm_exec_control & CPU_BASED_NMI_WINDOW_EXITING) &&
3616 !((vmcs12->cpu_based_vm_exec_control & CPU_BASED_INTR_WINDOW_EXITING) &&
David Brazdil0f672f62019-12-10 10:32:29 +00003617 (vmcs12->guest_rflags & X86_EFLAGS_IF))) {
3618 vmx->nested.nested_run_pending = 0;
3619 return kvm_vcpu_halt(vcpu);
3620 }
3621 return 1;
3622
3623vmentry_failed:
3624 vmx->nested.nested_run_pending = 0;
3625 if (status == NVMX_VMENTRY_KVM_INTERNAL_ERROR)
3626 return 0;
3627 if (status == NVMX_VMENTRY_VMEXIT)
3628 return 1;
3629 WARN_ON_ONCE(status != NVMX_VMENTRY_VMFAIL);
Olivier Deprez157378f2022-04-04 15:47:50 +02003630 return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
David Brazdil0f672f62019-12-10 10:32:29 +00003631}
3632
3633/*
3634 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
Olivier Deprez157378f2022-04-04 15:47:50 +02003635 * because L2 may have changed some cr0 bits directly (CR0_GUEST_HOST_MASK).
David Brazdil0f672f62019-12-10 10:32:29 +00003636 * This function returns the new value we should put in vmcs12.guest_cr0.
3637 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
3638 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
3639 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
3640 * didn't trap the bit, because if L1 did, so would L0).
3641 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
3642 * been modified by L2, and L1 knows it. So just leave the old value of
3643 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
3644 * isn't relevant, because if L0 traps this bit it can set it to anything.
3645 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
3646 * changed these bits, and therefore they need to be updated, but L0
3647 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
3648 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
3649 */
3650static inline unsigned long
3651vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
3652{
3653 return
3654 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
3655 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
3656 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
3657 vcpu->arch.cr0_guest_owned_bits));
3658}
3659
3660static inline unsigned long
3661vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
3662{
3663 return
3664 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
3665 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
3666 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
3667 vcpu->arch.cr4_guest_owned_bits));
3668}
3669
3670static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
3671 struct vmcs12 *vmcs12)
3672{
3673 u32 idt_vectoring;
3674 unsigned int nr;
3675
3676 if (vcpu->arch.exception.injected) {
3677 nr = vcpu->arch.exception.nr;
3678 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
3679
3680 if (kvm_exception_is_soft(nr)) {
3681 vmcs12->vm_exit_instruction_len =
3682 vcpu->arch.event_exit_inst_len;
3683 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
3684 } else
3685 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
3686
3687 if (vcpu->arch.exception.has_error_code) {
3688 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
3689 vmcs12->idt_vectoring_error_code =
3690 vcpu->arch.exception.error_code;
3691 }
3692
3693 vmcs12->idt_vectoring_info_field = idt_vectoring;
3694 } else if (vcpu->arch.nmi_injected) {
3695 vmcs12->idt_vectoring_info_field =
3696 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
3697 } else if (vcpu->arch.interrupt.injected) {
3698 nr = vcpu->arch.interrupt.nr;
3699 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
3700
3701 if (vcpu->arch.interrupt.soft) {
3702 idt_vectoring |= INTR_TYPE_SOFT_INTR;
3703 vmcs12->vm_entry_instruction_len =
3704 vcpu->arch.event_exit_inst_len;
3705 } else
3706 idt_vectoring |= INTR_TYPE_EXT_INTR;
3707
3708 vmcs12->idt_vectoring_info_field = idt_vectoring;
3709 }
3710}
3711
3712
Olivier Deprez157378f2022-04-04 15:47:50 +02003713void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
David Brazdil0f672f62019-12-10 10:32:29 +00003714{
3715 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3716 gfn_t gfn;
3717
3718 /*
3719 * Don't need to mark the APIC access page dirty; it is never
3720 * written to by the CPU during APIC virtualization.
3721 */
3722
3723 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
3724 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
3725 kvm_vcpu_mark_page_dirty(vcpu, gfn);
3726 }
3727
3728 if (nested_cpu_has_posted_intr(vmcs12)) {
3729 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
3730 kvm_vcpu_mark_page_dirty(vcpu, gfn);
3731 }
3732}
3733
3734static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
3735{
3736 struct vcpu_vmx *vmx = to_vmx(vcpu);
3737 int max_irr;
3738 void *vapic_page;
3739 u16 status;
3740
3741 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
3742 return;
3743
3744 vmx->nested.pi_pending = false;
3745 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
3746 return;
3747
3748 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
3749 if (max_irr != 256) {
3750 vapic_page = vmx->nested.virtual_apic_map.hva;
3751 if (!vapic_page)
3752 return;
3753
3754 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
3755 vapic_page, &max_irr);
3756 status = vmcs_read16(GUEST_INTR_STATUS);
3757 if ((u8)max_irr > ((u8)status & 0xff)) {
3758 status &= ~0xff;
3759 status |= (u8)max_irr;
3760 vmcs_write16(GUEST_INTR_STATUS, status);
3761 }
3762 }
3763
3764 nested_mark_vmcs12_pages_dirty(vcpu);
3765}
3766
3767static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3768 unsigned long exit_qual)
3769{
3770 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3771 unsigned int nr = vcpu->arch.exception.nr;
3772 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3773
3774 if (vcpu->arch.exception.has_error_code) {
3775 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3776 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3777 }
3778
3779 if (kvm_exception_is_soft(nr))
3780 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3781 else
3782 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3783
3784 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3785 vmx_get_nmi_mask(vcpu))
3786 intr_info |= INTR_INFO_UNBLOCK_NMI;
3787
3788 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3789}
3790
Olivier Deprez157378f2022-04-04 15:47:50 +02003791/*
3792 * Returns true if a debug trap is pending delivery.
3793 *
3794 * In KVM, debug traps bear an exception payload. As such, the class of a #DB
3795 * exception may be inferred from the presence of an exception payload.
3796 */
3797static inline bool vmx_pending_dbg_trap(struct kvm_vcpu *vcpu)
3798{
3799 return vcpu->arch.exception.pending &&
3800 vcpu->arch.exception.nr == DB_VECTOR &&
3801 vcpu->arch.exception.payload;
3802}
3803
3804/*
3805 * Certain VM-exits set the 'pending debug exceptions' field to indicate a
3806 * recognized #DB (data or single-step) that has yet to be delivered. Since KVM
3807 * represents these debug traps with a payload that is said to be compatible
3808 * with the 'pending debug exceptions' field, write the payload to the VMCS
3809 * field if a VM-exit is delivered before the debug trap.
3810 */
3811static void nested_vmx_update_pending_dbg(struct kvm_vcpu *vcpu)
3812{
3813 if (vmx_pending_dbg_trap(vcpu))
3814 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
3815 vcpu->arch.exception.payload);
3816}
3817
3818static bool nested_vmx_preemption_timer_pending(struct kvm_vcpu *vcpu)
3819{
3820 return nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
3821 to_vmx(vcpu)->nested.preemption_timer_expired;
3822}
3823
Olivier Deprez0e641232021-09-23 10:07:05 +02003824static int vmx_check_nested_events(struct kvm_vcpu *vcpu)
David Brazdil0f672f62019-12-10 10:32:29 +00003825{
3826 struct vcpu_vmx *vmx = to_vmx(vcpu);
3827 unsigned long exit_qual;
3828 bool block_nested_events =
3829 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Olivier Deprez157378f2022-04-04 15:47:50 +02003830 bool mtf_pending = vmx->nested.mtf_pending;
David Brazdil0f672f62019-12-10 10:32:29 +00003831 struct kvm_lapic *apic = vcpu->arch.apic;
3832
Olivier Deprez157378f2022-04-04 15:47:50 +02003833 /*
3834 * Clear the MTF state. If a higher priority VM-exit is delivered first,
3835 * this state is discarded.
3836 */
3837 if (!block_nested_events)
3838 vmx->nested.mtf_pending = false;
3839
David Brazdil0f672f62019-12-10 10:32:29 +00003840 if (lapic_in_kernel(vcpu) &&
3841 test_bit(KVM_APIC_INIT, &apic->pending_events)) {
3842 if (block_nested_events)
3843 return -EBUSY;
Olivier Deprez157378f2022-04-04 15:47:50 +02003844 nested_vmx_update_pending_dbg(vcpu);
3845 clear_bit(KVM_APIC_INIT, &apic->pending_events);
David Brazdil0f672f62019-12-10 10:32:29 +00003846 nested_vmx_vmexit(vcpu, EXIT_REASON_INIT_SIGNAL, 0, 0);
3847 return 0;
3848 }
3849
Olivier Deprez157378f2022-04-04 15:47:50 +02003850 /*
3851 * Process any exceptions that are not debug traps before MTF.
3852 */
3853 if (vcpu->arch.exception.pending && !vmx_pending_dbg_trap(vcpu)) {
David Brazdil0f672f62019-12-10 10:32:29 +00003854 if (block_nested_events)
3855 return -EBUSY;
Olivier Deprez157378f2022-04-04 15:47:50 +02003856 if (!nested_vmx_check_exception(vcpu, &exit_qual))
3857 goto no_vmexit;
David Brazdil0f672f62019-12-10 10:32:29 +00003858 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
3859 return 0;
3860 }
3861
Olivier Deprez157378f2022-04-04 15:47:50 +02003862 if (mtf_pending) {
3863 if (block_nested_events)
3864 return -EBUSY;
3865 nested_vmx_update_pending_dbg(vcpu);
3866 nested_vmx_vmexit(vcpu, EXIT_REASON_MONITOR_TRAP_FLAG, 0, 0);
3867 return 0;
3868 }
3869
3870 if (vcpu->arch.exception.pending) {
3871 if (block_nested_events)
3872 return -EBUSY;
3873 if (!nested_vmx_check_exception(vcpu, &exit_qual))
3874 goto no_vmexit;
3875 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
3876 return 0;
3877 }
3878
3879 if (nested_vmx_preemption_timer_pending(vcpu)) {
David Brazdil0f672f62019-12-10 10:32:29 +00003880 if (block_nested_events)
3881 return -EBUSY;
3882 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
3883 return 0;
3884 }
3885
Olivier Deprez157378f2022-04-04 15:47:50 +02003886 if (vcpu->arch.smi_pending && !is_smm(vcpu)) {
David Brazdil0f672f62019-12-10 10:32:29 +00003887 if (block_nested_events)
3888 return -EBUSY;
Olivier Deprez157378f2022-04-04 15:47:50 +02003889 goto no_vmexit;
3890 }
3891
3892 if (vcpu->arch.nmi_pending && !vmx_nmi_blocked(vcpu)) {
3893 if (block_nested_events)
3894 return -EBUSY;
3895 if (!nested_exit_on_nmi(vcpu))
3896 goto no_vmexit;
3897
David Brazdil0f672f62019-12-10 10:32:29 +00003898 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
3899 NMI_VECTOR | INTR_TYPE_NMI_INTR |
3900 INTR_INFO_VALID_MASK, 0);
3901 /*
3902 * The NMI-triggered VM exit counts as injection:
3903 * clear this one and block further NMIs.
3904 */
3905 vcpu->arch.nmi_pending = 0;
3906 vmx_set_nmi_mask(vcpu, true);
3907 return 0;
3908 }
3909
Olivier Deprez157378f2022-04-04 15:47:50 +02003910 if (kvm_cpu_has_interrupt(vcpu) && !vmx_interrupt_blocked(vcpu)) {
David Brazdil0f672f62019-12-10 10:32:29 +00003911 if (block_nested_events)
3912 return -EBUSY;
Olivier Deprez157378f2022-04-04 15:47:50 +02003913 if (!nested_exit_on_intr(vcpu))
3914 goto no_vmexit;
David Brazdil0f672f62019-12-10 10:32:29 +00003915 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
3916 return 0;
3917 }
3918
Olivier Deprez157378f2022-04-04 15:47:50 +02003919no_vmexit:
David Brazdil0f672f62019-12-10 10:32:29 +00003920 vmx_complete_nested_posted_interrupt(vcpu);
3921 return 0;
3922}
3923
3924static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
3925{
3926 ktime_t remaining =
3927 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
3928 u64 value;
3929
3930 if (ktime_to_ns(remaining) <= 0)
3931 return 0;
3932
3933 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
3934 do_div(value, 1000000);
3935 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
3936}
3937
3938static bool is_vmcs12_ext_field(unsigned long field)
3939{
3940 switch (field) {
3941 case GUEST_ES_SELECTOR:
3942 case GUEST_CS_SELECTOR:
3943 case GUEST_SS_SELECTOR:
3944 case GUEST_DS_SELECTOR:
3945 case GUEST_FS_SELECTOR:
3946 case GUEST_GS_SELECTOR:
3947 case GUEST_LDTR_SELECTOR:
3948 case GUEST_TR_SELECTOR:
3949 case GUEST_ES_LIMIT:
3950 case GUEST_CS_LIMIT:
3951 case GUEST_SS_LIMIT:
3952 case GUEST_DS_LIMIT:
3953 case GUEST_FS_LIMIT:
3954 case GUEST_GS_LIMIT:
3955 case GUEST_LDTR_LIMIT:
3956 case GUEST_TR_LIMIT:
3957 case GUEST_GDTR_LIMIT:
3958 case GUEST_IDTR_LIMIT:
3959 case GUEST_ES_AR_BYTES:
3960 case GUEST_DS_AR_BYTES:
3961 case GUEST_FS_AR_BYTES:
3962 case GUEST_GS_AR_BYTES:
3963 case GUEST_LDTR_AR_BYTES:
3964 case GUEST_TR_AR_BYTES:
3965 case GUEST_ES_BASE:
3966 case GUEST_CS_BASE:
3967 case GUEST_SS_BASE:
3968 case GUEST_DS_BASE:
3969 case GUEST_FS_BASE:
3970 case GUEST_GS_BASE:
3971 case GUEST_LDTR_BASE:
3972 case GUEST_TR_BASE:
3973 case GUEST_GDTR_BASE:
3974 case GUEST_IDTR_BASE:
3975 case GUEST_PENDING_DBG_EXCEPTIONS:
3976 case GUEST_BNDCFGS:
3977 return true;
3978 default:
3979 break;
3980 }
3981
3982 return false;
3983}
3984
3985static void sync_vmcs02_to_vmcs12_rare(struct kvm_vcpu *vcpu,
3986 struct vmcs12 *vmcs12)
3987{
3988 struct vcpu_vmx *vmx = to_vmx(vcpu);
3989
3990 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
3991 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
3992 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
3993 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
3994 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
3995 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
3996 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
3997 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
3998 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
3999 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
4000 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
4001 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
4002 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
4003 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
4004 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
4005 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
4006 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
4007 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
4008 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
4009 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
4010 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
4011 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
4012 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
4013 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
4014 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
4015 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
4016 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
4017 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
4018 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
4019 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
4020 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
4021 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
4022 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
4023 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
4024 vmcs12->guest_pending_dbg_exceptions =
4025 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
4026 if (kvm_mpx_supported())
4027 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
4028
4029 vmx->nested.need_sync_vmcs02_to_vmcs12_rare = false;
4030}
4031
4032static void copy_vmcs02_to_vmcs12_rare(struct kvm_vcpu *vcpu,
4033 struct vmcs12 *vmcs12)
4034{
4035 struct vcpu_vmx *vmx = to_vmx(vcpu);
4036 int cpu;
4037
4038 if (!vmx->nested.need_sync_vmcs02_to_vmcs12_rare)
4039 return;
4040
4041
4042 WARN_ON_ONCE(vmx->loaded_vmcs != &vmx->vmcs01);
4043
4044 cpu = get_cpu();
4045 vmx->loaded_vmcs = &vmx->nested.vmcs02;
Olivier Deprez157378f2022-04-04 15:47:50 +02004046 vmx_vcpu_load_vmcs(vcpu, cpu, &vmx->vmcs01);
David Brazdil0f672f62019-12-10 10:32:29 +00004047
4048 sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
4049
4050 vmx->loaded_vmcs = &vmx->vmcs01;
Olivier Deprez157378f2022-04-04 15:47:50 +02004051 vmx_vcpu_load_vmcs(vcpu, cpu, &vmx->nested.vmcs02);
David Brazdil0f672f62019-12-10 10:32:29 +00004052 put_cpu();
4053}
4054
4055/*
4056 * Update the guest state fields of vmcs12 to reflect changes that
4057 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
4058 * VM-entry controls is also updated, since this is really a guest
4059 * state bit.)
4060 */
4061static void sync_vmcs02_to_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
4062{
4063 struct vcpu_vmx *vmx = to_vmx(vcpu);
4064
4065 if (vmx->nested.hv_evmcs)
4066 sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
4067
4068 vmx->nested.need_sync_vmcs02_to_vmcs12_rare = !vmx->nested.hv_evmcs;
4069
4070 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
4071 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
4072
4073 vmcs12->guest_rsp = kvm_rsp_read(vcpu);
4074 vmcs12->guest_rip = kvm_rip_read(vcpu);
4075 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
4076
4077 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
4078 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
4079
David Brazdil0f672f62019-12-10 10:32:29 +00004080 vmcs12->guest_interruptibility_info =
4081 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
4082
4083 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
4084 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
4085 else
4086 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
4087
4088 if (nested_cpu_has_preemption_timer(vmcs12) &&
Olivier Deprez157378f2022-04-04 15:47:50 +02004089 vmcs12->vm_exit_controls & VM_EXIT_SAVE_VMX_PREEMPTION_TIMER &&
4090 !vmx->nested.nested_run_pending)
4091 vmcs12->vmx_preemption_timer_value =
4092 vmx_get_preemption_timer_value(vcpu);
David Brazdil0f672f62019-12-10 10:32:29 +00004093
4094 /*
4095 * In some cases (usually, nested EPT), L2 is allowed to change its
4096 * own CR3 without exiting. If it has changed it, we must keep it.
4097 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
4098 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
4099 *
4100 * Additionally, restore L2's PDPTR to vmcs12.
4101 */
4102 if (enable_ept) {
4103 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
4104 if (nested_cpu_has_ept(vmcs12) && is_pae_paging(vcpu)) {
4105 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
4106 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
4107 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
4108 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
4109 }
4110 }
4111
4112 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
4113
4114 if (nested_cpu_has_vid(vmcs12))
4115 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
4116
4117 vmcs12->vm_entry_controls =
4118 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
4119 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
4120
4121 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS)
4122 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
4123
4124 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
4125 vmcs12->guest_ia32_efer = vcpu->arch.efer;
4126}
4127
4128/*
4129 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
4130 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
4131 * and this function updates it to reflect the changes to the guest state while
4132 * L2 was running (and perhaps made some exits which were handled directly by L0
4133 * without going back to L1), and to reflect the exit reason.
4134 * Note that we do not have to copy here all VMCS fields, just those that
4135 * could have changed by the L2 guest or the exit - i.e., the guest-state and
4136 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
4137 * which already writes to vmcs12 directly.
4138 */
4139static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Olivier Deprez157378f2022-04-04 15:47:50 +02004140 u32 vm_exit_reason, u32 exit_intr_info,
David Brazdil0f672f62019-12-10 10:32:29 +00004141 unsigned long exit_qualification)
4142{
4143 /* update exit information fields: */
Olivier Deprez157378f2022-04-04 15:47:50 +02004144 vmcs12->vm_exit_reason = vm_exit_reason;
David Brazdil0f672f62019-12-10 10:32:29 +00004145 vmcs12->exit_qualification = exit_qualification;
4146 vmcs12->vm_exit_intr_info = exit_intr_info;
4147
4148 vmcs12->idt_vectoring_info_field = 0;
4149 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
4150 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
4151
4152 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
4153 vmcs12->launch_state = 1;
4154
4155 /* vm_entry_intr_info_field is cleared on exit. Emulate this
4156 * instead of reading the real value. */
4157 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
4158
4159 /*
4160 * Transfer the event that L0 or L1 may wanted to inject into
4161 * L2 to IDT_VECTORING_INFO_FIELD.
4162 */
4163 vmcs12_save_pending_event(vcpu, vmcs12);
4164
4165 /*
4166 * According to spec, there's no need to store the guest's
4167 * MSRs if the exit is due to a VM-entry failure that occurs
4168 * during or after loading the guest state. Since this exit
4169 * does not fall in that category, we need to save the MSRs.
4170 */
4171 if (nested_vmx_store_msr(vcpu,
4172 vmcs12->vm_exit_msr_store_addr,
4173 vmcs12->vm_exit_msr_store_count))
4174 nested_vmx_abort(vcpu,
4175 VMX_ABORT_SAVE_GUEST_MSR_FAIL);
4176 }
4177
4178 /*
4179 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
4180 * preserved above and would only end up incorrectly in L1.
4181 */
4182 vcpu->arch.nmi_injected = false;
4183 kvm_clear_exception_queue(vcpu);
4184 kvm_clear_interrupt_queue(vcpu);
4185}
4186
4187/*
4188 * A part of what we need to when the nested L2 guest exits and we want to
4189 * run its L1 parent, is to reset L1's guest state to the host state specified
4190 * in vmcs12.
4191 * This function is to be called not only on normal nested exit, but also on
4192 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
4193 * Failures During or After Loading Guest State").
4194 * This function should be called when the active VMCS is L1's (vmcs01).
4195 */
4196static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
4197 struct vmcs12 *vmcs12)
4198{
Olivier Deprez157378f2022-04-04 15:47:50 +02004199 enum vm_entry_failure_code ignored;
David Brazdil0f672f62019-12-10 10:32:29 +00004200 struct kvm_segment seg;
David Brazdil0f672f62019-12-10 10:32:29 +00004201
4202 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
4203 vcpu->arch.efer = vmcs12->host_ia32_efer;
4204 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
4205 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
4206 else
4207 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
4208 vmx_set_efer(vcpu, vcpu->arch.efer);
4209
4210 kvm_rsp_write(vcpu, vmcs12->host_rsp);
4211 kvm_rip_write(vcpu, vmcs12->host_rip);
4212 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
4213 vmx_set_interrupt_shadow(vcpu, 0);
4214
4215 /*
4216 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
4217 * actually changed, because vmx_set_cr0 refers to efer set above.
4218 *
4219 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
4220 * (KVM doesn't change it);
4221 */
Olivier Deprez157378f2022-04-04 15:47:50 +02004222 vcpu->arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS;
David Brazdil0f672f62019-12-10 10:32:29 +00004223 vmx_set_cr0(vcpu, vmcs12->host_cr0);
4224
4225 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
4226 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
4227 vmx_set_cr4(vcpu, vmcs12->host_cr4);
4228
4229 nested_ept_uninit_mmu_context(vcpu);
4230
4231 /*
4232 * Only PDPTE load can fail as the value of cr3 was checked on entry and
4233 * couldn't have changed.
4234 */
Olivier Deprez157378f2022-04-04 15:47:50 +02004235 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &ignored))
David Brazdil0f672f62019-12-10 10:32:29 +00004236 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
4237
4238 if (!enable_ept)
4239 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
4240
Olivier Deprez157378f2022-04-04 15:47:50 +02004241 nested_vmx_transition_tlb_flush(vcpu, vmcs12, false);
David Brazdil0f672f62019-12-10 10:32:29 +00004242
4243 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
4244 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
4245 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
4246 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
4247 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
4248 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
4249 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
4250
4251 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
4252 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
4253 vmcs_write64(GUEST_BNDCFGS, 0);
4254
4255 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
4256 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
4257 vcpu->arch.pat = vmcs12->host_ia32_pat;
4258 }
4259 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Olivier Deprez157378f2022-04-04 15:47:50 +02004260 WARN_ON_ONCE(kvm_set_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL,
4261 vmcs12->host_ia32_perf_global_ctrl));
David Brazdil0f672f62019-12-10 10:32:29 +00004262
4263 /* Set L1 segment info according to Intel SDM
4264 27.5.2 Loading Host Segment and Descriptor-Table Registers */
4265 seg = (struct kvm_segment) {
4266 .base = 0,
4267 .limit = 0xFFFFFFFF,
4268 .selector = vmcs12->host_cs_selector,
4269 .type = 11,
4270 .present = 1,
4271 .s = 1,
4272 .g = 1
4273 };
4274 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
4275 seg.l = 1;
4276 else
4277 seg.db = 1;
4278 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
4279 seg = (struct kvm_segment) {
4280 .base = 0,
4281 .limit = 0xFFFFFFFF,
4282 .type = 3,
4283 .present = 1,
4284 .s = 1,
4285 .db = 1,
4286 .g = 1
4287 };
4288 seg.selector = vmcs12->host_ds_selector;
4289 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
4290 seg.selector = vmcs12->host_es_selector;
4291 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
4292 seg.selector = vmcs12->host_ss_selector;
4293 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
4294 seg.selector = vmcs12->host_fs_selector;
4295 seg.base = vmcs12->host_fs_base;
4296 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
4297 seg.selector = vmcs12->host_gs_selector;
4298 seg.base = vmcs12->host_gs_base;
4299 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
4300 seg = (struct kvm_segment) {
4301 .base = vmcs12->host_tr_base,
4302 .limit = 0x67,
4303 .selector = vmcs12->host_tr_selector,
4304 .type = 11,
4305 .present = 1
4306 };
4307 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
4308
4309 kvm_set_dr(vcpu, 7, 0x400);
4310 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
4311
4312 if (cpu_has_vmx_msr_bitmap())
4313 vmx_update_msr_bitmap(vcpu);
4314
4315 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
4316 vmcs12->vm_exit_msr_load_count))
4317 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
4318}
4319
4320static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx)
4321{
Olivier Deprez157378f2022-04-04 15:47:50 +02004322 struct vmx_uret_msr *efer_msr;
David Brazdil0f672f62019-12-10 10:32:29 +00004323 unsigned int i;
4324
4325 if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER)
4326 return vmcs_read64(GUEST_IA32_EFER);
4327
4328 if (cpu_has_load_ia32_efer())
4329 return host_efer;
4330
4331 for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) {
4332 if (vmx->msr_autoload.guest.val[i].index == MSR_EFER)
4333 return vmx->msr_autoload.guest.val[i].value;
4334 }
4335
Olivier Deprez157378f2022-04-04 15:47:50 +02004336 efer_msr = vmx_find_uret_msr(vmx, MSR_EFER);
David Brazdil0f672f62019-12-10 10:32:29 +00004337 if (efer_msr)
4338 return efer_msr->data;
4339
4340 return host_efer;
4341}
4342
4343static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu)
4344{
4345 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4346 struct vcpu_vmx *vmx = to_vmx(vcpu);
4347 struct vmx_msr_entry g, h;
4348 gpa_t gpa;
4349 u32 i, j;
4350
4351 vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT);
4352
4353 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
4354 /*
4355 * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set
4356 * as vmcs01.GUEST_DR7 contains a userspace defined value
4357 * and vcpu->arch.dr7 is not squirreled away before the
4358 * nested VMENTER (not worth adding a variable in nested_vmx).
4359 */
4360 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
4361 kvm_set_dr(vcpu, 7, DR7_FIXED_1);
4362 else
4363 WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7)));
4364 }
4365
4366 /*
4367 * Note that calling vmx_set_{efer,cr0,cr4} is important as they
4368 * handle a variety of side effects to KVM's software model.
4369 */
4370 vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx));
4371
Olivier Deprez157378f2022-04-04 15:47:50 +02004372 vcpu->arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS;
David Brazdil0f672f62019-12-10 10:32:29 +00004373 vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW));
4374
4375 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
4376 vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW));
4377
4378 nested_ept_uninit_mmu_context(vcpu);
4379 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
Olivier Deprez157378f2022-04-04 15:47:50 +02004380 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
David Brazdil0f672f62019-12-10 10:32:29 +00004381
4382 /*
4383 * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs
4384 * from vmcs01 (if necessary). The PDPTRs are not loaded on
4385 * VMFail, like everything else we just need to ensure our
4386 * software model is up-to-date.
4387 */
Olivier Deprez157378f2022-04-04 15:47:50 +02004388 if (enable_ept && is_pae_paging(vcpu))
David Brazdil0f672f62019-12-10 10:32:29 +00004389 ept_save_pdptrs(vcpu);
4390
4391 kvm_mmu_reset_context(vcpu);
4392
4393 if (cpu_has_vmx_msr_bitmap())
4394 vmx_update_msr_bitmap(vcpu);
4395
4396 /*
4397 * This nasty bit of open coding is a compromise between blindly
4398 * loading L1's MSRs using the exit load lists (incorrect emulation
4399 * of VMFail), leaving the nested VM's MSRs in the software model
4400 * (incorrect behavior) and snapshotting the modified MSRs (too
4401 * expensive since the lists are unbound by hardware). For each
4402 * MSR that was (prematurely) loaded from the nested VMEntry load
4403 * list, reload it from the exit load list if it exists and differs
4404 * from the guest value. The intent is to stuff host state as
4405 * silently as possible, not to fully process the exit load list.
4406 */
4407 for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) {
4408 gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g));
4409 if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) {
4410 pr_debug_ratelimited(
4411 "%s read MSR index failed (%u, 0x%08llx)\n",
4412 __func__, i, gpa);
4413 goto vmabort;
4414 }
4415
4416 for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) {
4417 gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h));
4418 if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) {
4419 pr_debug_ratelimited(
4420 "%s read MSR failed (%u, 0x%08llx)\n",
4421 __func__, j, gpa);
4422 goto vmabort;
4423 }
4424 if (h.index != g.index)
4425 continue;
4426 if (h.value == g.value)
4427 break;
4428
4429 if (nested_vmx_load_msr_check(vcpu, &h)) {
4430 pr_debug_ratelimited(
4431 "%s check failed (%u, 0x%x, 0x%x)\n",
4432 __func__, j, h.index, h.reserved);
4433 goto vmabort;
4434 }
4435
4436 if (kvm_set_msr(vcpu, h.index, h.value)) {
4437 pr_debug_ratelimited(
4438 "%s WRMSR failed (%u, 0x%x, 0x%llx)\n",
4439 __func__, j, h.index, h.value);
4440 goto vmabort;
4441 }
4442 }
4443 }
4444
4445 return;
4446
4447vmabort:
4448 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
4449}
4450
4451/*
4452 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
4453 * and modify vmcs12 to make it see what it would expect to see there if
4454 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
4455 */
Olivier Deprez157378f2022-04-04 15:47:50 +02004456void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason,
David Brazdil0f672f62019-12-10 10:32:29 +00004457 u32 exit_intr_info, unsigned long exit_qualification)
4458{
4459 struct vcpu_vmx *vmx = to_vmx(vcpu);
4460 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4461
4462 /* trying to cancel vmlaunch/vmresume is a bug */
4463 WARN_ON_ONCE(vmx->nested.nested_run_pending);
4464
Olivier Deprez157378f2022-04-04 15:47:50 +02004465 if (kvm_check_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu)) {
4466 /*
4467 * KVM_REQ_GET_NESTED_STATE_PAGES is also used to map
4468 * Enlightened VMCS after migration and we still need to
4469 * do that when something is forcing L2->L1 exit prior to
4470 * the first L2 run.
4471 */
4472 (void)nested_get_evmcs_page(vcpu);
4473 }
4474
4475 /* Service the TLB flush request for L2 before switching to L1. */
4476 if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu))
4477 kvm_vcpu_flush_tlb_current(vcpu);
4478
4479 /*
4480 * VCPU_EXREG_PDPTR will be clobbered in arch/x86/kvm/vmx/vmx.h between
4481 * now and the new vmentry. Ensure that the VMCS02 PDPTR fields are
4482 * up-to-date before switching to L1.
4483 */
4484 if (enable_ept && is_pae_paging(vcpu))
4485 vmx_ept_load_pdptrs(vcpu);
4486
David Brazdil0f672f62019-12-10 10:32:29 +00004487 leave_guest_mode(vcpu);
4488
4489 if (nested_cpu_has_preemption_timer(vmcs12))
4490 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
4491
Olivier Deprez157378f2022-04-04 15:47:50 +02004492 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING)
David Brazdil0f672f62019-12-10 10:32:29 +00004493 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
4494
4495 if (likely(!vmx->fail)) {
4496 sync_vmcs02_to_vmcs12(vcpu, vmcs12);
4497
Olivier Deprez157378f2022-04-04 15:47:50 +02004498 if (vm_exit_reason != -1)
4499 prepare_vmcs12(vcpu, vmcs12, vm_exit_reason,
4500 exit_intr_info, exit_qualification);
David Brazdil0f672f62019-12-10 10:32:29 +00004501
4502 /*
4503 * Must happen outside of sync_vmcs02_to_vmcs12() as it will
4504 * also be used to capture vmcs12 cache as part of
4505 * capturing nVMX state for snapshot (migration).
4506 *
4507 * Otherwise, this flush will dirty guest memory at a
4508 * point it is already assumed by user-space to be
4509 * immutable.
4510 */
4511 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
4512 } else {
4513 /*
4514 * The only expected VM-instruction error is "VM entry with
4515 * invalid control field(s)." Anything else indicates a
4516 * problem with L0. And we should never get here with a
4517 * VMFail of any type if early consistency checks are enabled.
4518 */
4519 WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) !=
4520 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
4521 WARN_ON_ONCE(nested_early_check);
4522 }
4523
4524 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
4525
4526 /* Update any VMCS fields that might have changed while L2 ran */
4527 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
4528 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
4529 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Olivier Deprez157378f2022-04-04 15:47:50 +02004530 if (vmx->nested.l1_tpr_threshold != -1)
4531 vmcs_write32(TPR_THRESHOLD, vmx->nested.l1_tpr_threshold);
David Brazdil0f672f62019-12-10 10:32:29 +00004532
4533 if (kvm_has_tsc_control)
4534 decache_tsc_multiplier(vmx);
4535
4536 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
4537 vmx->nested.change_vmcs01_virtual_apic_mode = false;
4538 vmx_set_virtual_apic_mode(vcpu);
David Brazdil0f672f62019-12-10 10:32:29 +00004539 }
4540
4541 /* Unpin physical memory we referred to in vmcs02 */
4542 if (vmx->nested.apic_access_page) {
Olivier Deprez157378f2022-04-04 15:47:50 +02004543 kvm_release_page_clean(vmx->nested.apic_access_page);
David Brazdil0f672f62019-12-10 10:32:29 +00004544 vmx->nested.apic_access_page = NULL;
4545 }
4546 kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map, true);
4547 kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map, true);
4548 vmx->nested.pi_desc = NULL;
4549
Olivier Deprez157378f2022-04-04 15:47:50 +02004550 if (vmx->nested.reload_vmcs01_apic_access_page) {
4551 vmx->nested.reload_vmcs01_apic_access_page = false;
4552 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
4553 }
David Brazdil0f672f62019-12-10 10:32:29 +00004554
Olivier Deprez157378f2022-04-04 15:47:50 +02004555 if ((vm_exit_reason != -1) &&
4556 (enable_shadow_vmcs || vmx->nested.hv_evmcs))
David Brazdil0f672f62019-12-10 10:32:29 +00004557 vmx->nested.need_vmcs12_to_shadow_sync = true;
4558
4559 /* in case we halted in L2 */
4560 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
4561
4562 if (likely(!vmx->fail)) {
Olivier Deprez157378f2022-04-04 15:47:50 +02004563 if ((u16)vm_exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
Olivier Deprez0e641232021-09-23 10:07:05 +02004564 nested_exit_intr_ack_set(vcpu)) {
David Brazdil0f672f62019-12-10 10:32:29 +00004565 int irq = kvm_cpu_get_interrupt(vcpu);
4566 WARN_ON(irq < 0);
4567 vmcs12->vm_exit_intr_info = irq |
4568 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
4569 }
4570
Olivier Deprez157378f2022-04-04 15:47:50 +02004571 if (vm_exit_reason != -1)
David Brazdil0f672f62019-12-10 10:32:29 +00004572 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
4573 vmcs12->exit_qualification,
4574 vmcs12->idt_vectoring_info_field,
4575 vmcs12->vm_exit_intr_info,
4576 vmcs12->vm_exit_intr_error_code,
4577 KVM_ISA_VMX);
4578
4579 load_vmcs12_host_state(vcpu, vmcs12);
4580
4581 return;
4582 }
4583
4584 /*
4585 * After an early L2 VM-entry failure, we're now back
4586 * in L1 which thinks it just finished a VMLAUNCH or
4587 * VMRESUME instruction, so we need to set the failure
4588 * flag and the VM-instruction error field of the VMCS
4589 * accordingly, and skip the emulated instruction.
4590 */
Olivier Deprez157378f2022-04-04 15:47:50 +02004591 (void)nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
David Brazdil0f672f62019-12-10 10:32:29 +00004592
4593 /*
4594 * Restore L1's host state to KVM's software model. We're here
4595 * because a consistency check was caught by hardware, which
4596 * means some amount of guest state has been propagated to KVM's
4597 * model and needs to be unwound to the host's state.
4598 */
4599 nested_vmx_restore_host_state(vcpu);
4600
4601 vmx->fail = 0;
4602}
4603
4604/*
4605 * Decode the memory-address operand of a vmx instruction, as recorded on an
4606 * exit caused by such an instruction (run by a guest hypervisor).
4607 * On success, returns 0. When the operand is invalid, returns 1 and throws
Olivier Deprez157378f2022-04-04 15:47:50 +02004608 * #UD, #GP, or #SS.
David Brazdil0f672f62019-12-10 10:32:29 +00004609 */
4610int get_vmx_mem_address(struct kvm_vcpu *vcpu, unsigned long exit_qualification,
4611 u32 vmx_instruction_info, bool wr, int len, gva_t *ret)
4612{
4613 gva_t off;
4614 bool exn;
4615 struct kvm_segment s;
4616
4617 /*
4618 * According to Vol. 3B, "Information for VM Exits Due to Instruction
4619 * Execution", on an exit, vmx_instruction_info holds most of the
4620 * addressing components of the operand. Only the displacement part
4621 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
4622 * For how an actual address is calculated from all these components,
4623 * refer to Vol. 1, "Operand Addressing".
4624 */
4625 int scaling = vmx_instruction_info & 3;
4626 int addr_size = (vmx_instruction_info >> 7) & 7;
4627 bool is_reg = vmx_instruction_info & (1u << 10);
4628 int seg_reg = (vmx_instruction_info >> 15) & 7;
4629 int index_reg = (vmx_instruction_info >> 18) & 0xf;
4630 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
4631 int base_reg = (vmx_instruction_info >> 23) & 0xf;
4632 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
4633
4634 if (is_reg) {
4635 kvm_queue_exception(vcpu, UD_VECTOR);
4636 return 1;
4637 }
4638
4639 /* Addr = segment_base + offset */
4640 /* offset = base + [index * scale] + displacement */
4641 off = exit_qualification; /* holds the displacement */
4642 if (addr_size == 1)
4643 off = (gva_t)sign_extend64(off, 31);
4644 else if (addr_size == 0)
4645 off = (gva_t)sign_extend64(off, 15);
4646 if (base_is_valid)
Olivier Deprez157378f2022-04-04 15:47:50 +02004647 off += kvm_register_readl(vcpu, base_reg);
David Brazdil0f672f62019-12-10 10:32:29 +00004648 if (index_is_valid)
Olivier Deprez157378f2022-04-04 15:47:50 +02004649 off += kvm_register_readl(vcpu, index_reg) << scaling;
David Brazdil0f672f62019-12-10 10:32:29 +00004650 vmx_get_segment(vcpu, &s, seg_reg);
4651
4652 /*
4653 * The effective address, i.e. @off, of a memory operand is truncated
4654 * based on the address size of the instruction. Note that this is
4655 * the *effective address*, i.e. the address prior to accounting for
4656 * the segment's base.
4657 */
4658 if (addr_size == 1) /* 32 bit */
4659 off &= 0xffffffff;
4660 else if (addr_size == 0) /* 16 bit */
4661 off &= 0xffff;
4662
4663 /* Checks for #GP/#SS exceptions. */
4664 exn = false;
4665 if (is_long_mode(vcpu)) {
4666 /*
4667 * The virtual/linear address is never truncated in 64-bit
4668 * mode, e.g. a 32-bit address size can yield a 64-bit virtual
4669 * address when using FS/GS with a non-zero base.
4670 */
4671 if (seg_reg == VCPU_SREG_FS || seg_reg == VCPU_SREG_GS)
4672 *ret = s.base + off;
4673 else
4674 *ret = off;
4675
4676 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
4677 * non-canonical form. This is the only check on the memory
4678 * destination for long mode!
4679 */
4680 exn = is_noncanonical_address(*ret, vcpu);
4681 } else {
4682 /*
4683 * When not in long mode, the virtual/linear address is
4684 * unconditionally truncated to 32 bits regardless of the
4685 * address size.
4686 */
4687 *ret = (s.base + off) & 0xffffffff;
4688
4689 /* Protected mode: apply checks for segment validity in the
4690 * following order:
4691 * - segment type check (#GP(0) may be thrown)
4692 * - usability check (#GP(0)/#SS(0))
4693 * - limit check (#GP(0)/#SS(0))
4694 */
4695 if (wr)
4696 /* #GP(0) if the destination operand is located in a
4697 * read-only data segment or any code segment.
4698 */
4699 exn = ((s.type & 0xa) == 0 || (s.type & 8));
4700 else
4701 /* #GP(0) if the source operand is located in an
4702 * execute-only code segment
4703 */
4704 exn = ((s.type & 0xa) == 8);
4705 if (exn) {
4706 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
4707 return 1;
4708 }
4709 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
4710 */
4711 exn = (s.unusable != 0);
4712
4713 /*
4714 * Protected mode: #GP(0)/#SS(0) if the memory operand is
4715 * outside the segment limit. All CPUs that support VMX ignore
4716 * limit checks for flat segments, i.e. segments with base==0,
4717 * limit==0xffffffff and of type expand-up data or code.
4718 */
4719 if (!(s.base == 0 && s.limit == 0xffffffff &&
4720 ((s.type & 8) || !(s.type & 4))))
4721 exn = exn || ((u64)off + len - 1 > s.limit);
4722 }
4723 if (exn) {
4724 kvm_queue_exception_e(vcpu,
4725 seg_reg == VCPU_SREG_SS ?
4726 SS_VECTOR : GP_VECTOR,
4727 0);
4728 return 1;
4729 }
4730
4731 return 0;
4732}
4733
Olivier Deprez157378f2022-04-04 15:47:50 +02004734void nested_vmx_pmu_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
4735{
4736 struct vcpu_vmx *vmx;
4737
4738 if (!nested_vmx_allowed(vcpu))
4739 return;
4740
4741 vmx = to_vmx(vcpu);
4742 if (kvm_x86_ops.pmu_ops->is_valid_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL)) {
4743 vmx->nested.msrs.entry_ctls_high |=
4744 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
4745 vmx->nested.msrs.exit_ctls_high |=
4746 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
4747 } else {
4748 vmx->nested.msrs.entry_ctls_high &=
4749 ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
4750 vmx->nested.msrs.exit_ctls_high &=
4751 ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
4752 }
4753}
4754
4755static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer,
4756 int *ret)
David Brazdil0f672f62019-12-10 10:32:29 +00004757{
4758 gva_t gva;
4759 struct x86_exception e;
Olivier Deprez157378f2022-04-04 15:47:50 +02004760 int r;
David Brazdil0f672f62019-12-10 10:32:29 +00004761
Olivier Deprez157378f2022-04-04 15:47:50 +02004762 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
David Brazdil0f672f62019-12-10 10:32:29 +00004763 vmcs_read32(VMX_INSTRUCTION_INFO), false,
Olivier Deprez157378f2022-04-04 15:47:50 +02004764 sizeof(*vmpointer), &gva)) {
4765 *ret = 1;
4766 return -EINVAL;
4767 }
David Brazdil0f672f62019-12-10 10:32:29 +00004768
Olivier Deprez157378f2022-04-04 15:47:50 +02004769 r = kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e);
4770 if (r != X86EMUL_CONTINUE) {
4771 *ret = kvm_handle_memory_failure(vcpu, r, &e);
4772 return -EINVAL;
David Brazdil0f672f62019-12-10 10:32:29 +00004773 }
4774
4775 return 0;
4776}
4777
4778/*
4779 * Allocate a shadow VMCS and associate it with the currently loaded
4780 * VMCS, unless such a shadow VMCS already exists. The newly allocated
4781 * VMCS is also VMCLEARed, so that it is ready for use.
4782 */
4783static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
4784{
4785 struct vcpu_vmx *vmx = to_vmx(vcpu);
4786 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
4787
4788 /*
4789 * We should allocate a shadow vmcs for vmcs01 only when L1
4790 * executes VMXON and free it when L1 executes VMXOFF.
4791 * As it is invalid to execute VMXON twice, we shouldn't reach
4792 * here when vmcs01 already have an allocated shadow vmcs.
4793 */
4794 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
4795
4796 if (!loaded_vmcs->shadow_vmcs) {
4797 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
4798 if (loaded_vmcs->shadow_vmcs)
4799 vmcs_clear(loaded_vmcs->shadow_vmcs);
4800 }
4801 return loaded_vmcs->shadow_vmcs;
4802}
4803
4804static int enter_vmx_operation(struct kvm_vcpu *vcpu)
4805{
4806 struct vcpu_vmx *vmx = to_vmx(vcpu);
4807 int r;
4808
4809 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
4810 if (r < 0)
4811 goto out_vmcs02;
4812
4813 vmx->nested.cached_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT);
4814 if (!vmx->nested.cached_vmcs12)
4815 goto out_cached_vmcs12;
4816
4817 vmx->nested.cached_shadow_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT);
4818 if (!vmx->nested.cached_shadow_vmcs12)
4819 goto out_cached_shadow_vmcs12;
4820
4821 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
4822 goto out_shadow_vmcs;
4823
4824 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
Olivier Deprez157378f2022-04-04 15:47:50 +02004825 HRTIMER_MODE_ABS_PINNED);
David Brazdil0f672f62019-12-10 10:32:29 +00004826 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
4827
4828 vmx->nested.vpid02 = allocate_vpid();
4829
4830 vmx->nested.vmcs02_initialized = false;
4831 vmx->nested.vmxon = true;
4832
Olivier Deprez157378f2022-04-04 15:47:50 +02004833 if (vmx_pt_mode_is_host_guest()) {
David Brazdil0f672f62019-12-10 10:32:29 +00004834 vmx->pt_desc.guest.ctl = 0;
Olivier Deprez157378f2022-04-04 15:47:50 +02004835 pt_update_intercept_for_msr(vcpu);
David Brazdil0f672f62019-12-10 10:32:29 +00004836 }
4837
4838 return 0;
4839
4840out_shadow_vmcs:
4841 kfree(vmx->nested.cached_shadow_vmcs12);
4842
4843out_cached_shadow_vmcs12:
4844 kfree(vmx->nested.cached_vmcs12);
4845
4846out_cached_vmcs12:
4847 free_loaded_vmcs(&vmx->nested.vmcs02);
4848
4849out_vmcs02:
4850 return -ENOMEM;
4851}
4852
4853/*
4854 * Emulate the VMXON instruction.
4855 * Currently, we just remember that VMX is active, and do not save or even
4856 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
4857 * do not currently need to store anything in that guest-allocated memory
4858 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
4859 * argument is different from the VMXON pointer (which the spec says they do).
4860 */
4861static int handle_vmon(struct kvm_vcpu *vcpu)
4862{
4863 int ret;
4864 gpa_t vmptr;
4865 uint32_t revision;
4866 struct vcpu_vmx *vmx = to_vmx(vcpu);
Olivier Deprez157378f2022-04-04 15:47:50 +02004867 const u64 VMXON_NEEDED_FEATURES = FEAT_CTL_LOCKED
4868 | FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX;
David Brazdil0f672f62019-12-10 10:32:29 +00004869
4870 /*
4871 * The Intel VMX Instruction Reference lists a bunch of bits that are
4872 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
4873 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
4874 * Otherwise, we should fail with #UD. But most faulting conditions
4875 * have already been checked by hardware, prior to the VM-exit for
4876 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
4877 * that bit set to 1 in non-root mode.
4878 */
4879 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
4880 kvm_queue_exception(vcpu, UD_VECTOR);
4881 return 1;
4882 }
4883
4884 /* CPL=0 must be checked manually. */
4885 if (vmx_get_cpl(vcpu)) {
4886 kvm_inject_gp(vcpu, 0);
4887 return 1;
4888 }
4889
4890 if (vmx->nested.vmxon)
Olivier Deprez157378f2022-04-04 15:47:50 +02004891 return nested_vmx_fail(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
David Brazdil0f672f62019-12-10 10:32:29 +00004892
4893 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
4894 != VMXON_NEEDED_FEATURES) {
4895 kvm_inject_gp(vcpu, 0);
4896 return 1;
4897 }
4898
Olivier Deprez157378f2022-04-04 15:47:50 +02004899 if (nested_vmx_get_vmptr(vcpu, &vmptr, &ret))
4900 return ret;
David Brazdil0f672f62019-12-10 10:32:29 +00004901
4902 /*
4903 * SDM 3: 24.11.5
4904 * The first 4 bytes of VMXON region contain the supported
4905 * VMCS revision identifier
4906 *
4907 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
4908 * which replaces physical address width with 32
4909 */
4910 if (!page_address_valid(vcpu, vmptr))
4911 return nested_vmx_failInvalid(vcpu);
4912
4913 if (kvm_read_guest(vcpu->kvm, vmptr, &revision, sizeof(revision)) ||
4914 revision != VMCS12_REVISION)
4915 return nested_vmx_failInvalid(vcpu);
4916
4917 vmx->nested.vmxon_ptr = vmptr;
4918 ret = enter_vmx_operation(vcpu);
4919 if (ret)
4920 return ret;
4921
4922 return nested_vmx_succeed(vcpu);
4923}
4924
4925static inline void nested_release_vmcs12(struct kvm_vcpu *vcpu)
4926{
4927 struct vcpu_vmx *vmx = to_vmx(vcpu);
4928
4929 if (vmx->nested.current_vmptr == -1ull)
4930 return;
4931
4932 copy_vmcs02_to_vmcs12_rare(vcpu, get_vmcs12(vcpu));
4933
4934 if (enable_shadow_vmcs) {
4935 /* copy to memory all shadowed fields in case
4936 they were modified */
4937 copy_shadow_to_vmcs12(vmx);
4938 vmx_disable_shadow_vmcs(vmx);
4939 }
4940 vmx->nested.posted_intr_nv = -1;
4941
4942 /* Flush VMCS12 to guest memory */
4943 kvm_vcpu_write_guest_page(vcpu,
4944 vmx->nested.current_vmptr >> PAGE_SHIFT,
4945 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
4946
4947 kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
4948
4949 vmx->nested.current_vmptr = -1ull;
4950}
4951
4952/* Emulate the VMXOFF instruction */
4953static int handle_vmoff(struct kvm_vcpu *vcpu)
4954{
4955 if (!nested_vmx_check_permission(vcpu))
4956 return 1;
4957
4958 free_nested(vcpu);
4959
4960 /* Process a latched INIT during time CPU was in VMX operation */
4961 kvm_make_request(KVM_REQ_EVENT, vcpu);
4962
4963 return nested_vmx_succeed(vcpu);
4964}
4965
4966/* Emulate the VMCLEAR instruction */
4967static int handle_vmclear(struct kvm_vcpu *vcpu)
4968{
4969 struct vcpu_vmx *vmx = to_vmx(vcpu);
4970 u32 zero = 0;
4971 gpa_t vmptr;
4972 u64 evmcs_gpa;
Olivier Deprez157378f2022-04-04 15:47:50 +02004973 int r;
David Brazdil0f672f62019-12-10 10:32:29 +00004974
4975 if (!nested_vmx_check_permission(vcpu))
4976 return 1;
4977
Olivier Deprez157378f2022-04-04 15:47:50 +02004978 if (nested_vmx_get_vmptr(vcpu, &vmptr, &r))
4979 return r;
David Brazdil0f672f62019-12-10 10:32:29 +00004980
4981 if (!page_address_valid(vcpu, vmptr))
Olivier Deprez157378f2022-04-04 15:47:50 +02004982 return nested_vmx_fail(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
David Brazdil0f672f62019-12-10 10:32:29 +00004983
4984 if (vmptr == vmx->nested.vmxon_ptr)
Olivier Deprez157378f2022-04-04 15:47:50 +02004985 return nested_vmx_fail(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
David Brazdil0f672f62019-12-10 10:32:29 +00004986
4987 /*
4988 * When Enlightened VMEntry is enabled on the calling CPU we treat
4989 * memory area pointer by vmptr as Enlightened VMCS (as there's no good
4990 * way to distinguish it from VMCS12) and we must not corrupt it by
4991 * writing to the non-existent 'launch_state' field. The area doesn't
4992 * have to be the currently active EVMCS on the calling CPU and there's
4993 * nothing KVM has to do to transition it from 'active' to 'non-active'
4994 * state. It is possible that the area will stay mapped as
4995 * vmx->nested.hv_evmcs but this shouldn't be a problem.
4996 */
4997 if (likely(!vmx->nested.enlightened_vmcs_enabled ||
4998 !nested_enlightened_vmentry(vcpu, &evmcs_gpa))) {
4999 if (vmptr == vmx->nested.current_vmptr)
5000 nested_release_vmcs12(vcpu);
5001
5002 kvm_vcpu_write_guest(vcpu,
5003 vmptr + offsetof(struct vmcs12,
5004 launch_state),
5005 &zero, sizeof(zero));
5006 }
5007
5008 return nested_vmx_succeed(vcpu);
5009}
5010
David Brazdil0f672f62019-12-10 10:32:29 +00005011/* Emulate the VMLAUNCH instruction */
5012static int handle_vmlaunch(struct kvm_vcpu *vcpu)
5013{
5014 return nested_vmx_run(vcpu, true);
5015}
5016
5017/* Emulate the VMRESUME instruction */
5018static int handle_vmresume(struct kvm_vcpu *vcpu)
5019{
5020
5021 return nested_vmx_run(vcpu, false);
5022}
5023
5024static int handle_vmread(struct kvm_vcpu *vcpu)
5025{
Olivier Deprez0e641232021-09-23 10:07:05 +02005026 struct vmcs12 *vmcs12 = is_guest_mode(vcpu) ? get_shadow_vmcs12(vcpu)
5027 : get_vmcs12(vcpu);
Olivier Deprez157378f2022-04-04 15:47:50 +02005028 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
5029 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5030 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Brazdil0f672f62019-12-10 10:32:29 +00005031 struct x86_exception e;
Olivier Deprez157378f2022-04-04 15:47:50 +02005032 unsigned long field;
5033 u64 value;
5034 gva_t gva = 0;
David Brazdil0f672f62019-12-10 10:32:29 +00005035 short offset;
Olivier Deprez157378f2022-04-04 15:47:50 +02005036 int len, r;
David Brazdil0f672f62019-12-10 10:32:29 +00005037
5038 if (!nested_vmx_check_permission(vcpu))
5039 return 1;
5040
Olivier Deprez0e641232021-09-23 10:07:05 +02005041 /*
5042 * In VMX non-root operation, when the VMCS-link pointer is -1ull,
5043 * any VMREAD sets the ALU flags for VMfailInvalid.
5044 */
5045 if (vmx->nested.current_vmptr == -1ull ||
5046 (is_guest_mode(vcpu) &&
5047 get_vmcs12(vcpu)->vmcs_link_pointer == -1ull))
David Brazdil0f672f62019-12-10 10:32:29 +00005048 return nested_vmx_failInvalid(vcpu);
5049
David Brazdil0f672f62019-12-10 10:32:29 +00005050 /* Decode instruction info and find the field to read */
Olivier Deprez157378f2022-04-04 15:47:50 +02005051 field = kvm_register_readl(vcpu, (((instr_info) >> 28) & 0xf));
David Brazdil0f672f62019-12-10 10:32:29 +00005052
5053 offset = vmcs_field_to_offset(field);
5054 if (offset < 0)
Olivier Deprez157378f2022-04-04 15:47:50 +02005055 return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
David Brazdil0f672f62019-12-10 10:32:29 +00005056
5057 if (!is_guest_mode(vcpu) && is_vmcs12_ext_field(field))
5058 copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
5059
Olivier Deprez157378f2022-04-04 15:47:50 +02005060 /* Read the field, zero-extended to a u64 value */
5061 value = vmcs12_read_any(vmcs12, field, offset);
David Brazdil0f672f62019-12-10 10:32:29 +00005062
5063 /*
5064 * Now copy part of this value to register or memory, as requested.
5065 * Note that the number of bits actually copied is 32 or 64 depending
5066 * on the guest's mode (32 or 64 bit), not on the given field's length.
5067 */
Olivier Deprez157378f2022-04-04 15:47:50 +02005068 if (instr_info & BIT(10)) {
5069 kvm_register_writel(vcpu, (((instr_info) >> 3) & 0xf), value);
David Brazdil0f672f62019-12-10 10:32:29 +00005070 } else {
5071 len = is_64_bit_mode(vcpu) ? 8 : 4;
5072 if (get_vmx_mem_address(vcpu, exit_qualification,
Olivier Deprez157378f2022-04-04 15:47:50 +02005073 instr_info, true, len, &gva))
David Brazdil0f672f62019-12-10 10:32:29 +00005074 return 1;
5075 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Olivier Deprez157378f2022-04-04 15:47:50 +02005076 r = kvm_write_guest_virt_system(vcpu, gva, &value, len, &e);
5077 if (r != X86EMUL_CONTINUE)
5078 return kvm_handle_memory_failure(vcpu, r, &e);
David Brazdil0f672f62019-12-10 10:32:29 +00005079 }
5080
5081 return nested_vmx_succeed(vcpu);
5082}
5083
5084static bool is_shadow_field_rw(unsigned long field)
5085{
5086 switch (field) {
5087#define SHADOW_FIELD_RW(x, y) case x:
5088#include "vmcs_shadow_fields.h"
5089 return true;
5090 default:
5091 break;
5092 }
5093 return false;
5094}
5095
5096static bool is_shadow_field_ro(unsigned long field)
5097{
5098 switch (field) {
5099#define SHADOW_FIELD_RO(x, y) case x:
5100#include "vmcs_shadow_fields.h"
5101 return true;
5102 default:
5103 break;
5104 }
5105 return false;
5106}
5107
5108static int handle_vmwrite(struct kvm_vcpu *vcpu)
5109{
Olivier Deprez0e641232021-09-23 10:07:05 +02005110 struct vmcs12 *vmcs12 = is_guest_mode(vcpu) ? get_shadow_vmcs12(vcpu)
5111 : get_vmcs12(vcpu);
Olivier Deprez157378f2022-04-04 15:47:50 +02005112 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
5113 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5114 struct vcpu_vmx *vmx = to_vmx(vcpu);
5115 struct x86_exception e;
5116 unsigned long field;
David Brazdil0f672f62019-12-10 10:32:29 +00005117 short offset;
Olivier Deprez157378f2022-04-04 15:47:50 +02005118 gva_t gva;
5119 int len, r;
5120
5121 /*
5122 * The value to write might be 32 or 64 bits, depending on L1's long
5123 * mode, and eventually we need to write that into a field of several
5124 * possible lengths. The code below first zero-extends the value to 64
5125 * bit (value), and then copies only the appropriate number of
5126 * bits into the vmcs12 field.
5127 */
5128 u64 value = 0;
David Brazdil0f672f62019-12-10 10:32:29 +00005129
5130 if (!nested_vmx_check_permission(vcpu))
5131 return 1;
5132
Olivier Deprez0e641232021-09-23 10:07:05 +02005133 /*
5134 * In VMX non-root operation, when the VMCS-link pointer is -1ull,
5135 * any VMWRITE sets the ALU flags for VMfailInvalid.
5136 */
5137 if (vmx->nested.current_vmptr == -1ull ||
5138 (is_guest_mode(vcpu) &&
5139 get_vmcs12(vcpu)->vmcs_link_pointer == -1ull))
David Brazdil0f672f62019-12-10 10:32:29 +00005140 return nested_vmx_failInvalid(vcpu);
5141
Olivier Deprez157378f2022-04-04 15:47:50 +02005142 if (instr_info & BIT(10))
5143 value = kvm_register_readl(vcpu, (((instr_info) >> 3) & 0xf));
David Brazdil0f672f62019-12-10 10:32:29 +00005144 else {
5145 len = is_64_bit_mode(vcpu) ? 8 : 4;
5146 if (get_vmx_mem_address(vcpu, exit_qualification,
Olivier Deprez157378f2022-04-04 15:47:50 +02005147 instr_info, false, len, &gva))
David Brazdil0f672f62019-12-10 10:32:29 +00005148 return 1;
Olivier Deprez157378f2022-04-04 15:47:50 +02005149 r = kvm_read_guest_virt(vcpu, gva, &value, len, &e);
5150 if (r != X86EMUL_CONTINUE)
5151 return kvm_handle_memory_failure(vcpu, r, &e);
David Brazdil0f672f62019-12-10 10:32:29 +00005152 }
5153
Olivier Deprez157378f2022-04-04 15:47:50 +02005154 field = kvm_register_readl(vcpu, (((instr_info) >> 28) & 0xf));
Olivier Deprez0e641232021-09-23 10:07:05 +02005155
5156 offset = vmcs_field_to_offset(field);
5157 if (offset < 0)
Olivier Deprez157378f2022-04-04 15:47:50 +02005158 return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Olivier Deprez0e641232021-09-23 10:07:05 +02005159
David Brazdil0f672f62019-12-10 10:32:29 +00005160 /*
5161 * If the vCPU supports "VMWRITE to any supported field in the
5162 * VMCS," then the "read-only" fields are actually read/write.
5163 */
5164 if (vmcs_field_readonly(field) &&
5165 !nested_cpu_has_vmwrite_any_field(vcpu))
Olivier Deprez157378f2022-04-04 15:47:50 +02005166 return nested_vmx_fail(vcpu, VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
David Brazdil0f672f62019-12-10 10:32:29 +00005167
Olivier Deprez0e641232021-09-23 10:07:05 +02005168 /*
5169 * Ensure vmcs12 is up-to-date before any VMWRITE that dirties
5170 * vmcs12, else we may crush a field or consume a stale value.
5171 */
5172 if (!is_guest_mode(vcpu) && !is_shadow_field_rw(field))
5173 copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
David Brazdil0f672f62019-12-10 10:32:29 +00005174
5175 /*
5176 * Some Intel CPUs intentionally drop the reserved bits of the AR byte
5177 * fields on VMWRITE. Emulate this behavior to ensure consistent KVM
5178 * behavior regardless of the underlying hardware, e.g. if an AR_BYTE
5179 * field is intercepted for VMWRITE but not VMREAD (in L1), then VMREAD
5180 * from L1 will return a different value than VMREAD from L2 (L1 sees
5181 * the stripped down value, L2 sees the full value as stored by KVM).
5182 */
5183 if (field >= GUEST_ES_AR_BYTES && field <= GUEST_TR_AR_BYTES)
Olivier Deprez157378f2022-04-04 15:47:50 +02005184 value &= 0x1f0ff;
David Brazdil0f672f62019-12-10 10:32:29 +00005185
Olivier Deprez157378f2022-04-04 15:47:50 +02005186 vmcs12_write_any(vmcs12, field, offset, value);
David Brazdil0f672f62019-12-10 10:32:29 +00005187
5188 /*
5189 * Do not track vmcs12 dirty-state if in guest-mode as we actually
5190 * dirty shadow vmcs12 instead of vmcs12. Fields that can be updated
5191 * by L1 without a vmexit are always updated in the vmcs02, i.e. don't
5192 * "dirty" vmcs12, all others go down the prepare_vmcs02() slow path.
5193 */
5194 if (!is_guest_mode(vcpu) && !is_shadow_field_rw(field)) {
5195 /*
5196 * L1 can read these fields without exiting, ensure the
5197 * shadow VMCS is up-to-date.
5198 */
5199 if (enable_shadow_vmcs && is_shadow_field_ro(field)) {
5200 preempt_disable();
5201 vmcs_load(vmx->vmcs01.shadow_vmcs);
5202
Olivier Deprez157378f2022-04-04 15:47:50 +02005203 __vmcs_writel(field, value);
David Brazdil0f672f62019-12-10 10:32:29 +00005204
5205 vmcs_clear(vmx->vmcs01.shadow_vmcs);
5206 vmcs_load(vmx->loaded_vmcs->vmcs);
5207 preempt_enable();
5208 }
5209 vmx->nested.dirty_vmcs12 = true;
5210 }
5211
5212 return nested_vmx_succeed(vcpu);
5213}
5214
5215static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
5216{
5217 vmx->nested.current_vmptr = vmptr;
5218 if (enable_shadow_vmcs) {
5219 secondary_exec_controls_setbit(vmx, SECONDARY_EXEC_SHADOW_VMCS);
5220 vmcs_write64(VMCS_LINK_POINTER,
5221 __pa(vmx->vmcs01.shadow_vmcs));
5222 vmx->nested.need_vmcs12_to_shadow_sync = true;
5223 }
5224 vmx->nested.dirty_vmcs12 = true;
5225}
5226
5227/* Emulate the VMPTRLD instruction */
5228static int handle_vmptrld(struct kvm_vcpu *vcpu)
5229{
5230 struct vcpu_vmx *vmx = to_vmx(vcpu);
5231 gpa_t vmptr;
Olivier Deprez157378f2022-04-04 15:47:50 +02005232 int r;
David Brazdil0f672f62019-12-10 10:32:29 +00005233
5234 if (!nested_vmx_check_permission(vcpu))
5235 return 1;
5236
Olivier Deprez157378f2022-04-04 15:47:50 +02005237 if (nested_vmx_get_vmptr(vcpu, &vmptr, &r))
5238 return r;
David Brazdil0f672f62019-12-10 10:32:29 +00005239
5240 if (!page_address_valid(vcpu, vmptr))
Olivier Deprez157378f2022-04-04 15:47:50 +02005241 return nested_vmx_fail(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
David Brazdil0f672f62019-12-10 10:32:29 +00005242
5243 if (vmptr == vmx->nested.vmxon_ptr)
Olivier Deprez157378f2022-04-04 15:47:50 +02005244 return nested_vmx_fail(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
David Brazdil0f672f62019-12-10 10:32:29 +00005245
5246 /* Forbid normal VMPTRLD if Enlightened version was used */
5247 if (vmx->nested.hv_evmcs)
5248 return 1;
5249
5250 if (vmx->nested.current_vmptr != vmptr) {
5251 struct kvm_host_map map;
5252 struct vmcs12 *new_vmcs12;
5253
5254 if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmptr), &map)) {
5255 /*
5256 * Reads from an unbacked page return all 1s,
5257 * which means that the 32 bits located at the
5258 * given physical address won't match the required
5259 * VMCS12_REVISION identifier.
5260 */
Olivier Deprez157378f2022-04-04 15:47:50 +02005261 return nested_vmx_fail(vcpu,
David Brazdil0f672f62019-12-10 10:32:29 +00005262 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
5263 }
5264
5265 new_vmcs12 = map.hva;
5266
5267 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
5268 (new_vmcs12->hdr.shadow_vmcs &&
5269 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
5270 kvm_vcpu_unmap(vcpu, &map, false);
Olivier Deprez157378f2022-04-04 15:47:50 +02005271 return nested_vmx_fail(vcpu,
David Brazdil0f672f62019-12-10 10:32:29 +00005272 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
5273 }
5274
5275 nested_release_vmcs12(vcpu);
5276
5277 /*
5278 * Load VMCS12 from guest memory since it is not already
5279 * cached.
5280 */
5281 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
5282 kvm_vcpu_unmap(vcpu, &map, false);
5283
5284 set_current_vmptr(vmx, vmptr);
5285 }
5286
5287 return nested_vmx_succeed(vcpu);
5288}
5289
5290/* Emulate the VMPTRST instruction */
5291static int handle_vmptrst(struct kvm_vcpu *vcpu)
5292{
Olivier Deprez157378f2022-04-04 15:47:50 +02005293 unsigned long exit_qual = vmx_get_exit_qual(vcpu);
David Brazdil0f672f62019-12-10 10:32:29 +00005294 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5295 gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr;
5296 struct x86_exception e;
5297 gva_t gva;
Olivier Deprez157378f2022-04-04 15:47:50 +02005298 int r;
David Brazdil0f672f62019-12-10 10:32:29 +00005299
5300 if (!nested_vmx_check_permission(vcpu))
5301 return 1;
5302
5303 if (unlikely(to_vmx(vcpu)->nested.hv_evmcs))
5304 return 1;
5305
5306 if (get_vmx_mem_address(vcpu, exit_qual, instr_info,
5307 true, sizeof(gpa_t), &gva))
5308 return 1;
5309 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Olivier Deprez157378f2022-04-04 15:47:50 +02005310 r = kvm_write_guest_virt_system(vcpu, gva, (void *)&current_vmptr,
5311 sizeof(gpa_t), &e);
5312 if (r != X86EMUL_CONTINUE)
5313 return kvm_handle_memory_failure(vcpu, r, &e);
5314
David Brazdil0f672f62019-12-10 10:32:29 +00005315 return nested_vmx_succeed(vcpu);
5316}
5317
Olivier Deprez157378f2022-04-04 15:47:50 +02005318#define EPTP_PA_MASK GENMASK_ULL(51, 12)
5319
5320static bool nested_ept_root_matches(hpa_t root_hpa, u64 root_eptp, u64 eptp)
5321{
5322 return VALID_PAGE(root_hpa) &&
5323 ((root_eptp & EPTP_PA_MASK) == (eptp & EPTP_PA_MASK));
5324}
5325
David Brazdil0f672f62019-12-10 10:32:29 +00005326/* Emulate the INVEPT instruction */
5327static int handle_invept(struct kvm_vcpu *vcpu)
5328{
5329 struct vcpu_vmx *vmx = to_vmx(vcpu);
5330 u32 vmx_instruction_info, types;
Olivier Deprez157378f2022-04-04 15:47:50 +02005331 unsigned long type, roots_to_free;
5332 struct kvm_mmu *mmu;
David Brazdil0f672f62019-12-10 10:32:29 +00005333 gva_t gva;
5334 struct x86_exception e;
5335 struct {
5336 u64 eptp, gpa;
5337 } operand;
Olivier Deprez157378f2022-04-04 15:47:50 +02005338 int i, r;
David Brazdil0f672f62019-12-10 10:32:29 +00005339
5340 if (!(vmx->nested.msrs.secondary_ctls_high &
5341 SECONDARY_EXEC_ENABLE_EPT) ||
5342 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
5343 kvm_queue_exception(vcpu, UD_VECTOR);
5344 return 1;
5345 }
5346
5347 if (!nested_vmx_check_permission(vcpu))
5348 return 1;
5349
5350 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5351 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
5352
5353 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
5354
5355 if (type >= 32 || !(types & (1 << type)))
Olivier Deprez157378f2022-04-04 15:47:50 +02005356 return nested_vmx_fail(vcpu, VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
David Brazdil0f672f62019-12-10 10:32:29 +00005357
5358 /* According to the Intel VMX instruction reference, the memory
5359 * operand is read even if it isn't needed (e.g., for type==global)
5360 */
Olivier Deprez157378f2022-04-04 15:47:50 +02005361 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
David Brazdil0f672f62019-12-10 10:32:29 +00005362 vmx_instruction_info, false, sizeof(operand), &gva))
5363 return 1;
Olivier Deprez157378f2022-04-04 15:47:50 +02005364 r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e);
5365 if (r != X86EMUL_CONTINUE)
5366 return kvm_handle_memory_failure(vcpu, r, &e);
5367
5368 /*
5369 * Nested EPT roots are always held through guest_mmu,
5370 * not root_mmu.
5371 */
5372 mmu = &vcpu->arch.guest_mmu;
David Brazdil0f672f62019-12-10 10:32:29 +00005373
5374 switch (type) {
David Brazdil0f672f62019-12-10 10:32:29 +00005375 case VMX_EPT_EXTENT_CONTEXT:
Olivier Deprez157378f2022-04-04 15:47:50 +02005376 if (!nested_vmx_check_eptp(vcpu, operand.eptp))
5377 return nested_vmx_fail(vcpu,
5378 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
5379
5380 roots_to_free = 0;
5381 if (nested_ept_root_matches(mmu->root_hpa, mmu->root_pgd,
5382 operand.eptp))
5383 roots_to_free |= KVM_MMU_ROOT_CURRENT;
5384
5385 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) {
5386 if (nested_ept_root_matches(mmu->prev_roots[i].hpa,
5387 mmu->prev_roots[i].pgd,
5388 operand.eptp))
5389 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
5390 }
5391 break;
5392 case VMX_EPT_EXTENT_GLOBAL:
5393 roots_to_free = KVM_MMU_ROOTS_ALL;
David Brazdil0f672f62019-12-10 10:32:29 +00005394 break;
5395 default:
Olivier Deprez157378f2022-04-04 15:47:50 +02005396 BUG();
David Brazdil0f672f62019-12-10 10:32:29 +00005397 break;
5398 }
5399
Olivier Deprez157378f2022-04-04 15:47:50 +02005400 if (roots_to_free)
5401 kvm_mmu_free_roots(vcpu, mmu, roots_to_free);
5402
David Brazdil0f672f62019-12-10 10:32:29 +00005403 return nested_vmx_succeed(vcpu);
5404}
5405
5406static int handle_invvpid(struct kvm_vcpu *vcpu)
5407{
5408 struct vcpu_vmx *vmx = to_vmx(vcpu);
5409 u32 vmx_instruction_info;
5410 unsigned long type, types;
5411 gva_t gva;
5412 struct x86_exception e;
5413 struct {
5414 u64 vpid;
5415 u64 gla;
5416 } operand;
5417 u16 vpid02;
Olivier Deprez157378f2022-04-04 15:47:50 +02005418 int r;
David Brazdil0f672f62019-12-10 10:32:29 +00005419
5420 if (!(vmx->nested.msrs.secondary_ctls_high &
5421 SECONDARY_EXEC_ENABLE_VPID) ||
5422 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
5423 kvm_queue_exception(vcpu, UD_VECTOR);
5424 return 1;
5425 }
5426
5427 if (!nested_vmx_check_permission(vcpu))
5428 return 1;
5429
5430 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5431 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
5432
5433 types = (vmx->nested.msrs.vpid_caps &
5434 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
5435
5436 if (type >= 32 || !(types & (1 << type)))
Olivier Deprez157378f2022-04-04 15:47:50 +02005437 return nested_vmx_fail(vcpu,
David Brazdil0f672f62019-12-10 10:32:29 +00005438 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
5439
5440 /* according to the intel vmx instruction reference, the memory
5441 * operand is read even if it isn't needed (e.g., for type==global)
5442 */
Olivier Deprez157378f2022-04-04 15:47:50 +02005443 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
David Brazdil0f672f62019-12-10 10:32:29 +00005444 vmx_instruction_info, false, sizeof(operand), &gva))
5445 return 1;
Olivier Deprez157378f2022-04-04 15:47:50 +02005446 r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e);
5447 if (r != X86EMUL_CONTINUE)
5448 return kvm_handle_memory_failure(vcpu, r, &e);
5449
David Brazdil0f672f62019-12-10 10:32:29 +00005450 if (operand.vpid >> 16)
Olivier Deprez157378f2022-04-04 15:47:50 +02005451 return nested_vmx_fail(vcpu,
David Brazdil0f672f62019-12-10 10:32:29 +00005452 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
5453
5454 vpid02 = nested_get_vpid02(vcpu);
5455 switch (type) {
5456 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
5457 if (!operand.vpid ||
5458 is_noncanonical_address(operand.gla, vcpu))
Olivier Deprez157378f2022-04-04 15:47:50 +02005459 return nested_vmx_fail(vcpu,
David Brazdil0f672f62019-12-10 10:32:29 +00005460 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Olivier Deprez157378f2022-04-04 15:47:50 +02005461 vpid_sync_vcpu_addr(vpid02, operand.gla);
David Brazdil0f672f62019-12-10 10:32:29 +00005462 break;
5463 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
5464 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
5465 if (!operand.vpid)
Olivier Deprez157378f2022-04-04 15:47:50 +02005466 return nested_vmx_fail(vcpu,
David Brazdil0f672f62019-12-10 10:32:29 +00005467 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Olivier Deprez157378f2022-04-04 15:47:50 +02005468 vpid_sync_context(vpid02);
David Brazdil0f672f62019-12-10 10:32:29 +00005469 break;
5470 case VMX_VPID_EXTENT_ALL_CONTEXT:
Olivier Deprez157378f2022-04-04 15:47:50 +02005471 vpid_sync_context(vpid02);
David Brazdil0f672f62019-12-10 10:32:29 +00005472 break;
5473 default:
5474 WARN_ON_ONCE(1);
5475 return kvm_skip_emulated_instruction(vcpu);
5476 }
5477
Olivier Deprez157378f2022-04-04 15:47:50 +02005478 /*
5479 * Sync the shadow page tables if EPT is disabled, L1 is invalidating
5480 * linear mappings for L2 (tagged with L2's VPID). Free all roots as
5481 * VPIDs are not tracked in the MMU role.
5482 *
5483 * Note, this operates on root_mmu, not guest_mmu, as L1 and L2 share
5484 * an MMU when EPT is disabled.
5485 *
5486 * TODO: sync only the affected SPTEs for INVDIVIDUAL_ADDR.
5487 */
5488 if (!enable_ept)
5489 kvm_mmu_free_roots(vcpu, &vcpu->arch.root_mmu,
5490 KVM_MMU_ROOTS_ALL);
5491
David Brazdil0f672f62019-12-10 10:32:29 +00005492 return nested_vmx_succeed(vcpu);
5493}
5494
5495static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
5496 struct vmcs12 *vmcs12)
5497{
5498 u32 index = kvm_rcx_read(vcpu);
Olivier Deprez157378f2022-04-04 15:47:50 +02005499 u64 new_eptp;
David Brazdil0f672f62019-12-10 10:32:29 +00005500
5501 if (!nested_cpu_has_eptp_switching(vmcs12) ||
5502 !nested_cpu_has_ept(vmcs12))
5503 return 1;
5504
5505 if (index >= VMFUNC_EPTP_ENTRIES)
5506 return 1;
5507
David Brazdil0f672f62019-12-10 10:32:29 +00005508 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
Olivier Deprez157378f2022-04-04 15:47:50 +02005509 &new_eptp, index * 8, 8))
David Brazdil0f672f62019-12-10 10:32:29 +00005510 return 1;
5511
David Brazdil0f672f62019-12-10 10:32:29 +00005512 /*
5513 * If the (L2) guest does a vmfunc to the currently
5514 * active ept pointer, we don't have to do anything else
5515 */
Olivier Deprez157378f2022-04-04 15:47:50 +02005516 if (vmcs12->ept_pointer != new_eptp) {
5517 if (!nested_vmx_check_eptp(vcpu, new_eptp))
David Brazdil0f672f62019-12-10 10:32:29 +00005518 return 1;
5519
Olivier Deprez157378f2022-04-04 15:47:50 +02005520 vmcs12->ept_pointer = new_eptp;
5521
5522 kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu);
David Brazdil0f672f62019-12-10 10:32:29 +00005523 }
5524
5525 return 0;
5526}
5527
5528static int handle_vmfunc(struct kvm_vcpu *vcpu)
5529{
5530 struct vcpu_vmx *vmx = to_vmx(vcpu);
5531 struct vmcs12 *vmcs12;
5532 u32 function = kvm_rax_read(vcpu);
5533
5534 /*
5535 * VMFUNC is only supported for nested guests, but we always enable the
5536 * secondary control for simplicity; for non-nested mode, fake that we
5537 * didn't by injecting #UD.
5538 */
5539 if (!is_guest_mode(vcpu)) {
5540 kvm_queue_exception(vcpu, UD_VECTOR);
5541 return 1;
5542 }
5543
5544 vmcs12 = get_vmcs12(vcpu);
Olivier Deprez0e641232021-09-23 10:07:05 +02005545 if (!(vmcs12->vm_function_control & BIT_ULL(function)))
David Brazdil0f672f62019-12-10 10:32:29 +00005546 goto fail;
5547
5548 switch (function) {
5549 case 0:
5550 if (nested_vmx_eptp_switching(vcpu, vmcs12))
5551 goto fail;
5552 break;
5553 default:
5554 goto fail;
5555 }
5556 return kvm_skip_emulated_instruction(vcpu);
5557
5558fail:
Olivier Deprez157378f2022-04-04 15:47:50 +02005559 /*
5560 * This is effectively a reflected VM-Exit, as opposed to a synthesized
5561 * nested VM-Exit. Pass the original exit reason, i.e. don't hardcode
5562 * EXIT_REASON_VMFUNC as the exit reason.
5563 */
5564 nested_vmx_vmexit(vcpu, vmx->exit_reason.full,
5565 vmx_get_intr_info(vcpu),
5566 vmx_get_exit_qual(vcpu));
David Brazdil0f672f62019-12-10 10:32:29 +00005567 return 1;
5568}
5569
Olivier Deprez0e641232021-09-23 10:07:05 +02005570/*
5571 * Return true if an IO instruction with the specified port and size should cause
5572 * a VM-exit into L1.
5573 */
5574bool nested_vmx_check_io_bitmaps(struct kvm_vcpu *vcpu, unsigned int port,
5575 int size)
David Brazdil0f672f62019-12-10 10:32:29 +00005576{
Olivier Deprez0e641232021-09-23 10:07:05 +02005577 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
David Brazdil0f672f62019-12-10 10:32:29 +00005578 gpa_t bitmap, last_bitmap;
David Brazdil0f672f62019-12-10 10:32:29 +00005579 u8 b;
5580
David Brazdil0f672f62019-12-10 10:32:29 +00005581 last_bitmap = (gpa_t)-1;
5582 b = -1;
5583
5584 while (size > 0) {
5585 if (port < 0x8000)
5586 bitmap = vmcs12->io_bitmap_a;
5587 else if (port < 0x10000)
5588 bitmap = vmcs12->io_bitmap_b;
5589 else
5590 return true;
5591 bitmap += (port & 0x7fff) / 8;
5592
5593 if (last_bitmap != bitmap)
5594 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
5595 return true;
5596 if (b & (1 << (port & 7)))
5597 return true;
5598
5599 port++;
5600 size--;
5601 last_bitmap = bitmap;
5602 }
5603
5604 return false;
5605}
5606
Olivier Deprez0e641232021-09-23 10:07:05 +02005607static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
5608 struct vmcs12 *vmcs12)
5609{
5610 unsigned long exit_qualification;
5611 unsigned short port;
5612 int size;
5613
5614 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
5615 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
5616
Olivier Deprez157378f2022-04-04 15:47:50 +02005617 exit_qualification = vmx_get_exit_qual(vcpu);
Olivier Deprez0e641232021-09-23 10:07:05 +02005618
5619 port = exit_qualification >> 16;
5620 size = (exit_qualification & 7) + 1;
5621
5622 return nested_vmx_check_io_bitmaps(vcpu, port, size);
5623}
5624
David Brazdil0f672f62019-12-10 10:32:29 +00005625/*
Olivier Deprez157378f2022-04-04 15:47:50 +02005626 * Return 1 if we should exit from L2 to L1 to handle an MSR access,
David Brazdil0f672f62019-12-10 10:32:29 +00005627 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
5628 * disinterest in the current event (read or write a specific MSR) by using an
5629 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
5630 */
5631static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
Olivier Deprez157378f2022-04-04 15:47:50 +02005632 struct vmcs12 *vmcs12,
5633 union vmx_exit_reason exit_reason)
David Brazdil0f672f62019-12-10 10:32:29 +00005634{
5635 u32 msr_index = kvm_rcx_read(vcpu);
5636 gpa_t bitmap;
5637
5638 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
5639 return true;
5640
5641 /*
5642 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
5643 * for the four combinations of read/write and low/high MSR numbers.
5644 * First we need to figure out which of the four to use:
5645 */
5646 bitmap = vmcs12->msr_bitmap;
Olivier Deprez157378f2022-04-04 15:47:50 +02005647 if (exit_reason.basic == EXIT_REASON_MSR_WRITE)
David Brazdil0f672f62019-12-10 10:32:29 +00005648 bitmap += 2048;
5649 if (msr_index >= 0xc0000000) {
5650 msr_index -= 0xc0000000;
5651 bitmap += 1024;
5652 }
5653
5654 /* Then read the msr_index'th bit from this bitmap: */
5655 if (msr_index < 1024*8) {
5656 unsigned char b;
5657 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
5658 return true;
5659 return 1 & (b >> (msr_index & 7));
5660 } else
5661 return true; /* let L1 handle the wrong parameter */
5662}
5663
5664/*
5665 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
5666 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
5667 * intercept (via guest_host_mask etc.) the current event.
5668 */
5669static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
5670 struct vmcs12 *vmcs12)
5671{
Olivier Deprez157378f2022-04-04 15:47:50 +02005672 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
David Brazdil0f672f62019-12-10 10:32:29 +00005673 int cr = exit_qualification & 15;
5674 int reg;
5675 unsigned long val;
5676
5677 switch ((exit_qualification >> 4) & 3) {
5678 case 0: /* mov to cr */
5679 reg = (exit_qualification >> 8) & 15;
5680 val = kvm_register_readl(vcpu, reg);
5681 switch (cr) {
5682 case 0:
5683 if (vmcs12->cr0_guest_host_mask &
5684 (val ^ vmcs12->cr0_read_shadow))
5685 return true;
5686 break;
5687 case 3:
David Brazdil0f672f62019-12-10 10:32:29 +00005688 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
5689 return true;
5690 break;
5691 case 4:
5692 if (vmcs12->cr4_guest_host_mask &
5693 (vmcs12->cr4_read_shadow ^ val))
5694 return true;
5695 break;
5696 case 8:
5697 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
5698 return true;
5699 break;
5700 }
5701 break;
5702 case 2: /* clts */
5703 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
5704 (vmcs12->cr0_read_shadow & X86_CR0_TS))
5705 return true;
5706 break;
5707 case 1: /* mov from cr */
5708 switch (cr) {
5709 case 3:
5710 if (vmcs12->cpu_based_vm_exec_control &
5711 CPU_BASED_CR3_STORE_EXITING)
5712 return true;
5713 break;
5714 case 8:
5715 if (vmcs12->cpu_based_vm_exec_control &
5716 CPU_BASED_CR8_STORE_EXITING)
5717 return true;
5718 break;
5719 }
5720 break;
5721 case 3: /* lmsw */
5722 /*
5723 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
5724 * cr0. Other attempted changes are ignored, with no exit.
5725 */
5726 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
5727 if (vmcs12->cr0_guest_host_mask & 0xe &
5728 (val ^ vmcs12->cr0_read_shadow))
5729 return true;
5730 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
5731 !(vmcs12->cr0_read_shadow & 0x1) &&
5732 (val & 0x1))
5733 return true;
5734 break;
5735 }
5736 return false;
5737}
5738
5739static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
5740 struct vmcs12 *vmcs12, gpa_t bitmap)
5741{
5742 u32 vmx_instruction_info;
5743 unsigned long field;
5744 u8 b;
5745
5746 if (!nested_cpu_has_shadow_vmcs(vmcs12))
5747 return true;
5748
5749 /* Decode instruction info and find the field to access */
5750 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Olivier Deprez0e641232021-09-23 10:07:05 +02005751 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
David Brazdil0f672f62019-12-10 10:32:29 +00005752
5753 /* Out-of-range fields always cause a VM exit from L2 to L1 */
5754 if (field >> 15)
5755 return true;
5756
5757 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
5758 return true;
5759
5760 return 1 & (b >> (field & 7));
5761}
5762
Olivier Deprez157378f2022-04-04 15:47:50 +02005763static bool nested_vmx_exit_handled_mtf(struct vmcs12 *vmcs12)
David Brazdil0f672f62019-12-10 10:32:29 +00005764{
Olivier Deprez157378f2022-04-04 15:47:50 +02005765 u32 entry_intr_info = vmcs12->vm_entry_intr_info_field;
David Brazdil0f672f62019-12-10 10:32:29 +00005766
Olivier Deprez157378f2022-04-04 15:47:50 +02005767 if (nested_cpu_has_mtf(vmcs12))
David Brazdil0f672f62019-12-10 10:32:29 +00005768 return true;
David Brazdil0f672f62019-12-10 10:32:29 +00005769
5770 /*
Olivier Deprez157378f2022-04-04 15:47:50 +02005771 * An MTF VM-exit may be injected into the guest by setting the
5772 * interruption-type to 7 (other event) and the vector field to 0. Such
5773 * is the case regardless of the 'monitor trap flag' VM-execution
5774 * control.
David Brazdil0f672f62019-12-10 10:32:29 +00005775 */
Olivier Deprez157378f2022-04-04 15:47:50 +02005776 return entry_intr_info == (INTR_INFO_VALID_MASK
5777 | INTR_TYPE_OTHER_EVENT);
5778}
David Brazdil0f672f62019-12-10 10:32:29 +00005779
Olivier Deprez157378f2022-04-04 15:47:50 +02005780/*
5781 * Return true if L0 wants to handle an exit from L2 regardless of whether or not
5782 * L1 wants the exit. Only call this when in is_guest_mode (L2).
5783 */
5784static bool nested_vmx_l0_wants_exit(struct kvm_vcpu *vcpu,
5785 union vmx_exit_reason exit_reason)
5786{
5787 u32 intr_info;
David Brazdil0f672f62019-12-10 10:32:29 +00005788
Olivier Deprez157378f2022-04-04 15:47:50 +02005789 switch ((u16)exit_reason.basic) {
David Brazdil0f672f62019-12-10 10:32:29 +00005790 case EXIT_REASON_EXCEPTION_NMI:
Olivier Deprez157378f2022-04-04 15:47:50 +02005791 intr_info = vmx_get_intr_info(vcpu);
David Brazdil0f672f62019-12-10 10:32:29 +00005792 if (is_nmi(intr_info))
Olivier Deprez157378f2022-04-04 15:47:50 +02005793 return true;
David Brazdil0f672f62019-12-10 10:32:29 +00005794 else if (is_page_fault(intr_info))
Olivier Deprez157378f2022-04-04 15:47:50 +02005795 return vcpu->arch.apf.host_apf_flags ||
5796 vmx_need_pf_intercept(vcpu);
David Brazdil0f672f62019-12-10 10:32:29 +00005797 else if (is_debug(intr_info) &&
5798 vcpu->guest_debug &
5799 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
Olivier Deprez157378f2022-04-04 15:47:50 +02005800 return true;
David Brazdil0f672f62019-12-10 10:32:29 +00005801 else if (is_breakpoint(intr_info) &&
5802 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Olivier Deprez157378f2022-04-04 15:47:50 +02005803 return true;
5804 else if (is_alignment_check(intr_info) &&
5805 !vmx_guest_inject_ac(vcpu))
5806 return true;
5807 return false;
5808 case EXIT_REASON_EXTERNAL_INTERRUPT:
5809 return true;
5810 case EXIT_REASON_MCE_DURING_VMENTRY:
5811 return true;
5812 case EXIT_REASON_EPT_VIOLATION:
5813 /*
5814 * L0 always deals with the EPT violation. If nested EPT is
5815 * used, and the nested mmu code discovers that the address is
5816 * missing in the guest EPT table (EPT12), the EPT violation
5817 * will be injected with nested_ept_inject_page_fault()
5818 */
5819 return true;
5820 case EXIT_REASON_EPT_MISCONFIG:
5821 /*
5822 * L2 never uses directly L1's EPT, but rather L0's own EPT
5823 * table (shadow on EPT) or a merged EPT table that L0 built
5824 * (EPT on EPT). So any problems with the structure of the
5825 * table is L0's fault.
5826 */
5827 return true;
5828 case EXIT_REASON_PREEMPTION_TIMER:
5829 return true;
5830 case EXIT_REASON_PML_FULL:
5831 /* We emulate PML support to L1. */
5832 return true;
5833 case EXIT_REASON_VMFUNC:
5834 /* VM functions are emulated through L2->L0 vmexits. */
5835 return true;
5836 case EXIT_REASON_ENCLS:
5837 /* SGX is never exposed to L1 */
5838 return true;
5839 default:
5840 break;
5841 }
5842 return false;
5843}
5844
5845/*
5846 * Return 1 if L1 wants to intercept an exit from L2. Only call this when in
5847 * is_guest_mode (L2).
5848 */
5849static bool nested_vmx_l1_wants_exit(struct kvm_vcpu *vcpu,
5850 union vmx_exit_reason exit_reason)
5851{
5852 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5853 u32 intr_info;
5854
5855 switch ((u16)exit_reason.basic) {
5856 case EXIT_REASON_EXCEPTION_NMI:
5857 intr_info = vmx_get_intr_info(vcpu);
5858 if (is_nmi(intr_info))
5859 return true;
5860 else if (is_page_fault(intr_info))
5861 return true;
David Brazdil0f672f62019-12-10 10:32:29 +00005862 return vmcs12->exception_bitmap &
5863 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
5864 case EXIT_REASON_EXTERNAL_INTERRUPT:
Olivier Deprez157378f2022-04-04 15:47:50 +02005865 return nested_exit_on_intr(vcpu);
David Brazdil0f672f62019-12-10 10:32:29 +00005866 case EXIT_REASON_TRIPLE_FAULT:
5867 return true;
Olivier Deprez157378f2022-04-04 15:47:50 +02005868 case EXIT_REASON_INTERRUPT_WINDOW:
5869 return nested_cpu_has(vmcs12, CPU_BASED_INTR_WINDOW_EXITING);
David Brazdil0f672f62019-12-10 10:32:29 +00005870 case EXIT_REASON_NMI_WINDOW:
Olivier Deprez157378f2022-04-04 15:47:50 +02005871 return nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING);
David Brazdil0f672f62019-12-10 10:32:29 +00005872 case EXIT_REASON_TASK_SWITCH:
5873 return true;
5874 case EXIT_REASON_CPUID:
5875 return true;
5876 case EXIT_REASON_HLT:
5877 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
5878 case EXIT_REASON_INVD:
5879 return true;
5880 case EXIT_REASON_INVLPG:
5881 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
5882 case EXIT_REASON_RDPMC:
5883 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
5884 case EXIT_REASON_RDRAND:
5885 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
5886 case EXIT_REASON_RDSEED:
5887 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
5888 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
5889 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
5890 case EXIT_REASON_VMREAD:
5891 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
5892 vmcs12->vmread_bitmap);
5893 case EXIT_REASON_VMWRITE:
5894 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
5895 vmcs12->vmwrite_bitmap);
5896 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
5897 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
5898 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
5899 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
5900 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
5901 /*
5902 * VMX instructions trap unconditionally. This allows L1 to
5903 * emulate them for its L2 guest, i.e., allows 3-level nesting!
5904 */
5905 return true;
5906 case EXIT_REASON_CR_ACCESS:
5907 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
5908 case EXIT_REASON_DR_ACCESS:
5909 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
5910 case EXIT_REASON_IO_INSTRUCTION:
5911 return nested_vmx_exit_handled_io(vcpu, vmcs12);
5912 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
5913 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
5914 case EXIT_REASON_MSR_READ:
5915 case EXIT_REASON_MSR_WRITE:
5916 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
5917 case EXIT_REASON_INVALID_STATE:
5918 return true;
5919 case EXIT_REASON_MWAIT_INSTRUCTION:
5920 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
5921 case EXIT_REASON_MONITOR_TRAP_FLAG:
Olivier Deprez157378f2022-04-04 15:47:50 +02005922 return nested_vmx_exit_handled_mtf(vmcs12);
David Brazdil0f672f62019-12-10 10:32:29 +00005923 case EXIT_REASON_MONITOR_INSTRUCTION:
5924 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
5925 case EXIT_REASON_PAUSE_INSTRUCTION:
5926 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
5927 nested_cpu_has2(vmcs12,
5928 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
5929 case EXIT_REASON_MCE_DURING_VMENTRY:
Olivier Deprez157378f2022-04-04 15:47:50 +02005930 return true;
David Brazdil0f672f62019-12-10 10:32:29 +00005931 case EXIT_REASON_TPR_BELOW_THRESHOLD:
5932 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
5933 case EXIT_REASON_APIC_ACCESS:
5934 case EXIT_REASON_APIC_WRITE:
5935 case EXIT_REASON_EOI_INDUCED:
5936 /*
5937 * The controls for "virtualize APIC accesses," "APIC-
5938 * register virtualization," and "virtual-interrupt
5939 * delivery" only come from vmcs12.
5940 */
5941 return true;
David Brazdil0f672f62019-12-10 10:32:29 +00005942 case EXIT_REASON_INVPCID:
5943 return
5944 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
5945 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
5946 case EXIT_REASON_WBINVD:
5947 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
5948 case EXIT_REASON_XSETBV:
5949 return true;
5950 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
5951 /*
5952 * This should never happen, since it is not possible to
5953 * set XSS to a non-zero value---neither in L1 nor in L2.
5954 * If if it were, XSS would have to be checked against
5955 * the XSS exit bitmap in vmcs12.
5956 */
5957 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
David Brazdil0f672f62019-12-10 10:32:29 +00005958 case EXIT_REASON_UMWAIT:
5959 case EXIT_REASON_TPAUSE:
5960 return nested_cpu_has2(vmcs12,
5961 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE);
5962 default:
5963 return true;
5964 }
5965}
5966
Olivier Deprez157378f2022-04-04 15:47:50 +02005967/*
5968 * Conditionally reflect a VM-Exit into L1. Returns %true if the VM-Exit was
5969 * reflected into L1.
5970 */
5971bool nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu)
5972{
5973 struct vcpu_vmx *vmx = to_vmx(vcpu);
5974 union vmx_exit_reason exit_reason = vmx->exit_reason;
5975 unsigned long exit_qual;
5976 u32 exit_intr_info;
5977
5978 WARN_ON_ONCE(vmx->nested.nested_run_pending);
5979
5980 /*
5981 * Late nested VM-Fail shares the same flow as nested VM-Exit since KVM
5982 * has already loaded L2's state.
5983 */
5984 if (unlikely(vmx->fail)) {
5985 trace_kvm_nested_vmenter_failed(
5986 "hardware VM-instruction error: ",
5987 vmcs_read32(VM_INSTRUCTION_ERROR));
5988 exit_intr_info = 0;
5989 exit_qual = 0;
5990 goto reflect_vmexit;
5991 }
5992
5993 trace_kvm_nested_vmexit(exit_reason.full, vcpu, KVM_ISA_VMX);
5994
5995 /* If L0 (KVM) wants the exit, it trumps L1's desires. */
5996 if (nested_vmx_l0_wants_exit(vcpu, exit_reason))
5997 return false;
5998
5999 /* If L1 doesn't want the exit, handle it in L0. */
6000 if (!nested_vmx_l1_wants_exit(vcpu, exit_reason))
6001 return false;
6002
6003 /*
6004 * vmcs.VM_EXIT_INTR_INFO is only valid for EXCEPTION_NMI exits. For
6005 * EXTERNAL_INTERRUPT, the value for vmcs12->vm_exit_intr_info would
6006 * need to be synthesized by querying the in-kernel LAPIC, but external
6007 * interrupts are never reflected to L1 so it's a non-issue.
6008 */
6009 exit_intr_info = vmx_get_intr_info(vcpu);
6010 if (is_exception_with_error_code(exit_intr_info)) {
6011 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6012
6013 vmcs12->vm_exit_intr_error_code =
6014 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
6015 }
6016 exit_qual = vmx_get_exit_qual(vcpu);
6017
6018reflect_vmexit:
6019 nested_vmx_vmexit(vcpu, exit_reason.full, exit_intr_info, exit_qual);
6020 return true;
6021}
David Brazdil0f672f62019-12-10 10:32:29 +00006022
6023static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
6024 struct kvm_nested_state __user *user_kvm_nested_state,
6025 u32 user_data_size)
6026{
6027 struct vcpu_vmx *vmx;
6028 struct vmcs12 *vmcs12;
6029 struct kvm_nested_state kvm_state = {
6030 .flags = 0,
6031 .format = KVM_STATE_NESTED_FORMAT_VMX,
6032 .size = sizeof(kvm_state),
Olivier Deprez157378f2022-04-04 15:47:50 +02006033 .hdr.vmx.flags = 0,
David Brazdil0f672f62019-12-10 10:32:29 +00006034 .hdr.vmx.vmxon_pa = -1ull,
6035 .hdr.vmx.vmcs12_pa = -1ull,
Olivier Deprez157378f2022-04-04 15:47:50 +02006036 .hdr.vmx.preemption_timer_deadline = 0,
David Brazdil0f672f62019-12-10 10:32:29 +00006037 };
6038 struct kvm_vmx_nested_state_data __user *user_vmx_nested_state =
6039 &user_kvm_nested_state->data.vmx[0];
6040
6041 if (!vcpu)
6042 return kvm_state.size + sizeof(*user_vmx_nested_state);
6043
6044 vmx = to_vmx(vcpu);
6045 vmcs12 = get_vmcs12(vcpu);
6046
6047 if (nested_vmx_allowed(vcpu) &&
6048 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
6049 kvm_state.hdr.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
6050 kvm_state.hdr.vmx.vmcs12_pa = vmx->nested.current_vmptr;
6051
6052 if (vmx_has_valid_vmcs12(vcpu)) {
6053 kvm_state.size += sizeof(user_vmx_nested_state->vmcs12);
6054
6055 if (vmx->nested.hv_evmcs)
6056 kvm_state.flags |= KVM_STATE_NESTED_EVMCS;
6057
6058 if (is_guest_mode(vcpu) &&
6059 nested_cpu_has_shadow_vmcs(vmcs12) &&
6060 vmcs12->vmcs_link_pointer != -1ull)
6061 kvm_state.size += sizeof(user_vmx_nested_state->shadow_vmcs12);
6062 }
6063
6064 if (vmx->nested.smm.vmxon)
6065 kvm_state.hdr.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
6066
6067 if (vmx->nested.smm.guest_mode)
6068 kvm_state.hdr.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
6069
6070 if (is_guest_mode(vcpu)) {
6071 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
6072
6073 if (vmx->nested.nested_run_pending)
6074 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
Olivier Deprez157378f2022-04-04 15:47:50 +02006075
6076 if (vmx->nested.mtf_pending)
6077 kvm_state.flags |= KVM_STATE_NESTED_MTF_PENDING;
6078
6079 if (nested_cpu_has_preemption_timer(vmcs12) &&
6080 vmx->nested.has_preemption_timer_deadline) {
6081 kvm_state.hdr.vmx.flags |=
6082 KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE;
6083 kvm_state.hdr.vmx.preemption_timer_deadline =
6084 vmx->nested.preemption_timer_deadline;
6085 }
David Brazdil0f672f62019-12-10 10:32:29 +00006086 }
6087 }
6088
6089 if (user_data_size < kvm_state.size)
6090 goto out;
6091
6092 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
6093 return -EFAULT;
6094
6095 if (!vmx_has_valid_vmcs12(vcpu))
6096 goto out;
6097
6098 /*
6099 * When running L2, the authoritative vmcs12 state is in the
6100 * vmcs02. When running L1, the authoritative vmcs12 state is
6101 * in the shadow or enlightened vmcs linked to vmcs01, unless
6102 * need_vmcs12_to_shadow_sync is set, in which case, the authoritative
6103 * vmcs12 state is in the vmcs12 already.
6104 */
6105 if (is_guest_mode(vcpu)) {
6106 sync_vmcs02_to_vmcs12(vcpu, vmcs12);
6107 sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
Olivier Deprez0e641232021-09-23 10:07:05 +02006108 } else {
6109 copy_vmcs02_to_vmcs12_rare(vcpu, get_vmcs12(vcpu));
6110 if (!vmx->nested.need_vmcs12_to_shadow_sync) {
6111 if (vmx->nested.hv_evmcs)
6112 copy_enlightened_to_vmcs12(vmx);
6113 else if (enable_shadow_vmcs)
6114 copy_shadow_to_vmcs12(vmx);
6115 }
David Brazdil0f672f62019-12-10 10:32:29 +00006116 }
6117
6118 BUILD_BUG_ON(sizeof(user_vmx_nested_state->vmcs12) < VMCS12_SIZE);
6119 BUILD_BUG_ON(sizeof(user_vmx_nested_state->shadow_vmcs12) < VMCS12_SIZE);
6120
6121 /*
6122 * Copy over the full allocated size of vmcs12 rather than just the size
6123 * of the struct.
6124 */
6125 if (copy_to_user(user_vmx_nested_state->vmcs12, vmcs12, VMCS12_SIZE))
6126 return -EFAULT;
6127
6128 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
6129 vmcs12->vmcs_link_pointer != -1ull) {
6130 if (copy_to_user(user_vmx_nested_state->shadow_vmcs12,
6131 get_shadow_vmcs12(vcpu), VMCS12_SIZE))
6132 return -EFAULT;
6133 }
David Brazdil0f672f62019-12-10 10:32:29 +00006134out:
6135 return kvm_state.size;
6136}
6137
6138/*
6139 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
6140 */
6141void vmx_leave_nested(struct kvm_vcpu *vcpu)
6142{
6143 if (is_guest_mode(vcpu)) {
6144 to_vmx(vcpu)->nested.nested_run_pending = 0;
6145 nested_vmx_vmexit(vcpu, -1, 0, 0);
6146 }
6147 free_nested(vcpu);
6148}
6149
6150static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
6151 struct kvm_nested_state __user *user_kvm_nested_state,
6152 struct kvm_nested_state *kvm_state)
6153{
6154 struct vcpu_vmx *vmx = to_vmx(vcpu);
6155 struct vmcs12 *vmcs12;
Olivier Deprez157378f2022-04-04 15:47:50 +02006156 enum vm_entry_failure_code ignored;
David Brazdil0f672f62019-12-10 10:32:29 +00006157 struct kvm_vmx_nested_state_data __user *user_vmx_nested_state =
6158 &user_kvm_nested_state->data.vmx[0];
6159 int ret;
6160
6161 if (kvm_state->format != KVM_STATE_NESTED_FORMAT_VMX)
6162 return -EINVAL;
6163
6164 if (kvm_state->hdr.vmx.vmxon_pa == -1ull) {
6165 if (kvm_state->hdr.vmx.smm.flags)
6166 return -EINVAL;
6167
6168 if (kvm_state->hdr.vmx.vmcs12_pa != -1ull)
6169 return -EINVAL;
6170
6171 /*
6172 * KVM_STATE_NESTED_EVMCS used to signal that KVM should
6173 * enable eVMCS capability on vCPU. However, since then
6174 * code was changed such that flag signals vmcs12 should
6175 * be copied into eVMCS in guest memory.
6176 *
6177 * To preserve backwards compatability, allow user
6178 * to set this flag even when there is no VMXON region.
6179 */
6180 if (kvm_state->flags & ~KVM_STATE_NESTED_EVMCS)
6181 return -EINVAL;
6182 } else {
6183 if (!nested_vmx_allowed(vcpu))
6184 return -EINVAL;
6185
6186 if (!page_address_valid(vcpu, kvm_state->hdr.vmx.vmxon_pa))
6187 return -EINVAL;
6188 }
6189
6190 if ((kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
6191 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
6192 return -EINVAL;
6193
6194 if (kvm_state->hdr.vmx.smm.flags &
6195 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
6196 return -EINVAL;
6197
Olivier Deprez157378f2022-04-04 15:47:50 +02006198 if (kvm_state->hdr.vmx.flags & ~KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE)
6199 return -EINVAL;
6200
David Brazdil0f672f62019-12-10 10:32:29 +00006201 /*
6202 * SMM temporarily disables VMX, so we cannot be in guest mode,
6203 * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags
6204 * must be zero.
6205 */
6206 if (is_smm(vcpu) ?
6207 (kvm_state->flags &
6208 (KVM_STATE_NESTED_GUEST_MODE | KVM_STATE_NESTED_RUN_PENDING))
6209 : kvm_state->hdr.vmx.smm.flags)
6210 return -EINVAL;
6211
6212 if ((kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
6213 !(kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
6214 return -EINVAL;
6215
6216 if ((kvm_state->flags & KVM_STATE_NESTED_EVMCS) &&
6217 (!nested_vmx_allowed(vcpu) || !vmx->nested.enlightened_vmcs_enabled))
6218 return -EINVAL;
6219
6220 vmx_leave_nested(vcpu);
6221
6222 if (kvm_state->hdr.vmx.vmxon_pa == -1ull)
6223 return 0;
6224
6225 vmx->nested.vmxon_ptr = kvm_state->hdr.vmx.vmxon_pa;
6226 ret = enter_vmx_operation(vcpu);
6227 if (ret)
6228 return ret;
6229
Olivier Deprez157378f2022-04-04 15:47:50 +02006230 /* Empty 'VMXON' state is permitted if no VMCS loaded */
6231 if (kvm_state->size < sizeof(*kvm_state) + sizeof(*vmcs12)) {
6232 /* See vmx_has_valid_vmcs12. */
6233 if ((kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE) ||
6234 (kvm_state->flags & KVM_STATE_NESTED_EVMCS) ||
6235 (kvm_state->hdr.vmx.vmcs12_pa != -1ull))
6236 return -EINVAL;
6237 else
6238 return 0;
6239 }
David Brazdil0f672f62019-12-10 10:32:29 +00006240
6241 if (kvm_state->hdr.vmx.vmcs12_pa != -1ull) {
6242 if (kvm_state->hdr.vmx.vmcs12_pa == kvm_state->hdr.vmx.vmxon_pa ||
6243 !page_address_valid(vcpu, kvm_state->hdr.vmx.vmcs12_pa))
6244 return -EINVAL;
6245
6246 set_current_vmptr(vmx, kvm_state->hdr.vmx.vmcs12_pa);
6247 } else if (kvm_state->flags & KVM_STATE_NESTED_EVMCS) {
6248 /*
Olivier Deprez157378f2022-04-04 15:47:50 +02006249 * nested_vmx_handle_enlightened_vmptrld() cannot be called
6250 * directly from here as HV_X64_MSR_VP_ASSIST_PAGE may not be
6251 * restored yet. EVMCS will be mapped from
6252 * nested_get_vmcs12_pages().
David Brazdil0f672f62019-12-10 10:32:29 +00006253 */
Olivier Deprez157378f2022-04-04 15:47:50 +02006254 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
David Brazdil0f672f62019-12-10 10:32:29 +00006255 } else {
6256 return -EINVAL;
6257 }
6258
6259 if (kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
6260 vmx->nested.smm.vmxon = true;
6261 vmx->nested.vmxon = false;
6262
6263 if (kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
6264 vmx->nested.smm.guest_mode = true;
6265 }
6266
6267 vmcs12 = get_vmcs12(vcpu);
6268 if (copy_from_user(vmcs12, user_vmx_nested_state->vmcs12, sizeof(*vmcs12)))
6269 return -EFAULT;
6270
6271 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
6272 return -EINVAL;
6273
6274 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
6275 return 0;
6276
6277 vmx->nested.nested_run_pending =
6278 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
6279
Olivier Deprez157378f2022-04-04 15:47:50 +02006280 vmx->nested.mtf_pending =
6281 !!(kvm_state->flags & KVM_STATE_NESTED_MTF_PENDING);
6282
David Brazdil0f672f62019-12-10 10:32:29 +00006283 ret = -EINVAL;
6284 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
6285 vmcs12->vmcs_link_pointer != -1ull) {
6286 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
6287
6288 if (kvm_state->size <
6289 sizeof(*kvm_state) +
6290 sizeof(user_vmx_nested_state->vmcs12) + sizeof(*shadow_vmcs12))
6291 goto error_guest_mode;
6292
6293 if (copy_from_user(shadow_vmcs12,
6294 user_vmx_nested_state->shadow_vmcs12,
6295 sizeof(*shadow_vmcs12))) {
6296 ret = -EFAULT;
6297 goto error_guest_mode;
6298 }
6299
6300 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
6301 !shadow_vmcs12->hdr.shadow_vmcs)
6302 goto error_guest_mode;
6303 }
6304
Olivier Deprez157378f2022-04-04 15:47:50 +02006305 vmx->nested.has_preemption_timer_deadline = false;
6306 if (kvm_state->hdr.vmx.flags & KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE) {
6307 vmx->nested.has_preemption_timer_deadline = true;
6308 vmx->nested.preemption_timer_deadline =
6309 kvm_state->hdr.vmx.preemption_timer_deadline;
6310 }
6311
David Brazdil0f672f62019-12-10 10:32:29 +00006312 if (nested_vmx_check_controls(vcpu, vmcs12) ||
6313 nested_vmx_check_host_state(vcpu, vmcs12) ||
Olivier Deprez157378f2022-04-04 15:47:50 +02006314 nested_vmx_check_guest_state(vcpu, vmcs12, &ignored))
David Brazdil0f672f62019-12-10 10:32:29 +00006315 goto error_guest_mode;
6316
6317 vmx->nested.dirty_vmcs12 = true;
6318 ret = nested_vmx_enter_non_root_mode(vcpu, false);
6319 if (ret)
6320 goto error_guest_mode;
6321
6322 return 0;
6323
6324error_guest_mode:
6325 vmx->nested.nested_run_pending = 0;
6326 return ret;
6327}
6328
Olivier Deprez157378f2022-04-04 15:47:50 +02006329void nested_vmx_set_vmcs_shadowing_bitmap(void)
David Brazdil0f672f62019-12-10 10:32:29 +00006330{
6331 if (enable_shadow_vmcs) {
6332 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
6333 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
6334 }
6335}
6336
6337/*
6338 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
6339 * returned for the various VMX controls MSRs when nested VMX is enabled.
6340 * The same values should also be used to verify that vmcs12 control fields are
6341 * valid during nested entry from L1 to L2.
6342 * Each of these control msrs has a low and high 32-bit half: A low bit is on
6343 * if the corresponding bit in the (32-bit) control field *must* be on, and a
6344 * bit in the high half is on if the corresponding bit in the control field
6345 * may be on. See also vmx_control_verify().
6346 */
Olivier Deprez0e641232021-09-23 10:07:05 +02006347void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, u32 ept_caps)
David Brazdil0f672f62019-12-10 10:32:29 +00006348{
6349 /*
6350 * Note that as a general rule, the high half of the MSRs (bits in
6351 * the control fields which may be 1) should be initialized by the
6352 * intersection of the underlying hardware's MSR (i.e., features which
6353 * can be supported) and the list of features we want to expose -
6354 * because they are known to be properly supported in our code.
6355 * Also, usually, the low half of the MSRs (bits which must be 1) can
6356 * be set to 0, meaning that L1 may turn off any of these bits. The
6357 * reason is that if one of these bits is necessary, it will appear
6358 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
6359 * fields of vmcs01 and vmcs02, will turn these bits off - and
Olivier Deprez157378f2022-04-04 15:47:50 +02006360 * nested_vmx_l1_wants_exit() will not pass related exits to L1.
David Brazdil0f672f62019-12-10 10:32:29 +00006361 * These rules have exceptions below.
6362 */
6363
6364 /* pin-based controls */
6365 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
6366 msrs->pinbased_ctls_low,
6367 msrs->pinbased_ctls_high);
6368 msrs->pinbased_ctls_low |=
6369 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
6370 msrs->pinbased_ctls_high &=
6371 PIN_BASED_EXT_INTR_MASK |
6372 PIN_BASED_NMI_EXITING |
6373 PIN_BASED_VIRTUAL_NMIS |
Olivier Deprez0e641232021-09-23 10:07:05 +02006374 (enable_apicv ? PIN_BASED_POSTED_INTR : 0);
David Brazdil0f672f62019-12-10 10:32:29 +00006375 msrs->pinbased_ctls_high |=
6376 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
6377 PIN_BASED_VMX_PREEMPTION_TIMER;
6378
6379 /* exit controls */
6380 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
6381 msrs->exit_ctls_low,
6382 msrs->exit_ctls_high);
6383 msrs->exit_ctls_low =
6384 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
6385
6386 msrs->exit_ctls_high &=
6387#ifdef CONFIG_X86_64
6388 VM_EXIT_HOST_ADDR_SPACE_SIZE |
6389#endif
Olivier Deprez157378f2022-04-04 15:47:50 +02006390 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT |
6391 VM_EXIT_CLEAR_BNDCFGS | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
David Brazdil0f672f62019-12-10 10:32:29 +00006392 msrs->exit_ctls_high |=
6393 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
6394 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
6395 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
6396
6397 /* We support free control of debug control saving. */
6398 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
6399
6400 /* entry controls */
6401 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
6402 msrs->entry_ctls_low,
6403 msrs->entry_ctls_high);
6404 msrs->entry_ctls_low =
6405 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
6406 msrs->entry_ctls_high &=
6407#ifdef CONFIG_X86_64
6408 VM_ENTRY_IA32E_MODE |
6409#endif
Olivier Deprez157378f2022-04-04 15:47:50 +02006410 VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS |
6411 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
David Brazdil0f672f62019-12-10 10:32:29 +00006412 msrs->entry_ctls_high |=
6413 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
6414
6415 /* We support free control of debug control loading. */
6416 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
6417
6418 /* cpu-based controls */
6419 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
6420 msrs->procbased_ctls_low,
6421 msrs->procbased_ctls_high);
6422 msrs->procbased_ctls_low =
6423 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
6424 msrs->procbased_ctls_high &=
Olivier Deprez157378f2022-04-04 15:47:50 +02006425 CPU_BASED_INTR_WINDOW_EXITING |
6426 CPU_BASED_NMI_WINDOW_EXITING | CPU_BASED_USE_TSC_OFFSETTING |
David Brazdil0f672f62019-12-10 10:32:29 +00006427 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
6428 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
6429 CPU_BASED_CR3_STORE_EXITING |
6430#ifdef CONFIG_X86_64
6431 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
6432#endif
6433 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
6434 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
6435 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
6436 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
6437 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
6438 /*
6439 * We can allow some features even when not supported by the
6440 * hardware. For example, L1 can specify an MSR bitmap - and we
6441 * can use it to avoid exits to L1 - even when L0 runs L2
6442 * without MSR bitmaps.
6443 */
6444 msrs->procbased_ctls_high |=
6445 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
6446 CPU_BASED_USE_MSR_BITMAPS;
6447
6448 /* We support free control of CR3 access interception. */
6449 msrs->procbased_ctls_low &=
6450 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
6451
6452 /*
6453 * secondary cpu-based controls. Do not include those that
Olivier Deprez157378f2022-04-04 15:47:50 +02006454 * depend on CPUID bits, they are added later by
6455 * vmx_vcpu_after_set_cpuid.
David Brazdil0f672f62019-12-10 10:32:29 +00006456 */
6457 if (msrs->procbased_ctls_high & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)
6458 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
6459 msrs->secondary_ctls_low,
6460 msrs->secondary_ctls_high);
6461
6462 msrs->secondary_ctls_low = 0;
6463 msrs->secondary_ctls_high &=
6464 SECONDARY_EXEC_DESC |
Olivier Deprez157378f2022-04-04 15:47:50 +02006465 SECONDARY_EXEC_ENABLE_RDTSCP |
David Brazdil0f672f62019-12-10 10:32:29 +00006466 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
6467 SECONDARY_EXEC_WBINVD_EXITING |
6468 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6469 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
6470 SECONDARY_EXEC_RDRAND_EXITING |
6471 SECONDARY_EXEC_ENABLE_INVPCID |
6472 SECONDARY_EXEC_RDSEED_EXITING |
6473 SECONDARY_EXEC_XSAVES;
6474
6475 /*
6476 * We can emulate "VMCS shadowing," even if the hardware
6477 * doesn't support it.
6478 */
6479 msrs->secondary_ctls_high |=
6480 SECONDARY_EXEC_SHADOW_VMCS;
6481
6482 if (enable_ept) {
6483 /* nested EPT: emulate EPT also to L1 */
6484 msrs->secondary_ctls_high |=
6485 SECONDARY_EXEC_ENABLE_EPT;
Olivier Deprez157378f2022-04-04 15:47:50 +02006486 msrs->ept_caps =
6487 VMX_EPT_PAGE_WALK_4_BIT |
6488 VMX_EPT_PAGE_WALK_5_BIT |
6489 VMX_EPTP_WB_BIT |
6490 VMX_EPT_INVEPT_BIT |
6491 VMX_EPT_EXECUTE_ONLY_BIT;
6492
David Brazdil0f672f62019-12-10 10:32:29 +00006493 msrs->ept_caps &= ept_caps;
6494 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
6495 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
6496 VMX_EPT_1GB_PAGE_BIT;
6497 if (enable_ept_ad_bits) {
6498 msrs->secondary_ctls_high |=
6499 SECONDARY_EXEC_ENABLE_PML;
6500 msrs->ept_caps |= VMX_EPT_AD_BIT;
6501 }
6502 }
6503
6504 if (cpu_has_vmx_vmfunc()) {
6505 msrs->secondary_ctls_high |=
6506 SECONDARY_EXEC_ENABLE_VMFUNC;
6507 /*
6508 * Advertise EPTP switching unconditionally
6509 * since we emulate it
6510 */
6511 if (enable_ept)
6512 msrs->vmfunc_controls =
6513 VMX_VMFUNC_EPTP_SWITCHING;
6514 }
6515
6516 /*
6517 * Old versions of KVM use the single-context version without
6518 * checking for support, so declare that it is supported even
6519 * though it is treated as global context. The alternative is
6520 * not failing the single-context invvpid, and it is worse.
6521 */
6522 if (enable_vpid) {
6523 msrs->secondary_ctls_high |=
6524 SECONDARY_EXEC_ENABLE_VPID;
6525 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
6526 VMX_VPID_EXTENT_SUPPORTED_MASK;
6527 }
6528
6529 if (enable_unrestricted_guest)
6530 msrs->secondary_ctls_high |=
6531 SECONDARY_EXEC_UNRESTRICTED_GUEST;
6532
6533 if (flexpriority_enabled)
6534 msrs->secondary_ctls_high |=
6535 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6536
6537 /* miscellaneous data */
6538 rdmsr(MSR_IA32_VMX_MISC,
6539 msrs->misc_low,
6540 msrs->misc_high);
6541 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
6542 msrs->misc_low |=
6543 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
6544 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
6545 VMX_MISC_ACTIVITY_HLT;
6546 msrs->misc_high = 0;
6547
6548 /*
6549 * This MSR reports some information about VMX support. We
6550 * should return information about the VMX we emulate for the
6551 * guest, and the VMCS structure we give it - not about the
6552 * VMX support of the underlying hardware.
6553 */
6554 msrs->basic =
6555 VMCS12_REVISION |
6556 VMX_BASIC_TRUE_CTLS |
6557 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
6558 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
6559
6560 if (cpu_has_vmx_basic_inout())
6561 msrs->basic |= VMX_BASIC_INOUT;
6562
6563 /*
6564 * These MSRs specify bits which the guest must keep fixed on
6565 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
6566 * We picked the standard core2 setting.
6567 */
6568#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
6569#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
6570 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
6571 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
6572
6573 /* These MSRs specify bits which the guest must keep fixed off. */
6574 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
6575 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
6576
6577 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
6578 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
6579}
6580
6581void nested_vmx_hardware_unsetup(void)
6582{
6583 int i;
6584
6585 if (enable_shadow_vmcs) {
6586 for (i = 0; i < VMX_BITMAP_NR; i++)
6587 free_page((unsigned long)vmx_bitmap[i]);
6588 }
6589}
6590
6591__init int nested_vmx_hardware_setup(int (*exit_handlers[])(struct kvm_vcpu *))
6592{
6593 int i;
6594
6595 if (!cpu_has_vmx_shadow_vmcs())
6596 enable_shadow_vmcs = 0;
6597 if (enable_shadow_vmcs) {
6598 for (i = 0; i < VMX_BITMAP_NR; i++) {
6599 /*
6600 * The vmx_bitmap is not tied to a VM and so should
6601 * not be charged to a memcg.
6602 */
6603 vmx_bitmap[i] = (unsigned long *)
6604 __get_free_page(GFP_KERNEL);
6605 if (!vmx_bitmap[i]) {
6606 nested_vmx_hardware_unsetup();
6607 return -ENOMEM;
6608 }
6609 }
6610
6611 init_vmcs_shadow_fields();
6612 }
6613
Olivier Deprez157378f2022-04-04 15:47:50 +02006614 exit_handlers[EXIT_REASON_VMCLEAR] = handle_vmclear;
6615 exit_handlers[EXIT_REASON_VMLAUNCH] = handle_vmlaunch;
6616 exit_handlers[EXIT_REASON_VMPTRLD] = handle_vmptrld;
6617 exit_handlers[EXIT_REASON_VMPTRST] = handle_vmptrst;
6618 exit_handlers[EXIT_REASON_VMREAD] = handle_vmread;
6619 exit_handlers[EXIT_REASON_VMRESUME] = handle_vmresume;
6620 exit_handlers[EXIT_REASON_VMWRITE] = handle_vmwrite;
6621 exit_handlers[EXIT_REASON_VMOFF] = handle_vmoff;
6622 exit_handlers[EXIT_REASON_VMON] = handle_vmon;
6623 exit_handlers[EXIT_REASON_INVEPT] = handle_invept;
6624 exit_handlers[EXIT_REASON_INVVPID] = handle_invvpid;
6625 exit_handlers[EXIT_REASON_VMFUNC] = handle_vmfunc;
David Brazdil0f672f62019-12-10 10:32:29 +00006626
6627 return 0;
6628}
Olivier Deprez157378f2022-04-04 15:47:50 +02006629
6630struct kvm_x86_nested_ops vmx_nested_ops = {
6631 .leave_nested = vmx_leave_nested,
6632 .check_events = vmx_check_nested_events,
6633 .hv_timer_pending = nested_vmx_preemption_timer_pending,
6634 .get_state = vmx_get_nested_state,
6635 .set_state = vmx_set_nested_state,
6636 .get_nested_state_pages = vmx_get_nested_state_pages,
6637 .write_log_dirty = nested_vmx_write_pml_buffer,
6638 .enable_evmcs = nested_enable_evmcs,
6639 .get_evmcs_version = nested_get_evmcs_version,
6640};