blob: ac1e1761dd8745356cb5125f42e17dbba7138bcd [file] [log] [blame]
Sean Christopherson55d23752018-12-03 13:53:18 -08001// SPDX-License-Identifier: GPL-2.0
2
3#include <linux/frame.h>
4#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 Christopherson55d23752018-12-03 13:53:18 -080014#include "trace.h"
15#include "x86.h"
16
17static bool __read_mostly enable_shadow_vmcs = 1;
18module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
19
20static bool __read_mostly nested_early_check = 0;
21module_param(nested_early_check, bool, S_IRUGO);
22
Sean Christopherson5497b952019-07-11 08:58:29 -070023#define CC(consistency_check) \
24({ \
25 bool failed = (consistency_check); \
26 if (failed) \
Sean Christopherson380e0052019-07-11 08:58:30 -070027 trace_kvm_nested_vmenter_failed(#consistency_check, 0); \
Sean Christopherson5497b952019-07-11 08:58:29 -070028 failed; \
29})
30
Sean Christopherson55d23752018-12-03 13:53:18 -080031/*
32 * Hyper-V requires all of these, so mark them as supported even though
33 * they are just treated the same as all-context.
34 */
35#define VMX_VPID_EXTENT_SUPPORTED_MASK \
36 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
37 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
38 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
39 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
40
41#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
42
43enum {
44 VMX_VMREAD_BITMAP,
45 VMX_VMWRITE_BITMAP,
46 VMX_BITMAP_NR
47};
48static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
49
50#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
51#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
52
Sean Christopherson1c6f0b42019-05-07 08:36:25 -070053struct shadow_vmcs_field {
54 u16 encoding;
55 u16 offset;
56};
57static struct shadow_vmcs_field shadow_read_only_fields[] = {
58#define SHADOW_FIELD_RO(x, y) { x, offsetof(struct vmcs12, y) },
Sean Christopherson55d23752018-12-03 13:53:18 -080059#include "vmcs_shadow_fields.h"
60};
61static int max_shadow_read_only_fields =
62 ARRAY_SIZE(shadow_read_only_fields);
63
Sean Christopherson1c6f0b42019-05-07 08:36:25 -070064static struct shadow_vmcs_field shadow_read_write_fields[] = {
65#define SHADOW_FIELD_RW(x, y) { x, offsetof(struct vmcs12, y) },
Sean Christopherson55d23752018-12-03 13:53:18 -080066#include "vmcs_shadow_fields.h"
67};
68static int max_shadow_read_write_fields =
69 ARRAY_SIZE(shadow_read_write_fields);
70
Yi Wang8997f652019-01-21 15:27:05 +080071static void init_vmcs_shadow_fields(void)
Sean Christopherson55d23752018-12-03 13:53:18 -080072{
73 int i, j;
74
75 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
76 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
77
78 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
Sean Christopherson1c6f0b42019-05-07 08:36:25 -070079 struct shadow_vmcs_field entry = shadow_read_only_fields[i];
80 u16 field = entry.encoding;
Sean Christopherson55d23752018-12-03 13:53:18 -080081
82 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
83 (i + 1 == max_shadow_read_only_fields ||
Sean Christopherson1c6f0b42019-05-07 08:36:25 -070084 shadow_read_only_fields[i + 1].encoding != field + 1))
Sean Christopherson55d23752018-12-03 13:53:18 -080085 pr_err("Missing field from shadow_read_only_field %x\n",
86 field + 1);
87
88 clear_bit(field, vmx_vmread_bitmap);
Sean Christopherson55d23752018-12-03 13:53:18 -080089 if (field & 1)
Sean Christopherson1c6f0b42019-05-07 08:36:25 -070090#ifdef CONFIG_X86_64
Sean Christopherson55d23752018-12-03 13:53:18 -080091 continue;
Sean Christopherson1c6f0b42019-05-07 08:36:25 -070092#else
93 entry.offset += sizeof(u32);
Sean Christopherson55d23752018-12-03 13:53:18 -080094#endif
Sean Christopherson1c6f0b42019-05-07 08:36:25 -070095 shadow_read_only_fields[j++] = entry;
Sean Christopherson55d23752018-12-03 13:53:18 -080096 }
97 max_shadow_read_only_fields = j;
98
99 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Sean Christopherson1c6f0b42019-05-07 08:36:25 -0700100 struct shadow_vmcs_field entry = shadow_read_write_fields[i];
101 u16 field = entry.encoding;
Sean Christopherson55d23752018-12-03 13:53:18 -0800102
103 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
104 (i + 1 == max_shadow_read_write_fields ||
Sean Christopherson1c6f0b42019-05-07 08:36:25 -0700105 shadow_read_write_fields[i + 1].encoding != field + 1))
Sean Christopherson55d23752018-12-03 13:53:18 -0800106 pr_err("Missing field from shadow_read_write_field %x\n",
107 field + 1);
108
Sean Christophersonb6437802019-05-07 08:36:24 -0700109 WARN_ONCE(field >= GUEST_ES_AR_BYTES &&
110 field <= GUEST_TR_AR_BYTES,
Sean Christopherson1c6f0b42019-05-07 08:36:25 -0700111 "Update vmcs12_write_any() to drop reserved bits from AR_BYTES");
Sean Christophersonb6437802019-05-07 08:36:24 -0700112
Sean Christopherson55d23752018-12-03 13:53:18 -0800113 /*
114 * PML and the preemption timer can be emulated, but the
115 * processor cannot vmwrite to fields that don't exist
116 * on bare metal.
117 */
118 switch (field) {
119 case GUEST_PML_INDEX:
120 if (!cpu_has_vmx_pml())
121 continue;
122 break;
123 case VMX_PREEMPTION_TIMER_VALUE:
124 if (!cpu_has_vmx_preemption_timer())
125 continue;
126 break;
127 case GUEST_INTR_STATUS:
128 if (!cpu_has_vmx_apicv())
129 continue;
130 break;
131 default:
132 break;
133 }
134
135 clear_bit(field, vmx_vmwrite_bitmap);
136 clear_bit(field, vmx_vmread_bitmap);
Sean Christopherson55d23752018-12-03 13:53:18 -0800137 if (field & 1)
Sean Christopherson1c6f0b42019-05-07 08:36:25 -0700138#ifdef CONFIG_X86_64
Sean Christopherson55d23752018-12-03 13:53:18 -0800139 continue;
Sean Christopherson1c6f0b42019-05-07 08:36:25 -0700140#else
141 entry.offset += sizeof(u32);
Sean Christopherson55d23752018-12-03 13:53:18 -0800142#endif
Sean Christopherson1c6f0b42019-05-07 08:36:25 -0700143 shadow_read_write_fields[j++] = entry;
Sean Christopherson55d23752018-12-03 13:53:18 -0800144 }
145 max_shadow_read_write_fields = j;
146}
147
148/*
149 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
150 * set the success or error code of an emulated VMX instruction (as specified
151 * by Vol 2B, VMX Instruction Reference, "Conventions"), and skip the emulated
152 * instruction.
153 */
154static int nested_vmx_succeed(struct kvm_vcpu *vcpu)
155{
156 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
157 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
158 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
159 return kvm_skip_emulated_instruction(vcpu);
160}
161
162static int nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
163{
164 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
165 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
166 X86_EFLAGS_SF | X86_EFLAGS_OF))
167 | X86_EFLAGS_CF);
168 return kvm_skip_emulated_instruction(vcpu);
169}
170
171static int nested_vmx_failValid(struct kvm_vcpu *vcpu,
172 u32 vm_instruction_error)
173{
Sean Christopherson55d23752018-12-03 13:53:18 -0800174 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
175 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
176 X86_EFLAGS_SF | X86_EFLAGS_OF))
177 | X86_EFLAGS_ZF);
178 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
179 /*
180 * We don't need to force a shadow sync because
181 * VM_INSTRUCTION_ERROR is not shadowed
182 */
183 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 */
194 if (vmx->nested.current_vmptr == -1ull && !vmx->nested.hv_evmcs)
195 return nested_vmx_failInvalid(vcpu);
196
197 return nested_vmx_failValid(vcpu, vm_instruction_error);
198}
199
Sean Christopherson55d23752018-12-03 13:53:18 -0800200static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
201{
202 /* TODO: not to reset guest simply here. */
203 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
204 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
205}
206
Marc Orrf0b51052019-09-17 11:50:57 -0700207static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
208{
209 return fixed_bits_valid(control, low, high);
210}
211
212static inline u64 vmx_control_msr(u32 low, u32 high)
213{
214 return low | ((u64)high << 32);
215}
216
Sean Christopherson55d23752018-12-03 13:53:18 -0800217static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
218{
Sean Christophersonfe7f895d2019-05-07 12:17:57 -0700219 secondary_exec_controls_clearbit(vmx, SECONDARY_EXEC_SHADOW_VMCS);
Sean Christopherson55d23752018-12-03 13:53:18 -0800220 vmcs_write64(VMCS_LINK_POINTER, -1ull);
Paolo Bonzini88dddc12019-07-19 18:41:10 +0200221 vmx->nested.need_vmcs12_to_shadow_sync = false;
Sean Christopherson55d23752018-12-03 13:53:18 -0800222}
223
224static inline void nested_release_evmcs(struct kvm_vcpu *vcpu)
225{
226 struct vcpu_vmx *vmx = to_vmx(vcpu);
227
228 if (!vmx->nested.hv_evmcs)
229 return;
230
KarimAllah Ahmeddee9c042019-01-31 21:24:42 +0100231 kvm_vcpu_unmap(vcpu, &vmx->nested.hv_evmcs_map, true);
Vitaly Kuznetsov95fa1012020-03-09 16:52:11 +0100232 vmx->nested.hv_evmcs_vmptr = 0;
Sean Christopherson55d23752018-12-03 13:53:18 -0800233 vmx->nested.hv_evmcs = NULL;
234}
235
Sean Christophersonc61ca2f2020-09-23 11:44:49 -0700236static void vmx_sync_vmcs_host_state(struct vcpu_vmx *vmx,
237 struct loaded_vmcs *prev)
238{
239 struct vmcs_host_state *dest, *src;
240
241 if (unlikely(!vmx->guest_state_loaded))
242 return;
243
244 src = &prev->host_state;
245 dest = &vmx->loaded_vmcs->host_state;
246
247 vmx_set_host_fs_gs(dest, src->fs_sel, src->gs_sel, src->fs_base, src->gs_base);
248 dest->ldt_sel = src->ldt_sel;
249#ifdef CONFIG_X86_64
250 dest->ds_sel = src->ds_sel;
251 dest->es_sel = src->es_sel;
252#endif
253}
254
255static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
256{
257 struct vcpu_vmx *vmx = to_vmx(vcpu);
258 struct loaded_vmcs *prev;
259 int cpu;
260
261 if (vmx->loaded_vmcs == vmcs)
262 return;
263
264 cpu = get_cpu();
265 prev = vmx->loaded_vmcs;
266 vmx->loaded_vmcs = vmcs;
267 vmx_vcpu_load_vmcs(vcpu, cpu, prev);
268 vmx_sync_vmcs_host_state(vmx, prev);
269 put_cpu();
270
271 vmx_register_cache_reset(vcpu);
272}
273
Sean Christopherson55d23752018-12-03 13:53:18 -0800274/*
275 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
276 * just stops using VMX.
277 */
278static void free_nested(struct kvm_vcpu *vcpu)
279{
280 struct vcpu_vmx *vmx = to_vmx(vcpu);
281
282 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
283 return;
284
Jan Kiszkacf645272019-07-21 13:52:18 +0200285 kvm_clear_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
286
Sean Christopherson55d23752018-12-03 13:53:18 -0800287 vmx->nested.vmxon = false;
288 vmx->nested.smm.vmxon = false;
289 free_vpid(vmx->nested.vpid02);
290 vmx->nested.posted_intr_nv = -1;
291 vmx->nested.current_vmptr = -1ull;
292 if (enable_shadow_vmcs) {
293 vmx_disable_shadow_vmcs(vmx);
294 vmcs_clear(vmx->vmcs01.shadow_vmcs);
295 free_vmcs(vmx->vmcs01.shadow_vmcs);
296 vmx->vmcs01.shadow_vmcs = NULL;
297 }
298 kfree(vmx->nested.cached_vmcs12);
Jan Kiszkac6bf2ae2019-07-21 16:01:36 +0200299 vmx->nested.cached_vmcs12 = NULL;
Sean Christopherson55d23752018-12-03 13:53:18 -0800300 kfree(vmx->nested.cached_shadow_vmcs12);
Jan Kiszkac6bf2ae2019-07-21 16:01:36 +0200301 vmx->nested.cached_shadow_vmcs12 = NULL;
Sean Christopherson55d23752018-12-03 13:53:18 -0800302 /* Unpin physical memory we referred to in the vmcs02 */
303 if (vmx->nested.apic_access_page) {
Liran Alonb11494b2019-11-21 00:31:47 +0200304 kvm_release_page_clean(vmx->nested.apic_access_page);
Sean Christopherson55d23752018-12-03 13:53:18 -0800305 vmx->nested.apic_access_page = NULL;
306 }
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +0100307 kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map, true);
KarimAllah Ahmed3278e042019-01-31 21:24:38 +0100308 kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map, true);
309 vmx->nested.pi_desc = NULL;
Sean Christopherson55d23752018-12-03 13:53:18 -0800310
311 kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
312
313 nested_release_evmcs(vcpu);
314
315 free_loaded_vmcs(&vmx->nested.vmcs02);
316}
317
Sean Christopherson55d23752018-12-03 13:53:18 -0800318/*
319 * Ensure that the current vmcs of the logical processor is the
320 * vmcs01 of the vcpu before calling free_nested().
321 */
322void nested_vmx_free_vcpu(struct kvm_vcpu *vcpu)
323{
324 vcpu_load(vcpu);
Paolo Bonzinib4b65b52019-01-29 19:12:35 +0100325 vmx_leave_nested(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -0800326 vmx_switch_vmcs(vcpu, &to_vmx(vcpu)->vmcs01);
327 free_nested(vcpu);
328 vcpu_put(vcpu);
329}
330
331static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
332 struct x86_exception *fault)
333{
334 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
335 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson4dcefa32020-04-15 10:55:18 -0700336 u32 vm_exit_reason;
Sean Christopherson55d23752018-12-03 13:53:18 -0800337 unsigned long exit_qualification = vcpu->arch.exit_qualification;
338
339 if (vmx->nested.pml_full) {
Sean Christopherson4dcefa32020-04-15 10:55:18 -0700340 vm_exit_reason = EXIT_REASON_PML_FULL;
Sean Christopherson55d23752018-12-03 13:53:18 -0800341 vmx->nested.pml_full = false;
342 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
343 } else if (fault->error_code & PFERR_RSVD_MASK)
Sean Christopherson4dcefa32020-04-15 10:55:18 -0700344 vm_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Sean Christopherson55d23752018-12-03 13:53:18 -0800345 else
Sean Christopherson4dcefa32020-04-15 10:55:18 -0700346 vm_exit_reason = EXIT_REASON_EPT_VIOLATION;
Sean Christopherson55d23752018-12-03 13:53:18 -0800347
Sean Christopherson4dcefa32020-04-15 10:55:18 -0700348 nested_vmx_vmexit(vcpu, vm_exit_reason, 0, exit_qualification);
Sean Christopherson55d23752018-12-03 13:53:18 -0800349 vmcs12->guest_physical_address = fault->address;
350}
351
352static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
353{
354 WARN_ON(mmu_is_nested(vcpu));
355
356 vcpu->arch.mmu = &vcpu->arch.guest_mmu;
357 kvm_init_shadow_ept_mmu(vcpu,
358 to_vmx(vcpu)->nested.msrs.ept_caps &
359 VMX_EPT_EXECUTE_ONLY_BIT,
360 nested_ept_ad_enabled(vcpu),
Sean Christophersonac69dfa2020-03-02 18:02:37 -0800361 nested_ept_get_eptp(vcpu));
Sean Christophersond8dd54e2020-03-02 18:02:39 -0800362 vcpu->arch.mmu->get_guest_pgd = nested_ept_get_eptp;
Sean Christopherson55d23752018-12-03 13:53:18 -0800363 vcpu->arch.mmu->inject_page_fault = nested_ept_inject_page_fault;
364 vcpu->arch.mmu->get_pdptr = kvm_pdptr_read;
365
366 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
367}
368
369static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
370{
371 vcpu->arch.mmu = &vcpu->arch.root_mmu;
372 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
373}
374
375static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
376 u16 error_code)
377{
378 bool inequality, bit;
379
380 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
381 inequality =
382 (error_code & vmcs12->page_fault_error_code_mask) !=
383 vmcs12->page_fault_error_code_match;
384 return inequality ^ bit;
385}
386
387
388/*
389 * KVM wants to inject page-faults which it got to the guest. This function
390 * checks whether in a nested guest, we need to inject them to L1 or L2.
391 */
392static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
393{
394 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
395 unsigned int nr = vcpu->arch.exception.nr;
396 bool has_payload = vcpu->arch.exception.has_payload;
397 unsigned long payload = vcpu->arch.exception.payload;
398
399 if (nr == PF_VECTOR) {
400 if (vcpu->arch.exception.nested_apf) {
401 *exit_qual = vcpu->arch.apf.nested_apf_token;
402 return 1;
403 }
404 if (nested_vmx_is_page_fault_vmexit(vmcs12,
405 vcpu->arch.exception.error_code)) {
406 *exit_qual = has_payload ? payload : vcpu->arch.cr2;
407 return 1;
408 }
409 } else if (vmcs12->exception_bitmap & (1u << nr)) {
410 if (nr == DB_VECTOR) {
411 if (!has_payload) {
412 payload = vcpu->arch.dr6;
413 payload &= ~(DR6_FIXED_1 | DR6_BT);
414 payload ^= DR6_RTM;
415 }
416 *exit_qual = payload;
417 } else
418 *exit_qual = 0;
419 return 1;
420 }
421
422 return 0;
423}
424
425
426static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
427 struct x86_exception *fault)
428{
429 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
430
431 WARN_ON(!is_guest_mode(vcpu));
432
433 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
434 !to_vmx(vcpu)->nested.nested_run_pending) {
435 vmcs12->vm_exit_intr_error_code = fault->error_code;
436 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
437 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
438 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
439 fault->address);
440 } else {
441 kvm_inject_page_fault(vcpu, fault);
442 }
443}
444
Sean Christopherson55d23752018-12-03 13:53:18 -0800445static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
446 struct vmcs12 *vmcs12)
447{
448 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
449 return 0;
450
Sean Christopherson5497b952019-07-11 08:58:29 -0700451 if (CC(!page_address_valid(vcpu, vmcs12->io_bitmap_a)) ||
452 CC(!page_address_valid(vcpu, vmcs12->io_bitmap_b)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800453 return -EINVAL;
454
455 return 0;
456}
457
458static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
459 struct vmcs12 *vmcs12)
460{
461 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
462 return 0;
463
Sean Christopherson5497b952019-07-11 08:58:29 -0700464 if (CC(!page_address_valid(vcpu, vmcs12->msr_bitmap)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800465 return -EINVAL;
466
467 return 0;
468}
469
470static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
471 struct vmcs12 *vmcs12)
472{
473 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
474 return 0;
475
Sean Christopherson5497b952019-07-11 08:58:29 -0700476 if (CC(!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800477 return -EINVAL;
478
479 return 0;
480}
481
482/*
483 * Check if MSR is intercepted for L01 MSR bitmap.
484 */
485static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
486{
487 unsigned long *msr_bitmap;
488 int f = sizeof(unsigned long);
489
490 if (!cpu_has_vmx_msr_bitmap())
491 return true;
492
493 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
494
495 if (msr <= 0x1fff) {
496 return !!test_bit(msr, msr_bitmap + 0x800 / f);
497 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
498 msr &= 0x1fff;
499 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
500 }
501
502 return true;
503}
504
505/*
506 * If a msr is allowed by L0, we should check whether it is allowed by L1.
507 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
508 */
509static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
510 unsigned long *msr_bitmap_nested,
511 u32 msr, int type)
512{
513 int f = sizeof(unsigned long);
514
515 /*
516 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
517 * have the write-low and read-high bitmap offsets the wrong way round.
518 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
519 */
520 if (msr <= 0x1fff) {
521 if (type & MSR_TYPE_R &&
522 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
523 /* read-low */
524 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
525
526 if (type & MSR_TYPE_W &&
527 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
528 /* write-low */
529 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
530
531 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
532 msr &= 0x1fff;
533 if (type & MSR_TYPE_R &&
534 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
535 /* read-high */
536 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
537
538 if (type & MSR_TYPE_W &&
539 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
540 /* write-high */
541 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
542
543 }
544}
545
Miaohe Linffdbd502020-02-07 23:22:45 +0800546static inline void enable_x2apic_msr_intercepts(unsigned long *msr_bitmap)
547{
Marc Orracff7842019-04-01 23:55:59 -0700548 int msr;
549
550 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
551 unsigned word = msr / BITS_PER_LONG;
552
553 msr_bitmap[word] = ~0;
554 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
555 }
556}
557
Sean Christopherson55d23752018-12-03 13:53:18 -0800558/*
559 * Merge L0's and L1's MSR bitmap, return false to indicate that
560 * we do not use the hardware.
561 */
562static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
563 struct vmcs12 *vmcs12)
564{
565 int msr;
Sean Christopherson55d23752018-12-03 13:53:18 -0800566 unsigned long *msr_bitmap_l1;
567 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
KarimAllah Ahmed31f0b6c2019-01-31 21:24:36 +0100568 struct kvm_host_map *map = &to_vmx(vcpu)->nested.msr_bitmap_map;
Sean Christopherson55d23752018-12-03 13:53:18 -0800569
570 /* Nothing to do if the MSR bitmap is not in use. */
571 if (!cpu_has_vmx_msr_bitmap() ||
572 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
573 return false;
574
KarimAllah Ahmed31f0b6c2019-01-31 21:24:36 +0100575 if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->msr_bitmap), map))
Sean Christopherson55d23752018-12-03 13:53:18 -0800576 return false;
577
KarimAllah Ahmed31f0b6c2019-01-31 21:24:36 +0100578 msr_bitmap_l1 = (unsigned long *)map->hva;
Sean Christopherson55d23752018-12-03 13:53:18 -0800579
Marc Orracff7842019-04-01 23:55:59 -0700580 /*
581 * To keep the control flow simple, pay eight 8-byte writes (sixteen
582 * 4-byte writes on 32-bit systems) up front to enable intercepts for
583 * the x2APIC MSR range and selectively disable them below.
584 */
585 enable_x2apic_msr_intercepts(msr_bitmap_l0);
Sean Christopherson55d23752018-12-03 13:53:18 -0800586
Marc Orracff7842019-04-01 23:55:59 -0700587 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
588 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
589 /*
590 * L0 need not intercept reads for MSRs between 0x800
591 * and 0x8ff, it just lets the processor take the value
592 * from the virtual-APIC page; take those 256 bits
593 * directly from the L1 bitmap.
594 */
595 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
596 unsigned word = msr / BITS_PER_LONG;
597
598 msr_bitmap_l0[word] = msr_bitmap_l1[word];
599 }
600 }
601
Sean Christopherson55d23752018-12-03 13:53:18 -0800602 nested_vmx_disable_intercept_for_msr(
603 msr_bitmap_l1, msr_bitmap_l0,
Marc Orracff7842019-04-01 23:55:59 -0700604 X2APIC_MSR(APIC_TASKPRI),
Marc Orrc73f4c92019-04-01 23:56:00 -0700605 MSR_TYPE_R | MSR_TYPE_W);
Marc Orracff7842019-04-01 23:55:59 -0700606
607 if (nested_cpu_has_vid(vmcs12)) {
608 nested_vmx_disable_intercept_for_msr(
609 msr_bitmap_l1, msr_bitmap_l0,
610 X2APIC_MSR(APIC_EOI),
611 MSR_TYPE_W);
612 nested_vmx_disable_intercept_for_msr(
613 msr_bitmap_l1, msr_bitmap_l0,
614 X2APIC_MSR(APIC_SELF_IPI),
615 MSR_TYPE_W);
616 }
Sean Christopherson55d23752018-12-03 13:53:18 -0800617 }
618
Sean Christophersond69129b2019-05-08 07:32:15 -0700619 /* KVM unconditionally exposes the FS/GS base MSRs to L1. */
620 nested_vmx_disable_intercept_for_msr(msr_bitmap_l1, msr_bitmap_l0,
621 MSR_FS_BASE, MSR_TYPE_RW);
622
623 nested_vmx_disable_intercept_for_msr(msr_bitmap_l1, msr_bitmap_l0,
624 MSR_GS_BASE, MSR_TYPE_RW);
625
626 nested_vmx_disable_intercept_for_msr(msr_bitmap_l1, msr_bitmap_l0,
627 MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
628
629 /*
630 * Checking the L0->L1 bitmap is trying to verify two things:
631 *
632 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
633 * ensures that we do not accidentally generate an L02 MSR bitmap
634 * from the L12 MSR bitmap that is too permissive.
635 * 2. That L1 or L2s have actually used the MSR. This avoids
636 * unnecessarily merging of the bitmap if the MSR is unused. This
637 * works properly because we only update the L01 MSR bitmap lazily.
638 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
639 * updated to reflect this when L1 (or its L2s) actually write to
640 * the MSR.
641 */
642 if (!msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL))
Sean Christopherson55d23752018-12-03 13:53:18 -0800643 nested_vmx_disable_intercept_for_msr(
644 msr_bitmap_l1, msr_bitmap_l0,
645 MSR_IA32_SPEC_CTRL,
646 MSR_TYPE_R | MSR_TYPE_W);
647
Sean Christophersond69129b2019-05-08 07:32:15 -0700648 if (!msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD))
Sean Christopherson55d23752018-12-03 13:53:18 -0800649 nested_vmx_disable_intercept_for_msr(
650 msr_bitmap_l1, msr_bitmap_l0,
651 MSR_IA32_PRED_CMD,
652 MSR_TYPE_W);
653
KarimAllah Ahmed31f0b6c2019-01-31 21:24:36 +0100654 kvm_vcpu_unmap(vcpu, &to_vmx(vcpu)->nested.msr_bitmap_map, false);
Sean Christopherson55d23752018-12-03 13:53:18 -0800655
656 return true;
657}
658
659static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
660 struct vmcs12 *vmcs12)
661{
KarimAllah Ahmed88925302019-01-31 21:24:41 +0100662 struct kvm_host_map map;
Sean Christopherson55d23752018-12-03 13:53:18 -0800663 struct vmcs12 *shadow;
Sean Christopherson55d23752018-12-03 13:53:18 -0800664
665 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
666 vmcs12->vmcs_link_pointer == -1ull)
667 return;
668
669 shadow = get_shadow_vmcs12(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -0800670
KarimAllah Ahmed88925302019-01-31 21:24:41 +0100671 if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->vmcs_link_pointer), &map))
672 return;
Sean Christopherson55d23752018-12-03 13:53:18 -0800673
KarimAllah Ahmed88925302019-01-31 21:24:41 +0100674 memcpy(shadow, map.hva, VMCS12_SIZE);
675 kvm_vcpu_unmap(vcpu, &map, false);
Sean Christopherson55d23752018-12-03 13:53:18 -0800676}
677
678static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
679 struct vmcs12 *vmcs12)
680{
681 struct vcpu_vmx *vmx = to_vmx(vcpu);
682
683 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
684 vmcs12->vmcs_link_pointer == -1ull)
685 return;
686
687 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
688 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
689}
690
691/*
692 * In nested virtualization, check if L1 has set
693 * VM_EXIT_ACK_INTR_ON_EXIT
694 */
695static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
696{
697 return get_vmcs12(vcpu)->vm_exit_controls &
698 VM_EXIT_ACK_INTR_ON_EXIT;
699}
700
Sean Christopherson55d23752018-12-03 13:53:18 -0800701static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
702 struct vmcs12 *vmcs12)
703{
704 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
Sean Christopherson5497b952019-07-11 08:58:29 -0700705 CC(!page_address_valid(vcpu, vmcs12->apic_access_addr)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800706 return -EINVAL;
707 else
708 return 0;
709}
710
711static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
712 struct vmcs12 *vmcs12)
713{
714 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
715 !nested_cpu_has_apic_reg_virt(vmcs12) &&
716 !nested_cpu_has_vid(vmcs12) &&
717 !nested_cpu_has_posted_intr(vmcs12))
718 return 0;
719
720 /*
721 * If virtualize x2apic mode is enabled,
722 * virtualize apic access must be disabled.
723 */
Sean Christopherson5497b952019-07-11 08:58:29 -0700724 if (CC(nested_cpu_has_virt_x2apic_mode(vmcs12) &&
725 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800726 return -EINVAL;
727
728 /*
729 * If virtual interrupt delivery is enabled,
730 * we must exit on external interrupts.
731 */
Sean Christopherson5497b952019-07-11 08:58:29 -0700732 if (CC(nested_cpu_has_vid(vmcs12) && !nested_exit_on_intr(vcpu)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800733 return -EINVAL;
734
735 /*
736 * bits 15:8 should be zero in posted_intr_nv,
737 * the descriptor address has been already checked
738 * in nested_get_vmcs12_pages.
739 *
740 * bits 5:0 of posted_intr_desc_addr should be zero.
741 */
742 if (nested_cpu_has_posted_intr(vmcs12) &&
Sean Christopherson5497b952019-07-11 08:58:29 -0700743 (CC(!nested_cpu_has_vid(vmcs12)) ||
744 CC(!nested_exit_intr_ack_set(vcpu)) ||
745 CC((vmcs12->posted_intr_nv & 0xff00)) ||
746 CC((vmcs12->posted_intr_desc_addr & 0x3f)) ||
747 CC((vmcs12->posted_intr_desc_addr >> cpuid_maxphyaddr(vcpu)))))
Sean Christopherson55d23752018-12-03 13:53:18 -0800748 return -EINVAL;
749
750 /* tpr shadow is needed by all apicv features. */
Sean Christopherson5497b952019-07-11 08:58:29 -0700751 if (CC(!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800752 return -EINVAL;
753
754 return 0;
755}
756
757static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
Sean Christophersonf9b245e2018-12-12 13:30:08 -0500758 u32 count, u64 addr)
Sean Christopherson55d23752018-12-03 13:53:18 -0800759{
Sean Christopherson55d23752018-12-03 13:53:18 -0800760 int maxphyaddr;
Sean Christopherson55d23752018-12-03 13:53:18 -0800761
Sean Christopherson55d23752018-12-03 13:53:18 -0800762 if (count == 0)
763 return 0;
764 maxphyaddr = cpuid_maxphyaddr(vcpu);
765 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
Sean Christophersonf9b245e2018-12-12 13:30:08 -0500766 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr)
Sean Christopherson55d23752018-12-03 13:53:18 -0800767 return -EINVAL;
Sean Christophersonf9b245e2018-12-12 13:30:08 -0500768
Sean Christopherson55d23752018-12-03 13:53:18 -0800769 return 0;
770}
771
Krish Sadhukhan61446ba2018-12-12 13:30:09 -0500772static int nested_vmx_check_exit_msr_switch_controls(struct kvm_vcpu *vcpu,
773 struct vmcs12 *vmcs12)
Sean Christopherson55d23752018-12-03 13:53:18 -0800774{
Sean Christopherson5497b952019-07-11 08:58:29 -0700775 if (CC(nested_vmx_check_msr_switch(vcpu,
776 vmcs12->vm_exit_msr_load_count,
777 vmcs12->vm_exit_msr_load_addr)) ||
778 CC(nested_vmx_check_msr_switch(vcpu,
779 vmcs12->vm_exit_msr_store_count,
780 vmcs12->vm_exit_msr_store_addr)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800781 return -EINVAL;
Sean Christophersonf9b245e2018-12-12 13:30:08 -0500782
Sean Christopherson55d23752018-12-03 13:53:18 -0800783 return 0;
784}
785
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -0500786static int nested_vmx_check_entry_msr_switch_controls(struct kvm_vcpu *vcpu,
787 struct vmcs12 *vmcs12)
Krish Sadhukhan61446ba2018-12-12 13:30:09 -0500788{
Sean Christopherson5497b952019-07-11 08:58:29 -0700789 if (CC(nested_vmx_check_msr_switch(vcpu,
790 vmcs12->vm_entry_msr_load_count,
791 vmcs12->vm_entry_msr_load_addr)))
Krish Sadhukhan61446ba2018-12-12 13:30:09 -0500792 return -EINVAL;
793
794 return 0;
795}
796
Sean Christopherson55d23752018-12-03 13:53:18 -0800797static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
798 struct vmcs12 *vmcs12)
799{
800 if (!nested_cpu_has_pml(vmcs12))
801 return 0;
802
Sean Christopherson5497b952019-07-11 08:58:29 -0700803 if (CC(!nested_cpu_has_ept(vmcs12)) ||
804 CC(!page_address_valid(vcpu, vmcs12->pml_address)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800805 return -EINVAL;
806
807 return 0;
808}
809
810static int nested_vmx_check_unrestricted_guest_controls(struct kvm_vcpu *vcpu,
811 struct vmcs12 *vmcs12)
812{
Sean Christopherson5497b952019-07-11 08:58:29 -0700813 if (CC(nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST) &&
814 !nested_cpu_has_ept(vmcs12)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800815 return -EINVAL;
816 return 0;
817}
818
819static int nested_vmx_check_mode_based_ept_exec_controls(struct kvm_vcpu *vcpu,
820 struct vmcs12 *vmcs12)
821{
Sean Christopherson5497b952019-07-11 08:58:29 -0700822 if (CC(nested_cpu_has2(vmcs12, SECONDARY_EXEC_MODE_BASED_EPT_EXEC) &&
823 !nested_cpu_has_ept(vmcs12)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800824 return -EINVAL;
825 return 0;
826}
827
828static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
829 struct vmcs12 *vmcs12)
830{
831 if (!nested_cpu_has_shadow_vmcs(vmcs12))
832 return 0;
833
Sean Christopherson5497b952019-07-11 08:58:29 -0700834 if (CC(!page_address_valid(vcpu, vmcs12->vmread_bitmap)) ||
835 CC(!page_address_valid(vcpu, vmcs12->vmwrite_bitmap)))
Sean Christopherson55d23752018-12-03 13:53:18 -0800836 return -EINVAL;
837
838 return 0;
839}
840
841static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
842 struct vmx_msr_entry *e)
843{
844 /* x2APIC MSR accesses are not allowed */
Sean Christopherson5497b952019-07-11 08:58:29 -0700845 if (CC(vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8))
Sean Christopherson55d23752018-12-03 13:53:18 -0800846 return -EINVAL;
Sean Christopherson5497b952019-07-11 08:58:29 -0700847 if (CC(e->index == MSR_IA32_UCODE_WRITE) || /* SDM Table 35-2 */
848 CC(e->index == MSR_IA32_UCODE_REV))
Sean Christopherson55d23752018-12-03 13:53:18 -0800849 return -EINVAL;
Sean Christopherson5497b952019-07-11 08:58:29 -0700850 if (CC(e->reserved != 0))
Sean Christopherson55d23752018-12-03 13:53:18 -0800851 return -EINVAL;
852 return 0;
853}
854
855static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
856 struct vmx_msr_entry *e)
857{
Sean Christopherson5497b952019-07-11 08:58:29 -0700858 if (CC(e->index == MSR_FS_BASE) ||
859 CC(e->index == MSR_GS_BASE) ||
860 CC(e->index == MSR_IA32_SMM_MONITOR_CTL) || /* SMM is not supported */
Sean Christopherson55d23752018-12-03 13:53:18 -0800861 nested_vmx_msr_check_common(vcpu, e))
862 return -EINVAL;
863 return 0;
864}
865
866static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
867 struct vmx_msr_entry *e)
868{
Sean Christopherson5497b952019-07-11 08:58:29 -0700869 if (CC(e->index == MSR_IA32_SMBASE) || /* SMM is not supported */
Sean Christopherson55d23752018-12-03 13:53:18 -0800870 nested_vmx_msr_check_common(vcpu, e))
871 return -EINVAL;
872 return 0;
873}
874
Marc Orrf0b51052019-09-17 11:50:57 -0700875static u32 nested_vmx_max_atomic_switch_msrs(struct kvm_vcpu *vcpu)
876{
877 struct vcpu_vmx *vmx = to_vmx(vcpu);
878 u64 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
879 vmx->nested.msrs.misc_high);
880
881 return (vmx_misc_max_msr(vmx_misc) + 1) * VMX_MISC_MSR_LIST_MULTIPLIER;
882}
883
Sean Christopherson55d23752018-12-03 13:53:18 -0800884/*
885 * Load guest's/host's msr at nested entry/exit.
886 * return 0 for success, entry index for failure.
Marc Orrf0b51052019-09-17 11:50:57 -0700887 *
888 * One of the failure modes for MSR load/store is when a list exceeds the
889 * virtual hardware's capacity. To maintain compatibility with hardware inasmuch
890 * as possible, process all valid entries before failing rather than precheck
891 * for a capacity violation.
Sean Christopherson55d23752018-12-03 13:53:18 -0800892 */
893static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
894{
895 u32 i;
896 struct vmx_msr_entry e;
Marc Orrf0b51052019-09-17 11:50:57 -0700897 u32 max_msr_list_size = nested_vmx_max_atomic_switch_msrs(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -0800898
Sean Christopherson55d23752018-12-03 13:53:18 -0800899 for (i = 0; i < count; i++) {
Marc Orrf0b51052019-09-17 11:50:57 -0700900 if (unlikely(i >= max_msr_list_size))
901 goto fail;
902
Sean Christopherson55d23752018-12-03 13:53:18 -0800903 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
904 &e, sizeof(e))) {
905 pr_debug_ratelimited(
906 "%s cannot read MSR entry (%u, 0x%08llx)\n",
907 __func__, i, gpa + i * sizeof(e));
908 goto fail;
909 }
910 if (nested_vmx_load_msr_check(vcpu, &e)) {
911 pr_debug_ratelimited(
912 "%s check failed (%u, 0x%x, 0x%x)\n",
913 __func__, i, e.index, e.reserved);
914 goto fail;
915 }
Sean Christophersonf20935d2019-09-05 14:22:54 -0700916 if (kvm_set_msr(vcpu, e.index, e.value)) {
Sean Christopherson55d23752018-12-03 13:53:18 -0800917 pr_debug_ratelimited(
918 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
919 __func__, i, e.index, e.value);
920 goto fail;
921 }
922 }
923 return 0;
924fail:
Sean Christopherson68cda402020-05-11 15:05:29 -0700925 /* Note, max_msr_list_size is at most 4096, i.e. this can't wrap. */
Sean Christopherson55d23752018-12-03 13:53:18 -0800926 return i + 1;
927}
928
Aaron Lewis662f1d12019-11-07 21:14:39 -0800929static bool nested_vmx_get_vmexit_msr_value(struct kvm_vcpu *vcpu,
930 u32 msr_index,
931 u64 *data)
932{
933 struct vcpu_vmx *vmx = to_vmx(vcpu);
934
935 /*
936 * If the L0 hypervisor stored a more accurate value for the TSC that
937 * does not include the time taken for emulation of the L2->L1
938 * VM-exit in L0, use the more accurate value.
939 */
940 if (msr_index == MSR_IA32_TSC) {
941 int index = vmx_find_msr_index(&vmx->msr_autostore.guest,
942 MSR_IA32_TSC);
943
944 if (index >= 0) {
945 u64 val = vmx->msr_autostore.guest.val[index].value;
946
947 *data = kvm_read_l1_tsc(vcpu, val);
948 return true;
949 }
950 }
951
952 if (kvm_get_msr(vcpu, msr_index, data)) {
953 pr_debug_ratelimited("%s cannot read MSR (0x%x)\n", __func__,
954 msr_index);
955 return false;
956 }
957 return true;
958}
959
Aaron Lewis365d3d52019-11-07 21:14:36 -0800960static bool read_and_check_msr_entry(struct kvm_vcpu *vcpu, u64 gpa, int i,
961 struct vmx_msr_entry *e)
962{
963 if (kvm_vcpu_read_guest(vcpu,
964 gpa + i * sizeof(*e),
965 e, 2 * sizeof(u32))) {
966 pr_debug_ratelimited(
967 "%s cannot read MSR entry (%u, 0x%08llx)\n",
968 __func__, i, gpa + i * sizeof(*e));
969 return false;
970 }
971 if (nested_vmx_store_msr_check(vcpu, e)) {
972 pr_debug_ratelimited(
973 "%s check failed (%u, 0x%x, 0x%x)\n",
974 __func__, i, e->index, e->reserved);
975 return false;
976 }
977 return true;
978}
979
Sean Christopherson55d23752018-12-03 13:53:18 -0800980static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
981{
Sean Christophersonf20935d2019-09-05 14:22:54 -0700982 u64 data;
Sean Christopherson55d23752018-12-03 13:53:18 -0800983 u32 i;
984 struct vmx_msr_entry e;
Marc Orrf0b51052019-09-17 11:50:57 -0700985 u32 max_msr_list_size = nested_vmx_max_atomic_switch_msrs(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -0800986
987 for (i = 0; i < count; i++) {
Marc Orrf0b51052019-09-17 11:50:57 -0700988 if (unlikely(i >= max_msr_list_size))
989 return -EINVAL;
990
Aaron Lewis365d3d52019-11-07 21:14:36 -0800991 if (!read_and_check_msr_entry(vcpu, gpa, i, &e))
Sean Christopherson55d23752018-12-03 13:53:18 -0800992 return -EINVAL;
Aaron Lewis365d3d52019-11-07 21:14:36 -0800993
Aaron Lewis662f1d12019-11-07 21:14:39 -0800994 if (!nested_vmx_get_vmexit_msr_value(vcpu, e.index, &data))
Sean Christopherson55d23752018-12-03 13:53:18 -0800995 return -EINVAL;
Aaron Lewis662f1d12019-11-07 21:14:39 -0800996
Sean Christopherson55d23752018-12-03 13:53:18 -0800997 if (kvm_vcpu_write_guest(vcpu,
998 gpa + i * sizeof(e) +
999 offsetof(struct vmx_msr_entry, value),
Sean Christophersonf20935d2019-09-05 14:22:54 -07001000 &data, sizeof(data))) {
Sean Christopherson55d23752018-12-03 13:53:18 -08001001 pr_debug_ratelimited(
1002 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Sean Christophersonf20935d2019-09-05 14:22:54 -07001003 __func__, i, e.index, data);
Sean Christopherson55d23752018-12-03 13:53:18 -08001004 return -EINVAL;
1005 }
1006 }
1007 return 0;
1008}
1009
Aaron Lewis662f1d12019-11-07 21:14:39 -08001010static bool nested_msr_store_list_has_msr(struct kvm_vcpu *vcpu, u32 msr_index)
1011{
1012 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1013 u32 count = vmcs12->vm_exit_msr_store_count;
1014 u64 gpa = vmcs12->vm_exit_msr_store_addr;
1015 struct vmx_msr_entry e;
1016 u32 i;
1017
1018 for (i = 0; i < count; i++) {
1019 if (!read_and_check_msr_entry(vcpu, gpa, i, &e))
1020 return false;
1021
1022 if (e.index == msr_index)
1023 return true;
1024 }
1025 return false;
1026}
1027
1028static void prepare_vmx_msr_autostore_list(struct kvm_vcpu *vcpu,
1029 u32 msr_index)
1030{
1031 struct vcpu_vmx *vmx = to_vmx(vcpu);
1032 struct vmx_msrs *autostore = &vmx->msr_autostore.guest;
1033 bool in_vmcs12_store_list;
1034 int msr_autostore_index;
1035 bool in_autostore_list;
1036 int last;
1037
1038 msr_autostore_index = vmx_find_msr_index(autostore, msr_index);
1039 in_autostore_list = msr_autostore_index >= 0;
1040 in_vmcs12_store_list = nested_msr_store_list_has_msr(vcpu, msr_index);
1041
1042 if (in_vmcs12_store_list && !in_autostore_list) {
1043 if (autostore->nr == NR_LOADSTORE_MSRS) {
1044 /*
1045 * Emulated VMEntry does not fail here. Instead a less
1046 * accurate value will be returned by
1047 * nested_vmx_get_vmexit_msr_value() using kvm_get_msr()
1048 * instead of reading the value from the vmcs02 VMExit
1049 * MSR-store area.
1050 */
1051 pr_warn_ratelimited(
1052 "Not enough msr entries in msr_autostore. Can't add msr %x\n",
1053 msr_index);
1054 return;
1055 }
1056 last = autostore->nr++;
1057 autostore->val[last].index = msr_index;
1058 } else if (!in_vmcs12_store_list && in_autostore_list) {
1059 last = --autostore->nr;
1060 autostore->val[msr_autostore_index] = autostore->val[last];
1061 }
1062}
1063
Sean Christopherson55d23752018-12-03 13:53:18 -08001064static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
1065{
1066 unsigned long invalid_mask;
1067
1068 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
1069 return (val & invalid_mask) == 0;
1070}
1071
1072/*
Sean Christopherson41fab65e2020-03-20 14:28:29 -07001073 * Returns true if the MMU needs to be sync'd on nested VM-Enter/VM-Exit.
1074 * tl;dr: the MMU needs a sync if L0 is using shadow paging and L1 didn't
1075 * enable VPID for L2 (implying it expects a TLB flush on VMX transitions).
1076 * Here's why.
1077 *
1078 * If EPT is enabled by L0 a sync is never needed:
1079 * - if it is disabled by L1, then L0 is not shadowing L1 or L2 PTEs, there
1080 * cannot be unsync'd SPTEs for either L1 or L2.
1081 *
1082 * - if it is also enabled by L1, then L0 doesn't need to sync on VM-Enter
1083 * VM-Enter as VM-Enter isn't required to invalidate guest-physical mappings
1084 * (irrespective of VPID), i.e. L1 can't rely on the (virtual) CPU to flush
1085 * stale guest-physical mappings for L2 from the TLB. And as above, L0 isn't
1086 * shadowing L1 PTEs so there are no unsync'd SPTEs to sync on VM-Exit.
1087 *
1088 * If EPT is disabled by L0:
1089 * - if VPID is enabled by L1 (for L2), the situation is similar to when L1
1090 * enables EPT: L0 doesn't need to sync as VM-Enter and VM-Exit aren't
1091 * required to invalidate linear mappings (EPT is disabled so there are
1092 * no combined or guest-physical mappings), i.e. L1 can't rely on the
1093 * (virtual) CPU to flush stale linear mappings for either L2 or itself (L1).
1094 *
1095 * - however if VPID is disabled by L1, then a sync is needed as L1 expects all
1096 * linear mappings (EPT is disabled so there are no combined or guest-physical
1097 * mappings) to be invalidated on both VM-Enter and VM-Exit.
1098 *
1099 * Note, this logic is subtly different than nested_has_guest_tlb_tag(), which
1100 * additionally checks that L2 has been assigned a VPID (when EPT is disabled).
1101 * Whether or not L2 has been assigned a VPID by L0 is irrelevant with respect
1102 * to L1's expectations, e.g. L0 needs to invalidate hardware TLB entries if L2
1103 * doesn't have a unique VPID to prevent reusing L1's entries (assuming L1 has
1104 * been assigned a VPID), but L0 doesn't need to do a MMU sync because L1
1105 * doesn't expect stale (virtual) TLB entries to be flushed, i.e. L1 doesn't
1106 * know that L0 will flush the TLB and so L1 will do INVVPID as needed to flush
1107 * stale TLB entries, at which point L0 will sync L2's MMU.
1108 */
1109static bool nested_vmx_transition_mmu_sync(struct kvm_vcpu *vcpu)
1110{
1111 return !enable_ept && !nested_cpu_has_vpid(get_vmcs12(vcpu));
1112}
1113
1114/*
Sean Christophersonea79a752020-02-04 07:32:59 -08001115 * Load guest's/host's cr3 at nested entry/exit. @nested_ept is true if we are
1116 * emulating VM-Entry into a guest with EPT enabled. On failure, the expected
1117 * Exit Qualification (for a VM-Entry consistency check VM-Exit) is assigned to
1118 * @entry_failure_code.
Sean Christopherson55d23752018-12-03 13:53:18 -08001119 */
1120static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Sean Christopherson68cda402020-05-11 15:05:29 -07001121 enum vm_entry_failure_code *entry_failure_code)
Sean Christopherson55d23752018-12-03 13:53:18 -08001122{
Sean Christopherson0cc69202020-05-01 21:32:26 -07001123 if (CC(!nested_cr3_valid(vcpu, cr3))) {
1124 *entry_failure_code = ENTRY_FAIL_DEFAULT;
1125 return -EINVAL;
1126 }
Sean Christopherson55d23752018-12-03 13:53:18 -08001127
Sean Christopherson0cc69202020-05-01 21:32:26 -07001128 /*
1129 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
1130 * must not be dereferenced.
1131 */
1132 if (!nested_ept && is_pae_paging(vcpu) &&
1133 (cr3 != kvm_read_cr3(vcpu) || pdptrs_changed(vcpu))) {
1134 if (CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))) {
1135 *entry_failure_code = ENTRY_FAIL_PDPTE;
1136 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08001137 }
1138 }
1139
Sean Christopherson41fab65e2020-03-20 14:28:29 -07001140 /*
Sean Christopherson9805c5f2020-03-20 14:28:30 -07001141 * Unconditionally skip the TLB flush on fast CR3 switch, all TLB
1142 * flushes are handled by nested_vmx_transition_tlb_flush(). See
1143 * nested_vmx_transition_mmu_sync for details on skipping the MMU sync.
Sean Christopherson41fab65e2020-03-20 14:28:29 -07001144 */
Sean Christopherson55d23752018-12-03 13:53:18 -08001145 if (!nested_ept)
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07001146 kvm_mmu_new_pgd(vcpu, cr3, true,
Sean Christopherson41fab65e2020-03-20 14:28:29 -07001147 !nested_vmx_transition_mmu_sync(vcpu));
Sean Christopherson55d23752018-12-03 13:53:18 -08001148
1149 vcpu->arch.cr3 = cr3;
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07001150 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Sean Christopherson55d23752018-12-03 13:53:18 -08001151
1152 kvm_init_mmu(vcpu, false);
1153
1154 return 0;
1155}
1156
1157/*
1158 * Returns if KVM is able to config CPU to tag TLB entries
1159 * populated by L2 differently than TLB entries populated
1160 * by L1.
1161 *
Liran Alon992edea2019-11-20 14:24:52 +02001162 * If L0 uses EPT, L1 and L2 run with different EPTP because
1163 * guest_mode is part of kvm_mmu_page_role. Thus, TLB entries
1164 * are tagged with different EPTP.
Sean Christopherson55d23752018-12-03 13:53:18 -08001165 *
1166 * If L1 uses VPID and we allocated a vpid02, TLB entries are tagged
1167 * with different VPID (L1 entries are tagged with vmx->vpid
1168 * while L2 entries are tagged with vmx->nested.vpid02).
1169 */
1170static bool nested_has_guest_tlb_tag(struct kvm_vcpu *vcpu)
1171{
1172 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1173
Liran Alon992edea2019-11-20 14:24:52 +02001174 return enable_ept ||
Sean Christopherson55d23752018-12-03 13:53:18 -08001175 (nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02);
1176}
1177
Sean Christopherson50b265a2020-03-20 14:28:19 -07001178static void nested_vmx_transition_tlb_flush(struct kvm_vcpu *vcpu,
1179 struct vmcs12 *vmcs12,
1180 bool is_vmenter)
1181{
1182 struct vcpu_vmx *vmx = to_vmx(vcpu);
1183
1184 /*
1185 * If VPID is disabled, linear and combined mappings are flushed on
1186 * VM-Enter/VM-Exit, and guest-physical mappings are valid only for
1187 * their associated EPTP.
1188 */
1189 if (!enable_vpid)
1190 return;
1191
1192 /*
1193 * If vmcs12 doesn't use VPID, L1 expects linear and combined mappings
1194 * for *all* contexts to be flushed on VM-Enter/VM-Exit.
1195 *
1196 * If VPID is enabled and used by vmc12, but L2 does not have a unique
1197 * TLB tag (ASID), i.e. EPT is disabled and KVM was unable to allocate
Sean Christophersonc51e1ff2020-03-20 14:28:22 -07001198 * a VPID for L2, flush the current context as the effective ASID is
1199 * common to both L1 and L2.
Sean Christopherson50b265a2020-03-20 14:28:19 -07001200 *
1201 * Defer the flush so that it runs after vmcs02.EPTP has been set by
1202 * KVM_REQ_LOAD_MMU_PGD (if nested EPT is enabled) and to avoid
1203 * redundant flushes further down the nested pipeline.
1204 *
1205 * If a TLB flush isn't required due to any of the above, and vpid12 is
1206 * changing then the new "virtual" VPID (vpid12) will reuse the same
1207 * "real" VPID (vpid02), and so needs to be sync'd. There is no direct
1208 * mapping between vpid02 and vpid12, vpid02 is per-vCPU and reused for
1209 * all nested vCPUs.
1210 */
Sean Christophersonc51e1ff2020-03-20 14:28:22 -07001211 if (!nested_cpu_has_vpid(vmcs12)) {
Sean Christopherson50b265a2020-03-20 14:28:19 -07001212 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Sean Christophersonc51e1ff2020-03-20 14:28:22 -07001213 } else if (!nested_has_guest_tlb_tag(vcpu)) {
1214 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
Sean Christopherson50b265a2020-03-20 14:28:19 -07001215 } else if (is_vmenter &&
1216 vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
1217 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
1218 vpid_sync_context(nested_get_vpid02(vcpu));
1219 }
1220}
1221
Sean Christopherson55d23752018-12-03 13:53:18 -08001222static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
1223{
1224 superset &= mask;
1225 subset &= mask;
1226
1227 return (superset | subset) == superset;
1228}
1229
1230static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
1231{
1232 const u64 feature_and_reserved =
1233 /* feature (except bit 48; see below) */
1234 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
1235 /* reserved */
1236 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
1237 u64 vmx_basic = vmx->nested.msrs.basic;
1238
1239 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
1240 return -EINVAL;
1241
1242 /*
1243 * KVM does not emulate a version of VMX that constrains physical
1244 * addresses of VMX structures (e.g. VMCS) to 32-bits.
1245 */
1246 if (data & BIT_ULL(48))
1247 return -EINVAL;
1248
1249 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
1250 vmx_basic_vmcs_revision_id(data))
1251 return -EINVAL;
1252
1253 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
1254 return -EINVAL;
1255
1256 vmx->nested.msrs.basic = data;
1257 return 0;
1258}
1259
1260static int
1261vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
1262{
1263 u64 supported;
1264 u32 *lowp, *highp;
1265
1266 switch (msr_index) {
1267 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
1268 lowp = &vmx->nested.msrs.pinbased_ctls_low;
1269 highp = &vmx->nested.msrs.pinbased_ctls_high;
1270 break;
1271 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
1272 lowp = &vmx->nested.msrs.procbased_ctls_low;
1273 highp = &vmx->nested.msrs.procbased_ctls_high;
1274 break;
1275 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
1276 lowp = &vmx->nested.msrs.exit_ctls_low;
1277 highp = &vmx->nested.msrs.exit_ctls_high;
1278 break;
1279 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
1280 lowp = &vmx->nested.msrs.entry_ctls_low;
1281 highp = &vmx->nested.msrs.entry_ctls_high;
1282 break;
1283 case MSR_IA32_VMX_PROCBASED_CTLS2:
1284 lowp = &vmx->nested.msrs.secondary_ctls_low;
1285 highp = &vmx->nested.msrs.secondary_ctls_high;
1286 break;
1287 default:
1288 BUG();
1289 }
1290
1291 supported = vmx_control_msr(*lowp, *highp);
1292
1293 /* Check must-be-1 bits are still 1. */
1294 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
1295 return -EINVAL;
1296
1297 /* Check must-be-0 bits are still 0. */
1298 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
1299 return -EINVAL;
1300
1301 *lowp = data;
1302 *highp = data >> 32;
1303 return 0;
1304}
1305
1306static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
1307{
1308 const u64 feature_and_reserved_bits =
1309 /* feature */
1310 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
1311 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
1312 /* reserved */
1313 GENMASK_ULL(13, 9) | BIT_ULL(31);
1314 u64 vmx_misc;
1315
1316 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
1317 vmx->nested.msrs.misc_high);
1318
1319 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
1320 return -EINVAL;
1321
1322 if ((vmx->nested.msrs.pinbased_ctls_high &
1323 PIN_BASED_VMX_PREEMPTION_TIMER) &&
1324 vmx_misc_preemption_timer_rate(data) !=
1325 vmx_misc_preemption_timer_rate(vmx_misc))
1326 return -EINVAL;
1327
1328 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
1329 return -EINVAL;
1330
1331 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
1332 return -EINVAL;
1333
1334 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
1335 return -EINVAL;
1336
1337 vmx->nested.msrs.misc_low = data;
1338 vmx->nested.msrs.misc_high = data >> 32;
1339
Sean Christopherson55d23752018-12-03 13:53:18 -08001340 return 0;
1341}
1342
1343static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
1344{
1345 u64 vmx_ept_vpid_cap;
1346
1347 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
1348 vmx->nested.msrs.vpid_caps);
1349
1350 /* Every bit is either reserved or a feature bit. */
1351 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
1352 return -EINVAL;
1353
1354 vmx->nested.msrs.ept_caps = data;
1355 vmx->nested.msrs.vpid_caps = data >> 32;
1356 return 0;
1357}
1358
1359static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
1360{
1361 u64 *msr;
1362
1363 switch (msr_index) {
1364 case MSR_IA32_VMX_CR0_FIXED0:
1365 msr = &vmx->nested.msrs.cr0_fixed0;
1366 break;
1367 case MSR_IA32_VMX_CR4_FIXED0:
1368 msr = &vmx->nested.msrs.cr4_fixed0;
1369 break;
1370 default:
1371 BUG();
1372 }
1373
1374 /*
1375 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
1376 * must be 1 in the restored value.
1377 */
1378 if (!is_bitwise_subset(data, *msr, -1ULL))
1379 return -EINVAL;
1380
1381 *msr = data;
1382 return 0;
1383}
1384
1385/*
1386 * Called when userspace is restoring VMX MSRs.
1387 *
1388 * Returns 0 on success, non-0 otherwise.
1389 */
1390int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1391{
1392 struct vcpu_vmx *vmx = to_vmx(vcpu);
1393
1394 /*
1395 * Don't allow changes to the VMX capability MSRs while the vCPU
1396 * is in VMX operation.
1397 */
1398 if (vmx->nested.vmxon)
1399 return -EBUSY;
1400
1401 switch (msr_index) {
1402 case MSR_IA32_VMX_BASIC:
1403 return vmx_restore_vmx_basic(vmx, data);
1404 case MSR_IA32_VMX_PINBASED_CTLS:
1405 case MSR_IA32_VMX_PROCBASED_CTLS:
1406 case MSR_IA32_VMX_EXIT_CTLS:
1407 case MSR_IA32_VMX_ENTRY_CTLS:
1408 /*
1409 * The "non-true" VMX capability MSRs are generated from the
1410 * "true" MSRs, so we do not support restoring them directly.
1411 *
1412 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
1413 * should restore the "true" MSRs with the must-be-1 bits
1414 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
1415 * DEFAULT SETTINGS".
1416 */
1417 return -EINVAL;
1418 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
1419 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
1420 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
1421 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
1422 case MSR_IA32_VMX_PROCBASED_CTLS2:
1423 return vmx_restore_control_msr(vmx, msr_index, data);
1424 case MSR_IA32_VMX_MISC:
1425 return vmx_restore_vmx_misc(vmx, data);
1426 case MSR_IA32_VMX_CR0_FIXED0:
1427 case MSR_IA32_VMX_CR4_FIXED0:
1428 return vmx_restore_fixed0_msr(vmx, msr_index, data);
1429 case MSR_IA32_VMX_CR0_FIXED1:
1430 case MSR_IA32_VMX_CR4_FIXED1:
1431 /*
1432 * These MSRs are generated based on the vCPU's CPUID, so we
1433 * do not support restoring them directly.
1434 */
1435 return -EINVAL;
1436 case MSR_IA32_VMX_EPT_VPID_CAP:
1437 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
1438 case MSR_IA32_VMX_VMCS_ENUM:
1439 vmx->nested.msrs.vmcs_enum = data;
1440 return 0;
Paolo Bonzinie8a70bd2019-07-02 14:40:40 +02001441 case MSR_IA32_VMX_VMFUNC:
1442 if (data & ~vmx->nested.msrs.vmfunc_controls)
1443 return -EINVAL;
1444 vmx->nested.msrs.vmfunc_controls = data;
1445 return 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08001446 default:
1447 /*
1448 * The rest of the VMX capability MSRs do not support restore.
1449 */
1450 return -EINVAL;
1451 }
1452}
1453
1454/* Returns 0 on success, non-0 otherwise. */
1455int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
1456{
1457 switch (msr_index) {
1458 case MSR_IA32_VMX_BASIC:
1459 *pdata = msrs->basic;
1460 break;
1461 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
1462 case MSR_IA32_VMX_PINBASED_CTLS:
1463 *pdata = vmx_control_msr(
1464 msrs->pinbased_ctls_low,
1465 msrs->pinbased_ctls_high);
1466 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
1467 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
1468 break;
1469 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
1470 case MSR_IA32_VMX_PROCBASED_CTLS:
1471 *pdata = vmx_control_msr(
1472 msrs->procbased_ctls_low,
1473 msrs->procbased_ctls_high);
1474 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
1475 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
1476 break;
1477 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
1478 case MSR_IA32_VMX_EXIT_CTLS:
1479 *pdata = vmx_control_msr(
1480 msrs->exit_ctls_low,
1481 msrs->exit_ctls_high);
1482 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
1483 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
1484 break;
1485 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
1486 case MSR_IA32_VMX_ENTRY_CTLS:
1487 *pdata = vmx_control_msr(
1488 msrs->entry_ctls_low,
1489 msrs->entry_ctls_high);
1490 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
1491 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
1492 break;
1493 case MSR_IA32_VMX_MISC:
1494 *pdata = vmx_control_msr(
1495 msrs->misc_low,
1496 msrs->misc_high);
1497 break;
1498 case MSR_IA32_VMX_CR0_FIXED0:
1499 *pdata = msrs->cr0_fixed0;
1500 break;
1501 case MSR_IA32_VMX_CR0_FIXED1:
1502 *pdata = msrs->cr0_fixed1;
1503 break;
1504 case MSR_IA32_VMX_CR4_FIXED0:
1505 *pdata = msrs->cr4_fixed0;
1506 break;
1507 case MSR_IA32_VMX_CR4_FIXED1:
1508 *pdata = msrs->cr4_fixed1;
1509 break;
1510 case MSR_IA32_VMX_VMCS_ENUM:
1511 *pdata = msrs->vmcs_enum;
1512 break;
1513 case MSR_IA32_VMX_PROCBASED_CTLS2:
1514 *pdata = vmx_control_msr(
1515 msrs->secondary_ctls_low,
1516 msrs->secondary_ctls_high);
1517 break;
1518 case MSR_IA32_VMX_EPT_VPID_CAP:
1519 *pdata = msrs->ept_caps |
1520 ((u64)msrs->vpid_caps << 32);
1521 break;
1522 case MSR_IA32_VMX_VMFUNC:
1523 *pdata = msrs->vmfunc_controls;
1524 break;
1525 default:
1526 return 1;
1527 }
1528
1529 return 0;
1530}
1531
1532/*
Sean Christophersonfadcead2019-05-07 08:36:23 -07001533 * Copy the writable VMCS shadow fields back to the VMCS12, in case they have
1534 * been modified by the L1 guest. Note, "writable" in this context means
1535 * "writable by the guest", i.e. tagged SHADOW_FIELD_RW; the set of
1536 * fields tagged SHADOW_FIELD_RO may or may not align with the "read-only"
1537 * VM-exit information fields (which are actually writable if the vCPU is
1538 * configured to support "VMWRITE to any supported field in the VMCS").
Sean Christopherson55d23752018-12-03 13:53:18 -08001539 */
1540static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
1541{
Sean Christopherson55d23752018-12-03 13:53:18 -08001542 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Sean Christophersonfadcead2019-05-07 08:36:23 -07001543 struct vmcs12 *vmcs12 = get_vmcs12(&vmx->vcpu);
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07001544 struct shadow_vmcs_field field;
1545 unsigned long val;
Sean Christophersonfadcead2019-05-07 08:36:23 -07001546 int i;
Sean Christopherson55d23752018-12-03 13:53:18 -08001547
Paolo Bonzini88dddc12019-07-19 18:41:10 +02001548 if (WARN_ON(!shadow_vmcs))
1549 return;
1550
Sean Christopherson55d23752018-12-03 13:53:18 -08001551 preempt_disable();
1552
1553 vmcs_load(shadow_vmcs);
1554
Sean Christophersonfadcead2019-05-07 08:36:23 -07001555 for (i = 0; i < max_shadow_read_write_fields; i++) {
1556 field = shadow_read_write_fields[i];
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07001557 val = __vmcs_readl(field.encoding);
1558 vmcs12_write_any(vmcs12, field.encoding, field.offset, val);
Sean Christopherson55d23752018-12-03 13:53:18 -08001559 }
1560
1561 vmcs_clear(shadow_vmcs);
1562 vmcs_load(vmx->loaded_vmcs->vmcs);
1563
1564 preempt_enable();
1565}
1566
1567static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
1568{
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07001569 const struct shadow_vmcs_field *fields[] = {
Sean Christopherson55d23752018-12-03 13:53:18 -08001570 shadow_read_write_fields,
1571 shadow_read_only_fields
1572 };
1573 const int max_fields[] = {
1574 max_shadow_read_write_fields,
1575 max_shadow_read_only_fields
1576 };
Sean Christopherson55d23752018-12-03 13:53:18 -08001577 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07001578 struct vmcs12 *vmcs12 = get_vmcs12(&vmx->vcpu);
1579 struct shadow_vmcs_field field;
1580 unsigned long val;
1581 int i, q;
Sean Christopherson55d23752018-12-03 13:53:18 -08001582
Paolo Bonzini88dddc12019-07-19 18:41:10 +02001583 if (WARN_ON(!shadow_vmcs))
1584 return;
1585
Sean Christopherson55d23752018-12-03 13:53:18 -08001586 vmcs_load(shadow_vmcs);
1587
1588 for (q = 0; q < ARRAY_SIZE(fields); q++) {
1589 for (i = 0; i < max_fields[q]; i++) {
1590 field = fields[q][i];
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07001591 val = vmcs12_read_any(vmcs12, field.encoding,
1592 field.offset);
1593 __vmcs_writel(field.encoding, val);
Sean Christopherson55d23752018-12-03 13:53:18 -08001594 }
1595 }
1596
1597 vmcs_clear(shadow_vmcs);
1598 vmcs_load(vmx->loaded_vmcs->vmcs);
1599}
1600
1601static int copy_enlightened_to_vmcs12(struct vcpu_vmx *vmx)
1602{
1603 struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12;
1604 struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
1605
1606 /* HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE */
1607 vmcs12->tpr_threshold = evmcs->tpr_threshold;
1608 vmcs12->guest_rip = evmcs->guest_rip;
1609
1610 if (unlikely(!(evmcs->hv_clean_fields &
1611 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC))) {
1612 vmcs12->guest_rsp = evmcs->guest_rsp;
1613 vmcs12->guest_rflags = evmcs->guest_rflags;
1614 vmcs12->guest_interruptibility_info =
1615 evmcs->guest_interruptibility_info;
1616 }
1617
1618 if (unlikely(!(evmcs->hv_clean_fields &
1619 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC))) {
1620 vmcs12->cpu_based_vm_exec_control =
1621 evmcs->cpu_based_vm_exec_control;
1622 }
1623
1624 if (unlikely(!(evmcs->hv_clean_fields &
Vitaly Kuznetsovf9bc5222019-06-13 13:35:02 +02001625 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EXCPN))) {
Sean Christopherson55d23752018-12-03 13:53:18 -08001626 vmcs12->exception_bitmap = evmcs->exception_bitmap;
1627 }
1628
1629 if (unlikely(!(evmcs->hv_clean_fields &
1630 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_ENTRY))) {
1631 vmcs12->vm_entry_controls = evmcs->vm_entry_controls;
1632 }
1633
1634 if (unlikely(!(evmcs->hv_clean_fields &
1635 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT))) {
1636 vmcs12->vm_entry_intr_info_field =
1637 evmcs->vm_entry_intr_info_field;
1638 vmcs12->vm_entry_exception_error_code =
1639 evmcs->vm_entry_exception_error_code;
1640 vmcs12->vm_entry_instruction_len =
1641 evmcs->vm_entry_instruction_len;
1642 }
1643
1644 if (unlikely(!(evmcs->hv_clean_fields &
1645 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1))) {
1646 vmcs12->host_ia32_pat = evmcs->host_ia32_pat;
1647 vmcs12->host_ia32_efer = evmcs->host_ia32_efer;
1648 vmcs12->host_cr0 = evmcs->host_cr0;
1649 vmcs12->host_cr3 = evmcs->host_cr3;
1650 vmcs12->host_cr4 = evmcs->host_cr4;
1651 vmcs12->host_ia32_sysenter_esp = evmcs->host_ia32_sysenter_esp;
1652 vmcs12->host_ia32_sysenter_eip = evmcs->host_ia32_sysenter_eip;
1653 vmcs12->host_rip = evmcs->host_rip;
1654 vmcs12->host_ia32_sysenter_cs = evmcs->host_ia32_sysenter_cs;
1655 vmcs12->host_es_selector = evmcs->host_es_selector;
1656 vmcs12->host_cs_selector = evmcs->host_cs_selector;
1657 vmcs12->host_ss_selector = evmcs->host_ss_selector;
1658 vmcs12->host_ds_selector = evmcs->host_ds_selector;
1659 vmcs12->host_fs_selector = evmcs->host_fs_selector;
1660 vmcs12->host_gs_selector = evmcs->host_gs_selector;
1661 vmcs12->host_tr_selector = evmcs->host_tr_selector;
1662 }
1663
1664 if (unlikely(!(evmcs->hv_clean_fields &
Vitaly Kuznetsovf9bc5222019-06-13 13:35:02 +02001665 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1))) {
Sean Christopherson55d23752018-12-03 13:53:18 -08001666 vmcs12->pin_based_vm_exec_control =
1667 evmcs->pin_based_vm_exec_control;
1668 vmcs12->vm_exit_controls = evmcs->vm_exit_controls;
1669 vmcs12->secondary_vm_exec_control =
1670 evmcs->secondary_vm_exec_control;
1671 }
1672
1673 if (unlikely(!(evmcs->hv_clean_fields &
1674 HV_VMX_ENLIGHTENED_CLEAN_FIELD_IO_BITMAP))) {
1675 vmcs12->io_bitmap_a = evmcs->io_bitmap_a;
1676 vmcs12->io_bitmap_b = evmcs->io_bitmap_b;
1677 }
1678
1679 if (unlikely(!(evmcs->hv_clean_fields &
1680 HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP))) {
1681 vmcs12->msr_bitmap = evmcs->msr_bitmap;
1682 }
1683
1684 if (unlikely(!(evmcs->hv_clean_fields &
1685 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2))) {
1686 vmcs12->guest_es_base = evmcs->guest_es_base;
1687 vmcs12->guest_cs_base = evmcs->guest_cs_base;
1688 vmcs12->guest_ss_base = evmcs->guest_ss_base;
1689 vmcs12->guest_ds_base = evmcs->guest_ds_base;
1690 vmcs12->guest_fs_base = evmcs->guest_fs_base;
1691 vmcs12->guest_gs_base = evmcs->guest_gs_base;
1692 vmcs12->guest_ldtr_base = evmcs->guest_ldtr_base;
1693 vmcs12->guest_tr_base = evmcs->guest_tr_base;
1694 vmcs12->guest_gdtr_base = evmcs->guest_gdtr_base;
1695 vmcs12->guest_idtr_base = evmcs->guest_idtr_base;
1696 vmcs12->guest_es_limit = evmcs->guest_es_limit;
1697 vmcs12->guest_cs_limit = evmcs->guest_cs_limit;
1698 vmcs12->guest_ss_limit = evmcs->guest_ss_limit;
1699 vmcs12->guest_ds_limit = evmcs->guest_ds_limit;
1700 vmcs12->guest_fs_limit = evmcs->guest_fs_limit;
1701 vmcs12->guest_gs_limit = evmcs->guest_gs_limit;
1702 vmcs12->guest_ldtr_limit = evmcs->guest_ldtr_limit;
1703 vmcs12->guest_tr_limit = evmcs->guest_tr_limit;
1704 vmcs12->guest_gdtr_limit = evmcs->guest_gdtr_limit;
1705 vmcs12->guest_idtr_limit = evmcs->guest_idtr_limit;
1706 vmcs12->guest_es_ar_bytes = evmcs->guest_es_ar_bytes;
1707 vmcs12->guest_cs_ar_bytes = evmcs->guest_cs_ar_bytes;
1708 vmcs12->guest_ss_ar_bytes = evmcs->guest_ss_ar_bytes;
1709 vmcs12->guest_ds_ar_bytes = evmcs->guest_ds_ar_bytes;
1710 vmcs12->guest_fs_ar_bytes = evmcs->guest_fs_ar_bytes;
1711 vmcs12->guest_gs_ar_bytes = evmcs->guest_gs_ar_bytes;
1712 vmcs12->guest_ldtr_ar_bytes = evmcs->guest_ldtr_ar_bytes;
1713 vmcs12->guest_tr_ar_bytes = evmcs->guest_tr_ar_bytes;
1714 vmcs12->guest_es_selector = evmcs->guest_es_selector;
1715 vmcs12->guest_cs_selector = evmcs->guest_cs_selector;
1716 vmcs12->guest_ss_selector = evmcs->guest_ss_selector;
1717 vmcs12->guest_ds_selector = evmcs->guest_ds_selector;
1718 vmcs12->guest_fs_selector = evmcs->guest_fs_selector;
1719 vmcs12->guest_gs_selector = evmcs->guest_gs_selector;
1720 vmcs12->guest_ldtr_selector = evmcs->guest_ldtr_selector;
1721 vmcs12->guest_tr_selector = evmcs->guest_tr_selector;
1722 }
1723
1724 if (unlikely(!(evmcs->hv_clean_fields &
1725 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2))) {
1726 vmcs12->tsc_offset = evmcs->tsc_offset;
1727 vmcs12->virtual_apic_page_addr = evmcs->virtual_apic_page_addr;
1728 vmcs12->xss_exit_bitmap = evmcs->xss_exit_bitmap;
1729 }
1730
1731 if (unlikely(!(evmcs->hv_clean_fields &
1732 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR))) {
1733 vmcs12->cr0_guest_host_mask = evmcs->cr0_guest_host_mask;
1734 vmcs12->cr4_guest_host_mask = evmcs->cr4_guest_host_mask;
1735 vmcs12->cr0_read_shadow = evmcs->cr0_read_shadow;
1736 vmcs12->cr4_read_shadow = evmcs->cr4_read_shadow;
1737 vmcs12->guest_cr0 = evmcs->guest_cr0;
1738 vmcs12->guest_cr3 = evmcs->guest_cr3;
1739 vmcs12->guest_cr4 = evmcs->guest_cr4;
1740 vmcs12->guest_dr7 = evmcs->guest_dr7;
1741 }
1742
1743 if (unlikely(!(evmcs->hv_clean_fields &
1744 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER))) {
1745 vmcs12->host_fs_base = evmcs->host_fs_base;
1746 vmcs12->host_gs_base = evmcs->host_gs_base;
1747 vmcs12->host_tr_base = evmcs->host_tr_base;
1748 vmcs12->host_gdtr_base = evmcs->host_gdtr_base;
1749 vmcs12->host_idtr_base = evmcs->host_idtr_base;
1750 vmcs12->host_rsp = evmcs->host_rsp;
1751 }
1752
1753 if (unlikely(!(evmcs->hv_clean_fields &
1754 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_XLAT))) {
1755 vmcs12->ept_pointer = evmcs->ept_pointer;
1756 vmcs12->virtual_processor_id = evmcs->virtual_processor_id;
1757 }
1758
1759 if (unlikely(!(evmcs->hv_clean_fields &
1760 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1))) {
1761 vmcs12->vmcs_link_pointer = evmcs->vmcs_link_pointer;
1762 vmcs12->guest_ia32_debugctl = evmcs->guest_ia32_debugctl;
1763 vmcs12->guest_ia32_pat = evmcs->guest_ia32_pat;
1764 vmcs12->guest_ia32_efer = evmcs->guest_ia32_efer;
1765 vmcs12->guest_pdptr0 = evmcs->guest_pdptr0;
1766 vmcs12->guest_pdptr1 = evmcs->guest_pdptr1;
1767 vmcs12->guest_pdptr2 = evmcs->guest_pdptr2;
1768 vmcs12->guest_pdptr3 = evmcs->guest_pdptr3;
1769 vmcs12->guest_pending_dbg_exceptions =
1770 evmcs->guest_pending_dbg_exceptions;
1771 vmcs12->guest_sysenter_esp = evmcs->guest_sysenter_esp;
1772 vmcs12->guest_sysenter_eip = evmcs->guest_sysenter_eip;
1773 vmcs12->guest_bndcfgs = evmcs->guest_bndcfgs;
1774 vmcs12->guest_activity_state = evmcs->guest_activity_state;
1775 vmcs12->guest_sysenter_cs = evmcs->guest_sysenter_cs;
1776 }
1777
1778 /*
1779 * Not used?
1780 * vmcs12->vm_exit_msr_store_addr = evmcs->vm_exit_msr_store_addr;
1781 * vmcs12->vm_exit_msr_load_addr = evmcs->vm_exit_msr_load_addr;
1782 * vmcs12->vm_entry_msr_load_addr = evmcs->vm_entry_msr_load_addr;
Sean Christopherson55d23752018-12-03 13:53:18 -08001783 * vmcs12->page_fault_error_code_mask =
1784 * evmcs->page_fault_error_code_mask;
1785 * vmcs12->page_fault_error_code_match =
1786 * evmcs->page_fault_error_code_match;
1787 * vmcs12->cr3_target_count = evmcs->cr3_target_count;
1788 * vmcs12->vm_exit_msr_store_count = evmcs->vm_exit_msr_store_count;
1789 * vmcs12->vm_exit_msr_load_count = evmcs->vm_exit_msr_load_count;
1790 * vmcs12->vm_entry_msr_load_count = evmcs->vm_entry_msr_load_count;
1791 */
1792
1793 /*
1794 * Read only fields:
1795 * vmcs12->guest_physical_address = evmcs->guest_physical_address;
1796 * vmcs12->vm_instruction_error = evmcs->vm_instruction_error;
1797 * vmcs12->vm_exit_reason = evmcs->vm_exit_reason;
1798 * vmcs12->vm_exit_intr_info = evmcs->vm_exit_intr_info;
1799 * vmcs12->vm_exit_intr_error_code = evmcs->vm_exit_intr_error_code;
1800 * vmcs12->idt_vectoring_info_field = evmcs->idt_vectoring_info_field;
1801 * vmcs12->idt_vectoring_error_code = evmcs->idt_vectoring_error_code;
1802 * vmcs12->vm_exit_instruction_len = evmcs->vm_exit_instruction_len;
1803 * vmcs12->vmx_instruction_info = evmcs->vmx_instruction_info;
1804 * vmcs12->exit_qualification = evmcs->exit_qualification;
1805 * vmcs12->guest_linear_address = evmcs->guest_linear_address;
1806 *
1807 * Not present in struct vmcs12:
1808 * vmcs12->exit_io_instruction_ecx = evmcs->exit_io_instruction_ecx;
1809 * vmcs12->exit_io_instruction_esi = evmcs->exit_io_instruction_esi;
1810 * vmcs12->exit_io_instruction_edi = evmcs->exit_io_instruction_edi;
1811 * vmcs12->exit_io_instruction_eip = evmcs->exit_io_instruction_eip;
1812 */
1813
1814 return 0;
1815}
1816
1817static int copy_vmcs12_to_enlightened(struct vcpu_vmx *vmx)
1818{
1819 struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12;
1820 struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
1821
1822 /*
1823 * Should not be changed by KVM:
1824 *
1825 * evmcs->host_es_selector = vmcs12->host_es_selector;
1826 * evmcs->host_cs_selector = vmcs12->host_cs_selector;
1827 * evmcs->host_ss_selector = vmcs12->host_ss_selector;
1828 * evmcs->host_ds_selector = vmcs12->host_ds_selector;
1829 * evmcs->host_fs_selector = vmcs12->host_fs_selector;
1830 * evmcs->host_gs_selector = vmcs12->host_gs_selector;
1831 * evmcs->host_tr_selector = vmcs12->host_tr_selector;
1832 * evmcs->host_ia32_pat = vmcs12->host_ia32_pat;
1833 * evmcs->host_ia32_efer = vmcs12->host_ia32_efer;
1834 * evmcs->host_cr0 = vmcs12->host_cr0;
1835 * evmcs->host_cr3 = vmcs12->host_cr3;
1836 * evmcs->host_cr4 = vmcs12->host_cr4;
1837 * evmcs->host_ia32_sysenter_esp = vmcs12->host_ia32_sysenter_esp;
1838 * evmcs->host_ia32_sysenter_eip = vmcs12->host_ia32_sysenter_eip;
1839 * evmcs->host_rip = vmcs12->host_rip;
1840 * evmcs->host_ia32_sysenter_cs = vmcs12->host_ia32_sysenter_cs;
1841 * evmcs->host_fs_base = vmcs12->host_fs_base;
1842 * evmcs->host_gs_base = vmcs12->host_gs_base;
1843 * evmcs->host_tr_base = vmcs12->host_tr_base;
1844 * evmcs->host_gdtr_base = vmcs12->host_gdtr_base;
1845 * evmcs->host_idtr_base = vmcs12->host_idtr_base;
1846 * evmcs->host_rsp = vmcs12->host_rsp;
Sean Christopherson3731905ef2019-05-07 08:36:27 -07001847 * sync_vmcs02_to_vmcs12() doesn't read these:
Sean Christopherson55d23752018-12-03 13:53:18 -08001848 * evmcs->io_bitmap_a = vmcs12->io_bitmap_a;
1849 * evmcs->io_bitmap_b = vmcs12->io_bitmap_b;
1850 * evmcs->msr_bitmap = vmcs12->msr_bitmap;
1851 * evmcs->ept_pointer = vmcs12->ept_pointer;
1852 * evmcs->xss_exit_bitmap = vmcs12->xss_exit_bitmap;
1853 * evmcs->vm_exit_msr_store_addr = vmcs12->vm_exit_msr_store_addr;
1854 * evmcs->vm_exit_msr_load_addr = vmcs12->vm_exit_msr_load_addr;
1855 * evmcs->vm_entry_msr_load_addr = vmcs12->vm_entry_msr_load_addr;
Sean Christopherson55d23752018-12-03 13:53:18 -08001856 * evmcs->tpr_threshold = vmcs12->tpr_threshold;
1857 * evmcs->virtual_processor_id = vmcs12->virtual_processor_id;
1858 * evmcs->exception_bitmap = vmcs12->exception_bitmap;
1859 * evmcs->vmcs_link_pointer = vmcs12->vmcs_link_pointer;
1860 * evmcs->pin_based_vm_exec_control = vmcs12->pin_based_vm_exec_control;
1861 * evmcs->vm_exit_controls = vmcs12->vm_exit_controls;
1862 * evmcs->secondary_vm_exec_control = vmcs12->secondary_vm_exec_control;
1863 * evmcs->page_fault_error_code_mask =
1864 * vmcs12->page_fault_error_code_mask;
1865 * evmcs->page_fault_error_code_match =
1866 * vmcs12->page_fault_error_code_match;
1867 * evmcs->cr3_target_count = vmcs12->cr3_target_count;
1868 * evmcs->virtual_apic_page_addr = vmcs12->virtual_apic_page_addr;
1869 * evmcs->tsc_offset = vmcs12->tsc_offset;
1870 * evmcs->guest_ia32_debugctl = vmcs12->guest_ia32_debugctl;
1871 * evmcs->cr0_guest_host_mask = vmcs12->cr0_guest_host_mask;
1872 * evmcs->cr4_guest_host_mask = vmcs12->cr4_guest_host_mask;
1873 * evmcs->cr0_read_shadow = vmcs12->cr0_read_shadow;
1874 * evmcs->cr4_read_shadow = vmcs12->cr4_read_shadow;
1875 * evmcs->vm_exit_msr_store_count = vmcs12->vm_exit_msr_store_count;
1876 * evmcs->vm_exit_msr_load_count = vmcs12->vm_exit_msr_load_count;
1877 * evmcs->vm_entry_msr_load_count = vmcs12->vm_entry_msr_load_count;
1878 *
1879 * Not present in struct vmcs12:
1880 * evmcs->exit_io_instruction_ecx = vmcs12->exit_io_instruction_ecx;
1881 * evmcs->exit_io_instruction_esi = vmcs12->exit_io_instruction_esi;
1882 * evmcs->exit_io_instruction_edi = vmcs12->exit_io_instruction_edi;
1883 * evmcs->exit_io_instruction_eip = vmcs12->exit_io_instruction_eip;
1884 */
1885
1886 evmcs->guest_es_selector = vmcs12->guest_es_selector;
1887 evmcs->guest_cs_selector = vmcs12->guest_cs_selector;
1888 evmcs->guest_ss_selector = vmcs12->guest_ss_selector;
1889 evmcs->guest_ds_selector = vmcs12->guest_ds_selector;
1890 evmcs->guest_fs_selector = vmcs12->guest_fs_selector;
1891 evmcs->guest_gs_selector = vmcs12->guest_gs_selector;
1892 evmcs->guest_ldtr_selector = vmcs12->guest_ldtr_selector;
1893 evmcs->guest_tr_selector = vmcs12->guest_tr_selector;
1894
1895 evmcs->guest_es_limit = vmcs12->guest_es_limit;
1896 evmcs->guest_cs_limit = vmcs12->guest_cs_limit;
1897 evmcs->guest_ss_limit = vmcs12->guest_ss_limit;
1898 evmcs->guest_ds_limit = vmcs12->guest_ds_limit;
1899 evmcs->guest_fs_limit = vmcs12->guest_fs_limit;
1900 evmcs->guest_gs_limit = vmcs12->guest_gs_limit;
1901 evmcs->guest_ldtr_limit = vmcs12->guest_ldtr_limit;
1902 evmcs->guest_tr_limit = vmcs12->guest_tr_limit;
1903 evmcs->guest_gdtr_limit = vmcs12->guest_gdtr_limit;
1904 evmcs->guest_idtr_limit = vmcs12->guest_idtr_limit;
1905
1906 evmcs->guest_es_ar_bytes = vmcs12->guest_es_ar_bytes;
1907 evmcs->guest_cs_ar_bytes = vmcs12->guest_cs_ar_bytes;
1908 evmcs->guest_ss_ar_bytes = vmcs12->guest_ss_ar_bytes;
1909 evmcs->guest_ds_ar_bytes = vmcs12->guest_ds_ar_bytes;
1910 evmcs->guest_fs_ar_bytes = vmcs12->guest_fs_ar_bytes;
1911 evmcs->guest_gs_ar_bytes = vmcs12->guest_gs_ar_bytes;
1912 evmcs->guest_ldtr_ar_bytes = vmcs12->guest_ldtr_ar_bytes;
1913 evmcs->guest_tr_ar_bytes = vmcs12->guest_tr_ar_bytes;
1914
1915 evmcs->guest_es_base = vmcs12->guest_es_base;
1916 evmcs->guest_cs_base = vmcs12->guest_cs_base;
1917 evmcs->guest_ss_base = vmcs12->guest_ss_base;
1918 evmcs->guest_ds_base = vmcs12->guest_ds_base;
1919 evmcs->guest_fs_base = vmcs12->guest_fs_base;
1920 evmcs->guest_gs_base = vmcs12->guest_gs_base;
1921 evmcs->guest_ldtr_base = vmcs12->guest_ldtr_base;
1922 evmcs->guest_tr_base = vmcs12->guest_tr_base;
1923 evmcs->guest_gdtr_base = vmcs12->guest_gdtr_base;
1924 evmcs->guest_idtr_base = vmcs12->guest_idtr_base;
1925
1926 evmcs->guest_ia32_pat = vmcs12->guest_ia32_pat;
1927 evmcs->guest_ia32_efer = vmcs12->guest_ia32_efer;
1928
1929 evmcs->guest_pdptr0 = vmcs12->guest_pdptr0;
1930 evmcs->guest_pdptr1 = vmcs12->guest_pdptr1;
1931 evmcs->guest_pdptr2 = vmcs12->guest_pdptr2;
1932 evmcs->guest_pdptr3 = vmcs12->guest_pdptr3;
1933
1934 evmcs->guest_pending_dbg_exceptions =
1935 vmcs12->guest_pending_dbg_exceptions;
1936 evmcs->guest_sysenter_esp = vmcs12->guest_sysenter_esp;
1937 evmcs->guest_sysenter_eip = vmcs12->guest_sysenter_eip;
1938
1939 evmcs->guest_activity_state = vmcs12->guest_activity_state;
1940 evmcs->guest_sysenter_cs = vmcs12->guest_sysenter_cs;
1941
1942 evmcs->guest_cr0 = vmcs12->guest_cr0;
1943 evmcs->guest_cr3 = vmcs12->guest_cr3;
1944 evmcs->guest_cr4 = vmcs12->guest_cr4;
1945 evmcs->guest_dr7 = vmcs12->guest_dr7;
1946
1947 evmcs->guest_physical_address = vmcs12->guest_physical_address;
1948
1949 evmcs->vm_instruction_error = vmcs12->vm_instruction_error;
1950 evmcs->vm_exit_reason = vmcs12->vm_exit_reason;
1951 evmcs->vm_exit_intr_info = vmcs12->vm_exit_intr_info;
1952 evmcs->vm_exit_intr_error_code = vmcs12->vm_exit_intr_error_code;
1953 evmcs->idt_vectoring_info_field = vmcs12->idt_vectoring_info_field;
1954 evmcs->idt_vectoring_error_code = vmcs12->idt_vectoring_error_code;
1955 evmcs->vm_exit_instruction_len = vmcs12->vm_exit_instruction_len;
1956 evmcs->vmx_instruction_info = vmcs12->vmx_instruction_info;
1957
1958 evmcs->exit_qualification = vmcs12->exit_qualification;
1959
1960 evmcs->guest_linear_address = vmcs12->guest_linear_address;
1961 evmcs->guest_rsp = vmcs12->guest_rsp;
1962 evmcs->guest_rflags = vmcs12->guest_rflags;
1963
1964 evmcs->guest_interruptibility_info =
1965 vmcs12->guest_interruptibility_info;
1966 evmcs->cpu_based_vm_exec_control = vmcs12->cpu_based_vm_exec_control;
1967 evmcs->vm_entry_controls = vmcs12->vm_entry_controls;
1968 evmcs->vm_entry_intr_info_field = vmcs12->vm_entry_intr_info_field;
1969 evmcs->vm_entry_exception_error_code =
1970 vmcs12->vm_entry_exception_error_code;
1971 evmcs->vm_entry_instruction_len = vmcs12->vm_entry_instruction_len;
1972
1973 evmcs->guest_rip = vmcs12->guest_rip;
1974
1975 evmcs->guest_bndcfgs = vmcs12->guest_bndcfgs;
1976
1977 return 0;
1978}
1979
1980/*
1981 * This is an equivalent of the nested hypervisor executing the vmptrld
1982 * instruction.
1983 */
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01001984static enum nested_evmptrld_status nested_vmx_handle_enlightened_vmptrld(
1985 struct kvm_vcpu *vcpu, bool from_launch)
Sean Christopherson55d23752018-12-03 13:53:18 -08001986{
1987 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsova21a39c2019-06-28 13:23:32 +02001988 bool evmcs_gpa_changed = false;
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02001989 u64 evmcs_gpa;
Sean Christopherson55d23752018-12-03 13:53:18 -08001990
1991 if (likely(!vmx->nested.enlightened_vmcs_enabled))
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01001992 return EVMPTRLD_DISABLED;
Sean Christopherson55d23752018-12-03 13:53:18 -08001993
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02001994 if (!nested_enlightened_vmentry(vcpu, &evmcs_gpa))
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01001995 return EVMPTRLD_DISABLED;
Sean Christopherson55d23752018-12-03 13:53:18 -08001996
Vitaly Kuznetsov95fa1012020-03-09 16:52:11 +01001997 if (unlikely(!vmx->nested.hv_evmcs ||
1998 evmcs_gpa != vmx->nested.hv_evmcs_vmptr)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08001999 if (!vmx->nested.hv_evmcs)
2000 vmx->nested.current_vmptr = -1ull;
2001
2002 nested_release_evmcs(vcpu);
2003
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02002004 if (kvm_vcpu_map(vcpu, gpa_to_gfn(evmcs_gpa),
KarimAllah Ahmeddee9c042019-01-31 21:24:42 +01002005 &vmx->nested.hv_evmcs_map))
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01002006 return EVMPTRLD_ERROR;
Sean Christopherson55d23752018-12-03 13:53:18 -08002007
KarimAllah Ahmeddee9c042019-01-31 21:24:42 +01002008 vmx->nested.hv_evmcs = vmx->nested.hv_evmcs_map.hva;
Sean Christopherson55d23752018-12-03 13:53:18 -08002009
2010 /*
2011 * Currently, KVM only supports eVMCS version 1
2012 * (== KVM_EVMCS_VERSION) and thus we expect guest to set this
2013 * value to first u32 field of eVMCS which should specify eVMCS
2014 * VersionNumber.
2015 *
2016 * Guest should be aware of supported eVMCS versions by host by
2017 * examining CPUID.0x4000000A.EAX[0:15]. Host userspace VMM is
2018 * expected to set this CPUID leaf according to the value
2019 * returned in vmcs_version from nested_enable_evmcs().
2020 *
2021 * However, it turns out that Microsoft Hyper-V fails to comply
2022 * to their own invented interface: When Hyper-V use eVMCS, it
2023 * just sets first u32 field of eVMCS to revision_id specified
2024 * in MSR_IA32_VMX_BASIC. Instead of used eVMCS version number
2025 * which is one of the supported versions specified in
2026 * CPUID.0x4000000A.EAX[0:15].
2027 *
2028 * To overcome Hyper-V bug, we accept here either a supported
2029 * eVMCS version or VMCS12 revision_id as valid values for first
2030 * u32 field of eVMCS.
2031 */
2032 if ((vmx->nested.hv_evmcs->revision_id != KVM_EVMCS_VERSION) &&
2033 (vmx->nested.hv_evmcs->revision_id != VMCS12_REVISION)) {
2034 nested_release_evmcs(vcpu);
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01002035 return EVMPTRLD_VMFAIL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002036 }
2037
2038 vmx->nested.dirty_vmcs12 = true;
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02002039 vmx->nested.hv_evmcs_vmptr = evmcs_gpa;
Sean Christopherson55d23752018-12-03 13:53:18 -08002040
Vitaly Kuznetsova21a39c2019-06-28 13:23:32 +02002041 evmcs_gpa_changed = true;
Sean Christopherson55d23752018-12-03 13:53:18 -08002042 /*
2043 * Unlike normal vmcs12, enlightened vmcs12 is not fully
2044 * reloaded from guest's memory (read only fields, fields not
2045 * present in struct hv_enlightened_vmcs, ...). Make sure there
2046 * are no leftovers.
2047 */
2048 if (from_launch) {
2049 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2050 memset(vmcs12, 0, sizeof(*vmcs12));
2051 vmcs12->hdr.revision_id = VMCS12_REVISION;
2052 }
2053
2054 }
Vitaly Kuznetsova21a39c2019-06-28 13:23:32 +02002055
2056 /*
Miaohe Linffdbd502020-02-07 23:22:45 +08002057 * Clean fields data can't be used on VMLAUNCH and when we switch
Vitaly Kuznetsova21a39c2019-06-28 13:23:32 +02002058 * between different L2 guests as KVM keeps a single VMCS12 per L1.
2059 */
2060 if (from_launch || evmcs_gpa_changed)
2061 vmx->nested.hv_evmcs->hv_clean_fields &=
2062 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
2063
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01002064 return EVMPTRLD_SUCCEEDED;
Sean Christopherson55d23752018-12-03 13:53:18 -08002065}
2066
Sean Christopherson3731905ef2019-05-07 08:36:27 -07002067void nested_sync_vmcs12_to_shadow(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08002068{
2069 struct vcpu_vmx *vmx = to_vmx(vcpu);
2070
Sean Christopherson55d23752018-12-03 13:53:18 -08002071 if (vmx->nested.hv_evmcs) {
2072 copy_vmcs12_to_enlightened(vmx);
2073 /* All fields are clean */
2074 vmx->nested.hv_evmcs->hv_clean_fields |=
2075 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
2076 } else {
2077 copy_vmcs12_to_shadow(vmx);
2078 }
2079
Sean Christopherson3731905ef2019-05-07 08:36:27 -07002080 vmx->nested.need_vmcs12_to_shadow_sync = false;
Sean Christopherson55d23752018-12-03 13:53:18 -08002081}
2082
2083static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
2084{
2085 struct vcpu_vmx *vmx =
2086 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
2087
2088 vmx->nested.preemption_timer_expired = true;
2089 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
2090 kvm_vcpu_kick(&vmx->vcpu);
2091
2092 return HRTIMER_NORESTART;
2093}
2094
Peter Shier850448f2020-05-26 14:51:06 -07002095static u64 vmx_calc_preemption_timer_value(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08002096{
Peter Shier850448f2020-05-26 14:51:06 -07002097 struct vcpu_vmx *vmx = to_vmx(vcpu);
2098 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Peter Shier850448f2020-05-26 14:51:06 -07002099
2100 u64 l1_scaled_tsc = kvm_read_l1_tsc(vcpu, rdtsc()) >>
2101 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
2102
2103 if (!vmx->nested.has_preemption_timer_deadline) {
Makarand Sonare8d7fbf02020-05-26 14:51:07 -07002104 vmx->nested.preemption_timer_deadline =
2105 vmcs12->vmx_preemption_timer_value + l1_scaled_tsc;
Peter Shier850448f2020-05-26 14:51:06 -07002106 vmx->nested.has_preemption_timer_deadline = true;
Makarand Sonare8d7fbf02020-05-26 14:51:07 -07002107 }
2108 return vmx->nested.preemption_timer_deadline - l1_scaled_tsc;
Peter Shier850448f2020-05-26 14:51:06 -07002109}
2110
2111static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu,
2112 u64 preemption_timeout)
2113{
Sean Christopherson55d23752018-12-03 13:53:18 -08002114 struct vcpu_vmx *vmx = to_vmx(vcpu);
2115
2116 /*
2117 * A timer value of zero is architecturally guaranteed to cause
2118 * a VMExit prior to executing any instructions in the guest.
2119 */
2120 if (preemption_timeout == 0) {
2121 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
2122 return;
2123 }
2124
2125 if (vcpu->arch.virtual_tsc_khz == 0)
2126 return;
2127
2128 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
2129 preemption_timeout *= 1000000;
2130 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
2131 hrtimer_start(&vmx->nested.preemption_timer,
Jim Mattsonada00982020-05-08 13:36:42 -07002132 ktime_add_ns(ktime_get(), preemption_timeout),
2133 HRTIMER_MODE_ABS_PINNED);
Sean Christopherson55d23752018-12-03 13:53:18 -08002134}
2135
2136static u64 nested_vmx_calc_efer(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
2137{
2138 if (vmx->nested.nested_run_pending &&
2139 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
2140 return vmcs12->guest_ia32_efer;
2141 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
2142 return vmx->vcpu.arch.efer | (EFER_LMA | EFER_LME);
2143 else
2144 return vmx->vcpu.arch.efer & ~(EFER_LMA | EFER_LME);
2145}
2146
2147static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx)
2148{
2149 /*
2150 * If vmcs02 hasn't been initialized, set the constant vmcs02 state
2151 * according to L0's settings (vmcs12 is irrelevant here). Host
2152 * fields that come from L0 and are not constant, e.g. HOST_CR3,
2153 * will be set as needed prior to VMLAUNCH/VMRESUME.
2154 */
2155 if (vmx->nested.vmcs02_initialized)
2156 return;
2157 vmx->nested.vmcs02_initialized = true;
2158
2159 /*
2160 * We don't care what the EPTP value is we just need to guarantee
2161 * it's valid so we don't get a false positive when doing early
2162 * consistency checks.
2163 */
2164 if (enable_ept && nested_early_check)
Sean Christopherson2a40b902020-07-15 20:41:18 -07002165 vmcs_write64(EPT_POINTER,
2166 construct_eptp(&vmx->vcpu, 0, PT64_ROOT_4LEVEL));
Sean Christopherson55d23752018-12-03 13:53:18 -08002167
2168 /* All VMFUNCs are currently emulated through L0 vmexits. */
2169 if (cpu_has_vmx_vmfunc())
2170 vmcs_write64(VM_FUNCTION_CONTROL, 0);
2171
2172 if (cpu_has_vmx_posted_intr())
2173 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
2174
2175 if (cpu_has_vmx_msr_bitmap())
2176 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
2177
Sean Christopherson4d6c9892019-05-07 09:06:30 -07002178 /*
2179 * The PML address never changes, so it is constant in vmcs02.
2180 * Conceptually we want to copy the PML index from vmcs01 here,
2181 * and then back to vmcs01 on nested vmexit. But since we flush
2182 * the log and reset GUEST_PML_INDEX on each vmexit, the PML
2183 * index is also effectively constant in vmcs02.
2184 */
2185 if (enable_pml) {
Sean Christopherson55d23752018-12-03 13:53:18 -08002186 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
Sean Christopherson4d6c9892019-05-07 09:06:30 -07002187 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
2188 }
Sean Christopherson55d23752018-12-03 13:53:18 -08002189
Sean Christophersonc538d572019-05-07 09:06:29 -07002190 if (cpu_has_vmx_encls_vmexit())
2191 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
Sean Christopherson55d23752018-12-03 13:53:18 -08002192
2193 /*
2194 * Set the MSR load/store lists to match L0's settings. Only the
2195 * addresses are constant (for vmcs02), the counts can change based
2196 * on L2's behavior, e.g. switching to/from long mode.
2197 */
Aaron Lewis662f1d12019-11-07 21:14:39 -08002198 vmcs_write64(VM_EXIT_MSR_STORE_ADDR, __pa(vmx->msr_autostore.guest.val));
Sean Christopherson55d23752018-12-03 13:53:18 -08002199 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
2200 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
2201
2202 vmx_set_constant_host_state(vmx);
2203}
2204
Paolo Bonzinib1346ab2019-06-06 17:24:00 +02002205static void prepare_vmcs02_early_rare(struct vcpu_vmx *vmx,
Sean Christopherson55d23752018-12-03 13:53:18 -08002206 struct vmcs12 *vmcs12)
2207{
2208 prepare_vmcs02_constant_state(vmx);
2209
2210 vmcs_write64(VMCS_LINK_POINTER, -1ull);
2211
2212 if (enable_vpid) {
2213 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
2214 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
2215 else
2216 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
2217 }
2218}
2219
2220static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
2221{
2222 u32 exec_control, vmcs12_exec_ctrl;
2223 u64 guest_efer = nested_vmx_calc_efer(vmx, vmcs12);
2224
2225 if (vmx->nested.dirty_vmcs12 || vmx->nested.hv_evmcs)
Paolo Bonzinib1346ab2019-06-06 17:24:00 +02002226 prepare_vmcs02_early_rare(vmx, vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08002227
2228 /*
Sean Christopherson55d23752018-12-03 13:53:18 -08002229 * PIN CONTROLS
2230 */
Sean Christophersonc075c3e2019-05-07 12:17:53 -07002231 exec_control = vmx_pin_based_exec_ctrl(vmx);
Sean Christopherson804939e2019-05-07 12:18:05 -07002232 exec_control |= (vmcs12->pin_based_vm_exec_control &
2233 ~PIN_BASED_VMX_PREEMPTION_TIMER);
Sean Christopherson55d23752018-12-03 13:53:18 -08002234
2235 /* Posted interrupts setting is only taken from vmcs12. */
2236 if (nested_cpu_has_posted_intr(vmcs12)) {
2237 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
2238 vmx->nested.pi_pending = false;
2239 } else {
2240 exec_control &= ~PIN_BASED_POSTED_INTR;
2241 }
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002242 pin_controls_set(vmx, exec_control);
Sean Christopherson55d23752018-12-03 13:53:18 -08002243
2244 /*
2245 * EXEC CONTROLS
2246 */
2247 exec_control = vmx_exec_control(vmx); /* L0's desires */
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08002248 exec_control &= ~CPU_BASED_INTR_WINDOW_EXITING;
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08002249 exec_control &= ~CPU_BASED_NMI_WINDOW_EXITING;
Sean Christopherson55d23752018-12-03 13:53:18 -08002250 exec_control &= ~CPU_BASED_TPR_SHADOW;
2251 exec_control |= vmcs12->cpu_based_vm_exec_control;
2252
Liran Alon02d496cf2019-11-11 14:30:55 +02002253 vmx->nested.l1_tpr_threshold = -1;
Sean Christophersonca2f5462019-05-07 09:06:33 -07002254 if (exec_control & CPU_BASED_TPR_SHADOW)
Sean Christopherson55d23752018-12-03 13:53:18 -08002255 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Sean Christopherson55d23752018-12-03 13:53:18 -08002256#ifdef CONFIG_X86_64
Sean Christophersonca2f5462019-05-07 09:06:33 -07002257 else
Sean Christopherson55d23752018-12-03 13:53:18 -08002258 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
2259 CPU_BASED_CR8_STORE_EXITING;
2260#endif
Sean Christopherson55d23752018-12-03 13:53:18 -08002261
2262 /*
2263 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
2264 * for I/O port accesses.
2265 */
Sean Christopherson55d23752018-12-03 13:53:18 -08002266 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
Sean Christophersonde0286b2019-05-07 12:18:01 -07002267 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
2268
2269 /*
2270 * This bit will be computed in nested_get_vmcs12_pages, because
2271 * we do not have access to L1's MSR bitmap yet. For now, keep
2272 * the same bit as before, hoping to avoid multiple VMWRITEs that
2273 * only set/clear this bit.
2274 */
2275 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
2276 exec_control |= exec_controls_get(vmx) & CPU_BASED_USE_MSR_BITMAPS;
2277
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002278 exec_controls_set(vmx, exec_control);
Sean Christopherson55d23752018-12-03 13:53:18 -08002279
2280 /*
2281 * SECONDARY EXEC CONTROLS
2282 */
2283 if (cpu_has_secondary_exec_ctrls()) {
2284 exec_control = vmx->secondary_exec_control;
2285
2286 /* Take the following fields only from vmcs12 */
2287 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
2288 SECONDARY_EXEC_ENABLE_INVPCID |
Sean Christopherson7f3603b2020-09-23 09:50:47 -07002289 SECONDARY_EXEC_ENABLE_RDTSCP |
Sean Christopherson55d23752018-12-03 13:53:18 -08002290 SECONDARY_EXEC_XSAVES |
Tao Xue69e72fa2019-07-16 14:55:49 +08002291 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE |
Sean Christopherson55d23752018-12-03 13:53:18 -08002292 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
2293 SECONDARY_EXEC_APIC_REGISTER_VIRT |
2294 SECONDARY_EXEC_ENABLE_VMFUNC);
2295 if (nested_cpu_has(vmcs12,
2296 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
2297 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
2298 ~SECONDARY_EXEC_ENABLE_PML;
2299 exec_control |= vmcs12_exec_ctrl;
2300 }
2301
2302 /* VMCS shadowing for L2 is emulated for now */
2303 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
2304
Sean Christopherson469debd2019-05-07 12:18:02 -07002305 /*
2306 * Preset *DT exiting when emulating UMIP, so that vmx_set_cr4()
2307 * will not have to rewrite the controls just for this bit.
2308 */
2309 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated() &&
2310 (vmcs12->guest_cr4 & X86_CR4_UMIP))
2311 exec_control |= SECONDARY_EXEC_DESC;
2312
Sean Christopherson55d23752018-12-03 13:53:18 -08002313 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
2314 vmcs_write16(GUEST_INTR_STATUS,
2315 vmcs12->guest_intr_status);
2316
Krish Sadhukhanbddd82d2020-09-21 08:10:25 +00002317 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
2318 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
2319
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002320 secondary_exec_controls_set(vmx, exec_control);
Sean Christopherson55d23752018-12-03 13:53:18 -08002321 }
2322
2323 /*
2324 * ENTRY CONTROLS
2325 *
2326 * vmcs12's VM_{ENTRY,EXIT}_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE
2327 * are emulated by vmx_set_efer() in prepare_vmcs02(), but speculate
2328 * on the related bits (if supported by the CPU) in the hope that
2329 * we can avoid VMWrites during vmx_set_efer().
2330 */
2331 exec_control = (vmcs12->vm_entry_controls | vmx_vmentry_ctrl()) &
2332 ~VM_ENTRY_IA32E_MODE & ~VM_ENTRY_LOAD_IA32_EFER;
2333 if (cpu_has_load_ia32_efer()) {
2334 if (guest_efer & EFER_LMA)
2335 exec_control |= VM_ENTRY_IA32E_MODE;
2336 if (guest_efer != host_efer)
2337 exec_control |= VM_ENTRY_LOAD_IA32_EFER;
2338 }
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002339 vm_entry_controls_set(vmx, exec_control);
Sean Christopherson55d23752018-12-03 13:53:18 -08002340
2341 /*
2342 * EXIT CONTROLS
2343 *
2344 * L2->L1 exit controls are emulated - the hardware exit is to L0 so
2345 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
2346 * bits may be modified by vmx_set_efer() in prepare_vmcs02().
2347 */
2348 exec_control = vmx_vmexit_ctrl();
2349 if (cpu_has_load_ia32_efer() && guest_efer != host_efer)
2350 exec_control |= VM_EXIT_LOAD_IA32_EFER;
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002351 vm_exit_controls_set(vmx, exec_control);
Sean Christopherson55d23752018-12-03 13:53:18 -08002352
2353 /*
2354 * Interrupt/Exception Fields
2355 */
2356 if (vmx->nested.nested_run_pending) {
2357 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2358 vmcs12->vm_entry_intr_info_field);
2359 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
2360 vmcs12->vm_entry_exception_error_code);
2361 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2362 vmcs12->vm_entry_instruction_len);
2363 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
2364 vmcs12->guest_interruptibility_info);
2365 vmx->loaded_vmcs->nmi_known_unmasked =
2366 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
2367 } else {
2368 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
2369 }
2370}
2371
Paolo Bonzinib1346ab2019-06-06 17:24:00 +02002372static void prepare_vmcs02_rare(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
Sean Christopherson55d23752018-12-03 13:53:18 -08002373{
2374 struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs;
2375
2376 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
2377 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2)) {
2378 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
2379 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
2380 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
2381 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
2382 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
2383 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
2384 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
2385 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
2386 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
2387 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
2388 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
2389 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
2390 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
2391 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
2392 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
2393 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
2394 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
2395 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07002396 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
2397 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
Sean Christopherson55d23752018-12-03 13:53:18 -08002398 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
2399 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
2400 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
2401 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
2402 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
2403 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
2404 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
2405 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
2406 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
2407 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
2408 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
2409 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
2410 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
2411 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
2412 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
2413 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Sean Christophersonfc387d82020-09-23 11:44:46 -07002414
2415 vmx->segment_cache.bitmask = 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08002416 }
2417
2418 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
2419 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1)) {
2420 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
2421 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
2422 vmcs12->guest_pending_dbg_exceptions);
2423 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
2424 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
2425
2426 /*
2427 * L1 may access the L2's PDPTR, so save them to construct
2428 * vmcs12
2429 */
2430 if (enable_ept) {
2431 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
2432 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
2433 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
2434 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
2435 }
Sean Christophersonc27e5b02019-05-07 09:06:39 -07002436
2437 if (kvm_mpx_supported() && vmx->nested.nested_run_pending &&
2438 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
2439 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
Sean Christopherson55d23752018-12-03 13:53:18 -08002440 }
2441
2442 if (nested_cpu_has_xsaves(vmcs12))
2443 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
2444
2445 /*
2446 * Whether page-faults are trapped is determined by a combination of
Paolo Bonzinia0c13432020-07-10 17:48:08 +02002447 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF. If L0
2448 * doesn't care about page faults then we should set all of these to
2449 * L1's desires. However, if L0 does care about (some) page faults, it
2450 * is not easy (if at all possible?) to merge L0 and L1's desires, we
2451 * simply ask to exit on each and every L2 page fault. This is done by
2452 * setting MASK=MATCH=0 and (see below) EB.PF=1.
Sean Christopherson55d23752018-12-03 13:53:18 -08002453 * Note that below we don't need special code to set EB.PF beyond the
2454 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
2455 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
2456 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
2457 */
Paolo Bonzinia0c13432020-07-10 17:48:08 +02002458 if (vmx_need_pf_intercept(&vmx->vcpu)) {
2459 /*
2460 * TODO: if both L0 and L1 need the same MASK and MATCH,
2461 * go ahead and use it?
2462 */
2463 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
2464 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
2465 } else {
2466 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, vmcs12->page_fault_error_code_mask);
2467 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, vmcs12->page_fault_error_code_match);
2468 }
Sean Christopherson55d23752018-12-03 13:53:18 -08002469
2470 if (cpu_has_vmx_apicv()) {
2471 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
2472 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
2473 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
2474 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
2475 }
2476
Aaron Lewis662f1d12019-11-07 21:14:39 -08002477 /*
2478 * Make sure the msr_autostore list is up to date before we set the
2479 * count in the vmcs02.
2480 */
2481 prepare_vmx_msr_autostore_list(&vmx->vcpu, MSR_IA32_TSC);
2482
2483 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, vmx->msr_autostore.guest.nr);
Sean Christopherson55d23752018-12-03 13:53:18 -08002484 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
2485 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
2486
2487 set_cr4_guest_host_mask(vmx);
Sean Christopherson55d23752018-12-03 13:53:18 -08002488}
2489
2490/*
2491 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
2492 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
2493 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
2494 * guest in a way that will both be appropriate to L1's requests, and our
2495 * needs. In addition to modifying the active vmcs (which is vmcs02), this
2496 * function also has additional necessary side-effects, like setting various
2497 * vcpu->arch fields.
2498 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
2499 * is assigned to entry_failure_code on failure.
2500 */
2501static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Sean Christopherson68cda402020-05-11 15:05:29 -07002502 enum vm_entry_failure_code *entry_failure_code)
Sean Christopherson55d23752018-12-03 13:53:18 -08002503{
2504 struct vcpu_vmx *vmx = to_vmx(vcpu);
2505 struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs;
Sean Christophersonc7554efc2019-05-07 09:06:40 -07002506 bool load_guest_pdptrs_vmcs12 = false;
Sean Christopherson55d23752018-12-03 13:53:18 -08002507
Sean Christophersonc7554efc2019-05-07 09:06:40 -07002508 if (vmx->nested.dirty_vmcs12 || hv_evmcs) {
Paolo Bonzinib1346ab2019-06-06 17:24:00 +02002509 prepare_vmcs02_rare(vmx, vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08002510 vmx->nested.dirty_vmcs12 = false;
Sean Christopherson55d23752018-12-03 13:53:18 -08002511
Sean Christophersonc7554efc2019-05-07 09:06:40 -07002512 load_guest_pdptrs_vmcs12 = !hv_evmcs ||
2513 !(hv_evmcs->hv_clean_fields &
2514 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1);
Sean Christopherson55d23752018-12-03 13:53:18 -08002515 }
2516
2517 if (vmx->nested.nested_run_pending &&
2518 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
2519 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
2520 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
2521 } else {
2522 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
2523 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
2524 }
Sean Christopherson3b013a22019-05-07 09:06:28 -07002525 if (kvm_mpx_supported() && (!vmx->nested.nested_run_pending ||
2526 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)))
2527 vmcs_write64(GUEST_BNDCFGS, vmx->nested.vmcs01_guest_bndcfgs);
Sean Christopherson55d23752018-12-03 13:53:18 -08002528 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
2529
Sean Christopherson55d23752018-12-03 13:53:18 -08002530 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
2531 * bitwise-or of what L1 wants to trap for L2, and what we want to
2532 * trap. Note that CR0.TS also needs updating - we do this later.
2533 */
2534 update_exception_bitmap(vcpu);
2535 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
2536 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
2537
2538 if (vmx->nested.nested_run_pending &&
2539 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
2540 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
2541 vcpu->arch.pat = vmcs12->guest_ia32_pat;
2542 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2543 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
2544 }
2545
2546 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
2547
2548 if (kvm_has_tsc_control)
2549 decache_tsc_multiplier(vmx);
2550
Sean Christopherson50b265a2020-03-20 14:28:19 -07002551 nested_vmx_transition_tlb_flush(vcpu, vmcs12, true);
Sean Christopherson55d23752018-12-03 13:53:18 -08002552
2553 if (nested_cpu_has_ept(vmcs12))
2554 nested_ept_init_mmu_context(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08002555
2556 /*
2557 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
2558 * bits which we consider mandatory enabled.
2559 * The CR0_READ_SHADOW is what L2 should have expected to read given
2560 * the specifications by L1; It's not enough to take
2561 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
2562 * have more bits than L1 expected.
2563 */
2564 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
2565 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
2566
2567 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
2568 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
2569
2570 vcpu->arch.efer = nested_vmx_calc_efer(vmx, vmcs12);
2571 /* Note: may modify VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
2572 vmx_set_efer(vcpu, vcpu->arch.efer);
2573
2574 /*
2575 * Guest state is invalid and unrestricted guest is disabled,
2576 * which means L1 attempted VMEntry to L2 with invalid state.
2577 * Fail the VMEntry.
2578 */
Sean Christopherson2ba44932020-09-23 11:44:48 -07002579 if (CC(!vmx_guest_state_valid(vcpu))) {
Sean Christopherson55d23752018-12-03 13:53:18 -08002580 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christophersonc80add02019-04-11 12:18:09 -07002581 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002582 }
2583
2584 /* Shadow page tables on either EPT or shadow page tables. */
2585 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
2586 entry_failure_code))
Sean Christophersonc80add02019-04-11 12:18:09 -07002587 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002588
Sean Christopherson04f11ef2019-09-27 14:45:16 -07002589 /*
2590 * Immediately write vmcs02.GUEST_CR3. It will be propagated to vmcs12
2591 * on nested VM-Exit, which can occur without actually running L2 and
Paolo Bonzini727a7e22020-03-05 03:52:50 -05002592 * thus without hitting vmx_load_mmu_pgd(), e.g. if L1 is entering L2 with
Sean Christopherson04f11ef2019-09-27 14:45:16 -07002593 * vmcs12.GUEST_ACTIVITYSTATE=HLT, in which case KVM will intercept the
2594 * transition to HLT instead of running L2.
2595 */
2596 if (enable_ept)
2597 vmcs_writel(GUEST_CR3, vmcs12->guest_cr3);
2598
Sean Christophersonc7554efc2019-05-07 09:06:40 -07002599 /* Late preparation of GUEST_PDPTRs now that EFER and CRs are set. */
2600 if (load_guest_pdptrs_vmcs12 && nested_cpu_has_ept(vmcs12) &&
2601 is_pae_paging(vcpu)) {
2602 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
2603 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
2604 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
2605 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
2606 }
2607
Sean Christopherson55d23752018-12-03 13:53:18 -08002608 if (!enable_ept)
2609 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
2610
Oliver Upton71f73472019-11-13 16:17:19 -08002611 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) &&
Oliver Uptond1968422019-12-13 16:33:58 -08002612 WARN_ON_ONCE(kvm_set_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL,
2613 vmcs12->guest_ia32_perf_global_ctrl)))
Oliver Upton71f73472019-11-13 16:17:19 -08002614 return -EINVAL;
2615
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02002616 kvm_rsp_write(vcpu, vmcs12->guest_rsp);
2617 kvm_rip_write(vcpu, vmcs12->guest_rip);
Sean Christopherson55d23752018-12-03 13:53:18 -08002618 return 0;
2619}
2620
2621static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
2622{
Sean Christopherson5497b952019-07-11 08:58:29 -07002623 if (CC(!nested_cpu_has_nmi_exiting(vmcs12) &&
2624 nested_cpu_has_virtual_nmis(vmcs12)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002625 return -EINVAL;
2626
Sean Christopherson5497b952019-07-11 08:58:29 -07002627 if (CC(!nested_cpu_has_virtual_nmis(vmcs12) &&
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08002628 nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002629 return -EINVAL;
2630
2631 return 0;
2632}
2633
Sean Christophersonac6389a2020-03-02 18:02:38 -08002634static bool nested_vmx_check_eptp(struct kvm_vcpu *vcpu, u64 new_eptp)
Sean Christopherson55d23752018-12-03 13:53:18 -08002635{
2636 struct vcpu_vmx *vmx = to_vmx(vcpu);
2637 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2638
2639 /* Check for memory type validity */
Sean Christophersonac6389a2020-03-02 18:02:38 -08002640 switch (new_eptp & VMX_EPTP_MT_MASK) {
Sean Christopherson55d23752018-12-03 13:53:18 -08002641 case VMX_EPTP_MT_UC:
Sean Christopherson5497b952019-07-11 08:58:29 -07002642 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002643 return false;
2644 break;
2645 case VMX_EPTP_MT_WB:
Sean Christopherson5497b952019-07-11 08:58:29 -07002646 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002647 return false;
2648 break;
2649 default:
2650 return false;
2651 }
2652
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08002653 /* Page-walk levels validity. */
Sean Christophersonac6389a2020-03-02 18:02:38 -08002654 switch (new_eptp & VMX_EPTP_PWL_MASK) {
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08002655 case VMX_EPTP_PWL_5:
2656 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_PAGE_WALK_5_BIT)))
2657 return false;
2658 break;
2659 case VMX_EPTP_PWL_4:
2660 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_PAGE_WALK_4_BIT)))
2661 return false;
2662 break;
2663 default:
Sean Christopherson55d23752018-12-03 13:53:18 -08002664 return false;
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08002665 }
Sean Christopherson55d23752018-12-03 13:53:18 -08002666
2667 /* Reserved bits should not be set */
Sean Christophersonac6389a2020-03-02 18:02:38 -08002668 if (CC(new_eptp >> maxphyaddr || ((new_eptp >> 7) & 0x1f)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002669 return false;
2670
2671 /* AD, if set, should be supported */
Sean Christophersonac6389a2020-03-02 18:02:38 -08002672 if (new_eptp & VMX_EPTP_AD_ENABLE_BIT) {
Sean Christopherson5497b952019-07-11 08:58:29 -07002673 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002674 return false;
2675 }
2676
2677 return true;
2678}
2679
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002680/*
2681 * Checks related to VM-Execution Control Fields
2682 */
2683static int nested_check_vm_execution_controls(struct kvm_vcpu *vcpu,
2684 struct vmcs12 *vmcs12)
2685{
2686 struct vcpu_vmx *vmx = to_vmx(vcpu);
2687
Sean Christopherson5497b952019-07-11 08:58:29 -07002688 if (CC(!vmx_control_verify(vmcs12->pin_based_vm_exec_control,
2689 vmx->nested.msrs.pinbased_ctls_low,
2690 vmx->nested.msrs.pinbased_ctls_high)) ||
2691 CC(!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
2692 vmx->nested.msrs.procbased_ctls_low,
2693 vmx->nested.msrs.procbased_ctls_high)))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002694 return -EINVAL;
2695
2696 if (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
Sean Christopherson5497b952019-07-11 08:58:29 -07002697 CC(!vmx_control_verify(vmcs12->secondary_vm_exec_control,
2698 vmx->nested.msrs.secondary_ctls_low,
2699 vmx->nested.msrs.secondary_ctls_high)))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002700 return -EINVAL;
2701
Sean Christopherson5497b952019-07-11 08:58:29 -07002702 if (CC(vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu)) ||
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002703 nested_vmx_check_io_bitmap_controls(vcpu, vmcs12) ||
2704 nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12) ||
2705 nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12) ||
2706 nested_vmx_check_apic_access_controls(vcpu, vmcs12) ||
2707 nested_vmx_check_apicv_controls(vcpu, vmcs12) ||
2708 nested_vmx_check_nmi_controls(vmcs12) ||
2709 nested_vmx_check_pml_controls(vcpu, vmcs12) ||
2710 nested_vmx_check_unrestricted_guest_controls(vcpu, vmcs12) ||
2711 nested_vmx_check_mode_based_ept_exec_controls(vcpu, vmcs12) ||
2712 nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12) ||
Sean Christopherson5497b952019-07-11 08:58:29 -07002713 CC(nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002714 return -EINVAL;
2715
Sean Christophersonbc441212019-02-12 16:42:23 -08002716 if (!nested_cpu_has_preemption_timer(vmcs12) &&
2717 nested_cpu_has_save_preemption_timer(vmcs12))
2718 return -EINVAL;
2719
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002720 if (nested_cpu_has_ept(vmcs12) &&
Sean Christophersonac6389a2020-03-02 18:02:38 -08002721 CC(!nested_vmx_check_eptp(vcpu, vmcs12->ept_pointer)))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002722 return -EINVAL;
2723
2724 if (nested_cpu_has_vmfunc(vmcs12)) {
Sean Christopherson5497b952019-07-11 08:58:29 -07002725 if (CC(vmcs12->vm_function_control &
2726 ~vmx->nested.msrs.vmfunc_controls))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002727 return -EINVAL;
2728
2729 if (nested_cpu_has_eptp_switching(vmcs12)) {
Sean Christopherson5497b952019-07-11 08:58:29 -07002730 if (CC(!nested_cpu_has_ept(vmcs12)) ||
2731 CC(!page_address_valid(vcpu, vmcs12->eptp_list_address)))
Krish Sadhukhan461b4ba2018-12-12 13:30:07 -05002732 return -EINVAL;
2733 }
2734 }
2735
2736 return 0;
2737}
2738
Krish Sadhukhan61446ba2018-12-12 13:30:09 -05002739/*
2740 * Checks related to VM-Exit Control Fields
2741 */
2742static int nested_check_vm_exit_controls(struct kvm_vcpu *vcpu,
2743 struct vmcs12 *vmcs12)
2744{
2745 struct vcpu_vmx *vmx = to_vmx(vcpu);
2746
Sean Christopherson5497b952019-07-11 08:58:29 -07002747 if (CC(!vmx_control_verify(vmcs12->vm_exit_controls,
2748 vmx->nested.msrs.exit_ctls_low,
2749 vmx->nested.msrs.exit_ctls_high)) ||
2750 CC(nested_vmx_check_exit_msr_switch_controls(vcpu, vmcs12)))
Krish Sadhukhan61446ba2018-12-12 13:30:09 -05002751 return -EINVAL;
2752
2753 return 0;
2754}
2755
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002756/*
2757 * Checks related to VM-Entry Control Fields
2758 */
2759static int nested_check_vm_entry_controls(struct kvm_vcpu *vcpu,
2760 struct vmcs12 *vmcs12)
Sean Christopherson55d23752018-12-03 13:53:18 -08002761{
2762 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08002763
Sean Christopherson5497b952019-07-11 08:58:29 -07002764 if (CC(!vmx_control_verify(vmcs12->vm_entry_controls,
2765 vmx->nested.msrs.entry_ctls_low,
2766 vmx->nested.msrs.entry_ctls_high)))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002767 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002768
2769 /*
2770 * From the Intel SDM, volume 3:
2771 * Fields relevant to VM-entry event injection must be set properly.
2772 * These fields are the VM-entry interruption-information field, the
2773 * VM-entry exception error code, and the VM-entry instruction length.
2774 */
2775 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
2776 u32 intr_info = vmcs12->vm_entry_intr_info_field;
2777 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
2778 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
2779 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
2780 bool should_have_error_code;
2781 bool urg = nested_cpu_has2(vmcs12,
2782 SECONDARY_EXEC_UNRESTRICTED_GUEST);
2783 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
2784
2785 /* VM-entry interruption-info field: interruption type */
Sean Christopherson5497b952019-07-11 08:58:29 -07002786 if (CC(intr_type == INTR_TYPE_RESERVED) ||
2787 CC(intr_type == INTR_TYPE_OTHER_EVENT &&
2788 !nested_cpu_supports_monitor_trap_flag(vcpu)))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002789 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002790
2791 /* VM-entry interruption-info field: vector */
Sean Christopherson5497b952019-07-11 08:58:29 -07002792 if (CC(intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
2793 CC(intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
2794 CC(intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002795 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002796
2797 /* VM-entry interruption-info field: deliver error code */
2798 should_have_error_code =
2799 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
2800 x86_exception_has_error_code(vector);
Sean Christopherson5497b952019-07-11 08:58:29 -07002801 if (CC(has_error_code != should_have_error_code))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002802 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002803
2804 /* VM-entry exception error code */
Sean Christopherson5497b952019-07-11 08:58:29 -07002805 if (CC(has_error_code &&
Sean Christopherson567926c2019-10-01 09:21:23 -07002806 vmcs12->vm_entry_exception_error_code & GENMASK(31, 16)))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002807 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002808
2809 /* VM-entry interruption-info field: reserved bits */
Sean Christopherson5497b952019-07-11 08:58:29 -07002810 if (CC(intr_info & INTR_INFO_RESVD_BITS_MASK))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002811 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002812
2813 /* VM-entry instruction length */
2814 switch (intr_type) {
2815 case INTR_TYPE_SOFT_EXCEPTION:
2816 case INTR_TYPE_SOFT_INTR:
2817 case INTR_TYPE_PRIV_SW_EXCEPTION:
Sean Christopherson5497b952019-07-11 08:58:29 -07002818 if (CC(vmcs12->vm_entry_instruction_len > 15) ||
2819 CC(vmcs12->vm_entry_instruction_len == 0 &&
2820 CC(!nested_cpu_has_zero_length_injection(vcpu))))
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002821 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002822 }
2823 }
2824
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002825 if (nested_vmx_check_entry_msr_switch_controls(vcpu, vmcs12))
2826 return -EINVAL;
2827
2828 return 0;
2829}
2830
Sean Christopherson5478ba32019-04-11 12:18:06 -07002831static int nested_vmx_check_controls(struct kvm_vcpu *vcpu,
2832 struct vmcs12 *vmcs12)
2833{
2834 if (nested_check_vm_execution_controls(vcpu, vmcs12) ||
2835 nested_check_vm_exit_controls(vcpu, vmcs12) ||
2836 nested_check_vm_entry_controls(vcpu, vmcs12))
Paolo Bonzini98d9e852019-04-12 10:19:57 +02002837 return -EINVAL;
Sean Christopherson5478ba32019-04-11 12:18:06 -07002838
Vitaly Kuznetsova8350232020-02-05 13:30:34 +01002839 if (to_vmx(vcpu)->nested.enlightened_vmcs_enabled)
2840 return nested_evmcs_check_controls(vmcs12);
2841
Sean Christopherson5478ba32019-04-11 12:18:06 -07002842 return 0;
2843}
2844
Paolo Bonzini98d9e852019-04-12 10:19:57 +02002845static int nested_vmx_check_host_state(struct kvm_vcpu *vcpu,
2846 struct vmcs12 *vmcs12)
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002847{
2848 bool ia32e;
2849
Sean Christopherson5497b952019-07-11 08:58:29 -07002850 if (CC(!nested_host_cr0_valid(vcpu, vmcs12->host_cr0)) ||
2851 CC(!nested_host_cr4_valid(vcpu, vmcs12->host_cr4)) ||
2852 CC(!nested_cr3_valid(vcpu, vmcs12->host_cr3)))
Krish Sadhukhan254b2f32018-12-12 13:30:11 -05002853 return -EINVAL;
Krish Sadhukhan711eff32019-02-07 14:05:30 -05002854
Sean Christopherson5497b952019-07-11 08:58:29 -07002855 if (CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_esp, vcpu)) ||
2856 CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_eip, vcpu)))
Krish Sadhukhan711eff32019-02-07 14:05:30 -05002857 return -EINVAL;
2858
Krish Sadhukhanf6b0db1f2019-04-08 17:35:11 -04002859 if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) &&
Sean Christopherson5497b952019-07-11 08:58:29 -07002860 CC(!kvm_pat_valid(vmcs12->host_ia32_pat)))
Krish Sadhukhanf6b0db1f2019-04-08 17:35:11 -04002861 return -EINVAL;
2862
Oliver Uptonc547cb62019-11-13 16:17:17 -08002863 if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) &&
2864 CC(!kvm_valid_perf_global_ctrl(vcpu_to_pmu(vcpu),
2865 vmcs12->host_ia32_perf_global_ctrl)))
2866 return -EINVAL;
2867
Paolo Bonzinifd3edd42019-09-25 18:33:53 +02002868#ifdef CONFIG_X86_64
2869 ia32e = !!(vcpu->arch.efer & EFER_LMA);
2870#else
2871 ia32e = false;
2872#endif
2873
2874 if (ia32e) {
2875 if (CC(!(vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)) ||
2876 CC(!(vmcs12->host_cr4 & X86_CR4_PAE)))
2877 return -EINVAL;
2878 } else {
2879 if (CC(vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) ||
2880 CC(vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) ||
2881 CC(vmcs12->host_cr4 & X86_CR4_PCIDE) ||
2882 CC((vmcs12->host_rip) >> 32))
2883 return -EINVAL;
2884 }
Krish Sadhukhan1ef23e12019-07-03 19:54:35 -04002885
Sean Christopherson5497b952019-07-11 08:58:29 -07002886 if (CC(vmcs12->host_cs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2887 CC(vmcs12->host_ss_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2888 CC(vmcs12->host_ds_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2889 CC(vmcs12->host_es_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2890 CC(vmcs12->host_fs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2891 CC(vmcs12->host_gs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2892 CC(vmcs12->host_tr_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2893 CC(vmcs12->host_cs_selector == 0) ||
2894 CC(vmcs12->host_tr_selector == 0) ||
2895 CC(vmcs12->host_ss_selector == 0 && !ia32e))
Krish Sadhukhan1ef23e12019-07-03 19:54:35 -04002896 return -EINVAL;
2897
Sean Christopherson5497b952019-07-11 08:58:29 -07002898 if (CC(is_noncanonical_address(vmcs12->host_fs_base, vcpu)) ||
2899 CC(is_noncanonical_address(vmcs12->host_gs_base, vcpu)) ||
2900 CC(is_noncanonical_address(vmcs12->host_gdtr_base, vcpu)) ||
2901 CC(is_noncanonical_address(vmcs12->host_idtr_base, vcpu)) ||
Paolo Bonzinifd3edd42019-09-25 18:33:53 +02002902 CC(is_noncanonical_address(vmcs12->host_tr_base, vcpu)) ||
2903 CC(is_noncanonical_address(vmcs12->host_rip, vcpu)))
Krish Sadhukhan58450382019-08-09 12:26:19 -07002904 return -EINVAL;
Krish Sadhukhan1ef23e12019-07-03 19:54:35 -04002905
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002906 /*
2907 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
2908 * IA32_EFER MSR must be 0 in the field for that register. In addition,
2909 * the values of the LMA and LME bits in the field must each be that of
2910 * the host address-space size VM-exit control.
2911 */
2912 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
Sean Christopherson5497b952019-07-11 08:58:29 -07002913 if (CC(!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer)) ||
2914 CC(ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA)) ||
2915 CC(ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)))
Krish Sadhukhan254b2f32018-12-12 13:30:11 -05002916 return -EINVAL;
Krish Sadhukhan5fbf9632018-12-12 13:30:10 -05002917 }
2918
Sean Christopherson55d23752018-12-03 13:53:18 -08002919 return 0;
2920}
2921
2922static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
2923 struct vmcs12 *vmcs12)
2924{
KarimAllah Ahmed88925302019-01-31 21:24:41 +01002925 int r = 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08002926 struct vmcs12 *shadow;
KarimAllah Ahmed88925302019-01-31 21:24:41 +01002927 struct kvm_host_map map;
Sean Christopherson55d23752018-12-03 13:53:18 -08002928
2929 if (vmcs12->vmcs_link_pointer == -1ull)
2930 return 0;
2931
Sean Christopherson5497b952019-07-11 08:58:29 -07002932 if (CC(!page_address_valid(vcpu, vmcs12->vmcs_link_pointer)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002933 return -EINVAL;
2934
Sean Christopherson5497b952019-07-11 08:58:29 -07002935 if (CC(kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->vmcs_link_pointer), &map)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002936 return -EINVAL;
2937
KarimAllah Ahmed88925302019-01-31 21:24:41 +01002938 shadow = map.hva;
2939
Sean Christopherson5497b952019-07-11 08:58:29 -07002940 if (CC(shadow->hdr.revision_id != VMCS12_REVISION) ||
2941 CC(shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12)))
Sean Christopherson55d23752018-12-03 13:53:18 -08002942 r = -EINVAL;
KarimAllah Ahmed88925302019-01-31 21:24:41 +01002943
2944 kvm_vcpu_unmap(vcpu, &map, false);
Sean Christopherson55d23752018-12-03 13:53:18 -08002945 return r;
2946}
2947
Sean Christopherson55d23752018-12-03 13:53:18 -08002948/*
2949 * Checks related to Guest Non-register State
2950 */
2951static int nested_check_guest_non_reg_state(struct vmcs12 *vmcs12)
2952{
Sean Christopherson5497b952019-07-11 08:58:29 -07002953 if (CC(vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
2954 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT))
Sean Christopherson55d23752018-12-03 13:53:18 -08002955 return -EINVAL;
2956
2957 return 0;
2958}
2959
Sean Christopherson5478ba32019-04-11 12:18:06 -07002960static int nested_vmx_check_guest_state(struct kvm_vcpu *vcpu,
2961 struct vmcs12 *vmcs12,
Sean Christopherson68cda402020-05-11 15:05:29 -07002962 enum vm_entry_failure_code *entry_failure_code)
Sean Christopherson55d23752018-12-03 13:53:18 -08002963{
2964 bool ia32e;
2965
Sean Christopherson68cda402020-05-11 15:05:29 -07002966 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson55d23752018-12-03 13:53:18 -08002967
Sean Christopherson5497b952019-07-11 08:58:29 -07002968 if (CC(!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0)) ||
2969 CC(!nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4)))
Sean Christophersonc80add02019-04-11 12:18:09 -07002970 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002971
Krish Sadhukhanb91991b2020-01-15 19:54:32 -05002972 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) &&
2973 CC(!kvm_dr7_valid(vmcs12->guest_dr7)))
2974 return -EINVAL;
2975
Krish Sadhukhande2bc2b2019-04-08 17:35:12 -04002976 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) &&
Sean Christopherson5497b952019-07-11 08:58:29 -07002977 CC(!kvm_pat_valid(vmcs12->guest_ia32_pat)))
Sean Christophersonc80add02019-04-11 12:18:09 -07002978 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002979
2980 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
Sean Christopherson68cda402020-05-11 15:05:29 -07002981 *entry_failure_code = ENTRY_FAIL_VMCS_LINK_PTR;
Sean Christophersonc80add02019-04-11 12:18:09 -07002982 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08002983 }
2984
Oliver Uptonbfc6ad62019-11-13 16:17:16 -08002985 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) &&
2986 CC(!kvm_valid_perf_global_ctrl(vcpu_to_pmu(vcpu),
2987 vmcs12->guest_ia32_perf_global_ctrl)))
2988 return -EINVAL;
2989
Sean Christopherson55d23752018-12-03 13:53:18 -08002990 /*
2991 * If the load IA32_EFER VM-entry control is 1, the following checks
2992 * are performed on the field for the IA32_EFER MSR:
2993 * - Bits reserved in the IA32_EFER MSR must be 0.
2994 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
2995 * the IA-32e mode guest VM-exit control. It must also be identical
2996 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
2997 * CR0.PG) is 1.
2998 */
2999 if (to_vmx(vcpu)->nested.nested_run_pending &&
3000 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
3001 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
Sean Christopherson5497b952019-07-11 08:58:29 -07003002 if (CC(!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer)) ||
3003 CC(ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA)) ||
3004 CC(((vmcs12->guest_cr0 & X86_CR0_PG) &&
3005 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))))
Sean Christophersonc80add02019-04-11 12:18:09 -07003006 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08003007 }
3008
3009 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
Sean Christopherson5497b952019-07-11 08:58:29 -07003010 (CC(is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu)) ||
3011 CC((vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD))))
Sean Christophersonc80add02019-04-11 12:18:09 -07003012 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08003013
Sean Christopherson9c3e9222019-04-11 12:18:05 -07003014 if (nested_check_guest_non_reg_state(vmcs12))
Sean Christophersonc80add02019-04-11 12:18:09 -07003015 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08003016
3017 return 0;
3018}
3019
Sean Christopherson453eafb2018-12-20 12:25:17 -08003020static int nested_vmx_check_vmentry_hw(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08003021{
3022 struct vcpu_vmx *vmx = to_vmx(vcpu);
3023 unsigned long cr3, cr4;
Sean Christophersonf1727b42019-01-25 07:40:58 -08003024 bool vm_fail;
Sean Christopherson55d23752018-12-03 13:53:18 -08003025
3026 if (!nested_early_check)
3027 return 0;
3028
3029 if (vmx->msr_autoload.host.nr)
3030 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
3031 if (vmx->msr_autoload.guest.nr)
3032 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
3033
3034 preempt_disable();
3035
3036 vmx_prepare_switch_to_guest(vcpu);
3037
3038 /*
3039 * Induce a consistency check VMExit by clearing bit 1 in GUEST_RFLAGS,
3040 * which is reserved to '1' by hardware. GUEST_RFLAGS is guaranteed to
Miaohe Lin49f933d2020-02-27 11:20:54 +08003041 * be written (by prepare_vmcs02()) before the "real" VMEnter, i.e.
Sean Christopherson55d23752018-12-03 13:53:18 -08003042 * there is no need to preserve other bits or save/restore the field.
3043 */
3044 vmcs_writel(GUEST_RFLAGS, 0);
3045
Sean Christopherson55d23752018-12-03 13:53:18 -08003046 cr3 = __get_current_cr3_fast();
3047 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
3048 vmcs_writel(HOST_CR3, cr3);
3049 vmx->loaded_vmcs->host_state.cr3 = cr3;
3050 }
3051
3052 cr4 = cr4_read_shadow();
3053 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
3054 vmcs_writel(HOST_CR4, cr4);
3055 vmx->loaded_vmcs->host_state.cr4 = cr4;
3056 }
3057
Sean Christopherson55d23752018-12-03 13:53:18 -08003058 asm(
Sean Christopherson453eafb2018-12-20 12:25:17 -08003059 "sub $%c[wordsize], %%" _ASM_SP "\n\t" /* temporarily adjust RSP for CALL */
Sean Christopherson5a878162019-01-25 07:41:02 -08003060 "cmp %%" _ASM_SP ", %c[host_state_rsp](%[loaded_vmcs]) \n\t"
3061 "je 1f \n\t"
Sean Christophersonfbda0fd2019-01-25 07:41:01 -08003062 __ex("vmwrite %%" _ASM_SP ", %[HOST_RSP]") "\n\t"
Sean Christopherson5a878162019-01-25 07:41:02 -08003063 "mov %%" _ASM_SP ", %c[host_state_rsp](%[loaded_vmcs]) \n\t"
3064 "1: \n\t"
Sean Christopherson453eafb2018-12-20 12:25:17 -08003065 "add $%c[wordsize], %%" _ASM_SP "\n\t" /* un-adjust RSP */
Sean Christopherson55d23752018-12-03 13:53:18 -08003066
3067 /* Check if vmlaunch or vmresume is needed */
Sean Christopherson74dfa272019-01-25 07:41:00 -08003068 "cmpb $0, %c[launched](%[loaded_vmcs])\n\t"
Sean Christopherson453eafb2018-12-20 12:25:17 -08003069
Sean Christophersonf1727b42019-01-25 07:40:58 -08003070 /*
3071 * VMLAUNCH and VMRESUME clear RFLAGS.{CF,ZF} on VM-Exit, set
3072 * RFLAGS.CF on VM-Fail Invalid and set RFLAGS.ZF on VM-Fail
3073 * Valid. vmx_vmenter() directly "returns" RFLAGS, and so the
Sean Christophersonbbc0b822019-01-25 07:40:59 -08003074 * results of VM-Enter is captured via CC_{SET,OUT} to vm_fail.
Sean Christophersonf1727b42019-01-25 07:40:58 -08003075 */
Sean Christopherson453eafb2018-12-20 12:25:17 -08003076 "call vmx_vmenter\n\t"
3077
Sean Christophersonbbc0b822019-01-25 07:40:59 -08003078 CC_SET(be)
3079 : ASM_CALL_CONSTRAINT, CC_OUT(be) (vm_fail)
Sean Christopherson5a878162019-01-25 07:41:02 -08003080 : [HOST_RSP]"r"((unsigned long)HOST_RSP),
Sean Christopherson74dfa272019-01-25 07:41:00 -08003081 [loaded_vmcs]"r"(vmx->loaded_vmcs),
3082 [launched]"i"(offsetof(struct loaded_vmcs, launched)),
Sean Christopherson5a878162019-01-25 07:41:02 -08003083 [host_state_rsp]"i"(offsetof(struct loaded_vmcs, host_state.rsp)),
Sean Christopherson453eafb2018-12-20 12:25:17 -08003084 [wordsize]"i"(sizeof(ulong))
Jan Beulich5a253552019-05-27 02:45:44 -06003085 : "memory"
Sean Christopherson55d23752018-12-03 13:53:18 -08003086 );
3087
Sean Christopherson55d23752018-12-03 13:53:18 -08003088 if (vmx->msr_autoload.host.nr)
3089 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
3090 if (vmx->msr_autoload.guest.nr)
3091 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
3092
Sean Christophersonf1727b42019-01-25 07:40:58 -08003093 if (vm_fail) {
Sean Christopherson380e0052019-07-11 08:58:30 -07003094 u32 error = vmcs_read32(VM_INSTRUCTION_ERROR);
3095
Wanpeng Li541e8862019-05-17 16:49:50 +08003096 preempt_enable();
Sean Christopherson380e0052019-07-11 08:58:30 -07003097
3098 trace_kvm_nested_vmenter_failed(
3099 "early hardware check VM-instruction error: ", error);
3100 WARN_ON_ONCE(error != VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Sean Christopherson55d23752018-12-03 13:53:18 -08003101 return 1;
3102 }
3103
3104 /*
3105 * VMExit clears RFLAGS.IF and DR7, even on a consistency check.
3106 */
Sean Christopherson55d23752018-12-03 13:53:18 -08003107 if (hw_breakpoint_active())
3108 set_debugreg(__this_cpu_read(cpu_dr7), 7);
Peter Zijlstra84b6a342020-05-29 23:27:36 +02003109 local_irq_enable();
Wanpeng Li541e8862019-05-17 16:49:50 +08003110 preempt_enable();
Sean Christopherson55d23752018-12-03 13:53:18 -08003111
3112 /*
3113 * A non-failing VMEntry means we somehow entered guest mode with
3114 * an illegal RIP, and that's just the tip of the iceberg. There
3115 * is no telling what memory has been modified or what state has
3116 * been exposed to unknown code. Hitting this all but guarantees
3117 * a (very critical) hardware issue.
3118 */
3119 WARN_ON(!(vmcs_read32(VM_EXIT_REASON) &
3120 VMX_EXIT_REASONS_FAILED_VMENTRY));
3121
3122 return 0;
3123}
Sean Christopherson55d23752018-12-03 13:53:18 -08003124
Jim Mattson671ddc72019-10-15 10:44:05 -07003125static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08003126{
3127 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3128 struct vcpu_vmx *vmx = to_vmx(vcpu);
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003129 struct kvm_host_map *map;
Sean Christopherson55d23752018-12-03 13:53:18 -08003130 struct page *page;
3131 u64 hpa;
3132
Vitaly Kuznetsove942dbf2020-03-09 16:52:12 +01003133 /*
3134 * hv_evmcs may end up being not mapped after migration (when
3135 * L2 was running), map it here to make sure vmcs12 changes are
3136 * properly reflected.
3137 */
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003138 if (vmx->nested.enlightened_vmcs_enabled && !vmx->nested.hv_evmcs) {
3139 enum nested_evmptrld_status evmptrld_status =
3140 nested_vmx_handle_enlightened_vmptrld(vcpu, false);
3141
3142 if (evmptrld_status == EVMPTRLD_VMFAIL ||
3143 evmptrld_status == EVMPTRLD_ERROR) {
3144 pr_debug_ratelimited("%s: enlightened vmptrld failed\n",
3145 __func__);
3146 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3147 vcpu->run->internal.suberror =
3148 KVM_INTERNAL_ERROR_EMULATION;
3149 vcpu->run->internal.ndata = 0;
3150 return false;
3151 }
3152 }
Vitaly Kuznetsove942dbf2020-03-09 16:52:12 +01003153
Sean Christopherson55d23752018-12-03 13:53:18 -08003154 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
3155 /*
3156 * Translate L1 physical address to host physical
3157 * address for vmcs02. Keep the page pinned, so this
3158 * physical address remains valid. We keep a reference
3159 * to it so we can release it later.
3160 */
3161 if (vmx->nested.apic_access_page) { /* shouldn't happen */
Liran Alonb11494b2019-11-21 00:31:47 +02003162 kvm_release_page_clean(vmx->nested.apic_access_page);
Sean Christopherson55d23752018-12-03 13:53:18 -08003163 vmx->nested.apic_access_page = NULL;
3164 }
3165 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Sean Christopherson55d23752018-12-03 13:53:18 -08003166 if (!is_error_page(page)) {
3167 vmx->nested.apic_access_page = page;
3168 hpa = page_to_phys(vmx->nested.apic_access_page);
3169 vmcs_write64(APIC_ACCESS_ADDR, hpa);
3170 } else {
Jim Mattson671ddc72019-10-15 10:44:05 -07003171 pr_debug_ratelimited("%s: no backing 'struct page' for APIC-access address in vmcs12\n",
3172 __func__);
3173 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3174 vcpu->run->internal.suberror =
3175 KVM_INTERNAL_ERROR_EMULATION;
3176 vcpu->run->internal.ndata = 0;
3177 return false;
Sean Christopherson55d23752018-12-03 13:53:18 -08003178 }
3179 }
3180
3181 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003182 map = &vmx->nested.virtual_apic_map;
Sean Christopherson55d23752018-12-03 13:53:18 -08003183
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003184 if (!kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->virtual_apic_page_addr), map)) {
3185 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, pfn_to_hpa(map->pfn));
Paolo Bonzini69090812019-04-15 15:16:17 +02003186 } else if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING) &&
3187 nested_cpu_has(vmcs12, CPU_BASED_CR8_STORE_EXITING) &&
3188 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
3189 /*
3190 * The processor will never use the TPR shadow, simply
3191 * clear the bit from the execution control. Such a
3192 * configuration is useless, but it happens in tests.
3193 * For any other configuration, failing the vm entry is
3194 * _not_ what the processor does but it's basically the
3195 * only possibility we have.
3196 */
Sean Christopherson2183f562019-05-07 12:17:56 -07003197 exec_controls_clearbit(vmx, CPU_BASED_TPR_SHADOW);
Paolo Bonzini69090812019-04-15 15:16:17 +02003198 } else {
Sean Christophersonca2f5462019-05-07 09:06:33 -07003199 /*
3200 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR to
3201 * force VM-Entry to fail.
3202 */
3203 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Sean Christopherson55d23752018-12-03 13:53:18 -08003204 }
3205 }
3206
3207 if (nested_cpu_has_posted_intr(vmcs12)) {
KarimAllah Ahmed3278e042019-01-31 21:24:38 +01003208 map = &vmx->nested.pi_desc_map;
3209
3210 if (!kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->posted_intr_desc_addr), map)) {
3211 vmx->nested.pi_desc =
3212 (struct pi_desc *)(((void *)map->hva) +
3213 offset_in_page(vmcs12->posted_intr_desc_addr));
3214 vmcs_write64(POSTED_INTR_DESC_ADDR,
3215 pfn_to_hpa(map->pfn) + offset_in_page(vmcs12->posted_intr_desc_addr));
Sean Christopherson55d23752018-12-03 13:53:18 -08003216 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003217 }
3218 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
Sean Christopherson2183f562019-05-07 12:17:56 -07003219 exec_controls_setbit(vmx, CPU_BASED_USE_MSR_BITMAPS);
Sean Christopherson55d23752018-12-03 13:53:18 -08003220 else
Sean Christopherson2183f562019-05-07 12:17:56 -07003221 exec_controls_clearbit(vmx, CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson671ddc72019-10-15 10:44:05 -07003222 return true;
Sean Christopherson55d23752018-12-03 13:53:18 -08003223}
3224
Sean Christopherson02f5fb22020-06-22 14:58:32 -07003225static int nested_vmx_write_pml_buffer(struct kvm_vcpu *vcpu, gpa_t gpa)
3226{
3227 struct vmcs12 *vmcs12;
3228 struct vcpu_vmx *vmx = to_vmx(vcpu);
3229 gpa_t dst;
3230
3231 if (WARN_ON_ONCE(!is_guest_mode(vcpu)))
3232 return 0;
3233
3234 if (WARN_ON_ONCE(vmx->nested.pml_full))
3235 return 1;
3236
3237 /*
3238 * Check if PML is enabled for the nested guest. Whether eptp bit 6 is
3239 * set is already checked as part of A/D emulation.
3240 */
3241 vmcs12 = get_vmcs12(vcpu);
3242 if (!nested_cpu_has_pml(vmcs12))
3243 return 0;
3244
3245 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
3246 vmx->nested.pml_full = true;
3247 return 1;
3248 }
3249
3250 gpa &= ~0xFFFull;
3251 dst = vmcs12->pml_address + sizeof(u64) * vmcs12->guest_pml_index;
3252
3253 if (kvm_write_guest_page(vcpu->kvm, gpa_to_gfn(dst), &gpa,
3254 offset_in_page(dst), sizeof(gpa)))
3255 return 0;
3256
3257 vmcs12->guest_pml_index--;
3258
3259 return 0;
3260}
3261
Sean Christopherson55d23752018-12-03 13:53:18 -08003262/*
3263 * Intel's VMX Instruction Reference specifies a common set of prerequisites
3264 * for running VMX instructions (except VMXON, whose prerequisites are
3265 * slightly different). It also specifies what exception to inject otherwise.
3266 * Note that many of these exceptions have priority over VM exits, so they
3267 * don't have to be checked again here.
3268 */
3269static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
3270{
3271 if (!to_vmx(vcpu)->nested.vmxon) {
3272 kvm_queue_exception(vcpu, UD_VECTOR);
3273 return 0;
3274 }
3275
3276 if (vmx_get_cpl(vcpu)) {
3277 kvm_inject_gp(vcpu, 0);
3278 return 0;
3279 }
3280
3281 return 1;
3282}
3283
3284static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu)
3285{
3286 u8 rvi = vmx_get_rvi();
3287 u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI);
3288
3289 return ((rvi & 0xf0) > (vppr & 0xf0));
3290}
3291
3292static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
3293 struct vmcs12 *vmcs12);
3294
3295/*
3296 * If from_vmentry is false, this is being called from state restore (either RSM
3297 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
Jim Mattson671ddc72019-10-15 10:44:05 -07003298 *
3299 * Returns:
Miaohe Lin463bfee2020-02-14 10:44:05 +08003300 * NVMX_VMENTRY_SUCCESS: Entered VMX non-root mode
3301 * NVMX_VMENTRY_VMFAIL: Consistency check VMFail
3302 * NVMX_VMENTRY_VMEXIT: Consistency check VMExit
3303 * NVMX_VMENTRY_KVM_INTERNAL_ERROR: KVM internal error
Sean Christopherson55d23752018-12-03 13:53:18 -08003304 */
Jim Mattson671ddc72019-10-15 10:44:05 -07003305enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu,
3306 bool from_vmentry)
Sean Christopherson55d23752018-12-03 13:53:18 -08003307{
3308 struct vcpu_vmx *vmx = to_vmx(vcpu);
3309 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Sean Christopherson68cda402020-05-11 15:05:29 -07003310 enum vm_entry_failure_code entry_failure_code;
Sean Christopherson55d23752018-12-03 13:53:18 -08003311 bool evaluate_pending_interrupts;
Sean Christopherson68cda402020-05-11 15:05:29 -07003312 u32 exit_reason, failed_index;
Sean Christopherson55d23752018-12-03 13:53:18 -08003313
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07003314 if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu))
3315 kvm_vcpu_flush_tlb_current(vcpu);
3316
Sean Christopherson2183f562019-05-07 12:17:56 -07003317 evaluate_pending_interrupts = exec_controls_get(vmx) &
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08003318 (CPU_BASED_INTR_WINDOW_EXITING | CPU_BASED_NMI_WINDOW_EXITING);
Sean Christopherson55d23752018-12-03 13:53:18 -08003319 if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu))
3320 evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu);
3321
3322 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
3323 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
3324 if (kvm_mpx_supported() &&
3325 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
3326 vmx->nested.vmcs01_guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
3327
Sean Christophersonf087a022019-06-07 11:55:34 -07003328 /*
3329 * Overwrite vmcs01.GUEST_CR3 with L1's CR3 if EPT is disabled *and*
3330 * nested early checks are disabled. In the event of a "late" VM-Fail,
3331 * i.e. a VM-Fail detected by hardware but not KVM, KVM must unwind its
3332 * software model to the pre-VMEntry host state. When EPT is disabled,
3333 * GUEST_CR3 holds KVM's shadow CR3, not L1's "real" CR3, which causes
3334 * nested_vmx_restore_host_state() to corrupt vcpu->arch.cr3. Stuffing
3335 * vmcs01.GUEST_CR3 results in the unwind naturally setting arch.cr3 to
3336 * the correct value. Smashing vmcs01.GUEST_CR3 is safe because nested
3337 * VM-Exits, and the unwind, reset KVM's MMU, i.e. vmcs01.GUEST_CR3 is
3338 * guaranteed to be overwritten with a shadow CR3 prior to re-entering
3339 * L1. Don't stuff vmcs01.GUEST_CR3 when using nested early checks as
3340 * KVM modifies vcpu->arch.cr3 if and only if the early hardware checks
3341 * pass, and early VM-Fails do not reset KVM's MMU, i.e. the VM-Fail
3342 * path would need to manually save/restore vmcs01.GUEST_CR3.
3343 */
3344 if (!enable_ept && !nested_early_check)
3345 vmcs_writel(GUEST_CR3, vcpu->arch.cr3);
3346
Sean Christopherson55d23752018-12-03 13:53:18 -08003347 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
3348
3349 prepare_vmcs02_early(vmx, vmcs12);
3350
3351 if (from_vmentry) {
Sean Christophersonb89d5ad2020-09-23 11:44:47 -07003352 if (unlikely(!nested_get_vmcs12_pages(vcpu))) {
3353 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson671ddc72019-10-15 10:44:05 -07003354 return NVMX_VMENTRY_KVM_INTERNAL_ERROR;
Sean Christophersonb89d5ad2020-09-23 11:44:47 -07003355 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003356
3357 if (nested_vmx_check_vmentry_hw(vcpu)) {
3358 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson671ddc72019-10-15 10:44:05 -07003359 return NVMX_VMENTRY_VMFAIL;
Sean Christopherson55d23752018-12-03 13:53:18 -08003360 }
3361
Sean Christopherson68cda402020-05-11 15:05:29 -07003362 if (nested_vmx_check_guest_state(vcpu, vmcs12,
3363 &entry_failure_code)) {
3364 exit_reason = EXIT_REASON_INVALID_STATE;
3365 vmcs12->exit_qualification = entry_failure_code;
Sean Christopherson55d23752018-12-03 13:53:18 -08003366 goto vmentry_fail_vmexit;
Sean Christopherson68cda402020-05-11 15:05:29 -07003367 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003368 }
3369
3370 enter_guest_mode(vcpu);
Xiaoyao Li5e3d3942019-12-06 16:45:26 +08003371 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING)
Sean Christopherson55d23752018-12-03 13:53:18 -08003372 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
3373
Sean Christopherson68cda402020-05-11 15:05:29 -07003374 if (prepare_vmcs02(vcpu, vmcs12, &entry_failure_code)) {
3375 exit_reason = EXIT_REASON_INVALID_STATE;
3376 vmcs12->exit_qualification = entry_failure_code;
Sean Christopherson55d23752018-12-03 13:53:18 -08003377 goto vmentry_fail_vmexit_guest_mode;
Sean Christopherson68cda402020-05-11 15:05:29 -07003378 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003379
3380 if (from_vmentry) {
Sean Christopherson68cda402020-05-11 15:05:29 -07003381 failed_index = nested_vmx_load_msr(vcpu,
3382 vmcs12->vm_entry_msr_load_addr,
3383 vmcs12->vm_entry_msr_load_count);
3384 if (failed_index) {
3385 exit_reason = EXIT_REASON_MSR_LOAD_FAIL;
3386 vmcs12->exit_qualification = failed_index;
Sean Christopherson55d23752018-12-03 13:53:18 -08003387 goto vmentry_fail_vmexit_guest_mode;
Sean Christopherson68cda402020-05-11 15:05:29 -07003388 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003389 } else {
3390 /*
3391 * The MMU is not initialized to point at the right entities yet and
3392 * "get pages" would need to read data from the guest (i.e. we will
3393 * need to perform gpa to hpa translation). Request a call
3394 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
3395 * have already been set at vmentry time and should not be reset.
3396 */
3397 kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
3398 }
3399
3400 /*
3401 * If L1 had a pending IRQ/NMI until it executed
3402 * VMLAUNCH/VMRESUME which wasn't delivered because it was
3403 * disallowed (e.g. interrupts disabled), L0 needs to
3404 * evaluate if this pending event should cause an exit from L2
3405 * to L1 or delivered directly to L2 (e.g. In case L1 don't
3406 * intercept EXTERNAL_INTERRUPT).
3407 *
3408 * Usually this would be handled by the processor noticing an
3409 * IRQ/NMI window request, or checking RVI during evaluation of
3410 * pending virtual interrupts. However, this setting was done
3411 * on VMCS01 and now VMCS02 is active instead. Thus, we force L0
3412 * to perform pending event evaluation by requesting a KVM_REQ_EVENT.
3413 */
3414 if (unlikely(evaluate_pending_interrupts))
3415 kvm_make_request(KVM_REQ_EVENT, vcpu);
3416
3417 /*
Paolo Bonzini359a6c32019-01-29 19:14:46 +01003418 * Do not start the preemption timer hrtimer until after we know
3419 * we are successful, so that only nested_vmx_vmexit needs to cancel
3420 * the timer.
3421 */
3422 vmx->nested.preemption_timer_expired = false;
Peter Shier850448f2020-05-26 14:51:06 -07003423 if (nested_cpu_has_preemption_timer(vmcs12)) {
3424 u64 timer_value = vmx_calc_preemption_timer_value(vcpu);
3425 vmx_start_preemption_timer(vcpu, timer_value);
3426 }
Paolo Bonzini359a6c32019-01-29 19:14:46 +01003427
3428 /*
Sean Christopherson55d23752018-12-03 13:53:18 -08003429 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
3430 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
3431 * returned as far as L1 is concerned. It will only return (and set
3432 * the success flag) when L2 exits (see nested_vmx_vmexit()).
3433 */
Jim Mattson671ddc72019-10-15 10:44:05 -07003434 return NVMX_VMENTRY_SUCCESS;
Sean Christopherson55d23752018-12-03 13:53:18 -08003435
3436 /*
3437 * A failed consistency check that leads to a VMExit during L1's
3438 * VMEnter to L2 is a variation of a normal VMexit, as explained in
3439 * 26.7 "VM-entry failures during or after loading guest state".
3440 */
3441vmentry_fail_vmexit_guest_mode:
Xiaoyao Li5e3d3942019-12-06 16:45:26 +08003442 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING)
Sean Christopherson55d23752018-12-03 13:53:18 -08003443 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
3444 leave_guest_mode(vcpu);
3445
3446vmentry_fail_vmexit:
3447 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
3448
3449 if (!from_vmentry)
Jim Mattson671ddc72019-10-15 10:44:05 -07003450 return NVMX_VMENTRY_VMEXIT;
Sean Christopherson55d23752018-12-03 13:53:18 -08003451
3452 load_vmcs12_host_state(vcpu, vmcs12);
3453 vmcs12->vm_exit_reason = exit_reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
Sean Christopherson55d23752018-12-03 13:53:18 -08003454 if (enable_shadow_vmcs || vmx->nested.hv_evmcs)
Sean Christopherson3731905ef2019-05-07 08:36:27 -07003455 vmx->nested.need_vmcs12_to_shadow_sync = true;
Jim Mattson671ddc72019-10-15 10:44:05 -07003456 return NVMX_VMENTRY_VMEXIT;
Sean Christopherson55d23752018-12-03 13:53:18 -08003457}
3458
3459/*
3460 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
3461 * for running an L2 nested guest.
3462 */
3463static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
3464{
3465 struct vmcs12 *vmcs12;
Jim Mattson671ddc72019-10-15 10:44:05 -07003466 enum nvmx_vmentry_status status;
Sean Christopherson55d23752018-12-03 13:53:18 -08003467 struct vcpu_vmx *vmx = to_vmx(vcpu);
3468 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003469 enum nested_evmptrld_status evmptrld_status;
Sean Christopherson55d23752018-12-03 13:53:18 -08003470
3471 if (!nested_vmx_check_permission(vcpu))
3472 return 1;
3473
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003474 evmptrld_status = nested_vmx_handle_enlightened_vmptrld(vcpu, launch);
3475 if (evmptrld_status == EVMPTRLD_ERROR) {
3476 kvm_queue_exception(vcpu, UD_VECTOR);
Sean Christopherson55d23752018-12-03 13:53:18 -08003477 return 1;
Sean Christophersonfc595f32020-08-12 11:06:15 -07003478 } else if (CC(evmptrld_status == EVMPTRLD_VMFAIL)) {
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +01003479 return nested_vmx_failInvalid(vcpu);
3480 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003481
Sean Christophersonfc595f32020-08-12 11:06:15 -07003482 if (CC(!vmx->nested.hv_evmcs && vmx->nested.current_vmptr == -1ull))
Sean Christopherson55d23752018-12-03 13:53:18 -08003483 return nested_vmx_failInvalid(vcpu);
3484
3485 vmcs12 = get_vmcs12(vcpu);
3486
3487 /*
3488 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
3489 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
3490 * rather than RFLAGS.ZF, and no error number is stored to the
3491 * VM-instruction error field.
3492 */
Sean Christophersonfc595f32020-08-12 11:06:15 -07003493 if (CC(vmcs12->hdr.shadow_vmcs))
Sean Christopherson55d23752018-12-03 13:53:18 -08003494 return nested_vmx_failInvalid(vcpu);
3495
3496 if (vmx->nested.hv_evmcs) {
3497 copy_enlightened_to_vmcs12(vmx);
3498 /* Enlightened VMCS doesn't have launch state */
3499 vmcs12->launch_state = !launch;
3500 } else if (enable_shadow_vmcs) {
3501 copy_shadow_to_vmcs12(vmx);
3502 }
3503
3504 /*
3505 * The nested entry process starts with enforcing various prerequisites
3506 * on vmcs12 as required by the Intel SDM, and act appropriately when
3507 * they fail: As the SDM explains, some conditions should cause the
3508 * instruction to fail, while others will cause the instruction to seem
3509 * to succeed, but return an EXIT_REASON_INVALID_STATE.
3510 * To speed up the normal (success) code path, we should avoid checking
3511 * for misconfigurations which will anyway be caught by the processor
3512 * when using the merged vmcs02.
3513 */
Sean Christophersonfc595f32020-08-12 11:06:15 -07003514 if (CC(interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS))
Sean Christophersonb2656e42020-06-08 18:56:07 -07003515 return nested_vmx_fail(vcpu, VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
Sean Christopherson55d23752018-12-03 13:53:18 -08003516
Sean Christophersonfc595f32020-08-12 11:06:15 -07003517 if (CC(vmcs12->launch_state == launch))
Sean Christophersonb2656e42020-06-08 18:56:07 -07003518 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08003519 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
3520 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
3521
Paolo Bonzini98d9e852019-04-12 10:19:57 +02003522 if (nested_vmx_check_controls(vcpu, vmcs12))
Sean Christophersonb2656e42020-06-08 18:56:07 -07003523 return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Sean Christopherson5478ba32019-04-11 12:18:06 -07003524
Paolo Bonzini98d9e852019-04-12 10:19:57 +02003525 if (nested_vmx_check_host_state(vcpu, vmcs12))
Sean Christophersonb2656e42020-06-08 18:56:07 -07003526 return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
Sean Christopherson55d23752018-12-03 13:53:18 -08003527
3528 /*
3529 * We're finally done with prerequisite checking, and can start with
3530 * the nested entry.
3531 */
3532 vmx->nested.nested_run_pending = 1;
Peter Shier850448f2020-05-26 14:51:06 -07003533 vmx->nested.has_preemption_timer_deadline = false;
Jim Mattson671ddc72019-10-15 10:44:05 -07003534 status = nested_vmx_enter_non_root_mode(vcpu, true);
3535 if (unlikely(status != NVMX_VMENTRY_SUCCESS))
3536 goto vmentry_failed;
Sean Christopherson55d23752018-12-03 13:53:18 -08003537
Sean Christopherson25bb2cf2020-08-12 10:51:29 -07003538 /* Emulate processing of posted interrupts on VM-Enter. */
3539 if (nested_cpu_has_posted_intr(vmcs12) &&
3540 kvm_apic_has_interrupt(vcpu) == vmx->nested.posted_intr_nv) {
3541 vmx->nested.pi_pending = true;
3542 kvm_make_request(KVM_REQ_EVENT, vcpu);
3543 kvm_apic_clear_irr(vcpu, vmx->nested.posted_intr_nv);
3544 }
3545
Sean Christopherson55d23752018-12-03 13:53:18 -08003546 /* Hide L1D cache contents from the nested guest. */
3547 vmx->vcpu.arch.l1tf_flush_l1d = true;
3548
3549 /*
3550 * Must happen outside of nested_vmx_enter_non_root_mode() as it will
3551 * also be used as part of restoring nVMX state for
3552 * snapshot restore (migration).
3553 *
3554 * In this flow, it is assumed that vmcs12 cache was
3555 * trasferred as part of captured nVMX state and should
3556 * therefore not be read from guest memory (which may not
3557 * exist on destination host yet).
3558 */
3559 nested_cache_shadow_vmcs12(vcpu, vmcs12);
3560
3561 /*
Jim Mattson9ebdfe52018-11-26 11:22:32 -08003562 * If we're entering a halted L2 vcpu and the L2 vcpu won't be
3563 * awakened by event injection or by an NMI-window VM-exit or
3564 * by an interrupt-window VM-exit, halt the vcpu.
Sean Christopherson55d23752018-12-03 13:53:18 -08003565 */
3566 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson9ebdfe52018-11-26 11:22:32 -08003567 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) &&
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08003568 !(vmcs12->cpu_based_vm_exec_control & CPU_BASED_NMI_WINDOW_EXITING) &&
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08003569 !((vmcs12->cpu_based_vm_exec_control & CPU_BASED_INTR_WINDOW_EXITING) &&
Jim Mattson9ebdfe52018-11-26 11:22:32 -08003570 (vmcs12->guest_rflags & X86_EFLAGS_IF))) {
Sean Christopherson55d23752018-12-03 13:53:18 -08003571 vmx->nested.nested_run_pending = 0;
3572 return kvm_vcpu_halt(vcpu);
3573 }
3574 return 1;
Jim Mattson671ddc72019-10-15 10:44:05 -07003575
3576vmentry_failed:
3577 vmx->nested.nested_run_pending = 0;
3578 if (status == NVMX_VMENTRY_KVM_INTERNAL_ERROR)
3579 return 0;
3580 if (status == NVMX_VMENTRY_VMEXIT)
3581 return 1;
3582 WARN_ON_ONCE(status != NVMX_VMENTRY_VMFAIL);
Sean Christophersonb2656e42020-06-08 18:56:07 -07003583 return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Sean Christopherson55d23752018-12-03 13:53:18 -08003584}
3585
3586/*
3587 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
Miaohe Lin67b0ae42019-12-11 14:26:22 +08003588 * because L2 may have changed some cr0 bits directly (CR0_GUEST_HOST_MASK).
Sean Christopherson55d23752018-12-03 13:53:18 -08003589 * This function returns the new value we should put in vmcs12.guest_cr0.
3590 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
3591 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
3592 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
3593 * didn't trap the bit, because if L1 did, so would L0).
3594 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
3595 * been modified by L2, and L1 knows it. So just leave the old value of
3596 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
3597 * isn't relevant, because if L0 traps this bit it can set it to anything.
3598 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
3599 * changed these bits, and therefore they need to be updated, but L0
3600 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
3601 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
3602 */
3603static inline unsigned long
3604vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
3605{
3606 return
3607 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
3608 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
3609 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
3610 vcpu->arch.cr0_guest_owned_bits));
3611}
3612
3613static inline unsigned long
3614vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
3615{
3616 return
3617 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
3618 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
3619 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
3620 vcpu->arch.cr4_guest_owned_bits));
3621}
3622
3623static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
3624 struct vmcs12 *vmcs12)
3625{
3626 u32 idt_vectoring;
3627 unsigned int nr;
3628
3629 if (vcpu->arch.exception.injected) {
3630 nr = vcpu->arch.exception.nr;
3631 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
3632
3633 if (kvm_exception_is_soft(nr)) {
3634 vmcs12->vm_exit_instruction_len =
3635 vcpu->arch.event_exit_inst_len;
3636 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
3637 } else
3638 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
3639
3640 if (vcpu->arch.exception.has_error_code) {
3641 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
3642 vmcs12->idt_vectoring_error_code =
3643 vcpu->arch.exception.error_code;
3644 }
3645
3646 vmcs12->idt_vectoring_info_field = idt_vectoring;
3647 } else if (vcpu->arch.nmi_injected) {
3648 vmcs12->idt_vectoring_info_field =
3649 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
3650 } else if (vcpu->arch.interrupt.injected) {
3651 nr = vcpu->arch.interrupt.nr;
3652 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
3653
3654 if (vcpu->arch.interrupt.soft) {
3655 idt_vectoring |= INTR_TYPE_SOFT_INTR;
3656 vmcs12->vm_entry_instruction_len =
3657 vcpu->arch.event_exit_inst_len;
3658 } else
3659 idt_vectoring |= INTR_TYPE_EXT_INTR;
3660
3661 vmcs12->idt_vectoring_info_field = idt_vectoring;
3662 }
3663}
3664
3665
Paolo Bonzini96b100c2020-03-17 18:32:50 +01003666void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08003667{
3668 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3669 gfn_t gfn;
3670
3671 /*
3672 * Don't need to mark the APIC access page dirty; it is never
3673 * written to by the CPU during APIC virtualization.
3674 */
3675
3676 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
3677 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
3678 kvm_vcpu_mark_page_dirty(vcpu, gfn);
3679 }
3680
3681 if (nested_cpu_has_posted_intr(vmcs12)) {
3682 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
3683 kvm_vcpu_mark_page_dirty(vcpu, gfn);
3684 }
3685}
3686
3687static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
3688{
3689 struct vcpu_vmx *vmx = to_vmx(vcpu);
3690 int max_irr;
3691 void *vapic_page;
3692 u16 status;
3693
3694 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
3695 return;
3696
3697 vmx->nested.pi_pending = false;
3698 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
3699 return;
3700
3701 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
3702 if (max_irr != 256) {
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003703 vapic_page = vmx->nested.virtual_apic_map.hva;
3704 if (!vapic_page)
3705 return;
3706
Sean Christopherson55d23752018-12-03 13:53:18 -08003707 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
3708 vapic_page, &max_irr);
Sean Christopherson55d23752018-12-03 13:53:18 -08003709 status = vmcs_read16(GUEST_INTR_STATUS);
3710 if ((u8)max_irr > ((u8)status & 0xff)) {
3711 status &= ~0xff;
3712 status |= (u8)max_irr;
3713 vmcs_write16(GUEST_INTR_STATUS, status);
3714 }
3715 }
3716
3717 nested_mark_vmcs12_pages_dirty(vcpu);
3718}
3719
3720static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3721 unsigned long exit_qual)
3722{
3723 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3724 unsigned int nr = vcpu->arch.exception.nr;
3725 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3726
3727 if (vcpu->arch.exception.has_error_code) {
3728 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3729 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3730 }
3731
3732 if (kvm_exception_is_soft(nr))
3733 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3734 else
3735 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3736
3737 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3738 vmx_get_nmi_mask(vcpu))
3739 intr_info |= INTR_INFO_UNBLOCK_NMI;
3740
3741 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3742}
3743
Oliver Upton684c0422020-02-07 02:36:05 -08003744/*
3745 * Returns true if a debug trap is pending delivery.
3746 *
3747 * In KVM, debug traps bear an exception payload. As such, the class of a #DB
3748 * exception may be inferred from the presence of an exception payload.
3749 */
3750static inline bool vmx_pending_dbg_trap(struct kvm_vcpu *vcpu)
3751{
3752 return vcpu->arch.exception.pending &&
3753 vcpu->arch.exception.nr == DB_VECTOR &&
3754 vcpu->arch.exception.payload;
3755}
3756
3757/*
3758 * Certain VM-exits set the 'pending debug exceptions' field to indicate a
3759 * recognized #DB (data or single-step) that has yet to be delivered. Since KVM
3760 * represents these debug traps with a payload that is said to be compatible
3761 * with the 'pending debug exceptions' field, write the payload to the VMCS
3762 * field if a VM-exit is delivered before the debug trap.
3763 */
3764static void nested_vmx_update_pending_dbg(struct kvm_vcpu *vcpu)
3765{
3766 if (vmx_pending_dbg_trap(vcpu))
3767 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
3768 vcpu->arch.exception.payload);
3769}
3770
Sean Christophersond2060bd2020-04-22 19:25:39 -07003771static bool nested_vmx_preemption_timer_pending(struct kvm_vcpu *vcpu)
3772{
3773 return nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
3774 to_vmx(vcpu)->nested.preemption_timer_expired;
3775}
3776
Sean Christophersona1c77ab2020-03-02 22:27:35 -08003777static int vmx_check_nested_events(struct kvm_vcpu *vcpu)
Sean Christopherson55d23752018-12-03 13:53:18 -08003778{
3779 struct vcpu_vmx *vmx = to_vmx(vcpu);
3780 unsigned long exit_qual;
3781 bool block_nested_events =
3782 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003783 bool mtf_pending = vmx->nested.mtf_pending;
Liran Alon4b9852f2019-08-26 13:24:49 +03003784 struct kvm_lapic *apic = vcpu->arch.apic;
3785
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003786 /*
3787 * Clear the MTF state. If a higher priority VM-exit is delivered first,
3788 * this state is discarded.
3789 */
Oliver Upton5c8beb42020-04-06 20:12:37 +00003790 if (!block_nested_events)
3791 vmx->nested.mtf_pending = false;
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003792
Liran Alon4b9852f2019-08-26 13:24:49 +03003793 if (lapic_in_kernel(vcpu) &&
3794 test_bit(KVM_APIC_INIT, &apic->pending_events)) {
3795 if (block_nested_events)
3796 return -EBUSY;
Oliver Upton684c0422020-02-07 02:36:05 -08003797 nested_vmx_update_pending_dbg(vcpu);
Liran Alone64a8502019-11-11 14:16:05 +02003798 clear_bit(KVM_APIC_INIT, &apic->pending_events);
Liran Alon4b9852f2019-08-26 13:24:49 +03003799 nested_vmx_vmexit(vcpu, EXIT_REASON_INIT_SIGNAL, 0, 0);
3800 return 0;
3801 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003802
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003803 /*
3804 * Process any exceptions that are not debug traps before MTF.
3805 */
Sean Christopherson6ce347a2020-04-22 19:25:38 -07003806 if (vcpu->arch.exception.pending && !vmx_pending_dbg_trap(vcpu)) {
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003807 if (block_nested_events)
3808 return -EBUSY;
Sean Christopherson6ce347a2020-04-22 19:25:38 -07003809 if (!nested_vmx_check_exception(vcpu, &exit_qual))
3810 goto no_vmexit;
Oliver Upton5ef8acb2020-02-07 02:36:07 -08003811 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
3812 return 0;
3813 }
3814
3815 if (mtf_pending) {
3816 if (block_nested_events)
3817 return -EBUSY;
3818 nested_vmx_update_pending_dbg(vcpu);
3819 nested_vmx_vmexit(vcpu, EXIT_REASON_MONITOR_TRAP_FLAG, 0, 0);
3820 return 0;
3821 }
3822
Sean Christopherson6ce347a2020-04-22 19:25:38 -07003823 if (vcpu->arch.exception.pending) {
Sean Christopherson55d23752018-12-03 13:53:18 -08003824 if (block_nested_events)
3825 return -EBUSY;
Sean Christopherson6ce347a2020-04-22 19:25:38 -07003826 if (!nested_vmx_check_exception(vcpu, &exit_qual))
3827 goto no_vmexit;
Sean Christopherson55d23752018-12-03 13:53:18 -08003828 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
3829 return 0;
3830 }
3831
Sean Christophersond2060bd2020-04-22 19:25:39 -07003832 if (nested_vmx_preemption_timer_pending(vcpu)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08003833 if (block_nested_events)
3834 return -EBUSY;
3835 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
3836 return 0;
3837 }
3838
Sean Christopherson1cd2f0b2020-04-22 19:25:46 -07003839 if (vcpu->arch.smi_pending && !is_smm(vcpu)) {
3840 if (block_nested_events)
3841 return -EBUSY;
3842 goto no_vmexit;
3843 }
3844
Sean Christopherson15ff0b42020-04-22 19:25:45 -07003845 if (vcpu->arch.nmi_pending && !vmx_nmi_blocked(vcpu)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08003846 if (block_nested_events)
3847 return -EBUSY;
Sean Christopherson15ff0b42020-04-22 19:25:45 -07003848 if (!nested_exit_on_nmi(vcpu))
3849 goto no_vmexit;
3850
Sean Christopherson55d23752018-12-03 13:53:18 -08003851 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
3852 NMI_VECTOR | INTR_TYPE_NMI_INTR |
3853 INTR_INFO_VALID_MASK, 0);
3854 /*
3855 * The NMI-triggered VM exit counts as injection:
3856 * clear this one and block further NMIs.
3857 */
3858 vcpu->arch.nmi_pending = 0;
3859 vmx_set_nmi_mask(vcpu, true);
3860 return 0;
3861 }
3862
Sean Christopherson15ff0b42020-04-22 19:25:45 -07003863 if (kvm_cpu_has_interrupt(vcpu) && !vmx_interrupt_blocked(vcpu)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08003864 if (block_nested_events)
3865 return -EBUSY;
Sean Christopherson15ff0b42020-04-22 19:25:45 -07003866 if (!nested_exit_on_intr(vcpu))
3867 goto no_vmexit;
Sean Christopherson55d23752018-12-03 13:53:18 -08003868 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
3869 return 0;
3870 }
3871
Sean Christopherson6ce347a2020-04-22 19:25:38 -07003872no_vmexit:
Sean Christopherson55d23752018-12-03 13:53:18 -08003873 vmx_complete_nested_posted_interrupt(vcpu);
3874 return 0;
3875}
3876
3877static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
3878{
3879 ktime_t remaining =
3880 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
3881 u64 value;
3882
3883 if (ktime_to_ns(remaining) <= 0)
3884 return 0;
3885
3886 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
3887 do_div(value, 1000000);
3888 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
3889}
3890
Sean Christopherson7952d762019-05-07 08:36:29 -07003891static bool is_vmcs12_ext_field(unsigned long field)
Sean Christopherson55d23752018-12-03 13:53:18 -08003892{
Sean Christopherson7952d762019-05-07 08:36:29 -07003893 switch (field) {
3894 case GUEST_ES_SELECTOR:
3895 case GUEST_CS_SELECTOR:
3896 case GUEST_SS_SELECTOR:
3897 case GUEST_DS_SELECTOR:
3898 case GUEST_FS_SELECTOR:
3899 case GUEST_GS_SELECTOR:
3900 case GUEST_LDTR_SELECTOR:
3901 case GUEST_TR_SELECTOR:
3902 case GUEST_ES_LIMIT:
3903 case GUEST_CS_LIMIT:
3904 case GUEST_SS_LIMIT:
3905 case GUEST_DS_LIMIT:
3906 case GUEST_FS_LIMIT:
3907 case GUEST_GS_LIMIT:
3908 case GUEST_LDTR_LIMIT:
3909 case GUEST_TR_LIMIT:
3910 case GUEST_GDTR_LIMIT:
3911 case GUEST_IDTR_LIMIT:
3912 case GUEST_ES_AR_BYTES:
3913 case GUEST_DS_AR_BYTES:
3914 case GUEST_FS_AR_BYTES:
3915 case GUEST_GS_AR_BYTES:
3916 case GUEST_LDTR_AR_BYTES:
3917 case GUEST_TR_AR_BYTES:
3918 case GUEST_ES_BASE:
3919 case GUEST_CS_BASE:
3920 case GUEST_SS_BASE:
3921 case GUEST_DS_BASE:
3922 case GUEST_FS_BASE:
3923 case GUEST_GS_BASE:
3924 case GUEST_LDTR_BASE:
3925 case GUEST_TR_BASE:
3926 case GUEST_GDTR_BASE:
3927 case GUEST_IDTR_BASE:
3928 case GUEST_PENDING_DBG_EXCEPTIONS:
3929 case GUEST_BNDCFGS:
3930 return true;
3931 default:
3932 break;
3933 }
Sean Christopherson55d23752018-12-03 13:53:18 -08003934
Sean Christopherson7952d762019-05-07 08:36:29 -07003935 return false;
3936}
3937
3938static void sync_vmcs02_to_vmcs12_rare(struct kvm_vcpu *vcpu,
3939 struct vmcs12 *vmcs12)
3940{
3941 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08003942
3943 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
3944 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
3945 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
3946 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
3947 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
3948 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
3949 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
3950 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
3951 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
3952 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
3953 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
3954 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
3955 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
3956 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
3957 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
3958 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
3959 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
3960 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
3961 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
Sean Christopherson55d23752018-12-03 13:53:18 -08003962 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
3963 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
3964 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
3965 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
3966 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
3967 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
3968 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
3969 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
3970 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
3971 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
3972 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
3973 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
3974 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
3975 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
3976 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
Sean Christopherson7952d762019-05-07 08:36:29 -07003977 vmcs12->guest_pending_dbg_exceptions =
3978 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
3979 if (kvm_mpx_supported())
3980 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
3981
3982 vmx->nested.need_sync_vmcs02_to_vmcs12_rare = false;
3983}
3984
3985static void copy_vmcs02_to_vmcs12_rare(struct kvm_vcpu *vcpu,
3986 struct vmcs12 *vmcs12)
3987{
3988 struct vcpu_vmx *vmx = to_vmx(vcpu);
3989 int cpu;
3990
3991 if (!vmx->nested.need_sync_vmcs02_to_vmcs12_rare)
3992 return;
3993
3994
3995 WARN_ON_ONCE(vmx->loaded_vmcs != &vmx->vmcs01);
3996
3997 cpu = get_cpu();
3998 vmx->loaded_vmcs = &vmx->nested.vmcs02;
Sean Christopherson1af1bb02020-05-06 16:58:50 -07003999 vmx_vcpu_load_vmcs(vcpu, cpu, &vmx->vmcs01);
Sean Christopherson7952d762019-05-07 08:36:29 -07004000
4001 sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
4002
4003 vmx->loaded_vmcs = &vmx->vmcs01;
Sean Christopherson1af1bb02020-05-06 16:58:50 -07004004 vmx_vcpu_load_vmcs(vcpu, cpu, &vmx->nested.vmcs02);
Sean Christopherson7952d762019-05-07 08:36:29 -07004005 put_cpu();
4006}
4007
4008/*
4009 * Update the guest state fields of vmcs12 to reflect changes that
4010 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
4011 * VM-entry controls is also updated, since this is really a guest
4012 * state bit.)
4013 */
4014static void sync_vmcs02_to_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
4015{
4016 struct vcpu_vmx *vmx = to_vmx(vcpu);
4017
4018 if (vmx->nested.hv_evmcs)
4019 sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
4020
4021 vmx->nested.need_sync_vmcs02_to_vmcs12_rare = !vmx->nested.hv_evmcs;
4022
4023 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
4024 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
4025
4026 vmcs12->guest_rsp = kvm_rsp_read(vcpu);
4027 vmcs12->guest_rip = kvm_rip_read(vcpu);
4028 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
4029
4030 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
4031 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
Sean Christopherson55d23752018-12-03 13:53:18 -08004032
4033 vmcs12->guest_interruptibility_info =
4034 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
Sean Christopherson7952d762019-05-07 08:36:29 -07004035
Sean Christopherson55d23752018-12-03 13:53:18 -08004036 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
4037 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
4038 else
4039 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
4040
Paolo Bonzinib4b65b52019-01-29 19:12:35 +01004041 if (nested_cpu_has_preemption_timer(vmcs12) &&
Peter Shier850448f2020-05-26 14:51:06 -07004042 vmcs12->vm_exit_controls & VM_EXIT_SAVE_VMX_PREEMPTION_TIMER &&
4043 !vmx->nested.nested_run_pending)
4044 vmcs12->vmx_preemption_timer_value =
4045 vmx_get_preemption_timer_value(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08004046
4047 /*
4048 * In some cases (usually, nested EPT), L2 is allowed to change its
4049 * own CR3 without exiting. If it has changed it, we must keep it.
4050 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
4051 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
4052 *
4053 * Additionally, restore L2's PDPTR to vmcs12.
4054 */
4055 if (enable_ept) {
4056 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Sean Christophersonc7554efc2019-05-07 09:06:40 -07004057 if (nested_cpu_has_ept(vmcs12) && is_pae_paging(vcpu)) {
4058 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
4059 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
4060 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
4061 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
4062 }
Sean Christopherson55d23752018-12-03 13:53:18 -08004063 }
4064
4065 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
4066
4067 if (nested_cpu_has_vid(vmcs12))
4068 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
4069
4070 vmcs12->vm_entry_controls =
4071 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
4072 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
4073
Sean Christopherson699a1ac2019-05-07 09:06:37 -07004074 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS)
Sean Christopherson55d23752018-12-03 13:53:18 -08004075 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
Sean Christopherson55d23752018-12-03 13:53:18 -08004076
Sean Christopherson55d23752018-12-03 13:53:18 -08004077 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
4078 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Sean Christopherson55d23752018-12-03 13:53:18 -08004079}
4080
4081/*
4082 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
4083 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
4084 * and this function updates it to reflect the changes to the guest state while
4085 * L2 was running (and perhaps made some exits which were handled directly by L0
4086 * without going back to L1), and to reflect the exit reason.
4087 * Note that we do not have to copy here all VMCS fields, just those that
4088 * could have changed by the L2 guest or the exit - i.e., the guest-state and
4089 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
4090 * which already writes to vmcs12 directly.
4091 */
4092static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004093 u32 vm_exit_reason, u32 exit_intr_info,
Sean Christopherson55d23752018-12-03 13:53:18 -08004094 unsigned long exit_qualification)
4095{
Sean Christopherson55d23752018-12-03 13:53:18 -08004096 /* update exit information fields: */
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004097 vmcs12->vm_exit_reason = vm_exit_reason;
Sean Christopherson55d23752018-12-03 13:53:18 -08004098 vmcs12->exit_qualification = exit_qualification;
4099 vmcs12->vm_exit_intr_info = exit_intr_info;
4100
4101 vmcs12->idt_vectoring_info_field = 0;
4102 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
4103 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
4104
4105 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
4106 vmcs12->launch_state = 1;
4107
4108 /* vm_entry_intr_info_field is cleared on exit. Emulate this
4109 * instead of reading the real value. */
4110 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
4111
4112 /*
4113 * Transfer the event that L0 or L1 may wanted to inject into
4114 * L2 to IDT_VECTORING_INFO_FIELD.
4115 */
4116 vmcs12_save_pending_event(vcpu, vmcs12);
Krish Sadhukhana0d4f802018-12-04 19:00:13 -05004117
4118 /*
4119 * According to spec, there's no need to store the guest's
4120 * MSRs if the exit is due to a VM-entry failure that occurs
4121 * during or after loading the guest state. Since this exit
4122 * does not fall in that category, we need to save the MSRs.
4123 */
4124 if (nested_vmx_store_msr(vcpu,
4125 vmcs12->vm_exit_msr_store_addr,
4126 vmcs12->vm_exit_msr_store_count))
4127 nested_vmx_abort(vcpu,
4128 VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Sean Christopherson55d23752018-12-03 13:53:18 -08004129 }
4130
4131 /*
4132 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
4133 * preserved above and would only end up incorrectly in L1.
4134 */
4135 vcpu->arch.nmi_injected = false;
4136 kvm_clear_exception_queue(vcpu);
4137 kvm_clear_interrupt_queue(vcpu);
4138}
4139
4140/*
4141 * A part of what we need to when the nested L2 guest exits and we want to
4142 * run its L1 parent, is to reset L1's guest state to the host state specified
4143 * in vmcs12.
4144 * This function is to be called not only on normal nested exit, but also on
4145 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
4146 * Failures During or After Loading Guest State").
4147 * This function should be called when the active VMCS is L1's (vmcs01).
4148 */
4149static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
4150 struct vmcs12 *vmcs12)
4151{
Sean Christopherson68cda402020-05-11 15:05:29 -07004152 enum vm_entry_failure_code ignored;
Sean Christopherson55d23752018-12-03 13:53:18 -08004153 struct kvm_segment seg;
Sean Christopherson55d23752018-12-03 13:53:18 -08004154
4155 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
4156 vcpu->arch.efer = vmcs12->host_ia32_efer;
4157 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
4158 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
4159 else
4160 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
4161 vmx_set_efer(vcpu, vcpu->arch.efer);
4162
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02004163 kvm_rsp_write(vcpu, vmcs12->host_rsp);
4164 kvm_rip_write(vcpu, vmcs12->host_rip);
Sean Christopherson55d23752018-12-03 13:53:18 -08004165 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
4166 vmx_set_interrupt_shadow(vcpu, 0);
4167
4168 /*
4169 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
4170 * actually changed, because vmx_set_cr0 refers to efer set above.
4171 *
4172 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
4173 * (KVM doesn't change it);
4174 */
Sean Christophersonfa71e952020-07-02 21:04:22 -07004175 vcpu->arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS;
Sean Christopherson55d23752018-12-03 13:53:18 -08004176 vmx_set_cr0(vcpu, vmcs12->host_cr0);
4177
4178 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
4179 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
4180 vmx_set_cr4(vcpu, vmcs12->host_cr4);
4181
4182 nested_ept_uninit_mmu_context(vcpu);
4183
4184 /*
4185 * Only PDPTE load can fail as the value of cr3 was checked on entry and
4186 * couldn't have changed.
4187 */
Sean Christopherson68cda402020-05-11 15:05:29 -07004188 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &ignored))
Sean Christopherson55d23752018-12-03 13:53:18 -08004189 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
4190
4191 if (!enable_ept)
4192 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
4193
Sean Christopherson50b265a2020-03-20 14:28:19 -07004194 nested_vmx_transition_tlb_flush(vcpu, vmcs12, false);
Sean Christopherson55d23752018-12-03 13:53:18 -08004195
4196 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
4197 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
4198 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
4199 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
4200 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
4201 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
4202 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
4203
4204 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
4205 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
4206 vmcs_write64(GUEST_BNDCFGS, 0);
4207
4208 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
4209 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
4210 vcpu->arch.pat = vmcs12->host_ia32_pat;
4211 }
4212 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Oliver Uptond1968422019-12-13 16:33:58 -08004213 WARN_ON_ONCE(kvm_set_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL,
4214 vmcs12->host_ia32_perf_global_ctrl));
Sean Christopherson55d23752018-12-03 13:53:18 -08004215
4216 /* Set L1 segment info according to Intel SDM
4217 27.5.2 Loading Host Segment and Descriptor-Table Registers */
4218 seg = (struct kvm_segment) {
4219 .base = 0,
4220 .limit = 0xFFFFFFFF,
4221 .selector = vmcs12->host_cs_selector,
4222 .type = 11,
4223 .present = 1,
4224 .s = 1,
4225 .g = 1
4226 };
4227 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
4228 seg.l = 1;
4229 else
4230 seg.db = 1;
4231 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
4232 seg = (struct kvm_segment) {
4233 .base = 0,
4234 .limit = 0xFFFFFFFF,
4235 .type = 3,
4236 .present = 1,
4237 .s = 1,
4238 .db = 1,
4239 .g = 1
4240 };
4241 seg.selector = vmcs12->host_ds_selector;
4242 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
4243 seg.selector = vmcs12->host_es_selector;
4244 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
4245 seg.selector = vmcs12->host_ss_selector;
4246 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
4247 seg.selector = vmcs12->host_fs_selector;
4248 seg.base = vmcs12->host_fs_base;
4249 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
4250 seg.selector = vmcs12->host_gs_selector;
4251 seg.base = vmcs12->host_gs_base;
4252 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
4253 seg = (struct kvm_segment) {
4254 .base = vmcs12->host_tr_base,
4255 .limit = 0x67,
4256 .selector = vmcs12->host_tr_selector,
4257 .type = 11,
4258 .present = 1
4259 };
4260 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
4261
4262 kvm_set_dr(vcpu, 7, 0x400);
4263 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
4264
4265 if (cpu_has_vmx_msr_bitmap())
4266 vmx_update_msr_bitmap(vcpu);
4267
4268 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
4269 vmcs12->vm_exit_msr_load_count))
4270 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
4271}
4272
4273static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx)
4274{
4275 struct shared_msr_entry *efer_msr;
4276 unsigned int i;
4277
4278 if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER)
4279 return vmcs_read64(GUEST_IA32_EFER);
4280
4281 if (cpu_has_load_ia32_efer())
4282 return host_efer;
4283
4284 for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) {
4285 if (vmx->msr_autoload.guest.val[i].index == MSR_EFER)
4286 return vmx->msr_autoload.guest.val[i].value;
4287 }
4288
4289 efer_msr = find_msr_entry(vmx, MSR_EFER);
4290 if (efer_msr)
4291 return efer_msr->data;
4292
4293 return host_efer;
4294}
4295
4296static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu)
4297{
4298 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4299 struct vcpu_vmx *vmx = to_vmx(vcpu);
4300 struct vmx_msr_entry g, h;
Sean Christopherson55d23752018-12-03 13:53:18 -08004301 gpa_t gpa;
4302 u32 i, j;
4303
4304 vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT);
4305
4306 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
4307 /*
4308 * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set
4309 * as vmcs01.GUEST_DR7 contains a userspace defined value
4310 * and vcpu->arch.dr7 is not squirreled away before the
4311 * nested VMENTER (not worth adding a variable in nested_vmx).
4312 */
4313 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
4314 kvm_set_dr(vcpu, 7, DR7_FIXED_1);
4315 else
4316 WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7)));
4317 }
4318
4319 /*
4320 * Note that calling vmx_set_{efer,cr0,cr4} is important as they
4321 * handle a variety of side effects to KVM's software model.
4322 */
4323 vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx));
4324
Sean Christophersonfa71e952020-07-02 21:04:22 -07004325 vcpu->arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS;
Sean Christopherson55d23752018-12-03 13:53:18 -08004326 vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW));
4327
4328 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
4329 vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW));
4330
4331 nested_ept_uninit_mmu_context(vcpu);
Sean Christophersonf087a022019-06-07 11:55:34 -07004332 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07004333 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Sean Christopherson55d23752018-12-03 13:53:18 -08004334
4335 /*
4336 * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs
4337 * from vmcs01 (if necessary). The PDPTRs are not loaded on
4338 * VMFail, like everything else we just need to ensure our
4339 * software model is up-to-date.
4340 */
Sean Christopherson9932b492020-04-15 13:34:50 -07004341 if (enable_ept && is_pae_paging(vcpu))
Sean Christophersonf087a022019-06-07 11:55:34 -07004342 ept_save_pdptrs(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08004343
4344 kvm_mmu_reset_context(vcpu);
4345
4346 if (cpu_has_vmx_msr_bitmap())
4347 vmx_update_msr_bitmap(vcpu);
4348
4349 /*
4350 * This nasty bit of open coding is a compromise between blindly
4351 * loading L1's MSRs using the exit load lists (incorrect emulation
4352 * of VMFail), leaving the nested VM's MSRs in the software model
4353 * (incorrect behavior) and snapshotting the modified MSRs (too
4354 * expensive since the lists are unbound by hardware). For each
4355 * MSR that was (prematurely) loaded from the nested VMEntry load
4356 * list, reload it from the exit load list if it exists and differs
4357 * from the guest value. The intent is to stuff host state as
4358 * silently as possible, not to fully process the exit load list.
4359 */
Sean Christopherson55d23752018-12-03 13:53:18 -08004360 for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) {
4361 gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g));
4362 if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) {
4363 pr_debug_ratelimited(
4364 "%s read MSR index failed (%u, 0x%08llx)\n",
4365 __func__, i, gpa);
4366 goto vmabort;
4367 }
4368
4369 for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) {
4370 gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h));
4371 if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) {
4372 pr_debug_ratelimited(
4373 "%s read MSR failed (%u, 0x%08llx)\n",
4374 __func__, j, gpa);
4375 goto vmabort;
4376 }
4377 if (h.index != g.index)
4378 continue;
4379 if (h.value == g.value)
4380 break;
4381
4382 if (nested_vmx_load_msr_check(vcpu, &h)) {
4383 pr_debug_ratelimited(
4384 "%s check failed (%u, 0x%x, 0x%x)\n",
4385 __func__, j, h.index, h.reserved);
4386 goto vmabort;
4387 }
4388
Sean Christophersonf20935d2019-09-05 14:22:54 -07004389 if (kvm_set_msr(vcpu, h.index, h.value)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08004390 pr_debug_ratelimited(
4391 "%s WRMSR failed (%u, 0x%x, 0x%llx)\n",
4392 __func__, j, h.index, h.value);
4393 goto vmabort;
4394 }
4395 }
4396 }
4397
4398 return;
4399
4400vmabort:
4401 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
4402}
4403
4404/*
4405 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
4406 * and modify vmcs12 to make it see what it would expect to see there if
4407 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
4408 */
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004409void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason,
Sean Christopherson55d23752018-12-03 13:53:18 -08004410 u32 exit_intr_info, unsigned long exit_qualification)
4411{
4412 struct vcpu_vmx *vmx = to_vmx(vcpu);
4413 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4414
4415 /* trying to cancel vmlaunch/vmresume is a bug */
4416 WARN_ON_ONCE(vmx->nested.nested_run_pending);
4417
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07004418 /* Service the TLB flush request for L2 before switching to L1. */
4419 if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu))
4420 kvm_vcpu_flush_tlb_current(vcpu);
4421
Peter Shier43fea4e2020-08-20 16:05:45 -07004422 /*
4423 * VCPU_EXREG_PDPTR will be clobbered in arch/x86/kvm/vmx/vmx.h between
4424 * now and the new vmentry. Ensure that the VMCS02 PDPTR fields are
4425 * up-to-date before switching to L1.
4426 */
4427 if (enable_ept && is_pae_paging(vcpu))
4428 vmx_ept_load_pdptrs(vcpu);
4429
Sean Christopherson55d23752018-12-03 13:53:18 -08004430 leave_guest_mode(vcpu);
4431
Paolo Bonzinib4b65b52019-01-29 19:12:35 +01004432 if (nested_cpu_has_preemption_timer(vmcs12))
4433 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
4434
Xiaoyao Li5e3d3942019-12-06 16:45:26 +08004435 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING)
Sean Christopherson55d23752018-12-03 13:53:18 -08004436 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
4437
4438 if (likely(!vmx->fail)) {
Sean Christopherson3731905ef2019-05-07 08:36:27 -07004439 sync_vmcs02_to_vmcs12(vcpu, vmcs12);
Sean Christophersonf4f83162019-05-07 08:36:26 -07004440
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004441 if (vm_exit_reason != -1)
4442 prepare_vmcs12(vcpu, vmcs12, vm_exit_reason,
4443 exit_intr_info, exit_qualification);
Sean Christopherson55d23752018-12-03 13:53:18 -08004444
4445 /*
Sean Christopherson3731905ef2019-05-07 08:36:27 -07004446 * Must happen outside of sync_vmcs02_to_vmcs12() as it will
Sean Christopherson55d23752018-12-03 13:53:18 -08004447 * also be used to capture vmcs12 cache as part of
4448 * capturing nVMX state for snapshot (migration).
4449 *
4450 * Otherwise, this flush will dirty guest memory at a
4451 * point it is already assumed by user-space to be
4452 * immutable.
4453 */
4454 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08004455 } else {
4456 /*
4457 * The only expected VM-instruction error is "VM entry with
4458 * invalid control field(s)." Anything else indicates a
4459 * problem with L0. And we should never get here with a
4460 * VMFail of any type if early consistency checks are enabled.
4461 */
4462 WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) !=
4463 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
4464 WARN_ON_ONCE(nested_early_check);
4465 }
4466
4467 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
4468
4469 /* Update any VMCS fields that might have changed while L2 ran */
4470 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
4471 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
4472 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Liran Alon02d496cf2019-11-11 14:30:55 +02004473 if (vmx->nested.l1_tpr_threshold != -1)
4474 vmcs_write32(TPR_THRESHOLD, vmx->nested.l1_tpr_threshold);
Sean Christopherson55d23752018-12-03 13:53:18 -08004475
4476 if (kvm_has_tsc_control)
4477 decache_tsc_multiplier(vmx);
4478
4479 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
4480 vmx->nested.change_vmcs01_virtual_apic_mode = false;
4481 vmx_set_virtual_apic_mode(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08004482 }
4483
Sean Christopherson55d23752018-12-03 13:53:18 -08004484 /* Unpin physical memory we referred to in vmcs02 */
4485 if (vmx->nested.apic_access_page) {
Liran Alonb11494b2019-11-21 00:31:47 +02004486 kvm_release_page_clean(vmx->nested.apic_access_page);
Sean Christopherson55d23752018-12-03 13:53:18 -08004487 vmx->nested.apic_access_page = NULL;
4488 }
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01004489 kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map, true);
KarimAllah Ahmed3278e042019-01-31 21:24:38 +01004490 kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map, true);
4491 vmx->nested.pi_desc = NULL;
Sean Christopherson55d23752018-12-03 13:53:18 -08004492
Sean Christopherson1196cb92020-03-20 14:28:23 -07004493 if (vmx->nested.reload_vmcs01_apic_access_page) {
4494 vmx->nested.reload_vmcs01_apic_access_page = false;
4495 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
4496 }
Sean Christopherson55d23752018-12-03 13:53:18 -08004497
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004498 if ((vm_exit_reason != -1) &&
4499 (enable_shadow_vmcs || vmx->nested.hv_evmcs))
Sean Christopherson3731905ef2019-05-07 08:36:27 -07004500 vmx->nested.need_vmcs12_to_shadow_sync = true;
Sean Christopherson55d23752018-12-03 13:53:18 -08004501
4502 /* in case we halted in L2 */
4503 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
4504
4505 if (likely(!vmx->fail)) {
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004506 if ((u16)vm_exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
Sean Christophersona1c77ab2020-03-02 22:27:35 -08004507 nested_exit_intr_ack_set(vcpu)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08004508 int irq = kvm_cpu_get_interrupt(vcpu);
4509 WARN_ON(irq < 0);
4510 vmcs12->vm_exit_intr_info = irq |
4511 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
4512 }
4513
Sean Christopherson4dcefa32020-04-15 10:55:18 -07004514 if (vm_exit_reason != -1)
Sean Christopherson55d23752018-12-03 13:53:18 -08004515 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
4516 vmcs12->exit_qualification,
4517 vmcs12->idt_vectoring_info_field,
4518 vmcs12->vm_exit_intr_info,
4519 vmcs12->vm_exit_intr_error_code,
4520 KVM_ISA_VMX);
4521
4522 load_vmcs12_host_state(vcpu, vmcs12);
4523
4524 return;
4525 }
4526
4527 /*
4528 * After an early L2 VM-entry failure, we're now back
4529 * in L1 which thinks it just finished a VMLAUNCH or
4530 * VMRESUME instruction, so we need to set the failure
4531 * flag and the VM-instruction error field of the VMCS
4532 * accordingly, and skip the emulated instruction.
4533 */
Sean Christophersonb2656e42020-06-08 18:56:07 -07004534 (void)nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Sean Christopherson55d23752018-12-03 13:53:18 -08004535
4536 /*
4537 * Restore L1's host state to KVM's software model. We're here
4538 * because a consistency check was caught by hardware, which
4539 * means some amount of guest state has been propagated to KVM's
4540 * model and needs to be unwound to the host's state.
4541 */
4542 nested_vmx_restore_host_state(vcpu);
4543
4544 vmx->fail = 0;
4545}
4546
4547/*
4548 * Decode the memory-address operand of a vmx instruction, as recorded on an
4549 * exit caused by such an instruction (run by a guest hypervisor).
4550 * On success, returns 0. When the operand is invalid, returns 1 and throws
Miaohe Lin49f933d2020-02-27 11:20:54 +08004551 * #UD, #GP, or #SS.
Sean Christopherson55d23752018-12-03 13:53:18 -08004552 */
4553int get_vmx_mem_address(struct kvm_vcpu *vcpu, unsigned long exit_qualification,
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004554 u32 vmx_instruction_info, bool wr, int len, gva_t *ret)
Sean Christopherson55d23752018-12-03 13:53:18 -08004555{
4556 gva_t off;
4557 bool exn;
4558 struct kvm_segment s;
4559
4560 /*
4561 * According to Vol. 3B, "Information for VM Exits Due to Instruction
4562 * Execution", on an exit, vmx_instruction_info holds most of the
4563 * addressing components of the operand. Only the displacement part
4564 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
4565 * For how an actual address is calculated from all these components,
4566 * refer to Vol. 1, "Operand Addressing".
4567 */
4568 int scaling = vmx_instruction_info & 3;
4569 int addr_size = (vmx_instruction_info >> 7) & 7;
4570 bool is_reg = vmx_instruction_info & (1u << 10);
4571 int seg_reg = (vmx_instruction_info >> 15) & 7;
4572 int index_reg = (vmx_instruction_info >> 18) & 0xf;
4573 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
4574 int base_reg = (vmx_instruction_info >> 23) & 0xf;
4575 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
4576
4577 if (is_reg) {
4578 kvm_queue_exception(vcpu, UD_VECTOR);
4579 return 1;
4580 }
4581
4582 /* Addr = segment_base + offset */
4583 /* offset = base + [index * scale] + displacement */
4584 off = exit_qualification; /* holds the displacement */
Sean Christopherson946c5222019-01-23 14:39:23 -08004585 if (addr_size == 1)
4586 off = (gva_t)sign_extend64(off, 31);
4587 else if (addr_size == 0)
4588 off = (gva_t)sign_extend64(off, 15);
Sean Christopherson55d23752018-12-03 13:53:18 -08004589 if (base_is_valid)
4590 off += kvm_register_read(vcpu, base_reg);
4591 if (index_is_valid)
Miaohe Line6302692020-02-15 10:44:22 +08004592 off += kvm_register_read(vcpu, index_reg) << scaling;
Sean Christopherson55d23752018-12-03 13:53:18 -08004593 vmx_get_segment(vcpu, &s, seg_reg);
Sean Christopherson55d23752018-12-03 13:53:18 -08004594
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004595 /*
4596 * The effective address, i.e. @off, of a memory operand is truncated
4597 * based on the address size of the instruction. Note that this is
4598 * the *effective address*, i.e. the address prior to accounting for
4599 * the segment's base.
4600 */
Sean Christopherson55d23752018-12-03 13:53:18 -08004601 if (addr_size == 1) /* 32 bit */
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004602 off &= 0xffffffff;
4603 else if (addr_size == 0) /* 16 bit */
4604 off &= 0xffff;
Sean Christopherson55d23752018-12-03 13:53:18 -08004605
4606 /* Checks for #GP/#SS exceptions. */
4607 exn = false;
4608 if (is_long_mode(vcpu)) {
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004609 /*
4610 * The virtual/linear address is never truncated in 64-bit
4611 * mode, e.g. a 32-bit address size can yield a 64-bit virtual
4612 * address when using FS/GS with a non-zero base.
4613 */
Liran Alon6694e482019-07-15 18:47:44 +03004614 if (seg_reg == VCPU_SREG_FS || seg_reg == VCPU_SREG_GS)
4615 *ret = s.base + off;
4616 else
4617 *ret = off;
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004618
Sean Christopherson55d23752018-12-03 13:53:18 -08004619 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
4620 * non-canonical form. This is the only check on the memory
4621 * destination for long mode!
4622 */
4623 exn = is_noncanonical_address(*ret, vcpu);
Paolo Bonzinie0dfacb2019-01-30 17:25:38 +01004624 } else {
Sean Christopherson8570f9e2019-01-23 14:39:24 -08004625 /*
4626 * When not in long mode, the virtual/linear address is
4627 * unconditionally truncated to 32 bits regardless of the
4628 * address size.
4629 */
4630 *ret = (s.base + off) & 0xffffffff;
4631
Sean Christopherson55d23752018-12-03 13:53:18 -08004632 /* Protected mode: apply checks for segment validity in the
4633 * following order:
4634 * - segment type check (#GP(0) may be thrown)
4635 * - usability check (#GP(0)/#SS(0))
4636 * - limit check (#GP(0)/#SS(0))
4637 */
4638 if (wr)
4639 /* #GP(0) if the destination operand is located in a
4640 * read-only data segment or any code segment.
4641 */
4642 exn = ((s.type & 0xa) == 0 || (s.type & 8));
4643 else
4644 /* #GP(0) if the source operand is located in an
4645 * execute-only code segment
4646 */
4647 exn = ((s.type & 0xa) == 8);
4648 if (exn) {
4649 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
4650 return 1;
4651 }
4652 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
4653 */
4654 exn = (s.unusable != 0);
Sean Christopherson34333cc2019-01-23 14:39:25 -08004655
4656 /*
4657 * Protected mode: #GP(0)/#SS(0) if the memory operand is
4658 * outside the segment limit. All CPUs that support VMX ignore
4659 * limit checks for flat segments, i.e. segments with base==0,
4660 * limit==0xffffffff and of type expand-up data or code.
Sean Christopherson55d23752018-12-03 13:53:18 -08004661 */
Sean Christopherson34333cc2019-01-23 14:39:25 -08004662 if (!(s.base == 0 && s.limit == 0xffffffff &&
4663 ((s.type & 8) || !(s.type & 4))))
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004664 exn = exn || ((u64)off + len - 1 > s.limit);
Sean Christopherson55d23752018-12-03 13:53:18 -08004665 }
4666 if (exn) {
4667 kvm_queue_exception_e(vcpu,
4668 seg_reg == VCPU_SREG_SS ?
4669 SS_VECTOR : GP_VECTOR,
4670 0);
4671 return 1;
4672 }
4673
4674 return 0;
4675}
4676
Oliver Upton03a8871a2019-11-13 16:17:20 -08004677void nested_vmx_pmu_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
4678{
4679 struct vcpu_vmx *vmx;
4680
4681 if (!nested_vmx_allowed(vcpu))
4682 return;
4683
4684 vmx = to_vmx(vcpu);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004685 if (kvm_x86_ops.pmu_ops->is_valid_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL)) {
Oliver Upton03a8871a2019-11-13 16:17:20 -08004686 vmx->nested.msrs.entry_ctls_high |=
4687 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
4688 vmx->nested.msrs.exit_ctls_high |=
4689 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
4690 } else {
4691 vmx->nested.msrs.entry_ctls_high &=
4692 ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
4693 vmx->nested.msrs.exit_ctls_high &=
Chenyi Qiangc6b177a2020-08-28 16:56:21 +08004694 ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
Oliver Upton03a8871a2019-11-13 16:17:20 -08004695 }
4696}
4697
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004698static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer,
4699 int *ret)
Sean Christopherson55d23752018-12-03 13:53:18 -08004700{
4701 gva_t gva;
4702 struct x86_exception e;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004703 int r;
Sean Christopherson55d23752018-12-03 13:53:18 -08004704
Sean Christopherson5addc232020-04-15 13:34:53 -07004705 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03004706 vmcs_read32(VMX_INSTRUCTION_INFO), false,
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004707 sizeof(*vmpointer), &gva)) {
4708 *ret = 1;
4709 return -EINVAL;
4710 }
Sean Christopherson55d23752018-12-03 13:53:18 -08004711
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004712 r = kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e);
4713 if (r != X86EMUL_CONTINUE) {
Babu Moger3f3393b2020-09-11 14:29:05 -05004714 *ret = kvm_handle_memory_failure(vcpu, r, &e);
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004715 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08004716 }
4717
4718 return 0;
4719}
4720
4721/*
4722 * Allocate a shadow VMCS and associate it with the currently loaded
4723 * VMCS, unless such a shadow VMCS already exists. The newly allocated
4724 * VMCS is also VMCLEARed, so that it is ready for use.
4725 */
4726static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
4727{
4728 struct vcpu_vmx *vmx = to_vmx(vcpu);
4729 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
4730
4731 /*
4732 * We should allocate a shadow vmcs for vmcs01 only when L1
4733 * executes VMXON and free it when L1 executes VMXOFF.
4734 * As it is invalid to execute VMXON twice, we shouldn't reach
4735 * here when vmcs01 already have an allocated shadow vmcs.
4736 */
4737 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
4738
4739 if (!loaded_vmcs->shadow_vmcs) {
4740 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
4741 if (loaded_vmcs->shadow_vmcs)
4742 vmcs_clear(loaded_vmcs->shadow_vmcs);
4743 }
4744 return loaded_vmcs->shadow_vmcs;
4745}
4746
4747static int enter_vmx_operation(struct kvm_vcpu *vcpu)
4748{
4749 struct vcpu_vmx *vmx = to_vmx(vcpu);
4750 int r;
4751
4752 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
4753 if (r < 0)
4754 goto out_vmcs02;
4755
Ben Gardon41836832019-02-11 11:02:52 -08004756 vmx->nested.cached_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT);
Sean Christopherson55d23752018-12-03 13:53:18 -08004757 if (!vmx->nested.cached_vmcs12)
4758 goto out_cached_vmcs12;
4759
Ben Gardon41836832019-02-11 11:02:52 -08004760 vmx->nested.cached_shadow_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT);
Sean Christopherson55d23752018-12-03 13:53:18 -08004761 if (!vmx->nested.cached_shadow_vmcs12)
4762 goto out_cached_shadow_vmcs12;
4763
4764 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
4765 goto out_shadow_vmcs;
4766
4767 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
Jim Mattsonada00982020-05-08 13:36:42 -07004768 HRTIMER_MODE_ABS_PINNED);
Sean Christopherson55d23752018-12-03 13:53:18 -08004769 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
4770
4771 vmx->nested.vpid02 = allocate_vpid();
4772
4773 vmx->nested.vmcs02_initialized = false;
4774 vmx->nested.vmxon = true;
Luwei Kangee85dec2018-10-24 16:05:16 +08004775
Sean Christopherson2ef76192020-03-02 15:56:22 -08004776 if (vmx_pt_mode_is_host_guest()) {
Luwei Kangee85dec2018-10-24 16:05:16 +08004777 vmx->pt_desc.guest.ctl = 0;
4778 pt_update_intercept_for_msr(vmx);
4779 }
4780
Sean Christopherson55d23752018-12-03 13:53:18 -08004781 return 0;
4782
4783out_shadow_vmcs:
4784 kfree(vmx->nested.cached_shadow_vmcs12);
4785
4786out_cached_shadow_vmcs12:
4787 kfree(vmx->nested.cached_vmcs12);
4788
4789out_cached_vmcs12:
4790 free_loaded_vmcs(&vmx->nested.vmcs02);
4791
4792out_vmcs02:
4793 return -ENOMEM;
4794}
4795
4796/*
4797 * Emulate the VMXON instruction.
4798 * Currently, we just remember that VMX is active, and do not save or even
4799 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
4800 * do not currently need to store anything in that guest-allocated memory
4801 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
4802 * argument is different from the VMXON pointer (which the spec says they do).
4803 */
4804static int handle_vmon(struct kvm_vcpu *vcpu)
4805{
4806 int ret;
4807 gpa_t vmptr;
KarimAllah Ahmed2e408932019-01-31 21:24:31 +01004808 uint32_t revision;
Sean Christopherson55d23752018-12-03 13:53:18 -08004809 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson32ad73d2019-12-20 20:44:55 -08004810 const u64 VMXON_NEEDED_FEATURES = FEAT_CTL_LOCKED
4811 | FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX;
Sean Christopherson55d23752018-12-03 13:53:18 -08004812
4813 /*
4814 * The Intel VMX Instruction Reference lists a bunch of bits that are
4815 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
4816 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
4817 * Otherwise, we should fail with #UD. But most faulting conditions
4818 * have already been checked by hardware, prior to the VM-exit for
4819 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
4820 * that bit set to 1 in non-root mode.
4821 */
4822 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
4823 kvm_queue_exception(vcpu, UD_VECTOR);
4824 return 1;
4825 }
4826
4827 /* CPL=0 must be checked manually. */
4828 if (vmx_get_cpl(vcpu)) {
4829 kvm_inject_gp(vcpu, 0);
4830 return 1;
4831 }
4832
4833 if (vmx->nested.vmxon)
Sean Christophersonb2656e42020-06-08 18:56:07 -07004834 return nested_vmx_fail(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Sean Christopherson55d23752018-12-03 13:53:18 -08004835
4836 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
4837 != VMXON_NEEDED_FEATURES) {
4838 kvm_inject_gp(vcpu, 0);
4839 return 1;
4840 }
4841
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004842 if (nested_vmx_get_vmptr(vcpu, &vmptr, &ret))
4843 return ret;
Sean Christopherson55d23752018-12-03 13:53:18 -08004844
4845 /*
4846 * SDM 3: 24.11.5
4847 * The first 4 bytes of VMXON region contain the supported
4848 * VMCS revision identifier
4849 *
4850 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
4851 * which replaces physical address width with 32
4852 */
KarimAllah Ahmede0bf2662019-01-31 21:24:43 +01004853 if (!page_address_valid(vcpu, vmptr))
Sean Christopherson55d23752018-12-03 13:53:18 -08004854 return nested_vmx_failInvalid(vcpu);
4855
KarimAllah Ahmed2e408932019-01-31 21:24:31 +01004856 if (kvm_read_guest(vcpu->kvm, vmptr, &revision, sizeof(revision)) ||
4857 revision != VMCS12_REVISION)
Sean Christopherson55d23752018-12-03 13:53:18 -08004858 return nested_vmx_failInvalid(vcpu);
4859
Sean Christopherson55d23752018-12-03 13:53:18 -08004860 vmx->nested.vmxon_ptr = vmptr;
4861 ret = enter_vmx_operation(vcpu);
4862 if (ret)
4863 return ret;
4864
4865 return nested_vmx_succeed(vcpu);
4866}
4867
4868static inline void nested_release_vmcs12(struct kvm_vcpu *vcpu)
4869{
4870 struct vcpu_vmx *vmx = to_vmx(vcpu);
4871
4872 if (vmx->nested.current_vmptr == -1ull)
4873 return;
4874
Sean Christopherson7952d762019-05-07 08:36:29 -07004875 copy_vmcs02_to_vmcs12_rare(vcpu, get_vmcs12(vcpu));
4876
Sean Christopherson55d23752018-12-03 13:53:18 -08004877 if (enable_shadow_vmcs) {
4878 /* copy to memory all shadowed fields in case
4879 they were modified */
4880 copy_shadow_to_vmcs12(vmx);
Sean Christopherson55d23752018-12-03 13:53:18 -08004881 vmx_disable_shadow_vmcs(vmx);
4882 }
4883 vmx->nested.posted_intr_nv = -1;
4884
4885 /* Flush VMCS12 to guest memory */
4886 kvm_vcpu_write_guest_page(vcpu,
4887 vmx->nested.current_vmptr >> PAGE_SHIFT,
4888 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
4889
4890 kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
4891
4892 vmx->nested.current_vmptr = -1ull;
4893}
4894
4895/* Emulate the VMXOFF instruction */
4896static int handle_vmoff(struct kvm_vcpu *vcpu)
4897{
4898 if (!nested_vmx_check_permission(vcpu))
4899 return 1;
Liran Alon4b9852f2019-08-26 13:24:49 +03004900
Sean Christopherson55d23752018-12-03 13:53:18 -08004901 free_nested(vcpu);
Liran Alon4b9852f2019-08-26 13:24:49 +03004902
4903 /* Process a latched INIT during time CPU was in VMX operation */
4904 kvm_make_request(KVM_REQ_EVENT, vcpu);
4905
Sean Christopherson55d23752018-12-03 13:53:18 -08004906 return nested_vmx_succeed(vcpu);
4907}
4908
4909/* Emulate the VMCLEAR instruction */
4910static int handle_vmclear(struct kvm_vcpu *vcpu)
4911{
4912 struct vcpu_vmx *vmx = to_vmx(vcpu);
4913 u32 zero = 0;
4914 gpa_t vmptr;
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02004915 u64 evmcs_gpa;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004916 int r;
Sean Christopherson55d23752018-12-03 13:53:18 -08004917
4918 if (!nested_vmx_check_permission(vcpu))
4919 return 1;
4920
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004921 if (nested_vmx_get_vmptr(vcpu, &vmptr, &r))
4922 return r;
Sean Christopherson55d23752018-12-03 13:53:18 -08004923
KarimAllah Ahmede0bf2662019-01-31 21:24:43 +01004924 if (!page_address_valid(vcpu, vmptr))
Sean Christophersonb2656e42020-06-08 18:56:07 -07004925 return nested_vmx_fail(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
Sean Christopherson55d23752018-12-03 13:53:18 -08004926
4927 if (vmptr == vmx->nested.vmxon_ptr)
Sean Christophersonb2656e42020-06-08 18:56:07 -07004928 return nested_vmx_fail(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
Sean Christopherson55d23752018-12-03 13:53:18 -08004929
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +02004930 /*
4931 * When Enlightened VMEntry is enabled on the calling CPU we treat
4932 * memory area pointer by vmptr as Enlightened VMCS (as there's no good
4933 * way to distinguish it from VMCS12) and we must not corrupt it by
4934 * writing to the non-existent 'launch_state' field. The area doesn't
4935 * have to be the currently active EVMCS on the calling CPU and there's
4936 * nothing KVM has to do to transition it from 'active' to 'non-active'
4937 * state. It is possible that the area will stay mapped as
4938 * vmx->nested.hv_evmcs but this shouldn't be a problem.
4939 */
4940 if (likely(!vmx->nested.enlightened_vmcs_enabled ||
4941 !nested_enlightened_vmentry(vcpu, &evmcs_gpa))) {
Sean Christopherson55d23752018-12-03 13:53:18 -08004942 if (vmptr == vmx->nested.current_vmptr)
4943 nested_release_vmcs12(vcpu);
4944
4945 kvm_vcpu_write_guest(vcpu,
4946 vmptr + offsetof(struct vmcs12,
4947 launch_state),
4948 &zero, sizeof(zero));
4949 }
4950
4951 return nested_vmx_succeed(vcpu);
4952}
4953
Sean Christopherson55d23752018-12-03 13:53:18 -08004954/* Emulate the VMLAUNCH instruction */
4955static int handle_vmlaunch(struct kvm_vcpu *vcpu)
4956{
4957 return nested_vmx_run(vcpu, true);
4958}
4959
4960/* Emulate the VMRESUME instruction */
4961static int handle_vmresume(struct kvm_vcpu *vcpu)
4962{
4963
4964 return nested_vmx_run(vcpu, false);
4965}
4966
4967static int handle_vmread(struct kvm_vcpu *vcpu)
4968{
Jim Mattsondd2d6042019-12-06 15:46:35 -08004969 struct vmcs12 *vmcs12 = is_guest_mode(vcpu) ? get_shadow_vmcs12(vcpu)
4970 : get_vmcs12(vcpu);
Sean Christopherson5addc232020-04-15 13:34:53 -07004971 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Jim Mattsonc90f4d02019-12-06 15:46:37 -08004972 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
4973 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzinif7eea632019-09-14 00:26:27 +02004974 struct x86_exception e;
Jim Mattsonc90f4d02019-12-06 15:46:37 -08004975 unsigned long field;
4976 u64 value;
4977 gva_t gva = 0;
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07004978 short offset;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02004979 int len, r;
Sean Christopherson55d23752018-12-03 13:53:18 -08004980
4981 if (!nested_vmx_check_permission(vcpu))
4982 return 1;
4983
Jim Mattsondd2d6042019-12-06 15:46:35 -08004984 /*
4985 * In VMX non-root operation, when the VMCS-link pointer is -1ull,
4986 * any VMREAD sets the ALU flags for VMfailInvalid.
4987 */
4988 if (vmx->nested.current_vmptr == -1ull ||
4989 (is_guest_mode(vcpu) &&
4990 get_vmcs12(vcpu)->vmcs_link_pointer == -1ull))
Sean Christopherson55d23752018-12-03 13:53:18 -08004991 return nested_vmx_failInvalid(vcpu);
4992
Sean Christopherson55d23752018-12-03 13:53:18 -08004993 /* Decode instruction info and find the field to read */
Jim Mattsonc90f4d02019-12-06 15:46:37 -08004994 field = kvm_register_readl(vcpu, (((instr_info) >> 28) & 0xf));
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07004995
4996 offset = vmcs_field_to_offset(field);
4997 if (offset < 0)
Sean Christophersonb2656e42020-06-08 18:56:07 -07004998 return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Sean Christopherson55d23752018-12-03 13:53:18 -08004999
Sean Christopherson7952d762019-05-07 08:36:29 -07005000 if (!is_guest_mode(vcpu) && is_vmcs12_ext_field(field))
5001 copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
5002
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005003 /* Read the field, zero-extended to a u64 value */
5004 value = vmcs12_read_any(vmcs12, field, offset);
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07005005
Sean Christopherson55d23752018-12-03 13:53:18 -08005006 /*
5007 * Now copy part of this value to register or memory, as requested.
5008 * Note that the number of bits actually copied is 32 or 64 depending
5009 * on the guest's mode (32 or 64 bit), not on the given field's length.
5010 */
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005011 if (instr_info & BIT(10)) {
5012 kvm_register_writel(vcpu, (((instr_info) >> 3) & 0xf), value);
Sean Christopherson55d23752018-12-03 13:53:18 -08005013 } else {
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005014 len = is_64_bit_mode(vcpu) ? 8 : 4;
Sean Christopherson55d23752018-12-03 13:53:18 -08005015 if (get_vmx_mem_address(vcpu, exit_qualification,
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005016 instr_info, true, len, &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08005017 return 1;
5018 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005019 r = kvm_write_guest_virt_system(vcpu, gva, &value, len, &e);
5020 if (r != X86EMUL_CONTINUE)
Babu Moger3f3393b2020-09-11 14:29:05 -05005021 return kvm_handle_memory_failure(vcpu, r, &e);
Sean Christopherson55d23752018-12-03 13:53:18 -08005022 }
5023
5024 return nested_vmx_succeed(vcpu);
5025}
5026
Sean Christophersone2174292019-05-07 08:36:28 -07005027static bool is_shadow_field_rw(unsigned long field)
5028{
5029 switch (field) {
5030#define SHADOW_FIELD_RW(x, y) case x:
5031#include "vmcs_shadow_fields.h"
5032 return true;
5033 default:
5034 break;
5035 }
5036 return false;
5037}
5038
5039static bool is_shadow_field_ro(unsigned long field)
5040{
5041 switch (field) {
5042#define SHADOW_FIELD_RO(x, y) case x:
5043#include "vmcs_shadow_fields.h"
5044 return true;
5045 default:
5046 break;
5047 }
5048 return false;
5049}
Sean Christopherson55d23752018-12-03 13:53:18 -08005050
5051static int handle_vmwrite(struct kvm_vcpu *vcpu)
5052{
Jim Mattsondd2d6042019-12-06 15:46:35 -08005053 struct vmcs12 *vmcs12 = is_guest_mode(vcpu) ? get_shadow_vmcs12(vcpu)
5054 : get_vmcs12(vcpu);
Sean Christopherson5addc232020-04-15 13:34:53 -07005055 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005056 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5057 struct vcpu_vmx *vmx = to_vmx(vcpu);
5058 struct x86_exception e;
5059 unsigned long field;
Sean Christopherson1c6f0b42019-05-07 08:36:25 -07005060 short offset;
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005061 gva_t gva;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005062 int len, r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005063
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005064 /*
5065 * The value to write might be 32 or 64 bits, depending on L1's long
Sean Christopherson55d23752018-12-03 13:53:18 -08005066 * mode, and eventually we need to write that into a field of several
5067 * possible lengths. The code below first zero-extends the value to 64
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005068 * bit (value), and then copies only the appropriate number of
Sean Christopherson55d23752018-12-03 13:53:18 -08005069 * bits into the vmcs12 field.
5070 */
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005071 u64 value = 0;
Sean Christopherson55d23752018-12-03 13:53:18 -08005072
5073 if (!nested_vmx_check_permission(vcpu))
5074 return 1;
5075
Jim Mattsondd2d6042019-12-06 15:46:35 -08005076 /*
5077 * In VMX non-root operation, when the VMCS-link pointer is -1ull,
5078 * any VMWRITE sets the ALU flags for VMfailInvalid.
5079 */
5080 if (vmx->nested.current_vmptr == -1ull ||
5081 (is_guest_mode(vcpu) &&
5082 get_vmcs12(vcpu)->vmcs_link_pointer == -1ull))
Sean Christopherson55d23752018-12-03 13:53:18 -08005083 return nested_vmx_failInvalid(vcpu);
5084
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005085 if (instr_info & BIT(10))
5086 value = kvm_register_readl(vcpu, (((instr_info) >> 3) & 0xf));
Sean Christopherson55d23752018-12-03 13:53:18 -08005087 else {
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005088 len = is_64_bit_mode(vcpu) ? 8 : 4;
Sean Christopherson55d23752018-12-03 13:53:18 -08005089 if (get_vmx_mem_address(vcpu, exit_qualification,
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005090 instr_info, false, len, &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08005091 return 1;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005092 r = kvm_read_guest_virt(vcpu, gva, &value, len, &e);
5093 if (r != X86EMUL_CONTINUE)
Babu Moger3f3393b2020-09-11 14:29:05 -05005094 return kvm_handle_memory_failure(vcpu, r, &e);
Sean Christopherson55d23752018-12-03 13:53:18 -08005095 }
5096
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005097 field = kvm_register_readl(vcpu, (((instr_info) >> 28) & 0xf));
Sean Christopherson55d23752018-12-03 13:53:18 -08005098
Jim Mattson693e02c2019-12-06 15:46:36 -08005099 offset = vmcs_field_to_offset(field);
5100 if (offset < 0)
Sean Christophersonb2656e42020-06-08 18:56:07 -07005101 return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Jim Mattson693e02c2019-12-06 15:46:36 -08005102
Sean Christopherson55d23752018-12-03 13:53:18 -08005103 /*
5104 * If the vCPU supports "VMWRITE to any supported field in the
5105 * VMCS," then the "read-only" fields are actually read/write.
5106 */
5107 if (vmcs_field_readonly(field) &&
5108 !nested_cpu_has_vmwrite_any_field(vcpu))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005109 return nested_vmx_fail(vcpu, VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Sean Christopherson55d23752018-12-03 13:53:18 -08005110
Jim Mattsondd2d6042019-12-06 15:46:35 -08005111 /*
5112 * Ensure vmcs12 is up-to-date before any VMWRITE that dirties
5113 * vmcs12, else we may crush a field or consume a stale value.
5114 */
5115 if (!is_guest_mode(vcpu) && !is_shadow_field_rw(field))
5116 copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08005117
5118 /*
Sean Christophersonb6437802019-05-07 08:36:24 -07005119 * Some Intel CPUs intentionally drop the reserved bits of the AR byte
5120 * fields on VMWRITE. Emulate this behavior to ensure consistent KVM
5121 * behavior regardless of the underlying hardware, e.g. if an AR_BYTE
5122 * field is intercepted for VMWRITE but not VMREAD (in L1), then VMREAD
5123 * from L1 will return a different value than VMREAD from L2 (L1 sees
5124 * the stripped down value, L2 sees the full value as stored by KVM).
Sean Christopherson55d23752018-12-03 13:53:18 -08005125 */
Sean Christophersonb6437802019-05-07 08:36:24 -07005126 if (field >= GUEST_ES_AR_BYTES && field <= GUEST_TR_AR_BYTES)
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005127 value &= 0x1f0ff;
Sean Christophersonb6437802019-05-07 08:36:24 -07005128
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005129 vmcs12_write_any(vmcs12, field, offset, value);
Sean Christopherson55d23752018-12-03 13:53:18 -08005130
5131 /*
Sean Christophersone2174292019-05-07 08:36:28 -07005132 * Do not track vmcs12 dirty-state if in guest-mode as we actually
5133 * dirty shadow vmcs12 instead of vmcs12. Fields that can be updated
5134 * by L1 without a vmexit are always updated in the vmcs02, i.e. don't
5135 * "dirty" vmcs12, all others go down the prepare_vmcs02() slow path.
Sean Christopherson55d23752018-12-03 13:53:18 -08005136 */
Sean Christophersone2174292019-05-07 08:36:28 -07005137 if (!is_guest_mode(vcpu) && !is_shadow_field_rw(field)) {
5138 /*
5139 * L1 can read these fields without exiting, ensure the
5140 * shadow VMCS is up-to-date.
5141 */
5142 if (enable_shadow_vmcs && is_shadow_field_ro(field)) {
5143 preempt_disable();
5144 vmcs_load(vmx->vmcs01.shadow_vmcs);
Sean Christophersonfadcead2019-05-07 08:36:23 -07005145
Jim Mattsonc90f4d02019-12-06 15:46:37 -08005146 __vmcs_writel(field, value);
Sean Christophersonfadcead2019-05-07 08:36:23 -07005147
Sean Christophersone2174292019-05-07 08:36:28 -07005148 vmcs_clear(vmx->vmcs01.shadow_vmcs);
5149 vmcs_load(vmx->loaded_vmcs->vmcs);
5150 preempt_enable();
Sean Christopherson55d23752018-12-03 13:53:18 -08005151 }
Sean Christophersone2174292019-05-07 08:36:28 -07005152 vmx->nested.dirty_vmcs12 = true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005153 }
5154
5155 return nested_vmx_succeed(vcpu);
5156}
5157
5158static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
5159{
5160 vmx->nested.current_vmptr = vmptr;
5161 if (enable_shadow_vmcs) {
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07005162 secondary_exec_controls_setbit(vmx, SECONDARY_EXEC_SHADOW_VMCS);
Sean Christopherson55d23752018-12-03 13:53:18 -08005163 vmcs_write64(VMCS_LINK_POINTER,
5164 __pa(vmx->vmcs01.shadow_vmcs));
Sean Christopherson3731905ef2019-05-07 08:36:27 -07005165 vmx->nested.need_vmcs12_to_shadow_sync = true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005166 }
5167 vmx->nested.dirty_vmcs12 = true;
5168}
5169
5170/* Emulate the VMPTRLD instruction */
5171static int handle_vmptrld(struct kvm_vcpu *vcpu)
5172{
5173 struct vcpu_vmx *vmx = to_vmx(vcpu);
5174 gpa_t vmptr;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005175 int r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005176
5177 if (!nested_vmx_check_permission(vcpu))
5178 return 1;
5179
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005180 if (nested_vmx_get_vmptr(vcpu, &vmptr, &r))
5181 return r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005182
KarimAllah Ahmede0bf2662019-01-31 21:24:43 +01005183 if (!page_address_valid(vcpu, vmptr))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005184 return nested_vmx_fail(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
Sean Christopherson55d23752018-12-03 13:53:18 -08005185
5186 if (vmptr == vmx->nested.vmxon_ptr)
Sean Christophersonb2656e42020-06-08 18:56:07 -07005187 return nested_vmx_fail(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
Sean Christopherson55d23752018-12-03 13:53:18 -08005188
5189 /* Forbid normal VMPTRLD if Enlightened version was used */
5190 if (vmx->nested.hv_evmcs)
5191 return 1;
5192
5193 if (vmx->nested.current_vmptr != vmptr) {
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01005194 struct kvm_host_map map;
Sean Christopherson55d23752018-12-03 13:53:18 -08005195 struct vmcs12 *new_vmcs12;
Sean Christopherson55d23752018-12-03 13:53:18 -08005196
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01005197 if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmptr), &map)) {
Sean Christopherson55d23752018-12-03 13:53:18 -08005198 /*
5199 * Reads from an unbacked page return all 1s,
5200 * which means that the 32 bits located at the
5201 * given physical address won't match the required
5202 * VMCS12_REVISION identifier.
5203 */
Sean Christophersonb2656e42020-06-08 18:56:07 -07005204 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005205 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Sean Christopherson55d23752018-12-03 13:53:18 -08005206 }
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01005207
5208 new_vmcs12 = map.hva;
5209
Sean Christopherson55d23752018-12-03 13:53:18 -08005210 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
5211 (new_vmcs12->hdr.shadow_vmcs &&
5212 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01005213 kvm_vcpu_unmap(vcpu, &map, false);
Sean Christophersonb2656e42020-06-08 18:56:07 -07005214 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005215 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
5216 }
5217
5218 nested_release_vmcs12(vcpu);
5219
5220 /*
5221 * Load VMCS12 from guest memory since it is not already
5222 * cached.
5223 */
5224 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
KarimAllah Ahmedb146b832019-01-31 21:24:35 +01005225 kvm_vcpu_unmap(vcpu, &map, false);
Sean Christopherson55d23752018-12-03 13:53:18 -08005226
5227 set_current_vmptr(vmx, vmptr);
5228 }
5229
5230 return nested_vmx_succeed(vcpu);
5231}
5232
5233/* Emulate the VMPTRST instruction */
5234static int handle_vmptrst(struct kvm_vcpu *vcpu)
5235{
Sean Christopherson5addc232020-04-15 13:34:53 -07005236 unsigned long exit_qual = vmx_get_exit_qual(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005237 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5238 gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr;
5239 struct x86_exception e;
5240 gva_t gva;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005241 int r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005242
5243 if (!nested_vmx_check_permission(vcpu))
5244 return 1;
5245
5246 if (unlikely(to_vmx(vcpu)->nested.hv_evmcs))
5247 return 1;
5248
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005249 if (get_vmx_mem_address(vcpu, exit_qual, instr_info,
5250 true, sizeof(gpa_t), &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08005251 return 1;
5252 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005253 r = kvm_write_guest_virt_system(vcpu, gva, (void *)&current_vmptr,
5254 sizeof(gpa_t), &e);
5255 if (r != X86EMUL_CONTINUE)
Babu Moger3f3393b2020-09-11 14:29:05 -05005256 return kvm_handle_memory_failure(vcpu, r, &e);
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005257
Sean Christopherson55d23752018-12-03 13:53:18 -08005258 return nested_vmx_succeed(vcpu);
5259}
5260
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005261#define EPTP_PA_MASK GENMASK_ULL(51, 12)
5262
5263static bool nested_ept_root_matches(hpa_t root_hpa, u64 root_eptp, u64 eptp)
5264{
5265 return VALID_PAGE(root_hpa) &&
5266 ((root_eptp & EPTP_PA_MASK) == (eptp & EPTP_PA_MASK));
5267}
5268
Sean Christopherson55d23752018-12-03 13:53:18 -08005269/* Emulate the INVEPT instruction */
5270static int handle_invept(struct kvm_vcpu *vcpu)
5271{
5272 struct vcpu_vmx *vmx = to_vmx(vcpu);
5273 u32 vmx_instruction_info, types;
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005274 unsigned long type, roots_to_free;
5275 struct kvm_mmu *mmu;
Sean Christopherson55d23752018-12-03 13:53:18 -08005276 gva_t gva;
5277 struct x86_exception e;
5278 struct {
5279 u64 eptp, gpa;
5280 } operand;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005281 int i, r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005282
5283 if (!(vmx->nested.msrs.secondary_ctls_high &
5284 SECONDARY_EXEC_ENABLE_EPT) ||
5285 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
5286 kvm_queue_exception(vcpu, UD_VECTOR);
5287 return 1;
5288 }
5289
5290 if (!nested_vmx_check_permission(vcpu))
5291 return 1;
5292
5293 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5294 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
5295
5296 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
5297
5298 if (type >= 32 || !(types & (1 << type)))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005299 return nested_vmx_fail(vcpu, VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Sean Christopherson55d23752018-12-03 13:53:18 -08005300
5301 /* According to the Intel VMX instruction reference, the memory
5302 * operand is read even if it isn't needed (e.g., for type==global)
5303 */
Sean Christopherson5addc232020-04-15 13:34:53 -07005304 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005305 vmx_instruction_info, false, sizeof(operand), &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08005306 return 1;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005307 r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e);
5308 if (r != X86EMUL_CONTINUE)
Babu Moger3f3393b2020-09-11 14:29:05 -05005309 return kvm_handle_memory_failure(vcpu, r, &e);
Sean Christopherson55d23752018-12-03 13:53:18 -08005310
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005311 /*
5312 * Nested EPT roots are always held through guest_mmu,
5313 * not root_mmu.
5314 */
5315 mmu = &vcpu->arch.guest_mmu;
5316
Sean Christopherson55d23752018-12-03 13:53:18 -08005317 switch (type) {
Sean Christopherson55d23752018-12-03 13:53:18 -08005318 case VMX_EPT_EXTENT_CONTEXT:
Sean Christophersoneed00302020-03-20 14:27:58 -07005319 if (!nested_vmx_check_eptp(vcpu, operand.eptp))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005320 return nested_vmx_fail(vcpu,
Sean Christophersoneed00302020-03-20 14:27:58 -07005321 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Sean Christophersonf8aa7e32020-03-20 14:27:59 -07005322
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005323 roots_to_free = 0;
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07005324 if (nested_ept_root_matches(mmu->root_hpa, mmu->root_pgd,
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005325 operand.eptp))
5326 roots_to_free |= KVM_MMU_ROOT_CURRENT;
5327
5328 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) {
5329 if (nested_ept_root_matches(mmu->prev_roots[i].hpa,
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07005330 mmu->prev_roots[i].pgd,
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005331 operand.eptp))
5332 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
5333 }
5334 break;
Sean Christophersoneed00302020-03-20 14:27:58 -07005335 case VMX_EPT_EXTENT_GLOBAL:
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005336 roots_to_free = KVM_MMU_ROOTS_ALL;
Sean Christopherson55d23752018-12-03 13:53:18 -08005337 break;
5338 default:
Sean Christophersonf9336e32020-05-04 08:35:06 -07005339 BUG();
Sean Christopherson55d23752018-12-03 13:53:18 -08005340 break;
5341 }
5342
Sean Christophersonce8fe7b2020-03-20 14:28:31 -07005343 if (roots_to_free)
5344 kvm_mmu_free_roots(vcpu, mmu, roots_to_free);
5345
Sean Christopherson55d23752018-12-03 13:53:18 -08005346 return nested_vmx_succeed(vcpu);
5347}
5348
5349static int handle_invvpid(struct kvm_vcpu *vcpu)
5350{
5351 struct vcpu_vmx *vmx = to_vmx(vcpu);
5352 u32 vmx_instruction_info;
5353 unsigned long type, types;
5354 gva_t gva;
5355 struct x86_exception e;
5356 struct {
5357 u64 vpid;
5358 u64 gla;
5359 } operand;
5360 u16 vpid02;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005361 int r;
Sean Christopherson55d23752018-12-03 13:53:18 -08005362
5363 if (!(vmx->nested.msrs.secondary_ctls_high &
5364 SECONDARY_EXEC_ENABLE_VPID) ||
5365 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
5366 kvm_queue_exception(vcpu, UD_VECTOR);
5367 return 1;
5368 }
5369
5370 if (!nested_vmx_check_permission(vcpu))
5371 return 1;
5372
5373 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5374 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
5375
5376 types = (vmx->nested.msrs.vpid_caps &
5377 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
5378
5379 if (type >= 32 || !(types & (1 << type)))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005380 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005381 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
5382
5383 /* according to the intel vmx instruction reference, the memory
5384 * operand is read even if it isn't needed (e.g., for type==global)
5385 */
Sean Christopherson5addc232020-04-15 13:34:53 -07005386 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005387 vmx_instruction_info, false, sizeof(operand), &gva))
Sean Christopherson55d23752018-12-03 13:53:18 -08005388 return 1;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005389 r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e);
5390 if (r != X86EMUL_CONTINUE)
Babu Moger3f3393b2020-09-11 14:29:05 -05005391 return kvm_handle_memory_failure(vcpu, r, &e);
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005392
Sean Christopherson55d23752018-12-03 13:53:18 -08005393 if (operand.vpid >> 16)
Sean Christophersonb2656e42020-06-08 18:56:07 -07005394 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005395 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
5396
5397 vpid02 = nested_get_vpid02(vcpu);
5398 switch (type) {
5399 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
5400 if (!operand.vpid ||
5401 is_noncanonical_address(operand.gla, vcpu))
Sean Christophersonb2656e42020-06-08 18:56:07 -07005402 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005403 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Sean Christophersonbc41d0c2020-03-20 14:28:09 -07005404 vpid_sync_vcpu_addr(vpid02, operand.gla);
Sean Christopherson55d23752018-12-03 13:53:18 -08005405 break;
5406 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
5407 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
5408 if (!operand.vpid)
Sean Christophersonb2656e42020-06-08 18:56:07 -07005409 return nested_vmx_fail(vcpu,
Sean Christopherson55d23752018-12-03 13:53:18 -08005410 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Sean Christopherson446ace42020-03-20 14:28:05 -07005411 vpid_sync_context(vpid02);
Sean Christopherson55d23752018-12-03 13:53:18 -08005412 break;
5413 case VMX_VPID_EXTENT_ALL_CONTEXT:
Sean Christopherson446ace42020-03-20 14:28:05 -07005414 vpid_sync_context(vpid02);
Sean Christopherson55d23752018-12-03 13:53:18 -08005415 break;
5416 default:
5417 WARN_ON_ONCE(1);
5418 return kvm_skip_emulated_instruction(vcpu);
5419 }
5420
Junaid Shahidd6e3f832020-03-20 14:28:00 -07005421 /*
5422 * Sync the shadow page tables if EPT is disabled, L1 is invalidating
5423 * linear mappings for L2 (tagged with L2's VPID). Free all roots as
5424 * VPIDs are not tracked in the MMU role.
5425 *
5426 * Note, this operates on root_mmu, not guest_mmu, as L1 and L2 share
5427 * an MMU when EPT is disabled.
5428 *
5429 * TODO: sync only the affected SPTEs for INVDIVIDUAL_ADDR.
5430 */
5431 if (!enable_ept)
5432 kvm_mmu_free_roots(vcpu, &vcpu->arch.root_mmu,
5433 KVM_MMU_ROOTS_ALL);
5434
Sean Christopherson55d23752018-12-03 13:53:18 -08005435 return nested_vmx_succeed(vcpu);
5436}
5437
5438static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
5439 struct vmcs12 *vmcs12)
5440{
Sean Christopherson2b3eaf82019-04-30 10:36:19 -07005441 u32 index = kvm_rcx_read(vcpu);
Sean Christophersonac6389a2020-03-02 18:02:38 -08005442 u64 new_eptp;
Sean Christopherson55d23752018-12-03 13:53:18 -08005443 bool accessed_dirty;
5444 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5445
5446 if (!nested_cpu_has_eptp_switching(vmcs12) ||
5447 !nested_cpu_has_ept(vmcs12))
5448 return 1;
5449
5450 if (index >= VMFUNC_EPTP_ENTRIES)
5451 return 1;
5452
5453
5454 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
Sean Christophersonac6389a2020-03-02 18:02:38 -08005455 &new_eptp, index * 8, 8))
Sean Christopherson55d23752018-12-03 13:53:18 -08005456 return 1;
5457
Sean Christophersonac6389a2020-03-02 18:02:38 -08005458 accessed_dirty = !!(new_eptp & VMX_EPTP_AD_ENABLE_BIT);
Sean Christopherson55d23752018-12-03 13:53:18 -08005459
5460 /*
5461 * If the (L2) guest does a vmfunc to the currently
5462 * active ept pointer, we don't have to do anything else
5463 */
Sean Christophersonac6389a2020-03-02 18:02:38 -08005464 if (vmcs12->ept_pointer != new_eptp) {
5465 if (!nested_vmx_check_eptp(vcpu, new_eptp))
Sean Christopherson55d23752018-12-03 13:53:18 -08005466 return 1;
5467
5468 kvm_mmu_unload(vcpu);
5469 mmu->ept_ad = accessed_dirty;
5470 mmu->mmu_role.base.ad_disabled = !accessed_dirty;
Sean Christophersonac6389a2020-03-02 18:02:38 -08005471 vmcs12->ept_pointer = new_eptp;
Sean Christopherson55d23752018-12-03 13:53:18 -08005472 /*
5473 * TODO: Check what's the correct approach in case
5474 * mmu reload fails. Currently, we just let the next
5475 * reload potentially fail
5476 */
5477 kvm_mmu_reload(vcpu);
5478 }
5479
5480 return 0;
5481}
5482
5483static int handle_vmfunc(struct kvm_vcpu *vcpu)
5484{
5485 struct vcpu_vmx *vmx = to_vmx(vcpu);
5486 struct vmcs12 *vmcs12;
Sean Christopherson2b3eaf82019-04-30 10:36:19 -07005487 u32 function = kvm_rax_read(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005488
5489 /*
5490 * VMFUNC is only supported for nested guests, but we always enable the
5491 * secondary control for simplicity; for non-nested mode, fake that we
5492 * didn't by injecting #UD.
5493 */
5494 if (!is_guest_mode(vcpu)) {
5495 kvm_queue_exception(vcpu, UD_VECTOR);
5496 return 1;
5497 }
5498
5499 vmcs12 = get_vmcs12(vcpu);
5500 if ((vmcs12->vm_function_control & (1 << function)) == 0)
5501 goto fail;
5502
5503 switch (function) {
5504 case 0:
5505 if (nested_vmx_eptp_switching(vcpu, vmcs12))
5506 goto fail;
5507 break;
5508 default:
5509 goto fail;
5510 }
5511 return kvm_skip_emulated_instruction(vcpu);
5512
5513fail:
5514 nested_vmx_vmexit(vcpu, vmx->exit_reason,
Sean Christopherson87915852020-04-15 13:34:54 -07005515 vmx_get_intr_info(vcpu),
Sean Christopherson5addc232020-04-15 13:34:53 -07005516 vmx_get_exit_qual(vcpu));
Sean Christopherson55d23752018-12-03 13:53:18 -08005517 return 1;
5518}
5519
Oliver Uptone71237d2020-02-04 15:26:30 -08005520/*
5521 * Return true if an IO instruction with the specified port and size should cause
5522 * a VM-exit into L1.
5523 */
5524bool nested_vmx_check_io_bitmaps(struct kvm_vcpu *vcpu, unsigned int port,
5525 int size)
Sean Christopherson55d23752018-12-03 13:53:18 -08005526{
Oliver Uptone71237d2020-02-04 15:26:30 -08005527 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005528 gpa_t bitmap, last_bitmap;
Sean Christopherson55d23752018-12-03 13:53:18 -08005529 u8 b;
5530
Sean Christopherson55d23752018-12-03 13:53:18 -08005531 last_bitmap = (gpa_t)-1;
5532 b = -1;
5533
5534 while (size > 0) {
5535 if (port < 0x8000)
5536 bitmap = vmcs12->io_bitmap_a;
5537 else if (port < 0x10000)
5538 bitmap = vmcs12->io_bitmap_b;
5539 else
5540 return true;
5541 bitmap += (port & 0x7fff) / 8;
5542
5543 if (last_bitmap != bitmap)
5544 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
5545 return true;
5546 if (b & (1 << (port & 7)))
5547 return true;
5548
5549 port++;
5550 size--;
5551 last_bitmap = bitmap;
5552 }
5553
5554 return false;
5555}
5556
Oliver Uptone71237d2020-02-04 15:26:30 -08005557static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
5558 struct vmcs12 *vmcs12)
5559{
5560 unsigned long exit_qualification;
Oliver Upton35a57132020-02-04 15:26:31 -08005561 unsigned short port;
Oliver Uptone71237d2020-02-04 15:26:30 -08005562 int size;
5563
5564 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
5565 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
5566
Sean Christopherson5addc232020-04-15 13:34:53 -07005567 exit_qualification = vmx_get_exit_qual(vcpu);
Oliver Uptone71237d2020-02-04 15:26:30 -08005568
5569 port = exit_qualification >> 16;
5570 size = (exit_qualification & 7) + 1;
5571
5572 return nested_vmx_check_io_bitmaps(vcpu, port, size);
5573}
5574
Sean Christopherson55d23752018-12-03 13:53:18 -08005575/*
Miaohe Lin463bfee2020-02-14 10:44:05 +08005576 * Return 1 if we should exit from L2 to L1 to handle an MSR access,
Sean Christopherson55d23752018-12-03 13:53:18 -08005577 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
5578 * disinterest in the current event (read or write a specific MSR) by using an
5579 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
5580 */
5581static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
5582 struct vmcs12 *vmcs12, u32 exit_reason)
5583{
Sean Christopherson2b3eaf82019-04-30 10:36:19 -07005584 u32 msr_index = kvm_rcx_read(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005585 gpa_t bitmap;
5586
5587 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
5588 return true;
5589
5590 /*
5591 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
5592 * for the four combinations of read/write and low/high MSR numbers.
5593 * First we need to figure out which of the four to use:
5594 */
5595 bitmap = vmcs12->msr_bitmap;
5596 if (exit_reason == EXIT_REASON_MSR_WRITE)
5597 bitmap += 2048;
5598 if (msr_index >= 0xc0000000) {
5599 msr_index -= 0xc0000000;
5600 bitmap += 1024;
5601 }
5602
5603 /* Then read the msr_index'th bit from this bitmap: */
5604 if (msr_index < 1024*8) {
5605 unsigned char b;
5606 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
5607 return true;
5608 return 1 & (b >> (msr_index & 7));
5609 } else
5610 return true; /* let L1 handle the wrong parameter */
5611}
5612
5613/*
5614 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
5615 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
5616 * intercept (via guest_host_mask etc.) the current event.
5617 */
5618static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
5619 struct vmcs12 *vmcs12)
5620{
Sean Christopherson5addc232020-04-15 13:34:53 -07005621 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005622 int cr = exit_qualification & 15;
5623 int reg;
5624 unsigned long val;
5625
5626 switch ((exit_qualification >> 4) & 3) {
5627 case 0: /* mov to cr */
5628 reg = (exit_qualification >> 8) & 15;
5629 val = kvm_register_readl(vcpu, reg);
5630 switch (cr) {
5631 case 0:
5632 if (vmcs12->cr0_guest_host_mask &
5633 (val ^ vmcs12->cr0_read_shadow))
5634 return true;
5635 break;
5636 case 3:
Sean Christopherson55d23752018-12-03 13:53:18 -08005637 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
5638 return true;
5639 break;
5640 case 4:
5641 if (vmcs12->cr4_guest_host_mask &
5642 (vmcs12->cr4_read_shadow ^ val))
5643 return true;
5644 break;
5645 case 8:
5646 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
5647 return true;
5648 break;
5649 }
5650 break;
5651 case 2: /* clts */
5652 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
5653 (vmcs12->cr0_read_shadow & X86_CR0_TS))
5654 return true;
5655 break;
5656 case 1: /* mov from cr */
5657 switch (cr) {
5658 case 3:
5659 if (vmcs12->cpu_based_vm_exec_control &
5660 CPU_BASED_CR3_STORE_EXITING)
5661 return true;
5662 break;
5663 case 8:
5664 if (vmcs12->cpu_based_vm_exec_control &
5665 CPU_BASED_CR8_STORE_EXITING)
5666 return true;
5667 break;
5668 }
5669 break;
5670 case 3: /* lmsw */
5671 /*
5672 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
5673 * cr0. Other attempted changes are ignored, with no exit.
5674 */
5675 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
5676 if (vmcs12->cr0_guest_host_mask & 0xe &
5677 (val ^ vmcs12->cr0_read_shadow))
5678 return true;
5679 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
5680 !(vmcs12->cr0_read_shadow & 0x1) &&
5681 (val & 0x1))
5682 return true;
5683 break;
5684 }
5685 return false;
5686}
5687
5688static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
5689 struct vmcs12 *vmcs12, gpa_t bitmap)
5690{
5691 u32 vmx_instruction_info;
5692 unsigned long field;
5693 u8 b;
5694
5695 if (!nested_cpu_has_shadow_vmcs(vmcs12))
5696 return true;
5697
5698 /* Decode instruction info and find the field to access */
5699 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5700 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
5701
5702 /* Out-of-range fields always cause a VM exit from L2 to L1 */
5703 if (field >> 15)
5704 return true;
5705
5706 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
5707 return true;
5708
5709 return 1 & (b >> (field & 7));
5710}
5711
Oliver Uptonb045ae92020-04-14 22:47:45 +00005712static bool nested_vmx_exit_handled_mtf(struct vmcs12 *vmcs12)
5713{
5714 u32 entry_intr_info = vmcs12->vm_entry_intr_info_field;
5715
5716 if (nested_cpu_has_mtf(vmcs12))
5717 return true;
5718
5719 /*
5720 * An MTF VM-exit may be injected into the guest by setting the
5721 * interruption-type to 7 (other event) and the vector field to 0. Such
5722 * is the case regardless of the 'monitor trap flag' VM-execution
5723 * control.
5724 */
5725 return entry_intr_info == (INTR_INFO_VALID_MASK
5726 | INTR_TYPE_OTHER_EVENT);
5727}
5728
Sean Christopherson55d23752018-12-03 13:53:18 -08005729/*
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005730 * Return true if L0 wants to handle an exit from L2 regardless of whether or not
5731 * L1 wants the exit. Only call this when in is_guest_mode (L2).
Sean Christopherson55d23752018-12-03 13:53:18 -08005732 */
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005733static bool nested_vmx_l0_wants_exit(struct kvm_vcpu *vcpu, u32 exit_reason)
Sean Christopherson55d23752018-12-03 13:53:18 -08005734{
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005735 u32 intr_info;
5736
Sean Christopherson2ebac8b2020-02-27 09:44:30 -08005737 switch ((u16)exit_reason) {
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005738 case EXIT_REASON_EXCEPTION_NMI:
Sean Christopherson87915852020-04-15 13:34:54 -07005739 intr_info = vmx_get_intr_info(vcpu);
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005740 if (is_nmi(intr_info))
5741 return true;
5742 else if (is_page_fault(intr_info))
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +02005743 return vcpu->arch.apf.host_apf_flags || !enable_ept;
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005744 else if (is_debug(intr_info) &&
5745 vcpu->guest_debug &
5746 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5747 return true;
5748 else if (is_breakpoint(intr_info) &&
5749 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
5750 return true;
5751 return false;
5752 case EXIT_REASON_EXTERNAL_INTERRUPT:
5753 return true;
5754 case EXIT_REASON_MCE_DURING_VMENTRY:
5755 return true;
5756 case EXIT_REASON_EPT_VIOLATION:
5757 /*
5758 * L0 always deals with the EPT violation. If nested EPT is
5759 * used, and the nested mmu code discovers that the address is
5760 * missing in the guest EPT table (EPT12), the EPT violation
5761 * will be injected with nested_ept_inject_page_fault()
5762 */
5763 return true;
5764 case EXIT_REASON_EPT_MISCONFIG:
5765 /*
5766 * L2 never uses directly L1's EPT, but rather L0's own EPT
5767 * table (shadow on EPT) or a merged EPT table that L0 built
5768 * (EPT on EPT). So any problems with the structure of the
5769 * table is L0's fault.
5770 */
5771 return true;
5772 case EXIT_REASON_PREEMPTION_TIMER:
5773 return true;
5774 case EXIT_REASON_PML_FULL:
5775 /* We emulate PML support to L1. */
5776 return true;
5777 case EXIT_REASON_VMFUNC:
5778 /* VM functions are emulated through L2->L0 vmexits. */
5779 return true;
5780 case EXIT_REASON_ENCLS:
5781 /* SGX is never exposed to L1 */
5782 return true;
5783 default:
5784 break;
5785 }
5786 return false;
5787}
5788
5789/*
5790 * Return 1 if L1 wants to intercept an exit from L2. Only call this when in
5791 * is_guest_mode (L2).
5792 */
5793static bool nested_vmx_l1_wants_exit(struct kvm_vcpu *vcpu, u32 exit_reason)
5794{
Sean Christopherson55d23752018-12-03 13:53:18 -08005795 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Sean Christopherson9bd4af22020-04-21 00:53:27 -07005796 u32 intr_info;
Sean Christopherson55d23752018-12-03 13:53:18 -08005797
Paolo Bonzini77f81f32020-06-09 06:08:48 -04005798 switch ((u16)exit_reason) {
Sean Christopherson55d23752018-12-03 13:53:18 -08005799 case EXIT_REASON_EXCEPTION_NMI:
Sean Christopherson87915852020-04-15 13:34:54 -07005800 intr_info = vmx_get_intr_info(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005801 if (is_nmi(intr_info))
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005802 return true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005803 else if (is_page_fault(intr_info))
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005804 return true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005805 return vmcs12->exception_bitmap &
5806 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
5807 case EXIT_REASON_EXTERNAL_INTERRUPT:
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005808 return nested_exit_on_intr(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08005809 case EXIT_REASON_TRIPLE_FAULT:
5810 return true;
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08005811 case EXIT_REASON_INTERRUPT_WINDOW:
5812 return nested_cpu_has(vmcs12, CPU_BASED_INTR_WINDOW_EXITING);
Sean Christopherson55d23752018-12-03 13:53:18 -08005813 case EXIT_REASON_NMI_WINDOW:
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08005814 return nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING);
Sean Christopherson55d23752018-12-03 13:53:18 -08005815 case EXIT_REASON_TASK_SWITCH:
5816 return true;
5817 case EXIT_REASON_CPUID:
5818 return true;
5819 case EXIT_REASON_HLT:
5820 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
5821 case EXIT_REASON_INVD:
5822 return true;
5823 case EXIT_REASON_INVLPG:
5824 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
5825 case EXIT_REASON_RDPMC:
5826 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
5827 case EXIT_REASON_RDRAND:
5828 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
5829 case EXIT_REASON_RDSEED:
5830 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
5831 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
5832 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
5833 case EXIT_REASON_VMREAD:
5834 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
5835 vmcs12->vmread_bitmap);
5836 case EXIT_REASON_VMWRITE:
5837 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
5838 vmcs12->vmwrite_bitmap);
5839 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
5840 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
5841 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
5842 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
5843 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
5844 /*
5845 * VMX instructions trap unconditionally. This allows L1 to
5846 * emulate them for its L2 guest, i.e., allows 3-level nesting!
5847 */
5848 return true;
5849 case EXIT_REASON_CR_ACCESS:
5850 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
5851 case EXIT_REASON_DR_ACCESS:
5852 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
5853 case EXIT_REASON_IO_INSTRUCTION:
5854 return nested_vmx_exit_handled_io(vcpu, vmcs12);
5855 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
5856 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
5857 case EXIT_REASON_MSR_READ:
5858 case EXIT_REASON_MSR_WRITE:
5859 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
5860 case EXIT_REASON_INVALID_STATE:
5861 return true;
5862 case EXIT_REASON_MWAIT_INSTRUCTION:
5863 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
5864 case EXIT_REASON_MONITOR_TRAP_FLAG:
Oliver Uptonb045ae92020-04-14 22:47:45 +00005865 return nested_vmx_exit_handled_mtf(vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08005866 case EXIT_REASON_MONITOR_INSTRUCTION:
5867 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
5868 case EXIT_REASON_PAUSE_INSTRUCTION:
5869 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
5870 nested_cpu_has2(vmcs12,
5871 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
5872 case EXIT_REASON_MCE_DURING_VMENTRY:
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005873 return true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005874 case EXIT_REASON_TPR_BELOW_THRESHOLD:
5875 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
5876 case EXIT_REASON_APIC_ACCESS:
5877 case EXIT_REASON_APIC_WRITE:
5878 case EXIT_REASON_EOI_INDUCED:
5879 /*
5880 * The controls for "virtualize APIC accesses," "APIC-
5881 * register virtualization," and "virtual-interrupt
5882 * delivery" only come from vmcs12.
5883 */
5884 return true;
Sean Christopherson55d23752018-12-03 13:53:18 -08005885 case EXIT_REASON_INVPCID:
5886 return
5887 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
5888 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
5889 case EXIT_REASON_WBINVD:
5890 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
5891 case EXIT_REASON_XSETBV:
5892 return true;
5893 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
5894 /*
5895 * This should never happen, since it is not possible to
5896 * set XSS to a non-zero value---neither in L1 nor in L2.
5897 * If if it were, XSS would have to be checked against
5898 * the XSS exit bitmap in vmcs12.
5899 */
5900 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Tao Xubf653b72019-07-16 14:55:51 +08005901 case EXIT_REASON_UMWAIT:
5902 case EXIT_REASON_TPAUSE:
5903 return nested_cpu_has2(vmcs12,
5904 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE);
Sean Christopherson55d23752018-12-03 13:53:18 -08005905 default:
5906 return true;
5907 }
5908}
5909
Sean Christopherson7b7bd872020-04-15 10:55:11 -07005910/*
5911 * Conditionally reflect a VM-Exit into L1. Returns %true if the VM-Exit was
5912 * reflected into L1.
5913 */
Sean Christophersonf47baae2020-04-15 10:55:16 -07005914bool nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu)
Sean Christopherson7b7bd872020-04-15 10:55:11 -07005915{
Sean Christophersonfbdd5022020-04-15 10:55:12 -07005916 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersonf47baae2020-04-15 10:55:16 -07005917 u32 exit_reason = vmx->exit_reason;
Sean Christopherson87796552020-04-22 17:11:27 -07005918 unsigned long exit_qual;
5919 u32 exit_intr_info;
Sean Christophersonfbdd5022020-04-15 10:55:12 -07005920
5921 WARN_ON_ONCE(vmx->nested.nested_run_pending);
5922
5923 /*
5924 * Late nested VM-Fail shares the same flow as nested VM-Exit since KVM
5925 * has already loaded L2's state.
5926 */
5927 if (unlikely(vmx->fail)) {
5928 trace_kvm_nested_vmenter_failed(
5929 "hardware VM-instruction error: ",
5930 vmcs_read32(VM_INSTRUCTION_ERROR));
5931 exit_intr_info = 0;
5932 exit_qual = 0;
5933 goto reflect_vmexit;
5934 }
Sean Christopherson7b7bd872020-04-15 10:55:11 -07005935
Sean Christopherson87915852020-04-15 13:34:54 -07005936 exit_intr_info = vmx_get_intr_info(vcpu);
Sean Christopherson5addc232020-04-15 13:34:53 -07005937 exit_qual = vmx_get_exit_qual(vcpu);
Sean Christopherson236871b2020-04-15 10:55:13 -07005938
5939 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason, exit_qual,
5940 vmx->idt_vectoring_info, exit_intr_info,
5941 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
5942 KVM_ISA_VMX);
5943
Sean Christopherson2c1f3322020-04-15 10:55:14 -07005944 /* If L0 (KVM) wants the exit, it trumps L1's desires. */
5945 if (nested_vmx_l0_wants_exit(vcpu, exit_reason))
5946 return false;
5947
5948 /* If L1 doesn't want the exit, handle it in L0. */
5949 if (!nested_vmx_l1_wants_exit(vcpu, exit_reason))
Sean Christopherson7b7bd872020-04-15 10:55:11 -07005950 return false;
5951
5952 /*
Sean Christopherson1d283062020-04-15 10:55:15 -07005953 * vmcs.VM_EXIT_INTR_INFO is only valid for EXCEPTION_NMI exits. For
5954 * EXTERNAL_INTERRUPT, the value for vmcs12->vm_exit_intr_info would
5955 * need to be synthesized by querying the in-kernel LAPIC, but external
5956 * interrupts are never reflected to L1 so it's a non-issue.
Sean Christopherson7b7bd872020-04-15 10:55:11 -07005957 */
Sean Christopherson7b7bd872020-04-15 10:55:11 -07005958 if ((exit_intr_info &
5959 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
5960 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
5961 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5962
5963 vmcs12->vm_exit_intr_error_code =
5964 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
5965 }
5966
Sean Christophersonfbdd5022020-04-15 10:55:12 -07005967reflect_vmexit:
5968 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info, exit_qual);
Sean Christopherson7b7bd872020-04-15 10:55:11 -07005969 return true;
5970}
Sean Christopherson55d23752018-12-03 13:53:18 -08005971
5972static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
5973 struct kvm_nested_state __user *user_kvm_nested_state,
5974 u32 user_data_size)
5975{
5976 struct vcpu_vmx *vmx;
5977 struct vmcs12 *vmcs12;
5978 struct kvm_nested_state kvm_state = {
5979 .flags = 0,
Liran Alon6ca00df2019-06-16 15:03:10 +03005980 .format = KVM_STATE_NESTED_FORMAT_VMX,
Sean Christopherson55d23752018-12-03 13:53:18 -08005981 .size = sizeof(kvm_state),
Peter Shier850448f2020-05-26 14:51:06 -07005982 .hdr.vmx.flags = 0,
Liran Alon6ca00df2019-06-16 15:03:10 +03005983 .hdr.vmx.vmxon_pa = -1ull,
5984 .hdr.vmx.vmcs12_pa = -1ull,
Peter Shier850448f2020-05-26 14:51:06 -07005985 .hdr.vmx.preemption_timer_deadline = 0,
Sean Christopherson55d23752018-12-03 13:53:18 -08005986 };
Liran Alon6ca00df2019-06-16 15:03:10 +03005987 struct kvm_vmx_nested_state_data __user *user_vmx_nested_state =
5988 &user_kvm_nested_state->data.vmx[0];
Sean Christopherson55d23752018-12-03 13:53:18 -08005989
5990 if (!vcpu)
Liran Alon6ca00df2019-06-16 15:03:10 +03005991 return kvm_state.size + sizeof(*user_vmx_nested_state);
Sean Christopherson55d23752018-12-03 13:53:18 -08005992
5993 vmx = to_vmx(vcpu);
5994 vmcs12 = get_vmcs12(vcpu);
5995
Sean Christopherson55d23752018-12-03 13:53:18 -08005996 if (nested_vmx_allowed(vcpu) &&
5997 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
Liran Alon6ca00df2019-06-16 15:03:10 +03005998 kvm_state.hdr.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
5999 kvm_state.hdr.vmx.vmcs12_pa = vmx->nested.current_vmptr;
Sean Christopherson55d23752018-12-03 13:53:18 -08006000
6001 if (vmx_has_valid_vmcs12(vcpu)) {
Liran Alon6ca00df2019-06-16 15:03:10 +03006002 kvm_state.size += sizeof(user_vmx_nested_state->vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08006003
Liran Alon323d73a2019-06-26 16:09:27 +03006004 if (vmx->nested.hv_evmcs)
6005 kvm_state.flags |= KVM_STATE_NESTED_EVMCS;
6006
Sean Christopherson55d23752018-12-03 13:53:18 -08006007 if (is_guest_mode(vcpu) &&
6008 nested_cpu_has_shadow_vmcs(vmcs12) &&
6009 vmcs12->vmcs_link_pointer != -1ull)
Liran Alon6ca00df2019-06-16 15:03:10 +03006010 kvm_state.size += sizeof(user_vmx_nested_state->shadow_vmcs12);
Sean Christopherson55d23752018-12-03 13:53:18 -08006011 }
6012
6013 if (vmx->nested.smm.vmxon)
Liran Alon6ca00df2019-06-16 15:03:10 +03006014 kvm_state.hdr.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
Sean Christopherson55d23752018-12-03 13:53:18 -08006015
6016 if (vmx->nested.smm.guest_mode)
Liran Alon6ca00df2019-06-16 15:03:10 +03006017 kvm_state.hdr.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
Sean Christopherson55d23752018-12-03 13:53:18 -08006018
6019 if (is_guest_mode(vcpu)) {
6020 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
6021
6022 if (vmx->nested.nested_run_pending)
6023 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
Oliver Upton5ef8acb2020-02-07 02:36:07 -08006024
6025 if (vmx->nested.mtf_pending)
6026 kvm_state.flags |= KVM_STATE_NESTED_MTF_PENDING;
Peter Shier850448f2020-05-26 14:51:06 -07006027
6028 if (nested_cpu_has_preemption_timer(vmcs12) &&
6029 vmx->nested.has_preemption_timer_deadline) {
6030 kvm_state.hdr.vmx.flags |=
6031 KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE;
6032 kvm_state.hdr.vmx.preemption_timer_deadline =
6033 vmx->nested.preemption_timer_deadline;
6034 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006035 }
6036 }
6037
6038 if (user_data_size < kvm_state.size)
6039 goto out;
6040
6041 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
6042 return -EFAULT;
6043
6044 if (!vmx_has_valid_vmcs12(vcpu))
6045 goto out;
6046
6047 /*
6048 * When running L2, the authoritative vmcs12 state is in the
6049 * vmcs02. When running L1, the authoritative vmcs12 state is
6050 * in the shadow or enlightened vmcs linked to vmcs01, unless
Sean Christopherson3731905ef2019-05-07 08:36:27 -07006051 * need_vmcs12_to_shadow_sync is set, in which case, the authoritative
Sean Christopherson55d23752018-12-03 13:53:18 -08006052 * vmcs12 state is in the vmcs12 already.
6053 */
6054 if (is_guest_mode(vcpu)) {
Sean Christopherson3731905ef2019-05-07 08:36:27 -07006055 sync_vmcs02_to_vmcs12(vcpu, vmcs12);
Sean Christopherson7952d762019-05-07 08:36:29 -07006056 sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
Sean Christopherson3731905ef2019-05-07 08:36:27 -07006057 } else if (!vmx->nested.need_vmcs12_to_shadow_sync) {
Sean Christopherson55d23752018-12-03 13:53:18 -08006058 if (vmx->nested.hv_evmcs)
6059 copy_enlightened_to_vmcs12(vmx);
6060 else if (enable_shadow_vmcs)
6061 copy_shadow_to_vmcs12(vmx);
6062 }
6063
Liran Alon6ca00df2019-06-16 15:03:10 +03006064 BUILD_BUG_ON(sizeof(user_vmx_nested_state->vmcs12) < VMCS12_SIZE);
6065 BUILD_BUG_ON(sizeof(user_vmx_nested_state->shadow_vmcs12) < VMCS12_SIZE);
6066
Tom Roeder3a33d032019-01-24 13:48:20 -08006067 /*
6068 * Copy over the full allocated size of vmcs12 rather than just the size
6069 * of the struct.
6070 */
Liran Alon6ca00df2019-06-16 15:03:10 +03006071 if (copy_to_user(user_vmx_nested_state->vmcs12, vmcs12, VMCS12_SIZE))
Sean Christopherson55d23752018-12-03 13:53:18 -08006072 return -EFAULT;
6073
6074 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
6075 vmcs12->vmcs_link_pointer != -1ull) {
Liran Alon6ca00df2019-06-16 15:03:10 +03006076 if (copy_to_user(user_vmx_nested_state->shadow_vmcs12,
Tom Roeder3a33d032019-01-24 13:48:20 -08006077 get_shadow_vmcs12(vcpu), VMCS12_SIZE))
Sean Christopherson55d23752018-12-03 13:53:18 -08006078 return -EFAULT;
6079 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006080out:
6081 return kvm_state.size;
6082}
6083
6084/*
6085 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
6086 */
6087void vmx_leave_nested(struct kvm_vcpu *vcpu)
6088{
6089 if (is_guest_mode(vcpu)) {
6090 to_vmx(vcpu)->nested.nested_run_pending = 0;
6091 nested_vmx_vmexit(vcpu, -1, 0, 0);
6092 }
6093 free_nested(vcpu);
6094}
6095
6096static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
6097 struct kvm_nested_state __user *user_kvm_nested_state,
6098 struct kvm_nested_state *kvm_state)
6099{
6100 struct vcpu_vmx *vmx = to_vmx(vcpu);
6101 struct vmcs12 *vmcs12;
Sean Christopherson68cda402020-05-11 15:05:29 -07006102 enum vm_entry_failure_code ignored;
Liran Alon6ca00df2019-06-16 15:03:10 +03006103 struct kvm_vmx_nested_state_data __user *user_vmx_nested_state =
6104 &user_kvm_nested_state->data.vmx[0];
Sean Christopherson55d23752018-12-03 13:53:18 -08006105 int ret;
6106
Liran Alon6ca00df2019-06-16 15:03:10 +03006107 if (kvm_state->format != KVM_STATE_NESTED_FORMAT_VMX)
Sean Christopherson55d23752018-12-03 13:53:18 -08006108 return -EINVAL;
6109
Liran Alon6ca00df2019-06-16 15:03:10 +03006110 if (kvm_state->hdr.vmx.vmxon_pa == -1ull) {
6111 if (kvm_state->hdr.vmx.smm.flags)
Sean Christopherson55d23752018-12-03 13:53:18 -08006112 return -EINVAL;
6113
Liran Alon6ca00df2019-06-16 15:03:10 +03006114 if (kvm_state->hdr.vmx.vmcs12_pa != -1ull)
Sean Christopherson55d23752018-12-03 13:53:18 -08006115 return -EINVAL;
6116
Liran Alon323d73a2019-06-26 16:09:27 +03006117 /*
6118 * KVM_STATE_NESTED_EVMCS used to signal that KVM should
6119 * enable eVMCS capability on vCPU. However, since then
6120 * code was changed such that flag signals vmcs12 should
6121 * be copied into eVMCS in guest memory.
6122 *
6123 * To preserve backwards compatability, allow user
6124 * to set this flag even when there is no VMXON region.
6125 */
Paolo Bonzini9fd58872019-06-19 16:52:27 +02006126 if (kvm_state->flags & ~KVM_STATE_NESTED_EVMCS)
6127 return -EINVAL;
6128 } else {
6129 if (!nested_vmx_allowed(vcpu))
6130 return -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08006131
Paolo Bonzini9fd58872019-06-19 16:52:27 +02006132 if (!page_address_valid(vcpu, kvm_state->hdr.vmx.vmxon_pa))
6133 return -EINVAL;
Liran Alon323d73a2019-06-26 16:09:27 +03006134 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006135
Liran Alon6ca00df2019-06-16 15:03:10 +03006136 if ((kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
Sean Christopherson55d23752018-12-03 13:53:18 -08006137 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
6138 return -EINVAL;
6139
Liran Alon6ca00df2019-06-16 15:03:10 +03006140 if (kvm_state->hdr.vmx.smm.flags &
Sean Christopherson55d23752018-12-03 13:53:18 -08006141 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
6142 return -EINVAL;
6143
Paolo Bonzini5e105c82020-07-27 08:55:09 -04006144 if (kvm_state->hdr.vmx.flags & ~KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE)
6145 return -EINVAL;
6146
Sean Christopherson55d23752018-12-03 13:53:18 -08006147 /*
6148 * SMM temporarily disables VMX, so we cannot be in guest mode,
6149 * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags
6150 * must be zero.
6151 */
Liran Alon65b712f12019-06-25 14:26:42 +03006152 if (is_smm(vcpu) ?
6153 (kvm_state->flags &
6154 (KVM_STATE_NESTED_GUEST_MODE | KVM_STATE_NESTED_RUN_PENDING))
6155 : kvm_state->hdr.vmx.smm.flags)
Sean Christopherson55d23752018-12-03 13:53:18 -08006156 return -EINVAL;
6157
Liran Alon6ca00df2019-06-16 15:03:10 +03006158 if ((kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
6159 !(kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
Sean Christopherson55d23752018-12-03 13:53:18 -08006160 return -EINVAL;
6161
Liran Alon323d73a2019-06-26 16:09:27 +03006162 if ((kvm_state->flags & KVM_STATE_NESTED_EVMCS) &&
6163 (!nested_vmx_allowed(vcpu) || !vmx->nested.enlightened_vmcs_enabled))
Paolo Bonzini9fd58872019-06-19 16:52:27 +02006164 return -EINVAL;
6165
Liran Alon323d73a2019-06-26 16:09:27 +03006166 vmx_leave_nested(vcpu);
Paolo Bonzini9fd58872019-06-19 16:52:27 +02006167
Liran Alon6ca00df2019-06-16 15:03:10 +03006168 if (kvm_state->hdr.vmx.vmxon_pa == -1ull)
Sean Christopherson55d23752018-12-03 13:53:18 -08006169 return 0;
6170
Liran Alon6ca00df2019-06-16 15:03:10 +03006171 vmx->nested.vmxon_ptr = kvm_state->hdr.vmx.vmxon_pa;
Sean Christopherson55d23752018-12-03 13:53:18 -08006172 ret = enter_vmx_operation(vcpu);
6173 if (ret)
6174 return ret;
6175
Paolo Bonzini0f02bd02020-07-27 09:00:37 -04006176 /* Empty 'VMXON' state is permitted if no VMCS loaded */
6177 if (kvm_state->size < sizeof(*kvm_state) + sizeof(*vmcs12)) {
6178 /* See vmx_has_valid_vmcs12. */
6179 if ((kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE) ||
6180 (kvm_state->flags & KVM_STATE_NESTED_EVMCS) ||
6181 (kvm_state->hdr.vmx.vmcs12_pa != -1ull))
6182 return -EINVAL;
6183 else
6184 return 0;
6185 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006186
Liran Alon6ca00df2019-06-16 15:03:10 +03006187 if (kvm_state->hdr.vmx.vmcs12_pa != -1ull) {
6188 if (kvm_state->hdr.vmx.vmcs12_pa == kvm_state->hdr.vmx.vmxon_pa ||
6189 !page_address_valid(vcpu, kvm_state->hdr.vmx.vmcs12_pa))
Sean Christopherson55d23752018-12-03 13:53:18 -08006190 return -EINVAL;
6191
Liran Alon6ca00df2019-06-16 15:03:10 +03006192 set_current_vmptr(vmx, kvm_state->hdr.vmx.vmcs12_pa);
Sean Christopherson55d23752018-12-03 13:53:18 -08006193 } else if (kvm_state->flags & KVM_STATE_NESTED_EVMCS) {
6194 /*
Vitaly Kuznetsove942dbf2020-03-09 16:52:12 +01006195 * nested_vmx_handle_enlightened_vmptrld() cannot be called
6196 * directly from here as HV_X64_MSR_VP_ASSIST_PAGE may not be
6197 * restored yet. EVMCS will be mapped from
6198 * nested_get_vmcs12_pages().
Sean Christopherson55d23752018-12-03 13:53:18 -08006199 */
Vitaly Kuznetsove942dbf2020-03-09 16:52:12 +01006200 kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08006201 } else {
6202 return -EINVAL;
6203 }
6204
Liran Alon6ca00df2019-06-16 15:03:10 +03006205 if (kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
Sean Christopherson55d23752018-12-03 13:53:18 -08006206 vmx->nested.smm.vmxon = true;
6207 vmx->nested.vmxon = false;
6208
Liran Alon6ca00df2019-06-16 15:03:10 +03006209 if (kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
Sean Christopherson55d23752018-12-03 13:53:18 -08006210 vmx->nested.smm.guest_mode = true;
6211 }
6212
6213 vmcs12 = get_vmcs12(vcpu);
Liran Alon6ca00df2019-06-16 15:03:10 +03006214 if (copy_from_user(vmcs12, user_vmx_nested_state->vmcs12, sizeof(*vmcs12)))
Sean Christopherson55d23752018-12-03 13:53:18 -08006215 return -EFAULT;
6216
6217 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
6218 return -EINVAL;
6219
6220 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
6221 return 0;
6222
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006223 vmx->nested.nested_run_pending =
6224 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
6225
Oliver Upton5ef8acb2020-02-07 02:36:07 -08006226 vmx->nested.mtf_pending =
6227 !!(kvm_state->flags & KVM_STATE_NESTED_MTF_PENDING);
6228
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006229 ret = -EINVAL;
Sean Christopherson55d23752018-12-03 13:53:18 -08006230 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
6231 vmcs12->vmcs_link_pointer != -1ull) {
6232 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
6233
Liran Alon6ca00df2019-06-16 15:03:10 +03006234 if (kvm_state->size <
6235 sizeof(*kvm_state) +
6236 sizeof(user_vmx_nested_state->vmcs12) + sizeof(*shadow_vmcs12))
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006237 goto error_guest_mode;
Sean Christopherson55d23752018-12-03 13:53:18 -08006238
6239 if (copy_from_user(shadow_vmcs12,
Liran Alon6ca00df2019-06-16 15:03:10 +03006240 user_vmx_nested_state->shadow_vmcs12,
6241 sizeof(*shadow_vmcs12))) {
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006242 ret = -EFAULT;
6243 goto error_guest_mode;
6244 }
Sean Christopherson55d23752018-12-03 13:53:18 -08006245
6246 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
6247 !shadow_vmcs12->hdr.shadow_vmcs)
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006248 goto error_guest_mode;
Sean Christopherson55d23752018-12-03 13:53:18 -08006249 }
6250
Paolo Bonzini83d31e52020-07-09 13:12:09 -04006251 vmx->nested.has_preemption_timer_deadline = false;
Peter Shier850448f2020-05-26 14:51:06 -07006252 if (kvm_state->hdr.vmx.flags & KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE) {
6253 vmx->nested.has_preemption_timer_deadline = true;
6254 vmx->nested.preemption_timer_deadline =
6255 kvm_state->hdr.vmx.preemption_timer_deadline;
6256 }
6257
Sean Christopherson5478ba32019-04-11 12:18:06 -07006258 if (nested_vmx_check_controls(vcpu, vmcs12) ||
6259 nested_vmx_check_host_state(vcpu, vmcs12) ||
Sean Christopherson68cda402020-05-11 15:05:29 -07006260 nested_vmx_check_guest_state(vcpu, vmcs12, &ignored))
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006261 goto error_guest_mode;
Sean Christopherson55d23752018-12-03 13:53:18 -08006262
6263 vmx->nested.dirty_vmcs12 = true;
6264 ret = nested_vmx_enter_non_root_mode(vcpu, false);
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006265 if (ret)
6266 goto error_guest_mode;
Sean Christopherson55d23752018-12-03 13:53:18 -08006267
6268 return 0;
Sean Christopherson21be4ca2019-05-08 11:04:32 -07006269
6270error_guest_mode:
6271 vmx->nested.nested_run_pending = 0;
6272 return ret;
Sean Christopherson55d23752018-12-03 13:53:18 -08006273}
6274
Xiaoyao Li1b842922019-10-20 17:11:01 +08006275void nested_vmx_set_vmcs_shadowing_bitmap(void)
Sean Christopherson55d23752018-12-03 13:53:18 -08006276{
6277 if (enable_shadow_vmcs) {
Sean Christopherson55d23752018-12-03 13:53:18 -08006278 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Sean Christophersonfadcead2019-05-07 08:36:23 -07006279 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
Sean Christopherson55d23752018-12-03 13:53:18 -08006280 }
6281}
6282
6283/*
6284 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
6285 * returned for the various VMX controls MSRs when nested VMX is enabled.
6286 * The same values should also be used to verify that vmcs12 control fields are
6287 * valid during nested entry from L1 to L2.
6288 * Each of these control msrs has a low and high 32-bit half: A low bit is on
6289 * if the corresponding bit in the (32-bit) control field *must* be on, and a
6290 * bit in the high half is on if the corresponding bit in the control field
6291 * may be on. See also vmx_control_verify().
6292 */
Vitaly Kuznetsova4443262020-02-20 18:22:04 +01006293void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, u32 ept_caps)
Sean Christopherson55d23752018-12-03 13:53:18 -08006294{
6295 /*
6296 * Note that as a general rule, the high half of the MSRs (bits in
6297 * the control fields which may be 1) should be initialized by the
6298 * intersection of the underlying hardware's MSR (i.e., features which
6299 * can be supported) and the list of features we want to expose -
6300 * because they are known to be properly supported in our code.
6301 * Also, usually, the low half of the MSRs (bits which must be 1) can
6302 * be set to 0, meaning that L1 may turn off any of these bits. The
6303 * reason is that if one of these bits is necessary, it will appear
6304 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
6305 * fields of vmcs01 and vmcs02, will turn these bits off - and
Sean Christopherson2c1f3322020-04-15 10:55:14 -07006306 * nested_vmx_l1_wants_exit() will not pass related exits to L1.
Sean Christopherson55d23752018-12-03 13:53:18 -08006307 * These rules have exceptions below.
6308 */
6309
6310 /* pin-based controls */
6311 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
6312 msrs->pinbased_ctls_low,
6313 msrs->pinbased_ctls_high);
6314 msrs->pinbased_ctls_low |=
6315 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
6316 msrs->pinbased_ctls_high &=
6317 PIN_BASED_EXT_INTR_MASK |
6318 PIN_BASED_NMI_EXITING |
6319 PIN_BASED_VIRTUAL_NMIS |
Vitaly Kuznetsova4443262020-02-20 18:22:04 +01006320 (enable_apicv ? PIN_BASED_POSTED_INTR : 0);
Sean Christopherson55d23752018-12-03 13:53:18 -08006321 msrs->pinbased_ctls_high |=
6322 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
6323 PIN_BASED_VMX_PREEMPTION_TIMER;
6324
6325 /* exit controls */
6326 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
6327 msrs->exit_ctls_low,
6328 msrs->exit_ctls_high);
6329 msrs->exit_ctls_low =
6330 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
6331
6332 msrs->exit_ctls_high &=
6333#ifdef CONFIG_X86_64
6334 VM_EXIT_HOST_ADDR_SPACE_SIZE |
6335#endif
Chenyi Qiangefc83132020-08-28 16:56:18 +08006336 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT |
6337 VM_EXIT_CLEAR_BNDCFGS | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
Sean Christopherson55d23752018-12-03 13:53:18 -08006338 msrs->exit_ctls_high |=
6339 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
6340 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
6341 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
6342
6343 /* We support free control of debug control saving. */
6344 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
6345
6346 /* entry controls */
6347 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
6348 msrs->entry_ctls_low,
6349 msrs->entry_ctls_high);
6350 msrs->entry_ctls_low =
6351 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
6352 msrs->entry_ctls_high &=
6353#ifdef CONFIG_X86_64
6354 VM_ENTRY_IA32E_MODE |
6355#endif
Chenyi Qiangefc83132020-08-28 16:56:18 +08006356 VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS |
6357 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
Sean Christopherson55d23752018-12-03 13:53:18 -08006358 msrs->entry_ctls_high |=
6359 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
6360
6361 /* We support free control of debug control loading. */
6362 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
6363
6364 /* cpu-based controls */
6365 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
6366 msrs->procbased_ctls_low,
6367 msrs->procbased_ctls_high);
6368 msrs->procbased_ctls_low =
6369 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
6370 msrs->procbased_ctls_high &=
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08006371 CPU_BASED_INTR_WINDOW_EXITING |
Xiaoyao Li5e3d3942019-12-06 16:45:26 +08006372 CPU_BASED_NMI_WINDOW_EXITING | CPU_BASED_USE_TSC_OFFSETTING |
Sean Christopherson55d23752018-12-03 13:53:18 -08006373 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
6374 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
6375 CPU_BASED_CR3_STORE_EXITING |
6376#ifdef CONFIG_X86_64
6377 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
6378#endif
6379 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
6380 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
6381 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
6382 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
6383 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
6384 /*
6385 * We can allow some features even when not supported by the
6386 * hardware. For example, L1 can specify an MSR bitmap - and we
6387 * can use it to avoid exits to L1 - even when L0 runs L2
6388 * without MSR bitmaps.
6389 */
6390 msrs->procbased_ctls_high |=
6391 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
6392 CPU_BASED_USE_MSR_BITMAPS;
6393
6394 /* We support free control of CR3 access interception. */
6395 msrs->procbased_ctls_low &=
6396 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
6397
6398 /*
6399 * secondary cpu-based controls. Do not include those that
Xiaoyao Li7c1b7612020-07-09 12:34:25 +08006400 * depend on CPUID bits, they are added later by
6401 * vmx_vcpu_after_set_cpuid.
Sean Christopherson55d23752018-12-03 13:53:18 -08006402 */
Vitaly Kuznetsov6b1971c2019-02-07 11:42:14 +01006403 if (msrs->procbased_ctls_high & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)
6404 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
6405 msrs->secondary_ctls_low,
6406 msrs->secondary_ctls_high);
6407
Sean Christopherson55d23752018-12-03 13:53:18 -08006408 msrs->secondary_ctls_low = 0;
6409 msrs->secondary_ctls_high &=
6410 SECONDARY_EXEC_DESC |
Sean Christopherson7f3603b2020-09-23 09:50:47 -07006411 SECONDARY_EXEC_ENABLE_RDTSCP |
Sean Christopherson55d23752018-12-03 13:53:18 -08006412 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini6defc592019-07-02 14:39:29 +02006413 SECONDARY_EXEC_WBINVD_EXITING |
Sean Christopherson55d23752018-12-03 13:53:18 -08006414 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6415 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini6defc592019-07-02 14:39:29 +02006416 SECONDARY_EXEC_RDRAND_EXITING |
6417 SECONDARY_EXEC_ENABLE_INVPCID |
6418 SECONDARY_EXEC_RDSEED_EXITING |
6419 SECONDARY_EXEC_XSAVES;
Sean Christopherson55d23752018-12-03 13:53:18 -08006420
6421 /*
6422 * We can emulate "VMCS shadowing," even if the hardware
6423 * doesn't support it.
6424 */
6425 msrs->secondary_ctls_high |=
6426 SECONDARY_EXEC_SHADOW_VMCS;
6427
6428 if (enable_ept) {
6429 /* nested EPT: emulate EPT also to L1 */
6430 msrs->secondary_ctls_high |=
6431 SECONDARY_EXEC_ENABLE_EPT;
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08006432 msrs->ept_caps =
6433 VMX_EPT_PAGE_WALK_4_BIT |
6434 VMX_EPT_PAGE_WALK_5_BIT |
6435 VMX_EPTP_WB_BIT |
Sean Christopherson96d47012020-03-02 18:02:40 -08006436 VMX_EPT_INVEPT_BIT |
6437 VMX_EPT_EXECUTE_ONLY_BIT;
6438
Sean Christopherson55d23752018-12-03 13:53:18 -08006439 msrs->ept_caps &= ept_caps;
6440 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
6441 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
6442 VMX_EPT_1GB_PAGE_BIT;
6443 if (enable_ept_ad_bits) {
6444 msrs->secondary_ctls_high |=
6445 SECONDARY_EXEC_ENABLE_PML;
6446 msrs->ept_caps |= VMX_EPT_AD_BIT;
6447 }
6448 }
6449
6450 if (cpu_has_vmx_vmfunc()) {
6451 msrs->secondary_ctls_high |=
6452 SECONDARY_EXEC_ENABLE_VMFUNC;
6453 /*
6454 * Advertise EPTP switching unconditionally
6455 * since we emulate it
6456 */
6457 if (enable_ept)
6458 msrs->vmfunc_controls =
6459 VMX_VMFUNC_EPTP_SWITCHING;
6460 }
6461
6462 /*
6463 * Old versions of KVM use the single-context version without
6464 * checking for support, so declare that it is supported even
6465 * though it is treated as global context. The alternative is
6466 * not failing the single-context invvpid, and it is worse.
6467 */
6468 if (enable_vpid) {
6469 msrs->secondary_ctls_high |=
6470 SECONDARY_EXEC_ENABLE_VPID;
6471 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
6472 VMX_VPID_EXTENT_SUPPORTED_MASK;
6473 }
6474
6475 if (enable_unrestricted_guest)
6476 msrs->secondary_ctls_high |=
6477 SECONDARY_EXEC_UNRESTRICTED_GUEST;
6478
6479 if (flexpriority_enabled)
6480 msrs->secondary_ctls_high |=
6481 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6482
6483 /* miscellaneous data */
6484 rdmsr(MSR_IA32_VMX_MISC,
6485 msrs->misc_low,
6486 msrs->misc_high);
6487 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
6488 msrs->misc_low |=
6489 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
6490 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
6491 VMX_MISC_ACTIVITY_HLT;
6492 msrs->misc_high = 0;
6493
6494 /*
6495 * This MSR reports some information about VMX support. We
6496 * should return information about the VMX we emulate for the
6497 * guest, and the VMCS structure we give it - not about the
6498 * VMX support of the underlying hardware.
6499 */
6500 msrs->basic =
6501 VMCS12_REVISION |
6502 VMX_BASIC_TRUE_CTLS |
6503 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
6504 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
6505
6506 if (cpu_has_vmx_basic_inout())
6507 msrs->basic |= VMX_BASIC_INOUT;
6508
6509 /*
6510 * These MSRs specify bits which the guest must keep fixed on
6511 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
6512 * We picked the standard core2 setting.
6513 */
6514#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
6515#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
6516 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
6517 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
6518
6519 /* These MSRs specify bits which the guest must keep fixed off. */
6520 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
6521 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
6522
6523 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
6524 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
6525}
6526
6527void nested_vmx_hardware_unsetup(void)
6528{
6529 int i;
6530
6531 if (enable_shadow_vmcs) {
6532 for (i = 0; i < VMX_BITMAP_NR; i++)
6533 free_page((unsigned long)vmx_bitmap[i]);
6534 }
6535}
6536
Sean Christopherson6c1c6e52020-05-06 13:46:53 -07006537__init int nested_vmx_hardware_setup(int (*exit_handlers[])(struct kvm_vcpu *))
Sean Christopherson55d23752018-12-03 13:53:18 -08006538{
6539 int i;
6540
6541 if (!cpu_has_vmx_shadow_vmcs())
6542 enable_shadow_vmcs = 0;
6543 if (enable_shadow_vmcs) {
6544 for (i = 0; i < VMX_BITMAP_NR; i++) {
Ben Gardon41836832019-02-11 11:02:52 -08006545 /*
6546 * The vmx_bitmap is not tied to a VM and so should
6547 * not be charged to a memcg.
6548 */
Sean Christopherson55d23752018-12-03 13:53:18 -08006549 vmx_bitmap[i] = (unsigned long *)
6550 __get_free_page(GFP_KERNEL);
6551 if (!vmx_bitmap[i]) {
6552 nested_vmx_hardware_unsetup();
6553 return -ENOMEM;
6554 }
6555 }
6556
6557 init_vmcs_shadow_fields();
6558 }
6559
Liran Aloncc877672019-11-18 21:11:21 +02006560 exit_handlers[EXIT_REASON_VMCLEAR] = handle_vmclear;
6561 exit_handlers[EXIT_REASON_VMLAUNCH] = handle_vmlaunch;
6562 exit_handlers[EXIT_REASON_VMPTRLD] = handle_vmptrld;
6563 exit_handlers[EXIT_REASON_VMPTRST] = handle_vmptrst;
6564 exit_handlers[EXIT_REASON_VMREAD] = handle_vmread;
6565 exit_handlers[EXIT_REASON_VMRESUME] = handle_vmresume;
6566 exit_handlers[EXIT_REASON_VMWRITE] = handle_vmwrite;
6567 exit_handlers[EXIT_REASON_VMOFF] = handle_vmoff;
6568 exit_handlers[EXIT_REASON_VMON] = handle_vmon;
6569 exit_handlers[EXIT_REASON_INVEPT] = handle_invept;
6570 exit_handlers[EXIT_REASON_INVVPID] = handle_invvpid;
6571 exit_handlers[EXIT_REASON_VMFUNC] = handle_vmfunc;
Sean Christopherson55d23752018-12-03 13:53:18 -08006572
Sean Christopherson55d23752018-12-03 13:53:18 -08006573 return 0;
6574}
Paolo Bonzini33b22172020-04-17 10:24:18 -04006575
6576struct kvm_x86_nested_ops vmx_nested_ops = {
6577 .check_events = vmx_check_nested_events,
Sean Christophersond2060bd2020-04-22 19:25:39 -07006578 .hv_timer_pending = nested_vmx_preemption_timer_pending,
Paolo Bonzini33b22172020-04-17 10:24:18 -04006579 .get_state = vmx_get_nested_state,
6580 .set_state = vmx_set_nested_state,
6581 .get_vmcs12_pages = nested_get_vmcs12_pages,
Sean Christopherson02f5fb22020-06-22 14:58:32 -07006582 .write_log_dirty = nested_vmx_write_pml_buffer,
Paolo Bonzini33b22172020-04-17 10:24:18 -04006583 .enable_evmcs = nested_enable_evmcs,
6584 .get_evmcs_version = nested_get_evmcs_version,
6585};