Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * Copyright (C) 2012 ARM Ltd.
0004  * Author: Marc Zyngier <marc.zyngier@arm.com>
0005  */
0006 
0007 #ifndef __ASM__VIRT_H
0008 #define __ASM__VIRT_H
0009 
0010 /*
0011  * The arm64 hcall implementation uses x0 to specify the hcall
0012  * number. A value less than HVC_STUB_HCALL_NR indicates a special
0013  * hcall, such as set vector. Any other value is handled in a
0014  * hypervisor specific way.
0015  *
0016  * The hypercall is allowed to clobber any of the caller-saved
0017  * registers (x0-x18), so it is advisable to use it through the
0018  * indirection of a function call (as implemented in hyp-stub.S).
0019  */
0020 
0021 /*
0022  * HVC_SET_VECTORS - Set the value of the vbar_el2 register.
0023  *
0024  * @x1: Physical address of the new vector table.
0025  */
0026 #define HVC_SET_VECTORS 0
0027 
0028 /*
0029  * HVC_SOFT_RESTART - CPU soft reset, used by the cpu_soft_restart routine.
0030  */
0031 #define HVC_SOFT_RESTART 1
0032 
0033 /*
0034  * HVC_RESET_VECTORS - Restore the vectors to the original HYP stubs
0035  */
0036 #define HVC_RESET_VECTORS 2
0037 
0038 /*
0039  * HVC_FINALISE_EL2 - Upgrade the CPU from EL1 to EL2, if possible
0040  */
0041 #define HVC_FINALISE_EL2    3
0042 
0043 /* Max number of HYP stub hypercalls */
0044 #define HVC_STUB_HCALL_NR 4
0045 
0046 /* Error returned when an invalid stub number is passed into x0 */
0047 #define HVC_STUB_ERR    0xbadca11
0048 
0049 #define BOOT_CPU_MODE_EL1   (0xe11)
0050 #define BOOT_CPU_MODE_EL2   (0xe12)
0051 
0052 /*
0053  * Flags returned together with the boot mode, but not preserved in
0054  * __boot_cpu_mode. Used by the idreg override code to work out the
0055  * boot state.
0056  */
0057 #define BOOT_CPU_FLAG_E2H   BIT_ULL(32)
0058 
0059 #ifndef __ASSEMBLY__
0060 
0061 #include <asm/ptrace.h>
0062 #include <asm/sections.h>
0063 #include <asm/sysreg.h>
0064 #include <asm/cpufeature.h>
0065 
0066 /*
0067  * __boot_cpu_mode records what mode CPUs were booted in.
0068  * A correctly-implemented bootloader must start all CPUs in the same mode:
0069  * In this case, both 32bit halves of __boot_cpu_mode will contain the
0070  * same value (either 0 if booted in EL1, BOOT_CPU_MODE_EL2 if booted in EL2).
0071  *
0072  * Should the bootloader fail to do this, the two values will be different.
0073  * This allows the kernel to flag an error when the secondaries have come up.
0074  */
0075 extern u32 __boot_cpu_mode[2];
0076 
0077 #define ARM64_VECTOR_TABLE_LEN  SZ_2K
0078 
0079 void __hyp_set_vectors(phys_addr_t phys_vector_base);
0080 void __hyp_reset_vectors(void);
0081 
0082 DECLARE_STATIC_KEY_FALSE(kvm_protected_mode_initialized);
0083 
0084 /* Reports the availability of HYP mode */
0085 static inline bool is_hyp_mode_available(void)
0086 {
0087     /*
0088      * If KVM protected mode is initialized, all CPUs must have been booted
0089      * in EL2. Avoid checking __boot_cpu_mode as CPUs now come up in EL1.
0090      */
0091     if (IS_ENABLED(CONFIG_KVM) &&
0092         static_branch_likely(&kvm_protected_mode_initialized))
0093         return true;
0094 
0095     return (__boot_cpu_mode[0] == BOOT_CPU_MODE_EL2 &&
0096         __boot_cpu_mode[1] == BOOT_CPU_MODE_EL2);
0097 }
0098 
0099 /* Check if the bootloader has booted CPUs in different modes */
0100 static inline bool is_hyp_mode_mismatched(void)
0101 {
0102     /*
0103      * If KVM protected mode is initialized, all CPUs must have been booted
0104      * in EL2. Avoid checking __boot_cpu_mode as CPUs now come up in EL1.
0105      */
0106     if (IS_ENABLED(CONFIG_KVM) &&
0107         static_branch_likely(&kvm_protected_mode_initialized))
0108         return false;
0109 
0110     return __boot_cpu_mode[0] != __boot_cpu_mode[1];
0111 }
0112 
0113 static inline bool is_kernel_in_hyp_mode(void)
0114 {
0115     return read_sysreg(CurrentEL) == CurrentEL_EL2;
0116 }
0117 
0118 static __always_inline bool has_vhe(void)
0119 {
0120     /*
0121      * Code only run in VHE/NVHE hyp context can assume VHE is present or
0122      * absent. Otherwise fall back to caps.
0123      * This allows the compiler to discard VHE-specific code from the
0124      * nVHE object, reducing the number of external symbol references
0125      * needed to link.
0126      */
0127     if (is_vhe_hyp_code())
0128         return true;
0129     else if (is_nvhe_hyp_code())
0130         return false;
0131     else
0132         return cpus_have_final_cap(ARM64_HAS_VIRT_HOST_EXTN);
0133 }
0134 
0135 static __always_inline bool is_protected_kvm_enabled(void)
0136 {
0137     if (is_vhe_hyp_code())
0138         return false;
0139     else
0140         return cpus_have_final_cap(ARM64_KVM_PROTECTED_MODE);
0141 }
0142 
0143 static inline bool is_hyp_nvhe(void)
0144 {
0145     return is_hyp_mode_available() && !is_kernel_in_hyp_mode();
0146 }
0147 
0148 #endif /* __ASSEMBLY__ */
0149 
0150 #endif /* ! __ASM__VIRT_H */