14f6ad66aSAchin Gupta/* 297215e0fSDaniel Boulby * Copyright (c) 2013-2022, ARM Limited and Contributors. All rights reserved. 34f6ad66aSAchin Gupta * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 54f6ad66aSAchin Gupta */ 64f6ad66aSAchin Gupta 709d40e0eSAntonio Nino Diaz#include <platform_def.h> 809d40e0eSAntonio Nino Diaz 94f6ad66aSAchin Gupta#include <arch.h> 1035e98e55SDan Handley#include <asm_macros.S> 1109d40e0eSAntonio Nino Diaz#include <bl31/ea_handle.h> 1209d40e0eSAntonio Nino Diaz#include <bl31/interrupt_mgmt.h> 1309d40e0eSAntonio Nino Diaz#include <common/runtime_svc.h> 1497043ac9SDan Handley#include <context.h> 153b8456bdSManish V Badarkhe#include <el3_common_macros.S> 1609d40e0eSAntonio Nino Diaz#include <lib/el3_runtime/cpu_data.h> 1709d40e0eSAntonio Nino Diaz#include <lib/smccc.h> 184f6ad66aSAchin Gupta 194f6ad66aSAchin Gupta .globl runtime_exceptions 204f6ad66aSAchin Gupta 21f62ad322SDimitris Papastamos .globl sync_exception_sp_el0 22f62ad322SDimitris Papastamos .globl irq_sp_el0 23f62ad322SDimitris Papastamos .globl fiq_sp_el0 24f62ad322SDimitris Papastamos .globl serror_sp_el0 25f62ad322SDimitris Papastamos 26f62ad322SDimitris Papastamos .globl sync_exception_sp_elx 27f62ad322SDimitris Papastamos .globl irq_sp_elx 28f62ad322SDimitris Papastamos .globl fiq_sp_elx 29f62ad322SDimitris Papastamos .globl serror_sp_elx 30f62ad322SDimitris Papastamos 31f62ad322SDimitris Papastamos .globl sync_exception_aarch64 32f62ad322SDimitris Papastamos .globl irq_aarch64 33f62ad322SDimitris Papastamos .globl fiq_aarch64 34f62ad322SDimitris Papastamos .globl serror_aarch64 35f62ad322SDimitris Papastamos 36f62ad322SDimitris Papastamos .globl sync_exception_aarch32 37f62ad322SDimitris Papastamos .globl irq_aarch32 38f62ad322SDimitris Papastamos .globl fiq_aarch32 39f62ad322SDimitris Papastamos .globl serror_aarch32 40f62ad322SDimitris Papastamos 4176454abfSJeenu Viswambharan /* 4214c6016aSJeenu Viswambharan * Macro that prepares entry to EL3 upon taking an exception. 4314c6016aSJeenu Viswambharan * 4414c6016aSJeenu Viswambharan * With RAS_EXTENSION, this macro synchronizes pending errors with an ESB 4514c6016aSJeenu Viswambharan * instruction. When an error is thus synchronized, the handling is 4614c6016aSJeenu Viswambharan * delegated to platform EA handler. 4714c6016aSJeenu Viswambharan * 48c2d32a5fSMadhukar Pappireddy * Without RAS_EXTENSION, this macro synchronizes pending errors using 49c2d32a5fSMadhukar Pappireddy * a DSB, unmasks Asynchronous External Aborts and saves X30 before 50c2d32a5fSMadhukar Pappireddy * setting the flag CTX_IS_IN_EL3. 5114c6016aSJeenu Viswambharan */ 5214c6016aSJeenu Viswambharan .macro check_and_unmask_ea 5314c6016aSJeenu Viswambharan#if RAS_EXTENSION 5414c6016aSJeenu Viswambharan /* Synchronize pending External Aborts */ 5514c6016aSJeenu Viswambharan esb 5614c6016aSJeenu Viswambharan 5714c6016aSJeenu Viswambharan /* Unmask the SError interrupt */ 5814c6016aSJeenu Viswambharan msr daifclr, #DAIF_ABT_BIT 5914c6016aSJeenu Viswambharan 6014c6016aSJeenu Viswambharan /* 6114c6016aSJeenu Viswambharan * Explicitly save x30 so as to free up a register and to enable 6214c6016aSJeenu Viswambharan * branching 6314c6016aSJeenu Viswambharan */ 6414c6016aSJeenu Viswambharan str x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR] 6514c6016aSJeenu Viswambharan 6614c6016aSJeenu Viswambharan /* Check for SErrors synchronized by the ESB instruction */ 6714c6016aSJeenu Viswambharan mrs x30, DISR_EL1 6814c6016aSJeenu Viswambharan tbz x30, #DISR_A_BIT, 1f 6914c6016aSJeenu Viswambharan 70e290a8fcSAlexei Fedorov /* 71ed108b56SAlexei Fedorov * Save general purpose and ARMv8.3-PAuth registers (if enabled). 72ed108b56SAlexei Fedorov * If Secure Cycle Counter is not disabled in MDCR_EL3 when 73ed108b56SAlexei Fedorov * ARMv8.5-PMU is implemented, save PMCR_EL0 and disable Cycle Counter. 747d33ffe4SDaniel Boulby * Also set the PSTATE to a known state. 75e290a8fcSAlexei Fedorov */ 7697215e0fSDaniel Boulby bl prepare_el3_entry 77e290a8fcSAlexei Fedorov 78df8f3188SJeenu Viswambharan bl handle_lower_el_ea_esb 7914c6016aSJeenu Viswambharan 80ed108b56SAlexei Fedorov /* Restore general purpose, PMCR_EL0 and ARMv8.3-PAuth registers */ 81ed108b56SAlexei Fedorov bl restore_gp_pmcr_pauth_regs 8214c6016aSJeenu Viswambharan1: 8314c6016aSJeenu Viswambharan#else 84c2d32a5fSMadhukar Pappireddy /* 85c2d32a5fSMadhukar Pappireddy * For SoCs which do not implement RAS, use DSB as a barrier to 86c2d32a5fSMadhukar Pappireddy * synchronize pending external aborts. 87c2d32a5fSMadhukar Pappireddy */ 88c2d32a5fSMadhukar Pappireddy dsb sy 89c2d32a5fSMadhukar Pappireddy 9014c6016aSJeenu Viswambharan /* Unmask the SError interrupt */ 9114c6016aSJeenu Viswambharan msr daifclr, #DAIF_ABT_BIT 9214c6016aSJeenu Viswambharan 93c2d32a5fSMadhukar Pappireddy /* Use ISB for the above unmask operation to take effect immediately */ 94c2d32a5fSMadhukar Pappireddy isb 95c2d32a5fSMadhukar Pappireddy 96c2d32a5fSMadhukar Pappireddy /* 97c2d32a5fSMadhukar Pappireddy * Refer Note 1. No need to restore X30 as both handle_sync_exception 98c2d32a5fSMadhukar Pappireddy * and handle_interrupt_exception macro which follow this macro modify 99c2d32a5fSMadhukar Pappireddy * X30 anyway. 100c2d32a5fSMadhukar Pappireddy */ 10114c6016aSJeenu Viswambharan str x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR] 102c2d32a5fSMadhukar Pappireddy mov x30, #1 103c2d32a5fSMadhukar Pappireddy str x30, [sp, #CTX_EL3STATE_OFFSET + CTX_IS_IN_EL3] 104c2d32a5fSMadhukar Pappireddy dmb sy 10514c6016aSJeenu Viswambharan#endif 10614c6016aSJeenu Viswambharan .endm 10714c6016aSJeenu Viswambharan 108c2d32a5fSMadhukar Pappireddy#if !RAS_EXTENSION 109c2d32a5fSMadhukar Pappireddy /* 110c2d32a5fSMadhukar Pappireddy * Note 1: The explicit DSB at the entry of various exception vectors 111c2d32a5fSMadhukar Pappireddy * for handling exceptions from lower ELs can inadvertently trigger an 112c2d32a5fSMadhukar Pappireddy * SError exception in EL3 due to pending asynchronous aborts in lower 113c2d32a5fSMadhukar Pappireddy * ELs. This will end up being handled by serror_sp_elx which will 114c2d32a5fSMadhukar Pappireddy * ultimately panic and die. 115c2d32a5fSMadhukar Pappireddy * The way to workaround is to update a flag to indicate if the exception 116c2d32a5fSMadhukar Pappireddy * truly came from EL3. This flag is allocated in the cpu_context 117c2d32a5fSMadhukar Pappireddy * structure and located at offset "CTX_EL3STATE_OFFSET + CTX_IS_IN_EL3" 118c2d32a5fSMadhukar Pappireddy * This is not a bullet proof solution to the problem at hand because 119c2d32a5fSMadhukar Pappireddy * we assume the instructions following "isb" that help to update the 120c2d32a5fSMadhukar Pappireddy * flag execute without causing further exceptions. 121c2d32a5fSMadhukar Pappireddy */ 122c2d32a5fSMadhukar Pappireddy 123c2d32a5fSMadhukar Pappireddy /* --------------------------------------------------------------------- 124c2d32a5fSMadhukar Pappireddy * This macro handles Asynchronous External Aborts. 125c2d32a5fSMadhukar Pappireddy * --------------------------------------------------------------------- 126c2d32a5fSMadhukar Pappireddy */ 127c2d32a5fSMadhukar Pappireddy .macro handle_async_ea 128c2d32a5fSMadhukar Pappireddy /* 129c2d32a5fSMadhukar Pappireddy * Use a barrier to synchronize pending external aborts. 130c2d32a5fSMadhukar Pappireddy */ 131c2d32a5fSMadhukar Pappireddy dsb sy 132c2d32a5fSMadhukar Pappireddy 133c2d32a5fSMadhukar Pappireddy /* Unmask the SError interrupt */ 134c2d32a5fSMadhukar Pappireddy msr daifclr, #DAIF_ABT_BIT 135c2d32a5fSMadhukar Pappireddy 136c2d32a5fSMadhukar Pappireddy /* Use ISB for the above unmask operation to take effect immediately */ 137c2d32a5fSMadhukar Pappireddy isb 138c2d32a5fSMadhukar Pappireddy 139c2d32a5fSMadhukar Pappireddy /* Refer Note 1 */ 140c2d32a5fSMadhukar Pappireddy str x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR] 141c2d32a5fSMadhukar Pappireddy mov x30, #1 142c2d32a5fSMadhukar Pappireddy str x30, [sp, #CTX_EL3STATE_OFFSET + CTX_IS_IN_EL3] 143c2d32a5fSMadhukar Pappireddy dmb sy 144c2d32a5fSMadhukar Pappireddy 145c2d32a5fSMadhukar Pappireddy b handle_lower_el_async_ea 146c2d32a5fSMadhukar Pappireddy .endm 147c2d32a5fSMadhukar Pappireddy 148c2d32a5fSMadhukar Pappireddy /* 149c2d32a5fSMadhukar Pappireddy * This macro checks if the exception was taken due to SError in EL3 or 150c2d32a5fSMadhukar Pappireddy * because of pending asynchronous external aborts from lower EL that got 151c2d32a5fSMadhukar Pappireddy * triggered due to explicit synchronization in EL3. Refer Note 1. 152c2d32a5fSMadhukar Pappireddy */ 153c2d32a5fSMadhukar Pappireddy .macro check_if_serror_from_EL3 154c2d32a5fSMadhukar Pappireddy /* Assumes SP_EL3 on entry */ 155c2d32a5fSMadhukar Pappireddy str x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR] 156c2d32a5fSMadhukar Pappireddy ldr x30, [sp, #CTX_EL3STATE_OFFSET + CTX_IS_IN_EL3] 157c2d32a5fSMadhukar Pappireddy cbnz x30, exp_from_EL3 158c2d32a5fSMadhukar Pappireddy 159c2d32a5fSMadhukar Pappireddy /* Handle asynchronous external abort from lower EL */ 160c2d32a5fSMadhukar Pappireddy b handle_lower_el_async_ea 161c2d32a5fSMadhukar Pappireddy 162c2d32a5fSMadhukar Pappireddyexp_from_EL3: 163c2d32a5fSMadhukar Pappireddy /* Jump to plat_handle_el3_ea which does not return */ 164c2d32a5fSMadhukar Pappireddy .endm 165c2d32a5fSMadhukar Pappireddy#endif 166c2d32a5fSMadhukar Pappireddy 167a6ef4393SDouglas Raillard /* --------------------------------------------------------------------- 168a6ef4393SDouglas Raillard * This macro handles Synchronous exceptions. 169a6ef4393SDouglas Raillard * Only SMC exceptions are supported. 170a6ef4393SDouglas Raillard * --------------------------------------------------------------------- 171dce74b89SAchin Gupta */ 172dce74b89SAchin Gupta .macro handle_sync_exception 173872be88aSdp-arm#if ENABLE_RUNTIME_INSTRUMENTATION 174872be88aSdp-arm /* 175a6ef4393SDouglas Raillard * Read the timestamp value and store it in per-cpu data. The value 176a6ef4393SDouglas Raillard * will be extracted from per-cpu data by the C level SMC handler and 177a6ef4393SDouglas Raillard * saved to the PMF timestamp region. 178872be88aSdp-arm */ 179872be88aSdp-arm mrs x30, cntpct_el0 180872be88aSdp-arm str x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29] 181872be88aSdp-arm mrs x29, tpidr_el3 182872be88aSdp-arm str x30, [x29, #CPU_DATA_PMF_TS0_OFFSET] 183872be88aSdp-arm ldr x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29] 184872be88aSdp-arm#endif 185872be88aSdp-arm 186dce74b89SAchin Gupta mrs x30, esr_el3 187dce74b89SAchin Gupta ubfx x30, x30, #ESR_EC_SHIFT, #ESR_EC_LENGTH 188dce74b89SAchin Gupta 189a6ef4393SDouglas Raillard /* Handle SMC exceptions separately from other synchronous exceptions */ 190dce74b89SAchin Gupta cmp x30, #EC_AARCH32_SMC 191dce74b89SAchin Gupta b.eq smc_handler32 192dce74b89SAchin Gupta 193dce74b89SAchin Gupta cmp x30, #EC_AARCH64_SMC 194dce74b89SAchin Gupta b.eq smc_handler64 195dce74b89SAchin Gupta 196df8f3188SJeenu Viswambharan /* Synchronous exceptions other than the above are assumed to be EA */ 1974d91838bSJulius Werner ldr x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR] 198df8f3188SJeenu Viswambharan b enter_lower_el_sync_ea 199dce74b89SAchin Gupta .endm 200dce74b89SAchin Gupta 201dce74b89SAchin Gupta 202a6ef4393SDouglas Raillard /* --------------------------------------------------------------------- 203a6ef4393SDouglas Raillard * This macro handles FIQ or IRQ interrupts i.e. EL3, S-EL1 and NS 204a6ef4393SDouglas Raillard * interrupts. 205a6ef4393SDouglas Raillard * --------------------------------------------------------------------- 206dce74b89SAchin Gupta */ 207dce74b89SAchin Gupta .macro handle_interrupt_exception label 2085283962eSAntonio Nino Diaz 209e290a8fcSAlexei Fedorov /* 210ed108b56SAlexei Fedorov * Save general purpose and ARMv8.3-PAuth registers (if enabled). 211ed108b56SAlexei Fedorov * If Secure Cycle Counter is not disabled in MDCR_EL3 when 212ed108b56SAlexei Fedorov * ARMv8.5-PMU is implemented, save PMCR_EL0 and disable Cycle Counter. 2137d33ffe4SDaniel Boulby * Also set the PSTATE to a known state. 214e290a8fcSAlexei Fedorov */ 21597215e0fSDaniel Boulby bl prepare_el3_entry 216e290a8fcSAlexei Fedorov 217b86048c4SAntonio Nino Diaz#if ENABLE_PAUTH 218ed108b56SAlexei Fedorov /* Load and program APIAKey firmware key */ 219ed108b56SAlexei Fedorov bl pauth_load_bl31_apiakey 220b86048c4SAntonio Nino Diaz#endif 2215283962eSAntonio Nino Diaz 222a6ef4393SDouglas Raillard /* Save the EL3 system registers needed to return from this exception */ 2235717aae1SAchin Gupta mrs x0, spsr_el3 2245717aae1SAchin Gupta mrs x1, elr_el3 2255717aae1SAchin Gupta stp x0, x1, [sp, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3] 2265717aae1SAchin Gupta 227dce74b89SAchin Gupta /* Switch to the runtime stack i.e. SP_EL0 */ 228dce74b89SAchin Gupta ldr x2, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP] 229dce74b89SAchin Gupta mov x20, sp 230ed108b56SAlexei Fedorov msr spsel, #MODE_SP_EL0 231dce74b89SAchin Gupta mov sp, x2 232dce74b89SAchin Gupta 233dce74b89SAchin Gupta /* 234a6ef4393SDouglas Raillard * Find out whether this is a valid interrupt type. 235a6ef4393SDouglas Raillard * If the interrupt controller reports a spurious interrupt then return 236a6ef4393SDouglas Raillard * to where we came from. 237dce74b89SAchin Gupta */ 2389865ac15SDan Handley bl plat_ic_get_pending_interrupt_type 239dce74b89SAchin Gupta cmp x0, #INTR_TYPE_INVAL 240dce74b89SAchin Gupta b.eq interrupt_exit_\label 241dce74b89SAchin Gupta 242dce74b89SAchin Gupta /* 243a6ef4393SDouglas Raillard * Get the registered handler for this interrupt type. 244a6ef4393SDouglas Raillard * A NULL return value could be 'cause of the following conditions: 2455717aae1SAchin Gupta * 246a6ef4393SDouglas Raillard * a. An interrupt of a type was routed correctly but a handler for its 247a6ef4393SDouglas Raillard * type was not registered. 2485717aae1SAchin Gupta * 249a6ef4393SDouglas Raillard * b. An interrupt of a type was not routed correctly so a handler for 250a6ef4393SDouglas Raillard * its type was not registered. 2515717aae1SAchin Gupta * 252a6ef4393SDouglas Raillard * c. An interrupt of a type was routed correctly to EL3, but was 253a6ef4393SDouglas Raillard * deasserted before its pending state could be read. Another 254a6ef4393SDouglas Raillard * interrupt of a different type pended at the same time and its 255a6ef4393SDouglas Raillard * type was reported as pending instead. However, a handler for this 256a6ef4393SDouglas Raillard * type was not registered. 2575717aae1SAchin Gupta * 258a6ef4393SDouglas Raillard * a. and b. can only happen due to a programming error. The 259a6ef4393SDouglas Raillard * occurrence of c. could be beyond the control of Trusted Firmware. 260a6ef4393SDouglas Raillard * It makes sense to return from this exception instead of reporting an 261a6ef4393SDouglas Raillard * error. 262dce74b89SAchin Gupta */ 263dce74b89SAchin Gupta bl get_interrupt_type_handler 2645717aae1SAchin Gupta cbz x0, interrupt_exit_\label 265dce74b89SAchin Gupta mov x21, x0 266dce74b89SAchin Gupta 267dce74b89SAchin Gupta mov x0, #INTR_ID_UNAVAILABLE 268dce74b89SAchin Gupta 269dce74b89SAchin Gupta /* Set the current security state in the 'flags' parameter */ 270dce74b89SAchin Gupta mrs x2, scr_el3 271dce74b89SAchin Gupta ubfx x1, x2, #0, #1 272dce74b89SAchin Gupta 273dce74b89SAchin Gupta /* Restore the reference to the 'handle' i.e. SP_EL3 */ 274dce74b89SAchin Gupta mov x2, x20 275dce74b89SAchin Gupta 276b460b8bfSSoby Mathew /* x3 will point to a cookie (not used now) */ 277b460b8bfSSoby Mathew mov x3, xzr 278b460b8bfSSoby Mathew 279dce74b89SAchin Gupta /* Call the interrupt type handler */ 280dce74b89SAchin Gupta blr x21 281dce74b89SAchin Gupta 282dce74b89SAchin Guptainterrupt_exit_\label: 283dce74b89SAchin Gupta /* Return from exception, possibly in a different security state */ 284dce74b89SAchin Gupta b el3_exit 285dce74b89SAchin Gupta 286dce74b89SAchin Gupta .endm 287dce74b89SAchin Gupta 288dce74b89SAchin Gupta 289e0ae9fabSSandrine Bailleuxvector_base runtime_exceptions 290e0ae9fabSSandrine Bailleux 291a6ef4393SDouglas Raillard /* --------------------------------------------------------------------- 292a6ef4393SDouglas Raillard * Current EL with SP_EL0 : 0x0 - 0x200 293a6ef4393SDouglas Raillard * --------------------------------------------------------------------- 2944f6ad66aSAchin Gupta */ 295e0ae9fabSSandrine Bailleuxvector_entry sync_exception_sp_el0 2961f461979SJustin Chadwell#ifdef MONITOR_TRAPS 2971f461979SJustin Chadwell stp x29, x30, [sp, #-16]! 2981f461979SJustin Chadwell 2991f461979SJustin Chadwell mrs x30, esr_el3 3001f461979SJustin Chadwell ubfx x30, x30, #ESR_EC_SHIFT, #ESR_EC_LENGTH 3011f461979SJustin Chadwell 3021f461979SJustin Chadwell /* Check for BRK */ 3031f461979SJustin Chadwell cmp x30, #EC_BRK 3041f461979SJustin Chadwell b.eq brk_handler 3051f461979SJustin Chadwell 3061f461979SJustin Chadwell ldp x29, x30, [sp], #16 3071f461979SJustin Chadwell#endif /* MONITOR_TRAPS */ 3081f461979SJustin Chadwell 309a6ef4393SDouglas Raillard /* We don't expect any synchronous exceptions from EL3 */ 3104d91838bSJulius Werner b report_unhandled_exception 311a9203edaSRoberto Vargasend_vector_entry sync_exception_sp_el0 3124f6ad66aSAchin Gupta 313e0ae9fabSSandrine Bailleuxvector_entry irq_sp_el0 314a6ef4393SDouglas Raillard /* 315a6ef4393SDouglas Raillard * EL3 code is non-reentrant. Any asynchronous exception is a serious 316a6ef4393SDouglas Raillard * error. Loop infinitely. 317a6ef4393SDouglas Raillard */ 3184d91838bSJulius Werner b report_unhandled_interrupt 319a9203edaSRoberto Vargasend_vector_entry irq_sp_el0 3204f6ad66aSAchin Gupta 321e0ae9fabSSandrine Bailleux 322e0ae9fabSSandrine Bailleuxvector_entry fiq_sp_el0 3234d91838bSJulius Werner b report_unhandled_interrupt 324a9203edaSRoberto Vargasend_vector_entry fiq_sp_el0 3254f6ad66aSAchin Gupta 326e0ae9fabSSandrine Bailleux 327e0ae9fabSSandrine Bailleuxvector_entry serror_sp_el0 328eaeaa4d0SJeenu Viswambharan no_ret plat_handle_el3_ea 329a9203edaSRoberto Vargasend_vector_entry serror_sp_el0 3304f6ad66aSAchin Gupta 331a6ef4393SDouglas Raillard /* --------------------------------------------------------------------- 332a6ef4393SDouglas Raillard * Current EL with SP_ELx: 0x200 - 0x400 333a6ef4393SDouglas Raillard * --------------------------------------------------------------------- 3344f6ad66aSAchin Gupta */ 335e0ae9fabSSandrine Bailleuxvector_entry sync_exception_sp_elx 336a6ef4393SDouglas Raillard /* 337a6ef4393SDouglas Raillard * This exception will trigger if anything went wrong during a previous 338a6ef4393SDouglas Raillard * exception entry or exit or while handling an earlier unexpected 339a6ef4393SDouglas Raillard * synchronous exception. There is a high probability that SP_EL3 is 340a6ef4393SDouglas Raillard * corrupted. 341caa84939SJeenu Viswambharan */ 3424d91838bSJulius Werner b report_unhandled_exception 343a9203edaSRoberto Vargasend_vector_entry sync_exception_sp_elx 3444f6ad66aSAchin Gupta 345e0ae9fabSSandrine Bailleuxvector_entry irq_sp_elx 3464d91838bSJulius Werner b report_unhandled_interrupt 347a9203edaSRoberto Vargasend_vector_entry irq_sp_elx 348a7934d69SJeenu Viswambharan 349e0ae9fabSSandrine Bailleuxvector_entry fiq_sp_elx 3504d91838bSJulius Werner b report_unhandled_interrupt 351a9203edaSRoberto Vargasend_vector_entry fiq_sp_elx 352a7934d69SJeenu Viswambharan 353e0ae9fabSSandrine Bailleuxvector_entry serror_sp_elx 354c2d32a5fSMadhukar Pappireddy#if !RAS_EXTENSION 355c2d32a5fSMadhukar Pappireddy check_if_serror_from_EL3 356c2d32a5fSMadhukar Pappireddy#endif 357eaeaa4d0SJeenu Viswambharan no_ret plat_handle_el3_ea 358a9203edaSRoberto Vargasend_vector_entry serror_sp_elx 3594f6ad66aSAchin Gupta 360a6ef4393SDouglas Raillard /* --------------------------------------------------------------------- 36144804252SSandrine Bailleux * Lower EL using AArch64 : 0x400 - 0x600 362a6ef4393SDouglas Raillard * --------------------------------------------------------------------- 3634f6ad66aSAchin Gupta */ 364e0ae9fabSSandrine Bailleuxvector_entry sync_exception_aarch64 365a6ef4393SDouglas Raillard /* 366a6ef4393SDouglas Raillard * This exception vector will be the entry point for SMCs and traps 367a6ef4393SDouglas Raillard * that are unhandled at lower ELs most commonly. SP_EL3 should point 368a6ef4393SDouglas Raillard * to a valid cpu context where the general purpose and system register 369a6ef4393SDouglas Raillard * state can be saved. 370caa84939SJeenu Viswambharan */ 3713b8456bdSManish V Badarkhe apply_at_speculative_wa 37214c6016aSJeenu Viswambharan check_and_unmask_ea 373caa84939SJeenu Viswambharan handle_sync_exception 374a9203edaSRoberto Vargasend_vector_entry sync_exception_aarch64 3754f6ad66aSAchin Gupta 376e0ae9fabSSandrine Bailleuxvector_entry irq_aarch64 3773b8456bdSManish V Badarkhe apply_at_speculative_wa 37814c6016aSJeenu Viswambharan check_and_unmask_ea 379dce74b89SAchin Gupta handle_interrupt_exception irq_aarch64 380a9203edaSRoberto Vargasend_vector_entry irq_aarch64 3814f6ad66aSAchin Gupta 382e0ae9fabSSandrine Bailleuxvector_entry fiq_aarch64 3833b8456bdSManish V Badarkhe apply_at_speculative_wa 38414c6016aSJeenu Viswambharan check_and_unmask_ea 385dce74b89SAchin Gupta handle_interrupt_exception fiq_aarch64 386a9203edaSRoberto Vargasend_vector_entry fiq_aarch64 3874f6ad66aSAchin Gupta 388e0ae9fabSSandrine Bailleuxvector_entry serror_aarch64 3893b8456bdSManish V Badarkhe apply_at_speculative_wa 390c2d32a5fSMadhukar Pappireddy#if RAS_EXTENSION 39176454abfSJeenu Viswambharan msr daifclr, #DAIF_ABT_BIT 392df8f3188SJeenu Viswambharan b enter_lower_el_async_ea 393c2d32a5fSMadhukar Pappireddy#else 394c2d32a5fSMadhukar Pappireddy handle_async_ea 395c2d32a5fSMadhukar Pappireddy#endif 396a9203edaSRoberto Vargasend_vector_entry serror_aarch64 3974f6ad66aSAchin Gupta 398a6ef4393SDouglas Raillard /* --------------------------------------------------------------------- 39944804252SSandrine Bailleux * Lower EL using AArch32 : 0x600 - 0x800 400a6ef4393SDouglas Raillard * --------------------------------------------------------------------- 4014f6ad66aSAchin Gupta */ 402e0ae9fabSSandrine Bailleuxvector_entry sync_exception_aarch32 403a6ef4393SDouglas Raillard /* 404a6ef4393SDouglas Raillard * This exception vector will be the entry point for SMCs and traps 405a6ef4393SDouglas Raillard * that are unhandled at lower ELs most commonly. SP_EL3 should point 406a6ef4393SDouglas Raillard * to a valid cpu context where the general purpose and system register 407a6ef4393SDouglas Raillard * state can be saved. 408caa84939SJeenu Viswambharan */ 4093b8456bdSManish V Badarkhe apply_at_speculative_wa 41014c6016aSJeenu Viswambharan check_and_unmask_ea 411caa84939SJeenu Viswambharan handle_sync_exception 412a9203edaSRoberto Vargasend_vector_entry sync_exception_aarch32 4134f6ad66aSAchin Gupta 414e0ae9fabSSandrine Bailleuxvector_entry irq_aarch32 4153b8456bdSManish V Badarkhe apply_at_speculative_wa 41614c6016aSJeenu Viswambharan check_and_unmask_ea 417dce74b89SAchin Gupta handle_interrupt_exception irq_aarch32 418a9203edaSRoberto Vargasend_vector_entry irq_aarch32 4194f6ad66aSAchin Gupta 420e0ae9fabSSandrine Bailleuxvector_entry fiq_aarch32 4213b8456bdSManish V Badarkhe apply_at_speculative_wa 42214c6016aSJeenu Viswambharan check_and_unmask_ea 423dce74b89SAchin Gupta handle_interrupt_exception fiq_aarch32 424a9203edaSRoberto Vargasend_vector_entry fiq_aarch32 4254f6ad66aSAchin Gupta 426e0ae9fabSSandrine Bailleuxvector_entry serror_aarch32 4273b8456bdSManish V Badarkhe apply_at_speculative_wa 428c2d32a5fSMadhukar Pappireddy#if RAS_EXTENSION 42976454abfSJeenu Viswambharan msr daifclr, #DAIF_ABT_BIT 430df8f3188SJeenu Viswambharan b enter_lower_el_async_ea 431c2d32a5fSMadhukar Pappireddy#else 432c2d32a5fSMadhukar Pappireddy handle_async_ea 433c2d32a5fSMadhukar Pappireddy#endif 434a9203edaSRoberto Vargasend_vector_entry serror_aarch32 435a7934d69SJeenu Viswambharan 4361f461979SJustin Chadwell#ifdef MONITOR_TRAPS 4371f461979SJustin Chadwell .section .rodata.brk_string, "aS" 4381f461979SJustin Chadwellbrk_location: 4391f461979SJustin Chadwell .asciz "Error at instruction 0x" 4401f461979SJustin Chadwellbrk_message: 4411f461979SJustin Chadwell .asciz "Unexpected BRK instruction with value 0x" 4421f461979SJustin Chadwell#endif /* MONITOR_TRAPS */ 4431f461979SJustin Chadwell 4442f370465SAntonio Nino Diaz /* --------------------------------------------------------------------- 445caa84939SJeenu Viswambharan * The following code handles secure monitor calls. 446a6ef4393SDouglas Raillard * Depending upon the execution state from where the SMC has been 447a6ef4393SDouglas Raillard * invoked, it frees some general purpose registers to perform the 448a6ef4393SDouglas Raillard * remaining tasks. They involve finding the runtime service handler 449a6ef4393SDouglas Raillard * that is the target of the SMC & switching to runtime stacks (SP_EL0) 450a6ef4393SDouglas Raillard * before calling the handler. 451caa84939SJeenu Viswambharan * 452a6ef4393SDouglas Raillard * Note that x30 has been explicitly saved and can be used here 453a6ef4393SDouglas Raillard * --------------------------------------------------------------------- 454caa84939SJeenu Viswambharan */ 4550a30cf54SAndrew Thoelkefunc smc_handler 456caa84939SJeenu Viswambharansmc_handler32: 457caa84939SJeenu Viswambharan /* Check whether aarch32 issued an SMC64 */ 458caa84939SJeenu Viswambharan tbnz x0, #FUNCID_CC_SHIFT, smc_prohibited 459caa84939SJeenu Viswambharan 460caa84939SJeenu Viswambharansmc_handler64: 4615283962eSAntonio Nino Diaz /* NOTE: The code below must preserve x0-x4 */ 4625283962eSAntonio Nino Diaz 463e290a8fcSAlexei Fedorov /* 464ed108b56SAlexei Fedorov * Save general purpose and ARMv8.3-PAuth registers (if enabled). 465ed108b56SAlexei Fedorov * If Secure Cycle Counter is not disabled in MDCR_EL3 when 466ed108b56SAlexei Fedorov * ARMv8.5-PMU is implemented, save PMCR_EL0 and disable Cycle Counter. 4677d33ffe4SDaniel Boulby * Also set the PSTATE to a known state. 468e290a8fcSAlexei Fedorov */ 46997215e0fSDaniel Boulby bl prepare_el3_entry 470e290a8fcSAlexei Fedorov 471b86048c4SAntonio Nino Diaz#if ENABLE_PAUTH 472ed108b56SAlexei Fedorov /* Load and program APIAKey firmware key */ 473ed108b56SAlexei Fedorov bl pauth_load_bl31_apiakey 474b86048c4SAntonio Nino Diaz#endif 4755283962eSAntonio Nino Diaz 476a6ef4393SDouglas Raillard /* 477a6ef4393SDouglas Raillard * Populate the parameters for the SMC handler. 478a6ef4393SDouglas Raillard * We already have x0-x4 in place. x5 will point to a cookie (not used 479a6ef4393SDouglas Raillard * now). x6 will point to the context structure (SP_EL3) and x7 will 480201ca5b6SDimitris Papastamos * contain flags we need to pass to the handler. 481caa84939SJeenu Viswambharan */ 482caa84939SJeenu Viswambharan mov x5, xzr 483caa84939SJeenu Viswambharan mov x6, sp 484caa84939SJeenu Viswambharan 485a6ef4393SDouglas Raillard /* 486a6ef4393SDouglas Raillard * Restore the saved C runtime stack value which will become the new 487a6ef4393SDouglas Raillard * SP_EL0 i.e. EL3 runtime stack. It was saved in the 'cpu_context' 488a6ef4393SDouglas Raillard * structure prior to the last ERET from EL3. 489caa84939SJeenu Viswambharan */ 490caa84939SJeenu Viswambharan ldr x12, [x6, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP] 491caa84939SJeenu Viswambharan 492caa84939SJeenu Viswambharan /* Switch to SP_EL0 */ 493ed108b56SAlexei Fedorov msr spsel, #MODE_SP_EL0 494caa84939SJeenu Viswambharan 495a6ef4393SDouglas Raillard /* 496a6ef4393SDouglas Raillard * Save the SPSR_EL3, ELR_EL3, & SCR_EL3 in case there is a world 497a6ef4393SDouglas Raillard * switch during SMC handling. 498a6ef4393SDouglas Raillard * TODO: Revisit if all system registers can be saved later. 499caa84939SJeenu Viswambharan */ 500caa84939SJeenu Viswambharan mrs x16, spsr_el3 501caa84939SJeenu Viswambharan mrs x17, elr_el3 502caa84939SJeenu Viswambharan mrs x18, scr_el3 503caa84939SJeenu Viswambharan stp x16, x17, [x6, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3] 504b51da821SAchin Gupta str x18, [x6, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3] 505caa84939SJeenu Viswambharan 5064693ff72SZelalem Aweke /* Clear flag register */ 5074693ff72SZelalem Aweke mov x7, xzr 5084693ff72SZelalem Aweke 5094693ff72SZelalem Aweke#if ENABLE_RME 5104693ff72SZelalem Aweke /* Copy SCR_EL3.NSE bit to the flag to indicate caller's security */ 5114693ff72SZelalem Aweke ubfx x7, x18, #SCR_NSE_SHIFT, 1 5124693ff72SZelalem Aweke 5134693ff72SZelalem Aweke /* 5144693ff72SZelalem Aweke * Shift copied SCR_EL3.NSE bit by 5 to create space for 515*0fe7b9f2SOlivier Deprez * SCR_EL3.NS bit. Bit 5 of the flag corresponds to 5164693ff72SZelalem Aweke * the SCR_EL3.NSE bit. 5174693ff72SZelalem Aweke */ 5184693ff72SZelalem Aweke lsl x7, x7, #5 5194693ff72SZelalem Aweke#endif /* ENABLE_RME */ 5204693ff72SZelalem Aweke 521caa84939SJeenu Viswambharan /* Copy SCR_EL3.NS bit to the flag to indicate caller's security */ 522caa84939SJeenu Viswambharan bfi x7, x18, #0, #1 523caa84939SJeenu Viswambharan 524*0fe7b9f2SOlivier Deprez /* 525*0fe7b9f2SOlivier Deprez * Per SMCCCv1.3 a caller can set the SVE hint bit in the SMC FID 526*0fe7b9f2SOlivier Deprez * passed through x0. Copy the SVE hint bit to flags and mask the 527*0fe7b9f2SOlivier Deprez * bit in smc_fid passed to the standard service dispatcher. 528*0fe7b9f2SOlivier Deprez * A service/dispatcher can retrieve the SVE hint bit state from 529*0fe7b9f2SOlivier Deprez * flags using the appropriate helper. 530*0fe7b9f2SOlivier Deprez */ 531*0fe7b9f2SOlivier Deprez bfi x7, x0, #FUNCID_SVE_HINT_SHIFT, #FUNCID_SVE_HINT_MASK 532*0fe7b9f2SOlivier Deprez bic x0, x0, #(FUNCID_SVE_HINT_MASK << FUNCID_SVE_HINT_SHIFT) 533*0fe7b9f2SOlivier Deprez 534caa84939SJeenu Viswambharan mov sp, x12 535caa84939SJeenu Viswambharan 536cc485e27SMadhukar Pappireddy /* Get the unique owning entity number */ 537cc485e27SMadhukar Pappireddy ubfx x16, x0, #FUNCID_OEN_SHIFT, #FUNCID_OEN_WIDTH 538cc485e27SMadhukar Pappireddy ubfx x15, x0, #FUNCID_TYPE_SHIFT, #FUNCID_TYPE_WIDTH 539cc485e27SMadhukar Pappireddy orr x16, x16, x15, lsl #FUNCID_OEN_WIDTH 540cc485e27SMadhukar Pappireddy 541cc485e27SMadhukar Pappireddy /* Load descriptor index from array of indices */ 542c367b75eSMadhukar Pappireddy adrp x14, rt_svc_descs_indices 543c367b75eSMadhukar Pappireddy add x14, x14, :lo12:rt_svc_descs_indices 544cc485e27SMadhukar Pappireddy ldrb w15, [x14, x16] 545cc485e27SMadhukar Pappireddy 546cc485e27SMadhukar Pappireddy /* Any index greater than 127 is invalid. Check bit 7. */ 547cc485e27SMadhukar Pappireddy tbnz w15, 7, smc_unknown 548cc485e27SMadhukar Pappireddy 549cc485e27SMadhukar Pappireddy /* 550cc485e27SMadhukar Pappireddy * Get the descriptor using the index 551cc485e27SMadhukar Pappireddy * x11 = (base + off), w15 = index 552cc485e27SMadhukar Pappireddy * 553cc485e27SMadhukar Pappireddy * handler = (base + off) + (index << log2(size)) 554cc485e27SMadhukar Pappireddy */ 555cc485e27SMadhukar Pappireddy adr x11, (__RT_SVC_DESCS_START__ + RT_SVC_DESC_HANDLE) 556cc485e27SMadhukar Pappireddy lsl w10, w15, #RT_SVC_SIZE_LOG2 557cc485e27SMadhukar Pappireddy ldr x15, [x11, w10, uxtw] 558cc485e27SMadhukar Pappireddy 559a6ef4393SDouglas Raillard /* 560a6ef4393SDouglas Raillard * Call the Secure Monitor Call handler and then drop directly into 561a6ef4393SDouglas Raillard * el3_exit() which will program any remaining architectural state 562a6ef4393SDouglas Raillard * prior to issuing the ERET to the desired lower EL. 563caa84939SJeenu Viswambharan */ 564caa84939SJeenu Viswambharan#if DEBUG 565caa84939SJeenu Viswambharan cbz x15, rt_svc_fw_critical_error 566caa84939SJeenu Viswambharan#endif 567caa84939SJeenu Viswambharan blr x15 568caa84939SJeenu Viswambharan 569bbf8f6f9SYatharth Kochar b el3_exit 5704f6ad66aSAchin Gupta 571caa84939SJeenu Viswambharansmc_unknown: 572caa84939SJeenu Viswambharan /* 573cc485e27SMadhukar Pappireddy * Unknown SMC call. Populate return value with SMC_UNK and call 574cc485e27SMadhukar Pappireddy * el3_exit() which will restore the remaining architectural state 575cc485e27SMadhukar Pappireddy * i.e., SYS, GP and PAuth registers(if any) prior to issuing the ERET 576cc485e27SMadhukar Pappireddy * to the desired lower EL. 577caa84939SJeenu Viswambharan */ 5784abd7fa7SAntonio Nino Diaz mov x0, #SMC_UNK 579cc485e27SMadhukar Pappireddy str x0, [x6, #CTX_GPREGS_OFFSET + CTX_GPREG_X0] 580cc485e27SMadhukar Pappireddy b el3_exit 581caa84939SJeenu Viswambharan 582caa84939SJeenu Viswambharansmc_prohibited: 5833b8456bdSManish V Badarkhe restore_ptw_el1_sys_regs 5843b8456bdSManish V Badarkhe ldp x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28] 585c3260f9bSSoby Mathew ldr x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR] 5864abd7fa7SAntonio Nino Diaz mov x0, #SMC_UNK 587f461fe34SAnthony Steinhauser exception_return 588caa84939SJeenu Viswambharan 589ed108b56SAlexei Fedorov#if DEBUG 590caa84939SJeenu Viswambharanrt_svc_fw_critical_error: 591a6ef4393SDouglas Raillard /* Switch to SP_ELx */ 592ed108b56SAlexei Fedorov msr spsel, #MODE_SP_ELX 593a806dad5SJeenu Viswambharan no_ret report_unhandled_exception 594ed108b56SAlexei Fedorov#endif 5958b779620SKévin Petitendfunc smc_handler 5961f461979SJustin Chadwell 5971f461979SJustin Chadwell /* --------------------------------------------------------------------- 5981f461979SJustin Chadwell * The following code handles exceptions caused by BRK instructions. 5991f461979SJustin Chadwell * Following a BRK instruction, the only real valid cause of action is 6001f461979SJustin Chadwell * to print some information and panic, as the code that caused it is 6011f461979SJustin Chadwell * likely in an inconsistent internal state. 6021f461979SJustin Chadwell * 6031f461979SJustin Chadwell * This is initially intended to be used in conjunction with 6041f461979SJustin Chadwell * __builtin_trap. 6051f461979SJustin Chadwell * --------------------------------------------------------------------- 6061f461979SJustin Chadwell */ 6071f461979SJustin Chadwell#ifdef MONITOR_TRAPS 6081f461979SJustin Chadwellfunc brk_handler 6091f461979SJustin Chadwell /* Extract the ISS */ 6101f461979SJustin Chadwell mrs x10, esr_el3 6111f461979SJustin Chadwell ubfx x10, x10, #ESR_ISS_SHIFT, #ESR_ISS_LENGTH 6121f461979SJustin Chadwell 6131f461979SJustin Chadwell /* Ensure the console is initialized */ 6141f461979SJustin Chadwell bl plat_crash_console_init 6151f461979SJustin Chadwell 6161f461979SJustin Chadwell adr x4, brk_location 6171f461979SJustin Chadwell bl asm_print_str 6181f461979SJustin Chadwell mrs x4, elr_el3 6191f461979SJustin Chadwell bl asm_print_hex 6201f461979SJustin Chadwell bl asm_print_newline 6211f461979SJustin Chadwell 6221f461979SJustin Chadwell adr x4, brk_message 6231f461979SJustin Chadwell bl asm_print_str 6241f461979SJustin Chadwell mov x4, x10 6251f461979SJustin Chadwell mov x5, #28 6261f461979SJustin Chadwell bl asm_print_hex_bits 6271f461979SJustin Chadwell bl asm_print_newline 6281f461979SJustin Chadwell 6291f461979SJustin Chadwell no_ret plat_panic_handler 6301f461979SJustin Chadwellendfunc brk_handler 6311f461979SJustin Chadwell#endif /* MONITOR_TRAPS */ 632