blob: 7f8184f432b498cdd3471383827940101135a193 [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 */
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +0200194 if (vmx->nested.current_vmptr == -1ull &&
195 !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);
Sean Christopherson55d23752018-12-03 13:53:18 -0800221 vmcs_write64(VMCS_LINK_POINTER, -1ull);
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;
293 free_vpid(vmx->nested.vpid02);
294 vmx->nested.posted_intr_nv = -1;
295 vmx->nested.current_vmptr = -1ull;
296 if (enable_shadow_vmcs) {
297 vmx_disable_shadow_vmcs(vmx);
298 vmcs_clear(vmx->vmcs01.shadow_vmcs);
299 free_vmcs(vmx->vmcs01.shadow_vmcs);
300 vmx->vmcs01.shadow_vmcs = NULL;
301 }
302 kfree(vmx->nested.cached_vmcs12);
Jan Kiszkac6bf2ae2019-07-21 16:01:36 +0200303 vmx->nested.cached_vmcs12 = NULL;
Sean Christopherson55d23752018-12-03 13:53:18 -0800304 kfree(vmx->nested.cached_shadow_vmcs12);
Jan Kiszkac6bf2ae2019-07-21 16:01:36 +0200305 vmx->nested.cached_shadow_vmcs12 = NULL;
Sean Christopherson55d23752018-12-03 13:53:18 -0800306 /* Unpin physical memory we referred to in the vmcs02 */
307 if (vmx->nested.apic_access_page) {
Liran Alonb11494b2019-11-21 00:31:47 +0200308 kvm_release_page_clean(vmx->nested.apic_access_page);
Sean Christopherson55d23752018-12-03 13:53:18 -0800309 vmx->nested.apic_access_page = NULL;
310 }
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +0100311 kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map, true);
KarimAllah Ahmed3278e042019-01-31 21:24:38 +0100312 kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map, true);
313 vmx->nested.pi_desc = NULL;
Sean Christopherson55d23752018-12-03 13:53:18 -0800314
315 kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
316
317 nested_release_evmcs(vcpu);
318
319 free_loaded_vmcs(&vmx->nested.vmcs02);
320}
321
Sean Christopherson55d23752018-12-03 13:53:18 -0800322/*
323 * Ensure that the current vmcs of the logical processor is the
324 * vmcs01 of the vcpu before calling free_nested().
325 */
326void nested_vmx_free_vcpu(struct kvm_vcpu *vcpu)
327{
328 vcpu_load(vcpu);
Paolo Bonzinib4b65b52019-01-29 19:12:35 +0100329 vmx_leave_nested(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -0800330 vcpu_put(vcpu);
331}
332
333static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
334 struct x86_exception *fault)
335{
336 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
337 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson4dcefa32020-04-15 10:55:18 -0700338 u32 vm_exit_reason;
Sean Christopherson55d23752018-12-03 13:53:18 -0800339 unsigned long exit_qualification = vcpu->arch.exit_qualification;
340
341 if (vmx->nested.pml_full) {
Sean Christopherson4dcefa32020-04-15 10:55:18 -0700342 vm_exit_reason = EXIT_REASON_PML_FULL;
Sean Christopherson55d23752018-12-03 13:53:18 -0800343 vmx->nested.pml_full = false;
344 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
345 } else if (fault->error_code & PFERR_RSVD_MASK)
Sean Christopherson4dcefa32020-04-15 10:55:18 -0700346 vm_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Sean Christopherson55d23752018-12-03 13:53:18 -0800347 else
Sean Christopherson4dcefa32020-04-15 10:55:18 -0700348 vm_exit_reason = EXIT_REASON_EPT_VIOLATION;
Sean Christopherson55d23752018-12-03 13:53:18 -0800349
Sean Christopherson4dcefa32020-04-15 10:55:18 -0700350 nested_vmx_vmexit(vcpu, vm_exit_reason, 0, exit_qualification);
Sean Christopherson55d23752018-12-03 13:53:18 -0800351 vmcs12->guest_physical_address = fault->address;
352}
353
Sean Christopherson39353ab2021-06-09 16:42:31 -0700354static void nested_ept_new_eptp(struct kvm_vcpu *vcpu)
355{
356 kvm_init_shadow_ept_mmu(vcpu,
357 to_vmx(vcpu)->nested.msrs.ept_caps &
358 VMX_EPT_EXECUTE_ONLY_BIT,
359 nested_ept_ad_enabled(vcpu),
360 nested_ept_get_eptp(vcpu));
361}
362
Sean Christopherson55d23752018-12-03 13:53:18 -0800363static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
364{
365 WARN_ON(mmu_is_nested(vcpu));
366
367 vcpu->arch.mmu = &vcpu->arch.guest_mmu;
Sean Christopherson39353ab2021-06-09 16:42:31 -0700368 nested_ept_new_eptp(vcpu);
Sean Christophersond8dd54e2020-03-02 18:02:39 -0800369 vcpu->arch.mmu->get_guest_pgd = nested_ept_get_eptp;
Sean Christopherson55d23752018-12-03 13:53:18 -0800370 vcpu->arch.mmu->inject_page_fault = nested_ept_inject_page_fault;
371 vcpu->arch.mmu->get_pdptr = kvm_pdptr_read;
372
373 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
374}
375
376static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
377{
378 vcpu->arch.mmu = &vcpu->arch.root_mmu;
379 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
380}
381
382static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
383 u16 error_code)
384{
385 bool inequality, bit;
386
387 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
388 inequality =
389 (error_code & vmcs12->page_fault_error_code_mask) !=
390 vmcs12->page_fault_error_code_match;
391 return inequality ^ bit;
392}
393
394
395/*
396 * KVM wants to inject page-faults which it got to the guest. This function
397 * checks whether in a nested guest, we need to inject them to L1 or L2.
398 */
399static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
400{
401 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
402 unsigned int nr = vcpu->arch.exception.nr;
403 bool has_payload = vcpu->arch.exception.has_payload;
404 unsigned long payload = vcpu->arch.exception.payload;
405
406 if (nr == PF_VECTOR) {
407 if (vcpu->arch.exception.nested_apf) {
408 *exit_qual = vcpu->arch.apf.nested_apf_token;
409 return 1;
410 }
411 if (nested_vmx_is_page_fault_vmexit(vmcs12,
412 vcpu->arch.exception.error_code)) {
413 *exit_qual = has_payload ? payload : vcpu->arch.cr2;
414 return 1;
415 }
416 } else if (vmcs12->exception_bitmap & (1u << nr)) {
417 if (nr == DB_VECTOR) {
418 if (!has_payload) {
419 payload = vcpu->arch.dr6;
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +0800420 payload &= ~DR6_BT;
421 payload ^= DR6_ACTIVE_LOW;
Sean Christopherson55d23752018-12-03 13:53:18 -0800422 }
423 *exit_qual = payload;
424 } else
425 *exit_qual = 0;
426 return 1;
427 }
428
429 return 0;
430}
431
432
433static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
434 struct x86_exception *fault)
435{
436 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
437
438 WARN_ON(!is_guest_mode(vcpu));
439
440 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
441 !to_vmx(vcpu)->nested.nested_run_pending) {
442 vmcs12->vm_exit_intr_error_code = fault->error_code;
443 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
444 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
445 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
446 fault->address);
447 } else {
448 kvm_inject_page_fault(vcpu, fault);
449 }
450}
451
Sean Christopherson55d23752018-12-03 13:53:18 -0800452static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
453 struct vmcs12 *vmcs12)
454{
455 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
456 return 0;
457
Sean Christopherson5497b952019-07-11 08:58:29 -0700458 if (CC(!page_address_valid(vcpu, vmcs12->io_bitmap_a)) ||
459 CC(!page_address_valid(vcpu, vmcs12->io_bitmap_b)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800460 return -EINVAL;
461
462 return 0;
463}
464
465static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
466 struct vmcs12 *vmcs12)
467{
468 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
469 return 0;
470
Sean Christopherson5497b952019-07-11 08:58:29 -0700471 if (CC(!page_address_valid(vcpu, vmcs12->msr_bitmap)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800472 return -EINVAL;
473
474 return 0;
475}
476
477static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
478 struct vmcs12 *vmcs12)
479{
480 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
481 return 0;
482
Sean Christopherson5497b952019-07-11 08:58:29 -0700483 if (CC(!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800484 return -EINVAL;
485
486 return 0;
487}
488
489/*
490 * Check if MSR is intercepted for L01 MSR bitmap.
491 */
492static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
493{
494 unsigned long *msr_bitmap;
495 int f = sizeof(unsigned long);
496
497 if (!cpu_has_vmx_msr_bitmap())
498 return true;
499
500 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
501
502 if (msr <= 0x1fff) {
503 return !!test_bit(msr, msr_bitmap + 0x800 / f);
504 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
505 msr &= 0x1fff;
506 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
507 }
508
509 return true;
510}
511
512/*
513 * If a msr is allowed by L0, we should check whether it is allowed by L1.
514 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
515 */
516static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
517 unsigned long *msr_bitmap_nested,
518 u32 msr, int type)
519{
520 int f = sizeof(unsigned long);
521
522 /*
523 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
524 * have the write-low and read-high bitmap offsets the wrong way round.
525 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
526 */
527 if (msr <= 0x1fff) {
528 if (type & MSR_TYPE_R &&
529 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
530 /* read-low */
531 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
532
533 if (type & MSR_TYPE_W &&
534 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
535 /* write-low */
536 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
537
538 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
539 msr &= 0x1fff;
540 if (type & MSR_TYPE_R &&
541 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
542 /* read-high */
543 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
544
545 if (type & MSR_TYPE_W &&
546 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
547 /* write-high */
548 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
549
550 }
551}
552
Miaohe Linffdbd502020-02-07 23:22:45 +0800553static inline void enable_x2apic_msr_intercepts(unsigned long *msr_bitmap)
554{
Marc Orracff7842019-04-01 23:55:59 -0700555 int msr;
556
557 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
558 unsigned word = msr / BITS_PER_LONG;
559
560 msr_bitmap[word] = ~0;
561 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
562 }
563}
564
Sean Christopherson55d23752018-12-03 13:53:18 -0800565/*
566 * Merge L0's and L1's MSR bitmap, return false to indicate that
567 * we do not use the hardware.
568 */
569static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
570 struct vmcs12 *vmcs12)
571{
572 int msr;
Sean Christopherson55d23752018-12-03 13:53:18 -0800573 unsigned long *msr_bitmap_l1;
574 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
KarimAllah Ahmed31f0b6c2019-01-31 21:24:36 +0100575 struct kvm_host_map *map = &to_vmx(vcpu)->nested.msr_bitmap_map;
Sean Christopherson55d23752018-12-03 13:53:18 -0800576
577 /* Nothing to do if the MSR bitmap is not in use. */
578 if (!cpu_has_vmx_msr_bitmap() ||
579 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
580 return false;
581
KarimAllah Ahmed31f0b6c2019-01-31 21:24:36 +0100582 if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->msr_bitmap), map))
Sean Christopherson55d23752018-12-03 13:53:18 -0800583 return false;
584
KarimAllah Ahmed31f0b6c2019-01-31 21:24:36 +0100585 msr_bitmap_l1 = (unsigned long *)map->hva;
Sean Christopherson55d23752018-12-03 13:53:18 -0800586
Marc Orracff7842019-04-01 23:55:59 -0700587 /*
588 * To keep the control flow simple, pay eight 8-byte writes (sixteen
589 * 4-byte writes on 32-bit systems) up front to enable intercepts for
590 * the x2APIC MSR range and selectively disable them below.
591 */
592 enable_x2apic_msr_intercepts(msr_bitmap_l0);
Sean Christopherson55d23752018-12-03 13:53:18 -0800593
Marc Orracff7842019-04-01 23:55:59 -0700594 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
595 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
596 /*
597 * L0 need not intercept reads for MSRs between 0x800
598 * and 0x8ff, it just lets the processor take the value
599 * from the virtual-APIC page; take those 256 bits
600 * directly from the L1 bitmap.
601 */
602 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
603 unsigned word = msr / BITS_PER_LONG;
604
605 msr_bitmap_l0[word] = msr_bitmap_l1[word];
606 }
607 }
608
Sean Christopherson55d23752018-12-03 13:53:18 -0800609 nested_vmx_disable_intercept_for_msr(
610 msr_bitmap_l1, msr_bitmap_l0,
Marc Orracff7842019-04-01 23:55:59 -0700611 X2APIC_MSR(APIC_TASKPRI),
Marc Orrc73f4c92019-04-01 23:56:00 -0700612 MSR_TYPE_R | MSR_TYPE_W);
Marc Orracff7842019-04-01 23:55:59 -0700613
614 if (nested_cpu_has_vid(vmcs12)) {
615 nested_vmx_disable_intercept_for_msr(
616 msr_bitmap_l1, msr_bitmap_l0,
617 X2APIC_MSR(APIC_EOI),
618 MSR_TYPE_W);
619 nested_vmx_disable_intercept_for_msr(
620 msr_bitmap_l1, msr_bitmap_l0,
621 X2APIC_MSR(APIC_SELF_IPI),
622 MSR_TYPE_W);
623 }
Sean Christopherson55d23752018-12-03 13:53:18 -0800624 }
625
Sean Christophersond69129b2019-05-08 07:32:15 -0700626 /* KVM unconditionally exposes the FS/GS base MSRs to L1. */
Sean Christophersondbdd0962021-04-21 19:38:31 -0700627#ifdef CONFIG_X86_64
Sean Christophersond69129b2019-05-08 07:32:15 -0700628 nested_vmx_disable_intercept_for_msr(msr_bitmap_l1, msr_bitmap_l0,
629 MSR_FS_BASE, MSR_TYPE_RW);
630
631 nested_vmx_disable_intercept_for_msr(msr_bitmap_l1, msr_bitmap_l0,
632 MSR_GS_BASE, MSR_TYPE_RW);
633
634 nested_vmx_disable_intercept_for_msr(msr_bitmap_l1, msr_bitmap_l0,
635 MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
Sean Christophersondbdd0962021-04-21 19:38:31 -0700636#endif
Sean Christophersond69129b2019-05-08 07:32:15 -0700637
638 /*
639 * Checking the L0->L1 bitmap is trying to verify two things:
640 *
641 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
642 * ensures that we do not accidentally generate an L02 MSR bitmap
643 * from the L12 MSR bitmap that is too permissive.
644 * 2. That L1 or L2s have actually used the MSR. This avoids
645 * unnecessarily merging of the bitmap if the MSR is unused. This
646 * works properly because we only update the L01 MSR bitmap lazily.
647 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
648 * updated to reflect this when L1 (or its L2s) actually write to
649 * the MSR.
650 */
651 if (!msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL))
Sean Christopherson55d23752018-12-03 13:53:18 -0800652 nested_vmx_disable_intercept_for_msr(
653 msr_bitmap_l1, msr_bitmap_l0,
654 MSR_IA32_SPEC_CTRL,
655 MSR_TYPE_R | MSR_TYPE_W);
656
Sean Christophersond69129b2019-05-08 07:32:15 -0700657 if (!msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD))
Sean Christopherson55d23752018-12-03 13:53:18 -0800658 nested_vmx_disable_intercept_for_msr(
659 msr_bitmap_l1, msr_bitmap_l0,
660 MSR_IA32_PRED_CMD,
661 MSR_TYPE_W);
662
KarimAllah Ahmed31f0b6c2019-01-31 21:24:36 +0100663 kvm_vcpu_unmap(vcpu, &to_vmx(vcpu)->nested.msr_bitmap_map, false);
Sean Christopherson55d23752018-12-03 13:53:18 -0800664
665 return true;
666}
667
668static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
669 struct vmcs12 *vmcs12)
670{
KarimAllah Ahmed88925302019-01-31 21:24:41 +0100671 struct kvm_host_map map;
Sean Christopherson55d23752018-12-03 13:53:18 -0800672 struct vmcs12 *shadow;
Sean Christopherson55d23752018-12-03 13:53:18 -0800673
674 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
675 vmcs12->vmcs_link_pointer == -1ull)
676 return;
677
678 shadow = get_shadow_vmcs12(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -0800679
KarimAllah Ahmed88925302019-01-31 21:24:41 +0100680 if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->vmcs_link_pointer), &map))
681 return;
Sean Christopherson55d23752018-12-03 13:53:18 -0800682
KarimAllah Ahmed88925302019-01-31 21:24:41 +0100683 memcpy(shadow, map.hva, VMCS12_SIZE);
684 kvm_vcpu_unmap(vcpu, &map, false);
Sean Christopherson55d23752018-12-03 13:53:18 -0800685}
686
687static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
688 struct vmcs12 *vmcs12)
689{
690 struct vcpu_vmx *vmx = to_vmx(vcpu);
691
692 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
693 vmcs12->vmcs_link_pointer == -1ull)
694 return;
695
696 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
697 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
698}
699
700/*
701 * In nested virtualization, check if L1 has set
702 * VM_EXIT_ACK_INTR_ON_EXIT
703 */
704static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
705{
706 return get_vmcs12(vcpu)->vm_exit_controls &
707 VM_EXIT_ACK_INTR_ON_EXIT;
708}
709
Sean Christopherson55d23752018-12-03 13:53:18 -0800710static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
711 struct vmcs12 *vmcs12)
712{
713 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
Sean Christopherson5497b952019-07-11 08:58:29 -0700714 CC(!page_address_valid(vcpu, vmcs12->apic_access_addr)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800715 return -EINVAL;
716 else
717 return 0;
718}
719
720static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
721 struct vmcs12 *vmcs12)
722{
723 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
724 !nested_cpu_has_apic_reg_virt(vmcs12) &&
725 !nested_cpu_has_vid(vmcs12) &&
726 !nested_cpu_has_posted_intr(vmcs12))
727 return 0;
728
729 /*
730 * If virtualize x2apic mode is enabled,
731 * virtualize apic access must be disabled.
732 */
Sean Christopherson5497b952019-07-11 08:58:29 -0700733 if (CC(nested_cpu_has_virt_x2apic_mode(vmcs12) &&
734 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800735 return -EINVAL;
736
737 /*
738 * If virtual interrupt delivery is enabled,
739 * we must exit on external interrupts.
740 */
Sean Christopherson5497b952019-07-11 08:58:29 -0700741 if (CC(nested_cpu_has_vid(vmcs12) && !nested_exit_on_intr(vcpu)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800742 return -EINVAL;
743
744 /*
745 * bits 15:8 should be zero in posted_intr_nv,
746 * the descriptor address has been already checked
747 * in nested_get_vmcs12_pages.
748 *
749 * bits 5:0 of posted_intr_desc_addr should be zero.
750 */
751 if (nested_cpu_has_posted_intr(vmcs12) &&
Sean Christopherson5497b952019-07-11 08:58:29 -0700752 (CC(!nested_cpu_has_vid(vmcs12)) ||
753 CC(!nested_exit_intr_ack_set(vcpu)) ||
754 CC((vmcs12->posted_intr_nv & 0xff00)) ||
Sean Christopherson636e8b72021-02-03 16:01:10 -0800755 CC(!kvm_vcpu_is_legal_aligned_gpa(vcpu, vmcs12->posted_intr_desc_addr, 64))))
Sean Christopherson55d23752018-12-03 13:53:18 -0800756 return -EINVAL;
757
758 /* tpr shadow is needed by all apicv features. */
Sean Christopherson5497b952019-07-11 08:58:29 -0700759 if (CC(!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800760 return -EINVAL;
761
762 return 0;
763}
764
765static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
Sean Christophersonf9b245e2018-12-12 13:30:08 -0500766 u32 count, u64 addr)
Sean Christopherson55d23752018-12-03 13:53:18 -0800767{
Sean Christopherson55d23752018-12-03 13:53:18 -0800768 if (count == 0)
769 return 0;
Sean Christopherson636e8b72021-02-03 16:01:10 -0800770
771 if (!kvm_vcpu_is_legal_aligned_gpa(vcpu, addr, 16) ||
772 !kvm_vcpu_is_legal_gpa(vcpu, (addr + count * sizeof(struct vmx_msr_entry) - 1)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800773 return -EINVAL;
Sean Christophersonf9b245e2018-12-12 13:30:08 -0500774
Sean Christopherson55d23752018-12-03 13:53:18 -0800775 return 0;
776}
777
Krish Sadhukhan61446ba2018-12-12 13:30:09 -0500778static int nested_vmx_check_exit_msr_switch_controls(struct kvm_vcpu *vcpu,
779 struct vmcs12 *vmcs12)
Sean Christopherson55d23752018-12-03 13:53:18 -0800780{
Sean Christopherson5497b952019-07-11 08:58:29 -0700781 if (CC(nested_vmx_check_msr_switch(vcpu,
782 vmcs12->vm_exit_msr_load_count,
783 vmcs12->vm_exit_msr_load_addr)) ||
784 CC(nested_vmx_check_msr_switch(vcpu,
785 vmcs12->vm_exit_msr_store_count,
786 vmcs12->vm_exit_msr_store_addr)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800787 return -EINVAL;
Sean Christophersonf9b245e2018-12-12 13:30:08 -0500788
Sean Christopherson55d23752018-12-03 13:53:18 -0800789 return 0;
790}
791
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -0500792static int nested_vmx_check_entry_msr_switch_controls(struct kvm_vcpu *vcpu,
793 struct vmcs12 *vmcs12)
Krish Sadhukhan61446ba2018-12-12 13:30:09 -0500794{
Sean Christopherson5497b952019-07-11 08:58:29 -0700795 if (CC(nested_vmx_check_msr_switch(vcpu,
796 vmcs12->vm_entry_msr_load_count,
797 vmcs12->vm_entry_msr_load_addr)))
Krish Sadhukhan61446ba2018-12-12 13:30:09 -0500798 return -EINVAL;
799
800 return 0;
801}
802
Sean Christopherson55d23752018-12-03 13:53:18 -0800803static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
804 struct vmcs12 *vmcs12)
805{
806 if (!nested_cpu_has_pml(vmcs12))
807 return 0;
808
Sean Christopherson5497b952019-07-11 08:58:29 -0700809 if (CC(!nested_cpu_has_ept(vmcs12)) ||
810 CC(!page_address_valid(vcpu, vmcs12->pml_address)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800811 return -EINVAL;
812
813 return 0;
814}
815
816static int nested_vmx_check_unrestricted_guest_controls(struct kvm_vcpu *vcpu,
817 struct vmcs12 *vmcs12)
818{
Sean Christopherson5497b952019-07-11 08:58:29 -0700819 if (CC(nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST) &&
820 !nested_cpu_has_ept(vmcs12)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800821 return -EINVAL;
822 return 0;
823}
824
825static int nested_vmx_check_mode_based_ept_exec_controls(struct kvm_vcpu *vcpu,
826 struct vmcs12 *vmcs12)
827{
Sean Christopherson5497b952019-07-11 08:58:29 -0700828 if (CC(nested_cpu_has2(vmcs12, SECONDARY_EXEC_MODE_BASED_EPT_EXEC) &&
829 !nested_cpu_has_ept(vmcs12)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800830 return -EINVAL;
831 return 0;
832}
833
834static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
835 struct vmcs12 *vmcs12)
836{
837 if (!nested_cpu_has_shadow_vmcs(vmcs12))
838 return 0;
839
Sean Christopherson5497b952019-07-11 08:58:29 -0700840 if (CC(!page_address_valid(vcpu, vmcs12->vmread_bitmap)) ||
841 CC(!page_address_valid(vcpu, vmcs12->vmwrite_bitmap)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800842 return -EINVAL;
843
844 return 0;
845}
846
847static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
848 struct vmx_msr_entry *e)
849{
850 /* x2APIC MSR accesses are not allowed */
Sean Christopherson5497b952019-07-11 08:58:29 -0700851 if (CC(vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8))
Sean Christopherson55d23752018-12-03 13:53:18 -0800852 return -EINVAL;
Sean Christopherson5497b952019-07-11 08:58:29 -0700853 if (CC(e->index == MSR_IA32_UCODE_WRITE) || /* SDM Table 35-2 */
854 CC(e->index == MSR_IA32_UCODE_REV))
Sean Christopherson55d23752018-12-03 13:53:18 -0800855 return -EINVAL;
Sean Christopherson5497b952019-07-11 08:58:29 -0700856 if (CC(e->reserved != 0))
Sean Christopherson55d23752018-12-03 13:53:18 -0800857 return -EINVAL;
858 return 0;
859}
860
861static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
862 struct vmx_msr_entry *e)
863{
Sean Christopherson5497b952019-07-11 08:58:29 -0700864 if (CC(e->index == MSR_FS_BASE) ||
865 CC(e->index == MSR_GS_BASE) ||
866 CC(e->index == MSR_IA32_SMM_MONITOR_CTL) || /* SMM is not supported */
Sean Christopherson55d23752018-12-03 13:53:18 -0800867 nested_vmx_msr_check_common(vcpu, e))
868 return -EINVAL;
869 return 0;
870}
871
872static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
873 struct vmx_msr_entry *e)
874{
Sean Christopherson5497b952019-07-11 08:58:29 -0700875 if (CC(e->index == MSR_IA32_SMBASE) || /* SMM is not supported */
Sean Christopherson55d23752018-12-03 13:53:18 -0800876 nested_vmx_msr_check_common(vcpu, e))
877 return -EINVAL;
878 return 0;
879}
880
Marc Orrf0b51052019-09-17 11:50:57 -0700881static u32 nested_vmx_max_atomic_switch_msrs(struct kvm_vcpu *vcpu)
882{
883 struct vcpu_vmx *vmx = to_vmx(vcpu);
884 u64 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
885 vmx->nested.msrs.misc_high);
886
887 return (vmx_misc_max_msr(vmx_misc) + 1) * VMX_MISC_MSR_LIST_MULTIPLIER;
888}
889
Sean Christopherson55d23752018-12-03 13:53:18 -0800890/*
891 * Load guest's/host's msr at nested entry/exit.
892 * return 0 for success, entry index for failure.
Marc Orrf0b51052019-09-17 11:50:57 -0700893 *
894 * One of the failure modes for MSR load/store is when a list exceeds the
895 * virtual hardware's capacity. To maintain compatibility with hardware inasmuch
896 * as possible, process all valid entries before failing rather than precheck
897 * for a capacity violation.
Sean Christopherson55d23752018-12-03 13:53:18 -0800898 */
899static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
900{
901 u32 i;
902 struct vmx_msr_entry e;
Marc Orrf0b51052019-09-17 11:50:57 -0700903 u32 max_msr_list_size = nested_vmx_max_atomic_switch_msrs(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -0800904
Sean Christopherson55d23752018-12-03 13:53:18 -0800905 for (i = 0; i < count; i++) {
Marc Orrf0b51052019-09-17 11:50:57 -0700906 if (unlikely(i >= max_msr_list_size))
907 goto fail;
908
Sean Christopherson55d23752018-12-03 13:53:18 -0800909 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
910 &e, sizeof(e))) {
911 pr_debug_ratelimited(
912 "%s cannot read MSR entry (%u, 0x%08llx)\n",
913 __func__, i, gpa + i * sizeof(e));
914 goto fail;
915 }
916 if (nested_vmx_load_msr_check(vcpu, &e)) {
917 pr_debug_ratelimited(
918 "%s check failed (%u, 0x%x, 0x%x)\n",
919 __func__, i, e.index, e.reserved);
920 goto fail;
921 }
Sean Christophersonf20935d2019-09-05 14:22:54 -0700922 if (kvm_set_msr(vcpu, e.index, e.value)) {
Sean Christopherson55d23752018-12-03 13:53:18 -0800923 pr_debug_ratelimited(
924 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
925 __func__, i, e.index, e.value);
926 goto fail;
927 }
928 }
929 return 0;
930fail:
Sean Christopherson68cda402020-05-11 15:05:29 -0700931 /* Note, max_msr_list_size is at most 4096, i.e. this can't wrap. */
Sean Christopherson55d23752018-12-03 13:53:18 -0800932 return i + 1;
933}
934
Aaron Lewis662f1d12019-11-07 21:14:39 -0800935static bool nested_vmx_get_vmexit_msr_value(struct kvm_vcpu *vcpu,
936 u32 msr_index,
937 u64 *data)
938{
939 struct vcpu_vmx *vmx = to_vmx(vcpu);
940
941 /*
942 * If the L0 hypervisor stored a more accurate value for the TSC that
943 * does not include the time taken for emulation of the L2->L1
944 * VM-exit in L0, use the more accurate value.
945 */
946 if (msr_index == MSR_IA32_TSC) {
Sean Christophersona128a932020-09-23 11:03:57 -0700947 int i = vmx_find_loadstore_msr_slot(&vmx->msr_autostore.guest,
948 MSR_IA32_TSC);
Aaron Lewis662f1d12019-11-07 21:14:39 -0800949
Sean Christophersona128a932020-09-23 11:03:57 -0700950 if (i >= 0) {
951 u64 val = vmx->msr_autostore.guest.val[i].value;
Aaron Lewis662f1d12019-11-07 21:14:39 -0800952
953 *data = kvm_read_l1_tsc(vcpu, val);
954 return true;
955 }
956 }
957
958 if (kvm_get_msr(vcpu, msr_index, data)) {
959 pr_debug_ratelimited("%s cannot read MSR (0x%x)\n", __func__,
960 msr_index);
961 return false;
962 }
963 return true;
964}
965
Aaron Lewis365d3d52019-11-07 21:14:36 -0800966static bool read_and_check_msr_entry(struct kvm_vcpu *vcpu, u64 gpa, int i,
967 struct vmx_msr_entry *e)
968{
969 if (kvm_vcpu_read_guest(vcpu,
970 gpa + i * sizeof(*e),
971 e, 2 * sizeof(u32))) {
972 pr_debug_ratelimited(
973 "%s cannot read MSR entry (%u, 0x%08llx)\n",
974 __func__, i, gpa + i * sizeof(*e));
975 return false;
976 }
977 if (nested_vmx_store_msr_check(vcpu, e)) {
978 pr_debug_ratelimited(
979 "%s check failed (%u, 0x%x, 0x%x)\n",
980 __func__, i, e->index, e->reserved);
981 return false;
982 }
983 return true;
984}
985
Sean Christopherson55d23752018-12-03 13:53:18 -0800986static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
987{
Sean Christophersonf20935d2019-09-05 14:22:54 -0700988 u64 data;
Sean Christopherson55d23752018-12-03 13:53:18 -0800989 u32 i;
990 struct vmx_msr_entry e;
Marc Orrf0b51052019-09-17 11:50:57 -0700991 u32 max_msr_list_size = nested_vmx_max_atomic_switch_msrs(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -0800992
993 for (i = 0; i < count; i++) {
Marc Orrf0b51052019-09-17 11:50:57 -0700994 if (unlikely(i >= max_msr_list_size))
995 return -EINVAL;
996
Aaron Lewis365d3d52019-11-07 21:14:36 -0800997 if (!read_and_check_msr_entry(vcpu, gpa, i, &e))
Sean Christopherson55d23752018-12-03 13:53:18 -0800998 return -EINVAL;
Aaron Lewis365d3d52019-11-07 21:14:36 -0800999
Aaron Lewis662f1d12019-11-07 21:14:39 -08001000 if (!nested_vmx_get_vmexit_msr_value(vcpu, e.index, &data))
Sean Christopherson55d23752018-12-03 13:53:18 -08001001 return -EINVAL;
Aaron Lewis662f1d12019-11-07 21:14:39 -08001002
Sean Christopherson55d23752018-12-03 13:53:18 -08001003 if (kvm_vcpu_write_guest(vcpu,
1004 gpa + i * sizeof(e) +
1005 offsetof(struct vmx_msr_entry, value),
Sean Christophersonf20935d2019-09-05 14:22:54 -07001006 &data, sizeof(data))) {
Sean Christopherson55d23752018-12-03 13:53:18 -08001007 pr_debug_ratelimited(
1008 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Sean Christophersonf20935d2019-09-05 14:22:54 -07001009 __func__, i, e.index, data);
Sean Christopherson55d23752018-12-03 13:53:18 -08001010 return -EINVAL;
1011 }
1012 }
1013 return 0;
1014}
1015
Aaron Lewis662f1d12019-11-07 21:14:39 -08001016static bool nested_msr_store_list_has_msr(struct kvm_vcpu *vcpu, u32 msr_index)
1017{
1018 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1019 u32 count = vmcs12->vm_exit_msr_store_count;
1020 u64 gpa = vmcs12->vm_exit_msr_store_addr;
1021 struct vmx_msr_entry e;
1022 u32 i;
1023
1024 for (i = 0; i < count; i++) {
1025 if (!read_and_check_msr_entry(vcpu, gpa, i, &e))
1026 return false;
1027
1028 if (e.index == msr_index)
1029 return true;
1030 }
1031 return false;
1032}
1033
1034static void prepare_vmx_msr_autostore_list(struct kvm_vcpu *vcpu,
1035 u32 msr_index)
1036{
1037 struct vcpu_vmx *vmx = to_vmx(vcpu);
1038 struct vmx_msrs *autostore = &vmx->msr_autostore.guest;
1039 bool in_vmcs12_store_list;
Sean Christophersona128a932020-09-23 11:03:57 -07001040 int msr_autostore_slot;
Aaron Lewis662f1d12019-11-07 21:14:39 -08001041 bool in_autostore_list;
1042 int last;
1043
Sean Christophersona128a932020-09-23 11:03:57 -07001044 msr_autostore_slot = vmx_find_loadstore_msr_slot(autostore, msr_index);
1045 in_autostore_list = msr_autostore_slot >= 0;
Aaron Lewis662f1d12019-11-07 21:14:39 -08001046 in_vmcs12_store_list = nested_msr_store_list_has_msr(vcpu, msr_index);
1047
1048 if (in_vmcs12_store_list && !in_autostore_list) {
Sean Christophersonce833b22020-09-23 11:03:56 -07001049 if (autostore->nr == MAX_NR_LOADSTORE_MSRS) {
Aaron Lewis662f1d12019-11-07 21:14:39 -08001050 /*
1051 * Emulated VMEntry does not fail here. Instead a less
1052 * accurate value will be returned by
1053 * nested_vmx_get_vmexit_msr_value() using kvm_get_msr()
1054 * instead of reading the value from the vmcs02 VMExit
1055 * MSR-store area.
1056 */
1057 pr_warn_ratelimited(
1058 "Not enough msr entries in msr_autostore. Can't add msr %x\n",
1059 msr_index);
1060 return;
1061 }
1062 last = autostore->nr++;
1063 autostore->val[last].index = msr_index;
1064 } else if (!in_vmcs12_store_list && in_autostore_list) {
1065 last = --autostore->nr;
Sean Christophersona128a932020-09-23 11:03:57 -07001066 autostore->val[msr_autostore_slot] = autostore->val[last];
Aaron Lewis662f1d12019-11-07 21:14:39 -08001067 }
1068}
1069
Sean Christopherson55d23752018-12-03 13:53:18 -08001070/*
Sean Christophersonea79a752020-02-04 07:32:59 -08001071 * Load guest's/host's cr3 at nested entry/exit. @nested_ept is true if we are
1072 * emulating VM-Entry into a guest with EPT enabled. On failure, the expected
1073 * Exit Qualification (for a VM-Entry consistency check VM-Exit) is assigned to
1074 * @entry_failure_code.
Sean Christopherson55d23752018-12-03 13:53:18 -08001075 */
Maxim Levitsky0f857222021-06-07 12:02:00 +03001076static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,
1077 bool nested_ept, bool reload_pdptrs,
Sean Christopherson68cda402020-05-11 15:05:29 -07001078 enum vm_entry_failure_code *entry_failure_code)
Sean Christopherson55d23752018-12-03 13:53:18 -08001079{
Sean Christopherson636e8b72021-02-03 16:01:10 -08001080 if (CC(kvm_vcpu_is_illegal_gpa(vcpu, cr3))) {
Sean Christopherson0cc69202020-05-01 21:32:26 -07001081 *entry_failure_code = ENTRY_FAIL_DEFAULT;
1082 return -EINVAL;
1083 }
Sean Christopherson55d23752018-12-03 13:53:18 -08001084
Sean Christopherson0cc69202020-05-01 21:32:26 -07001085 /*
1086 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
1087 * must not be dereferenced.
1088 */
Maxim Levitsky0f857222021-06-07 12:02:00 +03001089 if (reload_pdptrs && !nested_ept && is_pae_paging(vcpu) &&
Sean Christophersonbcb72d02021-06-07 12:01:56 +03001090 CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))) {
1091 *entry_failure_code = ENTRY_FAIL_PDPTE;
1092 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08001093 }
1094
Sean Christopherson50a41792021-06-09 16:42:28 -07001095 if (!nested_ept)
Sean Christophersonb5129102021-06-09 16:42:27 -07001096 kvm_mmu_new_pgd(vcpu, cr3);
Sean Christopherson07ffaf32021-06-09 16:42:21 -07001097
Sean Christopherson55d23752018-12-03 13:53:18 -08001098 vcpu->arch.cr3 = cr3;
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07001099 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Sean Christopherson55d23752018-12-03 13:53:18 -08001100
Sean Christopherson616007c2021-06-22 10:57:34 -07001101 /* Re-initialize the MMU, e.g. to pick up CR4 MMU role changes. */
Sean Christophersonc9060662021-06-09 16:42:33 -07001102 kvm_init_mmu(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08001103
1104 return 0;
1105}
1106
1107/*
1108 * Returns if KVM is able to config CPU to tag TLB entries
1109 * populated by L2 differently than TLB entries populated
1110 * by L1.
1111 *
Liran Alon992edea2019-11-20 14:24:52 +02001112 * If L0 uses EPT, L1 and L2 run with different EPTP because
1113 * guest_mode is part of kvm_mmu_page_role. Thus, TLB entries
1114 * are tagged with different EPTP.
Sean Christopherson55d23752018-12-03 13:53:18 -08001115 *
1116 * If L1 uses VPID and we allocated a vpid02, TLB entries are tagged
1117 * with different VPID (L1 entries are tagged with vmx->vpid
1118 * while L2 entries are tagged with vmx->nested.vpid02).
1119 */
1120static bool nested_has_guest_tlb_tag(struct kvm_vcpu *vcpu)
1121{
1122 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1123
Liran Alon992edea2019-11-20 14:24:52 +02001124 return enable_ept ||
Sean Christopherson55d23752018-12-03 13:53:18 -08001125 (nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02);
1126}
1127
Sean Christopherson50b265a2020-03-20 14:28:19 -07001128static void nested_vmx_transition_tlb_flush(struct kvm_vcpu *vcpu,
1129 struct vmcs12 *vmcs12,
1130 bool is_vmenter)
1131{
1132 struct vcpu_vmx *vmx = to_vmx(vcpu);
1133
1134 /*
Sean Christopherson50a41792021-06-09 16:42:28 -07001135 * If vmcs12 doesn't use VPID, L1 expects linear and combined mappings
1136 * for *all* contexts to be flushed on VM-Enter/VM-Exit, i.e. it's a
1137 * full TLB flush from the guest's perspective. This is required even
1138 * if VPID is disabled in the host as KVM may need to synchronize the
1139 * MMU in response to the guest TLB flush.
1140 *
1141 * Note, using TLB_FLUSH_GUEST is correct even if nested EPT is in use.
1142 * EPT is a special snowflake, as guest-physical mappings aren't
1143 * flushed on VPID invalidations, including VM-Enter or VM-Exit with
1144 * VPID disabled. As a result, KVM _never_ needs to sync nEPT
1145 * entries on VM-Enter because L1 can't rely on VM-Enter to flush
1146 * those mappings.
Sean Christopherson50b265a2020-03-20 14:28:19 -07001147 */
Sean Christopherson50a41792021-06-09 16:42:28 -07001148 if (!nested_cpu_has_vpid(vmcs12)) {
1149 kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu);
Sean Christopherson50b265a2020-03-20 14:28:19 -07001150 return;
Sean Christopherson50a41792021-06-09 16:42:28 -07001151 }
1152
1153 /* L2 should never have a VPID if VPID is disabled. */
1154 WARN_ON(!enable_vpid);
Sean Christopherson50b265a2020-03-20 14:28:19 -07001155
1156 /*
Sean Christopherson50b265a2020-03-20 14:28:19 -07001157 * If VPID is enabled and used by vmc12, but L2 does not have a unique
1158 * TLB tag (ASID), i.e. EPT is disabled and KVM was unable to allocate
Sean Christophersonc51e1ff2020-03-20 14:28:22 -07001159 * a VPID for L2, flush the current context as the effective ASID is
1160 * common to both L1 and L2.
Sean Christopherson50b265a2020-03-20 14:28:19 -07001161 *
1162 * Defer the flush so that it runs after vmcs02.EPTP has been set by
1163 * KVM_REQ_LOAD_MMU_PGD (if nested EPT is enabled) and to avoid
1164 * redundant flushes further down the nested pipeline.
1165 *
1166 * If a TLB flush isn't required due to any of the above, and vpid12 is
1167 * changing then the new "virtual" VPID (vpid12) will reuse the same
Sean Christopherson50a41792021-06-09 16:42:28 -07001168 * "real" VPID (vpid02), and so needs to be flushed. There's no direct
Sean Christopherson50b265a2020-03-20 14:28:19 -07001169 * mapping between vpid02 and vpid12, vpid02 is per-vCPU and reused for
Sean Christopherson50a41792021-06-09 16:42:28 -07001170 * all nested vCPUs. Remember, a flush on VM-Enter does not invalidate
1171 * guest-physical mappings, so there is no need to sync the nEPT MMU.
Sean Christopherson50b265a2020-03-20 14:28:19 -07001172 */
Sean Christopherson50a41792021-06-09 16:42:28 -07001173 if (!nested_has_guest_tlb_tag(vcpu)) {
Sean Christophersonc51e1ff2020-03-20 14:28:22 -07001174 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
Sean Christopherson50b265a2020-03-20 14:28:19 -07001175 } else if (is_vmenter &&
1176 vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
1177 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
1178 vpid_sync_context(nested_get_vpid02(vcpu));
1179 }
1180}
1181
Sean Christopherson55d23752018-12-03 13:53:18 -08001182static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
1183{
1184 superset &= mask;
1185 subset &= mask;
1186
1187 return (superset | subset) == superset;
1188}
1189
1190static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
1191{
1192 const u64 feature_and_reserved =
1193 /* feature (except bit 48; see below) */
1194 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
1195 /* reserved */
1196 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
1197 u64 vmx_basic = vmx->nested.msrs.basic;
1198
1199 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
1200 return -EINVAL;
1201
1202 /*
1203 * KVM does not emulate a version of VMX that constrains physical
1204 * addresses of VMX structures (e.g. VMCS) to 32-bits.
1205 */
1206 if (data & BIT_ULL(48))
1207 return -EINVAL;
1208
1209 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
1210 vmx_basic_vmcs_revision_id(data))
1211 return -EINVAL;
1212
1213 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
1214 return -EINVAL;
1215
1216 vmx->nested.msrs.basic = data;
1217 return 0;
1218}
1219
1220static int
1221vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
1222{
1223 u64 supported;
1224 u32 *lowp, *highp;
1225
1226 switch (msr_index) {
1227 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
1228 lowp = &vmx->nested.msrs.pinbased_ctls_low;
1229 highp = &vmx->nested.msrs.pinbased_ctls_high;
1230 break;
1231 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
1232 lowp = &vmx->nested.msrs.procbased_ctls_low;
1233 highp = &vmx->nested.msrs.procbased_ctls_high;
1234 break;
1235 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
1236 lowp = &vmx->nested.msrs.exit_ctls_low;
1237 highp = &vmx->nested.msrs.exit_ctls_high;
1238 break;
1239 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
1240 lowp = &vmx->nested.msrs.entry_ctls_low;
1241 highp = &vmx->nested.msrs.entry_ctls_high;
1242 break;
1243 case MSR_IA32_VMX_PROCBASED_CTLS2:
1244 lowp = &vmx->nested.msrs.secondary_ctls_low;
1245 highp = &vmx->nested.msrs.secondary_ctls_high;
1246 break;
1247 default:
1248 BUG();
1249 }
1250
1251 supported = vmx_control_msr(*lowp, *highp);
1252
1253 /* Check must-be-1 bits are still 1. */
1254 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
1255 return -EINVAL;
1256
1257 /* Check must-be-0 bits are still 0. */
1258 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
1259 return -EINVAL;
1260
1261 *lowp = data;
1262 *highp = data >> 32;
1263 return 0;
1264}
1265
1266static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
1267{
1268 const u64 feature_and_reserved_bits =
1269 /* feature */
1270 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
1271 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
1272 /* reserved */
1273 GENMASK_ULL(13, 9) | BIT_ULL(31);
1274 u64 vmx_misc;
1275
1276 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
1277 vmx->nested.msrs.misc_high);
1278
1279 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
1280 return -EINVAL;
1281
1282 if ((vmx->nested.msrs.pinbased_ctls_high &
1283 PIN_BASED_VMX_PREEMPTION_TIMER) &&
1284 vmx_misc_preemption_timer_rate(data) !=
1285 vmx_misc_preemption_timer_rate(vmx_misc))
1286 return -EINVAL;
1287
1288 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
1289 return -EINVAL;
1290
1291 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
1292 return -EINVAL;
1293
1294 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
1295 return -EINVAL;
1296
1297 vmx->nested.msrs.misc_low = data;
1298 vmx->nested.msrs.misc_high = data >> 32;
1299
Sean Christopherson55d23752018-12-03 13:53:18 -08001300 return 0;
1301}
1302
1303static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
1304{
1305 u64 vmx_ept_vpid_cap;
1306
1307 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
1308 vmx->nested.msrs.vpid_caps);
1309
1310 /* Every bit is either reserved or a feature bit. */
1311 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
1312 return -EINVAL;
1313
1314 vmx->nested.msrs.ept_caps = data;
1315 vmx->nested.msrs.vpid_caps = data >> 32;
1316 return 0;
1317}
1318
1319static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
1320{
1321 u64 *msr;
1322
1323 switch (msr_index) {
1324 case MSR_IA32_VMX_CR0_FIXED0:
1325 msr = &vmx->nested.msrs.cr0_fixed0;
1326 break;
1327 case MSR_IA32_VMX_CR4_FIXED0:
1328 msr = &vmx->nested.msrs.cr4_fixed0;
1329 break;
1330 default:
1331 BUG();
1332 }
1333
1334 /*
1335 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
1336 * must be 1 in the restored value.
1337 */
1338 if (!is_bitwise_subset(data, *msr, -1ULL))
1339 return -EINVAL;
1340
1341 *msr = data;
1342 return 0;
1343}
1344
1345/*
1346 * Called when userspace is restoring VMX MSRs.
1347 *
1348 * Returns 0 on success, non-0 otherwise.
1349 */
1350int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1351{
1352 struct vcpu_vmx *vmx = to_vmx(vcpu);
1353
1354 /*
1355 * Don't allow changes to the VMX capability MSRs while the vCPU
1356 * is in VMX operation.
1357 */
1358 if (vmx->nested.vmxon)
1359 return -EBUSY;
1360
1361 switch (msr_index) {
1362 case MSR_IA32_VMX_BASIC:
1363 return vmx_restore_vmx_basic(vmx, data);
1364 case MSR_IA32_VMX_PINBASED_CTLS:
1365 case MSR_IA32_VMX_PROCBASED_CTLS:
1366 case MSR_IA32_VMX_EXIT_CTLS:
1367 case MSR_IA32_VMX_ENTRY_CTLS:
1368 /*
1369 * The "non-true" VMX capability MSRs are generated from the
1370 * "true" MSRs, so we do not support restoring them directly.
1371 *
1372 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
1373 * should restore the "true" MSRs with the must-be-1 bits
1374 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
1375 * DEFAULT SETTINGS".
1376 */
1377 return -EINVAL;
1378 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
1379 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
1380 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
1381 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
1382 case MSR_IA32_VMX_PROCBASED_CTLS2:
1383 return vmx_restore_control_msr(vmx, msr_index, data);
1384 case MSR_IA32_VMX_MISC:
1385 return vmx_restore_vmx_misc(vmx, data);
1386 case MSR_IA32_VMX_CR0_FIXED0:
1387 case MSR_IA32_VMX_CR4_FIXED0:
1388 return vmx_restore_fixed0_msr(vmx, msr_index, data);
1389 case MSR_IA32_VMX_CR0_FIXED1:
1390 case MSR_IA32_VMX_CR4_FIXED1:
1391 /*
1392 * These MSRs are generated based on the vCPU's CPUID, so we
1393 * do not support restoring them directly.
1394 */
1395 return -EINVAL;
1396 case MSR_IA32_VMX_EPT_VPID_CAP:
1397 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
1398 case MSR_IA32_VMX_VMCS_ENUM:
1399 vmx->nested.msrs.vmcs_enum = data;
1400 return 0;
Paolo Bonzinie8a70bd2019-07-02 14:40:40 +02001401 case MSR_IA32_VMX_VMFUNC:
1402 if (data & ~vmx->nested.msrs.vmfunc_controls)
1403 return -EINVAL;
1404 vmx->nested.msrs.vmfunc_controls = data;
1405 return 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08001406 default:
1407 /*
1408 * The rest of the VMX capability MSRs do not support restore.
1409 */
1410 return -EINVAL;
1411 }
1412}
1413
1414/* Returns 0 on success, non-0 otherwise. */
1415int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
1416{
1417 switch (msr_index) {
1418 case MSR_IA32_VMX_BASIC:
1419 *pdata = msrs->basic;
1420 break;
1421 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
1422 case MSR_IA32_VMX_PINBASED_CTLS:
1423 *pdata = vmx_control_msr(
1424 msrs->pinbased_ctls_low,
1425 msrs->pinbased_ctls_high);
1426 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
1427 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
1428 break;
1429 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
1430 case MSR_IA32_VMX_PROCBASED_CTLS:
1431 *pdata = vmx_control_msr(
1432 msrs->procbased_ctls_low,
1433 msrs->procbased_ctls_high);
1434 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
1435 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
1436 break;
1437 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
1438 case MSR_IA32_VMX_EXIT_CTLS:
1439 *pdata = vmx_control_msr(
1440 msrs->exit_ctls_low,
1441 msrs->exit_ctls_high);
1442 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
1443 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
1444 break;
1445 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
1446 case MSR_IA32_VMX_ENTRY_CTLS:
1447 *pdata = vmx_control_msr(
1448 msrs->entry_ctls_low,
1449 msrs->entry_ctls_high);
1450 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
1451 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
1452 break;
1453 case MSR_IA32_VMX_MISC:
1454 *pdata = vmx_control_msr(
1455 msrs->misc_low,
1456 msrs->misc_high);
1457 break;
1458 case MSR_IA32_VMX_CR0_FIXED0:
1459 *pdata = msrs->cr0_fixed0;
1460 break;
1461 case MSR_IA32_VMX_CR0_FIXED1:
1462 *pdata = msrs->cr0_fixed1;
1463 break;
1464 case MSR_IA32_VMX_CR4_FIXED0:
1465 *pdata = msrs->cr4_fixed0;
1466 break;
1467 case MSR_IA32_VMX_CR4_FIXED1:
1468 *pdata = msrs->cr4_fixed1;
1469 break;
1470 case MSR_IA32_VMX_VMCS_ENUM:
1471 *pdata = msrs->vmcs_enum;
1472 break;
1473 case MSR_IA32_VMX_PROCBASED_CTLS2:
1474 *pdata = vmx_control_msr(
1475 msrs->secondary_ctls_low,
1476 msrs->secondary_ctls_high);
1477 break;
1478 case MSR_IA32_VMX_EPT_VPID_CAP:
1479 *pdata = msrs->ept_caps |
1480 ((u64)msrs->vpid_caps << 32);
1481 break;
1482 case MSR_IA32_VMX_VMFUNC:
1483 *pdata = msrs->vmfunc_controls;
1484 break;
1485 default:
1486 return 1;
1487 }
1488
1489 return 0;
1490}
1491
1492/*
Sean Christophersonfadcead2019-05-07 08:36:23 -07001493 * Copy the writable VMCS shadow fields back to the VMCS12, in case they have
1494 * been modified by the L1 guest. Note, "writable" in this context means
1495 * "writable by the guest", i.e. tagged SHADOW_FIELD_RW; the set of
1496 * fields tagged SHADOW_FIELD_RO may or may not align with the "read-only"
1497 * VM-exit information fields (which are actually writable if the vCPU is
1498 * configured to support "VMWRITE to any supported field in the VMCS").
Sean Christopherson55d23752018-12-03 13:53:18 -08001499 */
1500static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
1501{
Sean Christopherson55d23752018-12-03 13:53:18 -08001502 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Sean Christophersonfadcead2019-05-07 08:36:23 -07001503 struct vmcs12 *vmcs12 = get_vmcs12(&vmx->vcpu);
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07001504 struct shadow_vmcs_field field;
1505 unsigned long val;
Sean Christophersonfadcead2019-05-07 08:36:23 -07001506 int i;
Sean Christopherson55d23752018-12-03 13:53:18 -08001507
Paolo Bonzini88dddc12019-07-19 18:41:10 +02001508 if (WARN_ON(!shadow_vmcs))
1509 return;
1510
Sean Christopherson55d23752018-12-03 13:53:18 -08001511 preempt_disable();
1512
1513 vmcs_load(shadow_vmcs);
1514
Sean Christophersonfadcead2019-05-07 08:36:23 -07001515 for (i = 0; i < max_shadow_read_write_fields; i++) {
1516 field = shadow_read_write_fields[i];
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07001517 val = __vmcs_readl(field.encoding);
1518 vmcs12_write_any(vmcs12, field.encoding, field.offset, val);
Sean Christopherson55d23752018-12-03 13:53:18 -08001519 }
1520
1521 vmcs_clear(shadow_vmcs);
1522 vmcs_load(vmx->loaded_vmcs->vmcs);
1523
1524 preempt_enable();
1525}
1526
1527static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
1528{
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07001529 const struct shadow_vmcs_field *fields[] = {
Sean Christopherson55d23752018-12-03 13:53:18 -08001530 shadow_read_write_fields,
1531 shadow_read_only_fields
1532 };
1533 const int max_fields[] = {
1534 max_shadow_read_write_fields,
1535 max_shadow_read_only_fields
1536 };
Sean Christopherson55d23752018-12-03 13:53:18 -08001537 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07001538 struct vmcs12 *vmcs12 = get_vmcs12(&vmx->vcpu);
1539 struct shadow_vmcs_field field;
1540 unsigned long val;
1541 int i, q;
Sean Christopherson55d23752018-12-03 13:53:18 -08001542
Paolo Bonzini88dddc12019-07-19 18:41:10 +02001543 if (WARN_ON(!shadow_vmcs))
1544 return;
1545
Sean Christopherson55d23752018-12-03 13:53:18 -08001546 vmcs_load(shadow_vmcs);
1547
1548 for (q = 0; q < ARRAY_SIZE(fields); q++) {
1549 for (i = 0; i < max_fields[q]; i++) {
1550 field = fields[q][i];
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07001551 val = vmcs12_read_any(vmcs12, field.encoding,
1552 field.offset);
1553 __vmcs_writel(field.encoding, val);
Sean Christopherson55d23752018-12-03 13:53:18 -08001554 }
1555 }
1556
1557 vmcs_clear(shadow_vmcs);
1558 vmcs_load(vmx->loaded_vmcs->vmcs);
1559}
1560
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001561static void copy_enlightened_to_vmcs12(struct vcpu_vmx *vmx, u32 hv_clean_fields)
Sean Christopherson55d23752018-12-03 13:53:18 -08001562{
1563 struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12;
1564 struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
1565
1566 /* HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE */
1567 vmcs12->tpr_threshold = evmcs->tpr_threshold;
1568 vmcs12->guest_rip = evmcs->guest_rip;
1569
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001570 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001571 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC))) {
1572 vmcs12->guest_rsp = evmcs->guest_rsp;
1573 vmcs12->guest_rflags = evmcs->guest_rflags;
1574 vmcs12->guest_interruptibility_info =
1575 evmcs->guest_interruptibility_info;
1576 }
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_CONTROL_PROC))) {
1580 vmcs12->cpu_based_vm_exec_control =
1581 evmcs->cpu_based_vm_exec_control;
1582 }
1583
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001584 if (unlikely(!(hv_clean_fields &
Vitaly Kuznetsovf9bc5222019-06-13 13:35:02 +02001585 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EXCPN))) {
Sean Christopherson55d23752018-12-03 13:53:18 -08001586 vmcs12->exception_bitmap = evmcs->exception_bitmap;
1587 }
1588
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001589 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001590 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_ENTRY))) {
1591 vmcs12->vm_entry_controls = evmcs->vm_entry_controls;
1592 }
1593
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001594 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001595 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT))) {
1596 vmcs12->vm_entry_intr_info_field =
1597 evmcs->vm_entry_intr_info_field;
1598 vmcs12->vm_entry_exception_error_code =
1599 evmcs->vm_entry_exception_error_code;
1600 vmcs12->vm_entry_instruction_len =
1601 evmcs->vm_entry_instruction_len;
1602 }
1603
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001604 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001605 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1))) {
1606 vmcs12->host_ia32_pat = evmcs->host_ia32_pat;
1607 vmcs12->host_ia32_efer = evmcs->host_ia32_efer;
1608 vmcs12->host_cr0 = evmcs->host_cr0;
1609 vmcs12->host_cr3 = evmcs->host_cr3;
1610 vmcs12->host_cr4 = evmcs->host_cr4;
1611 vmcs12->host_ia32_sysenter_esp = evmcs->host_ia32_sysenter_esp;
1612 vmcs12->host_ia32_sysenter_eip = evmcs->host_ia32_sysenter_eip;
1613 vmcs12->host_rip = evmcs->host_rip;
1614 vmcs12->host_ia32_sysenter_cs = evmcs->host_ia32_sysenter_cs;
1615 vmcs12->host_es_selector = evmcs->host_es_selector;
1616 vmcs12->host_cs_selector = evmcs->host_cs_selector;
1617 vmcs12->host_ss_selector = evmcs->host_ss_selector;
1618 vmcs12->host_ds_selector = evmcs->host_ds_selector;
1619 vmcs12->host_fs_selector = evmcs->host_fs_selector;
1620 vmcs12->host_gs_selector = evmcs->host_gs_selector;
1621 vmcs12->host_tr_selector = evmcs->host_tr_selector;
1622 }
1623
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001624 if (unlikely(!(hv_clean_fields &
Vitaly Kuznetsovf9bc5222019-06-13 13:35:02 +02001625 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1))) {
Sean Christopherson55d23752018-12-03 13:53:18 -08001626 vmcs12->pin_based_vm_exec_control =
1627 evmcs->pin_based_vm_exec_control;
1628 vmcs12->vm_exit_controls = evmcs->vm_exit_controls;
1629 vmcs12->secondary_vm_exec_control =
1630 evmcs->secondary_vm_exec_control;
1631 }
1632
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001633 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001634 HV_VMX_ENLIGHTENED_CLEAN_FIELD_IO_BITMAP))) {
1635 vmcs12->io_bitmap_a = evmcs->io_bitmap_a;
1636 vmcs12->io_bitmap_b = evmcs->io_bitmap_b;
1637 }
1638
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001639 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001640 HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP))) {
1641 vmcs12->msr_bitmap = evmcs->msr_bitmap;
1642 }
1643
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001644 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001645 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2))) {
1646 vmcs12->guest_es_base = evmcs->guest_es_base;
1647 vmcs12->guest_cs_base = evmcs->guest_cs_base;
1648 vmcs12->guest_ss_base = evmcs->guest_ss_base;
1649 vmcs12->guest_ds_base = evmcs->guest_ds_base;
1650 vmcs12->guest_fs_base = evmcs->guest_fs_base;
1651 vmcs12->guest_gs_base = evmcs->guest_gs_base;
1652 vmcs12->guest_ldtr_base = evmcs->guest_ldtr_base;
1653 vmcs12->guest_tr_base = evmcs->guest_tr_base;
1654 vmcs12->guest_gdtr_base = evmcs->guest_gdtr_base;
1655 vmcs12->guest_idtr_base = evmcs->guest_idtr_base;
1656 vmcs12->guest_es_limit = evmcs->guest_es_limit;
1657 vmcs12->guest_cs_limit = evmcs->guest_cs_limit;
1658 vmcs12->guest_ss_limit = evmcs->guest_ss_limit;
1659 vmcs12->guest_ds_limit = evmcs->guest_ds_limit;
1660 vmcs12->guest_fs_limit = evmcs->guest_fs_limit;
1661 vmcs12->guest_gs_limit = evmcs->guest_gs_limit;
1662 vmcs12->guest_ldtr_limit = evmcs->guest_ldtr_limit;
1663 vmcs12->guest_tr_limit = evmcs->guest_tr_limit;
1664 vmcs12->guest_gdtr_limit = evmcs->guest_gdtr_limit;
1665 vmcs12->guest_idtr_limit = evmcs->guest_idtr_limit;
1666 vmcs12->guest_es_ar_bytes = evmcs->guest_es_ar_bytes;
1667 vmcs12->guest_cs_ar_bytes = evmcs->guest_cs_ar_bytes;
1668 vmcs12->guest_ss_ar_bytes = evmcs->guest_ss_ar_bytes;
1669 vmcs12->guest_ds_ar_bytes = evmcs->guest_ds_ar_bytes;
1670 vmcs12->guest_fs_ar_bytes = evmcs->guest_fs_ar_bytes;
1671 vmcs12->guest_gs_ar_bytes = evmcs->guest_gs_ar_bytes;
1672 vmcs12->guest_ldtr_ar_bytes = evmcs->guest_ldtr_ar_bytes;
1673 vmcs12->guest_tr_ar_bytes = evmcs->guest_tr_ar_bytes;
1674 vmcs12->guest_es_selector = evmcs->guest_es_selector;
1675 vmcs12->guest_cs_selector = evmcs->guest_cs_selector;
1676 vmcs12->guest_ss_selector = evmcs->guest_ss_selector;
1677 vmcs12->guest_ds_selector = evmcs->guest_ds_selector;
1678 vmcs12->guest_fs_selector = evmcs->guest_fs_selector;
1679 vmcs12->guest_gs_selector = evmcs->guest_gs_selector;
1680 vmcs12->guest_ldtr_selector = evmcs->guest_ldtr_selector;
1681 vmcs12->guest_tr_selector = evmcs->guest_tr_selector;
1682 }
1683
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001684 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001685 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2))) {
1686 vmcs12->tsc_offset = evmcs->tsc_offset;
1687 vmcs12->virtual_apic_page_addr = evmcs->virtual_apic_page_addr;
1688 vmcs12->xss_exit_bitmap = evmcs->xss_exit_bitmap;
1689 }
1690
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001691 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001692 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR))) {
1693 vmcs12->cr0_guest_host_mask = evmcs->cr0_guest_host_mask;
1694 vmcs12->cr4_guest_host_mask = evmcs->cr4_guest_host_mask;
1695 vmcs12->cr0_read_shadow = evmcs->cr0_read_shadow;
1696 vmcs12->cr4_read_shadow = evmcs->cr4_read_shadow;
1697 vmcs12->guest_cr0 = evmcs->guest_cr0;
1698 vmcs12->guest_cr3 = evmcs->guest_cr3;
1699 vmcs12->guest_cr4 = evmcs->guest_cr4;
1700 vmcs12->guest_dr7 = evmcs->guest_dr7;
1701 }
1702
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001703 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001704 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER))) {
1705 vmcs12->host_fs_base = evmcs->host_fs_base;
1706 vmcs12->host_gs_base = evmcs->host_gs_base;
1707 vmcs12->host_tr_base = evmcs->host_tr_base;
1708 vmcs12->host_gdtr_base = evmcs->host_gdtr_base;
1709 vmcs12->host_idtr_base = evmcs->host_idtr_base;
1710 vmcs12->host_rsp = evmcs->host_rsp;
1711 }
1712
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001713 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001714 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_XLAT))) {
1715 vmcs12->ept_pointer = evmcs->ept_pointer;
1716 vmcs12->virtual_processor_id = evmcs->virtual_processor_id;
1717 }
1718
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001719 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001720 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1))) {
1721 vmcs12->vmcs_link_pointer = evmcs->vmcs_link_pointer;
1722 vmcs12->guest_ia32_debugctl = evmcs->guest_ia32_debugctl;
1723 vmcs12->guest_ia32_pat = evmcs->guest_ia32_pat;
1724 vmcs12->guest_ia32_efer = evmcs->guest_ia32_efer;
1725 vmcs12->guest_pdptr0 = evmcs->guest_pdptr0;
1726 vmcs12->guest_pdptr1 = evmcs->guest_pdptr1;
1727 vmcs12->guest_pdptr2 = evmcs->guest_pdptr2;
1728 vmcs12->guest_pdptr3 = evmcs->guest_pdptr3;
1729 vmcs12->guest_pending_dbg_exceptions =
1730 evmcs->guest_pending_dbg_exceptions;
1731 vmcs12->guest_sysenter_esp = evmcs->guest_sysenter_esp;
1732 vmcs12->guest_sysenter_eip = evmcs->guest_sysenter_eip;
1733 vmcs12->guest_bndcfgs = evmcs->guest_bndcfgs;
1734 vmcs12->guest_activity_state = evmcs->guest_activity_state;
1735 vmcs12->guest_sysenter_cs = evmcs->guest_sysenter_cs;
1736 }
1737
1738 /*
1739 * Not used?
1740 * vmcs12->vm_exit_msr_store_addr = evmcs->vm_exit_msr_store_addr;
1741 * vmcs12->vm_exit_msr_load_addr = evmcs->vm_exit_msr_load_addr;
1742 * vmcs12->vm_entry_msr_load_addr = evmcs->vm_entry_msr_load_addr;
Sean Christopherson55d23752018-12-03 13:53:18 -08001743 * vmcs12->page_fault_error_code_mask =
1744 * evmcs->page_fault_error_code_mask;
1745 * vmcs12->page_fault_error_code_match =
1746 * evmcs->page_fault_error_code_match;
1747 * vmcs12->cr3_target_count = evmcs->cr3_target_count;
1748 * vmcs12->vm_exit_msr_store_count = evmcs->vm_exit_msr_store_count;
1749 * vmcs12->vm_exit_msr_load_count = evmcs->vm_exit_msr_load_count;
1750 * vmcs12->vm_entry_msr_load_count = evmcs->vm_entry_msr_load_count;
1751 */
1752
1753 /*
1754 * Read only fields:
1755 * vmcs12->guest_physical_address = evmcs->guest_physical_address;
1756 * vmcs12->vm_instruction_error = evmcs->vm_instruction_error;
1757 * vmcs12->vm_exit_reason = evmcs->vm_exit_reason;
1758 * vmcs12->vm_exit_intr_info = evmcs->vm_exit_intr_info;
1759 * vmcs12->vm_exit_intr_error_code = evmcs->vm_exit_intr_error_code;
1760 * vmcs12->idt_vectoring_info_field = evmcs->idt_vectoring_info_field;
1761 * vmcs12->idt_vectoring_error_code = evmcs->idt_vectoring_error_code;
1762 * vmcs12->vm_exit_instruction_len = evmcs->vm_exit_instruction_len;
1763 * vmcs12->vmx_instruction_info = evmcs->vmx_instruction_info;
1764 * vmcs12->exit_qualification = evmcs->exit_qualification;
1765 * vmcs12->guest_linear_address = evmcs->guest_linear_address;
1766 *
1767 * Not present in struct vmcs12:
1768 * vmcs12->exit_io_instruction_ecx = evmcs->exit_io_instruction_ecx;
1769 * vmcs12->exit_io_instruction_esi = evmcs->exit_io_instruction_esi;
1770 * vmcs12->exit_io_instruction_edi = evmcs->exit_io_instruction_edi;
1771 * vmcs12->exit_io_instruction_eip = evmcs->exit_io_instruction_eip;
1772 */
1773
Vitaly Kuznetsov25641ca2021-05-26 15:20:19 +02001774 return;
Sean Christopherson55d23752018-12-03 13:53:18 -08001775}
1776
Vitaly Kuznetsov25641ca2021-05-26 15:20:19 +02001777static void copy_vmcs12_to_enlightened(struct vcpu_vmx *vmx)
Sean Christopherson55d23752018-12-03 13:53:18 -08001778{
1779 struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12;
1780 struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
1781
1782 /*
1783 * Should not be changed by KVM:
1784 *
1785 * evmcs->host_es_selector = vmcs12->host_es_selector;
1786 * evmcs->host_cs_selector = vmcs12->host_cs_selector;
1787 * evmcs->host_ss_selector = vmcs12->host_ss_selector;
1788 * evmcs->host_ds_selector = vmcs12->host_ds_selector;
1789 * evmcs->host_fs_selector = vmcs12->host_fs_selector;
1790 * evmcs->host_gs_selector = vmcs12->host_gs_selector;
1791 * evmcs->host_tr_selector = vmcs12->host_tr_selector;
1792 * evmcs->host_ia32_pat = vmcs12->host_ia32_pat;
1793 * evmcs->host_ia32_efer = vmcs12->host_ia32_efer;
1794 * evmcs->host_cr0 = vmcs12->host_cr0;
1795 * evmcs->host_cr3 = vmcs12->host_cr3;
1796 * evmcs->host_cr4 = vmcs12->host_cr4;
1797 * evmcs->host_ia32_sysenter_esp = vmcs12->host_ia32_sysenter_esp;
1798 * evmcs->host_ia32_sysenter_eip = vmcs12->host_ia32_sysenter_eip;
1799 * evmcs->host_rip = vmcs12->host_rip;
1800 * evmcs->host_ia32_sysenter_cs = vmcs12->host_ia32_sysenter_cs;
1801 * evmcs->host_fs_base = vmcs12->host_fs_base;
1802 * evmcs->host_gs_base = vmcs12->host_gs_base;
1803 * evmcs->host_tr_base = vmcs12->host_tr_base;
1804 * evmcs->host_gdtr_base = vmcs12->host_gdtr_base;
1805 * evmcs->host_idtr_base = vmcs12->host_idtr_base;
1806 * evmcs->host_rsp = vmcs12->host_rsp;
Sean Christopherson3731905ef2019-05-07 08:36:27 -07001807 * sync_vmcs02_to_vmcs12() doesn't read these:
Sean Christopherson55d23752018-12-03 13:53:18 -08001808 * evmcs->io_bitmap_a = vmcs12->io_bitmap_a;
1809 * evmcs->io_bitmap_b = vmcs12->io_bitmap_b;
1810 * evmcs->msr_bitmap = vmcs12->msr_bitmap;
1811 * evmcs->ept_pointer = vmcs12->ept_pointer;
1812 * evmcs->xss_exit_bitmap = vmcs12->xss_exit_bitmap;
1813 * evmcs->vm_exit_msr_store_addr = vmcs12->vm_exit_msr_store_addr;
1814 * evmcs->vm_exit_msr_load_addr = vmcs12->vm_exit_msr_load_addr;
1815 * evmcs->vm_entry_msr_load_addr = vmcs12->vm_entry_msr_load_addr;
Sean Christopherson55d23752018-12-03 13:53:18 -08001816 * evmcs->tpr_threshold = vmcs12->tpr_threshold;
1817 * evmcs->virtual_processor_id = vmcs12->virtual_processor_id;
1818 * evmcs->exception_bitmap = vmcs12->exception_bitmap;
1819 * evmcs->vmcs_link_pointer = vmcs12->vmcs_link_pointer;
1820 * evmcs->pin_based_vm_exec_control = vmcs12->pin_based_vm_exec_control;
1821 * evmcs->vm_exit_controls = vmcs12->vm_exit_controls;
1822 * evmcs->secondary_vm_exec_control = vmcs12->secondary_vm_exec_control;
1823 * evmcs->page_fault_error_code_mask =
1824 * vmcs12->page_fault_error_code_mask;
1825 * evmcs->page_fault_error_code_match =
1826 * vmcs12->page_fault_error_code_match;
1827 * evmcs->cr3_target_count = vmcs12->cr3_target_count;
1828 * evmcs->virtual_apic_page_addr = vmcs12->virtual_apic_page_addr;
1829 * evmcs->tsc_offset = vmcs12->tsc_offset;
1830 * evmcs->guest_ia32_debugctl = vmcs12->guest_ia32_debugctl;
1831 * evmcs->cr0_guest_host_mask = vmcs12->cr0_guest_host_mask;
1832 * evmcs->cr4_guest_host_mask = vmcs12->cr4_guest_host_mask;
1833 * evmcs->cr0_read_shadow = vmcs12->cr0_read_shadow;
1834 * evmcs->cr4_read_shadow = vmcs12->cr4_read_shadow;
1835 * evmcs->vm_exit_msr_store_count = vmcs12->vm_exit_msr_store_count;
1836 * evmcs->vm_exit_msr_load_count = vmcs12->vm_exit_msr_load_count;
1837 * evmcs->vm_entry_msr_load_count = vmcs12->vm_entry_msr_load_count;
1838 *
1839 * Not present in struct vmcs12:
1840 * evmcs->exit_io_instruction_ecx = vmcs12->exit_io_instruction_ecx;
1841 * evmcs->exit_io_instruction_esi = vmcs12->exit_io_instruction_esi;
1842 * evmcs->exit_io_instruction_edi = vmcs12->exit_io_instruction_edi;
1843 * evmcs->exit_io_instruction_eip = vmcs12->exit_io_instruction_eip;
1844 */
1845
1846 evmcs->guest_es_selector = vmcs12->guest_es_selector;
1847 evmcs->guest_cs_selector = vmcs12->guest_cs_selector;
1848 evmcs->guest_ss_selector = vmcs12->guest_ss_selector;
1849 evmcs->guest_ds_selector = vmcs12->guest_ds_selector;
1850 evmcs->guest_fs_selector = vmcs12->guest_fs_selector;
1851 evmcs->guest_gs_selector = vmcs12->guest_gs_selector;
1852 evmcs->guest_ldtr_selector = vmcs12->guest_ldtr_selector;
1853 evmcs->guest_tr_selector = vmcs12->guest_tr_selector;
1854
1855 evmcs->guest_es_limit = vmcs12->guest_es_limit;
1856 evmcs->guest_cs_limit = vmcs12->guest_cs_limit;
1857 evmcs->guest_ss_limit = vmcs12->guest_ss_limit;
1858 evmcs->guest_ds_limit = vmcs12->guest_ds_limit;
1859 evmcs->guest_fs_limit = vmcs12->guest_fs_limit;
1860 evmcs->guest_gs_limit = vmcs12->guest_gs_limit;
1861 evmcs->guest_ldtr_limit = vmcs12->guest_ldtr_limit;
1862 evmcs->guest_tr_limit = vmcs12->guest_tr_limit;
1863 evmcs->guest_gdtr_limit = vmcs12->guest_gdtr_limit;
1864 evmcs->guest_idtr_limit = vmcs12->guest_idtr_limit;
1865
1866 evmcs->guest_es_ar_bytes = vmcs12->guest_es_ar_bytes;
1867 evmcs->guest_cs_ar_bytes = vmcs12->guest_cs_ar_bytes;
1868 evmcs->guest_ss_ar_bytes = vmcs12->guest_ss_ar_bytes;
1869 evmcs->guest_ds_ar_bytes = vmcs12->guest_ds_ar_bytes;
1870 evmcs->guest_fs_ar_bytes = vmcs12->guest_fs_ar_bytes;
1871 evmcs->guest_gs_ar_bytes = vmcs12->guest_gs_ar_bytes;
1872 evmcs->guest_ldtr_ar_bytes = vmcs12->guest_ldtr_ar_bytes;
1873 evmcs->guest_tr_ar_bytes = vmcs12->guest_tr_ar_bytes;
1874
1875 evmcs->guest_es_base = vmcs12->guest_es_base;
1876 evmcs->guest_cs_base = vmcs12->guest_cs_base;
1877 evmcs->guest_ss_base = vmcs12->guest_ss_base;
1878 evmcs->guest_ds_base = vmcs12->guest_ds_base;
1879 evmcs->guest_fs_base = vmcs12->guest_fs_base;
1880 evmcs->guest_gs_base = vmcs12->guest_gs_base;
1881 evmcs->guest_ldtr_base = vmcs12->guest_ldtr_base;
1882 evmcs->guest_tr_base = vmcs12->guest_tr_base;
1883 evmcs->guest_gdtr_base = vmcs12->guest_gdtr_base;
1884 evmcs->guest_idtr_base = vmcs12->guest_idtr_base;
1885
1886 evmcs->guest_ia32_pat = vmcs12->guest_ia32_pat;
1887 evmcs->guest_ia32_efer = vmcs12->guest_ia32_efer;
1888
1889 evmcs->guest_pdptr0 = vmcs12->guest_pdptr0;
1890 evmcs->guest_pdptr1 = vmcs12->guest_pdptr1;
1891 evmcs->guest_pdptr2 = vmcs12->guest_pdptr2;
1892 evmcs->guest_pdptr3 = vmcs12->guest_pdptr3;
1893
1894 evmcs->guest_pending_dbg_exceptions =
1895 vmcs12->guest_pending_dbg_exceptions;
1896 evmcs->guest_sysenter_esp = vmcs12->guest_sysenter_esp;
1897 evmcs->guest_sysenter_eip = vmcs12->guest_sysenter_eip;
1898
1899 evmcs->guest_activity_state = vmcs12->guest_activity_state;
1900 evmcs->guest_sysenter_cs = vmcs12->guest_sysenter_cs;
1901
1902 evmcs->guest_cr0 = vmcs12->guest_cr0;
1903 evmcs->guest_cr3 = vmcs12->guest_cr3;
1904 evmcs->guest_cr4 = vmcs12->guest_cr4;
1905 evmcs->guest_dr7 = vmcs12->guest_dr7;
1906
1907 evmcs->guest_physical_address = vmcs12->guest_physical_address;
1908
1909 evmcs->vm_instruction_error = vmcs12->vm_instruction_error;
1910 evmcs->vm_exit_reason = vmcs12->vm_exit_reason;
1911 evmcs->vm_exit_intr_info = vmcs12->vm_exit_intr_info;
1912 evmcs->vm_exit_intr_error_code = vmcs12->vm_exit_intr_error_code;
1913 evmcs->idt_vectoring_info_field = vmcs12->idt_vectoring_info_field;
1914 evmcs->idt_vectoring_error_code = vmcs12->idt_vectoring_error_code;
1915 evmcs->vm_exit_instruction_len = vmcs12->vm_exit_instruction_len;
1916 evmcs->vmx_instruction_info = vmcs12->vmx_instruction_info;
1917
1918 evmcs->exit_qualification = vmcs12->exit_qualification;
1919
1920 evmcs->guest_linear_address = vmcs12->guest_linear_address;
1921 evmcs->guest_rsp = vmcs12->guest_rsp;
1922 evmcs->guest_rflags = vmcs12->guest_rflags;
1923
1924 evmcs->guest_interruptibility_info =
1925 vmcs12->guest_interruptibility_info;
1926 evmcs->cpu_based_vm_exec_control = vmcs12->cpu_based_vm_exec_control;
1927 evmcs->vm_entry_controls = vmcs12->vm_entry_controls;
1928 evmcs->vm_entry_intr_info_field = vmcs12->vm_entry_intr_info_field;
1929 evmcs->vm_entry_exception_error_code =
1930 vmcs12->vm_entry_exception_error_code;
1931 evmcs->vm_entry_instruction_len = vmcs12->vm_entry_instruction_len;
1932
1933 evmcs->guest_rip = vmcs12->guest_rip;
1934
1935 evmcs->guest_bndcfgs = vmcs12->guest_bndcfgs;
1936
Vitaly Kuznetsov25641ca2021-05-26 15:20:19 +02001937 return;
Sean Christopherson55d23752018-12-03 13:53:18 -08001938}
1939
1940/*
1941 * This is an equivalent of the nested hypervisor executing the vmptrld
1942 * instruction.
1943 */
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01001944static enum nested_evmptrld_status nested_vmx_handle_enlightened_vmptrld(
1945 struct kvm_vcpu *vcpu, bool from_launch)
Sean Christopherson55d23752018-12-03 13:53:18 -08001946{
1947 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsova21a39c2019-06-28 13:23:32 +02001948 bool evmcs_gpa_changed = false;
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02001949 u64 evmcs_gpa;
Sean Christopherson55d23752018-12-03 13:53:18 -08001950
1951 if (likely(!vmx->nested.enlightened_vmcs_enabled))
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01001952 return EVMPTRLD_DISABLED;
Sean Christopherson55d23752018-12-03 13:53:18 -08001953
Vitaly Kuznetsov02761712021-05-26 15:20:18 +02001954 if (!nested_enlightened_vmentry(vcpu, &evmcs_gpa)) {
1955 nested_release_evmcs(vcpu);
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01001956 return EVMPTRLD_DISABLED;
Vitaly Kuznetsov02761712021-05-26 15:20:18 +02001957 }
Sean Christopherson55d23752018-12-03 13:53:18 -08001958
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02001959 if (unlikely(evmcs_gpa != vmx->nested.hv_evmcs_vmptr)) {
1960 vmx->nested.current_vmptr = -1ull;
Sean Christopherson55d23752018-12-03 13:53:18 -08001961
1962 nested_release_evmcs(vcpu);
1963
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02001964 if (kvm_vcpu_map(vcpu, gpa_to_gfn(evmcs_gpa),
KarimAllah Ahmeddee9c042019-01-31 21:24:42 +01001965 &vmx->nested.hv_evmcs_map))
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01001966 return EVMPTRLD_ERROR;
Sean Christopherson55d23752018-12-03 13:53:18 -08001967
KarimAllah Ahmeddee9c042019-01-31 21:24:42 +01001968 vmx->nested.hv_evmcs = vmx->nested.hv_evmcs_map.hva;
Sean Christopherson55d23752018-12-03 13:53:18 -08001969
1970 /*
1971 * Currently, KVM only supports eVMCS version 1
1972 * (== KVM_EVMCS_VERSION) and thus we expect guest to set this
1973 * value to first u32 field of eVMCS which should specify eVMCS
1974 * VersionNumber.
1975 *
1976 * Guest should be aware of supported eVMCS versions by host by
1977 * examining CPUID.0x4000000A.EAX[0:15]. Host userspace VMM is
1978 * expected to set this CPUID leaf according to the value
1979 * returned in vmcs_version from nested_enable_evmcs().
1980 *
1981 * However, it turns out that Microsoft Hyper-V fails to comply
1982 * to their own invented interface: When Hyper-V use eVMCS, it
1983 * just sets first u32 field of eVMCS to revision_id specified
1984 * in MSR_IA32_VMX_BASIC. Instead of used eVMCS version number
1985 * which is one of the supported versions specified in
1986 * CPUID.0x4000000A.EAX[0:15].
1987 *
1988 * To overcome Hyper-V bug, we accept here either a supported
1989 * eVMCS version or VMCS12 revision_id as valid values for first
1990 * u32 field of eVMCS.
1991 */
1992 if ((vmx->nested.hv_evmcs->revision_id != KVM_EVMCS_VERSION) &&
1993 (vmx->nested.hv_evmcs->revision_id != VMCS12_REVISION)) {
1994 nested_release_evmcs(vcpu);
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01001995 return EVMPTRLD_VMFAIL;
Sean Christopherson55d23752018-12-03 13:53:18 -08001996 }
1997
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02001998 vmx->nested.hv_evmcs_vmptr = evmcs_gpa;
Sean Christopherson55d23752018-12-03 13:53:18 -08001999
Vitaly Kuznetsova21a39c2019-06-28 13:23:32 +02002000 evmcs_gpa_changed = true;
Sean Christopherson55d23752018-12-03 13:53:18 -08002001 /*
2002 * Unlike normal vmcs12, enlightened vmcs12 is not fully
2003 * reloaded from guest's memory (read only fields, fields not
2004 * present in struct hv_enlightened_vmcs, ...). Make sure there
2005 * are no leftovers.
2006 */
2007 if (from_launch) {
2008 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2009 memset(vmcs12, 0, sizeof(*vmcs12));
2010 vmcs12->hdr.revision_id = VMCS12_REVISION;
2011 }
2012
2013 }
Vitaly Kuznetsova21a39c2019-06-28 13:23:32 +02002014
2015 /*
Miaohe Linffdbd502020-02-07 23:22:45 +08002016 * Clean fields data can't be used on VMLAUNCH and when we switch
Vitaly Kuznetsova21a39c2019-06-28 13:23:32 +02002017 * between different L2 guests as KVM keeps a single VMCS12 per L1.
2018 */
2019 if (from_launch || evmcs_gpa_changed)
2020 vmx->nested.hv_evmcs->hv_clean_fields &=
2021 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
2022
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01002023 return EVMPTRLD_SUCCEEDED;
Sean Christopherson55d23752018-12-03 13:53:18 -08002024}
2025
Sean Christopherson3731905ef2019-05-07 08:36:27 -07002026void nested_sync_vmcs12_to_shadow(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08002027{
2028 struct vcpu_vmx *vmx = to_vmx(vcpu);
2029
Vitaly Kuznetsovdc313382021-05-26 15:20:24 +02002030 if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
Sean Christopherson55d23752018-12-03 13:53:18 -08002031 copy_vmcs12_to_enlightened(vmx);
Vitaly Kuznetsovdc313382021-05-26 15:20:24 +02002032 else
Sean Christopherson55d23752018-12-03 13:53:18 -08002033 copy_vmcs12_to_shadow(vmx);
Sean Christopherson55d23752018-12-03 13:53:18 -08002034
Sean Christopherson3731905ef2019-05-07 08:36:27 -07002035 vmx->nested.need_vmcs12_to_shadow_sync = false;
Sean Christopherson55d23752018-12-03 13:53:18 -08002036}
2037
2038static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
2039{
2040 struct vcpu_vmx *vmx =
2041 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
2042
2043 vmx->nested.preemption_timer_expired = true;
2044 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
2045 kvm_vcpu_kick(&vmx->vcpu);
2046
2047 return HRTIMER_NORESTART;
2048}
2049
Peter Shier850448f2020-05-26 14:51:06 -07002050static u64 vmx_calc_preemption_timer_value(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08002051{
Peter Shier850448f2020-05-26 14:51:06 -07002052 struct vcpu_vmx *vmx = to_vmx(vcpu);
2053 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Peter Shier850448f2020-05-26 14:51:06 -07002054
2055 u64 l1_scaled_tsc = kvm_read_l1_tsc(vcpu, rdtsc()) >>
2056 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
2057
2058 if (!vmx->nested.has_preemption_timer_deadline) {
Makarand Sonare8d7fbf02020-05-26 14:51:07 -07002059 vmx->nested.preemption_timer_deadline =
2060 vmcs12->vmx_preemption_timer_value + l1_scaled_tsc;
Peter Shier850448f2020-05-26 14:51:06 -07002061 vmx->nested.has_preemption_timer_deadline = true;
Makarand Sonare8d7fbf02020-05-26 14:51:07 -07002062 }
2063 return vmx->nested.preemption_timer_deadline - l1_scaled_tsc;
Peter Shier850448f2020-05-26 14:51:06 -07002064}
2065
2066static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu,
2067 u64 preemption_timeout)
2068{
Sean Christopherson55d23752018-12-03 13:53:18 -08002069 struct vcpu_vmx *vmx = to_vmx(vcpu);
2070
2071 /*
2072 * A timer value of zero is architecturally guaranteed to cause
2073 * a VMExit prior to executing any instructions in the guest.
2074 */
2075 if (preemption_timeout == 0) {
2076 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
2077 return;
2078 }
2079
2080 if (vcpu->arch.virtual_tsc_khz == 0)
2081 return;
2082
2083 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
2084 preemption_timeout *= 1000000;
2085 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
2086 hrtimer_start(&vmx->nested.preemption_timer,
Jim Mattsonada00982020-05-08 13:36:42 -07002087 ktime_add_ns(ktime_get(), preemption_timeout),
2088 HRTIMER_MODE_ABS_PINNED);
Sean Christopherson55d23752018-12-03 13:53:18 -08002089}
2090
2091static u64 nested_vmx_calc_efer(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
2092{
2093 if (vmx->nested.nested_run_pending &&
2094 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
2095 return vmcs12->guest_ia32_efer;
2096 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
2097 return vmx->vcpu.arch.efer | (EFER_LMA | EFER_LME);
2098 else
2099 return vmx->vcpu.arch.efer & ~(EFER_LMA | EFER_LME);
2100}
2101
2102static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx)
2103{
2104 /*
2105 * If vmcs02 hasn't been initialized, set the constant vmcs02 state
2106 * according to L0's settings (vmcs12 is irrelevant here). Host
2107 * fields that come from L0 and are not constant, e.g. HOST_CR3,
2108 * will be set as needed prior to VMLAUNCH/VMRESUME.
2109 */
2110 if (vmx->nested.vmcs02_initialized)
2111 return;
2112 vmx->nested.vmcs02_initialized = true;
2113
2114 /*
2115 * We don't care what the EPTP value is we just need to guarantee
2116 * it's valid so we don't get a false positive when doing early
2117 * consistency checks.
2118 */
2119 if (enable_ept && nested_early_check)
Sean Christopherson2a40b902020-07-15 20:41:18 -07002120 vmcs_write64(EPT_POINTER,
2121 construct_eptp(&vmx->vcpu, 0, PT64_ROOT_4LEVEL));
Sean Christopherson55d23752018-12-03 13:53:18 -08002122
2123 /* All VMFUNCs are currently emulated through L0 vmexits. */
2124 if (cpu_has_vmx_vmfunc())
2125 vmcs_write64(VM_FUNCTION_CONTROL, 0);
2126
2127 if (cpu_has_vmx_posted_intr())
2128 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
2129
2130 if (cpu_has_vmx_msr_bitmap())
2131 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
2132
Sean Christopherson4d6c9892019-05-07 09:06:30 -07002133 /*
Sean Christophersonc3bb9a22021-02-12 16:50:07 -08002134 * PML is emulated for L2, but never enabled in hardware as the MMU
2135 * handles A/D emulation. Disabling PML for L2 also avoids having to
2136 * deal with filtering out L2 GPAs from the buffer.
Sean Christopherson4d6c9892019-05-07 09:06:30 -07002137 */
2138 if (enable_pml) {
Sean Christophersonc3bb9a22021-02-12 16:50:07 -08002139 vmcs_write64(PML_ADDRESS, 0);
2140 vmcs_write16(GUEST_PML_INDEX, -1);
Sean Christopherson4d6c9892019-05-07 09:06:30 -07002141 }
Sean Christopherson55d23752018-12-03 13:53:18 -08002142
Sean Christophersonc538d572019-05-07 09:06:29 -07002143 if (cpu_has_vmx_encls_vmexit())
2144 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
Sean Christopherson55d23752018-12-03 13:53:18 -08002145
2146 /*
2147 * Set the MSR load/store lists to match L0's settings. Only the
2148 * addresses are constant (for vmcs02), the counts can change based
2149 * on L2's behavior, e.g. switching to/from long mode.
2150 */
Aaron Lewis662f1d12019-11-07 21:14:39 -08002151 vmcs_write64(VM_EXIT_MSR_STORE_ADDR, __pa(vmx->msr_autostore.guest.val));
Sean Christopherson55d23752018-12-03 13:53:18 -08002152 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
2153 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
2154
2155 vmx_set_constant_host_state(vmx);
2156}
2157
Paolo Bonzinib1346ab2019-06-06 17:24:00 +02002158static void prepare_vmcs02_early_rare(struct vcpu_vmx *vmx,
Sean Christopherson55d23752018-12-03 13:53:18 -08002159 struct vmcs12 *vmcs12)
2160{
2161 prepare_vmcs02_constant_state(vmx);
2162
2163 vmcs_write64(VMCS_LINK_POINTER, -1ull);
2164
2165 if (enable_vpid) {
2166 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
2167 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
2168 else
2169 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
2170 }
2171}
2172
2173static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
2174{
Sean Christophersonc3bb9a22021-02-12 16:50:07 -08002175 u32 exec_control;
Sean Christopherson55d23752018-12-03 13:53:18 -08002176 u64 guest_efer = nested_vmx_calc_efer(vmx, vmcs12);
2177
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02002178 if (vmx->nested.dirty_vmcs12 || evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
Paolo Bonzinib1346ab2019-06-06 17:24:00 +02002179 prepare_vmcs02_early_rare(vmx, vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08002180
2181 /*
Sean Christopherson55d23752018-12-03 13:53:18 -08002182 * PIN CONTROLS
2183 */
Sean Christophersonc075c3e2019-05-07 12:17:53 -07002184 exec_control = vmx_pin_based_exec_ctrl(vmx);
Sean Christopherson804939e2019-05-07 12:18:05 -07002185 exec_control |= (vmcs12->pin_based_vm_exec_control &
2186 ~PIN_BASED_VMX_PREEMPTION_TIMER);
Sean Christopherson55d23752018-12-03 13:53:18 -08002187
2188 /* Posted interrupts setting is only taken from vmcs12. */
2189 if (nested_cpu_has_posted_intr(vmcs12)) {
2190 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
2191 vmx->nested.pi_pending = false;
2192 } else {
2193 exec_control &= ~PIN_BASED_POSTED_INTR;
2194 }
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002195 pin_controls_set(vmx, exec_control);
Sean Christopherson55d23752018-12-03 13:53:18 -08002196
2197 /*
2198 * EXEC CONTROLS
2199 */
2200 exec_control = vmx_exec_control(vmx); /* L0's desires */
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08002201 exec_control &= ~CPU_BASED_INTR_WINDOW_EXITING;
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08002202 exec_control &= ~CPU_BASED_NMI_WINDOW_EXITING;
Sean Christopherson55d23752018-12-03 13:53:18 -08002203 exec_control &= ~CPU_BASED_TPR_SHADOW;
2204 exec_control |= vmcs12->cpu_based_vm_exec_control;
2205
Liran Alon02d496cf2019-11-11 14:30:55 +02002206 vmx->nested.l1_tpr_threshold = -1;
Sean Christophersonca2f5462019-05-07 09:06:33 -07002207 if (exec_control & CPU_BASED_TPR_SHADOW)
Sean Christopherson55d23752018-12-03 13:53:18 -08002208 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Sean Christopherson55d23752018-12-03 13:53:18 -08002209#ifdef CONFIG_X86_64
Sean Christophersonca2f5462019-05-07 09:06:33 -07002210 else
Sean Christopherson55d23752018-12-03 13:53:18 -08002211 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
2212 CPU_BASED_CR8_STORE_EXITING;
2213#endif
Sean Christopherson55d23752018-12-03 13:53:18 -08002214
2215 /*
2216 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
2217 * for I/O port accesses.
2218 */
Sean Christopherson55d23752018-12-03 13:53:18 -08002219 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
Sean Christophersonde0286b2019-05-07 12:18:01 -07002220 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
2221
2222 /*
2223 * This bit will be computed in nested_get_vmcs12_pages, because
2224 * we do not have access to L1's MSR bitmap yet. For now, keep
2225 * the same bit as before, hoping to avoid multiple VMWRITEs that
2226 * only set/clear this bit.
2227 */
2228 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
2229 exec_control |= exec_controls_get(vmx) & CPU_BASED_USE_MSR_BITMAPS;
2230
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002231 exec_controls_set(vmx, exec_control);
Sean Christopherson55d23752018-12-03 13:53:18 -08002232
2233 /*
2234 * SECONDARY EXEC CONTROLS
2235 */
2236 if (cpu_has_secondary_exec_ctrls()) {
2237 exec_control = vmx->secondary_exec_control;
2238
2239 /* Take the following fields only from vmcs12 */
2240 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
2241 SECONDARY_EXEC_ENABLE_INVPCID |
Sean Christopherson7f3603b2020-09-23 09:50:47 -07002242 SECONDARY_EXEC_ENABLE_RDTSCP |
Sean Christopherson55d23752018-12-03 13:53:18 -08002243 SECONDARY_EXEC_XSAVES |
Tao Xue69e72fa2019-07-16 14:55:49 +08002244 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE |
Sean Christopherson55d23752018-12-03 13:53:18 -08002245 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
2246 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Ilias Stamatisd041b5e2021-05-26 19:44:17 +01002247 SECONDARY_EXEC_ENABLE_VMFUNC |
2248 SECONDARY_EXEC_TSC_SCALING);
Sean Christopherson55d23752018-12-03 13:53:18 -08002249 if (nested_cpu_has(vmcs12,
Sean Christophersonc3bb9a22021-02-12 16:50:07 -08002250 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
2251 exec_control |= vmcs12->secondary_vm_exec_control;
2252
2253 /* PML is emulated and never enabled in hardware for L2. */
2254 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Sean Christopherson55d23752018-12-03 13:53:18 -08002255
2256 /* VMCS shadowing for L2 is emulated for now */
2257 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
2258
Sean Christopherson469debd2019-05-07 12:18:02 -07002259 /*
2260 * Preset *DT exiting when emulating UMIP, so that vmx_set_cr4()
2261 * will not have to rewrite the controls just for this bit.
2262 */
2263 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated() &&
2264 (vmcs12->guest_cr4 & X86_CR4_UMIP))
2265 exec_control |= SECONDARY_EXEC_DESC;
2266
Sean Christopherson55d23752018-12-03 13:53:18 -08002267 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
2268 vmcs_write16(GUEST_INTR_STATUS,
2269 vmcs12->guest_intr_status);
2270
Krish Sadhukhanbddd82d2020-09-21 08:10:25 +00002271 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
2272 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
2273
Sean Christopherson72add912021-04-12 16:21:42 +12002274 if (exec_control & SECONDARY_EXEC_ENCLS_EXITING)
2275 vmx_write_encls_bitmap(&vmx->vcpu, vmcs12);
2276
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002277 secondary_exec_controls_set(vmx, exec_control);
Sean Christopherson55d23752018-12-03 13:53:18 -08002278 }
2279
2280 /*
2281 * ENTRY CONTROLS
2282 *
2283 * vmcs12's VM_{ENTRY,EXIT}_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE
2284 * are emulated by vmx_set_efer() in prepare_vmcs02(), but speculate
2285 * on the related bits (if supported by the CPU) in the hope that
2286 * we can avoid VMWrites during vmx_set_efer().
2287 */
2288 exec_control = (vmcs12->vm_entry_controls | vmx_vmentry_ctrl()) &
2289 ~VM_ENTRY_IA32E_MODE & ~VM_ENTRY_LOAD_IA32_EFER;
2290 if (cpu_has_load_ia32_efer()) {
2291 if (guest_efer & EFER_LMA)
2292 exec_control |= VM_ENTRY_IA32E_MODE;
2293 if (guest_efer != host_efer)
2294 exec_control |= VM_ENTRY_LOAD_IA32_EFER;
2295 }
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002296 vm_entry_controls_set(vmx, exec_control);
Sean Christopherson55d23752018-12-03 13:53:18 -08002297
2298 /*
2299 * EXIT CONTROLS
2300 *
2301 * L2->L1 exit controls are emulated - the hardware exit is to L0 so
2302 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
2303 * bits may be modified by vmx_set_efer() in prepare_vmcs02().
2304 */
2305 exec_control = vmx_vmexit_ctrl();
2306 if (cpu_has_load_ia32_efer() && guest_efer != host_efer)
2307 exec_control |= VM_EXIT_LOAD_IA32_EFER;
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002308 vm_exit_controls_set(vmx, exec_control);
Sean Christopherson55d23752018-12-03 13:53:18 -08002309
2310 /*
2311 * Interrupt/Exception Fields
2312 */
2313 if (vmx->nested.nested_run_pending) {
2314 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2315 vmcs12->vm_entry_intr_info_field);
2316 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
2317 vmcs12->vm_entry_exception_error_code);
2318 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2319 vmcs12->vm_entry_instruction_len);
2320 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
2321 vmcs12->guest_interruptibility_info);
2322 vmx->loaded_vmcs->nmi_known_unmasked =
2323 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
2324 } else {
2325 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
2326 }
2327}
2328
Paolo Bonzinib1346ab2019-06-06 17:24:00 +02002329static void prepare_vmcs02_rare(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
Sean Christopherson55d23752018-12-03 13:53:18 -08002330{
2331 struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs;
2332
2333 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
2334 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2)) {
2335 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
2336 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
2337 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
2338 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
2339 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
2340 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
2341 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
2342 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
2343 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
2344 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
2345 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
2346 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
2347 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
2348 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
2349 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
2350 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
2351 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
2352 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07002353 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
2354 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
Sean Christopherson55d23752018-12-03 13:53:18 -08002355 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
2356 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
2357 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
2358 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
2359 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
2360 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
2361 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
2362 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
2363 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
2364 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
2365 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
2366 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
2367 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
2368 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
2369 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
2370 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Sean Christophersonfc387d82020-09-23 11:44:46 -07002371
2372 vmx->segment_cache.bitmask = 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08002373 }
2374
2375 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
2376 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1)) {
2377 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
2378 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
2379 vmcs12->guest_pending_dbg_exceptions);
2380 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
2381 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
2382
2383 /*
2384 * L1 may access the L2's PDPTR, so save them to construct
2385 * vmcs12
2386 */
2387 if (enable_ept) {
2388 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
2389 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
2390 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
2391 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
2392 }
Sean Christophersonc27e5b02019-05-07 09:06:39 -07002393
2394 if (kvm_mpx_supported() && vmx->nested.nested_run_pending &&
2395 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
2396 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
Sean Christopherson55d23752018-12-03 13:53:18 -08002397 }
2398
2399 if (nested_cpu_has_xsaves(vmcs12))
2400 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
2401
2402 /*
2403 * Whether page-faults are trapped is determined by a combination of
Paolo Bonzinia0c13432020-07-10 17:48:08 +02002404 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF. If L0
2405 * doesn't care about page faults then we should set all of these to
2406 * L1's desires. However, if L0 does care about (some) page faults, it
2407 * is not easy (if at all possible?) to merge L0 and L1's desires, we
2408 * simply ask to exit on each and every L2 page fault. This is done by
2409 * setting MASK=MATCH=0 and (see below) EB.PF=1.
Sean Christopherson55d23752018-12-03 13:53:18 -08002410 * Note that below we don't need special code to set EB.PF beyond the
2411 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
2412 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
2413 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
2414 */
Paolo Bonzinia0c13432020-07-10 17:48:08 +02002415 if (vmx_need_pf_intercept(&vmx->vcpu)) {
2416 /*
2417 * TODO: if both L0 and L1 need the same MASK and MATCH,
2418 * go ahead and use it?
2419 */
2420 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
2421 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
2422 } else {
2423 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, vmcs12->page_fault_error_code_mask);
2424 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, vmcs12->page_fault_error_code_match);
2425 }
Sean Christopherson55d23752018-12-03 13:53:18 -08002426
2427 if (cpu_has_vmx_apicv()) {
2428 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
2429 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
2430 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
2431 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
2432 }
2433
Aaron Lewis662f1d12019-11-07 21:14:39 -08002434 /*
2435 * Make sure the msr_autostore list is up to date before we set the
2436 * count in the vmcs02.
2437 */
2438 prepare_vmx_msr_autostore_list(&vmx->vcpu, MSR_IA32_TSC);
2439
2440 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, vmx->msr_autostore.guest.nr);
Sean Christopherson55d23752018-12-03 13:53:18 -08002441 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
2442 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
2443
2444 set_cr4_guest_host_mask(vmx);
Sean Christopherson55d23752018-12-03 13:53:18 -08002445}
2446
2447/*
2448 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
2449 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
2450 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
2451 * guest in a way that will both be appropriate to L1's requests, and our
2452 * needs. In addition to modifying the active vmcs (which is vmcs02), this
2453 * function also has additional necessary side-effects, like setting various
2454 * vcpu->arch fields.
2455 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
2456 * is assigned to entry_failure_code on failure.
2457 */
2458static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Maxim Levitsky0f857222021-06-07 12:02:00 +03002459 bool from_vmentry,
Sean Christopherson68cda402020-05-11 15:05:29 -07002460 enum vm_entry_failure_code *entry_failure_code)
Sean Christopherson55d23752018-12-03 13:53:18 -08002461{
2462 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersonc7554efc2019-05-07 09:06:40 -07002463 bool load_guest_pdptrs_vmcs12 = false;
Sean Christopherson55d23752018-12-03 13:53:18 -08002464
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02002465 if (vmx->nested.dirty_vmcs12 || evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) {
Paolo Bonzinib1346ab2019-06-06 17:24:00 +02002466 prepare_vmcs02_rare(vmx, vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08002467 vmx->nested.dirty_vmcs12 = false;
Sean Christopherson55d23752018-12-03 13:53:18 -08002468
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02002469 load_guest_pdptrs_vmcs12 = !evmptr_is_valid(vmx->nested.hv_evmcs_vmptr) ||
2470 !(vmx->nested.hv_evmcs->hv_clean_fields &
Sean Christophersonc7554efc2019-05-07 09:06:40 -07002471 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1);
Sean Christopherson55d23752018-12-03 13:53:18 -08002472 }
2473
2474 if (vmx->nested.nested_run_pending &&
2475 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
2476 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
2477 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
2478 } else {
2479 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
2480 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
2481 }
Sean Christopherson3b013a22019-05-07 09:06:28 -07002482 if (kvm_mpx_supported() && (!vmx->nested.nested_run_pending ||
2483 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)))
2484 vmcs_write64(GUEST_BNDCFGS, vmx->nested.vmcs01_guest_bndcfgs);
Sean Christopherson55d23752018-12-03 13:53:18 -08002485 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
2486
Sean Christopherson55d23752018-12-03 13:53:18 -08002487 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
2488 * bitwise-or of what L1 wants to trap for L2, and what we want to
2489 * trap. Note that CR0.TS also needs updating - we do this later.
2490 */
Jason Baronb6a7cc32021-01-14 22:27:54 -05002491 vmx_update_exception_bitmap(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08002492 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
2493 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
2494
2495 if (vmx->nested.nested_run_pending &&
2496 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
2497 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
2498 vcpu->arch.pat = vmcs12->guest_ia32_pat;
2499 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2500 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
2501 }
2502
Ilias Stamatisd041b5e2021-05-26 19:44:17 +01002503 vcpu->arch.tsc_offset = kvm_calc_nested_tsc_offset(
2504 vcpu->arch.l1_tsc_offset,
2505 vmx_get_l2_tsc_offset(vcpu),
2506 vmx_get_l2_tsc_multiplier(vcpu));
2507
2508 vcpu->arch.tsc_scaling_ratio = kvm_calc_nested_tsc_multiplier(
2509 vcpu->arch.l1_tsc_scaling_ratio,
2510 vmx_get_l2_tsc_multiplier(vcpu));
2511
Sean Christopherson55d23752018-12-03 13:53:18 -08002512 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Sean Christopherson55d23752018-12-03 13:53:18 -08002513 if (kvm_has_tsc_control)
Ilias Stamatis1ab92872021-06-07 11:54:38 +01002514 vmcs_write64(TSC_MULTIPLIER, vcpu->arch.tsc_scaling_ratio);
Sean Christopherson55d23752018-12-03 13:53:18 -08002515
Sean Christopherson50b265a2020-03-20 14:28:19 -07002516 nested_vmx_transition_tlb_flush(vcpu, vmcs12, true);
Sean Christopherson55d23752018-12-03 13:53:18 -08002517
2518 if (nested_cpu_has_ept(vmcs12))
2519 nested_ept_init_mmu_context(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08002520
2521 /*
2522 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
2523 * bits which we consider mandatory enabled.
2524 * The CR0_READ_SHADOW is what L2 should have expected to read given
2525 * the specifications by L1; It's not enough to take
2526 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
2527 * have more bits than L1 expected.
2528 */
2529 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
2530 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
2531
2532 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
2533 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
2534
2535 vcpu->arch.efer = nested_vmx_calc_efer(vmx, vmcs12);
2536 /* Note: may modify VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
2537 vmx_set_efer(vcpu, vcpu->arch.efer);
2538
2539 /*
2540 * Guest state is invalid and unrestricted guest is disabled,
2541 * which means L1 attempted VMEntry to L2 with invalid state.
2542 * Fail the VMEntry.
2543 */
Sean Christopherson2ba44932020-09-23 11:44:48 -07002544 if (CC(!vmx_guest_state_valid(vcpu))) {
Sean Christopherson55d23752018-12-03 13:53:18 -08002545 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christophersonc80add02019-04-11 12:18:09 -07002546 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002547 }
2548
2549 /* Shadow page tables on either EPT or shadow page tables. */
2550 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Maxim Levitsky0f857222021-06-07 12:02:00 +03002551 from_vmentry, entry_failure_code))
Sean Christophersonc80add02019-04-11 12:18:09 -07002552 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002553
Sean Christopherson04f11ef2019-09-27 14:45:16 -07002554 /*
2555 * Immediately write vmcs02.GUEST_CR3. It will be propagated to vmcs12
2556 * on nested VM-Exit, which can occur without actually running L2 and
Paolo Bonzini727a7e22020-03-05 03:52:50 -05002557 * thus without hitting vmx_load_mmu_pgd(), e.g. if L1 is entering L2 with
Sean Christopherson04f11ef2019-09-27 14:45:16 -07002558 * vmcs12.GUEST_ACTIVITYSTATE=HLT, in which case KVM will intercept the
2559 * transition to HLT instead of running L2.
2560 */
2561 if (enable_ept)
2562 vmcs_writel(GUEST_CR3, vmcs12->guest_cr3);
2563
Sean Christophersonc7554efc2019-05-07 09:06:40 -07002564 /* Late preparation of GUEST_PDPTRs now that EFER and CRs are set. */
2565 if (load_guest_pdptrs_vmcs12 && nested_cpu_has_ept(vmcs12) &&
2566 is_pae_paging(vcpu)) {
2567 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
2568 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
2569 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
2570 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
2571 }
2572
Sean Christopherson55d23752018-12-03 13:53:18 -08002573 if (!enable_ept)
2574 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
2575
Oliver Upton71f73472019-11-13 16:17:19 -08002576 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) &&
Oliver Uptond1968422019-12-13 16:33:58 -08002577 WARN_ON_ONCE(kvm_set_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL,
2578 vmcs12->guest_ia32_perf_global_ctrl)))
Oliver Upton71f73472019-11-13 16:17:19 -08002579 return -EINVAL;
2580
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02002581 kvm_rsp_write(vcpu, vmcs12->guest_rsp);
2582 kvm_rip_write(vcpu, vmcs12->guest_rip);
Vitaly Kuznetsovdc313382021-05-26 15:20:24 +02002583
2584 /*
2585 * It was observed that genuine Hyper-V running in L1 doesn't reset
2586 * 'hv_clean_fields' by itself, it only sets the corresponding dirty
2587 * bits when it changes a field in eVMCS. Mark all fields as clean
2588 * here.
2589 */
2590 if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
2591 vmx->nested.hv_evmcs->hv_clean_fields |=
2592 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
2593
Sean Christopherson55d23752018-12-03 13:53:18 -08002594 return 0;
2595}
2596
2597static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
2598{
Sean Christopherson5497b952019-07-11 08:58:29 -07002599 if (CC(!nested_cpu_has_nmi_exiting(vmcs12) &&
2600 nested_cpu_has_virtual_nmis(vmcs12)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002601 return -EINVAL;
2602
Sean Christopherson5497b952019-07-11 08:58:29 -07002603 if (CC(!nested_cpu_has_virtual_nmis(vmcs12) &&
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08002604 nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002605 return -EINVAL;
2606
2607 return 0;
2608}
2609
Sean Christophersonac6389a2020-03-02 18:02:38 -08002610static bool nested_vmx_check_eptp(struct kvm_vcpu *vcpu, u64 new_eptp)
Sean Christopherson55d23752018-12-03 13:53:18 -08002611{
2612 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08002613
2614 /* Check for memory type validity */
Sean Christophersonac6389a2020-03-02 18:02:38 -08002615 switch (new_eptp & VMX_EPTP_MT_MASK) {
Sean Christopherson55d23752018-12-03 13:53:18 -08002616 case VMX_EPTP_MT_UC:
Sean Christopherson5497b952019-07-11 08:58:29 -07002617 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002618 return false;
2619 break;
2620 case VMX_EPTP_MT_WB:
Sean Christopherson5497b952019-07-11 08:58:29 -07002621 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002622 return false;
2623 break;
2624 default:
2625 return false;
2626 }
2627
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08002628 /* Page-walk levels validity. */
Sean Christophersonac6389a2020-03-02 18:02:38 -08002629 switch (new_eptp & VMX_EPTP_PWL_MASK) {
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08002630 case VMX_EPTP_PWL_5:
2631 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_PAGE_WALK_5_BIT)))
2632 return false;
2633 break;
2634 case VMX_EPTP_PWL_4:
2635 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_PAGE_WALK_4_BIT)))
2636 return false;
2637 break;
2638 default:
Sean Christopherson55d23752018-12-03 13:53:18 -08002639 return false;
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08002640 }
Sean Christopherson55d23752018-12-03 13:53:18 -08002641
2642 /* Reserved bits should not be set */
Sean Christopherson636e8b72021-02-03 16:01:10 -08002643 if (CC(kvm_vcpu_is_illegal_gpa(vcpu, new_eptp) || ((new_eptp >> 7) & 0x1f)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002644 return false;
2645
2646 /* AD, if set, should be supported */
Sean Christophersonac6389a2020-03-02 18:02:38 -08002647 if (new_eptp & VMX_EPTP_AD_ENABLE_BIT) {
Sean Christopherson5497b952019-07-11 08:58:29 -07002648 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002649 return false;
2650 }
2651
2652 return true;
2653}
2654
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002655/*
2656 * Checks related to VM-Execution Control Fields
2657 */
2658static int nested_check_vm_execution_controls(struct kvm_vcpu *vcpu,
2659 struct vmcs12 *vmcs12)
2660{
2661 struct vcpu_vmx *vmx = to_vmx(vcpu);
2662
Sean Christopherson5497b952019-07-11 08:58:29 -07002663 if (CC(!vmx_control_verify(vmcs12->pin_based_vm_exec_control,
2664 vmx->nested.msrs.pinbased_ctls_low,
2665 vmx->nested.msrs.pinbased_ctls_high)) ||
2666 CC(!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
2667 vmx->nested.msrs.procbased_ctls_low,
2668 vmx->nested.msrs.procbased_ctls_high)))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002669 return -EINVAL;
2670
2671 if (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
Sean Christopherson5497b952019-07-11 08:58:29 -07002672 CC(!vmx_control_verify(vmcs12->secondary_vm_exec_control,
2673 vmx->nested.msrs.secondary_ctls_low,
2674 vmx->nested.msrs.secondary_ctls_high)))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002675 return -EINVAL;
2676
Sean Christopherson5497b952019-07-11 08:58:29 -07002677 if (CC(vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu)) ||
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002678 nested_vmx_check_io_bitmap_controls(vcpu, vmcs12) ||
2679 nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12) ||
2680 nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12) ||
2681 nested_vmx_check_apic_access_controls(vcpu, vmcs12) ||
2682 nested_vmx_check_apicv_controls(vcpu, vmcs12) ||
2683 nested_vmx_check_nmi_controls(vmcs12) ||
2684 nested_vmx_check_pml_controls(vcpu, vmcs12) ||
2685 nested_vmx_check_unrestricted_guest_controls(vcpu, vmcs12) ||
2686 nested_vmx_check_mode_based_ept_exec_controls(vcpu, vmcs12) ||
2687 nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12) ||
Sean Christopherson5497b952019-07-11 08:58:29 -07002688 CC(nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002689 return -EINVAL;
2690
Sean Christophersonbc441212019-02-12 16:42:23 -08002691 if (!nested_cpu_has_preemption_timer(vmcs12) &&
2692 nested_cpu_has_save_preemption_timer(vmcs12))
2693 return -EINVAL;
2694
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002695 if (nested_cpu_has_ept(vmcs12) &&
Sean Christophersonac6389a2020-03-02 18:02:38 -08002696 CC(!nested_vmx_check_eptp(vcpu, vmcs12->ept_pointer)))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002697 return -EINVAL;
2698
2699 if (nested_cpu_has_vmfunc(vmcs12)) {
Sean Christopherson5497b952019-07-11 08:58:29 -07002700 if (CC(vmcs12->vm_function_control &
2701 ~vmx->nested.msrs.vmfunc_controls))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002702 return -EINVAL;
2703
2704 if (nested_cpu_has_eptp_switching(vmcs12)) {
Sean Christopherson5497b952019-07-11 08:58:29 -07002705 if (CC(!nested_cpu_has_ept(vmcs12)) ||
2706 CC(!page_address_valid(vcpu, vmcs12->eptp_list_address)))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002707 return -EINVAL;
2708 }
2709 }
2710
2711 return 0;
2712}
2713
Krish Sadhukhan61446ba2018-12-12 13:30:09 -05002714/*
2715 * Checks related to VM-Exit Control Fields
2716 */
2717static int nested_check_vm_exit_controls(struct kvm_vcpu *vcpu,
2718 struct vmcs12 *vmcs12)
2719{
2720 struct vcpu_vmx *vmx = to_vmx(vcpu);
2721
Sean Christopherson5497b952019-07-11 08:58:29 -07002722 if (CC(!vmx_control_verify(vmcs12->vm_exit_controls,
2723 vmx->nested.msrs.exit_ctls_low,
2724 vmx->nested.msrs.exit_ctls_high)) ||
2725 CC(nested_vmx_check_exit_msr_switch_controls(vcpu, vmcs12)))
Krish Sadhukhan61446ba2018-12-12 13:30:09 -05002726 return -EINVAL;
2727
2728 return 0;
2729}
2730
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002731/*
2732 * Checks related to VM-Entry Control Fields
2733 */
2734static int nested_check_vm_entry_controls(struct kvm_vcpu *vcpu,
2735 struct vmcs12 *vmcs12)
Sean Christopherson55d23752018-12-03 13:53:18 -08002736{
2737 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08002738
Sean Christopherson5497b952019-07-11 08:58:29 -07002739 if (CC(!vmx_control_verify(vmcs12->vm_entry_controls,
2740 vmx->nested.msrs.entry_ctls_low,
2741 vmx->nested.msrs.entry_ctls_high)))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002742 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002743
2744 /*
2745 * From the Intel SDM, volume 3:
2746 * Fields relevant to VM-entry event injection must be set properly.
2747 * These fields are the VM-entry interruption-information field, the
2748 * VM-entry exception error code, and the VM-entry instruction length.
2749 */
2750 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
2751 u32 intr_info = vmcs12->vm_entry_intr_info_field;
2752 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
2753 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
2754 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
2755 bool should_have_error_code;
2756 bool urg = nested_cpu_has2(vmcs12,
2757 SECONDARY_EXEC_UNRESTRICTED_GUEST);
2758 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
2759
2760 /* VM-entry interruption-info field: interruption type */
Sean Christopherson5497b952019-07-11 08:58:29 -07002761 if (CC(intr_type == INTR_TYPE_RESERVED) ||
2762 CC(intr_type == INTR_TYPE_OTHER_EVENT &&
2763 !nested_cpu_supports_monitor_trap_flag(vcpu)))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002764 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002765
2766 /* VM-entry interruption-info field: vector */
Sean Christopherson5497b952019-07-11 08:58:29 -07002767 if (CC(intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
2768 CC(intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
2769 CC(intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002770 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002771
2772 /* VM-entry interruption-info field: deliver error code */
2773 should_have_error_code =
2774 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
2775 x86_exception_has_error_code(vector);
Sean Christopherson5497b952019-07-11 08:58:29 -07002776 if (CC(has_error_code != should_have_error_code))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002777 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002778
2779 /* VM-entry exception error code */
Sean Christopherson5497b952019-07-11 08:58:29 -07002780 if (CC(has_error_code &&
Sean Christopherson567926c2019-10-01 09:21:23 -07002781 vmcs12->vm_entry_exception_error_code & GENMASK(31, 16)))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002782 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002783
2784 /* VM-entry interruption-info field: reserved bits */
Sean Christopherson5497b952019-07-11 08:58:29 -07002785 if (CC(intr_info & INTR_INFO_RESVD_BITS_MASK))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002786 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002787
2788 /* VM-entry instruction length */
2789 switch (intr_type) {
2790 case INTR_TYPE_SOFT_EXCEPTION:
2791 case INTR_TYPE_SOFT_INTR:
2792 case INTR_TYPE_PRIV_SW_EXCEPTION:
Sean Christopherson5497b952019-07-11 08:58:29 -07002793 if (CC(vmcs12->vm_entry_instruction_len > 15) ||
2794 CC(vmcs12->vm_entry_instruction_len == 0 &&
2795 CC(!nested_cpu_has_zero_length_injection(vcpu))))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002796 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002797 }
2798 }
2799
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002800 if (nested_vmx_check_entry_msr_switch_controls(vcpu, vmcs12))
2801 return -EINVAL;
2802
2803 return 0;
2804}
2805
Sean Christopherson5478ba32019-04-11 12:18:06 -07002806static int nested_vmx_check_controls(struct kvm_vcpu *vcpu,
2807 struct vmcs12 *vmcs12)
2808{
2809 if (nested_check_vm_execution_controls(vcpu, vmcs12) ||
2810 nested_check_vm_exit_controls(vcpu, vmcs12) ||
2811 nested_check_vm_entry_controls(vcpu, vmcs12))
Paolo Bonzini98d9e852019-04-12 10:19:57 +02002812 return -EINVAL;
Sean Christopherson5478ba32019-04-11 12:18:06 -07002813
Vitaly Kuznetsova8350232020-02-05 13:30:34 +01002814 if (to_vmx(vcpu)->nested.enlightened_vmcs_enabled)
2815 return nested_evmcs_check_controls(vmcs12);
2816
Sean Christopherson5478ba32019-04-11 12:18:06 -07002817 return 0;
2818}
2819
Paolo Bonzini98d9e852019-04-12 10:19:57 +02002820static int nested_vmx_check_host_state(struct kvm_vcpu *vcpu,
2821 struct vmcs12 *vmcs12)
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002822{
2823 bool ia32e;
2824
Sean Christopherson5497b952019-07-11 08:58:29 -07002825 if (CC(!nested_host_cr0_valid(vcpu, vmcs12->host_cr0)) ||
2826 CC(!nested_host_cr4_valid(vcpu, vmcs12->host_cr4)) ||
Sean Christopherson636e8b72021-02-03 16:01:10 -08002827 CC(kvm_vcpu_is_illegal_gpa(vcpu, vmcs12->host_cr3)))
Krish Sadhukhan254b2f32018-12-12 13:30:11 -05002828 return -EINVAL;
Krish Sadhukhan711eff32019-02-07 14:05:30 -05002829
Sean Christopherson5497b952019-07-11 08:58:29 -07002830 if (CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_esp, vcpu)) ||
2831 CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_eip, vcpu)))
Krish Sadhukhan711eff32019-02-07 14:05:30 -05002832 return -EINVAL;
2833
Krish Sadhukhanf6b0db1f2019-04-08 17:35:11 -04002834 if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) &&
Sean Christopherson5497b952019-07-11 08:58:29 -07002835 CC(!kvm_pat_valid(vmcs12->host_ia32_pat)))
Krish Sadhukhanf6b0db1f2019-04-08 17:35:11 -04002836 return -EINVAL;
2837
Oliver Uptonc547cb62019-11-13 16:17:17 -08002838 if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) &&
2839 CC(!kvm_valid_perf_global_ctrl(vcpu_to_pmu(vcpu),
2840 vmcs12->host_ia32_perf_global_ctrl)))
2841 return -EINVAL;
2842
Paolo Bonzinifd3edd42019-09-25 18:33:53 +02002843#ifdef CONFIG_X86_64
2844 ia32e = !!(vcpu->arch.efer & EFER_LMA);
2845#else
2846 ia32e = false;
2847#endif
2848
2849 if (ia32e) {
2850 if (CC(!(vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)) ||
2851 CC(!(vmcs12->host_cr4 & X86_CR4_PAE)))
2852 return -EINVAL;
2853 } else {
2854 if (CC(vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) ||
2855 CC(vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) ||
2856 CC(vmcs12->host_cr4 & X86_CR4_PCIDE) ||
2857 CC((vmcs12->host_rip) >> 32))
2858 return -EINVAL;
2859 }
Krish Sadhukhan1ef23e12019-07-03 19:54:35 -04002860
Sean Christopherson5497b952019-07-11 08:58:29 -07002861 if (CC(vmcs12->host_cs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2862 CC(vmcs12->host_ss_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2863 CC(vmcs12->host_ds_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2864 CC(vmcs12->host_es_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2865 CC(vmcs12->host_fs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2866 CC(vmcs12->host_gs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2867 CC(vmcs12->host_tr_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2868 CC(vmcs12->host_cs_selector == 0) ||
2869 CC(vmcs12->host_tr_selector == 0) ||
2870 CC(vmcs12->host_ss_selector == 0 && !ia32e))
Krish Sadhukhan1ef23e12019-07-03 19:54:35 -04002871 return -EINVAL;
2872
Sean Christopherson5497b952019-07-11 08:58:29 -07002873 if (CC(is_noncanonical_address(vmcs12->host_fs_base, vcpu)) ||
2874 CC(is_noncanonical_address(vmcs12->host_gs_base, vcpu)) ||
2875 CC(is_noncanonical_address(vmcs12->host_gdtr_base, vcpu)) ||
2876 CC(is_noncanonical_address(vmcs12->host_idtr_base, vcpu)) ||
Paolo Bonzinifd3edd42019-09-25 18:33:53 +02002877 CC(is_noncanonical_address(vmcs12->host_tr_base, vcpu)) ||
2878 CC(is_noncanonical_address(vmcs12->host_rip, vcpu)))
Krish Sadhukhan58450382019-08-09 12:26:19 -07002879 return -EINVAL;
Krish Sadhukhan1ef23e12019-07-03 19:54:35 -04002880
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002881 /*
2882 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
2883 * IA32_EFER MSR must be 0 in the field for that register. In addition,
2884 * the values of the LMA and LME bits in the field must each be that of
2885 * the host address-space size VM-exit control.
2886 */
2887 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
Sean Christopherson5497b952019-07-11 08:58:29 -07002888 if (CC(!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer)) ||
2889 CC(ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA)) ||
2890 CC(ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)))
Krish Sadhukhan254b2f32018-12-12 13:30:11 -05002891 return -EINVAL;
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002892 }
2893
Sean Christopherson55d23752018-12-03 13:53:18 -08002894 return 0;
2895}
2896
2897static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
2898 struct vmcs12 *vmcs12)
2899{
KarimAllah Ahmed88925302019-01-31 21:24:41 +01002900 int r = 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08002901 struct vmcs12 *shadow;
KarimAllah Ahmed88925302019-01-31 21:24:41 +01002902 struct kvm_host_map map;
Sean Christopherson55d23752018-12-03 13:53:18 -08002903
2904 if (vmcs12->vmcs_link_pointer == -1ull)
2905 return 0;
2906
Sean Christopherson5497b952019-07-11 08:58:29 -07002907 if (CC(!page_address_valid(vcpu, vmcs12->vmcs_link_pointer)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002908 return -EINVAL;
2909
Sean Christopherson5497b952019-07-11 08:58:29 -07002910 if (CC(kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->vmcs_link_pointer), &map)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002911 return -EINVAL;
2912
KarimAllah Ahmed88925302019-01-31 21:24:41 +01002913 shadow = map.hva;
2914
Sean Christopherson5497b952019-07-11 08:58:29 -07002915 if (CC(shadow->hdr.revision_id != VMCS12_REVISION) ||
2916 CC(shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002917 r = -EINVAL;
KarimAllah Ahmed88925302019-01-31 21:24:41 +01002918
2919 kvm_vcpu_unmap(vcpu, &map, false);
Sean Christopherson55d23752018-12-03 13:53:18 -08002920 return r;
2921}
2922
Sean Christopherson55d23752018-12-03 13:53:18 -08002923/*
2924 * Checks related to Guest Non-register State
2925 */
2926static int nested_check_guest_non_reg_state(struct vmcs12 *vmcs12)
2927{
Sean Christopherson5497b952019-07-11 08:58:29 -07002928 if (CC(vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
Yadong Qibf0cd882020-11-06 14:51:22 +08002929 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT &&
2930 vmcs12->guest_activity_state != GUEST_ACTIVITY_WAIT_SIPI))
Sean Christopherson55d23752018-12-03 13:53:18 -08002931 return -EINVAL;
2932
2933 return 0;
2934}
2935
Sean Christopherson5478ba32019-04-11 12:18:06 -07002936static int nested_vmx_check_guest_state(struct kvm_vcpu *vcpu,
2937 struct vmcs12 *vmcs12,
Sean Christopherson68cda402020-05-11 15:05:29 -07002938 enum vm_entry_failure_code *entry_failure_code)
Sean Christopherson55d23752018-12-03 13:53:18 -08002939{
2940 bool ia32e;
2941
Sean Christopherson68cda402020-05-11 15:05:29 -07002942 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson55d23752018-12-03 13:53:18 -08002943
Sean Christopherson5497b952019-07-11 08:58:29 -07002944 if (CC(!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0)) ||
2945 CC(!nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4)))
Sean Christophersonc80add02019-04-11 12:18:09 -07002946 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002947
Krish Sadhukhanb91991b2020-01-15 19:54:32 -05002948 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) &&
2949 CC(!kvm_dr7_valid(vmcs12->guest_dr7)))
2950 return -EINVAL;
2951
Krish Sadhukhande2bc2b2019-04-08 17:35:12 -04002952 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) &&
Sean Christopherson5497b952019-07-11 08:58:29 -07002953 CC(!kvm_pat_valid(vmcs12->guest_ia32_pat)))
Sean Christophersonc80add02019-04-11 12:18:09 -07002954 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002955
2956 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
Sean Christopherson68cda402020-05-11 15:05:29 -07002957 *entry_failure_code = ENTRY_FAIL_VMCS_LINK_PTR;
Sean Christophersonc80add02019-04-11 12:18:09 -07002958 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002959 }
2960
Oliver Uptonbfc6ad62019-11-13 16:17:16 -08002961 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) &&
2962 CC(!kvm_valid_perf_global_ctrl(vcpu_to_pmu(vcpu),
2963 vmcs12->guest_ia32_perf_global_ctrl)))
2964 return -EINVAL;
2965
Sean Christopherson55d23752018-12-03 13:53:18 -08002966 /*
2967 * If the load IA32_EFER VM-entry control is 1, the following checks
2968 * are performed on the field for the IA32_EFER MSR:
2969 * - Bits reserved in the IA32_EFER MSR must be 0.
2970 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
2971 * the IA-32e mode guest VM-exit control. It must also be identical
2972 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
2973 * CR0.PG) is 1.
2974 */
2975 if (to_vmx(vcpu)->nested.nested_run_pending &&
2976 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
2977 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
Sean Christopherson5497b952019-07-11 08:58:29 -07002978 if (CC(!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer)) ||
2979 CC(ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA)) ||
2980 CC(((vmcs12->guest_cr0 & X86_CR0_PG) &&
2981 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))))
Sean Christophersonc80add02019-04-11 12:18:09 -07002982 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002983 }
2984
2985 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
Sean Christopherson5497b952019-07-11 08:58:29 -07002986 (CC(is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu)) ||
2987 CC((vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD))))
Sean Christophersonc80add02019-04-11 12:18:09 -07002988 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002989
Sean Christopherson9c3e9222019-04-11 12:18:05 -07002990 if (nested_check_guest_non_reg_state(vmcs12))
Sean Christophersonc80add02019-04-11 12:18:09 -07002991 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002992
2993 return 0;
2994}
2995
Sean Christopherson453eafb2018-12-20 12:25:17 -08002996static int nested_vmx_check_vmentry_hw(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08002997{
2998 struct vcpu_vmx *vmx = to_vmx(vcpu);
2999 unsigned long cr3, cr4;
Sean Christophersonf1727b42019-01-25 07:40:58 -08003000 bool vm_fail;
Sean Christopherson55d23752018-12-03 13:53:18 -08003001
3002 if (!nested_early_check)
3003 return 0;
3004
3005 if (vmx->msr_autoload.host.nr)
3006 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
3007 if (vmx->msr_autoload.guest.nr)
3008 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
3009
3010 preempt_disable();
3011
3012 vmx_prepare_switch_to_guest(vcpu);
3013
3014 /*
3015 * Induce a consistency check VMExit by clearing bit 1 in GUEST_RFLAGS,
3016 * which is reserved to '1' by hardware. GUEST_RFLAGS is guaranteed to
Miaohe Lin49f933d2020-02-27 11:20:54 +08003017 * be written (by prepare_vmcs02()) before the "real" VMEnter, i.e.
Sean Christopherson55d23752018-12-03 13:53:18 -08003018 * there is no need to preserve other bits or save/restore the field.
3019 */
3020 vmcs_writel(GUEST_RFLAGS, 0);
3021
Sean Christopherson55d23752018-12-03 13:53:18 -08003022 cr3 = __get_current_cr3_fast();
3023 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
3024 vmcs_writel(HOST_CR3, cr3);
3025 vmx->loaded_vmcs->host_state.cr3 = cr3;
3026 }
3027
3028 cr4 = cr4_read_shadow();
3029 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
3030 vmcs_writel(HOST_CR4, cr4);
3031 vmx->loaded_vmcs->host_state.cr4 = cr4;
3032 }
3033
Uros Bizjak150f17b2020-12-30 16:26:57 -08003034 vm_fail = __vmx_vcpu_run(vmx, (unsigned long *)&vcpu->arch.regs,
3035 vmx->loaded_vmcs->launched);
Sean Christopherson55d23752018-12-03 13:53:18 -08003036
Sean Christopherson55d23752018-12-03 13:53:18 -08003037 if (vmx->msr_autoload.host.nr)
3038 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
3039 if (vmx->msr_autoload.guest.nr)
3040 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
3041
Sean Christophersonf1727b42019-01-25 07:40:58 -08003042 if (vm_fail) {
Sean Christopherson380e0052019-07-11 08:58:30 -07003043 u32 error = vmcs_read32(VM_INSTRUCTION_ERROR);
3044
Wanpeng Li541e8862019-05-17 16:49:50 +08003045 preempt_enable();
Sean Christopherson380e0052019-07-11 08:58:30 -07003046
3047 trace_kvm_nested_vmenter_failed(
3048 "early hardware check VM-instruction error: ", error);
3049 WARN_ON_ONCE(error != VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Sean Christopherson55d23752018-12-03 13:53:18 -08003050 return 1;
3051 }
3052
3053 /*
3054 * VMExit clears RFLAGS.IF and DR7, even on a consistency check.
3055 */
Sean Christopherson55d23752018-12-03 13:53:18 -08003056 if (hw_breakpoint_active())
3057 set_debugreg(__this_cpu_read(cpu_dr7), 7);
Peter Zijlstra84b6a342020-05-29 23:27:36 +02003058 local_irq_enable();
Wanpeng Li541e8862019-05-17 16:49:50 +08003059 preempt_enable();
Sean Christopherson55d23752018-12-03 13:53:18 -08003060
3061 /*
3062 * A non-failing VMEntry means we somehow entered guest mode with
3063 * an illegal RIP, and that's just the tip of the iceberg. There
3064 * is no telling what memory has been modified or what state has
3065 * been exposed to unknown code. Hitting this all but guarantees
3066 * a (very critical) hardware issue.
3067 */
3068 WARN_ON(!(vmcs_read32(VM_EXIT_REASON) &
3069 VMX_EXIT_REASONS_FAILED_VMENTRY));
3070
3071 return 0;
3072}
Sean Christopherson55d23752018-12-03 13:53:18 -08003073
Paolo Bonzini9a78e152021-01-08 11:43:08 -05003074static bool nested_get_evmcs_page(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08003075{
Sean Christopherson55d23752018-12-03 13:53:18 -08003076 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08003077
Vitaly Kuznetsove942dbf2020-03-09 16:52:12 +01003078 /*
3079 * hv_evmcs may end up being not mapped after migration (when
3080 * L2 was running), map it here to make sure vmcs12 changes are
3081 * properly reflected.
3082 */
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02003083 if (vmx->nested.enlightened_vmcs_enabled &&
Vitaly Kuznetsov27849962021-05-26 15:20:20 +02003084 vmx->nested.hv_evmcs_vmptr == EVMPTR_MAP_PENDING) {
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003085 enum nested_evmptrld_status evmptrld_status =
3086 nested_vmx_handle_enlightened_vmptrld(vcpu, false);
3087
3088 if (evmptrld_status == EVMPTRLD_VMFAIL ||
Vitaly Kuznetsovf5c7e842021-05-03 17:08:51 +02003089 evmptrld_status == EVMPTRLD_ERROR)
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003090 return false;
Vitaly Kuznetsov8629b622021-05-26 15:20:25 +02003091
3092 /*
3093 * Post migration VMCS12 always provides the most actual
3094 * information, copy it to eVMCS upon entry.
3095 */
3096 vmx->nested.need_vmcs12_to_shadow_sync = true;
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003097 }
Vitaly Kuznetsove942dbf2020-03-09 16:52:12 +01003098
Paolo Bonzini9a78e152021-01-08 11:43:08 -05003099 return true;
3100}
3101
3102static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
3103{
3104 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3105 struct vcpu_vmx *vmx = to_vmx(vcpu);
3106 struct kvm_host_map *map;
3107 struct page *page;
3108 u64 hpa;
3109
Maxim Levitsky158a48e2021-06-07 12:02:03 +03003110 if (!vcpu->arch.pdptrs_from_userspace &&
3111 !nested_cpu_has_ept(vmcs12) && is_pae_paging(vcpu)) {
Maxim Levitsky0f857222021-06-07 12:02:00 +03003112 /*
3113 * Reload the guest's PDPTRs since after a migration
3114 * the guest CR3 might be restored prior to setting the nested
3115 * state which can lead to a load of wrong PDPTRs.
3116 */
3117 if (CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, vcpu->arch.cr3)))
3118 return false;
3119 }
3120
3121
Sean Christopherson55d23752018-12-03 13:53:18 -08003122 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
3123 /*
3124 * Translate L1 physical address to host physical
3125 * address for vmcs02. Keep the page pinned, so this
3126 * physical address remains valid. We keep a reference
3127 * to it so we can release it later.
3128 */
3129 if (vmx->nested.apic_access_page) { /* shouldn't happen */
Liran Alonb11494b2019-11-21 00:31:47 +02003130 kvm_release_page_clean(vmx->nested.apic_access_page);
Sean Christopherson55d23752018-12-03 13:53:18 -08003131 vmx->nested.apic_access_page = NULL;
3132 }
3133 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Sean Christopherson55d23752018-12-03 13:53:18 -08003134 if (!is_error_page(page)) {
3135 vmx->nested.apic_access_page = page;
3136 hpa = page_to_phys(vmx->nested.apic_access_page);
3137 vmcs_write64(APIC_ACCESS_ADDR, hpa);
3138 } else {
Jim Mattson671ddc72019-10-15 10:44:05 -07003139 pr_debug_ratelimited("%s: no backing 'struct page' for APIC-access address in vmcs12\n",
3140 __func__);
3141 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3142 vcpu->run->internal.suberror =
3143 KVM_INTERNAL_ERROR_EMULATION;
3144 vcpu->run->internal.ndata = 0;
3145 return false;
Sean Christopherson55d23752018-12-03 13:53:18 -08003146 }
3147 }
3148
3149 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003150 map = &vmx->nested.virtual_apic_map;
Sean Christopherson55d23752018-12-03 13:53:18 -08003151
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003152 if (!kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->virtual_apic_page_addr), map)) {
3153 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, pfn_to_hpa(map->pfn));
Paolo Bonzini69090812019-04-15 15:16:17 +02003154 } else if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING) &&
3155 nested_cpu_has(vmcs12, CPU_BASED_CR8_STORE_EXITING) &&
3156 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
3157 /*
3158 * The processor will never use the TPR shadow, simply
3159 * clear the bit from the execution control. Such a
3160 * configuration is useless, but it happens in tests.
3161 * For any other configuration, failing the vm entry is
3162 * _not_ what the processor does but it's basically the
3163 * only possibility we have.
3164 */
Sean Christopherson2183f562019-05-07 12:17:56 -07003165 exec_controls_clearbit(vmx, CPU_BASED_TPR_SHADOW);
Paolo Bonzini69090812019-04-15 15:16:17 +02003166 } else {
Sean Christophersonca2f5462019-05-07 09:06:33 -07003167 /*
3168 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR to
3169 * force VM-Entry to fail.
3170 */
3171 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Sean Christopherson55d23752018-12-03 13:53:18 -08003172 }
3173 }
3174
3175 if (nested_cpu_has_posted_intr(vmcs12)) {
KarimAllah Ahmed3278e042019-01-31 21:24:38 +01003176 map = &vmx->nested.pi_desc_map;
3177
3178 if (!kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->posted_intr_desc_addr), map)) {
3179 vmx->nested.pi_desc =
3180 (struct pi_desc *)(((void *)map->hva) +
3181 offset_in_page(vmcs12->posted_intr_desc_addr));
3182 vmcs_write64(POSTED_INTR_DESC_ADDR,
3183 pfn_to_hpa(map->pfn) + offset_in_page(vmcs12->posted_intr_desc_addr));
Jim Mattson966eefb2021-06-04 10:26:06 -07003184 } else {
3185 /*
3186 * Defer the KVM_INTERNAL_EXIT until KVM tries to
3187 * access the contents of the VMCS12 posted interrupt
3188 * descriptor. (Note that KVM may do this when it
3189 * should not, per the architectural specification.)
3190 */
3191 vmx->nested.pi_desc = NULL;
3192 pin_controls_clearbit(vmx, PIN_BASED_POSTED_INTR);
Sean Christopherson55d23752018-12-03 13:53:18 -08003193 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003194 }
3195 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
Sean Christopherson2183f562019-05-07 12:17:56 -07003196 exec_controls_setbit(vmx, CPU_BASED_USE_MSR_BITMAPS);
Sean Christopherson55d23752018-12-03 13:53:18 -08003197 else
Sean Christopherson2183f562019-05-07 12:17:56 -07003198 exec_controls_clearbit(vmx, CPU_BASED_USE_MSR_BITMAPS);
Paolo Bonzini9a78e152021-01-08 11:43:08 -05003199
3200 return true;
3201}
3202
3203static bool vmx_get_nested_state_pages(struct kvm_vcpu *vcpu)
3204{
Vitaly Kuznetsovf5c7e842021-05-03 17:08:51 +02003205 if (!nested_get_evmcs_page(vcpu)) {
3206 pr_debug_ratelimited("%s: enlightened vmptrld failed\n",
3207 __func__);
3208 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3209 vcpu->run->internal.suberror =
3210 KVM_INTERNAL_ERROR_EMULATION;
3211 vcpu->run->internal.ndata = 0;
3212
Paolo Bonzini9a78e152021-01-08 11:43:08 -05003213 return false;
Vitaly Kuznetsovf5c7e842021-05-03 17:08:51 +02003214 }
Paolo Bonzini9a78e152021-01-08 11:43:08 -05003215
3216 if (is_guest_mode(vcpu) && !nested_get_vmcs12_pages(vcpu))
3217 return false;
3218
Jim Mattson671ddc72019-10-15 10:44:05 -07003219 return true;
Sean Christopherson55d23752018-12-03 13:53:18 -08003220}
3221
Sean Christopherson02f5fb22020-06-22 14:58:32 -07003222static int nested_vmx_write_pml_buffer(struct kvm_vcpu *vcpu, gpa_t gpa)
3223{
3224 struct vmcs12 *vmcs12;
3225 struct vcpu_vmx *vmx = to_vmx(vcpu);
3226 gpa_t dst;
3227
3228 if (WARN_ON_ONCE(!is_guest_mode(vcpu)))
3229 return 0;
3230
3231 if (WARN_ON_ONCE(vmx->nested.pml_full))
3232 return 1;
3233
3234 /*
3235 * Check if PML is enabled for the nested guest. Whether eptp bit 6 is
3236 * set is already checked as part of A/D emulation.
3237 */
3238 vmcs12 = get_vmcs12(vcpu);
3239 if (!nested_cpu_has_pml(vmcs12))
3240 return 0;
3241
3242 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
3243 vmx->nested.pml_full = true;
3244 return 1;
3245 }
3246
3247 gpa &= ~0xFFFull;
3248 dst = vmcs12->pml_address + sizeof(u64) * vmcs12->guest_pml_index;
3249
3250 if (kvm_write_guest_page(vcpu->kvm, gpa_to_gfn(dst), &gpa,
3251 offset_in_page(dst), sizeof(gpa)))
3252 return 0;
3253
3254 vmcs12->guest_pml_index--;
3255
3256 return 0;
3257}
3258
Sean Christopherson55d23752018-12-03 13:53:18 -08003259/*
3260 * Intel's VMX Instruction Reference specifies a common set of prerequisites
3261 * for running VMX instructions (except VMXON, whose prerequisites are
3262 * slightly different). It also specifies what exception to inject otherwise.
3263 * Note that many of these exceptions have priority over VM exits, so they
3264 * don't have to be checked again here.
3265 */
3266static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
3267{
3268 if (!to_vmx(vcpu)->nested.vmxon) {
3269 kvm_queue_exception(vcpu, UD_VECTOR);
3270 return 0;
3271 }
3272
3273 if (vmx_get_cpl(vcpu)) {
3274 kvm_inject_gp(vcpu, 0);
3275 return 0;
3276 }
3277
3278 return 1;
3279}
3280
3281static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu)
3282{
3283 u8 rvi = vmx_get_rvi();
3284 u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI);
3285
3286 return ((rvi & 0xf0) > (vppr & 0xf0));
3287}
3288
3289static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
3290 struct vmcs12 *vmcs12);
3291
3292/*
3293 * If from_vmentry is false, this is being called from state restore (either RSM
3294 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
Jim Mattson671ddc72019-10-15 10:44:05 -07003295 *
3296 * Returns:
Miaohe Lin463bfee2020-02-14 10:44:05 +08003297 * NVMX_VMENTRY_SUCCESS: Entered VMX non-root mode
3298 * NVMX_VMENTRY_VMFAIL: Consistency check VMFail
3299 * NVMX_VMENTRY_VMEXIT: Consistency check VMExit
3300 * NVMX_VMENTRY_KVM_INTERNAL_ERROR: KVM internal error
Sean Christopherson55d23752018-12-03 13:53:18 -08003301 */
Jim Mattson671ddc72019-10-15 10:44:05 -07003302enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu,
3303 bool from_vmentry)
Sean Christopherson55d23752018-12-03 13:53:18 -08003304{
3305 struct vcpu_vmx *vmx = to_vmx(vcpu);
3306 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Sean Christopherson68cda402020-05-11 15:05:29 -07003307 enum vm_entry_failure_code entry_failure_code;
Sean Christopherson55d23752018-12-03 13:53:18 -08003308 bool evaluate_pending_interrupts;
Sean Christopherson8e533242020-11-06 17:03:12 +08003309 union vmx_exit_reason exit_reason = {
3310 .basic = EXIT_REASON_INVALID_STATE,
3311 .failed_vmentry = 1,
3312 };
3313 u32 failed_index;
Sean Christopherson55d23752018-12-03 13:53:18 -08003314
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07003315 if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu))
3316 kvm_vcpu_flush_tlb_current(vcpu);
3317
Sean Christopherson2183f562019-05-07 12:17:56 -07003318 evaluate_pending_interrupts = exec_controls_get(vmx) &
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08003319 (CPU_BASED_INTR_WINDOW_EXITING | CPU_BASED_NMI_WINDOW_EXITING);
Sean Christopherson55d23752018-12-03 13:53:18 -08003320 if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu))
3321 evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu);
3322
3323 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
3324 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
3325 if (kvm_mpx_supported() &&
3326 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
3327 vmx->nested.vmcs01_guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
3328
Sean Christophersonf087a022019-06-07 11:55:34 -07003329 /*
3330 * Overwrite vmcs01.GUEST_CR3 with L1's CR3 if EPT is disabled *and*
3331 * nested early checks are disabled. In the event of a "late" VM-Fail,
3332 * i.e. a VM-Fail detected by hardware but not KVM, KVM must unwind its
3333 * software model to the pre-VMEntry host state. When EPT is disabled,
3334 * GUEST_CR3 holds KVM's shadow CR3, not L1's "real" CR3, which causes
3335 * nested_vmx_restore_host_state() to corrupt vcpu->arch.cr3. Stuffing
3336 * vmcs01.GUEST_CR3 results in the unwind naturally setting arch.cr3 to
3337 * the correct value. Smashing vmcs01.GUEST_CR3 is safe because nested
3338 * VM-Exits, and the unwind, reset KVM's MMU, i.e. vmcs01.GUEST_CR3 is
3339 * guaranteed to be overwritten with a shadow CR3 prior to re-entering
3340 * L1. Don't stuff vmcs01.GUEST_CR3 when using nested early checks as
3341 * KVM modifies vcpu->arch.cr3 if and only if the early hardware checks
3342 * pass, and early VM-Fails do not reset KVM's MMU, i.e. the VM-Fail
3343 * path would need to manually save/restore vmcs01.GUEST_CR3.
3344 */
3345 if (!enable_ept && !nested_early_check)
3346 vmcs_writel(GUEST_CR3, vcpu->arch.cr3);
3347
Sean Christopherson55d23752018-12-03 13:53:18 -08003348 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
3349
3350 prepare_vmcs02_early(vmx, vmcs12);
3351
3352 if (from_vmentry) {
Sean Christophersonb89d5ad2020-09-23 11:44:47 -07003353 if (unlikely(!nested_get_vmcs12_pages(vcpu))) {
3354 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson671ddc72019-10-15 10:44:05 -07003355 return NVMX_VMENTRY_KVM_INTERNAL_ERROR;
Sean Christophersonb89d5ad2020-09-23 11:44:47 -07003356 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003357
3358 if (nested_vmx_check_vmentry_hw(vcpu)) {
3359 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson671ddc72019-10-15 10:44:05 -07003360 return NVMX_VMENTRY_VMFAIL;
Sean Christopherson55d23752018-12-03 13:53:18 -08003361 }
3362
Sean Christopherson68cda402020-05-11 15:05:29 -07003363 if (nested_vmx_check_guest_state(vcpu, vmcs12,
3364 &entry_failure_code)) {
Sean Christopherson8e533242020-11-06 17:03:12 +08003365 exit_reason.basic = EXIT_REASON_INVALID_STATE;
Sean Christopherson68cda402020-05-11 15:05:29 -07003366 vmcs12->exit_qualification = entry_failure_code;
Sean Christopherson55d23752018-12-03 13:53:18 -08003367 goto vmentry_fail_vmexit;
Sean Christopherson68cda402020-05-11 15:05:29 -07003368 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003369 }
3370
3371 enter_guest_mode(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08003372
Maxim Levitsky0f857222021-06-07 12:02:00 +03003373 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &entry_failure_code)) {
Sean Christopherson8e533242020-11-06 17:03:12 +08003374 exit_reason.basic = EXIT_REASON_INVALID_STATE;
Sean Christopherson68cda402020-05-11 15:05:29 -07003375 vmcs12->exit_qualification = entry_failure_code;
Sean Christopherson55d23752018-12-03 13:53:18 -08003376 goto vmentry_fail_vmexit_guest_mode;
Sean Christopherson68cda402020-05-11 15:05:29 -07003377 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003378
3379 if (from_vmentry) {
Sean Christopherson68cda402020-05-11 15:05:29 -07003380 failed_index = nested_vmx_load_msr(vcpu,
3381 vmcs12->vm_entry_msr_load_addr,
3382 vmcs12->vm_entry_msr_load_count);
3383 if (failed_index) {
Sean Christopherson8e533242020-11-06 17:03:12 +08003384 exit_reason.basic = EXIT_REASON_MSR_LOAD_FAIL;
Sean Christopherson68cda402020-05-11 15:05:29 -07003385 vmcs12->exit_qualification = failed_index;
Sean Christopherson55d23752018-12-03 13:53:18 -08003386 goto vmentry_fail_vmexit_guest_mode;
Sean Christopherson68cda402020-05-11 15:05:29 -07003387 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003388 } else {
3389 /*
3390 * The MMU is not initialized to point at the right entities yet and
3391 * "get pages" would need to read data from the guest (i.e. we will
3392 * need to perform gpa to hpa translation). Request a call
3393 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
3394 * have already been set at vmentry time and should not be reset.
3395 */
Paolo Bonzini729c15c2020-09-22 06:53:57 -04003396 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08003397 }
3398
3399 /*
3400 * If L1 had a pending IRQ/NMI until it executed
3401 * VMLAUNCH/VMRESUME which wasn't delivered because it was
3402 * disallowed (e.g. interrupts disabled), L0 needs to
3403 * evaluate if this pending event should cause an exit from L2
3404 * to L1 or delivered directly to L2 (e.g. In case L1 don't
3405 * intercept EXTERNAL_INTERRUPT).
3406 *
3407 * Usually this would be handled by the processor noticing an
3408 * IRQ/NMI window request, or checking RVI during evaluation of
3409 * pending virtual interrupts. However, this setting was done
3410 * on VMCS01 and now VMCS02 is active instead. Thus, we force L0
3411 * to perform pending event evaluation by requesting a KVM_REQ_EVENT.
3412 */
3413 if (unlikely(evaluate_pending_interrupts))
3414 kvm_make_request(KVM_REQ_EVENT, vcpu);
3415
3416 /*
Paolo Bonzini359a6c32019-01-29 19:14:46 +01003417 * Do not start the preemption timer hrtimer until after we know
3418 * we are successful, so that only nested_vmx_vmexit needs to cancel
3419 * the timer.
3420 */
3421 vmx->nested.preemption_timer_expired = false;
Peter Shier850448f2020-05-26 14:51:06 -07003422 if (nested_cpu_has_preemption_timer(vmcs12)) {
3423 u64 timer_value = vmx_calc_preemption_timer_value(vcpu);
3424 vmx_start_preemption_timer(vcpu, timer_value);
3425 }
Paolo Bonzini359a6c32019-01-29 19:14:46 +01003426
3427 /*
Sean Christopherson55d23752018-12-03 13:53:18 -08003428 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
3429 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
3430 * returned as far as L1 is concerned. It will only return (and set
3431 * the success flag) when L2 exits (see nested_vmx_vmexit()).
3432 */
Jim Mattson671ddc72019-10-15 10:44:05 -07003433 return NVMX_VMENTRY_SUCCESS;
Sean Christopherson55d23752018-12-03 13:53:18 -08003434
3435 /*
3436 * A failed consistency check that leads to a VMExit during L1's
3437 * VMEnter to L2 is a variation of a normal VMexit, as explained in
3438 * 26.7 "VM-entry failures during or after loading guest state".
3439 */
3440vmentry_fail_vmexit_guest_mode:
Xiaoyao Li5e3d3942019-12-06 16:45:26 +08003441 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING)
Sean Christopherson55d23752018-12-03 13:53:18 -08003442 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
3443 leave_guest_mode(vcpu);
3444
3445vmentry_fail_vmexit:
3446 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
3447
3448 if (!from_vmentry)
Jim Mattson671ddc72019-10-15 10:44:05 -07003449 return NVMX_VMENTRY_VMEXIT;
Sean Christopherson55d23752018-12-03 13:53:18 -08003450
3451 load_vmcs12_host_state(vcpu, vmcs12);
Sean Christopherson8e533242020-11-06 17:03:12 +08003452 vmcs12->vm_exit_reason = exit_reason.full;
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02003453 if (enable_shadow_vmcs || evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
Sean Christopherson3731905ef2019-05-07 08:36:27 -07003454 vmx->nested.need_vmcs12_to_shadow_sync = true;
Jim Mattson671ddc72019-10-15 10:44:05 -07003455 return NVMX_VMENTRY_VMEXIT;
Sean Christopherson55d23752018-12-03 13:53:18 -08003456}
3457
3458/*
3459 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
3460 * for running an L2 nested guest.
3461 */
3462static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
3463{
3464 struct vmcs12 *vmcs12;
Jim Mattson671ddc72019-10-15 10:44:05 -07003465 enum nvmx_vmentry_status status;
Sean Christopherson55d23752018-12-03 13:53:18 -08003466 struct vcpu_vmx *vmx = to_vmx(vcpu);
3467 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003468 enum nested_evmptrld_status evmptrld_status;
Sean Christopherson55d23752018-12-03 13:53:18 -08003469
3470 if (!nested_vmx_check_permission(vcpu))
3471 return 1;
3472
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003473 evmptrld_status = nested_vmx_handle_enlightened_vmptrld(vcpu, launch);
3474 if (evmptrld_status == EVMPTRLD_ERROR) {
3475 kvm_queue_exception(vcpu, UD_VECTOR);
Sean Christopherson55d23752018-12-03 13:53:18 -08003476 return 1;
Sean Christophersonfc595f32020-08-12 11:06:15 -07003477 } else if (CC(evmptrld_status == EVMPTRLD_VMFAIL)) {
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003478 return nested_vmx_failInvalid(vcpu);
3479 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003480
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02003481 if (CC(!evmptr_is_valid(vmx->nested.hv_evmcs_vmptr) &&
3482 vmx->nested.current_vmptr == -1ull))
Sean Christopherson55d23752018-12-03 13:53:18 -08003483 return nested_vmx_failInvalid(vcpu);
3484
3485 vmcs12 = get_vmcs12(vcpu);
3486
3487 /*
3488 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
3489 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
3490 * rather than RFLAGS.ZF, and no error number is stored to the
3491 * VM-instruction error field.
3492 */
Sean Christophersonfc595f32020-08-12 11:06:15 -07003493 if (CC(vmcs12->hdr.shadow_vmcs))
Sean Christopherson55d23752018-12-03 13:53:18 -08003494 return nested_vmx_failInvalid(vcpu);
3495
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02003496 if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) {
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02003497 copy_enlightened_to_vmcs12(vmx, vmx->nested.hv_evmcs->hv_clean_fields);
Sean Christopherson55d23752018-12-03 13:53:18 -08003498 /* Enlightened VMCS doesn't have launch state */
3499 vmcs12->launch_state = !launch;
3500 } else if (enable_shadow_vmcs) {
3501 copy_shadow_to_vmcs12(vmx);
3502 }
3503
3504 /*
3505 * The nested entry process starts with enforcing various prerequisites
3506 * on vmcs12 as required by the Intel SDM, and act appropriately when
3507 * they fail: As the SDM explains, some conditions should cause the
3508 * instruction to fail, while others will cause the instruction to seem
3509 * to succeed, but return an EXIT_REASON_INVALID_STATE.
3510 * To speed up the normal (success) code path, we should avoid checking
3511 * for misconfigurations which will anyway be caught by the processor
3512 * when using the merged vmcs02.
3513 */
Sean Christophersonfc595f32020-08-12 11:06:15 -07003514 if (CC(interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS))
Sean Christophersonb2656e42020-06-08 18:56:07 -07003515 return nested_vmx_fail(vcpu, VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
Sean Christopherson55d23752018-12-03 13:53:18 -08003516
Sean Christophersonfc595f32020-08-12 11:06:15 -07003517 if (CC(vmcs12->launch_state == launch))
Sean Christophersonb2656e42020-06-08 18:56:07 -07003518 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08003519 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
3520 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
3521
Paolo Bonzini98d9e852019-04-12 10:19:57 +02003522 if (nested_vmx_check_controls(vcpu, vmcs12))
Sean Christophersonb2656e42020-06-08 18:56:07 -07003523 return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Sean Christopherson5478ba32019-04-11 12:18:06 -07003524
Paolo Bonzini98d9e852019-04-12 10:19:57 +02003525 if (nested_vmx_check_host_state(vcpu, vmcs12))
Sean Christophersonb2656e42020-06-08 18:56:07 -07003526 return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
Sean Christopherson55d23752018-12-03 13:53:18 -08003527
3528 /*
3529 * We're finally done with prerequisite checking, and can start with
3530 * the nested entry.
3531 */
3532 vmx->nested.nested_run_pending = 1;
Peter Shier850448f2020-05-26 14:51:06 -07003533 vmx->nested.has_preemption_timer_deadline = false;
Jim Mattson671ddc72019-10-15 10:44:05 -07003534 status = nested_vmx_enter_non_root_mode(vcpu, true);
3535 if (unlikely(status != NVMX_VMENTRY_SUCCESS))
3536 goto vmentry_failed;
Sean Christopherson55d23752018-12-03 13:53:18 -08003537
Sean Christopherson25bb2cf2020-08-12 10:51:29 -07003538 /* Emulate processing of posted interrupts on VM-Enter. */
3539 if (nested_cpu_has_posted_intr(vmcs12) &&
3540 kvm_apic_has_interrupt(vcpu) == vmx->nested.posted_intr_nv) {
3541 vmx->nested.pi_pending = true;
3542 kvm_make_request(KVM_REQ_EVENT, vcpu);
3543 kvm_apic_clear_irr(vcpu, vmx->nested.posted_intr_nv);
3544 }
3545
Sean Christopherson55d23752018-12-03 13:53:18 -08003546 /* Hide L1D cache contents from the nested guest. */
3547 vmx->vcpu.arch.l1tf_flush_l1d = true;
3548
3549 /*
3550 * Must happen outside of nested_vmx_enter_non_root_mode() as it will
3551 * also be used as part of restoring nVMX state for
3552 * snapshot restore (migration).
3553 *
3554 * In this flow, it is assumed that vmcs12 cache was
Ingo Molnar163b0992021-03-21 22:28:53 +01003555 * transferred as part of captured nVMX state and should
Sean Christopherson55d23752018-12-03 13:53:18 -08003556 * therefore not be read from guest memory (which may not
3557 * exist on destination host yet).
3558 */
3559 nested_cache_shadow_vmcs12(vcpu, vmcs12);
3560
Yadong Qibf0cd882020-11-06 14:51:22 +08003561 switch (vmcs12->guest_activity_state) {
3562 case GUEST_ACTIVITY_HLT:
3563 /*
3564 * If we're entering a halted L2 vcpu and the L2 vcpu won't be
3565 * awakened by event injection or by an NMI-window VM-exit or
3566 * by an interrupt-window VM-exit, halt the vcpu.
3567 */
3568 if (!(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) &&
3569 !nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING) &&
3570 !(nested_cpu_has(vmcs12, CPU_BASED_INTR_WINDOW_EXITING) &&
3571 (vmcs12->guest_rflags & X86_EFLAGS_IF))) {
3572 vmx->nested.nested_run_pending = 0;
3573 return kvm_vcpu_halt(vcpu);
3574 }
3575 break;
3576 case GUEST_ACTIVITY_WAIT_SIPI:
Sean Christopherson55d23752018-12-03 13:53:18 -08003577 vmx->nested.nested_run_pending = 0;
Yadong Qibf0cd882020-11-06 14:51:22 +08003578 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
3579 break;
3580 default:
3581 break;
Sean Christopherson55d23752018-12-03 13:53:18 -08003582 }
Yadong Qibf0cd882020-11-06 14:51:22 +08003583
Sean Christopherson55d23752018-12-03 13:53:18 -08003584 return 1;
Jim Mattson671ddc72019-10-15 10:44:05 -07003585
3586vmentry_failed:
3587 vmx->nested.nested_run_pending = 0;
3588 if (status == NVMX_VMENTRY_KVM_INTERNAL_ERROR)
3589 return 0;
3590 if (status == NVMX_VMENTRY_VMEXIT)
3591 return 1;
3592 WARN_ON_ONCE(status != NVMX_VMENTRY_VMFAIL);
Sean Christophersonb2656e42020-06-08 18:56:07 -07003593 return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Sean Christopherson55d23752018-12-03 13:53:18 -08003594}
3595
3596/*
3597 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
Miaohe Lin67b0ae42019-12-11 14:26:22 +08003598 * because L2 may have changed some cr0 bits directly (CR0_GUEST_HOST_MASK).
Sean Christopherson55d23752018-12-03 13:53:18 -08003599 * This function returns the new value we should put in vmcs12.guest_cr0.
3600 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
3601 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
3602 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
3603 * didn't trap the bit, because if L1 did, so would L0).
3604 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
3605 * been modified by L2, and L1 knows it. So just leave the old value of
3606 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
3607 * isn't relevant, because if L0 traps this bit it can set it to anything.
3608 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
3609 * changed these bits, and therefore they need to be updated, but L0
3610 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
3611 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
3612 */
3613static inline unsigned long
3614vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
3615{
3616 return
3617 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
3618 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
3619 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
3620 vcpu->arch.cr0_guest_owned_bits));
3621}
3622
3623static inline unsigned long
3624vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
3625{
3626 return
3627 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
3628 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
3629 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
3630 vcpu->arch.cr4_guest_owned_bits));
3631}
3632
3633static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
3634 struct vmcs12 *vmcs12)
3635{
3636 u32 idt_vectoring;
3637 unsigned int nr;
3638
3639 if (vcpu->arch.exception.injected) {
3640 nr = vcpu->arch.exception.nr;
3641 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
3642
3643 if (kvm_exception_is_soft(nr)) {
3644 vmcs12->vm_exit_instruction_len =
3645 vcpu->arch.event_exit_inst_len;
3646 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
3647 } else
3648 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
3649
3650 if (vcpu->arch.exception.has_error_code) {
3651 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
3652 vmcs12->idt_vectoring_error_code =
3653 vcpu->arch.exception.error_code;
3654 }
3655
3656 vmcs12->idt_vectoring_info_field = idt_vectoring;
3657 } else if (vcpu->arch.nmi_injected) {
3658 vmcs12->idt_vectoring_info_field =
3659 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
3660 } else if (vcpu->arch.interrupt.injected) {
3661 nr = vcpu->arch.interrupt.nr;
3662 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
3663
3664 if (vcpu->arch.interrupt.soft) {
3665 idt_vectoring |= INTR_TYPE_SOFT_INTR;
3666 vmcs12->vm_entry_instruction_len =
3667 vcpu->arch.event_exit_inst_len;
3668 } else
3669 idt_vectoring |= INTR_TYPE_EXT_INTR;
3670
3671 vmcs12->idt_vectoring_info_field = idt_vectoring;
3672 }
3673}
3674
3675
Paolo Bonzini96b100c2020-03-17 18:32:50 +01003676void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08003677{
3678 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3679 gfn_t gfn;
3680
3681 /*
3682 * Don't need to mark the APIC access page dirty; it is never
3683 * written to by the CPU during APIC virtualization.
3684 */
3685
3686 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
3687 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
3688 kvm_vcpu_mark_page_dirty(vcpu, gfn);
3689 }
3690
3691 if (nested_cpu_has_posted_intr(vmcs12)) {
3692 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
3693 kvm_vcpu_mark_page_dirty(vcpu, gfn);
3694 }
3695}
3696
Jim Mattson650293c2021-06-04 10:26:02 -07003697static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08003698{
3699 struct vcpu_vmx *vmx = to_vmx(vcpu);
3700 int max_irr;
3701 void *vapic_page;
3702 u16 status;
3703
Jim Mattson966eefb2021-06-04 10:26:06 -07003704 if (!vmx->nested.pi_pending)
Jim Mattson650293c2021-06-04 10:26:02 -07003705 return 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08003706
Jim Mattson966eefb2021-06-04 10:26:06 -07003707 if (!vmx->nested.pi_desc)
3708 goto mmio_needed;
3709
Sean Christopherson55d23752018-12-03 13:53:18 -08003710 vmx->nested.pi_pending = false;
Jim Mattson966eefb2021-06-04 10:26:06 -07003711
Sean Christopherson55d23752018-12-03 13:53:18 -08003712 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
Jim Mattson650293c2021-06-04 10:26:02 -07003713 return 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08003714
3715 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
3716 if (max_irr != 256) {
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003717 vapic_page = vmx->nested.virtual_apic_map.hva;
3718 if (!vapic_page)
Jim Mattson0fe998b2021-06-04 10:26:05 -07003719 goto mmio_needed;
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003720
Sean Christopherson55d23752018-12-03 13:53:18 -08003721 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
3722 vapic_page, &max_irr);
Sean Christopherson55d23752018-12-03 13:53:18 -08003723 status = vmcs_read16(GUEST_INTR_STATUS);
3724 if ((u8)max_irr > ((u8)status & 0xff)) {
3725 status &= ~0xff;
3726 status |= (u8)max_irr;
3727 vmcs_write16(GUEST_INTR_STATUS, status);
3728 }
3729 }
3730
3731 nested_mark_vmcs12_pages_dirty(vcpu);
Jim Mattson650293c2021-06-04 10:26:02 -07003732 return 0;
Jim Mattson0fe998b2021-06-04 10:26:05 -07003733
3734mmio_needed:
3735 kvm_handle_memory_failure(vcpu, X86EMUL_IO_NEEDED, NULL);
3736 return -ENXIO;
Sean Christopherson55d23752018-12-03 13:53:18 -08003737}
3738
3739static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3740 unsigned long exit_qual)
3741{
3742 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3743 unsigned int nr = vcpu->arch.exception.nr;
3744 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3745
3746 if (vcpu->arch.exception.has_error_code) {
3747 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3748 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3749 }
3750
3751 if (kvm_exception_is_soft(nr))
3752 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3753 else
3754 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3755
3756 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3757 vmx_get_nmi_mask(vcpu))
3758 intr_info |= INTR_INFO_UNBLOCK_NMI;
3759
3760 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3761}
3762
Oliver Upton684c0422020-02-07 02:36:05 -08003763/*
3764 * Returns true if a debug trap is pending delivery.
3765 *
3766 * In KVM, debug traps bear an exception payload. As such, the class of a #DB
3767 * exception may be inferred from the presence of an exception payload.
3768 */
3769static inline bool vmx_pending_dbg_trap(struct kvm_vcpu *vcpu)
3770{
3771 return vcpu->arch.exception.pending &&
3772 vcpu->arch.exception.nr == DB_VECTOR &&
3773 vcpu->arch.exception.payload;
3774}
3775
3776/*
3777 * Certain VM-exits set the 'pending debug exceptions' field to indicate a
3778 * recognized #DB (data or single-step) that has yet to be delivered. Since KVM
3779 * represents these debug traps with a payload that is said to be compatible
3780 * with the 'pending debug exceptions' field, write the payload to the VMCS
3781 * field if a VM-exit is delivered before the debug trap.
3782 */
3783static void nested_vmx_update_pending_dbg(struct kvm_vcpu *vcpu)
3784{
3785 if (vmx_pending_dbg_trap(vcpu))
3786 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
3787 vcpu->arch.exception.payload);
3788}
3789
Sean Christophersond2060bd2020-04-22 19:25:39 -07003790static bool nested_vmx_preemption_timer_pending(struct kvm_vcpu *vcpu)
3791{
3792 return nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
3793 to_vmx(vcpu)->nested.preemption_timer_expired;
3794}
3795
Sean Christophersona1c77ab2020-03-02 22:27:35 -08003796static int vmx_check_nested_events(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08003797{
3798 struct vcpu_vmx *vmx = to_vmx(vcpu);
3799 unsigned long exit_qual;
3800 bool block_nested_events =
3801 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003802 bool mtf_pending = vmx->nested.mtf_pending;
Liran Alon4b9852f2019-08-26 13:24:49 +03003803 struct kvm_lapic *apic = vcpu->arch.apic;
3804
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003805 /*
3806 * Clear the MTF state. If a higher priority VM-exit is delivered first,
3807 * this state is discarded.
3808 */
Oliver Upton5c8beb42020-04-06 20:12:37 +00003809 if (!block_nested_events)
3810 vmx->nested.mtf_pending = false;
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003811
Liran Alon4b9852f2019-08-26 13:24:49 +03003812 if (lapic_in_kernel(vcpu) &&
3813 test_bit(KVM_APIC_INIT, &apic->pending_events)) {
3814 if (block_nested_events)
3815 return -EBUSY;
Oliver Upton684c0422020-02-07 02:36:05 -08003816 nested_vmx_update_pending_dbg(vcpu);
Liran Alone64a8502019-11-11 14:16:05 +02003817 clear_bit(KVM_APIC_INIT, &apic->pending_events);
Yadong Qibf0cd882020-11-06 14:51:22 +08003818 if (vcpu->arch.mp_state != KVM_MP_STATE_INIT_RECEIVED)
3819 nested_vmx_vmexit(vcpu, EXIT_REASON_INIT_SIGNAL, 0, 0);
3820 return 0;
3821 }
3822
3823 if (lapic_in_kernel(vcpu) &&
3824 test_bit(KVM_APIC_SIPI, &apic->pending_events)) {
3825 if (block_nested_events)
3826 return -EBUSY;
3827
3828 clear_bit(KVM_APIC_SIPI, &apic->pending_events);
3829 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED)
3830 nested_vmx_vmexit(vcpu, EXIT_REASON_SIPI_SIGNAL, 0,
3831 apic->sipi_vector & 0xFFUL);
Liran Alon4b9852f2019-08-26 13:24:49 +03003832 return 0;
3833 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003834
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003835 /*
3836 * Process any exceptions that are not debug traps before MTF.
Maxim Levitsky4020da32021-04-01 17:38:14 +03003837 *
3838 * Note that only a pending nested run can block a pending exception.
3839 * Otherwise an injected NMI/interrupt should either be
3840 * lost or delivered to the nested hypervisor in the IDT_VECTORING_INFO,
3841 * while delivering the pending exception.
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003842 */
Maxim Levitsky4020da32021-04-01 17:38:14 +03003843
Sean Christopherson6ce347a2020-04-22 19:25:38 -07003844 if (vcpu->arch.exception.pending && !vmx_pending_dbg_trap(vcpu)) {
Maxim Levitsky4020da32021-04-01 17:38:14 +03003845 if (vmx->nested.nested_run_pending)
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003846 return -EBUSY;
Sean Christopherson6ce347a2020-04-22 19:25:38 -07003847 if (!nested_vmx_check_exception(vcpu, &exit_qual))
3848 goto no_vmexit;
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003849 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
3850 return 0;
3851 }
3852
3853 if (mtf_pending) {
3854 if (block_nested_events)
3855 return -EBUSY;
3856 nested_vmx_update_pending_dbg(vcpu);
3857 nested_vmx_vmexit(vcpu, EXIT_REASON_MONITOR_TRAP_FLAG, 0, 0);
3858 return 0;
3859 }
3860
Sean Christopherson6ce347a2020-04-22 19:25:38 -07003861 if (vcpu->arch.exception.pending) {
Maxim Levitsky4020da32021-04-01 17:38:14 +03003862 if (vmx->nested.nested_run_pending)
Sean Christopherson55d23752018-12-03 13:53:18 -08003863 return -EBUSY;
Sean Christopherson6ce347a2020-04-22 19:25:38 -07003864 if (!nested_vmx_check_exception(vcpu, &exit_qual))
3865 goto no_vmexit;
Sean Christopherson55d23752018-12-03 13:53:18 -08003866 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
3867 return 0;
3868 }
3869
Sean Christophersond2060bd2020-04-22 19:25:39 -07003870 if (nested_vmx_preemption_timer_pending(vcpu)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08003871 if (block_nested_events)
3872 return -EBUSY;
3873 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
3874 return 0;
3875 }
3876
Sean Christopherson1cd2f0b2020-04-22 19:25:46 -07003877 if (vcpu->arch.smi_pending && !is_smm(vcpu)) {
3878 if (block_nested_events)
3879 return -EBUSY;
3880 goto no_vmexit;
3881 }
3882
Sean Christopherson15ff0b42020-04-22 19:25:45 -07003883 if (vcpu->arch.nmi_pending && !vmx_nmi_blocked(vcpu)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08003884 if (block_nested_events)
3885 return -EBUSY;
Sean Christopherson15ff0b42020-04-22 19:25:45 -07003886 if (!nested_exit_on_nmi(vcpu))
3887 goto no_vmexit;
3888
Sean Christopherson55d23752018-12-03 13:53:18 -08003889 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
3890 NMI_VECTOR | INTR_TYPE_NMI_INTR |
3891 INTR_INFO_VALID_MASK, 0);
3892 /*
3893 * The NMI-triggered VM exit counts as injection:
3894 * clear this one and block further NMIs.
3895 */
3896 vcpu->arch.nmi_pending = 0;
3897 vmx_set_nmi_mask(vcpu, true);
3898 return 0;
3899 }
3900
Sean Christopherson15ff0b42020-04-22 19:25:45 -07003901 if (kvm_cpu_has_interrupt(vcpu) && !vmx_interrupt_blocked(vcpu)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08003902 if (block_nested_events)
3903 return -EBUSY;
Sean Christopherson15ff0b42020-04-22 19:25:45 -07003904 if (!nested_exit_on_intr(vcpu))
3905 goto no_vmexit;
Sean Christopherson55d23752018-12-03 13:53:18 -08003906 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
3907 return 0;
3908 }
3909
Sean Christopherson6ce347a2020-04-22 19:25:38 -07003910no_vmexit:
Jim Mattson650293c2021-06-04 10:26:02 -07003911 return vmx_complete_nested_posted_interrupt(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08003912}
3913
3914static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
3915{
3916 ktime_t remaining =
3917 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
3918 u64 value;
3919
3920 if (ktime_to_ns(remaining) <= 0)
3921 return 0;
3922
3923 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
3924 do_div(value, 1000000);
3925 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
3926}
3927
Sean Christopherson7952d762019-05-07 08:36:29 -07003928static bool is_vmcs12_ext_field(unsigned long field)
Sean Christopherson55d23752018-12-03 13:53:18 -08003929{
Sean Christopherson7952d762019-05-07 08:36:29 -07003930 switch (field) {
3931 case GUEST_ES_SELECTOR:
3932 case GUEST_CS_SELECTOR:
3933 case GUEST_SS_SELECTOR:
3934 case GUEST_DS_SELECTOR:
3935 case GUEST_FS_SELECTOR:
3936 case GUEST_GS_SELECTOR:
3937 case GUEST_LDTR_SELECTOR:
3938 case GUEST_TR_SELECTOR:
3939 case GUEST_ES_LIMIT:
3940 case GUEST_CS_LIMIT:
3941 case GUEST_SS_LIMIT:
3942 case GUEST_DS_LIMIT:
3943 case GUEST_FS_LIMIT:
3944 case GUEST_GS_LIMIT:
3945 case GUEST_LDTR_LIMIT:
3946 case GUEST_TR_LIMIT:
3947 case GUEST_GDTR_LIMIT:
3948 case GUEST_IDTR_LIMIT:
3949 case GUEST_ES_AR_BYTES:
3950 case GUEST_DS_AR_BYTES:
3951 case GUEST_FS_AR_BYTES:
3952 case GUEST_GS_AR_BYTES:
3953 case GUEST_LDTR_AR_BYTES:
3954 case GUEST_TR_AR_BYTES:
3955 case GUEST_ES_BASE:
3956 case GUEST_CS_BASE:
3957 case GUEST_SS_BASE:
3958 case GUEST_DS_BASE:
3959 case GUEST_FS_BASE:
3960 case GUEST_GS_BASE:
3961 case GUEST_LDTR_BASE:
3962 case GUEST_TR_BASE:
3963 case GUEST_GDTR_BASE:
3964 case GUEST_IDTR_BASE:
3965 case GUEST_PENDING_DBG_EXCEPTIONS:
3966 case GUEST_BNDCFGS:
3967 return true;
3968 default:
3969 break;
3970 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003971
Sean Christopherson7952d762019-05-07 08:36:29 -07003972 return false;
3973}
3974
3975static void sync_vmcs02_to_vmcs12_rare(struct kvm_vcpu *vcpu,
3976 struct vmcs12 *vmcs12)
3977{
3978 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08003979
3980 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
3981 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
3982 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
3983 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
3984 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
3985 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
3986 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
3987 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
3988 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
3989 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
3990 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
3991 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
3992 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
3993 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
3994 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
3995 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
3996 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
3997 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
3998 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
Sean Christopherson55d23752018-12-03 13:53:18 -08003999 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
4000 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
4001 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
4002 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
4003 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
4004 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
4005 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
4006 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
4007 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
4008 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
4009 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
4010 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
4011 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
4012 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
4013 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
Sean Christopherson7952d762019-05-07 08:36:29 -07004014 vmcs12->guest_pending_dbg_exceptions =
4015 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
4016 if (kvm_mpx_supported())
4017 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
4018
4019 vmx->nested.need_sync_vmcs02_to_vmcs12_rare = false;
4020}
4021
4022static void copy_vmcs02_to_vmcs12_rare(struct kvm_vcpu *vcpu,
4023 struct vmcs12 *vmcs12)
4024{
4025 struct vcpu_vmx *vmx = to_vmx(vcpu);
4026 int cpu;
4027
4028 if (!vmx->nested.need_sync_vmcs02_to_vmcs12_rare)
4029 return;
4030
4031
4032 WARN_ON_ONCE(vmx->loaded_vmcs != &vmx->vmcs01);
4033
4034 cpu = get_cpu();
4035 vmx->loaded_vmcs = &vmx->nested.vmcs02;
Sean Christopherson1af1bb02020-05-06 16:58:50 -07004036 vmx_vcpu_load_vmcs(vcpu, cpu, &vmx->vmcs01);
Sean Christopherson7952d762019-05-07 08:36:29 -07004037
4038 sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
4039
4040 vmx->loaded_vmcs = &vmx->vmcs01;
Sean Christopherson1af1bb02020-05-06 16:58:50 -07004041 vmx_vcpu_load_vmcs(vcpu, cpu, &vmx->nested.vmcs02);
Sean Christopherson7952d762019-05-07 08:36:29 -07004042 put_cpu();
4043}
4044
4045/*
4046 * Update the guest state fields of vmcs12 to reflect changes that
4047 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
4048 * VM-entry controls is also updated, since this is really a guest
4049 * state bit.)
4050 */
4051static void sync_vmcs02_to_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
4052{
4053 struct vcpu_vmx *vmx = to_vmx(vcpu);
4054
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02004055 if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
Sean Christopherson7952d762019-05-07 08:36:29 -07004056 sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
4057
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02004058 vmx->nested.need_sync_vmcs02_to_vmcs12_rare =
4059 !evmptr_is_valid(vmx->nested.hv_evmcs_vmptr);
Sean Christopherson7952d762019-05-07 08:36:29 -07004060
4061 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
4062 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
4063
4064 vmcs12->guest_rsp = kvm_rsp_read(vcpu);
4065 vmcs12->guest_rip = kvm_rip_read(vcpu);
4066 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
4067
4068 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
4069 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
Sean Christopherson55d23752018-12-03 13:53:18 -08004070
4071 vmcs12->guest_interruptibility_info =
4072 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
Sean Christopherson7952d762019-05-07 08:36:29 -07004073
Sean Christopherson55d23752018-12-03 13:53:18 -08004074 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
4075 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
Yadong Qibf0cd882020-11-06 14:51:22 +08004076 else if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED)
4077 vmcs12->guest_activity_state = GUEST_ACTIVITY_WAIT_SIPI;
Sean Christopherson55d23752018-12-03 13:53:18 -08004078 else
4079 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
4080
Paolo Bonzinib4b65b52019-01-29 19:12:35 +01004081 if (nested_cpu_has_preemption_timer(vmcs12) &&
Peter Shier850448f2020-05-26 14:51:06 -07004082 vmcs12->vm_exit_controls & VM_EXIT_SAVE_VMX_PREEMPTION_TIMER &&
4083 !vmx->nested.nested_run_pending)
4084 vmcs12->vmx_preemption_timer_value =
4085 vmx_get_preemption_timer_value(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08004086
4087 /*
4088 * In some cases (usually, nested EPT), L2 is allowed to change its
4089 * own CR3 without exiting. If it has changed it, we must keep it.
4090 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
4091 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
4092 *
4093 * Additionally, restore L2's PDPTR to vmcs12.
4094 */
4095 if (enable_ept) {
4096 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Sean Christophersonc7554efc2019-05-07 09:06:40 -07004097 if (nested_cpu_has_ept(vmcs12) && is_pae_paging(vcpu)) {
4098 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
4099 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
4100 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
4101 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
4102 }
Sean Christopherson55d23752018-12-03 13:53:18 -08004103 }
4104
4105 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
4106
4107 if (nested_cpu_has_vid(vmcs12))
4108 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
4109
4110 vmcs12->vm_entry_controls =
4111 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
4112 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
4113
Sean Christopherson699a1ac2019-05-07 09:06:37 -07004114 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS)
Sean Christopherson55d23752018-12-03 13:53:18 -08004115 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
Sean Christopherson55d23752018-12-03 13:53:18 -08004116
Sean Christopherson55d23752018-12-03 13:53:18 -08004117 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
4118 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Sean Christopherson55d23752018-12-03 13:53:18 -08004119}
4120
4121/*
4122 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
4123 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
4124 * and this function updates it to reflect the changes to the guest state while
4125 * L2 was running (and perhaps made some exits which were handled directly by L0
4126 * without going back to L1), and to reflect the exit reason.
4127 * Note that we do not have to copy here all VMCS fields, just those that
4128 * could have changed by the L2 guest or the exit - i.e., the guest-state and
4129 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
4130 * which already writes to vmcs12 directly.
4131 */
4132static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004133 u32 vm_exit_reason, u32 exit_intr_info,
Sean Christopherson55d23752018-12-03 13:53:18 -08004134 unsigned long exit_qualification)
4135{
Sean Christopherson55d23752018-12-03 13:53:18 -08004136 /* update exit information fields: */
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004137 vmcs12->vm_exit_reason = vm_exit_reason;
Sean Christopherson3c0c2ad2021-04-12 16:21:37 +12004138 if (to_vmx(vcpu)->exit_reason.enclave_mode)
4139 vmcs12->vm_exit_reason |= VMX_EXIT_REASONS_SGX_ENCLAVE_MODE;
Sean Christopherson55d23752018-12-03 13:53:18 -08004140 vmcs12->exit_qualification = exit_qualification;
4141 vmcs12->vm_exit_intr_info = exit_intr_info;
4142
4143 vmcs12->idt_vectoring_info_field = 0;
4144 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
4145 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
4146
4147 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
4148 vmcs12->launch_state = 1;
4149
4150 /* vm_entry_intr_info_field is cleared on exit. Emulate this
4151 * instead of reading the real value. */
4152 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
4153
4154 /*
4155 * Transfer the event that L0 or L1 may wanted to inject into
4156 * L2 to IDT_VECTORING_INFO_FIELD.
4157 */
4158 vmcs12_save_pending_event(vcpu, vmcs12);
Krish Sadhukhana0d4f802018-12-04 19:00:13 -05004159
4160 /*
4161 * According to spec, there's no need to store the guest's
4162 * MSRs if the exit is due to a VM-entry failure that occurs
4163 * during or after loading the guest state. Since this exit
4164 * does not fall in that category, we need to save the MSRs.
4165 */
4166 if (nested_vmx_store_msr(vcpu,
4167 vmcs12->vm_exit_msr_store_addr,
4168 vmcs12->vm_exit_msr_store_count))
4169 nested_vmx_abort(vcpu,
4170 VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Sean Christopherson55d23752018-12-03 13:53:18 -08004171 }
4172
4173 /*
4174 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
4175 * preserved above and would only end up incorrectly in L1.
4176 */
4177 vcpu->arch.nmi_injected = false;
4178 kvm_clear_exception_queue(vcpu);
4179 kvm_clear_interrupt_queue(vcpu);
4180}
4181
4182/*
4183 * A part of what we need to when the nested L2 guest exits and we want to
4184 * run its L1 parent, is to reset L1's guest state to the host state specified
4185 * in vmcs12.
4186 * This function is to be called not only on normal nested exit, but also on
4187 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
4188 * Failures During or After Loading Guest State").
4189 * This function should be called when the active VMCS is L1's (vmcs01).
4190 */
4191static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
4192 struct vmcs12 *vmcs12)
4193{
Sean Christopherson68cda402020-05-11 15:05:29 -07004194 enum vm_entry_failure_code ignored;
Sean Christopherson55d23752018-12-03 13:53:18 -08004195 struct kvm_segment seg;
Sean Christopherson55d23752018-12-03 13:53:18 -08004196
4197 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
4198 vcpu->arch.efer = vmcs12->host_ia32_efer;
4199 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
4200 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
4201 else
4202 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
4203 vmx_set_efer(vcpu, vcpu->arch.efer);
4204
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02004205 kvm_rsp_write(vcpu, vmcs12->host_rsp);
4206 kvm_rip_write(vcpu, vmcs12->host_rip);
Sean Christopherson55d23752018-12-03 13:53:18 -08004207 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
4208 vmx_set_interrupt_shadow(vcpu, 0);
4209
4210 /*
4211 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
4212 * actually changed, because vmx_set_cr0 refers to efer set above.
4213 *
4214 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
4215 * (KVM doesn't change it);
4216 */
Sean Christophersonfa71e952020-07-02 21:04:22 -07004217 vcpu->arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS;
Sean Christopherson55d23752018-12-03 13:53:18 -08004218 vmx_set_cr0(vcpu, vmcs12->host_cr0);
4219
4220 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
4221 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
4222 vmx_set_cr4(vcpu, vmcs12->host_cr4);
4223
4224 nested_ept_uninit_mmu_context(vcpu);
4225
4226 /*
4227 * Only PDPTE load can fail as the value of cr3 was checked on entry and
4228 * couldn't have changed.
4229 */
Maxim Levitsky0f857222021-06-07 12:02:00 +03004230 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, true, &ignored))
Sean Christopherson55d23752018-12-03 13:53:18 -08004231 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
4232
Sean Christopherson50b265a2020-03-20 14:28:19 -07004233 nested_vmx_transition_tlb_flush(vcpu, vmcs12, false);
Sean Christopherson55d23752018-12-03 13:53:18 -08004234
4235 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
4236 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
4237 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
4238 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
4239 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
4240 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
4241 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
4242
4243 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
4244 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
4245 vmcs_write64(GUEST_BNDCFGS, 0);
4246
4247 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
4248 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
4249 vcpu->arch.pat = vmcs12->host_ia32_pat;
4250 }
4251 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Oliver Uptond1968422019-12-13 16:33:58 -08004252 WARN_ON_ONCE(kvm_set_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL,
4253 vmcs12->host_ia32_perf_global_ctrl));
Sean Christopherson55d23752018-12-03 13:53:18 -08004254
4255 /* Set L1 segment info according to Intel SDM
4256 27.5.2 Loading Host Segment and Descriptor-Table Registers */
4257 seg = (struct kvm_segment) {
4258 .base = 0,
4259 .limit = 0xFFFFFFFF,
4260 .selector = vmcs12->host_cs_selector,
4261 .type = 11,
4262 .present = 1,
4263 .s = 1,
4264 .g = 1
4265 };
4266 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
4267 seg.l = 1;
4268 else
4269 seg.db = 1;
4270 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
4271 seg = (struct kvm_segment) {
4272 .base = 0,
4273 .limit = 0xFFFFFFFF,
4274 .type = 3,
4275 .present = 1,
4276 .s = 1,
4277 .db = 1,
4278 .g = 1
4279 };
4280 seg.selector = vmcs12->host_ds_selector;
4281 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
4282 seg.selector = vmcs12->host_es_selector;
4283 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
4284 seg.selector = vmcs12->host_ss_selector;
4285 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
4286 seg.selector = vmcs12->host_fs_selector;
4287 seg.base = vmcs12->host_fs_base;
4288 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
4289 seg.selector = vmcs12->host_gs_selector;
4290 seg.base = vmcs12->host_gs_base;
4291 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
4292 seg = (struct kvm_segment) {
4293 .base = vmcs12->host_tr_base,
4294 .limit = 0x67,
4295 .selector = vmcs12->host_tr_selector,
4296 .type = 11,
4297 .present = 1
4298 };
4299 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
4300
Sean Christophersonafc8de02021-07-13 09:32:40 -07004301 memset(&seg, 0, sizeof(seg));
4302 seg.unusable = 1;
4303 vmx_set_segment(vcpu, &seg, VCPU_SREG_LDTR);
4304
Sean Christopherson55d23752018-12-03 13:53:18 -08004305 kvm_set_dr(vcpu, 7, 0x400);
4306 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
4307
4308 if (cpu_has_vmx_msr_bitmap())
4309 vmx_update_msr_bitmap(vcpu);
4310
4311 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
4312 vmcs12->vm_exit_msr_load_count))
4313 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
4314}
4315
4316static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx)
4317{
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07004318 struct vmx_uret_msr *efer_msr;
Sean Christopherson55d23752018-12-03 13:53:18 -08004319 unsigned int i;
4320
4321 if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER)
4322 return vmcs_read64(GUEST_IA32_EFER);
4323
4324 if (cpu_has_load_ia32_efer())
4325 return host_efer;
4326
4327 for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) {
4328 if (vmx->msr_autoload.guest.val[i].index == MSR_EFER)
4329 return vmx->msr_autoload.guest.val[i].value;
4330 }
4331
Sean Christophersond85a8032020-09-23 11:04:06 -07004332 efer_msr = vmx_find_uret_msr(vmx, MSR_EFER);
Sean Christopherson55d23752018-12-03 13:53:18 -08004333 if (efer_msr)
4334 return efer_msr->data;
4335
4336 return host_efer;
4337}
4338
4339static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu)
4340{
4341 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4342 struct vcpu_vmx *vmx = to_vmx(vcpu);
4343 struct vmx_msr_entry g, h;
Sean Christopherson55d23752018-12-03 13:53:18 -08004344 gpa_t gpa;
4345 u32 i, j;
4346
4347 vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT);
4348
4349 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
4350 /*
4351 * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set
4352 * as vmcs01.GUEST_DR7 contains a userspace defined value
4353 * and vcpu->arch.dr7 is not squirreled away before the
4354 * nested VMENTER (not worth adding a variable in nested_vmx).
4355 */
4356 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
4357 kvm_set_dr(vcpu, 7, DR7_FIXED_1);
4358 else
4359 WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7)));
4360 }
4361
4362 /*
4363 * Note that calling vmx_set_{efer,cr0,cr4} is important as they
4364 * handle a variety of side effects to KVM's software model.
4365 */
4366 vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx));
4367
Sean Christophersonfa71e952020-07-02 21:04:22 -07004368 vcpu->arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS;
Sean Christopherson55d23752018-12-03 13:53:18 -08004369 vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW));
4370
4371 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
4372 vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW));
4373
4374 nested_ept_uninit_mmu_context(vcpu);
Sean Christophersonf087a022019-06-07 11:55:34 -07004375 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07004376 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Sean Christopherson55d23752018-12-03 13:53:18 -08004377
4378 /*
4379 * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs
4380 * from vmcs01 (if necessary). The PDPTRs are not loaded on
4381 * VMFail, like everything else we just need to ensure our
4382 * software model is up-to-date.
4383 */
Sean Christopherson9932b492020-04-15 13:34:50 -07004384 if (enable_ept && is_pae_paging(vcpu))
Sean Christophersonf087a022019-06-07 11:55:34 -07004385 ept_save_pdptrs(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08004386
4387 kvm_mmu_reset_context(vcpu);
4388
4389 if (cpu_has_vmx_msr_bitmap())
4390 vmx_update_msr_bitmap(vcpu);
4391
4392 /*
4393 * This nasty bit of open coding is a compromise between blindly
4394 * loading L1's MSRs using the exit load lists (incorrect emulation
4395 * of VMFail), leaving the nested VM's MSRs in the software model
4396 * (incorrect behavior) and snapshotting the modified MSRs (too
4397 * expensive since the lists are unbound by hardware). For each
4398 * MSR that was (prematurely) loaded from the nested VMEntry load
4399 * list, reload it from the exit load list if it exists and differs
4400 * from the guest value. The intent is to stuff host state as
4401 * silently as possible, not to fully process the exit load list.
4402 */
Sean Christopherson55d23752018-12-03 13:53:18 -08004403 for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) {
4404 gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g));
4405 if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) {
4406 pr_debug_ratelimited(
4407 "%s read MSR index failed (%u, 0x%08llx)\n",
4408 __func__, i, gpa);
4409 goto vmabort;
4410 }
4411
4412 for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) {
4413 gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h));
4414 if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) {
4415 pr_debug_ratelimited(
4416 "%s read MSR failed (%u, 0x%08llx)\n",
4417 __func__, j, gpa);
4418 goto vmabort;
4419 }
4420 if (h.index != g.index)
4421 continue;
4422 if (h.value == g.value)
4423 break;
4424
4425 if (nested_vmx_load_msr_check(vcpu, &h)) {
4426 pr_debug_ratelimited(
4427 "%s check failed (%u, 0x%x, 0x%x)\n",
4428 __func__, j, h.index, h.reserved);
4429 goto vmabort;
4430 }
4431
Sean Christophersonf20935d2019-09-05 14:22:54 -07004432 if (kvm_set_msr(vcpu, h.index, h.value)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08004433 pr_debug_ratelimited(
4434 "%s WRMSR failed (%u, 0x%x, 0x%llx)\n",
4435 __func__, j, h.index, h.value);
4436 goto vmabort;
4437 }
4438 }
4439 }
4440
4441 return;
4442
4443vmabort:
4444 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
4445}
4446
4447/*
4448 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
4449 * and modify vmcs12 to make it see what it would expect to see there if
4450 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
4451 */
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004452void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason,
Sean Christopherson55d23752018-12-03 13:53:18 -08004453 u32 exit_intr_info, unsigned long exit_qualification)
4454{
4455 struct vcpu_vmx *vmx = to_vmx(vcpu);
4456 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4457
4458 /* trying to cancel vmlaunch/vmresume is a bug */
4459 WARN_ON_ONCE(vmx->nested.nested_run_pending);
4460
Sean Christophersoncb6a32c2021-03-02 09:45:14 -08004461 /* Similarly, triple faults in L2 should never escape. */
4462 WARN_ON_ONCE(kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu));
4463
Vitaly Kuznetsovf5c7e842021-05-03 17:08:51 +02004464 if (kvm_check_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu)) {
4465 /*
4466 * KVM_REQ_GET_NESTED_STATE_PAGES is also used to map
4467 * Enlightened VMCS after migration and we still need to
4468 * do that when something is forcing L2->L1 exit prior to
4469 * the first L2 run.
4470 */
4471 (void)nested_get_evmcs_page(vcpu);
4472 }
Maxim Levitskyf2c7ef32021-01-07 11:38:51 +02004473
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07004474 /* Service the TLB flush request for L2 before switching to L1. */
4475 if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu))
4476 kvm_vcpu_flush_tlb_current(vcpu);
4477
Peter Shier43fea4e2020-08-20 16:05:45 -07004478 /*
4479 * VCPU_EXREG_PDPTR will be clobbered in arch/x86/kvm/vmx/vmx.h between
4480 * now and the new vmentry. Ensure that the VMCS02 PDPTR fields are
4481 * up-to-date before switching to L1.
4482 */
4483 if (enable_ept && is_pae_paging(vcpu))
4484 vmx_ept_load_pdptrs(vcpu);
4485
Sean Christopherson55d23752018-12-03 13:53:18 -08004486 leave_guest_mode(vcpu);
4487
Paolo Bonzinib4b65b52019-01-29 19:12:35 +01004488 if (nested_cpu_has_preemption_timer(vmcs12))
4489 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
4490
Ilias Stamatisd041b5e2021-05-26 19:44:17 +01004491 if (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETTING)) {
4492 vcpu->arch.tsc_offset = vcpu->arch.l1_tsc_offset;
4493 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_TSC_SCALING))
4494 vcpu->arch.tsc_scaling_ratio = vcpu->arch.l1_tsc_scaling_ratio;
4495 }
Sean Christopherson55d23752018-12-03 13:53:18 -08004496
4497 if (likely(!vmx->fail)) {
Sean Christopherson3731905ef2019-05-07 08:36:27 -07004498 sync_vmcs02_to_vmcs12(vcpu, vmcs12);
Sean Christophersonf4f83162019-05-07 08:36:26 -07004499
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004500 if (vm_exit_reason != -1)
4501 prepare_vmcs12(vcpu, vmcs12, vm_exit_reason,
4502 exit_intr_info, exit_qualification);
Sean Christopherson55d23752018-12-03 13:53:18 -08004503
4504 /*
Sean Christopherson3731905ef2019-05-07 08:36:27 -07004505 * Must happen outside of sync_vmcs02_to_vmcs12() as it will
Sean Christopherson55d23752018-12-03 13:53:18 -08004506 * also be used to capture vmcs12 cache as part of
4507 * capturing nVMX state for snapshot (migration).
4508 *
4509 * Otherwise, this flush will dirty guest memory at a
4510 * point it is already assumed by user-space to be
4511 * immutable.
4512 */
4513 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08004514 } else {
4515 /*
4516 * The only expected VM-instruction error is "VM entry with
4517 * invalid control field(s)." Anything else indicates a
4518 * problem with L0. And we should never get here with a
4519 * VMFail of any type if early consistency checks are enabled.
4520 */
4521 WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) !=
4522 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
4523 WARN_ON_ONCE(nested_early_check);
4524 }
4525
4526 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
4527
4528 /* Update any VMCS fields that might have changed while L2 ran */
4529 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
4530 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
4531 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Ilias Stamatis1ab92872021-06-07 11:54:38 +01004532 if (kvm_has_tsc_control)
4533 vmcs_write64(TSC_MULTIPLIER, vcpu->arch.tsc_scaling_ratio);
4534
Liran Alon02d496cf2019-11-11 14:30:55 +02004535 if (vmx->nested.l1_tpr_threshold != -1)
4536 vmcs_write32(TPR_THRESHOLD, vmx->nested.l1_tpr_threshold);
Sean Christopherson55d23752018-12-03 13:53:18 -08004537
Sean Christopherson55d23752018-12-03 13:53:18 -08004538 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
4539 vmx->nested.change_vmcs01_virtual_apic_mode = false;
4540 vmx_set_virtual_apic_mode(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08004541 }
4542
Makarand Sonarea85863c2021-02-12 16:50:12 -08004543 if (vmx->nested.update_vmcs01_cpu_dirty_logging) {
4544 vmx->nested.update_vmcs01_cpu_dirty_logging = false;
4545 vmx_update_cpu_dirty_logging(vcpu);
4546 }
4547
Sean Christopherson55d23752018-12-03 13:53:18 -08004548 /* Unpin physical memory we referred to in vmcs02 */
4549 if (vmx->nested.apic_access_page) {
Liran Alonb11494b2019-11-21 00:31:47 +02004550 kvm_release_page_clean(vmx->nested.apic_access_page);
Sean Christopherson55d23752018-12-03 13:53:18 -08004551 vmx->nested.apic_access_page = NULL;
4552 }
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01004553 kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map, true);
KarimAllah Ahmed3278e042019-01-31 21:24:38 +01004554 kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map, true);
4555 vmx->nested.pi_desc = NULL;
Sean Christopherson55d23752018-12-03 13:53:18 -08004556
Sean Christopherson1196cb92020-03-20 14:28:23 -07004557 if (vmx->nested.reload_vmcs01_apic_access_page) {
4558 vmx->nested.reload_vmcs01_apic_access_page = false;
4559 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
4560 }
Sean Christopherson55d23752018-12-03 13:53:18 -08004561
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004562 if ((vm_exit_reason != -1) &&
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02004563 (enable_shadow_vmcs || evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)))
Sean Christopherson3731905ef2019-05-07 08:36:27 -07004564 vmx->nested.need_vmcs12_to_shadow_sync = true;
Sean Christopherson55d23752018-12-03 13:53:18 -08004565
4566 /* in case we halted in L2 */
4567 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
4568
4569 if (likely(!vmx->fail)) {
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004570 if ((u16)vm_exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
Sean Christophersona1c77ab2020-03-02 22:27:35 -08004571 nested_exit_intr_ack_set(vcpu)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08004572 int irq = kvm_cpu_get_interrupt(vcpu);
4573 WARN_ON(irq < 0);
4574 vmcs12->vm_exit_intr_info = irq |
4575 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
4576 }
4577
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004578 if (vm_exit_reason != -1)
Sean Christopherson55d23752018-12-03 13:53:18 -08004579 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
4580 vmcs12->exit_qualification,
4581 vmcs12->idt_vectoring_info_field,
4582 vmcs12->vm_exit_intr_info,
4583 vmcs12->vm_exit_intr_error_code,
4584 KVM_ISA_VMX);
4585
4586 load_vmcs12_host_state(vcpu, vmcs12);
4587
4588 return;
4589 }
4590
4591 /*
4592 * After an early L2 VM-entry failure, we're now back
4593 * in L1 which thinks it just finished a VMLAUNCH or
4594 * VMRESUME instruction, so we need to set the failure
4595 * flag and the VM-instruction error field of the VMCS
4596 * accordingly, and skip the emulated instruction.
4597 */
Sean Christophersonb2656e42020-06-08 18:56:07 -07004598 (void)nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Sean Christopherson55d23752018-12-03 13:53:18 -08004599
4600 /*
4601 * Restore L1's host state to KVM's software model. We're here
4602 * because a consistency check was caught by hardware, which
4603 * means some amount of guest state has been propagated to KVM's
4604 * model and needs to be unwound to the host's state.
4605 */
4606 nested_vmx_restore_host_state(vcpu);
4607
4608 vmx->fail = 0;
4609}
4610
Sean Christophersoncb6a32c2021-03-02 09:45:14 -08004611static void nested_vmx_triple_fault(struct kvm_vcpu *vcpu)
4612{
4613 nested_vmx_vmexit(vcpu, EXIT_REASON_TRIPLE_FAULT, 0, 0);
4614}
4615
Sean Christopherson55d23752018-12-03 13:53:18 -08004616/*
4617 * Decode the memory-address operand of a vmx instruction, as recorded on an
4618 * exit caused by such an instruction (run by a guest hypervisor).
4619 * On success, returns 0. When the operand is invalid, returns 1 and throws
Miaohe Lin49f933d2020-02-27 11:20:54 +08004620 * #UD, #GP, or #SS.
Sean Christopherson55d23752018-12-03 13:53:18 -08004621 */
4622int get_vmx_mem_address(struct kvm_vcpu *vcpu, unsigned long exit_qualification,
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004623 u32 vmx_instruction_info, bool wr, int len, gva_t *ret)
Sean Christopherson55d23752018-12-03 13:53:18 -08004624{
4625 gva_t off;
4626 bool exn;
4627 struct kvm_segment s;
4628
4629 /*
4630 * According to Vol. 3B, "Information for VM Exits Due to Instruction
4631 * Execution", on an exit, vmx_instruction_info holds most of the
4632 * addressing components of the operand. Only the displacement part
4633 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
4634 * For how an actual address is calculated from all these components,
4635 * refer to Vol. 1, "Operand Addressing".
4636 */
4637 int scaling = vmx_instruction_info & 3;
4638 int addr_size = (vmx_instruction_info >> 7) & 7;
4639 bool is_reg = vmx_instruction_info & (1u << 10);
4640 int seg_reg = (vmx_instruction_info >> 15) & 7;
4641 int index_reg = (vmx_instruction_info >> 18) & 0xf;
4642 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
4643 int base_reg = (vmx_instruction_info >> 23) & 0xf;
4644 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
4645
4646 if (is_reg) {
4647 kvm_queue_exception(vcpu, UD_VECTOR);
4648 return 1;
4649 }
4650
4651 /* Addr = segment_base + offset */
4652 /* offset = base + [index * scale] + displacement */
4653 off = exit_qualification; /* holds the displacement */
Sean Christopherson946c5222019-01-23 14:39:23 -08004654 if (addr_size == 1)
4655 off = (gva_t)sign_extend64(off, 31);
4656 else if (addr_size == 0)
4657 off = (gva_t)sign_extend64(off, 15);
Sean Christopherson55d23752018-12-03 13:53:18 -08004658 if (base_is_valid)
4659 off += kvm_register_read(vcpu, base_reg);
4660 if (index_is_valid)
Miaohe Line6302692020-02-15 10:44:22 +08004661 off += kvm_register_read(vcpu, index_reg) << scaling;
Sean Christopherson55d23752018-12-03 13:53:18 -08004662 vmx_get_segment(vcpu, &s, seg_reg);
Sean Christopherson55d23752018-12-03 13:53:18 -08004663
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004664 /*
4665 * The effective address, i.e. @off, of a memory operand is truncated
4666 * based on the address size of the instruction. Note that this is
4667 * the *effective address*, i.e. the address prior to accounting for
4668 * the segment's base.
4669 */
Sean Christopherson55d23752018-12-03 13:53:18 -08004670 if (addr_size == 1) /* 32 bit */
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004671 off &= 0xffffffff;
4672 else if (addr_size == 0) /* 16 bit */
4673 off &= 0xffff;
Sean Christopherson55d23752018-12-03 13:53:18 -08004674
4675 /* Checks for #GP/#SS exceptions. */
4676 exn = false;
4677 if (is_long_mode(vcpu)) {
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004678 /*
4679 * The virtual/linear address is never truncated in 64-bit
4680 * mode, e.g. a 32-bit address size can yield a 64-bit virtual
4681 * address when using FS/GS with a non-zero base.
4682 */
Liran Alon6694e482019-07-15 18:47:44 +03004683 if (seg_reg == VCPU_SREG_FS || seg_reg == VCPU_SREG_GS)
4684 *ret = s.base + off;
4685 else
4686 *ret = off;
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004687
Sean Christopherson55d23752018-12-03 13:53:18 -08004688 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
4689 * non-canonical form. This is the only check on the memory
4690 * destination for long mode!
4691 */
4692 exn = is_noncanonical_address(*ret, vcpu);
Paolo Bonzinie0dfacb2019-01-30 17:25:38 +01004693 } else {
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004694 /*
4695 * When not in long mode, the virtual/linear address is
4696 * unconditionally truncated to 32 bits regardless of the
4697 * address size.
4698 */
4699 *ret = (s.base + off) & 0xffffffff;
4700
Sean Christopherson55d23752018-12-03 13:53:18 -08004701 /* Protected mode: apply checks for segment validity in the
4702 * following order:
4703 * - segment type check (#GP(0) may be thrown)
4704 * - usability check (#GP(0)/#SS(0))
4705 * - limit check (#GP(0)/#SS(0))
4706 */
4707 if (wr)
4708 /* #GP(0) if the destination operand is located in a
4709 * read-only data segment or any code segment.
4710 */
4711 exn = ((s.type & 0xa) == 0 || (s.type & 8));
4712 else
4713 /* #GP(0) if the source operand is located in an
4714 * execute-only code segment
4715 */
4716 exn = ((s.type & 0xa) == 8);
4717 if (exn) {
4718 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
4719 return 1;
4720 }
4721 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
4722 */
4723 exn = (s.unusable != 0);
Sean Christopherson34333cc2019-01-23 14:39:25 -08004724
4725 /*
4726 * Protected mode: #GP(0)/#SS(0) if the memory operand is
4727 * outside the segment limit. All CPUs that support VMX ignore
4728 * limit checks for flat segments, i.e. segments with base==0,
4729 * limit==0xffffffff and of type expand-up data or code.
Sean Christopherson55d23752018-12-03 13:53:18 -08004730 */
Sean Christopherson34333cc2019-01-23 14:39:25 -08004731 if (!(s.base == 0 && s.limit == 0xffffffff &&
4732 ((s.type & 8) || !(s.type & 4))))
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004733 exn = exn || ((u64)off + len - 1 > s.limit);
Sean Christopherson55d23752018-12-03 13:53:18 -08004734 }
4735 if (exn) {
4736 kvm_queue_exception_e(vcpu,
4737 seg_reg == VCPU_SREG_SS ?
4738 SS_VECTOR : GP_VECTOR,
4739 0);
4740 return 1;
4741 }
4742
4743 return 0;
4744}
4745
Oliver Upton03a8871a2019-11-13 16:17:20 -08004746void nested_vmx_pmu_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
4747{
4748 struct vcpu_vmx *vmx;
4749
4750 if (!nested_vmx_allowed(vcpu))
4751 return;
4752
4753 vmx = to_vmx(vcpu);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004754 if (kvm_x86_ops.pmu_ops->is_valid_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL)) {
Oliver Upton03a8871a2019-11-13 16:17:20 -08004755 vmx->nested.msrs.entry_ctls_high |=
4756 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
4757 vmx->nested.msrs.exit_ctls_high |=
4758 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
4759 } else {
4760 vmx->nested.msrs.entry_ctls_high &=
4761 ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
4762 vmx->nested.msrs.exit_ctls_high &=
Chenyi Qiangc6b177a2020-08-28 16:56:21 +08004763 ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
Oliver Upton03a8871a2019-11-13 16:17:20 -08004764 }
4765}
4766
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004767static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer,
4768 int *ret)
Sean Christopherson55d23752018-12-03 13:53:18 -08004769{
4770 gva_t gva;
4771 struct x86_exception e;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004772 int r;
Sean Christopherson55d23752018-12-03 13:53:18 -08004773
Sean Christopherson5addc232020-04-15 13:34:53 -07004774 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004775 vmcs_read32(VMX_INSTRUCTION_INFO), false,
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004776 sizeof(*vmpointer), &gva)) {
4777 *ret = 1;
4778 return -EINVAL;
4779 }
Sean Christopherson55d23752018-12-03 13:53:18 -08004780
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004781 r = kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e);
4782 if (r != X86EMUL_CONTINUE) {
Babu Moger3f3393b2020-09-11 14:29:05 -05004783 *ret = kvm_handle_memory_failure(vcpu, r, &e);
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004784 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08004785 }
4786
4787 return 0;
4788}
4789
4790/*
4791 * Allocate a shadow VMCS and associate it with the currently loaded
4792 * VMCS, unless such a shadow VMCS already exists. The newly allocated
4793 * VMCS is also VMCLEARed, so that it is ready for use.
4794 */
4795static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
4796{
4797 struct vcpu_vmx *vmx = to_vmx(vcpu);
4798 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
4799
4800 /*
4801 * We should allocate a shadow vmcs for vmcs01 only when L1
4802 * executes VMXON and free it when L1 executes VMXOFF.
4803 * As it is invalid to execute VMXON twice, we shouldn't reach
4804 * here when vmcs01 already have an allocated shadow vmcs.
4805 */
4806 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
4807
4808 if (!loaded_vmcs->shadow_vmcs) {
4809 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
4810 if (loaded_vmcs->shadow_vmcs)
4811 vmcs_clear(loaded_vmcs->shadow_vmcs);
4812 }
4813 return loaded_vmcs->shadow_vmcs;
4814}
4815
4816static int enter_vmx_operation(struct kvm_vcpu *vcpu)
4817{
4818 struct vcpu_vmx *vmx = to_vmx(vcpu);
4819 int r;
4820
4821 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
4822 if (r < 0)
4823 goto out_vmcs02;
4824
Ben Gardon41836832019-02-11 11:02:52 -08004825 vmx->nested.cached_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT);
Sean Christopherson55d23752018-12-03 13:53:18 -08004826 if (!vmx->nested.cached_vmcs12)
4827 goto out_cached_vmcs12;
4828
Ben Gardon41836832019-02-11 11:02:52 -08004829 vmx->nested.cached_shadow_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT);
Sean Christopherson55d23752018-12-03 13:53:18 -08004830 if (!vmx->nested.cached_shadow_vmcs12)
4831 goto out_cached_shadow_vmcs12;
4832
4833 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
4834 goto out_shadow_vmcs;
4835
4836 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
Jim Mattsonada00982020-05-08 13:36:42 -07004837 HRTIMER_MODE_ABS_PINNED);
Sean Christopherson55d23752018-12-03 13:53:18 -08004838 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
4839
4840 vmx->nested.vpid02 = allocate_vpid();
4841
4842 vmx->nested.vmcs02_initialized = false;
4843 vmx->nested.vmxon = true;
Luwei Kangee85dec2018-10-24 16:05:16 +08004844
Sean Christopherson2ef76192020-03-02 15:56:22 -08004845 if (vmx_pt_mode_is_host_guest()) {
Luwei Kangee85dec2018-10-24 16:05:16 +08004846 vmx->pt_desc.guest.ctl = 0;
Aaron Lewis476c9bd2020-09-25 16:34:18 +02004847 pt_update_intercept_for_msr(vcpu);
Luwei Kangee85dec2018-10-24 16:05:16 +08004848 }
4849
Sean Christopherson55d23752018-12-03 13:53:18 -08004850 return 0;
4851
4852out_shadow_vmcs:
4853 kfree(vmx->nested.cached_shadow_vmcs12);
4854
4855out_cached_shadow_vmcs12:
4856 kfree(vmx->nested.cached_vmcs12);
4857
4858out_cached_vmcs12:
4859 free_loaded_vmcs(&vmx->nested.vmcs02);
4860
4861out_vmcs02:
4862 return -ENOMEM;
4863}
4864
4865/*
4866 * Emulate the VMXON instruction.
4867 * Currently, we just remember that VMX is active, and do not save or even
4868 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
4869 * do not currently need to store anything in that guest-allocated memory
4870 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
4871 * argument is different from the VMXON pointer (which the spec says they do).
4872 */
4873static int handle_vmon(struct kvm_vcpu *vcpu)
4874{
4875 int ret;
4876 gpa_t vmptr;
KarimAllah Ahmed2e408932019-01-31 21:24:31 +01004877 uint32_t revision;
Sean Christopherson55d23752018-12-03 13:53:18 -08004878 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson32ad73d2019-12-20 20:44:55 -08004879 const u64 VMXON_NEEDED_FEATURES = FEAT_CTL_LOCKED
4880 | FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX;
Sean Christopherson55d23752018-12-03 13:53:18 -08004881
4882 /*
4883 * The Intel VMX Instruction Reference lists a bunch of bits that are
4884 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
Sean Christophersonc2fe3cd2020-10-06 18:44:15 -07004885 * 1 (see vmx_is_valid_cr4() for when we allow the guest to set this).
Sean Christopherson55d23752018-12-03 13:53:18 -08004886 * Otherwise, we should fail with #UD. But most faulting conditions
4887 * have already been checked by hardware, prior to the VM-exit for
4888 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
4889 * that bit set to 1 in non-root mode.
4890 */
4891 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
4892 kvm_queue_exception(vcpu, UD_VECTOR);
4893 return 1;
4894 }
4895
4896 /* CPL=0 must be checked manually. */
4897 if (vmx_get_cpl(vcpu)) {
4898 kvm_inject_gp(vcpu, 0);
4899 return 1;
4900 }
4901
4902 if (vmx->nested.vmxon)
Sean Christophersonb2656e42020-06-08 18:56:07 -07004903 return nested_vmx_fail(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Sean Christopherson55d23752018-12-03 13:53:18 -08004904
4905 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
4906 != VMXON_NEEDED_FEATURES) {
4907 kvm_inject_gp(vcpu, 0);
4908 return 1;
4909 }
4910
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004911 if (nested_vmx_get_vmptr(vcpu, &vmptr, &ret))
4912 return ret;
Sean Christopherson55d23752018-12-03 13:53:18 -08004913
4914 /*
4915 * SDM 3: 24.11.5
4916 * The first 4 bytes of VMXON region contain the supported
4917 * VMCS revision identifier
4918 *
4919 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
4920 * which replaces physical address width with 32
4921 */
KarimAllah Ahmede0bf2662019-01-31 21:24:43 +01004922 if (!page_address_valid(vcpu, vmptr))
Sean Christopherson55d23752018-12-03 13:53:18 -08004923 return nested_vmx_failInvalid(vcpu);
4924
KarimAllah Ahmed2e408932019-01-31 21:24:31 +01004925 if (kvm_read_guest(vcpu->kvm, vmptr, &revision, sizeof(revision)) ||
4926 revision != VMCS12_REVISION)
Sean Christopherson55d23752018-12-03 13:53:18 -08004927 return nested_vmx_failInvalid(vcpu);
4928
Sean Christopherson55d23752018-12-03 13:53:18 -08004929 vmx->nested.vmxon_ptr = vmptr;
4930 ret = enter_vmx_operation(vcpu);
4931 if (ret)
4932 return ret;
4933
4934 return nested_vmx_succeed(vcpu);
4935}
4936
4937static inline void nested_release_vmcs12(struct kvm_vcpu *vcpu)
4938{
4939 struct vcpu_vmx *vmx = to_vmx(vcpu);
4940
4941 if (vmx->nested.current_vmptr == -1ull)
4942 return;
4943
Sean Christopherson7952d762019-05-07 08:36:29 -07004944 copy_vmcs02_to_vmcs12_rare(vcpu, get_vmcs12(vcpu));
4945
Sean Christopherson55d23752018-12-03 13:53:18 -08004946 if (enable_shadow_vmcs) {
4947 /* copy to memory all shadowed fields in case
4948 they were modified */
4949 copy_shadow_to_vmcs12(vmx);
Sean Christopherson55d23752018-12-03 13:53:18 -08004950 vmx_disable_shadow_vmcs(vmx);
4951 }
4952 vmx->nested.posted_intr_nv = -1;
4953
4954 /* Flush VMCS12 to guest memory */
4955 kvm_vcpu_write_guest_page(vcpu,
4956 vmx->nested.current_vmptr >> PAGE_SHIFT,
4957 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
4958
4959 kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
4960
4961 vmx->nested.current_vmptr = -1ull;
4962}
4963
4964/* Emulate the VMXOFF instruction */
4965static int handle_vmoff(struct kvm_vcpu *vcpu)
4966{
4967 if (!nested_vmx_check_permission(vcpu))
4968 return 1;
Liran Alon4b9852f2019-08-26 13:24:49 +03004969
Sean Christopherson55d23752018-12-03 13:53:18 -08004970 free_nested(vcpu);
Liran Alon4b9852f2019-08-26 13:24:49 +03004971
4972 /* Process a latched INIT during time CPU was in VMX operation */
4973 kvm_make_request(KVM_REQ_EVENT, vcpu);
4974
Sean Christopherson55d23752018-12-03 13:53:18 -08004975 return nested_vmx_succeed(vcpu);
4976}
4977
4978/* Emulate the VMCLEAR instruction */
4979static int handle_vmclear(struct kvm_vcpu *vcpu)
4980{
4981 struct vcpu_vmx *vmx = to_vmx(vcpu);
4982 u32 zero = 0;
4983 gpa_t vmptr;
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02004984 u64 evmcs_gpa;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004985 int r;
Sean Christopherson55d23752018-12-03 13:53:18 -08004986
4987 if (!nested_vmx_check_permission(vcpu))
4988 return 1;
4989
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004990 if (nested_vmx_get_vmptr(vcpu, &vmptr, &r))
4991 return r;
Sean Christopherson55d23752018-12-03 13:53:18 -08004992
KarimAllah Ahmede0bf2662019-01-31 21:24:43 +01004993 if (!page_address_valid(vcpu, vmptr))
Sean Christophersonb2656e42020-06-08 18:56:07 -07004994 return nested_vmx_fail(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
Sean Christopherson55d23752018-12-03 13:53:18 -08004995
4996 if (vmptr == vmx->nested.vmxon_ptr)
Sean Christophersonb2656e42020-06-08 18:56:07 -07004997 return nested_vmx_fail(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
Sean Christopherson55d23752018-12-03 13:53:18 -08004998
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02004999 /*
5000 * When Enlightened VMEntry is enabled on the calling CPU we treat
5001 * memory area pointer by vmptr as Enlightened VMCS (as there's no good
5002 * way to distinguish it from VMCS12) and we must not corrupt it by
5003 * writing to the non-existent 'launch_state' field. The area doesn't
5004 * have to be the currently active EVMCS on the calling CPU and there's
5005 * nothing KVM has to do to transition it from 'active' to 'non-active'
5006 * state. It is possible that the area will stay mapped as
5007 * vmx->nested.hv_evmcs but this shouldn't be a problem.
5008 */
5009 if (likely(!vmx->nested.enlightened_vmcs_enabled ||
5010 !nested_enlightened_vmentry(vcpu, &evmcs_gpa))) {
Sean Christopherson55d23752018-12-03 13:53:18 -08005011 if (vmptr == vmx->nested.current_vmptr)
5012 nested_release_vmcs12(vcpu);
5013
5014 kvm_vcpu_write_guest(vcpu,
5015 vmptr + offsetof(struct vmcs12,
5016 launch_state),
5017 &zero, sizeof(zero));
Vitaly Kuznetsov3b19b812021-05-26 15:20:21 +02005018 } else if (vmx->nested.hv_evmcs && vmptr == vmx->nested.hv_evmcs_vmptr) {
5019 nested_release_evmcs(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005020 }
5021
5022 return nested_vmx_succeed(vcpu);
5023}
5024
Sean Christopherson55d23752018-12-03 13:53:18 -08005025/* Emulate the VMLAUNCH instruction */
5026static int handle_vmlaunch(struct kvm_vcpu *vcpu)
5027{
5028 return nested_vmx_run(vcpu, true);
5029}
5030
5031/* Emulate the VMRESUME instruction */
5032static int handle_vmresume(struct kvm_vcpu *vcpu)
5033{
5034
5035 return nested_vmx_run(vcpu, false);
5036}
5037
5038static int handle_vmread(struct kvm_vcpu *vcpu)
5039{
Jim Mattsondd2d6042019-12-06 15:46:35 -08005040 struct vmcs12 *vmcs12 = is_guest_mode(vcpu) ? get_shadow_vmcs12(vcpu)
5041 : get_vmcs12(vcpu);
Sean Christopherson5addc232020-04-15 13:34:53 -07005042 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005043 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5044 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzinif7eea632019-09-14 00:26:27 +02005045 struct x86_exception e;
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005046 unsigned long field;
5047 u64 value;
5048 gva_t gva = 0;
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07005049 short offset;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005050 int len, r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005051
5052 if (!nested_vmx_check_permission(vcpu))
5053 return 1;
5054
Jim Mattsondd2d6042019-12-06 15:46:35 -08005055 /*
5056 * In VMX non-root operation, when the VMCS-link pointer is -1ull,
5057 * any VMREAD sets the ALU flags for VMfailInvalid.
5058 */
5059 if (vmx->nested.current_vmptr == -1ull ||
5060 (is_guest_mode(vcpu) &&
5061 get_vmcs12(vcpu)->vmcs_link_pointer == -1ull))
Sean Christopherson55d23752018-12-03 13:53:18 -08005062 return nested_vmx_failInvalid(vcpu);
5063
Sean Christopherson55d23752018-12-03 13:53:18 -08005064 /* Decode instruction info and find the field to read */
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005065 field = kvm_register_read(vcpu, (((instr_info) >> 28) & 0xf));
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07005066
5067 offset = vmcs_field_to_offset(field);
5068 if (offset < 0)
Sean Christophersonb2656e42020-06-08 18:56:07 -07005069 return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Sean Christopherson55d23752018-12-03 13:53:18 -08005070
Sean Christopherson7952d762019-05-07 08:36:29 -07005071 if (!is_guest_mode(vcpu) && is_vmcs12_ext_field(field))
5072 copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
5073
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005074 /* Read the field, zero-extended to a u64 value */
5075 value = vmcs12_read_any(vmcs12, field, offset);
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07005076
Sean Christopherson55d23752018-12-03 13:53:18 -08005077 /*
5078 * Now copy part of this value to register or memory, as requested.
5079 * Note that the number of bits actually copied is 32 or 64 depending
5080 * on the guest's mode (32 or 64 bit), not on the given field's length.
5081 */
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005082 if (instr_info & BIT(10)) {
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005083 kvm_register_write(vcpu, (((instr_info) >> 3) & 0xf), value);
Sean Christopherson55d23752018-12-03 13:53:18 -08005084 } else {
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005085 len = is_64_bit_mode(vcpu) ? 8 : 4;
Sean Christopherson55d23752018-12-03 13:53:18 -08005086 if (get_vmx_mem_address(vcpu, exit_qualification,
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005087 instr_info, true, len, &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08005088 return 1;
5089 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005090 r = kvm_write_guest_virt_system(vcpu, gva, &value, len, &e);
5091 if (r != X86EMUL_CONTINUE)
Babu Moger3f3393b2020-09-11 14:29:05 -05005092 return kvm_handle_memory_failure(vcpu, r, &e);
Sean Christopherson55d23752018-12-03 13:53:18 -08005093 }
5094
5095 return nested_vmx_succeed(vcpu);
5096}
5097
Sean Christophersone2174292019-05-07 08:36:28 -07005098static bool is_shadow_field_rw(unsigned long field)
5099{
5100 switch (field) {
5101#define SHADOW_FIELD_RW(x, y) case x:
5102#include "vmcs_shadow_fields.h"
5103 return true;
5104 default:
5105 break;
5106 }
5107 return false;
5108}
5109
5110static bool is_shadow_field_ro(unsigned long field)
5111{
5112 switch (field) {
5113#define SHADOW_FIELD_RO(x, y) case x:
5114#include "vmcs_shadow_fields.h"
5115 return true;
5116 default:
5117 break;
5118 }
5119 return false;
5120}
Sean Christopherson55d23752018-12-03 13:53:18 -08005121
5122static int handle_vmwrite(struct kvm_vcpu *vcpu)
5123{
Jim Mattsondd2d6042019-12-06 15:46:35 -08005124 struct vmcs12 *vmcs12 = is_guest_mode(vcpu) ? get_shadow_vmcs12(vcpu)
5125 : get_vmcs12(vcpu);
Sean Christopherson5addc232020-04-15 13:34:53 -07005126 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005127 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5128 struct vcpu_vmx *vmx = to_vmx(vcpu);
5129 struct x86_exception e;
5130 unsigned long field;
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07005131 short offset;
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005132 gva_t gva;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005133 int len, r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005134
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005135 /*
5136 * The value to write might be 32 or 64 bits, depending on L1's long
Sean Christopherson55d23752018-12-03 13:53:18 -08005137 * mode, and eventually we need to write that into a field of several
5138 * possible lengths. The code below first zero-extends the value to 64
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005139 * bit (value), and then copies only the appropriate number of
Sean Christopherson55d23752018-12-03 13:53:18 -08005140 * bits into the vmcs12 field.
5141 */
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005142 u64 value = 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08005143
5144 if (!nested_vmx_check_permission(vcpu))
5145 return 1;
5146
Jim Mattsondd2d6042019-12-06 15:46:35 -08005147 /*
5148 * In VMX non-root operation, when the VMCS-link pointer is -1ull,
5149 * any VMWRITE sets the ALU flags for VMfailInvalid.
5150 */
5151 if (vmx->nested.current_vmptr == -1ull ||
5152 (is_guest_mode(vcpu) &&
5153 get_vmcs12(vcpu)->vmcs_link_pointer == -1ull))
Sean Christopherson55d23752018-12-03 13:53:18 -08005154 return nested_vmx_failInvalid(vcpu);
5155
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005156 if (instr_info & BIT(10))
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005157 value = kvm_register_read(vcpu, (((instr_info) >> 3) & 0xf));
Sean Christopherson55d23752018-12-03 13:53:18 -08005158 else {
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005159 len = is_64_bit_mode(vcpu) ? 8 : 4;
Sean Christopherson55d23752018-12-03 13:53:18 -08005160 if (get_vmx_mem_address(vcpu, exit_qualification,
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005161 instr_info, false, len, &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08005162 return 1;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005163 r = kvm_read_guest_virt(vcpu, gva, &value, len, &e);
5164 if (r != X86EMUL_CONTINUE)
Babu Moger3f3393b2020-09-11 14:29:05 -05005165 return kvm_handle_memory_failure(vcpu, r, &e);
Sean Christopherson55d23752018-12-03 13:53:18 -08005166 }
5167
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005168 field = kvm_register_read(vcpu, (((instr_info) >> 28) & 0xf));
Sean Christopherson55d23752018-12-03 13:53:18 -08005169
Jim Mattson693e02c2019-12-06 15:46:36 -08005170 offset = vmcs_field_to_offset(field);
5171 if (offset < 0)
Sean Christophersonb2656e42020-06-08 18:56:07 -07005172 return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Jim Mattson693e02c2019-12-06 15:46:36 -08005173
Sean Christopherson55d23752018-12-03 13:53:18 -08005174 /*
5175 * If the vCPU supports "VMWRITE to any supported field in the
5176 * VMCS," then the "read-only" fields are actually read/write.
5177 */
5178 if (vmcs_field_readonly(field) &&
5179 !nested_cpu_has_vmwrite_any_field(vcpu))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005180 return nested_vmx_fail(vcpu, VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Sean Christopherson55d23752018-12-03 13:53:18 -08005181
Jim Mattsondd2d6042019-12-06 15:46:35 -08005182 /*
5183 * Ensure vmcs12 is up-to-date before any VMWRITE that dirties
5184 * vmcs12, else we may crush a field or consume a stale value.
5185 */
5186 if (!is_guest_mode(vcpu) && !is_shadow_field_rw(field))
5187 copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08005188
5189 /*
Sean Christophersonb6437802019-05-07 08:36:24 -07005190 * Some Intel CPUs intentionally drop the reserved bits of the AR byte
5191 * fields on VMWRITE. Emulate this behavior to ensure consistent KVM
5192 * behavior regardless of the underlying hardware, e.g. if an AR_BYTE
5193 * field is intercepted for VMWRITE but not VMREAD (in L1), then VMREAD
5194 * from L1 will return a different value than VMREAD from L2 (L1 sees
5195 * the stripped down value, L2 sees the full value as stored by KVM).
Sean Christopherson55d23752018-12-03 13:53:18 -08005196 */
Sean Christophersonb6437802019-05-07 08:36:24 -07005197 if (field >= GUEST_ES_AR_BYTES && field <= GUEST_TR_AR_BYTES)
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005198 value &= 0x1f0ff;
Sean Christophersonb6437802019-05-07 08:36:24 -07005199
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005200 vmcs12_write_any(vmcs12, field, offset, value);
Sean Christopherson55d23752018-12-03 13:53:18 -08005201
5202 /*
Sean Christophersone2174292019-05-07 08:36:28 -07005203 * Do not track vmcs12 dirty-state if in guest-mode as we actually
5204 * dirty shadow vmcs12 instead of vmcs12. Fields that can be updated
5205 * by L1 without a vmexit are always updated in the vmcs02, i.e. don't
5206 * "dirty" vmcs12, all others go down the prepare_vmcs02() slow path.
Sean Christopherson55d23752018-12-03 13:53:18 -08005207 */
Sean Christophersone2174292019-05-07 08:36:28 -07005208 if (!is_guest_mode(vcpu) && !is_shadow_field_rw(field)) {
5209 /*
5210 * L1 can read these fields without exiting, ensure the
5211 * shadow VMCS is up-to-date.
5212 */
5213 if (enable_shadow_vmcs && is_shadow_field_ro(field)) {
5214 preempt_disable();
5215 vmcs_load(vmx->vmcs01.shadow_vmcs);
Sean Christophersonfadcead2019-05-07 08:36:23 -07005216
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005217 __vmcs_writel(field, value);
Sean Christophersonfadcead2019-05-07 08:36:23 -07005218
Sean Christophersone2174292019-05-07 08:36:28 -07005219 vmcs_clear(vmx->vmcs01.shadow_vmcs);
5220 vmcs_load(vmx->loaded_vmcs->vmcs);
5221 preempt_enable();
Sean Christopherson55d23752018-12-03 13:53:18 -08005222 }
Sean Christophersone2174292019-05-07 08:36:28 -07005223 vmx->nested.dirty_vmcs12 = true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005224 }
5225
5226 return nested_vmx_succeed(vcpu);
5227}
5228
5229static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
5230{
5231 vmx->nested.current_vmptr = vmptr;
5232 if (enable_shadow_vmcs) {
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07005233 secondary_exec_controls_setbit(vmx, SECONDARY_EXEC_SHADOW_VMCS);
Sean Christopherson55d23752018-12-03 13:53:18 -08005234 vmcs_write64(VMCS_LINK_POINTER,
5235 __pa(vmx->vmcs01.shadow_vmcs));
Sean Christopherson3731905ef2019-05-07 08:36:27 -07005236 vmx->nested.need_vmcs12_to_shadow_sync = true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005237 }
5238 vmx->nested.dirty_vmcs12 = true;
5239}
5240
5241/* Emulate the VMPTRLD instruction */
5242static int handle_vmptrld(struct kvm_vcpu *vcpu)
5243{
5244 struct vcpu_vmx *vmx = to_vmx(vcpu);
5245 gpa_t vmptr;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005246 int r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005247
5248 if (!nested_vmx_check_permission(vcpu))
5249 return 1;
5250
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005251 if (nested_vmx_get_vmptr(vcpu, &vmptr, &r))
5252 return r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005253
KarimAllah Ahmede0bf2662019-01-31 21:24:43 +01005254 if (!page_address_valid(vcpu, vmptr))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005255 return nested_vmx_fail(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
Sean Christopherson55d23752018-12-03 13:53:18 -08005256
5257 if (vmptr == vmx->nested.vmxon_ptr)
Sean Christophersonb2656e42020-06-08 18:56:07 -07005258 return nested_vmx_fail(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
Sean Christopherson55d23752018-12-03 13:53:18 -08005259
5260 /* Forbid normal VMPTRLD if Enlightened version was used */
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02005261 if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
Sean Christopherson55d23752018-12-03 13:53:18 -08005262 return 1;
5263
5264 if (vmx->nested.current_vmptr != vmptr) {
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01005265 struct kvm_host_map map;
Sean Christopherson55d23752018-12-03 13:53:18 -08005266 struct vmcs12 *new_vmcs12;
Sean Christopherson55d23752018-12-03 13:53:18 -08005267
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01005268 if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmptr), &map)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08005269 /*
5270 * Reads from an unbacked page return all 1s,
5271 * which means that the 32 bits located at the
5272 * given physical address won't match the required
5273 * VMCS12_REVISION identifier.
5274 */
Sean Christophersonb2656e42020-06-08 18:56:07 -07005275 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005276 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Sean Christopherson55d23752018-12-03 13:53:18 -08005277 }
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01005278
5279 new_vmcs12 = map.hva;
5280
Sean Christopherson55d23752018-12-03 13:53:18 -08005281 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
5282 (new_vmcs12->hdr.shadow_vmcs &&
5283 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01005284 kvm_vcpu_unmap(vcpu, &map, false);
Sean Christophersonb2656e42020-06-08 18:56:07 -07005285 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005286 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
5287 }
5288
5289 nested_release_vmcs12(vcpu);
5290
5291 /*
5292 * Load VMCS12 from guest memory since it is not already
5293 * cached.
5294 */
5295 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01005296 kvm_vcpu_unmap(vcpu, &map, false);
Sean Christopherson55d23752018-12-03 13:53:18 -08005297
5298 set_current_vmptr(vmx, vmptr);
5299 }
5300
5301 return nested_vmx_succeed(vcpu);
5302}
5303
5304/* Emulate the VMPTRST instruction */
5305static int handle_vmptrst(struct kvm_vcpu *vcpu)
5306{
Sean Christopherson5addc232020-04-15 13:34:53 -07005307 unsigned long exit_qual = vmx_get_exit_qual(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005308 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5309 gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr;
5310 struct x86_exception e;
5311 gva_t gva;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005312 int r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005313
5314 if (!nested_vmx_check_permission(vcpu))
5315 return 1;
5316
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02005317 if (unlikely(evmptr_is_valid(to_vmx(vcpu)->nested.hv_evmcs_vmptr)))
Sean Christopherson55d23752018-12-03 13:53:18 -08005318 return 1;
5319
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005320 if (get_vmx_mem_address(vcpu, exit_qual, instr_info,
5321 true, sizeof(gpa_t), &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08005322 return 1;
5323 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005324 r = kvm_write_guest_virt_system(vcpu, gva, (void *)&current_vmptr,
5325 sizeof(gpa_t), &e);
5326 if (r != X86EMUL_CONTINUE)
Babu Moger3f3393b2020-09-11 14:29:05 -05005327 return kvm_handle_memory_failure(vcpu, r, &e);
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005328
Sean Christopherson55d23752018-12-03 13:53:18 -08005329 return nested_vmx_succeed(vcpu);
5330}
5331
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005332#define EPTP_PA_MASK GENMASK_ULL(51, 12)
5333
5334static bool nested_ept_root_matches(hpa_t root_hpa, u64 root_eptp, u64 eptp)
5335{
5336 return VALID_PAGE(root_hpa) &&
5337 ((root_eptp & EPTP_PA_MASK) == (eptp & EPTP_PA_MASK));
5338}
5339
Sean Christopherson55d23752018-12-03 13:53:18 -08005340/* Emulate the INVEPT instruction */
5341static int handle_invept(struct kvm_vcpu *vcpu)
5342{
5343 struct vcpu_vmx *vmx = to_vmx(vcpu);
5344 u32 vmx_instruction_info, types;
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005345 unsigned long type, roots_to_free;
5346 struct kvm_mmu *mmu;
Sean Christopherson55d23752018-12-03 13:53:18 -08005347 gva_t gva;
5348 struct x86_exception e;
5349 struct {
5350 u64 eptp, gpa;
5351 } operand;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005352 int i, r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005353
5354 if (!(vmx->nested.msrs.secondary_ctls_high &
5355 SECONDARY_EXEC_ENABLE_EPT) ||
5356 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
5357 kvm_queue_exception(vcpu, UD_VECTOR);
5358 return 1;
5359 }
5360
5361 if (!nested_vmx_check_permission(vcpu))
5362 return 1;
5363
5364 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005365 type = kvm_register_read(vcpu, (vmx_instruction_info >> 28) & 0xf);
Sean Christopherson55d23752018-12-03 13:53:18 -08005366
5367 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
5368
5369 if (type >= 32 || !(types & (1 << type)))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005370 return nested_vmx_fail(vcpu, VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Sean Christopherson55d23752018-12-03 13:53:18 -08005371
5372 /* According to the Intel VMX instruction reference, the memory
5373 * operand is read even if it isn't needed (e.g., for type==global)
5374 */
Sean Christopherson5addc232020-04-15 13:34:53 -07005375 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005376 vmx_instruction_info, false, sizeof(operand), &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08005377 return 1;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005378 r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e);
5379 if (r != X86EMUL_CONTINUE)
Babu Moger3f3393b2020-09-11 14:29:05 -05005380 return kvm_handle_memory_failure(vcpu, r, &e);
Sean Christopherson55d23752018-12-03 13:53:18 -08005381
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005382 /*
5383 * Nested EPT roots are always held through guest_mmu,
5384 * not root_mmu.
5385 */
5386 mmu = &vcpu->arch.guest_mmu;
5387
Sean Christopherson55d23752018-12-03 13:53:18 -08005388 switch (type) {
Sean Christopherson55d23752018-12-03 13:53:18 -08005389 case VMX_EPT_EXTENT_CONTEXT:
Sean Christophersoneed00302020-03-20 14:27:58 -07005390 if (!nested_vmx_check_eptp(vcpu, operand.eptp))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005391 return nested_vmx_fail(vcpu,
Sean Christophersoneed00302020-03-20 14:27:58 -07005392 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Sean Christophersonf8aa7e32020-03-20 14:27:59 -07005393
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005394 roots_to_free = 0;
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07005395 if (nested_ept_root_matches(mmu->root_hpa, mmu->root_pgd,
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005396 operand.eptp))
5397 roots_to_free |= KVM_MMU_ROOT_CURRENT;
5398
5399 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) {
5400 if (nested_ept_root_matches(mmu->prev_roots[i].hpa,
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07005401 mmu->prev_roots[i].pgd,
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005402 operand.eptp))
5403 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
5404 }
5405 break;
Sean Christophersoneed00302020-03-20 14:27:58 -07005406 case VMX_EPT_EXTENT_GLOBAL:
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005407 roots_to_free = KVM_MMU_ROOTS_ALL;
Sean Christopherson55d23752018-12-03 13:53:18 -08005408 break;
5409 default:
Sean Christophersonf9336e32020-05-04 08:35:06 -07005410 BUG();
Sean Christopherson55d23752018-12-03 13:53:18 -08005411 break;
5412 }
5413
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005414 if (roots_to_free)
5415 kvm_mmu_free_roots(vcpu, mmu, roots_to_free);
5416
Sean Christopherson55d23752018-12-03 13:53:18 -08005417 return nested_vmx_succeed(vcpu);
5418}
5419
5420static int handle_invvpid(struct kvm_vcpu *vcpu)
5421{
5422 struct vcpu_vmx *vmx = to_vmx(vcpu);
5423 u32 vmx_instruction_info;
5424 unsigned long type, types;
5425 gva_t gva;
5426 struct x86_exception e;
5427 struct {
5428 u64 vpid;
5429 u64 gla;
5430 } operand;
5431 u16 vpid02;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005432 int r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005433
5434 if (!(vmx->nested.msrs.secondary_ctls_high &
5435 SECONDARY_EXEC_ENABLE_VPID) ||
5436 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
5437 kvm_queue_exception(vcpu, UD_VECTOR);
5438 return 1;
5439 }
5440
5441 if (!nested_vmx_check_permission(vcpu))
5442 return 1;
5443
5444 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005445 type = kvm_register_read(vcpu, (vmx_instruction_info >> 28) & 0xf);
Sean Christopherson55d23752018-12-03 13:53:18 -08005446
5447 types = (vmx->nested.msrs.vpid_caps &
5448 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
5449
5450 if (type >= 32 || !(types & (1 << type)))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005451 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005452 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
5453
5454 /* according to the intel vmx instruction reference, the memory
5455 * operand is read even if it isn't needed (e.g., for type==global)
5456 */
Sean Christopherson5addc232020-04-15 13:34:53 -07005457 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005458 vmx_instruction_info, false, sizeof(operand), &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08005459 return 1;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005460 r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e);
5461 if (r != X86EMUL_CONTINUE)
Babu Moger3f3393b2020-09-11 14:29:05 -05005462 return kvm_handle_memory_failure(vcpu, r, &e);
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005463
Sean Christopherson55d23752018-12-03 13:53:18 -08005464 if (operand.vpid >> 16)
Sean Christophersonb2656e42020-06-08 18:56:07 -07005465 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005466 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
5467
5468 vpid02 = nested_get_vpid02(vcpu);
5469 switch (type) {
5470 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
5471 if (!operand.vpid ||
5472 is_noncanonical_address(operand.gla, vcpu))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005473 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005474 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Sean Christophersonbc41d0c2020-03-20 14:28:09 -07005475 vpid_sync_vcpu_addr(vpid02, operand.gla);
Sean Christopherson55d23752018-12-03 13:53:18 -08005476 break;
5477 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
5478 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
5479 if (!operand.vpid)
Sean Christophersonb2656e42020-06-08 18:56:07 -07005480 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005481 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Sean Christopherson446ace42020-03-20 14:28:05 -07005482 vpid_sync_context(vpid02);
Sean Christopherson55d23752018-12-03 13:53:18 -08005483 break;
5484 case VMX_VPID_EXTENT_ALL_CONTEXT:
Sean Christopherson446ace42020-03-20 14:28:05 -07005485 vpid_sync_context(vpid02);
Sean Christopherson55d23752018-12-03 13:53:18 -08005486 break;
5487 default:
5488 WARN_ON_ONCE(1);
5489 return kvm_skip_emulated_instruction(vcpu);
5490 }
5491
Junaid Shahidd6e3f832020-03-20 14:28:00 -07005492 /*
5493 * Sync the shadow page tables if EPT is disabled, L1 is invalidating
Sean Christopherson25b62c62021-06-09 16:42:29 -07005494 * linear mappings for L2 (tagged with L2's VPID). Free all guest
5495 * roots as VPIDs are not tracked in the MMU role.
Junaid Shahidd6e3f832020-03-20 14:28:00 -07005496 *
5497 * Note, this operates on root_mmu, not guest_mmu, as L1 and L2 share
5498 * an MMU when EPT is disabled.
5499 *
5500 * TODO: sync only the affected SPTEs for INVDIVIDUAL_ADDR.
5501 */
5502 if (!enable_ept)
Sean Christopherson25b62c62021-06-09 16:42:29 -07005503 kvm_mmu_free_guest_mode_roots(vcpu, &vcpu->arch.root_mmu);
Junaid Shahidd6e3f832020-03-20 14:28:00 -07005504
Sean Christopherson55d23752018-12-03 13:53:18 -08005505 return nested_vmx_succeed(vcpu);
5506}
5507
5508static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
5509 struct vmcs12 *vmcs12)
5510{
Sean Christopherson2b3eaf82019-04-30 10:36:19 -07005511 u32 index = kvm_rcx_read(vcpu);
Sean Christophersonac6389a2020-03-02 18:02:38 -08005512 u64 new_eptp;
Sean Christopherson55d23752018-12-03 13:53:18 -08005513
Sean Christophersonc5ffd402021-06-09 16:42:35 -07005514 if (WARN_ON_ONCE(!nested_cpu_has_ept(vmcs12)))
Sean Christopherson55d23752018-12-03 13:53:18 -08005515 return 1;
Sean Christopherson55d23752018-12-03 13:53:18 -08005516 if (index >= VMFUNC_EPTP_ENTRIES)
5517 return 1;
5518
Sean Christopherson55d23752018-12-03 13:53:18 -08005519 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
Sean Christophersonac6389a2020-03-02 18:02:38 -08005520 &new_eptp, index * 8, 8))
Sean Christopherson55d23752018-12-03 13:53:18 -08005521 return 1;
5522
Sean Christopherson55d23752018-12-03 13:53:18 -08005523 /*
5524 * If the (L2) guest does a vmfunc to the currently
5525 * active ept pointer, we don't have to do anything else
5526 */
Sean Christophersonac6389a2020-03-02 18:02:38 -08005527 if (vmcs12->ept_pointer != new_eptp) {
5528 if (!nested_vmx_check_eptp(vcpu, new_eptp))
Sean Christopherson55d23752018-12-03 13:53:18 -08005529 return 1;
5530
Sean Christophersonac6389a2020-03-02 18:02:38 -08005531 vmcs12->ept_pointer = new_eptp;
Sean Christopherson39353ab2021-06-09 16:42:31 -07005532 nested_ept_new_eptp(vcpu);
Sean Christophersonc805f5d2021-03-04 17:10:57 -08005533
Sean Christopherson39353ab2021-06-09 16:42:31 -07005534 if (!nested_cpu_has_vpid(vmcs12))
5535 kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005536 }
5537
5538 return 0;
5539}
5540
5541static int handle_vmfunc(struct kvm_vcpu *vcpu)
5542{
5543 struct vcpu_vmx *vmx = to_vmx(vcpu);
5544 struct vmcs12 *vmcs12;
Sean Christopherson2b3eaf82019-04-30 10:36:19 -07005545 u32 function = kvm_rax_read(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005546
5547 /*
5548 * VMFUNC is only supported for nested guests, but we always enable the
5549 * secondary control for simplicity; for non-nested mode, fake that we
5550 * didn't by injecting #UD.
5551 */
5552 if (!is_guest_mode(vcpu)) {
5553 kvm_queue_exception(vcpu, UD_VECTOR);
5554 return 1;
5555 }
5556
5557 vmcs12 = get_vmcs12(vcpu);
Sean Christopherson546e8392021-06-09 16:42:34 -07005558
5559 /*
5560 * #UD on out-of-bounds function has priority over VM-Exit, and VMFUNC
5561 * is enabled in vmcs02 if and only if it's enabled in vmcs12.
5562 */
5563 if (WARN_ON_ONCE((function > 63) || !nested_cpu_has_vmfunc(vmcs12))) {
5564 kvm_queue_exception(vcpu, UD_VECTOR);
5565 return 1;
5566 }
5567
Sean Christopherson0e752252021-06-09 16:42:22 -07005568 if (!(vmcs12->vm_function_control & BIT_ULL(function)))
Sean Christopherson55d23752018-12-03 13:53:18 -08005569 goto fail;
5570
5571 switch (function) {
5572 case 0:
5573 if (nested_vmx_eptp_switching(vcpu, vmcs12))
5574 goto fail;
5575 break;
5576 default:
5577 goto fail;
5578 }
5579 return kvm_skip_emulated_instruction(vcpu);
5580
5581fail:
Sean Christopherson8e533242020-11-06 17:03:12 +08005582 /*
5583 * This is effectively a reflected VM-Exit, as opposed to a synthesized
5584 * nested VM-Exit. Pass the original exit reason, i.e. don't hardcode
5585 * EXIT_REASON_VMFUNC as the exit reason.
5586 */
5587 nested_vmx_vmexit(vcpu, vmx->exit_reason.full,
Sean Christopherson87915852020-04-15 13:34:54 -07005588 vmx_get_intr_info(vcpu),
Sean Christopherson5addc232020-04-15 13:34:53 -07005589 vmx_get_exit_qual(vcpu));
Sean Christopherson55d23752018-12-03 13:53:18 -08005590 return 1;
5591}
5592
Oliver Uptone71237d2020-02-04 15:26:30 -08005593/*
5594 * Return true if an IO instruction with the specified port and size should cause
5595 * a VM-exit into L1.
5596 */
5597bool nested_vmx_check_io_bitmaps(struct kvm_vcpu *vcpu, unsigned int port,
5598 int size)
Sean Christopherson55d23752018-12-03 13:53:18 -08005599{
Oliver Uptone71237d2020-02-04 15:26:30 -08005600 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005601 gpa_t bitmap, last_bitmap;
Sean Christopherson55d23752018-12-03 13:53:18 -08005602 u8 b;
5603
Sean Christopherson55d23752018-12-03 13:53:18 -08005604 last_bitmap = (gpa_t)-1;
5605 b = -1;
5606
5607 while (size > 0) {
5608 if (port < 0x8000)
5609 bitmap = vmcs12->io_bitmap_a;
5610 else if (port < 0x10000)
5611 bitmap = vmcs12->io_bitmap_b;
5612 else
5613 return true;
5614 bitmap += (port & 0x7fff) / 8;
5615
5616 if (last_bitmap != bitmap)
5617 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
5618 return true;
5619 if (b & (1 << (port & 7)))
5620 return true;
5621
5622 port++;
5623 size--;
5624 last_bitmap = bitmap;
5625 }
5626
5627 return false;
5628}
5629
Oliver Uptone71237d2020-02-04 15:26:30 -08005630static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
5631 struct vmcs12 *vmcs12)
5632{
5633 unsigned long exit_qualification;
Oliver Upton35a57132020-02-04 15:26:31 -08005634 unsigned short port;
Oliver Uptone71237d2020-02-04 15:26:30 -08005635 int size;
5636
5637 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
5638 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
5639
Sean Christopherson5addc232020-04-15 13:34:53 -07005640 exit_qualification = vmx_get_exit_qual(vcpu);
Oliver Uptone71237d2020-02-04 15:26:30 -08005641
5642 port = exit_qualification >> 16;
5643 size = (exit_qualification & 7) + 1;
5644
5645 return nested_vmx_check_io_bitmaps(vcpu, port, size);
5646}
5647
Sean Christopherson55d23752018-12-03 13:53:18 -08005648/*
Miaohe Lin463bfee2020-02-14 10:44:05 +08005649 * Return 1 if we should exit from L2 to L1 to handle an MSR access,
Sean Christopherson55d23752018-12-03 13:53:18 -08005650 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
5651 * disinterest in the current event (read or write a specific MSR) by using an
5652 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
5653 */
5654static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
Sean Christopherson8e533242020-11-06 17:03:12 +08005655 struct vmcs12 *vmcs12,
5656 union vmx_exit_reason exit_reason)
Sean Christopherson55d23752018-12-03 13:53:18 -08005657{
Sean Christopherson2b3eaf82019-04-30 10:36:19 -07005658 u32 msr_index = kvm_rcx_read(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005659 gpa_t bitmap;
5660
5661 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
5662 return true;
5663
5664 /*
5665 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
5666 * for the four combinations of read/write and low/high MSR numbers.
5667 * First we need to figure out which of the four to use:
5668 */
5669 bitmap = vmcs12->msr_bitmap;
Sean Christopherson8e533242020-11-06 17:03:12 +08005670 if (exit_reason.basic == EXIT_REASON_MSR_WRITE)
Sean Christopherson55d23752018-12-03 13:53:18 -08005671 bitmap += 2048;
5672 if (msr_index >= 0xc0000000) {
5673 msr_index -= 0xc0000000;
5674 bitmap += 1024;
5675 }
5676
5677 /* Then read the msr_index'th bit from this bitmap: */
5678 if (msr_index < 1024*8) {
5679 unsigned char b;
5680 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
5681 return true;
5682 return 1 & (b >> (msr_index & 7));
5683 } else
5684 return true; /* let L1 handle the wrong parameter */
5685}
5686
5687/*
5688 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
5689 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
5690 * intercept (via guest_host_mask etc.) the current event.
5691 */
5692static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
5693 struct vmcs12 *vmcs12)
5694{
Sean Christopherson5addc232020-04-15 13:34:53 -07005695 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005696 int cr = exit_qualification & 15;
5697 int reg;
5698 unsigned long val;
5699
5700 switch ((exit_qualification >> 4) & 3) {
5701 case 0: /* mov to cr */
5702 reg = (exit_qualification >> 8) & 15;
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005703 val = kvm_register_read(vcpu, reg);
Sean Christopherson55d23752018-12-03 13:53:18 -08005704 switch (cr) {
5705 case 0:
5706 if (vmcs12->cr0_guest_host_mask &
5707 (val ^ vmcs12->cr0_read_shadow))
5708 return true;
5709 break;
5710 case 3:
Sean Christopherson55d23752018-12-03 13:53:18 -08005711 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
5712 return true;
5713 break;
5714 case 4:
5715 if (vmcs12->cr4_guest_host_mask &
5716 (vmcs12->cr4_read_shadow ^ val))
5717 return true;
5718 break;
5719 case 8:
5720 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
5721 return true;
5722 break;
5723 }
5724 break;
5725 case 2: /* clts */
5726 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
5727 (vmcs12->cr0_read_shadow & X86_CR0_TS))
5728 return true;
5729 break;
5730 case 1: /* mov from cr */
5731 switch (cr) {
5732 case 3:
5733 if (vmcs12->cpu_based_vm_exec_control &
5734 CPU_BASED_CR3_STORE_EXITING)
5735 return true;
5736 break;
5737 case 8:
5738 if (vmcs12->cpu_based_vm_exec_control &
5739 CPU_BASED_CR8_STORE_EXITING)
5740 return true;
5741 break;
5742 }
5743 break;
5744 case 3: /* lmsw */
5745 /*
5746 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
5747 * cr0. Other attempted changes are ignored, with no exit.
5748 */
5749 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
5750 if (vmcs12->cr0_guest_host_mask & 0xe &
5751 (val ^ vmcs12->cr0_read_shadow))
5752 return true;
5753 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
5754 !(vmcs12->cr0_read_shadow & 0x1) &&
5755 (val & 0x1))
5756 return true;
5757 break;
5758 }
5759 return false;
5760}
5761
Sean Christopherson72add912021-04-12 16:21:42 +12005762static bool nested_vmx_exit_handled_encls(struct kvm_vcpu *vcpu,
5763 struct vmcs12 *vmcs12)
5764{
5765 u32 encls_leaf;
5766
5767 if (!guest_cpuid_has(vcpu, X86_FEATURE_SGX) ||
5768 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENCLS_EXITING))
5769 return false;
5770
5771 encls_leaf = kvm_rax_read(vcpu);
5772 if (encls_leaf > 62)
5773 encls_leaf = 63;
5774 return vmcs12->encls_exiting_bitmap & BIT_ULL(encls_leaf);
5775}
5776
Sean Christopherson55d23752018-12-03 13:53:18 -08005777static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
5778 struct vmcs12 *vmcs12, gpa_t bitmap)
5779{
5780 u32 vmx_instruction_info;
5781 unsigned long field;
5782 u8 b;
5783
5784 if (!nested_cpu_has_shadow_vmcs(vmcs12))
5785 return true;
5786
5787 /* Decode instruction info and find the field to access */
5788 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5789 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
5790
5791 /* Out-of-range fields always cause a VM exit from L2 to L1 */
5792 if (field >> 15)
5793 return true;
5794
5795 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
5796 return true;
5797
5798 return 1 & (b >> (field & 7));
5799}
5800
Oliver Uptonb045ae92020-04-14 22:47:45 +00005801static bool nested_vmx_exit_handled_mtf(struct vmcs12 *vmcs12)
5802{
5803 u32 entry_intr_info = vmcs12->vm_entry_intr_info_field;
5804
5805 if (nested_cpu_has_mtf(vmcs12))
5806 return true;
5807
5808 /*
5809 * An MTF VM-exit may be injected into the guest by setting the
5810 * interruption-type to 7 (other event) and the vector field to 0. Such
5811 * is the case regardless of the 'monitor trap flag' VM-execution
5812 * control.
5813 */
5814 return entry_intr_info == (INTR_INFO_VALID_MASK
5815 | INTR_TYPE_OTHER_EVENT);
5816}
5817
Sean Christopherson55d23752018-12-03 13:53:18 -08005818/*
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005819 * Return true if L0 wants to handle an exit from L2 regardless of whether or not
5820 * L1 wants the exit. Only call this when in is_guest_mode (L2).
Sean Christopherson55d23752018-12-03 13:53:18 -08005821 */
Sean Christopherson8e533242020-11-06 17:03:12 +08005822static bool nested_vmx_l0_wants_exit(struct kvm_vcpu *vcpu,
5823 union vmx_exit_reason exit_reason)
Sean Christopherson55d23752018-12-03 13:53:18 -08005824{
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005825 u32 intr_info;
5826
Sean Christopherson8e533242020-11-06 17:03:12 +08005827 switch ((u16)exit_reason.basic) {
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005828 case EXIT_REASON_EXCEPTION_NMI:
Sean Christopherson87915852020-04-15 13:34:54 -07005829 intr_info = vmx_get_intr_info(vcpu);
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005830 if (is_nmi(intr_info))
5831 return true;
5832 else if (is_page_fault(intr_info))
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +02005833 return vcpu->arch.apf.host_apf_flags || !enable_ept;
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005834 else if (is_debug(intr_info) &&
5835 vcpu->guest_debug &
5836 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5837 return true;
5838 else if (is_breakpoint(intr_info) &&
5839 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
5840 return true;
Sean Christophersonb33bb782021-06-22 10:22:44 -07005841 else if (is_alignment_check(intr_info) &&
5842 !vmx_guest_inject_ac(vcpu))
5843 return true;
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005844 return false;
5845 case EXIT_REASON_EXTERNAL_INTERRUPT:
5846 return true;
5847 case EXIT_REASON_MCE_DURING_VMENTRY:
5848 return true;
5849 case EXIT_REASON_EPT_VIOLATION:
5850 /*
5851 * L0 always deals with the EPT violation. If nested EPT is
5852 * used, and the nested mmu code discovers that the address is
5853 * missing in the guest EPT table (EPT12), the EPT violation
5854 * will be injected with nested_ept_inject_page_fault()
5855 */
5856 return true;
5857 case EXIT_REASON_EPT_MISCONFIG:
5858 /*
5859 * L2 never uses directly L1's EPT, but rather L0's own EPT
5860 * table (shadow on EPT) or a merged EPT table that L0 built
5861 * (EPT on EPT). So any problems with the structure of the
5862 * table is L0's fault.
5863 */
5864 return true;
5865 case EXIT_REASON_PREEMPTION_TIMER:
5866 return true;
5867 case EXIT_REASON_PML_FULL:
Sean Christophersonc3bb9a22021-02-12 16:50:07 -08005868 /*
5869 * PML is emulated for an L1 VMM and should never be enabled in
5870 * vmcs02, always "handle" PML_FULL by exiting to userspace.
5871 */
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005872 return true;
5873 case EXIT_REASON_VMFUNC:
5874 /* VM functions are emulated through L2->L0 vmexits. */
5875 return true;
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005876 default:
5877 break;
5878 }
5879 return false;
5880}
5881
5882/*
5883 * Return 1 if L1 wants to intercept an exit from L2. Only call this when in
5884 * is_guest_mode (L2).
5885 */
Sean Christopherson8e533242020-11-06 17:03:12 +08005886static bool nested_vmx_l1_wants_exit(struct kvm_vcpu *vcpu,
5887 union vmx_exit_reason exit_reason)
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005888{
Sean Christopherson55d23752018-12-03 13:53:18 -08005889 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Sean Christopherson9bd4af22020-04-21 00:53:27 -07005890 u32 intr_info;
Sean Christopherson55d23752018-12-03 13:53:18 -08005891
Sean Christopherson8e533242020-11-06 17:03:12 +08005892 switch ((u16)exit_reason.basic) {
Sean Christopherson55d23752018-12-03 13:53:18 -08005893 case EXIT_REASON_EXCEPTION_NMI:
Sean Christopherson87915852020-04-15 13:34:54 -07005894 intr_info = vmx_get_intr_info(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005895 if (is_nmi(intr_info))
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005896 return true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005897 else if (is_page_fault(intr_info))
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005898 return true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005899 return vmcs12->exception_bitmap &
5900 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
5901 case EXIT_REASON_EXTERNAL_INTERRUPT:
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005902 return nested_exit_on_intr(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005903 case EXIT_REASON_TRIPLE_FAULT:
5904 return true;
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08005905 case EXIT_REASON_INTERRUPT_WINDOW:
5906 return nested_cpu_has(vmcs12, CPU_BASED_INTR_WINDOW_EXITING);
Sean Christopherson55d23752018-12-03 13:53:18 -08005907 case EXIT_REASON_NMI_WINDOW:
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08005908 return nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING);
Sean Christopherson55d23752018-12-03 13:53:18 -08005909 case EXIT_REASON_TASK_SWITCH:
5910 return true;
5911 case EXIT_REASON_CPUID:
5912 return true;
5913 case EXIT_REASON_HLT:
5914 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
5915 case EXIT_REASON_INVD:
5916 return true;
5917 case EXIT_REASON_INVLPG:
5918 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
5919 case EXIT_REASON_RDPMC:
5920 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
5921 case EXIT_REASON_RDRAND:
5922 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
5923 case EXIT_REASON_RDSEED:
5924 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
5925 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
5926 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
5927 case EXIT_REASON_VMREAD:
5928 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
5929 vmcs12->vmread_bitmap);
5930 case EXIT_REASON_VMWRITE:
5931 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
5932 vmcs12->vmwrite_bitmap);
5933 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
5934 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
5935 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
5936 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
5937 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
5938 /*
5939 * VMX instructions trap unconditionally. This allows L1 to
5940 * emulate them for its L2 guest, i.e., allows 3-level nesting!
5941 */
5942 return true;
5943 case EXIT_REASON_CR_ACCESS:
5944 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
5945 case EXIT_REASON_DR_ACCESS:
5946 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
5947 case EXIT_REASON_IO_INSTRUCTION:
5948 return nested_vmx_exit_handled_io(vcpu, vmcs12);
5949 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
5950 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
5951 case EXIT_REASON_MSR_READ:
5952 case EXIT_REASON_MSR_WRITE:
5953 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
5954 case EXIT_REASON_INVALID_STATE:
5955 return true;
5956 case EXIT_REASON_MWAIT_INSTRUCTION:
5957 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
5958 case EXIT_REASON_MONITOR_TRAP_FLAG:
Oliver Uptonb045ae92020-04-14 22:47:45 +00005959 return nested_vmx_exit_handled_mtf(vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08005960 case EXIT_REASON_MONITOR_INSTRUCTION:
5961 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
5962 case EXIT_REASON_PAUSE_INSTRUCTION:
5963 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
5964 nested_cpu_has2(vmcs12,
5965 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
5966 case EXIT_REASON_MCE_DURING_VMENTRY:
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005967 return true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005968 case EXIT_REASON_TPR_BELOW_THRESHOLD:
5969 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
5970 case EXIT_REASON_APIC_ACCESS:
5971 case EXIT_REASON_APIC_WRITE:
5972 case EXIT_REASON_EOI_INDUCED:
5973 /*
5974 * The controls for "virtualize APIC accesses," "APIC-
5975 * register virtualization," and "virtual-interrupt
5976 * delivery" only come from vmcs12.
5977 */
5978 return true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005979 case EXIT_REASON_INVPCID:
5980 return
5981 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
5982 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
5983 case EXIT_REASON_WBINVD:
5984 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
5985 case EXIT_REASON_XSETBV:
5986 return true;
5987 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
5988 /*
5989 * This should never happen, since it is not possible to
5990 * set XSS to a non-zero value---neither in L1 nor in L2.
5991 * If if it were, XSS would have to be checked against
5992 * the XSS exit bitmap in vmcs12.
5993 */
5994 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Tao Xubf653b72019-07-16 14:55:51 +08005995 case EXIT_REASON_UMWAIT:
5996 case EXIT_REASON_TPAUSE:
5997 return nested_cpu_has2(vmcs12,
5998 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE);
Sean Christopherson72add912021-04-12 16:21:42 +12005999 case EXIT_REASON_ENCLS:
6000 return nested_vmx_exit_handled_encls(vcpu, vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08006001 default:
6002 return true;
6003 }
6004}
6005
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006006/*
6007 * Conditionally reflect a VM-Exit into L1. Returns %true if the VM-Exit was
6008 * reflected into L1.
6009 */
Sean Christophersonf47baae2020-04-15 10:55:16 -07006010bool nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu)
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006011{
Sean Christophersonfbdd5022020-04-15 10:55:12 -07006012 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson8e533242020-11-06 17:03:12 +08006013 union vmx_exit_reason exit_reason = vmx->exit_reason;
Sean Christopherson87796552020-04-22 17:11:27 -07006014 unsigned long exit_qual;
6015 u32 exit_intr_info;
Sean Christophersonfbdd5022020-04-15 10:55:12 -07006016
6017 WARN_ON_ONCE(vmx->nested.nested_run_pending);
6018
6019 /*
6020 * Late nested VM-Fail shares the same flow as nested VM-Exit since KVM
6021 * has already loaded L2's state.
6022 */
6023 if (unlikely(vmx->fail)) {
6024 trace_kvm_nested_vmenter_failed(
6025 "hardware VM-instruction error: ",
6026 vmcs_read32(VM_INSTRUCTION_ERROR));
6027 exit_intr_info = 0;
6028 exit_qual = 0;
6029 goto reflect_vmexit;
6030 }
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006031
Sean Christopherson8e533242020-11-06 17:03:12 +08006032 trace_kvm_nested_vmexit(exit_reason.full, vcpu, KVM_ISA_VMX);
Sean Christopherson236871b2020-04-15 10:55:13 -07006033
Sean Christopherson2c1f3322020-04-15 10:55:14 -07006034 /* If L0 (KVM) wants the exit, it trumps L1's desires. */
6035 if (nested_vmx_l0_wants_exit(vcpu, exit_reason))
6036 return false;
6037
6038 /* If L1 doesn't want the exit, handle it in L0. */
6039 if (!nested_vmx_l1_wants_exit(vcpu, exit_reason))
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006040 return false;
6041
6042 /*
Sean Christopherson1d283062020-04-15 10:55:15 -07006043 * vmcs.VM_EXIT_INTR_INFO is only valid for EXCEPTION_NMI exits. For
6044 * EXTERNAL_INTERRUPT, the value for vmcs12->vm_exit_intr_info would
6045 * need to be synthesized by querying the in-kernel LAPIC, but external
6046 * interrupts are never reflected to L1 so it's a non-issue.
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006047 */
Sean Christopherson02f19652020-09-23 13:13:49 -07006048 exit_intr_info = vmx_get_intr_info(vcpu);
Sean Christophersonf315f2b2020-09-23 13:13:45 -07006049 if (is_exception_with_error_code(exit_intr_info)) {
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006050 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6051
6052 vmcs12->vm_exit_intr_error_code =
6053 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
6054 }
Sean Christopherson02f19652020-09-23 13:13:49 -07006055 exit_qual = vmx_get_exit_qual(vcpu);
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006056
Sean Christophersonfbdd5022020-04-15 10:55:12 -07006057reflect_vmexit:
Sean Christopherson8e533242020-11-06 17:03:12 +08006058 nested_vmx_vmexit(vcpu, exit_reason.full, exit_intr_info, exit_qual);
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006059 return true;
6060}
Sean Christopherson55d23752018-12-03 13:53:18 -08006061
6062static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
6063 struct kvm_nested_state __user *user_kvm_nested_state,
6064 u32 user_data_size)
6065{
6066 struct vcpu_vmx *vmx;
6067 struct vmcs12 *vmcs12;
6068 struct kvm_nested_state kvm_state = {
6069 .flags = 0,
Liran Alon6ca00df2019-06-16 15:03:10 +03006070 .format = KVM_STATE_NESTED_FORMAT_VMX,
Sean Christopherson55d23752018-12-03 13:53:18 -08006071 .size = sizeof(kvm_state),
Peter Shier850448f2020-05-26 14:51:06 -07006072 .hdr.vmx.flags = 0,
Liran Alon6ca00df2019-06-16 15:03:10 +03006073 .hdr.vmx.vmxon_pa = -1ull,
6074 .hdr.vmx.vmcs12_pa = -1ull,
Peter Shier850448f2020-05-26 14:51:06 -07006075 .hdr.vmx.preemption_timer_deadline = 0,
Sean Christopherson55d23752018-12-03 13:53:18 -08006076 };
Liran Alon6ca00df2019-06-16 15:03:10 +03006077 struct kvm_vmx_nested_state_data __user *user_vmx_nested_state =
6078 &user_kvm_nested_state->data.vmx[0];
Sean Christopherson55d23752018-12-03 13:53:18 -08006079
6080 if (!vcpu)
Liran Alon6ca00df2019-06-16 15:03:10 +03006081 return kvm_state.size + sizeof(*user_vmx_nested_state);
Sean Christopherson55d23752018-12-03 13:53:18 -08006082
6083 vmx = to_vmx(vcpu);
6084 vmcs12 = get_vmcs12(vcpu);
6085
Sean Christopherson55d23752018-12-03 13:53:18 -08006086 if (nested_vmx_allowed(vcpu) &&
6087 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
Liran Alon6ca00df2019-06-16 15:03:10 +03006088 kvm_state.hdr.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
6089 kvm_state.hdr.vmx.vmcs12_pa = vmx->nested.current_vmptr;
Sean Christopherson55d23752018-12-03 13:53:18 -08006090
6091 if (vmx_has_valid_vmcs12(vcpu)) {
Liran Alon6ca00df2019-06-16 15:03:10 +03006092 kvm_state.size += sizeof(user_vmx_nested_state->vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08006093
Vitaly Kuznetsov27849962021-05-26 15:20:20 +02006094 /* 'hv_evmcs_vmptr' can also be EVMPTR_MAP_PENDING here */
6095 if (vmx->nested.hv_evmcs_vmptr != EVMPTR_INVALID)
Liran Alon323d73a2019-06-26 16:09:27 +03006096 kvm_state.flags |= KVM_STATE_NESTED_EVMCS;
6097
Sean Christopherson55d23752018-12-03 13:53:18 -08006098 if (is_guest_mode(vcpu) &&
6099 nested_cpu_has_shadow_vmcs(vmcs12) &&
6100 vmcs12->vmcs_link_pointer != -1ull)
Liran Alon6ca00df2019-06-16 15:03:10 +03006101 kvm_state.size += sizeof(user_vmx_nested_state->shadow_vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08006102 }
6103
6104 if (vmx->nested.smm.vmxon)
Liran Alon6ca00df2019-06-16 15:03:10 +03006105 kvm_state.hdr.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
Sean Christopherson55d23752018-12-03 13:53:18 -08006106
6107 if (vmx->nested.smm.guest_mode)
Liran Alon6ca00df2019-06-16 15:03:10 +03006108 kvm_state.hdr.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
Sean Christopherson55d23752018-12-03 13:53:18 -08006109
6110 if (is_guest_mode(vcpu)) {
6111 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
6112
6113 if (vmx->nested.nested_run_pending)
6114 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
Oliver Upton5ef8acb2020-02-07 02:36:07 -08006115
6116 if (vmx->nested.mtf_pending)
6117 kvm_state.flags |= KVM_STATE_NESTED_MTF_PENDING;
Peter Shier850448f2020-05-26 14:51:06 -07006118
6119 if (nested_cpu_has_preemption_timer(vmcs12) &&
6120 vmx->nested.has_preemption_timer_deadline) {
6121 kvm_state.hdr.vmx.flags |=
6122 KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE;
6123 kvm_state.hdr.vmx.preemption_timer_deadline =
6124 vmx->nested.preemption_timer_deadline;
6125 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006126 }
6127 }
6128
6129 if (user_data_size < kvm_state.size)
6130 goto out;
6131
6132 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
6133 return -EFAULT;
6134
6135 if (!vmx_has_valid_vmcs12(vcpu))
6136 goto out;
6137
6138 /*
6139 * When running L2, the authoritative vmcs12 state is in the
6140 * vmcs02. When running L1, the authoritative vmcs12 state is
6141 * in the shadow or enlightened vmcs linked to vmcs01, unless
Sean Christopherson3731905ef2019-05-07 08:36:27 -07006142 * need_vmcs12_to_shadow_sync is set, in which case, the authoritative
Sean Christopherson55d23752018-12-03 13:53:18 -08006143 * vmcs12 state is in the vmcs12 already.
6144 */
6145 if (is_guest_mode(vcpu)) {
Sean Christopherson3731905ef2019-05-07 08:36:27 -07006146 sync_vmcs02_to_vmcs12(vcpu, vmcs12);
Sean Christopherson7952d762019-05-07 08:36:29 -07006147 sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
Maxim Levitskyd51e1d32021-01-14 22:54:47 +02006148 } else {
6149 copy_vmcs02_to_vmcs12_rare(vcpu, get_vmcs12(vcpu));
6150 if (!vmx->nested.need_vmcs12_to_shadow_sync) {
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02006151 if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02006152 /*
6153 * L1 hypervisor is not obliged to keep eVMCS
6154 * clean fields data always up-to-date while
6155 * not in guest mode, 'hv_clean_fields' is only
6156 * supposed to be actual upon vmentry so we need
6157 * to ignore it here and do full copy.
6158 */
6159 copy_enlightened_to_vmcs12(vmx, 0);
Maxim Levitskyd51e1d32021-01-14 22:54:47 +02006160 else if (enable_shadow_vmcs)
6161 copy_shadow_to_vmcs12(vmx);
6162 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006163 }
6164
Liran Alon6ca00df2019-06-16 15:03:10 +03006165 BUILD_BUG_ON(sizeof(user_vmx_nested_state->vmcs12) < VMCS12_SIZE);
6166 BUILD_BUG_ON(sizeof(user_vmx_nested_state->shadow_vmcs12) < VMCS12_SIZE);
6167
Tom Roeder3a33d032019-01-24 13:48:20 -08006168 /*
6169 * Copy over the full allocated size of vmcs12 rather than just the size
6170 * of the struct.
6171 */
Liran Alon6ca00df2019-06-16 15:03:10 +03006172 if (copy_to_user(user_vmx_nested_state->vmcs12, vmcs12, VMCS12_SIZE))
Sean Christopherson55d23752018-12-03 13:53:18 -08006173 return -EFAULT;
6174
6175 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
6176 vmcs12->vmcs_link_pointer != -1ull) {
Liran Alon6ca00df2019-06-16 15:03:10 +03006177 if (copy_to_user(user_vmx_nested_state->shadow_vmcs12,
Tom Roeder3a33d032019-01-24 13:48:20 -08006178 get_shadow_vmcs12(vcpu), VMCS12_SIZE))
Sean Christopherson55d23752018-12-03 13:53:18 -08006179 return -EFAULT;
6180 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006181out:
6182 return kvm_state.size;
6183}
6184
6185/*
6186 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
6187 */
6188void vmx_leave_nested(struct kvm_vcpu *vcpu)
6189{
6190 if (is_guest_mode(vcpu)) {
6191 to_vmx(vcpu)->nested.nested_run_pending = 0;
6192 nested_vmx_vmexit(vcpu, -1, 0, 0);
6193 }
6194 free_nested(vcpu);
6195}
6196
6197static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
6198 struct kvm_nested_state __user *user_kvm_nested_state,
6199 struct kvm_nested_state *kvm_state)
6200{
6201 struct vcpu_vmx *vmx = to_vmx(vcpu);
6202 struct vmcs12 *vmcs12;
Sean Christopherson68cda402020-05-11 15:05:29 -07006203 enum vm_entry_failure_code ignored;
Liran Alon6ca00df2019-06-16 15:03:10 +03006204 struct kvm_vmx_nested_state_data __user *user_vmx_nested_state =
6205 &user_kvm_nested_state->data.vmx[0];
Sean Christopherson55d23752018-12-03 13:53:18 -08006206 int ret;
6207
Liran Alon6ca00df2019-06-16 15:03:10 +03006208 if (kvm_state->format != KVM_STATE_NESTED_FORMAT_VMX)
Sean Christopherson55d23752018-12-03 13:53:18 -08006209 return -EINVAL;
6210
Liran Alon6ca00df2019-06-16 15:03:10 +03006211 if (kvm_state->hdr.vmx.vmxon_pa == -1ull) {
6212 if (kvm_state->hdr.vmx.smm.flags)
Sean Christopherson55d23752018-12-03 13:53:18 -08006213 return -EINVAL;
6214
Liran Alon6ca00df2019-06-16 15:03:10 +03006215 if (kvm_state->hdr.vmx.vmcs12_pa != -1ull)
Sean Christopherson55d23752018-12-03 13:53:18 -08006216 return -EINVAL;
6217
Liran Alon323d73a2019-06-26 16:09:27 +03006218 /*
6219 * KVM_STATE_NESTED_EVMCS used to signal that KVM should
6220 * enable eVMCS capability on vCPU. However, since then
6221 * code was changed such that flag signals vmcs12 should
6222 * be copied into eVMCS in guest memory.
6223 *
6224 * To preserve backwards compatability, allow user
6225 * to set this flag even when there is no VMXON region.
6226 */
Paolo Bonzini9fd58872019-06-19 16:52:27 +02006227 if (kvm_state->flags & ~KVM_STATE_NESTED_EVMCS)
6228 return -EINVAL;
6229 } else {
6230 if (!nested_vmx_allowed(vcpu))
6231 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08006232
Paolo Bonzini9fd58872019-06-19 16:52:27 +02006233 if (!page_address_valid(vcpu, kvm_state->hdr.vmx.vmxon_pa))
6234 return -EINVAL;
Liran Alon323d73a2019-06-26 16:09:27 +03006235 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006236
Liran Alon6ca00df2019-06-16 15:03:10 +03006237 if ((kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
Sean Christopherson55d23752018-12-03 13:53:18 -08006238 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
6239 return -EINVAL;
6240
Liran Alon6ca00df2019-06-16 15:03:10 +03006241 if (kvm_state->hdr.vmx.smm.flags &
Sean Christopherson55d23752018-12-03 13:53:18 -08006242 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
6243 return -EINVAL;
6244
Paolo Bonzini5e105c82020-07-27 08:55:09 -04006245 if (kvm_state->hdr.vmx.flags & ~KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE)
6246 return -EINVAL;
6247
Sean Christopherson55d23752018-12-03 13:53:18 -08006248 /*
6249 * SMM temporarily disables VMX, so we cannot be in guest mode,
6250 * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags
6251 * must be zero.
6252 */
Liran Alon65b712f12019-06-25 14:26:42 +03006253 if (is_smm(vcpu) ?
6254 (kvm_state->flags &
6255 (KVM_STATE_NESTED_GUEST_MODE | KVM_STATE_NESTED_RUN_PENDING))
6256 : kvm_state->hdr.vmx.smm.flags)
Sean Christopherson55d23752018-12-03 13:53:18 -08006257 return -EINVAL;
6258
Liran Alon6ca00df2019-06-16 15:03:10 +03006259 if ((kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
6260 !(kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
Sean Christopherson55d23752018-12-03 13:53:18 -08006261 return -EINVAL;
6262
Liran Alon323d73a2019-06-26 16:09:27 +03006263 if ((kvm_state->flags & KVM_STATE_NESTED_EVMCS) &&
6264 (!nested_vmx_allowed(vcpu) || !vmx->nested.enlightened_vmcs_enabled))
Paolo Bonzini9fd58872019-06-19 16:52:27 +02006265 return -EINVAL;
6266
Liran Alon323d73a2019-06-26 16:09:27 +03006267 vmx_leave_nested(vcpu);
Paolo Bonzini9fd58872019-06-19 16:52:27 +02006268
Liran Alon6ca00df2019-06-16 15:03:10 +03006269 if (kvm_state->hdr.vmx.vmxon_pa == -1ull)
Sean Christopherson55d23752018-12-03 13:53:18 -08006270 return 0;
6271
Liran Alon6ca00df2019-06-16 15:03:10 +03006272 vmx->nested.vmxon_ptr = kvm_state->hdr.vmx.vmxon_pa;
Sean Christopherson55d23752018-12-03 13:53:18 -08006273 ret = enter_vmx_operation(vcpu);
6274 if (ret)
6275 return ret;
6276
Paolo Bonzini0f02bd02020-07-27 09:00:37 -04006277 /* Empty 'VMXON' state is permitted if no VMCS loaded */
6278 if (kvm_state->size < sizeof(*kvm_state) + sizeof(*vmcs12)) {
6279 /* See vmx_has_valid_vmcs12. */
6280 if ((kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE) ||
6281 (kvm_state->flags & KVM_STATE_NESTED_EVMCS) ||
6282 (kvm_state->hdr.vmx.vmcs12_pa != -1ull))
6283 return -EINVAL;
6284 else
6285 return 0;
6286 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006287
Liran Alon6ca00df2019-06-16 15:03:10 +03006288 if (kvm_state->hdr.vmx.vmcs12_pa != -1ull) {
6289 if (kvm_state->hdr.vmx.vmcs12_pa == kvm_state->hdr.vmx.vmxon_pa ||
6290 !page_address_valid(vcpu, kvm_state->hdr.vmx.vmcs12_pa))
Sean Christopherson55d23752018-12-03 13:53:18 -08006291 return -EINVAL;
6292
Liran Alon6ca00df2019-06-16 15:03:10 +03006293 set_current_vmptr(vmx, kvm_state->hdr.vmx.vmcs12_pa);
Sean Christopherson55d23752018-12-03 13:53:18 -08006294 } else if (kvm_state->flags & KVM_STATE_NESTED_EVMCS) {
6295 /*
Vitaly Kuznetsove942dbf2020-03-09 16:52:12 +01006296 * nested_vmx_handle_enlightened_vmptrld() cannot be called
6297 * directly from here as HV_X64_MSR_VP_ASSIST_PAGE may not be
6298 * restored yet. EVMCS will be mapped from
6299 * nested_get_vmcs12_pages().
Sean Christopherson55d23752018-12-03 13:53:18 -08006300 */
Vitaly Kuznetsov27849962021-05-26 15:20:20 +02006301 vmx->nested.hv_evmcs_vmptr = EVMPTR_MAP_PENDING;
Paolo Bonzini729c15c2020-09-22 06:53:57 -04006302 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08006303 } else {
6304 return -EINVAL;
6305 }
6306
Liran Alon6ca00df2019-06-16 15:03:10 +03006307 if (kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
Sean Christopherson55d23752018-12-03 13:53:18 -08006308 vmx->nested.smm.vmxon = true;
6309 vmx->nested.vmxon = false;
6310
Liran Alon6ca00df2019-06-16 15:03:10 +03006311 if (kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
Sean Christopherson55d23752018-12-03 13:53:18 -08006312 vmx->nested.smm.guest_mode = true;
6313 }
6314
6315 vmcs12 = get_vmcs12(vcpu);
Liran Alon6ca00df2019-06-16 15:03:10 +03006316 if (copy_from_user(vmcs12, user_vmx_nested_state->vmcs12, sizeof(*vmcs12)))
Sean Christopherson55d23752018-12-03 13:53:18 -08006317 return -EFAULT;
6318
6319 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
6320 return -EINVAL;
6321
6322 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
6323 return 0;
6324
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006325 vmx->nested.nested_run_pending =
6326 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
6327
Oliver Upton5ef8acb2020-02-07 02:36:07 -08006328 vmx->nested.mtf_pending =
6329 !!(kvm_state->flags & KVM_STATE_NESTED_MTF_PENDING);
6330
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006331 ret = -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08006332 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
6333 vmcs12->vmcs_link_pointer != -1ull) {
6334 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
6335
Liran Alon6ca00df2019-06-16 15:03:10 +03006336 if (kvm_state->size <
6337 sizeof(*kvm_state) +
6338 sizeof(user_vmx_nested_state->vmcs12) + sizeof(*shadow_vmcs12))
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006339 goto error_guest_mode;
Sean Christopherson55d23752018-12-03 13:53:18 -08006340
6341 if (copy_from_user(shadow_vmcs12,
Liran Alon6ca00df2019-06-16 15:03:10 +03006342 user_vmx_nested_state->shadow_vmcs12,
6343 sizeof(*shadow_vmcs12))) {
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006344 ret = -EFAULT;
6345 goto error_guest_mode;
6346 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006347
6348 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
6349 !shadow_vmcs12->hdr.shadow_vmcs)
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006350 goto error_guest_mode;
Sean Christopherson55d23752018-12-03 13:53:18 -08006351 }
6352
Paolo Bonzini83d31e52020-07-09 13:12:09 -04006353 vmx->nested.has_preemption_timer_deadline = false;
Peter Shier850448f2020-05-26 14:51:06 -07006354 if (kvm_state->hdr.vmx.flags & KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE) {
6355 vmx->nested.has_preemption_timer_deadline = true;
6356 vmx->nested.preemption_timer_deadline =
6357 kvm_state->hdr.vmx.preemption_timer_deadline;
6358 }
6359
Sean Christopherson5478ba32019-04-11 12:18:06 -07006360 if (nested_vmx_check_controls(vcpu, vmcs12) ||
6361 nested_vmx_check_host_state(vcpu, vmcs12) ||
Sean Christopherson68cda402020-05-11 15:05:29 -07006362 nested_vmx_check_guest_state(vcpu, vmcs12, &ignored))
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006363 goto error_guest_mode;
Sean Christopherson55d23752018-12-03 13:53:18 -08006364
6365 vmx->nested.dirty_vmcs12 = true;
6366 ret = nested_vmx_enter_non_root_mode(vcpu, false);
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006367 if (ret)
6368 goto error_guest_mode;
Sean Christopherson55d23752018-12-03 13:53:18 -08006369
6370 return 0;
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006371
6372error_guest_mode:
6373 vmx->nested.nested_run_pending = 0;
6374 return ret;
Sean Christopherson55d23752018-12-03 13:53:18 -08006375}
6376
Xiaoyao Li1b842922019-10-20 17:11:01 +08006377void nested_vmx_set_vmcs_shadowing_bitmap(void)
Sean Christopherson55d23752018-12-03 13:53:18 -08006378{
6379 if (enable_shadow_vmcs) {
Sean Christopherson55d23752018-12-03 13:53:18 -08006380 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Sean Christophersonfadcead2019-05-07 08:36:23 -07006381 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
Sean Christopherson55d23752018-12-03 13:53:18 -08006382 }
6383}
6384
6385/*
Sean Christophersonba1f8242021-06-18 14:46:58 -07006386 * Indexing into the vmcs12 uses the VMCS encoding rotated left by 6. Undo
6387 * that madness to get the encoding for comparison.
6388 */
6389#define VMCS12_IDX_TO_ENC(idx) ((u16)(((u16)(idx) >> 6) | ((u16)(idx) << 10)))
6390
6391static u64 nested_vmx_calc_vmcs_enum_msr(void)
6392{
6393 /*
6394 * Note these are the so called "index" of the VMCS field encoding, not
6395 * the index into vmcs12.
6396 */
6397 unsigned int max_idx, idx;
6398 int i;
6399
6400 /*
6401 * For better or worse, KVM allows VMREAD/VMWRITE to all fields in
6402 * vmcs12, regardless of whether or not the associated feature is
6403 * exposed to L1. Simply find the field with the highest index.
6404 */
6405 max_idx = 0;
6406 for (i = 0; i < nr_vmcs12_fields; i++) {
6407 /* The vmcs12 table is very, very sparsely populated. */
6408 if (!vmcs_field_to_offset_table[i])
6409 continue;
6410
6411 idx = vmcs_field_index(VMCS12_IDX_TO_ENC(i));
6412 if (idx > max_idx)
6413 max_idx = idx;
6414 }
6415
6416 return (u64)max_idx << VMCS_FIELD_INDEX_SHIFT;
6417}
6418
6419/*
Sean Christopherson55d23752018-12-03 13:53:18 -08006420 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
6421 * returned for the various VMX controls MSRs when nested VMX is enabled.
6422 * The same values should also be used to verify that vmcs12 control fields are
6423 * valid during nested entry from L1 to L2.
6424 * Each of these control msrs has a low and high 32-bit half: A low bit is on
6425 * if the corresponding bit in the (32-bit) control field *must* be on, and a
6426 * bit in the high half is on if the corresponding bit in the control field
6427 * may be on. See also vmx_control_verify().
6428 */
Vitaly Kuznetsova4443262020-02-20 18:22:04 +01006429void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, u32 ept_caps)
Sean Christopherson55d23752018-12-03 13:53:18 -08006430{
6431 /*
6432 * Note that as a general rule, the high half of the MSRs (bits in
6433 * the control fields which may be 1) should be initialized by the
6434 * intersection of the underlying hardware's MSR (i.e., features which
6435 * can be supported) and the list of features we want to expose -
6436 * because they are known to be properly supported in our code.
6437 * Also, usually, the low half of the MSRs (bits which must be 1) can
6438 * be set to 0, meaning that L1 may turn off any of these bits. The
6439 * reason is that if one of these bits is necessary, it will appear
6440 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
6441 * fields of vmcs01 and vmcs02, will turn these bits off - and
Sean Christopherson2c1f3322020-04-15 10:55:14 -07006442 * nested_vmx_l1_wants_exit() will not pass related exits to L1.
Sean Christopherson55d23752018-12-03 13:53:18 -08006443 * These rules have exceptions below.
6444 */
6445
6446 /* pin-based controls */
6447 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
6448 msrs->pinbased_ctls_low,
6449 msrs->pinbased_ctls_high);
6450 msrs->pinbased_ctls_low |=
6451 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
6452 msrs->pinbased_ctls_high &=
6453 PIN_BASED_EXT_INTR_MASK |
6454 PIN_BASED_NMI_EXITING |
6455 PIN_BASED_VIRTUAL_NMIS |
Vitaly Kuznetsova4443262020-02-20 18:22:04 +01006456 (enable_apicv ? PIN_BASED_POSTED_INTR : 0);
Sean Christopherson55d23752018-12-03 13:53:18 -08006457 msrs->pinbased_ctls_high |=
6458 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
6459 PIN_BASED_VMX_PREEMPTION_TIMER;
6460
6461 /* exit controls */
6462 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
6463 msrs->exit_ctls_low,
6464 msrs->exit_ctls_high);
6465 msrs->exit_ctls_low =
6466 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
6467
6468 msrs->exit_ctls_high &=
6469#ifdef CONFIG_X86_64
6470 VM_EXIT_HOST_ADDR_SPACE_SIZE |
6471#endif
Chenyi Qiangefc83132020-08-28 16:56:18 +08006472 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT |
6473 VM_EXIT_CLEAR_BNDCFGS | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
Sean Christopherson55d23752018-12-03 13:53:18 -08006474 msrs->exit_ctls_high |=
6475 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
6476 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
6477 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
6478
6479 /* We support free control of debug control saving. */
6480 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
6481
6482 /* entry controls */
6483 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
6484 msrs->entry_ctls_low,
6485 msrs->entry_ctls_high);
6486 msrs->entry_ctls_low =
6487 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
6488 msrs->entry_ctls_high &=
6489#ifdef CONFIG_X86_64
6490 VM_ENTRY_IA32E_MODE |
6491#endif
Chenyi Qiangefc83132020-08-28 16:56:18 +08006492 VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS |
6493 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
Sean Christopherson55d23752018-12-03 13:53:18 -08006494 msrs->entry_ctls_high |=
6495 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
6496
6497 /* We support free control of debug control loading. */
6498 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
6499
6500 /* cpu-based controls */
6501 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
6502 msrs->procbased_ctls_low,
6503 msrs->procbased_ctls_high);
6504 msrs->procbased_ctls_low =
6505 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
6506 msrs->procbased_ctls_high &=
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08006507 CPU_BASED_INTR_WINDOW_EXITING |
Xiaoyao Li5e3d3942019-12-06 16:45:26 +08006508 CPU_BASED_NMI_WINDOW_EXITING | CPU_BASED_USE_TSC_OFFSETTING |
Sean Christopherson55d23752018-12-03 13:53:18 -08006509 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
6510 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
6511 CPU_BASED_CR3_STORE_EXITING |
6512#ifdef CONFIG_X86_64
6513 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
6514#endif
6515 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
6516 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
6517 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
6518 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
6519 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
6520 /*
6521 * We can allow some features even when not supported by the
6522 * hardware. For example, L1 can specify an MSR bitmap - and we
6523 * can use it to avoid exits to L1 - even when L0 runs L2
6524 * without MSR bitmaps.
6525 */
6526 msrs->procbased_ctls_high |=
6527 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
6528 CPU_BASED_USE_MSR_BITMAPS;
6529
6530 /* We support free control of CR3 access interception. */
6531 msrs->procbased_ctls_low &=
6532 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
6533
6534 /*
6535 * secondary cpu-based controls. Do not include those that
Xiaoyao Li7c1b7612020-07-09 12:34:25 +08006536 * depend on CPUID bits, they are added later by
6537 * vmx_vcpu_after_set_cpuid.
Sean Christopherson55d23752018-12-03 13:53:18 -08006538 */
Vitaly Kuznetsov6b1971c2019-02-07 11:42:14 +01006539 if (msrs->procbased_ctls_high & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)
6540 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
6541 msrs->secondary_ctls_low,
6542 msrs->secondary_ctls_high);
6543
Sean Christopherson55d23752018-12-03 13:53:18 -08006544 msrs->secondary_ctls_low = 0;
6545 msrs->secondary_ctls_high &=
6546 SECONDARY_EXEC_DESC |
Sean Christopherson7f3603b2020-09-23 09:50:47 -07006547 SECONDARY_EXEC_ENABLE_RDTSCP |
Sean Christopherson55d23752018-12-03 13:53:18 -08006548 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini6defc592019-07-02 14:39:29 +02006549 SECONDARY_EXEC_WBINVD_EXITING |
Sean Christopherson55d23752018-12-03 13:53:18 -08006550 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6551 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini6defc592019-07-02 14:39:29 +02006552 SECONDARY_EXEC_RDRAND_EXITING |
6553 SECONDARY_EXEC_ENABLE_INVPCID |
6554 SECONDARY_EXEC_RDSEED_EXITING |
Ilias Stamatisd041b5e2021-05-26 19:44:17 +01006555 SECONDARY_EXEC_XSAVES |
6556 SECONDARY_EXEC_TSC_SCALING;
Sean Christopherson55d23752018-12-03 13:53:18 -08006557
6558 /*
6559 * We can emulate "VMCS shadowing," even if the hardware
6560 * doesn't support it.
6561 */
6562 msrs->secondary_ctls_high |=
6563 SECONDARY_EXEC_SHADOW_VMCS;
6564
6565 if (enable_ept) {
6566 /* nested EPT: emulate EPT also to L1 */
6567 msrs->secondary_ctls_high |=
6568 SECONDARY_EXEC_ENABLE_EPT;
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08006569 msrs->ept_caps =
6570 VMX_EPT_PAGE_WALK_4_BIT |
6571 VMX_EPT_PAGE_WALK_5_BIT |
6572 VMX_EPTP_WB_BIT |
Sean Christopherson96d47012020-03-02 18:02:40 -08006573 VMX_EPT_INVEPT_BIT |
6574 VMX_EPT_EXECUTE_ONLY_BIT;
6575
Sean Christopherson55d23752018-12-03 13:53:18 -08006576 msrs->ept_caps &= ept_caps;
6577 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
6578 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
6579 VMX_EPT_1GB_PAGE_BIT;
6580 if (enable_ept_ad_bits) {
6581 msrs->secondary_ctls_high |=
6582 SECONDARY_EXEC_ENABLE_PML;
6583 msrs->ept_caps |= VMX_EPT_AD_BIT;
6584 }
6585 }
6586
6587 if (cpu_has_vmx_vmfunc()) {
6588 msrs->secondary_ctls_high |=
6589 SECONDARY_EXEC_ENABLE_VMFUNC;
6590 /*
6591 * Advertise EPTP switching unconditionally
6592 * since we emulate it
6593 */
6594 if (enable_ept)
6595 msrs->vmfunc_controls =
6596 VMX_VMFUNC_EPTP_SWITCHING;
6597 }
6598
6599 /*
6600 * Old versions of KVM use the single-context version without
6601 * checking for support, so declare that it is supported even
6602 * though it is treated as global context. The alternative is
6603 * not failing the single-context invvpid, and it is worse.
6604 */
6605 if (enable_vpid) {
6606 msrs->secondary_ctls_high |=
6607 SECONDARY_EXEC_ENABLE_VPID;
6608 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
6609 VMX_VPID_EXTENT_SUPPORTED_MASK;
6610 }
6611
6612 if (enable_unrestricted_guest)
6613 msrs->secondary_ctls_high |=
6614 SECONDARY_EXEC_UNRESTRICTED_GUEST;
6615
6616 if (flexpriority_enabled)
6617 msrs->secondary_ctls_high |=
6618 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6619
Sean Christopherson72add912021-04-12 16:21:42 +12006620 if (enable_sgx)
6621 msrs->secondary_ctls_high |= SECONDARY_EXEC_ENCLS_EXITING;
6622
Sean Christopherson55d23752018-12-03 13:53:18 -08006623 /* miscellaneous data */
6624 rdmsr(MSR_IA32_VMX_MISC,
6625 msrs->misc_low,
6626 msrs->misc_high);
6627 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
6628 msrs->misc_low |=
6629 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
6630 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Yadong Qibf0cd882020-11-06 14:51:22 +08006631 VMX_MISC_ACTIVITY_HLT |
6632 VMX_MISC_ACTIVITY_WAIT_SIPI;
Sean Christopherson55d23752018-12-03 13:53:18 -08006633 msrs->misc_high = 0;
6634
6635 /*
6636 * This MSR reports some information about VMX support. We
6637 * should return information about the VMX we emulate for the
6638 * guest, and the VMCS structure we give it - not about the
6639 * VMX support of the underlying hardware.
6640 */
6641 msrs->basic =
6642 VMCS12_REVISION |
6643 VMX_BASIC_TRUE_CTLS |
6644 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
6645 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
6646
6647 if (cpu_has_vmx_basic_inout())
6648 msrs->basic |= VMX_BASIC_INOUT;
6649
6650 /*
6651 * These MSRs specify bits which the guest must keep fixed on
6652 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
6653 * We picked the standard core2 setting.
6654 */
6655#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
6656#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
6657 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
6658 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
6659
6660 /* These MSRs specify bits which the guest must keep fixed off. */
6661 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
6662 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
6663
Sean Christophersonba1f8242021-06-18 14:46:58 -07006664 msrs->vmcs_enum = nested_vmx_calc_vmcs_enum_msr();
Sean Christopherson55d23752018-12-03 13:53:18 -08006665}
6666
6667void nested_vmx_hardware_unsetup(void)
6668{
6669 int i;
6670
6671 if (enable_shadow_vmcs) {
6672 for (i = 0; i < VMX_BITMAP_NR; i++)
6673 free_page((unsigned long)vmx_bitmap[i]);
6674 }
6675}
6676
Sean Christopherson6c1c6e52020-05-06 13:46:53 -07006677__init int nested_vmx_hardware_setup(int (*exit_handlers[])(struct kvm_vcpu *))
Sean Christopherson55d23752018-12-03 13:53:18 -08006678{
6679 int i;
6680
6681 if (!cpu_has_vmx_shadow_vmcs())
6682 enable_shadow_vmcs = 0;
6683 if (enable_shadow_vmcs) {
6684 for (i = 0; i < VMX_BITMAP_NR; i++) {
Ben Gardon41836832019-02-11 11:02:52 -08006685 /*
6686 * The vmx_bitmap is not tied to a VM and so should
6687 * not be charged to a memcg.
6688 */
Sean Christopherson55d23752018-12-03 13:53:18 -08006689 vmx_bitmap[i] = (unsigned long *)
6690 __get_free_page(GFP_KERNEL);
6691 if (!vmx_bitmap[i]) {
6692 nested_vmx_hardware_unsetup();
6693 return -ENOMEM;
6694 }
6695 }
6696
6697 init_vmcs_shadow_fields();
6698 }
6699
Liran Aloncc877672019-11-18 21:11:21 +02006700 exit_handlers[EXIT_REASON_VMCLEAR] = handle_vmclear;
6701 exit_handlers[EXIT_REASON_VMLAUNCH] = handle_vmlaunch;
6702 exit_handlers[EXIT_REASON_VMPTRLD] = handle_vmptrld;
6703 exit_handlers[EXIT_REASON_VMPTRST] = handle_vmptrst;
6704 exit_handlers[EXIT_REASON_VMREAD] = handle_vmread;
6705 exit_handlers[EXIT_REASON_VMRESUME] = handle_vmresume;
6706 exit_handlers[EXIT_REASON_VMWRITE] = handle_vmwrite;
6707 exit_handlers[EXIT_REASON_VMOFF] = handle_vmoff;
6708 exit_handlers[EXIT_REASON_VMON] = handle_vmon;
6709 exit_handlers[EXIT_REASON_INVEPT] = handle_invept;
6710 exit_handlers[EXIT_REASON_INVVPID] = handle_invvpid;
6711 exit_handlers[EXIT_REASON_VMFUNC] = handle_vmfunc;
Sean Christopherson55d23752018-12-03 13:53:18 -08006712
Sean Christopherson55d23752018-12-03 13:53:18 -08006713 return 0;
6714}
Paolo Bonzini33b22172020-04-17 10:24:18 -04006715
6716struct kvm_x86_nested_ops vmx_nested_ops = {
6717 .check_events = vmx_check_nested_events,
Sean Christophersond2060bd2020-04-22 19:25:39 -07006718 .hv_timer_pending = nested_vmx_preemption_timer_pending,
Sean Christophersoncb6a32c2021-03-02 09:45:14 -08006719 .triple_fault = nested_vmx_triple_fault,
Paolo Bonzini33b22172020-04-17 10:24:18 -04006720 .get_state = vmx_get_nested_state,
6721 .set_state = vmx_set_nested_state,
Paolo Bonzini9a78e152021-01-08 11:43:08 -05006722 .get_nested_state_pages = vmx_get_nested_state_pages,
Sean Christopherson02f5fb22020-06-22 14:58:32 -07006723 .write_log_dirty = nested_vmx_write_pml_buffer,
Paolo Bonzini33b22172020-04-17 10:24:18 -04006724 .enable_evmcs = nested_enable_evmcs,
6725 .get_evmcs_version = nested_get_evmcs_version,
6726};