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 u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
60 struct vcpu_svm *svm = to_svm(vcpu);
61 u64 cr3 = svm->nested.ctl.nested_cr3;
65 ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(cr3), &pdpte,
66 offset_in_page(cr3) + index * 8, 8);
72 static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
74 struct vcpu_svm *svm = to_svm(vcpu);
76 return svm->nested.ctl.nested_cr3;
79 static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
81 struct vcpu_svm *svm = to_svm(vcpu);
83 WARN_ON(mmu_is_nested(vcpu));
85 vcpu->arch.mmu = &vcpu->arch.guest_mmu;
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.
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;
101 static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
103 vcpu->arch.mmu = &vcpu->arch.root_mmu;
104 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
107 static bool nested_vmcb_needs_vls_intercept(struct vcpu_svm *svm)
109 if (!svm->v_vmload_vmsave_enabled)
112 if (!nested_npt_enabled(svm))
115 if (!(svm->nested.ctl.virt_ext & VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK))
121 void recalc_intercepts(struct vcpu_svm *svm)
123 struct vmcb_control_area *c, *h;
124 struct vmcb_ctrl_area_cached *g;
127 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
129 if (!is_guest_mode(&svm->vcpu))
132 c = &svm->vmcb->control;
133 h = &svm->vmcb01.ptr->control;
134 g = &svm->nested.ctl;
136 for (i = 0; i < MAX_INTERCEPT; i++)
137 c->intercepts[i] = h->intercepts[i];
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);
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.
149 vmcb_clr_intercept(c, INTERCEPT_VINTR);
152 /* We don't want to see VMMCALLs from a nested guest */
153 vmcb_clr_intercept(c, INTERCEPT_VMMCALL);
155 for (i = 0; i < MAX_INTERCEPT; i++)
156 c->intercepts[i] |= g->intercepts[i];
158 /* If SMI is not intercepted, ignore guest SMI intercept as well */
160 vmcb_clr_intercept(c, INTERCEPT_SMI);
162 if (nested_vmcb_needs_vls_intercept(svm)) {
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.
168 vmcb_set_intercept(c, INTERCEPT_VMLOAD);
169 vmcb_set_intercept(c, INTERCEPT_VMSAVE);
171 WARN_ON(!(c->virt_ext & VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK));
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.
180 static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
182 struct hv_vmcb_enlightenments *hve = &svm->nested.ctl.hv_enlightenments;
186 * MSR bitmap update can be skipped when:
187 * - MSR bitmap for L1 hasn't changed.
188 * - Nested hypervisor (L1) is attempting to launch the same L2 as
190 * - Nested hypervisor (L1) is using Hyper-V emulation interface and
191 * tells KVM (L0) there were no changes in MSR bitmap for L2.
193 if (!svm->nested.force_msr_bitmap_recalc &&
194 kvm_hv_hypercall_enabled(&svm->vcpu) &&
195 hve->hv_enlightenments_control.msr_bitmap &&
196 (svm->nested.ctl.clean & BIT(HV_VMCB_NESTED_ENLIGHTENMENTS)))
197 goto set_msrpm_base_pa;
199 if (!(vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
202 for (i = 0; i < MSRPM_OFFSETS; i++) {
206 if (msrpm_offsets[i] == 0xffffffff)
209 p = msrpm_offsets[i];
211 /* x2apic msrs are intercepted always for the nested guest */
212 if (is_x2apic_msrpm_offset(p))
215 offset = svm->nested.ctl.msrpm_base_pa + (p * 4);
217 if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
220 svm->nested.msrpm[p] = svm->msrpm[p] | value;
223 svm->nested.force_msr_bitmap_recalc = false;
226 svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm));
232 * Bits 11:0 of bitmap address are ignored by hardware
234 static bool nested_svm_check_bitmap_pa(struct kvm_vcpu *vcpu, u64 pa, u32 size)
236 u64 addr = PAGE_ALIGN(pa);
238 return kvm_vcpu_is_legal_gpa(vcpu, addr) &&
239 kvm_vcpu_is_legal_gpa(vcpu, addr + size - 1);
242 static bool __nested_vmcb_check_controls(struct kvm_vcpu *vcpu,
243 struct vmcb_ctrl_area_cached *control)
245 if (CC(!vmcb12_is_intercept(control, INTERCEPT_VMRUN)))
248 if (CC(control->asid == 0))
251 if (CC((control->nested_ctl & SVM_NESTED_CTL_NP_ENABLE) && !npt_enabled))
254 if (CC(!nested_svm_check_bitmap_pa(vcpu, control->msrpm_base_pa,
257 if (CC(!nested_svm_check_bitmap_pa(vcpu, control->iopm_base_pa,
265 /* Common checks that apply to both L1 and L2 state. */
266 static bool __nested_vmcb_check_save(struct kvm_vcpu *vcpu,
267 struct vmcb_save_area_cached *save)
269 if (CC(!(save->efer & EFER_SVME)))
272 if (CC((save->cr0 & X86_CR0_CD) == 0 && (save->cr0 & X86_CR0_NW)) ||
273 CC(save->cr0 & ~0xffffffffULL))
276 if (CC(!kvm_dr6_valid(save->dr6)) || CC(!kvm_dr7_valid(save->dr7)))
280 * These checks are also performed by KVM_SET_SREGS,
281 * except that EFER.LMA is not checked by SVM against
282 * CR0.PG && EFER.LME.
284 if ((save->efer & EFER_LME) && (save->cr0 & X86_CR0_PG)) {
285 if (CC(!(save->cr4 & X86_CR4_PAE)) ||
286 CC(!(save->cr0 & X86_CR0_PE)) ||
287 CC(kvm_vcpu_is_illegal_gpa(vcpu, save->cr3)))
291 /* Note, SVM doesn't have any additional restrictions on CR4. */
292 if (CC(!__kvm_is_valid_cr4(vcpu, save->cr4)))
295 if (CC(!kvm_valid_efer(vcpu, save->efer)))
301 static bool nested_vmcb_check_save(struct kvm_vcpu *vcpu)
303 struct vcpu_svm *svm = to_svm(vcpu);
304 struct vmcb_save_area_cached *save = &svm->nested.save;
306 return __nested_vmcb_check_save(vcpu, save);
309 static bool nested_vmcb_check_controls(struct kvm_vcpu *vcpu)
311 struct vcpu_svm *svm = to_svm(vcpu);
312 struct vmcb_ctrl_area_cached *ctl = &svm->nested.ctl;
314 return __nested_vmcb_check_controls(vcpu, ctl);
318 void __nested_copy_vmcb_control_to_cache(struct kvm_vcpu *vcpu,
319 struct vmcb_ctrl_area_cached *to,
320 struct vmcb_control_area *from)
324 for (i = 0; i < MAX_INTERCEPT; i++)
325 to->intercepts[i] = from->intercepts[i];
327 to->iopm_base_pa = from->iopm_base_pa;
328 to->msrpm_base_pa = from->msrpm_base_pa;
329 to->tsc_offset = from->tsc_offset;
330 to->tlb_ctl = from->tlb_ctl;
331 to->int_ctl = from->int_ctl;
332 to->int_vector = from->int_vector;
333 to->int_state = from->int_state;
334 to->exit_code = from->exit_code;
335 to->exit_code_hi = from->exit_code_hi;
336 to->exit_info_1 = from->exit_info_1;
337 to->exit_info_2 = from->exit_info_2;
338 to->exit_int_info = from->exit_int_info;
339 to->exit_int_info_err = from->exit_int_info_err;
340 to->nested_ctl = from->nested_ctl;
341 to->event_inj = from->event_inj;
342 to->event_inj_err = from->event_inj_err;
343 to->next_rip = from->next_rip;
344 to->nested_cr3 = from->nested_cr3;
345 to->virt_ext = from->virt_ext;
346 to->pause_filter_count = from->pause_filter_count;
347 to->pause_filter_thresh = from->pause_filter_thresh;
349 /* Copy asid here because nested_vmcb_check_controls will check it. */
350 to->asid = from->asid;
351 to->msrpm_base_pa &= ~0x0fffULL;
352 to->iopm_base_pa &= ~0x0fffULL;
354 /* Hyper-V extensions (Enlightened VMCB) */
355 if (kvm_hv_hypercall_enabled(vcpu)) {
356 to->clean = from->clean;
357 memcpy(&to->hv_enlightenments, &from->hv_enlightenments,
358 sizeof(to->hv_enlightenments));
362 void nested_copy_vmcb_control_to_cache(struct vcpu_svm *svm,
363 struct vmcb_control_area *control)
365 __nested_copy_vmcb_control_to_cache(&svm->vcpu, &svm->nested.ctl, control);
368 static void __nested_copy_vmcb_save_to_cache(struct vmcb_save_area_cached *to,
369 struct vmcb_save_area *from)
372 * Copy only fields that are validated, as we need them
373 * to avoid TOC/TOU races.
375 to->efer = from->efer;
384 void nested_copy_vmcb_save_to_cache(struct vcpu_svm *svm,
385 struct vmcb_save_area *save)
387 __nested_copy_vmcb_save_to_cache(&svm->nested.save, save);
391 * Synchronize fields that are written by the processor, so that
392 * they can be copied back into the vmcb12.
394 void nested_sync_control_from_vmcb02(struct vcpu_svm *svm)
397 svm->nested.ctl.event_inj = svm->vmcb->control.event_inj;
398 svm->nested.ctl.event_inj_err = svm->vmcb->control.event_inj_err;
400 /* Only a few fields of int_ctl are written by the processor. */
401 mask = V_IRQ_MASK | V_TPR_MASK;
402 if (!(svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK) &&
403 svm_is_intercept(svm, INTERCEPT_VINTR)) {
405 * In order to request an interrupt window, L0 is usurping
406 * svm->vmcb->control.int_ctl and possibly setting V_IRQ
407 * even if it was clear in L1's VMCB. Restoring it would be
408 * wrong. However, in this case V_IRQ will remain true until
409 * interrupt_window_interception calls svm_clear_vintr and
410 * restores int_ctl. We can just leave it aside.
415 if (nested_vgif_enabled(svm))
418 svm->nested.ctl.int_ctl &= ~mask;
419 svm->nested.ctl.int_ctl |= svm->vmcb->control.int_ctl & mask;
423 * Transfer any event that L0 or L1 wanted to inject into L2 to
426 static void nested_save_pending_event_to_vmcb12(struct vcpu_svm *svm,
429 struct kvm_vcpu *vcpu = &svm->vcpu;
430 u32 exit_int_info = 0;
433 if (vcpu->arch.exception.injected) {
434 nr = vcpu->arch.exception.vector;
435 exit_int_info = nr | SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_EXEPT;
437 if (vcpu->arch.exception.has_error_code) {
438 exit_int_info |= SVM_EVTINJ_VALID_ERR;
439 vmcb12->control.exit_int_info_err =
440 vcpu->arch.exception.error_code;
443 } else if (vcpu->arch.nmi_injected) {
444 exit_int_info = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
446 } else if (vcpu->arch.interrupt.injected) {
447 nr = vcpu->arch.interrupt.nr;
448 exit_int_info = nr | SVM_EVTINJ_VALID;
450 if (vcpu->arch.interrupt.soft)
451 exit_int_info |= SVM_EVTINJ_TYPE_SOFT;
453 exit_int_info |= SVM_EVTINJ_TYPE_INTR;
456 vmcb12->control.exit_int_info = exit_int_info;
459 static void nested_svm_transition_tlb_flush(struct kvm_vcpu *vcpu)
462 * TODO: optimize unconditional TLB flush/MMU sync. A partial list of
463 * things to fix before this can be conditional:
465 * - Flush TLBs for both L1 and L2 remote TLB flush
466 * - Honor L1's request to flush an ASID on nested VMRUN
467 * - Sync nested NPT MMU on VMRUN that flushes L2's ASID[*]
468 * - Don't crush a pending TLB flush in vmcb02 on nested VMRUN
469 * - Flush L1's ASID on KVM_REQ_TLB_FLUSH_GUEST
471 * [*] Unlike nested EPT, SVM's ASID management can invalidate nested
472 * NPT guest-physical mappings on VMRUN.
474 kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
475 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
479 * Load guest's/host's cr3 on nested vmentry or vmexit. @nested_npt is true
480 * if we are emulating VM-Entry into a guest with NPT enabled.
482 static int nested_svm_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,
483 bool nested_npt, bool reload_pdptrs)
485 if (CC(kvm_vcpu_is_illegal_gpa(vcpu, cr3)))
488 if (reload_pdptrs && !nested_npt && is_pae_paging(vcpu) &&
489 CC(!load_pdptrs(vcpu, cr3)))
492 vcpu->arch.cr3 = cr3;
494 /* Re-initialize the MMU, e.g. to pick up CR4 MMU role changes. */
498 kvm_mmu_new_pgd(vcpu, cr3);
503 void nested_vmcb02_compute_g_pat(struct vcpu_svm *svm)
505 if (!svm->nested.vmcb02.ptr)
508 /* FIXME: merge g_pat from vmcb01 and vmcb12. */
509 svm->nested.vmcb02.ptr->save.g_pat = svm->vmcb01.ptr->save.g_pat;
512 static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *vmcb12)
514 bool new_vmcb12 = false;
515 struct vmcb *vmcb01 = svm->vmcb01.ptr;
516 struct vmcb *vmcb02 = svm->nested.vmcb02.ptr;
518 nested_vmcb02_compute_g_pat(svm);
520 /* Load the nested guest state */
521 if (svm->nested.vmcb12_gpa != svm->nested.last_vmcb12_gpa) {
523 svm->nested.last_vmcb12_gpa = svm->nested.vmcb12_gpa;
524 svm->nested.force_msr_bitmap_recalc = true;
527 if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_SEG))) {
528 vmcb02->save.es = vmcb12->save.es;
529 vmcb02->save.cs = vmcb12->save.cs;
530 vmcb02->save.ss = vmcb12->save.ss;
531 vmcb02->save.ds = vmcb12->save.ds;
532 vmcb02->save.cpl = vmcb12->save.cpl;
533 vmcb_mark_dirty(vmcb02, VMCB_SEG);
536 if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_DT))) {
537 vmcb02->save.gdtr = vmcb12->save.gdtr;
538 vmcb02->save.idtr = vmcb12->save.idtr;
539 vmcb_mark_dirty(vmcb02, VMCB_DT);
542 kvm_set_rflags(&svm->vcpu, vmcb12->save.rflags | X86_EFLAGS_FIXED);
544 svm_set_efer(&svm->vcpu, svm->nested.save.efer);
546 svm_set_cr0(&svm->vcpu, svm->nested.save.cr0);
547 svm_set_cr4(&svm->vcpu, svm->nested.save.cr4);
549 svm->vcpu.arch.cr2 = vmcb12->save.cr2;
551 kvm_rax_write(&svm->vcpu, vmcb12->save.rax);
552 kvm_rsp_write(&svm->vcpu, vmcb12->save.rsp);
553 kvm_rip_write(&svm->vcpu, vmcb12->save.rip);
555 /* In case we don't even reach vcpu_run, the fields are not updated */
556 vmcb02->save.rax = vmcb12->save.rax;
557 vmcb02->save.rsp = vmcb12->save.rsp;
558 vmcb02->save.rip = vmcb12->save.rip;
560 /* These bits will be set properly on the first execution when new_vmc12 is true */
561 if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_DR))) {
562 vmcb02->save.dr7 = svm->nested.save.dr7 | DR7_FIXED_1;
563 svm->vcpu.arch.dr6 = svm->nested.save.dr6 | DR6_ACTIVE_LOW;
564 vmcb_mark_dirty(vmcb02, VMCB_DR);
567 if (unlikely(svm->lbrv_enabled && (svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK))) {
569 * Reserved bits of DEBUGCTL are ignored. Be consistent with
570 * svm_set_msr's definition of reserved bits.
572 svm_copy_lbrs(vmcb02, vmcb12);
573 vmcb02->save.dbgctl &= ~DEBUGCTL_RESERVED_BITS;
574 svm_update_lbrv(&svm->vcpu);
576 } else if (unlikely(vmcb01->control.virt_ext & LBR_CTL_ENABLE_MASK)) {
577 svm_copy_lbrs(vmcb02, vmcb01);
581 static inline bool is_evtinj_soft(u32 evtinj)
583 u32 type = evtinj & SVM_EVTINJ_TYPE_MASK;
584 u8 vector = evtinj & SVM_EVTINJ_VEC_MASK;
586 if (!(evtinj & SVM_EVTINJ_VALID))
589 if (type == SVM_EVTINJ_TYPE_SOFT)
592 return type == SVM_EVTINJ_TYPE_EXEPT && kvm_exception_is_soft(vector);
595 static bool is_evtinj_nmi(u32 evtinj)
597 u32 type = evtinj & SVM_EVTINJ_TYPE_MASK;
599 if (!(evtinj & SVM_EVTINJ_VALID))
602 return type == SVM_EVTINJ_TYPE_NMI;
605 static void nested_vmcb02_prepare_control(struct vcpu_svm *svm,
606 unsigned long vmcb12_rip,
607 unsigned long vmcb12_csbase)
609 u32 int_ctl_vmcb01_bits = V_INTR_MASKING_MASK;
610 u32 int_ctl_vmcb12_bits = V_TPR_MASK | V_IRQ_INJECTION_BITS_MASK;
612 struct kvm_vcpu *vcpu = &svm->vcpu;
613 struct vmcb *vmcb01 = svm->vmcb01.ptr;
614 struct vmcb *vmcb02 = svm->nested.vmcb02.ptr;
619 * Filled at exit: exit_code, exit_code_hi, exit_info_1, exit_info_2,
620 * exit_int_info, exit_int_info_err, next_rip, insn_len, insn_bytes.
623 if (svm->vgif_enabled && (svm->nested.ctl.int_ctl & V_GIF_ENABLE_MASK))
624 int_ctl_vmcb12_bits |= (V_GIF_MASK | V_GIF_ENABLE_MASK);
626 int_ctl_vmcb01_bits |= (V_GIF_MASK | V_GIF_ENABLE_MASK);
628 /* Copied from vmcb01. msrpm_base can be overwritten later. */
629 vmcb02->control.nested_ctl = vmcb01->control.nested_ctl;
630 vmcb02->control.iopm_base_pa = vmcb01->control.iopm_base_pa;
631 vmcb02->control.msrpm_base_pa = vmcb01->control.msrpm_base_pa;
633 /* Done at vmrun: asid. */
635 /* Also overwritten later if necessary. */
636 vmcb02->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
639 if (nested_npt_enabled(svm))
640 nested_svm_init_mmu_context(vcpu);
642 vcpu->arch.tsc_offset = kvm_calc_nested_tsc_offset(
643 vcpu->arch.l1_tsc_offset,
644 svm->nested.ctl.tsc_offset,
647 vmcb02->control.tsc_offset = vcpu->arch.tsc_offset;
649 if (svm->tsc_scaling_enabled &&
650 svm->tsc_ratio_msr != kvm_caps.default_tsc_scaling_ratio)
651 nested_svm_update_tsc_ratio_msr(vcpu);
653 vmcb02->control.int_ctl =
654 (svm->nested.ctl.int_ctl & int_ctl_vmcb12_bits) |
655 (vmcb01->control.int_ctl & int_ctl_vmcb01_bits);
657 vmcb02->control.int_vector = svm->nested.ctl.int_vector;
658 vmcb02->control.int_state = svm->nested.ctl.int_state;
659 vmcb02->control.event_inj = svm->nested.ctl.event_inj;
660 vmcb02->control.event_inj_err = svm->nested.ctl.event_inj_err;
663 * next_rip is consumed on VMRUN as the return address pushed on the
664 * stack for injected soft exceptions/interrupts. If nrips is exposed
665 * to L1, take it verbatim from vmcb12. If nrips is supported in
666 * hardware but not exposed to L1, stuff the actual L2 RIP to emulate
667 * what a nrips=0 CPU would do (L1 is responsible for advancing RIP
668 * prior to injecting the event).
670 if (svm->nrips_enabled)
671 vmcb02->control.next_rip = svm->nested.ctl.next_rip;
672 else if (boot_cpu_has(X86_FEATURE_NRIPS))
673 vmcb02->control.next_rip = vmcb12_rip;
675 svm->nmi_l1_to_l2 = is_evtinj_nmi(vmcb02->control.event_inj);
676 if (is_evtinj_soft(vmcb02->control.event_inj)) {
677 svm->soft_int_injected = true;
678 svm->soft_int_csbase = vmcb12_csbase;
679 svm->soft_int_old_rip = vmcb12_rip;
680 if (svm->nrips_enabled)
681 svm->soft_int_next_rip = svm->nested.ctl.next_rip;
683 svm->soft_int_next_rip = vmcb12_rip;
686 vmcb02->control.virt_ext = vmcb01->control.virt_ext &
688 if (svm->lbrv_enabled)
689 vmcb02->control.virt_ext |=
690 (svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK);
692 if (!nested_vmcb_needs_vls_intercept(svm))
693 vmcb02->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
695 pause_count12 = svm->pause_filter_enabled ? svm->nested.ctl.pause_filter_count : 0;
696 pause_thresh12 = svm->pause_threshold_enabled ? svm->nested.ctl.pause_filter_thresh : 0;
697 if (kvm_pause_in_guest(svm->vcpu.kvm)) {
698 /* use guest values since host doesn't intercept PAUSE */
699 vmcb02->control.pause_filter_count = pause_count12;
700 vmcb02->control.pause_filter_thresh = pause_thresh12;
703 /* start from host values otherwise */
704 vmcb02->control.pause_filter_count = vmcb01->control.pause_filter_count;
705 vmcb02->control.pause_filter_thresh = vmcb01->control.pause_filter_thresh;
707 /* ... but ensure filtering is disabled if so requested. */
708 if (vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_PAUSE)) {
710 vmcb02->control.pause_filter_count = 0;
712 vmcb02->control.pause_filter_thresh = 0;
716 nested_svm_transition_tlb_flush(vcpu);
718 /* Enter Guest-Mode */
719 enter_guest_mode(vcpu);
722 * Merge guest and host intercepts - must be called with vcpu in
723 * guest-mode to take effect.
725 recalc_intercepts(svm);
728 static void nested_svm_copy_common_state(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
731 * Some VMCB state is shared between L1 and L2 and thus has to be
732 * moved at the time of nested vmrun and vmexit.
734 * VMLOAD/VMSAVE state would also belong in this category, but KVM
735 * always performs VMLOAD and VMSAVE from the VMCB01.
737 to_vmcb->save.spec_ctrl = from_vmcb->save.spec_ctrl;
740 int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb12_gpa,
741 struct vmcb *vmcb12, bool from_vmrun)
743 struct vcpu_svm *svm = to_svm(vcpu);
746 trace_kvm_nested_vmenter(svm->vmcb->save.rip,
749 vmcb12->control.int_ctl,
750 vmcb12->control.event_inj,
751 vmcb12->control.nested_ctl,
752 vmcb12->control.nested_cr3,
756 trace_kvm_nested_intercepts(vmcb12->control.intercepts[INTERCEPT_CR] & 0xffff,
757 vmcb12->control.intercepts[INTERCEPT_CR] >> 16,
758 vmcb12->control.intercepts[INTERCEPT_EXCEPTION],
759 vmcb12->control.intercepts[INTERCEPT_WORD3],
760 vmcb12->control.intercepts[INTERCEPT_WORD4],
761 vmcb12->control.intercepts[INTERCEPT_WORD5]);
764 svm->nested.vmcb12_gpa = vmcb12_gpa;
766 WARN_ON(svm->vmcb == svm->nested.vmcb02.ptr);
768 nested_svm_copy_common_state(svm->vmcb01.ptr, svm->nested.vmcb02.ptr);
770 svm_switch_vmcb(svm, &svm->nested.vmcb02);
771 nested_vmcb02_prepare_control(svm, vmcb12->save.rip, vmcb12->save.cs.base);
772 nested_vmcb02_prepare_save(svm, vmcb12);
774 ret = nested_svm_load_cr3(&svm->vcpu, svm->nested.save.cr3,
775 nested_npt_enabled(svm), from_vmrun);
780 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
782 svm_set_gif(svm, true);
784 if (kvm_vcpu_apicv_active(vcpu))
785 kvm_make_request(KVM_REQ_APICV_UPDATE, vcpu);
790 int nested_svm_vmrun(struct kvm_vcpu *vcpu)
792 struct vcpu_svm *svm = to_svm(vcpu);
795 struct kvm_host_map map;
797 struct vmcb *vmcb01 = svm->vmcb01.ptr;
799 if (!svm->nested.hsave_msr) {
800 kvm_inject_gp(vcpu, 0);
805 kvm_queue_exception(vcpu, UD_VECTOR);
809 vmcb12_gpa = svm->vmcb->save.rax;
810 ret = kvm_vcpu_map(vcpu, gpa_to_gfn(vmcb12_gpa), &map);
811 if (ret == -EINVAL) {
812 kvm_inject_gp(vcpu, 0);
815 return kvm_skip_emulated_instruction(vcpu);
818 ret = kvm_skip_emulated_instruction(vcpu);
822 if (WARN_ON_ONCE(!svm->nested.initialized))
825 nested_copy_vmcb_control_to_cache(svm, &vmcb12->control);
826 nested_copy_vmcb_save_to_cache(svm, &vmcb12->save);
828 if (!nested_vmcb_check_save(vcpu) ||
829 !nested_vmcb_check_controls(vcpu)) {
830 vmcb12->control.exit_code = SVM_EXIT_ERR;
831 vmcb12->control.exit_code_hi = 0;
832 vmcb12->control.exit_info_1 = 0;
833 vmcb12->control.exit_info_2 = 0;
838 * Since vmcb01 is not in use, we can use it to store some of the L1
841 vmcb01->save.efer = vcpu->arch.efer;
842 vmcb01->save.cr0 = kvm_read_cr0(vcpu);
843 vmcb01->save.cr4 = vcpu->arch.cr4;
844 vmcb01->save.rflags = kvm_get_rflags(vcpu);
845 vmcb01->save.rip = kvm_rip_read(vcpu);
848 vmcb01->save.cr3 = kvm_read_cr3(vcpu);
850 svm->nested.nested_run_pending = 1;
852 if (enter_svm_guest_mode(vcpu, vmcb12_gpa, vmcb12, true))
855 if (nested_svm_vmrun_msrpm(svm))
859 svm->nested.nested_run_pending = 0;
860 svm->nmi_l1_to_l2 = false;
861 svm->soft_int_injected = false;
863 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
864 svm->vmcb->control.exit_code_hi = 0;
865 svm->vmcb->control.exit_info_1 = 0;
866 svm->vmcb->control.exit_info_2 = 0;
868 nested_svm_vmexit(svm);
871 kvm_vcpu_unmap(vcpu, &map, true);
876 /* Copy state save area fields which are handled by VMRUN */
877 void svm_copy_vmrun_state(struct vmcb_save_area *to_save,
878 struct vmcb_save_area *from_save)
880 to_save->es = from_save->es;
881 to_save->cs = from_save->cs;
882 to_save->ss = from_save->ss;
883 to_save->ds = from_save->ds;
884 to_save->gdtr = from_save->gdtr;
885 to_save->idtr = from_save->idtr;
886 to_save->rflags = from_save->rflags | X86_EFLAGS_FIXED;
887 to_save->efer = from_save->efer;
888 to_save->cr0 = from_save->cr0;
889 to_save->cr3 = from_save->cr3;
890 to_save->cr4 = from_save->cr4;
891 to_save->rax = from_save->rax;
892 to_save->rsp = from_save->rsp;
893 to_save->rip = from_save->rip;
897 void svm_copy_vmloadsave_state(struct vmcb *to_vmcb, struct vmcb *from_vmcb)
899 to_vmcb->save.fs = from_vmcb->save.fs;
900 to_vmcb->save.gs = from_vmcb->save.gs;
901 to_vmcb->save.tr = from_vmcb->save.tr;
902 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
903 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
904 to_vmcb->save.star = from_vmcb->save.star;
905 to_vmcb->save.lstar = from_vmcb->save.lstar;
906 to_vmcb->save.cstar = from_vmcb->save.cstar;
907 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
908 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
909 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
910 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
913 int nested_svm_vmexit(struct vcpu_svm *svm)
915 struct kvm_vcpu *vcpu = &svm->vcpu;
916 struct vmcb *vmcb01 = svm->vmcb01.ptr;
917 struct vmcb *vmcb02 = svm->nested.vmcb02.ptr;
919 struct kvm_host_map map;
922 rc = kvm_vcpu_map(vcpu, gpa_to_gfn(svm->nested.vmcb12_gpa), &map);
925 kvm_inject_gp(vcpu, 0);
931 /* Exit Guest-Mode */
932 leave_guest_mode(vcpu);
933 svm->nested.vmcb12_gpa = 0;
934 WARN_ON_ONCE(svm->nested.nested_run_pending);
936 kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
938 /* in case we halted in L2 */
939 svm->vcpu.arch.mp_state = KVM_MP_STATE_RUNNABLE;
941 /* Give the current vmcb to the guest */
943 vmcb12->save.es = vmcb02->save.es;
944 vmcb12->save.cs = vmcb02->save.cs;
945 vmcb12->save.ss = vmcb02->save.ss;
946 vmcb12->save.ds = vmcb02->save.ds;
947 vmcb12->save.gdtr = vmcb02->save.gdtr;
948 vmcb12->save.idtr = vmcb02->save.idtr;
949 vmcb12->save.efer = svm->vcpu.arch.efer;
950 vmcb12->save.cr0 = kvm_read_cr0(vcpu);
951 vmcb12->save.cr3 = kvm_read_cr3(vcpu);
952 vmcb12->save.cr2 = vmcb02->save.cr2;
953 vmcb12->save.cr4 = svm->vcpu.arch.cr4;
954 vmcb12->save.rflags = kvm_get_rflags(vcpu);
955 vmcb12->save.rip = kvm_rip_read(vcpu);
956 vmcb12->save.rsp = kvm_rsp_read(vcpu);
957 vmcb12->save.rax = kvm_rax_read(vcpu);
958 vmcb12->save.dr7 = vmcb02->save.dr7;
959 vmcb12->save.dr6 = svm->vcpu.arch.dr6;
960 vmcb12->save.cpl = vmcb02->save.cpl;
962 vmcb12->control.int_state = vmcb02->control.int_state;
963 vmcb12->control.exit_code = vmcb02->control.exit_code;
964 vmcb12->control.exit_code_hi = vmcb02->control.exit_code_hi;
965 vmcb12->control.exit_info_1 = vmcb02->control.exit_info_1;
966 vmcb12->control.exit_info_2 = vmcb02->control.exit_info_2;
968 if (vmcb12->control.exit_code != SVM_EXIT_ERR)
969 nested_save_pending_event_to_vmcb12(svm, vmcb12);
971 if (svm->nrips_enabled)
972 vmcb12->control.next_rip = vmcb02->control.next_rip;
974 vmcb12->control.int_ctl = svm->nested.ctl.int_ctl;
975 vmcb12->control.tlb_ctl = svm->nested.ctl.tlb_ctl;
976 vmcb12->control.event_inj = svm->nested.ctl.event_inj;
977 vmcb12->control.event_inj_err = svm->nested.ctl.event_inj_err;
979 if (!kvm_pause_in_guest(vcpu->kvm)) {
980 vmcb01->control.pause_filter_count = vmcb02->control.pause_filter_count;
981 vmcb_mark_dirty(vmcb01, VMCB_INTERCEPTS);
985 nested_svm_copy_common_state(svm->nested.vmcb02.ptr, svm->vmcb01.ptr);
987 svm_switch_vmcb(svm, &svm->vmcb01);
989 if (unlikely(svm->lbrv_enabled && (svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK))) {
990 svm_copy_lbrs(vmcb12, vmcb02);
991 svm_update_lbrv(vcpu);
992 } else if (unlikely(vmcb01->control.virt_ext & LBR_CTL_ENABLE_MASK)) {
993 svm_copy_lbrs(vmcb01, vmcb02);
994 svm_update_lbrv(vcpu);
998 * On vmexit the GIF is set to false and
999 * no event can be injected in L1.
1001 svm_set_gif(svm, false);
1002 vmcb01->control.exit_int_info = 0;
1004 svm->vcpu.arch.tsc_offset = svm->vcpu.arch.l1_tsc_offset;
1005 if (vmcb01->control.tsc_offset != svm->vcpu.arch.tsc_offset) {
1006 vmcb01->control.tsc_offset = svm->vcpu.arch.tsc_offset;
1007 vmcb_mark_dirty(vmcb01, VMCB_INTERCEPTS);
1010 if (kvm_caps.has_tsc_control &&
1011 vcpu->arch.tsc_scaling_ratio != vcpu->arch.l1_tsc_scaling_ratio) {
1012 vcpu->arch.tsc_scaling_ratio = vcpu->arch.l1_tsc_scaling_ratio;
1013 __svm_write_tsc_multiplier(vcpu->arch.tsc_scaling_ratio);
1016 svm->nested.ctl.nested_cr3 = 0;
1019 * Restore processor state that had been saved in vmcb01
1021 kvm_set_rflags(vcpu, vmcb01->save.rflags);
1022 svm_set_efer(vcpu, vmcb01->save.efer);
1023 svm_set_cr0(vcpu, vmcb01->save.cr0 | X86_CR0_PE);
1024 svm_set_cr4(vcpu, vmcb01->save.cr4);
1025 kvm_rax_write(vcpu, vmcb01->save.rax);
1026 kvm_rsp_write(vcpu, vmcb01->save.rsp);
1027 kvm_rip_write(vcpu, vmcb01->save.rip);
1029 svm->vcpu.arch.dr7 = DR7_FIXED_1;
1030 kvm_update_dr7(&svm->vcpu);
1032 trace_kvm_nested_vmexit_inject(vmcb12->control.exit_code,
1033 vmcb12->control.exit_info_1,
1034 vmcb12->control.exit_info_2,
1035 vmcb12->control.exit_int_info,
1036 vmcb12->control.exit_int_info_err,
1039 kvm_vcpu_unmap(vcpu, &map, true);
1041 nested_svm_transition_tlb_flush(vcpu);
1043 nested_svm_uninit_mmu_context(vcpu);
1045 rc = nested_svm_load_cr3(vcpu, vmcb01->save.cr3, false, true);
1050 * Drop what we picked up for L2 via svm_complete_interrupts() so it
1051 * doesn't end up in L1.
1053 svm->vcpu.arch.nmi_injected = false;
1054 kvm_clear_exception_queue(vcpu);
1055 kvm_clear_interrupt_queue(vcpu);
1058 * If we are here following the completion of a VMRUN that
1059 * is being single-stepped, queue the pending #DB intercept
1060 * right now so that it an be accounted for before we execute
1061 * L1's next instruction.
1063 if (unlikely(vmcb01->save.rflags & X86_EFLAGS_TF))
1064 kvm_queue_exception(&(svm->vcpu), DB_VECTOR);
1067 * Un-inhibit the AVIC right away, so that other vCPUs can start
1068 * to benefit from it right away.
1070 if (kvm_apicv_activated(vcpu->kvm))
1071 kvm_vcpu_update_apicv(vcpu);
1076 static void nested_svm_triple_fault(struct kvm_vcpu *vcpu)
1078 struct vcpu_svm *svm = to_svm(vcpu);
1080 if (!vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_SHUTDOWN))
1083 kvm_clear_request(KVM_REQ_TRIPLE_FAULT, vcpu);
1084 nested_svm_simple_vmexit(to_svm(vcpu), SVM_EXIT_SHUTDOWN);
1087 int svm_allocate_nested(struct vcpu_svm *svm)
1089 struct page *vmcb02_page;
1091 if (svm->nested.initialized)
1094 vmcb02_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
1097 svm->nested.vmcb02.ptr = page_address(vmcb02_page);
1098 svm->nested.vmcb02.pa = __sme_set(page_to_pfn(vmcb02_page) << PAGE_SHIFT);
1100 svm->nested.msrpm = svm_vcpu_alloc_msrpm();
1101 if (!svm->nested.msrpm)
1102 goto err_free_vmcb02;
1103 svm_vcpu_init_msrpm(&svm->vcpu, svm->nested.msrpm);
1105 svm->nested.initialized = true;
1109 __free_page(vmcb02_page);
1113 void svm_free_nested(struct vcpu_svm *svm)
1115 if (!svm->nested.initialized)
1118 if (WARN_ON_ONCE(svm->vmcb != svm->vmcb01.ptr))
1119 svm_switch_vmcb(svm, &svm->vmcb01);
1121 svm_vcpu_free_msrpm(svm->nested.msrpm);
1122 svm->nested.msrpm = NULL;
1124 __free_page(virt_to_page(svm->nested.vmcb02.ptr));
1125 svm->nested.vmcb02.ptr = NULL;
1128 * When last_vmcb12_gpa matches the current vmcb12 gpa,
1129 * some vmcb12 fields are not loaded if they are marked clean
1130 * in the vmcb12, since in this case they are up to date already.
1132 * When the vmcb02 is freed, this optimization becomes invalid.
1134 svm->nested.last_vmcb12_gpa = INVALID_GPA;
1136 svm->nested.initialized = false;
1139 void svm_leave_nested(struct kvm_vcpu *vcpu)
1141 struct vcpu_svm *svm = to_svm(vcpu);
1143 if (is_guest_mode(vcpu)) {
1144 svm->nested.nested_run_pending = 0;
1145 svm->nested.vmcb12_gpa = INVALID_GPA;
1147 leave_guest_mode(vcpu);
1149 svm_switch_vmcb(svm, &svm->vmcb01);
1151 nested_svm_uninit_mmu_context(vcpu);
1152 vmcb_mark_all_dirty(svm->vmcb);
1154 if (kvm_apicv_activated(vcpu->kvm))
1155 kvm_make_request(KVM_REQ_APICV_UPDATE, vcpu);
1158 kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
1161 static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
1163 u32 offset, msr, value;
1166 if (!(vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
1167 return NESTED_EXIT_HOST;
1169 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
1170 offset = svm_msrpm_offset(msr);
1171 write = svm->vmcb->control.exit_info_1 & 1;
1172 mask = 1 << ((2 * (msr & 0xf)) + write);
1174 if (offset == MSR_INVALID)
1175 return NESTED_EXIT_DONE;
1177 /* Offset is in 32 bit units but need in 8 bit units */
1180 if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.ctl.msrpm_base_pa + offset, &value, 4))
1181 return NESTED_EXIT_DONE;
1183 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
1186 static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
1188 unsigned port, size, iopm_len;
1193 if (!(vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_IOIO_PROT)))
1194 return NESTED_EXIT_HOST;
1196 port = svm->vmcb->control.exit_info_1 >> 16;
1197 size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
1198 SVM_IOIO_SIZE_SHIFT;
1199 gpa = svm->nested.ctl.iopm_base_pa + (port / 8);
1200 start_bit = port % 8;
1201 iopm_len = (start_bit + size > 8) ? 2 : 1;
1202 mask = (0xf >> (4 - size)) << start_bit;
1205 if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
1206 return NESTED_EXIT_DONE;
1208 return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
1211 static int nested_svm_intercept(struct vcpu_svm *svm)
1213 u32 exit_code = svm->vmcb->control.exit_code;
1214 int vmexit = NESTED_EXIT_HOST;
1216 switch (exit_code) {
1218 vmexit = nested_svm_exit_handled_msr(svm);
1221 vmexit = nested_svm_intercept_ioio(svm);
1223 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
1224 if (vmcb12_is_intercept(&svm->nested.ctl, exit_code))
1225 vmexit = NESTED_EXIT_DONE;
1228 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
1229 if (vmcb12_is_intercept(&svm->nested.ctl, exit_code))
1230 vmexit = NESTED_EXIT_DONE;
1233 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1235 * Host-intercepted exceptions have been checked already in
1236 * nested_svm_exit_special. There is nothing to do here,
1237 * the vmexit is injected by svm_check_nested_events.
1239 vmexit = NESTED_EXIT_DONE;
1242 case SVM_EXIT_ERR: {
1243 vmexit = NESTED_EXIT_DONE;
1247 if (vmcb12_is_intercept(&svm->nested.ctl, exit_code))
1248 vmexit = NESTED_EXIT_DONE;
1255 int nested_svm_exit_handled(struct vcpu_svm *svm)
1259 vmexit = nested_svm_intercept(svm);
1261 if (vmexit == NESTED_EXIT_DONE)
1262 nested_svm_vmexit(svm);
1267 int nested_svm_check_permissions(struct kvm_vcpu *vcpu)
1269 if (!(vcpu->arch.efer & EFER_SVME) || !is_paging(vcpu)) {
1270 kvm_queue_exception(vcpu, UD_VECTOR);
1274 if (to_svm(vcpu)->vmcb->save.cpl) {
1275 kvm_inject_gp(vcpu, 0);
1282 static bool nested_svm_is_exception_vmexit(struct kvm_vcpu *vcpu, u8 vector,
1285 struct vcpu_svm *svm = to_svm(vcpu);
1287 return (svm->nested.ctl.intercepts[INTERCEPT_EXCEPTION] & BIT(vector));
1290 static void nested_svm_inject_exception_vmexit(struct kvm_vcpu *vcpu)
1292 struct kvm_queued_exception *ex = &vcpu->arch.exception_vmexit;
1293 struct vcpu_svm *svm = to_svm(vcpu);
1294 struct vmcb *vmcb = svm->vmcb;
1296 vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + ex->vector;
1297 vmcb->control.exit_code_hi = 0;
1299 if (ex->has_error_code)
1300 vmcb->control.exit_info_1 = ex->error_code;
1303 * EXITINFO2 is undefined for all exception intercepts other
1306 if (ex->vector == PF_VECTOR) {
1307 if (ex->has_payload)
1308 vmcb->control.exit_info_2 = ex->payload;
1310 vmcb->control.exit_info_2 = vcpu->arch.cr2;
1311 } else if (ex->vector == DB_VECTOR) {
1312 /* See kvm_check_and_inject_events(). */
1313 kvm_deliver_exception_payload(vcpu, ex);
1315 if (vcpu->arch.dr7 & DR7_GD) {
1316 vcpu->arch.dr7 &= ~DR7_GD;
1317 kvm_update_dr7(vcpu);
1320 WARN_ON(ex->has_payload);
1323 nested_svm_vmexit(svm);
1326 static inline bool nested_exit_on_init(struct vcpu_svm *svm)
1328 return vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_INIT);
1331 static int svm_check_nested_events(struct kvm_vcpu *vcpu)
1333 struct kvm_lapic *apic = vcpu->arch.apic;
1334 struct vcpu_svm *svm = to_svm(vcpu);
1336 * Only a pending nested run blocks a pending exception. If there is a
1337 * previously injected event, the pending exception occurred while said
1338 * event was being delivered and thus needs to be handled.
1340 bool block_nested_exceptions = svm->nested.nested_run_pending;
1342 * New events (not exceptions) are only recognized at instruction
1343 * boundaries. If an event needs reinjection, then KVM is handling a
1344 * VM-Exit that occurred _during_ instruction execution; new events are
1345 * blocked until the instruction completes.
1347 bool block_nested_events = block_nested_exceptions ||
1348 kvm_event_needs_reinjection(vcpu);
1350 if (lapic_in_kernel(vcpu) &&
1351 test_bit(KVM_APIC_INIT, &apic->pending_events)) {
1352 if (block_nested_events)
1354 if (!nested_exit_on_init(svm))
1356 nested_svm_simple_vmexit(svm, SVM_EXIT_INIT);
1360 if (vcpu->arch.exception_vmexit.pending) {
1361 if (block_nested_exceptions)
1363 nested_svm_inject_exception_vmexit(vcpu);
1367 if (vcpu->arch.exception.pending) {
1368 if (block_nested_exceptions)
1373 if (vcpu->arch.smi_pending && !svm_smi_blocked(vcpu)) {
1374 if (block_nested_events)
1376 if (!nested_exit_on_smi(svm))
1378 nested_svm_simple_vmexit(svm, SVM_EXIT_SMI);
1382 if (vcpu->arch.nmi_pending && !svm_nmi_blocked(vcpu)) {
1383 if (block_nested_events)
1385 if (!nested_exit_on_nmi(svm))
1387 nested_svm_simple_vmexit(svm, SVM_EXIT_NMI);
1391 if (kvm_cpu_has_interrupt(vcpu) && !svm_interrupt_blocked(vcpu)) {
1392 if (block_nested_events)
1394 if (!nested_exit_on_intr(svm))
1396 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
1397 nested_svm_simple_vmexit(svm, SVM_EXIT_INTR);
1404 int nested_svm_exit_special(struct vcpu_svm *svm)
1406 u32 exit_code = svm->vmcb->control.exit_code;
1408 switch (exit_code) {
1412 return NESTED_EXIT_HOST;
1413 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1414 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
1416 if (svm->vmcb01.ptr->control.intercepts[INTERCEPT_EXCEPTION] &
1418 return NESTED_EXIT_HOST;
1419 else if (exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR &&
1420 svm->vcpu.arch.apf.host_apf_flags)
1421 /* Trap async PF even if not shadowing */
1422 return NESTED_EXIT_HOST;
1429 return NESTED_EXIT_CONTINUE;
1432 void nested_svm_update_tsc_ratio_msr(struct kvm_vcpu *vcpu)
1434 struct vcpu_svm *svm = to_svm(vcpu);
1436 vcpu->arch.tsc_scaling_ratio =
1437 kvm_calc_nested_tsc_multiplier(vcpu->arch.l1_tsc_scaling_ratio,
1438 svm->tsc_ratio_msr);
1439 __svm_write_tsc_multiplier(vcpu->arch.tsc_scaling_ratio);
1442 /* Inverse operation of nested_copy_vmcb_control_to_cache(). asid is copied too. */
1443 static void nested_copy_vmcb_cache_to_control(struct vmcb_control_area *dst,
1444 struct vmcb_ctrl_area_cached *from)
1448 memset(dst, 0, sizeof(struct vmcb_control_area));
1450 for (i = 0; i < MAX_INTERCEPT; i++)
1451 dst->intercepts[i] = from->intercepts[i];
1453 dst->iopm_base_pa = from->iopm_base_pa;
1454 dst->msrpm_base_pa = from->msrpm_base_pa;
1455 dst->tsc_offset = from->tsc_offset;
1456 dst->asid = from->asid;
1457 dst->tlb_ctl = from->tlb_ctl;
1458 dst->int_ctl = from->int_ctl;
1459 dst->int_vector = from->int_vector;
1460 dst->int_state = from->int_state;
1461 dst->exit_code = from->exit_code;
1462 dst->exit_code_hi = from->exit_code_hi;
1463 dst->exit_info_1 = from->exit_info_1;
1464 dst->exit_info_2 = from->exit_info_2;
1465 dst->exit_int_info = from->exit_int_info;
1466 dst->exit_int_info_err = from->exit_int_info_err;
1467 dst->nested_ctl = from->nested_ctl;
1468 dst->event_inj = from->event_inj;
1469 dst->event_inj_err = from->event_inj_err;
1470 dst->next_rip = from->next_rip;
1471 dst->nested_cr3 = from->nested_cr3;
1472 dst->virt_ext = from->virt_ext;
1473 dst->pause_filter_count = from->pause_filter_count;
1474 dst->pause_filter_thresh = from->pause_filter_thresh;
1475 /* 'clean' and 'hv_enlightenments' are not changed by KVM */
1478 static int svm_get_nested_state(struct kvm_vcpu *vcpu,
1479 struct kvm_nested_state __user *user_kvm_nested_state,
1482 struct vcpu_svm *svm;
1483 struct vmcb_control_area *ctl;
1485 struct kvm_nested_state kvm_state = {
1487 .format = KVM_STATE_NESTED_FORMAT_SVM,
1488 .size = sizeof(kvm_state),
1490 struct vmcb __user *user_vmcb = (struct vmcb __user *)
1491 &user_kvm_nested_state->data.svm[0];
1494 return kvm_state.size + KVM_STATE_NESTED_SVM_VMCB_SIZE;
1498 if (user_data_size < kvm_state.size)
1501 /* First fill in the header and copy it out. */
1502 if (is_guest_mode(vcpu)) {
1503 kvm_state.hdr.svm.vmcb_pa = svm->nested.vmcb12_gpa;
1504 kvm_state.size += KVM_STATE_NESTED_SVM_VMCB_SIZE;
1505 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
1507 if (svm->nested.nested_run_pending)
1508 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
1512 kvm_state.flags |= KVM_STATE_NESTED_GIF_SET;
1514 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
1517 if (!is_guest_mode(vcpu))
1521 * Copy over the full size of the VMCB rather than just the size
1524 if (clear_user(user_vmcb, KVM_STATE_NESTED_SVM_VMCB_SIZE))
1527 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
1531 nested_copy_vmcb_cache_to_control(ctl, &svm->nested.ctl);
1532 r = copy_to_user(&user_vmcb->control, ctl,
1533 sizeof(user_vmcb->control));
1538 if (copy_to_user(&user_vmcb->save, &svm->vmcb01.ptr->save,
1539 sizeof(user_vmcb->save)))
1542 return kvm_state.size;
1545 static int svm_set_nested_state(struct kvm_vcpu *vcpu,
1546 struct kvm_nested_state __user *user_kvm_nested_state,
1547 struct kvm_nested_state *kvm_state)
1549 struct vcpu_svm *svm = to_svm(vcpu);
1550 struct vmcb __user *user_vmcb = (struct vmcb __user *)
1551 &user_kvm_nested_state->data.svm[0];
1552 struct vmcb_control_area *ctl;
1553 struct vmcb_save_area *save;
1554 struct vmcb_save_area_cached save_cached;
1555 struct vmcb_ctrl_area_cached ctl_cached;
1559 BUILD_BUG_ON(sizeof(struct vmcb_control_area) + sizeof(struct vmcb_save_area) >
1560 KVM_STATE_NESTED_SVM_VMCB_SIZE);
1562 if (kvm_state->format != KVM_STATE_NESTED_FORMAT_SVM)
1565 if (kvm_state->flags & ~(KVM_STATE_NESTED_GUEST_MODE |
1566 KVM_STATE_NESTED_RUN_PENDING |
1567 KVM_STATE_NESTED_GIF_SET))
1571 * If in guest mode, vcpu->arch.efer actually refers to the L2 guest's
1572 * EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed.
1574 if (!(vcpu->arch.efer & EFER_SVME)) {
1575 /* GIF=1 and no guest mode are required if SVME=0. */
1576 if (kvm_state->flags != KVM_STATE_NESTED_GIF_SET)
1580 /* SMM temporarily disables SVM, so we cannot be in guest mode. */
1581 if (is_smm(vcpu) && (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
1584 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) {
1585 svm_leave_nested(vcpu);
1586 svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));
1590 if (!page_address_valid(vcpu, kvm_state->hdr.svm.vmcb_pa))
1592 if (kvm_state->size < sizeof(*kvm_state) + KVM_STATE_NESTED_SVM_VMCB_SIZE)
1596 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL_ACCOUNT);
1597 save = kzalloc(sizeof(*save), GFP_KERNEL_ACCOUNT);
1602 if (copy_from_user(ctl, &user_vmcb->control, sizeof(*ctl)))
1604 if (copy_from_user(save, &user_vmcb->save, sizeof(*save)))
1608 __nested_copy_vmcb_control_to_cache(vcpu, &ctl_cached, ctl);
1609 if (!__nested_vmcb_check_controls(vcpu, &ctl_cached))
1613 * Processor state contains L2 state. Check that it is
1614 * valid for guest mode (see nested_vmcb_check_save).
1616 cr0 = kvm_read_cr0(vcpu);
1617 if (((cr0 & X86_CR0_CD) == 0) && (cr0 & X86_CR0_NW))
1621 * Validate host state saved from before VMRUN (see
1622 * nested_svm_check_permissions).
1624 __nested_copy_vmcb_save_to_cache(&save_cached, save);
1625 if (!(save->cr0 & X86_CR0_PG) ||
1626 !(save->cr0 & X86_CR0_PE) ||
1627 (save->rflags & X86_EFLAGS_VM) ||
1628 !__nested_vmcb_check_save(vcpu, &save_cached))
1633 * All checks done, we can enter guest mode. Userspace provides
1634 * vmcb12.control, which will be combined with L1 and stored into
1635 * vmcb02, and the L1 save state which we store in vmcb01.
1636 * L2 registers if needed are moved from the current VMCB to VMCB02.
1639 if (is_guest_mode(vcpu))
1640 svm_leave_nested(vcpu);
1642 svm->nested.vmcb02.ptr->save = svm->vmcb01.ptr->save;
1644 svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));
1646 svm->nested.nested_run_pending =
1647 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
1649 svm->nested.vmcb12_gpa = kvm_state->hdr.svm.vmcb_pa;
1651 svm_copy_vmrun_state(&svm->vmcb01.ptr->save, save);
1652 nested_copy_vmcb_control_to_cache(svm, ctl);
1654 svm_switch_vmcb(svm, &svm->nested.vmcb02);
1655 nested_vmcb02_prepare_control(svm, svm->vmcb->save.rip, svm->vmcb->save.cs.base);
1658 * While the nested guest CR3 is already checked and set by
1659 * KVM_SET_SREGS, it was set when nested state was yet loaded,
1660 * thus MMU might not be initialized correctly.
1661 * Set it again to fix this.
1664 ret = nested_svm_load_cr3(&svm->vcpu, vcpu->arch.cr3,
1665 nested_npt_enabled(svm), false);
1666 if (WARN_ON_ONCE(ret))
1669 svm->nested.force_msr_bitmap_recalc = true;
1671 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
1680 static bool svm_get_nested_state_pages(struct kvm_vcpu *vcpu)
1682 struct vcpu_svm *svm = to_svm(vcpu);
1684 if (WARN_ON(!is_guest_mode(vcpu)))
1687 if (!vcpu->arch.pdptrs_from_userspace &&
1688 !nested_npt_enabled(svm) && is_pae_paging(vcpu))
1690 * Reload the guest's PDPTRs since after a migration
1691 * the guest CR3 might be restored prior to setting the nested
1692 * state which can lead to a load of wrong PDPTRs.
1694 if (CC(!load_pdptrs(vcpu, vcpu->arch.cr3)))
1697 if (!nested_svm_vmrun_msrpm(svm)) {
1698 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1699 vcpu->run->internal.suberror =
1700 KVM_INTERNAL_ERROR_EMULATION;
1701 vcpu->run->internal.ndata = 0;
1708 struct kvm_x86_nested_ops svm_nested_ops = {
1709 .leave_nested = svm_leave_nested,
1710 .is_exception_vmexit = nested_svm_is_exception_vmexit,
1711 .check_events = svm_check_nested_events,
1712 .triple_fault = nested_svm_triple_fault,
1713 .get_nested_state_pages = svm_get_nested_state_pages,
1714 .get_state = svm_get_nested_state,
1715 .set_state = svm_set_nested_state,