1 // SPDX-License-Identifier: GPL-2.0-only
3 * Kernel-based Virtual Machine driver for Linux
7 * Copyright (C) 2006 Qumranet, Inc.
8 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
11 * Yaniv Kamay <yaniv@qumranet.com>
12 * Avi Kivity <avi@qumranet.com>
15 #define pr_fmt(fmt) "SVM: " fmt
17 #include <linux/kvm_types.h>
18 #include <linux/kvm_host.h>
19 #include <linux/kernel.h>
21 #include <asm/msr-index.h>
22 #include <asm/debugreg.h>
24 #include "kvm_emulate.h"
33 #define CC KVM_NESTED_VMENTER_CONSISTENCY_CHECK
35 static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
36 struct x86_exception *fault)
38 struct vcpu_svm *svm = to_svm(vcpu);
39 struct vmcb *vmcb = svm->vmcb;
41 if (vmcb->control.exit_code != SVM_EXIT_NPF) {
43 * TODO: track the cause of the nested page fault, and
44 * correctly fill in the high bits of exit_info_1.
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;
52 vmcb->control.exit_info_1 &= ~0xffffffffULL;
53 vmcb->control.exit_info_1 |= fault->error_code;
55 nested_svm_vmexit(svm);
58 static bool nested_svm_handle_page_fault_workaround(struct kvm_vcpu *vcpu,
59 struct x86_exception *fault)
61 struct vcpu_svm *svm = to_svm(vcpu);
62 struct vmcb *vmcb = svm->vmcb;
64 WARN_ON(!is_guest_mode(vcpu));
66 if (vmcb12_is_intercept(&svm->nested.ctl,
67 INTERCEPT_EXCEPTION_OFFSET + PF_VECTOR) &&
68 !WARN_ON_ONCE(svm->nested.nested_run_pending)) {
69 vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + PF_VECTOR;
70 vmcb->control.exit_code_hi = 0;
71 vmcb->control.exit_info_1 = fault->error_code;
72 vmcb->control.exit_info_2 = fault->address;
73 nested_svm_vmexit(svm);
80 static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
82 struct vcpu_svm *svm = to_svm(vcpu);
83 u64 cr3 = svm->nested.ctl.nested_cr3;
87 ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(cr3), &pdpte,
88 offset_in_page(cr3) + index * 8, 8);
94 static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
96 struct vcpu_svm *svm = to_svm(vcpu);
98 return svm->nested.ctl.nested_cr3;
101 static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
103 struct vcpu_svm *svm = to_svm(vcpu);
105 WARN_ON(mmu_is_nested(vcpu));
107 vcpu->arch.mmu = &vcpu->arch.guest_mmu;
110 * The NPT format depends on L1's CR4 and EFER, which is in vmcb01. Note,
111 * when called via KVM_SET_NESTED_STATE, that state may _not_ match current
112 * vCPU state. CR0.WP is explicitly ignored, while CR0.PG is required.
114 kvm_init_shadow_npt_mmu(vcpu, X86_CR0_PG, svm->vmcb01.ptr->save.cr4,
115 svm->vmcb01.ptr->save.efer,
116 svm->nested.ctl.nested_cr3);
117 vcpu->arch.mmu->get_guest_pgd = nested_svm_get_tdp_cr3;
118 vcpu->arch.mmu->get_pdptr = nested_svm_get_tdp_pdptr;
119 vcpu->arch.mmu->inject_page_fault = nested_svm_inject_npf_exit;
120 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
123 static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
125 vcpu->arch.mmu = &vcpu->arch.root_mmu;
126 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
129 static bool nested_vmcb_needs_vls_intercept(struct vcpu_svm *svm)
131 if (!svm->v_vmload_vmsave_enabled)
134 if (!nested_npt_enabled(svm))
137 if (!(svm->nested.ctl.virt_ext & VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK))
143 void recalc_intercepts(struct vcpu_svm *svm)
145 struct vmcb_control_area *c, *h;
146 struct vmcb_ctrl_area_cached *g;
149 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
151 if (!is_guest_mode(&svm->vcpu))
154 c = &svm->vmcb->control;
155 h = &svm->vmcb01.ptr->control;
156 g = &svm->nested.ctl;
158 for (i = 0; i < MAX_INTERCEPT; i++)
159 c->intercepts[i] = h->intercepts[i];
161 if (g->int_ctl & V_INTR_MASKING_MASK) {
162 /* We only want the cr8 intercept bits of L1 */
163 vmcb_clr_intercept(c, INTERCEPT_CR8_READ);
164 vmcb_clr_intercept(c, INTERCEPT_CR8_WRITE);
167 * Once running L2 with HF_VINTR_MASK, EFLAGS.IF does not
168 * affect any interrupt we may want to inject; therefore,
169 * interrupt window vmexits are irrelevant to L0.
171 vmcb_clr_intercept(c, INTERCEPT_VINTR);
174 /* We don't want to see VMMCALLs from a nested guest */
175 vmcb_clr_intercept(c, INTERCEPT_VMMCALL);
177 for (i = 0; i < MAX_INTERCEPT; i++)
178 c->intercepts[i] |= g->intercepts[i];
180 /* If SMI is not intercepted, ignore guest SMI intercept as well */
182 vmcb_clr_intercept(c, INTERCEPT_SMI);
184 if (nested_vmcb_needs_vls_intercept(svm)) {
186 * If the virtual VMLOAD/VMSAVE is not enabled for the L2,
187 * we must intercept these instructions to correctly
188 * emulate them in case L1 doesn't intercept them.
190 vmcb_set_intercept(c, INTERCEPT_VMLOAD);
191 vmcb_set_intercept(c, INTERCEPT_VMSAVE);
193 WARN_ON(!(c->virt_ext & VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK));
198 * Merge L0's (KVM) and L1's (Nested VMCB) MSR permission bitmaps. The function
199 * is optimized in that it only merges the parts where KVM MSR permission bitmap
200 * may contain zero bits.
202 static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
204 struct hv_enlightenments *hve =
205 (struct hv_enlightenments *)svm->nested.ctl.reserved_sw;
209 * MSR bitmap update can be skipped when:
210 * - MSR bitmap for L1 hasn't changed.
211 * - Nested hypervisor (L1) is attempting to launch the same L2 as
213 * - Nested hypervisor (L1) is using Hyper-V emulation interface and
214 * tells KVM (L0) there were no changes in MSR bitmap for L2.
216 if (!svm->nested.force_msr_bitmap_recalc &&
217 kvm_hv_hypercall_enabled(&svm->vcpu) &&
218 hve->hv_enlightenments_control.msr_bitmap &&
219 (svm->nested.ctl.clean & BIT(VMCB_HV_NESTED_ENLIGHTENMENTS)))
220 goto set_msrpm_base_pa;
222 if (!(vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
225 for (i = 0; i < MSRPM_OFFSETS; i++) {
229 if (msrpm_offsets[i] == 0xffffffff)
232 p = msrpm_offsets[i];
233 offset = svm->nested.ctl.msrpm_base_pa + (p * 4);
235 if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
238 svm->nested.msrpm[p] = svm->msrpm[p] | value;
241 svm->nested.force_msr_bitmap_recalc = false;
244 svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm));
250 * Bits 11:0 of bitmap address are ignored by hardware
252 static bool nested_svm_check_bitmap_pa(struct kvm_vcpu *vcpu, u64 pa, u32 size)
254 u64 addr = PAGE_ALIGN(pa);
256 return kvm_vcpu_is_legal_gpa(vcpu, addr) &&
257 kvm_vcpu_is_legal_gpa(vcpu, addr + size - 1);
260 static bool nested_svm_check_tlb_ctl(struct kvm_vcpu *vcpu, u8 tlb_ctl)
262 /* Nested FLUSHBYASID is not supported yet. */
264 case TLB_CONTROL_DO_NOTHING:
265 case TLB_CONTROL_FLUSH_ALL_ASID:
272 static bool __nested_vmcb_check_controls(struct kvm_vcpu *vcpu,
273 struct vmcb_ctrl_area_cached *control)
275 if (CC(!vmcb12_is_intercept(control, INTERCEPT_VMRUN)))
278 if (CC(control->asid == 0))
281 if (CC((control->nested_ctl & SVM_NESTED_CTL_NP_ENABLE) && !npt_enabled))
284 if (CC(!nested_svm_check_bitmap_pa(vcpu, control->msrpm_base_pa,
287 if (CC(!nested_svm_check_bitmap_pa(vcpu, control->iopm_base_pa,
291 if (CC(!nested_svm_check_tlb_ctl(vcpu, control->tlb_ctl)))
297 /* Common checks that apply to both L1 and L2 state. */
298 static bool __nested_vmcb_check_save(struct kvm_vcpu *vcpu,
299 struct vmcb_save_area_cached *save)
301 if (CC(!(save->efer & EFER_SVME)))
304 if (CC((save->cr0 & X86_CR0_CD) == 0 && (save->cr0 & X86_CR0_NW)) ||
305 CC(save->cr0 & ~0xffffffffULL))
308 if (CC(!kvm_dr6_valid(save->dr6)) || CC(!kvm_dr7_valid(save->dr7)))
312 * These checks are also performed by KVM_SET_SREGS,
313 * except that EFER.LMA is not checked by SVM against
314 * CR0.PG && EFER.LME.
316 if ((save->efer & EFER_LME) && (save->cr0 & X86_CR0_PG)) {
317 if (CC(!(save->cr4 & X86_CR4_PAE)) ||
318 CC(!(save->cr0 & X86_CR0_PE)) ||
319 CC(kvm_vcpu_is_illegal_gpa(vcpu, save->cr3)))
323 if (CC(!kvm_is_valid_cr4(vcpu, save->cr4)))
326 if (CC(!kvm_valid_efer(vcpu, save->efer)))
332 static bool nested_vmcb_check_save(struct kvm_vcpu *vcpu)
334 struct vcpu_svm *svm = to_svm(vcpu);
335 struct vmcb_save_area_cached *save = &svm->nested.save;
337 return __nested_vmcb_check_save(vcpu, save);
340 static bool nested_vmcb_check_controls(struct kvm_vcpu *vcpu)
342 struct vcpu_svm *svm = to_svm(vcpu);
343 struct vmcb_ctrl_area_cached *ctl = &svm->nested.ctl;
345 return __nested_vmcb_check_controls(vcpu, ctl);
349 void __nested_copy_vmcb_control_to_cache(struct kvm_vcpu *vcpu,
350 struct vmcb_ctrl_area_cached *to,
351 struct vmcb_control_area *from)
355 for (i = 0; i < MAX_INTERCEPT; i++)
356 to->intercepts[i] = from->intercepts[i];
358 to->iopm_base_pa = from->iopm_base_pa;
359 to->msrpm_base_pa = from->msrpm_base_pa;
360 to->tsc_offset = from->tsc_offset;
361 to->tlb_ctl = from->tlb_ctl;
362 to->int_ctl = from->int_ctl;
363 to->int_vector = from->int_vector;
364 to->int_state = from->int_state;
365 to->exit_code = from->exit_code;
366 to->exit_code_hi = from->exit_code_hi;
367 to->exit_info_1 = from->exit_info_1;
368 to->exit_info_2 = from->exit_info_2;
369 to->exit_int_info = from->exit_int_info;
370 to->exit_int_info_err = from->exit_int_info_err;
371 to->nested_ctl = from->nested_ctl;
372 to->event_inj = from->event_inj;
373 to->event_inj_err = from->event_inj_err;
374 to->nested_cr3 = from->nested_cr3;
375 to->virt_ext = from->virt_ext;
376 to->pause_filter_count = from->pause_filter_count;
377 to->pause_filter_thresh = from->pause_filter_thresh;
379 /* Copy asid here because nested_vmcb_check_controls will check it. */
380 to->asid = from->asid;
381 to->msrpm_base_pa &= ~0x0fffULL;
382 to->iopm_base_pa &= ~0x0fffULL;
384 /* Hyper-V extensions (Enlightened VMCB) */
385 if (kvm_hv_hypercall_enabled(vcpu)) {
386 to->clean = from->clean;
387 memcpy(to->reserved_sw, from->reserved_sw,
388 sizeof(struct hv_enlightenments));
392 void nested_copy_vmcb_control_to_cache(struct vcpu_svm *svm,
393 struct vmcb_control_area *control)
395 __nested_copy_vmcb_control_to_cache(&svm->vcpu, &svm->nested.ctl, control);
398 static void __nested_copy_vmcb_save_to_cache(struct vmcb_save_area_cached *to,
399 struct vmcb_save_area *from)
402 * Copy only fields that are validated, as we need them
403 * to avoid TOC/TOU races.
405 to->efer = from->efer;
414 void nested_copy_vmcb_save_to_cache(struct vcpu_svm *svm,
415 struct vmcb_save_area *save)
417 __nested_copy_vmcb_save_to_cache(&svm->nested.save, save);
421 * Synchronize fields that are written by the processor, so that
422 * they can be copied back into the vmcb12.
424 void nested_sync_control_from_vmcb02(struct vcpu_svm *svm)
427 svm->nested.ctl.event_inj = svm->vmcb->control.event_inj;
428 svm->nested.ctl.event_inj_err = svm->vmcb->control.event_inj_err;
430 /* Only a few fields of int_ctl are written by the processor. */
431 mask = V_IRQ_MASK | V_TPR_MASK;
432 if (!(svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK) &&
433 svm_is_intercept(svm, INTERCEPT_VINTR)) {
435 * In order to request an interrupt window, L0 is usurping
436 * svm->vmcb->control.int_ctl and possibly setting V_IRQ
437 * even if it was clear in L1's VMCB. Restoring it would be
438 * wrong. However, in this case V_IRQ will remain true until
439 * interrupt_window_interception calls svm_clear_vintr and
440 * restores int_ctl. We can just leave it aside.
445 if (nested_vgif_enabled(svm))
448 svm->nested.ctl.int_ctl &= ~mask;
449 svm->nested.ctl.int_ctl |= svm->vmcb->control.int_ctl & mask;
453 * Transfer any event that L0 or L1 wanted to inject into L2 to
456 static void nested_save_pending_event_to_vmcb12(struct vcpu_svm *svm,
459 struct kvm_vcpu *vcpu = &svm->vcpu;
460 u32 exit_int_info = 0;
463 if (vcpu->arch.exception.injected) {
464 nr = vcpu->arch.exception.nr;
465 exit_int_info = nr | SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_EXEPT;
467 if (vcpu->arch.exception.has_error_code) {
468 exit_int_info |= SVM_EVTINJ_VALID_ERR;
469 vmcb12->control.exit_int_info_err =
470 vcpu->arch.exception.error_code;
473 } else if (vcpu->arch.nmi_injected) {
474 exit_int_info = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
476 } else if (vcpu->arch.interrupt.injected) {
477 nr = vcpu->arch.interrupt.nr;
478 exit_int_info = nr | SVM_EVTINJ_VALID;
480 if (vcpu->arch.interrupt.soft)
481 exit_int_info |= SVM_EVTINJ_TYPE_SOFT;
483 exit_int_info |= SVM_EVTINJ_TYPE_INTR;
486 vmcb12->control.exit_int_info = exit_int_info;
489 static void nested_svm_transition_tlb_flush(struct kvm_vcpu *vcpu)
492 * TODO: optimize unconditional TLB flush/MMU sync. A partial list of
493 * things to fix before this can be conditional:
495 * - Flush TLBs for both L1 and L2 remote TLB flush
496 * - Honor L1's request to flush an ASID on nested VMRUN
497 * - Sync nested NPT MMU on VMRUN that flushes L2's ASID[*]
498 * - Don't crush a pending TLB flush in vmcb02 on nested VMRUN
499 * - Flush L1's ASID on KVM_REQ_TLB_FLUSH_GUEST
501 * [*] Unlike nested EPT, SVM's ASID management can invalidate nested
502 * NPT guest-physical mappings on VMRUN.
504 kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
505 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
509 * Load guest's/host's cr3 on nested vmentry or vmexit. @nested_npt is true
510 * if we are emulating VM-Entry into a guest with NPT enabled.
512 static int nested_svm_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,
513 bool nested_npt, bool reload_pdptrs)
515 if (CC(kvm_vcpu_is_illegal_gpa(vcpu, cr3)))
518 if (reload_pdptrs && !nested_npt && is_pae_paging(vcpu) &&
519 CC(!load_pdptrs(vcpu, cr3)))
522 vcpu->arch.cr3 = cr3;
524 /* Re-initialize the MMU, e.g. to pick up CR4 MMU role changes. */
528 kvm_mmu_new_pgd(vcpu, cr3);
533 void nested_vmcb02_compute_g_pat(struct vcpu_svm *svm)
535 if (!svm->nested.vmcb02.ptr)
538 /* FIXME: merge g_pat from vmcb01 and vmcb12. */
539 svm->nested.vmcb02.ptr->save.g_pat = svm->vmcb01.ptr->save.g_pat;
542 static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *vmcb12)
544 bool new_vmcb12 = false;
545 struct vmcb *vmcb01 = svm->vmcb01.ptr;
546 struct vmcb *vmcb02 = svm->nested.vmcb02.ptr;
548 nested_vmcb02_compute_g_pat(svm);
550 /* Load the nested guest state */
551 if (svm->nested.vmcb12_gpa != svm->nested.last_vmcb12_gpa) {
553 svm->nested.last_vmcb12_gpa = svm->nested.vmcb12_gpa;
554 svm->nested.force_msr_bitmap_recalc = true;
557 if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_SEG))) {
558 vmcb02->save.es = vmcb12->save.es;
559 vmcb02->save.cs = vmcb12->save.cs;
560 vmcb02->save.ss = vmcb12->save.ss;
561 vmcb02->save.ds = vmcb12->save.ds;
562 vmcb02->save.cpl = vmcb12->save.cpl;
563 vmcb_mark_dirty(vmcb02, VMCB_SEG);
566 if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_DT))) {
567 vmcb02->save.gdtr = vmcb12->save.gdtr;
568 vmcb02->save.idtr = vmcb12->save.idtr;
569 vmcb_mark_dirty(vmcb02, VMCB_DT);
572 kvm_set_rflags(&svm->vcpu, vmcb12->save.rflags | X86_EFLAGS_FIXED);
574 svm_set_efer(&svm->vcpu, svm->nested.save.efer);
576 svm_set_cr0(&svm->vcpu, svm->nested.save.cr0);
577 svm_set_cr4(&svm->vcpu, svm->nested.save.cr4);
579 svm->vcpu.arch.cr2 = vmcb12->save.cr2;
581 kvm_rax_write(&svm->vcpu, vmcb12->save.rax);
582 kvm_rsp_write(&svm->vcpu, vmcb12->save.rsp);
583 kvm_rip_write(&svm->vcpu, vmcb12->save.rip);
585 /* In case we don't even reach vcpu_run, the fields are not updated */
586 vmcb02->save.rax = vmcb12->save.rax;
587 vmcb02->save.rsp = vmcb12->save.rsp;
588 vmcb02->save.rip = vmcb12->save.rip;
590 /* These bits will be set properly on the first execution when new_vmc12 is true */
591 if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_DR))) {
592 vmcb02->save.dr7 = svm->nested.save.dr7 | DR7_FIXED_1;
593 svm->vcpu.arch.dr6 = svm->nested.save.dr6 | DR6_ACTIVE_LOW;
594 vmcb_mark_dirty(vmcb02, VMCB_DR);
597 if (unlikely(svm->lbrv_enabled && (svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK))) {
599 * Reserved bits of DEBUGCTL are ignored. Be consistent with
600 * svm_set_msr's definition of reserved bits.
602 svm_copy_lbrs(vmcb02, vmcb12);
603 vmcb02->save.dbgctl &= ~DEBUGCTL_RESERVED_BITS;
604 svm_update_lbrv(&svm->vcpu);
606 } else if (unlikely(vmcb01->control.virt_ext & LBR_CTL_ENABLE_MASK)) {
607 svm_copy_lbrs(vmcb02, vmcb01);
611 static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)
613 u32 int_ctl_vmcb01_bits = V_INTR_MASKING_MASK;
614 u32 int_ctl_vmcb12_bits = V_TPR_MASK | V_IRQ_INJECTION_BITS_MASK;
616 struct kvm_vcpu *vcpu = &svm->vcpu;
617 struct vmcb *vmcb01 = svm->vmcb01.ptr;
618 struct vmcb *vmcb02 = svm->nested.vmcb02.ptr;
623 * Filled at exit: exit_code, exit_code_hi, exit_info_1, exit_info_2,
624 * exit_int_info, exit_int_info_err, next_rip, insn_len, insn_bytes.
627 if (svm->vgif_enabled && (svm->nested.ctl.int_ctl & V_GIF_ENABLE_MASK))
628 int_ctl_vmcb12_bits |= (V_GIF_MASK | V_GIF_ENABLE_MASK);
630 int_ctl_vmcb01_bits |= (V_GIF_MASK | V_GIF_ENABLE_MASK);
632 /* Copied from vmcb01. msrpm_base can be overwritten later. */
633 vmcb02->control.nested_ctl = vmcb01->control.nested_ctl;
634 vmcb02->control.iopm_base_pa = vmcb01->control.iopm_base_pa;
635 vmcb02->control.msrpm_base_pa = vmcb01->control.msrpm_base_pa;
637 /* Done at vmrun: asid. */
639 /* Also overwritten later if necessary. */
640 vmcb02->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
643 if (nested_npt_enabled(svm))
644 nested_svm_init_mmu_context(vcpu);
646 vcpu->arch.tsc_offset = kvm_calc_nested_tsc_offset(
647 vcpu->arch.l1_tsc_offset,
648 svm->nested.ctl.tsc_offset,
651 vmcb02->control.tsc_offset = vcpu->arch.tsc_offset;
653 if (svm->tsc_ratio_msr != kvm_default_tsc_scaling_ratio) {
654 WARN_ON(!svm->tsc_scaling_enabled);
655 nested_svm_update_tsc_ratio_msr(vcpu);
658 vmcb02->control.int_ctl =
659 (svm->nested.ctl.int_ctl & int_ctl_vmcb12_bits) |
660 (vmcb01->control.int_ctl & int_ctl_vmcb01_bits);
662 vmcb02->control.int_vector = svm->nested.ctl.int_vector;
663 vmcb02->control.int_state = svm->nested.ctl.int_state;
664 vmcb02->control.event_inj = svm->nested.ctl.event_inj;
665 vmcb02->control.event_inj_err = svm->nested.ctl.event_inj_err;
667 vmcb02->control.virt_ext = vmcb01->control.virt_ext &
669 if (svm->lbrv_enabled)
670 vmcb02->control.virt_ext |=
671 (svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK);
673 if (!nested_vmcb_needs_vls_intercept(svm))
674 vmcb02->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
676 pause_count12 = svm->pause_filter_enabled ? svm->nested.ctl.pause_filter_count : 0;
677 pause_thresh12 = svm->pause_threshold_enabled ? svm->nested.ctl.pause_filter_thresh : 0;
678 if (kvm_pause_in_guest(svm->vcpu.kvm)) {
679 /* use guest values since host doesn't intercept PAUSE */
680 vmcb02->control.pause_filter_count = pause_count12;
681 vmcb02->control.pause_filter_thresh = pause_thresh12;
684 /* start from host values otherwise */
685 vmcb02->control.pause_filter_count = vmcb01->control.pause_filter_count;
686 vmcb02->control.pause_filter_thresh = vmcb01->control.pause_filter_thresh;
688 /* ... but ensure filtering is disabled if so requested. */
689 if (vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_PAUSE)) {
691 vmcb02->control.pause_filter_count = 0;
693 vmcb02->control.pause_filter_thresh = 0;
697 nested_svm_transition_tlb_flush(vcpu);
699 /* Enter Guest-Mode */
700 enter_guest_mode(vcpu);
703 * Merge guest and host intercepts - must be called with vcpu in
704 * guest-mode to take effect.
706 recalc_intercepts(svm);
709 static void nested_svm_copy_common_state(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
712 * Some VMCB state is shared between L1 and L2 and thus has to be
713 * moved at the time of nested vmrun and vmexit.
715 * VMLOAD/VMSAVE state would also belong in this category, but KVM
716 * always performs VMLOAD and VMSAVE from the VMCB01.
718 to_vmcb->save.spec_ctrl = from_vmcb->save.spec_ctrl;
721 int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb12_gpa,
722 struct vmcb *vmcb12, bool from_vmrun)
724 struct vcpu_svm *svm = to_svm(vcpu);
727 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb12_gpa,
729 vmcb12->control.int_ctl,
730 vmcb12->control.event_inj,
731 vmcb12->control.nested_ctl);
733 trace_kvm_nested_intercepts(vmcb12->control.intercepts[INTERCEPT_CR] & 0xffff,
734 vmcb12->control.intercepts[INTERCEPT_CR] >> 16,
735 vmcb12->control.intercepts[INTERCEPT_EXCEPTION],
736 vmcb12->control.intercepts[INTERCEPT_WORD3],
737 vmcb12->control.intercepts[INTERCEPT_WORD4],
738 vmcb12->control.intercepts[INTERCEPT_WORD5]);
741 svm->nested.vmcb12_gpa = vmcb12_gpa;
743 WARN_ON(svm->vmcb == svm->nested.vmcb02.ptr);
745 nested_svm_copy_common_state(svm->vmcb01.ptr, svm->nested.vmcb02.ptr);
747 svm_switch_vmcb(svm, &svm->nested.vmcb02);
748 nested_vmcb02_prepare_control(svm);
749 nested_vmcb02_prepare_save(svm, vmcb12);
751 ret = nested_svm_load_cr3(&svm->vcpu, svm->nested.save.cr3,
752 nested_npt_enabled(svm), from_vmrun);
757 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
759 svm_set_gif(svm, true);
761 if (kvm_vcpu_apicv_active(vcpu))
762 kvm_make_request(KVM_REQ_APICV_UPDATE, vcpu);
767 int nested_svm_vmrun(struct kvm_vcpu *vcpu)
769 struct vcpu_svm *svm = to_svm(vcpu);
772 struct kvm_host_map map;
774 struct vmcb *vmcb01 = svm->vmcb01.ptr;
776 if (!svm->nested.hsave_msr) {
777 kvm_inject_gp(vcpu, 0);
782 kvm_queue_exception(vcpu, UD_VECTOR);
786 vmcb12_gpa = svm->vmcb->save.rax;
787 ret = kvm_vcpu_map(vcpu, gpa_to_gfn(vmcb12_gpa), &map);
788 if (ret == -EINVAL) {
789 kvm_inject_gp(vcpu, 0);
792 return kvm_skip_emulated_instruction(vcpu);
795 ret = kvm_skip_emulated_instruction(vcpu);
799 if (WARN_ON_ONCE(!svm->nested.initialized))
802 nested_copy_vmcb_control_to_cache(svm, &vmcb12->control);
803 nested_copy_vmcb_save_to_cache(svm, &vmcb12->save);
805 if (!nested_vmcb_check_save(vcpu) ||
806 !nested_vmcb_check_controls(vcpu)) {
807 vmcb12->control.exit_code = SVM_EXIT_ERR;
808 vmcb12->control.exit_code_hi = 0;
809 vmcb12->control.exit_info_1 = 0;
810 vmcb12->control.exit_info_2 = 0;
815 * Since vmcb01 is not in use, we can use it to store some of the L1
818 vmcb01->save.efer = vcpu->arch.efer;
819 vmcb01->save.cr0 = kvm_read_cr0(vcpu);
820 vmcb01->save.cr4 = vcpu->arch.cr4;
821 vmcb01->save.rflags = kvm_get_rflags(vcpu);
822 vmcb01->save.rip = kvm_rip_read(vcpu);
825 vmcb01->save.cr3 = kvm_read_cr3(vcpu);
827 svm->nested.nested_run_pending = 1;
829 if (enter_svm_guest_mode(vcpu, vmcb12_gpa, vmcb12, true))
832 if (nested_svm_vmrun_msrpm(svm))
836 svm->nested.nested_run_pending = 0;
838 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
839 svm->vmcb->control.exit_code_hi = 0;
840 svm->vmcb->control.exit_info_1 = 0;
841 svm->vmcb->control.exit_info_2 = 0;
843 nested_svm_vmexit(svm);
846 kvm_vcpu_unmap(vcpu, &map, true);
851 /* Copy state save area fields which are handled by VMRUN */
852 void svm_copy_vmrun_state(struct vmcb_save_area *to_save,
853 struct vmcb_save_area *from_save)
855 to_save->es = from_save->es;
856 to_save->cs = from_save->cs;
857 to_save->ss = from_save->ss;
858 to_save->ds = from_save->ds;
859 to_save->gdtr = from_save->gdtr;
860 to_save->idtr = from_save->idtr;
861 to_save->rflags = from_save->rflags | X86_EFLAGS_FIXED;
862 to_save->efer = from_save->efer;
863 to_save->cr0 = from_save->cr0;
864 to_save->cr3 = from_save->cr3;
865 to_save->cr4 = from_save->cr4;
866 to_save->rax = from_save->rax;
867 to_save->rsp = from_save->rsp;
868 to_save->rip = from_save->rip;
872 void svm_copy_vmloadsave_state(struct vmcb *to_vmcb, struct vmcb *from_vmcb)
874 to_vmcb->save.fs = from_vmcb->save.fs;
875 to_vmcb->save.gs = from_vmcb->save.gs;
876 to_vmcb->save.tr = from_vmcb->save.tr;
877 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
878 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
879 to_vmcb->save.star = from_vmcb->save.star;
880 to_vmcb->save.lstar = from_vmcb->save.lstar;
881 to_vmcb->save.cstar = from_vmcb->save.cstar;
882 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
883 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
884 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
885 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
888 int nested_svm_vmexit(struct vcpu_svm *svm)
890 struct kvm_vcpu *vcpu = &svm->vcpu;
891 struct vmcb *vmcb01 = svm->vmcb01.ptr;
892 struct vmcb *vmcb02 = svm->nested.vmcb02.ptr;
894 struct kvm_host_map map;
897 rc = kvm_vcpu_map(vcpu, gpa_to_gfn(svm->nested.vmcb12_gpa), &map);
900 kvm_inject_gp(vcpu, 0);
906 /* Exit Guest-Mode */
907 leave_guest_mode(vcpu);
908 svm->nested.vmcb12_gpa = 0;
909 WARN_ON_ONCE(svm->nested.nested_run_pending);
911 kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
913 /* in case we halted in L2 */
914 svm->vcpu.arch.mp_state = KVM_MP_STATE_RUNNABLE;
916 /* Give the current vmcb to the guest */
918 vmcb12->save.es = vmcb02->save.es;
919 vmcb12->save.cs = vmcb02->save.cs;
920 vmcb12->save.ss = vmcb02->save.ss;
921 vmcb12->save.ds = vmcb02->save.ds;
922 vmcb12->save.gdtr = vmcb02->save.gdtr;
923 vmcb12->save.idtr = vmcb02->save.idtr;
924 vmcb12->save.efer = svm->vcpu.arch.efer;
925 vmcb12->save.cr0 = kvm_read_cr0(vcpu);
926 vmcb12->save.cr3 = kvm_read_cr3(vcpu);
927 vmcb12->save.cr2 = vmcb02->save.cr2;
928 vmcb12->save.cr4 = svm->vcpu.arch.cr4;
929 vmcb12->save.rflags = kvm_get_rflags(vcpu);
930 vmcb12->save.rip = kvm_rip_read(vcpu);
931 vmcb12->save.rsp = kvm_rsp_read(vcpu);
932 vmcb12->save.rax = kvm_rax_read(vcpu);
933 vmcb12->save.dr7 = vmcb02->save.dr7;
934 vmcb12->save.dr6 = svm->vcpu.arch.dr6;
935 vmcb12->save.cpl = vmcb02->save.cpl;
937 vmcb12->control.int_state = vmcb02->control.int_state;
938 vmcb12->control.exit_code = vmcb02->control.exit_code;
939 vmcb12->control.exit_code_hi = vmcb02->control.exit_code_hi;
940 vmcb12->control.exit_info_1 = vmcb02->control.exit_info_1;
941 vmcb12->control.exit_info_2 = vmcb02->control.exit_info_2;
943 if (vmcb12->control.exit_code != SVM_EXIT_ERR)
944 nested_save_pending_event_to_vmcb12(svm, vmcb12);
946 if (svm->nrips_enabled)
947 vmcb12->control.next_rip = vmcb02->control.next_rip;
949 vmcb12->control.int_ctl = svm->nested.ctl.int_ctl;
950 vmcb12->control.tlb_ctl = svm->nested.ctl.tlb_ctl;
951 vmcb12->control.event_inj = svm->nested.ctl.event_inj;
952 vmcb12->control.event_inj_err = svm->nested.ctl.event_inj_err;
954 if (!kvm_pause_in_guest(vcpu->kvm)) {
955 vmcb01->control.pause_filter_count = vmcb02->control.pause_filter_count;
956 vmcb_mark_dirty(vmcb01, VMCB_INTERCEPTS);
960 nested_svm_copy_common_state(svm->nested.vmcb02.ptr, svm->vmcb01.ptr);
962 svm_switch_vmcb(svm, &svm->vmcb01);
964 if (unlikely(svm->lbrv_enabled && (svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK))) {
965 svm_copy_lbrs(vmcb12, vmcb02);
966 svm_update_lbrv(vcpu);
967 } else if (unlikely(vmcb01->control.virt_ext & LBR_CTL_ENABLE_MASK)) {
968 svm_copy_lbrs(vmcb01, vmcb02);
969 svm_update_lbrv(vcpu);
973 * On vmexit the GIF is set to false and
974 * no event can be injected in L1.
976 svm_set_gif(svm, false);
977 vmcb01->control.exit_int_info = 0;
979 svm->vcpu.arch.tsc_offset = svm->vcpu.arch.l1_tsc_offset;
980 if (vmcb01->control.tsc_offset != svm->vcpu.arch.tsc_offset) {
981 vmcb01->control.tsc_offset = svm->vcpu.arch.tsc_offset;
982 vmcb_mark_dirty(vmcb01, VMCB_INTERCEPTS);
985 if (svm->tsc_ratio_msr != kvm_default_tsc_scaling_ratio) {
986 WARN_ON(!svm->tsc_scaling_enabled);
987 vcpu->arch.tsc_scaling_ratio = vcpu->arch.l1_tsc_scaling_ratio;
988 __svm_write_tsc_multiplier(vcpu->arch.tsc_scaling_ratio);
991 svm->nested.ctl.nested_cr3 = 0;
994 * Restore processor state that had been saved in vmcb01
996 kvm_set_rflags(vcpu, vmcb01->save.rflags);
997 svm_set_efer(vcpu, vmcb01->save.efer);
998 svm_set_cr0(vcpu, vmcb01->save.cr0 | X86_CR0_PE);
999 svm_set_cr4(vcpu, vmcb01->save.cr4);
1000 kvm_rax_write(vcpu, vmcb01->save.rax);
1001 kvm_rsp_write(vcpu, vmcb01->save.rsp);
1002 kvm_rip_write(vcpu, vmcb01->save.rip);
1004 svm->vcpu.arch.dr7 = DR7_FIXED_1;
1005 kvm_update_dr7(&svm->vcpu);
1007 trace_kvm_nested_vmexit_inject(vmcb12->control.exit_code,
1008 vmcb12->control.exit_info_1,
1009 vmcb12->control.exit_info_2,
1010 vmcb12->control.exit_int_info,
1011 vmcb12->control.exit_int_info_err,
1014 kvm_vcpu_unmap(vcpu, &map, true);
1016 nested_svm_transition_tlb_flush(vcpu);
1018 nested_svm_uninit_mmu_context(vcpu);
1020 rc = nested_svm_load_cr3(vcpu, vmcb01->save.cr3, false, true);
1025 * Drop what we picked up for L2 via svm_complete_interrupts() so it
1026 * doesn't end up in L1.
1028 svm->vcpu.arch.nmi_injected = false;
1029 kvm_clear_exception_queue(vcpu);
1030 kvm_clear_interrupt_queue(vcpu);
1033 * If we are here following the completion of a VMRUN that
1034 * is being single-stepped, queue the pending #DB intercept
1035 * right now so that it an be accounted for before we execute
1036 * L1's next instruction.
1038 if (unlikely(vmcb01->save.rflags & X86_EFLAGS_TF))
1039 kvm_queue_exception(&(svm->vcpu), DB_VECTOR);
1042 * Un-inhibit the AVIC right away, so that other vCPUs can start
1043 * to benefit from it right away.
1045 if (kvm_apicv_activated(vcpu->kvm))
1046 kvm_vcpu_update_apicv(vcpu);
1051 static void nested_svm_triple_fault(struct kvm_vcpu *vcpu)
1053 nested_svm_simple_vmexit(to_svm(vcpu), SVM_EXIT_SHUTDOWN);
1056 int svm_allocate_nested(struct vcpu_svm *svm)
1058 struct page *vmcb02_page;
1060 if (svm->nested.initialized)
1063 vmcb02_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
1066 svm->nested.vmcb02.ptr = page_address(vmcb02_page);
1067 svm->nested.vmcb02.pa = __sme_set(page_to_pfn(vmcb02_page) << PAGE_SHIFT);
1069 svm->nested.msrpm = svm_vcpu_alloc_msrpm();
1070 if (!svm->nested.msrpm)
1071 goto err_free_vmcb02;
1072 svm_vcpu_init_msrpm(&svm->vcpu, svm->nested.msrpm);
1074 svm->nested.initialized = true;
1078 __free_page(vmcb02_page);
1082 void svm_free_nested(struct vcpu_svm *svm)
1084 if (!svm->nested.initialized)
1087 svm_vcpu_free_msrpm(svm->nested.msrpm);
1088 svm->nested.msrpm = NULL;
1090 __free_page(virt_to_page(svm->nested.vmcb02.ptr));
1091 svm->nested.vmcb02.ptr = NULL;
1094 * When last_vmcb12_gpa matches the current vmcb12 gpa,
1095 * some vmcb12 fields are not loaded if they are marked clean
1096 * in the vmcb12, since in this case they are up to date already.
1098 * When the vmcb02 is freed, this optimization becomes invalid.
1100 svm->nested.last_vmcb12_gpa = INVALID_GPA;
1102 svm->nested.initialized = false;
1106 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
1108 void svm_leave_nested(struct kvm_vcpu *vcpu)
1110 struct vcpu_svm *svm = to_svm(vcpu);
1112 if (is_guest_mode(vcpu)) {
1113 svm->nested.nested_run_pending = 0;
1114 svm->nested.vmcb12_gpa = INVALID_GPA;
1116 leave_guest_mode(vcpu);
1118 svm_switch_vmcb(svm, &svm->vmcb01);
1120 nested_svm_uninit_mmu_context(vcpu);
1121 vmcb_mark_all_dirty(svm->vmcb);
1124 kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
1127 static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
1129 u32 offset, msr, value;
1132 if (!(vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
1133 return NESTED_EXIT_HOST;
1135 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
1136 offset = svm_msrpm_offset(msr);
1137 write = svm->vmcb->control.exit_info_1 & 1;
1138 mask = 1 << ((2 * (msr & 0xf)) + write);
1140 if (offset == MSR_INVALID)
1141 return NESTED_EXIT_DONE;
1143 /* Offset is in 32 bit units but need in 8 bit units */
1146 if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.ctl.msrpm_base_pa + offset, &value, 4))
1147 return NESTED_EXIT_DONE;
1149 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
1152 static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
1154 unsigned port, size, iopm_len;
1159 if (!(vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_IOIO_PROT)))
1160 return NESTED_EXIT_HOST;
1162 port = svm->vmcb->control.exit_info_1 >> 16;
1163 size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
1164 SVM_IOIO_SIZE_SHIFT;
1165 gpa = svm->nested.ctl.iopm_base_pa + (port / 8);
1166 start_bit = port % 8;
1167 iopm_len = (start_bit + size > 8) ? 2 : 1;
1168 mask = (0xf >> (4 - size)) << start_bit;
1171 if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
1172 return NESTED_EXIT_DONE;
1174 return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
1177 static int nested_svm_intercept(struct vcpu_svm *svm)
1179 u32 exit_code = svm->vmcb->control.exit_code;
1180 int vmexit = NESTED_EXIT_HOST;
1182 switch (exit_code) {
1184 vmexit = nested_svm_exit_handled_msr(svm);
1187 vmexit = nested_svm_intercept_ioio(svm);
1189 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
1190 if (vmcb12_is_intercept(&svm->nested.ctl, exit_code))
1191 vmexit = NESTED_EXIT_DONE;
1194 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
1195 if (vmcb12_is_intercept(&svm->nested.ctl, exit_code))
1196 vmexit = NESTED_EXIT_DONE;
1199 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1201 * Host-intercepted exceptions have been checked already in
1202 * nested_svm_exit_special. There is nothing to do here,
1203 * the vmexit is injected by svm_check_nested_events.
1205 vmexit = NESTED_EXIT_DONE;
1208 case SVM_EXIT_ERR: {
1209 vmexit = NESTED_EXIT_DONE;
1213 if (vmcb12_is_intercept(&svm->nested.ctl, exit_code))
1214 vmexit = NESTED_EXIT_DONE;
1221 int nested_svm_exit_handled(struct vcpu_svm *svm)
1225 vmexit = nested_svm_intercept(svm);
1227 if (vmexit == NESTED_EXIT_DONE)
1228 nested_svm_vmexit(svm);
1233 int nested_svm_check_permissions(struct kvm_vcpu *vcpu)
1235 if (!(vcpu->arch.efer & EFER_SVME) || !is_paging(vcpu)) {
1236 kvm_queue_exception(vcpu, UD_VECTOR);
1240 if (to_svm(vcpu)->vmcb->save.cpl) {
1241 kvm_inject_gp(vcpu, 0);
1248 static bool nested_exit_on_exception(struct vcpu_svm *svm)
1250 unsigned int nr = svm->vcpu.arch.exception.nr;
1252 return (svm->nested.ctl.intercepts[INTERCEPT_EXCEPTION] & BIT(nr));
1255 static void nested_svm_inject_exception_vmexit(struct vcpu_svm *svm)
1257 unsigned int nr = svm->vcpu.arch.exception.nr;
1258 struct vmcb *vmcb = svm->vmcb;
1260 vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
1261 vmcb->control.exit_code_hi = 0;
1263 if (svm->vcpu.arch.exception.has_error_code)
1264 vmcb->control.exit_info_1 = svm->vcpu.arch.exception.error_code;
1267 * EXITINFO2 is undefined for all exception intercepts other
1270 if (nr == PF_VECTOR) {
1271 if (svm->vcpu.arch.exception.nested_apf)
1272 vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token;
1273 else if (svm->vcpu.arch.exception.has_payload)
1274 vmcb->control.exit_info_2 = svm->vcpu.arch.exception.payload;
1276 vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
1277 } else if (nr == DB_VECTOR) {
1278 /* See inject_pending_event. */
1279 kvm_deliver_exception_payload(&svm->vcpu);
1280 if (svm->vcpu.arch.dr7 & DR7_GD) {
1281 svm->vcpu.arch.dr7 &= ~DR7_GD;
1282 kvm_update_dr7(&svm->vcpu);
1285 WARN_ON(svm->vcpu.arch.exception.has_payload);
1287 nested_svm_vmexit(svm);
1290 static inline bool nested_exit_on_init(struct vcpu_svm *svm)
1292 return vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_INIT);
1295 static int svm_check_nested_events(struct kvm_vcpu *vcpu)
1297 struct vcpu_svm *svm = to_svm(vcpu);
1298 bool block_nested_events =
1299 kvm_event_needs_reinjection(vcpu) || svm->nested.nested_run_pending;
1300 struct kvm_lapic *apic = vcpu->arch.apic;
1302 if (lapic_in_kernel(vcpu) &&
1303 test_bit(KVM_APIC_INIT, &apic->pending_events)) {
1304 if (block_nested_events)
1306 if (!nested_exit_on_init(svm))
1308 nested_svm_simple_vmexit(svm, SVM_EXIT_INIT);
1312 if (vcpu->arch.exception.pending) {
1314 * Only a pending nested run can block a pending exception.
1315 * Otherwise an injected NMI/interrupt should either be
1316 * lost or delivered to the nested hypervisor in the EXITINTINFO
1317 * vmcb field, while delivering the pending exception.
1319 if (svm->nested.nested_run_pending)
1321 if (!nested_exit_on_exception(svm))
1323 nested_svm_inject_exception_vmexit(svm);
1327 if (vcpu->arch.smi_pending && !svm_smi_blocked(vcpu)) {
1328 if (block_nested_events)
1330 if (!nested_exit_on_smi(svm))
1332 nested_svm_simple_vmexit(svm, SVM_EXIT_SMI);
1336 if (vcpu->arch.nmi_pending && !svm_nmi_blocked(vcpu)) {
1337 if (block_nested_events)
1339 if (!nested_exit_on_nmi(svm))
1341 nested_svm_simple_vmexit(svm, SVM_EXIT_NMI);
1345 if (kvm_cpu_has_interrupt(vcpu) && !svm_interrupt_blocked(vcpu)) {
1346 if (block_nested_events)
1348 if (!nested_exit_on_intr(svm))
1350 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
1351 nested_svm_simple_vmexit(svm, SVM_EXIT_INTR);
1358 int nested_svm_exit_special(struct vcpu_svm *svm)
1360 u32 exit_code = svm->vmcb->control.exit_code;
1362 switch (exit_code) {
1366 return NESTED_EXIT_HOST;
1367 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1368 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
1370 if (svm->vmcb01.ptr->control.intercepts[INTERCEPT_EXCEPTION] &
1372 return NESTED_EXIT_HOST;
1373 else if (exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR &&
1374 svm->vcpu.arch.apf.host_apf_flags)
1375 /* Trap async PF even if not shadowing */
1376 return NESTED_EXIT_HOST;
1383 return NESTED_EXIT_CONTINUE;
1386 void nested_svm_update_tsc_ratio_msr(struct kvm_vcpu *vcpu)
1388 struct vcpu_svm *svm = to_svm(vcpu);
1390 vcpu->arch.tsc_scaling_ratio =
1391 kvm_calc_nested_tsc_multiplier(vcpu->arch.l1_tsc_scaling_ratio,
1392 svm->tsc_ratio_msr);
1393 __svm_write_tsc_multiplier(vcpu->arch.tsc_scaling_ratio);
1396 /* Inverse operation of nested_copy_vmcb_control_to_cache(). asid is copied too. */
1397 static void nested_copy_vmcb_cache_to_control(struct vmcb_control_area *dst,
1398 struct vmcb_ctrl_area_cached *from)
1402 memset(dst, 0, sizeof(struct vmcb_control_area));
1404 for (i = 0; i < MAX_INTERCEPT; i++)
1405 dst->intercepts[i] = from->intercepts[i];
1407 dst->iopm_base_pa = from->iopm_base_pa;
1408 dst->msrpm_base_pa = from->msrpm_base_pa;
1409 dst->tsc_offset = from->tsc_offset;
1410 dst->asid = from->asid;
1411 dst->tlb_ctl = from->tlb_ctl;
1412 dst->int_ctl = from->int_ctl;
1413 dst->int_vector = from->int_vector;
1414 dst->int_state = from->int_state;
1415 dst->exit_code = from->exit_code;
1416 dst->exit_code_hi = from->exit_code_hi;
1417 dst->exit_info_1 = from->exit_info_1;
1418 dst->exit_info_2 = from->exit_info_2;
1419 dst->exit_int_info = from->exit_int_info;
1420 dst->exit_int_info_err = from->exit_int_info_err;
1421 dst->nested_ctl = from->nested_ctl;
1422 dst->event_inj = from->event_inj;
1423 dst->event_inj_err = from->event_inj_err;
1424 dst->nested_cr3 = from->nested_cr3;
1425 dst->virt_ext = from->virt_ext;
1426 dst->pause_filter_count = from->pause_filter_count;
1427 dst->pause_filter_thresh = from->pause_filter_thresh;
1428 /* 'clean' and 'reserved_sw' are not changed by KVM */
1431 static int svm_get_nested_state(struct kvm_vcpu *vcpu,
1432 struct kvm_nested_state __user *user_kvm_nested_state,
1435 struct vcpu_svm *svm;
1436 struct vmcb_control_area *ctl;
1438 struct kvm_nested_state kvm_state = {
1440 .format = KVM_STATE_NESTED_FORMAT_SVM,
1441 .size = sizeof(kvm_state),
1443 struct vmcb __user *user_vmcb = (struct vmcb __user *)
1444 &user_kvm_nested_state->data.svm[0];
1447 return kvm_state.size + KVM_STATE_NESTED_SVM_VMCB_SIZE;
1451 if (user_data_size < kvm_state.size)
1454 /* First fill in the header and copy it out. */
1455 if (is_guest_mode(vcpu)) {
1456 kvm_state.hdr.svm.vmcb_pa = svm->nested.vmcb12_gpa;
1457 kvm_state.size += KVM_STATE_NESTED_SVM_VMCB_SIZE;
1458 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
1460 if (svm->nested.nested_run_pending)
1461 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
1465 kvm_state.flags |= KVM_STATE_NESTED_GIF_SET;
1467 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
1470 if (!is_guest_mode(vcpu))
1474 * Copy over the full size of the VMCB rather than just the size
1477 if (clear_user(user_vmcb, KVM_STATE_NESTED_SVM_VMCB_SIZE))
1480 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
1484 nested_copy_vmcb_cache_to_control(ctl, &svm->nested.ctl);
1485 r = copy_to_user(&user_vmcb->control, ctl,
1486 sizeof(user_vmcb->control));
1491 if (copy_to_user(&user_vmcb->save, &svm->vmcb01.ptr->save,
1492 sizeof(user_vmcb->save)))
1495 return kvm_state.size;
1498 static int svm_set_nested_state(struct kvm_vcpu *vcpu,
1499 struct kvm_nested_state __user *user_kvm_nested_state,
1500 struct kvm_nested_state *kvm_state)
1502 struct vcpu_svm *svm = to_svm(vcpu);
1503 struct vmcb __user *user_vmcb = (struct vmcb __user *)
1504 &user_kvm_nested_state->data.svm[0];
1505 struct vmcb_control_area *ctl;
1506 struct vmcb_save_area *save;
1507 struct vmcb_save_area_cached save_cached;
1508 struct vmcb_ctrl_area_cached ctl_cached;
1512 BUILD_BUG_ON(sizeof(struct vmcb_control_area) + sizeof(struct vmcb_save_area) >
1513 KVM_STATE_NESTED_SVM_VMCB_SIZE);
1515 if (kvm_state->format != KVM_STATE_NESTED_FORMAT_SVM)
1518 if (kvm_state->flags & ~(KVM_STATE_NESTED_GUEST_MODE |
1519 KVM_STATE_NESTED_RUN_PENDING |
1520 KVM_STATE_NESTED_GIF_SET))
1524 * If in guest mode, vcpu->arch.efer actually refers to the L2 guest's
1525 * EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed.
1527 if (!(vcpu->arch.efer & EFER_SVME)) {
1528 /* GIF=1 and no guest mode are required if SVME=0. */
1529 if (kvm_state->flags != KVM_STATE_NESTED_GIF_SET)
1533 /* SMM temporarily disables SVM, so we cannot be in guest mode. */
1534 if (is_smm(vcpu) && (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
1537 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) {
1538 svm_leave_nested(vcpu);
1539 svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));
1543 if (!page_address_valid(vcpu, kvm_state->hdr.svm.vmcb_pa))
1545 if (kvm_state->size < sizeof(*kvm_state) + KVM_STATE_NESTED_SVM_VMCB_SIZE)
1549 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL_ACCOUNT);
1550 save = kzalloc(sizeof(*save), GFP_KERNEL_ACCOUNT);
1555 if (copy_from_user(ctl, &user_vmcb->control, sizeof(*ctl)))
1557 if (copy_from_user(save, &user_vmcb->save, sizeof(*save)))
1561 __nested_copy_vmcb_control_to_cache(vcpu, &ctl_cached, ctl);
1562 if (!__nested_vmcb_check_controls(vcpu, &ctl_cached))
1566 * Processor state contains L2 state. Check that it is
1567 * valid for guest mode (see nested_vmcb_check_save).
1569 cr0 = kvm_read_cr0(vcpu);
1570 if (((cr0 & X86_CR0_CD) == 0) && (cr0 & X86_CR0_NW))
1574 * Validate host state saved from before VMRUN (see
1575 * nested_svm_check_permissions).
1577 __nested_copy_vmcb_save_to_cache(&save_cached, save);
1578 if (!(save->cr0 & X86_CR0_PG) ||
1579 !(save->cr0 & X86_CR0_PE) ||
1580 (save->rflags & X86_EFLAGS_VM) ||
1581 !__nested_vmcb_check_save(vcpu, &save_cached))
1586 * All checks done, we can enter guest mode. Userspace provides
1587 * vmcb12.control, which will be combined with L1 and stored into
1588 * vmcb02, and the L1 save state which we store in vmcb01.
1589 * L2 registers if needed are moved from the current VMCB to VMCB02.
1592 if (is_guest_mode(vcpu))
1593 svm_leave_nested(vcpu);
1595 svm->nested.vmcb02.ptr->save = svm->vmcb01.ptr->save;
1597 svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));
1599 svm->nested.nested_run_pending =
1600 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
1602 svm->nested.vmcb12_gpa = kvm_state->hdr.svm.vmcb_pa;
1604 svm_copy_vmrun_state(&svm->vmcb01.ptr->save, save);
1605 nested_copy_vmcb_control_to_cache(svm, ctl);
1607 svm_switch_vmcb(svm, &svm->nested.vmcb02);
1608 nested_vmcb02_prepare_control(svm);
1611 * While the nested guest CR3 is already checked and set by
1612 * KVM_SET_SREGS, it was set when nested state was yet loaded,
1613 * thus MMU might not be initialized correctly.
1614 * Set it again to fix this.
1617 ret = nested_svm_load_cr3(&svm->vcpu, vcpu->arch.cr3,
1618 nested_npt_enabled(svm), false);
1619 if (WARN_ON_ONCE(ret))
1622 svm->nested.force_msr_bitmap_recalc = true;
1624 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
1633 static bool svm_get_nested_state_pages(struct kvm_vcpu *vcpu)
1635 struct vcpu_svm *svm = to_svm(vcpu);
1637 if (WARN_ON(!is_guest_mode(vcpu)))
1640 if (!vcpu->arch.pdptrs_from_userspace &&
1641 !nested_npt_enabled(svm) && is_pae_paging(vcpu))
1643 * Reload the guest's PDPTRs since after a migration
1644 * the guest CR3 might be restored prior to setting the nested
1645 * state which can lead to a load of wrong PDPTRs.
1647 if (CC(!load_pdptrs(vcpu, vcpu->arch.cr3)))
1650 if (!nested_svm_vmrun_msrpm(svm)) {
1651 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1652 vcpu->run->internal.suberror =
1653 KVM_INTERNAL_ERROR_EMULATION;
1654 vcpu->run->internal.ndata = 0;
1661 struct kvm_x86_nested_ops svm_nested_ops = {
1662 .leave_nested = svm_leave_nested,
1663 .check_events = svm_check_nested_events,
1664 .handle_page_fault_workaround = nested_svm_handle_page_fault_workaround,
1665 .triple_fault = nested_svm_triple_fault,
1666 .get_nested_state_pages = svm_get_nested_state_pages,
1667 .get_state = svm_get_nested_state,
1668 .set_state = svm_set_nested_state,