xref: /openbmc/linux/arch/arm64/include/asm/kvm_hyp.h (revision 13aeb9b400c5d7c5e979fdbbf994c787487f7889)
1caab277bSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
213720a56SMarc Zyngier /*
313720a56SMarc Zyngier  * Copyright (C) 2015 - ARM Ltd
413720a56SMarc Zyngier  * Author: Marc Zyngier <marc.zyngier@arm.com>
513720a56SMarc Zyngier  */
613720a56SMarc Zyngier 
713720a56SMarc Zyngier #ifndef __ARM64_KVM_HYP_H__
813720a56SMarc Zyngier #define __ARM64_KVM_HYP_H__
913720a56SMarc Zyngier 
1013720a56SMarc Zyngier #include <linux/compiler.h>
1113720a56SMarc Zyngier #include <linux/kvm_host.h>
121e4448c5SMarc Zyngier #include <asm/alternative.h>
1313720a56SMarc Zyngier #include <asm/sysreg.h>
1413720a56SMarc Zyngier 
1596547368SSami Tolvanen #define __hyp_text __section(.hyp.text) notrace __noscs
1613720a56SMarc Zyngier 
1713720a56SMarc Zyngier #define read_sysreg_elx(r,nvh,vh)					\
1813720a56SMarc Zyngier 	({								\
1913720a56SMarc Zyngier 		u64 reg;						\
20fdec2a9eSDave Martin 		asm volatile(ALTERNATIVE(__mrs_s("%0", r##nvh),	\
21be604c61SKees Cook 					 __mrs_s("%0", r##vh),		\
2213720a56SMarc Zyngier 					 ARM64_HAS_VIRT_HOST_EXTN)	\
2313720a56SMarc Zyngier 			     : "=r" (reg));				\
2413720a56SMarc Zyngier 		reg;							\
2513720a56SMarc Zyngier 	})
2613720a56SMarc Zyngier 
2713720a56SMarc Zyngier #define write_sysreg_elx(v,r,nvh,vh)					\
2813720a56SMarc Zyngier 	do {								\
2913720a56SMarc Zyngier 		u64 __val = (u64)(v);					\
30fdec2a9eSDave Martin 		asm volatile(ALTERNATIVE(__msr_s(r##nvh, "%x0"),	\
31be604c61SKees Cook 					 __msr_s(r##vh, "%x0"),		\
3213720a56SMarc Zyngier 					 ARM64_HAS_VIRT_HOST_EXTN)	\
3313720a56SMarc Zyngier 					 : : "rZ" (__val));		\
3413720a56SMarc Zyngier 	} while (0)
3513720a56SMarc Zyngier 
3613720a56SMarc Zyngier /*
3713720a56SMarc Zyngier  * Unified accessors for registers that have a different encoding
3813720a56SMarc Zyngier  * between VHE and non-VHE. They must be specified without their "ELx"
39fdec2a9eSDave Martin  * encoding, but with the SYS_ prefix, as defined in asm/sysreg.h.
4013720a56SMarc Zyngier  */
4113720a56SMarc Zyngier 
4213720a56SMarc Zyngier #define read_sysreg_el0(r)	read_sysreg_elx(r, _EL0, _EL02)
4313720a56SMarc Zyngier #define write_sysreg_el0(v,r)	write_sysreg_elx(v, r, _EL0, _EL02)
4413720a56SMarc Zyngier #define read_sysreg_el1(r)	read_sysreg_elx(r, _EL1, _EL12)
4513720a56SMarc Zyngier #define write_sysreg_el1(v,r)	write_sysreg_elx(v, r, _EL1, _EL12)
46fdec2a9eSDave Martin #define read_sysreg_el2(r)	read_sysreg_elx(r, _EL2, _EL1)
47fdec2a9eSDave Martin #define write_sysreg_el2(v,r)	write_sysreg_elx(v, r, _EL2, _EL1)
4813720a56SMarc Zyngier 
498c2d146eSJames Morse /*
508c2d146eSJames Morse  * Without an __arch_swab32(), we fall back to ___constant_swab32(), but the
518c2d146eSJames Morse  * static inline can allow the compiler to out-of-line this. KVM always wants
528c2d146eSJames Morse  * the macro version as its always inlined.
538c2d146eSJames Morse  */
548c2d146eSJames Morse #define __kvm_swab32(x)	___constant_swab32(x)
558c2d146eSJames Morse 
563272f0d0SMarc Zyngier int __vgic_v2_perform_cpuif_access(struct kvm_vcpu *vcpu);
5713720a56SMarc Zyngier 
58fc5d1f1aSChristoffer Dall void __vgic_v3_save_state(struct vgic_v3_cpu_if *cpu_if);
59fc5d1f1aSChristoffer Dall void __vgic_v3_restore_state(struct vgic_v3_cpu_if *cpu_if);
60fc5d1f1aSChristoffer Dall void __vgic_v3_activate_traps(struct vgic_v3_cpu_if *cpu_if);
61fc5d1f1aSChristoffer Dall void __vgic_v3_deactivate_traps(struct vgic_v3_cpu_if *cpu_if);
62fc5d1f1aSChristoffer Dall void __vgic_v3_save_aprs(struct vgic_v3_cpu_if *cpu_if);
63fc5d1f1aSChristoffer Dall void __vgic_v3_restore_aprs(struct vgic_v3_cpu_if *cpu_if);
6459da1cbfSMarc Zyngier int __vgic_v3_perform_cpuif_access(struct kvm_vcpu *vcpu);
6513720a56SMarc Zyngier 
66688c50aaSChristoffer Dall void __timer_enable_traps(struct kvm_vcpu *vcpu);
67688c50aaSChristoffer Dall void __timer_disable_traps(struct kvm_vcpu *vcpu);
6813720a56SMarc Zyngier 
69*13aeb9b4SDavid Brazdil #ifdef __KVM_NVHE_HYPERVISOR__
704cdecabaSChristoffer Dall void __sysreg_save_state_nvhe(struct kvm_cpu_context *ctxt);
714cdecabaSChristoffer Dall void __sysreg_restore_state_nvhe(struct kvm_cpu_context *ctxt);
72*13aeb9b4SDavid Brazdil #else
73f837453dSChristoffer Dall void sysreg_save_host_state_vhe(struct kvm_cpu_context *ctxt);
74f837453dSChristoffer Dall void sysreg_restore_host_state_vhe(struct kvm_cpu_context *ctxt);
75f837453dSChristoffer Dall void sysreg_save_guest_state_vhe(struct kvm_cpu_context *ctxt);
76f837453dSChristoffer Dall void sysreg_restore_guest_state_vhe(struct kvm_cpu_context *ctxt);
77*13aeb9b4SDavid Brazdil #endif
7813720a56SMarc Zyngier 
79014c4c77SChristoffer Dall void __debug_switch_to_guest(struct kvm_vcpu *vcpu);
80014c4c77SChristoffer Dall void __debug_switch_to_host(struct kvm_vcpu *vcpu);
8113720a56SMarc Zyngier 
8213720a56SMarc Zyngier void __fpsimd_save_state(struct user_fpsimd_state *fp_regs);
8313720a56SMarc Zyngier void __fpsimd_restore_state(struct user_fpsimd_state *fp_regs);
8413720a56SMarc Zyngier 
8509cf57ebSDavid Brazdil #ifndef __KVM_NVHE_HYPERVISOR__
86a2465629SChristoffer Dall void activate_traps_vhe_load(struct kvm_vcpu *vcpu);
87a2465629SChristoffer Dall void deactivate_traps_vhe_put(void);
8809cf57ebSDavid Brazdil #endif
89a2465629SChristoffer Dall 
9013720a56SMarc Zyngier u64 __guest_enter(struct kvm_vcpu *vcpu, struct kvm_cpu_context *host_ctxt);
9109cf57ebSDavid Brazdil 
9209cf57ebSDavid Brazdil void __noreturn hyp_panic(struct kvm_cpu_context *host_ctxt);
9309cf57ebSDavid Brazdil #ifdef __KVM_NVHE_HYPERVISOR__
9413720a56SMarc Zyngier void __noreturn __hyp_do_panic(unsigned long, ...);
9509cf57ebSDavid Brazdil #endif
9613720a56SMarc Zyngier 
9713720a56SMarc Zyngier #endif /* __ARM64_KVM_HYP_H__ */
9813720a56SMarc Zyngier 
99