1*91f16700Schasinglulu/* 2*91f16700Schasinglulu * Copyright (c) 2013-2022, ARM Limited and Contributors. All rights reserved. 3*91f16700Schasinglulu * 4*91f16700Schasinglulu * SPDX-License-Identifier: BSD-3-Clause 5*91f16700Schasinglulu */ 6*91f16700Schasinglulu 7*91f16700Schasinglulu#include <arch.h> 8*91f16700Schasinglulu#include <asm_macros.S> 9*91f16700Schasinglulu#include <bl1/bl1.h> 10*91f16700Schasinglulu#include <common/bl_common.h> 11*91f16700Schasinglulu#include <context.h> 12*91f16700Schasinglulu 13*91f16700Schasinglulu/* ----------------------------------------------------------------------------- 14*91f16700Schasinglulu * Very simple stackless exception handlers used by BL1. 15*91f16700Schasinglulu * ----------------------------------------------------------------------------- 16*91f16700Schasinglulu */ 17*91f16700Schasinglulu .globl bl1_exceptions 18*91f16700Schasinglulu 19*91f16700Schasingluluvector_base bl1_exceptions 20*91f16700Schasinglulu 21*91f16700Schasinglulu /* ----------------------------------------------------- 22*91f16700Schasinglulu * Current EL with SP0 : 0x0 - 0x200 23*91f16700Schasinglulu * ----------------------------------------------------- 24*91f16700Schasinglulu */ 25*91f16700Schasingluluvector_entry SynchronousExceptionSP0 26*91f16700Schasinglulu mov x0, #SYNC_EXCEPTION_SP_EL0 27*91f16700Schasinglulu bl plat_report_exception 28*91f16700Schasinglulu no_ret plat_panic_handler 29*91f16700Schasingluluend_vector_entry SynchronousExceptionSP0 30*91f16700Schasinglulu 31*91f16700Schasingluluvector_entry IrqSP0 32*91f16700Schasinglulu mov x0, #IRQ_SP_EL0 33*91f16700Schasinglulu bl plat_report_exception 34*91f16700Schasinglulu no_ret plat_panic_handler 35*91f16700Schasingluluend_vector_entry IrqSP0 36*91f16700Schasinglulu 37*91f16700Schasingluluvector_entry FiqSP0 38*91f16700Schasinglulu mov x0, #FIQ_SP_EL0 39*91f16700Schasinglulu bl plat_report_exception 40*91f16700Schasinglulu no_ret plat_panic_handler 41*91f16700Schasingluluend_vector_entry FiqSP0 42*91f16700Schasinglulu 43*91f16700Schasingluluvector_entry SErrorSP0 44*91f16700Schasinglulu mov x0, #SERROR_SP_EL0 45*91f16700Schasinglulu bl plat_report_exception 46*91f16700Schasinglulu no_ret plat_panic_handler 47*91f16700Schasingluluend_vector_entry SErrorSP0 48*91f16700Schasinglulu 49*91f16700Schasinglulu /* ----------------------------------------------------- 50*91f16700Schasinglulu * Current EL with SPx: 0x200 - 0x400 51*91f16700Schasinglulu * ----------------------------------------------------- 52*91f16700Schasinglulu */ 53*91f16700Schasingluluvector_entry SynchronousExceptionSPx 54*91f16700Schasinglulu mov x0, #SYNC_EXCEPTION_SP_ELX 55*91f16700Schasinglulu bl plat_report_exception 56*91f16700Schasinglulu no_ret plat_panic_handler 57*91f16700Schasingluluend_vector_entry SynchronousExceptionSPx 58*91f16700Schasinglulu 59*91f16700Schasingluluvector_entry IrqSPx 60*91f16700Schasinglulu mov x0, #IRQ_SP_ELX 61*91f16700Schasinglulu bl plat_report_exception 62*91f16700Schasinglulu no_ret plat_panic_handler 63*91f16700Schasingluluend_vector_entry IrqSPx 64*91f16700Schasinglulu 65*91f16700Schasingluluvector_entry FiqSPx 66*91f16700Schasinglulu mov x0, #FIQ_SP_ELX 67*91f16700Schasinglulu bl plat_report_exception 68*91f16700Schasinglulu no_ret plat_panic_handler 69*91f16700Schasingluluend_vector_entry FiqSPx 70*91f16700Schasinglulu 71*91f16700Schasingluluvector_entry SErrorSPx 72*91f16700Schasinglulu mov x0, #SERROR_SP_ELX 73*91f16700Schasinglulu bl plat_report_exception 74*91f16700Schasinglulu no_ret plat_panic_handler 75*91f16700Schasingluluend_vector_entry SErrorSPx 76*91f16700Schasinglulu 77*91f16700Schasinglulu /* ----------------------------------------------------- 78*91f16700Schasinglulu * Lower EL using AArch64 : 0x400 - 0x600 79*91f16700Schasinglulu * ----------------------------------------------------- 80*91f16700Schasinglulu */ 81*91f16700Schasingluluvector_entry SynchronousExceptionA64 82*91f16700Schasinglulu /* Enable the SError interrupt */ 83*91f16700Schasinglulu msr daifclr, #DAIF_ABT_BIT 84*91f16700Schasinglulu 85*91f16700Schasinglulu str x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR] 86*91f16700Schasinglulu 87*91f16700Schasinglulu /* Expect only SMC exceptions */ 88*91f16700Schasinglulu mrs x30, esr_el3 89*91f16700Schasinglulu ubfx x30, x30, #ESR_EC_SHIFT, #ESR_EC_LENGTH 90*91f16700Schasinglulu cmp x30, #EC_AARCH64_SMC 91*91f16700Schasinglulu b.ne unexpected_sync_exception 92*91f16700Schasinglulu 93*91f16700Schasinglulu b smc_handler64 94*91f16700Schasingluluend_vector_entry SynchronousExceptionA64 95*91f16700Schasinglulu 96*91f16700Schasingluluvector_entry IrqA64 97*91f16700Schasinglulu mov x0, #IRQ_AARCH64 98*91f16700Schasinglulu bl plat_report_exception 99*91f16700Schasinglulu no_ret plat_panic_handler 100*91f16700Schasingluluend_vector_entry IrqA64 101*91f16700Schasinglulu 102*91f16700Schasingluluvector_entry FiqA64 103*91f16700Schasinglulu mov x0, #FIQ_AARCH64 104*91f16700Schasinglulu bl plat_report_exception 105*91f16700Schasinglulu no_ret plat_panic_handler 106*91f16700Schasingluluend_vector_entry FiqA64 107*91f16700Schasinglulu 108*91f16700Schasingluluvector_entry SErrorA64 109*91f16700Schasinglulu mov x0, #SERROR_AARCH64 110*91f16700Schasinglulu bl plat_report_exception 111*91f16700Schasinglulu no_ret plat_panic_handler 112*91f16700Schasingluluend_vector_entry SErrorA64 113*91f16700Schasinglulu 114*91f16700Schasinglulu /* ----------------------------------------------------- 115*91f16700Schasinglulu * Lower EL using AArch32 : 0x600 - 0x800 116*91f16700Schasinglulu * ----------------------------------------------------- 117*91f16700Schasinglulu */ 118*91f16700Schasingluluvector_entry SynchronousExceptionA32 119*91f16700Schasinglulu mov x0, #SYNC_EXCEPTION_AARCH32 120*91f16700Schasinglulu bl plat_report_exception 121*91f16700Schasinglulu no_ret plat_panic_handler 122*91f16700Schasingluluend_vector_entry SynchronousExceptionA32 123*91f16700Schasinglulu 124*91f16700Schasingluluvector_entry IrqA32 125*91f16700Schasinglulu mov x0, #IRQ_AARCH32 126*91f16700Schasinglulu bl plat_report_exception 127*91f16700Schasinglulu no_ret plat_panic_handler 128*91f16700Schasingluluend_vector_entry IrqA32 129*91f16700Schasinglulu 130*91f16700Schasingluluvector_entry FiqA32 131*91f16700Schasinglulu mov x0, #FIQ_AARCH32 132*91f16700Schasinglulu bl plat_report_exception 133*91f16700Schasinglulu no_ret plat_panic_handler 134*91f16700Schasingluluend_vector_entry FiqA32 135*91f16700Schasinglulu 136*91f16700Schasingluluvector_entry SErrorA32 137*91f16700Schasinglulu mov x0, #SERROR_AARCH32 138*91f16700Schasinglulu bl plat_report_exception 139*91f16700Schasinglulu no_ret plat_panic_handler 140*91f16700Schasingluluend_vector_entry SErrorA32 141*91f16700Schasinglulu 142*91f16700Schasinglulu 143*91f16700Schasinglulufunc smc_handler64 144*91f16700Schasinglulu 145*91f16700Schasinglulu /* ---------------------------------------------- 146*91f16700Schasinglulu * Detect if this is a RUN_IMAGE or other SMC. 147*91f16700Schasinglulu * ---------------------------------------------- 148*91f16700Schasinglulu */ 149*91f16700Schasinglulu mov x30, #BL1_SMC_RUN_IMAGE 150*91f16700Schasinglulu cmp x30, x0 151*91f16700Schasinglulu b.ne smc_handler 152*91f16700Schasinglulu 153*91f16700Schasinglulu /* ------------------------------------------------ 154*91f16700Schasinglulu * Make sure only Secure world reaches here. 155*91f16700Schasinglulu * ------------------------------------------------ 156*91f16700Schasinglulu */ 157*91f16700Schasinglulu mrs x30, scr_el3 158*91f16700Schasinglulu tst x30, #SCR_NS_BIT 159*91f16700Schasinglulu b.ne unexpected_sync_exception 160*91f16700Schasinglulu 161*91f16700Schasinglulu /* ---------------------------------------------- 162*91f16700Schasinglulu * Handling RUN_IMAGE SMC. First switch back to 163*91f16700Schasinglulu * SP_EL0 for the C runtime stack. 164*91f16700Schasinglulu * ---------------------------------------------- 165*91f16700Schasinglulu */ 166*91f16700Schasinglulu ldr x30, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP] 167*91f16700Schasinglulu msr spsel, #MODE_SP_EL0 168*91f16700Schasinglulu mov sp, x30 169*91f16700Schasinglulu 170*91f16700Schasinglulu /* --------------------------------------------------------------------- 171*91f16700Schasinglulu * Pass EL3 control to next BL image. 172*91f16700Schasinglulu * Here it expects X1 with the address of a entry_point_info_t 173*91f16700Schasinglulu * structure describing the next BL image entrypoint. 174*91f16700Schasinglulu * --------------------------------------------------------------------- 175*91f16700Schasinglulu */ 176*91f16700Schasinglulu mov x20, x1 177*91f16700Schasinglulu 178*91f16700Schasinglulu mov x0, x20 179*91f16700Schasinglulu bl bl1_print_next_bl_ep_info 180*91f16700Schasinglulu 181*91f16700Schasinglulu ldp x0, x1, [x20, #ENTRY_POINT_INFO_PC_OFFSET] 182*91f16700Schasinglulu msr elr_el3, x0 183*91f16700Schasinglulu msr spsr_el3, x1 184*91f16700Schasinglulu ubfx x0, x1, #MODE_EL_SHIFT, #2 185*91f16700Schasinglulu cmp x0, #MODE_EL3 186*91f16700Schasinglulu b.ne unexpected_sync_exception 187*91f16700Schasinglulu 188*91f16700Schasinglulu bl disable_mmu_icache_el3 189*91f16700Schasinglulu tlbi alle3 190*91f16700Schasinglulu dsb ish /* ERET implies ISB, so it is not needed here */ 191*91f16700Schasinglulu 192*91f16700Schasinglulu#if SPIN_ON_BL1_EXIT 193*91f16700Schasinglulu bl print_debug_loop_message 194*91f16700Schasingluludebug_loop: 195*91f16700Schasinglulu b debug_loop 196*91f16700Schasinglulu#endif 197*91f16700Schasinglulu 198*91f16700Schasinglulu mov x0, x20 199*91f16700Schasinglulu bl bl1_plat_prepare_exit 200*91f16700Schasinglulu 201*91f16700Schasinglulu ldp x6, x7, [x20, #(ENTRY_POINT_INFO_ARGS_OFFSET + 0x30)] 202*91f16700Schasinglulu ldp x4, x5, [x20, #(ENTRY_POINT_INFO_ARGS_OFFSET + 0x20)] 203*91f16700Schasinglulu ldp x2, x3, [x20, #(ENTRY_POINT_INFO_ARGS_OFFSET + 0x10)] 204*91f16700Schasinglulu ldp x0, x1, [x20, #(ENTRY_POINT_INFO_ARGS_OFFSET + 0x0)] 205*91f16700Schasinglulu exception_return 206*91f16700Schasingluluendfunc smc_handler64 207*91f16700Schasinglulu 208*91f16700Schasingluluunexpected_sync_exception: 209*91f16700Schasinglulu mov x0, #SYNC_EXCEPTION_AARCH64 210*91f16700Schasinglulu bl plat_report_exception 211*91f16700Schasinglulu no_ret plat_panic_handler 212*91f16700Schasinglulu 213*91f16700Schasinglulu /* ----------------------------------------------------- 214*91f16700Schasinglulu * Save Secure/Normal world context and jump to 215*91f16700Schasinglulu * BL1 SMC handler. 216*91f16700Schasinglulu * ----------------------------------------------------- 217*91f16700Schasinglulu */ 218*91f16700Schasinglulusmc_handler: 219*91f16700Schasinglulu /* ----------------------------------------------------- 220*91f16700Schasinglulu * Save x0-x29 and ARMv8.3-PAuth (if enabled) registers. 221*91f16700Schasinglulu * Save PMCR_EL0 and disable Cycle Counter. 222*91f16700Schasinglulu * TODO: Revisit to store only SMCCC specified registers. 223*91f16700Schasinglulu * ----------------------------------------------------- 224*91f16700Schasinglulu */ 225*91f16700Schasinglulu bl prepare_el3_entry 226*91f16700Schasinglulu 227*91f16700Schasinglulu#if ENABLE_PAUTH 228*91f16700Schasinglulu /* ----------------------------------------------------- 229*91f16700Schasinglulu * Load and program stored APIAKey firmware key. 230*91f16700Schasinglulu * Re-enable pointer authentication in EL3, as it was 231*91f16700Schasinglulu * disabled before jumping to the next boot image. 232*91f16700Schasinglulu * ----------------------------------------------------- 233*91f16700Schasinglulu */ 234*91f16700Schasinglulu bl pauth_load_bl1_apiakey_enable 235*91f16700Schasinglulu#endif 236*91f16700Schasinglulu /* ----------------------------------------------------- 237*91f16700Schasinglulu * Populate the parameters for the SMC handler. We 238*91f16700Schasinglulu * already have x0-x4 in place. x5 will point to a 239*91f16700Schasinglulu * cookie (not used now). x6 will point to the context 240*91f16700Schasinglulu * structure (SP_EL3) and x7 will contain flags we need 241*91f16700Schasinglulu * to pass to the handler. 242*91f16700Schasinglulu * ----------------------------------------------------- 243*91f16700Schasinglulu */ 244*91f16700Schasinglulu mov x5, xzr 245*91f16700Schasinglulu mov x6, sp 246*91f16700Schasinglulu 247*91f16700Schasinglulu /* ----------------------------------------------------- 248*91f16700Schasinglulu * Restore the saved C runtime stack value which will 249*91f16700Schasinglulu * become the new SP_EL0 i.e. EL3 runtime stack. It was 250*91f16700Schasinglulu * saved in the 'cpu_context' structure prior to the last 251*91f16700Schasinglulu * ERET from EL3. 252*91f16700Schasinglulu * ----------------------------------------------------- 253*91f16700Schasinglulu */ 254*91f16700Schasinglulu ldr x12, [x6, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP] 255*91f16700Schasinglulu 256*91f16700Schasinglulu /* --------------------------------------------- 257*91f16700Schasinglulu * Switch back to SP_EL0 for the C runtime stack. 258*91f16700Schasinglulu * --------------------------------------------- 259*91f16700Schasinglulu */ 260*91f16700Schasinglulu msr spsel, #MODE_SP_EL0 261*91f16700Schasinglulu mov sp, x12 262*91f16700Schasinglulu 263*91f16700Schasinglulu /* ----------------------------------------------------- 264*91f16700Schasinglulu * Save the SPSR_EL3, ELR_EL3, & SCR_EL3 in case there 265*91f16700Schasinglulu * is a world switch during SMC handling. 266*91f16700Schasinglulu * ----------------------------------------------------- 267*91f16700Schasinglulu */ 268*91f16700Schasinglulu mrs x16, spsr_el3 269*91f16700Schasinglulu mrs x17, elr_el3 270*91f16700Schasinglulu mrs x18, scr_el3 271*91f16700Schasinglulu stp x16, x17, [x6, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3] 272*91f16700Schasinglulu str x18, [x6, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3] 273*91f16700Schasinglulu 274*91f16700Schasinglulu /* Copy SCR_EL3.NS bit to the flag to indicate caller's security */ 275*91f16700Schasinglulu bfi x7, x18, #0, #1 276*91f16700Schasinglulu 277*91f16700Schasinglulu /* ----------------------------------------------------- 278*91f16700Schasinglulu * Go to BL1 SMC handler. 279*91f16700Schasinglulu * ----------------------------------------------------- 280*91f16700Schasinglulu */ 281*91f16700Schasinglulu bl bl1_smc_handler 282*91f16700Schasinglulu 283*91f16700Schasinglulu /* ----------------------------------------------------- 284*91f16700Schasinglulu * Do the transition to next BL image. 285*91f16700Schasinglulu * ----------------------------------------------------- 286*91f16700Schasinglulu */ 287*91f16700Schasinglulu b el3_exit 288