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> 22883b0a91SJoerg Roedel 23883b0a91SJoerg Roedel #include "kvm_emulate.h" 24883b0a91SJoerg Roedel #include "trace.h" 25883b0a91SJoerg Roedel #include "mmu.h" 26883b0a91SJoerg Roedel #include "x86.h" 27883b0a91SJoerg Roedel #include "svm.h" 28883b0a91SJoerg Roedel 29883b0a91SJoerg Roedel static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu, 30883b0a91SJoerg Roedel struct x86_exception *fault) 31883b0a91SJoerg Roedel { 32883b0a91SJoerg Roedel struct vcpu_svm *svm = to_svm(vcpu); 33883b0a91SJoerg Roedel 34883b0a91SJoerg Roedel if (svm->vmcb->control.exit_code != SVM_EXIT_NPF) { 35883b0a91SJoerg Roedel /* 36883b0a91SJoerg Roedel * TODO: track the cause of the nested page fault, and 37883b0a91SJoerg Roedel * correctly fill in the high bits of exit_info_1. 38883b0a91SJoerg Roedel */ 39883b0a91SJoerg Roedel svm->vmcb->control.exit_code = SVM_EXIT_NPF; 40883b0a91SJoerg Roedel svm->vmcb->control.exit_code_hi = 0; 41883b0a91SJoerg Roedel svm->vmcb->control.exit_info_1 = (1ULL << 32); 42883b0a91SJoerg Roedel svm->vmcb->control.exit_info_2 = fault->address; 43883b0a91SJoerg Roedel } 44883b0a91SJoerg Roedel 45883b0a91SJoerg Roedel svm->vmcb->control.exit_info_1 &= ~0xffffffffULL; 46883b0a91SJoerg Roedel svm->vmcb->control.exit_info_1 |= fault->error_code; 47883b0a91SJoerg Roedel 48883b0a91SJoerg Roedel /* 49883b0a91SJoerg Roedel * The present bit is always zero for page structure faults on real 50883b0a91SJoerg Roedel * hardware. 51883b0a91SJoerg Roedel */ 52883b0a91SJoerg Roedel if (svm->vmcb->control.exit_info_1 & (2ULL << 32)) 53883b0a91SJoerg Roedel svm->vmcb->control.exit_info_1 &= ~1; 54883b0a91SJoerg Roedel 55883b0a91SJoerg Roedel nested_svm_vmexit(svm); 56883b0a91SJoerg Roedel } 57883b0a91SJoerg Roedel 58883b0a91SJoerg Roedel static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index) 59883b0a91SJoerg Roedel { 60883b0a91SJoerg Roedel struct vcpu_svm *svm = to_svm(vcpu); 61883b0a91SJoerg Roedel u64 cr3 = svm->nested.nested_cr3; 62883b0a91SJoerg Roedel u64 pdpte; 63883b0a91SJoerg Roedel int ret; 64883b0a91SJoerg Roedel 65883b0a91SJoerg Roedel ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(__sme_clr(cr3)), &pdpte, 66883b0a91SJoerg Roedel offset_in_page(cr3) + index * 8, 8); 67883b0a91SJoerg Roedel if (ret) 68883b0a91SJoerg Roedel return 0; 69883b0a91SJoerg Roedel return pdpte; 70883b0a91SJoerg Roedel } 71883b0a91SJoerg Roedel 72883b0a91SJoerg Roedel static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu) 73883b0a91SJoerg Roedel { 74883b0a91SJoerg Roedel struct vcpu_svm *svm = to_svm(vcpu); 75883b0a91SJoerg Roedel 76883b0a91SJoerg Roedel return svm->nested.nested_cr3; 77883b0a91SJoerg Roedel } 78883b0a91SJoerg Roedel 79883b0a91SJoerg Roedel static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu) 80883b0a91SJoerg Roedel { 81883b0a91SJoerg Roedel WARN_ON(mmu_is_nested(vcpu)); 82883b0a91SJoerg Roedel 83883b0a91SJoerg Roedel vcpu->arch.mmu = &vcpu->arch.guest_mmu; 84883b0a91SJoerg Roedel kvm_init_shadow_mmu(vcpu); 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 vcpu->arch.mmu->shadow_root_level = kvm_x86_ops.get_tdp_level(vcpu); 89883b0a91SJoerg Roedel reset_shadow_zero_bits_mask(vcpu, vcpu->arch.mmu); 90883b0a91SJoerg Roedel vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; 91883b0a91SJoerg Roedel } 92883b0a91SJoerg Roedel 93883b0a91SJoerg Roedel static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu) 94883b0a91SJoerg Roedel { 95883b0a91SJoerg Roedel vcpu->arch.mmu = &vcpu->arch.root_mmu; 96883b0a91SJoerg Roedel vcpu->arch.walk_mmu = &vcpu->arch.root_mmu; 97883b0a91SJoerg Roedel } 98883b0a91SJoerg Roedel 99883b0a91SJoerg Roedel void recalc_intercepts(struct vcpu_svm *svm) 100883b0a91SJoerg Roedel { 101883b0a91SJoerg Roedel struct vmcb_control_area *c, *h; 102883b0a91SJoerg Roedel struct nested_state *g; 103883b0a91SJoerg Roedel 104883b0a91SJoerg Roedel mark_dirty(svm->vmcb, VMCB_INTERCEPTS); 105883b0a91SJoerg Roedel 106883b0a91SJoerg Roedel if (!is_guest_mode(&svm->vcpu)) 107883b0a91SJoerg Roedel return; 108883b0a91SJoerg Roedel 109883b0a91SJoerg Roedel c = &svm->vmcb->control; 110883b0a91SJoerg Roedel h = &svm->nested.hsave->control; 111883b0a91SJoerg Roedel g = &svm->nested; 112883b0a91SJoerg Roedel 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 118883b0a91SJoerg Roedel if (svm->vcpu.arch.hflags & HF_VINTR_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 140883b0a91SJoerg Roedel static void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb) 141883b0a91SJoerg Roedel { 142883b0a91SJoerg Roedel struct vmcb_control_area *dst = &dst_vmcb->control; 143883b0a91SJoerg Roedel struct vmcb_control_area *from = &from_vmcb->control; 144883b0a91SJoerg Roedel 145883b0a91SJoerg Roedel dst->intercept_cr = from->intercept_cr; 146883b0a91SJoerg Roedel dst->intercept_dr = from->intercept_dr; 147883b0a91SJoerg Roedel dst->intercept_exceptions = from->intercept_exceptions; 148883b0a91SJoerg Roedel dst->intercept = from->intercept; 149883b0a91SJoerg Roedel dst->iopm_base_pa = from->iopm_base_pa; 150883b0a91SJoerg Roedel dst->msrpm_base_pa = from->msrpm_base_pa; 151883b0a91SJoerg Roedel dst->tsc_offset = from->tsc_offset; 152883b0a91SJoerg Roedel dst->asid = from->asid; 153883b0a91SJoerg Roedel dst->tlb_ctl = from->tlb_ctl; 154883b0a91SJoerg Roedel dst->int_ctl = from->int_ctl; 155883b0a91SJoerg Roedel dst->int_vector = from->int_vector; 156883b0a91SJoerg Roedel dst->int_state = from->int_state; 157883b0a91SJoerg Roedel dst->exit_code = from->exit_code; 158883b0a91SJoerg Roedel dst->exit_code_hi = from->exit_code_hi; 159883b0a91SJoerg Roedel dst->exit_info_1 = from->exit_info_1; 160883b0a91SJoerg Roedel dst->exit_info_2 = from->exit_info_2; 161883b0a91SJoerg Roedel dst->exit_int_info = from->exit_int_info; 162883b0a91SJoerg Roedel dst->exit_int_info_err = from->exit_int_info_err; 163883b0a91SJoerg Roedel dst->nested_ctl = from->nested_ctl; 164883b0a91SJoerg Roedel dst->event_inj = from->event_inj; 165883b0a91SJoerg Roedel dst->event_inj_err = from->event_inj_err; 166883b0a91SJoerg Roedel dst->nested_cr3 = from->nested_cr3; 167883b0a91SJoerg Roedel dst->virt_ext = from->virt_ext; 168883b0a91SJoerg Roedel dst->pause_filter_count = from->pause_filter_count; 169883b0a91SJoerg Roedel dst->pause_filter_thresh = from->pause_filter_thresh; 170883b0a91SJoerg Roedel } 171883b0a91SJoerg Roedel 172883b0a91SJoerg Roedel static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm) 173883b0a91SJoerg Roedel { 174883b0a91SJoerg Roedel /* 175883b0a91SJoerg Roedel * This function merges the msr permission bitmaps of kvm and the 176883b0a91SJoerg Roedel * nested vmcb. It is optimized in that it only merges the parts where 177883b0a91SJoerg Roedel * the kvm msr permission bitmap may contain zero bits 178883b0a91SJoerg Roedel */ 179883b0a91SJoerg Roedel int i; 180883b0a91SJoerg Roedel 181883b0a91SJoerg Roedel if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT))) 182883b0a91SJoerg Roedel return true; 183883b0a91SJoerg Roedel 184883b0a91SJoerg Roedel for (i = 0; i < MSRPM_OFFSETS; i++) { 185883b0a91SJoerg Roedel u32 value, p; 186883b0a91SJoerg Roedel u64 offset; 187883b0a91SJoerg Roedel 188883b0a91SJoerg Roedel if (msrpm_offsets[i] == 0xffffffff) 189883b0a91SJoerg Roedel break; 190883b0a91SJoerg Roedel 191883b0a91SJoerg Roedel p = msrpm_offsets[i]; 192883b0a91SJoerg Roedel offset = svm->nested.vmcb_msrpm + (p * 4); 193883b0a91SJoerg Roedel 194883b0a91SJoerg Roedel if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4)) 195883b0a91SJoerg Roedel return false; 196883b0a91SJoerg Roedel 197883b0a91SJoerg Roedel svm->nested.msrpm[p] = svm->msrpm[p] | value; 198883b0a91SJoerg Roedel } 199883b0a91SJoerg Roedel 200883b0a91SJoerg Roedel svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm)); 201883b0a91SJoerg Roedel 202883b0a91SJoerg Roedel return true; 203883b0a91SJoerg Roedel } 204883b0a91SJoerg Roedel 205883b0a91SJoerg Roedel static bool nested_vmcb_checks(struct vmcb *vmcb) 206883b0a91SJoerg Roedel { 207883b0a91SJoerg Roedel if ((vmcb->save.efer & EFER_SVME) == 0) 208883b0a91SJoerg Roedel return false; 209883b0a91SJoerg Roedel 2104f233371SKrish Sadhukhan if (((vmcb->save.cr0 & X86_CR0_CD) == 0) && 2114f233371SKrish Sadhukhan (vmcb->save.cr0 & X86_CR0_NW)) 2124f233371SKrish Sadhukhan return false; 2134f233371SKrish Sadhukhan 214883b0a91SJoerg Roedel if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0) 215883b0a91SJoerg Roedel return false; 216883b0a91SJoerg Roedel 217883b0a91SJoerg Roedel if (vmcb->control.asid == 0) 218883b0a91SJoerg Roedel return false; 219883b0a91SJoerg Roedel 220883b0a91SJoerg Roedel if ((vmcb->control.nested_ctl & SVM_NESTED_CTL_NP_ENABLE) && 221883b0a91SJoerg Roedel !npt_enabled) 222883b0a91SJoerg Roedel return false; 223883b0a91SJoerg Roedel 224883b0a91SJoerg Roedel return true; 225883b0a91SJoerg Roedel } 226883b0a91SJoerg Roedel 227883b0a91SJoerg Roedel void enter_svm_guest_mode(struct vcpu_svm *svm, u64 vmcb_gpa, 228883b0a91SJoerg Roedel struct vmcb *nested_vmcb, struct kvm_host_map *map) 229883b0a91SJoerg Roedel { 230883b0a91SJoerg Roedel bool evaluate_pending_interrupts = 231883b0a91SJoerg Roedel is_intercept(svm, INTERCEPT_VINTR) || 232883b0a91SJoerg Roedel is_intercept(svm, INTERCEPT_IRET); 233883b0a91SJoerg Roedel 234883b0a91SJoerg Roedel if (kvm_get_rflags(&svm->vcpu) & X86_EFLAGS_IF) 235883b0a91SJoerg Roedel svm->vcpu.arch.hflags |= HF_HIF_MASK; 236883b0a91SJoerg Roedel else 237883b0a91SJoerg Roedel svm->vcpu.arch.hflags &= ~HF_HIF_MASK; 238883b0a91SJoerg Roedel 239883b0a91SJoerg Roedel if (nested_vmcb->control.nested_ctl & SVM_NESTED_CTL_NP_ENABLE) { 240883b0a91SJoerg Roedel svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3; 241883b0a91SJoerg Roedel nested_svm_init_mmu_context(&svm->vcpu); 242883b0a91SJoerg Roedel } 243883b0a91SJoerg Roedel 244883b0a91SJoerg Roedel /* Load the nested guest state */ 245883b0a91SJoerg Roedel svm->vmcb->save.es = nested_vmcb->save.es; 246883b0a91SJoerg Roedel svm->vmcb->save.cs = nested_vmcb->save.cs; 247883b0a91SJoerg Roedel svm->vmcb->save.ss = nested_vmcb->save.ss; 248883b0a91SJoerg Roedel svm->vmcb->save.ds = nested_vmcb->save.ds; 249883b0a91SJoerg Roedel svm->vmcb->save.gdtr = nested_vmcb->save.gdtr; 250883b0a91SJoerg Roedel svm->vmcb->save.idtr = nested_vmcb->save.idtr; 251883b0a91SJoerg Roedel kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags); 252883b0a91SJoerg Roedel svm_set_efer(&svm->vcpu, nested_vmcb->save.efer); 253883b0a91SJoerg Roedel svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0); 254883b0a91SJoerg Roedel svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4); 255883b0a91SJoerg Roedel if (npt_enabled) { 256883b0a91SJoerg Roedel svm->vmcb->save.cr3 = nested_vmcb->save.cr3; 257883b0a91SJoerg Roedel svm->vcpu.arch.cr3 = nested_vmcb->save.cr3; 258883b0a91SJoerg Roedel } else 259883b0a91SJoerg Roedel (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3); 260883b0a91SJoerg Roedel 261883b0a91SJoerg Roedel /* Guest paging mode is active - reset mmu */ 262883b0a91SJoerg Roedel kvm_mmu_reset_context(&svm->vcpu); 263883b0a91SJoerg Roedel 264883b0a91SJoerg Roedel svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2; 265883b0a91SJoerg Roedel kvm_rax_write(&svm->vcpu, nested_vmcb->save.rax); 266883b0a91SJoerg Roedel kvm_rsp_write(&svm->vcpu, nested_vmcb->save.rsp); 267883b0a91SJoerg Roedel kvm_rip_write(&svm->vcpu, nested_vmcb->save.rip); 268883b0a91SJoerg Roedel 269883b0a91SJoerg Roedel /* In case we don't even reach vcpu_run, the fields are not updated */ 270883b0a91SJoerg Roedel svm->vmcb->save.rax = nested_vmcb->save.rax; 271883b0a91SJoerg Roedel svm->vmcb->save.rsp = nested_vmcb->save.rsp; 272883b0a91SJoerg Roedel svm->vmcb->save.rip = nested_vmcb->save.rip; 273883b0a91SJoerg Roedel svm->vmcb->save.dr7 = nested_vmcb->save.dr7; 274883b0a91SJoerg Roedel svm->vmcb->save.dr6 = nested_vmcb->save.dr6; 275883b0a91SJoerg Roedel svm->vmcb->save.cpl = nested_vmcb->save.cpl; 276883b0a91SJoerg Roedel 277883b0a91SJoerg Roedel svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL; 278883b0a91SJoerg Roedel svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL; 279883b0a91SJoerg Roedel 280883b0a91SJoerg Roedel /* cache intercepts */ 281883b0a91SJoerg Roedel svm->nested.intercept_cr = nested_vmcb->control.intercept_cr; 282883b0a91SJoerg Roedel svm->nested.intercept_dr = nested_vmcb->control.intercept_dr; 283883b0a91SJoerg Roedel svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions; 284883b0a91SJoerg Roedel svm->nested.intercept = nested_vmcb->control.intercept; 285883b0a91SJoerg Roedel 286f55ac304SSean Christopherson svm_flush_tlb(&svm->vcpu); 287883b0a91SJoerg Roedel svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK; 288883b0a91SJoerg Roedel if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK) 289883b0a91SJoerg Roedel svm->vcpu.arch.hflags |= HF_VINTR_MASK; 290883b0a91SJoerg Roedel else 291883b0a91SJoerg Roedel svm->vcpu.arch.hflags &= ~HF_VINTR_MASK; 292883b0a91SJoerg Roedel 293883b0a91SJoerg Roedel svm->vcpu.arch.tsc_offset += nested_vmcb->control.tsc_offset; 294883b0a91SJoerg Roedel svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset; 295883b0a91SJoerg Roedel 296883b0a91SJoerg Roedel svm->vmcb->control.virt_ext = nested_vmcb->control.virt_ext; 297883b0a91SJoerg Roedel svm->vmcb->control.int_vector = nested_vmcb->control.int_vector; 298883b0a91SJoerg Roedel svm->vmcb->control.int_state = nested_vmcb->control.int_state; 299883b0a91SJoerg Roedel svm->vmcb->control.event_inj = nested_vmcb->control.event_inj; 300883b0a91SJoerg Roedel svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err; 301883b0a91SJoerg Roedel 302883b0a91SJoerg Roedel svm->vmcb->control.pause_filter_count = 303883b0a91SJoerg Roedel nested_vmcb->control.pause_filter_count; 304883b0a91SJoerg Roedel svm->vmcb->control.pause_filter_thresh = 305883b0a91SJoerg Roedel nested_vmcb->control.pause_filter_thresh; 306883b0a91SJoerg Roedel 307883b0a91SJoerg Roedel kvm_vcpu_unmap(&svm->vcpu, map, true); 308883b0a91SJoerg Roedel 309883b0a91SJoerg Roedel /* Enter Guest-Mode */ 310883b0a91SJoerg Roedel enter_guest_mode(&svm->vcpu); 311883b0a91SJoerg Roedel 312883b0a91SJoerg Roedel /* 313883b0a91SJoerg Roedel * Merge guest and host intercepts - must be called with vcpu in 314883b0a91SJoerg Roedel * guest-mode to take affect here 315883b0a91SJoerg Roedel */ 316883b0a91SJoerg Roedel recalc_intercepts(svm); 317883b0a91SJoerg Roedel 318883b0a91SJoerg Roedel svm->nested.vmcb = vmcb_gpa; 319883b0a91SJoerg Roedel 320883b0a91SJoerg Roedel /* 321883b0a91SJoerg Roedel * If L1 had a pending IRQ/NMI before executing VMRUN, 322883b0a91SJoerg Roedel * which wasn't delivered because it was disallowed (e.g. 323883b0a91SJoerg Roedel * interrupts disabled), L0 needs to evaluate if this pending 324883b0a91SJoerg Roedel * event should cause an exit from L2 to L1 or be delivered 325883b0a91SJoerg Roedel * directly to L2. 326883b0a91SJoerg Roedel * 327883b0a91SJoerg Roedel * Usually this would be handled by the processor noticing an 328883b0a91SJoerg Roedel * IRQ/NMI window request. However, VMRUN can unblock interrupts 329883b0a91SJoerg Roedel * by implicitly setting GIF, so force L0 to perform pending event 330883b0a91SJoerg Roedel * evaluation by requesting a KVM_REQ_EVENT. 331883b0a91SJoerg Roedel */ 332883b0a91SJoerg Roedel enable_gif(svm); 333883b0a91SJoerg Roedel if (unlikely(evaluate_pending_interrupts)) 334883b0a91SJoerg Roedel kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); 335883b0a91SJoerg Roedel 336883b0a91SJoerg Roedel mark_all_dirty(svm->vmcb); 337883b0a91SJoerg Roedel } 338883b0a91SJoerg Roedel 339883b0a91SJoerg Roedel int nested_svm_vmrun(struct vcpu_svm *svm) 340883b0a91SJoerg Roedel { 341883b0a91SJoerg Roedel int ret; 342883b0a91SJoerg Roedel struct vmcb *nested_vmcb; 343883b0a91SJoerg Roedel struct vmcb *hsave = svm->nested.hsave; 344883b0a91SJoerg Roedel struct vmcb *vmcb = svm->vmcb; 345883b0a91SJoerg Roedel struct kvm_host_map map; 346883b0a91SJoerg Roedel u64 vmcb_gpa; 347883b0a91SJoerg Roedel 348*7c67f546SPaolo Bonzini if (is_smm(&svm->vcpu)) { 349*7c67f546SPaolo Bonzini kvm_queue_exception(&svm->vcpu, UD_VECTOR); 350*7c67f546SPaolo Bonzini return 1; 351*7c67f546SPaolo Bonzini } 352883b0a91SJoerg Roedel 353*7c67f546SPaolo Bonzini vmcb_gpa = svm->vmcb->save.rax; 354883b0a91SJoerg Roedel ret = kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(vmcb_gpa), &map); 355883b0a91SJoerg Roedel if (ret == -EINVAL) { 356883b0a91SJoerg Roedel kvm_inject_gp(&svm->vcpu, 0); 357883b0a91SJoerg Roedel return 1; 358883b0a91SJoerg Roedel } else if (ret) { 359883b0a91SJoerg Roedel return kvm_skip_emulated_instruction(&svm->vcpu); 360883b0a91SJoerg Roedel } 361883b0a91SJoerg Roedel 362883b0a91SJoerg Roedel ret = kvm_skip_emulated_instruction(&svm->vcpu); 363883b0a91SJoerg Roedel 364883b0a91SJoerg Roedel nested_vmcb = map.hva; 365883b0a91SJoerg Roedel 366883b0a91SJoerg Roedel if (!nested_vmcb_checks(nested_vmcb)) { 367883b0a91SJoerg Roedel nested_vmcb->control.exit_code = SVM_EXIT_ERR; 368883b0a91SJoerg Roedel nested_vmcb->control.exit_code_hi = 0; 369883b0a91SJoerg Roedel nested_vmcb->control.exit_info_1 = 0; 370883b0a91SJoerg Roedel nested_vmcb->control.exit_info_2 = 0; 371883b0a91SJoerg Roedel 372883b0a91SJoerg Roedel kvm_vcpu_unmap(&svm->vcpu, &map, true); 373883b0a91SJoerg Roedel 374883b0a91SJoerg Roedel return ret; 375883b0a91SJoerg Roedel } 376883b0a91SJoerg Roedel 377883b0a91SJoerg Roedel trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa, 378883b0a91SJoerg Roedel nested_vmcb->save.rip, 379883b0a91SJoerg Roedel nested_vmcb->control.int_ctl, 380883b0a91SJoerg Roedel nested_vmcb->control.event_inj, 381883b0a91SJoerg Roedel nested_vmcb->control.nested_ctl); 382883b0a91SJoerg Roedel 383883b0a91SJoerg Roedel trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff, 384883b0a91SJoerg Roedel nested_vmcb->control.intercept_cr >> 16, 385883b0a91SJoerg Roedel nested_vmcb->control.intercept_exceptions, 386883b0a91SJoerg Roedel nested_vmcb->control.intercept); 387883b0a91SJoerg Roedel 388883b0a91SJoerg Roedel /* Clear internal status */ 389883b0a91SJoerg Roedel kvm_clear_exception_queue(&svm->vcpu); 390883b0a91SJoerg Roedel kvm_clear_interrupt_queue(&svm->vcpu); 391883b0a91SJoerg Roedel 392883b0a91SJoerg Roedel /* 393883b0a91SJoerg Roedel * Save the old vmcb, so we don't need to pick what we save, but can 394883b0a91SJoerg Roedel * restore everything when a VMEXIT occurs 395883b0a91SJoerg Roedel */ 396883b0a91SJoerg Roedel hsave->save.es = vmcb->save.es; 397883b0a91SJoerg Roedel hsave->save.cs = vmcb->save.cs; 398883b0a91SJoerg Roedel hsave->save.ss = vmcb->save.ss; 399883b0a91SJoerg Roedel hsave->save.ds = vmcb->save.ds; 400883b0a91SJoerg Roedel hsave->save.gdtr = vmcb->save.gdtr; 401883b0a91SJoerg Roedel hsave->save.idtr = vmcb->save.idtr; 402883b0a91SJoerg Roedel hsave->save.efer = svm->vcpu.arch.efer; 403883b0a91SJoerg Roedel hsave->save.cr0 = kvm_read_cr0(&svm->vcpu); 404883b0a91SJoerg Roedel hsave->save.cr4 = svm->vcpu.arch.cr4; 405883b0a91SJoerg Roedel hsave->save.rflags = kvm_get_rflags(&svm->vcpu); 406883b0a91SJoerg Roedel hsave->save.rip = kvm_rip_read(&svm->vcpu); 407883b0a91SJoerg Roedel hsave->save.rsp = vmcb->save.rsp; 408883b0a91SJoerg Roedel hsave->save.rax = vmcb->save.rax; 409883b0a91SJoerg Roedel if (npt_enabled) 410883b0a91SJoerg Roedel hsave->save.cr3 = vmcb->save.cr3; 411883b0a91SJoerg Roedel else 412883b0a91SJoerg Roedel hsave->save.cr3 = kvm_read_cr3(&svm->vcpu); 413883b0a91SJoerg Roedel 414883b0a91SJoerg Roedel copy_vmcb_control_area(hsave, vmcb); 415883b0a91SJoerg Roedel 416883b0a91SJoerg Roedel enter_svm_guest_mode(svm, vmcb_gpa, nested_vmcb, &map); 417883b0a91SJoerg Roedel 418883b0a91SJoerg Roedel if (!nested_svm_vmrun_msrpm(svm)) { 419883b0a91SJoerg Roedel svm->vmcb->control.exit_code = SVM_EXIT_ERR; 420883b0a91SJoerg Roedel svm->vmcb->control.exit_code_hi = 0; 421883b0a91SJoerg Roedel svm->vmcb->control.exit_info_1 = 0; 422883b0a91SJoerg Roedel svm->vmcb->control.exit_info_2 = 0; 423883b0a91SJoerg Roedel 424883b0a91SJoerg Roedel nested_svm_vmexit(svm); 425883b0a91SJoerg Roedel } 426883b0a91SJoerg Roedel 427883b0a91SJoerg Roedel return ret; 428883b0a91SJoerg Roedel } 429883b0a91SJoerg Roedel 430883b0a91SJoerg Roedel void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb) 431883b0a91SJoerg Roedel { 432883b0a91SJoerg Roedel to_vmcb->save.fs = from_vmcb->save.fs; 433883b0a91SJoerg Roedel to_vmcb->save.gs = from_vmcb->save.gs; 434883b0a91SJoerg Roedel to_vmcb->save.tr = from_vmcb->save.tr; 435883b0a91SJoerg Roedel to_vmcb->save.ldtr = from_vmcb->save.ldtr; 436883b0a91SJoerg Roedel to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base; 437883b0a91SJoerg Roedel to_vmcb->save.star = from_vmcb->save.star; 438883b0a91SJoerg Roedel to_vmcb->save.lstar = from_vmcb->save.lstar; 439883b0a91SJoerg Roedel to_vmcb->save.cstar = from_vmcb->save.cstar; 440883b0a91SJoerg Roedel to_vmcb->save.sfmask = from_vmcb->save.sfmask; 441883b0a91SJoerg Roedel to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs; 442883b0a91SJoerg Roedel to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp; 443883b0a91SJoerg Roedel to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip; 444883b0a91SJoerg Roedel } 445883b0a91SJoerg Roedel 446883b0a91SJoerg Roedel int nested_svm_vmexit(struct vcpu_svm *svm) 447883b0a91SJoerg Roedel { 448883b0a91SJoerg Roedel int rc; 449883b0a91SJoerg Roedel struct vmcb *nested_vmcb; 450883b0a91SJoerg Roedel struct vmcb *hsave = svm->nested.hsave; 451883b0a91SJoerg Roedel struct vmcb *vmcb = svm->vmcb; 452883b0a91SJoerg Roedel struct kvm_host_map map; 453883b0a91SJoerg Roedel 454883b0a91SJoerg Roedel trace_kvm_nested_vmexit_inject(vmcb->control.exit_code, 455883b0a91SJoerg Roedel vmcb->control.exit_info_1, 456883b0a91SJoerg Roedel vmcb->control.exit_info_2, 457883b0a91SJoerg Roedel vmcb->control.exit_int_info, 458883b0a91SJoerg Roedel vmcb->control.exit_int_info_err, 459883b0a91SJoerg Roedel KVM_ISA_SVM); 460883b0a91SJoerg Roedel 461883b0a91SJoerg Roedel rc = kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(svm->nested.vmcb), &map); 462883b0a91SJoerg Roedel if (rc) { 463883b0a91SJoerg Roedel if (rc == -EINVAL) 464883b0a91SJoerg Roedel kvm_inject_gp(&svm->vcpu, 0); 465883b0a91SJoerg Roedel return 1; 466883b0a91SJoerg Roedel } 467883b0a91SJoerg Roedel 468883b0a91SJoerg Roedel nested_vmcb = map.hva; 469883b0a91SJoerg Roedel 470883b0a91SJoerg Roedel /* Exit Guest-Mode */ 471883b0a91SJoerg Roedel leave_guest_mode(&svm->vcpu); 472883b0a91SJoerg Roedel svm->nested.vmcb = 0; 473883b0a91SJoerg Roedel 474883b0a91SJoerg Roedel /* Give the current vmcb to the guest */ 475883b0a91SJoerg Roedel disable_gif(svm); 476883b0a91SJoerg Roedel 477883b0a91SJoerg Roedel nested_vmcb->save.es = vmcb->save.es; 478883b0a91SJoerg Roedel nested_vmcb->save.cs = vmcb->save.cs; 479883b0a91SJoerg Roedel nested_vmcb->save.ss = vmcb->save.ss; 480883b0a91SJoerg Roedel nested_vmcb->save.ds = vmcb->save.ds; 481883b0a91SJoerg Roedel nested_vmcb->save.gdtr = vmcb->save.gdtr; 482883b0a91SJoerg Roedel nested_vmcb->save.idtr = vmcb->save.idtr; 483883b0a91SJoerg Roedel nested_vmcb->save.efer = svm->vcpu.arch.efer; 484883b0a91SJoerg Roedel nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu); 485883b0a91SJoerg Roedel nested_vmcb->save.cr3 = kvm_read_cr3(&svm->vcpu); 486883b0a91SJoerg Roedel nested_vmcb->save.cr2 = vmcb->save.cr2; 487883b0a91SJoerg Roedel nested_vmcb->save.cr4 = svm->vcpu.arch.cr4; 488883b0a91SJoerg Roedel nested_vmcb->save.rflags = kvm_get_rflags(&svm->vcpu); 489883b0a91SJoerg Roedel nested_vmcb->save.rip = vmcb->save.rip; 490883b0a91SJoerg Roedel nested_vmcb->save.rsp = vmcb->save.rsp; 491883b0a91SJoerg Roedel nested_vmcb->save.rax = vmcb->save.rax; 492883b0a91SJoerg Roedel nested_vmcb->save.dr7 = vmcb->save.dr7; 493883b0a91SJoerg Roedel nested_vmcb->save.dr6 = vmcb->save.dr6; 494883b0a91SJoerg Roedel nested_vmcb->save.cpl = vmcb->save.cpl; 495883b0a91SJoerg Roedel 496883b0a91SJoerg Roedel nested_vmcb->control.int_ctl = vmcb->control.int_ctl; 497883b0a91SJoerg Roedel nested_vmcb->control.int_vector = vmcb->control.int_vector; 498883b0a91SJoerg Roedel nested_vmcb->control.int_state = vmcb->control.int_state; 499883b0a91SJoerg Roedel nested_vmcb->control.exit_code = vmcb->control.exit_code; 500883b0a91SJoerg Roedel nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi; 501883b0a91SJoerg Roedel nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1; 502883b0a91SJoerg Roedel nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2; 503883b0a91SJoerg Roedel nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info; 504883b0a91SJoerg Roedel nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err; 505883b0a91SJoerg Roedel 506883b0a91SJoerg Roedel if (svm->nrips_enabled) 507883b0a91SJoerg Roedel nested_vmcb->control.next_rip = vmcb->control.next_rip; 508883b0a91SJoerg Roedel 509883b0a91SJoerg Roedel /* 510883b0a91SJoerg Roedel * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have 511883b0a91SJoerg Roedel * to make sure that we do not lose injected events. So check event_inj 512883b0a91SJoerg Roedel * here and copy it to exit_int_info if it is valid. 513883b0a91SJoerg Roedel * Exit_int_info and event_inj can't be both valid because the case 514883b0a91SJoerg Roedel * below only happens on a VMRUN instruction intercept which has 515883b0a91SJoerg Roedel * no valid exit_int_info set. 516883b0a91SJoerg Roedel */ 517883b0a91SJoerg Roedel if (vmcb->control.event_inj & SVM_EVTINJ_VALID) { 518883b0a91SJoerg Roedel struct vmcb_control_area *nc = &nested_vmcb->control; 519883b0a91SJoerg Roedel 520883b0a91SJoerg Roedel nc->exit_int_info = vmcb->control.event_inj; 521883b0a91SJoerg Roedel nc->exit_int_info_err = vmcb->control.event_inj_err; 522883b0a91SJoerg Roedel } 523883b0a91SJoerg Roedel 524883b0a91SJoerg Roedel nested_vmcb->control.tlb_ctl = 0; 525883b0a91SJoerg Roedel nested_vmcb->control.event_inj = 0; 526883b0a91SJoerg Roedel nested_vmcb->control.event_inj_err = 0; 527883b0a91SJoerg Roedel 528883b0a91SJoerg Roedel nested_vmcb->control.pause_filter_count = 529883b0a91SJoerg Roedel svm->vmcb->control.pause_filter_count; 530883b0a91SJoerg Roedel nested_vmcb->control.pause_filter_thresh = 531883b0a91SJoerg Roedel svm->vmcb->control.pause_filter_thresh; 532883b0a91SJoerg Roedel 533883b0a91SJoerg Roedel /* We always set V_INTR_MASKING and remember the old value in hflags */ 534883b0a91SJoerg Roedel if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK)) 535883b0a91SJoerg Roedel nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK; 536883b0a91SJoerg Roedel 537883b0a91SJoerg Roedel /* Restore the original control entries */ 538883b0a91SJoerg Roedel copy_vmcb_control_area(vmcb, hsave); 539883b0a91SJoerg Roedel 540883b0a91SJoerg Roedel svm->vcpu.arch.tsc_offset = svm->vmcb->control.tsc_offset; 541883b0a91SJoerg Roedel kvm_clear_exception_queue(&svm->vcpu); 542883b0a91SJoerg Roedel kvm_clear_interrupt_queue(&svm->vcpu); 543883b0a91SJoerg Roedel 544883b0a91SJoerg Roedel svm->nested.nested_cr3 = 0; 545883b0a91SJoerg Roedel 546883b0a91SJoerg Roedel /* Restore selected save entries */ 547883b0a91SJoerg Roedel svm->vmcb->save.es = hsave->save.es; 548883b0a91SJoerg Roedel svm->vmcb->save.cs = hsave->save.cs; 549883b0a91SJoerg Roedel svm->vmcb->save.ss = hsave->save.ss; 550883b0a91SJoerg Roedel svm->vmcb->save.ds = hsave->save.ds; 551883b0a91SJoerg Roedel svm->vmcb->save.gdtr = hsave->save.gdtr; 552883b0a91SJoerg Roedel svm->vmcb->save.idtr = hsave->save.idtr; 553883b0a91SJoerg Roedel kvm_set_rflags(&svm->vcpu, hsave->save.rflags); 554883b0a91SJoerg Roedel svm_set_efer(&svm->vcpu, hsave->save.efer); 555883b0a91SJoerg Roedel svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE); 556883b0a91SJoerg Roedel svm_set_cr4(&svm->vcpu, hsave->save.cr4); 557883b0a91SJoerg Roedel if (npt_enabled) { 558883b0a91SJoerg Roedel svm->vmcb->save.cr3 = hsave->save.cr3; 559883b0a91SJoerg Roedel svm->vcpu.arch.cr3 = hsave->save.cr3; 560883b0a91SJoerg Roedel } else { 561883b0a91SJoerg Roedel (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3); 562883b0a91SJoerg Roedel } 563883b0a91SJoerg Roedel kvm_rax_write(&svm->vcpu, hsave->save.rax); 564883b0a91SJoerg Roedel kvm_rsp_write(&svm->vcpu, hsave->save.rsp); 565883b0a91SJoerg Roedel kvm_rip_write(&svm->vcpu, hsave->save.rip); 566883b0a91SJoerg Roedel svm->vmcb->save.dr7 = 0; 567883b0a91SJoerg Roedel svm->vmcb->save.cpl = 0; 568883b0a91SJoerg Roedel svm->vmcb->control.exit_int_info = 0; 569883b0a91SJoerg Roedel 570883b0a91SJoerg Roedel mark_all_dirty(svm->vmcb); 571883b0a91SJoerg Roedel 572883b0a91SJoerg Roedel kvm_vcpu_unmap(&svm->vcpu, &map, true); 573883b0a91SJoerg Roedel 574883b0a91SJoerg Roedel nested_svm_uninit_mmu_context(&svm->vcpu); 575883b0a91SJoerg Roedel kvm_mmu_reset_context(&svm->vcpu); 576883b0a91SJoerg Roedel kvm_mmu_load(&svm->vcpu); 577883b0a91SJoerg Roedel 578883b0a91SJoerg Roedel /* 579883b0a91SJoerg Roedel * Drop what we picked up for L2 via svm_complete_interrupts() so it 580883b0a91SJoerg Roedel * doesn't end up in L1. 581883b0a91SJoerg Roedel */ 582883b0a91SJoerg Roedel svm->vcpu.arch.nmi_injected = false; 583883b0a91SJoerg Roedel kvm_clear_exception_queue(&svm->vcpu); 584883b0a91SJoerg Roedel kvm_clear_interrupt_queue(&svm->vcpu); 585883b0a91SJoerg Roedel 586883b0a91SJoerg Roedel return 0; 587883b0a91SJoerg Roedel } 588883b0a91SJoerg Roedel 589883b0a91SJoerg Roedel static int nested_svm_exit_handled_msr(struct vcpu_svm *svm) 590883b0a91SJoerg Roedel { 591883b0a91SJoerg Roedel u32 offset, msr, value; 592883b0a91SJoerg Roedel int write, mask; 593883b0a91SJoerg Roedel 594883b0a91SJoerg Roedel if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT))) 595883b0a91SJoerg Roedel return NESTED_EXIT_HOST; 596883b0a91SJoerg Roedel 597883b0a91SJoerg Roedel msr = svm->vcpu.arch.regs[VCPU_REGS_RCX]; 598883b0a91SJoerg Roedel offset = svm_msrpm_offset(msr); 599883b0a91SJoerg Roedel write = svm->vmcb->control.exit_info_1 & 1; 600883b0a91SJoerg Roedel mask = 1 << ((2 * (msr & 0xf)) + write); 601883b0a91SJoerg Roedel 602883b0a91SJoerg Roedel if (offset == MSR_INVALID) 603883b0a91SJoerg Roedel return NESTED_EXIT_DONE; 604883b0a91SJoerg Roedel 605883b0a91SJoerg Roedel /* Offset is in 32 bit units but need in 8 bit units */ 606883b0a91SJoerg Roedel offset *= 4; 607883b0a91SJoerg Roedel 608883b0a91SJoerg Roedel if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.vmcb_msrpm + offset, &value, 4)) 609883b0a91SJoerg Roedel return NESTED_EXIT_DONE; 610883b0a91SJoerg Roedel 611883b0a91SJoerg Roedel return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST; 612883b0a91SJoerg Roedel } 613883b0a91SJoerg Roedel 614883b0a91SJoerg Roedel /* DB exceptions for our internal use must not cause vmexit */ 615883b0a91SJoerg Roedel static int nested_svm_intercept_db(struct vcpu_svm *svm) 616883b0a91SJoerg Roedel { 617883b0a91SJoerg Roedel unsigned long dr6; 618883b0a91SJoerg Roedel 619883b0a91SJoerg Roedel /* if we're not singlestepping, it's not ours */ 620883b0a91SJoerg Roedel if (!svm->nmi_singlestep) 621883b0a91SJoerg Roedel return NESTED_EXIT_DONE; 622883b0a91SJoerg Roedel 623883b0a91SJoerg Roedel /* if it's not a singlestep exception, it's not ours */ 624883b0a91SJoerg Roedel if (kvm_get_dr(&svm->vcpu, 6, &dr6)) 625883b0a91SJoerg Roedel return NESTED_EXIT_DONE; 626883b0a91SJoerg Roedel if (!(dr6 & DR6_BS)) 627883b0a91SJoerg Roedel return NESTED_EXIT_DONE; 628883b0a91SJoerg Roedel 629883b0a91SJoerg Roedel /* if the guest is singlestepping, it should get the vmexit */ 630883b0a91SJoerg Roedel if (svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF) { 631883b0a91SJoerg Roedel disable_nmi_singlestep(svm); 632883b0a91SJoerg Roedel return NESTED_EXIT_DONE; 633883b0a91SJoerg Roedel } 634883b0a91SJoerg Roedel 635883b0a91SJoerg Roedel /* it's ours, the nested hypervisor must not see this one */ 636883b0a91SJoerg Roedel return NESTED_EXIT_HOST; 637883b0a91SJoerg Roedel } 638883b0a91SJoerg Roedel 639883b0a91SJoerg Roedel static int nested_svm_intercept_ioio(struct vcpu_svm *svm) 640883b0a91SJoerg Roedel { 641883b0a91SJoerg Roedel unsigned port, size, iopm_len; 642883b0a91SJoerg Roedel u16 val, mask; 643883b0a91SJoerg Roedel u8 start_bit; 644883b0a91SJoerg Roedel u64 gpa; 645883b0a91SJoerg Roedel 646883b0a91SJoerg Roedel if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT))) 647883b0a91SJoerg Roedel return NESTED_EXIT_HOST; 648883b0a91SJoerg Roedel 649883b0a91SJoerg Roedel port = svm->vmcb->control.exit_info_1 >> 16; 650883b0a91SJoerg Roedel size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >> 651883b0a91SJoerg Roedel SVM_IOIO_SIZE_SHIFT; 652883b0a91SJoerg Roedel gpa = svm->nested.vmcb_iopm + (port / 8); 653883b0a91SJoerg Roedel start_bit = port % 8; 654883b0a91SJoerg Roedel iopm_len = (start_bit + size > 8) ? 2 : 1; 655883b0a91SJoerg Roedel mask = (0xf >> (4 - size)) << start_bit; 656883b0a91SJoerg Roedel val = 0; 657883b0a91SJoerg Roedel 658883b0a91SJoerg Roedel if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len)) 659883b0a91SJoerg Roedel return NESTED_EXIT_DONE; 660883b0a91SJoerg Roedel 661883b0a91SJoerg Roedel return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST; 662883b0a91SJoerg Roedel } 663883b0a91SJoerg Roedel 664883b0a91SJoerg Roedel static int nested_svm_intercept(struct vcpu_svm *svm) 665883b0a91SJoerg Roedel { 666883b0a91SJoerg Roedel u32 exit_code = svm->vmcb->control.exit_code; 667883b0a91SJoerg Roedel int vmexit = NESTED_EXIT_HOST; 668883b0a91SJoerg Roedel 669883b0a91SJoerg Roedel switch (exit_code) { 670883b0a91SJoerg Roedel case SVM_EXIT_MSR: 671883b0a91SJoerg Roedel vmexit = nested_svm_exit_handled_msr(svm); 672883b0a91SJoerg Roedel break; 673883b0a91SJoerg Roedel case SVM_EXIT_IOIO: 674883b0a91SJoerg Roedel vmexit = nested_svm_intercept_ioio(svm); 675883b0a91SJoerg Roedel break; 676883b0a91SJoerg Roedel case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: { 677883b0a91SJoerg Roedel u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0); 678883b0a91SJoerg Roedel if (svm->nested.intercept_cr & bit) 679883b0a91SJoerg Roedel vmexit = NESTED_EXIT_DONE; 680883b0a91SJoerg Roedel break; 681883b0a91SJoerg Roedel } 682883b0a91SJoerg Roedel case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: { 683883b0a91SJoerg Roedel u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0); 684883b0a91SJoerg Roedel if (svm->nested.intercept_dr & bit) 685883b0a91SJoerg Roedel vmexit = NESTED_EXIT_DONE; 686883b0a91SJoerg Roedel break; 687883b0a91SJoerg Roedel } 688883b0a91SJoerg Roedel case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: { 689883b0a91SJoerg Roedel u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE); 690883b0a91SJoerg Roedel if (svm->nested.intercept_exceptions & excp_bits) { 691883b0a91SJoerg Roedel if (exit_code == SVM_EXIT_EXCP_BASE + DB_VECTOR) 692883b0a91SJoerg Roedel vmexit = nested_svm_intercept_db(svm); 693883b0a91SJoerg Roedel else 694883b0a91SJoerg Roedel vmexit = NESTED_EXIT_DONE; 695883b0a91SJoerg Roedel } 696883b0a91SJoerg Roedel /* async page fault always cause vmexit */ 697883b0a91SJoerg Roedel else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) && 698883b0a91SJoerg Roedel svm->vcpu.arch.exception.nested_apf != 0) 699883b0a91SJoerg Roedel vmexit = NESTED_EXIT_DONE; 700883b0a91SJoerg Roedel break; 701883b0a91SJoerg Roedel } 702883b0a91SJoerg Roedel case SVM_EXIT_ERR: { 703883b0a91SJoerg Roedel vmexit = NESTED_EXIT_DONE; 704883b0a91SJoerg Roedel break; 705883b0a91SJoerg Roedel } 706883b0a91SJoerg Roedel default: { 707883b0a91SJoerg Roedel u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR); 708883b0a91SJoerg Roedel if (svm->nested.intercept & exit_bits) 709883b0a91SJoerg Roedel vmexit = NESTED_EXIT_DONE; 710883b0a91SJoerg Roedel } 711883b0a91SJoerg Roedel } 712883b0a91SJoerg Roedel 713883b0a91SJoerg Roedel return vmexit; 714883b0a91SJoerg Roedel } 715883b0a91SJoerg Roedel 716883b0a91SJoerg Roedel int nested_svm_exit_handled(struct vcpu_svm *svm) 717883b0a91SJoerg Roedel { 718883b0a91SJoerg Roedel int vmexit; 719883b0a91SJoerg Roedel 720883b0a91SJoerg Roedel vmexit = nested_svm_intercept(svm); 721883b0a91SJoerg Roedel 722883b0a91SJoerg Roedel if (vmexit == NESTED_EXIT_DONE) 723883b0a91SJoerg Roedel nested_svm_vmexit(svm); 724883b0a91SJoerg Roedel 725883b0a91SJoerg Roedel return vmexit; 726883b0a91SJoerg Roedel } 727883b0a91SJoerg Roedel 728883b0a91SJoerg Roedel int nested_svm_check_permissions(struct vcpu_svm *svm) 729883b0a91SJoerg Roedel { 730883b0a91SJoerg Roedel if (!(svm->vcpu.arch.efer & EFER_SVME) || 731883b0a91SJoerg Roedel !is_paging(&svm->vcpu)) { 732883b0a91SJoerg Roedel kvm_queue_exception(&svm->vcpu, UD_VECTOR); 733883b0a91SJoerg Roedel return 1; 734883b0a91SJoerg Roedel } 735883b0a91SJoerg Roedel 736883b0a91SJoerg Roedel if (svm->vmcb->save.cpl) { 737883b0a91SJoerg Roedel kvm_inject_gp(&svm->vcpu, 0); 738883b0a91SJoerg Roedel return 1; 739883b0a91SJoerg Roedel } 740883b0a91SJoerg Roedel 741883b0a91SJoerg Roedel return 0; 742883b0a91SJoerg Roedel } 743883b0a91SJoerg Roedel 744883b0a91SJoerg Roedel int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr, 745883b0a91SJoerg Roedel bool has_error_code, u32 error_code) 746883b0a91SJoerg Roedel { 747883b0a91SJoerg Roedel int vmexit; 748883b0a91SJoerg Roedel 749883b0a91SJoerg Roedel if (!is_guest_mode(&svm->vcpu)) 750883b0a91SJoerg Roedel return 0; 751883b0a91SJoerg Roedel 752883b0a91SJoerg Roedel vmexit = nested_svm_intercept(svm); 753883b0a91SJoerg Roedel if (vmexit != NESTED_EXIT_DONE) 754883b0a91SJoerg Roedel return 0; 755883b0a91SJoerg Roedel 756883b0a91SJoerg Roedel svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr; 757883b0a91SJoerg Roedel svm->vmcb->control.exit_code_hi = 0; 758883b0a91SJoerg Roedel svm->vmcb->control.exit_info_1 = error_code; 759883b0a91SJoerg Roedel 760883b0a91SJoerg Roedel /* 761883b0a91SJoerg Roedel * EXITINFO2 is undefined for all exception intercepts other 762883b0a91SJoerg Roedel * than #PF. 763883b0a91SJoerg Roedel */ 764883b0a91SJoerg Roedel if (svm->vcpu.arch.exception.nested_apf) 765883b0a91SJoerg Roedel svm->vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token; 766883b0a91SJoerg Roedel else if (svm->vcpu.arch.exception.has_payload) 767883b0a91SJoerg Roedel svm->vmcb->control.exit_info_2 = svm->vcpu.arch.exception.payload; 768883b0a91SJoerg Roedel else 769883b0a91SJoerg Roedel svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2; 770883b0a91SJoerg Roedel 771883b0a91SJoerg Roedel svm->nested.exit_required = true; 772883b0a91SJoerg Roedel return vmexit; 773883b0a91SJoerg Roedel } 774883b0a91SJoerg Roedel 775883b0a91SJoerg Roedel static void nested_svm_intr(struct vcpu_svm *svm) 776883b0a91SJoerg Roedel { 777883b0a91SJoerg Roedel svm->vmcb->control.exit_code = SVM_EXIT_INTR; 778883b0a91SJoerg Roedel svm->vmcb->control.exit_info_1 = 0; 779883b0a91SJoerg Roedel svm->vmcb->control.exit_info_2 = 0; 780883b0a91SJoerg Roedel 781883b0a91SJoerg Roedel /* nested_svm_vmexit this gets called afterwards from handle_exit */ 782883b0a91SJoerg Roedel svm->nested.exit_required = true; 783883b0a91SJoerg Roedel trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip); 784883b0a91SJoerg Roedel } 785883b0a91SJoerg Roedel 786883b0a91SJoerg Roedel static bool nested_exit_on_intr(struct vcpu_svm *svm) 787883b0a91SJoerg Roedel { 788883b0a91SJoerg Roedel return (svm->nested.intercept & 1ULL); 789883b0a91SJoerg Roedel } 790883b0a91SJoerg Roedel 79133b22172SPaolo Bonzini static int svm_check_nested_events(struct kvm_vcpu *vcpu) 792883b0a91SJoerg Roedel { 793883b0a91SJoerg Roedel struct vcpu_svm *svm = to_svm(vcpu); 794883b0a91SJoerg Roedel bool block_nested_events = 795883b0a91SJoerg Roedel kvm_event_needs_reinjection(vcpu) || svm->nested.exit_required; 796883b0a91SJoerg Roedel 797883b0a91SJoerg Roedel if (kvm_cpu_has_interrupt(vcpu) && nested_exit_on_intr(svm)) { 798883b0a91SJoerg Roedel if (block_nested_events) 799883b0a91SJoerg Roedel return -EBUSY; 800883b0a91SJoerg Roedel nested_svm_intr(svm); 801883b0a91SJoerg Roedel return 0; 802883b0a91SJoerg Roedel } 803883b0a91SJoerg Roedel 804883b0a91SJoerg Roedel return 0; 805883b0a91SJoerg Roedel } 806883b0a91SJoerg Roedel 807883b0a91SJoerg Roedel int nested_svm_exit_special(struct vcpu_svm *svm) 808883b0a91SJoerg Roedel { 809883b0a91SJoerg Roedel u32 exit_code = svm->vmcb->control.exit_code; 810883b0a91SJoerg Roedel 811883b0a91SJoerg Roedel switch (exit_code) { 812883b0a91SJoerg Roedel case SVM_EXIT_INTR: 813883b0a91SJoerg Roedel case SVM_EXIT_NMI: 814883b0a91SJoerg Roedel case SVM_EXIT_EXCP_BASE + MC_VECTOR: 815883b0a91SJoerg Roedel return NESTED_EXIT_HOST; 816883b0a91SJoerg Roedel case SVM_EXIT_NPF: 817883b0a91SJoerg Roedel /* For now we are always handling NPFs when using them */ 818883b0a91SJoerg Roedel if (npt_enabled) 819883b0a91SJoerg Roedel return NESTED_EXIT_HOST; 820883b0a91SJoerg Roedel break; 821883b0a91SJoerg Roedel case SVM_EXIT_EXCP_BASE + PF_VECTOR: 822883b0a91SJoerg Roedel /* When we're shadowing, trap PFs, but not async PF */ 823883b0a91SJoerg Roedel if (!npt_enabled && svm->vcpu.arch.apf.host_apf_reason == 0) 824883b0a91SJoerg Roedel return NESTED_EXIT_HOST; 825883b0a91SJoerg Roedel break; 826883b0a91SJoerg Roedel default: 827883b0a91SJoerg Roedel break; 828883b0a91SJoerg Roedel } 829883b0a91SJoerg Roedel 830883b0a91SJoerg Roedel return NESTED_EXIT_CONTINUE; 831883b0a91SJoerg Roedel } 83233b22172SPaolo Bonzini 83333b22172SPaolo Bonzini struct kvm_x86_nested_ops svm_nested_ops = { 83433b22172SPaolo Bonzini .check_events = svm_check_nested_events, 83533b22172SPaolo Bonzini }; 836