xref: /linux/arch/arm64/include/asm/virt.h (revision 35230be87ec6147c20e7433ab9d41e2fd2664631)
1caab277bSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
2f35a9205SMarc Zyngier /*
3f35a9205SMarc Zyngier  * Copyright (C) 2012 ARM Ltd.
4f35a9205SMarc Zyngier  * Author: Marc Zyngier <marc.zyngier@arm.com>
5f35a9205SMarc Zyngier  */
6f35a9205SMarc Zyngier 
7f35a9205SMarc Zyngier #ifndef __ASM__VIRT_H
8f35a9205SMarc Zyngier #define __ASM__VIRT_H
9f35a9205SMarc Zyngier 
10ad72e59fSGeoff Levand /*
11810c86eeSMarc Zyngier  * The arm64 hcall implementation uses x0 to specify the hcall
12810c86eeSMarc Zyngier  * number. A value less than HVC_STUB_HCALL_NR indicates a special
13810c86eeSMarc Zyngier  * hcall, such as set vector. Any other value is handled in a
14810c86eeSMarc Zyngier  * hypervisor specific way.
15810c86eeSMarc Zyngier  *
16810c86eeSMarc Zyngier  * The hypercall is allowed to clobber any of the caller-saved
17810c86eeSMarc Zyngier  * registers (x0-x18), so it is advisable to use it through the
18810c86eeSMarc Zyngier  * indirection of a function call (as implemented in hyp-stub.S).
19ad72e59fSGeoff Levand  */
20ad72e59fSGeoff Levand 
21ad72e59fSGeoff Levand /*
22ad72e59fSGeoff Levand  * HVC_SET_VECTORS - Set the value of the vbar_el2 register.
23ad72e59fSGeoff Levand  *
24ad72e59fSGeoff Levand  * @x1: Physical address of the new vector table.
25ad72e59fSGeoff Levand  */
260b51c547SMarc Zyngier #define HVC_SET_VECTORS 0
27ad72e59fSGeoff Levand 
28f9076ecfSGeoff Levand /*
29f9076ecfSGeoff Levand  * HVC_SOFT_RESTART - CPU soft reset, used by the cpu_soft_restart routine.
30f9076ecfSGeoff Levand  */
310b51c547SMarc Zyngier #define HVC_SOFT_RESTART 1
32f9076ecfSGeoff Levand 
33fd0e0c61SMarc Zyngier /*
34fd0e0c61SMarc Zyngier  * HVC_RESET_VECTORS - Restore the vectors to the original HYP stubs
35fd0e0c61SMarc Zyngier  */
360b51c547SMarc Zyngier #define HVC_RESET_VECTORS 2
37fd0e0c61SMarc Zyngier 
38f3591822SMarc Zyngier /*
397ddb0c3dSMarc Zyngier  * HVC_FINALISE_EL2 - Upgrade the CPU from EL1 to EL2, if possible
40f3591822SMarc Zyngier  */
417ddb0c3dSMarc Zyngier #define HVC_FINALISE_EL2	3
42f3591822SMarc Zyngier 
43fd0e0c61SMarc Zyngier /* Max number of HYP stub hypercalls */
44f3591822SMarc Zyngier #define HVC_STUB_HCALL_NR 4
45fd0e0c61SMarc Zyngier 
464993fdcfSMarc Zyngier /* Error returned when an invalid stub number is passed into x0 */
474993fdcfSMarc Zyngier #define HVC_STUB_ERR	0xbadca11
484993fdcfSMarc Zyngier 
49828e9834SMatthew Leach #define BOOT_CPU_MODE_EL1	(0xe11)
50828e9834SMatthew Leach #define BOOT_CPU_MODE_EL2	(0xe12)
51f35a9205SMarc Zyngier 
52b65e411dSMarc Zyngier /*
53b65e411dSMarc Zyngier  * Flags returned together with the boot mode, but not preserved in
54b65e411dSMarc Zyngier  * __boot_cpu_mode. Used by the idreg override code to work out the
55b65e411dSMarc Zyngier  * boot state.
56b65e411dSMarc Zyngier  */
57b65e411dSMarc Zyngier #define BOOT_CPU_FLAG_E2H	BIT_ULL(32)
58b65e411dSMarc Zyngier 
59f35a9205SMarc Zyngier #ifndef __ASSEMBLY__
60f35a9205SMarc Zyngier 
6182deae0fSMarc Zyngier #include <asm/ptrace.h>
62ee78fdc7SJames Morse #include <asm/sections.h>
631f3d8699SMark Rutland #include <asm/sysreg.h>
64488f94d7SJintack Lim #include <asm/cpufeature.h>
6582deae0fSMarc Zyngier 
66f35a9205SMarc Zyngier /*
67f35a9205SMarc Zyngier  * __boot_cpu_mode records what mode CPUs were booted in.
68f35a9205SMarc Zyngier  * A correctly-implemented bootloader must start all CPUs in the same mode:
69f35a9205SMarc Zyngier  * In this case, both 32bit halves of __boot_cpu_mode will contain the
70f35a9205SMarc Zyngier  * same value (either 0 if booted in EL1, BOOT_CPU_MODE_EL2 if booted in EL2).
71f35a9205SMarc Zyngier  *
72f35a9205SMarc Zyngier  * Should the bootloader fail to do this, the two values will be different.
73f35a9205SMarc Zyngier  * This allows the kernel to flag an error when the secondaries have come up.
74f35a9205SMarc Zyngier  */
75f35a9205SMarc Zyngier extern u32 __boot_cpu_mode[2];
76f35a9205SMarc Zyngier 
77788bfdd9SPasha Tatashin #define ARM64_VECTOR_TABLE_LEN	SZ_2K
78788bfdd9SPasha Tatashin 
79712c6ff4SMarc Zyngier void __hyp_set_vectors(phys_addr_t phys_vector_base);
80fd0e0c61SMarc Zyngier void __hyp_reset_vectors(void);
81712c6ff4SMarc Zyngier 
82f19f6644SDavid Brazdil DECLARE_STATIC_KEY_FALSE(kvm_protected_mode_initialized);
83f19f6644SDavid Brazdil 
84f35a9205SMarc Zyngier /* Reports the availability of HYP mode */
85f35a9205SMarc Zyngier static inline bool is_hyp_mode_available(void)
86f35a9205SMarc Zyngier {
87f19f6644SDavid Brazdil 	/*
88f19f6644SDavid Brazdil 	 * If KVM protected mode is initialized, all CPUs must have been booted
89f19f6644SDavid Brazdil 	 * in EL2. Avoid checking __boot_cpu_mode as CPUs now come up in EL1.
90f19f6644SDavid Brazdil 	 */
91f19f6644SDavid Brazdil 	if (IS_ENABLED(CONFIG_KVM) &&
92f19f6644SDavid Brazdil 	    static_branch_likely(&kvm_protected_mode_initialized))
93f19f6644SDavid Brazdil 		return true;
94f19f6644SDavid Brazdil 
95f35a9205SMarc Zyngier 	return (__boot_cpu_mode[0] == BOOT_CPU_MODE_EL2 &&
96f35a9205SMarc Zyngier 		__boot_cpu_mode[1] == BOOT_CPU_MODE_EL2);
97f35a9205SMarc Zyngier }
98f35a9205SMarc Zyngier 
99f35a9205SMarc Zyngier /* Check if the bootloader has booted CPUs in different modes */
100f35a9205SMarc Zyngier static inline bool is_hyp_mode_mismatched(void)
101f35a9205SMarc Zyngier {
102f19f6644SDavid Brazdil 	/*
103f19f6644SDavid Brazdil 	 * If KVM protected mode is initialized, all CPUs must have been booted
104f19f6644SDavid Brazdil 	 * in EL2. Avoid checking __boot_cpu_mode as CPUs now come up in EL1.
105f19f6644SDavid Brazdil 	 */
106f19f6644SDavid Brazdil 	if (IS_ENABLED(CONFIG_KVM) &&
107f19f6644SDavid Brazdil 	    static_branch_likely(&kvm_protected_mode_initialized))
108f19f6644SDavid Brazdil 		return false;
109f19f6644SDavid Brazdil 
110f35a9205SMarc Zyngier 	return __boot_cpu_mode[0] != __boot_cpu_mode[1];
111f35a9205SMarc Zyngier }
112f35a9205SMarc Zyngier 
113*35230be8SMarc Zyngier static __always_inline bool is_kernel_in_hyp_mode(void)
11482deae0fSMarc Zyngier {
115*35230be8SMarc Zyngier 	BUILD_BUG_ON(__is_defined(__KVM_NVHE_HYPERVISOR__) ||
116*35230be8SMarc Zyngier 		     __is_defined(__KVM_VHE_HYPERVISOR__));
1171f3d8699SMark Rutland 	return read_sysreg(CurrentEL) == CurrentEL_EL2;
11882deae0fSMarc Zyngier }
11982deae0fSMarc Zyngier 
1205c37f1aeSJames Morse static __always_inline bool has_vhe(void)
121488f94d7SJintack Lim {
12253b67112SDavid Brazdil 	/*
123e9a33caeSMark Rutland 	 * Code only run in VHE/NVHE hyp context can assume VHE is present or
124e9a33caeSMark Rutland 	 * absent. Otherwise fall back to caps.
125112f3babSWill Deacon 	 * This allows the compiler to discard VHE-specific code from the
126112f3babSWill Deacon 	 * nVHE object, reducing the number of external symbol references
127112f3babSWill Deacon 	 * needed to link.
12853b67112SDavid Brazdil 	 */
129e9a33caeSMark Rutland 	if (is_vhe_hyp_code())
130488f94d7SJintack Lim 		return true;
131e9a33caeSMark Rutland 	else if (is_nvhe_hyp_code())
132488f94d7SJintack Lim 		return false;
13353b67112SDavid Brazdil 	else
13453b67112SDavid Brazdil 		return cpus_have_final_cap(ARM64_HAS_VIRT_HOST_EXTN);
135488f94d7SJintack Lim }
136488f94d7SJintack Lim 
1373eb681fbSDavid Brazdil static __always_inline bool is_protected_kvm_enabled(void)
1383eb681fbSDavid Brazdil {
1393eb681fbSDavid Brazdil 	if (is_vhe_hyp_code())
1403eb681fbSDavid Brazdil 		return false;
1413eb681fbSDavid Brazdil 	else
1423eb681fbSDavid Brazdil 		return cpus_have_final_cap(ARM64_KVM_PROTECTED_MODE);
1433eb681fbSDavid Brazdil }
1443eb681fbSDavid Brazdil 
145094a3684SPasha Tatashin static inline bool is_hyp_nvhe(void)
146094a3684SPasha Tatashin {
147094a3684SPasha Tatashin 	return is_hyp_mode_available() && !is_kernel_in_hyp_mode();
148094a3684SPasha Tatashin }
149094a3684SPasha Tatashin 
150f35a9205SMarc Zyngier #endif /* __ASSEMBLY__ */
151f35a9205SMarc Zyngier 
152f35a9205SMarc Zyngier #endif /* ! __ASM__VIRT_H */
153