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 /* 52883b0a91SJoerg Roedel * The present bit is always zero for page structure faults on real 53883b0a91SJoerg Roedel * hardware. 54883b0a91SJoerg Roedel */ 55883b0a91SJoerg Roedel if (svm->vmcb->control.exit_info_1 & (2ULL << 32)) 56883b0a91SJoerg Roedel svm->vmcb->control.exit_info_1 &= ~1; 57883b0a91SJoerg Roedel 58883b0a91SJoerg Roedel nested_svm_vmexit(svm); 59883b0a91SJoerg Roedel } 60883b0a91SJoerg Roedel 61883b0a91SJoerg Roedel static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index) 62883b0a91SJoerg Roedel { 63883b0a91SJoerg Roedel struct vcpu_svm *svm = to_svm(vcpu); 64e670bf68SPaolo Bonzini u64 cr3 = svm->nested.ctl.nested_cr3; 65883b0a91SJoerg Roedel u64 pdpte; 66883b0a91SJoerg Roedel int ret; 67883b0a91SJoerg Roedel 68883b0a91SJoerg Roedel ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(__sme_clr(cr3)), &pdpte, 69883b0a91SJoerg Roedel offset_in_page(cr3) + index * 8, 8); 70883b0a91SJoerg Roedel if (ret) 71883b0a91SJoerg Roedel return 0; 72883b0a91SJoerg Roedel return pdpte; 73883b0a91SJoerg Roedel } 74883b0a91SJoerg Roedel 75883b0a91SJoerg Roedel static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu) 76883b0a91SJoerg Roedel { 77883b0a91SJoerg Roedel struct vcpu_svm *svm = to_svm(vcpu); 78883b0a91SJoerg Roedel 79e670bf68SPaolo Bonzini return svm->nested.ctl.nested_cr3; 80883b0a91SJoerg Roedel } 81883b0a91SJoerg Roedel 82883b0a91SJoerg Roedel static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu) 83883b0a91SJoerg Roedel { 84929d1cfaSPaolo Bonzini struct vcpu_svm *svm = to_svm(vcpu); 85929d1cfaSPaolo Bonzini struct vmcb *hsave = svm->nested.hsave; 86929d1cfaSPaolo Bonzini 87883b0a91SJoerg Roedel WARN_ON(mmu_is_nested(vcpu)); 88883b0a91SJoerg Roedel 89883b0a91SJoerg Roedel vcpu->arch.mmu = &vcpu->arch.guest_mmu; 90929d1cfaSPaolo Bonzini kvm_init_shadow_mmu(vcpu, X86_CR0_PG, hsave->save.cr4, hsave->save.efer); 91883b0a91SJoerg Roedel vcpu->arch.mmu->get_guest_pgd = nested_svm_get_tdp_cr3; 92883b0a91SJoerg Roedel vcpu->arch.mmu->get_pdptr = nested_svm_get_tdp_pdptr; 93883b0a91SJoerg Roedel vcpu->arch.mmu->inject_page_fault = nested_svm_inject_npf_exit; 94e93fd3b3SSean Christopherson vcpu->arch.mmu->shadow_root_level = vcpu->arch.tdp_level; 95883b0a91SJoerg Roedel reset_shadow_zero_bits_mask(vcpu, vcpu->arch.mmu); 96883b0a91SJoerg Roedel vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; 97883b0a91SJoerg Roedel } 98883b0a91SJoerg Roedel 99883b0a91SJoerg Roedel static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu) 100883b0a91SJoerg Roedel { 101883b0a91SJoerg Roedel vcpu->arch.mmu = &vcpu->arch.root_mmu; 102883b0a91SJoerg Roedel vcpu->arch.walk_mmu = &vcpu->arch.root_mmu; 103883b0a91SJoerg Roedel } 104883b0a91SJoerg Roedel 105883b0a91SJoerg Roedel void recalc_intercepts(struct vcpu_svm *svm) 106883b0a91SJoerg Roedel { 107e670bf68SPaolo Bonzini struct vmcb_control_area *c, *h, *g; 108883b0a91SJoerg Roedel 109883b0a91SJoerg Roedel mark_dirty(svm->vmcb, VMCB_INTERCEPTS); 110883b0a91SJoerg Roedel 111883b0a91SJoerg Roedel if (!is_guest_mode(&svm->vcpu)) 112883b0a91SJoerg Roedel return; 113883b0a91SJoerg Roedel 114883b0a91SJoerg Roedel c = &svm->vmcb->control; 115883b0a91SJoerg Roedel h = &svm->nested.hsave->control; 116e670bf68SPaolo Bonzini g = &svm->nested.ctl; 117883b0a91SJoerg Roedel 1187c86663bSPaolo Bonzini svm->nested.host_intercept_exceptions = h->intercept_exceptions; 1197c86663bSPaolo Bonzini 120883b0a91SJoerg Roedel c->intercept_cr = h->intercept_cr; 121883b0a91SJoerg Roedel c->intercept_dr = h->intercept_dr; 122883b0a91SJoerg Roedel c->intercept_exceptions = h->intercept_exceptions; 123883b0a91SJoerg Roedel c->intercept = h->intercept; 124883b0a91SJoerg Roedel 125e9fd761aSPaolo Bonzini if (g->int_ctl & V_INTR_MASKING_MASK) { 126883b0a91SJoerg Roedel /* We only want the cr8 intercept bits of L1 */ 127883b0a91SJoerg Roedel c->intercept_cr &= ~(1U << INTERCEPT_CR8_READ); 128883b0a91SJoerg Roedel c->intercept_cr &= ~(1U << INTERCEPT_CR8_WRITE); 129883b0a91SJoerg Roedel 130883b0a91SJoerg Roedel /* 131883b0a91SJoerg Roedel * Once running L2 with HF_VINTR_MASK, EFLAGS.IF does not 132883b0a91SJoerg Roedel * affect any interrupt we may want to inject; therefore, 133883b0a91SJoerg Roedel * interrupt window vmexits are irrelevant to L0. 134883b0a91SJoerg Roedel */ 135883b0a91SJoerg Roedel c->intercept &= ~(1ULL << INTERCEPT_VINTR); 136883b0a91SJoerg Roedel } 137883b0a91SJoerg Roedel 138883b0a91SJoerg Roedel /* We don't want to see VMMCALLs from a nested guest */ 139883b0a91SJoerg Roedel c->intercept &= ~(1ULL << INTERCEPT_VMMCALL); 140883b0a91SJoerg Roedel 141883b0a91SJoerg Roedel c->intercept_cr |= g->intercept_cr; 142883b0a91SJoerg Roedel c->intercept_dr |= g->intercept_dr; 143883b0a91SJoerg Roedel c->intercept_exceptions |= g->intercept_exceptions; 144883b0a91SJoerg Roedel c->intercept |= g->intercept; 145883b0a91SJoerg Roedel } 146883b0a91SJoerg Roedel 1472f675917SPaolo Bonzini static void copy_vmcb_control_area(struct vmcb_control_area *dst, 1482f675917SPaolo Bonzini struct vmcb_control_area *from) 149883b0a91SJoerg Roedel { 150883b0a91SJoerg Roedel dst->intercept_cr = from->intercept_cr; 151883b0a91SJoerg Roedel dst->intercept_dr = from->intercept_dr; 152883b0a91SJoerg Roedel dst->intercept_exceptions = from->intercept_exceptions; 153883b0a91SJoerg Roedel dst->intercept = from->intercept; 154883b0a91SJoerg Roedel dst->iopm_base_pa = from->iopm_base_pa; 155883b0a91SJoerg Roedel dst->msrpm_base_pa = from->msrpm_base_pa; 156883b0a91SJoerg Roedel dst->tsc_offset = from->tsc_offset; 1576c0238c4SPaolo Bonzini /* asid not copied, it is handled manually for svm->vmcb. */ 158883b0a91SJoerg Roedel dst->tlb_ctl = from->tlb_ctl; 159883b0a91SJoerg Roedel dst->int_ctl = from->int_ctl; 160883b0a91SJoerg Roedel dst->int_vector = from->int_vector; 161883b0a91SJoerg Roedel dst->int_state = from->int_state; 162883b0a91SJoerg Roedel dst->exit_code = from->exit_code; 163883b0a91SJoerg Roedel dst->exit_code_hi = from->exit_code_hi; 164883b0a91SJoerg Roedel dst->exit_info_1 = from->exit_info_1; 165883b0a91SJoerg Roedel dst->exit_info_2 = from->exit_info_2; 166883b0a91SJoerg Roedel dst->exit_int_info = from->exit_int_info; 167883b0a91SJoerg Roedel dst->exit_int_info_err = from->exit_int_info_err; 168883b0a91SJoerg Roedel dst->nested_ctl = from->nested_ctl; 169883b0a91SJoerg Roedel dst->event_inj = from->event_inj; 170883b0a91SJoerg Roedel dst->event_inj_err = from->event_inj_err; 171883b0a91SJoerg Roedel dst->nested_cr3 = from->nested_cr3; 172883b0a91SJoerg Roedel dst->virt_ext = from->virt_ext; 173883b0a91SJoerg Roedel dst->pause_filter_count = from->pause_filter_count; 174883b0a91SJoerg Roedel dst->pause_filter_thresh = from->pause_filter_thresh; 175883b0a91SJoerg Roedel } 176883b0a91SJoerg Roedel 177883b0a91SJoerg Roedel static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm) 178883b0a91SJoerg Roedel { 179883b0a91SJoerg Roedel /* 180883b0a91SJoerg Roedel * This function merges the msr permission bitmaps of kvm and the 181883b0a91SJoerg Roedel * nested vmcb. It is optimized in that it only merges the parts where 182883b0a91SJoerg Roedel * the kvm msr permission bitmap may contain zero bits 183883b0a91SJoerg Roedel */ 184883b0a91SJoerg Roedel int i; 185883b0a91SJoerg Roedel 186e670bf68SPaolo Bonzini if (!(svm->nested.ctl.intercept & (1ULL << INTERCEPT_MSR_PROT))) 187883b0a91SJoerg Roedel return true; 188883b0a91SJoerg Roedel 189883b0a91SJoerg Roedel for (i = 0; i < MSRPM_OFFSETS; i++) { 190883b0a91SJoerg Roedel u32 value, p; 191883b0a91SJoerg Roedel u64 offset; 192883b0a91SJoerg Roedel 193883b0a91SJoerg Roedel if (msrpm_offsets[i] == 0xffffffff) 194883b0a91SJoerg Roedel break; 195883b0a91SJoerg Roedel 196883b0a91SJoerg Roedel p = msrpm_offsets[i]; 197e670bf68SPaolo Bonzini offset = svm->nested.ctl.msrpm_base_pa + (p * 4); 198883b0a91SJoerg Roedel 199883b0a91SJoerg Roedel if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4)) 200883b0a91SJoerg Roedel return false; 201883b0a91SJoerg Roedel 202883b0a91SJoerg Roedel svm->nested.msrpm[p] = svm->msrpm[p] | value; 203883b0a91SJoerg Roedel } 204883b0a91SJoerg Roedel 205883b0a91SJoerg Roedel svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm)); 206883b0a91SJoerg Roedel 207883b0a91SJoerg Roedel return true; 208883b0a91SJoerg Roedel } 209883b0a91SJoerg Roedel 210ca46d739SPaolo Bonzini static bool nested_vmcb_check_controls(struct vmcb_control_area *control) 211ca46d739SPaolo Bonzini { 212ca46d739SPaolo Bonzini if ((control->intercept & (1ULL << INTERCEPT_VMRUN)) == 0) 213ca46d739SPaolo Bonzini return false; 214ca46d739SPaolo Bonzini 215ca46d739SPaolo Bonzini if (control->asid == 0) 216ca46d739SPaolo Bonzini return false; 217ca46d739SPaolo Bonzini 218ca46d739SPaolo Bonzini if ((control->nested_ctl & SVM_NESTED_CTL_NP_ENABLE) && 219ca46d739SPaolo Bonzini !npt_enabled) 220ca46d739SPaolo Bonzini return false; 221ca46d739SPaolo Bonzini 222ca46d739SPaolo Bonzini return true; 223ca46d739SPaolo Bonzini } 224ca46d739SPaolo Bonzini 225883b0a91SJoerg Roedel static bool nested_vmcb_checks(struct vmcb *vmcb) 226883b0a91SJoerg Roedel { 227883b0a91SJoerg Roedel if ((vmcb->save.efer & EFER_SVME) == 0) 228883b0a91SJoerg Roedel return false; 229883b0a91SJoerg Roedel 2304f233371SKrish Sadhukhan if (((vmcb->save.cr0 & X86_CR0_CD) == 0) && 2314f233371SKrish Sadhukhan (vmcb->save.cr0 & X86_CR0_NW)) 2324f233371SKrish Sadhukhan return false; 2334f233371SKrish Sadhukhan 234*1aef8161SKrish Sadhukhan if (!kvm_dr6_valid(vmcb->save.dr6) || !kvm_dr7_valid(vmcb->save.dr7)) 235*1aef8161SKrish Sadhukhan return false; 236*1aef8161SKrish Sadhukhan 237ca46d739SPaolo Bonzini return nested_vmcb_check_controls(&vmcb->control); 238883b0a91SJoerg Roedel } 239883b0a91SJoerg Roedel 2403e06f016SPaolo Bonzini static void load_nested_vmcb_control(struct vcpu_svm *svm, 2413e06f016SPaolo Bonzini struct vmcb_control_area *control) 2423e06f016SPaolo Bonzini { 243e670bf68SPaolo Bonzini copy_vmcb_control_area(&svm->nested.ctl, control); 2443e06f016SPaolo Bonzini 245cc440cdaSPaolo Bonzini /* Copy it here because nested_svm_check_controls will check it. */ 246cc440cdaSPaolo Bonzini svm->nested.ctl.asid = control->asid; 247e670bf68SPaolo Bonzini svm->nested.ctl.msrpm_base_pa &= ~0x0fffULL; 248e670bf68SPaolo Bonzini svm->nested.ctl.iopm_base_pa &= ~0x0fffULL; 2493e06f016SPaolo Bonzini } 2503e06f016SPaolo Bonzini 2512d8a42beSPaolo Bonzini /* 2522d8a42beSPaolo Bonzini * Synchronize fields that are written by the processor, so that 2532d8a42beSPaolo Bonzini * they can be copied back into the nested_vmcb. 2542d8a42beSPaolo Bonzini */ 2552d8a42beSPaolo Bonzini void sync_nested_vmcb_control(struct vcpu_svm *svm) 2562d8a42beSPaolo Bonzini { 2572d8a42beSPaolo Bonzini u32 mask; 2582d8a42beSPaolo Bonzini svm->nested.ctl.event_inj = svm->vmcb->control.event_inj; 2592d8a42beSPaolo Bonzini svm->nested.ctl.event_inj_err = svm->vmcb->control.event_inj_err; 2602d8a42beSPaolo Bonzini 2612d8a42beSPaolo Bonzini /* Only a few fields of int_ctl are written by the processor. */ 2622d8a42beSPaolo Bonzini mask = V_IRQ_MASK | V_TPR_MASK; 2632d8a42beSPaolo Bonzini if (!(svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK) && 264fb7333dfSPaolo Bonzini is_intercept(svm, INTERCEPT_VINTR)) { 2652d8a42beSPaolo Bonzini /* 2662d8a42beSPaolo Bonzini * In order to request an interrupt window, L0 is usurping 2672d8a42beSPaolo Bonzini * svm->vmcb->control.int_ctl and possibly setting V_IRQ 2682d8a42beSPaolo Bonzini * even if it was clear in L1's VMCB. Restoring it would be 2692d8a42beSPaolo Bonzini * wrong. However, in this case V_IRQ will remain true until 2702d8a42beSPaolo Bonzini * interrupt_window_interception calls svm_clear_vintr and 2712d8a42beSPaolo Bonzini * restores int_ctl. We can just leave it aside. 2722d8a42beSPaolo Bonzini */ 2732d8a42beSPaolo Bonzini mask &= ~V_IRQ_MASK; 2742d8a42beSPaolo Bonzini } 2752d8a42beSPaolo Bonzini svm->nested.ctl.int_ctl &= ~mask; 2762d8a42beSPaolo Bonzini svm->nested.ctl.int_ctl |= svm->vmcb->control.int_ctl & mask; 2772d8a42beSPaolo Bonzini } 2782d8a42beSPaolo Bonzini 27936e2e983SPaolo Bonzini /* 28036e2e983SPaolo Bonzini * Transfer any event that L0 or L1 wanted to inject into L2 to 28136e2e983SPaolo Bonzini * EXIT_INT_INFO. 28236e2e983SPaolo Bonzini */ 28336e2e983SPaolo Bonzini static void nested_vmcb_save_pending_event(struct vcpu_svm *svm, 28436e2e983SPaolo Bonzini struct vmcb *nested_vmcb) 28536e2e983SPaolo Bonzini { 28636e2e983SPaolo Bonzini struct kvm_vcpu *vcpu = &svm->vcpu; 28736e2e983SPaolo Bonzini u32 exit_int_info = 0; 28836e2e983SPaolo Bonzini unsigned int nr; 28936e2e983SPaolo Bonzini 29036e2e983SPaolo Bonzini if (vcpu->arch.exception.injected) { 29136e2e983SPaolo Bonzini nr = vcpu->arch.exception.nr; 29236e2e983SPaolo Bonzini exit_int_info = nr | SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_EXEPT; 29336e2e983SPaolo Bonzini 29436e2e983SPaolo Bonzini if (vcpu->arch.exception.has_error_code) { 29536e2e983SPaolo Bonzini exit_int_info |= SVM_EVTINJ_VALID_ERR; 29636e2e983SPaolo Bonzini nested_vmcb->control.exit_int_info_err = 29736e2e983SPaolo Bonzini vcpu->arch.exception.error_code; 29836e2e983SPaolo Bonzini } 29936e2e983SPaolo Bonzini 30036e2e983SPaolo Bonzini } else if (vcpu->arch.nmi_injected) { 30136e2e983SPaolo Bonzini exit_int_info = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI; 30236e2e983SPaolo Bonzini 30336e2e983SPaolo Bonzini } else if (vcpu->arch.interrupt.injected) { 30436e2e983SPaolo Bonzini nr = vcpu->arch.interrupt.nr; 30536e2e983SPaolo Bonzini exit_int_info = nr | SVM_EVTINJ_VALID; 30636e2e983SPaolo Bonzini 30736e2e983SPaolo Bonzini if (vcpu->arch.interrupt.soft) 30836e2e983SPaolo Bonzini exit_int_info |= SVM_EVTINJ_TYPE_SOFT; 30936e2e983SPaolo Bonzini else 31036e2e983SPaolo Bonzini exit_int_info |= SVM_EVTINJ_TYPE_INTR; 31136e2e983SPaolo Bonzini } 31236e2e983SPaolo Bonzini 31336e2e983SPaolo Bonzini nested_vmcb->control.exit_int_info = exit_int_info; 31436e2e983SPaolo Bonzini } 31536e2e983SPaolo Bonzini 316f241d711SPaolo Bonzini static void nested_prepare_vmcb_save(struct vcpu_svm *svm, struct vmcb *nested_vmcb) 317883b0a91SJoerg Roedel { 318883b0a91SJoerg Roedel /* Load the nested guest state */ 319883b0a91SJoerg Roedel svm->vmcb->save.es = nested_vmcb->save.es; 320883b0a91SJoerg Roedel svm->vmcb->save.cs = nested_vmcb->save.cs; 321883b0a91SJoerg Roedel svm->vmcb->save.ss = nested_vmcb->save.ss; 322883b0a91SJoerg Roedel svm->vmcb->save.ds = nested_vmcb->save.ds; 323883b0a91SJoerg Roedel svm->vmcb->save.gdtr = nested_vmcb->save.gdtr; 324883b0a91SJoerg Roedel svm->vmcb->save.idtr = nested_vmcb->save.idtr; 325883b0a91SJoerg Roedel kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags); 326883b0a91SJoerg Roedel svm_set_efer(&svm->vcpu, nested_vmcb->save.efer); 327883b0a91SJoerg Roedel svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0); 328883b0a91SJoerg Roedel svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4); 329883b0a91SJoerg Roedel (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3); 330883b0a91SJoerg Roedel 331883b0a91SJoerg Roedel svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2; 332883b0a91SJoerg Roedel kvm_rax_write(&svm->vcpu, nested_vmcb->save.rax); 333883b0a91SJoerg Roedel kvm_rsp_write(&svm->vcpu, nested_vmcb->save.rsp); 334883b0a91SJoerg Roedel kvm_rip_write(&svm->vcpu, nested_vmcb->save.rip); 335883b0a91SJoerg Roedel 336883b0a91SJoerg Roedel /* In case we don't even reach vcpu_run, the fields are not updated */ 337883b0a91SJoerg Roedel svm->vmcb->save.rax = nested_vmcb->save.rax; 338883b0a91SJoerg Roedel svm->vmcb->save.rsp = nested_vmcb->save.rsp; 339883b0a91SJoerg Roedel svm->vmcb->save.rip = nested_vmcb->save.rip; 340883b0a91SJoerg Roedel svm->vmcb->save.dr7 = nested_vmcb->save.dr7; 3415679b803SPaolo Bonzini svm->vcpu.arch.dr6 = nested_vmcb->save.dr6; 342883b0a91SJoerg Roedel svm->vmcb->save.cpl = nested_vmcb->save.cpl; 343f241d711SPaolo Bonzini } 344883b0a91SJoerg Roedel 345e670bf68SPaolo Bonzini static void nested_prepare_vmcb_control(struct vcpu_svm *svm) 346f241d711SPaolo Bonzini { 34791b7130cSPaolo Bonzini const u32 mask = V_INTR_MASKING_MASK | V_GIF_ENABLE_MASK | V_GIF_MASK; 348e670bf68SPaolo Bonzini if (svm->nested.ctl.nested_ctl & SVM_NESTED_CTL_NP_ENABLE) 34969cb8774SPaolo Bonzini nested_svm_init_mmu_context(&svm->vcpu); 35069cb8774SPaolo Bonzini 35169cb8774SPaolo Bonzini /* Guest paging mode is active - reset mmu */ 35269cb8774SPaolo Bonzini kvm_mmu_reset_context(&svm->vcpu); 35369cb8774SPaolo Bonzini 354f55ac304SSean Christopherson svm_flush_tlb(&svm->vcpu); 355883b0a91SJoerg Roedel 35618fc6c55SPaolo Bonzini svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset = 357e670bf68SPaolo Bonzini svm->vcpu.arch.l1_tsc_offset + svm->nested.ctl.tsc_offset; 358883b0a91SJoerg Roedel 35991b7130cSPaolo Bonzini svm->vmcb->control.int_ctl = 36091b7130cSPaolo Bonzini (svm->nested.ctl.int_ctl & ~mask) | 36191b7130cSPaolo Bonzini (svm->nested.hsave->control.int_ctl & mask); 36291b7130cSPaolo Bonzini 363e670bf68SPaolo Bonzini svm->vmcb->control.virt_ext = svm->nested.ctl.virt_ext; 364e670bf68SPaolo Bonzini svm->vmcb->control.int_vector = svm->nested.ctl.int_vector; 365e670bf68SPaolo Bonzini svm->vmcb->control.int_state = svm->nested.ctl.int_state; 366e670bf68SPaolo Bonzini svm->vmcb->control.event_inj = svm->nested.ctl.event_inj; 367e670bf68SPaolo Bonzini svm->vmcb->control.event_inj_err = svm->nested.ctl.event_inj_err; 368883b0a91SJoerg Roedel 369e670bf68SPaolo Bonzini svm->vmcb->control.pause_filter_count = svm->nested.ctl.pause_filter_count; 370e670bf68SPaolo Bonzini svm->vmcb->control.pause_filter_thresh = svm->nested.ctl.pause_filter_thresh; 371883b0a91SJoerg Roedel 372883b0a91SJoerg Roedel /* Enter Guest-Mode */ 373883b0a91SJoerg Roedel enter_guest_mode(&svm->vcpu); 374883b0a91SJoerg Roedel 375883b0a91SJoerg Roedel /* 376883b0a91SJoerg Roedel * Merge guest and host intercepts - must be called with vcpu in 377883b0a91SJoerg Roedel * guest-mode to take affect here 378883b0a91SJoerg Roedel */ 379883b0a91SJoerg Roedel recalc_intercepts(svm); 380883b0a91SJoerg Roedel 381f241d711SPaolo Bonzini mark_all_dirty(svm->vmcb); 382f241d711SPaolo Bonzini } 383f241d711SPaolo Bonzini 384f241d711SPaolo Bonzini void enter_svm_guest_mode(struct vcpu_svm *svm, u64 vmcb_gpa, 385f241d711SPaolo Bonzini struct vmcb *nested_vmcb) 386f241d711SPaolo Bonzini { 387f241d711SPaolo Bonzini svm->nested.vmcb = vmcb_gpa; 388f241d711SPaolo Bonzini load_nested_vmcb_control(svm, &nested_vmcb->control); 389f241d711SPaolo Bonzini nested_prepare_vmcb_save(svm, nested_vmcb); 390e670bf68SPaolo Bonzini nested_prepare_vmcb_control(svm); 391f241d711SPaolo Bonzini 392ffdf7f9eSPaolo Bonzini svm_set_gif(svm, true); 393883b0a91SJoerg Roedel } 394883b0a91SJoerg Roedel 395883b0a91SJoerg Roedel int nested_svm_vmrun(struct vcpu_svm *svm) 396883b0a91SJoerg Roedel { 397883b0a91SJoerg Roedel int ret; 398883b0a91SJoerg Roedel struct vmcb *nested_vmcb; 399883b0a91SJoerg Roedel struct vmcb *hsave = svm->nested.hsave; 400883b0a91SJoerg Roedel struct vmcb *vmcb = svm->vmcb; 401883b0a91SJoerg Roedel struct kvm_host_map map; 402883b0a91SJoerg Roedel u64 vmcb_gpa; 403883b0a91SJoerg Roedel 4047c67f546SPaolo Bonzini if (is_smm(&svm->vcpu)) { 4057c67f546SPaolo Bonzini kvm_queue_exception(&svm->vcpu, UD_VECTOR); 4067c67f546SPaolo Bonzini return 1; 4077c67f546SPaolo Bonzini } 408883b0a91SJoerg Roedel 4097c67f546SPaolo Bonzini vmcb_gpa = svm->vmcb->save.rax; 410883b0a91SJoerg Roedel ret = kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(vmcb_gpa), &map); 411883b0a91SJoerg Roedel if (ret == -EINVAL) { 412883b0a91SJoerg Roedel kvm_inject_gp(&svm->vcpu, 0); 413883b0a91SJoerg Roedel return 1; 414883b0a91SJoerg Roedel } else if (ret) { 415883b0a91SJoerg Roedel return kvm_skip_emulated_instruction(&svm->vcpu); 416883b0a91SJoerg Roedel } 417883b0a91SJoerg Roedel 418883b0a91SJoerg Roedel ret = kvm_skip_emulated_instruction(&svm->vcpu); 419883b0a91SJoerg Roedel 420883b0a91SJoerg Roedel nested_vmcb = map.hva; 421883b0a91SJoerg Roedel 422883b0a91SJoerg Roedel if (!nested_vmcb_checks(nested_vmcb)) { 423883b0a91SJoerg Roedel nested_vmcb->control.exit_code = SVM_EXIT_ERR; 424883b0a91SJoerg Roedel nested_vmcb->control.exit_code_hi = 0; 425883b0a91SJoerg Roedel nested_vmcb->control.exit_info_1 = 0; 426883b0a91SJoerg Roedel nested_vmcb->control.exit_info_2 = 0; 42769c9dfa2SPaolo Bonzini goto out; 428883b0a91SJoerg Roedel } 429883b0a91SJoerg Roedel 430883b0a91SJoerg Roedel trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa, 431883b0a91SJoerg Roedel nested_vmcb->save.rip, 432883b0a91SJoerg Roedel nested_vmcb->control.int_ctl, 433883b0a91SJoerg Roedel nested_vmcb->control.event_inj, 434883b0a91SJoerg Roedel nested_vmcb->control.nested_ctl); 435883b0a91SJoerg Roedel 436883b0a91SJoerg Roedel trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff, 437883b0a91SJoerg Roedel nested_vmcb->control.intercept_cr >> 16, 438883b0a91SJoerg Roedel nested_vmcb->control.intercept_exceptions, 439883b0a91SJoerg Roedel nested_vmcb->control.intercept); 440883b0a91SJoerg Roedel 441883b0a91SJoerg Roedel /* Clear internal status */ 442883b0a91SJoerg Roedel kvm_clear_exception_queue(&svm->vcpu); 443883b0a91SJoerg Roedel kvm_clear_interrupt_queue(&svm->vcpu); 444883b0a91SJoerg Roedel 445883b0a91SJoerg Roedel /* 446883b0a91SJoerg Roedel * Save the old vmcb, so we don't need to pick what we save, but can 447883b0a91SJoerg Roedel * restore everything when a VMEXIT occurs 448883b0a91SJoerg Roedel */ 449883b0a91SJoerg Roedel hsave->save.es = vmcb->save.es; 450883b0a91SJoerg Roedel hsave->save.cs = vmcb->save.cs; 451883b0a91SJoerg Roedel hsave->save.ss = vmcb->save.ss; 452883b0a91SJoerg Roedel hsave->save.ds = vmcb->save.ds; 453883b0a91SJoerg Roedel hsave->save.gdtr = vmcb->save.gdtr; 454883b0a91SJoerg Roedel hsave->save.idtr = vmcb->save.idtr; 455883b0a91SJoerg Roedel hsave->save.efer = svm->vcpu.arch.efer; 456883b0a91SJoerg Roedel hsave->save.cr0 = kvm_read_cr0(&svm->vcpu); 457883b0a91SJoerg Roedel hsave->save.cr4 = svm->vcpu.arch.cr4; 458883b0a91SJoerg Roedel hsave->save.rflags = kvm_get_rflags(&svm->vcpu); 459883b0a91SJoerg Roedel hsave->save.rip = kvm_rip_read(&svm->vcpu); 460883b0a91SJoerg Roedel hsave->save.rsp = vmcb->save.rsp; 461883b0a91SJoerg Roedel hsave->save.rax = vmcb->save.rax; 462883b0a91SJoerg Roedel if (npt_enabled) 463883b0a91SJoerg Roedel hsave->save.cr3 = vmcb->save.cr3; 464883b0a91SJoerg Roedel else 465883b0a91SJoerg Roedel hsave->save.cr3 = kvm_read_cr3(&svm->vcpu); 466883b0a91SJoerg Roedel 4672f675917SPaolo Bonzini copy_vmcb_control_area(&hsave->control, &vmcb->control); 468883b0a91SJoerg Roedel 469f74f9414SPaolo Bonzini svm->nested.nested_run_pending = 1; 47069c9dfa2SPaolo Bonzini enter_svm_guest_mode(svm, vmcb_gpa, nested_vmcb); 471883b0a91SJoerg Roedel 472883b0a91SJoerg Roedel if (!nested_svm_vmrun_msrpm(svm)) { 473883b0a91SJoerg Roedel svm->vmcb->control.exit_code = SVM_EXIT_ERR; 474883b0a91SJoerg Roedel svm->vmcb->control.exit_code_hi = 0; 475883b0a91SJoerg Roedel svm->vmcb->control.exit_info_1 = 0; 476883b0a91SJoerg Roedel svm->vmcb->control.exit_info_2 = 0; 477883b0a91SJoerg Roedel 478883b0a91SJoerg Roedel nested_svm_vmexit(svm); 479883b0a91SJoerg Roedel } 480883b0a91SJoerg Roedel 48169c9dfa2SPaolo Bonzini out: 48269c9dfa2SPaolo Bonzini kvm_vcpu_unmap(&svm->vcpu, &map, true); 48369c9dfa2SPaolo Bonzini 484883b0a91SJoerg Roedel return ret; 485883b0a91SJoerg Roedel } 486883b0a91SJoerg Roedel 487883b0a91SJoerg Roedel void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb) 488883b0a91SJoerg Roedel { 489883b0a91SJoerg Roedel to_vmcb->save.fs = from_vmcb->save.fs; 490883b0a91SJoerg Roedel to_vmcb->save.gs = from_vmcb->save.gs; 491883b0a91SJoerg Roedel to_vmcb->save.tr = from_vmcb->save.tr; 492883b0a91SJoerg Roedel to_vmcb->save.ldtr = from_vmcb->save.ldtr; 493883b0a91SJoerg Roedel to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base; 494883b0a91SJoerg Roedel to_vmcb->save.star = from_vmcb->save.star; 495883b0a91SJoerg Roedel to_vmcb->save.lstar = from_vmcb->save.lstar; 496883b0a91SJoerg Roedel to_vmcb->save.cstar = from_vmcb->save.cstar; 497883b0a91SJoerg Roedel to_vmcb->save.sfmask = from_vmcb->save.sfmask; 498883b0a91SJoerg Roedel to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs; 499883b0a91SJoerg Roedel to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp; 500883b0a91SJoerg Roedel to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip; 501883b0a91SJoerg Roedel } 502883b0a91SJoerg Roedel 503883b0a91SJoerg Roedel int nested_svm_vmexit(struct vcpu_svm *svm) 504883b0a91SJoerg Roedel { 505883b0a91SJoerg Roedel int rc; 506883b0a91SJoerg Roedel struct vmcb *nested_vmcb; 507883b0a91SJoerg Roedel struct vmcb *hsave = svm->nested.hsave; 508883b0a91SJoerg Roedel struct vmcb *vmcb = svm->vmcb; 509883b0a91SJoerg Roedel struct kvm_host_map map; 510883b0a91SJoerg Roedel 511883b0a91SJoerg Roedel rc = kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(svm->nested.vmcb), &map); 512883b0a91SJoerg Roedel if (rc) { 513883b0a91SJoerg Roedel if (rc == -EINVAL) 514883b0a91SJoerg Roedel kvm_inject_gp(&svm->vcpu, 0); 515883b0a91SJoerg Roedel return 1; 516883b0a91SJoerg Roedel } 517883b0a91SJoerg Roedel 518883b0a91SJoerg Roedel nested_vmcb = map.hva; 519883b0a91SJoerg Roedel 520883b0a91SJoerg Roedel /* Exit Guest-Mode */ 521883b0a91SJoerg Roedel leave_guest_mode(&svm->vcpu); 522883b0a91SJoerg Roedel svm->nested.vmcb = 0; 5232d8a42beSPaolo Bonzini WARN_ON_ONCE(svm->nested.nested_run_pending); 524883b0a91SJoerg Roedel 52538c0b192SPaolo Bonzini /* in case we halted in L2 */ 52638c0b192SPaolo Bonzini svm->vcpu.arch.mp_state = KVM_MP_STATE_RUNNABLE; 52738c0b192SPaolo Bonzini 528883b0a91SJoerg Roedel /* Give the current vmcb to the guest */ 529ffdf7f9eSPaolo Bonzini svm_set_gif(svm, false); 530883b0a91SJoerg Roedel 531883b0a91SJoerg Roedel nested_vmcb->save.es = vmcb->save.es; 532883b0a91SJoerg Roedel nested_vmcb->save.cs = vmcb->save.cs; 533883b0a91SJoerg Roedel nested_vmcb->save.ss = vmcb->save.ss; 534883b0a91SJoerg Roedel nested_vmcb->save.ds = vmcb->save.ds; 535883b0a91SJoerg Roedel nested_vmcb->save.gdtr = vmcb->save.gdtr; 536883b0a91SJoerg Roedel nested_vmcb->save.idtr = vmcb->save.idtr; 537883b0a91SJoerg Roedel nested_vmcb->save.efer = svm->vcpu.arch.efer; 538883b0a91SJoerg Roedel nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu); 539883b0a91SJoerg Roedel nested_vmcb->save.cr3 = kvm_read_cr3(&svm->vcpu); 540883b0a91SJoerg Roedel nested_vmcb->save.cr2 = vmcb->save.cr2; 541883b0a91SJoerg Roedel nested_vmcb->save.cr4 = svm->vcpu.arch.cr4; 542883b0a91SJoerg Roedel nested_vmcb->save.rflags = kvm_get_rflags(&svm->vcpu); 543b6162e82SVitaly Kuznetsov nested_vmcb->save.rip = kvm_rip_read(&svm->vcpu); 544b6162e82SVitaly Kuznetsov nested_vmcb->save.rsp = kvm_rsp_read(&svm->vcpu); 545b6162e82SVitaly Kuznetsov nested_vmcb->save.rax = kvm_rax_read(&svm->vcpu); 546883b0a91SJoerg Roedel nested_vmcb->save.dr7 = vmcb->save.dr7; 5475679b803SPaolo Bonzini nested_vmcb->save.dr6 = svm->vcpu.arch.dr6; 548883b0a91SJoerg Roedel nested_vmcb->save.cpl = vmcb->save.cpl; 549883b0a91SJoerg Roedel 550883b0a91SJoerg Roedel nested_vmcb->control.int_state = vmcb->control.int_state; 551883b0a91SJoerg Roedel nested_vmcb->control.exit_code = vmcb->control.exit_code; 552883b0a91SJoerg Roedel nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi; 553883b0a91SJoerg Roedel nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1; 554883b0a91SJoerg Roedel nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2; 55536e2e983SPaolo Bonzini 55636e2e983SPaolo Bonzini if (nested_vmcb->control.exit_code != SVM_EXIT_ERR) 55736e2e983SPaolo Bonzini nested_vmcb_save_pending_event(svm, nested_vmcb); 558883b0a91SJoerg Roedel 559883b0a91SJoerg Roedel if (svm->nrips_enabled) 560883b0a91SJoerg Roedel nested_vmcb->control.next_rip = vmcb->control.next_rip; 561883b0a91SJoerg Roedel 5622d8a42beSPaolo Bonzini nested_vmcb->control.int_ctl = svm->nested.ctl.int_ctl; 5632d8a42beSPaolo Bonzini nested_vmcb->control.tlb_ctl = svm->nested.ctl.tlb_ctl; 5642d8a42beSPaolo Bonzini nested_vmcb->control.event_inj = svm->nested.ctl.event_inj; 5652d8a42beSPaolo Bonzini nested_vmcb->control.event_inj_err = svm->nested.ctl.event_inj_err; 566883b0a91SJoerg Roedel 567883b0a91SJoerg Roedel nested_vmcb->control.pause_filter_count = 568883b0a91SJoerg Roedel svm->vmcb->control.pause_filter_count; 569883b0a91SJoerg Roedel nested_vmcb->control.pause_filter_thresh = 570883b0a91SJoerg Roedel svm->vmcb->control.pause_filter_thresh; 571883b0a91SJoerg Roedel 572883b0a91SJoerg Roedel /* Restore the original control entries */ 5732f675917SPaolo Bonzini copy_vmcb_control_area(&vmcb->control, &hsave->control); 574883b0a91SJoerg Roedel 57518fc6c55SPaolo Bonzini svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset = 57618fc6c55SPaolo Bonzini svm->vcpu.arch.l1_tsc_offset; 57718fc6c55SPaolo Bonzini 578e670bf68SPaolo Bonzini svm->nested.ctl.nested_cr3 = 0; 579883b0a91SJoerg Roedel 580883b0a91SJoerg Roedel /* Restore selected save entries */ 581883b0a91SJoerg Roedel svm->vmcb->save.es = hsave->save.es; 582883b0a91SJoerg Roedel svm->vmcb->save.cs = hsave->save.cs; 583883b0a91SJoerg Roedel svm->vmcb->save.ss = hsave->save.ss; 584883b0a91SJoerg Roedel svm->vmcb->save.ds = hsave->save.ds; 585883b0a91SJoerg Roedel svm->vmcb->save.gdtr = hsave->save.gdtr; 586883b0a91SJoerg Roedel svm->vmcb->save.idtr = hsave->save.idtr; 587883b0a91SJoerg Roedel kvm_set_rflags(&svm->vcpu, hsave->save.rflags); 588883b0a91SJoerg Roedel svm_set_efer(&svm->vcpu, hsave->save.efer); 589883b0a91SJoerg Roedel svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE); 590883b0a91SJoerg Roedel svm_set_cr4(&svm->vcpu, hsave->save.cr4); 591883b0a91SJoerg Roedel if (npt_enabled) { 592883b0a91SJoerg Roedel svm->vmcb->save.cr3 = hsave->save.cr3; 593883b0a91SJoerg Roedel svm->vcpu.arch.cr3 = hsave->save.cr3; 594883b0a91SJoerg Roedel } else { 595883b0a91SJoerg Roedel (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3); 596883b0a91SJoerg Roedel } 597883b0a91SJoerg Roedel kvm_rax_write(&svm->vcpu, hsave->save.rax); 598883b0a91SJoerg Roedel kvm_rsp_write(&svm->vcpu, hsave->save.rsp); 599883b0a91SJoerg Roedel kvm_rip_write(&svm->vcpu, hsave->save.rip); 600883b0a91SJoerg Roedel svm->vmcb->save.dr7 = 0; 601883b0a91SJoerg Roedel svm->vmcb->save.cpl = 0; 602883b0a91SJoerg Roedel svm->vmcb->control.exit_int_info = 0; 603883b0a91SJoerg Roedel 604883b0a91SJoerg Roedel mark_all_dirty(svm->vmcb); 605883b0a91SJoerg Roedel 60636e2e983SPaolo Bonzini trace_kvm_nested_vmexit_inject(nested_vmcb->control.exit_code, 60736e2e983SPaolo Bonzini nested_vmcb->control.exit_info_1, 60836e2e983SPaolo Bonzini nested_vmcb->control.exit_info_2, 60936e2e983SPaolo Bonzini nested_vmcb->control.exit_int_info, 61036e2e983SPaolo Bonzini nested_vmcb->control.exit_int_info_err, 61136e2e983SPaolo Bonzini KVM_ISA_SVM); 61236e2e983SPaolo Bonzini 613883b0a91SJoerg Roedel kvm_vcpu_unmap(&svm->vcpu, &map, true); 614883b0a91SJoerg Roedel 615883b0a91SJoerg Roedel nested_svm_uninit_mmu_context(&svm->vcpu); 616883b0a91SJoerg Roedel kvm_mmu_reset_context(&svm->vcpu); 617883b0a91SJoerg Roedel kvm_mmu_load(&svm->vcpu); 618883b0a91SJoerg Roedel 619883b0a91SJoerg Roedel /* 620883b0a91SJoerg Roedel * Drop what we picked up for L2 via svm_complete_interrupts() so it 621883b0a91SJoerg Roedel * doesn't end up in L1. 622883b0a91SJoerg Roedel */ 623883b0a91SJoerg Roedel svm->vcpu.arch.nmi_injected = false; 624883b0a91SJoerg Roedel kvm_clear_exception_queue(&svm->vcpu); 625883b0a91SJoerg Roedel kvm_clear_interrupt_queue(&svm->vcpu); 626883b0a91SJoerg Roedel 627883b0a91SJoerg Roedel return 0; 628883b0a91SJoerg Roedel } 629883b0a91SJoerg Roedel 630c513f484SPaolo Bonzini /* 631c513f484SPaolo Bonzini * Forcibly leave nested mode in order to be able to reset the VCPU later on. 632c513f484SPaolo Bonzini */ 633c513f484SPaolo Bonzini void svm_leave_nested(struct vcpu_svm *svm) 634c513f484SPaolo Bonzini { 635c513f484SPaolo Bonzini if (is_guest_mode(&svm->vcpu)) { 636c513f484SPaolo Bonzini struct vmcb *hsave = svm->nested.hsave; 637c513f484SPaolo Bonzini struct vmcb *vmcb = svm->vmcb; 638c513f484SPaolo Bonzini 639c513f484SPaolo Bonzini svm->nested.nested_run_pending = 0; 640c513f484SPaolo Bonzini leave_guest_mode(&svm->vcpu); 641c513f484SPaolo Bonzini copy_vmcb_control_area(&vmcb->control, &hsave->control); 642c513f484SPaolo Bonzini nested_svm_uninit_mmu_context(&svm->vcpu); 643c513f484SPaolo Bonzini } 644c513f484SPaolo Bonzini } 645c513f484SPaolo Bonzini 646883b0a91SJoerg Roedel static int nested_svm_exit_handled_msr(struct vcpu_svm *svm) 647883b0a91SJoerg Roedel { 648883b0a91SJoerg Roedel u32 offset, msr, value; 649883b0a91SJoerg Roedel int write, mask; 650883b0a91SJoerg Roedel 651e670bf68SPaolo Bonzini if (!(svm->nested.ctl.intercept & (1ULL << INTERCEPT_MSR_PROT))) 652883b0a91SJoerg Roedel return NESTED_EXIT_HOST; 653883b0a91SJoerg Roedel 654883b0a91SJoerg Roedel msr = svm->vcpu.arch.regs[VCPU_REGS_RCX]; 655883b0a91SJoerg Roedel offset = svm_msrpm_offset(msr); 656883b0a91SJoerg Roedel write = svm->vmcb->control.exit_info_1 & 1; 657883b0a91SJoerg Roedel mask = 1 << ((2 * (msr & 0xf)) + write); 658883b0a91SJoerg Roedel 659883b0a91SJoerg Roedel if (offset == MSR_INVALID) 660883b0a91SJoerg Roedel return NESTED_EXIT_DONE; 661883b0a91SJoerg Roedel 662883b0a91SJoerg Roedel /* Offset is in 32 bit units but need in 8 bit units */ 663883b0a91SJoerg Roedel offset *= 4; 664883b0a91SJoerg Roedel 665e670bf68SPaolo Bonzini if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.ctl.msrpm_base_pa + offset, &value, 4)) 666883b0a91SJoerg Roedel return NESTED_EXIT_DONE; 667883b0a91SJoerg Roedel 668883b0a91SJoerg Roedel return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST; 669883b0a91SJoerg Roedel } 670883b0a91SJoerg Roedel 671883b0a91SJoerg Roedel static int nested_svm_intercept_ioio(struct vcpu_svm *svm) 672883b0a91SJoerg Roedel { 673883b0a91SJoerg Roedel unsigned port, size, iopm_len; 674883b0a91SJoerg Roedel u16 val, mask; 675883b0a91SJoerg Roedel u8 start_bit; 676883b0a91SJoerg Roedel u64 gpa; 677883b0a91SJoerg Roedel 678e670bf68SPaolo Bonzini if (!(svm->nested.ctl.intercept & (1ULL << INTERCEPT_IOIO_PROT))) 679883b0a91SJoerg Roedel return NESTED_EXIT_HOST; 680883b0a91SJoerg Roedel 681883b0a91SJoerg Roedel port = svm->vmcb->control.exit_info_1 >> 16; 682883b0a91SJoerg Roedel size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >> 683883b0a91SJoerg Roedel SVM_IOIO_SIZE_SHIFT; 684e670bf68SPaolo Bonzini gpa = svm->nested.ctl.iopm_base_pa + (port / 8); 685883b0a91SJoerg Roedel start_bit = port % 8; 686883b0a91SJoerg Roedel iopm_len = (start_bit + size > 8) ? 2 : 1; 687883b0a91SJoerg Roedel mask = (0xf >> (4 - size)) << start_bit; 688883b0a91SJoerg Roedel val = 0; 689883b0a91SJoerg Roedel 690883b0a91SJoerg Roedel if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len)) 691883b0a91SJoerg Roedel return NESTED_EXIT_DONE; 692883b0a91SJoerg Roedel 693883b0a91SJoerg Roedel return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST; 694883b0a91SJoerg Roedel } 695883b0a91SJoerg Roedel 696883b0a91SJoerg Roedel static int nested_svm_intercept(struct vcpu_svm *svm) 697883b0a91SJoerg Roedel { 698883b0a91SJoerg Roedel u32 exit_code = svm->vmcb->control.exit_code; 699883b0a91SJoerg Roedel int vmexit = NESTED_EXIT_HOST; 700883b0a91SJoerg Roedel 701883b0a91SJoerg Roedel switch (exit_code) { 702883b0a91SJoerg Roedel case SVM_EXIT_MSR: 703883b0a91SJoerg Roedel vmexit = nested_svm_exit_handled_msr(svm); 704883b0a91SJoerg Roedel break; 705883b0a91SJoerg Roedel case SVM_EXIT_IOIO: 706883b0a91SJoerg Roedel vmexit = nested_svm_intercept_ioio(svm); 707883b0a91SJoerg Roedel break; 708883b0a91SJoerg Roedel case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: { 709883b0a91SJoerg Roedel u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0); 710e670bf68SPaolo Bonzini if (svm->nested.ctl.intercept_cr & bit) 711883b0a91SJoerg Roedel vmexit = NESTED_EXIT_DONE; 712883b0a91SJoerg Roedel break; 713883b0a91SJoerg Roedel } 714883b0a91SJoerg Roedel case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: { 715883b0a91SJoerg Roedel u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0); 716e670bf68SPaolo Bonzini if (svm->nested.ctl.intercept_dr & bit) 717883b0a91SJoerg Roedel vmexit = NESTED_EXIT_DONE; 718883b0a91SJoerg Roedel break; 719883b0a91SJoerg Roedel } 720883b0a91SJoerg Roedel case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: { 7217c86663bSPaolo Bonzini /* 7227c86663bSPaolo Bonzini * Host-intercepted exceptions have been checked already in 7237c86663bSPaolo Bonzini * nested_svm_exit_special. There is nothing to do here, 7247c86663bSPaolo Bonzini * the vmexit is injected by svm_check_nested_events. 7257c86663bSPaolo Bonzini */ 726883b0a91SJoerg Roedel vmexit = NESTED_EXIT_DONE; 727883b0a91SJoerg Roedel break; 728883b0a91SJoerg Roedel } 729883b0a91SJoerg Roedel case SVM_EXIT_ERR: { 730883b0a91SJoerg Roedel vmexit = NESTED_EXIT_DONE; 731883b0a91SJoerg Roedel break; 732883b0a91SJoerg Roedel } 733883b0a91SJoerg Roedel default: { 734883b0a91SJoerg Roedel u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR); 735e670bf68SPaolo Bonzini if (svm->nested.ctl.intercept & exit_bits) 736883b0a91SJoerg Roedel vmexit = NESTED_EXIT_DONE; 737883b0a91SJoerg Roedel } 738883b0a91SJoerg Roedel } 739883b0a91SJoerg Roedel 740883b0a91SJoerg Roedel return vmexit; 741883b0a91SJoerg Roedel } 742883b0a91SJoerg Roedel 743883b0a91SJoerg Roedel int nested_svm_exit_handled(struct vcpu_svm *svm) 744883b0a91SJoerg Roedel { 745883b0a91SJoerg Roedel int vmexit; 746883b0a91SJoerg Roedel 747883b0a91SJoerg Roedel vmexit = nested_svm_intercept(svm); 748883b0a91SJoerg Roedel 749883b0a91SJoerg Roedel if (vmexit == NESTED_EXIT_DONE) 750883b0a91SJoerg Roedel nested_svm_vmexit(svm); 751883b0a91SJoerg Roedel 752883b0a91SJoerg Roedel return vmexit; 753883b0a91SJoerg Roedel } 754883b0a91SJoerg Roedel 755883b0a91SJoerg Roedel int nested_svm_check_permissions(struct vcpu_svm *svm) 756883b0a91SJoerg Roedel { 757883b0a91SJoerg Roedel if (!(svm->vcpu.arch.efer & EFER_SVME) || 758883b0a91SJoerg Roedel !is_paging(&svm->vcpu)) { 759883b0a91SJoerg Roedel kvm_queue_exception(&svm->vcpu, UD_VECTOR); 760883b0a91SJoerg Roedel return 1; 761883b0a91SJoerg Roedel } 762883b0a91SJoerg Roedel 763883b0a91SJoerg Roedel if (svm->vmcb->save.cpl) { 764883b0a91SJoerg Roedel kvm_inject_gp(&svm->vcpu, 0); 765883b0a91SJoerg Roedel return 1; 766883b0a91SJoerg Roedel } 767883b0a91SJoerg Roedel 768883b0a91SJoerg Roedel return 0; 769883b0a91SJoerg Roedel } 770883b0a91SJoerg Roedel 7717c86663bSPaolo Bonzini static bool nested_exit_on_exception(struct vcpu_svm *svm) 772883b0a91SJoerg Roedel { 7737c86663bSPaolo Bonzini unsigned int nr = svm->vcpu.arch.exception.nr; 774883b0a91SJoerg Roedel 775e670bf68SPaolo Bonzini return (svm->nested.ctl.intercept_exceptions & (1 << nr)); 7767c86663bSPaolo Bonzini } 777883b0a91SJoerg Roedel 7787c86663bSPaolo Bonzini static void nested_svm_inject_exception_vmexit(struct vcpu_svm *svm) 7797c86663bSPaolo Bonzini { 7807c86663bSPaolo Bonzini unsigned int nr = svm->vcpu.arch.exception.nr; 781883b0a91SJoerg Roedel 782883b0a91SJoerg Roedel svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr; 783883b0a91SJoerg Roedel svm->vmcb->control.exit_code_hi = 0; 7847c86663bSPaolo Bonzini 7857c86663bSPaolo Bonzini if (svm->vcpu.arch.exception.has_error_code) 7867c86663bSPaolo Bonzini svm->vmcb->control.exit_info_1 = svm->vcpu.arch.exception.error_code; 787883b0a91SJoerg Roedel 788883b0a91SJoerg Roedel /* 789883b0a91SJoerg Roedel * EXITINFO2 is undefined for all exception intercepts other 790883b0a91SJoerg Roedel * than #PF. 791883b0a91SJoerg Roedel */ 7927c86663bSPaolo Bonzini if (nr == PF_VECTOR) { 793883b0a91SJoerg Roedel if (svm->vcpu.arch.exception.nested_apf) 794883b0a91SJoerg Roedel svm->vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token; 795883b0a91SJoerg Roedel else if (svm->vcpu.arch.exception.has_payload) 796883b0a91SJoerg Roedel svm->vmcb->control.exit_info_2 = svm->vcpu.arch.exception.payload; 797883b0a91SJoerg Roedel else 798883b0a91SJoerg Roedel svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2; 7997c86663bSPaolo Bonzini } else if (nr == DB_VECTOR) { 8007c86663bSPaolo Bonzini /* See inject_pending_event. */ 8017c86663bSPaolo Bonzini kvm_deliver_exception_payload(&svm->vcpu); 8027c86663bSPaolo Bonzini if (svm->vcpu.arch.dr7 & DR7_GD) { 8037c86663bSPaolo Bonzini svm->vcpu.arch.dr7 &= ~DR7_GD; 8047c86663bSPaolo Bonzini kvm_update_dr7(&svm->vcpu); 8057c86663bSPaolo Bonzini } 8067c86663bSPaolo Bonzini } else 8077c86663bSPaolo Bonzini WARN_ON(svm->vcpu.arch.exception.has_payload); 808883b0a91SJoerg Roedel 8097c86663bSPaolo Bonzini nested_svm_vmexit(svm); 810883b0a91SJoerg Roedel } 811883b0a91SJoerg Roedel 81255714cddSPaolo Bonzini static void nested_svm_smi(struct vcpu_svm *svm) 81355714cddSPaolo Bonzini { 81455714cddSPaolo Bonzini svm->vmcb->control.exit_code = SVM_EXIT_SMI; 81555714cddSPaolo Bonzini svm->vmcb->control.exit_info_1 = 0; 81655714cddSPaolo Bonzini svm->vmcb->control.exit_info_2 = 0; 81755714cddSPaolo Bonzini 81855714cddSPaolo Bonzini nested_svm_vmexit(svm); 81955714cddSPaolo Bonzini } 82055714cddSPaolo Bonzini 8219c3d370aSCathy Avery static void nested_svm_nmi(struct vcpu_svm *svm) 8229c3d370aSCathy Avery { 8239c3d370aSCathy Avery svm->vmcb->control.exit_code = SVM_EXIT_NMI; 8249c3d370aSCathy Avery svm->vmcb->control.exit_info_1 = 0; 8259c3d370aSCathy Avery svm->vmcb->control.exit_info_2 = 0; 8269c3d370aSCathy Avery 8279c3d370aSCathy Avery nested_svm_vmexit(svm); 8289c3d370aSCathy Avery } 8299c3d370aSCathy Avery 830883b0a91SJoerg Roedel static void nested_svm_intr(struct vcpu_svm *svm) 831883b0a91SJoerg Roedel { 8326e085cbfSPaolo Bonzini trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip); 8336e085cbfSPaolo Bonzini 834883b0a91SJoerg Roedel svm->vmcb->control.exit_code = SVM_EXIT_INTR; 835883b0a91SJoerg Roedel svm->vmcb->control.exit_info_1 = 0; 836883b0a91SJoerg Roedel svm->vmcb->control.exit_info_2 = 0; 837883b0a91SJoerg Roedel 8386e085cbfSPaolo Bonzini nested_svm_vmexit(svm); 839883b0a91SJoerg Roedel } 840883b0a91SJoerg Roedel 8415b672408SPaolo Bonzini static inline bool nested_exit_on_init(struct vcpu_svm *svm) 8425b672408SPaolo Bonzini { 843e670bf68SPaolo Bonzini return (svm->nested.ctl.intercept & (1ULL << INTERCEPT_INIT)); 8445b672408SPaolo Bonzini } 8455b672408SPaolo Bonzini 8465b672408SPaolo Bonzini static void nested_svm_init(struct vcpu_svm *svm) 8475b672408SPaolo Bonzini { 8485b672408SPaolo Bonzini svm->vmcb->control.exit_code = SVM_EXIT_INIT; 8495b672408SPaolo Bonzini svm->vmcb->control.exit_info_1 = 0; 8505b672408SPaolo Bonzini svm->vmcb->control.exit_info_2 = 0; 8515b672408SPaolo Bonzini 8525b672408SPaolo Bonzini nested_svm_vmexit(svm); 8535b672408SPaolo Bonzini } 8545b672408SPaolo Bonzini 8555b672408SPaolo Bonzini 85633b22172SPaolo Bonzini static int svm_check_nested_events(struct kvm_vcpu *vcpu) 857883b0a91SJoerg Roedel { 858883b0a91SJoerg Roedel struct vcpu_svm *svm = to_svm(vcpu); 859883b0a91SJoerg Roedel bool block_nested_events = 860bd279629SPaolo Bonzini kvm_event_needs_reinjection(vcpu) || svm->nested.nested_run_pending; 8615b672408SPaolo Bonzini struct kvm_lapic *apic = vcpu->arch.apic; 8625b672408SPaolo Bonzini 8635b672408SPaolo Bonzini if (lapic_in_kernel(vcpu) && 8645b672408SPaolo Bonzini test_bit(KVM_APIC_INIT, &apic->pending_events)) { 8655b672408SPaolo Bonzini if (block_nested_events) 8665b672408SPaolo Bonzini return -EBUSY; 8675b672408SPaolo Bonzini if (!nested_exit_on_init(svm)) 8685b672408SPaolo Bonzini return 0; 8695b672408SPaolo Bonzini nested_svm_init(svm); 8705b672408SPaolo Bonzini return 0; 8715b672408SPaolo Bonzini } 872883b0a91SJoerg Roedel 8737c86663bSPaolo Bonzini if (vcpu->arch.exception.pending) { 8747c86663bSPaolo Bonzini if (block_nested_events) 8757c86663bSPaolo Bonzini return -EBUSY; 8767c86663bSPaolo Bonzini if (!nested_exit_on_exception(svm)) 8777c86663bSPaolo Bonzini return 0; 8787c86663bSPaolo Bonzini nested_svm_inject_exception_vmexit(svm); 8797c86663bSPaolo Bonzini return 0; 8807c86663bSPaolo Bonzini } 8817c86663bSPaolo Bonzini 882221e7610SPaolo Bonzini if (vcpu->arch.smi_pending && !svm_smi_blocked(vcpu)) { 88355714cddSPaolo Bonzini if (block_nested_events) 88455714cddSPaolo Bonzini return -EBUSY; 885221e7610SPaolo Bonzini if (!nested_exit_on_smi(svm)) 886221e7610SPaolo Bonzini return 0; 88755714cddSPaolo Bonzini nested_svm_smi(svm); 88855714cddSPaolo Bonzini return 0; 88955714cddSPaolo Bonzini } 89055714cddSPaolo Bonzini 891221e7610SPaolo Bonzini if (vcpu->arch.nmi_pending && !svm_nmi_blocked(vcpu)) { 8929c3d370aSCathy Avery if (block_nested_events) 8939c3d370aSCathy Avery return -EBUSY; 894221e7610SPaolo Bonzini if (!nested_exit_on_nmi(svm)) 895221e7610SPaolo Bonzini return 0; 8969c3d370aSCathy Avery nested_svm_nmi(svm); 8979c3d370aSCathy Avery return 0; 8989c3d370aSCathy Avery } 8999c3d370aSCathy Avery 900221e7610SPaolo Bonzini if (kvm_cpu_has_interrupt(vcpu) && !svm_interrupt_blocked(vcpu)) { 901883b0a91SJoerg Roedel if (block_nested_events) 902883b0a91SJoerg Roedel return -EBUSY; 903221e7610SPaolo Bonzini if (!nested_exit_on_intr(svm)) 904221e7610SPaolo Bonzini return 0; 905883b0a91SJoerg Roedel nested_svm_intr(svm); 906883b0a91SJoerg Roedel return 0; 907883b0a91SJoerg Roedel } 908883b0a91SJoerg Roedel 909883b0a91SJoerg Roedel return 0; 910883b0a91SJoerg Roedel } 911883b0a91SJoerg Roedel 912883b0a91SJoerg Roedel int nested_svm_exit_special(struct vcpu_svm *svm) 913883b0a91SJoerg Roedel { 914883b0a91SJoerg Roedel u32 exit_code = svm->vmcb->control.exit_code; 915883b0a91SJoerg Roedel 916883b0a91SJoerg Roedel switch (exit_code) { 917883b0a91SJoerg Roedel case SVM_EXIT_INTR: 918883b0a91SJoerg Roedel case SVM_EXIT_NMI: 919883b0a91SJoerg Roedel case SVM_EXIT_NPF: 920883b0a91SJoerg Roedel return NESTED_EXIT_HOST; 9217c86663bSPaolo Bonzini case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: { 9227c86663bSPaolo Bonzini u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE); 9237c86663bSPaolo Bonzini 9247c86663bSPaolo Bonzini if (get_host_vmcb(svm)->control.intercept_exceptions & excp_bits) 9257c86663bSPaolo Bonzini return NESTED_EXIT_HOST; 9267c86663bSPaolo Bonzini else if (exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR && 92768fd66f1SVitaly Kuznetsov svm->vcpu.arch.apf.host_apf_flags) 928a3535be7SPaolo Bonzini /* Trap async PF even if not shadowing */ 929883b0a91SJoerg Roedel return NESTED_EXIT_HOST; 930883b0a91SJoerg Roedel break; 9317c86663bSPaolo Bonzini } 932883b0a91SJoerg Roedel default: 933883b0a91SJoerg Roedel break; 934883b0a91SJoerg Roedel } 935883b0a91SJoerg Roedel 936883b0a91SJoerg Roedel return NESTED_EXIT_CONTINUE; 937883b0a91SJoerg Roedel } 93833b22172SPaolo Bonzini 939cc440cdaSPaolo Bonzini static int svm_get_nested_state(struct kvm_vcpu *vcpu, 940cc440cdaSPaolo Bonzini struct kvm_nested_state __user *user_kvm_nested_state, 941cc440cdaSPaolo Bonzini u32 user_data_size) 942cc440cdaSPaolo Bonzini { 943cc440cdaSPaolo Bonzini struct vcpu_svm *svm; 944cc440cdaSPaolo Bonzini struct kvm_nested_state kvm_state = { 945cc440cdaSPaolo Bonzini .flags = 0, 946cc440cdaSPaolo Bonzini .format = KVM_STATE_NESTED_FORMAT_SVM, 947cc440cdaSPaolo Bonzini .size = sizeof(kvm_state), 948cc440cdaSPaolo Bonzini }; 949cc440cdaSPaolo Bonzini struct vmcb __user *user_vmcb = (struct vmcb __user *) 950cc440cdaSPaolo Bonzini &user_kvm_nested_state->data.svm[0]; 951cc440cdaSPaolo Bonzini 952cc440cdaSPaolo Bonzini if (!vcpu) 953cc440cdaSPaolo Bonzini return kvm_state.size + KVM_STATE_NESTED_SVM_VMCB_SIZE; 954cc440cdaSPaolo Bonzini 955cc440cdaSPaolo Bonzini svm = to_svm(vcpu); 956cc440cdaSPaolo Bonzini 957cc440cdaSPaolo Bonzini if (user_data_size < kvm_state.size) 958cc440cdaSPaolo Bonzini goto out; 959cc440cdaSPaolo Bonzini 960cc440cdaSPaolo Bonzini /* First fill in the header and copy it out. */ 961cc440cdaSPaolo Bonzini if (is_guest_mode(vcpu)) { 962cc440cdaSPaolo Bonzini kvm_state.hdr.svm.vmcb_pa = svm->nested.vmcb; 963cc440cdaSPaolo Bonzini kvm_state.size += KVM_STATE_NESTED_SVM_VMCB_SIZE; 964cc440cdaSPaolo Bonzini kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE; 965cc440cdaSPaolo Bonzini 966cc440cdaSPaolo Bonzini if (svm->nested.nested_run_pending) 967cc440cdaSPaolo Bonzini kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING; 968cc440cdaSPaolo Bonzini } 969cc440cdaSPaolo Bonzini 970cc440cdaSPaolo Bonzini if (gif_set(svm)) 971cc440cdaSPaolo Bonzini kvm_state.flags |= KVM_STATE_NESTED_GIF_SET; 972cc440cdaSPaolo Bonzini 973cc440cdaSPaolo Bonzini if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state))) 974cc440cdaSPaolo Bonzini return -EFAULT; 975cc440cdaSPaolo Bonzini 976cc440cdaSPaolo Bonzini if (!is_guest_mode(vcpu)) 977cc440cdaSPaolo Bonzini goto out; 978cc440cdaSPaolo Bonzini 979cc440cdaSPaolo Bonzini /* 980cc440cdaSPaolo Bonzini * Copy over the full size of the VMCB rather than just the size 981cc440cdaSPaolo Bonzini * of the structs. 982cc440cdaSPaolo Bonzini */ 983cc440cdaSPaolo Bonzini if (clear_user(user_vmcb, KVM_STATE_NESTED_SVM_VMCB_SIZE)) 984cc440cdaSPaolo Bonzini return -EFAULT; 985cc440cdaSPaolo Bonzini if (copy_to_user(&user_vmcb->control, &svm->nested.ctl, 986cc440cdaSPaolo Bonzini sizeof(user_vmcb->control))) 987cc440cdaSPaolo Bonzini return -EFAULT; 988cc440cdaSPaolo Bonzini if (copy_to_user(&user_vmcb->save, &svm->nested.hsave->save, 989cc440cdaSPaolo Bonzini sizeof(user_vmcb->save))) 990cc440cdaSPaolo Bonzini return -EFAULT; 991cc440cdaSPaolo Bonzini 992cc440cdaSPaolo Bonzini out: 993cc440cdaSPaolo Bonzini return kvm_state.size; 994cc440cdaSPaolo Bonzini } 995cc440cdaSPaolo Bonzini 996cc440cdaSPaolo Bonzini static int svm_set_nested_state(struct kvm_vcpu *vcpu, 997cc440cdaSPaolo Bonzini struct kvm_nested_state __user *user_kvm_nested_state, 998cc440cdaSPaolo Bonzini struct kvm_nested_state *kvm_state) 999cc440cdaSPaolo Bonzini { 1000cc440cdaSPaolo Bonzini struct vcpu_svm *svm = to_svm(vcpu); 1001cc440cdaSPaolo Bonzini struct vmcb *hsave = svm->nested.hsave; 1002cc440cdaSPaolo Bonzini struct vmcb __user *user_vmcb = (struct vmcb __user *) 1003cc440cdaSPaolo Bonzini &user_kvm_nested_state->data.svm[0]; 1004cc440cdaSPaolo Bonzini struct vmcb_control_area ctl; 1005cc440cdaSPaolo Bonzini struct vmcb_save_area save; 1006cc440cdaSPaolo Bonzini u32 cr0; 1007cc440cdaSPaolo Bonzini 1008cc440cdaSPaolo Bonzini if (kvm_state->format != KVM_STATE_NESTED_FORMAT_SVM) 1009cc440cdaSPaolo Bonzini return -EINVAL; 1010cc440cdaSPaolo Bonzini 1011cc440cdaSPaolo Bonzini if (kvm_state->flags & ~(KVM_STATE_NESTED_GUEST_MODE | 1012cc440cdaSPaolo Bonzini KVM_STATE_NESTED_RUN_PENDING | 1013cc440cdaSPaolo Bonzini KVM_STATE_NESTED_GIF_SET)) 1014cc440cdaSPaolo Bonzini return -EINVAL; 1015cc440cdaSPaolo Bonzini 1016cc440cdaSPaolo Bonzini /* 1017cc440cdaSPaolo Bonzini * If in guest mode, vcpu->arch.efer actually refers to the L2 guest's 1018cc440cdaSPaolo Bonzini * EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed. 1019cc440cdaSPaolo Bonzini */ 1020cc440cdaSPaolo Bonzini if (!(vcpu->arch.efer & EFER_SVME)) { 1021cc440cdaSPaolo Bonzini /* GIF=1 and no guest mode are required if SVME=0. */ 1022cc440cdaSPaolo Bonzini if (kvm_state->flags != KVM_STATE_NESTED_GIF_SET) 1023cc440cdaSPaolo Bonzini return -EINVAL; 1024cc440cdaSPaolo Bonzini } 1025cc440cdaSPaolo Bonzini 1026cc440cdaSPaolo Bonzini /* SMM temporarily disables SVM, so we cannot be in guest mode. */ 1027cc440cdaSPaolo Bonzini if (is_smm(vcpu) && (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) 1028cc440cdaSPaolo Bonzini return -EINVAL; 1029cc440cdaSPaolo Bonzini 1030cc440cdaSPaolo Bonzini if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) { 1031cc440cdaSPaolo Bonzini svm_leave_nested(svm); 1032cc440cdaSPaolo Bonzini goto out_set_gif; 1033cc440cdaSPaolo Bonzini } 1034cc440cdaSPaolo Bonzini 1035cc440cdaSPaolo Bonzini if (!page_address_valid(vcpu, kvm_state->hdr.svm.vmcb_pa)) 1036cc440cdaSPaolo Bonzini return -EINVAL; 1037cc440cdaSPaolo Bonzini if (kvm_state->size < sizeof(*kvm_state) + KVM_STATE_NESTED_SVM_VMCB_SIZE) 1038cc440cdaSPaolo Bonzini return -EINVAL; 1039cc440cdaSPaolo Bonzini if (copy_from_user(&ctl, &user_vmcb->control, sizeof(ctl))) 1040cc440cdaSPaolo Bonzini return -EFAULT; 1041cc440cdaSPaolo Bonzini if (copy_from_user(&save, &user_vmcb->save, sizeof(save))) 1042cc440cdaSPaolo Bonzini return -EFAULT; 1043cc440cdaSPaolo Bonzini 1044cc440cdaSPaolo Bonzini if (!nested_vmcb_check_controls(&ctl)) 1045cc440cdaSPaolo Bonzini return -EINVAL; 1046cc440cdaSPaolo Bonzini 1047cc440cdaSPaolo Bonzini /* 1048cc440cdaSPaolo Bonzini * Processor state contains L2 state. Check that it is 1049cc440cdaSPaolo Bonzini * valid for guest mode (see nested_vmcb_checks). 1050cc440cdaSPaolo Bonzini */ 1051cc440cdaSPaolo Bonzini cr0 = kvm_read_cr0(vcpu); 1052cc440cdaSPaolo Bonzini if (((cr0 & X86_CR0_CD) == 0) && (cr0 & X86_CR0_NW)) 1053cc440cdaSPaolo Bonzini return -EINVAL; 1054cc440cdaSPaolo Bonzini 1055cc440cdaSPaolo Bonzini /* 1056cc440cdaSPaolo Bonzini * Validate host state saved from before VMRUN (see 1057cc440cdaSPaolo Bonzini * nested_svm_check_permissions). 1058cc440cdaSPaolo Bonzini * TODO: validate reserved bits for all saved state. 1059cc440cdaSPaolo Bonzini */ 1060cc440cdaSPaolo Bonzini if (!(save.cr0 & X86_CR0_PG)) 1061cc440cdaSPaolo Bonzini return -EINVAL; 1062cc440cdaSPaolo Bonzini 1063cc440cdaSPaolo Bonzini /* 1064cc440cdaSPaolo Bonzini * All checks done, we can enter guest mode. L1 control fields 1065cc440cdaSPaolo Bonzini * come from the nested save state. Guest state is already 1066cc440cdaSPaolo Bonzini * in the registers, the save area of the nested state instead 1067cc440cdaSPaolo Bonzini * contains saved L1 state. 1068cc440cdaSPaolo Bonzini */ 1069cc440cdaSPaolo Bonzini copy_vmcb_control_area(&hsave->control, &svm->vmcb->control); 1070cc440cdaSPaolo Bonzini hsave->save = save; 1071cc440cdaSPaolo Bonzini 1072cc440cdaSPaolo Bonzini svm->nested.vmcb = kvm_state->hdr.svm.vmcb_pa; 1073cc440cdaSPaolo Bonzini load_nested_vmcb_control(svm, &ctl); 1074cc440cdaSPaolo Bonzini nested_prepare_vmcb_control(svm); 1075cc440cdaSPaolo Bonzini 1076cc440cdaSPaolo Bonzini out_set_gif: 1077cc440cdaSPaolo Bonzini svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET)); 1078cc440cdaSPaolo Bonzini return 0; 1079cc440cdaSPaolo Bonzini } 1080cc440cdaSPaolo Bonzini 108133b22172SPaolo Bonzini struct kvm_x86_nested_ops svm_nested_ops = { 108233b22172SPaolo Bonzini .check_events = svm_check_nested_events, 1083cc440cdaSPaolo Bonzini .get_state = svm_get_nested_state, 1084cc440cdaSPaolo Bonzini .set_state = svm_set_nested_state, 108533b22172SPaolo Bonzini }; 1086