blob: 376fd9eabe4269391c36b584df29b90b586e5a51 [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;
1618 * sync_vmcs12() doesn't read these:
1619 * 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
1842void nested_sync_from_vmcs12(struct kvm_vcpu *vcpu)
1843{
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
1863 vmx->nested.need_vmcs12_sync = false;
1864}
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
1958static void prepare_vmcs02_early_full(struct vcpu_vmx *vmx,
1959 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)
1979 prepare_vmcs02_early_full(vmx, vmcs12);
1980
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
2133static void prepare_vmcs02_full(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
2134{
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) {
2257 prepare_vmcs02_full(vmx, vmcs12);
2258 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)
3045 vmx->nested.need_vmcs12_sync = true;
3046 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
3379/*
3380 * Update the guest state fields of vmcs12 to reflect changes that
3381 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
3382 * VM-entry controls is also updated, since this is really a guest
3383 * state bit.)
3384 */
3385static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
3386{
3387 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
3388 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
3389
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02003390 vmcs12->guest_rsp = kvm_rsp_read(vcpu);
3391 vmcs12->guest_rip = kvm_rip_read(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08003392 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
3393
3394 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
3395 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
3396 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
3397 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
3398 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
3399 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
3400 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
3401 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
3402 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
3403 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
3404 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
3405 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
3406 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
3407 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
3408 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
3409 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
3410 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
3411 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
3412 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
3413 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
3414 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
3415 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
3416 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
3417 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
3418 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
3419 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
3420 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
3421 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
3422 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
3423 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
3424 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
3425 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
3426 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
3427 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
3428 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
3429 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
3430
3431 vmcs12->guest_interruptibility_info =
3432 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3433 vmcs12->guest_pending_dbg_exceptions =
3434 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
3435 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
3436 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
3437 else
3438 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
3439
Paolo Bonzinib4b65b52019-01-29 19:12:35 +01003440 if (nested_cpu_has_preemption_timer(vmcs12) &&
3441 vmcs12->vm_exit_controls & VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
Sean Christopherson55d23752018-12-03 13:53:18 -08003442 vmcs12->vmx_preemption_timer_value =
3443 vmx_get_preemption_timer_value(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08003444
3445 /*
3446 * In some cases (usually, nested EPT), L2 is allowed to change its
3447 * own CR3 without exiting. If it has changed it, we must keep it.
3448 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
3449 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
3450 *
3451 * Additionally, restore L2's PDPTR to vmcs12.
3452 */
3453 if (enable_ept) {
3454 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
3455 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
3456 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
3457 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
3458 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
3459 }
3460
3461 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
3462
3463 if (nested_cpu_has_vid(vmcs12))
3464 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
3465
3466 vmcs12->vm_entry_controls =
3467 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
3468 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
3469
3470 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
3471 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
3472 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
3473 }
3474
3475 /* TODO: These cannot have changed unless we have MSR bitmaps and
3476 * the relevant bit asks not to trap the change */
3477 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
3478 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
3479 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
3480 vmcs12->guest_ia32_efer = vcpu->arch.efer;
3481 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
3482 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
3483 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
3484 if (kvm_mpx_supported())
3485 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
3486}
3487
3488/*
3489 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
3490 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
3491 * and this function updates it to reflect the changes to the guest state while
3492 * L2 was running (and perhaps made some exits which were handled directly by L0
3493 * without going back to L1), and to reflect the exit reason.
3494 * Note that we do not have to copy here all VMCS fields, just those that
3495 * could have changed by the L2 guest or the exit - i.e., the guest-state and
3496 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
3497 * which already writes to vmcs12 directly.
3498 */
3499static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
3500 u32 exit_reason, u32 exit_intr_info,
3501 unsigned long exit_qualification)
3502{
3503 /* update guest state fields: */
3504 sync_vmcs12(vcpu, vmcs12);
3505
3506 /* update exit information fields: */
3507
3508 vmcs12->vm_exit_reason = exit_reason;
3509 vmcs12->exit_qualification = exit_qualification;
3510 vmcs12->vm_exit_intr_info = exit_intr_info;
3511
3512 vmcs12->idt_vectoring_info_field = 0;
3513 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
3514 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
3515
3516 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
3517 vmcs12->launch_state = 1;
3518
3519 /* vm_entry_intr_info_field is cleared on exit. Emulate this
3520 * instead of reading the real value. */
3521 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
3522
3523 /*
3524 * Transfer the event that L0 or L1 may wanted to inject into
3525 * L2 to IDT_VECTORING_INFO_FIELD.
3526 */
3527 vmcs12_save_pending_event(vcpu, vmcs12);
Krish Sadhukhana0d4f802018-12-04 19:00:13 -05003528
3529 /*
3530 * According to spec, there's no need to store the guest's
3531 * MSRs if the exit is due to a VM-entry failure that occurs
3532 * during or after loading the guest state. Since this exit
3533 * does not fall in that category, we need to save the MSRs.
3534 */
3535 if (nested_vmx_store_msr(vcpu,
3536 vmcs12->vm_exit_msr_store_addr,
3537 vmcs12->vm_exit_msr_store_count))
3538 nested_vmx_abort(vcpu,
3539 VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Sean Christopherson55d23752018-12-03 13:53:18 -08003540 }
3541
3542 /*
3543 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
3544 * preserved above and would only end up incorrectly in L1.
3545 */
3546 vcpu->arch.nmi_injected = false;
3547 kvm_clear_exception_queue(vcpu);
3548 kvm_clear_interrupt_queue(vcpu);
3549}
3550
3551/*
3552 * A part of what we need to when the nested L2 guest exits and we want to
3553 * run its L1 parent, is to reset L1's guest state to the host state specified
3554 * in vmcs12.
3555 * This function is to be called not only on normal nested exit, but also on
3556 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
3557 * Failures During or After Loading Guest State").
3558 * This function should be called when the active VMCS is L1's (vmcs01).
3559 */
3560static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
3561 struct vmcs12 *vmcs12)
3562{
3563 struct kvm_segment seg;
3564 u32 entry_failure_code;
3565
3566 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
3567 vcpu->arch.efer = vmcs12->host_ia32_efer;
3568 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
3569 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
3570 else
3571 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
3572 vmx_set_efer(vcpu, vcpu->arch.efer);
3573
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02003574 kvm_rsp_write(vcpu, vmcs12->host_rsp);
3575 kvm_rip_write(vcpu, vmcs12->host_rip);
Sean Christopherson55d23752018-12-03 13:53:18 -08003576 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
3577 vmx_set_interrupt_shadow(vcpu, 0);
3578
3579 /*
3580 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
3581 * actually changed, because vmx_set_cr0 refers to efer set above.
3582 *
3583 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
3584 * (KVM doesn't change it);
3585 */
3586 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
3587 vmx_set_cr0(vcpu, vmcs12->host_cr0);
3588
3589 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
3590 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
3591 vmx_set_cr4(vcpu, vmcs12->host_cr4);
3592
3593 nested_ept_uninit_mmu_context(vcpu);
3594
3595 /*
3596 * Only PDPTE load can fail as the value of cr3 was checked on entry and
3597 * couldn't have changed.
3598 */
3599 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
3600 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
3601
3602 if (!enable_ept)
3603 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
3604
3605 /*
3606 * If vmcs01 doesn't use VPID, CPU flushes TLB on every
3607 * VMEntry/VMExit. Thus, no need to flush TLB.
3608 *
3609 * If vmcs12 doesn't use VPID, L1 expects TLB to be
3610 * flushed on every VMEntry/VMExit.
3611 *
3612 * Otherwise, we can preserve TLB entries as long as we are
3613 * able to tag L1 TLB entries differently than L2 TLB entries.
3614 *
3615 * If vmcs12 uses EPT, we need to execute this flush on EPTP01
3616 * and therefore we request the TLB flush to happen only after VMCS EPTP
3617 * has been set by KVM_REQ_LOAD_CR3.
3618 */
3619 if (enable_vpid &&
3620 (!nested_cpu_has_vpid(vmcs12) || !nested_has_guest_tlb_tag(vcpu))) {
3621 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
3622 }
3623
3624 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
3625 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
3626 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
3627 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
3628 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
3629 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
3630 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
3631
3632 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
3633 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
3634 vmcs_write64(GUEST_BNDCFGS, 0);
3635
3636 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
3637 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
3638 vcpu->arch.pat = vmcs12->host_ia32_pat;
3639 }
3640 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
3641 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
3642 vmcs12->host_ia32_perf_global_ctrl);
3643
3644 /* Set L1 segment info according to Intel SDM
3645 27.5.2 Loading Host Segment and Descriptor-Table Registers */
3646 seg = (struct kvm_segment) {
3647 .base = 0,
3648 .limit = 0xFFFFFFFF,
3649 .selector = vmcs12->host_cs_selector,
3650 .type = 11,
3651 .present = 1,
3652 .s = 1,
3653 .g = 1
3654 };
3655 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
3656 seg.l = 1;
3657 else
3658 seg.db = 1;
3659 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
3660 seg = (struct kvm_segment) {
3661 .base = 0,
3662 .limit = 0xFFFFFFFF,
3663 .type = 3,
3664 .present = 1,
3665 .s = 1,
3666 .db = 1,
3667 .g = 1
3668 };
3669 seg.selector = vmcs12->host_ds_selector;
3670 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
3671 seg.selector = vmcs12->host_es_selector;
3672 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
3673 seg.selector = vmcs12->host_ss_selector;
3674 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
3675 seg.selector = vmcs12->host_fs_selector;
3676 seg.base = vmcs12->host_fs_base;
3677 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
3678 seg.selector = vmcs12->host_gs_selector;
3679 seg.base = vmcs12->host_gs_base;
3680 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
3681 seg = (struct kvm_segment) {
3682 .base = vmcs12->host_tr_base,
3683 .limit = 0x67,
3684 .selector = vmcs12->host_tr_selector,
3685 .type = 11,
3686 .present = 1
3687 };
3688 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
3689
3690 kvm_set_dr(vcpu, 7, 0x400);
3691 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
3692
3693 if (cpu_has_vmx_msr_bitmap())
3694 vmx_update_msr_bitmap(vcpu);
3695
3696 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
3697 vmcs12->vm_exit_msr_load_count))
3698 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
3699}
3700
3701static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx)
3702{
3703 struct shared_msr_entry *efer_msr;
3704 unsigned int i;
3705
3706 if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER)
3707 return vmcs_read64(GUEST_IA32_EFER);
3708
3709 if (cpu_has_load_ia32_efer())
3710 return host_efer;
3711
3712 for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) {
3713 if (vmx->msr_autoload.guest.val[i].index == MSR_EFER)
3714 return vmx->msr_autoload.guest.val[i].value;
3715 }
3716
3717 efer_msr = find_msr_entry(vmx, MSR_EFER);
3718 if (efer_msr)
3719 return efer_msr->data;
3720
3721 return host_efer;
3722}
3723
3724static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu)
3725{
3726 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3727 struct vcpu_vmx *vmx = to_vmx(vcpu);
3728 struct vmx_msr_entry g, h;
3729 struct msr_data msr;
3730 gpa_t gpa;
3731 u32 i, j;
3732
3733 vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT);
3734
3735 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
3736 /*
3737 * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set
3738 * as vmcs01.GUEST_DR7 contains a userspace defined value
3739 * and vcpu->arch.dr7 is not squirreled away before the
3740 * nested VMENTER (not worth adding a variable in nested_vmx).
3741 */
3742 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
3743 kvm_set_dr(vcpu, 7, DR7_FIXED_1);
3744 else
3745 WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7)));
3746 }
3747
3748 /*
3749 * Note that calling vmx_set_{efer,cr0,cr4} is important as they
3750 * handle a variety of side effects to KVM's software model.
3751 */
3752 vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx));
3753
3754 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
3755 vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW));
3756
3757 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
3758 vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW));
3759
3760 nested_ept_uninit_mmu_context(vcpu);
Paolo Bonzini2b279242019-04-15 15:57:19 +02003761
3762 /*
3763 * This is only valid if EPT is in use, otherwise the vmcs01 GUEST_CR3
3764 * points to shadow pages! Fortunately we only get here after a WARN_ON
3765 * if EPT is disabled, so a VMabort is perfectly fine.
3766 */
3767 if (enable_ept) {
3768 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
3769 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
3770 } else {
3771 nested_vmx_abort(vcpu, VMX_ABORT_VMCS_CORRUPTED);
3772 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003773
3774 /*
3775 * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs
3776 * from vmcs01 (if necessary). The PDPTRs are not loaded on
3777 * VMFail, like everything else we just need to ensure our
3778 * software model is up-to-date.
3779 */
3780 ept_save_pdptrs(vcpu);
3781
3782 kvm_mmu_reset_context(vcpu);
3783
3784 if (cpu_has_vmx_msr_bitmap())
3785 vmx_update_msr_bitmap(vcpu);
3786
3787 /*
3788 * This nasty bit of open coding is a compromise between blindly
3789 * loading L1's MSRs using the exit load lists (incorrect emulation
3790 * of VMFail), leaving the nested VM's MSRs in the software model
3791 * (incorrect behavior) and snapshotting the modified MSRs (too
3792 * expensive since the lists are unbound by hardware). For each
3793 * MSR that was (prematurely) loaded from the nested VMEntry load
3794 * list, reload it from the exit load list if it exists and differs
3795 * from the guest value. The intent is to stuff host state as
3796 * silently as possible, not to fully process the exit load list.
3797 */
3798 msr.host_initiated = false;
3799 for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) {
3800 gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g));
3801 if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) {
3802 pr_debug_ratelimited(
3803 "%s read MSR index failed (%u, 0x%08llx)\n",
3804 __func__, i, gpa);
3805 goto vmabort;
3806 }
3807
3808 for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) {
3809 gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h));
3810 if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) {
3811 pr_debug_ratelimited(
3812 "%s read MSR failed (%u, 0x%08llx)\n",
3813 __func__, j, gpa);
3814 goto vmabort;
3815 }
3816 if (h.index != g.index)
3817 continue;
3818 if (h.value == g.value)
3819 break;
3820
3821 if (nested_vmx_load_msr_check(vcpu, &h)) {
3822 pr_debug_ratelimited(
3823 "%s check failed (%u, 0x%x, 0x%x)\n",
3824 __func__, j, h.index, h.reserved);
3825 goto vmabort;
3826 }
3827
3828 msr.index = h.index;
3829 msr.data = h.value;
3830 if (kvm_set_msr(vcpu, &msr)) {
3831 pr_debug_ratelimited(
3832 "%s WRMSR failed (%u, 0x%x, 0x%llx)\n",
3833 __func__, j, h.index, h.value);
3834 goto vmabort;
3835 }
3836 }
3837 }
3838
3839 return;
3840
3841vmabort:
3842 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
3843}
3844
3845/*
3846 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
3847 * and modify vmcs12 to make it see what it would expect to see there if
3848 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
3849 */
3850void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
3851 u32 exit_intr_info, unsigned long exit_qualification)
3852{
3853 struct vcpu_vmx *vmx = to_vmx(vcpu);
3854 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3855
3856 /* trying to cancel vmlaunch/vmresume is a bug */
3857 WARN_ON_ONCE(vmx->nested.nested_run_pending);
3858
3859 leave_guest_mode(vcpu);
3860
Paolo Bonzinib4b65b52019-01-29 19:12:35 +01003861 if (nested_cpu_has_preemption_timer(vmcs12))
3862 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
3863
Sean Christopherson55d23752018-12-03 13:53:18 -08003864 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
3865 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
3866
3867 if (likely(!vmx->fail)) {
3868 if (exit_reason == -1)
3869 sync_vmcs12(vcpu, vmcs12);
3870 else
3871 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
3872 exit_qualification);
3873
3874 /*
3875 * Must happen outside of sync_vmcs12() as it will
3876 * also be used to capture vmcs12 cache as part of
3877 * capturing nVMX state for snapshot (migration).
3878 *
3879 * Otherwise, this flush will dirty guest memory at a
3880 * point it is already assumed by user-space to be
3881 * immutable.
3882 */
3883 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08003884 } else {
3885 /*
3886 * The only expected VM-instruction error is "VM entry with
3887 * invalid control field(s)." Anything else indicates a
3888 * problem with L0. And we should never get here with a
3889 * VMFail of any type if early consistency checks are enabled.
3890 */
3891 WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) !=
3892 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
3893 WARN_ON_ONCE(nested_early_check);
3894 }
3895
3896 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
3897
3898 /* Update any VMCS fields that might have changed while L2 ran */
3899 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
3900 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
3901 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
3902
3903 if (kvm_has_tsc_control)
3904 decache_tsc_multiplier(vmx);
3905
3906 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
3907 vmx->nested.change_vmcs01_virtual_apic_mode = false;
3908 vmx_set_virtual_apic_mode(vcpu);
3909 } else if (!nested_cpu_has_ept(vmcs12) &&
3910 nested_cpu_has2(vmcs12,
3911 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
3912 vmx_flush_tlb(vcpu, true);
3913 }
3914
Sean Christopherson55d23752018-12-03 13:53:18 -08003915 /* Unpin physical memory we referred to in vmcs02 */
3916 if (vmx->nested.apic_access_page) {
3917 kvm_release_page_dirty(vmx->nested.apic_access_page);
3918 vmx->nested.apic_access_page = NULL;
3919 }
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003920 kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map, true);
KarimAllah Ahmed3278e042019-01-31 21:24:38 +01003921 kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map, true);
3922 vmx->nested.pi_desc = NULL;
Sean Christopherson55d23752018-12-03 13:53:18 -08003923
3924 /*
3925 * We are now running in L2, mmu_notifier will force to reload the
3926 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
3927 */
3928 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
3929
3930 if ((exit_reason != -1) && (enable_shadow_vmcs || vmx->nested.hv_evmcs))
3931 vmx->nested.need_vmcs12_sync = true;
3932
3933 /* in case we halted in L2 */
3934 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
3935
3936 if (likely(!vmx->fail)) {
3937 /*
3938 * TODO: SDM says that with acknowledge interrupt on
3939 * exit, bit 31 of the VM-exit interrupt information
3940 * (valid interrupt) is always set to 1 on
3941 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
3942 * need kvm_cpu_has_interrupt(). See the commit
3943 * message for details.
3944 */
3945 if (nested_exit_intr_ack_set(vcpu) &&
3946 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
3947 kvm_cpu_has_interrupt(vcpu)) {
3948 int irq = kvm_cpu_get_interrupt(vcpu);
3949 WARN_ON(irq < 0);
3950 vmcs12->vm_exit_intr_info = irq |
3951 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
3952 }
3953
3954 if (exit_reason != -1)
3955 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
3956 vmcs12->exit_qualification,
3957 vmcs12->idt_vectoring_info_field,
3958 vmcs12->vm_exit_intr_info,
3959 vmcs12->vm_exit_intr_error_code,
3960 KVM_ISA_VMX);
3961
3962 load_vmcs12_host_state(vcpu, vmcs12);
3963
3964 return;
3965 }
3966
3967 /*
3968 * After an early L2 VM-entry failure, we're now back
3969 * in L1 which thinks it just finished a VMLAUNCH or
3970 * VMRESUME instruction, so we need to set the failure
3971 * flag and the VM-instruction error field of the VMCS
3972 * accordingly, and skip the emulated instruction.
3973 */
3974 (void)nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
3975
3976 /*
3977 * Restore L1's host state to KVM's software model. We're here
3978 * because a consistency check was caught by hardware, which
3979 * means some amount of guest state has been propagated to KVM's
3980 * model and needs to be unwound to the host's state.
3981 */
3982 nested_vmx_restore_host_state(vcpu);
3983
3984 vmx->fail = 0;
3985}
3986
3987/*
3988 * Decode the memory-address operand of a vmx instruction, as recorded on an
3989 * exit caused by such an instruction (run by a guest hypervisor).
3990 * On success, returns 0. When the operand is invalid, returns 1 and throws
3991 * #UD or #GP.
3992 */
3993int get_vmx_mem_address(struct kvm_vcpu *vcpu, unsigned long exit_qualification,
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03003994 u32 vmx_instruction_info, bool wr, int len, gva_t *ret)
Sean Christopherson55d23752018-12-03 13:53:18 -08003995{
3996 gva_t off;
3997 bool exn;
3998 struct kvm_segment s;
3999
4000 /*
4001 * According to Vol. 3B, "Information for VM Exits Due to Instruction
4002 * Execution", on an exit, vmx_instruction_info holds most of the
4003 * addressing components of the operand. Only the displacement part
4004 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
4005 * For how an actual address is calculated from all these components,
4006 * refer to Vol. 1, "Operand Addressing".
4007 */
4008 int scaling = vmx_instruction_info & 3;
4009 int addr_size = (vmx_instruction_info >> 7) & 7;
4010 bool is_reg = vmx_instruction_info & (1u << 10);
4011 int seg_reg = (vmx_instruction_info >> 15) & 7;
4012 int index_reg = (vmx_instruction_info >> 18) & 0xf;
4013 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
4014 int base_reg = (vmx_instruction_info >> 23) & 0xf;
4015 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
4016
4017 if (is_reg) {
4018 kvm_queue_exception(vcpu, UD_VECTOR);
4019 return 1;
4020 }
4021
4022 /* Addr = segment_base + offset */
4023 /* offset = base + [index * scale] + displacement */
4024 off = exit_qualification; /* holds the displacement */
Sean Christopherson946c5222019-01-23 14:39:23 -08004025 if (addr_size == 1)
4026 off = (gva_t)sign_extend64(off, 31);
4027 else if (addr_size == 0)
4028 off = (gva_t)sign_extend64(off, 15);
Sean Christopherson55d23752018-12-03 13:53:18 -08004029 if (base_is_valid)
4030 off += kvm_register_read(vcpu, base_reg);
4031 if (index_is_valid)
4032 off += kvm_register_read(vcpu, index_reg)<<scaling;
4033 vmx_get_segment(vcpu, &s, seg_reg);
Sean Christopherson55d23752018-12-03 13:53:18 -08004034
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004035 /*
4036 * The effective address, i.e. @off, of a memory operand is truncated
4037 * based on the address size of the instruction. Note that this is
4038 * the *effective address*, i.e. the address prior to accounting for
4039 * the segment's base.
4040 */
Sean Christopherson55d23752018-12-03 13:53:18 -08004041 if (addr_size == 1) /* 32 bit */
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004042 off &= 0xffffffff;
4043 else if (addr_size == 0) /* 16 bit */
4044 off &= 0xffff;
Sean Christopherson55d23752018-12-03 13:53:18 -08004045
4046 /* Checks for #GP/#SS exceptions. */
4047 exn = false;
4048 if (is_long_mode(vcpu)) {
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004049 /*
4050 * The virtual/linear address is never truncated in 64-bit
4051 * mode, e.g. a 32-bit address size can yield a 64-bit virtual
4052 * address when using FS/GS with a non-zero base.
4053 */
4054 *ret = s.base + off;
4055
Sean Christopherson55d23752018-12-03 13:53:18 -08004056 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
4057 * non-canonical form. This is the only check on the memory
4058 * destination for long mode!
4059 */
4060 exn = is_noncanonical_address(*ret, vcpu);
Paolo Bonzinie0dfacb2019-01-30 17:25:38 +01004061 } else {
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004062 /*
4063 * When not in long mode, the virtual/linear address is
4064 * unconditionally truncated to 32 bits regardless of the
4065 * address size.
4066 */
4067 *ret = (s.base + off) & 0xffffffff;
4068
Sean Christopherson55d23752018-12-03 13:53:18 -08004069 /* Protected mode: apply checks for segment validity in the
4070 * following order:
4071 * - segment type check (#GP(0) may be thrown)
4072 * - usability check (#GP(0)/#SS(0))
4073 * - limit check (#GP(0)/#SS(0))
4074 */
4075 if (wr)
4076 /* #GP(0) if the destination operand is located in a
4077 * read-only data segment or any code segment.
4078 */
4079 exn = ((s.type & 0xa) == 0 || (s.type & 8));
4080 else
4081 /* #GP(0) if the source operand is located in an
4082 * execute-only code segment
4083 */
4084 exn = ((s.type & 0xa) == 8);
4085 if (exn) {
4086 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
4087 return 1;
4088 }
4089 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
4090 */
4091 exn = (s.unusable != 0);
Sean Christopherson34333cc2019-01-23 14:39:25 -08004092
4093 /*
4094 * Protected mode: #GP(0)/#SS(0) if the memory operand is
4095 * outside the segment limit. All CPUs that support VMX ignore
4096 * limit checks for flat segments, i.e. segments with base==0,
4097 * limit==0xffffffff and of type expand-up data or code.
Sean Christopherson55d23752018-12-03 13:53:18 -08004098 */
Sean Christopherson34333cc2019-01-23 14:39:25 -08004099 if (!(s.base == 0 && s.limit == 0xffffffff &&
4100 ((s.type & 8) || !(s.type & 4))))
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004101 exn = exn || ((u64)off + len - 1 > s.limit);
Sean Christopherson55d23752018-12-03 13:53:18 -08004102 }
4103 if (exn) {
4104 kvm_queue_exception_e(vcpu,
4105 seg_reg == VCPU_SREG_SS ?
4106 SS_VECTOR : GP_VECTOR,
4107 0);
4108 return 1;
4109 }
4110
4111 return 0;
4112}
4113
4114static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
4115{
4116 gva_t gva;
4117 struct x86_exception e;
4118
4119 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004120 vmcs_read32(VMX_INSTRUCTION_INFO), false,
4121 sizeof(*vmpointer), &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08004122 return 1;
4123
4124 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
4125 kvm_inject_page_fault(vcpu, &e);
4126 return 1;
4127 }
4128
4129 return 0;
4130}
4131
4132/*
4133 * Allocate a shadow VMCS and associate it with the currently loaded
4134 * VMCS, unless such a shadow VMCS already exists. The newly allocated
4135 * VMCS is also VMCLEARed, so that it is ready for use.
4136 */
4137static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
4138{
4139 struct vcpu_vmx *vmx = to_vmx(vcpu);
4140 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
4141
4142 /*
4143 * We should allocate a shadow vmcs for vmcs01 only when L1
4144 * executes VMXON and free it when L1 executes VMXOFF.
4145 * As it is invalid to execute VMXON twice, we shouldn't reach
4146 * here when vmcs01 already have an allocated shadow vmcs.
4147 */
4148 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
4149
4150 if (!loaded_vmcs->shadow_vmcs) {
4151 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
4152 if (loaded_vmcs->shadow_vmcs)
4153 vmcs_clear(loaded_vmcs->shadow_vmcs);
4154 }
4155 return loaded_vmcs->shadow_vmcs;
4156}
4157
4158static int enter_vmx_operation(struct kvm_vcpu *vcpu)
4159{
4160 struct vcpu_vmx *vmx = to_vmx(vcpu);
4161 int r;
4162
4163 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
4164 if (r < 0)
4165 goto out_vmcs02;
4166
Ben Gardon41836832019-02-11 11:02:52 -08004167 vmx->nested.cached_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT);
Sean Christopherson55d23752018-12-03 13:53:18 -08004168 if (!vmx->nested.cached_vmcs12)
4169 goto out_cached_vmcs12;
4170
Ben Gardon41836832019-02-11 11:02:52 -08004171 vmx->nested.cached_shadow_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT);
Sean Christopherson55d23752018-12-03 13:53:18 -08004172 if (!vmx->nested.cached_shadow_vmcs12)
4173 goto out_cached_shadow_vmcs12;
4174
4175 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
4176 goto out_shadow_vmcs;
4177
4178 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
4179 HRTIMER_MODE_REL_PINNED);
4180 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
4181
4182 vmx->nested.vpid02 = allocate_vpid();
4183
4184 vmx->nested.vmcs02_initialized = false;
4185 vmx->nested.vmxon = true;
Luwei Kangee85dec2018-10-24 16:05:16 +08004186
4187 if (pt_mode == PT_MODE_HOST_GUEST) {
4188 vmx->pt_desc.guest.ctl = 0;
4189 pt_update_intercept_for_msr(vmx);
4190 }
4191
Sean Christopherson55d23752018-12-03 13:53:18 -08004192 return 0;
4193
4194out_shadow_vmcs:
4195 kfree(vmx->nested.cached_shadow_vmcs12);
4196
4197out_cached_shadow_vmcs12:
4198 kfree(vmx->nested.cached_vmcs12);
4199
4200out_cached_vmcs12:
4201 free_loaded_vmcs(&vmx->nested.vmcs02);
4202
4203out_vmcs02:
4204 return -ENOMEM;
4205}
4206
4207/*
4208 * Emulate the VMXON instruction.
4209 * Currently, we just remember that VMX is active, and do not save or even
4210 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
4211 * do not currently need to store anything in that guest-allocated memory
4212 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
4213 * argument is different from the VMXON pointer (which the spec says they do).
4214 */
4215static int handle_vmon(struct kvm_vcpu *vcpu)
4216{
4217 int ret;
4218 gpa_t vmptr;
KarimAllah Ahmed2e408932019-01-31 21:24:31 +01004219 uint32_t revision;
Sean Christopherson55d23752018-12-03 13:53:18 -08004220 struct vcpu_vmx *vmx = to_vmx(vcpu);
4221 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
4222 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4223
4224 /*
4225 * The Intel VMX Instruction Reference lists a bunch of bits that are
4226 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
4227 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
4228 * Otherwise, we should fail with #UD. But most faulting conditions
4229 * have already been checked by hardware, prior to the VM-exit for
4230 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
4231 * that bit set to 1 in non-root mode.
4232 */
4233 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
4234 kvm_queue_exception(vcpu, UD_VECTOR);
4235 return 1;
4236 }
4237
4238 /* CPL=0 must be checked manually. */
4239 if (vmx_get_cpl(vcpu)) {
4240 kvm_inject_gp(vcpu, 0);
4241 return 1;
4242 }
4243
4244 if (vmx->nested.vmxon)
4245 return nested_vmx_failValid(vcpu,
4246 VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
4247
4248 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
4249 != VMXON_NEEDED_FEATURES) {
4250 kvm_inject_gp(vcpu, 0);
4251 return 1;
4252 }
4253
4254 if (nested_vmx_get_vmptr(vcpu, &vmptr))
4255 return 1;
4256
4257 /*
4258 * SDM 3: 24.11.5
4259 * The first 4 bytes of VMXON region contain the supported
4260 * VMCS revision identifier
4261 *
4262 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
4263 * which replaces physical address width with 32
4264 */
KarimAllah Ahmede0bf2662019-01-31 21:24:43 +01004265 if (!page_address_valid(vcpu, vmptr))
Sean Christopherson55d23752018-12-03 13:53:18 -08004266 return nested_vmx_failInvalid(vcpu);
4267
KarimAllah Ahmed2e408932019-01-31 21:24:31 +01004268 if (kvm_read_guest(vcpu->kvm, vmptr, &revision, sizeof(revision)) ||
4269 revision != VMCS12_REVISION)
Sean Christopherson55d23752018-12-03 13:53:18 -08004270 return nested_vmx_failInvalid(vcpu);
4271
Sean Christopherson55d23752018-12-03 13:53:18 -08004272 vmx->nested.vmxon_ptr = vmptr;
4273 ret = enter_vmx_operation(vcpu);
4274 if (ret)
4275 return ret;
4276
4277 return nested_vmx_succeed(vcpu);
4278}
4279
4280static inline void nested_release_vmcs12(struct kvm_vcpu *vcpu)
4281{
4282 struct vcpu_vmx *vmx = to_vmx(vcpu);
4283
4284 if (vmx->nested.current_vmptr == -1ull)
4285 return;
4286
4287 if (enable_shadow_vmcs) {
4288 /* copy to memory all shadowed fields in case
4289 they were modified */
4290 copy_shadow_to_vmcs12(vmx);
4291 vmx->nested.need_vmcs12_sync = false;
4292 vmx_disable_shadow_vmcs(vmx);
4293 }
4294 vmx->nested.posted_intr_nv = -1;
4295
4296 /* Flush VMCS12 to guest memory */
4297 kvm_vcpu_write_guest_page(vcpu,
4298 vmx->nested.current_vmptr >> PAGE_SHIFT,
4299 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
4300
4301 kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
4302
4303 vmx->nested.current_vmptr = -1ull;
4304}
4305
4306/* Emulate the VMXOFF instruction */
4307static int handle_vmoff(struct kvm_vcpu *vcpu)
4308{
4309 if (!nested_vmx_check_permission(vcpu))
4310 return 1;
4311 free_nested(vcpu);
4312 return nested_vmx_succeed(vcpu);
4313}
4314
4315/* Emulate the VMCLEAR instruction */
4316static int handle_vmclear(struct kvm_vcpu *vcpu)
4317{
4318 struct vcpu_vmx *vmx = to_vmx(vcpu);
4319 u32 zero = 0;
4320 gpa_t vmptr;
4321
4322 if (!nested_vmx_check_permission(vcpu))
4323 return 1;
4324
4325 if (nested_vmx_get_vmptr(vcpu, &vmptr))
4326 return 1;
4327
KarimAllah Ahmede0bf2662019-01-31 21:24:43 +01004328 if (!page_address_valid(vcpu, vmptr))
Sean Christopherson55d23752018-12-03 13:53:18 -08004329 return nested_vmx_failValid(vcpu,
4330 VMXERR_VMCLEAR_INVALID_ADDRESS);
4331
4332 if (vmptr == vmx->nested.vmxon_ptr)
4333 return nested_vmx_failValid(vcpu,
4334 VMXERR_VMCLEAR_VMXON_POINTER);
4335
KarimAllah Ahmeddee9c042019-01-31 21:24:42 +01004336 if (vmx->nested.hv_evmcs_map.hva) {
Sean Christopherson55d23752018-12-03 13:53:18 -08004337 if (vmptr == vmx->nested.hv_evmcs_vmptr)
4338 nested_release_evmcs(vcpu);
4339 } else {
4340 if (vmptr == vmx->nested.current_vmptr)
4341 nested_release_vmcs12(vcpu);
4342
4343 kvm_vcpu_write_guest(vcpu,
4344 vmptr + offsetof(struct vmcs12,
4345 launch_state),
4346 &zero, sizeof(zero));
4347 }
4348
4349 return nested_vmx_succeed(vcpu);
4350}
4351
4352static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
4353
4354/* Emulate the VMLAUNCH instruction */
4355static int handle_vmlaunch(struct kvm_vcpu *vcpu)
4356{
4357 return nested_vmx_run(vcpu, true);
4358}
4359
4360/* Emulate the VMRESUME instruction */
4361static int handle_vmresume(struct kvm_vcpu *vcpu)
4362{
4363
4364 return nested_vmx_run(vcpu, false);
4365}
4366
4367static int handle_vmread(struct kvm_vcpu *vcpu)
4368{
4369 unsigned long field;
4370 u64 field_value;
4371 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4372 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004373 int len;
Sean Christopherson55d23752018-12-03 13:53:18 -08004374 gva_t gva = 0;
4375 struct vmcs12 *vmcs12;
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07004376 short offset;
Sean Christopherson55d23752018-12-03 13:53:18 -08004377
4378 if (!nested_vmx_check_permission(vcpu))
4379 return 1;
4380
4381 if (to_vmx(vcpu)->nested.current_vmptr == -1ull)
4382 return nested_vmx_failInvalid(vcpu);
4383
4384 if (!is_guest_mode(vcpu))
4385 vmcs12 = get_vmcs12(vcpu);
4386 else {
4387 /*
4388 * When vmcs->vmcs_link_pointer is -1ull, any VMREAD
4389 * to shadowed-field sets the ALU flags for VMfailInvalid.
4390 */
4391 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull)
4392 return nested_vmx_failInvalid(vcpu);
4393 vmcs12 = get_shadow_vmcs12(vcpu);
4394 }
4395
4396 /* Decode instruction info and find the field to read */
4397 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07004398
4399 offset = vmcs_field_to_offset(field);
4400 if (offset < 0)
Sean Christopherson55d23752018-12-03 13:53:18 -08004401 return nested_vmx_failValid(vcpu,
4402 VMXERR_UNSUPPORTED_VMCS_COMPONENT);
4403
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07004404 /* Read the field, zero-extended to a u64 field_value */
4405 field_value = vmcs12_read_any(vmcs12, field, offset);
4406
Sean Christopherson55d23752018-12-03 13:53:18 -08004407 /*
4408 * Now copy part of this value to register or memory, as requested.
4409 * Note that the number of bits actually copied is 32 or 64 depending
4410 * on the guest's mode (32 or 64 bit), not on the given field's length.
4411 */
4412 if (vmx_instruction_info & (1u << 10)) {
4413 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
4414 field_value);
4415 } else {
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004416 len = is_64_bit_mode(vcpu) ? 8 : 4;
Sean Christopherson55d23752018-12-03 13:53:18 -08004417 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004418 vmx_instruction_info, true, len, &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08004419 return 1;
4420 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004421 kvm_write_guest_virt_system(vcpu, gva, &field_value, len, NULL);
Sean Christopherson55d23752018-12-03 13:53:18 -08004422 }
4423
4424 return nested_vmx_succeed(vcpu);
4425}
4426
4427
4428static int handle_vmwrite(struct kvm_vcpu *vcpu)
4429{
4430 unsigned long field;
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004431 int len;
Sean Christopherson55d23752018-12-03 13:53:18 -08004432 gva_t gva;
4433 struct vcpu_vmx *vmx = to_vmx(vcpu);
4434 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4435 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
4436
4437 /* The value to write might be 32 or 64 bits, depending on L1's long
4438 * mode, and eventually we need to write that into a field of several
4439 * possible lengths. The code below first zero-extends the value to 64
4440 * bit (field_value), and then copies only the appropriate number of
4441 * bits into the vmcs12 field.
4442 */
4443 u64 field_value = 0;
4444 struct x86_exception e;
4445 struct vmcs12 *vmcs12;
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07004446 short offset;
Sean Christopherson55d23752018-12-03 13:53:18 -08004447
4448 if (!nested_vmx_check_permission(vcpu))
4449 return 1;
4450
4451 if (vmx->nested.current_vmptr == -1ull)
4452 return nested_vmx_failInvalid(vcpu);
4453
4454 if (vmx_instruction_info & (1u << 10))
4455 field_value = kvm_register_readl(vcpu,
4456 (((vmx_instruction_info) >> 3) & 0xf));
4457 else {
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004458 len = is_64_bit_mode(vcpu) ? 8 : 4;
Sean Christopherson55d23752018-12-03 13:53:18 -08004459 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004460 vmx_instruction_info, false, len, &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08004461 return 1;
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004462 if (kvm_read_guest_virt(vcpu, gva, &field_value, len, &e)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08004463 kvm_inject_page_fault(vcpu, &e);
4464 return 1;
4465 }
4466 }
4467
4468
4469 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
4470 /*
4471 * If the vCPU supports "VMWRITE to any supported field in the
4472 * VMCS," then the "read-only" fields are actually read/write.
4473 */
4474 if (vmcs_field_readonly(field) &&
4475 !nested_cpu_has_vmwrite_any_field(vcpu))
4476 return nested_vmx_failValid(vcpu,
4477 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
4478
4479 if (!is_guest_mode(vcpu))
4480 vmcs12 = get_vmcs12(vcpu);
4481 else {
4482 /*
4483 * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE
4484 * to shadowed-field sets the ALU flags for VMfailInvalid.
4485 */
4486 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull)
4487 return nested_vmx_failInvalid(vcpu);
4488 vmcs12 = get_shadow_vmcs12(vcpu);
4489 }
4490
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07004491 offset = vmcs_field_to_offset(field);
4492 if (offset < 0)
4493 return nested_vmx_failValid(vcpu,
4494 VMXERR_UNSUPPORTED_VMCS_COMPONENT);
4495
Sean Christophersonb6437802019-05-07 08:36:24 -07004496 /*
4497 * Some Intel CPUs intentionally drop the reserved bits of the AR byte
4498 * fields on VMWRITE. Emulate this behavior to ensure consistent KVM
4499 * behavior regardless of the underlying hardware, e.g. if an AR_BYTE
4500 * field is intercepted for VMWRITE but not VMREAD (in L1), then VMREAD
4501 * from L1 will return a different value than VMREAD from L2 (L1 sees
4502 * the stripped down value, L2 sees the full value as stored by KVM).
4503 */
4504 if (field >= GUEST_ES_AR_BYTES && field <= GUEST_TR_AR_BYTES)
4505 field_value &= 0x1f0ff;
4506
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07004507 vmcs12_write_any(vmcs12, field, offset, field_value);
Sean Christopherson55d23752018-12-03 13:53:18 -08004508
4509 /*
4510 * Do not track vmcs12 dirty-state if in guest-mode
4511 * as we actually dirty shadow vmcs12 instead of vmcs12.
4512 */
4513 if (!is_guest_mode(vcpu)) {
4514 switch (field) {
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07004515#define SHADOW_FIELD_RW(x, y) case x:
Sean Christopherson55d23752018-12-03 13:53:18 -08004516#include "vmcs_shadow_fields.h"
4517 /*
4518 * The fields that can be updated by L1 without a vmexit are
4519 * always updated in the vmcs02, the others go down the slow
4520 * path of prepare_vmcs02.
4521 */
4522 break;
Sean Christophersonfadcead2019-05-07 08:36:23 -07004523
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07004524#define SHADOW_FIELD_RO(x, y) case x:
Sean Christophersonfadcead2019-05-07 08:36:23 -07004525#include "vmcs_shadow_fields.h"
4526 /*
4527 * L1 can read these fields without exiting, ensure the
4528 * shadow VMCS is up-to-date.
4529 */
4530 if (enable_shadow_vmcs) {
4531 preempt_disable();
4532 vmcs_load(vmx->vmcs01.shadow_vmcs);
4533
4534 __vmcs_writel(field, field_value);
4535
4536 vmcs_clear(vmx->vmcs01.shadow_vmcs);
4537 vmcs_load(vmx->loaded_vmcs->vmcs);
4538 preempt_enable();
4539 }
4540 /* fall through */
Sean Christopherson55d23752018-12-03 13:53:18 -08004541 default:
4542 vmx->nested.dirty_vmcs12 = true;
4543 break;
4544 }
4545 }
4546
4547 return nested_vmx_succeed(vcpu);
4548}
4549
4550static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
4551{
4552 vmx->nested.current_vmptr = vmptr;
4553 if (enable_shadow_vmcs) {
4554 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
4555 SECONDARY_EXEC_SHADOW_VMCS);
4556 vmcs_write64(VMCS_LINK_POINTER,
4557 __pa(vmx->vmcs01.shadow_vmcs));
4558 vmx->nested.need_vmcs12_sync = true;
4559 }
4560 vmx->nested.dirty_vmcs12 = true;
4561}
4562
4563/* Emulate the VMPTRLD instruction */
4564static int handle_vmptrld(struct kvm_vcpu *vcpu)
4565{
4566 struct vcpu_vmx *vmx = to_vmx(vcpu);
4567 gpa_t vmptr;
4568
4569 if (!nested_vmx_check_permission(vcpu))
4570 return 1;
4571
4572 if (nested_vmx_get_vmptr(vcpu, &vmptr))
4573 return 1;
4574
KarimAllah Ahmede0bf2662019-01-31 21:24:43 +01004575 if (!page_address_valid(vcpu, vmptr))
Sean Christopherson55d23752018-12-03 13:53:18 -08004576 return nested_vmx_failValid(vcpu,
4577 VMXERR_VMPTRLD_INVALID_ADDRESS);
4578
4579 if (vmptr == vmx->nested.vmxon_ptr)
4580 return nested_vmx_failValid(vcpu,
4581 VMXERR_VMPTRLD_VMXON_POINTER);
4582
4583 /* Forbid normal VMPTRLD if Enlightened version was used */
4584 if (vmx->nested.hv_evmcs)
4585 return 1;
4586
4587 if (vmx->nested.current_vmptr != vmptr) {
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01004588 struct kvm_host_map map;
Sean Christopherson55d23752018-12-03 13:53:18 -08004589 struct vmcs12 *new_vmcs12;
Sean Christopherson55d23752018-12-03 13:53:18 -08004590
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01004591 if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmptr), &map)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08004592 /*
4593 * Reads from an unbacked page return all 1s,
4594 * which means that the 32 bits located at the
4595 * given physical address won't match the required
4596 * VMCS12_REVISION identifier.
4597 */
Vitaly Kuznetsov826c1362019-01-09 18:22:56 +01004598 return nested_vmx_failValid(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08004599 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Sean Christopherson55d23752018-12-03 13:53:18 -08004600 }
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01004601
4602 new_vmcs12 = map.hva;
4603
Sean Christopherson55d23752018-12-03 13:53:18 -08004604 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
4605 (new_vmcs12->hdr.shadow_vmcs &&
4606 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01004607 kvm_vcpu_unmap(vcpu, &map, false);
Sean Christopherson55d23752018-12-03 13:53:18 -08004608 return nested_vmx_failValid(vcpu,
4609 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
4610 }
4611
4612 nested_release_vmcs12(vcpu);
4613
4614 /*
4615 * Load VMCS12 from guest memory since it is not already
4616 * cached.
4617 */
4618 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01004619 kvm_vcpu_unmap(vcpu, &map, false);
Sean Christopherson55d23752018-12-03 13:53:18 -08004620
4621 set_current_vmptr(vmx, vmptr);
4622 }
4623
4624 return nested_vmx_succeed(vcpu);
4625}
4626
4627/* Emulate the VMPTRST instruction */
4628static int handle_vmptrst(struct kvm_vcpu *vcpu)
4629{
4630 unsigned long exit_qual = vmcs_readl(EXIT_QUALIFICATION);
4631 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
4632 gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr;
4633 struct x86_exception e;
4634 gva_t gva;
4635
4636 if (!nested_vmx_check_permission(vcpu))
4637 return 1;
4638
4639 if (unlikely(to_vmx(vcpu)->nested.hv_evmcs))
4640 return 1;
4641
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004642 if (get_vmx_mem_address(vcpu, exit_qual, instr_info,
4643 true, sizeof(gpa_t), &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08004644 return 1;
4645 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
4646 if (kvm_write_guest_virt_system(vcpu, gva, (void *)&current_vmptr,
4647 sizeof(gpa_t), &e)) {
4648 kvm_inject_page_fault(vcpu, &e);
4649 return 1;
4650 }
4651 return nested_vmx_succeed(vcpu);
4652}
4653
4654/* Emulate the INVEPT instruction */
4655static int handle_invept(struct kvm_vcpu *vcpu)
4656{
4657 struct vcpu_vmx *vmx = to_vmx(vcpu);
4658 u32 vmx_instruction_info, types;
4659 unsigned long type;
4660 gva_t gva;
4661 struct x86_exception e;
4662 struct {
4663 u64 eptp, gpa;
4664 } operand;
4665
4666 if (!(vmx->nested.msrs.secondary_ctls_high &
4667 SECONDARY_EXEC_ENABLE_EPT) ||
4668 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
4669 kvm_queue_exception(vcpu, UD_VECTOR);
4670 return 1;
4671 }
4672
4673 if (!nested_vmx_check_permission(vcpu))
4674 return 1;
4675
4676 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
4677 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
4678
4679 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
4680
4681 if (type >= 32 || !(types & (1 << type)))
4682 return nested_vmx_failValid(vcpu,
4683 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
4684
4685 /* According to the Intel VMX instruction reference, the memory
4686 * operand is read even if it isn't needed (e.g., for type==global)
4687 */
4688 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004689 vmx_instruction_info, false, sizeof(operand), &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08004690 return 1;
4691 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
4692 kvm_inject_page_fault(vcpu, &e);
4693 return 1;
4694 }
4695
4696 switch (type) {
4697 case VMX_EPT_EXTENT_GLOBAL:
4698 /*
4699 * TODO: track mappings and invalidate
4700 * single context requests appropriately
4701 */
4702 case VMX_EPT_EXTENT_CONTEXT:
4703 kvm_mmu_sync_roots(vcpu);
4704 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
4705 break;
4706 default:
4707 BUG_ON(1);
4708 break;
4709 }
4710
4711 return nested_vmx_succeed(vcpu);
4712}
4713
4714static int handle_invvpid(struct kvm_vcpu *vcpu)
4715{
4716 struct vcpu_vmx *vmx = to_vmx(vcpu);
4717 u32 vmx_instruction_info;
4718 unsigned long type, types;
4719 gva_t gva;
4720 struct x86_exception e;
4721 struct {
4722 u64 vpid;
4723 u64 gla;
4724 } operand;
4725 u16 vpid02;
4726
4727 if (!(vmx->nested.msrs.secondary_ctls_high &
4728 SECONDARY_EXEC_ENABLE_VPID) ||
4729 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
4730 kvm_queue_exception(vcpu, UD_VECTOR);
4731 return 1;
4732 }
4733
4734 if (!nested_vmx_check_permission(vcpu))
4735 return 1;
4736
4737 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
4738 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
4739
4740 types = (vmx->nested.msrs.vpid_caps &
4741 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
4742
4743 if (type >= 32 || !(types & (1 << type)))
4744 return nested_vmx_failValid(vcpu,
4745 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
4746
4747 /* according to the intel vmx instruction reference, the memory
4748 * operand is read even if it isn't needed (e.g., for type==global)
4749 */
4750 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004751 vmx_instruction_info, false, sizeof(operand), &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08004752 return 1;
4753 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
4754 kvm_inject_page_fault(vcpu, &e);
4755 return 1;
4756 }
4757 if (operand.vpid >> 16)
4758 return nested_vmx_failValid(vcpu,
4759 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
4760
4761 vpid02 = nested_get_vpid02(vcpu);
4762 switch (type) {
4763 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
4764 if (!operand.vpid ||
4765 is_noncanonical_address(operand.gla, vcpu))
4766 return nested_vmx_failValid(vcpu,
4767 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
4768 if (cpu_has_vmx_invvpid_individual_addr()) {
4769 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
4770 vpid02, operand.gla);
4771 } else
4772 __vmx_flush_tlb(vcpu, vpid02, false);
4773 break;
4774 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
4775 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
4776 if (!operand.vpid)
4777 return nested_vmx_failValid(vcpu,
4778 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
4779 __vmx_flush_tlb(vcpu, vpid02, false);
4780 break;
4781 case VMX_VPID_EXTENT_ALL_CONTEXT:
4782 __vmx_flush_tlb(vcpu, vpid02, false);
4783 break;
4784 default:
4785 WARN_ON_ONCE(1);
4786 return kvm_skip_emulated_instruction(vcpu);
4787 }
4788
4789 return nested_vmx_succeed(vcpu);
4790}
4791
4792static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
4793 struct vmcs12 *vmcs12)
4794{
Sean Christopherson2b3eaf82019-04-30 10:36:19 -07004795 u32 index = kvm_rcx_read(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08004796 u64 address;
4797 bool accessed_dirty;
4798 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4799
4800 if (!nested_cpu_has_eptp_switching(vmcs12) ||
4801 !nested_cpu_has_ept(vmcs12))
4802 return 1;
4803
4804 if (index >= VMFUNC_EPTP_ENTRIES)
4805 return 1;
4806
4807
4808 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
4809 &address, index * 8, 8))
4810 return 1;
4811
4812 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
4813
4814 /*
4815 * If the (L2) guest does a vmfunc to the currently
4816 * active ept pointer, we don't have to do anything else
4817 */
4818 if (vmcs12->ept_pointer != address) {
4819 if (!valid_ept_address(vcpu, address))
4820 return 1;
4821
4822 kvm_mmu_unload(vcpu);
4823 mmu->ept_ad = accessed_dirty;
4824 mmu->mmu_role.base.ad_disabled = !accessed_dirty;
4825 vmcs12->ept_pointer = address;
4826 /*
4827 * TODO: Check what's the correct approach in case
4828 * mmu reload fails. Currently, we just let the next
4829 * reload potentially fail
4830 */
4831 kvm_mmu_reload(vcpu);
4832 }
4833
4834 return 0;
4835}
4836
4837static int handle_vmfunc(struct kvm_vcpu *vcpu)
4838{
4839 struct vcpu_vmx *vmx = to_vmx(vcpu);
4840 struct vmcs12 *vmcs12;
Sean Christopherson2b3eaf82019-04-30 10:36:19 -07004841 u32 function = kvm_rax_read(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08004842
4843 /*
4844 * VMFUNC is only supported for nested guests, but we always enable the
4845 * secondary control for simplicity; for non-nested mode, fake that we
4846 * didn't by injecting #UD.
4847 */
4848 if (!is_guest_mode(vcpu)) {
4849 kvm_queue_exception(vcpu, UD_VECTOR);
4850 return 1;
4851 }
4852
4853 vmcs12 = get_vmcs12(vcpu);
4854 if ((vmcs12->vm_function_control & (1 << function)) == 0)
4855 goto fail;
4856
4857 switch (function) {
4858 case 0:
4859 if (nested_vmx_eptp_switching(vcpu, vmcs12))
4860 goto fail;
4861 break;
4862 default:
4863 goto fail;
4864 }
4865 return kvm_skip_emulated_instruction(vcpu);
4866
4867fail:
4868 nested_vmx_vmexit(vcpu, vmx->exit_reason,
4869 vmcs_read32(VM_EXIT_INTR_INFO),
4870 vmcs_readl(EXIT_QUALIFICATION));
4871 return 1;
4872}
4873
4874
4875static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
4876 struct vmcs12 *vmcs12)
4877{
4878 unsigned long exit_qualification;
4879 gpa_t bitmap, last_bitmap;
4880 unsigned int port;
4881 int size;
4882 u8 b;
4883
4884 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
4885 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
4886
4887 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4888
4889 port = exit_qualification >> 16;
4890 size = (exit_qualification & 7) + 1;
4891
4892 last_bitmap = (gpa_t)-1;
4893 b = -1;
4894
4895 while (size > 0) {
4896 if (port < 0x8000)
4897 bitmap = vmcs12->io_bitmap_a;
4898 else if (port < 0x10000)
4899 bitmap = vmcs12->io_bitmap_b;
4900 else
4901 return true;
4902 bitmap += (port & 0x7fff) / 8;
4903
4904 if (last_bitmap != bitmap)
4905 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
4906 return true;
4907 if (b & (1 << (port & 7)))
4908 return true;
4909
4910 port++;
4911 size--;
4912 last_bitmap = bitmap;
4913 }
4914
4915 return false;
4916}
4917
4918/*
4919 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
4920 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
4921 * disinterest in the current event (read or write a specific MSR) by using an
4922 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
4923 */
4924static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
4925 struct vmcs12 *vmcs12, u32 exit_reason)
4926{
Sean Christopherson2b3eaf82019-04-30 10:36:19 -07004927 u32 msr_index = kvm_rcx_read(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08004928 gpa_t bitmap;
4929
4930 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
4931 return true;
4932
4933 /*
4934 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
4935 * for the four combinations of read/write and low/high MSR numbers.
4936 * First we need to figure out which of the four to use:
4937 */
4938 bitmap = vmcs12->msr_bitmap;
4939 if (exit_reason == EXIT_REASON_MSR_WRITE)
4940 bitmap += 2048;
4941 if (msr_index >= 0xc0000000) {
4942 msr_index -= 0xc0000000;
4943 bitmap += 1024;
4944 }
4945
4946 /* Then read the msr_index'th bit from this bitmap: */
4947 if (msr_index < 1024*8) {
4948 unsigned char b;
4949 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
4950 return true;
4951 return 1 & (b >> (msr_index & 7));
4952 } else
4953 return true; /* let L1 handle the wrong parameter */
4954}
4955
4956/*
4957 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
4958 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
4959 * intercept (via guest_host_mask etc.) the current event.
4960 */
4961static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
4962 struct vmcs12 *vmcs12)
4963{
4964 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4965 int cr = exit_qualification & 15;
4966 int reg;
4967 unsigned long val;
4968
4969 switch ((exit_qualification >> 4) & 3) {
4970 case 0: /* mov to cr */
4971 reg = (exit_qualification >> 8) & 15;
4972 val = kvm_register_readl(vcpu, reg);
4973 switch (cr) {
4974 case 0:
4975 if (vmcs12->cr0_guest_host_mask &
4976 (val ^ vmcs12->cr0_read_shadow))
4977 return true;
4978 break;
4979 case 3:
4980 if ((vmcs12->cr3_target_count >= 1 &&
4981 vmcs12->cr3_target_value0 == val) ||
4982 (vmcs12->cr3_target_count >= 2 &&
4983 vmcs12->cr3_target_value1 == val) ||
4984 (vmcs12->cr3_target_count >= 3 &&
4985 vmcs12->cr3_target_value2 == val) ||
4986 (vmcs12->cr3_target_count >= 4 &&
4987 vmcs12->cr3_target_value3 == val))
4988 return false;
4989 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
4990 return true;
4991 break;
4992 case 4:
4993 if (vmcs12->cr4_guest_host_mask &
4994 (vmcs12->cr4_read_shadow ^ val))
4995 return true;
4996 break;
4997 case 8:
4998 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
4999 return true;
5000 break;
5001 }
5002 break;
5003 case 2: /* clts */
5004 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
5005 (vmcs12->cr0_read_shadow & X86_CR0_TS))
5006 return true;
5007 break;
5008 case 1: /* mov from cr */
5009 switch (cr) {
5010 case 3:
5011 if (vmcs12->cpu_based_vm_exec_control &
5012 CPU_BASED_CR3_STORE_EXITING)
5013 return true;
5014 break;
5015 case 8:
5016 if (vmcs12->cpu_based_vm_exec_control &
5017 CPU_BASED_CR8_STORE_EXITING)
5018 return true;
5019 break;
5020 }
5021 break;
5022 case 3: /* lmsw */
5023 /*
5024 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
5025 * cr0. Other attempted changes are ignored, with no exit.
5026 */
5027 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
5028 if (vmcs12->cr0_guest_host_mask & 0xe &
5029 (val ^ vmcs12->cr0_read_shadow))
5030 return true;
5031 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
5032 !(vmcs12->cr0_read_shadow & 0x1) &&
5033 (val & 0x1))
5034 return true;
5035 break;
5036 }
5037 return false;
5038}
5039
5040static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
5041 struct vmcs12 *vmcs12, gpa_t bitmap)
5042{
5043 u32 vmx_instruction_info;
5044 unsigned long field;
5045 u8 b;
5046
5047 if (!nested_cpu_has_shadow_vmcs(vmcs12))
5048 return true;
5049
5050 /* Decode instruction info and find the field to access */
5051 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5052 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
5053
5054 /* Out-of-range fields always cause a VM exit from L2 to L1 */
5055 if (field >> 15)
5056 return true;
5057
5058 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
5059 return true;
5060
5061 return 1 & (b >> (field & 7));
5062}
5063
5064/*
5065 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
5066 * should handle it ourselves in L0 (and then continue L2). Only call this
5067 * when in is_guest_mode (L2).
5068 */
5069bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
5070{
5071 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
5072 struct vcpu_vmx *vmx = to_vmx(vcpu);
5073 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5074
5075 if (vmx->nested.nested_run_pending)
5076 return false;
5077
5078 if (unlikely(vmx->fail)) {
5079 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
5080 vmcs_read32(VM_INSTRUCTION_ERROR));
5081 return true;
5082 }
5083
5084 /*
5085 * The host physical addresses of some pages of guest memory
5086 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
5087 * Page). The CPU may write to these pages via their host
5088 * physical address while L2 is running, bypassing any
5089 * address-translation-based dirty tracking (e.g. EPT write
5090 * protection).
5091 *
5092 * Mark them dirty on every exit from L2 to prevent them from
5093 * getting out of sync with dirty tracking.
5094 */
5095 nested_mark_vmcs12_pages_dirty(vcpu);
5096
5097 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
5098 vmcs_readl(EXIT_QUALIFICATION),
5099 vmx->idt_vectoring_info,
5100 intr_info,
5101 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
5102 KVM_ISA_VMX);
5103
5104 switch (exit_reason) {
5105 case EXIT_REASON_EXCEPTION_NMI:
5106 if (is_nmi(intr_info))
5107 return false;
5108 else if (is_page_fault(intr_info))
5109 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
5110 else if (is_debug(intr_info) &&
5111 vcpu->guest_debug &
5112 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5113 return false;
5114 else if (is_breakpoint(intr_info) &&
5115 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
5116 return false;
5117 return vmcs12->exception_bitmap &
5118 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
5119 case EXIT_REASON_EXTERNAL_INTERRUPT:
5120 return false;
5121 case EXIT_REASON_TRIPLE_FAULT:
5122 return true;
5123 case EXIT_REASON_PENDING_INTERRUPT:
5124 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
5125 case EXIT_REASON_NMI_WINDOW:
5126 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
5127 case EXIT_REASON_TASK_SWITCH:
5128 return true;
5129 case EXIT_REASON_CPUID:
5130 return true;
5131 case EXIT_REASON_HLT:
5132 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
5133 case EXIT_REASON_INVD:
5134 return true;
5135 case EXIT_REASON_INVLPG:
5136 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
5137 case EXIT_REASON_RDPMC:
5138 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
5139 case EXIT_REASON_RDRAND:
5140 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
5141 case EXIT_REASON_RDSEED:
5142 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
5143 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
5144 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
5145 case EXIT_REASON_VMREAD:
5146 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
5147 vmcs12->vmread_bitmap);
5148 case EXIT_REASON_VMWRITE:
5149 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
5150 vmcs12->vmwrite_bitmap);
5151 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
5152 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
5153 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
5154 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
5155 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
5156 /*
5157 * VMX instructions trap unconditionally. This allows L1 to
5158 * emulate them for its L2 guest, i.e., allows 3-level nesting!
5159 */
5160 return true;
5161 case EXIT_REASON_CR_ACCESS:
5162 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
5163 case EXIT_REASON_DR_ACCESS:
5164 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
5165 case EXIT_REASON_IO_INSTRUCTION:
5166 return nested_vmx_exit_handled_io(vcpu, vmcs12);
5167 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
5168 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
5169 case EXIT_REASON_MSR_READ:
5170 case EXIT_REASON_MSR_WRITE:
5171 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
5172 case EXIT_REASON_INVALID_STATE:
5173 return true;
5174 case EXIT_REASON_MWAIT_INSTRUCTION:
5175 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
5176 case EXIT_REASON_MONITOR_TRAP_FLAG:
5177 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
5178 case EXIT_REASON_MONITOR_INSTRUCTION:
5179 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
5180 case EXIT_REASON_PAUSE_INSTRUCTION:
5181 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
5182 nested_cpu_has2(vmcs12,
5183 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
5184 case EXIT_REASON_MCE_DURING_VMENTRY:
5185 return false;
5186 case EXIT_REASON_TPR_BELOW_THRESHOLD:
5187 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
5188 case EXIT_REASON_APIC_ACCESS:
5189 case EXIT_REASON_APIC_WRITE:
5190 case EXIT_REASON_EOI_INDUCED:
5191 /*
5192 * The controls for "virtualize APIC accesses," "APIC-
5193 * register virtualization," and "virtual-interrupt
5194 * delivery" only come from vmcs12.
5195 */
5196 return true;
5197 case EXIT_REASON_EPT_VIOLATION:
5198 /*
5199 * L0 always deals with the EPT violation. If nested EPT is
5200 * used, and the nested mmu code discovers that the address is
5201 * missing in the guest EPT table (EPT12), the EPT violation
5202 * will be injected with nested_ept_inject_page_fault()
5203 */
5204 return false;
5205 case EXIT_REASON_EPT_MISCONFIG:
5206 /*
5207 * L2 never uses directly L1's EPT, but rather L0's own EPT
5208 * table (shadow on EPT) or a merged EPT table that L0 built
5209 * (EPT on EPT). So any problems with the structure of the
5210 * table is L0's fault.
5211 */
5212 return false;
5213 case EXIT_REASON_INVPCID:
5214 return
5215 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
5216 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
5217 case EXIT_REASON_WBINVD:
5218 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
5219 case EXIT_REASON_XSETBV:
5220 return true;
5221 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
5222 /*
5223 * This should never happen, since it is not possible to
5224 * set XSS to a non-zero value---neither in L1 nor in L2.
5225 * If if it were, XSS would have to be checked against
5226 * the XSS exit bitmap in vmcs12.
5227 */
5228 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
5229 case EXIT_REASON_PREEMPTION_TIMER:
5230 return false;
5231 case EXIT_REASON_PML_FULL:
5232 /* We emulate PML support to L1. */
5233 return false;
5234 case EXIT_REASON_VMFUNC:
5235 /* VM functions are emulated through L2->L0 vmexits. */
5236 return false;
5237 case EXIT_REASON_ENCLS:
5238 /* SGX is never exposed to L1 */
5239 return false;
5240 default:
5241 return true;
5242 }
5243}
5244
5245
5246static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
5247 struct kvm_nested_state __user *user_kvm_nested_state,
5248 u32 user_data_size)
5249{
5250 struct vcpu_vmx *vmx;
5251 struct vmcs12 *vmcs12;
5252 struct kvm_nested_state kvm_state = {
5253 .flags = 0,
5254 .format = 0,
5255 .size = sizeof(kvm_state),
5256 .vmx.vmxon_pa = -1ull,
5257 .vmx.vmcs_pa = -1ull,
5258 };
5259
5260 if (!vcpu)
5261 return kvm_state.size + 2 * VMCS12_SIZE;
5262
5263 vmx = to_vmx(vcpu);
5264 vmcs12 = get_vmcs12(vcpu);
5265
5266 if (nested_vmx_allowed(vcpu) && vmx->nested.enlightened_vmcs_enabled)
5267 kvm_state.flags |= KVM_STATE_NESTED_EVMCS;
5268
5269 if (nested_vmx_allowed(vcpu) &&
5270 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
5271 kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
5272 kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
5273
5274 if (vmx_has_valid_vmcs12(vcpu)) {
5275 kvm_state.size += VMCS12_SIZE;
5276
5277 if (is_guest_mode(vcpu) &&
5278 nested_cpu_has_shadow_vmcs(vmcs12) &&
5279 vmcs12->vmcs_link_pointer != -1ull)
5280 kvm_state.size += VMCS12_SIZE;
5281 }
5282
5283 if (vmx->nested.smm.vmxon)
5284 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
5285
5286 if (vmx->nested.smm.guest_mode)
5287 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
5288
5289 if (is_guest_mode(vcpu)) {
5290 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
5291
5292 if (vmx->nested.nested_run_pending)
5293 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
5294 }
5295 }
5296
5297 if (user_data_size < kvm_state.size)
5298 goto out;
5299
5300 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
5301 return -EFAULT;
5302
5303 if (!vmx_has_valid_vmcs12(vcpu))
5304 goto out;
5305
5306 /*
5307 * When running L2, the authoritative vmcs12 state is in the
5308 * vmcs02. When running L1, the authoritative vmcs12 state is
5309 * in the shadow or enlightened vmcs linked to vmcs01, unless
5310 * need_vmcs12_sync is set, in which case, the authoritative
5311 * vmcs12 state is in the vmcs12 already.
5312 */
5313 if (is_guest_mode(vcpu)) {
5314 sync_vmcs12(vcpu, vmcs12);
5315 } else if (!vmx->nested.need_vmcs12_sync) {
5316 if (vmx->nested.hv_evmcs)
5317 copy_enlightened_to_vmcs12(vmx);
5318 else if (enable_shadow_vmcs)
5319 copy_shadow_to_vmcs12(vmx);
5320 }
5321
Tom Roeder3a33d032019-01-24 13:48:20 -08005322 /*
5323 * Copy over the full allocated size of vmcs12 rather than just the size
5324 * of the struct.
5325 */
5326 if (copy_to_user(user_kvm_nested_state->data, vmcs12, VMCS12_SIZE))
Sean Christopherson55d23752018-12-03 13:53:18 -08005327 return -EFAULT;
5328
5329 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
5330 vmcs12->vmcs_link_pointer != -1ull) {
5331 if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE,
Tom Roeder3a33d032019-01-24 13:48:20 -08005332 get_shadow_vmcs12(vcpu), VMCS12_SIZE))
Sean Christopherson55d23752018-12-03 13:53:18 -08005333 return -EFAULT;
5334 }
5335
5336out:
5337 return kvm_state.size;
5338}
5339
5340/*
5341 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
5342 */
5343void vmx_leave_nested(struct kvm_vcpu *vcpu)
5344{
5345 if (is_guest_mode(vcpu)) {
5346 to_vmx(vcpu)->nested.nested_run_pending = 0;
5347 nested_vmx_vmexit(vcpu, -1, 0, 0);
5348 }
5349 free_nested(vcpu);
5350}
5351
5352static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
5353 struct kvm_nested_state __user *user_kvm_nested_state,
5354 struct kvm_nested_state *kvm_state)
5355{
5356 struct vcpu_vmx *vmx = to_vmx(vcpu);
5357 struct vmcs12 *vmcs12;
5358 u32 exit_qual;
5359 int ret;
5360
5361 if (kvm_state->format != 0)
5362 return -EINVAL;
5363
Sean Christopherson55d23752018-12-03 13:53:18 -08005364 if (!nested_vmx_allowed(vcpu))
5365 return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
5366
5367 if (kvm_state->vmx.vmxon_pa == -1ull) {
5368 if (kvm_state->vmx.smm.flags)
5369 return -EINVAL;
5370
5371 if (kvm_state->vmx.vmcs_pa != -1ull)
5372 return -EINVAL;
5373
5374 vmx_leave_nested(vcpu);
5375 return 0;
5376 }
5377
5378 if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
5379 return -EINVAL;
5380
5381 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
5382 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
5383 return -EINVAL;
5384
5385 if (kvm_state->vmx.smm.flags &
5386 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
5387 return -EINVAL;
5388
5389 /*
5390 * SMM temporarily disables VMX, so we cannot be in guest mode,
5391 * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags
5392 * must be zero.
5393 */
5394 if (is_smm(vcpu) ? kvm_state->flags : kvm_state->vmx.smm.flags)
5395 return -EINVAL;
5396
5397 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
5398 !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
5399 return -EINVAL;
5400
5401 vmx_leave_nested(vcpu);
5402 if (kvm_state->vmx.vmxon_pa == -1ull)
5403 return 0;
5404
Aaron Lewis332d0792019-05-02 11:31:33 -07005405 if (kvm_state->flags & KVM_STATE_NESTED_EVMCS)
5406 nested_enable_evmcs(vcpu, NULL);
5407
Sean Christopherson55d23752018-12-03 13:53:18 -08005408 vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
5409 ret = enter_vmx_operation(vcpu);
5410 if (ret)
5411 return ret;
5412
5413 /* Empty 'VMXON' state is permitted */
Jim Mattsone8ab8d22019-01-17 11:55:58 -08005414 if (kvm_state->size < sizeof(*kvm_state) + sizeof(*vmcs12))
Sean Christopherson55d23752018-12-03 13:53:18 -08005415 return 0;
5416
5417 if (kvm_state->vmx.vmcs_pa != -1ull) {
5418 if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
5419 !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
5420 return -EINVAL;
5421
5422 set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
5423 } else if (kvm_state->flags & KVM_STATE_NESTED_EVMCS) {
5424 /*
5425 * Sync eVMCS upon entry as we may not have
5426 * HV_X64_MSR_VP_ASSIST_PAGE set up yet.
5427 */
5428 vmx->nested.need_vmcs12_sync = true;
5429 } else {
5430 return -EINVAL;
5431 }
5432
5433 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
5434 vmx->nested.smm.vmxon = true;
5435 vmx->nested.vmxon = false;
5436
5437 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
5438 vmx->nested.smm.guest_mode = true;
5439 }
5440
5441 vmcs12 = get_vmcs12(vcpu);
5442 if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
5443 return -EFAULT;
5444
5445 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
5446 return -EINVAL;
5447
5448 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
5449 return 0;
5450
Sean Christopherson21be4ca2019-05-08 11:04:32 -07005451 vmx->nested.nested_run_pending =
5452 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
5453
5454 ret = -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08005455 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
5456 vmcs12->vmcs_link_pointer != -1ull) {
5457 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
5458
Paolo Bonzinidb809272019-05-20 11:55:36 +02005459 if (kvm_state->size < sizeof(*kvm_state) + VMCS12_SIZE + sizeof(*vmcs12))
Sean Christopherson21be4ca2019-05-08 11:04:32 -07005460 goto error_guest_mode;
Sean Christopherson55d23752018-12-03 13:53:18 -08005461
5462 if (copy_from_user(shadow_vmcs12,
5463 user_kvm_nested_state->data + VMCS12_SIZE,
Sean Christopherson21be4ca2019-05-08 11:04:32 -07005464 sizeof(*vmcs12))) {
5465 ret = -EFAULT;
5466 goto error_guest_mode;
5467 }
Sean Christopherson55d23752018-12-03 13:53:18 -08005468
5469 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
5470 !shadow_vmcs12->hdr.shadow_vmcs)
Sean Christopherson21be4ca2019-05-08 11:04:32 -07005471 goto error_guest_mode;
Sean Christopherson55d23752018-12-03 13:53:18 -08005472 }
5473
Sean Christopherson5478ba32019-04-11 12:18:06 -07005474 if (nested_vmx_check_controls(vcpu, vmcs12) ||
5475 nested_vmx_check_host_state(vcpu, vmcs12) ||
5476 nested_vmx_check_guest_state(vcpu, vmcs12, &exit_qual))
Sean Christopherson21be4ca2019-05-08 11:04:32 -07005477 goto error_guest_mode;
Sean Christopherson55d23752018-12-03 13:53:18 -08005478
5479 vmx->nested.dirty_vmcs12 = true;
5480 ret = nested_vmx_enter_non_root_mode(vcpu, false);
Sean Christopherson21be4ca2019-05-08 11:04:32 -07005481 if (ret)
5482 goto error_guest_mode;
Sean Christopherson55d23752018-12-03 13:53:18 -08005483
5484 return 0;
Sean Christopherson21be4ca2019-05-08 11:04:32 -07005485
5486error_guest_mode:
5487 vmx->nested.nested_run_pending = 0;
5488 return ret;
Sean Christopherson55d23752018-12-03 13:53:18 -08005489}
5490
5491void nested_vmx_vcpu_setup(void)
5492{
5493 if (enable_shadow_vmcs) {
Sean Christopherson55d23752018-12-03 13:53:18 -08005494 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Sean Christophersonfadcead2019-05-07 08:36:23 -07005495 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
Sean Christopherson55d23752018-12-03 13:53:18 -08005496 }
5497}
5498
5499/*
5500 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
5501 * returned for the various VMX controls MSRs when nested VMX is enabled.
5502 * The same values should also be used to verify that vmcs12 control fields are
5503 * valid during nested entry from L1 to L2.
5504 * Each of these control msrs has a low and high 32-bit half: A low bit is on
5505 * if the corresponding bit in the (32-bit) control field *must* be on, and a
5506 * bit in the high half is on if the corresponding bit in the control field
5507 * may be on. See also vmx_control_verify().
5508 */
5509void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, u32 ept_caps,
5510 bool apicv)
5511{
5512 /*
5513 * Note that as a general rule, the high half of the MSRs (bits in
5514 * the control fields which may be 1) should be initialized by the
5515 * intersection of the underlying hardware's MSR (i.e., features which
5516 * can be supported) and the list of features we want to expose -
5517 * because they are known to be properly supported in our code.
5518 * Also, usually, the low half of the MSRs (bits which must be 1) can
5519 * be set to 0, meaning that L1 may turn off any of these bits. The
5520 * reason is that if one of these bits is necessary, it will appear
5521 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
5522 * fields of vmcs01 and vmcs02, will turn these bits off - and
5523 * nested_vmx_exit_reflected() will not pass related exits to L1.
5524 * These rules have exceptions below.
5525 */
5526
5527 /* pin-based controls */
5528 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
5529 msrs->pinbased_ctls_low,
5530 msrs->pinbased_ctls_high);
5531 msrs->pinbased_ctls_low |=
5532 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
5533 msrs->pinbased_ctls_high &=
5534 PIN_BASED_EXT_INTR_MASK |
5535 PIN_BASED_NMI_EXITING |
5536 PIN_BASED_VIRTUAL_NMIS |
5537 (apicv ? PIN_BASED_POSTED_INTR : 0);
5538 msrs->pinbased_ctls_high |=
5539 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
5540 PIN_BASED_VMX_PREEMPTION_TIMER;
5541
5542 /* exit controls */
5543 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
5544 msrs->exit_ctls_low,
5545 msrs->exit_ctls_high);
5546 msrs->exit_ctls_low =
5547 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
5548
5549 msrs->exit_ctls_high &=
5550#ifdef CONFIG_X86_64
5551 VM_EXIT_HOST_ADDR_SPACE_SIZE |
5552#endif
5553 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
5554 msrs->exit_ctls_high |=
5555 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
5556 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
5557 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
5558
5559 /* We support free control of debug control saving. */
5560 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
5561
5562 /* entry controls */
5563 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
5564 msrs->entry_ctls_low,
5565 msrs->entry_ctls_high);
5566 msrs->entry_ctls_low =
5567 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
5568 msrs->entry_ctls_high &=
5569#ifdef CONFIG_X86_64
5570 VM_ENTRY_IA32E_MODE |
5571#endif
5572 VM_ENTRY_LOAD_IA32_PAT;
5573 msrs->entry_ctls_high |=
5574 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
5575
5576 /* We support free control of debug control loading. */
5577 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
5578
5579 /* cpu-based controls */
5580 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
5581 msrs->procbased_ctls_low,
5582 msrs->procbased_ctls_high);
5583 msrs->procbased_ctls_low =
5584 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
5585 msrs->procbased_ctls_high &=
5586 CPU_BASED_VIRTUAL_INTR_PENDING |
5587 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
5588 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
5589 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
5590 CPU_BASED_CR3_STORE_EXITING |
5591#ifdef CONFIG_X86_64
5592 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
5593#endif
5594 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
5595 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
5596 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
5597 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
5598 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
5599 /*
5600 * We can allow some features even when not supported by the
5601 * hardware. For example, L1 can specify an MSR bitmap - and we
5602 * can use it to avoid exits to L1 - even when L0 runs L2
5603 * without MSR bitmaps.
5604 */
5605 msrs->procbased_ctls_high |=
5606 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
5607 CPU_BASED_USE_MSR_BITMAPS;
5608
5609 /* We support free control of CR3 access interception. */
5610 msrs->procbased_ctls_low &=
5611 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
5612
5613 /*
5614 * secondary cpu-based controls. Do not include those that
5615 * depend on CPUID bits, they are added later by vmx_cpuid_update.
5616 */
Vitaly Kuznetsov6b1971c2019-02-07 11:42:14 +01005617 if (msrs->procbased_ctls_high & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)
5618 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
5619 msrs->secondary_ctls_low,
5620 msrs->secondary_ctls_high);
5621
Sean Christopherson55d23752018-12-03 13:53:18 -08005622 msrs->secondary_ctls_low = 0;
5623 msrs->secondary_ctls_high &=
5624 SECONDARY_EXEC_DESC |
5625 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
5626 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5627 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
5628 SECONDARY_EXEC_WBINVD_EXITING;
5629
5630 /*
5631 * We can emulate "VMCS shadowing," even if the hardware
5632 * doesn't support it.
5633 */
5634 msrs->secondary_ctls_high |=
5635 SECONDARY_EXEC_SHADOW_VMCS;
5636
5637 if (enable_ept) {
5638 /* nested EPT: emulate EPT also to L1 */
5639 msrs->secondary_ctls_high |=
5640 SECONDARY_EXEC_ENABLE_EPT;
5641 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
5642 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
5643 if (cpu_has_vmx_ept_execute_only())
5644 msrs->ept_caps |=
5645 VMX_EPT_EXECUTE_ONLY_BIT;
5646 msrs->ept_caps &= ept_caps;
5647 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
5648 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
5649 VMX_EPT_1GB_PAGE_BIT;
5650 if (enable_ept_ad_bits) {
5651 msrs->secondary_ctls_high |=
5652 SECONDARY_EXEC_ENABLE_PML;
5653 msrs->ept_caps |= VMX_EPT_AD_BIT;
5654 }
5655 }
5656
5657 if (cpu_has_vmx_vmfunc()) {
5658 msrs->secondary_ctls_high |=
5659 SECONDARY_EXEC_ENABLE_VMFUNC;
5660 /*
5661 * Advertise EPTP switching unconditionally
5662 * since we emulate it
5663 */
5664 if (enable_ept)
5665 msrs->vmfunc_controls =
5666 VMX_VMFUNC_EPTP_SWITCHING;
5667 }
5668
5669 /*
5670 * Old versions of KVM use the single-context version without
5671 * checking for support, so declare that it is supported even
5672 * though it is treated as global context. The alternative is
5673 * not failing the single-context invvpid, and it is worse.
5674 */
5675 if (enable_vpid) {
5676 msrs->secondary_ctls_high |=
5677 SECONDARY_EXEC_ENABLE_VPID;
5678 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
5679 VMX_VPID_EXTENT_SUPPORTED_MASK;
5680 }
5681
5682 if (enable_unrestricted_guest)
5683 msrs->secondary_ctls_high |=
5684 SECONDARY_EXEC_UNRESTRICTED_GUEST;
5685
5686 if (flexpriority_enabled)
5687 msrs->secondary_ctls_high |=
5688 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
5689
5690 /* miscellaneous data */
5691 rdmsr(MSR_IA32_VMX_MISC,
5692 msrs->misc_low,
5693 msrs->misc_high);
5694 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
5695 msrs->misc_low |=
5696 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
5697 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
5698 VMX_MISC_ACTIVITY_HLT;
5699 msrs->misc_high = 0;
5700
5701 /*
5702 * This MSR reports some information about VMX support. We
5703 * should return information about the VMX we emulate for the
5704 * guest, and the VMCS structure we give it - not about the
5705 * VMX support of the underlying hardware.
5706 */
5707 msrs->basic =
5708 VMCS12_REVISION |
5709 VMX_BASIC_TRUE_CTLS |
5710 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
5711 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
5712
5713 if (cpu_has_vmx_basic_inout())
5714 msrs->basic |= VMX_BASIC_INOUT;
5715
5716 /*
5717 * These MSRs specify bits which the guest must keep fixed on
5718 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
5719 * We picked the standard core2 setting.
5720 */
5721#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
5722#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
5723 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
5724 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
5725
5726 /* These MSRs specify bits which the guest must keep fixed off. */
5727 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
5728 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
5729
5730 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
5731 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
5732}
5733
5734void nested_vmx_hardware_unsetup(void)
5735{
5736 int i;
5737
5738 if (enable_shadow_vmcs) {
5739 for (i = 0; i < VMX_BITMAP_NR; i++)
5740 free_page((unsigned long)vmx_bitmap[i]);
5741 }
5742}
5743
5744__init int nested_vmx_hardware_setup(int (*exit_handlers[])(struct kvm_vcpu *))
5745{
5746 int i;
5747
Paolo Bonzini2b279242019-04-15 15:57:19 +02005748 /*
5749 * Without EPT it is not possible to restore L1's CR3 and PDPTR on
5750 * VMfail, because they are not available in vmcs01. Just always
5751 * use hardware checks.
5752 */
5753 if (!enable_ept)
5754 nested_early_check = 1;
5755
Sean Christopherson55d23752018-12-03 13:53:18 -08005756 if (!cpu_has_vmx_shadow_vmcs())
5757 enable_shadow_vmcs = 0;
5758 if (enable_shadow_vmcs) {
5759 for (i = 0; i < VMX_BITMAP_NR; i++) {
Ben Gardon41836832019-02-11 11:02:52 -08005760 /*
5761 * The vmx_bitmap is not tied to a VM and so should
5762 * not be charged to a memcg.
5763 */
Sean Christopherson55d23752018-12-03 13:53:18 -08005764 vmx_bitmap[i] = (unsigned long *)
5765 __get_free_page(GFP_KERNEL);
5766 if (!vmx_bitmap[i]) {
5767 nested_vmx_hardware_unsetup();
5768 return -ENOMEM;
5769 }
5770 }
5771
5772 init_vmcs_shadow_fields();
5773 }
5774
5775 exit_handlers[EXIT_REASON_VMCLEAR] = handle_vmclear,
5776 exit_handlers[EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
5777 exit_handlers[EXIT_REASON_VMPTRLD] = handle_vmptrld,
5778 exit_handlers[EXIT_REASON_VMPTRST] = handle_vmptrst,
5779 exit_handlers[EXIT_REASON_VMREAD] = handle_vmread,
5780 exit_handlers[EXIT_REASON_VMRESUME] = handle_vmresume,
5781 exit_handlers[EXIT_REASON_VMWRITE] = handle_vmwrite,
5782 exit_handlers[EXIT_REASON_VMOFF] = handle_vmoff,
5783 exit_handlers[EXIT_REASON_VMON] = handle_vmon,
5784 exit_handlers[EXIT_REASON_INVEPT] = handle_invept,
5785 exit_handlers[EXIT_REASON_INVVPID] = handle_invvpid,
5786 exit_handlers[EXIT_REASON_VMFUNC] = handle_vmfunc,
5787
5788 kvm_x86_ops->check_nested_events = vmx_check_nested_events;
5789 kvm_x86_ops->get_nested_state = vmx_get_nested_state;
5790 kvm_x86_ops->set_nested_state = vmx_set_nested_state;
5791 kvm_x86_ops->get_vmcs12_pages = nested_get_vmcs12_pages,
5792 kvm_x86_ops->nested_enable_evmcs = nested_enable_evmcs;
Vitaly Kuznetsove2e871a2018-12-10 18:21:55 +01005793 kvm_x86_ops->nested_get_evmcs_version = nested_get_evmcs_version;
Sean Christopherson55d23752018-12-03 13:53:18 -08005794
5795 return 0;
5796}