1883b0a91SJoerg Roedel // SPDX-License-Identifier: GPL-2.0-only 2883b0a91SJoerg Roedel /* 3883b0a91SJoerg Roedel * Kernel-based Virtual Machine driver for Linux 4883b0a91SJoerg Roedel * 5883b0a91SJoerg Roedel * AMD SVM support 6883b0a91SJoerg Roedel * 7883b0a91SJoerg Roedel * Copyright (C) 2006 Qumranet, Inc. 8883b0a91SJoerg Roedel * Copyright 2010 Red Hat, Inc. and/or its affiliates. 9883b0a91SJoerg Roedel * 10883b0a91SJoerg Roedel * Authors: 11883b0a91SJoerg Roedel * Yaniv Kamay <yaniv@qumranet.com> 12883b0a91SJoerg Roedel * Avi Kivity <avi@qumranet.com> 13883b0a91SJoerg Roedel */ 14883b0a91SJoerg Roedel 15883b0a91SJoerg Roedel #define pr_fmt(fmt) "SVM: " fmt 16883b0a91SJoerg Roedel 17883b0a91SJoerg Roedel #include <linux/kvm_types.h> 18883b0a91SJoerg Roedel #include <linux/kvm_host.h> 19883b0a91SJoerg Roedel #include <linux/kernel.h> 20883b0a91SJoerg Roedel 21883b0a91SJoerg Roedel #include <asm/msr-index.h> 225679b803SPaolo Bonzini #include <asm/debugreg.h> 23883b0a91SJoerg Roedel 24883b0a91SJoerg Roedel #include "kvm_emulate.h" 25883b0a91SJoerg Roedel #include "trace.h" 26883b0a91SJoerg Roedel #include "mmu.h" 27883b0a91SJoerg Roedel #include "x86.h" 28cc440cdaSPaolo Bonzini #include "cpuid.h" 295b672408SPaolo Bonzini #include "lapic.h" 30883b0a91SJoerg Roedel #include "svm.h" 31883b0a91SJoerg Roedel 32883b0a91SJoerg Roedel static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu, 33883b0a91SJoerg Roedel struct x86_exception *fault) 34883b0a91SJoerg Roedel { 35883b0a91SJoerg Roedel struct vcpu_svm *svm = to_svm(vcpu); 36883b0a91SJoerg Roedel 37883b0a91SJoerg Roedel if (svm->vmcb->control.exit_code != SVM_EXIT_NPF) { 38883b0a91SJoerg Roedel /* 39883b0a91SJoerg Roedel * TODO: track the cause of the nested page fault, and 40883b0a91SJoerg Roedel * correctly fill in the high bits of exit_info_1. 41883b0a91SJoerg Roedel */ 42883b0a91SJoerg Roedel svm->vmcb->control.exit_code = SVM_EXIT_NPF; 43883b0a91SJoerg Roedel svm->vmcb->control.exit_code_hi = 0; 44883b0a91SJoerg Roedel svm->vmcb->control.exit_info_1 = (1ULL << 32); 45883b0a91SJoerg Roedel svm->vmcb->control.exit_info_2 = fault->address; 46883b0a91SJoerg Roedel } 47883b0a91SJoerg Roedel 48883b0a91SJoerg Roedel svm->vmcb->control.exit_info_1 &= ~0xffffffffULL; 49883b0a91SJoerg Roedel svm->vmcb->control.exit_info_1 |= fault->error_code; 50883b0a91SJoerg Roedel 51883b0a91SJoerg Roedel nested_svm_vmexit(svm); 52883b0a91SJoerg Roedel } 53883b0a91SJoerg Roedel 54883b0a91SJoerg Roedel static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index) 55883b0a91SJoerg Roedel { 56883b0a91SJoerg Roedel struct vcpu_svm *svm = to_svm(vcpu); 57e670bf68SPaolo Bonzini u64 cr3 = svm->nested.ctl.nested_cr3; 58883b0a91SJoerg Roedel u64 pdpte; 59883b0a91SJoerg Roedel int ret; 60883b0a91SJoerg Roedel 61883b0a91SJoerg Roedel ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(__sme_clr(cr3)), &pdpte, 62883b0a91SJoerg Roedel offset_in_page(cr3) + index * 8, 8); 63883b0a91SJoerg Roedel if (ret) 64883b0a91SJoerg Roedel return 0; 65883b0a91SJoerg Roedel return pdpte; 66883b0a91SJoerg Roedel } 67883b0a91SJoerg Roedel 68883b0a91SJoerg Roedel static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu) 69883b0a91SJoerg Roedel { 70883b0a91SJoerg Roedel struct vcpu_svm *svm = to_svm(vcpu); 71883b0a91SJoerg Roedel 72e670bf68SPaolo Bonzini return svm->nested.ctl.nested_cr3; 73883b0a91SJoerg Roedel } 74883b0a91SJoerg Roedel 75883b0a91SJoerg Roedel static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu) 76883b0a91SJoerg Roedel { 77929d1cfaSPaolo Bonzini struct vcpu_svm *svm = to_svm(vcpu); 78929d1cfaSPaolo Bonzini struct vmcb *hsave = svm->nested.hsave; 79929d1cfaSPaolo Bonzini 80883b0a91SJoerg Roedel WARN_ON(mmu_is_nested(vcpu)); 81883b0a91SJoerg Roedel 82883b0a91SJoerg Roedel vcpu->arch.mmu = &vcpu->arch.guest_mmu; 830f04a2acSVitaly Kuznetsov kvm_init_shadow_npt_mmu(vcpu, X86_CR0_PG, hsave->save.cr4, hsave->save.efer, 840f04a2acSVitaly Kuznetsov svm->nested.ctl.nested_cr3); 85883b0a91SJoerg Roedel vcpu->arch.mmu->get_guest_pgd = nested_svm_get_tdp_cr3; 86883b0a91SJoerg Roedel vcpu->arch.mmu->get_pdptr = nested_svm_get_tdp_pdptr; 87883b0a91SJoerg Roedel vcpu->arch.mmu->inject_page_fault = nested_svm_inject_npf_exit; 88883b0a91SJoerg Roedel reset_shadow_zero_bits_mask(vcpu, vcpu->arch.mmu); 89883b0a91SJoerg Roedel vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; 90883b0a91SJoerg Roedel } 91883b0a91SJoerg Roedel 92883b0a91SJoerg Roedel static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu) 93883b0a91SJoerg Roedel { 94883b0a91SJoerg Roedel vcpu->arch.mmu = &vcpu->arch.root_mmu; 95883b0a91SJoerg Roedel vcpu->arch.walk_mmu = &vcpu->arch.root_mmu; 96883b0a91SJoerg Roedel } 97883b0a91SJoerg Roedel 98883b0a91SJoerg Roedel void recalc_intercepts(struct vcpu_svm *svm) 99883b0a91SJoerg Roedel { 100e670bf68SPaolo Bonzini struct vmcb_control_area *c, *h, *g; 101883b0a91SJoerg Roedel 10206e7852cSJoerg Roedel vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS); 103883b0a91SJoerg Roedel 104883b0a91SJoerg Roedel if (!is_guest_mode(&svm->vcpu)) 105883b0a91SJoerg Roedel return; 106883b0a91SJoerg Roedel 107883b0a91SJoerg Roedel c = &svm->vmcb->control; 108883b0a91SJoerg Roedel h = &svm->nested.hsave->control; 109e670bf68SPaolo Bonzini g = &svm->nested.ctl; 110883b0a91SJoerg Roedel 1117c86663bSPaolo Bonzini svm->nested.host_intercept_exceptions = h->intercept_exceptions; 1127c86663bSPaolo Bonzini 113883b0a91SJoerg Roedel c->intercept_cr = h->intercept_cr; 114883b0a91SJoerg Roedel c->intercept_dr = h->intercept_dr; 115883b0a91SJoerg Roedel c->intercept_exceptions = h->intercept_exceptions; 116883b0a91SJoerg Roedel c->intercept = h->intercept; 117883b0a91SJoerg Roedel 118e9fd761aSPaolo Bonzini if (g->int_ctl & V_INTR_MASKING_MASK) { 119883b0a91SJoerg Roedel /* We only want the cr8 intercept bits of L1 */ 120883b0a91SJoerg Roedel c->intercept_cr &= ~(1U << INTERCEPT_CR8_READ); 121883b0a91SJoerg Roedel c->intercept_cr &= ~(1U << INTERCEPT_CR8_WRITE); 122883b0a91SJoerg Roedel 123883b0a91SJoerg Roedel /* 124883b0a91SJoerg Roedel * Once running L2 with HF_VINTR_MASK, EFLAGS.IF does not 125883b0a91SJoerg Roedel * affect any interrupt we may want to inject; therefore, 126883b0a91SJoerg Roedel * interrupt window vmexits are irrelevant to L0. 127883b0a91SJoerg Roedel */ 128883b0a91SJoerg Roedel c->intercept &= ~(1ULL << INTERCEPT_VINTR); 129883b0a91SJoerg Roedel } 130883b0a91SJoerg Roedel 131883b0a91SJoerg Roedel /* We don't want to see VMMCALLs from a nested guest */ 132883b0a91SJoerg Roedel c->intercept &= ~(1ULL << INTERCEPT_VMMCALL); 133883b0a91SJoerg Roedel 134883b0a91SJoerg Roedel c->intercept_cr |= g->intercept_cr; 135883b0a91SJoerg Roedel c->intercept_dr |= g->intercept_dr; 136883b0a91SJoerg Roedel c->intercept_exceptions |= g->intercept_exceptions; 137883b0a91SJoerg Roedel c->intercept |= g->intercept; 138883b0a91SJoerg Roedel } 139883b0a91SJoerg Roedel 1402f675917SPaolo Bonzini static void copy_vmcb_control_area(struct vmcb_control_area *dst, 1412f675917SPaolo Bonzini struct vmcb_control_area *from) 142883b0a91SJoerg Roedel { 143883b0a91SJoerg Roedel dst->intercept_cr = from->intercept_cr; 144883b0a91SJoerg Roedel dst->intercept_dr = from->intercept_dr; 145883b0a91SJoerg Roedel dst->intercept_exceptions = from->intercept_exceptions; 146883b0a91SJoerg Roedel dst->intercept = from->intercept; 147883b0a91SJoerg Roedel dst->iopm_base_pa = from->iopm_base_pa; 148883b0a91SJoerg Roedel dst->msrpm_base_pa = from->msrpm_base_pa; 149883b0a91SJoerg Roedel dst->tsc_offset = from->tsc_offset; 1506c0238c4SPaolo Bonzini /* asid not copied, it is handled manually for svm->vmcb. */ 151883b0a91SJoerg Roedel dst->tlb_ctl = from->tlb_ctl; 152883b0a91SJoerg Roedel dst->int_ctl = from->int_ctl; 153883b0a91SJoerg Roedel dst->int_vector = from->int_vector; 154883b0a91SJoerg Roedel dst->int_state = from->int_state; 155883b0a91SJoerg Roedel dst->exit_code = from->exit_code; 156883b0a91SJoerg Roedel dst->exit_code_hi = from->exit_code_hi; 157883b0a91SJoerg Roedel dst->exit_info_1 = from->exit_info_1; 158883b0a91SJoerg Roedel dst->exit_info_2 = from->exit_info_2; 159883b0a91SJoerg Roedel dst->exit_int_info = from->exit_int_info; 160883b0a91SJoerg Roedel dst->exit_int_info_err = from->exit_int_info_err; 161883b0a91SJoerg Roedel dst->nested_ctl = from->nested_ctl; 162883b0a91SJoerg Roedel dst->event_inj = from->event_inj; 163883b0a91SJoerg Roedel dst->event_inj_err = from->event_inj_err; 164883b0a91SJoerg Roedel dst->nested_cr3 = from->nested_cr3; 165883b0a91SJoerg Roedel dst->virt_ext = from->virt_ext; 166883b0a91SJoerg Roedel dst->pause_filter_count = from->pause_filter_count; 167883b0a91SJoerg Roedel dst->pause_filter_thresh = from->pause_filter_thresh; 168883b0a91SJoerg Roedel } 169883b0a91SJoerg Roedel 170883b0a91SJoerg Roedel static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm) 171883b0a91SJoerg Roedel { 172883b0a91SJoerg Roedel /* 173883b0a91SJoerg Roedel * This function merges the msr permission bitmaps of kvm and the 174883b0a91SJoerg Roedel * nested vmcb. It is optimized in that it only merges the parts where 175883b0a91SJoerg Roedel * the kvm msr permission bitmap may contain zero bits 176883b0a91SJoerg Roedel */ 177883b0a91SJoerg Roedel int i; 178883b0a91SJoerg Roedel 179e670bf68SPaolo Bonzini if (!(svm->nested.ctl.intercept & (1ULL << INTERCEPT_MSR_PROT))) 180883b0a91SJoerg Roedel return true; 181883b0a91SJoerg Roedel 182883b0a91SJoerg Roedel for (i = 0; i < MSRPM_OFFSETS; i++) { 183883b0a91SJoerg Roedel u32 value, p; 184883b0a91SJoerg Roedel u64 offset; 185883b0a91SJoerg Roedel 186883b0a91SJoerg Roedel if (msrpm_offsets[i] == 0xffffffff) 187883b0a91SJoerg Roedel break; 188883b0a91SJoerg Roedel 189883b0a91SJoerg Roedel p = msrpm_offsets[i]; 190e670bf68SPaolo Bonzini offset = svm->nested.ctl.msrpm_base_pa + (p * 4); 191883b0a91SJoerg Roedel 192883b0a91SJoerg Roedel if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4)) 193883b0a91SJoerg Roedel return false; 194883b0a91SJoerg Roedel 195883b0a91SJoerg Roedel svm->nested.msrpm[p] = svm->msrpm[p] | value; 196883b0a91SJoerg Roedel } 197883b0a91SJoerg Roedel 198883b0a91SJoerg Roedel svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm)); 199883b0a91SJoerg Roedel 200883b0a91SJoerg Roedel return true; 201883b0a91SJoerg Roedel } 202883b0a91SJoerg Roedel 203ca46d739SPaolo Bonzini static bool nested_vmcb_check_controls(struct vmcb_control_area *control) 204ca46d739SPaolo Bonzini { 205ca46d739SPaolo Bonzini if ((control->intercept & (1ULL << INTERCEPT_VMRUN)) == 0) 206ca46d739SPaolo Bonzini return false; 207ca46d739SPaolo Bonzini 208ca46d739SPaolo Bonzini if (control->asid == 0) 209ca46d739SPaolo Bonzini return false; 210ca46d739SPaolo Bonzini 211ca46d739SPaolo Bonzini if ((control->nested_ctl & SVM_NESTED_CTL_NP_ENABLE) && 212ca46d739SPaolo Bonzini !npt_enabled) 213ca46d739SPaolo Bonzini return false; 214ca46d739SPaolo Bonzini 215ca46d739SPaolo Bonzini return true; 216ca46d739SPaolo Bonzini } 217ca46d739SPaolo Bonzini 218761e4169SKrish Sadhukhan static bool nested_vmcb_checks(struct vcpu_svm *svm, struct vmcb *vmcb) 219883b0a91SJoerg Roedel { 220761e4169SKrish Sadhukhan bool nested_vmcb_lma; 221883b0a91SJoerg Roedel if ((vmcb->save.efer & EFER_SVME) == 0) 222883b0a91SJoerg Roedel return false; 223883b0a91SJoerg Roedel 2244f233371SKrish Sadhukhan if (((vmcb->save.cr0 & X86_CR0_CD) == 0) && 2254f233371SKrish Sadhukhan (vmcb->save.cr0 & X86_CR0_NW)) 2264f233371SKrish Sadhukhan return false; 2274f233371SKrish Sadhukhan 2281aef8161SKrish Sadhukhan if (!kvm_dr6_valid(vmcb->save.dr6) || !kvm_dr7_valid(vmcb->save.dr7)) 2291aef8161SKrish Sadhukhan return false; 2301aef8161SKrish Sadhukhan 231761e4169SKrish Sadhukhan nested_vmcb_lma = 232761e4169SKrish Sadhukhan (vmcb->save.efer & EFER_LME) && 233761e4169SKrish Sadhukhan (vmcb->save.cr0 & X86_CR0_PG); 234761e4169SKrish Sadhukhan 235761e4169SKrish Sadhukhan if (!nested_vmcb_lma) { 236761e4169SKrish Sadhukhan if (vmcb->save.cr4 & X86_CR4_PAE) { 237761e4169SKrish Sadhukhan if (vmcb->save.cr3 & MSR_CR3_LEGACY_PAE_RESERVED_MASK) 238761e4169SKrish Sadhukhan return false; 239761e4169SKrish Sadhukhan } else { 240761e4169SKrish Sadhukhan if (vmcb->save.cr3 & MSR_CR3_LEGACY_RESERVED_MASK) 241761e4169SKrish Sadhukhan return false; 242761e4169SKrish Sadhukhan } 243761e4169SKrish Sadhukhan } else { 244761e4169SKrish Sadhukhan if (!(vmcb->save.cr4 & X86_CR4_PAE) || 245761e4169SKrish Sadhukhan !(vmcb->save.cr0 & X86_CR0_PE) || 246761e4169SKrish Sadhukhan (vmcb->save.cr3 & MSR_CR3_LONG_RESERVED_MASK)) 247761e4169SKrish Sadhukhan return false; 248761e4169SKrish Sadhukhan } 249761e4169SKrish Sadhukhan if (kvm_valid_cr4(&svm->vcpu, vmcb->save.cr4)) 250761e4169SKrish Sadhukhan return false; 251761e4169SKrish Sadhukhan 252ca46d739SPaolo Bonzini return nested_vmcb_check_controls(&vmcb->control); 253883b0a91SJoerg Roedel } 254883b0a91SJoerg Roedel 2553e06f016SPaolo Bonzini static void load_nested_vmcb_control(struct vcpu_svm *svm, 2563e06f016SPaolo Bonzini struct vmcb_control_area *control) 2573e06f016SPaolo Bonzini { 258e670bf68SPaolo Bonzini copy_vmcb_control_area(&svm->nested.ctl, control); 2593e06f016SPaolo Bonzini 260cc440cdaSPaolo Bonzini /* Copy it here because nested_svm_check_controls will check it. */ 261cc440cdaSPaolo Bonzini svm->nested.ctl.asid = control->asid; 262e670bf68SPaolo Bonzini svm->nested.ctl.msrpm_base_pa &= ~0x0fffULL; 263e670bf68SPaolo Bonzini svm->nested.ctl.iopm_base_pa &= ~0x0fffULL; 2643e06f016SPaolo Bonzini } 2653e06f016SPaolo Bonzini 2662d8a42beSPaolo Bonzini /* 2672d8a42beSPaolo Bonzini * Synchronize fields that are written by the processor, so that 2682d8a42beSPaolo Bonzini * they can be copied back into the nested_vmcb. 2692d8a42beSPaolo Bonzini */ 2702d8a42beSPaolo Bonzini void sync_nested_vmcb_control(struct vcpu_svm *svm) 2712d8a42beSPaolo Bonzini { 2722d8a42beSPaolo Bonzini u32 mask; 2732d8a42beSPaolo Bonzini svm->nested.ctl.event_inj = svm->vmcb->control.event_inj; 2742d8a42beSPaolo Bonzini svm->nested.ctl.event_inj_err = svm->vmcb->control.event_inj_err; 2752d8a42beSPaolo Bonzini 2762d8a42beSPaolo Bonzini /* Only a few fields of int_ctl are written by the processor. */ 2772d8a42beSPaolo Bonzini mask = V_IRQ_MASK | V_TPR_MASK; 2782d8a42beSPaolo Bonzini if (!(svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK) && 279a284ba56SJoerg Roedel svm_is_intercept(svm, INTERCEPT_VINTR)) { 2802d8a42beSPaolo Bonzini /* 2812d8a42beSPaolo Bonzini * In order to request an interrupt window, L0 is usurping 2822d8a42beSPaolo Bonzini * svm->vmcb->control.int_ctl and possibly setting V_IRQ 2832d8a42beSPaolo Bonzini * even if it was clear in L1's VMCB. Restoring it would be 2842d8a42beSPaolo Bonzini * wrong. However, in this case V_IRQ will remain true until 2852d8a42beSPaolo Bonzini * interrupt_window_interception calls svm_clear_vintr and 2862d8a42beSPaolo Bonzini * restores int_ctl. We can just leave it aside. 2872d8a42beSPaolo Bonzini */ 2882d8a42beSPaolo Bonzini mask &= ~V_IRQ_MASK; 2892d8a42beSPaolo Bonzini } 2902d8a42beSPaolo Bonzini svm->nested.ctl.int_ctl &= ~mask; 2912d8a42beSPaolo Bonzini svm->nested.ctl.int_ctl |= svm->vmcb->control.int_ctl & mask; 2922d8a42beSPaolo Bonzini } 2932d8a42beSPaolo Bonzini 29436e2e983SPaolo Bonzini /* 29536e2e983SPaolo Bonzini * Transfer any event that L0 or L1 wanted to inject into L2 to 29636e2e983SPaolo Bonzini * EXIT_INT_INFO. 29736e2e983SPaolo Bonzini */ 29836e2e983SPaolo Bonzini static void nested_vmcb_save_pending_event(struct vcpu_svm *svm, 29936e2e983SPaolo Bonzini struct vmcb *nested_vmcb) 30036e2e983SPaolo Bonzini { 30136e2e983SPaolo Bonzini struct kvm_vcpu *vcpu = &svm->vcpu; 30236e2e983SPaolo Bonzini u32 exit_int_info = 0; 30336e2e983SPaolo Bonzini unsigned int nr; 30436e2e983SPaolo Bonzini 30536e2e983SPaolo Bonzini if (vcpu->arch.exception.injected) { 30636e2e983SPaolo Bonzini nr = vcpu->arch.exception.nr; 30736e2e983SPaolo Bonzini exit_int_info = nr | SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_EXEPT; 30836e2e983SPaolo Bonzini 30936e2e983SPaolo Bonzini if (vcpu->arch.exception.has_error_code) { 31036e2e983SPaolo Bonzini exit_int_info |= SVM_EVTINJ_VALID_ERR; 31136e2e983SPaolo Bonzini nested_vmcb->control.exit_int_info_err = 31236e2e983SPaolo Bonzini vcpu->arch.exception.error_code; 31336e2e983SPaolo Bonzini } 31436e2e983SPaolo Bonzini 31536e2e983SPaolo Bonzini } else if (vcpu->arch.nmi_injected) { 31636e2e983SPaolo Bonzini exit_int_info = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI; 31736e2e983SPaolo Bonzini 31836e2e983SPaolo Bonzini } else if (vcpu->arch.interrupt.injected) { 31936e2e983SPaolo Bonzini nr = vcpu->arch.interrupt.nr; 32036e2e983SPaolo Bonzini exit_int_info = nr | SVM_EVTINJ_VALID; 32136e2e983SPaolo Bonzini 32236e2e983SPaolo Bonzini if (vcpu->arch.interrupt.soft) 32336e2e983SPaolo Bonzini exit_int_info |= SVM_EVTINJ_TYPE_SOFT; 32436e2e983SPaolo Bonzini else 32536e2e983SPaolo Bonzini exit_int_info |= SVM_EVTINJ_TYPE_INTR; 32636e2e983SPaolo Bonzini } 32736e2e983SPaolo Bonzini 32836e2e983SPaolo Bonzini nested_vmcb->control.exit_int_info = exit_int_info; 32936e2e983SPaolo Bonzini } 33036e2e983SPaolo Bonzini 33162156f6cSVitaly Kuznetsov static inline bool nested_npt_enabled(struct vcpu_svm *svm) 33262156f6cSVitaly Kuznetsov { 33362156f6cSVitaly Kuznetsov return svm->nested.ctl.nested_ctl & SVM_NESTED_CTL_NP_ENABLE; 33462156f6cSVitaly Kuznetsov } 33562156f6cSVitaly Kuznetsov 33662156f6cSVitaly Kuznetsov /* 337d82aaef9SVitaly Kuznetsov * Load guest's/host's cr3 on nested vmentry or vmexit. @nested_npt is true 338d82aaef9SVitaly Kuznetsov * if we are emulating VM-Entry into a guest with NPT enabled. 33962156f6cSVitaly Kuznetsov */ 34062156f6cSVitaly Kuznetsov static int nested_svm_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, 34162156f6cSVitaly Kuznetsov bool nested_npt) 34262156f6cSVitaly Kuznetsov { 343a506fdd2SVitaly Kuznetsov if (cr3 & rsvd_bits(cpuid_maxphyaddr(vcpu), 63)) 344a506fdd2SVitaly Kuznetsov return -EINVAL; 345a506fdd2SVitaly Kuznetsov 346a506fdd2SVitaly Kuznetsov if (!nested_npt && is_pae_paging(vcpu) && 347a506fdd2SVitaly Kuznetsov (cr3 != kvm_read_cr3(vcpu) || pdptrs_changed(vcpu))) { 348a506fdd2SVitaly Kuznetsov if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) 349a506fdd2SVitaly Kuznetsov return -EINVAL; 350a506fdd2SVitaly Kuznetsov } 351a506fdd2SVitaly Kuznetsov 352a506fdd2SVitaly Kuznetsov /* 353a506fdd2SVitaly Kuznetsov * TODO: optimize unconditional TLB flush/MMU sync here and in 354a506fdd2SVitaly Kuznetsov * kvm_init_shadow_npt_mmu(). 355a506fdd2SVitaly Kuznetsov */ 356a506fdd2SVitaly Kuznetsov if (!nested_npt) 357a506fdd2SVitaly Kuznetsov kvm_mmu_new_pgd(vcpu, cr3, false, false); 358a506fdd2SVitaly Kuznetsov 359a506fdd2SVitaly Kuznetsov vcpu->arch.cr3 = cr3; 360a506fdd2SVitaly Kuznetsov kvm_register_mark_available(vcpu, VCPU_EXREG_CR3); 361a506fdd2SVitaly Kuznetsov 362a506fdd2SVitaly Kuznetsov kvm_init_mmu(vcpu, false); 363a506fdd2SVitaly Kuznetsov 364a506fdd2SVitaly Kuznetsov return 0; 36562156f6cSVitaly Kuznetsov } 36662156f6cSVitaly Kuznetsov 367f241d711SPaolo Bonzini static void nested_prepare_vmcb_save(struct vcpu_svm *svm, struct vmcb *nested_vmcb) 368883b0a91SJoerg Roedel { 369883b0a91SJoerg Roedel /* Load the nested guest state */ 370883b0a91SJoerg Roedel svm->vmcb->save.es = nested_vmcb->save.es; 371883b0a91SJoerg Roedel svm->vmcb->save.cs = nested_vmcb->save.cs; 372883b0a91SJoerg Roedel svm->vmcb->save.ss = nested_vmcb->save.ss; 373883b0a91SJoerg Roedel svm->vmcb->save.ds = nested_vmcb->save.ds; 374883b0a91SJoerg Roedel svm->vmcb->save.gdtr = nested_vmcb->save.gdtr; 375883b0a91SJoerg Roedel svm->vmcb->save.idtr = nested_vmcb->save.idtr; 376883b0a91SJoerg Roedel kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags); 377883b0a91SJoerg Roedel svm_set_efer(&svm->vcpu, nested_vmcb->save.efer); 378883b0a91SJoerg Roedel svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0); 379883b0a91SJoerg Roedel svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4); 380883b0a91SJoerg Roedel svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2; 381883b0a91SJoerg Roedel kvm_rax_write(&svm->vcpu, nested_vmcb->save.rax); 382883b0a91SJoerg Roedel kvm_rsp_write(&svm->vcpu, nested_vmcb->save.rsp); 383883b0a91SJoerg Roedel kvm_rip_write(&svm->vcpu, nested_vmcb->save.rip); 384883b0a91SJoerg Roedel 385883b0a91SJoerg Roedel /* In case we don't even reach vcpu_run, the fields are not updated */ 386883b0a91SJoerg Roedel svm->vmcb->save.rax = nested_vmcb->save.rax; 387883b0a91SJoerg Roedel svm->vmcb->save.rsp = nested_vmcb->save.rsp; 388883b0a91SJoerg Roedel svm->vmcb->save.rip = nested_vmcb->save.rip; 389883b0a91SJoerg Roedel svm->vmcb->save.dr7 = nested_vmcb->save.dr7; 3905679b803SPaolo Bonzini svm->vcpu.arch.dr6 = nested_vmcb->save.dr6; 391883b0a91SJoerg Roedel svm->vmcb->save.cpl = nested_vmcb->save.cpl; 392f241d711SPaolo Bonzini } 393883b0a91SJoerg Roedel 394e670bf68SPaolo Bonzini static void nested_prepare_vmcb_control(struct vcpu_svm *svm) 395f241d711SPaolo Bonzini { 39691b7130cSPaolo Bonzini const u32 mask = V_INTR_MASKING_MASK | V_GIF_ENABLE_MASK | V_GIF_MASK; 39762156f6cSVitaly Kuznetsov 39862156f6cSVitaly Kuznetsov if (nested_npt_enabled(svm)) 39969cb8774SPaolo Bonzini nested_svm_init_mmu_context(&svm->vcpu); 40069cb8774SPaolo Bonzini 40118fc6c55SPaolo Bonzini svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset = 402e670bf68SPaolo Bonzini svm->vcpu.arch.l1_tsc_offset + svm->nested.ctl.tsc_offset; 403883b0a91SJoerg Roedel 40491b7130cSPaolo Bonzini svm->vmcb->control.int_ctl = 40591b7130cSPaolo Bonzini (svm->nested.ctl.int_ctl & ~mask) | 40691b7130cSPaolo Bonzini (svm->nested.hsave->control.int_ctl & mask); 40791b7130cSPaolo Bonzini 408e670bf68SPaolo Bonzini svm->vmcb->control.virt_ext = svm->nested.ctl.virt_ext; 409e670bf68SPaolo Bonzini svm->vmcb->control.int_vector = svm->nested.ctl.int_vector; 410e670bf68SPaolo Bonzini svm->vmcb->control.int_state = svm->nested.ctl.int_state; 411e670bf68SPaolo Bonzini svm->vmcb->control.event_inj = svm->nested.ctl.event_inj; 412e670bf68SPaolo Bonzini svm->vmcb->control.event_inj_err = svm->nested.ctl.event_inj_err; 413883b0a91SJoerg Roedel 414e670bf68SPaolo Bonzini svm->vmcb->control.pause_filter_count = svm->nested.ctl.pause_filter_count; 415e670bf68SPaolo Bonzini svm->vmcb->control.pause_filter_thresh = svm->nested.ctl.pause_filter_thresh; 416883b0a91SJoerg Roedel 417883b0a91SJoerg Roedel /* Enter Guest-Mode */ 418883b0a91SJoerg Roedel enter_guest_mode(&svm->vcpu); 419883b0a91SJoerg Roedel 420883b0a91SJoerg Roedel /* 421883b0a91SJoerg Roedel * Merge guest and host intercepts - must be called with vcpu in 422883b0a91SJoerg Roedel * guest-mode to take affect here 423883b0a91SJoerg Roedel */ 424883b0a91SJoerg Roedel recalc_intercepts(svm); 425883b0a91SJoerg Roedel 42606e7852cSJoerg Roedel vmcb_mark_all_dirty(svm->vmcb); 427f241d711SPaolo Bonzini } 428f241d711SPaolo Bonzini 42959cd9bc5SVitaly Kuznetsov int enter_svm_guest_mode(struct vcpu_svm *svm, u64 vmcb_gpa, 430f241d711SPaolo Bonzini struct vmcb *nested_vmcb) 431f241d711SPaolo Bonzini { 432a506fdd2SVitaly Kuznetsov int ret; 433a506fdd2SVitaly Kuznetsov 434f241d711SPaolo Bonzini svm->nested.vmcb = vmcb_gpa; 435f241d711SPaolo Bonzini load_nested_vmcb_control(svm, &nested_vmcb->control); 436f241d711SPaolo Bonzini nested_prepare_vmcb_save(svm, nested_vmcb); 437e670bf68SPaolo Bonzini nested_prepare_vmcb_control(svm); 438f241d711SPaolo Bonzini 439a506fdd2SVitaly Kuznetsov ret = nested_svm_load_cr3(&svm->vcpu, nested_vmcb->save.cr3, 440a506fdd2SVitaly Kuznetsov nested_npt_enabled(svm)); 441a506fdd2SVitaly Kuznetsov if (ret) 442a506fdd2SVitaly Kuznetsov return ret; 443a506fdd2SVitaly Kuznetsov 444ffdf7f9eSPaolo Bonzini svm_set_gif(svm, true); 44559cd9bc5SVitaly Kuznetsov 44659cd9bc5SVitaly Kuznetsov return 0; 447883b0a91SJoerg Roedel } 448883b0a91SJoerg Roedel 449883b0a91SJoerg Roedel int nested_svm_vmrun(struct vcpu_svm *svm) 450883b0a91SJoerg Roedel { 451883b0a91SJoerg Roedel int ret; 452883b0a91SJoerg Roedel struct vmcb *nested_vmcb; 453883b0a91SJoerg Roedel struct vmcb *hsave = svm->nested.hsave; 454883b0a91SJoerg Roedel struct vmcb *vmcb = svm->vmcb; 455883b0a91SJoerg Roedel struct kvm_host_map map; 456883b0a91SJoerg Roedel u64 vmcb_gpa; 457883b0a91SJoerg Roedel 4587c67f546SPaolo Bonzini if (is_smm(&svm->vcpu)) { 4597c67f546SPaolo Bonzini kvm_queue_exception(&svm->vcpu, UD_VECTOR); 4607c67f546SPaolo Bonzini return 1; 4617c67f546SPaolo Bonzini } 462883b0a91SJoerg Roedel 4637c67f546SPaolo Bonzini vmcb_gpa = svm->vmcb->save.rax; 464883b0a91SJoerg Roedel ret = kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(vmcb_gpa), &map); 465883b0a91SJoerg Roedel if (ret == -EINVAL) { 466883b0a91SJoerg Roedel kvm_inject_gp(&svm->vcpu, 0); 467883b0a91SJoerg Roedel return 1; 468883b0a91SJoerg Roedel } else if (ret) { 469883b0a91SJoerg Roedel return kvm_skip_emulated_instruction(&svm->vcpu); 470883b0a91SJoerg Roedel } 471883b0a91SJoerg Roedel 472883b0a91SJoerg Roedel ret = kvm_skip_emulated_instruction(&svm->vcpu); 473883b0a91SJoerg Roedel 474883b0a91SJoerg Roedel nested_vmcb = map.hva; 475883b0a91SJoerg Roedel 476761e4169SKrish Sadhukhan if (!nested_vmcb_checks(svm, nested_vmcb)) { 477883b0a91SJoerg Roedel nested_vmcb->control.exit_code = SVM_EXIT_ERR; 478883b0a91SJoerg Roedel nested_vmcb->control.exit_code_hi = 0; 479883b0a91SJoerg Roedel nested_vmcb->control.exit_info_1 = 0; 480883b0a91SJoerg Roedel nested_vmcb->control.exit_info_2 = 0; 48169c9dfa2SPaolo Bonzini goto out; 482883b0a91SJoerg Roedel } 483883b0a91SJoerg Roedel 484883b0a91SJoerg Roedel trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa, 485883b0a91SJoerg Roedel nested_vmcb->save.rip, 486883b0a91SJoerg Roedel nested_vmcb->control.int_ctl, 487883b0a91SJoerg Roedel nested_vmcb->control.event_inj, 488883b0a91SJoerg Roedel nested_vmcb->control.nested_ctl); 489883b0a91SJoerg Roedel 490883b0a91SJoerg Roedel trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff, 491883b0a91SJoerg Roedel nested_vmcb->control.intercept_cr >> 16, 492883b0a91SJoerg Roedel nested_vmcb->control.intercept_exceptions, 493883b0a91SJoerg Roedel nested_vmcb->control.intercept); 494883b0a91SJoerg Roedel 495883b0a91SJoerg Roedel /* Clear internal status */ 496883b0a91SJoerg Roedel kvm_clear_exception_queue(&svm->vcpu); 497883b0a91SJoerg Roedel kvm_clear_interrupt_queue(&svm->vcpu); 498883b0a91SJoerg Roedel 499883b0a91SJoerg Roedel /* 500883b0a91SJoerg Roedel * Save the old vmcb, so we don't need to pick what we save, but can 501883b0a91SJoerg Roedel * restore everything when a VMEXIT occurs 502883b0a91SJoerg Roedel */ 503883b0a91SJoerg Roedel hsave->save.es = vmcb->save.es; 504883b0a91SJoerg Roedel hsave->save.cs = vmcb->save.cs; 505883b0a91SJoerg Roedel hsave->save.ss = vmcb->save.ss; 506883b0a91SJoerg Roedel hsave->save.ds = vmcb->save.ds; 507883b0a91SJoerg Roedel hsave->save.gdtr = vmcb->save.gdtr; 508883b0a91SJoerg Roedel hsave->save.idtr = vmcb->save.idtr; 509883b0a91SJoerg Roedel hsave->save.efer = svm->vcpu.arch.efer; 510883b0a91SJoerg Roedel hsave->save.cr0 = kvm_read_cr0(&svm->vcpu); 511883b0a91SJoerg Roedel hsave->save.cr4 = svm->vcpu.arch.cr4; 512883b0a91SJoerg Roedel hsave->save.rflags = kvm_get_rflags(&svm->vcpu); 513883b0a91SJoerg Roedel hsave->save.rip = kvm_rip_read(&svm->vcpu); 514883b0a91SJoerg Roedel hsave->save.rsp = vmcb->save.rsp; 515883b0a91SJoerg Roedel hsave->save.rax = vmcb->save.rax; 516883b0a91SJoerg Roedel if (npt_enabled) 517883b0a91SJoerg Roedel hsave->save.cr3 = vmcb->save.cr3; 518883b0a91SJoerg Roedel else 519883b0a91SJoerg Roedel hsave->save.cr3 = kvm_read_cr3(&svm->vcpu); 520883b0a91SJoerg Roedel 5212f675917SPaolo Bonzini copy_vmcb_control_area(&hsave->control, &vmcb->control); 522883b0a91SJoerg Roedel 523f74f9414SPaolo Bonzini svm->nested.nested_run_pending = 1; 524883b0a91SJoerg Roedel 52559cd9bc5SVitaly Kuznetsov if (enter_svm_guest_mode(svm, vmcb_gpa, nested_vmcb)) 52659cd9bc5SVitaly Kuznetsov goto out_exit_err; 52759cd9bc5SVitaly Kuznetsov 52859cd9bc5SVitaly Kuznetsov if (nested_svm_vmrun_msrpm(svm)) 52959cd9bc5SVitaly Kuznetsov goto out; 53059cd9bc5SVitaly Kuznetsov 53159cd9bc5SVitaly Kuznetsov out_exit_err: 532ebdb3dbaSVitaly Kuznetsov svm->nested.nested_run_pending = 0; 533ebdb3dbaSVitaly Kuznetsov 534883b0a91SJoerg Roedel svm->vmcb->control.exit_code = SVM_EXIT_ERR; 535883b0a91SJoerg Roedel svm->vmcb->control.exit_code_hi = 0; 536883b0a91SJoerg Roedel svm->vmcb->control.exit_info_1 = 0; 537883b0a91SJoerg Roedel svm->vmcb->control.exit_info_2 = 0; 538883b0a91SJoerg Roedel 539883b0a91SJoerg Roedel nested_svm_vmexit(svm); 540883b0a91SJoerg Roedel 54169c9dfa2SPaolo Bonzini out: 54269c9dfa2SPaolo Bonzini kvm_vcpu_unmap(&svm->vcpu, &map, true); 54369c9dfa2SPaolo Bonzini 544883b0a91SJoerg Roedel return ret; 545883b0a91SJoerg Roedel } 546883b0a91SJoerg Roedel 547883b0a91SJoerg Roedel void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb) 548883b0a91SJoerg Roedel { 549883b0a91SJoerg Roedel to_vmcb->save.fs = from_vmcb->save.fs; 550883b0a91SJoerg Roedel to_vmcb->save.gs = from_vmcb->save.gs; 551883b0a91SJoerg Roedel to_vmcb->save.tr = from_vmcb->save.tr; 552883b0a91SJoerg Roedel to_vmcb->save.ldtr = from_vmcb->save.ldtr; 553883b0a91SJoerg Roedel to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base; 554883b0a91SJoerg Roedel to_vmcb->save.star = from_vmcb->save.star; 555883b0a91SJoerg Roedel to_vmcb->save.lstar = from_vmcb->save.lstar; 556883b0a91SJoerg Roedel to_vmcb->save.cstar = from_vmcb->save.cstar; 557883b0a91SJoerg Roedel to_vmcb->save.sfmask = from_vmcb->save.sfmask; 558883b0a91SJoerg Roedel to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs; 559883b0a91SJoerg Roedel to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp; 560883b0a91SJoerg Roedel to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip; 561883b0a91SJoerg Roedel } 562883b0a91SJoerg Roedel 563883b0a91SJoerg Roedel int nested_svm_vmexit(struct vcpu_svm *svm) 564883b0a91SJoerg Roedel { 565883b0a91SJoerg Roedel int rc; 566883b0a91SJoerg Roedel struct vmcb *nested_vmcb; 567883b0a91SJoerg Roedel struct vmcb *hsave = svm->nested.hsave; 568883b0a91SJoerg Roedel struct vmcb *vmcb = svm->vmcb; 569883b0a91SJoerg Roedel struct kvm_host_map map; 570883b0a91SJoerg Roedel 571883b0a91SJoerg Roedel rc = kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(svm->nested.vmcb), &map); 572883b0a91SJoerg Roedel if (rc) { 573883b0a91SJoerg Roedel if (rc == -EINVAL) 574883b0a91SJoerg Roedel kvm_inject_gp(&svm->vcpu, 0); 575883b0a91SJoerg Roedel return 1; 576883b0a91SJoerg Roedel } 577883b0a91SJoerg Roedel 578883b0a91SJoerg Roedel nested_vmcb = map.hva; 579883b0a91SJoerg Roedel 580883b0a91SJoerg Roedel /* Exit Guest-Mode */ 581883b0a91SJoerg Roedel leave_guest_mode(&svm->vcpu); 582883b0a91SJoerg Roedel svm->nested.vmcb = 0; 5832d8a42beSPaolo Bonzini WARN_ON_ONCE(svm->nested.nested_run_pending); 584883b0a91SJoerg Roedel 58538c0b192SPaolo Bonzini /* in case we halted in L2 */ 58638c0b192SPaolo Bonzini svm->vcpu.arch.mp_state = KVM_MP_STATE_RUNNABLE; 58738c0b192SPaolo Bonzini 588883b0a91SJoerg Roedel /* Give the current vmcb to the guest */ 589ffdf7f9eSPaolo Bonzini svm_set_gif(svm, false); 590883b0a91SJoerg Roedel 591883b0a91SJoerg Roedel nested_vmcb->save.es = vmcb->save.es; 592883b0a91SJoerg Roedel nested_vmcb->save.cs = vmcb->save.cs; 593883b0a91SJoerg Roedel nested_vmcb->save.ss = vmcb->save.ss; 594883b0a91SJoerg Roedel nested_vmcb->save.ds = vmcb->save.ds; 595883b0a91SJoerg Roedel nested_vmcb->save.gdtr = vmcb->save.gdtr; 596883b0a91SJoerg Roedel nested_vmcb->save.idtr = vmcb->save.idtr; 597883b0a91SJoerg Roedel nested_vmcb->save.efer = svm->vcpu.arch.efer; 598883b0a91SJoerg Roedel nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu); 599883b0a91SJoerg Roedel nested_vmcb->save.cr3 = kvm_read_cr3(&svm->vcpu); 600883b0a91SJoerg Roedel nested_vmcb->save.cr2 = vmcb->save.cr2; 601883b0a91SJoerg Roedel nested_vmcb->save.cr4 = svm->vcpu.arch.cr4; 602883b0a91SJoerg Roedel nested_vmcb->save.rflags = kvm_get_rflags(&svm->vcpu); 603b6162e82SVitaly Kuznetsov nested_vmcb->save.rip = kvm_rip_read(&svm->vcpu); 604b6162e82SVitaly Kuznetsov nested_vmcb->save.rsp = kvm_rsp_read(&svm->vcpu); 605b6162e82SVitaly Kuznetsov nested_vmcb->save.rax = kvm_rax_read(&svm->vcpu); 606883b0a91SJoerg Roedel nested_vmcb->save.dr7 = vmcb->save.dr7; 6075679b803SPaolo Bonzini nested_vmcb->save.dr6 = svm->vcpu.arch.dr6; 608883b0a91SJoerg Roedel nested_vmcb->save.cpl = vmcb->save.cpl; 609883b0a91SJoerg Roedel 610883b0a91SJoerg Roedel nested_vmcb->control.int_state = vmcb->control.int_state; 611883b0a91SJoerg Roedel nested_vmcb->control.exit_code = vmcb->control.exit_code; 612883b0a91SJoerg Roedel nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi; 613883b0a91SJoerg Roedel nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1; 614883b0a91SJoerg Roedel nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2; 61536e2e983SPaolo Bonzini 61636e2e983SPaolo Bonzini if (nested_vmcb->control.exit_code != SVM_EXIT_ERR) 61736e2e983SPaolo Bonzini nested_vmcb_save_pending_event(svm, nested_vmcb); 618883b0a91SJoerg Roedel 619883b0a91SJoerg Roedel if (svm->nrips_enabled) 620883b0a91SJoerg Roedel nested_vmcb->control.next_rip = vmcb->control.next_rip; 621883b0a91SJoerg Roedel 6222d8a42beSPaolo Bonzini nested_vmcb->control.int_ctl = svm->nested.ctl.int_ctl; 6232d8a42beSPaolo Bonzini nested_vmcb->control.tlb_ctl = svm->nested.ctl.tlb_ctl; 6242d8a42beSPaolo Bonzini nested_vmcb->control.event_inj = svm->nested.ctl.event_inj; 6252d8a42beSPaolo Bonzini nested_vmcb->control.event_inj_err = svm->nested.ctl.event_inj_err; 626883b0a91SJoerg Roedel 627883b0a91SJoerg Roedel nested_vmcb->control.pause_filter_count = 628883b0a91SJoerg Roedel svm->vmcb->control.pause_filter_count; 629883b0a91SJoerg Roedel nested_vmcb->control.pause_filter_thresh = 630883b0a91SJoerg Roedel svm->vmcb->control.pause_filter_thresh; 631883b0a91SJoerg Roedel 632883b0a91SJoerg Roedel /* Restore the original control entries */ 6332f675917SPaolo Bonzini copy_vmcb_control_area(&vmcb->control, &hsave->control); 634883b0a91SJoerg Roedel 63518fc6c55SPaolo Bonzini svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset = 63618fc6c55SPaolo Bonzini svm->vcpu.arch.l1_tsc_offset; 63718fc6c55SPaolo Bonzini 638e670bf68SPaolo Bonzini svm->nested.ctl.nested_cr3 = 0; 639883b0a91SJoerg Roedel 640883b0a91SJoerg Roedel /* Restore selected save entries */ 641883b0a91SJoerg Roedel svm->vmcb->save.es = hsave->save.es; 642883b0a91SJoerg Roedel svm->vmcb->save.cs = hsave->save.cs; 643883b0a91SJoerg Roedel svm->vmcb->save.ss = hsave->save.ss; 644883b0a91SJoerg Roedel svm->vmcb->save.ds = hsave->save.ds; 645883b0a91SJoerg Roedel svm->vmcb->save.gdtr = hsave->save.gdtr; 646883b0a91SJoerg Roedel svm->vmcb->save.idtr = hsave->save.idtr; 647883b0a91SJoerg Roedel kvm_set_rflags(&svm->vcpu, hsave->save.rflags); 648883b0a91SJoerg Roedel svm_set_efer(&svm->vcpu, hsave->save.efer); 649883b0a91SJoerg Roedel svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE); 650883b0a91SJoerg Roedel svm_set_cr4(&svm->vcpu, hsave->save.cr4); 651883b0a91SJoerg Roedel kvm_rax_write(&svm->vcpu, hsave->save.rax); 652883b0a91SJoerg Roedel kvm_rsp_write(&svm->vcpu, hsave->save.rsp); 653883b0a91SJoerg Roedel kvm_rip_write(&svm->vcpu, hsave->save.rip); 654883b0a91SJoerg Roedel svm->vmcb->save.dr7 = 0; 655883b0a91SJoerg Roedel svm->vmcb->save.cpl = 0; 656883b0a91SJoerg Roedel svm->vmcb->control.exit_int_info = 0; 657883b0a91SJoerg Roedel 65806e7852cSJoerg Roedel vmcb_mark_all_dirty(svm->vmcb); 659883b0a91SJoerg Roedel 66036e2e983SPaolo Bonzini trace_kvm_nested_vmexit_inject(nested_vmcb->control.exit_code, 66136e2e983SPaolo Bonzini nested_vmcb->control.exit_info_1, 66236e2e983SPaolo Bonzini nested_vmcb->control.exit_info_2, 66336e2e983SPaolo Bonzini nested_vmcb->control.exit_int_info, 66436e2e983SPaolo Bonzini nested_vmcb->control.exit_int_info_err, 66536e2e983SPaolo Bonzini KVM_ISA_SVM); 66636e2e983SPaolo Bonzini 667883b0a91SJoerg Roedel kvm_vcpu_unmap(&svm->vcpu, &map, true); 668883b0a91SJoerg Roedel 669883b0a91SJoerg Roedel nested_svm_uninit_mmu_context(&svm->vcpu); 670bf7dea42SVitaly Kuznetsov 671d82aaef9SVitaly Kuznetsov rc = nested_svm_load_cr3(&svm->vcpu, hsave->save.cr3, false); 672d82aaef9SVitaly Kuznetsov if (rc) 673d82aaef9SVitaly Kuznetsov return 1; 674bf7dea42SVitaly Kuznetsov 675d82aaef9SVitaly Kuznetsov if (npt_enabled) 676d82aaef9SVitaly Kuznetsov svm->vmcb->save.cr3 = hsave->save.cr3; 677883b0a91SJoerg Roedel 678883b0a91SJoerg Roedel /* 679883b0a91SJoerg Roedel * Drop what we picked up for L2 via svm_complete_interrupts() so it 680883b0a91SJoerg Roedel * doesn't end up in L1. 681883b0a91SJoerg Roedel */ 682883b0a91SJoerg Roedel svm->vcpu.arch.nmi_injected = false; 683883b0a91SJoerg Roedel kvm_clear_exception_queue(&svm->vcpu); 684883b0a91SJoerg Roedel kvm_clear_interrupt_queue(&svm->vcpu); 685883b0a91SJoerg Roedel 686883b0a91SJoerg Roedel return 0; 687883b0a91SJoerg Roedel } 688883b0a91SJoerg Roedel 689c513f484SPaolo Bonzini /* 690c513f484SPaolo Bonzini * Forcibly leave nested mode in order to be able to reset the VCPU later on. 691c513f484SPaolo Bonzini */ 692c513f484SPaolo Bonzini void svm_leave_nested(struct vcpu_svm *svm) 693c513f484SPaolo Bonzini { 694c513f484SPaolo Bonzini if (is_guest_mode(&svm->vcpu)) { 695c513f484SPaolo Bonzini struct vmcb *hsave = svm->nested.hsave; 696c513f484SPaolo Bonzini struct vmcb *vmcb = svm->vmcb; 697c513f484SPaolo Bonzini 698c513f484SPaolo Bonzini svm->nested.nested_run_pending = 0; 699c513f484SPaolo Bonzini leave_guest_mode(&svm->vcpu); 700c513f484SPaolo Bonzini copy_vmcb_control_area(&vmcb->control, &hsave->control); 701c513f484SPaolo Bonzini nested_svm_uninit_mmu_context(&svm->vcpu); 702c513f484SPaolo Bonzini } 703c513f484SPaolo Bonzini } 704c513f484SPaolo Bonzini 705883b0a91SJoerg Roedel static int nested_svm_exit_handled_msr(struct vcpu_svm *svm) 706883b0a91SJoerg Roedel { 707883b0a91SJoerg Roedel u32 offset, msr, value; 708883b0a91SJoerg Roedel int write, mask; 709883b0a91SJoerg Roedel 710e670bf68SPaolo Bonzini if (!(svm->nested.ctl.intercept & (1ULL << INTERCEPT_MSR_PROT))) 711883b0a91SJoerg Roedel return NESTED_EXIT_HOST; 712883b0a91SJoerg Roedel 713883b0a91SJoerg Roedel msr = svm->vcpu.arch.regs[VCPU_REGS_RCX]; 714883b0a91SJoerg Roedel offset = svm_msrpm_offset(msr); 715883b0a91SJoerg Roedel write = svm->vmcb->control.exit_info_1 & 1; 716883b0a91SJoerg Roedel mask = 1 << ((2 * (msr & 0xf)) + write); 717883b0a91SJoerg Roedel 718883b0a91SJoerg Roedel if (offset == MSR_INVALID) 719883b0a91SJoerg Roedel return NESTED_EXIT_DONE; 720883b0a91SJoerg Roedel 721883b0a91SJoerg Roedel /* Offset is in 32 bit units but need in 8 bit units */ 722883b0a91SJoerg Roedel offset *= 4; 723883b0a91SJoerg Roedel 724e670bf68SPaolo Bonzini if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.ctl.msrpm_base_pa + offset, &value, 4)) 725883b0a91SJoerg Roedel return NESTED_EXIT_DONE; 726883b0a91SJoerg Roedel 727883b0a91SJoerg Roedel return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST; 728883b0a91SJoerg Roedel } 729883b0a91SJoerg Roedel 730883b0a91SJoerg Roedel static int nested_svm_intercept_ioio(struct vcpu_svm *svm) 731883b0a91SJoerg Roedel { 732883b0a91SJoerg Roedel unsigned port, size, iopm_len; 733883b0a91SJoerg Roedel u16 val, mask; 734883b0a91SJoerg Roedel u8 start_bit; 735883b0a91SJoerg Roedel u64 gpa; 736883b0a91SJoerg Roedel 737e670bf68SPaolo Bonzini if (!(svm->nested.ctl.intercept & (1ULL << INTERCEPT_IOIO_PROT))) 738883b0a91SJoerg Roedel return NESTED_EXIT_HOST; 739883b0a91SJoerg Roedel 740883b0a91SJoerg Roedel port = svm->vmcb->control.exit_info_1 >> 16; 741883b0a91SJoerg Roedel size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >> 742883b0a91SJoerg Roedel SVM_IOIO_SIZE_SHIFT; 743e670bf68SPaolo Bonzini gpa = svm->nested.ctl.iopm_base_pa + (port / 8); 744883b0a91SJoerg Roedel start_bit = port % 8; 745883b0a91SJoerg Roedel iopm_len = (start_bit + size > 8) ? 2 : 1; 746883b0a91SJoerg Roedel mask = (0xf >> (4 - size)) << start_bit; 747883b0a91SJoerg Roedel val = 0; 748883b0a91SJoerg Roedel 749883b0a91SJoerg Roedel if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len)) 750883b0a91SJoerg Roedel return NESTED_EXIT_DONE; 751883b0a91SJoerg Roedel 752883b0a91SJoerg Roedel return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST; 753883b0a91SJoerg Roedel } 754883b0a91SJoerg Roedel 755883b0a91SJoerg Roedel static int nested_svm_intercept(struct vcpu_svm *svm) 756883b0a91SJoerg Roedel { 757883b0a91SJoerg Roedel u32 exit_code = svm->vmcb->control.exit_code; 758883b0a91SJoerg Roedel int vmexit = NESTED_EXIT_HOST; 759883b0a91SJoerg Roedel 760883b0a91SJoerg Roedel switch (exit_code) { 761883b0a91SJoerg Roedel case SVM_EXIT_MSR: 762883b0a91SJoerg Roedel vmexit = nested_svm_exit_handled_msr(svm); 763883b0a91SJoerg Roedel break; 764883b0a91SJoerg Roedel case SVM_EXIT_IOIO: 765883b0a91SJoerg Roedel vmexit = nested_svm_intercept_ioio(svm); 766883b0a91SJoerg Roedel break; 767883b0a91SJoerg Roedel case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: { 768883b0a91SJoerg Roedel u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0); 769e670bf68SPaolo Bonzini if (svm->nested.ctl.intercept_cr & bit) 770883b0a91SJoerg Roedel vmexit = NESTED_EXIT_DONE; 771883b0a91SJoerg Roedel break; 772883b0a91SJoerg Roedel } 773883b0a91SJoerg Roedel case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: { 774883b0a91SJoerg Roedel u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0); 775e670bf68SPaolo Bonzini if (svm->nested.ctl.intercept_dr & bit) 776883b0a91SJoerg Roedel vmexit = NESTED_EXIT_DONE; 777883b0a91SJoerg Roedel break; 778883b0a91SJoerg Roedel } 779883b0a91SJoerg Roedel case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: { 7807c86663bSPaolo Bonzini /* 7817c86663bSPaolo Bonzini * Host-intercepted exceptions have been checked already in 7827c86663bSPaolo Bonzini * nested_svm_exit_special. There is nothing to do here, 7837c86663bSPaolo Bonzini * the vmexit is injected by svm_check_nested_events. 7847c86663bSPaolo Bonzini */ 785883b0a91SJoerg Roedel vmexit = NESTED_EXIT_DONE; 786883b0a91SJoerg Roedel break; 787883b0a91SJoerg Roedel } 788883b0a91SJoerg Roedel case SVM_EXIT_ERR: { 789883b0a91SJoerg Roedel vmexit = NESTED_EXIT_DONE; 790883b0a91SJoerg Roedel break; 791883b0a91SJoerg Roedel } 792883b0a91SJoerg Roedel default: { 793883b0a91SJoerg Roedel u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR); 794e670bf68SPaolo Bonzini if (svm->nested.ctl.intercept & exit_bits) 795883b0a91SJoerg Roedel vmexit = NESTED_EXIT_DONE; 796883b0a91SJoerg Roedel } 797883b0a91SJoerg Roedel } 798883b0a91SJoerg Roedel 799883b0a91SJoerg Roedel return vmexit; 800883b0a91SJoerg Roedel } 801883b0a91SJoerg Roedel 802883b0a91SJoerg Roedel int nested_svm_exit_handled(struct vcpu_svm *svm) 803883b0a91SJoerg Roedel { 804883b0a91SJoerg Roedel int vmexit; 805883b0a91SJoerg Roedel 806883b0a91SJoerg Roedel vmexit = nested_svm_intercept(svm); 807883b0a91SJoerg Roedel 808883b0a91SJoerg Roedel if (vmexit == NESTED_EXIT_DONE) 809883b0a91SJoerg Roedel nested_svm_vmexit(svm); 810883b0a91SJoerg Roedel 811883b0a91SJoerg Roedel return vmexit; 812883b0a91SJoerg Roedel } 813883b0a91SJoerg Roedel 814883b0a91SJoerg Roedel int nested_svm_check_permissions(struct vcpu_svm *svm) 815883b0a91SJoerg Roedel { 816883b0a91SJoerg Roedel if (!(svm->vcpu.arch.efer & EFER_SVME) || 817883b0a91SJoerg Roedel !is_paging(&svm->vcpu)) { 818883b0a91SJoerg Roedel kvm_queue_exception(&svm->vcpu, UD_VECTOR); 819883b0a91SJoerg Roedel return 1; 820883b0a91SJoerg Roedel } 821883b0a91SJoerg Roedel 822883b0a91SJoerg Roedel if (svm->vmcb->save.cpl) { 823883b0a91SJoerg Roedel kvm_inject_gp(&svm->vcpu, 0); 824883b0a91SJoerg Roedel return 1; 825883b0a91SJoerg Roedel } 826883b0a91SJoerg Roedel 827883b0a91SJoerg Roedel return 0; 828883b0a91SJoerg Roedel } 829883b0a91SJoerg Roedel 8307c86663bSPaolo Bonzini static bool nested_exit_on_exception(struct vcpu_svm *svm) 831883b0a91SJoerg Roedel { 8327c86663bSPaolo Bonzini unsigned int nr = svm->vcpu.arch.exception.nr; 833883b0a91SJoerg Roedel 834e670bf68SPaolo Bonzini return (svm->nested.ctl.intercept_exceptions & (1 << nr)); 8357c86663bSPaolo Bonzini } 836883b0a91SJoerg Roedel 8377c86663bSPaolo Bonzini static void nested_svm_inject_exception_vmexit(struct vcpu_svm *svm) 8387c86663bSPaolo Bonzini { 8397c86663bSPaolo Bonzini unsigned int nr = svm->vcpu.arch.exception.nr; 840883b0a91SJoerg Roedel 841883b0a91SJoerg Roedel svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr; 842883b0a91SJoerg Roedel svm->vmcb->control.exit_code_hi = 0; 8437c86663bSPaolo Bonzini 8447c86663bSPaolo Bonzini if (svm->vcpu.arch.exception.has_error_code) 8457c86663bSPaolo Bonzini svm->vmcb->control.exit_info_1 = svm->vcpu.arch.exception.error_code; 846883b0a91SJoerg Roedel 847883b0a91SJoerg Roedel /* 848883b0a91SJoerg Roedel * EXITINFO2 is undefined for all exception intercepts other 849883b0a91SJoerg Roedel * than #PF. 850883b0a91SJoerg Roedel */ 8517c86663bSPaolo Bonzini if (nr == PF_VECTOR) { 852883b0a91SJoerg Roedel if (svm->vcpu.arch.exception.nested_apf) 853883b0a91SJoerg Roedel svm->vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token; 854883b0a91SJoerg Roedel else if (svm->vcpu.arch.exception.has_payload) 855883b0a91SJoerg Roedel svm->vmcb->control.exit_info_2 = svm->vcpu.arch.exception.payload; 856883b0a91SJoerg Roedel else 857883b0a91SJoerg Roedel svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2; 8587c86663bSPaolo Bonzini } else if (nr == DB_VECTOR) { 8597c86663bSPaolo Bonzini /* See inject_pending_event. */ 8607c86663bSPaolo Bonzini kvm_deliver_exception_payload(&svm->vcpu); 8617c86663bSPaolo Bonzini if (svm->vcpu.arch.dr7 & DR7_GD) { 8627c86663bSPaolo Bonzini svm->vcpu.arch.dr7 &= ~DR7_GD; 8637c86663bSPaolo Bonzini kvm_update_dr7(&svm->vcpu); 8647c86663bSPaolo Bonzini } 8657c86663bSPaolo Bonzini } else 8667c86663bSPaolo Bonzini WARN_ON(svm->vcpu.arch.exception.has_payload); 867883b0a91SJoerg Roedel 8687c86663bSPaolo Bonzini nested_svm_vmexit(svm); 869883b0a91SJoerg Roedel } 870883b0a91SJoerg Roedel 87155714cddSPaolo Bonzini static void nested_svm_smi(struct vcpu_svm *svm) 87255714cddSPaolo Bonzini { 87355714cddSPaolo Bonzini svm->vmcb->control.exit_code = SVM_EXIT_SMI; 87455714cddSPaolo Bonzini svm->vmcb->control.exit_info_1 = 0; 87555714cddSPaolo Bonzini svm->vmcb->control.exit_info_2 = 0; 87655714cddSPaolo Bonzini 87755714cddSPaolo Bonzini nested_svm_vmexit(svm); 87855714cddSPaolo Bonzini } 87955714cddSPaolo Bonzini 8809c3d370aSCathy Avery static void nested_svm_nmi(struct vcpu_svm *svm) 8819c3d370aSCathy Avery { 8829c3d370aSCathy Avery svm->vmcb->control.exit_code = SVM_EXIT_NMI; 8839c3d370aSCathy Avery svm->vmcb->control.exit_info_1 = 0; 8849c3d370aSCathy Avery svm->vmcb->control.exit_info_2 = 0; 8859c3d370aSCathy Avery 8869c3d370aSCathy Avery nested_svm_vmexit(svm); 8879c3d370aSCathy Avery } 8889c3d370aSCathy Avery 889883b0a91SJoerg Roedel static void nested_svm_intr(struct vcpu_svm *svm) 890883b0a91SJoerg Roedel { 8916e085cbfSPaolo Bonzini trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip); 8926e085cbfSPaolo Bonzini 893883b0a91SJoerg Roedel svm->vmcb->control.exit_code = SVM_EXIT_INTR; 894883b0a91SJoerg Roedel svm->vmcb->control.exit_info_1 = 0; 895883b0a91SJoerg Roedel svm->vmcb->control.exit_info_2 = 0; 896883b0a91SJoerg Roedel 8976e085cbfSPaolo Bonzini nested_svm_vmexit(svm); 898883b0a91SJoerg Roedel } 899883b0a91SJoerg Roedel 9005b672408SPaolo Bonzini static inline bool nested_exit_on_init(struct vcpu_svm *svm) 9015b672408SPaolo Bonzini { 902e670bf68SPaolo Bonzini return (svm->nested.ctl.intercept & (1ULL << INTERCEPT_INIT)); 9035b672408SPaolo Bonzini } 9045b672408SPaolo Bonzini 9055b672408SPaolo Bonzini static void nested_svm_init(struct vcpu_svm *svm) 9065b672408SPaolo Bonzini { 9075b672408SPaolo Bonzini svm->vmcb->control.exit_code = SVM_EXIT_INIT; 9085b672408SPaolo Bonzini svm->vmcb->control.exit_info_1 = 0; 9095b672408SPaolo Bonzini svm->vmcb->control.exit_info_2 = 0; 9105b672408SPaolo Bonzini 9115b672408SPaolo Bonzini nested_svm_vmexit(svm); 9125b672408SPaolo Bonzini } 9135b672408SPaolo Bonzini 9145b672408SPaolo Bonzini 91533b22172SPaolo Bonzini static int svm_check_nested_events(struct kvm_vcpu *vcpu) 916883b0a91SJoerg Roedel { 917883b0a91SJoerg Roedel struct vcpu_svm *svm = to_svm(vcpu); 918883b0a91SJoerg Roedel bool block_nested_events = 919bd279629SPaolo Bonzini kvm_event_needs_reinjection(vcpu) || svm->nested.nested_run_pending; 9205b672408SPaolo Bonzini struct kvm_lapic *apic = vcpu->arch.apic; 9215b672408SPaolo Bonzini 9225b672408SPaolo Bonzini if (lapic_in_kernel(vcpu) && 9235b672408SPaolo Bonzini test_bit(KVM_APIC_INIT, &apic->pending_events)) { 9245b672408SPaolo Bonzini if (block_nested_events) 9255b672408SPaolo Bonzini return -EBUSY; 9265b672408SPaolo Bonzini if (!nested_exit_on_init(svm)) 9275b672408SPaolo Bonzini return 0; 9285b672408SPaolo Bonzini nested_svm_init(svm); 9295b672408SPaolo Bonzini return 0; 9305b672408SPaolo Bonzini } 931883b0a91SJoerg Roedel 9327c86663bSPaolo Bonzini if (vcpu->arch.exception.pending) { 9337c86663bSPaolo Bonzini if (block_nested_events) 9347c86663bSPaolo Bonzini return -EBUSY; 9357c86663bSPaolo Bonzini if (!nested_exit_on_exception(svm)) 9367c86663bSPaolo Bonzini return 0; 9377c86663bSPaolo Bonzini nested_svm_inject_exception_vmexit(svm); 9387c86663bSPaolo Bonzini return 0; 9397c86663bSPaolo Bonzini } 9407c86663bSPaolo Bonzini 941221e7610SPaolo Bonzini if (vcpu->arch.smi_pending && !svm_smi_blocked(vcpu)) { 94255714cddSPaolo Bonzini if (block_nested_events) 94355714cddSPaolo Bonzini return -EBUSY; 944221e7610SPaolo Bonzini if (!nested_exit_on_smi(svm)) 945221e7610SPaolo Bonzini return 0; 94655714cddSPaolo Bonzini nested_svm_smi(svm); 94755714cddSPaolo Bonzini return 0; 94855714cddSPaolo Bonzini } 94955714cddSPaolo Bonzini 950221e7610SPaolo Bonzini if (vcpu->arch.nmi_pending && !svm_nmi_blocked(vcpu)) { 9519c3d370aSCathy Avery if (block_nested_events) 9529c3d370aSCathy Avery return -EBUSY; 953221e7610SPaolo Bonzini if (!nested_exit_on_nmi(svm)) 954221e7610SPaolo Bonzini return 0; 9559c3d370aSCathy Avery nested_svm_nmi(svm); 9569c3d370aSCathy Avery return 0; 9579c3d370aSCathy Avery } 9589c3d370aSCathy Avery 959221e7610SPaolo Bonzini if (kvm_cpu_has_interrupt(vcpu) && !svm_interrupt_blocked(vcpu)) { 960883b0a91SJoerg Roedel if (block_nested_events) 961883b0a91SJoerg Roedel return -EBUSY; 962221e7610SPaolo Bonzini if (!nested_exit_on_intr(svm)) 963221e7610SPaolo Bonzini return 0; 964883b0a91SJoerg Roedel nested_svm_intr(svm); 965883b0a91SJoerg Roedel return 0; 966883b0a91SJoerg Roedel } 967883b0a91SJoerg Roedel 968883b0a91SJoerg Roedel return 0; 969883b0a91SJoerg Roedel } 970883b0a91SJoerg Roedel 971883b0a91SJoerg Roedel int nested_svm_exit_special(struct vcpu_svm *svm) 972883b0a91SJoerg Roedel { 973883b0a91SJoerg Roedel u32 exit_code = svm->vmcb->control.exit_code; 974883b0a91SJoerg Roedel 975883b0a91SJoerg Roedel switch (exit_code) { 976883b0a91SJoerg Roedel case SVM_EXIT_INTR: 977883b0a91SJoerg Roedel case SVM_EXIT_NMI: 978883b0a91SJoerg Roedel case SVM_EXIT_NPF: 979883b0a91SJoerg Roedel return NESTED_EXIT_HOST; 9807c86663bSPaolo Bonzini case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: { 9817c86663bSPaolo Bonzini u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE); 9827c86663bSPaolo Bonzini 9837c86663bSPaolo Bonzini if (get_host_vmcb(svm)->control.intercept_exceptions & excp_bits) 9847c86663bSPaolo Bonzini return NESTED_EXIT_HOST; 9857c86663bSPaolo Bonzini else if (exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR && 98668fd66f1SVitaly Kuznetsov svm->vcpu.arch.apf.host_apf_flags) 987a3535be7SPaolo Bonzini /* Trap async PF even if not shadowing */ 988883b0a91SJoerg Roedel return NESTED_EXIT_HOST; 989883b0a91SJoerg Roedel break; 9907c86663bSPaolo Bonzini } 991883b0a91SJoerg Roedel default: 992883b0a91SJoerg Roedel break; 993883b0a91SJoerg Roedel } 994883b0a91SJoerg Roedel 995883b0a91SJoerg Roedel return NESTED_EXIT_CONTINUE; 996883b0a91SJoerg Roedel } 99733b22172SPaolo Bonzini 998cc440cdaSPaolo Bonzini static int svm_get_nested_state(struct kvm_vcpu *vcpu, 999cc440cdaSPaolo Bonzini struct kvm_nested_state __user *user_kvm_nested_state, 1000cc440cdaSPaolo Bonzini u32 user_data_size) 1001cc440cdaSPaolo Bonzini { 1002cc440cdaSPaolo Bonzini struct vcpu_svm *svm; 1003cc440cdaSPaolo Bonzini struct kvm_nested_state kvm_state = { 1004cc440cdaSPaolo Bonzini .flags = 0, 1005cc440cdaSPaolo Bonzini .format = KVM_STATE_NESTED_FORMAT_SVM, 1006cc440cdaSPaolo Bonzini .size = sizeof(kvm_state), 1007cc440cdaSPaolo Bonzini }; 1008cc440cdaSPaolo Bonzini struct vmcb __user *user_vmcb = (struct vmcb __user *) 1009cc440cdaSPaolo Bonzini &user_kvm_nested_state->data.svm[0]; 1010cc440cdaSPaolo Bonzini 1011cc440cdaSPaolo Bonzini if (!vcpu) 1012cc440cdaSPaolo Bonzini return kvm_state.size + KVM_STATE_NESTED_SVM_VMCB_SIZE; 1013cc440cdaSPaolo Bonzini 1014cc440cdaSPaolo Bonzini svm = to_svm(vcpu); 1015cc440cdaSPaolo Bonzini 1016cc440cdaSPaolo Bonzini if (user_data_size < kvm_state.size) 1017cc440cdaSPaolo Bonzini goto out; 1018cc440cdaSPaolo Bonzini 1019cc440cdaSPaolo Bonzini /* First fill in the header and copy it out. */ 1020cc440cdaSPaolo Bonzini if (is_guest_mode(vcpu)) { 1021cc440cdaSPaolo Bonzini kvm_state.hdr.svm.vmcb_pa = svm->nested.vmcb; 1022cc440cdaSPaolo Bonzini kvm_state.size += KVM_STATE_NESTED_SVM_VMCB_SIZE; 1023cc440cdaSPaolo Bonzini kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE; 1024cc440cdaSPaolo Bonzini 1025cc440cdaSPaolo Bonzini if (svm->nested.nested_run_pending) 1026cc440cdaSPaolo Bonzini kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING; 1027cc440cdaSPaolo Bonzini } 1028cc440cdaSPaolo Bonzini 1029cc440cdaSPaolo Bonzini if (gif_set(svm)) 1030cc440cdaSPaolo Bonzini kvm_state.flags |= KVM_STATE_NESTED_GIF_SET; 1031cc440cdaSPaolo Bonzini 1032cc440cdaSPaolo Bonzini if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state))) 1033cc440cdaSPaolo Bonzini return -EFAULT; 1034cc440cdaSPaolo Bonzini 1035cc440cdaSPaolo Bonzini if (!is_guest_mode(vcpu)) 1036cc440cdaSPaolo Bonzini goto out; 1037cc440cdaSPaolo Bonzini 1038cc440cdaSPaolo Bonzini /* 1039cc440cdaSPaolo Bonzini * Copy over the full size of the VMCB rather than just the size 1040cc440cdaSPaolo Bonzini * of the structs. 1041cc440cdaSPaolo Bonzini */ 1042cc440cdaSPaolo Bonzini if (clear_user(user_vmcb, KVM_STATE_NESTED_SVM_VMCB_SIZE)) 1043cc440cdaSPaolo Bonzini return -EFAULT; 1044cc440cdaSPaolo Bonzini if (copy_to_user(&user_vmcb->control, &svm->nested.ctl, 1045cc440cdaSPaolo Bonzini sizeof(user_vmcb->control))) 1046cc440cdaSPaolo Bonzini return -EFAULT; 1047cc440cdaSPaolo Bonzini if (copy_to_user(&user_vmcb->save, &svm->nested.hsave->save, 1048cc440cdaSPaolo Bonzini sizeof(user_vmcb->save))) 1049cc440cdaSPaolo Bonzini return -EFAULT; 1050cc440cdaSPaolo Bonzini 1051cc440cdaSPaolo Bonzini out: 1052cc440cdaSPaolo Bonzini return kvm_state.size; 1053cc440cdaSPaolo Bonzini } 1054cc440cdaSPaolo Bonzini 1055cc440cdaSPaolo Bonzini static int svm_set_nested_state(struct kvm_vcpu *vcpu, 1056cc440cdaSPaolo Bonzini struct kvm_nested_state __user *user_kvm_nested_state, 1057cc440cdaSPaolo Bonzini struct kvm_nested_state *kvm_state) 1058cc440cdaSPaolo Bonzini { 1059cc440cdaSPaolo Bonzini struct vcpu_svm *svm = to_svm(vcpu); 1060cc440cdaSPaolo Bonzini struct vmcb *hsave = svm->nested.hsave; 1061cc440cdaSPaolo Bonzini struct vmcb __user *user_vmcb = (struct vmcb __user *) 1062cc440cdaSPaolo Bonzini &user_kvm_nested_state->data.svm[0]; 1063*6ccbd29aSJoerg Roedel struct vmcb_control_area *ctl; 1064*6ccbd29aSJoerg Roedel struct vmcb_save_area *save; 1065*6ccbd29aSJoerg Roedel int ret; 1066cc440cdaSPaolo Bonzini u32 cr0; 1067cc440cdaSPaolo Bonzini 1068*6ccbd29aSJoerg Roedel BUILD_BUG_ON(sizeof(struct vmcb_control_area) + sizeof(struct vmcb_save_area) > 1069*6ccbd29aSJoerg Roedel KVM_STATE_NESTED_SVM_VMCB_SIZE); 1070*6ccbd29aSJoerg Roedel 1071cc440cdaSPaolo Bonzini if (kvm_state->format != KVM_STATE_NESTED_FORMAT_SVM) 1072cc440cdaSPaolo Bonzini return -EINVAL; 1073cc440cdaSPaolo Bonzini 1074cc440cdaSPaolo Bonzini if (kvm_state->flags & ~(KVM_STATE_NESTED_GUEST_MODE | 1075cc440cdaSPaolo Bonzini KVM_STATE_NESTED_RUN_PENDING | 1076cc440cdaSPaolo Bonzini KVM_STATE_NESTED_GIF_SET)) 1077cc440cdaSPaolo Bonzini return -EINVAL; 1078cc440cdaSPaolo Bonzini 1079cc440cdaSPaolo Bonzini /* 1080cc440cdaSPaolo Bonzini * If in guest mode, vcpu->arch.efer actually refers to the L2 guest's 1081cc440cdaSPaolo Bonzini * EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed. 1082cc440cdaSPaolo Bonzini */ 1083cc440cdaSPaolo Bonzini if (!(vcpu->arch.efer & EFER_SVME)) { 1084cc440cdaSPaolo Bonzini /* GIF=1 and no guest mode are required if SVME=0. */ 1085cc440cdaSPaolo Bonzini if (kvm_state->flags != KVM_STATE_NESTED_GIF_SET) 1086cc440cdaSPaolo Bonzini return -EINVAL; 1087cc440cdaSPaolo Bonzini } 1088cc440cdaSPaolo Bonzini 1089cc440cdaSPaolo Bonzini /* SMM temporarily disables SVM, so we cannot be in guest mode. */ 1090cc440cdaSPaolo Bonzini if (is_smm(vcpu) && (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) 1091cc440cdaSPaolo Bonzini return -EINVAL; 1092cc440cdaSPaolo Bonzini 1093cc440cdaSPaolo Bonzini if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) { 1094cc440cdaSPaolo Bonzini svm_leave_nested(svm); 1095cc440cdaSPaolo Bonzini goto out_set_gif; 1096cc440cdaSPaolo Bonzini } 1097cc440cdaSPaolo Bonzini 1098cc440cdaSPaolo Bonzini if (!page_address_valid(vcpu, kvm_state->hdr.svm.vmcb_pa)) 1099cc440cdaSPaolo Bonzini return -EINVAL; 1100cc440cdaSPaolo Bonzini if (kvm_state->size < sizeof(*kvm_state) + KVM_STATE_NESTED_SVM_VMCB_SIZE) 1101cc440cdaSPaolo Bonzini return -EINVAL; 1102cc440cdaSPaolo Bonzini 1103*6ccbd29aSJoerg Roedel ret = -ENOMEM; 1104*6ccbd29aSJoerg Roedel ctl = kzalloc(sizeof(*ctl), GFP_KERNEL); 1105*6ccbd29aSJoerg Roedel save = kzalloc(sizeof(*save), GFP_KERNEL); 1106*6ccbd29aSJoerg Roedel if (!ctl || !save) 1107*6ccbd29aSJoerg Roedel goto out_free; 1108*6ccbd29aSJoerg Roedel 1109*6ccbd29aSJoerg Roedel ret = -EFAULT; 1110*6ccbd29aSJoerg Roedel if (copy_from_user(ctl, &user_vmcb->control, sizeof(*ctl))) 1111*6ccbd29aSJoerg Roedel goto out_free; 1112*6ccbd29aSJoerg Roedel if (copy_from_user(save, &user_vmcb->save, sizeof(*save))) 1113*6ccbd29aSJoerg Roedel goto out_free; 1114*6ccbd29aSJoerg Roedel 1115*6ccbd29aSJoerg Roedel ret = -EINVAL; 1116*6ccbd29aSJoerg Roedel if (!nested_vmcb_check_controls(ctl)) 1117*6ccbd29aSJoerg Roedel goto out_free; 1118cc440cdaSPaolo Bonzini 1119cc440cdaSPaolo Bonzini /* 1120cc440cdaSPaolo Bonzini * Processor state contains L2 state. Check that it is 1121cc440cdaSPaolo Bonzini * valid for guest mode (see nested_vmcb_checks). 1122cc440cdaSPaolo Bonzini */ 1123cc440cdaSPaolo Bonzini cr0 = kvm_read_cr0(vcpu); 1124cc440cdaSPaolo Bonzini if (((cr0 & X86_CR0_CD) == 0) && (cr0 & X86_CR0_NW)) 1125*6ccbd29aSJoerg Roedel goto out_free; 1126cc440cdaSPaolo Bonzini 1127cc440cdaSPaolo Bonzini /* 1128cc440cdaSPaolo Bonzini * Validate host state saved from before VMRUN (see 1129cc440cdaSPaolo Bonzini * nested_svm_check_permissions). 1130cc440cdaSPaolo Bonzini * TODO: validate reserved bits for all saved state. 1131cc440cdaSPaolo Bonzini */ 1132*6ccbd29aSJoerg Roedel if (!(save->cr0 & X86_CR0_PG)) 1133*6ccbd29aSJoerg Roedel goto out_free; 1134cc440cdaSPaolo Bonzini 1135cc440cdaSPaolo Bonzini /* 1136cc440cdaSPaolo Bonzini * All checks done, we can enter guest mode. L1 control fields 1137cc440cdaSPaolo Bonzini * come from the nested save state. Guest state is already 1138cc440cdaSPaolo Bonzini * in the registers, the save area of the nested state instead 1139cc440cdaSPaolo Bonzini * contains saved L1 state. 1140cc440cdaSPaolo Bonzini */ 1141cc440cdaSPaolo Bonzini copy_vmcb_control_area(&hsave->control, &svm->vmcb->control); 1142*6ccbd29aSJoerg Roedel hsave->save = *save; 1143cc440cdaSPaolo Bonzini 1144cc440cdaSPaolo Bonzini svm->nested.vmcb = kvm_state->hdr.svm.vmcb_pa; 1145*6ccbd29aSJoerg Roedel load_nested_vmcb_control(svm, ctl); 1146cc440cdaSPaolo Bonzini nested_prepare_vmcb_control(svm); 1147cc440cdaSPaolo Bonzini 1148cc440cdaSPaolo Bonzini out_set_gif: 1149cc440cdaSPaolo Bonzini svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET)); 1150*6ccbd29aSJoerg Roedel 1151*6ccbd29aSJoerg Roedel ret = 0; 1152*6ccbd29aSJoerg Roedel out_free: 1153*6ccbd29aSJoerg Roedel kfree(save); 1154*6ccbd29aSJoerg Roedel kfree(ctl); 1155*6ccbd29aSJoerg Roedel 1156*6ccbd29aSJoerg Roedel return ret; 1157cc440cdaSPaolo Bonzini } 1158cc440cdaSPaolo Bonzini 115933b22172SPaolo Bonzini struct kvm_x86_nested_ops svm_nested_ops = { 116033b22172SPaolo Bonzini .check_events = svm_check_nested_events, 1161cc440cdaSPaolo Bonzini .get_state = svm_get_nested_state, 1162cc440cdaSPaolo Bonzini .set_state = svm_set_nested_state, 116333b22172SPaolo Bonzini }; 1164