blob: 775df9e2ff88eae2559e19c741eff8e5b83e8240 [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
354static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
355{
356 WARN_ON(mmu_is_nested(vcpu));
357
358 vcpu->arch.mmu = &vcpu->arch.guest_mmu;
359 kvm_init_shadow_ept_mmu(vcpu,
360 to_vmx(vcpu)->nested.msrs.ept_caps &
361 VMX_EPT_EXECUTE_ONLY_BIT,
362 nested_ept_ad_enabled(vcpu),
Sean Christophersonac69dfa2020-03-02 18:02:37 -0800363 nested_ept_get_eptp(vcpu));
Sean Christophersond8dd54e2020-03-02 18:02:39 -0800364 vcpu->arch.mmu->get_guest_pgd = nested_ept_get_eptp;
Sean Christopherson55d23752018-12-03 13:53:18 -0800365 vcpu->arch.mmu->inject_page_fault = nested_ept_inject_page_fault;
366 vcpu->arch.mmu->get_pdptr = kvm_pdptr_read;
367
368 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
369}
370
371static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
372{
373 vcpu->arch.mmu = &vcpu->arch.root_mmu;
374 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
375}
376
377static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
378 u16 error_code)
379{
380 bool inequality, bit;
381
382 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
383 inequality =
384 (error_code & vmcs12->page_fault_error_code_mask) !=
385 vmcs12->page_fault_error_code_match;
386 return inequality ^ bit;
387}
388
389
390/*
391 * KVM wants to inject page-faults which it got to the guest. This function
392 * checks whether in a nested guest, we need to inject them to L1 or L2.
393 */
394static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
395{
396 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
397 unsigned int nr = vcpu->arch.exception.nr;
398 bool has_payload = vcpu->arch.exception.has_payload;
399 unsigned long payload = vcpu->arch.exception.payload;
400
401 if (nr == PF_VECTOR) {
402 if (vcpu->arch.exception.nested_apf) {
403 *exit_qual = vcpu->arch.apf.nested_apf_token;
404 return 1;
405 }
406 if (nested_vmx_is_page_fault_vmexit(vmcs12,
407 vcpu->arch.exception.error_code)) {
408 *exit_qual = has_payload ? payload : vcpu->arch.cr2;
409 return 1;
410 }
411 } else if (vmcs12->exception_bitmap & (1u << nr)) {
412 if (nr == DB_VECTOR) {
413 if (!has_payload) {
414 payload = vcpu->arch.dr6;
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +0800415 payload &= ~DR6_BT;
416 payload ^= DR6_ACTIVE_LOW;
Sean Christopherson55d23752018-12-03 13:53:18 -0800417 }
418 *exit_qual = payload;
419 } else
420 *exit_qual = 0;
421 return 1;
422 }
423
424 return 0;
425}
426
427
428static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
429 struct x86_exception *fault)
430{
431 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
432
433 WARN_ON(!is_guest_mode(vcpu));
434
435 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
436 !to_vmx(vcpu)->nested.nested_run_pending) {
437 vmcs12->vm_exit_intr_error_code = fault->error_code;
438 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
439 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
440 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
441 fault->address);
442 } else {
443 kvm_inject_page_fault(vcpu, fault);
444 }
445}
446
Sean Christopherson55d23752018-12-03 13:53:18 -0800447static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
448 struct vmcs12 *vmcs12)
449{
450 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
451 return 0;
452
Sean Christopherson5497b952019-07-11 08:58:29 -0700453 if (CC(!page_address_valid(vcpu, vmcs12->io_bitmap_a)) ||
454 CC(!page_address_valid(vcpu, vmcs12->io_bitmap_b)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800455 return -EINVAL;
456
457 return 0;
458}
459
460static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
461 struct vmcs12 *vmcs12)
462{
463 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
464 return 0;
465
Sean Christopherson5497b952019-07-11 08:58:29 -0700466 if (CC(!page_address_valid(vcpu, vmcs12->msr_bitmap)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800467 return -EINVAL;
468
469 return 0;
470}
471
472static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
473 struct vmcs12 *vmcs12)
474{
475 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
476 return 0;
477
Sean Christopherson5497b952019-07-11 08:58:29 -0700478 if (CC(!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800479 return -EINVAL;
480
481 return 0;
482}
483
484/*
485 * Check if MSR is intercepted for L01 MSR bitmap.
486 */
487static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
488{
489 unsigned long *msr_bitmap;
490 int f = sizeof(unsigned long);
491
492 if (!cpu_has_vmx_msr_bitmap())
493 return true;
494
495 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
496
497 if (msr <= 0x1fff) {
498 return !!test_bit(msr, msr_bitmap + 0x800 / f);
499 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
500 msr &= 0x1fff;
501 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
502 }
503
504 return true;
505}
506
507/*
508 * If a msr is allowed by L0, we should check whether it is allowed by L1.
509 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
510 */
511static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
512 unsigned long *msr_bitmap_nested,
513 u32 msr, int type)
514{
515 int f = sizeof(unsigned long);
516
517 /*
518 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
519 * have the write-low and read-high bitmap offsets the wrong way round.
520 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
521 */
522 if (msr <= 0x1fff) {
523 if (type & MSR_TYPE_R &&
524 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
525 /* read-low */
526 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
527
528 if (type & MSR_TYPE_W &&
529 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
530 /* write-low */
531 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
532
533 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
534 msr &= 0x1fff;
535 if (type & MSR_TYPE_R &&
536 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
537 /* read-high */
538 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
539
540 if (type & MSR_TYPE_W &&
541 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
542 /* write-high */
543 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
544
545 }
546}
547
Miaohe Linffdbd502020-02-07 23:22:45 +0800548static inline void enable_x2apic_msr_intercepts(unsigned long *msr_bitmap)
549{
Marc Orracff7842019-04-01 23:55:59 -0700550 int msr;
551
552 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
553 unsigned word = msr / BITS_PER_LONG;
554
555 msr_bitmap[word] = ~0;
556 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
557 }
558}
559
Sean Christopherson55d23752018-12-03 13:53:18 -0800560/*
561 * Merge L0's and L1's MSR bitmap, return false to indicate that
562 * we do not use the hardware.
563 */
564static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
565 struct vmcs12 *vmcs12)
566{
567 int msr;
Sean Christopherson55d23752018-12-03 13:53:18 -0800568 unsigned long *msr_bitmap_l1;
569 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
KarimAllah Ahmed31f0b6c2019-01-31 21:24:36 +0100570 struct kvm_host_map *map = &to_vmx(vcpu)->nested.msr_bitmap_map;
Sean Christopherson55d23752018-12-03 13:53:18 -0800571
572 /* Nothing to do if the MSR bitmap is not in use. */
573 if (!cpu_has_vmx_msr_bitmap() ||
574 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
575 return false;
576
KarimAllah Ahmed31f0b6c2019-01-31 21:24:36 +0100577 if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->msr_bitmap), map))
Sean Christopherson55d23752018-12-03 13:53:18 -0800578 return false;
579
KarimAllah Ahmed31f0b6c2019-01-31 21:24:36 +0100580 msr_bitmap_l1 = (unsigned long *)map->hva;
Sean Christopherson55d23752018-12-03 13:53:18 -0800581
Marc Orracff7842019-04-01 23:55:59 -0700582 /*
583 * To keep the control flow simple, pay eight 8-byte writes (sixteen
584 * 4-byte writes on 32-bit systems) up front to enable intercepts for
585 * the x2APIC MSR range and selectively disable them below.
586 */
587 enable_x2apic_msr_intercepts(msr_bitmap_l0);
Sean Christopherson55d23752018-12-03 13:53:18 -0800588
Marc Orracff7842019-04-01 23:55:59 -0700589 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
590 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
591 /*
592 * L0 need not intercept reads for MSRs between 0x800
593 * and 0x8ff, it just lets the processor take the value
594 * from the virtual-APIC page; take those 256 bits
595 * directly from the L1 bitmap.
596 */
597 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
598 unsigned word = msr / BITS_PER_LONG;
599
600 msr_bitmap_l0[word] = msr_bitmap_l1[word];
601 }
602 }
603
Sean Christopherson55d23752018-12-03 13:53:18 -0800604 nested_vmx_disable_intercept_for_msr(
605 msr_bitmap_l1, msr_bitmap_l0,
Marc Orracff7842019-04-01 23:55:59 -0700606 X2APIC_MSR(APIC_TASKPRI),
Marc Orrc73f4c92019-04-01 23:56:00 -0700607 MSR_TYPE_R | MSR_TYPE_W);
Marc Orracff7842019-04-01 23:55:59 -0700608
609 if (nested_cpu_has_vid(vmcs12)) {
610 nested_vmx_disable_intercept_for_msr(
611 msr_bitmap_l1, msr_bitmap_l0,
612 X2APIC_MSR(APIC_EOI),
613 MSR_TYPE_W);
614 nested_vmx_disable_intercept_for_msr(
615 msr_bitmap_l1, msr_bitmap_l0,
616 X2APIC_MSR(APIC_SELF_IPI),
617 MSR_TYPE_W);
618 }
Sean Christopherson55d23752018-12-03 13:53:18 -0800619 }
620
Sean Christophersond69129b2019-05-08 07:32:15 -0700621 /* KVM unconditionally exposes the FS/GS base MSRs to L1. */
Sean Christophersondbdd0962021-04-21 19:38:31 -0700622#ifdef CONFIG_X86_64
Sean Christophersond69129b2019-05-08 07:32:15 -0700623 nested_vmx_disable_intercept_for_msr(msr_bitmap_l1, msr_bitmap_l0,
624 MSR_FS_BASE, MSR_TYPE_RW);
625
626 nested_vmx_disable_intercept_for_msr(msr_bitmap_l1, msr_bitmap_l0,
627 MSR_GS_BASE, MSR_TYPE_RW);
628
629 nested_vmx_disable_intercept_for_msr(msr_bitmap_l1, msr_bitmap_l0,
630 MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
Sean Christophersondbdd0962021-04-21 19:38:31 -0700631#endif
Sean Christophersond69129b2019-05-08 07:32:15 -0700632
633 /*
634 * Checking the L0->L1 bitmap is trying to verify two things:
635 *
636 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
637 * ensures that we do not accidentally generate an L02 MSR bitmap
638 * from the L12 MSR bitmap that is too permissive.
639 * 2. That L1 or L2s have actually used the MSR. This avoids
640 * unnecessarily merging of the bitmap if the MSR is unused. This
641 * works properly because we only update the L01 MSR bitmap lazily.
642 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
643 * updated to reflect this when L1 (or its L2s) actually write to
644 * the MSR.
645 */
646 if (!msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL))
Sean Christopherson55d23752018-12-03 13:53:18 -0800647 nested_vmx_disable_intercept_for_msr(
648 msr_bitmap_l1, msr_bitmap_l0,
649 MSR_IA32_SPEC_CTRL,
650 MSR_TYPE_R | MSR_TYPE_W);
651
Sean Christophersond69129b2019-05-08 07:32:15 -0700652 if (!msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD))
Sean Christopherson55d23752018-12-03 13:53:18 -0800653 nested_vmx_disable_intercept_for_msr(
654 msr_bitmap_l1, msr_bitmap_l0,
655 MSR_IA32_PRED_CMD,
656 MSR_TYPE_W);
657
KarimAllah Ahmed31f0b6c2019-01-31 21:24:36 +0100658 kvm_vcpu_unmap(vcpu, &to_vmx(vcpu)->nested.msr_bitmap_map, false);
Sean Christopherson55d23752018-12-03 13:53:18 -0800659
660 return true;
661}
662
663static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
664 struct vmcs12 *vmcs12)
665{
KarimAllah Ahmed88925302019-01-31 21:24:41 +0100666 struct kvm_host_map map;
Sean Christopherson55d23752018-12-03 13:53:18 -0800667 struct vmcs12 *shadow;
Sean Christopherson55d23752018-12-03 13:53:18 -0800668
669 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
670 vmcs12->vmcs_link_pointer == -1ull)
671 return;
672
673 shadow = get_shadow_vmcs12(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -0800674
KarimAllah Ahmed88925302019-01-31 21:24:41 +0100675 if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->vmcs_link_pointer), &map))
676 return;
Sean Christopherson55d23752018-12-03 13:53:18 -0800677
KarimAllah Ahmed88925302019-01-31 21:24:41 +0100678 memcpy(shadow, map.hva, VMCS12_SIZE);
679 kvm_vcpu_unmap(vcpu, &map, false);
Sean Christopherson55d23752018-12-03 13:53:18 -0800680}
681
682static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
683 struct vmcs12 *vmcs12)
684{
685 struct vcpu_vmx *vmx = to_vmx(vcpu);
686
687 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
688 vmcs12->vmcs_link_pointer == -1ull)
689 return;
690
691 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
692 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
693}
694
695/*
696 * In nested virtualization, check if L1 has set
697 * VM_EXIT_ACK_INTR_ON_EXIT
698 */
699static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
700{
701 return get_vmcs12(vcpu)->vm_exit_controls &
702 VM_EXIT_ACK_INTR_ON_EXIT;
703}
704
Sean Christopherson55d23752018-12-03 13:53:18 -0800705static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
706 struct vmcs12 *vmcs12)
707{
708 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
Sean Christopherson5497b952019-07-11 08:58:29 -0700709 CC(!page_address_valid(vcpu, vmcs12->apic_access_addr)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800710 return -EINVAL;
711 else
712 return 0;
713}
714
715static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
716 struct vmcs12 *vmcs12)
717{
718 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
719 !nested_cpu_has_apic_reg_virt(vmcs12) &&
720 !nested_cpu_has_vid(vmcs12) &&
721 !nested_cpu_has_posted_intr(vmcs12))
722 return 0;
723
724 /*
725 * If virtualize x2apic mode is enabled,
726 * virtualize apic access must be disabled.
727 */
Sean Christopherson5497b952019-07-11 08:58:29 -0700728 if (CC(nested_cpu_has_virt_x2apic_mode(vmcs12) &&
729 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800730 return -EINVAL;
731
732 /*
733 * If virtual interrupt delivery is enabled,
734 * we must exit on external interrupts.
735 */
Sean Christopherson5497b952019-07-11 08:58:29 -0700736 if (CC(nested_cpu_has_vid(vmcs12) && !nested_exit_on_intr(vcpu)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800737 return -EINVAL;
738
739 /*
740 * bits 15:8 should be zero in posted_intr_nv,
741 * the descriptor address has been already checked
742 * in nested_get_vmcs12_pages.
743 *
744 * bits 5:0 of posted_intr_desc_addr should be zero.
745 */
746 if (nested_cpu_has_posted_intr(vmcs12) &&
Sean Christopherson5497b952019-07-11 08:58:29 -0700747 (CC(!nested_cpu_has_vid(vmcs12)) ||
748 CC(!nested_exit_intr_ack_set(vcpu)) ||
749 CC((vmcs12->posted_intr_nv & 0xff00)) ||
Sean Christopherson636e8b72021-02-03 16:01:10 -0800750 CC(!kvm_vcpu_is_legal_aligned_gpa(vcpu, vmcs12->posted_intr_desc_addr, 64))))
Sean Christopherson55d23752018-12-03 13:53:18 -0800751 return -EINVAL;
752
753 /* tpr shadow is needed by all apicv features. */
Sean Christopherson5497b952019-07-11 08:58:29 -0700754 if (CC(!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800755 return -EINVAL;
756
757 return 0;
758}
759
760static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
Sean Christophersonf9b245e2018-12-12 13:30:08 -0500761 u32 count, u64 addr)
Sean Christopherson55d23752018-12-03 13:53:18 -0800762{
Sean Christopherson55d23752018-12-03 13:53:18 -0800763 if (count == 0)
764 return 0;
Sean Christopherson636e8b72021-02-03 16:01:10 -0800765
766 if (!kvm_vcpu_is_legal_aligned_gpa(vcpu, addr, 16) ||
767 !kvm_vcpu_is_legal_gpa(vcpu, (addr + count * sizeof(struct vmx_msr_entry) - 1)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800768 return -EINVAL;
Sean Christophersonf9b245e2018-12-12 13:30:08 -0500769
Sean Christopherson55d23752018-12-03 13:53:18 -0800770 return 0;
771}
772
Krish Sadhukhan61446ba2018-12-12 13:30:09 -0500773static int nested_vmx_check_exit_msr_switch_controls(struct kvm_vcpu *vcpu,
774 struct vmcs12 *vmcs12)
Sean Christopherson55d23752018-12-03 13:53:18 -0800775{
Sean Christopherson5497b952019-07-11 08:58:29 -0700776 if (CC(nested_vmx_check_msr_switch(vcpu,
777 vmcs12->vm_exit_msr_load_count,
778 vmcs12->vm_exit_msr_load_addr)) ||
779 CC(nested_vmx_check_msr_switch(vcpu,
780 vmcs12->vm_exit_msr_store_count,
781 vmcs12->vm_exit_msr_store_addr)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800782 return -EINVAL;
Sean Christophersonf9b245e2018-12-12 13:30:08 -0500783
Sean Christopherson55d23752018-12-03 13:53:18 -0800784 return 0;
785}
786
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -0500787static int nested_vmx_check_entry_msr_switch_controls(struct kvm_vcpu *vcpu,
788 struct vmcs12 *vmcs12)
Krish Sadhukhan61446ba2018-12-12 13:30:09 -0500789{
Sean Christopherson5497b952019-07-11 08:58:29 -0700790 if (CC(nested_vmx_check_msr_switch(vcpu,
791 vmcs12->vm_entry_msr_load_count,
792 vmcs12->vm_entry_msr_load_addr)))
Krish Sadhukhan61446ba2018-12-12 13:30:09 -0500793 return -EINVAL;
794
795 return 0;
796}
797
Sean Christopherson55d23752018-12-03 13:53:18 -0800798static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
799 struct vmcs12 *vmcs12)
800{
801 if (!nested_cpu_has_pml(vmcs12))
802 return 0;
803
Sean Christopherson5497b952019-07-11 08:58:29 -0700804 if (CC(!nested_cpu_has_ept(vmcs12)) ||
805 CC(!page_address_valid(vcpu, vmcs12->pml_address)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800806 return -EINVAL;
807
808 return 0;
809}
810
811static int nested_vmx_check_unrestricted_guest_controls(struct kvm_vcpu *vcpu,
812 struct vmcs12 *vmcs12)
813{
Sean Christopherson5497b952019-07-11 08:58:29 -0700814 if (CC(nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST) &&
815 !nested_cpu_has_ept(vmcs12)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800816 return -EINVAL;
817 return 0;
818}
819
820static int nested_vmx_check_mode_based_ept_exec_controls(struct kvm_vcpu *vcpu,
821 struct vmcs12 *vmcs12)
822{
Sean Christopherson5497b952019-07-11 08:58:29 -0700823 if (CC(nested_cpu_has2(vmcs12, SECONDARY_EXEC_MODE_BASED_EPT_EXEC) &&
824 !nested_cpu_has_ept(vmcs12)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800825 return -EINVAL;
826 return 0;
827}
828
829static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
830 struct vmcs12 *vmcs12)
831{
832 if (!nested_cpu_has_shadow_vmcs(vmcs12))
833 return 0;
834
Sean Christopherson5497b952019-07-11 08:58:29 -0700835 if (CC(!page_address_valid(vcpu, vmcs12->vmread_bitmap)) ||
836 CC(!page_address_valid(vcpu, vmcs12->vmwrite_bitmap)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800837 return -EINVAL;
838
839 return 0;
840}
841
842static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
843 struct vmx_msr_entry *e)
844{
845 /* x2APIC MSR accesses are not allowed */
Sean Christopherson5497b952019-07-11 08:58:29 -0700846 if (CC(vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8))
Sean Christopherson55d23752018-12-03 13:53:18 -0800847 return -EINVAL;
Sean Christopherson5497b952019-07-11 08:58:29 -0700848 if (CC(e->index == MSR_IA32_UCODE_WRITE) || /* SDM Table 35-2 */
849 CC(e->index == MSR_IA32_UCODE_REV))
Sean Christopherson55d23752018-12-03 13:53:18 -0800850 return -EINVAL;
Sean Christopherson5497b952019-07-11 08:58:29 -0700851 if (CC(e->reserved != 0))
Sean Christopherson55d23752018-12-03 13:53:18 -0800852 return -EINVAL;
853 return 0;
854}
855
856static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
857 struct vmx_msr_entry *e)
858{
Sean Christopherson5497b952019-07-11 08:58:29 -0700859 if (CC(e->index == MSR_FS_BASE) ||
860 CC(e->index == MSR_GS_BASE) ||
861 CC(e->index == MSR_IA32_SMM_MONITOR_CTL) || /* SMM is not supported */
Sean Christopherson55d23752018-12-03 13:53:18 -0800862 nested_vmx_msr_check_common(vcpu, e))
863 return -EINVAL;
864 return 0;
865}
866
867static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
868 struct vmx_msr_entry *e)
869{
Sean Christopherson5497b952019-07-11 08:58:29 -0700870 if (CC(e->index == MSR_IA32_SMBASE) || /* SMM is not supported */
Sean Christopherson55d23752018-12-03 13:53:18 -0800871 nested_vmx_msr_check_common(vcpu, e))
872 return -EINVAL;
873 return 0;
874}
875
Marc Orrf0b51052019-09-17 11:50:57 -0700876static u32 nested_vmx_max_atomic_switch_msrs(struct kvm_vcpu *vcpu)
877{
878 struct vcpu_vmx *vmx = to_vmx(vcpu);
879 u64 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
880 vmx->nested.msrs.misc_high);
881
882 return (vmx_misc_max_msr(vmx_misc) + 1) * VMX_MISC_MSR_LIST_MULTIPLIER;
883}
884
Sean Christopherson55d23752018-12-03 13:53:18 -0800885/*
886 * Load guest's/host's msr at nested entry/exit.
887 * return 0 for success, entry index for failure.
Marc Orrf0b51052019-09-17 11:50:57 -0700888 *
889 * One of the failure modes for MSR load/store is when a list exceeds the
890 * virtual hardware's capacity. To maintain compatibility with hardware inasmuch
891 * as possible, process all valid entries before failing rather than precheck
892 * for a capacity violation.
Sean Christopherson55d23752018-12-03 13:53:18 -0800893 */
894static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
895{
896 u32 i;
897 struct vmx_msr_entry e;
Marc Orrf0b51052019-09-17 11:50:57 -0700898 u32 max_msr_list_size = nested_vmx_max_atomic_switch_msrs(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -0800899
Sean Christopherson55d23752018-12-03 13:53:18 -0800900 for (i = 0; i < count; i++) {
Marc Orrf0b51052019-09-17 11:50:57 -0700901 if (unlikely(i >= max_msr_list_size))
902 goto fail;
903
Sean Christopherson55d23752018-12-03 13:53:18 -0800904 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
905 &e, sizeof(e))) {
906 pr_debug_ratelimited(
907 "%s cannot read MSR entry (%u, 0x%08llx)\n",
908 __func__, i, gpa + i * sizeof(e));
909 goto fail;
910 }
911 if (nested_vmx_load_msr_check(vcpu, &e)) {
912 pr_debug_ratelimited(
913 "%s check failed (%u, 0x%x, 0x%x)\n",
914 __func__, i, e.index, e.reserved);
915 goto fail;
916 }
Sean Christophersonf20935d2019-09-05 14:22:54 -0700917 if (kvm_set_msr(vcpu, e.index, e.value)) {
Sean Christopherson55d23752018-12-03 13:53:18 -0800918 pr_debug_ratelimited(
919 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
920 __func__, i, e.index, e.value);
921 goto fail;
922 }
923 }
924 return 0;
925fail:
Sean Christopherson68cda402020-05-11 15:05:29 -0700926 /* Note, max_msr_list_size is at most 4096, i.e. this can't wrap. */
Sean Christopherson55d23752018-12-03 13:53:18 -0800927 return i + 1;
928}
929
Aaron Lewis662f1d12019-11-07 21:14:39 -0800930static bool nested_vmx_get_vmexit_msr_value(struct kvm_vcpu *vcpu,
931 u32 msr_index,
932 u64 *data)
933{
934 struct vcpu_vmx *vmx = to_vmx(vcpu);
935
936 /*
937 * If the L0 hypervisor stored a more accurate value for the TSC that
938 * does not include the time taken for emulation of the L2->L1
939 * VM-exit in L0, use the more accurate value.
940 */
941 if (msr_index == MSR_IA32_TSC) {
Sean Christophersona128a932020-09-23 11:03:57 -0700942 int i = vmx_find_loadstore_msr_slot(&vmx->msr_autostore.guest,
943 MSR_IA32_TSC);
Aaron Lewis662f1d12019-11-07 21:14:39 -0800944
Sean Christophersona128a932020-09-23 11:03:57 -0700945 if (i >= 0) {
946 u64 val = vmx->msr_autostore.guest.val[i].value;
Aaron Lewis662f1d12019-11-07 21:14:39 -0800947
948 *data = kvm_read_l1_tsc(vcpu, val);
949 return true;
950 }
951 }
952
953 if (kvm_get_msr(vcpu, msr_index, data)) {
954 pr_debug_ratelimited("%s cannot read MSR (0x%x)\n", __func__,
955 msr_index);
956 return false;
957 }
958 return true;
959}
960
Aaron Lewis365d3d52019-11-07 21:14:36 -0800961static bool read_and_check_msr_entry(struct kvm_vcpu *vcpu, u64 gpa, int i,
962 struct vmx_msr_entry *e)
963{
964 if (kvm_vcpu_read_guest(vcpu,
965 gpa + i * sizeof(*e),
966 e, 2 * sizeof(u32))) {
967 pr_debug_ratelimited(
968 "%s cannot read MSR entry (%u, 0x%08llx)\n",
969 __func__, i, gpa + i * sizeof(*e));
970 return false;
971 }
972 if (nested_vmx_store_msr_check(vcpu, e)) {
973 pr_debug_ratelimited(
974 "%s check failed (%u, 0x%x, 0x%x)\n",
975 __func__, i, e->index, e->reserved);
976 return false;
977 }
978 return true;
979}
980
Sean Christopherson55d23752018-12-03 13:53:18 -0800981static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
982{
Sean Christophersonf20935d2019-09-05 14:22:54 -0700983 u64 data;
Sean Christopherson55d23752018-12-03 13:53:18 -0800984 u32 i;
985 struct vmx_msr_entry e;
Marc Orrf0b51052019-09-17 11:50:57 -0700986 u32 max_msr_list_size = nested_vmx_max_atomic_switch_msrs(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -0800987
988 for (i = 0; i < count; i++) {
Marc Orrf0b51052019-09-17 11:50:57 -0700989 if (unlikely(i >= max_msr_list_size))
990 return -EINVAL;
991
Aaron Lewis365d3d52019-11-07 21:14:36 -0800992 if (!read_and_check_msr_entry(vcpu, gpa, i, &e))
Sean Christopherson55d23752018-12-03 13:53:18 -0800993 return -EINVAL;
Aaron Lewis365d3d52019-11-07 21:14:36 -0800994
Aaron Lewis662f1d12019-11-07 21:14:39 -0800995 if (!nested_vmx_get_vmexit_msr_value(vcpu, e.index, &data))
Sean Christopherson55d23752018-12-03 13:53:18 -0800996 return -EINVAL;
Aaron Lewis662f1d12019-11-07 21:14:39 -0800997
Sean Christopherson55d23752018-12-03 13:53:18 -0800998 if (kvm_vcpu_write_guest(vcpu,
999 gpa + i * sizeof(e) +
1000 offsetof(struct vmx_msr_entry, value),
Sean Christophersonf20935d2019-09-05 14:22:54 -07001001 &data, sizeof(data))) {
Sean Christopherson55d23752018-12-03 13:53:18 -08001002 pr_debug_ratelimited(
1003 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Sean Christophersonf20935d2019-09-05 14:22:54 -07001004 __func__, i, e.index, data);
Sean Christopherson55d23752018-12-03 13:53:18 -08001005 return -EINVAL;
1006 }
1007 }
1008 return 0;
1009}
1010
Aaron Lewis662f1d12019-11-07 21:14:39 -08001011static bool nested_msr_store_list_has_msr(struct kvm_vcpu *vcpu, u32 msr_index)
1012{
1013 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1014 u32 count = vmcs12->vm_exit_msr_store_count;
1015 u64 gpa = vmcs12->vm_exit_msr_store_addr;
1016 struct vmx_msr_entry e;
1017 u32 i;
1018
1019 for (i = 0; i < count; i++) {
1020 if (!read_and_check_msr_entry(vcpu, gpa, i, &e))
1021 return false;
1022
1023 if (e.index == msr_index)
1024 return true;
1025 }
1026 return false;
1027}
1028
1029static void prepare_vmx_msr_autostore_list(struct kvm_vcpu *vcpu,
1030 u32 msr_index)
1031{
1032 struct vcpu_vmx *vmx = to_vmx(vcpu);
1033 struct vmx_msrs *autostore = &vmx->msr_autostore.guest;
1034 bool in_vmcs12_store_list;
Sean Christophersona128a932020-09-23 11:03:57 -07001035 int msr_autostore_slot;
Aaron Lewis662f1d12019-11-07 21:14:39 -08001036 bool in_autostore_list;
1037 int last;
1038
Sean Christophersona128a932020-09-23 11:03:57 -07001039 msr_autostore_slot = vmx_find_loadstore_msr_slot(autostore, msr_index);
1040 in_autostore_list = msr_autostore_slot >= 0;
Aaron Lewis662f1d12019-11-07 21:14:39 -08001041 in_vmcs12_store_list = nested_msr_store_list_has_msr(vcpu, msr_index);
1042
1043 if (in_vmcs12_store_list && !in_autostore_list) {
Sean Christophersonce833b22020-09-23 11:03:56 -07001044 if (autostore->nr == MAX_NR_LOADSTORE_MSRS) {
Aaron Lewis662f1d12019-11-07 21:14:39 -08001045 /*
1046 * Emulated VMEntry does not fail here. Instead a less
1047 * accurate value will be returned by
1048 * nested_vmx_get_vmexit_msr_value() using kvm_get_msr()
1049 * instead of reading the value from the vmcs02 VMExit
1050 * MSR-store area.
1051 */
1052 pr_warn_ratelimited(
1053 "Not enough msr entries in msr_autostore. Can't add msr %x\n",
1054 msr_index);
1055 return;
1056 }
1057 last = autostore->nr++;
1058 autostore->val[last].index = msr_index;
1059 } else if (!in_vmcs12_store_list && in_autostore_list) {
1060 last = --autostore->nr;
Sean Christophersona128a932020-09-23 11:03:57 -07001061 autostore->val[msr_autostore_slot] = autostore->val[last];
Aaron Lewis662f1d12019-11-07 21:14:39 -08001062 }
1063}
1064
Sean Christopherson55d23752018-12-03 13:53:18 -08001065/*
Sean Christopherson41fab65e2020-03-20 14:28:29 -07001066 * Returns true if the MMU needs to be sync'd on nested VM-Enter/VM-Exit.
1067 * tl;dr: the MMU needs a sync if L0 is using shadow paging and L1 didn't
1068 * enable VPID for L2 (implying it expects a TLB flush on VMX transitions).
1069 * Here's why.
1070 *
1071 * If EPT is enabled by L0 a sync is never needed:
1072 * - if it is disabled by L1, then L0 is not shadowing L1 or L2 PTEs, there
1073 * cannot be unsync'd SPTEs for either L1 or L2.
1074 *
1075 * - if it is also enabled by L1, then L0 doesn't need to sync on VM-Enter
1076 * VM-Enter as VM-Enter isn't required to invalidate guest-physical mappings
1077 * (irrespective of VPID), i.e. L1 can't rely on the (virtual) CPU to flush
1078 * stale guest-physical mappings for L2 from the TLB. And as above, L0 isn't
1079 * shadowing L1 PTEs so there are no unsync'd SPTEs to sync on VM-Exit.
1080 *
1081 * If EPT is disabled by L0:
1082 * - if VPID is enabled by L1 (for L2), the situation is similar to when L1
1083 * enables EPT: L0 doesn't need to sync as VM-Enter and VM-Exit aren't
1084 * required to invalidate linear mappings (EPT is disabled so there are
1085 * no combined or guest-physical mappings), i.e. L1 can't rely on the
1086 * (virtual) CPU to flush stale linear mappings for either L2 or itself (L1).
1087 *
1088 * - however if VPID is disabled by L1, then a sync is needed as L1 expects all
1089 * linear mappings (EPT is disabled so there are no combined or guest-physical
1090 * mappings) to be invalidated on both VM-Enter and VM-Exit.
1091 *
1092 * Note, this logic is subtly different than nested_has_guest_tlb_tag(), which
1093 * additionally checks that L2 has been assigned a VPID (when EPT is disabled).
1094 * Whether or not L2 has been assigned a VPID by L0 is irrelevant with respect
1095 * to L1's expectations, e.g. L0 needs to invalidate hardware TLB entries if L2
1096 * doesn't have a unique VPID to prevent reusing L1's entries (assuming L1 has
1097 * been assigned a VPID), but L0 doesn't need to do a MMU sync because L1
1098 * doesn't expect stale (virtual) TLB entries to be flushed, i.e. L1 doesn't
1099 * know that L0 will flush the TLB and so L1 will do INVVPID as needed to flush
1100 * stale TLB entries, at which point L0 will sync L2's MMU.
1101 */
1102static bool nested_vmx_transition_mmu_sync(struct kvm_vcpu *vcpu)
1103{
1104 return !enable_ept && !nested_cpu_has_vpid(get_vmcs12(vcpu));
1105}
1106
1107/*
Sean Christophersonea79a752020-02-04 07:32:59 -08001108 * Load guest's/host's cr3 at nested entry/exit. @nested_ept is true if we are
1109 * emulating VM-Entry into a guest with EPT enabled. On failure, the expected
1110 * Exit Qualification (for a VM-Entry consistency check VM-Exit) is assigned to
1111 * @entry_failure_code.
Sean Christopherson55d23752018-12-03 13:53:18 -08001112 */
Maxim Levitsky0f857222021-06-07 12:02:00 +03001113static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,
1114 bool nested_ept, bool reload_pdptrs,
Sean Christopherson68cda402020-05-11 15:05:29 -07001115 enum vm_entry_failure_code *entry_failure_code)
Sean Christopherson55d23752018-12-03 13:53:18 -08001116{
Sean Christopherson636e8b72021-02-03 16:01:10 -08001117 if (CC(kvm_vcpu_is_illegal_gpa(vcpu, cr3))) {
Sean Christopherson0cc69202020-05-01 21:32:26 -07001118 *entry_failure_code = ENTRY_FAIL_DEFAULT;
1119 return -EINVAL;
1120 }
Sean Christopherson55d23752018-12-03 13:53:18 -08001121
Sean Christopherson0cc69202020-05-01 21:32:26 -07001122 /*
1123 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
1124 * must not be dereferenced.
1125 */
Maxim Levitsky0f857222021-06-07 12:02:00 +03001126 if (reload_pdptrs && !nested_ept && is_pae_paging(vcpu) &&
Sean Christophersonbcb72d02021-06-07 12:01:56 +03001127 CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))) {
1128 *entry_failure_code = ENTRY_FAIL_PDPTE;
1129 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08001130 }
1131
Sean Christopherson41fab65e2020-03-20 14:28:29 -07001132 /*
Sean Christopherson9805c5f2020-03-20 14:28:30 -07001133 * Unconditionally skip the TLB flush on fast CR3 switch, all TLB
Sean Christopherson07ffaf32021-06-09 16:42:21 -07001134 * flushes are handled by nested_vmx_transition_tlb_flush().
Sean Christopherson41fab65e2020-03-20 14:28:29 -07001135 */
Sean Christopherson07ffaf32021-06-09 16:42:21 -07001136 if (!nested_ept) {
1137 kvm_mmu_new_pgd(vcpu, cr3, true, true);
1138
1139 /*
1140 * A TLB flush on VM-Enter/VM-Exit flushes all linear mappings
1141 * across all PCIDs, i.e. all PGDs need to be synchronized.
1142 * See nested_vmx_transition_mmu_sync() for more details.
1143 */
1144 if (nested_vmx_transition_mmu_sync(vcpu))
1145 kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu);
1146 }
Sean Christopherson55d23752018-12-03 13:53:18 -08001147
1148 vcpu->arch.cr3 = cr3;
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07001149 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Sean Christopherson55d23752018-12-03 13:53:18 -08001150
1151 kvm_init_mmu(vcpu, false);
1152
1153 return 0;
1154}
1155
1156/*
1157 * Returns if KVM is able to config CPU to tag TLB entries
1158 * populated by L2 differently than TLB entries populated
1159 * by L1.
1160 *
Liran Alon992edea2019-11-20 14:24:52 +02001161 * If L0 uses EPT, L1 and L2 run with different EPTP because
1162 * guest_mode is part of kvm_mmu_page_role. Thus, TLB entries
1163 * are tagged with different EPTP.
Sean Christopherson55d23752018-12-03 13:53:18 -08001164 *
1165 * If L1 uses VPID and we allocated a vpid02, TLB entries are tagged
1166 * with different VPID (L1 entries are tagged with vmx->vpid
1167 * while L2 entries are tagged with vmx->nested.vpid02).
1168 */
1169static bool nested_has_guest_tlb_tag(struct kvm_vcpu *vcpu)
1170{
1171 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1172
Liran Alon992edea2019-11-20 14:24:52 +02001173 return enable_ept ||
Sean Christopherson55d23752018-12-03 13:53:18 -08001174 (nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02);
1175}
1176
Sean Christopherson50b265a2020-03-20 14:28:19 -07001177static void nested_vmx_transition_tlb_flush(struct kvm_vcpu *vcpu,
1178 struct vmcs12 *vmcs12,
1179 bool is_vmenter)
1180{
1181 struct vcpu_vmx *vmx = to_vmx(vcpu);
1182
1183 /*
1184 * If VPID is disabled, linear and combined mappings are flushed on
1185 * VM-Enter/VM-Exit, and guest-physical mappings are valid only for
1186 * their associated EPTP.
1187 */
1188 if (!enable_vpid)
1189 return;
1190
1191 /*
1192 * If vmcs12 doesn't use VPID, L1 expects linear and combined mappings
1193 * for *all* contexts to be flushed on VM-Enter/VM-Exit.
1194 *
1195 * If VPID is enabled and used by vmc12, but L2 does not have a unique
1196 * TLB tag (ASID), i.e. EPT is disabled and KVM was unable to allocate
Sean Christophersonc51e1ff2020-03-20 14:28:22 -07001197 * a VPID for L2, flush the current context as the effective ASID is
1198 * common to both L1 and L2.
Sean Christopherson50b265a2020-03-20 14:28:19 -07001199 *
1200 * Defer the flush so that it runs after vmcs02.EPTP has been set by
1201 * KVM_REQ_LOAD_MMU_PGD (if nested EPT is enabled) and to avoid
1202 * redundant flushes further down the nested pipeline.
1203 *
1204 * If a TLB flush isn't required due to any of the above, and vpid12 is
1205 * changing then the new "virtual" VPID (vpid12) will reuse the same
1206 * "real" VPID (vpid02), and so needs to be sync'd. There is no direct
1207 * mapping between vpid02 and vpid12, vpid02 is per-vCPU and reused for
1208 * all nested vCPUs.
1209 */
Sean Christophersonc51e1ff2020-03-20 14:28:22 -07001210 if (!nested_cpu_has_vpid(vmcs12)) {
Sean Christopherson50b265a2020-03-20 14:28:19 -07001211 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Sean Christophersonc51e1ff2020-03-20 14:28:22 -07001212 } else if (!nested_has_guest_tlb_tag(vcpu)) {
1213 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
Sean Christopherson50b265a2020-03-20 14:28:19 -07001214 } else if (is_vmenter &&
1215 vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
1216 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
1217 vpid_sync_context(nested_get_vpid02(vcpu));
1218 }
1219}
1220
Sean Christopherson55d23752018-12-03 13:53:18 -08001221static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
1222{
1223 superset &= mask;
1224 subset &= mask;
1225
1226 return (superset | subset) == superset;
1227}
1228
1229static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
1230{
1231 const u64 feature_and_reserved =
1232 /* feature (except bit 48; see below) */
1233 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
1234 /* reserved */
1235 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
1236 u64 vmx_basic = vmx->nested.msrs.basic;
1237
1238 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
1239 return -EINVAL;
1240
1241 /*
1242 * KVM does not emulate a version of VMX that constrains physical
1243 * addresses of VMX structures (e.g. VMCS) to 32-bits.
1244 */
1245 if (data & BIT_ULL(48))
1246 return -EINVAL;
1247
1248 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
1249 vmx_basic_vmcs_revision_id(data))
1250 return -EINVAL;
1251
1252 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
1253 return -EINVAL;
1254
1255 vmx->nested.msrs.basic = data;
1256 return 0;
1257}
1258
1259static int
1260vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
1261{
1262 u64 supported;
1263 u32 *lowp, *highp;
1264
1265 switch (msr_index) {
1266 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
1267 lowp = &vmx->nested.msrs.pinbased_ctls_low;
1268 highp = &vmx->nested.msrs.pinbased_ctls_high;
1269 break;
1270 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
1271 lowp = &vmx->nested.msrs.procbased_ctls_low;
1272 highp = &vmx->nested.msrs.procbased_ctls_high;
1273 break;
1274 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
1275 lowp = &vmx->nested.msrs.exit_ctls_low;
1276 highp = &vmx->nested.msrs.exit_ctls_high;
1277 break;
1278 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
1279 lowp = &vmx->nested.msrs.entry_ctls_low;
1280 highp = &vmx->nested.msrs.entry_ctls_high;
1281 break;
1282 case MSR_IA32_VMX_PROCBASED_CTLS2:
1283 lowp = &vmx->nested.msrs.secondary_ctls_low;
1284 highp = &vmx->nested.msrs.secondary_ctls_high;
1285 break;
1286 default:
1287 BUG();
1288 }
1289
1290 supported = vmx_control_msr(*lowp, *highp);
1291
1292 /* Check must-be-1 bits are still 1. */
1293 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
1294 return -EINVAL;
1295
1296 /* Check must-be-0 bits are still 0. */
1297 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
1298 return -EINVAL;
1299
1300 *lowp = data;
1301 *highp = data >> 32;
1302 return 0;
1303}
1304
1305static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
1306{
1307 const u64 feature_and_reserved_bits =
1308 /* feature */
1309 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
1310 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
1311 /* reserved */
1312 GENMASK_ULL(13, 9) | BIT_ULL(31);
1313 u64 vmx_misc;
1314
1315 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
1316 vmx->nested.msrs.misc_high);
1317
1318 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
1319 return -EINVAL;
1320
1321 if ((vmx->nested.msrs.pinbased_ctls_high &
1322 PIN_BASED_VMX_PREEMPTION_TIMER) &&
1323 vmx_misc_preemption_timer_rate(data) !=
1324 vmx_misc_preemption_timer_rate(vmx_misc))
1325 return -EINVAL;
1326
1327 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
1328 return -EINVAL;
1329
1330 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
1331 return -EINVAL;
1332
1333 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
1334 return -EINVAL;
1335
1336 vmx->nested.msrs.misc_low = data;
1337 vmx->nested.msrs.misc_high = data >> 32;
1338
Sean Christopherson55d23752018-12-03 13:53:18 -08001339 return 0;
1340}
1341
1342static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
1343{
1344 u64 vmx_ept_vpid_cap;
1345
1346 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
1347 vmx->nested.msrs.vpid_caps);
1348
1349 /* Every bit is either reserved or a feature bit. */
1350 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
1351 return -EINVAL;
1352
1353 vmx->nested.msrs.ept_caps = data;
1354 vmx->nested.msrs.vpid_caps = data >> 32;
1355 return 0;
1356}
1357
1358static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
1359{
1360 u64 *msr;
1361
1362 switch (msr_index) {
1363 case MSR_IA32_VMX_CR0_FIXED0:
1364 msr = &vmx->nested.msrs.cr0_fixed0;
1365 break;
1366 case MSR_IA32_VMX_CR4_FIXED0:
1367 msr = &vmx->nested.msrs.cr4_fixed0;
1368 break;
1369 default:
1370 BUG();
1371 }
1372
1373 /*
1374 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
1375 * must be 1 in the restored value.
1376 */
1377 if (!is_bitwise_subset(data, *msr, -1ULL))
1378 return -EINVAL;
1379
1380 *msr = data;
1381 return 0;
1382}
1383
1384/*
1385 * Called when userspace is restoring VMX MSRs.
1386 *
1387 * Returns 0 on success, non-0 otherwise.
1388 */
1389int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1390{
1391 struct vcpu_vmx *vmx = to_vmx(vcpu);
1392
1393 /*
1394 * Don't allow changes to the VMX capability MSRs while the vCPU
1395 * is in VMX operation.
1396 */
1397 if (vmx->nested.vmxon)
1398 return -EBUSY;
1399
1400 switch (msr_index) {
1401 case MSR_IA32_VMX_BASIC:
1402 return vmx_restore_vmx_basic(vmx, data);
1403 case MSR_IA32_VMX_PINBASED_CTLS:
1404 case MSR_IA32_VMX_PROCBASED_CTLS:
1405 case MSR_IA32_VMX_EXIT_CTLS:
1406 case MSR_IA32_VMX_ENTRY_CTLS:
1407 /*
1408 * The "non-true" VMX capability MSRs are generated from the
1409 * "true" MSRs, so we do not support restoring them directly.
1410 *
1411 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
1412 * should restore the "true" MSRs with the must-be-1 bits
1413 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
1414 * DEFAULT SETTINGS".
1415 */
1416 return -EINVAL;
1417 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
1418 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
1419 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
1420 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
1421 case MSR_IA32_VMX_PROCBASED_CTLS2:
1422 return vmx_restore_control_msr(vmx, msr_index, data);
1423 case MSR_IA32_VMX_MISC:
1424 return vmx_restore_vmx_misc(vmx, data);
1425 case MSR_IA32_VMX_CR0_FIXED0:
1426 case MSR_IA32_VMX_CR4_FIXED0:
1427 return vmx_restore_fixed0_msr(vmx, msr_index, data);
1428 case MSR_IA32_VMX_CR0_FIXED1:
1429 case MSR_IA32_VMX_CR4_FIXED1:
1430 /*
1431 * These MSRs are generated based on the vCPU's CPUID, so we
1432 * do not support restoring them directly.
1433 */
1434 return -EINVAL;
1435 case MSR_IA32_VMX_EPT_VPID_CAP:
1436 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
1437 case MSR_IA32_VMX_VMCS_ENUM:
1438 vmx->nested.msrs.vmcs_enum = data;
1439 return 0;
Paolo Bonzinie8a70bd2019-07-02 14:40:40 +02001440 case MSR_IA32_VMX_VMFUNC:
1441 if (data & ~vmx->nested.msrs.vmfunc_controls)
1442 return -EINVAL;
1443 vmx->nested.msrs.vmfunc_controls = data;
1444 return 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08001445 default:
1446 /*
1447 * The rest of the VMX capability MSRs do not support restore.
1448 */
1449 return -EINVAL;
1450 }
1451}
1452
1453/* Returns 0 on success, non-0 otherwise. */
1454int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
1455{
1456 switch (msr_index) {
1457 case MSR_IA32_VMX_BASIC:
1458 *pdata = msrs->basic;
1459 break;
1460 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
1461 case MSR_IA32_VMX_PINBASED_CTLS:
1462 *pdata = vmx_control_msr(
1463 msrs->pinbased_ctls_low,
1464 msrs->pinbased_ctls_high);
1465 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
1466 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
1467 break;
1468 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
1469 case MSR_IA32_VMX_PROCBASED_CTLS:
1470 *pdata = vmx_control_msr(
1471 msrs->procbased_ctls_low,
1472 msrs->procbased_ctls_high);
1473 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
1474 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
1475 break;
1476 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
1477 case MSR_IA32_VMX_EXIT_CTLS:
1478 *pdata = vmx_control_msr(
1479 msrs->exit_ctls_low,
1480 msrs->exit_ctls_high);
1481 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
1482 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
1483 break;
1484 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
1485 case MSR_IA32_VMX_ENTRY_CTLS:
1486 *pdata = vmx_control_msr(
1487 msrs->entry_ctls_low,
1488 msrs->entry_ctls_high);
1489 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
1490 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
1491 break;
1492 case MSR_IA32_VMX_MISC:
1493 *pdata = vmx_control_msr(
1494 msrs->misc_low,
1495 msrs->misc_high);
1496 break;
1497 case MSR_IA32_VMX_CR0_FIXED0:
1498 *pdata = msrs->cr0_fixed0;
1499 break;
1500 case MSR_IA32_VMX_CR0_FIXED1:
1501 *pdata = msrs->cr0_fixed1;
1502 break;
1503 case MSR_IA32_VMX_CR4_FIXED0:
1504 *pdata = msrs->cr4_fixed0;
1505 break;
1506 case MSR_IA32_VMX_CR4_FIXED1:
1507 *pdata = msrs->cr4_fixed1;
1508 break;
1509 case MSR_IA32_VMX_VMCS_ENUM:
1510 *pdata = msrs->vmcs_enum;
1511 break;
1512 case MSR_IA32_VMX_PROCBASED_CTLS2:
1513 *pdata = vmx_control_msr(
1514 msrs->secondary_ctls_low,
1515 msrs->secondary_ctls_high);
1516 break;
1517 case MSR_IA32_VMX_EPT_VPID_CAP:
1518 *pdata = msrs->ept_caps |
1519 ((u64)msrs->vpid_caps << 32);
1520 break;
1521 case MSR_IA32_VMX_VMFUNC:
1522 *pdata = msrs->vmfunc_controls;
1523 break;
1524 default:
1525 return 1;
1526 }
1527
1528 return 0;
1529}
1530
1531/*
Sean Christophersonfadcead2019-05-07 08:36:23 -07001532 * Copy the writable VMCS shadow fields back to the VMCS12, in case they have
1533 * been modified by the L1 guest. Note, "writable" in this context means
1534 * "writable by the guest", i.e. tagged SHADOW_FIELD_RW; the set of
1535 * fields tagged SHADOW_FIELD_RO may or may not align with the "read-only"
1536 * VM-exit information fields (which are actually writable if the vCPU is
1537 * configured to support "VMWRITE to any supported field in the VMCS").
Sean Christopherson55d23752018-12-03 13:53:18 -08001538 */
1539static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
1540{
Sean Christopherson55d23752018-12-03 13:53:18 -08001541 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Sean Christophersonfadcead2019-05-07 08:36:23 -07001542 struct vmcs12 *vmcs12 = get_vmcs12(&vmx->vcpu);
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07001543 struct shadow_vmcs_field field;
1544 unsigned long val;
Sean Christophersonfadcead2019-05-07 08:36:23 -07001545 int i;
Sean Christopherson55d23752018-12-03 13:53:18 -08001546
Paolo Bonzini88dddc12019-07-19 18:41:10 +02001547 if (WARN_ON(!shadow_vmcs))
1548 return;
1549
Sean Christopherson55d23752018-12-03 13:53:18 -08001550 preempt_disable();
1551
1552 vmcs_load(shadow_vmcs);
1553
Sean Christophersonfadcead2019-05-07 08:36:23 -07001554 for (i = 0; i < max_shadow_read_write_fields; i++) {
1555 field = shadow_read_write_fields[i];
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07001556 val = __vmcs_readl(field.encoding);
1557 vmcs12_write_any(vmcs12, field.encoding, field.offset, val);
Sean Christopherson55d23752018-12-03 13:53:18 -08001558 }
1559
1560 vmcs_clear(shadow_vmcs);
1561 vmcs_load(vmx->loaded_vmcs->vmcs);
1562
1563 preempt_enable();
1564}
1565
1566static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
1567{
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07001568 const struct shadow_vmcs_field *fields[] = {
Sean Christopherson55d23752018-12-03 13:53:18 -08001569 shadow_read_write_fields,
1570 shadow_read_only_fields
1571 };
1572 const int max_fields[] = {
1573 max_shadow_read_write_fields,
1574 max_shadow_read_only_fields
1575 };
Sean Christopherson55d23752018-12-03 13:53:18 -08001576 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07001577 struct vmcs12 *vmcs12 = get_vmcs12(&vmx->vcpu);
1578 struct shadow_vmcs_field field;
1579 unsigned long val;
1580 int i, q;
Sean Christopherson55d23752018-12-03 13:53:18 -08001581
Paolo Bonzini88dddc12019-07-19 18:41:10 +02001582 if (WARN_ON(!shadow_vmcs))
1583 return;
1584
Sean Christopherson55d23752018-12-03 13:53:18 -08001585 vmcs_load(shadow_vmcs);
1586
1587 for (q = 0; q < ARRAY_SIZE(fields); q++) {
1588 for (i = 0; i < max_fields[q]; i++) {
1589 field = fields[q][i];
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07001590 val = vmcs12_read_any(vmcs12, field.encoding,
1591 field.offset);
1592 __vmcs_writel(field.encoding, val);
Sean Christopherson55d23752018-12-03 13:53:18 -08001593 }
1594 }
1595
1596 vmcs_clear(shadow_vmcs);
1597 vmcs_load(vmx->loaded_vmcs->vmcs);
1598}
1599
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001600static void copy_enlightened_to_vmcs12(struct vcpu_vmx *vmx, u32 hv_clean_fields)
Sean Christopherson55d23752018-12-03 13:53:18 -08001601{
1602 struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12;
1603 struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
1604
1605 /* HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE */
1606 vmcs12->tpr_threshold = evmcs->tpr_threshold;
1607 vmcs12->guest_rip = evmcs->guest_rip;
1608
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001609 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001610 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC))) {
1611 vmcs12->guest_rsp = evmcs->guest_rsp;
1612 vmcs12->guest_rflags = evmcs->guest_rflags;
1613 vmcs12->guest_interruptibility_info =
1614 evmcs->guest_interruptibility_info;
1615 }
1616
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001617 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001618 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC))) {
1619 vmcs12->cpu_based_vm_exec_control =
1620 evmcs->cpu_based_vm_exec_control;
1621 }
1622
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001623 if (unlikely(!(hv_clean_fields &
Vitaly Kuznetsovf9bc5222019-06-13 13:35:02 +02001624 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EXCPN))) {
Sean Christopherson55d23752018-12-03 13:53:18 -08001625 vmcs12->exception_bitmap = evmcs->exception_bitmap;
1626 }
1627
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001628 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001629 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_ENTRY))) {
1630 vmcs12->vm_entry_controls = evmcs->vm_entry_controls;
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_CONTROL_EVENT))) {
1635 vmcs12->vm_entry_intr_info_field =
1636 evmcs->vm_entry_intr_info_field;
1637 vmcs12->vm_entry_exception_error_code =
1638 evmcs->vm_entry_exception_error_code;
1639 vmcs12->vm_entry_instruction_len =
1640 evmcs->vm_entry_instruction_len;
1641 }
1642
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001643 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001644 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1))) {
1645 vmcs12->host_ia32_pat = evmcs->host_ia32_pat;
1646 vmcs12->host_ia32_efer = evmcs->host_ia32_efer;
1647 vmcs12->host_cr0 = evmcs->host_cr0;
1648 vmcs12->host_cr3 = evmcs->host_cr3;
1649 vmcs12->host_cr4 = evmcs->host_cr4;
1650 vmcs12->host_ia32_sysenter_esp = evmcs->host_ia32_sysenter_esp;
1651 vmcs12->host_ia32_sysenter_eip = evmcs->host_ia32_sysenter_eip;
1652 vmcs12->host_rip = evmcs->host_rip;
1653 vmcs12->host_ia32_sysenter_cs = evmcs->host_ia32_sysenter_cs;
1654 vmcs12->host_es_selector = evmcs->host_es_selector;
1655 vmcs12->host_cs_selector = evmcs->host_cs_selector;
1656 vmcs12->host_ss_selector = evmcs->host_ss_selector;
1657 vmcs12->host_ds_selector = evmcs->host_ds_selector;
1658 vmcs12->host_fs_selector = evmcs->host_fs_selector;
1659 vmcs12->host_gs_selector = evmcs->host_gs_selector;
1660 vmcs12->host_tr_selector = evmcs->host_tr_selector;
1661 }
1662
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001663 if (unlikely(!(hv_clean_fields &
Vitaly Kuznetsovf9bc5222019-06-13 13:35:02 +02001664 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1))) {
Sean Christopherson55d23752018-12-03 13:53:18 -08001665 vmcs12->pin_based_vm_exec_control =
1666 evmcs->pin_based_vm_exec_control;
1667 vmcs12->vm_exit_controls = evmcs->vm_exit_controls;
1668 vmcs12->secondary_vm_exec_control =
1669 evmcs->secondary_vm_exec_control;
1670 }
1671
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001672 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001673 HV_VMX_ENLIGHTENED_CLEAN_FIELD_IO_BITMAP))) {
1674 vmcs12->io_bitmap_a = evmcs->io_bitmap_a;
1675 vmcs12->io_bitmap_b = evmcs->io_bitmap_b;
1676 }
1677
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001678 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001679 HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP))) {
1680 vmcs12->msr_bitmap = evmcs->msr_bitmap;
1681 }
1682
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001683 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001684 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2))) {
1685 vmcs12->guest_es_base = evmcs->guest_es_base;
1686 vmcs12->guest_cs_base = evmcs->guest_cs_base;
1687 vmcs12->guest_ss_base = evmcs->guest_ss_base;
1688 vmcs12->guest_ds_base = evmcs->guest_ds_base;
1689 vmcs12->guest_fs_base = evmcs->guest_fs_base;
1690 vmcs12->guest_gs_base = evmcs->guest_gs_base;
1691 vmcs12->guest_ldtr_base = evmcs->guest_ldtr_base;
1692 vmcs12->guest_tr_base = evmcs->guest_tr_base;
1693 vmcs12->guest_gdtr_base = evmcs->guest_gdtr_base;
1694 vmcs12->guest_idtr_base = evmcs->guest_idtr_base;
1695 vmcs12->guest_es_limit = evmcs->guest_es_limit;
1696 vmcs12->guest_cs_limit = evmcs->guest_cs_limit;
1697 vmcs12->guest_ss_limit = evmcs->guest_ss_limit;
1698 vmcs12->guest_ds_limit = evmcs->guest_ds_limit;
1699 vmcs12->guest_fs_limit = evmcs->guest_fs_limit;
1700 vmcs12->guest_gs_limit = evmcs->guest_gs_limit;
1701 vmcs12->guest_ldtr_limit = evmcs->guest_ldtr_limit;
1702 vmcs12->guest_tr_limit = evmcs->guest_tr_limit;
1703 vmcs12->guest_gdtr_limit = evmcs->guest_gdtr_limit;
1704 vmcs12->guest_idtr_limit = evmcs->guest_idtr_limit;
1705 vmcs12->guest_es_ar_bytes = evmcs->guest_es_ar_bytes;
1706 vmcs12->guest_cs_ar_bytes = evmcs->guest_cs_ar_bytes;
1707 vmcs12->guest_ss_ar_bytes = evmcs->guest_ss_ar_bytes;
1708 vmcs12->guest_ds_ar_bytes = evmcs->guest_ds_ar_bytes;
1709 vmcs12->guest_fs_ar_bytes = evmcs->guest_fs_ar_bytes;
1710 vmcs12->guest_gs_ar_bytes = evmcs->guest_gs_ar_bytes;
1711 vmcs12->guest_ldtr_ar_bytes = evmcs->guest_ldtr_ar_bytes;
1712 vmcs12->guest_tr_ar_bytes = evmcs->guest_tr_ar_bytes;
1713 vmcs12->guest_es_selector = evmcs->guest_es_selector;
1714 vmcs12->guest_cs_selector = evmcs->guest_cs_selector;
1715 vmcs12->guest_ss_selector = evmcs->guest_ss_selector;
1716 vmcs12->guest_ds_selector = evmcs->guest_ds_selector;
1717 vmcs12->guest_fs_selector = evmcs->guest_fs_selector;
1718 vmcs12->guest_gs_selector = evmcs->guest_gs_selector;
1719 vmcs12->guest_ldtr_selector = evmcs->guest_ldtr_selector;
1720 vmcs12->guest_tr_selector = evmcs->guest_tr_selector;
1721 }
1722
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001723 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001724 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2))) {
1725 vmcs12->tsc_offset = evmcs->tsc_offset;
1726 vmcs12->virtual_apic_page_addr = evmcs->virtual_apic_page_addr;
1727 vmcs12->xss_exit_bitmap = evmcs->xss_exit_bitmap;
1728 }
1729
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001730 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001731 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR))) {
1732 vmcs12->cr0_guest_host_mask = evmcs->cr0_guest_host_mask;
1733 vmcs12->cr4_guest_host_mask = evmcs->cr4_guest_host_mask;
1734 vmcs12->cr0_read_shadow = evmcs->cr0_read_shadow;
1735 vmcs12->cr4_read_shadow = evmcs->cr4_read_shadow;
1736 vmcs12->guest_cr0 = evmcs->guest_cr0;
1737 vmcs12->guest_cr3 = evmcs->guest_cr3;
1738 vmcs12->guest_cr4 = evmcs->guest_cr4;
1739 vmcs12->guest_dr7 = evmcs->guest_dr7;
1740 }
1741
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001742 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001743 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER))) {
1744 vmcs12->host_fs_base = evmcs->host_fs_base;
1745 vmcs12->host_gs_base = evmcs->host_gs_base;
1746 vmcs12->host_tr_base = evmcs->host_tr_base;
1747 vmcs12->host_gdtr_base = evmcs->host_gdtr_base;
1748 vmcs12->host_idtr_base = evmcs->host_idtr_base;
1749 vmcs12->host_rsp = evmcs->host_rsp;
1750 }
1751
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001752 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001753 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_XLAT))) {
1754 vmcs12->ept_pointer = evmcs->ept_pointer;
1755 vmcs12->virtual_processor_id = evmcs->virtual_processor_id;
1756 }
1757
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02001758 if (unlikely(!(hv_clean_fields &
Sean Christopherson55d23752018-12-03 13:53:18 -08001759 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1))) {
1760 vmcs12->vmcs_link_pointer = evmcs->vmcs_link_pointer;
1761 vmcs12->guest_ia32_debugctl = evmcs->guest_ia32_debugctl;
1762 vmcs12->guest_ia32_pat = evmcs->guest_ia32_pat;
1763 vmcs12->guest_ia32_efer = evmcs->guest_ia32_efer;
1764 vmcs12->guest_pdptr0 = evmcs->guest_pdptr0;
1765 vmcs12->guest_pdptr1 = evmcs->guest_pdptr1;
1766 vmcs12->guest_pdptr2 = evmcs->guest_pdptr2;
1767 vmcs12->guest_pdptr3 = evmcs->guest_pdptr3;
1768 vmcs12->guest_pending_dbg_exceptions =
1769 evmcs->guest_pending_dbg_exceptions;
1770 vmcs12->guest_sysenter_esp = evmcs->guest_sysenter_esp;
1771 vmcs12->guest_sysenter_eip = evmcs->guest_sysenter_eip;
1772 vmcs12->guest_bndcfgs = evmcs->guest_bndcfgs;
1773 vmcs12->guest_activity_state = evmcs->guest_activity_state;
1774 vmcs12->guest_sysenter_cs = evmcs->guest_sysenter_cs;
1775 }
1776
1777 /*
1778 * Not used?
1779 * vmcs12->vm_exit_msr_store_addr = evmcs->vm_exit_msr_store_addr;
1780 * vmcs12->vm_exit_msr_load_addr = evmcs->vm_exit_msr_load_addr;
1781 * vmcs12->vm_entry_msr_load_addr = evmcs->vm_entry_msr_load_addr;
Sean Christopherson55d23752018-12-03 13:53:18 -08001782 * vmcs12->page_fault_error_code_mask =
1783 * evmcs->page_fault_error_code_mask;
1784 * vmcs12->page_fault_error_code_match =
1785 * evmcs->page_fault_error_code_match;
1786 * vmcs12->cr3_target_count = evmcs->cr3_target_count;
1787 * vmcs12->vm_exit_msr_store_count = evmcs->vm_exit_msr_store_count;
1788 * vmcs12->vm_exit_msr_load_count = evmcs->vm_exit_msr_load_count;
1789 * vmcs12->vm_entry_msr_load_count = evmcs->vm_entry_msr_load_count;
1790 */
1791
1792 /*
1793 * Read only fields:
1794 * vmcs12->guest_physical_address = evmcs->guest_physical_address;
1795 * vmcs12->vm_instruction_error = evmcs->vm_instruction_error;
1796 * vmcs12->vm_exit_reason = evmcs->vm_exit_reason;
1797 * vmcs12->vm_exit_intr_info = evmcs->vm_exit_intr_info;
1798 * vmcs12->vm_exit_intr_error_code = evmcs->vm_exit_intr_error_code;
1799 * vmcs12->idt_vectoring_info_field = evmcs->idt_vectoring_info_field;
1800 * vmcs12->idt_vectoring_error_code = evmcs->idt_vectoring_error_code;
1801 * vmcs12->vm_exit_instruction_len = evmcs->vm_exit_instruction_len;
1802 * vmcs12->vmx_instruction_info = evmcs->vmx_instruction_info;
1803 * vmcs12->exit_qualification = evmcs->exit_qualification;
1804 * vmcs12->guest_linear_address = evmcs->guest_linear_address;
1805 *
1806 * Not present in struct vmcs12:
1807 * vmcs12->exit_io_instruction_ecx = evmcs->exit_io_instruction_ecx;
1808 * vmcs12->exit_io_instruction_esi = evmcs->exit_io_instruction_esi;
1809 * vmcs12->exit_io_instruction_edi = evmcs->exit_io_instruction_edi;
1810 * vmcs12->exit_io_instruction_eip = evmcs->exit_io_instruction_eip;
1811 */
1812
Vitaly Kuznetsov25641ca2021-05-26 15:20:19 +02001813 return;
Sean Christopherson55d23752018-12-03 13:53:18 -08001814}
1815
Vitaly Kuznetsov25641ca2021-05-26 15:20:19 +02001816static void copy_vmcs12_to_enlightened(struct vcpu_vmx *vmx)
Sean Christopherson55d23752018-12-03 13:53:18 -08001817{
1818 struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12;
1819 struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
1820
1821 /*
1822 * Should not be changed by KVM:
1823 *
1824 * evmcs->host_es_selector = vmcs12->host_es_selector;
1825 * evmcs->host_cs_selector = vmcs12->host_cs_selector;
1826 * evmcs->host_ss_selector = vmcs12->host_ss_selector;
1827 * evmcs->host_ds_selector = vmcs12->host_ds_selector;
1828 * evmcs->host_fs_selector = vmcs12->host_fs_selector;
1829 * evmcs->host_gs_selector = vmcs12->host_gs_selector;
1830 * evmcs->host_tr_selector = vmcs12->host_tr_selector;
1831 * evmcs->host_ia32_pat = vmcs12->host_ia32_pat;
1832 * evmcs->host_ia32_efer = vmcs12->host_ia32_efer;
1833 * evmcs->host_cr0 = vmcs12->host_cr0;
1834 * evmcs->host_cr3 = vmcs12->host_cr3;
1835 * evmcs->host_cr4 = vmcs12->host_cr4;
1836 * evmcs->host_ia32_sysenter_esp = vmcs12->host_ia32_sysenter_esp;
1837 * evmcs->host_ia32_sysenter_eip = vmcs12->host_ia32_sysenter_eip;
1838 * evmcs->host_rip = vmcs12->host_rip;
1839 * evmcs->host_ia32_sysenter_cs = vmcs12->host_ia32_sysenter_cs;
1840 * evmcs->host_fs_base = vmcs12->host_fs_base;
1841 * evmcs->host_gs_base = vmcs12->host_gs_base;
1842 * evmcs->host_tr_base = vmcs12->host_tr_base;
1843 * evmcs->host_gdtr_base = vmcs12->host_gdtr_base;
1844 * evmcs->host_idtr_base = vmcs12->host_idtr_base;
1845 * evmcs->host_rsp = vmcs12->host_rsp;
Sean Christopherson3731905ef2019-05-07 08:36:27 -07001846 * sync_vmcs02_to_vmcs12() doesn't read these:
Sean Christopherson55d23752018-12-03 13:53:18 -08001847 * evmcs->io_bitmap_a = vmcs12->io_bitmap_a;
1848 * evmcs->io_bitmap_b = vmcs12->io_bitmap_b;
1849 * evmcs->msr_bitmap = vmcs12->msr_bitmap;
1850 * evmcs->ept_pointer = vmcs12->ept_pointer;
1851 * evmcs->xss_exit_bitmap = vmcs12->xss_exit_bitmap;
1852 * evmcs->vm_exit_msr_store_addr = vmcs12->vm_exit_msr_store_addr;
1853 * evmcs->vm_exit_msr_load_addr = vmcs12->vm_exit_msr_load_addr;
1854 * evmcs->vm_entry_msr_load_addr = vmcs12->vm_entry_msr_load_addr;
Sean Christopherson55d23752018-12-03 13:53:18 -08001855 * evmcs->tpr_threshold = vmcs12->tpr_threshold;
1856 * evmcs->virtual_processor_id = vmcs12->virtual_processor_id;
1857 * evmcs->exception_bitmap = vmcs12->exception_bitmap;
1858 * evmcs->vmcs_link_pointer = vmcs12->vmcs_link_pointer;
1859 * evmcs->pin_based_vm_exec_control = vmcs12->pin_based_vm_exec_control;
1860 * evmcs->vm_exit_controls = vmcs12->vm_exit_controls;
1861 * evmcs->secondary_vm_exec_control = vmcs12->secondary_vm_exec_control;
1862 * evmcs->page_fault_error_code_mask =
1863 * vmcs12->page_fault_error_code_mask;
1864 * evmcs->page_fault_error_code_match =
1865 * vmcs12->page_fault_error_code_match;
1866 * evmcs->cr3_target_count = vmcs12->cr3_target_count;
1867 * evmcs->virtual_apic_page_addr = vmcs12->virtual_apic_page_addr;
1868 * evmcs->tsc_offset = vmcs12->tsc_offset;
1869 * evmcs->guest_ia32_debugctl = vmcs12->guest_ia32_debugctl;
1870 * evmcs->cr0_guest_host_mask = vmcs12->cr0_guest_host_mask;
1871 * evmcs->cr4_guest_host_mask = vmcs12->cr4_guest_host_mask;
1872 * evmcs->cr0_read_shadow = vmcs12->cr0_read_shadow;
1873 * evmcs->cr4_read_shadow = vmcs12->cr4_read_shadow;
1874 * evmcs->vm_exit_msr_store_count = vmcs12->vm_exit_msr_store_count;
1875 * evmcs->vm_exit_msr_load_count = vmcs12->vm_exit_msr_load_count;
1876 * evmcs->vm_entry_msr_load_count = vmcs12->vm_entry_msr_load_count;
1877 *
1878 * Not present in struct vmcs12:
1879 * evmcs->exit_io_instruction_ecx = vmcs12->exit_io_instruction_ecx;
1880 * evmcs->exit_io_instruction_esi = vmcs12->exit_io_instruction_esi;
1881 * evmcs->exit_io_instruction_edi = vmcs12->exit_io_instruction_edi;
1882 * evmcs->exit_io_instruction_eip = vmcs12->exit_io_instruction_eip;
1883 */
1884
1885 evmcs->guest_es_selector = vmcs12->guest_es_selector;
1886 evmcs->guest_cs_selector = vmcs12->guest_cs_selector;
1887 evmcs->guest_ss_selector = vmcs12->guest_ss_selector;
1888 evmcs->guest_ds_selector = vmcs12->guest_ds_selector;
1889 evmcs->guest_fs_selector = vmcs12->guest_fs_selector;
1890 evmcs->guest_gs_selector = vmcs12->guest_gs_selector;
1891 evmcs->guest_ldtr_selector = vmcs12->guest_ldtr_selector;
1892 evmcs->guest_tr_selector = vmcs12->guest_tr_selector;
1893
1894 evmcs->guest_es_limit = vmcs12->guest_es_limit;
1895 evmcs->guest_cs_limit = vmcs12->guest_cs_limit;
1896 evmcs->guest_ss_limit = vmcs12->guest_ss_limit;
1897 evmcs->guest_ds_limit = vmcs12->guest_ds_limit;
1898 evmcs->guest_fs_limit = vmcs12->guest_fs_limit;
1899 evmcs->guest_gs_limit = vmcs12->guest_gs_limit;
1900 evmcs->guest_ldtr_limit = vmcs12->guest_ldtr_limit;
1901 evmcs->guest_tr_limit = vmcs12->guest_tr_limit;
1902 evmcs->guest_gdtr_limit = vmcs12->guest_gdtr_limit;
1903 evmcs->guest_idtr_limit = vmcs12->guest_idtr_limit;
1904
1905 evmcs->guest_es_ar_bytes = vmcs12->guest_es_ar_bytes;
1906 evmcs->guest_cs_ar_bytes = vmcs12->guest_cs_ar_bytes;
1907 evmcs->guest_ss_ar_bytes = vmcs12->guest_ss_ar_bytes;
1908 evmcs->guest_ds_ar_bytes = vmcs12->guest_ds_ar_bytes;
1909 evmcs->guest_fs_ar_bytes = vmcs12->guest_fs_ar_bytes;
1910 evmcs->guest_gs_ar_bytes = vmcs12->guest_gs_ar_bytes;
1911 evmcs->guest_ldtr_ar_bytes = vmcs12->guest_ldtr_ar_bytes;
1912 evmcs->guest_tr_ar_bytes = vmcs12->guest_tr_ar_bytes;
1913
1914 evmcs->guest_es_base = vmcs12->guest_es_base;
1915 evmcs->guest_cs_base = vmcs12->guest_cs_base;
1916 evmcs->guest_ss_base = vmcs12->guest_ss_base;
1917 evmcs->guest_ds_base = vmcs12->guest_ds_base;
1918 evmcs->guest_fs_base = vmcs12->guest_fs_base;
1919 evmcs->guest_gs_base = vmcs12->guest_gs_base;
1920 evmcs->guest_ldtr_base = vmcs12->guest_ldtr_base;
1921 evmcs->guest_tr_base = vmcs12->guest_tr_base;
1922 evmcs->guest_gdtr_base = vmcs12->guest_gdtr_base;
1923 evmcs->guest_idtr_base = vmcs12->guest_idtr_base;
1924
1925 evmcs->guest_ia32_pat = vmcs12->guest_ia32_pat;
1926 evmcs->guest_ia32_efer = vmcs12->guest_ia32_efer;
1927
1928 evmcs->guest_pdptr0 = vmcs12->guest_pdptr0;
1929 evmcs->guest_pdptr1 = vmcs12->guest_pdptr1;
1930 evmcs->guest_pdptr2 = vmcs12->guest_pdptr2;
1931 evmcs->guest_pdptr3 = vmcs12->guest_pdptr3;
1932
1933 evmcs->guest_pending_dbg_exceptions =
1934 vmcs12->guest_pending_dbg_exceptions;
1935 evmcs->guest_sysenter_esp = vmcs12->guest_sysenter_esp;
1936 evmcs->guest_sysenter_eip = vmcs12->guest_sysenter_eip;
1937
1938 evmcs->guest_activity_state = vmcs12->guest_activity_state;
1939 evmcs->guest_sysenter_cs = vmcs12->guest_sysenter_cs;
1940
1941 evmcs->guest_cr0 = vmcs12->guest_cr0;
1942 evmcs->guest_cr3 = vmcs12->guest_cr3;
1943 evmcs->guest_cr4 = vmcs12->guest_cr4;
1944 evmcs->guest_dr7 = vmcs12->guest_dr7;
1945
1946 evmcs->guest_physical_address = vmcs12->guest_physical_address;
1947
1948 evmcs->vm_instruction_error = vmcs12->vm_instruction_error;
1949 evmcs->vm_exit_reason = vmcs12->vm_exit_reason;
1950 evmcs->vm_exit_intr_info = vmcs12->vm_exit_intr_info;
1951 evmcs->vm_exit_intr_error_code = vmcs12->vm_exit_intr_error_code;
1952 evmcs->idt_vectoring_info_field = vmcs12->idt_vectoring_info_field;
1953 evmcs->idt_vectoring_error_code = vmcs12->idt_vectoring_error_code;
1954 evmcs->vm_exit_instruction_len = vmcs12->vm_exit_instruction_len;
1955 evmcs->vmx_instruction_info = vmcs12->vmx_instruction_info;
1956
1957 evmcs->exit_qualification = vmcs12->exit_qualification;
1958
1959 evmcs->guest_linear_address = vmcs12->guest_linear_address;
1960 evmcs->guest_rsp = vmcs12->guest_rsp;
1961 evmcs->guest_rflags = vmcs12->guest_rflags;
1962
1963 evmcs->guest_interruptibility_info =
1964 vmcs12->guest_interruptibility_info;
1965 evmcs->cpu_based_vm_exec_control = vmcs12->cpu_based_vm_exec_control;
1966 evmcs->vm_entry_controls = vmcs12->vm_entry_controls;
1967 evmcs->vm_entry_intr_info_field = vmcs12->vm_entry_intr_info_field;
1968 evmcs->vm_entry_exception_error_code =
1969 vmcs12->vm_entry_exception_error_code;
1970 evmcs->vm_entry_instruction_len = vmcs12->vm_entry_instruction_len;
1971
1972 evmcs->guest_rip = vmcs12->guest_rip;
1973
1974 evmcs->guest_bndcfgs = vmcs12->guest_bndcfgs;
1975
Vitaly Kuznetsov25641ca2021-05-26 15:20:19 +02001976 return;
Sean Christopherson55d23752018-12-03 13:53:18 -08001977}
1978
1979/*
1980 * This is an equivalent of the nested hypervisor executing the vmptrld
1981 * instruction.
1982 */
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01001983static enum nested_evmptrld_status nested_vmx_handle_enlightened_vmptrld(
1984 struct kvm_vcpu *vcpu, bool from_launch)
Sean Christopherson55d23752018-12-03 13:53:18 -08001985{
1986 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsova21a39c2019-06-28 13:23:32 +02001987 bool evmcs_gpa_changed = false;
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02001988 u64 evmcs_gpa;
Sean Christopherson55d23752018-12-03 13:53:18 -08001989
1990 if (likely(!vmx->nested.enlightened_vmcs_enabled))
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01001991 return EVMPTRLD_DISABLED;
Sean Christopherson55d23752018-12-03 13:53:18 -08001992
Vitaly Kuznetsov02761712021-05-26 15:20:18 +02001993 if (!nested_enlightened_vmentry(vcpu, &evmcs_gpa)) {
1994 nested_release_evmcs(vcpu);
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01001995 return EVMPTRLD_DISABLED;
Vitaly Kuznetsov02761712021-05-26 15:20:18 +02001996 }
Sean Christopherson55d23752018-12-03 13:53:18 -08001997
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02001998 if (unlikely(evmcs_gpa != vmx->nested.hv_evmcs_vmptr)) {
1999 vmx->nested.current_vmptr = -1ull;
Sean Christopherson55d23752018-12-03 13:53:18 -08002000
2001 nested_release_evmcs(vcpu);
2002
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02002003 if (kvm_vcpu_map(vcpu, gpa_to_gfn(evmcs_gpa),
KarimAllah Ahmeddee9c042019-01-31 21:24:42 +01002004 &vmx->nested.hv_evmcs_map))
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01002005 return EVMPTRLD_ERROR;
Sean Christopherson55d23752018-12-03 13:53:18 -08002006
KarimAllah Ahmeddee9c042019-01-31 21:24:42 +01002007 vmx->nested.hv_evmcs = vmx->nested.hv_evmcs_map.hva;
Sean Christopherson55d23752018-12-03 13:53:18 -08002008
2009 /*
2010 * Currently, KVM only supports eVMCS version 1
2011 * (== KVM_EVMCS_VERSION) and thus we expect guest to set this
2012 * value to first u32 field of eVMCS which should specify eVMCS
2013 * VersionNumber.
2014 *
2015 * Guest should be aware of supported eVMCS versions by host by
2016 * examining CPUID.0x4000000A.EAX[0:15]. Host userspace VMM is
2017 * expected to set this CPUID leaf according to the value
2018 * returned in vmcs_version from nested_enable_evmcs().
2019 *
2020 * However, it turns out that Microsoft Hyper-V fails to comply
2021 * to their own invented interface: When Hyper-V use eVMCS, it
2022 * just sets first u32 field of eVMCS to revision_id specified
2023 * in MSR_IA32_VMX_BASIC. Instead of used eVMCS version number
2024 * which is one of the supported versions specified in
2025 * CPUID.0x4000000A.EAX[0:15].
2026 *
2027 * To overcome Hyper-V bug, we accept here either a supported
2028 * eVMCS version or VMCS12 revision_id as valid values for first
2029 * u32 field of eVMCS.
2030 */
2031 if ((vmx->nested.hv_evmcs->revision_id != KVM_EVMCS_VERSION) &&
2032 (vmx->nested.hv_evmcs->revision_id != VMCS12_REVISION)) {
2033 nested_release_evmcs(vcpu);
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01002034 return EVMPTRLD_VMFAIL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002035 }
2036
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02002037 vmx->nested.hv_evmcs_vmptr = evmcs_gpa;
Sean Christopherson55d23752018-12-03 13:53:18 -08002038
Vitaly Kuznetsova21a39c2019-06-28 13:23:32 +02002039 evmcs_gpa_changed = true;
Sean Christopherson55d23752018-12-03 13:53:18 -08002040 /*
2041 * Unlike normal vmcs12, enlightened vmcs12 is not fully
2042 * reloaded from guest's memory (read only fields, fields not
2043 * present in struct hv_enlightened_vmcs, ...). Make sure there
2044 * are no leftovers.
2045 */
2046 if (from_launch) {
2047 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2048 memset(vmcs12, 0, sizeof(*vmcs12));
2049 vmcs12->hdr.revision_id = VMCS12_REVISION;
2050 }
2051
2052 }
Vitaly Kuznetsova21a39c2019-06-28 13:23:32 +02002053
2054 /*
Miaohe Linffdbd502020-02-07 23:22:45 +08002055 * Clean fields data can't be used on VMLAUNCH and when we switch
Vitaly Kuznetsova21a39c2019-06-28 13:23:32 +02002056 * between different L2 guests as KVM keeps a single VMCS12 per L1.
2057 */
2058 if (from_launch || evmcs_gpa_changed)
2059 vmx->nested.hv_evmcs->hv_clean_fields &=
2060 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
2061
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01002062 return EVMPTRLD_SUCCEEDED;
Sean Christopherson55d23752018-12-03 13:53:18 -08002063}
2064
Sean Christopherson3731905ef2019-05-07 08:36:27 -07002065void nested_sync_vmcs12_to_shadow(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08002066{
2067 struct vcpu_vmx *vmx = to_vmx(vcpu);
2068
Vitaly Kuznetsovdc313382021-05-26 15:20:24 +02002069 if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
Sean Christopherson55d23752018-12-03 13:53:18 -08002070 copy_vmcs12_to_enlightened(vmx);
Vitaly Kuznetsovdc313382021-05-26 15:20:24 +02002071 else
Sean Christopherson55d23752018-12-03 13:53:18 -08002072 copy_vmcs12_to_shadow(vmx);
Sean Christopherson55d23752018-12-03 13:53:18 -08002073
Sean Christopherson3731905ef2019-05-07 08:36:27 -07002074 vmx->nested.need_vmcs12_to_shadow_sync = false;
Sean Christopherson55d23752018-12-03 13:53:18 -08002075}
2076
2077static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
2078{
2079 struct vcpu_vmx *vmx =
2080 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
2081
2082 vmx->nested.preemption_timer_expired = true;
2083 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
2084 kvm_vcpu_kick(&vmx->vcpu);
2085
2086 return HRTIMER_NORESTART;
2087}
2088
Peter Shier850448f2020-05-26 14:51:06 -07002089static u64 vmx_calc_preemption_timer_value(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08002090{
Peter Shier850448f2020-05-26 14:51:06 -07002091 struct vcpu_vmx *vmx = to_vmx(vcpu);
2092 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Peter Shier850448f2020-05-26 14:51:06 -07002093
2094 u64 l1_scaled_tsc = kvm_read_l1_tsc(vcpu, rdtsc()) >>
2095 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
2096
2097 if (!vmx->nested.has_preemption_timer_deadline) {
Makarand Sonare8d7fbf02020-05-26 14:51:07 -07002098 vmx->nested.preemption_timer_deadline =
2099 vmcs12->vmx_preemption_timer_value + l1_scaled_tsc;
Peter Shier850448f2020-05-26 14:51:06 -07002100 vmx->nested.has_preemption_timer_deadline = true;
Makarand Sonare8d7fbf02020-05-26 14:51:07 -07002101 }
2102 return vmx->nested.preemption_timer_deadline - l1_scaled_tsc;
Peter Shier850448f2020-05-26 14:51:06 -07002103}
2104
2105static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu,
2106 u64 preemption_timeout)
2107{
Sean Christopherson55d23752018-12-03 13:53:18 -08002108 struct vcpu_vmx *vmx = to_vmx(vcpu);
2109
2110 /*
2111 * A timer value of zero is architecturally guaranteed to cause
2112 * a VMExit prior to executing any instructions in the guest.
2113 */
2114 if (preemption_timeout == 0) {
2115 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
2116 return;
2117 }
2118
2119 if (vcpu->arch.virtual_tsc_khz == 0)
2120 return;
2121
2122 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
2123 preemption_timeout *= 1000000;
2124 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
2125 hrtimer_start(&vmx->nested.preemption_timer,
Jim Mattsonada00982020-05-08 13:36:42 -07002126 ktime_add_ns(ktime_get(), preemption_timeout),
2127 HRTIMER_MODE_ABS_PINNED);
Sean Christopherson55d23752018-12-03 13:53:18 -08002128}
2129
2130static u64 nested_vmx_calc_efer(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
2131{
2132 if (vmx->nested.nested_run_pending &&
2133 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
2134 return vmcs12->guest_ia32_efer;
2135 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
2136 return vmx->vcpu.arch.efer | (EFER_LMA | EFER_LME);
2137 else
2138 return vmx->vcpu.arch.efer & ~(EFER_LMA | EFER_LME);
2139}
2140
2141static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx)
2142{
2143 /*
2144 * If vmcs02 hasn't been initialized, set the constant vmcs02 state
2145 * according to L0's settings (vmcs12 is irrelevant here). Host
2146 * fields that come from L0 and are not constant, e.g. HOST_CR3,
2147 * will be set as needed prior to VMLAUNCH/VMRESUME.
2148 */
2149 if (vmx->nested.vmcs02_initialized)
2150 return;
2151 vmx->nested.vmcs02_initialized = true;
2152
2153 /*
2154 * We don't care what the EPTP value is we just need to guarantee
2155 * it's valid so we don't get a false positive when doing early
2156 * consistency checks.
2157 */
2158 if (enable_ept && nested_early_check)
Sean Christopherson2a40b902020-07-15 20:41:18 -07002159 vmcs_write64(EPT_POINTER,
2160 construct_eptp(&vmx->vcpu, 0, PT64_ROOT_4LEVEL));
Sean Christopherson55d23752018-12-03 13:53:18 -08002161
2162 /* All VMFUNCs are currently emulated through L0 vmexits. */
2163 if (cpu_has_vmx_vmfunc())
2164 vmcs_write64(VM_FUNCTION_CONTROL, 0);
2165
2166 if (cpu_has_vmx_posted_intr())
2167 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
2168
2169 if (cpu_has_vmx_msr_bitmap())
2170 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
2171
Sean Christopherson4d6c9892019-05-07 09:06:30 -07002172 /*
Sean Christophersonc3bb9a22021-02-12 16:50:07 -08002173 * PML is emulated for L2, but never enabled in hardware as the MMU
2174 * handles A/D emulation. Disabling PML for L2 also avoids having to
2175 * deal with filtering out L2 GPAs from the buffer.
Sean Christopherson4d6c9892019-05-07 09:06:30 -07002176 */
2177 if (enable_pml) {
Sean Christophersonc3bb9a22021-02-12 16:50:07 -08002178 vmcs_write64(PML_ADDRESS, 0);
2179 vmcs_write16(GUEST_PML_INDEX, -1);
Sean Christopherson4d6c9892019-05-07 09:06:30 -07002180 }
Sean Christopherson55d23752018-12-03 13:53:18 -08002181
Sean Christophersonc538d572019-05-07 09:06:29 -07002182 if (cpu_has_vmx_encls_vmexit())
2183 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
Sean Christopherson55d23752018-12-03 13:53:18 -08002184
2185 /*
2186 * Set the MSR load/store lists to match L0's settings. Only the
2187 * addresses are constant (for vmcs02), the counts can change based
2188 * on L2's behavior, e.g. switching to/from long mode.
2189 */
Aaron Lewis662f1d12019-11-07 21:14:39 -08002190 vmcs_write64(VM_EXIT_MSR_STORE_ADDR, __pa(vmx->msr_autostore.guest.val));
Sean Christopherson55d23752018-12-03 13:53:18 -08002191 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
2192 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
2193
2194 vmx_set_constant_host_state(vmx);
2195}
2196
Paolo Bonzinib1346ab2019-06-06 17:24:00 +02002197static void prepare_vmcs02_early_rare(struct vcpu_vmx *vmx,
Sean Christopherson55d23752018-12-03 13:53:18 -08002198 struct vmcs12 *vmcs12)
2199{
2200 prepare_vmcs02_constant_state(vmx);
2201
2202 vmcs_write64(VMCS_LINK_POINTER, -1ull);
2203
2204 if (enable_vpid) {
2205 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
2206 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
2207 else
2208 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
2209 }
2210}
2211
2212static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
2213{
Sean Christophersonc3bb9a22021-02-12 16:50:07 -08002214 u32 exec_control;
Sean Christopherson55d23752018-12-03 13:53:18 -08002215 u64 guest_efer = nested_vmx_calc_efer(vmx, vmcs12);
2216
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02002217 if (vmx->nested.dirty_vmcs12 || evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
Paolo Bonzinib1346ab2019-06-06 17:24:00 +02002218 prepare_vmcs02_early_rare(vmx, vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08002219
2220 /*
Sean Christopherson55d23752018-12-03 13:53:18 -08002221 * PIN CONTROLS
2222 */
Sean Christophersonc075c3e2019-05-07 12:17:53 -07002223 exec_control = vmx_pin_based_exec_ctrl(vmx);
Sean Christopherson804939e2019-05-07 12:18:05 -07002224 exec_control |= (vmcs12->pin_based_vm_exec_control &
2225 ~PIN_BASED_VMX_PREEMPTION_TIMER);
Sean Christopherson55d23752018-12-03 13:53:18 -08002226
2227 /* Posted interrupts setting is only taken from vmcs12. */
2228 if (nested_cpu_has_posted_intr(vmcs12)) {
2229 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
2230 vmx->nested.pi_pending = false;
2231 } else {
2232 exec_control &= ~PIN_BASED_POSTED_INTR;
2233 }
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002234 pin_controls_set(vmx, exec_control);
Sean Christopherson55d23752018-12-03 13:53:18 -08002235
2236 /*
2237 * EXEC CONTROLS
2238 */
2239 exec_control = vmx_exec_control(vmx); /* L0's desires */
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08002240 exec_control &= ~CPU_BASED_INTR_WINDOW_EXITING;
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08002241 exec_control &= ~CPU_BASED_NMI_WINDOW_EXITING;
Sean Christopherson55d23752018-12-03 13:53:18 -08002242 exec_control &= ~CPU_BASED_TPR_SHADOW;
2243 exec_control |= vmcs12->cpu_based_vm_exec_control;
2244
Liran Alon02d496cf2019-11-11 14:30:55 +02002245 vmx->nested.l1_tpr_threshold = -1;
Sean Christophersonca2f5462019-05-07 09:06:33 -07002246 if (exec_control & CPU_BASED_TPR_SHADOW)
Sean Christopherson55d23752018-12-03 13:53:18 -08002247 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Sean Christopherson55d23752018-12-03 13:53:18 -08002248#ifdef CONFIG_X86_64
Sean Christophersonca2f5462019-05-07 09:06:33 -07002249 else
Sean Christopherson55d23752018-12-03 13:53:18 -08002250 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
2251 CPU_BASED_CR8_STORE_EXITING;
2252#endif
Sean Christopherson55d23752018-12-03 13:53:18 -08002253
2254 /*
2255 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
2256 * for I/O port accesses.
2257 */
Sean Christopherson55d23752018-12-03 13:53:18 -08002258 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
Sean Christophersonde0286b2019-05-07 12:18:01 -07002259 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
2260
2261 /*
2262 * This bit will be computed in nested_get_vmcs12_pages, because
2263 * we do not have access to L1's MSR bitmap yet. For now, keep
2264 * the same bit as before, hoping to avoid multiple VMWRITEs that
2265 * only set/clear this bit.
2266 */
2267 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
2268 exec_control |= exec_controls_get(vmx) & CPU_BASED_USE_MSR_BITMAPS;
2269
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002270 exec_controls_set(vmx, exec_control);
Sean Christopherson55d23752018-12-03 13:53:18 -08002271
2272 /*
2273 * SECONDARY EXEC CONTROLS
2274 */
2275 if (cpu_has_secondary_exec_ctrls()) {
2276 exec_control = vmx->secondary_exec_control;
2277
2278 /* Take the following fields only from vmcs12 */
2279 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
2280 SECONDARY_EXEC_ENABLE_INVPCID |
Sean Christopherson7f3603b2020-09-23 09:50:47 -07002281 SECONDARY_EXEC_ENABLE_RDTSCP |
Sean Christopherson55d23752018-12-03 13:53:18 -08002282 SECONDARY_EXEC_XSAVES |
Tao Xue69e72fa2019-07-16 14:55:49 +08002283 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE |
Sean Christopherson55d23752018-12-03 13:53:18 -08002284 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
2285 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Ilias Stamatisd041b5e2021-05-26 19:44:17 +01002286 SECONDARY_EXEC_ENABLE_VMFUNC |
2287 SECONDARY_EXEC_TSC_SCALING);
Sean Christopherson55d23752018-12-03 13:53:18 -08002288 if (nested_cpu_has(vmcs12,
Sean Christophersonc3bb9a22021-02-12 16:50:07 -08002289 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
2290 exec_control |= vmcs12->secondary_vm_exec_control;
2291
2292 /* PML is emulated and never enabled in hardware for L2. */
2293 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Sean Christopherson55d23752018-12-03 13:53:18 -08002294
2295 /* VMCS shadowing for L2 is emulated for now */
2296 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
2297
Sean Christopherson469debd2019-05-07 12:18:02 -07002298 /*
2299 * Preset *DT exiting when emulating UMIP, so that vmx_set_cr4()
2300 * will not have to rewrite the controls just for this bit.
2301 */
2302 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated() &&
2303 (vmcs12->guest_cr4 & X86_CR4_UMIP))
2304 exec_control |= SECONDARY_EXEC_DESC;
2305
Sean Christopherson55d23752018-12-03 13:53:18 -08002306 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
2307 vmcs_write16(GUEST_INTR_STATUS,
2308 vmcs12->guest_intr_status);
2309
Krish Sadhukhanbddd82d2020-09-21 08:10:25 +00002310 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
2311 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
2312
Sean Christopherson72add912021-04-12 16:21:42 +12002313 if (exec_control & SECONDARY_EXEC_ENCLS_EXITING)
2314 vmx_write_encls_bitmap(&vmx->vcpu, vmcs12);
2315
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002316 secondary_exec_controls_set(vmx, exec_control);
Sean Christopherson55d23752018-12-03 13:53:18 -08002317 }
2318
2319 /*
2320 * ENTRY CONTROLS
2321 *
2322 * vmcs12's VM_{ENTRY,EXIT}_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE
2323 * are emulated by vmx_set_efer() in prepare_vmcs02(), but speculate
2324 * on the related bits (if supported by the CPU) in the hope that
2325 * we can avoid VMWrites during vmx_set_efer().
2326 */
2327 exec_control = (vmcs12->vm_entry_controls | vmx_vmentry_ctrl()) &
2328 ~VM_ENTRY_IA32E_MODE & ~VM_ENTRY_LOAD_IA32_EFER;
2329 if (cpu_has_load_ia32_efer()) {
2330 if (guest_efer & EFER_LMA)
2331 exec_control |= VM_ENTRY_IA32E_MODE;
2332 if (guest_efer != host_efer)
2333 exec_control |= VM_ENTRY_LOAD_IA32_EFER;
2334 }
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002335 vm_entry_controls_set(vmx, exec_control);
Sean Christopherson55d23752018-12-03 13:53:18 -08002336
2337 /*
2338 * EXIT CONTROLS
2339 *
2340 * L2->L1 exit controls are emulated - the hardware exit is to L0 so
2341 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
2342 * bits may be modified by vmx_set_efer() in prepare_vmcs02().
2343 */
2344 exec_control = vmx_vmexit_ctrl();
2345 if (cpu_has_load_ia32_efer() && guest_efer != host_efer)
2346 exec_control |= VM_EXIT_LOAD_IA32_EFER;
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002347 vm_exit_controls_set(vmx, exec_control);
Sean Christopherson55d23752018-12-03 13:53:18 -08002348
2349 /*
2350 * Interrupt/Exception Fields
2351 */
2352 if (vmx->nested.nested_run_pending) {
2353 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2354 vmcs12->vm_entry_intr_info_field);
2355 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
2356 vmcs12->vm_entry_exception_error_code);
2357 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2358 vmcs12->vm_entry_instruction_len);
2359 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
2360 vmcs12->guest_interruptibility_info);
2361 vmx->loaded_vmcs->nmi_known_unmasked =
2362 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
2363 } else {
2364 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
2365 }
2366}
2367
Paolo Bonzinib1346ab2019-06-06 17:24:00 +02002368static void prepare_vmcs02_rare(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
Sean Christopherson55d23752018-12-03 13:53:18 -08002369{
2370 struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs;
2371
2372 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
2373 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2)) {
2374 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
2375 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
2376 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
2377 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
2378 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
2379 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
2380 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
2381 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
2382 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
2383 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
2384 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
2385 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
2386 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
2387 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
2388 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
2389 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
2390 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
2391 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07002392 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
2393 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
Sean Christopherson55d23752018-12-03 13:53:18 -08002394 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
2395 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
2396 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
2397 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
2398 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
2399 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
2400 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
2401 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
2402 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
2403 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
2404 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
2405 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
2406 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
2407 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
2408 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
2409 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Sean Christophersonfc387d82020-09-23 11:44:46 -07002410
2411 vmx->segment_cache.bitmask = 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08002412 }
2413
2414 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
2415 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1)) {
2416 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
2417 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
2418 vmcs12->guest_pending_dbg_exceptions);
2419 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
2420 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
2421
2422 /*
2423 * L1 may access the L2's PDPTR, so save them to construct
2424 * vmcs12
2425 */
2426 if (enable_ept) {
2427 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
2428 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
2429 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
2430 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
2431 }
Sean Christophersonc27e5b02019-05-07 09:06:39 -07002432
2433 if (kvm_mpx_supported() && vmx->nested.nested_run_pending &&
2434 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
2435 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
Sean Christopherson55d23752018-12-03 13:53:18 -08002436 }
2437
2438 if (nested_cpu_has_xsaves(vmcs12))
2439 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
2440
2441 /*
2442 * Whether page-faults are trapped is determined by a combination of
Paolo Bonzinia0c13432020-07-10 17:48:08 +02002443 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF. If L0
2444 * doesn't care about page faults then we should set all of these to
2445 * L1's desires. However, if L0 does care about (some) page faults, it
2446 * is not easy (if at all possible?) to merge L0 and L1's desires, we
2447 * simply ask to exit on each and every L2 page fault. This is done by
2448 * setting MASK=MATCH=0 and (see below) EB.PF=1.
Sean Christopherson55d23752018-12-03 13:53:18 -08002449 * Note that below we don't need special code to set EB.PF beyond the
2450 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
2451 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
2452 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
2453 */
Paolo Bonzinia0c13432020-07-10 17:48:08 +02002454 if (vmx_need_pf_intercept(&vmx->vcpu)) {
2455 /*
2456 * TODO: if both L0 and L1 need the same MASK and MATCH,
2457 * go ahead and use it?
2458 */
2459 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
2460 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
2461 } else {
2462 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, vmcs12->page_fault_error_code_mask);
2463 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, vmcs12->page_fault_error_code_match);
2464 }
Sean Christopherson55d23752018-12-03 13:53:18 -08002465
2466 if (cpu_has_vmx_apicv()) {
2467 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
2468 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
2469 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
2470 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
2471 }
2472
Aaron Lewis662f1d12019-11-07 21:14:39 -08002473 /*
2474 * Make sure the msr_autostore list is up to date before we set the
2475 * count in the vmcs02.
2476 */
2477 prepare_vmx_msr_autostore_list(&vmx->vcpu, MSR_IA32_TSC);
2478
2479 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, vmx->msr_autostore.guest.nr);
Sean Christopherson55d23752018-12-03 13:53:18 -08002480 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
2481 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
2482
2483 set_cr4_guest_host_mask(vmx);
Sean Christopherson55d23752018-12-03 13:53:18 -08002484}
2485
2486/*
2487 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
2488 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
2489 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
2490 * guest in a way that will both be appropriate to L1's requests, and our
2491 * needs. In addition to modifying the active vmcs (which is vmcs02), this
2492 * function also has additional necessary side-effects, like setting various
2493 * vcpu->arch fields.
2494 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
2495 * is assigned to entry_failure_code on failure.
2496 */
2497static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Maxim Levitsky0f857222021-06-07 12:02:00 +03002498 bool from_vmentry,
Sean Christopherson68cda402020-05-11 15:05:29 -07002499 enum vm_entry_failure_code *entry_failure_code)
Sean Christopherson55d23752018-12-03 13:53:18 -08002500{
2501 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersonc7554efc2019-05-07 09:06:40 -07002502 bool load_guest_pdptrs_vmcs12 = false;
Sean Christopherson55d23752018-12-03 13:53:18 -08002503
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02002504 if (vmx->nested.dirty_vmcs12 || evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) {
Paolo Bonzinib1346ab2019-06-06 17:24:00 +02002505 prepare_vmcs02_rare(vmx, vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08002506 vmx->nested.dirty_vmcs12 = false;
Sean Christopherson55d23752018-12-03 13:53:18 -08002507
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02002508 load_guest_pdptrs_vmcs12 = !evmptr_is_valid(vmx->nested.hv_evmcs_vmptr) ||
2509 !(vmx->nested.hv_evmcs->hv_clean_fields &
Sean Christophersonc7554efc2019-05-07 09:06:40 -07002510 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1);
Sean Christopherson55d23752018-12-03 13:53:18 -08002511 }
2512
2513 if (vmx->nested.nested_run_pending &&
2514 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
2515 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
2516 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
2517 } else {
2518 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
2519 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
2520 }
Sean Christopherson3b013a22019-05-07 09:06:28 -07002521 if (kvm_mpx_supported() && (!vmx->nested.nested_run_pending ||
2522 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)))
2523 vmcs_write64(GUEST_BNDCFGS, vmx->nested.vmcs01_guest_bndcfgs);
Sean Christopherson55d23752018-12-03 13:53:18 -08002524 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
2525
Sean Christopherson55d23752018-12-03 13:53:18 -08002526 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
2527 * bitwise-or of what L1 wants to trap for L2, and what we want to
2528 * trap. Note that CR0.TS also needs updating - we do this later.
2529 */
Jason Baronb6a7cc32021-01-14 22:27:54 -05002530 vmx_update_exception_bitmap(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08002531 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
2532 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
2533
2534 if (vmx->nested.nested_run_pending &&
2535 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
2536 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
2537 vcpu->arch.pat = vmcs12->guest_ia32_pat;
2538 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2539 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
2540 }
2541
Ilias Stamatisd041b5e2021-05-26 19:44:17 +01002542 vcpu->arch.tsc_offset = kvm_calc_nested_tsc_offset(
2543 vcpu->arch.l1_tsc_offset,
2544 vmx_get_l2_tsc_offset(vcpu),
2545 vmx_get_l2_tsc_multiplier(vcpu));
2546
2547 vcpu->arch.tsc_scaling_ratio = kvm_calc_nested_tsc_multiplier(
2548 vcpu->arch.l1_tsc_scaling_ratio,
2549 vmx_get_l2_tsc_multiplier(vcpu));
2550
Sean Christopherson55d23752018-12-03 13:53:18 -08002551 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Sean Christopherson55d23752018-12-03 13:53:18 -08002552 if (kvm_has_tsc_control)
Ilias Stamatis1ab92872021-06-07 11:54:38 +01002553 vmcs_write64(TSC_MULTIPLIER, vcpu->arch.tsc_scaling_ratio);
Sean Christopherson55d23752018-12-03 13:53:18 -08002554
Sean Christopherson50b265a2020-03-20 14:28:19 -07002555 nested_vmx_transition_tlb_flush(vcpu, vmcs12, true);
Sean Christopherson55d23752018-12-03 13:53:18 -08002556
2557 if (nested_cpu_has_ept(vmcs12))
2558 nested_ept_init_mmu_context(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08002559
2560 /*
2561 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
2562 * bits which we consider mandatory enabled.
2563 * The CR0_READ_SHADOW is what L2 should have expected to read given
2564 * the specifications by L1; It's not enough to take
2565 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
2566 * have more bits than L1 expected.
2567 */
2568 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
2569 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
2570
2571 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
2572 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
2573
2574 vcpu->arch.efer = nested_vmx_calc_efer(vmx, vmcs12);
2575 /* Note: may modify VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
2576 vmx_set_efer(vcpu, vcpu->arch.efer);
2577
2578 /*
2579 * Guest state is invalid and unrestricted guest is disabled,
2580 * which means L1 attempted VMEntry to L2 with invalid state.
2581 * Fail the VMEntry.
2582 */
Sean Christopherson2ba44932020-09-23 11:44:48 -07002583 if (CC(!vmx_guest_state_valid(vcpu))) {
Sean Christopherson55d23752018-12-03 13:53:18 -08002584 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christophersonc80add02019-04-11 12:18:09 -07002585 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002586 }
2587
2588 /* Shadow page tables on either EPT or shadow page tables. */
2589 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Maxim Levitsky0f857222021-06-07 12:02:00 +03002590 from_vmentry, entry_failure_code))
Sean Christophersonc80add02019-04-11 12:18:09 -07002591 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002592
Sean Christopherson04f11ef2019-09-27 14:45:16 -07002593 /*
2594 * Immediately write vmcs02.GUEST_CR3. It will be propagated to vmcs12
2595 * on nested VM-Exit, which can occur without actually running L2 and
Paolo Bonzini727a7e22020-03-05 03:52:50 -05002596 * thus without hitting vmx_load_mmu_pgd(), e.g. if L1 is entering L2 with
Sean Christopherson04f11ef2019-09-27 14:45:16 -07002597 * vmcs12.GUEST_ACTIVITYSTATE=HLT, in which case KVM will intercept the
2598 * transition to HLT instead of running L2.
2599 */
2600 if (enable_ept)
2601 vmcs_writel(GUEST_CR3, vmcs12->guest_cr3);
2602
Sean Christophersonc7554efc2019-05-07 09:06:40 -07002603 /* Late preparation of GUEST_PDPTRs now that EFER and CRs are set. */
2604 if (load_guest_pdptrs_vmcs12 && nested_cpu_has_ept(vmcs12) &&
2605 is_pae_paging(vcpu)) {
2606 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
2607 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
2608 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
2609 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
2610 }
2611
Sean Christopherson55d23752018-12-03 13:53:18 -08002612 if (!enable_ept)
2613 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
2614
Oliver Upton71f73472019-11-13 16:17:19 -08002615 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) &&
Oliver Uptond1968422019-12-13 16:33:58 -08002616 WARN_ON_ONCE(kvm_set_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL,
2617 vmcs12->guest_ia32_perf_global_ctrl)))
Oliver Upton71f73472019-11-13 16:17:19 -08002618 return -EINVAL;
2619
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02002620 kvm_rsp_write(vcpu, vmcs12->guest_rsp);
2621 kvm_rip_write(vcpu, vmcs12->guest_rip);
Vitaly Kuznetsovdc313382021-05-26 15:20:24 +02002622
2623 /*
2624 * It was observed that genuine Hyper-V running in L1 doesn't reset
2625 * 'hv_clean_fields' by itself, it only sets the corresponding dirty
2626 * bits when it changes a field in eVMCS. Mark all fields as clean
2627 * here.
2628 */
2629 if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
2630 vmx->nested.hv_evmcs->hv_clean_fields |=
2631 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
2632
Sean Christopherson55d23752018-12-03 13:53:18 -08002633 return 0;
2634}
2635
2636static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
2637{
Sean Christopherson5497b952019-07-11 08:58:29 -07002638 if (CC(!nested_cpu_has_nmi_exiting(vmcs12) &&
2639 nested_cpu_has_virtual_nmis(vmcs12)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002640 return -EINVAL;
2641
Sean Christopherson5497b952019-07-11 08:58:29 -07002642 if (CC(!nested_cpu_has_virtual_nmis(vmcs12) &&
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08002643 nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002644 return -EINVAL;
2645
2646 return 0;
2647}
2648
Sean Christophersonac6389a2020-03-02 18:02:38 -08002649static bool nested_vmx_check_eptp(struct kvm_vcpu *vcpu, u64 new_eptp)
Sean Christopherson55d23752018-12-03 13:53:18 -08002650{
2651 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08002652
2653 /* Check for memory type validity */
Sean Christophersonac6389a2020-03-02 18:02:38 -08002654 switch (new_eptp & VMX_EPTP_MT_MASK) {
Sean Christopherson55d23752018-12-03 13:53:18 -08002655 case VMX_EPTP_MT_UC:
Sean Christopherson5497b952019-07-11 08:58:29 -07002656 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002657 return false;
2658 break;
2659 case VMX_EPTP_MT_WB:
Sean Christopherson5497b952019-07-11 08:58:29 -07002660 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002661 return false;
2662 break;
2663 default:
2664 return false;
2665 }
2666
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08002667 /* Page-walk levels validity. */
Sean Christophersonac6389a2020-03-02 18:02:38 -08002668 switch (new_eptp & VMX_EPTP_PWL_MASK) {
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08002669 case VMX_EPTP_PWL_5:
2670 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_PAGE_WALK_5_BIT)))
2671 return false;
2672 break;
2673 case VMX_EPTP_PWL_4:
2674 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_PAGE_WALK_4_BIT)))
2675 return false;
2676 break;
2677 default:
Sean Christopherson55d23752018-12-03 13:53:18 -08002678 return false;
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08002679 }
Sean Christopherson55d23752018-12-03 13:53:18 -08002680
2681 /* Reserved bits should not be set */
Sean Christopherson636e8b72021-02-03 16:01:10 -08002682 if (CC(kvm_vcpu_is_illegal_gpa(vcpu, new_eptp) || ((new_eptp >> 7) & 0x1f)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002683 return false;
2684
2685 /* AD, if set, should be supported */
Sean Christophersonac6389a2020-03-02 18:02:38 -08002686 if (new_eptp & VMX_EPTP_AD_ENABLE_BIT) {
Sean Christopherson5497b952019-07-11 08:58:29 -07002687 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002688 return false;
2689 }
2690
2691 return true;
2692}
2693
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002694/*
2695 * Checks related to VM-Execution Control Fields
2696 */
2697static int nested_check_vm_execution_controls(struct kvm_vcpu *vcpu,
2698 struct vmcs12 *vmcs12)
2699{
2700 struct vcpu_vmx *vmx = to_vmx(vcpu);
2701
Sean Christopherson5497b952019-07-11 08:58:29 -07002702 if (CC(!vmx_control_verify(vmcs12->pin_based_vm_exec_control,
2703 vmx->nested.msrs.pinbased_ctls_low,
2704 vmx->nested.msrs.pinbased_ctls_high)) ||
2705 CC(!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
2706 vmx->nested.msrs.procbased_ctls_low,
2707 vmx->nested.msrs.procbased_ctls_high)))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002708 return -EINVAL;
2709
2710 if (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
Sean Christopherson5497b952019-07-11 08:58:29 -07002711 CC(!vmx_control_verify(vmcs12->secondary_vm_exec_control,
2712 vmx->nested.msrs.secondary_ctls_low,
2713 vmx->nested.msrs.secondary_ctls_high)))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002714 return -EINVAL;
2715
Sean Christopherson5497b952019-07-11 08:58:29 -07002716 if (CC(vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu)) ||
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002717 nested_vmx_check_io_bitmap_controls(vcpu, vmcs12) ||
2718 nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12) ||
2719 nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12) ||
2720 nested_vmx_check_apic_access_controls(vcpu, vmcs12) ||
2721 nested_vmx_check_apicv_controls(vcpu, vmcs12) ||
2722 nested_vmx_check_nmi_controls(vmcs12) ||
2723 nested_vmx_check_pml_controls(vcpu, vmcs12) ||
2724 nested_vmx_check_unrestricted_guest_controls(vcpu, vmcs12) ||
2725 nested_vmx_check_mode_based_ept_exec_controls(vcpu, vmcs12) ||
2726 nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12) ||
Sean Christopherson5497b952019-07-11 08:58:29 -07002727 CC(nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002728 return -EINVAL;
2729
Sean Christophersonbc441212019-02-12 16:42:23 -08002730 if (!nested_cpu_has_preemption_timer(vmcs12) &&
2731 nested_cpu_has_save_preemption_timer(vmcs12))
2732 return -EINVAL;
2733
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002734 if (nested_cpu_has_ept(vmcs12) &&
Sean Christophersonac6389a2020-03-02 18:02:38 -08002735 CC(!nested_vmx_check_eptp(vcpu, vmcs12->ept_pointer)))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002736 return -EINVAL;
2737
2738 if (nested_cpu_has_vmfunc(vmcs12)) {
Sean Christopherson5497b952019-07-11 08:58:29 -07002739 if (CC(vmcs12->vm_function_control &
2740 ~vmx->nested.msrs.vmfunc_controls))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002741 return -EINVAL;
2742
2743 if (nested_cpu_has_eptp_switching(vmcs12)) {
Sean Christopherson5497b952019-07-11 08:58:29 -07002744 if (CC(!nested_cpu_has_ept(vmcs12)) ||
2745 CC(!page_address_valid(vcpu, vmcs12->eptp_list_address)))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002746 return -EINVAL;
2747 }
2748 }
2749
2750 return 0;
2751}
2752
Krish Sadhukhan61446ba2018-12-12 13:30:09 -05002753/*
2754 * Checks related to VM-Exit Control Fields
2755 */
2756static int nested_check_vm_exit_controls(struct kvm_vcpu *vcpu,
2757 struct vmcs12 *vmcs12)
2758{
2759 struct vcpu_vmx *vmx = to_vmx(vcpu);
2760
Sean Christopherson5497b952019-07-11 08:58:29 -07002761 if (CC(!vmx_control_verify(vmcs12->vm_exit_controls,
2762 vmx->nested.msrs.exit_ctls_low,
2763 vmx->nested.msrs.exit_ctls_high)) ||
2764 CC(nested_vmx_check_exit_msr_switch_controls(vcpu, vmcs12)))
Krish Sadhukhan61446ba2018-12-12 13:30:09 -05002765 return -EINVAL;
2766
2767 return 0;
2768}
2769
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002770/*
2771 * Checks related to VM-Entry Control Fields
2772 */
2773static int nested_check_vm_entry_controls(struct kvm_vcpu *vcpu,
2774 struct vmcs12 *vmcs12)
Sean Christopherson55d23752018-12-03 13:53:18 -08002775{
2776 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08002777
Sean Christopherson5497b952019-07-11 08:58:29 -07002778 if (CC(!vmx_control_verify(vmcs12->vm_entry_controls,
2779 vmx->nested.msrs.entry_ctls_low,
2780 vmx->nested.msrs.entry_ctls_high)))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002781 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002782
2783 /*
2784 * From the Intel SDM, volume 3:
2785 * Fields relevant to VM-entry event injection must be set properly.
2786 * These fields are the VM-entry interruption-information field, the
2787 * VM-entry exception error code, and the VM-entry instruction length.
2788 */
2789 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
2790 u32 intr_info = vmcs12->vm_entry_intr_info_field;
2791 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
2792 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
2793 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
2794 bool should_have_error_code;
2795 bool urg = nested_cpu_has2(vmcs12,
2796 SECONDARY_EXEC_UNRESTRICTED_GUEST);
2797 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
2798
2799 /* VM-entry interruption-info field: interruption type */
Sean Christopherson5497b952019-07-11 08:58:29 -07002800 if (CC(intr_type == INTR_TYPE_RESERVED) ||
2801 CC(intr_type == INTR_TYPE_OTHER_EVENT &&
2802 !nested_cpu_supports_monitor_trap_flag(vcpu)))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002803 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002804
2805 /* VM-entry interruption-info field: vector */
Sean Christopherson5497b952019-07-11 08:58:29 -07002806 if (CC(intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
2807 CC(intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
2808 CC(intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002809 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002810
2811 /* VM-entry interruption-info field: deliver error code */
2812 should_have_error_code =
2813 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
2814 x86_exception_has_error_code(vector);
Sean Christopherson5497b952019-07-11 08:58:29 -07002815 if (CC(has_error_code != should_have_error_code))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002816 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002817
2818 /* VM-entry exception error code */
Sean Christopherson5497b952019-07-11 08:58:29 -07002819 if (CC(has_error_code &&
Sean Christopherson567926c2019-10-01 09:21:23 -07002820 vmcs12->vm_entry_exception_error_code & GENMASK(31, 16)))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002821 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002822
2823 /* VM-entry interruption-info field: reserved bits */
Sean Christopherson5497b952019-07-11 08:58:29 -07002824 if (CC(intr_info & INTR_INFO_RESVD_BITS_MASK))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002825 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002826
2827 /* VM-entry instruction length */
2828 switch (intr_type) {
2829 case INTR_TYPE_SOFT_EXCEPTION:
2830 case INTR_TYPE_SOFT_INTR:
2831 case INTR_TYPE_PRIV_SW_EXCEPTION:
Sean Christopherson5497b952019-07-11 08:58:29 -07002832 if (CC(vmcs12->vm_entry_instruction_len > 15) ||
2833 CC(vmcs12->vm_entry_instruction_len == 0 &&
2834 CC(!nested_cpu_has_zero_length_injection(vcpu))))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002835 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002836 }
2837 }
2838
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002839 if (nested_vmx_check_entry_msr_switch_controls(vcpu, vmcs12))
2840 return -EINVAL;
2841
2842 return 0;
2843}
2844
Sean Christopherson5478ba32019-04-11 12:18:06 -07002845static int nested_vmx_check_controls(struct kvm_vcpu *vcpu,
2846 struct vmcs12 *vmcs12)
2847{
2848 if (nested_check_vm_execution_controls(vcpu, vmcs12) ||
2849 nested_check_vm_exit_controls(vcpu, vmcs12) ||
2850 nested_check_vm_entry_controls(vcpu, vmcs12))
Paolo Bonzini98d9e852019-04-12 10:19:57 +02002851 return -EINVAL;
Sean Christopherson5478ba32019-04-11 12:18:06 -07002852
Vitaly Kuznetsova8350232020-02-05 13:30:34 +01002853 if (to_vmx(vcpu)->nested.enlightened_vmcs_enabled)
2854 return nested_evmcs_check_controls(vmcs12);
2855
Sean Christopherson5478ba32019-04-11 12:18:06 -07002856 return 0;
2857}
2858
Paolo Bonzini98d9e852019-04-12 10:19:57 +02002859static int nested_vmx_check_host_state(struct kvm_vcpu *vcpu,
2860 struct vmcs12 *vmcs12)
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002861{
2862 bool ia32e;
2863
Sean Christopherson5497b952019-07-11 08:58:29 -07002864 if (CC(!nested_host_cr0_valid(vcpu, vmcs12->host_cr0)) ||
2865 CC(!nested_host_cr4_valid(vcpu, vmcs12->host_cr4)) ||
Sean Christopherson636e8b72021-02-03 16:01:10 -08002866 CC(kvm_vcpu_is_illegal_gpa(vcpu, vmcs12->host_cr3)))
Krish Sadhukhan254b2f32018-12-12 13:30:11 -05002867 return -EINVAL;
Krish Sadhukhan711eff32019-02-07 14:05:30 -05002868
Sean Christopherson5497b952019-07-11 08:58:29 -07002869 if (CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_esp, vcpu)) ||
2870 CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_eip, vcpu)))
Krish Sadhukhan711eff32019-02-07 14:05:30 -05002871 return -EINVAL;
2872
Krish Sadhukhanf6b0db1f2019-04-08 17:35:11 -04002873 if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) &&
Sean Christopherson5497b952019-07-11 08:58:29 -07002874 CC(!kvm_pat_valid(vmcs12->host_ia32_pat)))
Krish Sadhukhanf6b0db1f2019-04-08 17:35:11 -04002875 return -EINVAL;
2876
Oliver Uptonc547cb62019-11-13 16:17:17 -08002877 if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) &&
2878 CC(!kvm_valid_perf_global_ctrl(vcpu_to_pmu(vcpu),
2879 vmcs12->host_ia32_perf_global_ctrl)))
2880 return -EINVAL;
2881
Paolo Bonzinifd3edd42019-09-25 18:33:53 +02002882#ifdef CONFIG_X86_64
2883 ia32e = !!(vcpu->arch.efer & EFER_LMA);
2884#else
2885 ia32e = false;
2886#endif
2887
2888 if (ia32e) {
2889 if (CC(!(vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)) ||
2890 CC(!(vmcs12->host_cr4 & X86_CR4_PAE)))
2891 return -EINVAL;
2892 } else {
2893 if (CC(vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) ||
2894 CC(vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) ||
2895 CC(vmcs12->host_cr4 & X86_CR4_PCIDE) ||
2896 CC((vmcs12->host_rip) >> 32))
2897 return -EINVAL;
2898 }
Krish Sadhukhan1ef23e12019-07-03 19:54:35 -04002899
Sean Christopherson5497b952019-07-11 08:58:29 -07002900 if (CC(vmcs12->host_cs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2901 CC(vmcs12->host_ss_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2902 CC(vmcs12->host_ds_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2903 CC(vmcs12->host_es_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2904 CC(vmcs12->host_fs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2905 CC(vmcs12->host_gs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2906 CC(vmcs12->host_tr_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2907 CC(vmcs12->host_cs_selector == 0) ||
2908 CC(vmcs12->host_tr_selector == 0) ||
2909 CC(vmcs12->host_ss_selector == 0 && !ia32e))
Krish Sadhukhan1ef23e12019-07-03 19:54:35 -04002910 return -EINVAL;
2911
Sean Christopherson5497b952019-07-11 08:58:29 -07002912 if (CC(is_noncanonical_address(vmcs12->host_fs_base, vcpu)) ||
2913 CC(is_noncanonical_address(vmcs12->host_gs_base, vcpu)) ||
2914 CC(is_noncanonical_address(vmcs12->host_gdtr_base, vcpu)) ||
2915 CC(is_noncanonical_address(vmcs12->host_idtr_base, vcpu)) ||
Paolo Bonzinifd3edd42019-09-25 18:33:53 +02002916 CC(is_noncanonical_address(vmcs12->host_tr_base, vcpu)) ||
2917 CC(is_noncanonical_address(vmcs12->host_rip, vcpu)))
Krish Sadhukhan58450382019-08-09 12:26:19 -07002918 return -EINVAL;
Krish Sadhukhan1ef23e12019-07-03 19:54:35 -04002919
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002920 /*
2921 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
2922 * IA32_EFER MSR must be 0 in the field for that register. In addition,
2923 * the values of the LMA and LME bits in the field must each be that of
2924 * the host address-space size VM-exit control.
2925 */
2926 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
Sean Christopherson5497b952019-07-11 08:58:29 -07002927 if (CC(!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer)) ||
2928 CC(ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA)) ||
2929 CC(ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)))
Krish Sadhukhan254b2f32018-12-12 13:30:11 -05002930 return -EINVAL;
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002931 }
2932
Sean Christopherson55d23752018-12-03 13:53:18 -08002933 return 0;
2934}
2935
2936static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
2937 struct vmcs12 *vmcs12)
2938{
KarimAllah Ahmed88925302019-01-31 21:24:41 +01002939 int r = 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08002940 struct vmcs12 *shadow;
KarimAllah Ahmed88925302019-01-31 21:24:41 +01002941 struct kvm_host_map map;
Sean Christopherson55d23752018-12-03 13:53:18 -08002942
2943 if (vmcs12->vmcs_link_pointer == -1ull)
2944 return 0;
2945
Sean Christopherson5497b952019-07-11 08:58:29 -07002946 if (CC(!page_address_valid(vcpu, vmcs12->vmcs_link_pointer)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002947 return -EINVAL;
2948
Sean Christopherson5497b952019-07-11 08:58:29 -07002949 if (CC(kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->vmcs_link_pointer), &map)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002950 return -EINVAL;
2951
KarimAllah Ahmed88925302019-01-31 21:24:41 +01002952 shadow = map.hva;
2953
Sean Christopherson5497b952019-07-11 08:58:29 -07002954 if (CC(shadow->hdr.revision_id != VMCS12_REVISION) ||
2955 CC(shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002956 r = -EINVAL;
KarimAllah Ahmed88925302019-01-31 21:24:41 +01002957
2958 kvm_vcpu_unmap(vcpu, &map, false);
Sean Christopherson55d23752018-12-03 13:53:18 -08002959 return r;
2960}
2961
Sean Christopherson55d23752018-12-03 13:53:18 -08002962/*
2963 * Checks related to Guest Non-register State
2964 */
2965static int nested_check_guest_non_reg_state(struct vmcs12 *vmcs12)
2966{
Sean Christopherson5497b952019-07-11 08:58:29 -07002967 if (CC(vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
Yadong Qibf0cd882020-11-06 14:51:22 +08002968 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT &&
2969 vmcs12->guest_activity_state != GUEST_ACTIVITY_WAIT_SIPI))
Sean Christopherson55d23752018-12-03 13:53:18 -08002970 return -EINVAL;
2971
2972 return 0;
2973}
2974
Sean Christopherson5478ba32019-04-11 12:18:06 -07002975static int nested_vmx_check_guest_state(struct kvm_vcpu *vcpu,
2976 struct vmcs12 *vmcs12,
Sean Christopherson68cda402020-05-11 15:05:29 -07002977 enum vm_entry_failure_code *entry_failure_code)
Sean Christopherson55d23752018-12-03 13:53:18 -08002978{
2979 bool ia32e;
2980
Sean Christopherson68cda402020-05-11 15:05:29 -07002981 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson55d23752018-12-03 13:53:18 -08002982
Sean Christopherson5497b952019-07-11 08:58:29 -07002983 if (CC(!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0)) ||
2984 CC(!nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4)))
Sean Christophersonc80add02019-04-11 12:18:09 -07002985 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002986
Krish Sadhukhanb91991b2020-01-15 19:54:32 -05002987 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) &&
2988 CC(!kvm_dr7_valid(vmcs12->guest_dr7)))
2989 return -EINVAL;
2990
Krish Sadhukhande2bc2b2019-04-08 17:35:12 -04002991 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) &&
Sean Christopherson5497b952019-07-11 08:58:29 -07002992 CC(!kvm_pat_valid(vmcs12->guest_ia32_pat)))
Sean Christophersonc80add02019-04-11 12:18:09 -07002993 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002994
2995 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
Sean Christopherson68cda402020-05-11 15:05:29 -07002996 *entry_failure_code = ENTRY_FAIL_VMCS_LINK_PTR;
Sean Christophersonc80add02019-04-11 12:18:09 -07002997 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002998 }
2999
Oliver Uptonbfc6ad62019-11-13 16:17:16 -08003000 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) &&
3001 CC(!kvm_valid_perf_global_ctrl(vcpu_to_pmu(vcpu),
3002 vmcs12->guest_ia32_perf_global_ctrl)))
3003 return -EINVAL;
3004
Sean Christopherson55d23752018-12-03 13:53:18 -08003005 /*
3006 * If the load IA32_EFER VM-entry control is 1, the following checks
3007 * are performed on the field for the IA32_EFER MSR:
3008 * - Bits reserved in the IA32_EFER MSR must be 0.
3009 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
3010 * the IA-32e mode guest VM-exit control. It must also be identical
3011 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
3012 * CR0.PG) is 1.
3013 */
3014 if (to_vmx(vcpu)->nested.nested_run_pending &&
3015 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
3016 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
Sean Christopherson5497b952019-07-11 08:58:29 -07003017 if (CC(!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer)) ||
3018 CC(ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA)) ||
3019 CC(((vmcs12->guest_cr0 & X86_CR0_PG) &&
3020 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))))
Sean Christophersonc80add02019-04-11 12:18:09 -07003021 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08003022 }
3023
3024 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
Sean Christopherson5497b952019-07-11 08:58:29 -07003025 (CC(is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu)) ||
3026 CC((vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD))))
Sean Christophersonc80add02019-04-11 12:18:09 -07003027 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08003028
Sean Christopherson9c3e9222019-04-11 12:18:05 -07003029 if (nested_check_guest_non_reg_state(vmcs12))
Sean Christophersonc80add02019-04-11 12:18:09 -07003030 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08003031
3032 return 0;
3033}
3034
Sean Christopherson453eafb2018-12-20 12:25:17 -08003035static int nested_vmx_check_vmentry_hw(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08003036{
3037 struct vcpu_vmx *vmx = to_vmx(vcpu);
3038 unsigned long cr3, cr4;
Sean Christophersonf1727b42019-01-25 07:40:58 -08003039 bool vm_fail;
Sean Christopherson55d23752018-12-03 13:53:18 -08003040
3041 if (!nested_early_check)
3042 return 0;
3043
3044 if (vmx->msr_autoload.host.nr)
3045 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
3046 if (vmx->msr_autoload.guest.nr)
3047 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
3048
3049 preempt_disable();
3050
3051 vmx_prepare_switch_to_guest(vcpu);
3052
3053 /*
3054 * Induce a consistency check VMExit by clearing bit 1 in GUEST_RFLAGS,
3055 * which is reserved to '1' by hardware. GUEST_RFLAGS is guaranteed to
Miaohe Lin49f933d2020-02-27 11:20:54 +08003056 * be written (by prepare_vmcs02()) before the "real" VMEnter, i.e.
Sean Christopherson55d23752018-12-03 13:53:18 -08003057 * there is no need to preserve other bits or save/restore the field.
3058 */
3059 vmcs_writel(GUEST_RFLAGS, 0);
3060
Sean Christopherson55d23752018-12-03 13:53:18 -08003061 cr3 = __get_current_cr3_fast();
3062 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
3063 vmcs_writel(HOST_CR3, cr3);
3064 vmx->loaded_vmcs->host_state.cr3 = cr3;
3065 }
3066
3067 cr4 = cr4_read_shadow();
3068 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
3069 vmcs_writel(HOST_CR4, cr4);
3070 vmx->loaded_vmcs->host_state.cr4 = cr4;
3071 }
3072
Uros Bizjak150f17b2020-12-30 16:26:57 -08003073 vm_fail = __vmx_vcpu_run(vmx, (unsigned long *)&vcpu->arch.regs,
3074 vmx->loaded_vmcs->launched);
Sean Christopherson55d23752018-12-03 13:53:18 -08003075
Sean Christopherson55d23752018-12-03 13:53:18 -08003076 if (vmx->msr_autoload.host.nr)
3077 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
3078 if (vmx->msr_autoload.guest.nr)
3079 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
3080
Sean Christophersonf1727b42019-01-25 07:40:58 -08003081 if (vm_fail) {
Sean Christopherson380e0052019-07-11 08:58:30 -07003082 u32 error = vmcs_read32(VM_INSTRUCTION_ERROR);
3083
Wanpeng Li541e8862019-05-17 16:49:50 +08003084 preempt_enable();
Sean Christopherson380e0052019-07-11 08:58:30 -07003085
3086 trace_kvm_nested_vmenter_failed(
3087 "early hardware check VM-instruction error: ", error);
3088 WARN_ON_ONCE(error != VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Sean Christopherson55d23752018-12-03 13:53:18 -08003089 return 1;
3090 }
3091
3092 /*
3093 * VMExit clears RFLAGS.IF and DR7, even on a consistency check.
3094 */
Sean Christopherson55d23752018-12-03 13:53:18 -08003095 if (hw_breakpoint_active())
3096 set_debugreg(__this_cpu_read(cpu_dr7), 7);
Peter Zijlstra84b6a342020-05-29 23:27:36 +02003097 local_irq_enable();
Wanpeng Li541e8862019-05-17 16:49:50 +08003098 preempt_enable();
Sean Christopherson55d23752018-12-03 13:53:18 -08003099
3100 /*
3101 * A non-failing VMEntry means we somehow entered guest mode with
3102 * an illegal RIP, and that's just the tip of the iceberg. There
3103 * is no telling what memory has been modified or what state has
3104 * been exposed to unknown code. Hitting this all but guarantees
3105 * a (very critical) hardware issue.
3106 */
3107 WARN_ON(!(vmcs_read32(VM_EXIT_REASON) &
3108 VMX_EXIT_REASONS_FAILED_VMENTRY));
3109
3110 return 0;
3111}
Sean Christopherson55d23752018-12-03 13:53:18 -08003112
Paolo Bonzini9a78e152021-01-08 11:43:08 -05003113static bool nested_get_evmcs_page(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08003114{
Sean Christopherson55d23752018-12-03 13:53:18 -08003115 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08003116
Vitaly Kuznetsove942dbf2020-03-09 16:52:12 +01003117 /*
3118 * hv_evmcs may end up being not mapped after migration (when
3119 * L2 was running), map it here to make sure vmcs12 changes are
3120 * properly reflected.
3121 */
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02003122 if (vmx->nested.enlightened_vmcs_enabled &&
Vitaly Kuznetsov27849962021-05-26 15:20:20 +02003123 vmx->nested.hv_evmcs_vmptr == EVMPTR_MAP_PENDING) {
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003124 enum nested_evmptrld_status evmptrld_status =
3125 nested_vmx_handle_enlightened_vmptrld(vcpu, false);
3126
3127 if (evmptrld_status == EVMPTRLD_VMFAIL ||
Vitaly Kuznetsovf5c7e842021-05-03 17:08:51 +02003128 evmptrld_status == EVMPTRLD_ERROR)
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003129 return false;
Vitaly Kuznetsov8629b622021-05-26 15:20:25 +02003130
3131 /*
3132 * Post migration VMCS12 always provides the most actual
3133 * information, copy it to eVMCS upon entry.
3134 */
3135 vmx->nested.need_vmcs12_to_shadow_sync = true;
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003136 }
Vitaly Kuznetsove942dbf2020-03-09 16:52:12 +01003137
Paolo Bonzini9a78e152021-01-08 11:43:08 -05003138 return true;
3139}
3140
3141static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
3142{
3143 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3144 struct vcpu_vmx *vmx = to_vmx(vcpu);
3145 struct kvm_host_map *map;
3146 struct page *page;
3147 u64 hpa;
3148
Maxim Levitsky158a48e2021-06-07 12:02:03 +03003149 if (!vcpu->arch.pdptrs_from_userspace &&
3150 !nested_cpu_has_ept(vmcs12) && is_pae_paging(vcpu)) {
Maxim Levitsky0f857222021-06-07 12:02:00 +03003151 /*
3152 * Reload the guest's PDPTRs since after a migration
3153 * the guest CR3 might be restored prior to setting the nested
3154 * state which can lead to a load of wrong PDPTRs.
3155 */
3156 if (CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, vcpu->arch.cr3)))
3157 return false;
3158 }
3159
3160
Sean Christopherson55d23752018-12-03 13:53:18 -08003161 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
3162 /*
3163 * Translate L1 physical address to host physical
3164 * address for vmcs02. Keep the page pinned, so this
3165 * physical address remains valid. We keep a reference
3166 * to it so we can release it later.
3167 */
3168 if (vmx->nested.apic_access_page) { /* shouldn't happen */
Liran Alonb11494b2019-11-21 00:31:47 +02003169 kvm_release_page_clean(vmx->nested.apic_access_page);
Sean Christopherson55d23752018-12-03 13:53:18 -08003170 vmx->nested.apic_access_page = NULL;
3171 }
3172 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Sean Christopherson55d23752018-12-03 13:53:18 -08003173 if (!is_error_page(page)) {
3174 vmx->nested.apic_access_page = page;
3175 hpa = page_to_phys(vmx->nested.apic_access_page);
3176 vmcs_write64(APIC_ACCESS_ADDR, hpa);
3177 } else {
Jim Mattson671ddc72019-10-15 10:44:05 -07003178 pr_debug_ratelimited("%s: no backing 'struct page' for APIC-access address in vmcs12\n",
3179 __func__);
3180 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3181 vcpu->run->internal.suberror =
3182 KVM_INTERNAL_ERROR_EMULATION;
3183 vcpu->run->internal.ndata = 0;
3184 return false;
Sean Christopherson55d23752018-12-03 13:53:18 -08003185 }
3186 }
3187
3188 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003189 map = &vmx->nested.virtual_apic_map;
Sean Christopherson55d23752018-12-03 13:53:18 -08003190
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003191 if (!kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->virtual_apic_page_addr), map)) {
3192 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, pfn_to_hpa(map->pfn));
Paolo Bonzini69090812019-04-15 15:16:17 +02003193 } else if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING) &&
3194 nested_cpu_has(vmcs12, CPU_BASED_CR8_STORE_EXITING) &&
3195 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
3196 /*
3197 * The processor will never use the TPR shadow, simply
3198 * clear the bit from the execution control. Such a
3199 * configuration is useless, but it happens in tests.
3200 * For any other configuration, failing the vm entry is
3201 * _not_ what the processor does but it's basically the
3202 * only possibility we have.
3203 */
Sean Christopherson2183f562019-05-07 12:17:56 -07003204 exec_controls_clearbit(vmx, CPU_BASED_TPR_SHADOW);
Paolo Bonzini69090812019-04-15 15:16:17 +02003205 } else {
Sean Christophersonca2f5462019-05-07 09:06:33 -07003206 /*
3207 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR to
3208 * force VM-Entry to fail.
3209 */
3210 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Sean Christopherson55d23752018-12-03 13:53:18 -08003211 }
3212 }
3213
3214 if (nested_cpu_has_posted_intr(vmcs12)) {
KarimAllah Ahmed3278e042019-01-31 21:24:38 +01003215 map = &vmx->nested.pi_desc_map;
3216
3217 if (!kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->posted_intr_desc_addr), map)) {
3218 vmx->nested.pi_desc =
3219 (struct pi_desc *)(((void *)map->hva) +
3220 offset_in_page(vmcs12->posted_intr_desc_addr));
3221 vmcs_write64(POSTED_INTR_DESC_ADDR,
3222 pfn_to_hpa(map->pfn) + offset_in_page(vmcs12->posted_intr_desc_addr));
Jim Mattson966eefb2021-06-04 10:26:06 -07003223 } else {
3224 /*
3225 * Defer the KVM_INTERNAL_EXIT until KVM tries to
3226 * access the contents of the VMCS12 posted interrupt
3227 * descriptor. (Note that KVM may do this when it
3228 * should not, per the architectural specification.)
3229 */
3230 vmx->nested.pi_desc = NULL;
3231 pin_controls_clearbit(vmx, PIN_BASED_POSTED_INTR);
Sean Christopherson55d23752018-12-03 13:53:18 -08003232 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003233 }
3234 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
Sean Christopherson2183f562019-05-07 12:17:56 -07003235 exec_controls_setbit(vmx, CPU_BASED_USE_MSR_BITMAPS);
Sean Christopherson55d23752018-12-03 13:53:18 -08003236 else
Sean Christopherson2183f562019-05-07 12:17:56 -07003237 exec_controls_clearbit(vmx, CPU_BASED_USE_MSR_BITMAPS);
Paolo Bonzini9a78e152021-01-08 11:43:08 -05003238
3239 return true;
3240}
3241
3242static bool vmx_get_nested_state_pages(struct kvm_vcpu *vcpu)
3243{
Vitaly Kuznetsovf5c7e842021-05-03 17:08:51 +02003244 if (!nested_get_evmcs_page(vcpu)) {
3245 pr_debug_ratelimited("%s: enlightened vmptrld failed\n",
3246 __func__);
3247 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3248 vcpu->run->internal.suberror =
3249 KVM_INTERNAL_ERROR_EMULATION;
3250 vcpu->run->internal.ndata = 0;
3251
Paolo Bonzini9a78e152021-01-08 11:43:08 -05003252 return false;
Vitaly Kuznetsovf5c7e842021-05-03 17:08:51 +02003253 }
Paolo Bonzini9a78e152021-01-08 11:43:08 -05003254
3255 if (is_guest_mode(vcpu) && !nested_get_vmcs12_pages(vcpu))
3256 return false;
3257
Jim Mattson671ddc72019-10-15 10:44:05 -07003258 return true;
Sean Christopherson55d23752018-12-03 13:53:18 -08003259}
3260
Sean Christopherson02f5fb22020-06-22 14:58:32 -07003261static int nested_vmx_write_pml_buffer(struct kvm_vcpu *vcpu, gpa_t gpa)
3262{
3263 struct vmcs12 *vmcs12;
3264 struct vcpu_vmx *vmx = to_vmx(vcpu);
3265 gpa_t dst;
3266
3267 if (WARN_ON_ONCE(!is_guest_mode(vcpu)))
3268 return 0;
3269
3270 if (WARN_ON_ONCE(vmx->nested.pml_full))
3271 return 1;
3272
3273 /*
3274 * Check if PML is enabled for the nested guest. Whether eptp bit 6 is
3275 * set is already checked as part of A/D emulation.
3276 */
3277 vmcs12 = get_vmcs12(vcpu);
3278 if (!nested_cpu_has_pml(vmcs12))
3279 return 0;
3280
3281 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
3282 vmx->nested.pml_full = true;
3283 return 1;
3284 }
3285
3286 gpa &= ~0xFFFull;
3287 dst = vmcs12->pml_address + sizeof(u64) * vmcs12->guest_pml_index;
3288
3289 if (kvm_write_guest_page(vcpu->kvm, gpa_to_gfn(dst), &gpa,
3290 offset_in_page(dst), sizeof(gpa)))
3291 return 0;
3292
3293 vmcs12->guest_pml_index--;
3294
3295 return 0;
3296}
3297
Sean Christopherson55d23752018-12-03 13:53:18 -08003298/*
3299 * Intel's VMX Instruction Reference specifies a common set of prerequisites
3300 * for running VMX instructions (except VMXON, whose prerequisites are
3301 * slightly different). It also specifies what exception to inject otherwise.
3302 * Note that many of these exceptions have priority over VM exits, so they
3303 * don't have to be checked again here.
3304 */
3305static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
3306{
3307 if (!to_vmx(vcpu)->nested.vmxon) {
3308 kvm_queue_exception(vcpu, UD_VECTOR);
3309 return 0;
3310 }
3311
3312 if (vmx_get_cpl(vcpu)) {
3313 kvm_inject_gp(vcpu, 0);
3314 return 0;
3315 }
3316
3317 return 1;
3318}
3319
3320static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu)
3321{
3322 u8 rvi = vmx_get_rvi();
3323 u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI);
3324
3325 return ((rvi & 0xf0) > (vppr & 0xf0));
3326}
3327
3328static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
3329 struct vmcs12 *vmcs12);
3330
3331/*
3332 * If from_vmentry is false, this is being called from state restore (either RSM
3333 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
Jim Mattson671ddc72019-10-15 10:44:05 -07003334 *
3335 * Returns:
Miaohe Lin463bfee2020-02-14 10:44:05 +08003336 * NVMX_VMENTRY_SUCCESS: Entered VMX non-root mode
3337 * NVMX_VMENTRY_VMFAIL: Consistency check VMFail
3338 * NVMX_VMENTRY_VMEXIT: Consistency check VMExit
3339 * NVMX_VMENTRY_KVM_INTERNAL_ERROR: KVM internal error
Sean Christopherson55d23752018-12-03 13:53:18 -08003340 */
Jim Mattson671ddc72019-10-15 10:44:05 -07003341enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu,
3342 bool from_vmentry)
Sean Christopherson55d23752018-12-03 13:53:18 -08003343{
3344 struct vcpu_vmx *vmx = to_vmx(vcpu);
3345 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Sean Christopherson68cda402020-05-11 15:05:29 -07003346 enum vm_entry_failure_code entry_failure_code;
Sean Christopherson55d23752018-12-03 13:53:18 -08003347 bool evaluate_pending_interrupts;
Sean Christopherson8e533242020-11-06 17:03:12 +08003348 union vmx_exit_reason exit_reason = {
3349 .basic = EXIT_REASON_INVALID_STATE,
3350 .failed_vmentry = 1,
3351 };
3352 u32 failed_index;
Sean Christopherson55d23752018-12-03 13:53:18 -08003353
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07003354 if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu))
3355 kvm_vcpu_flush_tlb_current(vcpu);
3356
Sean Christopherson2183f562019-05-07 12:17:56 -07003357 evaluate_pending_interrupts = exec_controls_get(vmx) &
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08003358 (CPU_BASED_INTR_WINDOW_EXITING | CPU_BASED_NMI_WINDOW_EXITING);
Sean Christopherson55d23752018-12-03 13:53:18 -08003359 if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu))
3360 evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu);
3361
3362 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
3363 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
3364 if (kvm_mpx_supported() &&
3365 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
3366 vmx->nested.vmcs01_guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
3367
Sean Christophersonf087a022019-06-07 11:55:34 -07003368 /*
3369 * Overwrite vmcs01.GUEST_CR3 with L1's CR3 if EPT is disabled *and*
3370 * nested early checks are disabled. In the event of a "late" VM-Fail,
3371 * i.e. a VM-Fail detected by hardware but not KVM, KVM must unwind its
3372 * software model to the pre-VMEntry host state. When EPT is disabled,
3373 * GUEST_CR3 holds KVM's shadow CR3, not L1's "real" CR3, which causes
3374 * nested_vmx_restore_host_state() to corrupt vcpu->arch.cr3. Stuffing
3375 * vmcs01.GUEST_CR3 results in the unwind naturally setting arch.cr3 to
3376 * the correct value. Smashing vmcs01.GUEST_CR3 is safe because nested
3377 * VM-Exits, and the unwind, reset KVM's MMU, i.e. vmcs01.GUEST_CR3 is
3378 * guaranteed to be overwritten with a shadow CR3 prior to re-entering
3379 * L1. Don't stuff vmcs01.GUEST_CR3 when using nested early checks as
3380 * KVM modifies vcpu->arch.cr3 if and only if the early hardware checks
3381 * pass, and early VM-Fails do not reset KVM's MMU, i.e. the VM-Fail
3382 * path would need to manually save/restore vmcs01.GUEST_CR3.
3383 */
3384 if (!enable_ept && !nested_early_check)
3385 vmcs_writel(GUEST_CR3, vcpu->arch.cr3);
3386
Sean Christopherson55d23752018-12-03 13:53:18 -08003387 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
3388
3389 prepare_vmcs02_early(vmx, vmcs12);
3390
3391 if (from_vmentry) {
Sean Christophersonb89d5ad2020-09-23 11:44:47 -07003392 if (unlikely(!nested_get_vmcs12_pages(vcpu))) {
3393 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson671ddc72019-10-15 10:44:05 -07003394 return NVMX_VMENTRY_KVM_INTERNAL_ERROR;
Sean Christophersonb89d5ad2020-09-23 11:44:47 -07003395 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003396
3397 if (nested_vmx_check_vmentry_hw(vcpu)) {
3398 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson671ddc72019-10-15 10:44:05 -07003399 return NVMX_VMENTRY_VMFAIL;
Sean Christopherson55d23752018-12-03 13:53:18 -08003400 }
3401
Sean Christopherson68cda402020-05-11 15:05:29 -07003402 if (nested_vmx_check_guest_state(vcpu, vmcs12,
3403 &entry_failure_code)) {
Sean Christopherson8e533242020-11-06 17:03:12 +08003404 exit_reason.basic = EXIT_REASON_INVALID_STATE;
Sean Christopherson68cda402020-05-11 15:05:29 -07003405 vmcs12->exit_qualification = entry_failure_code;
Sean Christopherson55d23752018-12-03 13:53:18 -08003406 goto vmentry_fail_vmexit;
Sean Christopherson68cda402020-05-11 15:05:29 -07003407 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003408 }
3409
3410 enter_guest_mode(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08003411
Maxim Levitsky0f857222021-06-07 12:02:00 +03003412 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &entry_failure_code)) {
Sean Christopherson8e533242020-11-06 17:03:12 +08003413 exit_reason.basic = EXIT_REASON_INVALID_STATE;
Sean Christopherson68cda402020-05-11 15:05:29 -07003414 vmcs12->exit_qualification = entry_failure_code;
Sean Christopherson55d23752018-12-03 13:53:18 -08003415 goto vmentry_fail_vmexit_guest_mode;
Sean Christopherson68cda402020-05-11 15:05:29 -07003416 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003417
3418 if (from_vmentry) {
Sean Christopherson68cda402020-05-11 15:05:29 -07003419 failed_index = nested_vmx_load_msr(vcpu,
3420 vmcs12->vm_entry_msr_load_addr,
3421 vmcs12->vm_entry_msr_load_count);
3422 if (failed_index) {
Sean Christopherson8e533242020-11-06 17:03:12 +08003423 exit_reason.basic = EXIT_REASON_MSR_LOAD_FAIL;
Sean Christopherson68cda402020-05-11 15:05:29 -07003424 vmcs12->exit_qualification = failed_index;
Sean Christopherson55d23752018-12-03 13:53:18 -08003425 goto vmentry_fail_vmexit_guest_mode;
Sean Christopherson68cda402020-05-11 15:05:29 -07003426 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003427 } else {
3428 /*
3429 * The MMU is not initialized to point at the right entities yet and
3430 * "get pages" would need to read data from the guest (i.e. we will
3431 * need to perform gpa to hpa translation). Request a call
3432 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
3433 * have already been set at vmentry time and should not be reset.
3434 */
Paolo Bonzini729c15c2020-09-22 06:53:57 -04003435 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08003436 }
3437
3438 /*
3439 * If L1 had a pending IRQ/NMI until it executed
3440 * VMLAUNCH/VMRESUME which wasn't delivered because it was
3441 * disallowed (e.g. interrupts disabled), L0 needs to
3442 * evaluate if this pending event should cause an exit from L2
3443 * to L1 or delivered directly to L2 (e.g. In case L1 don't
3444 * intercept EXTERNAL_INTERRUPT).
3445 *
3446 * Usually this would be handled by the processor noticing an
3447 * IRQ/NMI window request, or checking RVI during evaluation of
3448 * pending virtual interrupts. However, this setting was done
3449 * on VMCS01 and now VMCS02 is active instead. Thus, we force L0
3450 * to perform pending event evaluation by requesting a KVM_REQ_EVENT.
3451 */
3452 if (unlikely(evaluate_pending_interrupts))
3453 kvm_make_request(KVM_REQ_EVENT, vcpu);
3454
3455 /*
Paolo Bonzini359a6c32019-01-29 19:14:46 +01003456 * Do not start the preemption timer hrtimer until after we know
3457 * we are successful, so that only nested_vmx_vmexit needs to cancel
3458 * the timer.
3459 */
3460 vmx->nested.preemption_timer_expired = false;
Peter Shier850448f2020-05-26 14:51:06 -07003461 if (nested_cpu_has_preemption_timer(vmcs12)) {
3462 u64 timer_value = vmx_calc_preemption_timer_value(vcpu);
3463 vmx_start_preemption_timer(vcpu, timer_value);
3464 }
Paolo Bonzini359a6c32019-01-29 19:14:46 +01003465
3466 /*
Sean Christopherson55d23752018-12-03 13:53:18 -08003467 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
3468 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
3469 * returned as far as L1 is concerned. It will only return (and set
3470 * the success flag) when L2 exits (see nested_vmx_vmexit()).
3471 */
Jim Mattson671ddc72019-10-15 10:44:05 -07003472 return NVMX_VMENTRY_SUCCESS;
Sean Christopherson55d23752018-12-03 13:53:18 -08003473
3474 /*
3475 * A failed consistency check that leads to a VMExit during L1's
3476 * VMEnter to L2 is a variation of a normal VMexit, as explained in
3477 * 26.7 "VM-entry failures during or after loading guest state".
3478 */
3479vmentry_fail_vmexit_guest_mode:
Xiaoyao Li5e3d3942019-12-06 16:45:26 +08003480 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING)
Sean Christopherson55d23752018-12-03 13:53:18 -08003481 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
3482 leave_guest_mode(vcpu);
3483
3484vmentry_fail_vmexit:
3485 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
3486
3487 if (!from_vmentry)
Jim Mattson671ddc72019-10-15 10:44:05 -07003488 return NVMX_VMENTRY_VMEXIT;
Sean Christopherson55d23752018-12-03 13:53:18 -08003489
3490 load_vmcs12_host_state(vcpu, vmcs12);
Sean Christopherson8e533242020-11-06 17:03:12 +08003491 vmcs12->vm_exit_reason = exit_reason.full;
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02003492 if (enable_shadow_vmcs || evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
Sean Christopherson3731905ef2019-05-07 08:36:27 -07003493 vmx->nested.need_vmcs12_to_shadow_sync = true;
Jim Mattson671ddc72019-10-15 10:44:05 -07003494 return NVMX_VMENTRY_VMEXIT;
Sean Christopherson55d23752018-12-03 13:53:18 -08003495}
3496
3497/*
3498 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
3499 * for running an L2 nested guest.
3500 */
3501static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
3502{
3503 struct vmcs12 *vmcs12;
Jim Mattson671ddc72019-10-15 10:44:05 -07003504 enum nvmx_vmentry_status status;
Sean Christopherson55d23752018-12-03 13:53:18 -08003505 struct vcpu_vmx *vmx = to_vmx(vcpu);
3506 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003507 enum nested_evmptrld_status evmptrld_status;
Sean Christopherson55d23752018-12-03 13:53:18 -08003508
3509 if (!nested_vmx_check_permission(vcpu))
3510 return 1;
3511
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003512 evmptrld_status = nested_vmx_handle_enlightened_vmptrld(vcpu, launch);
3513 if (evmptrld_status == EVMPTRLD_ERROR) {
3514 kvm_queue_exception(vcpu, UD_VECTOR);
Sean Christopherson55d23752018-12-03 13:53:18 -08003515 return 1;
Sean Christophersonfc595f32020-08-12 11:06:15 -07003516 } else if (CC(evmptrld_status == EVMPTRLD_VMFAIL)) {
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003517 return nested_vmx_failInvalid(vcpu);
3518 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003519
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02003520 if (CC(!evmptr_is_valid(vmx->nested.hv_evmcs_vmptr) &&
3521 vmx->nested.current_vmptr == -1ull))
Sean Christopherson55d23752018-12-03 13:53:18 -08003522 return nested_vmx_failInvalid(vcpu);
3523
3524 vmcs12 = get_vmcs12(vcpu);
3525
3526 /*
3527 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
3528 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
3529 * rather than RFLAGS.ZF, and no error number is stored to the
3530 * VM-instruction error field.
3531 */
Sean Christophersonfc595f32020-08-12 11:06:15 -07003532 if (CC(vmcs12->hdr.shadow_vmcs))
Sean Christopherson55d23752018-12-03 13:53:18 -08003533 return nested_vmx_failInvalid(vcpu);
3534
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02003535 if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) {
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02003536 copy_enlightened_to_vmcs12(vmx, vmx->nested.hv_evmcs->hv_clean_fields);
Sean Christopherson55d23752018-12-03 13:53:18 -08003537 /* Enlightened VMCS doesn't have launch state */
3538 vmcs12->launch_state = !launch;
3539 } else if (enable_shadow_vmcs) {
3540 copy_shadow_to_vmcs12(vmx);
3541 }
3542
3543 /*
3544 * The nested entry process starts with enforcing various prerequisites
3545 * on vmcs12 as required by the Intel SDM, and act appropriately when
3546 * they fail: As the SDM explains, some conditions should cause the
3547 * instruction to fail, while others will cause the instruction to seem
3548 * to succeed, but return an EXIT_REASON_INVALID_STATE.
3549 * To speed up the normal (success) code path, we should avoid checking
3550 * for misconfigurations which will anyway be caught by the processor
3551 * when using the merged vmcs02.
3552 */
Sean Christophersonfc595f32020-08-12 11:06:15 -07003553 if (CC(interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS))
Sean Christophersonb2656e42020-06-08 18:56:07 -07003554 return nested_vmx_fail(vcpu, VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
Sean Christopherson55d23752018-12-03 13:53:18 -08003555
Sean Christophersonfc595f32020-08-12 11:06:15 -07003556 if (CC(vmcs12->launch_state == launch))
Sean Christophersonb2656e42020-06-08 18:56:07 -07003557 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08003558 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
3559 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
3560
Paolo Bonzini98d9e852019-04-12 10:19:57 +02003561 if (nested_vmx_check_controls(vcpu, vmcs12))
Sean Christophersonb2656e42020-06-08 18:56:07 -07003562 return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Sean Christopherson5478ba32019-04-11 12:18:06 -07003563
Paolo Bonzini98d9e852019-04-12 10:19:57 +02003564 if (nested_vmx_check_host_state(vcpu, vmcs12))
Sean Christophersonb2656e42020-06-08 18:56:07 -07003565 return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
Sean Christopherson55d23752018-12-03 13:53:18 -08003566
3567 /*
3568 * We're finally done with prerequisite checking, and can start with
3569 * the nested entry.
3570 */
3571 vmx->nested.nested_run_pending = 1;
Peter Shier850448f2020-05-26 14:51:06 -07003572 vmx->nested.has_preemption_timer_deadline = false;
Jim Mattson671ddc72019-10-15 10:44:05 -07003573 status = nested_vmx_enter_non_root_mode(vcpu, true);
3574 if (unlikely(status != NVMX_VMENTRY_SUCCESS))
3575 goto vmentry_failed;
Sean Christopherson55d23752018-12-03 13:53:18 -08003576
Sean Christopherson25bb2cf2020-08-12 10:51:29 -07003577 /* Emulate processing of posted interrupts on VM-Enter. */
3578 if (nested_cpu_has_posted_intr(vmcs12) &&
3579 kvm_apic_has_interrupt(vcpu) == vmx->nested.posted_intr_nv) {
3580 vmx->nested.pi_pending = true;
3581 kvm_make_request(KVM_REQ_EVENT, vcpu);
3582 kvm_apic_clear_irr(vcpu, vmx->nested.posted_intr_nv);
3583 }
3584
Sean Christopherson55d23752018-12-03 13:53:18 -08003585 /* Hide L1D cache contents from the nested guest. */
3586 vmx->vcpu.arch.l1tf_flush_l1d = true;
3587
3588 /*
3589 * Must happen outside of nested_vmx_enter_non_root_mode() as it will
3590 * also be used as part of restoring nVMX state for
3591 * snapshot restore (migration).
3592 *
3593 * In this flow, it is assumed that vmcs12 cache was
Ingo Molnar163b0992021-03-21 22:28:53 +01003594 * transferred as part of captured nVMX state and should
Sean Christopherson55d23752018-12-03 13:53:18 -08003595 * therefore not be read from guest memory (which may not
3596 * exist on destination host yet).
3597 */
3598 nested_cache_shadow_vmcs12(vcpu, vmcs12);
3599
Yadong Qibf0cd882020-11-06 14:51:22 +08003600 switch (vmcs12->guest_activity_state) {
3601 case GUEST_ACTIVITY_HLT:
3602 /*
3603 * If we're entering a halted L2 vcpu and the L2 vcpu won't be
3604 * awakened by event injection or by an NMI-window VM-exit or
3605 * by an interrupt-window VM-exit, halt the vcpu.
3606 */
3607 if (!(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) &&
3608 !nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING) &&
3609 !(nested_cpu_has(vmcs12, CPU_BASED_INTR_WINDOW_EXITING) &&
3610 (vmcs12->guest_rflags & X86_EFLAGS_IF))) {
3611 vmx->nested.nested_run_pending = 0;
3612 return kvm_vcpu_halt(vcpu);
3613 }
3614 break;
3615 case GUEST_ACTIVITY_WAIT_SIPI:
Sean Christopherson55d23752018-12-03 13:53:18 -08003616 vmx->nested.nested_run_pending = 0;
Yadong Qibf0cd882020-11-06 14:51:22 +08003617 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
3618 break;
3619 default:
3620 break;
Sean Christopherson55d23752018-12-03 13:53:18 -08003621 }
Yadong Qibf0cd882020-11-06 14:51:22 +08003622
Sean Christopherson55d23752018-12-03 13:53:18 -08003623 return 1;
Jim Mattson671ddc72019-10-15 10:44:05 -07003624
3625vmentry_failed:
3626 vmx->nested.nested_run_pending = 0;
3627 if (status == NVMX_VMENTRY_KVM_INTERNAL_ERROR)
3628 return 0;
3629 if (status == NVMX_VMENTRY_VMEXIT)
3630 return 1;
3631 WARN_ON_ONCE(status != NVMX_VMENTRY_VMFAIL);
Sean Christophersonb2656e42020-06-08 18:56:07 -07003632 return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Sean Christopherson55d23752018-12-03 13:53:18 -08003633}
3634
3635/*
3636 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
Miaohe Lin67b0ae42019-12-11 14:26:22 +08003637 * because L2 may have changed some cr0 bits directly (CR0_GUEST_HOST_MASK).
Sean Christopherson55d23752018-12-03 13:53:18 -08003638 * This function returns the new value we should put in vmcs12.guest_cr0.
3639 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
3640 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
3641 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
3642 * didn't trap the bit, because if L1 did, so would L0).
3643 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
3644 * been modified by L2, and L1 knows it. So just leave the old value of
3645 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
3646 * isn't relevant, because if L0 traps this bit it can set it to anything.
3647 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
3648 * changed these bits, and therefore they need to be updated, but L0
3649 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
3650 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
3651 */
3652static inline unsigned long
3653vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
3654{
3655 return
3656 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
3657 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
3658 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
3659 vcpu->arch.cr0_guest_owned_bits));
3660}
3661
3662static inline unsigned long
3663vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
3664{
3665 return
3666 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
3667 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
3668 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
3669 vcpu->arch.cr4_guest_owned_bits));
3670}
3671
3672static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
3673 struct vmcs12 *vmcs12)
3674{
3675 u32 idt_vectoring;
3676 unsigned int nr;
3677
3678 if (vcpu->arch.exception.injected) {
3679 nr = vcpu->arch.exception.nr;
3680 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
3681
3682 if (kvm_exception_is_soft(nr)) {
3683 vmcs12->vm_exit_instruction_len =
3684 vcpu->arch.event_exit_inst_len;
3685 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
3686 } else
3687 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
3688
3689 if (vcpu->arch.exception.has_error_code) {
3690 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
3691 vmcs12->idt_vectoring_error_code =
3692 vcpu->arch.exception.error_code;
3693 }
3694
3695 vmcs12->idt_vectoring_info_field = idt_vectoring;
3696 } else if (vcpu->arch.nmi_injected) {
3697 vmcs12->idt_vectoring_info_field =
3698 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
3699 } else if (vcpu->arch.interrupt.injected) {
3700 nr = vcpu->arch.interrupt.nr;
3701 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
3702
3703 if (vcpu->arch.interrupt.soft) {
3704 idt_vectoring |= INTR_TYPE_SOFT_INTR;
3705 vmcs12->vm_entry_instruction_len =
3706 vcpu->arch.event_exit_inst_len;
3707 } else
3708 idt_vectoring |= INTR_TYPE_EXT_INTR;
3709
3710 vmcs12->idt_vectoring_info_field = idt_vectoring;
3711 }
3712}
3713
3714
Paolo Bonzini96b100c2020-03-17 18:32:50 +01003715void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08003716{
3717 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3718 gfn_t gfn;
3719
3720 /*
3721 * Don't need to mark the APIC access page dirty; it is never
3722 * written to by the CPU during APIC virtualization.
3723 */
3724
3725 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
3726 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
3727 kvm_vcpu_mark_page_dirty(vcpu, gfn);
3728 }
3729
3730 if (nested_cpu_has_posted_intr(vmcs12)) {
3731 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
3732 kvm_vcpu_mark_page_dirty(vcpu, gfn);
3733 }
3734}
3735
Jim Mattson650293c2021-06-04 10:26:02 -07003736static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08003737{
3738 struct vcpu_vmx *vmx = to_vmx(vcpu);
3739 int max_irr;
3740 void *vapic_page;
3741 u16 status;
3742
Jim Mattson966eefb2021-06-04 10:26:06 -07003743 if (!vmx->nested.pi_pending)
Jim Mattson650293c2021-06-04 10:26:02 -07003744 return 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08003745
Jim Mattson966eefb2021-06-04 10:26:06 -07003746 if (!vmx->nested.pi_desc)
3747 goto mmio_needed;
3748
Sean Christopherson55d23752018-12-03 13:53:18 -08003749 vmx->nested.pi_pending = false;
Jim Mattson966eefb2021-06-04 10:26:06 -07003750
Sean Christopherson55d23752018-12-03 13:53:18 -08003751 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
Jim Mattson650293c2021-06-04 10:26:02 -07003752 return 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08003753
3754 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
3755 if (max_irr != 256) {
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003756 vapic_page = vmx->nested.virtual_apic_map.hva;
3757 if (!vapic_page)
Jim Mattson0fe998b2021-06-04 10:26:05 -07003758 goto mmio_needed;
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003759
Sean Christopherson55d23752018-12-03 13:53:18 -08003760 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
3761 vapic_page, &max_irr);
Sean Christopherson55d23752018-12-03 13:53:18 -08003762 status = vmcs_read16(GUEST_INTR_STATUS);
3763 if ((u8)max_irr > ((u8)status & 0xff)) {
3764 status &= ~0xff;
3765 status |= (u8)max_irr;
3766 vmcs_write16(GUEST_INTR_STATUS, status);
3767 }
3768 }
3769
3770 nested_mark_vmcs12_pages_dirty(vcpu);
Jim Mattson650293c2021-06-04 10:26:02 -07003771 return 0;
Jim Mattson0fe998b2021-06-04 10:26:05 -07003772
3773mmio_needed:
3774 kvm_handle_memory_failure(vcpu, X86EMUL_IO_NEEDED, NULL);
3775 return -ENXIO;
Sean Christopherson55d23752018-12-03 13:53:18 -08003776}
3777
3778static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3779 unsigned long exit_qual)
3780{
3781 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3782 unsigned int nr = vcpu->arch.exception.nr;
3783 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3784
3785 if (vcpu->arch.exception.has_error_code) {
3786 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3787 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3788 }
3789
3790 if (kvm_exception_is_soft(nr))
3791 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3792 else
3793 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3794
3795 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3796 vmx_get_nmi_mask(vcpu))
3797 intr_info |= INTR_INFO_UNBLOCK_NMI;
3798
3799 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3800}
3801
Oliver Upton684c0422020-02-07 02:36:05 -08003802/*
3803 * Returns true if a debug trap is pending delivery.
3804 *
3805 * In KVM, debug traps bear an exception payload. As such, the class of a #DB
3806 * exception may be inferred from the presence of an exception payload.
3807 */
3808static inline bool vmx_pending_dbg_trap(struct kvm_vcpu *vcpu)
3809{
3810 return vcpu->arch.exception.pending &&
3811 vcpu->arch.exception.nr == DB_VECTOR &&
3812 vcpu->arch.exception.payload;
3813}
3814
3815/*
3816 * Certain VM-exits set the 'pending debug exceptions' field to indicate a
3817 * recognized #DB (data or single-step) that has yet to be delivered. Since KVM
3818 * represents these debug traps with a payload that is said to be compatible
3819 * with the 'pending debug exceptions' field, write the payload to the VMCS
3820 * field if a VM-exit is delivered before the debug trap.
3821 */
3822static void nested_vmx_update_pending_dbg(struct kvm_vcpu *vcpu)
3823{
3824 if (vmx_pending_dbg_trap(vcpu))
3825 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
3826 vcpu->arch.exception.payload);
3827}
3828
Sean Christophersond2060bd2020-04-22 19:25:39 -07003829static bool nested_vmx_preemption_timer_pending(struct kvm_vcpu *vcpu)
3830{
3831 return nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
3832 to_vmx(vcpu)->nested.preemption_timer_expired;
3833}
3834
Sean Christophersona1c77ab2020-03-02 22:27:35 -08003835static int vmx_check_nested_events(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08003836{
3837 struct vcpu_vmx *vmx = to_vmx(vcpu);
3838 unsigned long exit_qual;
3839 bool block_nested_events =
3840 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003841 bool mtf_pending = vmx->nested.mtf_pending;
Liran Alon4b9852f2019-08-26 13:24:49 +03003842 struct kvm_lapic *apic = vcpu->arch.apic;
3843
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003844 /*
3845 * Clear the MTF state. If a higher priority VM-exit is delivered first,
3846 * this state is discarded.
3847 */
Oliver Upton5c8beb42020-04-06 20:12:37 +00003848 if (!block_nested_events)
3849 vmx->nested.mtf_pending = false;
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003850
Liran Alon4b9852f2019-08-26 13:24:49 +03003851 if (lapic_in_kernel(vcpu) &&
3852 test_bit(KVM_APIC_INIT, &apic->pending_events)) {
3853 if (block_nested_events)
3854 return -EBUSY;
Oliver Upton684c0422020-02-07 02:36:05 -08003855 nested_vmx_update_pending_dbg(vcpu);
Liran Alone64a8502019-11-11 14:16:05 +02003856 clear_bit(KVM_APIC_INIT, &apic->pending_events);
Yadong Qibf0cd882020-11-06 14:51:22 +08003857 if (vcpu->arch.mp_state != KVM_MP_STATE_INIT_RECEIVED)
3858 nested_vmx_vmexit(vcpu, EXIT_REASON_INIT_SIGNAL, 0, 0);
3859 return 0;
3860 }
3861
3862 if (lapic_in_kernel(vcpu) &&
3863 test_bit(KVM_APIC_SIPI, &apic->pending_events)) {
3864 if (block_nested_events)
3865 return -EBUSY;
3866
3867 clear_bit(KVM_APIC_SIPI, &apic->pending_events);
3868 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED)
3869 nested_vmx_vmexit(vcpu, EXIT_REASON_SIPI_SIGNAL, 0,
3870 apic->sipi_vector & 0xFFUL);
Liran Alon4b9852f2019-08-26 13:24:49 +03003871 return 0;
3872 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003873
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003874 /*
3875 * Process any exceptions that are not debug traps before MTF.
Maxim Levitsky4020da32021-04-01 17:38:14 +03003876 *
3877 * Note that only a pending nested run can block a pending exception.
3878 * Otherwise an injected NMI/interrupt should either be
3879 * lost or delivered to the nested hypervisor in the IDT_VECTORING_INFO,
3880 * while delivering the pending exception.
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003881 */
Maxim Levitsky4020da32021-04-01 17:38:14 +03003882
Sean Christopherson6ce347a2020-04-22 19:25:38 -07003883 if (vcpu->arch.exception.pending && !vmx_pending_dbg_trap(vcpu)) {
Maxim Levitsky4020da32021-04-01 17:38:14 +03003884 if (vmx->nested.nested_run_pending)
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003885 return -EBUSY;
Sean Christopherson6ce347a2020-04-22 19:25:38 -07003886 if (!nested_vmx_check_exception(vcpu, &exit_qual))
3887 goto no_vmexit;
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003888 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
3889 return 0;
3890 }
3891
3892 if (mtf_pending) {
3893 if (block_nested_events)
3894 return -EBUSY;
3895 nested_vmx_update_pending_dbg(vcpu);
3896 nested_vmx_vmexit(vcpu, EXIT_REASON_MONITOR_TRAP_FLAG, 0, 0);
3897 return 0;
3898 }
3899
Sean Christopherson6ce347a2020-04-22 19:25:38 -07003900 if (vcpu->arch.exception.pending) {
Maxim Levitsky4020da32021-04-01 17:38:14 +03003901 if (vmx->nested.nested_run_pending)
Sean Christopherson55d23752018-12-03 13:53:18 -08003902 return -EBUSY;
Sean Christopherson6ce347a2020-04-22 19:25:38 -07003903 if (!nested_vmx_check_exception(vcpu, &exit_qual))
3904 goto no_vmexit;
Sean Christopherson55d23752018-12-03 13:53:18 -08003905 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
3906 return 0;
3907 }
3908
Sean Christophersond2060bd2020-04-22 19:25:39 -07003909 if (nested_vmx_preemption_timer_pending(vcpu)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08003910 if (block_nested_events)
3911 return -EBUSY;
3912 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
3913 return 0;
3914 }
3915
Sean Christopherson1cd2f0b2020-04-22 19:25:46 -07003916 if (vcpu->arch.smi_pending && !is_smm(vcpu)) {
3917 if (block_nested_events)
3918 return -EBUSY;
3919 goto no_vmexit;
3920 }
3921
Sean Christopherson15ff0b42020-04-22 19:25:45 -07003922 if (vcpu->arch.nmi_pending && !vmx_nmi_blocked(vcpu)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08003923 if (block_nested_events)
3924 return -EBUSY;
Sean Christopherson15ff0b42020-04-22 19:25:45 -07003925 if (!nested_exit_on_nmi(vcpu))
3926 goto no_vmexit;
3927
Sean Christopherson55d23752018-12-03 13:53:18 -08003928 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
3929 NMI_VECTOR | INTR_TYPE_NMI_INTR |
3930 INTR_INFO_VALID_MASK, 0);
3931 /*
3932 * The NMI-triggered VM exit counts as injection:
3933 * clear this one and block further NMIs.
3934 */
3935 vcpu->arch.nmi_pending = 0;
3936 vmx_set_nmi_mask(vcpu, true);
3937 return 0;
3938 }
3939
Sean Christopherson15ff0b42020-04-22 19:25:45 -07003940 if (kvm_cpu_has_interrupt(vcpu) && !vmx_interrupt_blocked(vcpu)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08003941 if (block_nested_events)
3942 return -EBUSY;
Sean Christopherson15ff0b42020-04-22 19:25:45 -07003943 if (!nested_exit_on_intr(vcpu))
3944 goto no_vmexit;
Sean Christopherson55d23752018-12-03 13:53:18 -08003945 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
3946 return 0;
3947 }
3948
Sean Christopherson6ce347a2020-04-22 19:25:38 -07003949no_vmexit:
Jim Mattson650293c2021-06-04 10:26:02 -07003950 return vmx_complete_nested_posted_interrupt(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08003951}
3952
3953static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
3954{
3955 ktime_t remaining =
3956 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
3957 u64 value;
3958
3959 if (ktime_to_ns(remaining) <= 0)
3960 return 0;
3961
3962 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
3963 do_div(value, 1000000);
3964 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
3965}
3966
Sean Christopherson7952d762019-05-07 08:36:29 -07003967static bool is_vmcs12_ext_field(unsigned long field)
Sean Christopherson55d23752018-12-03 13:53:18 -08003968{
Sean Christopherson7952d762019-05-07 08:36:29 -07003969 switch (field) {
3970 case GUEST_ES_SELECTOR:
3971 case GUEST_CS_SELECTOR:
3972 case GUEST_SS_SELECTOR:
3973 case GUEST_DS_SELECTOR:
3974 case GUEST_FS_SELECTOR:
3975 case GUEST_GS_SELECTOR:
3976 case GUEST_LDTR_SELECTOR:
3977 case GUEST_TR_SELECTOR:
3978 case GUEST_ES_LIMIT:
3979 case GUEST_CS_LIMIT:
3980 case GUEST_SS_LIMIT:
3981 case GUEST_DS_LIMIT:
3982 case GUEST_FS_LIMIT:
3983 case GUEST_GS_LIMIT:
3984 case GUEST_LDTR_LIMIT:
3985 case GUEST_TR_LIMIT:
3986 case GUEST_GDTR_LIMIT:
3987 case GUEST_IDTR_LIMIT:
3988 case GUEST_ES_AR_BYTES:
3989 case GUEST_DS_AR_BYTES:
3990 case GUEST_FS_AR_BYTES:
3991 case GUEST_GS_AR_BYTES:
3992 case GUEST_LDTR_AR_BYTES:
3993 case GUEST_TR_AR_BYTES:
3994 case GUEST_ES_BASE:
3995 case GUEST_CS_BASE:
3996 case GUEST_SS_BASE:
3997 case GUEST_DS_BASE:
3998 case GUEST_FS_BASE:
3999 case GUEST_GS_BASE:
4000 case GUEST_LDTR_BASE:
4001 case GUEST_TR_BASE:
4002 case GUEST_GDTR_BASE:
4003 case GUEST_IDTR_BASE:
4004 case GUEST_PENDING_DBG_EXCEPTIONS:
4005 case GUEST_BNDCFGS:
4006 return true;
4007 default:
4008 break;
4009 }
Sean Christopherson55d23752018-12-03 13:53:18 -08004010
Sean Christopherson7952d762019-05-07 08:36:29 -07004011 return false;
4012}
4013
4014static void sync_vmcs02_to_vmcs12_rare(struct kvm_vcpu *vcpu,
4015 struct vmcs12 *vmcs12)
4016{
4017 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08004018
4019 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
4020 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
4021 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
4022 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
4023 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
4024 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
4025 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
4026 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
4027 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
4028 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
4029 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
4030 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
4031 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
4032 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
4033 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
4034 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
4035 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
4036 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
4037 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
Sean Christopherson55d23752018-12-03 13:53:18 -08004038 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
4039 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
4040 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
4041 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
4042 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
4043 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
4044 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
4045 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
4046 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
4047 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
4048 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
4049 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
4050 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
4051 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
4052 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
Sean Christopherson7952d762019-05-07 08:36:29 -07004053 vmcs12->guest_pending_dbg_exceptions =
4054 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
4055 if (kvm_mpx_supported())
4056 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
4057
4058 vmx->nested.need_sync_vmcs02_to_vmcs12_rare = false;
4059}
4060
4061static void copy_vmcs02_to_vmcs12_rare(struct kvm_vcpu *vcpu,
4062 struct vmcs12 *vmcs12)
4063{
4064 struct vcpu_vmx *vmx = to_vmx(vcpu);
4065 int cpu;
4066
4067 if (!vmx->nested.need_sync_vmcs02_to_vmcs12_rare)
4068 return;
4069
4070
4071 WARN_ON_ONCE(vmx->loaded_vmcs != &vmx->vmcs01);
4072
4073 cpu = get_cpu();
4074 vmx->loaded_vmcs = &vmx->nested.vmcs02;
Sean Christopherson1af1bb02020-05-06 16:58:50 -07004075 vmx_vcpu_load_vmcs(vcpu, cpu, &vmx->vmcs01);
Sean Christopherson7952d762019-05-07 08:36:29 -07004076
4077 sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
4078
4079 vmx->loaded_vmcs = &vmx->vmcs01;
Sean Christopherson1af1bb02020-05-06 16:58:50 -07004080 vmx_vcpu_load_vmcs(vcpu, cpu, &vmx->nested.vmcs02);
Sean Christopherson7952d762019-05-07 08:36:29 -07004081 put_cpu();
4082}
4083
4084/*
4085 * Update the guest state fields of vmcs12 to reflect changes that
4086 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
4087 * VM-entry controls is also updated, since this is really a guest
4088 * state bit.)
4089 */
4090static void sync_vmcs02_to_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
4091{
4092 struct vcpu_vmx *vmx = to_vmx(vcpu);
4093
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02004094 if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
Sean Christopherson7952d762019-05-07 08:36:29 -07004095 sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
4096
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02004097 vmx->nested.need_sync_vmcs02_to_vmcs12_rare =
4098 !evmptr_is_valid(vmx->nested.hv_evmcs_vmptr);
Sean Christopherson7952d762019-05-07 08:36:29 -07004099
4100 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
4101 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
4102
4103 vmcs12->guest_rsp = kvm_rsp_read(vcpu);
4104 vmcs12->guest_rip = kvm_rip_read(vcpu);
4105 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
4106
4107 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
4108 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
Sean Christopherson55d23752018-12-03 13:53:18 -08004109
4110 vmcs12->guest_interruptibility_info =
4111 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
Sean Christopherson7952d762019-05-07 08:36:29 -07004112
Sean Christopherson55d23752018-12-03 13:53:18 -08004113 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
4114 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
Yadong Qibf0cd882020-11-06 14:51:22 +08004115 else if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED)
4116 vmcs12->guest_activity_state = GUEST_ACTIVITY_WAIT_SIPI;
Sean Christopherson55d23752018-12-03 13:53:18 -08004117 else
4118 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
4119
Paolo Bonzinib4b65b52019-01-29 19:12:35 +01004120 if (nested_cpu_has_preemption_timer(vmcs12) &&
Peter Shier850448f2020-05-26 14:51:06 -07004121 vmcs12->vm_exit_controls & VM_EXIT_SAVE_VMX_PREEMPTION_TIMER &&
4122 !vmx->nested.nested_run_pending)
4123 vmcs12->vmx_preemption_timer_value =
4124 vmx_get_preemption_timer_value(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08004125
4126 /*
4127 * In some cases (usually, nested EPT), L2 is allowed to change its
4128 * own CR3 without exiting. If it has changed it, we must keep it.
4129 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
4130 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
4131 *
4132 * Additionally, restore L2's PDPTR to vmcs12.
4133 */
4134 if (enable_ept) {
4135 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Sean Christophersonc7554efc2019-05-07 09:06:40 -07004136 if (nested_cpu_has_ept(vmcs12) && is_pae_paging(vcpu)) {
4137 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
4138 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
4139 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
4140 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
4141 }
Sean Christopherson55d23752018-12-03 13:53:18 -08004142 }
4143
4144 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
4145
4146 if (nested_cpu_has_vid(vmcs12))
4147 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
4148
4149 vmcs12->vm_entry_controls =
4150 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
4151 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
4152
Sean Christopherson699a1ac2019-05-07 09:06:37 -07004153 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS)
Sean Christopherson55d23752018-12-03 13:53:18 -08004154 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
Sean Christopherson55d23752018-12-03 13:53:18 -08004155
Sean Christopherson55d23752018-12-03 13:53:18 -08004156 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
4157 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Sean Christopherson55d23752018-12-03 13:53:18 -08004158}
4159
4160/*
4161 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
4162 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
4163 * and this function updates it to reflect the changes to the guest state while
4164 * L2 was running (and perhaps made some exits which were handled directly by L0
4165 * without going back to L1), and to reflect the exit reason.
4166 * Note that we do not have to copy here all VMCS fields, just those that
4167 * could have changed by the L2 guest or the exit - i.e., the guest-state and
4168 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
4169 * which already writes to vmcs12 directly.
4170 */
4171static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004172 u32 vm_exit_reason, u32 exit_intr_info,
Sean Christopherson55d23752018-12-03 13:53:18 -08004173 unsigned long exit_qualification)
4174{
Sean Christopherson55d23752018-12-03 13:53:18 -08004175 /* update exit information fields: */
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004176 vmcs12->vm_exit_reason = vm_exit_reason;
Sean Christopherson3c0c2ad2021-04-12 16:21:37 +12004177 if (to_vmx(vcpu)->exit_reason.enclave_mode)
4178 vmcs12->vm_exit_reason |= VMX_EXIT_REASONS_SGX_ENCLAVE_MODE;
Sean Christopherson55d23752018-12-03 13:53:18 -08004179 vmcs12->exit_qualification = exit_qualification;
4180 vmcs12->vm_exit_intr_info = exit_intr_info;
4181
4182 vmcs12->idt_vectoring_info_field = 0;
4183 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
4184 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
4185
4186 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
4187 vmcs12->launch_state = 1;
4188
4189 /* vm_entry_intr_info_field is cleared on exit. Emulate this
4190 * instead of reading the real value. */
4191 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
4192
4193 /*
4194 * Transfer the event that L0 or L1 may wanted to inject into
4195 * L2 to IDT_VECTORING_INFO_FIELD.
4196 */
4197 vmcs12_save_pending_event(vcpu, vmcs12);
Krish Sadhukhana0d4f802018-12-04 19:00:13 -05004198
4199 /*
4200 * According to spec, there's no need to store the guest's
4201 * MSRs if the exit is due to a VM-entry failure that occurs
4202 * during or after loading the guest state. Since this exit
4203 * does not fall in that category, we need to save the MSRs.
4204 */
4205 if (nested_vmx_store_msr(vcpu,
4206 vmcs12->vm_exit_msr_store_addr,
4207 vmcs12->vm_exit_msr_store_count))
4208 nested_vmx_abort(vcpu,
4209 VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Sean Christopherson55d23752018-12-03 13:53:18 -08004210 }
4211
4212 /*
4213 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
4214 * preserved above and would only end up incorrectly in L1.
4215 */
4216 vcpu->arch.nmi_injected = false;
4217 kvm_clear_exception_queue(vcpu);
4218 kvm_clear_interrupt_queue(vcpu);
4219}
4220
4221/*
4222 * A part of what we need to when the nested L2 guest exits and we want to
4223 * run its L1 parent, is to reset L1's guest state to the host state specified
4224 * in vmcs12.
4225 * This function is to be called not only on normal nested exit, but also on
4226 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
4227 * Failures During or After Loading Guest State").
4228 * This function should be called when the active VMCS is L1's (vmcs01).
4229 */
4230static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
4231 struct vmcs12 *vmcs12)
4232{
Sean Christopherson68cda402020-05-11 15:05:29 -07004233 enum vm_entry_failure_code ignored;
Sean Christopherson55d23752018-12-03 13:53:18 -08004234 struct kvm_segment seg;
Sean Christopherson55d23752018-12-03 13:53:18 -08004235
4236 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
4237 vcpu->arch.efer = vmcs12->host_ia32_efer;
4238 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
4239 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
4240 else
4241 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
4242 vmx_set_efer(vcpu, vcpu->arch.efer);
4243
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02004244 kvm_rsp_write(vcpu, vmcs12->host_rsp);
4245 kvm_rip_write(vcpu, vmcs12->host_rip);
Sean Christopherson55d23752018-12-03 13:53:18 -08004246 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
4247 vmx_set_interrupt_shadow(vcpu, 0);
4248
4249 /*
4250 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
4251 * actually changed, because vmx_set_cr0 refers to efer set above.
4252 *
4253 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
4254 * (KVM doesn't change it);
4255 */
Sean Christophersonfa71e952020-07-02 21:04:22 -07004256 vcpu->arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS;
Sean Christopherson55d23752018-12-03 13:53:18 -08004257 vmx_set_cr0(vcpu, vmcs12->host_cr0);
4258
4259 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
4260 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
4261 vmx_set_cr4(vcpu, vmcs12->host_cr4);
4262
4263 nested_ept_uninit_mmu_context(vcpu);
4264
4265 /*
4266 * Only PDPTE load can fail as the value of cr3 was checked on entry and
4267 * couldn't have changed.
4268 */
Maxim Levitsky0f857222021-06-07 12:02:00 +03004269 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, true, &ignored))
Sean Christopherson55d23752018-12-03 13:53:18 -08004270 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
4271
Sean Christopherson50b265a2020-03-20 14:28:19 -07004272 nested_vmx_transition_tlb_flush(vcpu, vmcs12, false);
Sean Christopherson55d23752018-12-03 13:53:18 -08004273
4274 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
4275 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
4276 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
4277 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
4278 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
4279 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
4280 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
4281
4282 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
4283 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
4284 vmcs_write64(GUEST_BNDCFGS, 0);
4285
4286 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
4287 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
4288 vcpu->arch.pat = vmcs12->host_ia32_pat;
4289 }
4290 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Oliver Uptond1968422019-12-13 16:33:58 -08004291 WARN_ON_ONCE(kvm_set_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL,
4292 vmcs12->host_ia32_perf_global_ctrl));
Sean Christopherson55d23752018-12-03 13:53:18 -08004293
4294 /* Set L1 segment info according to Intel SDM
4295 27.5.2 Loading Host Segment and Descriptor-Table Registers */
4296 seg = (struct kvm_segment) {
4297 .base = 0,
4298 .limit = 0xFFFFFFFF,
4299 .selector = vmcs12->host_cs_selector,
4300 .type = 11,
4301 .present = 1,
4302 .s = 1,
4303 .g = 1
4304 };
4305 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
4306 seg.l = 1;
4307 else
4308 seg.db = 1;
4309 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
4310 seg = (struct kvm_segment) {
4311 .base = 0,
4312 .limit = 0xFFFFFFFF,
4313 .type = 3,
4314 .present = 1,
4315 .s = 1,
4316 .db = 1,
4317 .g = 1
4318 };
4319 seg.selector = vmcs12->host_ds_selector;
4320 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
4321 seg.selector = vmcs12->host_es_selector;
4322 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
4323 seg.selector = vmcs12->host_ss_selector;
4324 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
4325 seg.selector = vmcs12->host_fs_selector;
4326 seg.base = vmcs12->host_fs_base;
4327 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
4328 seg.selector = vmcs12->host_gs_selector;
4329 seg.base = vmcs12->host_gs_base;
4330 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
4331 seg = (struct kvm_segment) {
4332 .base = vmcs12->host_tr_base,
4333 .limit = 0x67,
4334 .selector = vmcs12->host_tr_selector,
4335 .type = 11,
4336 .present = 1
4337 };
4338 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
4339
4340 kvm_set_dr(vcpu, 7, 0x400);
4341 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
4342
4343 if (cpu_has_vmx_msr_bitmap())
4344 vmx_update_msr_bitmap(vcpu);
4345
4346 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
4347 vmcs12->vm_exit_msr_load_count))
4348 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
4349}
4350
4351static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx)
4352{
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07004353 struct vmx_uret_msr *efer_msr;
Sean Christopherson55d23752018-12-03 13:53:18 -08004354 unsigned int i;
4355
4356 if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER)
4357 return vmcs_read64(GUEST_IA32_EFER);
4358
4359 if (cpu_has_load_ia32_efer())
4360 return host_efer;
4361
4362 for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) {
4363 if (vmx->msr_autoload.guest.val[i].index == MSR_EFER)
4364 return vmx->msr_autoload.guest.val[i].value;
4365 }
4366
Sean Christophersond85a8032020-09-23 11:04:06 -07004367 efer_msr = vmx_find_uret_msr(vmx, MSR_EFER);
Sean Christopherson55d23752018-12-03 13:53:18 -08004368 if (efer_msr)
4369 return efer_msr->data;
4370
4371 return host_efer;
4372}
4373
4374static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu)
4375{
4376 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4377 struct vcpu_vmx *vmx = to_vmx(vcpu);
4378 struct vmx_msr_entry g, h;
Sean Christopherson55d23752018-12-03 13:53:18 -08004379 gpa_t gpa;
4380 u32 i, j;
4381
4382 vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT);
4383
4384 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
4385 /*
4386 * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set
4387 * as vmcs01.GUEST_DR7 contains a userspace defined value
4388 * and vcpu->arch.dr7 is not squirreled away before the
4389 * nested VMENTER (not worth adding a variable in nested_vmx).
4390 */
4391 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
4392 kvm_set_dr(vcpu, 7, DR7_FIXED_1);
4393 else
4394 WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7)));
4395 }
4396
4397 /*
4398 * Note that calling vmx_set_{efer,cr0,cr4} is important as they
4399 * handle a variety of side effects to KVM's software model.
4400 */
4401 vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx));
4402
Sean Christophersonfa71e952020-07-02 21:04:22 -07004403 vcpu->arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS;
Sean Christopherson55d23752018-12-03 13:53:18 -08004404 vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW));
4405
4406 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
4407 vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW));
4408
4409 nested_ept_uninit_mmu_context(vcpu);
Sean Christophersonf087a022019-06-07 11:55:34 -07004410 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07004411 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Sean Christopherson55d23752018-12-03 13:53:18 -08004412
4413 /*
4414 * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs
4415 * from vmcs01 (if necessary). The PDPTRs are not loaded on
4416 * VMFail, like everything else we just need to ensure our
4417 * software model is up-to-date.
4418 */
Sean Christopherson9932b492020-04-15 13:34:50 -07004419 if (enable_ept && is_pae_paging(vcpu))
Sean Christophersonf087a022019-06-07 11:55:34 -07004420 ept_save_pdptrs(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08004421
4422 kvm_mmu_reset_context(vcpu);
4423
4424 if (cpu_has_vmx_msr_bitmap())
4425 vmx_update_msr_bitmap(vcpu);
4426
4427 /*
4428 * This nasty bit of open coding is a compromise between blindly
4429 * loading L1's MSRs using the exit load lists (incorrect emulation
4430 * of VMFail), leaving the nested VM's MSRs in the software model
4431 * (incorrect behavior) and snapshotting the modified MSRs (too
4432 * expensive since the lists are unbound by hardware). For each
4433 * MSR that was (prematurely) loaded from the nested VMEntry load
4434 * list, reload it from the exit load list if it exists and differs
4435 * from the guest value. The intent is to stuff host state as
4436 * silently as possible, not to fully process the exit load list.
4437 */
Sean Christopherson55d23752018-12-03 13:53:18 -08004438 for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) {
4439 gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g));
4440 if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) {
4441 pr_debug_ratelimited(
4442 "%s read MSR index failed (%u, 0x%08llx)\n",
4443 __func__, i, gpa);
4444 goto vmabort;
4445 }
4446
4447 for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) {
4448 gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h));
4449 if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) {
4450 pr_debug_ratelimited(
4451 "%s read MSR failed (%u, 0x%08llx)\n",
4452 __func__, j, gpa);
4453 goto vmabort;
4454 }
4455 if (h.index != g.index)
4456 continue;
4457 if (h.value == g.value)
4458 break;
4459
4460 if (nested_vmx_load_msr_check(vcpu, &h)) {
4461 pr_debug_ratelimited(
4462 "%s check failed (%u, 0x%x, 0x%x)\n",
4463 __func__, j, h.index, h.reserved);
4464 goto vmabort;
4465 }
4466
Sean Christophersonf20935d2019-09-05 14:22:54 -07004467 if (kvm_set_msr(vcpu, h.index, h.value)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08004468 pr_debug_ratelimited(
4469 "%s WRMSR failed (%u, 0x%x, 0x%llx)\n",
4470 __func__, j, h.index, h.value);
4471 goto vmabort;
4472 }
4473 }
4474 }
4475
4476 return;
4477
4478vmabort:
4479 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
4480}
4481
4482/*
4483 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
4484 * and modify vmcs12 to make it see what it would expect to see there if
4485 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
4486 */
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004487void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason,
Sean Christopherson55d23752018-12-03 13:53:18 -08004488 u32 exit_intr_info, unsigned long exit_qualification)
4489{
4490 struct vcpu_vmx *vmx = to_vmx(vcpu);
4491 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4492
4493 /* trying to cancel vmlaunch/vmresume is a bug */
4494 WARN_ON_ONCE(vmx->nested.nested_run_pending);
4495
Sean Christophersoncb6a32c2021-03-02 09:45:14 -08004496 /* Similarly, triple faults in L2 should never escape. */
4497 WARN_ON_ONCE(kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu));
4498
Vitaly Kuznetsovf5c7e842021-05-03 17:08:51 +02004499 if (kvm_check_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu)) {
4500 /*
4501 * KVM_REQ_GET_NESTED_STATE_PAGES is also used to map
4502 * Enlightened VMCS after migration and we still need to
4503 * do that when something is forcing L2->L1 exit prior to
4504 * the first L2 run.
4505 */
4506 (void)nested_get_evmcs_page(vcpu);
4507 }
Maxim Levitskyf2c7ef32021-01-07 11:38:51 +02004508
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07004509 /* Service the TLB flush request for L2 before switching to L1. */
4510 if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu))
4511 kvm_vcpu_flush_tlb_current(vcpu);
4512
Peter Shier43fea4e2020-08-20 16:05:45 -07004513 /*
4514 * VCPU_EXREG_PDPTR will be clobbered in arch/x86/kvm/vmx/vmx.h between
4515 * now and the new vmentry. Ensure that the VMCS02 PDPTR fields are
4516 * up-to-date before switching to L1.
4517 */
4518 if (enable_ept && is_pae_paging(vcpu))
4519 vmx_ept_load_pdptrs(vcpu);
4520
Sean Christopherson55d23752018-12-03 13:53:18 -08004521 leave_guest_mode(vcpu);
4522
Paolo Bonzinib4b65b52019-01-29 19:12:35 +01004523 if (nested_cpu_has_preemption_timer(vmcs12))
4524 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
4525
Ilias Stamatisd041b5e2021-05-26 19:44:17 +01004526 if (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETTING)) {
4527 vcpu->arch.tsc_offset = vcpu->arch.l1_tsc_offset;
4528 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_TSC_SCALING))
4529 vcpu->arch.tsc_scaling_ratio = vcpu->arch.l1_tsc_scaling_ratio;
4530 }
Sean Christopherson55d23752018-12-03 13:53:18 -08004531
4532 if (likely(!vmx->fail)) {
Sean Christopherson3731905ef2019-05-07 08:36:27 -07004533 sync_vmcs02_to_vmcs12(vcpu, vmcs12);
Sean Christophersonf4f83162019-05-07 08:36:26 -07004534
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004535 if (vm_exit_reason != -1)
4536 prepare_vmcs12(vcpu, vmcs12, vm_exit_reason,
4537 exit_intr_info, exit_qualification);
Sean Christopherson55d23752018-12-03 13:53:18 -08004538
4539 /*
Sean Christopherson3731905ef2019-05-07 08:36:27 -07004540 * Must happen outside of sync_vmcs02_to_vmcs12() as it will
Sean Christopherson55d23752018-12-03 13:53:18 -08004541 * also be used to capture vmcs12 cache as part of
4542 * capturing nVMX state for snapshot (migration).
4543 *
4544 * Otherwise, this flush will dirty guest memory at a
4545 * point it is already assumed by user-space to be
4546 * immutable.
4547 */
4548 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08004549 } else {
4550 /*
4551 * The only expected VM-instruction error is "VM entry with
4552 * invalid control field(s)." Anything else indicates a
4553 * problem with L0. And we should never get here with a
4554 * VMFail of any type if early consistency checks are enabled.
4555 */
4556 WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) !=
4557 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
4558 WARN_ON_ONCE(nested_early_check);
4559 }
4560
4561 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
4562
4563 /* Update any VMCS fields that might have changed while L2 ran */
4564 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
4565 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
4566 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Ilias Stamatis1ab92872021-06-07 11:54:38 +01004567 if (kvm_has_tsc_control)
4568 vmcs_write64(TSC_MULTIPLIER, vcpu->arch.tsc_scaling_ratio);
4569
Liran Alon02d496cf2019-11-11 14:30:55 +02004570 if (vmx->nested.l1_tpr_threshold != -1)
4571 vmcs_write32(TPR_THRESHOLD, vmx->nested.l1_tpr_threshold);
Sean Christopherson55d23752018-12-03 13:53:18 -08004572
Sean Christopherson55d23752018-12-03 13:53:18 -08004573 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
4574 vmx->nested.change_vmcs01_virtual_apic_mode = false;
4575 vmx_set_virtual_apic_mode(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08004576 }
4577
Makarand Sonarea85863c2021-02-12 16:50:12 -08004578 if (vmx->nested.update_vmcs01_cpu_dirty_logging) {
4579 vmx->nested.update_vmcs01_cpu_dirty_logging = false;
4580 vmx_update_cpu_dirty_logging(vcpu);
4581 }
4582
Sean Christopherson55d23752018-12-03 13:53:18 -08004583 /* Unpin physical memory we referred to in vmcs02 */
4584 if (vmx->nested.apic_access_page) {
Liran Alonb11494b2019-11-21 00:31:47 +02004585 kvm_release_page_clean(vmx->nested.apic_access_page);
Sean Christopherson55d23752018-12-03 13:53:18 -08004586 vmx->nested.apic_access_page = NULL;
4587 }
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01004588 kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map, true);
KarimAllah Ahmed3278e042019-01-31 21:24:38 +01004589 kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map, true);
4590 vmx->nested.pi_desc = NULL;
Sean Christopherson55d23752018-12-03 13:53:18 -08004591
Sean Christopherson1196cb92020-03-20 14:28:23 -07004592 if (vmx->nested.reload_vmcs01_apic_access_page) {
4593 vmx->nested.reload_vmcs01_apic_access_page = false;
4594 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
4595 }
Sean Christopherson55d23752018-12-03 13:53:18 -08004596
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004597 if ((vm_exit_reason != -1) &&
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02004598 (enable_shadow_vmcs || evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)))
Sean Christopherson3731905ef2019-05-07 08:36:27 -07004599 vmx->nested.need_vmcs12_to_shadow_sync = true;
Sean Christopherson55d23752018-12-03 13:53:18 -08004600
4601 /* in case we halted in L2 */
4602 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
4603
4604 if (likely(!vmx->fail)) {
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004605 if ((u16)vm_exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
Sean Christophersona1c77ab2020-03-02 22:27:35 -08004606 nested_exit_intr_ack_set(vcpu)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08004607 int irq = kvm_cpu_get_interrupt(vcpu);
4608 WARN_ON(irq < 0);
4609 vmcs12->vm_exit_intr_info = irq |
4610 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
4611 }
4612
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004613 if (vm_exit_reason != -1)
Sean Christopherson55d23752018-12-03 13:53:18 -08004614 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
4615 vmcs12->exit_qualification,
4616 vmcs12->idt_vectoring_info_field,
4617 vmcs12->vm_exit_intr_info,
4618 vmcs12->vm_exit_intr_error_code,
4619 KVM_ISA_VMX);
4620
4621 load_vmcs12_host_state(vcpu, vmcs12);
4622
4623 return;
4624 }
4625
4626 /*
4627 * After an early L2 VM-entry failure, we're now back
4628 * in L1 which thinks it just finished a VMLAUNCH or
4629 * VMRESUME instruction, so we need to set the failure
4630 * flag and the VM-instruction error field of the VMCS
4631 * accordingly, and skip the emulated instruction.
4632 */
Sean Christophersonb2656e42020-06-08 18:56:07 -07004633 (void)nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Sean Christopherson55d23752018-12-03 13:53:18 -08004634
4635 /*
4636 * Restore L1's host state to KVM's software model. We're here
4637 * because a consistency check was caught by hardware, which
4638 * means some amount of guest state has been propagated to KVM's
4639 * model and needs to be unwound to the host's state.
4640 */
4641 nested_vmx_restore_host_state(vcpu);
4642
4643 vmx->fail = 0;
4644}
4645
Sean Christophersoncb6a32c2021-03-02 09:45:14 -08004646static void nested_vmx_triple_fault(struct kvm_vcpu *vcpu)
4647{
4648 nested_vmx_vmexit(vcpu, EXIT_REASON_TRIPLE_FAULT, 0, 0);
4649}
4650
Sean Christopherson55d23752018-12-03 13:53:18 -08004651/*
4652 * Decode the memory-address operand of a vmx instruction, as recorded on an
4653 * exit caused by such an instruction (run by a guest hypervisor).
4654 * On success, returns 0. When the operand is invalid, returns 1 and throws
Miaohe Lin49f933d2020-02-27 11:20:54 +08004655 * #UD, #GP, or #SS.
Sean Christopherson55d23752018-12-03 13:53:18 -08004656 */
4657int get_vmx_mem_address(struct kvm_vcpu *vcpu, unsigned long exit_qualification,
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004658 u32 vmx_instruction_info, bool wr, int len, gva_t *ret)
Sean Christopherson55d23752018-12-03 13:53:18 -08004659{
4660 gva_t off;
4661 bool exn;
4662 struct kvm_segment s;
4663
4664 /*
4665 * According to Vol. 3B, "Information for VM Exits Due to Instruction
4666 * Execution", on an exit, vmx_instruction_info holds most of the
4667 * addressing components of the operand. Only the displacement part
4668 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
4669 * For how an actual address is calculated from all these components,
4670 * refer to Vol. 1, "Operand Addressing".
4671 */
4672 int scaling = vmx_instruction_info & 3;
4673 int addr_size = (vmx_instruction_info >> 7) & 7;
4674 bool is_reg = vmx_instruction_info & (1u << 10);
4675 int seg_reg = (vmx_instruction_info >> 15) & 7;
4676 int index_reg = (vmx_instruction_info >> 18) & 0xf;
4677 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
4678 int base_reg = (vmx_instruction_info >> 23) & 0xf;
4679 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
4680
4681 if (is_reg) {
4682 kvm_queue_exception(vcpu, UD_VECTOR);
4683 return 1;
4684 }
4685
4686 /* Addr = segment_base + offset */
4687 /* offset = base + [index * scale] + displacement */
4688 off = exit_qualification; /* holds the displacement */
Sean Christopherson946c5222019-01-23 14:39:23 -08004689 if (addr_size == 1)
4690 off = (gva_t)sign_extend64(off, 31);
4691 else if (addr_size == 0)
4692 off = (gva_t)sign_extend64(off, 15);
Sean Christopherson55d23752018-12-03 13:53:18 -08004693 if (base_is_valid)
4694 off += kvm_register_read(vcpu, base_reg);
4695 if (index_is_valid)
Miaohe Line6302692020-02-15 10:44:22 +08004696 off += kvm_register_read(vcpu, index_reg) << scaling;
Sean Christopherson55d23752018-12-03 13:53:18 -08004697 vmx_get_segment(vcpu, &s, seg_reg);
Sean Christopherson55d23752018-12-03 13:53:18 -08004698
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004699 /*
4700 * The effective address, i.e. @off, of a memory operand is truncated
4701 * based on the address size of the instruction. Note that this is
4702 * the *effective address*, i.e. the address prior to accounting for
4703 * the segment's base.
4704 */
Sean Christopherson55d23752018-12-03 13:53:18 -08004705 if (addr_size == 1) /* 32 bit */
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004706 off &= 0xffffffff;
4707 else if (addr_size == 0) /* 16 bit */
4708 off &= 0xffff;
Sean Christopherson55d23752018-12-03 13:53:18 -08004709
4710 /* Checks for #GP/#SS exceptions. */
4711 exn = false;
4712 if (is_long_mode(vcpu)) {
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004713 /*
4714 * The virtual/linear address is never truncated in 64-bit
4715 * mode, e.g. a 32-bit address size can yield a 64-bit virtual
4716 * address when using FS/GS with a non-zero base.
4717 */
Liran Alon6694e482019-07-15 18:47:44 +03004718 if (seg_reg == VCPU_SREG_FS || seg_reg == VCPU_SREG_GS)
4719 *ret = s.base + off;
4720 else
4721 *ret = off;
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004722
Sean Christopherson55d23752018-12-03 13:53:18 -08004723 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
4724 * non-canonical form. This is the only check on the memory
4725 * destination for long mode!
4726 */
4727 exn = is_noncanonical_address(*ret, vcpu);
Paolo Bonzinie0dfacb2019-01-30 17:25:38 +01004728 } else {
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004729 /*
4730 * When not in long mode, the virtual/linear address is
4731 * unconditionally truncated to 32 bits regardless of the
4732 * address size.
4733 */
4734 *ret = (s.base + off) & 0xffffffff;
4735
Sean Christopherson55d23752018-12-03 13:53:18 -08004736 /* Protected mode: apply checks for segment validity in the
4737 * following order:
4738 * - segment type check (#GP(0) may be thrown)
4739 * - usability check (#GP(0)/#SS(0))
4740 * - limit check (#GP(0)/#SS(0))
4741 */
4742 if (wr)
4743 /* #GP(0) if the destination operand is located in a
4744 * read-only data segment or any code segment.
4745 */
4746 exn = ((s.type & 0xa) == 0 || (s.type & 8));
4747 else
4748 /* #GP(0) if the source operand is located in an
4749 * execute-only code segment
4750 */
4751 exn = ((s.type & 0xa) == 8);
4752 if (exn) {
4753 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
4754 return 1;
4755 }
4756 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
4757 */
4758 exn = (s.unusable != 0);
Sean Christopherson34333cc2019-01-23 14:39:25 -08004759
4760 /*
4761 * Protected mode: #GP(0)/#SS(0) if the memory operand is
4762 * outside the segment limit. All CPUs that support VMX ignore
4763 * limit checks for flat segments, i.e. segments with base==0,
4764 * limit==0xffffffff and of type expand-up data or code.
Sean Christopherson55d23752018-12-03 13:53:18 -08004765 */
Sean Christopherson34333cc2019-01-23 14:39:25 -08004766 if (!(s.base == 0 && s.limit == 0xffffffff &&
4767 ((s.type & 8) || !(s.type & 4))))
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004768 exn = exn || ((u64)off + len - 1 > s.limit);
Sean Christopherson55d23752018-12-03 13:53:18 -08004769 }
4770 if (exn) {
4771 kvm_queue_exception_e(vcpu,
4772 seg_reg == VCPU_SREG_SS ?
4773 SS_VECTOR : GP_VECTOR,
4774 0);
4775 return 1;
4776 }
4777
4778 return 0;
4779}
4780
Oliver Upton03a8871a2019-11-13 16:17:20 -08004781void nested_vmx_pmu_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
4782{
4783 struct vcpu_vmx *vmx;
4784
4785 if (!nested_vmx_allowed(vcpu))
4786 return;
4787
4788 vmx = to_vmx(vcpu);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004789 if (kvm_x86_ops.pmu_ops->is_valid_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL)) {
Oliver Upton03a8871a2019-11-13 16:17:20 -08004790 vmx->nested.msrs.entry_ctls_high |=
4791 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
4792 vmx->nested.msrs.exit_ctls_high |=
4793 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
4794 } else {
4795 vmx->nested.msrs.entry_ctls_high &=
4796 ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
4797 vmx->nested.msrs.exit_ctls_high &=
Chenyi Qiangc6b177a2020-08-28 16:56:21 +08004798 ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
Oliver Upton03a8871a2019-11-13 16:17:20 -08004799 }
4800}
4801
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004802static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer,
4803 int *ret)
Sean Christopherson55d23752018-12-03 13:53:18 -08004804{
4805 gva_t gva;
4806 struct x86_exception e;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004807 int r;
Sean Christopherson55d23752018-12-03 13:53:18 -08004808
Sean Christopherson5addc232020-04-15 13:34:53 -07004809 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004810 vmcs_read32(VMX_INSTRUCTION_INFO), false,
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004811 sizeof(*vmpointer), &gva)) {
4812 *ret = 1;
4813 return -EINVAL;
4814 }
Sean Christopherson55d23752018-12-03 13:53:18 -08004815
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004816 r = kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e);
4817 if (r != X86EMUL_CONTINUE) {
Babu Moger3f3393b2020-09-11 14:29:05 -05004818 *ret = kvm_handle_memory_failure(vcpu, r, &e);
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004819 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08004820 }
4821
4822 return 0;
4823}
4824
4825/*
4826 * Allocate a shadow VMCS and associate it with the currently loaded
4827 * VMCS, unless such a shadow VMCS already exists. The newly allocated
4828 * VMCS is also VMCLEARed, so that it is ready for use.
4829 */
4830static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
4831{
4832 struct vcpu_vmx *vmx = to_vmx(vcpu);
4833 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
4834
4835 /*
4836 * We should allocate a shadow vmcs for vmcs01 only when L1
4837 * executes VMXON and free it when L1 executes VMXOFF.
4838 * As it is invalid to execute VMXON twice, we shouldn't reach
4839 * here when vmcs01 already have an allocated shadow vmcs.
4840 */
4841 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
4842
4843 if (!loaded_vmcs->shadow_vmcs) {
4844 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
4845 if (loaded_vmcs->shadow_vmcs)
4846 vmcs_clear(loaded_vmcs->shadow_vmcs);
4847 }
4848 return loaded_vmcs->shadow_vmcs;
4849}
4850
4851static int enter_vmx_operation(struct kvm_vcpu *vcpu)
4852{
4853 struct vcpu_vmx *vmx = to_vmx(vcpu);
4854 int r;
4855
4856 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
4857 if (r < 0)
4858 goto out_vmcs02;
4859
Ben Gardon41836832019-02-11 11:02:52 -08004860 vmx->nested.cached_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT);
Sean Christopherson55d23752018-12-03 13:53:18 -08004861 if (!vmx->nested.cached_vmcs12)
4862 goto out_cached_vmcs12;
4863
Ben Gardon41836832019-02-11 11:02:52 -08004864 vmx->nested.cached_shadow_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT);
Sean Christopherson55d23752018-12-03 13:53:18 -08004865 if (!vmx->nested.cached_shadow_vmcs12)
4866 goto out_cached_shadow_vmcs12;
4867
4868 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
4869 goto out_shadow_vmcs;
4870
4871 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
Jim Mattsonada00982020-05-08 13:36:42 -07004872 HRTIMER_MODE_ABS_PINNED);
Sean Christopherson55d23752018-12-03 13:53:18 -08004873 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
4874
4875 vmx->nested.vpid02 = allocate_vpid();
4876
4877 vmx->nested.vmcs02_initialized = false;
4878 vmx->nested.vmxon = true;
Luwei Kangee85dec2018-10-24 16:05:16 +08004879
Sean Christopherson2ef76192020-03-02 15:56:22 -08004880 if (vmx_pt_mode_is_host_guest()) {
Luwei Kangee85dec2018-10-24 16:05:16 +08004881 vmx->pt_desc.guest.ctl = 0;
Aaron Lewis476c9bd2020-09-25 16:34:18 +02004882 pt_update_intercept_for_msr(vcpu);
Luwei Kangee85dec2018-10-24 16:05:16 +08004883 }
4884
Sean Christopherson55d23752018-12-03 13:53:18 -08004885 return 0;
4886
4887out_shadow_vmcs:
4888 kfree(vmx->nested.cached_shadow_vmcs12);
4889
4890out_cached_shadow_vmcs12:
4891 kfree(vmx->nested.cached_vmcs12);
4892
4893out_cached_vmcs12:
4894 free_loaded_vmcs(&vmx->nested.vmcs02);
4895
4896out_vmcs02:
4897 return -ENOMEM;
4898}
4899
4900/*
4901 * Emulate the VMXON instruction.
4902 * Currently, we just remember that VMX is active, and do not save or even
4903 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
4904 * do not currently need to store anything in that guest-allocated memory
4905 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
4906 * argument is different from the VMXON pointer (which the spec says they do).
4907 */
4908static int handle_vmon(struct kvm_vcpu *vcpu)
4909{
4910 int ret;
4911 gpa_t vmptr;
KarimAllah Ahmed2e408932019-01-31 21:24:31 +01004912 uint32_t revision;
Sean Christopherson55d23752018-12-03 13:53:18 -08004913 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson32ad73d2019-12-20 20:44:55 -08004914 const u64 VMXON_NEEDED_FEATURES = FEAT_CTL_LOCKED
4915 | FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX;
Sean Christopherson55d23752018-12-03 13:53:18 -08004916
4917 /*
4918 * The Intel VMX Instruction Reference lists a bunch of bits that are
4919 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
Sean Christophersonc2fe3cd2020-10-06 18:44:15 -07004920 * 1 (see vmx_is_valid_cr4() for when we allow the guest to set this).
Sean Christopherson55d23752018-12-03 13:53:18 -08004921 * Otherwise, we should fail with #UD. But most faulting conditions
4922 * have already been checked by hardware, prior to the VM-exit for
4923 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
4924 * that bit set to 1 in non-root mode.
4925 */
4926 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
4927 kvm_queue_exception(vcpu, UD_VECTOR);
4928 return 1;
4929 }
4930
4931 /* CPL=0 must be checked manually. */
4932 if (vmx_get_cpl(vcpu)) {
4933 kvm_inject_gp(vcpu, 0);
4934 return 1;
4935 }
4936
4937 if (vmx->nested.vmxon)
Sean Christophersonb2656e42020-06-08 18:56:07 -07004938 return nested_vmx_fail(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Sean Christopherson55d23752018-12-03 13:53:18 -08004939
4940 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
4941 != VMXON_NEEDED_FEATURES) {
4942 kvm_inject_gp(vcpu, 0);
4943 return 1;
4944 }
4945
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004946 if (nested_vmx_get_vmptr(vcpu, &vmptr, &ret))
4947 return ret;
Sean Christopherson55d23752018-12-03 13:53:18 -08004948
4949 /*
4950 * SDM 3: 24.11.5
4951 * The first 4 bytes of VMXON region contain the supported
4952 * VMCS revision identifier
4953 *
4954 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
4955 * which replaces physical address width with 32
4956 */
KarimAllah Ahmede0bf2662019-01-31 21:24:43 +01004957 if (!page_address_valid(vcpu, vmptr))
Sean Christopherson55d23752018-12-03 13:53:18 -08004958 return nested_vmx_failInvalid(vcpu);
4959
KarimAllah Ahmed2e408932019-01-31 21:24:31 +01004960 if (kvm_read_guest(vcpu->kvm, vmptr, &revision, sizeof(revision)) ||
4961 revision != VMCS12_REVISION)
Sean Christopherson55d23752018-12-03 13:53:18 -08004962 return nested_vmx_failInvalid(vcpu);
4963
Sean Christopherson55d23752018-12-03 13:53:18 -08004964 vmx->nested.vmxon_ptr = vmptr;
4965 ret = enter_vmx_operation(vcpu);
4966 if (ret)
4967 return ret;
4968
4969 return nested_vmx_succeed(vcpu);
4970}
4971
4972static inline void nested_release_vmcs12(struct kvm_vcpu *vcpu)
4973{
4974 struct vcpu_vmx *vmx = to_vmx(vcpu);
4975
4976 if (vmx->nested.current_vmptr == -1ull)
4977 return;
4978
Sean Christopherson7952d762019-05-07 08:36:29 -07004979 copy_vmcs02_to_vmcs12_rare(vcpu, get_vmcs12(vcpu));
4980
Sean Christopherson55d23752018-12-03 13:53:18 -08004981 if (enable_shadow_vmcs) {
4982 /* copy to memory all shadowed fields in case
4983 they were modified */
4984 copy_shadow_to_vmcs12(vmx);
Sean Christopherson55d23752018-12-03 13:53:18 -08004985 vmx_disable_shadow_vmcs(vmx);
4986 }
4987 vmx->nested.posted_intr_nv = -1;
4988
4989 /* Flush VMCS12 to guest memory */
4990 kvm_vcpu_write_guest_page(vcpu,
4991 vmx->nested.current_vmptr >> PAGE_SHIFT,
4992 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
4993
4994 kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
4995
4996 vmx->nested.current_vmptr = -1ull;
4997}
4998
4999/* Emulate the VMXOFF instruction */
5000static int handle_vmoff(struct kvm_vcpu *vcpu)
5001{
5002 if (!nested_vmx_check_permission(vcpu))
5003 return 1;
Liran Alon4b9852f2019-08-26 13:24:49 +03005004
Sean Christopherson55d23752018-12-03 13:53:18 -08005005 free_nested(vcpu);
Liran Alon4b9852f2019-08-26 13:24:49 +03005006
5007 /* Process a latched INIT during time CPU was in VMX operation */
5008 kvm_make_request(KVM_REQ_EVENT, vcpu);
5009
Sean Christopherson55d23752018-12-03 13:53:18 -08005010 return nested_vmx_succeed(vcpu);
5011}
5012
5013/* Emulate the VMCLEAR instruction */
5014static int handle_vmclear(struct kvm_vcpu *vcpu)
5015{
5016 struct vcpu_vmx *vmx = to_vmx(vcpu);
5017 u32 zero = 0;
5018 gpa_t vmptr;
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02005019 u64 evmcs_gpa;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005020 int r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005021
5022 if (!nested_vmx_check_permission(vcpu))
5023 return 1;
5024
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005025 if (nested_vmx_get_vmptr(vcpu, &vmptr, &r))
5026 return r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005027
KarimAllah Ahmede0bf2662019-01-31 21:24:43 +01005028 if (!page_address_valid(vcpu, vmptr))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005029 return nested_vmx_fail(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
Sean Christopherson55d23752018-12-03 13:53:18 -08005030
5031 if (vmptr == vmx->nested.vmxon_ptr)
Sean Christophersonb2656e42020-06-08 18:56:07 -07005032 return nested_vmx_fail(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
Sean Christopherson55d23752018-12-03 13:53:18 -08005033
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02005034 /*
5035 * When Enlightened VMEntry is enabled on the calling CPU we treat
5036 * memory area pointer by vmptr as Enlightened VMCS (as there's no good
5037 * way to distinguish it from VMCS12) and we must not corrupt it by
5038 * writing to the non-existent 'launch_state' field. The area doesn't
5039 * have to be the currently active EVMCS on the calling CPU and there's
5040 * nothing KVM has to do to transition it from 'active' to 'non-active'
5041 * state. It is possible that the area will stay mapped as
5042 * vmx->nested.hv_evmcs but this shouldn't be a problem.
5043 */
5044 if (likely(!vmx->nested.enlightened_vmcs_enabled ||
5045 !nested_enlightened_vmentry(vcpu, &evmcs_gpa))) {
Sean Christopherson55d23752018-12-03 13:53:18 -08005046 if (vmptr == vmx->nested.current_vmptr)
5047 nested_release_vmcs12(vcpu);
5048
5049 kvm_vcpu_write_guest(vcpu,
5050 vmptr + offsetof(struct vmcs12,
5051 launch_state),
5052 &zero, sizeof(zero));
Vitaly Kuznetsov3b19b812021-05-26 15:20:21 +02005053 } else if (vmx->nested.hv_evmcs && vmptr == vmx->nested.hv_evmcs_vmptr) {
5054 nested_release_evmcs(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005055 }
5056
5057 return nested_vmx_succeed(vcpu);
5058}
5059
Sean Christopherson55d23752018-12-03 13:53:18 -08005060/* Emulate the VMLAUNCH instruction */
5061static int handle_vmlaunch(struct kvm_vcpu *vcpu)
5062{
5063 return nested_vmx_run(vcpu, true);
5064}
5065
5066/* Emulate the VMRESUME instruction */
5067static int handle_vmresume(struct kvm_vcpu *vcpu)
5068{
5069
5070 return nested_vmx_run(vcpu, false);
5071}
5072
5073static int handle_vmread(struct kvm_vcpu *vcpu)
5074{
Jim Mattsondd2d6042019-12-06 15:46:35 -08005075 struct vmcs12 *vmcs12 = is_guest_mode(vcpu) ? get_shadow_vmcs12(vcpu)
5076 : get_vmcs12(vcpu);
Sean Christopherson5addc232020-04-15 13:34:53 -07005077 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005078 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5079 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzinif7eea632019-09-14 00:26:27 +02005080 struct x86_exception e;
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005081 unsigned long field;
5082 u64 value;
5083 gva_t gva = 0;
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07005084 short offset;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005085 int len, r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005086
5087 if (!nested_vmx_check_permission(vcpu))
5088 return 1;
5089
Jim Mattsondd2d6042019-12-06 15:46:35 -08005090 /*
5091 * In VMX non-root operation, when the VMCS-link pointer is -1ull,
5092 * any VMREAD sets the ALU flags for VMfailInvalid.
5093 */
5094 if (vmx->nested.current_vmptr == -1ull ||
5095 (is_guest_mode(vcpu) &&
5096 get_vmcs12(vcpu)->vmcs_link_pointer == -1ull))
Sean Christopherson55d23752018-12-03 13:53:18 -08005097 return nested_vmx_failInvalid(vcpu);
5098
Sean Christopherson55d23752018-12-03 13:53:18 -08005099 /* Decode instruction info and find the field to read */
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005100 field = kvm_register_read(vcpu, (((instr_info) >> 28) & 0xf));
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07005101
5102 offset = vmcs_field_to_offset(field);
5103 if (offset < 0)
Sean Christophersonb2656e42020-06-08 18:56:07 -07005104 return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Sean Christopherson55d23752018-12-03 13:53:18 -08005105
Sean Christopherson7952d762019-05-07 08:36:29 -07005106 if (!is_guest_mode(vcpu) && is_vmcs12_ext_field(field))
5107 copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
5108
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005109 /* Read the field, zero-extended to a u64 value */
5110 value = vmcs12_read_any(vmcs12, field, offset);
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07005111
Sean Christopherson55d23752018-12-03 13:53:18 -08005112 /*
5113 * Now copy part of this value to register or memory, as requested.
5114 * Note that the number of bits actually copied is 32 or 64 depending
5115 * on the guest's mode (32 or 64 bit), not on the given field's length.
5116 */
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005117 if (instr_info & BIT(10)) {
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005118 kvm_register_write(vcpu, (((instr_info) >> 3) & 0xf), value);
Sean Christopherson55d23752018-12-03 13:53:18 -08005119 } else {
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005120 len = is_64_bit_mode(vcpu) ? 8 : 4;
Sean Christopherson55d23752018-12-03 13:53:18 -08005121 if (get_vmx_mem_address(vcpu, exit_qualification,
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005122 instr_info, true, len, &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08005123 return 1;
5124 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005125 r = kvm_write_guest_virt_system(vcpu, gva, &value, len, &e);
5126 if (r != X86EMUL_CONTINUE)
Babu Moger3f3393b2020-09-11 14:29:05 -05005127 return kvm_handle_memory_failure(vcpu, r, &e);
Sean Christopherson55d23752018-12-03 13:53:18 -08005128 }
5129
5130 return nested_vmx_succeed(vcpu);
5131}
5132
Sean Christophersone2174292019-05-07 08:36:28 -07005133static bool is_shadow_field_rw(unsigned long field)
5134{
5135 switch (field) {
5136#define SHADOW_FIELD_RW(x, y) case x:
5137#include "vmcs_shadow_fields.h"
5138 return true;
5139 default:
5140 break;
5141 }
5142 return false;
5143}
5144
5145static bool is_shadow_field_ro(unsigned long field)
5146{
5147 switch (field) {
5148#define SHADOW_FIELD_RO(x, y) case x:
5149#include "vmcs_shadow_fields.h"
5150 return true;
5151 default:
5152 break;
5153 }
5154 return false;
5155}
Sean Christopherson55d23752018-12-03 13:53:18 -08005156
5157static int handle_vmwrite(struct kvm_vcpu *vcpu)
5158{
Jim Mattsondd2d6042019-12-06 15:46:35 -08005159 struct vmcs12 *vmcs12 = is_guest_mode(vcpu) ? get_shadow_vmcs12(vcpu)
5160 : get_vmcs12(vcpu);
Sean Christopherson5addc232020-04-15 13:34:53 -07005161 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005162 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5163 struct vcpu_vmx *vmx = to_vmx(vcpu);
5164 struct x86_exception e;
5165 unsigned long field;
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07005166 short offset;
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005167 gva_t gva;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005168 int len, r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005169
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005170 /*
5171 * The value to write might be 32 or 64 bits, depending on L1's long
Sean Christopherson55d23752018-12-03 13:53:18 -08005172 * mode, and eventually we need to write that into a field of several
5173 * possible lengths. The code below first zero-extends the value to 64
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005174 * bit (value), and then copies only the appropriate number of
Sean Christopherson55d23752018-12-03 13:53:18 -08005175 * bits into the vmcs12 field.
5176 */
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005177 u64 value = 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08005178
5179 if (!nested_vmx_check_permission(vcpu))
5180 return 1;
5181
Jim Mattsondd2d6042019-12-06 15:46:35 -08005182 /*
5183 * In VMX non-root operation, when the VMCS-link pointer is -1ull,
5184 * any VMWRITE sets the ALU flags for VMfailInvalid.
5185 */
5186 if (vmx->nested.current_vmptr == -1ull ||
5187 (is_guest_mode(vcpu) &&
5188 get_vmcs12(vcpu)->vmcs_link_pointer == -1ull))
Sean Christopherson55d23752018-12-03 13:53:18 -08005189 return nested_vmx_failInvalid(vcpu);
5190
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005191 if (instr_info & BIT(10))
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005192 value = kvm_register_read(vcpu, (((instr_info) >> 3) & 0xf));
Sean Christopherson55d23752018-12-03 13:53:18 -08005193 else {
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005194 len = is_64_bit_mode(vcpu) ? 8 : 4;
Sean Christopherson55d23752018-12-03 13:53:18 -08005195 if (get_vmx_mem_address(vcpu, exit_qualification,
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005196 instr_info, false, len, &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08005197 return 1;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005198 r = kvm_read_guest_virt(vcpu, gva, &value, len, &e);
5199 if (r != X86EMUL_CONTINUE)
Babu Moger3f3393b2020-09-11 14:29:05 -05005200 return kvm_handle_memory_failure(vcpu, r, &e);
Sean Christopherson55d23752018-12-03 13:53:18 -08005201 }
5202
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005203 field = kvm_register_read(vcpu, (((instr_info) >> 28) & 0xf));
Sean Christopherson55d23752018-12-03 13:53:18 -08005204
Jim Mattson693e02c2019-12-06 15:46:36 -08005205 offset = vmcs_field_to_offset(field);
5206 if (offset < 0)
Sean Christophersonb2656e42020-06-08 18:56:07 -07005207 return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Jim Mattson693e02c2019-12-06 15:46:36 -08005208
Sean Christopherson55d23752018-12-03 13:53:18 -08005209 /*
5210 * If the vCPU supports "VMWRITE to any supported field in the
5211 * VMCS," then the "read-only" fields are actually read/write.
5212 */
5213 if (vmcs_field_readonly(field) &&
5214 !nested_cpu_has_vmwrite_any_field(vcpu))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005215 return nested_vmx_fail(vcpu, VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Sean Christopherson55d23752018-12-03 13:53:18 -08005216
Jim Mattsondd2d6042019-12-06 15:46:35 -08005217 /*
5218 * Ensure vmcs12 is up-to-date before any VMWRITE that dirties
5219 * vmcs12, else we may crush a field or consume a stale value.
5220 */
5221 if (!is_guest_mode(vcpu) && !is_shadow_field_rw(field))
5222 copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08005223
5224 /*
Sean Christophersonb6437802019-05-07 08:36:24 -07005225 * Some Intel CPUs intentionally drop the reserved bits of the AR byte
5226 * fields on VMWRITE. Emulate this behavior to ensure consistent KVM
5227 * behavior regardless of the underlying hardware, e.g. if an AR_BYTE
5228 * field is intercepted for VMWRITE but not VMREAD (in L1), then VMREAD
5229 * from L1 will return a different value than VMREAD from L2 (L1 sees
5230 * the stripped down value, L2 sees the full value as stored by KVM).
Sean Christopherson55d23752018-12-03 13:53:18 -08005231 */
Sean Christophersonb6437802019-05-07 08:36:24 -07005232 if (field >= GUEST_ES_AR_BYTES && field <= GUEST_TR_AR_BYTES)
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005233 value &= 0x1f0ff;
Sean Christophersonb6437802019-05-07 08:36:24 -07005234
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005235 vmcs12_write_any(vmcs12, field, offset, value);
Sean Christopherson55d23752018-12-03 13:53:18 -08005236
5237 /*
Sean Christophersone2174292019-05-07 08:36:28 -07005238 * Do not track vmcs12 dirty-state if in guest-mode as we actually
5239 * dirty shadow vmcs12 instead of vmcs12. Fields that can be updated
5240 * by L1 without a vmexit are always updated in the vmcs02, i.e. don't
5241 * "dirty" vmcs12, all others go down the prepare_vmcs02() slow path.
Sean Christopherson55d23752018-12-03 13:53:18 -08005242 */
Sean Christophersone2174292019-05-07 08:36:28 -07005243 if (!is_guest_mode(vcpu) && !is_shadow_field_rw(field)) {
5244 /*
5245 * L1 can read these fields without exiting, ensure the
5246 * shadow VMCS is up-to-date.
5247 */
5248 if (enable_shadow_vmcs && is_shadow_field_ro(field)) {
5249 preempt_disable();
5250 vmcs_load(vmx->vmcs01.shadow_vmcs);
Sean Christophersonfadcead2019-05-07 08:36:23 -07005251
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005252 __vmcs_writel(field, value);
Sean Christophersonfadcead2019-05-07 08:36:23 -07005253
Sean Christophersone2174292019-05-07 08:36:28 -07005254 vmcs_clear(vmx->vmcs01.shadow_vmcs);
5255 vmcs_load(vmx->loaded_vmcs->vmcs);
5256 preempt_enable();
Sean Christopherson55d23752018-12-03 13:53:18 -08005257 }
Sean Christophersone2174292019-05-07 08:36:28 -07005258 vmx->nested.dirty_vmcs12 = true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005259 }
5260
5261 return nested_vmx_succeed(vcpu);
5262}
5263
5264static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
5265{
5266 vmx->nested.current_vmptr = vmptr;
5267 if (enable_shadow_vmcs) {
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07005268 secondary_exec_controls_setbit(vmx, SECONDARY_EXEC_SHADOW_VMCS);
Sean Christopherson55d23752018-12-03 13:53:18 -08005269 vmcs_write64(VMCS_LINK_POINTER,
5270 __pa(vmx->vmcs01.shadow_vmcs));
Sean Christopherson3731905ef2019-05-07 08:36:27 -07005271 vmx->nested.need_vmcs12_to_shadow_sync = true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005272 }
5273 vmx->nested.dirty_vmcs12 = true;
5274}
5275
5276/* Emulate the VMPTRLD instruction */
5277static int handle_vmptrld(struct kvm_vcpu *vcpu)
5278{
5279 struct vcpu_vmx *vmx = to_vmx(vcpu);
5280 gpa_t vmptr;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005281 int r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005282
5283 if (!nested_vmx_check_permission(vcpu))
5284 return 1;
5285
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005286 if (nested_vmx_get_vmptr(vcpu, &vmptr, &r))
5287 return r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005288
KarimAllah Ahmede0bf2662019-01-31 21:24:43 +01005289 if (!page_address_valid(vcpu, vmptr))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005290 return nested_vmx_fail(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
Sean Christopherson55d23752018-12-03 13:53:18 -08005291
5292 if (vmptr == vmx->nested.vmxon_ptr)
Sean Christophersonb2656e42020-06-08 18:56:07 -07005293 return nested_vmx_fail(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
Sean Christopherson55d23752018-12-03 13:53:18 -08005294
5295 /* Forbid normal VMPTRLD if Enlightened version was used */
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02005296 if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
Sean Christopherson55d23752018-12-03 13:53:18 -08005297 return 1;
5298
5299 if (vmx->nested.current_vmptr != vmptr) {
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01005300 struct kvm_host_map map;
Sean Christopherson55d23752018-12-03 13:53:18 -08005301 struct vmcs12 *new_vmcs12;
Sean Christopherson55d23752018-12-03 13:53:18 -08005302
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01005303 if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmptr), &map)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08005304 /*
5305 * Reads from an unbacked page return all 1s,
5306 * which means that the 32 bits located at the
5307 * given physical address won't match the required
5308 * VMCS12_REVISION identifier.
5309 */
Sean Christophersonb2656e42020-06-08 18:56:07 -07005310 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005311 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Sean Christopherson55d23752018-12-03 13:53:18 -08005312 }
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01005313
5314 new_vmcs12 = map.hva;
5315
Sean Christopherson55d23752018-12-03 13:53:18 -08005316 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
5317 (new_vmcs12->hdr.shadow_vmcs &&
5318 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01005319 kvm_vcpu_unmap(vcpu, &map, false);
Sean Christophersonb2656e42020-06-08 18:56:07 -07005320 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005321 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
5322 }
5323
5324 nested_release_vmcs12(vcpu);
5325
5326 /*
5327 * Load VMCS12 from guest memory since it is not already
5328 * cached.
5329 */
5330 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01005331 kvm_vcpu_unmap(vcpu, &map, false);
Sean Christopherson55d23752018-12-03 13:53:18 -08005332
5333 set_current_vmptr(vmx, vmptr);
5334 }
5335
5336 return nested_vmx_succeed(vcpu);
5337}
5338
5339/* Emulate the VMPTRST instruction */
5340static int handle_vmptrst(struct kvm_vcpu *vcpu)
5341{
Sean Christopherson5addc232020-04-15 13:34:53 -07005342 unsigned long exit_qual = vmx_get_exit_qual(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005343 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5344 gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr;
5345 struct x86_exception e;
5346 gva_t gva;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005347 int r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005348
5349 if (!nested_vmx_check_permission(vcpu))
5350 return 1;
5351
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02005352 if (unlikely(evmptr_is_valid(to_vmx(vcpu)->nested.hv_evmcs_vmptr)))
Sean Christopherson55d23752018-12-03 13:53:18 -08005353 return 1;
5354
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005355 if (get_vmx_mem_address(vcpu, exit_qual, instr_info,
5356 true, sizeof(gpa_t), &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08005357 return 1;
5358 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005359 r = kvm_write_guest_virt_system(vcpu, gva, (void *)&current_vmptr,
5360 sizeof(gpa_t), &e);
5361 if (r != X86EMUL_CONTINUE)
Babu Moger3f3393b2020-09-11 14:29:05 -05005362 return kvm_handle_memory_failure(vcpu, r, &e);
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005363
Sean Christopherson55d23752018-12-03 13:53:18 -08005364 return nested_vmx_succeed(vcpu);
5365}
5366
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005367#define EPTP_PA_MASK GENMASK_ULL(51, 12)
5368
5369static bool nested_ept_root_matches(hpa_t root_hpa, u64 root_eptp, u64 eptp)
5370{
5371 return VALID_PAGE(root_hpa) &&
5372 ((root_eptp & EPTP_PA_MASK) == (eptp & EPTP_PA_MASK));
5373}
5374
Sean Christopherson55d23752018-12-03 13:53:18 -08005375/* Emulate the INVEPT instruction */
5376static int handle_invept(struct kvm_vcpu *vcpu)
5377{
5378 struct vcpu_vmx *vmx = to_vmx(vcpu);
5379 u32 vmx_instruction_info, types;
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005380 unsigned long type, roots_to_free;
5381 struct kvm_mmu *mmu;
Sean Christopherson55d23752018-12-03 13:53:18 -08005382 gva_t gva;
5383 struct x86_exception e;
5384 struct {
5385 u64 eptp, gpa;
5386 } operand;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005387 int i, r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005388
5389 if (!(vmx->nested.msrs.secondary_ctls_high &
5390 SECONDARY_EXEC_ENABLE_EPT) ||
5391 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
5392 kvm_queue_exception(vcpu, UD_VECTOR);
5393 return 1;
5394 }
5395
5396 if (!nested_vmx_check_permission(vcpu))
5397 return 1;
5398
5399 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005400 type = kvm_register_read(vcpu, (vmx_instruction_info >> 28) & 0xf);
Sean Christopherson55d23752018-12-03 13:53:18 -08005401
5402 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
5403
5404 if (type >= 32 || !(types & (1 << type)))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005405 return nested_vmx_fail(vcpu, VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Sean Christopherson55d23752018-12-03 13:53:18 -08005406
5407 /* According to the Intel VMX instruction reference, the memory
5408 * operand is read even if it isn't needed (e.g., for type==global)
5409 */
Sean Christopherson5addc232020-04-15 13:34:53 -07005410 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005411 vmx_instruction_info, false, sizeof(operand), &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08005412 return 1;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005413 r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e);
5414 if (r != X86EMUL_CONTINUE)
Babu Moger3f3393b2020-09-11 14:29:05 -05005415 return kvm_handle_memory_failure(vcpu, r, &e);
Sean Christopherson55d23752018-12-03 13:53:18 -08005416
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005417 /*
5418 * Nested EPT roots are always held through guest_mmu,
5419 * not root_mmu.
5420 */
5421 mmu = &vcpu->arch.guest_mmu;
5422
Sean Christopherson55d23752018-12-03 13:53:18 -08005423 switch (type) {
Sean Christopherson55d23752018-12-03 13:53:18 -08005424 case VMX_EPT_EXTENT_CONTEXT:
Sean Christophersoneed00302020-03-20 14:27:58 -07005425 if (!nested_vmx_check_eptp(vcpu, operand.eptp))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005426 return nested_vmx_fail(vcpu,
Sean Christophersoneed00302020-03-20 14:27:58 -07005427 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Sean Christophersonf8aa7e32020-03-20 14:27:59 -07005428
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005429 roots_to_free = 0;
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07005430 if (nested_ept_root_matches(mmu->root_hpa, mmu->root_pgd,
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005431 operand.eptp))
5432 roots_to_free |= KVM_MMU_ROOT_CURRENT;
5433
5434 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) {
5435 if (nested_ept_root_matches(mmu->prev_roots[i].hpa,
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07005436 mmu->prev_roots[i].pgd,
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005437 operand.eptp))
5438 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
5439 }
5440 break;
Sean Christophersoneed00302020-03-20 14:27:58 -07005441 case VMX_EPT_EXTENT_GLOBAL:
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005442 roots_to_free = KVM_MMU_ROOTS_ALL;
Sean Christopherson55d23752018-12-03 13:53:18 -08005443 break;
5444 default:
Sean Christophersonf9336e32020-05-04 08:35:06 -07005445 BUG();
Sean Christopherson55d23752018-12-03 13:53:18 -08005446 break;
5447 }
5448
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005449 if (roots_to_free)
5450 kvm_mmu_free_roots(vcpu, mmu, roots_to_free);
5451
Sean Christopherson55d23752018-12-03 13:53:18 -08005452 return nested_vmx_succeed(vcpu);
5453}
5454
5455static int handle_invvpid(struct kvm_vcpu *vcpu)
5456{
5457 struct vcpu_vmx *vmx = to_vmx(vcpu);
5458 u32 vmx_instruction_info;
5459 unsigned long type, types;
5460 gva_t gva;
5461 struct x86_exception e;
5462 struct {
5463 u64 vpid;
5464 u64 gla;
5465 } operand;
5466 u16 vpid02;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005467 int r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005468
5469 if (!(vmx->nested.msrs.secondary_ctls_high &
5470 SECONDARY_EXEC_ENABLE_VPID) ||
5471 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
5472 kvm_queue_exception(vcpu, UD_VECTOR);
5473 return 1;
5474 }
5475
5476 if (!nested_vmx_check_permission(vcpu))
5477 return 1;
5478
5479 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005480 type = kvm_register_read(vcpu, (vmx_instruction_info >> 28) & 0xf);
Sean Christopherson55d23752018-12-03 13:53:18 -08005481
5482 types = (vmx->nested.msrs.vpid_caps &
5483 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
5484
5485 if (type >= 32 || !(types & (1 << type)))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005486 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005487 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
5488
5489 /* according to the intel vmx instruction reference, the memory
5490 * operand is read even if it isn't needed (e.g., for type==global)
5491 */
Sean Christopherson5addc232020-04-15 13:34:53 -07005492 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005493 vmx_instruction_info, false, sizeof(operand), &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08005494 return 1;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005495 r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e);
5496 if (r != X86EMUL_CONTINUE)
Babu Moger3f3393b2020-09-11 14:29:05 -05005497 return kvm_handle_memory_failure(vcpu, r, &e);
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005498
Sean Christopherson55d23752018-12-03 13:53:18 -08005499 if (operand.vpid >> 16)
Sean Christophersonb2656e42020-06-08 18:56:07 -07005500 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005501 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
5502
5503 vpid02 = nested_get_vpid02(vcpu);
5504 switch (type) {
5505 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
5506 if (!operand.vpid ||
5507 is_noncanonical_address(operand.gla, vcpu))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005508 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005509 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Sean Christophersonbc41d0c2020-03-20 14:28:09 -07005510 vpid_sync_vcpu_addr(vpid02, operand.gla);
Sean Christopherson55d23752018-12-03 13:53:18 -08005511 break;
5512 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
5513 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
5514 if (!operand.vpid)
Sean Christophersonb2656e42020-06-08 18:56:07 -07005515 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005516 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Sean Christopherson446ace42020-03-20 14:28:05 -07005517 vpid_sync_context(vpid02);
Sean Christopherson55d23752018-12-03 13:53:18 -08005518 break;
5519 case VMX_VPID_EXTENT_ALL_CONTEXT:
Sean Christopherson446ace42020-03-20 14:28:05 -07005520 vpid_sync_context(vpid02);
Sean Christopherson55d23752018-12-03 13:53:18 -08005521 break;
5522 default:
5523 WARN_ON_ONCE(1);
5524 return kvm_skip_emulated_instruction(vcpu);
5525 }
5526
Junaid Shahidd6e3f832020-03-20 14:28:00 -07005527 /*
5528 * Sync the shadow page tables if EPT is disabled, L1 is invalidating
5529 * linear mappings for L2 (tagged with L2's VPID). Free all roots as
5530 * VPIDs are not tracked in the MMU role.
5531 *
5532 * Note, this operates on root_mmu, not guest_mmu, as L1 and L2 share
5533 * an MMU when EPT is disabled.
5534 *
5535 * TODO: sync only the affected SPTEs for INVDIVIDUAL_ADDR.
5536 */
5537 if (!enable_ept)
5538 kvm_mmu_free_roots(vcpu, &vcpu->arch.root_mmu,
5539 KVM_MMU_ROOTS_ALL);
5540
Sean Christopherson55d23752018-12-03 13:53:18 -08005541 return nested_vmx_succeed(vcpu);
5542}
5543
5544static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
5545 struct vmcs12 *vmcs12)
5546{
Sean Christopherson2b3eaf82019-04-30 10:36:19 -07005547 u32 index = kvm_rcx_read(vcpu);
Sean Christophersonac6389a2020-03-02 18:02:38 -08005548 u64 new_eptp;
Sean Christopherson55d23752018-12-03 13:53:18 -08005549 bool accessed_dirty;
5550 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5551
5552 if (!nested_cpu_has_eptp_switching(vmcs12) ||
5553 !nested_cpu_has_ept(vmcs12))
5554 return 1;
5555
5556 if (index >= VMFUNC_EPTP_ENTRIES)
5557 return 1;
5558
5559
5560 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
Sean Christophersonac6389a2020-03-02 18:02:38 -08005561 &new_eptp, index * 8, 8))
Sean Christopherson55d23752018-12-03 13:53:18 -08005562 return 1;
5563
Sean Christophersonac6389a2020-03-02 18:02:38 -08005564 accessed_dirty = !!(new_eptp & VMX_EPTP_AD_ENABLE_BIT);
Sean Christopherson55d23752018-12-03 13:53:18 -08005565
5566 /*
5567 * If the (L2) guest does a vmfunc to the currently
5568 * active ept pointer, we don't have to do anything else
5569 */
Sean Christophersonac6389a2020-03-02 18:02:38 -08005570 if (vmcs12->ept_pointer != new_eptp) {
5571 if (!nested_vmx_check_eptp(vcpu, new_eptp))
Sean Christopherson55d23752018-12-03 13:53:18 -08005572 return 1;
5573
Sean Christopherson55d23752018-12-03 13:53:18 -08005574 mmu->ept_ad = accessed_dirty;
5575 mmu->mmu_role.base.ad_disabled = !accessed_dirty;
Sean Christophersonac6389a2020-03-02 18:02:38 -08005576 vmcs12->ept_pointer = new_eptp;
Sean Christophersonc805f5d2021-03-04 17:10:57 -08005577
5578 kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005579 }
5580
5581 return 0;
5582}
5583
5584static int handle_vmfunc(struct kvm_vcpu *vcpu)
5585{
5586 struct vcpu_vmx *vmx = to_vmx(vcpu);
5587 struct vmcs12 *vmcs12;
Sean Christopherson2b3eaf82019-04-30 10:36:19 -07005588 u32 function = kvm_rax_read(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005589
5590 /*
5591 * VMFUNC is only supported for nested guests, but we always enable the
5592 * secondary control for simplicity; for non-nested mode, fake that we
5593 * didn't by injecting #UD.
5594 */
5595 if (!is_guest_mode(vcpu)) {
5596 kvm_queue_exception(vcpu, UD_VECTOR);
5597 return 1;
5598 }
5599
5600 vmcs12 = get_vmcs12(vcpu);
Sean Christopherson0e752252021-06-09 16:42:22 -07005601 if (!(vmcs12->vm_function_control & BIT_ULL(function)))
Sean Christopherson55d23752018-12-03 13:53:18 -08005602 goto fail;
5603
5604 switch (function) {
5605 case 0:
5606 if (nested_vmx_eptp_switching(vcpu, vmcs12))
5607 goto fail;
5608 break;
5609 default:
5610 goto fail;
5611 }
5612 return kvm_skip_emulated_instruction(vcpu);
5613
5614fail:
Sean Christopherson8e533242020-11-06 17:03:12 +08005615 /*
5616 * This is effectively a reflected VM-Exit, as opposed to a synthesized
5617 * nested VM-Exit. Pass the original exit reason, i.e. don't hardcode
5618 * EXIT_REASON_VMFUNC as the exit reason.
5619 */
5620 nested_vmx_vmexit(vcpu, vmx->exit_reason.full,
Sean Christopherson87915852020-04-15 13:34:54 -07005621 vmx_get_intr_info(vcpu),
Sean Christopherson5addc232020-04-15 13:34:53 -07005622 vmx_get_exit_qual(vcpu));
Sean Christopherson55d23752018-12-03 13:53:18 -08005623 return 1;
5624}
5625
Oliver Uptone71237d2020-02-04 15:26:30 -08005626/*
5627 * Return true if an IO instruction with the specified port and size should cause
5628 * a VM-exit into L1.
5629 */
5630bool nested_vmx_check_io_bitmaps(struct kvm_vcpu *vcpu, unsigned int port,
5631 int size)
Sean Christopherson55d23752018-12-03 13:53:18 -08005632{
Oliver Uptone71237d2020-02-04 15:26:30 -08005633 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005634 gpa_t bitmap, last_bitmap;
Sean Christopherson55d23752018-12-03 13:53:18 -08005635 u8 b;
5636
Sean Christopherson55d23752018-12-03 13:53:18 -08005637 last_bitmap = (gpa_t)-1;
5638 b = -1;
5639
5640 while (size > 0) {
5641 if (port < 0x8000)
5642 bitmap = vmcs12->io_bitmap_a;
5643 else if (port < 0x10000)
5644 bitmap = vmcs12->io_bitmap_b;
5645 else
5646 return true;
5647 bitmap += (port & 0x7fff) / 8;
5648
5649 if (last_bitmap != bitmap)
5650 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
5651 return true;
5652 if (b & (1 << (port & 7)))
5653 return true;
5654
5655 port++;
5656 size--;
5657 last_bitmap = bitmap;
5658 }
5659
5660 return false;
5661}
5662
Oliver Uptone71237d2020-02-04 15:26:30 -08005663static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
5664 struct vmcs12 *vmcs12)
5665{
5666 unsigned long exit_qualification;
Oliver Upton35a57132020-02-04 15:26:31 -08005667 unsigned short port;
Oliver Uptone71237d2020-02-04 15:26:30 -08005668 int size;
5669
5670 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
5671 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
5672
Sean Christopherson5addc232020-04-15 13:34:53 -07005673 exit_qualification = vmx_get_exit_qual(vcpu);
Oliver Uptone71237d2020-02-04 15:26:30 -08005674
5675 port = exit_qualification >> 16;
5676 size = (exit_qualification & 7) + 1;
5677
5678 return nested_vmx_check_io_bitmaps(vcpu, port, size);
5679}
5680
Sean Christopherson55d23752018-12-03 13:53:18 -08005681/*
Miaohe Lin463bfee2020-02-14 10:44:05 +08005682 * Return 1 if we should exit from L2 to L1 to handle an MSR access,
Sean Christopherson55d23752018-12-03 13:53:18 -08005683 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
5684 * disinterest in the current event (read or write a specific MSR) by using an
5685 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
5686 */
5687static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
Sean Christopherson8e533242020-11-06 17:03:12 +08005688 struct vmcs12 *vmcs12,
5689 union vmx_exit_reason exit_reason)
Sean Christopherson55d23752018-12-03 13:53:18 -08005690{
Sean Christopherson2b3eaf82019-04-30 10:36:19 -07005691 u32 msr_index = kvm_rcx_read(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005692 gpa_t bitmap;
5693
5694 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
5695 return true;
5696
5697 /*
5698 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
5699 * for the four combinations of read/write and low/high MSR numbers.
5700 * First we need to figure out which of the four to use:
5701 */
5702 bitmap = vmcs12->msr_bitmap;
Sean Christopherson8e533242020-11-06 17:03:12 +08005703 if (exit_reason.basic == EXIT_REASON_MSR_WRITE)
Sean Christopherson55d23752018-12-03 13:53:18 -08005704 bitmap += 2048;
5705 if (msr_index >= 0xc0000000) {
5706 msr_index -= 0xc0000000;
5707 bitmap += 1024;
5708 }
5709
5710 /* Then read the msr_index'th bit from this bitmap: */
5711 if (msr_index < 1024*8) {
5712 unsigned char b;
5713 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
5714 return true;
5715 return 1 & (b >> (msr_index & 7));
5716 } else
5717 return true; /* let L1 handle the wrong parameter */
5718}
5719
5720/*
5721 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
5722 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
5723 * intercept (via guest_host_mask etc.) the current event.
5724 */
5725static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
5726 struct vmcs12 *vmcs12)
5727{
Sean Christopherson5addc232020-04-15 13:34:53 -07005728 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005729 int cr = exit_qualification & 15;
5730 int reg;
5731 unsigned long val;
5732
5733 switch ((exit_qualification >> 4) & 3) {
5734 case 0: /* mov to cr */
5735 reg = (exit_qualification >> 8) & 15;
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005736 val = kvm_register_read(vcpu, reg);
Sean Christopherson55d23752018-12-03 13:53:18 -08005737 switch (cr) {
5738 case 0:
5739 if (vmcs12->cr0_guest_host_mask &
5740 (val ^ vmcs12->cr0_read_shadow))
5741 return true;
5742 break;
5743 case 3:
Sean Christopherson55d23752018-12-03 13:53:18 -08005744 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
5745 return true;
5746 break;
5747 case 4:
5748 if (vmcs12->cr4_guest_host_mask &
5749 (vmcs12->cr4_read_shadow ^ val))
5750 return true;
5751 break;
5752 case 8:
5753 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
5754 return true;
5755 break;
5756 }
5757 break;
5758 case 2: /* clts */
5759 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
5760 (vmcs12->cr0_read_shadow & X86_CR0_TS))
5761 return true;
5762 break;
5763 case 1: /* mov from cr */
5764 switch (cr) {
5765 case 3:
5766 if (vmcs12->cpu_based_vm_exec_control &
5767 CPU_BASED_CR3_STORE_EXITING)
5768 return true;
5769 break;
5770 case 8:
5771 if (vmcs12->cpu_based_vm_exec_control &
5772 CPU_BASED_CR8_STORE_EXITING)
5773 return true;
5774 break;
5775 }
5776 break;
5777 case 3: /* lmsw */
5778 /*
5779 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
5780 * cr0. Other attempted changes are ignored, with no exit.
5781 */
5782 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
5783 if (vmcs12->cr0_guest_host_mask & 0xe &
5784 (val ^ vmcs12->cr0_read_shadow))
5785 return true;
5786 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
5787 !(vmcs12->cr0_read_shadow & 0x1) &&
5788 (val & 0x1))
5789 return true;
5790 break;
5791 }
5792 return false;
5793}
5794
Sean Christopherson72add912021-04-12 16:21:42 +12005795static bool nested_vmx_exit_handled_encls(struct kvm_vcpu *vcpu,
5796 struct vmcs12 *vmcs12)
5797{
5798 u32 encls_leaf;
5799
5800 if (!guest_cpuid_has(vcpu, X86_FEATURE_SGX) ||
5801 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENCLS_EXITING))
5802 return false;
5803
5804 encls_leaf = kvm_rax_read(vcpu);
5805 if (encls_leaf > 62)
5806 encls_leaf = 63;
5807 return vmcs12->encls_exiting_bitmap & BIT_ULL(encls_leaf);
5808}
5809
Sean Christopherson55d23752018-12-03 13:53:18 -08005810static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
5811 struct vmcs12 *vmcs12, gpa_t bitmap)
5812{
5813 u32 vmx_instruction_info;
5814 unsigned long field;
5815 u8 b;
5816
5817 if (!nested_cpu_has_shadow_vmcs(vmcs12))
5818 return true;
5819
5820 /* Decode instruction info and find the field to access */
5821 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5822 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
5823
5824 /* Out-of-range fields always cause a VM exit from L2 to L1 */
5825 if (field >> 15)
5826 return true;
5827
5828 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
5829 return true;
5830
5831 return 1 & (b >> (field & 7));
5832}
5833
Oliver Uptonb045ae92020-04-14 22:47:45 +00005834static bool nested_vmx_exit_handled_mtf(struct vmcs12 *vmcs12)
5835{
5836 u32 entry_intr_info = vmcs12->vm_entry_intr_info_field;
5837
5838 if (nested_cpu_has_mtf(vmcs12))
5839 return true;
5840
5841 /*
5842 * An MTF VM-exit may be injected into the guest by setting the
5843 * interruption-type to 7 (other event) and the vector field to 0. Such
5844 * is the case regardless of the 'monitor trap flag' VM-execution
5845 * control.
5846 */
5847 return entry_intr_info == (INTR_INFO_VALID_MASK
5848 | INTR_TYPE_OTHER_EVENT);
5849}
5850
Sean Christopherson55d23752018-12-03 13:53:18 -08005851/*
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005852 * Return true if L0 wants to handle an exit from L2 regardless of whether or not
5853 * L1 wants the exit. Only call this when in is_guest_mode (L2).
Sean Christopherson55d23752018-12-03 13:53:18 -08005854 */
Sean Christopherson8e533242020-11-06 17:03:12 +08005855static bool nested_vmx_l0_wants_exit(struct kvm_vcpu *vcpu,
5856 union vmx_exit_reason exit_reason)
Sean Christopherson55d23752018-12-03 13:53:18 -08005857{
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005858 u32 intr_info;
5859
Sean Christopherson8e533242020-11-06 17:03:12 +08005860 switch ((u16)exit_reason.basic) {
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005861 case EXIT_REASON_EXCEPTION_NMI:
Sean Christopherson87915852020-04-15 13:34:54 -07005862 intr_info = vmx_get_intr_info(vcpu);
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005863 if (is_nmi(intr_info))
5864 return true;
5865 else if (is_page_fault(intr_info))
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +02005866 return vcpu->arch.apf.host_apf_flags || !enable_ept;
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005867 else if (is_debug(intr_info) &&
5868 vcpu->guest_debug &
5869 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5870 return true;
5871 else if (is_breakpoint(intr_info) &&
5872 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
5873 return true;
5874 return false;
5875 case EXIT_REASON_EXTERNAL_INTERRUPT:
5876 return true;
5877 case EXIT_REASON_MCE_DURING_VMENTRY:
5878 return true;
5879 case EXIT_REASON_EPT_VIOLATION:
5880 /*
5881 * L0 always deals with the EPT violation. If nested EPT is
5882 * used, and the nested mmu code discovers that the address is
5883 * missing in the guest EPT table (EPT12), the EPT violation
5884 * will be injected with nested_ept_inject_page_fault()
5885 */
5886 return true;
5887 case EXIT_REASON_EPT_MISCONFIG:
5888 /*
5889 * L2 never uses directly L1's EPT, but rather L0's own EPT
5890 * table (shadow on EPT) or a merged EPT table that L0 built
5891 * (EPT on EPT). So any problems with the structure of the
5892 * table is L0's fault.
5893 */
5894 return true;
5895 case EXIT_REASON_PREEMPTION_TIMER:
5896 return true;
5897 case EXIT_REASON_PML_FULL:
Sean Christophersonc3bb9a22021-02-12 16:50:07 -08005898 /*
5899 * PML is emulated for an L1 VMM and should never be enabled in
5900 * vmcs02, always "handle" PML_FULL by exiting to userspace.
5901 */
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005902 return true;
5903 case EXIT_REASON_VMFUNC:
5904 /* VM functions are emulated through L2->L0 vmexits. */
5905 return true;
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005906 default:
5907 break;
5908 }
5909 return false;
5910}
5911
5912/*
5913 * Return 1 if L1 wants to intercept an exit from L2. Only call this when in
5914 * is_guest_mode (L2).
5915 */
Sean Christopherson8e533242020-11-06 17:03:12 +08005916static bool nested_vmx_l1_wants_exit(struct kvm_vcpu *vcpu,
5917 union vmx_exit_reason exit_reason)
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005918{
Sean Christopherson55d23752018-12-03 13:53:18 -08005919 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Sean Christopherson9bd4af22020-04-21 00:53:27 -07005920 u32 intr_info;
Sean Christopherson55d23752018-12-03 13:53:18 -08005921
Sean Christopherson8e533242020-11-06 17:03:12 +08005922 switch ((u16)exit_reason.basic) {
Sean Christopherson55d23752018-12-03 13:53:18 -08005923 case EXIT_REASON_EXCEPTION_NMI:
Sean Christopherson87915852020-04-15 13:34:54 -07005924 intr_info = vmx_get_intr_info(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005925 if (is_nmi(intr_info))
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005926 return true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005927 else if (is_page_fault(intr_info))
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005928 return true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005929 return vmcs12->exception_bitmap &
5930 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
5931 case EXIT_REASON_EXTERNAL_INTERRUPT:
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005932 return nested_exit_on_intr(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005933 case EXIT_REASON_TRIPLE_FAULT:
5934 return true;
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08005935 case EXIT_REASON_INTERRUPT_WINDOW:
5936 return nested_cpu_has(vmcs12, CPU_BASED_INTR_WINDOW_EXITING);
Sean Christopherson55d23752018-12-03 13:53:18 -08005937 case EXIT_REASON_NMI_WINDOW:
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08005938 return nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING);
Sean Christopherson55d23752018-12-03 13:53:18 -08005939 case EXIT_REASON_TASK_SWITCH:
5940 return true;
5941 case EXIT_REASON_CPUID:
5942 return true;
5943 case EXIT_REASON_HLT:
5944 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
5945 case EXIT_REASON_INVD:
5946 return true;
5947 case EXIT_REASON_INVLPG:
5948 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
5949 case EXIT_REASON_RDPMC:
5950 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
5951 case EXIT_REASON_RDRAND:
5952 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
5953 case EXIT_REASON_RDSEED:
5954 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
5955 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
5956 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
5957 case EXIT_REASON_VMREAD:
5958 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
5959 vmcs12->vmread_bitmap);
5960 case EXIT_REASON_VMWRITE:
5961 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
5962 vmcs12->vmwrite_bitmap);
5963 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
5964 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
5965 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
5966 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
5967 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
5968 /*
5969 * VMX instructions trap unconditionally. This allows L1 to
5970 * emulate them for its L2 guest, i.e., allows 3-level nesting!
5971 */
5972 return true;
5973 case EXIT_REASON_CR_ACCESS:
5974 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
5975 case EXIT_REASON_DR_ACCESS:
5976 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
5977 case EXIT_REASON_IO_INSTRUCTION:
5978 return nested_vmx_exit_handled_io(vcpu, vmcs12);
5979 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
5980 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
5981 case EXIT_REASON_MSR_READ:
5982 case EXIT_REASON_MSR_WRITE:
5983 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
5984 case EXIT_REASON_INVALID_STATE:
5985 return true;
5986 case EXIT_REASON_MWAIT_INSTRUCTION:
5987 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
5988 case EXIT_REASON_MONITOR_TRAP_FLAG:
Oliver Uptonb045ae92020-04-14 22:47:45 +00005989 return nested_vmx_exit_handled_mtf(vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08005990 case EXIT_REASON_MONITOR_INSTRUCTION:
5991 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
5992 case EXIT_REASON_PAUSE_INSTRUCTION:
5993 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
5994 nested_cpu_has2(vmcs12,
5995 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
5996 case EXIT_REASON_MCE_DURING_VMENTRY:
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005997 return true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005998 case EXIT_REASON_TPR_BELOW_THRESHOLD:
5999 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
6000 case EXIT_REASON_APIC_ACCESS:
6001 case EXIT_REASON_APIC_WRITE:
6002 case EXIT_REASON_EOI_INDUCED:
6003 /*
6004 * The controls for "virtualize APIC accesses," "APIC-
6005 * register virtualization," and "virtual-interrupt
6006 * delivery" only come from vmcs12.
6007 */
6008 return true;
Sean Christopherson55d23752018-12-03 13:53:18 -08006009 case EXIT_REASON_INVPCID:
6010 return
6011 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
6012 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
6013 case EXIT_REASON_WBINVD:
6014 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
6015 case EXIT_REASON_XSETBV:
6016 return true;
6017 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
6018 /*
6019 * This should never happen, since it is not possible to
6020 * set XSS to a non-zero value---neither in L1 nor in L2.
6021 * If if it were, XSS would have to be checked against
6022 * the XSS exit bitmap in vmcs12.
6023 */
6024 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Tao Xubf653b72019-07-16 14:55:51 +08006025 case EXIT_REASON_UMWAIT:
6026 case EXIT_REASON_TPAUSE:
6027 return nested_cpu_has2(vmcs12,
6028 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE);
Sean Christopherson72add912021-04-12 16:21:42 +12006029 case EXIT_REASON_ENCLS:
6030 return nested_vmx_exit_handled_encls(vcpu, vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08006031 default:
6032 return true;
6033 }
6034}
6035
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006036/*
6037 * Conditionally reflect a VM-Exit into L1. Returns %true if the VM-Exit was
6038 * reflected into L1.
6039 */
Sean Christophersonf47baae2020-04-15 10:55:16 -07006040bool nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu)
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006041{
Sean Christophersonfbdd5022020-04-15 10:55:12 -07006042 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson8e533242020-11-06 17:03:12 +08006043 union vmx_exit_reason exit_reason = vmx->exit_reason;
Sean Christopherson87796552020-04-22 17:11:27 -07006044 unsigned long exit_qual;
6045 u32 exit_intr_info;
Sean Christophersonfbdd5022020-04-15 10:55:12 -07006046
6047 WARN_ON_ONCE(vmx->nested.nested_run_pending);
6048
6049 /*
6050 * Late nested VM-Fail shares the same flow as nested VM-Exit since KVM
6051 * has already loaded L2's state.
6052 */
6053 if (unlikely(vmx->fail)) {
6054 trace_kvm_nested_vmenter_failed(
6055 "hardware VM-instruction error: ",
6056 vmcs_read32(VM_INSTRUCTION_ERROR));
6057 exit_intr_info = 0;
6058 exit_qual = 0;
6059 goto reflect_vmexit;
6060 }
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006061
Sean Christopherson8e533242020-11-06 17:03:12 +08006062 trace_kvm_nested_vmexit(exit_reason.full, vcpu, KVM_ISA_VMX);
Sean Christopherson236871b2020-04-15 10:55:13 -07006063
Sean Christopherson2c1f3322020-04-15 10:55:14 -07006064 /* If L0 (KVM) wants the exit, it trumps L1's desires. */
6065 if (nested_vmx_l0_wants_exit(vcpu, exit_reason))
6066 return false;
6067
6068 /* If L1 doesn't want the exit, handle it in L0. */
6069 if (!nested_vmx_l1_wants_exit(vcpu, exit_reason))
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006070 return false;
6071
6072 /*
Sean Christopherson1d283062020-04-15 10:55:15 -07006073 * vmcs.VM_EXIT_INTR_INFO is only valid for EXCEPTION_NMI exits. For
6074 * EXTERNAL_INTERRUPT, the value for vmcs12->vm_exit_intr_info would
6075 * need to be synthesized by querying the in-kernel LAPIC, but external
6076 * interrupts are never reflected to L1 so it's a non-issue.
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006077 */
Sean Christopherson02f19652020-09-23 13:13:49 -07006078 exit_intr_info = vmx_get_intr_info(vcpu);
Sean Christophersonf315f2b2020-09-23 13:13:45 -07006079 if (is_exception_with_error_code(exit_intr_info)) {
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006080 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6081
6082 vmcs12->vm_exit_intr_error_code =
6083 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
6084 }
Sean Christopherson02f19652020-09-23 13:13:49 -07006085 exit_qual = vmx_get_exit_qual(vcpu);
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006086
Sean Christophersonfbdd5022020-04-15 10:55:12 -07006087reflect_vmexit:
Sean Christopherson8e533242020-11-06 17:03:12 +08006088 nested_vmx_vmexit(vcpu, exit_reason.full, exit_intr_info, exit_qual);
Sean Christopherson7b7bd872020-04-15 10:55:11 -07006089 return true;
6090}
Sean Christopherson55d23752018-12-03 13:53:18 -08006091
6092static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
6093 struct kvm_nested_state __user *user_kvm_nested_state,
6094 u32 user_data_size)
6095{
6096 struct vcpu_vmx *vmx;
6097 struct vmcs12 *vmcs12;
6098 struct kvm_nested_state kvm_state = {
6099 .flags = 0,
Liran Alon6ca00df2019-06-16 15:03:10 +03006100 .format = KVM_STATE_NESTED_FORMAT_VMX,
Sean Christopherson55d23752018-12-03 13:53:18 -08006101 .size = sizeof(kvm_state),
Peter Shier850448f2020-05-26 14:51:06 -07006102 .hdr.vmx.flags = 0,
Liran Alon6ca00df2019-06-16 15:03:10 +03006103 .hdr.vmx.vmxon_pa = -1ull,
6104 .hdr.vmx.vmcs12_pa = -1ull,
Peter Shier850448f2020-05-26 14:51:06 -07006105 .hdr.vmx.preemption_timer_deadline = 0,
Sean Christopherson55d23752018-12-03 13:53:18 -08006106 };
Liran Alon6ca00df2019-06-16 15:03:10 +03006107 struct kvm_vmx_nested_state_data __user *user_vmx_nested_state =
6108 &user_kvm_nested_state->data.vmx[0];
Sean Christopherson55d23752018-12-03 13:53:18 -08006109
6110 if (!vcpu)
Liran Alon6ca00df2019-06-16 15:03:10 +03006111 return kvm_state.size + sizeof(*user_vmx_nested_state);
Sean Christopherson55d23752018-12-03 13:53:18 -08006112
6113 vmx = to_vmx(vcpu);
6114 vmcs12 = get_vmcs12(vcpu);
6115
Sean Christopherson55d23752018-12-03 13:53:18 -08006116 if (nested_vmx_allowed(vcpu) &&
6117 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
Liran Alon6ca00df2019-06-16 15:03:10 +03006118 kvm_state.hdr.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
6119 kvm_state.hdr.vmx.vmcs12_pa = vmx->nested.current_vmptr;
Sean Christopherson55d23752018-12-03 13:53:18 -08006120
6121 if (vmx_has_valid_vmcs12(vcpu)) {
Liran Alon6ca00df2019-06-16 15:03:10 +03006122 kvm_state.size += sizeof(user_vmx_nested_state->vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08006123
Vitaly Kuznetsov27849962021-05-26 15:20:20 +02006124 /* 'hv_evmcs_vmptr' can also be EVMPTR_MAP_PENDING here */
6125 if (vmx->nested.hv_evmcs_vmptr != EVMPTR_INVALID)
Liran Alon323d73a2019-06-26 16:09:27 +03006126 kvm_state.flags |= KVM_STATE_NESTED_EVMCS;
6127
Sean Christopherson55d23752018-12-03 13:53:18 -08006128 if (is_guest_mode(vcpu) &&
6129 nested_cpu_has_shadow_vmcs(vmcs12) &&
6130 vmcs12->vmcs_link_pointer != -1ull)
Liran Alon6ca00df2019-06-16 15:03:10 +03006131 kvm_state.size += sizeof(user_vmx_nested_state->shadow_vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08006132 }
6133
6134 if (vmx->nested.smm.vmxon)
Liran Alon6ca00df2019-06-16 15:03:10 +03006135 kvm_state.hdr.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
Sean Christopherson55d23752018-12-03 13:53:18 -08006136
6137 if (vmx->nested.smm.guest_mode)
Liran Alon6ca00df2019-06-16 15:03:10 +03006138 kvm_state.hdr.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
Sean Christopherson55d23752018-12-03 13:53:18 -08006139
6140 if (is_guest_mode(vcpu)) {
6141 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
6142
6143 if (vmx->nested.nested_run_pending)
6144 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
Oliver Upton5ef8acb2020-02-07 02:36:07 -08006145
6146 if (vmx->nested.mtf_pending)
6147 kvm_state.flags |= KVM_STATE_NESTED_MTF_PENDING;
Peter Shier850448f2020-05-26 14:51:06 -07006148
6149 if (nested_cpu_has_preemption_timer(vmcs12) &&
6150 vmx->nested.has_preemption_timer_deadline) {
6151 kvm_state.hdr.vmx.flags |=
6152 KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE;
6153 kvm_state.hdr.vmx.preemption_timer_deadline =
6154 vmx->nested.preemption_timer_deadline;
6155 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006156 }
6157 }
6158
6159 if (user_data_size < kvm_state.size)
6160 goto out;
6161
6162 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
6163 return -EFAULT;
6164
6165 if (!vmx_has_valid_vmcs12(vcpu))
6166 goto out;
6167
6168 /*
6169 * When running L2, the authoritative vmcs12 state is in the
6170 * vmcs02. When running L1, the authoritative vmcs12 state is
6171 * in the shadow or enlightened vmcs linked to vmcs01, unless
Sean Christopherson3731905ef2019-05-07 08:36:27 -07006172 * need_vmcs12_to_shadow_sync is set, in which case, the authoritative
Sean Christopherson55d23752018-12-03 13:53:18 -08006173 * vmcs12 state is in the vmcs12 already.
6174 */
6175 if (is_guest_mode(vcpu)) {
Sean Christopherson3731905ef2019-05-07 08:36:27 -07006176 sync_vmcs02_to_vmcs12(vcpu, vmcs12);
Sean Christopherson7952d762019-05-07 08:36:29 -07006177 sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
Maxim Levitskyd51e1d32021-01-14 22:54:47 +02006178 } else {
6179 copy_vmcs02_to_vmcs12_rare(vcpu, get_vmcs12(vcpu));
6180 if (!vmx->nested.need_vmcs12_to_shadow_sync) {
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +02006181 if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
Vitaly Kuznetsovd6bf71a2021-05-26 15:20:22 +02006182 /*
6183 * L1 hypervisor is not obliged to keep eVMCS
6184 * clean fields data always up-to-date while
6185 * not in guest mode, 'hv_clean_fields' is only
6186 * supposed to be actual upon vmentry so we need
6187 * to ignore it here and do full copy.
6188 */
6189 copy_enlightened_to_vmcs12(vmx, 0);
Maxim Levitskyd51e1d32021-01-14 22:54:47 +02006190 else if (enable_shadow_vmcs)
6191 copy_shadow_to_vmcs12(vmx);
6192 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006193 }
6194
Liran Alon6ca00df2019-06-16 15:03:10 +03006195 BUILD_BUG_ON(sizeof(user_vmx_nested_state->vmcs12) < VMCS12_SIZE);
6196 BUILD_BUG_ON(sizeof(user_vmx_nested_state->shadow_vmcs12) < VMCS12_SIZE);
6197
Tom Roeder3a33d032019-01-24 13:48:20 -08006198 /*
6199 * Copy over the full allocated size of vmcs12 rather than just the size
6200 * of the struct.
6201 */
Liran Alon6ca00df2019-06-16 15:03:10 +03006202 if (copy_to_user(user_vmx_nested_state->vmcs12, vmcs12, VMCS12_SIZE))
Sean Christopherson55d23752018-12-03 13:53:18 -08006203 return -EFAULT;
6204
6205 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
6206 vmcs12->vmcs_link_pointer != -1ull) {
Liran Alon6ca00df2019-06-16 15:03:10 +03006207 if (copy_to_user(user_vmx_nested_state->shadow_vmcs12,
Tom Roeder3a33d032019-01-24 13:48:20 -08006208 get_shadow_vmcs12(vcpu), VMCS12_SIZE))
Sean Christopherson55d23752018-12-03 13:53:18 -08006209 return -EFAULT;
6210 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006211out:
6212 return kvm_state.size;
6213}
6214
6215/*
6216 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
6217 */
6218void vmx_leave_nested(struct kvm_vcpu *vcpu)
6219{
6220 if (is_guest_mode(vcpu)) {
6221 to_vmx(vcpu)->nested.nested_run_pending = 0;
6222 nested_vmx_vmexit(vcpu, -1, 0, 0);
6223 }
6224 free_nested(vcpu);
6225}
6226
6227static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
6228 struct kvm_nested_state __user *user_kvm_nested_state,
6229 struct kvm_nested_state *kvm_state)
6230{
6231 struct vcpu_vmx *vmx = to_vmx(vcpu);
6232 struct vmcs12 *vmcs12;
Sean Christopherson68cda402020-05-11 15:05:29 -07006233 enum vm_entry_failure_code ignored;
Liran Alon6ca00df2019-06-16 15:03:10 +03006234 struct kvm_vmx_nested_state_data __user *user_vmx_nested_state =
6235 &user_kvm_nested_state->data.vmx[0];
Sean Christopherson55d23752018-12-03 13:53:18 -08006236 int ret;
6237
Liran Alon6ca00df2019-06-16 15:03:10 +03006238 if (kvm_state->format != KVM_STATE_NESTED_FORMAT_VMX)
Sean Christopherson55d23752018-12-03 13:53:18 -08006239 return -EINVAL;
6240
Liran Alon6ca00df2019-06-16 15:03:10 +03006241 if (kvm_state->hdr.vmx.vmxon_pa == -1ull) {
6242 if (kvm_state->hdr.vmx.smm.flags)
Sean Christopherson55d23752018-12-03 13:53:18 -08006243 return -EINVAL;
6244
Liran Alon6ca00df2019-06-16 15:03:10 +03006245 if (kvm_state->hdr.vmx.vmcs12_pa != -1ull)
Sean Christopherson55d23752018-12-03 13:53:18 -08006246 return -EINVAL;
6247
Liran Alon323d73a2019-06-26 16:09:27 +03006248 /*
6249 * KVM_STATE_NESTED_EVMCS used to signal that KVM should
6250 * enable eVMCS capability on vCPU. However, since then
6251 * code was changed such that flag signals vmcs12 should
6252 * be copied into eVMCS in guest memory.
6253 *
6254 * To preserve backwards compatability, allow user
6255 * to set this flag even when there is no VMXON region.
6256 */
Paolo Bonzini9fd58872019-06-19 16:52:27 +02006257 if (kvm_state->flags & ~KVM_STATE_NESTED_EVMCS)
6258 return -EINVAL;
6259 } else {
6260 if (!nested_vmx_allowed(vcpu))
6261 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08006262
Paolo Bonzini9fd58872019-06-19 16:52:27 +02006263 if (!page_address_valid(vcpu, kvm_state->hdr.vmx.vmxon_pa))
6264 return -EINVAL;
Liran Alon323d73a2019-06-26 16:09:27 +03006265 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006266
Liran Alon6ca00df2019-06-16 15:03:10 +03006267 if ((kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
Sean Christopherson55d23752018-12-03 13:53:18 -08006268 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
6269 return -EINVAL;
6270
Liran Alon6ca00df2019-06-16 15:03:10 +03006271 if (kvm_state->hdr.vmx.smm.flags &
Sean Christopherson55d23752018-12-03 13:53:18 -08006272 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
6273 return -EINVAL;
6274
Paolo Bonzini5e105c82020-07-27 08:55:09 -04006275 if (kvm_state->hdr.vmx.flags & ~KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE)
6276 return -EINVAL;
6277
Sean Christopherson55d23752018-12-03 13:53:18 -08006278 /*
6279 * SMM temporarily disables VMX, so we cannot be in guest mode,
6280 * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags
6281 * must be zero.
6282 */
Liran Alon65b712f12019-06-25 14:26:42 +03006283 if (is_smm(vcpu) ?
6284 (kvm_state->flags &
6285 (KVM_STATE_NESTED_GUEST_MODE | KVM_STATE_NESTED_RUN_PENDING))
6286 : kvm_state->hdr.vmx.smm.flags)
Sean Christopherson55d23752018-12-03 13:53:18 -08006287 return -EINVAL;
6288
Liran Alon6ca00df2019-06-16 15:03:10 +03006289 if ((kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
6290 !(kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
Sean Christopherson55d23752018-12-03 13:53:18 -08006291 return -EINVAL;
6292
Liran Alon323d73a2019-06-26 16:09:27 +03006293 if ((kvm_state->flags & KVM_STATE_NESTED_EVMCS) &&
6294 (!nested_vmx_allowed(vcpu) || !vmx->nested.enlightened_vmcs_enabled))
Paolo Bonzini9fd58872019-06-19 16:52:27 +02006295 return -EINVAL;
6296
Liran Alon323d73a2019-06-26 16:09:27 +03006297 vmx_leave_nested(vcpu);
Paolo Bonzini9fd58872019-06-19 16:52:27 +02006298
Liran Alon6ca00df2019-06-16 15:03:10 +03006299 if (kvm_state->hdr.vmx.vmxon_pa == -1ull)
Sean Christopherson55d23752018-12-03 13:53:18 -08006300 return 0;
6301
Liran Alon6ca00df2019-06-16 15:03:10 +03006302 vmx->nested.vmxon_ptr = kvm_state->hdr.vmx.vmxon_pa;
Sean Christopherson55d23752018-12-03 13:53:18 -08006303 ret = enter_vmx_operation(vcpu);
6304 if (ret)
6305 return ret;
6306
Paolo Bonzini0f02bd02020-07-27 09:00:37 -04006307 /* Empty 'VMXON' state is permitted if no VMCS loaded */
6308 if (kvm_state->size < sizeof(*kvm_state) + sizeof(*vmcs12)) {
6309 /* See vmx_has_valid_vmcs12. */
6310 if ((kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE) ||
6311 (kvm_state->flags & KVM_STATE_NESTED_EVMCS) ||
6312 (kvm_state->hdr.vmx.vmcs12_pa != -1ull))
6313 return -EINVAL;
6314 else
6315 return 0;
6316 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006317
Liran Alon6ca00df2019-06-16 15:03:10 +03006318 if (kvm_state->hdr.vmx.vmcs12_pa != -1ull) {
6319 if (kvm_state->hdr.vmx.vmcs12_pa == kvm_state->hdr.vmx.vmxon_pa ||
6320 !page_address_valid(vcpu, kvm_state->hdr.vmx.vmcs12_pa))
Sean Christopherson55d23752018-12-03 13:53:18 -08006321 return -EINVAL;
6322
Liran Alon6ca00df2019-06-16 15:03:10 +03006323 set_current_vmptr(vmx, kvm_state->hdr.vmx.vmcs12_pa);
Sean Christopherson55d23752018-12-03 13:53:18 -08006324 } else if (kvm_state->flags & KVM_STATE_NESTED_EVMCS) {
6325 /*
Vitaly Kuznetsove942dbf2020-03-09 16:52:12 +01006326 * nested_vmx_handle_enlightened_vmptrld() cannot be called
6327 * directly from here as HV_X64_MSR_VP_ASSIST_PAGE may not be
6328 * restored yet. EVMCS will be mapped from
6329 * nested_get_vmcs12_pages().
Sean Christopherson55d23752018-12-03 13:53:18 -08006330 */
Vitaly Kuznetsov27849962021-05-26 15:20:20 +02006331 vmx->nested.hv_evmcs_vmptr = EVMPTR_MAP_PENDING;
Paolo Bonzini729c15c2020-09-22 06:53:57 -04006332 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08006333 } else {
6334 return -EINVAL;
6335 }
6336
Liran Alon6ca00df2019-06-16 15:03:10 +03006337 if (kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
Sean Christopherson55d23752018-12-03 13:53:18 -08006338 vmx->nested.smm.vmxon = true;
6339 vmx->nested.vmxon = false;
6340
Liran Alon6ca00df2019-06-16 15:03:10 +03006341 if (kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
Sean Christopherson55d23752018-12-03 13:53:18 -08006342 vmx->nested.smm.guest_mode = true;
6343 }
6344
6345 vmcs12 = get_vmcs12(vcpu);
Liran Alon6ca00df2019-06-16 15:03:10 +03006346 if (copy_from_user(vmcs12, user_vmx_nested_state->vmcs12, sizeof(*vmcs12)))
Sean Christopherson55d23752018-12-03 13:53:18 -08006347 return -EFAULT;
6348
6349 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
6350 return -EINVAL;
6351
6352 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
6353 return 0;
6354
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006355 vmx->nested.nested_run_pending =
6356 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
6357
Oliver Upton5ef8acb2020-02-07 02:36:07 -08006358 vmx->nested.mtf_pending =
6359 !!(kvm_state->flags & KVM_STATE_NESTED_MTF_PENDING);
6360
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006361 ret = -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08006362 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
6363 vmcs12->vmcs_link_pointer != -1ull) {
6364 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
6365
Liran Alon6ca00df2019-06-16 15:03:10 +03006366 if (kvm_state->size <
6367 sizeof(*kvm_state) +
6368 sizeof(user_vmx_nested_state->vmcs12) + sizeof(*shadow_vmcs12))
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006369 goto error_guest_mode;
Sean Christopherson55d23752018-12-03 13:53:18 -08006370
6371 if (copy_from_user(shadow_vmcs12,
Liran Alon6ca00df2019-06-16 15:03:10 +03006372 user_vmx_nested_state->shadow_vmcs12,
6373 sizeof(*shadow_vmcs12))) {
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006374 ret = -EFAULT;
6375 goto error_guest_mode;
6376 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006377
6378 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
6379 !shadow_vmcs12->hdr.shadow_vmcs)
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006380 goto error_guest_mode;
Sean Christopherson55d23752018-12-03 13:53:18 -08006381 }
6382
Paolo Bonzini83d31e52020-07-09 13:12:09 -04006383 vmx->nested.has_preemption_timer_deadline = false;
Peter Shier850448f2020-05-26 14:51:06 -07006384 if (kvm_state->hdr.vmx.flags & KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE) {
6385 vmx->nested.has_preemption_timer_deadline = true;
6386 vmx->nested.preemption_timer_deadline =
6387 kvm_state->hdr.vmx.preemption_timer_deadline;
6388 }
6389
Sean Christopherson5478ba32019-04-11 12:18:06 -07006390 if (nested_vmx_check_controls(vcpu, vmcs12) ||
6391 nested_vmx_check_host_state(vcpu, vmcs12) ||
Sean Christopherson68cda402020-05-11 15:05:29 -07006392 nested_vmx_check_guest_state(vcpu, vmcs12, &ignored))
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006393 goto error_guest_mode;
Sean Christopherson55d23752018-12-03 13:53:18 -08006394
6395 vmx->nested.dirty_vmcs12 = true;
6396 ret = nested_vmx_enter_non_root_mode(vcpu, false);
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006397 if (ret)
6398 goto error_guest_mode;
Sean Christopherson55d23752018-12-03 13:53:18 -08006399
6400 return 0;
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006401
6402error_guest_mode:
6403 vmx->nested.nested_run_pending = 0;
6404 return ret;
Sean Christopherson55d23752018-12-03 13:53:18 -08006405}
6406
Xiaoyao Li1b842922019-10-20 17:11:01 +08006407void nested_vmx_set_vmcs_shadowing_bitmap(void)
Sean Christopherson55d23752018-12-03 13:53:18 -08006408{
6409 if (enable_shadow_vmcs) {
Sean Christopherson55d23752018-12-03 13:53:18 -08006410 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Sean Christophersonfadcead2019-05-07 08:36:23 -07006411 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
Sean Christopherson55d23752018-12-03 13:53:18 -08006412 }
6413}
6414
6415/*
6416 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
6417 * returned for the various VMX controls MSRs when nested VMX is enabled.
6418 * The same values should also be used to verify that vmcs12 control fields are
6419 * valid during nested entry from L1 to L2.
6420 * Each of these control msrs has a low and high 32-bit half: A low bit is on
6421 * if the corresponding bit in the (32-bit) control field *must* be on, and a
6422 * bit in the high half is on if the corresponding bit in the control field
6423 * may be on. See also vmx_control_verify().
6424 */
Vitaly Kuznetsova4443262020-02-20 18:22:04 +01006425void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, u32 ept_caps)
Sean Christopherson55d23752018-12-03 13:53:18 -08006426{
6427 /*
6428 * Note that as a general rule, the high half of the MSRs (bits in
6429 * the control fields which may be 1) should be initialized by the
6430 * intersection of the underlying hardware's MSR (i.e., features which
6431 * can be supported) and the list of features we want to expose -
6432 * because they are known to be properly supported in our code.
6433 * Also, usually, the low half of the MSRs (bits which must be 1) can
6434 * be set to 0, meaning that L1 may turn off any of these bits. The
6435 * reason is that if one of these bits is necessary, it will appear
6436 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
6437 * fields of vmcs01 and vmcs02, will turn these bits off - and
Sean Christopherson2c1f3322020-04-15 10:55:14 -07006438 * nested_vmx_l1_wants_exit() will not pass related exits to L1.
Sean Christopherson55d23752018-12-03 13:53:18 -08006439 * These rules have exceptions below.
6440 */
6441
6442 /* pin-based controls */
6443 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
6444 msrs->pinbased_ctls_low,
6445 msrs->pinbased_ctls_high);
6446 msrs->pinbased_ctls_low |=
6447 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
6448 msrs->pinbased_ctls_high &=
6449 PIN_BASED_EXT_INTR_MASK |
6450 PIN_BASED_NMI_EXITING |
6451 PIN_BASED_VIRTUAL_NMIS |
Vitaly Kuznetsova4443262020-02-20 18:22:04 +01006452 (enable_apicv ? PIN_BASED_POSTED_INTR : 0);
Sean Christopherson55d23752018-12-03 13:53:18 -08006453 msrs->pinbased_ctls_high |=
6454 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
6455 PIN_BASED_VMX_PREEMPTION_TIMER;
6456
6457 /* exit controls */
6458 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
6459 msrs->exit_ctls_low,
6460 msrs->exit_ctls_high);
6461 msrs->exit_ctls_low =
6462 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
6463
6464 msrs->exit_ctls_high &=
6465#ifdef CONFIG_X86_64
6466 VM_EXIT_HOST_ADDR_SPACE_SIZE |
6467#endif
Chenyi Qiangefc83132020-08-28 16:56:18 +08006468 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT |
6469 VM_EXIT_CLEAR_BNDCFGS | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
Sean Christopherson55d23752018-12-03 13:53:18 -08006470 msrs->exit_ctls_high |=
6471 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
6472 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
6473 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
6474
6475 /* We support free control of debug control saving. */
6476 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
6477
6478 /* entry controls */
6479 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
6480 msrs->entry_ctls_low,
6481 msrs->entry_ctls_high);
6482 msrs->entry_ctls_low =
6483 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
6484 msrs->entry_ctls_high &=
6485#ifdef CONFIG_X86_64
6486 VM_ENTRY_IA32E_MODE |
6487#endif
Chenyi Qiangefc83132020-08-28 16:56:18 +08006488 VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS |
6489 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
Sean Christopherson55d23752018-12-03 13:53:18 -08006490 msrs->entry_ctls_high |=
6491 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
6492
6493 /* We support free control of debug control loading. */
6494 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
6495
6496 /* cpu-based controls */
6497 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
6498 msrs->procbased_ctls_low,
6499 msrs->procbased_ctls_high);
6500 msrs->procbased_ctls_low =
6501 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
6502 msrs->procbased_ctls_high &=
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08006503 CPU_BASED_INTR_WINDOW_EXITING |
Xiaoyao Li5e3d3942019-12-06 16:45:26 +08006504 CPU_BASED_NMI_WINDOW_EXITING | CPU_BASED_USE_TSC_OFFSETTING |
Sean Christopherson55d23752018-12-03 13:53:18 -08006505 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
6506 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
6507 CPU_BASED_CR3_STORE_EXITING |
6508#ifdef CONFIG_X86_64
6509 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
6510#endif
6511 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
6512 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
6513 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
6514 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
6515 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
6516 /*
6517 * We can allow some features even when not supported by the
6518 * hardware. For example, L1 can specify an MSR bitmap - and we
6519 * can use it to avoid exits to L1 - even when L0 runs L2
6520 * without MSR bitmaps.
6521 */
6522 msrs->procbased_ctls_high |=
6523 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
6524 CPU_BASED_USE_MSR_BITMAPS;
6525
6526 /* We support free control of CR3 access interception. */
6527 msrs->procbased_ctls_low &=
6528 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
6529
6530 /*
6531 * secondary cpu-based controls. Do not include those that
Xiaoyao Li7c1b7612020-07-09 12:34:25 +08006532 * depend on CPUID bits, they are added later by
6533 * vmx_vcpu_after_set_cpuid.
Sean Christopherson55d23752018-12-03 13:53:18 -08006534 */
Vitaly Kuznetsov6b1971c2019-02-07 11:42:14 +01006535 if (msrs->procbased_ctls_high & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)
6536 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
6537 msrs->secondary_ctls_low,
6538 msrs->secondary_ctls_high);
6539
Sean Christopherson55d23752018-12-03 13:53:18 -08006540 msrs->secondary_ctls_low = 0;
6541 msrs->secondary_ctls_high &=
6542 SECONDARY_EXEC_DESC |
Sean Christopherson7f3603b2020-09-23 09:50:47 -07006543 SECONDARY_EXEC_ENABLE_RDTSCP |
Sean Christopherson55d23752018-12-03 13:53:18 -08006544 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini6defc592019-07-02 14:39:29 +02006545 SECONDARY_EXEC_WBINVD_EXITING |
Sean Christopherson55d23752018-12-03 13:53:18 -08006546 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6547 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini6defc592019-07-02 14:39:29 +02006548 SECONDARY_EXEC_RDRAND_EXITING |
6549 SECONDARY_EXEC_ENABLE_INVPCID |
6550 SECONDARY_EXEC_RDSEED_EXITING |
Ilias Stamatisd041b5e2021-05-26 19:44:17 +01006551 SECONDARY_EXEC_XSAVES |
6552 SECONDARY_EXEC_TSC_SCALING;
Sean Christopherson55d23752018-12-03 13:53:18 -08006553
6554 /*
6555 * We can emulate "VMCS shadowing," even if the hardware
6556 * doesn't support it.
6557 */
6558 msrs->secondary_ctls_high |=
6559 SECONDARY_EXEC_SHADOW_VMCS;
6560
6561 if (enable_ept) {
6562 /* nested EPT: emulate EPT also to L1 */
6563 msrs->secondary_ctls_high |=
6564 SECONDARY_EXEC_ENABLE_EPT;
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08006565 msrs->ept_caps =
6566 VMX_EPT_PAGE_WALK_4_BIT |
6567 VMX_EPT_PAGE_WALK_5_BIT |
6568 VMX_EPTP_WB_BIT |
Sean Christopherson96d47012020-03-02 18:02:40 -08006569 VMX_EPT_INVEPT_BIT |
6570 VMX_EPT_EXECUTE_ONLY_BIT;
6571
Sean Christopherson55d23752018-12-03 13:53:18 -08006572 msrs->ept_caps &= ept_caps;
6573 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
6574 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
6575 VMX_EPT_1GB_PAGE_BIT;
6576 if (enable_ept_ad_bits) {
6577 msrs->secondary_ctls_high |=
6578 SECONDARY_EXEC_ENABLE_PML;
6579 msrs->ept_caps |= VMX_EPT_AD_BIT;
6580 }
6581 }
6582
6583 if (cpu_has_vmx_vmfunc()) {
6584 msrs->secondary_ctls_high |=
6585 SECONDARY_EXEC_ENABLE_VMFUNC;
6586 /*
6587 * Advertise EPTP switching unconditionally
6588 * since we emulate it
6589 */
6590 if (enable_ept)
6591 msrs->vmfunc_controls =
6592 VMX_VMFUNC_EPTP_SWITCHING;
6593 }
6594
6595 /*
6596 * Old versions of KVM use the single-context version without
6597 * checking for support, so declare that it is supported even
6598 * though it is treated as global context. The alternative is
6599 * not failing the single-context invvpid, and it is worse.
6600 */
6601 if (enable_vpid) {
6602 msrs->secondary_ctls_high |=
6603 SECONDARY_EXEC_ENABLE_VPID;
6604 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
6605 VMX_VPID_EXTENT_SUPPORTED_MASK;
6606 }
6607
6608 if (enable_unrestricted_guest)
6609 msrs->secondary_ctls_high |=
6610 SECONDARY_EXEC_UNRESTRICTED_GUEST;
6611
6612 if (flexpriority_enabled)
6613 msrs->secondary_ctls_high |=
6614 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6615
Sean Christopherson72add912021-04-12 16:21:42 +12006616 if (enable_sgx)
6617 msrs->secondary_ctls_high |= SECONDARY_EXEC_ENCLS_EXITING;
6618
Sean Christopherson55d23752018-12-03 13:53:18 -08006619 /* miscellaneous data */
6620 rdmsr(MSR_IA32_VMX_MISC,
6621 msrs->misc_low,
6622 msrs->misc_high);
6623 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
6624 msrs->misc_low |=
6625 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
6626 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Yadong Qibf0cd882020-11-06 14:51:22 +08006627 VMX_MISC_ACTIVITY_HLT |
6628 VMX_MISC_ACTIVITY_WAIT_SIPI;
Sean Christopherson55d23752018-12-03 13:53:18 -08006629 msrs->misc_high = 0;
6630
6631 /*
6632 * This MSR reports some information about VMX support. We
6633 * should return information about the VMX we emulate for the
6634 * guest, and the VMCS structure we give it - not about the
6635 * VMX support of the underlying hardware.
6636 */
6637 msrs->basic =
6638 VMCS12_REVISION |
6639 VMX_BASIC_TRUE_CTLS |
6640 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
6641 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
6642
6643 if (cpu_has_vmx_basic_inout())
6644 msrs->basic |= VMX_BASIC_INOUT;
6645
6646 /*
6647 * These MSRs specify bits which the guest must keep fixed on
6648 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
6649 * We picked the standard core2 setting.
6650 */
6651#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
6652#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
6653 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
6654 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
6655
6656 /* These MSRs specify bits which the guest must keep fixed off. */
6657 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
6658 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
6659
6660 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
6661 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
6662}
6663
6664void nested_vmx_hardware_unsetup(void)
6665{
6666 int i;
6667
6668 if (enable_shadow_vmcs) {
6669 for (i = 0; i < VMX_BITMAP_NR; i++)
6670 free_page((unsigned long)vmx_bitmap[i]);
6671 }
6672}
6673
Sean Christopherson6c1c6e52020-05-06 13:46:53 -07006674__init int nested_vmx_hardware_setup(int (*exit_handlers[])(struct kvm_vcpu *))
Sean Christopherson55d23752018-12-03 13:53:18 -08006675{
6676 int i;
6677
6678 if (!cpu_has_vmx_shadow_vmcs())
6679 enable_shadow_vmcs = 0;
6680 if (enable_shadow_vmcs) {
6681 for (i = 0; i < VMX_BITMAP_NR; i++) {
Ben Gardon41836832019-02-11 11:02:52 -08006682 /*
6683 * The vmx_bitmap is not tied to a VM and so should
6684 * not be charged to a memcg.
6685 */
Sean Christopherson55d23752018-12-03 13:53:18 -08006686 vmx_bitmap[i] = (unsigned long *)
6687 __get_free_page(GFP_KERNEL);
6688 if (!vmx_bitmap[i]) {
6689 nested_vmx_hardware_unsetup();
6690 return -ENOMEM;
6691 }
6692 }
6693
6694 init_vmcs_shadow_fields();
6695 }
6696
Liran Aloncc877672019-11-18 21:11:21 +02006697 exit_handlers[EXIT_REASON_VMCLEAR] = handle_vmclear;
6698 exit_handlers[EXIT_REASON_VMLAUNCH] = handle_vmlaunch;
6699 exit_handlers[EXIT_REASON_VMPTRLD] = handle_vmptrld;
6700 exit_handlers[EXIT_REASON_VMPTRST] = handle_vmptrst;
6701 exit_handlers[EXIT_REASON_VMREAD] = handle_vmread;
6702 exit_handlers[EXIT_REASON_VMRESUME] = handle_vmresume;
6703 exit_handlers[EXIT_REASON_VMWRITE] = handle_vmwrite;
6704 exit_handlers[EXIT_REASON_VMOFF] = handle_vmoff;
6705 exit_handlers[EXIT_REASON_VMON] = handle_vmon;
6706 exit_handlers[EXIT_REASON_INVEPT] = handle_invept;
6707 exit_handlers[EXIT_REASON_INVVPID] = handle_invvpid;
6708 exit_handlers[EXIT_REASON_VMFUNC] = handle_vmfunc;
Sean Christopherson55d23752018-12-03 13:53:18 -08006709
Sean Christopherson55d23752018-12-03 13:53:18 -08006710 return 0;
6711}
Paolo Bonzini33b22172020-04-17 10:24:18 -04006712
6713struct kvm_x86_nested_ops vmx_nested_ops = {
6714 .check_events = vmx_check_nested_events,
Sean Christophersond2060bd2020-04-22 19:25:39 -07006715 .hv_timer_pending = nested_vmx_preemption_timer_pending,
Sean Christophersoncb6a32c2021-03-02 09:45:14 -08006716 .triple_fault = nested_vmx_triple_fault,
Paolo Bonzini33b22172020-04-17 10:24:18 -04006717 .get_state = vmx_get_nested_state,
6718 .set_state = vmx_set_nested_state,
Paolo Bonzini9a78e152021-01-08 11:43:08 -05006719 .get_nested_state_pages = vmx_get_nested_state_pages,
Sean Christopherson02f5fb22020-06-22 14:58:32 -07006720 .write_log_dirty = nested_vmx_write_pml_buffer,
Paolo Bonzini33b22172020-04-17 10:24:18 -04006721 .enable_evmcs = nested_enable_evmcs,
6722 .get_evmcs_version = nested_get_evmcs_version,
6723};