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