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;
621 * Filled at exit: exit_code, exit_code_hi, exit_info_1, exit_info_2,
622 * exit_int_info, exit_int_info_err, next_rip, insn_len, insn_bytes.
625 if (svm->vgif_enabled && (svm->nested.ctl.int_ctl & V_GIF_ENABLE_MASK))
626 int_ctl_vmcb12_bits |= (V_GIF_MASK | V_GIF_ENABLE_MASK);
628 int_ctl_vmcb01_bits |= (V_GIF_MASK | V_GIF_ENABLE_MASK);
630 /* Copied from vmcb01. msrpm_base can be overwritten later. */
631 vmcb02->control.nested_ctl = vmcb01->control.nested_ctl;
632 vmcb02->control.iopm_base_pa = vmcb01->control.iopm_base_pa;
633 vmcb02->control.msrpm_base_pa = vmcb01->control.msrpm_base_pa;
635 /* Done at vmrun: asid. */
637 /* Also overwritten later if necessary. */
638 vmcb02->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
641 if (nested_npt_enabled(svm))
642 nested_svm_init_mmu_context(vcpu);
644 vcpu->arch.tsc_offset = kvm_calc_nested_tsc_offset(
645 vcpu->arch.l1_tsc_offset,
646 svm->nested.ctl.tsc_offset,
649 vmcb02->control.tsc_offset = vcpu->arch.tsc_offset;
651 if (svm->tsc_ratio_msr != kvm_default_tsc_scaling_ratio) {
652 WARN_ON(!svm->tsc_scaling_enabled);
653 nested_svm_update_tsc_ratio_msr(vcpu);
656 vmcb02->control.int_ctl =
657 (svm->nested.ctl.int_ctl & int_ctl_vmcb12_bits) |
658 (vmcb01->control.int_ctl & int_ctl_vmcb01_bits);
660 vmcb02->control.int_vector = svm->nested.ctl.int_vector;
661 vmcb02->control.int_state = svm->nested.ctl.int_state;
662 vmcb02->control.event_inj = svm->nested.ctl.event_inj;
663 vmcb02->control.event_inj_err = svm->nested.ctl.event_inj_err;
665 vmcb02->control.virt_ext = vmcb01->control.virt_ext &
667 if (svm->lbrv_enabled)
668 vmcb02->control.virt_ext |=
669 (svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK);
671 if (!nested_vmcb_needs_vls_intercept(svm))
672 vmcb02->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
674 if (kvm_pause_in_guest(svm->vcpu.kvm)) {
675 /* use guest values since host doesn't use them */
676 vmcb02->control.pause_filter_count =
677 svm->pause_filter_enabled ?
678 svm->nested.ctl.pause_filter_count : 0;
680 vmcb02->control.pause_filter_thresh =
681 svm->pause_threshold_enabled ?
682 svm->nested.ctl.pause_filter_thresh : 0;
684 } else if (!vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_PAUSE)) {
685 /* use host values when guest doesn't use them */
686 vmcb02->control.pause_filter_count = vmcb01->control.pause_filter_count;
687 vmcb02->control.pause_filter_thresh = vmcb01->control.pause_filter_thresh;
690 * Intercept every PAUSE otherwise and
691 * ignore both host and guest values
693 vmcb02->control.pause_filter_count = 0;
694 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) && vmcb02->control.pause_filter_count)
955 vmcb01->control.pause_filter_count = vmcb02->control.pause_filter_count;
957 nested_svm_copy_common_state(svm->nested.vmcb02.ptr, svm->vmcb01.ptr);
959 svm_switch_vmcb(svm, &svm->vmcb01);
961 if (unlikely(svm->lbrv_enabled && (svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK))) {
962 svm_copy_lbrs(vmcb12, vmcb02);
963 svm_update_lbrv(vcpu);
964 } else if (unlikely(vmcb01->control.virt_ext & LBR_CTL_ENABLE_MASK)) {
965 svm_copy_lbrs(vmcb01, vmcb02);
966 svm_update_lbrv(vcpu);
970 * On vmexit the GIF is set to false and
971 * no event can be injected in L1.
973 svm_set_gif(svm, false);
974 vmcb01->control.exit_int_info = 0;
976 svm->vcpu.arch.tsc_offset = svm->vcpu.arch.l1_tsc_offset;
977 if (vmcb01->control.tsc_offset != svm->vcpu.arch.tsc_offset) {
978 vmcb01->control.tsc_offset = svm->vcpu.arch.tsc_offset;
979 vmcb_mark_dirty(vmcb01, VMCB_INTERCEPTS);
982 if (svm->tsc_ratio_msr != kvm_default_tsc_scaling_ratio) {
983 WARN_ON(!svm->tsc_scaling_enabled);
984 vcpu->arch.tsc_scaling_ratio = vcpu->arch.l1_tsc_scaling_ratio;
985 svm_write_tsc_multiplier(vcpu, vcpu->arch.tsc_scaling_ratio);
988 svm->nested.ctl.nested_cr3 = 0;
991 * Restore processor state that had been saved in vmcb01
993 kvm_set_rflags(vcpu, vmcb01->save.rflags);
994 svm_set_efer(vcpu, vmcb01->save.efer);
995 svm_set_cr0(vcpu, vmcb01->save.cr0 | X86_CR0_PE);
996 svm_set_cr4(vcpu, vmcb01->save.cr4);
997 kvm_rax_write(vcpu, vmcb01->save.rax);
998 kvm_rsp_write(vcpu, vmcb01->save.rsp);
999 kvm_rip_write(vcpu, vmcb01->save.rip);
1001 svm->vcpu.arch.dr7 = DR7_FIXED_1;
1002 kvm_update_dr7(&svm->vcpu);
1004 trace_kvm_nested_vmexit_inject(vmcb12->control.exit_code,
1005 vmcb12->control.exit_info_1,
1006 vmcb12->control.exit_info_2,
1007 vmcb12->control.exit_int_info,
1008 vmcb12->control.exit_int_info_err,
1011 kvm_vcpu_unmap(vcpu, &map, true);
1013 nested_svm_transition_tlb_flush(vcpu);
1015 nested_svm_uninit_mmu_context(vcpu);
1017 rc = nested_svm_load_cr3(vcpu, vmcb01->save.cr3, false, true);
1022 * Drop what we picked up for L2 via svm_complete_interrupts() so it
1023 * doesn't end up in L1.
1025 svm->vcpu.arch.nmi_injected = false;
1026 kvm_clear_exception_queue(vcpu);
1027 kvm_clear_interrupt_queue(vcpu);
1030 * If we are here following the completion of a VMRUN that
1031 * is being single-stepped, queue the pending #DB intercept
1032 * right now so that it an be accounted for before we execute
1033 * L1's next instruction.
1035 if (unlikely(vmcb01->save.rflags & X86_EFLAGS_TF))
1036 kvm_queue_exception(&(svm->vcpu), DB_VECTOR);
1039 * Un-inhibit the AVIC right away, so that other vCPUs can start
1040 * to benefit from it right away.
1042 if (kvm_apicv_activated(vcpu->kvm))
1043 kvm_vcpu_update_apicv(vcpu);
1048 static void nested_svm_triple_fault(struct kvm_vcpu *vcpu)
1050 nested_svm_simple_vmexit(to_svm(vcpu), SVM_EXIT_SHUTDOWN);
1053 int svm_allocate_nested(struct vcpu_svm *svm)
1055 struct page *vmcb02_page;
1057 if (svm->nested.initialized)
1060 vmcb02_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
1063 svm->nested.vmcb02.ptr = page_address(vmcb02_page);
1064 svm->nested.vmcb02.pa = __sme_set(page_to_pfn(vmcb02_page) << PAGE_SHIFT);
1066 svm->nested.msrpm = svm_vcpu_alloc_msrpm();
1067 if (!svm->nested.msrpm)
1068 goto err_free_vmcb02;
1069 svm_vcpu_init_msrpm(&svm->vcpu, svm->nested.msrpm);
1071 svm->nested.initialized = true;
1075 __free_page(vmcb02_page);
1079 void svm_free_nested(struct vcpu_svm *svm)
1081 if (!svm->nested.initialized)
1084 svm_vcpu_free_msrpm(svm->nested.msrpm);
1085 svm->nested.msrpm = NULL;
1087 __free_page(virt_to_page(svm->nested.vmcb02.ptr));
1088 svm->nested.vmcb02.ptr = NULL;
1091 * When last_vmcb12_gpa matches the current vmcb12 gpa,
1092 * some vmcb12 fields are not loaded if they are marked clean
1093 * in the vmcb12, since in this case they are up to date already.
1095 * When the vmcb02 is freed, this optimization becomes invalid.
1097 svm->nested.last_vmcb12_gpa = INVALID_GPA;
1099 svm->nested.initialized = false;
1103 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
1105 void svm_leave_nested(struct kvm_vcpu *vcpu)
1107 struct vcpu_svm *svm = to_svm(vcpu);
1109 if (is_guest_mode(vcpu)) {
1110 svm->nested.nested_run_pending = 0;
1111 svm->nested.vmcb12_gpa = INVALID_GPA;
1113 leave_guest_mode(vcpu);
1115 svm_switch_vmcb(svm, &svm->vmcb01);
1117 nested_svm_uninit_mmu_context(vcpu);
1118 vmcb_mark_all_dirty(svm->vmcb);
1121 kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
1124 static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
1126 u32 offset, msr, value;
1129 if (!(vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
1130 return NESTED_EXIT_HOST;
1132 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
1133 offset = svm_msrpm_offset(msr);
1134 write = svm->vmcb->control.exit_info_1 & 1;
1135 mask = 1 << ((2 * (msr & 0xf)) + write);
1137 if (offset == MSR_INVALID)
1138 return NESTED_EXIT_DONE;
1140 /* Offset is in 32 bit units but need in 8 bit units */
1143 if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.ctl.msrpm_base_pa + offset, &value, 4))
1144 return NESTED_EXIT_DONE;
1146 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
1149 static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
1151 unsigned port, size, iopm_len;
1156 if (!(vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_IOIO_PROT)))
1157 return NESTED_EXIT_HOST;
1159 port = svm->vmcb->control.exit_info_1 >> 16;
1160 size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
1161 SVM_IOIO_SIZE_SHIFT;
1162 gpa = svm->nested.ctl.iopm_base_pa + (port / 8);
1163 start_bit = port % 8;
1164 iopm_len = (start_bit + size > 8) ? 2 : 1;
1165 mask = (0xf >> (4 - size)) << start_bit;
1168 if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
1169 return NESTED_EXIT_DONE;
1171 return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
1174 static int nested_svm_intercept(struct vcpu_svm *svm)
1176 u32 exit_code = svm->vmcb->control.exit_code;
1177 int vmexit = NESTED_EXIT_HOST;
1179 switch (exit_code) {
1181 vmexit = nested_svm_exit_handled_msr(svm);
1184 vmexit = nested_svm_intercept_ioio(svm);
1186 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
1187 if (vmcb12_is_intercept(&svm->nested.ctl, exit_code))
1188 vmexit = NESTED_EXIT_DONE;
1191 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
1192 if (vmcb12_is_intercept(&svm->nested.ctl, exit_code))
1193 vmexit = NESTED_EXIT_DONE;
1196 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1198 * Host-intercepted exceptions have been checked already in
1199 * nested_svm_exit_special. There is nothing to do here,
1200 * the vmexit is injected by svm_check_nested_events.
1202 vmexit = NESTED_EXIT_DONE;
1205 case SVM_EXIT_ERR: {
1206 vmexit = NESTED_EXIT_DONE;
1210 if (vmcb12_is_intercept(&svm->nested.ctl, exit_code))
1211 vmexit = NESTED_EXIT_DONE;
1218 int nested_svm_exit_handled(struct vcpu_svm *svm)
1222 vmexit = nested_svm_intercept(svm);
1224 if (vmexit == NESTED_EXIT_DONE)
1225 nested_svm_vmexit(svm);
1230 int nested_svm_check_permissions(struct kvm_vcpu *vcpu)
1232 if (!(vcpu->arch.efer & EFER_SVME) || !is_paging(vcpu)) {
1233 kvm_queue_exception(vcpu, UD_VECTOR);
1237 if (to_svm(vcpu)->vmcb->save.cpl) {
1238 kvm_inject_gp(vcpu, 0);
1245 static bool nested_exit_on_exception(struct vcpu_svm *svm)
1247 unsigned int nr = svm->vcpu.arch.exception.nr;
1249 return (svm->nested.ctl.intercepts[INTERCEPT_EXCEPTION] & BIT(nr));
1252 static void nested_svm_inject_exception_vmexit(struct vcpu_svm *svm)
1254 unsigned int nr = svm->vcpu.arch.exception.nr;
1255 struct vmcb *vmcb = svm->vmcb;
1257 vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
1258 vmcb->control.exit_code_hi = 0;
1260 if (svm->vcpu.arch.exception.has_error_code)
1261 vmcb->control.exit_info_1 = svm->vcpu.arch.exception.error_code;
1264 * EXITINFO2 is undefined for all exception intercepts other
1267 if (nr == PF_VECTOR) {
1268 if (svm->vcpu.arch.exception.nested_apf)
1269 vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token;
1270 else if (svm->vcpu.arch.exception.has_payload)
1271 vmcb->control.exit_info_2 = svm->vcpu.arch.exception.payload;
1273 vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
1274 } else if (nr == DB_VECTOR) {
1275 /* See inject_pending_event. */
1276 kvm_deliver_exception_payload(&svm->vcpu);
1277 if (svm->vcpu.arch.dr7 & DR7_GD) {
1278 svm->vcpu.arch.dr7 &= ~DR7_GD;
1279 kvm_update_dr7(&svm->vcpu);
1282 WARN_ON(svm->vcpu.arch.exception.has_payload);
1284 nested_svm_vmexit(svm);
1287 static inline bool nested_exit_on_init(struct vcpu_svm *svm)
1289 return vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_INIT);
1292 static int svm_check_nested_events(struct kvm_vcpu *vcpu)
1294 struct vcpu_svm *svm = to_svm(vcpu);
1295 bool block_nested_events =
1296 kvm_event_needs_reinjection(vcpu) || svm->nested.nested_run_pending;
1297 struct kvm_lapic *apic = vcpu->arch.apic;
1299 if (lapic_in_kernel(vcpu) &&
1300 test_bit(KVM_APIC_INIT, &apic->pending_events)) {
1301 if (block_nested_events)
1303 if (!nested_exit_on_init(svm))
1305 nested_svm_simple_vmexit(svm, SVM_EXIT_INIT);
1309 if (vcpu->arch.exception.pending) {
1311 * Only a pending nested run can block a pending exception.
1312 * Otherwise an injected NMI/interrupt should either be
1313 * lost or delivered to the nested hypervisor in the EXITINTINFO
1314 * vmcb field, while delivering the pending exception.
1316 if (svm->nested.nested_run_pending)
1318 if (!nested_exit_on_exception(svm))
1320 nested_svm_inject_exception_vmexit(svm);
1324 if (vcpu->arch.smi_pending && !svm_smi_blocked(vcpu)) {
1325 if (block_nested_events)
1327 if (!nested_exit_on_smi(svm))
1329 nested_svm_simple_vmexit(svm, SVM_EXIT_SMI);
1333 if (vcpu->arch.nmi_pending && !svm_nmi_blocked(vcpu)) {
1334 if (block_nested_events)
1336 if (!nested_exit_on_nmi(svm))
1338 nested_svm_simple_vmexit(svm, SVM_EXIT_NMI);
1342 if (kvm_cpu_has_interrupt(vcpu) && !svm_interrupt_blocked(vcpu)) {
1343 if (block_nested_events)
1345 if (!nested_exit_on_intr(svm))
1347 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
1348 nested_svm_simple_vmexit(svm, SVM_EXIT_INTR);
1355 int nested_svm_exit_special(struct vcpu_svm *svm)
1357 u32 exit_code = svm->vmcb->control.exit_code;
1359 switch (exit_code) {
1363 return NESTED_EXIT_HOST;
1364 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1365 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
1367 if (svm->vmcb01.ptr->control.intercepts[INTERCEPT_EXCEPTION] &
1369 return NESTED_EXIT_HOST;
1370 else if (exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR &&
1371 svm->vcpu.arch.apf.host_apf_flags)
1372 /* Trap async PF even if not shadowing */
1373 return NESTED_EXIT_HOST;
1380 return NESTED_EXIT_CONTINUE;
1383 void nested_svm_update_tsc_ratio_msr(struct kvm_vcpu *vcpu)
1385 struct vcpu_svm *svm = to_svm(vcpu);
1387 vcpu->arch.tsc_scaling_ratio =
1388 kvm_calc_nested_tsc_multiplier(vcpu->arch.l1_tsc_scaling_ratio,
1389 svm->tsc_ratio_msr);
1390 svm_write_tsc_multiplier(vcpu, vcpu->arch.tsc_scaling_ratio);
1393 /* Inverse operation of nested_copy_vmcb_control_to_cache(). asid is copied too. */
1394 static void nested_copy_vmcb_cache_to_control(struct vmcb_control_area *dst,
1395 struct vmcb_ctrl_area_cached *from)
1399 memset(dst, 0, sizeof(struct vmcb_control_area));
1401 for (i = 0; i < MAX_INTERCEPT; i++)
1402 dst->intercepts[i] = from->intercepts[i];
1404 dst->iopm_base_pa = from->iopm_base_pa;
1405 dst->msrpm_base_pa = from->msrpm_base_pa;
1406 dst->tsc_offset = from->tsc_offset;
1407 dst->asid = from->asid;
1408 dst->tlb_ctl = from->tlb_ctl;
1409 dst->int_ctl = from->int_ctl;
1410 dst->int_vector = from->int_vector;
1411 dst->int_state = from->int_state;
1412 dst->exit_code = from->exit_code;
1413 dst->exit_code_hi = from->exit_code_hi;
1414 dst->exit_info_1 = from->exit_info_1;
1415 dst->exit_info_2 = from->exit_info_2;
1416 dst->exit_int_info = from->exit_int_info;
1417 dst->exit_int_info_err = from->exit_int_info_err;
1418 dst->nested_ctl = from->nested_ctl;
1419 dst->event_inj = from->event_inj;
1420 dst->event_inj_err = from->event_inj_err;
1421 dst->nested_cr3 = from->nested_cr3;
1422 dst->virt_ext = from->virt_ext;
1423 dst->pause_filter_count = from->pause_filter_count;
1424 dst->pause_filter_thresh = from->pause_filter_thresh;
1425 /* 'clean' and 'reserved_sw' are not changed by KVM */
1428 static int svm_get_nested_state(struct kvm_vcpu *vcpu,
1429 struct kvm_nested_state __user *user_kvm_nested_state,
1432 struct vcpu_svm *svm;
1433 struct vmcb_control_area *ctl;
1435 struct kvm_nested_state kvm_state = {
1437 .format = KVM_STATE_NESTED_FORMAT_SVM,
1438 .size = sizeof(kvm_state),
1440 struct vmcb __user *user_vmcb = (struct vmcb __user *)
1441 &user_kvm_nested_state->data.svm[0];
1444 return kvm_state.size + KVM_STATE_NESTED_SVM_VMCB_SIZE;
1448 if (user_data_size < kvm_state.size)
1451 /* First fill in the header and copy it out. */
1452 if (is_guest_mode(vcpu)) {
1453 kvm_state.hdr.svm.vmcb_pa = svm->nested.vmcb12_gpa;
1454 kvm_state.size += KVM_STATE_NESTED_SVM_VMCB_SIZE;
1455 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
1457 if (svm->nested.nested_run_pending)
1458 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
1462 kvm_state.flags |= KVM_STATE_NESTED_GIF_SET;
1464 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
1467 if (!is_guest_mode(vcpu))
1471 * Copy over the full size of the VMCB rather than just the size
1474 if (clear_user(user_vmcb, KVM_STATE_NESTED_SVM_VMCB_SIZE))
1477 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
1481 nested_copy_vmcb_cache_to_control(ctl, &svm->nested.ctl);
1482 r = copy_to_user(&user_vmcb->control, ctl,
1483 sizeof(user_vmcb->control));
1488 if (copy_to_user(&user_vmcb->save, &svm->vmcb01.ptr->save,
1489 sizeof(user_vmcb->save)))
1492 return kvm_state.size;
1495 static int svm_set_nested_state(struct kvm_vcpu *vcpu,
1496 struct kvm_nested_state __user *user_kvm_nested_state,
1497 struct kvm_nested_state *kvm_state)
1499 struct vcpu_svm *svm = to_svm(vcpu);
1500 struct vmcb __user *user_vmcb = (struct vmcb __user *)
1501 &user_kvm_nested_state->data.svm[0];
1502 struct vmcb_control_area *ctl;
1503 struct vmcb_save_area *save;
1504 struct vmcb_save_area_cached save_cached;
1505 struct vmcb_ctrl_area_cached ctl_cached;
1509 BUILD_BUG_ON(sizeof(struct vmcb_control_area) + sizeof(struct vmcb_save_area) >
1510 KVM_STATE_NESTED_SVM_VMCB_SIZE);
1512 if (kvm_state->format != KVM_STATE_NESTED_FORMAT_SVM)
1515 if (kvm_state->flags & ~(KVM_STATE_NESTED_GUEST_MODE |
1516 KVM_STATE_NESTED_RUN_PENDING |
1517 KVM_STATE_NESTED_GIF_SET))
1521 * If in guest mode, vcpu->arch.efer actually refers to the L2 guest's
1522 * EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed.
1524 if (!(vcpu->arch.efer & EFER_SVME)) {
1525 /* GIF=1 and no guest mode are required if SVME=0. */
1526 if (kvm_state->flags != KVM_STATE_NESTED_GIF_SET)
1530 /* SMM temporarily disables SVM, so we cannot be in guest mode. */
1531 if (is_smm(vcpu) && (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
1534 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) {
1535 svm_leave_nested(vcpu);
1536 svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));
1540 if (!page_address_valid(vcpu, kvm_state->hdr.svm.vmcb_pa))
1542 if (kvm_state->size < sizeof(*kvm_state) + KVM_STATE_NESTED_SVM_VMCB_SIZE)
1546 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL_ACCOUNT);
1547 save = kzalloc(sizeof(*save), GFP_KERNEL_ACCOUNT);
1552 if (copy_from_user(ctl, &user_vmcb->control, sizeof(*ctl)))
1554 if (copy_from_user(save, &user_vmcb->save, sizeof(*save)))
1558 __nested_copy_vmcb_control_to_cache(vcpu, &ctl_cached, ctl);
1559 if (!__nested_vmcb_check_controls(vcpu, &ctl_cached))
1563 * Processor state contains L2 state. Check that it is
1564 * valid for guest mode (see nested_vmcb_check_save).
1566 cr0 = kvm_read_cr0(vcpu);
1567 if (((cr0 & X86_CR0_CD) == 0) && (cr0 & X86_CR0_NW))
1571 * Validate host state saved from before VMRUN (see
1572 * nested_svm_check_permissions).
1574 __nested_copy_vmcb_save_to_cache(&save_cached, save);
1575 if (!(save->cr0 & X86_CR0_PG) ||
1576 !(save->cr0 & X86_CR0_PE) ||
1577 (save->rflags & X86_EFLAGS_VM) ||
1578 !__nested_vmcb_check_save(vcpu, &save_cached))
1583 * All checks done, we can enter guest mode. Userspace provides
1584 * vmcb12.control, which will be combined with L1 and stored into
1585 * vmcb02, and the L1 save state which we store in vmcb01.
1586 * L2 registers if needed are moved from the current VMCB to VMCB02.
1589 if (is_guest_mode(vcpu))
1590 svm_leave_nested(vcpu);
1592 svm->nested.vmcb02.ptr->save = svm->vmcb01.ptr->save;
1594 svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));
1596 svm->nested.nested_run_pending =
1597 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
1599 svm->nested.vmcb12_gpa = kvm_state->hdr.svm.vmcb_pa;
1601 svm_copy_vmrun_state(&svm->vmcb01.ptr->save, save);
1602 nested_copy_vmcb_control_to_cache(svm, ctl);
1604 svm_switch_vmcb(svm, &svm->nested.vmcb02);
1605 nested_vmcb02_prepare_control(svm);
1608 * While the nested guest CR3 is already checked and set by
1609 * KVM_SET_SREGS, it was set when nested state was yet loaded,
1610 * thus MMU might not be initialized correctly.
1611 * Set it again to fix this.
1614 ret = nested_svm_load_cr3(&svm->vcpu, vcpu->arch.cr3,
1615 nested_npt_enabled(svm), false);
1616 if (WARN_ON_ONCE(ret))
1619 svm->nested.force_msr_bitmap_recalc = true;
1621 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
1630 static bool svm_get_nested_state_pages(struct kvm_vcpu *vcpu)
1632 struct vcpu_svm *svm = to_svm(vcpu);
1634 if (WARN_ON(!is_guest_mode(vcpu)))
1637 if (!vcpu->arch.pdptrs_from_userspace &&
1638 !nested_npt_enabled(svm) && is_pae_paging(vcpu))
1640 * Reload the guest's PDPTRs since after a migration
1641 * the guest CR3 might be restored prior to setting the nested
1642 * state which can lead to a load of wrong PDPTRs.
1644 if (CC(!load_pdptrs(vcpu, vcpu->arch.cr3)))
1647 if (!nested_svm_vmrun_msrpm(svm)) {
1648 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1649 vcpu->run->internal.suberror =
1650 KVM_INTERNAL_ERROR_EMULATION;
1651 vcpu->run->internal.ndata = 0;
1658 struct kvm_x86_nested_ops svm_nested_ops = {
1659 .leave_nested = svm_leave_nested,
1660 .check_events = svm_check_nested_events,
1661 .handle_page_fault_workaround = nested_svm_handle_page_fault_workaround,
1662 .triple_fault = nested_svm_triple_fault,
1663 .get_nested_state_pages = svm_get_nested_state_pages,
1664 .get_state = svm_get_nested_state,
1665 .set_state = svm_set_nested_state,