1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2012 ARM Ltd.
4 * Author: Marc Zyngier <marc.zyngier@arm.com>
11 * The arm64 hcall implementation uses x0 to specify the hcall
12 * number. A value less than HVC_STUB_HCALL_NR indicates a special
13 * hcall, such as set vector. Any other value is handled in a
14 * hypervisor specific way.
16 * The hypercall is allowed to clobber any of the caller-saved
17 * registers (x0-x18), so it is advisable to use it through the
18 * indirection of a function call (as implemented in hyp-stub.S).
22 * HVC_SET_VECTORS - Set the value of the vbar_el2 register.
24 * @x1: Physical address of the new vector table.
26 #define HVC_SET_VECTORS 0
29 * HVC_SOFT_RESTART - CPU soft reset, used by the cpu_soft_restart routine.
31 #define HVC_SOFT_RESTART 1
34 * HVC_RESET_VECTORS - Restore the vectors to the original HYP stubs
36 #define HVC_RESET_VECTORS 2
38 /* Max number of HYP stub hypercalls */
39 #define HVC_STUB_HCALL_NR 3
41 /* Error returned when an invalid stub number is passed into x0 */
42 #define HVC_STUB_ERR 0xbadca11
44 #define BOOT_CPU_MODE_EL1 (0xe11)
45 #define BOOT_CPU_MODE_EL2 (0xe12)
49 #include <asm/ptrace.h>
50 #include <asm/sections.h>
51 #include <asm/sysreg.h>
52 #include <asm/cpufeature.h>
55 * __boot_cpu_mode records what mode CPUs were booted in.
56 * A correctly-implemented bootloader must start all CPUs in the same mode:
57 * In this case, both 32bit halves of __boot_cpu_mode will contain the
58 * same value (either 0 if booted in EL1, BOOT_CPU_MODE_EL2 if booted in EL2).
60 * Should the bootloader fail to do this, the two values will be different.
61 * This allows the kernel to flag an error when the secondaries have come up.
63 extern u32 __boot_cpu_mode
[2];
65 void __hyp_set_vectors(phys_addr_t phys_vector_base
);
66 void __hyp_reset_vectors(void);
68 DECLARE_STATIC_KEY_FALSE(kvm_protected_mode_initialized
);
70 /* Reports the availability of HYP mode */
71 static inline bool is_hyp_mode_available(void)
74 * If KVM protected mode is initialized, all CPUs must have been booted
75 * in EL2. Avoid checking __boot_cpu_mode as CPUs now come up in EL1.
77 if (IS_ENABLED(CONFIG_KVM
) &&
78 static_branch_likely(&kvm_protected_mode_initialized
))
81 return (__boot_cpu_mode
[0] == BOOT_CPU_MODE_EL2
&&
82 __boot_cpu_mode
[1] == BOOT_CPU_MODE_EL2
);
85 /* Check if the bootloader has booted CPUs in different modes */
86 static inline bool is_hyp_mode_mismatched(void)
89 * If KVM protected mode is initialized, all CPUs must have been booted
90 * in EL2. Avoid checking __boot_cpu_mode as CPUs now come up in EL1.
92 if (IS_ENABLED(CONFIG_KVM
) &&
93 static_branch_likely(&kvm_protected_mode_initialized
))
96 return __boot_cpu_mode
[0] != __boot_cpu_mode
[1];
99 static inline bool is_kernel_in_hyp_mode(void)
101 return read_sysreg(CurrentEL
) == CurrentEL_EL2
;
104 static __always_inline
bool has_vhe(void)
107 * Code only run in VHE/NVHE hyp context can assume VHE is present or
108 * absent. Otherwise fall back to caps.
110 if (is_vhe_hyp_code())
112 else if (is_nvhe_hyp_code())
115 return cpus_have_final_cap(ARM64_HAS_VIRT_HOST_EXTN
);
118 static __always_inline
bool is_protected_kvm_enabled(void)
120 if (is_vhe_hyp_code())
123 return cpus_have_final_cap(ARM64_KVM_PROTECTED_MODE
);
126 #endif /* __ASSEMBLY__ */
128 #endif /* ! __ASM__VIRT_H */