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);
81fa729bc7SSudeep Holla bool is_kvm_arm_initialised(void);
82712c6ff4SMarc Zyngier
83f19f6644SDavid Brazdil DECLARE_STATIC_KEY_FALSE(kvm_protected_mode_initialized);
84f19f6644SDavid Brazdil
is_pkvm_initialized(void)85*d81a91afSQuentin Perret static inline bool is_pkvm_initialized(void)
86*d81a91afSQuentin Perret {
87*d81a91afSQuentin Perret return IS_ENABLED(CONFIG_KVM) &&
88*d81a91afSQuentin Perret static_branch_likely(&kvm_protected_mode_initialized);
89*d81a91afSQuentin Perret }
90*d81a91afSQuentin Perret
91f35a9205SMarc Zyngier /* Reports the availability of HYP mode */
is_hyp_mode_available(void)92f35a9205SMarc Zyngier static inline bool is_hyp_mode_available(void)
93f35a9205SMarc Zyngier {
94f19f6644SDavid Brazdil /*
95f19f6644SDavid Brazdil * If KVM protected mode is initialized, all CPUs must have been booted
96f19f6644SDavid Brazdil * in EL2. Avoid checking __boot_cpu_mode as CPUs now come up in EL1.
97f19f6644SDavid Brazdil */
98*d81a91afSQuentin Perret if (is_pkvm_initialized())
99f19f6644SDavid Brazdil return true;
100f19f6644SDavid Brazdil
101f35a9205SMarc Zyngier return (__boot_cpu_mode[0] == BOOT_CPU_MODE_EL2 &&
102f35a9205SMarc Zyngier __boot_cpu_mode[1] == BOOT_CPU_MODE_EL2);
103f35a9205SMarc Zyngier }
104f35a9205SMarc Zyngier
105f35a9205SMarc Zyngier /* Check if the bootloader has booted CPUs in different modes */
is_hyp_mode_mismatched(void)106f35a9205SMarc Zyngier static inline bool is_hyp_mode_mismatched(void)
107f35a9205SMarc Zyngier {
108f19f6644SDavid Brazdil /*
109f19f6644SDavid Brazdil * If KVM protected mode is initialized, all CPUs must have been booted
110f19f6644SDavid Brazdil * in EL2. Avoid checking __boot_cpu_mode as CPUs now come up in EL1.
111f19f6644SDavid Brazdil */
112*d81a91afSQuentin Perret if (is_pkvm_initialized())
113f19f6644SDavid Brazdil return false;
114f19f6644SDavid Brazdil
115f35a9205SMarc Zyngier return __boot_cpu_mode[0] != __boot_cpu_mode[1];
116f35a9205SMarc Zyngier }
117f35a9205SMarc Zyngier
is_kernel_in_hyp_mode(void)11835230be8SMarc Zyngier static __always_inline bool is_kernel_in_hyp_mode(void)
11982deae0fSMarc Zyngier {
12035230be8SMarc Zyngier BUILD_BUG_ON(__is_defined(__KVM_NVHE_HYPERVISOR__) ||
12135230be8SMarc Zyngier __is_defined(__KVM_VHE_HYPERVISOR__));
1221f3d8699SMark Rutland return read_sysreg(CurrentEL) == CurrentEL_EL2;
12382deae0fSMarc Zyngier }
12482deae0fSMarc Zyngier
has_vhe(void)1255c37f1aeSJames Morse static __always_inline bool has_vhe(void)
126488f94d7SJintack Lim {
12753b67112SDavid Brazdil /*
128e9a33caeSMark Rutland * Code only run in VHE/NVHE hyp context can assume VHE is present or
129e9a33caeSMark Rutland * absent. Otherwise fall back to caps.
130112f3babSWill Deacon * This allows the compiler to discard VHE-specific code from the
131112f3babSWill Deacon * nVHE object, reducing the number of external symbol references
132112f3babSWill Deacon * needed to link.
13353b67112SDavid Brazdil */
134e9a33caeSMark Rutland if (is_vhe_hyp_code())
135488f94d7SJintack Lim return true;
136e9a33caeSMark Rutland else if (is_nvhe_hyp_code())
137488f94d7SJintack Lim return false;
13853b67112SDavid Brazdil else
13953b67112SDavid Brazdil return cpus_have_final_cap(ARM64_HAS_VIRT_HOST_EXTN);
140488f94d7SJintack Lim }
141488f94d7SJintack Lim
is_protected_kvm_enabled(void)1423eb681fbSDavid Brazdil static __always_inline bool is_protected_kvm_enabled(void)
1433eb681fbSDavid Brazdil {
1443eb681fbSDavid Brazdil if (is_vhe_hyp_code())
1453eb681fbSDavid Brazdil return false;
1463eb681fbSDavid Brazdil else
1473eb681fbSDavid Brazdil return cpus_have_final_cap(ARM64_KVM_PROTECTED_MODE);
1483eb681fbSDavid Brazdil }
1493eb681fbSDavid Brazdil
has_hvhe(void)150e2d6c906SMarc Zyngier static __always_inline bool has_hvhe(void)
151e2d6c906SMarc Zyngier {
152e2d6c906SMarc Zyngier if (is_vhe_hyp_code())
153e2d6c906SMarc Zyngier return false;
154e2d6c906SMarc Zyngier
155e2d6c906SMarc Zyngier return cpus_have_final_cap(ARM64_KVM_HVHE);
156e2d6c906SMarc Zyngier }
157e2d6c906SMarc Zyngier
is_hyp_nvhe(void)158094a3684SPasha Tatashin static inline bool is_hyp_nvhe(void)
159094a3684SPasha Tatashin {
160094a3684SPasha Tatashin return is_hyp_mode_available() && !is_kernel_in_hyp_mode();
161094a3684SPasha Tatashin }
162094a3684SPasha Tatashin
163f35a9205SMarc Zyngier #endif /* __ASSEMBLY__ */
164f35a9205SMarc Zyngier
165f35a9205SMarc Zyngier #endif /* ! __ASM__VIRT_H */
166