blob: 1c38640dd4d8550ddf1af6661f3f4f2cd2d108f4 [file] [log] [blame]
Sean Christopherson55d23752018-12-03 13:53:18 -08001// SPDX-License-Identifier: GPL-2.0
2
Julien Thierry00089c02020-09-04 16:30:25 +01003#include <linux/objtool.h>
Sean Christopherson55d23752018-12-03 13:53:18 -08004#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"
Oliver Uptonbfc6ad62019-11-13 16:17:16 -080013#include "pmu.h"
Sean Christopherson55d23752018-12-03 13:53:18 -080014#include "trace.h"
Uros Bizjak150f17b2020-12-30 16:26:57 -080015#include "vmx.h"
Sean Christopherson55d23752018-12-03 13:53:18 -080016#include "x86.h"
17
18static bool __read_mostly enable_shadow_vmcs = 1;
19module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
20
21static bool __read_mostly nested_early_check = 0;
22module_param(nested_early_check, bool, S_IRUGO);
23
Sean Christopherson648fc8a2021-02-03 16:01:16 -080024#define CC KVM_NESTED_VMENTER_CONSISTENCY_CHECK
Sean Christopherson5497b952019-07-11 08:58:29 -070025
Sean Christopherson55d23752018-12-03 13:53:18 -080026/*
27 * Hyper-V requires all of these, so mark them as supported even though
28 * they are just treated the same as all-context.
29 */
30#define VMX_VPID_EXTENT_SUPPORTED_MASK \
31 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
32 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
33 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
34 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
35
36#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
37
38enum {
39 VMX_VMREAD_BITMAP,
40 VMX_VMWRITE_BITMAP,
41 VMX_BITMAP_NR
42};
43static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
44
45#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
46#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
47
Sean Christopherson1c6f0b42019-05-07 08:36:25 -070048struct shadow_vmcs_field {
49 u16 encoding;
50 u16 offset;
51};
52static struct shadow_vmcs_field shadow_read_only_fields[] = {
53#define SHADOW_FIELD_RO(x, y) { x, offsetof(struct vmcs12, y) },
Sean Christopherson55d23752018-12-03 13:53:18 -080054#include "vmcs_shadow_fields.h"
55};
56static int max_shadow_read_only_fields =
57 ARRAY_SIZE(shadow_read_only_fields);
58
Sean Christopherson1c6f0b42019-05-07 08:36:25 -070059static struct shadow_vmcs_field shadow_read_write_fields[] = {
60#define SHADOW_FIELD_RW(x, y) { x, offsetof(struct vmcs12, y) },
Sean Christopherson55d23752018-12-03 13:53:18 -080061#include "vmcs_shadow_fields.h"
62};
63static int max_shadow_read_write_fields =
64 ARRAY_SIZE(shadow_read_write_fields);
65
Yi Wang8997f652019-01-21 15:27:05 +080066static void init_vmcs_shadow_fields(void)
Sean Christopherson55d23752018-12-03 13:53:18 -080067{
68 int i, j;
69
70 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
71 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
72
73 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
Sean Christopherson1c6f0b42019-05-07 08:36:25 -070074 struct shadow_vmcs_field entry = shadow_read_only_fields[i];
75 u16 field = entry.encoding;
Sean Christopherson55d23752018-12-03 13:53:18 -080076
77 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
78 (i + 1 == max_shadow_read_only_fields ||
Sean Christopherson1c6f0b42019-05-07 08:36:25 -070079 shadow_read_only_fields[i + 1].encoding != field + 1))
Sean Christopherson55d23752018-12-03 13:53:18 -080080 pr_err("Missing field from shadow_read_only_field %x\n",
81 field + 1);
82
83 clear_bit(field, vmx_vmread_bitmap);
Sean Christopherson55d23752018-12-03 13:53:18 -080084 if (field & 1)
Sean Christopherson1c6f0b42019-05-07 08:36:25 -070085#ifdef CONFIG_X86_64
Sean Christopherson55d23752018-12-03 13:53:18 -080086 continue;
Sean Christopherson1c6f0b42019-05-07 08:36:25 -070087#else
88 entry.offset += sizeof(u32);
Sean Christopherson55d23752018-12-03 13:53:18 -080089#endif
Sean Christopherson1c6f0b42019-05-07 08:36:25 -070090 shadow_read_only_fields[j++] = entry;
Sean Christopherson55d23752018-12-03 13:53:18 -080091 }
92 max_shadow_read_only_fields = j;
93
94 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Sean Christopherson1c6f0b42019-05-07 08:36:25 -070095 struct shadow_vmcs_field entry = shadow_read_write_fields[i];
96 u16 field = entry.encoding;
Sean Christopherson55d23752018-12-03 13:53:18 -080097
98 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
99 (i + 1 == max_shadow_read_write_fields ||
Sean Christopherson1c6f0b42019-05-07 08:36:25 -0700100 shadow_read_write_fields[i + 1].encoding != field + 1))
Sean Christopherson55d23752018-12-03 13:53:18 -0800101 pr_err("Missing field from shadow_read_write_field %x\n",
102 field + 1);
103
Sean Christophersonb6437802019-05-07 08:36:24 -0700104 WARN_ONCE(field >= GUEST_ES_AR_BYTES &&
105 field <= GUEST_TR_AR_BYTES,
Sean Christopherson1c6f0b42019-05-07 08:36:25 -0700106 "Update vmcs12_write_any() to drop reserved bits from AR_BYTES");
Sean Christophersonb6437802019-05-07 08:36:24 -0700107
Sean Christopherson55d23752018-12-03 13:53:18 -0800108 /*
109 * PML and the preemption timer can be emulated, but the
110 * processor cannot vmwrite to fields that don't exist
111 * on bare metal.
112 */
113 switch (field) {
114 case GUEST_PML_INDEX:
115 if (!cpu_has_vmx_pml())
116 continue;
117 break;
118 case VMX_PREEMPTION_TIMER_VALUE:
119 if (!cpu_has_vmx_preemption_timer())
120 continue;
121 break;
122 case GUEST_INTR_STATUS:
123 if (!cpu_has_vmx_apicv())
124 continue;
125 break;
126 default:
127 break;
128 }
129
130 clear_bit(field, vmx_vmwrite_bitmap);
131 clear_bit(field, vmx_vmread_bitmap);
Sean Christopherson55d23752018-12-03 13:53:18 -0800132 if (field & 1)
Sean Christopherson1c6f0b42019-05-07 08:36:25 -0700133#ifdef CONFIG_X86_64
Sean Christopherson55d23752018-12-03 13:53:18 -0800134 continue;
Sean Christopherson1c6f0b42019-05-07 08:36:25 -0700135#else
136 entry.offset += sizeof(u32);
Sean Christopherson55d23752018-12-03 13:53:18 -0800137#endif
Sean Christopherson1c6f0b42019-05-07 08:36:25 -0700138 shadow_read_write_fields[j++] = entry;
Sean Christopherson55d23752018-12-03 13:53:18 -0800139 }
140 max_shadow_read_write_fields = j;
141}
142
143/*
144 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
145 * set the success or error code of an emulated VMX instruction (as specified
146 * by Vol 2B, VMX Instruction Reference, "Conventions"), and skip the emulated
147 * instruction.
148 */
149static int nested_vmx_succeed(struct kvm_vcpu *vcpu)
150{
151 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
152 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
153 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
154 return kvm_skip_emulated_instruction(vcpu);
155}
156
157static int nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
158{
159 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
160 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
161 X86_EFLAGS_SF | X86_EFLAGS_OF))
162 | X86_EFLAGS_CF);
163 return kvm_skip_emulated_instruction(vcpu);
164}
165
166static int nested_vmx_failValid(struct kvm_vcpu *vcpu,
167 u32 vm_instruction_error)
168{
Sean Christopherson55d23752018-12-03 13:53:18 -0800169 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
170 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
171 X86_EFLAGS_SF | X86_EFLAGS_OF))
172 | X86_EFLAGS_ZF);
173 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
174 /*
175 * We don't need to force a shadow sync because
176 * VM_INSTRUCTION_ERROR is not shadowed
177 */
178 return kvm_skip_emulated_instruction(vcpu);
179}
180
Sean Christophersonb2656e42020-06-08 18:56:07 -0700181static int nested_vmx_fail(struct kvm_vcpu *vcpu, u32 vm_instruction_error)
182{
183 struct vcpu_vmx *vmx = to_vmx(vcpu);
184
185 /*
186 * failValid writes the error number to the current VMCS, which
187 * can't be done if there isn't a current VMCS.
188 */
189 if (vmx->nested.current_vmptr == -1ull && !vmx->nested.hv_evmcs)
190 return nested_vmx_failInvalid(vcpu);
191
192 return nested_vmx_failValid(vcpu, vm_instruction_error);
193}
194
Sean Christopherson55d23752018-12-03 13:53:18 -0800195static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
196{
197 /* TODO: not to reset guest simply here. */
198 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
199 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
200}
201
Marc Orrf0b51052019-09-17 11:50:57 -0700202static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
203{
204 return fixed_bits_valid(control, low, high);
205}
206
207static inline u64 vmx_control_msr(u32 low, u32 high)
208{
209 return low | ((u64)high << 32);
210}
211
Sean Christopherson55d23752018-12-03 13:53:18 -0800212static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
213{
Sean Christophersonfe7f895d2019-05-07 12:17:57 -0700214 secondary_exec_controls_clearbit(vmx, SECONDARY_EXEC_SHADOW_VMCS);
Sean Christopherson55d23752018-12-03 13:53:18 -0800215 vmcs_write64(VMCS_LINK_POINTER, -1ull);
Paolo Bonzini88dddc12019-07-19 18:41:10 +0200216 vmx->nested.need_vmcs12_to_shadow_sync = false;
Sean Christopherson55d23752018-12-03 13:53:18 -0800217}
218
219static inline void nested_release_evmcs(struct kvm_vcpu *vcpu)
220{
221 struct vcpu_vmx *vmx = to_vmx(vcpu);
222
223 if (!vmx->nested.hv_evmcs)
224 return;
225
KarimAllah Ahmeddee9c042019-01-31 21:24:42 +0100226 kvm_vcpu_unmap(vcpu, &vmx->nested.hv_evmcs_map, true);
Vitaly Kuznetsov95fa1012020-03-09 16:52:11 +0100227 vmx->nested.hv_evmcs_vmptr = 0;
Sean Christopherson55d23752018-12-03 13:53:18 -0800228 vmx->nested.hv_evmcs = NULL;
229}
230
Sean Christophersonc61ca2f2020-09-23 11:44:49 -0700231static void vmx_sync_vmcs_host_state(struct vcpu_vmx *vmx,
232 struct loaded_vmcs *prev)
233{
234 struct vmcs_host_state *dest, *src;
235
236 if (unlikely(!vmx->guest_state_loaded))
237 return;
238
239 src = &prev->host_state;
240 dest = &vmx->loaded_vmcs->host_state;
241
242 vmx_set_host_fs_gs(dest, src->fs_sel, src->gs_sel, src->fs_base, src->gs_base);
243 dest->ldt_sel = src->ldt_sel;
244#ifdef CONFIG_X86_64
245 dest->ds_sel = src->ds_sel;
246 dest->es_sel = src->es_sel;
247#endif
248}
249
250static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
251{
252 struct vcpu_vmx *vmx = to_vmx(vcpu);
253 struct loaded_vmcs *prev;
254 int cpu;
255
Sean Christopherson138534a2020-09-23 11:44:52 -0700256 if (WARN_ON_ONCE(vmx->loaded_vmcs == vmcs))
Sean Christophersonc61ca2f2020-09-23 11:44:49 -0700257 return;
258
259 cpu = get_cpu();
260 prev = vmx->loaded_vmcs;
261 vmx->loaded_vmcs = vmcs;
262 vmx_vcpu_load_vmcs(vcpu, cpu, prev);
263 vmx_sync_vmcs_host_state(vmx, prev);
264 put_cpu();
265
266 vmx_register_cache_reset(vcpu);
267}
268
Sean Christopherson55d23752018-12-03 13:53:18 -0800269/*
270 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
271 * just stops using VMX.
272 */
273static void free_nested(struct kvm_vcpu *vcpu)
274{
275 struct vcpu_vmx *vmx = to_vmx(vcpu);
276
Sean Christophersondf82a242020-09-23 11:44:50 -0700277 if (WARN_ON_ONCE(vmx->loaded_vmcs != &vmx->vmcs01))
278 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
279
Sean Christopherson55d23752018-12-03 13:53:18 -0800280 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
281 return;
282
Paolo Bonzini729c15c2020-09-22 06:53:57 -0400283 kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
Jan Kiszkacf645272019-07-21 13:52:18 +0200284
Sean Christopherson55d23752018-12-03 13:53:18 -0800285 vmx->nested.vmxon = false;
286 vmx->nested.smm.vmxon = false;
287 free_vpid(vmx->nested.vpid02);
288 vmx->nested.posted_intr_nv = -1;
289 vmx->nested.current_vmptr = -1ull;
290 if (enable_shadow_vmcs) {
291 vmx_disable_shadow_vmcs(vmx);
292 vmcs_clear(vmx->vmcs01.shadow_vmcs);
293 free_vmcs(vmx->vmcs01.shadow_vmcs);
294 vmx->vmcs01.shadow_vmcs = NULL;
295 }
296 kfree(vmx->nested.cached_vmcs12);
Jan Kiszkac6bf2ae2019-07-21 16:01:36 +0200297 vmx->nested.cached_vmcs12 = NULL;
Sean Christopherson55d23752018-12-03 13:53:18 -0800298 kfree(vmx->nested.cached_shadow_vmcs12);
Jan Kiszkac6bf2ae2019-07-21 16:01:36 +0200299 vmx->nested.cached_shadow_vmcs12 = NULL;
Sean Christopherson55d23752018-12-03 13:53:18 -0800300 /* Unpin physical memory we referred to in the vmcs02 */
301 if (vmx->nested.apic_access_page) {
Liran Alonb11494b2019-11-21 00:31:47 +0200302 kvm_release_page_clean(vmx->nested.apic_access_page);
Sean Christopherson55d23752018-12-03 13:53:18 -0800303 vmx->nested.apic_access_page = NULL;
304 }
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +0100305 kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map, true);
KarimAllah Ahmed3278e042019-01-31 21:24:38 +0100306 kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map, true);
307 vmx->nested.pi_desc = NULL;
Sean Christopherson55d23752018-12-03 13:53:18 -0800308
309 kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
310
311 nested_release_evmcs(vcpu);
312
313 free_loaded_vmcs(&vmx->nested.vmcs02);
314}
315
Sean Christopherson55d23752018-12-03 13:53:18 -0800316/*
317 * Ensure that the current vmcs of the logical processor is the
318 * vmcs01 of the vcpu before calling free_nested().
319 */
320void nested_vmx_free_vcpu(struct kvm_vcpu *vcpu)
321{
322 vcpu_load(vcpu);
Paolo Bonzinib4b65b52019-01-29 19:12:35 +0100323 vmx_leave_nested(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -0800324 vcpu_put(vcpu);
325}
326
327static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
328 struct x86_exception *fault)
329{
330 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
331 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson4dcefa32020-04-15 10:55:18 -0700332 u32 vm_exit_reason;
Sean Christopherson55d23752018-12-03 13:53:18 -0800333 unsigned long exit_qualification = vcpu->arch.exit_qualification;
334
335 if (vmx->nested.pml_full) {
Sean Christopherson4dcefa32020-04-15 10:55:18 -0700336 vm_exit_reason = EXIT_REASON_PML_FULL;
Sean Christopherson55d23752018-12-03 13:53:18 -0800337 vmx->nested.pml_full = false;
338 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
339 } else if (fault->error_code & PFERR_RSVD_MASK)
Sean Christopherson4dcefa32020-04-15 10:55:18 -0700340 vm_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Sean Christopherson55d23752018-12-03 13:53:18 -0800341 else
Sean Christopherson4dcefa32020-04-15 10:55:18 -0700342 vm_exit_reason = EXIT_REASON_EPT_VIOLATION;
Sean Christopherson55d23752018-12-03 13:53:18 -0800343
Sean Christopherson4dcefa32020-04-15 10:55:18 -0700344 nested_vmx_vmexit(vcpu, vm_exit_reason, 0, exit_qualification);
Sean Christopherson55d23752018-12-03 13:53:18 -0800345 vmcs12->guest_physical_address = fault->address;
346}
347
348static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
349{
350 WARN_ON(mmu_is_nested(vcpu));
351
352 vcpu->arch.mmu = &vcpu->arch.guest_mmu;
353 kvm_init_shadow_ept_mmu(vcpu,
354 to_vmx(vcpu)->nested.msrs.ept_caps &
355 VMX_EPT_EXECUTE_ONLY_BIT,
356 nested_ept_ad_enabled(vcpu),
Sean Christophersonac69dfa2020-03-02 18:02:37 -0800357 nested_ept_get_eptp(vcpu));
Sean Christophersond8dd54e2020-03-02 18:02:39 -0800358 vcpu->arch.mmu->get_guest_pgd = nested_ept_get_eptp;
Sean Christopherson55d23752018-12-03 13:53:18 -0800359 vcpu->arch.mmu->inject_page_fault = nested_ept_inject_page_fault;
360 vcpu->arch.mmu->get_pdptr = kvm_pdptr_read;
361
362 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
363}
364
365static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
366{
367 vcpu->arch.mmu = &vcpu->arch.root_mmu;
368 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
369}
370
371static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
372 u16 error_code)
373{
374 bool inequality, bit;
375
376 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
377 inequality =
378 (error_code & vmcs12->page_fault_error_code_mask) !=
379 vmcs12->page_fault_error_code_match;
380 return inequality ^ bit;
381}
382
383
384/*
385 * KVM wants to inject page-faults which it got to the guest. This function
386 * checks whether in a nested guest, we need to inject them to L1 or L2.
387 */
388static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
389{
390 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
391 unsigned int nr = vcpu->arch.exception.nr;
392 bool has_payload = vcpu->arch.exception.has_payload;
393 unsigned long payload = vcpu->arch.exception.payload;
394
395 if (nr == PF_VECTOR) {
396 if (vcpu->arch.exception.nested_apf) {
397 *exit_qual = vcpu->arch.apf.nested_apf_token;
398 return 1;
399 }
400 if (nested_vmx_is_page_fault_vmexit(vmcs12,
401 vcpu->arch.exception.error_code)) {
402 *exit_qual = has_payload ? payload : vcpu->arch.cr2;
403 return 1;
404 }
405 } else if (vmcs12->exception_bitmap & (1u << nr)) {
406 if (nr == DB_VECTOR) {
407 if (!has_payload) {
408 payload = vcpu->arch.dr6;
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +0800409 payload &= ~DR6_BT;
410 payload ^= DR6_ACTIVE_LOW;
Sean Christopherson55d23752018-12-03 13:53:18 -0800411 }
412 *exit_qual = payload;
413 } else
414 *exit_qual = 0;
415 return 1;
416 }
417
418 return 0;
419}
420
421
422static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
423 struct x86_exception *fault)
424{
425 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
426
427 WARN_ON(!is_guest_mode(vcpu));
428
429 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
430 !to_vmx(vcpu)->nested.nested_run_pending) {
431 vmcs12->vm_exit_intr_error_code = fault->error_code;
432 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
433 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
434 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
435 fault->address);
436 } else {
437 kvm_inject_page_fault(vcpu, fault);
438 }
439}
440
Sean Christopherson55d23752018-12-03 13:53:18 -0800441static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
442 struct vmcs12 *vmcs12)
443{
444 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
445 return 0;
446
Sean Christopherson5497b952019-07-11 08:58:29 -0700447 if (CC(!page_address_valid(vcpu, vmcs12->io_bitmap_a)) ||
448 CC(!page_address_valid(vcpu, vmcs12->io_bitmap_b)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800449 return -EINVAL;
450
451 return 0;
452}
453
454static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
455 struct vmcs12 *vmcs12)
456{
457 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
458 return 0;
459
Sean Christopherson5497b952019-07-11 08:58:29 -0700460 if (CC(!page_address_valid(vcpu, vmcs12->msr_bitmap)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800461 return -EINVAL;
462
463 return 0;
464}
465
466static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
467 struct vmcs12 *vmcs12)
468{
469 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
470 return 0;
471
Sean Christopherson5497b952019-07-11 08:58:29 -0700472 if (CC(!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800473 return -EINVAL;
474
475 return 0;
476}
477
478/*
479 * Check if MSR is intercepted for L01 MSR bitmap.
480 */
481static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
482{
483 unsigned long *msr_bitmap;
484 int f = sizeof(unsigned long);
485
486 if (!cpu_has_vmx_msr_bitmap())
487 return true;
488
489 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
490
491 if (msr <= 0x1fff) {
492 return !!test_bit(msr, msr_bitmap + 0x800 / f);
493 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
494 msr &= 0x1fff;
495 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
496 }
497
498 return true;
499}
500
501/*
502 * If a msr is allowed by L0, we should check whether it is allowed by L1.
503 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
504 */
505static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
506 unsigned long *msr_bitmap_nested,
507 u32 msr, int type)
508{
509 int f = sizeof(unsigned long);
510
511 /*
512 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
513 * have the write-low and read-high bitmap offsets the wrong way round.
514 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
515 */
516 if (msr <= 0x1fff) {
517 if (type & MSR_TYPE_R &&
518 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
519 /* read-low */
520 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
521
522 if (type & MSR_TYPE_W &&
523 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
524 /* write-low */
525 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
526
527 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
528 msr &= 0x1fff;
529 if (type & MSR_TYPE_R &&
530 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
531 /* read-high */
532 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
533
534 if (type & MSR_TYPE_W &&
535 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
536 /* write-high */
537 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
538
539 }
540}
541
Miaohe Linffdbd502020-02-07 23:22:45 +0800542static inline void enable_x2apic_msr_intercepts(unsigned long *msr_bitmap)
543{
Marc Orracff7842019-04-01 23:55:59 -0700544 int msr;
545
546 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
547 unsigned word = msr / BITS_PER_LONG;
548
549 msr_bitmap[word] = ~0;
550 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
551 }
552}
553
Sean Christopherson55d23752018-12-03 13:53:18 -0800554/*
555 * Merge L0's and L1's MSR bitmap, return false to indicate that
556 * we do not use the hardware.
557 */
558static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
559 struct vmcs12 *vmcs12)
560{
561 int msr;
Sean Christopherson55d23752018-12-03 13:53:18 -0800562 unsigned long *msr_bitmap_l1;
563 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
KarimAllah Ahmed31f0b6c2019-01-31 21:24:36 +0100564 struct kvm_host_map *map = &to_vmx(vcpu)->nested.msr_bitmap_map;
Sean Christopherson55d23752018-12-03 13:53:18 -0800565
566 /* Nothing to do if the MSR bitmap is not in use. */
567 if (!cpu_has_vmx_msr_bitmap() ||
568 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
569 return false;
570
KarimAllah Ahmed31f0b6c2019-01-31 21:24:36 +0100571 if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->msr_bitmap), map))
Sean Christopherson55d23752018-12-03 13:53:18 -0800572 return false;
573
KarimAllah Ahmed31f0b6c2019-01-31 21:24:36 +0100574 msr_bitmap_l1 = (unsigned long *)map->hva;
Sean Christopherson55d23752018-12-03 13:53:18 -0800575
Marc Orracff7842019-04-01 23:55:59 -0700576 /*
577 * To keep the control flow simple, pay eight 8-byte writes (sixteen
578 * 4-byte writes on 32-bit systems) up front to enable intercepts for
579 * the x2APIC MSR range and selectively disable them below.
580 */
581 enable_x2apic_msr_intercepts(msr_bitmap_l0);
Sean Christopherson55d23752018-12-03 13:53:18 -0800582
Marc Orracff7842019-04-01 23:55:59 -0700583 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
584 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
585 /*
586 * L0 need not intercept reads for MSRs between 0x800
587 * and 0x8ff, it just lets the processor take the value
588 * from the virtual-APIC page; take those 256 bits
589 * directly from the L1 bitmap.
590 */
591 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
592 unsigned word = msr / BITS_PER_LONG;
593
594 msr_bitmap_l0[word] = msr_bitmap_l1[word];
595 }
596 }
597
Sean Christopherson55d23752018-12-03 13:53:18 -0800598 nested_vmx_disable_intercept_for_msr(
599 msr_bitmap_l1, msr_bitmap_l0,
Marc Orracff7842019-04-01 23:55:59 -0700600 X2APIC_MSR(APIC_TASKPRI),
Marc Orrc73f4c92019-04-01 23:56:00 -0700601 MSR_TYPE_R | MSR_TYPE_W);
Marc Orracff7842019-04-01 23:55:59 -0700602
603 if (nested_cpu_has_vid(vmcs12)) {
604 nested_vmx_disable_intercept_for_msr(
605 msr_bitmap_l1, msr_bitmap_l0,
606 X2APIC_MSR(APIC_EOI),
607 MSR_TYPE_W);
608 nested_vmx_disable_intercept_for_msr(
609 msr_bitmap_l1, msr_bitmap_l0,
610 X2APIC_MSR(APIC_SELF_IPI),
611 MSR_TYPE_W);
612 }
Sean Christopherson55d23752018-12-03 13:53:18 -0800613 }
614
Sean Christophersond69129b2019-05-08 07:32:15 -0700615 /* KVM unconditionally exposes the FS/GS base MSRs to L1. */
616 nested_vmx_disable_intercept_for_msr(msr_bitmap_l1, msr_bitmap_l0,
617 MSR_FS_BASE, MSR_TYPE_RW);
618
619 nested_vmx_disable_intercept_for_msr(msr_bitmap_l1, msr_bitmap_l0,
620 MSR_GS_BASE, MSR_TYPE_RW);
621
622 nested_vmx_disable_intercept_for_msr(msr_bitmap_l1, msr_bitmap_l0,
623 MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
624
625 /*
626 * Checking the L0->L1 bitmap is trying to verify two things:
627 *
628 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
629 * ensures that we do not accidentally generate an L02 MSR bitmap
630 * from the L12 MSR bitmap that is too permissive.
631 * 2. That L1 or L2s have actually used the MSR. This avoids
632 * unnecessarily merging of the bitmap if the MSR is unused. This
633 * works properly because we only update the L01 MSR bitmap lazily.
634 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
635 * updated to reflect this when L1 (or its L2s) actually write to
636 * the MSR.
637 */
638 if (!msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL))
Sean Christopherson55d23752018-12-03 13:53:18 -0800639 nested_vmx_disable_intercept_for_msr(
640 msr_bitmap_l1, msr_bitmap_l0,
641 MSR_IA32_SPEC_CTRL,
642 MSR_TYPE_R | MSR_TYPE_W);
643
Sean Christophersond69129b2019-05-08 07:32:15 -0700644 if (!msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD))
Sean Christopherson55d23752018-12-03 13:53:18 -0800645 nested_vmx_disable_intercept_for_msr(
646 msr_bitmap_l1, msr_bitmap_l0,
647 MSR_IA32_PRED_CMD,
648 MSR_TYPE_W);
649
KarimAllah Ahmed31f0b6c2019-01-31 21:24:36 +0100650 kvm_vcpu_unmap(vcpu, &to_vmx(vcpu)->nested.msr_bitmap_map, false);
Sean Christopherson55d23752018-12-03 13:53:18 -0800651
652 return true;
653}
654
655static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
656 struct vmcs12 *vmcs12)
657{
KarimAllah Ahmed88925302019-01-31 21:24:41 +0100658 struct kvm_host_map map;
Sean Christopherson55d23752018-12-03 13:53:18 -0800659 struct vmcs12 *shadow;
Sean Christopherson55d23752018-12-03 13:53:18 -0800660
661 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
662 vmcs12->vmcs_link_pointer == -1ull)
663 return;
664
665 shadow = get_shadow_vmcs12(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -0800666
KarimAllah Ahmed88925302019-01-31 21:24:41 +0100667 if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->vmcs_link_pointer), &map))
668 return;
Sean Christopherson55d23752018-12-03 13:53:18 -0800669
KarimAllah Ahmed88925302019-01-31 21:24:41 +0100670 memcpy(shadow, map.hva, VMCS12_SIZE);
671 kvm_vcpu_unmap(vcpu, &map, false);
Sean Christopherson55d23752018-12-03 13:53:18 -0800672}
673
674static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
675 struct vmcs12 *vmcs12)
676{
677 struct vcpu_vmx *vmx = to_vmx(vcpu);
678
679 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
680 vmcs12->vmcs_link_pointer == -1ull)
681 return;
682
683 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
684 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
685}
686
687/*
688 * In nested virtualization, check if L1 has set
689 * VM_EXIT_ACK_INTR_ON_EXIT
690 */
691static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
692{
693 return get_vmcs12(vcpu)->vm_exit_controls &
694 VM_EXIT_ACK_INTR_ON_EXIT;
695}
696
Sean Christopherson55d23752018-12-03 13:53:18 -0800697static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
698 struct vmcs12 *vmcs12)
699{
700 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
Sean Christopherson5497b952019-07-11 08:58:29 -0700701 CC(!page_address_valid(vcpu, vmcs12->apic_access_addr)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800702 return -EINVAL;
703 else
704 return 0;
705}
706
707static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
708 struct vmcs12 *vmcs12)
709{
710 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
711 !nested_cpu_has_apic_reg_virt(vmcs12) &&
712 !nested_cpu_has_vid(vmcs12) &&
713 !nested_cpu_has_posted_intr(vmcs12))
714 return 0;
715
716 /*
717 * If virtualize x2apic mode is enabled,
718 * virtualize apic access must be disabled.
719 */
Sean Christopherson5497b952019-07-11 08:58:29 -0700720 if (CC(nested_cpu_has_virt_x2apic_mode(vmcs12) &&
721 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800722 return -EINVAL;
723
724 /*
725 * If virtual interrupt delivery is enabled,
726 * we must exit on external interrupts.
727 */
Sean Christopherson5497b952019-07-11 08:58:29 -0700728 if (CC(nested_cpu_has_vid(vmcs12) && !nested_exit_on_intr(vcpu)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800729 return -EINVAL;
730
731 /*
732 * bits 15:8 should be zero in posted_intr_nv,
733 * the descriptor address has been already checked
734 * in nested_get_vmcs12_pages.
735 *
736 * bits 5:0 of posted_intr_desc_addr should be zero.
737 */
738 if (nested_cpu_has_posted_intr(vmcs12) &&
Sean Christopherson5497b952019-07-11 08:58:29 -0700739 (CC(!nested_cpu_has_vid(vmcs12)) ||
740 CC(!nested_exit_intr_ack_set(vcpu)) ||
741 CC((vmcs12->posted_intr_nv & 0xff00)) ||
Sean Christopherson636e8b72021-02-03 16:01:10 -0800742 CC(!kvm_vcpu_is_legal_aligned_gpa(vcpu, vmcs12->posted_intr_desc_addr, 64))))
Sean Christopherson55d23752018-12-03 13:53:18 -0800743 return -EINVAL;
744
745 /* tpr shadow is needed by all apicv features. */
Sean Christopherson5497b952019-07-11 08:58:29 -0700746 if (CC(!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800747 return -EINVAL;
748
749 return 0;
750}
751
752static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
Sean Christophersonf9b245e2018-12-12 13:30:08 -0500753 u32 count, u64 addr)
Sean Christopherson55d23752018-12-03 13:53:18 -0800754{
Sean Christopherson55d23752018-12-03 13:53:18 -0800755 if (count == 0)
756 return 0;
Sean Christopherson636e8b72021-02-03 16:01:10 -0800757
758 if (!kvm_vcpu_is_legal_aligned_gpa(vcpu, addr, 16) ||
759 !kvm_vcpu_is_legal_gpa(vcpu, (addr + count * sizeof(struct vmx_msr_entry) - 1)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800760 return -EINVAL;
Sean Christophersonf9b245e2018-12-12 13:30:08 -0500761
Sean Christopherson55d23752018-12-03 13:53:18 -0800762 return 0;
763}
764
Krish Sadhukhan61446ba2018-12-12 13:30:09 -0500765static int nested_vmx_check_exit_msr_switch_controls(struct kvm_vcpu *vcpu,
766 struct vmcs12 *vmcs12)
Sean Christopherson55d23752018-12-03 13:53:18 -0800767{
Sean Christopherson5497b952019-07-11 08:58:29 -0700768 if (CC(nested_vmx_check_msr_switch(vcpu,
769 vmcs12->vm_exit_msr_load_count,
770 vmcs12->vm_exit_msr_load_addr)) ||
771 CC(nested_vmx_check_msr_switch(vcpu,
772 vmcs12->vm_exit_msr_store_count,
773 vmcs12->vm_exit_msr_store_addr)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800774 return -EINVAL;
Sean Christophersonf9b245e2018-12-12 13:30:08 -0500775
Sean Christopherson55d23752018-12-03 13:53:18 -0800776 return 0;
777}
778
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -0500779static int nested_vmx_check_entry_msr_switch_controls(struct kvm_vcpu *vcpu,
780 struct vmcs12 *vmcs12)
Krish Sadhukhan61446ba2018-12-12 13:30:09 -0500781{
Sean Christopherson5497b952019-07-11 08:58:29 -0700782 if (CC(nested_vmx_check_msr_switch(vcpu,
783 vmcs12->vm_entry_msr_load_count,
784 vmcs12->vm_entry_msr_load_addr)))
Krish Sadhukhan61446ba2018-12-12 13:30:09 -0500785 return -EINVAL;
786
787 return 0;
788}
789
Sean Christopherson55d23752018-12-03 13:53:18 -0800790static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
791 struct vmcs12 *vmcs12)
792{
793 if (!nested_cpu_has_pml(vmcs12))
794 return 0;
795
Sean Christopherson5497b952019-07-11 08:58:29 -0700796 if (CC(!nested_cpu_has_ept(vmcs12)) ||
797 CC(!page_address_valid(vcpu, vmcs12->pml_address)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800798 return -EINVAL;
799
800 return 0;
801}
802
803static int nested_vmx_check_unrestricted_guest_controls(struct kvm_vcpu *vcpu,
804 struct vmcs12 *vmcs12)
805{
Sean Christopherson5497b952019-07-11 08:58:29 -0700806 if (CC(nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST) &&
807 !nested_cpu_has_ept(vmcs12)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800808 return -EINVAL;
809 return 0;
810}
811
812static int nested_vmx_check_mode_based_ept_exec_controls(struct kvm_vcpu *vcpu,
813 struct vmcs12 *vmcs12)
814{
Sean Christopherson5497b952019-07-11 08:58:29 -0700815 if (CC(nested_cpu_has2(vmcs12, SECONDARY_EXEC_MODE_BASED_EPT_EXEC) &&
816 !nested_cpu_has_ept(vmcs12)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800817 return -EINVAL;
818 return 0;
819}
820
821static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
822 struct vmcs12 *vmcs12)
823{
824 if (!nested_cpu_has_shadow_vmcs(vmcs12))
825 return 0;
826
Sean Christopherson5497b952019-07-11 08:58:29 -0700827 if (CC(!page_address_valid(vcpu, vmcs12->vmread_bitmap)) ||
828 CC(!page_address_valid(vcpu, vmcs12->vmwrite_bitmap)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800829 return -EINVAL;
830
831 return 0;
832}
833
834static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
835 struct vmx_msr_entry *e)
836{
837 /* x2APIC MSR accesses are not allowed */
Sean Christopherson5497b952019-07-11 08:58:29 -0700838 if (CC(vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8))
Sean Christopherson55d23752018-12-03 13:53:18 -0800839 return -EINVAL;
Sean Christopherson5497b952019-07-11 08:58:29 -0700840 if (CC(e->index == MSR_IA32_UCODE_WRITE) || /* SDM Table 35-2 */
841 CC(e->index == MSR_IA32_UCODE_REV))
Sean Christopherson55d23752018-12-03 13:53:18 -0800842 return -EINVAL;
Sean Christopherson5497b952019-07-11 08:58:29 -0700843 if (CC(e->reserved != 0))
Sean Christopherson55d23752018-12-03 13:53:18 -0800844 return -EINVAL;
845 return 0;
846}
847
848static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
849 struct vmx_msr_entry *e)
850{
Sean Christopherson5497b952019-07-11 08:58:29 -0700851 if (CC(e->index == MSR_FS_BASE) ||
852 CC(e->index == MSR_GS_BASE) ||
853 CC(e->index == MSR_IA32_SMM_MONITOR_CTL) || /* SMM is not supported */
Sean Christopherson55d23752018-12-03 13:53:18 -0800854 nested_vmx_msr_check_common(vcpu, e))
855 return -EINVAL;
856 return 0;
857}
858
859static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
860 struct vmx_msr_entry *e)
861{
Sean Christopherson5497b952019-07-11 08:58:29 -0700862 if (CC(e->index == MSR_IA32_SMBASE) || /* SMM is not supported */
Sean Christopherson55d23752018-12-03 13:53:18 -0800863 nested_vmx_msr_check_common(vcpu, e))
864 return -EINVAL;
865 return 0;
866}
867
Marc Orrf0b51052019-09-17 11:50:57 -0700868static u32 nested_vmx_max_atomic_switch_msrs(struct kvm_vcpu *vcpu)
869{
870 struct vcpu_vmx *vmx = to_vmx(vcpu);
871 u64 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
872 vmx->nested.msrs.misc_high);
873
874 return (vmx_misc_max_msr(vmx_misc) + 1) * VMX_MISC_MSR_LIST_MULTIPLIER;
875}
876
Sean Christopherson55d23752018-12-03 13:53:18 -0800877/*
878 * Load guest's/host's msr at nested entry/exit.
879 * return 0 for success, entry index for failure.
Marc Orrf0b51052019-09-17 11:50:57 -0700880 *
881 * One of the failure modes for MSR load/store is when a list exceeds the
882 * virtual hardware's capacity. To maintain compatibility with hardware inasmuch
883 * as possible, process all valid entries before failing rather than precheck
884 * for a capacity violation.
Sean Christopherson55d23752018-12-03 13:53:18 -0800885 */
886static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
887{
888 u32 i;
889 struct vmx_msr_entry e;
Marc Orrf0b51052019-09-17 11:50:57 -0700890 u32 max_msr_list_size = nested_vmx_max_atomic_switch_msrs(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -0800891
Sean Christopherson55d23752018-12-03 13:53:18 -0800892 for (i = 0; i < count; i++) {
Marc Orrf0b51052019-09-17 11:50:57 -0700893 if (unlikely(i >= max_msr_list_size))
894 goto fail;
895
Sean Christopherson55d23752018-12-03 13:53:18 -0800896 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
897 &e, sizeof(e))) {
898 pr_debug_ratelimited(
899 "%s cannot read MSR entry (%u, 0x%08llx)\n",
900 __func__, i, gpa + i * sizeof(e));
901 goto fail;
902 }
903 if (nested_vmx_load_msr_check(vcpu, &e)) {
904 pr_debug_ratelimited(
905 "%s check failed (%u, 0x%x, 0x%x)\n",
906 __func__, i, e.index, e.reserved);
907 goto fail;
908 }
Sean Christophersonf20935d2019-09-05 14:22:54 -0700909 if (kvm_set_msr(vcpu, e.index, e.value)) {
Sean Christopherson55d23752018-12-03 13:53:18 -0800910 pr_debug_ratelimited(
911 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
912 __func__, i, e.index, e.value);
913 goto fail;
914 }
915 }
916 return 0;
917fail:
Sean Christopherson68cda402020-05-11 15:05:29 -0700918 /* Note, max_msr_list_size is at most 4096, i.e. this can't wrap. */
Sean Christopherson55d23752018-12-03 13:53:18 -0800919 return i + 1;
920}
921
Aaron Lewis662f1d12019-11-07 21:14:39 -0800922static bool nested_vmx_get_vmexit_msr_value(struct kvm_vcpu *vcpu,
923 u32 msr_index,
924 u64 *data)
925{
926 struct vcpu_vmx *vmx = to_vmx(vcpu);
927
928 /*
929 * If the L0 hypervisor stored a more accurate value for the TSC that
930 * does not include the time taken for emulation of the L2->L1
931 * VM-exit in L0, use the more accurate value.
932 */
933 if (msr_index == MSR_IA32_TSC) {
Sean Christophersona128a932020-09-23 11:03:57 -0700934 int i = vmx_find_loadstore_msr_slot(&vmx->msr_autostore.guest,
935 MSR_IA32_TSC);
Aaron Lewis662f1d12019-11-07 21:14:39 -0800936
Sean Christophersona128a932020-09-23 11:03:57 -0700937 if (i >= 0) {
938 u64 val = vmx->msr_autostore.guest.val[i].value;
Aaron Lewis662f1d12019-11-07 21:14:39 -0800939
940 *data = kvm_read_l1_tsc(vcpu, val);
941 return true;
942 }
943 }
944
945 if (kvm_get_msr(vcpu, msr_index, data)) {
946 pr_debug_ratelimited("%s cannot read MSR (0x%x)\n", __func__,
947 msr_index);
948 return false;
949 }
950 return true;
951}
952
Aaron Lewis365d3d52019-11-07 21:14:36 -0800953static bool read_and_check_msr_entry(struct kvm_vcpu *vcpu, u64 gpa, int i,
954 struct vmx_msr_entry *e)
955{
956 if (kvm_vcpu_read_guest(vcpu,
957 gpa + i * sizeof(*e),
958 e, 2 * sizeof(u32))) {
959 pr_debug_ratelimited(
960 "%s cannot read MSR entry (%u, 0x%08llx)\n",
961 __func__, i, gpa + i * sizeof(*e));
962 return false;
963 }
964 if (nested_vmx_store_msr_check(vcpu, e)) {
965 pr_debug_ratelimited(
966 "%s check failed (%u, 0x%x, 0x%x)\n",
967 __func__, i, e->index, e->reserved);
968 return false;
969 }
970 return true;
971}
972
Sean Christopherson55d23752018-12-03 13:53:18 -0800973static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
974{
Sean Christophersonf20935d2019-09-05 14:22:54 -0700975 u64 data;
Sean Christopherson55d23752018-12-03 13:53:18 -0800976 u32 i;
977 struct vmx_msr_entry e;
Marc Orrf0b51052019-09-17 11:50:57 -0700978 u32 max_msr_list_size = nested_vmx_max_atomic_switch_msrs(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -0800979
980 for (i = 0; i < count; i++) {
Marc Orrf0b51052019-09-17 11:50:57 -0700981 if (unlikely(i >= max_msr_list_size))
982 return -EINVAL;
983
Aaron Lewis365d3d52019-11-07 21:14:36 -0800984 if (!read_and_check_msr_entry(vcpu, gpa, i, &e))
Sean Christopherson55d23752018-12-03 13:53:18 -0800985 return -EINVAL;
Aaron Lewis365d3d52019-11-07 21:14:36 -0800986
Aaron Lewis662f1d12019-11-07 21:14:39 -0800987 if (!nested_vmx_get_vmexit_msr_value(vcpu, e.index, &data))
Sean Christopherson55d23752018-12-03 13:53:18 -0800988 return -EINVAL;
Aaron Lewis662f1d12019-11-07 21:14:39 -0800989
Sean Christopherson55d23752018-12-03 13:53:18 -0800990 if (kvm_vcpu_write_guest(vcpu,
991 gpa + i * sizeof(e) +
992 offsetof(struct vmx_msr_entry, value),
Sean Christophersonf20935d2019-09-05 14:22:54 -0700993 &data, sizeof(data))) {
Sean Christopherson55d23752018-12-03 13:53:18 -0800994 pr_debug_ratelimited(
995 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Sean Christophersonf20935d2019-09-05 14:22:54 -0700996 __func__, i, e.index, data);
Sean Christopherson55d23752018-12-03 13:53:18 -0800997 return -EINVAL;
998 }
999 }
1000 return 0;
1001}
1002
Aaron Lewis662f1d12019-11-07 21:14:39 -08001003static bool nested_msr_store_list_has_msr(struct kvm_vcpu *vcpu, u32 msr_index)
1004{
1005 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1006 u32 count = vmcs12->vm_exit_msr_store_count;
1007 u64 gpa = vmcs12->vm_exit_msr_store_addr;
1008 struct vmx_msr_entry e;
1009 u32 i;
1010
1011 for (i = 0; i < count; i++) {
1012 if (!read_and_check_msr_entry(vcpu, gpa, i, &e))
1013 return false;
1014
1015 if (e.index == msr_index)
1016 return true;
1017 }
1018 return false;
1019}
1020
1021static void prepare_vmx_msr_autostore_list(struct kvm_vcpu *vcpu,
1022 u32 msr_index)
1023{
1024 struct vcpu_vmx *vmx = to_vmx(vcpu);
1025 struct vmx_msrs *autostore = &vmx->msr_autostore.guest;
1026 bool in_vmcs12_store_list;
Sean Christophersona128a932020-09-23 11:03:57 -07001027 int msr_autostore_slot;
Aaron Lewis662f1d12019-11-07 21:14:39 -08001028 bool in_autostore_list;
1029 int last;
1030
Sean Christophersona128a932020-09-23 11:03:57 -07001031 msr_autostore_slot = vmx_find_loadstore_msr_slot(autostore, msr_index);
1032 in_autostore_list = msr_autostore_slot >= 0;
Aaron Lewis662f1d12019-11-07 21:14:39 -08001033 in_vmcs12_store_list = nested_msr_store_list_has_msr(vcpu, msr_index);
1034
1035 if (in_vmcs12_store_list && !in_autostore_list) {
Sean Christophersonce833b22020-09-23 11:03:56 -07001036 if (autostore->nr == MAX_NR_LOADSTORE_MSRS) {
Aaron Lewis662f1d12019-11-07 21:14:39 -08001037 /*
1038 * Emulated VMEntry does not fail here. Instead a less
1039 * accurate value will be returned by
1040 * nested_vmx_get_vmexit_msr_value() using kvm_get_msr()
1041 * instead of reading the value from the vmcs02 VMExit
1042 * MSR-store area.
1043 */
1044 pr_warn_ratelimited(
1045 "Not enough msr entries in msr_autostore. Can't add msr %x\n",
1046 msr_index);
1047 return;
1048 }
1049 last = autostore->nr++;
1050 autostore->val[last].index = msr_index;
1051 } else if (!in_vmcs12_store_list && in_autostore_list) {
1052 last = --autostore->nr;
Sean Christophersona128a932020-09-23 11:03:57 -07001053 autostore->val[msr_autostore_slot] = autostore->val[last];
Aaron Lewis662f1d12019-11-07 21:14:39 -08001054 }
1055}
1056
Sean Christopherson55d23752018-12-03 13:53:18 -08001057/*
Sean Christopherson41fab65e2020-03-20 14:28:29 -07001058 * Returns true if the MMU needs to be sync'd on nested VM-Enter/VM-Exit.
1059 * tl;dr: the MMU needs a sync if L0 is using shadow paging and L1 didn't
1060 * enable VPID for L2 (implying it expects a TLB flush on VMX transitions).
1061 * Here's why.
1062 *
1063 * If EPT is enabled by L0 a sync is never needed:
1064 * - if it is disabled by L1, then L0 is not shadowing L1 or L2 PTEs, there
1065 * cannot be unsync'd SPTEs for either L1 or L2.
1066 *
1067 * - if it is also enabled by L1, then L0 doesn't need to sync on VM-Enter
1068 * VM-Enter as VM-Enter isn't required to invalidate guest-physical mappings
1069 * (irrespective of VPID), i.e. L1 can't rely on the (virtual) CPU to flush
1070 * stale guest-physical mappings for L2 from the TLB. And as above, L0 isn't
1071 * shadowing L1 PTEs so there are no unsync'd SPTEs to sync on VM-Exit.
1072 *
1073 * If EPT is disabled by L0:
1074 * - if VPID is enabled by L1 (for L2), the situation is similar to when L1
1075 * enables EPT: L0 doesn't need to sync as VM-Enter and VM-Exit aren't
1076 * required to invalidate linear mappings (EPT is disabled so there are
1077 * no combined or guest-physical mappings), i.e. L1 can't rely on the
1078 * (virtual) CPU to flush stale linear mappings for either L2 or itself (L1).
1079 *
1080 * - however if VPID is disabled by L1, then a sync is needed as L1 expects all
1081 * linear mappings (EPT is disabled so there are no combined or guest-physical
1082 * mappings) to be invalidated on both VM-Enter and VM-Exit.
1083 *
1084 * Note, this logic is subtly different than nested_has_guest_tlb_tag(), which
1085 * additionally checks that L2 has been assigned a VPID (when EPT is disabled).
1086 * Whether or not L2 has been assigned a VPID by L0 is irrelevant with respect
1087 * to L1's expectations, e.g. L0 needs to invalidate hardware TLB entries if L2
1088 * doesn't have a unique VPID to prevent reusing L1's entries (assuming L1 has
1089 * been assigned a VPID), but L0 doesn't need to do a MMU sync because L1
1090 * doesn't expect stale (virtual) TLB entries to be flushed, i.e. L1 doesn't
1091 * know that L0 will flush the TLB and so L1 will do INVVPID as needed to flush
1092 * stale TLB entries, at which point L0 will sync L2's MMU.
1093 */
1094static bool nested_vmx_transition_mmu_sync(struct kvm_vcpu *vcpu)
1095{
1096 return !enable_ept && !nested_cpu_has_vpid(get_vmcs12(vcpu));
1097}
1098
1099/*
Sean Christophersonea79a752020-02-04 07:32:59 -08001100 * Load guest's/host's cr3 at nested entry/exit. @nested_ept is true if we are
1101 * emulating VM-Entry into a guest with EPT enabled. On failure, the expected
1102 * Exit Qualification (for a VM-Entry consistency check VM-Exit) is assigned to
1103 * @entry_failure_code.
Sean Christopherson55d23752018-12-03 13:53:18 -08001104 */
1105static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Sean Christopherson68cda402020-05-11 15:05:29 -07001106 enum vm_entry_failure_code *entry_failure_code)
Sean Christopherson55d23752018-12-03 13:53:18 -08001107{
Sean Christopherson636e8b72021-02-03 16:01:10 -08001108 if (CC(kvm_vcpu_is_illegal_gpa(vcpu, cr3))) {
Sean Christopherson0cc69202020-05-01 21:32:26 -07001109 *entry_failure_code = ENTRY_FAIL_DEFAULT;
1110 return -EINVAL;
1111 }
Sean Christopherson55d23752018-12-03 13:53:18 -08001112
Sean Christopherson0cc69202020-05-01 21:32:26 -07001113 /*
1114 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
1115 * must not be dereferenced.
1116 */
1117 if (!nested_ept && is_pae_paging(vcpu) &&
1118 (cr3 != kvm_read_cr3(vcpu) || pdptrs_changed(vcpu))) {
1119 if (CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))) {
1120 *entry_failure_code = ENTRY_FAIL_PDPTE;
1121 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08001122 }
1123 }
1124
Sean Christopherson41fab65e2020-03-20 14:28:29 -07001125 /*
Sean Christopherson9805c5f2020-03-20 14:28:30 -07001126 * Unconditionally skip the TLB flush on fast CR3 switch, all TLB
1127 * flushes are handled by nested_vmx_transition_tlb_flush(). See
1128 * nested_vmx_transition_mmu_sync for details on skipping the MMU sync.
Sean Christopherson41fab65e2020-03-20 14:28:29 -07001129 */
Sean Christopherson55d23752018-12-03 13:53:18 -08001130 if (!nested_ept)
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07001131 kvm_mmu_new_pgd(vcpu, cr3, true,
Sean Christopherson41fab65e2020-03-20 14:28:29 -07001132 !nested_vmx_transition_mmu_sync(vcpu));
Sean Christopherson55d23752018-12-03 13:53:18 -08001133
1134 vcpu->arch.cr3 = cr3;
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07001135 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Sean Christopherson55d23752018-12-03 13:53:18 -08001136
1137 kvm_init_mmu(vcpu, false);
1138
1139 return 0;
1140}
1141
1142/*
1143 * Returns if KVM is able to config CPU to tag TLB entries
1144 * populated by L2 differently than TLB entries populated
1145 * by L1.
1146 *
Liran Alon992edea2019-11-20 14:24:52 +02001147 * If L0 uses EPT, L1 and L2 run with different EPTP because
1148 * guest_mode is part of kvm_mmu_page_role. Thus, TLB entries
1149 * are tagged with different EPTP.
Sean Christopherson55d23752018-12-03 13:53:18 -08001150 *
1151 * If L1 uses VPID and we allocated a vpid02, TLB entries are tagged
1152 * with different VPID (L1 entries are tagged with vmx->vpid
1153 * while L2 entries are tagged with vmx->nested.vpid02).
1154 */
1155static bool nested_has_guest_tlb_tag(struct kvm_vcpu *vcpu)
1156{
1157 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1158
Liran Alon992edea2019-11-20 14:24:52 +02001159 return enable_ept ||
Sean Christopherson55d23752018-12-03 13:53:18 -08001160 (nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02);
1161}
1162
Sean Christopherson50b265a2020-03-20 14:28:19 -07001163static void nested_vmx_transition_tlb_flush(struct kvm_vcpu *vcpu,
1164 struct vmcs12 *vmcs12,
1165 bool is_vmenter)
1166{
1167 struct vcpu_vmx *vmx = to_vmx(vcpu);
1168
1169 /*
1170 * If VPID is disabled, linear and combined mappings are flushed on
1171 * VM-Enter/VM-Exit, and guest-physical mappings are valid only for
1172 * their associated EPTP.
1173 */
1174 if (!enable_vpid)
1175 return;
1176
1177 /*
1178 * If vmcs12 doesn't use VPID, L1 expects linear and combined mappings
1179 * for *all* contexts to be flushed on VM-Enter/VM-Exit.
1180 *
1181 * If VPID is enabled and used by vmc12, but L2 does not have a unique
1182 * TLB tag (ASID), i.e. EPT is disabled and KVM was unable to allocate
Sean Christophersonc51e1ff2020-03-20 14:28:22 -07001183 * a VPID for L2, flush the current context as the effective ASID is
1184 * common to both L1 and L2.
Sean Christopherson50b265a2020-03-20 14:28:19 -07001185 *
1186 * Defer the flush so that it runs after vmcs02.EPTP has been set by
1187 * KVM_REQ_LOAD_MMU_PGD (if nested EPT is enabled) and to avoid
1188 * redundant flushes further down the nested pipeline.
1189 *
1190 * If a TLB flush isn't required due to any of the above, and vpid12 is
1191 * changing then the new "virtual" VPID (vpid12) will reuse the same
1192 * "real" VPID (vpid02), and so needs to be sync'd. There is no direct
1193 * mapping between vpid02 and vpid12, vpid02 is per-vCPU and reused for
1194 * all nested vCPUs.
1195 */
Sean Christophersonc51e1ff2020-03-20 14:28:22 -07001196 if (!nested_cpu_has_vpid(vmcs12)) {
Sean Christopherson50b265a2020-03-20 14:28:19 -07001197 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Sean Christophersonc51e1ff2020-03-20 14:28:22 -07001198 } else if (!nested_has_guest_tlb_tag(vcpu)) {
1199 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
Sean Christopherson50b265a2020-03-20 14:28:19 -07001200 } else if (is_vmenter &&
1201 vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
1202 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
1203 vpid_sync_context(nested_get_vpid02(vcpu));
1204 }
1205}
1206
Sean Christopherson55d23752018-12-03 13:53:18 -08001207static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
1208{
1209 superset &= mask;
1210 subset &= mask;
1211
1212 return (superset | subset) == superset;
1213}
1214
1215static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
1216{
1217 const u64 feature_and_reserved =
1218 /* feature (except bit 48; see below) */
1219 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
1220 /* reserved */
1221 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
1222 u64 vmx_basic = vmx->nested.msrs.basic;
1223
1224 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
1225 return -EINVAL;
1226
1227 /*
1228 * KVM does not emulate a version of VMX that constrains physical
1229 * addresses of VMX structures (e.g. VMCS) to 32-bits.
1230 */
1231 if (data & BIT_ULL(48))
1232 return -EINVAL;
1233
1234 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
1235 vmx_basic_vmcs_revision_id(data))
1236 return -EINVAL;
1237
1238 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
1239 return -EINVAL;
1240
1241 vmx->nested.msrs.basic = data;
1242 return 0;
1243}
1244
1245static int
1246vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
1247{
1248 u64 supported;
1249 u32 *lowp, *highp;
1250
1251 switch (msr_index) {
1252 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
1253 lowp = &vmx->nested.msrs.pinbased_ctls_low;
1254 highp = &vmx->nested.msrs.pinbased_ctls_high;
1255 break;
1256 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
1257 lowp = &vmx->nested.msrs.procbased_ctls_low;
1258 highp = &vmx->nested.msrs.procbased_ctls_high;
1259 break;
1260 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
1261 lowp = &vmx->nested.msrs.exit_ctls_low;
1262 highp = &vmx->nested.msrs.exit_ctls_high;
1263 break;
1264 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
1265 lowp = &vmx->nested.msrs.entry_ctls_low;
1266 highp = &vmx->nested.msrs.entry_ctls_high;
1267 break;
1268 case MSR_IA32_VMX_PROCBASED_CTLS2:
1269 lowp = &vmx->nested.msrs.secondary_ctls_low;
1270 highp = &vmx->nested.msrs.secondary_ctls_high;
1271 break;
1272 default:
1273 BUG();
1274 }
1275
1276 supported = vmx_control_msr(*lowp, *highp);
1277
1278 /* Check must-be-1 bits are still 1. */
1279 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
1280 return -EINVAL;
1281
1282 /* Check must-be-0 bits are still 0. */
1283 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
1284 return -EINVAL;
1285
1286 *lowp = data;
1287 *highp = data >> 32;
1288 return 0;
1289}
1290
1291static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
1292{
1293 const u64 feature_and_reserved_bits =
1294 /* feature */
1295 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
1296 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
1297 /* reserved */
1298 GENMASK_ULL(13, 9) | BIT_ULL(31);
1299 u64 vmx_misc;
1300
1301 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
1302 vmx->nested.msrs.misc_high);
1303
1304 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
1305 return -EINVAL;
1306
1307 if ((vmx->nested.msrs.pinbased_ctls_high &
1308 PIN_BASED_VMX_PREEMPTION_TIMER) &&
1309 vmx_misc_preemption_timer_rate(data) !=
1310 vmx_misc_preemption_timer_rate(vmx_misc))
1311 return -EINVAL;
1312
1313 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
1314 return -EINVAL;
1315
1316 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
1317 return -EINVAL;
1318
1319 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
1320 return -EINVAL;
1321
1322 vmx->nested.msrs.misc_low = data;
1323 vmx->nested.msrs.misc_high = data >> 32;
1324
Sean Christopherson55d23752018-12-03 13:53:18 -08001325 return 0;
1326}
1327
1328static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
1329{
1330 u64 vmx_ept_vpid_cap;
1331
1332 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
1333 vmx->nested.msrs.vpid_caps);
1334
1335 /* Every bit is either reserved or a feature bit. */
1336 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
1337 return -EINVAL;
1338
1339 vmx->nested.msrs.ept_caps = data;
1340 vmx->nested.msrs.vpid_caps = data >> 32;
1341 return 0;
1342}
1343
1344static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
1345{
1346 u64 *msr;
1347
1348 switch (msr_index) {
1349 case MSR_IA32_VMX_CR0_FIXED0:
1350 msr = &vmx->nested.msrs.cr0_fixed0;
1351 break;
1352 case MSR_IA32_VMX_CR4_FIXED0:
1353 msr = &vmx->nested.msrs.cr4_fixed0;
1354 break;
1355 default:
1356 BUG();
1357 }
1358
1359 /*
1360 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
1361 * must be 1 in the restored value.
1362 */
1363 if (!is_bitwise_subset(data, *msr, -1ULL))
1364 return -EINVAL;
1365
1366 *msr = data;
1367 return 0;
1368}
1369
1370/*
1371 * Called when userspace is restoring VMX MSRs.
1372 *
1373 * Returns 0 on success, non-0 otherwise.
1374 */
1375int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1376{
1377 struct vcpu_vmx *vmx = to_vmx(vcpu);
1378
1379 /*
1380 * Don't allow changes to the VMX capability MSRs while the vCPU
1381 * is in VMX operation.
1382 */
1383 if (vmx->nested.vmxon)
1384 return -EBUSY;
1385
1386 switch (msr_index) {
1387 case MSR_IA32_VMX_BASIC:
1388 return vmx_restore_vmx_basic(vmx, data);
1389 case MSR_IA32_VMX_PINBASED_CTLS:
1390 case MSR_IA32_VMX_PROCBASED_CTLS:
1391 case MSR_IA32_VMX_EXIT_CTLS:
1392 case MSR_IA32_VMX_ENTRY_CTLS:
1393 /*
1394 * The "non-true" VMX capability MSRs are generated from the
1395 * "true" MSRs, so we do not support restoring them directly.
1396 *
1397 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
1398 * should restore the "true" MSRs with the must-be-1 bits
1399 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
1400 * DEFAULT SETTINGS".
1401 */
1402 return -EINVAL;
1403 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
1404 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
1405 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
1406 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
1407 case MSR_IA32_VMX_PROCBASED_CTLS2:
1408 return vmx_restore_control_msr(vmx, msr_index, data);
1409 case MSR_IA32_VMX_MISC:
1410 return vmx_restore_vmx_misc(vmx, data);
1411 case MSR_IA32_VMX_CR0_FIXED0:
1412 case MSR_IA32_VMX_CR4_FIXED0:
1413 return vmx_restore_fixed0_msr(vmx, msr_index, data);
1414 case MSR_IA32_VMX_CR0_FIXED1:
1415 case MSR_IA32_VMX_CR4_FIXED1:
1416 /*
1417 * These MSRs are generated based on the vCPU's CPUID, so we
1418 * do not support restoring them directly.
1419 */
1420 return -EINVAL;
1421 case MSR_IA32_VMX_EPT_VPID_CAP:
1422 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
1423 case MSR_IA32_VMX_VMCS_ENUM:
1424 vmx->nested.msrs.vmcs_enum = data;
1425 return 0;
Paolo Bonzinie8a70bd2019-07-02 14:40:40 +02001426 case MSR_IA32_VMX_VMFUNC:
1427 if (data & ~vmx->nested.msrs.vmfunc_controls)
1428 return -EINVAL;
1429 vmx->nested.msrs.vmfunc_controls = data;
1430 return 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08001431 default:
1432 /*
1433 * The rest of the VMX capability MSRs do not support restore.
1434 */
1435 return -EINVAL;
1436 }
1437}
1438
1439/* Returns 0 on success, non-0 otherwise. */
1440int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
1441{
1442 switch (msr_index) {
1443 case MSR_IA32_VMX_BASIC:
1444 *pdata = msrs->basic;
1445 break;
1446 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
1447 case MSR_IA32_VMX_PINBASED_CTLS:
1448 *pdata = vmx_control_msr(
1449 msrs->pinbased_ctls_low,
1450 msrs->pinbased_ctls_high);
1451 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
1452 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
1453 break;
1454 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
1455 case MSR_IA32_VMX_PROCBASED_CTLS:
1456 *pdata = vmx_control_msr(
1457 msrs->procbased_ctls_low,
1458 msrs->procbased_ctls_high);
1459 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
1460 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
1461 break;
1462 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
1463 case MSR_IA32_VMX_EXIT_CTLS:
1464 *pdata = vmx_control_msr(
1465 msrs->exit_ctls_low,
1466 msrs->exit_ctls_high);
1467 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
1468 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
1469 break;
1470 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
1471 case MSR_IA32_VMX_ENTRY_CTLS:
1472 *pdata = vmx_control_msr(
1473 msrs->entry_ctls_low,
1474 msrs->entry_ctls_high);
1475 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
1476 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
1477 break;
1478 case MSR_IA32_VMX_MISC:
1479 *pdata = vmx_control_msr(
1480 msrs->misc_low,
1481 msrs->misc_high);
1482 break;
1483 case MSR_IA32_VMX_CR0_FIXED0:
1484 *pdata = msrs->cr0_fixed0;
1485 break;
1486 case MSR_IA32_VMX_CR0_FIXED1:
1487 *pdata = msrs->cr0_fixed1;
1488 break;
1489 case MSR_IA32_VMX_CR4_FIXED0:
1490 *pdata = msrs->cr4_fixed0;
1491 break;
1492 case MSR_IA32_VMX_CR4_FIXED1:
1493 *pdata = msrs->cr4_fixed1;
1494 break;
1495 case MSR_IA32_VMX_VMCS_ENUM:
1496 *pdata = msrs->vmcs_enum;
1497 break;
1498 case MSR_IA32_VMX_PROCBASED_CTLS2:
1499 *pdata = vmx_control_msr(
1500 msrs->secondary_ctls_low,
1501 msrs->secondary_ctls_high);
1502 break;
1503 case MSR_IA32_VMX_EPT_VPID_CAP:
1504 *pdata = msrs->ept_caps |
1505 ((u64)msrs->vpid_caps << 32);
1506 break;
1507 case MSR_IA32_VMX_VMFUNC:
1508 *pdata = msrs->vmfunc_controls;
1509 break;
1510 default:
1511 return 1;
1512 }
1513
1514 return 0;
1515}
1516
1517/*
Sean Christophersonfadcead2019-05-07 08:36:23 -07001518 * Copy the writable VMCS shadow fields back to the VMCS12, in case they have
1519 * been modified by the L1 guest. Note, "writable" in this context means
1520 * "writable by the guest", i.e. tagged SHADOW_FIELD_RW; the set of
1521 * fields tagged SHADOW_FIELD_RO may or may not align with the "read-only"
1522 * VM-exit information fields (which are actually writable if the vCPU is
1523 * configured to support "VMWRITE to any supported field in the VMCS").
Sean Christopherson55d23752018-12-03 13:53:18 -08001524 */
1525static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
1526{
Sean Christopherson55d23752018-12-03 13:53:18 -08001527 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Sean Christophersonfadcead2019-05-07 08:36:23 -07001528 struct vmcs12 *vmcs12 = get_vmcs12(&vmx->vcpu);
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07001529 struct shadow_vmcs_field field;
1530 unsigned long val;
Sean Christophersonfadcead2019-05-07 08:36:23 -07001531 int i;
Sean Christopherson55d23752018-12-03 13:53:18 -08001532
Paolo Bonzini88dddc12019-07-19 18:41:10 +02001533 if (WARN_ON(!shadow_vmcs))
1534 return;
1535
Sean Christopherson55d23752018-12-03 13:53:18 -08001536 preempt_disable();
1537
1538 vmcs_load(shadow_vmcs);
1539
Sean Christophersonfadcead2019-05-07 08:36:23 -07001540 for (i = 0; i < max_shadow_read_write_fields; i++) {
1541 field = shadow_read_write_fields[i];
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07001542 val = __vmcs_readl(field.encoding);
1543 vmcs12_write_any(vmcs12, field.encoding, field.offset, val);
Sean Christopherson55d23752018-12-03 13:53:18 -08001544 }
1545
1546 vmcs_clear(shadow_vmcs);
1547 vmcs_load(vmx->loaded_vmcs->vmcs);
1548
1549 preempt_enable();
1550}
1551
1552static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
1553{
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07001554 const struct shadow_vmcs_field *fields[] = {
Sean Christopherson55d23752018-12-03 13:53:18 -08001555 shadow_read_write_fields,
1556 shadow_read_only_fields
1557 };
1558 const int max_fields[] = {
1559 max_shadow_read_write_fields,
1560 max_shadow_read_only_fields
1561 };
Sean Christopherson55d23752018-12-03 13:53:18 -08001562 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07001563 struct vmcs12 *vmcs12 = get_vmcs12(&vmx->vcpu);
1564 struct shadow_vmcs_field field;
1565 unsigned long val;
1566 int i, q;
Sean Christopherson55d23752018-12-03 13:53:18 -08001567
Paolo Bonzini88dddc12019-07-19 18:41:10 +02001568 if (WARN_ON(!shadow_vmcs))
1569 return;
1570
Sean Christopherson55d23752018-12-03 13:53:18 -08001571 vmcs_load(shadow_vmcs);
1572
1573 for (q = 0; q < ARRAY_SIZE(fields); q++) {
1574 for (i = 0; i < max_fields[q]; i++) {
1575 field = fields[q][i];
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07001576 val = vmcs12_read_any(vmcs12, field.encoding,
1577 field.offset);
1578 __vmcs_writel(field.encoding, val);
Sean Christopherson55d23752018-12-03 13:53:18 -08001579 }
1580 }
1581
1582 vmcs_clear(shadow_vmcs);
1583 vmcs_load(vmx->loaded_vmcs->vmcs);
1584}
1585
1586static int copy_enlightened_to_vmcs12(struct vcpu_vmx *vmx)
1587{
1588 struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12;
1589 struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
1590
1591 /* HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE */
1592 vmcs12->tpr_threshold = evmcs->tpr_threshold;
1593 vmcs12->guest_rip = evmcs->guest_rip;
1594
1595 if (unlikely(!(evmcs->hv_clean_fields &
1596 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC))) {
1597 vmcs12->guest_rsp = evmcs->guest_rsp;
1598 vmcs12->guest_rflags = evmcs->guest_rflags;
1599 vmcs12->guest_interruptibility_info =
1600 evmcs->guest_interruptibility_info;
1601 }
1602
1603 if (unlikely(!(evmcs->hv_clean_fields &
1604 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC))) {
1605 vmcs12->cpu_based_vm_exec_control =
1606 evmcs->cpu_based_vm_exec_control;
1607 }
1608
1609 if (unlikely(!(evmcs->hv_clean_fields &
Vitaly Kuznetsovf9bc5222019-06-13 13:35:02 +02001610 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EXCPN))) {
Sean Christopherson55d23752018-12-03 13:53:18 -08001611 vmcs12->exception_bitmap = evmcs->exception_bitmap;
1612 }
1613
1614 if (unlikely(!(evmcs->hv_clean_fields &
1615 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_ENTRY))) {
1616 vmcs12->vm_entry_controls = evmcs->vm_entry_controls;
1617 }
1618
1619 if (unlikely(!(evmcs->hv_clean_fields &
1620 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT))) {
1621 vmcs12->vm_entry_intr_info_field =
1622 evmcs->vm_entry_intr_info_field;
1623 vmcs12->vm_entry_exception_error_code =
1624 evmcs->vm_entry_exception_error_code;
1625 vmcs12->vm_entry_instruction_len =
1626 evmcs->vm_entry_instruction_len;
1627 }
1628
1629 if (unlikely(!(evmcs->hv_clean_fields &
1630 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1))) {
1631 vmcs12->host_ia32_pat = evmcs->host_ia32_pat;
1632 vmcs12->host_ia32_efer = evmcs->host_ia32_efer;
1633 vmcs12->host_cr0 = evmcs->host_cr0;
1634 vmcs12->host_cr3 = evmcs->host_cr3;
1635 vmcs12->host_cr4 = evmcs->host_cr4;
1636 vmcs12->host_ia32_sysenter_esp = evmcs->host_ia32_sysenter_esp;
1637 vmcs12->host_ia32_sysenter_eip = evmcs->host_ia32_sysenter_eip;
1638 vmcs12->host_rip = evmcs->host_rip;
1639 vmcs12->host_ia32_sysenter_cs = evmcs->host_ia32_sysenter_cs;
1640 vmcs12->host_es_selector = evmcs->host_es_selector;
1641 vmcs12->host_cs_selector = evmcs->host_cs_selector;
1642 vmcs12->host_ss_selector = evmcs->host_ss_selector;
1643 vmcs12->host_ds_selector = evmcs->host_ds_selector;
1644 vmcs12->host_fs_selector = evmcs->host_fs_selector;
1645 vmcs12->host_gs_selector = evmcs->host_gs_selector;
1646 vmcs12->host_tr_selector = evmcs->host_tr_selector;
1647 }
1648
1649 if (unlikely(!(evmcs->hv_clean_fields &
Vitaly Kuznetsovf9bc5222019-06-13 13:35:02 +02001650 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1))) {
Sean Christopherson55d23752018-12-03 13:53:18 -08001651 vmcs12->pin_based_vm_exec_control =
1652 evmcs->pin_based_vm_exec_control;
1653 vmcs12->vm_exit_controls = evmcs->vm_exit_controls;
1654 vmcs12->secondary_vm_exec_control =
1655 evmcs->secondary_vm_exec_control;
1656 }
1657
1658 if (unlikely(!(evmcs->hv_clean_fields &
1659 HV_VMX_ENLIGHTENED_CLEAN_FIELD_IO_BITMAP))) {
1660 vmcs12->io_bitmap_a = evmcs->io_bitmap_a;
1661 vmcs12->io_bitmap_b = evmcs->io_bitmap_b;
1662 }
1663
1664 if (unlikely(!(evmcs->hv_clean_fields &
1665 HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP))) {
1666 vmcs12->msr_bitmap = evmcs->msr_bitmap;
1667 }
1668
1669 if (unlikely(!(evmcs->hv_clean_fields &
1670 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2))) {
1671 vmcs12->guest_es_base = evmcs->guest_es_base;
1672 vmcs12->guest_cs_base = evmcs->guest_cs_base;
1673 vmcs12->guest_ss_base = evmcs->guest_ss_base;
1674 vmcs12->guest_ds_base = evmcs->guest_ds_base;
1675 vmcs12->guest_fs_base = evmcs->guest_fs_base;
1676 vmcs12->guest_gs_base = evmcs->guest_gs_base;
1677 vmcs12->guest_ldtr_base = evmcs->guest_ldtr_base;
1678 vmcs12->guest_tr_base = evmcs->guest_tr_base;
1679 vmcs12->guest_gdtr_base = evmcs->guest_gdtr_base;
1680 vmcs12->guest_idtr_base = evmcs->guest_idtr_base;
1681 vmcs12->guest_es_limit = evmcs->guest_es_limit;
1682 vmcs12->guest_cs_limit = evmcs->guest_cs_limit;
1683 vmcs12->guest_ss_limit = evmcs->guest_ss_limit;
1684 vmcs12->guest_ds_limit = evmcs->guest_ds_limit;
1685 vmcs12->guest_fs_limit = evmcs->guest_fs_limit;
1686 vmcs12->guest_gs_limit = evmcs->guest_gs_limit;
1687 vmcs12->guest_ldtr_limit = evmcs->guest_ldtr_limit;
1688 vmcs12->guest_tr_limit = evmcs->guest_tr_limit;
1689 vmcs12->guest_gdtr_limit = evmcs->guest_gdtr_limit;
1690 vmcs12->guest_idtr_limit = evmcs->guest_idtr_limit;
1691 vmcs12->guest_es_ar_bytes = evmcs->guest_es_ar_bytes;
1692 vmcs12->guest_cs_ar_bytes = evmcs->guest_cs_ar_bytes;
1693 vmcs12->guest_ss_ar_bytes = evmcs->guest_ss_ar_bytes;
1694 vmcs12->guest_ds_ar_bytes = evmcs->guest_ds_ar_bytes;
1695 vmcs12->guest_fs_ar_bytes = evmcs->guest_fs_ar_bytes;
1696 vmcs12->guest_gs_ar_bytes = evmcs->guest_gs_ar_bytes;
1697 vmcs12->guest_ldtr_ar_bytes = evmcs->guest_ldtr_ar_bytes;
1698 vmcs12->guest_tr_ar_bytes = evmcs->guest_tr_ar_bytes;
1699 vmcs12->guest_es_selector = evmcs->guest_es_selector;
1700 vmcs12->guest_cs_selector = evmcs->guest_cs_selector;
1701 vmcs12->guest_ss_selector = evmcs->guest_ss_selector;
1702 vmcs12->guest_ds_selector = evmcs->guest_ds_selector;
1703 vmcs12->guest_fs_selector = evmcs->guest_fs_selector;
1704 vmcs12->guest_gs_selector = evmcs->guest_gs_selector;
1705 vmcs12->guest_ldtr_selector = evmcs->guest_ldtr_selector;
1706 vmcs12->guest_tr_selector = evmcs->guest_tr_selector;
1707 }
1708
1709 if (unlikely(!(evmcs->hv_clean_fields &
1710 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2))) {
1711 vmcs12->tsc_offset = evmcs->tsc_offset;
1712 vmcs12->virtual_apic_page_addr = evmcs->virtual_apic_page_addr;
1713 vmcs12->xss_exit_bitmap = evmcs->xss_exit_bitmap;
1714 }
1715
1716 if (unlikely(!(evmcs->hv_clean_fields &
1717 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR))) {
1718 vmcs12->cr0_guest_host_mask = evmcs->cr0_guest_host_mask;
1719 vmcs12->cr4_guest_host_mask = evmcs->cr4_guest_host_mask;
1720 vmcs12->cr0_read_shadow = evmcs->cr0_read_shadow;
1721 vmcs12->cr4_read_shadow = evmcs->cr4_read_shadow;
1722 vmcs12->guest_cr0 = evmcs->guest_cr0;
1723 vmcs12->guest_cr3 = evmcs->guest_cr3;
1724 vmcs12->guest_cr4 = evmcs->guest_cr4;
1725 vmcs12->guest_dr7 = evmcs->guest_dr7;
1726 }
1727
1728 if (unlikely(!(evmcs->hv_clean_fields &
1729 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER))) {
1730 vmcs12->host_fs_base = evmcs->host_fs_base;
1731 vmcs12->host_gs_base = evmcs->host_gs_base;
1732 vmcs12->host_tr_base = evmcs->host_tr_base;
1733 vmcs12->host_gdtr_base = evmcs->host_gdtr_base;
1734 vmcs12->host_idtr_base = evmcs->host_idtr_base;
1735 vmcs12->host_rsp = evmcs->host_rsp;
1736 }
1737
1738 if (unlikely(!(evmcs->hv_clean_fields &
1739 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_XLAT))) {
1740 vmcs12->ept_pointer = evmcs->ept_pointer;
1741 vmcs12->virtual_processor_id = evmcs->virtual_processor_id;
1742 }
1743
1744 if (unlikely(!(evmcs->hv_clean_fields &
1745 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1))) {
1746 vmcs12->vmcs_link_pointer = evmcs->vmcs_link_pointer;
1747 vmcs12->guest_ia32_debugctl = evmcs->guest_ia32_debugctl;
1748 vmcs12->guest_ia32_pat = evmcs->guest_ia32_pat;
1749 vmcs12->guest_ia32_efer = evmcs->guest_ia32_efer;
1750 vmcs12->guest_pdptr0 = evmcs->guest_pdptr0;
1751 vmcs12->guest_pdptr1 = evmcs->guest_pdptr1;
1752 vmcs12->guest_pdptr2 = evmcs->guest_pdptr2;
1753 vmcs12->guest_pdptr3 = evmcs->guest_pdptr3;
1754 vmcs12->guest_pending_dbg_exceptions =
1755 evmcs->guest_pending_dbg_exceptions;
1756 vmcs12->guest_sysenter_esp = evmcs->guest_sysenter_esp;
1757 vmcs12->guest_sysenter_eip = evmcs->guest_sysenter_eip;
1758 vmcs12->guest_bndcfgs = evmcs->guest_bndcfgs;
1759 vmcs12->guest_activity_state = evmcs->guest_activity_state;
1760 vmcs12->guest_sysenter_cs = evmcs->guest_sysenter_cs;
1761 }
1762
1763 /*
1764 * Not used?
1765 * vmcs12->vm_exit_msr_store_addr = evmcs->vm_exit_msr_store_addr;
1766 * vmcs12->vm_exit_msr_load_addr = evmcs->vm_exit_msr_load_addr;
1767 * vmcs12->vm_entry_msr_load_addr = evmcs->vm_entry_msr_load_addr;
Sean Christopherson55d23752018-12-03 13:53:18 -08001768 * vmcs12->page_fault_error_code_mask =
1769 * evmcs->page_fault_error_code_mask;
1770 * vmcs12->page_fault_error_code_match =
1771 * evmcs->page_fault_error_code_match;
1772 * vmcs12->cr3_target_count = evmcs->cr3_target_count;
1773 * vmcs12->vm_exit_msr_store_count = evmcs->vm_exit_msr_store_count;
1774 * vmcs12->vm_exit_msr_load_count = evmcs->vm_exit_msr_load_count;
1775 * vmcs12->vm_entry_msr_load_count = evmcs->vm_entry_msr_load_count;
1776 */
1777
1778 /*
1779 * Read only fields:
1780 * vmcs12->guest_physical_address = evmcs->guest_physical_address;
1781 * vmcs12->vm_instruction_error = evmcs->vm_instruction_error;
1782 * vmcs12->vm_exit_reason = evmcs->vm_exit_reason;
1783 * vmcs12->vm_exit_intr_info = evmcs->vm_exit_intr_info;
1784 * vmcs12->vm_exit_intr_error_code = evmcs->vm_exit_intr_error_code;
1785 * vmcs12->idt_vectoring_info_field = evmcs->idt_vectoring_info_field;
1786 * vmcs12->idt_vectoring_error_code = evmcs->idt_vectoring_error_code;
1787 * vmcs12->vm_exit_instruction_len = evmcs->vm_exit_instruction_len;
1788 * vmcs12->vmx_instruction_info = evmcs->vmx_instruction_info;
1789 * vmcs12->exit_qualification = evmcs->exit_qualification;
1790 * vmcs12->guest_linear_address = evmcs->guest_linear_address;
1791 *
1792 * Not present in struct vmcs12:
1793 * vmcs12->exit_io_instruction_ecx = evmcs->exit_io_instruction_ecx;
1794 * vmcs12->exit_io_instruction_esi = evmcs->exit_io_instruction_esi;
1795 * vmcs12->exit_io_instruction_edi = evmcs->exit_io_instruction_edi;
1796 * vmcs12->exit_io_instruction_eip = evmcs->exit_io_instruction_eip;
1797 */
1798
1799 return 0;
1800}
1801
1802static int copy_vmcs12_to_enlightened(struct vcpu_vmx *vmx)
1803{
1804 struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12;
1805 struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
1806
1807 /*
1808 * Should not be changed by KVM:
1809 *
1810 * evmcs->host_es_selector = vmcs12->host_es_selector;
1811 * evmcs->host_cs_selector = vmcs12->host_cs_selector;
1812 * evmcs->host_ss_selector = vmcs12->host_ss_selector;
1813 * evmcs->host_ds_selector = vmcs12->host_ds_selector;
1814 * evmcs->host_fs_selector = vmcs12->host_fs_selector;
1815 * evmcs->host_gs_selector = vmcs12->host_gs_selector;
1816 * evmcs->host_tr_selector = vmcs12->host_tr_selector;
1817 * evmcs->host_ia32_pat = vmcs12->host_ia32_pat;
1818 * evmcs->host_ia32_efer = vmcs12->host_ia32_efer;
1819 * evmcs->host_cr0 = vmcs12->host_cr0;
1820 * evmcs->host_cr3 = vmcs12->host_cr3;
1821 * evmcs->host_cr4 = vmcs12->host_cr4;
1822 * evmcs->host_ia32_sysenter_esp = vmcs12->host_ia32_sysenter_esp;
1823 * evmcs->host_ia32_sysenter_eip = vmcs12->host_ia32_sysenter_eip;
1824 * evmcs->host_rip = vmcs12->host_rip;
1825 * evmcs->host_ia32_sysenter_cs = vmcs12->host_ia32_sysenter_cs;
1826 * evmcs->host_fs_base = vmcs12->host_fs_base;
1827 * evmcs->host_gs_base = vmcs12->host_gs_base;
1828 * evmcs->host_tr_base = vmcs12->host_tr_base;
1829 * evmcs->host_gdtr_base = vmcs12->host_gdtr_base;
1830 * evmcs->host_idtr_base = vmcs12->host_idtr_base;
1831 * evmcs->host_rsp = vmcs12->host_rsp;
Sean Christopherson3731905ef2019-05-07 08:36:27 -07001832 * sync_vmcs02_to_vmcs12() doesn't read these:
Sean Christopherson55d23752018-12-03 13:53:18 -08001833 * evmcs->io_bitmap_a = vmcs12->io_bitmap_a;
1834 * evmcs->io_bitmap_b = vmcs12->io_bitmap_b;
1835 * evmcs->msr_bitmap = vmcs12->msr_bitmap;
1836 * evmcs->ept_pointer = vmcs12->ept_pointer;
1837 * evmcs->xss_exit_bitmap = vmcs12->xss_exit_bitmap;
1838 * evmcs->vm_exit_msr_store_addr = vmcs12->vm_exit_msr_store_addr;
1839 * evmcs->vm_exit_msr_load_addr = vmcs12->vm_exit_msr_load_addr;
1840 * evmcs->vm_entry_msr_load_addr = vmcs12->vm_entry_msr_load_addr;
Sean Christopherson55d23752018-12-03 13:53:18 -08001841 * evmcs->tpr_threshold = vmcs12->tpr_threshold;
1842 * evmcs->virtual_processor_id = vmcs12->virtual_processor_id;
1843 * evmcs->exception_bitmap = vmcs12->exception_bitmap;
1844 * evmcs->vmcs_link_pointer = vmcs12->vmcs_link_pointer;
1845 * evmcs->pin_based_vm_exec_control = vmcs12->pin_based_vm_exec_control;
1846 * evmcs->vm_exit_controls = vmcs12->vm_exit_controls;
1847 * evmcs->secondary_vm_exec_control = vmcs12->secondary_vm_exec_control;
1848 * evmcs->page_fault_error_code_mask =
1849 * vmcs12->page_fault_error_code_mask;
1850 * evmcs->page_fault_error_code_match =
1851 * vmcs12->page_fault_error_code_match;
1852 * evmcs->cr3_target_count = vmcs12->cr3_target_count;
1853 * evmcs->virtual_apic_page_addr = vmcs12->virtual_apic_page_addr;
1854 * evmcs->tsc_offset = vmcs12->tsc_offset;
1855 * evmcs->guest_ia32_debugctl = vmcs12->guest_ia32_debugctl;
1856 * evmcs->cr0_guest_host_mask = vmcs12->cr0_guest_host_mask;
1857 * evmcs->cr4_guest_host_mask = vmcs12->cr4_guest_host_mask;
1858 * evmcs->cr0_read_shadow = vmcs12->cr0_read_shadow;
1859 * evmcs->cr4_read_shadow = vmcs12->cr4_read_shadow;
1860 * evmcs->vm_exit_msr_store_count = vmcs12->vm_exit_msr_store_count;
1861 * evmcs->vm_exit_msr_load_count = vmcs12->vm_exit_msr_load_count;
1862 * evmcs->vm_entry_msr_load_count = vmcs12->vm_entry_msr_load_count;
1863 *
1864 * Not present in struct vmcs12:
1865 * evmcs->exit_io_instruction_ecx = vmcs12->exit_io_instruction_ecx;
1866 * evmcs->exit_io_instruction_esi = vmcs12->exit_io_instruction_esi;
1867 * evmcs->exit_io_instruction_edi = vmcs12->exit_io_instruction_edi;
1868 * evmcs->exit_io_instruction_eip = vmcs12->exit_io_instruction_eip;
1869 */
1870
1871 evmcs->guest_es_selector = vmcs12->guest_es_selector;
1872 evmcs->guest_cs_selector = vmcs12->guest_cs_selector;
1873 evmcs->guest_ss_selector = vmcs12->guest_ss_selector;
1874 evmcs->guest_ds_selector = vmcs12->guest_ds_selector;
1875 evmcs->guest_fs_selector = vmcs12->guest_fs_selector;
1876 evmcs->guest_gs_selector = vmcs12->guest_gs_selector;
1877 evmcs->guest_ldtr_selector = vmcs12->guest_ldtr_selector;
1878 evmcs->guest_tr_selector = vmcs12->guest_tr_selector;
1879
1880 evmcs->guest_es_limit = vmcs12->guest_es_limit;
1881 evmcs->guest_cs_limit = vmcs12->guest_cs_limit;
1882 evmcs->guest_ss_limit = vmcs12->guest_ss_limit;
1883 evmcs->guest_ds_limit = vmcs12->guest_ds_limit;
1884 evmcs->guest_fs_limit = vmcs12->guest_fs_limit;
1885 evmcs->guest_gs_limit = vmcs12->guest_gs_limit;
1886 evmcs->guest_ldtr_limit = vmcs12->guest_ldtr_limit;
1887 evmcs->guest_tr_limit = vmcs12->guest_tr_limit;
1888 evmcs->guest_gdtr_limit = vmcs12->guest_gdtr_limit;
1889 evmcs->guest_idtr_limit = vmcs12->guest_idtr_limit;
1890
1891 evmcs->guest_es_ar_bytes = vmcs12->guest_es_ar_bytes;
1892 evmcs->guest_cs_ar_bytes = vmcs12->guest_cs_ar_bytes;
1893 evmcs->guest_ss_ar_bytes = vmcs12->guest_ss_ar_bytes;
1894 evmcs->guest_ds_ar_bytes = vmcs12->guest_ds_ar_bytes;
1895 evmcs->guest_fs_ar_bytes = vmcs12->guest_fs_ar_bytes;
1896 evmcs->guest_gs_ar_bytes = vmcs12->guest_gs_ar_bytes;
1897 evmcs->guest_ldtr_ar_bytes = vmcs12->guest_ldtr_ar_bytes;
1898 evmcs->guest_tr_ar_bytes = vmcs12->guest_tr_ar_bytes;
1899
1900 evmcs->guest_es_base = vmcs12->guest_es_base;
1901 evmcs->guest_cs_base = vmcs12->guest_cs_base;
1902 evmcs->guest_ss_base = vmcs12->guest_ss_base;
1903 evmcs->guest_ds_base = vmcs12->guest_ds_base;
1904 evmcs->guest_fs_base = vmcs12->guest_fs_base;
1905 evmcs->guest_gs_base = vmcs12->guest_gs_base;
1906 evmcs->guest_ldtr_base = vmcs12->guest_ldtr_base;
1907 evmcs->guest_tr_base = vmcs12->guest_tr_base;
1908 evmcs->guest_gdtr_base = vmcs12->guest_gdtr_base;
1909 evmcs->guest_idtr_base = vmcs12->guest_idtr_base;
1910
1911 evmcs->guest_ia32_pat = vmcs12->guest_ia32_pat;
1912 evmcs->guest_ia32_efer = vmcs12->guest_ia32_efer;
1913
1914 evmcs->guest_pdptr0 = vmcs12->guest_pdptr0;
1915 evmcs->guest_pdptr1 = vmcs12->guest_pdptr1;
1916 evmcs->guest_pdptr2 = vmcs12->guest_pdptr2;
1917 evmcs->guest_pdptr3 = vmcs12->guest_pdptr3;
1918
1919 evmcs->guest_pending_dbg_exceptions =
1920 vmcs12->guest_pending_dbg_exceptions;
1921 evmcs->guest_sysenter_esp = vmcs12->guest_sysenter_esp;
1922 evmcs->guest_sysenter_eip = vmcs12->guest_sysenter_eip;
1923
1924 evmcs->guest_activity_state = vmcs12->guest_activity_state;
1925 evmcs->guest_sysenter_cs = vmcs12->guest_sysenter_cs;
1926
1927 evmcs->guest_cr0 = vmcs12->guest_cr0;
1928 evmcs->guest_cr3 = vmcs12->guest_cr3;
1929 evmcs->guest_cr4 = vmcs12->guest_cr4;
1930 evmcs->guest_dr7 = vmcs12->guest_dr7;
1931
1932 evmcs->guest_physical_address = vmcs12->guest_physical_address;
1933
1934 evmcs->vm_instruction_error = vmcs12->vm_instruction_error;
1935 evmcs->vm_exit_reason = vmcs12->vm_exit_reason;
1936 evmcs->vm_exit_intr_info = vmcs12->vm_exit_intr_info;
1937 evmcs->vm_exit_intr_error_code = vmcs12->vm_exit_intr_error_code;
1938 evmcs->idt_vectoring_info_field = vmcs12->idt_vectoring_info_field;
1939 evmcs->idt_vectoring_error_code = vmcs12->idt_vectoring_error_code;
1940 evmcs->vm_exit_instruction_len = vmcs12->vm_exit_instruction_len;
1941 evmcs->vmx_instruction_info = vmcs12->vmx_instruction_info;
1942
1943 evmcs->exit_qualification = vmcs12->exit_qualification;
1944
1945 evmcs->guest_linear_address = vmcs12->guest_linear_address;
1946 evmcs->guest_rsp = vmcs12->guest_rsp;
1947 evmcs->guest_rflags = vmcs12->guest_rflags;
1948
1949 evmcs->guest_interruptibility_info =
1950 vmcs12->guest_interruptibility_info;
1951 evmcs->cpu_based_vm_exec_control = vmcs12->cpu_based_vm_exec_control;
1952 evmcs->vm_entry_controls = vmcs12->vm_entry_controls;
1953 evmcs->vm_entry_intr_info_field = vmcs12->vm_entry_intr_info_field;
1954 evmcs->vm_entry_exception_error_code =
1955 vmcs12->vm_entry_exception_error_code;
1956 evmcs->vm_entry_instruction_len = vmcs12->vm_entry_instruction_len;
1957
1958 evmcs->guest_rip = vmcs12->guest_rip;
1959
1960 evmcs->guest_bndcfgs = vmcs12->guest_bndcfgs;
1961
1962 return 0;
1963}
1964
1965/*
1966 * This is an equivalent of the nested hypervisor executing the vmptrld
1967 * instruction.
1968 */
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01001969static enum nested_evmptrld_status nested_vmx_handle_enlightened_vmptrld(
1970 struct kvm_vcpu *vcpu, bool from_launch)
Sean Christopherson55d23752018-12-03 13:53:18 -08001971{
1972 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsova21a39c2019-06-28 13:23:32 +02001973 bool evmcs_gpa_changed = false;
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02001974 u64 evmcs_gpa;
Sean Christopherson55d23752018-12-03 13:53:18 -08001975
1976 if (likely(!vmx->nested.enlightened_vmcs_enabled))
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01001977 return EVMPTRLD_DISABLED;
Sean Christopherson55d23752018-12-03 13:53:18 -08001978
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02001979 if (!nested_enlightened_vmentry(vcpu, &evmcs_gpa))
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01001980 return EVMPTRLD_DISABLED;
Sean Christopherson55d23752018-12-03 13:53:18 -08001981
Vitaly Kuznetsov95fa1012020-03-09 16:52:11 +01001982 if (unlikely(!vmx->nested.hv_evmcs ||
1983 evmcs_gpa != vmx->nested.hv_evmcs_vmptr)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08001984 if (!vmx->nested.hv_evmcs)
1985 vmx->nested.current_vmptr = -1ull;
1986
1987 nested_release_evmcs(vcpu);
1988
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02001989 if (kvm_vcpu_map(vcpu, gpa_to_gfn(evmcs_gpa),
KarimAllah Ahmeddee9c042019-01-31 21:24:42 +01001990 &vmx->nested.hv_evmcs_map))
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01001991 return EVMPTRLD_ERROR;
Sean Christopherson55d23752018-12-03 13:53:18 -08001992
KarimAllah Ahmeddee9c042019-01-31 21:24:42 +01001993 vmx->nested.hv_evmcs = vmx->nested.hv_evmcs_map.hva;
Sean Christopherson55d23752018-12-03 13:53:18 -08001994
1995 /*
1996 * Currently, KVM only supports eVMCS version 1
1997 * (== KVM_EVMCS_VERSION) and thus we expect guest to set this
1998 * value to first u32 field of eVMCS which should specify eVMCS
1999 * VersionNumber.
2000 *
2001 * Guest should be aware of supported eVMCS versions by host by
2002 * examining CPUID.0x4000000A.EAX[0:15]. Host userspace VMM is
2003 * expected to set this CPUID leaf according to the value
2004 * returned in vmcs_version from nested_enable_evmcs().
2005 *
2006 * However, it turns out that Microsoft Hyper-V fails to comply
2007 * to their own invented interface: When Hyper-V use eVMCS, it
2008 * just sets first u32 field of eVMCS to revision_id specified
2009 * in MSR_IA32_VMX_BASIC. Instead of used eVMCS version number
2010 * which is one of the supported versions specified in
2011 * CPUID.0x4000000A.EAX[0:15].
2012 *
2013 * To overcome Hyper-V bug, we accept here either a supported
2014 * eVMCS version or VMCS12 revision_id as valid values for first
2015 * u32 field of eVMCS.
2016 */
2017 if ((vmx->nested.hv_evmcs->revision_id != KVM_EVMCS_VERSION) &&
2018 (vmx->nested.hv_evmcs->revision_id != VMCS12_REVISION)) {
2019 nested_release_evmcs(vcpu);
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01002020 return EVMPTRLD_VMFAIL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002021 }
2022
2023 vmx->nested.dirty_vmcs12 = true;
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02002024 vmx->nested.hv_evmcs_vmptr = evmcs_gpa;
Sean Christopherson55d23752018-12-03 13:53:18 -08002025
Vitaly Kuznetsova21a39c2019-06-28 13:23:32 +02002026 evmcs_gpa_changed = true;
Sean Christopherson55d23752018-12-03 13:53:18 -08002027 /*
2028 * Unlike normal vmcs12, enlightened vmcs12 is not fully
2029 * reloaded from guest's memory (read only fields, fields not
2030 * present in struct hv_enlightened_vmcs, ...). Make sure there
2031 * are no leftovers.
2032 */
2033 if (from_launch) {
2034 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2035 memset(vmcs12, 0, sizeof(*vmcs12));
2036 vmcs12->hdr.revision_id = VMCS12_REVISION;
2037 }
2038
2039 }
Vitaly Kuznetsova21a39c2019-06-28 13:23:32 +02002040
2041 /*
Miaohe Linffdbd502020-02-07 23:22:45 +08002042 * Clean fields data can't be used on VMLAUNCH and when we switch
Vitaly Kuznetsova21a39c2019-06-28 13:23:32 +02002043 * between different L2 guests as KVM keeps a single VMCS12 per L1.
2044 */
2045 if (from_launch || evmcs_gpa_changed)
2046 vmx->nested.hv_evmcs->hv_clean_fields &=
2047 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
2048
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01002049 return EVMPTRLD_SUCCEEDED;
Sean Christopherson55d23752018-12-03 13:53:18 -08002050}
2051
Sean Christopherson3731905ef2019-05-07 08:36:27 -07002052void nested_sync_vmcs12_to_shadow(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08002053{
2054 struct vcpu_vmx *vmx = to_vmx(vcpu);
2055
Sean Christopherson55d23752018-12-03 13:53:18 -08002056 if (vmx->nested.hv_evmcs) {
2057 copy_vmcs12_to_enlightened(vmx);
2058 /* All fields are clean */
2059 vmx->nested.hv_evmcs->hv_clean_fields |=
2060 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
2061 } else {
2062 copy_vmcs12_to_shadow(vmx);
2063 }
2064
Sean Christopherson3731905ef2019-05-07 08:36:27 -07002065 vmx->nested.need_vmcs12_to_shadow_sync = false;
Sean Christopherson55d23752018-12-03 13:53:18 -08002066}
2067
2068static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
2069{
2070 struct vcpu_vmx *vmx =
2071 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
2072
2073 vmx->nested.preemption_timer_expired = true;
2074 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
2075 kvm_vcpu_kick(&vmx->vcpu);
2076
2077 return HRTIMER_NORESTART;
2078}
2079
Peter Shier850448f2020-05-26 14:51:06 -07002080static u64 vmx_calc_preemption_timer_value(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08002081{
Peter Shier850448f2020-05-26 14:51:06 -07002082 struct vcpu_vmx *vmx = to_vmx(vcpu);
2083 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Peter Shier850448f2020-05-26 14:51:06 -07002084
2085 u64 l1_scaled_tsc = kvm_read_l1_tsc(vcpu, rdtsc()) >>
2086 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
2087
2088 if (!vmx->nested.has_preemption_timer_deadline) {
Makarand Sonare8d7fbf02020-05-26 14:51:07 -07002089 vmx->nested.preemption_timer_deadline =
2090 vmcs12->vmx_preemption_timer_value + l1_scaled_tsc;
Peter Shier850448f2020-05-26 14:51:06 -07002091 vmx->nested.has_preemption_timer_deadline = true;
Makarand Sonare8d7fbf02020-05-26 14:51:07 -07002092 }
2093 return vmx->nested.preemption_timer_deadline - l1_scaled_tsc;
Peter Shier850448f2020-05-26 14:51:06 -07002094}
2095
2096static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu,
2097 u64 preemption_timeout)
2098{
Sean Christopherson55d23752018-12-03 13:53:18 -08002099 struct vcpu_vmx *vmx = to_vmx(vcpu);
2100
2101 /*
2102 * A timer value of zero is architecturally guaranteed to cause
2103 * a VMExit prior to executing any instructions in the guest.
2104 */
2105 if (preemption_timeout == 0) {
2106 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
2107 return;
2108 }
2109
2110 if (vcpu->arch.virtual_tsc_khz == 0)
2111 return;
2112
2113 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
2114 preemption_timeout *= 1000000;
2115 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
2116 hrtimer_start(&vmx->nested.preemption_timer,
Jim Mattsonada00982020-05-08 13:36:42 -07002117 ktime_add_ns(ktime_get(), preemption_timeout),
2118 HRTIMER_MODE_ABS_PINNED);
Sean Christopherson55d23752018-12-03 13:53:18 -08002119}
2120
2121static u64 nested_vmx_calc_efer(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
2122{
2123 if (vmx->nested.nested_run_pending &&
2124 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
2125 return vmcs12->guest_ia32_efer;
2126 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
2127 return vmx->vcpu.arch.efer | (EFER_LMA | EFER_LME);
2128 else
2129 return vmx->vcpu.arch.efer & ~(EFER_LMA | EFER_LME);
2130}
2131
2132static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx)
2133{
2134 /*
2135 * If vmcs02 hasn't been initialized, set the constant vmcs02 state
2136 * according to L0's settings (vmcs12 is irrelevant here). Host
2137 * fields that come from L0 and are not constant, e.g. HOST_CR3,
2138 * will be set as needed prior to VMLAUNCH/VMRESUME.
2139 */
2140 if (vmx->nested.vmcs02_initialized)
2141 return;
2142 vmx->nested.vmcs02_initialized = true;
2143
2144 /*
2145 * We don't care what the EPTP value is we just need to guarantee
2146 * it's valid so we don't get a false positive when doing early
2147 * consistency checks.
2148 */
2149 if (enable_ept && nested_early_check)
Sean Christopherson2a40b902020-07-15 20:41:18 -07002150 vmcs_write64(EPT_POINTER,
2151 construct_eptp(&vmx->vcpu, 0, PT64_ROOT_4LEVEL));
Sean Christopherson55d23752018-12-03 13:53:18 -08002152
2153 /* All VMFUNCs are currently emulated through L0 vmexits. */
2154 if (cpu_has_vmx_vmfunc())
2155 vmcs_write64(VM_FUNCTION_CONTROL, 0);
2156
2157 if (cpu_has_vmx_posted_intr())
2158 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
2159
2160 if (cpu_has_vmx_msr_bitmap())
2161 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
2162
Sean Christopherson4d6c9892019-05-07 09:06:30 -07002163 /*
Sean Christophersonc3bb9a22021-02-12 16:50:07 -08002164 * PML is emulated for L2, but never enabled in hardware as the MMU
2165 * handles A/D emulation. Disabling PML for L2 also avoids having to
2166 * deal with filtering out L2 GPAs from the buffer.
Sean Christopherson4d6c9892019-05-07 09:06:30 -07002167 */
2168 if (enable_pml) {
Sean Christophersonc3bb9a22021-02-12 16:50:07 -08002169 vmcs_write64(PML_ADDRESS, 0);
2170 vmcs_write16(GUEST_PML_INDEX, -1);
Sean Christopherson4d6c9892019-05-07 09:06:30 -07002171 }
Sean Christopherson55d23752018-12-03 13:53:18 -08002172
Sean Christophersonc538d572019-05-07 09:06:29 -07002173 if (cpu_has_vmx_encls_vmexit())
2174 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
Sean Christopherson55d23752018-12-03 13:53:18 -08002175
2176 /*
2177 * Set the MSR load/store lists to match L0's settings. Only the
2178 * addresses are constant (for vmcs02), the counts can change based
2179 * on L2's behavior, e.g. switching to/from long mode.
2180 */
Aaron Lewis662f1d12019-11-07 21:14:39 -08002181 vmcs_write64(VM_EXIT_MSR_STORE_ADDR, __pa(vmx->msr_autostore.guest.val));
Sean Christopherson55d23752018-12-03 13:53:18 -08002182 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
2183 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
2184
2185 vmx_set_constant_host_state(vmx);
2186}
2187
Paolo Bonzinib1346ab2019-06-06 17:24:00 +02002188static void prepare_vmcs02_early_rare(struct vcpu_vmx *vmx,
Sean Christopherson55d23752018-12-03 13:53:18 -08002189 struct vmcs12 *vmcs12)
2190{
2191 prepare_vmcs02_constant_state(vmx);
2192
2193 vmcs_write64(VMCS_LINK_POINTER, -1ull);
2194
2195 if (enable_vpid) {
2196 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
2197 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
2198 else
2199 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
2200 }
2201}
2202
2203static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
2204{
Sean Christophersonc3bb9a22021-02-12 16:50:07 -08002205 u32 exec_control;
Sean Christopherson55d23752018-12-03 13:53:18 -08002206 u64 guest_efer = nested_vmx_calc_efer(vmx, vmcs12);
2207
2208 if (vmx->nested.dirty_vmcs12 || vmx->nested.hv_evmcs)
Paolo Bonzinib1346ab2019-06-06 17:24:00 +02002209 prepare_vmcs02_early_rare(vmx, vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08002210
2211 /*
Sean Christopherson55d23752018-12-03 13:53:18 -08002212 * PIN CONTROLS
2213 */
Sean Christophersonc075c3e2019-05-07 12:17:53 -07002214 exec_control = vmx_pin_based_exec_ctrl(vmx);
Sean Christopherson804939e2019-05-07 12:18:05 -07002215 exec_control |= (vmcs12->pin_based_vm_exec_control &
2216 ~PIN_BASED_VMX_PREEMPTION_TIMER);
Sean Christopherson55d23752018-12-03 13:53:18 -08002217
2218 /* Posted interrupts setting is only taken from vmcs12. */
2219 if (nested_cpu_has_posted_intr(vmcs12)) {
2220 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
2221 vmx->nested.pi_pending = false;
2222 } else {
2223 exec_control &= ~PIN_BASED_POSTED_INTR;
2224 }
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002225 pin_controls_set(vmx, exec_control);
Sean Christopherson55d23752018-12-03 13:53:18 -08002226
2227 /*
2228 * EXEC CONTROLS
2229 */
2230 exec_control = vmx_exec_control(vmx); /* L0's desires */
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08002231 exec_control &= ~CPU_BASED_INTR_WINDOW_EXITING;
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08002232 exec_control &= ~CPU_BASED_NMI_WINDOW_EXITING;
Sean Christopherson55d23752018-12-03 13:53:18 -08002233 exec_control &= ~CPU_BASED_TPR_SHADOW;
2234 exec_control |= vmcs12->cpu_based_vm_exec_control;
2235
Liran Alon02d496cf2019-11-11 14:30:55 +02002236 vmx->nested.l1_tpr_threshold = -1;
Sean Christophersonca2f5462019-05-07 09:06:33 -07002237 if (exec_control & CPU_BASED_TPR_SHADOW)
Sean Christopherson55d23752018-12-03 13:53:18 -08002238 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Sean Christopherson55d23752018-12-03 13:53:18 -08002239#ifdef CONFIG_X86_64
Sean Christophersonca2f5462019-05-07 09:06:33 -07002240 else
Sean Christopherson55d23752018-12-03 13:53:18 -08002241 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
2242 CPU_BASED_CR8_STORE_EXITING;
2243#endif
Sean Christopherson55d23752018-12-03 13:53:18 -08002244
2245 /*
2246 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
2247 * for I/O port accesses.
2248 */
Sean Christopherson55d23752018-12-03 13:53:18 -08002249 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
Sean Christophersonde0286b2019-05-07 12:18:01 -07002250 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
2251
2252 /*
2253 * This bit will be computed in nested_get_vmcs12_pages, because
2254 * we do not have access to L1's MSR bitmap yet. For now, keep
2255 * the same bit as before, hoping to avoid multiple VMWRITEs that
2256 * only set/clear this bit.
2257 */
2258 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
2259 exec_control |= exec_controls_get(vmx) & CPU_BASED_USE_MSR_BITMAPS;
2260
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002261 exec_controls_set(vmx, exec_control);
Sean Christopherson55d23752018-12-03 13:53:18 -08002262
2263 /*
2264 * SECONDARY EXEC CONTROLS
2265 */
2266 if (cpu_has_secondary_exec_ctrls()) {
2267 exec_control = vmx->secondary_exec_control;
2268
2269 /* Take the following fields only from vmcs12 */
2270 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
2271 SECONDARY_EXEC_ENABLE_INVPCID |
Sean Christopherson7f3603b2020-09-23 09:50:47 -07002272 SECONDARY_EXEC_ENABLE_RDTSCP |
Sean Christopherson55d23752018-12-03 13:53:18 -08002273 SECONDARY_EXEC_XSAVES |
Tao Xue69e72fa2019-07-16 14:55:49 +08002274 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE |
Sean Christopherson55d23752018-12-03 13:53:18 -08002275 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
2276 SECONDARY_EXEC_APIC_REGISTER_VIRT |
2277 SECONDARY_EXEC_ENABLE_VMFUNC);
2278 if (nested_cpu_has(vmcs12,
Sean Christophersonc3bb9a22021-02-12 16:50:07 -08002279 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
2280 exec_control |= vmcs12->secondary_vm_exec_control;
2281
2282 /* PML is emulated and never enabled in hardware for L2. */
2283 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Sean Christopherson55d23752018-12-03 13:53:18 -08002284
2285 /* VMCS shadowing for L2 is emulated for now */
2286 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
2287
Sean Christopherson469debd2019-05-07 12:18:02 -07002288 /*
2289 * Preset *DT exiting when emulating UMIP, so that vmx_set_cr4()
2290 * will not have to rewrite the controls just for this bit.
2291 */
2292 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated() &&
2293 (vmcs12->guest_cr4 & X86_CR4_UMIP))
2294 exec_control |= SECONDARY_EXEC_DESC;
2295
Sean Christopherson55d23752018-12-03 13:53:18 -08002296 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
2297 vmcs_write16(GUEST_INTR_STATUS,
2298 vmcs12->guest_intr_status);
2299
Krish Sadhukhanbddd82d2020-09-21 08:10:25 +00002300 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
2301 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
2302
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002303 secondary_exec_controls_set(vmx, exec_control);
Sean Christopherson55d23752018-12-03 13:53:18 -08002304 }
2305
2306 /*
2307 * ENTRY CONTROLS
2308 *
2309 * vmcs12's VM_{ENTRY,EXIT}_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE
2310 * are emulated by vmx_set_efer() in prepare_vmcs02(), but speculate
2311 * on the related bits (if supported by the CPU) in the hope that
2312 * we can avoid VMWrites during vmx_set_efer().
2313 */
2314 exec_control = (vmcs12->vm_entry_controls | vmx_vmentry_ctrl()) &
2315 ~VM_ENTRY_IA32E_MODE & ~VM_ENTRY_LOAD_IA32_EFER;
2316 if (cpu_has_load_ia32_efer()) {
2317 if (guest_efer & EFER_LMA)
2318 exec_control |= VM_ENTRY_IA32E_MODE;
2319 if (guest_efer != host_efer)
2320 exec_control |= VM_ENTRY_LOAD_IA32_EFER;
2321 }
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002322 vm_entry_controls_set(vmx, exec_control);
Sean Christopherson55d23752018-12-03 13:53:18 -08002323
2324 /*
2325 * EXIT CONTROLS
2326 *
2327 * L2->L1 exit controls are emulated - the hardware exit is to L0 so
2328 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
2329 * bits may be modified by vmx_set_efer() in prepare_vmcs02().
2330 */
2331 exec_control = vmx_vmexit_ctrl();
2332 if (cpu_has_load_ia32_efer() && guest_efer != host_efer)
2333 exec_control |= VM_EXIT_LOAD_IA32_EFER;
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002334 vm_exit_controls_set(vmx, exec_control);
Sean Christopherson55d23752018-12-03 13:53:18 -08002335
2336 /*
2337 * Interrupt/Exception Fields
2338 */
2339 if (vmx->nested.nested_run_pending) {
2340 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2341 vmcs12->vm_entry_intr_info_field);
2342 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
2343 vmcs12->vm_entry_exception_error_code);
2344 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2345 vmcs12->vm_entry_instruction_len);
2346 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
2347 vmcs12->guest_interruptibility_info);
2348 vmx->loaded_vmcs->nmi_known_unmasked =
2349 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
2350 } else {
2351 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
2352 }
2353}
2354
Paolo Bonzinib1346ab2019-06-06 17:24:00 +02002355static void prepare_vmcs02_rare(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
Sean Christopherson55d23752018-12-03 13:53:18 -08002356{
2357 struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs;
2358
2359 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
2360 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2)) {
2361 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
2362 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
2363 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
2364 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
2365 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
2366 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
2367 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
2368 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
2369 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
2370 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
2371 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
2372 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
2373 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
2374 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
2375 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
2376 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
2377 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
2378 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07002379 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
2380 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
Sean Christopherson55d23752018-12-03 13:53:18 -08002381 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
2382 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
2383 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
2384 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
2385 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
2386 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
2387 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
2388 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
2389 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
2390 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
2391 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
2392 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
2393 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
2394 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
2395 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
2396 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Sean Christophersonfc387d82020-09-23 11:44:46 -07002397
2398 vmx->segment_cache.bitmask = 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08002399 }
2400
2401 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
2402 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1)) {
2403 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
2404 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
2405 vmcs12->guest_pending_dbg_exceptions);
2406 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
2407 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
2408
2409 /*
2410 * L1 may access the L2's PDPTR, so save them to construct
2411 * vmcs12
2412 */
2413 if (enable_ept) {
2414 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
2415 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
2416 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
2417 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
2418 }
Sean Christophersonc27e5b02019-05-07 09:06:39 -07002419
2420 if (kvm_mpx_supported() && vmx->nested.nested_run_pending &&
2421 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
2422 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
Sean Christopherson55d23752018-12-03 13:53:18 -08002423 }
2424
2425 if (nested_cpu_has_xsaves(vmcs12))
2426 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
2427
2428 /*
2429 * Whether page-faults are trapped is determined by a combination of
Paolo Bonzinia0c13432020-07-10 17:48:08 +02002430 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF. If L0
2431 * doesn't care about page faults then we should set all of these to
2432 * L1's desires. However, if L0 does care about (some) page faults, it
2433 * is not easy (if at all possible?) to merge L0 and L1's desires, we
2434 * simply ask to exit on each and every L2 page fault. This is done by
2435 * setting MASK=MATCH=0 and (see below) EB.PF=1.
Sean Christopherson55d23752018-12-03 13:53:18 -08002436 * Note that below we don't need special code to set EB.PF beyond the
2437 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
2438 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
2439 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
2440 */
Paolo Bonzinia0c13432020-07-10 17:48:08 +02002441 if (vmx_need_pf_intercept(&vmx->vcpu)) {
2442 /*
2443 * TODO: if both L0 and L1 need the same MASK and MATCH,
2444 * go ahead and use it?
2445 */
2446 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
2447 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
2448 } else {
2449 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, vmcs12->page_fault_error_code_mask);
2450 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, vmcs12->page_fault_error_code_match);
2451 }
Sean Christopherson55d23752018-12-03 13:53:18 -08002452
2453 if (cpu_has_vmx_apicv()) {
2454 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
2455 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
2456 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
2457 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
2458 }
2459
Aaron Lewis662f1d12019-11-07 21:14:39 -08002460 /*
2461 * Make sure the msr_autostore list is up to date before we set the
2462 * count in the vmcs02.
2463 */
2464 prepare_vmx_msr_autostore_list(&vmx->vcpu, MSR_IA32_TSC);
2465
2466 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, vmx->msr_autostore.guest.nr);
Sean Christopherson55d23752018-12-03 13:53:18 -08002467 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
2468 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
2469
2470 set_cr4_guest_host_mask(vmx);
Sean Christopherson55d23752018-12-03 13:53:18 -08002471}
2472
2473/*
2474 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
2475 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
2476 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
2477 * guest in a way that will both be appropriate to L1's requests, and our
2478 * needs. In addition to modifying the active vmcs (which is vmcs02), this
2479 * function also has additional necessary side-effects, like setting various
2480 * vcpu->arch fields.
2481 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
2482 * is assigned to entry_failure_code on failure.
2483 */
2484static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Sean Christopherson68cda402020-05-11 15:05:29 -07002485 enum vm_entry_failure_code *entry_failure_code)
Sean Christopherson55d23752018-12-03 13:53:18 -08002486{
2487 struct vcpu_vmx *vmx = to_vmx(vcpu);
2488 struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs;
Sean Christophersonc7554efc2019-05-07 09:06:40 -07002489 bool load_guest_pdptrs_vmcs12 = false;
Sean Christopherson55d23752018-12-03 13:53:18 -08002490
Sean Christophersonc7554efc2019-05-07 09:06:40 -07002491 if (vmx->nested.dirty_vmcs12 || hv_evmcs) {
Paolo Bonzinib1346ab2019-06-06 17:24:00 +02002492 prepare_vmcs02_rare(vmx, vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08002493 vmx->nested.dirty_vmcs12 = false;
Sean Christopherson55d23752018-12-03 13:53:18 -08002494
Sean Christophersonc7554efc2019-05-07 09:06:40 -07002495 load_guest_pdptrs_vmcs12 = !hv_evmcs ||
2496 !(hv_evmcs->hv_clean_fields &
2497 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1);
Sean Christopherson55d23752018-12-03 13:53:18 -08002498 }
2499
2500 if (vmx->nested.nested_run_pending &&
2501 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
2502 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
2503 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
2504 } else {
2505 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
2506 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
2507 }
Sean Christopherson3b013a22019-05-07 09:06:28 -07002508 if (kvm_mpx_supported() && (!vmx->nested.nested_run_pending ||
2509 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)))
2510 vmcs_write64(GUEST_BNDCFGS, vmx->nested.vmcs01_guest_bndcfgs);
Sean Christopherson55d23752018-12-03 13:53:18 -08002511 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
2512
Sean Christopherson55d23752018-12-03 13:53:18 -08002513 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
2514 * bitwise-or of what L1 wants to trap for L2, and what we want to
2515 * trap. Note that CR0.TS also needs updating - we do this later.
2516 */
Jason Baronb6a7cc32021-01-14 22:27:54 -05002517 vmx_update_exception_bitmap(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08002518 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
2519 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
2520
2521 if (vmx->nested.nested_run_pending &&
2522 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
2523 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
2524 vcpu->arch.pat = vmcs12->guest_ia32_pat;
2525 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2526 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
2527 }
2528
2529 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
2530
2531 if (kvm_has_tsc_control)
2532 decache_tsc_multiplier(vmx);
2533
Sean Christopherson50b265a2020-03-20 14:28:19 -07002534 nested_vmx_transition_tlb_flush(vcpu, vmcs12, true);
Sean Christopherson55d23752018-12-03 13:53:18 -08002535
2536 if (nested_cpu_has_ept(vmcs12))
2537 nested_ept_init_mmu_context(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08002538
2539 /*
2540 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
2541 * bits which we consider mandatory enabled.
2542 * The CR0_READ_SHADOW is what L2 should have expected to read given
2543 * the specifications by L1; It's not enough to take
2544 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
2545 * have more bits than L1 expected.
2546 */
2547 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
2548 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
2549
2550 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
2551 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
2552
2553 vcpu->arch.efer = nested_vmx_calc_efer(vmx, vmcs12);
2554 /* Note: may modify VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
2555 vmx_set_efer(vcpu, vcpu->arch.efer);
2556
2557 /*
2558 * Guest state is invalid and unrestricted guest is disabled,
2559 * which means L1 attempted VMEntry to L2 with invalid state.
2560 * Fail the VMEntry.
2561 */
Sean Christopherson2ba44932020-09-23 11:44:48 -07002562 if (CC(!vmx_guest_state_valid(vcpu))) {
Sean Christopherson55d23752018-12-03 13:53:18 -08002563 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christophersonc80add02019-04-11 12:18:09 -07002564 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002565 }
2566
2567 /* Shadow page tables on either EPT or shadow page tables. */
2568 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
2569 entry_failure_code))
Sean Christophersonc80add02019-04-11 12:18:09 -07002570 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002571
Sean Christopherson04f11ef2019-09-27 14:45:16 -07002572 /*
2573 * Immediately write vmcs02.GUEST_CR3. It will be propagated to vmcs12
2574 * on nested VM-Exit, which can occur without actually running L2 and
Paolo Bonzini727a7e22020-03-05 03:52:50 -05002575 * thus without hitting vmx_load_mmu_pgd(), e.g. if L1 is entering L2 with
Sean Christopherson04f11ef2019-09-27 14:45:16 -07002576 * vmcs12.GUEST_ACTIVITYSTATE=HLT, in which case KVM will intercept the
2577 * transition to HLT instead of running L2.
2578 */
2579 if (enable_ept)
2580 vmcs_writel(GUEST_CR3, vmcs12->guest_cr3);
2581
Sean Christophersonc7554efc2019-05-07 09:06:40 -07002582 /* Late preparation of GUEST_PDPTRs now that EFER and CRs are set. */
2583 if (load_guest_pdptrs_vmcs12 && nested_cpu_has_ept(vmcs12) &&
2584 is_pae_paging(vcpu)) {
2585 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
2586 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
2587 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
2588 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
2589 }
2590
Sean Christopherson55d23752018-12-03 13:53:18 -08002591 if (!enable_ept)
2592 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
2593
Oliver Upton71f73472019-11-13 16:17:19 -08002594 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) &&
Oliver Uptond1968422019-12-13 16:33:58 -08002595 WARN_ON_ONCE(kvm_set_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL,
2596 vmcs12->guest_ia32_perf_global_ctrl)))
Oliver Upton71f73472019-11-13 16:17:19 -08002597 return -EINVAL;
2598
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02002599 kvm_rsp_write(vcpu, vmcs12->guest_rsp);
2600 kvm_rip_write(vcpu, vmcs12->guest_rip);
Sean Christopherson55d23752018-12-03 13:53:18 -08002601 return 0;
2602}
2603
2604static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
2605{
Sean Christopherson5497b952019-07-11 08:58:29 -07002606 if (CC(!nested_cpu_has_nmi_exiting(vmcs12) &&
2607 nested_cpu_has_virtual_nmis(vmcs12)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002608 return -EINVAL;
2609
Sean Christopherson5497b952019-07-11 08:58:29 -07002610 if (CC(!nested_cpu_has_virtual_nmis(vmcs12) &&
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08002611 nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002612 return -EINVAL;
2613
2614 return 0;
2615}
2616
Sean Christophersonac6389a2020-03-02 18:02:38 -08002617static bool nested_vmx_check_eptp(struct kvm_vcpu *vcpu, u64 new_eptp)
Sean Christopherson55d23752018-12-03 13:53:18 -08002618{
2619 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08002620
2621 /* Check for memory type validity */
Sean Christophersonac6389a2020-03-02 18:02:38 -08002622 switch (new_eptp & VMX_EPTP_MT_MASK) {
Sean Christopherson55d23752018-12-03 13:53:18 -08002623 case VMX_EPTP_MT_UC:
Sean Christopherson5497b952019-07-11 08:58:29 -07002624 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002625 return false;
2626 break;
2627 case VMX_EPTP_MT_WB:
Sean Christopherson5497b952019-07-11 08:58:29 -07002628 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002629 return false;
2630 break;
2631 default:
2632 return false;
2633 }
2634
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08002635 /* Page-walk levels validity. */
Sean Christophersonac6389a2020-03-02 18:02:38 -08002636 switch (new_eptp & VMX_EPTP_PWL_MASK) {
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08002637 case VMX_EPTP_PWL_5:
2638 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_PAGE_WALK_5_BIT)))
2639 return false;
2640 break;
2641 case VMX_EPTP_PWL_4:
2642 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_PAGE_WALK_4_BIT)))
2643 return false;
2644 break;
2645 default:
Sean Christopherson55d23752018-12-03 13:53:18 -08002646 return false;
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08002647 }
Sean Christopherson55d23752018-12-03 13:53:18 -08002648
2649 /* Reserved bits should not be set */
Sean Christopherson636e8b72021-02-03 16:01:10 -08002650 if (CC(kvm_vcpu_is_illegal_gpa(vcpu, new_eptp) || ((new_eptp >> 7) & 0x1f)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002651 return false;
2652
2653 /* AD, if set, should be supported */
Sean Christophersonac6389a2020-03-02 18:02:38 -08002654 if (new_eptp & VMX_EPTP_AD_ENABLE_BIT) {
Sean Christopherson5497b952019-07-11 08:58:29 -07002655 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002656 return false;
2657 }
2658
2659 return true;
2660}
2661
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002662/*
2663 * Checks related to VM-Execution Control Fields
2664 */
2665static int nested_check_vm_execution_controls(struct kvm_vcpu *vcpu,
2666 struct vmcs12 *vmcs12)
2667{
2668 struct vcpu_vmx *vmx = to_vmx(vcpu);
2669
Sean Christopherson5497b952019-07-11 08:58:29 -07002670 if (CC(!vmx_control_verify(vmcs12->pin_based_vm_exec_control,
2671 vmx->nested.msrs.pinbased_ctls_low,
2672 vmx->nested.msrs.pinbased_ctls_high)) ||
2673 CC(!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
2674 vmx->nested.msrs.procbased_ctls_low,
2675 vmx->nested.msrs.procbased_ctls_high)))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002676 return -EINVAL;
2677
2678 if (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
Sean Christopherson5497b952019-07-11 08:58:29 -07002679 CC(!vmx_control_verify(vmcs12->secondary_vm_exec_control,
2680 vmx->nested.msrs.secondary_ctls_low,
2681 vmx->nested.msrs.secondary_ctls_high)))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002682 return -EINVAL;
2683
Sean Christopherson5497b952019-07-11 08:58:29 -07002684 if (CC(vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu)) ||
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002685 nested_vmx_check_io_bitmap_controls(vcpu, vmcs12) ||
2686 nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12) ||
2687 nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12) ||
2688 nested_vmx_check_apic_access_controls(vcpu, vmcs12) ||
2689 nested_vmx_check_apicv_controls(vcpu, vmcs12) ||
2690 nested_vmx_check_nmi_controls(vmcs12) ||
2691 nested_vmx_check_pml_controls(vcpu, vmcs12) ||
2692 nested_vmx_check_unrestricted_guest_controls(vcpu, vmcs12) ||
2693 nested_vmx_check_mode_based_ept_exec_controls(vcpu, vmcs12) ||
2694 nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12) ||
Sean Christopherson5497b952019-07-11 08:58:29 -07002695 CC(nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002696 return -EINVAL;
2697
Sean Christophersonbc441212019-02-12 16:42:23 -08002698 if (!nested_cpu_has_preemption_timer(vmcs12) &&
2699 nested_cpu_has_save_preemption_timer(vmcs12))
2700 return -EINVAL;
2701
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002702 if (nested_cpu_has_ept(vmcs12) &&
Sean Christophersonac6389a2020-03-02 18:02:38 -08002703 CC(!nested_vmx_check_eptp(vcpu, vmcs12->ept_pointer)))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002704 return -EINVAL;
2705
2706 if (nested_cpu_has_vmfunc(vmcs12)) {
Sean Christopherson5497b952019-07-11 08:58:29 -07002707 if (CC(vmcs12->vm_function_control &
2708 ~vmx->nested.msrs.vmfunc_controls))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002709 return -EINVAL;
2710
2711 if (nested_cpu_has_eptp_switching(vmcs12)) {
Sean Christopherson5497b952019-07-11 08:58:29 -07002712 if (CC(!nested_cpu_has_ept(vmcs12)) ||
2713 CC(!page_address_valid(vcpu, vmcs12->eptp_list_address)))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002714 return -EINVAL;
2715 }
2716 }
2717
2718 return 0;
2719}
2720
Krish Sadhukhan61446ba2018-12-12 13:30:09 -05002721/*
2722 * Checks related to VM-Exit Control Fields
2723 */
2724static int nested_check_vm_exit_controls(struct kvm_vcpu *vcpu,
2725 struct vmcs12 *vmcs12)
2726{
2727 struct vcpu_vmx *vmx = to_vmx(vcpu);
2728
Sean Christopherson5497b952019-07-11 08:58:29 -07002729 if (CC(!vmx_control_verify(vmcs12->vm_exit_controls,
2730 vmx->nested.msrs.exit_ctls_low,
2731 vmx->nested.msrs.exit_ctls_high)) ||
2732 CC(nested_vmx_check_exit_msr_switch_controls(vcpu, vmcs12)))
Krish Sadhukhan61446ba2018-12-12 13:30:09 -05002733 return -EINVAL;
2734
2735 return 0;
2736}
2737
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002738/*
2739 * Checks related to VM-Entry Control Fields
2740 */
2741static int nested_check_vm_entry_controls(struct kvm_vcpu *vcpu,
2742 struct vmcs12 *vmcs12)
Sean Christopherson55d23752018-12-03 13:53:18 -08002743{
2744 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08002745
Sean Christopherson5497b952019-07-11 08:58:29 -07002746 if (CC(!vmx_control_verify(vmcs12->vm_entry_controls,
2747 vmx->nested.msrs.entry_ctls_low,
2748 vmx->nested.msrs.entry_ctls_high)))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002749 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002750
2751 /*
2752 * From the Intel SDM, volume 3:
2753 * Fields relevant to VM-entry event injection must be set properly.
2754 * These fields are the VM-entry interruption-information field, the
2755 * VM-entry exception error code, and the VM-entry instruction length.
2756 */
2757 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
2758 u32 intr_info = vmcs12->vm_entry_intr_info_field;
2759 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
2760 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
2761 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
2762 bool should_have_error_code;
2763 bool urg = nested_cpu_has2(vmcs12,
2764 SECONDARY_EXEC_UNRESTRICTED_GUEST);
2765 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
2766
2767 /* VM-entry interruption-info field: interruption type */
Sean Christopherson5497b952019-07-11 08:58:29 -07002768 if (CC(intr_type == INTR_TYPE_RESERVED) ||
2769 CC(intr_type == INTR_TYPE_OTHER_EVENT &&
2770 !nested_cpu_supports_monitor_trap_flag(vcpu)))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002771 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002772
2773 /* VM-entry interruption-info field: vector */
Sean Christopherson5497b952019-07-11 08:58:29 -07002774 if (CC(intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
2775 CC(intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
2776 CC(intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002777 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002778
2779 /* VM-entry interruption-info field: deliver error code */
2780 should_have_error_code =
2781 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
2782 x86_exception_has_error_code(vector);
Sean Christopherson5497b952019-07-11 08:58:29 -07002783 if (CC(has_error_code != should_have_error_code))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002784 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002785
2786 /* VM-entry exception error code */
Sean Christopherson5497b952019-07-11 08:58:29 -07002787 if (CC(has_error_code &&
Sean Christopherson567926c2019-10-01 09:21:23 -07002788 vmcs12->vm_entry_exception_error_code & GENMASK(31, 16)))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002789 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002790
2791 /* VM-entry interruption-info field: reserved bits */
Sean Christopherson5497b952019-07-11 08:58:29 -07002792 if (CC(intr_info & INTR_INFO_RESVD_BITS_MASK))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002793 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002794
2795 /* VM-entry instruction length */
2796 switch (intr_type) {
2797 case INTR_TYPE_SOFT_EXCEPTION:
2798 case INTR_TYPE_SOFT_INTR:
2799 case INTR_TYPE_PRIV_SW_EXCEPTION:
Sean Christopherson5497b952019-07-11 08:58:29 -07002800 if (CC(vmcs12->vm_entry_instruction_len > 15) ||
2801 CC(vmcs12->vm_entry_instruction_len == 0 &&
2802 CC(!nested_cpu_has_zero_length_injection(vcpu))))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002803 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002804 }
2805 }
2806
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002807 if (nested_vmx_check_entry_msr_switch_controls(vcpu, vmcs12))
2808 return -EINVAL;
2809
2810 return 0;
2811}
2812
Sean Christopherson5478ba32019-04-11 12:18:06 -07002813static int nested_vmx_check_controls(struct kvm_vcpu *vcpu,
2814 struct vmcs12 *vmcs12)
2815{
2816 if (nested_check_vm_execution_controls(vcpu, vmcs12) ||
2817 nested_check_vm_exit_controls(vcpu, vmcs12) ||
2818 nested_check_vm_entry_controls(vcpu, vmcs12))
Paolo Bonzini98d9e852019-04-12 10:19:57 +02002819 return -EINVAL;
Sean Christopherson5478ba32019-04-11 12:18:06 -07002820
Vitaly Kuznetsova8350232020-02-05 13:30:34 +01002821 if (to_vmx(vcpu)->nested.enlightened_vmcs_enabled)
2822 return nested_evmcs_check_controls(vmcs12);
2823
Sean Christopherson5478ba32019-04-11 12:18:06 -07002824 return 0;
2825}
2826
Paolo Bonzini98d9e852019-04-12 10:19:57 +02002827static int nested_vmx_check_host_state(struct kvm_vcpu *vcpu,
2828 struct vmcs12 *vmcs12)
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002829{
2830 bool ia32e;
2831
Sean Christopherson5497b952019-07-11 08:58:29 -07002832 if (CC(!nested_host_cr0_valid(vcpu, vmcs12->host_cr0)) ||
2833 CC(!nested_host_cr4_valid(vcpu, vmcs12->host_cr4)) ||
Sean Christopherson636e8b72021-02-03 16:01:10 -08002834 CC(kvm_vcpu_is_illegal_gpa(vcpu, vmcs12->host_cr3)))
Krish Sadhukhan254b2f32018-12-12 13:30:11 -05002835 return -EINVAL;
Krish Sadhukhan711eff32019-02-07 14:05:30 -05002836
Sean Christopherson5497b952019-07-11 08:58:29 -07002837 if (CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_esp, vcpu)) ||
2838 CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_eip, vcpu)))
Krish Sadhukhan711eff32019-02-07 14:05:30 -05002839 return -EINVAL;
2840
Krish Sadhukhanf6b0db1f2019-04-08 17:35:11 -04002841 if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) &&
Sean Christopherson5497b952019-07-11 08:58:29 -07002842 CC(!kvm_pat_valid(vmcs12->host_ia32_pat)))
Krish Sadhukhanf6b0db1f2019-04-08 17:35:11 -04002843 return -EINVAL;
2844
Oliver Uptonc547cb62019-11-13 16:17:17 -08002845 if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) &&
2846 CC(!kvm_valid_perf_global_ctrl(vcpu_to_pmu(vcpu),
2847 vmcs12->host_ia32_perf_global_ctrl)))
2848 return -EINVAL;
2849
Paolo Bonzinifd3edd42019-09-25 18:33:53 +02002850#ifdef CONFIG_X86_64
2851 ia32e = !!(vcpu->arch.efer & EFER_LMA);
2852#else
2853 ia32e = false;
2854#endif
2855
2856 if (ia32e) {
2857 if (CC(!(vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)) ||
2858 CC(!(vmcs12->host_cr4 & X86_CR4_PAE)))
2859 return -EINVAL;
2860 } else {
2861 if (CC(vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) ||
2862 CC(vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) ||
2863 CC(vmcs12->host_cr4 & X86_CR4_PCIDE) ||
2864 CC((vmcs12->host_rip) >> 32))
2865 return -EINVAL;
2866 }
Krish Sadhukhan1ef23e12019-07-03 19:54:35 -04002867
Sean Christopherson5497b952019-07-11 08:58:29 -07002868 if (CC(vmcs12->host_cs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2869 CC(vmcs12->host_ss_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2870 CC(vmcs12->host_ds_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2871 CC(vmcs12->host_es_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2872 CC(vmcs12->host_fs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2873 CC(vmcs12->host_gs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2874 CC(vmcs12->host_tr_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2875 CC(vmcs12->host_cs_selector == 0) ||
2876 CC(vmcs12->host_tr_selector == 0) ||
2877 CC(vmcs12->host_ss_selector == 0 && !ia32e))
Krish Sadhukhan1ef23e12019-07-03 19:54:35 -04002878 return -EINVAL;
2879
Sean Christopherson5497b952019-07-11 08:58:29 -07002880 if (CC(is_noncanonical_address(vmcs12->host_fs_base, vcpu)) ||
2881 CC(is_noncanonical_address(vmcs12->host_gs_base, vcpu)) ||
2882 CC(is_noncanonical_address(vmcs12->host_gdtr_base, vcpu)) ||
2883 CC(is_noncanonical_address(vmcs12->host_idtr_base, vcpu)) ||
Paolo Bonzinifd3edd42019-09-25 18:33:53 +02002884 CC(is_noncanonical_address(vmcs12->host_tr_base, vcpu)) ||
2885 CC(is_noncanonical_address(vmcs12->host_rip, vcpu)))
Krish Sadhukhan58450382019-08-09 12:26:19 -07002886 return -EINVAL;
Krish Sadhukhan1ef23e12019-07-03 19:54:35 -04002887
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002888 /*
2889 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
2890 * IA32_EFER MSR must be 0 in the field for that register. In addition,
2891 * the values of the LMA and LME bits in the field must each be that of
2892 * the host address-space size VM-exit control.
2893 */
2894 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
Sean Christopherson5497b952019-07-11 08:58:29 -07002895 if (CC(!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer)) ||
2896 CC(ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA)) ||
2897 CC(ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)))
Krish Sadhukhan254b2f32018-12-12 13:30:11 -05002898 return -EINVAL;
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002899 }
2900
Sean Christopherson55d23752018-12-03 13:53:18 -08002901 return 0;
2902}
2903
2904static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
2905 struct vmcs12 *vmcs12)
2906{
KarimAllah Ahmed88925302019-01-31 21:24:41 +01002907 int r = 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08002908 struct vmcs12 *shadow;
KarimAllah Ahmed88925302019-01-31 21:24:41 +01002909 struct kvm_host_map map;
Sean Christopherson55d23752018-12-03 13:53:18 -08002910
2911 if (vmcs12->vmcs_link_pointer == -1ull)
2912 return 0;
2913
Sean Christopherson5497b952019-07-11 08:58:29 -07002914 if (CC(!page_address_valid(vcpu, vmcs12->vmcs_link_pointer)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002915 return -EINVAL;
2916
Sean Christopherson5497b952019-07-11 08:58:29 -07002917 if (CC(kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->vmcs_link_pointer), &map)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002918 return -EINVAL;
2919
KarimAllah Ahmed88925302019-01-31 21:24:41 +01002920 shadow = map.hva;
2921
Sean Christopherson5497b952019-07-11 08:58:29 -07002922 if (CC(shadow->hdr.revision_id != VMCS12_REVISION) ||
2923 CC(shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002924 r = -EINVAL;
KarimAllah Ahmed88925302019-01-31 21:24:41 +01002925
2926 kvm_vcpu_unmap(vcpu, &map, false);
Sean Christopherson55d23752018-12-03 13:53:18 -08002927 return r;
2928}
2929
Sean Christopherson55d23752018-12-03 13:53:18 -08002930/*
2931 * Checks related to Guest Non-register State
2932 */
2933static int nested_check_guest_non_reg_state(struct vmcs12 *vmcs12)
2934{
Sean Christopherson5497b952019-07-11 08:58:29 -07002935 if (CC(vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
Yadong Qibf0cd882020-11-06 14:51:22 +08002936 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT &&
2937 vmcs12->guest_activity_state != GUEST_ACTIVITY_WAIT_SIPI))
Sean Christopherson55d23752018-12-03 13:53:18 -08002938 return -EINVAL;
2939
2940 return 0;
2941}
2942
Sean Christopherson5478ba32019-04-11 12:18:06 -07002943static int nested_vmx_check_guest_state(struct kvm_vcpu *vcpu,
2944 struct vmcs12 *vmcs12,
Sean Christopherson68cda402020-05-11 15:05:29 -07002945 enum vm_entry_failure_code *entry_failure_code)
Sean Christopherson55d23752018-12-03 13:53:18 -08002946{
2947 bool ia32e;
2948
Sean Christopherson68cda402020-05-11 15:05:29 -07002949 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson55d23752018-12-03 13:53:18 -08002950
Sean Christopherson5497b952019-07-11 08:58:29 -07002951 if (CC(!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0)) ||
2952 CC(!nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4)))
Sean Christophersonc80add02019-04-11 12:18:09 -07002953 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002954
Krish Sadhukhanb91991b2020-01-15 19:54:32 -05002955 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) &&
2956 CC(!kvm_dr7_valid(vmcs12->guest_dr7)))
2957 return -EINVAL;
2958
Krish Sadhukhande2bc2b2019-04-08 17:35:12 -04002959 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) &&
Sean Christopherson5497b952019-07-11 08:58:29 -07002960 CC(!kvm_pat_valid(vmcs12->guest_ia32_pat)))
Sean Christophersonc80add02019-04-11 12:18:09 -07002961 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002962
2963 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
Sean Christopherson68cda402020-05-11 15:05:29 -07002964 *entry_failure_code = ENTRY_FAIL_VMCS_LINK_PTR;
Sean Christophersonc80add02019-04-11 12:18:09 -07002965 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002966 }
2967
Oliver Uptonbfc6ad62019-11-13 16:17:16 -08002968 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) &&
2969 CC(!kvm_valid_perf_global_ctrl(vcpu_to_pmu(vcpu),
2970 vmcs12->guest_ia32_perf_global_ctrl)))
2971 return -EINVAL;
2972
Sean Christopherson55d23752018-12-03 13:53:18 -08002973 /*
2974 * If the load IA32_EFER VM-entry control is 1, the following checks
2975 * are performed on the field for the IA32_EFER MSR:
2976 * - Bits reserved in the IA32_EFER MSR must be 0.
2977 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
2978 * the IA-32e mode guest VM-exit control. It must also be identical
2979 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
2980 * CR0.PG) is 1.
2981 */
2982 if (to_vmx(vcpu)->nested.nested_run_pending &&
2983 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
2984 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
Sean Christopherson5497b952019-07-11 08:58:29 -07002985 if (CC(!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer)) ||
2986 CC(ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA)) ||
2987 CC(((vmcs12->guest_cr0 & X86_CR0_PG) &&
2988 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))))
Sean Christophersonc80add02019-04-11 12:18:09 -07002989 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002990 }
2991
2992 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
Sean Christopherson5497b952019-07-11 08:58:29 -07002993 (CC(is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu)) ||
2994 CC((vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD))))
Sean Christophersonc80add02019-04-11 12:18:09 -07002995 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002996
Sean Christopherson9c3e9222019-04-11 12:18:05 -07002997 if (nested_check_guest_non_reg_state(vmcs12))
Sean Christophersonc80add02019-04-11 12:18:09 -07002998 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002999
3000 return 0;
3001}
3002
Sean Christopherson453eafb2018-12-20 12:25:17 -08003003static int nested_vmx_check_vmentry_hw(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08003004{
3005 struct vcpu_vmx *vmx = to_vmx(vcpu);
3006 unsigned long cr3, cr4;
Sean Christophersonf1727b42019-01-25 07:40:58 -08003007 bool vm_fail;
Sean Christopherson55d23752018-12-03 13:53:18 -08003008
3009 if (!nested_early_check)
3010 return 0;
3011
3012 if (vmx->msr_autoload.host.nr)
3013 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
3014 if (vmx->msr_autoload.guest.nr)
3015 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
3016
3017 preempt_disable();
3018
3019 vmx_prepare_switch_to_guest(vcpu);
3020
3021 /*
3022 * Induce a consistency check VMExit by clearing bit 1 in GUEST_RFLAGS,
3023 * which is reserved to '1' by hardware. GUEST_RFLAGS is guaranteed to
Miaohe Lin49f933d2020-02-27 11:20:54 +08003024 * be written (by prepare_vmcs02()) before the "real" VMEnter, i.e.
Sean Christopherson55d23752018-12-03 13:53:18 -08003025 * there is no need to preserve other bits or save/restore the field.
3026 */
3027 vmcs_writel(GUEST_RFLAGS, 0);
3028
Sean Christopherson55d23752018-12-03 13:53:18 -08003029 cr3 = __get_current_cr3_fast();
3030 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
3031 vmcs_writel(HOST_CR3, cr3);
3032 vmx->loaded_vmcs->host_state.cr3 = cr3;
3033 }
3034
3035 cr4 = cr4_read_shadow();
3036 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
3037 vmcs_writel(HOST_CR4, cr4);
3038 vmx->loaded_vmcs->host_state.cr4 = cr4;
3039 }
3040
Uros Bizjak150f17b2020-12-30 16:26:57 -08003041 vm_fail = __vmx_vcpu_run(vmx, (unsigned long *)&vcpu->arch.regs,
3042 vmx->loaded_vmcs->launched);
Sean Christopherson55d23752018-12-03 13:53:18 -08003043
Sean Christopherson55d23752018-12-03 13:53:18 -08003044 if (vmx->msr_autoload.host.nr)
3045 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
3046 if (vmx->msr_autoload.guest.nr)
3047 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
3048
Sean Christophersonf1727b42019-01-25 07:40:58 -08003049 if (vm_fail) {
Sean Christopherson380e0052019-07-11 08:58:30 -07003050 u32 error = vmcs_read32(VM_INSTRUCTION_ERROR);
3051
Wanpeng Li541e8862019-05-17 16:49:50 +08003052 preempt_enable();
Sean Christopherson380e0052019-07-11 08:58:30 -07003053
3054 trace_kvm_nested_vmenter_failed(
3055 "early hardware check VM-instruction error: ", error);
3056 WARN_ON_ONCE(error != VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Sean Christopherson55d23752018-12-03 13:53:18 -08003057 return 1;
3058 }
3059
3060 /*
3061 * VMExit clears RFLAGS.IF and DR7, even on a consistency check.
3062 */
Sean Christopherson55d23752018-12-03 13:53:18 -08003063 if (hw_breakpoint_active())
3064 set_debugreg(__this_cpu_read(cpu_dr7), 7);
Peter Zijlstra84b6a342020-05-29 23:27:36 +02003065 local_irq_enable();
Wanpeng Li541e8862019-05-17 16:49:50 +08003066 preempt_enable();
Sean Christopherson55d23752018-12-03 13:53:18 -08003067
3068 /*
3069 * A non-failing VMEntry means we somehow entered guest mode with
3070 * an illegal RIP, and that's just the tip of the iceberg. There
3071 * is no telling what memory has been modified or what state has
3072 * been exposed to unknown code. Hitting this all but guarantees
3073 * a (very critical) hardware issue.
3074 */
3075 WARN_ON(!(vmcs_read32(VM_EXIT_REASON) &
3076 VMX_EXIT_REASONS_FAILED_VMENTRY));
3077
3078 return 0;
3079}
Sean Christopherson55d23752018-12-03 13:53:18 -08003080
Paolo Bonzini9a78e152021-01-08 11:43:08 -05003081static bool nested_get_evmcs_page(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08003082{
Sean Christopherson55d23752018-12-03 13:53:18 -08003083 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08003084
Vitaly Kuznetsove942dbf2020-03-09 16:52:12 +01003085 /*
3086 * hv_evmcs may end up being not mapped after migration (when
3087 * L2 was running), map it here to make sure vmcs12 changes are
3088 * properly reflected.
3089 */
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003090 if (vmx->nested.enlightened_vmcs_enabled && !vmx->nested.hv_evmcs) {
3091 enum nested_evmptrld_status evmptrld_status =
3092 nested_vmx_handle_enlightened_vmptrld(vcpu, false);
3093
3094 if (evmptrld_status == EVMPTRLD_VMFAIL ||
3095 evmptrld_status == EVMPTRLD_ERROR) {
3096 pr_debug_ratelimited("%s: enlightened vmptrld failed\n",
3097 __func__);
3098 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3099 vcpu->run->internal.suberror =
3100 KVM_INTERNAL_ERROR_EMULATION;
3101 vcpu->run->internal.ndata = 0;
3102 return false;
3103 }
3104 }
Vitaly Kuznetsove942dbf2020-03-09 16:52:12 +01003105
Paolo Bonzini9a78e152021-01-08 11:43:08 -05003106 return true;
3107}
3108
3109static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
3110{
3111 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3112 struct vcpu_vmx *vmx = to_vmx(vcpu);
3113 struct kvm_host_map *map;
3114 struct page *page;
3115 u64 hpa;
3116
Sean Christopherson55d23752018-12-03 13:53:18 -08003117 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
3118 /*
3119 * Translate L1 physical address to host physical
3120 * address for vmcs02. Keep the page pinned, so this
3121 * physical address remains valid. We keep a reference
3122 * to it so we can release it later.
3123 */
3124 if (vmx->nested.apic_access_page) { /* shouldn't happen */
Liran Alonb11494b2019-11-21 00:31:47 +02003125 kvm_release_page_clean(vmx->nested.apic_access_page);
Sean Christopherson55d23752018-12-03 13:53:18 -08003126 vmx->nested.apic_access_page = NULL;
3127 }
3128 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Sean Christopherson55d23752018-12-03 13:53:18 -08003129 if (!is_error_page(page)) {
3130 vmx->nested.apic_access_page = page;
3131 hpa = page_to_phys(vmx->nested.apic_access_page);
3132 vmcs_write64(APIC_ACCESS_ADDR, hpa);
3133 } else {
Jim Mattson671ddc72019-10-15 10:44:05 -07003134 pr_debug_ratelimited("%s: no backing 'struct page' for APIC-access address in vmcs12\n",
3135 __func__);
3136 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3137 vcpu->run->internal.suberror =
3138 KVM_INTERNAL_ERROR_EMULATION;
3139 vcpu->run->internal.ndata = 0;
3140 return false;
Sean Christopherson55d23752018-12-03 13:53:18 -08003141 }
3142 }
3143
3144 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003145 map = &vmx->nested.virtual_apic_map;
Sean Christopherson55d23752018-12-03 13:53:18 -08003146
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003147 if (!kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->virtual_apic_page_addr), map)) {
3148 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, pfn_to_hpa(map->pfn));
Paolo Bonzini69090812019-04-15 15:16:17 +02003149 } else if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING) &&
3150 nested_cpu_has(vmcs12, CPU_BASED_CR8_STORE_EXITING) &&
3151 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
3152 /*
3153 * The processor will never use the TPR shadow, simply
3154 * clear the bit from the execution control. Such a
3155 * configuration is useless, but it happens in tests.
3156 * For any other configuration, failing the vm entry is
3157 * _not_ what the processor does but it's basically the
3158 * only possibility we have.
3159 */
Sean Christopherson2183f562019-05-07 12:17:56 -07003160 exec_controls_clearbit(vmx, CPU_BASED_TPR_SHADOW);
Paolo Bonzini69090812019-04-15 15:16:17 +02003161 } else {
Sean Christophersonca2f5462019-05-07 09:06:33 -07003162 /*
3163 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR to
3164 * force VM-Entry to fail.
3165 */
3166 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Sean Christopherson55d23752018-12-03 13:53:18 -08003167 }
3168 }
3169
3170 if (nested_cpu_has_posted_intr(vmcs12)) {
KarimAllah Ahmed3278e042019-01-31 21:24:38 +01003171 map = &vmx->nested.pi_desc_map;
3172
3173 if (!kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->posted_intr_desc_addr), map)) {
3174 vmx->nested.pi_desc =
3175 (struct pi_desc *)(((void *)map->hva) +
3176 offset_in_page(vmcs12->posted_intr_desc_addr));
3177 vmcs_write64(POSTED_INTR_DESC_ADDR,
3178 pfn_to_hpa(map->pfn) + offset_in_page(vmcs12->posted_intr_desc_addr));
Sean Christopherson55d23752018-12-03 13:53:18 -08003179 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003180 }
3181 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
Sean Christopherson2183f562019-05-07 12:17:56 -07003182 exec_controls_setbit(vmx, CPU_BASED_USE_MSR_BITMAPS);
Sean Christopherson55d23752018-12-03 13:53:18 -08003183 else
Sean Christopherson2183f562019-05-07 12:17:56 -07003184 exec_controls_clearbit(vmx, CPU_BASED_USE_MSR_BITMAPS);
Paolo Bonzini9a78e152021-01-08 11:43:08 -05003185
3186 return true;
3187}
3188
3189static bool vmx_get_nested_state_pages(struct kvm_vcpu *vcpu)
3190{
3191 if (!nested_get_evmcs_page(vcpu))
3192 return false;
3193
3194 if (is_guest_mode(vcpu) && !nested_get_vmcs12_pages(vcpu))
3195 return false;
3196
Jim Mattson671ddc72019-10-15 10:44:05 -07003197 return true;
Sean Christopherson55d23752018-12-03 13:53:18 -08003198}
3199
Sean Christopherson02f5fb22020-06-22 14:58:32 -07003200static int nested_vmx_write_pml_buffer(struct kvm_vcpu *vcpu, gpa_t gpa)
3201{
3202 struct vmcs12 *vmcs12;
3203 struct vcpu_vmx *vmx = to_vmx(vcpu);
3204 gpa_t dst;
3205
3206 if (WARN_ON_ONCE(!is_guest_mode(vcpu)))
3207 return 0;
3208
3209 if (WARN_ON_ONCE(vmx->nested.pml_full))
3210 return 1;
3211
3212 /*
3213 * Check if PML is enabled for the nested guest. Whether eptp bit 6 is
3214 * set is already checked as part of A/D emulation.
3215 */
3216 vmcs12 = get_vmcs12(vcpu);
3217 if (!nested_cpu_has_pml(vmcs12))
3218 return 0;
3219
3220 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
3221 vmx->nested.pml_full = true;
3222 return 1;
3223 }
3224
3225 gpa &= ~0xFFFull;
3226 dst = vmcs12->pml_address + sizeof(u64) * vmcs12->guest_pml_index;
3227
3228 if (kvm_write_guest_page(vcpu->kvm, gpa_to_gfn(dst), &gpa,
3229 offset_in_page(dst), sizeof(gpa)))
3230 return 0;
3231
3232 vmcs12->guest_pml_index--;
3233
3234 return 0;
3235}
3236
Sean Christopherson55d23752018-12-03 13:53:18 -08003237/*
3238 * Intel's VMX Instruction Reference specifies a common set of prerequisites
3239 * for running VMX instructions (except VMXON, whose prerequisites are
3240 * slightly different). It also specifies what exception to inject otherwise.
3241 * Note that many of these exceptions have priority over VM exits, so they
3242 * don't have to be checked again here.
3243 */
3244static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
3245{
3246 if (!to_vmx(vcpu)->nested.vmxon) {
3247 kvm_queue_exception(vcpu, UD_VECTOR);
3248 return 0;
3249 }
3250
3251 if (vmx_get_cpl(vcpu)) {
3252 kvm_inject_gp(vcpu, 0);
3253 return 0;
3254 }
3255
3256 return 1;
3257}
3258
3259static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu)
3260{
3261 u8 rvi = vmx_get_rvi();
3262 u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI);
3263
3264 return ((rvi & 0xf0) > (vppr & 0xf0));
3265}
3266
3267static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
3268 struct vmcs12 *vmcs12);
3269
3270/*
3271 * If from_vmentry is false, this is being called from state restore (either RSM
3272 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
Jim Mattson671ddc72019-10-15 10:44:05 -07003273 *
3274 * Returns:
Miaohe Lin463bfee2020-02-14 10:44:05 +08003275 * NVMX_VMENTRY_SUCCESS: Entered VMX non-root mode
3276 * NVMX_VMENTRY_VMFAIL: Consistency check VMFail
3277 * NVMX_VMENTRY_VMEXIT: Consistency check VMExit
3278 * NVMX_VMENTRY_KVM_INTERNAL_ERROR: KVM internal error
Sean Christopherson55d23752018-12-03 13:53:18 -08003279 */
Jim Mattson671ddc72019-10-15 10:44:05 -07003280enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu,
3281 bool from_vmentry)
Sean Christopherson55d23752018-12-03 13:53:18 -08003282{
3283 struct vcpu_vmx *vmx = to_vmx(vcpu);
3284 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Sean Christopherson68cda402020-05-11 15:05:29 -07003285 enum vm_entry_failure_code entry_failure_code;
Sean Christopherson55d23752018-12-03 13:53:18 -08003286 bool evaluate_pending_interrupts;
Sean Christopherson8e533242020-11-06 17:03:12 +08003287 union vmx_exit_reason exit_reason = {
3288 .basic = EXIT_REASON_INVALID_STATE,
3289 .failed_vmentry = 1,
3290 };
3291 u32 failed_index;
Sean Christopherson55d23752018-12-03 13:53:18 -08003292
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07003293 if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu))
3294 kvm_vcpu_flush_tlb_current(vcpu);
3295
Sean Christopherson2183f562019-05-07 12:17:56 -07003296 evaluate_pending_interrupts = exec_controls_get(vmx) &
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08003297 (CPU_BASED_INTR_WINDOW_EXITING | CPU_BASED_NMI_WINDOW_EXITING);
Sean Christopherson55d23752018-12-03 13:53:18 -08003298 if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu))
3299 evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu);
3300
3301 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
3302 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
3303 if (kvm_mpx_supported() &&
3304 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
3305 vmx->nested.vmcs01_guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
3306
Sean Christophersonf087a022019-06-07 11:55:34 -07003307 /*
3308 * Overwrite vmcs01.GUEST_CR3 with L1's CR3 if EPT is disabled *and*
3309 * nested early checks are disabled. In the event of a "late" VM-Fail,
3310 * i.e. a VM-Fail detected by hardware but not KVM, KVM must unwind its
3311 * software model to the pre-VMEntry host state. When EPT is disabled,
3312 * GUEST_CR3 holds KVM's shadow CR3, not L1's "real" CR3, which causes
3313 * nested_vmx_restore_host_state() to corrupt vcpu->arch.cr3. Stuffing
3314 * vmcs01.GUEST_CR3 results in the unwind naturally setting arch.cr3 to
3315 * the correct value. Smashing vmcs01.GUEST_CR3 is safe because nested
3316 * VM-Exits, and the unwind, reset KVM's MMU, i.e. vmcs01.GUEST_CR3 is
3317 * guaranteed to be overwritten with a shadow CR3 prior to re-entering
3318 * L1. Don't stuff vmcs01.GUEST_CR3 when using nested early checks as
3319 * KVM modifies vcpu->arch.cr3 if and only if the early hardware checks
3320 * pass, and early VM-Fails do not reset KVM's MMU, i.e. the VM-Fail
3321 * path would need to manually save/restore vmcs01.GUEST_CR3.
3322 */
3323 if (!enable_ept && !nested_early_check)
3324 vmcs_writel(GUEST_CR3, vcpu->arch.cr3);
3325
Sean Christopherson55d23752018-12-03 13:53:18 -08003326 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
3327
3328 prepare_vmcs02_early(vmx, vmcs12);
3329
3330 if (from_vmentry) {
Sean Christophersonb89d5ad2020-09-23 11:44:47 -07003331 if (unlikely(!nested_get_vmcs12_pages(vcpu))) {
3332 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson671ddc72019-10-15 10:44:05 -07003333 return NVMX_VMENTRY_KVM_INTERNAL_ERROR;
Sean Christophersonb89d5ad2020-09-23 11:44:47 -07003334 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003335
3336 if (nested_vmx_check_vmentry_hw(vcpu)) {
3337 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson671ddc72019-10-15 10:44:05 -07003338 return NVMX_VMENTRY_VMFAIL;
Sean Christopherson55d23752018-12-03 13:53:18 -08003339 }
3340
Sean Christopherson68cda402020-05-11 15:05:29 -07003341 if (nested_vmx_check_guest_state(vcpu, vmcs12,
3342 &entry_failure_code)) {
Sean Christopherson8e533242020-11-06 17:03:12 +08003343 exit_reason.basic = EXIT_REASON_INVALID_STATE;
Sean Christopherson68cda402020-05-11 15:05:29 -07003344 vmcs12->exit_qualification = entry_failure_code;
Sean Christopherson55d23752018-12-03 13:53:18 -08003345 goto vmentry_fail_vmexit;
Sean Christopherson68cda402020-05-11 15:05:29 -07003346 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003347 }
3348
3349 enter_guest_mode(vcpu);
Xiaoyao Li5e3d3942019-12-06 16:45:26 +08003350 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING)
Sean Christopherson55d23752018-12-03 13:53:18 -08003351 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
3352
Sean Christopherson68cda402020-05-11 15:05:29 -07003353 if (prepare_vmcs02(vcpu, vmcs12, &entry_failure_code)) {
Sean Christopherson8e533242020-11-06 17:03:12 +08003354 exit_reason.basic = EXIT_REASON_INVALID_STATE;
Sean Christopherson68cda402020-05-11 15:05:29 -07003355 vmcs12->exit_qualification = entry_failure_code;
Sean Christopherson55d23752018-12-03 13:53:18 -08003356 goto vmentry_fail_vmexit_guest_mode;
Sean Christopherson68cda402020-05-11 15:05:29 -07003357 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003358
3359 if (from_vmentry) {
Sean Christopherson68cda402020-05-11 15:05:29 -07003360 failed_index = nested_vmx_load_msr(vcpu,
3361 vmcs12->vm_entry_msr_load_addr,
3362 vmcs12->vm_entry_msr_load_count);
3363 if (failed_index) {
Sean Christopherson8e533242020-11-06 17:03:12 +08003364 exit_reason.basic = EXIT_REASON_MSR_LOAD_FAIL;
Sean Christopherson68cda402020-05-11 15:05:29 -07003365 vmcs12->exit_qualification = failed_index;
Sean Christopherson55d23752018-12-03 13:53:18 -08003366 goto vmentry_fail_vmexit_guest_mode;
Sean Christopherson68cda402020-05-11 15:05:29 -07003367 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003368 } else {
3369 /*
3370 * The MMU is not initialized to point at the right entities yet and
3371 * "get pages" would need to read data from the guest (i.e. we will
3372 * need to perform gpa to hpa translation). Request a call
3373 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
3374 * have already been set at vmentry time and should not be reset.
3375 */
Paolo Bonzini729c15c2020-09-22 06:53:57 -04003376 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08003377 }
3378
3379 /*
3380 * If L1 had a pending IRQ/NMI until it executed
3381 * VMLAUNCH/VMRESUME which wasn't delivered because it was
3382 * disallowed (e.g. interrupts disabled), L0 needs to
3383 * evaluate if this pending event should cause an exit from L2
3384 * to L1 or delivered directly to L2 (e.g. In case L1 don't
3385 * intercept EXTERNAL_INTERRUPT).
3386 *
3387 * Usually this would be handled by the processor noticing an
3388 * IRQ/NMI window request, or checking RVI during evaluation of
3389 * pending virtual interrupts. However, this setting was done
3390 * on VMCS01 and now VMCS02 is active instead. Thus, we force L0
3391 * to perform pending event evaluation by requesting a KVM_REQ_EVENT.
3392 */
3393 if (unlikely(evaluate_pending_interrupts))
3394 kvm_make_request(KVM_REQ_EVENT, vcpu);
3395
3396 /*
Paolo Bonzini359a6c32019-01-29 19:14:46 +01003397 * Do not start the preemption timer hrtimer until after we know
3398 * we are successful, so that only nested_vmx_vmexit needs to cancel
3399 * the timer.
3400 */
3401 vmx->nested.preemption_timer_expired = false;
Peter Shier850448f2020-05-26 14:51:06 -07003402 if (nested_cpu_has_preemption_timer(vmcs12)) {
3403 u64 timer_value = vmx_calc_preemption_timer_value(vcpu);
3404 vmx_start_preemption_timer(vcpu, timer_value);
3405 }
Paolo Bonzini359a6c32019-01-29 19:14:46 +01003406
3407 /*
Sean Christopherson55d23752018-12-03 13:53:18 -08003408 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
3409 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
3410 * returned as far as L1 is concerned. It will only return (and set
3411 * the success flag) when L2 exits (see nested_vmx_vmexit()).
3412 */
Jim Mattson671ddc72019-10-15 10:44:05 -07003413 return NVMX_VMENTRY_SUCCESS;
Sean Christopherson55d23752018-12-03 13:53:18 -08003414
3415 /*
3416 * A failed consistency check that leads to a VMExit during L1's
3417 * VMEnter to L2 is a variation of a normal VMexit, as explained in
3418 * 26.7 "VM-entry failures during or after loading guest state".
3419 */
3420vmentry_fail_vmexit_guest_mode:
Xiaoyao Li5e3d3942019-12-06 16:45:26 +08003421 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING)
Sean Christopherson55d23752018-12-03 13:53:18 -08003422 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
3423 leave_guest_mode(vcpu);
3424
3425vmentry_fail_vmexit:
3426 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
3427
3428 if (!from_vmentry)
Jim Mattson671ddc72019-10-15 10:44:05 -07003429 return NVMX_VMENTRY_VMEXIT;
Sean Christopherson55d23752018-12-03 13:53:18 -08003430
3431 load_vmcs12_host_state(vcpu, vmcs12);
Sean Christopherson8e533242020-11-06 17:03:12 +08003432 vmcs12->vm_exit_reason = exit_reason.full;
Sean Christopherson55d23752018-12-03 13:53:18 -08003433 if (enable_shadow_vmcs || vmx->nested.hv_evmcs)
Sean Christopherson3731905ef2019-05-07 08:36:27 -07003434 vmx->nested.need_vmcs12_to_shadow_sync = true;
Jim Mattson671ddc72019-10-15 10:44:05 -07003435 return NVMX_VMENTRY_VMEXIT;
Sean Christopherson55d23752018-12-03 13:53:18 -08003436}
3437
3438/*
3439 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
3440 * for running an L2 nested guest.
3441 */
3442static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
3443{
3444 struct vmcs12 *vmcs12;
Jim Mattson671ddc72019-10-15 10:44:05 -07003445 enum nvmx_vmentry_status status;
Sean Christopherson55d23752018-12-03 13:53:18 -08003446 struct vcpu_vmx *vmx = to_vmx(vcpu);
3447 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003448 enum nested_evmptrld_status evmptrld_status;
Sean Christopherson55d23752018-12-03 13:53:18 -08003449
Dongli Zhang43c11d92021-03-05 14:57:47 -08003450 ++vcpu->stat.nested_run;
3451
Sean Christopherson55d23752018-12-03 13:53:18 -08003452 if (!nested_vmx_check_permission(vcpu))
3453 return 1;
3454
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003455 evmptrld_status = nested_vmx_handle_enlightened_vmptrld(vcpu, launch);
3456 if (evmptrld_status == EVMPTRLD_ERROR) {
3457 kvm_queue_exception(vcpu, UD_VECTOR);
Sean Christopherson55d23752018-12-03 13:53:18 -08003458 return 1;
Sean Christophersonfc595f32020-08-12 11:06:15 -07003459 } else if (CC(evmptrld_status == EVMPTRLD_VMFAIL)) {
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003460 return nested_vmx_failInvalid(vcpu);
3461 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003462
Sean Christophersonfc595f32020-08-12 11:06:15 -07003463 if (CC(!vmx->nested.hv_evmcs && vmx->nested.current_vmptr == -1ull))
Sean Christopherson55d23752018-12-03 13:53:18 -08003464 return nested_vmx_failInvalid(vcpu);
3465
3466 vmcs12 = get_vmcs12(vcpu);
3467
3468 /*
3469 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
3470 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
3471 * rather than RFLAGS.ZF, and no error number is stored to the
3472 * VM-instruction error field.
3473 */
Sean Christophersonfc595f32020-08-12 11:06:15 -07003474 if (CC(vmcs12->hdr.shadow_vmcs))
Sean Christopherson55d23752018-12-03 13:53:18 -08003475 return nested_vmx_failInvalid(vcpu);
3476
3477 if (vmx->nested.hv_evmcs) {
3478 copy_enlightened_to_vmcs12(vmx);
3479 /* Enlightened VMCS doesn't have launch state */
3480 vmcs12->launch_state = !launch;
3481 } else if (enable_shadow_vmcs) {
3482 copy_shadow_to_vmcs12(vmx);
3483 }
3484
3485 /*
3486 * The nested entry process starts with enforcing various prerequisites
3487 * on vmcs12 as required by the Intel SDM, and act appropriately when
3488 * they fail: As the SDM explains, some conditions should cause the
3489 * instruction to fail, while others will cause the instruction to seem
3490 * to succeed, but return an EXIT_REASON_INVALID_STATE.
3491 * To speed up the normal (success) code path, we should avoid checking
3492 * for misconfigurations which will anyway be caught by the processor
3493 * when using the merged vmcs02.
3494 */
Sean Christophersonfc595f32020-08-12 11:06:15 -07003495 if (CC(interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS))
Sean Christophersonb2656e42020-06-08 18:56:07 -07003496 return nested_vmx_fail(vcpu, VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
Sean Christopherson55d23752018-12-03 13:53:18 -08003497
Sean Christophersonfc595f32020-08-12 11:06:15 -07003498 if (CC(vmcs12->launch_state == launch))
Sean Christophersonb2656e42020-06-08 18:56:07 -07003499 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08003500 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
3501 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
3502
Paolo Bonzini98d9e852019-04-12 10:19:57 +02003503 if (nested_vmx_check_controls(vcpu, vmcs12))
Sean Christophersonb2656e42020-06-08 18:56:07 -07003504 return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Sean Christopherson5478ba32019-04-11 12:18:06 -07003505
Paolo Bonzini98d9e852019-04-12 10:19:57 +02003506 if (nested_vmx_check_host_state(vcpu, vmcs12))
Sean Christophersonb2656e42020-06-08 18:56:07 -07003507 return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
Sean Christopherson55d23752018-12-03 13:53:18 -08003508
3509 /*
3510 * We're finally done with prerequisite checking, and can start with
3511 * the nested entry.
3512 */
3513 vmx->nested.nested_run_pending = 1;
Peter Shier850448f2020-05-26 14:51:06 -07003514 vmx->nested.has_preemption_timer_deadline = false;
Jim Mattson671ddc72019-10-15 10:44:05 -07003515 status = nested_vmx_enter_non_root_mode(vcpu, true);
3516 if (unlikely(status != NVMX_VMENTRY_SUCCESS))
3517 goto vmentry_failed;
Sean Christopherson55d23752018-12-03 13:53:18 -08003518
Sean Christopherson25bb2cf2020-08-12 10:51:29 -07003519 /* Emulate processing of posted interrupts on VM-Enter. */
3520 if (nested_cpu_has_posted_intr(vmcs12) &&
3521 kvm_apic_has_interrupt(vcpu) == vmx->nested.posted_intr_nv) {
3522 vmx->nested.pi_pending = true;
3523 kvm_make_request(KVM_REQ_EVENT, vcpu);
3524 kvm_apic_clear_irr(vcpu, vmx->nested.posted_intr_nv);
3525 }
3526
Sean Christopherson55d23752018-12-03 13:53:18 -08003527 /* Hide L1D cache contents from the nested guest. */
3528 vmx->vcpu.arch.l1tf_flush_l1d = true;
3529
3530 /*
3531 * Must happen outside of nested_vmx_enter_non_root_mode() as it will
3532 * also be used as part of restoring nVMX state for
3533 * snapshot restore (migration).
3534 *
3535 * In this flow, it is assumed that vmcs12 cache was
3536 * trasferred as part of captured nVMX state and should
3537 * therefore not be read from guest memory (which may not
3538 * exist on destination host yet).
3539 */
3540 nested_cache_shadow_vmcs12(vcpu, vmcs12);
3541
Yadong Qibf0cd882020-11-06 14:51:22 +08003542 switch (vmcs12->guest_activity_state) {
3543 case GUEST_ACTIVITY_HLT:
3544 /*
3545 * If we're entering a halted L2 vcpu and the L2 vcpu won't be
3546 * awakened by event injection or by an NMI-window VM-exit or
3547 * by an interrupt-window VM-exit, halt the vcpu.
3548 */
3549 if (!(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) &&
3550 !nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING) &&
3551 !(nested_cpu_has(vmcs12, CPU_BASED_INTR_WINDOW_EXITING) &&
3552 (vmcs12->guest_rflags & X86_EFLAGS_IF))) {
3553 vmx->nested.nested_run_pending = 0;
3554 return kvm_vcpu_halt(vcpu);
3555 }
3556 break;
3557 case GUEST_ACTIVITY_WAIT_SIPI:
Sean Christopherson55d23752018-12-03 13:53:18 -08003558 vmx->nested.nested_run_pending = 0;
Yadong Qibf0cd882020-11-06 14:51:22 +08003559 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
3560 break;
3561 default:
3562 break;
Sean Christopherson55d23752018-12-03 13:53:18 -08003563 }
Yadong Qibf0cd882020-11-06 14:51:22 +08003564
Sean Christopherson55d23752018-12-03 13:53:18 -08003565 return 1;
Jim Mattson671ddc72019-10-15 10:44:05 -07003566
3567vmentry_failed:
3568 vmx->nested.nested_run_pending = 0;
3569 if (status == NVMX_VMENTRY_KVM_INTERNAL_ERROR)
3570 return 0;
3571 if (status == NVMX_VMENTRY_VMEXIT)
3572 return 1;
3573 WARN_ON_ONCE(status != NVMX_VMENTRY_VMFAIL);
Sean Christophersonb2656e42020-06-08 18:56:07 -07003574 return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Sean Christopherson55d23752018-12-03 13:53:18 -08003575}
3576
3577/*
3578 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
Miaohe Lin67b0ae42019-12-11 14:26:22 +08003579 * because L2 may have changed some cr0 bits directly (CR0_GUEST_HOST_MASK).
Sean Christopherson55d23752018-12-03 13:53:18 -08003580 * This function returns the new value we should put in vmcs12.guest_cr0.
3581 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
3582 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
3583 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
3584 * didn't trap the bit, because if L1 did, so would L0).
3585 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
3586 * been modified by L2, and L1 knows it. So just leave the old value of
3587 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
3588 * isn't relevant, because if L0 traps this bit it can set it to anything.
3589 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
3590 * changed these bits, and therefore they need to be updated, but L0
3591 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
3592 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
3593 */
3594static inline unsigned long
3595vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
3596{
3597 return
3598 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
3599 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
3600 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
3601 vcpu->arch.cr0_guest_owned_bits));
3602}
3603
3604static inline unsigned long
3605vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
3606{
3607 return
3608 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
3609 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
3610 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
3611 vcpu->arch.cr4_guest_owned_bits));
3612}
3613
3614static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
3615 struct vmcs12 *vmcs12)
3616{
3617 u32 idt_vectoring;
3618 unsigned int nr;
3619
3620 if (vcpu->arch.exception.injected) {
3621 nr = vcpu->arch.exception.nr;
3622 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
3623
3624 if (kvm_exception_is_soft(nr)) {
3625 vmcs12->vm_exit_instruction_len =
3626 vcpu->arch.event_exit_inst_len;
3627 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
3628 } else
3629 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
3630
3631 if (vcpu->arch.exception.has_error_code) {
3632 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
3633 vmcs12->idt_vectoring_error_code =
3634 vcpu->arch.exception.error_code;
3635 }
3636
3637 vmcs12->idt_vectoring_info_field = idt_vectoring;
3638 } else if (vcpu->arch.nmi_injected) {
3639 vmcs12->idt_vectoring_info_field =
3640 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
3641 } else if (vcpu->arch.interrupt.injected) {
3642 nr = vcpu->arch.interrupt.nr;
3643 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
3644
3645 if (vcpu->arch.interrupt.soft) {
3646 idt_vectoring |= INTR_TYPE_SOFT_INTR;
3647 vmcs12->vm_entry_instruction_len =
3648 vcpu->arch.event_exit_inst_len;
3649 } else
3650 idt_vectoring |= INTR_TYPE_EXT_INTR;
3651
3652 vmcs12->idt_vectoring_info_field = idt_vectoring;
3653 }
3654}
3655
3656
Paolo Bonzini96b100c2020-03-17 18:32:50 +01003657void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08003658{
3659 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3660 gfn_t gfn;
3661
3662 /*
3663 * Don't need to mark the APIC access page dirty; it is never
3664 * written to by the CPU during APIC virtualization.
3665 */
3666
3667 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
3668 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
3669 kvm_vcpu_mark_page_dirty(vcpu, gfn);
3670 }
3671
3672 if (nested_cpu_has_posted_intr(vmcs12)) {
3673 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
3674 kvm_vcpu_mark_page_dirty(vcpu, gfn);
3675 }
3676}
3677
3678static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
3679{
3680 struct vcpu_vmx *vmx = to_vmx(vcpu);
3681 int max_irr;
3682 void *vapic_page;
3683 u16 status;
3684
3685 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
3686 return;
3687
3688 vmx->nested.pi_pending = false;
3689 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
3690 return;
3691
3692 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
3693 if (max_irr != 256) {
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003694 vapic_page = vmx->nested.virtual_apic_map.hva;
3695 if (!vapic_page)
3696 return;
3697
Sean Christopherson55d23752018-12-03 13:53:18 -08003698 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
3699 vapic_page, &max_irr);
Sean Christopherson55d23752018-12-03 13:53:18 -08003700 status = vmcs_read16(GUEST_INTR_STATUS);
3701 if ((u8)max_irr > ((u8)status & 0xff)) {
3702 status &= ~0xff;
3703 status |= (u8)max_irr;
3704 vmcs_write16(GUEST_INTR_STATUS, status);
3705 }
3706 }
3707
3708 nested_mark_vmcs12_pages_dirty(vcpu);
3709}
3710
3711static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3712 unsigned long exit_qual)
3713{
3714 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3715 unsigned int nr = vcpu->arch.exception.nr;
3716 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3717
3718 if (vcpu->arch.exception.has_error_code) {
3719 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3720 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3721 }
3722
3723 if (kvm_exception_is_soft(nr))
3724 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3725 else
3726 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3727
3728 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3729 vmx_get_nmi_mask(vcpu))
3730 intr_info |= INTR_INFO_UNBLOCK_NMI;
3731
3732 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3733}
3734
Oliver Upton684c0422020-02-07 02:36:05 -08003735/*
3736 * Returns true if a debug trap is pending delivery.
3737 *
3738 * In KVM, debug traps bear an exception payload. As such, the class of a #DB
3739 * exception may be inferred from the presence of an exception payload.
3740 */
3741static inline bool vmx_pending_dbg_trap(struct kvm_vcpu *vcpu)
3742{
3743 return vcpu->arch.exception.pending &&
3744 vcpu->arch.exception.nr == DB_VECTOR &&
3745 vcpu->arch.exception.payload;
3746}
3747
3748/*
3749 * Certain VM-exits set the 'pending debug exceptions' field to indicate a
3750 * recognized #DB (data or single-step) that has yet to be delivered. Since KVM
3751 * represents these debug traps with a payload that is said to be compatible
3752 * with the 'pending debug exceptions' field, write the payload to the VMCS
3753 * field if a VM-exit is delivered before the debug trap.
3754 */
3755static void nested_vmx_update_pending_dbg(struct kvm_vcpu *vcpu)
3756{
3757 if (vmx_pending_dbg_trap(vcpu))
3758 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
3759 vcpu->arch.exception.payload);
3760}
3761
Sean Christophersond2060bd2020-04-22 19:25:39 -07003762static bool nested_vmx_preemption_timer_pending(struct kvm_vcpu *vcpu)
3763{
3764 return nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
3765 to_vmx(vcpu)->nested.preemption_timer_expired;
3766}
3767
Sean Christophersona1c77ab2020-03-02 22:27:35 -08003768static int vmx_check_nested_events(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08003769{
3770 struct vcpu_vmx *vmx = to_vmx(vcpu);
3771 unsigned long exit_qual;
3772 bool block_nested_events =
3773 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003774 bool mtf_pending = vmx->nested.mtf_pending;
Liran Alon4b9852f2019-08-26 13:24:49 +03003775 struct kvm_lapic *apic = vcpu->arch.apic;
3776
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003777 /*
3778 * Clear the MTF state. If a higher priority VM-exit is delivered first,
3779 * this state is discarded.
3780 */
Oliver Upton5c8beb42020-04-06 20:12:37 +00003781 if (!block_nested_events)
3782 vmx->nested.mtf_pending = false;
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003783
Liran Alon4b9852f2019-08-26 13:24:49 +03003784 if (lapic_in_kernel(vcpu) &&
3785 test_bit(KVM_APIC_INIT, &apic->pending_events)) {
3786 if (block_nested_events)
3787 return -EBUSY;
Oliver Upton684c0422020-02-07 02:36:05 -08003788 nested_vmx_update_pending_dbg(vcpu);
Liran Alone64a8502019-11-11 14:16:05 +02003789 clear_bit(KVM_APIC_INIT, &apic->pending_events);
Yadong Qibf0cd882020-11-06 14:51:22 +08003790 if (vcpu->arch.mp_state != KVM_MP_STATE_INIT_RECEIVED)
3791 nested_vmx_vmexit(vcpu, EXIT_REASON_INIT_SIGNAL, 0, 0);
3792 return 0;
3793 }
3794
3795 if (lapic_in_kernel(vcpu) &&
3796 test_bit(KVM_APIC_SIPI, &apic->pending_events)) {
3797 if (block_nested_events)
3798 return -EBUSY;
3799
3800 clear_bit(KVM_APIC_SIPI, &apic->pending_events);
3801 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED)
3802 nested_vmx_vmexit(vcpu, EXIT_REASON_SIPI_SIGNAL, 0,
3803 apic->sipi_vector & 0xFFUL);
Liran Alon4b9852f2019-08-26 13:24:49 +03003804 return 0;
3805 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003806
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003807 /*
3808 * Process any exceptions that are not debug traps before MTF.
Maxim Levitsky4020da32021-04-01 17:38:14 +03003809 *
3810 * Note that only a pending nested run can block a pending exception.
3811 * Otherwise an injected NMI/interrupt should either be
3812 * lost or delivered to the nested hypervisor in the IDT_VECTORING_INFO,
3813 * while delivering the pending exception.
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003814 */
Maxim Levitsky4020da32021-04-01 17:38:14 +03003815
Sean Christopherson6ce347a2020-04-22 19:25:38 -07003816 if (vcpu->arch.exception.pending && !vmx_pending_dbg_trap(vcpu)) {
Maxim Levitsky4020da32021-04-01 17:38:14 +03003817 if (vmx->nested.nested_run_pending)
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003818 return -EBUSY;
Sean Christopherson6ce347a2020-04-22 19:25:38 -07003819 if (!nested_vmx_check_exception(vcpu, &exit_qual))
3820 goto no_vmexit;
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003821 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
3822 return 0;
3823 }
3824
3825 if (mtf_pending) {
3826 if (block_nested_events)
3827 return -EBUSY;
3828 nested_vmx_update_pending_dbg(vcpu);
3829 nested_vmx_vmexit(vcpu, EXIT_REASON_MONITOR_TRAP_FLAG, 0, 0);
3830 return 0;
3831 }
3832
Sean Christopherson6ce347a2020-04-22 19:25:38 -07003833 if (vcpu->arch.exception.pending) {
Maxim Levitsky4020da32021-04-01 17:38:14 +03003834 if (vmx->nested.nested_run_pending)
Sean Christopherson55d23752018-12-03 13:53:18 -08003835 return -EBUSY;
Sean Christopherson6ce347a2020-04-22 19:25:38 -07003836 if (!nested_vmx_check_exception(vcpu, &exit_qual))
3837 goto no_vmexit;
Sean Christopherson55d23752018-12-03 13:53:18 -08003838 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
3839 return 0;
3840 }
3841
Sean Christophersond2060bd2020-04-22 19:25:39 -07003842 if (nested_vmx_preemption_timer_pending(vcpu)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08003843 if (block_nested_events)
3844 return -EBUSY;
3845 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
3846 return 0;
3847 }
3848
Sean Christopherson1cd2f0b2020-04-22 19:25:46 -07003849 if (vcpu->arch.smi_pending && !is_smm(vcpu)) {
3850 if (block_nested_events)
3851 return -EBUSY;
3852 goto no_vmexit;
3853 }
3854
Sean Christopherson15ff0b42020-04-22 19:25:45 -07003855 if (vcpu->arch.nmi_pending && !vmx_nmi_blocked(vcpu)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08003856 if (block_nested_events)
3857 return -EBUSY;
Sean Christopherson15ff0b42020-04-22 19:25:45 -07003858 if (!nested_exit_on_nmi(vcpu))
3859 goto no_vmexit;
3860
Sean Christopherson55d23752018-12-03 13:53:18 -08003861 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
3862 NMI_VECTOR | INTR_TYPE_NMI_INTR |
3863 INTR_INFO_VALID_MASK, 0);
3864 /*
3865 * The NMI-triggered VM exit counts as injection:
3866 * clear this one and block further NMIs.
3867 */
3868 vcpu->arch.nmi_pending = 0;
3869 vmx_set_nmi_mask(vcpu, true);
3870 return 0;
3871 }
3872
Sean Christopherson15ff0b42020-04-22 19:25:45 -07003873 if (kvm_cpu_has_interrupt(vcpu) && !vmx_interrupt_blocked(vcpu)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08003874 if (block_nested_events)
3875 return -EBUSY;
Sean Christopherson15ff0b42020-04-22 19:25:45 -07003876 if (!nested_exit_on_intr(vcpu))
3877 goto no_vmexit;
Sean Christopherson55d23752018-12-03 13:53:18 -08003878 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
3879 return 0;
3880 }
3881
Sean Christopherson6ce347a2020-04-22 19:25:38 -07003882no_vmexit:
Sean Christopherson55d23752018-12-03 13:53:18 -08003883 vmx_complete_nested_posted_interrupt(vcpu);
3884 return 0;
3885}
3886
3887static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
3888{
3889 ktime_t remaining =
3890 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
3891 u64 value;
3892
3893 if (ktime_to_ns(remaining) <= 0)
3894 return 0;
3895
3896 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
3897 do_div(value, 1000000);
3898 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
3899}
3900
Sean Christopherson7952d762019-05-07 08:36:29 -07003901static bool is_vmcs12_ext_field(unsigned long field)
Sean Christopherson55d23752018-12-03 13:53:18 -08003902{
Sean Christopherson7952d762019-05-07 08:36:29 -07003903 switch (field) {
3904 case GUEST_ES_SELECTOR:
3905 case GUEST_CS_SELECTOR:
3906 case GUEST_SS_SELECTOR:
3907 case GUEST_DS_SELECTOR:
3908 case GUEST_FS_SELECTOR:
3909 case GUEST_GS_SELECTOR:
3910 case GUEST_LDTR_SELECTOR:
3911 case GUEST_TR_SELECTOR:
3912 case GUEST_ES_LIMIT:
3913 case GUEST_CS_LIMIT:
3914 case GUEST_SS_LIMIT:
3915 case GUEST_DS_LIMIT:
3916 case GUEST_FS_LIMIT:
3917 case GUEST_GS_LIMIT:
3918 case GUEST_LDTR_LIMIT:
3919 case GUEST_TR_LIMIT:
3920 case GUEST_GDTR_LIMIT:
3921 case GUEST_IDTR_LIMIT:
3922 case GUEST_ES_AR_BYTES:
3923 case GUEST_DS_AR_BYTES:
3924 case GUEST_FS_AR_BYTES:
3925 case GUEST_GS_AR_BYTES:
3926 case GUEST_LDTR_AR_BYTES:
3927 case GUEST_TR_AR_BYTES:
3928 case GUEST_ES_BASE:
3929 case GUEST_CS_BASE:
3930 case GUEST_SS_BASE:
3931 case GUEST_DS_BASE:
3932 case GUEST_FS_BASE:
3933 case GUEST_GS_BASE:
3934 case GUEST_LDTR_BASE:
3935 case GUEST_TR_BASE:
3936 case GUEST_GDTR_BASE:
3937 case GUEST_IDTR_BASE:
3938 case GUEST_PENDING_DBG_EXCEPTIONS:
3939 case GUEST_BNDCFGS:
3940 return true;
3941 default:
3942 break;
3943 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003944
Sean Christopherson7952d762019-05-07 08:36:29 -07003945 return false;
3946}
3947
3948static void sync_vmcs02_to_vmcs12_rare(struct kvm_vcpu *vcpu,
3949 struct vmcs12 *vmcs12)
3950{
3951 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08003952
3953 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
3954 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
3955 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
3956 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
3957 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
3958 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
3959 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
3960 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
3961 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
3962 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
3963 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
3964 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
3965 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
3966 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
3967 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
3968 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
3969 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
3970 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
3971 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
Sean Christopherson55d23752018-12-03 13:53:18 -08003972 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
3973 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
3974 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
3975 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
3976 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
3977 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
3978 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
3979 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
3980 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
3981 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
3982 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
3983 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
3984 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
3985 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
3986 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
Sean Christopherson7952d762019-05-07 08:36:29 -07003987 vmcs12->guest_pending_dbg_exceptions =
3988 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
3989 if (kvm_mpx_supported())
3990 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
3991
3992 vmx->nested.need_sync_vmcs02_to_vmcs12_rare = false;
3993}
3994
3995static void copy_vmcs02_to_vmcs12_rare(struct kvm_vcpu *vcpu,
3996 struct vmcs12 *vmcs12)
3997{
3998 struct vcpu_vmx *vmx = to_vmx(vcpu);
3999 int cpu;
4000
4001 if (!vmx->nested.need_sync_vmcs02_to_vmcs12_rare)
4002 return;
4003
4004
4005 WARN_ON_ONCE(vmx->loaded_vmcs != &vmx->vmcs01);
4006
4007 cpu = get_cpu();
4008 vmx->loaded_vmcs = &vmx->nested.vmcs02;
Sean Christopherson1af1bb02020-05-06 16:58:50 -07004009 vmx_vcpu_load_vmcs(vcpu, cpu, &vmx->vmcs01);
Sean Christopherson7952d762019-05-07 08:36:29 -07004010
4011 sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
4012
4013 vmx->loaded_vmcs = &vmx->vmcs01;
Sean Christopherson1af1bb02020-05-06 16:58:50 -07004014 vmx_vcpu_load_vmcs(vcpu, cpu, &vmx->nested.vmcs02);
Sean Christopherson7952d762019-05-07 08:36:29 -07004015 put_cpu();
4016}
4017
4018/*
4019 * Update the guest state fields of vmcs12 to reflect changes that
4020 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
4021 * VM-entry controls is also updated, since this is really a guest
4022 * state bit.)
4023 */
4024static void sync_vmcs02_to_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
4025{
4026 struct vcpu_vmx *vmx = to_vmx(vcpu);
4027
4028 if (vmx->nested.hv_evmcs)
4029 sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
4030
4031 vmx->nested.need_sync_vmcs02_to_vmcs12_rare = !vmx->nested.hv_evmcs;
4032
4033 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
4034 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
4035
4036 vmcs12->guest_rsp = kvm_rsp_read(vcpu);
4037 vmcs12->guest_rip = kvm_rip_read(vcpu);
4038 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
4039
4040 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
4041 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
Sean Christopherson55d23752018-12-03 13:53:18 -08004042
4043 vmcs12->guest_interruptibility_info =
4044 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
Sean Christopherson7952d762019-05-07 08:36:29 -07004045
Sean Christopherson55d23752018-12-03 13:53:18 -08004046 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
4047 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
Yadong Qibf0cd882020-11-06 14:51:22 +08004048 else if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED)
4049 vmcs12->guest_activity_state = GUEST_ACTIVITY_WAIT_SIPI;
Sean Christopherson55d23752018-12-03 13:53:18 -08004050 else
4051 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
4052
Paolo Bonzinib4b65b52019-01-29 19:12:35 +01004053 if (nested_cpu_has_preemption_timer(vmcs12) &&
Peter Shier850448f2020-05-26 14:51:06 -07004054 vmcs12->vm_exit_controls & VM_EXIT_SAVE_VMX_PREEMPTION_TIMER &&
4055 !vmx->nested.nested_run_pending)
4056 vmcs12->vmx_preemption_timer_value =
4057 vmx_get_preemption_timer_value(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08004058
4059 /*
4060 * In some cases (usually, nested EPT), L2 is allowed to change its
4061 * own CR3 without exiting. If it has changed it, we must keep it.
4062 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
4063 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
4064 *
4065 * Additionally, restore L2's PDPTR to vmcs12.
4066 */
4067 if (enable_ept) {
4068 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Sean Christophersonc7554efc2019-05-07 09:06:40 -07004069 if (nested_cpu_has_ept(vmcs12) && is_pae_paging(vcpu)) {
4070 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
4071 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
4072 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
4073 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
4074 }
Sean Christopherson55d23752018-12-03 13:53:18 -08004075 }
4076
4077 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
4078
4079 if (nested_cpu_has_vid(vmcs12))
4080 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
4081
4082 vmcs12->vm_entry_controls =
4083 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
4084 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
4085
Sean Christopherson699a1ac2019-05-07 09:06:37 -07004086 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS)
Sean Christopherson55d23752018-12-03 13:53:18 -08004087 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
Sean Christopherson55d23752018-12-03 13:53:18 -08004088
Sean Christopherson55d23752018-12-03 13:53:18 -08004089 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
4090 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Sean Christopherson55d23752018-12-03 13:53:18 -08004091}
4092
4093/*
4094 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
4095 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
4096 * and this function updates it to reflect the changes to the guest state while
4097 * L2 was running (and perhaps made some exits which were handled directly by L0
4098 * without going back to L1), and to reflect the exit reason.
4099 * Note that we do not have to copy here all VMCS fields, just those that
4100 * could have changed by the L2 guest or the exit - i.e., the guest-state and
4101 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
4102 * which already writes to vmcs12 directly.
4103 */
4104static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004105 u32 vm_exit_reason, u32 exit_intr_info,
Sean Christopherson55d23752018-12-03 13:53:18 -08004106 unsigned long exit_qualification)
4107{
Sean Christopherson55d23752018-12-03 13:53:18 -08004108 /* update exit information fields: */
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004109 vmcs12->vm_exit_reason = vm_exit_reason;
Sean Christopherson3c0c2ad2021-04-12 16:21:37 +12004110 if (to_vmx(vcpu)->exit_reason.enclave_mode)
4111 vmcs12->vm_exit_reason |= VMX_EXIT_REASONS_SGX_ENCLAVE_MODE;
Sean Christopherson55d23752018-12-03 13:53:18 -08004112 vmcs12->exit_qualification = exit_qualification;
4113 vmcs12->vm_exit_intr_info = exit_intr_info;
4114
4115 vmcs12->idt_vectoring_info_field = 0;
4116 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
4117 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
4118
4119 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
4120 vmcs12->launch_state = 1;
4121
4122 /* vm_entry_intr_info_field is cleared on exit. Emulate this
4123 * instead of reading the real value. */
4124 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
4125
4126 /*
4127 * Transfer the event that L0 or L1 may wanted to inject into
4128 * L2 to IDT_VECTORING_INFO_FIELD.
4129 */
4130 vmcs12_save_pending_event(vcpu, vmcs12);
Krish Sadhukhana0d4f802018-12-04 19:00:13 -05004131
4132 /*
4133 * According to spec, there's no need to store the guest's
4134 * MSRs if the exit is due to a VM-entry failure that occurs
4135 * during or after loading the guest state. Since this exit
4136 * does not fall in that category, we need to save the MSRs.
4137 */
4138 if (nested_vmx_store_msr(vcpu,
4139 vmcs12->vm_exit_msr_store_addr,
4140 vmcs12->vm_exit_msr_store_count))
4141 nested_vmx_abort(vcpu,
4142 VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Sean Christopherson55d23752018-12-03 13:53:18 -08004143 }
4144
4145 /*
4146 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
4147 * preserved above and would only end up incorrectly in L1.
4148 */
4149 vcpu->arch.nmi_injected = false;
4150 kvm_clear_exception_queue(vcpu);
4151 kvm_clear_interrupt_queue(vcpu);
4152}
4153
4154/*
4155 * A part of what we need to when the nested L2 guest exits and we want to
4156 * run its L1 parent, is to reset L1's guest state to the host state specified
4157 * in vmcs12.
4158 * This function is to be called not only on normal nested exit, but also on
4159 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
4160 * Failures During or After Loading Guest State").
4161 * This function should be called when the active VMCS is L1's (vmcs01).
4162 */
4163static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
4164 struct vmcs12 *vmcs12)
4165{
Sean Christopherson68cda402020-05-11 15:05:29 -07004166 enum vm_entry_failure_code ignored;
Sean Christopherson55d23752018-12-03 13:53:18 -08004167 struct kvm_segment seg;
Sean Christopherson55d23752018-12-03 13:53:18 -08004168
4169 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
4170 vcpu->arch.efer = vmcs12->host_ia32_efer;
4171 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
4172 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
4173 else
4174 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
4175 vmx_set_efer(vcpu, vcpu->arch.efer);
4176
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02004177 kvm_rsp_write(vcpu, vmcs12->host_rsp);
4178 kvm_rip_write(vcpu, vmcs12->host_rip);
Sean Christopherson55d23752018-12-03 13:53:18 -08004179 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
4180 vmx_set_interrupt_shadow(vcpu, 0);
4181
4182 /*
4183 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
4184 * actually changed, because vmx_set_cr0 refers to efer set above.
4185 *
4186 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
4187 * (KVM doesn't change it);
4188 */
Sean Christophersonfa71e952020-07-02 21:04:22 -07004189 vcpu->arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS;
Sean Christopherson55d23752018-12-03 13:53:18 -08004190 vmx_set_cr0(vcpu, vmcs12->host_cr0);
4191
4192 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
4193 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
4194 vmx_set_cr4(vcpu, vmcs12->host_cr4);
4195
4196 nested_ept_uninit_mmu_context(vcpu);
4197
4198 /*
4199 * Only PDPTE load can fail as the value of cr3 was checked on entry and
4200 * couldn't have changed.
4201 */
Sean Christopherson68cda402020-05-11 15:05:29 -07004202 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &ignored))
Sean Christopherson55d23752018-12-03 13:53:18 -08004203 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
4204
Sean Christopherson50b265a2020-03-20 14:28:19 -07004205 nested_vmx_transition_tlb_flush(vcpu, vmcs12, false);
Sean Christopherson55d23752018-12-03 13:53:18 -08004206
4207 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
4208 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
4209 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
4210 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
4211 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
4212 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
4213 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
4214
4215 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
4216 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
4217 vmcs_write64(GUEST_BNDCFGS, 0);
4218
4219 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
4220 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
4221 vcpu->arch.pat = vmcs12->host_ia32_pat;
4222 }
4223 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Oliver Uptond1968422019-12-13 16:33:58 -08004224 WARN_ON_ONCE(kvm_set_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL,
4225 vmcs12->host_ia32_perf_global_ctrl));
Sean Christopherson55d23752018-12-03 13:53:18 -08004226
4227 /* Set L1 segment info according to Intel SDM
4228 27.5.2 Loading Host Segment and Descriptor-Table Registers */
4229 seg = (struct kvm_segment) {
4230 .base = 0,
4231 .limit = 0xFFFFFFFF,
4232 .selector = vmcs12->host_cs_selector,
4233 .type = 11,
4234 .present = 1,
4235 .s = 1,
4236 .g = 1
4237 };
4238 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
4239 seg.l = 1;
4240 else
4241 seg.db = 1;
4242 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
4243 seg = (struct kvm_segment) {
4244 .base = 0,
4245 .limit = 0xFFFFFFFF,
4246 .type = 3,
4247 .present = 1,
4248 .s = 1,
4249 .db = 1,
4250 .g = 1
4251 };
4252 seg.selector = vmcs12->host_ds_selector;
4253 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
4254 seg.selector = vmcs12->host_es_selector;
4255 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
4256 seg.selector = vmcs12->host_ss_selector;
4257 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
4258 seg.selector = vmcs12->host_fs_selector;
4259 seg.base = vmcs12->host_fs_base;
4260 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
4261 seg.selector = vmcs12->host_gs_selector;
4262 seg.base = vmcs12->host_gs_base;
4263 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
4264 seg = (struct kvm_segment) {
4265 .base = vmcs12->host_tr_base,
4266 .limit = 0x67,
4267 .selector = vmcs12->host_tr_selector,
4268 .type = 11,
4269 .present = 1
4270 };
4271 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
4272
4273 kvm_set_dr(vcpu, 7, 0x400);
4274 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
4275
4276 if (cpu_has_vmx_msr_bitmap())
4277 vmx_update_msr_bitmap(vcpu);
4278
4279 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
4280 vmcs12->vm_exit_msr_load_count))
4281 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
4282}
4283
4284static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx)
4285{
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07004286 struct vmx_uret_msr *efer_msr;
Sean Christopherson55d23752018-12-03 13:53:18 -08004287 unsigned int i;
4288
4289 if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER)
4290 return vmcs_read64(GUEST_IA32_EFER);
4291
4292 if (cpu_has_load_ia32_efer())
4293 return host_efer;
4294
4295 for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) {
4296 if (vmx->msr_autoload.guest.val[i].index == MSR_EFER)
4297 return vmx->msr_autoload.guest.val[i].value;
4298 }
4299
Sean Christophersond85a8032020-09-23 11:04:06 -07004300 efer_msr = vmx_find_uret_msr(vmx, MSR_EFER);
Sean Christopherson55d23752018-12-03 13:53:18 -08004301 if (efer_msr)
4302 return efer_msr->data;
4303
4304 return host_efer;
4305}
4306
4307static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu)
4308{
4309 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4310 struct vcpu_vmx *vmx = to_vmx(vcpu);
4311 struct vmx_msr_entry g, h;
Sean Christopherson55d23752018-12-03 13:53:18 -08004312 gpa_t gpa;
4313 u32 i, j;
4314
4315 vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT);
4316
4317 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
4318 /*
4319 * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set
4320 * as vmcs01.GUEST_DR7 contains a userspace defined value
4321 * and vcpu->arch.dr7 is not squirreled away before the
4322 * nested VMENTER (not worth adding a variable in nested_vmx).
4323 */
4324 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
4325 kvm_set_dr(vcpu, 7, DR7_FIXED_1);
4326 else
4327 WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7)));
4328 }
4329
4330 /*
4331 * Note that calling vmx_set_{efer,cr0,cr4} is important as they
4332 * handle a variety of side effects to KVM's software model.
4333 */
4334 vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx));
4335
Sean Christophersonfa71e952020-07-02 21:04:22 -07004336 vcpu->arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS;
Sean Christopherson55d23752018-12-03 13:53:18 -08004337 vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW));
4338
4339 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
4340 vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW));
4341
4342 nested_ept_uninit_mmu_context(vcpu);
Sean Christophersonf087a022019-06-07 11:55:34 -07004343 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07004344 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Sean Christopherson55d23752018-12-03 13:53:18 -08004345
4346 /*
4347 * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs
4348 * from vmcs01 (if necessary). The PDPTRs are not loaded on
4349 * VMFail, like everything else we just need to ensure our
4350 * software model is up-to-date.
4351 */
Sean Christopherson9932b492020-04-15 13:34:50 -07004352 if (enable_ept && is_pae_paging(vcpu))
Sean Christophersonf087a022019-06-07 11:55:34 -07004353 ept_save_pdptrs(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08004354
4355 kvm_mmu_reset_context(vcpu);
4356
4357 if (cpu_has_vmx_msr_bitmap())
4358 vmx_update_msr_bitmap(vcpu);
4359
4360 /*
4361 * This nasty bit of open coding is a compromise between blindly
4362 * loading L1's MSRs using the exit load lists (incorrect emulation
4363 * of VMFail), leaving the nested VM's MSRs in the software model
4364 * (incorrect behavior) and snapshotting the modified MSRs (too
4365 * expensive since the lists are unbound by hardware). For each
4366 * MSR that was (prematurely) loaded from the nested VMEntry load
4367 * list, reload it from the exit load list if it exists and differs
4368 * from the guest value. The intent is to stuff host state as
4369 * silently as possible, not to fully process the exit load list.
4370 */
Sean Christopherson55d23752018-12-03 13:53:18 -08004371 for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) {
4372 gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g));
4373 if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) {
4374 pr_debug_ratelimited(
4375 "%s read MSR index failed (%u, 0x%08llx)\n",
4376 __func__, i, gpa);
4377 goto vmabort;
4378 }
4379
4380 for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) {
4381 gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h));
4382 if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) {
4383 pr_debug_ratelimited(
4384 "%s read MSR failed (%u, 0x%08llx)\n",
4385 __func__, j, gpa);
4386 goto vmabort;
4387 }
4388 if (h.index != g.index)
4389 continue;
4390 if (h.value == g.value)
4391 break;
4392
4393 if (nested_vmx_load_msr_check(vcpu, &h)) {
4394 pr_debug_ratelimited(
4395 "%s check failed (%u, 0x%x, 0x%x)\n",
4396 __func__, j, h.index, h.reserved);
4397 goto vmabort;
4398 }
4399
Sean Christophersonf20935d2019-09-05 14:22:54 -07004400 if (kvm_set_msr(vcpu, h.index, h.value)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08004401 pr_debug_ratelimited(
4402 "%s WRMSR failed (%u, 0x%x, 0x%llx)\n",
4403 __func__, j, h.index, h.value);
4404 goto vmabort;
4405 }
4406 }
4407 }
4408
4409 return;
4410
4411vmabort:
4412 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
4413}
4414
4415/*
4416 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
4417 * and modify vmcs12 to make it see what it would expect to see there if
4418 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
4419 */
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004420void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason,
Sean Christopherson55d23752018-12-03 13:53:18 -08004421 u32 exit_intr_info, unsigned long exit_qualification)
4422{
4423 struct vcpu_vmx *vmx = to_vmx(vcpu);
4424 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4425
4426 /* trying to cancel vmlaunch/vmresume is a bug */
4427 WARN_ON_ONCE(vmx->nested.nested_run_pending);
4428
Sean Christophersoncb6a32c2021-03-02 09:45:14 -08004429 /* Similarly, triple faults in L2 should never escape. */
4430 WARN_ON_ONCE(kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu));
4431
Maxim Levitskyf2c7ef32021-01-07 11:38:51 +02004432 kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
4433
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07004434 /* Service the TLB flush request for L2 before switching to L1. */
4435 if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu))
4436 kvm_vcpu_flush_tlb_current(vcpu);
4437
Peter Shier43fea4e2020-08-20 16:05:45 -07004438 /*
4439 * VCPU_EXREG_PDPTR will be clobbered in arch/x86/kvm/vmx/vmx.h between
4440 * now and the new vmentry. Ensure that the VMCS02 PDPTR fields are
4441 * up-to-date before switching to L1.
4442 */
4443 if (enable_ept && is_pae_paging(vcpu))
4444 vmx_ept_load_pdptrs(vcpu);
4445
Sean Christopherson55d23752018-12-03 13:53:18 -08004446 leave_guest_mode(vcpu);
4447
Paolo Bonzinib4b65b52019-01-29 19:12:35 +01004448 if (nested_cpu_has_preemption_timer(vmcs12))
4449 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
4450
Xiaoyao Li5e3d3942019-12-06 16:45:26 +08004451 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING)
Sean Christopherson55d23752018-12-03 13:53:18 -08004452 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
4453
4454 if (likely(!vmx->fail)) {
Sean Christopherson3731905ef2019-05-07 08:36:27 -07004455 sync_vmcs02_to_vmcs12(vcpu, vmcs12);
Sean Christophersonf4f83162019-05-07 08:36:26 -07004456
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004457 if (vm_exit_reason != -1)
4458 prepare_vmcs12(vcpu, vmcs12, vm_exit_reason,
4459 exit_intr_info, exit_qualification);
Sean Christopherson55d23752018-12-03 13:53:18 -08004460
4461 /*
Sean Christopherson3731905ef2019-05-07 08:36:27 -07004462 * Must happen outside of sync_vmcs02_to_vmcs12() as it will
Sean Christopherson55d23752018-12-03 13:53:18 -08004463 * also be used to capture vmcs12 cache as part of
4464 * capturing nVMX state for snapshot (migration).
4465 *
4466 * Otherwise, this flush will dirty guest memory at a
4467 * point it is already assumed by user-space to be
4468 * immutable.
4469 */
4470 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08004471 } else {
4472 /*
4473 * The only expected VM-instruction error is "VM entry with
4474 * invalid control field(s)." Anything else indicates a
4475 * problem with L0. And we should never get here with a
4476 * VMFail of any type if early consistency checks are enabled.
4477 */
4478 WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) !=
4479 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
4480 WARN_ON_ONCE(nested_early_check);
4481 }
4482
4483 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
4484
4485 /* Update any VMCS fields that might have changed while L2 ran */
4486 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
4487 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
4488 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Liran Alon02d496cf2019-11-11 14:30:55 +02004489 if (vmx->nested.l1_tpr_threshold != -1)
4490 vmcs_write32(TPR_THRESHOLD, vmx->nested.l1_tpr_threshold);
Sean Christopherson55d23752018-12-03 13:53:18 -08004491
4492 if (kvm_has_tsc_control)
4493 decache_tsc_multiplier(vmx);
4494
4495 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
4496 vmx->nested.change_vmcs01_virtual_apic_mode = false;
4497 vmx_set_virtual_apic_mode(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08004498 }
4499
Makarand Sonarea85863c2021-02-12 16:50:12 -08004500 if (vmx->nested.update_vmcs01_cpu_dirty_logging) {
4501 vmx->nested.update_vmcs01_cpu_dirty_logging = false;
4502 vmx_update_cpu_dirty_logging(vcpu);
4503 }
4504
Sean Christopherson55d23752018-12-03 13:53:18 -08004505 /* Unpin physical memory we referred to in vmcs02 */
4506 if (vmx->nested.apic_access_page) {
Liran Alonb11494b2019-11-21 00:31:47 +02004507 kvm_release_page_clean(vmx->nested.apic_access_page);
Sean Christopherson55d23752018-12-03 13:53:18 -08004508 vmx->nested.apic_access_page = NULL;
4509 }
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01004510 kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map, true);
KarimAllah Ahmed3278e042019-01-31 21:24:38 +01004511 kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map, true);
4512 vmx->nested.pi_desc = NULL;
Sean Christopherson55d23752018-12-03 13:53:18 -08004513
Sean Christopherson1196cb92020-03-20 14:28:23 -07004514 if (vmx->nested.reload_vmcs01_apic_access_page) {
4515 vmx->nested.reload_vmcs01_apic_access_page = false;
4516 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
4517 }
Sean Christopherson55d23752018-12-03 13:53:18 -08004518
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004519 if ((vm_exit_reason != -1) &&
4520 (enable_shadow_vmcs || vmx->nested.hv_evmcs))
Sean Christopherson3731905ef2019-05-07 08:36:27 -07004521 vmx->nested.need_vmcs12_to_shadow_sync = true;
Sean Christopherson55d23752018-12-03 13:53:18 -08004522
4523 /* in case we halted in L2 */
4524 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
4525
4526 if (likely(!vmx->fail)) {
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004527 if ((u16)vm_exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
Sean Christophersona1c77ab2020-03-02 22:27:35 -08004528 nested_exit_intr_ack_set(vcpu)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08004529 int irq = kvm_cpu_get_interrupt(vcpu);
4530 WARN_ON(irq < 0);
4531 vmcs12->vm_exit_intr_info = irq |
4532 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
4533 }
4534
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004535 if (vm_exit_reason != -1)
Sean Christopherson55d23752018-12-03 13:53:18 -08004536 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
4537 vmcs12->exit_qualification,
4538 vmcs12->idt_vectoring_info_field,
4539 vmcs12->vm_exit_intr_info,
4540 vmcs12->vm_exit_intr_error_code,
4541 KVM_ISA_VMX);
4542
4543 load_vmcs12_host_state(vcpu, vmcs12);
4544
4545 return;
4546 }
4547
4548 /*
4549 * After an early L2 VM-entry failure, we're now back
4550 * in L1 which thinks it just finished a VMLAUNCH or
4551 * VMRESUME instruction, so we need to set the failure
4552 * flag and the VM-instruction error field of the VMCS
4553 * accordingly, and skip the emulated instruction.
4554 */
Sean Christophersonb2656e42020-06-08 18:56:07 -07004555 (void)nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Sean Christopherson55d23752018-12-03 13:53:18 -08004556
4557 /*
4558 * Restore L1's host state to KVM's software model. We're here
4559 * because a consistency check was caught by hardware, which
4560 * means some amount of guest state has been propagated to KVM's
4561 * model and needs to be unwound to the host's state.
4562 */
4563 nested_vmx_restore_host_state(vcpu);
4564
4565 vmx->fail = 0;
4566}
4567
Sean Christophersoncb6a32c2021-03-02 09:45:14 -08004568static void nested_vmx_triple_fault(struct kvm_vcpu *vcpu)
4569{
4570 nested_vmx_vmexit(vcpu, EXIT_REASON_TRIPLE_FAULT, 0, 0);
4571}
4572
Sean Christopherson55d23752018-12-03 13:53:18 -08004573/*
4574 * Decode the memory-address operand of a vmx instruction, as recorded on an
4575 * exit caused by such an instruction (run by a guest hypervisor).
4576 * On success, returns 0. When the operand is invalid, returns 1 and throws
Miaohe Lin49f933d2020-02-27 11:20:54 +08004577 * #UD, #GP, or #SS.
Sean Christopherson55d23752018-12-03 13:53:18 -08004578 */
4579int get_vmx_mem_address(struct kvm_vcpu *vcpu, unsigned long exit_qualification,
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004580 u32 vmx_instruction_info, bool wr, int len, gva_t *ret)
Sean Christopherson55d23752018-12-03 13:53:18 -08004581{
4582 gva_t off;
4583 bool exn;
4584 struct kvm_segment s;
4585
4586 /*
4587 * According to Vol. 3B, "Information for VM Exits Due to Instruction
4588 * Execution", on an exit, vmx_instruction_info holds most of the
4589 * addressing components of the operand. Only the displacement part
4590 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
4591 * For how an actual address is calculated from all these components,
4592 * refer to Vol. 1, "Operand Addressing".
4593 */
4594 int scaling = vmx_instruction_info & 3;
4595 int addr_size = (vmx_instruction_info >> 7) & 7;
4596 bool is_reg = vmx_instruction_info & (1u << 10);
4597 int seg_reg = (vmx_instruction_info >> 15) & 7;
4598 int index_reg = (vmx_instruction_info >> 18) & 0xf;
4599 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
4600 int base_reg = (vmx_instruction_info >> 23) & 0xf;
4601 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
4602
4603 if (is_reg) {
4604 kvm_queue_exception(vcpu, UD_VECTOR);
4605 return 1;
4606 }
4607
4608 /* Addr = segment_base + offset */
4609 /* offset = base + [index * scale] + displacement */
4610 off = exit_qualification; /* holds the displacement */
Sean Christopherson946c5222019-01-23 14:39:23 -08004611 if (addr_size == 1)
4612 off = (gva_t)sign_extend64(off, 31);
4613 else if (addr_size == 0)
4614 off = (gva_t)sign_extend64(off, 15);
Sean Christopherson55d23752018-12-03 13:53:18 -08004615 if (base_is_valid)
4616 off += kvm_register_read(vcpu, base_reg);
4617 if (index_is_valid)
Miaohe Line6302692020-02-15 10:44:22 +08004618 off += kvm_register_read(vcpu, index_reg) << scaling;
Sean Christopherson55d23752018-12-03 13:53:18 -08004619 vmx_get_segment(vcpu, &s, seg_reg);
Sean Christopherson55d23752018-12-03 13:53:18 -08004620
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004621 /*
4622 * The effective address, i.e. @off, of a memory operand is truncated
4623 * based on the address size of the instruction. Note that this is
4624 * the *effective address*, i.e. the address prior to accounting for
4625 * the segment's base.
4626 */
Sean Christopherson55d23752018-12-03 13:53:18 -08004627 if (addr_size == 1) /* 32 bit */
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004628 off &= 0xffffffff;
4629 else if (addr_size == 0) /* 16 bit */
4630 off &= 0xffff;
Sean Christopherson55d23752018-12-03 13:53:18 -08004631
4632 /* Checks for #GP/#SS exceptions. */
4633 exn = false;
4634 if (is_long_mode(vcpu)) {
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004635 /*
4636 * The virtual/linear address is never truncated in 64-bit
4637 * mode, e.g. a 32-bit address size can yield a 64-bit virtual
4638 * address when using FS/GS with a non-zero base.
4639 */
Liran Alon6694e482019-07-15 18:47:44 +03004640 if (seg_reg == VCPU_SREG_FS || seg_reg == VCPU_SREG_GS)
4641 *ret = s.base + off;
4642 else
4643 *ret = off;
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004644
Sean Christopherson55d23752018-12-03 13:53:18 -08004645 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
4646 * non-canonical form. This is the only check on the memory
4647 * destination for long mode!
4648 */
4649 exn = is_noncanonical_address(*ret, vcpu);
Paolo Bonzinie0dfacb2019-01-30 17:25:38 +01004650 } else {
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004651 /*
4652 * When not in long mode, the virtual/linear address is
4653 * unconditionally truncated to 32 bits regardless of the
4654 * address size.
4655 */
4656 *ret = (s.base + off) & 0xffffffff;
4657
Sean Christopherson55d23752018-12-03 13:53:18 -08004658 /* Protected mode: apply checks for segment validity in the
4659 * following order:
4660 * - segment type check (#GP(0) may be thrown)
4661 * - usability check (#GP(0)/#SS(0))
4662 * - limit check (#GP(0)/#SS(0))
4663 */
4664 if (wr)
4665 /* #GP(0) if the destination operand is located in a
4666 * read-only data segment or any code segment.
4667 */
4668 exn = ((s.type & 0xa) == 0 || (s.type & 8));
4669 else
4670 /* #GP(0) if the source operand is located in an
4671 * execute-only code segment
4672 */
4673 exn = ((s.type & 0xa) == 8);
4674 if (exn) {
4675 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
4676 return 1;
4677 }
4678 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
4679 */
4680 exn = (s.unusable != 0);
Sean Christopherson34333cc2019-01-23 14:39:25 -08004681
4682 /*
4683 * Protected mode: #GP(0)/#SS(0) if the memory operand is
4684 * outside the segment limit. All CPUs that support VMX ignore
4685 * limit checks for flat segments, i.e. segments with base==0,
4686 * limit==0xffffffff and of type expand-up data or code.
Sean Christopherson55d23752018-12-03 13:53:18 -08004687 */
Sean Christopherson34333cc2019-01-23 14:39:25 -08004688 if (!(s.base == 0 && s.limit == 0xffffffff &&
4689 ((s.type & 8) || !(s.type & 4))))
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004690 exn = exn || ((u64)off + len - 1 > s.limit);
Sean Christopherson55d23752018-12-03 13:53:18 -08004691 }
4692 if (exn) {
4693 kvm_queue_exception_e(vcpu,
4694 seg_reg == VCPU_SREG_SS ?
4695 SS_VECTOR : GP_VECTOR,
4696 0);
4697 return 1;
4698 }
4699
4700 return 0;
4701}
4702
Oliver Upton03a8871a2019-11-13 16:17:20 -08004703void nested_vmx_pmu_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
4704{
4705 struct vcpu_vmx *vmx;
4706
4707 if (!nested_vmx_allowed(vcpu))
4708 return;
4709
4710 vmx = to_vmx(vcpu);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004711 if (kvm_x86_ops.pmu_ops->is_valid_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL)) {
Oliver Upton03a8871a2019-11-13 16:17:20 -08004712 vmx->nested.msrs.entry_ctls_high |=
4713 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
4714 vmx->nested.msrs.exit_ctls_high |=
4715 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
4716 } else {
4717 vmx->nested.msrs.entry_ctls_high &=
4718 ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
4719 vmx->nested.msrs.exit_ctls_high &=
Chenyi Qiangc6b177a2020-08-28 16:56:21 +08004720 ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
Oliver Upton03a8871a2019-11-13 16:17:20 -08004721 }
4722}
4723
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004724static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer,
4725 int *ret)
Sean Christopherson55d23752018-12-03 13:53:18 -08004726{
4727 gva_t gva;
4728 struct x86_exception e;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004729 int r;
Sean Christopherson55d23752018-12-03 13:53:18 -08004730
Sean Christopherson5addc232020-04-15 13:34:53 -07004731 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004732 vmcs_read32(VMX_INSTRUCTION_INFO), false,
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004733 sizeof(*vmpointer), &gva)) {
4734 *ret = 1;
4735 return -EINVAL;
4736 }
Sean Christopherson55d23752018-12-03 13:53:18 -08004737
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004738 r = kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e);
4739 if (r != X86EMUL_CONTINUE) {
Babu Moger3f3393b2020-09-11 14:29:05 -05004740 *ret = kvm_handle_memory_failure(vcpu, r, &e);
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004741 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08004742 }
4743
4744 return 0;
4745}
4746
4747/*
4748 * Allocate a shadow VMCS and associate it with the currently loaded
4749 * VMCS, unless such a shadow VMCS already exists. The newly allocated
4750 * VMCS is also VMCLEARed, so that it is ready for use.
4751 */
4752static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
4753{
4754 struct vcpu_vmx *vmx = to_vmx(vcpu);
4755 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
4756
4757 /*
4758 * We should allocate a shadow vmcs for vmcs01 only when L1
4759 * executes VMXON and free it when L1 executes VMXOFF.
4760 * As it is invalid to execute VMXON twice, we shouldn't reach
4761 * here when vmcs01 already have an allocated shadow vmcs.
4762 */
4763 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
4764
4765 if (!loaded_vmcs->shadow_vmcs) {
4766 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
4767 if (loaded_vmcs->shadow_vmcs)
4768 vmcs_clear(loaded_vmcs->shadow_vmcs);
4769 }
4770 return loaded_vmcs->shadow_vmcs;
4771}
4772
4773static int enter_vmx_operation(struct kvm_vcpu *vcpu)
4774{
4775 struct vcpu_vmx *vmx = to_vmx(vcpu);
4776 int r;
4777
4778 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
4779 if (r < 0)
4780 goto out_vmcs02;
4781
Ben Gardon41836832019-02-11 11:02:52 -08004782 vmx->nested.cached_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT);
Sean Christopherson55d23752018-12-03 13:53:18 -08004783 if (!vmx->nested.cached_vmcs12)
4784 goto out_cached_vmcs12;
4785
Ben Gardon41836832019-02-11 11:02:52 -08004786 vmx->nested.cached_shadow_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT);
Sean Christopherson55d23752018-12-03 13:53:18 -08004787 if (!vmx->nested.cached_shadow_vmcs12)
4788 goto out_cached_shadow_vmcs12;
4789
4790 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
4791 goto out_shadow_vmcs;
4792
4793 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
Jim Mattsonada00982020-05-08 13:36:42 -07004794 HRTIMER_MODE_ABS_PINNED);
Sean Christopherson55d23752018-12-03 13:53:18 -08004795 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
4796
4797 vmx->nested.vpid02 = allocate_vpid();
4798
4799 vmx->nested.vmcs02_initialized = false;
4800 vmx->nested.vmxon = true;
Luwei Kangee85dec2018-10-24 16:05:16 +08004801
Sean Christopherson2ef76192020-03-02 15:56:22 -08004802 if (vmx_pt_mode_is_host_guest()) {
Luwei Kangee85dec2018-10-24 16:05:16 +08004803 vmx->pt_desc.guest.ctl = 0;
Aaron Lewis476c9bd2020-09-25 16:34:18 +02004804 pt_update_intercept_for_msr(vcpu);
Luwei Kangee85dec2018-10-24 16:05:16 +08004805 }
4806
Sean Christopherson55d23752018-12-03 13:53:18 -08004807 return 0;
4808
4809out_shadow_vmcs:
4810 kfree(vmx->nested.cached_shadow_vmcs12);
4811
4812out_cached_shadow_vmcs12:
4813 kfree(vmx->nested.cached_vmcs12);
4814
4815out_cached_vmcs12:
4816 free_loaded_vmcs(&vmx->nested.vmcs02);
4817
4818out_vmcs02:
4819 return -ENOMEM;
4820}
4821
4822/*
4823 * Emulate the VMXON instruction.
4824 * Currently, we just remember that VMX is active, and do not save or even
4825 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
4826 * do not currently need to store anything in that guest-allocated memory
4827 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
4828 * argument is different from the VMXON pointer (which the spec says they do).
4829 */
4830static int handle_vmon(struct kvm_vcpu *vcpu)
4831{
4832 int ret;
4833 gpa_t vmptr;
KarimAllah Ahmed2e408932019-01-31 21:24:31 +01004834 uint32_t revision;
Sean Christopherson55d23752018-12-03 13:53:18 -08004835 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson32ad73d2019-12-20 20:44:55 -08004836 const u64 VMXON_NEEDED_FEATURES = FEAT_CTL_LOCKED
4837 | FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX;
Sean Christopherson55d23752018-12-03 13:53:18 -08004838
4839 /*
4840 * The Intel VMX Instruction Reference lists a bunch of bits that are
4841 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
Sean Christophersonc2fe3cd2020-10-06 18:44:15 -07004842 * 1 (see vmx_is_valid_cr4() for when we allow the guest to set this).
Sean Christopherson55d23752018-12-03 13:53:18 -08004843 * Otherwise, we should fail with #UD. But most faulting conditions
4844 * have already been checked by hardware, prior to the VM-exit for
4845 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
4846 * that bit set to 1 in non-root mode.
4847 */
4848 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
4849 kvm_queue_exception(vcpu, UD_VECTOR);
4850 return 1;
4851 }
4852
4853 /* CPL=0 must be checked manually. */
4854 if (vmx_get_cpl(vcpu)) {
4855 kvm_inject_gp(vcpu, 0);
4856 return 1;
4857 }
4858
4859 if (vmx->nested.vmxon)
Sean Christophersonb2656e42020-06-08 18:56:07 -07004860 return nested_vmx_fail(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Sean Christopherson55d23752018-12-03 13:53:18 -08004861
4862 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
4863 != VMXON_NEEDED_FEATURES) {
4864 kvm_inject_gp(vcpu, 0);
4865 return 1;
4866 }
4867
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004868 if (nested_vmx_get_vmptr(vcpu, &vmptr, &ret))
4869 return ret;
Sean Christopherson55d23752018-12-03 13:53:18 -08004870
4871 /*
4872 * SDM 3: 24.11.5
4873 * The first 4 bytes of VMXON region contain the supported
4874 * VMCS revision identifier
4875 *
4876 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
4877 * which replaces physical address width with 32
4878 */
KarimAllah Ahmede0bf2662019-01-31 21:24:43 +01004879 if (!page_address_valid(vcpu, vmptr))
Sean Christopherson55d23752018-12-03 13:53:18 -08004880 return nested_vmx_failInvalid(vcpu);
4881
KarimAllah Ahmed2e408932019-01-31 21:24:31 +01004882 if (kvm_read_guest(vcpu->kvm, vmptr, &revision, sizeof(revision)) ||
4883 revision != VMCS12_REVISION)
Sean Christopherson55d23752018-12-03 13:53:18 -08004884 return nested_vmx_failInvalid(vcpu);
4885
Sean Christopherson55d23752018-12-03 13:53:18 -08004886 vmx->nested.vmxon_ptr = vmptr;
4887 ret = enter_vmx_operation(vcpu);
4888 if (ret)
4889 return ret;
4890
4891 return nested_vmx_succeed(vcpu);
4892}
4893
4894static inline void nested_release_vmcs12(struct kvm_vcpu *vcpu)
4895{
4896 struct vcpu_vmx *vmx = to_vmx(vcpu);
4897
4898 if (vmx->nested.current_vmptr == -1ull)
4899 return;
4900
Sean Christopherson7952d762019-05-07 08:36:29 -07004901 copy_vmcs02_to_vmcs12_rare(vcpu, get_vmcs12(vcpu));
4902
Sean Christopherson55d23752018-12-03 13:53:18 -08004903 if (enable_shadow_vmcs) {
4904 /* copy to memory all shadowed fields in case
4905 they were modified */
4906 copy_shadow_to_vmcs12(vmx);
Sean Christopherson55d23752018-12-03 13:53:18 -08004907 vmx_disable_shadow_vmcs(vmx);
4908 }
4909 vmx->nested.posted_intr_nv = -1;
4910
4911 /* Flush VMCS12 to guest memory */
4912 kvm_vcpu_write_guest_page(vcpu,
4913 vmx->nested.current_vmptr >> PAGE_SHIFT,
4914 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
4915
4916 kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
4917
4918 vmx->nested.current_vmptr = -1ull;
4919}
4920
4921/* Emulate the VMXOFF instruction */
4922static int handle_vmoff(struct kvm_vcpu *vcpu)
4923{
4924 if (!nested_vmx_check_permission(vcpu))
4925 return 1;
Liran Alon4b9852f2019-08-26 13:24:49 +03004926
Sean Christopherson55d23752018-12-03 13:53:18 -08004927 free_nested(vcpu);
Liran Alon4b9852f2019-08-26 13:24:49 +03004928
4929 /* Process a latched INIT during time CPU was in VMX operation */
4930 kvm_make_request(KVM_REQ_EVENT, vcpu);
4931
Sean Christopherson55d23752018-12-03 13:53:18 -08004932 return nested_vmx_succeed(vcpu);
4933}
4934
4935/* Emulate the VMCLEAR instruction */
4936static int handle_vmclear(struct kvm_vcpu *vcpu)
4937{
4938 struct vcpu_vmx *vmx = to_vmx(vcpu);
4939 u32 zero = 0;
4940 gpa_t vmptr;
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02004941 u64 evmcs_gpa;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004942 int r;
Sean Christopherson55d23752018-12-03 13:53:18 -08004943
4944 if (!nested_vmx_check_permission(vcpu))
4945 return 1;
4946
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004947 if (nested_vmx_get_vmptr(vcpu, &vmptr, &r))
4948 return r;
Sean Christopherson55d23752018-12-03 13:53:18 -08004949
KarimAllah Ahmede0bf2662019-01-31 21:24:43 +01004950 if (!page_address_valid(vcpu, vmptr))
Sean Christophersonb2656e42020-06-08 18:56:07 -07004951 return nested_vmx_fail(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
Sean Christopherson55d23752018-12-03 13:53:18 -08004952
4953 if (vmptr == vmx->nested.vmxon_ptr)
Sean Christophersonb2656e42020-06-08 18:56:07 -07004954 return nested_vmx_fail(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
Sean Christopherson55d23752018-12-03 13:53:18 -08004955
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02004956 /*
4957 * When Enlightened VMEntry is enabled on the calling CPU we treat
4958 * memory area pointer by vmptr as Enlightened VMCS (as there's no good
4959 * way to distinguish it from VMCS12) and we must not corrupt it by
4960 * writing to the non-existent 'launch_state' field. The area doesn't
4961 * have to be the currently active EVMCS on the calling CPU and there's
4962 * nothing KVM has to do to transition it from 'active' to 'non-active'
4963 * state. It is possible that the area will stay mapped as
4964 * vmx->nested.hv_evmcs but this shouldn't be a problem.
4965 */
4966 if (likely(!vmx->nested.enlightened_vmcs_enabled ||
4967 !nested_enlightened_vmentry(vcpu, &evmcs_gpa))) {
Sean Christopherson55d23752018-12-03 13:53:18 -08004968 if (vmptr == vmx->nested.current_vmptr)
4969 nested_release_vmcs12(vcpu);
4970
4971 kvm_vcpu_write_guest(vcpu,
4972 vmptr + offsetof(struct vmcs12,
4973 launch_state),
4974 &zero, sizeof(zero));
4975 }
4976
4977 return nested_vmx_succeed(vcpu);
4978}
4979
Sean Christopherson55d23752018-12-03 13:53:18 -08004980/* Emulate the VMLAUNCH instruction */
4981static int handle_vmlaunch(struct kvm_vcpu *vcpu)
4982{
4983 return nested_vmx_run(vcpu, true);
4984}
4985
4986/* Emulate the VMRESUME instruction */
4987static int handle_vmresume(struct kvm_vcpu *vcpu)
4988{
4989
4990 return nested_vmx_run(vcpu, false);
4991}
4992
4993static int handle_vmread(struct kvm_vcpu *vcpu)
4994{
Jim Mattsondd2d6042019-12-06 15:46:35 -08004995 struct vmcs12 *vmcs12 = is_guest_mode(vcpu) ? get_shadow_vmcs12(vcpu)
4996 : get_vmcs12(vcpu);
Sean Christopherson5addc232020-04-15 13:34:53 -07004997 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Jim Mattsonc90f4d02019-12-06 15:46:37 -08004998 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
4999 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzinif7eea632019-09-14 00:26:27 +02005000 struct x86_exception e;
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005001 unsigned long field;
5002 u64 value;
5003 gva_t gva = 0;
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07005004 short offset;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005005 int len, r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005006
5007 if (!nested_vmx_check_permission(vcpu))
5008 return 1;
5009
Jim Mattsondd2d6042019-12-06 15:46:35 -08005010 /*
5011 * In VMX non-root operation, when the VMCS-link pointer is -1ull,
5012 * any VMREAD sets the ALU flags for VMfailInvalid.
5013 */
5014 if (vmx->nested.current_vmptr == -1ull ||
5015 (is_guest_mode(vcpu) &&
5016 get_vmcs12(vcpu)->vmcs_link_pointer == -1ull))
Sean Christopherson55d23752018-12-03 13:53:18 -08005017 return nested_vmx_failInvalid(vcpu);
5018
Sean Christopherson55d23752018-12-03 13:53:18 -08005019 /* Decode instruction info and find the field to read */
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005020 field = kvm_register_readl(vcpu, (((instr_info) >> 28) & 0xf));
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07005021
5022 offset = vmcs_field_to_offset(field);
5023 if (offset < 0)
Sean Christophersonb2656e42020-06-08 18:56:07 -07005024 return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Sean Christopherson55d23752018-12-03 13:53:18 -08005025
Sean Christopherson7952d762019-05-07 08:36:29 -07005026 if (!is_guest_mode(vcpu) && is_vmcs12_ext_field(field))
5027 copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
5028
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005029 /* Read the field, zero-extended to a u64 value */
5030 value = vmcs12_read_any(vmcs12, field, offset);
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07005031
Sean Christopherson55d23752018-12-03 13:53:18 -08005032 /*
5033 * Now copy part of this value to register or memory, as requested.
5034 * Note that the number of bits actually copied is 32 or 64 depending
5035 * on the guest's mode (32 or 64 bit), not on the given field's length.
5036 */
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005037 if (instr_info & BIT(10)) {
5038 kvm_register_writel(vcpu, (((instr_info) >> 3) & 0xf), value);
Sean Christopherson55d23752018-12-03 13:53:18 -08005039 } else {
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005040 len = is_64_bit_mode(vcpu) ? 8 : 4;
Sean Christopherson55d23752018-12-03 13:53:18 -08005041 if (get_vmx_mem_address(vcpu, exit_qualification,
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005042 instr_info, true, len, &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08005043 return 1;
5044 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005045 r = kvm_write_guest_virt_system(vcpu, gva, &value, len, &e);
5046 if (r != X86EMUL_CONTINUE)
Babu Moger3f3393b2020-09-11 14:29:05 -05005047 return kvm_handle_memory_failure(vcpu, r, &e);
Sean Christopherson55d23752018-12-03 13:53:18 -08005048 }
5049
5050 return nested_vmx_succeed(vcpu);
5051}
5052
Sean Christophersone2174292019-05-07 08:36:28 -07005053static bool is_shadow_field_rw(unsigned long field)
5054{
5055 switch (field) {
5056#define SHADOW_FIELD_RW(x, y) case x:
5057#include "vmcs_shadow_fields.h"
5058 return true;
5059 default:
5060 break;
5061 }
5062 return false;
5063}
5064
5065static bool is_shadow_field_ro(unsigned long field)
5066{
5067 switch (field) {
5068#define SHADOW_FIELD_RO(x, y) case x:
5069#include "vmcs_shadow_fields.h"
5070 return true;
5071 default:
5072 break;
5073 }
5074 return false;
5075}
Sean Christopherson55d23752018-12-03 13:53:18 -08005076
5077static int handle_vmwrite(struct kvm_vcpu *vcpu)
5078{
Jim Mattsondd2d6042019-12-06 15:46:35 -08005079 struct vmcs12 *vmcs12 = is_guest_mode(vcpu) ? get_shadow_vmcs12(vcpu)
5080 : get_vmcs12(vcpu);
Sean Christopherson5addc232020-04-15 13:34:53 -07005081 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005082 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5083 struct vcpu_vmx *vmx = to_vmx(vcpu);
5084 struct x86_exception e;
5085 unsigned long field;
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07005086 short offset;
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005087 gva_t gva;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005088 int len, r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005089
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005090 /*
5091 * The value to write might be 32 or 64 bits, depending on L1's long
Sean Christopherson55d23752018-12-03 13:53:18 -08005092 * mode, and eventually we need to write that into a field of several
5093 * possible lengths. The code below first zero-extends the value to 64
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005094 * bit (value), and then copies only the appropriate number of
Sean Christopherson55d23752018-12-03 13:53:18 -08005095 * bits into the vmcs12 field.
5096 */
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005097 u64 value = 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08005098
5099 if (!nested_vmx_check_permission(vcpu))
5100 return 1;
5101
Jim Mattsondd2d6042019-12-06 15:46:35 -08005102 /*
5103 * In VMX non-root operation, when the VMCS-link pointer is -1ull,
5104 * any VMWRITE sets the ALU flags for VMfailInvalid.
5105 */
5106 if (vmx->nested.current_vmptr == -1ull ||
5107 (is_guest_mode(vcpu) &&
5108 get_vmcs12(vcpu)->vmcs_link_pointer == -1ull))
Sean Christopherson55d23752018-12-03 13:53:18 -08005109 return nested_vmx_failInvalid(vcpu);
5110
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005111 if (instr_info & BIT(10))
5112 value = kvm_register_readl(vcpu, (((instr_info) >> 3) & 0xf));
Sean Christopherson55d23752018-12-03 13:53:18 -08005113 else {
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005114 len = is_64_bit_mode(vcpu) ? 8 : 4;
Sean Christopherson55d23752018-12-03 13:53:18 -08005115 if (get_vmx_mem_address(vcpu, exit_qualification,
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005116 instr_info, false, len, &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08005117 return 1;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005118 r = kvm_read_guest_virt(vcpu, gva, &value, len, &e);
5119 if (r != X86EMUL_CONTINUE)
Babu Moger3f3393b2020-09-11 14:29:05 -05005120 return kvm_handle_memory_failure(vcpu, r, &e);
Sean Christopherson55d23752018-12-03 13:53:18 -08005121 }
5122
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005123 field = kvm_register_readl(vcpu, (((instr_info) >> 28) & 0xf));
Sean Christopherson55d23752018-12-03 13:53:18 -08005124
Jim Mattson693e02c2019-12-06 15:46:36 -08005125 offset = vmcs_field_to_offset(field);
5126 if (offset < 0)
Sean Christophersonb2656e42020-06-08 18:56:07 -07005127 return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Jim Mattson693e02c2019-12-06 15:46:36 -08005128
Sean Christopherson55d23752018-12-03 13:53:18 -08005129 /*
5130 * If the vCPU supports "VMWRITE to any supported field in the
5131 * VMCS," then the "read-only" fields are actually read/write.
5132 */
5133 if (vmcs_field_readonly(field) &&
5134 !nested_cpu_has_vmwrite_any_field(vcpu))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005135 return nested_vmx_fail(vcpu, VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Sean Christopherson55d23752018-12-03 13:53:18 -08005136
Jim Mattsondd2d6042019-12-06 15:46:35 -08005137 /*
5138 * Ensure vmcs12 is up-to-date before any VMWRITE that dirties
5139 * vmcs12, else we may crush a field or consume a stale value.
5140 */
5141 if (!is_guest_mode(vcpu) && !is_shadow_field_rw(field))
5142 copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08005143
5144 /*
Sean Christophersonb6437802019-05-07 08:36:24 -07005145 * Some Intel CPUs intentionally drop the reserved bits of the AR byte
5146 * fields on VMWRITE. Emulate this behavior to ensure consistent KVM
5147 * behavior regardless of the underlying hardware, e.g. if an AR_BYTE
5148 * field is intercepted for VMWRITE but not VMREAD (in L1), then VMREAD
5149 * from L1 will return a different value than VMREAD from L2 (L1 sees
5150 * the stripped down value, L2 sees the full value as stored by KVM).
Sean Christopherson55d23752018-12-03 13:53:18 -08005151 */
Sean Christophersonb6437802019-05-07 08:36:24 -07005152 if (field >= GUEST_ES_AR_BYTES && field <= GUEST_TR_AR_BYTES)
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005153 value &= 0x1f0ff;
Sean Christophersonb6437802019-05-07 08:36:24 -07005154
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005155 vmcs12_write_any(vmcs12, field, offset, value);
Sean Christopherson55d23752018-12-03 13:53:18 -08005156
5157 /*
Sean Christophersone2174292019-05-07 08:36:28 -07005158 * Do not track vmcs12 dirty-state if in guest-mode as we actually
5159 * dirty shadow vmcs12 instead of vmcs12. Fields that can be updated
5160 * by L1 without a vmexit are always updated in the vmcs02, i.e. don't
5161 * "dirty" vmcs12, all others go down the prepare_vmcs02() slow path.
Sean Christopherson55d23752018-12-03 13:53:18 -08005162 */
Sean Christophersone2174292019-05-07 08:36:28 -07005163 if (!is_guest_mode(vcpu) && !is_shadow_field_rw(field)) {
5164 /*
5165 * L1 can read these fields without exiting, ensure the
5166 * shadow VMCS is up-to-date.
5167 */
5168 if (enable_shadow_vmcs && is_shadow_field_ro(field)) {
5169 preempt_disable();
5170 vmcs_load(vmx->vmcs01.shadow_vmcs);
Sean Christophersonfadcead2019-05-07 08:36:23 -07005171
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005172 __vmcs_writel(field, value);
Sean Christophersonfadcead2019-05-07 08:36:23 -07005173
Sean Christophersone2174292019-05-07 08:36:28 -07005174 vmcs_clear(vmx->vmcs01.shadow_vmcs);
5175 vmcs_load(vmx->loaded_vmcs->vmcs);
5176 preempt_enable();
Sean Christopherson55d23752018-12-03 13:53:18 -08005177 }
Sean Christophersone2174292019-05-07 08:36:28 -07005178 vmx->nested.dirty_vmcs12 = true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005179 }
5180
5181 return nested_vmx_succeed(vcpu);
5182}
5183
5184static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
5185{
5186 vmx->nested.current_vmptr = vmptr;
5187 if (enable_shadow_vmcs) {
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07005188 secondary_exec_controls_setbit(vmx, SECONDARY_EXEC_SHADOW_VMCS);
Sean Christopherson55d23752018-12-03 13:53:18 -08005189 vmcs_write64(VMCS_LINK_POINTER,
5190 __pa(vmx->vmcs01.shadow_vmcs));
Sean Christopherson3731905ef2019-05-07 08:36:27 -07005191 vmx->nested.need_vmcs12_to_shadow_sync = true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005192 }
5193 vmx->nested.dirty_vmcs12 = true;
5194}
5195
5196/* Emulate the VMPTRLD instruction */
5197static int handle_vmptrld(struct kvm_vcpu *vcpu)
5198{
5199 struct vcpu_vmx *vmx = to_vmx(vcpu);
5200 gpa_t vmptr;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005201 int r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005202
5203 if (!nested_vmx_check_permission(vcpu))
5204 return 1;
5205
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005206 if (nested_vmx_get_vmptr(vcpu, &vmptr, &r))
5207 return r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005208
KarimAllah Ahmede0bf2662019-01-31 21:24:43 +01005209 if (!page_address_valid(vcpu, vmptr))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005210 return nested_vmx_fail(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
Sean Christopherson55d23752018-12-03 13:53:18 -08005211
5212 if (vmptr == vmx->nested.vmxon_ptr)
Sean Christophersonb2656e42020-06-08 18:56:07 -07005213 return nested_vmx_fail(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
Sean Christopherson55d23752018-12-03 13:53:18 -08005214
5215 /* Forbid normal VMPTRLD if Enlightened version was used */
5216 if (vmx->nested.hv_evmcs)
5217 return 1;
5218
5219 if (vmx->nested.current_vmptr != vmptr) {
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01005220 struct kvm_host_map map;
Sean Christopherson55d23752018-12-03 13:53:18 -08005221 struct vmcs12 *new_vmcs12;
Sean Christopherson55d23752018-12-03 13:53:18 -08005222
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01005223 if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmptr), &map)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08005224 /*
5225 * Reads from an unbacked page return all 1s,
5226 * which means that the 32 bits located at the
5227 * given physical address won't match the required
5228 * VMCS12_REVISION identifier.
5229 */
Sean Christophersonb2656e42020-06-08 18:56:07 -07005230 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005231 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Sean Christopherson55d23752018-12-03 13:53:18 -08005232 }
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01005233
5234 new_vmcs12 = map.hva;
5235
Sean Christopherson55d23752018-12-03 13:53:18 -08005236 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
5237 (new_vmcs12->hdr.shadow_vmcs &&
5238 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01005239 kvm_vcpu_unmap(vcpu, &map, false);
Sean Christophersonb2656e42020-06-08 18:56:07 -07005240 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005241 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
5242 }
5243
5244 nested_release_vmcs12(vcpu);
5245
5246 /*
5247 * Load VMCS12 from guest memory since it is not already
5248 * cached.
5249 */
5250 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01005251 kvm_vcpu_unmap(vcpu, &map, false);
Sean Christopherson55d23752018-12-03 13:53:18 -08005252
5253 set_current_vmptr(vmx, vmptr);
5254 }
5255
5256 return nested_vmx_succeed(vcpu);
5257}
5258
5259/* Emulate the VMPTRST instruction */
5260static int handle_vmptrst(struct kvm_vcpu *vcpu)
5261{
Sean Christopherson5addc232020-04-15 13:34:53 -07005262 unsigned long exit_qual = vmx_get_exit_qual(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005263 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5264 gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr;
5265 struct x86_exception e;
5266 gva_t gva;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005267 int r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005268
5269 if (!nested_vmx_check_permission(vcpu))
5270 return 1;
5271
5272 if (unlikely(to_vmx(vcpu)->nested.hv_evmcs))
5273 return 1;
5274
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005275 if (get_vmx_mem_address(vcpu, exit_qual, instr_info,
5276 true, sizeof(gpa_t), &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08005277 return 1;
5278 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005279 r = kvm_write_guest_virt_system(vcpu, gva, (void *)&current_vmptr,
5280 sizeof(gpa_t), &e);
5281 if (r != X86EMUL_CONTINUE)
Babu Moger3f3393b2020-09-11 14:29:05 -05005282 return kvm_handle_memory_failure(vcpu, r, &e);
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005283
Sean Christopherson55d23752018-12-03 13:53:18 -08005284 return nested_vmx_succeed(vcpu);
5285}
5286
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005287#define EPTP_PA_MASK GENMASK_ULL(51, 12)
5288
5289static bool nested_ept_root_matches(hpa_t root_hpa, u64 root_eptp, u64 eptp)
5290{
5291 return VALID_PAGE(root_hpa) &&
5292 ((root_eptp & EPTP_PA_MASK) == (eptp & EPTP_PA_MASK));
5293}
5294
Sean Christopherson55d23752018-12-03 13:53:18 -08005295/* Emulate the INVEPT instruction */
5296static int handle_invept(struct kvm_vcpu *vcpu)
5297{
5298 struct vcpu_vmx *vmx = to_vmx(vcpu);
5299 u32 vmx_instruction_info, types;
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005300 unsigned long type, roots_to_free;
5301 struct kvm_mmu *mmu;
Sean Christopherson55d23752018-12-03 13:53:18 -08005302 gva_t gva;
5303 struct x86_exception e;
5304 struct {
5305 u64 eptp, gpa;
5306 } operand;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005307 int i, r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005308
5309 if (!(vmx->nested.msrs.secondary_ctls_high &
5310 SECONDARY_EXEC_ENABLE_EPT) ||
5311 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
5312 kvm_queue_exception(vcpu, UD_VECTOR);
5313 return 1;
5314 }
5315
5316 if (!nested_vmx_check_permission(vcpu))
5317 return 1;
5318
5319 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5320 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
5321
5322 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
5323
5324 if (type >= 32 || !(types & (1 << type)))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005325 return nested_vmx_fail(vcpu, VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Sean Christopherson55d23752018-12-03 13:53:18 -08005326
5327 /* According to the Intel VMX instruction reference, the memory
5328 * operand is read even if it isn't needed (e.g., for type==global)
5329 */
Sean Christopherson5addc232020-04-15 13:34:53 -07005330 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005331 vmx_instruction_info, false, sizeof(operand), &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08005332 return 1;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005333 r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e);
5334 if (r != X86EMUL_CONTINUE)
Babu Moger3f3393b2020-09-11 14:29:05 -05005335 return kvm_handle_memory_failure(vcpu, r, &e);
Sean Christopherson55d23752018-12-03 13:53:18 -08005336
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005337 /*
5338 * Nested EPT roots are always held through guest_mmu,
5339 * not root_mmu.
5340 */
5341 mmu = &vcpu->arch.guest_mmu;
5342
Sean Christopherson55d23752018-12-03 13:53:18 -08005343 switch (type) {
Sean Christopherson55d23752018-12-03 13:53:18 -08005344 case VMX_EPT_EXTENT_CONTEXT:
Sean Christophersoneed00302020-03-20 14:27:58 -07005345 if (!nested_vmx_check_eptp(vcpu, operand.eptp))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005346 return nested_vmx_fail(vcpu,
Sean Christophersoneed00302020-03-20 14:27:58 -07005347 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Sean Christophersonf8aa7e32020-03-20 14:27:59 -07005348
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005349 roots_to_free = 0;
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07005350 if (nested_ept_root_matches(mmu->root_hpa, mmu->root_pgd,
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005351 operand.eptp))
5352 roots_to_free |= KVM_MMU_ROOT_CURRENT;
5353
5354 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) {
5355 if (nested_ept_root_matches(mmu->prev_roots[i].hpa,
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07005356 mmu->prev_roots[i].pgd,
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005357 operand.eptp))
5358 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
5359 }
5360 break;
Sean Christophersoneed00302020-03-20 14:27:58 -07005361 case VMX_EPT_EXTENT_GLOBAL:
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005362 roots_to_free = KVM_MMU_ROOTS_ALL;
Sean Christopherson55d23752018-12-03 13:53:18 -08005363 break;
5364 default:
Sean Christophersonf9336e32020-05-04 08:35:06 -07005365 BUG();
Sean Christopherson55d23752018-12-03 13:53:18 -08005366 break;
5367 }
5368
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005369 if (roots_to_free)
5370 kvm_mmu_free_roots(vcpu, mmu, roots_to_free);
5371
Sean Christopherson55d23752018-12-03 13:53:18 -08005372 return nested_vmx_succeed(vcpu);
5373}
5374
5375static int handle_invvpid(struct kvm_vcpu *vcpu)
5376{
5377 struct vcpu_vmx *vmx = to_vmx(vcpu);
5378 u32 vmx_instruction_info;
5379 unsigned long type, types;
5380 gva_t gva;
5381 struct x86_exception e;
5382 struct {
5383 u64 vpid;
5384 u64 gla;
5385 } operand;
5386 u16 vpid02;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005387 int r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005388
5389 if (!(vmx->nested.msrs.secondary_ctls_high &
5390 SECONDARY_EXEC_ENABLE_VPID) ||
5391 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
5392 kvm_queue_exception(vcpu, UD_VECTOR);
5393 return 1;
5394 }
5395
5396 if (!nested_vmx_check_permission(vcpu))
5397 return 1;
5398
5399 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5400 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
5401
5402 types = (vmx->nested.msrs.vpid_caps &
5403 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
5404
5405 if (type >= 32 || !(types & (1 << type)))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005406 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005407 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
5408
5409 /* according to the intel vmx instruction reference, the memory
5410 * operand is read even if it isn't needed (e.g., for type==global)
5411 */
Sean Christopherson5addc232020-04-15 13:34:53 -07005412 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005413 vmx_instruction_info, false, sizeof(operand), &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08005414 return 1;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005415 r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e);
5416 if (r != X86EMUL_CONTINUE)
Babu Moger3f3393b2020-09-11 14:29:05 -05005417 return kvm_handle_memory_failure(vcpu, r, &e);
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005418
Sean Christopherson55d23752018-12-03 13:53:18 -08005419 if (operand.vpid >> 16)
Sean Christophersonb2656e42020-06-08 18:56:07 -07005420 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005421 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
5422
5423 vpid02 = nested_get_vpid02(vcpu);
5424 switch (type) {
5425 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
5426 if (!operand.vpid ||
5427 is_noncanonical_address(operand.gla, vcpu))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005428 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005429 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Sean Christophersonbc41d0c2020-03-20 14:28:09 -07005430 vpid_sync_vcpu_addr(vpid02, operand.gla);
Sean Christopherson55d23752018-12-03 13:53:18 -08005431 break;
5432 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
5433 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
5434 if (!operand.vpid)
Sean Christophersonb2656e42020-06-08 18:56:07 -07005435 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005436 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Sean Christopherson446ace42020-03-20 14:28:05 -07005437 vpid_sync_context(vpid02);
Sean Christopherson55d23752018-12-03 13:53:18 -08005438 break;
5439 case VMX_VPID_EXTENT_ALL_CONTEXT:
Sean Christopherson446ace42020-03-20 14:28:05 -07005440 vpid_sync_context(vpid02);
Sean Christopherson55d23752018-12-03 13:53:18 -08005441 break;
5442 default:
5443 WARN_ON_ONCE(1);
5444 return kvm_skip_emulated_instruction(vcpu);
5445 }
5446
Junaid Shahidd6e3f832020-03-20 14:28:00 -07005447 /*
5448 * Sync the shadow page tables if EPT is disabled, L1 is invalidating
5449 * linear mappings for L2 (tagged with L2's VPID). Free all roots as
5450 * VPIDs are not tracked in the MMU role.
5451 *
5452 * Note, this operates on root_mmu, not guest_mmu, as L1 and L2 share
5453 * an MMU when EPT is disabled.
5454 *
5455 * TODO: sync only the affected SPTEs for INVDIVIDUAL_ADDR.
5456 */
5457 if (!enable_ept)
5458 kvm_mmu_free_roots(vcpu, &vcpu->arch.root_mmu,
5459 KVM_MMU_ROOTS_ALL);
5460
Sean Christopherson55d23752018-12-03 13:53:18 -08005461 return nested_vmx_succeed(vcpu);
5462}
5463
5464static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
5465 struct vmcs12 *vmcs12)
5466{
Sean Christopherson2b3eaf82019-04-30 10:36:19 -07005467 u32 index = kvm_rcx_read(vcpu);
Sean Christophersonac6389a2020-03-02 18:02:38 -08005468 u64 new_eptp;
Sean Christopherson55d23752018-12-03 13:53:18 -08005469 bool accessed_dirty;
5470 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5471
5472 if (!nested_cpu_has_eptp_switching(vmcs12) ||
5473 !nested_cpu_has_ept(vmcs12))
5474 return 1;
5475
5476 if (index >= VMFUNC_EPTP_ENTRIES)
5477 return 1;
5478
5479
5480 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
Sean Christophersonac6389a2020-03-02 18:02:38 -08005481 &new_eptp, index * 8, 8))
Sean Christopherson55d23752018-12-03 13:53:18 -08005482 return 1;
5483
Sean Christophersonac6389a2020-03-02 18:02:38 -08005484 accessed_dirty = !!(new_eptp & VMX_EPTP_AD_ENABLE_BIT);
Sean Christopherson55d23752018-12-03 13:53:18 -08005485
5486 /*
5487 * If the (L2) guest does a vmfunc to the currently
5488 * active ept pointer, we don't have to do anything else
5489 */
Sean Christophersonac6389a2020-03-02 18:02:38 -08005490 if (vmcs12->ept_pointer != new_eptp) {
5491 if (!nested_vmx_check_eptp(vcpu, new_eptp))
Sean Christopherson55d23752018-12-03 13:53:18 -08005492 return 1;
5493
Sean Christopherson55d23752018-12-03 13:53:18 -08005494 mmu->ept_ad = accessed_dirty;
5495 mmu->mmu_role.base.ad_disabled = !accessed_dirty;
Sean Christophersonac6389a2020-03-02 18:02:38 -08005496 vmcs12->ept_pointer = new_eptp;
Sean Christophersonc805f5d2021-03-04 17:10:57 -08005497
5498 kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005499 }
5500
5501 return 0;
5502}
5503
5504static int handle_vmfunc(struct kvm_vcpu *vcpu)
5505{
5506 struct vcpu_vmx *vmx = to_vmx(vcpu);
5507 struct vmcs12 *vmcs12;
Sean Christopherson2b3eaf82019-04-30 10:36:19 -07005508 u32 function = kvm_rax_read(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005509
5510 /*
5511 * VMFUNC is only supported for nested guests, but we always enable the
5512 * secondary control for simplicity; for non-nested mode, fake that we
5513 * didn't by injecting #UD.
5514 */
5515 if (!is_guest_mode(vcpu)) {
5516 kvm_queue_exception(vcpu, UD_VECTOR);
5517 return 1;
5518 }
5519
5520 vmcs12 = get_vmcs12(vcpu);
5521 if ((vmcs12->vm_function_control & (1 << function)) == 0)
5522 goto fail;
5523
5524 switch (function) {
5525 case 0:
5526 if (nested_vmx_eptp_switching(vcpu, vmcs12))
5527 goto fail;
5528 break;
5529 default:
5530 goto fail;
5531 }
5532 return kvm_skip_emulated_instruction(vcpu);
5533
5534fail:
Sean Christopherson8e533242020-11-06 17:03:12 +08005535 /*
5536 * This is effectively a reflected VM-Exit, as opposed to a synthesized
5537 * nested VM-Exit. Pass the original exit reason, i.e. don't hardcode
5538 * EXIT_REASON_VMFUNC as the exit reason.
5539 */
5540 nested_vmx_vmexit(vcpu, vmx->exit_reason.full,
Sean Christopherson87915852020-04-15 13:34:54 -07005541 vmx_get_intr_info(vcpu),
Sean Christopherson5addc232020-04-15 13:34:53 -07005542 vmx_get_exit_qual(vcpu));
Sean Christopherson55d23752018-12-03 13:53:18 -08005543 return 1;
5544}
5545
Oliver Uptone71237d2020-02-04 15:26:30 -08005546/*
5547 * Return true if an IO instruction with the specified port and size should cause
5548 * a VM-exit into L1.
5549 */
5550bool nested_vmx_check_io_bitmaps(struct kvm_vcpu *vcpu, unsigned int port,
5551 int size)
Sean Christopherson55d23752018-12-03 13:53:18 -08005552{
Oliver Uptone71237d2020-02-04 15:26:30 -08005553 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005554 gpa_t bitmap, last_bitmap;
Sean Christopherson55d23752018-12-03 13:53:18 -08005555 u8 b;
5556
Sean Christopherson55d23752018-12-03 13:53:18 -08005557 last_bitmap = (gpa_t)-1;
5558 b = -1;
5559
5560 while (size > 0) {
5561 if (port < 0x8000)
5562 bitmap = vmcs12->io_bitmap_a;
5563 else if (port < 0x10000)
5564 bitmap = vmcs12->io_bitmap_b;
5565 else
5566 return true;
5567 bitmap += (port & 0x7fff) / 8;
5568
5569 if (last_bitmap != bitmap)
5570 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
5571 return true;
5572 if (b & (1 << (port & 7)))
5573 return true;
5574
5575 port++;
5576 size--;
5577 last_bitmap = bitmap;
5578 }
5579
5580 return false;
5581}
5582
Oliver Uptone71237d2020-02-04 15:26:30 -08005583static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
5584 struct vmcs12 *vmcs12)
5585{
5586 unsigned long exit_qualification;
Oliver Upton35a57132020-02-04 15:26:31 -08005587 unsigned short port;
Oliver Uptone71237d2020-02-04 15:26:30 -08005588 int size;
5589
5590 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
5591 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
5592
Sean Christopherson5addc232020-04-15 13:34:53 -07005593 exit_qualification = vmx_get_exit_qual(vcpu);
Oliver Uptone71237d2020-02-04 15:26:30 -08005594
5595 port = exit_qualification >> 16;
5596 size = (exit_qualification & 7) + 1;
5597
5598 return nested_vmx_check_io_bitmaps(vcpu, port, size);
5599}
5600
Sean Christopherson55d23752018-12-03 13:53:18 -08005601/*
Miaohe Lin463bfee2020-02-14 10:44:05 +08005602 * Return 1 if we should exit from L2 to L1 to handle an MSR access,
Sean Christopherson55d23752018-12-03 13:53:18 -08005603 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
5604 * disinterest in the current event (read or write a specific MSR) by using an
5605 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
5606 */
5607static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
Sean Christopherson8e533242020-11-06 17:03:12 +08005608 struct vmcs12 *vmcs12,
5609 union vmx_exit_reason exit_reason)
Sean Christopherson55d23752018-12-03 13:53:18 -08005610{
Sean Christopherson2b3eaf82019-04-30 10:36:19 -07005611 u32 msr_index = kvm_rcx_read(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005612 gpa_t bitmap;
5613
5614 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
5615 return true;
5616
5617 /*
5618 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
5619 * for the four combinations of read/write and low/high MSR numbers.
5620 * First we need to figure out which of the four to use:
5621 */
5622 bitmap = vmcs12->msr_bitmap;
Sean Christopherson8e533242020-11-06 17:03:12 +08005623 if (exit_reason.basic == EXIT_REASON_MSR_WRITE)
Sean Christopherson55d23752018-12-03 13:53:18 -08005624 bitmap += 2048;
5625 if (msr_index >= 0xc0000000) {
5626 msr_index -= 0xc0000000;
5627 bitmap += 1024;
5628 }
5629
5630 /* Then read the msr_index'th bit from this bitmap: */
5631 if (msr_index < 1024*8) {
5632 unsigned char b;
5633 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
5634 return true;
5635 return 1 & (b >> (msr_index & 7));
5636 } else
5637 return true; /* let L1 handle the wrong parameter */
5638}
5639
5640/*
5641 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
5642 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
5643 * intercept (via guest_host_mask etc.) the current event.
5644 */
5645static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
5646 struct vmcs12 *vmcs12)
5647{
Sean Christopherson5addc232020-04-15 13:34:53 -07005648 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005649 int cr = exit_qualification & 15;
5650 int reg;
5651 unsigned long val;
5652
5653 switch ((exit_qualification >> 4) & 3) {
5654 case 0: /* mov to cr */
5655 reg = (exit_qualification >> 8) & 15;
5656 val = kvm_register_readl(vcpu, reg);
5657 switch (cr) {
5658 case 0:
5659 if (vmcs12->cr0_guest_host_mask &
5660 (val ^ vmcs12->cr0_read_shadow))
5661 return true;
5662 break;
5663 case 3:
Sean Christopherson55d23752018-12-03 13:53:18 -08005664 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
5665 return true;
5666 break;
5667 case 4:
5668 if (vmcs12->cr4_guest_host_mask &
5669 (vmcs12->cr4_read_shadow ^ val))
5670 return true;
5671 break;
5672 case 8:
5673 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
5674 return true;
5675 break;
5676 }
5677 break;
5678 case 2: /* clts */
5679 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
5680 (vmcs12->cr0_read_shadow & X86_CR0_TS))
5681 return true;
5682 break;
5683 case 1: /* mov from cr */
5684 switch (cr) {
5685 case 3:
5686 if (vmcs12->cpu_based_vm_exec_control &
5687 CPU_BASED_CR3_STORE_EXITING)
5688 return true;
5689 break;
5690 case 8:
5691 if (vmcs12->cpu_based_vm_exec_control &
5692 CPU_BASED_CR8_STORE_EXITING)
5693 return true;
5694 break;
5695 }
5696 break;
5697 case 3: /* lmsw */
5698 /*
5699 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
5700 * cr0. Other attempted changes are ignored, with no exit.
5701 */
5702 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
5703 if (vmcs12->cr0_guest_host_mask & 0xe &
5704 (val ^ vmcs12->cr0_read_shadow))
5705 return true;
5706 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
5707 !(vmcs12->cr0_read_shadow & 0x1) &&
5708 (val & 0x1))
5709 return true;
5710 break;
5711 }
5712 return false;
5713}
5714
5715static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
5716 struct vmcs12 *vmcs12, gpa_t bitmap)
5717{
5718 u32 vmx_instruction_info;
5719 unsigned long field;
5720 u8 b;
5721
5722 if (!nested_cpu_has_shadow_vmcs(vmcs12))
5723 return true;
5724
5725 /* Decode instruction info and find the field to access */
5726 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5727 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
5728
5729 /* Out-of-range fields always cause a VM exit from L2 to L1 */
5730 if (field >> 15)
5731 return true;
5732
5733 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
5734 return true;
5735
5736 return 1 & (b >> (field & 7));
5737}
5738
Oliver Uptonb045ae92020-04-14 22:47:45 +00005739static bool nested_vmx_exit_handled_mtf(struct vmcs12 *vmcs12)
5740{
5741 u32 entry_intr_info = vmcs12->vm_entry_intr_info_field;
5742
5743 if (nested_cpu_has_mtf(vmcs12))
5744 return true;
5745
5746 /*
5747 * An MTF VM-exit may be injected into the guest by setting the
5748 * interruption-type to 7 (other event) and the vector field to 0. Such
5749 * is the case regardless of the 'monitor trap flag' VM-execution
5750 * control.
5751 */
5752 return entry_intr_info == (INTR_INFO_VALID_MASK
5753 | INTR_TYPE_OTHER_EVENT);
5754}
5755
Sean Christopherson55d23752018-12-03 13:53:18 -08005756/*
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005757 * Return true if L0 wants to handle an exit from L2 regardless of whether or not
5758 * L1 wants the exit. Only call this when in is_guest_mode (L2).
Sean Christopherson55d23752018-12-03 13:53:18 -08005759 */
Sean Christopherson8e533242020-11-06 17:03:12 +08005760static bool nested_vmx_l0_wants_exit(struct kvm_vcpu *vcpu,
5761 union vmx_exit_reason exit_reason)
Sean Christopherson55d23752018-12-03 13:53:18 -08005762{
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005763 u32 intr_info;
5764
Sean Christopherson8e533242020-11-06 17:03:12 +08005765 switch ((u16)exit_reason.basic) {
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005766 case EXIT_REASON_EXCEPTION_NMI:
Sean Christopherson87915852020-04-15 13:34:54 -07005767 intr_info = vmx_get_intr_info(vcpu);
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005768 if (is_nmi(intr_info))
5769 return true;
5770 else if (is_page_fault(intr_info))
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +02005771 return vcpu->arch.apf.host_apf_flags || !enable_ept;
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005772 else if (is_debug(intr_info) &&
5773 vcpu->guest_debug &
5774 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5775 return true;
5776 else if (is_breakpoint(intr_info) &&
5777 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
5778 return true;
5779 return false;
5780 case EXIT_REASON_EXTERNAL_INTERRUPT:
5781 return true;
5782 case EXIT_REASON_MCE_DURING_VMENTRY:
5783 return true;
5784 case EXIT_REASON_EPT_VIOLATION:
5785 /*
5786 * L0 always deals with the EPT violation. If nested EPT is
5787 * used, and the nested mmu code discovers that the address is
5788 * missing in the guest EPT table (EPT12), the EPT violation
5789 * will be injected with nested_ept_inject_page_fault()
5790 */
5791 return true;
5792 case EXIT_REASON_EPT_MISCONFIG:
5793 /*
5794 * L2 never uses directly L1's EPT, but rather L0's own EPT
5795 * table (shadow on EPT) or a merged EPT table that L0 built
5796 * (EPT on EPT). So any problems with the structure of the
5797 * table is L0's fault.
5798 */
5799 return true;
5800 case EXIT_REASON_PREEMPTION_TIMER:
5801 return true;
5802 case EXIT_REASON_PML_FULL:
Sean Christophersonc3bb9a22021-02-12 16:50:07 -08005803 /*
5804 * PML is emulated for an L1 VMM and should never be enabled in
5805 * vmcs02, always "handle" PML_FULL by exiting to userspace.
5806 */
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005807 return true;
5808 case EXIT_REASON_VMFUNC:
5809 /* VM functions are emulated through L2->L0 vmexits. */
5810 return true;
5811 case EXIT_REASON_ENCLS:
5812 /* SGX is never exposed to L1 */
5813 return true;
5814 default:
5815 break;
5816 }
5817 return false;
5818}
5819
5820/*
5821 * Return 1 if L1 wants to intercept an exit from L2. Only call this when in
5822 * is_guest_mode (L2).
5823 */
Sean Christopherson8e533242020-11-06 17:03:12 +08005824static bool nested_vmx_l1_wants_exit(struct kvm_vcpu *vcpu,
5825 union vmx_exit_reason exit_reason)
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005826{
Sean Christopherson55d23752018-12-03 13:53:18 -08005827 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Sean Christopherson9bd4af22020-04-21 00:53:27 -07005828 u32 intr_info;
Sean Christopherson55d23752018-12-03 13:53:18 -08005829
Sean Christopherson8e533242020-11-06 17:03:12 +08005830 switch ((u16)exit_reason.basic) {
Sean Christopherson55d23752018-12-03 13:53:18 -08005831 case EXIT_REASON_EXCEPTION_NMI:
Sean Christopherson87915852020-04-15 13:34:54 -07005832 intr_info = vmx_get_intr_info(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005833 if (is_nmi(intr_info))
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005834 return true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005835 else if (is_page_fault(intr_info))
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005836 return true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005837 return vmcs12->exception_bitmap &
5838 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
5839 case EXIT_REASON_EXTERNAL_INTERRUPT:
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005840 return nested_exit_on_intr(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005841 case EXIT_REASON_TRIPLE_FAULT:
5842 return true;
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08005843 case EXIT_REASON_INTERRUPT_WINDOW:
5844 return nested_cpu_has(vmcs12, CPU_BASED_INTR_WINDOW_EXITING);
Sean Christopherson55d23752018-12-03 13:53:18 -08005845 case EXIT_REASON_NMI_WINDOW:
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08005846 return nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING);
Sean Christopherson55d23752018-12-03 13:53:18 -08005847 case EXIT_REASON_TASK_SWITCH:
5848 return true;
5849 case EXIT_REASON_CPUID:
5850 return true;
5851 case EXIT_REASON_HLT:
5852 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
5853 case EXIT_REASON_INVD:
5854 return true;
5855 case EXIT_REASON_INVLPG:
5856 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
5857 case EXIT_REASON_RDPMC:
5858 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
5859 case EXIT_REASON_RDRAND:
5860 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
5861 case EXIT_REASON_RDSEED:
5862 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
5863 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
5864 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
5865 case EXIT_REASON_VMREAD:
5866 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
5867 vmcs12->vmread_bitmap);
5868 case EXIT_REASON_VMWRITE:
5869 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
5870 vmcs12->vmwrite_bitmap);
5871 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
5872 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
5873 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
5874 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
5875 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
5876 /*
5877 * VMX instructions trap unconditionally. This allows L1 to
5878 * emulate them for its L2 guest, i.e., allows 3-level nesting!
5879 */
5880 return true;
5881 case EXIT_REASON_CR_ACCESS:
5882 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
5883 case EXIT_REASON_DR_ACCESS:
5884 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
5885 case EXIT_REASON_IO_INSTRUCTION:
5886 return nested_vmx_exit_handled_io(vcpu, vmcs12);
5887 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
5888 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
5889 case EXIT_REASON_MSR_READ:
5890 case EXIT_REASON_MSR_WRITE:
5891 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
5892 case EXIT_REASON_INVALID_STATE:
5893 return true;
5894 case EXIT_REASON_MWAIT_INSTRUCTION:
5895 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
5896 case EXIT_REASON_MONITOR_TRAP_FLAG:
Oliver Uptonb045ae92020-04-14 22:47:45 +00005897 return nested_vmx_exit_handled_mtf(vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08005898 case EXIT_REASON_MONITOR_INSTRUCTION:
5899 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
5900 case EXIT_REASON_PAUSE_INSTRUCTION:
5901 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
5902 nested_cpu_has2(vmcs12,
5903 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
5904 case EXIT_REASON_MCE_DURING_VMENTRY:
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005905 return true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005906 case EXIT_REASON_TPR_BELOW_THRESHOLD:
5907 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
5908 case EXIT_REASON_APIC_ACCESS:
5909 case EXIT_REASON_APIC_WRITE:
5910 case EXIT_REASON_EOI_INDUCED:
5911 /*
5912 * The controls for "virtualize APIC accesses," "APIC-
5913 * register virtualization," and "virtual-interrupt
5914 * delivery" only come from vmcs12.
5915 */
5916 return true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005917 case EXIT_REASON_INVPCID:
5918 return
5919 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
5920 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
5921 case EXIT_REASON_WBINVD:
5922 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
5923 case EXIT_REASON_XSETBV:
5924 return true;
5925 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
5926 /*
5927 * This should never happen, since it is not possible to
5928 * set XSS to a non-zero value---neither in L1 nor in L2.
5929 * If if it were, XSS would have to be checked against
5930 * the XSS exit bitmap in vmcs12.
5931 */
5932 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Tao Xubf653b72019-07-16 14:55:51 +08005933 case EXIT_REASON_UMWAIT:
5934 case EXIT_REASON_TPAUSE:
5935 return nested_cpu_has2(vmcs12,
5936 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE);
Sean Christopherson55d23752018-12-03 13:53:18 -08005937 default:
5938 return true;
5939 }
5940}
5941
Sean Christopherson7b7bd872020-04-15 10:55:11 -07005942/*
5943 * Conditionally reflect a VM-Exit into L1. Returns %true if the VM-Exit was
5944 * reflected into L1.
5945 */
Sean Christophersonf47baae2020-04-15 10:55:16 -07005946bool nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu)
Sean Christopherson7b7bd872020-04-15 10:55:11 -07005947{
Sean Christophersonfbdd5022020-04-15 10:55:12 -07005948 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson8e533242020-11-06 17:03:12 +08005949 union vmx_exit_reason exit_reason = vmx->exit_reason;
Sean Christopherson87796552020-04-22 17:11:27 -07005950 unsigned long exit_qual;
5951 u32 exit_intr_info;
Sean Christophersonfbdd5022020-04-15 10:55:12 -07005952
5953 WARN_ON_ONCE(vmx->nested.nested_run_pending);
5954
5955 /*
5956 * Late nested VM-Fail shares the same flow as nested VM-Exit since KVM
5957 * has already loaded L2's state.
5958 */
5959 if (unlikely(vmx->fail)) {
5960 trace_kvm_nested_vmenter_failed(
5961 "hardware VM-instruction error: ",
5962 vmcs_read32(VM_INSTRUCTION_ERROR));
5963 exit_intr_info = 0;
5964 exit_qual = 0;
5965 goto reflect_vmexit;
5966 }
Sean Christopherson7b7bd872020-04-15 10:55:11 -07005967
Sean Christopherson8e533242020-11-06 17:03:12 +08005968 trace_kvm_nested_vmexit(exit_reason.full, vcpu, KVM_ISA_VMX);
Sean Christopherson236871b2020-04-15 10:55:13 -07005969
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005970 /* If L0 (KVM) wants the exit, it trumps L1's desires. */
5971 if (nested_vmx_l0_wants_exit(vcpu, exit_reason))
5972 return false;
5973
5974 /* If L1 doesn't want the exit, handle it in L0. */
5975 if (!nested_vmx_l1_wants_exit(vcpu, exit_reason))
Sean Christopherson7b7bd872020-04-15 10:55:11 -07005976 return false;
5977
5978 /*
Sean Christopherson1d283062020-04-15 10:55:15 -07005979 * vmcs.VM_EXIT_INTR_INFO is only valid for EXCEPTION_NMI exits. For
5980 * EXTERNAL_INTERRUPT, the value for vmcs12->vm_exit_intr_info would
5981 * need to be synthesized by querying the in-kernel LAPIC, but external
5982 * interrupts are never reflected to L1 so it's a non-issue.
Sean Christopherson7b7bd872020-04-15 10:55:11 -07005983 */
Sean Christopherson02f19652020-09-23 13:13:49 -07005984 exit_intr_info = vmx_get_intr_info(vcpu);
Sean Christophersonf315f2b2020-09-23 13:13:45 -07005985 if (is_exception_with_error_code(exit_intr_info)) {
Sean Christopherson7b7bd872020-04-15 10:55:11 -07005986 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5987
5988 vmcs12->vm_exit_intr_error_code =
5989 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
5990 }
Sean Christopherson02f19652020-09-23 13:13:49 -07005991 exit_qual = vmx_get_exit_qual(vcpu);
Sean Christopherson7b7bd872020-04-15 10:55:11 -07005992
Sean Christophersonfbdd5022020-04-15 10:55:12 -07005993reflect_vmexit:
Sean Christopherson8e533242020-11-06 17:03:12 +08005994 nested_vmx_vmexit(vcpu, exit_reason.full, exit_intr_info, exit_qual);
Sean Christopherson7b7bd872020-04-15 10:55:11 -07005995 return true;
5996}
Sean Christopherson55d23752018-12-03 13:53:18 -08005997
5998static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
5999 struct kvm_nested_state __user *user_kvm_nested_state,
6000 u32 user_data_size)
6001{
6002 struct vcpu_vmx *vmx;
6003 struct vmcs12 *vmcs12;
6004 struct kvm_nested_state kvm_state = {
6005 .flags = 0,
Liran Alon6ca00df2019-06-16 15:03:10 +03006006 .format = KVM_STATE_NESTED_FORMAT_VMX,
Sean Christopherson55d23752018-12-03 13:53:18 -08006007 .size = sizeof(kvm_state),
Peter Shier850448f2020-05-26 14:51:06 -07006008 .hdr.vmx.flags = 0,
Liran Alon6ca00df2019-06-16 15:03:10 +03006009 .hdr.vmx.vmxon_pa = -1ull,
6010 .hdr.vmx.vmcs12_pa = -1ull,
Peter Shier850448f2020-05-26 14:51:06 -07006011 .hdr.vmx.preemption_timer_deadline = 0,
Sean Christopherson55d23752018-12-03 13:53:18 -08006012 };
Liran Alon6ca00df2019-06-16 15:03:10 +03006013 struct kvm_vmx_nested_state_data __user *user_vmx_nested_state =
6014 &user_kvm_nested_state->data.vmx[0];
Sean Christopherson55d23752018-12-03 13:53:18 -08006015
6016 if (!vcpu)
Liran Alon6ca00df2019-06-16 15:03:10 +03006017 return kvm_state.size + sizeof(*user_vmx_nested_state);
Sean Christopherson55d23752018-12-03 13:53:18 -08006018
6019 vmx = to_vmx(vcpu);
6020 vmcs12 = get_vmcs12(vcpu);
6021
Sean Christopherson55d23752018-12-03 13:53:18 -08006022 if (nested_vmx_allowed(vcpu) &&
6023 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
Liran Alon6ca00df2019-06-16 15:03:10 +03006024 kvm_state.hdr.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
6025 kvm_state.hdr.vmx.vmcs12_pa = vmx->nested.current_vmptr;
Sean Christopherson55d23752018-12-03 13:53:18 -08006026
6027 if (vmx_has_valid_vmcs12(vcpu)) {
Liran Alon6ca00df2019-06-16 15:03:10 +03006028 kvm_state.size += sizeof(user_vmx_nested_state->vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08006029
Liran Alon323d73a2019-06-26 16:09:27 +03006030 if (vmx->nested.hv_evmcs)
6031 kvm_state.flags |= KVM_STATE_NESTED_EVMCS;
6032
Sean Christopherson55d23752018-12-03 13:53:18 -08006033 if (is_guest_mode(vcpu) &&
6034 nested_cpu_has_shadow_vmcs(vmcs12) &&
6035 vmcs12->vmcs_link_pointer != -1ull)
Liran Alon6ca00df2019-06-16 15:03:10 +03006036 kvm_state.size += sizeof(user_vmx_nested_state->shadow_vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08006037 }
6038
6039 if (vmx->nested.smm.vmxon)
Liran Alon6ca00df2019-06-16 15:03:10 +03006040 kvm_state.hdr.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
Sean Christopherson55d23752018-12-03 13:53:18 -08006041
6042 if (vmx->nested.smm.guest_mode)
Liran Alon6ca00df2019-06-16 15:03:10 +03006043 kvm_state.hdr.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
Sean Christopherson55d23752018-12-03 13:53:18 -08006044
6045 if (is_guest_mode(vcpu)) {
6046 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
6047
6048 if (vmx->nested.nested_run_pending)
6049 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
Oliver Upton5ef8acb2020-02-07 02:36:07 -08006050
6051 if (vmx->nested.mtf_pending)
6052 kvm_state.flags |= KVM_STATE_NESTED_MTF_PENDING;
Peter Shier850448f2020-05-26 14:51:06 -07006053
6054 if (nested_cpu_has_preemption_timer(vmcs12) &&
6055 vmx->nested.has_preemption_timer_deadline) {
6056 kvm_state.hdr.vmx.flags |=
6057 KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE;
6058 kvm_state.hdr.vmx.preemption_timer_deadline =
6059 vmx->nested.preemption_timer_deadline;
6060 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006061 }
6062 }
6063
6064 if (user_data_size < kvm_state.size)
6065 goto out;
6066
6067 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
6068 return -EFAULT;
6069
6070 if (!vmx_has_valid_vmcs12(vcpu))
6071 goto out;
6072
6073 /*
6074 * When running L2, the authoritative vmcs12 state is in the
6075 * vmcs02. When running L1, the authoritative vmcs12 state is
6076 * in the shadow or enlightened vmcs linked to vmcs01, unless
Sean Christopherson3731905ef2019-05-07 08:36:27 -07006077 * need_vmcs12_to_shadow_sync is set, in which case, the authoritative
Sean Christopherson55d23752018-12-03 13:53:18 -08006078 * vmcs12 state is in the vmcs12 already.
6079 */
6080 if (is_guest_mode(vcpu)) {
Sean Christopherson3731905ef2019-05-07 08:36:27 -07006081 sync_vmcs02_to_vmcs12(vcpu, vmcs12);
Sean Christopherson7952d762019-05-07 08:36:29 -07006082 sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
Maxim Levitskyd51e1d32021-01-14 22:54:47 +02006083 } else {
6084 copy_vmcs02_to_vmcs12_rare(vcpu, get_vmcs12(vcpu));
6085 if (!vmx->nested.need_vmcs12_to_shadow_sync) {
6086 if (vmx->nested.hv_evmcs)
6087 copy_enlightened_to_vmcs12(vmx);
6088 else if (enable_shadow_vmcs)
6089 copy_shadow_to_vmcs12(vmx);
6090 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006091 }
6092
Liran Alon6ca00df2019-06-16 15:03:10 +03006093 BUILD_BUG_ON(sizeof(user_vmx_nested_state->vmcs12) < VMCS12_SIZE);
6094 BUILD_BUG_ON(sizeof(user_vmx_nested_state->shadow_vmcs12) < VMCS12_SIZE);
6095
Tom Roeder3a33d032019-01-24 13:48:20 -08006096 /*
6097 * Copy over the full allocated size of vmcs12 rather than just the size
6098 * of the struct.
6099 */
Liran Alon6ca00df2019-06-16 15:03:10 +03006100 if (copy_to_user(user_vmx_nested_state->vmcs12, vmcs12, VMCS12_SIZE))
Sean Christopherson55d23752018-12-03 13:53:18 -08006101 return -EFAULT;
6102
6103 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
6104 vmcs12->vmcs_link_pointer != -1ull) {
Liran Alon6ca00df2019-06-16 15:03:10 +03006105 if (copy_to_user(user_vmx_nested_state->shadow_vmcs12,
Tom Roeder3a33d032019-01-24 13:48:20 -08006106 get_shadow_vmcs12(vcpu), VMCS12_SIZE))
Sean Christopherson55d23752018-12-03 13:53:18 -08006107 return -EFAULT;
6108 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006109out:
6110 return kvm_state.size;
6111}
6112
6113/*
6114 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
6115 */
6116void vmx_leave_nested(struct kvm_vcpu *vcpu)
6117{
6118 if (is_guest_mode(vcpu)) {
6119 to_vmx(vcpu)->nested.nested_run_pending = 0;
6120 nested_vmx_vmexit(vcpu, -1, 0, 0);
6121 }
6122 free_nested(vcpu);
6123}
6124
6125static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
6126 struct kvm_nested_state __user *user_kvm_nested_state,
6127 struct kvm_nested_state *kvm_state)
6128{
6129 struct vcpu_vmx *vmx = to_vmx(vcpu);
6130 struct vmcs12 *vmcs12;
Sean Christopherson68cda402020-05-11 15:05:29 -07006131 enum vm_entry_failure_code ignored;
Liran Alon6ca00df2019-06-16 15:03:10 +03006132 struct kvm_vmx_nested_state_data __user *user_vmx_nested_state =
6133 &user_kvm_nested_state->data.vmx[0];
Sean Christopherson55d23752018-12-03 13:53:18 -08006134 int ret;
6135
Liran Alon6ca00df2019-06-16 15:03:10 +03006136 if (kvm_state->format != KVM_STATE_NESTED_FORMAT_VMX)
Sean Christopherson55d23752018-12-03 13:53:18 -08006137 return -EINVAL;
6138
Liran Alon6ca00df2019-06-16 15:03:10 +03006139 if (kvm_state->hdr.vmx.vmxon_pa == -1ull) {
6140 if (kvm_state->hdr.vmx.smm.flags)
Sean Christopherson55d23752018-12-03 13:53:18 -08006141 return -EINVAL;
6142
Liran Alon6ca00df2019-06-16 15:03:10 +03006143 if (kvm_state->hdr.vmx.vmcs12_pa != -1ull)
Sean Christopherson55d23752018-12-03 13:53:18 -08006144 return -EINVAL;
6145
Liran Alon323d73a2019-06-26 16:09:27 +03006146 /*
6147 * KVM_STATE_NESTED_EVMCS used to signal that KVM should
6148 * enable eVMCS capability on vCPU. However, since then
6149 * code was changed such that flag signals vmcs12 should
6150 * be copied into eVMCS in guest memory.
6151 *
6152 * To preserve backwards compatability, allow user
6153 * to set this flag even when there is no VMXON region.
6154 */
Paolo Bonzini9fd58872019-06-19 16:52:27 +02006155 if (kvm_state->flags & ~KVM_STATE_NESTED_EVMCS)
6156 return -EINVAL;
6157 } else {
6158 if (!nested_vmx_allowed(vcpu))
6159 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08006160
Paolo Bonzini9fd58872019-06-19 16:52:27 +02006161 if (!page_address_valid(vcpu, kvm_state->hdr.vmx.vmxon_pa))
6162 return -EINVAL;
Liran Alon323d73a2019-06-26 16:09:27 +03006163 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006164
Liran Alon6ca00df2019-06-16 15:03:10 +03006165 if ((kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
Sean Christopherson55d23752018-12-03 13:53:18 -08006166 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
6167 return -EINVAL;
6168
Liran Alon6ca00df2019-06-16 15:03:10 +03006169 if (kvm_state->hdr.vmx.smm.flags &
Sean Christopherson55d23752018-12-03 13:53:18 -08006170 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
6171 return -EINVAL;
6172
Paolo Bonzini5e105c82020-07-27 08:55:09 -04006173 if (kvm_state->hdr.vmx.flags & ~KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE)
6174 return -EINVAL;
6175
Sean Christopherson55d23752018-12-03 13:53:18 -08006176 /*
6177 * SMM temporarily disables VMX, so we cannot be in guest mode,
6178 * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags
6179 * must be zero.
6180 */
Liran Alon65b712f12019-06-25 14:26:42 +03006181 if (is_smm(vcpu) ?
6182 (kvm_state->flags &
6183 (KVM_STATE_NESTED_GUEST_MODE | KVM_STATE_NESTED_RUN_PENDING))
6184 : kvm_state->hdr.vmx.smm.flags)
Sean Christopherson55d23752018-12-03 13:53:18 -08006185 return -EINVAL;
6186
Liran Alon6ca00df2019-06-16 15:03:10 +03006187 if ((kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
6188 !(kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
Sean Christopherson55d23752018-12-03 13:53:18 -08006189 return -EINVAL;
6190
Liran Alon323d73a2019-06-26 16:09:27 +03006191 if ((kvm_state->flags & KVM_STATE_NESTED_EVMCS) &&
6192 (!nested_vmx_allowed(vcpu) || !vmx->nested.enlightened_vmcs_enabled))
Paolo Bonzini9fd58872019-06-19 16:52:27 +02006193 return -EINVAL;
6194
Liran Alon323d73a2019-06-26 16:09:27 +03006195 vmx_leave_nested(vcpu);
Paolo Bonzini9fd58872019-06-19 16:52:27 +02006196
Liran Alon6ca00df2019-06-16 15:03:10 +03006197 if (kvm_state->hdr.vmx.vmxon_pa == -1ull)
Sean Christopherson55d23752018-12-03 13:53:18 -08006198 return 0;
6199
Liran Alon6ca00df2019-06-16 15:03:10 +03006200 vmx->nested.vmxon_ptr = kvm_state->hdr.vmx.vmxon_pa;
Sean Christopherson55d23752018-12-03 13:53:18 -08006201 ret = enter_vmx_operation(vcpu);
6202 if (ret)
6203 return ret;
6204
Paolo Bonzini0f02bd02020-07-27 09:00:37 -04006205 /* Empty 'VMXON' state is permitted if no VMCS loaded */
6206 if (kvm_state->size < sizeof(*kvm_state) + sizeof(*vmcs12)) {
6207 /* See vmx_has_valid_vmcs12. */
6208 if ((kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE) ||
6209 (kvm_state->flags & KVM_STATE_NESTED_EVMCS) ||
6210 (kvm_state->hdr.vmx.vmcs12_pa != -1ull))
6211 return -EINVAL;
6212 else
6213 return 0;
6214 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006215
Liran Alon6ca00df2019-06-16 15:03:10 +03006216 if (kvm_state->hdr.vmx.vmcs12_pa != -1ull) {
6217 if (kvm_state->hdr.vmx.vmcs12_pa == kvm_state->hdr.vmx.vmxon_pa ||
6218 !page_address_valid(vcpu, kvm_state->hdr.vmx.vmcs12_pa))
Sean Christopherson55d23752018-12-03 13:53:18 -08006219 return -EINVAL;
6220
Liran Alon6ca00df2019-06-16 15:03:10 +03006221 set_current_vmptr(vmx, kvm_state->hdr.vmx.vmcs12_pa);
Sean Christopherson55d23752018-12-03 13:53:18 -08006222 } else if (kvm_state->flags & KVM_STATE_NESTED_EVMCS) {
6223 /*
Vitaly Kuznetsove942dbf2020-03-09 16:52:12 +01006224 * nested_vmx_handle_enlightened_vmptrld() cannot be called
6225 * directly from here as HV_X64_MSR_VP_ASSIST_PAGE may not be
6226 * restored yet. EVMCS will be mapped from
6227 * nested_get_vmcs12_pages().
Sean Christopherson55d23752018-12-03 13:53:18 -08006228 */
Paolo Bonzini729c15c2020-09-22 06:53:57 -04006229 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08006230 } else {
6231 return -EINVAL;
6232 }
6233
Liran Alon6ca00df2019-06-16 15:03:10 +03006234 if (kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
Sean Christopherson55d23752018-12-03 13:53:18 -08006235 vmx->nested.smm.vmxon = true;
6236 vmx->nested.vmxon = false;
6237
Liran Alon6ca00df2019-06-16 15:03:10 +03006238 if (kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
Sean Christopherson55d23752018-12-03 13:53:18 -08006239 vmx->nested.smm.guest_mode = true;
6240 }
6241
6242 vmcs12 = get_vmcs12(vcpu);
Liran Alon6ca00df2019-06-16 15:03:10 +03006243 if (copy_from_user(vmcs12, user_vmx_nested_state->vmcs12, sizeof(*vmcs12)))
Sean Christopherson55d23752018-12-03 13:53:18 -08006244 return -EFAULT;
6245
6246 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
6247 return -EINVAL;
6248
6249 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
6250 return 0;
6251
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006252 vmx->nested.nested_run_pending =
6253 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
6254
Oliver Upton5ef8acb2020-02-07 02:36:07 -08006255 vmx->nested.mtf_pending =
6256 !!(kvm_state->flags & KVM_STATE_NESTED_MTF_PENDING);
6257
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006258 ret = -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08006259 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
6260 vmcs12->vmcs_link_pointer != -1ull) {
6261 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
6262
Liran Alon6ca00df2019-06-16 15:03:10 +03006263 if (kvm_state->size <
6264 sizeof(*kvm_state) +
6265 sizeof(user_vmx_nested_state->vmcs12) + sizeof(*shadow_vmcs12))
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006266 goto error_guest_mode;
Sean Christopherson55d23752018-12-03 13:53:18 -08006267
6268 if (copy_from_user(shadow_vmcs12,
Liran Alon6ca00df2019-06-16 15:03:10 +03006269 user_vmx_nested_state->shadow_vmcs12,
6270 sizeof(*shadow_vmcs12))) {
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006271 ret = -EFAULT;
6272 goto error_guest_mode;
6273 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006274
6275 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
6276 !shadow_vmcs12->hdr.shadow_vmcs)
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006277 goto error_guest_mode;
Sean Christopherson55d23752018-12-03 13:53:18 -08006278 }
6279
Paolo Bonzini83d31e52020-07-09 13:12:09 -04006280 vmx->nested.has_preemption_timer_deadline = false;
Peter Shier850448f2020-05-26 14:51:06 -07006281 if (kvm_state->hdr.vmx.flags & KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE) {
6282 vmx->nested.has_preemption_timer_deadline = true;
6283 vmx->nested.preemption_timer_deadline =
6284 kvm_state->hdr.vmx.preemption_timer_deadline;
6285 }
6286
Sean Christopherson5478ba32019-04-11 12:18:06 -07006287 if (nested_vmx_check_controls(vcpu, vmcs12) ||
6288 nested_vmx_check_host_state(vcpu, vmcs12) ||
Sean Christopherson68cda402020-05-11 15:05:29 -07006289 nested_vmx_check_guest_state(vcpu, vmcs12, &ignored))
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006290 goto error_guest_mode;
Sean Christopherson55d23752018-12-03 13:53:18 -08006291
6292 vmx->nested.dirty_vmcs12 = true;
6293 ret = nested_vmx_enter_non_root_mode(vcpu, false);
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006294 if (ret)
6295 goto error_guest_mode;
Sean Christopherson55d23752018-12-03 13:53:18 -08006296
6297 return 0;
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006298
6299error_guest_mode:
6300 vmx->nested.nested_run_pending = 0;
6301 return ret;
Sean Christopherson55d23752018-12-03 13:53:18 -08006302}
6303
Xiaoyao Li1b842922019-10-20 17:11:01 +08006304void nested_vmx_set_vmcs_shadowing_bitmap(void)
Sean Christopherson55d23752018-12-03 13:53:18 -08006305{
6306 if (enable_shadow_vmcs) {
Sean Christopherson55d23752018-12-03 13:53:18 -08006307 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Sean Christophersonfadcead2019-05-07 08:36:23 -07006308 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
Sean Christopherson55d23752018-12-03 13:53:18 -08006309 }
6310}
6311
6312/*
6313 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
6314 * returned for the various VMX controls MSRs when nested VMX is enabled.
6315 * The same values should also be used to verify that vmcs12 control fields are
6316 * valid during nested entry from L1 to L2.
6317 * Each of these control msrs has a low and high 32-bit half: A low bit is on
6318 * if the corresponding bit in the (32-bit) control field *must* be on, and a
6319 * bit in the high half is on if the corresponding bit in the control field
6320 * may be on. See also vmx_control_verify().
6321 */
Vitaly Kuznetsova4443262020-02-20 18:22:04 +01006322void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, u32 ept_caps)
Sean Christopherson55d23752018-12-03 13:53:18 -08006323{
6324 /*
6325 * Note that as a general rule, the high half of the MSRs (bits in
6326 * the control fields which may be 1) should be initialized by the
6327 * intersection of the underlying hardware's MSR (i.e., features which
6328 * can be supported) and the list of features we want to expose -
6329 * because they are known to be properly supported in our code.
6330 * Also, usually, the low half of the MSRs (bits which must be 1) can
6331 * be set to 0, meaning that L1 may turn off any of these bits. The
6332 * reason is that if one of these bits is necessary, it will appear
6333 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
6334 * fields of vmcs01 and vmcs02, will turn these bits off - and
Sean Christopherson2c1f3322020-04-15 10:55:14 -07006335 * nested_vmx_l1_wants_exit() will not pass related exits to L1.
Sean Christopherson55d23752018-12-03 13:53:18 -08006336 * These rules have exceptions below.
6337 */
6338
6339 /* pin-based controls */
6340 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
6341 msrs->pinbased_ctls_low,
6342 msrs->pinbased_ctls_high);
6343 msrs->pinbased_ctls_low |=
6344 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
6345 msrs->pinbased_ctls_high &=
6346 PIN_BASED_EXT_INTR_MASK |
6347 PIN_BASED_NMI_EXITING |
6348 PIN_BASED_VIRTUAL_NMIS |
Vitaly Kuznetsova4443262020-02-20 18:22:04 +01006349 (enable_apicv ? PIN_BASED_POSTED_INTR : 0);
Sean Christopherson55d23752018-12-03 13:53:18 -08006350 msrs->pinbased_ctls_high |=
6351 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
6352 PIN_BASED_VMX_PREEMPTION_TIMER;
6353
6354 /* exit controls */
6355 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
6356 msrs->exit_ctls_low,
6357 msrs->exit_ctls_high);
6358 msrs->exit_ctls_low =
6359 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
6360
6361 msrs->exit_ctls_high &=
6362#ifdef CONFIG_X86_64
6363 VM_EXIT_HOST_ADDR_SPACE_SIZE |
6364#endif
Chenyi Qiangefc83132020-08-28 16:56:18 +08006365 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT |
6366 VM_EXIT_CLEAR_BNDCFGS | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
Sean Christopherson55d23752018-12-03 13:53:18 -08006367 msrs->exit_ctls_high |=
6368 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
6369 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
6370 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
6371
6372 /* We support free control of debug control saving. */
6373 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
6374
6375 /* entry controls */
6376 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
6377 msrs->entry_ctls_low,
6378 msrs->entry_ctls_high);
6379 msrs->entry_ctls_low =
6380 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
6381 msrs->entry_ctls_high &=
6382#ifdef CONFIG_X86_64
6383 VM_ENTRY_IA32E_MODE |
6384#endif
Chenyi Qiangefc83132020-08-28 16:56:18 +08006385 VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS |
6386 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
Sean Christopherson55d23752018-12-03 13:53:18 -08006387 msrs->entry_ctls_high |=
6388 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
6389
6390 /* We support free control of debug control loading. */
6391 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
6392
6393 /* cpu-based controls */
6394 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
6395 msrs->procbased_ctls_low,
6396 msrs->procbased_ctls_high);
6397 msrs->procbased_ctls_low =
6398 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
6399 msrs->procbased_ctls_high &=
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08006400 CPU_BASED_INTR_WINDOW_EXITING |
Xiaoyao Li5e3d3942019-12-06 16:45:26 +08006401 CPU_BASED_NMI_WINDOW_EXITING | CPU_BASED_USE_TSC_OFFSETTING |
Sean Christopherson55d23752018-12-03 13:53:18 -08006402 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
6403 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
6404 CPU_BASED_CR3_STORE_EXITING |
6405#ifdef CONFIG_X86_64
6406 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
6407#endif
6408 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
6409 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
6410 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
6411 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
6412 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
6413 /*
6414 * We can allow some features even when not supported by the
6415 * hardware. For example, L1 can specify an MSR bitmap - and we
6416 * can use it to avoid exits to L1 - even when L0 runs L2
6417 * without MSR bitmaps.
6418 */
6419 msrs->procbased_ctls_high |=
6420 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
6421 CPU_BASED_USE_MSR_BITMAPS;
6422
6423 /* We support free control of CR3 access interception. */
6424 msrs->procbased_ctls_low &=
6425 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
6426
6427 /*
6428 * secondary cpu-based controls. Do not include those that
Xiaoyao Li7c1b7612020-07-09 12:34:25 +08006429 * depend on CPUID bits, they are added later by
6430 * vmx_vcpu_after_set_cpuid.
Sean Christopherson55d23752018-12-03 13:53:18 -08006431 */
Vitaly Kuznetsov6b1971c2019-02-07 11:42:14 +01006432 if (msrs->procbased_ctls_high & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)
6433 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
6434 msrs->secondary_ctls_low,
6435 msrs->secondary_ctls_high);
6436
Sean Christopherson55d23752018-12-03 13:53:18 -08006437 msrs->secondary_ctls_low = 0;
6438 msrs->secondary_ctls_high &=
6439 SECONDARY_EXEC_DESC |
Sean Christopherson7f3603b2020-09-23 09:50:47 -07006440 SECONDARY_EXEC_ENABLE_RDTSCP |
Sean Christopherson55d23752018-12-03 13:53:18 -08006441 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini6defc592019-07-02 14:39:29 +02006442 SECONDARY_EXEC_WBINVD_EXITING |
Sean Christopherson55d23752018-12-03 13:53:18 -08006443 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6444 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini6defc592019-07-02 14:39:29 +02006445 SECONDARY_EXEC_RDRAND_EXITING |
6446 SECONDARY_EXEC_ENABLE_INVPCID |
6447 SECONDARY_EXEC_RDSEED_EXITING |
6448 SECONDARY_EXEC_XSAVES;
Sean Christopherson55d23752018-12-03 13:53:18 -08006449
6450 /*
6451 * We can emulate "VMCS shadowing," even if the hardware
6452 * doesn't support it.
6453 */
6454 msrs->secondary_ctls_high |=
6455 SECONDARY_EXEC_SHADOW_VMCS;
6456
6457 if (enable_ept) {
6458 /* nested EPT: emulate EPT also to L1 */
6459 msrs->secondary_ctls_high |=
6460 SECONDARY_EXEC_ENABLE_EPT;
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08006461 msrs->ept_caps =
6462 VMX_EPT_PAGE_WALK_4_BIT |
6463 VMX_EPT_PAGE_WALK_5_BIT |
6464 VMX_EPTP_WB_BIT |
Sean Christopherson96d47012020-03-02 18:02:40 -08006465 VMX_EPT_INVEPT_BIT |
6466 VMX_EPT_EXECUTE_ONLY_BIT;
6467
Sean Christopherson55d23752018-12-03 13:53:18 -08006468 msrs->ept_caps &= ept_caps;
6469 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
6470 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
6471 VMX_EPT_1GB_PAGE_BIT;
6472 if (enable_ept_ad_bits) {
6473 msrs->secondary_ctls_high |=
6474 SECONDARY_EXEC_ENABLE_PML;
6475 msrs->ept_caps |= VMX_EPT_AD_BIT;
6476 }
6477 }
6478
6479 if (cpu_has_vmx_vmfunc()) {
6480 msrs->secondary_ctls_high |=
6481 SECONDARY_EXEC_ENABLE_VMFUNC;
6482 /*
6483 * Advertise EPTP switching unconditionally
6484 * since we emulate it
6485 */
6486 if (enable_ept)
6487 msrs->vmfunc_controls =
6488 VMX_VMFUNC_EPTP_SWITCHING;
6489 }
6490
6491 /*
6492 * Old versions of KVM use the single-context version without
6493 * checking for support, so declare that it is supported even
6494 * though it is treated as global context. The alternative is
6495 * not failing the single-context invvpid, and it is worse.
6496 */
6497 if (enable_vpid) {
6498 msrs->secondary_ctls_high |=
6499 SECONDARY_EXEC_ENABLE_VPID;
6500 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
6501 VMX_VPID_EXTENT_SUPPORTED_MASK;
6502 }
6503
6504 if (enable_unrestricted_guest)
6505 msrs->secondary_ctls_high |=
6506 SECONDARY_EXEC_UNRESTRICTED_GUEST;
6507
6508 if (flexpriority_enabled)
6509 msrs->secondary_ctls_high |=
6510 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6511
6512 /* miscellaneous data */
6513 rdmsr(MSR_IA32_VMX_MISC,
6514 msrs->misc_low,
6515 msrs->misc_high);
6516 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
6517 msrs->misc_low |=
6518 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
6519 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Yadong Qibf0cd882020-11-06 14:51:22 +08006520 VMX_MISC_ACTIVITY_HLT |
6521 VMX_MISC_ACTIVITY_WAIT_SIPI;
Sean Christopherson55d23752018-12-03 13:53:18 -08006522 msrs->misc_high = 0;
6523
6524 /*
6525 * This MSR reports some information about VMX support. We
6526 * should return information about the VMX we emulate for the
6527 * guest, and the VMCS structure we give it - not about the
6528 * VMX support of the underlying hardware.
6529 */
6530 msrs->basic =
6531 VMCS12_REVISION |
6532 VMX_BASIC_TRUE_CTLS |
6533 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
6534 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
6535
6536 if (cpu_has_vmx_basic_inout())
6537 msrs->basic |= VMX_BASIC_INOUT;
6538
6539 /*
6540 * These MSRs specify bits which the guest must keep fixed on
6541 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
6542 * We picked the standard core2 setting.
6543 */
6544#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
6545#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
6546 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
6547 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
6548
6549 /* These MSRs specify bits which the guest must keep fixed off. */
6550 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
6551 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
6552
6553 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
6554 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
6555}
6556
6557void nested_vmx_hardware_unsetup(void)
6558{
6559 int i;
6560
6561 if (enable_shadow_vmcs) {
6562 for (i = 0; i < VMX_BITMAP_NR; i++)
6563 free_page((unsigned long)vmx_bitmap[i]);
6564 }
6565}
6566
Sean Christopherson6c1c6e52020-05-06 13:46:53 -07006567__init int nested_vmx_hardware_setup(int (*exit_handlers[])(struct kvm_vcpu *))
Sean Christopherson55d23752018-12-03 13:53:18 -08006568{
6569 int i;
6570
6571 if (!cpu_has_vmx_shadow_vmcs())
6572 enable_shadow_vmcs = 0;
6573 if (enable_shadow_vmcs) {
6574 for (i = 0; i < VMX_BITMAP_NR; i++) {
Ben Gardon41836832019-02-11 11:02:52 -08006575 /*
6576 * The vmx_bitmap is not tied to a VM and so should
6577 * not be charged to a memcg.
6578 */
Sean Christopherson55d23752018-12-03 13:53:18 -08006579 vmx_bitmap[i] = (unsigned long *)
6580 __get_free_page(GFP_KERNEL);
6581 if (!vmx_bitmap[i]) {
6582 nested_vmx_hardware_unsetup();
6583 return -ENOMEM;
6584 }
6585 }
6586
6587 init_vmcs_shadow_fields();
6588 }
6589
Liran Aloncc877672019-11-18 21:11:21 +02006590 exit_handlers[EXIT_REASON_VMCLEAR] = handle_vmclear;
6591 exit_handlers[EXIT_REASON_VMLAUNCH] = handle_vmlaunch;
6592 exit_handlers[EXIT_REASON_VMPTRLD] = handle_vmptrld;
6593 exit_handlers[EXIT_REASON_VMPTRST] = handle_vmptrst;
6594 exit_handlers[EXIT_REASON_VMREAD] = handle_vmread;
6595 exit_handlers[EXIT_REASON_VMRESUME] = handle_vmresume;
6596 exit_handlers[EXIT_REASON_VMWRITE] = handle_vmwrite;
6597 exit_handlers[EXIT_REASON_VMOFF] = handle_vmoff;
6598 exit_handlers[EXIT_REASON_VMON] = handle_vmon;
6599 exit_handlers[EXIT_REASON_INVEPT] = handle_invept;
6600 exit_handlers[EXIT_REASON_INVVPID] = handle_invvpid;
6601 exit_handlers[EXIT_REASON_VMFUNC] = handle_vmfunc;
Sean Christopherson55d23752018-12-03 13:53:18 -08006602
Sean Christopherson55d23752018-12-03 13:53:18 -08006603 return 0;
6604}
Paolo Bonzini33b22172020-04-17 10:24:18 -04006605
6606struct kvm_x86_nested_ops vmx_nested_ops = {
6607 .check_events = vmx_check_nested_events,
Sean Christophersond2060bd2020-04-22 19:25:39 -07006608 .hv_timer_pending = nested_vmx_preemption_timer_pending,
Sean Christophersoncb6a32c2021-03-02 09:45:14 -08006609 .triple_fault = nested_vmx_triple_fault,
Paolo Bonzini33b22172020-04-17 10:24:18 -04006610 .get_state = vmx_get_nested_state,
6611 .set_state = vmx_set_nested_state,
Paolo Bonzini9a78e152021-01-08 11:43:08 -05006612 .get_nested_state_pages = vmx_get_nested_state_pages,
Sean Christopherson02f5fb22020-06-22 14:58:32 -07006613 .write_log_dirty = nested_vmx_write_pml_buffer,
Paolo Bonzini33b22172020-04-17 10:24:18 -04006614 .enable_evmcs = nested_enable_evmcs,
6615 .get_evmcs_version = nested_get_evmcs_version,
6616};