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 /* 39*7ddb0c3dSMarc Zyngier * HVC_FINALISE_EL2 - Upgrade the CPU from EL1 to EL2, if possible 40f3591822SMarc Zyngier */ 41*7ddb0c3dSMarc 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 52f35a9205SMarc Zyngier #ifndef __ASSEMBLY__ 53f35a9205SMarc Zyngier 5482deae0fSMarc Zyngier #include <asm/ptrace.h> 55ee78fdc7SJames Morse #include <asm/sections.h> 561f3d8699SMark Rutland #include <asm/sysreg.h> 57488f94d7SJintack Lim #include <asm/cpufeature.h> 5882deae0fSMarc Zyngier 59f35a9205SMarc Zyngier /* 60f35a9205SMarc Zyngier * __boot_cpu_mode records what mode CPUs were booted in. 61f35a9205SMarc Zyngier * A correctly-implemented bootloader must start all CPUs in the same mode: 62f35a9205SMarc Zyngier * In this case, both 32bit halves of __boot_cpu_mode will contain the 63f35a9205SMarc Zyngier * same value (either 0 if booted in EL1, BOOT_CPU_MODE_EL2 if booted in EL2). 64f35a9205SMarc Zyngier * 65f35a9205SMarc Zyngier * Should the bootloader fail to do this, the two values will be different. 66f35a9205SMarc Zyngier * This allows the kernel to flag an error when the secondaries have come up. 67f35a9205SMarc Zyngier */ 68f35a9205SMarc Zyngier extern u32 __boot_cpu_mode[2]; 69f35a9205SMarc Zyngier 70788bfdd9SPasha Tatashin #define ARM64_VECTOR_TABLE_LEN SZ_2K 71788bfdd9SPasha Tatashin 72712c6ff4SMarc Zyngier void __hyp_set_vectors(phys_addr_t phys_vector_base); 73fd0e0c61SMarc Zyngier void __hyp_reset_vectors(void); 74712c6ff4SMarc Zyngier 75f19f6644SDavid Brazdil DECLARE_STATIC_KEY_FALSE(kvm_protected_mode_initialized); 76f19f6644SDavid Brazdil 77f35a9205SMarc Zyngier /* Reports the availability of HYP mode */ 78f35a9205SMarc Zyngier static inline bool is_hyp_mode_available(void) 79f35a9205SMarc Zyngier { 80f19f6644SDavid Brazdil /* 81f19f6644SDavid Brazdil * If KVM protected mode is initialized, all CPUs must have been booted 82f19f6644SDavid Brazdil * in EL2. Avoid checking __boot_cpu_mode as CPUs now come up in EL1. 83f19f6644SDavid Brazdil */ 84f19f6644SDavid Brazdil if (IS_ENABLED(CONFIG_KVM) && 85f19f6644SDavid Brazdil static_branch_likely(&kvm_protected_mode_initialized)) 86f19f6644SDavid Brazdil return true; 87f19f6644SDavid Brazdil 88f35a9205SMarc Zyngier return (__boot_cpu_mode[0] == BOOT_CPU_MODE_EL2 && 89f35a9205SMarc Zyngier __boot_cpu_mode[1] == BOOT_CPU_MODE_EL2); 90f35a9205SMarc Zyngier } 91f35a9205SMarc Zyngier 92f35a9205SMarc Zyngier /* Check if the bootloader has booted CPUs in different modes */ 93f35a9205SMarc Zyngier static inline bool is_hyp_mode_mismatched(void) 94f35a9205SMarc Zyngier { 95f19f6644SDavid Brazdil /* 96f19f6644SDavid Brazdil * If KVM protected mode is initialized, all CPUs must have been booted 97f19f6644SDavid Brazdil * in EL2. Avoid checking __boot_cpu_mode as CPUs now come up in EL1. 98f19f6644SDavid Brazdil */ 99f19f6644SDavid Brazdil if (IS_ENABLED(CONFIG_KVM) && 100f19f6644SDavid Brazdil static_branch_likely(&kvm_protected_mode_initialized)) 101f19f6644SDavid Brazdil return false; 102f19f6644SDavid Brazdil 103f35a9205SMarc Zyngier return __boot_cpu_mode[0] != __boot_cpu_mode[1]; 104f35a9205SMarc Zyngier } 105f35a9205SMarc Zyngier 10682deae0fSMarc Zyngier static inline bool is_kernel_in_hyp_mode(void) 10782deae0fSMarc Zyngier { 1081f3d8699SMark Rutland return read_sysreg(CurrentEL) == CurrentEL_EL2; 10982deae0fSMarc Zyngier } 11082deae0fSMarc Zyngier 1115c37f1aeSJames Morse static __always_inline bool has_vhe(void) 112488f94d7SJintack Lim { 11353b67112SDavid Brazdil /* 114e9a33caeSMark Rutland * Code only run in VHE/NVHE hyp context can assume VHE is present or 115e9a33caeSMark Rutland * absent. Otherwise fall back to caps. 116112f3babSWill Deacon * This allows the compiler to discard VHE-specific code from the 117112f3babSWill Deacon * nVHE object, reducing the number of external symbol references 118112f3babSWill Deacon * needed to link. 11953b67112SDavid Brazdil */ 120e9a33caeSMark Rutland if (is_vhe_hyp_code()) 121488f94d7SJintack Lim return true; 122e9a33caeSMark Rutland else if (is_nvhe_hyp_code()) 123488f94d7SJintack Lim return false; 12453b67112SDavid Brazdil else 12553b67112SDavid Brazdil return cpus_have_final_cap(ARM64_HAS_VIRT_HOST_EXTN); 126488f94d7SJintack Lim } 127488f94d7SJintack Lim 1283eb681fbSDavid Brazdil static __always_inline bool is_protected_kvm_enabled(void) 1293eb681fbSDavid Brazdil { 1303eb681fbSDavid Brazdil if (is_vhe_hyp_code()) 1313eb681fbSDavid Brazdil return false; 1323eb681fbSDavid Brazdil else 1333eb681fbSDavid Brazdil return cpus_have_final_cap(ARM64_KVM_PROTECTED_MODE); 1343eb681fbSDavid Brazdil } 1353eb681fbSDavid Brazdil 136094a3684SPasha Tatashin static inline bool is_hyp_nvhe(void) 137094a3684SPasha Tatashin { 138094a3684SPasha Tatashin return is_hyp_mode_available() && !is_kernel_in_hyp_mode(); 139094a3684SPasha Tatashin } 140094a3684SPasha Tatashin 141f35a9205SMarc Zyngier #endif /* __ASSEMBLY__ */ 142f35a9205SMarc Zyngier 143f35a9205SMarc Zyngier #endif /* ! __ASM__VIRT_H */ 144