14f8d6632SMarc Zyngier /* 24f8d6632SMarc Zyngier * Copyright (C) 2012,2013 - ARM Ltd 34f8d6632SMarc Zyngier * Author: Marc Zyngier <marc.zyngier@arm.com> 44f8d6632SMarc Zyngier * 54f8d6632SMarc Zyngier * Derived from arch/arm/include/asm/kvm_host.h: 64f8d6632SMarc Zyngier * Copyright (C) 2012 - Virtual Open Systems and Columbia University 74f8d6632SMarc Zyngier * Author: Christoffer Dall <c.dall@virtualopensystems.com> 84f8d6632SMarc Zyngier * 94f8d6632SMarc Zyngier * This program is free software; you can redistribute it and/or modify 104f8d6632SMarc Zyngier * it under the terms of the GNU General Public License version 2 as 114f8d6632SMarc Zyngier * published by the Free Software Foundation. 124f8d6632SMarc Zyngier * 134f8d6632SMarc Zyngier * This program is distributed in the hope that it will be useful, 144f8d6632SMarc Zyngier * but WITHOUT ANY WARRANTY; without even the implied warranty of 154f8d6632SMarc Zyngier * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 164f8d6632SMarc Zyngier * GNU General Public License for more details. 174f8d6632SMarc Zyngier * 184f8d6632SMarc Zyngier * You should have received a copy of the GNU General Public License 194f8d6632SMarc Zyngier * along with this program. If not, see <http://www.gnu.org/licenses/>. 204f8d6632SMarc Zyngier */ 214f8d6632SMarc Zyngier 224f8d6632SMarc Zyngier #ifndef __ARM64_KVM_HOST_H__ 234f8d6632SMarc Zyngier #define __ARM64_KVM_HOST_H__ 244f8d6632SMarc Zyngier 2565647300SPaolo Bonzini #include <linux/types.h> 2665647300SPaolo Bonzini #include <linux/kvm_types.h> 2763a1e1c9SMark Rutland #include <asm/cpufeature.h> 284f5abad9SJames Morse #include <asm/daifflags.h> 2917eed27bSDave Martin #include <asm/fpsimd.h> 304f8d6632SMarc Zyngier #include <asm/kvm.h> 313a3604bcSMarc Zyngier #include <asm/kvm_asm.h> 324f8d6632SMarc Zyngier #include <asm/kvm_mmio.h> 33e6b673b7SDave Martin #include <asm/thread_info.h> 344f8d6632SMarc Zyngier 35c1426e4cSEric Auger #define __KVM_HAVE_ARCH_INTC_INITIALIZED 36c1426e4cSEric Auger 37955a3fc6SLinu Cherian #define KVM_USER_MEM_SLOTS 512 38920552b2SDavid Hildenbrand #define KVM_HALT_POLL_NS_DEFAULT 500000 394f8d6632SMarc Zyngier 404f8d6632SMarc Zyngier #include <kvm/arm_vgic.h> 414f8d6632SMarc Zyngier #include <kvm/arm_arch_timer.h> 4204fe4726SShannon Zhao #include <kvm/arm_pmu.h> 434f8d6632SMarc Zyngier 44ef748917SMing Lei #define KVM_MAX_VCPUS VGIC_V3_MAX_CPUS 45ef748917SMing Lei 46808e7381SShannon Zhao #define KVM_VCPU_MAX_FEATURES 4 474f8d6632SMarc Zyngier 487b244e2bSAndrew Jones #define KVM_REQ_SLEEP \ 492387149eSAndrew Jones KVM_ARCH_REQ_FLAGS(0, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) 50325f9c64SAndrew Jones #define KVM_REQ_IRQ_PENDING KVM_ARCH_REQ(1) 51b13216cfSChristoffer Dall 5261bbe380SChristoffer Dall DECLARE_STATIC_KEY_FALSE(userspace_irqchip_in_use); 5361bbe380SChristoffer Dall 546951e48bSWill Deacon int __attribute_const__ kvm_target_cpu(void); 554f8d6632SMarc Zyngier int kvm_reset_vcpu(struct kvm_vcpu *vcpu); 56b46f01ceSAndre Przywara int kvm_arch_dev_ioctl_check_extension(struct kvm *kvm, long ext); 57c612505fSJames Morse void __extended_idmap_trampoline(phys_addr_t boot_pgd, phys_addr_t idmap_start); 584f8d6632SMarc Zyngier 594f8d6632SMarc Zyngier struct kvm_arch { 604f8d6632SMarc Zyngier /* The VMID generation used for the virt. memory system */ 614f8d6632SMarc Zyngier u64 vmid_gen; 624f8d6632SMarc Zyngier u32 vmid; 634f8d6632SMarc Zyngier 644f8d6632SMarc Zyngier /* 1-level 2nd stage table and lock */ 654f8d6632SMarc Zyngier spinlock_t pgd_lock; 664f8d6632SMarc Zyngier pgd_t *pgd; 674f8d6632SMarc Zyngier 684f8d6632SMarc Zyngier /* VTTBR value associated with above pgd and vmid */ 694f8d6632SMarc Zyngier u64 vttbr; 704f8d6632SMarc Zyngier 7194d0e598SMarc Zyngier /* The last vcpu id that ran on each physical CPU */ 7294d0e598SMarc Zyngier int __percpu *last_vcpu_ran; 7394d0e598SMarc Zyngier 743caa2d8cSAndre Przywara /* The maximum number of vCPUs depends on the used GIC model */ 753caa2d8cSAndre Przywara int max_vcpus; 763caa2d8cSAndre Przywara 774f8d6632SMarc Zyngier /* Interrupt controller */ 784f8d6632SMarc Zyngier struct vgic_dist vgic; 7985bd0ba1SMarc Zyngier 8085bd0ba1SMarc Zyngier /* Mandated version of PSCI */ 8185bd0ba1SMarc Zyngier u32 psci_version; 824f8d6632SMarc Zyngier }; 834f8d6632SMarc Zyngier 844f8d6632SMarc Zyngier #define KVM_NR_MEM_OBJS 40 854f8d6632SMarc Zyngier 864f8d6632SMarc Zyngier /* 874f8d6632SMarc Zyngier * We don't want allocation failures within the mmu code, so we preallocate 884f8d6632SMarc Zyngier * enough memory for a single page fault in a cache. 894f8d6632SMarc Zyngier */ 904f8d6632SMarc Zyngier struct kvm_mmu_memory_cache { 914f8d6632SMarc Zyngier int nobjs; 924f8d6632SMarc Zyngier void *objects[KVM_NR_MEM_OBJS]; 934f8d6632SMarc Zyngier }; 944f8d6632SMarc Zyngier 954f8d6632SMarc Zyngier struct kvm_vcpu_fault_info { 964f8d6632SMarc Zyngier u32 esr_el2; /* Hyp Syndrom Register */ 974f8d6632SMarc Zyngier u64 far_el2; /* Hyp Fault Address Register */ 984f8d6632SMarc Zyngier u64 hpfar_el2; /* Hyp IPA Fault Address Register */ 990067df41SJames Morse u64 disr_el1; /* Deferred [SError] Status Register */ 1004f8d6632SMarc Zyngier }; 1014f8d6632SMarc Zyngier 1029d8415d6SMarc Zyngier /* 1039d8415d6SMarc Zyngier * 0 is reserved as an invalid value. 1049d8415d6SMarc Zyngier * Order should be kept in sync with the save/restore code. 1059d8415d6SMarc Zyngier */ 1069d8415d6SMarc Zyngier enum vcpu_sysreg { 1079d8415d6SMarc Zyngier __INVALID_SYSREG__, 1089d8415d6SMarc Zyngier MPIDR_EL1, /* MultiProcessor Affinity Register */ 1099d8415d6SMarc Zyngier CSSELR_EL1, /* Cache Size Selection Register */ 1109d8415d6SMarc Zyngier SCTLR_EL1, /* System Control Register */ 1119d8415d6SMarc Zyngier ACTLR_EL1, /* Auxiliary Control Register */ 1129d8415d6SMarc Zyngier CPACR_EL1, /* Coprocessor Access Control */ 1139d8415d6SMarc Zyngier TTBR0_EL1, /* Translation Table Base Register 0 */ 1149d8415d6SMarc Zyngier TTBR1_EL1, /* Translation Table Base Register 1 */ 1159d8415d6SMarc Zyngier TCR_EL1, /* Translation Control Register */ 1169d8415d6SMarc Zyngier ESR_EL1, /* Exception Syndrome Register */ 117ef769e32SAdam Buchbinder AFSR0_EL1, /* Auxiliary Fault Status Register 0 */ 118ef769e32SAdam Buchbinder AFSR1_EL1, /* Auxiliary Fault Status Register 1 */ 1199d8415d6SMarc Zyngier FAR_EL1, /* Fault Address Register */ 1209d8415d6SMarc Zyngier MAIR_EL1, /* Memory Attribute Indirection Register */ 1219d8415d6SMarc Zyngier VBAR_EL1, /* Vector Base Address Register */ 1229d8415d6SMarc Zyngier CONTEXTIDR_EL1, /* Context ID Register */ 1239d8415d6SMarc Zyngier TPIDR_EL0, /* Thread ID, User R/W */ 1249d8415d6SMarc Zyngier TPIDRRO_EL0, /* Thread ID, User R/O */ 1259d8415d6SMarc Zyngier TPIDR_EL1, /* Thread ID, Privileged */ 1269d8415d6SMarc Zyngier AMAIR_EL1, /* Aux Memory Attribute Indirection Register */ 1279d8415d6SMarc Zyngier CNTKCTL_EL1, /* Timer Control Register (EL1) */ 1289d8415d6SMarc Zyngier PAR_EL1, /* Physical Address Register */ 1299d8415d6SMarc Zyngier MDSCR_EL1, /* Monitor Debug System Control Register */ 1309d8415d6SMarc Zyngier MDCCINT_EL1, /* Monitor Debug Comms Channel Interrupt Enable Reg */ 131c773ae2bSJames Morse DISR_EL1, /* Deferred Interrupt Status Register */ 1329d8415d6SMarc Zyngier 133ab946834SShannon Zhao /* Performance Monitors Registers */ 134ab946834SShannon Zhao PMCR_EL0, /* Control Register */ 1353965c3ceSShannon Zhao PMSELR_EL0, /* Event Counter Selection Register */ 136051ff581SShannon Zhao PMEVCNTR0_EL0, /* Event Counter Register (0-30) */ 137051ff581SShannon Zhao PMEVCNTR30_EL0 = PMEVCNTR0_EL0 + 30, 138051ff581SShannon Zhao PMCCNTR_EL0, /* Cycle Counter Register */ 1399feb21acSShannon Zhao PMEVTYPER0_EL0, /* Event Type Register (0-30) */ 1409feb21acSShannon Zhao PMEVTYPER30_EL0 = PMEVTYPER0_EL0 + 30, 1419feb21acSShannon Zhao PMCCFILTR_EL0, /* Cycle Count Filter Register */ 14296b0eebcSShannon Zhao PMCNTENSET_EL0, /* Count Enable Set Register */ 1439db52c78SShannon Zhao PMINTENSET_EL1, /* Interrupt Enable Set Register */ 14476d883c4SShannon Zhao PMOVSSET_EL0, /* Overflow Flag Status Set Register */ 1457a0adc70SShannon Zhao PMSWINC_EL0, /* Software Increment Register */ 146d692b8adSShannon Zhao PMUSERENR_EL0, /* User Enable Register */ 147ab946834SShannon Zhao 1489d8415d6SMarc Zyngier /* 32bit specific registers. Keep them at the end of the range */ 1499d8415d6SMarc Zyngier DACR32_EL2, /* Domain Access Control Register */ 1509d8415d6SMarc Zyngier IFSR32_EL2, /* Instruction Fault Status Register */ 1519d8415d6SMarc Zyngier FPEXC32_EL2, /* Floating-Point Exception Control Register */ 1529d8415d6SMarc Zyngier DBGVCR32_EL2, /* Debug Vector Catch Register */ 1539d8415d6SMarc Zyngier 1549d8415d6SMarc Zyngier NR_SYS_REGS /* Nothing after this line! */ 1559d8415d6SMarc Zyngier }; 1569d8415d6SMarc Zyngier 1579d8415d6SMarc Zyngier /* 32bit mapping */ 1589d8415d6SMarc Zyngier #define c0_MPIDR (MPIDR_EL1 * 2) /* MultiProcessor ID Register */ 1599d8415d6SMarc Zyngier #define c0_CSSELR (CSSELR_EL1 * 2)/* Cache Size Selection Register */ 1609d8415d6SMarc Zyngier #define c1_SCTLR (SCTLR_EL1 * 2) /* System Control Register */ 1619d8415d6SMarc Zyngier #define c1_ACTLR (ACTLR_EL1 * 2) /* Auxiliary Control Register */ 1629d8415d6SMarc Zyngier #define c1_CPACR (CPACR_EL1 * 2) /* Coprocessor Access Control */ 1639d8415d6SMarc Zyngier #define c2_TTBR0 (TTBR0_EL1 * 2) /* Translation Table Base Register 0 */ 1649d8415d6SMarc Zyngier #define c2_TTBR0_high (c2_TTBR0 + 1) /* TTBR0 top 32 bits */ 1659d8415d6SMarc Zyngier #define c2_TTBR1 (TTBR1_EL1 * 2) /* Translation Table Base Register 1 */ 1669d8415d6SMarc Zyngier #define c2_TTBR1_high (c2_TTBR1 + 1) /* TTBR1 top 32 bits */ 1679d8415d6SMarc Zyngier #define c2_TTBCR (TCR_EL1 * 2) /* Translation Table Base Control R. */ 1689d8415d6SMarc Zyngier #define c3_DACR (DACR32_EL2 * 2)/* Domain Access Control Register */ 1699d8415d6SMarc Zyngier #define c5_DFSR (ESR_EL1 * 2) /* Data Fault Status Register */ 1709d8415d6SMarc Zyngier #define c5_IFSR (IFSR32_EL2 * 2)/* Instruction Fault Status Register */ 1719d8415d6SMarc Zyngier #define c5_ADFSR (AFSR0_EL1 * 2) /* Auxiliary Data Fault Status R */ 1729d8415d6SMarc Zyngier #define c5_AIFSR (AFSR1_EL1 * 2) /* Auxiliary Instr Fault Status R */ 1739d8415d6SMarc Zyngier #define c6_DFAR (FAR_EL1 * 2) /* Data Fault Address Register */ 1749d8415d6SMarc Zyngier #define c6_IFAR (c6_DFAR + 1) /* Instruction Fault Address Register */ 1759d8415d6SMarc Zyngier #define c7_PAR (PAR_EL1 * 2) /* Physical Address Register */ 1769d8415d6SMarc Zyngier #define c7_PAR_high (c7_PAR + 1) /* PAR top 32 bits */ 1779d8415d6SMarc Zyngier #define c10_PRRR (MAIR_EL1 * 2) /* Primary Region Remap Register */ 1789d8415d6SMarc Zyngier #define c10_NMRR (c10_PRRR + 1) /* Normal Memory Remap Register */ 1799d8415d6SMarc Zyngier #define c12_VBAR (VBAR_EL1 * 2) /* Vector Base Address Register */ 1809d8415d6SMarc Zyngier #define c13_CID (CONTEXTIDR_EL1 * 2) /* Context ID Register */ 1819d8415d6SMarc Zyngier #define c13_TID_URW (TPIDR_EL0 * 2) /* Thread ID, User R/W */ 1829d8415d6SMarc Zyngier #define c13_TID_URO (TPIDRRO_EL0 * 2)/* Thread ID, User R/O */ 1839d8415d6SMarc Zyngier #define c13_TID_PRIV (TPIDR_EL1 * 2) /* Thread ID, Privileged */ 1849d8415d6SMarc Zyngier #define c10_AMAIR0 (AMAIR_EL1 * 2) /* Aux Memory Attr Indirection Reg */ 1859d8415d6SMarc Zyngier #define c10_AMAIR1 (c10_AMAIR0 + 1)/* Aux Memory Attr Indirection Reg */ 1869d8415d6SMarc Zyngier #define c14_CNTKCTL (CNTKCTL_EL1 * 2) /* Timer Control Register (PL1) */ 1879d8415d6SMarc Zyngier 1889d8415d6SMarc Zyngier #define cp14_DBGDSCRext (MDSCR_EL1 * 2) 1899d8415d6SMarc Zyngier #define cp14_DBGBCR0 (DBGBCR0_EL1 * 2) 1909d8415d6SMarc Zyngier #define cp14_DBGBVR0 (DBGBVR0_EL1 * 2) 1919d8415d6SMarc Zyngier #define cp14_DBGBXVR0 (cp14_DBGBVR0 + 1) 1929d8415d6SMarc Zyngier #define cp14_DBGWCR0 (DBGWCR0_EL1 * 2) 1939d8415d6SMarc Zyngier #define cp14_DBGWVR0 (DBGWVR0_EL1 * 2) 1949d8415d6SMarc Zyngier #define cp14_DBGDCCINT (MDCCINT_EL1 * 2) 1959d8415d6SMarc Zyngier 1969d8415d6SMarc Zyngier #define NR_COPRO_REGS (NR_SYS_REGS * 2) 1979d8415d6SMarc Zyngier 1984f8d6632SMarc Zyngier struct kvm_cpu_context { 1994f8d6632SMarc Zyngier struct kvm_regs gp_regs; 20040033a61SMarc Zyngier union { 2014f8d6632SMarc Zyngier u64 sys_regs[NR_SYS_REGS]; 20272564016SMarc Zyngier u32 copro[NR_COPRO_REGS]; 20340033a61SMarc Zyngier }; 204c97e166eSJames Morse 205c97e166eSJames Morse struct kvm_vcpu *__hyp_running_vcpu; 2064f8d6632SMarc Zyngier }; 2074f8d6632SMarc Zyngier 2084f8d6632SMarc Zyngier typedef struct kvm_cpu_context kvm_cpu_context_t; 2094f8d6632SMarc Zyngier 2104f8d6632SMarc Zyngier struct kvm_vcpu_arch { 2114f8d6632SMarc Zyngier struct kvm_cpu_context ctxt; 2124f8d6632SMarc Zyngier 2134f8d6632SMarc Zyngier /* HYP configuration */ 2144f8d6632SMarc Zyngier u64 hcr_el2; 21556c7f5e7SAlex Bennée u32 mdcr_el2; 2164f8d6632SMarc Zyngier 2174f8d6632SMarc Zyngier /* Exception Information */ 2184f8d6632SMarc Zyngier struct kvm_vcpu_fault_info fault; 2194f8d6632SMarc Zyngier 22055e3748eSMarc Zyngier /* State of various workarounds, see kvm_asm.h for bit assignment */ 22155e3748eSMarc Zyngier u64 workaround_flags; 22255e3748eSMarc Zyngier 223fa89d31cSDave Martin /* Miscellaneous vcpu state flags */ 224fa89d31cSDave Martin u64 flags; 2250c557ed4SMarc Zyngier 22684e690bfSAlex Bennée /* 22784e690bfSAlex Bennée * We maintain more than a single set of debug registers to support 22884e690bfSAlex Bennée * debugging the guest from the host and to maintain separate host and 22984e690bfSAlex Bennée * guest state during world switches. vcpu_debug_state are the debug 23084e690bfSAlex Bennée * registers of the vcpu as the guest sees them. host_debug_state are 231834bf887SAlex Bennée * the host registers which are saved and restored during 232834bf887SAlex Bennée * world switches. external_debug_state contains the debug 233834bf887SAlex Bennée * values we want to debug the guest. This is set via the 234834bf887SAlex Bennée * KVM_SET_GUEST_DEBUG ioctl. 23584e690bfSAlex Bennée * 23684e690bfSAlex Bennée * debug_ptr points to the set of debug registers that should be loaded 23784e690bfSAlex Bennée * onto the hardware when running the guest. 23884e690bfSAlex Bennée */ 23984e690bfSAlex Bennée struct kvm_guest_debug_arch *debug_ptr; 24084e690bfSAlex Bennée struct kvm_guest_debug_arch vcpu_debug_state; 241834bf887SAlex Bennée struct kvm_guest_debug_arch external_debug_state; 24284e690bfSAlex Bennée 2434f8d6632SMarc Zyngier /* Pointer to host CPU context */ 2444f8d6632SMarc Zyngier kvm_cpu_context_t *host_cpu_context; 245e6b673b7SDave Martin 246e6b673b7SDave Martin struct thread_info *host_thread_info; /* hyp VA */ 247e6b673b7SDave Martin struct user_fpsimd_state *host_fpsimd_state; /* hyp VA */ 248e6b673b7SDave Martin 249f85279b4SWill Deacon struct { 250f85279b4SWill Deacon /* {Break,watch}point registers */ 251f85279b4SWill Deacon struct kvm_guest_debug_arch regs; 252f85279b4SWill Deacon /* Statistical profiling extension */ 253f85279b4SWill Deacon u64 pmscr_el1; 254f85279b4SWill Deacon } host_debug_state; 2554f8d6632SMarc Zyngier 2564f8d6632SMarc Zyngier /* VGIC state */ 2574f8d6632SMarc Zyngier struct vgic_cpu vgic_cpu; 2584f8d6632SMarc Zyngier struct arch_timer_cpu timer_cpu; 25904fe4726SShannon Zhao struct kvm_pmu pmu; 2604f8d6632SMarc Zyngier 2614f8d6632SMarc Zyngier /* 2624f8d6632SMarc Zyngier * Anything that is not used directly from assembly code goes 2634f8d6632SMarc Zyngier * here. 2644f8d6632SMarc Zyngier */ 2654f8d6632SMarc Zyngier 266337b99bfSAlex Bennée /* 267337b99bfSAlex Bennée * Guest registers we preserve during guest debugging. 268337b99bfSAlex Bennée * 269337b99bfSAlex Bennée * These shadow registers are updated by the kvm_handle_sys_reg 270337b99bfSAlex Bennée * trap handler if the guest accesses or updates them while we 271337b99bfSAlex Bennée * are using guest debug. 272337b99bfSAlex Bennée */ 273337b99bfSAlex Bennée struct { 274337b99bfSAlex Bennée u32 mdscr_el1; 275337b99bfSAlex Bennée } guest_debug_preserved; 276337b99bfSAlex Bennée 2773781528eSEric Auger /* vcpu power-off state */ 2783781528eSEric Auger bool power_off; 2794f8d6632SMarc Zyngier 2803b92830aSEric Auger /* Don't run the guest (internal implementation need) */ 2813b92830aSEric Auger bool pause; 2823b92830aSEric Auger 2834f8d6632SMarc Zyngier /* IO related fields */ 2844f8d6632SMarc Zyngier struct kvm_decode mmio_decode; 2854f8d6632SMarc Zyngier 2864f8d6632SMarc Zyngier /* Cache some mmu pages needed inside spinlock regions */ 2874f8d6632SMarc Zyngier struct kvm_mmu_memory_cache mmu_page_cache; 2884f8d6632SMarc Zyngier 2894f8d6632SMarc Zyngier /* Target CPU and feature flags */ 2906c8c0c4dSChen Gang int target; 2914f8d6632SMarc Zyngier DECLARE_BITMAP(features, KVM_VCPU_MAX_FEATURES); 2924f8d6632SMarc Zyngier 2934f8d6632SMarc Zyngier /* Detect first run of a vcpu */ 2944f8d6632SMarc Zyngier bool has_run_once; 2954715c14bSJames Morse 2964715c14bSJames Morse /* Virtual SError ESR to restore when HCR_EL2.VSE is set */ 2974715c14bSJames Morse u64 vsesr_el2; 298d47533daSChristoffer Dall 299d47533daSChristoffer Dall /* True when deferrable sysregs are loaded on the physical CPU, 300d47533daSChristoffer Dall * see kvm_vcpu_load_sysregs and kvm_vcpu_put_sysregs. */ 301d47533daSChristoffer Dall bool sysregs_loaded_on_cpu; 3024f8d6632SMarc Zyngier }; 3034f8d6632SMarc Zyngier 304fa89d31cSDave Martin /* vcpu_arch flags field values: */ 305fa89d31cSDave Martin #define KVM_ARM64_DEBUG_DIRTY (1 << 0) 306e6b673b7SDave Martin #define KVM_ARM64_FP_ENABLED (1 << 1) /* guest FP regs loaded */ 307e6b673b7SDave Martin #define KVM_ARM64_FP_HOST (1 << 2) /* host FP regs loaded */ 308e6b673b7SDave Martin #define KVM_ARM64_HOST_SVE_IN_USE (1 << 3) /* backup for host TIF_SVE */ 309b3eb56b6SDave Martin #define KVM_ARM64_HOST_SVE_ENABLED (1 << 4) /* SVE enabled for EL0 */ 310fa89d31cSDave Martin 3114f8d6632SMarc Zyngier #define vcpu_gp_regs(v) (&(v)->arch.ctxt.gp_regs) 3128d404c4cSChristoffer Dall 3138d404c4cSChristoffer Dall /* 3148d404c4cSChristoffer Dall * Only use __vcpu_sys_reg if you know you want the memory backed version of a 3158d404c4cSChristoffer Dall * register, and not the one most recently accessed by a running VCPU. For 3168d404c4cSChristoffer Dall * example, for userspace access or for system registers that are never context 3178d404c4cSChristoffer Dall * switched, but only emulated. 3188d404c4cSChristoffer Dall */ 3198d404c4cSChristoffer Dall #define __vcpu_sys_reg(v,r) ((v)->arch.ctxt.sys_regs[(r)]) 3208d404c4cSChristoffer Dall 321d47533daSChristoffer Dall u64 vcpu_read_sys_reg(struct kvm_vcpu *vcpu, int reg); 322d47533daSChristoffer Dall void vcpu_write_sys_reg(struct kvm_vcpu *vcpu, u64 val, int reg); 3238d404c4cSChristoffer Dall 32472564016SMarc Zyngier /* 32572564016SMarc Zyngier * CP14 and CP15 live in the same array, as they are backed by the 32672564016SMarc Zyngier * same system registers. 32772564016SMarc Zyngier */ 32872564016SMarc Zyngier #define vcpu_cp14(v,r) ((v)->arch.ctxt.copro[(r)]) 32972564016SMarc Zyngier #define vcpu_cp15(v,r) ((v)->arch.ctxt.copro[(r)]) 3304f8d6632SMarc Zyngier 3314f8d6632SMarc Zyngier struct kvm_vm_stat { 3328a7e75d4SSuraj Jitindar Singh ulong remote_tlb_flush; 3334f8d6632SMarc Zyngier }; 3344f8d6632SMarc Zyngier 3354f8d6632SMarc Zyngier struct kvm_vcpu_stat { 3368a7e75d4SSuraj Jitindar Singh u64 halt_successful_poll; 3378a7e75d4SSuraj Jitindar Singh u64 halt_attempted_poll; 3388a7e75d4SSuraj Jitindar Singh u64 halt_poll_invalid; 3398a7e75d4SSuraj Jitindar Singh u64 halt_wakeup; 3408a7e75d4SSuraj Jitindar Singh u64 hvc_exit_stat; 341b19e6892SAmit Tomar u64 wfe_exit_stat; 342b19e6892SAmit Tomar u64 wfi_exit_stat; 343b19e6892SAmit Tomar u64 mmio_exit_user; 344b19e6892SAmit Tomar u64 mmio_exit_kernel; 345b19e6892SAmit Tomar u64 exits; 3464f8d6632SMarc Zyngier }; 3474f8d6632SMarc Zyngier 348473bdc0eSAnup Patel int kvm_vcpu_preferred_target(struct kvm_vcpu_init *init); 3494f8d6632SMarc Zyngier unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu); 3504f8d6632SMarc Zyngier int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices); 3514f8d6632SMarc Zyngier int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg); 3524f8d6632SMarc Zyngier int kvm_arm_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg); 3534f8d6632SMarc Zyngier 3544f8d6632SMarc Zyngier #define KVM_ARCH_WANT_MMU_NOTIFIER 3554f8d6632SMarc Zyngier int kvm_unmap_hva(struct kvm *kvm, unsigned long hva); 3564f8d6632SMarc Zyngier int kvm_unmap_hva_range(struct kvm *kvm, 3574f8d6632SMarc Zyngier unsigned long start, unsigned long end); 3584f8d6632SMarc Zyngier void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte); 35935307b9aSMarc Zyngier int kvm_age_hva(struct kvm *kvm, unsigned long start, unsigned long end); 36035307b9aSMarc Zyngier int kvm_test_age_hva(struct kvm *kvm, unsigned long hva); 3614f8d6632SMarc Zyngier 3624f8d6632SMarc Zyngier struct kvm_vcpu *kvm_arm_get_running_vcpu(void); 3634000be42SWill Deacon struct kvm_vcpu * __percpu *kvm_get_running_vcpus(void); 364b13216cfSChristoffer Dall void kvm_arm_halt_guest(struct kvm *kvm); 365b13216cfSChristoffer Dall void kvm_arm_resume_guest(struct kvm *kvm); 3664f8d6632SMarc Zyngier 367a0bf9776SArd Biesheuvel u64 __kvm_call_hyp(void *hypfn, ...); 36822b39ca3SMarc Zyngier #define kvm_call_hyp(f, ...) __kvm_call_hyp(kvm_ksym_ref(f), ##__VA_ARGS__) 36922b39ca3SMarc Zyngier 370cf5d3188SChristoffer Dall void force_vm_exit(const cpumask_t *mask); 3718199ed0eSMario Smarduch void kvm_mmu_wp_memory_region(struct kvm *kvm, int slot); 3724f8d6632SMarc Zyngier 3734f8d6632SMarc Zyngier int handle_exit(struct kvm_vcpu *vcpu, struct kvm_run *run, 3744f8d6632SMarc Zyngier int exception_index); 3753368bd80SJames Morse void handle_exit_early(struct kvm_vcpu *vcpu, struct kvm_run *run, 3763368bd80SJames Morse int exception_index); 3774f8d6632SMarc Zyngier 3784f8d6632SMarc Zyngier int kvm_perf_init(void); 3794f8d6632SMarc Zyngier int kvm_perf_teardown(void); 3804f8d6632SMarc Zyngier 3814429fc64SAndre Przywara struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr); 3824429fc64SAndre Przywara 3834464e210SChristoffer Dall DECLARE_PER_CPU(kvm_cpu_context_t, kvm_host_cpu_state); 3844464e210SChristoffer Dall 38512fda812SMarc Zyngier static inline void __cpu_init_hyp_mode(phys_addr_t pgd_ptr, 386092bd143SMarc Zyngier unsigned long hyp_stack_ptr, 387092bd143SMarc Zyngier unsigned long vector_ptr) 388092bd143SMarc Zyngier { 3899bc03f1dSMarc Zyngier /* 3909bc03f1dSMarc Zyngier * Calculate the raw per-cpu offset without a translation from the 3919bc03f1dSMarc Zyngier * kernel's mapping to the linear mapping, and store it in tpidr_el2 3929bc03f1dSMarc Zyngier * so that we can use adr_l to access per-cpu variables in EL2. 3939bc03f1dSMarc Zyngier */ 3949bc03f1dSMarc Zyngier u64 tpidr_el2 = ((u64)this_cpu_ptr(&kvm_host_cpu_state) - 3959bc03f1dSMarc Zyngier (u64)kvm_ksym_ref(kvm_host_cpu_state)); 3964464e210SChristoffer Dall 397092bd143SMarc Zyngier /* 39863a1e1c9SMark Rutland * Call initialization code, and switch to the full blown HYP code. 39963a1e1c9SMark Rutland * If the cpucaps haven't been finalized yet, something has gone very 40063a1e1c9SMark Rutland * wrong, and hyp will crash and burn when it uses any 40163a1e1c9SMark Rutland * cpus_have_const_cap() wrapper. 402092bd143SMarc Zyngier */ 40363a1e1c9SMark Rutland BUG_ON(!static_branch_likely(&arm64_const_caps_ready)); 4049bc03f1dSMarc Zyngier __kvm_call_hyp((void *)pgd_ptr, hyp_stack_ptr, vector_ptr, tpidr_el2); 405092bd143SMarc Zyngier } 406092bd143SMarc Zyngier 40785acda3bSDave Martin static inline bool kvm_arch_check_sve_has_vhe(void) 40885acda3bSDave Martin { 40985acda3bSDave Martin /* 41085acda3bSDave Martin * The Arm architecture specifies that implementation of SVE 41185acda3bSDave Martin * requires VHE also to be implemented. The KVM code for arm64 41285acda3bSDave Martin * relies on this when SVE is present: 41385acda3bSDave Martin */ 41485acda3bSDave Martin if (system_supports_sve()) 41585acda3bSDave Martin return has_vhe(); 41685acda3bSDave Martin else 41785acda3bSDave Martin return true; 41885acda3bSDave Martin } 41985acda3bSDave Martin 4200865e636SRadim Krčmář static inline void kvm_arch_hardware_unsetup(void) {} 4210865e636SRadim Krčmář static inline void kvm_arch_sync_events(struct kvm *kvm) {} 4220865e636SRadim Krčmář static inline void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) {} 4230865e636SRadim Krčmář static inline void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu) {} 4243491caf2SChristian Borntraeger static inline void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu) {} 4250865e636SRadim Krčmář 42656c7f5e7SAlex Bennée void kvm_arm_init_debug(void); 42756c7f5e7SAlex Bennée void kvm_arm_setup_debug(struct kvm_vcpu *vcpu); 42856c7f5e7SAlex Bennée void kvm_arm_clear_debug(struct kvm_vcpu *vcpu); 42984e690bfSAlex Bennée void kvm_arm_reset_debug_ptr(struct kvm_vcpu *vcpu); 430696673d1SAlex Bennée bool kvm_arm_handle_step_debug(struct kvm_vcpu *vcpu, struct kvm_run *run); 431bb0c70bcSShannon Zhao int kvm_arm_vcpu_arch_set_attr(struct kvm_vcpu *vcpu, 432bb0c70bcSShannon Zhao struct kvm_device_attr *attr); 433bb0c70bcSShannon Zhao int kvm_arm_vcpu_arch_get_attr(struct kvm_vcpu *vcpu, 434bb0c70bcSShannon Zhao struct kvm_device_attr *attr); 435bb0c70bcSShannon Zhao int kvm_arm_vcpu_arch_has_attr(struct kvm_vcpu *vcpu, 436bb0c70bcSShannon Zhao struct kvm_device_attr *attr); 43756c7f5e7SAlex Bennée 43821a4179cSMarc Zyngier static inline void __cpu_init_stage2(void) 43921a4179cSMarc Zyngier { 4406141570cSMarc Zyngier u32 parange = kvm_call_hyp(__init_stage2_translation); 4416141570cSMarc Zyngier 4426141570cSMarc Zyngier WARN_ONCE(parange < 40, 4436141570cSMarc Zyngier "PARange is %d bits, unsupported configuration!", parange); 44421a4179cSMarc Zyngier } 44521a4179cSMarc Zyngier 446e6b673b7SDave Martin /* Guest/host FPSIMD coordination helpers */ 447e6b673b7SDave Martin int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu); 448e6b673b7SDave Martin void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu); 449e6b673b7SDave Martin void kvm_arch_vcpu_ctxsync_fp(struct kvm_vcpu *vcpu); 450e6b673b7SDave Martin void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu); 451e6b673b7SDave Martin 452e6b673b7SDave Martin #ifdef CONFIG_KVM /* Avoid conflicts with core headers if CONFIG_KVM=n */ 453e6b673b7SDave Martin static inline int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu) 45417eed27bSDave Martin { 455e6b673b7SDave Martin return kvm_arch_vcpu_run_map_fp(vcpu); 45617eed27bSDave Martin } 457e6b673b7SDave Martin #endif 45817eed27bSDave Martin 4594f5abad9SJames Morse static inline void kvm_arm_vhe_guest_enter(void) 4604f5abad9SJames Morse { 4614f5abad9SJames Morse local_daif_mask(); 4624f5abad9SJames Morse } 4634f5abad9SJames Morse 4644f5abad9SJames Morse static inline void kvm_arm_vhe_guest_exit(void) 4654f5abad9SJames Morse { 4664f5abad9SJames Morse local_daif_restore(DAIF_PROCCTX_NOIRQ); 4673f5c90b8SChristoffer Dall 4683f5c90b8SChristoffer Dall /* 4693f5c90b8SChristoffer Dall * When we exit from the guest we change a number of CPU configuration 4703f5c90b8SChristoffer Dall * parameters, such as traps. Make sure these changes take effect 4713f5c90b8SChristoffer Dall * before running the host or additional guests. 4723f5c90b8SChristoffer Dall */ 4733f5c90b8SChristoffer Dall isb(); 4744f5abad9SJames Morse } 4756167ec5cSMarc Zyngier 4766167ec5cSMarc Zyngier static inline bool kvm_arm_harden_branch_predictor(void) 4776167ec5cSMarc Zyngier { 4786167ec5cSMarc Zyngier return cpus_have_const_cap(ARM64_HARDEN_BRANCH_PREDICTOR); 4796167ec5cSMarc Zyngier } 4806167ec5cSMarc Zyngier 4815d81f7dcSMarc Zyngier #define KVM_SSBD_UNKNOWN -1 4825d81f7dcSMarc Zyngier #define KVM_SSBD_FORCE_DISABLE 0 4835d81f7dcSMarc Zyngier #define KVM_SSBD_KERNEL 1 4845d81f7dcSMarc Zyngier #define KVM_SSBD_FORCE_ENABLE 2 4855d81f7dcSMarc Zyngier #define KVM_SSBD_MITIGATED 3 4865d81f7dcSMarc Zyngier 4875d81f7dcSMarc Zyngier static inline int kvm_arm_have_ssbd(void) 4885d81f7dcSMarc Zyngier { 4895d81f7dcSMarc Zyngier switch (arm64_get_ssbd_state()) { 4905d81f7dcSMarc Zyngier case ARM64_SSBD_FORCE_DISABLE: 4915d81f7dcSMarc Zyngier return KVM_SSBD_FORCE_DISABLE; 4925d81f7dcSMarc Zyngier case ARM64_SSBD_KERNEL: 4935d81f7dcSMarc Zyngier return KVM_SSBD_KERNEL; 4945d81f7dcSMarc Zyngier case ARM64_SSBD_FORCE_ENABLE: 4955d81f7dcSMarc Zyngier return KVM_SSBD_FORCE_ENABLE; 4965d81f7dcSMarc Zyngier case ARM64_SSBD_MITIGATED: 4975d81f7dcSMarc Zyngier return KVM_SSBD_MITIGATED; 4985d81f7dcSMarc Zyngier case ARM64_SSBD_UNKNOWN: 4995d81f7dcSMarc Zyngier default: 5005d81f7dcSMarc Zyngier return KVM_SSBD_UNKNOWN; 5015d81f7dcSMarc Zyngier } 5025d81f7dcSMarc Zyngier } 5035d81f7dcSMarc Zyngier 504bc192ceeSChristoffer Dall void kvm_vcpu_load_sysregs(struct kvm_vcpu *vcpu); 505bc192ceeSChristoffer Dall void kvm_vcpu_put_sysregs(struct kvm_vcpu *vcpu); 506bc192ceeSChristoffer Dall 507d1e5b0e9SMarc Orr #define __KVM_HAVE_ARCH_VM_ALLOC 508d1e5b0e9SMarc Orr struct kvm *kvm_arch_alloc_vm(void); 509d1e5b0e9SMarc Orr void kvm_arch_free_vm(struct kvm *kvm); 510d1e5b0e9SMarc Orr 5114f8d6632SMarc Zyngier #endif /* __ARM64_KVM_HOST_H__ */ 512