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