1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Kernel-based Virtual Machine driver for Linux
4 *
5 * AMD SVM support
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
8 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
9 *
10 * Authors:
11 * Yaniv Kamay <yaniv@qumranet.com>
12 * Avi Kivity <avi@qumranet.com>
13 */
14
15 #define pr_fmt(fmt) "SVM: " fmt
16
17 #include <linux/kvm_types.h>
18 #include <linux/kvm_host.h>
19 #include <linux/kernel.h>
20
21 #include <asm/msr-index.h>
22 #include <asm/debugreg.h>
23
24 #include "kvm_emulate.h"
25 #include "trace.h"
26 #include "mmu.h"
27 #include "x86.h"
28 #include "cpuid.h"
29 #include "lapic.h"
30 #include "svm.h"
31 #include "hyperv.h"
32
33 #define CC KVM_NESTED_VMENTER_CONSISTENCY_CHECK
34
nested_svm_inject_npf_exit(struct kvm_vcpu * vcpu,struct x86_exception * fault)35 static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
36 struct x86_exception *fault)
37 {
38 struct vcpu_svm *svm = to_svm(vcpu);
39 struct vmcb *vmcb = svm->vmcb;
40
41 if (vmcb->control.exit_code != SVM_EXIT_NPF) {
42 /*
43 * TODO: track the cause of the nested page fault, and
44 * correctly fill in the high bits of exit_info_1.
45 */
46 vmcb->control.exit_code = SVM_EXIT_NPF;
47 vmcb->control.exit_code_hi = 0;
48 vmcb->control.exit_info_1 = (1ULL << 32);
49 vmcb->control.exit_info_2 = fault->address;
50 }
51
52 vmcb->control.exit_info_1 &= ~0xffffffffULL;
53 vmcb->control.exit_info_1 |= fault->error_code;
54
55 nested_svm_vmexit(svm);
56 }
57
nested_svm_get_tdp_pdptr(struct kvm_vcpu * vcpu,int index)58 static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
59 {
60 struct vcpu_svm *svm = to_svm(vcpu);
61 u64 cr3 = svm->nested.ctl.nested_cr3;
62 u64 pdpte;
63 int ret;
64
65 ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(cr3), &pdpte,
66 offset_in_page(cr3) + index * 8, 8);
67 if (ret)
68 return 0;
69 return pdpte;
70 }
71
nested_svm_get_tdp_cr3(struct kvm_vcpu * vcpu)72 static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
73 {
74 struct vcpu_svm *svm = to_svm(vcpu);
75
76 return svm->nested.ctl.nested_cr3;
77 }
78
nested_svm_init_mmu_context(struct kvm_vcpu * vcpu)79 static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
80 {
81 struct vcpu_svm *svm = to_svm(vcpu);
82
83 WARN_ON(mmu_is_nested(vcpu));
84
85 vcpu->arch.mmu = &vcpu->arch.guest_mmu;
86
87 /*
88 * The NPT format depends on L1's CR4 and EFER, which is in vmcb01. Note,
89 * when called via KVM_SET_NESTED_STATE, that state may _not_ match current
90 * vCPU state. CR0.WP is explicitly ignored, while CR0.PG is required.
91 */
92 kvm_init_shadow_npt_mmu(vcpu, X86_CR0_PG, svm->vmcb01.ptr->save.cr4,
93 svm->vmcb01.ptr->save.efer,
94 svm->nested.ctl.nested_cr3);
95 vcpu->arch.mmu->get_guest_pgd = nested_svm_get_tdp_cr3;
96 vcpu->arch.mmu->get_pdptr = nested_svm_get_tdp_pdptr;
97 vcpu->arch.mmu->inject_page_fault = nested_svm_inject_npf_exit;
98 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
99 }
100
nested_svm_uninit_mmu_context(struct kvm_vcpu * vcpu)101 static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
102 {
103 vcpu->arch.mmu = &vcpu->arch.root_mmu;
104 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
105 }
106
nested_vmcb_needs_vls_intercept(struct vcpu_svm * svm)107 static bool nested_vmcb_needs_vls_intercept(struct vcpu_svm *svm)
108 {
109 if (!svm->v_vmload_vmsave_enabled)
110 return true;
111
112 if (!nested_npt_enabled(svm))
113 return true;
114
115 if (!(svm->nested.ctl.virt_ext & VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK))
116 return true;
117
118 return false;
119 }
120
recalc_intercepts(struct vcpu_svm * svm)121 void recalc_intercepts(struct vcpu_svm *svm)
122 {
123 struct vmcb_control_area *c, *h;
124 struct vmcb_ctrl_area_cached *g;
125 unsigned int i;
126
127 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
128
129 if (!is_guest_mode(&svm->vcpu))
130 return;
131
132 c = &svm->vmcb->control;
133 h = &svm->vmcb01.ptr->control;
134 g = &svm->nested.ctl;
135
136 for (i = 0; i < MAX_INTERCEPT; i++)
137 c->intercepts[i] = h->intercepts[i];
138
139 if (g->int_ctl & V_INTR_MASKING_MASK) {
140 /* We only want the cr8 intercept bits of L1 */
141 vmcb_clr_intercept(c, INTERCEPT_CR8_READ);
142 vmcb_clr_intercept(c, INTERCEPT_CR8_WRITE);
143
144 /*
145 * Once running L2 with HF_VINTR_MASK, EFLAGS.IF does not
146 * affect any interrupt we may want to inject; therefore,
147 * interrupt window vmexits are irrelevant to L0.
148 */
149 vmcb_clr_intercept(c, INTERCEPT_VINTR);
150 }
151
152 /* We don't want to see VMMCALLs from a nested guest */
153 vmcb_clr_intercept(c, INTERCEPT_VMMCALL);
154
155 for (i = 0; i < MAX_INTERCEPT; i++)
156 c->intercepts[i] |= g->intercepts[i];
157
158 /* If SMI is not intercepted, ignore guest SMI intercept as well */
159 if (!intercept_smi)
160 vmcb_clr_intercept(c, INTERCEPT_SMI);
161
162 if (nested_vmcb_needs_vls_intercept(svm)) {
163 /*
164 * If the virtual VMLOAD/VMSAVE is not enabled for the L2,
165 * we must intercept these instructions to correctly
166 * emulate them in case L1 doesn't intercept them.
167 */
168 vmcb_set_intercept(c, INTERCEPT_VMLOAD);
169 vmcb_set_intercept(c, INTERCEPT_VMSAVE);
170 } else {
171 WARN_ON(!(c->virt_ext & VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK));
172 }
173 }
174
175 /*
176 * Merge L0's (KVM) and L1's (Nested VMCB) MSR permission bitmaps. The function
177 * is optimized in that it only merges the parts where KVM MSR permission bitmap
178 * may contain zero bits.
179 */
nested_svm_vmrun_msrpm(struct vcpu_svm * svm)180 static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
181 {
182 struct hv_enlightenments *hve =
183 (struct hv_enlightenments *)svm->nested.ctl.reserved_sw;
184 int i;
185
186 /*
187 * MSR bitmap update can be skipped when:
188 * - MSR bitmap for L1 hasn't changed.
189 * - Nested hypervisor (L1) is attempting to launch the same L2 as
190 * before.
191 * - Nested hypervisor (L1) is using Hyper-V emulation interface and
192 * tells KVM (L0) there were no changes in MSR bitmap for L2.
193 */
194 if (!svm->nested.force_msr_bitmap_recalc &&
195 kvm_hv_hypercall_enabled(&svm->vcpu) &&
196 hve->hv_enlightenments_control.msr_bitmap &&
197 (svm->nested.ctl.clean & BIT(VMCB_HV_NESTED_ENLIGHTENMENTS)))
198 goto set_msrpm_base_pa;
199
200 if (!(vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
201 return true;
202
203 for (i = 0; i < MSRPM_OFFSETS; i++) {
204 u32 value, p;
205 u64 offset;
206
207 if (msrpm_offsets[i] == 0xffffffff)
208 break;
209
210 p = msrpm_offsets[i];
211
212 /* x2apic msrs are intercepted always for the nested guest */
213 if (is_x2apic_msrpm_offset(p))
214 continue;
215
216 offset = svm->nested.ctl.msrpm_base_pa + (p * 4);
217
218 if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
219 return false;
220
221 svm->nested.msrpm[p] = svm->msrpm[p] | value;
222 }
223
224 svm->nested.force_msr_bitmap_recalc = false;
225
226 set_msrpm_base_pa:
227 svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm));
228
229 return true;
230 }
231
232 /*
233 * Bits 11:0 of bitmap address are ignored by hardware
234 */
nested_svm_check_bitmap_pa(struct kvm_vcpu * vcpu,u64 pa,u32 size)235 static bool nested_svm_check_bitmap_pa(struct kvm_vcpu *vcpu, u64 pa, u32 size)
236 {
237 u64 addr = PAGE_ALIGN(pa);
238
239 return kvm_vcpu_is_legal_gpa(vcpu, addr) &&
240 kvm_vcpu_is_legal_gpa(vcpu, addr + size - 1);
241 }
242
nested_svm_check_tlb_ctl(struct kvm_vcpu * vcpu,u8 tlb_ctl)243 static bool nested_svm_check_tlb_ctl(struct kvm_vcpu *vcpu, u8 tlb_ctl)
244 {
245 /* Nested FLUSHBYASID is not supported yet. */
246 switch(tlb_ctl) {
247 case TLB_CONTROL_DO_NOTHING:
248 case TLB_CONTROL_FLUSH_ALL_ASID:
249 return true;
250 default:
251 return false;
252 }
253 }
254
__nested_vmcb_check_controls(struct kvm_vcpu * vcpu,struct vmcb_ctrl_area_cached * control)255 static bool __nested_vmcb_check_controls(struct kvm_vcpu *vcpu,
256 struct vmcb_ctrl_area_cached *control)
257 {
258 if (CC(!vmcb12_is_intercept(control, INTERCEPT_VMRUN)))
259 return false;
260
261 if (CC(control->asid == 0))
262 return false;
263
264 if (CC((control->nested_ctl & SVM_NESTED_CTL_NP_ENABLE) && !npt_enabled))
265 return false;
266
267 if (CC(!nested_svm_check_bitmap_pa(vcpu, control->msrpm_base_pa,
268 MSRPM_SIZE)))
269 return false;
270 if (CC(!nested_svm_check_bitmap_pa(vcpu, control->iopm_base_pa,
271 IOPM_SIZE)))
272 return false;
273
274 if (CC(!nested_svm_check_tlb_ctl(vcpu, control->tlb_ctl)))
275 return false;
276
277 return true;
278 }
279
280 /* Common checks that apply to both L1 and L2 state. */
__nested_vmcb_check_save(struct kvm_vcpu * vcpu,struct vmcb_save_area_cached * save)281 static bool __nested_vmcb_check_save(struct kvm_vcpu *vcpu,
282 struct vmcb_save_area_cached *save)
283 {
284 if (CC(!(save->efer & EFER_SVME)))
285 return false;
286
287 if (CC((save->cr0 & X86_CR0_CD) == 0 && (save->cr0 & X86_CR0_NW)) ||
288 CC(save->cr0 & ~0xffffffffULL))
289 return false;
290
291 if (CC(!kvm_dr6_valid(save->dr6)) || CC(!kvm_dr7_valid(save->dr7)))
292 return false;
293
294 /*
295 * These checks are also performed by KVM_SET_SREGS,
296 * except that EFER.LMA is not checked by SVM against
297 * CR0.PG && EFER.LME.
298 */
299 if ((save->efer & EFER_LME) && (save->cr0 & X86_CR0_PG)) {
300 if (CC(!(save->cr4 & X86_CR4_PAE)) ||
301 CC(!(save->cr0 & X86_CR0_PE)) ||
302 CC(kvm_vcpu_is_illegal_gpa(vcpu, save->cr3)))
303 return false;
304 }
305
306 /* Note, SVM doesn't have any additional restrictions on CR4. */
307 if (CC(!__kvm_is_valid_cr4(vcpu, save->cr4)))
308 return false;
309
310 if (CC(!kvm_valid_efer(vcpu, save->efer)))
311 return false;
312
313 return true;
314 }
315
nested_vmcb_check_save(struct kvm_vcpu * vcpu)316 static bool nested_vmcb_check_save(struct kvm_vcpu *vcpu)
317 {
318 struct vcpu_svm *svm = to_svm(vcpu);
319 struct vmcb_save_area_cached *save = &svm->nested.save;
320
321 return __nested_vmcb_check_save(vcpu, save);
322 }
323
nested_vmcb_check_controls(struct kvm_vcpu * vcpu)324 static bool nested_vmcb_check_controls(struct kvm_vcpu *vcpu)
325 {
326 struct vcpu_svm *svm = to_svm(vcpu);
327 struct vmcb_ctrl_area_cached *ctl = &svm->nested.ctl;
328
329 return __nested_vmcb_check_controls(vcpu, ctl);
330 }
331
332 static
__nested_copy_vmcb_control_to_cache(struct kvm_vcpu * vcpu,struct vmcb_ctrl_area_cached * to,struct vmcb_control_area * from)333 void __nested_copy_vmcb_control_to_cache(struct kvm_vcpu *vcpu,
334 struct vmcb_ctrl_area_cached *to,
335 struct vmcb_control_area *from)
336 {
337 unsigned int i;
338
339 for (i = 0; i < MAX_INTERCEPT; i++)
340 to->intercepts[i] = from->intercepts[i];
341
342 to->iopm_base_pa = from->iopm_base_pa;
343 to->msrpm_base_pa = from->msrpm_base_pa;
344 to->tsc_offset = from->tsc_offset;
345 to->tlb_ctl = from->tlb_ctl;
346 to->int_ctl = from->int_ctl;
347 to->int_vector = from->int_vector;
348 to->int_state = from->int_state;
349 to->exit_code = from->exit_code;
350 to->exit_code_hi = from->exit_code_hi;
351 to->exit_info_1 = from->exit_info_1;
352 to->exit_info_2 = from->exit_info_2;
353 to->exit_int_info = from->exit_int_info;
354 to->exit_int_info_err = from->exit_int_info_err;
355 to->nested_ctl = from->nested_ctl;
356 to->event_inj = from->event_inj;
357 to->event_inj_err = from->event_inj_err;
358 to->next_rip = from->next_rip;
359 to->nested_cr3 = from->nested_cr3;
360 to->virt_ext = from->virt_ext;
361 to->pause_filter_count = from->pause_filter_count;
362 to->pause_filter_thresh = from->pause_filter_thresh;
363
364 /* Copy asid here because nested_vmcb_check_controls will check it. */
365 to->asid = from->asid;
366 to->msrpm_base_pa &= ~0x0fffULL;
367 to->iopm_base_pa &= ~0x0fffULL;
368
369 /* Hyper-V extensions (Enlightened VMCB) */
370 if (kvm_hv_hypercall_enabled(vcpu)) {
371 to->clean = from->clean;
372 memcpy(to->reserved_sw, from->reserved_sw,
373 sizeof(struct hv_enlightenments));
374 }
375 }
376
nested_copy_vmcb_control_to_cache(struct vcpu_svm * svm,struct vmcb_control_area * control)377 void nested_copy_vmcb_control_to_cache(struct vcpu_svm *svm,
378 struct vmcb_control_area *control)
379 {
380 __nested_copy_vmcb_control_to_cache(&svm->vcpu, &svm->nested.ctl, control);
381 }
382
__nested_copy_vmcb_save_to_cache(struct vmcb_save_area_cached * to,struct vmcb_save_area * from)383 static void __nested_copy_vmcb_save_to_cache(struct vmcb_save_area_cached *to,
384 struct vmcb_save_area *from)
385 {
386 /*
387 * Copy only fields that are validated, as we need them
388 * to avoid TOC/TOU races.
389 */
390 to->efer = from->efer;
391 to->cr0 = from->cr0;
392 to->cr3 = from->cr3;
393 to->cr4 = from->cr4;
394
395 to->dr6 = from->dr6;
396 to->dr7 = from->dr7;
397 }
398
nested_copy_vmcb_save_to_cache(struct vcpu_svm * svm,struct vmcb_save_area * save)399 void nested_copy_vmcb_save_to_cache(struct vcpu_svm *svm,
400 struct vmcb_save_area *save)
401 {
402 __nested_copy_vmcb_save_to_cache(&svm->nested.save, save);
403 }
404
405 /*
406 * Synchronize fields that are written by the processor, so that
407 * they can be copied back into the vmcb12.
408 */
nested_sync_control_from_vmcb02(struct vcpu_svm * svm)409 void nested_sync_control_from_vmcb02(struct vcpu_svm *svm)
410 {
411 u32 mask;
412 svm->nested.ctl.event_inj = svm->vmcb->control.event_inj;
413 svm->nested.ctl.event_inj_err = svm->vmcb->control.event_inj_err;
414
415 /* Only a few fields of int_ctl are written by the processor. */
416 mask = V_IRQ_MASK | V_TPR_MASK;
417 if (!(svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK) &&
418 svm_is_intercept(svm, INTERCEPT_VINTR)) {
419 /*
420 * In order to request an interrupt window, L0 is usurping
421 * svm->vmcb->control.int_ctl and possibly setting V_IRQ
422 * even if it was clear in L1's VMCB. Restoring it would be
423 * wrong. However, in this case V_IRQ will remain true until
424 * interrupt_window_interception calls svm_clear_vintr and
425 * restores int_ctl. We can just leave it aside.
426 */
427 mask &= ~V_IRQ_MASK;
428 }
429
430 if (nested_vgif_enabled(svm))
431 mask |= V_GIF_MASK;
432
433 svm->nested.ctl.int_ctl &= ~mask;
434 svm->nested.ctl.int_ctl |= svm->vmcb->control.int_ctl & mask;
435 }
436
437 /*
438 * Transfer any event that L0 or L1 wanted to inject into L2 to
439 * EXIT_INT_INFO.
440 */
nested_save_pending_event_to_vmcb12(struct vcpu_svm * svm,struct vmcb * vmcb12)441 static void nested_save_pending_event_to_vmcb12(struct vcpu_svm *svm,
442 struct vmcb *vmcb12)
443 {
444 struct kvm_vcpu *vcpu = &svm->vcpu;
445 u32 exit_int_info = 0;
446 unsigned int nr;
447
448 if (vcpu->arch.exception.injected) {
449 nr = vcpu->arch.exception.vector;
450 exit_int_info = nr | SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_EXEPT;
451
452 if (vcpu->arch.exception.has_error_code) {
453 exit_int_info |= SVM_EVTINJ_VALID_ERR;
454 vmcb12->control.exit_int_info_err =
455 vcpu->arch.exception.error_code;
456 }
457
458 } else if (vcpu->arch.nmi_injected) {
459 exit_int_info = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
460
461 } else if (vcpu->arch.interrupt.injected) {
462 nr = vcpu->arch.interrupt.nr;
463 exit_int_info = nr | SVM_EVTINJ_VALID;
464
465 if (vcpu->arch.interrupt.soft)
466 exit_int_info |= SVM_EVTINJ_TYPE_SOFT;
467 else
468 exit_int_info |= SVM_EVTINJ_TYPE_INTR;
469 }
470
471 vmcb12->control.exit_int_info = exit_int_info;
472 }
473
nested_svm_transition_tlb_flush(struct kvm_vcpu * vcpu)474 static void nested_svm_transition_tlb_flush(struct kvm_vcpu *vcpu)
475 {
476 /*
477 * TODO: optimize unconditional TLB flush/MMU sync. A partial list of
478 * things to fix before this can be conditional:
479 *
480 * - Flush TLBs for both L1 and L2 remote TLB flush
481 * - Honor L1's request to flush an ASID on nested VMRUN
482 * - Sync nested NPT MMU on VMRUN that flushes L2's ASID[*]
483 * - Don't crush a pending TLB flush in vmcb02 on nested VMRUN
484 * - Flush L1's ASID on KVM_REQ_TLB_FLUSH_GUEST
485 *
486 * [*] Unlike nested EPT, SVM's ASID management can invalidate nested
487 * NPT guest-physical mappings on VMRUN.
488 */
489 kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
490 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
491 }
492
493 /*
494 * Load guest's/host's cr3 on nested vmentry or vmexit. @nested_npt is true
495 * if we are emulating VM-Entry into a guest with NPT enabled.
496 */
nested_svm_load_cr3(struct kvm_vcpu * vcpu,unsigned long cr3,bool nested_npt,bool reload_pdptrs)497 static int nested_svm_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,
498 bool nested_npt, bool reload_pdptrs)
499 {
500 if (CC(kvm_vcpu_is_illegal_gpa(vcpu, cr3)))
501 return -EINVAL;
502
503 if (reload_pdptrs && !nested_npt && is_pae_paging(vcpu) &&
504 CC(!load_pdptrs(vcpu, cr3)))
505 return -EINVAL;
506
507 vcpu->arch.cr3 = cr3;
508
509 /* Re-initialize the MMU, e.g. to pick up CR4 MMU role changes. */
510 kvm_init_mmu(vcpu);
511
512 if (!nested_npt)
513 kvm_mmu_new_pgd(vcpu, cr3);
514
515 return 0;
516 }
517
nested_vmcb02_compute_g_pat(struct vcpu_svm * svm)518 void nested_vmcb02_compute_g_pat(struct vcpu_svm *svm)
519 {
520 if (!svm->nested.vmcb02.ptr)
521 return;
522
523 /* FIXME: merge g_pat from vmcb01 and vmcb12. */
524 svm->nested.vmcb02.ptr->save.g_pat = svm->vmcb01.ptr->save.g_pat;
525 }
526
nested_vmcb02_prepare_save(struct vcpu_svm * svm,struct vmcb * vmcb12)527 static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *vmcb12)
528 {
529 bool new_vmcb12 = false;
530 struct vmcb *vmcb01 = svm->vmcb01.ptr;
531 struct vmcb *vmcb02 = svm->nested.vmcb02.ptr;
532
533 nested_vmcb02_compute_g_pat(svm);
534
535 /* Load the nested guest state */
536 if (svm->nested.vmcb12_gpa != svm->nested.last_vmcb12_gpa) {
537 new_vmcb12 = true;
538 svm->nested.last_vmcb12_gpa = svm->nested.vmcb12_gpa;
539 svm->nested.force_msr_bitmap_recalc = true;
540 }
541
542 if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_SEG))) {
543 vmcb02->save.es = vmcb12->save.es;
544 vmcb02->save.cs = vmcb12->save.cs;
545 vmcb02->save.ss = vmcb12->save.ss;
546 vmcb02->save.ds = vmcb12->save.ds;
547 vmcb02->save.cpl = vmcb12->save.cpl;
548 vmcb_mark_dirty(vmcb02, VMCB_SEG);
549 }
550
551 if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_DT))) {
552 vmcb02->save.gdtr = vmcb12->save.gdtr;
553 vmcb02->save.idtr = vmcb12->save.idtr;
554 vmcb_mark_dirty(vmcb02, VMCB_DT);
555 }
556
557 kvm_set_rflags(&svm->vcpu, vmcb12->save.rflags | X86_EFLAGS_FIXED);
558
559 svm_set_efer(&svm->vcpu, svm->nested.save.efer);
560
561 svm_set_cr0(&svm->vcpu, svm->nested.save.cr0);
562 svm_set_cr4(&svm->vcpu, svm->nested.save.cr4);
563
564 svm->vcpu.arch.cr2 = vmcb12->save.cr2;
565
566 kvm_rax_write(&svm->vcpu, vmcb12->save.rax);
567 kvm_rsp_write(&svm->vcpu, vmcb12->save.rsp);
568 kvm_rip_write(&svm->vcpu, vmcb12->save.rip);
569
570 /* In case we don't even reach vcpu_run, the fields are not updated */
571 vmcb02->save.rax = vmcb12->save.rax;
572 vmcb02->save.rsp = vmcb12->save.rsp;
573 vmcb02->save.rip = vmcb12->save.rip;
574
575 /* These bits will be set properly on the first execution when new_vmc12 is true */
576 if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_DR))) {
577 vmcb02->save.dr7 = svm->nested.save.dr7 | DR7_FIXED_1;
578 svm->vcpu.arch.dr6 = svm->nested.save.dr6 | DR6_ACTIVE_LOW;
579 vmcb_mark_dirty(vmcb02, VMCB_DR);
580 }
581
582 if (unlikely(svm->lbrv_enabled && (svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK))) {
583 /*
584 * Reserved bits of DEBUGCTL are ignored. Be consistent with
585 * svm_set_msr's definition of reserved bits.
586 */
587 svm_copy_lbrs(vmcb02, vmcb12);
588 vmcb02->save.dbgctl &= ~DEBUGCTL_RESERVED_BITS;
589 svm_update_lbrv(&svm->vcpu);
590
591 } else if (unlikely(vmcb01->control.virt_ext & LBR_CTL_ENABLE_MASK)) {
592 svm_copy_lbrs(vmcb02, vmcb01);
593 }
594 }
595
is_evtinj_soft(u32 evtinj)596 static inline bool is_evtinj_soft(u32 evtinj)
597 {
598 u32 type = evtinj & SVM_EVTINJ_TYPE_MASK;
599 u8 vector = evtinj & SVM_EVTINJ_VEC_MASK;
600
601 if (!(evtinj & SVM_EVTINJ_VALID))
602 return false;
603
604 if (type == SVM_EVTINJ_TYPE_SOFT)
605 return true;
606
607 return type == SVM_EVTINJ_TYPE_EXEPT && kvm_exception_is_soft(vector);
608 }
609
is_evtinj_nmi(u32 evtinj)610 static bool is_evtinj_nmi(u32 evtinj)
611 {
612 u32 type = evtinj & SVM_EVTINJ_TYPE_MASK;
613
614 if (!(evtinj & SVM_EVTINJ_VALID))
615 return false;
616
617 return type == SVM_EVTINJ_TYPE_NMI;
618 }
619
nested_vmcb02_prepare_control(struct vcpu_svm * svm,unsigned long vmcb12_rip,unsigned long vmcb12_csbase)620 static void nested_vmcb02_prepare_control(struct vcpu_svm *svm,
621 unsigned long vmcb12_rip,
622 unsigned long vmcb12_csbase)
623 {
624 u32 int_ctl_vmcb01_bits = V_INTR_MASKING_MASK;
625 u32 int_ctl_vmcb12_bits = V_TPR_MASK | V_IRQ_INJECTION_BITS_MASK;
626
627 struct kvm_vcpu *vcpu = &svm->vcpu;
628 struct vmcb *vmcb01 = svm->vmcb01.ptr;
629 struct vmcb *vmcb02 = svm->nested.vmcb02.ptr;
630 u32 pause_count12;
631 u32 pause_thresh12;
632
633 /*
634 * Filled at exit: exit_code, exit_code_hi, exit_info_1, exit_info_2,
635 * exit_int_info, exit_int_info_err, next_rip, insn_len, insn_bytes.
636 */
637
638 if (svm->vgif_enabled && (svm->nested.ctl.int_ctl & V_GIF_ENABLE_MASK))
639 int_ctl_vmcb12_bits |= (V_GIF_MASK | V_GIF_ENABLE_MASK);
640 else
641 int_ctl_vmcb01_bits |= (V_GIF_MASK | V_GIF_ENABLE_MASK);
642
643 /* Copied from vmcb01. msrpm_base can be overwritten later. */
644 vmcb02->control.nested_ctl = vmcb01->control.nested_ctl;
645 vmcb02->control.iopm_base_pa = vmcb01->control.iopm_base_pa;
646 vmcb02->control.msrpm_base_pa = vmcb01->control.msrpm_base_pa;
647
648 /* Done at vmrun: asid. */
649
650 /* Also overwritten later if necessary. */
651 vmcb02->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
652
653 /* nested_cr3. */
654 if (nested_npt_enabled(svm))
655 nested_svm_init_mmu_context(vcpu);
656
657 vcpu->arch.tsc_offset = kvm_calc_nested_tsc_offset(
658 vcpu->arch.l1_tsc_offset,
659 svm->nested.ctl.tsc_offset,
660 svm->tsc_ratio_msr);
661
662 vmcb02->control.tsc_offset = vcpu->arch.tsc_offset;
663
664 if (svm->tsc_ratio_msr != kvm_caps.default_tsc_scaling_ratio) {
665 WARN_ON(!svm->tsc_scaling_enabled);
666 nested_svm_update_tsc_ratio_msr(vcpu);
667 }
668
669 vmcb02->control.int_ctl =
670 (svm->nested.ctl.int_ctl & int_ctl_vmcb12_bits) |
671 (vmcb01->control.int_ctl & int_ctl_vmcb01_bits);
672
673 vmcb02->control.int_vector = svm->nested.ctl.int_vector;
674 vmcb02->control.int_state = svm->nested.ctl.int_state;
675 vmcb02->control.event_inj = svm->nested.ctl.event_inj;
676 vmcb02->control.event_inj_err = svm->nested.ctl.event_inj_err;
677
678 /*
679 * next_rip is consumed on VMRUN as the return address pushed on the
680 * stack for injected soft exceptions/interrupts. If nrips is exposed
681 * to L1, take it verbatim from vmcb12. If nrips is supported in
682 * hardware but not exposed to L1, stuff the actual L2 RIP to emulate
683 * what a nrips=0 CPU would do (L1 is responsible for advancing RIP
684 * prior to injecting the event).
685 */
686 if (svm->nrips_enabled)
687 vmcb02->control.next_rip = svm->nested.ctl.next_rip;
688 else if (boot_cpu_has(X86_FEATURE_NRIPS))
689 vmcb02->control.next_rip = vmcb12_rip;
690
691 svm->nmi_l1_to_l2 = is_evtinj_nmi(vmcb02->control.event_inj);
692 if (is_evtinj_soft(vmcb02->control.event_inj)) {
693 svm->soft_int_injected = true;
694 svm->soft_int_csbase = vmcb12_csbase;
695 svm->soft_int_old_rip = vmcb12_rip;
696 if (svm->nrips_enabled)
697 svm->soft_int_next_rip = svm->nested.ctl.next_rip;
698 else
699 svm->soft_int_next_rip = vmcb12_rip;
700 }
701
702 vmcb02->control.virt_ext = vmcb01->control.virt_ext &
703 LBR_CTL_ENABLE_MASK;
704 if (svm->lbrv_enabled)
705 vmcb02->control.virt_ext |=
706 (svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK);
707
708 if (!nested_vmcb_needs_vls_intercept(svm))
709 vmcb02->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
710
711 pause_count12 = svm->pause_filter_enabled ? svm->nested.ctl.pause_filter_count : 0;
712 pause_thresh12 = svm->pause_threshold_enabled ? svm->nested.ctl.pause_filter_thresh : 0;
713 if (kvm_pause_in_guest(svm->vcpu.kvm)) {
714 /* use guest values since host doesn't intercept PAUSE */
715 vmcb02->control.pause_filter_count = pause_count12;
716 vmcb02->control.pause_filter_thresh = pause_thresh12;
717
718 } else {
719 /* start from host values otherwise */
720 vmcb02->control.pause_filter_count = vmcb01->control.pause_filter_count;
721 vmcb02->control.pause_filter_thresh = vmcb01->control.pause_filter_thresh;
722
723 /* ... but ensure filtering is disabled if so requested. */
724 if (vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_PAUSE)) {
725 if (!pause_count12)
726 vmcb02->control.pause_filter_count = 0;
727 if (!pause_thresh12)
728 vmcb02->control.pause_filter_thresh = 0;
729 }
730 }
731
732 nested_svm_transition_tlb_flush(vcpu);
733
734 /* Enter Guest-Mode */
735 enter_guest_mode(vcpu);
736
737 /*
738 * Merge guest and host intercepts - must be called with vcpu in
739 * guest-mode to take effect.
740 */
741 recalc_intercepts(svm);
742 }
743
nested_svm_copy_common_state(struct vmcb * from_vmcb,struct vmcb * to_vmcb)744 static void nested_svm_copy_common_state(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
745 {
746 /*
747 * Some VMCB state is shared between L1 and L2 and thus has to be
748 * moved at the time of nested vmrun and vmexit.
749 *
750 * VMLOAD/VMSAVE state would also belong in this category, but KVM
751 * always performs VMLOAD and VMSAVE from the VMCB01.
752 */
753 to_vmcb->save.spec_ctrl = from_vmcb->save.spec_ctrl;
754 }
755
enter_svm_guest_mode(struct kvm_vcpu * vcpu,u64 vmcb12_gpa,struct vmcb * vmcb12,bool from_vmrun)756 int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb12_gpa,
757 struct vmcb *vmcb12, bool from_vmrun)
758 {
759 struct vcpu_svm *svm = to_svm(vcpu);
760 int ret;
761
762 trace_kvm_nested_vmenter(svm->vmcb->save.rip,
763 vmcb12_gpa,
764 vmcb12->save.rip,
765 vmcb12->control.int_ctl,
766 vmcb12->control.event_inj,
767 vmcb12->control.nested_ctl,
768 vmcb12->control.nested_cr3,
769 vmcb12->save.cr3,
770 KVM_ISA_SVM);
771
772 trace_kvm_nested_intercepts(vmcb12->control.intercepts[INTERCEPT_CR] & 0xffff,
773 vmcb12->control.intercepts[INTERCEPT_CR] >> 16,
774 vmcb12->control.intercepts[INTERCEPT_EXCEPTION],
775 vmcb12->control.intercepts[INTERCEPT_WORD3],
776 vmcb12->control.intercepts[INTERCEPT_WORD4],
777 vmcb12->control.intercepts[INTERCEPT_WORD5]);
778
779
780 svm->nested.vmcb12_gpa = vmcb12_gpa;
781
782 WARN_ON(svm->vmcb == svm->nested.vmcb02.ptr);
783
784 nested_svm_copy_common_state(svm->vmcb01.ptr, svm->nested.vmcb02.ptr);
785
786 svm_switch_vmcb(svm, &svm->nested.vmcb02);
787 nested_vmcb02_prepare_control(svm, vmcb12->save.rip, vmcb12->save.cs.base);
788 nested_vmcb02_prepare_save(svm, vmcb12);
789
790 ret = nested_svm_load_cr3(&svm->vcpu, svm->nested.save.cr3,
791 nested_npt_enabled(svm), from_vmrun);
792 if (ret)
793 return ret;
794
795 if (!from_vmrun)
796 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
797
798 svm_set_gif(svm, true);
799
800 if (kvm_vcpu_apicv_active(vcpu))
801 kvm_make_request(KVM_REQ_APICV_UPDATE, vcpu);
802
803 return 0;
804 }
805
nested_svm_vmrun(struct kvm_vcpu * vcpu)806 int nested_svm_vmrun(struct kvm_vcpu *vcpu)
807 {
808 struct vcpu_svm *svm = to_svm(vcpu);
809 int ret;
810 struct vmcb *vmcb12;
811 struct kvm_host_map map;
812 u64 vmcb12_gpa;
813 struct vmcb *vmcb01 = svm->vmcb01.ptr;
814
815 if (!svm->nested.hsave_msr) {
816 kvm_inject_gp(vcpu, 0);
817 return 1;
818 }
819
820 if (is_smm(vcpu)) {
821 kvm_queue_exception(vcpu, UD_VECTOR);
822 return 1;
823 }
824
825 vmcb12_gpa = svm->vmcb->save.rax;
826 ret = kvm_vcpu_map(vcpu, gpa_to_gfn(vmcb12_gpa), &map);
827 if (ret == -EINVAL) {
828 kvm_inject_gp(vcpu, 0);
829 return 1;
830 } else if (ret) {
831 return kvm_skip_emulated_instruction(vcpu);
832 }
833
834 ret = kvm_skip_emulated_instruction(vcpu);
835
836 vmcb12 = map.hva;
837
838 if (WARN_ON_ONCE(!svm->nested.initialized))
839 return -EINVAL;
840
841 nested_copy_vmcb_control_to_cache(svm, &vmcb12->control);
842 nested_copy_vmcb_save_to_cache(svm, &vmcb12->save);
843
844 if (!nested_vmcb_check_save(vcpu) ||
845 !nested_vmcb_check_controls(vcpu)) {
846 vmcb12->control.exit_code = SVM_EXIT_ERR;
847 vmcb12->control.exit_code_hi = 0;
848 vmcb12->control.exit_info_1 = 0;
849 vmcb12->control.exit_info_2 = 0;
850 goto out;
851 }
852
853 /*
854 * Since vmcb01 is not in use, we can use it to store some of the L1
855 * state.
856 */
857 vmcb01->save.efer = vcpu->arch.efer;
858 vmcb01->save.cr0 = kvm_read_cr0(vcpu);
859 vmcb01->save.cr4 = vcpu->arch.cr4;
860 vmcb01->save.rflags = kvm_get_rflags(vcpu);
861 vmcb01->save.rip = kvm_rip_read(vcpu);
862
863 if (!npt_enabled)
864 vmcb01->save.cr3 = kvm_read_cr3(vcpu);
865
866 svm->nested.nested_run_pending = 1;
867
868 if (enter_svm_guest_mode(vcpu, vmcb12_gpa, vmcb12, true))
869 goto out_exit_err;
870
871 if (nested_svm_vmrun_msrpm(svm))
872 goto out;
873
874 out_exit_err:
875 svm->nested.nested_run_pending = 0;
876 svm->nmi_l1_to_l2 = false;
877 svm->soft_int_injected = false;
878
879 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
880 svm->vmcb->control.exit_code_hi = 0;
881 svm->vmcb->control.exit_info_1 = 0;
882 svm->vmcb->control.exit_info_2 = 0;
883
884 nested_svm_vmexit(svm);
885
886 out:
887 kvm_vcpu_unmap(vcpu, &map, true);
888
889 return ret;
890 }
891
892 /* Copy state save area fields which are handled by VMRUN */
svm_copy_vmrun_state(struct vmcb_save_area * to_save,struct vmcb_save_area * from_save)893 void svm_copy_vmrun_state(struct vmcb_save_area *to_save,
894 struct vmcb_save_area *from_save)
895 {
896 to_save->es = from_save->es;
897 to_save->cs = from_save->cs;
898 to_save->ss = from_save->ss;
899 to_save->ds = from_save->ds;
900 to_save->gdtr = from_save->gdtr;
901 to_save->idtr = from_save->idtr;
902 to_save->rflags = from_save->rflags | X86_EFLAGS_FIXED;
903 to_save->efer = from_save->efer;
904 to_save->cr0 = from_save->cr0;
905 to_save->cr3 = from_save->cr3;
906 to_save->cr4 = from_save->cr4;
907 to_save->rax = from_save->rax;
908 to_save->rsp = from_save->rsp;
909 to_save->rip = from_save->rip;
910 to_save->cpl = 0;
911 }
912
svm_copy_vmloadsave_state(struct vmcb * to_vmcb,struct vmcb * from_vmcb)913 void svm_copy_vmloadsave_state(struct vmcb *to_vmcb, struct vmcb *from_vmcb)
914 {
915 to_vmcb->save.fs = from_vmcb->save.fs;
916 to_vmcb->save.gs = from_vmcb->save.gs;
917 to_vmcb->save.tr = from_vmcb->save.tr;
918 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
919 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
920 to_vmcb->save.star = from_vmcb->save.star;
921 to_vmcb->save.lstar = from_vmcb->save.lstar;
922 to_vmcb->save.cstar = from_vmcb->save.cstar;
923 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
924 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
925 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
926 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
927 }
928
nested_svm_vmexit(struct vcpu_svm * svm)929 int nested_svm_vmexit(struct vcpu_svm *svm)
930 {
931 struct kvm_vcpu *vcpu = &svm->vcpu;
932 struct vmcb *vmcb01 = svm->vmcb01.ptr;
933 struct vmcb *vmcb02 = svm->nested.vmcb02.ptr;
934 struct vmcb *vmcb12;
935 struct kvm_host_map map;
936 int rc;
937
938 rc = kvm_vcpu_map(vcpu, gpa_to_gfn(svm->nested.vmcb12_gpa), &map);
939 if (rc) {
940 if (rc == -EINVAL)
941 kvm_inject_gp(vcpu, 0);
942 return 1;
943 }
944
945 vmcb12 = map.hva;
946
947 /* Exit Guest-Mode */
948 leave_guest_mode(vcpu);
949 svm->nested.vmcb12_gpa = 0;
950 WARN_ON_ONCE(svm->nested.nested_run_pending);
951
952 kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
953
954 /* in case we halted in L2 */
955 svm->vcpu.arch.mp_state = KVM_MP_STATE_RUNNABLE;
956
957 /* Give the current vmcb to the guest */
958
959 vmcb12->save.es = vmcb02->save.es;
960 vmcb12->save.cs = vmcb02->save.cs;
961 vmcb12->save.ss = vmcb02->save.ss;
962 vmcb12->save.ds = vmcb02->save.ds;
963 vmcb12->save.gdtr = vmcb02->save.gdtr;
964 vmcb12->save.idtr = vmcb02->save.idtr;
965 vmcb12->save.efer = svm->vcpu.arch.efer;
966 vmcb12->save.cr0 = kvm_read_cr0(vcpu);
967 vmcb12->save.cr3 = kvm_read_cr3(vcpu);
968 vmcb12->save.cr2 = vmcb02->save.cr2;
969 vmcb12->save.cr4 = svm->vcpu.arch.cr4;
970 vmcb12->save.rflags = kvm_get_rflags(vcpu);
971 vmcb12->save.rip = kvm_rip_read(vcpu);
972 vmcb12->save.rsp = kvm_rsp_read(vcpu);
973 vmcb12->save.rax = kvm_rax_read(vcpu);
974 vmcb12->save.dr7 = vmcb02->save.dr7;
975 vmcb12->save.dr6 = svm->vcpu.arch.dr6;
976 vmcb12->save.cpl = vmcb02->save.cpl;
977
978 vmcb12->control.int_state = vmcb02->control.int_state;
979 vmcb12->control.exit_code = vmcb02->control.exit_code;
980 vmcb12->control.exit_code_hi = vmcb02->control.exit_code_hi;
981 vmcb12->control.exit_info_1 = vmcb02->control.exit_info_1;
982 vmcb12->control.exit_info_2 = vmcb02->control.exit_info_2;
983
984 if (vmcb12->control.exit_code != SVM_EXIT_ERR)
985 nested_save_pending_event_to_vmcb12(svm, vmcb12);
986
987 if (svm->nrips_enabled)
988 vmcb12->control.next_rip = vmcb02->control.next_rip;
989
990 vmcb12->control.int_ctl = svm->nested.ctl.int_ctl;
991 vmcb12->control.tlb_ctl = svm->nested.ctl.tlb_ctl;
992 vmcb12->control.event_inj = svm->nested.ctl.event_inj;
993 vmcb12->control.event_inj_err = svm->nested.ctl.event_inj_err;
994
995 if (!kvm_pause_in_guest(vcpu->kvm)) {
996 vmcb01->control.pause_filter_count = vmcb02->control.pause_filter_count;
997 vmcb_mark_dirty(vmcb01, VMCB_INTERCEPTS);
998
999 }
1000
1001 nested_svm_copy_common_state(svm->nested.vmcb02.ptr, svm->vmcb01.ptr);
1002
1003 svm_switch_vmcb(svm, &svm->vmcb01);
1004
1005 if (unlikely(svm->lbrv_enabled && (svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK))) {
1006 svm_copy_lbrs(vmcb12, vmcb02);
1007 svm_update_lbrv(vcpu);
1008 } else if (unlikely(vmcb01->control.virt_ext & LBR_CTL_ENABLE_MASK)) {
1009 svm_copy_lbrs(vmcb01, vmcb02);
1010 svm_update_lbrv(vcpu);
1011 }
1012
1013 /*
1014 * On vmexit the GIF is set to false and
1015 * no event can be injected in L1.
1016 */
1017 svm_set_gif(svm, false);
1018 vmcb01->control.exit_int_info = 0;
1019
1020 svm->vcpu.arch.tsc_offset = svm->vcpu.arch.l1_tsc_offset;
1021 if (vmcb01->control.tsc_offset != svm->vcpu.arch.tsc_offset) {
1022 vmcb01->control.tsc_offset = svm->vcpu.arch.tsc_offset;
1023 vmcb_mark_dirty(vmcb01, VMCB_INTERCEPTS);
1024 }
1025
1026 if (svm->tsc_ratio_msr != kvm_caps.default_tsc_scaling_ratio) {
1027 WARN_ON(!svm->tsc_scaling_enabled);
1028 vcpu->arch.tsc_scaling_ratio = vcpu->arch.l1_tsc_scaling_ratio;
1029 __svm_write_tsc_multiplier(vcpu->arch.tsc_scaling_ratio);
1030 }
1031
1032 svm->nested.ctl.nested_cr3 = 0;
1033
1034 /*
1035 * Restore processor state that had been saved in vmcb01
1036 */
1037 kvm_set_rflags(vcpu, vmcb01->save.rflags);
1038 svm_set_efer(vcpu, vmcb01->save.efer);
1039 svm_set_cr0(vcpu, vmcb01->save.cr0 | X86_CR0_PE);
1040 svm_set_cr4(vcpu, vmcb01->save.cr4);
1041 kvm_rax_write(vcpu, vmcb01->save.rax);
1042 kvm_rsp_write(vcpu, vmcb01->save.rsp);
1043 kvm_rip_write(vcpu, vmcb01->save.rip);
1044
1045 svm->vcpu.arch.dr7 = DR7_FIXED_1;
1046 kvm_update_dr7(&svm->vcpu);
1047
1048 trace_kvm_nested_vmexit_inject(vmcb12->control.exit_code,
1049 vmcb12->control.exit_info_1,
1050 vmcb12->control.exit_info_2,
1051 vmcb12->control.exit_int_info,
1052 vmcb12->control.exit_int_info_err,
1053 KVM_ISA_SVM);
1054
1055 kvm_vcpu_unmap(vcpu, &map, true);
1056
1057 nested_svm_transition_tlb_flush(vcpu);
1058
1059 nested_svm_uninit_mmu_context(vcpu);
1060
1061 rc = nested_svm_load_cr3(vcpu, vmcb01->save.cr3, false, true);
1062 if (rc)
1063 return 1;
1064
1065 /*
1066 * Drop what we picked up for L2 via svm_complete_interrupts() so it
1067 * doesn't end up in L1.
1068 */
1069 svm->vcpu.arch.nmi_injected = false;
1070 kvm_clear_exception_queue(vcpu);
1071 kvm_clear_interrupt_queue(vcpu);
1072
1073 /*
1074 * If we are here following the completion of a VMRUN that
1075 * is being single-stepped, queue the pending #DB intercept
1076 * right now so that it an be accounted for before we execute
1077 * L1's next instruction.
1078 */
1079 if (unlikely(vmcb01->save.rflags & X86_EFLAGS_TF))
1080 kvm_queue_exception(&(svm->vcpu), DB_VECTOR);
1081
1082 /*
1083 * Un-inhibit the AVIC right away, so that other vCPUs can start
1084 * to benefit from it right away.
1085 */
1086 if (kvm_apicv_activated(vcpu->kvm))
1087 kvm_vcpu_update_apicv(vcpu);
1088
1089 return 0;
1090 }
1091
nested_svm_triple_fault(struct kvm_vcpu * vcpu)1092 static void nested_svm_triple_fault(struct kvm_vcpu *vcpu)
1093 {
1094 struct vcpu_svm *svm = to_svm(vcpu);
1095
1096 if (!vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_SHUTDOWN))
1097 return;
1098
1099 kvm_clear_request(KVM_REQ_TRIPLE_FAULT, vcpu);
1100 nested_svm_simple_vmexit(to_svm(vcpu), SVM_EXIT_SHUTDOWN);
1101 }
1102
svm_allocate_nested(struct vcpu_svm * svm)1103 int svm_allocate_nested(struct vcpu_svm *svm)
1104 {
1105 struct page *vmcb02_page;
1106
1107 if (svm->nested.initialized)
1108 return 0;
1109
1110 vmcb02_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
1111 if (!vmcb02_page)
1112 return -ENOMEM;
1113 svm->nested.vmcb02.ptr = page_address(vmcb02_page);
1114 svm->nested.vmcb02.pa = __sme_set(page_to_pfn(vmcb02_page) << PAGE_SHIFT);
1115
1116 svm->nested.msrpm = svm_vcpu_alloc_msrpm();
1117 if (!svm->nested.msrpm)
1118 goto err_free_vmcb02;
1119 svm_vcpu_init_msrpm(&svm->vcpu, svm->nested.msrpm);
1120
1121 svm->nested.initialized = true;
1122 return 0;
1123
1124 err_free_vmcb02:
1125 __free_page(vmcb02_page);
1126 return -ENOMEM;
1127 }
1128
svm_free_nested(struct vcpu_svm * svm)1129 void svm_free_nested(struct vcpu_svm *svm)
1130 {
1131 if (!svm->nested.initialized)
1132 return;
1133
1134 if (WARN_ON_ONCE(svm->vmcb != svm->vmcb01.ptr))
1135 svm_switch_vmcb(svm, &svm->vmcb01);
1136
1137 svm_vcpu_free_msrpm(svm->nested.msrpm);
1138 svm->nested.msrpm = NULL;
1139
1140 __free_page(virt_to_page(svm->nested.vmcb02.ptr));
1141 svm->nested.vmcb02.ptr = NULL;
1142
1143 /*
1144 * When last_vmcb12_gpa matches the current vmcb12 gpa,
1145 * some vmcb12 fields are not loaded if they are marked clean
1146 * in the vmcb12, since in this case they are up to date already.
1147 *
1148 * When the vmcb02 is freed, this optimization becomes invalid.
1149 */
1150 svm->nested.last_vmcb12_gpa = INVALID_GPA;
1151
1152 svm->nested.initialized = false;
1153 }
1154
svm_leave_nested(struct kvm_vcpu * vcpu)1155 void svm_leave_nested(struct kvm_vcpu *vcpu)
1156 {
1157 struct vcpu_svm *svm = to_svm(vcpu);
1158
1159 if (is_guest_mode(vcpu)) {
1160 svm->nested.nested_run_pending = 0;
1161 svm->nested.vmcb12_gpa = INVALID_GPA;
1162
1163 leave_guest_mode(vcpu);
1164
1165 svm_switch_vmcb(svm, &svm->vmcb01);
1166
1167 nested_svm_uninit_mmu_context(vcpu);
1168 vmcb_mark_all_dirty(svm->vmcb);
1169 }
1170
1171 kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
1172 }
1173
nested_svm_exit_handled_msr(struct vcpu_svm * svm)1174 static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
1175 {
1176 u32 offset, msr, value;
1177 int write, mask;
1178
1179 if (!(vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
1180 return NESTED_EXIT_HOST;
1181
1182 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
1183 offset = svm_msrpm_offset(msr);
1184 write = svm->vmcb->control.exit_info_1 & 1;
1185 mask = 1 << ((2 * (msr & 0xf)) + write);
1186
1187 if (offset == MSR_INVALID)
1188 return NESTED_EXIT_DONE;
1189
1190 /* Offset is in 32 bit units but need in 8 bit units */
1191 offset *= 4;
1192
1193 if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.ctl.msrpm_base_pa + offset, &value, 4))
1194 return NESTED_EXIT_DONE;
1195
1196 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
1197 }
1198
nested_svm_intercept_ioio(struct vcpu_svm * svm)1199 static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
1200 {
1201 unsigned port, size, iopm_len;
1202 u16 val, mask;
1203 u8 start_bit;
1204 u64 gpa;
1205
1206 if (!(vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_IOIO_PROT)))
1207 return NESTED_EXIT_HOST;
1208
1209 port = svm->vmcb->control.exit_info_1 >> 16;
1210 size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
1211 SVM_IOIO_SIZE_SHIFT;
1212 gpa = svm->nested.ctl.iopm_base_pa + (port / 8);
1213 start_bit = port % 8;
1214 iopm_len = (start_bit + size > 8) ? 2 : 1;
1215 mask = (0xf >> (4 - size)) << start_bit;
1216 val = 0;
1217
1218 if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
1219 return NESTED_EXIT_DONE;
1220
1221 return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
1222 }
1223
nested_svm_intercept(struct vcpu_svm * svm)1224 static int nested_svm_intercept(struct vcpu_svm *svm)
1225 {
1226 u32 exit_code = svm->vmcb->control.exit_code;
1227 int vmexit = NESTED_EXIT_HOST;
1228
1229 switch (exit_code) {
1230 case SVM_EXIT_MSR:
1231 vmexit = nested_svm_exit_handled_msr(svm);
1232 break;
1233 case SVM_EXIT_IOIO:
1234 vmexit = nested_svm_intercept_ioio(svm);
1235 break;
1236 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
1237 if (vmcb12_is_intercept(&svm->nested.ctl, exit_code))
1238 vmexit = NESTED_EXIT_DONE;
1239 break;
1240 }
1241 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
1242 if (vmcb12_is_intercept(&svm->nested.ctl, exit_code))
1243 vmexit = NESTED_EXIT_DONE;
1244 break;
1245 }
1246 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1247 /*
1248 * Host-intercepted exceptions have been checked already in
1249 * nested_svm_exit_special. There is nothing to do here,
1250 * the vmexit is injected by svm_check_nested_events.
1251 */
1252 vmexit = NESTED_EXIT_DONE;
1253 break;
1254 }
1255 case SVM_EXIT_ERR: {
1256 vmexit = NESTED_EXIT_DONE;
1257 break;
1258 }
1259 default: {
1260 if (vmcb12_is_intercept(&svm->nested.ctl, exit_code))
1261 vmexit = NESTED_EXIT_DONE;
1262 }
1263 }
1264
1265 return vmexit;
1266 }
1267
nested_svm_exit_handled(struct vcpu_svm * svm)1268 int nested_svm_exit_handled(struct vcpu_svm *svm)
1269 {
1270 int vmexit;
1271
1272 vmexit = nested_svm_intercept(svm);
1273
1274 if (vmexit == NESTED_EXIT_DONE)
1275 nested_svm_vmexit(svm);
1276
1277 return vmexit;
1278 }
1279
nested_svm_check_permissions(struct kvm_vcpu * vcpu)1280 int nested_svm_check_permissions(struct kvm_vcpu *vcpu)
1281 {
1282 if (!(vcpu->arch.efer & EFER_SVME) || !is_paging(vcpu)) {
1283 kvm_queue_exception(vcpu, UD_VECTOR);
1284 return 1;
1285 }
1286
1287 if (to_svm(vcpu)->vmcb->save.cpl) {
1288 kvm_inject_gp(vcpu, 0);
1289 return 1;
1290 }
1291
1292 return 0;
1293 }
1294
nested_svm_is_exception_vmexit(struct kvm_vcpu * vcpu,u8 vector,u32 error_code)1295 static bool nested_svm_is_exception_vmexit(struct kvm_vcpu *vcpu, u8 vector,
1296 u32 error_code)
1297 {
1298 struct vcpu_svm *svm = to_svm(vcpu);
1299
1300 return (svm->nested.ctl.intercepts[INTERCEPT_EXCEPTION] & BIT(vector));
1301 }
1302
nested_svm_inject_exception_vmexit(struct kvm_vcpu * vcpu)1303 static void nested_svm_inject_exception_vmexit(struct kvm_vcpu *vcpu)
1304 {
1305 struct kvm_queued_exception *ex = &vcpu->arch.exception_vmexit;
1306 struct vcpu_svm *svm = to_svm(vcpu);
1307 struct vmcb *vmcb = svm->vmcb;
1308
1309 vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + ex->vector;
1310 vmcb->control.exit_code_hi = 0;
1311
1312 if (ex->has_error_code)
1313 vmcb->control.exit_info_1 = ex->error_code;
1314
1315 /*
1316 * EXITINFO2 is undefined for all exception intercepts other
1317 * than #PF.
1318 */
1319 if (ex->vector == PF_VECTOR) {
1320 if (ex->has_payload)
1321 vmcb->control.exit_info_2 = ex->payload;
1322 else
1323 vmcb->control.exit_info_2 = vcpu->arch.cr2;
1324 } else if (ex->vector == DB_VECTOR) {
1325 /* See kvm_check_and_inject_events(). */
1326 kvm_deliver_exception_payload(vcpu, ex);
1327
1328 if (vcpu->arch.dr7 & DR7_GD) {
1329 vcpu->arch.dr7 &= ~DR7_GD;
1330 kvm_update_dr7(vcpu);
1331 }
1332 } else {
1333 WARN_ON(ex->has_payload);
1334 }
1335
1336 nested_svm_vmexit(svm);
1337 }
1338
nested_exit_on_init(struct vcpu_svm * svm)1339 static inline bool nested_exit_on_init(struct vcpu_svm *svm)
1340 {
1341 return vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_INIT);
1342 }
1343
svm_check_nested_events(struct kvm_vcpu * vcpu)1344 static int svm_check_nested_events(struct kvm_vcpu *vcpu)
1345 {
1346 struct kvm_lapic *apic = vcpu->arch.apic;
1347 struct vcpu_svm *svm = to_svm(vcpu);
1348 /*
1349 * Only a pending nested run blocks a pending exception. If there is a
1350 * previously injected event, the pending exception occurred while said
1351 * event was being delivered and thus needs to be handled.
1352 */
1353 bool block_nested_exceptions = svm->nested.nested_run_pending;
1354 /*
1355 * New events (not exceptions) are only recognized at instruction
1356 * boundaries. If an event needs reinjection, then KVM is handling a
1357 * VM-Exit that occurred _during_ instruction execution; new events are
1358 * blocked until the instruction completes.
1359 */
1360 bool block_nested_events = block_nested_exceptions ||
1361 kvm_event_needs_reinjection(vcpu);
1362
1363 if (lapic_in_kernel(vcpu) &&
1364 test_bit(KVM_APIC_INIT, &apic->pending_events)) {
1365 if (block_nested_events)
1366 return -EBUSY;
1367 if (!nested_exit_on_init(svm))
1368 return 0;
1369 nested_svm_simple_vmexit(svm, SVM_EXIT_INIT);
1370 return 0;
1371 }
1372
1373 if (vcpu->arch.exception_vmexit.pending) {
1374 if (block_nested_exceptions)
1375 return -EBUSY;
1376 nested_svm_inject_exception_vmexit(vcpu);
1377 return 0;
1378 }
1379
1380 if (vcpu->arch.exception.pending) {
1381 if (block_nested_exceptions)
1382 return -EBUSY;
1383 return 0;
1384 }
1385
1386 if (vcpu->arch.smi_pending && !svm_smi_blocked(vcpu)) {
1387 if (block_nested_events)
1388 return -EBUSY;
1389 if (!nested_exit_on_smi(svm))
1390 return 0;
1391 nested_svm_simple_vmexit(svm, SVM_EXIT_SMI);
1392 return 0;
1393 }
1394
1395 if (vcpu->arch.nmi_pending && !svm_nmi_blocked(vcpu)) {
1396 if (block_nested_events)
1397 return -EBUSY;
1398 if (!nested_exit_on_nmi(svm))
1399 return 0;
1400 nested_svm_simple_vmexit(svm, SVM_EXIT_NMI);
1401 return 0;
1402 }
1403
1404 if (kvm_cpu_has_interrupt(vcpu) && !svm_interrupt_blocked(vcpu)) {
1405 if (block_nested_events)
1406 return -EBUSY;
1407 if (!nested_exit_on_intr(svm))
1408 return 0;
1409 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
1410 nested_svm_simple_vmexit(svm, SVM_EXIT_INTR);
1411 return 0;
1412 }
1413
1414 return 0;
1415 }
1416
nested_svm_exit_special(struct vcpu_svm * svm)1417 int nested_svm_exit_special(struct vcpu_svm *svm)
1418 {
1419 u32 exit_code = svm->vmcb->control.exit_code;
1420
1421 switch (exit_code) {
1422 case SVM_EXIT_INTR:
1423 case SVM_EXIT_NMI:
1424 case SVM_EXIT_NPF:
1425 return NESTED_EXIT_HOST;
1426 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1427 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
1428
1429 if (svm->vmcb01.ptr->control.intercepts[INTERCEPT_EXCEPTION] &
1430 excp_bits)
1431 return NESTED_EXIT_HOST;
1432 else if (exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR &&
1433 svm->vcpu.arch.apf.host_apf_flags)
1434 /* Trap async PF even if not shadowing */
1435 return NESTED_EXIT_HOST;
1436 break;
1437 }
1438 default:
1439 break;
1440 }
1441
1442 return NESTED_EXIT_CONTINUE;
1443 }
1444
nested_svm_update_tsc_ratio_msr(struct kvm_vcpu * vcpu)1445 void nested_svm_update_tsc_ratio_msr(struct kvm_vcpu *vcpu)
1446 {
1447 struct vcpu_svm *svm = to_svm(vcpu);
1448
1449 vcpu->arch.tsc_scaling_ratio =
1450 kvm_calc_nested_tsc_multiplier(vcpu->arch.l1_tsc_scaling_ratio,
1451 svm->tsc_ratio_msr);
1452 __svm_write_tsc_multiplier(vcpu->arch.tsc_scaling_ratio);
1453 }
1454
1455 /* Inverse operation of nested_copy_vmcb_control_to_cache(). asid is copied too. */
nested_copy_vmcb_cache_to_control(struct vmcb_control_area * dst,struct vmcb_ctrl_area_cached * from)1456 static void nested_copy_vmcb_cache_to_control(struct vmcb_control_area *dst,
1457 struct vmcb_ctrl_area_cached *from)
1458 {
1459 unsigned int i;
1460
1461 memset(dst, 0, sizeof(struct vmcb_control_area));
1462
1463 for (i = 0; i < MAX_INTERCEPT; i++)
1464 dst->intercepts[i] = from->intercepts[i];
1465
1466 dst->iopm_base_pa = from->iopm_base_pa;
1467 dst->msrpm_base_pa = from->msrpm_base_pa;
1468 dst->tsc_offset = from->tsc_offset;
1469 dst->asid = from->asid;
1470 dst->tlb_ctl = from->tlb_ctl;
1471 dst->int_ctl = from->int_ctl;
1472 dst->int_vector = from->int_vector;
1473 dst->int_state = from->int_state;
1474 dst->exit_code = from->exit_code;
1475 dst->exit_code_hi = from->exit_code_hi;
1476 dst->exit_info_1 = from->exit_info_1;
1477 dst->exit_info_2 = from->exit_info_2;
1478 dst->exit_int_info = from->exit_int_info;
1479 dst->exit_int_info_err = from->exit_int_info_err;
1480 dst->nested_ctl = from->nested_ctl;
1481 dst->event_inj = from->event_inj;
1482 dst->event_inj_err = from->event_inj_err;
1483 dst->next_rip = from->next_rip;
1484 dst->nested_cr3 = from->nested_cr3;
1485 dst->virt_ext = from->virt_ext;
1486 dst->pause_filter_count = from->pause_filter_count;
1487 dst->pause_filter_thresh = from->pause_filter_thresh;
1488 /* 'clean' and 'reserved_sw' are not changed by KVM */
1489 }
1490
svm_get_nested_state(struct kvm_vcpu * vcpu,struct kvm_nested_state __user * user_kvm_nested_state,u32 user_data_size)1491 static int svm_get_nested_state(struct kvm_vcpu *vcpu,
1492 struct kvm_nested_state __user *user_kvm_nested_state,
1493 u32 user_data_size)
1494 {
1495 struct vcpu_svm *svm;
1496 struct vmcb_control_area *ctl;
1497 unsigned long r;
1498 struct kvm_nested_state kvm_state = {
1499 .flags = 0,
1500 .format = KVM_STATE_NESTED_FORMAT_SVM,
1501 .size = sizeof(kvm_state),
1502 };
1503 struct vmcb __user *user_vmcb = (struct vmcb __user *)
1504 &user_kvm_nested_state->data.svm[0];
1505
1506 if (!vcpu)
1507 return kvm_state.size + KVM_STATE_NESTED_SVM_VMCB_SIZE;
1508
1509 svm = to_svm(vcpu);
1510
1511 if (user_data_size < kvm_state.size)
1512 goto out;
1513
1514 /* First fill in the header and copy it out. */
1515 if (is_guest_mode(vcpu)) {
1516 kvm_state.hdr.svm.vmcb_pa = svm->nested.vmcb12_gpa;
1517 kvm_state.size += KVM_STATE_NESTED_SVM_VMCB_SIZE;
1518 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
1519
1520 if (svm->nested.nested_run_pending)
1521 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
1522 }
1523
1524 if (gif_set(svm))
1525 kvm_state.flags |= KVM_STATE_NESTED_GIF_SET;
1526
1527 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
1528 return -EFAULT;
1529
1530 if (!is_guest_mode(vcpu))
1531 goto out;
1532
1533 /*
1534 * Copy over the full size of the VMCB rather than just the size
1535 * of the structs.
1536 */
1537 if (clear_user(user_vmcb, KVM_STATE_NESTED_SVM_VMCB_SIZE))
1538 return -EFAULT;
1539
1540 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
1541 if (!ctl)
1542 return -ENOMEM;
1543
1544 nested_copy_vmcb_cache_to_control(ctl, &svm->nested.ctl);
1545 r = copy_to_user(&user_vmcb->control, ctl,
1546 sizeof(user_vmcb->control));
1547 kfree(ctl);
1548 if (r)
1549 return -EFAULT;
1550
1551 if (copy_to_user(&user_vmcb->save, &svm->vmcb01.ptr->save,
1552 sizeof(user_vmcb->save)))
1553 return -EFAULT;
1554 out:
1555 return kvm_state.size;
1556 }
1557
svm_set_nested_state(struct kvm_vcpu * vcpu,struct kvm_nested_state __user * user_kvm_nested_state,struct kvm_nested_state * kvm_state)1558 static int svm_set_nested_state(struct kvm_vcpu *vcpu,
1559 struct kvm_nested_state __user *user_kvm_nested_state,
1560 struct kvm_nested_state *kvm_state)
1561 {
1562 struct vcpu_svm *svm = to_svm(vcpu);
1563 struct vmcb __user *user_vmcb = (struct vmcb __user *)
1564 &user_kvm_nested_state->data.svm[0];
1565 struct vmcb_control_area *ctl;
1566 struct vmcb_save_area *save;
1567 struct vmcb_save_area_cached save_cached;
1568 struct vmcb_ctrl_area_cached ctl_cached;
1569 unsigned long cr0;
1570 int ret;
1571
1572 BUILD_BUG_ON(sizeof(struct vmcb_control_area) + sizeof(struct vmcb_save_area) >
1573 KVM_STATE_NESTED_SVM_VMCB_SIZE);
1574
1575 if (kvm_state->format != KVM_STATE_NESTED_FORMAT_SVM)
1576 return -EINVAL;
1577
1578 if (kvm_state->flags & ~(KVM_STATE_NESTED_GUEST_MODE |
1579 KVM_STATE_NESTED_RUN_PENDING |
1580 KVM_STATE_NESTED_GIF_SET))
1581 return -EINVAL;
1582
1583 /*
1584 * If in guest mode, vcpu->arch.efer actually refers to the L2 guest's
1585 * EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed.
1586 */
1587 if (!(vcpu->arch.efer & EFER_SVME)) {
1588 /* GIF=1 and no guest mode are required if SVME=0. */
1589 if (kvm_state->flags != KVM_STATE_NESTED_GIF_SET)
1590 return -EINVAL;
1591 }
1592
1593 /* SMM temporarily disables SVM, so we cannot be in guest mode. */
1594 if (is_smm(vcpu) && (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
1595 return -EINVAL;
1596
1597 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) {
1598 svm_leave_nested(vcpu);
1599 svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));
1600 return 0;
1601 }
1602
1603 if (!page_address_valid(vcpu, kvm_state->hdr.svm.vmcb_pa))
1604 return -EINVAL;
1605 if (kvm_state->size < sizeof(*kvm_state) + KVM_STATE_NESTED_SVM_VMCB_SIZE)
1606 return -EINVAL;
1607
1608 ret = -ENOMEM;
1609 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL_ACCOUNT);
1610 save = kzalloc(sizeof(*save), GFP_KERNEL_ACCOUNT);
1611 if (!ctl || !save)
1612 goto out_free;
1613
1614 ret = -EFAULT;
1615 if (copy_from_user(ctl, &user_vmcb->control, sizeof(*ctl)))
1616 goto out_free;
1617 if (copy_from_user(save, &user_vmcb->save, sizeof(*save)))
1618 goto out_free;
1619
1620 ret = -EINVAL;
1621 __nested_copy_vmcb_control_to_cache(vcpu, &ctl_cached, ctl);
1622 if (!__nested_vmcb_check_controls(vcpu, &ctl_cached))
1623 goto out_free;
1624
1625 /*
1626 * Processor state contains L2 state. Check that it is
1627 * valid for guest mode (see nested_vmcb_check_save).
1628 */
1629 cr0 = kvm_read_cr0(vcpu);
1630 if (((cr0 & X86_CR0_CD) == 0) && (cr0 & X86_CR0_NW))
1631 goto out_free;
1632
1633 /*
1634 * Validate host state saved from before VMRUN (see
1635 * nested_svm_check_permissions).
1636 */
1637 __nested_copy_vmcb_save_to_cache(&save_cached, save);
1638 if (!(save->cr0 & X86_CR0_PG) ||
1639 !(save->cr0 & X86_CR0_PE) ||
1640 (save->rflags & X86_EFLAGS_VM) ||
1641 !__nested_vmcb_check_save(vcpu, &save_cached))
1642 goto out_free;
1643
1644
1645 /*
1646 * All checks done, we can enter guest mode. Userspace provides
1647 * vmcb12.control, which will be combined with L1 and stored into
1648 * vmcb02, and the L1 save state which we store in vmcb01.
1649 * L2 registers if needed are moved from the current VMCB to VMCB02.
1650 */
1651
1652 if (is_guest_mode(vcpu))
1653 svm_leave_nested(vcpu);
1654 else
1655 svm->nested.vmcb02.ptr->save = svm->vmcb01.ptr->save;
1656
1657 svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));
1658
1659 svm->nested.nested_run_pending =
1660 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
1661
1662 svm->nested.vmcb12_gpa = kvm_state->hdr.svm.vmcb_pa;
1663
1664 svm_copy_vmrun_state(&svm->vmcb01.ptr->save, save);
1665 nested_copy_vmcb_control_to_cache(svm, ctl);
1666
1667 svm_switch_vmcb(svm, &svm->nested.vmcb02);
1668 nested_vmcb02_prepare_control(svm, svm->vmcb->save.rip, svm->vmcb->save.cs.base);
1669
1670 /*
1671 * While the nested guest CR3 is already checked and set by
1672 * KVM_SET_SREGS, it was set when nested state was yet loaded,
1673 * thus MMU might not be initialized correctly.
1674 * Set it again to fix this.
1675 */
1676
1677 ret = nested_svm_load_cr3(&svm->vcpu, vcpu->arch.cr3,
1678 nested_npt_enabled(svm), false);
1679 if (WARN_ON_ONCE(ret))
1680 goto out_free;
1681
1682 svm->nested.force_msr_bitmap_recalc = true;
1683
1684 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
1685 ret = 0;
1686 out_free:
1687 kfree(save);
1688 kfree(ctl);
1689
1690 return ret;
1691 }
1692
svm_get_nested_state_pages(struct kvm_vcpu * vcpu)1693 static bool svm_get_nested_state_pages(struct kvm_vcpu *vcpu)
1694 {
1695 struct vcpu_svm *svm = to_svm(vcpu);
1696
1697 if (WARN_ON(!is_guest_mode(vcpu)))
1698 return true;
1699
1700 if (!vcpu->arch.pdptrs_from_userspace &&
1701 !nested_npt_enabled(svm) && is_pae_paging(vcpu))
1702 /*
1703 * Reload the guest's PDPTRs since after a migration
1704 * the guest CR3 might be restored prior to setting the nested
1705 * state which can lead to a load of wrong PDPTRs.
1706 */
1707 if (CC(!load_pdptrs(vcpu, vcpu->arch.cr3)))
1708 return false;
1709
1710 if (!nested_svm_vmrun_msrpm(svm)) {
1711 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1712 vcpu->run->internal.suberror =
1713 KVM_INTERNAL_ERROR_EMULATION;
1714 vcpu->run->internal.ndata = 0;
1715 return false;
1716 }
1717
1718 return true;
1719 }
1720
1721 struct kvm_x86_nested_ops svm_nested_ops = {
1722 .leave_nested = svm_leave_nested,
1723 .is_exception_vmexit = nested_svm_is_exception_vmexit,
1724 .check_events = svm_check_nested_events,
1725 .triple_fault = nested_svm_triple_fault,
1726 .get_nested_state_pages = svm_get_nested_state_pages,
1727 .get_state = svm_get_nested_state,
1728 .set_state = svm_set_nested_state,
1729 };
1730