1*91f16700Schasinglulu/* 2*91f16700Schasinglulu * Copyright (c) 2013-2023, Arm Limited and Contributors. All rights reserved. 3*91f16700Schasinglulu * 4*91f16700Schasinglulu * SPDX-License-Identifier: BSD-3-Clause 5*91f16700Schasinglulu */ 6*91f16700Schasinglulu 7*91f16700Schasinglulu#include <platform_def.h> 8*91f16700Schasinglulu 9*91f16700Schasinglulu#include <arch.h> 10*91f16700Schasinglulu#include <asm_macros.S> 11*91f16700Schasinglulu#include <bl31/ea_handle.h> 12*91f16700Schasinglulu#include <bl31/interrupt_mgmt.h> 13*91f16700Schasinglulu#include <bl31/sync_handle.h> 14*91f16700Schasinglulu#include <common/runtime_svc.h> 15*91f16700Schasinglulu#include <context.h> 16*91f16700Schasinglulu#include <cpu_macros.S> 17*91f16700Schasinglulu#include <el3_common_macros.S> 18*91f16700Schasinglulu#include <lib/el3_runtime/cpu_data.h> 19*91f16700Schasinglulu#include <lib/smccc.h> 20*91f16700Schasinglulu 21*91f16700Schasinglulu .globl runtime_exceptions 22*91f16700Schasinglulu 23*91f16700Schasinglulu .globl sync_exception_sp_el0 24*91f16700Schasinglulu .globl irq_sp_el0 25*91f16700Schasinglulu .globl fiq_sp_el0 26*91f16700Schasinglulu .globl serror_sp_el0 27*91f16700Schasinglulu 28*91f16700Schasinglulu .globl sync_exception_sp_elx 29*91f16700Schasinglulu .globl irq_sp_elx 30*91f16700Schasinglulu .globl fiq_sp_elx 31*91f16700Schasinglulu .globl serror_sp_elx 32*91f16700Schasinglulu 33*91f16700Schasinglulu .globl sync_exception_aarch64 34*91f16700Schasinglulu .globl irq_aarch64 35*91f16700Schasinglulu .globl fiq_aarch64 36*91f16700Schasinglulu .globl serror_aarch64 37*91f16700Schasinglulu 38*91f16700Schasinglulu .globl sync_exception_aarch32 39*91f16700Schasinglulu .globl irq_aarch32 40*91f16700Schasinglulu .globl fiq_aarch32 41*91f16700Schasinglulu .globl serror_aarch32 42*91f16700Schasinglulu 43*91f16700Schasinglulu /* 44*91f16700Schasinglulu * Save LR and make x30 available as most of the routines in vector entry 45*91f16700Schasinglulu * need a free register 46*91f16700Schasinglulu */ 47*91f16700Schasinglulu .macro save_x30 48*91f16700Schasinglulu str x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR] 49*91f16700Schasinglulu .endm 50*91f16700Schasinglulu 51*91f16700Schasinglulu .macro restore_x30 52*91f16700Schasinglulu ldr x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR] 53*91f16700Schasinglulu .endm 54*91f16700Schasinglulu 55*91f16700Schasinglulu /* 56*91f16700Schasinglulu * Macro that synchronizes errors (EA) and checks for pending SError. 57*91f16700Schasinglulu * On detecting a pending SError it either reflects it back to lower 58*91f16700Schasinglulu * EL (KFH) or handles it in EL3 (FFH) based on EA routing model. 59*91f16700Schasinglulu */ 60*91f16700Schasinglulu .macro sync_and_handle_pending_serror 61*91f16700Schasinglulu synchronize_errors 62*91f16700Schasinglulu mrs x30, ISR_EL1 63*91f16700Schasinglulu tbz x30, #ISR_A_SHIFT, 2f 64*91f16700Schasinglulu#if FFH_SUPPORT 65*91f16700Schasinglulu mrs x30, scr_el3 66*91f16700Schasinglulu tst x30, #SCR_EA_BIT 67*91f16700Schasinglulu b.eq 1f 68*91f16700Schasinglulu bl handle_pending_async_ea 69*91f16700Schasinglulu b 2f 70*91f16700Schasinglulu#endif 71*91f16700Schasinglulu1: 72*91f16700Schasinglulu /* This function never returns, but need LR for decision making */ 73*91f16700Schasinglulu bl reflect_pending_async_ea_to_lower_el 74*91f16700Schasinglulu2: 75*91f16700Schasinglulu .endm 76*91f16700Schasinglulu 77*91f16700Schasinglulu /* --------------------------------------------------------------------- 78*91f16700Schasinglulu * This macro handles Synchronous exceptions. 79*91f16700Schasinglulu * Only SMC exceptions are supported. 80*91f16700Schasinglulu * --------------------------------------------------------------------- 81*91f16700Schasinglulu */ 82*91f16700Schasinglulu .macro handle_sync_exception 83*91f16700Schasinglulu#if ENABLE_RUNTIME_INSTRUMENTATION 84*91f16700Schasinglulu /* 85*91f16700Schasinglulu * Read the timestamp value and store it in per-cpu data. The value 86*91f16700Schasinglulu * will be extracted from per-cpu data by the C level SMC handler and 87*91f16700Schasinglulu * saved to the PMF timestamp region. 88*91f16700Schasinglulu */ 89*91f16700Schasinglulu mrs x30, cntpct_el0 90*91f16700Schasinglulu str x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29] 91*91f16700Schasinglulu mrs x29, tpidr_el3 92*91f16700Schasinglulu str x30, [x29, #CPU_DATA_PMF_TS0_OFFSET] 93*91f16700Schasinglulu ldr x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29] 94*91f16700Schasinglulu#endif 95*91f16700Schasinglulu 96*91f16700Schasinglulu mrs x30, esr_el3 97*91f16700Schasinglulu ubfx x30, x30, #ESR_EC_SHIFT, #ESR_EC_LENGTH 98*91f16700Schasinglulu 99*91f16700Schasinglulu /* Handle SMC exceptions separately from other synchronous exceptions */ 100*91f16700Schasinglulu cmp x30, #EC_AARCH32_SMC 101*91f16700Schasinglulu b.eq smc_handler32 102*91f16700Schasinglulu 103*91f16700Schasinglulu cmp x30, #EC_AARCH64_SMC 104*91f16700Schasinglulu b.eq sync_handler64 105*91f16700Schasinglulu 106*91f16700Schasinglulu cmp x30, #EC_AARCH64_SYS 107*91f16700Schasinglulu b.eq sync_handler64 108*91f16700Schasinglulu 109*91f16700Schasinglulu cmp x30, #EC_IMP_DEF_EL3 110*91f16700Schasinglulu b.eq imp_def_el3_handler 111*91f16700Schasinglulu 112*91f16700Schasinglulu /* If FFH Support then try to handle lower EL EA exceptions. */ 113*91f16700Schasinglulu#if FFH_SUPPORT 114*91f16700Schasinglulu mrs x30, scr_el3 115*91f16700Schasinglulu tst x30, #SCR_EA_BIT 116*91f16700Schasinglulu b.eq 1f 117*91f16700Schasinglulu b handle_lower_el_sync_ea 118*91f16700Schasinglulu#endif 119*91f16700Schasinglulu1: 120*91f16700Schasinglulu /* Synchronous exceptions other than the above are unhandled */ 121*91f16700Schasinglulu b report_unhandled_exception 122*91f16700Schasinglulu .endm 123*91f16700Schasinglulu 124*91f16700Schasingluluvector_base runtime_exceptions 125*91f16700Schasinglulu 126*91f16700Schasinglulu /* --------------------------------------------------------------------- 127*91f16700Schasinglulu * Current EL with SP_EL0 : 0x0 - 0x200 128*91f16700Schasinglulu * --------------------------------------------------------------------- 129*91f16700Schasinglulu */ 130*91f16700Schasingluluvector_entry sync_exception_sp_el0 131*91f16700Schasinglulu#ifdef MONITOR_TRAPS 132*91f16700Schasinglulu stp x29, x30, [sp, #-16]! 133*91f16700Schasinglulu 134*91f16700Schasinglulu mrs x30, esr_el3 135*91f16700Schasinglulu ubfx x30, x30, #ESR_EC_SHIFT, #ESR_EC_LENGTH 136*91f16700Schasinglulu 137*91f16700Schasinglulu /* Check for BRK */ 138*91f16700Schasinglulu cmp x30, #EC_BRK 139*91f16700Schasinglulu b.eq brk_handler 140*91f16700Schasinglulu 141*91f16700Schasinglulu ldp x29, x30, [sp], #16 142*91f16700Schasinglulu#endif /* MONITOR_TRAPS */ 143*91f16700Schasinglulu 144*91f16700Schasinglulu /* We don't expect any synchronous exceptions from EL3 */ 145*91f16700Schasinglulu b report_unhandled_exception 146*91f16700Schasingluluend_vector_entry sync_exception_sp_el0 147*91f16700Schasinglulu 148*91f16700Schasingluluvector_entry irq_sp_el0 149*91f16700Schasinglulu /* 150*91f16700Schasinglulu * EL3 code is non-reentrant. Any asynchronous exception is a serious 151*91f16700Schasinglulu * error. Loop infinitely. 152*91f16700Schasinglulu */ 153*91f16700Schasinglulu b report_unhandled_interrupt 154*91f16700Schasingluluend_vector_entry irq_sp_el0 155*91f16700Schasinglulu 156*91f16700Schasinglulu 157*91f16700Schasingluluvector_entry fiq_sp_el0 158*91f16700Schasinglulu b report_unhandled_interrupt 159*91f16700Schasingluluend_vector_entry fiq_sp_el0 160*91f16700Schasinglulu 161*91f16700Schasinglulu 162*91f16700Schasingluluvector_entry serror_sp_el0 163*91f16700Schasinglulu no_ret plat_handle_el3_ea 164*91f16700Schasingluluend_vector_entry serror_sp_el0 165*91f16700Schasinglulu 166*91f16700Schasinglulu /* --------------------------------------------------------------------- 167*91f16700Schasinglulu * Current EL with SP_ELx: 0x200 - 0x400 168*91f16700Schasinglulu * --------------------------------------------------------------------- 169*91f16700Schasinglulu */ 170*91f16700Schasingluluvector_entry sync_exception_sp_elx 171*91f16700Schasinglulu /* 172*91f16700Schasinglulu * This exception will trigger if anything went wrong during a previous 173*91f16700Schasinglulu * exception entry or exit or while handling an earlier unexpected 174*91f16700Schasinglulu * synchronous exception. There is a high probability that SP_EL3 is 175*91f16700Schasinglulu * corrupted. 176*91f16700Schasinglulu */ 177*91f16700Schasinglulu b report_unhandled_exception 178*91f16700Schasingluluend_vector_entry sync_exception_sp_elx 179*91f16700Schasinglulu 180*91f16700Schasingluluvector_entry irq_sp_elx 181*91f16700Schasinglulu b report_unhandled_interrupt 182*91f16700Schasingluluend_vector_entry irq_sp_elx 183*91f16700Schasinglulu 184*91f16700Schasingluluvector_entry fiq_sp_elx 185*91f16700Schasinglulu b report_unhandled_interrupt 186*91f16700Schasingluluend_vector_entry fiq_sp_elx 187*91f16700Schasinglulu 188*91f16700Schasingluluvector_entry serror_sp_elx 189*91f16700Schasinglulu#if FFH_SUPPORT 190*91f16700Schasinglulu /* 191*91f16700Schasinglulu * This will trigger if the exception was taken due to SError in EL3 or 192*91f16700Schasinglulu * because of pending asynchronous external aborts from lower EL that got 193*91f16700Schasinglulu * triggered due to implicit/explicit synchronization in EL3 (SCR_EL3.EA=1) 194*91f16700Schasinglulu * during EL3 entry. For the former case we continue with "plat_handle_el3_ea". 195*91f16700Schasinglulu * The later case will occur when PSTATE.A bit is cleared in 196*91f16700Schasinglulu * "handle_pending_async_ea". This means we are doing a nested 197*91f16700Schasinglulu * exception in EL3. Call the handler for async EA which will eret back to 198*91f16700Schasinglulu * original el3 handler if it is nested exception. Also, unmask EA so that we 199*91f16700Schasinglulu * catch any further EA arise when handling this nested exception at EL3. 200*91f16700Schasinglulu */ 201*91f16700Schasinglulu save_x30 202*91f16700Schasinglulu ldr x30, [sp, #CTX_EL3STATE_OFFSET + CTX_NESTED_EA_FLAG] 203*91f16700Schasinglulu cbz x30, 1f 204*91f16700Schasinglulu /* 205*91f16700Schasinglulu * This is nested exception handling, clear the flag to avoid taking this 206*91f16700Schasinglulu * path for further exceptions caused by EA handling 207*91f16700Schasinglulu */ 208*91f16700Schasinglulu str xzr, [sp, #CTX_EL3STATE_OFFSET + CTX_NESTED_EA_FLAG] 209*91f16700Schasinglulu unmask_async_ea 210*91f16700Schasinglulu b handle_lower_el_async_ea 211*91f16700Schasinglulu1: 212*91f16700Schasinglulu restore_x30 213*91f16700Schasinglulu#endif 214*91f16700Schasinglulu no_ret plat_handle_el3_ea 215*91f16700Schasinglulu 216*91f16700Schasingluluend_vector_entry serror_sp_elx 217*91f16700Schasinglulu 218*91f16700Schasinglulu /* --------------------------------------------------------------------- 219*91f16700Schasinglulu * Lower EL using AArch64 : 0x400 - 0x600 220*91f16700Schasinglulu * --------------------------------------------------------------------- 221*91f16700Schasinglulu */ 222*91f16700Schasingluluvector_entry sync_exception_aarch64 223*91f16700Schasinglulu /* 224*91f16700Schasinglulu * This exception vector will be the entry point for SMCs and traps 225*91f16700Schasinglulu * that are unhandled at lower ELs most commonly. SP_EL3 should point 226*91f16700Schasinglulu * to a valid cpu context where the general purpose and system register 227*91f16700Schasinglulu * state can be saved. 228*91f16700Schasinglulu */ 229*91f16700Schasinglulu save_x30 230*91f16700Schasinglulu apply_at_speculative_wa 231*91f16700Schasinglulu sync_and_handle_pending_serror 232*91f16700Schasinglulu unmask_async_ea 233*91f16700Schasinglulu handle_sync_exception 234*91f16700Schasingluluend_vector_entry sync_exception_aarch64 235*91f16700Schasinglulu 236*91f16700Schasingluluvector_entry irq_aarch64 237*91f16700Schasinglulu save_x30 238*91f16700Schasinglulu apply_at_speculative_wa 239*91f16700Schasinglulu sync_and_handle_pending_serror 240*91f16700Schasinglulu unmask_async_ea 241*91f16700Schasinglulu b handle_interrupt_exception 242*91f16700Schasingluluend_vector_entry irq_aarch64 243*91f16700Schasinglulu 244*91f16700Schasingluluvector_entry fiq_aarch64 245*91f16700Schasinglulu save_x30 246*91f16700Schasinglulu apply_at_speculative_wa 247*91f16700Schasinglulu sync_and_handle_pending_serror 248*91f16700Schasinglulu unmask_async_ea 249*91f16700Schasinglulu b handle_interrupt_exception 250*91f16700Schasingluluend_vector_entry fiq_aarch64 251*91f16700Schasinglulu 252*91f16700Schasinglulu /* 253*91f16700Schasinglulu * Need to synchronize any outstanding SError since we can get a burst of errors. 254*91f16700Schasinglulu * So reuse the sync mechanism to catch any further errors which are pending. 255*91f16700Schasinglulu */ 256*91f16700Schasingluluvector_entry serror_aarch64 257*91f16700Schasinglulu#if FFH_SUPPORT 258*91f16700Schasinglulu save_x30 259*91f16700Schasinglulu apply_at_speculative_wa 260*91f16700Schasinglulu sync_and_handle_pending_serror 261*91f16700Schasinglulu unmask_async_ea 262*91f16700Schasinglulu b handle_lower_el_async_ea 263*91f16700Schasinglulu#else 264*91f16700Schasinglulu b report_unhandled_exception 265*91f16700Schasinglulu#endif 266*91f16700Schasingluluend_vector_entry serror_aarch64 267*91f16700Schasinglulu 268*91f16700Schasinglulu /* --------------------------------------------------------------------- 269*91f16700Schasinglulu * Lower EL using AArch32 : 0x600 - 0x800 270*91f16700Schasinglulu * --------------------------------------------------------------------- 271*91f16700Schasinglulu */ 272*91f16700Schasingluluvector_entry sync_exception_aarch32 273*91f16700Schasinglulu /* 274*91f16700Schasinglulu * This exception vector will be the entry point for SMCs and traps 275*91f16700Schasinglulu * that are unhandled at lower ELs most commonly. SP_EL3 should point 276*91f16700Schasinglulu * to a valid cpu context where the general purpose and system register 277*91f16700Schasinglulu * state can be saved. 278*91f16700Schasinglulu */ 279*91f16700Schasinglulu save_x30 280*91f16700Schasinglulu apply_at_speculative_wa 281*91f16700Schasinglulu sync_and_handle_pending_serror 282*91f16700Schasinglulu unmask_async_ea 283*91f16700Schasinglulu handle_sync_exception 284*91f16700Schasingluluend_vector_entry sync_exception_aarch32 285*91f16700Schasinglulu 286*91f16700Schasingluluvector_entry irq_aarch32 287*91f16700Schasinglulu save_x30 288*91f16700Schasinglulu apply_at_speculative_wa 289*91f16700Schasinglulu sync_and_handle_pending_serror 290*91f16700Schasinglulu unmask_async_ea 291*91f16700Schasinglulu b handle_interrupt_exception 292*91f16700Schasingluluend_vector_entry irq_aarch32 293*91f16700Schasinglulu 294*91f16700Schasingluluvector_entry fiq_aarch32 295*91f16700Schasinglulu save_x30 296*91f16700Schasinglulu apply_at_speculative_wa 297*91f16700Schasinglulu sync_and_handle_pending_serror 298*91f16700Schasinglulu unmask_async_ea 299*91f16700Schasinglulu b handle_interrupt_exception 300*91f16700Schasingluluend_vector_entry fiq_aarch32 301*91f16700Schasinglulu 302*91f16700Schasinglulu /* 303*91f16700Schasinglulu * Need to synchronize any outstanding SError since we can get a burst of errors. 304*91f16700Schasinglulu * So reuse the sync mechanism to catch any further errors which are pending. 305*91f16700Schasinglulu */ 306*91f16700Schasingluluvector_entry serror_aarch32 307*91f16700Schasinglulu#if FFH_SUPPORT 308*91f16700Schasinglulu save_x30 309*91f16700Schasinglulu apply_at_speculative_wa 310*91f16700Schasinglulu sync_and_handle_pending_serror 311*91f16700Schasinglulu unmask_async_ea 312*91f16700Schasinglulu b handle_lower_el_async_ea 313*91f16700Schasinglulu#else 314*91f16700Schasinglulu b report_unhandled_exception 315*91f16700Schasinglulu#endif 316*91f16700Schasingluluend_vector_entry serror_aarch32 317*91f16700Schasinglulu 318*91f16700Schasinglulu#ifdef MONITOR_TRAPS 319*91f16700Schasinglulu .section .rodata.brk_string, "aS" 320*91f16700Schasinglulubrk_location: 321*91f16700Schasinglulu .asciz "Error at instruction 0x" 322*91f16700Schasinglulubrk_message: 323*91f16700Schasinglulu .asciz "Unexpected BRK instruction with value 0x" 324*91f16700Schasinglulu#endif /* MONITOR_TRAPS */ 325*91f16700Schasinglulu 326*91f16700Schasinglulu /* --------------------------------------------------------------------- 327*91f16700Schasinglulu * The following code handles secure monitor calls. 328*91f16700Schasinglulu * Depending upon the execution state from where the SMC has been 329*91f16700Schasinglulu * invoked, it frees some general purpose registers to perform the 330*91f16700Schasinglulu * remaining tasks. They involve finding the runtime service handler 331*91f16700Schasinglulu * that is the target of the SMC & switching to runtime stacks (SP_EL0) 332*91f16700Schasinglulu * before calling the handler. 333*91f16700Schasinglulu * 334*91f16700Schasinglulu * Note that x30 has been explicitly saved and can be used here 335*91f16700Schasinglulu * --------------------------------------------------------------------- 336*91f16700Schasinglulu */ 337*91f16700Schasinglulufunc sync_exception_handler 338*91f16700Schasinglulusmc_handler32: 339*91f16700Schasinglulu /* Check whether aarch32 issued an SMC64 */ 340*91f16700Schasinglulu tbnz x0, #FUNCID_CC_SHIFT, smc_prohibited 341*91f16700Schasinglulu 342*91f16700Schasinglulusync_handler64: 343*91f16700Schasinglulu /* NOTE: The code below must preserve x0-x4 */ 344*91f16700Schasinglulu 345*91f16700Schasinglulu /* 346*91f16700Schasinglulu * Save general purpose and ARMv8.3-PAuth registers (if enabled). 347*91f16700Schasinglulu * Also save PMCR_EL0 and set the PSTATE to a known state. 348*91f16700Schasinglulu */ 349*91f16700Schasinglulu bl prepare_el3_entry 350*91f16700Schasinglulu 351*91f16700Schasinglulu#if ENABLE_PAUTH 352*91f16700Schasinglulu /* Load and program APIAKey firmware key */ 353*91f16700Schasinglulu bl pauth_load_bl31_apiakey 354*91f16700Schasinglulu#endif 355*91f16700Schasinglulu 356*91f16700Schasinglulu /* 357*91f16700Schasinglulu * Populate the parameters for the SMC handler. 358*91f16700Schasinglulu * We already have x0-x4 in place. x5 will point to a cookie (not used 359*91f16700Schasinglulu * now). x6 will point to the context structure (SP_EL3) and x7 will 360*91f16700Schasinglulu * contain flags we need to pass to the handler. 361*91f16700Schasinglulu */ 362*91f16700Schasinglulu mov x5, xzr 363*91f16700Schasinglulu mov x6, sp 364*91f16700Schasinglulu 365*91f16700Schasinglulu /* 366*91f16700Schasinglulu * Restore the saved C runtime stack value which will become the new 367*91f16700Schasinglulu * SP_EL0 i.e. EL3 runtime stack. It was saved in the 'cpu_context' 368*91f16700Schasinglulu * structure prior to the last ERET from EL3. 369*91f16700Schasinglulu */ 370*91f16700Schasinglulu ldr x12, [x6, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP] 371*91f16700Schasinglulu 372*91f16700Schasinglulu /* Switch to SP_EL0 */ 373*91f16700Schasinglulu msr spsel, #MODE_SP_EL0 374*91f16700Schasinglulu 375*91f16700Schasinglulu /* 376*91f16700Schasinglulu * Save the SPSR_EL3 and ELR_EL3 in case there is a world 377*91f16700Schasinglulu * switch during SMC handling. 378*91f16700Schasinglulu * TODO: Revisit if all system registers can be saved later. 379*91f16700Schasinglulu */ 380*91f16700Schasinglulu mrs x16, spsr_el3 381*91f16700Schasinglulu mrs x17, elr_el3 382*91f16700Schasinglulu stp x16, x17, [x6, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3] 383*91f16700Schasinglulu 384*91f16700Schasinglulu /* Load SCR_EL3 */ 385*91f16700Schasinglulu mrs x18, scr_el3 386*91f16700Schasinglulu 387*91f16700Schasinglulu /* check for system register traps */ 388*91f16700Schasinglulu mrs x16, esr_el3 389*91f16700Schasinglulu ubfx x17, x16, #ESR_EC_SHIFT, #ESR_EC_LENGTH 390*91f16700Schasinglulu cmp x17, #EC_AARCH64_SYS 391*91f16700Schasinglulu b.eq sysreg_handler64 392*91f16700Schasinglulu 393*91f16700Schasinglulu /* Clear flag register */ 394*91f16700Schasinglulu mov x7, xzr 395*91f16700Schasinglulu 396*91f16700Schasinglulu#if ENABLE_RME 397*91f16700Schasinglulu /* Copy SCR_EL3.NSE bit to the flag to indicate caller's security */ 398*91f16700Schasinglulu ubfx x7, x18, #SCR_NSE_SHIFT, #1 399*91f16700Schasinglulu 400*91f16700Schasinglulu /* 401*91f16700Schasinglulu * Shift copied SCR_EL3.NSE bit by 5 to create space for 402*91f16700Schasinglulu * SCR_EL3.NS bit. Bit 5 of the flag corresponds to 403*91f16700Schasinglulu * the SCR_EL3.NSE bit. 404*91f16700Schasinglulu */ 405*91f16700Schasinglulu lsl x7, x7, #5 406*91f16700Schasinglulu#endif /* ENABLE_RME */ 407*91f16700Schasinglulu 408*91f16700Schasinglulu /* Copy SCR_EL3.NS bit to the flag to indicate caller's security */ 409*91f16700Schasinglulu bfi x7, x18, #0, #1 410*91f16700Schasinglulu 411*91f16700Schasinglulu mov sp, x12 412*91f16700Schasinglulu 413*91f16700Schasinglulu /* 414*91f16700Schasinglulu * Per SMCCC documentation, bits [23:17] must be zero for Fast 415*91f16700Schasinglulu * SMCs. Other values are reserved for future use. Ensure that 416*91f16700Schasinglulu * these bits are zeroes, if not report as unknown SMC. 417*91f16700Schasinglulu */ 418*91f16700Schasinglulu tbz x0, #FUNCID_TYPE_SHIFT, 2f /* Skip check if its a Yield Call*/ 419*91f16700Schasinglulu tst x0, #(FUNCID_FC_RESERVED_MASK << FUNCID_FC_RESERVED_SHIFT) 420*91f16700Schasinglulu b.ne smc_unknown 421*91f16700Schasinglulu 422*91f16700Schasinglulu /* 423*91f16700Schasinglulu * Per SMCCCv1.3 a caller can set the SVE hint bit in the SMC FID 424*91f16700Schasinglulu * passed through x0. Copy the SVE hint bit to flags and mask the 425*91f16700Schasinglulu * bit in smc_fid passed to the standard service dispatcher. 426*91f16700Schasinglulu * A service/dispatcher can retrieve the SVE hint bit state from 427*91f16700Schasinglulu * flags using the appropriate helper. 428*91f16700Schasinglulu */ 429*91f16700Schasinglulu2: 430*91f16700Schasinglulu and x16, x0, #(FUNCID_SVE_HINT_MASK << FUNCID_SVE_HINT_SHIFT) 431*91f16700Schasinglulu orr x7, x7, x16 432*91f16700Schasinglulu bic x0, x0, #(FUNCID_SVE_HINT_MASK << FUNCID_SVE_HINT_SHIFT) 433*91f16700Schasinglulu 434*91f16700Schasinglulu /* Get the unique owning entity number */ 435*91f16700Schasinglulu ubfx x16, x0, #FUNCID_OEN_SHIFT, #FUNCID_OEN_WIDTH 436*91f16700Schasinglulu ubfx x15, x0, #FUNCID_TYPE_SHIFT, #FUNCID_TYPE_WIDTH 437*91f16700Schasinglulu orr x16, x16, x15, lsl #FUNCID_OEN_WIDTH 438*91f16700Schasinglulu 439*91f16700Schasinglulu /* Load descriptor index from array of indices */ 440*91f16700Schasinglulu adrp x14, rt_svc_descs_indices 441*91f16700Schasinglulu add x14, x14, :lo12:rt_svc_descs_indices 442*91f16700Schasinglulu ldrb w15, [x14, x16] 443*91f16700Schasinglulu 444*91f16700Schasinglulu /* Any index greater than 127 is invalid. Check bit 7. */ 445*91f16700Schasinglulu tbnz w15, 7, smc_unknown 446*91f16700Schasinglulu 447*91f16700Schasinglulu /* 448*91f16700Schasinglulu * Get the descriptor using the index 449*91f16700Schasinglulu * x11 = (base + off), w15 = index 450*91f16700Schasinglulu * 451*91f16700Schasinglulu * handler = (base + off) + (index << log2(size)) 452*91f16700Schasinglulu */ 453*91f16700Schasinglulu adr x11, (__RT_SVC_DESCS_START__ + RT_SVC_DESC_HANDLE) 454*91f16700Schasinglulu lsl w10, w15, #RT_SVC_SIZE_LOG2 455*91f16700Schasinglulu ldr x15, [x11, w10, uxtw] 456*91f16700Schasinglulu 457*91f16700Schasinglulu /* 458*91f16700Schasinglulu * Call the Secure Monitor Call handler and then drop directly into 459*91f16700Schasinglulu * el3_exit() which will program any remaining architectural state 460*91f16700Schasinglulu * prior to issuing the ERET to the desired lower EL. 461*91f16700Schasinglulu */ 462*91f16700Schasinglulu#if DEBUG 463*91f16700Schasinglulu cbz x15, rt_svc_fw_critical_error 464*91f16700Schasinglulu#endif 465*91f16700Schasinglulu blr x15 466*91f16700Schasinglulu 467*91f16700Schasinglulu b el3_exit 468*91f16700Schasinglulu 469*91f16700Schasinglulusysreg_handler64: 470*91f16700Schasinglulu mov x0, x16 /* ESR_EL3, containing syndrome information */ 471*91f16700Schasinglulu mov x1, x6 /* lower EL's context */ 472*91f16700Schasinglulu mov x19, x6 /* save context pointer for after the call */ 473*91f16700Schasinglulu mov sp, x12 /* EL3 runtime stack, as loaded above */ 474*91f16700Schasinglulu 475*91f16700Schasinglulu /* int handle_sysreg_trap(uint64_t esr_el3, cpu_context_t *ctx); */ 476*91f16700Schasinglulu bl handle_sysreg_trap 477*91f16700Schasinglulu /* 478*91f16700Schasinglulu * returns: 479*91f16700Schasinglulu * -1: unhandled trap, panic 480*91f16700Schasinglulu * 0: handled trap, return to the trapping instruction (repeating it) 481*91f16700Schasinglulu * 1: handled trap, return to the next instruction 482*91f16700Schasinglulu */ 483*91f16700Schasinglulu 484*91f16700Schasinglulu tst w0, w0 485*91f16700Schasinglulu b.mi elx_panic /* negative return value: panic */ 486*91f16700Schasinglulu b.eq 1f /* zero: do not change ELR_EL3 */ 487*91f16700Schasinglulu 488*91f16700Schasinglulu /* advance the PC to continue after the instruction */ 489*91f16700Schasinglulu ldr x1, [x19, #CTX_EL3STATE_OFFSET + CTX_ELR_EL3] 490*91f16700Schasinglulu add x1, x1, #4 491*91f16700Schasinglulu str x1, [x19, #CTX_EL3STATE_OFFSET + CTX_ELR_EL3] 492*91f16700Schasinglulu1: 493*91f16700Schasinglulu b el3_exit 494*91f16700Schasinglulu 495*91f16700Schasinglulusmc_unknown: 496*91f16700Schasinglulu /* 497*91f16700Schasinglulu * Unknown SMC call. Populate return value with SMC_UNK and call 498*91f16700Schasinglulu * el3_exit() which will restore the remaining architectural state 499*91f16700Schasinglulu * i.e., SYS, GP and PAuth registers(if any) prior to issuing the ERET 500*91f16700Schasinglulu * to the desired lower EL. 501*91f16700Schasinglulu */ 502*91f16700Schasinglulu mov x0, #SMC_UNK 503*91f16700Schasinglulu str x0, [x6, #CTX_GPREGS_OFFSET + CTX_GPREG_X0] 504*91f16700Schasinglulu b el3_exit 505*91f16700Schasinglulu 506*91f16700Schasinglulusmc_prohibited: 507*91f16700Schasinglulu restore_ptw_el1_sys_regs 508*91f16700Schasinglulu ldp x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28] 509*91f16700Schasinglulu ldr x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR] 510*91f16700Schasinglulu mov x0, #SMC_UNK 511*91f16700Schasinglulu exception_return 512*91f16700Schasinglulu 513*91f16700Schasinglulu#if DEBUG 514*91f16700Schasinglulurt_svc_fw_critical_error: 515*91f16700Schasinglulu /* Switch to SP_ELx */ 516*91f16700Schasinglulu msr spsel, #MODE_SP_ELX 517*91f16700Schasinglulu no_ret report_unhandled_exception 518*91f16700Schasinglulu#endif 519*91f16700Schasingluluendfunc sync_exception_handler 520*91f16700Schasinglulu 521*91f16700Schasinglulu /* --------------------------------------------------------------------- 522*91f16700Schasinglulu * This function handles FIQ or IRQ interrupts i.e. EL3, S-EL1 and NS 523*91f16700Schasinglulu * interrupts. 524*91f16700Schasinglulu * 525*91f16700Schasinglulu * Note that x30 has been explicitly saved and can be used here 526*91f16700Schasinglulu * --------------------------------------------------------------------- 527*91f16700Schasinglulu */ 528*91f16700Schasinglulufunc handle_interrupt_exception 529*91f16700Schasinglulu /* 530*91f16700Schasinglulu * Save general purpose and ARMv8.3-PAuth registers (if enabled). 531*91f16700Schasinglulu * Also save PMCR_EL0 and set the PSTATE to a known state. 532*91f16700Schasinglulu */ 533*91f16700Schasinglulu bl prepare_el3_entry 534*91f16700Schasinglulu 535*91f16700Schasinglulu#if ENABLE_PAUTH 536*91f16700Schasinglulu /* Load and program APIAKey firmware key */ 537*91f16700Schasinglulu bl pauth_load_bl31_apiakey 538*91f16700Schasinglulu#endif 539*91f16700Schasinglulu 540*91f16700Schasinglulu /* Save the EL3 system registers needed to return from this exception */ 541*91f16700Schasinglulu mrs x0, spsr_el3 542*91f16700Schasinglulu mrs x1, elr_el3 543*91f16700Schasinglulu stp x0, x1, [sp, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3] 544*91f16700Schasinglulu 545*91f16700Schasinglulu /* Switch to the runtime stack i.e. SP_EL0 */ 546*91f16700Schasinglulu ldr x2, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP] 547*91f16700Schasinglulu mov x20, sp 548*91f16700Schasinglulu msr spsel, #MODE_SP_EL0 549*91f16700Schasinglulu mov sp, x2 550*91f16700Schasinglulu 551*91f16700Schasinglulu /* 552*91f16700Schasinglulu * Find out whether this is a valid interrupt type. 553*91f16700Schasinglulu * If the interrupt controller reports a spurious interrupt then return 554*91f16700Schasinglulu * to where we came from. 555*91f16700Schasinglulu */ 556*91f16700Schasinglulu bl plat_ic_get_pending_interrupt_type 557*91f16700Schasinglulu cmp x0, #INTR_TYPE_INVAL 558*91f16700Schasinglulu b.eq interrupt_exit 559*91f16700Schasinglulu 560*91f16700Schasinglulu /* 561*91f16700Schasinglulu * Get the registered handler for this interrupt type. 562*91f16700Schasinglulu * A NULL return value could be 'cause of the following conditions: 563*91f16700Schasinglulu * 564*91f16700Schasinglulu * a. An interrupt of a type was routed correctly but a handler for its 565*91f16700Schasinglulu * type was not registered. 566*91f16700Schasinglulu * 567*91f16700Schasinglulu * b. An interrupt of a type was not routed correctly so a handler for 568*91f16700Schasinglulu * its type was not registered. 569*91f16700Schasinglulu * 570*91f16700Schasinglulu * c. An interrupt of a type was routed correctly to EL3, but was 571*91f16700Schasinglulu * deasserted before its pending state could be read. Another 572*91f16700Schasinglulu * interrupt of a different type pended at the same time and its 573*91f16700Schasinglulu * type was reported as pending instead. However, a handler for this 574*91f16700Schasinglulu * type was not registered. 575*91f16700Schasinglulu * 576*91f16700Schasinglulu * a. and b. can only happen due to a programming error. The 577*91f16700Schasinglulu * occurrence of c. could be beyond the control of Trusted Firmware. 578*91f16700Schasinglulu * It makes sense to return from this exception instead of reporting an 579*91f16700Schasinglulu * error. 580*91f16700Schasinglulu */ 581*91f16700Schasinglulu bl get_interrupt_type_handler 582*91f16700Schasinglulu cbz x0, interrupt_exit 583*91f16700Schasinglulu mov x21, x0 584*91f16700Schasinglulu 585*91f16700Schasinglulu mov x0, #INTR_ID_UNAVAILABLE 586*91f16700Schasinglulu 587*91f16700Schasinglulu /* Set the current security state in the 'flags' parameter */ 588*91f16700Schasinglulu mrs x2, scr_el3 589*91f16700Schasinglulu ubfx x1, x2, #0, #1 590*91f16700Schasinglulu 591*91f16700Schasinglulu /* Restore the reference to the 'handle' i.e. SP_EL3 */ 592*91f16700Schasinglulu mov x2, x20 593*91f16700Schasinglulu 594*91f16700Schasinglulu /* x3 will point to a cookie (not used now) */ 595*91f16700Schasinglulu mov x3, xzr 596*91f16700Schasinglulu 597*91f16700Schasinglulu /* Call the interrupt type handler */ 598*91f16700Schasinglulu blr x21 599*91f16700Schasinglulu 600*91f16700Schasingluluinterrupt_exit: 601*91f16700Schasinglulu /* Return from exception, possibly in a different security state */ 602*91f16700Schasinglulu b el3_exit 603*91f16700Schasingluluendfunc handle_interrupt_exception 604*91f16700Schasinglulu 605*91f16700Schasinglulufunc imp_def_el3_handler 606*91f16700Schasinglulu /* Save GP registers */ 607*91f16700Schasinglulu stp x0, x1, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0] 608*91f16700Schasinglulu stp x2, x3, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X2] 609*91f16700Schasinglulu stp x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4] 610*91f16700Schasinglulu 611*91f16700Schasinglulu /* Get the cpu_ops pointer */ 612*91f16700Schasinglulu bl get_cpu_ops_ptr 613*91f16700Schasinglulu 614*91f16700Schasinglulu /* Get the cpu_ops exception handler */ 615*91f16700Schasinglulu ldr x0, [x0, #CPU_E_HANDLER_FUNC] 616*91f16700Schasinglulu 617*91f16700Schasinglulu /* 618*91f16700Schasinglulu * If the reserved function pointer is NULL, this CPU does not have an 619*91f16700Schasinglulu * implementation defined exception handler function 620*91f16700Schasinglulu */ 621*91f16700Schasinglulu cbz x0, el3_handler_exit 622*91f16700Schasinglulu mrs x1, esr_el3 623*91f16700Schasinglulu ubfx x1, x1, #ESR_EC_SHIFT, #ESR_EC_LENGTH 624*91f16700Schasinglulu blr x0 625*91f16700Schasingluluel3_handler_exit: 626*91f16700Schasinglulu ldp x0, x1, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0] 627*91f16700Schasinglulu ldp x2, x3, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X2] 628*91f16700Schasinglulu ldp x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4] 629*91f16700Schasinglulu restore_x30 630*91f16700Schasinglulu no_ret report_unhandled_exception 631*91f16700Schasingluluendfunc imp_def_el3_handler 632*91f16700Schasinglulu 633*91f16700Schasinglulu/* 634*91f16700Schasinglulu * Handler for async EA from lower EL synchronized at EL3 entry in KFH mode. 635*91f16700Schasinglulu * 636*91f16700Schasinglulu * This scenario may arise when there is an error (EA) in the system which is not 637*91f16700Schasinglulu * yet signaled to PE while executing in lower EL. During entry into EL3, the errors 638*91f16700Schasinglulu * are synchronized either implicitly or explicitly causing async EA to pend at EL3. 639*91f16700Schasinglulu * 640*91f16700Schasinglulu * On detecting the pending EA (via ISR_EL1.A) and if the EA routing model is 641*91f16700Schasinglulu * KFH (SCR_EL3.EA = 1) this handler reflects ther error back to lower EL. 642*91f16700Schasinglulu * 643*91f16700Schasinglulu * This function assumes x30 has been saved. 644*91f16700Schasinglulu */ 645*91f16700Schasinglulufunc reflect_pending_async_ea_to_lower_el 646*91f16700Schasinglulu /* 647*91f16700Schasinglulu * As the original exception was not handled we need to ensure that we return 648*91f16700Schasinglulu * back to the instruction which caused the exception. To acheive that, eret 649*91f16700Schasinglulu * to "elr-4" (Label "subtract_elr_el3") for SMC or simply eret otherwise 650*91f16700Schasinglulu * (Label "skip_smc_check"). 651*91f16700Schasinglulu * 652*91f16700Schasinglulu * LIMITATION: It could be that async EA is masked at the target exception level 653*91f16700Schasinglulu * or the priority of async EA wrt to the EL3/secure interrupt is lower, which 654*91f16700Schasinglulu * causes back and forth between lower EL and EL3. In case of back and forth between 655*91f16700Schasinglulu * lower EL and EL3, we can track the loop count in "CTX_NESTED_EA_FLAG" and leverage 656*91f16700Schasinglulu * previous ELR in "CTX_SAVED_ELR_EL3" to detect this cycle and further panic 657*91f16700Schasinglulu * to indicate a problem here (Label "check_loop_ctr"). If we are in this cycle, loop 658*91f16700Schasinglulu * counter retains its value but if we do a normal el3_exit this flag gets cleared. 659*91f16700Schasinglulu * However, setting SCR_EL3.IESB = 1, should give priority to SError handling 660*91f16700Schasinglulu * as per AArch64.TakeException pseudo code in Arm ARM. 661*91f16700Schasinglulu * 662*91f16700Schasinglulu * TODO: In future if EL3 gets a capability to inject a virtual SError to lower 663*91f16700Schasinglulu * ELs, we can remove the el3_panic and handle the original exception first and 664*91f16700Schasinglulu * inject SError to lower EL before ereting back. 665*91f16700Schasinglulu */ 666*91f16700Schasinglulu stp x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28] 667*91f16700Schasinglulu ldr x29, [sp, #CTX_EL3STATE_OFFSET + CTX_SAVED_ELR_EL3] 668*91f16700Schasinglulu mrs x28, elr_el3 669*91f16700Schasinglulu cmp x29, x28 670*91f16700Schasinglulu b.eq check_loop_ctr 671*91f16700Schasinglulu str x28, [sp, #CTX_EL3STATE_OFFSET + CTX_SAVED_ELR_EL3] 672*91f16700Schasinglulu /* Zero the loop counter */ 673*91f16700Schasinglulu str xzr, [sp, #CTX_EL3STATE_OFFSET + CTX_NESTED_EA_FLAG] 674*91f16700Schasinglulu b skip_loop_ctr 675*91f16700Schasinglulucheck_loop_ctr: 676*91f16700Schasinglulu ldr x29, [sp, #CTX_EL3STATE_OFFSET + CTX_NESTED_EA_FLAG] 677*91f16700Schasinglulu add x29, x29, #1 678*91f16700Schasinglulu str x29, [sp, #CTX_EL3STATE_OFFSET + CTX_NESTED_EA_FLAG] 679*91f16700Schasinglulu cmp x29, #ASYNC_EA_REPLAY_COUNTER 680*91f16700Schasinglulu b.ge el3_panic 681*91f16700Schasingluluskip_loop_ctr: 682*91f16700Schasinglulu /* 683*91f16700Schasinglulu * Logic to distinguish if we came from SMC or any other exception. 684*91f16700Schasinglulu * Use offsets in vector entry to get which exception we are handling. 685*91f16700Schasinglulu * In each vector entry of size 0x200, address "0x0-0x80" is for sync 686*91f16700Schasinglulu * exception and "0x80-0x200" is for async exceptions. 687*91f16700Schasinglulu * Use vector base address (vbar_el3) and exception offset (LR) to 688*91f16700Schasinglulu * calculate whether the address we came from is any of the following 689*91f16700Schasinglulu * "0x0-0x80", "0x200-0x280", "0x400-0x480" or "0x600-0x680" 690*91f16700Schasinglulu */ 691*91f16700Schasinglulu mrs x29, vbar_el3 692*91f16700Schasinglulu sub x30, x30, x29 693*91f16700Schasinglulu and x30, x30, #0x1ff 694*91f16700Schasinglulu cmp x30, #0x80 695*91f16700Schasinglulu b.ge skip_smc_check 696*91f16700Schasinglulu /* Its a synchronous exception, Now check if it is SMC or not? */ 697*91f16700Schasinglulu mrs x30, esr_el3 698*91f16700Schasinglulu ubfx x30, x30, #ESR_EC_SHIFT, #ESR_EC_LENGTH 699*91f16700Schasinglulu cmp x30, #EC_AARCH32_SMC 700*91f16700Schasinglulu b.eq subtract_elr_el3 701*91f16700Schasinglulu cmp x30, #EC_AARCH64_SMC 702*91f16700Schasinglulu b.eq subtract_elr_el3 703*91f16700Schasinglulu b skip_smc_check 704*91f16700Schasinglulusubtract_elr_el3: 705*91f16700Schasinglulu sub x28, x28, #4 706*91f16700Schasingluluskip_smc_check: 707*91f16700Schasinglulu msr elr_el3, x28 708*91f16700Schasinglulu ldp x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28] 709*91f16700Schasinglulu ldr x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR] 710*91f16700Schasinglulu exception_return 711*91f16700Schasingluluendfunc reflect_pending_async_ea_to_lower_el 712*91f16700Schasinglulu 713*91f16700Schasinglulu /* --------------------------------------------------------------------- 714*91f16700Schasinglulu * The following code handles exceptions caused by BRK instructions. 715*91f16700Schasinglulu * Following a BRK instruction, the only real valid cause of action is 716*91f16700Schasinglulu * to print some information and panic, as the code that caused it is 717*91f16700Schasinglulu * likely in an inconsistent internal state. 718*91f16700Schasinglulu * 719*91f16700Schasinglulu * This is initially intended to be used in conjunction with 720*91f16700Schasinglulu * __builtin_trap. 721*91f16700Schasinglulu * --------------------------------------------------------------------- 722*91f16700Schasinglulu */ 723*91f16700Schasinglulu#ifdef MONITOR_TRAPS 724*91f16700Schasinglulufunc brk_handler 725*91f16700Schasinglulu /* Extract the ISS */ 726*91f16700Schasinglulu mrs x10, esr_el3 727*91f16700Schasinglulu ubfx x10, x10, #ESR_ISS_SHIFT, #ESR_ISS_LENGTH 728*91f16700Schasinglulu 729*91f16700Schasinglulu /* Ensure the console is initialized */ 730*91f16700Schasinglulu bl plat_crash_console_init 731*91f16700Schasinglulu 732*91f16700Schasinglulu adr x4, brk_location 733*91f16700Schasinglulu bl asm_print_str 734*91f16700Schasinglulu mrs x4, elr_el3 735*91f16700Schasinglulu bl asm_print_hex 736*91f16700Schasinglulu bl asm_print_newline 737*91f16700Schasinglulu 738*91f16700Schasinglulu adr x4, brk_message 739*91f16700Schasinglulu bl asm_print_str 740*91f16700Schasinglulu mov x4, x10 741*91f16700Schasinglulu mov x5, #28 742*91f16700Schasinglulu bl asm_print_hex_bits 743*91f16700Schasinglulu bl asm_print_newline 744*91f16700Schasinglulu 745*91f16700Schasinglulu no_ret plat_panic_handler 746*91f16700Schasingluluendfunc brk_handler 747*91f16700Schasinglulu#endif /* MONITOR_TRAPS */ 748