blob: 315fa456d3682bcca61ed865bf4446aa6b051df2 [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{
David Woodhouse297d5972021-11-15 16:50:24 +0000673 struct vcpu_vmx *vmx = to_vmx(vcpu);
674 struct gfn_to_hva_cache *ghc = &vmx->nested.shadow_vmcs12_cache;
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
David Woodhouse297d5972021-11-15 16:50:24 +0000680 if (ghc->gpa != vmcs12->vmcs_link_pointer &&
681 kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc,
682 vmcs12->vmcs_link_pointer, VMCS12_SIZE))
KarimAllah Ahmed88925302019-01-31 21:24:41 +0100683 return;
Sean Christopherson55d23752018-12-03 13:53:18 -0800684
David Woodhouse297d5972021-11-15 16:50:24 +0000685 kvm_read_guest_cached(vmx->vcpu.kvm, ghc, get_shadow_vmcs12(vcpu),
686 VMCS12_SIZE);
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);
David Woodhouse297d5972021-11-15 16:50:24 +0000693 struct gfn_to_hva_cache *ghc = &vmx->nested.shadow_vmcs12_cache;
Sean Christopherson55d23752018-12-03 13:53:18 -0800694
695 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
Yu Zhang64c78502021-09-30 01:51:53 +0800696 vmcs12->vmcs_link_pointer == INVALID_GPA)
Sean Christopherson55d23752018-12-03 13:53:18 -0800697 return;
698
David Woodhouse297d5972021-11-15 16:50:24 +0000699 if (ghc->gpa != vmcs12->vmcs_link_pointer &&
700 kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc,
701 vmcs12->vmcs_link_pointer, VMCS12_SIZE))
702 return;
703
704 kvm_write_guest_cached(vmx->vcpu.kvm, ghc, get_shadow_vmcs12(vcpu),
705 VMCS12_SIZE);
Sean Christopherson55d23752018-12-03 13:53:18 -0800706}
707
708/*
709 * In nested virtualization, check if L1 has set
710 * VM_EXIT_ACK_INTR_ON_EXIT
711 */
712static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
713{
714 return get_vmcs12(vcpu)->vm_exit_controls &
715 VM_EXIT_ACK_INTR_ON_EXIT;
716}
717
Sean Christopherson55d23752018-12-03 13:53:18 -0800718static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
719 struct vmcs12 *vmcs12)
720{
721 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
Sean Christopherson5497b952019-07-11 08:58:29 -0700722 CC(!page_address_valid(vcpu, vmcs12->apic_access_addr)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800723 return -EINVAL;
724 else
725 return 0;
726}
727
728static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
729 struct vmcs12 *vmcs12)
730{
731 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
732 !nested_cpu_has_apic_reg_virt(vmcs12) &&
733 !nested_cpu_has_vid(vmcs12) &&
734 !nested_cpu_has_posted_intr(vmcs12))
735 return 0;
736
737 /*
738 * If virtualize x2apic mode is enabled,
739 * virtualize apic access must be disabled.
740 */
Sean Christopherson5497b952019-07-11 08:58:29 -0700741 if (CC(nested_cpu_has_virt_x2apic_mode(vmcs12) &&
742 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800743 return -EINVAL;
744
745 /*
746 * If virtual interrupt delivery is enabled,
747 * we must exit on external interrupts.
748 */
Sean Christopherson5497b952019-07-11 08:58:29 -0700749 if (CC(nested_cpu_has_vid(vmcs12) && !nested_exit_on_intr(vcpu)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800750 return -EINVAL;
751
752 /*
753 * bits 15:8 should be zero in posted_intr_nv,
754 * the descriptor address has been already checked
755 * in nested_get_vmcs12_pages.
756 *
757 * bits 5:0 of posted_intr_desc_addr should be zero.
758 */
759 if (nested_cpu_has_posted_intr(vmcs12) &&
Sean Christopherson5497b952019-07-11 08:58:29 -0700760 (CC(!nested_cpu_has_vid(vmcs12)) ||
761 CC(!nested_exit_intr_ack_set(vcpu)) ||
762 CC((vmcs12->posted_intr_nv & 0xff00)) ||
Sean Christopherson636e8b72021-02-03 16:01:10 -0800763 CC(!kvm_vcpu_is_legal_aligned_gpa(vcpu, vmcs12->posted_intr_desc_addr, 64))))
Sean Christopherson55d23752018-12-03 13:53:18 -0800764 return -EINVAL;
765
766 /* tpr shadow is needed by all apicv features. */
Sean Christopherson5497b952019-07-11 08:58:29 -0700767 if (CC(!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800768 return -EINVAL;
769
770 return 0;
771}
772
773static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
Sean Christophersonf9b245e2018-12-12 13:30:08 -0500774 u32 count, u64 addr)
Sean Christopherson55d23752018-12-03 13:53:18 -0800775{
Sean Christopherson55d23752018-12-03 13:53:18 -0800776 if (count == 0)
777 return 0;
Sean Christopherson636e8b72021-02-03 16:01:10 -0800778
779 if (!kvm_vcpu_is_legal_aligned_gpa(vcpu, addr, 16) ||
780 !kvm_vcpu_is_legal_gpa(vcpu, (addr + count * sizeof(struct vmx_msr_entry) - 1)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800781 return -EINVAL;
Sean Christophersonf9b245e2018-12-12 13:30:08 -0500782
Sean Christopherson55d23752018-12-03 13:53:18 -0800783 return 0;
784}
785
Krish Sadhukhan61446ba2018-12-12 13:30:09 -0500786static int nested_vmx_check_exit_msr_switch_controls(struct kvm_vcpu *vcpu,
787 struct vmcs12 *vmcs12)
Sean Christopherson55d23752018-12-03 13:53:18 -0800788{
Sean Christopherson5497b952019-07-11 08:58:29 -0700789 if (CC(nested_vmx_check_msr_switch(vcpu,
790 vmcs12->vm_exit_msr_load_count,
791 vmcs12->vm_exit_msr_load_addr)) ||
792 CC(nested_vmx_check_msr_switch(vcpu,
793 vmcs12->vm_exit_msr_store_count,
794 vmcs12->vm_exit_msr_store_addr)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800795 return -EINVAL;
Sean Christophersonf9b245e2018-12-12 13:30:08 -0500796
Sean Christopherson55d23752018-12-03 13:53:18 -0800797 return 0;
798}
799
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -0500800static int nested_vmx_check_entry_msr_switch_controls(struct kvm_vcpu *vcpu,
801 struct vmcs12 *vmcs12)
Krish Sadhukhan61446ba2018-12-12 13:30:09 -0500802{
Sean Christopherson5497b952019-07-11 08:58:29 -0700803 if (CC(nested_vmx_check_msr_switch(vcpu,
804 vmcs12->vm_entry_msr_load_count,
805 vmcs12->vm_entry_msr_load_addr)))
Krish Sadhukhan61446ba2018-12-12 13:30:09 -0500806 return -EINVAL;
807
808 return 0;
809}
810
Sean Christopherson55d23752018-12-03 13:53:18 -0800811static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
812 struct vmcs12 *vmcs12)
813{
814 if (!nested_cpu_has_pml(vmcs12))
815 return 0;
816
Sean Christopherson5497b952019-07-11 08:58:29 -0700817 if (CC(!nested_cpu_has_ept(vmcs12)) ||
818 CC(!page_address_valid(vcpu, vmcs12->pml_address)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800819 return -EINVAL;
820
821 return 0;
822}
823
824static int nested_vmx_check_unrestricted_guest_controls(struct kvm_vcpu *vcpu,
825 struct vmcs12 *vmcs12)
826{
Sean Christopherson5497b952019-07-11 08:58:29 -0700827 if (CC(nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST) &&
828 !nested_cpu_has_ept(vmcs12)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800829 return -EINVAL;
830 return 0;
831}
832
833static int nested_vmx_check_mode_based_ept_exec_controls(struct kvm_vcpu *vcpu,
834 struct vmcs12 *vmcs12)
835{
Sean Christopherson5497b952019-07-11 08:58:29 -0700836 if (CC(nested_cpu_has2(vmcs12, SECONDARY_EXEC_MODE_BASED_EPT_EXEC) &&
837 !nested_cpu_has_ept(vmcs12)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800838 return -EINVAL;
839 return 0;
840}
841
842static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
843 struct vmcs12 *vmcs12)
844{
845 if (!nested_cpu_has_shadow_vmcs(vmcs12))
846 return 0;
847
Sean Christopherson5497b952019-07-11 08:58:29 -0700848 if (CC(!page_address_valid(vcpu, vmcs12->vmread_bitmap)) ||
849 CC(!page_address_valid(vcpu, vmcs12->vmwrite_bitmap)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800850 return -EINVAL;
851
852 return 0;
853}
854
855static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
856 struct vmx_msr_entry *e)
857{
858 /* x2APIC MSR accesses are not allowed */
Sean Christopherson5497b952019-07-11 08:58:29 -0700859 if (CC(vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8))
Sean Christopherson55d23752018-12-03 13:53:18 -0800860 return -EINVAL;
Sean Christopherson5497b952019-07-11 08:58:29 -0700861 if (CC(e->index == MSR_IA32_UCODE_WRITE) || /* SDM Table 35-2 */
862 CC(e->index == MSR_IA32_UCODE_REV))
Sean Christopherson55d23752018-12-03 13:53:18 -0800863 return -EINVAL;
Sean Christopherson5497b952019-07-11 08:58:29 -0700864 if (CC(e->reserved != 0))
Sean Christopherson55d23752018-12-03 13:53:18 -0800865 return -EINVAL;
866 return 0;
867}
868
869static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
870 struct vmx_msr_entry *e)
871{
Sean Christopherson5497b952019-07-11 08:58:29 -0700872 if (CC(e->index == MSR_FS_BASE) ||
873 CC(e->index == MSR_GS_BASE) ||
874 CC(e->index == MSR_IA32_SMM_MONITOR_CTL) || /* SMM is not supported */
Sean Christopherson55d23752018-12-03 13:53:18 -0800875 nested_vmx_msr_check_common(vcpu, e))
876 return -EINVAL;
877 return 0;
878}
879
880static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
881 struct vmx_msr_entry *e)
882{
Sean Christopherson5497b952019-07-11 08:58:29 -0700883 if (CC(e->index == MSR_IA32_SMBASE) || /* SMM is not supported */
Sean Christopherson55d23752018-12-03 13:53:18 -0800884 nested_vmx_msr_check_common(vcpu, e))
885 return -EINVAL;
886 return 0;
887}
888
Marc Orrf0b51052019-09-17 11:50:57 -0700889static u32 nested_vmx_max_atomic_switch_msrs(struct kvm_vcpu *vcpu)
890{
891 struct vcpu_vmx *vmx = to_vmx(vcpu);
892 u64 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
893 vmx->nested.msrs.misc_high);
894
895 return (vmx_misc_max_msr(vmx_misc) + 1) * VMX_MISC_MSR_LIST_MULTIPLIER;
896}
897
Sean Christopherson55d23752018-12-03 13:53:18 -0800898/*
899 * Load guest's/host's msr at nested entry/exit.
900 * return 0 for success, entry index for failure.
Marc Orrf0b51052019-09-17 11:50:57 -0700901 *
902 * One of the failure modes for MSR load/store is when a list exceeds the
903 * virtual hardware's capacity. To maintain compatibility with hardware inasmuch
904 * as possible, process all valid entries before failing rather than precheck
905 * for a capacity violation.
Sean Christopherson55d23752018-12-03 13:53:18 -0800906 */
907static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
908{
909 u32 i;
910 struct vmx_msr_entry e;
Marc Orrf0b51052019-09-17 11:50:57 -0700911 u32 max_msr_list_size = nested_vmx_max_atomic_switch_msrs(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -0800912
Sean Christopherson55d23752018-12-03 13:53:18 -0800913 for (i = 0; i < count; i++) {
Marc Orrf0b51052019-09-17 11:50:57 -0700914 if (unlikely(i >= max_msr_list_size))
915 goto fail;
916
Sean Christopherson55d23752018-12-03 13:53:18 -0800917 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
918 &e, sizeof(e))) {
919 pr_debug_ratelimited(
920 "%s cannot read MSR entry (%u, 0x%08llx)\n",
921 __func__, i, gpa + i * sizeof(e));
922 goto fail;
923 }
924 if (nested_vmx_load_msr_check(vcpu, &e)) {
925 pr_debug_ratelimited(
926 "%s check failed (%u, 0x%x, 0x%x)\n",
927 __func__, i, e.index, e.reserved);
928 goto fail;
929 }
Sean Christophersonf20935d2019-09-05 14:22:54 -0700930 if (kvm_set_msr(vcpu, e.index, e.value)) {
Sean Christopherson55d23752018-12-03 13:53:18 -0800931 pr_debug_ratelimited(
932 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
933 __func__, i, e.index, e.value);
934 goto fail;
935 }
936 }
937 return 0;
938fail:
Sean Christopherson68cda402020-05-11 15:05:29 -0700939 /* Note, max_msr_list_size is at most 4096, i.e. this can't wrap. */
Sean Christopherson55d23752018-12-03 13:53:18 -0800940 return i + 1;
941}
942
Aaron Lewis662f1d12019-11-07 21:14:39 -0800943static bool nested_vmx_get_vmexit_msr_value(struct kvm_vcpu *vcpu,
944 u32 msr_index,
945 u64 *data)
946{
947 struct vcpu_vmx *vmx = to_vmx(vcpu);
948
949 /*
950 * If the L0 hypervisor stored a more accurate value for the TSC that
951 * does not include the time taken for emulation of the L2->L1
952 * VM-exit in L0, use the more accurate value.
953 */
954 if (msr_index == MSR_IA32_TSC) {
Sean Christophersona128a932020-09-23 11:03:57 -0700955 int i = vmx_find_loadstore_msr_slot(&vmx->msr_autostore.guest,
956 MSR_IA32_TSC);
Aaron Lewis662f1d12019-11-07 21:14:39 -0800957
Sean Christophersona128a932020-09-23 11:03:57 -0700958 if (i >= 0) {
959 u64 val = vmx->msr_autostore.guest.val[i].value;
Aaron Lewis662f1d12019-11-07 21:14:39 -0800960
961 *data = kvm_read_l1_tsc(vcpu, val);
962 return true;
963 }
964 }
965
966 if (kvm_get_msr(vcpu, msr_index, data)) {
967 pr_debug_ratelimited("%s cannot read MSR (0x%x)\n", __func__,
968 msr_index);
969 return false;
970 }
971 return true;
972}
973
Aaron Lewis365d3d52019-11-07 21:14:36 -0800974static bool read_and_check_msr_entry(struct kvm_vcpu *vcpu, u64 gpa, int i,
975 struct vmx_msr_entry *e)
976{
977 if (kvm_vcpu_read_guest(vcpu,
978 gpa + i * sizeof(*e),
979 e, 2 * sizeof(u32))) {
980 pr_debug_ratelimited(
981 "%s cannot read MSR entry (%u, 0x%08llx)\n",
982 __func__, i, gpa + i * sizeof(*e));
983 return false;
984 }
985 if (nested_vmx_store_msr_check(vcpu, e)) {
986 pr_debug_ratelimited(
987 "%s check failed (%u, 0x%x, 0x%x)\n",
988 __func__, i, e->index, e->reserved);
989 return false;
990 }
991 return true;
992}
993
Sean Christopherson55d23752018-12-03 13:53:18 -0800994static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
995{
Sean Christophersonf20935d2019-09-05 14:22:54 -0700996 u64 data;
Sean Christopherson55d23752018-12-03 13:53:18 -0800997 u32 i;
998 struct vmx_msr_entry e;
Marc Orrf0b51052019-09-17 11:50:57 -0700999 u32 max_msr_list_size = nested_vmx_max_atomic_switch_msrs(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08001000
1001 for (i = 0; i < count; i++) {
Marc Orrf0b51052019-09-17 11:50:57 -07001002 if (unlikely(i >= max_msr_list_size))
1003 return -EINVAL;
1004
Aaron Lewis365d3d52019-11-07 21:14:36 -08001005 if (!read_and_check_msr_entry(vcpu, gpa, i, &e))
Sean Christopherson55d23752018-12-03 13:53:18 -08001006 return -EINVAL;
Aaron Lewis365d3d52019-11-07 21:14:36 -08001007
Aaron Lewis662f1d12019-11-07 21:14:39 -08001008 if (!nested_vmx_get_vmexit_msr_value(vcpu, e.index, &data))
Sean Christopherson55d23752018-12-03 13:53:18 -08001009 return -EINVAL;
Aaron Lewis662f1d12019-11-07 21:14:39 -08001010
Sean Christopherson55d23752018-12-03 13:53:18 -08001011 if (kvm_vcpu_write_guest(vcpu,
1012 gpa + i * sizeof(e) +
1013 offsetof(struct vmx_msr_entry, value),
Sean Christophersonf20935d2019-09-05 14:22:54 -07001014 &data, sizeof(data))) {
Sean Christopherson55d23752018-12-03 13:53:18 -08001015 pr_debug_ratelimited(
1016 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Sean Christophersonf20935d2019-09-05 14:22:54 -07001017 __func__, i, e.index, data);
Sean Christopherson55d23752018-12-03 13:53:18 -08001018 return -EINVAL;
1019 }
1020 }
1021 return 0;
1022}
1023
Aaron Lewis662f1d12019-11-07 21:14:39 -08001024static bool nested_msr_store_list_has_msr(struct kvm_vcpu *vcpu, u32 msr_index)
1025{
1026 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1027 u32 count = vmcs12->vm_exit_msr_store_count;
1028 u64 gpa = vmcs12->vm_exit_msr_store_addr;
1029 struct vmx_msr_entry e;
1030 u32 i;
1031
1032 for (i = 0; i < count; i++) {
1033 if (!read_and_check_msr_entry(vcpu, gpa, i, &e))
1034 return false;
1035
1036 if (e.index == msr_index)
1037 return true;
1038 }
1039 return false;
1040}
1041
1042static void prepare_vmx_msr_autostore_list(struct kvm_vcpu *vcpu,
1043 u32 msr_index)
1044{
1045 struct vcpu_vmx *vmx = to_vmx(vcpu);
1046 struct vmx_msrs *autostore = &vmx->msr_autostore.guest;
1047 bool in_vmcs12_store_list;
Sean Christophersona128a932020-09-23 11:03:57 -07001048 int msr_autostore_slot;
Aaron Lewis662f1d12019-11-07 21:14:39 -08001049 bool in_autostore_list;
1050 int last;
1051
Sean Christophersona128a932020-09-23 11:03:57 -07001052 msr_autostore_slot = vmx_find_loadstore_msr_slot(autostore, msr_index);
1053 in_autostore_list = msr_autostore_slot >= 0;
Aaron Lewis662f1d12019-11-07 21:14:39 -08001054 in_vmcs12_store_list = nested_msr_store_list_has_msr(vcpu, msr_index);
1055
1056 if (in_vmcs12_store_list && !in_autostore_list) {
Sean Christophersonce833b22020-09-23 11:03:56 -07001057 if (autostore->nr == MAX_NR_LOADSTORE_MSRS) {
Aaron Lewis662f1d12019-11-07 21:14:39 -08001058 /*
1059 * Emulated VMEntry does not fail here. Instead a less
1060 * accurate value will be returned by
1061 * nested_vmx_get_vmexit_msr_value() using kvm_get_msr()
1062 * instead of reading the value from the vmcs02 VMExit
1063 * MSR-store area.
1064 */
1065 pr_warn_ratelimited(
1066 "Not enough msr entries in msr_autostore. Can't add msr %x\n",
1067 msr_index);
1068 return;
1069 }
1070 last = autostore->nr++;
1071 autostore->val[last].index = msr_index;
1072 } else if (!in_vmcs12_store_list && in_autostore_list) {
1073 last = --autostore->nr;
Sean Christophersona128a932020-09-23 11:03:57 -07001074 autostore->val[msr_autostore_slot] = autostore->val[last];
Aaron Lewis662f1d12019-11-07 21:14:39 -08001075 }
1076}
1077
Sean Christopherson55d23752018-12-03 13:53:18 -08001078/*
Sean Christophersonea79a752020-02-04 07:32:59 -08001079 * Load guest's/host's cr3 at nested entry/exit. @nested_ept is true if we are
1080 * emulating VM-Entry into a guest with EPT enabled. On failure, the expected
1081 * Exit Qualification (for a VM-Entry consistency check VM-Exit) is assigned to
1082 * @entry_failure_code.
Sean Christopherson55d23752018-12-03 13:53:18 -08001083 */
Maxim Levitsky0f857222021-06-07 12:02:00 +03001084static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,
1085 bool nested_ept, bool reload_pdptrs,
Sean Christopherson68cda402020-05-11 15:05:29 -07001086 enum vm_entry_failure_code *entry_failure_code)
Sean Christopherson55d23752018-12-03 13:53:18 -08001087{
Sean Christopherson636e8b72021-02-03 16:01:10 -08001088 if (CC(kvm_vcpu_is_illegal_gpa(vcpu, cr3))) {
Sean Christopherson0cc69202020-05-01 21:32:26 -07001089 *entry_failure_code = ENTRY_FAIL_DEFAULT;
1090 return -EINVAL;
1091 }
Sean Christopherson55d23752018-12-03 13:53:18 -08001092
Sean Christopherson0cc69202020-05-01 21:32:26 -07001093 /*
1094 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
1095 * must not be dereferenced.
1096 */
Maxim Levitsky0f857222021-06-07 12:02:00 +03001097 if (reload_pdptrs && !nested_ept && is_pae_paging(vcpu) &&
Sean Christophersonbcb72d02021-06-07 12:01:56 +03001098 CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))) {
1099 *entry_failure_code = ENTRY_FAIL_PDPTE;
1100 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08001101 }
1102
Sean Christopherson50a41792021-06-09 16:42:28 -07001103 if (!nested_ept)
Sean Christophersonb5129102021-06-09 16:42:27 -07001104 kvm_mmu_new_pgd(vcpu, cr3);
Sean Christopherson07ffaf32021-06-09 16:42:21 -07001105
Sean Christopherson55d23752018-12-03 13:53:18 -08001106 vcpu->arch.cr3 = cr3;
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07001107 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Sean Christopherson55d23752018-12-03 13:53:18 -08001108
Sean Christopherson616007c2021-06-22 10:57:34 -07001109 /* Re-initialize the MMU, e.g. to pick up CR4 MMU role changes. */
Sean Christophersonc9060662021-06-09 16:42:33 -07001110 kvm_init_mmu(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08001111
1112 return 0;
1113}
1114
1115/*
1116 * Returns if KVM is able to config CPU to tag TLB entries
1117 * populated by L2 differently than TLB entries populated
1118 * by L1.
1119 *
Liran Alon992edea2019-11-20 14:24:52 +02001120 * If L0 uses EPT, L1 and L2 run with different EPTP because
1121 * guest_mode is part of kvm_mmu_page_role. Thus, TLB entries
1122 * are tagged with different EPTP.
Sean Christopherson55d23752018-12-03 13:53:18 -08001123 *
1124 * If L1 uses VPID and we allocated a vpid02, TLB entries are tagged
1125 * with different VPID (L1 entries are tagged with vmx->vpid
1126 * while L2 entries are tagged with vmx->nested.vpid02).
1127 */
1128static bool nested_has_guest_tlb_tag(struct kvm_vcpu *vcpu)
1129{
1130 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1131
Liran Alon992edea2019-11-20 14:24:52 +02001132 return enable_ept ||
Sean Christopherson55d23752018-12-03 13:53:18 -08001133 (nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02);
1134}
1135
Sean Christopherson50b265a2020-03-20 14:28:19 -07001136static void nested_vmx_transition_tlb_flush(struct kvm_vcpu *vcpu,
1137 struct vmcs12 *vmcs12,
1138 bool is_vmenter)
1139{
1140 struct vcpu_vmx *vmx = to_vmx(vcpu);
1141
1142 /*
Sean Christopherson50a41792021-06-09 16:42:28 -07001143 * If vmcs12 doesn't use VPID, L1 expects linear and combined mappings
1144 * for *all* contexts to be flushed on VM-Enter/VM-Exit, i.e. it's a
1145 * full TLB flush from the guest's perspective. This is required even
1146 * if VPID is disabled in the host as KVM may need to synchronize the
1147 * MMU in response to the guest TLB flush.
1148 *
1149 * Note, using TLB_FLUSH_GUEST is correct even if nested EPT is in use.
1150 * EPT is a special snowflake, as guest-physical mappings aren't
1151 * flushed on VPID invalidations, including VM-Enter or VM-Exit with
1152 * VPID disabled. As a result, KVM _never_ needs to sync nEPT
1153 * entries on VM-Enter because L1 can't rely on VM-Enter to flush
1154 * those mappings.
Sean Christopherson50b265a2020-03-20 14:28:19 -07001155 */
Sean Christopherson50a41792021-06-09 16:42:28 -07001156 if (!nested_cpu_has_vpid(vmcs12)) {
1157 kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu);
Sean Christopherson50b265a2020-03-20 14:28:19 -07001158 return;
Sean Christopherson50a41792021-06-09 16:42:28 -07001159 }
1160
1161 /* L2 should never have a VPID if VPID is disabled. */
1162 WARN_ON(!enable_vpid);
Sean Christopherson50b265a2020-03-20 14:28:19 -07001163
1164 /*
Sean Christopherson50b265a2020-03-20 14:28:19 -07001165 * If VPID is enabled and used by vmc12, but L2 does not have a unique
1166 * TLB tag (ASID), i.e. EPT is disabled and KVM was unable to allocate
Sean Christophersonc51e1ff2020-03-20 14:28:22 -07001167 * a VPID for L2, flush the current context as the effective ASID is
1168 * common to both L1 and L2.
Sean Christopherson50b265a2020-03-20 14:28:19 -07001169 *
1170 * Defer the flush so that it runs after vmcs02.EPTP has been set by
1171 * KVM_REQ_LOAD_MMU_PGD (if nested EPT is enabled) and to avoid
1172 * redundant flushes further down the nested pipeline.
1173 *
1174 * If a TLB flush isn't required due to any of the above, and vpid12 is
1175 * changing then the new "virtual" VPID (vpid12) will reuse the same
Sean Christopherson50a41792021-06-09 16:42:28 -07001176 * "real" VPID (vpid02), and so needs to be flushed. There's no direct
Sean Christopherson50b265a2020-03-20 14:28:19 -07001177 * mapping between vpid02 and vpid12, vpid02 is per-vCPU and reused for
Sean Christopherson50a41792021-06-09 16:42:28 -07001178 * all nested vCPUs. Remember, a flush on VM-Enter does not invalidate
1179 * guest-physical mappings, so there is no need to sync the nEPT MMU.
Sean Christopherson50b265a2020-03-20 14:28:19 -07001180 */
Sean Christopherson50a41792021-06-09 16:42:28 -07001181 if (!nested_has_guest_tlb_tag(vcpu)) {
Sean Christophersonc51e1ff2020-03-20 14:28:22 -07001182 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
Sean Christopherson50b265a2020-03-20 14:28:19 -07001183 } else if (is_vmenter &&
1184 vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
1185 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
1186 vpid_sync_context(nested_get_vpid02(vcpu));
1187 }
1188}
1189
Sean Christopherson55d23752018-12-03 13:53:18 -08001190static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
1191{
1192 superset &= mask;
1193 subset &= mask;
1194
1195 return (superset | subset) == superset;
1196}
1197
1198static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
1199{
1200 const u64 feature_and_reserved =
1201 /* feature (except bit 48; see below) */
1202 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
1203 /* reserved */
1204 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
1205 u64 vmx_basic = vmx->nested.msrs.basic;
1206
1207 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
1208 return -EINVAL;
1209
1210 /*
1211 * KVM does not emulate a version of VMX that constrains physical
1212 * addresses of VMX structures (e.g. VMCS) to 32-bits.
1213 */
1214 if (data & BIT_ULL(48))
1215 return -EINVAL;
1216
1217 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
1218 vmx_basic_vmcs_revision_id(data))
1219 return -EINVAL;
1220
1221 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
1222 return -EINVAL;
1223
1224 vmx->nested.msrs.basic = data;
1225 return 0;
1226}
1227
1228static int
1229vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
1230{
1231 u64 supported;
1232 u32 *lowp, *highp;
1233
1234 switch (msr_index) {
1235 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
1236 lowp = &vmx->nested.msrs.pinbased_ctls_low;
1237 highp = &vmx->nested.msrs.pinbased_ctls_high;
1238 break;
1239 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
1240 lowp = &vmx->nested.msrs.procbased_ctls_low;
1241 highp = &vmx->nested.msrs.procbased_ctls_high;
1242 break;
1243 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
1244 lowp = &vmx->nested.msrs.exit_ctls_low;
1245 highp = &vmx->nested.msrs.exit_ctls_high;
1246 break;
1247 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
1248 lowp = &vmx->nested.msrs.entry_ctls_low;
1249 highp = &vmx->nested.msrs.entry_ctls_high;
1250 break;
1251 case MSR_IA32_VMX_PROCBASED_CTLS2:
1252 lowp = &vmx->nested.msrs.secondary_ctls_low;
1253 highp = &vmx->nested.msrs.secondary_ctls_high;
1254 break;
1255 default:
1256 BUG();
1257 }
1258
1259 supported = vmx_control_msr(*lowp, *highp);
1260
1261 /* Check must-be-1 bits are still 1. */
1262 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
1263 return -EINVAL;
1264
1265 /* Check must-be-0 bits are still 0. */
1266 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
1267 return -EINVAL;
1268
1269 *lowp = data;
1270 *highp = data >> 32;
1271 return 0;
1272}
1273
1274static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
1275{
1276 const u64 feature_and_reserved_bits =
1277 /* feature */
1278 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
1279 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
1280 /* reserved */
1281 GENMASK_ULL(13, 9) | BIT_ULL(31);
1282 u64 vmx_misc;
1283
1284 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
1285 vmx->nested.msrs.misc_high);
1286
1287 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
1288 return -EINVAL;
1289
1290 if ((vmx->nested.msrs.pinbased_ctls_high &
1291 PIN_BASED_VMX_PREEMPTION_TIMER) &&
1292 vmx_misc_preemption_timer_rate(data) !=
1293 vmx_misc_preemption_timer_rate(vmx_misc))
1294 return -EINVAL;
1295
1296 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
1297 return -EINVAL;
1298
1299 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
1300 return -EINVAL;
1301
1302 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
1303 return -EINVAL;
1304
1305 vmx->nested.msrs.misc_low = data;
1306 vmx->nested.msrs.misc_high = data >> 32;
1307
Sean Christopherson55d23752018-12-03 13:53:18 -08001308 return 0;
1309}
1310
1311static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
1312{
1313 u64 vmx_ept_vpid_cap;
1314
1315 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
1316 vmx->nested.msrs.vpid_caps);
1317
1318 /* Every bit is either reserved or a feature bit. */
1319 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
1320 return -EINVAL;
1321
1322 vmx->nested.msrs.ept_caps = data;
1323 vmx->nested.msrs.vpid_caps = data >> 32;
1324 return 0;
1325}
1326
1327static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
1328{
1329 u64 *msr;
1330
1331 switch (msr_index) {
1332 case MSR_IA32_VMX_CR0_FIXED0:
1333 msr = &vmx->nested.msrs.cr0_fixed0;
1334 break;
1335 case MSR_IA32_VMX_CR4_FIXED0:
1336 msr = &vmx->nested.msrs.cr4_fixed0;
1337 break;
1338 default:
1339 BUG();
1340 }
1341
1342 /*
1343 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
1344 * must be 1 in the restored value.
1345 */
1346 if (!is_bitwise_subset(data, *msr, -1ULL))
1347 return -EINVAL;
1348
1349 *msr = data;
1350 return 0;
1351}
1352
1353/*
1354 * Called when userspace is restoring VMX MSRs.
1355 *
1356 * Returns 0 on success, non-0 otherwise.
1357 */
1358int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1359{
1360 struct vcpu_vmx *vmx = to_vmx(vcpu);
1361
1362 /*
1363 * Don't allow changes to the VMX capability MSRs while the vCPU
1364 * is in VMX operation.
1365 */
1366 if (vmx->nested.vmxon)
1367 return -EBUSY;
1368
1369 switch (msr_index) {
1370 case MSR_IA32_VMX_BASIC:
1371 return vmx_restore_vmx_basic(vmx, data);
1372 case MSR_IA32_VMX_PINBASED_CTLS:
1373 case MSR_IA32_VMX_PROCBASED_CTLS:
1374 case MSR_IA32_VMX_EXIT_CTLS:
1375 case MSR_IA32_VMX_ENTRY_CTLS:
1376 /*
1377 * The "non-true" VMX capability MSRs are generated from the
1378 * "true" MSRs, so we do not support restoring them directly.
1379 *
1380 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
1381 * should restore the "true" MSRs with the must-be-1 bits
1382 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
1383 * DEFAULT SETTINGS".
1384 */
1385 return -EINVAL;
1386 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
1387 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
1388 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
1389 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
1390 case MSR_IA32_VMX_PROCBASED_CTLS2:
1391 return vmx_restore_control_msr(vmx, msr_index, data);
1392 case MSR_IA32_VMX_MISC:
1393 return vmx_restore_vmx_misc(vmx, data);
1394 case MSR_IA32_VMX_CR0_FIXED0:
1395 case MSR_IA32_VMX_CR4_FIXED0:
1396 return vmx_restore_fixed0_msr(vmx, msr_index, data);
1397 case MSR_IA32_VMX_CR0_FIXED1:
1398 case MSR_IA32_VMX_CR4_FIXED1:
1399 /*
1400 * These MSRs are generated based on the vCPU's CPUID, so we
1401 * do not support restoring them directly.
1402 */
1403 return -EINVAL;
1404 case MSR_IA32_VMX_EPT_VPID_CAP:
1405 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
1406 case MSR_IA32_VMX_VMCS_ENUM:
1407 vmx->nested.msrs.vmcs_enum = data;
1408 return 0;
Paolo Bonzinie8a70bd2019-07-02 14:40:40 +02001409 case MSR_IA32_VMX_VMFUNC:
1410 if (data & ~vmx->nested.msrs.vmfunc_controls)
1411 return -EINVAL;
1412 vmx->nested.msrs.vmfunc_controls = data;
1413 return 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08001414 default:
1415 /*
1416 * The rest of the VMX capability MSRs do not support restore.
1417 */
1418 return -EINVAL;
1419 }
1420}
1421
1422/* Returns 0 on success, non-0 otherwise. */
1423int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
1424{
1425 switch (msr_index) {
1426 case MSR_IA32_VMX_BASIC:
1427 *pdata = msrs->basic;
1428 break;
1429 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
1430 case MSR_IA32_VMX_PINBASED_CTLS:
1431 *pdata = vmx_control_msr(
1432 msrs->pinbased_ctls_low,
1433 msrs->pinbased_ctls_high);
1434 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
1435 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
1436 break;
1437 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
1438 case MSR_IA32_VMX_PROCBASED_CTLS:
1439 *pdata = vmx_control_msr(
1440 msrs->procbased_ctls_low,
1441 msrs->procbased_ctls_high);
1442 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
1443 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
1444 break;
1445 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
1446 case MSR_IA32_VMX_EXIT_CTLS:
1447 *pdata = vmx_control_msr(
1448 msrs->exit_ctls_low,
1449 msrs->exit_ctls_high);
1450 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
1451 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
1452 break;
1453 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
1454 case MSR_IA32_VMX_ENTRY_CTLS:
1455 *pdata = vmx_control_msr(
1456 msrs->entry_ctls_low,
1457 msrs->entry_ctls_high);
1458 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
1459 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
1460 break;
1461 case MSR_IA32_VMX_MISC:
1462 *pdata = vmx_control_msr(
1463 msrs->misc_low,
1464 msrs->misc_high);
1465 break;
1466 case MSR_IA32_VMX_CR0_FIXED0:
1467 *pdata = msrs->cr0_fixed0;
1468 break;
1469 case MSR_IA32_VMX_CR0_FIXED1:
1470 *pdata = msrs->cr0_fixed1;
1471 break;
1472 case MSR_IA32_VMX_CR4_FIXED0:
1473 *pdata = msrs->cr4_fixed0;
1474 break;
1475 case MSR_IA32_VMX_CR4_FIXED1:
1476 *pdata = msrs->cr4_fixed1;
1477 break;
1478 case MSR_IA32_VMX_VMCS_ENUM:
1479 *pdata = msrs->vmcs_enum;
1480 break;
1481 case MSR_IA32_VMX_PROCBASED_CTLS2:
1482 *pdata = vmx_control_msr(
1483 msrs->secondary_ctls_low,
1484 msrs->secondary_ctls_high);
1485 break;
1486 case MSR_IA32_VMX_EPT_VPID_CAP:
1487 *pdata = msrs->ept_caps |
1488 ((u64)msrs->vpid_caps << 32);
1489 break;
1490 case MSR_IA32_VMX_VMFUNC:
1491 *pdata = msrs->vmfunc_controls;
1492 break;
1493 default:
1494 return 1;
1495 }
1496
1497 return 0;
1498}
1499
1500/*
Sean Christophersonfadcead2019-05-07 08:36:23 -07001501 * Copy the writable VMCS shadow fields back to the VMCS12, in case they have
1502 * been modified by the L1 guest. Note, "writable" in this context means
1503 * "writable by the guest", i.e. tagged SHADOW_FIELD_RW; the set of
1504 * fields tagged SHADOW_FIELD_RO may or may not align with the "read-only"
1505 * VM-exit information fields (which are actually writable if the vCPU is
1506 * configured to support "VMWRITE to any supported field in the VMCS").
Sean Christopherson55d23752018-12-03 13:53:18 -08001507 */
1508static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
1509{
Sean Christopherson55d23752018-12-03 13:53:18 -08001510 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Sean Christophersonfadcead2019-05-07 08:36:23 -07001511 struct vmcs12 *vmcs12 = get_vmcs12(&vmx->vcpu);
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07001512 struct shadow_vmcs_field field;
1513 unsigned long val;
Sean Christophersonfadcead2019-05-07 08:36:23 -07001514 int i;
Sean Christopherson55d23752018-12-03 13:53:18 -08001515
Paolo Bonzini88dddc12019-07-19 18:41:10 +02001516 if (WARN_ON(!shadow_vmcs))
1517 return;
1518
Sean Christopherson55d23752018-12-03 13:53:18 -08001519 preempt_disable();
1520
1521 vmcs_load(shadow_vmcs);
1522
Sean Christophersonfadcead2019-05-07 08:36:23 -07001523 for (i = 0; i < max_shadow_read_write_fields; i++) {
1524 field = shadow_read_write_fields[i];
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07001525 val = __vmcs_readl(field.encoding);
1526 vmcs12_write_any(vmcs12, field.encoding, field.offset, val);
Sean Christopherson55d23752018-12-03 13:53:18 -08001527 }
1528
1529 vmcs_clear(shadow_vmcs);
1530 vmcs_load(vmx->loaded_vmcs->vmcs);
1531
1532 preempt_enable();
1533}
1534
1535static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
1536{
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07001537 const struct shadow_vmcs_field *fields[] = {
Sean Christopherson55d23752018-12-03 13:53:18 -08001538 shadow_read_write_fields,
1539 shadow_read_only_fields
1540 };
1541 const int max_fields[] = {
1542 max_shadow_read_write_fields,
1543 max_shadow_read_only_fields
1544 };
Sean Christopherson55d23752018-12-03 13:53:18 -08001545 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07001546 struct vmcs12 *vmcs12 = get_vmcs12(&vmx->vcpu);
1547 struct shadow_vmcs_field field;
1548 unsigned long val;
1549 int i, q;
Sean Christopherson55d23752018-12-03 13:53:18 -08001550
Paolo Bonzini88dddc12019-07-19 18:41:10 +02001551 if (WARN_ON(!shadow_vmcs))
1552 return;
1553
Sean Christopherson55d23752018-12-03 13:53:18 -08001554 vmcs_load(shadow_vmcs);
1555
1556 for (q = 0; q < ARRAY_SIZE(fields); q++) {
1557 for (i = 0; i < max_fields[q]; i++) {
1558 field = fields[q][i];
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07001559 val = vmcs12_read_any(vmcs12, field.encoding,
1560 field.offset);
1561 __vmcs_writel(field.encoding, val);
Sean Christopherson55d23752018-12-03 13:53:18 -08001562 }
1563 }
1564
1565 vmcs_clear(shadow_vmcs);
1566 vmcs_load(vmx->loaded_vmcs->vmcs);
1567}
1568
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001569static void copy_enlightened_to_vmcs12(struct vcpu_vmx *vmx, u32 hv_clean_fields)
Sean Christopherson55d23752018-12-03 13:53:18 -08001570{
1571 struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12;
1572 struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
1573
1574 /* HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE */
1575 vmcs12->tpr_threshold = evmcs->tpr_threshold;
1576 vmcs12->guest_rip = evmcs->guest_rip;
1577
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001578 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001579 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC))) {
1580 vmcs12->guest_rsp = evmcs->guest_rsp;
1581 vmcs12->guest_rflags = evmcs->guest_rflags;
1582 vmcs12->guest_interruptibility_info =
1583 evmcs->guest_interruptibility_info;
1584 }
1585
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001586 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001587 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC))) {
1588 vmcs12->cpu_based_vm_exec_control =
1589 evmcs->cpu_based_vm_exec_control;
1590 }
1591
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001592 if (unlikely(!(hv_clean_fields &
Vitaly Kuznetsovf9bc5222019-06-13 13:35:02 +02001593 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EXCPN))) {
Sean Christopherson55d23752018-12-03 13:53:18 -08001594 vmcs12->exception_bitmap = evmcs->exception_bitmap;
1595 }
1596
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001597 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001598 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_ENTRY))) {
1599 vmcs12->vm_entry_controls = evmcs->vm_entry_controls;
1600 }
1601
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001602 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001603 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT))) {
1604 vmcs12->vm_entry_intr_info_field =
1605 evmcs->vm_entry_intr_info_field;
1606 vmcs12->vm_entry_exception_error_code =
1607 evmcs->vm_entry_exception_error_code;
1608 vmcs12->vm_entry_instruction_len =
1609 evmcs->vm_entry_instruction_len;
1610 }
1611
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001612 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001613 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1))) {
1614 vmcs12->host_ia32_pat = evmcs->host_ia32_pat;
1615 vmcs12->host_ia32_efer = evmcs->host_ia32_efer;
1616 vmcs12->host_cr0 = evmcs->host_cr0;
1617 vmcs12->host_cr3 = evmcs->host_cr3;
1618 vmcs12->host_cr4 = evmcs->host_cr4;
1619 vmcs12->host_ia32_sysenter_esp = evmcs->host_ia32_sysenter_esp;
1620 vmcs12->host_ia32_sysenter_eip = evmcs->host_ia32_sysenter_eip;
1621 vmcs12->host_rip = evmcs->host_rip;
1622 vmcs12->host_ia32_sysenter_cs = evmcs->host_ia32_sysenter_cs;
1623 vmcs12->host_es_selector = evmcs->host_es_selector;
1624 vmcs12->host_cs_selector = evmcs->host_cs_selector;
1625 vmcs12->host_ss_selector = evmcs->host_ss_selector;
1626 vmcs12->host_ds_selector = evmcs->host_ds_selector;
1627 vmcs12->host_fs_selector = evmcs->host_fs_selector;
1628 vmcs12->host_gs_selector = evmcs->host_gs_selector;
1629 vmcs12->host_tr_selector = evmcs->host_tr_selector;
1630 }
1631
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001632 if (unlikely(!(hv_clean_fields &
Vitaly Kuznetsovf9bc5222019-06-13 13:35:02 +02001633 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1))) {
Sean Christopherson55d23752018-12-03 13:53:18 -08001634 vmcs12->pin_based_vm_exec_control =
1635 evmcs->pin_based_vm_exec_control;
1636 vmcs12->vm_exit_controls = evmcs->vm_exit_controls;
1637 vmcs12->secondary_vm_exec_control =
1638 evmcs->secondary_vm_exec_control;
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_IO_BITMAP))) {
1643 vmcs12->io_bitmap_a = evmcs->io_bitmap_a;
1644 vmcs12->io_bitmap_b = evmcs->io_bitmap_b;
1645 }
1646
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001647 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001648 HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP))) {
1649 vmcs12->msr_bitmap = evmcs->msr_bitmap;
1650 }
1651
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001652 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001653 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2))) {
1654 vmcs12->guest_es_base = evmcs->guest_es_base;
1655 vmcs12->guest_cs_base = evmcs->guest_cs_base;
1656 vmcs12->guest_ss_base = evmcs->guest_ss_base;
1657 vmcs12->guest_ds_base = evmcs->guest_ds_base;
1658 vmcs12->guest_fs_base = evmcs->guest_fs_base;
1659 vmcs12->guest_gs_base = evmcs->guest_gs_base;
1660 vmcs12->guest_ldtr_base = evmcs->guest_ldtr_base;
1661 vmcs12->guest_tr_base = evmcs->guest_tr_base;
1662 vmcs12->guest_gdtr_base = evmcs->guest_gdtr_base;
1663 vmcs12->guest_idtr_base = evmcs->guest_idtr_base;
1664 vmcs12->guest_es_limit = evmcs->guest_es_limit;
1665 vmcs12->guest_cs_limit = evmcs->guest_cs_limit;
1666 vmcs12->guest_ss_limit = evmcs->guest_ss_limit;
1667 vmcs12->guest_ds_limit = evmcs->guest_ds_limit;
1668 vmcs12->guest_fs_limit = evmcs->guest_fs_limit;
1669 vmcs12->guest_gs_limit = evmcs->guest_gs_limit;
1670 vmcs12->guest_ldtr_limit = evmcs->guest_ldtr_limit;
1671 vmcs12->guest_tr_limit = evmcs->guest_tr_limit;
1672 vmcs12->guest_gdtr_limit = evmcs->guest_gdtr_limit;
1673 vmcs12->guest_idtr_limit = evmcs->guest_idtr_limit;
1674 vmcs12->guest_es_ar_bytes = evmcs->guest_es_ar_bytes;
1675 vmcs12->guest_cs_ar_bytes = evmcs->guest_cs_ar_bytes;
1676 vmcs12->guest_ss_ar_bytes = evmcs->guest_ss_ar_bytes;
1677 vmcs12->guest_ds_ar_bytes = evmcs->guest_ds_ar_bytes;
1678 vmcs12->guest_fs_ar_bytes = evmcs->guest_fs_ar_bytes;
1679 vmcs12->guest_gs_ar_bytes = evmcs->guest_gs_ar_bytes;
1680 vmcs12->guest_ldtr_ar_bytes = evmcs->guest_ldtr_ar_bytes;
1681 vmcs12->guest_tr_ar_bytes = evmcs->guest_tr_ar_bytes;
1682 vmcs12->guest_es_selector = evmcs->guest_es_selector;
1683 vmcs12->guest_cs_selector = evmcs->guest_cs_selector;
1684 vmcs12->guest_ss_selector = evmcs->guest_ss_selector;
1685 vmcs12->guest_ds_selector = evmcs->guest_ds_selector;
1686 vmcs12->guest_fs_selector = evmcs->guest_fs_selector;
1687 vmcs12->guest_gs_selector = evmcs->guest_gs_selector;
1688 vmcs12->guest_ldtr_selector = evmcs->guest_ldtr_selector;
1689 vmcs12->guest_tr_selector = evmcs->guest_tr_selector;
1690 }
1691
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001692 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001693 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2))) {
1694 vmcs12->tsc_offset = evmcs->tsc_offset;
1695 vmcs12->virtual_apic_page_addr = evmcs->virtual_apic_page_addr;
1696 vmcs12->xss_exit_bitmap = evmcs->xss_exit_bitmap;
1697 }
1698
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001699 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001700 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR))) {
1701 vmcs12->cr0_guest_host_mask = evmcs->cr0_guest_host_mask;
1702 vmcs12->cr4_guest_host_mask = evmcs->cr4_guest_host_mask;
1703 vmcs12->cr0_read_shadow = evmcs->cr0_read_shadow;
1704 vmcs12->cr4_read_shadow = evmcs->cr4_read_shadow;
1705 vmcs12->guest_cr0 = evmcs->guest_cr0;
1706 vmcs12->guest_cr3 = evmcs->guest_cr3;
1707 vmcs12->guest_cr4 = evmcs->guest_cr4;
1708 vmcs12->guest_dr7 = evmcs->guest_dr7;
1709 }
1710
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001711 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001712 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER))) {
1713 vmcs12->host_fs_base = evmcs->host_fs_base;
1714 vmcs12->host_gs_base = evmcs->host_gs_base;
1715 vmcs12->host_tr_base = evmcs->host_tr_base;
1716 vmcs12->host_gdtr_base = evmcs->host_gdtr_base;
1717 vmcs12->host_idtr_base = evmcs->host_idtr_base;
1718 vmcs12->host_rsp = evmcs->host_rsp;
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_CONTROL_XLAT))) {
1723 vmcs12->ept_pointer = evmcs->ept_pointer;
1724 vmcs12->virtual_processor_id = evmcs->virtual_processor_id;
1725 }
1726
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001727 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001728 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1))) {
1729 vmcs12->vmcs_link_pointer = evmcs->vmcs_link_pointer;
1730 vmcs12->guest_ia32_debugctl = evmcs->guest_ia32_debugctl;
1731 vmcs12->guest_ia32_pat = evmcs->guest_ia32_pat;
1732 vmcs12->guest_ia32_efer = evmcs->guest_ia32_efer;
1733 vmcs12->guest_pdptr0 = evmcs->guest_pdptr0;
1734 vmcs12->guest_pdptr1 = evmcs->guest_pdptr1;
1735 vmcs12->guest_pdptr2 = evmcs->guest_pdptr2;
1736 vmcs12->guest_pdptr3 = evmcs->guest_pdptr3;
1737 vmcs12->guest_pending_dbg_exceptions =
1738 evmcs->guest_pending_dbg_exceptions;
1739 vmcs12->guest_sysenter_esp = evmcs->guest_sysenter_esp;
1740 vmcs12->guest_sysenter_eip = evmcs->guest_sysenter_eip;
1741 vmcs12->guest_bndcfgs = evmcs->guest_bndcfgs;
1742 vmcs12->guest_activity_state = evmcs->guest_activity_state;
1743 vmcs12->guest_sysenter_cs = evmcs->guest_sysenter_cs;
1744 }
1745
1746 /*
1747 * Not used?
1748 * vmcs12->vm_exit_msr_store_addr = evmcs->vm_exit_msr_store_addr;
1749 * vmcs12->vm_exit_msr_load_addr = evmcs->vm_exit_msr_load_addr;
1750 * vmcs12->vm_entry_msr_load_addr = evmcs->vm_entry_msr_load_addr;
Sean Christopherson55d23752018-12-03 13:53:18 -08001751 * vmcs12->page_fault_error_code_mask =
1752 * evmcs->page_fault_error_code_mask;
1753 * vmcs12->page_fault_error_code_match =
1754 * evmcs->page_fault_error_code_match;
1755 * vmcs12->cr3_target_count = evmcs->cr3_target_count;
1756 * vmcs12->vm_exit_msr_store_count = evmcs->vm_exit_msr_store_count;
1757 * vmcs12->vm_exit_msr_load_count = evmcs->vm_exit_msr_load_count;
1758 * vmcs12->vm_entry_msr_load_count = evmcs->vm_entry_msr_load_count;
1759 */
1760
1761 /*
1762 * Read only fields:
1763 * vmcs12->guest_physical_address = evmcs->guest_physical_address;
1764 * vmcs12->vm_instruction_error = evmcs->vm_instruction_error;
1765 * vmcs12->vm_exit_reason = evmcs->vm_exit_reason;
1766 * vmcs12->vm_exit_intr_info = evmcs->vm_exit_intr_info;
1767 * vmcs12->vm_exit_intr_error_code = evmcs->vm_exit_intr_error_code;
1768 * vmcs12->idt_vectoring_info_field = evmcs->idt_vectoring_info_field;
1769 * vmcs12->idt_vectoring_error_code = evmcs->idt_vectoring_error_code;
1770 * vmcs12->vm_exit_instruction_len = evmcs->vm_exit_instruction_len;
1771 * vmcs12->vmx_instruction_info = evmcs->vmx_instruction_info;
1772 * vmcs12->exit_qualification = evmcs->exit_qualification;
1773 * vmcs12->guest_linear_address = evmcs->guest_linear_address;
1774 *
1775 * Not present in struct vmcs12:
1776 * vmcs12->exit_io_instruction_ecx = evmcs->exit_io_instruction_ecx;
1777 * vmcs12->exit_io_instruction_esi = evmcs->exit_io_instruction_esi;
1778 * vmcs12->exit_io_instruction_edi = evmcs->exit_io_instruction_edi;
1779 * vmcs12->exit_io_instruction_eip = evmcs->exit_io_instruction_eip;
1780 */
1781
Vitaly Kuznetsov25641ca2021-05-26 15:20:19 +02001782 return;
Sean Christopherson55d23752018-12-03 13:53:18 -08001783}
1784
Vitaly Kuznetsov25641ca2021-05-26 15:20:19 +02001785static void copy_vmcs12_to_enlightened(struct vcpu_vmx *vmx)
Sean Christopherson55d23752018-12-03 13:53:18 -08001786{
1787 struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12;
1788 struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
1789
1790 /*
1791 * Should not be changed by KVM:
1792 *
1793 * evmcs->host_es_selector = vmcs12->host_es_selector;
1794 * evmcs->host_cs_selector = vmcs12->host_cs_selector;
1795 * evmcs->host_ss_selector = vmcs12->host_ss_selector;
1796 * evmcs->host_ds_selector = vmcs12->host_ds_selector;
1797 * evmcs->host_fs_selector = vmcs12->host_fs_selector;
1798 * evmcs->host_gs_selector = vmcs12->host_gs_selector;
1799 * evmcs->host_tr_selector = vmcs12->host_tr_selector;
1800 * evmcs->host_ia32_pat = vmcs12->host_ia32_pat;
1801 * evmcs->host_ia32_efer = vmcs12->host_ia32_efer;
1802 * evmcs->host_cr0 = vmcs12->host_cr0;
1803 * evmcs->host_cr3 = vmcs12->host_cr3;
1804 * evmcs->host_cr4 = vmcs12->host_cr4;
1805 * evmcs->host_ia32_sysenter_esp = vmcs12->host_ia32_sysenter_esp;
1806 * evmcs->host_ia32_sysenter_eip = vmcs12->host_ia32_sysenter_eip;
1807 * evmcs->host_rip = vmcs12->host_rip;
1808 * evmcs->host_ia32_sysenter_cs = vmcs12->host_ia32_sysenter_cs;
1809 * evmcs->host_fs_base = vmcs12->host_fs_base;
1810 * evmcs->host_gs_base = vmcs12->host_gs_base;
1811 * evmcs->host_tr_base = vmcs12->host_tr_base;
1812 * evmcs->host_gdtr_base = vmcs12->host_gdtr_base;
1813 * evmcs->host_idtr_base = vmcs12->host_idtr_base;
1814 * evmcs->host_rsp = vmcs12->host_rsp;
Sean Christopherson3731905ef2019-05-07 08:36:27 -07001815 * sync_vmcs02_to_vmcs12() doesn't read these:
Sean Christopherson55d23752018-12-03 13:53:18 -08001816 * evmcs->io_bitmap_a = vmcs12->io_bitmap_a;
1817 * evmcs->io_bitmap_b = vmcs12->io_bitmap_b;
1818 * evmcs->msr_bitmap = vmcs12->msr_bitmap;
1819 * evmcs->ept_pointer = vmcs12->ept_pointer;
1820 * evmcs->xss_exit_bitmap = vmcs12->xss_exit_bitmap;
1821 * evmcs->vm_exit_msr_store_addr = vmcs12->vm_exit_msr_store_addr;
1822 * evmcs->vm_exit_msr_load_addr = vmcs12->vm_exit_msr_load_addr;
1823 * evmcs->vm_entry_msr_load_addr = vmcs12->vm_entry_msr_load_addr;
Sean Christopherson55d23752018-12-03 13:53:18 -08001824 * evmcs->tpr_threshold = vmcs12->tpr_threshold;
1825 * evmcs->virtual_processor_id = vmcs12->virtual_processor_id;
1826 * evmcs->exception_bitmap = vmcs12->exception_bitmap;
1827 * evmcs->vmcs_link_pointer = vmcs12->vmcs_link_pointer;
1828 * evmcs->pin_based_vm_exec_control = vmcs12->pin_based_vm_exec_control;
1829 * evmcs->vm_exit_controls = vmcs12->vm_exit_controls;
1830 * evmcs->secondary_vm_exec_control = vmcs12->secondary_vm_exec_control;
1831 * evmcs->page_fault_error_code_mask =
1832 * vmcs12->page_fault_error_code_mask;
1833 * evmcs->page_fault_error_code_match =
1834 * vmcs12->page_fault_error_code_match;
1835 * evmcs->cr3_target_count = vmcs12->cr3_target_count;
1836 * evmcs->virtual_apic_page_addr = vmcs12->virtual_apic_page_addr;
1837 * evmcs->tsc_offset = vmcs12->tsc_offset;
1838 * evmcs->guest_ia32_debugctl = vmcs12->guest_ia32_debugctl;
1839 * evmcs->cr0_guest_host_mask = vmcs12->cr0_guest_host_mask;
1840 * evmcs->cr4_guest_host_mask = vmcs12->cr4_guest_host_mask;
1841 * evmcs->cr0_read_shadow = vmcs12->cr0_read_shadow;
1842 * evmcs->cr4_read_shadow = vmcs12->cr4_read_shadow;
1843 * evmcs->vm_exit_msr_store_count = vmcs12->vm_exit_msr_store_count;
1844 * evmcs->vm_exit_msr_load_count = vmcs12->vm_exit_msr_load_count;
1845 * evmcs->vm_entry_msr_load_count = vmcs12->vm_entry_msr_load_count;
1846 *
1847 * Not present in struct vmcs12:
1848 * evmcs->exit_io_instruction_ecx = vmcs12->exit_io_instruction_ecx;
1849 * evmcs->exit_io_instruction_esi = vmcs12->exit_io_instruction_esi;
1850 * evmcs->exit_io_instruction_edi = vmcs12->exit_io_instruction_edi;
1851 * evmcs->exit_io_instruction_eip = vmcs12->exit_io_instruction_eip;
1852 */
1853
1854 evmcs->guest_es_selector = vmcs12->guest_es_selector;
1855 evmcs->guest_cs_selector = vmcs12->guest_cs_selector;
1856 evmcs->guest_ss_selector = vmcs12->guest_ss_selector;
1857 evmcs->guest_ds_selector = vmcs12->guest_ds_selector;
1858 evmcs->guest_fs_selector = vmcs12->guest_fs_selector;
1859 evmcs->guest_gs_selector = vmcs12->guest_gs_selector;
1860 evmcs->guest_ldtr_selector = vmcs12->guest_ldtr_selector;
1861 evmcs->guest_tr_selector = vmcs12->guest_tr_selector;
1862
1863 evmcs->guest_es_limit = vmcs12->guest_es_limit;
1864 evmcs->guest_cs_limit = vmcs12->guest_cs_limit;
1865 evmcs->guest_ss_limit = vmcs12->guest_ss_limit;
1866 evmcs->guest_ds_limit = vmcs12->guest_ds_limit;
1867 evmcs->guest_fs_limit = vmcs12->guest_fs_limit;
1868 evmcs->guest_gs_limit = vmcs12->guest_gs_limit;
1869 evmcs->guest_ldtr_limit = vmcs12->guest_ldtr_limit;
1870 evmcs->guest_tr_limit = vmcs12->guest_tr_limit;
1871 evmcs->guest_gdtr_limit = vmcs12->guest_gdtr_limit;
1872 evmcs->guest_idtr_limit = vmcs12->guest_idtr_limit;
1873
1874 evmcs->guest_es_ar_bytes = vmcs12->guest_es_ar_bytes;
1875 evmcs->guest_cs_ar_bytes = vmcs12->guest_cs_ar_bytes;
1876 evmcs->guest_ss_ar_bytes = vmcs12->guest_ss_ar_bytes;
1877 evmcs->guest_ds_ar_bytes = vmcs12->guest_ds_ar_bytes;
1878 evmcs->guest_fs_ar_bytes = vmcs12->guest_fs_ar_bytes;
1879 evmcs->guest_gs_ar_bytes = vmcs12->guest_gs_ar_bytes;
1880 evmcs->guest_ldtr_ar_bytes = vmcs12->guest_ldtr_ar_bytes;
1881 evmcs->guest_tr_ar_bytes = vmcs12->guest_tr_ar_bytes;
1882
1883 evmcs->guest_es_base = vmcs12->guest_es_base;
1884 evmcs->guest_cs_base = vmcs12->guest_cs_base;
1885 evmcs->guest_ss_base = vmcs12->guest_ss_base;
1886 evmcs->guest_ds_base = vmcs12->guest_ds_base;
1887 evmcs->guest_fs_base = vmcs12->guest_fs_base;
1888 evmcs->guest_gs_base = vmcs12->guest_gs_base;
1889 evmcs->guest_ldtr_base = vmcs12->guest_ldtr_base;
1890 evmcs->guest_tr_base = vmcs12->guest_tr_base;
1891 evmcs->guest_gdtr_base = vmcs12->guest_gdtr_base;
1892 evmcs->guest_idtr_base = vmcs12->guest_idtr_base;
1893
1894 evmcs->guest_ia32_pat = vmcs12->guest_ia32_pat;
1895 evmcs->guest_ia32_efer = vmcs12->guest_ia32_efer;
1896
1897 evmcs->guest_pdptr0 = vmcs12->guest_pdptr0;
1898 evmcs->guest_pdptr1 = vmcs12->guest_pdptr1;
1899 evmcs->guest_pdptr2 = vmcs12->guest_pdptr2;
1900 evmcs->guest_pdptr3 = vmcs12->guest_pdptr3;
1901
1902 evmcs->guest_pending_dbg_exceptions =
1903 vmcs12->guest_pending_dbg_exceptions;
1904 evmcs->guest_sysenter_esp = vmcs12->guest_sysenter_esp;
1905 evmcs->guest_sysenter_eip = vmcs12->guest_sysenter_eip;
1906
1907 evmcs->guest_activity_state = vmcs12->guest_activity_state;
1908 evmcs->guest_sysenter_cs = vmcs12->guest_sysenter_cs;
1909
1910 evmcs->guest_cr0 = vmcs12->guest_cr0;
1911 evmcs->guest_cr3 = vmcs12->guest_cr3;
1912 evmcs->guest_cr4 = vmcs12->guest_cr4;
1913 evmcs->guest_dr7 = vmcs12->guest_dr7;
1914
1915 evmcs->guest_physical_address = vmcs12->guest_physical_address;
1916
1917 evmcs->vm_instruction_error = vmcs12->vm_instruction_error;
1918 evmcs->vm_exit_reason = vmcs12->vm_exit_reason;
1919 evmcs->vm_exit_intr_info = vmcs12->vm_exit_intr_info;
1920 evmcs->vm_exit_intr_error_code = vmcs12->vm_exit_intr_error_code;
1921 evmcs->idt_vectoring_info_field = vmcs12->idt_vectoring_info_field;
1922 evmcs->idt_vectoring_error_code = vmcs12->idt_vectoring_error_code;
1923 evmcs->vm_exit_instruction_len = vmcs12->vm_exit_instruction_len;
1924 evmcs->vmx_instruction_info = vmcs12->vmx_instruction_info;
1925
1926 evmcs->exit_qualification = vmcs12->exit_qualification;
1927
1928 evmcs->guest_linear_address = vmcs12->guest_linear_address;
1929 evmcs->guest_rsp = vmcs12->guest_rsp;
1930 evmcs->guest_rflags = vmcs12->guest_rflags;
1931
1932 evmcs->guest_interruptibility_info =
1933 vmcs12->guest_interruptibility_info;
1934 evmcs->cpu_based_vm_exec_control = vmcs12->cpu_based_vm_exec_control;
1935 evmcs->vm_entry_controls = vmcs12->vm_entry_controls;
1936 evmcs->vm_entry_intr_info_field = vmcs12->vm_entry_intr_info_field;
1937 evmcs->vm_entry_exception_error_code =
1938 vmcs12->vm_entry_exception_error_code;
1939 evmcs->vm_entry_instruction_len = vmcs12->vm_entry_instruction_len;
1940
1941 evmcs->guest_rip = vmcs12->guest_rip;
1942
1943 evmcs->guest_bndcfgs = vmcs12->guest_bndcfgs;
1944
Vitaly Kuznetsov25641ca2021-05-26 15:20:19 +02001945 return;
Sean Christopherson55d23752018-12-03 13:53:18 -08001946}
1947
1948/*
1949 * This is an equivalent of the nested hypervisor executing the vmptrld
1950 * instruction.
1951 */
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01001952static enum nested_evmptrld_status nested_vmx_handle_enlightened_vmptrld(
1953 struct kvm_vcpu *vcpu, bool from_launch)
Sean Christopherson55d23752018-12-03 13:53:18 -08001954{
1955 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsova21a39c2019-06-28 13:23:32 +02001956 bool evmcs_gpa_changed = false;
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02001957 u64 evmcs_gpa;
Sean Christopherson55d23752018-12-03 13:53:18 -08001958
1959 if (likely(!vmx->nested.enlightened_vmcs_enabled))
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01001960 return EVMPTRLD_DISABLED;
Sean Christopherson55d23752018-12-03 13:53:18 -08001961
Vitaly Kuznetsov02761712021-05-26 15:20:18 +02001962 if (!nested_enlightened_vmentry(vcpu, &evmcs_gpa)) {
1963 nested_release_evmcs(vcpu);
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01001964 return EVMPTRLD_DISABLED;
Vitaly Kuznetsov02761712021-05-26 15:20:18 +02001965 }
Sean Christopherson55d23752018-12-03 13:53:18 -08001966
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02001967 if (unlikely(evmcs_gpa != vmx->nested.hv_evmcs_vmptr)) {
Yu Zhang64c78502021-09-30 01:51:53 +08001968 vmx->nested.current_vmptr = INVALID_GPA;
Sean Christopherson55d23752018-12-03 13:53:18 -08001969
1970 nested_release_evmcs(vcpu);
1971
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02001972 if (kvm_vcpu_map(vcpu, gpa_to_gfn(evmcs_gpa),
KarimAllah Ahmeddee9c042019-01-31 21:24:42 +01001973 &vmx->nested.hv_evmcs_map))
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01001974 return EVMPTRLD_ERROR;
Sean Christopherson55d23752018-12-03 13:53:18 -08001975
KarimAllah Ahmeddee9c042019-01-31 21:24:42 +01001976 vmx->nested.hv_evmcs = vmx->nested.hv_evmcs_map.hva;
Sean Christopherson55d23752018-12-03 13:53:18 -08001977
1978 /*
1979 * Currently, KVM only supports eVMCS version 1
1980 * (== KVM_EVMCS_VERSION) and thus we expect guest to set this
1981 * value to first u32 field of eVMCS which should specify eVMCS
1982 * VersionNumber.
1983 *
1984 * Guest should be aware of supported eVMCS versions by host by
1985 * examining CPUID.0x4000000A.EAX[0:15]. Host userspace VMM is
1986 * expected to set this CPUID leaf according to the value
1987 * returned in vmcs_version from nested_enable_evmcs().
1988 *
1989 * However, it turns out that Microsoft Hyper-V fails to comply
1990 * to their own invented interface: When Hyper-V use eVMCS, it
1991 * just sets first u32 field of eVMCS to revision_id specified
1992 * in MSR_IA32_VMX_BASIC. Instead of used eVMCS version number
1993 * which is one of the supported versions specified in
1994 * CPUID.0x4000000A.EAX[0:15].
1995 *
1996 * To overcome Hyper-V bug, we accept here either a supported
1997 * eVMCS version or VMCS12 revision_id as valid values for first
1998 * u32 field of eVMCS.
1999 */
2000 if ((vmx->nested.hv_evmcs->revision_id != KVM_EVMCS_VERSION) &&
2001 (vmx->nested.hv_evmcs->revision_id != VMCS12_REVISION)) {
2002 nested_release_evmcs(vcpu);
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01002003 return EVMPTRLD_VMFAIL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002004 }
2005
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02002006 vmx->nested.hv_evmcs_vmptr = evmcs_gpa;
Sean Christopherson55d23752018-12-03 13:53:18 -08002007
Vitaly Kuznetsova21a39c2019-06-28 13:23:32 +02002008 evmcs_gpa_changed = true;
Sean Christopherson55d23752018-12-03 13:53:18 -08002009 /*
2010 * Unlike normal vmcs12, enlightened vmcs12 is not fully
2011 * reloaded from guest's memory (read only fields, fields not
2012 * present in struct hv_enlightened_vmcs, ...). Make sure there
2013 * are no leftovers.
2014 */
2015 if (from_launch) {
2016 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2017 memset(vmcs12, 0, sizeof(*vmcs12));
2018 vmcs12->hdr.revision_id = VMCS12_REVISION;
2019 }
2020
2021 }
Vitaly Kuznetsova21a39c2019-06-28 13:23:32 +02002022
2023 /*
Miaohe Linffdbd502020-02-07 23:22:45 +08002024 * Clean fields data can't be used on VMLAUNCH and when we switch
Vitaly Kuznetsova21a39c2019-06-28 13:23:32 +02002025 * between different L2 guests as KVM keeps a single VMCS12 per L1.
2026 */
2027 if (from_launch || evmcs_gpa_changed)
2028 vmx->nested.hv_evmcs->hv_clean_fields &=
2029 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
2030
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01002031 return EVMPTRLD_SUCCEEDED;
Sean Christopherson55d23752018-12-03 13:53:18 -08002032}
2033
Sean Christopherson3731905ef2019-05-07 08:36:27 -07002034void nested_sync_vmcs12_to_shadow(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08002035{
2036 struct vcpu_vmx *vmx = to_vmx(vcpu);
2037
Vitaly Kuznetsovdc313382021-05-26 15:20:24 +02002038 if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
Sean Christopherson55d23752018-12-03 13:53:18 -08002039 copy_vmcs12_to_enlightened(vmx);
Vitaly Kuznetsovdc313382021-05-26 15:20:24 +02002040 else
Sean Christopherson55d23752018-12-03 13:53:18 -08002041 copy_vmcs12_to_shadow(vmx);
Sean Christopherson55d23752018-12-03 13:53:18 -08002042
Sean Christopherson3731905ef2019-05-07 08:36:27 -07002043 vmx->nested.need_vmcs12_to_shadow_sync = false;
Sean Christopherson55d23752018-12-03 13:53:18 -08002044}
2045
2046static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
2047{
2048 struct vcpu_vmx *vmx =
2049 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
2050
2051 vmx->nested.preemption_timer_expired = true;
2052 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
2053 kvm_vcpu_kick(&vmx->vcpu);
2054
2055 return HRTIMER_NORESTART;
2056}
2057
Peter Shier850448f2020-05-26 14:51:06 -07002058static u64 vmx_calc_preemption_timer_value(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08002059{
Peter Shier850448f2020-05-26 14:51:06 -07002060 struct vcpu_vmx *vmx = to_vmx(vcpu);
2061 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Peter Shier850448f2020-05-26 14:51:06 -07002062
2063 u64 l1_scaled_tsc = kvm_read_l1_tsc(vcpu, rdtsc()) >>
2064 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
2065
2066 if (!vmx->nested.has_preemption_timer_deadline) {
Makarand Sonare8d7fbf02020-05-26 14:51:07 -07002067 vmx->nested.preemption_timer_deadline =
2068 vmcs12->vmx_preemption_timer_value + l1_scaled_tsc;
Peter Shier850448f2020-05-26 14:51:06 -07002069 vmx->nested.has_preemption_timer_deadline = true;
Makarand Sonare8d7fbf02020-05-26 14:51:07 -07002070 }
2071 return vmx->nested.preemption_timer_deadline - l1_scaled_tsc;
Peter Shier850448f2020-05-26 14:51:06 -07002072}
2073
2074static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu,
2075 u64 preemption_timeout)
2076{
Sean Christopherson55d23752018-12-03 13:53:18 -08002077 struct vcpu_vmx *vmx = to_vmx(vcpu);
2078
2079 /*
2080 * A timer value of zero is architecturally guaranteed to cause
2081 * a VMExit prior to executing any instructions in the guest.
2082 */
2083 if (preemption_timeout == 0) {
2084 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
2085 return;
2086 }
2087
2088 if (vcpu->arch.virtual_tsc_khz == 0)
2089 return;
2090
2091 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
2092 preemption_timeout *= 1000000;
2093 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
2094 hrtimer_start(&vmx->nested.preemption_timer,
Jim Mattsonada00982020-05-08 13:36:42 -07002095 ktime_add_ns(ktime_get(), preemption_timeout),
2096 HRTIMER_MODE_ABS_PINNED);
Sean Christopherson55d23752018-12-03 13:53:18 -08002097}
2098
2099static u64 nested_vmx_calc_efer(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
2100{
2101 if (vmx->nested.nested_run_pending &&
2102 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
2103 return vmcs12->guest_ia32_efer;
2104 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
2105 return vmx->vcpu.arch.efer | (EFER_LMA | EFER_LME);
2106 else
2107 return vmx->vcpu.arch.efer & ~(EFER_LMA | EFER_LME);
2108}
2109
2110static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx)
2111{
2112 /*
2113 * If vmcs02 hasn't been initialized, set the constant vmcs02 state
2114 * according to L0's settings (vmcs12 is irrelevant here). Host
2115 * fields that come from L0 and are not constant, e.g. HOST_CR3,
2116 * will be set as needed prior to VMLAUNCH/VMRESUME.
2117 */
2118 if (vmx->nested.vmcs02_initialized)
2119 return;
2120 vmx->nested.vmcs02_initialized = true;
2121
2122 /*
2123 * We don't care what the EPTP value is we just need to guarantee
2124 * it's valid so we don't get a false positive when doing early
2125 * consistency checks.
2126 */
2127 if (enable_ept && nested_early_check)
Sean Christopherson2a40b902020-07-15 20:41:18 -07002128 vmcs_write64(EPT_POINTER,
2129 construct_eptp(&vmx->vcpu, 0, PT64_ROOT_4LEVEL));
Sean Christopherson55d23752018-12-03 13:53:18 -08002130
2131 /* All VMFUNCs are currently emulated through L0 vmexits. */
2132 if (cpu_has_vmx_vmfunc())
2133 vmcs_write64(VM_FUNCTION_CONTROL, 0);
2134
2135 if (cpu_has_vmx_posted_intr())
2136 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
2137
2138 if (cpu_has_vmx_msr_bitmap())
2139 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
2140
Sean Christopherson4d6c9892019-05-07 09:06:30 -07002141 /*
Sean Christophersonc3bb9a22021-02-12 16:50:07 -08002142 * PML is emulated for L2, but never enabled in hardware as the MMU
2143 * handles A/D emulation. Disabling PML for L2 also avoids having to
2144 * deal with filtering out L2 GPAs from the buffer.
Sean Christopherson4d6c9892019-05-07 09:06:30 -07002145 */
2146 if (enable_pml) {
Sean Christophersonc3bb9a22021-02-12 16:50:07 -08002147 vmcs_write64(PML_ADDRESS, 0);
2148 vmcs_write16(GUEST_PML_INDEX, -1);
Sean Christopherson4d6c9892019-05-07 09:06:30 -07002149 }
Sean Christopherson55d23752018-12-03 13:53:18 -08002150
Sean Christophersonc538d572019-05-07 09:06:29 -07002151 if (cpu_has_vmx_encls_vmexit())
Yu Zhang64c78502021-09-30 01:51:53 +08002152 vmcs_write64(ENCLS_EXITING_BITMAP, INVALID_GPA);
Sean Christopherson55d23752018-12-03 13:53:18 -08002153
2154 /*
2155 * Set the MSR load/store lists to match L0's settings. Only the
2156 * addresses are constant (for vmcs02), the counts can change based
2157 * on L2's behavior, e.g. switching to/from long mode.
2158 */
Aaron Lewis662f1d12019-11-07 21:14:39 -08002159 vmcs_write64(VM_EXIT_MSR_STORE_ADDR, __pa(vmx->msr_autostore.guest.val));
Sean Christopherson55d23752018-12-03 13:53:18 -08002160 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
2161 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
2162
2163 vmx_set_constant_host_state(vmx);
2164}
2165
Paolo Bonzinib1346ab2019-06-06 17:24:00 +02002166static void prepare_vmcs02_early_rare(struct vcpu_vmx *vmx,
Sean Christopherson55d23752018-12-03 13:53:18 -08002167 struct vmcs12 *vmcs12)
2168{
2169 prepare_vmcs02_constant_state(vmx);
2170
Yu Zhang64c78502021-09-30 01:51:53 +08002171 vmcs_write64(VMCS_LINK_POINTER, INVALID_GPA);
Sean Christopherson55d23752018-12-03 13:53:18 -08002172
2173 if (enable_vpid) {
2174 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
2175 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
2176 else
2177 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
2178 }
2179}
2180
Sean Christopherson389ab252021-08-10 10:19:50 -07002181static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct loaded_vmcs *vmcs01,
2182 struct vmcs12 *vmcs12)
Sean Christopherson55d23752018-12-03 13:53:18 -08002183{
Sean Christophersonc3bb9a22021-02-12 16:50:07 -08002184 u32 exec_control;
Sean Christopherson55d23752018-12-03 13:53:18 -08002185 u64 guest_efer = nested_vmx_calc_efer(vmx, vmcs12);
2186
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02002187 if (vmx->nested.dirty_vmcs12 || evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
Paolo Bonzinib1346ab2019-06-06 17:24:00 +02002188 prepare_vmcs02_early_rare(vmx, vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08002189
2190 /*
Sean Christopherson55d23752018-12-03 13:53:18 -08002191 * PIN CONTROLS
2192 */
Sean Christopherson389ab252021-08-10 10:19:50 -07002193 exec_control = __pin_controls_get(vmcs01);
Sean Christopherson804939e2019-05-07 12:18:05 -07002194 exec_control |= (vmcs12->pin_based_vm_exec_control &
2195 ~PIN_BASED_VMX_PREEMPTION_TIMER);
Sean Christopherson55d23752018-12-03 13:53:18 -08002196
2197 /* Posted interrupts setting is only taken from vmcs12. */
Sean Christophersonf7782bb82021-08-10 07:45:26 -07002198 vmx->nested.pi_pending = false;
2199 if (nested_cpu_has_posted_intr(vmcs12))
Sean Christopherson55d23752018-12-03 13:53:18 -08002200 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
Sean Christophersonf7782bb82021-08-10 07:45:26 -07002201 else
Sean Christopherson55d23752018-12-03 13:53:18 -08002202 exec_control &= ~PIN_BASED_POSTED_INTR;
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002203 pin_controls_set(vmx, exec_control);
Sean Christopherson55d23752018-12-03 13:53:18 -08002204
2205 /*
2206 * EXEC CONTROLS
2207 */
Sean Christopherson389ab252021-08-10 10:19:50 -07002208 exec_control = __exec_controls_get(vmcs01); /* L0's desires */
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08002209 exec_control &= ~CPU_BASED_INTR_WINDOW_EXITING;
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08002210 exec_control &= ~CPU_BASED_NMI_WINDOW_EXITING;
Sean Christopherson55d23752018-12-03 13:53:18 -08002211 exec_control &= ~CPU_BASED_TPR_SHADOW;
2212 exec_control |= vmcs12->cpu_based_vm_exec_control;
2213
Liran Alon02d496cf2019-11-11 14:30:55 +02002214 vmx->nested.l1_tpr_threshold = -1;
Sean Christophersonca2f5462019-05-07 09:06:33 -07002215 if (exec_control & CPU_BASED_TPR_SHADOW)
Sean Christopherson55d23752018-12-03 13:53:18 -08002216 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Sean Christopherson55d23752018-12-03 13:53:18 -08002217#ifdef CONFIG_X86_64
Sean Christophersonca2f5462019-05-07 09:06:33 -07002218 else
Sean Christopherson55d23752018-12-03 13:53:18 -08002219 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
2220 CPU_BASED_CR8_STORE_EXITING;
2221#endif
Sean Christopherson55d23752018-12-03 13:53:18 -08002222
2223 /*
2224 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
2225 * for I/O port accesses.
2226 */
Sean Christopherson55d23752018-12-03 13:53:18 -08002227 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
Sean Christophersonde0286b2019-05-07 12:18:01 -07002228 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
2229
2230 /*
2231 * This bit will be computed in nested_get_vmcs12_pages, because
2232 * we do not have access to L1's MSR bitmap yet. For now, keep
2233 * the same bit as before, hoping to avoid multiple VMWRITEs that
2234 * only set/clear this bit.
2235 */
2236 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
2237 exec_control |= exec_controls_get(vmx) & CPU_BASED_USE_MSR_BITMAPS;
2238
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002239 exec_controls_set(vmx, exec_control);
Sean Christopherson55d23752018-12-03 13:53:18 -08002240
2241 /*
2242 * SECONDARY EXEC CONTROLS
2243 */
2244 if (cpu_has_secondary_exec_ctrls()) {
Sean Christopherson389ab252021-08-10 10:19:50 -07002245 exec_control = __secondary_exec_controls_get(vmcs01);
Sean Christopherson55d23752018-12-03 13:53:18 -08002246
2247 /* Take the following fields only from vmcs12 */
2248 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Sean Christopherson389ab252021-08-10 10:19:50 -07002249 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sean Christopherson55d23752018-12-03 13:53:18 -08002250 SECONDARY_EXEC_ENABLE_INVPCID |
Sean Christopherson7f3603b2020-09-23 09:50:47 -07002251 SECONDARY_EXEC_ENABLE_RDTSCP |
Sean Christopherson55d23752018-12-03 13:53:18 -08002252 SECONDARY_EXEC_XSAVES |
Tao Xue69e72fa2019-07-16 14:55:49 +08002253 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE |
Sean Christopherson55d23752018-12-03 13:53:18 -08002254 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
2255 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Ilias Stamatisd041b5e2021-05-26 19:44:17 +01002256 SECONDARY_EXEC_ENABLE_VMFUNC |
Sean Christopherson389ab252021-08-10 10:19:50 -07002257 SECONDARY_EXEC_TSC_SCALING |
2258 SECONDARY_EXEC_DESC);
2259
Sean Christopherson55d23752018-12-03 13:53:18 -08002260 if (nested_cpu_has(vmcs12,
Sean Christophersonc3bb9a22021-02-12 16:50:07 -08002261 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
2262 exec_control |= vmcs12->secondary_vm_exec_control;
2263
2264 /* PML is emulated and never enabled in hardware for L2. */
2265 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Sean Christopherson55d23752018-12-03 13:53:18 -08002266
2267 /* VMCS shadowing for L2 is emulated for now */
2268 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
2269
Sean Christopherson469debd2019-05-07 12:18:02 -07002270 /*
2271 * Preset *DT exiting when emulating UMIP, so that vmx_set_cr4()
2272 * will not have to rewrite the controls just for this bit.
2273 */
2274 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated() &&
2275 (vmcs12->guest_cr4 & X86_CR4_UMIP))
2276 exec_control |= SECONDARY_EXEC_DESC;
2277
Sean Christopherson55d23752018-12-03 13:53:18 -08002278 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
2279 vmcs_write16(GUEST_INTR_STATUS,
2280 vmcs12->guest_intr_status);
2281
Krish Sadhukhanbddd82d2020-09-21 08:10:25 +00002282 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
2283 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
2284
Sean Christopherson72add912021-04-12 16:21:42 +12002285 if (exec_control & SECONDARY_EXEC_ENCLS_EXITING)
2286 vmx_write_encls_bitmap(&vmx->vcpu, vmcs12);
2287
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002288 secondary_exec_controls_set(vmx, exec_control);
Sean Christopherson55d23752018-12-03 13:53:18 -08002289 }
2290
2291 /*
2292 * ENTRY CONTROLS
2293 *
2294 * vmcs12's VM_{ENTRY,EXIT}_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE
2295 * are emulated by vmx_set_efer() in prepare_vmcs02(), but speculate
2296 * on the related bits (if supported by the CPU) in the hope that
2297 * we can avoid VMWrites during vmx_set_efer().
2298 */
Sean Christopherson389ab252021-08-10 10:19:50 -07002299 exec_control = __vm_entry_controls_get(vmcs01);
2300 exec_control |= vmcs12->vm_entry_controls;
2301 exec_control &= ~(VM_ENTRY_IA32E_MODE | VM_ENTRY_LOAD_IA32_EFER);
Sean Christopherson55d23752018-12-03 13:53:18 -08002302 if (cpu_has_load_ia32_efer()) {
2303 if (guest_efer & EFER_LMA)
2304 exec_control |= VM_ENTRY_IA32E_MODE;
2305 if (guest_efer != host_efer)
2306 exec_control |= VM_ENTRY_LOAD_IA32_EFER;
2307 }
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002308 vm_entry_controls_set(vmx, exec_control);
Sean Christopherson55d23752018-12-03 13:53:18 -08002309
2310 /*
2311 * EXIT CONTROLS
2312 *
2313 * L2->L1 exit controls are emulated - the hardware exit is to L0 so
2314 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
2315 * bits may be modified by vmx_set_efer() in prepare_vmcs02().
2316 */
Sean Christopherson389ab252021-08-10 10:19:50 -07002317 exec_control = __vm_exit_controls_get(vmcs01);
Sean Christopherson55d23752018-12-03 13:53:18 -08002318 if (cpu_has_load_ia32_efer() && guest_efer != host_efer)
2319 exec_control |= VM_EXIT_LOAD_IA32_EFER;
Sean Christopherson389ab252021-08-10 10:19:50 -07002320 else
2321 exec_control &= ~VM_EXIT_LOAD_IA32_EFER;
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002322 vm_exit_controls_set(vmx, exec_control);
Sean Christopherson55d23752018-12-03 13:53:18 -08002323
2324 /*
2325 * Interrupt/Exception Fields
2326 */
2327 if (vmx->nested.nested_run_pending) {
2328 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2329 vmcs12->vm_entry_intr_info_field);
2330 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
2331 vmcs12->vm_entry_exception_error_code);
2332 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2333 vmcs12->vm_entry_instruction_len);
2334 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
2335 vmcs12->guest_interruptibility_info);
2336 vmx->loaded_vmcs->nmi_known_unmasked =
2337 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
2338 } else {
2339 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
2340 }
2341}
2342
Paolo Bonzinib1346ab2019-06-06 17:24:00 +02002343static void prepare_vmcs02_rare(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
Sean Christopherson55d23752018-12-03 13:53:18 -08002344{
2345 struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs;
2346
2347 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
2348 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2)) {
2349 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
2350 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
2351 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
2352 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
2353 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
2354 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
2355 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
2356 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
2357 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
2358 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
2359 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
2360 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
2361 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
2362 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
2363 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
2364 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
2365 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
2366 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07002367 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
2368 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
Sean Christopherson55d23752018-12-03 13:53:18 -08002369 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
2370 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
2371 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
2372 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
2373 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
2374 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
2375 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
2376 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
2377 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
2378 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
2379 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
2380 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
2381 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
2382 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
2383 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
2384 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Sean Christophersonfc387d82020-09-23 11:44:46 -07002385
2386 vmx->segment_cache.bitmask = 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08002387 }
2388
2389 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
2390 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1)) {
2391 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
2392 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
2393 vmcs12->guest_pending_dbg_exceptions);
2394 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
2395 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
2396
2397 /*
2398 * L1 may access the L2's PDPTR, so save them to construct
2399 * vmcs12
2400 */
2401 if (enable_ept) {
2402 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
2403 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
2404 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
2405 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
2406 }
Sean Christophersonc27e5b02019-05-07 09:06:39 -07002407
2408 if (kvm_mpx_supported() && vmx->nested.nested_run_pending &&
2409 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
2410 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
Sean Christopherson55d23752018-12-03 13:53:18 -08002411 }
2412
2413 if (nested_cpu_has_xsaves(vmcs12))
2414 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
2415
2416 /*
2417 * Whether page-faults are trapped is determined by a combination of
Paolo Bonzinia0c13432020-07-10 17:48:08 +02002418 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF. If L0
2419 * doesn't care about page faults then we should set all of these to
2420 * L1's desires. However, if L0 does care about (some) page faults, it
2421 * is not easy (if at all possible?) to merge L0 and L1's desires, we
2422 * simply ask to exit on each and every L2 page fault. This is done by
2423 * setting MASK=MATCH=0 and (see below) EB.PF=1.
Sean Christopherson55d23752018-12-03 13:53:18 -08002424 * Note that below we don't need special code to set EB.PF beyond the
2425 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
2426 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
2427 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
2428 */
Paolo Bonzinia0c13432020-07-10 17:48:08 +02002429 if (vmx_need_pf_intercept(&vmx->vcpu)) {
2430 /*
2431 * TODO: if both L0 and L1 need the same MASK and MATCH,
2432 * go ahead and use it?
2433 */
2434 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
2435 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
2436 } else {
2437 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, vmcs12->page_fault_error_code_mask);
2438 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, vmcs12->page_fault_error_code_match);
2439 }
Sean Christopherson55d23752018-12-03 13:53:18 -08002440
2441 if (cpu_has_vmx_apicv()) {
2442 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
2443 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
2444 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
2445 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
2446 }
2447
Aaron Lewis662f1d12019-11-07 21:14:39 -08002448 /*
2449 * Make sure the msr_autostore list is up to date before we set the
2450 * count in the vmcs02.
2451 */
2452 prepare_vmx_msr_autostore_list(&vmx->vcpu, MSR_IA32_TSC);
2453
2454 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, vmx->msr_autostore.guest.nr);
Sean Christopherson55d23752018-12-03 13:53:18 -08002455 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
2456 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
2457
2458 set_cr4_guest_host_mask(vmx);
Sean Christopherson55d23752018-12-03 13:53:18 -08002459}
2460
2461/*
2462 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
2463 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
2464 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
2465 * guest in a way that will both be appropriate to L1's requests, and our
2466 * needs. In addition to modifying the active vmcs (which is vmcs02), this
2467 * function also has additional necessary side-effects, like setting various
2468 * vcpu->arch fields.
2469 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
2470 * is assigned to entry_failure_code on failure.
2471 */
2472static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Maxim Levitsky0f857222021-06-07 12:02:00 +03002473 bool from_vmentry,
Sean Christopherson68cda402020-05-11 15:05:29 -07002474 enum vm_entry_failure_code *entry_failure_code)
Sean Christopherson55d23752018-12-03 13:53:18 -08002475{
2476 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersonc7554efc2019-05-07 09:06:40 -07002477 bool load_guest_pdptrs_vmcs12 = false;
Sean Christopherson55d23752018-12-03 13:53:18 -08002478
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02002479 if (vmx->nested.dirty_vmcs12 || evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) {
Paolo Bonzinib1346ab2019-06-06 17:24:00 +02002480 prepare_vmcs02_rare(vmx, vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08002481 vmx->nested.dirty_vmcs12 = false;
Sean Christopherson55d23752018-12-03 13:53:18 -08002482
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02002483 load_guest_pdptrs_vmcs12 = !evmptr_is_valid(vmx->nested.hv_evmcs_vmptr) ||
2484 !(vmx->nested.hv_evmcs->hv_clean_fields &
Sean Christophersonc7554efc2019-05-07 09:06:40 -07002485 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1);
Sean Christopherson55d23752018-12-03 13:53:18 -08002486 }
2487
2488 if (vmx->nested.nested_run_pending &&
2489 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
2490 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
2491 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
2492 } else {
2493 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
2494 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
2495 }
Sean Christopherson3b013a22019-05-07 09:06:28 -07002496 if (kvm_mpx_supported() && (!vmx->nested.nested_run_pending ||
2497 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)))
2498 vmcs_write64(GUEST_BNDCFGS, vmx->nested.vmcs01_guest_bndcfgs);
Sean Christopherson55d23752018-12-03 13:53:18 -08002499 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
2500
Sean Christopherson55d23752018-12-03 13:53:18 -08002501 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
2502 * bitwise-or of what L1 wants to trap for L2, and what we want to
2503 * trap. Note that CR0.TS also needs updating - we do this later.
2504 */
Jason Baronb6a7cc32021-01-14 22:27:54 -05002505 vmx_update_exception_bitmap(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08002506 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
2507 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
2508
2509 if (vmx->nested.nested_run_pending &&
2510 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
2511 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
2512 vcpu->arch.pat = vmcs12->guest_ia32_pat;
2513 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2514 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
2515 }
2516
Ilias Stamatisd041b5e2021-05-26 19:44:17 +01002517 vcpu->arch.tsc_offset = kvm_calc_nested_tsc_offset(
2518 vcpu->arch.l1_tsc_offset,
2519 vmx_get_l2_tsc_offset(vcpu),
2520 vmx_get_l2_tsc_multiplier(vcpu));
2521
2522 vcpu->arch.tsc_scaling_ratio = kvm_calc_nested_tsc_multiplier(
2523 vcpu->arch.l1_tsc_scaling_ratio,
2524 vmx_get_l2_tsc_multiplier(vcpu));
2525
Sean Christopherson55d23752018-12-03 13:53:18 -08002526 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Sean Christopherson55d23752018-12-03 13:53:18 -08002527 if (kvm_has_tsc_control)
Ilias Stamatis1ab92872021-06-07 11:54:38 +01002528 vmcs_write64(TSC_MULTIPLIER, vcpu->arch.tsc_scaling_ratio);
Sean Christopherson55d23752018-12-03 13:53:18 -08002529
Sean Christopherson50b265a2020-03-20 14:28:19 -07002530 nested_vmx_transition_tlb_flush(vcpu, vmcs12, true);
Sean Christopherson55d23752018-12-03 13:53:18 -08002531
2532 if (nested_cpu_has_ept(vmcs12))
2533 nested_ept_init_mmu_context(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08002534
2535 /*
2536 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
2537 * bits which we consider mandatory enabled.
2538 * The CR0_READ_SHADOW is what L2 should have expected to read given
2539 * the specifications by L1; It's not enough to take
2540 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
2541 * have more bits than L1 expected.
2542 */
2543 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
2544 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
2545
2546 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
2547 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
2548
2549 vcpu->arch.efer = nested_vmx_calc_efer(vmx, vmcs12);
2550 /* Note: may modify VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
2551 vmx_set_efer(vcpu, vcpu->arch.efer);
2552
2553 /*
2554 * Guest state is invalid and unrestricted guest is disabled,
2555 * which means L1 attempted VMEntry to L2 with invalid state.
2556 * Fail the VMEntry.
Maxim Levitskyc8607e42021-09-13 17:09:53 +03002557 *
2558 * However when force loading the guest state (SMM exit or
2559 * loading nested state after migration, it is possible to
2560 * have invalid guest state now, which will be later fixed by
2561 * restoring L2 register state
Sean Christopherson55d23752018-12-03 13:53:18 -08002562 */
Maxim Levitskyc8607e42021-09-13 17:09:53 +03002563 if (CC(from_vmentry && !vmx_guest_state_valid(vcpu))) {
Sean Christopherson55d23752018-12-03 13:53:18 -08002564 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christophersonc80add02019-04-11 12:18:09 -07002565 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002566 }
2567
2568 /* Shadow page tables on either EPT or shadow page tables. */
2569 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Maxim Levitsky0f857222021-06-07 12:02:00 +03002570 from_vmentry, entry_failure_code))
Sean Christophersonc80add02019-04-11 12:18:09 -07002571 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002572
Sean Christopherson04f11ef2019-09-27 14:45:16 -07002573 /*
2574 * Immediately write vmcs02.GUEST_CR3. It will be propagated to vmcs12
2575 * on nested VM-Exit, which can occur without actually running L2 and
Paolo Bonzini727a7e22020-03-05 03:52:50 -05002576 * thus without hitting vmx_load_mmu_pgd(), e.g. if L1 is entering L2 with
Sean Christopherson04f11ef2019-09-27 14:45:16 -07002577 * vmcs12.GUEST_ACTIVITYSTATE=HLT, in which case KVM will intercept the
2578 * transition to HLT instead of running L2.
2579 */
2580 if (enable_ept)
2581 vmcs_writel(GUEST_CR3, vmcs12->guest_cr3);
2582
Sean Christophersonc7554efc2019-05-07 09:06:40 -07002583 /* Late preparation of GUEST_PDPTRs now that EFER and CRs are set. */
2584 if (load_guest_pdptrs_vmcs12 && nested_cpu_has_ept(vmcs12) &&
2585 is_pae_paging(vcpu)) {
2586 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
2587 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
2588 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
2589 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
2590 }
2591
Sean Christopherson55d23752018-12-03 13:53:18 -08002592 if (!enable_ept)
2593 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
2594
Oliver Upton71f73472019-11-13 16:17:19 -08002595 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) &&
Oliver Uptond1968422019-12-13 16:33:58 -08002596 WARN_ON_ONCE(kvm_set_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL,
2597 vmcs12->guest_ia32_perf_global_ctrl)))
Oliver Upton71f73472019-11-13 16:17:19 -08002598 return -EINVAL;
2599
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02002600 kvm_rsp_write(vcpu, vmcs12->guest_rsp);
2601 kvm_rip_write(vcpu, vmcs12->guest_rip);
Vitaly Kuznetsovdc313382021-05-26 15:20:24 +02002602
2603 /*
2604 * It was observed that genuine Hyper-V running in L1 doesn't reset
2605 * 'hv_clean_fields' by itself, it only sets the corresponding dirty
2606 * bits when it changes a field in eVMCS. Mark all fields as clean
2607 * here.
2608 */
2609 if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
2610 vmx->nested.hv_evmcs->hv_clean_fields |=
2611 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
2612
Sean Christopherson55d23752018-12-03 13:53:18 -08002613 return 0;
2614}
2615
2616static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
2617{
Sean Christopherson5497b952019-07-11 08:58:29 -07002618 if (CC(!nested_cpu_has_nmi_exiting(vmcs12) &&
2619 nested_cpu_has_virtual_nmis(vmcs12)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002620 return -EINVAL;
2621
Sean Christopherson5497b952019-07-11 08:58:29 -07002622 if (CC(!nested_cpu_has_virtual_nmis(vmcs12) &&
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08002623 nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002624 return -EINVAL;
2625
2626 return 0;
2627}
2628
Sean Christophersonac6389a2020-03-02 18:02:38 -08002629static bool nested_vmx_check_eptp(struct kvm_vcpu *vcpu, u64 new_eptp)
Sean Christopherson55d23752018-12-03 13:53:18 -08002630{
2631 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08002632
2633 /* Check for memory type validity */
Sean Christophersonac6389a2020-03-02 18:02:38 -08002634 switch (new_eptp & VMX_EPTP_MT_MASK) {
Sean Christopherson55d23752018-12-03 13:53:18 -08002635 case VMX_EPTP_MT_UC:
Sean Christopherson5497b952019-07-11 08:58:29 -07002636 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002637 return false;
2638 break;
2639 case VMX_EPTP_MT_WB:
Sean Christopherson5497b952019-07-11 08:58:29 -07002640 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002641 return false;
2642 break;
2643 default:
2644 return false;
2645 }
2646
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08002647 /* Page-walk levels validity. */
Sean Christophersonac6389a2020-03-02 18:02:38 -08002648 switch (new_eptp & VMX_EPTP_PWL_MASK) {
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08002649 case VMX_EPTP_PWL_5:
2650 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_PAGE_WALK_5_BIT)))
2651 return false;
2652 break;
2653 case VMX_EPTP_PWL_4:
2654 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_PAGE_WALK_4_BIT)))
2655 return false;
2656 break;
2657 default:
Sean Christopherson55d23752018-12-03 13:53:18 -08002658 return false;
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08002659 }
Sean Christopherson55d23752018-12-03 13:53:18 -08002660
2661 /* Reserved bits should not be set */
Sean Christopherson636e8b72021-02-03 16:01:10 -08002662 if (CC(kvm_vcpu_is_illegal_gpa(vcpu, new_eptp) || ((new_eptp >> 7) & 0x1f)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002663 return false;
2664
2665 /* AD, if set, should be supported */
Sean Christophersonac6389a2020-03-02 18:02:38 -08002666 if (new_eptp & VMX_EPTP_AD_ENABLE_BIT) {
Sean Christopherson5497b952019-07-11 08:58:29 -07002667 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002668 return false;
2669 }
2670
2671 return true;
2672}
2673
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002674/*
2675 * Checks related to VM-Execution Control Fields
2676 */
2677static int nested_check_vm_execution_controls(struct kvm_vcpu *vcpu,
2678 struct vmcs12 *vmcs12)
2679{
2680 struct vcpu_vmx *vmx = to_vmx(vcpu);
2681
Sean Christopherson5497b952019-07-11 08:58:29 -07002682 if (CC(!vmx_control_verify(vmcs12->pin_based_vm_exec_control,
2683 vmx->nested.msrs.pinbased_ctls_low,
2684 vmx->nested.msrs.pinbased_ctls_high)) ||
2685 CC(!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
2686 vmx->nested.msrs.procbased_ctls_low,
2687 vmx->nested.msrs.procbased_ctls_high)))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002688 return -EINVAL;
2689
2690 if (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
Sean Christopherson5497b952019-07-11 08:58:29 -07002691 CC(!vmx_control_verify(vmcs12->secondary_vm_exec_control,
2692 vmx->nested.msrs.secondary_ctls_low,
2693 vmx->nested.msrs.secondary_ctls_high)))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002694 return -EINVAL;
2695
Sean Christopherson5497b952019-07-11 08:58:29 -07002696 if (CC(vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu)) ||
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002697 nested_vmx_check_io_bitmap_controls(vcpu, vmcs12) ||
2698 nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12) ||
2699 nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12) ||
2700 nested_vmx_check_apic_access_controls(vcpu, vmcs12) ||
2701 nested_vmx_check_apicv_controls(vcpu, vmcs12) ||
2702 nested_vmx_check_nmi_controls(vmcs12) ||
2703 nested_vmx_check_pml_controls(vcpu, vmcs12) ||
2704 nested_vmx_check_unrestricted_guest_controls(vcpu, vmcs12) ||
2705 nested_vmx_check_mode_based_ept_exec_controls(vcpu, vmcs12) ||
2706 nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12) ||
Sean Christopherson5497b952019-07-11 08:58:29 -07002707 CC(nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002708 return -EINVAL;
2709
Sean Christophersonbc441212019-02-12 16:42:23 -08002710 if (!nested_cpu_has_preemption_timer(vmcs12) &&
2711 nested_cpu_has_save_preemption_timer(vmcs12))
2712 return -EINVAL;
2713
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002714 if (nested_cpu_has_ept(vmcs12) &&
Sean Christophersonac6389a2020-03-02 18:02:38 -08002715 CC(!nested_vmx_check_eptp(vcpu, vmcs12->ept_pointer)))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002716 return -EINVAL;
2717
2718 if (nested_cpu_has_vmfunc(vmcs12)) {
Sean Christopherson5497b952019-07-11 08:58:29 -07002719 if (CC(vmcs12->vm_function_control &
2720 ~vmx->nested.msrs.vmfunc_controls))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002721 return -EINVAL;
2722
2723 if (nested_cpu_has_eptp_switching(vmcs12)) {
Sean Christopherson5497b952019-07-11 08:58:29 -07002724 if (CC(!nested_cpu_has_ept(vmcs12)) ||
2725 CC(!page_address_valid(vcpu, vmcs12->eptp_list_address)))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002726 return -EINVAL;
2727 }
2728 }
2729
2730 return 0;
2731}
2732
Krish Sadhukhan61446ba2018-12-12 13:30:09 -05002733/*
2734 * Checks related to VM-Exit Control Fields
2735 */
2736static int nested_check_vm_exit_controls(struct kvm_vcpu *vcpu,
2737 struct vmcs12 *vmcs12)
2738{
2739 struct vcpu_vmx *vmx = to_vmx(vcpu);
2740
Sean Christopherson5497b952019-07-11 08:58:29 -07002741 if (CC(!vmx_control_verify(vmcs12->vm_exit_controls,
2742 vmx->nested.msrs.exit_ctls_low,
2743 vmx->nested.msrs.exit_ctls_high)) ||
2744 CC(nested_vmx_check_exit_msr_switch_controls(vcpu, vmcs12)))
Krish Sadhukhan61446ba2018-12-12 13:30:09 -05002745 return -EINVAL;
2746
2747 return 0;
2748}
2749
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002750/*
2751 * Checks related to VM-Entry Control Fields
2752 */
2753static int nested_check_vm_entry_controls(struct kvm_vcpu *vcpu,
2754 struct vmcs12 *vmcs12)
Sean Christopherson55d23752018-12-03 13:53:18 -08002755{
2756 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08002757
Sean Christopherson5497b952019-07-11 08:58:29 -07002758 if (CC(!vmx_control_verify(vmcs12->vm_entry_controls,
2759 vmx->nested.msrs.entry_ctls_low,
2760 vmx->nested.msrs.entry_ctls_high)))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002761 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002762
2763 /*
2764 * From the Intel SDM, volume 3:
2765 * Fields relevant to VM-entry event injection must be set properly.
2766 * These fields are the VM-entry interruption-information field, the
2767 * VM-entry exception error code, and the VM-entry instruction length.
2768 */
2769 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
2770 u32 intr_info = vmcs12->vm_entry_intr_info_field;
2771 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
2772 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
2773 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
2774 bool should_have_error_code;
2775 bool urg = nested_cpu_has2(vmcs12,
2776 SECONDARY_EXEC_UNRESTRICTED_GUEST);
2777 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
2778
2779 /* VM-entry interruption-info field: interruption type */
Sean Christopherson5497b952019-07-11 08:58:29 -07002780 if (CC(intr_type == INTR_TYPE_RESERVED) ||
2781 CC(intr_type == INTR_TYPE_OTHER_EVENT &&
2782 !nested_cpu_supports_monitor_trap_flag(vcpu)))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002783 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002784
2785 /* VM-entry interruption-info field: vector */
Sean Christopherson5497b952019-07-11 08:58:29 -07002786 if (CC(intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
2787 CC(intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
2788 CC(intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002789 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002790
2791 /* VM-entry interruption-info field: deliver error code */
2792 should_have_error_code =
2793 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
2794 x86_exception_has_error_code(vector);
Sean Christopherson5497b952019-07-11 08:58:29 -07002795 if (CC(has_error_code != should_have_error_code))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002796 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002797
2798 /* VM-entry exception error code */
Sean Christopherson5497b952019-07-11 08:58:29 -07002799 if (CC(has_error_code &&
Sean Christopherson567926c2019-10-01 09:21:23 -07002800 vmcs12->vm_entry_exception_error_code & GENMASK(31, 16)))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002801 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002802
2803 /* VM-entry interruption-info field: reserved bits */
Sean Christopherson5497b952019-07-11 08:58:29 -07002804 if (CC(intr_info & INTR_INFO_RESVD_BITS_MASK))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002805 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002806
2807 /* VM-entry instruction length */
2808 switch (intr_type) {
2809 case INTR_TYPE_SOFT_EXCEPTION:
2810 case INTR_TYPE_SOFT_INTR:
2811 case INTR_TYPE_PRIV_SW_EXCEPTION:
Sean Christopherson5497b952019-07-11 08:58:29 -07002812 if (CC(vmcs12->vm_entry_instruction_len > 15) ||
2813 CC(vmcs12->vm_entry_instruction_len == 0 &&
2814 CC(!nested_cpu_has_zero_length_injection(vcpu))))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002815 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002816 }
2817 }
2818
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002819 if (nested_vmx_check_entry_msr_switch_controls(vcpu, vmcs12))
2820 return -EINVAL;
2821
2822 return 0;
2823}
2824
Sean Christopherson5478ba32019-04-11 12:18:06 -07002825static int nested_vmx_check_controls(struct kvm_vcpu *vcpu,
2826 struct vmcs12 *vmcs12)
2827{
2828 if (nested_check_vm_execution_controls(vcpu, vmcs12) ||
2829 nested_check_vm_exit_controls(vcpu, vmcs12) ||
2830 nested_check_vm_entry_controls(vcpu, vmcs12))
Paolo Bonzini98d9e852019-04-12 10:19:57 +02002831 return -EINVAL;
Sean Christopherson5478ba32019-04-11 12:18:06 -07002832
Vitaly Kuznetsova8350232020-02-05 13:30:34 +01002833 if (to_vmx(vcpu)->nested.enlightened_vmcs_enabled)
2834 return nested_evmcs_check_controls(vmcs12);
2835
Sean Christopherson5478ba32019-04-11 12:18:06 -07002836 return 0;
2837}
2838
Maxim Levitskyaf957ee2021-11-15 15:18:36 +02002839static int nested_vmx_check_address_space_size(struct kvm_vcpu *vcpu,
2840 struct vmcs12 *vmcs12)
2841{
2842#ifdef CONFIG_X86_64
2843 if (CC(!!(vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) !=
2844 !!(vcpu->arch.efer & EFER_LMA)))
2845 return -EINVAL;
2846#endif
2847 return 0;
2848}
2849
Paolo Bonzini98d9e852019-04-12 10:19:57 +02002850static int nested_vmx_check_host_state(struct kvm_vcpu *vcpu,
2851 struct vmcs12 *vmcs12)
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002852{
2853 bool ia32e;
2854
Sean Christopherson5497b952019-07-11 08:58:29 -07002855 if (CC(!nested_host_cr0_valid(vcpu, vmcs12->host_cr0)) ||
2856 CC(!nested_host_cr4_valid(vcpu, vmcs12->host_cr4)) ||
Sean Christopherson636e8b72021-02-03 16:01:10 -08002857 CC(kvm_vcpu_is_illegal_gpa(vcpu, vmcs12->host_cr3)))
Krish Sadhukhan254b2f32018-12-12 13:30:11 -05002858 return -EINVAL;
Krish Sadhukhan711eff32019-02-07 14:05:30 -05002859
Sean Christopherson5497b952019-07-11 08:58:29 -07002860 if (CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_esp, vcpu)) ||
2861 CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_eip, vcpu)))
Krish Sadhukhan711eff32019-02-07 14:05:30 -05002862 return -EINVAL;
2863
Krish Sadhukhanf6b0db1f2019-04-08 17:35:11 -04002864 if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) &&
Sean Christopherson5497b952019-07-11 08:58:29 -07002865 CC(!kvm_pat_valid(vmcs12->host_ia32_pat)))
Krish Sadhukhanf6b0db1f2019-04-08 17:35:11 -04002866 return -EINVAL;
2867
Oliver Uptonc547cb62019-11-13 16:17:17 -08002868 if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) &&
2869 CC(!kvm_valid_perf_global_ctrl(vcpu_to_pmu(vcpu),
2870 vmcs12->host_ia32_perf_global_ctrl)))
2871 return -EINVAL;
2872
Paolo Bonzinifd3edd42019-09-25 18:33:53 +02002873#ifdef CONFIG_X86_64
Maxim Levitskyaf957ee2021-11-15 15:18:36 +02002874 ia32e = !!(vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE);
Paolo Bonzinifd3edd42019-09-25 18:33:53 +02002875#else
2876 ia32e = false;
2877#endif
2878
2879 if (ia32e) {
Maxim Levitskyaf957ee2021-11-15 15:18:36 +02002880 if (CC(!(vmcs12->host_cr4 & X86_CR4_PAE)))
Paolo Bonzinifd3edd42019-09-25 18:33:53 +02002881 return -EINVAL;
2882 } else {
Maxim Levitskyaf957ee2021-11-15 15:18:36 +02002883 if (CC(vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) ||
Paolo Bonzinifd3edd42019-09-25 18:33:53 +02002884 CC(vmcs12->host_cr4 & X86_CR4_PCIDE) ||
2885 CC((vmcs12->host_rip) >> 32))
2886 return -EINVAL;
2887 }
Krish Sadhukhan1ef23e12019-07-03 19:54:35 -04002888
Sean Christopherson5497b952019-07-11 08:58:29 -07002889 if (CC(vmcs12->host_cs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2890 CC(vmcs12->host_ss_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2891 CC(vmcs12->host_ds_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2892 CC(vmcs12->host_es_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2893 CC(vmcs12->host_fs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2894 CC(vmcs12->host_gs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2895 CC(vmcs12->host_tr_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2896 CC(vmcs12->host_cs_selector == 0) ||
2897 CC(vmcs12->host_tr_selector == 0) ||
2898 CC(vmcs12->host_ss_selector == 0 && !ia32e))
Krish Sadhukhan1ef23e12019-07-03 19:54:35 -04002899 return -EINVAL;
2900
Sean Christopherson5497b952019-07-11 08:58:29 -07002901 if (CC(is_noncanonical_address(vmcs12->host_fs_base, vcpu)) ||
2902 CC(is_noncanonical_address(vmcs12->host_gs_base, vcpu)) ||
2903 CC(is_noncanonical_address(vmcs12->host_gdtr_base, vcpu)) ||
2904 CC(is_noncanonical_address(vmcs12->host_idtr_base, vcpu)) ||
Paolo Bonzinifd3edd42019-09-25 18:33:53 +02002905 CC(is_noncanonical_address(vmcs12->host_tr_base, vcpu)) ||
2906 CC(is_noncanonical_address(vmcs12->host_rip, vcpu)))
Krish Sadhukhan58450382019-08-09 12:26:19 -07002907 return -EINVAL;
Krish Sadhukhan1ef23e12019-07-03 19:54:35 -04002908
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002909 /*
2910 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
2911 * IA32_EFER MSR must be 0 in the field for that register. In addition,
2912 * the values of the LMA and LME bits in the field must each be that of
2913 * the host address-space size VM-exit control.
2914 */
2915 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
Sean Christopherson5497b952019-07-11 08:58:29 -07002916 if (CC(!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer)) ||
2917 CC(ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA)) ||
2918 CC(ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)))
Krish Sadhukhan254b2f32018-12-12 13:30:11 -05002919 return -EINVAL;
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002920 }
2921
Sean Christopherson55d23752018-12-03 13:53:18 -08002922 return 0;
2923}
2924
2925static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
2926 struct vmcs12 *vmcs12)
2927{
David Woodhouse7d0172b2021-11-15 16:50:25 +00002928 struct vcpu_vmx *vmx = to_vmx(vcpu);
2929 struct gfn_to_hva_cache *ghc = &vmx->nested.shadow_vmcs12_cache;
2930 struct vmcs_hdr hdr;
Sean Christopherson55d23752018-12-03 13:53:18 -08002931
Yu Zhang64c78502021-09-30 01:51:53 +08002932 if (vmcs12->vmcs_link_pointer == INVALID_GPA)
Sean Christopherson55d23752018-12-03 13:53:18 -08002933 return 0;
2934
Sean Christopherson5497b952019-07-11 08:58:29 -07002935 if (CC(!page_address_valid(vcpu, vmcs12->vmcs_link_pointer)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002936 return -EINVAL;
2937
David Woodhouse7d0172b2021-11-15 16:50:25 +00002938 if (ghc->gpa != vmcs12->vmcs_link_pointer &&
2939 CC(kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc,
2940 vmcs12->vmcs_link_pointer, VMCS12_SIZE)))
2941 return -EINVAL;
2942
2943 if (CC(kvm_read_guest_offset_cached(vcpu->kvm, ghc, &hdr,
2944 offsetof(struct vmcs12, hdr),
2945 sizeof(hdr))))
Sean Christopherson55d23752018-12-03 13:53:18 -08002946 return -EINVAL;
2947
David Woodhouse7d0172b2021-11-15 16:50:25 +00002948 if (CC(hdr.revision_id != VMCS12_REVISION) ||
2949 CC(hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12)))
2950 return -EINVAL;
KarimAllah Ahmed88925302019-01-31 21:24:41 +01002951
David Woodhouse7d0172b2021-11-15 16:50:25 +00002952 return 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08002953}
2954
Sean Christopherson55d23752018-12-03 13:53:18 -08002955/*
2956 * Checks related to Guest Non-register State
2957 */
2958static int nested_check_guest_non_reg_state(struct vmcs12 *vmcs12)
2959{
Sean Christopherson5497b952019-07-11 08:58:29 -07002960 if (CC(vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
Yadong Qibf0cd882020-11-06 14:51:22 +08002961 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT &&
2962 vmcs12->guest_activity_state != GUEST_ACTIVITY_WAIT_SIPI))
Sean Christopherson55d23752018-12-03 13:53:18 -08002963 return -EINVAL;
2964
2965 return 0;
2966}
2967
Sean Christopherson5478ba32019-04-11 12:18:06 -07002968static int nested_vmx_check_guest_state(struct kvm_vcpu *vcpu,
2969 struct vmcs12 *vmcs12,
Sean Christopherson68cda402020-05-11 15:05:29 -07002970 enum vm_entry_failure_code *entry_failure_code)
Sean Christopherson55d23752018-12-03 13:53:18 -08002971{
2972 bool ia32e;
2973
Sean Christopherson68cda402020-05-11 15:05:29 -07002974 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson55d23752018-12-03 13:53:18 -08002975
Sean Christopherson5497b952019-07-11 08:58:29 -07002976 if (CC(!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0)) ||
2977 CC(!nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4)))
Sean Christophersonc80add02019-04-11 12:18:09 -07002978 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002979
Krish Sadhukhanb91991b2020-01-15 19:54:32 -05002980 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) &&
2981 CC(!kvm_dr7_valid(vmcs12->guest_dr7)))
2982 return -EINVAL;
2983
Krish Sadhukhande2bc2b2019-04-08 17:35:12 -04002984 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) &&
Sean Christopherson5497b952019-07-11 08:58:29 -07002985 CC(!kvm_pat_valid(vmcs12->guest_ia32_pat)))
Sean Christophersonc80add02019-04-11 12:18:09 -07002986 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002987
2988 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
Sean Christopherson68cda402020-05-11 15:05:29 -07002989 *entry_failure_code = ENTRY_FAIL_VMCS_LINK_PTR;
Sean Christophersonc80add02019-04-11 12:18:09 -07002990 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002991 }
2992
Oliver Uptonbfc6ad62019-11-13 16:17:16 -08002993 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) &&
2994 CC(!kvm_valid_perf_global_ctrl(vcpu_to_pmu(vcpu),
2995 vmcs12->guest_ia32_perf_global_ctrl)))
2996 return -EINVAL;
2997
Sean Christopherson55d23752018-12-03 13:53:18 -08002998 /*
2999 * If the load IA32_EFER VM-entry control is 1, the following checks
3000 * are performed on the field for the IA32_EFER MSR:
3001 * - Bits reserved in the IA32_EFER MSR must be 0.
3002 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
3003 * the IA-32e mode guest VM-exit control. It must also be identical
3004 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
3005 * CR0.PG) is 1.
3006 */
3007 if (to_vmx(vcpu)->nested.nested_run_pending &&
3008 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
3009 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
Sean Christopherson5497b952019-07-11 08:58:29 -07003010 if (CC(!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer)) ||
3011 CC(ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA)) ||
3012 CC(((vmcs12->guest_cr0 & X86_CR0_PG) &&
3013 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))))
Sean Christophersonc80add02019-04-11 12:18:09 -07003014 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08003015 }
3016
3017 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
Sean Christopherson5497b952019-07-11 08:58:29 -07003018 (CC(is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu)) ||
3019 CC((vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD))))
Sean Christophersonc80add02019-04-11 12:18:09 -07003020 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08003021
Sean Christopherson9c3e9222019-04-11 12:18:05 -07003022 if (nested_check_guest_non_reg_state(vmcs12))
Sean Christophersonc80add02019-04-11 12:18:09 -07003023 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08003024
3025 return 0;
3026}
3027
Sean Christopherson453eafb2018-12-20 12:25:17 -08003028static int nested_vmx_check_vmentry_hw(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08003029{
3030 struct vcpu_vmx *vmx = to_vmx(vcpu);
3031 unsigned long cr3, cr4;
Sean Christophersonf1727b42019-01-25 07:40:58 -08003032 bool vm_fail;
Sean Christopherson55d23752018-12-03 13:53:18 -08003033
3034 if (!nested_early_check)
3035 return 0;
3036
3037 if (vmx->msr_autoload.host.nr)
3038 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
3039 if (vmx->msr_autoload.guest.nr)
3040 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
3041
3042 preempt_disable();
3043
3044 vmx_prepare_switch_to_guest(vcpu);
3045
3046 /*
3047 * Induce a consistency check VMExit by clearing bit 1 in GUEST_RFLAGS,
3048 * which is reserved to '1' by hardware. GUEST_RFLAGS is guaranteed to
Miaohe Lin49f933d2020-02-27 11:20:54 +08003049 * be written (by prepare_vmcs02()) before the "real" VMEnter, i.e.
Sean Christopherson55d23752018-12-03 13:53:18 -08003050 * there is no need to preserve other bits or save/restore the field.
3051 */
3052 vmcs_writel(GUEST_RFLAGS, 0);
3053
Sean Christopherson55d23752018-12-03 13:53:18 -08003054 cr3 = __get_current_cr3_fast();
3055 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
3056 vmcs_writel(HOST_CR3, cr3);
3057 vmx->loaded_vmcs->host_state.cr3 = cr3;
3058 }
3059
3060 cr4 = cr4_read_shadow();
3061 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
3062 vmcs_writel(HOST_CR4, cr4);
3063 vmx->loaded_vmcs->host_state.cr4 = cr4;
3064 }
3065
Uros Bizjak150f17b2020-12-30 16:26:57 -08003066 vm_fail = __vmx_vcpu_run(vmx, (unsigned long *)&vcpu->arch.regs,
3067 vmx->loaded_vmcs->launched);
Sean Christopherson55d23752018-12-03 13:53:18 -08003068
Sean Christopherson55d23752018-12-03 13:53:18 -08003069 if (vmx->msr_autoload.host.nr)
3070 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
3071 if (vmx->msr_autoload.guest.nr)
3072 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
3073
Sean Christophersonf1727b42019-01-25 07:40:58 -08003074 if (vm_fail) {
Sean Christopherson380e0052019-07-11 08:58:30 -07003075 u32 error = vmcs_read32(VM_INSTRUCTION_ERROR);
3076
Wanpeng Li541e8862019-05-17 16:49:50 +08003077 preempt_enable();
Sean Christopherson380e0052019-07-11 08:58:30 -07003078
3079 trace_kvm_nested_vmenter_failed(
3080 "early hardware check VM-instruction error: ", error);
3081 WARN_ON_ONCE(error != VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Sean Christopherson55d23752018-12-03 13:53:18 -08003082 return 1;
3083 }
3084
3085 /*
3086 * VMExit clears RFLAGS.IF and DR7, even on a consistency check.
3087 */
Sean Christopherson55d23752018-12-03 13:53:18 -08003088 if (hw_breakpoint_active())
3089 set_debugreg(__this_cpu_read(cpu_dr7), 7);
Peter Zijlstra84b6a342020-05-29 23:27:36 +02003090 local_irq_enable();
Wanpeng Li541e8862019-05-17 16:49:50 +08003091 preempt_enable();
Sean Christopherson55d23752018-12-03 13:53:18 -08003092
3093 /*
3094 * A non-failing VMEntry means we somehow entered guest mode with
3095 * an illegal RIP, and that's just the tip of the iceberg. There
3096 * is no telling what memory has been modified or what state has
3097 * been exposed to unknown code. Hitting this all but guarantees
3098 * a (very critical) hardware issue.
3099 */
3100 WARN_ON(!(vmcs_read32(VM_EXIT_REASON) &
3101 VMX_EXIT_REASONS_FAILED_VMENTRY));
3102
3103 return 0;
3104}
Sean Christopherson55d23752018-12-03 13:53:18 -08003105
Paolo Bonzini9a78e152021-01-08 11:43:08 -05003106static bool nested_get_evmcs_page(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08003107{
Sean Christopherson55d23752018-12-03 13:53:18 -08003108 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08003109
Vitaly Kuznetsove942dbf2020-03-09 16:52:12 +01003110 /*
3111 * hv_evmcs may end up being not mapped after migration (when
3112 * L2 was running), map it here to make sure vmcs12 changes are
3113 * properly reflected.
3114 */
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02003115 if (vmx->nested.enlightened_vmcs_enabled &&
Vitaly Kuznetsov27849962021-05-26 15:20:20 +02003116 vmx->nested.hv_evmcs_vmptr == EVMPTR_MAP_PENDING) {
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003117 enum nested_evmptrld_status evmptrld_status =
3118 nested_vmx_handle_enlightened_vmptrld(vcpu, false);
3119
3120 if (evmptrld_status == EVMPTRLD_VMFAIL ||
Vitaly Kuznetsovf5c7e842021-05-03 17:08:51 +02003121 evmptrld_status == EVMPTRLD_ERROR)
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003122 return false;
Vitaly Kuznetsov8629b622021-05-26 15:20:25 +02003123
3124 /*
3125 * Post migration VMCS12 always provides the most actual
3126 * information, copy it to eVMCS upon entry.
3127 */
3128 vmx->nested.need_vmcs12_to_shadow_sync = true;
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003129 }
Vitaly Kuznetsove942dbf2020-03-09 16:52:12 +01003130
Paolo Bonzini9a78e152021-01-08 11:43:08 -05003131 return true;
3132}
3133
3134static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
3135{
3136 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3137 struct vcpu_vmx *vmx = to_vmx(vcpu);
3138 struct kvm_host_map *map;
3139 struct page *page;
3140 u64 hpa;
3141
Maxim Levitsky158a48e2021-06-07 12:02:03 +03003142 if (!vcpu->arch.pdptrs_from_userspace &&
3143 !nested_cpu_has_ept(vmcs12) && is_pae_paging(vcpu)) {
Maxim Levitsky0f857222021-06-07 12:02:00 +03003144 /*
3145 * Reload the guest's PDPTRs since after a migration
3146 * the guest CR3 might be restored prior to setting the nested
3147 * state which can lead to a load of wrong PDPTRs.
3148 */
3149 if (CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, vcpu->arch.cr3)))
3150 return false;
3151 }
3152
3153
Sean Christopherson55d23752018-12-03 13:53:18 -08003154 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
3155 /*
3156 * Translate L1 physical address to host physical
3157 * address for vmcs02. Keep the page pinned, so this
3158 * physical address remains valid. We keep a reference
3159 * to it so we can release it later.
3160 */
3161 if (vmx->nested.apic_access_page) { /* shouldn't happen */
Liran Alonb11494b2019-11-21 00:31:47 +02003162 kvm_release_page_clean(vmx->nested.apic_access_page);
Sean Christopherson55d23752018-12-03 13:53:18 -08003163 vmx->nested.apic_access_page = NULL;
3164 }
3165 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Sean Christopherson55d23752018-12-03 13:53:18 -08003166 if (!is_error_page(page)) {
3167 vmx->nested.apic_access_page = page;
3168 hpa = page_to_phys(vmx->nested.apic_access_page);
3169 vmcs_write64(APIC_ACCESS_ADDR, hpa);
3170 } else {
Jim Mattson671ddc72019-10-15 10:44:05 -07003171 pr_debug_ratelimited("%s: no backing 'struct page' for APIC-access address in vmcs12\n",
3172 __func__);
3173 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3174 vcpu->run->internal.suberror =
3175 KVM_INTERNAL_ERROR_EMULATION;
3176 vcpu->run->internal.ndata = 0;
3177 return false;
Sean Christopherson55d23752018-12-03 13:53:18 -08003178 }
3179 }
3180
3181 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003182 map = &vmx->nested.virtual_apic_map;
Sean Christopherson55d23752018-12-03 13:53:18 -08003183
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003184 if (!kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->virtual_apic_page_addr), map)) {
3185 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, pfn_to_hpa(map->pfn));
Paolo Bonzini69090812019-04-15 15:16:17 +02003186 } else if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING) &&
3187 nested_cpu_has(vmcs12, CPU_BASED_CR8_STORE_EXITING) &&
3188 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
3189 /*
3190 * The processor will never use the TPR shadow, simply
3191 * clear the bit from the execution control. Such a
3192 * configuration is useless, but it happens in tests.
3193 * For any other configuration, failing the vm entry is
3194 * _not_ what the processor does but it's basically the
3195 * only possibility we have.
3196 */
Sean Christopherson2183f562019-05-07 12:17:56 -07003197 exec_controls_clearbit(vmx, CPU_BASED_TPR_SHADOW);
Paolo Bonzini69090812019-04-15 15:16:17 +02003198 } else {
Sean Christophersonca2f5462019-05-07 09:06:33 -07003199 /*
3200 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR to
3201 * force VM-Entry to fail.
3202 */
Yu Zhang64c78502021-09-30 01:51:53 +08003203 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, INVALID_GPA);
Sean Christopherson55d23752018-12-03 13:53:18 -08003204 }
3205 }
3206
3207 if (nested_cpu_has_posted_intr(vmcs12)) {
KarimAllah Ahmed3278e042019-01-31 21:24:38 +01003208 map = &vmx->nested.pi_desc_map;
3209
3210 if (!kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->posted_intr_desc_addr), map)) {
3211 vmx->nested.pi_desc =
3212 (struct pi_desc *)(((void *)map->hva) +
3213 offset_in_page(vmcs12->posted_intr_desc_addr));
3214 vmcs_write64(POSTED_INTR_DESC_ADDR,
3215 pfn_to_hpa(map->pfn) + offset_in_page(vmcs12->posted_intr_desc_addr));
Jim Mattson966eefb2021-06-04 10:26:06 -07003216 } else {
3217 /*
3218 * Defer the KVM_INTERNAL_EXIT until KVM tries to
3219 * access the contents of the VMCS12 posted interrupt
3220 * descriptor. (Note that KVM may do this when it
3221 * should not, per the architectural specification.)
3222 */
3223 vmx->nested.pi_desc = NULL;
3224 pin_controls_clearbit(vmx, PIN_BASED_POSTED_INTR);
Sean Christopherson55d23752018-12-03 13:53:18 -08003225 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003226 }
3227 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
Sean Christopherson2183f562019-05-07 12:17:56 -07003228 exec_controls_setbit(vmx, CPU_BASED_USE_MSR_BITMAPS);
Sean Christopherson55d23752018-12-03 13:53:18 -08003229 else
Sean Christopherson2183f562019-05-07 12:17:56 -07003230 exec_controls_clearbit(vmx, CPU_BASED_USE_MSR_BITMAPS);
Paolo Bonzini9a78e152021-01-08 11:43:08 -05003231
3232 return true;
3233}
3234
3235static bool vmx_get_nested_state_pages(struct kvm_vcpu *vcpu)
3236{
Vitaly Kuznetsovf5c7e842021-05-03 17:08:51 +02003237 if (!nested_get_evmcs_page(vcpu)) {
3238 pr_debug_ratelimited("%s: enlightened vmptrld failed\n",
3239 __func__);
3240 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3241 vcpu->run->internal.suberror =
3242 KVM_INTERNAL_ERROR_EMULATION;
3243 vcpu->run->internal.ndata = 0;
3244
Paolo Bonzini9a78e152021-01-08 11:43:08 -05003245 return false;
Vitaly Kuznetsovf5c7e842021-05-03 17:08:51 +02003246 }
Paolo Bonzini9a78e152021-01-08 11:43:08 -05003247
3248 if (is_guest_mode(vcpu) && !nested_get_vmcs12_pages(vcpu))
3249 return false;
3250
Jim Mattson671ddc72019-10-15 10:44:05 -07003251 return true;
Sean Christopherson55d23752018-12-03 13:53:18 -08003252}
3253
Sean Christopherson02f5fb22020-06-22 14:58:32 -07003254static int nested_vmx_write_pml_buffer(struct kvm_vcpu *vcpu, gpa_t gpa)
3255{
3256 struct vmcs12 *vmcs12;
3257 struct vcpu_vmx *vmx = to_vmx(vcpu);
3258 gpa_t dst;
3259
3260 if (WARN_ON_ONCE(!is_guest_mode(vcpu)))
3261 return 0;
3262
3263 if (WARN_ON_ONCE(vmx->nested.pml_full))
3264 return 1;
3265
3266 /*
3267 * Check if PML is enabled for the nested guest. Whether eptp bit 6 is
3268 * set is already checked as part of A/D emulation.
3269 */
3270 vmcs12 = get_vmcs12(vcpu);
3271 if (!nested_cpu_has_pml(vmcs12))
3272 return 0;
3273
3274 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
3275 vmx->nested.pml_full = true;
3276 return 1;
3277 }
3278
3279 gpa &= ~0xFFFull;
3280 dst = vmcs12->pml_address + sizeof(u64) * vmcs12->guest_pml_index;
3281
3282 if (kvm_write_guest_page(vcpu->kvm, gpa_to_gfn(dst), &gpa,
3283 offset_in_page(dst), sizeof(gpa)))
3284 return 0;
3285
3286 vmcs12->guest_pml_index--;
3287
3288 return 0;
3289}
3290
Sean Christopherson55d23752018-12-03 13:53:18 -08003291/*
3292 * Intel's VMX Instruction Reference specifies a common set of prerequisites
3293 * for running VMX instructions (except VMXON, whose prerequisites are
3294 * slightly different). It also specifies what exception to inject otherwise.
3295 * Note that many of these exceptions have priority over VM exits, so they
3296 * don't have to be checked again here.
3297 */
3298static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
3299{
3300 if (!to_vmx(vcpu)->nested.vmxon) {
3301 kvm_queue_exception(vcpu, UD_VECTOR);
3302 return 0;
3303 }
3304
3305 if (vmx_get_cpl(vcpu)) {
3306 kvm_inject_gp(vcpu, 0);
3307 return 0;
3308 }
3309
3310 return 1;
3311}
3312
3313static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu)
3314{
3315 u8 rvi = vmx_get_rvi();
3316 u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI);
3317
3318 return ((rvi & 0xf0) > (vppr & 0xf0));
3319}
3320
3321static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
3322 struct vmcs12 *vmcs12);
3323
3324/*
3325 * If from_vmentry is false, this is being called from state restore (either RSM
3326 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
Jim Mattson671ddc72019-10-15 10:44:05 -07003327 *
3328 * Returns:
Miaohe Lin463bfee2020-02-14 10:44:05 +08003329 * NVMX_VMENTRY_SUCCESS: Entered VMX non-root mode
3330 * NVMX_VMENTRY_VMFAIL: Consistency check VMFail
3331 * NVMX_VMENTRY_VMEXIT: Consistency check VMExit
3332 * NVMX_VMENTRY_KVM_INTERNAL_ERROR: KVM internal error
Sean Christopherson55d23752018-12-03 13:53:18 -08003333 */
Jim Mattson671ddc72019-10-15 10:44:05 -07003334enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu,
3335 bool from_vmentry)
Sean Christopherson55d23752018-12-03 13:53:18 -08003336{
3337 struct vcpu_vmx *vmx = to_vmx(vcpu);
3338 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Sean Christopherson68cda402020-05-11 15:05:29 -07003339 enum vm_entry_failure_code entry_failure_code;
Sean Christopherson55d23752018-12-03 13:53:18 -08003340 bool evaluate_pending_interrupts;
Sean Christopherson8e533242020-11-06 17:03:12 +08003341 union vmx_exit_reason exit_reason = {
3342 .basic = EXIT_REASON_INVALID_STATE,
3343 .failed_vmentry = 1,
3344 };
3345 u32 failed_index;
Sean Christopherson55d23752018-12-03 13:53:18 -08003346
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07003347 if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu))
3348 kvm_vcpu_flush_tlb_current(vcpu);
3349
Sean Christopherson2183f562019-05-07 12:17:56 -07003350 evaluate_pending_interrupts = exec_controls_get(vmx) &
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08003351 (CPU_BASED_INTR_WINDOW_EXITING | CPU_BASED_NMI_WINDOW_EXITING);
Sean Christopherson55d23752018-12-03 13:53:18 -08003352 if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu))
3353 evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu);
3354
3355 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
3356 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
3357 if (kvm_mpx_supported() &&
3358 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
3359 vmx->nested.vmcs01_guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
3360
Sean Christophersonf087a022019-06-07 11:55:34 -07003361 /*
3362 * Overwrite vmcs01.GUEST_CR3 with L1's CR3 if EPT is disabled *and*
3363 * nested early checks are disabled. In the event of a "late" VM-Fail,
3364 * i.e. a VM-Fail detected by hardware but not KVM, KVM must unwind its
3365 * software model to the pre-VMEntry host state. When EPT is disabled,
3366 * GUEST_CR3 holds KVM's shadow CR3, not L1's "real" CR3, which causes
3367 * nested_vmx_restore_host_state() to corrupt vcpu->arch.cr3. Stuffing
3368 * vmcs01.GUEST_CR3 results in the unwind naturally setting arch.cr3 to
3369 * the correct value. Smashing vmcs01.GUEST_CR3 is safe because nested
3370 * VM-Exits, and the unwind, reset KVM's MMU, i.e. vmcs01.GUEST_CR3 is
3371 * guaranteed to be overwritten with a shadow CR3 prior to re-entering
3372 * L1. Don't stuff vmcs01.GUEST_CR3 when using nested early checks as
3373 * KVM modifies vcpu->arch.cr3 if and only if the early hardware checks
3374 * pass, and early VM-Fails do not reset KVM's MMU, i.e. the VM-Fail
3375 * path would need to manually save/restore vmcs01.GUEST_CR3.
3376 */
3377 if (!enable_ept && !nested_early_check)
3378 vmcs_writel(GUEST_CR3, vcpu->arch.cr3);
3379
Sean Christopherson55d23752018-12-03 13:53:18 -08003380 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
3381
Sean Christopherson389ab252021-08-10 10:19:50 -07003382 prepare_vmcs02_early(vmx, &vmx->vmcs01, vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08003383
3384 if (from_vmentry) {
Sean Christophersonb89d5ad2020-09-23 11:44:47 -07003385 if (unlikely(!nested_get_vmcs12_pages(vcpu))) {
3386 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson671ddc72019-10-15 10:44:05 -07003387 return NVMX_VMENTRY_KVM_INTERNAL_ERROR;
Sean Christophersonb89d5ad2020-09-23 11:44:47 -07003388 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003389
3390 if (nested_vmx_check_vmentry_hw(vcpu)) {
3391 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson671ddc72019-10-15 10:44:05 -07003392 return NVMX_VMENTRY_VMFAIL;
Sean Christopherson55d23752018-12-03 13:53:18 -08003393 }
3394
Sean Christopherson68cda402020-05-11 15:05:29 -07003395 if (nested_vmx_check_guest_state(vcpu, vmcs12,
3396 &entry_failure_code)) {
Sean Christopherson8e533242020-11-06 17:03:12 +08003397 exit_reason.basic = EXIT_REASON_INVALID_STATE;
Sean Christopherson68cda402020-05-11 15:05:29 -07003398 vmcs12->exit_qualification = entry_failure_code;
Sean Christopherson55d23752018-12-03 13:53:18 -08003399 goto vmentry_fail_vmexit;
Sean Christopherson68cda402020-05-11 15:05:29 -07003400 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003401 }
3402
3403 enter_guest_mode(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08003404
Maxim Levitsky0f857222021-06-07 12:02:00 +03003405 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &entry_failure_code)) {
Sean Christopherson8e533242020-11-06 17:03:12 +08003406 exit_reason.basic = EXIT_REASON_INVALID_STATE;
Sean Christopherson68cda402020-05-11 15:05:29 -07003407 vmcs12->exit_qualification = entry_failure_code;
Sean Christopherson55d23752018-12-03 13:53:18 -08003408 goto vmentry_fail_vmexit_guest_mode;
Sean Christopherson68cda402020-05-11 15:05:29 -07003409 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003410
3411 if (from_vmentry) {
Sean Christopherson68cda402020-05-11 15:05:29 -07003412 failed_index = nested_vmx_load_msr(vcpu,
3413 vmcs12->vm_entry_msr_load_addr,
3414 vmcs12->vm_entry_msr_load_count);
3415 if (failed_index) {
Sean Christopherson8e533242020-11-06 17:03:12 +08003416 exit_reason.basic = EXIT_REASON_MSR_LOAD_FAIL;
Sean Christopherson68cda402020-05-11 15:05:29 -07003417 vmcs12->exit_qualification = failed_index;
Sean Christopherson55d23752018-12-03 13:53:18 -08003418 goto vmentry_fail_vmexit_guest_mode;
Sean Christopherson68cda402020-05-11 15:05:29 -07003419 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003420 } else {
3421 /*
3422 * The MMU is not initialized to point at the right entities yet and
3423 * "get pages" would need to read data from the guest (i.e. we will
3424 * need to perform gpa to hpa translation). Request a call
3425 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
3426 * have already been set at vmentry time and should not be reset.
3427 */
Paolo Bonzini729c15c2020-09-22 06:53:57 -04003428 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08003429 }
3430
3431 /*
3432 * If L1 had a pending IRQ/NMI until it executed
3433 * VMLAUNCH/VMRESUME which wasn't delivered because it was
3434 * disallowed (e.g. interrupts disabled), L0 needs to
3435 * evaluate if this pending event should cause an exit from L2
3436 * to L1 or delivered directly to L2 (e.g. In case L1 don't
3437 * intercept EXTERNAL_INTERRUPT).
3438 *
3439 * Usually this would be handled by the processor noticing an
3440 * IRQ/NMI window request, or checking RVI during evaluation of
3441 * pending virtual interrupts. However, this setting was done
3442 * on VMCS01 and now VMCS02 is active instead. Thus, we force L0
3443 * to perform pending event evaluation by requesting a KVM_REQ_EVENT.
3444 */
3445 if (unlikely(evaluate_pending_interrupts))
3446 kvm_make_request(KVM_REQ_EVENT, vcpu);
3447
3448 /*
Paolo Bonzini359a6c32019-01-29 19:14:46 +01003449 * Do not start the preemption timer hrtimer until after we know
3450 * we are successful, so that only nested_vmx_vmexit needs to cancel
3451 * the timer.
3452 */
3453 vmx->nested.preemption_timer_expired = false;
Peter Shier850448f2020-05-26 14:51:06 -07003454 if (nested_cpu_has_preemption_timer(vmcs12)) {
3455 u64 timer_value = vmx_calc_preemption_timer_value(vcpu);
3456 vmx_start_preemption_timer(vcpu, timer_value);
3457 }
Paolo Bonzini359a6c32019-01-29 19:14:46 +01003458
3459 /*
Sean Christopherson55d23752018-12-03 13:53:18 -08003460 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
3461 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
3462 * returned as far as L1 is concerned. It will only return (and set
3463 * the success flag) when L2 exits (see nested_vmx_vmexit()).
3464 */
Jim Mattson671ddc72019-10-15 10:44:05 -07003465 return NVMX_VMENTRY_SUCCESS;
Sean Christopherson55d23752018-12-03 13:53:18 -08003466
3467 /*
3468 * A failed consistency check that leads to a VMExit during L1's
3469 * VMEnter to L2 is a variation of a normal VMexit, as explained in
3470 * 26.7 "VM-entry failures during or after loading guest state".
3471 */
3472vmentry_fail_vmexit_guest_mode:
Xiaoyao Li5e3d3942019-12-06 16:45:26 +08003473 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING)
Sean Christopherson55d23752018-12-03 13:53:18 -08003474 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
3475 leave_guest_mode(vcpu);
3476
3477vmentry_fail_vmexit:
3478 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
3479
3480 if (!from_vmentry)
Jim Mattson671ddc72019-10-15 10:44:05 -07003481 return NVMX_VMENTRY_VMEXIT;
Sean Christopherson55d23752018-12-03 13:53:18 -08003482
3483 load_vmcs12_host_state(vcpu, vmcs12);
Sean Christopherson8e533242020-11-06 17:03:12 +08003484 vmcs12->vm_exit_reason = exit_reason.full;
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02003485 if (enable_shadow_vmcs || evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
Sean Christopherson3731905ef2019-05-07 08:36:27 -07003486 vmx->nested.need_vmcs12_to_shadow_sync = true;
Jim Mattson671ddc72019-10-15 10:44:05 -07003487 return NVMX_VMENTRY_VMEXIT;
Sean Christopherson55d23752018-12-03 13:53:18 -08003488}
3489
3490/*
3491 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
3492 * for running an L2 nested guest.
3493 */
3494static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
3495{
3496 struct vmcs12 *vmcs12;
Jim Mattson671ddc72019-10-15 10:44:05 -07003497 enum nvmx_vmentry_status status;
Sean Christopherson55d23752018-12-03 13:53:18 -08003498 struct vcpu_vmx *vmx = to_vmx(vcpu);
3499 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003500 enum nested_evmptrld_status evmptrld_status;
Sean Christopherson55d23752018-12-03 13:53:18 -08003501
3502 if (!nested_vmx_check_permission(vcpu))
3503 return 1;
3504
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003505 evmptrld_status = nested_vmx_handle_enlightened_vmptrld(vcpu, launch);
3506 if (evmptrld_status == EVMPTRLD_ERROR) {
3507 kvm_queue_exception(vcpu, UD_VECTOR);
Sean Christopherson55d23752018-12-03 13:53:18 -08003508 return 1;
Sean Christophersonfc595f32020-08-12 11:06:15 -07003509 } else if (CC(evmptrld_status == EVMPTRLD_VMFAIL)) {
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003510 return nested_vmx_failInvalid(vcpu);
3511 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003512
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02003513 if (CC(!evmptr_is_valid(vmx->nested.hv_evmcs_vmptr) &&
Yu Zhang64c78502021-09-30 01:51:53 +08003514 vmx->nested.current_vmptr == INVALID_GPA))
Sean Christopherson55d23752018-12-03 13:53:18 -08003515 return nested_vmx_failInvalid(vcpu);
3516
3517 vmcs12 = get_vmcs12(vcpu);
3518
3519 /*
3520 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
3521 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
3522 * rather than RFLAGS.ZF, and no error number is stored to the
3523 * VM-instruction error field.
3524 */
Sean Christophersonfc595f32020-08-12 11:06:15 -07003525 if (CC(vmcs12->hdr.shadow_vmcs))
Sean Christopherson55d23752018-12-03 13:53:18 -08003526 return nested_vmx_failInvalid(vcpu);
3527
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02003528 if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) {
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02003529 copy_enlightened_to_vmcs12(vmx, vmx->nested.hv_evmcs->hv_clean_fields);
Sean Christopherson55d23752018-12-03 13:53:18 -08003530 /* Enlightened VMCS doesn't have launch state */
3531 vmcs12->launch_state = !launch;
3532 } else if (enable_shadow_vmcs) {
3533 copy_shadow_to_vmcs12(vmx);
3534 }
3535
3536 /*
3537 * The nested entry process starts with enforcing various prerequisites
3538 * on vmcs12 as required by the Intel SDM, and act appropriately when
3539 * they fail: As the SDM explains, some conditions should cause the
3540 * instruction to fail, while others will cause the instruction to seem
3541 * to succeed, but return an EXIT_REASON_INVALID_STATE.
3542 * To speed up the normal (success) code path, we should avoid checking
3543 * for misconfigurations which will anyway be caught by the processor
3544 * when using the merged vmcs02.
3545 */
Sean Christophersonfc595f32020-08-12 11:06:15 -07003546 if (CC(interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS))
Sean Christophersonb2656e42020-06-08 18:56:07 -07003547 return nested_vmx_fail(vcpu, VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
Sean Christopherson55d23752018-12-03 13:53:18 -08003548
Sean Christophersonfc595f32020-08-12 11:06:15 -07003549 if (CC(vmcs12->launch_state == launch))
Sean Christophersonb2656e42020-06-08 18:56:07 -07003550 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08003551 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
3552 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
3553
Paolo Bonzini98d9e852019-04-12 10:19:57 +02003554 if (nested_vmx_check_controls(vcpu, vmcs12))
Sean Christophersonb2656e42020-06-08 18:56:07 -07003555 return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Sean Christopherson5478ba32019-04-11 12:18:06 -07003556
Maxim Levitskyaf957ee2021-11-15 15:18:36 +02003557 if (nested_vmx_check_address_space_size(vcpu, vmcs12))
3558 return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
3559
Paolo Bonzini98d9e852019-04-12 10:19:57 +02003560 if (nested_vmx_check_host_state(vcpu, vmcs12))
Sean Christophersonb2656e42020-06-08 18:56:07 -07003561 return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
Sean Christopherson55d23752018-12-03 13:53:18 -08003562
3563 /*
3564 * We're finally done with prerequisite checking, and can start with
3565 * the nested entry.
3566 */
3567 vmx->nested.nested_run_pending = 1;
Peter Shier850448f2020-05-26 14:51:06 -07003568 vmx->nested.has_preemption_timer_deadline = false;
Jim Mattson671ddc72019-10-15 10:44:05 -07003569 status = nested_vmx_enter_non_root_mode(vcpu, true);
3570 if (unlikely(status != NVMX_VMENTRY_SUCCESS))
3571 goto vmentry_failed;
Sean Christopherson55d23752018-12-03 13:53:18 -08003572
Sean Christopherson25bb2cf2020-08-12 10:51:29 -07003573 /* Emulate processing of posted interrupts on VM-Enter. */
3574 if (nested_cpu_has_posted_intr(vmcs12) &&
3575 kvm_apic_has_interrupt(vcpu) == vmx->nested.posted_intr_nv) {
3576 vmx->nested.pi_pending = true;
3577 kvm_make_request(KVM_REQ_EVENT, vcpu);
3578 kvm_apic_clear_irr(vcpu, vmx->nested.posted_intr_nv);
3579 }
3580
Sean Christopherson55d23752018-12-03 13:53:18 -08003581 /* Hide L1D cache contents from the nested guest. */
3582 vmx->vcpu.arch.l1tf_flush_l1d = true;
3583
3584 /*
3585 * Must happen outside of nested_vmx_enter_non_root_mode() as it will
3586 * also be used as part of restoring nVMX state for
3587 * snapshot restore (migration).
3588 *
3589 * In this flow, it is assumed that vmcs12 cache was
Ingo Molnar163b0992021-03-21 22:28:53 +01003590 * transferred as part of captured nVMX state and should
Sean Christopherson55d23752018-12-03 13:53:18 -08003591 * therefore not be read from guest memory (which may not
3592 * exist on destination host yet).
3593 */
3594 nested_cache_shadow_vmcs12(vcpu, vmcs12);
3595
Yadong Qibf0cd882020-11-06 14:51:22 +08003596 switch (vmcs12->guest_activity_state) {
3597 case GUEST_ACTIVITY_HLT:
3598 /*
3599 * If we're entering a halted L2 vcpu and the L2 vcpu won't be
3600 * awakened by event injection or by an NMI-window VM-exit or
3601 * by an interrupt-window VM-exit, halt the vcpu.
3602 */
3603 if (!(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) &&
3604 !nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING) &&
3605 !(nested_cpu_has(vmcs12, CPU_BASED_INTR_WINDOW_EXITING) &&
3606 (vmcs12->guest_rflags & X86_EFLAGS_IF))) {
3607 vmx->nested.nested_run_pending = 0;
3608 return kvm_vcpu_halt(vcpu);
3609 }
3610 break;
3611 case GUEST_ACTIVITY_WAIT_SIPI:
Sean Christopherson55d23752018-12-03 13:53:18 -08003612 vmx->nested.nested_run_pending = 0;
Yadong Qibf0cd882020-11-06 14:51:22 +08003613 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
3614 break;
3615 default:
3616 break;
Sean Christopherson55d23752018-12-03 13:53:18 -08003617 }
Yadong Qibf0cd882020-11-06 14:51:22 +08003618
Sean Christopherson55d23752018-12-03 13:53:18 -08003619 return 1;
Jim Mattson671ddc72019-10-15 10:44:05 -07003620
3621vmentry_failed:
3622 vmx->nested.nested_run_pending = 0;
3623 if (status == NVMX_VMENTRY_KVM_INTERNAL_ERROR)
3624 return 0;
3625 if (status == NVMX_VMENTRY_VMEXIT)
3626 return 1;
3627 WARN_ON_ONCE(status != NVMX_VMENTRY_VMFAIL);
Sean Christophersonb2656e42020-06-08 18:56:07 -07003628 return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Sean Christopherson55d23752018-12-03 13:53:18 -08003629}
3630
3631/*
3632 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
Miaohe Lin67b0ae42019-12-11 14:26:22 +08003633 * because L2 may have changed some cr0 bits directly (CR0_GUEST_HOST_MASK).
Sean Christopherson55d23752018-12-03 13:53:18 -08003634 * This function returns the new value we should put in vmcs12.guest_cr0.
3635 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
3636 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
3637 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
3638 * didn't trap the bit, because if L1 did, so would L0).
3639 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
3640 * been modified by L2, and L1 knows it. So just leave the old value of
3641 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
3642 * isn't relevant, because if L0 traps this bit it can set it to anything.
3643 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
3644 * changed these bits, and therefore they need to be updated, but L0
3645 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
3646 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
3647 */
3648static inline unsigned long
3649vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
3650{
3651 return
3652 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
3653 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
3654 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
3655 vcpu->arch.cr0_guest_owned_bits));
3656}
3657
3658static inline unsigned long
3659vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
3660{
3661 return
3662 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
3663 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
3664 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
3665 vcpu->arch.cr4_guest_owned_bits));
3666}
3667
3668static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
3669 struct vmcs12 *vmcs12)
3670{
3671 u32 idt_vectoring;
3672 unsigned int nr;
3673
3674 if (vcpu->arch.exception.injected) {
3675 nr = vcpu->arch.exception.nr;
3676 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
3677
3678 if (kvm_exception_is_soft(nr)) {
3679 vmcs12->vm_exit_instruction_len =
3680 vcpu->arch.event_exit_inst_len;
3681 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
3682 } else
3683 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
3684
3685 if (vcpu->arch.exception.has_error_code) {
3686 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
3687 vmcs12->idt_vectoring_error_code =
3688 vcpu->arch.exception.error_code;
3689 }
3690
3691 vmcs12->idt_vectoring_info_field = idt_vectoring;
3692 } else if (vcpu->arch.nmi_injected) {
3693 vmcs12->idt_vectoring_info_field =
3694 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
3695 } else if (vcpu->arch.interrupt.injected) {
3696 nr = vcpu->arch.interrupt.nr;
3697 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
3698
3699 if (vcpu->arch.interrupt.soft) {
3700 idt_vectoring |= INTR_TYPE_SOFT_INTR;
3701 vmcs12->vm_entry_instruction_len =
3702 vcpu->arch.event_exit_inst_len;
3703 } else
3704 idt_vectoring |= INTR_TYPE_EXT_INTR;
3705
3706 vmcs12->idt_vectoring_info_field = idt_vectoring;
3707 }
3708}
3709
3710
Paolo Bonzini96b100c2020-03-17 18:32:50 +01003711void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08003712{
3713 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3714 gfn_t gfn;
3715
3716 /*
3717 * Don't need to mark the APIC access page dirty; it is never
3718 * written to by the CPU during APIC virtualization.
3719 */
3720
3721 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
3722 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
3723 kvm_vcpu_mark_page_dirty(vcpu, gfn);
3724 }
3725
3726 if (nested_cpu_has_posted_intr(vmcs12)) {
3727 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
3728 kvm_vcpu_mark_page_dirty(vcpu, gfn);
3729 }
3730}
3731
Jim Mattson650293c2021-06-04 10:26:02 -07003732static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08003733{
3734 struct vcpu_vmx *vmx = to_vmx(vcpu);
3735 int max_irr;
3736 void *vapic_page;
3737 u16 status;
3738
Jim Mattson966eefb2021-06-04 10:26:06 -07003739 if (!vmx->nested.pi_pending)
Jim Mattson650293c2021-06-04 10:26:02 -07003740 return 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08003741
Jim Mattson966eefb2021-06-04 10:26:06 -07003742 if (!vmx->nested.pi_desc)
3743 goto mmio_needed;
3744
Sean Christopherson55d23752018-12-03 13:53:18 -08003745 vmx->nested.pi_pending = false;
Jim Mattson966eefb2021-06-04 10:26:06 -07003746
Sean Christopherson55d23752018-12-03 13:53:18 -08003747 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
Jim Mattson650293c2021-06-04 10:26:02 -07003748 return 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08003749
3750 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
3751 if (max_irr != 256) {
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003752 vapic_page = vmx->nested.virtual_apic_map.hva;
3753 if (!vapic_page)
Jim Mattson0fe998b2021-06-04 10:26:05 -07003754 goto mmio_needed;
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003755
Sean Christopherson55d23752018-12-03 13:53:18 -08003756 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
3757 vapic_page, &max_irr);
Sean Christopherson55d23752018-12-03 13:53:18 -08003758 status = vmcs_read16(GUEST_INTR_STATUS);
3759 if ((u8)max_irr > ((u8)status & 0xff)) {
3760 status &= ~0xff;
3761 status |= (u8)max_irr;
3762 vmcs_write16(GUEST_INTR_STATUS, status);
3763 }
3764 }
3765
3766 nested_mark_vmcs12_pages_dirty(vcpu);
Jim Mattson650293c2021-06-04 10:26:02 -07003767 return 0;
Jim Mattson0fe998b2021-06-04 10:26:05 -07003768
3769mmio_needed:
3770 kvm_handle_memory_failure(vcpu, X86EMUL_IO_NEEDED, NULL);
3771 return -ENXIO;
Sean Christopherson55d23752018-12-03 13:53:18 -08003772}
3773
3774static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3775 unsigned long exit_qual)
3776{
3777 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3778 unsigned int nr = vcpu->arch.exception.nr;
3779 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3780
3781 if (vcpu->arch.exception.has_error_code) {
3782 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3783 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3784 }
3785
3786 if (kvm_exception_is_soft(nr))
3787 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3788 else
3789 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3790
3791 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3792 vmx_get_nmi_mask(vcpu))
3793 intr_info |= INTR_INFO_UNBLOCK_NMI;
3794
3795 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3796}
3797
Oliver Upton684c0422020-02-07 02:36:05 -08003798/*
3799 * Returns true if a debug trap is pending delivery.
3800 *
3801 * In KVM, debug traps bear an exception payload. As such, the class of a #DB
3802 * exception may be inferred from the presence of an exception payload.
3803 */
3804static inline bool vmx_pending_dbg_trap(struct kvm_vcpu *vcpu)
3805{
3806 return vcpu->arch.exception.pending &&
3807 vcpu->arch.exception.nr == DB_VECTOR &&
3808 vcpu->arch.exception.payload;
3809}
3810
3811/*
3812 * Certain VM-exits set the 'pending debug exceptions' field to indicate a
3813 * recognized #DB (data or single-step) that has yet to be delivered. Since KVM
3814 * represents these debug traps with a payload that is said to be compatible
3815 * with the 'pending debug exceptions' field, write the payload to the VMCS
3816 * field if a VM-exit is delivered before the debug trap.
3817 */
3818static void nested_vmx_update_pending_dbg(struct kvm_vcpu *vcpu)
3819{
3820 if (vmx_pending_dbg_trap(vcpu))
3821 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
3822 vcpu->arch.exception.payload);
3823}
3824
Sean Christophersond2060bd2020-04-22 19:25:39 -07003825static bool nested_vmx_preemption_timer_pending(struct kvm_vcpu *vcpu)
3826{
3827 return nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
3828 to_vmx(vcpu)->nested.preemption_timer_expired;
3829}
3830
Sean Christophersona1c77ab2020-03-02 22:27:35 -08003831static int vmx_check_nested_events(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08003832{
3833 struct vcpu_vmx *vmx = to_vmx(vcpu);
3834 unsigned long exit_qual;
3835 bool block_nested_events =
3836 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003837 bool mtf_pending = vmx->nested.mtf_pending;
Liran Alon4b9852f2019-08-26 13:24:49 +03003838 struct kvm_lapic *apic = vcpu->arch.apic;
3839
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003840 /*
3841 * Clear the MTF state. If a higher priority VM-exit is delivered first,
3842 * this state is discarded.
3843 */
Oliver Upton5c8beb42020-04-06 20:12:37 +00003844 if (!block_nested_events)
3845 vmx->nested.mtf_pending = false;
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003846
Liran Alon4b9852f2019-08-26 13:24:49 +03003847 if (lapic_in_kernel(vcpu) &&
3848 test_bit(KVM_APIC_INIT, &apic->pending_events)) {
3849 if (block_nested_events)
3850 return -EBUSY;
Oliver Upton684c0422020-02-07 02:36:05 -08003851 nested_vmx_update_pending_dbg(vcpu);
Liran Alone64a8502019-11-11 14:16:05 +02003852 clear_bit(KVM_APIC_INIT, &apic->pending_events);
Yadong Qibf0cd882020-11-06 14:51:22 +08003853 if (vcpu->arch.mp_state != KVM_MP_STATE_INIT_RECEIVED)
3854 nested_vmx_vmexit(vcpu, EXIT_REASON_INIT_SIGNAL, 0, 0);
3855 return 0;
3856 }
3857
3858 if (lapic_in_kernel(vcpu) &&
3859 test_bit(KVM_APIC_SIPI, &apic->pending_events)) {
3860 if (block_nested_events)
3861 return -EBUSY;
3862
3863 clear_bit(KVM_APIC_SIPI, &apic->pending_events);
3864 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED)
3865 nested_vmx_vmexit(vcpu, EXIT_REASON_SIPI_SIGNAL, 0,
3866 apic->sipi_vector & 0xFFUL);
Liran Alon4b9852f2019-08-26 13:24:49 +03003867 return 0;
3868 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003869
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003870 /*
3871 * Process any exceptions that are not debug traps before MTF.
Maxim Levitsky4020da32021-04-01 17:38:14 +03003872 *
3873 * Note that only a pending nested run can block a pending exception.
3874 * Otherwise an injected NMI/interrupt should either be
3875 * lost or delivered to the nested hypervisor in the IDT_VECTORING_INFO,
3876 * while delivering the pending exception.
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003877 */
Maxim Levitsky4020da32021-04-01 17:38:14 +03003878
Sean Christopherson6ce347a2020-04-22 19:25:38 -07003879 if (vcpu->arch.exception.pending && !vmx_pending_dbg_trap(vcpu)) {
Maxim Levitsky4020da32021-04-01 17:38:14 +03003880 if (vmx->nested.nested_run_pending)
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003881 return -EBUSY;
Sean Christopherson6ce347a2020-04-22 19:25:38 -07003882 if (!nested_vmx_check_exception(vcpu, &exit_qual))
3883 goto no_vmexit;
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003884 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
3885 return 0;
3886 }
3887
3888 if (mtf_pending) {
3889 if (block_nested_events)
3890 return -EBUSY;
3891 nested_vmx_update_pending_dbg(vcpu);
3892 nested_vmx_vmexit(vcpu, EXIT_REASON_MONITOR_TRAP_FLAG, 0, 0);
3893 return 0;
3894 }
3895
Sean Christopherson6ce347a2020-04-22 19:25:38 -07003896 if (vcpu->arch.exception.pending) {
Maxim Levitsky4020da32021-04-01 17:38:14 +03003897 if (vmx->nested.nested_run_pending)
Sean Christopherson55d23752018-12-03 13:53:18 -08003898 return -EBUSY;
Sean Christopherson6ce347a2020-04-22 19:25:38 -07003899 if (!nested_vmx_check_exception(vcpu, &exit_qual))
3900 goto no_vmexit;
Sean Christopherson55d23752018-12-03 13:53:18 -08003901 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
3902 return 0;
3903 }
3904
Sean Christophersond2060bd2020-04-22 19:25:39 -07003905 if (nested_vmx_preemption_timer_pending(vcpu)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08003906 if (block_nested_events)
3907 return -EBUSY;
3908 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
3909 return 0;
3910 }
3911
Sean Christopherson1cd2f0b2020-04-22 19:25:46 -07003912 if (vcpu->arch.smi_pending && !is_smm(vcpu)) {
3913 if (block_nested_events)
3914 return -EBUSY;
3915 goto no_vmexit;
3916 }
3917
Sean Christopherson15ff0b42020-04-22 19:25:45 -07003918 if (vcpu->arch.nmi_pending && !vmx_nmi_blocked(vcpu)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08003919 if (block_nested_events)
3920 return -EBUSY;
Sean Christopherson15ff0b42020-04-22 19:25:45 -07003921 if (!nested_exit_on_nmi(vcpu))
3922 goto no_vmexit;
3923
Sean Christopherson55d23752018-12-03 13:53:18 -08003924 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
3925 NMI_VECTOR | INTR_TYPE_NMI_INTR |
3926 INTR_INFO_VALID_MASK, 0);
3927 /*
3928 * The NMI-triggered VM exit counts as injection:
3929 * clear this one and block further NMIs.
3930 */
3931 vcpu->arch.nmi_pending = 0;
3932 vmx_set_nmi_mask(vcpu, true);
3933 return 0;
3934 }
3935
Sean Christopherson15ff0b42020-04-22 19:25:45 -07003936 if (kvm_cpu_has_interrupt(vcpu) && !vmx_interrupt_blocked(vcpu)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08003937 if (block_nested_events)
3938 return -EBUSY;
Sean Christopherson15ff0b42020-04-22 19:25:45 -07003939 if (!nested_exit_on_intr(vcpu))
3940 goto no_vmexit;
Sean Christopherson55d23752018-12-03 13:53:18 -08003941 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
3942 return 0;
3943 }
3944
Sean Christopherson6ce347a2020-04-22 19:25:38 -07003945no_vmexit:
Jim Mattson650293c2021-06-04 10:26:02 -07003946 return vmx_complete_nested_posted_interrupt(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08003947}
3948
3949static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
3950{
3951 ktime_t remaining =
3952 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
3953 u64 value;
3954
3955 if (ktime_to_ns(remaining) <= 0)
3956 return 0;
3957
3958 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
3959 do_div(value, 1000000);
3960 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
3961}
3962
Sean Christopherson7952d762019-05-07 08:36:29 -07003963static bool is_vmcs12_ext_field(unsigned long field)
Sean Christopherson55d23752018-12-03 13:53:18 -08003964{
Sean Christopherson7952d762019-05-07 08:36:29 -07003965 switch (field) {
3966 case GUEST_ES_SELECTOR:
3967 case GUEST_CS_SELECTOR:
3968 case GUEST_SS_SELECTOR:
3969 case GUEST_DS_SELECTOR:
3970 case GUEST_FS_SELECTOR:
3971 case GUEST_GS_SELECTOR:
3972 case GUEST_LDTR_SELECTOR:
3973 case GUEST_TR_SELECTOR:
3974 case GUEST_ES_LIMIT:
3975 case GUEST_CS_LIMIT:
3976 case GUEST_SS_LIMIT:
3977 case GUEST_DS_LIMIT:
3978 case GUEST_FS_LIMIT:
3979 case GUEST_GS_LIMIT:
3980 case GUEST_LDTR_LIMIT:
3981 case GUEST_TR_LIMIT:
3982 case GUEST_GDTR_LIMIT:
3983 case GUEST_IDTR_LIMIT:
3984 case GUEST_ES_AR_BYTES:
3985 case GUEST_DS_AR_BYTES:
3986 case GUEST_FS_AR_BYTES:
3987 case GUEST_GS_AR_BYTES:
3988 case GUEST_LDTR_AR_BYTES:
3989 case GUEST_TR_AR_BYTES:
3990 case GUEST_ES_BASE:
3991 case GUEST_CS_BASE:
3992 case GUEST_SS_BASE:
3993 case GUEST_DS_BASE:
3994 case GUEST_FS_BASE:
3995 case GUEST_GS_BASE:
3996 case GUEST_LDTR_BASE:
3997 case GUEST_TR_BASE:
3998 case GUEST_GDTR_BASE:
3999 case GUEST_IDTR_BASE:
4000 case GUEST_PENDING_DBG_EXCEPTIONS:
4001 case GUEST_BNDCFGS:
4002 return true;
4003 default:
4004 break;
4005 }
Sean Christopherson55d23752018-12-03 13:53:18 -08004006
Sean Christopherson7952d762019-05-07 08:36:29 -07004007 return false;
4008}
4009
4010static void sync_vmcs02_to_vmcs12_rare(struct kvm_vcpu *vcpu,
4011 struct vmcs12 *vmcs12)
4012{
4013 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08004014
4015 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
4016 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
4017 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
4018 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
4019 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
4020 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
4021 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
4022 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
4023 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
4024 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
4025 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
4026 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
4027 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
4028 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
4029 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
4030 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
4031 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
4032 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
4033 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
Sean Christopherson55d23752018-12-03 13:53:18 -08004034 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
4035 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
4036 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
4037 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
4038 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
4039 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
4040 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
4041 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
4042 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
4043 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
4044 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
4045 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
4046 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
4047 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
4048 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
Sean Christopherson7952d762019-05-07 08:36:29 -07004049 vmcs12->guest_pending_dbg_exceptions =
4050 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
4051 if (kvm_mpx_supported())
4052 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
4053
4054 vmx->nested.need_sync_vmcs02_to_vmcs12_rare = false;
4055}
4056
4057static void copy_vmcs02_to_vmcs12_rare(struct kvm_vcpu *vcpu,
4058 struct vmcs12 *vmcs12)
4059{
4060 struct vcpu_vmx *vmx = to_vmx(vcpu);
4061 int cpu;
4062
4063 if (!vmx->nested.need_sync_vmcs02_to_vmcs12_rare)
4064 return;
4065
4066
4067 WARN_ON_ONCE(vmx->loaded_vmcs != &vmx->vmcs01);
4068
4069 cpu = get_cpu();
4070 vmx->loaded_vmcs = &vmx->nested.vmcs02;
Sean Christopherson1af1bb02020-05-06 16:58:50 -07004071 vmx_vcpu_load_vmcs(vcpu, cpu, &vmx->vmcs01);
Sean Christopherson7952d762019-05-07 08:36:29 -07004072
4073 sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
4074
4075 vmx->loaded_vmcs = &vmx->vmcs01;
Sean Christopherson1af1bb02020-05-06 16:58:50 -07004076 vmx_vcpu_load_vmcs(vcpu, cpu, &vmx->nested.vmcs02);
Sean Christopherson7952d762019-05-07 08:36:29 -07004077 put_cpu();
4078}
4079
4080/*
4081 * Update the guest state fields of vmcs12 to reflect changes that
4082 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
4083 * VM-entry controls is also updated, since this is really a guest
4084 * state bit.)
4085 */
4086static void sync_vmcs02_to_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
4087{
4088 struct vcpu_vmx *vmx = to_vmx(vcpu);
4089
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02004090 if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
Sean Christopherson7952d762019-05-07 08:36:29 -07004091 sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
4092
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02004093 vmx->nested.need_sync_vmcs02_to_vmcs12_rare =
4094 !evmptr_is_valid(vmx->nested.hv_evmcs_vmptr);
Sean Christopherson7952d762019-05-07 08:36:29 -07004095
4096 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
4097 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
4098
4099 vmcs12->guest_rsp = kvm_rsp_read(vcpu);
4100 vmcs12->guest_rip = kvm_rip_read(vcpu);
4101 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
4102
4103 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
4104 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
Sean Christopherson55d23752018-12-03 13:53:18 -08004105
4106 vmcs12->guest_interruptibility_info =
4107 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
Sean Christopherson7952d762019-05-07 08:36:29 -07004108
Sean Christopherson55d23752018-12-03 13:53:18 -08004109 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
4110 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
Yadong Qibf0cd882020-11-06 14:51:22 +08004111 else if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED)
4112 vmcs12->guest_activity_state = GUEST_ACTIVITY_WAIT_SIPI;
Sean Christopherson55d23752018-12-03 13:53:18 -08004113 else
4114 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
4115
Paolo Bonzinib4b65b52019-01-29 19:12:35 +01004116 if (nested_cpu_has_preemption_timer(vmcs12) &&
Peter Shier850448f2020-05-26 14:51:06 -07004117 vmcs12->vm_exit_controls & VM_EXIT_SAVE_VMX_PREEMPTION_TIMER &&
4118 !vmx->nested.nested_run_pending)
4119 vmcs12->vmx_preemption_timer_value =
4120 vmx_get_preemption_timer_value(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08004121
4122 /*
4123 * In some cases (usually, nested EPT), L2 is allowed to change its
4124 * own CR3 without exiting. If it has changed it, we must keep it.
4125 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
4126 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
4127 *
4128 * Additionally, restore L2's PDPTR to vmcs12.
4129 */
4130 if (enable_ept) {
4131 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Sean Christophersonc7554efc2019-05-07 09:06:40 -07004132 if (nested_cpu_has_ept(vmcs12) && is_pae_paging(vcpu)) {
4133 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
4134 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
4135 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
4136 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
4137 }
Sean Christopherson55d23752018-12-03 13:53:18 -08004138 }
4139
4140 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
4141
4142 if (nested_cpu_has_vid(vmcs12))
4143 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
4144
4145 vmcs12->vm_entry_controls =
4146 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
4147 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
4148
Sean Christopherson699a1ac2019-05-07 09:06:37 -07004149 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS)
Sean Christopherson55d23752018-12-03 13:53:18 -08004150 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
Sean Christopherson55d23752018-12-03 13:53:18 -08004151
Sean Christopherson55d23752018-12-03 13:53:18 -08004152 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
4153 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Sean Christopherson55d23752018-12-03 13:53:18 -08004154}
4155
4156/*
4157 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
4158 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
4159 * and this function updates it to reflect the changes to the guest state while
4160 * L2 was running (and perhaps made some exits which were handled directly by L0
4161 * without going back to L1), and to reflect the exit reason.
4162 * Note that we do not have to copy here all VMCS fields, just those that
4163 * could have changed by the L2 guest or the exit - i.e., the guest-state and
4164 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
4165 * which already writes to vmcs12 directly.
4166 */
4167static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004168 u32 vm_exit_reason, u32 exit_intr_info,
Sean Christopherson55d23752018-12-03 13:53:18 -08004169 unsigned long exit_qualification)
4170{
Sean Christopherson55d23752018-12-03 13:53:18 -08004171 /* update exit information fields: */
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004172 vmcs12->vm_exit_reason = vm_exit_reason;
Sean Christopherson3c0c2ad2021-04-12 16:21:37 +12004173 if (to_vmx(vcpu)->exit_reason.enclave_mode)
4174 vmcs12->vm_exit_reason |= VMX_EXIT_REASONS_SGX_ENCLAVE_MODE;
Sean Christopherson55d23752018-12-03 13:53:18 -08004175 vmcs12->exit_qualification = exit_qualification;
4176 vmcs12->vm_exit_intr_info = exit_intr_info;
4177
4178 vmcs12->idt_vectoring_info_field = 0;
4179 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
4180 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
4181
4182 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
4183 vmcs12->launch_state = 1;
4184
4185 /* vm_entry_intr_info_field is cleared on exit. Emulate this
4186 * instead of reading the real value. */
4187 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
4188
4189 /*
4190 * Transfer the event that L0 or L1 may wanted to inject into
4191 * L2 to IDT_VECTORING_INFO_FIELD.
4192 */
4193 vmcs12_save_pending_event(vcpu, vmcs12);
Krish Sadhukhana0d4f802018-12-04 19:00:13 -05004194
4195 /*
4196 * According to spec, there's no need to store the guest's
4197 * MSRs if the exit is due to a VM-entry failure that occurs
4198 * during or after loading the guest state. Since this exit
4199 * does not fall in that category, we need to save the MSRs.
4200 */
4201 if (nested_vmx_store_msr(vcpu,
4202 vmcs12->vm_exit_msr_store_addr,
4203 vmcs12->vm_exit_msr_store_count))
4204 nested_vmx_abort(vcpu,
4205 VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Sean Christopherson55d23752018-12-03 13:53:18 -08004206 }
4207
4208 /*
4209 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
4210 * preserved above and would only end up incorrectly in L1.
4211 */
4212 vcpu->arch.nmi_injected = false;
4213 kvm_clear_exception_queue(vcpu);
4214 kvm_clear_interrupt_queue(vcpu);
4215}
4216
4217/*
4218 * A part of what we need to when the nested L2 guest exits and we want to
4219 * run its L1 parent, is to reset L1's guest state to the host state specified
4220 * in vmcs12.
4221 * This function is to be called not only on normal nested exit, but also on
4222 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
4223 * Failures During or After Loading Guest State").
4224 * This function should be called when the active VMCS is L1's (vmcs01).
4225 */
4226static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
4227 struct vmcs12 *vmcs12)
4228{
Sean Christopherson68cda402020-05-11 15:05:29 -07004229 enum vm_entry_failure_code ignored;
Sean Christopherson55d23752018-12-03 13:53:18 -08004230 struct kvm_segment seg;
Sean Christopherson55d23752018-12-03 13:53:18 -08004231
4232 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
4233 vcpu->arch.efer = vmcs12->host_ia32_efer;
4234 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
4235 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
4236 else
4237 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
4238 vmx_set_efer(vcpu, vcpu->arch.efer);
4239
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02004240 kvm_rsp_write(vcpu, vmcs12->host_rsp);
4241 kvm_rip_write(vcpu, vmcs12->host_rip);
Sean Christopherson55d23752018-12-03 13:53:18 -08004242 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
4243 vmx_set_interrupt_shadow(vcpu, 0);
4244
4245 /*
4246 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
4247 * actually changed, because vmx_set_cr0 refers to efer set above.
4248 *
4249 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
4250 * (KVM doesn't change it);
4251 */
Sean Christophersonfa71e952020-07-02 21:04:22 -07004252 vcpu->arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS;
Sean Christopherson55d23752018-12-03 13:53:18 -08004253 vmx_set_cr0(vcpu, vmcs12->host_cr0);
4254
4255 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
4256 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
4257 vmx_set_cr4(vcpu, vmcs12->host_cr4);
4258
4259 nested_ept_uninit_mmu_context(vcpu);
4260
4261 /*
4262 * Only PDPTE load can fail as the value of cr3 was checked on entry and
4263 * couldn't have changed.
4264 */
Maxim Levitsky0f857222021-06-07 12:02:00 +03004265 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, true, &ignored))
Sean Christopherson55d23752018-12-03 13:53:18 -08004266 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
4267
Sean Christopherson50b265a2020-03-20 14:28:19 -07004268 nested_vmx_transition_tlb_flush(vcpu, vmcs12, false);
Sean Christopherson55d23752018-12-03 13:53:18 -08004269
4270 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
4271 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
4272 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
4273 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
4274 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
4275 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
4276 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
4277
4278 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
4279 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
4280 vmcs_write64(GUEST_BNDCFGS, 0);
4281
4282 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
4283 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
4284 vcpu->arch.pat = vmcs12->host_ia32_pat;
4285 }
4286 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Oliver Uptond1968422019-12-13 16:33:58 -08004287 WARN_ON_ONCE(kvm_set_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL,
4288 vmcs12->host_ia32_perf_global_ctrl));
Sean Christopherson55d23752018-12-03 13:53:18 -08004289
4290 /* Set L1 segment info according to Intel SDM
4291 27.5.2 Loading Host Segment and Descriptor-Table Registers */
4292 seg = (struct kvm_segment) {
4293 .base = 0,
4294 .limit = 0xFFFFFFFF,
4295 .selector = vmcs12->host_cs_selector,
4296 .type = 11,
4297 .present = 1,
4298 .s = 1,
4299 .g = 1
4300 };
4301 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
4302 seg.l = 1;
4303 else
4304 seg.db = 1;
Sean Christopherson816be9e2021-07-13 09:33:07 -07004305 __vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
Sean Christopherson55d23752018-12-03 13:53:18 -08004306 seg = (struct kvm_segment) {
4307 .base = 0,
4308 .limit = 0xFFFFFFFF,
4309 .type = 3,
4310 .present = 1,
4311 .s = 1,
4312 .db = 1,
4313 .g = 1
4314 };
4315 seg.selector = vmcs12->host_ds_selector;
Sean Christopherson816be9e2021-07-13 09:33:07 -07004316 __vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
Sean Christopherson55d23752018-12-03 13:53:18 -08004317 seg.selector = vmcs12->host_es_selector;
Sean Christopherson816be9e2021-07-13 09:33:07 -07004318 __vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
Sean Christopherson55d23752018-12-03 13:53:18 -08004319 seg.selector = vmcs12->host_ss_selector;
Sean Christopherson816be9e2021-07-13 09:33:07 -07004320 __vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
Sean Christopherson55d23752018-12-03 13:53:18 -08004321 seg.selector = vmcs12->host_fs_selector;
4322 seg.base = vmcs12->host_fs_base;
Sean Christopherson816be9e2021-07-13 09:33:07 -07004323 __vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
Sean Christopherson55d23752018-12-03 13:53:18 -08004324 seg.selector = vmcs12->host_gs_selector;
4325 seg.base = vmcs12->host_gs_base;
Sean Christopherson816be9e2021-07-13 09:33:07 -07004326 __vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
Sean Christopherson55d23752018-12-03 13:53:18 -08004327 seg = (struct kvm_segment) {
4328 .base = vmcs12->host_tr_base,
4329 .limit = 0x67,
4330 .selector = vmcs12->host_tr_selector,
4331 .type = 11,
4332 .present = 1
4333 };
Sean Christopherson816be9e2021-07-13 09:33:07 -07004334 __vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
Sean Christopherson55d23752018-12-03 13:53:18 -08004335
Sean Christophersonafc8de02021-07-13 09:32:40 -07004336 memset(&seg, 0, sizeof(seg));
4337 seg.unusable = 1;
Sean Christopherson816be9e2021-07-13 09:33:07 -07004338 __vmx_set_segment(vcpu, &seg, VCPU_SREG_LDTR);
Sean Christopherson55d23752018-12-03 13:53:18 -08004339
4340 kvm_set_dr(vcpu, 7, 0x400);
4341 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
4342
Sean Christopherson55d23752018-12-03 13:53:18 -08004343 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
4344 vmcs12->vm_exit_msr_load_count))
4345 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Maxim Levitskydbab6102021-09-13 17:09:54 +03004346
4347 to_vmx(vcpu)->emulation_required = vmx_emulation_required(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08004348}
4349
4350static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx)
4351{
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07004352 struct vmx_uret_msr *efer_msr;
Sean Christopherson55d23752018-12-03 13:53:18 -08004353 unsigned int i;
4354
4355 if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER)
4356 return vmcs_read64(GUEST_IA32_EFER);
4357
4358 if (cpu_has_load_ia32_efer())
4359 return host_efer;
4360
4361 for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) {
4362 if (vmx->msr_autoload.guest.val[i].index == MSR_EFER)
4363 return vmx->msr_autoload.guest.val[i].value;
4364 }
4365
Sean Christophersond85a8032020-09-23 11:04:06 -07004366 efer_msr = vmx_find_uret_msr(vmx, MSR_EFER);
Sean Christopherson55d23752018-12-03 13:53:18 -08004367 if (efer_msr)
4368 return efer_msr->data;
4369
4370 return host_efer;
4371}
4372
4373static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu)
4374{
4375 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4376 struct vcpu_vmx *vmx = to_vmx(vcpu);
4377 struct vmx_msr_entry g, h;
Sean Christopherson55d23752018-12-03 13:53:18 -08004378 gpa_t gpa;
4379 u32 i, j;
4380
4381 vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT);
4382
4383 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
4384 /*
4385 * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set
4386 * as vmcs01.GUEST_DR7 contains a userspace defined value
4387 * and vcpu->arch.dr7 is not squirreled away before the
4388 * nested VMENTER (not worth adding a variable in nested_vmx).
4389 */
4390 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
4391 kvm_set_dr(vcpu, 7, DR7_FIXED_1);
4392 else
4393 WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7)));
4394 }
4395
4396 /*
4397 * Note that calling vmx_set_{efer,cr0,cr4} is important as they
4398 * handle a variety of side effects to KVM's software model.
4399 */
4400 vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx));
4401
Sean Christophersonfa71e952020-07-02 21:04:22 -07004402 vcpu->arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS;
Sean Christopherson55d23752018-12-03 13:53:18 -08004403 vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW));
4404
4405 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
4406 vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW));
4407
4408 nested_ept_uninit_mmu_context(vcpu);
Sean Christophersonf087a022019-06-07 11:55:34 -07004409 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07004410 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Sean Christopherson55d23752018-12-03 13:53:18 -08004411
4412 /*
4413 * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs
4414 * from vmcs01 (if necessary). The PDPTRs are not loaded on
4415 * VMFail, like everything else we just need to ensure our
4416 * software model is up-to-date.
4417 */
Sean Christopherson9932b492020-04-15 13:34:50 -07004418 if (enable_ept && is_pae_paging(vcpu))
Sean Christophersonf087a022019-06-07 11:55:34 -07004419 ept_save_pdptrs(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08004420
4421 kvm_mmu_reset_context(vcpu);
4422
Sean Christopherson55d23752018-12-03 13:53:18 -08004423 /*
4424 * This nasty bit of open coding is a compromise between blindly
4425 * loading L1's MSRs using the exit load lists (incorrect emulation
4426 * of VMFail), leaving the nested VM's MSRs in the software model
4427 * (incorrect behavior) and snapshotting the modified MSRs (too
4428 * expensive since the lists are unbound by hardware). For each
4429 * MSR that was (prematurely) loaded from the nested VMEntry load
4430 * list, reload it from the exit load list if it exists and differs
4431 * from the guest value. The intent is to stuff host state as
4432 * silently as possible, not to fully process the exit load list.
4433 */
Sean Christopherson55d23752018-12-03 13:53:18 -08004434 for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) {
4435 gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g));
4436 if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) {
4437 pr_debug_ratelimited(
4438 "%s read MSR index failed (%u, 0x%08llx)\n",
4439 __func__, i, gpa);
4440 goto vmabort;
4441 }
4442
4443 for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) {
4444 gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h));
4445 if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) {
4446 pr_debug_ratelimited(
4447 "%s read MSR failed (%u, 0x%08llx)\n",
4448 __func__, j, gpa);
4449 goto vmabort;
4450 }
4451 if (h.index != g.index)
4452 continue;
4453 if (h.value == g.value)
4454 break;
4455
4456 if (nested_vmx_load_msr_check(vcpu, &h)) {
4457 pr_debug_ratelimited(
4458 "%s check failed (%u, 0x%x, 0x%x)\n",
4459 __func__, j, h.index, h.reserved);
4460 goto vmabort;
4461 }
4462
Sean Christophersonf20935d2019-09-05 14:22:54 -07004463 if (kvm_set_msr(vcpu, h.index, h.value)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08004464 pr_debug_ratelimited(
4465 "%s WRMSR failed (%u, 0x%x, 0x%llx)\n",
4466 __func__, j, h.index, h.value);
4467 goto vmabort;
4468 }
4469 }
4470 }
4471
4472 return;
4473
4474vmabort:
4475 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
4476}
4477
4478/*
4479 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
4480 * and modify vmcs12 to make it see what it would expect to see there if
4481 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
4482 */
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004483void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason,
Sean Christopherson55d23752018-12-03 13:53:18 -08004484 u32 exit_intr_info, unsigned long exit_qualification)
4485{
4486 struct vcpu_vmx *vmx = to_vmx(vcpu);
4487 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4488
4489 /* trying to cancel vmlaunch/vmresume is a bug */
4490 WARN_ON_ONCE(vmx->nested.nested_run_pending);
4491
Sean Christophersoncb6a32c2021-03-02 09:45:14 -08004492 /* Similarly, triple faults in L2 should never escape. */
4493 WARN_ON_ONCE(kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu));
4494
Vitaly Kuznetsovf5c7e842021-05-03 17:08:51 +02004495 if (kvm_check_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu)) {
4496 /*
4497 * KVM_REQ_GET_NESTED_STATE_PAGES is also used to map
4498 * Enlightened VMCS after migration and we still need to
4499 * do that when something is forcing L2->L1 exit prior to
4500 * the first L2 run.
4501 */
4502 (void)nested_get_evmcs_page(vcpu);
4503 }
Maxim Levitskyf2c7ef32021-01-07 11:38:51 +02004504
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07004505 /* Service the TLB flush request for L2 before switching to L1. */
4506 if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu))
4507 kvm_vcpu_flush_tlb_current(vcpu);
4508
Peter Shier43fea4e2020-08-20 16:05:45 -07004509 /*
4510 * VCPU_EXREG_PDPTR will be clobbered in arch/x86/kvm/vmx/vmx.h between
4511 * now and the new vmentry. Ensure that the VMCS02 PDPTR fields are
4512 * up-to-date before switching to L1.
4513 */
4514 if (enable_ept && is_pae_paging(vcpu))
4515 vmx_ept_load_pdptrs(vcpu);
4516
Sean Christopherson55d23752018-12-03 13:53:18 -08004517 leave_guest_mode(vcpu);
4518
Paolo Bonzinib4b65b52019-01-29 19:12:35 +01004519 if (nested_cpu_has_preemption_timer(vmcs12))
4520 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
4521
Ilias Stamatisd041b5e2021-05-26 19:44:17 +01004522 if (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETTING)) {
4523 vcpu->arch.tsc_offset = vcpu->arch.l1_tsc_offset;
4524 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_TSC_SCALING))
4525 vcpu->arch.tsc_scaling_ratio = vcpu->arch.l1_tsc_scaling_ratio;
4526 }
Sean Christopherson55d23752018-12-03 13:53:18 -08004527
4528 if (likely(!vmx->fail)) {
Sean Christopherson3731905ef2019-05-07 08:36:27 -07004529 sync_vmcs02_to_vmcs12(vcpu, vmcs12);
Sean Christophersonf4f83162019-05-07 08:36:26 -07004530
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004531 if (vm_exit_reason != -1)
4532 prepare_vmcs12(vcpu, vmcs12, vm_exit_reason,
4533 exit_intr_info, exit_qualification);
Sean Christopherson55d23752018-12-03 13:53:18 -08004534
4535 /*
Sean Christopherson3731905ef2019-05-07 08:36:27 -07004536 * Must happen outside of sync_vmcs02_to_vmcs12() as it will
Sean Christopherson55d23752018-12-03 13:53:18 -08004537 * also be used to capture vmcs12 cache as part of
4538 * capturing nVMX state for snapshot (migration).
4539 *
4540 * Otherwise, this flush will dirty guest memory at a
4541 * point it is already assumed by user-space to be
4542 * immutable.
4543 */
4544 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08004545 } else {
4546 /*
4547 * The only expected VM-instruction error is "VM entry with
4548 * invalid control field(s)." Anything else indicates a
4549 * problem with L0. And we should never get here with a
4550 * VMFail of any type if early consistency checks are enabled.
4551 */
4552 WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) !=
4553 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
4554 WARN_ON_ONCE(nested_early_check);
4555 }
4556
4557 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
4558
4559 /* Update any VMCS fields that might have changed while L2 ran */
4560 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
4561 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
4562 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Ilias Stamatis1ab92872021-06-07 11:54:38 +01004563 if (kvm_has_tsc_control)
4564 vmcs_write64(TSC_MULTIPLIER, vcpu->arch.tsc_scaling_ratio);
4565
Liran Alon02d496cf2019-11-11 14:30:55 +02004566 if (vmx->nested.l1_tpr_threshold != -1)
4567 vmcs_write32(TPR_THRESHOLD, vmx->nested.l1_tpr_threshold);
Sean Christopherson55d23752018-12-03 13:53:18 -08004568
Sean Christopherson55d23752018-12-03 13:53:18 -08004569 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
4570 vmx->nested.change_vmcs01_virtual_apic_mode = false;
4571 vmx_set_virtual_apic_mode(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08004572 }
4573
Makarand Sonarea85863c2021-02-12 16:50:12 -08004574 if (vmx->nested.update_vmcs01_cpu_dirty_logging) {
4575 vmx->nested.update_vmcs01_cpu_dirty_logging = false;
4576 vmx_update_cpu_dirty_logging(vcpu);
4577 }
4578
Sean Christopherson55d23752018-12-03 13:53:18 -08004579 /* Unpin physical memory we referred to in vmcs02 */
4580 if (vmx->nested.apic_access_page) {
Liran Alonb11494b2019-11-21 00:31:47 +02004581 kvm_release_page_clean(vmx->nested.apic_access_page);
Sean Christopherson55d23752018-12-03 13:53:18 -08004582 vmx->nested.apic_access_page = NULL;
4583 }
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01004584 kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map, true);
KarimAllah Ahmed3278e042019-01-31 21:24:38 +01004585 kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map, true);
4586 vmx->nested.pi_desc = NULL;
Sean Christopherson55d23752018-12-03 13:53:18 -08004587
Sean Christopherson1196cb92020-03-20 14:28:23 -07004588 if (vmx->nested.reload_vmcs01_apic_access_page) {
4589 vmx->nested.reload_vmcs01_apic_access_page = false;
4590 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
4591 }
Sean Christopherson55d23752018-12-03 13:53:18 -08004592
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004593 if ((vm_exit_reason != -1) &&
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02004594 (enable_shadow_vmcs || evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)))
Sean Christopherson3731905ef2019-05-07 08:36:27 -07004595 vmx->nested.need_vmcs12_to_shadow_sync = true;
Sean Christopherson55d23752018-12-03 13:53:18 -08004596
4597 /* in case we halted in L2 */
4598 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
4599
4600 if (likely(!vmx->fail)) {
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004601 if ((u16)vm_exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
Sean Christophersona1c77ab2020-03-02 22:27:35 -08004602 nested_exit_intr_ack_set(vcpu)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08004603 int irq = kvm_cpu_get_interrupt(vcpu);
4604 WARN_ON(irq < 0);
4605 vmcs12->vm_exit_intr_info = irq |
4606 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
4607 }
4608
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004609 if (vm_exit_reason != -1)
Sean Christopherson55d23752018-12-03 13:53:18 -08004610 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
4611 vmcs12->exit_qualification,
4612 vmcs12->idt_vectoring_info_field,
4613 vmcs12->vm_exit_intr_info,
4614 vmcs12->vm_exit_intr_error_code,
4615 KVM_ISA_VMX);
4616
4617 load_vmcs12_host_state(vcpu, vmcs12);
4618
4619 return;
4620 }
4621
4622 /*
4623 * After an early L2 VM-entry failure, we're now back
4624 * in L1 which thinks it just finished a VMLAUNCH or
4625 * VMRESUME instruction, so we need to set the failure
4626 * flag and the VM-instruction error field of the VMCS
4627 * accordingly, and skip the emulated instruction.
4628 */
Sean Christophersonb2656e42020-06-08 18:56:07 -07004629 (void)nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Sean Christopherson55d23752018-12-03 13:53:18 -08004630
4631 /*
4632 * Restore L1's host state to KVM's software model. We're here
4633 * because a consistency check was caught by hardware, which
4634 * means some amount of guest state has been propagated to KVM's
4635 * model and needs to be unwound to the host's state.
4636 */
4637 nested_vmx_restore_host_state(vcpu);
4638
4639 vmx->fail = 0;
4640}
4641
Sean Christophersoncb6a32c2021-03-02 09:45:14 -08004642static void nested_vmx_triple_fault(struct kvm_vcpu *vcpu)
4643{
4644 nested_vmx_vmexit(vcpu, EXIT_REASON_TRIPLE_FAULT, 0, 0);
4645}
4646
Sean Christopherson55d23752018-12-03 13:53:18 -08004647/*
4648 * Decode the memory-address operand of a vmx instruction, as recorded on an
4649 * exit caused by such an instruction (run by a guest hypervisor).
4650 * On success, returns 0. When the operand is invalid, returns 1 and throws
Miaohe Lin49f933d2020-02-27 11:20:54 +08004651 * #UD, #GP, or #SS.
Sean Christopherson55d23752018-12-03 13:53:18 -08004652 */
4653int get_vmx_mem_address(struct kvm_vcpu *vcpu, unsigned long exit_qualification,
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004654 u32 vmx_instruction_info, bool wr, int len, gva_t *ret)
Sean Christopherson55d23752018-12-03 13:53:18 -08004655{
4656 gva_t off;
4657 bool exn;
4658 struct kvm_segment s;
4659
4660 /*
4661 * According to Vol. 3B, "Information for VM Exits Due to Instruction
4662 * Execution", on an exit, vmx_instruction_info holds most of the
4663 * addressing components of the operand. Only the displacement part
4664 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
4665 * For how an actual address is calculated from all these components,
4666 * refer to Vol. 1, "Operand Addressing".
4667 */
4668 int scaling = vmx_instruction_info & 3;
4669 int addr_size = (vmx_instruction_info >> 7) & 7;
4670 bool is_reg = vmx_instruction_info & (1u << 10);
4671 int seg_reg = (vmx_instruction_info >> 15) & 7;
4672 int index_reg = (vmx_instruction_info >> 18) & 0xf;
4673 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
4674 int base_reg = (vmx_instruction_info >> 23) & 0xf;
4675 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
4676
4677 if (is_reg) {
4678 kvm_queue_exception(vcpu, UD_VECTOR);
4679 return 1;
4680 }
4681
4682 /* Addr = segment_base + offset */
4683 /* offset = base + [index * scale] + displacement */
4684 off = exit_qualification; /* holds the displacement */
Sean Christopherson946c5222019-01-23 14:39:23 -08004685 if (addr_size == 1)
4686 off = (gva_t)sign_extend64(off, 31);
4687 else if (addr_size == 0)
4688 off = (gva_t)sign_extend64(off, 15);
Sean Christopherson55d23752018-12-03 13:53:18 -08004689 if (base_is_valid)
4690 off += kvm_register_read(vcpu, base_reg);
4691 if (index_is_valid)
Miaohe Line6302692020-02-15 10:44:22 +08004692 off += kvm_register_read(vcpu, index_reg) << scaling;
Sean Christopherson55d23752018-12-03 13:53:18 -08004693 vmx_get_segment(vcpu, &s, seg_reg);
Sean Christopherson55d23752018-12-03 13:53:18 -08004694
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004695 /*
4696 * The effective address, i.e. @off, of a memory operand is truncated
4697 * based on the address size of the instruction. Note that this is
4698 * the *effective address*, i.e. the address prior to accounting for
4699 * the segment's base.
4700 */
Sean Christopherson55d23752018-12-03 13:53:18 -08004701 if (addr_size == 1) /* 32 bit */
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004702 off &= 0xffffffff;
4703 else if (addr_size == 0) /* 16 bit */
4704 off &= 0xffff;
Sean Christopherson55d23752018-12-03 13:53:18 -08004705
4706 /* Checks for #GP/#SS exceptions. */
4707 exn = false;
4708 if (is_long_mode(vcpu)) {
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004709 /*
4710 * The virtual/linear address is never truncated in 64-bit
4711 * mode, e.g. a 32-bit address size can yield a 64-bit virtual
4712 * address when using FS/GS with a non-zero base.
4713 */
Liran Alon6694e482019-07-15 18:47:44 +03004714 if (seg_reg == VCPU_SREG_FS || seg_reg == VCPU_SREG_GS)
4715 *ret = s.base + off;
4716 else
4717 *ret = off;
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004718
Sean Christopherson55d23752018-12-03 13:53:18 -08004719 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
4720 * non-canonical form. This is the only check on the memory
4721 * destination for long mode!
4722 */
4723 exn = is_noncanonical_address(*ret, vcpu);
Paolo Bonzinie0dfacb2019-01-30 17:25:38 +01004724 } else {
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004725 /*
4726 * When not in long mode, the virtual/linear address is
4727 * unconditionally truncated to 32 bits regardless of the
4728 * address size.
4729 */
4730 *ret = (s.base + off) & 0xffffffff;
4731
Sean Christopherson55d23752018-12-03 13:53:18 -08004732 /* Protected mode: apply checks for segment validity in the
4733 * following order:
4734 * - segment type check (#GP(0) may be thrown)
4735 * - usability check (#GP(0)/#SS(0))
4736 * - limit check (#GP(0)/#SS(0))
4737 */
4738 if (wr)
4739 /* #GP(0) if the destination operand is located in a
4740 * read-only data segment or any code segment.
4741 */
4742 exn = ((s.type & 0xa) == 0 || (s.type & 8));
4743 else
4744 /* #GP(0) if the source operand is located in an
4745 * execute-only code segment
4746 */
4747 exn = ((s.type & 0xa) == 8);
4748 if (exn) {
4749 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
4750 return 1;
4751 }
4752 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
4753 */
4754 exn = (s.unusable != 0);
Sean Christopherson34333cc2019-01-23 14:39:25 -08004755
4756 /*
4757 * Protected mode: #GP(0)/#SS(0) if the memory operand is
4758 * outside the segment limit. All CPUs that support VMX ignore
4759 * limit checks for flat segments, i.e. segments with base==0,
4760 * limit==0xffffffff and of type expand-up data or code.
Sean Christopherson55d23752018-12-03 13:53:18 -08004761 */
Sean Christopherson34333cc2019-01-23 14:39:25 -08004762 if (!(s.base == 0 && s.limit == 0xffffffff &&
4763 ((s.type & 8) || !(s.type & 4))))
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004764 exn = exn || ((u64)off + len - 1 > s.limit);
Sean Christopherson55d23752018-12-03 13:53:18 -08004765 }
4766 if (exn) {
4767 kvm_queue_exception_e(vcpu,
4768 seg_reg == VCPU_SREG_SS ?
4769 SS_VECTOR : GP_VECTOR,
4770 0);
4771 return 1;
4772 }
4773
4774 return 0;
4775}
4776
Oliver Upton03a8871a2019-11-13 16:17:20 -08004777void nested_vmx_pmu_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
4778{
4779 struct vcpu_vmx *vmx;
4780
4781 if (!nested_vmx_allowed(vcpu))
4782 return;
4783
4784 vmx = to_vmx(vcpu);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004785 if (kvm_x86_ops.pmu_ops->is_valid_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL)) {
Oliver Upton03a8871a2019-11-13 16:17:20 -08004786 vmx->nested.msrs.entry_ctls_high |=
4787 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
4788 vmx->nested.msrs.exit_ctls_high |=
4789 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
4790 } else {
4791 vmx->nested.msrs.entry_ctls_high &=
4792 ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
4793 vmx->nested.msrs.exit_ctls_high &=
Chenyi Qiangc6b177a2020-08-28 16:56:21 +08004794 ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
Oliver Upton03a8871a2019-11-13 16:17:20 -08004795 }
4796}
4797
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004798static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer,
4799 int *ret)
Sean Christopherson55d23752018-12-03 13:53:18 -08004800{
4801 gva_t gva;
4802 struct x86_exception e;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004803 int r;
Sean Christopherson55d23752018-12-03 13:53:18 -08004804
Sean Christopherson5addc232020-04-15 13:34:53 -07004805 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004806 vmcs_read32(VMX_INSTRUCTION_INFO), false,
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004807 sizeof(*vmpointer), &gva)) {
4808 *ret = 1;
4809 return -EINVAL;
4810 }
Sean Christopherson55d23752018-12-03 13:53:18 -08004811
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004812 r = kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e);
4813 if (r != X86EMUL_CONTINUE) {
Babu Moger3f3393b2020-09-11 14:29:05 -05004814 *ret = kvm_handle_memory_failure(vcpu, r, &e);
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004815 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08004816 }
4817
4818 return 0;
4819}
4820
4821/*
4822 * Allocate a shadow VMCS and associate it with the currently loaded
4823 * VMCS, unless such a shadow VMCS already exists. The newly allocated
4824 * VMCS is also VMCLEARed, so that it is ready for use.
4825 */
4826static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
4827{
4828 struct vcpu_vmx *vmx = to_vmx(vcpu);
4829 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
4830
4831 /*
4832 * We should allocate a shadow vmcs for vmcs01 only when L1
4833 * executes VMXON and free it when L1 executes VMXOFF.
4834 * As it is invalid to execute VMXON twice, we shouldn't reach
4835 * here when vmcs01 already have an allocated shadow vmcs.
4836 */
4837 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
4838
4839 if (!loaded_vmcs->shadow_vmcs) {
4840 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
4841 if (loaded_vmcs->shadow_vmcs)
4842 vmcs_clear(loaded_vmcs->shadow_vmcs);
4843 }
4844 return loaded_vmcs->shadow_vmcs;
4845}
4846
4847static int enter_vmx_operation(struct kvm_vcpu *vcpu)
4848{
4849 struct vcpu_vmx *vmx = to_vmx(vcpu);
4850 int r;
4851
4852 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
4853 if (r < 0)
4854 goto out_vmcs02;
4855
Ben Gardon41836832019-02-11 11:02:52 -08004856 vmx->nested.cached_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT);
Sean Christopherson55d23752018-12-03 13:53:18 -08004857 if (!vmx->nested.cached_vmcs12)
4858 goto out_cached_vmcs12;
4859
Paolo Bonzini8503fea2021-11-22 18:20:16 -05004860 vmx->nested.shadow_vmcs12_cache.gpa = INVALID_GPA;
Ben Gardon41836832019-02-11 11:02:52 -08004861 vmx->nested.cached_shadow_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT);
Sean Christopherson55d23752018-12-03 13:53:18 -08004862 if (!vmx->nested.cached_shadow_vmcs12)
4863 goto out_cached_shadow_vmcs12;
4864
4865 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
4866 goto out_shadow_vmcs;
4867
4868 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
Jim Mattsonada00982020-05-08 13:36:42 -07004869 HRTIMER_MODE_ABS_PINNED);
Sean Christopherson55d23752018-12-03 13:53:18 -08004870 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
4871
4872 vmx->nested.vpid02 = allocate_vpid();
4873
4874 vmx->nested.vmcs02_initialized = false;
4875 vmx->nested.vmxon = true;
Luwei Kangee85dec2018-10-24 16:05:16 +08004876
Sean Christopherson2ef76192020-03-02 15:56:22 -08004877 if (vmx_pt_mode_is_host_guest()) {
Luwei Kangee85dec2018-10-24 16:05:16 +08004878 vmx->pt_desc.guest.ctl = 0;
Aaron Lewis476c9bd2020-09-25 16:34:18 +02004879 pt_update_intercept_for_msr(vcpu);
Luwei Kangee85dec2018-10-24 16:05:16 +08004880 }
4881
Sean Christopherson55d23752018-12-03 13:53:18 -08004882 return 0;
4883
4884out_shadow_vmcs:
4885 kfree(vmx->nested.cached_shadow_vmcs12);
4886
4887out_cached_shadow_vmcs12:
4888 kfree(vmx->nested.cached_vmcs12);
4889
4890out_cached_vmcs12:
4891 free_loaded_vmcs(&vmx->nested.vmcs02);
4892
4893out_vmcs02:
4894 return -ENOMEM;
4895}
4896
Yu Zhanged7023a2021-09-09 01:17:31 +08004897/* Emulate the VMXON instruction. */
Sean Christopherson55d23752018-12-03 13:53:18 -08004898static int handle_vmon(struct kvm_vcpu *vcpu)
4899{
4900 int ret;
4901 gpa_t vmptr;
KarimAllah Ahmed2e408932019-01-31 21:24:31 +01004902 uint32_t revision;
Sean Christopherson55d23752018-12-03 13:53:18 -08004903 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson32ad73d2019-12-20 20:44:55 -08004904 const u64 VMXON_NEEDED_FEATURES = FEAT_CTL_LOCKED
4905 | FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX;
Sean Christopherson55d23752018-12-03 13:53:18 -08004906
4907 /*
4908 * The Intel VMX Instruction Reference lists a bunch of bits that are
4909 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
Sean Christophersonc2fe3cd2020-10-06 18:44:15 -07004910 * 1 (see vmx_is_valid_cr4() for when we allow the guest to set this).
Sean Christopherson55d23752018-12-03 13:53:18 -08004911 * Otherwise, we should fail with #UD. But most faulting conditions
4912 * have already been checked by hardware, prior to the VM-exit for
4913 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
4914 * that bit set to 1 in non-root mode.
4915 */
4916 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
4917 kvm_queue_exception(vcpu, UD_VECTOR);
4918 return 1;
4919 }
4920
4921 /* CPL=0 must be checked manually. */
4922 if (vmx_get_cpl(vcpu)) {
4923 kvm_inject_gp(vcpu, 0);
4924 return 1;
4925 }
4926
4927 if (vmx->nested.vmxon)
Sean Christophersonb2656e42020-06-08 18:56:07 -07004928 return nested_vmx_fail(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Sean Christopherson55d23752018-12-03 13:53:18 -08004929
4930 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
4931 != VMXON_NEEDED_FEATURES) {
4932 kvm_inject_gp(vcpu, 0);
4933 return 1;
4934 }
4935
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004936 if (nested_vmx_get_vmptr(vcpu, &vmptr, &ret))
4937 return ret;
Sean Christopherson55d23752018-12-03 13:53:18 -08004938
4939 /*
4940 * SDM 3: 24.11.5
4941 * The first 4 bytes of VMXON region contain the supported
4942 * VMCS revision identifier
4943 *
4944 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
4945 * which replaces physical address width with 32
4946 */
KarimAllah Ahmede0bf2662019-01-31 21:24:43 +01004947 if (!page_address_valid(vcpu, vmptr))
Sean Christopherson55d23752018-12-03 13:53:18 -08004948 return nested_vmx_failInvalid(vcpu);
4949
KarimAllah Ahmed2e408932019-01-31 21:24:31 +01004950 if (kvm_read_guest(vcpu->kvm, vmptr, &revision, sizeof(revision)) ||
4951 revision != VMCS12_REVISION)
Sean Christopherson55d23752018-12-03 13:53:18 -08004952 return nested_vmx_failInvalid(vcpu);
4953
Sean Christopherson55d23752018-12-03 13:53:18 -08004954 vmx->nested.vmxon_ptr = vmptr;
4955 ret = enter_vmx_operation(vcpu);
4956 if (ret)
4957 return ret;
4958
4959 return nested_vmx_succeed(vcpu);
4960}
4961
4962static inline void nested_release_vmcs12(struct kvm_vcpu *vcpu)
4963{
4964 struct vcpu_vmx *vmx = to_vmx(vcpu);
4965
Yu Zhang64c78502021-09-30 01:51:53 +08004966 if (vmx->nested.current_vmptr == INVALID_GPA)
Sean Christopherson55d23752018-12-03 13:53:18 -08004967 return;
4968
Sean Christopherson7952d762019-05-07 08:36:29 -07004969 copy_vmcs02_to_vmcs12_rare(vcpu, get_vmcs12(vcpu));
4970
Sean Christopherson55d23752018-12-03 13:53:18 -08004971 if (enable_shadow_vmcs) {
4972 /* copy to memory all shadowed fields in case
4973 they were modified */
4974 copy_shadow_to_vmcs12(vmx);
Sean Christopherson55d23752018-12-03 13:53:18 -08004975 vmx_disable_shadow_vmcs(vmx);
4976 }
4977 vmx->nested.posted_intr_nv = -1;
4978
4979 /* Flush VMCS12 to guest memory */
4980 kvm_vcpu_write_guest_page(vcpu,
4981 vmx->nested.current_vmptr >> PAGE_SHIFT,
4982 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
4983
4984 kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
4985
Yu Zhang64c78502021-09-30 01:51:53 +08004986 vmx->nested.current_vmptr = INVALID_GPA;
Sean Christopherson55d23752018-12-03 13:53:18 -08004987}
4988
4989/* Emulate the VMXOFF instruction */
4990static int handle_vmoff(struct kvm_vcpu *vcpu)
4991{
4992 if (!nested_vmx_check_permission(vcpu))
4993 return 1;
Liran Alon4b9852f2019-08-26 13:24:49 +03004994
Sean Christopherson55d23752018-12-03 13:53:18 -08004995 free_nested(vcpu);
Liran Alon4b9852f2019-08-26 13:24:49 +03004996
4997 /* Process a latched INIT during time CPU was in VMX operation */
4998 kvm_make_request(KVM_REQ_EVENT, vcpu);
4999
Sean Christopherson55d23752018-12-03 13:53:18 -08005000 return nested_vmx_succeed(vcpu);
5001}
5002
5003/* Emulate the VMCLEAR instruction */
5004static int handle_vmclear(struct kvm_vcpu *vcpu)
5005{
5006 struct vcpu_vmx *vmx = to_vmx(vcpu);
5007 u32 zero = 0;
5008 gpa_t vmptr;
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02005009 u64 evmcs_gpa;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005010 int r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005011
5012 if (!nested_vmx_check_permission(vcpu))
5013 return 1;
5014
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005015 if (nested_vmx_get_vmptr(vcpu, &vmptr, &r))
5016 return r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005017
KarimAllah Ahmede0bf2662019-01-31 21:24:43 +01005018 if (!page_address_valid(vcpu, vmptr))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005019 return nested_vmx_fail(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
Sean Christopherson55d23752018-12-03 13:53:18 -08005020
5021 if (vmptr == vmx->nested.vmxon_ptr)
Sean Christophersonb2656e42020-06-08 18:56:07 -07005022 return nested_vmx_fail(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
Sean Christopherson55d23752018-12-03 13:53:18 -08005023
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02005024 /*
5025 * When Enlightened VMEntry is enabled on the calling CPU we treat
5026 * memory area pointer by vmptr as Enlightened VMCS (as there's no good
5027 * way to distinguish it from VMCS12) and we must not corrupt it by
5028 * writing to the non-existent 'launch_state' field. The area doesn't
5029 * have to be the currently active EVMCS on the calling CPU and there's
5030 * nothing KVM has to do to transition it from 'active' to 'non-active'
5031 * state. It is possible that the area will stay mapped as
5032 * vmx->nested.hv_evmcs but this shouldn't be a problem.
5033 */
5034 if (likely(!vmx->nested.enlightened_vmcs_enabled ||
5035 !nested_enlightened_vmentry(vcpu, &evmcs_gpa))) {
Sean Christopherson55d23752018-12-03 13:53:18 -08005036 if (vmptr == vmx->nested.current_vmptr)
5037 nested_release_vmcs12(vcpu);
5038
5039 kvm_vcpu_write_guest(vcpu,
5040 vmptr + offsetof(struct vmcs12,
5041 launch_state),
5042 &zero, sizeof(zero));
Vitaly Kuznetsov3b19b812021-05-26 15:20:21 +02005043 } else if (vmx->nested.hv_evmcs && vmptr == vmx->nested.hv_evmcs_vmptr) {
5044 nested_release_evmcs(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005045 }
5046
5047 return nested_vmx_succeed(vcpu);
5048}
5049
Sean Christopherson55d23752018-12-03 13:53:18 -08005050/* Emulate the VMLAUNCH instruction */
5051static int handle_vmlaunch(struct kvm_vcpu *vcpu)
5052{
5053 return nested_vmx_run(vcpu, true);
5054}
5055
5056/* Emulate the VMRESUME instruction */
5057static int handle_vmresume(struct kvm_vcpu *vcpu)
5058{
5059
5060 return nested_vmx_run(vcpu, false);
5061}
5062
5063static int handle_vmread(struct kvm_vcpu *vcpu)
5064{
Jim Mattsondd2d6042019-12-06 15:46:35 -08005065 struct vmcs12 *vmcs12 = is_guest_mode(vcpu) ? get_shadow_vmcs12(vcpu)
5066 : get_vmcs12(vcpu);
Sean Christopherson5addc232020-04-15 13:34:53 -07005067 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005068 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5069 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzinif7eea632019-09-14 00:26:27 +02005070 struct x86_exception e;
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005071 unsigned long field;
5072 u64 value;
5073 gva_t gva = 0;
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07005074 short offset;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005075 int len, r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005076
5077 if (!nested_vmx_check_permission(vcpu))
5078 return 1;
5079
Jim Mattsondd2d6042019-12-06 15:46:35 -08005080 /*
Yu Zhang64c78502021-09-30 01:51:53 +08005081 * In VMX non-root operation, when the VMCS-link pointer is INVALID_GPA,
Jim Mattsondd2d6042019-12-06 15:46:35 -08005082 * any VMREAD sets the ALU flags for VMfailInvalid.
5083 */
Yu Zhang64c78502021-09-30 01:51:53 +08005084 if (vmx->nested.current_vmptr == INVALID_GPA ||
Jim Mattsondd2d6042019-12-06 15:46:35 -08005085 (is_guest_mode(vcpu) &&
Yu Zhang64c78502021-09-30 01:51:53 +08005086 get_vmcs12(vcpu)->vmcs_link_pointer == INVALID_GPA))
Sean Christopherson55d23752018-12-03 13:53:18 -08005087 return nested_vmx_failInvalid(vcpu);
5088
Sean Christopherson55d23752018-12-03 13:53:18 -08005089 /* Decode instruction info and find the field to read */
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005090 field = kvm_register_read(vcpu, (((instr_info) >> 28) & 0xf));
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07005091
5092 offset = vmcs_field_to_offset(field);
5093 if (offset < 0)
Sean Christophersonb2656e42020-06-08 18:56:07 -07005094 return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Sean Christopherson55d23752018-12-03 13:53:18 -08005095
Sean Christopherson7952d762019-05-07 08:36:29 -07005096 if (!is_guest_mode(vcpu) && is_vmcs12_ext_field(field))
5097 copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
5098
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005099 /* Read the field, zero-extended to a u64 value */
5100 value = vmcs12_read_any(vmcs12, field, offset);
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07005101
Sean Christopherson55d23752018-12-03 13:53:18 -08005102 /*
5103 * Now copy part of this value to register or memory, as requested.
5104 * Note that the number of bits actually copied is 32 or 64 depending
5105 * on the guest's mode (32 or 64 bit), not on the given field's length.
5106 */
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005107 if (instr_info & BIT(10)) {
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005108 kvm_register_write(vcpu, (((instr_info) >> 3) & 0xf), value);
Sean Christopherson55d23752018-12-03 13:53:18 -08005109 } else {
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005110 len = is_64_bit_mode(vcpu) ? 8 : 4;
Sean Christopherson55d23752018-12-03 13:53:18 -08005111 if (get_vmx_mem_address(vcpu, exit_qualification,
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005112 instr_info, true, len, &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08005113 return 1;
5114 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005115 r = kvm_write_guest_virt_system(vcpu, gva, &value, len, &e);
5116 if (r != X86EMUL_CONTINUE)
Babu Moger3f3393b2020-09-11 14:29:05 -05005117 return kvm_handle_memory_failure(vcpu, r, &e);
Sean Christopherson55d23752018-12-03 13:53:18 -08005118 }
5119
5120 return nested_vmx_succeed(vcpu);
5121}
5122
Sean Christophersone2174292019-05-07 08:36:28 -07005123static bool is_shadow_field_rw(unsigned long field)
5124{
5125 switch (field) {
5126#define SHADOW_FIELD_RW(x, y) case x:
5127#include "vmcs_shadow_fields.h"
5128 return true;
5129 default:
5130 break;
5131 }
5132 return false;
5133}
5134
5135static bool is_shadow_field_ro(unsigned long field)
5136{
5137 switch (field) {
5138#define SHADOW_FIELD_RO(x, y) case x:
5139#include "vmcs_shadow_fields.h"
5140 return true;
5141 default:
5142 break;
5143 }
5144 return false;
5145}
Sean Christopherson55d23752018-12-03 13:53:18 -08005146
5147static int handle_vmwrite(struct kvm_vcpu *vcpu)
5148{
Jim Mattsondd2d6042019-12-06 15:46:35 -08005149 struct vmcs12 *vmcs12 = is_guest_mode(vcpu) ? get_shadow_vmcs12(vcpu)
5150 : get_vmcs12(vcpu);
Sean Christopherson5addc232020-04-15 13:34:53 -07005151 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005152 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5153 struct vcpu_vmx *vmx = to_vmx(vcpu);
5154 struct x86_exception e;
5155 unsigned long field;
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07005156 short offset;
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005157 gva_t gva;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005158 int len, r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005159
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005160 /*
5161 * The value to write might be 32 or 64 bits, depending on L1's long
Sean Christopherson55d23752018-12-03 13:53:18 -08005162 * mode, and eventually we need to write that into a field of several
5163 * possible lengths. The code below first zero-extends the value to 64
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005164 * bit (value), and then copies only the appropriate number of
Sean Christopherson55d23752018-12-03 13:53:18 -08005165 * bits into the vmcs12 field.
5166 */
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005167 u64 value = 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08005168
5169 if (!nested_vmx_check_permission(vcpu))
5170 return 1;
5171
Jim Mattsondd2d6042019-12-06 15:46:35 -08005172 /*
Yu Zhang64c78502021-09-30 01:51:53 +08005173 * In VMX non-root operation, when the VMCS-link pointer is INVALID_GPA,
Jim Mattsondd2d6042019-12-06 15:46:35 -08005174 * any VMWRITE sets the ALU flags for VMfailInvalid.
5175 */
Yu Zhang64c78502021-09-30 01:51:53 +08005176 if (vmx->nested.current_vmptr == INVALID_GPA ||
Jim Mattsondd2d6042019-12-06 15:46:35 -08005177 (is_guest_mode(vcpu) &&
Yu Zhang64c78502021-09-30 01:51:53 +08005178 get_vmcs12(vcpu)->vmcs_link_pointer == INVALID_GPA))
Sean Christopherson55d23752018-12-03 13:53:18 -08005179 return nested_vmx_failInvalid(vcpu);
5180
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005181 if (instr_info & BIT(10))
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005182 value = kvm_register_read(vcpu, (((instr_info) >> 3) & 0xf));
Sean Christopherson55d23752018-12-03 13:53:18 -08005183 else {
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005184 len = is_64_bit_mode(vcpu) ? 8 : 4;
Sean Christopherson55d23752018-12-03 13:53:18 -08005185 if (get_vmx_mem_address(vcpu, exit_qualification,
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005186 instr_info, false, len, &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08005187 return 1;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005188 r = kvm_read_guest_virt(vcpu, gva, &value, len, &e);
5189 if (r != X86EMUL_CONTINUE)
Babu Moger3f3393b2020-09-11 14:29:05 -05005190 return kvm_handle_memory_failure(vcpu, r, &e);
Sean Christopherson55d23752018-12-03 13:53:18 -08005191 }
5192
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005193 field = kvm_register_read(vcpu, (((instr_info) >> 28) & 0xf));
Sean Christopherson55d23752018-12-03 13:53:18 -08005194
Jim Mattson693e02c2019-12-06 15:46:36 -08005195 offset = vmcs_field_to_offset(field);
5196 if (offset < 0)
Sean Christophersonb2656e42020-06-08 18:56:07 -07005197 return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Jim Mattson693e02c2019-12-06 15:46:36 -08005198
Sean Christopherson55d23752018-12-03 13:53:18 -08005199 /*
5200 * If the vCPU supports "VMWRITE to any supported field in the
5201 * VMCS," then the "read-only" fields are actually read/write.
5202 */
5203 if (vmcs_field_readonly(field) &&
5204 !nested_cpu_has_vmwrite_any_field(vcpu))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005205 return nested_vmx_fail(vcpu, VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Sean Christopherson55d23752018-12-03 13:53:18 -08005206
Jim Mattsondd2d6042019-12-06 15:46:35 -08005207 /*
5208 * Ensure vmcs12 is up-to-date before any VMWRITE that dirties
5209 * vmcs12, else we may crush a field or consume a stale value.
5210 */
5211 if (!is_guest_mode(vcpu) && !is_shadow_field_rw(field))
5212 copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08005213
5214 /*
Sean Christophersonb6437802019-05-07 08:36:24 -07005215 * Some Intel CPUs intentionally drop the reserved bits of the AR byte
5216 * fields on VMWRITE. Emulate this behavior to ensure consistent KVM
5217 * behavior regardless of the underlying hardware, e.g. if an AR_BYTE
5218 * field is intercepted for VMWRITE but not VMREAD (in L1), then VMREAD
5219 * from L1 will return a different value than VMREAD from L2 (L1 sees
5220 * the stripped down value, L2 sees the full value as stored by KVM).
Sean Christopherson55d23752018-12-03 13:53:18 -08005221 */
Sean Christophersonb6437802019-05-07 08:36:24 -07005222 if (field >= GUEST_ES_AR_BYTES && field <= GUEST_TR_AR_BYTES)
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005223 value &= 0x1f0ff;
Sean Christophersonb6437802019-05-07 08:36:24 -07005224
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005225 vmcs12_write_any(vmcs12, field, offset, value);
Sean Christopherson55d23752018-12-03 13:53:18 -08005226
5227 /*
Sean Christophersone2174292019-05-07 08:36:28 -07005228 * Do not track vmcs12 dirty-state if in guest-mode as we actually
5229 * dirty shadow vmcs12 instead of vmcs12. Fields that can be updated
5230 * by L1 without a vmexit are always updated in the vmcs02, i.e. don't
5231 * "dirty" vmcs12, all others go down the prepare_vmcs02() slow path.
Sean Christopherson55d23752018-12-03 13:53:18 -08005232 */
Sean Christophersone2174292019-05-07 08:36:28 -07005233 if (!is_guest_mode(vcpu) && !is_shadow_field_rw(field)) {
5234 /*
5235 * L1 can read these fields without exiting, ensure the
5236 * shadow VMCS is up-to-date.
5237 */
5238 if (enable_shadow_vmcs && is_shadow_field_ro(field)) {
5239 preempt_disable();
5240 vmcs_load(vmx->vmcs01.shadow_vmcs);
Sean Christophersonfadcead2019-05-07 08:36:23 -07005241
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005242 __vmcs_writel(field, value);
Sean Christophersonfadcead2019-05-07 08:36:23 -07005243
Sean Christophersone2174292019-05-07 08:36:28 -07005244 vmcs_clear(vmx->vmcs01.shadow_vmcs);
5245 vmcs_load(vmx->loaded_vmcs->vmcs);
5246 preempt_enable();
Sean Christopherson55d23752018-12-03 13:53:18 -08005247 }
Sean Christophersone2174292019-05-07 08:36:28 -07005248 vmx->nested.dirty_vmcs12 = true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005249 }
5250
5251 return nested_vmx_succeed(vcpu);
5252}
5253
5254static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
5255{
5256 vmx->nested.current_vmptr = vmptr;
5257 if (enable_shadow_vmcs) {
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07005258 secondary_exec_controls_setbit(vmx, SECONDARY_EXEC_SHADOW_VMCS);
Sean Christopherson55d23752018-12-03 13:53:18 -08005259 vmcs_write64(VMCS_LINK_POINTER,
5260 __pa(vmx->vmcs01.shadow_vmcs));
Sean Christopherson3731905ef2019-05-07 08:36:27 -07005261 vmx->nested.need_vmcs12_to_shadow_sync = true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005262 }
5263 vmx->nested.dirty_vmcs12 = true;
5264}
5265
5266/* Emulate the VMPTRLD instruction */
5267static int handle_vmptrld(struct kvm_vcpu *vcpu)
5268{
5269 struct vcpu_vmx *vmx = to_vmx(vcpu);
5270 gpa_t vmptr;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005271 int r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005272
5273 if (!nested_vmx_check_permission(vcpu))
5274 return 1;
5275
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005276 if (nested_vmx_get_vmptr(vcpu, &vmptr, &r))
5277 return r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005278
KarimAllah Ahmede0bf2662019-01-31 21:24:43 +01005279 if (!page_address_valid(vcpu, vmptr))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005280 return nested_vmx_fail(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
Sean Christopherson55d23752018-12-03 13:53:18 -08005281
5282 if (vmptr == vmx->nested.vmxon_ptr)
Sean Christophersonb2656e42020-06-08 18:56:07 -07005283 return nested_vmx_fail(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
Sean Christopherson55d23752018-12-03 13:53:18 -08005284
5285 /* Forbid normal VMPTRLD if Enlightened version was used */
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02005286 if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
Sean Christopherson55d23752018-12-03 13:53:18 -08005287 return 1;
5288
5289 if (vmx->nested.current_vmptr != vmptr) {
David Woodhousecee66662021-11-15 16:50:26 +00005290 struct gfn_to_hva_cache *ghc = &vmx->nested.vmcs12_cache;
5291 struct vmcs_hdr hdr;
Sean Christopherson55d23752018-12-03 13:53:18 -08005292
Paolo Bonzini8503fea2021-11-22 18:20:16 -05005293 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, vmptr, VMCS12_SIZE)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08005294 /*
5295 * Reads from an unbacked page return all 1s,
5296 * which means that the 32 bits located at the
5297 * given physical address won't match the required
5298 * VMCS12_REVISION identifier.
5299 */
Sean Christophersonb2656e42020-06-08 18:56:07 -07005300 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005301 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Sean Christopherson55d23752018-12-03 13:53:18 -08005302 }
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01005303
David Woodhousecee66662021-11-15 16:50:26 +00005304 if (kvm_read_guest_offset_cached(vcpu->kvm, ghc, &hdr,
5305 offsetof(struct vmcs12, hdr),
5306 sizeof(hdr))) {
5307 return nested_vmx_fail(vcpu,
5308 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
5309 }
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01005310
David Woodhousecee66662021-11-15 16:50:26 +00005311 if (hdr.revision_id != VMCS12_REVISION ||
5312 (hdr.shadow_vmcs &&
Sean Christopherson55d23752018-12-03 13:53:18 -08005313 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
Sean Christophersonb2656e42020-06-08 18:56:07 -07005314 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005315 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
5316 }
5317
5318 nested_release_vmcs12(vcpu);
5319
5320 /*
5321 * Load VMCS12 from guest memory since it is not already
5322 * cached.
5323 */
David Woodhousecee66662021-11-15 16:50:26 +00005324 if (kvm_read_guest_cached(vcpu->kvm, ghc, vmx->nested.cached_vmcs12,
5325 VMCS12_SIZE)) {
5326 return nested_vmx_fail(vcpu,
5327 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
5328 }
Sean Christopherson55d23752018-12-03 13:53:18 -08005329
5330 set_current_vmptr(vmx, vmptr);
5331 }
5332
5333 return nested_vmx_succeed(vcpu);
5334}
5335
5336/* Emulate the VMPTRST instruction */
5337static int handle_vmptrst(struct kvm_vcpu *vcpu)
5338{
Sean Christopherson5addc232020-04-15 13:34:53 -07005339 unsigned long exit_qual = vmx_get_exit_qual(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005340 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5341 gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr;
5342 struct x86_exception e;
5343 gva_t gva;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005344 int r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005345
5346 if (!nested_vmx_check_permission(vcpu))
5347 return 1;
5348
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02005349 if (unlikely(evmptr_is_valid(to_vmx(vcpu)->nested.hv_evmcs_vmptr)))
Sean Christopherson55d23752018-12-03 13:53:18 -08005350 return 1;
5351
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005352 if (get_vmx_mem_address(vcpu, exit_qual, instr_info,
5353 true, sizeof(gpa_t), &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08005354 return 1;
5355 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005356 r = kvm_write_guest_virt_system(vcpu, gva, (void *)&current_vmptr,
5357 sizeof(gpa_t), &e);
5358 if (r != X86EMUL_CONTINUE)
Babu Moger3f3393b2020-09-11 14:29:05 -05005359 return kvm_handle_memory_failure(vcpu, r, &e);
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005360
Sean Christopherson55d23752018-12-03 13:53:18 -08005361 return nested_vmx_succeed(vcpu);
5362}
5363
5364/* Emulate the INVEPT instruction */
5365static int handle_invept(struct kvm_vcpu *vcpu)
5366{
5367 struct vcpu_vmx *vmx = to_vmx(vcpu);
5368 u32 vmx_instruction_info, types;
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005369 unsigned long type, roots_to_free;
5370 struct kvm_mmu *mmu;
Sean Christopherson55d23752018-12-03 13:53:18 -08005371 gva_t gva;
5372 struct x86_exception e;
5373 struct {
5374 u64 eptp, gpa;
5375 } operand;
Vipin Sharma329bd562021-11-09 17:44:25 +00005376 int i, r, gpr_index;
Sean Christopherson55d23752018-12-03 13:53:18 -08005377
5378 if (!(vmx->nested.msrs.secondary_ctls_high &
5379 SECONDARY_EXEC_ENABLE_EPT) ||
5380 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
5381 kvm_queue_exception(vcpu, UD_VECTOR);
5382 return 1;
5383 }
5384
5385 if (!nested_vmx_check_permission(vcpu))
5386 return 1;
5387
5388 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Vipin Sharma329bd562021-11-09 17:44:25 +00005389 gpr_index = vmx_get_instr_info_reg2(vmx_instruction_info);
5390 type = kvm_register_read(vcpu, gpr_index);
Sean Christopherson55d23752018-12-03 13:53:18 -08005391
5392 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
5393
5394 if (type >= 32 || !(types & (1 << type)))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005395 return nested_vmx_fail(vcpu, VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Sean Christopherson55d23752018-12-03 13:53:18 -08005396
5397 /* According to the Intel VMX instruction reference, the memory
5398 * operand is read even if it isn't needed (e.g., for type==global)
5399 */
Sean Christopherson5addc232020-04-15 13:34:53 -07005400 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005401 vmx_instruction_info, false, sizeof(operand), &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08005402 return 1;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005403 r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e);
5404 if (r != X86EMUL_CONTINUE)
Babu Moger3f3393b2020-09-11 14:29:05 -05005405 return kvm_handle_memory_failure(vcpu, r, &e);
Sean Christopherson55d23752018-12-03 13:53:18 -08005406
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005407 /*
5408 * Nested EPT roots are always held through guest_mmu,
5409 * not root_mmu.
5410 */
5411 mmu = &vcpu->arch.guest_mmu;
5412
Sean Christopherson55d23752018-12-03 13:53:18 -08005413 switch (type) {
Sean Christopherson55d23752018-12-03 13:53:18 -08005414 case VMX_EPT_EXTENT_CONTEXT:
Sean Christophersoneed00302020-03-20 14:27:58 -07005415 if (!nested_vmx_check_eptp(vcpu, operand.eptp))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005416 return nested_vmx_fail(vcpu,
Sean Christophersoneed00302020-03-20 14:27:58 -07005417 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Sean Christophersonf8aa7e32020-03-20 14:27:59 -07005418
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005419 roots_to_free = 0;
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07005420 if (nested_ept_root_matches(mmu->root_hpa, mmu->root_pgd,
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005421 operand.eptp))
5422 roots_to_free |= KVM_MMU_ROOT_CURRENT;
5423
5424 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) {
5425 if (nested_ept_root_matches(mmu->prev_roots[i].hpa,
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07005426 mmu->prev_roots[i].pgd,
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005427 operand.eptp))
5428 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
5429 }
5430 break;
Sean Christophersoneed00302020-03-20 14:27:58 -07005431 case VMX_EPT_EXTENT_GLOBAL:
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005432 roots_to_free = KVM_MMU_ROOTS_ALL;
Sean Christopherson55d23752018-12-03 13:53:18 -08005433 break;
5434 default:
Sean Christophersonf9336e32020-05-04 08:35:06 -07005435 BUG();
Sean Christopherson55d23752018-12-03 13:53:18 -08005436 break;
5437 }
5438
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005439 if (roots_to_free)
5440 kvm_mmu_free_roots(vcpu, mmu, roots_to_free);
5441
Sean Christopherson55d23752018-12-03 13:53:18 -08005442 return nested_vmx_succeed(vcpu);
5443}
5444
5445static int handle_invvpid(struct kvm_vcpu *vcpu)
5446{
5447 struct vcpu_vmx *vmx = to_vmx(vcpu);
5448 u32 vmx_instruction_info;
5449 unsigned long type, types;
5450 gva_t gva;
5451 struct x86_exception e;
5452 struct {
5453 u64 vpid;
5454 u64 gla;
5455 } operand;
5456 u16 vpid02;
Vipin Sharma329bd562021-11-09 17:44:25 +00005457 int r, gpr_index;
Sean Christopherson55d23752018-12-03 13:53:18 -08005458
5459 if (!(vmx->nested.msrs.secondary_ctls_high &
5460 SECONDARY_EXEC_ENABLE_VPID) ||
5461 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
5462 kvm_queue_exception(vcpu, UD_VECTOR);
5463 return 1;
5464 }
5465
5466 if (!nested_vmx_check_permission(vcpu))
5467 return 1;
5468
5469 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Vipin Sharma329bd562021-11-09 17:44:25 +00005470 gpr_index = vmx_get_instr_info_reg2(vmx_instruction_info);
5471 type = kvm_register_read(vcpu, gpr_index);
Sean Christopherson55d23752018-12-03 13:53:18 -08005472
5473 types = (vmx->nested.msrs.vpid_caps &
5474 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
5475
5476 if (type >= 32 || !(types & (1 << type)))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005477 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005478 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
5479
5480 /* according to the intel vmx instruction reference, the memory
5481 * operand is read even if it isn't needed (e.g., for type==global)
5482 */
Sean Christopherson5addc232020-04-15 13:34:53 -07005483 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005484 vmx_instruction_info, false, sizeof(operand), &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08005485 return 1;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005486 r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e);
5487 if (r != X86EMUL_CONTINUE)
Babu Moger3f3393b2020-09-11 14:29:05 -05005488 return kvm_handle_memory_failure(vcpu, r, &e);
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005489
Sean Christopherson55d23752018-12-03 13:53:18 -08005490 if (operand.vpid >> 16)
Sean Christophersonb2656e42020-06-08 18:56:07 -07005491 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005492 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
5493
5494 vpid02 = nested_get_vpid02(vcpu);
5495 switch (type) {
5496 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
5497 if (!operand.vpid ||
5498 is_noncanonical_address(operand.gla, vcpu))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005499 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005500 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Sean Christophersonbc41d0c2020-03-20 14:28:09 -07005501 vpid_sync_vcpu_addr(vpid02, operand.gla);
Sean Christopherson55d23752018-12-03 13:53:18 -08005502 break;
5503 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
5504 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
5505 if (!operand.vpid)
Sean Christophersonb2656e42020-06-08 18:56:07 -07005506 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005507 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Sean Christopherson446ace42020-03-20 14:28:05 -07005508 vpid_sync_context(vpid02);
Sean Christopherson55d23752018-12-03 13:53:18 -08005509 break;
5510 case VMX_VPID_EXTENT_ALL_CONTEXT:
Sean Christopherson446ace42020-03-20 14:28:05 -07005511 vpid_sync_context(vpid02);
Sean Christopherson55d23752018-12-03 13:53:18 -08005512 break;
5513 default:
5514 WARN_ON_ONCE(1);
5515 return kvm_skip_emulated_instruction(vcpu);
5516 }
5517
Junaid Shahidd6e3f832020-03-20 14:28:00 -07005518 /*
5519 * Sync the shadow page tables if EPT is disabled, L1 is invalidating
Sean Christopherson25b62c62021-06-09 16:42:29 -07005520 * linear mappings for L2 (tagged with L2's VPID). Free all guest
5521 * roots as VPIDs are not tracked in the MMU role.
Junaid Shahidd6e3f832020-03-20 14:28:00 -07005522 *
5523 * Note, this operates on root_mmu, not guest_mmu, as L1 and L2 share
5524 * an MMU when EPT is disabled.
5525 *
5526 * TODO: sync only the affected SPTEs for INVDIVIDUAL_ADDR.
5527 */
5528 if (!enable_ept)
Sean Christopherson25b62c62021-06-09 16:42:29 -07005529 kvm_mmu_free_guest_mode_roots(vcpu, &vcpu->arch.root_mmu);
Junaid Shahidd6e3f832020-03-20 14:28:00 -07005530
Sean Christopherson55d23752018-12-03 13:53:18 -08005531 return nested_vmx_succeed(vcpu);
5532}
5533
5534static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
5535 struct vmcs12 *vmcs12)
5536{
Sean Christopherson2b3eaf82019-04-30 10:36:19 -07005537 u32 index = kvm_rcx_read(vcpu);
Sean Christophersonac6389a2020-03-02 18:02:38 -08005538 u64 new_eptp;
Sean Christopherson55d23752018-12-03 13:53:18 -08005539
Sean Christophersonc5ffd402021-06-09 16:42:35 -07005540 if (WARN_ON_ONCE(!nested_cpu_has_ept(vmcs12)))
Sean Christopherson55d23752018-12-03 13:53:18 -08005541 return 1;
Sean Christopherson55d23752018-12-03 13:53:18 -08005542 if (index >= VMFUNC_EPTP_ENTRIES)
5543 return 1;
5544
Sean Christopherson55d23752018-12-03 13:53:18 -08005545 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
Sean Christophersonac6389a2020-03-02 18:02:38 -08005546 &new_eptp, index * 8, 8))
Sean Christopherson55d23752018-12-03 13:53:18 -08005547 return 1;
5548
Sean Christopherson55d23752018-12-03 13:53:18 -08005549 /*
5550 * If the (L2) guest does a vmfunc to the currently
5551 * active ept pointer, we don't have to do anything else
5552 */
Sean Christophersonac6389a2020-03-02 18:02:38 -08005553 if (vmcs12->ept_pointer != new_eptp) {
5554 if (!nested_vmx_check_eptp(vcpu, new_eptp))
Sean Christopherson55d23752018-12-03 13:53:18 -08005555 return 1;
5556
Sean Christophersonac6389a2020-03-02 18:02:38 -08005557 vmcs12->ept_pointer = new_eptp;
Sean Christopherson39353ab2021-06-09 16:42:31 -07005558 nested_ept_new_eptp(vcpu);
Sean Christophersonc805f5d2021-03-04 17:10:57 -08005559
Sean Christopherson39353ab2021-06-09 16:42:31 -07005560 if (!nested_cpu_has_vpid(vmcs12))
5561 kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005562 }
5563
5564 return 0;
5565}
5566
5567static int handle_vmfunc(struct kvm_vcpu *vcpu)
5568{
5569 struct vcpu_vmx *vmx = to_vmx(vcpu);
5570 struct vmcs12 *vmcs12;
Sean Christopherson2b3eaf82019-04-30 10:36:19 -07005571 u32 function = kvm_rax_read(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005572
5573 /*
5574 * VMFUNC is only supported for nested guests, but we always enable the
5575 * secondary control for simplicity; for non-nested mode, fake that we
5576 * didn't by injecting #UD.
5577 */
5578 if (!is_guest_mode(vcpu)) {
5579 kvm_queue_exception(vcpu, UD_VECTOR);
5580 return 1;
5581 }
5582
5583 vmcs12 = get_vmcs12(vcpu);
Sean Christopherson546e8392021-06-09 16:42:34 -07005584
5585 /*
5586 * #UD on out-of-bounds function has priority over VM-Exit, and VMFUNC
5587 * is enabled in vmcs02 if and only if it's enabled in vmcs12.
5588 */
5589 if (WARN_ON_ONCE((function > 63) || !nested_cpu_has_vmfunc(vmcs12))) {
5590 kvm_queue_exception(vcpu, UD_VECTOR);
5591 return 1;
5592 }
5593
Sean Christopherson0e752252021-06-09 16:42:22 -07005594 if (!(vmcs12->vm_function_control & BIT_ULL(function)))
Sean Christopherson55d23752018-12-03 13:53:18 -08005595 goto fail;
5596
5597 switch (function) {
5598 case 0:
5599 if (nested_vmx_eptp_switching(vcpu, vmcs12))
5600 goto fail;
5601 break;
5602 default:
5603 goto fail;
5604 }
5605 return kvm_skip_emulated_instruction(vcpu);
5606
5607fail:
Sean Christopherson8e533242020-11-06 17:03:12 +08005608 /*
5609 * This is effectively a reflected VM-Exit, as opposed to a synthesized
5610 * nested VM-Exit. Pass the original exit reason, i.e. don't hardcode
5611 * EXIT_REASON_VMFUNC as the exit reason.
5612 */
5613 nested_vmx_vmexit(vcpu, vmx->exit_reason.full,
Sean Christopherson87915852020-04-15 13:34:54 -07005614 vmx_get_intr_info(vcpu),
Sean Christopherson5addc232020-04-15 13:34:53 -07005615 vmx_get_exit_qual(vcpu));
Sean Christopherson55d23752018-12-03 13:53:18 -08005616 return 1;
5617}
5618
Oliver Uptone71237d2020-02-04 15:26:30 -08005619/*
5620 * Return true if an IO instruction with the specified port and size should cause
5621 * a VM-exit into L1.
5622 */
5623bool nested_vmx_check_io_bitmaps(struct kvm_vcpu *vcpu, unsigned int port,
5624 int size)
Sean Christopherson55d23752018-12-03 13:53:18 -08005625{
Oliver Uptone71237d2020-02-04 15:26:30 -08005626 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005627 gpa_t bitmap, last_bitmap;
Sean Christopherson55d23752018-12-03 13:53:18 -08005628 u8 b;
5629
Yu Zhang64c78502021-09-30 01:51:53 +08005630 last_bitmap = INVALID_GPA;
Sean Christopherson55d23752018-12-03 13:53:18 -08005631 b = -1;
5632
5633 while (size > 0) {
5634 if (port < 0x8000)
5635 bitmap = vmcs12->io_bitmap_a;
5636 else if (port < 0x10000)
5637 bitmap = vmcs12->io_bitmap_b;
5638 else
5639 return true;
5640 bitmap += (port & 0x7fff) / 8;
5641
5642 if (last_bitmap != bitmap)
5643 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
5644 return true;
5645 if (b & (1 << (port & 7)))
5646 return true;
5647
5648 port++;
5649 size--;
5650 last_bitmap = bitmap;
5651 }
5652
5653 return false;
5654}
5655
Oliver Uptone71237d2020-02-04 15:26:30 -08005656static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
5657 struct vmcs12 *vmcs12)
5658{
5659 unsigned long exit_qualification;
Oliver Upton35a57132020-02-04 15:26:31 -08005660 unsigned short port;
Oliver Uptone71237d2020-02-04 15:26:30 -08005661 int size;
5662
5663 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
5664 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
5665
Sean Christopherson5addc232020-04-15 13:34:53 -07005666 exit_qualification = vmx_get_exit_qual(vcpu);
Oliver Uptone71237d2020-02-04 15:26:30 -08005667
5668 port = exit_qualification >> 16;
5669 size = (exit_qualification & 7) + 1;
5670
5671 return nested_vmx_check_io_bitmaps(vcpu, port, size);
5672}
5673
Sean Christopherson55d23752018-12-03 13:53:18 -08005674/*
Miaohe Lin463bfee2020-02-14 10:44:05 +08005675 * Return 1 if we should exit from L2 to L1 to handle an MSR access,
Sean Christopherson55d23752018-12-03 13:53:18 -08005676 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
5677 * disinterest in the current event (read or write a specific MSR) by using an
5678 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
5679 */
5680static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
Sean Christopherson8e533242020-11-06 17:03:12 +08005681 struct vmcs12 *vmcs12,
5682 union vmx_exit_reason exit_reason)
Sean Christopherson55d23752018-12-03 13:53:18 -08005683{
Sean Christopherson2b3eaf82019-04-30 10:36:19 -07005684 u32 msr_index = kvm_rcx_read(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005685 gpa_t bitmap;
5686
5687 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
5688 return true;
5689
5690 /*
5691 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
5692 * for the four combinations of read/write and low/high MSR numbers.
5693 * First we need to figure out which of the four to use:
5694 */
5695 bitmap = vmcs12->msr_bitmap;
Sean Christopherson8e533242020-11-06 17:03:12 +08005696 if (exit_reason.basic == EXIT_REASON_MSR_WRITE)
Sean Christopherson55d23752018-12-03 13:53:18 -08005697 bitmap += 2048;
5698 if (msr_index >= 0xc0000000) {
5699 msr_index -= 0xc0000000;
5700 bitmap += 1024;
5701 }
5702
5703 /* Then read the msr_index'th bit from this bitmap: */
5704 if (msr_index < 1024*8) {
5705 unsigned char b;
5706 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
5707 return true;
5708 return 1 & (b >> (msr_index & 7));
5709 } else
5710 return true; /* let L1 handle the wrong parameter */
5711}
5712
5713/*
5714 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
5715 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
5716 * intercept (via guest_host_mask etc.) the current event.
5717 */
5718static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
5719 struct vmcs12 *vmcs12)
5720{
Sean Christopherson5addc232020-04-15 13:34:53 -07005721 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005722 int cr = exit_qualification & 15;
5723 int reg;
5724 unsigned long val;
5725
5726 switch ((exit_qualification >> 4) & 3) {
5727 case 0: /* mov to cr */
5728 reg = (exit_qualification >> 8) & 15;
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005729 val = kvm_register_read(vcpu, reg);
Sean Christopherson55d23752018-12-03 13:53:18 -08005730 switch (cr) {
5731 case 0:
5732 if (vmcs12->cr0_guest_host_mask &
5733 (val ^ vmcs12->cr0_read_shadow))
5734 return true;
5735 break;
5736 case 3:
Sean Christopherson55d23752018-12-03 13:53:18 -08005737 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
5738 return true;
5739 break;
5740 case 4:
5741 if (vmcs12->cr4_guest_host_mask &
5742 (vmcs12->cr4_read_shadow ^ val))
5743 return true;
5744 break;
5745 case 8:
5746 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
5747 return true;
5748 break;
5749 }
5750 break;
5751 case 2: /* clts */
5752 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
5753 (vmcs12->cr0_read_shadow & X86_CR0_TS))
5754 return true;
5755 break;
5756 case 1: /* mov from cr */
5757 switch (cr) {
5758 case 3:
5759 if (vmcs12->cpu_based_vm_exec_control &
5760 CPU_BASED_CR3_STORE_EXITING)
5761 return true;
5762 break;
5763 case 8:
5764 if (vmcs12->cpu_based_vm_exec_control &
5765 CPU_BASED_CR8_STORE_EXITING)
5766 return true;
5767 break;
5768 }
5769 break;
5770 case 3: /* lmsw */
5771 /*
5772 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
5773 * cr0. Other attempted changes are ignored, with no exit.
5774 */
5775 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
5776 if (vmcs12->cr0_guest_host_mask & 0xe &
5777 (val ^ vmcs12->cr0_read_shadow))
5778 return true;
5779 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
5780 !(vmcs12->cr0_read_shadow & 0x1) &&
5781 (val & 0x1))
5782 return true;
5783 break;
5784 }
5785 return false;
5786}
5787
Sean Christopherson72add912021-04-12 16:21:42 +12005788static bool nested_vmx_exit_handled_encls(struct kvm_vcpu *vcpu,
5789 struct vmcs12 *vmcs12)
5790{
5791 u32 encls_leaf;
5792
5793 if (!guest_cpuid_has(vcpu, X86_FEATURE_SGX) ||
5794 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENCLS_EXITING))
5795 return false;
5796
5797 encls_leaf = kvm_rax_read(vcpu);
5798 if (encls_leaf > 62)
5799 encls_leaf = 63;
5800 return vmcs12->encls_exiting_bitmap & BIT_ULL(encls_leaf);
5801}
5802
Sean Christopherson55d23752018-12-03 13:53:18 -08005803static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
5804 struct vmcs12 *vmcs12, gpa_t bitmap)
5805{
5806 u32 vmx_instruction_info;
5807 unsigned long field;
5808 u8 b;
5809
5810 if (!nested_cpu_has_shadow_vmcs(vmcs12))
5811 return true;
5812
5813 /* Decode instruction info and find the field to access */
5814 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5815 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
5816
5817 /* Out-of-range fields always cause a VM exit from L2 to L1 */
5818 if (field >> 15)
5819 return true;
5820
5821 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
5822 return true;
5823
5824 return 1 & (b >> (field & 7));
5825}
5826
Oliver Uptonb045ae92020-04-14 22:47:45 +00005827static bool nested_vmx_exit_handled_mtf(struct vmcs12 *vmcs12)
5828{
5829 u32 entry_intr_info = vmcs12->vm_entry_intr_info_field;
5830
5831 if (nested_cpu_has_mtf(vmcs12))
5832 return true;
5833
5834 /*
5835 * An MTF VM-exit may be injected into the guest by setting the
5836 * interruption-type to 7 (other event) and the vector field to 0. Such
5837 * is the case regardless of the 'monitor trap flag' VM-execution
5838 * control.
5839 */
5840 return entry_intr_info == (INTR_INFO_VALID_MASK
5841 | INTR_TYPE_OTHER_EVENT);
5842}
5843
Sean Christopherson55d23752018-12-03 13:53:18 -08005844/*
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005845 * Return true if L0 wants to handle an exit from L2 regardless of whether or not
5846 * L1 wants the exit. Only call this when in is_guest_mode (L2).
Sean Christopherson55d23752018-12-03 13:53:18 -08005847 */
Sean Christopherson8e533242020-11-06 17:03:12 +08005848static bool nested_vmx_l0_wants_exit(struct kvm_vcpu *vcpu,
5849 union vmx_exit_reason exit_reason)
Sean Christopherson55d23752018-12-03 13:53:18 -08005850{
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005851 u32 intr_info;
5852
Sean Christopherson8e533242020-11-06 17:03:12 +08005853 switch ((u16)exit_reason.basic) {
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005854 case EXIT_REASON_EXCEPTION_NMI:
Sean Christopherson87915852020-04-15 13:34:54 -07005855 intr_info = vmx_get_intr_info(vcpu);
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005856 if (is_nmi(intr_info))
5857 return true;
5858 else if (is_page_fault(intr_info))
Sean Christopherson18712c12021-08-11 21:56:15 -07005859 return vcpu->arch.apf.host_apf_flags ||
5860 vmx_need_pf_intercept(vcpu);
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005861 else if (is_debug(intr_info) &&
5862 vcpu->guest_debug &
5863 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5864 return true;
5865 else if (is_breakpoint(intr_info) &&
5866 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
5867 return true;
Sean Christophersonb33bb782021-06-22 10:22:44 -07005868 else if (is_alignment_check(intr_info) &&
5869 !vmx_guest_inject_ac(vcpu))
5870 return true;
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005871 return false;
5872 case EXIT_REASON_EXTERNAL_INTERRUPT:
5873 return true;
5874 case EXIT_REASON_MCE_DURING_VMENTRY:
5875 return true;
5876 case EXIT_REASON_EPT_VIOLATION:
5877 /*
5878 * L0 always deals with the EPT violation. If nested EPT is
5879 * used, and the nested mmu code discovers that the address is
5880 * missing in the guest EPT table (EPT12), the EPT violation
5881 * will be injected with nested_ept_inject_page_fault()
5882 */
5883 return true;
5884 case EXIT_REASON_EPT_MISCONFIG:
5885 /*
5886 * L2 never uses directly L1's EPT, but rather L0's own EPT
5887 * table (shadow on EPT) or a merged EPT table that L0 built
5888 * (EPT on EPT). So any problems with the structure of the
5889 * table is L0's fault.
5890 */
5891 return true;
5892 case EXIT_REASON_PREEMPTION_TIMER:
5893 return true;
5894 case EXIT_REASON_PML_FULL:
Sean Christophersonc3bb9a22021-02-12 16:50:07 -08005895 /*
5896 * PML is emulated for an L1 VMM and should never be enabled in
5897 * vmcs02, always "handle" PML_FULL by exiting to userspace.
5898 */
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005899 return true;
5900 case EXIT_REASON_VMFUNC:
5901 /* VM functions are emulated through L2->L0 vmexits. */
5902 return true;
Chenyi Qiang24a996a2021-09-14 17:50:41 +08005903 case EXIT_REASON_BUS_LOCK:
5904 /*
5905 * At present, bus lock VM exit is never exposed to L1.
5906 * Handle L2's bus locks in L0 directly.
5907 */
5908 return true;
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005909 default:
5910 break;
5911 }
5912 return false;
5913}
5914
5915/*
5916 * Return 1 if L1 wants to intercept an exit from L2. Only call this when in
5917 * is_guest_mode (L2).
5918 */
Sean Christopherson8e533242020-11-06 17:03:12 +08005919static bool nested_vmx_l1_wants_exit(struct kvm_vcpu *vcpu,
5920 union vmx_exit_reason exit_reason)
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005921{
Sean Christopherson55d23752018-12-03 13:53:18 -08005922 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Sean Christopherson9bd4af22020-04-21 00:53:27 -07005923 u32 intr_info;
Sean Christopherson55d23752018-12-03 13:53:18 -08005924
Sean Christopherson8e533242020-11-06 17:03:12 +08005925 switch ((u16)exit_reason.basic) {
Sean Christopherson55d23752018-12-03 13:53:18 -08005926 case EXIT_REASON_EXCEPTION_NMI:
Sean Christopherson87915852020-04-15 13:34:54 -07005927 intr_info = vmx_get_intr_info(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005928 if (is_nmi(intr_info))
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005929 return true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005930 else if (is_page_fault(intr_info))
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005931 return true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005932 return vmcs12->exception_bitmap &
5933 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
5934 case EXIT_REASON_EXTERNAL_INTERRUPT:
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005935 return nested_exit_on_intr(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005936 case EXIT_REASON_TRIPLE_FAULT:
5937 return true;
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08005938 case EXIT_REASON_INTERRUPT_WINDOW:
5939 return nested_cpu_has(vmcs12, CPU_BASED_INTR_WINDOW_EXITING);
Sean Christopherson55d23752018-12-03 13:53:18 -08005940 case EXIT_REASON_NMI_WINDOW:
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08005941 return nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING);
Sean Christopherson55d23752018-12-03 13:53:18 -08005942 case EXIT_REASON_TASK_SWITCH:
5943 return true;
5944 case EXIT_REASON_CPUID:
5945 return true;
5946 case EXIT_REASON_HLT:
5947 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
5948 case EXIT_REASON_INVD:
5949 return true;
5950 case EXIT_REASON_INVLPG:
5951 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
5952 case EXIT_REASON_RDPMC:
5953 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
5954 case EXIT_REASON_RDRAND:
5955 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
5956 case EXIT_REASON_RDSEED:
5957 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
5958 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
5959 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
5960 case EXIT_REASON_VMREAD:
5961 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
5962 vmcs12->vmread_bitmap);
5963 case EXIT_REASON_VMWRITE:
5964 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
5965 vmcs12->vmwrite_bitmap);
5966 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
5967 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
5968 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
5969 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
5970 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
5971 /*
5972 * VMX instructions trap unconditionally. This allows L1 to
5973 * emulate them for its L2 guest, i.e., allows 3-level nesting!
5974 */
5975 return true;
5976 case EXIT_REASON_CR_ACCESS:
5977 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
5978 case EXIT_REASON_DR_ACCESS:
5979 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
5980 case EXIT_REASON_IO_INSTRUCTION:
5981 return nested_vmx_exit_handled_io(vcpu, vmcs12);
5982 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
5983 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
5984 case EXIT_REASON_MSR_READ:
5985 case EXIT_REASON_MSR_WRITE:
5986 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
5987 case EXIT_REASON_INVALID_STATE:
5988 return true;
5989 case EXIT_REASON_MWAIT_INSTRUCTION:
5990 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
5991 case EXIT_REASON_MONITOR_TRAP_FLAG:
Oliver Uptonb045ae92020-04-14 22:47:45 +00005992 return nested_vmx_exit_handled_mtf(vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08005993 case EXIT_REASON_MONITOR_INSTRUCTION:
5994 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
5995 case EXIT_REASON_PAUSE_INSTRUCTION:
5996 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
5997 nested_cpu_has2(vmcs12,
5998 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
5999 case EXIT_REASON_MCE_DURING_VMENTRY:
Sean Christopherson2c1f3322020-04-15 10:55:14 -07006000 return true;
Sean Christopherson55d23752018-12-03 13:53:18 -08006001 case EXIT_REASON_TPR_BELOW_THRESHOLD:
6002 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
6003 case EXIT_REASON_APIC_ACCESS:
6004 case EXIT_REASON_APIC_WRITE:
6005 case EXIT_REASON_EOI_INDUCED:
6006 /*
6007 * The controls for "virtualize APIC accesses," "APIC-
6008 * register virtualization," and "virtual-interrupt
6009 * delivery" only come from vmcs12.
6010 */
6011 return true;
Sean Christopherson55d23752018-12-03 13:53:18 -08006012 case EXIT_REASON_INVPCID:
6013 return
6014 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
6015 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
6016 case EXIT_REASON_WBINVD:
6017 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
6018 case EXIT_REASON_XSETBV:
6019 return true;
6020 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
6021 /*
6022 * This should never happen, since it is not possible to
6023 * set XSS to a non-zero value---neither in L1 nor in L2.
6024 * If if it were, XSS would have to be checked against
6025 * the XSS exit bitmap in vmcs12.
6026 */
6027 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Tao Xubf653b72019-07-16 14:55:51 +08006028 case EXIT_REASON_UMWAIT:
6029 case EXIT_REASON_TPAUSE:
6030 return nested_cpu_has2(vmcs12,
6031 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE);
Sean Christopherson72add912021-04-12 16:21:42 +12006032 case EXIT_REASON_ENCLS:
6033 return nested_vmx_exit_handled_encls(vcpu, vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08006034 default:
6035 return true;
6036 }
6037}
6038
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006039/*
6040 * Conditionally reflect a VM-Exit into L1. Returns %true if the VM-Exit was
6041 * reflected into L1.
6042 */
Sean Christophersonf47baae2020-04-15 10:55:16 -07006043bool nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu)
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006044{
Sean Christophersonfbdd5022020-04-15 10:55:12 -07006045 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson8e533242020-11-06 17:03:12 +08006046 union vmx_exit_reason exit_reason = vmx->exit_reason;
Sean Christopherson87796552020-04-22 17:11:27 -07006047 unsigned long exit_qual;
6048 u32 exit_intr_info;
Sean Christophersonfbdd5022020-04-15 10:55:12 -07006049
6050 WARN_ON_ONCE(vmx->nested.nested_run_pending);
6051
6052 /*
6053 * Late nested VM-Fail shares the same flow as nested VM-Exit since KVM
6054 * has already loaded L2's state.
6055 */
6056 if (unlikely(vmx->fail)) {
6057 trace_kvm_nested_vmenter_failed(
6058 "hardware VM-instruction error: ",
6059 vmcs_read32(VM_INSTRUCTION_ERROR));
6060 exit_intr_info = 0;
6061 exit_qual = 0;
6062 goto reflect_vmexit;
6063 }
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006064
David Edmondson0a62a032021-09-20 11:37:35 +01006065 trace_kvm_nested_vmexit(vcpu, KVM_ISA_VMX);
Sean Christopherson236871b2020-04-15 10:55:13 -07006066
Sean Christopherson2c1f3322020-04-15 10:55:14 -07006067 /* If L0 (KVM) wants the exit, it trumps L1's desires. */
6068 if (nested_vmx_l0_wants_exit(vcpu, exit_reason))
6069 return false;
6070
6071 /* If L1 doesn't want the exit, handle it in L0. */
6072 if (!nested_vmx_l1_wants_exit(vcpu, exit_reason))
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006073 return false;
6074
6075 /*
Sean Christopherson1d283062020-04-15 10:55:15 -07006076 * vmcs.VM_EXIT_INTR_INFO is only valid for EXCEPTION_NMI exits. For
6077 * EXTERNAL_INTERRUPT, the value for vmcs12->vm_exit_intr_info would
6078 * need to be synthesized by querying the in-kernel LAPIC, but external
6079 * interrupts are never reflected to L1 so it's a non-issue.
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006080 */
Sean Christopherson02f19652020-09-23 13:13:49 -07006081 exit_intr_info = vmx_get_intr_info(vcpu);
Sean Christophersonf315f2b2020-09-23 13:13:45 -07006082 if (is_exception_with_error_code(exit_intr_info)) {
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006083 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6084
6085 vmcs12->vm_exit_intr_error_code =
6086 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
6087 }
Sean Christopherson02f19652020-09-23 13:13:49 -07006088 exit_qual = vmx_get_exit_qual(vcpu);
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006089
Sean Christophersonfbdd5022020-04-15 10:55:12 -07006090reflect_vmexit:
Sean Christopherson8e533242020-11-06 17:03:12 +08006091 nested_vmx_vmexit(vcpu, exit_reason.full, exit_intr_info, exit_qual);
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006092 return true;
6093}
Sean Christopherson55d23752018-12-03 13:53:18 -08006094
6095static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
6096 struct kvm_nested_state __user *user_kvm_nested_state,
6097 u32 user_data_size)
6098{
6099 struct vcpu_vmx *vmx;
6100 struct vmcs12 *vmcs12;
6101 struct kvm_nested_state kvm_state = {
6102 .flags = 0,
Liran Alon6ca00df2019-06-16 15:03:10 +03006103 .format = KVM_STATE_NESTED_FORMAT_VMX,
Sean Christopherson55d23752018-12-03 13:53:18 -08006104 .size = sizeof(kvm_state),
Peter Shier850448f2020-05-26 14:51:06 -07006105 .hdr.vmx.flags = 0,
Yu Zhang64c78502021-09-30 01:51:53 +08006106 .hdr.vmx.vmxon_pa = INVALID_GPA,
6107 .hdr.vmx.vmcs12_pa = INVALID_GPA,
Peter Shier850448f2020-05-26 14:51:06 -07006108 .hdr.vmx.preemption_timer_deadline = 0,
Sean Christopherson55d23752018-12-03 13:53:18 -08006109 };
Liran Alon6ca00df2019-06-16 15:03:10 +03006110 struct kvm_vmx_nested_state_data __user *user_vmx_nested_state =
6111 &user_kvm_nested_state->data.vmx[0];
Sean Christopherson55d23752018-12-03 13:53:18 -08006112
6113 if (!vcpu)
Liran Alon6ca00df2019-06-16 15:03:10 +03006114 return kvm_state.size + sizeof(*user_vmx_nested_state);
Sean Christopherson55d23752018-12-03 13:53:18 -08006115
6116 vmx = to_vmx(vcpu);
6117 vmcs12 = get_vmcs12(vcpu);
6118
Sean Christopherson55d23752018-12-03 13:53:18 -08006119 if (nested_vmx_allowed(vcpu) &&
6120 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
Liran Alon6ca00df2019-06-16 15:03:10 +03006121 kvm_state.hdr.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
6122 kvm_state.hdr.vmx.vmcs12_pa = vmx->nested.current_vmptr;
Sean Christopherson55d23752018-12-03 13:53:18 -08006123
6124 if (vmx_has_valid_vmcs12(vcpu)) {
Liran Alon6ca00df2019-06-16 15:03:10 +03006125 kvm_state.size += sizeof(user_vmx_nested_state->vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08006126
Vitaly Kuznetsov27849962021-05-26 15:20:20 +02006127 /* 'hv_evmcs_vmptr' can also be EVMPTR_MAP_PENDING here */
6128 if (vmx->nested.hv_evmcs_vmptr != EVMPTR_INVALID)
Liran Alon323d73a2019-06-26 16:09:27 +03006129 kvm_state.flags |= KVM_STATE_NESTED_EVMCS;
6130
Sean Christopherson55d23752018-12-03 13:53:18 -08006131 if (is_guest_mode(vcpu) &&
6132 nested_cpu_has_shadow_vmcs(vmcs12) &&
Yu Zhang64c78502021-09-30 01:51:53 +08006133 vmcs12->vmcs_link_pointer != INVALID_GPA)
Liran Alon6ca00df2019-06-16 15:03:10 +03006134 kvm_state.size += sizeof(user_vmx_nested_state->shadow_vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08006135 }
6136
6137 if (vmx->nested.smm.vmxon)
Liran Alon6ca00df2019-06-16 15:03:10 +03006138 kvm_state.hdr.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
Sean Christopherson55d23752018-12-03 13:53:18 -08006139
6140 if (vmx->nested.smm.guest_mode)
Liran Alon6ca00df2019-06-16 15:03:10 +03006141 kvm_state.hdr.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
Sean Christopherson55d23752018-12-03 13:53:18 -08006142
6143 if (is_guest_mode(vcpu)) {
6144 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
6145
6146 if (vmx->nested.nested_run_pending)
6147 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
Oliver Upton5ef8acb2020-02-07 02:36:07 -08006148
6149 if (vmx->nested.mtf_pending)
6150 kvm_state.flags |= KVM_STATE_NESTED_MTF_PENDING;
Peter Shier850448f2020-05-26 14:51:06 -07006151
6152 if (nested_cpu_has_preemption_timer(vmcs12) &&
6153 vmx->nested.has_preemption_timer_deadline) {
6154 kvm_state.hdr.vmx.flags |=
6155 KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE;
6156 kvm_state.hdr.vmx.preemption_timer_deadline =
6157 vmx->nested.preemption_timer_deadline;
6158 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006159 }
6160 }
6161
6162 if (user_data_size < kvm_state.size)
6163 goto out;
6164
6165 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
6166 return -EFAULT;
6167
6168 if (!vmx_has_valid_vmcs12(vcpu))
6169 goto out;
6170
6171 /*
6172 * When running L2, the authoritative vmcs12 state is in the
6173 * vmcs02. When running L1, the authoritative vmcs12 state is
6174 * in the shadow or enlightened vmcs linked to vmcs01, unless
Sean Christopherson3731905ef2019-05-07 08:36:27 -07006175 * need_vmcs12_to_shadow_sync is set, in which case, the authoritative
Sean Christopherson55d23752018-12-03 13:53:18 -08006176 * vmcs12 state is in the vmcs12 already.
6177 */
6178 if (is_guest_mode(vcpu)) {
Sean Christopherson3731905ef2019-05-07 08:36:27 -07006179 sync_vmcs02_to_vmcs12(vcpu, vmcs12);
Sean Christopherson7952d762019-05-07 08:36:29 -07006180 sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
Maxim Levitskyd51e1d32021-01-14 22:54:47 +02006181 } else {
6182 copy_vmcs02_to_vmcs12_rare(vcpu, get_vmcs12(vcpu));
6183 if (!vmx->nested.need_vmcs12_to_shadow_sync) {
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02006184 if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02006185 /*
6186 * L1 hypervisor is not obliged to keep eVMCS
6187 * clean fields data always up-to-date while
6188 * not in guest mode, 'hv_clean_fields' is only
6189 * supposed to be actual upon vmentry so we need
6190 * to ignore it here and do full copy.
6191 */
6192 copy_enlightened_to_vmcs12(vmx, 0);
Maxim Levitskyd51e1d32021-01-14 22:54:47 +02006193 else if (enable_shadow_vmcs)
6194 copy_shadow_to_vmcs12(vmx);
6195 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006196 }
6197
Liran Alon6ca00df2019-06-16 15:03:10 +03006198 BUILD_BUG_ON(sizeof(user_vmx_nested_state->vmcs12) < VMCS12_SIZE);
6199 BUILD_BUG_ON(sizeof(user_vmx_nested_state->shadow_vmcs12) < VMCS12_SIZE);
6200
Tom Roeder3a33d032019-01-24 13:48:20 -08006201 /*
6202 * Copy over the full allocated size of vmcs12 rather than just the size
6203 * of the struct.
6204 */
Liran Alon6ca00df2019-06-16 15:03:10 +03006205 if (copy_to_user(user_vmx_nested_state->vmcs12, vmcs12, VMCS12_SIZE))
Sean Christopherson55d23752018-12-03 13:53:18 -08006206 return -EFAULT;
6207
6208 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
Yu Zhang64c78502021-09-30 01:51:53 +08006209 vmcs12->vmcs_link_pointer != INVALID_GPA) {
Liran Alon6ca00df2019-06-16 15:03:10 +03006210 if (copy_to_user(user_vmx_nested_state->shadow_vmcs12,
Tom Roeder3a33d032019-01-24 13:48:20 -08006211 get_shadow_vmcs12(vcpu), VMCS12_SIZE))
Sean Christopherson55d23752018-12-03 13:53:18 -08006212 return -EFAULT;
6213 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006214out:
6215 return kvm_state.size;
6216}
6217
6218/*
6219 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
6220 */
6221void vmx_leave_nested(struct kvm_vcpu *vcpu)
6222{
6223 if (is_guest_mode(vcpu)) {
6224 to_vmx(vcpu)->nested.nested_run_pending = 0;
6225 nested_vmx_vmexit(vcpu, -1, 0, 0);
6226 }
6227 free_nested(vcpu);
6228}
6229
6230static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
6231 struct kvm_nested_state __user *user_kvm_nested_state,
6232 struct kvm_nested_state *kvm_state)
6233{
6234 struct vcpu_vmx *vmx = to_vmx(vcpu);
6235 struct vmcs12 *vmcs12;
Sean Christopherson68cda402020-05-11 15:05:29 -07006236 enum vm_entry_failure_code ignored;
Liran Alon6ca00df2019-06-16 15:03:10 +03006237 struct kvm_vmx_nested_state_data __user *user_vmx_nested_state =
6238 &user_kvm_nested_state->data.vmx[0];
Sean Christopherson55d23752018-12-03 13:53:18 -08006239 int ret;
6240
Liran Alon6ca00df2019-06-16 15:03:10 +03006241 if (kvm_state->format != KVM_STATE_NESTED_FORMAT_VMX)
Sean Christopherson55d23752018-12-03 13:53:18 -08006242 return -EINVAL;
6243
Yu Zhang64c78502021-09-30 01:51:53 +08006244 if (kvm_state->hdr.vmx.vmxon_pa == INVALID_GPA) {
Liran Alon6ca00df2019-06-16 15:03:10 +03006245 if (kvm_state->hdr.vmx.smm.flags)
Sean Christopherson55d23752018-12-03 13:53:18 -08006246 return -EINVAL;
6247
Yu Zhang64c78502021-09-30 01:51:53 +08006248 if (kvm_state->hdr.vmx.vmcs12_pa != INVALID_GPA)
Sean Christopherson55d23752018-12-03 13:53:18 -08006249 return -EINVAL;
6250
Liran Alon323d73a2019-06-26 16:09:27 +03006251 /*
6252 * KVM_STATE_NESTED_EVMCS used to signal that KVM should
6253 * enable eVMCS capability on vCPU. However, since then
6254 * code was changed such that flag signals vmcs12 should
6255 * be copied into eVMCS in guest memory.
6256 *
6257 * To preserve backwards compatability, allow user
6258 * to set this flag even when there is no VMXON region.
6259 */
Paolo Bonzini9fd58872019-06-19 16:52:27 +02006260 if (kvm_state->flags & ~KVM_STATE_NESTED_EVMCS)
6261 return -EINVAL;
6262 } else {
6263 if (!nested_vmx_allowed(vcpu))
6264 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08006265
Paolo Bonzini9fd58872019-06-19 16:52:27 +02006266 if (!page_address_valid(vcpu, kvm_state->hdr.vmx.vmxon_pa))
6267 return -EINVAL;
Liran Alon323d73a2019-06-26 16:09:27 +03006268 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006269
Liran Alon6ca00df2019-06-16 15:03:10 +03006270 if ((kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
Sean Christopherson55d23752018-12-03 13:53:18 -08006271 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
6272 return -EINVAL;
6273
Liran Alon6ca00df2019-06-16 15:03:10 +03006274 if (kvm_state->hdr.vmx.smm.flags &
Sean Christopherson55d23752018-12-03 13:53:18 -08006275 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
6276 return -EINVAL;
6277
Paolo Bonzini5e105c82020-07-27 08:55:09 -04006278 if (kvm_state->hdr.vmx.flags & ~KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE)
6279 return -EINVAL;
6280
Sean Christopherson55d23752018-12-03 13:53:18 -08006281 /*
6282 * SMM temporarily disables VMX, so we cannot be in guest mode,
6283 * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags
6284 * must be zero.
6285 */
Liran Alon65b712f12019-06-25 14:26:42 +03006286 if (is_smm(vcpu) ?
6287 (kvm_state->flags &
6288 (KVM_STATE_NESTED_GUEST_MODE | KVM_STATE_NESTED_RUN_PENDING))
6289 : kvm_state->hdr.vmx.smm.flags)
Sean Christopherson55d23752018-12-03 13:53:18 -08006290 return -EINVAL;
6291
Liran Alon6ca00df2019-06-16 15:03:10 +03006292 if ((kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
6293 !(kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
Sean Christopherson55d23752018-12-03 13:53:18 -08006294 return -EINVAL;
6295
Liran Alon323d73a2019-06-26 16:09:27 +03006296 if ((kvm_state->flags & KVM_STATE_NESTED_EVMCS) &&
6297 (!nested_vmx_allowed(vcpu) || !vmx->nested.enlightened_vmcs_enabled))
Paolo Bonzini9fd58872019-06-19 16:52:27 +02006298 return -EINVAL;
6299
Liran Alon323d73a2019-06-26 16:09:27 +03006300 vmx_leave_nested(vcpu);
Paolo Bonzini9fd58872019-06-19 16:52:27 +02006301
Yu Zhang64c78502021-09-30 01:51:53 +08006302 if (kvm_state->hdr.vmx.vmxon_pa == INVALID_GPA)
Sean Christopherson55d23752018-12-03 13:53:18 -08006303 return 0;
6304
Liran Alon6ca00df2019-06-16 15:03:10 +03006305 vmx->nested.vmxon_ptr = kvm_state->hdr.vmx.vmxon_pa;
Sean Christopherson55d23752018-12-03 13:53:18 -08006306 ret = enter_vmx_operation(vcpu);
6307 if (ret)
6308 return ret;
6309
Paolo Bonzini0f02bd02020-07-27 09:00:37 -04006310 /* Empty 'VMXON' state is permitted if no VMCS loaded */
6311 if (kvm_state->size < sizeof(*kvm_state) + sizeof(*vmcs12)) {
6312 /* See vmx_has_valid_vmcs12. */
6313 if ((kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE) ||
6314 (kvm_state->flags & KVM_STATE_NESTED_EVMCS) ||
Yu Zhang64c78502021-09-30 01:51:53 +08006315 (kvm_state->hdr.vmx.vmcs12_pa != INVALID_GPA))
Paolo Bonzini0f02bd02020-07-27 09:00:37 -04006316 return -EINVAL;
6317 else
6318 return 0;
6319 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006320
Yu Zhang64c78502021-09-30 01:51:53 +08006321 if (kvm_state->hdr.vmx.vmcs12_pa != INVALID_GPA) {
Liran Alon6ca00df2019-06-16 15:03:10 +03006322 if (kvm_state->hdr.vmx.vmcs12_pa == kvm_state->hdr.vmx.vmxon_pa ||
6323 !page_address_valid(vcpu, kvm_state->hdr.vmx.vmcs12_pa))
Sean Christopherson55d23752018-12-03 13:53:18 -08006324 return -EINVAL;
6325
Liran Alon6ca00df2019-06-16 15:03:10 +03006326 set_current_vmptr(vmx, kvm_state->hdr.vmx.vmcs12_pa);
Sean Christopherson55d23752018-12-03 13:53:18 -08006327 } else if (kvm_state->flags & KVM_STATE_NESTED_EVMCS) {
6328 /*
Vitaly Kuznetsove942dbf2020-03-09 16:52:12 +01006329 * nested_vmx_handle_enlightened_vmptrld() cannot be called
6330 * directly from here as HV_X64_MSR_VP_ASSIST_PAGE may not be
6331 * restored yet. EVMCS will be mapped from
6332 * nested_get_vmcs12_pages().
Sean Christopherson55d23752018-12-03 13:53:18 -08006333 */
Vitaly Kuznetsov27849962021-05-26 15:20:20 +02006334 vmx->nested.hv_evmcs_vmptr = EVMPTR_MAP_PENDING;
Paolo Bonzini729c15c2020-09-22 06:53:57 -04006335 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08006336 } else {
6337 return -EINVAL;
6338 }
6339
Liran Alon6ca00df2019-06-16 15:03:10 +03006340 if (kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
Sean Christopherson55d23752018-12-03 13:53:18 -08006341 vmx->nested.smm.vmxon = true;
6342 vmx->nested.vmxon = false;
6343
Liran Alon6ca00df2019-06-16 15:03:10 +03006344 if (kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
Sean Christopherson55d23752018-12-03 13:53:18 -08006345 vmx->nested.smm.guest_mode = true;
6346 }
6347
6348 vmcs12 = get_vmcs12(vcpu);
Liran Alon6ca00df2019-06-16 15:03:10 +03006349 if (copy_from_user(vmcs12, user_vmx_nested_state->vmcs12, sizeof(*vmcs12)))
Sean Christopherson55d23752018-12-03 13:53:18 -08006350 return -EFAULT;
6351
6352 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
6353 return -EINVAL;
6354
6355 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
6356 return 0;
6357
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006358 vmx->nested.nested_run_pending =
6359 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
6360
Oliver Upton5ef8acb2020-02-07 02:36:07 -08006361 vmx->nested.mtf_pending =
6362 !!(kvm_state->flags & KVM_STATE_NESTED_MTF_PENDING);
6363
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006364 ret = -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08006365 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
Yu Zhang64c78502021-09-30 01:51:53 +08006366 vmcs12->vmcs_link_pointer != INVALID_GPA) {
Sean Christopherson55d23752018-12-03 13:53:18 -08006367 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
6368
Liran Alon6ca00df2019-06-16 15:03:10 +03006369 if (kvm_state->size <
6370 sizeof(*kvm_state) +
6371 sizeof(user_vmx_nested_state->vmcs12) + sizeof(*shadow_vmcs12))
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006372 goto error_guest_mode;
Sean Christopherson55d23752018-12-03 13:53:18 -08006373
6374 if (copy_from_user(shadow_vmcs12,
Liran Alon6ca00df2019-06-16 15:03:10 +03006375 user_vmx_nested_state->shadow_vmcs12,
6376 sizeof(*shadow_vmcs12))) {
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006377 ret = -EFAULT;
6378 goto error_guest_mode;
6379 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006380
6381 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
6382 !shadow_vmcs12->hdr.shadow_vmcs)
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006383 goto error_guest_mode;
Sean Christopherson55d23752018-12-03 13:53:18 -08006384 }
6385
Paolo Bonzini83d31e52020-07-09 13:12:09 -04006386 vmx->nested.has_preemption_timer_deadline = false;
Peter Shier850448f2020-05-26 14:51:06 -07006387 if (kvm_state->hdr.vmx.flags & KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE) {
6388 vmx->nested.has_preemption_timer_deadline = true;
6389 vmx->nested.preemption_timer_deadline =
6390 kvm_state->hdr.vmx.preemption_timer_deadline;
6391 }
6392
Sean Christopherson5478ba32019-04-11 12:18:06 -07006393 if (nested_vmx_check_controls(vcpu, vmcs12) ||
6394 nested_vmx_check_host_state(vcpu, vmcs12) ||
Sean Christopherson68cda402020-05-11 15:05:29 -07006395 nested_vmx_check_guest_state(vcpu, vmcs12, &ignored))
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006396 goto error_guest_mode;
Sean Christopherson55d23752018-12-03 13:53:18 -08006397
6398 vmx->nested.dirty_vmcs12 = true;
6399 ret = nested_vmx_enter_non_root_mode(vcpu, false);
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006400 if (ret)
6401 goto error_guest_mode;
Sean Christopherson55d23752018-12-03 13:53:18 -08006402
6403 return 0;
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006404
6405error_guest_mode:
6406 vmx->nested.nested_run_pending = 0;
6407 return ret;
Sean Christopherson55d23752018-12-03 13:53:18 -08006408}
6409
Xiaoyao Li1b842922019-10-20 17:11:01 +08006410void nested_vmx_set_vmcs_shadowing_bitmap(void)
Sean Christopherson55d23752018-12-03 13:53:18 -08006411{
6412 if (enable_shadow_vmcs) {
Sean Christopherson55d23752018-12-03 13:53:18 -08006413 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Sean Christophersonfadcead2019-05-07 08:36:23 -07006414 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
Sean Christopherson55d23752018-12-03 13:53:18 -08006415 }
6416}
6417
6418/*
Sean Christophersonba1f8242021-06-18 14:46:58 -07006419 * Indexing into the vmcs12 uses the VMCS encoding rotated left by 6. Undo
6420 * that madness to get the encoding for comparison.
6421 */
6422#define VMCS12_IDX_TO_ENC(idx) ((u16)(((u16)(idx) >> 6) | ((u16)(idx) << 10)))
6423
6424static u64 nested_vmx_calc_vmcs_enum_msr(void)
6425{
6426 /*
6427 * Note these are the so called "index" of the VMCS field encoding, not
6428 * the index into vmcs12.
6429 */
6430 unsigned int max_idx, idx;
6431 int i;
6432
6433 /*
6434 * For better or worse, KVM allows VMREAD/VMWRITE to all fields in
6435 * vmcs12, regardless of whether or not the associated feature is
6436 * exposed to L1. Simply find the field with the highest index.
6437 */
6438 max_idx = 0;
6439 for (i = 0; i < nr_vmcs12_fields; i++) {
6440 /* The vmcs12 table is very, very sparsely populated. */
6441 if (!vmcs_field_to_offset_table[i])
6442 continue;
6443
6444 idx = vmcs_field_index(VMCS12_IDX_TO_ENC(i));
6445 if (idx > max_idx)
6446 max_idx = idx;
6447 }
6448
6449 return (u64)max_idx << VMCS_FIELD_INDEX_SHIFT;
6450}
6451
6452/*
Sean Christopherson55d23752018-12-03 13:53:18 -08006453 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
6454 * returned for the various VMX controls MSRs when nested VMX is enabled.
6455 * The same values should also be used to verify that vmcs12 control fields are
6456 * valid during nested entry from L1 to L2.
6457 * Each of these control msrs has a low and high 32-bit half: A low bit is on
6458 * if the corresponding bit in the (32-bit) control field *must* be on, and a
6459 * bit in the high half is on if the corresponding bit in the control field
6460 * may be on. See also vmx_control_verify().
6461 */
Vitaly Kuznetsova4443262020-02-20 18:22:04 +01006462void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, u32 ept_caps)
Sean Christopherson55d23752018-12-03 13:53:18 -08006463{
6464 /*
6465 * Note that as a general rule, the high half of the MSRs (bits in
6466 * the control fields which may be 1) should be initialized by the
6467 * intersection of the underlying hardware's MSR (i.e., features which
6468 * can be supported) and the list of features we want to expose -
6469 * because they are known to be properly supported in our code.
6470 * Also, usually, the low half of the MSRs (bits which must be 1) can
6471 * be set to 0, meaning that L1 may turn off any of these bits. The
6472 * reason is that if one of these bits is necessary, it will appear
6473 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
6474 * fields of vmcs01 and vmcs02, will turn these bits off - and
Sean Christopherson2c1f3322020-04-15 10:55:14 -07006475 * nested_vmx_l1_wants_exit() will not pass related exits to L1.
Sean Christopherson55d23752018-12-03 13:53:18 -08006476 * These rules have exceptions below.
6477 */
6478
6479 /* pin-based controls */
6480 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
6481 msrs->pinbased_ctls_low,
6482 msrs->pinbased_ctls_high);
6483 msrs->pinbased_ctls_low |=
6484 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
6485 msrs->pinbased_ctls_high &=
6486 PIN_BASED_EXT_INTR_MASK |
6487 PIN_BASED_NMI_EXITING |
6488 PIN_BASED_VIRTUAL_NMIS |
Vitaly Kuznetsova4443262020-02-20 18:22:04 +01006489 (enable_apicv ? PIN_BASED_POSTED_INTR : 0);
Sean Christopherson55d23752018-12-03 13:53:18 -08006490 msrs->pinbased_ctls_high |=
6491 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
6492 PIN_BASED_VMX_PREEMPTION_TIMER;
6493
6494 /* exit controls */
6495 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
6496 msrs->exit_ctls_low,
6497 msrs->exit_ctls_high);
6498 msrs->exit_ctls_low =
6499 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
6500
6501 msrs->exit_ctls_high &=
6502#ifdef CONFIG_X86_64
6503 VM_EXIT_HOST_ADDR_SPACE_SIZE |
6504#endif
Chenyi Qiangefc83132020-08-28 16:56:18 +08006505 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT |
6506 VM_EXIT_CLEAR_BNDCFGS | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
Sean Christopherson55d23752018-12-03 13:53:18 -08006507 msrs->exit_ctls_high |=
6508 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
6509 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
6510 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
6511
6512 /* We support free control of debug control saving. */
6513 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
6514
6515 /* entry controls */
6516 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
6517 msrs->entry_ctls_low,
6518 msrs->entry_ctls_high);
6519 msrs->entry_ctls_low =
6520 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
6521 msrs->entry_ctls_high &=
6522#ifdef CONFIG_X86_64
6523 VM_ENTRY_IA32E_MODE |
6524#endif
Chenyi Qiangefc83132020-08-28 16:56:18 +08006525 VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS |
6526 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
Sean Christopherson55d23752018-12-03 13:53:18 -08006527 msrs->entry_ctls_high |=
6528 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
6529
6530 /* We support free control of debug control loading. */
6531 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
6532
6533 /* cpu-based controls */
6534 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
6535 msrs->procbased_ctls_low,
6536 msrs->procbased_ctls_high);
6537 msrs->procbased_ctls_low =
6538 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
6539 msrs->procbased_ctls_high &=
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08006540 CPU_BASED_INTR_WINDOW_EXITING |
Xiaoyao Li5e3d3942019-12-06 16:45:26 +08006541 CPU_BASED_NMI_WINDOW_EXITING | CPU_BASED_USE_TSC_OFFSETTING |
Sean Christopherson55d23752018-12-03 13:53:18 -08006542 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
6543 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
6544 CPU_BASED_CR3_STORE_EXITING |
6545#ifdef CONFIG_X86_64
6546 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
6547#endif
6548 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
6549 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
6550 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
6551 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
6552 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
6553 /*
6554 * We can allow some features even when not supported by the
6555 * hardware. For example, L1 can specify an MSR bitmap - and we
6556 * can use it to avoid exits to L1 - even when L0 runs L2
6557 * without MSR bitmaps.
6558 */
6559 msrs->procbased_ctls_high |=
6560 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
6561 CPU_BASED_USE_MSR_BITMAPS;
6562
6563 /* We support free control of CR3 access interception. */
6564 msrs->procbased_ctls_low &=
6565 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
6566
6567 /*
6568 * secondary cpu-based controls. Do not include those that
Xiaoyao Li7c1b7612020-07-09 12:34:25 +08006569 * depend on CPUID bits, they are added later by
6570 * vmx_vcpu_after_set_cpuid.
Sean Christopherson55d23752018-12-03 13:53:18 -08006571 */
Vitaly Kuznetsov6b1971c2019-02-07 11:42:14 +01006572 if (msrs->procbased_ctls_high & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)
6573 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
6574 msrs->secondary_ctls_low,
6575 msrs->secondary_ctls_high);
6576
Sean Christopherson55d23752018-12-03 13:53:18 -08006577 msrs->secondary_ctls_low = 0;
6578 msrs->secondary_ctls_high &=
6579 SECONDARY_EXEC_DESC |
Sean Christopherson7f3603b2020-09-23 09:50:47 -07006580 SECONDARY_EXEC_ENABLE_RDTSCP |
Sean Christopherson55d23752018-12-03 13:53:18 -08006581 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini6defc592019-07-02 14:39:29 +02006582 SECONDARY_EXEC_WBINVD_EXITING |
Sean Christopherson55d23752018-12-03 13:53:18 -08006583 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6584 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini6defc592019-07-02 14:39:29 +02006585 SECONDARY_EXEC_RDRAND_EXITING |
6586 SECONDARY_EXEC_ENABLE_INVPCID |
6587 SECONDARY_EXEC_RDSEED_EXITING |
Ilias Stamatisd041b5e2021-05-26 19:44:17 +01006588 SECONDARY_EXEC_XSAVES |
6589 SECONDARY_EXEC_TSC_SCALING;
Sean Christopherson55d23752018-12-03 13:53:18 -08006590
6591 /*
6592 * We can emulate "VMCS shadowing," even if the hardware
6593 * doesn't support it.
6594 */
6595 msrs->secondary_ctls_high |=
6596 SECONDARY_EXEC_SHADOW_VMCS;
6597
6598 if (enable_ept) {
6599 /* nested EPT: emulate EPT also to L1 */
6600 msrs->secondary_ctls_high |=
6601 SECONDARY_EXEC_ENABLE_EPT;
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08006602 msrs->ept_caps =
6603 VMX_EPT_PAGE_WALK_4_BIT |
6604 VMX_EPT_PAGE_WALK_5_BIT |
6605 VMX_EPTP_WB_BIT |
Sean Christopherson96d47012020-03-02 18:02:40 -08006606 VMX_EPT_INVEPT_BIT |
6607 VMX_EPT_EXECUTE_ONLY_BIT;
6608
Sean Christopherson55d23752018-12-03 13:53:18 -08006609 msrs->ept_caps &= ept_caps;
6610 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
6611 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
6612 VMX_EPT_1GB_PAGE_BIT;
6613 if (enable_ept_ad_bits) {
6614 msrs->secondary_ctls_high |=
6615 SECONDARY_EXEC_ENABLE_PML;
6616 msrs->ept_caps |= VMX_EPT_AD_BIT;
6617 }
6618 }
6619
6620 if (cpu_has_vmx_vmfunc()) {
6621 msrs->secondary_ctls_high |=
6622 SECONDARY_EXEC_ENABLE_VMFUNC;
6623 /*
6624 * Advertise EPTP switching unconditionally
6625 * since we emulate it
6626 */
6627 if (enable_ept)
6628 msrs->vmfunc_controls =
6629 VMX_VMFUNC_EPTP_SWITCHING;
6630 }
6631
6632 /*
6633 * Old versions of KVM use the single-context version without
6634 * checking for support, so declare that it is supported even
6635 * though it is treated as global context. The alternative is
6636 * not failing the single-context invvpid, and it is worse.
6637 */
6638 if (enable_vpid) {
6639 msrs->secondary_ctls_high |=
6640 SECONDARY_EXEC_ENABLE_VPID;
6641 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
6642 VMX_VPID_EXTENT_SUPPORTED_MASK;
6643 }
6644
6645 if (enable_unrestricted_guest)
6646 msrs->secondary_ctls_high |=
6647 SECONDARY_EXEC_UNRESTRICTED_GUEST;
6648
6649 if (flexpriority_enabled)
6650 msrs->secondary_ctls_high |=
6651 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6652
Sean Christopherson72add912021-04-12 16:21:42 +12006653 if (enable_sgx)
6654 msrs->secondary_ctls_high |= SECONDARY_EXEC_ENCLS_EXITING;
6655
Sean Christopherson55d23752018-12-03 13:53:18 -08006656 /* miscellaneous data */
6657 rdmsr(MSR_IA32_VMX_MISC,
6658 msrs->misc_low,
6659 msrs->misc_high);
6660 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
6661 msrs->misc_low |=
6662 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
6663 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Yadong Qibf0cd882020-11-06 14:51:22 +08006664 VMX_MISC_ACTIVITY_HLT |
6665 VMX_MISC_ACTIVITY_WAIT_SIPI;
Sean Christopherson55d23752018-12-03 13:53:18 -08006666 msrs->misc_high = 0;
6667
6668 /*
6669 * This MSR reports some information about VMX support. We
6670 * should return information about the VMX we emulate for the
6671 * guest, and the VMCS structure we give it - not about the
6672 * VMX support of the underlying hardware.
6673 */
6674 msrs->basic =
6675 VMCS12_REVISION |
6676 VMX_BASIC_TRUE_CTLS |
6677 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
6678 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
6679
6680 if (cpu_has_vmx_basic_inout())
6681 msrs->basic |= VMX_BASIC_INOUT;
6682
6683 /*
6684 * These MSRs specify bits which the guest must keep fixed on
6685 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
6686 * We picked the standard core2 setting.
6687 */
6688#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
6689#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
6690 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
6691 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
6692
6693 /* These MSRs specify bits which the guest must keep fixed off. */
6694 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
6695 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
6696
Sean Christophersonba1f8242021-06-18 14:46:58 -07006697 msrs->vmcs_enum = nested_vmx_calc_vmcs_enum_msr();
Sean Christopherson55d23752018-12-03 13:53:18 -08006698}
6699
6700void nested_vmx_hardware_unsetup(void)
6701{
6702 int i;
6703
6704 if (enable_shadow_vmcs) {
6705 for (i = 0; i < VMX_BITMAP_NR; i++)
6706 free_page((unsigned long)vmx_bitmap[i]);
6707 }
6708}
6709
Sean Christopherson6c1c6e52020-05-06 13:46:53 -07006710__init int nested_vmx_hardware_setup(int (*exit_handlers[])(struct kvm_vcpu *))
Sean Christopherson55d23752018-12-03 13:53:18 -08006711{
6712 int i;
6713
6714 if (!cpu_has_vmx_shadow_vmcs())
6715 enable_shadow_vmcs = 0;
6716 if (enable_shadow_vmcs) {
6717 for (i = 0; i < VMX_BITMAP_NR; i++) {
Ben Gardon41836832019-02-11 11:02:52 -08006718 /*
6719 * The vmx_bitmap is not tied to a VM and so should
6720 * not be charged to a memcg.
6721 */
Sean Christopherson55d23752018-12-03 13:53:18 -08006722 vmx_bitmap[i] = (unsigned long *)
6723 __get_free_page(GFP_KERNEL);
6724 if (!vmx_bitmap[i]) {
6725 nested_vmx_hardware_unsetup();
6726 return -ENOMEM;
6727 }
6728 }
6729
6730 init_vmcs_shadow_fields();
6731 }
6732
Liran Aloncc877672019-11-18 21:11:21 +02006733 exit_handlers[EXIT_REASON_VMCLEAR] = handle_vmclear;
6734 exit_handlers[EXIT_REASON_VMLAUNCH] = handle_vmlaunch;
6735 exit_handlers[EXIT_REASON_VMPTRLD] = handle_vmptrld;
6736 exit_handlers[EXIT_REASON_VMPTRST] = handle_vmptrst;
6737 exit_handlers[EXIT_REASON_VMREAD] = handle_vmread;
6738 exit_handlers[EXIT_REASON_VMRESUME] = handle_vmresume;
6739 exit_handlers[EXIT_REASON_VMWRITE] = handle_vmwrite;
6740 exit_handlers[EXIT_REASON_VMOFF] = handle_vmoff;
6741 exit_handlers[EXIT_REASON_VMON] = handle_vmon;
6742 exit_handlers[EXIT_REASON_INVEPT] = handle_invept;
6743 exit_handlers[EXIT_REASON_INVVPID] = handle_invvpid;
6744 exit_handlers[EXIT_REASON_VMFUNC] = handle_vmfunc;
Sean Christopherson55d23752018-12-03 13:53:18 -08006745
Sean Christopherson55d23752018-12-03 13:53:18 -08006746 return 0;
6747}
Paolo Bonzini33b22172020-04-17 10:24:18 -04006748
6749struct kvm_x86_nested_ops vmx_nested_ops = {
6750 .check_events = vmx_check_nested_events,
Sean Christophersond2060bd2020-04-22 19:25:39 -07006751 .hv_timer_pending = nested_vmx_preemption_timer_pending,
Sean Christophersoncb6a32c2021-03-02 09:45:14 -08006752 .triple_fault = nested_vmx_triple_fault,
Paolo Bonzini33b22172020-04-17 10:24:18 -04006753 .get_state = vmx_get_nested_state,
6754 .set_state = vmx_set_nested_state,
Paolo Bonzini9a78e152021-01-08 11:43:08 -05006755 .get_nested_state_pages = vmx_get_nested_state_pages,
Sean Christopherson02f5fb22020-06-22 14:58:32 -07006756 .write_log_dirty = nested_vmx_write_pml_buffer,
Paolo Bonzini33b22172020-04-17 10:24:18 -04006757 .enable_evmcs = nested_enable_evmcs,
6758 .get_evmcs_version = nested_get_evmcs_version,
6759};