blob: 341c5081682293ffb2cdc368b34c60284268f240 [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 /*
Vitaly Kuznetsovb7685cf2021-05-26 15:20:23 +0200176 * We don't need to force sync to shadow VMCS because
177 * VM_INSTRUCTION_ERROR is not shadowed. Enlightened VMCS 'shadows' all
178 * fields and thus must be synced.
Sean Christopherson55d23752018-12-03 13:53:18 -0800179 */
Vitaly Kuznetsovb7685cf2021-05-26 15:20:23 +0200180 if (to_vmx(vcpu)->nested.hv_evmcs_vmptr != EVMPTR_INVALID)
181 to_vmx(vcpu)->nested.need_vmcs12_to_shadow_sync = true;
182
Sean Christopherson55d23752018-12-03 13:53:18 -0800183 return kvm_skip_emulated_instruction(vcpu);
184}
185
Sean Christophersonb2656e42020-06-08 18:56:07 -0700186static int nested_vmx_fail(struct kvm_vcpu *vcpu, u32 vm_instruction_error)
187{
188 struct vcpu_vmx *vmx = to_vmx(vcpu);
189
190 /*
191 * failValid writes the error number to the current VMCS, which
192 * can't be done if there isn't a current VMCS.
193 */
Yu Zhang64c78502021-09-30 01:51:53 +0800194 if (vmx->nested.current_vmptr == INVALID_GPA &&
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +0200195 !evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
Sean Christophersonb2656e42020-06-08 18:56:07 -0700196 return nested_vmx_failInvalid(vcpu);
197
198 return nested_vmx_failValid(vcpu, vm_instruction_error);
199}
200
Sean Christopherson55d23752018-12-03 13:53:18 -0800201static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
202{
203 /* TODO: not to reset guest simply here. */
204 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
205 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
206}
207
Marc Orrf0b51052019-09-17 11:50:57 -0700208static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
209{
210 return fixed_bits_valid(control, low, high);
211}
212
213static inline u64 vmx_control_msr(u32 low, u32 high)
214{
215 return low | ((u64)high << 32);
216}
217
Sean Christopherson55d23752018-12-03 13:53:18 -0800218static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
219{
Sean Christophersonfe7f895d2019-05-07 12:17:57 -0700220 secondary_exec_controls_clearbit(vmx, SECONDARY_EXEC_SHADOW_VMCS);
Yu Zhang64c78502021-09-30 01:51:53 +0800221 vmcs_write64(VMCS_LINK_POINTER, INVALID_GPA);
Paolo Bonzini88dddc12019-07-19 18:41:10 +0200222 vmx->nested.need_vmcs12_to_shadow_sync = false;
Sean Christopherson55d23752018-12-03 13:53:18 -0800223}
224
225static inline void nested_release_evmcs(struct kvm_vcpu *vcpu)
226{
227 struct vcpu_vmx *vmx = to_vmx(vcpu);
228
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +0200229 if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) {
230 kvm_vcpu_unmap(vcpu, &vmx->nested.hv_evmcs_map, true);
231 vmx->nested.hv_evmcs = NULL;
232 }
Sean Christopherson55d23752018-12-03 13:53:18 -0800233
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +0200234 vmx->nested.hv_evmcs_vmptr = EVMPTR_INVALID;
Sean Christopherson55d23752018-12-03 13:53:18 -0800235}
236
Sean Christophersonc61ca2f2020-09-23 11:44:49 -0700237static void vmx_sync_vmcs_host_state(struct vcpu_vmx *vmx,
238 struct loaded_vmcs *prev)
239{
240 struct vmcs_host_state *dest, *src;
241
242 if (unlikely(!vmx->guest_state_loaded))
243 return;
244
245 src = &prev->host_state;
246 dest = &vmx->loaded_vmcs->host_state;
247
248 vmx_set_host_fs_gs(dest, src->fs_sel, src->gs_sel, src->fs_base, src->gs_base);
249 dest->ldt_sel = src->ldt_sel;
250#ifdef CONFIG_X86_64
251 dest->ds_sel = src->ds_sel;
252 dest->es_sel = src->es_sel;
253#endif
254}
255
256static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
257{
258 struct vcpu_vmx *vmx = to_vmx(vcpu);
259 struct loaded_vmcs *prev;
260 int cpu;
261
Sean Christopherson138534a2020-09-23 11:44:52 -0700262 if (WARN_ON_ONCE(vmx->loaded_vmcs == vmcs))
Sean Christophersonc61ca2f2020-09-23 11:44:49 -0700263 return;
264
265 cpu = get_cpu();
266 prev = vmx->loaded_vmcs;
267 vmx->loaded_vmcs = vmcs;
268 vmx_vcpu_load_vmcs(vcpu, cpu, prev);
269 vmx_sync_vmcs_host_state(vmx, prev);
270 put_cpu();
271
272 vmx_register_cache_reset(vcpu);
273}
274
Sean Christopherson55d23752018-12-03 13:53:18 -0800275/*
276 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
277 * just stops using VMX.
278 */
279static void free_nested(struct kvm_vcpu *vcpu)
280{
281 struct vcpu_vmx *vmx = to_vmx(vcpu);
282
Sean Christophersondf82a242020-09-23 11:44:50 -0700283 if (WARN_ON_ONCE(vmx->loaded_vmcs != &vmx->vmcs01))
284 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
285
Sean Christopherson55d23752018-12-03 13:53:18 -0800286 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
287 return;
288
Paolo Bonzini729c15c2020-09-22 06:53:57 -0400289 kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
Jan Kiszkacf645272019-07-21 13:52:18 +0200290
Sean Christopherson55d23752018-12-03 13:53:18 -0800291 vmx->nested.vmxon = false;
292 vmx->nested.smm.vmxon = false;
Vitaly Kuznetsovfeb31622021-09-30 01:51:54 +0800293 vmx->nested.vmxon_ptr = INVALID_GPA;
Sean Christopherson55d23752018-12-03 13:53:18 -0800294 free_vpid(vmx->nested.vpid02);
295 vmx->nested.posted_intr_nv = -1;
Yu Zhang64c78502021-09-30 01:51:53 +0800296 vmx->nested.current_vmptr = INVALID_GPA;
Sean Christopherson55d23752018-12-03 13:53:18 -0800297 if (enable_shadow_vmcs) {
298 vmx_disable_shadow_vmcs(vmx);
299 vmcs_clear(vmx->vmcs01.shadow_vmcs);
300 free_vmcs(vmx->vmcs01.shadow_vmcs);
301 vmx->vmcs01.shadow_vmcs = NULL;
302 }
303 kfree(vmx->nested.cached_vmcs12);
Jan Kiszkac6bf2ae2019-07-21 16:01:36 +0200304 vmx->nested.cached_vmcs12 = NULL;
Sean Christopherson55d23752018-12-03 13:53:18 -0800305 kfree(vmx->nested.cached_shadow_vmcs12);
Jan Kiszkac6bf2ae2019-07-21 16:01:36 +0200306 vmx->nested.cached_shadow_vmcs12 = NULL;
Sean Christopherson55d23752018-12-03 13:53:18 -0800307 /* Unpin physical memory we referred to in the vmcs02 */
308 if (vmx->nested.apic_access_page) {
Liran Alonb11494b2019-11-21 00:31:47 +0200309 kvm_release_page_clean(vmx->nested.apic_access_page);
Sean Christopherson55d23752018-12-03 13:53:18 -0800310 vmx->nested.apic_access_page = NULL;
311 }
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +0100312 kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map, true);
KarimAllah Ahmed3278e042019-01-31 21:24:38 +0100313 kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map, true);
314 vmx->nested.pi_desc = NULL;
Sean Christopherson55d23752018-12-03 13:53:18 -0800315
316 kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
317
318 nested_release_evmcs(vcpu);
319
320 free_loaded_vmcs(&vmx->nested.vmcs02);
321}
322
Sean Christopherson55d23752018-12-03 13:53:18 -0800323/*
324 * Ensure that the current vmcs of the logical processor is the
325 * vmcs01 of the vcpu before calling free_nested().
326 */
327void nested_vmx_free_vcpu(struct kvm_vcpu *vcpu)
328{
329 vcpu_load(vcpu);
Paolo Bonzinib4b65b52019-01-29 19:12:35 +0100330 vmx_leave_nested(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -0800331 vcpu_put(vcpu);
332}
333
Junaid Shahid85aa8882021-08-06 15:22:29 -0700334#define EPTP_PA_MASK GENMASK_ULL(51, 12)
335
336static bool nested_ept_root_matches(hpa_t root_hpa, u64 root_eptp, u64 eptp)
337{
338 return VALID_PAGE(root_hpa) &&
339 ((root_eptp & EPTP_PA_MASK) == (eptp & EPTP_PA_MASK));
340}
341
342static void nested_ept_invalidate_addr(struct kvm_vcpu *vcpu, gpa_t eptp,
343 gpa_t addr)
344{
345 uint i;
346 struct kvm_mmu_root_info *cached_root;
347
348 WARN_ON_ONCE(!mmu_is_nested(vcpu));
349
350 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) {
351 cached_root = &vcpu->arch.mmu->prev_roots[i];
352
353 if (nested_ept_root_matches(cached_root->hpa, cached_root->pgd,
354 eptp))
355 vcpu->arch.mmu->invlpg(vcpu, addr, cached_root->hpa);
356 }
357}
358
Sean Christopherson55d23752018-12-03 13:53:18 -0800359static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
360 struct x86_exception *fault)
361{
362 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
363 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson4dcefa32020-04-15 10:55:18 -0700364 u32 vm_exit_reason;
Sean Christopherson55d23752018-12-03 13:53:18 -0800365 unsigned long exit_qualification = vcpu->arch.exit_qualification;
366
367 if (vmx->nested.pml_full) {
Sean Christopherson4dcefa32020-04-15 10:55:18 -0700368 vm_exit_reason = EXIT_REASON_PML_FULL;
Sean Christopherson55d23752018-12-03 13:53:18 -0800369 vmx->nested.pml_full = false;
370 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
Junaid Shahid85aa8882021-08-06 15:22:29 -0700371 } else {
372 if (fault->error_code & PFERR_RSVD_MASK)
373 vm_exit_reason = EXIT_REASON_EPT_MISCONFIG;
374 else
375 vm_exit_reason = EXIT_REASON_EPT_VIOLATION;
376
377 /*
378 * Although the caller (kvm_inject_emulated_page_fault) would
379 * have already synced the faulting address in the shadow EPT
380 * tables for the current EPTP12, we also need to sync it for
381 * any other cached EPTP02s based on the same EP4TA, since the
382 * TLB associates mappings to the EP4TA rather than the full EPTP.
383 */
384 nested_ept_invalidate_addr(vcpu, vmcs12->ept_pointer,
385 fault->address);
386 }
Sean Christopherson55d23752018-12-03 13:53:18 -0800387
Sean Christopherson4dcefa32020-04-15 10:55:18 -0700388 nested_vmx_vmexit(vcpu, vm_exit_reason, 0, exit_qualification);
Sean Christopherson55d23752018-12-03 13:53:18 -0800389 vmcs12->guest_physical_address = fault->address;
390}
391
Sean Christopherson39353ab2021-06-09 16:42:31 -0700392static void nested_ept_new_eptp(struct kvm_vcpu *vcpu)
393{
394 kvm_init_shadow_ept_mmu(vcpu,
395 to_vmx(vcpu)->nested.msrs.ept_caps &
396 VMX_EPT_EXECUTE_ONLY_BIT,
397 nested_ept_ad_enabled(vcpu),
398 nested_ept_get_eptp(vcpu));
399}
400
Sean Christopherson55d23752018-12-03 13:53:18 -0800401static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
402{
403 WARN_ON(mmu_is_nested(vcpu));
404
405 vcpu->arch.mmu = &vcpu->arch.guest_mmu;
Sean Christopherson39353ab2021-06-09 16:42:31 -0700406 nested_ept_new_eptp(vcpu);
Sean Christophersond8dd54e2020-03-02 18:02:39 -0800407 vcpu->arch.mmu->get_guest_pgd = nested_ept_get_eptp;
Sean Christopherson55d23752018-12-03 13:53:18 -0800408 vcpu->arch.mmu->inject_page_fault = nested_ept_inject_page_fault;
409 vcpu->arch.mmu->get_pdptr = kvm_pdptr_read;
410
411 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
412}
413
414static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
415{
416 vcpu->arch.mmu = &vcpu->arch.root_mmu;
417 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
418}
419
420static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
421 u16 error_code)
422{
423 bool inequality, bit;
424
425 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
426 inequality =
427 (error_code & vmcs12->page_fault_error_code_mask) !=
428 vmcs12->page_fault_error_code_match;
429 return inequality ^ bit;
430}
431
432
433/*
434 * KVM wants to inject page-faults which it got to the guest. This function
435 * checks whether in a nested guest, we need to inject them to L1 or L2.
436 */
437static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
438{
439 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
440 unsigned int nr = vcpu->arch.exception.nr;
441 bool has_payload = vcpu->arch.exception.has_payload;
442 unsigned long payload = vcpu->arch.exception.payload;
443
444 if (nr == PF_VECTOR) {
445 if (vcpu->arch.exception.nested_apf) {
446 *exit_qual = vcpu->arch.apf.nested_apf_token;
447 return 1;
448 }
449 if (nested_vmx_is_page_fault_vmexit(vmcs12,
450 vcpu->arch.exception.error_code)) {
451 *exit_qual = has_payload ? payload : vcpu->arch.cr2;
452 return 1;
453 }
454 } else if (vmcs12->exception_bitmap & (1u << nr)) {
455 if (nr == DB_VECTOR) {
456 if (!has_payload) {
457 payload = vcpu->arch.dr6;
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +0800458 payload &= ~DR6_BT;
459 payload ^= DR6_ACTIVE_LOW;
Sean Christopherson55d23752018-12-03 13:53:18 -0800460 }
461 *exit_qual = payload;
462 } else
463 *exit_qual = 0;
464 return 1;
465 }
466
467 return 0;
468}
469
470
471static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
472 struct x86_exception *fault)
473{
474 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
475
476 WARN_ON(!is_guest_mode(vcpu));
477
478 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
479 !to_vmx(vcpu)->nested.nested_run_pending) {
480 vmcs12->vm_exit_intr_error_code = fault->error_code;
481 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
482 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
483 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
484 fault->address);
485 } else {
486 kvm_inject_page_fault(vcpu, fault);
487 }
488}
489
Sean Christopherson55d23752018-12-03 13:53:18 -0800490static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
491 struct vmcs12 *vmcs12)
492{
493 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
494 return 0;
495
Sean Christopherson5497b952019-07-11 08:58:29 -0700496 if (CC(!page_address_valid(vcpu, vmcs12->io_bitmap_a)) ||
497 CC(!page_address_valid(vcpu, vmcs12->io_bitmap_b)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800498 return -EINVAL;
499
500 return 0;
501}
502
503static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
504 struct vmcs12 *vmcs12)
505{
506 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
507 return 0;
508
Sean Christopherson5497b952019-07-11 08:58:29 -0700509 if (CC(!page_address_valid(vcpu, vmcs12->msr_bitmap)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800510 return -EINVAL;
511
512 return 0;
513}
514
515static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
516 struct vmcs12 *vmcs12)
517{
518 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
519 return 0;
520
Sean Christopherson5497b952019-07-11 08:58:29 -0700521 if (CC(!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800522 return -EINVAL;
523
524 return 0;
525}
526
527/*
Sean Christophersona5e0c252021-11-09 01:30:47 +0000528 * For x2APIC MSRs, ignore the vmcs01 bitmap. L1 can enable x2APIC without L1
529 * itself utilizing x2APIC. All MSRs were previously set to be intercepted,
530 * only the "disable intercept" case needs to be handled.
Sean Christopherson55d23752018-12-03 13:53:18 -0800531 */
Sean Christophersona5e0c252021-11-09 01:30:47 +0000532static void nested_vmx_disable_intercept_for_x2apic_msr(unsigned long *msr_bitmap_l1,
533 unsigned long *msr_bitmap_l0,
534 u32 msr, int type)
Sean Christopherson55d23752018-12-03 13:53:18 -0800535{
Sean Christophersona5e0c252021-11-09 01:30:47 +0000536 if (type & MSR_TYPE_R && !vmx_test_msr_bitmap_read(msr_bitmap_l1, msr))
537 vmx_clear_msr_bitmap_read(msr_bitmap_l0, msr);
Sean Christopherson55d23752018-12-03 13:53:18 -0800538
Sean Christophersona5e0c252021-11-09 01:30:47 +0000539 if (type & MSR_TYPE_W && !vmx_test_msr_bitmap_write(msr_bitmap_l1, msr))
540 vmx_clear_msr_bitmap_write(msr_bitmap_l0, msr);
Sean Christopherson55d23752018-12-03 13:53:18 -0800541}
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 Christopherson67f4b992021-11-09 01:30:45 +0000555#define BUILD_NVMX_MSR_INTERCEPT_HELPER(rw) \
556static inline \
557void nested_vmx_set_msr_##rw##_intercept(struct vcpu_vmx *vmx, \
558 unsigned long *msr_bitmap_l1, \
559 unsigned long *msr_bitmap_l0, u32 msr) \
560{ \
561 if (vmx_test_msr_bitmap_##rw(vmx->vmcs01.msr_bitmap, msr) || \
562 vmx_test_msr_bitmap_##rw(msr_bitmap_l1, msr)) \
563 vmx_set_msr_bitmap_##rw(msr_bitmap_l0, msr); \
564 else \
565 vmx_clear_msr_bitmap_##rw(msr_bitmap_l0, msr); \
566}
567BUILD_NVMX_MSR_INTERCEPT_HELPER(read)
568BUILD_NVMX_MSR_INTERCEPT_HELPER(write)
569
570static inline void nested_vmx_set_intercept_for_msr(struct vcpu_vmx *vmx,
571 unsigned long *msr_bitmap_l1,
572 unsigned long *msr_bitmap_l0,
573 u32 msr, int types)
574{
575 if (types & MSR_TYPE_R)
576 nested_vmx_set_msr_read_intercept(vmx, msr_bitmap_l1,
577 msr_bitmap_l0, msr);
578 if (types & MSR_TYPE_W)
579 nested_vmx_set_msr_write_intercept(vmx, msr_bitmap_l1,
580 msr_bitmap_l0, msr);
581}
582
Sean Christopherson55d23752018-12-03 13:53:18 -0800583/*
584 * Merge L0's and L1's MSR bitmap, return false to indicate that
585 * we do not use the hardware.
586 */
587static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
588 struct vmcs12 *vmcs12)
589{
Sean Christopherson67f4b992021-11-09 01:30:45 +0000590 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -0800591 int msr;
Sean Christopherson55d23752018-12-03 13:53:18 -0800592 unsigned long *msr_bitmap_l1;
Sean Christopherson67f4b992021-11-09 01:30:45 +0000593 unsigned long *msr_bitmap_l0 = vmx->nested.vmcs02.msr_bitmap;
594 struct kvm_host_map *map = &vmx->nested.msr_bitmap_map;
Sean Christopherson55d23752018-12-03 13:53:18 -0800595
596 /* Nothing to do if the MSR bitmap is not in use. */
597 if (!cpu_has_vmx_msr_bitmap() ||
598 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
599 return false;
600
KarimAllah Ahmed31f0b6c2019-01-31 21:24:36 +0100601 if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->msr_bitmap), map))
Sean Christopherson55d23752018-12-03 13:53:18 -0800602 return false;
603
KarimAllah Ahmed31f0b6c2019-01-31 21:24:36 +0100604 msr_bitmap_l1 = (unsigned long *)map->hva;
Sean Christopherson55d23752018-12-03 13:53:18 -0800605
Marc Orracff7842019-04-01 23:55:59 -0700606 /*
607 * To keep the control flow simple, pay eight 8-byte writes (sixteen
608 * 4-byte writes on 32-bit systems) up front to enable intercepts for
Sean Christophersona5e0c252021-11-09 01:30:47 +0000609 * the x2APIC MSR range and selectively toggle those relevant to L2.
Marc Orracff7842019-04-01 23:55:59 -0700610 */
611 enable_x2apic_msr_intercepts(msr_bitmap_l0);
Sean Christopherson55d23752018-12-03 13:53:18 -0800612
Marc Orracff7842019-04-01 23:55:59 -0700613 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
614 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
615 /*
616 * L0 need not intercept reads for MSRs between 0x800
617 * and 0x8ff, it just lets the processor take the value
618 * from the virtual-APIC page; take those 256 bits
619 * directly from the L1 bitmap.
620 */
621 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
622 unsigned word = msr / BITS_PER_LONG;
623
624 msr_bitmap_l0[word] = msr_bitmap_l1[word];
625 }
626 }
627
Sean Christophersona5e0c252021-11-09 01:30:47 +0000628 nested_vmx_disable_intercept_for_x2apic_msr(
Sean Christopherson55d23752018-12-03 13:53:18 -0800629 msr_bitmap_l1, msr_bitmap_l0,
Marc Orracff7842019-04-01 23:55:59 -0700630 X2APIC_MSR(APIC_TASKPRI),
Marc Orrc73f4c92019-04-01 23:56:00 -0700631 MSR_TYPE_R | MSR_TYPE_W);
Marc Orracff7842019-04-01 23:55:59 -0700632
633 if (nested_cpu_has_vid(vmcs12)) {
Sean Christophersona5e0c252021-11-09 01:30:47 +0000634 nested_vmx_disable_intercept_for_x2apic_msr(
Marc Orracff7842019-04-01 23:55:59 -0700635 msr_bitmap_l1, msr_bitmap_l0,
636 X2APIC_MSR(APIC_EOI),
637 MSR_TYPE_W);
Sean Christophersona5e0c252021-11-09 01:30:47 +0000638 nested_vmx_disable_intercept_for_x2apic_msr(
Marc Orracff7842019-04-01 23:55:59 -0700639 msr_bitmap_l1, msr_bitmap_l0,
640 X2APIC_MSR(APIC_SELF_IPI),
641 MSR_TYPE_W);
642 }
Sean Christopherson55d23752018-12-03 13:53:18 -0800643 }
644
Sean Christophersond69129b2019-05-08 07:32:15 -0700645 /*
Sean Christopherson67f4b992021-11-09 01:30:45 +0000646 * Always check vmcs01's bitmap to honor userspace MSR filters and any
647 * other runtime changes to vmcs01's bitmap, e.g. dynamic pass-through.
Sean Christophersond69129b2019-05-08 07:32:15 -0700648 */
Sean Christopherson67f4b992021-11-09 01:30:45 +0000649#ifdef CONFIG_X86_64
650 nested_vmx_set_intercept_for_msr(vmx, msr_bitmap_l1, msr_bitmap_l0,
651 MSR_FS_BASE, MSR_TYPE_RW);
Sean Christopherson55d23752018-12-03 13:53:18 -0800652
Sean Christopherson67f4b992021-11-09 01:30:45 +0000653 nested_vmx_set_intercept_for_msr(vmx, msr_bitmap_l1, msr_bitmap_l0,
654 MSR_GS_BASE, MSR_TYPE_RW);
Sean Christopherson55d23752018-12-03 13:53:18 -0800655
Sean Christopherson67f4b992021-11-09 01:30:45 +0000656 nested_vmx_set_intercept_for_msr(vmx, msr_bitmap_l1, msr_bitmap_l0,
657 MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
658#endif
659 nested_vmx_set_intercept_for_msr(vmx, msr_bitmap_l1, msr_bitmap_l0,
660 MSR_IA32_SPEC_CTRL, MSR_TYPE_RW);
661
662 nested_vmx_set_intercept_for_msr(vmx, msr_bitmap_l1, msr_bitmap_l0,
663 MSR_IA32_PRED_CMD, MSR_TYPE_W);
664
665 kvm_vcpu_unmap(vcpu, &vmx->nested.msr_bitmap_map, false);
Sean Christopherson55d23752018-12-03 13:53:18 -0800666
667 return true;
668}
669
670static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
671 struct vmcs12 *vmcs12)
672{
KarimAllah Ahmed88925302019-01-31 21:24:41 +0100673 struct kvm_host_map map;
Sean Christopherson55d23752018-12-03 13:53:18 -0800674 struct vmcs12 *shadow;
Sean Christopherson55d23752018-12-03 13:53:18 -0800675
676 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
Yu Zhang64c78502021-09-30 01:51:53 +0800677 vmcs12->vmcs_link_pointer == INVALID_GPA)
Sean Christopherson55d23752018-12-03 13:53:18 -0800678 return;
679
680 shadow = get_shadow_vmcs12(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -0800681
KarimAllah Ahmed88925302019-01-31 21:24:41 +0100682 if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->vmcs_link_pointer), &map))
683 return;
Sean Christopherson55d23752018-12-03 13:53:18 -0800684
KarimAllah Ahmed88925302019-01-31 21:24:41 +0100685 memcpy(shadow, map.hva, VMCS12_SIZE);
686 kvm_vcpu_unmap(vcpu, &map, false);
Sean Christopherson55d23752018-12-03 13:53:18 -0800687}
688
689static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
690 struct vmcs12 *vmcs12)
691{
692 struct vcpu_vmx *vmx = to_vmx(vcpu);
693
694 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
Yu Zhang64c78502021-09-30 01:51:53 +0800695 vmcs12->vmcs_link_pointer == INVALID_GPA)
Sean Christopherson55d23752018-12-03 13:53:18 -0800696 return;
697
698 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
699 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
700}
701
702/*
703 * In nested virtualization, check if L1 has set
704 * VM_EXIT_ACK_INTR_ON_EXIT
705 */
706static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
707{
708 return get_vmcs12(vcpu)->vm_exit_controls &
709 VM_EXIT_ACK_INTR_ON_EXIT;
710}
711
Sean Christopherson55d23752018-12-03 13:53:18 -0800712static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
713 struct vmcs12 *vmcs12)
714{
715 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
Sean Christopherson5497b952019-07-11 08:58:29 -0700716 CC(!page_address_valid(vcpu, vmcs12->apic_access_addr)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800717 return -EINVAL;
718 else
719 return 0;
720}
721
722static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
723 struct vmcs12 *vmcs12)
724{
725 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
726 !nested_cpu_has_apic_reg_virt(vmcs12) &&
727 !nested_cpu_has_vid(vmcs12) &&
728 !nested_cpu_has_posted_intr(vmcs12))
729 return 0;
730
731 /*
732 * If virtualize x2apic mode is enabled,
733 * virtualize apic access must be disabled.
734 */
Sean Christopherson5497b952019-07-11 08:58:29 -0700735 if (CC(nested_cpu_has_virt_x2apic_mode(vmcs12) &&
736 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800737 return -EINVAL;
738
739 /*
740 * If virtual interrupt delivery is enabled,
741 * we must exit on external interrupts.
742 */
Sean Christopherson5497b952019-07-11 08:58:29 -0700743 if (CC(nested_cpu_has_vid(vmcs12) && !nested_exit_on_intr(vcpu)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800744 return -EINVAL;
745
746 /*
747 * bits 15:8 should be zero in posted_intr_nv,
748 * the descriptor address has been already checked
749 * in nested_get_vmcs12_pages.
750 *
751 * bits 5:0 of posted_intr_desc_addr should be zero.
752 */
753 if (nested_cpu_has_posted_intr(vmcs12) &&
Sean Christopherson5497b952019-07-11 08:58:29 -0700754 (CC(!nested_cpu_has_vid(vmcs12)) ||
755 CC(!nested_exit_intr_ack_set(vcpu)) ||
756 CC((vmcs12->posted_intr_nv & 0xff00)) ||
Sean Christopherson636e8b72021-02-03 16:01:10 -0800757 CC(!kvm_vcpu_is_legal_aligned_gpa(vcpu, vmcs12->posted_intr_desc_addr, 64))))
Sean Christopherson55d23752018-12-03 13:53:18 -0800758 return -EINVAL;
759
760 /* tpr shadow is needed by all apicv features. */
Sean Christopherson5497b952019-07-11 08:58:29 -0700761 if (CC(!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800762 return -EINVAL;
763
764 return 0;
765}
766
767static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
Sean Christophersonf9b245e2018-12-12 13:30:08 -0500768 u32 count, u64 addr)
Sean Christopherson55d23752018-12-03 13:53:18 -0800769{
Sean Christopherson55d23752018-12-03 13:53:18 -0800770 if (count == 0)
771 return 0;
Sean Christopherson636e8b72021-02-03 16:01:10 -0800772
773 if (!kvm_vcpu_is_legal_aligned_gpa(vcpu, addr, 16) ||
774 !kvm_vcpu_is_legal_gpa(vcpu, (addr + count * sizeof(struct vmx_msr_entry) - 1)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800775 return -EINVAL;
Sean Christophersonf9b245e2018-12-12 13:30:08 -0500776
Sean Christopherson55d23752018-12-03 13:53:18 -0800777 return 0;
778}
779
Krish Sadhukhan61446ba2018-12-12 13:30:09 -0500780static int nested_vmx_check_exit_msr_switch_controls(struct kvm_vcpu *vcpu,
781 struct vmcs12 *vmcs12)
Sean Christopherson55d23752018-12-03 13:53:18 -0800782{
Sean Christopherson5497b952019-07-11 08:58:29 -0700783 if (CC(nested_vmx_check_msr_switch(vcpu,
784 vmcs12->vm_exit_msr_load_count,
785 vmcs12->vm_exit_msr_load_addr)) ||
786 CC(nested_vmx_check_msr_switch(vcpu,
787 vmcs12->vm_exit_msr_store_count,
788 vmcs12->vm_exit_msr_store_addr)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800789 return -EINVAL;
Sean Christophersonf9b245e2018-12-12 13:30:08 -0500790
Sean Christopherson55d23752018-12-03 13:53:18 -0800791 return 0;
792}
793
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -0500794static int nested_vmx_check_entry_msr_switch_controls(struct kvm_vcpu *vcpu,
795 struct vmcs12 *vmcs12)
Krish Sadhukhan61446ba2018-12-12 13:30:09 -0500796{
Sean Christopherson5497b952019-07-11 08:58:29 -0700797 if (CC(nested_vmx_check_msr_switch(vcpu,
798 vmcs12->vm_entry_msr_load_count,
799 vmcs12->vm_entry_msr_load_addr)))
Krish Sadhukhan61446ba2018-12-12 13:30:09 -0500800 return -EINVAL;
801
802 return 0;
803}
804
Sean Christopherson55d23752018-12-03 13:53:18 -0800805static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
806 struct vmcs12 *vmcs12)
807{
808 if (!nested_cpu_has_pml(vmcs12))
809 return 0;
810
Sean Christopherson5497b952019-07-11 08:58:29 -0700811 if (CC(!nested_cpu_has_ept(vmcs12)) ||
812 CC(!page_address_valid(vcpu, vmcs12->pml_address)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800813 return -EINVAL;
814
815 return 0;
816}
817
818static int nested_vmx_check_unrestricted_guest_controls(struct kvm_vcpu *vcpu,
819 struct vmcs12 *vmcs12)
820{
Sean Christopherson5497b952019-07-11 08:58:29 -0700821 if (CC(nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST) &&
822 !nested_cpu_has_ept(vmcs12)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800823 return -EINVAL;
824 return 0;
825}
826
827static int nested_vmx_check_mode_based_ept_exec_controls(struct kvm_vcpu *vcpu,
828 struct vmcs12 *vmcs12)
829{
Sean Christopherson5497b952019-07-11 08:58:29 -0700830 if (CC(nested_cpu_has2(vmcs12, SECONDARY_EXEC_MODE_BASED_EPT_EXEC) &&
831 !nested_cpu_has_ept(vmcs12)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800832 return -EINVAL;
833 return 0;
834}
835
836static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
837 struct vmcs12 *vmcs12)
838{
839 if (!nested_cpu_has_shadow_vmcs(vmcs12))
840 return 0;
841
Sean Christopherson5497b952019-07-11 08:58:29 -0700842 if (CC(!page_address_valid(vcpu, vmcs12->vmread_bitmap)) ||
843 CC(!page_address_valid(vcpu, vmcs12->vmwrite_bitmap)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800844 return -EINVAL;
845
846 return 0;
847}
848
849static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
850 struct vmx_msr_entry *e)
851{
852 /* x2APIC MSR accesses are not allowed */
Sean Christopherson5497b952019-07-11 08:58:29 -0700853 if (CC(vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8))
Sean Christopherson55d23752018-12-03 13:53:18 -0800854 return -EINVAL;
Sean Christopherson5497b952019-07-11 08:58:29 -0700855 if (CC(e->index == MSR_IA32_UCODE_WRITE) || /* SDM Table 35-2 */
856 CC(e->index == MSR_IA32_UCODE_REV))
Sean Christopherson55d23752018-12-03 13:53:18 -0800857 return -EINVAL;
Sean Christopherson5497b952019-07-11 08:58:29 -0700858 if (CC(e->reserved != 0))
Sean Christopherson55d23752018-12-03 13:53:18 -0800859 return -EINVAL;
860 return 0;
861}
862
863static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
864 struct vmx_msr_entry *e)
865{
Sean Christopherson5497b952019-07-11 08:58:29 -0700866 if (CC(e->index == MSR_FS_BASE) ||
867 CC(e->index == MSR_GS_BASE) ||
868 CC(e->index == MSR_IA32_SMM_MONITOR_CTL) || /* SMM is not supported */
Sean Christopherson55d23752018-12-03 13:53:18 -0800869 nested_vmx_msr_check_common(vcpu, e))
870 return -EINVAL;
871 return 0;
872}
873
874static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
875 struct vmx_msr_entry *e)
876{
Sean Christopherson5497b952019-07-11 08:58:29 -0700877 if (CC(e->index == MSR_IA32_SMBASE) || /* SMM is not supported */
Sean Christopherson55d23752018-12-03 13:53:18 -0800878 nested_vmx_msr_check_common(vcpu, e))
879 return -EINVAL;
880 return 0;
881}
882
Marc Orrf0b51052019-09-17 11:50:57 -0700883static u32 nested_vmx_max_atomic_switch_msrs(struct kvm_vcpu *vcpu)
884{
885 struct vcpu_vmx *vmx = to_vmx(vcpu);
886 u64 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
887 vmx->nested.msrs.misc_high);
888
889 return (vmx_misc_max_msr(vmx_misc) + 1) * VMX_MISC_MSR_LIST_MULTIPLIER;
890}
891
Sean Christopherson55d23752018-12-03 13:53:18 -0800892/*
893 * Load guest's/host's msr at nested entry/exit.
894 * return 0 for success, entry index for failure.
Marc Orrf0b51052019-09-17 11:50:57 -0700895 *
896 * One of the failure modes for MSR load/store is when a list exceeds the
897 * virtual hardware's capacity. To maintain compatibility with hardware inasmuch
898 * as possible, process all valid entries before failing rather than precheck
899 * for a capacity violation.
Sean Christopherson55d23752018-12-03 13:53:18 -0800900 */
901static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
902{
903 u32 i;
904 struct vmx_msr_entry e;
Marc Orrf0b51052019-09-17 11:50:57 -0700905 u32 max_msr_list_size = nested_vmx_max_atomic_switch_msrs(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -0800906
Sean Christopherson55d23752018-12-03 13:53:18 -0800907 for (i = 0; i < count; i++) {
Marc Orrf0b51052019-09-17 11:50:57 -0700908 if (unlikely(i >= max_msr_list_size))
909 goto fail;
910
Sean Christopherson55d23752018-12-03 13:53:18 -0800911 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
912 &e, sizeof(e))) {
913 pr_debug_ratelimited(
914 "%s cannot read MSR entry (%u, 0x%08llx)\n",
915 __func__, i, gpa + i * sizeof(e));
916 goto fail;
917 }
918 if (nested_vmx_load_msr_check(vcpu, &e)) {
919 pr_debug_ratelimited(
920 "%s check failed (%u, 0x%x, 0x%x)\n",
921 __func__, i, e.index, e.reserved);
922 goto fail;
923 }
Sean Christophersonf20935d2019-09-05 14:22:54 -0700924 if (kvm_set_msr(vcpu, e.index, e.value)) {
Sean Christopherson55d23752018-12-03 13:53:18 -0800925 pr_debug_ratelimited(
926 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
927 __func__, i, e.index, e.value);
928 goto fail;
929 }
930 }
931 return 0;
932fail:
Sean Christopherson68cda402020-05-11 15:05:29 -0700933 /* Note, max_msr_list_size is at most 4096, i.e. this can't wrap. */
Sean Christopherson55d23752018-12-03 13:53:18 -0800934 return i + 1;
935}
936
Aaron Lewis662f1d12019-11-07 21:14:39 -0800937static bool nested_vmx_get_vmexit_msr_value(struct kvm_vcpu *vcpu,
938 u32 msr_index,
939 u64 *data)
940{
941 struct vcpu_vmx *vmx = to_vmx(vcpu);
942
943 /*
944 * If the L0 hypervisor stored a more accurate value for the TSC that
945 * does not include the time taken for emulation of the L2->L1
946 * VM-exit in L0, use the more accurate value.
947 */
948 if (msr_index == MSR_IA32_TSC) {
Sean Christophersona128a932020-09-23 11:03:57 -0700949 int i = vmx_find_loadstore_msr_slot(&vmx->msr_autostore.guest,
950 MSR_IA32_TSC);
Aaron Lewis662f1d12019-11-07 21:14:39 -0800951
Sean Christophersona128a932020-09-23 11:03:57 -0700952 if (i >= 0) {
953 u64 val = vmx->msr_autostore.guest.val[i].value;
Aaron Lewis662f1d12019-11-07 21:14:39 -0800954
955 *data = kvm_read_l1_tsc(vcpu, val);
956 return true;
957 }
958 }
959
960 if (kvm_get_msr(vcpu, msr_index, data)) {
961 pr_debug_ratelimited("%s cannot read MSR (0x%x)\n", __func__,
962 msr_index);
963 return false;
964 }
965 return true;
966}
967
Aaron Lewis365d3d52019-11-07 21:14:36 -0800968static bool read_and_check_msr_entry(struct kvm_vcpu *vcpu, u64 gpa, int i,
969 struct vmx_msr_entry *e)
970{
971 if (kvm_vcpu_read_guest(vcpu,
972 gpa + i * sizeof(*e),
973 e, 2 * sizeof(u32))) {
974 pr_debug_ratelimited(
975 "%s cannot read MSR entry (%u, 0x%08llx)\n",
976 __func__, i, gpa + i * sizeof(*e));
977 return false;
978 }
979 if (nested_vmx_store_msr_check(vcpu, e)) {
980 pr_debug_ratelimited(
981 "%s check failed (%u, 0x%x, 0x%x)\n",
982 __func__, i, e->index, e->reserved);
983 return false;
984 }
985 return true;
986}
987
Sean Christopherson55d23752018-12-03 13:53:18 -0800988static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
989{
Sean Christophersonf20935d2019-09-05 14:22:54 -0700990 u64 data;
Sean Christopherson55d23752018-12-03 13:53:18 -0800991 u32 i;
992 struct vmx_msr_entry e;
Marc Orrf0b51052019-09-17 11:50:57 -0700993 u32 max_msr_list_size = nested_vmx_max_atomic_switch_msrs(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -0800994
995 for (i = 0; i < count; i++) {
Marc Orrf0b51052019-09-17 11:50:57 -0700996 if (unlikely(i >= max_msr_list_size))
997 return -EINVAL;
998
Aaron Lewis365d3d52019-11-07 21:14:36 -0800999 if (!read_and_check_msr_entry(vcpu, gpa, i, &e))
Sean Christopherson55d23752018-12-03 13:53:18 -08001000 return -EINVAL;
Aaron Lewis365d3d52019-11-07 21:14:36 -08001001
Aaron Lewis662f1d12019-11-07 21:14:39 -08001002 if (!nested_vmx_get_vmexit_msr_value(vcpu, e.index, &data))
Sean Christopherson55d23752018-12-03 13:53:18 -08001003 return -EINVAL;
Aaron Lewis662f1d12019-11-07 21:14:39 -08001004
Sean Christopherson55d23752018-12-03 13:53:18 -08001005 if (kvm_vcpu_write_guest(vcpu,
1006 gpa + i * sizeof(e) +
1007 offsetof(struct vmx_msr_entry, value),
Sean Christophersonf20935d2019-09-05 14:22:54 -07001008 &data, sizeof(data))) {
Sean Christopherson55d23752018-12-03 13:53:18 -08001009 pr_debug_ratelimited(
1010 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Sean Christophersonf20935d2019-09-05 14:22:54 -07001011 __func__, i, e.index, data);
Sean Christopherson55d23752018-12-03 13:53:18 -08001012 return -EINVAL;
1013 }
1014 }
1015 return 0;
1016}
1017
Aaron Lewis662f1d12019-11-07 21:14:39 -08001018static bool nested_msr_store_list_has_msr(struct kvm_vcpu *vcpu, u32 msr_index)
1019{
1020 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1021 u32 count = vmcs12->vm_exit_msr_store_count;
1022 u64 gpa = vmcs12->vm_exit_msr_store_addr;
1023 struct vmx_msr_entry e;
1024 u32 i;
1025
1026 for (i = 0; i < count; i++) {
1027 if (!read_and_check_msr_entry(vcpu, gpa, i, &e))
1028 return false;
1029
1030 if (e.index == msr_index)
1031 return true;
1032 }
1033 return false;
1034}
1035
1036static void prepare_vmx_msr_autostore_list(struct kvm_vcpu *vcpu,
1037 u32 msr_index)
1038{
1039 struct vcpu_vmx *vmx = to_vmx(vcpu);
1040 struct vmx_msrs *autostore = &vmx->msr_autostore.guest;
1041 bool in_vmcs12_store_list;
Sean Christophersona128a932020-09-23 11:03:57 -07001042 int msr_autostore_slot;
Aaron Lewis662f1d12019-11-07 21:14:39 -08001043 bool in_autostore_list;
1044 int last;
1045
Sean Christophersona128a932020-09-23 11:03:57 -07001046 msr_autostore_slot = vmx_find_loadstore_msr_slot(autostore, msr_index);
1047 in_autostore_list = msr_autostore_slot >= 0;
Aaron Lewis662f1d12019-11-07 21:14:39 -08001048 in_vmcs12_store_list = nested_msr_store_list_has_msr(vcpu, msr_index);
1049
1050 if (in_vmcs12_store_list && !in_autostore_list) {
Sean Christophersonce833b22020-09-23 11:03:56 -07001051 if (autostore->nr == MAX_NR_LOADSTORE_MSRS) {
Aaron Lewis662f1d12019-11-07 21:14:39 -08001052 /*
1053 * Emulated VMEntry does not fail here. Instead a less
1054 * accurate value will be returned by
1055 * nested_vmx_get_vmexit_msr_value() using kvm_get_msr()
1056 * instead of reading the value from the vmcs02 VMExit
1057 * MSR-store area.
1058 */
1059 pr_warn_ratelimited(
1060 "Not enough msr entries in msr_autostore. Can't add msr %x\n",
1061 msr_index);
1062 return;
1063 }
1064 last = autostore->nr++;
1065 autostore->val[last].index = msr_index;
1066 } else if (!in_vmcs12_store_list && in_autostore_list) {
1067 last = --autostore->nr;
Sean Christophersona128a932020-09-23 11:03:57 -07001068 autostore->val[msr_autostore_slot] = autostore->val[last];
Aaron Lewis662f1d12019-11-07 21:14:39 -08001069 }
1070}
1071
Sean Christopherson55d23752018-12-03 13:53:18 -08001072/*
Sean Christophersonea79a752020-02-04 07:32:59 -08001073 * Load guest's/host's cr3 at nested entry/exit. @nested_ept is true if we are
1074 * emulating VM-Entry into a guest with EPT enabled. On failure, the expected
1075 * Exit Qualification (for a VM-Entry consistency check VM-Exit) is assigned to
1076 * @entry_failure_code.
Sean Christopherson55d23752018-12-03 13:53:18 -08001077 */
Maxim Levitsky0f857222021-06-07 12:02:00 +03001078static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,
1079 bool nested_ept, bool reload_pdptrs,
Sean Christopherson68cda402020-05-11 15:05:29 -07001080 enum vm_entry_failure_code *entry_failure_code)
Sean Christopherson55d23752018-12-03 13:53:18 -08001081{
Sean Christopherson636e8b72021-02-03 16:01:10 -08001082 if (CC(kvm_vcpu_is_illegal_gpa(vcpu, cr3))) {
Sean Christopherson0cc69202020-05-01 21:32:26 -07001083 *entry_failure_code = ENTRY_FAIL_DEFAULT;
1084 return -EINVAL;
1085 }
Sean Christopherson55d23752018-12-03 13:53:18 -08001086
Sean Christopherson0cc69202020-05-01 21:32:26 -07001087 /*
1088 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
1089 * must not be dereferenced.
1090 */
Maxim Levitsky0f857222021-06-07 12:02:00 +03001091 if (reload_pdptrs && !nested_ept && is_pae_paging(vcpu) &&
Sean Christophersonbcb72d02021-06-07 12:01:56 +03001092 CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))) {
1093 *entry_failure_code = ENTRY_FAIL_PDPTE;
1094 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08001095 }
1096
Sean Christopherson50a41792021-06-09 16:42:28 -07001097 if (!nested_ept)
Sean Christophersonb5129102021-06-09 16:42:27 -07001098 kvm_mmu_new_pgd(vcpu, cr3);
Sean Christopherson07ffaf32021-06-09 16:42:21 -07001099
Sean Christopherson55d23752018-12-03 13:53:18 -08001100 vcpu->arch.cr3 = cr3;
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07001101 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Sean Christopherson55d23752018-12-03 13:53:18 -08001102
Sean Christopherson616007c2021-06-22 10:57:34 -07001103 /* Re-initialize the MMU, e.g. to pick up CR4 MMU role changes. */
Sean Christophersonc9060662021-06-09 16:42:33 -07001104 kvm_init_mmu(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08001105
1106 return 0;
1107}
1108
1109/*
1110 * Returns if KVM is able to config CPU to tag TLB entries
1111 * populated by L2 differently than TLB entries populated
1112 * by L1.
1113 *
Liran Alon992edea2019-11-20 14:24:52 +02001114 * If L0 uses EPT, L1 and L2 run with different EPTP because
1115 * guest_mode is part of kvm_mmu_page_role. Thus, TLB entries
1116 * are tagged with different EPTP.
Sean Christopherson55d23752018-12-03 13:53:18 -08001117 *
1118 * If L1 uses VPID and we allocated a vpid02, TLB entries are tagged
1119 * with different VPID (L1 entries are tagged with vmx->vpid
1120 * while L2 entries are tagged with vmx->nested.vpid02).
1121 */
1122static bool nested_has_guest_tlb_tag(struct kvm_vcpu *vcpu)
1123{
1124 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1125
Liran Alon992edea2019-11-20 14:24:52 +02001126 return enable_ept ||
Sean Christopherson55d23752018-12-03 13:53:18 -08001127 (nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02);
1128}
1129
Sean Christopherson50b265a2020-03-20 14:28:19 -07001130static void nested_vmx_transition_tlb_flush(struct kvm_vcpu *vcpu,
1131 struct vmcs12 *vmcs12,
1132 bool is_vmenter)
1133{
1134 struct vcpu_vmx *vmx = to_vmx(vcpu);
1135
1136 /*
Sean Christopherson50a41792021-06-09 16:42:28 -07001137 * If vmcs12 doesn't use VPID, L1 expects linear and combined mappings
1138 * for *all* contexts to be flushed on VM-Enter/VM-Exit, i.e. it's a
1139 * full TLB flush from the guest's perspective. This is required even
1140 * if VPID is disabled in the host as KVM may need to synchronize the
1141 * MMU in response to the guest TLB flush.
1142 *
1143 * Note, using TLB_FLUSH_GUEST is correct even if nested EPT is in use.
1144 * EPT is a special snowflake, as guest-physical mappings aren't
1145 * flushed on VPID invalidations, including VM-Enter or VM-Exit with
1146 * VPID disabled. As a result, KVM _never_ needs to sync nEPT
1147 * entries on VM-Enter because L1 can't rely on VM-Enter to flush
1148 * those mappings.
Sean Christopherson50b265a2020-03-20 14:28:19 -07001149 */
Sean Christopherson50a41792021-06-09 16:42:28 -07001150 if (!nested_cpu_has_vpid(vmcs12)) {
1151 kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu);
Sean Christopherson50b265a2020-03-20 14:28:19 -07001152 return;
Sean Christopherson50a41792021-06-09 16:42:28 -07001153 }
1154
1155 /* L2 should never have a VPID if VPID is disabled. */
1156 WARN_ON(!enable_vpid);
Sean Christopherson50b265a2020-03-20 14:28:19 -07001157
1158 /*
Sean Christopherson50b265a2020-03-20 14:28:19 -07001159 * If VPID is enabled and used by vmc12, but L2 does not have a unique
1160 * TLB tag (ASID), i.e. EPT is disabled and KVM was unable to allocate
Sean Christophersonc51e1ff2020-03-20 14:28:22 -07001161 * a VPID for L2, flush the current context as the effective ASID is
1162 * common to both L1 and L2.
Sean Christopherson50b265a2020-03-20 14:28:19 -07001163 *
1164 * Defer the flush so that it runs after vmcs02.EPTP has been set by
1165 * KVM_REQ_LOAD_MMU_PGD (if nested EPT is enabled) and to avoid
1166 * redundant flushes further down the nested pipeline.
1167 *
1168 * If a TLB flush isn't required due to any of the above, and vpid12 is
1169 * changing then the new "virtual" VPID (vpid12) will reuse the same
Sean Christopherson50a41792021-06-09 16:42:28 -07001170 * "real" VPID (vpid02), and so needs to be flushed. There's no direct
Sean Christopherson50b265a2020-03-20 14:28:19 -07001171 * mapping between vpid02 and vpid12, vpid02 is per-vCPU and reused for
Sean Christopherson50a41792021-06-09 16:42:28 -07001172 * all nested vCPUs. Remember, a flush on VM-Enter does not invalidate
1173 * guest-physical mappings, so there is no need to sync the nEPT MMU.
Sean Christopherson50b265a2020-03-20 14:28:19 -07001174 */
Sean Christopherson50a41792021-06-09 16:42:28 -07001175 if (!nested_has_guest_tlb_tag(vcpu)) {
Sean Christophersonc51e1ff2020-03-20 14:28:22 -07001176 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
Sean Christopherson50b265a2020-03-20 14:28:19 -07001177 } else if (is_vmenter &&
1178 vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
1179 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
1180 vpid_sync_context(nested_get_vpid02(vcpu));
1181 }
1182}
1183
Sean Christopherson55d23752018-12-03 13:53:18 -08001184static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
1185{
1186 superset &= mask;
1187 subset &= mask;
1188
1189 return (superset | subset) == superset;
1190}
1191
1192static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
1193{
1194 const u64 feature_and_reserved =
1195 /* feature (except bit 48; see below) */
1196 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
1197 /* reserved */
1198 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
1199 u64 vmx_basic = vmx->nested.msrs.basic;
1200
1201 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
1202 return -EINVAL;
1203
1204 /*
1205 * KVM does not emulate a version of VMX that constrains physical
1206 * addresses of VMX structures (e.g. VMCS) to 32-bits.
1207 */
1208 if (data & BIT_ULL(48))
1209 return -EINVAL;
1210
1211 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
1212 vmx_basic_vmcs_revision_id(data))
1213 return -EINVAL;
1214
1215 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
1216 return -EINVAL;
1217
1218 vmx->nested.msrs.basic = data;
1219 return 0;
1220}
1221
1222static int
1223vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
1224{
1225 u64 supported;
1226 u32 *lowp, *highp;
1227
1228 switch (msr_index) {
1229 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
1230 lowp = &vmx->nested.msrs.pinbased_ctls_low;
1231 highp = &vmx->nested.msrs.pinbased_ctls_high;
1232 break;
1233 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
1234 lowp = &vmx->nested.msrs.procbased_ctls_low;
1235 highp = &vmx->nested.msrs.procbased_ctls_high;
1236 break;
1237 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
1238 lowp = &vmx->nested.msrs.exit_ctls_low;
1239 highp = &vmx->nested.msrs.exit_ctls_high;
1240 break;
1241 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
1242 lowp = &vmx->nested.msrs.entry_ctls_low;
1243 highp = &vmx->nested.msrs.entry_ctls_high;
1244 break;
1245 case MSR_IA32_VMX_PROCBASED_CTLS2:
1246 lowp = &vmx->nested.msrs.secondary_ctls_low;
1247 highp = &vmx->nested.msrs.secondary_ctls_high;
1248 break;
1249 default:
1250 BUG();
1251 }
1252
1253 supported = vmx_control_msr(*lowp, *highp);
1254
1255 /* Check must-be-1 bits are still 1. */
1256 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
1257 return -EINVAL;
1258
1259 /* Check must-be-0 bits are still 0. */
1260 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
1261 return -EINVAL;
1262
1263 *lowp = data;
1264 *highp = data >> 32;
1265 return 0;
1266}
1267
1268static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
1269{
1270 const u64 feature_and_reserved_bits =
1271 /* feature */
1272 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
1273 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
1274 /* reserved */
1275 GENMASK_ULL(13, 9) | BIT_ULL(31);
1276 u64 vmx_misc;
1277
1278 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
1279 vmx->nested.msrs.misc_high);
1280
1281 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
1282 return -EINVAL;
1283
1284 if ((vmx->nested.msrs.pinbased_ctls_high &
1285 PIN_BASED_VMX_PREEMPTION_TIMER) &&
1286 vmx_misc_preemption_timer_rate(data) !=
1287 vmx_misc_preemption_timer_rate(vmx_misc))
1288 return -EINVAL;
1289
1290 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
1291 return -EINVAL;
1292
1293 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
1294 return -EINVAL;
1295
1296 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
1297 return -EINVAL;
1298
1299 vmx->nested.msrs.misc_low = data;
1300 vmx->nested.msrs.misc_high = data >> 32;
1301
Sean Christopherson55d23752018-12-03 13:53:18 -08001302 return 0;
1303}
1304
1305static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
1306{
1307 u64 vmx_ept_vpid_cap;
1308
1309 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
1310 vmx->nested.msrs.vpid_caps);
1311
1312 /* Every bit is either reserved or a feature bit. */
1313 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
1314 return -EINVAL;
1315
1316 vmx->nested.msrs.ept_caps = data;
1317 vmx->nested.msrs.vpid_caps = data >> 32;
1318 return 0;
1319}
1320
1321static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
1322{
1323 u64 *msr;
1324
1325 switch (msr_index) {
1326 case MSR_IA32_VMX_CR0_FIXED0:
1327 msr = &vmx->nested.msrs.cr0_fixed0;
1328 break;
1329 case MSR_IA32_VMX_CR4_FIXED0:
1330 msr = &vmx->nested.msrs.cr4_fixed0;
1331 break;
1332 default:
1333 BUG();
1334 }
1335
1336 /*
1337 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
1338 * must be 1 in the restored value.
1339 */
1340 if (!is_bitwise_subset(data, *msr, -1ULL))
1341 return -EINVAL;
1342
1343 *msr = data;
1344 return 0;
1345}
1346
1347/*
1348 * Called when userspace is restoring VMX MSRs.
1349 *
1350 * Returns 0 on success, non-0 otherwise.
1351 */
1352int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1353{
1354 struct vcpu_vmx *vmx = to_vmx(vcpu);
1355
1356 /*
1357 * Don't allow changes to the VMX capability MSRs while the vCPU
1358 * is in VMX operation.
1359 */
1360 if (vmx->nested.vmxon)
1361 return -EBUSY;
1362
1363 switch (msr_index) {
1364 case MSR_IA32_VMX_BASIC:
1365 return vmx_restore_vmx_basic(vmx, data);
1366 case MSR_IA32_VMX_PINBASED_CTLS:
1367 case MSR_IA32_VMX_PROCBASED_CTLS:
1368 case MSR_IA32_VMX_EXIT_CTLS:
1369 case MSR_IA32_VMX_ENTRY_CTLS:
1370 /*
1371 * The "non-true" VMX capability MSRs are generated from the
1372 * "true" MSRs, so we do not support restoring them directly.
1373 *
1374 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
1375 * should restore the "true" MSRs with the must-be-1 bits
1376 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
1377 * DEFAULT SETTINGS".
1378 */
1379 return -EINVAL;
1380 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
1381 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
1382 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
1383 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
1384 case MSR_IA32_VMX_PROCBASED_CTLS2:
1385 return vmx_restore_control_msr(vmx, msr_index, data);
1386 case MSR_IA32_VMX_MISC:
1387 return vmx_restore_vmx_misc(vmx, data);
1388 case MSR_IA32_VMX_CR0_FIXED0:
1389 case MSR_IA32_VMX_CR4_FIXED0:
1390 return vmx_restore_fixed0_msr(vmx, msr_index, data);
1391 case MSR_IA32_VMX_CR0_FIXED1:
1392 case MSR_IA32_VMX_CR4_FIXED1:
1393 /*
1394 * These MSRs are generated based on the vCPU's CPUID, so we
1395 * do not support restoring them directly.
1396 */
1397 return -EINVAL;
1398 case MSR_IA32_VMX_EPT_VPID_CAP:
1399 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
1400 case MSR_IA32_VMX_VMCS_ENUM:
1401 vmx->nested.msrs.vmcs_enum = data;
1402 return 0;
Paolo Bonzinie8a70bd2019-07-02 14:40:40 +02001403 case MSR_IA32_VMX_VMFUNC:
1404 if (data & ~vmx->nested.msrs.vmfunc_controls)
1405 return -EINVAL;
1406 vmx->nested.msrs.vmfunc_controls = data;
1407 return 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08001408 default:
1409 /*
1410 * The rest of the VMX capability MSRs do not support restore.
1411 */
1412 return -EINVAL;
1413 }
1414}
1415
1416/* Returns 0 on success, non-0 otherwise. */
1417int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
1418{
1419 switch (msr_index) {
1420 case MSR_IA32_VMX_BASIC:
1421 *pdata = msrs->basic;
1422 break;
1423 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
1424 case MSR_IA32_VMX_PINBASED_CTLS:
1425 *pdata = vmx_control_msr(
1426 msrs->pinbased_ctls_low,
1427 msrs->pinbased_ctls_high);
1428 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
1429 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
1430 break;
1431 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
1432 case MSR_IA32_VMX_PROCBASED_CTLS:
1433 *pdata = vmx_control_msr(
1434 msrs->procbased_ctls_low,
1435 msrs->procbased_ctls_high);
1436 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
1437 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
1438 break;
1439 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
1440 case MSR_IA32_VMX_EXIT_CTLS:
1441 *pdata = vmx_control_msr(
1442 msrs->exit_ctls_low,
1443 msrs->exit_ctls_high);
1444 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
1445 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
1446 break;
1447 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
1448 case MSR_IA32_VMX_ENTRY_CTLS:
1449 *pdata = vmx_control_msr(
1450 msrs->entry_ctls_low,
1451 msrs->entry_ctls_high);
1452 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
1453 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
1454 break;
1455 case MSR_IA32_VMX_MISC:
1456 *pdata = vmx_control_msr(
1457 msrs->misc_low,
1458 msrs->misc_high);
1459 break;
1460 case MSR_IA32_VMX_CR0_FIXED0:
1461 *pdata = msrs->cr0_fixed0;
1462 break;
1463 case MSR_IA32_VMX_CR0_FIXED1:
1464 *pdata = msrs->cr0_fixed1;
1465 break;
1466 case MSR_IA32_VMX_CR4_FIXED0:
1467 *pdata = msrs->cr4_fixed0;
1468 break;
1469 case MSR_IA32_VMX_CR4_FIXED1:
1470 *pdata = msrs->cr4_fixed1;
1471 break;
1472 case MSR_IA32_VMX_VMCS_ENUM:
1473 *pdata = msrs->vmcs_enum;
1474 break;
1475 case MSR_IA32_VMX_PROCBASED_CTLS2:
1476 *pdata = vmx_control_msr(
1477 msrs->secondary_ctls_low,
1478 msrs->secondary_ctls_high);
1479 break;
1480 case MSR_IA32_VMX_EPT_VPID_CAP:
1481 *pdata = msrs->ept_caps |
1482 ((u64)msrs->vpid_caps << 32);
1483 break;
1484 case MSR_IA32_VMX_VMFUNC:
1485 *pdata = msrs->vmfunc_controls;
1486 break;
1487 default:
1488 return 1;
1489 }
1490
1491 return 0;
1492}
1493
1494/*
Sean Christophersonfadcead2019-05-07 08:36:23 -07001495 * Copy the writable VMCS shadow fields back to the VMCS12, in case they have
1496 * been modified by the L1 guest. Note, "writable" in this context means
1497 * "writable by the guest", i.e. tagged SHADOW_FIELD_RW; the set of
1498 * fields tagged SHADOW_FIELD_RO may or may not align with the "read-only"
1499 * VM-exit information fields (which are actually writable if the vCPU is
1500 * configured to support "VMWRITE to any supported field in the VMCS").
Sean Christopherson55d23752018-12-03 13:53:18 -08001501 */
1502static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
1503{
Sean Christopherson55d23752018-12-03 13:53:18 -08001504 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Sean Christophersonfadcead2019-05-07 08:36:23 -07001505 struct vmcs12 *vmcs12 = get_vmcs12(&vmx->vcpu);
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07001506 struct shadow_vmcs_field field;
1507 unsigned long val;
Sean Christophersonfadcead2019-05-07 08:36:23 -07001508 int i;
Sean Christopherson55d23752018-12-03 13:53:18 -08001509
Paolo Bonzini88dddc12019-07-19 18:41:10 +02001510 if (WARN_ON(!shadow_vmcs))
1511 return;
1512
Sean Christopherson55d23752018-12-03 13:53:18 -08001513 preempt_disable();
1514
1515 vmcs_load(shadow_vmcs);
1516
Sean Christophersonfadcead2019-05-07 08:36:23 -07001517 for (i = 0; i < max_shadow_read_write_fields; i++) {
1518 field = shadow_read_write_fields[i];
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07001519 val = __vmcs_readl(field.encoding);
1520 vmcs12_write_any(vmcs12, field.encoding, field.offset, val);
Sean Christopherson55d23752018-12-03 13:53:18 -08001521 }
1522
1523 vmcs_clear(shadow_vmcs);
1524 vmcs_load(vmx->loaded_vmcs->vmcs);
1525
1526 preempt_enable();
1527}
1528
1529static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
1530{
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07001531 const struct shadow_vmcs_field *fields[] = {
Sean Christopherson55d23752018-12-03 13:53:18 -08001532 shadow_read_write_fields,
1533 shadow_read_only_fields
1534 };
1535 const int max_fields[] = {
1536 max_shadow_read_write_fields,
1537 max_shadow_read_only_fields
1538 };
Sean Christopherson55d23752018-12-03 13:53:18 -08001539 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07001540 struct vmcs12 *vmcs12 = get_vmcs12(&vmx->vcpu);
1541 struct shadow_vmcs_field field;
1542 unsigned long val;
1543 int i, q;
Sean Christopherson55d23752018-12-03 13:53:18 -08001544
Paolo Bonzini88dddc12019-07-19 18:41:10 +02001545 if (WARN_ON(!shadow_vmcs))
1546 return;
1547
Sean Christopherson55d23752018-12-03 13:53:18 -08001548 vmcs_load(shadow_vmcs);
1549
1550 for (q = 0; q < ARRAY_SIZE(fields); q++) {
1551 for (i = 0; i < max_fields[q]; i++) {
1552 field = fields[q][i];
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07001553 val = vmcs12_read_any(vmcs12, field.encoding,
1554 field.offset);
1555 __vmcs_writel(field.encoding, val);
Sean Christopherson55d23752018-12-03 13:53:18 -08001556 }
1557 }
1558
1559 vmcs_clear(shadow_vmcs);
1560 vmcs_load(vmx->loaded_vmcs->vmcs);
1561}
1562
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001563static void copy_enlightened_to_vmcs12(struct vcpu_vmx *vmx, u32 hv_clean_fields)
Sean Christopherson55d23752018-12-03 13:53:18 -08001564{
1565 struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12;
1566 struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
1567
1568 /* HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE */
1569 vmcs12->tpr_threshold = evmcs->tpr_threshold;
1570 vmcs12->guest_rip = evmcs->guest_rip;
1571
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001572 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001573 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC))) {
1574 vmcs12->guest_rsp = evmcs->guest_rsp;
1575 vmcs12->guest_rflags = evmcs->guest_rflags;
1576 vmcs12->guest_interruptibility_info =
1577 evmcs->guest_interruptibility_info;
1578 }
1579
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001580 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001581 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC))) {
1582 vmcs12->cpu_based_vm_exec_control =
1583 evmcs->cpu_based_vm_exec_control;
1584 }
1585
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001586 if (unlikely(!(hv_clean_fields &
Vitaly Kuznetsovf9bc5222019-06-13 13:35:02 +02001587 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EXCPN))) {
Sean Christopherson55d23752018-12-03 13:53:18 -08001588 vmcs12->exception_bitmap = evmcs->exception_bitmap;
1589 }
1590
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001591 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001592 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_ENTRY))) {
1593 vmcs12->vm_entry_controls = evmcs->vm_entry_controls;
1594 }
1595
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001596 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001597 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT))) {
1598 vmcs12->vm_entry_intr_info_field =
1599 evmcs->vm_entry_intr_info_field;
1600 vmcs12->vm_entry_exception_error_code =
1601 evmcs->vm_entry_exception_error_code;
1602 vmcs12->vm_entry_instruction_len =
1603 evmcs->vm_entry_instruction_len;
1604 }
1605
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001606 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001607 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1))) {
1608 vmcs12->host_ia32_pat = evmcs->host_ia32_pat;
1609 vmcs12->host_ia32_efer = evmcs->host_ia32_efer;
1610 vmcs12->host_cr0 = evmcs->host_cr0;
1611 vmcs12->host_cr3 = evmcs->host_cr3;
1612 vmcs12->host_cr4 = evmcs->host_cr4;
1613 vmcs12->host_ia32_sysenter_esp = evmcs->host_ia32_sysenter_esp;
1614 vmcs12->host_ia32_sysenter_eip = evmcs->host_ia32_sysenter_eip;
1615 vmcs12->host_rip = evmcs->host_rip;
1616 vmcs12->host_ia32_sysenter_cs = evmcs->host_ia32_sysenter_cs;
1617 vmcs12->host_es_selector = evmcs->host_es_selector;
1618 vmcs12->host_cs_selector = evmcs->host_cs_selector;
1619 vmcs12->host_ss_selector = evmcs->host_ss_selector;
1620 vmcs12->host_ds_selector = evmcs->host_ds_selector;
1621 vmcs12->host_fs_selector = evmcs->host_fs_selector;
1622 vmcs12->host_gs_selector = evmcs->host_gs_selector;
1623 vmcs12->host_tr_selector = evmcs->host_tr_selector;
1624 }
1625
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001626 if (unlikely(!(hv_clean_fields &
Vitaly Kuznetsovf9bc5222019-06-13 13:35:02 +02001627 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1))) {
Sean Christopherson55d23752018-12-03 13:53:18 -08001628 vmcs12->pin_based_vm_exec_control =
1629 evmcs->pin_based_vm_exec_control;
1630 vmcs12->vm_exit_controls = evmcs->vm_exit_controls;
1631 vmcs12->secondary_vm_exec_control =
1632 evmcs->secondary_vm_exec_control;
1633 }
1634
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001635 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001636 HV_VMX_ENLIGHTENED_CLEAN_FIELD_IO_BITMAP))) {
1637 vmcs12->io_bitmap_a = evmcs->io_bitmap_a;
1638 vmcs12->io_bitmap_b = evmcs->io_bitmap_b;
1639 }
1640
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001641 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001642 HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP))) {
1643 vmcs12->msr_bitmap = evmcs->msr_bitmap;
1644 }
1645
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001646 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001647 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2))) {
1648 vmcs12->guest_es_base = evmcs->guest_es_base;
1649 vmcs12->guest_cs_base = evmcs->guest_cs_base;
1650 vmcs12->guest_ss_base = evmcs->guest_ss_base;
1651 vmcs12->guest_ds_base = evmcs->guest_ds_base;
1652 vmcs12->guest_fs_base = evmcs->guest_fs_base;
1653 vmcs12->guest_gs_base = evmcs->guest_gs_base;
1654 vmcs12->guest_ldtr_base = evmcs->guest_ldtr_base;
1655 vmcs12->guest_tr_base = evmcs->guest_tr_base;
1656 vmcs12->guest_gdtr_base = evmcs->guest_gdtr_base;
1657 vmcs12->guest_idtr_base = evmcs->guest_idtr_base;
1658 vmcs12->guest_es_limit = evmcs->guest_es_limit;
1659 vmcs12->guest_cs_limit = evmcs->guest_cs_limit;
1660 vmcs12->guest_ss_limit = evmcs->guest_ss_limit;
1661 vmcs12->guest_ds_limit = evmcs->guest_ds_limit;
1662 vmcs12->guest_fs_limit = evmcs->guest_fs_limit;
1663 vmcs12->guest_gs_limit = evmcs->guest_gs_limit;
1664 vmcs12->guest_ldtr_limit = evmcs->guest_ldtr_limit;
1665 vmcs12->guest_tr_limit = evmcs->guest_tr_limit;
1666 vmcs12->guest_gdtr_limit = evmcs->guest_gdtr_limit;
1667 vmcs12->guest_idtr_limit = evmcs->guest_idtr_limit;
1668 vmcs12->guest_es_ar_bytes = evmcs->guest_es_ar_bytes;
1669 vmcs12->guest_cs_ar_bytes = evmcs->guest_cs_ar_bytes;
1670 vmcs12->guest_ss_ar_bytes = evmcs->guest_ss_ar_bytes;
1671 vmcs12->guest_ds_ar_bytes = evmcs->guest_ds_ar_bytes;
1672 vmcs12->guest_fs_ar_bytes = evmcs->guest_fs_ar_bytes;
1673 vmcs12->guest_gs_ar_bytes = evmcs->guest_gs_ar_bytes;
1674 vmcs12->guest_ldtr_ar_bytes = evmcs->guest_ldtr_ar_bytes;
1675 vmcs12->guest_tr_ar_bytes = evmcs->guest_tr_ar_bytes;
1676 vmcs12->guest_es_selector = evmcs->guest_es_selector;
1677 vmcs12->guest_cs_selector = evmcs->guest_cs_selector;
1678 vmcs12->guest_ss_selector = evmcs->guest_ss_selector;
1679 vmcs12->guest_ds_selector = evmcs->guest_ds_selector;
1680 vmcs12->guest_fs_selector = evmcs->guest_fs_selector;
1681 vmcs12->guest_gs_selector = evmcs->guest_gs_selector;
1682 vmcs12->guest_ldtr_selector = evmcs->guest_ldtr_selector;
1683 vmcs12->guest_tr_selector = evmcs->guest_tr_selector;
1684 }
1685
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001686 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001687 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2))) {
1688 vmcs12->tsc_offset = evmcs->tsc_offset;
1689 vmcs12->virtual_apic_page_addr = evmcs->virtual_apic_page_addr;
1690 vmcs12->xss_exit_bitmap = evmcs->xss_exit_bitmap;
1691 }
1692
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001693 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001694 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR))) {
1695 vmcs12->cr0_guest_host_mask = evmcs->cr0_guest_host_mask;
1696 vmcs12->cr4_guest_host_mask = evmcs->cr4_guest_host_mask;
1697 vmcs12->cr0_read_shadow = evmcs->cr0_read_shadow;
1698 vmcs12->cr4_read_shadow = evmcs->cr4_read_shadow;
1699 vmcs12->guest_cr0 = evmcs->guest_cr0;
1700 vmcs12->guest_cr3 = evmcs->guest_cr3;
1701 vmcs12->guest_cr4 = evmcs->guest_cr4;
1702 vmcs12->guest_dr7 = evmcs->guest_dr7;
1703 }
1704
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001705 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001706 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER))) {
1707 vmcs12->host_fs_base = evmcs->host_fs_base;
1708 vmcs12->host_gs_base = evmcs->host_gs_base;
1709 vmcs12->host_tr_base = evmcs->host_tr_base;
1710 vmcs12->host_gdtr_base = evmcs->host_gdtr_base;
1711 vmcs12->host_idtr_base = evmcs->host_idtr_base;
1712 vmcs12->host_rsp = evmcs->host_rsp;
1713 }
1714
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001715 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001716 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_XLAT))) {
1717 vmcs12->ept_pointer = evmcs->ept_pointer;
1718 vmcs12->virtual_processor_id = evmcs->virtual_processor_id;
1719 }
1720
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001721 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001722 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1))) {
1723 vmcs12->vmcs_link_pointer = evmcs->vmcs_link_pointer;
1724 vmcs12->guest_ia32_debugctl = evmcs->guest_ia32_debugctl;
1725 vmcs12->guest_ia32_pat = evmcs->guest_ia32_pat;
1726 vmcs12->guest_ia32_efer = evmcs->guest_ia32_efer;
1727 vmcs12->guest_pdptr0 = evmcs->guest_pdptr0;
1728 vmcs12->guest_pdptr1 = evmcs->guest_pdptr1;
1729 vmcs12->guest_pdptr2 = evmcs->guest_pdptr2;
1730 vmcs12->guest_pdptr3 = evmcs->guest_pdptr3;
1731 vmcs12->guest_pending_dbg_exceptions =
1732 evmcs->guest_pending_dbg_exceptions;
1733 vmcs12->guest_sysenter_esp = evmcs->guest_sysenter_esp;
1734 vmcs12->guest_sysenter_eip = evmcs->guest_sysenter_eip;
1735 vmcs12->guest_bndcfgs = evmcs->guest_bndcfgs;
1736 vmcs12->guest_activity_state = evmcs->guest_activity_state;
1737 vmcs12->guest_sysenter_cs = evmcs->guest_sysenter_cs;
1738 }
1739
1740 /*
1741 * Not used?
1742 * vmcs12->vm_exit_msr_store_addr = evmcs->vm_exit_msr_store_addr;
1743 * vmcs12->vm_exit_msr_load_addr = evmcs->vm_exit_msr_load_addr;
1744 * vmcs12->vm_entry_msr_load_addr = evmcs->vm_entry_msr_load_addr;
Sean Christopherson55d23752018-12-03 13:53:18 -08001745 * vmcs12->page_fault_error_code_mask =
1746 * evmcs->page_fault_error_code_mask;
1747 * vmcs12->page_fault_error_code_match =
1748 * evmcs->page_fault_error_code_match;
1749 * vmcs12->cr3_target_count = evmcs->cr3_target_count;
1750 * vmcs12->vm_exit_msr_store_count = evmcs->vm_exit_msr_store_count;
1751 * vmcs12->vm_exit_msr_load_count = evmcs->vm_exit_msr_load_count;
1752 * vmcs12->vm_entry_msr_load_count = evmcs->vm_entry_msr_load_count;
1753 */
1754
1755 /*
1756 * Read only fields:
1757 * vmcs12->guest_physical_address = evmcs->guest_physical_address;
1758 * vmcs12->vm_instruction_error = evmcs->vm_instruction_error;
1759 * vmcs12->vm_exit_reason = evmcs->vm_exit_reason;
1760 * vmcs12->vm_exit_intr_info = evmcs->vm_exit_intr_info;
1761 * vmcs12->vm_exit_intr_error_code = evmcs->vm_exit_intr_error_code;
1762 * vmcs12->idt_vectoring_info_field = evmcs->idt_vectoring_info_field;
1763 * vmcs12->idt_vectoring_error_code = evmcs->idt_vectoring_error_code;
1764 * vmcs12->vm_exit_instruction_len = evmcs->vm_exit_instruction_len;
1765 * vmcs12->vmx_instruction_info = evmcs->vmx_instruction_info;
1766 * vmcs12->exit_qualification = evmcs->exit_qualification;
1767 * vmcs12->guest_linear_address = evmcs->guest_linear_address;
1768 *
1769 * Not present in struct vmcs12:
1770 * vmcs12->exit_io_instruction_ecx = evmcs->exit_io_instruction_ecx;
1771 * vmcs12->exit_io_instruction_esi = evmcs->exit_io_instruction_esi;
1772 * vmcs12->exit_io_instruction_edi = evmcs->exit_io_instruction_edi;
1773 * vmcs12->exit_io_instruction_eip = evmcs->exit_io_instruction_eip;
1774 */
1775
Vitaly Kuznetsov25641ca2021-05-26 15:20:19 +02001776 return;
Sean Christopherson55d23752018-12-03 13:53:18 -08001777}
1778
Vitaly Kuznetsov25641ca2021-05-26 15:20:19 +02001779static void copy_vmcs12_to_enlightened(struct vcpu_vmx *vmx)
Sean Christopherson55d23752018-12-03 13:53:18 -08001780{
1781 struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12;
1782 struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
1783
1784 /*
1785 * Should not be changed by KVM:
1786 *
1787 * evmcs->host_es_selector = vmcs12->host_es_selector;
1788 * evmcs->host_cs_selector = vmcs12->host_cs_selector;
1789 * evmcs->host_ss_selector = vmcs12->host_ss_selector;
1790 * evmcs->host_ds_selector = vmcs12->host_ds_selector;
1791 * evmcs->host_fs_selector = vmcs12->host_fs_selector;
1792 * evmcs->host_gs_selector = vmcs12->host_gs_selector;
1793 * evmcs->host_tr_selector = vmcs12->host_tr_selector;
1794 * evmcs->host_ia32_pat = vmcs12->host_ia32_pat;
1795 * evmcs->host_ia32_efer = vmcs12->host_ia32_efer;
1796 * evmcs->host_cr0 = vmcs12->host_cr0;
1797 * evmcs->host_cr3 = vmcs12->host_cr3;
1798 * evmcs->host_cr4 = vmcs12->host_cr4;
1799 * evmcs->host_ia32_sysenter_esp = vmcs12->host_ia32_sysenter_esp;
1800 * evmcs->host_ia32_sysenter_eip = vmcs12->host_ia32_sysenter_eip;
1801 * evmcs->host_rip = vmcs12->host_rip;
1802 * evmcs->host_ia32_sysenter_cs = vmcs12->host_ia32_sysenter_cs;
1803 * evmcs->host_fs_base = vmcs12->host_fs_base;
1804 * evmcs->host_gs_base = vmcs12->host_gs_base;
1805 * evmcs->host_tr_base = vmcs12->host_tr_base;
1806 * evmcs->host_gdtr_base = vmcs12->host_gdtr_base;
1807 * evmcs->host_idtr_base = vmcs12->host_idtr_base;
1808 * evmcs->host_rsp = vmcs12->host_rsp;
Sean Christopherson3731905ef2019-05-07 08:36:27 -07001809 * sync_vmcs02_to_vmcs12() doesn't read these:
Sean Christopherson55d23752018-12-03 13:53:18 -08001810 * evmcs->io_bitmap_a = vmcs12->io_bitmap_a;
1811 * evmcs->io_bitmap_b = vmcs12->io_bitmap_b;
1812 * evmcs->msr_bitmap = vmcs12->msr_bitmap;
1813 * evmcs->ept_pointer = vmcs12->ept_pointer;
1814 * evmcs->xss_exit_bitmap = vmcs12->xss_exit_bitmap;
1815 * evmcs->vm_exit_msr_store_addr = vmcs12->vm_exit_msr_store_addr;
1816 * evmcs->vm_exit_msr_load_addr = vmcs12->vm_exit_msr_load_addr;
1817 * evmcs->vm_entry_msr_load_addr = vmcs12->vm_entry_msr_load_addr;
Sean Christopherson55d23752018-12-03 13:53:18 -08001818 * evmcs->tpr_threshold = vmcs12->tpr_threshold;
1819 * evmcs->virtual_processor_id = vmcs12->virtual_processor_id;
1820 * evmcs->exception_bitmap = vmcs12->exception_bitmap;
1821 * evmcs->vmcs_link_pointer = vmcs12->vmcs_link_pointer;
1822 * evmcs->pin_based_vm_exec_control = vmcs12->pin_based_vm_exec_control;
1823 * evmcs->vm_exit_controls = vmcs12->vm_exit_controls;
1824 * evmcs->secondary_vm_exec_control = vmcs12->secondary_vm_exec_control;
1825 * evmcs->page_fault_error_code_mask =
1826 * vmcs12->page_fault_error_code_mask;
1827 * evmcs->page_fault_error_code_match =
1828 * vmcs12->page_fault_error_code_match;
1829 * evmcs->cr3_target_count = vmcs12->cr3_target_count;
1830 * evmcs->virtual_apic_page_addr = vmcs12->virtual_apic_page_addr;
1831 * evmcs->tsc_offset = vmcs12->tsc_offset;
1832 * evmcs->guest_ia32_debugctl = vmcs12->guest_ia32_debugctl;
1833 * evmcs->cr0_guest_host_mask = vmcs12->cr0_guest_host_mask;
1834 * evmcs->cr4_guest_host_mask = vmcs12->cr4_guest_host_mask;
1835 * evmcs->cr0_read_shadow = vmcs12->cr0_read_shadow;
1836 * evmcs->cr4_read_shadow = vmcs12->cr4_read_shadow;
1837 * evmcs->vm_exit_msr_store_count = vmcs12->vm_exit_msr_store_count;
1838 * evmcs->vm_exit_msr_load_count = vmcs12->vm_exit_msr_load_count;
1839 * evmcs->vm_entry_msr_load_count = vmcs12->vm_entry_msr_load_count;
1840 *
1841 * Not present in struct vmcs12:
1842 * evmcs->exit_io_instruction_ecx = vmcs12->exit_io_instruction_ecx;
1843 * evmcs->exit_io_instruction_esi = vmcs12->exit_io_instruction_esi;
1844 * evmcs->exit_io_instruction_edi = vmcs12->exit_io_instruction_edi;
1845 * evmcs->exit_io_instruction_eip = vmcs12->exit_io_instruction_eip;
1846 */
1847
1848 evmcs->guest_es_selector = vmcs12->guest_es_selector;
1849 evmcs->guest_cs_selector = vmcs12->guest_cs_selector;
1850 evmcs->guest_ss_selector = vmcs12->guest_ss_selector;
1851 evmcs->guest_ds_selector = vmcs12->guest_ds_selector;
1852 evmcs->guest_fs_selector = vmcs12->guest_fs_selector;
1853 evmcs->guest_gs_selector = vmcs12->guest_gs_selector;
1854 evmcs->guest_ldtr_selector = vmcs12->guest_ldtr_selector;
1855 evmcs->guest_tr_selector = vmcs12->guest_tr_selector;
1856
1857 evmcs->guest_es_limit = vmcs12->guest_es_limit;
1858 evmcs->guest_cs_limit = vmcs12->guest_cs_limit;
1859 evmcs->guest_ss_limit = vmcs12->guest_ss_limit;
1860 evmcs->guest_ds_limit = vmcs12->guest_ds_limit;
1861 evmcs->guest_fs_limit = vmcs12->guest_fs_limit;
1862 evmcs->guest_gs_limit = vmcs12->guest_gs_limit;
1863 evmcs->guest_ldtr_limit = vmcs12->guest_ldtr_limit;
1864 evmcs->guest_tr_limit = vmcs12->guest_tr_limit;
1865 evmcs->guest_gdtr_limit = vmcs12->guest_gdtr_limit;
1866 evmcs->guest_idtr_limit = vmcs12->guest_idtr_limit;
1867
1868 evmcs->guest_es_ar_bytes = vmcs12->guest_es_ar_bytes;
1869 evmcs->guest_cs_ar_bytes = vmcs12->guest_cs_ar_bytes;
1870 evmcs->guest_ss_ar_bytes = vmcs12->guest_ss_ar_bytes;
1871 evmcs->guest_ds_ar_bytes = vmcs12->guest_ds_ar_bytes;
1872 evmcs->guest_fs_ar_bytes = vmcs12->guest_fs_ar_bytes;
1873 evmcs->guest_gs_ar_bytes = vmcs12->guest_gs_ar_bytes;
1874 evmcs->guest_ldtr_ar_bytes = vmcs12->guest_ldtr_ar_bytes;
1875 evmcs->guest_tr_ar_bytes = vmcs12->guest_tr_ar_bytes;
1876
1877 evmcs->guest_es_base = vmcs12->guest_es_base;
1878 evmcs->guest_cs_base = vmcs12->guest_cs_base;
1879 evmcs->guest_ss_base = vmcs12->guest_ss_base;
1880 evmcs->guest_ds_base = vmcs12->guest_ds_base;
1881 evmcs->guest_fs_base = vmcs12->guest_fs_base;
1882 evmcs->guest_gs_base = vmcs12->guest_gs_base;
1883 evmcs->guest_ldtr_base = vmcs12->guest_ldtr_base;
1884 evmcs->guest_tr_base = vmcs12->guest_tr_base;
1885 evmcs->guest_gdtr_base = vmcs12->guest_gdtr_base;
1886 evmcs->guest_idtr_base = vmcs12->guest_idtr_base;
1887
1888 evmcs->guest_ia32_pat = vmcs12->guest_ia32_pat;
1889 evmcs->guest_ia32_efer = vmcs12->guest_ia32_efer;
1890
1891 evmcs->guest_pdptr0 = vmcs12->guest_pdptr0;
1892 evmcs->guest_pdptr1 = vmcs12->guest_pdptr1;
1893 evmcs->guest_pdptr2 = vmcs12->guest_pdptr2;
1894 evmcs->guest_pdptr3 = vmcs12->guest_pdptr3;
1895
1896 evmcs->guest_pending_dbg_exceptions =
1897 vmcs12->guest_pending_dbg_exceptions;
1898 evmcs->guest_sysenter_esp = vmcs12->guest_sysenter_esp;
1899 evmcs->guest_sysenter_eip = vmcs12->guest_sysenter_eip;
1900
1901 evmcs->guest_activity_state = vmcs12->guest_activity_state;
1902 evmcs->guest_sysenter_cs = vmcs12->guest_sysenter_cs;
1903
1904 evmcs->guest_cr0 = vmcs12->guest_cr0;
1905 evmcs->guest_cr3 = vmcs12->guest_cr3;
1906 evmcs->guest_cr4 = vmcs12->guest_cr4;
1907 evmcs->guest_dr7 = vmcs12->guest_dr7;
1908
1909 evmcs->guest_physical_address = vmcs12->guest_physical_address;
1910
1911 evmcs->vm_instruction_error = vmcs12->vm_instruction_error;
1912 evmcs->vm_exit_reason = vmcs12->vm_exit_reason;
1913 evmcs->vm_exit_intr_info = vmcs12->vm_exit_intr_info;
1914 evmcs->vm_exit_intr_error_code = vmcs12->vm_exit_intr_error_code;
1915 evmcs->idt_vectoring_info_field = vmcs12->idt_vectoring_info_field;
1916 evmcs->idt_vectoring_error_code = vmcs12->idt_vectoring_error_code;
1917 evmcs->vm_exit_instruction_len = vmcs12->vm_exit_instruction_len;
1918 evmcs->vmx_instruction_info = vmcs12->vmx_instruction_info;
1919
1920 evmcs->exit_qualification = vmcs12->exit_qualification;
1921
1922 evmcs->guest_linear_address = vmcs12->guest_linear_address;
1923 evmcs->guest_rsp = vmcs12->guest_rsp;
1924 evmcs->guest_rflags = vmcs12->guest_rflags;
1925
1926 evmcs->guest_interruptibility_info =
1927 vmcs12->guest_interruptibility_info;
1928 evmcs->cpu_based_vm_exec_control = vmcs12->cpu_based_vm_exec_control;
1929 evmcs->vm_entry_controls = vmcs12->vm_entry_controls;
1930 evmcs->vm_entry_intr_info_field = vmcs12->vm_entry_intr_info_field;
1931 evmcs->vm_entry_exception_error_code =
1932 vmcs12->vm_entry_exception_error_code;
1933 evmcs->vm_entry_instruction_len = vmcs12->vm_entry_instruction_len;
1934
1935 evmcs->guest_rip = vmcs12->guest_rip;
1936
1937 evmcs->guest_bndcfgs = vmcs12->guest_bndcfgs;
1938
Vitaly Kuznetsov25641ca2021-05-26 15:20:19 +02001939 return;
Sean Christopherson55d23752018-12-03 13:53:18 -08001940}
1941
1942/*
1943 * This is an equivalent of the nested hypervisor executing the vmptrld
1944 * instruction.
1945 */
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01001946static enum nested_evmptrld_status nested_vmx_handle_enlightened_vmptrld(
1947 struct kvm_vcpu *vcpu, bool from_launch)
Sean Christopherson55d23752018-12-03 13:53:18 -08001948{
1949 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsova21a39c2019-06-28 13:23:32 +02001950 bool evmcs_gpa_changed = false;
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02001951 u64 evmcs_gpa;
Sean Christopherson55d23752018-12-03 13:53:18 -08001952
1953 if (likely(!vmx->nested.enlightened_vmcs_enabled))
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01001954 return EVMPTRLD_DISABLED;
Sean Christopherson55d23752018-12-03 13:53:18 -08001955
Vitaly Kuznetsov02761712021-05-26 15:20:18 +02001956 if (!nested_enlightened_vmentry(vcpu, &evmcs_gpa)) {
1957 nested_release_evmcs(vcpu);
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01001958 return EVMPTRLD_DISABLED;
Vitaly Kuznetsov02761712021-05-26 15:20:18 +02001959 }
Sean Christopherson55d23752018-12-03 13:53:18 -08001960
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02001961 if (unlikely(evmcs_gpa != vmx->nested.hv_evmcs_vmptr)) {
Yu Zhang64c78502021-09-30 01:51:53 +08001962 vmx->nested.current_vmptr = INVALID_GPA;
Sean Christopherson55d23752018-12-03 13:53:18 -08001963
1964 nested_release_evmcs(vcpu);
1965
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02001966 if (kvm_vcpu_map(vcpu, gpa_to_gfn(evmcs_gpa),
KarimAllah Ahmeddee9c042019-01-31 21:24:42 +01001967 &vmx->nested.hv_evmcs_map))
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01001968 return EVMPTRLD_ERROR;
Sean Christopherson55d23752018-12-03 13:53:18 -08001969
KarimAllah Ahmeddee9c042019-01-31 21:24:42 +01001970 vmx->nested.hv_evmcs = vmx->nested.hv_evmcs_map.hva;
Sean Christopherson55d23752018-12-03 13:53:18 -08001971
1972 /*
1973 * Currently, KVM only supports eVMCS version 1
1974 * (== KVM_EVMCS_VERSION) and thus we expect guest to set this
1975 * value to first u32 field of eVMCS which should specify eVMCS
1976 * VersionNumber.
1977 *
1978 * Guest should be aware of supported eVMCS versions by host by
1979 * examining CPUID.0x4000000A.EAX[0:15]. Host userspace VMM is
1980 * expected to set this CPUID leaf according to the value
1981 * returned in vmcs_version from nested_enable_evmcs().
1982 *
1983 * However, it turns out that Microsoft Hyper-V fails to comply
1984 * to their own invented interface: When Hyper-V use eVMCS, it
1985 * just sets first u32 field of eVMCS to revision_id specified
1986 * in MSR_IA32_VMX_BASIC. Instead of used eVMCS version number
1987 * which is one of the supported versions specified in
1988 * CPUID.0x4000000A.EAX[0:15].
1989 *
1990 * To overcome Hyper-V bug, we accept here either a supported
1991 * eVMCS version or VMCS12 revision_id as valid values for first
1992 * u32 field of eVMCS.
1993 */
1994 if ((vmx->nested.hv_evmcs->revision_id != KVM_EVMCS_VERSION) &&
1995 (vmx->nested.hv_evmcs->revision_id != VMCS12_REVISION)) {
1996 nested_release_evmcs(vcpu);
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01001997 return EVMPTRLD_VMFAIL;
Sean Christopherson55d23752018-12-03 13:53:18 -08001998 }
1999
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02002000 vmx->nested.hv_evmcs_vmptr = evmcs_gpa;
Sean Christopherson55d23752018-12-03 13:53:18 -08002001
Vitaly Kuznetsova21a39c2019-06-28 13:23:32 +02002002 evmcs_gpa_changed = true;
Sean Christopherson55d23752018-12-03 13:53:18 -08002003 /*
2004 * Unlike normal vmcs12, enlightened vmcs12 is not fully
2005 * reloaded from guest's memory (read only fields, fields not
2006 * present in struct hv_enlightened_vmcs, ...). Make sure there
2007 * are no leftovers.
2008 */
2009 if (from_launch) {
2010 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2011 memset(vmcs12, 0, sizeof(*vmcs12));
2012 vmcs12->hdr.revision_id = VMCS12_REVISION;
2013 }
2014
2015 }
Vitaly Kuznetsova21a39c2019-06-28 13:23:32 +02002016
2017 /*
Miaohe Linffdbd502020-02-07 23:22:45 +08002018 * Clean fields data can't be used on VMLAUNCH and when we switch
Vitaly Kuznetsova21a39c2019-06-28 13:23:32 +02002019 * between different L2 guests as KVM keeps a single VMCS12 per L1.
2020 */
2021 if (from_launch || evmcs_gpa_changed)
2022 vmx->nested.hv_evmcs->hv_clean_fields &=
2023 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
2024
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01002025 return EVMPTRLD_SUCCEEDED;
Sean Christopherson55d23752018-12-03 13:53:18 -08002026}
2027
Sean Christopherson3731905ef2019-05-07 08:36:27 -07002028void nested_sync_vmcs12_to_shadow(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08002029{
2030 struct vcpu_vmx *vmx = to_vmx(vcpu);
2031
Vitaly Kuznetsovdc313382021-05-26 15:20:24 +02002032 if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
Sean Christopherson55d23752018-12-03 13:53:18 -08002033 copy_vmcs12_to_enlightened(vmx);
Vitaly Kuznetsovdc313382021-05-26 15:20:24 +02002034 else
Sean Christopherson55d23752018-12-03 13:53:18 -08002035 copy_vmcs12_to_shadow(vmx);
Sean Christopherson55d23752018-12-03 13:53:18 -08002036
Sean Christopherson3731905ef2019-05-07 08:36:27 -07002037 vmx->nested.need_vmcs12_to_shadow_sync = false;
Sean Christopherson55d23752018-12-03 13:53:18 -08002038}
2039
2040static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
2041{
2042 struct vcpu_vmx *vmx =
2043 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
2044
2045 vmx->nested.preemption_timer_expired = true;
2046 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
2047 kvm_vcpu_kick(&vmx->vcpu);
2048
2049 return HRTIMER_NORESTART;
2050}
2051
Peter Shier850448f2020-05-26 14:51:06 -07002052static u64 vmx_calc_preemption_timer_value(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08002053{
Peter Shier850448f2020-05-26 14:51:06 -07002054 struct vcpu_vmx *vmx = to_vmx(vcpu);
2055 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Peter Shier850448f2020-05-26 14:51:06 -07002056
2057 u64 l1_scaled_tsc = kvm_read_l1_tsc(vcpu, rdtsc()) >>
2058 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
2059
2060 if (!vmx->nested.has_preemption_timer_deadline) {
Makarand Sonare8d7fbf02020-05-26 14:51:07 -07002061 vmx->nested.preemption_timer_deadline =
2062 vmcs12->vmx_preemption_timer_value + l1_scaled_tsc;
Peter Shier850448f2020-05-26 14:51:06 -07002063 vmx->nested.has_preemption_timer_deadline = true;
Makarand Sonare8d7fbf02020-05-26 14:51:07 -07002064 }
2065 return vmx->nested.preemption_timer_deadline - l1_scaled_tsc;
Peter Shier850448f2020-05-26 14:51:06 -07002066}
2067
2068static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu,
2069 u64 preemption_timeout)
2070{
Sean Christopherson55d23752018-12-03 13:53:18 -08002071 struct vcpu_vmx *vmx = to_vmx(vcpu);
2072
2073 /*
2074 * A timer value of zero is architecturally guaranteed to cause
2075 * a VMExit prior to executing any instructions in the guest.
2076 */
2077 if (preemption_timeout == 0) {
2078 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
2079 return;
2080 }
2081
2082 if (vcpu->arch.virtual_tsc_khz == 0)
2083 return;
2084
2085 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
2086 preemption_timeout *= 1000000;
2087 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
2088 hrtimer_start(&vmx->nested.preemption_timer,
Jim Mattsonada00982020-05-08 13:36:42 -07002089 ktime_add_ns(ktime_get(), preemption_timeout),
2090 HRTIMER_MODE_ABS_PINNED);
Sean Christopherson55d23752018-12-03 13:53:18 -08002091}
2092
2093static u64 nested_vmx_calc_efer(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
2094{
2095 if (vmx->nested.nested_run_pending &&
2096 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
2097 return vmcs12->guest_ia32_efer;
2098 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
2099 return vmx->vcpu.arch.efer | (EFER_LMA | EFER_LME);
2100 else
2101 return vmx->vcpu.arch.efer & ~(EFER_LMA | EFER_LME);
2102}
2103
2104static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx)
2105{
2106 /*
2107 * If vmcs02 hasn't been initialized, set the constant vmcs02 state
2108 * according to L0's settings (vmcs12 is irrelevant here). Host
2109 * fields that come from L0 and are not constant, e.g. HOST_CR3,
2110 * will be set as needed prior to VMLAUNCH/VMRESUME.
2111 */
2112 if (vmx->nested.vmcs02_initialized)
2113 return;
2114 vmx->nested.vmcs02_initialized = true;
2115
2116 /*
2117 * We don't care what the EPTP value is we just need to guarantee
2118 * it's valid so we don't get a false positive when doing early
2119 * consistency checks.
2120 */
2121 if (enable_ept && nested_early_check)
Sean Christopherson2a40b902020-07-15 20:41:18 -07002122 vmcs_write64(EPT_POINTER,
2123 construct_eptp(&vmx->vcpu, 0, PT64_ROOT_4LEVEL));
Sean Christopherson55d23752018-12-03 13:53:18 -08002124
2125 /* All VMFUNCs are currently emulated through L0 vmexits. */
2126 if (cpu_has_vmx_vmfunc())
2127 vmcs_write64(VM_FUNCTION_CONTROL, 0);
2128
2129 if (cpu_has_vmx_posted_intr())
2130 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
2131
2132 if (cpu_has_vmx_msr_bitmap())
2133 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
2134
Sean Christopherson4d6c9892019-05-07 09:06:30 -07002135 /*
Sean Christophersonc3bb9a22021-02-12 16:50:07 -08002136 * PML is emulated for L2, but never enabled in hardware as the MMU
2137 * handles A/D emulation. Disabling PML for L2 also avoids having to
2138 * deal with filtering out L2 GPAs from the buffer.
Sean Christopherson4d6c9892019-05-07 09:06:30 -07002139 */
2140 if (enable_pml) {
Sean Christophersonc3bb9a22021-02-12 16:50:07 -08002141 vmcs_write64(PML_ADDRESS, 0);
2142 vmcs_write16(GUEST_PML_INDEX, -1);
Sean Christopherson4d6c9892019-05-07 09:06:30 -07002143 }
Sean Christopherson55d23752018-12-03 13:53:18 -08002144
Sean Christophersonc538d572019-05-07 09:06:29 -07002145 if (cpu_has_vmx_encls_vmexit())
Yu Zhang64c78502021-09-30 01:51:53 +08002146 vmcs_write64(ENCLS_EXITING_BITMAP, INVALID_GPA);
Sean Christopherson55d23752018-12-03 13:53:18 -08002147
2148 /*
2149 * Set the MSR load/store lists to match L0's settings. Only the
2150 * addresses are constant (for vmcs02), the counts can change based
2151 * on L2's behavior, e.g. switching to/from long mode.
2152 */
Aaron Lewis662f1d12019-11-07 21:14:39 -08002153 vmcs_write64(VM_EXIT_MSR_STORE_ADDR, __pa(vmx->msr_autostore.guest.val));
Sean Christopherson55d23752018-12-03 13:53:18 -08002154 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
2155 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
2156
2157 vmx_set_constant_host_state(vmx);
2158}
2159
Paolo Bonzinib1346ab2019-06-06 17:24:00 +02002160static void prepare_vmcs02_early_rare(struct vcpu_vmx *vmx,
Sean Christopherson55d23752018-12-03 13:53:18 -08002161 struct vmcs12 *vmcs12)
2162{
2163 prepare_vmcs02_constant_state(vmx);
2164
Yu Zhang64c78502021-09-30 01:51:53 +08002165 vmcs_write64(VMCS_LINK_POINTER, INVALID_GPA);
Sean Christopherson55d23752018-12-03 13:53:18 -08002166
2167 if (enable_vpid) {
2168 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
2169 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
2170 else
2171 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
2172 }
2173}
2174
Sean Christopherson389ab252021-08-10 10:19:50 -07002175static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct loaded_vmcs *vmcs01,
2176 struct vmcs12 *vmcs12)
Sean Christopherson55d23752018-12-03 13:53:18 -08002177{
Sean Christophersonc3bb9a22021-02-12 16:50:07 -08002178 u32 exec_control;
Sean Christopherson55d23752018-12-03 13:53:18 -08002179 u64 guest_efer = nested_vmx_calc_efer(vmx, vmcs12);
2180
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02002181 if (vmx->nested.dirty_vmcs12 || evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
Paolo Bonzinib1346ab2019-06-06 17:24:00 +02002182 prepare_vmcs02_early_rare(vmx, vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08002183
2184 /*
Sean Christopherson55d23752018-12-03 13:53:18 -08002185 * PIN CONTROLS
2186 */
Sean Christopherson389ab252021-08-10 10:19:50 -07002187 exec_control = __pin_controls_get(vmcs01);
Sean Christopherson804939e2019-05-07 12:18:05 -07002188 exec_control |= (vmcs12->pin_based_vm_exec_control &
2189 ~PIN_BASED_VMX_PREEMPTION_TIMER);
Sean Christopherson55d23752018-12-03 13:53:18 -08002190
2191 /* Posted interrupts setting is only taken from vmcs12. */
Sean Christophersonf7782bb82021-08-10 07:45:26 -07002192 vmx->nested.pi_pending = false;
2193 if (nested_cpu_has_posted_intr(vmcs12))
Sean Christopherson55d23752018-12-03 13:53:18 -08002194 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
Sean Christophersonf7782bb82021-08-10 07:45:26 -07002195 else
Sean Christopherson55d23752018-12-03 13:53:18 -08002196 exec_control &= ~PIN_BASED_POSTED_INTR;
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002197 pin_controls_set(vmx, exec_control);
Sean Christopherson55d23752018-12-03 13:53:18 -08002198
2199 /*
2200 * EXEC CONTROLS
2201 */
Sean Christopherson389ab252021-08-10 10:19:50 -07002202 exec_control = __exec_controls_get(vmcs01); /* L0's desires */
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08002203 exec_control &= ~CPU_BASED_INTR_WINDOW_EXITING;
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08002204 exec_control &= ~CPU_BASED_NMI_WINDOW_EXITING;
Sean Christopherson55d23752018-12-03 13:53:18 -08002205 exec_control &= ~CPU_BASED_TPR_SHADOW;
2206 exec_control |= vmcs12->cpu_based_vm_exec_control;
2207
Liran Alon02d496cf2019-11-11 14:30:55 +02002208 vmx->nested.l1_tpr_threshold = -1;
Sean Christophersonca2f5462019-05-07 09:06:33 -07002209 if (exec_control & CPU_BASED_TPR_SHADOW)
Sean Christopherson55d23752018-12-03 13:53:18 -08002210 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Sean Christopherson55d23752018-12-03 13:53:18 -08002211#ifdef CONFIG_X86_64
Sean Christophersonca2f5462019-05-07 09:06:33 -07002212 else
Sean Christopherson55d23752018-12-03 13:53:18 -08002213 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
2214 CPU_BASED_CR8_STORE_EXITING;
2215#endif
Sean Christopherson55d23752018-12-03 13:53:18 -08002216
2217 /*
2218 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
2219 * for I/O port accesses.
2220 */
Sean Christopherson55d23752018-12-03 13:53:18 -08002221 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
Sean Christophersonde0286b2019-05-07 12:18:01 -07002222 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
2223
2224 /*
2225 * This bit will be computed in nested_get_vmcs12_pages, because
2226 * we do not have access to L1's MSR bitmap yet. For now, keep
2227 * the same bit as before, hoping to avoid multiple VMWRITEs that
2228 * only set/clear this bit.
2229 */
2230 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
2231 exec_control |= exec_controls_get(vmx) & CPU_BASED_USE_MSR_BITMAPS;
2232
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002233 exec_controls_set(vmx, exec_control);
Sean Christopherson55d23752018-12-03 13:53:18 -08002234
2235 /*
2236 * SECONDARY EXEC CONTROLS
2237 */
2238 if (cpu_has_secondary_exec_ctrls()) {
Sean Christopherson389ab252021-08-10 10:19:50 -07002239 exec_control = __secondary_exec_controls_get(vmcs01);
Sean Christopherson55d23752018-12-03 13:53:18 -08002240
2241 /* Take the following fields only from vmcs12 */
2242 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Sean Christopherson389ab252021-08-10 10:19:50 -07002243 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sean Christopherson55d23752018-12-03 13:53:18 -08002244 SECONDARY_EXEC_ENABLE_INVPCID |
Sean Christopherson7f3603b2020-09-23 09:50:47 -07002245 SECONDARY_EXEC_ENABLE_RDTSCP |
Sean Christopherson55d23752018-12-03 13:53:18 -08002246 SECONDARY_EXEC_XSAVES |
Tao Xue69e72fa2019-07-16 14:55:49 +08002247 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE |
Sean Christopherson55d23752018-12-03 13:53:18 -08002248 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
2249 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Ilias Stamatisd041b5e2021-05-26 19:44:17 +01002250 SECONDARY_EXEC_ENABLE_VMFUNC |
Sean Christopherson389ab252021-08-10 10:19:50 -07002251 SECONDARY_EXEC_TSC_SCALING |
2252 SECONDARY_EXEC_DESC);
2253
Sean Christopherson55d23752018-12-03 13:53:18 -08002254 if (nested_cpu_has(vmcs12,
Sean Christophersonc3bb9a22021-02-12 16:50:07 -08002255 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
2256 exec_control |= vmcs12->secondary_vm_exec_control;
2257
2258 /* PML is emulated and never enabled in hardware for L2. */
2259 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Sean Christopherson55d23752018-12-03 13:53:18 -08002260
2261 /* VMCS shadowing for L2 is emulated for now */
2262 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
2263
Sean Christopherson469debd2019-05-07 12:18:02 -07002264 /*
2265 * Preset *DT exiting when emulating UMIP, so that vmx_set_cr4()
2266 * will not have to rewrite the controls just for this bit.
2267 */
2268 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated() &&
2269 (vmcs12->guest_cr4 & X86_CR4_UMIP))
2270 exec_control |= SECONDARY_EXEC_DESC;
2271
Sean Christopherson55d23752018-12-03 13:53:18 -08002272 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
2273 vmcs_write16(GUEST_INTR_STATUS,
2274 vmcs12->guest_intr_status);
2275
Krish Sadhukhanbddd82d2020-09-21 08:10:25 +00002276 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
2277 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
2278
Sean Christopherson72add912021-04-12 16:21:42 +12002279 if (exec_control & SECONDARY_EXEC_ENCLS_EXITING)
2280 vmx_write_encls_bitmap(&vmx->vcpu, vmcs12);
2281
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002282 secondary_exec_controls_set(vmx, exec_control);
Sean Christopherson55d23752018-12-03 13:53:18 -08002283 }
2284
2285 /*
2286 * ENTRY CONTROLS
2287 *
2288 * vmcs12's VM_{ENTRY,EXIT}_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE
2289 * are emulated by vmx_set_efer() in prepare_vmcs02(), but speculate
2290 * on the related bits (if supported by the CPU) in the hope that
2291 * we can avoid VMWrites during vmx_set_efer().
2292 */
Sean Christopherson389ab252021-08-10 10:19:50 -07002293 exec_control = __vm_entry_controls_get(vmcs01);
2294 exec_control |= vmcs12->vm_entry_controls;
2295 exec_control &= ~(VM_ENTRY_IA32E_MODE | VM_ENTRY_LOAD_IA32_EFER);
Sean Christopherson55d23752018-12-03 13:53:18 -08002296 if (cpu_has_load_ia32_efer()) {
2297 if (guest_efer & EFER_LMA)
2298 exec_control |= VM_ENTRY_IA32E_MODE;
2299 if (guest_efer != host_efer)
2300 exec_control |= VM_ENTRY_LOAD_IA32_EFER;
2301 }
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002302 vm_entry_controls_set(vmx, exec_control);
Sean Christopherson55d23752018-12-03 13:53:18 -08002303
2304 /*
2305 * EXIT CONTROLS
2306 *
2307 * L2->L1 exit controls are emulated - the hardware exit is to L0 so
2308 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
2309 * bits may be modified by vmx_set_efer() in prepare_vmcs02().
2310 */
Sean Christopherson389ab252021-08-10 10:19:50 -07002311 exec_control = __vm_exit_controls_get(vmcs01);
Sean Christopherson55d23752018-12-03 13:53:18 -08002312 if (cpu_has_load_ia32_efer() && guest_efer != host_efer)
2313 exec_control |= VM_EXIT_LOAD_IA32_EFER;
Sean Christopherson389ab252021-08-10 10:19:50 -07002314 else
2315 exec_control &= ~VM_EXIT_LOAD_IA32_EFER;
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002316 vm_exit_controls_set(vmx, exec_control);
Sean Christopherson55d23752018-12-03 13:53:18 -08002317
2318 /*
2319 * Interrupt/Exception Fields
2320 */
2321 if (vmx->nested.nested_run_pending) {
2322 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2323 vmcs12->vm_entry_intr_info_field);
2324 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
2325 vmcs12->vm_entry_exception_error_code);
2326 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2327 vmcs12->vm_entry_instruction_len);
2328 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
2329 vmcs12->guest_interruptibility_info);
2330 vmx->loaded_vmcs->nmi_known_unmasked =
2331 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
2332 } else {
2333 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
2334 }
2335}
2336
Paolo Bonzinib1346ab2019-06-06 17:24:00 +02002337static void prepare_vmcs02_rare(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
Sean Christopherson55d23752018-12-03 13:53:18 -08002338{
2339 struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs;
2340
2341 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
2342 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2)) {
2343 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
2344 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
2345 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
2346 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
2347 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
2348 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
2349 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
2350 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
2351 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
2352 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
2353 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
2354 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
2355 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
2356 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
2357 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
2358 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
2359 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
2360 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07002361 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
2362 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
Sean Christopherson55d23752018-12-03 13:53:18 -08002363 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
2364 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
2365 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
2366 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
2367 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
2368 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
2369 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
2370 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
2371 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
2372 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
2373 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
2374 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
2375 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
2376 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
2377 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
2378 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Sean Christophersonfc387d82020-09-23 11:44:46 -07002379
2380 vmx->segment_cache.bitmask = 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08002381 }
2382
2383 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
2384 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1)) {
2385 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
2386 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
2387 vmcs12->guest_pending_dbg_exceptions);
2388 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
2389 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
2390
2391 /*
2392 * L1 may access the L2's PDPTR, so save them to construct
2393 * vmcs12
2394 */
2395 if (enable_ept) {
2396 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
2397 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
2398 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
2399 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
2400 }
Sean Christophersonc27e5b02019-05-07 09:06:39 -07002401
2402 if (kvm_mpx_supported() && vmx->nested.nested_run_pending &&
2403 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
2404 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
Sean Christopherson55d23752018-12-03 13:53:18 -08002405 }
2406
2407 if (nested_cpu_has_xsaves(vmcs12))
2408 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
2409
2410 /*
2411 * Whether page-faults are trapped is determined by a combination of
Paolo Bonzinia0c13432020-07-10 17:48:08 +02002412 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF. If L0
2413 * doesn't care about page faults then we should set all of these to
2414 * L1's desires. However, if L0 does care about (some) page faults, it
2415 * is not easy (if at all possible?) to merge L0 and L1's desires, we
2416 * simply ask to exit on each and every L2 page fault. This is done by
2417 * setting MASK=MATCH=0 and (see below) EB.PF=1.
Sean Christopherson55d23752018-12-03 13:53:18 -08002418 * Note that below we don't need special code to set EB.PF beyond the
2419 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
2420 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
2421 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
2422 */
Paolo Bonzinia0c13432020-07-10 17:48:08 +02002423 if (vmx_need_pf_intercept(&vmx->vcpu)) {
2424 /*
2425 * TODO: if both L0 and L1 need the same MASK and MATCH,
2426 * go ahead and use it?
2427 */
2428 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
2429 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
2430 } else {
2431 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, vmcs12->page_fault_error_code_mask);
2432 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, vmcs12->page_fault_error_code_match);
2433 }
Sean Christopherson55d23752018-12-03 13:53:18 -08002434
2435 if (cpu_has_vmx_apicv()) {
2436 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
2437 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
2438 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
2439 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
2440 }
2441
Aaron Lewis662f1d12019-11-07 21:14:39 -08002442 /*
2443 * Make sure the msr_autostore list is up to date before we set the
2444 * count in the vmcs02.
2445 */
2446 prepare_vmx_msr_autostore_list(&vmx->vcpu, MSR_IA32_TSC);
2447
2448 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, vmx->msr_autostore.guest.nr);
Sean Christopherson55d23752018-12-03 13:53:18 -08002449 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
2450 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
2451
2452 set_cr4_guest_host_mask(vmx);
Sean Christopherson55d23752018-12-03 13:53:18 -08002453}
2454
2455/*
2456 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
2457 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
2458 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
2459 * guest in a way that will both be appropriate to L1's requests, and our
2460 * needs. In addition to modifying the active vmcs (which is vmcs02), this
2461 * function also has additional necessary side-effects, like setting various
2462 * vcpu->arch fields.
2463 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
2464 * is assigned to entry_failure_code on failure.
2465 */
2466static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Maxim Levitsky0f857222021-06-07 12:02:00 +03002467 bool from_vmentry,
Sean Christopherson68cda402020-05-11 15:05:29 -07002468 enum vm_entry_failure_code *entry_failure_code)
Sean Christopherson55d23752018-12-03 13:53:18 -08002469{
2470 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersonc7554efc2019-05-07 09:06:40 -07002471 bool load_guest_pdptrs_vmcs12 = false;
Sean Christopherson55d23752018-12-03 13:53:18 -08002472
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02002473 if (vmx->nested.dirty_vmcs12 || evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) {
Paolo Bonzinib1346ab2019-06-06 17:24:00 +02002474 prepare_vmcs02_rare(vmx, vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08002475 vmx->nested.dirty_vmcs12 = false;
Sean Christopherson55d23752018-12-03 13:53:18 -08002476
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02002477 load_guest_pdptrs_vmcs12 = !evmptr_is_valid(vmx->nested.hv_evmcs_vmptr) ||
2478 !(vmx->nested.hv_evmcs->hv_clean_fields &
Sean Christophersonc7554efc2019-05-07 09:06:40 -07002479 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1);
Sean Christopherson55d23752018-12-03 13:53:18 -08002480 }
2481
2482 if (vmx->nested.nested_run_pending &&
2483 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
2484 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
2485 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
2486 } else {
2487 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
2488 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
2489 }
Sean Christopherson3b013a22019-05-07 09:06:28 -07002490 if (kvm_mpx_supported() && (!vmx->nested.nested_run_pending ||
2491 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)))
2492 vmcs_write64(GUEST_BNDCFGS, vmx->nested.vmcs01_guest_bndcfgs);
Sean Christopherson55d23752018-12-03 13:53:18 -08002493 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
2494
Sean Christopherson55d23752018-12-03 13:53:18 -08002495 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
2496 * bitwise-or of what L1 wants to trap for L2, and what we want to
2497 * trap. Note that CR0.TS also needs updating - we do this later.
2498 */
Jason Baronb6a7cc32021-01-14 22:27:54 -05002499 vmx_update_exception_bitmap(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08002500 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
2501 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
2502
2503 if (vmx->nested.nested_run_pending &&
2504 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
2505 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
2506 vcpu->arch.pat = vmcs12->guest_ia32_pat;
2507 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2508 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
2509 }
2510
Ilias Stamatisd041b5e2021-05-26 19:44:17 +01002511 vcpu->arch.tsc_offset = kvm_calc_nested_tsc_offset(
2512 vcpu->arch.l1_tsc_offset,
2513 vmx_get_l2_tsc_offset(vcpu),
2514 vmx_get_l2_tsc_multiplier(vcpu));
2515
2516 vcpu->arch.tsc_scaling_ratio = kvm_calc_nested_tsc_multiplier(
2517 vcpu->arch.l1_tsc_scaling_ratio,
2518 vmx_get_l2_tsc_multiplier(vcpu));
2519
Sean Christopherson55d23752018-12-03 13:53:18 -08002520 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Sean Christopherson55d23752018-12-03 13:53:18 -08002521 if (kvm_has_tsc_control)
Ilias Stamatis1ab92872021-06-07 11:54:38 +01002522 vmcs_write64(TSC_MULTIPLIER, vcpu->arch.tsc_scaling_ratio);
Sean Christopherson55d23752018-12-03 13:53:18 -08002523
Sean Christopherson50b265a2020-03-20 14:28:19 -07002524 nested_vmx_transition_tlb_flush(vcpu, vmcs12, true);
Sean Christopherson55d23752018-12-03 13:53:18 -08002525
2526 if (nested_cpu_has_ept(vmcs12))
2527 nested_ept_init_mmu_context(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08002528
2529 /*
2530 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
2531 * bits which we consider mandatory enabled.
2532 * The CR0_READ_SHADOW is what L2 should have expected to read given
2533 * the specifications by L1; It's not enough to take
2534 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
2535 * have more bits than L1 expected.
2536 */
2537 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
2538 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
2539
2540 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
2541 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
2542
2543 vcpu->arch.efer = nested_vmx_calc_efer(vmx, vmcs12);
2544 /* Note: may modify VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
2545 vmx_set_efer(vcpu, vcpu->arch.efer);
2546
2547 /*
2548 * Guest state is invalid and unrestricted guest is disabled,
2549 * which means L1 attempted VMEntry to L2 with invalid state.
2550 * Fail the VMEntry.
Maxim Levitskyc8607e42021-09-13 17:09:53 +03002551 *
2552 * However when force loading the guest state (SMM exit or
2553 * loading nested state after migration, it is possible to
2554 * have invalid guest state now, which will be later fixed by
2555 * restoring L2 register state
Sean Christopherson55d23752018-12-03 13:53:18 -08002556 */
Maxim Levitskyc8607e42021-09-13 17:09:53 +03002557 if (CC(from_vmentry && !vmx_guest_state_valid(vcpu))) {
Sean Christopherson55d23752018-12-03 13:53:18 -08002558 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christophersonc80add02019-04-11 12:18:09 -07002559 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002560 }
2561
2562 /* Shadow page tables on either EPT or shadow page tables. */
2563 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Maxim Levitsky0f857222021-06-07 12:02:00 +03002564 from_vmentry, entry_failure_code))
Sean Christophersonc80add02019-04-11 12:18:09 -07002565 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002566
Sean Christopherson04f11ef2019-09-27 14:45:16 -07002567 /*
2568 * Immediately write vmcs02.GUEST_CR3. It will be propagated to vmcs12
2569 * on nested VM-Exit, which can occur without actually running L2 and
Paolo Bonzini727a7e22020-03-05 03:52:50 -05002570 * thus without hitting vmx_load_mmu_pgd(), e.g. if L1 is entering L2 with
Sean Christopherson04f11ef2019-09-27 14:45:16 -07002571 * vmcs12.GUEST_ACTIVITYSTATE=HLT, in which case KVM will intercept the
2572 * transition to HLT instead of running L2.
2573 */
2574 if (enable_ept)
2575 vmcs_writel(GUEST_CR3, vmcs12->guest_cr3);
2576
Sean Christophersonc7554efc2019-05-07 09:06:40 -07002577 /* Late preparation of GUEST_PDPTRs now that EFER and CRs are set. */
2578 if (load_guest_pdptrs_vmcs12 && nested_cpu_has_ept(vmcs12) &&
2579 is_pae_paging(vcpu)) {
2580 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
2581 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
2582 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
2583 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
2584 }
2585
Sean Christopherson55d23752018-12-03 13:53:18 -08002586 if (!enable_ept)
2587 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
2588
Oliver Upton71f73472019-11-13 16:17:19 -08002589 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) &&
Oliver Uptond1968422019-12-13 16:33:58 -08002590 WARN_ON_ONCE(kvm_set_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL,
2591 vmcs12->guest_ia32_perf_global_ctrl)))
Oliver Upton71f73472019-11-13 16:17:19 -08002592 return -EINVAL;
2593
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02002594 kvm_rsp_write(vcpu, vmcs12->guest_rsp);
2595 kvm_rip_write(vcpu, vmcs12->guest_rip);
Vitaly Kuznetsovdc313382021-05-26 15:20:24 +02002596
2597 /*
2598 * It was observed that genuine Hyper-V running in L1 doesn't reset
2599 * 'hv_clean_fields' by itself, it only sets the corresponding dirty
2600 * bits when it changes a field in eVMCS. Mark all fields as clean
2601 * here.
2602 */
2603 if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
2604 vmx->nested.hv_evmcs->hv_clean_fields |=
2605 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
2606
Sean Christopherson55d23752018-12-03 13:53:18 -08002607 return 0;
2608}
2609
2610static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
2611{
Sean Christopherson5497b952019-07-11 08:58:29 -07002612 if (CC(!nested_cpu_has_nmi_exiting(vmcs12) &&
2613 nested_cpu_has_virtual_nmis(vmcs12)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002614 return -EINVAL;
2615
Sean Christopherson5497b952019-07-11 08:58:29 -07002616 if (CC(!nested_cpu_has_virtual_nmis(vmcs12) &&
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08002617 nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002618 return -EINVAL;
2619
2620 return 0;
2621}
2622
Sean Christophersonac6389a2020-03-02 18:02:38 -08002623static bool nested_vmx_check_eptp(struct kvm_vcpu *vcpu, u64 new_eptp)
Sean Christopherson55d23752018-12-03 13:53:18 -08002624{
2625 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08002626
2627 /* Check for memory type validity */
Sean Christophersonac6389a2020-03-02 18:02:38 -08002628 switch (new_eptp & VMX_EPTP_MT_MASK) {
Sean Christopherson55d23752018-12-03 13:53:18 -08002629 case VMX_EPTP_MT_UC:
Sean Christopherson5497b952019-07-11 08:58:29 -07002630 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002631 return false;
2632 break;
2633 case VMX_EPTP_MT_WB:
Sean Christopherson5497b952019-07-11 08:58:29 -07002634 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002635 return false;
2636 break;
2637 default:
2638 return false;
2639 }
2640
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08002641 /* Page-walk levels validity. */
Sean Christophersonac6389a2020-03-02 18:02:38 -08002642 switch (new_eptp & VMX_EPTP_PWL_MASK) {
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08002643 case VMX_EPTP_PWL_5:
2644 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_PAGE_WALK_5_BIT)))
2645 return false;
2646 break;
2647 case VMX_EPTP_PWL_4:
2648 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_PAGE_WALK_4_BIT)))
2649 return false;
2650 break;
2651 default:
Sean Christopherson55d23752018-12-03 13:53:18 -08002652 return false;
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08002653 }
Sean Christopherson55d23752018-12-03 13:53:18 -08002654
2655 /* Reserved bits should not be set */
Sean Christopherson636e8b72021-02-03 16:01:10 -08002656 if (CC(kvm_vcpu_is_illegal_gpa(vcpu, new_eptp) || ((new_eptp >> 7) & 0x1f)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002657 return false;
2658
2659 /* AD, if set, should be supported */
Sean Christophersonac6389a2020-03-02 18:02:38 -08002660 if (new_eptp & VMX_EPTP_AD_ENABLE_BIT) {
Sean Christopherson5497b952019-07-11 08:58:29 -07002661 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002662 return false;
2663 }
2664
2665 return true;
2666}
2667
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002668/*
2669 * Checks related to VM-Execution Control Fields
2670 */
2671static int nested_check_vm_execution_controls(struct kvm_vcpu *vcpu,
2672 struct vmcs12 *vmcs12)
2673{
2674 struct vcpu_vmx *vmx = to_vmx(vcpu);
2675
Sean Christopherson5497b952019-07-11 08:58:29 -07002676 if (CC(!vmx_control_verify(vmcs12->pin_based_vm_exec_control,
2677 vmx->nested.msrs.pinbased_ctls_low,
2678 vmx->nested.msrs.pinbased_ctls_high)) ||
2679 CC(!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
2680 vmx->nested.msrs.procbased_ctls_low,
2681 vmx->nested.msrs.procbased_ctls_high)))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002682 return -EINVAL;
2683
2684 if (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
Sean Christopherson5497b952019-07-11 08:58:29 -07002685 CC(!vmx_control_verify(vmcs12->secondary_vm_exec_control,
2686 vmx->nested.msrs.secondary_ctls_low,
2687 vmx->nested.msrs.secondary_ctls_high)))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002688 return -EINVAL;
2689
Sean Christopherson5497b952019-07-11 08:58:29 -07002690 if (CC(vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu)) ||
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002691 nested_vmx_check_io_bitmap_controls(vcpu, vmcs12) ||
2692 nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12) ||
2693 nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12) ||
2694 nested_vmx_check_apic_access_controls(vcpu, vmcs12) ||
2695 nested_vmx_check_apicv_controls(vcpu, vmcs12) ||
2696 nested_vmx_check_nmi_controls(vmcs12) ||
2697 nested_vmx_check_pml_controls(vcpu, vmcs12) ||
2698 nested_vmx_check_unrestricted_guest_controls(vcpu, vmcs12) ||
2699 nested_vmx_check_mode_based_ept_exec_controls(vcpu, vmcs12) ||
2700 nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12) ||
Sean Christopherson5497b952019-07-11 08:58:29 -07002701 CC(nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002702 return -EINVAL;
2703
Sean Christophersonbc441212019-02-12 16:42:23 -08002704 if (!nested_cpu_has_preemption_timer(vmcs12) &&
2705 nested_cpu_has_save_preemption_timer(vmcs12))
2706 return -EINVAL;
2707
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002708 if (nested_cpu_has_ept(vmcs12) &&
Sean Christophersonac6389a2020-03-02 18:02:38 -08002709 CC(!nested_vmx_check_eptp(vcpu, vmcs12->ept_pointer)))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002710 return -EINVAL;
2711
2712 if (nested_cpu_has_vmfunc(vmcs12)) {
Sean Christopherson5497b952019-07-11 08:58:29 -07002713 if (CC(vmcs12->vm_function_control &
2714 ~vmx->nested.msrs.vmfunc_controls))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002715 return -EINVAL;
2716
2717 if (nested_cpu_has_eptp_switching(vmcs12)) {
Sean Christopherson5497b952019-07-11 08:58:29 -07002718 if (CC(!nested_cpu_has_ept(vmcs12)) ||
2719 CC(!page_address_valid(vcpu, vmcs12->eptp_list_address)))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002720 return -EINVAL;
2721 }
2722 }
2723
2724 return 0;
2725}
2726
Krish Sadhukhan61446ba2018-12-12 13:30:09 -05002727/*
2728 * Checks related to VM-Exit Control Fields
2729 */
2730static int nested_check_vm_exit_controls(struct kvm_vcpu *vcpu,
2731 struct vmcs12 *vmcs12)
2732{
2733 struct vcpu_vmx *vmx = to_vmx(vcpu);
2734
Sean Christopherson5497b952019-07-11 08:58:29 -07002735 if (CC(!vmx_control_verify(vmcs12->vm_exit_controls,
2736 vmx->nested.msrs.exit_ctls_low,
2737 vmx->nested.msrs.exit_ctls_high)) ||
2738 CC(nested_vmx_check_exit_msr_switch_controls(vcpu, vmcs12)))
Krish Sadhukhan61446ba2018-12-12 13:30:09 -05002739 return -EINVAL;
2740
2741 return 0;
2742}
2743
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002744/*
2745 * Checks related to VM-Entry Control Fields
2746 */
2747static int nested_check_vm_entry_controls(struct kvm_vcpu *vcpu,
2748 struct vmcs12 *vmcs12)
Sean Christopherson55d23752018-12-03 13:53:18 -08002749{
2750 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08002751
Sean Christopherson5497b952019-07-11 08:58:29 -07002752 if (CC(!vmx_control_verify(vmcs12->vm_entry_controls,
2753 vmx->nested.msrs.entry_ctls_low,
2754 vmx->nested.msrs.entry_ctls_high)))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002755 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002756
2757 /*
2758 * From the Intel SDM, volume 3:
2759 * Fields relevant to VM-entry event injection must be set properly.
2760 * These fields are the VM-entry interruption-information field, the
2761 * VM-entry exception error code, and the VM-entry instruction length.
2762 */
2763 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
2764 u32 intr_info = vmcs12->vm_entry_intr_info_field;
2765 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
2766 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
2767 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
2768 bool should_have_error_code;
2769 bool urg = nested_cpu_has2(vmcs12,
2770 SECONDARY_EXEC_UNRESTRICTED_GUEST);
2771 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
2772
2773 /* VM-entry interruption-info field: interruption type */
Sean Christopherson5497b952019-07-11 08:58:29 -07002774 if (CC(intr_type == INTR_TYPE_RESERVED) ||
2775 CC(intr_type == INTR_TYPE_OTHER_EVENT &&
2776 !nested_cpu_supports_monitor_trap_flag(vcpu)))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002777 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002778
2779 /* VM-entry interruption-info field: vector */
Sean Christopherson5497b952019-07-11 08:58:29 -07002780 if (CC(intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
2781 CC(intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
2782 CC(intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002783 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002784
2785 /* VM-entry interruption-info field: deliver error code */
2786 should_have_error_code =
2787 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
2788 x86_exception_has_error_code(vector);
Sean Christopherson5497b952019-07-11 08:58:29 -07002789 if (CC(has_error_code != should_have_error_code))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002790 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002791
2792 /* VM-entry exception error code */
Sean Christopherson5497b952019-07-11 08:58:29 -07002793 if (CC(has_error_code &&
Sean Christopherson567926c2019-10-01 09:21:23 -07002794 vmcs12->vm_entry_exception_error_code & GENMASK(31, 16)))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002795 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002796
2797 /* VM-entry interruption-info field: reserved bits */
Sean Christopherson5497b952019-07-11 08:58:29 -07002798 if (CC(intr_info & INTR_INFO_RESVD_BITS_MASK))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002799 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002800
2801 /* VM-entry instruction length */
2802 switch (intr_type) {
2803 case INTR_TYPE_SOFT_EXCEPTION:
2804 case INTR_TYPE_SOFT_INTR:
2805 case INTR_TYPE_PRIV_SW_EXCEPTION:
Sean Christopherson5497b952019-07-11 08:58:29 -07002806 if (CC(vmcs12->vm_entry_instruction_len > 15) ||
2807 CC(vmcs12->vm_entry_instruction_len == 0 &&
2808 CC(!nested_cpu_has_zero_length_injection(vcpu))))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002809 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002810 }
2811 }
2812
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002813 if (nested_vmx_check_entry_msr_switch_controls(vcpu, vmcs12))
2814 return -EINVAL;
2815
2816 return 0;
2817}
2818
Sean Christopherson5478ba32019-04-11 12:18:06 -07002819static int nested_vmx_check_controls(struct kvm_vcpu *vcpu,
2820 struct vmcs12 *vmcs12)
2821{
2822 if (nested_check_vm_execution_controls(vcpu, vmcs12) ||
2823 nested_check_vm_exit_controls(vcpu, vmcs12) ||
2824 nested_check_vm_entry_controls(vcpu, vmcs12))
Paolo Bonzini98d9e852019-04-12 10:19:57 +02002825 return -EINVAL;
Sean Christopherson5478ba32019-04-11 12:18:06 -07002826
Vitaly Kuznetsova8350232020-02-05 13:30:34 +01002827 if (to_vmx(vcpu)->nested.enlightened_vmcs_enabled)
2828 return nested_evmcs_check_controls(vmcs12);
2829
Sean Christopherson5478ba32019-04-11 12:18:06 -07002830 return 0;
2831}
2832
Paolo Bonzini98d9e852019-04-12 10:19:57 +02002833static int nested_vmx_check_host_state(struct kvm_vcpu *vcpu,
2834 struct vmcs12 *vmcs12)
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002835{
2836 bool ia32e;
2837
Sean Christopherson5497b952019-07-11 08:58:29 -07002838 if (CC(!nested_host_cr0_valid(vcpu, vmcs12->host_cr0)) ||
2839 CC(!nested_host_cr4_valid(vcpu, vmcs12->host_cr4)) ||
Sean Christopherson636e8b72021-02-03 16:01:10 -08002840 CC(kvm_vcpu_is_illegal_gpa(vcpu, vmcs12->host_cr3)))
Krish Sadhukhan254b2f32018-12-12 13:30:11 -05002841 return -EINVAL;
Krish Sadhukhan711eff32019-02-07 14:05:30 -05002842
Sean Christopherson5497b952019-07-11 08:58:29 -07002843 if (CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_esp, vcpu)) ||
2844 CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_eip, vcpu)))
Krish Sadhukhan711eff32019-02-07 14:05:30 -05002845 return -EINVAL;
2846
Krish Sadhukhanf6b0db1f2019-04-08 17:35:11 -04002847 if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) &&
Sean Christopherson5497b952019-07-11 08:58:29 -07002848 CC(!kvm_pat_valid(vmcs12->host_ia32_pat)))
Krish Sadhukhanf6b0db1f2019-04-08 17:35:11 -04002849 return -EINVAL;
2850
Oliver Uptonc547cb62019-11-13 16:17:17 -08002851 if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) &&
2852 CC(!kvm_valid_perf_global_ctrl(vcpu_to_pmu(vcpu),
2853 vmcs12->host_ia32_perf_global_ctrl)))
2854 return -EINVAL;
2855
Paolo Bonzinifd3edd42019-09-25 18:33:53 +02002856#ifdef CONFIG_X86_64
2857 ia32e = !!(vcpu->arch.efer & EFER_LMA);
2858#else
2859 ia32e = false;
2860#endif
2861
2862 if (ia32e) {
2863 if (CC(!(vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)) ||
2864 CC(!(vmcs12->host_cr4 & X86_CR4_PAE)))
2865 return -EINVAL;
2866 } else {
2867 if (CC(vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) ||
2868 CC(vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) ||
2869 CC(vmcs12->host_cr4 & X86_CR4_PCIDE) ||
2870 CC((vmcs12->host_rip) >> 32))
2871 return -EINVAL;
2872 }
Krish Sadhukhan1ef23e12019-07-03 19:54:35 -04002873
Sean Christopherson5497b952019-07-11 08:58:29 -07002874 if (CC(vmcs12->host_cs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2875 CC(vmcs12->host_ss_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2876 CC(vmcs12->host_ds_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2877 CC(vmcs12->host_es_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2878 CC(vmcs12->host_fs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2879 CC(vmcs12->host_gs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2880 CC(vmcs12->host_tr_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2881 CC(vmcs12->host_cs_selector == 0) ||
2882 CC(vmcs12->host_tr_selector == 0) ||
2883 CC(vmcs12->host_ss_selector == 0 && !ia32e))
Krish Sadhukhan1ef23e12019-07-03 19:54:35 -04002884 return -EINVAL;
2885
Sean Christopherson5497b952019-07-11 08:58:29 -07002886 if (CC(is_noncanonical_address(vmcs12->host_fs_base, vcpu)) ||
2887 CC(is_noncanonical_address(vmcs12->host_gs_base, vcpu)) ||
2888 CC(is_noncanonical_address(vmcs12->host_gdtr_base, vcpu)) ||
2889 CC(is_noncanonical_address(vmcs12->host_idtr_base, vcpu)) ||
Paolo Bonzinifd3edd42019-09-25 18:33:53 +02002890 CC(is_noncanonical_address(vmcs12->host_tr_base, vcpu)) ||
2891 CC(is_noncanonical_address(vmcs12->host_rip, vcpu)))
Krish Sadhukhan58450382019-08-09 12:26:19 -07002892 return -EINVAL;
Krish Sadhukhan1ef23e12019-07-03 19:54:35 -04002893
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002894 /*
2895 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
2896 * IA32_EFER MSR must be 0 in the field for that register. In addition,
2897 * the values of the LMA and LME bits in the field must each be that of
2898 * the host address-space size VM-exit control.
2899 */
2900 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
Sean Christopherson5497b952019-07-11 08:58:29 -07002901 if (CC(!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer)) ||
2902 CC(ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA)) ||
2903 CC(ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)))
Krish Sadhukhan254b2f32018-12-12 13:30:11 -05002904 return -EINVAL;
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002905 }
2906
Sean Christopherson55d23752018-12-03 13:53:18 -08002907 return 0;
2908}
2909
2910static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
2911 struct vmcs12 *vmcs12)
2912{
KarimAllah Ahmed88925302019-01-31 21:24:41 +01002913 int r = 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08002914 struct vmcs12 *shadow;
KarimAllah Ahmed88925302019-01-31 21:24:41 +01002915 struct kvm_host_map map;
Sean Christopherson55d23752018-12-03 13:53:18 -08002916
Yu Zhang64c78502021-09-30 01:51:53 +08002917 if (vmcs12->vmcs_link_pointer == INVALID_GPA)
Sean Christopherson55d23752018-12-03 13:53:18 -08002918 return 0;
2919
Sean Christopherson5497b952019-07-11 08:58:29 -07002920 if (CC(!page_address_valid(vcpu, vmcs12->vmcs_link_pointer)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002921 return -EINVAL;
2922
Sean Christopherson5497b952019-07-11 08:58:29 -07002923 if (CC(kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->vmcs_link_pointer), &map)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002924 return -EINVAL;
2925
KarimAllah Ahmed88925302019-01-31 21:24:41 +01002926 shadow = map.hva;
2927
Sean Christopherson5497b952019-07-11 08:58:29 -07002928 if (CC(shadow->hdr.revision_id != VMCS12_REVISION) ||
2929 CC(shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002930 r = -EINVAL;
KarimAllah Ahmed88925302019-01-31 21:24:41 +01002931
2932 kvm_vcpu_unmap(vcpu, &map, false);
Sean Christopherson55d23752018-12-03 13:53:18 -08002933 return r;
2934}
2935
Sean Christopherson55d23752018-12-03 13:53:18 -08002936/*
2937 * Checks related to Guest Non-register State
2938 */
2939static int nested_check_guest_non_reg_state(struct vmcs12 *vmcs12)
2940{
Sean Christopherson5497b952019-07-11 08:58:29 -07002941 if (CC(vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
Yadong Qibf0cd882020-11-06 14:51:22 +08002942 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT &&
2943 vmcs12->guest_activity_state != GUEST_ACTIVITY_WAIT_SIPI))
Sean Christopherson55d23752018-12-03 13:53:18 -08002944 return -EINVAL;
2945
2946 return 0;
2947}
2948
Sean Christopherson5478ba32019-04-11 12:18:06 -07002949static int nested_vmx_check_guest_state(struct kvm_vcpu *vcpu,
2950 struct vmcs12 *vmcs12,
Sean Christopherson68cda402020-05-11 15:05:29 -07002951 enum vm_entry_failure_code *entry_failure_code)
Sean Christopherson55d23752018-12-03 13:53:18 -08002952{
2953 bool ia32e;
2954
Sean Christopherson68cda402020-05-11 15:05:29 -07002955 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson55d23752018-12-03 13:53:18 -08002956
Sean Christopherson5497b952019-07-11 08:58:29 -07002957 if (CC(!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0)) ||
2958 CC(!nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4)))
Sean Christophersonc80add02019-04-11 12:18:09 -07002959 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002960
Krish Sadhukhanb91991b2020-01-15 19:54:32 -05002961 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) &&
2962 CC(!kvm_dr7_valid(vmcs12->guest_dr7)))
2963 return -EINVAL;
2964
Krish Sadhukhande2bc2b2019-04-08 17:35:12 -04002965 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) &&
Sean Christopherson5497b952019-07-11 08:58:29 -07002966 CC(!kvm_pat_valid(vmcs12->guest_ia32_pat)))
Sean Christophersonc80add02019-04-11 12:18:09 -07002967 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002968
2969 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
Sean Christopherson68cda402020-05-11 15:05:29 -07002970 *entry_failure_code = ENTRY_FAIL_VMCS_LINK_PTR;
Sean Christophersonc80add02019-04-11 12:18:09 -07002971 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002972 }
2973
Oliver Uptonbfc6ad62019-11-13 16:17:16 -08002974 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) &&
2975 CC(!kvm_valid_perf_global_ctrl(vcpu_to_pmu(vcpu),
2976 vmcs12->guest_ia32_perf_global_ctrl)))
2977 return -EINVAL;
2978
Sean Christopherson55d23752018-12-03 13:53:18 -08002979 /*
2980 * If the load IA32_EFER VM-entry control is 1, the following checks
2981 * are performed on the field for the IA32_EFER MSR:
2982 * - Bits reserved in the IA32_EFER MSR must be 0.
2983 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
2984 * the IA-32e mode guest VM-exit control. It must also be identical
2985 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
2986 * CR0.PG) is 1.
2987 */
2988 if (to_vmx(vcpu)->nested.nested_run_pending &&
2989 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
2990 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
Sean Christopherson5497b952019-07-11 08:58:29 -07002991 if (CC(!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer)) ||
2992 CC(ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA)) ||
2993 CC(((vmcs12->guest_cr0 & X86_CR0_PG) &&
2994 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))))
Sean Christophersonc80add02019-04-11 12:18:09 -07002995 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002996 }
2997
2998 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
Sean Christopherson5497b952019-07-11 08:58:29 -07002999 (CC(is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu)) ||
3000 CC((vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD))))
Sean Christophersonc80add02019-04-11 12:18:09 -07003001 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08003002
Sean Christopherson9c3e9222019-04-11 12:18:05 -07003003 if (nested_check_guest_non_reg_state(vmcs12))
Sean Christophersonc80add02019-04-11 12:18:09 -07003004 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08003005
3006 return 0;
3007}
3008
Sean Christopherson453eafb2018-12-20 12:25:17 -08003009static int nested_vmx_check_vmentry_hw(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08003010{
3011 struct vcpu_vmx *vmx = to_vmx(vcpu);
3012 unsigned long cr3, cr4;
Sean Christophersonf1727b42019-01-25 07:40:58 -08003013 bool vm_fail;
Sean Christopherson55d23752018-12-03 13:53:18 -08003014
3015 if (!nested_early_check)
3016 return 0;
3017
3018 if (vmx->msr_autoload.host.nr)
3019 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
3020 if (vmx->msr_autoload.guest.nr)
3021 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
3022
3023 preempt_disable();
3024
3025 vmx_prepare_switch_to_guest(vcpu);
3026
3027 /*
3028 * Induce a consistency check VMExit by clearing bit 1 in GUEST_RFLAGS,
3029 * which is reserved to '1' by hardware. GUEST_RFLAGS is guaranteed to
Miaohe Lin49f933d2020-02-27 11:20:54 +08003030 * be written (by prepare_vmcs02()) before the "real" VMEnter, i.e.
Sean Christopherson55d23752018-12-03 13:53:18 -08003031 * there is no need to preserve other bits or save/restore the field.
3032 */
3033 vmcs_writel(GUEST_RFLAGS, 0);
3034
Sean Christopherson55d23752018-12-03 13:53:18 -08003035 cr3 = __get_current_cr3_fast();
3036 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
3037 vmcs_writel(HOST_CR3, cr3);
3038 vmx->loaded_vmcs->host_state.cr3 = cr3;
3039 }
3040
3041 cr4 = cr4_read_shadow();
3042 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
3043 vmcs_writel(HOST_CR4, cr4);
3044 vmx->loaded_vmcs->host_state.cr4 = cr4;
3045 }
3046
Uros Bizjak150f17b2020-12-30 16:26:57 -08003047 vm_fail = __vmx_vcpu_run(vmx, (unsigned long *)&vcpu->arch.regs,
3048 vmx->loaded_vmcs->launched);
Sean Christopherson55d23752018-12-03 13:53:18 -08003049
Sean Christopherson55d23752018-12-03 13:53:18 -08003050 if (vmx->msr_autoload.host.nr)
3051 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
3052 if (vmx->msr_autoload.guest.nr)
3053 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
3054
Sean Christophersonf1727b42019-01-25 07:40:58 -08003055 if (vm_fail) {
Sean Christopherson380e0052019-07-11 08:58:30 -07003056 u32 error = vmcs_read32(VM_INSTRUCTION_ERROR);
3057
Wanpeng Li541e8862019-05-17 16:49:50 +08003058 preempt_enable();
Sean Christopherson380e0052019-07-11 08:58:30 -07003059
3060 trace_kvm_nested_vmenter_failed(
3061 "early hardware check VM-instruction error: ", error);
3062 WARN_ON_ONCE(error != VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Sean Christopherson55d23752018-12-03 13:53:18 -08003063 return 1;
3064 }
3065
3066 /*
3067 * VMExit clears RFLAGS.IF and DR7, even on a consistency check.
3068 */
Sean Christopherson55d23752018-12-03 13:53:18 -08003069 if (hw_breakpoint_active())
3070 set_debugreg(__this_cpu_read(cpu_dr7), 7);
Peter Zijlstra84b6a342020-05-29 23:27:36 +02003071 local_irq_enable();
Wanpeng Li541e8862019-05-17 16:49:50 +08003072 preempt_enable();
Sean Christopherson55d23752018-12-03 13:53:18 -08003073
3074 /*
3075 * A non-failing VMEntry means we somehow entered guest mode with
3076 * an illegal RIP, and that's just the tip of the iceberg. There
3077 * is no telling what memory has been modified or what state has
3078 * been exposed to unknown code. Hitting this all but guarantees
3079 * a (very critical) hardware issue.
3080 */
3081 WARN_ON(!(vmcs_read32(VM_EXIT_REASON) &
3082 VMX_EXIT_REASONS_FAILED_VMENTRY));
3083
3084 return 0;
3085}
Sean Christopherson55d23752018-12-03 13:53:18 -08003086
Paolo Bonzini9a78e152021-01-08 11:43:08 -05003087static bool nested_get_evmcs_page(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08003088{
Sean Christopherson55d23752018-12-03 13:53:18 -08003089 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08003090
Vitaly Kuznetsove942dbf2020-03-09 16:52:12 +01003091 /*
3092 * hv_evmcs may end up being not mapped after migration (when
3093 * L2 was running), map it here to make sure vmcs12 changes are
3094 * properly reflected.
3095 */
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02003096 if (vmx->nested.enlightened_vmcs_enabled &&
Vitaly Kuznetsov27849962021-05-26 15:20:20 +02003097 vmx->nested.hv_evmcs_vmptr == EVMPTR_MAP_PENDING) {
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003098 enum nested_evmptrld_status evmptrld_status =
3099 nested_vmx_handle_enlightened_vmptrld(vcpu, false);
3100
3101 if (evmptrld_status == EVMPTRLD_VMFAIL ||
Vitaly Kuznetsovf5c7e842021-05-03 17:08:51 +02003102 evmptrld_status == EVMPTRLD_ERROR)
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003103 return false;
Vitaly Kuznetsov8629b622021-05-26 15:20:25 +02003104
3105 /*
3106 * Post migration VMCS12 always provides the most actual
3107 * information, copy it to eVMCS upon entry.
3108 */
3109 vmx->nested.need_vmcs12_to_shadow_sync = true;
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003110 }
Vitaly Kuznetsove942dbf2020-03-09 16:52:12 +01003111
Paolo Bonzini9a78e152021-01-08 11:43:08 -05003112 return true;
3113}
3114
3115static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
3116{
3117 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3118 struct vcpu_vmx *vmx = to_vmx(vcpu);
3119 struct kvm_host_map *map;
3120 struct page *page;
3121 u64 hpa;
3122
Maxim Levitsky158a48e2021-06-07 12:02:03 +03003123 if (!vcpu->arch.pdptrs_from_userspace &&
3124 !nested_cpu_has_ept(vmcs12) && is_pae_paging(vcpu)) {
Maxim Levitsky0f857222021-06-07 12:02:00 +03003125 /*
3126 * Reload the guest's PDPTRs since after a migration
3127 * the guest CR3 might be restored prior to setting the nested
3128 * state which can lead to a load of wrong PDPTRs.
3129 */
3130 if (CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, vcpu->arch.cr3)))
3131 return false;
3132 }
3133
3134
Sean Christopherson55d23752018-12-03 13:53:18 -08003135 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
3136 /*
3137 * Translate L1 physical address to host physical
3138 * address for vmcs02. Keep the page pinned, so this
3139 * physical address remains valid. We keep a reference
3140 * to it so we can release it later.
3141 */
3142 if (vmx->nested.apic_access_page) { /* shouldn't happen */
Liran Alonb11494b2019-11-21 00:31:47 +02003143 kvm_release_page_clean(vmx->nested.apic_access_page);
Sean Christopherson55d23752018-12-03 13:53:18 -08003144 vmx->nested.apic_access_page = NULL;
3145 }
3146 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Sean Christopherson55d23752018-12-03 13:53:18 -08003147 if (!is_error_page(page)) {
3148 vmx->nested.apic_access_page = page;
3149 hpa = page_to_phys(vmx->nested.apic_access_page);
3150 vmcs_write64(APIC_ACCESS_ADDR, hpa);
3151 } else {
Jim Mattson671ddc72019-10-15 10:44:05 -07003152 pr_debug_ratelimited("%s: no backing 'struct page' for APIC-access address in vmcs12\n",
3153 __func__);
3154 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3155 vcpu->run->internal.suberror =
3156 KVM_INTERNAL_ERROR_EMULATION;
3157 vcpu->run->internal.ndata = 0;
3158 return false;
Sean Christopherson55d23752018-12-03 13:53:18 -08003159 }
3160 }
3161
3162 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003163 map = &vmx->nested.virtual_apic_map;
Sean Christopherson55d23752018-12-03 13:53:18 -08003164
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003165 if (!kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->virtual_apic_page_addr), map)) {
3166 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, pfn_to_hpa(map->pfn));
Paolo Bonzini69090812019-04-15 15:16:17 +02003167 } else if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING) &&
3168 nested_cpu_has(vmcs12, CPU_BASED_CR8_STORE_EXITING) &&
3169 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
3170 /*
3171 * The processor will never use the TPR shadow, simply
3172 * clear the bit from the execution control. Such a
3173 * configuration is useless, but it happens in tests.
3174 * For any other configuration, failing the vm entry is
3175 * _not_ what the processor does but it's basically the
3176 * only possibility we have.
3177 */
Sean Christopherson2183f562019-05-07 12:17:56 -07003178 exec_controls_clearbit(vmx, CPU_BASED_TPR_SHADOW);
Paolo Bonzini69090812019-04-15 15:16:17 +02003179 } else {
Sean Christophersonca2f5462019-05-07 09:06:33 -07003180 /*
3181 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR to
3182 * force VM-Entry to fail.
3183 */
Yu Zhang64c78502021-09-30 01:51:53 +08003184 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, INVALID_GPA);
Sean Christopherson55d23752018-12-03 13:53:18 -08003185 }
3186 }
3187
3188 if (nested_cpu_has_posted_intr(vmcs12)) {
KarimAllah Ahmed3278e042019-01-31 21:24:38 +01003189 map = &vmx->nested.pi_desc_map;
3190
3191 if (!kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->posted_intr_desc_addr), map)) {
3192 vmx->nested.pi_desc =
3193 (struct pi_desc *)(((void *)map->hva) +
3194 offset_in_page(vmcs12->posted_intr_desc_addr));
3195 vmcs_write64(POSTED_INTR_DESC_ADDR,
3196 pfn_to_hpa(map->pfn) + offset_in_page(vmcs12->posted_intr_desc_addr));
Jim Mattson966eefb2021-06-04 10:26:06 -07003197 } else {
3198 /*
3199 * Defer the KVM_INTERNAL_EXIT until KVM tries to
3200 * access the contents of the VMCS12 posted interrupt
3201 * descriptor. (Note that KVM may do this when it
3202 * should not, per the architectural specification.)
3203 */
3204 vmx->nested.pi_desc = NULL;
3205 pin_controls_clearbit(vmx, PIN_BASED_POSTED_INTR);
Sean Christopherson55d23752018-12-03 13:53:18 -08003206 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003207 }
3208 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
Sean Christopherson2183f562019-05-07 12:17:56 -07003209 exec_controls_setbit(vmx, CPU_BASED_USE_MSR_BITMAPS);
Sean Christopherson55d23752018-12-03 13:53:18 -08003210 else
Sean Christopherson2183f562019-05-07 12:17:56 -07003211 exec_controls_clearbit(vmx, CPU_BASED_USE_MSR_BITMAPS);
Paolo Bonzini9a78e152021-01-08 11:43:08 -05003212
3213 return true;
3214}
3215
3216static bool vmx_get_nested_state_pages(struct kvm_vcpu *vcpu)
3217{
Vitaly Kuznetsovf5c7e842021-05-03 17:08:51 +02003218 if (!nested_get_evmcs_page(vcpu)) {
3219 pr_debug_ratelimited("%s: enlightened vmptrld failed\n",
3220 __func__);
3221 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3222 vcpu->run->internal.suberror =
3223 KVM_INTERNAL_ERROR_EMULATION;
3224 vcpu->run->internal.ndata = 0;
3225
Paolo Bonzini9a78e152021-01-08 11:43:08 -05003226 return false;
Vitaly Kuznetsovf5c7e842021-05-03 17:08:51 +02003227 }
Paolo Bonzini9a78e152021-01-08 11:43:08 -05003228
3229 if (is_guest_mode(vcpu) && !nested_get_vmcs12_pages(vcpu))
3230 return false;
3231
Jim Mattson671ddc72019-10-15 10:44:05 -07003232 return true;
Sean Christopherson55d23752018-12-03 13:53:18 -08003233}
3234
Sean Christopherson02f5fb22020-06-22 14:58:32 -07003235static int nested_vmx_write_pml_buffer(struct kvm_vcpu *vcpu, gpa_t gpa)
3236{
3237 struct vmcs12 *vmcs12;
3238 struct vcpu_vmx *vmx = to_vmx(vcpu);
3239 gpa_t dst;
3240
3241 if (WARN_ON_ONCE(!is_guest_mode(vcpu)))
3242 return 0;
3243
3244 if (WARN_ON_ONCE(vmx->nested.pml_full))
3245 return 1;
3246
3247 /*
3248 * Check if PML is enabled for the nested guest. Whether eptp bit 6 is
3249 * set is already checked as part of A/D emulation.
3250 */
3251 vmcs12 = get_vmcs12(vcpu);
3252 if (!nested_cpu_has_pml(vmcs12))
3253 return 0;
3254
3255 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
3256 vmx->nested.pml_full = true;
3257 return 1;
3258 }
3259
3260 gpa &= ~0xFFFull;
3261 dst = vmcs12->pml_address + sizeof(u64) * vmcs12->guest_pml_index;
3262
3263 if (kvm_write_guest_page(vcpu->kvm, gpa_to_gfn(dst), &gpa,
3264 offset_in_page(dst), sizeof(gpa)))
3265 return 0;
3266
3267 vmcs12->guest_pml_index--;
3268
3269 return 0;
3270}
3271
Sean Christopherson55d23752018-12-03 13:53:18 -08003272/*
3273 * Intel's VMX Instruction Reference specifies a common set of prerequisites
3274 * for running VMX instructions (except VMXON, whose prerequisites are
3275 * slightly different). It also specifies what exception to inject otherwise.
3276 * Note that many of these exceptions have priority over VM exits, so they
3277 * don't have to be checked again here.
3278 */
3279static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
3280{
3281 if (!to_vmx(vcpu)->nested.vmxon) {
3282 kvm_queue_exception(vcpu, UD_VECTOR);
3283 return 0;
3284 }
3285
3286 if (vmx_get_cpl(vcpu)) {
3287 kvm_inject_gp(vcpu, 0);
3288 return 0;
3289 }
3290
3291 return 1;
3292}
3293
3294static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu)
3295{
3296 u8 rvi = vmx_get_rvi();
3297 u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI);
3298
3299 return ((rvi & 0xf0) > (vppr & 0xf0));
3300}
3301
3302static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
3303 struct vmcs12 *vmcs12);
3304
3305/*
3306 * If from_vmentry is false, this is being called from state restore (either RSM
3307 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
Jim Mattson671ddc72019-10-15 10:44:05 -07003308 *
3309 * Returns:
Miaohe Lin463bfee2020-02-14 10:44:05 +08003310 * NVMX_VMENTRY_SUCCESS: Entered VMX non-root mode
3311 * NVMX_VMENTRY_VMFAIL: Consistency check VMFail
3312 * NVMX_VMENTRY_VMEXIT: Consistency check VMExit
3313 * NVMX_VMENTRY_KVM_INTERNAL_ERROR: KVM internal error
Sean Christopherson55d23752018-12-03 13:53:18 -08003314 */
Jim Mattson671ddc72019-10-15 10:44:05 -07003315enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu,
3316 bool from_vmentry)
Sean Christopherson55d23752018-12-03 13:53:18 -08003317{
3318 struct vcpu_vmx *vmx = to_vmx(vcpu);
3319 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Sean Christopherson68cda402020-05-11 15:05:29 -07003320 enum vm_entry_failure_code entry_failure_code;
Sean Christopherson55d23752018-12-03 13:53:18 -08003321 bool evaluate_pending_interrupts;
Sean Christopherson8e533242020-11-06 17:03:12 +08003322 union vmx_exit_reason exit_reason = {
3323 .basic = EXIT_REASON_INVALID_STATE,
3324 .failed_vmentry = 1,
3325 };
3326 u32 failed_index;
Sean Christopherson55d23752018-12-03 13:53:18 -08003327
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07003328 if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu))
3329 kvm_vcpu_flush_tlb_current(vcpu);
3330
Sean Christopherson2183f562019-05-07 12:17:56 -07003331 evaluate_pending_interrupts = exec_controls_get(vmx) &
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08003332 (CPU_BASED_INTR_WINDOW_EXITING | CPU_BASED_NMI_WINDOW_EXITING);
Sean Christopherson55d23752018-12-03 13:53:18 -08003333 if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu))
3334 evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu);
3335
3336 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
3337 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
3338 if (kvm_mpx_supported() &&
3339 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
3340 vmx->nested.vmcs01_guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
3341
Sean Christophersonf087a022019-06-07 11:55:34 -07003342 /*
3343 * Overwrite vmcs01.GUEST_CR3 with L1's CR3 if EPT is disabled *and*
3344 * nested early checks are disabled. In the event of a "late" VM-Fail,
3345 * i.e. a VM-Fail detected by hardware but not KVM, KVM must unwind its
3346 * software model to the pre-VMEntry host state. When EPT is disabled,
3347 * GUEST_CR3 holds KVM's shadow CR3, not L1's "real" CR3, which causes
3348 * nested_vmx_restore_host_state() to corrupt vcpu->arch.cr3. Stuffing
3349 * vmcs01.GUEST_CR3 results in the unwind naturally setting arch.cr3 to
3350 * the correct value. Smashing vmcs01.GUEST_CR3 is safe because nested
3351 * VM-Exits, and the unwind, reset KVM's MMU, i.e. vmcs01.GUEST_CR3 is
3352 * guaranteed to be overwritten with a shadow CR3 prior to re-entering
3353 * L1. Don't stuff vmcs01.GUEST_CR3 when using nested early checks as
3354 * KVM modifies vcpu->arch.cr3 if and only if the early hardware checks
3355 * pass, and early VM-Fails do not reset KVM's MMU, i.e. the VM-Fail
3356 * path would need to manually save/restore vmcs01.GUEST_CR3.
3357 */
3358 if (!enable_ept && !nested_early_check)
3359 vmcs_writel(GUEST_CR3, vcpu->arch.cr3);
3360
Sean Christopherson55d23752018-12-03 13:53:18 -08003361 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
3362
Sean Christopherson389ab252021-08-10 10:19:50 -07003363 prepare_vmcs02_early(vmx, &vmx->vmcs01, vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08003364
3365 if (from_vmentry) {
Sean Christophersonb89d5ad2020-09-23 11:44:47 -07003366 if (unlikely(!nested_get_vmcs12_pages(vcpu))) {
3367 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson671ddc72019-10-15 10:44:05 -07003368 return NVMX_VMENTRY_KVM_INTERNAL_ERROR;
Sean Christophersonb89d5ad2020-09-23 11:44:47 -07003369 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003370
3371 if (nested_vmx_check_vmentry_hw(vcpu)) {
3372 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson671ddc72019-10-15 10:44:05 -07003373 return NVMX_VMENTRY_VMFAIL;
Sean Christopherson55d23752018-12-03 13:53:18 -08003374 }
3375
Sean Christopherson68cda402020-05-11 15:05:29 -07003376 if (nested_vmx_check_guest_state(vcpu, vmcs12,
3377 &entry_failure_code)) {
Sean Christopherson8e533242020-11-06 17:03:12 +08003378 exit_reason.basic = EXIT_REASON_INVALID_STATE;
Sean Christopherson68cda402020-05-11 15:05:29 -07003379 vmcs12->exit_qualification = entry_failure_code;
Sean Christopherson55d23752018-12-03 13:53:18 -08003380 goto vmentry_fail_vmexit;
Sean Christopherson68cda402020-05-11 15:05:29 -07003381 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003382 }
3383
3384 enter_guest_mode(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08003385
Maxim Levitsky0f857222021-06-07 12:02:00 +03003386 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &entry_failure_code)) {
Sean Christopherson8e533242020-11-06 17:03:12 +08003387 exit_reason.basic = EXIT_REASON_INVALID_STATE;
Sean Christopherson68cda402020-05-11 15:05:29 -07003388 vmcs12->exit_qualification = entry_failure_code;
Sean Christopherson55d23752018-12-03 13:53:18 -08003389 goto vmentry_fail_vmexit_guest_mode;
Sean Christopherson68cda402020-05-11 15:05:29 -07003390 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003391
3392 if (from_vmentry) {
Sean Christopherson68cda402020-05-11 15:05:29 -07003393 failed_index = nested_vmx_load_msr(vcpu,
3394 vmcs12->vm_entry_msr_load_addr,
3395 vmcs12->vm_entry_msr_load_count);
3396 if (failed_index) {
Sean Christopherson8e533242020-11-06 17:03:12 +08003397 exit_reason.basic = EXIT_REASON_MSR_LOAD_FAIL;
Sean Christopherson68cda402020-05-11 15:05:29 -07003398 vmcs12->exit_qualification = failed_index;
Sean Christopherson55d23752018-12-03 13:53:18 -08003399 goto vmentry_fail_vmexit_guest_mode;
Sean Christopherson68cda402020-05-11 15:05:29 -07003400 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003401 } else {
3402 /*
3403 * The MMU is not initialized to point at the right entities yet and
3404 * "get pages" would need to read data from the guest (i.e. we will
3405 * need to perform gpa to hpa translation). Request a call
3406 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
3407 * have already been set at vmentry time and should not be reset.
3408 */
Paolo Bonzini729c15c2020-09-22 06:53:57 -04003409 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08003410 }
3411
3412 /*
3413 * If L1 had a pending IRQ/NMI until it executed
3414 * VMLAUNCH/VMRESUME which wasn't delivered because it was
3415 * disallowed (e.g. interrupts disabled), L0 needs to
3416 * evaluate if this pending event should cause an exit from L2
3417 * to L1 or delivered directly to L2 (e.g. In case L1 don't
3418 * intercept EXTERNAL_INTERRUPT).
3419 *
3420 * Usually this would be handled by the processor noticing an
3421 * IRQ/NMI window request, or checking RVI during evaluation of
3422 * pending virtual interrupts. However, this setting was done
3423 * on VMCS01 and now VMCS02 is active instead. Thus, we force L0
3424 * to perform pending event evaluation by requesting a KVM_REQ_EVENT.
3425 */
3426 if (unlikely(evaluate_pending_interrupts))
3427 kvm_make_request(KVM_REQ_EVENT, vcpu);
3428
3429 /*
Paolo Bonzini359a6c32019-01-29 19:14:46 +01003430 * Do not start the preemption timer hrtimer until after we know
3431 * we are successful, so that only nested_vmx_vmexit needs to cancel
3432 * the timer.
3433 */
3434 vmx->nested.preemption_timer_expired = false;
Peter Shier850448f2020-05-26 14:51:06 -07003435 if (nested_cpu_has_preemption_timer(vmcs12)) {
3436 u64 timer_value = vmx_calc_preemption_timer_value(vcpu);
3437 vmx_start_preemption_timer(vcpu, timer_value);
3438 }
Paolo Bonzini359a6c32019-01-29 19:14:46 +01003439
3440 /*
Sean Christopherson55d23752018-12-03 13:53:18 -08003441 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
3442 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
3443 * returned as far as L1 is concerned. It will only return (and set
3444 * the success flag) when L2 exits (see nested_vmx_vmexit()).
3445 */
Jim Mattson671ddc72019-10-15 10:44:05 -07003446 return NVMX_VMENTRY_SUCCESS;
Sean Christopherson55d23752018-12-03 13:53:18 -08003447
3448 /*
3449 * A failed consistency check that leads to a VMExit during L1's
3450 * VMEnter to L2 is a variation of a normal VMexit, as explained in
3451 * 26.7 "VM-entry failures during or after loading guest state".
3452 */
3453vmentry_fail_vmexit_guest_mode:
Xiaoyao Li5e3d3942019-12-06 16:45:26 +08003454 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING)
Sean Christopherson55d23752018-12-03 13:53:18 -08003455 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
3456 leave_guest_mode(vcpu);
3457
3458vmentry_fail_vmexit:
3459 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
3460
3461 if (!from_vmentry)
Jim Mattson671ddc72019-10-15 10:44:05 -07003462 return NVMX_VMENTRY_VMEXIT;
Sean Christopherson55d23752018-12-03 13:53:18 -08003463
3464 load_vmcs12_host_state(vcpu, vmcs12);
Sean Christopherson8e533242020-11-06 17:03:12 +08003465 vmcs12->vm_exit_reason = exit_reason.full;
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02003466 if (enable_shadow_vmcs || evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
Sean Christopherson3731905ef2019-05-07 08:36:27 -07003467 vmx->nested.need_vmcs12_to_shadow_sync = true;
Jim Mattson671ddc72019-10-15 10:44:05 -07003468 return NVMX_VMENTRY_VMEXIT;
Sean Christopherson55d23752018-12-03 13:53:18 -08003469}
3470
3471/*
3472 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
3473 * for running an L2 nested guest.
3474 */
3475static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
3476{
3477 struct vmcs12 *vmcs12;
Jim Mattson671ddc72019-10-15 10:44:05 -07003478 enum nvmx_vmentry_status status;
Sean Christopherson55d23752018-12-03 13:53:18 -08003479 struct vcpu_vmx *vmx = to_vmx(vcpu);
3480 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003481 enum nested_evmptrld_status evmptrld_status;
Sean Christopherson55d23752018-12-03 13:53:18 -08003482
3483 if (!nested_vmx_check_permission(vcpu))
3484 return 1;
3485
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003486 evmptrld_status = nested_vmx_handle_enlightened_vmptrld(vcpu, launch);
3487 if (evmptrld_status == EVMPTRLD_ERROR) {
3488 kvm_queue_exception(vcpu, UD_VECTOR);
Sean Christopherson55d23752018-12-03 13:53:18 -08003489 return 1;
Sean Christophersonfc595f32020-08-12 11:06:15 -07003490 } else if (CC(evmptrld_status == EVMPTRLD_VMFAIL)) {
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003491 return nested_vmx_failInvalid(vcpu);
3492 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003493
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02003494 if (CC(!evmptr_is_valid(vmx->nested.hv_evmcs_vmptr) &&
Yu Zhang64c78502021-09-30 01:51:53 +08003495 vmx->nested.current_vmptr == INVALID_GPA))
Sean Christopherson55d23752018-12-03 13:53:18 -08003496 return nested_vmx_failInvalid(vcpu);
3497
3498 vmcs12 = get_vmcs12(vcpu);
3499
3500 /*
3501 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
3502 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
3503 * rather than RFLAGS.ZF, and no error number is stored to the
3504 * VM-instruction error field.
3505 */
Sean Christophersonfc595f32020-08-12 11:06:15 -07003506 if (CC(vmcs12->hdr.shadow_vmcs))
Sean Christopherson55d23752018-12-03 13:53:18 -08003507 return nested_vmx_failInvalid(vcpu);
3508
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02003509 if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) {
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02003510 copy_enlightened_to_vmcs12(vmx, vmx->nested.hv_evmcs->hv_clean_fields);
Sean Christopherson55d23752018-12-03 13:53:18 -08003511 /* Enlightened VMCS doesn't have launch state */
3512 vmcs12->launch_state = !launch;
3513 } else if (enable_shadow_vmcs) {
3514 copy_shadow_to_vmcs12(vmx);
3515 }
3516
3517 /*
3518 * The nested entry process starts with enforcing various prerequisites
3519 * on vmcs12 as required by the Intel SDM, and act appropriately when
3520 * they fail: As the SDM explains, some conditions should cause the
3521 * instruction to fail, while others will cause the instruction to seem
3522 * to succeed, but return an EXIT_REASON_INVALID_STATE.
3523 * To speed up the normal (success) code path, we should avoid checking
3524 * for misconfigurations which will anyway be caught by the processor
3525 * when using the merged vmcs02.
3526 */
Sean Christophersonfc595f32020-08-12 11:06:15 -07003527 if (CC(interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS))
Sean Christophersonb2656e42020-06-08 18:56:07 -07003528 return nested_vmx_fail(vcpu, VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
Sean Christopherson55d23752018-12-03 13:53:18 -08003529
Sean Christophersonfc595f32020-08-12 11:06:15 -07003530 if (CC(vmcs12->launch_state == launch))
Sean Christophersonb2656e42020-06-08 18:56:07 -07003531 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08003532 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
3533 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
3534
Paolo Bonzini98d9e852019-04-12 10:19:57 +02003535 if (nested_vmx_check_controls(vcpu, vmcs12))
Sean Christophersonb2656e42020-06-08 18:56:07 -07003536 return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Sean Christopherson5478ba32019-04-11 12:18:06 -07003537
Paolo Bonzini98d9e852019-04-12 10:19:57 +02003538 if (nested_vmx_check_host_state(vcpu, vmcs12))
Sean Christophersonb2656e42020-06-08 18:56:07 -07003539 return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
Sean Christopherson55d23752018-12-03 13:53:18 -08003540
3541 /*
3542 * We're finally done with prerequisite checking, and can start with
3543 * the nested entry.
3544 */
3545 vmx->nested.nested_run_pending = 1;
Peter Shier850448f2020-05-26 14:51:06 -07003546 vmx->nested.has_preemption_timer_deadline = false;
Jim Mattson671ddc72019-10-15 10:44:05 -07003547 status = nested_vmx_enter_non_root_mode(vcpu, true);
3548 if (unlikely(status != NVMX_VMENTRY_SUCCESS))
3549 goto vmentry_failed;
Sean Christopherson55d23752018-12-03 13:53:18 -08003550
Sean Christopherson25bb2cf2020-08-12 10:51:29 -07003551 /* Emulate processing of posted interrupts on VM-Enter. */
3552 if (nested_cpu_has_posted_intr(vmcs12) &&
3553 kvm_apic_has_interrupt(vcpu) == vmx->nested.posted_intr_nv) {
3554 vmx->nested.pi_pending = true;
3555 kvm_make_request(KVM_REQ_EVENT, vcpu);
3556 kvm_apic_clear_irr(vcpu, vmx->nested.posted_intr_nv);
3557 }
3558
Sean Christopherson55d23752018-12-03 13:53:18 -08003559 /* Hide L1D cache contents from the nested guest. */
3560 vmx->vcpu.arch.l1tf_flush_l1d = true;
3561
3562 /*
3563 * Must happen outside of nested_vmx_enter_non_root_mode() as it will
3564 * also be used as part of restoring nVMX state for
3565 * snapshot restore (migration).
3566 *
3567 * In this flow, it is assumed that vmcs12 cache was
Ingo Molnar163b0992021-03-21 22:28:53 +01003568 * transferred as part of captured nVMX state and should
Sean Christopherson55d23752018-12-03 13:53:18 -08003569 * therefore not be read from guest memory (which may not
3570 * exist on destination host yet).
3571 */
3572 nested_cache_shadow_vmcs12(vcpu, vmcs12);
3573
Yadong Qibf0cd882020-11-06 14:51:22 +08003574 switch (vmcs12->guest_activity_state) {
3575 case GUEST_ACTIVITY_HLT:
3576 /*
3577 * If we're entering a halted L2 vcpu and the L2 vcpu won't be
3578 * awakened by event injection or by an NMI-window VM-exit or
3579 * by an interrupt-window VM-exit, halt the vcpu.
3580 */
3581 if (!(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) &&
3582 !nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING) &&
3583 !(nested_cpu_has(vmcs12, CPU_BASED_INTR_WINDOW_EXITING) &&
3584 (vmcs12->guest_rflags & X86_EFLAGS_IF))) {
3585 vmx->nested.nested_run_pending = 0;
3586 return kvm_vcpu_halt(vcpu);
3587 }
3588 break;
3589 case GUEST_ACTIVITY_WAIT_SIPI:
Sean Christopherson55d23752018-12-03 13:53:18 -08003590 vmx->nested.nested_run_pending = 0;
Yadong Qibf0cd882020-11-06 14:51:22 +08003591 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
3592 break;
3593 default:
3594 break;
Sean Christopherson55d23752018-12-03 13:53:18 -08003595 }
Yadong Qibf0cd882020-11-06 14:51:22 +08003596
Sean Christopherson55d23752018-12-03 13:53:18 -08003597 return 1;
Jim Mattson671ddc72019-10-15 10:44:05 -07003598
3599vmentry_failed:
3600 vmx->nested.nested_run_pending = 0;
3601 if (status == NVMX_VMENTRY_KVM_INTERNAL_ERROR)
3602 return 0;
3603 if (status == NVMX_VMENTRY_VMEXIT)
3604 return 1;
3605 WARN_ON_ONCE(status != NVMX_VMENTRY_VMFAIL);
Sean Christophersonb2656e42020-06-08 18:56:07 -07003606 return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Sean Christopherson55d23752018-12-03 13:53:18 -08003607}
3608
3609/*
3610 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
Miaohe Lin67b0ae42019-12-11 14:26:22 +08003611 * because L2 may have changed some cr0 bits directly (CR0_GUEST_HOST_MASK).
Sean Christopherson55d23752018-12-03 13:53:18 -08003612 * This function returns the new value we should put in vmcs12.guest_cr0.
3613 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
3614 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
3615 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
3616 * didn't trap the bit, because if L1 did, so would L0).
3617 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
3618 * been modified by L2, and L1 knows it. So just leave the old value of
3619 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
3620 * isn't relevant, because if L0 traps this bit it can set it to anything.
3621 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
3622 * changed these bits, and therefore they need to be updated, but L0
3623 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
3624 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
3625 */
3626static inline unsigned long
3627vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
3628{
3629 return
3630 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
3631 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
3632 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
3633 vcpu->arch.cr0_guest_owned_bits));
3634}
3635
3636static inline unsigned long
3637vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
3638{
3639 return
3640 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
3641 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
3642 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
3643 vcpu->arch.cr4_guest_owned_bits));
3644}
3645
3646static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
3647 struct vmcs12 *vmcs12)
3648{
3649 u32 idt_vectoring;
3650 unsigned int nr;
3651
3652 if (vcpu->arch.exception.injected) {
3653 nr = vcpu->arch.exception.nr;
3654 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
3655
3656 if (kvm_exception_is_soft(nr)) {
3657 vmcs12->vm_exit_instruction_len =
3658 vcpu->arch.event_exit_inst_len;
3659 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
3660 } else
3661 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
3662
3663 if (vcpu->arch.exception.has_error_code) {
3664 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
3665 vmcs12->idt_vectoring_error_code =
3666 vcpu->arch.exception.error_code;
3667 }
3668
3669 vmcs12->idt_vectoring_info_field = idt_vectoring;
3670 } else if (vcpu->arch.nmi_injected) {
3671 vmcs12->idt_vectoring_info_field =
3672 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
3673 } else if (vcpu->arch.interrupt.injected) {
3674 nr = vcpu->arch.interrupt.nr;
3675 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
3676
3677 if (vcpu->arch.interrupt.soft) {
3678 idt_vectoring |= INTR_TYPE_SOFT_INTR;
3679 vmcs12->vm_entry_instruction_len =
3680 vcpu->arch.event_exit_inst_len;
3681 } else
3682 idt_vectoring |= INTR_TYPE_EXT_INTR;
3683
3684 vmcs12->idt_vectoring_info_field = idt_vectoring;
3685 }
3686}
3687
3688
Paolo Bonzini96b100c2020-03-17 18:32:50 +01003689void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08003690{
3691 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3692 gfn_t gfn;
3693
3694 /*
3695 * Don't need to mark the APIC access page dirty; it is never
3696 * written to by the CPU during APIC virtualization.
3697 */
3698
3699 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
3700 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
3701 kvm_vcpu_mark_page_dirty(vcpu, gfn);
3702 }
3703
3704 if (nested_cpu_has_posted_intr(vmcs12)) {
3705 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
3706 kvm_vcpu_mark_page_dirty(vcpu, gfn);
3707 }
3708}
3709
Jim Mattson650293c2021-06-04 10:26:02 -07003710static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08003711{
3712 struct vcpu_vmx *vmx = to_vmx(vcpu);
3713 int max_irr;
3714 void *vapic_page;
3715 u16 status;
3716
Jim Mattson966eefb2021-06-04 10:26:06 -07003717 if (!vmx->nested.pi_pending)
Jim Mattson650293c2021-06-04 10:26:02 -07003718 return 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08003719
Jim Mattson966eefb2021-06-04 10:26:06 -07003720 if (!vmx->nested.pi_desc)
3721 goto mmio_needed;
3722
Sean Christopherson55d23752018-12-03 13:53:18 -08003723 vmx->nested.pi_pending = false;
Jim Mattson966eefb2021-06-04 10:26:06 -07003724
Sean Christopherson55d23752018-12-03 13:53:18 -08003725 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
Jim Mattson650293c2021-06-04 10:26:02 -07003726 return 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08003727
3728 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
3729 if (max_irr != 256) {
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003730 vapic_page = vmx->nested.virtual_apic_map.hva;
3731 if (!vapic_page)
Jim Mattson0fe998b2021-06-04 10:26:05 -07003732 goto mmio_needed;
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003733
Sean Christopherson55d23752018-12-03 13:53:18 -08003734 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
3735 vapic_page, &max_irr);
Sean Christopherson55d23752018-12-03 13:53:18 -08003736 status = vmcs_read16(GUEST_INTR_STATUS);
3737 if ((u8)max_irr > ((u8)status & 0xff)) {
3738 status &= ~0xff;
3739 status |= (u8)max_irr;
3740 vmcs_write16(GUEST_INTR_STATUS, status);
3741 }
3742 }
3743
3744 nested_mark_vmcs12_pages_dirty(vcpu);
Jim Mattson650293c2021-06-04 10:26:02 -07003745 return 0;
Jim Mattson0fe998b2021-06-04 10:26:05 -07003746
3747mmio_needed:
3748 kvm_handle_memory_failure(vcpu, X86EMUL_IO_NEEDED, NULL);
3749 return -ENXIO;
Sean Christopherson55d23752018-12-03 13:53:18 -08003750}
3751
3752static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3753 unsigned long exit_qual)
3754{
3755 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3756 unsigned int nr = vcpu->arch.exception.nr;
3757 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3758
3759 if (vcpu->arch.exception.has_error_code) {
3760 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3761 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3762 }
3763
3764 if (kvm_exception_is_soft(nr))
3765 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3766 else
3767 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3768
3769 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3770 vmx_get_nmi_mask(vcpu))
3771 intr_info |= INTR_INFO_UNBLOCK_NMI;
3772
3773 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3774}
3775
Oliver Upton684c0422020-02-07 02:36:05 -08003776/*
3777 * Returns true if a debug trap is pending delivery.
3778 *
3779 * In KVM, debug traps bear an exception payload. As such, the class of a #DB
3780 * exception may be inferred from the presence of an exception payload.
3781 */
3782static inline bool vmx_pending_dbg_trap(struct kvm_vcpu *vcpu)
3783{
3784 return vcpu->arch.exception.pending &&
3785 vcpu->arch.exception.nr == DB_VECTOR &&
3786 vcpu->arch.exception.payload;
3787}
3788
3789/*
3790 * Certain VM-exits set the 'pending debug exceptions' field to indicate a
3791 * recognized #DB (data or single-step) that has yet to be delivered. Since KVM
3792 * represents these debug traps with a payload that is said to be compatible
3793 * with the 'pending debug exceptions' field, write the payload to the VMCS
3794 * field if a VM-exit is delivered before the debug trap.
3795 */
3796static void nested_vmx_update_pending_dbg(struct kvm_vcpu *vcpu)
3797{
3798 if (vmx_pending_dbg_trap(vcpu))
3799 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
3800 vcpu->arch.exception.payload);
3801}
3802
Sean Christophersond2060bd2020-04-22 19:25:39 -07003803static bool nested_vmx_preemption_timer_pending(struct kvm_vcpu *vcpu)
3804{
3805 return nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
3806 to_vmx(vcpu)->nested.preemption_timer_expired;
3807}
3808
Sean Christophersona1c77ab2020-03-02 22:27:35 -08003809static int vmx_check_nested_events(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08003810{
3811 struct vcpu_vmx *vmx = to_vmx(vcpu);
3812 unsigned long exit_qual;
3813 bool block_nested_events =
3814 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003815 bool mtf_pending = vmx->nested.mtf_pending;
Liran Alon4b9852f2019-08-26 13:24:49 +03003816 struct kvm_lapic *apic = vcpu->arch.apic;
3817
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003818 /*
3819 * Clear the MTF state. If a higher priority VM-exit is delivered first,
3820 * this state is discarded.
3821 */
Oliver Upton5c8beb42020-04-06 20:12:37 +00003822 if (!block_nested_events)
3823 vmx->nested.mtf_pending = false;
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003824
Liran Alon4b9852f2019-08-26 13:24:49 +03003825 if (lapic_in_kernel(vcpu) &&
3826 test_bit(KVM_APIC_INIT, &apic->pending_events)) {
3827 if (block_nested_events)
3828 return -EBUSY;
Oliver Upton684c0422020-02-07 02:36:05 -08003829 nested_vmx_update_pending_dbg(vcpu);
Liran Alone64a8502019-11-11 14:16:05 +02003830 clear_bit(KVM_APIC_INIT, &apic->pending_events);
Yadong Qibf0cd882020-11-06 14:51:22 +08003831 if (vcpu->arch.mp_state != KVM_MP_STATE_INIT_RECEIVED)
3832 nested_vmx_vmexit(vcpu, EXIT_REASON_INIT_SIGNAL, 0, 0);
3833 return 0;
3834 }
3835
3836 if (lapic_in_kernel(vcpu) &&
3837 test_bit(KVM_APIC_SIPI, &apic->pending_events)) {
3838 if (block_nested_events)
3839 return -EBUSY;
3840
3841 clear_bit(KVM_APIC_SIPI, &apic->pending_events);
3842 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED)
3843 nested_vmx_vmexit(vcpu, EXIT_REASON_SIPI_SIGNAL, 0,
3844 apic->sipi_vector & 0xFFUL);
Liran Alon4b9852f2019-08-26 13:24:49 +03003845 return 0;
3846 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003847
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003848 /*
3849 * Process any exceptions that are not debug traps before MTF.
Maxim Levitsky4020da32021-04-01 17:38:14 +03003850 *
3851 * Note that only a pending nested run can block a pending exception.
3852 * Otherwise an injected NMI/interrupt should either be
3853 * lost or delivered to the nested hypervisor in the IDT_VECTORING_INFO,
3854 * while delivering the pending exception.
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003855 */
Maxim Levitsky4020da32021-04-01 17:38:14 +03003856
Sean Christopherson6ce347a2020-04-22 19:25:38 -07003857 if (vcpu->arch.exception.pending && !vmx_pending_dbg_trap(vcpu)) {
Maxim Levitsky4020da32021-04-01 17:38:14 +03003858 if (vmx->nested.nested_run_pending)
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003859 return -EBUSY;
Sean Christopherson6ce347a2020-04-22 19:25:38 -07003860 if (!nested_vmx_check_exception(vcpu, &exit_qual))
3861 goto no_vmexit;
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003862 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
3863 return 0;
3864 }
3865
3866 if (mtf_pending) {
3867 if (block_nested_events)
3868 return -EBUSY;
3869 nested_vmx_update_pending_dbg(vcpu);
3870 nested_vmx_vmexit(vcpu, EXIT_REASON_MONITOR_TRAP_FLAG, 0, 0);
3871 return 0;
3872 }
3873
Sean Christopherson6ce347a2020-04-22 19:25:38 -07003874 if (vcpu->arch.exception.pending) {
Maxim Levitsky4020da32021-04-01 17:38:14 +03003875 if (vmx->nested.nested_run_pending)
Sean Christopherson55d23752018-12-03 13:53:18 -08003876 return -EBUSY;
Sean Christopherson6ce347a2020-04-22 19:25:38 -07003877 if (!nested_vmx_check_exception(vcpu, &exit_qual))
3878 goto no_vmexit;
Sean Christopherson55d23752018-12-03 13:53:18 -08003879 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
3880 return 0;
3881 }
3882
Sean Christophersond2060bd2020-04-22 19:25:39 -07003883 if (nested_vmx_preemption_timer_pending(vcpu)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08003884 if (block_nested_events)
3885 return -EBUSY;
3886 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
3887 return 0;
3888 }
3889
Sean Christopherson1cd2f0b2020-04-22 19:25:46 -07003890 if (vcpu->arch.smi_pending && !is_smm(vcpu)) {
3891 if (block_nested_events)
3892 return -EBUSY;
3893 goto no_vmexit;
3894 }
3895
Sean Christopherson15ff0b42020-04-22 19:25:45 -07003896 if (vcpu->arch.nmi_pending && !vmx_nmi_blocked(vcpu)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08003897 if (block_nested_events)
3898 return -EBUSY;
Sean Christopherson15ff0b42020-04-22 19:25:45 -07003899 if (!nested_exit_on_nmi(vcpu))
3900 goto no_vmexit;
3901
Sean Christopherson55d23752018-12-03 13:53:18 -08003902 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
3903 NMI_VECTOR | INTR_TYPE_NMI_INTR |
3904 INTR_INFO_VALID_MASK, 0);
3905 /*
3906 * The NMI-triggered VM exit counts as injection:
3907 * clear this one and block further NMIs.
3908 */
3909 vcpu->arch.nmi_pending = 0;
3910 vmx_set_nmi_mask(vcpu, true);
3911 return 0;
3912 }
3913
Sean Christopherson15ff0b42020-04-22 19:25:45 -07003914 if (kvm_cpu_has_interrupt(vcpu) && !vmx_interrupt_blocked(vcpu)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08003915 if (block_nested_events)
3916 return -EBUSY;
Sean Christopherson15ff0b42020-04-22 19:25:45 -07003917 if (!nested_exit_on_intr(vcpu))
3918 goto no_vmexit;
Sean Christopherson55d23752018-12-03 13:53:18 -08003919 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
3920 return 0;
3921 }
3922
Sean Christopherson6ce347a2020-04-22 19:25:38 -07003923no_vmexit:
Jim Mattson650293c2021-06-04 10:26:02 -07003924 return vmx_complete_nested_posted_interrupt(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08003925}
3926
3927static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
3928{
3929 ktime_t remaining =
3930 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
3931 u64 value;
3932
3933 if (ktime_to_ns(remaining) <= 0)
3934 return 0;
3935
3936 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
3937 do_div(value, 1000000);
3938 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
3939}
3940
Sean Christopherson7952d762019-05-07 08:36:29 -07003941static bool is_vmcs12_ext_field(unsigned long field)
Sean Christopherson55d23752018-12-03 13:53:18 -08003942{
Sean Christopherson7952d762019-05-07 08:36:29 -07003943 switch (field) {
3944 case GUEST_ES_SELECTOR:
3945 case GUEST_CS_SELECTOR:
3946 case GUEST_SS_SELECTOR:
3947 case GUEST_DS_SELECTOR:
3948 case GUEST_FS_SELECTOR:
3949 case GUEST_GS_SELECTOR:
3950 case GUEST_LDTR_SELECTOR:
3951 case GUEST_TR_SELECTOR:
3952 case GUEST_ES_LIMIT:
3953 case GUEST_CS_LIMIT:
3954 case GUEST_SS_LIMIT:
3955 case GUEST_DS_LIMIT:
3956 case GUEST_FS_LIMIT:
3957 case GUEST_GS_LIMIT:
3958 case GUEST_LDTR_LIMIT:
3959 case GUEST_TR_LIMIT:
3960 case GUEST_GDTR_LIMIT:
3961 case GUEST_IDTR_LIMIT:
3962 case GUEST_ES_AR_BYTES:
3963 case GUEST_DS_AR_BYTES:
3964 case GUEST_FS_AR_BYTES:
3965 case GUEST_GS_AR_BYTES:
3966 case GUEST_LDTR_AR_BYTES:
3967 case GUEST_TR_AR_BYTES:
3968 case GUEST_ES_BASE:
3969 case GUEST_CS_BASE:
3970 case GUEST_SS_BASE:
3971 case GUEST_DS_BASE:
3972 case GUEST_FS_BASE:
3973 case GUEST_GS_BASE:
3974 case GUEST_LDTR_BASE:
3975 case GUEST_TR_BASE:
3976 case GUEST_GDTR_BASE:
3977 case GUEST_IDTR_BASE:
3978 case GUEST_PENDING_DBG_EXCEPTIONS:
3979 case GUEST_BNDCFGS:
3980 return true;
3981 default:
3982 break;
3983 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003984
Sean Christopherson7952d762019-05-07 08:36:29 -07003985 return false;
3986}
3987
3988static void sync_vmcs02_to_vmcs12_rare(struct kvm_vcpu *vcpu,
3989 struct vmcs12 *vmcs12)
3990{
3991 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08003992
3993 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
3994 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
3995 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
3996 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
3997 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
3998 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
3999 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
4000 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
4001 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
4002 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
4003 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
4004 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
4005 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
4006 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
4007 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
4008 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
4009 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
4010 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
4011 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
Sean Christopherson55d23752018-12-03 13:53:18 -08004012 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
4013 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
4014 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
4015 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
4016 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
4017 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
4018 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
4019 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
4020 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
4021 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
4022 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
4023 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
4024 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
4025 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
4026 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
Sean Christopherson7952d762019-05-07 08:36:29 -07004027 vmcs12->guest_pending_dbg_exceptions =
4028 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
4029 if (kvm_mpx_supported())
4030 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
4031
4032 vmx->nested.need_sync_vmcs02_to_vmcs12_rare = false;
4033}
4034
4035static void copy_vmcs02_to_vmcs12_rare(struct kvm_vcpu *vcpu,
4036 struct vmcs12 *vmcs12)
4037{
4038 struct vcpu_vmx *vmx = to_vmx(vcpu);
4039 int cpu;
4040
4041 if (!vmx->nested.need_sync_vmcs02_to_vmcs12_rare)
4042 return;
4043
4044
4045 WARN_ON_ONCE(vmx->loaded_vmcs != &vmx->vmcs01);
4046
4047 cpu = get_cpu();
4048 vmx->loaded_vmcs = &vmx->nested.vmcs02;
Sean Christopherson1af1bb02020-05-06 16:58:50 -07004049 vmx_vcpu_load_vmcs(vcpu, cpu, &vmx->vmcs01);
Sean Christopherson7952d762019-05-07 08:36:29 -07004050
4051 sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
4052
4053 vmx->loaded_vmcs = &vmx->vmcs01;
Sean Christopherson1af1bb02020-05-06 16:58:50 -07004054 vmx_vcpu_load_vmcs(vcpu, cpu, &vmx->nested.vmcs02);
Sean Christopherson7952d762019-05-07 08:36:29 -07004055 put_cpu();
4056}
4057
4058/*
4059 * Update the guest state fields of vmcs12 to reflect changes that
4060 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
4061 * VM-entry controls is also updated, since this is really a guest
4062 * state bit.)
4063 */
4064static void sync_vmcs02_to_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
4065{
4066 struct vcpu_vmx *vmx = to_vmx(vcpu);
4067
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02004068 if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
Sean Christopherson7952d762019-05-07 08:36:29 -07004069 sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
4070
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02004071 vmx->nested.need_sync_vmcs02_to_vmcs12_rare =
4072 !evmptr_is_valid(vmx->nested.hv_evmcs_vmptr);
Sean Christopherson7952d762019-05-07 08:36:29 -07004073
4074 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
4075 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
4076
4077 vmcs12->guest_rsp = kvm_rsp_read(vcpu);
4078 vmcs12->guest_rip = kvm_rip_read(vcpu);
4079 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
4080
4081 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
4082 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
Sean Christopherson55d23752018-12-03 13:53:18 -08004083
4084 vmcs12->guest_interruptibility_info =
4085 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
Sean Christopherson7952d762019-05-07 08:36:29 -07004086
Sean Christopherson55d23752018-12-03 13:53:18 -08004087 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
4088 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
Yadong Qibf0cd882020-11-06 14:51:22 +08004089 else if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED)
4090 vmcs12->guest_activity_state = GUEST_ACTIVITY_WAIT_SIPI;
Sean Christopherson55d23752018-12-03 13:53:18 -08004091 else
4092 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
4093
Paolo Bonzinib4b65b52019-01-29 19:12:35 +01004094 if (nested_cpu_has_preemption_timer(vmcs12) &&
Peter Shier850448f2020-05-26 14:51:06 -07004095 vmcs12->vm_exit_controls & VM_EXIT_SAVE_VMX_PREEMPTION_TIMER &&
4096 !vmx->nested.nested_run_pending)
4097 vmcs12->vmx_preemption_timer_value =
4098 vmx_get_preemption_timer_value(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08004099
4100 /*
4101 * In some cases (usually, nested EPT), L2 is allowed to change its
4102 * own CR3 without exiting. If it has changed it, we must keep it.
4103 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
4104 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
4105 *
4106 * Additionally, restore L2's PDPTR to vmcs12.
4107 */
4108 if (enable_ept) {
4109 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Sean Christophersonc7554efc2019-05-07 09:06:40 -07004110 if (nested_cpu_has_ept(vmcs12) && is_pae_paging(vcpu)) {
4111 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
4112 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
4113 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
4114 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
4115 }
Sean Christopherson55d23752018-12-03 13:53:18 -08004116 }
4117
4118 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
4119
4120 if (nested_cpu_has_vid(vmcs12))
4121 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
4122
4123 vmcs12->vm_entry_controls =
4124 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
4125 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
4126
Sean Christopherson699a1ac2019-05-07 09:06:37 -07004127 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS)
Sean Christopherson55d23752018-12-03 13:53:18 -08004128 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
Sean Christopherson55d23752018-12-03 13:53:18 -08004129
Sean Christopherson55d23752018-12-03 13:53:18 -08004130 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
4131 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Sean Christopherson55d23752018-12-03 13:53:18 -08004132}
4133
4134/*
4135 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
4136 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
4137 * and this function updates it to reflect the changes to the guest state while
4138 * L2 was running (and perhaps made some exits which were handled directly by L0
4139 * without going back to L1), and to reflect the exit reason.
4140 * Note that we do not have to copy here all VMCS fields, just those that
4141 * could have changed by the L2 guest or the exit - i.e., the guest-state and
4142 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
4143 * which already writes to vmcs12 directly.
4144 */
4145static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004146 u32 vm_exit_reason, u32 exit_intr_info,
Sean Christopherson55d23752018-12-03 13:53:18 -08004147 unsigned long exit_qualification)
4148{
Sean Christopherson55d23752018-12-03 13:53:18 -08004149 /* update exit information fields: */
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004150 vmcs12->vm_exit_reason = vm_exit_reason;
Sean Christopherson3c0c2ad2021-04-12 16:21:37 +12004151 if (to_vmx(vcpu)->exit_reason.enclave_mode)
4152 vmcs12->vm_exit_reason |= VMX_EXIT_REASONS_SGX_ENCLAVE_MODE;
Sean Christopherson55d23752018-12-03 13:53:18 -08004153 vmcs12->exit_qualification = exit_qualification;
4154 vmcs12->vm_exit_intr_info = exit_intr_info;
4155
4156 vmcs12->idt_vectoring_info_field = 0;
4157 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
4158 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
4159
4160 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
4161 vmcs12->launch_state = 1;
4162
4163 /* vm_entry_intr_info_field is cleared on exit. Emulate this
4164 * instead of reading the real value. */
4165 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
4166
4167 /*
4168 * Transfer the event that L0 or L1 may wanted to inject into
4169 * L2 to IDT_VECTORING_INFO_FIELD.
4170 */
4171 vmcs12_save_pending_event(vcpu, vmcs12);
Krish Sadhukhana0d4f802018-12-04 19:00:13 -05004172
4173 /*
4174 * According to spec, there's no need to store the guest's
4175 * MSRs if the exit is due to a VM-entry failure that occurs
4176 * during or after loading the guest state. Since this exit
4177 * does not fall in that category, we need to save the MSRs.
4178 */
4179 if (nested_vmx_store_msr(vcpu,
4180 vmcs12->vm_exit_msr_store_addr,
4181 vmcs12->vm_exit_msr_store_count))
4182 nested_vmx_abort(vcpu,
4183 VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Sean Christopherson55d23752018-12-03 13:53:18 -08004184 }
4185
4186 /*
4187 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
4188 * preserved above and would only end up incorrectly in L1.
4189 */
4190 vcpu->arch.nmi_injected = false;
4191 kvm_clear_exception_queue(vcpu);
4192 kvm_clear_interrupt_queue(vcpu);
4193}
4194
4195/*
4196 * A part of what we need to when the nested L2 guest exits and we want to
4197 * run its L1 parent, is to reset L1's guest state to the host state specified
4198 * in vmcs12.
4199 * This function is to be called not only on normal nested exit, but also on
4200 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
4201 * Failures During or After Loading Guest State").
4202 * This function should be called when the active VMCS is L1's (vmcs01).
4203 */
4204static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
4205 struct vmcs12 *vmcs12)
4206{
Sean Christopherson68cda402020-05-11 15:05:29 -07004207 enum vm_entry_failure_code ignored;
Sean Christopherson55d23752018-12-03 13:53:18 -08004208 struct kvm_segment seg;
Sean Christopherson55d23752018-12-03 13:53:18 -08004209
4210 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
4211 vcpu->arch.efer = vmcs12->host_ia32_efer;
4212 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
4213 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
4214 else
4215 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
4216 vmx_set_efer(vcpu, vcpu->arch.efer);
4217
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02004218 kvm_rsp_write(vcpu, vmcs12->host_rsp);
4219 kvm_rip_write(vcpu, vmcs12->host_rip);
Sean Christopherson55d23752018-12-03 13:53:18 -08004220 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
4221 vmx_set_interrupt_shadow(vcpu, 0);
4222
4223 /*
4224 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
4225 * actually changed, because vmx_set_cr0 refers to efer set above.
4226 *
4227 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
4228 * (KVM doesn't change it);
4229 */
Sean Christophersonfa71e952020-07-02 21:04:22 -07004230 vcpu->arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS;
Sean Christopherson55d23752018-12-03 13:53:18 -08004231 vmx_set_cr0(vcpu, vmcs12->host_cr0);
4232
4233 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
4234 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
4235 vmx_set_cr4(vcpu, vmcs12->host_cr4);
4236
4237 nested_ept_uninit_mmu_context(vcpu);
4238
4239 /*
4240 * Only PDPTE load can fail as the value of cr3 was checked on entry and
4241 * couldn't have changed.
4242 */
Maxim Levitsky0f857222021-06-07 12:02:00 +03004243 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, true, &ignored))
Sean Christopherson55d23752018-12-03 13:53:18 -08004244 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
4245
Sean Christopherson50b265a2020-03-20 14:28:19 -07004246 nested_vmx_transition_tlb_flush(vcpu, vmcs12, false);
Sean Christopherson55d23752018-12-03 13:53:18 -08004247
4248 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
4249 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
4250 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
4251 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
4252 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
4253 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
4254 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
4255
4256 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
4257 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
4258 vmcs_write64(GUEST_BNDCFGS, 0);
4259
4260 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
4261 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
4262 vcpu->arch.pat = vmcs12->host_ia32_pat;
4263 }
4264 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Oliver Uptond1968422019-12-13 16:33:58 -08004265 WARN_ON_ONCE(kvm_set_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL,
4266 vmcs12->host_ia32_perf_global_ctrl));
Sean Christopherson55d23752018-12-03 13:53:18 -08004267
4268 /* Set L1 segment info according to Intel SDM
4269 27.5.2 Loading Host Segment and Descriptor-Table Registers */
4270 seg = (struct kvm_segment) {
4271 .base = 0,
4272 .limit = 0xFFFFFFFF,
4273 .selector = vmcs12->host_cs_selector,
4274 .type = 11,
4275 .present = 1,
4276 .s = 1,
4277 .g = 1
4278 };
4279 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
4280 seg.l = 1;
4281 else
4282 seg.db = 1;
Sean Christopherson816be9e2021-07-13 09:33:07 -07004283 __vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
Sean Christopherson55d23752018-12-03 13:53:18 -08004284 seg = (struct kvm_segment) {
4285 .base = 0,
4286 .limit = 0xFFFFFFFF,
4287 .type = 3,
4288 .present = 1,
4289 .s = 1,
4290 .db = 1,
4291 .g = 1
4292 };
4293 seg.selector = vmcs12->host_ds_selector;
Sean Christopherson816be9e2021-07-13 09:33:07 -07004294 __vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
Sean Christopherson55d23752018-12-03 13:53:18 -08004295 seg.selector = vmcs12->host_es_selector;
Sean Christopherson816be9e2021-07-13 09:33:07 -07004296 __vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
Sean Christopherson55d23752018-12-03 13:53:18 -08004297 seg.selector = vmcs12->host_ss_selector;
Sean Christopherson816be9e2021-07-13 09:33:07 -07004298 __vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
Sean Christopherson55d23752018-12-03 13:53:18 -08004299 seg.selector = vmcs12->host_fs_selector;
4300 seg.base = vmcs12->host_fs_base;
Sean Christopherson816be9e2021-07-13 09:33:07 -07004301 __vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
Sean Christopherson55d23752018-12-03 13:53:18 -08004302 seg.selector = vmcs12->host_gs_selector;
4303 seg.base = vmcs12->host_gs_base;
Sean Christopherson816be9e2021-07-13 09:33:07 -07004304 __vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
Sean Christopherson55d23752018-12-03 13:53:18 -08004305 seg = (struct kvm_segment) {
4306 .base = vmcs12->host_tr_base,
4307 .limit = 0x67,
4308 .selector = vmcs12->host_tr_selector,
4309 .type = 11,
4310 .present = 1
4311 };
Sean Christopherson816be9e2021-07-13 09:33:07 -07004312 __vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
Sean Christopherson55d23752018-12-03 13:53:18 -08004313
Sean Christophersonafc8de02021-07-13 09:32:40 -07004314 memset(&seg, 0, sizeof(seg));
4315 seg.unusable = 1;
Sean Christopherson816be9e2021-07-13 09:33:07 -07004316 __vmx_set_segment(vcpu, &seg, VCPU_SREG_LDTR);
Sean Christopherson55d23752018-12-03 13:53:18 -08004317
4318 kvm_set_dr(vcpu, 7, 0x400);
4319 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
4320
Sean Christopherson55d23752018-12-03 13:53:18 -08004321 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
4322 vmcs12->vm_exit_msr_load_count))
4323 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Maxim Levitskydbab6102021-09-13 17:09:54 +03004324
4325 to_vmx(vcpu)->emulation_required = vmx_emulation_required(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08004326}
4327
4328static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx)
4329{
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07004330 struct vmx_uret_msr *efer_msr;
Sean Christopherson55d23752018-12-03 13:53:18 -08004331 unsigned int i;
4332
4333 if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER)
4334 return vmcs_read64(GUEST_IA32_EFER);
4335
4336 if (cpu_has_load_ia32_efer())
4337 return host_efer;
4338
4339 for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) {
4340 if (vmx->msr_autoload.guest.val[i].index == MSR_EFER)
4341 return vmx->msr_autoload.guest.val[i].value;
4342 }
4343
Sean Christophersond85a8032020-09-23 11:04:06 -07004344 efer_msr = vmx_find_uret_msr(vmx, MSR_EFER);
Sean Christopherson55d23752018-12-03 13:53:18 -08004345 if (efer_msr)
4346 return efer_msr->data;
4347
4348 return host_efer;
4349}
4350
4351static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu)
4352{
4353 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4354 struct vcpu_vmx *vmx = to_vmx(vcpu);
4355 struct vmx_msr_entry g, h;
Sean Christopherson55d23752018-12-03 13:53:18 -08004356 gpa_t gpa;
4357 u32 i, j;
4358
4359 vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT);
4360
4361 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
4362 /*
4363 * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set
4364 * as vmcs01.GUEST_DR7 contains a userspace defined value
4365 * and vcpu->arch.dr7 is not squirreled away before the
4366 * nested VMENTER (not worth adding a variable in nested_vmx).
4367 */
4368 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
4369 kvm_set_dr(vcpu, 7, DR7_FIXED_1);
4370 else
4371 WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7)));
4372 }
4373
4374 /*
4375 * Note that calling vmx_set_{efer,cr0,cr4} is important as they
4376 * handle a variety of side effects to KVM's software model.
4377 */
4378 vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx));
4379
Sean Christophersonfa71e952020-07-02 21:04:22 -07004380 vcpu->arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS;
Sean Christopherson55d23752018-12-03 13:53:18 -08004381 vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW));
4382
4383 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
4384 vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW));
4385
4386 nested_ept_uninit_mmu_context(vcpu);
Sean Christophersonf087a022019-06-07 11:55:34 -07004387 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07004388 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Sean Christopherson55d23752018-12-03 13:53:18 -08004389
4390 /*
4391 * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs
4392 * from vmcs01 (if necessary). The PDPTRs are not loaded on
4393 * VMFail, like everything else we just need to ensure our
4394 * software model is up-to-date.
4395 */
Sean Christopherson9932b492020-04-15 13:34:50 -07004396 if (enable_ept && is_pae_paging(vcpu))
Sean Christophersonf087a022019-06-07 11:55:34 -07004397 ept_save_pdptrs(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08004398
4399 kvm_mmu_reset_context(vcpu);
4400
Sean Christopherson55d23752018-12-03 13:53:18 -08004401 /*
4402 * This nasty bit of open coding is a compromise between blindly
4403 * loading L1's MSRs using the exit load lists (incorrect emulation
4404 * of VMFail), leaving the nested VM's MSRs in the software model
4405 * (incorrect behavior) and snapshotting the modified MSRs (too
4406 * expensive since the lists are unbound by hardware). For each
4407 * MSR that was (prematurely) loaded from the nested VMEntry load
4408 * list, reload it from the exit load list if it exists and differs
4409 * from the guest value. The intent is to stuff host state as
4410 * silently as possible, not to fully process the exit load list.
4411 */
Sean Christopherson55d23752018-12-03 13:53:18 -08004412 for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) {
4413 gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g));
4414 if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) {
4415 pr_debug_ratelimited(
4416 "%s read MSR index failed (%u, 0x%08llx)\n",
4417 __func__, i, gpa);
4418 goto vmabort;
4419 }
4420
4421 for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) {
4422 gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h));
4423 if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) {
4424 pr_debug_ratelimited(
4425 "%s read MSR failed (%u, 0x%08llx)\n",
4426 __func__, j, gpa);
4427 goto vmabort;
4428 }
4429 if (h.index != g.index)
4430 continue;
4431 if (h.value == g.value)
4432 break;
4433
4434 if (nested_vmx_load_msr_check(vcpu, &h)) {
4435 pr_debug_ratelimited(
4436 "%s check failed (%u, 0x%x, 0x%x)\n",
4437 __func__, j, h.index, h.reserved);
4438 goto vmabort;
4439 }
4440
Sean Christophersonf20935d2019-09-05 14:22:54 -07004441 if (kvm_set_msr(vcpu, h.index, h.value)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08004442 pr_debug_ratelimited(
4443 "%s WRMSR failed (%u, 0x%x, 0x%llx)\n",
4444 __func__, j, h.index, h.value);
4445 goto vmabort;
4446 }
4447 }
4448 }
4449
4450 return;
4451
4452vmabort:
4453 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
4454}
4455
4456/*
4457 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
4458 * and modify vmcs12 to make it see what it would expect to see there if
4459 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
4460 */
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004461void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason,
Sean Christopherson55d23752018-12-03 13:53:18 -08004462 u32 exit_intr_info, unsigned long exit_qualification)
4463{
4464 struct vcpu_vmx *vmx = to_vmx(vcpu);
4465 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4466
4467 /* trying to cancel vmlaunch/vmresume is a bug */
4468 WARN_ON_ONCE(vmx->nested.nested_run_pending);
4469
Sean Christophersoncb6a32c2021-03-02 09:45:14 -08004470 /* Similarly, triple faults in L2 should never escape. */
4471 WARN_ON_ONCE(kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu));
4472
Vitaly Kuznetsovf5c7e842021-05-03 17:08:51 +02004473 if (kvm_check_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu)) {
4474 /*
4475 * KVM_REQ_GET_NESTED_STATE_PAGES is also used to map
4476 * Enlightened VMCS after migration and we still need to
4477 * do that when something is forcing L2->L1 exit prior to
4478 * the first L2 run.
4479 */
4480 (void)nested_get_evmcs_page(vcpu);
4481 }
Maxim Levitskyf2c7ef32021-01-07 11:38:51 +02004482
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07004483 /* Service the TLB flush request for L2 before switching to L1. */
4484 if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu))
4485 kvm_vcpu_flush_tlb_current(vcpu);
4486
Peter Shier43fea4e2020-08-20 16:05:45 -07004487 /*
4488 * VCPU_EXREG_PDPTR will be clobbered in arch/x86/kvm/vmx/vmx.h between
4489 * now and the new vmentry. Ensure that the VMCS02 PDPTR fields are
4490 * up-to-date before switching to L1.
4491 */
4492 if (enable_ept && is_pae_paging(vcpu))
4493 vmx_ept_load_pdptrs(vcpu);
4494
Sean Christopherson55d23752018-12-03 13:53:18 -08004495 leave_guest_mode(vcpu);
4496
Paolo Bonzinib4b65b52019-01-29 19:12:35 +01004497 if (nested_cpu_has_preemption_timer(vmcs12))
4498 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
4499
Ilias Stamatisd041b5e2021-05-26 19:44:17 +01004500 if (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETTING)) {
4501 vcpu->arch.tsc_offset = vcpu->arch.l1_tsc_offset;
4502 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_TSC_SCALING))
4503 vcpu->arch.tsc_scaling_ratio = vcpu->arch.l1_tsc_scaling_ratio;
4504 }
Sean Christopherson55d23752018-12-03 13:53:18 -08004505
4506 if (likely(!vmx->fail)) {
Sean Christopherson3731905ef2019-05-07 08:36:27 -07004507 sync_vmcs02_to_vmcs12(vcpu, vmcs12);
Sean Christophersonf4f83162019-05-07 08:36:26 -07004508
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004509 if (vm_exit_reason != -1)
4510 prepare_vmcs12(vcpu, vmcs12, vm_exit_reason,
4511 exit_intr_info, exit_qualification);
Sean Christopherson55d23752018-12-03 13:53:18 -08004512
4513 /*
Sean Christopherson3731905ef2019-05-07 08:36:27 -07004514 * Must happen outside of sync_vmcs02_to_vmcs12() as it will
Sean Christopherson55d23752018-12-03 13:53:18 -08004515 * also be used to capture vmcs12 cache as part of
4516 * capturing nVMX state for snapshot (migration).
4517 *
4518 * Otherwise, this flush will dirty guest memory at a
4519 * point it is already assumed by user-space to be
4520 * immutable.
4521 */
4522 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08004523 } else {
4524 /*
4525 * The only expected VM-instruction error is "VM entry with
4526 * invalid control field(s)." Anything else indicates a
4527 * problem with L0. And we should never get here with a
4528 * VMFail of any type if early consistency checks are enabled.
4529 */
4530 WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) !=
4531 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
4532 WARN_ON_ONCE(nested_early_check);
4533 }
4534
4535 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
4536
4537 /* Update any VMCS fields that might have changed while L2 ran */
4538 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
4539 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
4540 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Ilias Stamatis1ab92872021-06-07 11:54:38 +01004541 if (kvm_has_tsc_control)
4542 vmcs_write64(TSC_MULTIPLIER, vcpu->arch.tsc_scaling_ratio);
4543
Liran Alon02d496cf2019-11-11 14:30:55 +02004544 if (vmx->nested.l1_tpr_threshold != -1)
4545 vmcs_write32(TPR_THRESHOLD, vmx->nested.l1_tpr_threshold);
Sean Christopherson55d23752018-12-03 13:53:18 -08004546
Sean Christopherson55d23752018-12-03 13:53:18 -08004547 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
4548 vmx->nested.change_vmcs01_virtual_apic_mode = false;
4549 vmx_set_virtual_apic_mode(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08004550 }
4551
Makarand Sonarea85863c2021-02-12 16:50:12 -08004552 if (vmx->nested.update_vmcs01_cpu_dirty_logging) {
4553 vmx->nested.update_vmcs01_cpu_dirty_logging = false;
4554 vmx_update_cpu_dirty_logging(vcpu);
4555 }
4556
Sean Christopherson55d23752018-12-03 13:53:18 -08004557 /* Unpin physical memory we referred to in vmcs02 */
4558 if (vmx->nested.apic_access_page) {
Liran Alonb11494b2019-11-21 00:31:47 +02004559 kvm_release_page_clean(vmx->nested.apic_access_page);
Sean Christopherson55d23752018-12-03 13:53:18 -08004560 vmx->nested.apic_access_page = NULL;
4561 }
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01004562 kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map, true);
KarimAllah Ahmed3278e042019-01-31 21:24:38 +01004563 kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map, true);
4564 vmx->nested.pi_desc = NULL;
Sean Christopherson55d23752018-12-03 13:53:18 -08004565
Sean Christopherson1196cb92020-03-20 14:28:23 -07004566 if (vmx->nested.reload_vmcs01_apic_access_page) {
4567 vmx->nested.reload_vmcs01_apic_access_page = false;
4568 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
4569 }
Sean Christopherson55d23752018-12-03 13:53:18 -08004570
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004571 if ((vm_exit_reason != -1) &&
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02004572 (enable_shadow_vmcs || evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)))
Sean Christopherson3731905ef2019-05-07 08:36:27 -07004573 vmx->nested.need_vmcs12_to_shadow_sync = true;
Sean Christopherson55d23752018-12-03 13:53:18 -08004574
4575 /* in case we halted in L2 */
4576 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
4577
4578 if (likely(!vmx->fail)) {
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004579 if ((u16)vm_exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
Sean Christophersona1c77ab2020-03-02 22:27:35 -08004580 nested_exit_intr_ack_set(vcpu)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08004581 int irq = kvm_cpu_get_interrupt(vcpu);
4582 WARN_ON(irq < 0);
4583 vmcs12->vm_exit_intr_info = irq |
4584 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
4585 }
4586
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004587 if (vm_exit_reason != -1)
Sean Christopherson55d23752018-12-03 13:53:18 -08004588 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
4589 vmcs12->exit_qualification,
4590 vmcs12->idt_vectoring_info_field,
4591 vmcs12->vm_exit_intr_info,
4592 vmcs12->vm_exit_intr_error_code,
4593 KVM_ISA_VMX);
4594
4595 load_vmcs12_host_state(vcpu, vmcs12);
4596
4597 return;
4598 }
4599
4600 /*
4601 * After an early L2 VM-entry failure, we're now back
4602 * in L1 which thinks it just finished a VMLAUNCH or
4603 * VMRESUME instruction, so we need to set the failure
4604 * flag and the VM-instruction error field of the VMCS
4605 * accordingly, and skip the emulated instruction.
4606 */
Sean Christophersonb2656e42020-06-08 18:56:07 -07004607 (void)nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Sean Christopherson55d23752018-12-03 13:53:18 -08004608
4609 /*
4610 * Restore L1's host state to KVM's software model. We're here
4611 * because a consistency check was caught by hardware, which
4612 * means some amount of guest state has been propagated to KVM's
4613 * model and needs to be unwound to the host's state.
4614 */
4615 nested_vmx_restore_host_state(vcpu);
4616
4617 vmx->fail = 0;
4618}
4619
Sean Christophersoncb6a32c2021-03-02 09:45:14 -08004620static void nested_vmx_triple_fault(struct kvm_vcpu *vcpu)
4621{
4622 nested_vmx_vmexit(vcpu, EXIT_REASON_TRIPLE_FAULT, 0, 0);
4623}
4624
Sean Christopherson55d23752018-12-03 13:53:18 -08004625/*
4626 * Decode the memory-address operand of a vmx instruction, as recorded on an
4627 * exit caused by such an instruction (run by a guest hypervisor).
4628 * On success, returns 0. When the operand is invalid, returns 1 and throws
Miaohe Lin49f933d2020-02-27 11:20:54 +08004629 * #UD, #GP, or #SS.
Sean Christopherson55d23752018-12-03 13:53:18 -08004630 */
4631int get_vmx_mem_address(struct kvm_vcpu *vcpu, unsigned long exit_qualification,
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004632 u32 vmx_instruction_info, bool wr, int len, gva_t *ret)
Sean Christopherson55d23752018-12-03 13:53:18 -08004633{
4634 gva_t off;
4635 bool exn;
4636 struct kvm_segment s;
4637
4638 /*
4639 * According to Vol. 3B, "Information for VM Exits Due to Instruction
4640 * Execution", on an exit, vmx_instruction_info holds most of the
4641 * addressing components of the operand. Only the displacement part
4642 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
4643 * For how an actual address is calculated from all these components,
4644 * refer to Vol. 1, "Operand Addressing".
4645 */
4646 int scaling = vmx_instruction_info & 3;
4647 int addr_size = (vmx_instruction_info >> 7) & 7;
4648 bool is_reg = vmx_instruction_info & (1u << 10);
4649 int seg_reg = (vmx_instruction_info >> 15) & 7;
4650 int index_reg = (vmx_instruction_info >> 18) & 0xf;
4651 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
4652 int base_reg = (vmx_instruction_info >> 23) & 0xf;
4653 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
4654
4655 if (is_reg) {
4656 kvm_queue_exception(vcpu, UD_VECTOR);
4657 return 1;
4658 }
4659
4660 /* Addr = segment_base + offset */
4661 /* offset = base + [index * scale] + displacement */
4662 off = exit_qualification; /* holds the displacement */
Sean Christopherson946c5222019-01-23 14:39:23 -08004663 if (addr_size == 1)
4664 off = (gva_t)sign_extend64(off, 31);
4665 else if (addr_size == 0)
4666 off = (gva_t)sign_extend64(off, 15);
Sean Christopherson55d23752018-12-03 13:53:18 -08004667 if (base_is_valid)
4668 off += kvm_register_read(vcpu, base_reg);
4669 if (index_is_valid)
Miaohe Line6302692020-02-15 10:44:22 +08004670 off += kvm_register_read(vcpu, index_reg) << scaling;
Sean Christopherson55d23752018-12-03 13:53:18 -08004671 vmx_get_segment(vcpu, &s, seg_reg);
Sean Christopherson55d23752018-12-03 13:53:18 -08004672
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004673 /*
4674 * The effective address, i.e. @off, of a memory operand is truncated
4675 * based on the address size of the instruction. Note that this is
4676 * the *effective address*, i.e. the address prior to accounting for
4677 * the segment's base.
4678 */
Sean Christopherson55d23752018-12-03 13:53:18 -08004679 if (addr_size == 1) /* 32 bit */
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004680 off &= 0xffffffff;
4681 else if (addr_size == 0) /* 16 bit */
4682 off &= 0xffff;
Sean Christopherson55d23752018-12-03 13:53:18 -08004683
4684 /* Checks for #GP/#SS exceptions. */
4685 exn = false;
4686 if (is_long_mode(vcpu)) {
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004687 /*
4688 * The virtual/linear address is never truncated in 64-bit
4689 * mode, e.g. a 32-bit address size can yield a 64-bit virtual
4690 * address when using FS/GS with a non-zero base.
4691 */
Liran Alon6694e482019-07-15 18:47:44 +03004692 if (seg_reg == VCPU_SREG_FS || seg_reg == VCPU_SREG_GS)
4693 *ret = s.base + off;
4694 else
4695 *ret = off;
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004696
Sean Christopherson55d23752018-12-03 13:53:18 -08004697 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
4698 * non-canonical form. This is the only check on the memory
4699 * destination for long mode!
4700 */
4701 exn = is_noncanonical_address(*ret, vcpu);
Paolo Bonzinie0dfacb2019-01-30 17:25:38 +01004702 } else {
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004703 /*
4704 * When not in long mode, the virtual/linear address is
4705 * unconditionally truncated to 32 bits regardless of the
4706 * address size.
4707 */
4708 *ret = (s.base + off) & 0xffffffff;
4709
Sean Christopherson55d23752018-12-03 13:53:18 -08004710 /* Protected mode: apply checks for segment validity in the
4711 * following order:
4712 * - segment type check (#GP(0) may be thrown)
4713 * - usability check (#GP(0)/#SS(0))
4714 * - limit check (#GP(0)/#SS(0))
4715 */
4716 if (wr)
4717 /* #GP(0) if the destination operand is located in a
4718 * read-only data segment or any code segment.
4719 */
4720 exn = ((s.type & 0xa) == 0 || (s.type & 8));
4721 else
4722 /* #GP(0) if the source operand is located in an
4723 * execute-only code segment
4724 */
4725 exn = ((s.type & 0xa) == 8);
4726 if (exn) {
4727 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
4728 return 1;
4729 }
4730 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
4731 */
4732 exn = (s.unusable != 0);
Sean Christopherson34333cc2019-01-23 14:39:25 -08004733
4734 /*
4735 * Protected mode: #GP(0)/#SS(0) if the memory operand is
4736 * outside the segment limit. All CPUs that support VMX ignore
4737 * limit checks for flat segments, i.e. segments with base==0,
4738 * limit==0xffffffff and of type expand-up data or code.
Sean Christopherson55d23752018-12-03 13:53:18 -08004739 */
Sean Christopherson34333cc2019-01-23 14:39:25 -08004740 if (!(s.base == 0 && s.limit == 0xffffffff &&
4741 ((s.type & 8) || !(s.type & 4))))
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004742 exn = exn || ((u64)off + len - 1 > s.limit);
Sean Christopherson55d23752018-12-03 13:53:18 -08004743 }
4744 if (exn) {
4745 kvm_queue_exception_e(vcpu,
4746 seg_reg == VCPU_SREG_SS ?
4747 SS_VECTOR : GP_VECTOR,
4748 0);
4749 return 1;
4750 }
4751
4752 return 0;
4753}
4754
Oliver Upton03a8871a2019-11-13 16:17:20 -08004755void nested_vmx_pmu_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
4756{
4757 struct vcpu_vmx *vmx;
4758
4759 if (!nested_vmx_allowed(vcpu))
4760 return;
4761
4762 vmx = to_vmx(vcpu);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004763 if (kvm_x86_ops.pmu_ops->is_valid_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL)) {
Oliver Upton03a8871a2019-11-13 16:17:20 -08004764 vmx->nested.msrs.entry_ctls_high |=
4765 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
4766 vmx->nested.msrs.exit_ctls_high |=
4767 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
4768 } else {
4769 vmx->nested.msrs.entry_ctls_high &=
4770 ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
4771 vmx->nested.msrs.exit_ctls_high &=
Chenyi Qiangc6b177a2020-08-28 16:56:21 +08004772 ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
Oliver Upton03a8871a2019-11-13 16:17:20 -08004773 }
4774}
4775
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004776static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer,
4777 int *ret)
Sean Christopherson55d23752018-12-03 13:53:18 -08004778{
4779 gva_t gva;
4780 struct x86_exception e;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004781 int r;
Sean Christopherson55d23752018-12-03 13:53:18 -08004782
Sean Christopherson5addc232020-04-15 13:34:53 -07004783 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004784 vmcs_read32(VMX_INSTRUCTION_INFO), false,
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004785 sizeof(*vmpointer), &gva)) {
4786 *ret = 1;
4787 return -EINVAL;
4788 }
Sean Christopherson55d23752018-12-03 13:53:18 -08004789
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004790 r = kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e);
4791 if (r != X86EMUL_CONTINUE) {
Babu Moger3f3393b2020-09-11 14:29:05 -05004792 *ret = kvm_handle_memory_failure(vcpu, r, &e);
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004793 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08004794 }
4795
4796 return 0;
4797}
4798
4799/*
4800 * Allocate a shadow VMCS and associate it with the currently loaded
4801 * VMCS, unless such a shadow VMCS already exists. The newly allocated
4802 * VMCS is also VMCLEARed, so that it is ready for use.
4803 */
4804static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
4805{
4806 struct vcpu_vmx *vmx = to_vmx(vcpu);
4807 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
4808
4809 /*
4810 * We should allocate a shadow vmcs for vmcs01 only when L1
4811 * executes VMXON and free it when L1 executes VMXOFF.
4812 * As it is invalid to execute VMXON twice, we shouldn't reach
4813 * here when vmcs01 already have an allocated shadow vmcs.
4814 */
4815 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
4816
4817 if (!loaded_vmcs->shadow_vmcs) {
4818 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
4819 if (loaded_vmcs->shadow_vmcs)
4820 vmcs_clear(loaded_vmcs->shadow_vmcs);
4821 }
4822 return loaded_vmcs->shadow_vmcs;
4823}
4824
4825static int enter_vmx_operation(struct kvm_vcpu *vcpu)
4826{
4827 struct vcpu_vmx *vmx = to_vmx(vcpu);
4828 int r;
4829
4830 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
4831 if (r < 0)
4832 goto out_vmcs02;
4833
Ben Gardon41836832019-02-11 11:02:52 -08004834 vmx->nested.cached_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT);
Sean Christopherson55d23752018-12-03 13:53:18 -08004835 if (!vmx->nested.cached_vmcs12)
4836 goto out_cached_vmcs12;
4837
Ben Gardon41836832019-02-11 11:02:52 -08004838 vmx->nested.cached_shadow_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT);
Sean Christopherson55d23752018-12-03 13:53:18 -08004839 if (!vmx->nested.cached_shadow_vmcs12)
4840 goto out_cached_shadow_vmcs12;
4841
4842 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
4843 goto out_shadow_vmcs;
4844
4845 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
Jim Mattsonada00982020-05-08 13:36:42 -07004846 HRTIMER_MODE_ABS_PINNED);
Sean Christopherson55d23752018-12-03 13:53:18 -08004847 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
4848
4849 vmx->nested.vpid02 = allocate_vpid();
4850
4851 vmx->nested.vmcs02_initialized = false;
4852 vmx->nested.vmxon = true;
Luwei Kangee85dec2018-10-24 16:05:16 +08004853
Sean Christopherson2ef76192020-03-02 15:56:22 -08004854 if (vmx_pt_mode_is_host_guest()) {
Luwei Kangee85dec2018-10-24 16:05:16 +08004855 vmx->pt_desc.guest.ctl = 0;
Aaron Lewis476c9bd2020-09-25 16:34:18 +02004856 pt_update_intercept_for_msr(vcpu);
Luwei Kangee85dec2018-10-24 16:05:16 +08004857 }
4858
Sean Christopherson55d23752018-12-03 13:53:18 -08004859 return 0;
4860
4861out_shadow_vmcs:
4862 kfree(vmx->nested.cached_shadow_vmcs12);
4863
4864out_cached_shadow_vmcs12:
4865 kfree(vmx->nested.cached_vmcs12);
4866
4867out_cached_vmcs12:
4868 free_loaded_vmcs(&vmx->nested.vmcs02);
4869
4870out_vmcs02:
4871 return -ENOMEM;
4872}
4873
Yu Zhanged7023a2021-09-09 01:17:31 +08004874/* Emulate the VMXON instruction. */
Sean Christopherson55d23752018-12-03 13:53:18 -08004875static int handle_vmon(struct kvm_vcpu *vcpu)
4876{
4877 int ret;
4878 gpa_t vmptr;
KarimAllah Ahmed2e408932019-01-31 21:24:31 +01004879 uint32_t revision;
Sean Christopherson55d23752018-12-03 13:53:18 -08004880 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson32ad73d2019-12-20 20:44:55 -08004881 const u64 VMXON_NEEDED_FEATURES = FEAT_CTL_LOCKED
4882 | FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX;
Sean Christopherson55d23752018-12-03 13:53:18 -08004883
4884 /*
4885 * The Intel VMX Instruction Reference lists a bunch of bits that are
4886 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
Sean Christophersonc2fe3cd2020-10-06 18:44:15 -07004887 * 1 (see vmx_is_valid_cr4() for when we allow the guest to set this).
Sean Christopherson55d23752018-12-03 13:53:18 -08004888 * Otherwise, we should fail with #UD. But most faulting conditions
4889 * have already been checked by hardware, prior to the VM-exit for
4890 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
4891 * that bit set to 1 in non-root mode.
4892 */
4893 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
4894 kvm_queue_exception(vcpu, UD_VECTOR);
4895 return 1;
4896 }
4897
4898 /* CPL=0 must be checked manually. */
4899 if (vmx_get_cpl(vcpu)) {
4900 kvm_inject_gp(vcpu, 0);
4901 return 1;
4902 }
4903
4904 if (vmx->nested.vmxon)
Sean Christophersonb2656e42020-06-08 18:56:07 -07004905 return nested_vmx_fail(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Sean Christopherson55d23752018-12-03 13:53:18 -08004906
4907 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
4908 != VMXON_NEEDED_FEATURES) {
4909 kvm_inject_gp(vcpu, 0);
4910 return 1;
4911 }
4912
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004913 if (nested_vmx_get_vmptr(vcpu, &vmptr, &ret))
4914 return ret;
Sean Christopherson55d23752018-12-03 13:53:18 -08004915
4916 /*
4917 * SDM 3: 24.11.5
4918 * The first 4 bytes of VMXON region contain the supported
4919 * VMCS revision identifier
4920 *
4921 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
4922 * which replaces physical address width with 32
4923 */
KarimAllah Ahmede0bf2662019-01-31 21:24:43 +01004924 if (!page_address_valid(vcpu, vmptr))
Sean Christopherson55d23752018-12-03 13:53:18 -08004925 return nested_vmx_failInvalid(vcpu);
4926
KarimAllah Ahmed2e408932019-01-31 21:24:31 +01004927 if (kvm_read_guest(vcpu->kvm, vmptr, &revision, sizeof(revision)) ||
4928 revision != VMCS12_REVISION)
Sean Christopherson55d23752018-12-03 13:53:18 -08004929 return nested_vmx_failInvalid(vcpu);
4930
Sean Christopherson55d23752018-12-03 13:53:18 -08004931 vmx->nested.vmxon_ptr = vmptr;
4932 ret = enter_vmx_operation(vcpu);
4933 if (ret)
4934 return ret;
4935
4936 return nested_vmx_succeed(vcpu);
4937}
4938
4939static inline void nested_release_vmcs12(struct kvm_vcpu *vcpu)
4940{
4941 struct vcpu_vmx *vmx = to_vmx(vcpu);
4942
Yu Zhang64c78502021-09-30 01:51:53 +08004943 if (vmx->nested.current_vmptr == INVALID_GPA)
Sean Christopherson55d23752018-12-03 13:53:18 -08004944 return;
4945
Sean Christopherson7952d762019-05-07 08:36:29 -07004946 copy_vmcs02_to_vmcs12_rare(vcpu, get_vmcs12(vcpu));
4947
Sean Christopherson55d23752018-12-03 13:53:18 -08004948 if (enable_shadow_vmcs) {
4949 /* copy to memory all shadowed fields in case
4950 they were modified */
4951 copy_shadow_to_vmcs12(vmx);
Sean Christopherson55d23752018-12-03 13:53:18 -08004952 vmx_disable_shadow_vmcs(vmx);
4953 }
4954 vmx->nested.posted_intr_nv = -1;
4955
4956 /* Flush VMCS12 to guest memory */
4957 kvm_vcpu_write_guest_page(vcpu,
4958 vmx->nested.current_vmptr >> PAGE_SHIFT,
4959 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
4960
4961 kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
4962
Yu Zhang64c78502021-09-30 01:51:53 +08004963 vmx->nested.current_vmptr = INVALID_GPA;
Sean Christopherson55d23752018-12-03 13:53:18 -08004964}
4965
4966/* Emulate the VMXOFF instruction */
4967static int handle_vmoff(struct kvm_vcpu *vcpu)
4968{
4969 if (!nested_vmx_check_permission(vcpu))
4970 return 1;
Liran Alon4b9852f2019-08-26 13:24:49 +03004971
Sean Christopherson55d23752018-12-03 13:53:18 -08004972 free_nested(vcpu);
Liran Alon4b9852f2019-08-26 13:24:49 +03004973
4974 /* Process a latched INIT during time CPU was in VMX operation */
4975 kvm_make_request(KVM_REQ_EVENT, vcpu);
4976
Sean Christopherson55d23752018-12-03 13:53:18 -08004977 return nested_vmx_succeed(vcpu);
4978}
4979
4980/* Emulate the VMCLEAR instruction */
4981static int handle_vmclear(struct kvm_vcpu *vcpu)
4982{
4983 struct vcpu_vmx *vmx = to_vmx(vcpu);
4984 u32 zero = 0;
4985 gpa_t vmptr;
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02004986 u64 evmcs_gpa;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004987 int r;
Sean Christopherson55d23752018-12-03 13:53:18 -08004988
4989 if (!nested_vmx_check_permission(vcpu))
4990 return 1;
4991
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004992 if (nested_vmx_get_vmptr(vcpu, &vmptr, &r))
4993 return r;
Sean Christopherson55d23752018-12-03 13:53:18 -08004994
KarimAllah Ahmede0bf2662019-01-31 21:24:43 +01004995 if (!page_address_valid(vcpu, vmptr))
Sean Christophersonb2656e42020-06-08 18:56:07 -07004996 return nested_vmx_fail(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
Sean Christopherson55d23752018-12-03 13:53:18 -08004997
4998 if (vmptr == vmx->nested.vmxon_ptr)
Sean Christophersonb2656e42020-06-08 18:56:07 -07004999 return nested_vmx_fail(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
Sean Christopherson55d23752018-12-03 13:53:18 -08005000
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02005001 /*
5002 * When Enlightened VMEntry is enabled on the calling CPU we treat
5003 * memory area pointer by vmptr as Enlightened VMCS (as there's no good
5004 * way to distinguish it from VMCS12) and we must not corrupt it by
5005 * writing to the non-existent 'launch_state' field. The area doesn't
5006 * have to be the currently active EVMCS on the calling CPU and there's
5007 * nothing KVM has to do to transition it from 'active' to 'non-active'
5008 * state. It is possible that the area will stay mapped as
5009 * vmx->nested.hv_evmcs but this shouldn't be a problem.
5010 */
5011 if (likely(!vmx->nested.enlightened_vmcs_enabled ||
5012 !nested_enlightened_vmentry(vcpu, &evmcs_gpa))) {
Sean Christopherson55d23752018-12-03 13:53:18 -08005013 if (vmptr == vmx->nested.current_vmptr)
5014 nested_release_vmcs12(vcpu);
5015
5016 kvm_vcpu_write_guest(vcpu,
5017 vmptr + offsetof(struct vmcs12,
5018 launch_state),
5019 &zero, sizeof(zero));
Vitaly Kuznetsov3b19b812021-05-26 15:20:21 +02005020 } else if (vmx->nested.hv_evmcs && vmptr == vmx->nested.hv_evmcs_vmptr) {
5021 nested_release_evmcs(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005022 }
5023
5024 return nested_vmx_succeed(vcpu);
5025}
5026
Sean Christopherson55d23752018-12-03 13:53:18 -08005027/* Emulate the VMLAUNCH instruction */
5028static int handle_vmlaunch(struct kvm_vcpu *vcpu)
5029{
5030 return nested_vmx_run(vcpu, true);
5031}
5032
5033/* Emulate the VMRESUME instruction */
5034static int handle_vmresume(struct kvm_vcpu *vcpu)
5035{
5036
5037 return nested_vmx_run(vcpu, false);
5038}
5039
5040static int handle_vmread(struct kvm_vcpu *vcpu)
5041{
Jim Mattsondd2d6042019-12-06 15:46:35 -08005042 struct vmcs12 *vmcs12 = is_guest_mode(vcpu) ? get_shadow_vmcs12(vcpu)
5043 : get_vmcs12(vcpu);
Sean Christopherson5addc232020-04-15 13:34:53 -07005044 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005045 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5046 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzinif7eea632019-09-14 00:26:27 +02005047 struct x86_exception e;
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005048 unsigned long field;
5049 u64 value;
5050 gva_t gva = 0;
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07005051 short offset;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005052 int len, r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005053
5054 if (!nested_vmx_check_permission(vcpu))
5055 return 1;
5056
Jim Mattsondd2d6042019-12-06 15:46:35 -08005057 /*
Yu Zhang64c78502021-09-30 01:51:53 +08005058 * In VMX non-root operation, when the VMCS-link pointer is INVALID_GPA,
Jim Mattsondd2d6042019-12-06 15:46:35 -08005059 * any VMREAD sets the ALU flags for VMfailInvalid.
5060 */
Yu Zhang64c78502021-09-30 01:51:53 +08005061 if (vmx->nested.current_vmptr == INVALID_GPA ||
Jim Mattsondd2d6042019-12-06 15:46:35 -08005062 (is_guest_mode(vcpu) &&
Yu Zhang64c78502021-09-30 01:51:53 +08005063 get_vmcs12(vcpu)->vmcs_link_pointer == INVALID_GPA))
Sean Christopherson55d23752018-12-03 13:53:18 -08005064 return nested_vmx_failInvalid(vcpu);
5065
Sean Christopherson55d23752018-12-03 13:53:18 -08005066 /* Decode instruction info and find the field to read */
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005067 field = kvm_register_read(vcpu, (((instr_info) >> 28) & 0xf));
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07005068
5069 offset = vmcs_field_to_offset(field);
5070 if (offset < 0)
Sean Christophersonb2656e42020-06-08 18:56:07 -07005071 return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Sean Christopherson55d23752018-12-03 13:53:18 -08005072
Sean Christopherson7952d762019-05-07 08:36:29 -07005073 if (!is_guest_mode(vcpu) && is_vmcs12_ext_field(field))
5074 copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
5075
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005076 /* Read the field, zero-extended to a u64 value */
5077 value = vmcs12_read_any(vmcs12, field, offset);
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07005078
Sean Christopherson55d23752018-12-03 13:53:18 -08005079 /*
5080 * Now copy part of this value to register or memory, as requested.
5081 * Note that the number of bits actually copied is 32 or 64 depending
5082 * on the guest's mode (32 or 64 bit), not on the given field's length.
5083 */
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005084 if (instr_info & BIT(10)) {
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005085 kvm_register_write(vcpu, (((instr_info) >> 3) & 0xf), value);
Sean Christopherson55d23752018-12-03 13:53:18 -08005086 } else {
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005087 len = is_64_bit_mode(vcpu) ? 8 : 4;
Sean Christopherson55d23752018-12-03 13:53:18 -08005088 if (get_vmx_mem_address(vcpu, exit_qualification,
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005089 instr_info, true, len, &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08005090 return 1;
5091 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005092 r = kvm_write_guest_virt_system(vcpu, gva, &value, len, &e);
5093 if (r != X86EMUL_CONTINUE)
Babu Moger3f3393b2020-09-11 14:29:05 -05005094 return kvm_handle_memory_failure(vcpu, r, &e);
Sean Christopherson55d23752018-12-03 13:53:18 -08005095 }
5096
5097 return nested_vmx_succeed(vcpu);
5098}
5099
Sean Christophersone2174292019-05-07 08:36:28 -07005100static bool is_shadow_field_rw(unsigned long field)
5101{
5102 switch (field) {
5103#define SHADOW_FIELD_RW(x, y) case x:
5104#include "vmcs_shadow_fields.h"
5105 return true;
5106 default:
5107 break;
5108 }
5109 return false;
5110}
5111
5112static bool is_shadow_field_ro(unsigned long field)
5113{
5114 switch (field) {
5115#define SHADOW_FIELD_RO(x, y) case x:
5116#include "vmcs_shadow_fields.h"
5117 return true;
5118 default:
5119 break;
5120 }
5121 return false;
5122}
Sean Christopherson55d23752018-12-03 13:53:18 -08005123
5124static int handle_vmwrite(struct kvm_vcpu *vcpu)
5125{
Jim Mattsondd2d6042019-12-06 15:46:35 -08005126 struct vmcs12 *vmcs12 = is_guest_mode(vcpu) ? get_shadow_vmcs12(vcpu)
5127 : get_vmcs12(vcpu);
Sean Christopherson5addc232020-04-15 13:34:53 -07005128 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005129 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5130 struct vcpu_vmx *vmx = to_vmx(vcpu);
5131 struct x86_exception e;
5132 unsigned long field;
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07005133 short offset;
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005134 gva_t gva;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005135 int len, r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005136
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005137 /*
5138 * The value to write might be 32 or 64 bits, depending on L1's long
Sean Christopherson55d23752018-12-03 13:53:18 -08005139 * mode, and eventually we need to write that into a field of several
5140 * possible lengths. The code below first zero-extends the value to 64
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005141 * bit (value), and then copies only the appropriate number of
Sean Christopherson55d23752018-12-03 13:53:18 -08005142 * bits into the vmcs12 field.
5143 */
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005144 u64 value = 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08005145
5146 if (!nested_vmx_check_permission(vcpu))
5147 return 1;
5148
Jim Mattsondd2d6042019-12-06 15:46:35 -08005149 /*
Yu Zhang64c78502021-09-30 01:51:53 +08005150 * In VMX non-root operation, when the VMCS-link pointer is INVALID_GPA,
Jim Mattsondd2d6042019-12-06 15:46:35 -08005151 * any VMWRITE sets the ALU flags for VMfailInvalid.
5152 */
Yu Zhang64c78502021-09-30 01:51:53 +08005153 if (vmx->nested.current_vmptr == INVALID_GPA ||
Jim Mattsondd2d6042019-12-06 15:46:35 -08005154 (is_guest_mode(vcpu) &&
Yu Zhang64c78502021-09-30 01:51:53 +08005155 get_vmcs12(vcpu)->vmcs_link_pointer == INVALID_GPA))
Sean Christopherson55d23752018-12-03 13:53:18 -08005156 return nested_vmx_failInvalid(vcpu);
5157
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005158 if (instr_info & BIT(10))
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005159 value = kvm_register_read(vcpu, (((instr_info) >> 3) & 0xf));
Sean Christopherson55d23752018-12-03 13:53:18 -08005160 else {
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005161 len = is_64_bit_mode(vcpu) ? 8 : 4;
Sean Christopherson55d23752018-12-03 13:53:18 -08005162 if (get_vmx_mem_address(vcpu, exit_qualification,
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005163 instr_info, false, len, &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08005164 return 1;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005165 r = kvm_read_guest_virt(vcpu, gva, &value, len, &e);
5166 if (r != X86EMUL_CONTINUE)
Babu Moger3f3393b2020-09-11 14:29:05 -05005167 return kvm_handle_memory_failure(vcpu, r, &e);
Sean Christopherson55d23752018-12-03 13:53:18 -08005168 }
5169
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005170 field = kvm_register_read(vcpu, (((instr_info) >> 28) & 0xf));
Sean Christopherson55d23752018-12-03 13:53:18 -08005171
Jim Mattson693e02c2019-12-06 15:46:36 -08005172 offset = vmcs_field_to_offset(field);
5173 if (offset < 0)
Sean Christophersonb2656e42020-06-08 18:56:07 -07005174 return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Jim Mattson693e02c2019-12-06 15:46:36 -08005175
Sean Christopherson55d23752018-12-03 13:53:18 -08005176 /*
5177 * If the vCPU supports "VMWRITE to any supported field in the
5178 * VMCS," then the "read-only" fields are actually read/write.
5179 */
5180 if (vmcs_field_readonly(field) &&
5181 !nested_cpu_has_vmwrite_any_field(vcpu))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005182 return nested_vmx_fail(vcpu, VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Sean Christopherson55d23752018-12-03 13:53:18 -08005183
Jim Mattsondd2d6042019-12-06 15:46:35 -08005184 /*
5185 * Ensure vmcs12 is up-to-date before any VMWRITE that dirties
5186 * vmcs12, else we may crush a field or consume a stale value.
5187 */
5188 if (!is_guest_mode(vcpu) && !is_shadow_field_rw(field))
5189 copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08005190
5191 /*
Sean Christophersonb6437802019-05-07 08:36:24 -07005192 * Some Intel CPUs intentionally drop the reserved bits of the AR byte
5193 * fields on VMWRITE. Emulate this behavior to ensure consistent KVM
5194 * behavior regardless of the underlying hardware, e.g. if an AR_BYTE
5195 * field is intercepted for VMWRITE but not VMREAD (in L1), then VMREAD
5196 * from L1 will return a different value than VMREAD from L2 (L1 sees
5197 * the stripped down value, L2 sees the full value as stored by KVM).
Sean Christopherson55d23752018-12-03 13:53:18 -08005198 */
Sean Christophersonb6437802019-05-07 08:36:24 -07005199 if (field >= GUEST_ES_AR_BYTES && field <= GUEST_TR_AR_BYTES)
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005200 value &= 0x1f0ff;
Sean Christophersonb6437802019-05-07 08:36:24 -07005201
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005202 vmcs12_write_any(vmcs12, field, offset, value);
Sean Christopherson55d23752018-12-03 13:53:18 -08005203
5204 /*
Sean Christophersone2174292019-05-07 08:36:28 -07005205 * Do not track vmcs12 dirty-state if in guest-mode as we actually
5206 * dirty shadow vmcs12 instead of vmcs12. Fields that can be updated
5207 * by L1 without a vmexit are always updated in the vmcs02, i.e. don't
5208 * "dirty" vmcs12, all others go down the prepare_vmcs02() slow path.
Sean Christopherson55d23752018-12-03 13:53:18 -08005209 */
Sean Christophersone2174292019-05-07 08:36:28 -07005210 if (!is_guest_mode(vcpu) && !is_shadow_field_rw(field)) {
5211 /*
5212 * L1 can read these fields without exiting, ensure the
5213 * shadow VMCS is up-to-date.
5214 */
5215 if (enable_shadow_vmcs && is_shadow_field_ro(field)) {
5216 preempt_disable();
5217 vmcs_load(vmx->vmcs01.shadow_vmcs);
Sean Christophersonfadcead2019-05-07 08:36:23 -07005218
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005219 __vmcs_writel(field, value);
Sean Christophersonfadcead2019-05-07 08:36:23 -07005220
Sean Christophersone2174292019-05-07 08:36:28 -07005221 vmcs_clear(vmx->vmcs01.shadow_vmcs);
5222 vmcs_load(vmx->loaded_vmcs->vmcs);
5223 preempt_enable();
Sean Christopherson55d23752018-12-03 13:53:18 -08005224 }
Sean Christophersone2174292019-05-07 08:36:28 -07005225 vmx->nested.dirty_vmcs12 = true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005226 }
5227
5228 return nested_vmx_succeed(vcpu);
5229}
5230
5231static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
5232{
5233 vmx->nested.current_vmptr = vmptr;
5234 if (enable_shadow_vmcs) {
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07005235 secondary_exec_controls_setbit(vmx, SECONDARY_EXEC_SHADOW_VMCS);
Sean Christopherson55d23752018-12-03 13:53:18 -08005236 vmcs_write64(VMCS_LINK_POINTER,
5237 __pa(vmx->vmcs01.shadow_vmcs));
Sean Christopherson3731905ef2019-05-07 08:36:27 -07005238 vmx->nested.need_vmcs12_to_shadow_sync = true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005239 }
5240 vmx->nested.dirty_vmcs12 = true;
5241}
5242
5243/* Emulate the VMPTRLD instruction */
5244static int handle_vmptrld(struct kvm_vcpu *vcpu)
5245{
5246 struct vcpu_vmx *vmx = to_vmx(vcpu);
5247 gpa_t vmptr;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005248 int r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005249
5250 if (!nested_vmx_check_permission(vcpu))
5251 return 1;
5252
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005253 if (nested_vmx_get_vmptr(vcpu, &vmptr, &r))
5254 return r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005255
KarimAllah Ahmede0bf2662019-01-31 21:24:43 +01005256 if (!page_address_valid(vcpu, vmptr))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005257 return nested_vmx_fail(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
Sean Christopherson55d23752018-12-03 13:53:18 -08005258
5259 if (vmptr == vmx->nested.vmxon_ptr)
Sean Christophersonb2656e42020-06-08 18:56:07 -07005260 return nested_vmx_fail(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
Sean Christopherson55d23752018-12-03 13:53:18 -08005261
5262 /* Forbid normal VMPTRLD if Enlightened version was used */
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02005263 if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
Sean Christopherson55d23752018-12-03 13:53:18 -08005264 return 1;
5265
5266 if (vmx->nested.current_vmptr != vmptr) {
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01005267 struct kvm_host_map map;
Sean Christopherson55d23752018-12-03 13:53:18 -08005268 struct vmcs12 *new_vmcs12;
Sean Christopherson55d23752018-12-03 13:53:18 -08005269
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01005270 if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmptr), &map)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08005271 /*
5272 * Reads from an unbacked page return all 1s,
5273 * which means that the 32 bits located at the
5274 * given physical address won't match the required
5275 * VMCS12_REVISION identifier.
5276 */
Sean Christophersonb2656e42020-06-08 18:56:07 -07005277 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005278 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Sean Christopherson55d23752018-12-03 13:53:18 -08005279 }
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01005280
5281 new_vmcs12 = map.hva;
5282
Sean Christopherson55d23752018-12-03 13:53:18 -08005283 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
5284 (new_vmcs12->hdr.shadow_vmcs &&
5285 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01005286 kvm_vcpu_unmap(vcpu, &map, false);
Sean Christophersonb2656e42020-06-08 18:56:07 -07005287 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005288 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
5289 }
5290
5291 nested_release_vmcs12(vcpu);
5292
5293 /*
5294 * Load VMCS12 from guest memory since it is not already
5295 * cached.
5296 */
5297 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01005298 kvm_vcpu_unmap(vcpu, &map, false);
Sean Christopherson55d23752018-12-03 13:53:18 -08005299
5300 set_current_vmptr(vmx, vmptr);
5301 }
5302
5303 return nested_vmx_succeed(vcpu);
5304}
5305
5306/* Emulate the VMPTRST instruction */
5307static int handle_vmptrst(struct kvm_vcpu *vcpu)
5308{
Sean Christopherson5addc232020-04-15 13:34:53 -07005309 unsigned long exit_qual = vmx_get_exit_qual(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005310 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5311 gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr;
5312 struct x86_exception e;
5313 gva_t gva;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005314 int r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005315
5316 if (!nested_vmx_check_permission(vcpu))
5317 return 1;
5318
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02005319 if (unlikely(evmptr_is_valid(to_vmx(vcpu)->nested.hv_evmcs_vmptr)))
Sean Christopherson55d23752018-12-03 13:53:18 -08005320 return 1;
5321
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005322 if (get_vmx_mem_address(vcpu, exit_qual, instr_info,
5323 true, sizeof(gpa_t), &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08005324 return 1;
5325 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005326 r = kvm_write_guest_virt_system(vcpu, gva, (void *)&current_vmptr,
5327 sizeof(gpa_t), &e);
5328 if (r != X86EMUL_CONTINUE)
Babu Moger3f3393b2020-09-11 14:29:05 -05005329 return kvm_handle_memory_failure(vcpu, r, &e);
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005330
Sean Christopherson55d23752018-12-03 13:53:18 -08005331 return nested_vmx_succeed(vcpu);
5332}
5333
5334/* Emulate the INVEPT instruction */
5335static int handle_invept(struct kvm_vcpu *vcpu)
5336{
5337 struct vcpu_vmx *vmx = to_vmx(vcpu);
5338 u32 vmx_instruction_info, types;
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005339 unsigned long type, roots_to_free;
5340 struct kvm_mmu *mmu;
Sean Christopherson55d23752018-12-03 13:53:18 -08005341 gva_t gva;
5342 struct x86_exception e;
5343 struct {
5344 u64 eptp, gpa;
5345 } operand;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005346 int i, r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005347
5348 if (!(vmx->nested.msrs.secondary_ctls_high &
5349 SECONDARY_EXEC_ENABLE_EPT) ||
5350 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
5351 kvm_queue_exception(vcpu, UD_VECTOR);
5352 return 1;
5353 }
5354
5355 if (!nested_vmx_check_permission(vcpu))
5356 return 1;
5357
5358 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005359 type = kvm_register_read(vcpu, (vmx_instruction_info >> 28) & 0xf);
Sean Christopherson55d23752018-12-03 13:53:18 -08005360
5361 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
5362
5363 if (type >= 32 || !(types & (1 << type)))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005364 return nested_vmx_fail(vcpu, VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Sean Christopherson55d23752018-12-03 13:53:18 -08005365
5366 /* According to the Intel VMX instruction reference, the memory
5367 * operand is read even if it isn't needed (e.g., for type==global)
5368 */
Sean Christopherson5addc232020-04-15 13:34:53 -07005369 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005370 vmx_instruction_info, false, sizeof(operand), &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08005371 return 1;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005372 r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e);
5373 if (r != X86EMUL_CONTINUE)
Babu Moger3f3393b2020-09-11 14:29:05 -05005374 return kvm_handle_memory_failure(vcpu, r, &e);
Sean Christopherson55d23752018-12-03 13:53:18 -08005375
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005376 /*
5377 * Nested EPT roots are always held through guest_mmu,
5378 * not root_mmu.
5379 */
5380 mmu = &vcpu->arch.guest_mmu;
5381
Sean Christopherson55d23752018-12-03 13:53:18 -08005382 switch (type) {
Sean Christopherson55d23752018-12-03 13:53:18 -08005383 case VMX_EPT_EXTENT_CONTEXT:
Sean Christophersoneed00302020-03-20 14:27:58 -07005384 if (!nested_vmx_check_eptp(vcpu, operand.eptp))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005385 return nested_vmx_fail(vcpu,
Sean Christophersoneed00302020-03-20 14:27:58 -07005386 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Sean Christophersonf8aa7e32020-03-20 14:27:59 -07005387
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005388 roots_to_free = 0;
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07005389 if (nested_ept_root_matches(mmu->root_hpa, mmu->root_pgd,
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005390 operand.eptp))
5391 roots_to_free |= KVM_MMU_ROOT_CURRENT;
5392
5393 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) {
5394 if (nested_ept_root_matches(mmu->prev_roots[i].hpa,
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07005395 mmu->prev_roots[i].pgd,
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005396 operand.eptp))
5397 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
5398 }
5399 break;
Sean Christophersoneed00302020-03-20 14:27:58 -07005400 case VMX_EPT_EXTENT_GLOBAL:
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005401 roots_to_free = KVM_MMU_ROOTS_ALL;
Sean Christopherson55d23752018-12-03 13:53:18 -08005402 break;
5403 default:
Sean Christophersonf9336e32020-05-04 08:35:06 -07005404 BUG();
Sean Christopherson55d23752018-12-03 13:53:18 -08005405 break;
5406 }
5407
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005408 if (roots_to_free)
5409 kvm_mmu_free_roots(vcpu, mmu, roots_to_free);
5410
Sean Christopherson55d23752018-12-03 13:53:18 -08005411 return nested_vmx_succeed(vcpu);
5412}
5413
5414static int handle_invvpid(struct kvm_vcpu *vcpu)
5415{
5416 struct vcpu_vmx *vmx = to_vmx(vcpu);
5417 u32 vmx_instruction_info;
5418 unsigned long type, types;
5419 gva_t gva;
5420 struct x86_exception e;
5421 struct {
5422 u64 vpid;
5423 u64 gla;
5424 } operand;
5425 u16 vpid02;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005426 int r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005427
5428 if (!(vmx->nested.msrs.secondary_ctls_high &
5429 SECONDARY_EXEC_ENABLE_VPID) ||
5430 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
5431 kvm_queue_exception(vcpu, UD_VECTOR);
5432 return 1;
5433 }
5434
5435 if (!nested_vmx_check_permission(vcpu))
5436 return 1;
5437
5438 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005439 type = kvm_register_read(vcpu, (vmx_instruction_info >> 28) & 0xf);
Sean Christopherson55d23752018-12-03 13:53:18 -08005440
5441 types = (vmx->nested.msrs.vpid_caps &
5442 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
5443
5444 if (type >= 32 || !(types & (1 << type)))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005445 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005446 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
5447
5448 /* according to the intel vmx instruction reference, the memory
5449 * operand is read even if it isn't needed (e.g., for type==global)
5450 */
Sean Christopherson5addc232020-04-15 13:34:53 -07005451 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005452 vmx_instruction_info, false, sizeof(operand), &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08005453 return 1;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005454 r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e);
5455 if (r != X86EMUL_CONTINUE)
Babu Moger3f3393b2020-09-11 14:29:05 -05005456 return kvm_handle_memory_failure(vcpu, r, &e);
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005457
Sean Christopherson55d23752018-12-03 13:53:18 -08005458 if (operand.vpid >> 16)
Sean Christophersonb2656e42020-06-08 18:56:07 -07005459 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005460 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
5461
5462 vpid02 = nested_get_vpid02(vcpu);
5463 switch (type) {
5464 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
5465 if (!operand.vpid ||
5466 is_noncanonical_address(operand.gla, vcpu))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005467 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005468 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Sean Christophersonbc41d0c2020-03-20 14:28:09 -07005469 vpid_sync_vcpu_addr(vpid02, operand.gla);
Sean Christopherson55d23752018-12-03 13:53:18 -08005470 break;
5471 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
5472 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
5473 if (!operand.vpid)
Sean Christophersonb2656e42020-06-08 18:56:07 -07005474 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005475 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Sean Christopherson446ace42020-03-20 14:28:05 -07005476 vpid_sync_context(vpid02);
Sean Christopherson55d23752018-12-03 13:53:18 -08005477 break;
5478 case VMX_VPID_EXTENT_ALL_CONTEXT:
Sean Christopherson446ace42020-03-20 14:28:05 -07005479 vpid_sync_context(vpid02);
Sean Christopherson55d23752018-12-03 13:53:18 -08005480 break;
5481 default:
5482 WARN_ON_ONCE(1);
5483 return kvm_skip_emulated_instruction(vcpu);
5484 }
5485
Junaid Shahidd6e3f832020-03-20 14:28:00 -07005486 /*
5487 * Sync the shadow page tables if EPT is disabled, L1 is invalidating
Sean Christopherson25b62c62021-06-09 16:42:29 -07005488 * linear mappings for L2 (tagged with L2's VPID). Free all guest
5489 * roots as VPIDs are not tracked in the MMU role.
Junaid Shahidd6e3f832020-03-20 14:28:00 -07005490 *
5491 * Note, this operates on root_mmu, not guest_mmu, as L1 and L2 share
5492 * an MMU when EPT is disabled.
5493 *
5494 * TODO: sync only the affected SPTEs for INVDIVIDUAL_ADDR.
5495 */
5496 if (!enable_ept)
Sean Christopherson25b62c62021-06-09 16:42:29 -07005497 kvm_mmu_free_guest_mode_roots(vcpu, &vcpu->arch.root_mmu);
Junaid Shahidd6e3f832020-03-20 14:28:00 -07005498
Sean Christopherson55d23752018-12-03 13:53:18 -08005499 return nested_vmx_succeed(vcpu);
5500}
5501
5502static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
5503 struct vmcs12 *vmcs12)
5504{
Sean Christopherson2b3eaf82019-04-30 10:36:19 -07005505 u32 index = kvm_rcx_read(vcpu);
Sean Christophersonac6389a2020-03-02 18:02:38 -08005506 u64 new_eptp;
Sean Christopherson55d23752018-12-03 13:53:18 -08005507
Sean Christophersonc5ffd402021-06-09 16:42:35 -07005508 if (WARN_ON_ONCE(!nested_cpu_has_ept(vmcs12)))
Sean Christopherson55d23752018-12-03 13:53:18 -08005509 return 1;
Sean Christopherson55d23752018-12-03 13:53:18 -08005510 if (index >= VMFUNC_EPTP_ENTRIES)
5511 return 1;
5512
Sean Christopherson55d23752018-12-03 13:53:18 -08005513 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
Sean Christophersonac6389a2020-03-02 18:02:38 -08005514 &new_eptp, index * 8, 8))
Sean Christopherson55d23752018-12-03 13:53:18 -08005515 return 1;
5516
Sean Christopherson55d23752018-12-03 13:53:18 -08005517 /*
5518 * If the (L2) guest does a vmfunc to the currently
5519 * active ept pointer, we don't have to do anything else
5520 */
Sean Christophersonac6389a2020-03-02 18:02:38 -08005521 if (vmcs12->ept_pointer != new_eptp) {
5522 if (!nested_vmx_check_eptp(vcpu, new_eptp))
Sean Christopherson55d23752018-12-03 13:53:18 -08005523 return 1;
5524
Sean Christophersonac6389a2020-03-02 18:02:38 -08005525 vmcs12->ept_pointer = new_eptp;
Sean Christopherson39353ab2021-06-09 16:42:31 -07005526 nested_ept_new_eptp(vcpu);
Sean Christophersonc805f5d2021-03-04 17:10:57 -08005527
Sean Christopherson39353ab2021-06-09 16:42:31 -07005528 if (!nested_cpu_has_vpid(vmcs12))
5529 kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005530 }
5531
5532 return 0;
5533}
5534
5535static int handle_vmfunc(struct kvm_vcpu *vcpu)
5536{
5537 struct vcpu_vmx *vmx = to_vmx(vcpu);
5538 struct vmcs12 *vmcs12;
Sean Christopherson2b3eaf82019-04-30 10:36:19 -07005539 u32 function = kvm_rax_read(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005540
5541 /*
5542 * VMFUNC is only supported for nested guests, but we always enable the
5543 * secondary control for simplicity; for non-nested mode, fake that we
5544 * didn't by injecting #UD.
5545 */
5546 if (!is_guest_mode(vcpu)) {
5547 kvm_queue_exception(vcpu, UD_VECTOR);
5548 return 1;
5549 }
5550
5551 vmcs12 = get_vmcs12(vcpu);
Sean Christopherson546e8392021-06-09 16:42:34 -07005552
5553 /*
5554 * #UD on out-of-bounds function has priority over VM-Exit, and VMFUNC
5555 * is enabled in vmcs02 if and only if it's enabled in vmcs12.
5556 */
5557 if (WARN_ON_ONCE((function > 63) || !nested_cpu_has_vmfunc(vmcs12))) {
5558 kvm_queue_exception(vcpu, UD_VECTOR);
5559 return 1;
5560 }
5561
Sean Christopherson0e752252021-06-09 16:42:22 -07005562 if (!(vmcs12->vm_function_control & BIT_ULL(function)))
Sean Christopherson55d23752018-12-03 13:53:18 -08005563 goto fail;
5564
5565 switch (function) {
5566 case 0:
5567 if (nested_vmx_eptp_switching(vcpu, vmcs12))
5568 goto fail;
5569 break;
5570 default:
5571 goto fail;
5572 }
5573 return kvm_skip_emulated_instruction(vcpu);
5574
5575fail:
Sean Christopherson8e533242020-11-06 17:03:12 +08005576 /*
5577 * This is effectively a reflected VM-Exit, as opposed to a synthesized
5578 * nested VM-Exit. Pass the original exit reason, i.e. don't hardcode
5579 * EXIT_REASON_VMFUNC as the exit reason.
5580 */
5581 nested_vmx_vmexit(vcpu, vmx->exit_reason.full,
Sean Christopherson87915852020-04-15 13:34:54 -07005582 vmx_get_intr_info(vcpu),
Sean Christopherson5addc232020-04-15 13:34:53 -07005583 vmx_get_exit_qual(vcpu));
Sean Christopherson55d23752018-12-03 13:53:18 -08005584 return 1;
5585}
5586
Oliver Uptone71237d2020-02-04 15:26:30 -08005587/*
5588 * Return true if an IO instruction with the specified port and size should cause
5589 * a VM-exit into L1.
5590 */
5591bool nested_vmx_check_io_bitmaps(struct kvm_vcpu *vcpu, unsigned int port,
5592 int size)
Sean Christopherson55d23752018-12-03 13:53:18 -08005593{
Oliver Uptone71237d2020-02-04 15:26:30 -08005594 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005595 gpa_t bitmap, last_bitmap;
Sean Christopherson55d23752018-12-03 13:53:18 -08005596 u8 b;
5597
Yu Zhang64c78502021-09-30 01:51:53 +08005598 last_bitmap = INVALID_GPA;
Sean Christopherson55d23752018-12-03 13:53:18 -08005599 b = -1;
5600
5601 while (size > 0) {
5602 if (port < 0x8000)
5603 bitmap = vmcs12->io_bitmap_a;
5604 else if (port < 0x10000)
5605 bitmap = vmcs12->io_bitmap_b;
5606 else
5607 return true;
5608 bitmap += (port & 0x7fff) / 8;
5609
5610 if (last_bitmap != bitmap)
5611 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
5612 return true;
5613 if (b & (1 << (port & 7)))
5614 return true;
5615
5616 port++;
5617 size--;
5618 last_bitmap = bitmap;
5619 }
5620
5621 return false;
5622}
5623
Oliver Uptone71237d2020-02-04 15:26:30 -08005624static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
5625 struct vmcs12 *vmcs12)
5626{
5627 unsigned long exit_qualification;
Oliver Upton35a57132020-02-04 15:26:31 -08005628 unsigned short port;
Oliver Uptone71237d2020-02-04 15:26:30 -08005629 int size;
5630
5631 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
5632 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
5633
Sean Christopherson5addc232020-04-15 13:34:53 -07005634 exit_qualification = vmx_get_exit_qual(vcpu);
Oliver Uptone71237d2020-02-04 15:26:30 -08005635
5636 port = exit_qualification >> 16;
5637 size = (exit_qualification & 7) + 1;
5638
5639 return nested_vmx_check_io_bitmaps(vcpu, port, size);
5640}
5641
Sean Christopherson55d23752018-12-03 13:53:18 -08005642/*
Miaohe Lin463bfee2020-02-14 10:44:05 +08005643 * Return 1 if we should exit from L2 to L1 to handle an MSR access,
Sean Christopherson55d23752018-12-03 13:53:18 -08005644 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
5645 * disinterest in the current event (read or write a specific MSR) by using an
5646 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
5647 */
5648static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
Sean Christopherson8e533242020-11-06 17:03:12 +08005649 struct vmcs12 *vmcs12,
5650 union vmx_exit_reason exit_reason)
Sean Christopherson55d23752018-12-03 13:53:18 -08005651{
Sean Christopherson2b3eaf82019-04-30 10:36:19 -07005652 u32 msr_index = kvm_rcx_read(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005653 gpa_t bitmap;
5654
5655 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
5656 return true;
5657
5658 /*
5659 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
5660 * for the four combinations of read/write and low/high MSR numbers.
5661 * First we need to figure out which of the four to use:
5662 */
5663 bitmap = vmcs12->msr_bitmap;
Sean Christopherson8e533242020-11-06 17:03:12 +08005664 if (exit_reason.basic == EXIT_REASON_MSR_WRITE)
Sean Christopherson55d23752018-12-03 13:53:18 -08005665 bitmap += 2048;
5666 if (msr_index >= 0xc0000000) {
5667 msr_index -= 0xc0000000;
5668 bitmap += 1024;
5669 }
5670
5671 /* Then read the msr_index'th bit from this bitmap: */
5672 if (msr_index < 1024*8) {
5673 unsigned char b;
5674 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
5675 return true;
5676 return 1 & (b >> (msr_index & 7));
5677 } else
5678 return true; /* let L1 handle the wrong parameter */
5679}
5680
5681/*
5682 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
5683 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
5684 * intercept (via guest_host_mask etc.) the current event.
5685 */
5686static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
5687 struct vmcs12 *vmcs12)
5688{
Sean Christopherson5addc232020-04-15 13:34:53 -07005689 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005690 int cr = exit_qualification & 15;
5691 int reg;
5692 unsigned long val;
5693
5694 switch ((exit_qualification >> 4) & 3) {
5695 case 0: /* mov to cr */
5696 reg = (exit_qualification >> 8) & 15;
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005697 val = kvm_register_read(vcpu, reg);
Sean Christopherson55d23752018-12-03 13:53:18 -08005698 switch (cr) {
5699 case 0:
5700 if (vmcs12->cr0_guest_host_mask &
5701 (val ^ vmcs12->cr0_read_shadow))
5702 return true;
5703 break;
5704 case 3:
Sean Christopherson55d23752018-12-03 13:53:18 -08005705 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
5706 return true;
5707 break;
5708 case 4:
5709 if (vmcs12->cr4_guest_host_mask &
5710 (vmcs12->cr4_read_shadow ^ val))
5711 return true;
5712 break;
5713 case 8:
5714 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
5715 return true;
5716 break;
5717 }
5718 break;
5719 case 2: /* clts */
5720 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
5721 (vmcs12->cr0_read_shadow & X86_CR0_TS))
5722 return true;
5723 break;
5724 case 1: /* mov from cr */
5725 switch (cr) {
5726 case 3:
5727 if (vmcs12->cpu_based_vm_exec_control &
5728 CPU_BASED_CR3_STORE_EXITING)
5729 return true;
5730 break;
5731 case 8:
5732 if (vmcs12->cpu_based_vm_exec_control &
5733 CPU_BASED_CR8_STORE_EXITING)
5734 return true;
5735 break;
5736 }
5737 break;
5738 case 3: /* lmsw */
5739 /*
5740 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
5741 * cr0. Other attempted changes are ignored, with no exit.
5742 */
5743 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
5744 if (vmcs12->cr0_guest_host_mask & 0xe &
5745 (val ^ vmcs12->cr0_read_shadow))
5746 return true;
5747 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
5748 !(vmcs12->cr0_read_shadow & 0x1) &&
5749 (val & 0x1))
5750 return true;
5751 break;
5752 }
5753 return false;
5754}
5755
Sean Christopherson72add912021-04-12 16:21:42 +12005756static bool nested_vmx_exit_handled_encls(struct kvm_vcpu *vcpu,
5757 struct vmcs12 *vmcs12)
5758{
5759 u32 encls_leaf;
5760
5761 if (!guest_cpuid_has(vcpu, X86_FEATURE_SGX) ||
5762 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENCLS_EXITING))
5763 return false;
5764
5765 encls_leaf = kvm_rax_read(vcpu);
5766 if (encls_leaf > 62)
5767 encls_leaf = 63;
5768 return vmcs12->encls_exiting_bitmap & BIT_ULL(encls_leaf);
5769}
5770
Sean Christopherson55d23752018-12-03 13:53:18 -08005771static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
5772 struct vmcs12 *vmcs12, gpa_t bitmap)
5773{
5774 u32 vmx_instruction_info;
5775 unsigned long field;
5776 u8 b;
5777
5778 if (!nested_cpu_has_shadow_vmcs(vmcs12))
5779 return true;
5780
5781 /* Decode instruction info and find the field to access */
5782 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5783 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
5784
5785 /* Out-of-range fields always cause a VM exit from L2 to L1 */
5786 if (field >> 15)
5787 return true;
5788
5789 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
5790 return true;
5791
5792 return 1 & (b >> (field & 7));
5793}
5794
Oliver Uptonb045ae92020-04-14 22:47:45 +00005795static bool nested_vmx_exit_handled_mtf(struct vmcs12 *vmcs12)
5796{
5797 u32 entry_intr_info = vmcs12->vm_entry_intr_info_field;
5798
5799 if (nested_cpu_has_mtf(vmcs12))
5800 return true;
5801
5802 /*
5803 * An MTF VM-exit may be injected into the guest by setting the
5804 * interruption-type to 7 (other event) and the vector field to 0. Such
5805 * is the case regardless of the 'monitor trap flag' VM-execution
5806 * control.
5807 */
5808 return entry_intr_info == (INTR_INFO_VALID_MASK
5809 | INTR_TYPE_OTHER_EVENT);
5810}
5811
Sean Christopherson55d23752018-12-03 13:53:18 -08005812/*
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005813 * Return true if L0 wants to handle an exit from L2 regardless of whether or not
5814 * L1 wants the exit. Only call this when in is_guest_mode (L2).
Sean Christopherson55d23752018-12-03 13:53:18 -08005815 */
Sean Christopherson8e533242020-11-06 17:03:12 +08005816static bool nested_vmx_l0_wants_exit(struct kvm_vcpu *vcpu,
5817 union vmx_exit_reason exit_reason)
Sean Christopherson55d23752018-12-03 13:53:18 -08005818{
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005819 u32 intr_info;
5820
Sean Christopherson8e533242020-11-06 17:03:12 +08005821 switch ((u16)exit_reason.basic) {
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005822 case EXIT_REASON_EXCEPTION_NMI:
Sean Christopherson87915852020-04-15 13:34:54 -07005823 intr_info = vmx_get_intr_info(vcpu);
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005824 if (is_nmi(intr_info))
5825 return true;
5826 else if (is_page_fault(intr_info))
Sean Christopherson18712c12021-08-11 21:56:15 -07005827 return vcpu->arch.apf.host_apf_flags ||
5828 vmx_need_pf_intercept(vcpu);
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005829 else if (is_debug(intr_info) &&
5830 vcpu->guest_debug &
5831 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5832 return true;
5833 else if (is_breakpoint(intr_info) &&
5834 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
5835 return true;
Sean Christophersonb33bb782021-06-22 10:22:44 -07005836 else if (is_alignment_check(intr_info) &&
5837 !vmx_guest_inject_ac(vcpu))
5838 return true;
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005839 return false;
5840 case EXIT_REASON_EXTERNAL_INTERRUPT:
5841 return true;
5842 case EXIT_REASON_MCE_DURING_VMENTRY:
5843 return true;
5844 case EXIT_REASON_EPT_VIOLATION:
5845 /*
5846 * L0 always deals with the EPT violation. If nested EPT is
5847 * used, and the nested mmu code discovers that the address is
5848 * missing in the guest EPT table (EPT12), the EPT violation
5849 * will be injected with nested_ept_inject_page_fault()
5850 */
5851 return true;
5852 case EXIT_REASON_EPT_MISCONFIG:
5853 /*
5854 * L2 never uses directly L1's EPT, but rather L0's own EPT
5855 * table (shadow on EPT) or a merged EPT table that L0 built
5856 * (EPT on EPT). So any problems with the structure of the
5857 * table is L0's fault.
5858 */
5859 return true;
5860 case EXIT_REASON_PREEMPTION_TIMER:
5861 return true;
5862 case EXIT_REASON_PML_FULL:
Sean Christophersonc3bb9a22021-02-12 16:50:07 -08005863 /*
5864 * PML is emulated for an L1 VMM and should never be enabled in
5865 * vmcs02, always "handle" PML_FULL by exiting to userspace.
5866 */
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005867 return true;
5868 case EXIT_REASON_VMFUNC:
5869 /* VM functions are emulated through L2->L0 vmexits. */
5870 return true;
Chenyi Qiang24a996a2021-09-14 17:50:41 +08005871 case EXIT_REASON_BUS_LOCK:
5872 /*
5873 * At present, bus lock VM exit is never exposed to L1.
5874 * Handle L2's bus locks in L0 directly.
5875 */
5876 return true;
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005877 default:
5878 break;
5879 }
5880 return false;
5881}
5882
5883/*
5884 * Return 1 if L1 wants to intercept an exit from L2. Only call this when in
5885 * is_guest_mode (L2).
5886 */
Sean Christopherson8e533242020-11-06 17:03:12 +08005887static bool nested_vmx_l1_wants_exit(struct kvm_vcpu *vcpu,
5888 union vmx_exit_reason exit_reason)
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005889{
Sean Christopherson55d23752018-12-03 13:53:18 -08005890 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Sean Christopherson9bd4af22020-04-21 00:53:27 -07005891 u32 intr_info;
Sean Christopherson55d23752018-12-03 13:53:18 -08005892
Sean Christopherson8e533242020-11-06 17:03:12 +08005893 switch ((u16)exit_reason.basic) {
Sean Christopherson55d23752018-12-03 13:53:18 -08005894 case EXIT_REASON_EXCEPTION_NMI:
Sean Christopherson87915852020-04-15 13:34:54 -07005895 intr_info = vmx_get_intr_info(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005896 if (is_nmi(intr_info))
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005897 return true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005898 else if (is_page_fault(intr_info))
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005899 return true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005900 return vmcs12->exception_bitmap &
5901 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
5902 case EXIT_REASON_EXTERNAL_INTERRUPT:
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005903 return nested_exit_on_intr(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005904 case EXIT_REASON_TRIPLE_FAULT:
5905 return true;
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08005906 case EXIT_REASON_INTERRUPT_WINDOW:
5907 return nested_cpu_has(vmcs12, CPU_BASED_INTR_WINDOW_EXITING);
Sean Christopherson55d23752018-12-03 13:53:18 -08005908 case EXIT_REASON_NMI_WINDOW:
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08005909 return nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING);
Sean Christopherson55d23752018-12-03 13:53:18 -08005910 case EXIT_REASON_TASK_SWITCH:
5911 return true;
5912 case EXIT_REASON_CPUID:
5913 return true;
5914 case EXIT_REASON_HLT:
5915 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
5916 case EXIT_REASON_INVD:
5917 return true;
5918 case EXIT_REASON_INVLPG:
5919 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
5920 case EXIT_REASON_RDPMC:
5921 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
5922 case EXIT_REASON_RDRAND:
5923 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
5924 case EXIT_REASON_RDSEED:
5925 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
5926 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
5927 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
5928 case EXIT_REASON_VMREAD:
5929 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
5930 vmcs12->vmread_bitmap);
5931 case EXIT_REASON_VMWRITE:
5932 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
5933 vmcs12->vmwrite_bitmap);
5934 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
5935 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
5936 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
5937 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
5938 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
5939 /*
5940 * VMX instructions trap unconditionally. This allows L1 to
5941 * emulate them for its L2 guest, i.e., allows 3-level nesting!
5942 */
5943 return true;
5944 case EXIT_REASON_CR_ACCESS:
5945 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
5946 case EXIT_REASON_DR_ACCESS:
5947 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
5948 case EXIT_REASON_IO_INSTRUCTION:
5949 return nested_vmx_exit_handled_io(vcpu, vmcs12);
5950 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
5951 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
5952 case EXIT_REASON_MSR_READ:
5953 case EXIT_REASON_MSR_WRITE:
5954 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
5955 case EXIT_REASON_INVALID_STATE:
5956 return true;
5957 case EXIT_REASON_MWAIT_INSTRUCTION:
5958 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
5959 case EXIT_REASON_MONITOR_TRAP_FLAG:
Oliver Uptonb045ae92020-04-14 22:47:45 +00005960 return nested_vmx_exit_handled_mtf(vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08005961 case EXIT_REASON_MONITOR_INSTRUCTION:
5962 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
5963 case EXIT_REASON_PAUSE_INSTRUCTION:
5964 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
5965 nested_cpu_has2(vmcs12,
5966 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
5967 case EXIT_REASON_MCE_DURING_VMENTRY:
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005968 return true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005969 case EXIT_REASON_TPR_BELOW_THRESHOLD:
5970 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
5971 case EXIT_REASON_APIC_ACCESS:
5972 case EXIT_REASON_APIC_WRITE:
5973 case EXIT_REASON_EOI_INDUCED:
5974 /*
5975 * The controls for "virtualize APIC accesses," "APIC-
5976 * register virtualization," and "virtual-interrupt
5977 * delivery" only come from vmcs12.
5978 */
5979 return true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005980 case EXIT_REASON_INVPCID:
5981 return
5982 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
5983 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
5984 case EXIT_REASON_WBINVD:
5985 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
5986 case EXIT_REASON_XSETBV:
5987 return true;
5988 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
5989 /*
5990 * This should never happen, since it is not possible to
5991 * set XSS to a non-zero value---neither in L1 nor in L2.
5992 * If if it were, XSS would have to be checked against
5993 * the XSS exit bitmap in vmcs12.
5994 */
5995 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Tao Xubf653b72019-07-16 14:55:51 +08005996 case EXIT_REASON_UMWAIT:
5997 case EXIT_REASON_TPAUSE:
5998 return nested_cpu_has2(vmcs12,
5999 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE);
Sean Christopherson72add912021-04-12 16:21:42 +12006000 case EXIT_REASON_ENCLS:
6001 return nested_vmx_exit_handled_encls(vcpu, vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08006002 default:
6003 return true;
6004 }
6005}
6006
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006007/*
6008 * Conditionally reflect a VM-Exit into L1. Returns %true if the VM-Exit was
6009 * reflected into L1.
6010 */
Sean Christophersonf47baae2020-04-15 10:55:16 -07006011bool nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu)
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006012{
Sean Christophersonfbdd5022020-04-15 10:55:12 -07006013 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson8e533242020-11-06 17:03:12 +08006014 union vmx_exit_reason exit_reason = vmx->exit_reason;
Sean Christopherson87796552020-04-22 17:11:27 -07006015 unsigned long exit_qual;
6016 u32 exit_intr_info;
Sean Christophersonfbdd5022020-04-15 10:55:12 -07006017
6018 WARN_ON_ONCE(vmx->nested.nested_run_pending);
6019
6020 /*
6021 * Late nested VM-Fail shares the same flow as nested VM-Exit since KVM
6022 * has already loaded L2's state.
6023 */
6024 if (unlikely(vmx->fail)) {
6025 trace_kvm_nested_vmenter_failed(
6026 "hardware VM-instruction error: ",
6027 vmcs_read32(VM_INSTRUCTION_ERROR));
6028 exit_intr_info = 0;
6029 exit_qual = 0;
6030 goto reflect_vmexit;
6031 }
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006032
David Edmondson0a62a032021-09-20 11:37:35 +01006033 trace_kvm_nested_vmexit(vcpu, KVM_ISA_VMX);
Sean Christopherson236871b2020-04-15 10:55:13 -07006034
Sean Christopherson2c1f3322020-04-15 10:55:14 -07006035 /* If L0 (KVM) wants the exit, it trumps L1's desires. */
6036 if (nested_vmx_l0_wants_exit(vcpu, exit_reason))
6037 return false;
6038
6039 /* If L1 doesn't want the exit, handle it in L0. */
6040 if (!nested_vmx_l1_wants_exit(vcpu, exit_reason))
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006041 return false;
6042
6043 /*
Sean Christopherson1d283062020-04-15 10:55:15 -07006044 * vmcs.VM_EXIT_INTR_INFO is only valid for EXCEPTION_NMI exits. For
6045 * EXTERNAL_INTERRUPT, the value for vmcs12->vm_exit_intr_info would
6046 * need to be synthesized by querying the in-kernel LAPIC, but external
6047 * interrupts are never reflected to L1 so it's a non-issue.
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006048 */
Sean Christopherson02f19652020-09-23 13:13:49 -07006049 exit_intr_info = vmx_get_intr_info(vcpu);
Sean Christophersonf315f2b2020-09-23 13:13:45 -07006050 if (is_exception_with_error_code(exit_intr_info)) {
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006051 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6052
6053 vmcs12->vm_exit_intr_error_code =
6054 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
6055 }
Sean Christopherson02f19652020-09-23 13:13:49 -07006056 exit_qual = vmx_get_exit_qual(vcpu);
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006057
Sean Christophersonfbdd5022020-04-15 10:55:12 -07006058reflect_vmexit:
Sean Christopherson8e533242020-11-06 17:03:12 +08006059 nested_vmx_vmexit(vcpu, exit_reason.full, exit_intr_info, exit_qual);
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006060 return true;
6061}
Sean Christopherson55d23752018-12-03 13:53:18 -08006062
6063static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
6064 struct kvm_nested_state __user *user_kvm_nested_state,
6065 u32 user_data_size)
6066{
6067 struct vcpu_vmx *vmx;
6068 struct vmcs12 *vmcs12;
6069 struct kvm_nested_state kvm_state = {
6070 .flags = 0,
Liran Alon6ca00df2019-06-16 15:03:10 +03006071 .format = KVM_STATE_NESTED_FORMAT_VMX,
Sean Christopherson55d23752018-12-03 13:53:18 -08006072 .size = sizeof(kvm_state),
Peter Shier850448f2020-05-26 14:51:06 -07006073 .hdr.vmx.flags = 0,
Yu Zhang64c78502021-09-30 01:51:53 +08006074 .hdr.vmx.vmxon_pa = INVALID_GPA,
6075 .hdr.vmx.vmcs12_pa = INVALID_GPA,
Peter Shier850448f2020-05-26 14:51:06 -07006076 .hdr.vmx.preemption_timer_deadline = 0,
Sean Christopherson55d23752018-12-03 13:53:18 -08006077 };
Liran Alon6ca00df2019-06-16 15:03:10 +03006078 struct kvm_vmx_nested_state_data __user *user_vmx_nested_state =
6079 &user_kvm_nested_state->data.vmx[0];
Sean Christopherson55d23752018-12-03 13:53:18 -08006080
6081 if (!vcpu)
Liran Alon6ca00df2019-06-16 15:03:10 +03006082 return kvm_state.size + sizeof(*user_vmx_nested_state);
Sean Christopherson55d23752018-12-03 13:53:18 -08006083
6084 vmx = to_vmx(vcpu);
6085 vmcs12 = get_vmcs12(vcpu);
6086
Sean Christopherson55d23752018-12-03 13:53:18 -08006087 if (nested_vmx_allowed(vcpu) &&
6088 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
Liran Alon6ca00df2019-06-16 15:03:10 +03006089 kvm_state.hdr.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
6090 kvm_state.hdr.vmx.vmcs12_pa = vmx->nested.current_vmptr;
Sean Christopherson55d23752018-12-03 13:53:18 -08006091
6092 if (vmx_has_valid_vmcs12(vcpu)) {
Liran Alon6ca00df2019-06-16 15:03:10 +03006093 kvm_state.size += sizeof(user_vmx_nested_state->vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08006094
Vitaly Kuznetsov27849962021-05-26 15:20:20 +02006095 /* 'hv_evmcs_vmptr' can also be EVMPTR_MAP_PENDING here */
6096 if (vmx->nested.hv_evmcs_vmptr != EVMPTR_INVALID)
Liran Alon323d73a2019-06-26 16:09:27 +03006097 kvm_state.flags |= KVM_STATE_NESTED_EVMCS;
6098
Sean Christopherson55d23752018-12-03 13:53:18 -08006099 if (is_guest_mode(vcpu) &&
6100 nested_cpu_has_shadow_vmcs(vmcs12) &&
Yu Zhang64c78502021-09-30 01:51:53 +08006101 vmcs12->vmcs_link_pointer != INVALID_GPA)
Liran Alon6ca00df2019-06-16 15:03:10 +03006102 kvm_state.size += sizeof(user_vmx_nested_state->shadow_vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08006103 }
6104
6105 if (vmx->nested.smm.vmxon)
Liran Alon6ca00df2019-06-16 15:03:10 +03006106 kvm_state.hdr.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
Sean Christopherson55d23752018-12-03 13:53:18 -08006107
6108 if (vmx->nested.smm.guest_mode)
Liran Alon6ca00df2019-06-16 15:03:10 +03006109 kvm_state.hdr.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
Sean Christopherson55d23752018-12-03 13:53:18 -08006110
6111 if (is_guest_mode(vcpu)) {
6112 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
6113
6114 if (vmx->nested.nested_run_pending)
6115 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
Oliver Upton5ef8acb2020-02-07 02:36:07 -08006116
6117 if (vmx->nested.mtf_pending)
6118 kvm_state.flags |= KVM_STATE_NESTED_MTF_PENDING;
Peter Shier850448f2020-05-26 14:51:06 -07006119
6120 if (nested_cpu_has_preemption_timer(vmcs12) &&
6121 vmx->nested.has_preemption_timer_deadline) {
6122 kvm_state.hdr.vmx.flags |=
6123 KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE;
6124 kvm_state.hdr.vmx.preemption_timer_deadline =
6125 vmx->nested.preemption_timer_deadline;
6126 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006127 }
6128 }
6129
6130 if (user_data_size < kvm_state.size)
6131 goto out;
6132
6133 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
6134 return -EFAULT;
6135
6136 if (!vmx_has_valid_vmcs12(vcpu))
6137 goto out;
6138
6139 /*
6140 * When running L2, the authoritative vmcs12 state is in the
6141 * vmcs02. When running L1, the authoritative vmcs12 state is
6142 * in the shadow or enlightened vmcs linked to vmcs01, unless
Sean Christopherson3731905ef2019-05-07 08:36:27 -07006143 * need_vmcs12_to_shadow_sync is set, in which case, the authoritative
Sean Christopherson55d23752018-12-03 13:53:18 -08006144 * vmcs12 state is in the vmcs12 already.
6145 */
6146 if (is_guest_mode(vcpu)) {
Sean Christopherson3731905ef2019-05-07 08:36:27 -07006147 sync_vmcs02_to_vmcs12(vcpu, vmcs12);
Sean Christopherson7952d762019-05-07 08:36:29 -07006148 sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
Maxim Levitskyd51e1d32021-01-14 22:54:47 +02006149 } else {
6150 copy_vmcs02_to_vmcs12_rare(vcpu, get_vmcs12(vcpu));
6151 if (!vmx->nested.need_vmcs12_to_shadow_sync) {
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02006152 if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02006153 /*
6154 * L1 hypervisor is not obliged to keep eVMCS
6155 * clean fields data always up-to-date while
6156 * not in guest mode, 'hv_clean_fields' is only
6157 * supposed to be actual upon vmentry so we need
6158 * to ignore it here and do full copy.
6159 */
6160 copy_enlightened_to_vmcs12(vmx, 0);
Maxim Levitskyd51e1d32021-01-14 22:54:47 +02006161 else if (enable_shadow_vmcs)
6162 copy_shadow_to_vmcs12(vmx);
6163 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006164 }
6165
Liran Alon6ca00df2019-06-16 15:03:10 +03006166 BUILD_BUG_ON(sizeof(user_vmx_nested_state->vmcs12) < VMCS12_SIZE);
6167 BUILD_BUG_ON(sizeof(user_vmx_nested_state->shadow_vmcs12) < VMCS12_SIZE);
6168
Tom Roeder3a33d032019-01-24 13:48:20 -08006169 /*
6170 * Copy over the full allocated size of vmcs12 rather than just the size
6171 * of the struct.
6172 */
Liran Alon6ca00df2019-06-16 15:03:10 +03006173 if (copy_to_user(user_vmx_nested_state->vmcs12, vmcs12, VMCS12_SIZE))
Sean Christopherson55d23752018-12-03 13:53:18 -08006174 return -EFAULT;
6175
6176 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
Yu Zhang64c78502021-09-30 01:51:53 +08006177 vmcs12->vmcs_link_pointer != INVALID_GPA) {
Liran Alon6ca00df2019-06-16 15:03:10 +03006178 if (copy_to_user(user_vmx_nested_state->shadow_vmcs12,
Tom Roeder3a33d032019-01-24 13:48:20 -08006179 get_shadow_vmcs12(vcpu), VMCS12_SIZE))
Sean Christopherson55d23752018-12-03 13:53:18 -08006180 return -EFAULT;
6181 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006182out:
6183 return kvm_state.size;
6184}
6185
6186/*
6187 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
6188 */
6189void vmx_leave_nested(struct kvm_vcpu *vcpu)
6190{
6191 if (is_guest_mode(vcpu)) {
6192 to_vmx(vcpu)->nested.nested_run_pending = 0;
6193 nested_vmx_vmexit(vcpu, -1, 0, 0);
6194 }
6195 free_nested(vcpu);
6196}
6197
6198static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
6199 struct kvm_nested_state __user *user_kvm_nested_state,
6200 struct kvm_nested_state *kvm_state)
6201{
6202 struct vcpu_vmx *vmx = to_vmx(vcpu);
6203 struct vmcs12 *vmcs12;
Sean Christopherson68cda402020-05-11 15:05:29 -07006204 enum vm_entry_failure_code ignored;
Liran Alon6ca00df2019-06-16 15:03:10 +03006205 struct kvm_vmx_nested_state_data __user *user_vmx_nested_state =
6206 &user_kvm_nested_state->data.vmx[0];
Sean Christopherson55d23752018-12-03 13:53:18 -08006207 int ret;
6208
Liran Alon6ca00df2019-06-16 15:03:10 +03006209 if (kvm_state->format != KVM_STATE_NESTED_FORMAT_VMX)
Sean Christopherson55d23752018-12-03 13:53:18 -08006210 return -EINVAL;
6211
Yu Zhang64c78502021-09-30 01:51:53 +08006212 if (kvm_state->hdr.vmx.vmxon_pa == INVALID_GPA) {
Liran Alon6ca00df2019-06-16 15:03:10 +03006213 if (kvm_state->hdr.vmx.smm.flags)
Sean Christopherson55d23752018-12-03 13:53:18 -08006214 return -EINVAL;
6215
Yu Zhang64c78502021-09-30 01:51:53 +08006216 if (kvm_state->hdr.vmx.vmcs12_pa != INVALID_GPA)
Sean Christopherson55d23752018-12-03 13:53:18 -08006217 return -EINVAL;
6218
Liran Alon323d73a2019-06-26 16:09:27 +03006219 /*
6220 * KVM_STATE_NESTED_EVMCS used to signal that KVM should
6221 * enable eVMCS capability on vCPU. However, since then
6222 * code was changed such that flag signals vmcs12 should
6223 * be copied into eVMCS in guest memory.
6224 *
6225 * To preserve backwards compatability, allow user
6226 * to set this flag even when there is no VMXON region.
6227 */
Paolo Bonzini9fd58872019-06-19 16:52:27 +02006228 if (kvm_state->flags & ~KVM_STATE_NESTED_EVMCS)
6229 return -EINVAL;
6230 } else {
6231 if (!nested_vmx_allowed(vcpu))
6232 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08006233
Paolo Bonzini9fd58872019-06-19 16:52:27 +02006234 if (!page_address_valid(vcpu, kvm_state->hdr.vmx.vmxon_pa))
6235 return -EINVAL;
Liran Alon323d73a2019-06-26 16:09:27 +03006236 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006237
Liran Alon6ca00df2019-06-16 15:03:10 +03006238 if ((kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
Sean Christopherson55d23752018-12-03 13:53:18 -08006239 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
6240 return -EINVAL;
6241
Liran Alon6ca00df2019-06-16 15:03:10 +03006242 if (kvm_state->hdr.vmx.smm.flags &
Sean Christopherson55d23752018-12-03 13:53:18 -08006243 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
6244 return -EINVAL;
6245
Paolo Bonzini5e105c82020-07-27 08:55:09 -04006246 if (kvm_state->hdr.vmx.flags & ~KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE)
6247 return -EINVAL;
6248
Sean Christopherson55d23752018-12-03 13:53:18 -08006249 /*
6250 * SMM temporarily disables VMX, so we cannot be in guest mode,
6251 * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags
6252 * must be zero.
6253 */
Liran Alon65b712f12019-06-25 14:26:42 +03006254 if (is_smm(vcpu) ?
6255 (kvm_state->flags &
6256 (KVM_STATE_NESTED_GUEST_MODE | KVM_STATE_NESTED_RUN_PENDING))
6257 : kvm_state->hdr.vmx.smm.flags)
Sean Christopherson55d23752018-12-03 13:53:18 -08006258 return -EINVAL;
6259
Liran Alon6ca00df2019-06-16 15:03:10 +03006260 if ((kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
6261 !(kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
Sean Christopherson55d23752018-12-03 13:53:18 -08006262 return -EINVAL;
6263
Liran Alon323d73a2019-06-26 16:09:27 +03006264 if ((kvm_state->flags & KVM_STATE_NESTED_EVMCS) &&
6265 (!nested_vmx_allowed(vcpu) || !vmx->nested.enlightened_vmcs_enabled))
Paolo Bonzini9fd58872019-06-19 16:52:27 +02006266 return -EINVAL;
6267
Liran Alon323d73a2019-06-26 16:09:27 +03006268 vmx_leave_nested(vcpu);
Paolo Bonzini9fd58872019-06-19 16:52:27 +02006269
Yu Zhang64c78502021-09-30 01:51:53 +08006270 if (kvm_state->hdr.vmx.vmxon_pa == INVALID_GPA)
Sean Christopherson55d23752018-12-03 13:53:18 -08006271 return 0;
6272
Liran Alon6ca00df2019-06-16 15:03:10 +03006273 vmx->nested.vmxon_ptr = kvm_state->hdr.vmx.vmxon_pa;
Sean Christopherson55d23752018-12-03 13:53:18 -08006274 ret = enter_vmx_operation(vcpu);
6275 if (ret)
6276 return ret;
6277
Paolo Bonzini0f02bd02020-07-27 09:00:37 -04006278 /* Empty 'VMXON' state is permitted if no VMCS loaded */
6279 if (kvm_state->size < sizeof(*kvm_state) + sizeof(*vmcs12)) {
6280 /* See vmx_has_valid_vmcs12. */
6281 if ((kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE) ||
6282 (kvm_state->flags & KVM_STATE_NESTED_EVMCS) ||
Yu Zhang64c78502021-09-30 01:51:53 +08006283 (kvm_state->hdr.vmx.vmcs12_pa != INVALID_GPA))
Paolo Bonzini0f02bd02020-07-27 09:00:37 -04006284 return -EINVAL;
6285 else
6286 return 0;
6287 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006288
Yu Zhang64c78502021-09-30 01:51:53 +08006289 if (kvm_state->hdr.vmx.vmcs12_pa != INVALID_GPA) {
Liran Alon6ca00df2019-06-16 15:03:10 +03006290 if (kvm_state->hdr.vmx.vmcs12_pa == kvm_state->hdr.vmx.vmxon_pa ||
6291 !page_address_valid(vcpu, kvm_state->hdr.vmx.vmcs12_pa))
Sean Christopherson55d23752018-12-03 13:53:18 -08006292 return -EINVAL;
6293
Liran Alon6ca00df2019-06-16 15:03:10 +03006294 set_current_vmptr(vmx, kvm_state->hdr.vmx.vmcs12_pa);
Sean Christopherson55d23752018-12-03 13:53:18 -08006295 } else if (kvm_state->flags & KVM_STATE_NESTED_EVMCS) {
6296 /*
Vitaly Kuznetsove942dbf2020-03-09 16:52:12 +01006297 * nested_vmx_handle_enlightened_vmptrld() cannot be called
6298 * directly from here as HV_X64_MSR_VP_ASSIST_PAGE may not be
6299 * restored yet. EVMCS will be mapped from
6300 * nested_get_vmcs12_pages().
Sean Christopherson55d23752018-12-03 13:53:18 -08006301 */
Vitaly Kuznetsov27849962021-05-26 15:20:20 +02006302 vmx->nested.hv_evmcs_vmptr = EVMPTR_MAP_PENDING;
Paolo Bonzini729c15c2020-09-22 06:53:57 -04006303 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08006304 } else {
6305 return -EINVAL;
6306 }
6307
Liran Alon6ca00df2019-06-16 15:03:10 +03006308 if (kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
Sean Christopherson55d23752018-12-03 13:53:18 -08006309 vmx->nested.smm.vmxon = true;
6310 vmx->nested.vmxon = false;
6311
Liran Alon6ca00df2019-06-16 15:03:10 +03006312 if (kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
Sean Christopherson55d23752018-12-03 13:53:18 -08006313 vmx->nested.smm.guest_mode = true;
6314 }
6315
6316 vmcs12 = get_vmcs12(vcpu);
Liran Alon6ca00df2019-06-16 15:03:10 +03006317 if (copy_from_user(vmcs12, user_vmx_nested_state->vmcs12, sizeof(*vmcs12)))
Sean Christopherson55d23752018-12-03 13:53:18 -08006318 return -EFAULT;
6319
6320 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
6321 return -EINVAL;
6322
6323 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
6324 return 0;
6325
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006326 vmx->nested.nested_run_pending =
6327 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
6328
Oliver Upton5ef8acb2020-02-07 02:36:07 -08006329 vmx->nested.mtf_pending =
6330 !!(kvm_state->flags & KVM_STATE_NESTED_MTF_PENDING);
6331
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006332 ret = -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08006333 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
Yu Zhang64c78502021-09-30 01:51:53 +08006334 vmcs12->vmcs_link_pointer != INVALID_GPA) {
Sean Christopherson55d23752018-12-03 13:53:18 -08006335 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
6336
Liran Alon6ca00df2019-06-16 15:03:10 +03006337 if (kvm_state->size <
6338 sizeof(*kvm_state) +
6339 sizeof(user_vmx_nested_state->vmcs12) + sizeof(*shadow_vmcs12))
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006340 goto error_guest_mode;
Sean Christopherson55d23752018-12-03 13:53:18 -08006341
6342 if (copy_from_user(shadow_vmcs12,
Liran Alon6ca00df2019-06-16 15:03:10 +03006343 user_vmx_nested_state->shadow_vmcs12,
6344 sizeof(*shadow_vmcs12))) {
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006345 ret = -EFAULT;
6346 goto error_guest_mode;
6347 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006348
6349 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
6350 !shadow_vmcs12->hdr.shadow_vmcs)
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006351 goto error_guest_mode;
Sean Christopherson55d23752018-12-03 13:53:18 -08006352 }
6353
Paolo Bonzini83d31e52020-07-09 13:12:09 -04006354 vmx->nested.has_preemption_timer_deadline = false;
Peter Shier850448f2020-05-26 14:51:06 -07006355 if (kvm_state->hdr.vmx.flags & KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE) {
6356 vmx->nested.has_preemption_timer_deadline = true;
6357 vmx->nested.preemption_timer_deadline =
6358 kvm_state->hdr.vmx.preemption_timer_deadline;
6359 }
6360
Sean Christopherson5478ba32019-04-11 12:18:06 -07006361 if (nested_vmx_check_controls(vcpu, vmcs12) ||
6362 nested_vmx_check_host_state(vcpu, vmcs12) ||
Sean Christopherson68cda402020-05-11 15:05:29 -07006363 nested_vmx_check_guest_state(vcpu, vmcs12, &ignored))
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006364 goto error_guest_mode;
Sean Christopherson55d23752018-12-03 13:53:18 -08006365
6366 vmx->nested.dirty_vmcs12 = true;
6367 ret = nested_vmx_enter_non_root_mode(vcpu, false);
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006368 if (ret)
6369 goto error_guest_mode;
Sean Christopherson55d23752018-12-03 13:53:18 -08006370
6371 return 0;
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006372
6373error_guest_mode:
6374 vmx->nested.nested_run_pending = 0;
6375 return ret;
Sean Christopherson55d23752018-12-03 13:53:18 -08006376}
6377
Xiaoyao Li1b842922019-10-20 17:11:01 +08006378void nested_vmx_set_vmcs_shadowing_bitmap(void)
Sean Christopherson55d23752018-12-03 13:53:18 -08006379{
6380 if (enable_shadow_vmcs) {
Sean Christopherson55d23752018-12-03 13:53:18 -08006381 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Sean Christophersonfadcead2019-05-07 08:36:23 -07006382 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
Sean Christopherson55d23752018-12-03 13:53:18 -08006383 }
6384}
6385
6386/*
Sean Christophersonba1f8242021-06-18 14:46:58 -07006387 * Indexing into the vmcs12 uses the VMCS encoding rotated left by 6. Undo
6388 * that madness to get the encoding for comparison.
6389 */
6390#define VMCS12_IDX_TO_ENC(idx) ((u16)(((u16)(idx) >> 6) | ((u16)(idx) << 10)))
6391
6392static u64 nested_vmx_calc_vmcs_enum_msr(void)
6393{
6394 /*
6395 * Note these are the so called "index" of the VMCS field encoding, not
6396 * the index into vmcs12.
6397 */
6398 unsigned int max_idx, idx;
6399 int i;
6400
6401 /*
6402 * For better or worse, KVM allows VMREAD/VMWRITE to all fields in
6403 * vmcs12, regardless of whether or not the associated feature is
6404 * exposed to L1. Simply find the field with the highest index.
6405 */
6406 max_idx = 0;
6407 for (i = 0; i < nr_vmcs12_fields; i++) {
6408 /* The vmcs12 table is very, very sparsely populated. */
6409 if (!vmcs_field_to_offset_table[i])
6410 continue;
6411
6412 idx = vmcs_field_index(VMCS12_IDX_TO_ENC(i));
6413 if (idx > max_idx)
6414 max_idx = idx;
6415 }
6416
6417 return (u64)max_idx << VMCS_FIELD_INDEX_SHIFT;
6418}
6419
6420/*
Sean Christopherson55d23752018-12-03 13:53:18 -08006421 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
6422 * returned for the various VMX controls MSRs when nested VMX is enabled.
6423 * The same values should also be used to verify that vmcs12 control fields are
6424 * valid during nested entry from L1 to L2.
6425 * Each of these control msrs has a low and high 32-bit half: A low bit is on
6426 * if the corresponding bit in the (32-bit) control field *must* be on, and a
6427 * bit in the high half is on if the corresponding bit in the control field
6428 * may be on. See also vmx_control_verify().
6429 */
Vitaly Kuznetsova4443262020-02-20 18:22:04 +01006430void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, u32 ept_caps)
Sean Christopherson55d23752018-12-03 13:53:18 -08006431{
6432 /*
6433 * Note that as a general rule, the high half of the MSRs (bits in
6434 * the control fields which may be 1) should be initialized by the
6435 * intersection of the underlying hardware's MSR (i.e., features which
6436 * can be supported) and the list of features we want to expose -
6437 * because they are known to be properly supported in our code.
6438 * Also, usually, the low half of the MSRs (bits which must be 1) can
6439 * be set to 0, meaning that L1 may turn off any of these bits. The
6440 * reason is that if one of these bits is necessary, it will appear
6441 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
6442 * fields of vmcs01 and vmcs02, will turn these bits off - and
Sean Christopherson2c1f3322020-04-15 10:55:14 -07006443 * nested_vmx_l1_wants_exit() will not pass related exits to L1.
Sean Christopherson55d23752018-12-03 13:53:18 -08006444 * These rules have exceptions below.
6445 */
6446
6447 /* pin-based controls */
6448 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
6449 msrs->pinbased_ctls_low,
6450 msrs->pinbased_ctls_high);
6451 msrs->pinbased_ctls_low |=
6452 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
6453 msrs->pinbased_ctls_high &=
6454 PIN_BASED_EXT_INTR_MASK |
6455 PIN_BASED_NMI_EXITING |
6456 PIN_BASED_VIRTUAL_NMIS |
Vitaly Kuznetsova4443262020-02-20 18:22:04 +01006457 (enable_apicv ? PIN_BASED_POSTED_INTR : 0);
Sean Christopherson55d23752018-12-03 13:53:18 -08006458 msrs->pinbased_ctls_high |=
6459 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
6460 PIN_BASED_VMX_PREEMPTION_TIMER;
6461
6462 /* exit controls */
6463 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
6464 msrs->exit_ctls_low,
6465 msrs->exit_ctls_high);
6466 msrs->exit_ctls_low =
6467 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
6468
6469 msrs->exit_ctls_high &=
6470#ifdef CONFIG_X86_64
6471 VM_EXIT_HOST_ADDR_SPACE_SIZE |
6472#endif
Chenyi Qiangefc83132020-08-28 16:56:18 +08006473 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT |
6474 VM_EXIT_CLEAR_BNDCFGS | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
Sean Christopherson55d23752018-12-03 13:53:18 -08006475 msrs->exit_ctls_high |=
6476 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
6477 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
6478 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
6479
6480 /* We support free control of debug control saving. */
6481 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
6482
6483 /* entry controls */
6484 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
6485 msrs->entry_ctls_low,
6486 msrs->entry_ctls_high);
6487 msrs->entry_ctls_low =
6488 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
6489 msrs->entry_ctls_high &=
6490#ifdef CONFIG_X86_64
6491 VM_ENTRY_IA32E_MODE |
6492#endif
Chenyi Qiangefc83132020-08-28 16:56:18 +08006493 VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS |
6494 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
Sean Christopherson55d23752018-12-03 13:53:18 -08006495 msrs->entry_ctls_high |=
6496 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
6497
6498 /* We support free control of debug control loading. */
6499 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
6500
6501 /* cpu-based controls */
6502 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
6503 msrs->procbased_ctls_low,
6504 msrs->procbased_ctls_high);
6505 msrs->procbased_ctls_low =
6506 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
6507 msrs->procbased_ctls_high &=
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08006508 CPU_BASED_INTR_WINDOW_EXITING |
Xiaoyao Li5e3d3942019-12-06 16:45:26 +08006509 CPU_BASED_NMI_WINDOW_EXITING | CPU_BASED_USE_TSC_OFFSETTING |
Sean Christopherson55d23752018-12-03 13:53:18 -08006510 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
6511 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
6512 CPU_BASED_CR3_STORE_EXITING |
6513#ifdef CONFIG_X86_64
6514 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
6515#endif
6516 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
6517 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
6518 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
6519 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
6520 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
6521 /*
6522 * We can allow some features even when not supported by the
6523 * hardware. For example, L1 can specify an MSR bitmap - and we
6524 * can use it to avoid exits to L1 - even when L0 runs L2
6525 * without MSR bitmaps.
6526 */
6527 msrs->procbased_ctls_high |=
6528 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
6529 CPU_BASED_USE_MSR_BITMAPS;
6530
6531 /* We support free control of CR3 access interception. */
6532 msrs->procbased_ctls_low &=
6533 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
6534
6535 /*
6536 * secondary cpu-based controls. Do not include those that
Xiaoyao Li7c1b7612020-07-09 12:34:25 +08006537 * depend on CPUID bits, they are added later by
6538 * vmx_vcpu_after_set_cpuid.
Sean Christopherson55d23752018-12-03 13:53:18 -08006539 */
Vitaly Kuznetsov6b1971c2019-02-07 11:42:14 +01006540 if (msrs->procbased_ctls_high & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)
6541 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
6542 msrs->secondary_ctls_low,
6543 msrs->secondary_ctls_high);
6544
Sean Christopherson55d23752018-12-03 13:53:18 -08006545 msrs->secondary_ctls_low = 0;
6546 msrs->secondary_ctls_high &=
6547 SECONDARY_EXEC_DESC |
Sean Christopherson7f3603b2020-09-23 09:50:47 -07006548 SECONDARY_EXEC_ENABLE_RDTSCP |
Sean Christopherson55d23752018-12-03 13:53:18 -08006549 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini6defc592019-07-02 14:39:29 +02006550 SECONDARY_EXEC_WBINVD_EXITING |
Sean Christopherson55d23752018-12-03 13:53:18 -08006551 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6552 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini6defc592019-07-02 14:39:29 +02006553 SECONDARY_EXEC_RDRAND_EXITING |
6554 SECONDARY_EXEC_ENABLE_INVPCID |
6555 SECONDARY_EXEC_RDSEED_EXITING |
Ilias Stamatisd041b5e2021-05-26 19:44:17 +01006556 SECONDARY_EXEC_XSAVES |
6557 SECONDARY_EXEC_TSC_SCALING;
Sean Christopherson55d23752018-12-03 13:53:18 -08006558
6559 /*
6560 * We can emulate "VMCS shadowing," even if the hardware
6561 * doesn't support it.
6562 */
6563 msrs->secondary_ctls_high |=
6564 SECONDARY_EXEC_SHADOW_VMCS;
6565
6566 if (enable_ept) {
6567 /* nested EPT: emulate EPT also to L1 */
6568 msrs->secondary_ctls_high |=
6569 SECONDARY_EXEC_ENABLE_EPT;
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08006570 msrs->ept_caps =
6571 VMX_EPT_PAGE_WALK_4_BIT |
6572 VMX_EPT_PAGE_WALK_5_BIT |
6573 VMX_EPTP_WB_BIT |
Sean Christopherson96d47012020-03-02 18:02:40 -08006574 VMX_EPT_INVEPT_BIT |
6575 VMX_EPT_EXECUTE_ONLY_BIT;
6576
Sean Christopherson55d23752018-12-03 13:53:18 -08006577 msrs->ept_caps &= ept_caps;
6578 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
6579 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
6580 VMX_EPT_1GB_PAGE_BIT;
6581 if (enable_ept_ad_bits) {
6582 msrs->secondary_ctls_high |=
6583 SECONDARY_EXEC_ENABLE_PML;
6584 msrs->ept_caps |= VMX_EPT_AD_BIT;
6585 }
6586 }
6587
6588 if (cpu_has_vmx_vmfunc()) {
6589 msrs->secondary_ctls_high |=
6590 SECONDARY_EXEC_ENABLE_VMFUNC;
6591 /*
6592 * Advertise EPTP switching unconditionally
6593 * since we emulate it
6594 */
6595 if (enable_ept)
6596 msrs->vmfunc_controls =
6597 VMX_VMFUNC_EPTP_SWITCHING;
6598 }
6599
6600 /*
6601 * Old versions of KVM use the single-context version without
6602 * checking for support, so declare that it is supported even
6603 * though it is treated as global context. The alternative is
6604 * not failing the single-context invvpid, and it is worse.
6605 */
6606 if (enable_vpid) {
6607 msrs->secondary_ctls_high |=
6608 SECONDARY_EXEC_ENABLE_VPID;
6609 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
6610 VMX_VPID_EXTENT_SUPPORTED_MASK;
6611 }
6612
6613 if (enable_unrestricted_guest)
6614 msrs->secondary_ctls_high |=
6615 SECONDARY_EXEC_UNRESTRICTED_GUEST;
6616
6617 if (flexpriority_enabled)
6618 msrs->secondary_ctls_high |=
6619 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6620
Sean Christopherson72add912021-04-12 16:21:42 +12006621 if (enable_sgx)
6622 msrs->secondary_ctls_high |= SECONDARY_EXEC_ENCLS_EXITING;
6623
Sean Christopherson55d23752018-12-03 13:53:18 -08006624 /* miscellaneous data */
6625 rdmsr(MSR_IA32_VMX_MISC,
6626 msrs->misc_low,
6627 msrs->misc_high);
6628 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
6629 msrs->misc_low |=
6630 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
6631 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Yadong Qibf0cd882020-11-06 14:51:22 +08006632 VMX_MISC_ACTIVITY_HLT |
6633 VMX_MISC_ACTIVITY_WAIT_SIPI;
Sean Christopherson55d23752018-12-03 13:53:18 -08006634 msrs->misc_high = 0;
6635
6636 /*
6637 * This MSR reports some information about VMX support. We
6638 * should return information about the VMX we emulate for the
6639 * guest, and the VMCS structure we give it - not about the
6640 * VMX support of the underlying hardware.
6641 */
6642 msrs->basic =
6643 VMCS12_REVISION |
6644 VMX_BASIC_TRUE_CTLS |
6645 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
6646 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
6647
6648 if (cpu_has_vmx_basic_inout())
6649 msrs->basic |= VMX_BASIC_INOUT;
6650
6651 /*
6652 * These MSRs specify bits which the guest must keep fixed on
6653 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
6654 * We picked the standard core2 setting.
6655 */
6656#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
6657#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
6658 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
6659 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
6660
6661 /* These MSRs specify bits which the guest must keep fixed off. */
6662 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
6663 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
6664
Sean Christophersonba1f8242021-06-18 14:46:58 -07006665 msrs->vmcs_enum = nested_vmx_calc_vmcs_enum_msr();
Sean Christopherson55d23752018-12-03 13:53:18 -08006666}
6667
6668void nested_vmx_hardware_unsetup(void)
6669{
6670 int i;
6671
6672 if (enable_shadow_vmcs) {
6673 for (i = 0; i < VMX_BITMAP_NR; i++)
6674 free_page((unsigned long)vmx_bitmap[i]);
6675 }
6676}
6677
Sean Christopherson6c1c6e52020-05-06 13:46:53 -07006678__init int nested_vmx_hardware_setup(int (*exit_handlers[])(struct kvm_vcpu *))
Sean Christopherson55d23752018-12-03 13:53:18 -08006679{
6680 int i;
6681
6682 if (!cpu_has_vmx_shadow_vmcs())
6683 enable_shadow_vmcs = 0;
6684 if (enable_shadow_vmcs) {
6685 for (i = 0; i < VMX_BITMAP_NR; i++) {
Ben Gardon41836832019-02-11 11:02:52 -08006686 /*
6687 * The vmx_bitmap is not tied to a VM and so should
6688 * not be charged to a memcg.
6689 */
Sean Christopherson55d23752018-12-03 13:53:18 -08006690 vmx_bitmap[i] = (unsigned long *)
6691 __get_free_page(GFP_KERNEL);
6692 if (!vmx_bitmap[i]) {
6693 nested_vmx_hardware_unsetup();
6694 return -ENOMEM;
6695 }
6696 }
6697
6698 init_vmcs_shadow_fields();
6699 }
6700
Liran Aloncc877672019-11-18 21:11:21 +02006701 exit_handlers[EXIT_REASON_VMCLEAR] = handle_vmclear;
6702 exit_handlers[EXIT_REASON_VMLAUNCH] = handle_vmlaunch;
6703 exit_handlers[EXIT_REASON_VMPTRLD] = handle_vmptrld;
6704 exit_handlers[EXIT_REASON_VMPTRST] = handle_vmptrst;
6705 exit_handlers[EXIT_REASON_VMREAD] = handle_vmread;
6706 exit_handlers[EXIT_REASON_VMRESUME] = handle_vmresume;
6707 exit_handlers[EXIT_REASON_VMWRITE] = handle_vmwrite;
6708 exit_handlers[EXIT_REASON_VMOFF] = handle_vmoff;
6709 exit_handlers[EXIT_REASON_VMON] = handle_vmon;
6710 exit_handlers[EXIT_REASON_INVEPT] = handle_invept;
6711 exit_handlers[EXIT_REASON_INVVPID] = handle_invvpid;
6712 exit_handlers[EXIT_REASON_VMFUNC] = handle_vmfunc;
Sean Christopherson55d23752018-12-03 13:53:18 -08006713
Sean Christopherson55d23752018-12-03 13:53:18 -08006714 return 0;
6715}
Paolo Bonzini33b22172020-04-17 10:24:18 -04006716
6717struct kvm_x86_nested_ops vmx_nested_ops = {
6718 .check_events = vmx_check_nested_events,
Sean Christophersond2060bd2020-04-22 19:25:39 -07006719 .hv_timer_pending = nested_vmx_preemption_timer_pending,
Sean Christophersoncb6a32c2021-03-02 09:45:14 -08006720 .triple_fault = nested_vmx_triple_fault,
Paolo Bonzini33b22172020-04-17 10:24:18 -04006721 .get_state = vmx_get_nested_state,
6722 .set_state = vmx_set_nested_state,
Paolo Bonzini9a78e152021-01-08 11:43:08 -05006723 .get_nested_state_pages = vmx_get_nested_state_pages,
Sean Christopherson02f5fb22020-06-22 14:58:32 -07006724 .write_log_dirty = nested_vmx_write_pml_buffer,
Paolo Bonzini33b22172020-04-17 10:24:18 -04006725 .enable_evmcs = nested_enable_evmcs,
6726 .get_evmcs_version = nested_get_evmcs_version,
6727};