blob: 4e545996440bb54623ca9ec2f9ecabc14a601cc4 [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 Christopherson72add912021-04-12 16:21:42 +120014#include "sgx.h"
Sean Christopherson55d23752018-12-03 13:53:18 -080015#include "trace.h"
Uros Bizjak150f17b2020-12-30 16:26:57 -080016#include "vmx.h"
Sean Christopherson55d23752018-12-03 13:53:18 -080017#include "x86.h"
18
19static bool __read_mostly enable_shadow_vmcs = 1;
20module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
21
22static bool __read_mostly nested_early_check = 0;
23module_param(nested_early_check, bool, S_IRUGO);
24
Sean Christopherson648fc8a2021-02-03 16:01:16 -080025#define CC KVM_NESTED_VMENTER_CONSISTENCY_CHECK
Sean Christopherson5497b952019-07-11 08:58:29 -070026
Sean Christopherson55d23752018-12-03 13:53:18 -080027/*
28 * Hyper-V requires all of these, so mark them as supported even though
29 * they are just treated the same as all-context.
30 */
31#define VMX_VPID_EXTENT_SUPPORTED_MASK \
32 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
33 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
34 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
35 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
36
37#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
38
39enum {
40 VMX_VMREAD_BITMAP,
41 VMX_VMWRITE_BITMAP,
42 VMX_BITMAP_NR
43};
44static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
45
46#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
47#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
48
Sean Christopherson1c6f0b42019-05-07 08:36:25 -070049struct shadow_vmcs_field {
50 u16 encoding;
51 u16 offset;
52};
53static struct shadow_vmcs_field shadow_read_only_fields[] = {
54#define SHADOW_FIELD_RO(x, y) { x, offsetof(struct vmcs12, y) },
Sean Christopherson55d23752018-12-03 13:53:18 -080055#include "vmcs_shadow_fields.h"
56};
57static int max_shadow_read_only_fields =
58 ARRAY_SIZE(shadow_read_only_fields);
59
Sean Christopherson1c6f0b42019-05-07 08:36:25 -070060static struct shadow_vmcs_field shadow_read_write_fields[] = {
61#define SHADOW_FIELD_RW(x, y) { x, offsetof(struct vmcs12, y) },
Sean Christopherson55d23752018-12-03 13:53:18 -080062#include "vmcs_shadow_fields.h"
63};
64static int max_shadow_read_write_fields =
65 ARRAY_SIZE(shadow_read_write_fields);
66
Yi Wang8997f652019-01-21 15:27:05 +080067static void init_vmcs_shadow_fields(void)
Sean Christopherson55d23752018-12-03 13:53:18 -080068{
69 int i, j;
70
71 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
72 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
73
74 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
Sean Christopherson1c6f0b42019-05-07 08:36:25 -070075 struct shadow_vmcs_field entry = shadow_read_only_fields[i];
76 u16 field = entry.encoding;
Sean Christopherson55d23752018-12-03 13:53:18 -080077
78 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
79 (i + 1 == max_shadow_read_only_fields ||
Sean Christopherson1c6f0b42019-05-07 08:36:25 -070080 shadow_read_only_fields[i + 1].encoding != field + 1))
Sean Christopherson55d23752018-12-03 13:53:18 -080081 pr_err("Missing field from shadow_read_only_field %x\n",
82 field + 1);
83
84 clear_bit(field, vmx_vmread_bitmap);
Sean Christopherson55d23752018-12-03 13:53:18 -080085 if (field & 1)
Sean Christopherson1c6f0b42019-05-07 08:36:25 -070086#ifdef CONFIG_X86_64
Sean Christopherson55d23752018-12-03 13:53:18 -080087 continue;
Sean Christopherson1c6f0b42019-05-07 08:36:25 -070088#else
89 entry.offset += sizeof(u32);
Sean Christopherson55d23752018-12-03 13:53:18 -080090#endif
Sean Christopherson1c6f0b42019-05-07 08:36:25 -070091 shadow_read_only_fields[j++] = entry;
Sean Christopherson55d23752018-12-03 13:53:18 -080092 }
93 max_shadow_read_only_fields = j;
94
95 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Sean Christopherson1c6f0b42019-05-07 08:36:25 -070096 struct shadow_vmcs_field entry = shadow_read_write_fields[i];
97 u16 field = entry.encoding;
Sean Christopherson55d23752018-12-03 13:53:18 -080098
99 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
100 (i + 1 == max_shadow_read_write_fields ||
Sean Christopherson1c6f0b42019-05-07 08:36:25 -0700101 shadow_read_write_fields[i + 1].encoding != field + 1))
Sean Christopherson55d23752018-12-03 13:53:18 -0800102 pr_err("Missing field from shadow_read_write_field %x\n",
103 field + 1);
104
Sean Christophersonb6437802019-05-07 08:36:24 -0700105 WARN_ONCE(field >= GUEST_ES_AR_BYTES &&
106 field <= GUEST_TR_AR_BYTES,
Sean Christopherson1c6f0b42019-05-07 08:36:25 -0700107 "Update vmcs12_write_any() to drop reserved bits from AR_BYTES");
Sean Christophersonb6437802019-05-07 08:36:24 -0700108
Sean Christopherson55d23752018-12-03 13:53:18 -0800109 /*
110 * PML and the preemption timer can be emulated, but the
111 * processor cannot vmwrite to fields that don't exist
112 * on bare metal.
113 */
114 switch (field) {
115 case GUEST_PML_INDEX:
116 if (!cpu_has_vmx_pml())
117 continue;
118 break;
119 case VMX_PREEMPTION_TIMER_VALUE:
120 if (!cpu_has_vmx_preemption_timer())
121 continue;
122 break;
123 case GUEST_INTR_STATUS:
124 if (!cpu_has_vmx_apicv())
125 continue;
126 break;
127 default:
128 break;
129 }
130
131 clear_bit(field, vmx_vmwrite_bitmap);
132 clear_bit(field, vmx_vmread_bitmap);
Sean Christopherson55d23752018-12-03 13:53:18 -0800133 if (field & 1)
Sean Christopherson1c6f0b42019-05-07 08:36:25 -0700134#ifdef CONFIG_X86_64
Sean Christopherson55d23752018-12-03 13:53:18 -0800135 continue;
Sean Christopherson1c6f0b42019-05-07 08:36:25 -0700136#else
137 entry.offset += sizeof(u32);
Sean Christopherson55d23752018-12-03 13:53:18 -0800138#endif
Sean Christopherson1c6f0b42019-05-07 08:36:25 -0700139 shadow_read_write_fields[j++] = entry;
Sean Christopherson55d23752018-12-03 13:53:18 -0800140 }
141 max_shadow_read_write_fields = j;
142}
143
144/*
145 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
146 * set the success or error code of an emulated VMX instruction (as specified
147 * by Vol 2B, VMX Instruction Reference, "Conventions"), and skip the emulated
148 * instruction.
149 */
150static int nested_vmx_succeed(struct kvm_vcpu *vcpu)
151{
152 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
153 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
154 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
155 return kvm_skip_emulated_instruction(vcpu);
156}
157
158static int nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
159{
160 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
161 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
162 X86_EFLAGS_SF | X86_EFLAGS_OF))
163 | X86_EFLAGS_CF);
164 return kvm_skip_emulated_instruction(vcpu);
165}
166
167static int nested_vmx_failValid(struct kvm_vcpu *vcpu,
168 u32 vm_instruction_error)
169{
Sean Christopherson55d23752018-12-03 13:53:18 -0800170 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
171 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
172 X86_EFLAGS_SF | X86_EFLAGS_OF))
173 | X86_EFLAGS_ZF);
174 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
175 /*
176 * We don't need to force a shadow sync because
177 * VM_INSTRUCTION_ERROR is not shadowed
178 */
179 return kvm_skip_emulated_instruction(vcpu);
180}
181
Sean Christophersonb2656e42020-06-08 18:56:07 -0700182static int nested_vmx_fail(struct kvm_vcpu *vcpu, u32 vm_instruction_error)
183{
184 struct vcpu_vmx *vmx = to_vmx(vcpu);
185
186 /*
187 * failValid writes the error number to the current VMCS, which
188 * can't be done if there isn't a current VMCS.
189 */
190 if (vmx->nested.current_vmptr == -1ull && !vmx->nested.hv_evmcs)
191 return nested_vmx_failInvalid(vcpu);
192
193 return nested_vmx_failValid(vcpu, vm_instruction_error);
194}
195
Sean Christopherson55d23752018-12-03 13:53:18 -0800196static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
197{
198 /* TODO: not to reset guest simply here. */
199 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
200 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
201}
202
Marc Orrf0b51052019-09-17 11:50:57 -0700203static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
204{
205 return fixed_bits_valid(control, low, high);
206}
207
208static inline u64 vmx_control_msr(u32 low, u32 high)
209{
210 return low | ((u64)high << 32);
211}
212
Sean Christopherson55d23752018-12-03 13:53:18 -0800213static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
214{
Sean Christophersonfe7f895d2019-05-07 12:17:57 -0700215 secondary_exec_controls_clearbit(vmx, SECONDARY_EXEC_SHADOW_VMCS);
Sean Christopherson55d23752018-12-03 13:53:18 -0800216 vmcs_write64(VMCS_LINK_POINTER, -1ull);
Paolo Bonzini88dddc12019-07-19 18:41:10 +0200217 vmx->nested.need_vmcs12_to_shadow_sync = false;
Sean Christopherson55d23752018-12-03 13:53:18 -0800218}
219
220static inline void nested_release_evmcs(struct kvm_vcpu *vcpu)
221{
222 struct vcpu_vmx *vmx = to_vmx(vcpu);
223
224 if (!vmx->nested.hv_evmcs)
225 return;
226
KarimAllah Ahmeddee9c042019-01-31 21:24:42 +0100227 kvm_vcpu_unmap(vcpu, &vmx->nested.hv_evmcs_map, true);
Vitaly Kuznetsov95fa1012020-03-09 16:52:11 +0100228 vmx->nested.hv_evmcs_vmptr = 0;
Sean Christopherson55d23752018-12-03 13:53:18 -0800229 vmx->nested.hv_evmcs = NULL;
230}
231
Sean Christophersonc61ca2f2020-09-23 11:44:49 -0700232static void vmx_sync_vmcs_host_state(struct vcpu_vmx *vmx,
233 struct loaded_vmcs *prev)
234{
235 struct vmcs_host_state *dest, *src;
236
237 if (unlikely(!vmx->guest_state_loaded))
238 return;
239
240 src = &prev->host_state;
241 dest = &vmx->loaded_vmcs->host_state;
242
243 vmx_set_host_fs_gs(dest, src->fs_sel, src->gs_sel, src->fs_base, src->gs_base);
244 dest->ldt_sel = src->ldt_sel;
245#ifdef CONFIG_X86_64
246 dest->ds_sel = src->ds_sel;
247 dest->es_sel = src->es_sel;
248#endif
249}
250
251static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
252{
253 struct vcpu_vmx *vmx = to_vmx(vcpu);
254 struct loaded_vmcs *prev;
255 int cpu;
256
Sean Christopherson138534a2020-09-23 11:44:52 -0700257 if (WARN_ON_ONCE(vmx->loaded_vmcs == vmcs))
Sean Christophersonc61ca2f2020-09-23 11:44:49 -0700258 return;
259
260 cpu = get_cpu();
261 prev = vmx->loaded_vmcs;
262 vmx->loaded_vmcs = vmcs;
263 vmx_vcpu_load_vmcs(vcpu, cpu, prev);
264 vmx_sync_vmcs_host_state(vmx, prev);
265 put_cpu();
266
267 vmx_register_cache_reset(vcpu);
268}
269
Sean Christopherson55d23752018-12-03 13:53:18 -0800270/*
271 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
272 * just stops using VMX.
273 */
274static void free_nested(struct kvm_vcpu *vcpu)
275{
276 struct vcpu_vmx *vmx = to_vmx(vcpu);
277
Sean Christophersondf82a242020-09-23 11:44:50 -0700278 if (WARN_ON_ONCE(vmx->loaded_vmcs != &vmx->vmcs01))
279 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
280
Sean Christopherson55d23752018-12-03 13:53:18 -0800281 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
282 return;
283
Paolo Bonzini729c15c2020-09-22 06:53:57 -0400284 kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
Jan Kiszkacf645272019-07-21 13:52:18 +0200285
Sean Christopherson55d23752018-12-03 13:53:18 -0800286 vmx->nested.vmxon = false;
287 vmx->nested.smm.vmxon = false;
288 free_vpid(vmx->nested.vpid02);
289 vmx->nested.posted_intr_nv = -1;
290 vmx->nested.current_vmptr = -1ull;
291 if (enable_shadow_vmcs) {
292 vmx_disable_shadow_vmcs(vmx);
293 vmcs_clear(vmx->vmcs01.shadow_vmcs);
294 free_vmcs(vmx->vmcs01.shadow_vmcs);
295 vmx->vmcs01.shadow_vmcs = NULL;
296 }
297 kfree(vmx->nested.cached_vmcs12);
Jan Kiszkac6bf2ae2019-07-21 16:01:36 +0200298 vmx->nested.cached_vmcs12 = NULL;
Sean Christopherson55d23752018-12-03 13:53:18 -0800299 kfree(vmx->nested.cached_shadow_vmcs12);
Jan Kiszkac6bf2ae2019-07-21 16:01:36 +0200300 vmx->nested.cached_shadow_vmcs12 = NULL;
Sean Christopherson55d23752018-12-03 13:53:18 -0800301 /* Unpin physical memory we referred to in the vmcs02 */
302 if (vmx->nested.apic_access_page) {
Liran Alonb11494b2019-11-21 00:31:47 +0200303 kvm_release_page_clean(vmx->nested.apic_access_page);
Sean Christopherson55d23752018-12-03 13:53:18 -0800304 vmx->nested.apic_access_page = NULL;
305 }
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +0100306 kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map, true);
KarimAllah Ahmed3278e042019-01-31 21:24:38 +0100307 kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map, true);
308 vmx->nested.pi_desc = NULL;
Sean Christopherson55d23752018-12-03 13:53:18 -0800309
310 kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
311
312 nested_release_evmcs(vcpu);
313
314 free_loaded_vmcs(&vmx->nested.vmcs02);
315}
316
Sean Christopherson55d23752018-12-03 13:53:18 -0800317/*
318 * Ensure that the current vmcs of the logical processor is the
319 * vmcs01 of the vcpu before calling free_nested().
320 */
321void nested_vmx_free_vcpu(struct kvm_vcpu *vcpu)
322{
323 vcpu_load(vcpu);
Paolo Bonzinib4b65b52019-01-29 19:12:35 +0100324 vmx_leave_nested(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -0800325 vcpu_put(vcpu);
326}
327
328static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
329 struct x86_exception *fault)
330{
331 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
332 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson4dcefa32020-04-15 10:55:18 -0700333 u32 vm_exit_reason;
Sean Christopherson55d23752018-12-03 13:53:18 -0800334 unsigned long exit_qualification = vcpu->arch.exit_qualification;
335
336 if (vmx->nested.pml_full) {
Sean Christopherson4dcefa32020-04-15 10:55:18 -0700337 vm_exit_reason = EXIT_REASON_PML_FULL;
Sean Christopherson55d23752018-12-03 13:53:18 -0800338 vmx->nested.pml_full = false;
339 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
340 } else if (fault->error_code & PFERR_RSVD_MASK)
Sean Christopherson4dcefa32020-04-15 10:55:18 -0700341 vm_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Sean Christopherson55d23752018-12-03 13:53:18 -0800342 else
Sean Christopherson4dcefa32020-04-15 10:55:18 -0700343 vm_exit_reason = EXIT_REASON_EPT_VIOLATION;
Sean Christopherson55d23752018-12-03 13:53:18 -0800344
Sean Christopherson4dcefa32020-04-15 10:55:18 -0700345 nested_vmx_vmexit(vcpu, vm_exit_reason, 0, exit_qualification);
Sean Christopherson55d23752018-12-03 13:53:18 -0800346 vmcs12->guest_physical_address = fault->address;
347}
348
349static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
350{
351 WARN_ON(mmu_is_nested(vcpu));
352
353 vcpu->arch.mmu = &vcpu->arch.guest_mmu;
354 kvm_init_shadow_ept_mmu(vcpu,
355 to_vmx(vcpu)->nested.msrs.ept_caps &
356 VMX_EPT_EXECUTE_ONLY_BIT,
357 nested_ept_ad_enabled(vcpu),
Sean Christophersonac69dfa2020-03-02 18:02:37 -0800358 nested_ept_get_eptp(vcpu));
Sean Christophersond8dd54e2020-03-02 18:02:39 -0800359 vcpu->arch.mmu->get_guest_pgd = nested_ept_get_eptp;
Sean Christopherson55d23752018-12-03 13:53:18 -0800360 vcpu->arch.mmu->inject_page_fault = nested_ept_inject_page_fault;
361 vcpu->arch.mmu->get_pdptr = kvm_pdptr_read;
362
363 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
364}
365
366static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
367{
368 vcpu->arch.mmu = &vcpu->arch.root_mmu;
369 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
370}
371
372static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
373 u16 error_code)
374{
375 bool inequality, bit;
376
377 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
378 inequality =
379 (error_code & vmcs12->page_fault_error_code_mask) !=
380 vmcs12->page_fault_error_code_match;
381 return inequality ^ bit;
382}
383
384
385/*
386 * KVM wants to inject page-faults which it got to the guest. This function
387 * checks whether in a nested guest, we need to inject them to L1 or L2.
388 */
389static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
390{
391 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
392 unsigned int nr = vcpu->arch.exception.nr;
393 bool has_payload = vcpu->arch.exception.has_payload;
394 unsigned long payload = vcpu->arch.exception.payload;
395
396 if (nr == PF_VECTOR) {
397 if (vcpu->arch.exception.nested_apf) {
398 *exit_qual = vcpu->arch.apf.nested_apf_token;
399 return 1;
400 }
401 if (nested_vmx_is_page_fault_vmexit(vmcs12,
402 vcpu->arch.exception.error_code)) {
403 *exit_qual = has_payload ? payload : vcpu->arch.cr2;
404 return 1;
405 }
406 } else if (vmcs12->exception_bitmap & (1u << nr)) {
407 if (nr == DB_VECTOR) {
408 if (!has_payload) {
409 payload = vcpu->arch.dr6;
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +0800410 payload &= ~DR6_BT;
411 payload ^= DR6_ACTIVE_LOW;
Sean Christopherson55d23752018-12-03 13:53:18 -0800412 }
413 *exit_qual = payload;
414 } else
415 *exit_qual = 0;
416 return 1;
417 }
418
419 return 0;
420}
421
422
423static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
424 struct x86_exception *fault)
425{
426 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
427
428 WARN_ON(!is_guest_mode(vcpu));
429
430 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
431 !to_vmx(vcpu)->nested.nested_run_pending) {
432 vmcs12->vm_exit_intr_error_code = fault->error_code;
433 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
434 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
435 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
436 fault->address);
437 } else {
438 kvm_inject_page_fault(vcpu, fault);
439 }
440}
441
Sean Christopherson55d23752018-12-03 13:53:18 -0800442static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
443 struct vmcs12 *vmcs12)
444{
445 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
446 return 0;
447
Sean Christopherson5497b952019-07-11 08:58:29 -0700448 if (CC(!page_address_valid(vcpu, vmcs12->io_bitmap_a)) ||
449 CC(!page_address_valid(vcpu, vmcs12->io_bitmap_b)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800450 return -EINVAL;
451
452 return 0;
453}
454
455static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
456 struct vmcs12 *vmcs12)
457{
458 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
459 return 0;
460
Sean Christopherson5497b952019-07-11 08:58:29 -0700461 if (CC(!page_address_valid(vcpu, vmcs12->msr_bitmap)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800462 return -EINVAL;
463
464 return 0;
465}
466
467static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
468 struct vmcs12 *vmcs12)
469{
470 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
471 return 0;
472
Sean Christopherson5497b952019-07-11 08:58:29 -0700473 if (CC(!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800474 return -EINVAL;
475
476 return 0;
477}
478
479/*
480 * Check if MSR is intercepted for L01 MSR bitmap.
481 */
482static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
483{
484 unsigned long *msr_bitmap;
485 int f = sizeof(unsigned long);
486
487 if (!cpu_has_vmx_msr_bitmap())
488 return true;
489
490 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
491
492 if (msr <= 0x1fff) {
493 return !!test_bit(msr, msr_bitmap + 0x800 / f);
494 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
495 msr &= 0x1fff;
496 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
497 }
498
499 return true;
500}
501
502/*
503 * If a msr is allowed by L0, we should check whether it is allowed by L1.
504 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
505 */
506static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
507 unsigned long *msr_bitmap_nested,
508 u32 msr, int type)
509{
510 int f = sizeof(unsigned long);
511
512 /*
513 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
514 * have the write-low and read-high bitmap offsets the wrong way round.
515 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
516 */
517 if (msr <= 0x1fff) {
518 if (type & MSR_TYPE_R &&
519 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
520 /* read-low */
521 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
522
523 if (type & MSR_TYPE_W &&
524 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
525 /* write-low */
526 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
527
528 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
529 msr &= 0x1fff;
530 if (type & MSR_TYPE_R &&
531 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
532 /* read-high */
533 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
534
535 if (type & MSR_TYPE_W &&
536 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
537 /* write-high */
538 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
539
540 }
541}
542
Miaohe Linffdbd502020-02-07 23:22:45 +0800543static inline void enable_x2apic_msr_intercepts(unsigned long *msr_bitmap)
544{
Marc Orracff7842019-04-01 23:55:59 -0700545 int msr;
546
547 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
548 unsigned word = msr / BITS_PER_LONG;
549
550 msr_bitmap[word] = ~0;
551 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
552 }
553}
554
Sean Christopherson55d23752018-12-03 13:53:18 -0800555/*
556 * Merge L0's and L1's MSR bitmap, return false to indicate that
557 * we do not use the hardware.
558 */
559static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
560 struct vmcs12 *vmcs12)
561{
562 int msr;
Sean Christopherson55d23752018-12-03 13:53:18 -0800563 unsigned long *msr_bitmap_l1;
564 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
KarimAllah Ahmed31f0b6c2019-01-31 21:24:36 +0100565 struct kvm_host_map *map = &to_vmx(vcpu)->nested.msr_bitmap_map;
Sean Christopherson55d23752018-12-03 13:53:18 -0800566
567 /* Nothing to do if the MSR bitmap is not in use. */
568 if (!cpu_has_vmx_msr_bitmap() ||
569 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
570 return false;
571
KarimAllah Ahmed31f0b6c2019-01-31 21:24:36 +0100572 if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->msr_bitmap), map))
Sean Christopherson55d23752018-12-03 13:53:18 -0800573 return false;
574
KarimAllah Ahmed31f0b6c2019-01-31 21:24:36 +0100575 msr_bitmap_l1 = (unsigned long *)map->hva;
Sean Christopherson55d23752018-12-03 13:53:18 -0800576
Marc Orracff7842019-04-01 23:55:59 -0700577 /*
578 * To keep the control flow simple, pay eight 8-byte writes (sixteen
579 * 4-byte writes on 32-bit systems) up front to enable intercepts for
580 * the x2APIC MSR range and selectively disable them below.
581 */
582 enable_x2apic_msr_intercepts(msr_bitmap_l0);
Sean Christopherson55d23752018-12-03 13:53:18 -0800583
Marc Orracff7842019-04-01 23:55:59 -0700584 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
585 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
586 /*
587 * L0 need not intercept reads for MSRs between 0x800
588 * and 0x8ff, it just lets the processor take the value
589 * from the virtual-APIC page; take those 256 bits
590 * directly from the L1 bitmap.
591 */
592 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
593 unsigned word = msr / BITS_PER_LONG;
594
595 msr_bitmap_l0[word] = msr_bitmap_l1[word];
596 }
597 }
598
Sean Christopherson55d23752018-12-03 13:53:18 -0800599 nested_vmx_disable_intercept_for_msr(
600 msr_bitmap_l1, msr_bitmap_l0,
Marc Orracff7842019-04-01 23:55:59 -0700601 X2APIC_MSR(APIC_TASKPRI),
Marc Orrc73f4c92019-04-01 23:56:00 -0700602 MSR_TYPE_R | MSR_TYPE_W);
Marc Orracff7842019-04-01 23:55:59 -0700603
604 if (nested_cpu_has_vid(vmcs12)) {
605 nested_vmx_disable_intercept_for_msr(
606 msr_bitmap_l1, msr_bitmap_l0,
607 X2APIC_MSR(APIC_EOI),
608 MSR_TYPE_W);
609 nested_vmx_disable_intercept_for_msr(
610 msr_bitmap_l1, msr_bitmap_l0,
611 X2APIC_MSR(APIC_SELF_IPI),
612 MSR_TYPE_W);
613 }
Sean Christopherson55d23752018-12-03 13:53:18 -0800614 }
615
Sean Christophersond69129b2019-05-08 07:32:15 -0700616 /* KVM unconditionally exposes the FS/GS base MSRs to L1. */
Sean Christophersondbdd0962021-04-21 19:38:31 -0700617#ifdef CONFIG_X86_64
Sean Christophersond69129b2019-05-08 07:32:15 -0700618 nested_vmx_disable_intercept_for_msr(msr_bitmap_l1, msr_bitmap_l0,
619 MSR_FS_BASE, MSR_TYPE_RW);
620
621 nested_vmx_disable_intercept_for_msr(msr_bitmap_l1, msr_bitmap_l0,
622 MSR_GS_BASE, MSR_TYPE_RW);
623
624 nested_vmx_disable_intercept_for_msr(msr_bitmap_l1, msr_bitmap_l0,
625 MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
Sean Christophersondbdd0962021-04-21 19:38:31 -0700626#endif
Sean Christophersond69129b2019-05-08 07:32:15 -0700627
628 /*
629 * Checking the L0->L1 bitmap is trying to verify two things:
630 *
631 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
632 * ensures that we do not accidentally generate an L02 MSR bitmap
633 * from the L12 MSR bitmap that is too permissive.
634 * 2. That L1 or L2s have actually used the MSR. This avoids
635 * unnecessarily merging of the bitmap if the MSR is unused. This
636 * works properly because we only update the L01 MSR bitmap lazily.
637 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
638 * updated to reflect this when L1 (or its L2s) actually write to
639 * the MSR.
640 */
641 if (!msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL))
Sean Christopherson55d23752018-12-03 13:53:18 -0800642 nested_vmx_disable_intercept_for_msr(
643 msr_bitmap_l1, msr_bitmap_l0,
644 MSR_IA32_SPEC_CTRL,
645 MSR_TYPE_R | MSR_TYPE_W);
646
Sean Christophersond69129b2019-05-08 07:32:15 -0700647 if (!msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD))
Sean Christopherson55d23752018-12-03 13:53:18 -0800648 nested_vmx_disable_intercept_for_msr(
649 msr_bitmap_l1, msr_bitmap_l0,
650 MSR_IA32_PRED_CMD,
651 MSR_TYPE_W);
652
KarimAllah Ahmed31f0b6c2019-01-31 21:24:36 +0100653 kvm_vcpu_unmap(vcpu, &to_vmx(vcpu)->nested.msr_bitmap_map, false);
Sean Christopherson55d23752018-12-03 13:53:18 -0800654
655 return true;
656}
657
658static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
659 struct vmcs12 *vmcs12)
660{
KarimAllah Ahmed88925302019-01-31 21:24:41 +0100661 struct kvm_host_map map;
Sean Christopherson55d23752018-12-03 13:53:18 -0800662 struct vmcs12 *shadow;
Sean Christopherson55d23752018-12-03 13:53:18 -0800663
664 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
665 vmcs12->vmcs_link_pointer == -1ull)
666 return;
667
668 shadow = get_shadow_vmcs12(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -0800669
KarimAllah Ahmed88925302019-01-31 21:24:41 +0100670 if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->vmcs_link_pointer), &map))
671 return;
Sean Christopherson55d23752018-12-03 13:53:18 -0800672
KarimAllah Ahmed88925302019-01-31 21:24:41 +0100673 memcpy(shadow, map.hva, VMCS12_SIZE);
674 kvm_vcpu_unmap(vcpu, &map, false);
Sean Christopherson55d23752018-12-03 13:53:18 -0800675}
676
677static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
678 struct vmcs12 *vmcs12)
679{
680 struct vcpu_vmx *vmx = to_vmx(vcpu);
681
682 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
683 vmcs12->vmcs_link_pointer == -1ull)
684 return;
685
686 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
687 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
688}
689
690/*
691 * In nested virtualization, check if L1 has set
692 * VM_EXIT_ACK_INTR_ON_EXIT
693 */
694static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
695{
696 return get_vmcs12(vcpu)->vm_exit_controls &
697 VM_EXIT_ACK_INTR_ON_EXIT;
698}
699
Sean Christopherson55d23752018-12-03 13:53:18 -0800700static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
701 struct vmcs12 *vmcs12)
702{
703 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
Sean Christopherson5497b952019-07-11 08:58:29 -0700704 CC(!page_address_valid(vcpu, vmcs12->apic_access_addr)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800705 return -EINVAL;
706 else
707 return 0;
708}
709
710static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
711 struct vmcs12 *vmcs12)
712{
713 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
714 !nested_cpu_has_apic_reg_virt(vmcs12) &&
715 !nested_cpu_has_vid(vmcs12) &&
716 !nested_cpu_has_posted_intr(vmcs12))
717 return 0;
718
719 /*
720 * If virtualize x2apic mode is enabled,
721 * virtualize apic access must be disabled.
722 */
Sean Christopherson5497b952019-07-11 08:58:29 -0700723 if (CC(nested_cpu_has_virt_x2apic_mode(vmcs12) &&
724 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800725 return -EINVAL;
726
727 /*
728 * If virtual interrupt delivery is enabled,
729 * we must exit on external interrupts.
730 */
Sean Christopherson5497b952019-07-11 08:58:29 -0700731 if (CC(nested_cpu_has_vid(vmcs12) && !nested_exit_on_intr(vcpu)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800732 return -EINVAL;
733
734 /*
735 * bits 15:8 should be zero in posted_intr_nv,
736 * the descriptor address has been already checked
737 * in nested_get_vmcs12_pages.
738 *
739 * bits 5:0 of posted_intr_desc_addr should be zero.
740 */
741 if (nested_cpu_has_posted_intr(vmcs12) &&
Sean Christopherson5497b952019-07-11 08:58:29 -0700742 (CC(!nested_cpu_has_vid(vmcs12)) ||
743 CC(!nested_exit_intr_ack_set(vcpu)) ||
744 CC((vmcs12->posted_intr_nv & 0xff00)) ||
Sean Christopherson636e8b72021-02-03 16:01:10 -0800745 CC(!kvm_vcpu_is_legal_aligned_gpa(vcpu, vmcs12->posted_intr_desc_addr, 64))))
Sean Christopherson55d23752018-12-03 13:53:18 -0800746 return -EINVAL;
747
748 /* tpr shadow is needed by all apicv features. */
Sean Christopherson5497b952019-07-11 08:58:29 -0700749 if (CC(!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800750 return -EINVAL;
751
752 return 0;
753}
754
755static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
Sean Christophersonf9b245e2018-12-12 13:30:08 -0500756 u32 count, u64 addr)
Sean Christopherson55d23752018-12-03 13:53:18 -0800757{
Sean Christopherson55d23752018-12-03 13:53:18 -0800758 if (count == 0)
759 return 0;
Sean Christopherson636e8b72021-02-03 16:01:10 -0800760
761 if (!kvm_vcpu_is_legal_aligned_gpa(vcpu, addr, 16) ||
762 !kvm_vcpu_is_legal_gpa(vcpu, (addr + count * sizeof(struct vmx_msr_entry) - 1)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800763 return -EINVAL;
Sean Christophersonf9b245e2018-12-12 13:30:08 -0500764
Sean Christopherson55d23752018-12-03 13:53:18 -0800765 return 0;
766}
767
Krish Sadhukhan61446ba2018-12-12 13:30:09 -0500768static int nested_vmx_check_exit_msr_switch_controls(struct kvm_vcpu *vcpu,
769 struct vmcs12 *vmcs12)
Sean Christopherson55d23752018-12-03 13:53:18 -0800770{
Sean Christopherson5497b952019-07-11 08:58:29 -0700771 if (CC(nested_vmx_check_msr_switch(vcpu,
772 vmcs12->vm_exit_msr_load_count,
773 vmcs12->vm_exit_msr_load_addr)) ||
774 CC(nested_vmx_check_msr_switch(vcpu,
775 vmcs12->vm_exit_msr_store_count,
776 vmcs12->vm_exit_msr_store_addr)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800777 return -EINVAL;
Sean Christophersonf9b245e2018-12-12 13:30:08 -0500778
Sean Christopherson55d23752018-12-03 13:53:18 -0800779 return 0;
780}
781
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -0500782static int nested_vmx_check_entry_msr_switch_controls(struct kvm_vcpu *vcpu,
783 struct vmcs12 *vmcs12)
Krish Sadhukhan61446ba2018-12-12 13:30:09 -0500784{
Sean Christopherson5497b952019-07-11 08:58:29 -0700785 if (CC(nested_vmx_check_msr_switch(vcpu,
786 vmcs12->vm_entry_msr_load_count,
787 vmcs12->vm_entry_msr_load_addr)))
Krish Sadhukhan61446ba2018-12-12 13:30:09 -0500788 return -EINVAL;
789
790 return 0;
791}
792
Sean Christopherson55d23752018-12-03 13:53:18 -0800793static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
794 struct vmcs12 *vmcs12)
795{
796 if (!nested_cpu_has_pml(vmcs12))
797 return 0;
798
Sean Christopherson5497b952019-07-11 08:58:29 -0700799 if (CC(!nested_cpu_has_ept(vmcs12)) ||
800 CC(!page_address_valid(vcpu, vmcs12->pml_address)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800801 return -EINVAL;
802
803 return 0;
804}
805
806static int nested_vmx_check_unrestricted_guest_controls(struct kvm_vcpu *vcpu,
807 struct vmcs12 *vmcs12)
808{
Sean Christopherson5497b952019-07-11 08:58:29 -0700809 if (CC(nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST) &&
810 !nested_cpu_has_ept(vmcs12)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800811 return -EINVAL;
812 return 0;
813}
814
815static int nested_vmx_check_mode_based_ept_exec_controls(struct kvm_vcpu *vcpu,
816 struct vmcs12 *vmcs12)
817{
Sean Christopherson5497b952019-07-11 08:58:29 -0700818 if (CC(nested_cpu_has2(vmcs12, SECONDARY_EXEC_MODE_BASED_EPT_EXEC) &&
819 !nested_cpu_has_ept(vmcs12)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800820 return -EINVAL;
821 return 0;
822}
823
824static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
825 struct vmcs12 *vmcs12)
826{
827 if (!nested_cpu_has_shadow_vmcs(vmcs12))
828 return 0;
829
Sean Christopherson5497b952019-07-11 08:58:29 -0700830 if (CC(!page_address_valid(vcpu, vmcs12->vmread_bitmap)) ||
831 CC(!page_address_valid(vcpu, vmcs12->vmwrite_bitmap)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800832 return -EINVAL;
833
834 return 0;
835}
836
837static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
838 struct vmx_msr_entry *e)
839{
840 /* x2APIC MSR accesses are not allowed */
Sean Christopherson5497b952019-07-11 08:58:29 -0700841 if (CC(vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8))
Sean Christopherson55d23752018-12-03 13:53:18 -0800842 return -EINVAL;
Sean Christopherson5497b952019-07-11 08:58:29 -0700843 if (CC(e->index == MSR_IA32_UCODE_WRITE) || /* SDM Table 35-2 */
844 CC(e->index == MSR_IA32_UCODE_REV))
Sean Christopherson55d23752018-12-03 13:53:18 -0800845 return -EINVAL;
Sean Christopherson5497b952019-07-11 08:58:29 -0700846 if (CC(e->reserved != 0))
Sean Christopherson55d23752018-12-03 13:53:18 -0800847 return -EINVAL;
848 return 0;
849}
850
851static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
852 struct vmx_msr_entry *e)
853{
Sean Christopherson5497b952019-07-11 08:58:29 -0700854 if (CC(e->index == MSR_FS_BASE) ||
855 CC(e->index == MSR_GS_BASE) ||
856 CC(e->index == MSR_IA32_SMM_MONITOR_CTL) || /* SMM is not supported */
Sean Christopherson55d23752018-12-03 13:53:18 -0800857 nested_vmx_msr_check_common(vcpu, e))
858 return -EINVAL;
859 return 0;
860}
861
862static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
863 struct vmx_msr_entry *e)
864{
Sean Christopherson5497b952019-07-11 08:58:29 -0700865 if (CC(e->index == MSR_IA32_SMBASE) || /* SMM is not supported */
Sean Christopherson55d23752018-12-03 13:53:18 -0800866 nested_vmx_msr_check_common(vcpu, e))
867 return -EINVAL;
868 return 0;
869}
870
Marc Orrf0b51052019-09-17 11:50:57 -0700871static u32 nested_vmx_max_atomic_switch_msrs(struct kvm_vcpu *vcpu)
872{
873 struct vcpu_vmx *vmx = to_vmx(vcpu);
874 u64 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
875 vmx->nested.msrs.misc_high);
876
877 return (vmx_misc_max_msr(vmx_misc) + 1) * VMX_MISC_MSR_LIST_MULTIPLIER;
878}
879
Sean Christopherson55d23752018-12-03 13:53:18 -0800880/*
881 * Load guest's/host's msr at nested entry/exit.
882 * return 0 for success, entry index for failure.
Marc Orrf0b51052019-09-17 11:50:57 -0700883 *
884 * One of the failure modes for MSR load/store is when a list exceeds the
885 * virtual hardware's capacity. To maintain compatibility with hardware inasmuch
886 * as possible, process all valid entries before failing rather than precheck
887 * for a capacity violation.
Sean Christopherson55d23752018-12-03 13:53:18 -0800888 */
889static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
890{
891 u32 i;
892 struct vmx_msr_entry e;
Marc Orrf0b51052019-09-17 11:50:57 -0700893 u32 max_msr_list_size = nested_vmx_max_atomic_switch_msrs(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -0800894
Sean Christopherson55d23752018-12-03 13:53:18 -0800895 for (i = 0; i < count; i++) {
Marc Orrf0b51052019-09-17 11:50:57 -0700896 if (unlikely(i >= max_msr_list_size))
897 goto fail;
898
Sean Christopherson55d23752018-12-03 13:53:18 -0800899 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
900 &e, sizeof(e))) {
901 pr_debug_ratelimited(
902 "%s cannot read MSR entry (%u, 0x%08llx)\n",
903 __func__, i, gpa + i * sizeof(e));
904 goto fail;
905 }
906 if (nested_vmx_load_msr_check(vcpu, &e)) {
907 pr_debug_ratelimited(
908 "%s check failed (%u, 0x%x, 0x%x)\n",
909 __func__, i, e.index, e.reserved);
910 goto fail;
911 }
Sean Christophersonf20935d2019-09-05 14:22:54 -0700912 if (kvm_set_msr(vcpu, e.index, e.value)) {
Sean Christopherson55d23752018-12-03 13:53:18 -0800913 pr_debug_ratelimited(
914 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
915 __func__, i, e.index, e.value);
916 goto fail;
917 }
918 }
919 return 0;
920fail:
Sean Christopherson68cda402020-05-11 15:05:29 -0700921 /* Note, max_msr_list_size is at most 4096, i.e. this can't wrap. */
Sean Christopherson55d23752018-12-03 13:53:18 -0800922 return i + 1;
923}
924
Aaron Lewis662f1d12019-11-07 21:14:39 -0800925static bool nested_vmx_get_vmexit_msr_value(struct kvm_vcpu *vcpu,
926 u32 msr_index,
927 u64 *data)
928{
929 struct vcpu_vmx *vmx = to_vmx(vcpu);
930
931 /*
932 * If the L0 hypervisor stored a more accurate value for the TSC that
933 * does not include the time taken for emulation of the L2->L1
934 * VM-exit in L0, use the more accurate value.
935 */
936 if (msr_index == MSR_IA32_TSC) {
Sean Christophersona128a932020-09-23 11:03:57 -0700937 int i = vmx_find_loadstore_msr_slot(&vmx->msr_autostore.guest,
938 MSR_IA32_TSC);
Aaron Lewis662f1d12019-11-07 21:14:39 -0800939
Sean Christophersona128a932020-09-23 11:03:57 -0700940 if (i >= 0) {
941 u64 val = vmx->msr_autostore.guest.val[i].value;
Aaron Lewis662f1d12019-11-07 21:14:39 -0800942
943 *data = kvm_read_l1_tsc(vcpu, val);
944 return true;
945 }
946 }
947
948 if (kvm_get_msr(vcpu, msr_index, data)) {
949 pr_debug_ratelimited("%s cannot read MSR (0x%x)\n", __func__,
950 msr_index);
951 return false;
952 }
953 return true;
954}
955
Aaron Lewis365d3d52019-11-07 21:14:36 -0800956static bool read_and_check_msr_entry(struct kvm_vcpu *vcpu, u64 gpa, int i,
957 struct vmx_msr_entry *e)
958{
959 if (kvm_vcpu_read_guest(vcpu,
960 gpa + i * sizeof(*e),
961 e, 2 * sizeof(u32))) {
962 pr_debug_ratelimited(
963 "%s cannot read MSR entry (%u, 0x%08llx)\n",
964 __func__, i, gpa + i * sizeof(*e));
965 return false;
966 }
967 if (nested_vmx_store_msr_check(vcpu, e)) {
968 pr_debug_ratelimited(
969 "%s check failed (%u, 0x%x, 0x%x)\n",
970 __func__, i, e->index, e->reserved);
971 return false;
972 }
973 return true;
974}
975
Sean Christopherson55d23752018-12-03 13:53:18 -0800976static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
977{
Sean Christophersonf20935d2019-09-05 14:22:54 -0700978 u64 data;
Sean Christopherson55d23752018-12-03 13:53:18 -0800979 u32 i;
980 struct vmx_msr_entry e;
Marc Orrf0b51052019-09-17 11:50:57 -0700981 u32 max_msr_list_size = nested_vmx_max_atomic_switch_msrs(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -0800982
983 for (i = 0; i < count; i++) {
Marc Orrf0b51052019-09-17 11:50:57 -0700984 if (unlikely(i >= max_msr_list_size))
985 return -EINVAL;
986
Aaron Lewis365d3d52019-11-07 21:14:36 -0800987 if (!read_and_check_msr_entry(vcpu, gpa, i, &e))
Sean Christopherson55d23752018-12-03 13:53:18 -0800988 return -EINVAL;
Aaron Lewis365d3d52019-11-07 21:14:36 -0800989
Aaron Lewis662f1d12019-11-07 21:14:39 -0800990 if (!nested_vmx_get_vmexit_msr_value(vcpu, e.index, &data))
Sean Christopherson55d23752018-12-03 13:53:18 -0800991 return -EINVAL;
Aaron Lewis662f1d12019-11-07 21:14:39 -0800992
Sean Christopherson55d23752018-12-03 13:53:18 -0800993 if (kvm_vcpu_write_guest(vcpu,
994 gpa + i * sizeof(e) +
995 offsetof(struct vmx_msr_entry, value),
Sean Christophersonf20935d2019-09-05 14:22:54 -0700996 &data, sizeof(data))) {
Sean Christopherson55d23752018-12-03 13:53:18 -0800997 pr_debug_ratelimited(
998 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Sean Christophersonf20935d2019-09-05 14:22:54 -0700999 __func__, i, e.index, data);
Sean Christopherson55d23752018-12-03 13:53:18 -08001000 return -EINVAL;
1001 }
1002 }
1003 return 0;
1004}
1005
Aaron Lewis662f1d12019-11-07 21:14:39 -08001006static bool nested_msr_store_list_has_msr(struct kvm_vcpu *vcpu, u32 msr_index)
1007{
1008 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1009 u32 count = vmcs12->vm_exit_msr_store_count;
1010 u64 gpa = vmcs12->vm_exit_msr_store_addr;
1011 struct vmx_msr_entry e;
1012 u32 i;
1013
1014 for (i = 0; i < count; i++) {
1015 if (!read_and_check_msr_entry(vcpu, gpa, i, &e))
1016 return false;
1017
1018 if (e.index == msr_index)
1019 return true;
1020 }
1021 return false;
1022}
1023
1024static void prepare_vmx_msr_autostore_list(struct kvm_vcpu *vcpu,
1025 u32 msr_index)
1026{
1027 struct vcpu_vmx *vmx = to_vmx(vcpu);
1028 struct vmx_msrs *autostore = &vmx->msr_autostore.guest;
1029 bool in_vmcs12_store_list;
Sean Christophersona128a932020-09-23 11:03:57 -07001030 int msr_autostore_slot;
Aaron Lewis662f1d12019-11-07 21:14:39 -08001031 bool in_autostore_list;
1032 int last;
1033
Sean Christophersona128a932020-09-23 11:03:57 -07001034 msr_autostore_slot = vmx_find_loadstore_msr_slot(autostore, msr_index);
1035 in_autostore_list = msr_autostore_slot >= 0;
Aaron Lewis662f1d12019-11-07 21:14:39 -08001036 in_vmcs12_store_list = nested_msr_store_list_has_msr(vcpu, msr_index);
1037
1038 if (in_vmcs12_store_list && !in_autostore_list) {
Sean Christophersonce833b22020-09-23 11:03:56 -07001039 if (autostore->nr == MAX_NR_LOADSTORE_MSRS) {
Aaron Lewis662f1d12019-11-07 21:14:39 -08001040 /*
1041 * Emulated VMEntry does not fail here. Instead a less
1042 * accurate value will be returned by
1043 * nested_vmx_get_vmexit_msr_value() using kvm_get_msr()
1044 * instead of reading the value from the vmcs02 VMExit
1045 * MSR-store area.
1046 */
1047 pr_warn_ratelimited(
1048 "Not enough msr entries in msr_autostore. Can't add msr %x\n",
1049 msr_index);
1050 return;
1051 }
1052 last = autostore->nr++;
1053 autostore->val[last].index = msr_index;
1054 } else if (!in_vmcs12_store_list && in_autostore_list) {
1055 last = --autostore->nr;
Sean Christophersona128a932020-09-23 11:03:57 -07001056 autostore->val[msr_autostore_slot] = autostore->val[last];
Aaron Lewis662f1d12019-11-07 21:14:39 -08001057 }
1058}
1059
Sean Christopherson55d23752018-12-03 13:53:18 -08001060/*
Sean Christopherson41fab65e2020-03-20 14:28:29 -07001061 * Returns true if the MMU needs to be sync'd on nested VM-Enter/VM-Exit.
1062 * tl;dr: the MMU needs a sync if L0 is using shadow paging and L1 didn't
1063 * enable VPID for L2 (implying it expects a TLB flush on VMX transitions).
1064 * Here's why.
1065 *
1066 * If EPT is enabled by L0 a sync is never needed:
1067 * - if it is disabled by L1, then L0 is not shadowing L1 or L2 PTEs, there
1068 * cannot be unsync'd SPTEs for either L1 or L2.
1069 *
1070 * - if it is also enabled by L1, then L0 doesn't need to sync on VM-Enter
1071 * VM-Enter as VM-Enter isn't required to invalidate guest-physical mappings
1072 * (irrespective of VPID), i.e. L1 can't rely on the (virtual) CPU to flush
1073 * stale guest-physical mappings for L2 from the TLB. And as above, L0 isn't
1074 * shadowing L1 PTEs so there are no unsync'd SPTEs to sync on VM-Exit.
1075 *
1076 * If EPT is disabled by L0:
1077 * - if VPID is enabled by L1 (for L2), the situation is similar to when L1
1078 * enables EPT: L0 doesn't need to sync as VM-Enter and VM-Exit aren't
1079 * required to invalidate linear mappings (EPT is disabled so there are
1080 * no combined or guest-physical mappings), i.e. L1 can't rely on the
1081 * (virtual) CPU to flush stale linear mappings for either L2 or itself (L1).
1082 *
1083 * - however if VPID is disabled by L1, then a sync is needed as L1 expects all
1084 * linear mappings (EPT is disabled so there are no combined or guest-physical
1085 * mappings) to be invalidated on both VM-Enter and VM-Exit.
1086 *
1087 * Note, this logic is subtly different than nested_has_guest_tlb_tag(), which
1088 * additionally checks that L2 has been assigned a VPID (when EPT is disabled).
1089 * Whether or not L2 has been assigned a VPID by L0 is irrelevant with respect
1090 * to L1's expectations, e.g. L0 needs to invalidate hardware TLB entries if L2
1091 * doesn't have a unique VPID to prevent reusing L1's entries (assuming L1 has
1092 * been assigned a VPID), but L0 doesn't need to do a MMU sync because L1
1093 * doesn't expect stale (virtual) TLB entries to be flushed, i.e. L1 doesn't
1094 * know that L0 will flush the TLB and so L1 will do INVVPID as needed to flush
1095 * stale TLB entries, at which point L0 will sync L2's MMU.
1096 */
1097static bool nested_vmx_transition_mmu_sync(struct kvm_vcpu *vcpu)
1098{
1099 return !enable_ept && !nested_cpu_has_vpid(get_vmcs12(vcpu));
1100}
1101
1102/*
Sean Christophersonea79a752020-02-04 07:32:59 -08001103 * Load guest's/host's cr3 at nested entry/exit. @nested_ept is true if we are
1104 * emulating VM-Entry into a guest with EPT enabled. On failure, the expected
1105 * Exit Qualification (for a VM-Entry consistency check VM-Exit) is assigned to
1106 * @entry_failure_code.
Sean Christopherson55d23752018-12-03 13:53:18 -08001107 */
1108static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Sean Christopherson68cda402020-05-11 15:05:29 -07001109 enum vm_entry_failure_code *entry_failure_code)
Sean Christopherson55d23752018-12-03 13:53:18 -08001110{
Sean Christopherson636e8b72021-02-03 16:01:10 -08001111 if (CC(kvm_vcpu_is_illegal_gpa(vcpu, cr3))) {
Sean Christopherson0cc69202020-05-01 21:32:26 -07001112 *entry_failure_code = ENTRY_FAIL_DEFAULT;
1113 return -EINVAL;
1114 }
Sean Christopherson55d23752018-12-03 13:53:18 -08001115
Sean Christopherson0cc69202020-05-01 21:32:26 -07001116 /*
1117 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
1118 * must not be dereferenced.
1119 */
1120 if (!nested_ept && is_pae_paging(vcpu) &&
1121 (cr3 != kvm_read_cr3(vcpu) || pdptrs_changed(vcpu))) {
1122 if (CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))) {
1123 *entry_failure_code = ENTRY_FAIL_PDPTE;
1124 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08001125 }
1126 }
1127
Sean Christopherson41fab65e2020-03-20 14:28:29 -07001128 /*
Sean Christopherson9805c5f2020-03-20 14:28:30 -07001129 * Unconditionally skip the TLB flush on fast CR3 switch, all TLB
1130 * flushes are handled by nested_vmx_transition_tlb_flush(). See
1131 * nested_vmx_transition_mmu_sync for details on skipping the MMU sync.
Sean Christopherson41fab65e2020-03-20 14:28:29 -07001132 */
Sean Christopherson55d23752018-12-03 13:53:18 -08001133 if (!nested_ept)
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07001134 kvm_mmu_new_pgd(vcpu, cr3, true,
Sean Christopherson41fab65e2020-03-20 14:28:29 -07001135 !nested_vmx_transition_mmu_sync(vcpu));
Sean Christopherson55d23752018-12-03 13:53:18 -08001136
1137 vcpu->arch.cr3 = cr3;
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07001138 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Sean Christopherson55d23752018-12-03 13:53:18 -08001139
1140 kvm_init_mmu(vcpu, false);
1141
1142 return 0;
1143}
1144
1145/*
1146 * Returns if KVM is able to config CPU to tag TLB entries
1147 * populated by L2 differently than TLB entries populated
1148 * by L1.
1149 *
Liran Alon992edea2019-11-20 14:24:52 +02001150 * If L0 uses EPT, L1 and L2 run with different EPTP because
1151 * guest_mode is part of kvm_mmu_page_role. Thus, TLB entries
1152 * are tagged with different EPTP.
Sean Christopherson55d23752018-12-03 13:53:18 -08001153 *
1154 * If L1 uses VPID and we allocated a vpid02, TLB entries are tagged
1155 * with different VPID (L1 entries are tagged with vmx->vpid
1156 * while L2 entries are tagged with vmx->nested.vpid02).
1157 */
1158static bool nested_has_guest_tlb_tag(struct kvm_vcpu *vcpu)
1159{
1160 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1161
Liran Alon992edea2019-11-20 14:24:52 +02001162 return enable_ept ||
Sean Christopherson55d23752018-12-03 13:53:18 -08001163 (nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02);
1164}
1165
Sean Christopherson50b265a2020-03-20 14:28:19 -07001166static void nested_vmx_transition_tlb_flush(struct kvm_vcpu *vcpu,
1167 struct vmcs12 *vmcs12,
1168 bool is_vmenter)
1169{
1170 struct vcpu_vmx *vmx = to_vmx(vcpu);
1171
1172 /*
1173 * If VPID is disabled, linear and combined mappings are flushed on
1174 * VM-Enter/VM-Exit, and guest-physical mappings are valid only for
1175 * their associated EPTP.
1176 */
1177 if (!enable_vpid)
1178 return;
1179
1180 /*
1181 * If vmcs12 doesn't use VPID, L1 expects linear and combined mappings
1182 * for *all* contexts to be flushed on VM-Enter/VM-Exit.
1183 *
1184 * If VPID is enabled and used by vmc12, but L2 does not have a unique
1185 * TLB tag (ASID), i.e. EPT is disabled and KVM was unable to allocate
Sean Christophersonc51e1ff2020-03-20 14:28:22 -07001186 * a VPID for L2, flush the current context as the effective ASID is
1187 * common to both L1 and L2.
Sean Christopherson50b265a2020-03-20 14:28:19 -07001188 *
1189 * Defer the flush so that it runs after vmcs02.EPTP has been set by
1190 * KVM_REQ_LOAD_MMU_PGD (if nested EPT is enabled) and to avoid
1191 * redundant flushes further down the nested pipeline.
1192 *
1193 * If a TLB flush isn't required due to any of the above, and vpid12 is
1194 * changing then the new "virtual" VPID (vpid12) will reuse the same
1195 * "real" VPID (vpid02), and so needs to be sync'd. There is no direct
1196 * mapping between vpid02 and vpid12, vpid02 is per-vCPU and reused for
1197 * all nested vCPUs.
1198 */
Sean Christophersonc51e1ff2020-03-20 14:28:22 -07001199 if (!nested_cpu_has_vpid(vmcs12)) {
Sean Christopherson50b265a2020-03-20 14:28:19 -07001200 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Sean Christophersonc51e1ff2020-03-20 14:28:22 -07001201 } else if (!nested_has_guest_tlb_tag(vcpu)) {
1202 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
Sean Christopherson50b265a2020-03-20 14:28:19 -07001203 } else if (is_vmenter &&
1204 vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
1205 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
1206 vpid_sync_context(nested_get_vpid02(vcpu));
1207 }
1208}
1209
Sean Christopherson55d23752018-12-03 13:53:18 -08001210static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
1211{
1212 superset &= mask;
1213 subset &= mask;
1214
1215 return (superset | subset) == superset;
1216}
1217
1218static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
1219{
1220 const u64 feature_and_reserved =
1221 /* feature (except bit 48; see below) */
1222 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
1223 /* reserved */
1224 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
1225 u64 vmx_basic = vmx->nested.msrs.basic;
1226
1227 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
1228 return -EINVAL;
1229
1230 /*
1231 * KVM does not emulate a version of VMX that constrains physical
1232 * addresses of VMX structures (e.g. VMCS) to 32-bits.
1233 */
1234 if (data & BIT_ULL(48))
1235 return -EINVAL;
1236
1237 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
1238 vmx_basic_vmcs_revision_id(data))
1239 return -EINVAL;
1240
1241 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
1242 return -EINVAL;
1243
1244 vmx->nested.msrs.basic = data;
1245 return 0;
1246}
1247
1248static int
1249vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
1250{
1251 u64 supported;
1252 u32 *lowp, *highp;
1253
1254 switch (msr_index) {
1255 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
1256 lowp = &vmx->nested.msrs.pinbased_ctls_low;
1257 highp = &vmx->nested.msrs.pinbased_ctls_high;
1258 break;
1259 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
1260 lowp = &vmx->nested.msrs.procbased_ctls_low;
1261 highp = &vmx->nested.msrs.procbased_ctls_high;
1262 break;
1263 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
1264 lowp = &vmx->nested.msrs.exit_ctls_low;
1265 highp = &vmx->nested.msrs.exit_ctls_high;
1266 break;
1267 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
1268 lowp = &vmx->nested.msrs.entry_ctls_low;
1269 highp = &vmx->nested.msrs.entry_ctls_high;
1270 break;
1271 case MSR_IA32_VMX_PROCBASED_CTLS2:
1272 lowp = &vmx->nested.msrs.secondary_ctls_low;
1273 highp = &vmx->nested.msrs.secondary_ctls_high;
1274 break;
1275 default:
1276 BUG();
1277 }
1278
1279 supported = vmx_control_msr(*lowp, *highp);
1280
1281 /* Check must-be-1 bits are still 1. */
1282 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
1283 return -EINVAL;
1284
1285 /* Check must-be-0 bits are still 0. */
1286 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
1287 return -EINVAL;
1288
1289 *lowp = data;
1290 *highp = data >> 32;
1291 return 0;
1292}
1293
1294static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
1295{
1296 const u64 feature_and_reserved_bits =
1297 /* feature */
1298 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
1299 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
1300 /* reserved */
1301 GENMASK_ULL(13, 9) | BIT_ULL(31);
1302 u64 vmx_misc;
1303
1304 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
1305 vmx->nested.msrs.misc_high);
1306
1307 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
1308 return -EINVAL;
1309
1310 if ((vmx->nested.msrs.pinbased_ctls_high &
1311 PIN_BASED_VMX_PREEMPTION_TIMER) &&
1312 vmx_misc_preemption_timer_rate(data) !=
1313 vmx_misc_preemption_timer_rate(vmx_misc))
1314 return -EINVAL;
1315
1316 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
1317 return -EINVAL;
1318
1319 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
1320 return -EINVAL;
1321
1322 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
1323 return -EINVAL;
1324
1325 vmx->nested.msrs.misc_low = data;
1326 vmx->nested.msrs.misc_high = data >> 32;
1327
Sean Christopherson55d23752018-12-03 13:53:18 -08001328 return 0;
1329}
1330
1331static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
1332{
1333 u64 vmx_ept_vpid_cap;
1334
1335 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
1336 vmx->nested.msrs.vpid_caps);
1337
1338 /* Every bit is either reserved or a feature bit. */
1339 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
1340 return -EINVAL;
1341
1342 vmx->nested.msrs.ept_caps = data;
1343 vmx->nested.msrs.vpid_caps = data >> 32;
1344 return 0;
1345}
1346
1347static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
1348{
1349 u64 *msr;
1350
1351 switch (msr_index) {
1352 case MSR_IA32_VMX_CR0_FIXED0:
1353 msr = &vmx->nested.msrs.cr0_fixed0;
1354 break;
1355 case MSR_IA32_VMX_CR4_FIXED0:
1356 msr = &vmx->nested.msrs.cr4_fixed0;
1357 break;
1358 default:
1359 BUG();
1360 }
1361
1362 /*
1363 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
1364 * must be 1 in the restored value.
1365 */
1366 if (!is_bitwise_subset(data, *msr, -1ULL))
1367 return -EINVAL;
1368
1369 *msr = data;
1370 return 0;
1371}
1372
1373/*
1374 * Called when userspace is restoring VMX MSRs.
1375 *
1376 * Returns 0 on success, non-0 otherwise.
1377 */
1378int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1379{
1380 struct vcpu_vmx *vmx = to_vmx(vcpu);
1381
1382 /*
1383 * Don't allow changes to the VMX capability MSRs while the vCPU
1384 * is in VMX operation.
1385 */
1386 if (vmx->nested.vmxon)
1387 return -EBUSY;
1388
1389 switch (msr_index) {
1390 case MSR_IA32_VMX_BASIC:
1391 return vmx_restore_vmx_basic(vmx, data);
1392 case MSR_IA32_VMX_PINBASED_CTLS:
1393 case MSR_IA32_VMX_PROCBASED_CTLS:
1394 case MSR_IA32_VMX_EXIT_CTLS:
1395 case MSR_IA32_VMX_ENTRY_CTLS:
1396 /*
1397 * The "non-true" VMX capability MSRs are generated from the
1398 * "true" MSRs, so we do not support restoring them directly.
1399 *
1400 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
1401 * should restore the "true" MSRs with the must-be-1 bits
1402 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
1403 * DEFAULT SETTINGS".
1404 */
1405 return -EINVAL;
1406 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
1407 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
1408 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
1409 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
1410 case MSR_IA32_VMX_PROCBASED_CTLS2:
1411 return vmx_restore_control_msr(vmx, msr_index, data);
1412 case MSR_IA32_VMX_MISC:
1413 return vmx_restore_vmx_misc(vmx, data);
1414 case MSR_IA32_VMX_CR0_FIXED0:
1415 case MSR_IA32_VMX_CR4_FIXED0:
1416 return vmx_restore_fixed0_msr(vmx, msr_index, data);
1417 case MSR_IA32_VMX_CR0_FIXED1:
1418 case MSR_IA32_VMX_CR4_FIXED1:
1419 /*
1420 * These MSRs are generated based on the vCPU's CPUID, so we
1421 * do not support restoring them directly.
1422 */
1423 return -EINVAL;
1424 case MSR_IA32_VMX_EPT_VPID_CAP:
1425 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
1426 case MSR_IA32_VMX_VMCS_ENUM:
1427 vmx->nested.msrs.vmcs_enum = data;
1428 return 0;
Paolo Bonzinie8a70bd2019-07-02 14:40:40 +02001429 case MSR_IA32_VMX_VMFUNC:
1430 if (data & ~vmx->nested.msrs.vmfunc_controls)
1431 return -EINVAL;
1432 vmx->nested.msrs.vmfunc_controls = data;
1433 return 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08001434 default:
1435 /*
1436 * The rest of the VMX capability MSRs do not support restore.
1437 */
1438 return -EINVAL;
1439 }
1440}
1441
1442/* Returns 0 on success, non-0 otherwise. */
1443int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
1444{
1445 switch (msr_index) {
1446 case MSR_IA32_VMX_BASIC:
1447 *pdata = msrs->basic;
1448 break;
1449 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
1450 case MSR_IA32_VMX_PINBASED_CTLS:
1451 *pdata = vmx_control_msr(
1452 msrs->pinbased_ctls_low,
1453 msrs->pinbased_ctls_high);
1454 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
1455 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
1456 break;
1457 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
1458 case MSR_IA32_VMX_PROCBASED_CTLS:
1459 *pdata = vmx_control_msr(
1460 msrs->procbased_ctls_low,
1461 msrs->procbased_ctls_high);
1462 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
1463 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
1464 break;
1465 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
1466 case MSR_IA32_VMX_EXIT_CTLS:
1467 *pdata = vmx_control_msr(
1468 msrs->exit_ctls_low,
1469 msrs->exit_ctls_high);
1470 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
1471 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
1472 break;
1473 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
1474 case MSR_IA32_VMX_ENTRY_CTLS:
1475 *pdata = vmx_control_msr(
1476 msrs->entry_ctls_low,
1477 msrs->entry_ctls_high);
1478 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
1479 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
1480 break;
1481 case MSR_IA32_VMX_MISC:
1482 *pdata = vmx_control_msr(
1483 msrs->misc_low,
1484 msrs->misc_high);
1485 break;
1486 case MSR_IA32_VMX_CR0_FIXED0:
1487 *pdata = msrs->cr0_fixed0;
1488 break;
1489 case MSR_IA32_VMX_CR0_FIXED1:
1490 *pdata = msrs->cr0_fixed1;
1491 break;
1492 case MSR_IA32_VMX_CR4_FIXED0:
1493 *pdata = msrs->cr4_fixed0;
1494 break;
1495 case MSR_IA32_VMX_CR4_FIXED1:
1496 *pdata = msrs->cr4_fixed1;
1497 break;
1498 case MSR_IA32_VMX_VMCS_ENUM:
1499 *pdata = msrs->vmcs_enum;
1500 break;
1501 case MSR_IA32_VMX_PROCBASED_CTLS2:
1502 *pdata = vmx_control_msr(
1503 msrs->secondary_ctls_low,
1504 msrs->secondary_ctls_high);
1505 break;
1506 case MSR_IA32_VMX_EPT_VPID_CAP:
1507 *pdata = msrs->ept_caps |
1508 ((u64)msrs->vpid_caps << 32);
1509 break;
1510 case MSR_IA32_VMX_VMFUNC:
1511 *pdata = msrs->vmfunc_controls;
1512 break;
1513 default:
1514 return 1;
1515 }
1516
1517 return 0;
1518}
1519
1520/*
Sean Christophersonfadcead2019-05-07 08:36:23 -07001521 * Copy the writable VMCS shadow fields back to the VMCS12, in case they have
1522 * been modified by the L1 guest. Note, "writable" in this context means
1523 * "writable by the guest", i.e. tagged SHADOW_FIELD_RW; the set of
1524 * fields tagged SHADOW_FIELD_RO may or may not align with the "read-only"
1525 * VM-exit information fields (which are actually writable if the vCPU is
1526 * configured to support "VMWRITE to any supported field in the VMCS").
Sean Christopherson55d23752018-12-03 13:53:18 -08001527 */
1528static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
1529{
Sean Christopherson55d23752018-12-03 13:53:18 -08001530 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Sean Christophersonfadcead2019-05-07 08:36:23 -07001531 struct vmcs12 *vmcs12 = get_vmcs12(&vmx->vcpu);
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07001532 struct shadow_vmcs_field field;
1533 unsigned long val;
Sean Christophersonfadcead2019-05-07 08:36:23 -07001534 int i;
Sean Christopherson55d23752018-12-03 13:53:18 -08001535
Paolo Bonzini88dddc12019-07-19 18:41:10 +02001536 if (WARN_ON(!shadow_vmcs))
1537 return;
1538
Sean Christopherson55d23752018-12-03 13:53:18 -08001539 preempt_disable();
1540
1541 vmcs_load(shadow_vmcs);
1542
Sean Christophersonfadcead2019-05-07 08:36:23 -07001543 for (i = 0; i < max_shadow_read_write_fields; i++) {
1544 field = shadow_read_write_fields[i];
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07001545 val = __vmcs_readl(field.encoding);
1546 vmcs12_write_any(vmcs12, field.encoding, field.offset, val);
Sean Christopherson55d23752018-12-03 13:53:18 -08001547 }
1548
1549 vmcs_clear(shadow_vmcs);
1550 vmcs_load(vmx->loaded_vmcs->vmcs);
1551
1552 preempt_enable();
1553}
1554
1555static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
1556{
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07001557 const struct shadow_vmcs_field *fields[] = {
Sean Christopherson55d23752018-12-03 13:53:18 -08001558 shadow_read_write_fields,
1559 shadow_read_only_fields
1560 };
1561 const int max_fields[] = {
1562 max_shadow_read_write_fields,
1563 max_shadow_read_only_fields
1564 };
Sean Christopherson55d23752018-12-03 13:53:18 -08001565 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07001566 struct vmcs12 *vmcs12 = get_vmcs12(&vmx->vcpu);
1567 struct shadow_vmcs_field field;
1568 unsigned long val;
1569 int i, q;
Sean Christopherson55d23752018-12-03 13:53:18 -08001570
Paolo Bonzini88dddc12019-07-19 18:41:10 +02001571 if (WARN_ON(!shadow_vmcs))
1572 return;
1573
Sean Christopherson55d23752018-12-03 13:53:18 -08001574 vmcs_load(shadow_vmcs);
1575
1576 for (q = 0; q < ARRAY_SIZE(fields); q++) {
1577 for (i = 0; i < max_fields[q]; i++) {
1578 field = fields[q][i];
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07001579 val = vmcs12_read_any(vmcs12, field.encoding,
1580 field.offset);
1581 __vmcs_writel(field.encoding, val);
Sean Christopherson55d23752018-12-03 13:53:18 -08001582 }
1583 }
1584
1585 vmcs_clear(shadow_vmcs);
1586 vmcs_load(vmx->loaded_vmcs->vmcs);
1587}
1588
1589static int copy_enlightened_to_vmcs12(struct vcpu_vmx *vmx)
1590{
1591 struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12;
1592 struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
1593
1594 /* HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE */
1595 vmcs12->tpr_threshold = evmcs->tpr_threshold;
1596 vmcs12->guest_rip = evmcs->guest_rip;
1597
1598 if (unlikely(!(evmcs->hv_clean_fields &
1599 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC))) {
1600 vmcs12->guest_rsp = evmcs->guest_rsp;
1601 vmcs12->guest_rflags = evmcs->guest_rflags;
1602 vmcs12->guest_interruptibility_info =
1603 evmcs->guest_interruptibility_info;
1604 }
1605
1606 if (unlikely(!(evmcs->hv_clean_fields &
1607 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC))) {
1608 vmcs12->cpu_based_vm_exec_control =
1609 evmcs->cpu_based_vm_exec_control;
1610 }
1611
1612 if (unlikely(!(evmcs->hv_clean_fields &
Vitaly Kuznetsovf9bc5222019-06-13 13:35:02 +02001613 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EXCPN))) {
Sean Christopherson55d23752018-12-03 13:53:18 -08001614 vmcs12->exception_bitmap = evmcs->exception_bitmap;
1615 }
1616
1617 if (unlikely(!(evmcs->hv_clean_fields &
1618 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_ENTRY))) {
1619 vmcs12->vm_entry_controls = evmcs->vm_entry_controls;
1620 }
1621
1622 if (unlikely(!(evmcs->hv_clean_fields &
1623 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT))) {
1624 vmcs12->vm_entry_intr_info_field =
1625 evmcs->vm_entry_intr_info_field;
1626 vmcs12->vm_entry_exception_error_code =
1627 evmcs->vm_entry_exception_error_code;
1628 vmcs12->vm_entry_instruction_len =
1629 evmcs->vm_entry_instruction_len;
1630 }
1631
1632 if (unlikely(!(evmcs->hv_clean_fields &
1633 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1))) {
1634 vmcs12->host_ia32_pat = evmcs->host_ia32_pat;
1635 vmcs12->host_ia32_efer = evmcs->host_ia32_efer;
1636 vmcs12->host_cr0 = evmcs->host_cr0;
1637 vmcs12->host_cr3 = evmcs->host_cr3;
1638 vmcs12->host_cr4 = evmcs->host_cr4;
1639 vmcs12->host_ia32_sysenter_esp = evmcs->host_ia32_sysenter_esp;
1640 vmcs12->host_ia32_sysenter_eip = evmcs->host_ia32_sysenter_eip;
1641 vmcs12->host_rip = evmcs->host_rip;
1642 vmcs12->host_ia32_sysenter_cs = evmcs->host_ia32_sysenter_cs;
1643 vmcs12->host_es_selector = evmcs->host_es_selector;
1644 vmcs12->host_cs_selector = evmcs->host_cs_selector;
1645 vmcs12->host_ss_selector = evmcs->host_ss_selector;
1646 vmcs12->host_ds_selector = evmcs->host_ds_selector;
1647 vmcs12->host_fs_selector = evmcs->host_fs_selector;
1648 vmcs12->host_gs_selector = evmcs->host_gs_selector;
1649 vmcs12->host_tr_selector = evmcs->host_tr_selector;
1650 }
1651
1652 if (unlikely(!(evmcs->hv_clean_fields &
Vitaly Kuznetsovf9bc5222019-06-13 13:35:02 +02001653 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1))) {
Sean Christopherson55d23752018-12-03 13:53:18 -08001654 vmcs12->pin_based_vm_exec_control =
1655 evmcs->pin_based_vm_exec_control;
1656 vmcs12->vm_exit_controls = evmcs->vm_exit_controls;
1657 vmcs12->secondary_vm_exec_control =
1658 evmcs->secondary_vm_exec_control;
1659 }
1660
1661 if (unlikely(!(evmcs->hv_clean_fields &
1662 HV_VMX_ENLIGHTENED_CLEAN_FIELD_IO_BITMAP))) {
1663 vmcs12->io_bitmap_a = evmcs->io_bitmap_a;
1664 vmcs12->io_bitmap_b = evmcs->io_bitmap_b;
1665 }
1666
1667 if (unlikely(!(evmcs->hv_clean_fields &
1668 HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP))) {
1669 vmcs12->msr_bitmap = evmcs->msr_bitmap;
1670 }
1671
1672 if (unlikely(!(evmcs->hv_clean_fields &
1673 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2))) {
1674 vmcs12->guest_es_base = evmcs->guest_es_base;
1675 vmcs12->guest_cs_base = evmcs->guest_cs_base;
1676 vmcs12->guest_ss_base = evmcs->guest_ss_base;
1677 vmcs12->guest_ds_base = evmcs->guest_ds_base;
1678 vmcs12->guest_fs_base = evmcs->guest_fs_base;
1679 vmcs12->guest_gs_base = evmcs->guest_gs_base;
1680 vmcs12->guest_ldtr_base = evmcs->guest_ldtr_base;
1681 vmcs12->guest_tr_base = evmcs->guest_tr_base;
1682 vmcs12->guest_gdtr_base = evmcs->guest_gdtr_base;
1683 vmcs12->guest_idtr_base = evmcs->guest_idtr_base;
1684 vmcs12->guest_es_limit = evmcs->guest_es_limit;
1685 vmcs12->guest_cs_limit = evmcs->guest_cs_limit;
1686 vmcs12->guest_ss_limit = evmcs->guest_ss_limit;
1687 vmcs12->guest_ds_limit = evmcs->guest_ds_limit;
1688 vmcs12->guest_fs_limit = evmcs->guest_fs_limit;
1689 vmcs12->guest_gs_limit = evmcs->guest_gs_limit;
1690 vmcs12->guest_ldtr_limit = evmcs->guest_ldtr_limit;
1691 vmcs12->guest_tr_limit = evmcs->guest_tr_limit;
1692 vmcs12->guest_gdtr_limit = evmcs->guest_gdtr_limit;
1693 vmcs12->guest_idtr_limit = evmcs->guest_idtr_limit;
1694 vmcs12->guest_es_ar_bytes = evmcs->guest_es_ar_bytes;
1695 vmcs12->guest_cs_ar_bytes = evmcs->guest_cs_ar_bytes;
1696 vmcs12->guest_ss_ar_bytes = evmcs->guest_ss_ar_bytes;
1697 vmcs12->guest_ds_ar_bytes = evmcs->guest_ds_ar_bytes;
1698 vmcs12->guest_fs_ar_bytes = evmcs->guest_fs_ar_bytes;
1699 vmcs12->guest_gs_ar_bytes = evmcs->guest_gs_ar_bytes;
1700 vmcs12->guest_ldtr_ar_bytes = evmcs->guest_ldtr_ar_bytes;
1701 vmcs12->guest_tr_ar_bytes = evmcs->guest_tr_ar_bytes;
1702 vmcs12->guest_es_selector = evmcs->guest_es_selector;
1703 vmcs12->guest_cs_selector = evmcs->guest_cs_selector;
1704 vmcs12->guest_ss_selector = evmcs->guest_ss_selector;
1705 vmcs12->guest_ds_selector = evmcs->guest_ds_selector;
1706 vmcs12->guest_fs_selector = evmcs->guest_fs_selector;
1707 vmcs12->guest_gs_selector = evmcs->guest_gs_selector;
1708 vmcs12->guest_ldtr_selector = evmcs->guest_ldtr_selector;
1709 vmcs12->guest_tr_selector = evmcs->guest_tr_selector;
1710 }
1711
1712 if (unlikely(!(evmcs->hv_clean_fields &
1713 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2))) {
1714 vmcs12->tsc_offset = evmcs->tsc_offset;
1715 vmcs12->virtual_apic_page_addr = evmcs->virtual_apic_page_addr;
1716 vmcs12->xss_exit_bitmap = evmcs->xss_exit_bitmap;
1717 }
1718
1719 if (unlikely(!(evmcs->hv_clean_fields &
1720 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR))) {
1721 vmcs12->cr0_guest_host_mask = evmcs->cr0_guest_host_mask;
1722 vmcs12->cr4_guest_host_mask = evmcs->cr4_guest_host_mask;
1723 vmcs12->cr0_read_shadow = evmcs->cr0_read_shadow;
1724 vmcs12->cr4_read_shadow = evmcs->cr4_read_shadow;
1725 vmcs12->guest_cr0 = evmcs->guest_cr0;
1726 vmcs12->guest_cr3 = evmcs->guest_cr3;
1727 vmcs12->guest_cr4 = evmcs->guest_cr4;
1728 vmcs12->guest_dr7 = evmcs->guest_dr7;
1729 }
1730
1731 if (unlikely(!(evmcs->hv_clean_fields &
1732 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER))) {
1733 vmcs12->host_fs_base = evmcs->host_fs_base;
1734 vmcs12->host_gs_base = evmcs->host_gs_base;
1735 vmcs12->host_tr_base = evmcs->host_tr_base;
1736 vmcs12->host_gdtr_base = evmcs->host_gdtr_base;
1737 vmcs12->host_idtr_base = evmcs->host_idtr_base;
1738 vmcs12->host_rsp = evmcs->host_rsp;
1739 }
1740
1741 if (unlikely(!(evmcs->hv_clean_fields &
1742 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_XLAT))) {
1743 vmcs12->ept_pointer = evmcs->ept_pointer;
1744 vmcs12->virtual_processor_id = evmcs->virtual_processor_id;
1745 }
1746
1747 if (unlikely(!(evmcs->hv_clean_fields &
1748 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1))) {
1749 vmcs12->vmcs_link_pointer = evmcs->vmcs_link_pointer;
1750 vmcs12->guest_ia32_debugctl = evmcs->guest_ia32_debugctl;
1751 vmcs12->guest_ia32_pat = evmcs->guest_ia32_pat;
1752 vmcs12->guest_ia32_efer = evmcs->guest_ia32_efer;
1753 vmcs12->guest_pdptr0 = evmcs->guest_pdptr0;
1754 vmcs12->guest_pdptr1 = evmcs->guest_pdptr1;
1755 vmcs12->guest_pdptr2 = evmcs->guest_pdptr2;
1756 vmcs12->guest_pdptr3 = evmcs->guest_pdptr3;
1757 vmcs12->guest_pending_dbg_exceptions =
1758 evmcs->guest_pending_dbg_exceptions;
1759 vmcs12->guest_sysenter_esp = evmcs->guest_sysenter_esp;
1760 vmcs12->guest_sysenter_eip = evmcs->guest_sysenter_eip;
1761 vmcs12->guest_bndcfgs = evmcs->guest_bndcfgs;
1762 vmcs12->guest_activity_state = evmcs->guest_activity_state;
1763 vmcs12->guest_sysenter_cs = evmcs->guest_sysenter_cs;
1764 }
1765
1766 /*
1767 * Not used?
1768 * vmcs12->vm_exit_msr_store_addr = evmcs->vm_exit_msr_store_addr;
1769 * vmcs12->vm_exit_msr_load_addr = evmcs->vm_exit_msr_load_addr;
1770 * vmcs12->vm_entry_msr_load_addr = evmcs->vm_entry_msr_load_addr;
Sean Christopherson55d23752018-12-03 13:53:18 -08001771 * vmcs12->page_fault_error_code_mask =
1772 * evmcs->page_fault_error_code_mask;
1773 * vmcs12->page_fault_error_code_match =
1774 * evmcs->page_fault_error_code_match;
1775 * vmcs12->cr3_target_count = evmcs->cr3_target_count;
1776 * vmcs12->vm_exit_msr_store_count = evmcs->vm_exit_msr_store_count;
1777 * vmcs12->vm_exit_msr_load_count = evmcs->vm_exit_msr_load_count;
1778 * vmcs12->vm_entry_msr_load_count = evmcs->vm_entry_msr_load_count;
1779 */
1780
1781 /*
1782 * Read only fields:
1783 * vmcs12->guest_physical_address = evmcs->guest_physical_address;
1784 * vmcs12->vm_instruction_error = evmcs->vm_instruction_error;
1785 * vmcs12->vm_exit_reason = evmcs->vm_exit_reason;
1786 * vmcs12->vm_exit_intr_info = evmcs->vm_exit_intr_info;
1787 * vmcs12->vm_exit_intr_error_code = evmcs->vm_exit_intr_error_code;
1788 * vmcs12->idt_vectoring_info_field = evmcs->idt_vectoring_info_field;
1789 * vmcs12->idt_vectoring_error_code = evmcs->idt_vectoring_error_code;
1790 * vmcs12->vm_exit_instruction_len = evmcs->vm_exit_instruction_len;
1791 * vmcs12->vmx_instruction_info = evmcs->vmx_instruction_info;
1792 * vmcs12->exit_qualification = evmcs->exit_qualification;
1793 * vmcs12->guest_linear_address = evmcs->guest_linear_address;
1794 *
1795 * Not present in struct vmcs12:
1796 * vmcs12->exit_io_instruction_ecx = evmcs->exit_io_instruction_ecx;
1797 * vmcs12->exit_io_instruction_esi = evmcs->exit_io_instruction_esi;
1798 * vmcs12->exit_io_instruction_edi = evmcs->exit_io_instruction_edi;
1799 * vmcs12->exit_io_instruction_eip = evmcs->exit_io_instruction_eip;
1800 */
1801
1802 return 0;
1803}
1804
1805static int copy_vmcs12_to_enlightened(struct vcpu_vmx *vmx)
1806{
1807 struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12;
1808 struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
1809
1810 /*
1811 * Should not be changed by KVM:
1812 *
1813 * evmcs->host_es_selector = vmcs12->host_es_selector;
1814 * evmcs->host_cs_selector = vmcs12->host_cs_selector;
1815 * evmcs->host_ss_selector = vmcs12->host_ss_selector;
1816 * evmcs->host_ds_selector = vmcs12->host_ds_selector;
1817 * evmcs->host_fs_selector = vmcs12->host_fs_selector;
1818 * evmcs->host_gs_selector = vmcs12->host_gs_selector;
1819 * evmcs->host_tr_selector = vmcs12->host_tr_selector;
1820 * evmcs->host_ia32_pat = vmcs12->host_ia32_pat;
1821 * evmcs->host_ia32_efer = vmcs12->host_ia32_efer;
1822 * evmcs->host_cr0 = vmcs12->host_cr0;
1823 * evmcs->host_cr3 = vmcs12->host_cr3;
1824 * evmcs->host_cr4 = vmcs12->host_cr4;
1825 * evmcs->host_ia32_sysenter_esp = vmcs12->host_ia32_sysenter_esp;
1826 * evmcs->host_ia32_sysenter_eip = vmcs12->host_ia32_sysenter_eip;
1827 * evmcs->host_rip = vmcs12->host_rip;
1828 * evmcs->host_ia32_sysenter_cs = vmcs12->host_ia32_sysenter_cs;
1829 * evmcs->host_fs_base = vmcs12->host_fs_base;
1830 * evmcs->host_gs_base = vmcs12->host_gs_base;
1831 * evmcs->host_tr_base = vmcs12->host_tr_base;
1832 * evmcs->host_gdtr_base = vmcs12->host_gdtr_base;
1833 * evmcs->host_idtr_base = vmcs12->host_idtr_base;
1834 * evmcs->host_rsp = vmcs12->host_rsp;
Sean Christopherson3731905ef2019-05-07 08:36:27 -07001835 * sync_vmcs02_to_vmcs12() doesn't read these:
Sean Christopherson55d23752018-12-03 13:53:18 -08001836 * evmcs->io_bitmap_a = vmcs12->io_bitmap_a;
1837 * evmcs->io_bitmap_b = vmcs12->io_bitmap_b;
1838 * evmcs->msr_bitmap = vmcs12->msr_bitmap;
1839 * evmcs->ept_pointer = vmcs12->ept_pointer;
1840 * evmcs->xss_exit_bitmap = vmcs12->xss_exit_bitmap;
1841 * evmcs->vm_exit_msr_store_addr = vmcs12->vm_exit_msr_store_addr;
1842 * evmcs->vm_exit_msr_load_addr = vmcs12->vm_exit_msr_load_addr;
1843 * evmcs->vm_entry_msr_load_addr = vmcs12->vm_entry_msr_load_addr;
Sean Christopherson55d23752018-12-03 13:53:18 -08001844 * evmcs->tpr_threshold = vmcs12->tpr_threshold;
1845 * evmcs->virtual_processor_id = vmcs12->virtual_processor_id;
1846 * evmcs->exception_bitmap = vmcs12->exception_bitmap;
1847 * evmcs->vmcs_link_pointer = vmcs12->vmcs_link_pointer;
1848 * evmcs->pin_based_vm_exec_control = vmcs12->pin_based_vm_exec_control;
1849 * evmcs->vm_exit_controls = vmcs12->vm_exit_controls;
1850 * evmcs->secondary_vm_exec_control = vmcs12->secondary_vm_exec_control;
1851 * evmcs->page_fault_error_code_mask =
1852 * vmcs12->page_fault_error_code_mask;
1853 * evmcs->page_fault_error_code_match =
1854 * vmcs12->page_fault_error_code_match;
1855 * evmcs->cr3_target_count = vmcs12->cr3_target_count;
1856 * evmcs->virtual_apic_page_addr = vmcs12->virtual_apic_page_addr;
1857 * evmcs->tsc_offset = vmcs12->tsc_offset;
1858 * evmcs->guest_ia32_debugctl = vmcs12->guest_ia32_debugctl;
1859 * evmcs->cr0_guest_host_mask = vmcs12->cr0_guest_host_mask;
1860 * evmcs->cr4_guest_host_mask = vmcs12->cr4_guest_host_mask;
1861 * evmcs->cr0_read_shadow = vmcs12->cr0_read_shadow;
1862 * evmcs->cr4_read_shadow = vmcs12->cr4_read_shadow;
1863 * evmcs->vm_exit_msr_store_count = vmcs12->vm_exit_msr_store_count;
1864 * evmcs->vm_exit_msr_load_count = vmcs12->vm_exit_msr_load_count;
1865 * evmcs->vm_entry_msr_load_count = vmcs12->vm_entry_msr_load_count;
1866 *
1867 * Not present in struct vmcs12:
1868 * evmcs->exit_io_instruction_ecx = vmcs12->exit_io_instruction_ecx;
1869 * evmcs->exit_io_instruction_esi = vmcs12->exit_io_instruction_esi;
1870 * evmcs->exit_io_instruction_edi = vmcs12->exit_io_instruction_edi;
1871 * evmcs->exit_io_instruction_eip = vmcs12->exit_io_instruction_eip;
1872 */
1873
1874 evmcs->guest_es_selector = vmcs12->guest_es_selector;
1875 evmcs->guest_cs_selector = vmcs12->guest_cs_selector;
1876 evmcs->guest_ss_selector = vmcs12->guest_ss_selector;
1877 evmcs->guest_ds_selector = vmcs12->guest_ds_selector;
1878 evmcs->guest_fs_selector = vmcs12->guest_fs_selector;
1879 evmcs->guest_gs_selector = vmcs12->guest_gs_selector;
1880 evmcs->guest_ldtr_selector = vmcs12->guest_ldtr_selector;
1881 evmcs->guest_tr_selector = vmcs12->guest_tr_selector;
1882
1883 evmcs->guest_es_limit = vmcs12->guest_es_limit;
1884 evmcs->guest_cs_limit = vmcs12->guest_cs_limit;
1885 evmcs->guest_ss_limit = vmcs12->guest_ss_limit;
1886 evmcs->guest_ds_limit = vmcs12->guest_ds_limit;
1887 evmcs->guest_fs_limit = vmcs12->guest_fs_limit;
1888 evmcs->guest_gs_limit = vmcs12->guest_gs_limit;
1889 evmcs->guest_ldtr_limit = vmcs12->guest_ldtr_limit;
1890 evmcs->guest_tr_limit = vmcs12->guest_tr_limit;
1891 evmcs->guest_gdtr_limit = vmcs12->guest_gdtr_limit;
1892 evmcs->guest_idtr_limit = vmcs12->guest_idtr_limit;
1893
1894 evmcs->guest_es_ar_bytes = vmcs12->guest_es_ar_bytes;
1895 evmcs->guest_cs_ar_bytes = vmcs12->guest_cs_ar_bytes;
1896 evmcs->guest_ss_ar_bytes = vmcs12->guest_ss_ar_bytes;
1897 evmcs->guest_ds_ar_bytes = vmcs12->guest_ds_ar_bytes;
1898 evmcs->guest_fs_ar_bytes = vmcs12->guest_fs_ar_bytes;
1899 evmcs->guest_gs_ar_bytes = vmcs12->guest_gs_ar_bytes;
1900 evmcs->guest_ldtr_ar_bytes = vmcs12->guest_ldtr_ar_bytes;
1901 evmcs->guest_tr_ar_bytes = vmcs12->guest_tr_ar_bytes;
1902
1903 evmcs->guest_es_base = vmcs12->guest_es_base;
1904 evmcs->guest_cs_base = vmcs12->guest_cs_base;
1905 evmcs->guest_ss_base = vmcs12->guest_ss_base;
1906 evmcs->guest_ds_base = vmcs12->guest_ds_base;
1907 evmcs->guest_fs_base = vmcs12->guest_fs_base;
1908 evmcs->guest_gs_base = vmcs12->guest_gs_base;
1909 evmcs->guest_ldtr_base = vmcs12->guest_ldtr_base;
1910 evmcs->guest_tr_base = vmcs12->guest_tr_base;
1911 evmcs->guest_gdtr_base = vmcs12->guest_gdtr_base;
1912 evmcs->guest_idtr_base = vmcs12->guest_idtr_base;
1913
1914 evmcs->guest_ia32_pat = vmcs12->guest_ia32_pat;
1915 evmcs->guest_ia32_efer = vmcs12->guest_ia32_efer;
1916
1917 evmcs->guest_pdptr0 = vmcs12->guest_pdptr0;
1918 evmcs->guest_pdptr1 = vmcs12->guest_pdptr1;
1919 evmcs->guest_pdptr2 = vmcs12->guest_pdptr2;
1920 evmcs->guest_pdptr3 = vmcs12->guest_pdptr3;
1921
1922 evmcs->guest_pending_dbg_exceptions =
1923 vmcs12->guest_pending_dbg_exceptions;
1924 evmcs->guest_sysenter_esp = vmcs12->guest_sysenter_esp;
1925 evmcs->guest_sysenter_eip = vmcs12->guest_sysenter_eip;
1926
1927 evmcs->guest_activity_state = vmcs12->guest_activity_state;
1928 evmcs->guest_sysenter_cs = vmcs12->guest_sysenter_cs;
1929
1930 evmcs->guest_cr0 = vmcs12->guest_cr0;
1931 evmcs->guest_cr3 = vmcs12->guest_cr3;
1932 evmcs->guest_cr4 = vmcs12->guest_cr4;
1933 evmcs->guest_dr7 = vmcs12->guest_dr7;
1934
1935 evmcs->guest_physical_address = vmcs12->guest_physical_address;
1936
1937 evmcs->vm_instruction_error = vmcs12->vm_instruction_error;
1938 evmcs->vm_exit_reason = vmcs12->vm_exit_reason;
1939 evmcs->vm_exit_intr_info = vmcs12->vm_exit_intr_info;
1940 evmcs->vm_exit_intr_error_code = vmcs12->vm_exit_intr_error_code;
1941 evmcs->idt_vectoring_info_field = vmcs12->idt_vectoring_info_field;
1942 evmcs->idt_vectoring_error_code = vmcs12->idt_vectoring_error_code;
1943 evmcs->vm_exit_instruction_len = vmcs12->vm_exit_instruction_len;
1944 evmcs->vmx_instruction_info = vmcs12->vmx_instruction_info;
1945
1946 evmcs->exit_qualification = vmcs12->exit_qualification;
1947
1948 evmcs->guest_linear_address = vmcs12->guest_linear_address;
1949 evmcs->guest_rsp = vmcs12->guest_rsp;
1950 evmcs->guest_rflags = vmcs12->guest_rflags;
1951
1952 evmcs->guest_interruptibility_info =
1953 vmcs12->guest_interruptibility_info;
1954 evmcs->cpu_based_vm_exec_control = vmcs12->cpu_based_vm_exec_control;
1955 evmcs->vm_entry_controls = vmcs12->vm_entry_controls;
1956 evmcs->vm_entry_intr_info_field = vmcs12->vm_entry_intr_info_field;
1957 evmcs->vm_entry_exception_error_code =
1958 vmcs12->vm_entry_exception_error_code;
1959 evmcs->vm_entry_instruction_len = vmcs12->vm_entry_instruction_len;
1960
1961 evmcs->guest_rip = vmcs12->guest_rip;
1962
1963 evmcs->guest_bndcfgs = vmcs12->guest_bndcfgs;
1964
1965 return 0;
1966}
1967
1968/*
1969 * This is an equivalent of the nested hypervisor executing the vmptrld
1970 * instruction.
1971 */
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01001972static enum nested_evmptrld_status nested_vmx_handle_enlightened_vmptrld(
1973 struct kvm_vcpu *vcpu, bool from_launch)
Sean Christopherson55d23752018-12-03 13:53:18 -08001974{
1975 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsova21a39c2019-06-28 13:23:32 +02001976 bool evmcs_gpa_changed = false;
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02001977 u64 evmcs_gpa;
Sean Christopherson55d23752018-12-03 13:53:18 -08001978
1979 if (likely(!vmx->nested.enlightened_vmcs_enabled))
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01001980 return EVMPTRLD_DISABLED;
Sean Christopherson55d23752018-12-03 13:53:18 -08001981
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02001982 if (!nested_enlightened_vmentry(vcpu, &evmcs_gpa))
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01001983 return EVMPTRLD_DISABLED;
Sean Christopherson55d23752018-12-03 13:53:18 -08001984
Vitaly Kuznetsov95fa1012020-03-09 16:52:11 +01001985 if (unlikely(!vmx->nested.hv_evmcs ||
1986 evmcs_gpa != vmx->nested.hv_evmcs_vmptr)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08001987 if (!vmx->nested.hv_evmcs)
1988 vmx->nested.current_vmptr = -1ull;
1989
1990 nested_release_evmcs(vcpu);
1991
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02001992 if (kvm_vcpu_map(vcpu, gpa_to_gfn(evmcs_gpa),
KarimAllah Ahmeddee9c042019-01-31 21:24:42 +01001993 &vmx->nested.hv_evmcs_map))
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01001994 return EVMPTRLD_ERROR;
Sean Christopherson55d23752018-12-03 13:53:18 -08001995
KarimAllah Ahmeddee9c042019-01-31 21:24:42 +01001996 vmx->nested.hv_evmcs = vmx->nested.hv_evmcs_map.hva;
Sean Christopherson55d23752018-12-03 13:53:18 -08001997
1998 /*
1999 * Currently, KVM only supports eVMCS version 1
2000 * (== KVM_EVMCS_VERSION) and thus we expect guest to set this
2001 * value to first u32 field of eVMCS which should specify eVMCS
2002 * VersionNumber.
2003 *
2004 * Guest should be aware of supported eVMCS versions by host by
2005 * examining CPUID.0x4000000A.EAX[0:15]. Host userspace VMM is
2006 * expected to set this CPUID leaf according to the value
2007 * returned in vmcs_version from nested_enable_evmcs().
2008 *
2009 * However, it turns out that Microsoft Hyper-V fails to comply
2010 * to their own invented interface: When Hyper-V use eVMCS, it
2011 * just sets first u32 field of eVMCS to revision_id specified
2012 * in MSR_IA32_VMX_BASIC. Instead of used eVMCS version number
2013 * which is one of the supported versions specified in
2014 * CPUID.0x4000000A.EAX[0:15].
2015 *
2016 * To overcome Hyper-V bug, we accept here either a supported
2017 * eVMCS version or VMCS12 revision_id as valid values for first
2018 * u32 field of eVMCS.
2019 */
2020 if ((vmx->nested.hv_evmcs->revision_id != KVM_EVMCS_VERSION) &&
2021 (vmx->nested.hv_evmcs->revision_id != VMCS12_REVISION)) {
2022 nested_release_evmcs(vcpu);
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01002023 return EVMPTRLD_VMFAIL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002024 }
2025
2026 vmx->nested.dirty_vmcs12 = true;
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02002027 vmx->nested.hv_evmcs_vmptr = evmcs_gpa;
Sean Christopherson55d23752018-12-03 13:53:18 -08002028
Vitaly Kuznetsova21a39c2019-06-28 13:23:32 +02002029 evmcs_gpa_changed = true;
Sean Christopherson55d23752018-12-03 13:53:18 -08002030 /*
2031 * Unlike normal vmcs12, enlightened vmcs12 is not fully
2032 * reloaded from guest's memory (read only fields, fields not
2033 * present in struct hv_enlightened_vmcs, ...). Make sure there
2034 * are no leftovers.
2035 */
2036 if (from_launch) {
2037 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2038 memset(vmcs12, 0, sizeof(*vmcs12));
2039 vmcs12->hdr.revision_id = VMCS12_REVISION;
2040 }
2041
2042 }
Vitaly Kuznetsova21a39c2019-06-28 13:23:32 +02002043
2044 /*
Miaohe Linffdbd502020-02-07 23:22:45 +08002045 * Clean fields data can't be used on VMLAUNCH and when we switch
Vitaly Kuznetsova21a39c2019-06-28 13:23:32 +02002046 * between different L2 guests as KVM keeps a single VMCS12 per L1.
2047 */
2048 if (from_launch || evmcs_gpa_changed)
2049 vmx->nested.hv_evmcs->hv_clean_fields &=
2050 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
2051
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01002052 return EVMPTRLD_SUCCEEDED;
Sean Christopherson55d23752018-12-03 13:53:18 -08002053}
2054
Sean Christopherson3731905ef2019-05-07 08:36:27 -07002055void nested_sync_vmcs12_to_shadow(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08002056{
2057 struct vcpu_vmx *vmx = to_vmx(vcpu);
2058
Sean Christopherson55d23752018-12-03 13:53:18 -08002059 if (vmx->nested.hv_evmcs) {
2060 copy_vmcs12_to_enlightened(vmx);
2061 /* All fields are clean */
2062 vmx->nested.hv_evmcs->hv_clean_fields |=
2063 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
2064 } else {
2065 copy_vmcs12_to_shadow(vmx);
2066 }
2067
Sean Christopherson3731905ef2019-05-07 08:36:27 -07002068 vmx->nested.need_vmcs12_to_shadow_sync = false;
Sean Christopherson55d23752018-12-03 13:53:18 -08002069}
2070
2071static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
2072{
2073 struct vcpu_vmx *vmx =
2074 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
2075
2076 vmx->nested.preemption_timer_expired = true;
2077 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
2078 kvm_vcpu_kick(&vmx->vcpu);
2079
2080 return HRTIMER_NORESTART;
2081}
2082
Peter Shier850448f2020-05-26 14:51:06 -07002083static u64 vmx_calc_preemption_timer_value(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08002084{
Peter Shier850448f2020-05-26 14:51:06 -07002085 struct vcpu_vmx *vmx = to_vmx(vcpu);
2086 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Peter Shier850448f2020-05-26 14:51:06 -07002087
2088 u64 l1_scaled_tsc = kvm_read_l1_tsc(vcpu, rdtsc()) >>
2089 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
2090
2091 if (!vmx->nested.has_preemption_timer_deadline) {
Makarand Sonare8d7fbf02020-05-26 14:51:07 -07002092 vmx->nested.preemption_timer_deadline =
2093 vmcs12->vmx_preemption_timer_value + l1_scaled_tsc;
Peter Shier850448f2020-05-26 14:51:06 -07002094 vmx->nested.has_preemption_timer_deadline = true;
Makarand Sonare8d7fbf02020-05-26 14:51:07 -07002095 }
2096 return vmx->nested.preemption_timer_deadline - l1_scaled_tsc;
Peter Shier850448f2020-05-26 14:51:06 -07002097}
2098
2099static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu,
2100 u64 preemption_timeout)
2101{
Sean Christopherson55d23752018-12-03 13:53:18 -08002102 struct vcpu_vmx *vmx = to_vmx(vcpu);
2103
2104 /*
2105 * A timer value of zero is architecturally guaranteed to cause
2106 * a VMExit prior to executing any instructions in the guest.
2107 */
2108 if (preemption_timeout == 0) {
2109 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
2110 return;
2111 }
2112
2113 if (vcpu->arch.virtual_tsc_khz == 0)
2114 return;
2115
2116 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
2117 preemption_timeout *= 1000000;
2118 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
2119 hrtimer_start(&vmx->nested.preemption_timer,
Jim Mattsonada00982020-05-08 13:36:42 -07002120 ktime_add_ns(ktime_get(), preemption_timeout),
2121 HRTIMER_MODE_ABS_PINNED);
Sean Christopherson55d23752018-12-03 13:53:18 -08002122}
2123
2124static u64 nested_vmx_calc_efer(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
2125{
2126 if (vmx->nested.nested_run_pending &&
2127 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
2128 return vmcs12->guest_ia32_efer;
2129 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
2130 return vmx->vcpu.arch.efer | (EFER_LMA | EFER_LME);
2131 else
2132 return vmx->vcpu.arch.efer & ~(EFER_LMA | EFER_LME);
2133}
2134
2135static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx)
2136{
2137 /*
2138 * If vmcs02 hasn't been initialized, set the constant vmcs02 state
2139 * according to L0's settings (vmcs12 is irrelevant here). Host
2140 * fields that come from L0 and are not constant, e.g. HOST_CR3,
2141 * will be set as needed prior to VMLAUNCH/VMRESUME.
2142 */
2143 if (vmx->nested.vmcs02_initialized)
2144 return;
2145 vmx->nested.vmcs02_initialized = true;
2146
2147 /*
2148 * We don't care what the EPTP value is we just need to guarantee
2149 * it's valid so we don't get a false positive when doing early
2150 * consistency checks.
2151 */
2152 if (enable_ept && nested_early_check)
Sean Christopherson2a40b902020-07-15 20:41:18 -07002153 vmcs_write64(EPT_POINTER,
2154 construct_eptp(&vmx->vcpu, 0, PT64_ROOT_4LEVEL));
Sean Christopherson55d23752018-12-03 13:53:18 -08002155
2156 /* All VMFUNCs are currently emulated through L0 vmexits. */
2157 if (cpu_has_vmx_vmfunc())
2158 vmcs_write64(VM_FUNCTION_CONTROL, 0);
2159
2160 if (cpu_has_vmx_posted_intr())
2161 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
2162
2163 if (cpu_has_vmx_msr_bitmap())
2164 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
2165
Sean Christopherson4d6c9892019-05-07 09:06:30 -07002166 /*
Sean Christophersonc3bb9a22021-02-12 16:50:07 -08002167 * PML is emulated for L2, but never enabled in hardware as the MMU
2168 * handles A/D emulation. Disabling PML for L2 also avoids having to
2169 * deal with filtering out L2 GPAs from the buffer.
Sean Christopherson4d6c9892019-05-07 09:06:30 -07002170 */
2171 if (enable_pml) {
Sean Christophersonc3bb9a22021-02-12 16:50:07 -08002172 vmcs_write64(PML_ADDRESS, 0);
2173 vmcs_write16(GUEST_PML_INDEX, -1);
Sean Christopherson4d6c9892019-05-07 09:06:30 -07002174 }
Sean Christopherson55d23752018-12-03 13:53:18 -08002175
Sean Christophersonc538d572019-05-07 09:06:29 -07002176 if (cpu_has_vmx_encls_vmexit())
2177 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
Sean Christopherson55d23752018-12-03 13:53:18 -08002178
2179 /*
2180 * Set the MSR load/store lists to match L0's settings. Only the
2181 * addresses are constant (for vmcs02), the counts can change based
2182 * on L2's behavior, e.g. switching to/from long mode.
2183 */
Aaron Lewis662f1d12019-11-07 21:14:39 -08002184 vmcs_write64(VM_EXIT_MSR_STORE_ADDR, __pa(vmx->msr_autostore.guest.val));
Sean Christopherson55d23752018-12-03 13:53:18 -08002185 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
2186 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
2187
2188 vmx_set_constant_host_state(vmx);
2189}
2190
Paolo Bonzinib1346ab2019-06-06 17:24:00 +02002191static void prepare_vmcs02_early_rare(struct vcpu_vmx *vmx,
Sean Christopherson55d23752018-12-03 13:53:18 -08002192 struct vmcs12 *vmcs12)
2193{
2194 prepare_vmcs02_constant_state(vmx);
2195
2196 vmcs_write64(VMCS_LINK_POINTER, -1ull);
2197
2198 if (enable_vpid) {
2199 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
2200 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
2201 else
2202 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
2203 }
2204}
2205
2206static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
2207{
Sean Christophersonc3bb9a22021-02-12 16:50:07 -08002208 u32 exec_control;
Sean Christopherson55d23752018-12-03 13:53:18 -08002209 u64 guest_efer = nested_vmx_calc_efer(vmx, vmcs12);
2210
2211 if (vmx->nested.dirty_vmcs12 || vmx->nested.hv_evmcs)
Paolo Bonzinib1346ab2019-06-06 17:24:00 +02002212 prepare_vmcs02_early_rare(vmx, vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08002213
2214 /*
Sean Christopherson55d23752018-12-03 13:53:18 -08002215 * PIN CONTROLS
2216 */
Sean Christophersonc075c3e2019-05-07 12:17:53 -07002217 exec_control = vmx_pin_based_exec_ctrl(vmx);
Sean Christopherson804939e2019-05-07 12:18:05 -07002218 exec_control |= (vmcs12->pin_based_vm_exec_control &
2219 ~PIN_BASED_VMX_PREEMPTION_TIMER);
Sean Christopherson55d23752018-12-03 13:53:18 -08002220
2221 /* Posted interrupts setting is only taken from vmcs12. */
2222 if (nested_cpu_has_posted_intr(vmcs12)) {
2223 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
2224 vmx->nested.pi_pending = false;
2225 } else {
2226 exec_control &= ~PIN_BASED_POSTED_INTR;
2227 }
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002228 pin_controls_set(vmx, exec_control);
Sean Christopherson55d23752018-12-03 13:53:18 -08002229
2230 /*
2231 * EXEC CONTROLS
2232 */
2233 exec_control = vmx_exec_control(vmx); /* L0's desires */
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08002234 exec_control &= ~CPU_BASED_INTR_WINDOW_EXITING;
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08002235 exec_control &= ~CPU_BASED_NMI_WINDOW_EXITING;
Sean Christopherson55d23752018-12-03 13:53:18 -08002236 exec_control &= ~CPU_BASED_TPR_SHADOW;
2237 exec_control |= vmcs12->cpu_based_vm_exec_control;
2238
Liran Alon02d496cf2019-11-11 14:30:55 +02002239 vmx->nested.l1_tpr_threshold = -1;
Sean Christophersonca2f5462019-05-07 09:06:33 -07002240 if (exec_control & CPU_BASED_TPR_SHADOW)
Sean Christopherson55d23752018-12-03 13:53:18 -08002241 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Sean Christopherson55d23752018-12-03 13:53:18 -08002242#ifdef CONFIG_X86_64
Sean Christophersonca2f5462019-05-07 09:06:33 -07002243 else
Sean Christopherson55d23752018-12-03 13:53:18 -08002244 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
2245 CPU_BASED_CR8_STORE_EXITING;
2246#endif
Sean Christopherson55d23752018-12-03 13:53:18 -08002247
2248 /*
2249 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
2250 * for I/O port accesses.
2251 */
Sean Christopherson55d23752018-12-03 13:53:18 -08002252 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
Sean Christophersonde0286b2019-05-07 12:18:01 -07002253 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
2254
2255 /*
2256 * This bit will be computed in nested_get_vmcs12_pages, because
2257 * we do not have access to L1's MSR bitmap yet. For now, keep
2258 * the same bit as before, hoping to avoid multiple VMWRITEs that
2259 * only set/clear this bit.
2260 */
2261 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
2262 exec_control |= exec_controls_get(vmx) & CPU_BASED_USE_MSR_BITMAPS;
2263
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002264 exec_controls_set(vmx, exec_control);
Sean Christopherson55d23752018-12-03 13:53:18 -08002265
2266 /*
2267 * SECONDARY EXEC CONTROLS
2268 */
2269 if (cpu_has_secondary_exec_ctrls()) {
2270 exec_control = vmx->secondary_exec_control;
2271
2272 /* Take the following fields only from vmcs12 */
2273 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
2274 SECONDARY_EXEC_ENABLE_INVPCID |
Sean Christopherson7f3603b2020-09-23 09:50:47 -07002275 SECONDARY_EXEC_ENABLE_RDTSCP |
Sean Christopherson55d23752018-12-03 13:53:18 -08002276 SECONDARY_EXEC_XSAVES |
Tao Xue69e72fa2019-07-16 14:55:49 +08002277 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE |
Sean Christopherson55d23752018-12-03 13:53:18 -08002278 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
2279 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Ilias Stamatisd041b5e2021-05-26 19:44:17 +01002280 SECONDARY_EXEC_ENABLE_VMFUNC |
2281 SECONDARY_EXEC_TSC_SCALING);
Sean Christopherson55d23752018-12-03 13:53:18 -08002282 if (nested_cpu_has(vmcs12,
Sean Christophersonc3bb9a22021-02-12 16:50:07 -08002283 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
2284 exec_control |= vmcs12->secondary_vm_exec_control;
2285
2286 /* PML is emulated and never enabled in hardware for L2. */
2287 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Sean Christopherson55d23752018-12-03 13:53:18 -08002288
2289 /* VMCS shadowing for L2 is emulated for now */
2290 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
2291
Sean Christopherson469debd2019-05-07 12:18:02 -07002292 /*
2293 * Preset *DT exiting when emulating UMIP, so that vmx_set_cr4()
2294 * will not have to rewrite the controls just for this bit.
2295 */
2296 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated() &&
2297 (vmcs12->guest_cr4 & X86_CR4_UMIP))
2298 exec_control |= SECONDARY_EXEC_DESC;
2299
Sean Christopherson55d23752018-12-03 13:53:18 -08002300 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
2301 vmcs_write16(GUEST_INTR_STATUS,
2302 vmcs12->guest_intr_status);
2303
Krish Sadhukhanbddd82d2020-09-21 08:10:25 +00002304 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
2305 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
2306
Sean Christopherson72add912021-04-12 16:21:42 +12002307 if (exec_control & SECONDARY_EXEC_ENCLS_EXITING)
2308 vmx_write_encls_bitmap(&vmx->vcpu, vmcs12);
2309
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002310 secondary_exec_controls_set(vmx, exec_control);
Sean Christopherson55d23752018-12-03 13:53:18 -08002311 }
2312
2313 /*
2314 * ENTRY CONTROLS
2315 *
2316 * vmcs12's VM_{ENTRY,EXIT}_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE
2317 * are emulated by vmx_set_efer() in prepare_vmcs02(), but speculate
2318 * on the related bits (if supported by the CPU) in the hope that
2319 * we can avoid VMWrites during vmx_set_efer().
2320 */
2321 exec_control = (vmcs12->vm_entry_controls | vmx_vmentry_ctrl()) &
2322 ~VM_ENTRY_IA32E_MODE & ~VM_ENTRY_LOAD_IA32_EFER;
2323 if (cpu_has_load_ia32_efer()) {
2324 if (guest_efer & EFER_LMA)
2325 exec_control |= VM_ENTRY_IA32E_MODE;
2326 if (guest_efer != host_efer)
2327 exec_control |= VM_ENTRY_LOAD_IA32_EFER;
2328 }
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002329 vm_entry_controls_set(vmx, exec_control);
Sean Christopherson55d23752018-12-03 13:53:18 -08002330
2331 /*
2332 * EXIT CONTROLS
2333 *
2334 * L2->L1 exit controls are emulated - the hardware exit is to L0 so
2335 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
2336 * bits may be modified by vmx_set_efer() in prepare_vmcs02().
2337 */
2338 exec_control = vmx_vmexit_ctrl();
2339 if (cpu_has_load_ia32_efer() && guest_efer != host_efer)
2340 exec_control |= VM_EXIT_LOAD_IA32_EFER;
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002341 vm_exit_controls_set(vmx, exec_control);
Sean Christopherson55d23752018-12-03 13:53:18 -08002342
2343 /*
2344 * Interrupt/Exception Fields
2345 */
2346 if (vmx->nested.nested_run_pending) {
2347 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2348 vmcs12->vm_entry_intr_info_field);
2349 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
2350 vmcs12->vm_entry_exception_error_code);
2351 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2352 vmcs12->vm_entry_instruction_len);
2353 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
2354 vmcs12->guest_interruptibility_info);
2355 vmx->loaded_vmcs->nmi_known_unmasked =
2356 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
2357 } else {
2358 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
2359 }
2360}
2361
Paolo Bonzinib1346ab2019-06-06 17:24:00 +02002362static void prepare_vmcs02_rare(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
Sean Christopherson55d23752018-12-03 13:53:18 -08002363{
2364 struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs;
2365
2366 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
2367 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2)) {
2368 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
2369 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
2370 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
2371 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
2372 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
2373 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
2374 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
2375 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
2376 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
2377 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
2378 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
2379 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
2380 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
2381 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
2382 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
2383 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
2384 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
2385 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07002386 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
2387 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
Sean Christopherson55d23752018-12-03 13:53:18 -08002388 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
2389 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
2390 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
2391 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
2392 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
2393 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
2394 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
2395 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
2396 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
2397 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
2398 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
2399 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
2400 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
2401 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
2402 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
2403 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Sean Christophersonfc387d82020-09-23 11:44:46 -07002404
2405 vmx->segment_cache.bitmask = 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08002406 }
2407
2408 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
2409 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1)) {
2410 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
2411 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
2412 vmcs12->guest_pending_dbg_exceptions);
2413 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
2414 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
2415
2416 /*
2417 * L1 may access the L2's PDPTR, so save them to construct
2418 * vmcs12
2419 */
2420 if (enable_ept) {
2421 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
2422 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
2423 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
2424 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
2425 }
Sean Christophersonc27e5b02019-05-07 09:06:39 -07002426
2427 if (kvm_mpx_supported() && vmx->nested.nested_run_pending &&
2428 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
2429 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
Sean Christopherson55d23752018-12-03 13:53:18 -08002430 }
2431
2432 if (nested_cpu_has_xsaves(vmcs12))
2433 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
2434
2435 /*
2436 * Whether page-faults are trapped is determined by a combination of
Paolo Bonzinia0c13432020-07-10 17:48:08 +02002437 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF. If L0
2438 * doesn't care about page faults then we should set all of these to
2439 * L1's desires. However, if L0 does care about (some) page faults, it
2440 * is not easy (if at all possible?) to merge L0 and L1's desires, we
2441 * simply ask to exit on each and every L2 page fault. This is done by
2442 * setting MASK=MATCH=0 and (see below) EB.PF=1.
Sean Christopherson55d23752018-12-03 13:53:18 -08002443 * Note that below we don't need special code to set EB.PF beyond the
2444 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
2445 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
2446 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
2447 */
Paolo Bonzinia0c13432020-07-10 17:48:08 +02002448 if (vmx_need_pf_intercept(&vmx->vcpu)) {
2449 /*
2450 * TODO: if both L0 and L1 need the same MASK and MATCH,
2451 * go ahead and use it?
2452 */
2453 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
2454 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
2455 } else {
2456 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, vmcs12->page_fault_error_code_mask);
2457 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, vmcs12->page_fault_error_code_match);
2458 }
Sean Christopherson55d23752018-12-03 13:53:18 -08002459
2460 if (cpu_has_vmx_apicv()) {
2461 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
2462 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
2463 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
2464 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
2465 }
2466
Aaron Lewis662f1d12019-11-07 21:14:39 -08002467 /*
2468 * Make sure the msr_autostore list is up to date before we set the
2469 * count in the vmcs02.
2470 */
2471 prepare_vmx_msr_autostore_list(&vmx->vcpu, MSR_IA32_TSC);
2472
2473 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, vmx->msr_autostore.guest.nr);
Sean Christopherson55d23752018-12-03 13:53:18 -08002474 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
2475 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
2476
2477 set_cr4_guest_host_mask(vmx);
Sean Christopherson55d23752018-12-03 13:53:18 -08002478}
2479
2480/*
2481 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
2482 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
2483 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
2484 * guest in a way that will both be appropriate to L1's requests, and our
2485 * needs. In addition to modifying the active vmcs (which is vmcs02), this
2486 * function also has additional necessary side-effects, like setting various
2487 * vcpu->arch fields.
2488 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
2489 * is assigned to entry_failure_code on failure.
2490 */
2491static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Sean Christopherson68cda402020-05-11 15:05:29 -07002492 enum vm_entry_failure_code *entry_failure_code)
Sean Christopherson55d23752018-12-03 13:53:18 -08002493{
2494 struct vcpu_vmx *vmx = to_vmx(vcpu);
2495 struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs;
Sean Christophersonc7554efc2019-05-07 09:06:40 -07002496 bool load_guest_pdptrs_vmcs12 = false;
Sean Christopherson55d23752018-12-03 13:53:18 -08002497
Sean Christophersonc7554efc2019-05-07 09:06:40 -07002498 if (vmx->nested.dirty_vmcs12 || hv_evmcs) {
Paolo Bonzinib1346ab2019-06-06 17:24:00 +02002499 prepare_vmcs02_rare(vmx, vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08002500 vmx->nested.dirty_vmcs12 = false;
Sean Christopherson55d23752018-12-03 13:53:18 -08002501
Sean Christophersonc7554efc2019-05-07 09:06:40 -07002502 load_guest_pdptrs_vmcs12 = !hv_evmcs ||
2503 !(hv_evmcs->hv_clean_fields &
2504 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1);
Sean Christopherson55d23752018-12-03 13:53:18 -08002505 }
2506
2507 if (vmx->nested.nested_run_pending &&
2508 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
2509 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
2510 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
2511 } else {
2512 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
2513 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
2514 }
Sean Christopherson3b013a22019-05-07 09:06:28 -07002515 if (kvm_mpx_supported() && (!vmx->nested.nested_run_pending ||
2516 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)))
2517 vmcs_write64(GUEST_BNDCFGS, vmx->nested.vmcs01_guest_bndcfgs);
Sean Christopherson55d23752018-12-03 13:53:18 -08002518 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
2519
Sean Christopherson55d23752018-12-03 13:53:18 -08002520 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
2521 * bitwise-or of what L1 wants to trap for L2, and what we want to
2522 * trap. Note that CR0.TS also needs updating - we do this later.
2523 */
Jason Baronb6a7cc32021-01-14 22:27:54 -05002524 vmx_update_exception_bitmap(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08002525 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
2526 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
2527
2528 if (vmx->nested.nested_run_pending &&
2529 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
2530 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
2531 vcpu->arch.pat = vmcs12->guest_ia32_pat;
2532 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2533 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
2534 }
2535
Ilias Stamatisd041b5e2021-05-26 19:44:17 +01002536 vcpu->arch.tsc_offset = kvm_calc_nested_tsc_offset(
2537 vcpu->arch.l1_tsc_offset,
2538 vmx_get_l2_tsc_offset(vcpu),
2539 vmx_get_l2_tsc_multiplier(vcpu));
2540
2541 vcpu->arch.tsc_scaling_ratio = kvm_calc_nested_tsc_multiplier(
2542 vcpu->arch.l1_tsc_scaling_ratio,
2543 vmx_get_l2_tsc_multiplier(vcpu));
2544
Sean Christopherson55d23752018-12-03 13:53:18 -08002545 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Sean Christopherson55d23752018-12-03 13:53:18 -08002546 if (kvm_has_tsc_control)
Ilias Stamatis1ab92872021-06-07 11:54:38 +01002547 vmcs_write64(TSC_MULTIPLIER, vcpu->arch.tsc_scaling_ratio);
Sean Christopherson55d23752018-12-03 13:53:18 -08002548
Sean Christopherson50b265a2020-03-20 14:28:19 -07002549 nested_vmx_transition_tlb_flush(vcpu, vmcs12, true);
Sean Christopherson55d23752018-12-03 13:53:18 -08002550
2551 if (nested_cpu_has_ept(vmcs12))
2552 nested_ept_init_mmu_context(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08002553
2554 /*
2555 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
2556 * bits which we consider mandatory enabled.
2557 * The CR0_READ_SHADOW is what L2 should have expected to read given
2558 * the specifications by L1; It's not enough to take
2559 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
2560 * have more bits than L1 expected.
2561 */
2562 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
2563 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
2564
2565 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
2566 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
2567
2568 vcpu->arch.efer = nested_vmx_calc_efer(vmx, vmcs12);
2569 /* Note: may modify VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
2570 vmx_set_efer(vcpu, vcpu->arch.efer);
2571
2572 /*
2573 * Guest state is invalid and unrestricted guest is disabled,
2574 * which means L1 attempted VMEntry to L2 with invalid state.
2575 * Fail the VMEntry.
2576 */
Sean Christopherson2ba44932020-09-23 11:44:48 -07002577 if (CC(!vmx_guest_state_valid(vcpu))) {
Sean Christopherson55d23752018-12-03 13:53:18 -08002578 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christophersonc80add02019-04-11 12:18:09 -07002579 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002580 }
2581
2582 /* Shadow page tables on either EPT or shadow page tables. */
2583 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
2584 entry_failure_code))
Sean Christophersonc80add02019-04-11 12:18:09 -07002585 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002586
Sean Christopherson04f11ef2019-09-27 14:45:16 -07002587 /*
2588 * Immediately write vmcs02.GUEST_CR3. It will be propagated to vmcs12
2589 * on nested VM-Exit, which can occur without actually running L2 and
Paolo Bonzini727a7e22020-03-05 03:52:50 -05002590 * thus without hitting vmx_load_mmu_pgd(), e.g. if L1 is entering L2 with
Sean Christopherson04f11ef2019-09-27 14:45:16 -07002591 * vmcs12.GUEST_ACTIVITYSTATE=HLT, in which case KVM will intercept the
2592 * transition to HLT instead of running L2.
2593 */
2594 if (enable_ept)
2595 vmcs_writel(GUEST_CR3, vmcs12->guest_cr3);
2596
Sean Christophersonc7554efc2019-05-07 09:06:40 -07002597 /* Late preparation of GUEST_PDPTRs now that EFER and CRs are set. */
2598 if (load_guest_pdptrs_vmcs12 && nested_cpu_has_ept(vmcs12) &&
2599 is_pae_paging(vcpu)) {
2600 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
2601 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
2602 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
2603 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
2604 }
2605
Sean Christopherson55d23752018-12-03 13:53:18 -08002606 if (!enable_ept)
2607 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
2608
Oliver Upton71f73472019-11-13 16:17:19 -08002609 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) &&
Oliver Uptond1968422019-12-13 16:33:58 -08002610 WARN_ON_ONCE(kvm_set_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL,
2611 vmcs12->guest_ia32_perf_global_ctrl)))
Oliver Upton71f73472019-11-13 16:17:19 -08002612 return -EINVAL;
2613
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02002614 kvm_rsp_write(vcpu, vmcs12->guest_rsp);
2615 kvm_rip_write(vcpu, vmcs12->guest_rip);
Sean Christopherson55d23752018-12-03 13:53:18 -08002616 return 0;
2617}
2618
2619static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
2620{
Sean Christopherson5497b952019-07-11 08:58:29 -07002621 if (CC(!nested_cpu_has_nmi_exiting(vmcs12) &&
2622 nested_cpu_has_virtual_nmis(vmcs12)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002623 return -EINVAL;
2624
Sean Christopherson5497b952019-07-11 08:58:29 -07002625 if (CC(!nested_cpu_has_virtual_nmis(vmcs12) &&
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08002626 nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002627 return -EINVAL;
2628
2629 return 0;
2630}
2631
Sean Christophersonac6389a2020-03-02 18:02:38 -08002632static bool nested_vmx_check_eptp(struct kvm_vcpu *vcpu, u64 new_eptp)
Sean Christopherson55d23752018-12-03 13:53:18 -08002633{
2634 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08002635
2636 /* Check for memory type validity */
Sean Christophersonac6389a2020-03-02 18:02:38 -08002637 switch (new_eptp & VMX_EPTP_MT_MASK) {
Sean Christopherson55d23752018-12-03 13:53:18 -08002638 case VMX_EPTP_MT_UC:
Sean Christopherson5497b952019-07-11 08:58:29 -07002639 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002640 return false;
2641 break;
2642 case VMX_EPTP_MT_WB:
Sean Christopherson5497b952019-07-11 08:58:29 -07002643 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002644 return false;
2645 break;
2646 default:
2647 return false;
2648 }
2649
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08002650 /* Page-walk levels validity. */
Sean Christophersonac6389a2020-03-02 18:02:38 -08002651 switch (new_eptp & VMX_EPTP_PWL_MASK) {
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08002652 case VMX_EPTP_PWL_5:
2653 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_PAGE_WALK_5_BIT)))
2654 return false;
2655 break;
2656 case VMX_EPTP_PWL_4:
2657 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_PAGE_WALK_4_BIT)))
2658 return false;
2659 break;
2660 default:
Sean Christopherson55d23752018-12-03 13:53:18 -08002661 return false;
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08002662 }
Sean Christopherson55d23752018-12-03 13:53:18 -08002663
2664 /* Reserved bits should not be set */
Sean Christopherson636e8b72021-02-03 16:01:10 -08002665 if (CC(kvm_vcpu_is_illegal_gpa(vcpu, new_eptp) || ((new_eptp >> 7) & 0x1f)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002666 return false;
2667
2668 /* AD, if set, should be supported */
Sean Christophersonac6389a2020-03-02 18:02:38 -08002669 if (new_eptp & VMX_EPTP_AD_ENABLE_BIT) {
Sean Christopherson5497b952019-07-11 08:58:29 -07002670 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002671 return false;
2672 }
2673
2674 return true;
2675}
2676
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002677/*
2678 * Checks related to VM-Execution Control Fields
2679 */
2680static int nested_check_vm_execution_controls(struct kvm_vcpu *vcpu,
2681 struct vmcs12 *vmcs12)
2682{
2683 struct vcpu_vmx *vmx = to_vmx(vcpu);
2684
Sean Christopherson5497b952019-07-11 08:58:29 -07002685 if (CC(!vmx_control_verify(vmcs12->pin_based_vm_exec_control,
2686 vmx->nested.msrs.pinbased_ctls_low,
2687 vmx->nested.msrs.pinbased_ctls_high)) ||
2688 CC(!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
2689 vmx->nested.msrs.procbased_ctls_low,
2690 vmx->nested.msrs.procbased_ctls_high)))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002691 return -EINVAL;
2692
2693 if (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
Sean Christopherson5497b952019-07-11 08:58:29 -07002694 CC(!vmx_control_verify(vmcs12->secondary_vm_exec_control,
2695 vmx->nested.msrs.secondary_ctls_low,
2696 vmx->nested.msrs.secondary_ctls_high)))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002697 return -EINVAL;
2698
Sean Christopherson5497b952019-07-11 08:58:29 -07002699 if (CC(vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu)) ||
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002700 nested_vmx_check_io_bitmap_controls(vcpu, vmcs12) ||
2701 nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12) ||
2702 nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12) ||
2703 nested_vmx_check_apic_access_controls(vcpu, vmcs12) ||
2704 nested_vmx_check_apicv_controls(vcpu, vmcs12) ||
2705 nested_vmx_check_nmi_controls(vmcs12) ||
2706 nested_vmx_check_pml_controls(vcpu, vmcs12) ||
2707 nested_vmx_check_unrestricted_guest_controls(vcpu, vmcs12) ||
2708 nested_vmx_check_mode_based_ept_exec_controls(vcpu, vmcs12) ||
2709 nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12) ||
Sean Christopherson5497b952019-07-11 08:58:29 -07002710 CC(nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002711 return -EINVAL;
2712
Sean Christophersonbc441212019-02-12 16:42:23 -08002713 if (!nested_cpu_has_preemption_timer(vmcs12) &&
2714 nested_cpu_has_save_preemption_timer(vmcs12))
2715 return -EINVAL;
2716
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002717 if (nested_cpu_has_ept(vmcs12) &&
Sean Christophersonac6389a2020-03-02 18:02:38 -08002718 CC(!nested_vmx_check_eptp(vcpu, vmcs12->ept_pointer)))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002719 return -EINVAL;
2720
2721 if (nested_cpu_has_vmfunc(vmcs12)) {
Sean Christopherson5497b952019-07-11 08:58:29 -07002722 if (CC(vmcs12->vm_function_control &
2723 ~vmx->nested.msrs.vmfunc_controls))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002724 return -EINVAL;
2725
2726 if (nested_cpu_has_eptp_switching(vmcs12)) {
Sean Christopherson5497b952019-07-11 08:58:29 -07002727 if (CC(!nested_cpu_has_ept(vmcs12)) ||
2728 CC(!page_address_valid(vcpu, vmcs12->eptp_list_address)))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002729 return -EINVAL;
2730 }
2731 }
2732
2733 return 0;
2734}
2735
Krish Sadhukhan61446ba2018-12-12 13:30:09 -05002736/*
2737 * Checks related to VM-Exit Control Fields
2738 */
2739static int nested_check_vm_exit_controls(struct kvm_vcpu *vcpu,
2740 struct vmcs12 *vmcs12)
2741{
2742 struct vcpu_vmx *vmx = to_vmx(vcpu);
2743
Sean Christopherson5497b952019-07-11 08:58:29 -07002744 if (CC(!vmx_control_verify(vmcs12->vm_exit_controls,
2745 vmx->nested.msrs.exit_ctls_low,
2746 vmx->nested.msrs.exit_ctls_high)) ||
2747 CC(nested_vmx_check_exit_msr_switch_controls(vcpu, vmcs12)))
Krish Sadhukhan61446ba2018-12-12 13:30:09 -05002748 return -EINVAL;
2749
2750 return 0;
2751}
2752
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002753/*
2754 * Checks related to VM-Entry Control Fields
2755 */
2756static int nested_check_vm_entry_controls(struct kvm_vcpu *vcpu,
2757 struct vmcs12 *vmcs12)
Sean Christopherson55d23752018-12-03 13:53:18 -08002758{
2759 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08002760
Sean Christopherson5497b952019-07-11 08:58:29 -07002761 if (CC(!vmx_control_verify(vmcs12->vm_entry_controls,
2762 vmx->nested.msrs.entry_ctls_low,
2763 vmx->nested.msrs.entry_ctls_high)))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002764 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002765
2766 /*
2767 * From the Intel SDM, volume 3:
2768 * Fields relevant to VM-entry event injection must be set properly.
2769 * These fields are the VM-entry interruption-information field, the
2770 * VM-entry exception error code, and the VM-entry instruction length.
2771 */
2772 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
2773 u32 intr_info = vmcs12->vm_entry_intr_info_field;
2774 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
2775 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
2776 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
2777 bool should_have_error_code;
2778 bool urg = nested_cpu_has2(vmcs12,
2779 SECONDARY_EXEC_UNRESTRICTED_GUEST);
2780 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
2781
2782 /* VM-entry interruption-info field: interruption type */
Sean Christopherson5497b952019-07-11 08:58:29 -07002783 if (CC(intr_type == INTR_TYPE_RESERVED) ||
2784 CC(intr_type == INTR_TYPE_OTHER_EVENT &&
2785 !nested_cpu_supports_monitor_trap_flag(vcpu)))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002786 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002787
2788 /* VM-entry interruption-info field: vector */
Sean Christopherson5497b952019-07-11 08:58:29 -07002789 if (CC(intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
2790 CC(intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
2791 CC(intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002792 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002793
2794 /* VM-entry interruption-info field: deliver error code */
2795 should_have_error_code =
2796 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
2797 x86_exception_has_error_code(vector);
Sean Christopherson5497b952019-07-11 08:58:29 -07002798 if (CC(has_error_code != should_have_error_code))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002799 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002800
2801 /* VM-entry exception error code */
Sean Christopherson5497b952019-07-11 08:58:29 -07002802 if (CC(has_error_code &&
Sean Christopherson567926c2019-10-01 09:21:23 -07002803 vmcs12->vm_entry_exception_error_code & GENMASK(31, 16)))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002804 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002805
2806 /* VM-entry interruption-info field: reserved bits */
Sean Christopherson5497b952019-07-11 08:58:29 -07002807 if (CC(intr_info & INTR_INFO_RESVD_BITS_MASK))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002808 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002809
2810 /* VM-entry instruction length */
2811 switch (intr_type) {
2812 case INTR_TYPE_SOFT_EXCEPTION:
2813 case INTR_TYPE_SOFT_INTR:
2814 case INTR_TYPE_PRIV_SW_EXCEPTION:
Sean Christopherson5497b952019-07-11 08:58:29 -07002815 if (CC(vmcs12->vm_entry_instruction_len > 15) ||
2816 CC(vmcs12->vm_entry_instruction_len == 0 &&
2817 CC(!nested_cpu_has_zero_length_injection(vcpu))))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002818 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002819 }
2820 }
2821
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002822 if (nested_vmx_check_entry_msr_switch_controls(vcpu, vmcs12))
2823 return -EINVAL;
2824
2825 return 0;
2826}
2827
Sean Christopherson5478ba32019-04-11 12:18:06 -07002828static int nested_vmx_check_controls(struct kvm_vcpu *vcpu,
2829 struct vmcs12 *vmcs12)
2830{
2831 if (nested_check_vm_execution_controls(vcpu, vmcs12) ||
2832 nested_check_vm_exit_controls(vcpu, vmcs12) ||
2833 nested_check_vm_entry_controls(vcpu, vmcs12))
Paolo Bonzini98d9e852019-04-12 10:19:57 +02002834 return -EINVAL;
Sean Christopherson5478ba32019-04-11 12:18:06 -07002835
Vitaly Kuznetsova8350232020-02-05 13:30:34 +01002836 if (to_vmx(vcpu)->nested.enlightened_vmcs_enabled)
2837 return nested_evmcs_check_controls(vmcs12);
2838
Sean Christopherson5478ba32019-04-11 12:18:06 -07002839 return 0;
2840}
2841
Paolo Bonzini98d9e852019-04-12 10:19:57 +02002842static int nested_vmx_check_host_state(struct kvm_vcpu *vcpu,
2843 struct vmcs12 *vmcs12)
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002844{
2845 bool ia32e;
2846
Sean Christopherson5497b952019-07-11 08:58:29 -07002847 if (CC(!nested_host_cr0_valid(vcpu, vmcs12->host_cr0)) ||
2848 CC(!nested_host_cr4_valid(vcpu, vmcs12->host_cr4)) ||
Sean Christopherson636e8b72021-02-03 16:01:10 -08002849 CC(kvm_vcpu_is_illegal_gpa(vcpu, vmcs12->host_cr3)))
Krish Sadhukhan254b2f32018-12-12 13:30:11 -05002850 return -EINVAL;
Krish Sadhukhan711eff32019-02-07 14:05:30 -05002851
Sean Christopherson5497b952019-07-11 08:58:29 -07002852 if (CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_esp, vcpu)) ||
2853 CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_eip, vcpu)))
Krish Sadhukhan711eff32019-02-07 14:05:30 -05002854 return -EINVAL;
2855
Krish Sadhukhanf6b0db1f2019-04-08 17:35:11 -04002856 if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) &&
Sean Christopherson5497b952019-07-11 08:58:29 -07002857 CC(!kvm_pat_valid(vmcs12->host_ia32_pat)))
Krish Sadhukhanf6b0db1f2019-04-08 17:35:11 -04002858 return -EINVAL;
2859
Oliver Uptonc547cb62019-11-13 16:17:17 -08002860 if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) &&
2861 CC(!kvm_valid_perf_global_ctrl(vcpu_to_pmu(vcpu),
2862 vmcs12->host_ia32_perf_global_ctrl)))
2863 return -EINVAL;
2864
Paolo Bonzinifd3edd42019-09-25 18:33:53 +02002865#ifdef CONFIG_X86_64
2866 ia32e = !!(vcpu->arch.efer & EFER_LMA);
2867#else
2868 ia32e = false;
2869#endif
2870
2871 if (ia32e) {
2872 if (CC(!(vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)) ||
2873 CC(!(vmcs12->host_cr4 & X86_CR4_PAE)))
2874 return -EINVAL;
2875 } else {
2876 if (CC(vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) ||
2877 CC(vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) ||
2878 CC(vmcs12->host_cr4 & X86_CR4_PCIDE) ||
2879 CC((vmcs12->host_rip) >> 32))
2880 return -EINVAL;
2881 }
Krish Sadhukhan1ef23e12019-07-03 19:54:35 -04002882
Sean Christopherson5497b952019-07-11 08:58:29 -07002883 if (CC(vmcs12->host_cs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2884 CC(vmcs12->host_ss_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2885 CC(vmcs12->host_ds_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2886 CC(vmcs12->host_es_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2887 CC(vmcs12->host_fs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2888 CC(vmcs12->host_gs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2889 CC(vmcs12->host_tr_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2890 CC(vmcs12->host_cs_selector == 0) ||
2891 CC(vmcs12->host_tr_selector == 0) ||
2892 CC(vmcs12->host_ss_selector == 0 && !ia32e))
Krish Sadhukhan1ef23e12019-07-03 19:54:35 -04002893 return -EINVAL;
2894
Sean Christopherson5497b952019-07-11 08:58:29 -07002895 if (CC(is_noncanonical_address(vmcs12->host_fs_base, vcpu)) ||
2896 CC(is_noncanonical_address(vmcs12->host_gs_base, vcpu)) ||
2897 CC(is_noncanonical_address(vmcs12->host_gdtr_base, vcpu)) ||
2898 CC(is_noncanonical_address(vmcs12->host_idtr_base, vcpu)) ||
Paolo Bonzinifd3edd42019-09-25 18:33:53 +02002899 CC(is_noncanonical_address(vmcs12->host_tr_base, vcpu)) ||
2900 CC(is_noncanonical_address(vmcs12->host_rip, vcpu)))
Krish Sadhukhan58450382019-08-09 12:26:19 -07002901 return -EINVAL;
Krish Sadhukhan1ef23e12019-07-03 19:54:35 -04002902
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002903 /*
2904 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
2905 * IA32_EFER MSR must be 0 in the field for that register. In addition,
2906 * the values of the LMA and LME bits in the field must each be that of
2907 * the host address-space size VM-exit control.
2908 */
2909 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
Sean Christopherson5497b952019-07-11 08:58:29 -07002910 if (CC(!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer)) ||
2911 CC(ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA)) ||
2912 CC(ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)))
Krish Sadhukhan254b2f32018-12-12 13:30:11 -05002913 return -EINVAL;
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002914 }
2915
Sean Christopherson55d23752018-12-03 13:53:18 -08002916 return 0;
2917}
2918
2919static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
2920 struct vmcs12 *vmcs12)
2921{
KarimAllah Ahmed88925302019-01-31 21:24:41 +01002922 int r = 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08002923 struct vmcs12 *shadow;
KarimAllah Ahmed88925302019-01-31 21:24:41 +01002924 struct kvm_host_map map;
Sean Christopherson55d23752018-12-03 13:53:18 -08002925
2926 if (vmcs12->vmcs_link_pointer == -1ull)
2927 return 0;
2928
Sean Christopherson5497b952019-07-11 08:58:29 -07002929 if (CC(!page_address_valid(vcpu, vmcs12->vmcs_link_pointer)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002930 return -EINVAL;
2931
Sean Christopherson5497b952019-07-11 08:58:29 -07002932 if (CC(kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->vmcs_link_pointer), &map)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002933 return -EINVAL;
2934
KarimAllah Ahmed88925302019-01-31 21:24:41 +01002935 shadow = map.hva;
2936
Sean Christopherson5497b952019-07-11 08:58:29 -07002937 if (CC(shadow->hdr.revision_id != VMCS12_REVISION) ||
2938 CC(shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002939 r = -EINVAL;
KarimAllah Ahmed88925302019-01-31 21:24:41 +01002940
2941 kvm_vcpu_unmap(vcpu, &map, false);
Sean Christopherson55d23752018-12-03 13:53:18 -08002942 return r;
2943}
2944
Sean Christopherson55d23752018-12-03 13:53:18 -08002945/*
2946 * Checks related to Guest Non-register State
2947 */
2948static int nested_check_guest_non_reg_state(struct vmcs12 *vmcs12)
2949{
Sean Christopherson5497b952019-07-11 08:58:29 -07002950 if (CC(vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
Yadong Qibf0cd882020-11-06 14:51:22 +08002951 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT &&
2952 vmcs12->guest_activity_state != GUEST_ACTIVITY_WAIT_SIPI))
Sean Christopherson55d23752018-12-03 13:53:18 -08002953 return -EINVAL;
2954
2955 return 0;
2956}
2957
Sean Christopherson5478ba32019-04-11 12:18:06 -07002958static int nested_vmx_check_guest_state(struct kvm_vcpu *vcpu,
2959 struct vmcs12 *vmcs12,
Sean Christopherson68cda402020-05-11 15:05:29 -07002960 enum vm_entry_failure_code *entry_failure_code)
Sean Christopherson55d23752018-12-03 13:53:18 -08002961{
2962 bool ia32e;
2963
Sean Christopherson68cda402020-05-11 15:05:29 -07002964 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson55d23752018-12-03 13:53:18 -08002965
Sean Christopherson5497b952019-07-11 08:58:29 -07002966 if (CC(!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0)) ||
2967 CC(!nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4)))
Sean Christophersonc80add02019-04-11 12:18:09 -07002968 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002969
Krish Sadhukhanb91991b2020-01-15 19:54:32 -05002970 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) &&
2971 CC(!kvm_dr7_valid(vmcs12->guest_dr7)))
2972 return -EINVAL;
2973
Krish Sadhukhande2bc2b2019-04-08 17:35:12 -04002974 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) &&
Sean Christopherson5497b952019-07-11 08:58:29 -07002975 CC(!kvm_pat_valid(vmcs12->guest_ia32_pat)))
Sean Christophersonc80add02019-04-11 12:18:09 -07002976 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002977
2978 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
Sean Christopherson68cda402020-05-11 15:05:29 -07002979 *entry_failure_code = ENTRY_FAIL_VMCS_LINK_PTR;
Sean Christophersonc80add02019-04-11 12:18:09 -07002980 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002981 }
2982
Oliver Uptonbfc6ad62019-11-13 16:17:16 -08002983 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) &&
2984 CC(!kvm_valid_perf_global_ctrl(vcpu_to_pmu(vcpu),
2985 vmcs12->guest_ia32_perf_global_ctrl)))
2986 return -EINVAL;
2987
Sean Christopherson55d23752018-12-03 13:53:18 -08002988 /*
2989 * If the load IA32_EFER VM-entry control is 1, the following checks
2990 * are performed on the field for the IA32_EFER MSR:
2991 * - Bits reserved in the IA32_EFER MSR must be 0.
2992 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
2993 * the IA-32e mode guest VM-exit control. It must also be identical
2994 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
2995 * CR0.PG) is 1.
2996 */
2997 if (to_vmx(vcpu)->nested.nested_run_pending &&
2998 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
2999 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
Sean Christopherson5497b952019-07-11 08:58:29 -07003000 if (CC(!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer)) ||
3001 CC(ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA)) ||
3002 CC(((vmcs12->guest_cr0 & X86_CR0_PG) &&
3003 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))))
Sean Christophersonc80add02019-04-11 12:18:09 -07003004 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08003005 }
3006
3007 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
Sean Christopherson5497b952019-07-11 08:58:29 -07003008 (CC(is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu)) ||
3009 CC((vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD))))
Sean Christophersonc80add02019-04-11 12:18:09 -07003010 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08003011
Sean Christopherson9c3e9222019-04-11 12:18:05 -07003012 if (nested_check_guest_non_reg_state(vmcs12))
Sean Christophersonc80add02019-04-11 12:18:09 -07003013 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08003014
3015 return 0;
3016}
3017
Sean Christopherson453eafb2018-12-20 12:25:17 -08003018static int nested_vmx_check_vmentry_hw(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08003019{
3020 struct vcpu_vmx *vmx = to_vmx(vcpu);
3021 unsigned long cr3, cr4;
Sean Christophersonf1727b42019-01-25 07:40:58 -08003022 bool vm_fail;
Sean Christopherson55d23752018-12-03 13:53:18 -08003023
3024 if (!nested_early_check)
3025 return 0;
3026
3027 if (vmx->msr_autoload.host.nr)
3028 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
3029 if (vmx->msr_autoload.guest.nr)
3030 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
3031
3032 preempt_disable();
3033
3034 vmx_prepare_switch_to_guest(vcpu);
3035
3036 /*
3037 * Induce a consistency check VMExit by clearing bit 1 in GUEST_RFLAGS,
3038 * which is reserved to '1' by hardware. GUEST_RFLAGS is guaranteed to
Miaohe Lin49f933d2020-02-27 11:20:54 +08003039 * be written (by prepare_vmcs02()) before the "real" VMEnter, i.e.
Sean Christopherson55d23752018-12-03 13:53:18 -08003040 * there is no need to preserve other bits or save/restore the field.
3041 */
3042 vmcs_writel(GUEST_RFLAGS, 0);
3043
Sean Christopherson55d23752018-12-03 13:53:18 -08003044 cr3 = __get_current_cr3_fast();
3045 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
3046 vmcs_writel(HOST_CR3, cr3);
3047 vmx->loaded_vmcs->host_state.cr3 = cr3;
3048 }
3049
3050 cr4 = cr4_read_shadow();
3051 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
3052 vmcs_writel(HOST_CR4, cr4);
3053 vmx->loaded_vmcs->host_state.cr4 = cr4;
3054 }
3055
Uros Bizjak150f17b2020-12-30 16:26:57 -08003056 vm_fail = __vmx_vcpu_run(vmx, (unsigned long *)&vcpu->arch.regs,
3057 vmx->loaded_vmcs->launched);
Sean Christopherson55d23752018-12-03 13:53:18 -08003058
Sean Christopherson55d23752018-12-03 13:53:18 -08003059 if (vmx->msr_autoload.host.nr)
3060 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
3061 if (vmx->msr_autoload.guest.nr)
3062 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
3063
Sean Christophersonf1727b42019-01-25 07:40:58 -08003064 if (vm_fail) {
Sean Christopherson380e0052019-07-11 08:58:30 -07003065 u32 error = vmcs_read32(VM_INSTRUCTION_ERROR);
3066
Wanpeng Li541e8862019-05-17 16:49:50 +08003067 preempt_enable();
Sean Christopherson380e0052019-07-11 08:58:30 -07003068
3069 trace_kvm_nested_vmenter_failed(
3070 "early hardware check VM-instruction error: ", error);
3071 WARN_ON_ONCE(error != VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Sean Christopherson55d23752018-12-03 13:53:18 -08003072 return 1;
3073 }
3074
3075 /*
3076 * VMExit clears RFLAGS.IF and DR7, even on a consistency check.
3077 */
Sean Christopherson55d23752018-12-03 13:53:18 -08003078 if (hw_breakpoint_active())
3079 set_debugreg(__this_cpu_read(cpu_dr7), 7);
Peter Zijlstra84b6a342020-05-29 23:27:36 +02003080 local_irq_enable();
Wanpeng Li541e8862019-05-17 16:49:50 +08003081 preempt_enable();
Sean Christopherson55d23752018-12-03 13:53:18 -08003082
3083 /*
3084 * A non-failing VMEntry means we somehow entered guest mode with
3085 * an illegal RIP, and that's just the tip of the iceberg. There
3086 * is no telling what memory has been modified or what state has
3087 * been exposed to unknown code. Hitting this all but guarantees
3088 * a (very critical) hardware issue.
3089 */
3090 WARN_ON(!(vmcs_read32(VM_EXIT_REASON) &
3091 VMX_EXIT_REASONS_FAILED_VMENTRY));
3092
3093 return 0;
3094}
Sean Christopherson55d23752018-12-03 13:53:18 -08003095
Paolo Bonzini9a78e152021-01-08 11:43:08 -05003096static bool nested_get_evmcs_page(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08003097{
Sean Christopherson55d23752018-12-03 13:53:18 -08003098 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08003099
Vitaly Kuznetsove942dbf2020-03-09 16:52:12 +01003100 /*
3101 * hv_evmcs may end up being not mapped after migration (when
3102 * L2 was running), map it here to make sure vmcs12 changes are
3103 * properly reflected.
3104 */
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003105 if (vmx->nested.enlightened_vmcs_enabled && !vmx->nested.hv_evmcs) {
3106 enum nested_evmptrld_status evmptrld_status =
3107 nested_vmx_handle_enlightened_vmptrld(vcpu, false);
3108
3109 if (evmptrld_status == EVMPTRLD_VMFAIL ||
Vitaly Kuznetsovf5c7e842021-05-03 17:08:51 +02003110 evmptrld_status == EVMPTRLD_ERROR)
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003111 return false;
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003112 }
Vitaly Kuznetsove942dbf2020-03-09 16:52:12 +01003113
Paolo Bonzini9a78e152021-01-08 11:43:08 -05003114 return true;
3115}
3116
3117static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
3118{
3119 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3120 struct vcpu_vmx *vmx = to_vmx(vcpu);
3121 struct kvm_host_map *map;
3122 struct page *page;
3123 u64 hpa;
3124
Sean Christopherson55d23752018-12-03 13:53:18 -08003125 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
3126 /*
3127 * Translate L1 physical address to host physical
3128 * address for vmcs02. Keep the page pinned, so this
3129 * physical address remains valid. We keep a reference
3130 * to it so we can release it later.
3131 */
3132 if (vmx->nested.apic_access_page) { /* shouldn't happen */
Liran Alonb11494b2019-11-21 00:31:47 +02003133 kvm_release_page_clean(vmx->nested.apic_access_page);
Sean Christopherson55d23752018-12-03 13:53:18 -08003134 vmx->nested.apic_access_page = NULL;
3135 }
3136 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Sean Christopherson55d23752018-12-03 13:53:18 -08003137 if (!is_error_page(page)) {
3138 vmx->nested.apic_access_page = page;
3139 hpa = page_to_phys(vmx->nested.apic_access_page);
3140 vmcs_write64(APIC_ACCESS_ADDR, hpa);
3141 } else {
Jim Mattson671ddc72019-10-15 10:44:05 -07003142 pr_debug_ratelimited("%s: no backing 'struct page' for APIC-access address in vmcs12\n",
3143 __func__);
3144 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3145 vcpu->run->internal.suberror =
3146 KVM_INTERNAL_ERROR_EMULATION;
3147 vcpu->run->internal.ndata = 0;
3148 return false;
Sean Christopherson55d23752018-12-03 13:53:18 -08003149 }
3150 }
3151
3152 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003153 map = &vmx->nested.virtual_apic_map;
Sean Christopherson55d23752018-12-03 13:53:18 -08003154
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003155 if (!kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->virtual_apic_page_addr), map)) {
3156 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, pfn_to_hpa(map->pfn));
Paolo Bonzini69090812019-04-15 15:16:17 +02003157 } else if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING) &&
3158 nested_cpu_has(vmcs12, CPU_BASED_CR8_STORE_EXITING) &&
3159 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
3160 /*
3161 * The processor will never use the TPR shadow, simply
3162 * clear the bit from the execution control. Such a
3163 * configuration is useless, but it happens in tests.
3164 * For any other configuration, failing the vm entry is
3165 * _not_ what the processor does but it's basically the
3166 * only possibility we have.
3167 */
Sean Christopherson2183f562019-05-07 12:17:56 -07003168 exec_controls_clearbit(vmx, CPU_BASED_TPR_SHADOW);
Paolo Bonzini69090812019-04-15 15:16:17 +02003169 } else {
Sean Christophersonca2f5462019-05-07 09:06:33 -07003170 /*
3171 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR to
3172 * force VM-Entry to fail.
3173 */
3174 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Sean Christopherson55d23752018-12-03 13:53:18 -08003175 }
3176 }
3177
3178 if (nested_cpu_has_posted_intr(vmcs12)) {
KarimAllah Ahmed3278e042019-01-31 21:24:38 +01003179 map = &vmx->nested.pi_desc_map;
3180
3181 if (!kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->posted_intr_desc_addr), map)) {
3182 vmx->nested.pi_desc =
3183 (struct pi_desc *)(((void *)map->hva) +
3184 offset_in_page(vmcs12->posted_intr_desc_addr));
3185 vmcs_write64(POSTED_INTR_DESC_ADDR,
3186 pfn_to_hpa(map->pfn) + offset_in_page(vmcs12->posted_intr_desc_addr));
Sean Christopherson55d23752018-12-03 13:53:18 -08003187 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003188 }
3189 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
Sean Christopherson2183f562019-05-07 12:17:56 -07003190 exec_controls_setbit(vmx, CPU_BASED_USE_MSR_BITMAPS);
Sean Christopherson55d23752018-12-03 13:53:18 -08003191 else
Sean Christopherson2183f562019-05-07 12:17:56 -07003192 exec_controls_clearbit(vmx, CPU_BASED_USE_MSR_BITMAPS);
Paolo Bonzini9a78e152021-01-08 11:43:08 -05003193
3194 return true;
3195}
3196
3197static bool vmx_get_nested_state_pages(struct kvm_vcpu *vcpu)
3198{
Vitaly Kuznetsovf5c7e842021-05-03 17:08:51 +02003199 if (!nested_get_evmcs_page(vcpu)) {
3200 pr_debug_ratelimited("%s: enlightened vmptrld failed\n",
3201 __func__);
3202 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3203 vcpu->run->internal.suberror =
3204 KVM_INTERNAL_ERROR_EMULATION;
3205 vcpu->run->internal.ndata = 0;
3206
Paolo Bonzini9a78e152021-01-08 11:43:08 -05003207 return false;
Vitaly Kuznetsovf5c7e842021-05-03 17:08:51 +02003208 }
Paolo Bonzini9a78e152021-01-08 11:43:08 -05003209
3210 if (is_guest_mode(vcpu) && !nested_get_vmcs12_pages(vcpu))
3211 return false;
3212
Jim Mattson671ddc72019-10-15 10:44:05 -07003213 return true;
Sean Christopherson55d23752018-12-03 13:53:18 -08003214}
3215
Sean Christopherson02f5fb22020-06-22 14:58:32 -07003216static int nested_vmx_write_pml_buffer(struct kvm_vcpu *vcpu, gpa_t gpa)
3217{
3218 struct vmcs12 *vmcs12;
3219 struct vcpu_vmx *vmx = to_vmx(vcpu);
3220 gpa_t dst;
3221
3222 if (WARN_ON_ONCE(!is_guest_mode(vcpu)))
3223 return 0;
3224
3225 if (WARN_ON_ONCE(vmx->nested.pml_full))
3226 return 1;
3227
3228 /*
3229 * Check if PML is enabled for the nested guest. Whether eptp bit 6 is
3230 * set is already checked as part of A/D emulation.
3231 */
3232 vmcs12 = get_vmcs12(vcpu);
3233 if (!nested_cpu_has_pml(vmcs12))
3234 return 0;
3235
3236 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
3237 vmx->nested.pml_full = true;
3238 return 1;
3239 }
3240
3241 gpa &= ~0xFFFull;
3242 dst = vmcs12->pml_address + sizeof(u64) * vmcs12->guest_pml_index;
3243
3244 if (kvm_write_guest_page(vcpu->kvm, gpa_to_gfn(dst), &gpa,
3245 offset_in_page(dst), sizeof(gpa)))
3246 return 0;
3247
3248 vmcs12->guest_pml_index--;
3249
3250 return 0;
3251}
3252
Sean Christopherson55d23752018-12-03 13:53:18 -08003253/*
3254 * Intel's VMX Instruction Reference specifies a common set of prerequisites
3255 * for running VMX instructions (except VMXON, whose prerequisites are
3256 * slightly different). It also specifies what exception to inject otherwise.
3257 * Note that many of these exceptions have priority over VM exits, so they
3258 * don't have to be checked again here.
3259 */
3260static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
3261{
3262 if (!to_vmx(vcpu)->nested.vmxon) {
3263 kvm_queue_exception(vcpu, UD_VECTOR);
3264 return 0;
3265 }
3266
3267 if (vmx_get_cpl(vcpu)) {
3268 kvm_inject_gp(vcpu, 0);
3269 return 0;
3270 }
3271
3272 return 1;
3273}
3274
3275static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu)
3276{
3277 u8 rvi = vmx_get_rvi();
3278 u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI);
3279
3280 return ((rvi & 0xf0) > (vppr & 0xf0));
3281}
3282
3283static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
3284 struct vmcs12 *vmcs12);
3285
3286/*
3287 * If from_vmentry is false, this is being called from state restore (either RSM
3288 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
Jim Mattson671ddc72019-10-15 10:44:05 -07003289 *
3290 * Returns:
Miaohe Lin463bfee2020-02-14 10:44:05 +08003291 * NVMX_VMENTRY_SUCCESS: Entered VMX non-root mode
3292 * NVMX_VMENTRY_VMFAIL: Consistency check VMFail
3293 * NVMX_VMENTRY_VMEXIT: Consistency check VMExit
3294 * NVMX_VMENTRY_KVM_INTERNAL_ERROR: KVM internal error
Sean Christopherson55d23752018-12-03 13:53:18 -08003295 */
Jim Mattson671ddc72019-10-15 10:44:05 -07003296enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu,
3297 bool from_vmentry)
Sean Christopherson55d23752018-12-03 13:53:18 -08003298{
3299 struct vcpu_vmx *vmx = to_vmx(vcpu);
3300 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Sean Christopherson68cda402020-05-11 15:05:29 -07003301 enum vm_entry_failure_code entry_failure_code;
Sean Christopherson55d23752018-12-03 13:53:18 -08003302 bool evaluate_pending_interrupts;
Sean Christopherson8e533242020-11-06 17:03:12 +08003303 union vmx_exit_reason exit_reason = {
3304 .basic = EXIT_REASON_INVALID_STATE,
3305 .failed_vmentry = 1,
3306 };
3307 u32 failed_index;
Sean Christopherson55d23752018-12-03 13:53:18 -08003308
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07003309 if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu))
3310 kvm_vcpu_flush_tlb_current(vcpu);
3311
Sean Christopherson2183f562019-05-07 12:17:56 -07003312 evaluate_pending_interrupts = exec_controls_get(vmx) &
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08003313 (CPU_BASED_INTR_WINDOW_EXITING | CPU_BASED_NMI_WINDOW_EXITING);
Sean Christopherson55d23752018-12-03 13:53:18 -08003314 if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu))
3315 evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu);
3316
3317 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
3318 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
3319 if (kvm_mpx_supported() &&
3320 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
3321 vmx->nested.vmcs01_guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
3322
Sean Christophersonf087a022019-06-07 11:55:34 -07003323 /*
3324 * Overwrite vmcs01.GUEST_CR3 with L1's CR3 if EPT is disabled *and*
3325 * nested early checks are disabled. In the event of a "late" VM-Fail,
3326 * i.e. a VM-Fail detected by hardware but not KVM, KVM must unwind its
3327 * software model to the pre-VMEntry host state. When EPT is disabled,
3328 * GUEST_CR3 holds KVM's shadow CR3, not L1's "real" CR3, which causes
3329 * nested_vmx_restore_host_state() to corrupt vcpu->arch.cr3. Stuffing
3330 * vmcs01.GUEST_CR3 results in the unwind naturally setting arch.cr3 to
3331 * the correct value. Smashing vmcs01.GUEST_CR3 is safe because nested
3332 * VM-Exits, and the unwind, reset KVM's MMU, i.e. vmcs01.GUEST_CR3 is
3333 * guaranteed to be overwritten with a shadow CR3 prior to re-entering
3334 * L1. Don't stuff vmcs01.GUEST_CR3 when using nested early checks as
3335 * KVM modifies vcpu->arch.cr3 if and only if the early hardware checks
3336 * pass, and early VM-Fails do not reset KVM's MMU, i.e. the VM-Fail
3337 * path would need to manually save/restore vmcs01.GUEST_CR3.
3338 */
3339 if (!enable_ept && !nested_early_check)
3340 vmcs_writel(GUEST_CR3, vcpu->arch.cr3);
3341
Sean Christopherson55d23752018-12-03 13:53:18 -08003342 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
3343
3344 prepare_vmcs02_early(vmx, vmcs12);
3345
3346 if (from_vmentry) {
Sean Christophersonb89d5ad2020-09-23 11:44:47 -07003347 if (unlikely(!nested_get_vmcs12_pages(vcpu))) {
3348 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson671ddc72019-10-15 10:44:05 -07003349 return NVMX_VMENTRY_KVM_INTERNAL_ERROR;
Sean Christophersonb89d5ad2020-09-23 11:44:47 -07003350 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003351
3352 if (nested_vmx_check_vmentry_hw(vcpu)) {
3353 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson671ddc72019-10-15 10:44:05 -07003354 return NVMX_VMENTRY_VMFAIL;
Sean Christopherson55d23752018-12-03 13:53:18 -08003355 }
3356
Sean Christopherson68cda402020-05-11 15:05:29 -07003357 if (nested_vmx_check_guest_state(vcpu, vmcs12,
3358 &entry_failure_code)) {
Sean Christopherson8e533242020-11-06 17:03:12 +08003359 exit_reason.basic = EXIT_REASON_INVALID_STATE;
Sean Christopherson68cda402020-05-11 15:05:29 -07003360 vmcs12->exit_qualification = entry_failure_code;
Sean Christopherson55d23752018-12-03 13:53:18 -08003361 goto vmentry_fail_vmexit;
Sean Christopherson68cda402020-05-11 15:05:29 -07003362 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003363 }
3364
3365 enter_guest_mode(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08003366
Sean Christopherson68cda402020-05-11 15:05:29 -07003367 if (prepare_vmcs02(vcpu, vmcs12, &entry_failure_code)) {
Sean Christopherson8e533242020-11-06 17:03:12 +08003368 exit_reason.basic = EXIT_REASON_INVALID_STATE;
Sean Christopherson68cda402020-05-11 15:05:29 -07003369 vmcs12->exit_qualification = entry_failure_code;
Sean Christopherson55d23752018-12-03 13:53:18 -08003370 goto vmentry_fail_vmexit_guest_mode;
Sean Christopherson68cda402020-05-11 15:05:29 -07003371 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003372
3373 if (from_vmentry) {
Sean Christopherson68cda402020-05-11 15:05:29 -07003374 failed_index = nested_vmx_load_msr(vcpu,
3375 vmcs12->vm_entry_msr_load_addr,
3376 vmcs12->vm_entry_msr_load_count);
3377 if (failed_index) {
Sean Christopherson8e533242020-11-06 17:03:12 +08003378 exit_reason.basic = EXIT_REASON_MSR_LOAD_FAIL;
Sean Christopherson68cda402020-05-11 15:05:29 -07003379 vmcs12->exit_qualification = failed_index;
Sean Christopherson55d23752018-12-03 13:53:18 -08003380 goto vmentry_fail_vmexit_guest_mode;
Sean Christopherson68cda402020-05-11 15:05:29 -07003381 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003382 } else {
3383 /*
3384 * The MMU is not initialized to point at the right entities yet and
3385 * "get pages" would need to read data from the guest (i.e. we will
3386 * need to perform gpa to hpa translation). Request a call
3387 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
3388 * have already been set at vmentry time and should not be reset.
3389 */
Paolo Bonzini729c15c2020-09-22 06:53:57 -04003390 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08003391 }
3392
3393 /*
3394 * If L1 had a pending IRQ/NMI until it executed
3395 * VMLAUNCH/VMRESUME which wasn't delivered because it was
3396 * disallowed (e.g. interrupts disabled), L0 needs to
3397 * evaluate if this pending event should cause an exit from L2
3398 * to L1 or delivered directly to L2 (e.g. In case L1 don't
3399 * intercept EXTERNAL_INTERRUPT).
3400 *
3401 * Usually this would be handled by the processor noticing an
3402 * IRQ/NMI window request, or checking RVI during evaluation of
3403 * pending virtual interrupts. However, this setting was done
3404 * on VMCS01 and now VMCS02 is active instead. Thus, we force L0
3405 * to perform pending event evaluation by requesting a KVM_REQ_EVENT.
3406 */
3407 if (unlikely(evaluate_pending_interrupts))
3408 kvm_make_request(KVM_REQ_EVENT, vcpu);
3409
3410 /*
Paolo Bonzini359a6c32019-01-29 19:14:46 +01003411 * Do not start the preemption timer hrtimer until after we know
3412 * we are successful, so that only nested_vmx_vmexit needs to cancel
3413 * the timer.
3414 */
3415 vmx->nested.preemption_timer_expired = false;
Peter Shier850448f2020-05-26 14:51:06 -07003416 if (nested_cpu_has_preemption_timer(vmcs12)) {
3417 u64 timer_value = vmx_calc_preemption_timer_value(vcpu);
3418 vmx_start_preemption_timer(vcpu, timer_value);
3419 }
Paolo Bonzini359a6c32019-01-29 19:14:46 +01003420
3421 /*
Sean Christopherson55d23752018-12-03 13:53:18 -08003422 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
3423 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
3424 * returned as far as L1 is concerned. It will only return (and set
3425 * the success flag) when L2 exits (see nested_vmx_vmexit()).
3426 */
Jim Mattson671ddc72019-10-15 10:44:05 -07003427 return NVMX_VMENTRY_SUCCESS;
Sean Christopherson55d23752018-12-03 13:53:18 -08003428
3429 /*
3430 * A failed consistency check that leads to a VMExit during L1's
3431 * VMEnter to L2 is a variation of a normal VMexit, as explained in
3432 * 26.7 "VM-entry failures during or after loading guest state".
3433 */
3434vmentry_fail_vmexit_guest_mode:
Xiaoyao Li5e3d3942019-12-06 16:45:26 +08003435 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING)
Sean Christopherson55d23752018-12-03 13:53:18 -08003436 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
3437 leave_guest_mode(vcpu);
3438
3439vmentry_fail_vmexit:
3440 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
3441
3442 if (!from_vmentry)
Jim Mattson671ddc72019-10-15 10:44:05 -07003443 return NVMX_VMENTRY_VMEXIT;
Sean Christopherson55d23752018-12-03 13:53:18 -08003444
3445 load_vmcs12_host_state(vcpu, vmcs12);
Sean Christopherson8e533242020-11-06 17:03:12 +08003446 vmcs12->vm_exit_reason = exit_reason.full;
Sean Christopherson55d23752018-12-03 13:53:18 -08003447 if (enable_shadow_vmcs || vmx->nested.hv_evmcs)
Sean Christopherson3731905ef2019-05-07 08:36:27 -07003448 vmx->nested.need_vmcs12_to_shadow_sync = true;
Jim Mattson671ddc72019-10-15 10:44:05 -07003449 return NVMX_VMENTRY_VMEXIT;
Sean Christopherson55d23752018-12-03 13:53:18 -08003450}
3451
3452/*
3453 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
3454 * for running an L2 nested guest.
3455 */
3456static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
3457{
3458 struct vmcs12 *vmcs12;
Jim Mattson671ddc72019-10-15 10:44:05 -07003459 enum nvmx_vmentry_status status;
Sean Christopherson55d23752018-12-03 13:53:18 -08003460 struct vcpu_vmx *vmx = to_vmx(vcpu);
3461 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003462 enum nested_evmptrld_status evmptrld_status;
Sean Christopherson55d23752018-12-03 13:53:18 -08003463
Dongli Zhang43c11d92021-03-05 14:57:47 -08003464 ++vcpu->stat.nested_run;
3465
Sean Christopherson55d23752018-12-03 13:53:18 -08003466 if (!nested_vmx_check_permission(vcpu))
3467 return 1;
3468
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003469 evmptrld_status = nested_vmx_handle_enlightened_vmptrld(vcpu, launch);
3470 if (evmptrld_status == EVMPTRLD_ERROR) {
3471 kvm_queue_exception(vcpu, UD_VECTOR);
Sean Christopherson55d23752018-12-03 13:53:18 -08003472 return 1;
Sean Christophersonfc595f32020-08-12 11:06:15 -07003473 } else if (CC(evmptrld_status == EVMPTRLD_VMFAIL)) {
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003474 return nested_vmx_failInvalid(vcpu);
3475 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003476
Sean Christophersonfc595f32020-08-12 11:06:15 -07003477 if (CC(!vmx->nested.hv_evmcs && vmx->nested.current_vmptr == -1ull))
Sean Christopherson55d23752018-12-03 13:53:18 -08003478 return nested_vmx_failInvalid(vcpu);
3479
3480 vmcs12 = get_vmcs12(vcpu);
3481
3482 /*
3483 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
3484 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
3485 * rather than RFLAGS.ZF, and no error number is stored to the
3486 * VM-instruction error field.
3487 */
Sean Christophersonfc595f32020-08-12 11:06:15 -07003488 if (CC(vmcs12->hdr.shadow_vmcs))
Sean Christopherson55d23752018-12-03 13:53:18 -08003489 return nested_vmx_failInvalid(vcpu);
3490
3491 if (vmx->nested.hv_evmcs) {
3492 copy_enlightened_to_vmcs12(vmx);
3493 /* Enlightened VMCS doesn't have launch state */
3494 vmcs12->launch_state = !launch;
3495 } else if (enable_shadow_vmcs) {
3496 copy_shadow_to_vmcs12(vmx);
3497 }
3498
3499 /*
3500 * The nested entry process starts with enforcing various prerequisites
3501 * on vmcs12 as required by the Intel SDM, and act appropriately when
3502 * they fail: As the SDM explains, some conditions should cause the
3503 * instruction to fail, while others will cause the instruction to seem
3504 * to succeed, but return an EXIT_REASON_INVALID_STATE.
3505 * To speed up the normal (success) code path, we should avoid checking
3506 * for misconfigurations which will anyway be caught by the processor
3507 * when using the merged vmcs02.
3508 */
Sean Christophersonfc595f32020-08-12 11:06:15 -07003509 if (CC(interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS))
Sean Christophersonb2656e42020-06-08 18:56:07 -07003510 return nested_vmx_fail(vcpu, VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
Sean Christopherson55d23752018-12-03 13:53:18 -08003511
Sean Christophersonfc595f32020-08-12 11:06:15 -07003512 if (CC(vmcs12->launch_state == launch))
Sean Christophersonb2656e42020-06-08 18:56:07 -07003513 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08003514 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
3515 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
3516
Paolo Bonzini98d9e852019-04-12 10:19:57 +02003517 if (nested_vmx_check_controls(vcpu, vmcs12))
Sean Christophersonb2656e42020-06-08 18:56:07 -07003518 return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Sean Christopherson5478ba32019-04-11 12:18:06 -07003519
Paolo Bonzini98d9e852019-04-12 10:19:57 +02003520 if (nested_vmx_check_host_state(vcpu, vmcs12))
Sean Christophersonb2656e42020-06-08 18:56:07 -07003521 return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
Sean Christopherson55d23752018-12-03 13:53:18 -08003522
3523 /*
3524 * We're finally done with prerequisite checking, and can start with
3525 * the nested entry.
3526 */
3527 vmx->nested.nested_run_pending = 1;
Peter Shier850448f2020-05-26 14:51:06 -07003528 vmx->nested.has_preemption_timer_deadline = false;
Jim Mattson671ddc72019-10-15 10:44:05 -07003529 status = nested_vmx_enter_non_root_mode(vcpu, true);
3530 if (unlikely(status != NVMX_VMENTRY_SUCCESS))
3531 goto vmentry_failed;
Sean Christopherson55d23752018-12-03 13:53:18 -08003532
Sean Christopherson25bb2cf2020-08-12 10:51:29 -07003533 /* Emulate processing of posted interrupts on VM-Enter. */
3534 if (nested_cpu_has_posted_intr(vmcs12) &&
3535 kvm_apic_has_interrupt(vcpu) == vmx->nested.posted_intr_nv) {
3536 vmx->nested.pi_pending = true;
3537 kvm_make_request(KVM_REQ_EVENT, vcpu);
3538 kvm_apic_clear_irr(vcpu, vmx->nested.posted_intr_nv);
3539 }
3540
Sean Christopherson55d23752018-12-03 13:53:18 -08003541 /* Hide L1D cache contents from the nested guest. */
3542 vmx->vcpu.arch.l1tf_flush_l1d = true;
3543
3544 /*
3545 * Must happen outside of nested_vmx_enter_non_root_mode() as it will
3546 * also be used as part of restoring nVMX state for
3547 * snapshot restore (migration).
3548 *
3549 * In this flow, it is assumed that vmcs12 cache was
Ingo Molnar163b0992021-03-21 22:28:53 +01003550 * transferred as part of captured nVMX state and should
Sean Christopherson55d23752018-12-03 13:53:18 -08003551 * therefore not be read from guest memory (which may not
3552 * exist on destination host yet).
3553 */
3554 nested_cache_shadow_vmcs12(vcpu, vmcs12);
3555
Yadong Qibf0cd882020-11-06 14:51:22 +08003556 switch (vmcs12->guest_activity_state) {
3557 case GUEST_ACTIVITY_HLT:
3558 /*
3559 * If we're entering a halted L2 vcpu and the L2 vcpu won't be
3560 * awakened by event injection or by an NMI-window VM-exit or
3561 * by an interrupt-window VM-exit, halt the vcpu.
3562 */
3563 if (!(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) &&
3564 !nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING) &&
3565 !(nested_cpu_has(vmcs12, CPU_BASED_INTR_WINDOW_EXITING) &&
3566 (vmcs12->guest_rflags & X86_EFLAGS_IF))) {
3567 vmx->nested.nested_run_pending = 0;
3568 return kvm_vcpu_halt(vcpu);
3569 }
3570 break;
3571 case GUEST_ACTIVITY_WAIT_SIPI:
Sean Christopherson55d23752018-12-03 13:53:18 -08003572 vmx->nested.nested_run_pending = 0;
Yadong Qibf0cd882020-11-06 14:51:22 +08003573 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
3574 break;
3575 default:
3576 break;
Sean Christopherson55d23752018-12-03 13:53:18 -08003577 }
Yadong Qibf0cd882020-11-06 14:51:22 +08003578
Sean Christopherson55d23752018-12-03 13:53:18 -08003579 return 1;
Jim Mattson671ddc72019-10-15 10:44:05 -07003580
3581vmentry_failed:
3582 vmx->nested.nested_run_pending = 0;
3583 if (status == NVMX_VMENTRY_KVM_INTERNAL_ERROR)
3584 return 0;
3585 if (status == NVMX_VMENTRY_VMEXIT)
3586 return 1;
3587 WARN_ON_ONCE(status != NVMX_VMENTRY_VMFAIL);
Sean Christophersonb2656e42020-06-08 18:56:07 -07003588 return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Sean Christopherson55d23752018-12-03 13:53:18 -08003589}
3590
3591/*
3592 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
Miaohe Lin67b0ae42019-12-11 14:26:22 +08003593 * because L2 may have changed some cr0 bits directly (CR0_GUEST_HOST_MASK).
Sean Christopherson55d23752018-12-03 13:53:18 -08003594 * This function returns the new value we should put in vmcs12.guest_cr0.
3595 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
3596 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
3597 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
3598 * didn't trap the bit, because if L1 did, so would L0).
3599 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
3600 * been modified by L2, and L1 knows it. So just leave the old value of
3601 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
3602 * isn't relevant, because if L0 traps this bit it can set it to anything.
3603 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
3604 * changed these bits, and therefore they need to be updated, but L0
3605 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
3606 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
3607 */
3608static inline unsigned long
3609vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
3610{
3611 return
3612 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
3613 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
3614 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
3615 vcpu->arch.cr0_guest_owned_bits));
3616}
3617
3618static inline unsigned long
3619vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
3620{
3621 return
3622 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
3623 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
3624 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
3625 vcpu->arch.cr4_guest_owned_bits));
3626}
3627
3628static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
3629 struct vmcs12 *vmcs12)
3630{
3631 u32 idt_vectoring;
3632 unsigned int nr;
3633
3634 if (vcpu->arch.exception.injected) {
3635 nr = vcpu->arch.exception.nr;
3636 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
3637
3638 if (kvm_exception_is_soft(nr)) {
3639 vmcs12->vm_exit_instruction_len =
3640 vcpu->arch.event_exit_inst_len;
3641 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
3642 } else
3643 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
3644
3645 if (vcpu->arch.exception.has_error_code) {
3646 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
3647 vmcs12->idt_vectoring_error_code =
3648 vcpu->arch.exception.error_code;
3649 }
3650
3651 vmcs12->idt_vectoring_info_field = idt_vectoring;
3652 } else if (vcpu->arch.nmi_injected) {
3653 vmcs12->idt_vectoring_info_field =
3654 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
3655 } else if (vcpu->arch.interrupt.injected) {
3656 nr = vcpu->arch.interrupt.nr;
3657 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
3658
3659 if (vcpu->arch.interrupt.soft) {
3660 idt_vectoring |= INTR_TYPE_SOFT_INTR;
3661 vmcs12->vm_entry_instruction_len =
3662 vcpu->arch.event_exit_inst_len;
3663 } else
3664 idt_vectoring |= INTR_TYPE_EXT_INTR;
3665
3666 vmcs12->idt_vectoring_info_field = idt_vectoring;
3667 }
3668}
3669
3670
Paolo Bonzini96b100c2020-03-17 18:32:50 +01003671void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08003672{
3673 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3674 gfn_t gfn;
3675
3676 /*
3677 * Don't need to mark the APIC access page dirty; it is never
3678 * written to by the CPU during APIC virtualization.
3679 */
3680
3681 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
3682 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
3683 kvm_vcpu_mark_page_dirty(vcpu, gfn);
3684 }
3685
3686 if (nested_cpu_has_posted_intr(vmcs12)) {
3687 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
3688 kvm_vcpu_mark_page_dirty(vcpu, gfn);
3689 }
3690}
3691
Jim Mattson650293c2021-06-04 10:26:02 -07003692static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08003693{
3694 struct vcpu_vmx *vmx = to_vmx(vcpu);
3695 int max_irr;
3696 void *vapic_page;
3697 u16 status;
3698
3699 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
Jim Mattson650293c2021-06-04 10:26:02 -07003700 return 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08003701
3702 vmx->nested.pi_pending = false;
3703 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
Jim Mattson650293c2021-06-04 10:26:02 -07003704 return 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08003705
3706 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
3707 if (max_irr != 256) {
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003708 vapic_page = vmx->nested.virtual_apic_map.hva;
3709 if (!vapic_page)
Jim Mattson0fe998b2021-06-04 10:26:05 -07003710 goto mmio_needed;
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003711
Sean Christopherson55d23752018-12-03 13:53:18 -08003712 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
3713 vapic_page, &max_irr);
Sean Christopherson55d23752018-12-03 13:53:18 -08003714 status = vmcs_read16(GUEST_INTR_STATUS);
3715 if ((u8)max_irr > ((u8)status & 0xff)) {
3716 status &= ~0xff;
3717 status |= (u8)max_irr;
3718 vmcs_write16(GUEST_INTR_STATUS, status);
3719 }
3720 }
3721
3722 nested_mark_vmcs12_pages_dirty(vcpu);
Jim Mattson650293c2021-06-04 10:26:02 -07003723 return 0;
Jim Mattson0fe998b2021-06-04 10:26:05 -07003724
3725mmio_needed:
3726 kvm_handle_memory_failure(vcpu, X86EMUL_IO_NEEDED, NULL);
3727 return -ENXIO;
Sean Christopherson55d23752018-12-03 13:53:18 -08003728}
3729
3730static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3731 unsigned long exit_qual)
3732{
3733 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3734 unsigned int nr = vcpu->arch.exception.nr;
3735 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3736
3737 if (vcpu->arch.exception.has_error_code) {
3738 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3739 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3740 }
3741
3742 if (kvm_exception_is_soft(nr))
3743 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3744 else
3745 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3746
3747 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3748 vmx_get_nmi_mask(vcpu))
3749 intr_info |= INTR_INFO_UNBLOCK_NMI;
3750
3751 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3752}
3753
Oliver Upton684c0422020-02-07 02:36:05 -08003754/*
3755 * Returns true if a debug trap is pending delivery.
3756 *
3757 * In KVM, debug traps bear an exception payload. As such, the class of a #DB
3758 * exception may be inferred from the presence of an exception payload.
3759 */
3760static inline bool vmx_pending_dbg_trap(struct kvm_vcpu *vcpu)
3761{
3762 return vcpu->arch.exception.pending &&
3763 vcpu->arch.exception.nr == DB_VECTOR &&
3764 vcpu->arch.exception.payload;
3765}
3766
3767/*
3768 * Certain VM-exits set the 'pending debug exceptions' field to indicate a
3769 * recognized #DB (data or single-step) that has yet to be delivered. Since KVM
3770 * represents these debug traps with a payload that is said to be compatible
3771 * with the 'pending debug exceptions' field, write the payload to the VMCS
3772 * field if a VM-exit is delivered before the debug trap.
3773 */
3774static void nested_vmx_update_pending_dbg(struct kvm_vcpu *vcpu)
3775{
3776 if (vmx_pending_dbg_trap(vcpu))
3777 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
3778 vcpu->arch.exception.payload);
3779}
3780
Sean Christophersond2060bd2020-04-22 19:25:39 -07003781static bool nested_vmx_preemption_timer_pending(struct kvm_vcpu *vcpu)
3782{
3783 return nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
3784 to_vmx(vcpu)->nested.preemption_timer_expired;
3785}
3786
Sean Christophersona1c77ab2020-03-02 22:27:35 -08003787static int vmx_check_nested_events(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08003788{
3789 struct vcpu_vmx *vmx = to_vmx(vcpu);
3790 unsigned long exit_qual;
3791 bool block_nested_events =
3792 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003793 bool mtf_pending = vmx->nested.mtf_pending;
Liran Alon4b9852f2019-08-26 13:24:49 +03003794 struct kvm_lapic *apic = vcpu->arch.apic;
3795
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003796 /*
3797 * Clear the MTF state. If a higher priority VM-exit is delivered first,
3798 * this state is discarded.
3799 */
Oliver Upton5c8beb42020-04-06 20:12:37 +00003800 if (!block_nested_events)
3801 vmx->nested.mtf_pending = false;
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003802
Liran Alon4b9852f2019-08-26 13:24:49 +03003803 if (lapic_in_kernel(vcpu) &&
3804 test_bit(KVM_APIC_INIT, &apic->pending_events)) {
3805 if (block_nested_events)
3806 return -EBUSY;
Oliver Upton684c0422020-02-07 02:36:05 -08003807 nested_vmx_update_pending_dbg(vcpu);
Liran Alone64a8502019-11-11 14:16:05 +02003808 clear_bit(KVM_APIC_INIT, &apic->pending_events);
Yadong Qibf0cd882020-11-06 14:51:22 +08003809 if (vcpu->arch.mp_state != KVM_MP_STATE_INIT_RECEIVED)
3810 nested_vmx_vmexit(vcpu, EXIT_REASON_INIT_SIGNAL, 0, 0);
3811 return 0;
3812 }
3813
3814 if (lapic_in_kernel(vcpu) &&
3815 test_bit(KVM_APIC_SIPI, &apic->pending_events)) {
3816 if (block_nested_events)
3817 return -EBUSY;
3818
3819 clear_bit(KVM_APIC_SIPI, &apic->pending_events);
3820 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED)
3821 nested_vmx_vmexit(vcpu, EXIT_REASON_SIPI_SIGNAL, 0,
3822 apic->sipi_vector & 0xFFUL);
Liran Alon4b9852f2019-08-26 13:24:49 +03003823 return 0;
3824 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003825
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003826 /*
3827 * Process any exceptions that are not debug traps before MTF.
Maxim Levitsky4020da32021-04-01 17:38:14 +03003828 *
3829 * Note that only a pending nested run can block a pending exception.
3830 * Otherwise an injected NMI/interrupt should either be
3831 * lost or delivered to the nested hypervisor in the IDT_VECTORING_INFO,
3832 * while delivering the pending exception.
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003833 */
Maxim Levitsky4020da32021-04-01 17:38:14 +03003834
Sean Christopherson6ce347a2020-04-22 19:25:38 -07003835 if (vcpu->arch.exception.pending && !vmx_pending_dbg_trap(vcpu)) {
Maxim Levitsky4020da32021-04-01 17:38:14 +03003836 if (vmx->nested.nested_run_pending)
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003837 return -EBUSY;
Sean Christopherson6ce347a2020-04-22 19:25:38 -07003838 if (!nested_vmx_check_exception(vcpu, &exit_qual))
3839 goto no_vmexit;
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003840 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
3841 return 0;
3842 }
3843
3844 if (mtf_pending) {
3845 if (block_nested_events)
3846 return -EBUSY;
3847 nested_vmx_update_pending_dbg(vcpu);
3848 nested_vmx_vmexit(vcpu, EXIT_REASON_MONITOR_TRAP_FLAG, 0, 0);
3849 return 0;
3850 }
3851
Sean Christopherson6ce347a2020-04-22 19:25:38 -07003852 if (vcpu->arch.exception.pending) {
Maxim Levitsky4020da32021-04-01 17:38:14 +03003853 if (vmx->nested.nested_run_pending)
Sean Christopherson55d23752018-12-03 13:53:18 -08003854 return -EBUSY;
Sean Christopherson6ce347a2020-04-22 19:25:38 -07003855 if (!nested_vmx_check_exception(vcpu, &exit_qual))
3856 goto no_vmexit;
Sean Christopherson55d23752018-12-03 13:53:18 -08003857 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
3858 return 0;
3859 }
3860
Sean Christophersond2060bd2020-04-22 19:25:39 -07003861 if (nested_vmx_preemption_timer_pending(vcpu)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08003862 if (block_nested_events)
3863 return -EBUSY;
3864 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
3865 return 0;
3866 }
3867
Sean Christopherson1cd2f0b2020-04-22 19:25:46 -07003868 if (vcpu->arch.smi_pending && !is_smm(vcpu)) {
3869 if (block_nested_events)
3870 return -EBUSY;
3871 goto no_vmexit;
3872 }
3873
Sean Christopherson15ff0b42020-04-22 19:25:45 -07003874 if (vcpu->arch.nmi_pending && !vmx_nmi_blocked(vcpu)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08003875 if (block_nested_events)
3876 return -EBUSY;
Sean Christopherson15ff0b42020-04-22 19:25:45 -07003877 if (!nested_exit_on_nmi(vcpu))
3878 goto no_vmexit;
3879
Sean Christopherson55d23752018-12-03 13:53:18 -08003880 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
3881 NMI_VECTOR | INTR_TYPE_NMI_INTR |
3882 INTR_INFO_VALID_MASK, 0);
3883 /*
3884 * The NMI-triggered VM exit counts as injection:
3885 * clear this one and block further NMIs.
3886 */
3887 vcpu->arch.nmi_pending = 0;
3888 vmx_set_nmi_mask(vcpu, true);
3889 return 0;
3890 }
3891
Sean Christopherson15ff0b42020-04-22 19:25:45 -07003892 if (kvm_cpu_has_interrupt(vcpu) && !vmx_interrupt_blocked(vcpu)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08003893 if (block_nested_events)
3894 return -EBUSY;
Sean Christopherson15ff0b42020-04-22 19:25:45 -07003895 if (!nested_exit_on_intr(vcpu))
3896 goto no_vmexit;
Sean Christopherson55d23752018-12-03 13:53:18 -08003897 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
3898 return 0;
3899 }
3900
Sean Christopherson6ce347a2020-04-22 19:25:38 -07003901no_vmexit:
Jim Mattson650293c2021-06-04 10:26:02 -07003902 return vmx_complete_nested_posted_interrupt(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08003903}
3904
3905static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
3906{
3907 ktime_t remaining =
3908 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
3909 u64 value;
3910
3911 if (ktime_to_ns(remaining) <= 0)
3912 return 0;
3913
3914 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
3915 do_div(value, 1000000);
3916 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
3917}
3918
Sean Christopherson7952d762019-05-07 08:36:29 -07003919static bool is_vmcs12_ext_field(unsigned long field)
Sean Christopherson55d23752018-12-03 13:53:18 -08003920{
Sean Christopherson7952d762019-05-07 08:36:29 -07003921 switch (field) {
3922 case GUEST_ES_SELECTOR:
3923 case GUEST_CS_SELECTOR:
3924 case GUEST_SS_SELECTOR:
3925 case GUEST_DS_SELECTOR:
3926 case GUEST_FS_SELECTOR:
3927 case GUEST_GS_SELECTOR:
3928 case GUEST_LDTR_SELECTOR:
3929 case GUEST_TR_SELECTOR:
3930 case GUEST_ES_LIMIT:
3931 case GUEST_CS_LIMIT:
3932 case GUEST_SS_LIMIT:
3933 case GUEST_DS_LIMIT:
3934 case GUEST_FS_LIMIT:
3935 case GUEST_GS_LIMIT:
3936 case GUEST_LDTR_LIMIT:
3937 case GUEST_TR_LIMIT:
3938 case GUEST_GDTR_LIMIT:
3939 case GUEST_IDTR_LIMIT:
3940 case GUEST_ES_AR_BYTES:
3941 case GUEST_DS_AR_BYTES:
3942 case GUEST_FS_AR_BYTES:
3943 case GUEST_GS_AR_BYTES:
3944 case GUEST_LDTR_AR_BYTES:
3945 case GUEST_TR_AR_BYTES:
3946 case GUEST_ES_BASE:
3947 case GUEST_CS_BASE:
3948 case GUEST_SS_BASE:
3949 case GUEST_DS_BASE:
3950 case GUEST_FS_BASE:
3951 case GUEST_GS_BASE:
3952 case GUEST_LDTR_BASE:
3953 case GUEST_TR_BASE:
3954 case GUEST_GDTR_BASE:
3955 case GUEST_IDTR_BASE:
3956 case GUEST_PENDING_DBG_EXCEPTIONS:
3957 case GUEST_BNDCFGS:
3958 return true;
3959 default:
3960 break;
3961 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003962
Sean Christopherson7952d762019-05-07 08:36:29 -07003963 return false;
3964}
3965
3966static void sync_vmcs02_to_vmcs12_rare(struct kvm_vcpu *vcpu,
3967 struct vmcs12 *vmcs12)
3968{
3969 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08003970
3971 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
3972 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
3973 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
3974 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
3975 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
3976 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
3977 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
3978 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
3979 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
3980 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
3981 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
3982 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
3983 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
3984 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
3985 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
3986 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
3987 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
3988 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
3989 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
Sean Christopherson55d23752018-12-03 13:53:18 -08003990 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
3991 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
3992 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
3993 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
3994 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
3995 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
3996 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
3997 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
3998 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
3999 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
4000 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
4001 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
4002 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
4003 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
4004 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
Sean Christopherson7952d762019-05-07 08:36:29 -07004005 vmcs12->guest_pending_dbg_exceptions =
4006 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
4007 if (kvm_mpx_supported())
4008 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
4009
4010 vmx->nested.need_sync_vmcs02_to_vmcs12_rare = false;
4011}
4012
4013static void copy_vmcs02_to_vmcs12_rare(struct kvm_vcpu *vcpu,
4014 struct vmcs12 *vmcs12)
4015{
4016 struct vcpu_vmx *vmx = to_vmx(vcpu);
4017 int cpu;
4018
4019 if (!vmx->nested.need_sync_vmcs02_to_vmcs12_rare)
4020 return;
4021
4022
4023 WARN_ON_ONCE(vmx->loaded_vmcs != &vmx->vmcs01);
4024
4025 cpu = get_cpu();
4026 vmx->loaded_vmcs = &vmx->nested.vmcs02;
Sean Christopherson1af1bb02020-05-06 16:58:50 -07004027 vmx_vcpu_load_vmcs(vcpu, cpu, &vmx->vmcs01);
Sean Christopherson7952d762019-05-07 08:36:29 -07004028
4029 sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
4030
4031 vmx->loaded_vmcs = &vmx->vmcs01;
Sean Christopherson1af1bb02020-05-06 16:58:50 -07004032 vmx_vcpu_load_vmcs(vcpu, cpu, &vmx->nested.vmcs02);
Sean Christopherson7952d762019-05-07 08:36:29 -07004033 put_cpu();
4034}
4035
4036/*
4037 * Update the guest state fields of vmcs12 to reflect changes that
4038 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
4039 * VM-entry controls is also updated, since this is really a guest
4040 * state bit.)
4041 */
4042static void sync_vmcs02_to_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
4043{
4044 struct vcpu_vmx *vmx = to_vmx(vcpu);
4045
4046 if (vmx->nested.hv_evmcs)
4047 sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
4048
4049 vmx->nested.need_sync_vmcs02_to_vmcs12_rare = !vmx->nested.hv_evmcs;
4050
4051 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
4052 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
4053
4054 vmcs12->guest_rsp = kvm_rsp_read(vcpu);
4055 vmcs12->guest_rip = kvm_rip_read(vcpu);
4056 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
4057
4058 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
4059 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
Sean Christopherson55d23752018-12-03 13:53:18 -08004060
4061 vmcs12->guest_interruptibility_info =
4062 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
Sean Christopherson7952d762019-05-07 08:36:29 -07004063
Sean Christopherson55d23752018-12-03 13:53:18 -08004064 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
4065 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
Yadong Qibf0cd882020-11-06 14:51:22 +08004066 else if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED)
4067 vmcs12->guest_activity_state = GUEST_ACTIVITY_WAIT_SIPI;
Sean Christopherson55d23752018-12-03 13:53:18 -08004068 else
4069 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
4070
Paolo Bonzinib4b65b52019-01-29 19:12:35 +01004071 if (nested_cpu_has_preemption_timer(vmcs12) &&
Peter Shier850448f2020-05-26 14:51:06 -07004072 vmcs12->vm_exit_controls & VM_EXIT_SAVE_VMX_PREEMPTION_TIMER &&
4073 !vmx->nested.nested_run_pending)
4074 vmcs12->vmx_preemption_timer_value =
4075 vmx_get_preemption_timer_value(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08004076
4077 /*
4078 * In some cases (usually, nested EPT), L2 is allowed to change its
4079 * own CR3 without exiting. If it has changed it, we must keep it.
4080 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
4081 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
4082 *
4083 * Additionally, restore L2's PDPTR to vmcs12.
4084 */
4085 if (enable_ept) {
4086 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Sean Christophersonc7554efc2019-05-07 09:06:40 -07004087 if (nested_cpu_has_ept(vmcs12) && is_pae_paging(vcpu)) {
4088 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
4089 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
4090 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
4091 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
4092 }
Sean Christopherson55d23752018-12-03 13:53:18 -08004093 }
4094
4095 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
4096
4097 if (nested_cpu_has_vid(vmcs12))
4098 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
4099
4100 vmcs12->vm_entry_controls =
4101 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
4102 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
4103
Sean Christopherson699a1ac2019-05-07 09:06:37 -07004104 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS)
Sean Christopherson55d23752018-12-03 13:53:18 -08004105 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
Sean Christopherson55d23752018-12-03 13:53:18 -08004106
Sean Christopherson55d23752018-12-03 13:53:18 -08004107 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
4108 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Sean Christopherson55d23752018-12-03 13:53:18 -08004109}
4110
4111/*
4112 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
4113 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
4114 * and this function updates it to reflect the changes to the guest state while
4115 * L2 was running (and perhaps made some exits which were handled directly by L0
4116 * without going back to L1), and to reflect the exit reason.
4117 * Note that we do not have to copy here all VMCS fields, just those that
4118 * could have changed by the L2 guest or the exit - i.e., the guest-state and
4119 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
4120 * which already writes to vmcs12 directly.
4121 */
4122static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004123 u32 vm_exit_reason, u32 exit_intr_info,
Sean Christopherson55d23752018-12-03 13:53:18 -08004124 unsigned long exit_qualification)
4125{
Sean Christopherson55d23752018-12-03 13:53:18 -08004126 /* update exit information fields: */
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004127 vmcs12->vm_exit_reason = vm_exit_reason;
Sean Christopherson3c0c2ad2021-04-12 16:21:37 +12004128 if (to_vmx(vcpu)->exit_reason.enclave_mode)
4129 vmcs12->vm_exit_reason |= VMX_EXIT_REASONS_SGX_ENCLAVE_MODE;
Sean Christopherson55d23752018-12-03 13:53:18 -08004130 vmcs12->exit_qualification = exit_qualification;
4131 vmcs12->vm_exit_intr_info = exit_intr_info;
4132
4133 vmcs12->idt_vectoring_info_field = 0;
4134 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
4135 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
4136
4137 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
4138 vmcs12->launch_state = 1;
4139
4140 /* vm_entry_intr_info_field is cleared on exit. Emulate this
4141 * instead of reading the real value. */
4142 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
4143
4144 /*
4145 * Transfer the event that L0 or L1 may wanted to inject into
4146 * L2 to IDT_VECTORING_INFO_FIELD.
4147 */
4148 vmcs12_save_pending_event(vcpu, vmcs12);
Krish Sadhukhana0d4f802018-12-04 19:00:13 -05004149
4150 /*
4151 * According to spec, there's no need to store the guest's
4152 * MSRs if the exit is due to a VM-entry failure that occurs
4153 * during or after loading the guest state. Since this exit
4154 * does not fall in that category, we need to save the MSRs.
4155 */
4156 if (nested_vmx_store_msr(vcpu,
4157 vmcs12->vm_exit_msr_store_addr,
4158 vmcs12->vm_exit_msr_store_count))
4159 nested_vmx_abort(vcpu,
4160 VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Sean Christopherson55d23752018-12-03 13:53:18 -08004161 }
4162
4163 /*
4164 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
4165 * preserved above and would only end up incorrectly in L1.
4166 */
4167 vcpu->arch.nmi_injected = false;
4168 kvm_clear_exception_queue(vcpu);
4169 kvm_clear_interrupt_queue(vcpu);
4170}
4171
4172/*
4173 * A part of what we need to when the nested L2 guest exits and we want to
4174 * run its L1 parent, is to reset L1's guest state to the host state specified
4175 * in vmcs12.
4176 * This function is to be called not only on normal nested exit, but also on
4177 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
4178 * Failures During or After Loading Guest State").
4179 * This function should be called when the active VMCS is L1's (vmcs01).
4180 */
4181static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
4182 struct vmcs12 *vmcs12)
4183{
Sean Christopherson68cda402020-05-11 15:05:29 -07004184 enum vm_entry_failure_code ignored;
Sean Christopherson55d23752018-12-03 13:53:18 -08004185 struct kvm_segment seg;
Sean Christopherson55d23752018-12-03 13:53:18 -08004186
4187 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
4188 vcpu->arch.efer = vmcs12->host_ia32_efer;
4189 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
4190 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
4191 else
4192 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
4193 vmx_set_efer(vcpu, vcpu->arch.efer);
4194
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02004195 kvm_rsp_write(vcpu, vmcs12->host_rsp);
4196 kvm_rip_write(vcpu, vmcs12->host_rip);
Sean Christopherson55d23752018-12-03 13:53:18 -08004197 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
4198 vmx_set_interrupt_shadow(vcpu, 0);
4199
4200 /*
4201 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
4202 * actually changed, because vmx_set_cr0 refers to efer set above.
4203 *
4204 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
4205 * (KVM doesn't change it);
4206 */
Sean Christophersonfa71e952020-07-02 21:04:22 -07004207 vcpu->arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS;
Sean Christopherson55d23752018-12-03 13:53:18 -08004208 vmx_set_cr0(vcpu, vmcs12->host_cr0);
4209
4210 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
4211 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
4212 vmx_set_cr4(vcpu, vmcs12->host_cr4);
4213
4214 nested_ept_uninit_mmu_context(vcpu);
4215
4216 /*
4217 * Only PDPTE load can fail as the value of cr3 was checked on entry and
4218 * couldn't have changed.
4219 */
Sean Christopherson68cda402020-05-11 15:05:29 -07004220 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &ignored))
Sean Christopherson55d23752018-12-03 13:53:18 -08004221 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
4222
Sean Christopherson50b265a2020-03-20 14:28:19 -07004223 nested_vmx_transition_tlb_flush(vcpu, vmcs12, false);
Sean Christopherson55d23752018-12-03 13:53:18 -08004224
4225 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
4226 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
4227 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
4228 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
4229 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
4230 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
4231 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
4232
4233 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
4234 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
4235 vmcs_write64(GUEST_BNDCFGS, 0);
4236
4237 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
4238 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
4239 vcpu->arch.pat = vmcs12->host_ia32_pat;
4240 }
4241 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Oliver Uptond1968422019-12-13 16:33:58 -08004242 WARN_ON_ONCE(kvm_set_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL,
4243 vmcs12->host_ia32_perf_global_ctrl));
Sean Christopherson55d23752018-12-03 13:53:18 -08004244
4245 /* Set L1 segment info according to Intel SDM
4246 27.5.2 Loading Host Segment and Descriptor-Table Registers */
4247 seg = (struct kvm_segment) {
4248 .base = 0,
4249 .limit = 0xFFFFFFFF,
4250 .selector = vmcs12->host_cs_selector,
4251 .type = 11,
4252 .present = 1,
4253 .s = 1,
4254 .g = 1
4255 };
4256 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
4257 seg.l = 1;
4258 else
4259 seg.db = 1;
4260 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
4261 seg = (struct kvm_segment) {
4262 .base = 0,
4263 .limit = 0xFFFFFFFF,
4264 .type = 3,
4265 .present = 1,
4266 .s = 1,
4267 .db = 1,
4268 .g = 1
4269 };
4270 seg.selector = vmcs12->host_ds_selector;
4271 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
4272 seg.selector = vmcs12->host_es_selector;
4273 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
4274 seg.selector = vmcs12->host_ss_selector;
4275 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
4276 seg.selector = vmcs12->host_fs_selector;
4277 seg.base = vmcs12->host_fs_base;
4278 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
4279 seg.selector = vmcs12->host_gs_selector;
4280 seg.base = vmcs12->host_gs_base;
4281 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
4282 seg = (struct kvm_segment) {
4283 .base = vmcs12->host_tr_base,
4284 .limit = 0x67,
4285 .selector = vmcs12->host_tr_selector,
4286 .type = 11,
4287 .present = 1
4288 };
4289 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
4290
4291 kvm_set_dr(vcpu, 7, 0x400);
4292 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
4293
4294 if (cpu_has_vmx_msr_bitmap())
4295 vmx_update_msr_bitmap(vcpu);
4296
4297 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
4298 vmcs12->vm_exit_msr_load_count))
4299 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
4300}
4301
4302static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx)
4303{
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07004304 struct vmx_uret_msr *efer_msr;
Sean Christopherson55d23752018-12-03 13:53:18 -08004305 unsigned int i;
4306
4307 if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER)
4308 return vmcs_read64(GUEST_IA32_EFER);
4309
4310 if (cpu_has_load_ia32_efer())
4311 return host_efer;
4312
4313 for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) {
4314 if (vmx->msr_autoload.guest.val[i].index == MSR_EFER)
4315 return vmx->msr_autoload.guest.val[i].value;
4316 }
4317
Sean Christophersond85a8032020-09-23 11:04:06 -07004318 efer_msr = vmx_find_uret_msr(vmx, MSR_EFER);
Sean Christopherson55d23752018-12-03 13:53:18 -08004319 if (efer_msr)
4320 return efer_msr->data;
4321
4322 return host_efer;
4323}
4324
4325static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu)
4326{
4327 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4328 struct vcpu_vmx *vmx = to_vmx(vcpu);
4329 struct vmx_msr_entry g, h;
Sean Christopherson55d23752018-12-03 13:53:18 -08004330 gpa_t gpa;
4331 u32 i, j;
4332
4333 vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT);
4334
4335 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
4336 /*
4337 * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set
4338 * as vmcs01.GUEST_DR7 contains a userspace defined value
4339 * and vcpu->arch.dr7 is not squirreled away before the
4340 * nested VMENTER (not worth adding a variable in nested_vmx).
4341 */
4342 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
4343 kvm_set_dr(vcpu, 7, DR7_FIXED_1);
4344 else
4345 WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7)));
4346 }
4347
4348 /*
4349 * Note that calling vmx_set_{efer,cr0,cr4} is important as they
4350 * handle a variety of side effects to KVM's software model.
4351 */
4352 vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx));
4353
Sean Christophersonfa71e952020-07-02 21:04:22 -07004354 vcpu->arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS;
Sean Christopherson55d23752018-12-03 13:53:18 -08004355 vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW));
4356
4357 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
4358 vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW));
4359
4360 nested_ept_uninit_mmu_context(vcpu);
Sean Christophersonf087a022019-06-07 11:55:34 -07004361 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07004362 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Sean Christopherson55d23752018-12-03 13:53:18 -08004363
4364 /*
4365 * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs
4366 * from vmcs01 (if necessary). The PDPTRs are not loaded on
4367 * VMFail, like everything else we just need to ensure our
4368 * software model is up-to-date.
4369 */
Sean Christopherson9932b492020-04-15 13:34:50 -07004370 if (enable_ept && is_pae_paging(vcpu))
Sean Christophersonf087a022019-06-07 11:55:34 -07004371 ept_save_pdptrs(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08004372
4373 kvm_mmu_reset_context(vcpu);
4374
4375 if (cpu_has_vmx_msr_bitmap())
4376 vmx_update_msr_bitmap(vcpu);
4377
4378 /*
4379 * This nasty bit of open coding is a compromise between blindly
4380 * loading L1's MSRs using the exit load lists (incorrect emulation
4381 * of VMFail), leaving the nested VM's MSRs in the software model
4382 * (incorrect behavior) and snapshotting the modified MSRs (too
4383 * expensive since the lists are unbound by hardware). For each
4384 * MSR that was (prematurely) loaded from the nested VMEntry load
4385 * list, reload it from the exit load list if it exists and differs
4386 * from the guest value. The intent is to stuff host state as
4387 * silently as possible, not to fully process the exit load list.
4388 */
Sean Christopherson55d23752018-12-03 13:53:18 -08004389 for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) {
4390 gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g));
4391 if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) {
4392 pr_debug_ratelimited(
4393 "%s read MSR index failed (%u, 0x%08llx)\n",
4394 __func__, i, gpa);
4395 goto vmabort;
4396 }
4397
4398 for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) {
4399 gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h));
4400 if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) {
4401 pr_debug_ratelimited(
4402 "%s read MSR failed (%u, 0x%08llx)\n",
4403 __func__, j, gpa);
4404 goto vmabort;
4405 }
4406 if (h.index != g.index)
4407 continue;
4408 if (h.value == g.value)
4409 break;
4410
4411 if (nested_vmx_load_msr_check(vcpu, &h)) {
4412 pr_debug_ratelimited(
4413 "%s check failed (%u, 0x%x, 0x%x)\n",
4414 __func__, j, h.index, h.reserved);
4415 goto vmabort;
4416 }
4417
Sean Christophersonf20935d2019-09-05 14:22:54 -07004418 if (kvm_set_msr(vcpu, h.index, h.value)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08004419 pr_debug_ratelimited(
4420 "%s WRMSR failed (%u, 0x%x, 0x%llx)\n",
4421 __func__, j, h.index, h.value);
4422 goto vmabort;
4423 }
4424 }
4425 }
4426
4427 return;
4428
4429vmabort:
4430 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
4431}
4432
4433/*
4434 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
4435 * and modify vmcs12 to make it see what it would expect to see there if
4436 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
4437 */
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004438void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason,
Sean Christopherson55d23752018-12-03 13:53:18 -08004439 u32 exit_intr_info, unsigned long exit_qualification)
4440{
4441 struct vcpu_vmx *vmx = to_vmx(vcpu);
4442 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4443
4444 /* trying to cancel vmlaunch/vmresume is a bug */
4445 WARN_ON_ONCE(vmx->nested.nested_run_pending);
4446
Sean Christophersoncb6a32c2021-03-02 09:45:14 -08004447 /* Similarly, triple faults in L2 should never escape. */
4448 WARN_ON_ONCE(kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu));
4449
Vitaly Kuznetsovf5c7e842021-05-03 17:08:51 +02004450 if (kvm_check_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu)) {
4451 /*
4452 * KVM_REQ_GET_NESTED_STATE_PAGES is also used to map
4453 * Enlightened VMCS after migration and we still need to
4454 * do that when something is forcing L2->L1 exit prior to
4455 * the first L2 run.
4456 */
4457 (void)nested_get_evmcs_page(vcpu);
4458 }
Maxim Levitskyf2c7ef32021-01-07 11:38:51 +02004459
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07004460 /* Service the TLB flush request for L2 before switching to L1. */
4461 if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu))
4462 kvm_vcpu_flush_tlb_current(vcpu);
4463
Peter Shier43fea4e2020-08-20 16:05:45 -07004464 /*
4465 * VCPU_EXREG_PDPTR will be clobbered in arch/x86/kvm/vmx/vmx.h between
4466 * now and the new vmentry. Ensure that the VMCS02 PDPTR fields are
4467 * up-to-date before switching to L1.
4468 */
4469 if (enable_ept && is_pae_paging(vcpu))
4470 vmx_ept_load_pdptrs(vcpu);
4471
Sean Christopherson55d23752018-12-03 13:53:18 -08004472 leave_guest_mode(vcpu);
4473
Paolo Bonzinib4b65b52019-01-29 19:12:35 +01004474 if (nested_cpu_has_preemption_timer(vmcs12))
4475 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
4476
Ilias Stamatisd041b5e2021-05-26 19:44:17 +01004477 if (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETTING)) {
4478 vcpu->arch.tsc_offset = vcpu->arch.l1_tsc_offset;
4479 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_TSC_SCALING))
4480 vcpu->arch.tsc_scaling_ratio = vcpu->arch.l1_tsc_scaling_ratio;
4481 }
Sean Christopherson55d23752018-12-03 13:53:18 -08004482
4483 if (likely(!vmx->fail)) {
Sean Christopherson3731905ef2019-05-07 08:36:27 -07004484 sync_vmcs02_to_vmcs12(vcpu, vmcs12);
Sean Christophersonf4f83162019-05-07 08:36:26 -07004485
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004486 if (vm_exit_reason != -1)
4487 prepare_vmcs12(vcpu, vmcs12, vm_exit_reason,
4488 exit_intr_info, exit_qualification);
Sean Christopherson55d23752018-12-03 13:53:18 -08004489
4490 /*
Sean Christopherson3731905ef2019-05-07 08:36:27 -07004491 * Must happen outside of sync_vmcs02_to_vmcs12() as it will
Sean Christopherson55d23752018-12-03 13:53:18 -08004492 * also be used to capture vmcs12 cache as part of
4493 * capturing nVMX state for snapshot (migration).
4494 *
4495 * Otherwise, this flush will dirty guest memory at a
4496 * point it is already assumed by user-space to be
4497 * immutable.
4498 */
4499 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08004500 } else {
4501 /*
4502 * The only expected VM-instruction error is "VM entry with
4503 * invalid control field(s)." Anything else indicates a
4504 * problem with L0. And we should never get here with a
4505 * VMFail of any type if early consistency checks are enabled.
4506 */
4507 WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) !=
4508 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
4509 WARN_ON_ONCE(nested_early_check);
4510 }
4511
4512 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
4513
4514 /* Update any VMCS fields that might have changed while L2 ran */
4515 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
4516 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
4517 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Ilias Stamatis1ab92872021-06-07 11:54:38 +01004518 if (kvm_has_tsc_control)
4519 vmcs_write64(TSC_MULTIPLIER, vcpu->arch.tsc_scaling_ratio);
4520
Liran Alon02d496cf2019-11-11 14:30:55 +02004521 if (vmx->nested.l1_tpr_threshold != -1)
4522 vmcs_write32(TPR_THRESHOLD, vmx->nested.l1_tpr_threshold);
Sean Christopherson55d23752018-12-03 13:53:18 -08004523
Sean Christopherson55d23752018-12-03 13:53:18 -08004524 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
4525 vmx->nested.change_vmcs01_virtual_apic_mode = false;
4526 vmx_set_virtual_apic_mode(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08004527 }
4528
Makarand Sonarea85863c2021-02-12 16:50:12 -08004529 if (vmx->nested.update_vmcs01_cpu_dirty_logging) {
4530 vmx->nested.update_vmcs01_cpu_dirty_logging = false;
4531 vmx_update_cpu_dirty_logging(vcpu);
4532 }
4533
Sean Christopherson55d23752018-12-03 13:53:18 -08004534 /* Unpin physical memory we referred to in vmcs02 */
4535 if (vmx->nested.apic_access_page) {
Liran Alonb11494b2019-11-21 00:31:47 +02004536 kvm_release_page_clean(vmx->nested.apic_access_page);
Sean Christopherson55d23752018-12-03 13:53:18 -08004537 vmx->nested.apic_access_page = NULL;
4538 }
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01004539 kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map, true);
KarimAllah Ahmed3278e042019-01-31 21:24:38 +01004540 kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map, true);
4541 vmx->nested.pi_desc = NULL;
Sean Christopherson55d23752018-12-03 13:53:18 -08004542
Sean Christopherson1196cb92020-03-20 14:28:23 -07004543 if (vmx->nested.reload_vmcs01_apic_access_page) {
4544 vmx->nested.reload_vmcs01_apic_access_page = false;
4545 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
4546 }
Sean Christopherson55d23752018-12-03 13:53:18 -08004547
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004548 if ((vm_exit_reason != -1) &&
4549 (enable_shadow_vmcs || vmx->nested.hv_evmcs))
Sean Christopherson3731905ef2019-05-07 08:36:27 -07004550 vmx->nested.need_vmcs12_to_shadow_sync = true;
Sean Christopherson55d23752018-12-03 13:53:18 -08004551
4552 /* in case we halted in L2 */
4553 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
4554
4555 if (likely(!vmx->fail)) {
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004556 if ((u16)vm_exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
Sean Christophersona1c77ab2020-03-02 22:27:35 -08004557 nested_exit_intr_ack_set(vcpu)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08004558 int irq = kvm_cpu_get_interrupt(vcpu);
4559 WARN_ON(irq < 0);
4560 vmcs12->vm_exit_intr_info = irq |
4561 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
4562 }
4563
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004564 if (vm_exit_reason != -1)
Sean Christopherson55d23752018-12-03 13:53:18 -08004565 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
4566 vmcs12->exit_qualification,
4567 vmcs12->idt_vectoring_info_field,
4568 vmcs12->vm_exit_intr_info,
4569 vmcs12->vm_exit_intr_error_code,
4570 KVM_ISA_VMX);
4571
4572 load_vmcs12_host_state(vcpu, vmcs12);
4573
4574 return;
4575 }
4576
4577 /*
4578 * After an early L2 VM-entry failure, we're now back
4579 * in L1 which thinks it just finished a VMLAUNCH or
4580 * VMRESUME instruction, so we need to set the failure
4581 * flag and the VM-instruction error field of the VMCS
4582 * accordingly, and skip the emulated instruction.
4583 */
Sean Christophersonb2656e42020-06-08 18:56:07 -07004584 (void)nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Sean Christopherson55d23752018-12-03 13:53:18 -08004585
4586 /*
4587 * Restore L1's host state to KVM's software model. We're here
4588 * because a consistency check was caught by hardware, which
4589 * means some amount of guest state has been propagated to KVM's
4590 * model and needs to be unwound to the host's state.
4591 */
4592 nested_vmx_restore_host_state(vcpu);
4593
4594 vmx->fail = 0;
4595}
4596
Sean Christophersoncb6a32c2021-03-02 09:45:14 -08004597static void nested_vmx_triple_fault(struct kvm_vcpu *vcpu)
4598{
4599 nested_vmx_vmexit(vcpu, EXIT_REASON_TRIPLE_FAULT, 0, 0);
4600}
4601
Sean Christopherson55d23752018-12-03 13:53:18 -08004602/*
4603 * Decode the memory-address operand of a vmx instruction, as recorded on an
4604 * exit caused by such an instruction (run by a guest hypervisor).
4605 * On success, returns 0. When the operand is invalid, returns 1 and throws
Miaohe Lin49f933d2020-02-27 11:20:54 +08004606 * #UD, #GP, or #SS.
Sean Christopherson55d23752018-12-03 13:53:18 -08004607 */
4608int get_vmx_mem_address(struct kvm_vcpu *vcpu, unsigned long exit_qualification,
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004609 u32 vmx_instruction_info, bool wr, int len, gva_t *ret)
Sean Christopherson55d23752018-12-03 13:53:18 -08004610{
4611 gva_t off;
4612 bool exn;
4613 struct kvm_segment s;
4614
4615 /*
4616 * According to Vol. 3B, "Information for VM Exits Due to Instruction
4617 * Execution", on an exit, vmx_instruction_info holds most of the
4618 * addressing components of the operand. Only the displacement part
4619 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
4620 * For how an actual address is calculated from all these components,
4621 * refer to Vol. 1, "Operand Addressing".
4622 */
4623 int scaling = vmx_instruction_info & 3;
4624 int addr_size = (vmx_instruction_info >> 7) & 7;
4625 bool is_reg = vmx_instruction_info & (1u << 10);
4626 int seg_reg = (vmx_instruction_info >> 15) & 7;
4627 int index_reg = (vmx_instruction_info >> 18) & 0xf;
4628 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
4629 int base_reg = (vmx_instruction_info >> 23) & 0xf;
4630 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
4631
4632 if (is_reg) {
4633 kvm_queue_exception(vcpu, UD_VECTOR);
4634 return 1;
4635 }
4636
4637 /* Addr = segment_base + offset */
4638 /* offset = base + [index * scale] + displacement */
4639 off = exit_qualification; /* holds the displacement */
Sean Christopherson946c5222019-01-23 14:39:23 -08004640 if (addr_size == 1)
4641 off = (gva_t)sign_extend64(off, 31);
4642 else if (addr_size == 0)
4643 off = (gva_t)sign_extend64(off, 15);
Sean Christopherson55d23752018-12-03 13:53:18 -08004644 if (base_is_valid)
4645 off += kvm_register_read(vcpu, base_reg);
4646 if (index_is_valid)
Miaohe Line6302692020-02-15 10:44:22 +08004647 off += kvm_register_read(vcpu, index_reg) << scaling;
Sean Christopherson55d23752018-12-03 13:53:18 -08004648 vmx_get_segment(vcpu, &s, seg_reg);
Sean Christopherson55d23752018-12-03 13:53:18 -08004649
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004650 /*
4651 * The effective address, i.e. @off, of a memory operand is truncated
4652 * based on the address size of the instruction. Note that this is
4653 * the *effective address*, i.e. the address prior to accounting for
4654 * the segment's base.
4655 */
Sean Christopherson55d23752018-12-03 13:53:18 -08004656 if (addr_size == 1) /* 32 bit */
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004657 off &= 0xffffffff;
4658 else if (addr_size == 0) /* 16 bit */
4659 off &= 0xffff;
Sean Christopherson55d23752018-12-03 13:53:18 -08004660
4661 /* Checks for #GP/#SS exceptions. */
4662 exn = false;
4663 if (is_long_mode(vcpu)) {
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004664 /*
4665 * The virtual/linear address is never truncated in 64-bit
4666 * mode, e.g. a 32-bit address size can yield a 64-bit virtual
4667 * address when using FS/GS with a non-zero base.
4668 */
Liran Alon6694e482019-07-15 18:47:44 +03004669 if (seg_reg == VCPU_SREG_FS || seg_reg == VCPU_SREG_GS)
4670 *ret = s.base + off;
4671 else
4672 *ret = off;
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004673
Sean Christopherson55d23752018-12-03 13:53:18 -08004674 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
4675 * non-canonical form. This is the only check on the memory
4676 * destination for long mode!
4677 */
4678 exn = is_noncanonical_address(*ret, vcpu);
Paolo Bonzinie0dfacb2019-01-30 17:25:38 +01004679 } else {
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004680 /*
4681 * When not in long mode, the virtual/linear address is
4682 * unconditionally truncated to 32 bits regardless of the
4683 * address size.
4684 */
4685 *ret = (s.base + off) & 0xffffffff;
4686
Sean Christopherson55d23752018-12-03 13:53:18 -08004687 /* Protected mode: apply checks for segment validity in the
4688 * following order:
4689 * - segment type check (#GP(0) may be thrown)
4690 * - usability check (#GP(0)/#SS(0))
4691 * - limit check (#GP(0)/#SS(0))
4692 */
4693 if (wr)
4694 /* #GP(0) if the destination operand is located in a
4695 * read-only data segment or any code segment.
4696 */
4697 exn = ((s.type & 0xa) == 0 || (s.type & 8));
4698 else
4699 /* #GP(0) if the source operand is located in an
4700 * execute-only code segment
4701 */
4702 exn = ((s.type & 0xa) == 8);
4703 if (exn) {
4704 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
4705 return 1;
4706 }
4707 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
4708 */
4709 exn = (s.unusable != 0);
Sean Christopherson34333cc2019-01-23 14:39:25 -08004710
4711 /*
4712 * Protected mode: #GP(0)/#SS(0) if the memory operand is
4713 * outside the segment limit. All CPUs that support VMX ignore
4714 * limit checks for flat segments, i.e. segments with base==0,
4715 * limit==0xffffffff and of type expand-up data or code.
Sean Christopherson55d23752018-12-03 13:53:18 -08004716 */
Sean Christopherson34333cc2019-01-23 14:39:25 -08004717 if (!(s.base == 0 && s.limit == 0xffffffff &&
4718 ((s.type & 8) || !(s.type & 4))))
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004719 exn = exn || ((u64)off + len - 1 > s.limit);
Sean Christopherson55d23752018-12-03 13:53:18 -08004720 }
4721 if (exn) {
4722 kvm_queue_exception_e(vcpu,
4723 seg_reg == VCPU_SREG_SS ?
4724 SS_VECTOR : GP_VECTOR,
4725 0);
4726 return 1;
4727 }
4728
4729 return 0;
4730}
4731
Oliver Upton03a8871a2019-11-13 16:17:20 -08004732void nested_vmx_pmu_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
4733{
4734 struct vcpu_vmx *vmx;
4735
4736 if (!nested_vmx_allowed(vcpu))
4737 return;
4738
4739 vmx = to_vmx(vcpu);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004740 if (kvm_x86_ops.pmu_ops->is_valid_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL)) {
Oliver Upton03a8871a2019-11-13 16:17:20 -08004741 vmx->nested.msrs.entry_ctls_high |=
4742 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
4743 vmx->nested.msrs.exit_ctls_high |=
4744 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
4745 } else {
4746 vmx->nested.msrs.entry_ctls_high &=
4747 ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
4748 vmx->nested.msrs.exit_ctls_high &=
Chenyi Qiangc6b177a2020-08-28 16:56:21 +08004749 ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
Oliver Upton03a8871a2019-11-13 16:17:20 -08004750 }
4751}
4752
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004753static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer,
4754 int *ret)
Sean Christopherson55d23752018-12-03 13:53:18 -08004755{
4756 gva_t gva;
4757 struct x86_exception e;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004758 int r;
Sean Christopherson55d23752018-12-03 13:53:18 -08004759
Sean Christopherson5addc232020-04-15 13:34:53 -07004760 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004761 vmcs_read32(VMX_INSTRUCTION_INFO), false,
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004762 sizeof(*vmpointer), &gva)) {
4763 *ret = 1;
4764 return -EINVAL;
4765 }
Sean Christopherson55d23752018-12-03 13:53:18 -08004766
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004767 r = kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e);
4768 if (r != X86EMUL_CONTINUE) {
Babu Moger3f3393b2020-09-11 14:29:05 -05004769 *ret = kvm_handle_memory_failure(vcpu, r, &e);
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004770 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08004771 }
4772
4773 return 0;
4774}
4775
4776/*
4777 * Allocate a shadow VMCS and associate it with the currently loaded
4778 * VMCS, unless such a shadow VMCS already exists. The newly allocated
4779 * VMCS is also VMCLEARed, so that it is ready for use.
4780 */
4781static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
4782{
4783 struct vcpu_vmx *vmx = to_vmx(vcpu);
4784 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
4785
4786 /*
4787 * We should allocate a shadow vmcs for vmcs01 only when L1
4788 * executes VMXON and free it when L1 executes VMXOFF.
4789 * As it is invalid to execute VMXON twice, we shouldn't reach
4790 * here when vmcs01 already have an allocated shadow vmcs.
4791 */
4792 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
4793
4794 if (!loaded_vmcs->shadow_vmcs) {
4795 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
4796 if (loaded_vmcs->shadow_vmcs)
4797 vmcs_clear(loaded_vmcs->shadow_vmcs);
4798 }
4799 return loaded_vmcs->shadow_vmcs;
4800}
4801
4802static int enter_vmx_operation(struct kvm_vcpu *vcpu)
4803{
4804 struct vcpu_vmx *vmx = to_vmx(vcpu);
4805 int r;
4806
4807 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
4808 if (r < 0)
4809 goto out_vmcs02;
4810
Ben Gardon41836832019-02-11 11:02:52 -08004811 vmx->nested.cached_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT);
Sean Christopherson55d23752018-12-03 13:53:18 -08004812 if (!vmx->nested.cached_vmcs12)
4813 goto out_cached_vmcs12;
4814
Ben Gardon41836832019-02-11 11:02:52 -08004815 vmx->nested.cached_shadow_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT);
Sean Christopherson55d23752018-12-03 13:53:18 -08004816 if (!vmx->nested.cached_shadow_vmcs12)
4817 goto out_cached_shadow_vmcs12;
4818
4819 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
4820 goto out_shadow_vmcs;
4821
4822 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
Jim Mattsonada00982020-05-08 13:36:42 -07004823 HRTIMER_MODE_ABS_PINNED);
Sean Christopherson55d23752018-12-03 13:53:18 -08004824 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
4825
4826 vmx->nested.vpid02 = allocate_vpid();
4827
4828 vmx->nested.vmcs02_initialized = false;
4829 vmx->nested.vmxon = true;
Luwei Kangee85dec2018-10-24 16:05:16 +08004830
Sean Christopherson2ef76192020-03-02 15:56:22 -08004831 if (vmx_pt_mode_is_host_guest()) {
Luwei Kangee85dec2018-10-24 16:05:16 +08004832 vmx->pt_desc.guest.ctl = 0;
Aaron Lewis476c9bd2020-09-25 16:34:18 +02004833 pt_update_intercept_for_msr(vcpu);
Luwei Kangee85dec2018-10-24 16:05:16 +08004834 }
4835
Sean Christopherson55d23752018-12-03 13:53:18 -08004836 return 0;
4837
4838out_shadow_vmcs:
4839 kfree(vmx->nested.cached_shadow_vmcs12);
4840
4841out_cached_shadow_vmcs12:
4842 kfree(vmx->nested.cached_vmcs12);
4843
4844out_cached_vmcs12:
4845 free_loaded_vmcs(&vmx->nested.vmcs02);
4846
4847out_vmcs02:
4848 return -ENOMEM;
4849}
4850
4851/*
4852 * Emulate the VMXON instruction.
4853 * Currently, we just remember that VMX is active, and do not save or even
4854 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
4855 * do not currently need to store anything in that guest-allocated memory
4856 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
4857 * argument is different from the VMXON pointer (which the spec says they do).
4858 */
4859static int handle_vmon(struct kvm_vcpu *vcpu)
4860{
4861 int ret;
4862 gpa_t vmptr;
KarimAllah Ahmed2e408932019-01-31 21:24:31 +01004863 uint32_t revision;
Sean Christopherson55d23752018-12-03 13:53:18 -08004864 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson32ad73d2019-12-20 20:44:55 -08004865 const u64 VMXON_NEEDED_FEATURES = FEAT_CTL_LOCKED
4866 | FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX;
Sean Christopherson55d23752018-12-03 13:53:18 -08004867
4868 /*
4869 * The Intel VMX Instruction Reference lists a bunch of bits that are
4870 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
Sean Christophersonc2fe3cd2020-10-06 18:44:15 -07004871 * 1 (see vmx_is_valid_cr4() for when we allow the guest to set this).
Sean Christopherson55d23752018-12-03 13:53:18 -08004872 * Otherwise, we should fail with #UD. But most faulting conditions
4873 * have already been checked by hardware, prior to the VM-exit for
4874 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
4875 * that bit set to 1 in non-root mode.
4876 */
4877 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
4878 kvm_queue_exception(vcpu, UD_VECTOR);
4879 return 1;
4880 }
4881
4882 /* CPL=0 must be checked manually. */
4883 if (vmx_get_cpl(vcpu)) {
4884 kvm_inject_gp(vcpu, 0);
4885 return 1;
4886 }
4887
4888 if (vmx->nested.vmxon)
Sean Christophersonb2656e42020-06-08 18:56:07 -07004889 return nested_vmx_fail(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Sean Christopherson55d23752018-12-03 13:53:18 -08004890
4891 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
4892 != VMXON_NEEDED_FEATURES) {
4893 kvm_inject_gp(vcpu, 0);
4894 return 1;
4895 }
4896
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004897 if (nested_vmx_get_vmptr(vcpu, &vmptr, &ret))
4898 return ret;
Sean Christopherson55d23752018-12-03 13:53:18 -08004899
4900 /*
4901 * SDM 3: 24.11.5
4902 * The first 4 bytes of VMXON region contain the supported
4903 * VMCS revision identifier
4904 *
4905 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
4906 * which replaces physical address width with 32
4907 */
KarimAllah Ahmede0bf2662019-01-31 21:24:43 +01004908 if (!page_address_valid(vcpu, vmptr))
Sean Christopherson55d23752018-12-03 13:53:18 -08004909 return nested_vmx_failInvalid(vcpu);
4910
KarimAllah Ahmed2e408932019-01-31 21:24:31 +01004911 if (kvm_read_guest(vcpu->kvm, vmptr, &revision, sizeof(revision)) ||
4912 revision != VMCS12_REVISION)
Sean Christopherson55d23752018-12-03 13:53:18 -08004913 return nested_vmx_failInvalid(vcpu);
4914
Sean Christopherson55d23752018-12-03 13:53:18 -08004915 vmx->nested.vmxon_ptr = vmptr;
4916 ret = enter_vmx_operation(vcpu);
4917 if (ret)
4918 return ret;
4919
4920 return nested_vmx_succeed(vcpu);
4921}
4922
4923static inline void nested_release_vmcs12(struct kvm_vcpu *vcpu)
4924{
4925 struct vcpu_vmx *vmx = to_vmx(vcpu);
4926
4927 if (vmx->nested.current_vmptr == -1ull)
4928 return;
4929
Sean Christopherson7952d762019-05-07 08:36:29 -07004930 copy_vmcs02_to_vmcs12_rare(vcpu, get_vmcs12(vcpu));
4931
Sean Christopherson55d23752018-12-03 13:53:18 -08004932 if (enable_shadow_vmcs) {
4933 /* copy to memory all shadowed fields in case
4934 they were modified */
4935 copy_shadow_to_vmcs12(vmx);
Sean Christopherson55d23752018-12-03 13:53:18 -08004936 vmx_disable_shadow_vmcs(vmx);
4937 }
4938 vmx->nested.posted_intr_nv = -1;
4939
4940 /* Flush VMCS12 to guest memory */
4941 kvm_vcpu_write_guest_page(vcpu,
4942 vmx->nested.current_vmptr >> PAGE_SHIFT,
4943 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
4944
4945 kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
4946
4947 vmx->nested.current_vmptr = -1ull;
4948}
4949
4950/* Emulate the VMXOFF instruction */
4951static int handle_vmoff(struct kvm_vcpu *vcpu)
4952{
4953 if (!nested_vmx_check_permission(vcpu))
4954 return 1;
Liran Alon4b9852f2019-08-26 13:24:49 +03004955
Sean Christopherson55d23752018-12-03 13:53:18 -08004956 free_nested(vcpu);
Liran Alon4b9852f2019-08-26 13:24:49 +03004957
4958 /* Process a latched INIT during time CPU was in VMX operation */
4959 kvm_make_request(KVM_REQ_EVENT, vcpu);
4960
Sean Christopherson55d23752018-12-03 13:53:18 -08004961 return nested_vmx_succeed(vcpu);
4962}
4963
4964/* Emulate the VMCLEAR instruction */
4965static int handle_vmclear(struct kvm_vcpu *vcpu)
4966{
4967 struct vcpu_vmx *vmx = to_vmx(vcpu);
4968 u32 zero = 0;
4969 gpa_t vmptr;
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02004970 u64 evmcs_gpa;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004971 int r;
Sean Christopherson55d23752018-12-03 13:53:18 -08004972
4973 if (!nested_vmx_check_permission(vcpu))
4974 return 1;
4975
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004976 if (nested_vmx_get_vmptr(vcpu, &vmptr, &r))
4977 return r;
Sean Christopherson55d23752018-12-03 13:53:18 -08004978
KarimAllah Ahmede0bf2662019-01-31 21:24:43 +01004979 if (!page_address_valid(vcpu, vmptr))
Sean Christophersonb2656e42020-06-08 18:56:07 -07004980 return nested_vmx_fail(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
Sean Christopherson55d23752018-12-03 13:53:18 -08004981
4982 if (vmptr == vmx->nested.vmxon_ptr)
Sean Christophersonb2656e42020-06-08 18:56:07 -07004983 return nested_vmx_fail(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
Sean Christopherson55d23752018-12-03 13:53:18 -08004984
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02004985 /*
4986 * When Enlightened VMEntry is enabled on the calling CPU we treat
4987 * memory area pointer by vmptr as Enlightened VMCS (as there's no good
4988 * way to distinguish it from VMCS12) and we must not corrupt it by
4989 * writing to the non-existent 'launch_state' field. The area doesn't
4990 * have to be the currently active EVMCS on the calling CPU and there's
4991 * nothing KVM has to do to transition it from 'active' to 'non-active'
4992 * state. It is possible that the area will stay mapped as
4993 * vmx->nested.hv_evmcs but this shouldn't be a problem.
4994 */
4995 if (likely(!vmx->nested.enlightened_vmcs_enabled ||
4996 !nested_enlightened_vmentry(vcpu, &evmcs_gpa))) {
Sean Christopherson55d23752018-12-03 13:53:18 -08004997 if (vmptr == vmx->nested.current_vmptr)
4998 nested_release_vmcs12(vcpu);
4999
5000 kvm_vcpu_write_guest(vcpu,
5001 vmptr + offsetof(struct vmcs12,
5002 launch_state),
5003 &zero, sizeof(zero));
5004 }
5005
5006 return nested_vmx_succeed(vcpu);
5007}
5008
Sean Christopherson55d23752018-12-03 13:53:18 -08005009/* Emulate the VMLAUNCH instruction */
5010static int handle_vmlaunch(struct kvm_vcpu *vcpu)
5011{
5012 return nested_vmx_run(vcpu, true);
5013}
5014
5015/* Emulate the VMRESUME instruction */
5016static int handle_vmresume(struct kvm_vcpu *vcpu)
5017{
5018
5019 return nested_vmx_run(vcpu, false);
5020}
5021
5022static int handle_vmread(struct kvm_vcpu *vcpu)
5023{
Jim Mattsondd2d6042019-12-06 15:46:35 -08005024 struct vmcs12 *vmcs12 = is_guest_mode(vcpu) ? get_shadow_vmcs12(vcpu)
5025 : get_vmcs12(vcpu);
Sean Christopherson5addc232020-04-15 13:34:53 -07005026 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005027 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5028 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzinif7eea632019-09-14 00:26:27 +02005029 struct x86_exception e;
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005030 unsigned long field;
5031 u64 value;
5032 gva_t gva = 0;
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07005033 short offset;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005034 int len, r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005035
5036 if (!nested_vmx_check_permission(vcpu))
5037 return 1;
5038
Jim Mattsondd2d6042019-12-06 15:46:35 -08005039 /*
5040 * In VMX non-root operation, when the VMCS-link pointer is -1ull,
5041 * any VMREAD sets the ALU flags for VMfailInvalid.
5042 */
5043 if (vmx->nested.current_vmptr == -1ull ||
5044 (is_guest_mode(vcpu) &&
5045 get_vmcs12(vcpu)->vmcs_link_pointer == -1ull))
Sean Christopherson55d23752018-12-03 13:53:18 -08005046 return nested_vmx_failInvalid(vcpu);
5047
Sean Christopherson55d23752018-12-03 13:53:18 -08005048 /* Decode instruction info and find the field to read */
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005049 field = kvm_register_read(vcpu, (((instr_info) >> 28) & 0xf));
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07005050
5051 offset = vmcs_field_to_offset(field);
5052 if (offset < 0)
Sean Christophersonb2656e42020-06-08 18:56:07 -07005053 return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Sean Christopherson55d23752018-12-03 13:53:18 -08005054
Sean Christopherson7952d762019-05-07 08:36:29 -07005055 if (!is_guest_mode(vcpu) && is_vmcs12_ext_field(field))
5056 copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
5057
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005058 /* Read the field, zero-extended to a u64 value */
5059 value = vmcs12_read_any(vmcs12, field, offset);
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07005060
Sean Christopherson55d23752018-12-03 13:53:18 -08005061 /*
5062 * Now copy part of this value to register or memory, as requested.
5063 * Note that the number of bits actually copied is 32 or 64 depending
5064 * on the guest's mode (32 or 64 bit), not on the given field's length.
5065 */
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005066 if (instr_info & BIT(10)) {
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005067 kvm_register_write(vcpu, (((instr_info) >> 3) & 0xf), value);
Sean Christopherson55d23752018-12-03 13:53:18 -08005068 } else {
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005069 len = is_64_bit_mode(vcpu) ? 8 : 4;
Sean Christopherson55d23752018-12-03 13:53:18 -08005070 if (get_vmx_mem_address(vcpu, exit_qualification,
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005071 instr_info, true, len, &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08005072 return 1;
5073 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005074 r = kvm_write_guest_virt_system(vcpu, gva, &value, len, &e);
5075 if (r != X86EMUL_CONTINUE)
Babu Moger3f3393b2020-09-11 14:29:05 -05005076 return kvm_handle_memory_failure(vcpu, r, &e);
Sean Christopherson55d23752018-12-03 13:53:18 -08005077 }
5078
5079 return nested_vmx_succeed(vcpu);
5080}
5081
Sean Christophersone2174292019-05-07 08:36:28 -07005082static bool is_shadow_field_rw(unsigned long field)
5083{
5084 switch (field) {
5085#define SHADOW_FIELD_RW(x, y) case x:
5086#include "vmcs_shadow_fields.h"
5087 return true;
5088 default:
5089 break;
5090 }
5091 return false;
5092}
5093
5094static bool is_shadow_field_ro(unsigned long field)
5095{
5096 switch (field) {
5097#define SHADOW_FIELD_RO(x, y) case x:
5098#include "vmcs_shadow_fields.h"
5099 return true;
5100 default:
5101 break;
5102 }
5103 return false;
5104}
Sean Christopherson55d23752018-12-03 13:53:18 -08005105
5106static int handle_vmwrite(struct kvm_vcpu *vcpu)
5107{
Jim Mattsondd2d6042019-12-06 15:46:35 -08005108 struct vmcs12 *vmcs12 = is_guest_mode(vcpu) ? get_shadow_vmcs12(vcpu)
5109 : get_vmcs12(vcpu);
Sean Christopherson5addc232020-04-15 13:34:53 -07005110 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005111 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5112 struct vcpu_vmx *vmx = to_vmx(vcpu);
5113 struct x86_exception e;
5114 unsigned long field;
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07005115 short offset;
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005116 gva_t gva;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005117 int len, r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005118
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005119 /*
5120 * The value to write might be 32 or 64 bits, depending on L1's long
Sean Christopherson55d23752018-12-03 13:53:18 -08005121 * mode, and eventually we need to write that into a field of several
5122 * possible lengths. The code below first zero-extends the value to 64
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005123 * bit (value), and then copies only the appropriate number of
Sean Christopherson55d23752018-12-03 13:53:18 -08005124 * bits into the vmcs12 field.
5125 */
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005126 u64 value = 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08005127
5128 if (!nested_vmx_check_permission(vcpu))
5129 return 1;
5130
Jim Mattsondd2d6042019-12-06 15:46:35 -08005131 /*
5132 * In VMX non-root operation, when the VMCS-link pointer is -1ull,
5133 * any VMWRITE sets the ALU flags for VMfailInvalid.
5134 */
5135 if (vmx->nested.current_vmptr == -1ull ||
5136 (is_guest_mode(vcpu) &&
5137 get_vmcs12(vcpu)->vmcs_link_pointer == -1ull))
Sean Christopherson55d23752018-12-03 13:53:18 -08005138 return nested_vmx_failInvalid(vcpu);
5139
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005140 if (instr_info & BIT(10))
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005141 value = kvm_register_read(vcpu, (((instr_info) >> 3) & 0xf));
Sean Christopherson55d23752018-12-03 13:53:18 -08005142 else {
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005143 len = is_64_bit_mode(vcpu) ? 8 : 4;
Sean Christopherson55d23752018-12-03 13:53:18 -08005144 if (get_vmx_mem_address(vcpu, exit_qualification,
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005145 instr_info, false, len, &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08005146 return 1;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005147 r = kvm_read_guest_virt(vcpu, gva, &value, len, &e);
5148 if (r != X86EMUL_CONTINUE)
Babu Moger3f3393b2020-09-11 14:29:05 -05005149 return kvm_handle_memory_failure(vcpu, r, &e);
Sean Christopherson55d23752018-12-03 13:53:18 -08005150 }
5151
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005152 field = kvm_register_read(vcpu, (((instr_info) >> 28) & 0xf));
Sean Christopherson55d23752018-12-03 13:53:18 -08005153
Jim Mattson693e02c2019-12-06 15:46:36 -08005154 offset = vmcs_field_to_offset(field);
5155 if (offset < 0)
Sean Christophersonb2656e42020-06-08 18:56:07 -07005156 return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Jim Mattson693e02c2019-12-06 15:46:36 -08005157
Sean Christopherson55d23752018-12-03 13:53:18 -08005158 /*
5159 * If the vCPU supports "VMWRITE to any supported field in the
5160 * VMCS," then the "read-only" fields are actually read/write.
5161 */
5162 if (vmcs_field_readonly(field) &&
5163 !nested_cpu_has_vmwrite_any_field(vcpu))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005164 return nested_vmx_fail(vcpu, VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Sean Christopherson55d23752018-12-03 13:53:18 -08005165
Jim Mattsondd2d6042019-12-06 15:46:35 -08005166 /*
5167 * Ensure vmcs12 is up-to-date before any VMWRITE that dirties
5168 * vmcs12, else we may crush a field or consume a stale value.
5169 */
5170 if (!is_guest_mode(vcpu) && !is_shadow_field_rw(field))
5171 copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08005172
5173 /*
Sean Christophersonb6437802019-05-07 08:36:24 -07005174 * Some Intel CPUs intentionally drop the reserved bits of the AR byte
5175 * fields on VMWRITE. Emulate this behavior to ensure consistent KVM
5176 * behavior regardless of the underlying hardware, e.g. if an AR_BYTE
5177 * field is intercepted for VMWRITE but not VMREAD (in L1), then VMREAD
5178 * from L1 will return a different value than VMREAD from L2 (L1 sees
5179 * the stripped down value, L2 sees the full value as stored by KVM).
Sean Christopherson55d23752018-12-03 13:53:18 -08005180 */
Sean Christophersonb6437802019-05-07 08:36:24 -07005181 if (field >= GUEST_ES_AR_BYTES && field <= GUEST_TR_AR_BYTES)
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005182 value &= 0x1f0ff;
Sean Christophersonb6437802019-05-07 08:36:24 -07005183
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005184 vmcs12_write_any(vmcs12, field, offset, value);
Sean Christopherson55d23752018-12-03 13:53:18 -08005185
5186 /*
Sean Christophersone2174292019-05-07 08:36:28 -07005187 * Do not track vmcs12 dirty-state if in guest-mode as we actually
5188 * dirty shadow vmcs12 instead of vmcs12. Fields that can be updated
5189 * by L1 without a vmexit are always updated in the vmcs02, i.e. don't
5190 * "dirty" vmcs12, all others go down the prepare_vmcs02() slow path.
Sean Christopherson55d23752018-12-03 13:53:18 -08005191 */
Sean Christophersone2174292019-05-07 08:36:28 -07005192 if (!is_guest_mode(vcpu) && !is_shadow_field_rw(field)) {
5193 /*
5194 * L1 can read these fields without exiting, ensure the
5195 * shadow VMCS is up-to-date.
5196 */
5197 if (enable_shadow_vmcs && is_shadow_field_ro(field)) {
5198 preempt_disable();
5199 vmcs_load(vmx->vmcs01.shadow_vmcs);
Sean Christophersonfadcead2019-05-07 08:36:23 -07005200
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005201 __vmcs_writel(field, value);
Sean Christophersonfadcead2019-05-07 08:36:23 -07005202
Sean Christophersone2174292019-05-07 08:36:28 -07005203 vmcs_clear(vmx->vmcs01.shadow_vmcs);
5204 vmcs_load(vmx->loaded_vmcs->vmcs);
5205 preempt_enable();
Sean Christopherson55d23752018-12-03 13:53:18 -08005206 }
Sean Christophersone2174292019-05-07 08:36:28 -07005207 vmx->nested.dirty_vmcs12 = true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005208 }
5209
5210 return nested_vmx_succeed(vcpu);
5211}
5212
5213static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
5214{
5215 vmx->nested.current_vmptr = vmptr;
5216 if (enable_shadow_vmcs) {
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07005217 secondary_exec_controls_setbit(vmx, SECONDARY_EXEC_SHADOW_VMCS);
Sean Christopherson55d23752018-12-03 13:53:18 -08005218 vmcs_write64(VMCS_LINK_POINTER,
5219 __pa(vmx->vmcs01.shadow_vmcs));
Sean Christopherson3731905ef2019-05-07 08:36:27 -07005220 vmx->nested.need_vmcs12_to_shadow_sync = true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005221 }
5222 vmx->nested.dirty_vmcs12 = true;
5223}
5224
5225/* Emulate the VMPTRLD instruction */
5226static int handle_vmptrld(struct kvm_vcpu *vcpu)
5227{
5228 struct vcpu_vmx *vmx = to_vmx(vcpu);
5229 gpa_t vmptr;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005230 int r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005231
5232 if (!nested_vmx_check_permission(vcpu))
5233 return 1;
5234
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005235 if (nested_vmx_get_vmptr(vcpu, &vmptr, &r))
5236 return r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005237
KarimAllah Ahmede0bf2662019-01-31 21:24:43 +01005238 if (!page_address_valid(vcpu, vmptr))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005239 return nested_vmx_fail(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
Sean Christopherson55d23752018-12-03 13:53:18 -08005240
5241 if (vmptr == vmx->nested.vmxon_ptr)
Sean Christophersonb2656e42020-06-08 18:56:07 -07005242 return nested_vmx_fail(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
Sean Christopherson55d23752018-12-03 13:53:18 -08005243
5244 /* Forbid normal VMPTRLD if Enlightened version was used */
5245 if (vmx->nested.hv_evmcs)
5246 return 1;
5247
5248 if (vmx->nested.current_vmptr != vmptr) {
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01005249 struct kvm_host_map map;
Sean Christopherson55d23752018-12-03 13:53:18 -08005250 struct vmcs12 *new_vmcs12;
Sean Christopherson55d23752018-12-03 13:53:18 -08005251
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01005252 if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmptr), &map)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08005253 /*
5254 * Reads from an unbacked page return all 1s,
5255 * which means that the 32 bits located at the
5256 * given physical address won't match the required
5257 * VMCS12_REVISION identifier.
5258 */
Sean Christophersonb2656e42020-06-08 18:56:07 -07005259 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005260 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Sean Christopherson55d23752018-12-03 13:53:18 -08005261 }
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01005262
5263 new_vmcs12 = map.hva;
5264
Sean Christopherson55d23752018-12-03 13:53:18 -08005265 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
5266 (new_vmcs12->hdr.shadow_vmcs &&
5267 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01005268 kvm_vcpu_unmap(vcpu, &map, false);
Sean Christophersonb2656e42020-06-08 18:56:07 -07005269 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005270 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
5271 }
5272
5273 nested_release_vmcs12(vcpu);
5274
5275 /*
5276 * Load VMCS12 from guest memory since it is not already
5277 * cached.
5278 */
5279 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01005280 kvm_vcpu_unmap(vcpu, &map, false);
Sean Christopherson55d23752018-12-03 13:53:18 -08005281
5282 set_current_vmptr(vmx, vmptr);
5283 }
5284
5285 return nested_vmx_succeed(vcpu);
5286}
5287
5288/* Emulate the VMPTRST instruction */
5289static int handle_vmptrst(struct kvm_vcpu *vcpu)
5290{
Sean Christopherson5addc232020-04-15 13:34:53 -07005291 unsigned long exit_qual = vmx_get_exit_qual(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005292 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5293 gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr;
5294 struct x86_exception e;
5295 gva_t gva;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005296 int r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005297
5298 if (!nested_vmx_check_permission(vcpu))
5299 return 1;
5300
5301 if (unlikely(to_vmx(vcpu)->nested.hv_evmcs))
5302 return 1;
5303
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005304 if (get_vmx_mem_address(vcpu, exit_qual, instr_info,
5305 true, sizeof(gpa_t), &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08005306 return 1;
5307 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005308 r = kvm_write_guest_virt_system(vcpu, gva, (void *)&current_vmptr,
5309 sizeof(gpa_t), &e);
5310 if (r != X86EMUL_CONTINUE)
Babu Moger3f3393b2020-09-11 14:29:05 -05005311 return kvm_handle_memory_failure(vcpu, r, &e);
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005312
Sean Christopherson55d23752018-12-03 13:53:18 -08005313 return nested_vmx_succeed(vcpu);
5314}
5315
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005316#define EPTP_PA_MASK GENMASK_ULL(51, 12)
5317
5318static bool nested_ept_root_matches(hpa_t root_hpa, u64 root_eptp, u64 eptp)
5319{
5320 return VALID_PAGE(root_hpa) &&
5321 ((root_eptp & EPTP_PA_MASK) == (eptp & EPTP_PA_MASK));
5322}
5323
Sean Christopherson55d23752018-12-03 13:53:18 -08005324/* Emulate the INVEPT instruction */
5325static int handle_invept(struct kvm_vcpu *vcpu)
5326{
5327 struct vcpu_vmx *vmx = to_vmx(vcpu);
5328 u32 vmx_instruction_info, types;
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005329 unsigned long type, roots_to_free;
5330 struct kvm_mmu *mmu;
Sean Christopherson55d23752018-12-03 13:53:18 -08005331 gva_t gva;
5332 struct x86_exception e;
5333 struct {
5334 u64 eptp, gpa;
5335 } operand;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005336 int i, r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005337
5338 if (!(vmx->nested.msrs.secondary_ctls_high &
5339 SECONDARY_EXEC_ENABLE_EPT) ||
5340 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
5341 kvm_queue_exception(vcpu, UD_VECTOR);
5342 return 1;
5343 }
5344
5345 if (!nested_vmx_check_permission(vcpu))
5346 return 1;
5347
5348 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005349 type = kvm_register_read(vcpu, (vmx_instruction_info >> 28) & 0xf);
Sean Christopherson55d23752018-12-03 13:53:18 -08005350
5351 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
5352
5353 if (type >= 32 || !(types & (1 << type)))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005354 return nested_vmx_fail(vcpu, VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Sean Christopherson55d23752018-12-03 13:53:18 -08005355
5356 /* According to the Intel VMX instruction reference, the memory
5357 * operand is read even if it isn't needed (e.g., for type==global)
5358 */
Sean Christopherson5addc232020-04-15 13:34:53 -07005359 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005360 vmx_instruction_info, false, sizeof(operand), &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08005361 return 1;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005362 r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e);
5363 if (r != X86EMUL_CONTINUE)
Babu Moger3f3393b2020-09-11 14:29:05 -05005364 return kvm_handle_memory_failure(vcpu, r, &e);
Sean Christopherson55d23752018-12-03 13:53:18 -08005365
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005366 /*
5367 * Nested EPT roots are always held through guest_mmu,
5368 * not root_mmu.
5369 */
5370 mmu = &vcpu->arch.guest_mmu;
5371
Sean Christopherson55d23752018-12-03 13:53:18 -08005372 switch (type) {
Sean Christopherson55d23752018-12-03 13:53:18 -08005373 case VMX_EPT_EXTENT_CONTEXT:
Sean Christophersoneed00302020-03-20 14:27:58 -07005374 if (!nested_vmx_check_eptp(vcpu, operand.eptp))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005375 return nested_vmx_fail(vcpu,
Sean Christophersoneed00302020-03-20 14:27:58 -07005376 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Sean Christophersonf8aa7e32020-03-20 14:27:59 -07005377
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005378 roots_to_free = 0;
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07005379 if (nested_ept_root_matches(mmu->root_hpa, mmu->root_pgd,
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005380 operand.eptp))
5381 roots_to_free |= KVM_MMU_ROOT_CURRENT;
5382
5383 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) {
5384 if (nested_ept_root_matches(mmu->prev_roots[i].hpa,
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07005385 mmu->prev_roots[i].pgd,
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005386 operand.eptp))
5387 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
5388 }
5389 break;
Sean Christophersoneed00302020-03-20 14:27:58 -07005390 case VMX_EPT_EXTENT_GLOBAL:
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005391 roots_to_free = KVM_MMU_ROOTS_ALL;
Sean Christopherson55d23752018-12-03 13:53:18 -08005392 break;
5393 default:
Sean Christophersonf9336e32020-05-04 08:35:06 -07005394 BUG();
Sean Christopherson55d23752018-12-03 13:53:18 -08005395 break;
5396 }
5397
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005398 if (roots_to_free)
5399 kvm_mmu_free_roots(vcpu, mmu, roots_to_free);
5400
Sean Christopherson55d23752018-12-03 13:53:18 -08005401 return nested_vmx_succeed(vcpu);
5402}
5403
5404static int handle_invvpid(struct kvm_vcpu *vcpu)
5405{
5406 struct vcpu_vmx *vmx = to_vmx(vcpu);
5407 u32 vmx_instruction_info;
5408 unsigned long type, types;
5409 gva_t gva;
5410 struct x86_exception e;
5411 struct {
5412 u64 vpid;
5413 u64 gla;
5414 } operand;
5415 u16 vpid02;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005416 int r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005417
5418 if (!(vmx->nested.msrs.secondary_ctls_high &
5419 SECONDARY_EXEC_ENABLE_VPID) ||
5420 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
5421 kvm_queue_exception(vcpu, UD_VECTOR);
5422 return 1;
5423 }
5424
5425 if (!nested_vmx_check_permission(vcpu))
5426 return 1;
5427
5428 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005429 type = kvm_register_read(vcpu, (vmx_instruction_info >> 28) & 0xf);
Sean Christopherson55d23752018-12-03 13:53:18 -08005430
5431 types = (vmx->nested.msrs.vpid_caps &
5432 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
5433
5434 if (type >= 32 || !(types & (1 << type)))
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);
5437
5438 /* according to the intel vmx instruction reference, the memory
5439 * operand is read even if it isn't needed (e.g., for type==global)
5440 */
Sean Christopherson5addc232020-04-15 13:34:53 -07005441 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005442 vmx_instruction_info, false, sizeof(operand), &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08005443 return 1;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005444 r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e);
5445 if (r != X86EMUL_CONTINUE)
Babu Moger3f3393b2020-09-11 14:29:05 -05005446 return kvm_handle_memory_failure(vcpu, r, &e);
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005447
Sean Christopherson55d23752018-12-03 13:53:18 -08005448 if (operand.vpid >> 16)
Sean Christophersonb2656e42020-06-08 18:56:07 -07005449 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005450 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
5451
5452 vpid02 = nested_get_vpid02(vcpu);
5453 switch (type) {
5454 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
5455 if (!operand.vpid ||
5456 is_noncanonical_address(operand.gla, vcpu))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005457 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005458 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Sean Christophersonbc41d0c2020-03-20 14:28:09 -07005459 vpid_sync_vcpu_addr(vpid02, operand.gla);
Sean Christopherson55d23752018-12-03 13:53:18 -08005460 break;
5461 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
5462 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
5463 if (!operand.vpid)
Sean Christophersonb2656e42020-06-08 18:56:07 -07005464 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005465 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Sean Christopherson446ace42020-03-20 14:28:05 -07005466 vpid_sync_context(vpid02);
Sean Christopherson55d23752018-12-03 13:53:18 -08005467 break;
5468 case VMX_VPID_EXTENT_ALL_CONTEXT:
Sean Christopherson446ace42020-03-20 14:28:05 -07005469 vpid_sync_context(vpid02);
Sean Christopherson55d23752018-12-03 13:53:18 -08005470 break;
5471 default:
5472 WARN_ON_ONCE(1);
5473 return kvm_skip_emulated_instruction(vcpu);
5474 }
5475
Junaid Shahidd6e3f832020-03-20 14:28:00 -07005476 /*
5477 * Sync the shadow page tables if EPT is disabled, L1 is invalidating
5478 * linear mappings for L2 (tagged with L2's VPID). Free all roots as
5479 * VPIDs are not tracked in the MMU role.
5480 *
5481 * Note, this operates on root_mmu, not guest_mmu, as L1 and L2 share
5482 * an MMU when EPT is disabled.
5483 *
5484 * TODO: sync only the affected SPTEs for INVDIVIDUAL_ADDR.
5485 */
5486 if (!enable_ept)
5487 kvm_mmu_free_roots(vcpu, &vcpu->arch.root_mmu,
5488 KVM_MMU_ROOTS_ALL);
5489
Sean Christopherson55d23752018-12-03 13:53:18 -08005490 return nested_vmx_succeed(vcpu);
5491}
5492
5493static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
5494 struct vmcs12 *vmcs12)
5495{
Sean Christopherson2b3eaf82019-04-30 10:36:19 -07005496 u32 index = kvm_rcx_read(vcpu);
Sean Christophersonac6389a2020-03-02 18:02:38 -08005497 u64 new_eptp;
Sean Christopherson55d23752018-12-03 13:53:18 -08005498 bool accessed_dirty;
5499 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5500
5501 if (!nested_cpu_has_eptp_switching(vmcs12) ||
5502 !nested_cpu_has_ept(vmcs12))
5503 return 1;
5504
5505 if (index >= VMFUNC_EPTP_ENTRIES)
5506 return 1;
5507
5508
5509 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
Sean Christophersonac6389a2020-03-02 18:02:38 -08005510 &new_eptp, index * 8, 8))
Sean Christopherson55d23752018-12-03 13:53:18 -08005511 return 1;
5512
Sean Christophersonac6389a2020-03-02 18:02:38 -08005513 accessed_dirty = !!(new_eptp & VMX_EPTP_AD_ENABLE_BIT);
Sean Christopherson55d23752018-12-03 13:53:18 -08005514
5515 /*
5516 * If the (L2) guest does a vmfunc to the currently
5517 * active ept pointer, we don't have to do anything else
5518 */
Sean Christophersonac6389a2020-03-02 18:02:38 -08005519 if (vmcs12->ept_pointer != new_eptp) {
5520 if (!nested_vmx_check_eptp(vcpu, new_eptp))
Sean Christopherson55d23752018-12-03 13:53:18 -08005521 return 1;
5522
Sean Christopherson55d23752018-12-03 13:53:18 -08005523 mmu->ept_ad = accessed_dirty;
5524 mmu->mmu_role.base.ad_disabled = !accessed_dirty;
Sean Christophersonac6389a2020-03-02 18:02:38 -08005525 vmcs12->ept_pointer = new_eptp;
Sean Christophersonc805f5d2021-03-04 17:10:57 -08005526
5527 kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005528 }
5529
5530 return 0;
5531}
5532
5533static int handle_vmfunc(struct kvm_vcpu *vcpu)
5534{
5535 struct vcpu_vmx *vmx = to_vmx(vcpu);
5536 struct vmcs12 *vmcs12;
Sean Christopherson2b3eaf82019-04-30 10:36:19 -07005537 u32 function = kvm_rax_read(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005538
5539 /*
5540 * VMFUNC is only supported for nested guests, but we always enable the
5541 * secondary control for simplicity; for non-nested mode, fake that we
5542 * didn't by injecting #UD.
5543 */
5544 if (!is_guest_mode(vcpu)) {
5545 kvm_queue_exception(vcpu, UD_VECTOR);
5546 return 1;
5547 }
5548
5549 vmcs12 = get_vmcs12(vcpu);
5550 if ((vmcs12->vm_function_control & (1 << function)) == 0)
5551 goto fail;
5552
5553 switch (function) {
5554 case 0:
5555 if (nested_vmx_eptp_switching(vcpu, vmcs12))
5556 goto fail;
5557 break;
5558 default:
5559 goto fail;
5560 }
5561 return kvm_skip_emulated_instruction(vcpu);
5562
5563fail:
Sean Christopherson8e533242020-11-06 17:03:12 +08005564 /*
5565 * This is effectively a reflected VM-Exit, as opposed to a synthesized
5566 * nested VM-Exit. Pass the original exit reason, i.e. don't hardcode
5567 * EXIT_REASON_VMFUNC as the exit reason.
5568 */
5569 nested_vmx_vmexit(vcpu, vmx->exit_reason.full,
Sean Christopherson87915852020-04-15 13:34:54 -07005570 vmx_get_intr_info(vcpu),
Sean Christopherson5addc232020-04-15 13:34:53 -07005571 vmx_get_exit_qual(vcpu));
Sean Christopherson55d23752018-12-03 13:53:18 -08005572 return 1;
5573}
5574
Oliver Uptone71237d2020-02-04 15:26:30 -08005575/*
5576 * Return true if an IO instruction with the specified port and size should cause
5577 * a VM-exit into L1.
5578 */
5579bool nested_vmx_check_io_bitmaps(struct kvm_vcpu *vcpu, unsigned int port,
5580 int size)
Sean Christopherson55d23752018-12-03 13:53:18 -08005581{
Oliver Uptone71237d2020-02-04 15:26:30 -08005582 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005583 gpa_t bitmap, last_bitmap;
Sean Christopherson55d23752018-12-03 13:53:18 -08005584 u8 b;
5585
Sean Christopherson55d23752018-12-03 13:53:18 -08005586 last_bitmap = (gpa_t)-1;
5587 b = -1;
5588
5589 while (size > 0) {
5590 if (port < 0x8000)
5591 bitmap = vmcs12->io_bitmap_a;
5592 else if (port < 0x10000)
5593 bitmap = vmcs12->io_bitmap_b;
5594 else
5595 return true;
5596 bitmap += (port & 0x7fff) / 8;
5597
5598 if (last_bitmap != bitmap)
5599 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
5600 return true;
5601 if (b & (1 << (port & 7)))
5602 return true;
5603
5604 port++;
5605 size--;
5606 last_bitmap = bitmap;
5607 }
5608
5609 return false;
5610}
5611
Oliver Uptone71237d2020-02-04 15:26:30 -08005612static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
5613 struct vmcs12 *vmcs12)
5614{
5615 unsigned long exit_qualification;
Oliver Upton35a57132020-02-04 15:26:31 -08005616 unsigned short port;
Oliver Uptone71237d2020-02-04 15:26:30 -08005617 int size;
5618
5619 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
5620 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
5621
Sean Christopherson5addc232020-04-15 13:34:53 -07005622 exit_qualification = vmx_get_exit_qual(vcpu);
Oliver Uptone71237d2020-02-04 15:26:30 -08005623
5624 port = exit_qualification >> 16;
5625 size = (exit_qualification & 7) + 1;
5626
5627 return nested_vmx_check_io_bitmaps(vcpu, port, size);
5628}
5629
Sean Christopherson55d23752018-12-03 13:53:18 -08005630/*
Miaohe Lin463bfee2020-02-14 10:44:05 +08005631 * Return 1 if we should exit from L2 to L1 to handle an MSR access,
Sean Christopherson55d23752018-12-03 13:53:18 -08005632 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
5633 * disinterest in the current event (read or write a specific MSR) by using an
5634 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
5635 */
5636static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
Sean Christopherson8e533242020-11-06 17:03:12 +08005637 struct vmcs12 *vmcs12,
5638 union vmx_exit_reason exit_reason)
Sean Christopherson55d23752018-12-03 13:53:18 -08005639{
Sean Christopherson2b3eaf82019-04-30 10:36:19 -07005640 u32 msr_index = kvm_rcx_read(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005641 gpa_t bitmap;
5642
5643 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
5644 return true;
5645
5646 /*
5647 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
5648 * for the four combinations of read/write and low/high MSR numbers.
5649 * First we need to figure out which of the four to use:
5650 */
5651 bitmap = vmcs12->msr_bitmap;
Sean Christopherson8e533242020-11-06 17:03:12 +08005652 if (exit_reason.basic == EXIT_REASON_MSR_WRITE)
Sean Christopherson55d23752018-12-03 13:53:18 -08005653 bitmap += 2048;
5654 if (msr_index >= 0xc0000000) {
5655 msr_index -= 0xc0000000;
5656 bitmap += 1024;
5657 }
5658
5659 /* Then read the msr_index'th bit from this bitmap: */
5660 if (msr_index < 1024*8) {
5661 unsigned char b;
5662 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
5663 return true;
5664 return 1 & (b >> (msr_index & 7));
5665 } else
5666 return true; /* let L1 handle the wrong parameter */
5667}
5668
5669/*
5670 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
5671 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
5672 * intercept (via guest_host_mask etc.) the current event.
5673 */
5674static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
5675 struct vmcs12 *vmcs12)
5676{
Sean Christopherson5addc232020-04-15 13:34:53 -07005677 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005678 int cr = exit_qualification & 15;
5679 int reg;
5680 unsigned long val;
5681
5682 switch ((exit_qualification >> 4) & 3) {
5683 case 0: /* mov to cr */
5684 reg = (exit_qualification >> 8) & 15;
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005685 val = kvm_register_read(vcpu, reg);
Sean Christopherson55d23752018-12-03 13:53:18 -08005686 switch (cr) {
5687 case 0:
5688 if (vmcs12->cr0_guest_host_mask &
5689 (val ^ vmcs12->cr0_read_shadow))
5690 return true;
5691 break;
5692 case 3:
Sean Christopherson55d23752018-12-03 13:53:18 -08005693 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
5694 return true;
5695 break;
5696 case 4:
5697 if (vmcs12->cr4_guest_host_mask &
5698 (vmcs12->cr4_read_shadow ^ val))
5699 return true;
5700 break;
5701 case 8:
5702 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
5703 return true;
5704 break;
5705 }
5706 break;
5707 case 2: /* clts */
5708 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
5709 (vmcs12->cr0_read_shadow & X86_CR0_TS))
5710 return true;
5711 break;
5712 case 1: /* mov from cr */
5713 switch (cr) {
5714 case 3:
5715 if (vmcs12->cpu_based_vm_exec_control &
5716 CPU_BASED_CR3_STORE_EXITING)
5717 return true;
5718 break;
5719 case 8:
5720 if (vmcs12->cpu_based_vm_exec_control &
5721 CPU_BASED_CR8_STORE_EXITING)
5722 return true;
5723 break;
5724 }
5725 break;
5726 case 3: /* lmsw */
5727 /*
5728 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
5729 * cr0. Other attempted changes are ignored, with no exit.
5730 */
5731 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
5732 if (vmcs12->cr0_guest_host_mask & 0xe &
5733 (val ^ vmcs12->cr0_read_shadow))
5734 return true;
5735 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
5736 !(vmcs12->cr0_read_shadow & 0x1) &&
5737 (val & 0x1))
5738 return true;
5739 break;
5740 }
5741 return false;
5742}
5743
Sean Christopherson72add912021-04-12 16:21:42 +12005744static bool nested_vmx_exit_handled_encls(struct kvm_vcpu *vcpu,
5745 struct vmcs12 *vmcs12)
5746{
5747 u32 encls_leaf;
5748
5749 if (!guest_cpuid_has(vcpu, X86_FEATURE_SGX) ||
5750 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENCLS_EXITING))
5751 return false;
5752
5753 encls_leaf = kvm_rax_read(vcpu);
5754 if (encls_leaf > 62)
5755 encls_leaf = 63;
5756 return vmcs12->encls_exiting_bitmap & BIT_ULL(encls_leaf);
5757}
5758
Sean Christopherson55d23752018-12-03 13:53:18 -08005759static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
5760 struct vmcs12 *vmcs12, gpa_t bitmap)
5761{
5762 u32 vmx_instruction_info;
5763 unsigned long field;
5764 u8 b;
5765
5766 if (!nested_cpu_has_shadow_vmcs(vmcs12))
5767 return true;
5768
5769 /* Decode instruction info and find the field to access */
5770 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5771 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
5772
5773 /* Out-of-range fields always cause a VM exit from L2 to L1 */
5774 if (field >> 15)
5775 return true;
5776
5777 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
5778 return true;
5779
5780 return 1 & (b >> (field & 7));
5781}
5782
Oliver Uptonb045ae92020-04-14 22:47:45 +00005783static bool nested_vmx_exit_handled_mtf(struct vmcs12 *vmcs12)
5784{
5785 u32 entry_intr_info = vmcs12->vm_entry_intr_info_field;
5786
5787 if (nested_cpu_has_mtf(vmcs12))
5788 return true;
5789
5790 /*
5791 * An MTF VM-exit may be injected into the guest by setting the
5792 * interruption-type to 7 (other event) and the vector field to 0. Such
5793 * is the case regardless of the 'monitor trap flag' VM-execution
5794 * control.
5795 */
5796 return entry_intr_info == (INTR_INFO_VALID_MASK
5797 | INTR_TYPE_OTHER_EVENT);
5798}
5799
Sean Christopherson55d23752018-12-03 13:53:18 -08005800/*
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005801 * Return true if L0 wants to handle an exit from L2 regardless of whether or not
5802 * L1 wants the exit. Only call this when in is_guest_mode (L2).
Sean Christopherson55d23752018-12-03 13:53:18 -08005803 */
Sean Christopherson8e533242020-11-06 17:03:12 +08005804static bool nested_vmx_l0_wants_exit(struct kvm_vcpu *vcpu,
5805 union vmx_exit_reason exit_reason)
Sean Christopherson55d23752018-12-03 13:53:18 -08005806{
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005807 u32 intr_info;
5808
Sean Christopherson8e533242020-11-06 17:03:12 +08005809 switch ((u16)exit_reason.basic) {
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005810 case EXIT_REASON_EXCEPTION_NMI:
Sean Christopherson87915852020-04-15 13:34:54 -07005811 intr_info = vmx_get_intr_info(vcpu);
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005812 if (is_nmi(intr_info))
5813 return true;
5814 else if (is_page_fault(intr_info))
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +02005815 return vcpu->arch.apf.host_apf_flags || !enable_ept;
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005816 else if (is_debug(intr_info) &&
5817 vcpu->guest_debug &
5818 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5819 return true;
5820 else if (is_breakpoint(intr_info) &&
5821 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
5822 return true;
5823 return false;
5824 case EXIT_REASON_EXTERNAL_INTERRUPT:
5825 return true;
5826 case EXIT_REASON_MCE_DURING_VMENTRY:
5827 return true;
5828 case EXIT_REASON_EPT_VIOLATION:
5829 /*
5830 * L0 always deals with the EPT violation. If nested EPT is
5831 * used, and the nested mmu code discovers that the address is
5832 * missing in the guest EPT table (EPT12), the EPT violation
5833 * will be injected with nested_ept_inject_page_fault()
5834 */
5835 return true;
5836 case EXIT_REASON_EPT_MISCONFIG:
5837 /*
5838 * L2 never uses directly L1's EPT, but rather L0's own EPT
5839 * table (shadow on EPT) or a merged EPT table that L0 built
5840 * (EPT on EPT). So any problems with the structure of the
5841 * table is L0's fault.
5842 */
5843 return true;
5844 case EXIT_REASON_PREEMPTION_TIMER:
5845 return true;
5846 case EXIT_REASON_PML_FULL:
Sean Christophersonc3bb9a22021-02-12 16:50:07 -08005847 /*
5848 * PML is emulated for an L1 VMM and should never be enabled in
5849 * vmcs02, always "handle" PML_FULL by exiting to userspace.
5850 */
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005851 return true;
5852 case EXIT_REASON_VMFUNC:
5853 /* VM functions are emulated through L2->L0 vmexits. */
5854 return true;
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005855 default:
5856 break;
5857 }
5858 return false;
5859}
5860
5861/*
5862 * Return 1 if L1 wants to intercept an exit from L2. Only call this when in
5863 * is_guest_mode (L2).
5864 */
Sean Christopherson8e533242020-11-06 17:03:12 +08005865static bool nested_vmx_l1_wants_exit(struct kvm_vcpu *vcpu,
5866 union vmx_exit_reason exit_reason)
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005867{
Sean Christopherson55d23752018-12-03 13:53:18 -08005868 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Sean Christopherson9bd4af22020-04-21 00:53:27 -07005869 u32 intr_info;
Sean Christopherson55d23752018-12-03 13:53:18 -08005870
Sean Christopherson8e533242020-11-06 17:03:12 +08005871 switch ((u16)exit_reason.basic) {
Sean Christopherson55d23752018-12-03 13:53:18 -08005872 case EXIT_REASON_EXCEPTION_NMI:
Sean Christopherson87915852020-04-15 13:34:54 -07005873 intr_info = vmx_get_intr_info(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005874 if (is_nmi(intr_info))
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005875 return true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005876 else if (is_page_fault(intr_info))
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005877 return true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005878 return vmcs12->exception_bitmap &
5879 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
5880 case EXIT_REASON_EXTERNAL_INTERRUPT:
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005881 return nested_exit_on_intr(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005882 case EXIT_REASON_TRIPLE_FAULT:
5883 return true;
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08005884 case EXIT_REASON_INTERRUPT_WINDOW:
5885 return nested_cpu_has(vmcs12, CPU_BASED_INTR_WINDOW_EXITING);
Sean Christopherson55d23752018-12-03 13:53:18 -08005886 case EXIT_REASON_NMI_WINDOW:
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08005887 return nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING);
Sean Christopherson55d23752018-12-03 13:53:18 -08005888 case EXIT_REASON_TASK_SWITCH:
5889 return true;
5890 case EXIT_REASON_CPUID:
5891 return true;
5892 case EXIT_REASON_HLT:
5893 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
5894 case EXIT_REASON_INVD:
5895 return true;
5896 case EXIT_REASON_INVLPG:
5897 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
5898 case EXIT_REASON_RDPMC:
5899 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
5900 case EXIT_REASON_RDRAND:
5901 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
5902 case EXIT_REASON_RDSEED:
5903 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
5904 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
5905 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
5906 case EXIT_REASON_VMREAD:
5907 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
5908 vmcs12->vmread_bitmap);
5909 case EXIT_REASON_VMWRITE:
5910 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
5911 vmcs12->vmwrite_bitmap);
5912 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
5913 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
5914 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
5915 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
5916 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
5917 /*
5918 * VMX instructions trap unconditionally. This allows L1 to
5919 * emulate them for its L2 guest, i.e., allows 3-level nesting!
5920 */
5921 return true;
5922 case EXIT_REASON_CR_ACCESS:
5923 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
5924 case EXIT_REASON_DR_ACCESS:
5925 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
5926 case EXIT_REASON_IO_INSTRUCTION:
5927 return nested_vmx_exit_handled_io(vcpu, vmcs12);
5928 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
5929 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
5930 case EXIT_REASON_MSR_READ:
5931 case EXIT_REASON_MSR_WRITE:
5932 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
5933 case EXIT_REASON_INVALID_STATE:
5934 return true;
5935 case EXIT_REASON_MWAIT_INSTRUCTION:
5936 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
5937 case EXIT_REASON_MONITOR_TRAP_FLAG:
Oliver Uptonb045ae92020-04-14 22:47:45 +00005938 return nested_vmx_exit_handled_mtf(vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08005939 case EXIT_REASON_MONITOR_INSTRUCTION:
5940 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
5941 case EXIT_REASON_PAUSE_INSTRUCTION:
5942 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
5943 nested_cpu_has2(vmcs12,
5944 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
5945 case EXIT_REASON_MCE_DURING_VMENTRY:
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005946 return true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005947 case EXIT_REASON_TPR_BELOW_THRESHOLD:
5948 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
5949 case EXIT_REASON_APIC_ACCESS:
5950 case EXIT_REASON_APIC_WRITE:
5951 case EXIT_REASON_EOI_INDUCED:
5952 /*
5953 * The controls for "virtualize APIC accesses," "APIC-
5954 * register virtualization," and "virtual-interrupt
5955 * delivery" only come from vmcs12.
5956 */
5957 return true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005958 case EXIT_REASON_INVPCID:
5959 return
5960 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
5961 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
5962 case EXIT_REASON_WBINVD:
5963 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
5964 case EXIT_REASON_XSETBV:
5965 return true;
5966 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
5967 /*
5968 * This should never happen, since it is not possible to
5969 * set XSS to a non-zero value---neither in L1 nor in L2.
5970 * If if it were, XSS would have to be checked against
5971 * the XSS exit bitmap in vmcs12.
5972 */
5973 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Tao Xubf653b72019-07-16 14:55:51 +08005974 case EXIT_REASON_UMWAIT:
5975 case EXIT_REASON_TPAUSE:
5976 return nested_cpu_has2(vmcs12,
5977 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE);
Sean Christopherson72add912021-04-12 16:21:42 +12005978 case EXIT_REASON_ENCLS:
5979 return nested_vmx_exit_handled_encls(vcpu, vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08005980 default:
5981 return true;
5982 }
5983}
5984
Sean Christopherson7b7bd872020-04-15 10:55:11 -07005985/*
5986 * Conditionally reflect a VM-Exit into L1. Returns %true if the VM-Exit was
5987 * reflected into L1.
5988 */
Sean Christophersonf47baae2020-04-15 10:55:16 -07005989bool nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu)
Sean Christopherson7b7bd872020-04-15 10:55:11 -07005990{
Sean Christophersonfbdd5022020-04-15 10:55:12 -07005991 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson8e533242020-11-06 17:03:12 +08005992 union vmx_exit_reason exit_reason = vmx->exit_reason;
Sean Christopherson87796552020-04-22 17:11:27 -07005993 unsigned long exit_qual;
5994 u32 exit_intr_info;
Sean Christophersonfbdd5022020-04-15 10:55:12 -07005995
5996 WARN_ON_ONCE(vmx->nested.nested_run_pending);
5997
5998 /*
5999 * Late nested VM-Fail shares the same flow as nested VM-Exit since KVM
6000 * has already loaded L2's state.
6001 */
6002 if (unlikely(vmx->fail)) {
6003 trace_kvm_nested_vmenter_failed(
6004 "hardware VM-instruction error: ",
6005 vmcs_read32(VM_INSTRUCTION_ERROR));
6006 exit_intr_info = 0;
6007 exit_qual = 0;
6008 goto reflect_vmexit;
6009 }
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006010
Sean Christopherson8e533242020-11-06 17:03:12 +08006011 trace_kvm_nested_vmexit(exit_reason.full, vcpu, KVM_ISA_VMX);
Sean Christopherson236871b2020-04-15 10:55:13 -07006012
Sean Christopherson2c1f3322020-04-15 10:55:14 -07006013 /* If L0 (KVM) wants the exit, it trumps L1's desires. */
6014 if (nested_vmx_l0_wants_exit(vcpu, exit_reason))
6015 return false;
6016
6017 /* If L1 doesn't want the exit, handle it in L0. */
6018 if (!nested_vmx_l1_wants_exit(vcpu, exit_reason))
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006019 return false;
6020
6021 /*
Sean Christopherson1d283062020-04-15 10:55:15 -07006022 * vmcs.VM_EXIT_INTR_INFO is only valid for EXCEPTION_NMI exits. For
6023 * EXTERNAL_INTERRUPT, the value for vmcs12->vm_exit_intr_info would
6024 * need to be synthesized by querying the in-kernel LAPIC, but external
6025 * interrupts are never reflected to L1 so it's a non-issue.
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006026 */
Sean Christopherson02f19652020-09-23 13:13:49 -07006027 exit_intr_info = vmx_get_intr_info(vcpu);
Sean Christophersonf315f2b2020-09-23 13:13:45 -07006028 if (is_exception_with_error_code(exit_intr_info)) {
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006029 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6030
6031 vmcs12->vm_exit_intr_error_code =
6032 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
6033 }
Sean Christopherson02f19652020-09-23 13:13:49 -07006034 exit_qual = vmx_get_exit_qual(vcpu);
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006035
Sean Christophersonfbdd5022020-04-15 10:55:12 -07006036reflect_vmexit:
Sean Christopherson8e533242020-11-06 17:03:12 +08006037 nested_vmx_vmexit(vcpu, exit_reason.full, exit_intr_info, exit_qual);
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006038 return true;
6039}
Sean Christopherson55d23752018-12-03 13:53:18 -08006040
6041static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
6042 struct kvm_nested_state __user *user_kvm_nested_state,
6043 u32 user_data_size)
6044{
6045 struct vcpu_vmx *vmx;
6046 struct vmcs12 *vmcs12;
6047 struct kvm_nested_state kvm_state = {
6048 .flags = 0,
Liran Alon6ca00df2019-06-16 15:03:10 +03006049 .format = KVM_STATE_NESTED_FORMAT_VMX,
Sean Christopherson55d23752018-12-03 13:53:18 -08006050 .size = sizeof(kvm_state),
Peter Shier850448f2020-05-26 14:51:06 -07006051 .hdr.vmx.flags = 0,
Liran Alon6ca00df2019-06-16 15:03:10 +03006052 .hdr.vmx.vmxon_pa = -1ull,
6053 .hdr.vmx.vmcs12_pa = -1ull,
Peter Shier850448f2020-05-26 14:51:06 -07006054 .hdr.vmx.preemption_timer_deadline = 0,
Sean Christopherson55d23752018-12-03 13:53:18 -08006055 };
Liran Alon6ca00df2019-06-16 15:03:10 +03006056 struct kvm_vmx_nested_state_data __user *user_vmx_nested_state =
6057 &user_kvm_nested_state->data.vmx[0];
Sean Christopherson55d23752018-12-03 13:53:18 -08006058
6059 if (!vcpu)
Liran Alon6ca00df2019-06-16 15:03:10 +03006060 return kvm_state.size + sizeof(*user_vmx_nested_state);
Sean Christopherson55d23752018-12-03 13:53:18 -08006061
6062 vmx = to_vmx(vcpu);
6063 vmcs12 = get_vmcs12(vcpu);
6064
Sean Christopherson55d23752018-12-03 13:53:18 -08006065 if (nested_vmx_allowed(vcpu) &&
6066 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
Liran Alon6ca00df2019-06-16 15:03:10 +03006067 kvm_state.hdr.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
6068 kvm_state.hdr.vmx.vmcs12_pa = vmx->nested.current_vmptr;
Sean Christopherson55d23752018-12-03 13:53:18 -08006069
6070 if (vmx_has_valid_vmcs12(vcpu)) {
Liran Alon6ca00df2019-06-16 15:03:10 +03006071 kvm_state.size += sizeof(user_vmx_nested_state->vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08006072
Liran Alon323d73a2019-06-26 16:09:27 +03006073 if (vmx->nested.hv_evmcs)
6074 kvm_state.flags |= KVM_STATE_NESTED_EVMCS;
6075
Sean Christopherson55d23752018-12-03 13:53:18 -08006076 if (is_guest_mode(vcpu) &&
6077 nested_cpu_has_shadow_vmcs(vmcs12) &&
6078 vmcs12->vmcs_link_pointer != -1ull)
Liran Alon6ca00df2019-06-16 15:03:10 +03006079 kvm_state.size += sizeof(user_vmx_nested_state->shadow_vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08006080 }
6081
6082 if (vmx->nested.smm.vmxon)
Liran Alon6ca00df2019-06-16 15:03:10 +03006083 kvm_state.hdr.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
Sean Christopherson55d23752018-12-03 13:53:18 -08006084
6085 if (vmx->nested.smm.guest_mode)
Liran Alon6ca00df2019-06-16 15:03:10 +03006086 kvm_state.hdr.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
Sean Christopherson55d23752018-12-03 13:53:18 -08006087
6088 if (is_guest_mode(vcpu)) {
6089 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
6090
6091 if (vmx->nested.nested_run_pending)
6092 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
Oliver Upton5ef8acb2020-02-07 02:36:07 -08006093
6094 if (vmx->nested.mtf_pending)
6095 kvm_state.flags |= KVM_STATE_NESTED_MTF_PENDING;
Peter Shier850448f2020-05-26 14:51:06 -07006096
6097 if (nested_cpu_has_preemption_timer(vmcs12) &&
6098 vmx->nested.has_preemption_timer_deadline) {
6099 kvm_state.hdr.vmx.flags |=
6100 KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE;
6101 kvm_state.hdr.vmx.preemption_timer_deadline =
6102 vmx->nested.preemption_timer_deadline;
6103 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006104 }
6105 }
6106
6107 if (user_data_size < kvm_state.size)
6108 goto out;
6109
6110 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
6111 return -EFAULT;
6112
6113 if (!vmx_has_valid_vmcs12(vcpu))
6114 goto out;
6115
6116 /*
6117 * When running L2, the authoritative vmcs12 state is in the
6118 * vmcs02. When running L1, the authoritative vmcs12 state is
6119 * in the shadow or enlightened vmcs linked to vmcs01, unless
Sean Christopherson3731905ef2019-05-07 08:36:27 -07006120 * need_vmcs12_to_shadow_sync is set, in which case, the authoritative
Sean Christopherson55d23752018-12-03 13:53:18 -08006121 * vmcs12 state is in the vmcs12 already.
6122 */
6123 if (is_guest_mode(vcpu)) {
Sean Christopherson3731905ef2019-05-07 08:36:27 -07006124 sync_vmcs02_to_vmcs12(vcpu, vmcs12);
Sean Christopherson7952d762019-05-07 08:36:29 -07006125 sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
Maxim Levitskyd51e1d32021-01-14 22:54:47 +02006126 } else {
6127 copy_vmcs02_to_vmcs12_rare(vcpu, get_vmcs12(vcpu));
6128 if (!vmx->nested.need_vmcs12_to_shadow_sync) {
6129 if (vmx->nested.hv_evmcs)
6130 copy_enlightened_to_vmcs12(vmx);
6131 else if (enable_shadow_vmcs)
6132 copy_shadow_to_vmcs12(vmx);
6133 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006134 }
6135
Liran Alon6ca00df2019-06-16 15:03:10 +03006136 BUILD_BUG_ON(sizeof(user_vmx_nested_state->vmcs12) < VMCS12_SIZE);
6137 BUILD_BUG_ON(sizeof(user_vmx_nested_state->shadow_vmcs12) < VMCS12_SIZE);
6138
Tom Roeder3a33d032019-01-24 13:48:20 -08006139 /*
6140 * Copy over the full allocated size of vmcs12 rather than just the size
6141 * of the struct.
6142 */
Liran Alon6ca00df2019-06-16 15:03:10 +03006143 if (copy_to_user(user_vmx_nested_state->vmcs12, vmcs12, VMCS12_SIZE))
Sean Christopherson55d23752018-12-03 13:53:18 -08006144 return -EFAULT;
6145
6146 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
6147 vmcs12->vmcs_link_pointer != -1ull) {
Liran Alon6ca00df2019-06-16 15:03:10 +03006148 if (copy_to_user(user_vmx_nested_state->shadow_vmcs12,
Tom Roeder3a33d032019-01-24 13:48:20 -08006149 get_shadow_vmcs12(vcpu), VMCS12_SIZE))
Sean Christopherson55d23752018-12-03 13:53:18 -08006150 return -EFAULT;
6151 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006152out:
6153 return kvm_state.size;
6154}
6155
6156/*
6157 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
6158 */
6159void vmx_leave_nested(struct kvm_vcpu *vcpu)
6160{
6161 if (is_guest_mode(vcpu)) {
6162 to_vmx(vcpu)->nested.nested_run_pending = 0;
6163 nested_vmx_vmexit(vcpu, -1, 0, 0);
6164 }
6165 free_nested(vcpu);
6166}
6167
6168static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
6169 struct kvm_nested_state __user *user_kvm_nested_state,
6170 struct kvm_nested_state *kvm_state)
6171{
6172 struct vcpu_vmx *vmx = to_vmx(vcpu);
6173 struct vmcs12 *vmcs12;
Sean Christopherson68cda402020-05-11 15:05:29 -07006174 enum vm_entry_failure_code ignored;
Liran Alon6ca00df2019-06-16 15:03:10 +03006175 struct kvm_vmx_nested_state_data __user *user_vmx_nested_state =
6176 &user_kvm_nested_state->data.vmx[0];
Sean Christopherson55d23752018-12-03 13:53:18 -08006177 int ret;
6178
Liran Alon6ca00df2019-06-16 15:03:10 +03006179 if (kvm_state->format != KVM_STATE_NESTED_FORMAT_VMX)
Sean Christopherson55d23752018-12-03 13:53:18 -08006180 return -EINVAL;
6181
Liran Alon6ca00df2019-06-16 15:03:10 +03006182 if (kvm_state->hdr.vmx.vmxon_pa == -1ull) {
6183 if (kvm_state->hdr.vmx.smm.flags)
Sean Christopherson55d23752018-12-03 13:53:18 -08006184 return -EINVAL;
6185
Liran Alon6ca00df2019-06-16 15:03:10 +03006186 if (kvm_state->hdr.vmx.vmcs12_pa != -1ull)
Sean Christopherson55d23752018-12-03 13:53:18 -08006187 return -EINVAL;
6188
Liran Alon323d73a2019-06-26 16:09:27 +03006189 /*
6190 * KVM_STATE_NESTED_EVMCS used to signal that KVM should
6191 * enable eVMCS capability on vCPU. However, since then
6192 * code was changed such that flag signals vmcs12 should
6193 * be copied into eVMCS in guest memory.
6194 *
6195 * To preserve backwards compatability, allow user
6196 * to set this flag even when there is no VMXON region.
6197 */
Paolo Bonzini9fd58872019-06-19 16:52:27 +02006198 if (kvm_state->flags & ~KVM_STATE_NESTED_EVMCS)
6199 return -EINVAL;
6200 } else {
6201 if (!nested_vmx_allowed(vcpu))
6202 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08006203
Paolo Bonzini9fd58872019-06-19 16:52:27 +02006204 if (!page_address_valid(vcpu, kvm_state->hdr.vmx.vmxon_pa))
6205 return -EINVAL;
Liran Alon323d73a2019-06-26 16:09:27 +03006206 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006207
Liran Alon6ca00df2019-06-16 15:03:10 +03006208 if ((kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
Sean Christopherson55d23752018-12-03 13:53:18 -08006209 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
6210 return -EINVAL;
6211
Liran Alon6ca00df2019-06-16 15:03:10 +03006212 if (kvm_state->hdr.vmx.smm.flags &
Sean Christopherson55d23752018-12-03 13:53:18 -08006213 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
6214 return -EINVAL;
6215
Paolo Bonzini5e105c82020-07-27 08:55:09 -04006216 if (kvm_state->hdr.vmx.flags & ~KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE)
6217 return -EINVAL;
6218
Sean Christopherson55d23752018-12-03 13:53:18 -08006219 /*
6220 * SMM temporarily disables VMX, so we cannot be in guest mode,
6221 * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags
6222 * must be zero.
6223 */
Liran Alon65b712f12019-06-25 14:26:42 +03006224 if (is_smm(vcpu) ?
6225 (kvm_state->flags &
6226 (KVM_STATE_NESTED_GUEST_MODE | KVM_STATE_NESTED_RUN_PENDING))
6227 : kvm_state->hdr.vmx.smm.flags)
Sean Christopherson55d23752018-12-03 13:53:18 -08006228 return -EINVAL;
6229
Liran Alon6ca00df2019-06-16 15:03:10 +03006230 if ((kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
6231 !(kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
Sean Christopherson55d23752018-12-03 13:53:18 -08006232 return -EINVAL;
6233
Liran Alon323d73a2019-06-26 16:09:27 +03006234 if ((kvm_state->flags & KVM_STATE_NESTED_EVMCS) &&
6235 (!nested_vmx_allowed(vcpu) || !vmx->nested.enlightened_vmcs_enabled))
Paolo Bonzini9fd58872019-06-19 16:52:27 +02006236 return -EINVAL;
6237
Liran Alon323d73a2019-06-26 16:09:27 +03006238 vmx_leave_nested(vcpu);
Paolo Bonzini9fd58872019-06-19 16:52:27 +02006239
Liran Alon6ca00df2019-06-16 15:03:10 +03006240 if (kvm_state->hdr.vmx.vmxon_pa == -1ull)
Sean Christopherson55d23752018-12-03 13:53:18 -08006241 return 0;
6242
Liran Alon6ca00df2019-06-16 15:03:10 +03006243 vmx->nested.vmxon_ptr = kvm_state->hdr.vmx.vmxon_pa;
Sean Christopherson55d23752018-12-03 13:53:18 -08006244 ret = enter_vmx_operation(vcpu);
6245 if (ret)
6246 return ret;
6247
Paolo Bonzini0f02bd02020-07-27 09:00:37 -04006248 /* Empty 'VMXON' state is permitted if no VMCS loaded */
6249 if (kvm_state->size < sizeof(*kvm_state) + sizeof(*vmcs12)) {
6250 /* See vmx_has_valid_vmcs12. */
6251 if ((kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE) ||
6252 (kvm_state->flags & KVM_STATE_NESTED_EVMCS) ||
6253 (kvm_state->hdr.vmx.vmcs12_pa != -1ull))
6254 return -EINVAL;
6255 else
6256 return 0;
6257 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006258
Liran Alon6ca00df2019-06-16 15:03:10 +03006259 if (kvm_state->hdr.vmx.vmcs12_pa != -1ull) {
6260 if (kvm_state->hdr.vmx.vmcs12_pa == kvm_state->hdr.vmx.vmxon_pa ||
6261 !page_address_valid(vcpu, kvm_state->hdr.vmx.vmcs12_pa))
Sean Christopherson55d23752018-12-03 13:53:18 -08006262 return -EINVAL;
6263
Liran Alon6ca00df2019-06-16 15:03:10 +03006264 set_current_vmptr(vmx, kvm_state->hdr.vmx.vmcs12_pa);
Sean Christopherson55d23752018-12-03 13:53:18 -08006265 } else if (kvm_state->flags & KVM_STATE_NESTED_EVMCS) {
6266 /*
Vitaly Kuznetsove942dbf2020-03-09 16:52:12 +01006267 * nested_vmx_handle_enlightened_vmptrld() cannot be called
6268 * directly from here as HV_X64_MSR_VP_ASSIST_PAGE may not be
6269 * restored yet. EVMCS will be mapped from
6270 * nested_get_vmcs12_pages().
Sean Christopherson55d23752018-12-03 13:53:18 -08006271 */
Paolo Bonzini729c15c2020-09-22 06:53:57 -04006272 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08006273 } else {
6274 return -EINVAL;
6275 }
6276
Liran Alon6ca00df2019-06-16 15:03:10 +03006277 if (kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
Sean Christopherson55d23752018-12-03 13:53:18 -08006278 vmx->nested.smm.vmxon = true;
6279 vmx->nested.vmxon = false;
6280
Liran Alon6ca00df2019-06-16 15:03:10 +03006281 if (kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
Sean Christopherson55d23752018-12-03 13:53:18 -08006282 vmx->nested.smm.guest_mode = true;
6283 }
6284
6285 vmcs12 = get_vmcs12(vcpu);
Liran Alon6ca00df2019-06-16 15:03:10 +03006286 if (copy_from_user(vmcs12, user_vmx_nested_state->vmcs12, sizeof(*vmcs12)))
Sean Christopherson55d23752018-12-03 13:53:18 -08006287 return -EFAULT;
6288
6289 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
6290 return -EINVAL;
6291
6292 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
6293 return 0;
6294
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006295 vmx->nested.nested_run_pending =
6296 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
6297
Oliver Upton5ef8acb2020-02-07 02:36:07 -08006298 vmx->nested.mtf_pending =
6299 !!(kvm_state->flags & KVM_STATE_NESTED_MTF_PENDING);
6300
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006301 ret = -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08006302 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
6303 vmcs12->vmcs_link_pointer != -1ull) {
6304 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
6305
Liran Alon6ca00df2019-06-16 15:03:10 +03006306 if (kvm_state->size <
6307 sizeof(*kvm_state) +
6308 sizeof(user_vmx_nested_state->vmcs12) + sizeof(*shadow_vmcs12))
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006309 goto error_guest_mode;
Sean Christopherson55d23752018-12-03 13:53:18 -08006310
6311 if (copy_from_user(shadow_vmcs12,
Liran Alon6ca00df2019-06-16 15:03:10 +03006312 user_vmx_nested_state->shadow_vmcs12,
6313 sizeof(*shadow_vmcs12))) {
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006314 ret = -EFAULT;
6315 goto error_guest_mode;
6316 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006317
6318 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
6319 !shadow_vmcs12->hdr.shadow_vmcs)
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006320 goto error_guest_mode;
Sean Christopherson55d23752018-12-03 13:53:18 -08006321 }
6322
Paolo Bonzini83d31e52020-07-09 13:12:09 -04006323 vmx->nested.has_preemption_timer_deadline = false;
Peter Shier850448f2020-05-26 14:51:06 -07006324 if (kvm_state->hdr.vmx.flags & KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE) {
6325 vmx->nested.has_preemption_timer_deadline = true;
6326 vmx->nested.preemption_timer_deadline =
6327 kvm_state->hdr.vmx.preemption_timer_deadline;
6328 }
6329
Sean Christopherson5478ba32019-04-11 12:18:06 -07006330 if (nested_vmx_check_controls(vcpu, vmcs12) ||
6331 nested_vmx_check_host_state(vcpu, vmcs12) ||
Sean Christopherson68cda402020-05-11 15:05:29 -07006332 nested_vmx_check_guest_state(vcpu, vmcs12, &ignored))
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006333 goto error_guest_mode;
Sean Christopherson55d23752018-12-03 13:53:18 -08006334
6335 vmx->nested.dirty_vmcs12 = true;
6336 ret = nested_vmx_enter_non_root_mode(vcpu, false);
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006337 if (ret)
6338 goto error_guest_mode;
Sean Christopherson55d23752018-12-03 13:53:18 -08006339
6340 return 0;
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006341
6342error_guest_mode:
6343 vmx->nested.nested_run_pending = 0;
6344 return ret;
Sean Christopherson55d23752018-12-03 13:53:18 -08006345}
6346
Xiaoyao Li1b842922019-10-20 17:11:01 +08006347void nested_vmx_set_vmcs_shadowing_bitmap(void)
Sean Christopherson55d23752018-12-03 13:53:18 -08006348{
6349 if (enable_shadow_vmcs) {
Sean Christopherson55d23752018-12-03 13:53:18 -08006350 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Sean Christophersonfadcead2019-05-07 08:36:23 -07006351 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
Sean Christopherson55d23752018-12-03 13:53:18 -08006352 }
6353}
6354
6355/*
6356 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
6357 * returned for the various VMX controls MSRs when nested VMX is enabled.
6358 * The same values should also be used to verify that vmcs12 control fields are
6359 * valid during nested entry from L1 to L2.
6360 * Each of these control msrs has a low and high 32-bit half: A low bit is on
6361 * if the corresponding bit in the (32-bit) control field *must* be on, and a
6362 * bit in the high half is on if the corresponding bit in the control field
6363 * may be on. See also vmx_control_verify().
6364 */
Vitaly Kuznetsova4443262020-02-20 18:22:04 +01006365void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, u32 ept_caps)
Sean Christopherson55d23752018-12-03 13:53:18 -08006366{
6367 /*
6368 * Note that as a general rule, the high half of the MSRs (bits in
6369 * the control fields which may be 1) should be initialized by the
6370 * intersection of the underlying hardware's MSR (i.e., features which
6371 * can be supported) and the list of features we want to expose -
6372 * because they are known to be properly supported in our code.
6373 * Also, usually, the low half of the MSRs (bits which must be 1) can
6374 * be set to 0, meaning that L1 may turn off any of these bits. The
6375 * reason is that if one of these bits is necessary, it will appear
6376 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
6377 * fields of vmcs01 and vmcs02, will turn these bits off - and
Sean Christopherson2c1f3322020-04-15 10:55:14 -07006378 * nested_vmx_l1_wants_exit() will not pass related exits to L1.
Sean Christopherson55d23752018-12-03 13:53:18 -08006379 * These rules have exceptions below.
6380 */
6381
6382 /* pin-based controls */
6383 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
6384 msrs->pinbased_ctls_low,
6385 msrs->pinbased_ctls_high);
6386 msrs->pinbased_ctls_low |=
6387 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
6388 msrs->pinbased_ctls_high &=
6389 PIN_BASED_EXT_INTR_MASK |
6390 PIN_BASED_NMI_EXITING |
6391 PIN_BASED_VIRTUAL_NMIS |
Vitaly Kuznetsova4443262020-02-20 18:22:04 +01006392 (enable_apicv ? PIN_BASED_POSTED_INTR : 0);
Sean Christopherson55d23752018-12-03 13:53:18 -08006393 msrs->pinbased_ctls_high |=
6394 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
6395 PIN_BASED_VMX_PREEMPTION_TIMER;
6396
6397 /* exit controls */
6398 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
6399 msrs->exit_ctls_low,
6400 msrs->exit_ctls_high);
6401 msrs->exit_ctls_low =
6402 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
6403
6404 msrs->exit_ctls_high &=
6405#ifdef CONFIG_X86_64
6406 VM_EXIT_HOST_ADDR_SPACE_SIZE |
6407#endif
Chenyi Qiangefc83132020-08-28 16:56:18 +08006408 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT |
6409 VM_EXIT_CLEAR_BNDCFGS | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
Sean Christopherson55d23752018-12-03 13:53:18 -08006410 msrs->exit_ctls_high |=
6411 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
6412 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
6413 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
6414
6415 /* We support free control of debug control saving. */
6416 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
6417
6418 /* entry controls */
6419 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
6420 msrs->entry_ctls_low,
6421 msrs->entry_ctls_high);
6422 msrs->entry_ctls_low =
6423 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
6424 msrs->entry_ctls_high &=
6425#ifdef CONFIG_X86_64
6426 VM_ENTRY_IA32E_MODE |
6427#endif
Chenyi Qiangefc83132020-08-28 16:56:18 +08006428 VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS |
6429 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
Sean Christopherson55d23752018-12-03 13:53:18 -08006430 msrs->entry_ctls_high |=
6431 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
6432
6433 /* We support free control of debug control loading. */
6434 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
6435
6436 /* cpu-based controls */
6437 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
6438 msrs->procbased_ctls_low,
6439 msrs->procbased_ctls_high);
6440 msrs->procbased_ctls_low =
6441 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
6442 msrs->procbased_ctls_high &=
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08006443 CPU_BASED_INTR_WINDOW_EXITING |
Xiaoyao Li5e3d3942019-12-06 16:45:26 +08006444 CPU_BASED_NMI_WINDOW_EXITING | CPU_BASED_USE_TSC_OFFSETTING |
Sean Christopherson55d23752018-12-03 13:53:18 -08006445 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
6446 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
6447 CPU_BASED_CR3_STORE_EXITING |
6448#ifdef CONFIG_X86_64
6449 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
6450#endif
6451 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
6452 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
6453 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
6454 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
6455 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
6456 /*
6457 * We can allow some features even when not supported by the
6458 * hardware. For example, L1 can specify an MSR bitmap - and we
6459 * can use it to avoid exits to L1 - even when L0 runs L2
6460 * without MSR bitmaps.
6461 */
6462 msrs->procbased_ctls_high |=
6463 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
6464 CPU_BASED_USE_MSR_BITMAPS;
6465
6466 /* We support free control of CR3 access interception. */
6467 msrs->procbased_ctls_low &=
6468 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
6469
6470 /*
6471 * secondary cpu-based controls. Do not include those that
Xiaoyao Li7c1b7612020-07-09 12:34:25 +08006472 * depend on CPUID bits, they are added later by
6473 * vmx_vcpu_after_set_cpuid.
Sean Christopherson55d23752018-12-03 13:53:18 -08006474 */
Vitaly Kuznetsov6b1971c2019-02-07 11:42:14 +01006475 if (msrs->procbased_ctls_high & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)
6476 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
6477 msrs->secondary_ctls_low,
6478 msrs->secondary_ctls_high);
6479
Sean Christopherson55d23752018-12-03 13:53:18 -08006480 msrs->secondary_ctls_low = 0;
6481 msrs->secondary_ctls_high &=
6482 SECONDARY_EXEC_DESC |
Sean Christopherson7f3603b2020-09-23 09:50:47 -07006483 SECONDARY_EXEC_ENABLE_RDTSCP |
Sean Christopherson55d23752018-12-03 13:53:18 -08006484 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini6defc592019-07-02 14:39:29 +02006485 SECONDARY_EXEC_WBINVD_EXITING |
Sean Christopherson55d23752018-12-03 13:53:18 -08006486 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6487 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini6defc592019-07-02 14:39:29 +02006488 SECONDARY_EXEC_RDRAND_EXITING |
6489 SECONDARY_EXEC_ENABLE_INVPCID |
6490 SECONDARY_EXEC_RDSEED_EXITING |
Ilias Stamatisd041b5e2021-05-26 19:44:17 +01006491 SECONDARY_EXEC_XSAVES |
6492 SECONDARY_EXEC_TSC_SCALING;
Sean Christopherson55d23752018-12-03 13:53:18 -08006493
6494 /*
6495 * We can emulate "VMCS shadowing," even if the hardware
6496 * doesn't support it.
6497 */
6498 msrs->secondary_ctls_high |=
6499 SECONDARY_EXEC_SHADOW_VMCS;
6500
6501 if (enable_ept) {
6502 /* nested EPT: emulate EPT also to L1 */
6503 msrs->secondary_ctls_high |=
6504 SECONDARY_EXEC_ENABLE_EPT;
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08006505 msrs->ept_caps =
6506 VMX_EPT_PAGE_WALK_4_BIT |
6507 VMX_EPT_PAGE_WALK_5_BIT |
6508 VMX_EPTP_WB_BIT |
Sean Christopherson96d47012020-03-02 18:02:40 -08006509 VMX_EPT_INVEPT_BIT |
6510 VMX_EPT_EXECUTE_ONLY_BIT;
6511
Sean Christopherson55d23752018-12-03 13:53:18 -08006512 msrs->ept_caps &= ept_caps;
6513 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
6514 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
6515 VMX_EPT_1GB_PAGE_BIT;
6516 if (enable_ept_ad_bits) {
6517 msrs->secondary_ctls_high |=
6518 SECONDARY_EXEC_ENABLE_PML;
6519 msrs->ept_caps |= VMX_EPT_AD_BIT;
6520 }
6521 }
6522
6523 if (cpu_has_vmx_vmfunc()) {
6524 msrs->secondary_ctls_high |=
6525 SECONDARY_EXEC_ENABLE_VMFUNC;
6526 /*
6527 * Advertise EPTP switching unconditionally
6528 * since we emulate it
6529 */
6530 if (enable_ept)
6531 msrs->vmfunc_controls =
6532 VMX_VMFUNC_EPTP_SWITCHING;
6533 }
6534
6535 /*
6536 * Old versions of KVM use the single-context version without
6537 * checking for support, so declare that it is supported even
6538 * though it is treated as global context. The alternative is
6539 * not failing the single-context invvpid, and it is worse.
6540 */
6541 if (enable_vpid) {
6542 msrs->secondary_ctls_high |=
6543 SECONDARY_EXEC_ENABLE_VPID;
6544 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
6545 VMX_VPID_EXTENT_SUPPORTED_MASK;
6546 }
6547
6548 if (enable_unrestricted_guest)
6549 msrs->secondary_ctls_high |=
6550 SECONDARY_EXEC_UNRESTRICTED_GUEST;
6551
6552 if (flexpriority_enabled)
6553 msrs->secondary_ctls_high |=
6554 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6555
Sean Christopherson72add912021-04-12 16:21:42 +12006556 if (enable_sgx)
6557 msrs->secondary_ctls_high |= SECONDARY_EXEC_ENCLS_EXITING;
6558
Sean Christopherson55d23752018-12-03 13:53:18 -08006559 /* miscellaneous data */
6560 rdmsr(MSR_IA32_VMX_MISC,
6561 msrs->misc_low,
6562 msrs->misc_high);
6563 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
6564 msrs->misc_low |=
6565 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
6566 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Yadong Qibf0cd882020-11-06 14:51:22 +08006567 VMX_MISC_ACTIVITY_HLT |
6568 VMX_MISC_ACTIVITY_WAIT_SIPI;
Sean Christopherson55d23752018-12-03 13:53:18 -08006569 msrs->misc_high = 0;
6570
6571 /*
6572 * This MSR reports some information about VMX support. We
6573 * should return information about the VMX we emulate for the
6574 * guest, and the VMCS structure we give it - not about the
6575 * VMX support of the underlying hardware.
6576 */
6577 msrs->basic =
6578 VMCS12_REVISION |
6579 VMX_BASIC_TRUE_CTLS |
6580 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
6581 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
6582
6583 if (cpu_has_vmx_basic_inout())
6584 msrs->basic |= VMX_BASIC_INOUT;
6585
6586 /*
6587 * These MSRs specify bits which the guest must keep fixed on
6588 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
6589 * We picked the standard core2 setting.
6590 */
6591#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
6592#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
6593 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
6594 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
6595
6596 /* These MSRs specify bits which the guest must keep fixed off. */
6597 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
6598 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
6599
6600 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
6601 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
6602}
6603
6604void nested_vmx_hardware_unsetup(void)
6605{
6606 int i;
6607
6608 if (enable_shadow_vmcs) {
6609 for (i = 0; i < VMX_BITMAP_NR; i++)
6610 free_page((unsigned long)vmx_bitmap[i]);
6611 }
6612}
6613
Sean Christopherson6c1c6e52020-05-06 13:46:53 -07006614__init int nested_vmx_hardware_setup(int (*exit_handlers[])(struct kvm_vcpu *))
Sean Christopherson55d23752018-12-03 13:53:18 -08006615{
6616 int i;
6617
6618 if (!cpu_has_vmx_shadow_vmcs())
6619 enable_shadow_vmcs = 0;
6620 if (enable_shadow_vmcs) {
6621 for (i = 0; i < VMX_BITMAP_NR; i++) {
Ben Gardon41836832019-02-11 11:02:52 -08006622 /*
6623 * The vmx_bitmap is not tied to a VM and so should
6624 * not be charged to a memcg.
6625 */
Sean Christopherson55d23752018-12-03 13:53:18 -08006626 vmx_bitmap[i] = (unsigned long *)
6627 __get_free_page(GFP_KERNEL);
6628 if (!vmx_bitmap[i]) {
6629 nested_vmx_hardware_unsetup();
6630 return -ENOMEM;
6631 }
6632 }
6633
6634 init_vmcs_shadow_fields();
6635 }
6636
Liran Aloncc877672019-11-18 21:11:21 +02006637 exit_handlers[EXIT_REASON_VMCLEAR] = handle_vmclear;
6638 exit_handlers[EXIT_REASON_VMLAUNCH] = handle_vmlaunch;
6639 exit_handlers[EXIT_REASON_VMPTRLD] = handle_vmptrld;
6640 exit_handlers[EXIT_REASON_VMPTRST] = handle_vmptrst;
6641 exit_handlers[EXIT_REASON_VMREAD] = handle_vmread;
6642 exit_handlers[EXIT_REASON_VMRESUME] = handle_vmresume;
6643 exit_handlers[EXIT_REASON_VMWRITE] = handle_vmwrite;
6644 exit_handlers[EXIT_REASON_VMOFF] = handle_vmoff;
6645 exit_handlers[EXIT_REASON_VMON] = handle_vmon;
6646 exit_handlers[EXIT_REASON_INVEPT] = handle_invept;
6647 exit_handlers[EXIT_REASON_INVVPID] = handle_invvpid;
6648 exit_handlers[EXIT_REASON_VMFUNC] = handle_vmfunc;
Sean Christopherson55d23752018-12-03 13:53:18 -08006649
Sean Christopherson55d23752018-12-03 13:53:18 -08006650 return 0;
6651}
Paolo Bonzini33b22172020-04-17 10:24:18 -04006652
6653struct kvm_x86_nested_ops vmx_nested_ops = {
6654 .check_events = vmx_check_nested_events,
Sean Christophersond2060bd2020-04-22 19:25:39 -07006655 .hv_timer_pending = nested_vmx_preemption_timer_pending,
Sean Christophersoncb6a32c2021-03-02 09:45:14 -08006656 .triple_fault = nested_vmx_triple_fault,
Paolo Bonzini33b22172020-04-17 10:24:18 -04006657 .get_state = vmx_get_nested_state,
6658 .set_state = vmx_set_nested_state,
Paolo Bonzini9a78e152021-01-08 11:43:08 -05006659 .get_nested_state_pages = vmx_get_nested_state_pages,
Sean Christopherson02f5fb22020-06-22 14:58:32 -07006660 .write_log_dirty = nested_vmx_write_pml_buffer,
Paolo Bonzini33b22172020-04-17 10:24:18 -04006661 .enable_evmcs = nested_enable_evmcs,
6662 .get_evmcs_version = nested_get_evmcs_version,
6663};