xref: /openbmc/linux/arch/arm64/include/asm/kvm_host.h (revision 17eed27b)
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>
2817eed27bSDave Martin #include <asm/fpsimd.h>
294f8d6632SMarc Zyngier #include <asm/kvm.h>
303a3604bcSMarc Zyngier #include <asm/kvm_asm.h>
314f8d6632SMarc Zyngier #include <asm/kvm_mmio.h>
324f8d6632SMarc Zyngier 
33c1426e4cSEric Auger #define __KVM_HAVE_ARCH_INTC_INITIALIZED
34c1426e4cSEric Auger 
35955a3fc6SLinu Cherian #define KVM_USER_MEM_SLOTS 512
36920552b2SDavid Hildenbrand #define KVM_HALT_POLL_NS_DEFAULT 500000
374f8d6632SMarc Zyngier 
384f8d6632SMarc Zyngier #include <kvm/arm_vgic.h>
394f8d6632SMarc Zyngier #include <kvm/arm_arch_timer.h>
4004fe4726SShannon Zhao #include <kvm/arm_pmu.h>
414f8d6632SMarc Zyngier 
42ef748917SMing Lei #define KVM_MAX_VCPUS VGIC_V3_MAX_CPUS
43ef748917SMing Lei 
44808e7381SShannon Zhao #define KVM_VCPU_MAX_FEATURES 4
454f8d6632SMarc Zyngier 
467b244e2bSAndrew Jones #define KVM_REQ_SLEEP \
472387149eSAndrew Jones 	KVM_ARCH_REQ_FLAGS(0, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
48325f9c64SAndrew Jones #define KVM_REQ_IRQ_PENDING	KVM_ARCH_REQ(1)
49b13216cfSChristoffer Dall 
506951e48bSWill Deacon int __attribute_const__ kvm_target_cpu(void);
514f8d6632SMarc Zyngier int kvm_reset_vcpu(struct kvm_vcpu *vcpu);
52b46f01ceSAndre Przywara int kvm_arch_dev_ioctl_check_extension(struct kvm *kvm, long ext);
53c612505fSJames Morse void __extended_idmap_trampoline(phys_addr_t boot_pgd, phys_addr_t idmap_start);
544f8d6632SMarc Zyngier 
554f8d6632SMarc Zyngier struct kvm_arch {
564f8d6632SMarc Zyngier 	/* The VMID generation used for the virt. memory system */
574f8d6632SMarc Zyngier 	u64    vmid_gen;
584f8d6632SMarc Zyngier 	u32    vmid;
594f8d6632SMarc Zyngier 
604f8d6632SMarc Zyngier 	/* 1-level 2nd stage table and lock */
614f8d6632SMarc Zyngier 	spinlock_t pgd_lock;
624f8d6632SMarc Zyngier 	pgd_t *pgd;
634f8d6632SMarc Zyngier 
644f8d6632SMarc Zyngier 	/* VTTBR value associated with above pgd and vmid */
654f8d6632SMarc Zyngier 	u64    vttbr;
664f8d6632SMarc Zyngier 
6794d0e598SMarc Zyngier 	/* The last vcpu id that ran on each physical CPU */
6894d0e598SMarc Zyngier 	int __percpu *last_vcpu_ran;
6994d0e598SMarc Zyngier 
703caa2d8cSAndre Przywara 	/* The maximum number of vCPUs depends on the used GIC model */
713caa2d8cSAndre Przywara 	int max_vcpus;
723caa2d8cSAndre Przywara 
734f8d6632SMarc Zyngier 	/* Interrupt controller */
744f8d6632SMarc Zyngier 	struct vgic_dist	vgic;
754f8d6632SMarc Zyngier };
764f8d6632SMarc Zyngier 
774f8d6632SMarc Zyngier #define KVM_NR_MEM_OBJS     40
784f8d6632SMarc Zyngier 
794f8d6632SMarc Zyngier /*
804f8d6632SMarc Zyngier  * We don't want allocation failures within the mmu code, so we preallocate
814f8d6632SMarc Zyngier  * enough memory for a single page fault in a cache.
824f8d6632SMarc Zyngier  */
834f8d6632SMarc Zyngier struct kvm_mmu_memory_cache {
844f8d6632SMarc Zyngier 	int nobjs;
854f8d6632SMarc Zyngier 	void *objects[KVM_NR_MEM_OBJS];
864f8d6632SMarc Zyngier };
874f8d6632SMarc Zyngier 
884f8d6632SMarc Zyngier struct kvm_vcpu_fault_info {
894f8d6632SMarc Zyngier 	u32 esr_el2;		/* Hyp Syndrom Register */
904f8d6632SMarc Zyngier 	u64 far_el2;		/* Hyp Fault Address Register */
914f8d6632SMarc Zyngier 	u64 hpfar_el2;		/* Hyp IPA Fault Address Register */
924f8d6632SMarc Zyngier };
934f8d6632SMarc Zyngier 
949d8415d6SMarc Zyngier /*
959d8415d6SMarc Zyngier  * 0 is reserved as an invalid value.
969d8415d6SMarc Zyngier  * Order should be kept in sync with the save/restore code.
979d8415d6SMarc Zyngier  */
989d8415d6SMarc Zyngier enum vcpu_sysreg {
999d8415d6SMarc Zyngier 	__INVALID_SYSREG__,
1009d8415d6SMarc Zyngier 	MPIDR_EL1,	/* MultiProcessor Affinity Register */
1019d8415d6SMarc Zyngier 	CSSELR_EL1,	/* Cache Size Selection Register */
1029d8415d6SMarc Zyngier 	SCTLR_EL1,	/* System Control Register */
1039d8415d6SMarc Zyngier 	ACTLR_EL1,	/* Auxiliary Control Register */
1049d8415d6SMarc Zyngier 	CPACR_EL1,	/* Coprocessor Access Control */
1059d8415d6SMarc Zyngier 	TTBR0_EL1,	/* Translation Table Base Register 0 */
1069d8415d6SMarc Zyngier 	TTBR1_EL1,	/* Translation Table Base Register 1 */
1079d8415d6SMarc Zyngier 	TCR_EL1,	/* Translation Control Register */
1089d8415d6SMarc Zyngier 	ESR_EL1,	/* Exception Syndrome Register */
109ef769e32SAdam Buchbinder 	AFSR0_EL1,	/* Auxiliary Fault Status Register 0 */
110ef769e32SAdam Buchbinder 	AFSR1_EL1,	/* Auxiliary Fault Status Register 1 */
1119d8415d6SMarc Zyngier 	FAR_EL1,	/* Fault Address Register */
1129d8415d6SMarc Zyngier 	MAIR_EL1,	/* Memory Attribute Indirection Register */
1139d8415d6SMarc Zyngier 	VBAR_EL1,	/* Vector Base Address Register */
1149d8415d6SMarc Zyngier 	CONTEXTIDR_EL1,	/* Context ID Register */
1159d8415d6SMarc Zyngier 	TPIDR_EL0,	/* Thread ID, User R/W */
1169d8415d6SMarc Zyngier 	TPIDRRO_EL0,	/* Thread ID, User R/O */
1179d8415d6SMarc Zyngier 	TPIDR_EL1,	/* Thread ID, Privileged */
1189d8415d6SMarc Zyngier 	AMAIR_EL1,	/* Aux Memory Attribute Indirection Register */
1199d8415d6SMarc Zyngier 	CNTKCTL_EL1,	/* Timer Control Register (EL1) */
1209d8415d6SMarc Zyngier 	PAR_EL1,	/* Physical Address Register */
1219d8415d6SMarc Zyngier 	MDSCR_EL1,	/* Monitor Debug System Control Register */
1229d8415d6SMarc Zyngier 	MDCCINT_EL1,	/* Monitor Debug Comms Channel Interrupt Enable Reg */
1239d8415d6SMarc Zyngier 
124ab946834SShannon Zhao 	/* Performance Monitors Registers */
125ab946834SShannon Zhao 	PMCR_EL0,	/* Control Register */
1263965c3ceSShannon Zhao 	PMSELR_EL0,	/* Event Counter Selection Register */
127051ff581SShannon Zhao 	PMEVCNTR0_EL0,	/* Event Counter Register (0-30) */
128051ff581SShannon Zhao 	PMEVCNTR30_EL0 = PMEVCNTR0_EL0 + 30,
129051ff581SShannon Zhao 	PMCCNTR_EL0,	/* Cycle Counter Register */
1309feb21acSShannon Zhao 	PMEVTYPER0_EL0,	/* Event Type Register (0-30) */
1319feb21acSShannon Zhao 	PMEVTYPER30_EL0 = PMEVTYPER0_EL0 + 30,
1329feb21acSShannon Zhao 	PMCCFILTR_EL0,	/* Cycle Count Filter Register */
13396b0eebcSShannon Zhao 	PMCNTENSET_EL0,	/* Count Enable Set Register */
1349db52c78SShannon Zhao 	PMINTENSET_EL1,	/* Interrupt Enable Set Register */
13576d883c4SShannon Zhao 	PMOVSSET_EL0,	/* Overflow Flag Status Set Register */
1367a0adc70SShannon Zhao 	PMSWINC_EL0,	/* Software Increment Register */
137d692b8adSShannon Zhao 	PMUSERENR_EL0,	/* User Enable Register */
138ab946834SShannon Zhao 
1399d8415d6SMarc Zyngier 	/* 32bit specific registers. Keep them at the end of the range */
1409d8415d6SMarc Zyngier 	DACR32_EL2,	/* Domain Access Control Register */
1419d8415d6SMarc Zyngier 	IFSR32_EL2,	/* Instruction Fault Status Register */
1429d8415d6SMarc Zyngier 	FPEXC32_EL2,	/* Floating-Point Exception Control Register */
1439d8415d6SMarc Zyngier 	DBGVCR32_EL2,	/* Debug Vector Catch Register */
1449d8415d6SMarc Zyngier 
1459d8415d6SMarc Zyngier 	NR_SYS_REGS	/* Nothing after this line! */
1469d8415d6SMarc Zyngier };
1479d8415d6SMarc Zyngier 
1489d8415d6SMarc Zyngier /* 32bit mapping */
1499d8415d6SMarc Zyngier #define c0_MPIDR	(MPIDR_EL1 * 2)	/* MultiProcessor ID Register */
1509d8415d6SMarc Zyngier #define c0_CSSELR	(CSSELR_EL1 * 2)/* Cache Size Selection Register */
1519d8415d6SMarc Zyngier #define c1_SCTLR	(SCTLR_EL1 * 2)	/* System Control Register */
1529d8415d6SMarc Zyngier #define c1_ACTLR	(ACTLR_EL1 * 2)	/* Auxiliary Control Register */
1539d8415d6SMarc Zyngier #define c1_CPACR	(CPACR_EL1 * 2)	/* Coprocessor Access Control */
1549d8415d6SMarc Zyngier #define c2_TTBR0	(TTBR0_EL1 * 2)	/* Translation Table Base Register 0 */
1559d8415d6SMarc Zyngier #define c2_TTBR0_high	(c2_TTBR0 + 1)	/* TTBR0 top 32 bits */
1569d8415d6SMarc Zyngier #define c2_TTBR1	(TTBR1_EL1 * 2)	/* Translation Table Base Register 1 */
1579d8415d6SMarc Zyngier #define c2_TTBR1_high	(c2_TTBR1 + 1)	/* TTBR1 top 32 bits */
1589d8415d6SMarc Zyngier #define c2_TTBCR	(TCR_EL1 * 2)	/* Translation Table Base Control R. */
1599d8415d6SMarc Zyngier #define c3_DACR		(DACR32_EL2 * 2)/* Domain Access Control Register */
1609d8415d6SMarc Zyngier #define c5_DFSR		(ESR_EL1 * 2)	/* Data Fault Status Register */
1619d8415d6SMarc Zyngier #define c5_IFSR		(IFSR32_EL2 * 2)/* Instruction Fault Status Register */
1629d8415d6SMarc Zyngier #define c5_ADFSR	(AFSR0_EL1 * 2)	/* Auxiliary Data Fault Status R */
1639d8415d6SMarc Zyngier #define c5_AIFSR	(AFSR1_EL1 * 2)	/* Auxiliary Instr Fault Status R */
1649d8415d6SMarc Zyngier #define c6_DFAR		(FAR_EL1 * 2)	/* Data Fault Address Register */
1659d8415d6SMarc Zyngier #define c6_IFAR		(c6_DFAR + 1)	/* Instruction Fault Address Register */
1669d8415d6SMarc Zyngier #define c7_PAR		(PAR_EL1 * 2)	/* Physical Address Register */
1679d8415d6SMarc Zyngier #define c7_PAR_high	(c7_PAR + 1)	/* PAR top 32 bits */
1689d8415d6SMarc Zyngier #define c10_PRRR	(MAIR_EL1 * 2)	/* Primary Region Remap Register */
1699d8415d6SMarc Zyngier #define c10_NMRR	(c10_PRRR + 1)	/* Normal Memory Remap Register */
1709d8415d6SMarc Zyngier #define c12_VBAR	(VBAR_EL1 * 2)	/* Vector Base Address Register */
1719d8415d6SMarc Zyngier #define c13_CID		(CONTEXTIDR_EL1 * 2)	/* Context ID Register */
1729d8415d6SMarc Zyngier #define c13_TID_URW	(TPIDR_EL0 * 2)	/* Thread ID, User R/W */
1739d8415d6SMarc Zyngier #define c13_TID_URO	(TPIDRRO_EL0 * 2)/* Thread ID, User R/O */
1749d8415d6SMarc Zyngier #define c13_TID_PRIV	(TPIDR_EL1 * 2)	/* Thread ID, Privileged */
1759d8415d6SMarc Zyngier #define c10_AMAIR0	(AMAIR_EL1 * 2)	/* Aux Memory Attr Indirection Reg */
1769d8415d6SMarc Zyngier #define c10_AMAIR1	(c10_AMAIR0 + 1)/* Aux Memory Attr Indirection Reg */
1779d8415d6SMarc Zyngier #define c14_CNTKCTL	(CNTKCTL_EL1 * 2) /* Timer Control Register (PL1) */
1789d8415d6SMarc Zyngier 
1799d8415d6SMarc Zyngier #define cp14_DBGDSCRext	(MDSCR_EL1 * 2)
1809d8415d6SMarc Zyngier #define cp14_DBGBCR0	(DBGBCR0_EL1 * 2)
1819d8415d6SMarc Zyngier #define cp14_DBGBVR0	(DBGBVR0_EL1 * 2)
1829d8415d6SMarc Zyngier #define cp14_DBGBXVR0	(cp14_DBGBVR0 + 1)
1839d8415d6SMarc Zyngier #define cp14_DBGWCR0	(DBGWCR0_EL1 * 2)
1849d8415d6SMarc Zyngier #define cp14_DBGWVR0	(DBGWVR0_EL1 * 2)
1859d8415d6SMarc Zyngier #define cp14_DBGDCCINT	(MDCCINT_EL1 * 2)
1869d8415d6SMarc Zyngier 
1879d8415d6SMarc Zyngier #define NR_COPRO_REGS	(NR_SYS_REGS * 2)
1889d8415d6SMarc Zyngier 
1894f8d6632SMarc Zyngier struct kvm_cpu_context {
1904f8d6632SMarc Zyngier 	struct kvm_regs	gp_regs;
19140033a61SMarc Zyngier 	union {
1924f8d6632SMarc Zyngier 		u64 sys_regs[NR_SYS_REGS];
19372564016SMarc Zyngier 		u32 copro[NR_COPRO_REGS];
19440033a61SMarc Zyngier 	};
1954f8d6632SMarc Zyngier };
1964f8d6632SMarc Zyngier 
1974f8d6632SMarc Zyngier typedef struct kvm_cpu_context kvm_cpu_context_t;
1984f8d6632SMarc Zyngier 
1994f8d6632SMarc Zyngier struct kvm_vcpu_arch {
2004f8d6632SMarc Zyngier 	struct kvm_cpu_context ctxt;
2014f8d6632SMarc Zyngier 
2024f8d6632SMarc Zyngier 	/* HYP configuration */
2034f8d6632SMarc Zyngier 	u64 hcr_el2;
20456c7f5e7SAlex Bennée 	u32 mdcr_el2;
2054f8d6632SMarc Zyngier 
2064f8d6632SMarc Zyngier 	/* Exception Information */
2074f8d6632SMarc Zyngier 	struct kvm_vcpu_fault_info fault;
2084f8d6632SMarc Zyngier 
20984e690bfSAlex Bennée 	/* Guest debug state */
2100c557ed4SMarc Zyngier 	u64 debug_flags;
2110c557ed4SMarc Zyngier 
21284e690bfSAlex Bennée 	/*
21384e690bfSAlex Bennée 	 * We maintain more than a single set of debug registers to support
21484e690bfSAlex Bennée 	 * debugging the guest from the host and to maintain separate host and
21584e690bfSAlex Bennée 	 * guest state during world switches. vcpu_debug_state are the debug
21684e690bfSAlex Bennée 	 * registers of the vcpu as the guest sees them.  host_debug_state are
217834bf887SAlex Bennée 	 * the host registers which are saved and restored during
218834bf887SAlex Bennée 	 * world switches. external_debug_state contains the debug
219834bf887SAlex Bennée 	 * values we want to debug the guest. This is set via the
220834bf887SAlex Bennée 	 * KVM_SET_GUEST_DEBUG ioctl.
22184e690bfSAlex Bennée 	 *
22284e690bfSAlex Bennée 	 * debug_ptr points to the set of debug registers that should be loaded
22384e690bfSAlex Bennée 	 * onto the hardware when running the guest.
22484e690bfSAlex Bennée 	 */
22584e690bfSAlex Bennée 	struct kvm_guest_debug_arch *debug_ptr;
22684e690bfSAlex Bennée 	struct kvm_guest_debug_arch vcpu_debug_state;
227834bf887SAlex Bennée 	struct kvm_guest_debug_arch external_debug_state;
22884e690bfSAlex Bennée 
2294f8d6632SMarc Zyngier 	/* Pointer to host CPU context */
2304f8d6632SMarc Zyngier 	kvm_cpu_context_t *host_cpu_context;
231f85279b4SWill Deacon 	struct {
232f85279b4SWill Deacon 		/* {Break,watch}point registers */
233f85279b4SWill Deacon 		struct kvm_guest_debug_arch regs;
234f85279b4SWill Deacon 		/* Statistical profiling extension */
235f85279b4SWill Deacon 		u64 pmscr_el1;
236f85279b4SWill Deacon 	} host_debug_state;
2374f8d6632SMarc Zyngier 
2384f8d6632SMarc Zyngier 	/* VGIC state */
2394f8d6632SMarc Zyngier 	struct vgic_cpu vgic_cpu;
2404f8d6632SMarc Zyngier 	struct arch_timer_cpu timer_cpu;
24104fe4726SShannon Zhao 	struct kvm_pmu pmu;
2424f8d6632SMarc Zyngier 
2434f8d6632SMarc Zyngier 	/*
2444f8d6632SMarc Zyngier 	 * Anything that is not used directly from assembly code goes
2454f8d6632SMarc Zyngier 	 * here.
2464f8d6632SMarc Zyngier 	 */
2474f8d6632SMarc Zyngier 
248337b99bfSAlex Bennée 	/*
249337b99bfSAlex Bennée 	 * Guest registers we preserve during guest debugging.
250337b99bfSAlex Bennée 	 *
251337b99bfSAlex Bennée 	 * These shadow registers are updated by the kvm_handle_sys_reg
252337b99bfSAlex Bennée 	 * trap handler if the guest accesses or updates them while we
253337b99bfSAlex Bennée 	 * are using guest debug.
254337b99bfSAlex Bennée 	 */
255337b99bfSAlex Bennée 	struct {
256337b99bfSAlex Bennée 		u32	mdscr_el1;
257337b99bfSAlex Bennée 	} guest_debug_preserved;
258337b99bfSAlex Bennée 
2593781528eSEric Auger 	/* vcpu power-off state */
2603781528eSEric Auger 	bool power_off;
2614f8d6632SMarc Zyngier 
2623b92830aSEric Auger 	/* Don't run the guest (internal implementation need) */
2633b92830aSEric Auger 	bool pause;
2643b92830aSEric Auger 
2654f8d6632SMarc Zyngier 	/* IO related fields */
2664f8d6632SMarc Zyngier 	struct kvm_decode mmio_decode;
2674f8d6632SMarc Zyngier 
2684f8d6632SMarc Zyngier 	/* Interrupt related fields */
2694f8d6632SMarc Zyngier 	u64 irq_lines;		/* IRQ and FIQ levels */
2704f8d6632SMarc Zyngier 
2714f8d6632SMarc Zyngier 	/* Cache some mmu pages needed inside spinlock regions */
2724f8d6632SMarc Zyngier 	struct kvm_mmu_memory_cache mmu_page_cache;
2734f8d6632SMarc Zyngier 
2744f8d6632SMarc Zyngier 	/* Target CPU and feature flags */
2756c8c0c4dSChen Gang 	int target;
2764f8d6632SMarc Zyngier 	DECLARE_BITMAP(features, KVM_VCPU_MAX_FEATURES);
2774f8d6632SMarc Zyngier 
2784f8d6632SMarc Zyngier 	/* Detect first run of a vcpu */
2794f8d6632SMarc Zyngier 	bool has_run_once;
2804f8d6632SMarc Zyngier };
2814f8d6632SMarc Zyngier 
2824f8d6632SMarc Zyngier #define vcpu_gp_regs(v)		(&(v)->arch.ctxt.gp_regs)
2834f8d6632SMarc Zyngier #define vcpu_sys_reg(v,r)	((v)->arch.ctxt.sys_regs[(r)])
28472564016SMarc Zyngier /*
28572564016SMarc Zyngier  * CP14 and CP15 live in the same array, as they are backed by the
28672564016SMarc Zyngier  * same system registers.
28772564016SMarc Zyngier  */
28872564016SMarc Zyngier #define vcpu_cp14(v,r)		((v)->arch.ctxt.copro[(r)])
28972564016SMarc Zyngier #define vcpu_cp15(v,r)		((v)->arch.ctxt.copro[(r)])
2904f8d6632SMarc Zyngier 
291f0a3eaffSVictor Kamensky #ifdef CONFIG_CPU_BIG_ENDIAN
292dedf97e8SMarc Zyngier #define vcpu_cp15_64_high(v,r)	vcpu_cp15((v),(r))
293dedf97e8SMarc Zyngier #define vcpu_cp15_64_low(v,r)	vcpu_cp15((v),(r) + 1)
294f0a3eaffSVictor Kamensky #else
295dedf97e8SMarc Zyngier #define vcpu_cp15_64_high(v,r)	vcpu_cp15((v),(r) + 1)
296dedf97e8SMarc Zyngier #define vcpu_cp15_64_low(v,r)	vcpu_cp15((v),(r))
297f0a3eaffSVictor Kamensky #endif
298f0a3eaffSVictor Kamensky 
2994f8d6632SMarc Zyngier struct kvm_vm_stat {
3008a7e75d4SSuraj Jitindar Singh 	ulong remote_tlb_flush;
3014f8d6632SMarc Zyngier };
3024f8d6632SMarc Zyngier 
3034f8d6632SMarc Zyngier struct kvm_vcpu_stat {
3048a7e75d4SSuraj Jitindar Singh 	u64 halt_successful_poll;
3058a7e75d4SSuraj Jitindar Singh 	u64 halt_attempted_poll;
3068a7e75d4SSuraj Jitindar Singh 	u64 halt_poll_invalid;
3078a7e75d4SSuraj Jitindar Singh 	u64 halt_wakeup;
3088a7e75d4SSuraj Jitindar Singh 	u64 hvc_exit_stat;
309b19e6892SAmit Tomar 	u64 wfe_exit_stat;
310b19e6892SAmit Tomar 	u64 wfi_exit_stat;
311b19e6892SAmit Tomar 	u64 mmio_exit_user;
312b19e6892SAmit Tomar 	u64 mmio_exit_kernel;
313b19e6892SAmit Tomar 	u64 exits;
3144f8d6632SMarc Zyngier };
3154f8d6632SMarc Zyngier 
316473bdc0eSAnup Patel int kvm_vcpu_preferred_target(struct kvm_vcpu_init *init);
3174f8d6632SMarc Zyngier unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu);
3184f8d6632SMarc Zyngier int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices);
3194f8d6632SMarc Zyngier int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg);
3204f8d6632SMarc Zyngier int kvm_arm_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg);
3214f8d6632SMarc Zyngier 
3224f8d6632SMarc Zyngier #define KVM_ARCH_WANT_MMU_NOTIFIER
3234f8d6632SMarc Zyngier int kvm_unmap_hva(struct kvm *kvm, unsigned long hva);
3244f8d6632SMarc Zyngier int kvm_unmap_hva_range(struct kvm *kvm,
3254f8d6632SMarc Zyngier 			unsigned long start, unsigned long end);
3264f8d6632SMarc Zyngier void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte);
32735307b9aSMarc Zyngier int kvm_age_hva(struct kvm *kvm, unsigned long start, unsigned long end);
32835307b9aSMarc Zyngier int kvm_test_age_hva(struct kvm *kvm, unsigned long hva);
3294f8d6632SMarc Zyngier 
3304f8d6632SMarc Zyngier struct kvm_vcpu *kvm_arm_get_running_vcpu(void);
3314000be42SWill Deacon struct kvm_vcpu * __percpu *kvm_get_running_vcpus(void);
332b13216cfSChristoffer Dall void kvm_arm_halt_guest(struct kvm *kvm);
333b13216cfSChristoffer Dall void kvm_arm_resume_guest(struct kvm *kvm);
3344f8d6632SMarc Zyngier 
335a0bf9776SArd Biesheuvel u64 __kvm_call_hyp(void *hypfn, ...);
33622b39ca3SMarc Zyngier #define kvm_call_hyp(f, ...) __kvm_call_hyp(kvm_ksym_ref(f), ##__VA_ARGS__)
33722b39ca3SMarc Zyngier 
338cf5d3188SChristoffer Dall void force_vm_exit(const cpumask_t *mask);
3398199ed0eSMario Smarduch void kvm_mmu_wp_memory_region(struct kvm *kvm, int slot);
3404f8d6632SMarc Zyngier 
3414f8d6632SMarc Zyngier int handle_exit(struct kvm_vcpu *vcpu, struct kvm_run *run,
3424f8d6632SMarc Zyngier 		int exception_index);
3434f8d6632SMarc Zyngier 
3444f8d6632SMarc Zyngier int kvm_perf_init(void);
3454f8d6632SMarc Zyngier int kvm_perf_teardown(void);
3464f8d6632SMarc Zyngier 
3474429fc64SAndre Przywara struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr);
3484429fc64SAndre Przywara 
34912fda812SMarc Zyngier static inline void __cpu_init_hyp_mode(phys_addr_t pgd_ptr,
350092bd143SMarc Zyngier 				       unsigned long hyp_stack_ptr,
351092bd143SMarc Zyngier 				       unsigned long vector_ptr)
352092bd143SMarc Zyngier {
353092bd143SMarc Zyngier 	/*
35463a1e1c9SMark Rutland 	 * Call initialization code, and switch to the full blown HYP code.
35563a1e1c9SMark Rutland 	 * If the cpucaps haven't been finalized yet, something has gone very
35663a1e1c9SMark Rutland 	 * wrong, and hyp will crash and burn when it uses any
35763a1e1c9SMark Rutland 	 * cpus_have_const_cap() wrapper.
358092bd143SMarc Zyngier 	 */
35963a1e1c9SMark Rutland 	BUG_ON(!static_branch_likely(&arm64_const_caps_ready));
3603421e9d8SMarc Zyngier 	__kvm_call_hyp((void *)pgd_ptr, hyp_stack_ptr, vector_ptr);
361092bd143SMarc Zyngier }
362092bd143SMarc Zyngier 
3630865e636SRadim Krčmář static inline void kvm_arch_hardware_unsetup(void) {}
3640865e636SRadim Krčmář static inline void kvm_arch_sync_events(struct kvm *kvm) {}
3650865e636SRadim Krčmář static inline void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) {}
3660865e636SRadim Krčmář static inline void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu) {}
3673491caf2SChristian Borntraeger static inline void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu) {}
3680865e636SRadim Krčmář 
36956c7f5e7SAlex Bennée void kvm_arm_init_debug(void);
37056c7f5e7SAlex Bennée void kvm_arm_setup_debug(struct kvm_vcpu *vcpu);
37156c7f5e7SAlex Bennée void kvm_arm_clear_debug(struct kvm_vcpu *vcpu);
37284e690bfSAlex Bennée void kvm_arm_reset_debug_ptr(struct kvm_vcpu *vcpu);
373bb0c70bcSShannon Zhao int kvm_arm_vcpu_arch_set_attr(struct kvm_vcpu *vcpu,
374bb0c70bcSShannon Zhao 			       struct kvm_device_attr *attr);
375bb0c70bcSShannon Zhao int kvm_arm_vcpu_arch_get_attr(struct kvm_vcpu *vcpu,
376bb0c70bcSShannon Zhao 			       struct kvm_device_attr *attr);
377bb0c70bcSShannon Zhao int kvm_arm_vcpu_arch_has_attr(struct kvm_vcpu *vcpu,
378bb0c70bcSShannon Zhao 			       struct kvm_device_attr *attr);
37956c7f5e7SAlex Bennée 
38021a4179cSMarc Zyngier static inline void __cpu_init_stage2(void)
38121a4179cSMarc Zyngier {
3826141570cSMarc Zyngier 	u32 parange = kvm_call_hyp(__init_stage2_translation);
3836141570cSMarc Zyngier 
3846141570cSMarc Zyngier 	WARN_ONCE(parange < 40,
3856141570cSMarc Zyngier 		  "PARange is %d bits, unsupported configuration!", parange);
38621a4179cSMarc Zyngier }
38721a4179cSMarc Zyngier 
38817eed27bSDave Martin /*
38917eed27bSDave Martin  * All host FP/SIMD state is restored on guest exit, so nothing needs
39017eed27bSDave Martin  * doing here except in the SVE case:
39117eed27bSDave Martin */
39217eed27bSDave Martin static inline void kvm_fpsimd_flush_cpu_state(void)
39317eed27bSDave Martin {
39417eed27bSDave Martin 	if (system_supports_sve())
39517eed27bSDave Martin 		sve_flush_cpu_state();
39617eed27bSDave Martin }
39717eed27bSDave Martin 
3984f8d6632SMarc Zyngier #endif /* __ARM64_KVM_HOST_H__ */
399