14f6ad66aSAchin Gupta/* 2*17d07a55SGovindraj Raja * Copyright (c) 2013-2023, 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> 13ccd81f1eSAndre Przywara#include <bl31/sync_handle.h> 1409d40e0eSAntonio Nino Diaz#include <common/runtime_svc.h> 1597043ac9SDan Handley#include <context.h> 163b8456bdSManish V Badarkhe#include <el3_common_macros.S> 1709d40e0eSAntonio Nino Diaz#include <lib/el3_runtime/cpu_data.h> 1809d40e0eSAntonio Nino Diaz#include <lib/smccc.h> 194f6ad66aSAchin Gupta 204f6ad66aSAchin Gupta .globl runtime_exceptions 214f6ad66aSAchin Gupta 22f62ad322SDimitris Papastamos .globl sync_exception_sp_el0 23f62ad322SDimitris Papastamos .globl irq_sp_el0 24f62ad322SDimitris Papastamos .globl fiq_sp_el0 25f62ad322SDimitris Papastamos .globl serror_sp_el0 26f62ad322SDimitris Papastamos 27f62ad322SDimitris Papastamos .globl sync_exception_sp_elx 28f62ad322SDimitris Papastamos .globl irq_sp_elx 29f62ad322SDimitris Papastamos .globl fiq_sp_elx 30f62ad322SDimitris Papastamos .globl serror_sp_elx 31f62ad322SDimitris Papastamos 32f62ad322SDimitris Papastamos .globl sync_exception_aarch64 33f62ad322SDimitris Papastamos .globl irq_aarch64 34f62ad322SDimitris Papastamos .globl fiq_aarch64 35f62ad322SDimitris Papastamos .globl serror_aarch64 36f62ad322SDimitris Papastamos 37f62ad322SDimitris Papastamos .globl sync_exception_aarch32 38f62ad322SDimitris Papastamos .globl irq_aarch32 39f62ad322SDimitris Papastamos .globl fiq_aarch32 40f62ad322SDimitris Papastamos .globl serror_aarch32 41f62ad322SDimitris Papastamos 4276454abfSJeenu Viswambharan /* 4314c6016aSJeenu Viswambharan * Macro that prepares entry to EL3 upon taking an exception. 4414c6016aSJeenu Viswambharan * 4514c6016aSJeenu Viswambharan * With RAS_EXTENSION, this macro synchronizes pending errors with an ESB 4614c6016aSJeenu Viswambharan * instruction. When an error is thus synchronized, the handling is 4714c6016aSJeenu Viswambharan * delegated to platform EA handler. 4814c6016aSJeenu Viswambharan * 49c2d32a5fSMadhukar Pappireddy * Without RAS_EXTENSION, this macro synchronizes pending errors using 50c2d32a5fSMadhukar Pappireddy * a DSB, unmasks Asynchronous External Aborts and saves X30 before 51c2d32a5fSMadhukar Pappireddy * setting the flag CTX_IS_IN_EL3. 5214c6016aSJeenu Viswambharan */ 5314c6016aSJeenu Viswambharan .macro check_and_unmask_ea 5414c6016aSJeenu Viswambharan#if RAS_EXTENSION 5514c6016aSJeenu Viswambharan /* Synchronize pending External Aborts */ 5614c6016aSJeenu Viswambharan esb 5714c6016aSJeenu Viswambharan 5814c6016aSJeenu Viswambharan /* Unmask the SError interrupt */ 5914c6016aSJeenu Viswambharan msr daifclr, #DAIF_ABT_BIT 6014c6016aSJeenu Viswambharan 6114c6016aSJeenu Viswambharan /* 6214c6016aSJeenu Viswambharan * Explicitly save x30 so as to free up a register and to enable 6314c6016aSJeenu Viswambharan * branching 6414c6016aSJeenu Viswambharan */ 6514c6016aSJeenu Viswambharan str x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR] 6614c6016aSJeenu Viswambharan 6714c6016aSJeenu Viswambharan /* Check for SErrors synchronized by the ESB instruction */ 6814c6016aSJeenu Viswambharan mrs x30, DISR_EL1 6914c6016aSJeenu Viswambharan tbz x30, #DISR_A_BIT, 1f 7014c6016aSJeenu Viswambharan 71e290a8fcSAlexei Fedorov /* 72ed108b56SAlexei Fedorov * Save general purpose and ARMv8.3-PAuth registers (if enabled). 73ed108b56SAlexei Fedorov * If Secure Cycle Counter is not disabled in MDCR_EL3 when 74ed108b56SAlexei Fedorov * ARMv8.5-PMU is implemented, save PMCR_EL0 and disable Cycle Counter. 757d33ffe4SDaniel Boulby * Also set the PSTATE to a known state. 76e290a8fcSAlexei Fedorov */ 7797215e0fSDaniel Boulby bl prepare_el3_entry 78e290a8fcSAlexei Fedorov 79df8f3188SJeenu Viswambharan bl handle_lower_el_ea_esb 8014c6016aSJeenu Viswambharan 81ed108b56SAlexei Fedorov /* Restore general purpose, PMCR_EL0 and ARMv8.3-PAuth registers */ 82ed108b56SAlexei Fedorov bl restore_gp_pmcr_pauth_regs 8314c6016aSJeenu Viswambharan1: 8414c6016aSJeenu Viswambharan#else 85c2d32a5fSMadhukar Pappireddy /* 86c2d32a5fSMadhukar Pappireddy * Note 1: The explicit DSB at the entry of various exception vectors 87c2d32a5fSMadhukar Pappireddy * for handling exceptions from lower ELs can inadvertently trigger an 88c2d32a5fSMadhukar Pappireddy * SError exception in EL3 due to pending asynchronous aborts in lower 89c2d32a5fSMadhukar Pappireddy * ELs. This will end up being handled by serror_sp_elx which will 90c2d32a5fSMadhukar Pappireddy * ultimately panic and die. 91c2d32a5fSMadhukar Pappireddy * The way to workaround is to update a flag to indicate if the exception 92c2d32a5fSMadhukar Pappireddy * truly came from EL3. This flag is allocated in the cpu_context 93c2d32a5fSMadhukar Pappireddy * structure and located at offset "CTX_EL3STATE_OFFSET + CTX_IS_IN_EL3" 94c2d32a5fSMadhukar Pappireddy * This is not a bullet proof solution to the problem at hand because 95c2d32a5fSMadhukar Pappireddy * we assume the instructions following "isb" that help to update the 96c2d32a5fSMadhukar Pappireddy * flag execute without causing further exceptions. 97c2d32a5fSMadhukar Pappireddy */ 98c2d32a5fSMadhukar Pappireddy 99c2d32a5fSMadhukar Pappireddy /* 10076a91d87SManish Pandey * For SoCs which do not implement RAS, use DSB as a barrier to 10176a91d87SManish Pandey * synchronize pending external aborts. 102c2d32a5fSMadhukar Pappireddy */ 103c2d32a5fSMadhukar Pappireddy dsb sy 104c2d32a5fSMadhukar Pappireddy 105c2d32a5fSMadhukar Pappireddy /* Unmask the SError interrupt */ 106c2d32a5fSMadhukar Pappireddy msr daifclr, #DAIF_ABT_BIT 107c2d32a5fSMadhukar Pappireddy 108c2d32a5fSMadhukar Pappireddy /* Use ISB for the above unmask operation to take effect immediately */ 109c2d32a5fSMadhukar Pappireddy isb 110c2d32a5fSMadhukar Pappireddy 11176a91d87SManish Pandey /* 11276a91d87SManish Pandey * Refer Note 1. 11376a91d87SManish Pandey * No need to restore X30 as macros following this modify x30 anyway. 11476a91d87SManish Pandey */ 115c2d32a5fSMadhukar Pappireddy str x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR] 116c2d32a5fSMadhukar Pappireddy mov x30, #1 117c2d32a5fSMadhukar Pappireddy str x30, [sp, #CTX_EL3STATE_OFFSET + CTX_IS_IN_EL3] 118c2d32a5fSMadhukar Pappireddy dmb sy 119c2d32a5fSMadhukar Pappireddy#endif 12076a91d87SManish Pandey .endm 121c2d32a5fSMadhukar Pappireddy 122a6ef4393SDouglas Raillard /* --------------------------------------------------------------------- 123a6ef4393SDouglas Raillard * This macro handles Synchronous exceptions. 124a6ef4393SDouglas Raillard * Only SMC exceptions are supported. 125a6ef4393SDouglas Raillard * --------------------------------------------------------------------- 126dce74b89SAchin Gupta */ 127dce74b89SAchin Gupta .macro handle_sync_exception 128872be88aSdp-arm#if ENABLE_RUNTIME_INSTRUMENTATION 129872be88aSdp-arm /* 130a6ef4393SDouglas Raillard * Read the timestamp value and store it in per-cpu data. The value 131a6ef4393SDouglas Raillard * will be extracted from per-cpu data by the C level SMC handler and 132a6ef4393SDouglas Raillard * saved to the PMF timestamp region. 133872be88aSdp-arm */ 134872be88aSdp-arm mrs x30, cntpct_el0 135872be88aSdp-arm str x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29] 136872be88aSdp-arm mrs x29, tpidr_el3 137872be88aSdp-arm str x30, [x29, #CPU_DATA_PMF_TS0_OFFSET] 138872be88aSdp-arm ldr x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29] 139872be88aSdp-arm#endif 140872be88aSdp-arm 141dce74b89SAchin Gupta mrs x30, esr_el3 142dce74b89SAchin Gupta ubfx x30, x30, #ESR_EC_SHIFT, #ESR_EC_LENGTH 143dce74b89SAchin Gupta 144a6ef4393SDouglas Raillard /* Handle SMC exceptions separately from other synchronous exceptions */ 145dce74b89SAchin Gupta cmp x30, #EC_AARCH32_SMC 146dce74b89SAchin Gupta b.eq smc_handler32 147dce74b89SAchin Gupta 148dce74b89SAchin Gupta cmp x30, #EC_AARCH64_SMC 149ccd81f1eSAndre Przywara b.eq sync_handler64 150ccd81f1eSAndre Przywara 151ccd81f1eSAndre Przywara cmp x30, #EC_AARCH64_SYS 152ccd81f1eSAndre Przywara b.eq sync_handler64 153dce74b89SAchin Gupta 154df8f3188SJeenu Viswambharan /* Synchronous exceptions other than the above are assumed to be EA */ 1554d91838bSJulius Werner ldr x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR] 156df8f3188SJeenu Viswambharan b enter_lower_el_sync_ea 157dce74b89SAchin Gupta .endm 158dce74b89SAchin Gupta 159dce74b89SAchin Gupta 160a6ef4393SDouglas Raillard /* --------------------------------------------------------------------- 161a6ef4393SDouglas Raillard * This macro handles FIQ or IRQ interrupts i.e. EL3, S-EL1 and NS 162a6ef4393SDouglas Raillard * interrupts. 163a6ef4393SDouglas Raillard * --------------------------------------------------------------------- 164dce74b89SAchin Gupta */ 165dce74b89SAchin Gupta .macro handle_interrupt_exception label 1665283962eSAntonio Nino Diaz 167e290a8fcSAlexei Fedorov /* 168ed108b56SAlexei Fedorov * Save general purpose and ARMv8.3-PAuth registers (if enabled). 169ed108b56SAlexei Fedorov * If Secure Cycle Counter is not disabled in MDCR_EL3 when 170ed108b56SAlexei Fedorov * ARMv8.5-PMU is implemented, save PMCR_EL0 and disable Cycle Counter. 1717d33ffe4SDaniel Boulby * Also set the PSTATE to a known state. 172e290a8fcSAlexei Fedorov */ 17397215e0fSDaniel Boulby bl prepare_el3_entry 174e290a8fcSAlexei Fedorov 175b86048c4SAntonio Nino Diaz#if ENABLE_PAUTH 176ed108b56SAlexei Fedorov /* Load and program APIAKey firmware key */ 177ed108b56SAlexei Fedorov bl pauth_load_bl31_apiakey 178b86048c4SAntonio Nino Diaz#endif 1795283962eSAntonio Nino Diaz 180a6ef4393SDouglas Raillard /* Save the EL3 system registers needed to return from this exception */ 1815717aae1SAchin Gupta mrs x0, spsr_el3 1825717aae1SAchin Gupta mrs x1, elr_el3 1835717aae1SAchin Gupta stp x0, x1, [sp, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3] 1845717aae1SAchin Gupta 185dce74b89SAchin Gupta /* Switch to the runtime stack i.e. SP_EL0 */ 186dce74b89SAchin Gupta ldr x2, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP] 187dce74b89SAchin Gupta mov x20, sp 188ed108b56SAlexei Fedorov msr spsel, #MODE_SP_EL0 189dce74b89SAchin Gupta mov sp, x2 190dce74b89SAchin Gupta 191dce74b89SAchin Gupta /* 192a6ef4393SDouglas Raillard * Find out whether this is a valid interrupt type. 193a6ef4393SDouglas Raillard * If the interrupt controller reports a spurious interrupt then return 194a6ef4393SDouglas Raillard * to where we came from. 195dce74b89SAchin Gupta */ 1969865ac15SDan Handley bl plat_ic_get_pending_interrupt_type 197dce74b89SAchin Gupta cmp x0, #INTR_TYPE_INVAL 198dce74b89SAchin Gupta b.eq interrupt_exit_\label 199dce74b89SAchin Gupta 200dce74b89SAchin Gupta /* 201a6ef4393SDouglas Raillard * Get the registered handler for this interrupt type. 202a6ef4393SDouglas Raillard * A NULL return value could be 'cause of the following conditions: 2035717aae1SAchin Gupta * 204a6ef4393SDouglas Raillard * a. An interrupt of a type was routed correctly but a handler for its 205a6ef4393SDouglas Raillard * type was not registered. 2065717aae1SAchin Gupta * 207a6ef4393SDouglas Raillard * b. An interrupt of a type was not routed correctly so a handler for 208a6ef4393SDouglas Raillard * its type was not registered. 2095717aae1SAchin Gupta * 210a6ef4393SDouglas Raillard * c. An interrupt of a type was routed correctly to EL3, but was 211a6ef4393SDouglas Raillard * deasserted before its pending state could be read. Another 212a6ef4393SDouglas Raillard * interrupt of a different type pended at the same time and its 213a6ef4393SDouglas Raillard * type was reported as pending instead. However, a handler for this 214a6ef4393SDouglas Raillard * type was not registered. 2155717aae1SAchin Gupta * 216a6ef4393SDouglas Raillard * a. and b. can only happen due to a programming error. The 217a6ef4393SDouglas Raillard * occurrence of c. could be beyond the control of Trusted Firmware. 218a6ef4393SDouglas Raillard * It makes sense to return from this exception instead of reporting an 219a6ef4393SDouglas Raillard * error. 220dce74b89SAchin Gupta */ 221dce74b89SAchin Gupta bl get_interrupt_type_handler 2225717aae1SAchin Gupta cbz x0, interrupt_exit_\label 223dce74b89SAchin Gupta mov x21, x0 224dce74b89SAchin Gupta 225dce74b89SAchin Gupta mov x0, #INTR_ID_UNAVAILABLE 226dce74b89SAchin Gupta 227dce74b89SAchin Gupta /* Set the current security state in the 'flags' parameter */ 228dce74b89SAchin Gupta mrs x2, scr_el3 229dce74b89SAchin Gupta ubfx x1, x2, #0, #1 230dce74b89SAchin Gupta 231dce74b89SAchin Gupta /* Restore the reference to the 'handle' i.e. SP_EL3 */ 232dce74b89SAchin Gupta mov x2, x20 233dce74b89SAchin Gupta 234b460b8bfSSoby Mathew /* x3 will point to a cookie (not used now) */ 235b460b8bfSSoby Mathew mov x3, xzr 236b460b8bfSSoby Mathew 237dce74b89SAchin Gupta /* Call the interrupt type handler */ 238dce74b89SAchin Gupta blr x21 239dce74b89SAchin Gupta 240dce74b89SAchin Guptainterrupt_exit_\label: 241dce74b89SAchin Gupta /* Return from exception, possibly in a different security state */ 242dce74b89SAchin Gupta b el3_exit 243dce74b89SAchin Gupta 244dce74b89SAchin Gupta .endm 245dce74b89SAchin Gupta 246dce74b89SAchin Gupta 247e0ae9fabSSandrine Bailleuxvector_base runtime_exceptions 248e0ae9fabSSandrine Bailleux 249a6ef4393SDouglas Raillard /* --------------------------------------------------------------------- 250a6ef4393SDouglas Raillard * Current EL with SP_EL0 : 0x0 - 0x200 251a6ef4393SDouglas Raillard * --------------------------------------------------------------------- 2524f6ad66aSAchin Gupta */ 253e0ae9fabSSandrine Bailleuxvector_entry sync_exception_sp_el0 2541f461979SJustin Chadwell#ifdef MONITOR_TRAPS 2551f461979SJustin Chadwell stp x29, x30, [sp, #-16]! 2561f461979SJustin Chadwell 2571f461979SJustin Chadwell mrs x30, esr_el3 2581f461979SJustin Chadwell ubfx x30, x30, #ESR_EC_SHIFT, #ESR_EC_LENGTH 2591f461979SJustin Chadwell 2601f461979SJustin Chadwell /* Check for BRK */ 2611f461979SJustin Chadwell cmp x30, #EC_BRK 2621f461979SJustin Chadwell b.eq brk_handler 2631f461979SJustin Chadwell 2641f461979SJustin Chadwell ldp x29, x30, [sp], #16 2651f461979SJustin Chadwell#endif /* MONITOR_TRAPS */ 2661f461979SJustin Chadwell 267a6ef4393SDouglas Raillard /* We don't expect any synchronous exceptions from EL3 */ 2684d91838bSJulius Werner b report_unhandled_exception 269a9203edaSRoberto Vargasend_vector_entry sync_exception_sp_el0 2704f6ad66aSAchin Gupta 271e0ae9fabSSandrine Bailleuxvector_entry irq_sp_el0 272a6ef4393SDouglas Raillard /* 273a6ef4393SDouglas Raillard * EL3 code is non-reentrant. Any asynchronous exception is a serious 274a6ef4393SDouglas Raillard * error. Loop infinitely. 275a6ef4393SDouglas Raillard */ 2764d91838bSJulius Werner b report_unhandled_interrupt 277a9203edaSRoberto Vargasend_vector_entry irq_sp_el0 2784f6ad66aSAchin Gupta 279e0ae9fabSSandrine Bailleux 280e0ae9fabSSandrine Bailleuxvector_entry fiq_sp_el0 2814d91838bSJulius Werner b report_unhandled_interrupt 282a9203edaSRoberto Vargasend_vector_entry fiq_sp_el0 2834f6ad66aSAchin Gupta 284e0ae9fabSSandrine Bailleux 285e0ae9fabSSandrine Bailleuxvector_entry serror_sp_el0 286eaeaa4d0SJeenu Viswambharan no_ret plat_handle_el3_ea 287a9203edaSRoberto Vargasend_vector_entry serror_sp_el0 2884f6ad66aSAchin Gupta 289a6ef4393SDouglas Raillard /* --------------------------------------------------------------------- 290a6ef4393SDouglas Raillard * Current EL with SP_ELx: 0x200 - 0x400 291a6ef4393SDouglas Raillard * --------------------------------------------------------------------- 2924f6ad66aSAchin Gupta */ 293e0ae9fabSSandrine Bailleuxvector_entry sync_exception_sp_elx 294a6ef4393SDouglas Raillard /* 295a6ef4393SDouglas Raillard * This exception will trigger if anything went wrong during a previous 296a6ef4393SDouglas Raillard * exception entry or exit or while handling an earlier unexpected 297a6ef4393SDouglas Raillard * synchronous exception. There is a high probability that SP_EL3 is 298a6ef4393SDouglas Raillard * corrupted. 299caa84939SJeenu Viswambharan */ 3004d91838bSJulius Werner b report_unhandled_exception 301a9203edaSRoberto Vargasend_vector_entry sync_exception_sp_elx 3024f6ad66aSAchin Gupta 303e0ae9fabSSandrine Bailleuxvector_entry irq_sp_elx 3044d91838bSJulius Werner b report_unhandled_interrupt 305a9203edaSRoberto Vargasend_vector_entry irq_sp_elx 306a7934d69SJeenu Viswambharan 307e0ae9fabSSandrine Bailleuxvector_entry fiq_sp_elx 3084d91838bSJulius Werner b report_unhandled_interrupt 309a9203edaSRoberto Vargasend_vector_entry fiq_sp_elx 310a7934d69SJeenu Viswambharan 311e0ae9fabSSandrine Bailleuxvector_entry serror_sp_elx 312c2d32a5fSMadhukar Pappireddy#if !RAS_EXTENSION 31376a91d87SManish Pandey /* 31476a91d87SManish Pandey * This will trigger if the exception was taken due to SError in EL3 or 31576a91d87SManish Pandey * because of pending asynchronous external aborts from lower EL that got 31676a91d87SManish Pandey * triggered due to explicit synchronization in EL3. Refer Note 1. 31776a91d87SManish Pandey */ 31876a91d87SManish Pandey /* Assumes SP_EL3 on entry */ 31976a91d87SManish Pandey str x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR] 32076a91d87SManish Pandey ldr x30, [sp, #CTX_EL3STATE_OFFSET + CTX_IS_IN_EL3] 32176a91d87SManish Pandey cbnz x30, 1f 32276a91d87SManish Pandey 32376a91d87SManish Pandey /* Handle asynchronous external abort from lower EL */ 32476a91d87SManish Pandey b handle_lower_el_async_ea 32576a91d87SManish Pandey1: 326c2d32a5fSMadhukar Pappireddy#endif 327eaeaa4d0SJeenu Viswambharan no_ret plat_handle_el3_ea 328a9203edaSRoberto Vargasend_vector_entry serror_sp_elx 3294f6ad66aSAchin Gupta 330a6ef4393SDouglas Raillard /* --------------------------------------------------------------------- 33144804252SSandrine Bailleux * Lower EL using AArch64 : 0x400 - 0x600 332a6ef4393SDouglas Raillard * --------------------------------------------------------------------- 3334f6ad66aSAchin Gupta */ 334e0ae9fabSSandrine Bailleuxvector_entry sync_exception_aarch64 335a6ef4393SDouglas Raillard /* 336a6ef4393SDouglas Raillard * This exception vector will be the entry point for SMCs and traps 337a6ef4393SDouglas Raillard * that are unhandled at lower ELs most commonly. SP_EL3 should point 338a6ef4393SDouglas Raillard * to a valid cpu context where the general purpose and system register 339a6ef4393SDouglas Raillard * state can be saved. 340caa84939SJeenu Viswambharan */ 3413b8456bdSManish V Badarkhe apply_at_speculative_wa 34214c6016aSJeenu Viswambharan check_and_unmask_ea 343caa84939SJeenu Viswambharan handle_sync_exception 344a9203edaSRoberto Vargasend_vector_entry sync_exception_aarch64 3454f6ad66aSAchin Gupta 346e0ae9fabSSandrine Bailleuxvector_entry irq_aarch64 3473b8456bdSManish V Badarkhe apply_at_speculative_wa 34814c6016aSJeenu Viswambharan check_and_unmask_ea 349dce74b89SAchin Gupta handle_interrupt_exception irq_aarch64 350a9203edaSRoberto Vargasend_vector_entry irq_aarch64 3514f6ad66aSAchin Gupta 352e0ae9fabSSandrine Bailleuxvector_entry fiq_aarch64 3533b8456bdSManish V Badarkhe apply_at_speculative_wa 35414c6016aSJeenu Viswambharan check_and_unmask_ea 355dce74b89SAchin Gupta handle_interrupt_exception fiq_aarch64 356a9203edaSRoberto Vargasend_vector_entry fiq_aarch64 3574f6ad66aSAchin Gupta 358e0ae9fabSSandrine Bailleuxvector_entry serror_aarch64 3593b8456bdSManish V Badarkhe apply_at_speculative_wa 360c2d32a5fSMadhukar Pappireddy#if RAS_EXTENSION 36176454abfSJeenu Viswambharan msr daifclr, #DAIF_ABT_BIT 362df8f3188SJeenu Viswambharan b enter_lower_el_async_ea 363c2d32a5fSMadhukar Pappireddy#else 36476a91d87SManish Pandey check_and_unmask_ea 36576a91d87SManish Pandey b handle_lower_el_async_ea 366c2d32a5fSMadhukar Pappireddy#endif 367a9203edaSRoberto Vargasend_vector_entry serror_aarch64 3684f6ad66aSAchin Gupta 369a6ef4393SDouglas Raillard /* --------------------------------------------------------------------- 37044804252SSandrine Bailleux * Lower EL using AArch32 : 0x600 - 0x800 371a6ef4393SDouglas Raillard * --------------------------------------------------------------------- 3724f6ad66aSAchin Gupta */ 373e0ae9fabSSandrine Bailleuxvector_entry sync_exception_aarch32 374a6ef4393SDouglas Raillard /* 375a6ef4393SDouglas Raillard * This exception vector will be the entry point for SMCs and traps 376a6ef4393SDouglas Raillard * that are unhandled at lower ELs most commonly. SP_EL3 should point 377a6ef4393SDouglas Raillard * to a valid cpu context where the general purpose and system register 378a6ef4393SDouglas Raillard * state can be saved. 379caa84939SJeenu Viswambharan */ 3803b8456bdSManish V Badarkhe apply_at_speculative_wa 38114c6016aSJeenu Viswambharan check_and_unmask_ea 382caa84939SJeenu Viswambharan handle_sync_exception 383a9203edaSRoberto Vargasend_vector_entry sync_exception_aarch32 3844f6ad66aSAchin Gupta 385e0ae9fabSSandrine Bailleuxvector_entry irq_aarch32 3863b8456bdSManish V Badarkhe apply_at_speculative_wa 38714c6016aSJeenu Viswambharan check_and_unmask_ea 388dce74b89SAchin Gupta handle_interrupt_exception irq_aarch32 389a9203edaSRoberto Vargasend_vector_entry irq_aarch32 3904f6ad66aSAchin Gupta 391e0ae9fabSSandrine Bailleuxvector_entry fiq_aarch32 3923b8456bdSManish V Badarkhe apply_at_speculative_wa 39314c6016aSJeenu Viswambharan check_and_unmask_ea 394dce74b89SAchin Gupta handle_interrupt_exception fiq_aarch32 395a9203edaSRoberto Vargasend_vector_entry fiq_aarch32 3964f6ad66aSAchin Gupta 397e0ae9fabSSandrine Bailleuxvector_entry serror_aarch32 3983b8456bdSManish V Badarkhe apply_at_speculative_wa 399c2d32a5fSMadhukar Pappireddy#if RAS_EXTENSION 40076454abfSJeenu Viswambharan msr daifclr, #DAIF_ABT_BIT 401df8f3188SJeenu Viswambharan b enter_lower_el_async_ea 402c2d32a5fSMadhukar Pappireddy#else 40376a91d87SManish Pandey check_and_unmask_ea 40476a91d87SManish Pandey b handle_lower_el_async_ea 405c2d32a5fSMadhukar Pappireddy#endif 406a9203edaSRoberto Vargasend_vector_entry serror_aarch32 407a7934d69SJeenu Viswambharan 4081f461979SJustin Chadwell#ifdef MONITOR_TRAPS 4091f461979SJustin Chadwell .section .rodata.brk_string, "aS" 4101f461979SJustin Chadwellbrk_location: 4111f461979SJustin Chadwell .asciz "Error at instruction 0x" 4121f461979SJustin Chadwellbrk_message: 4131f461979SJustin Chadwell .asciz "Unexpected BRK instruction with value 0x" 4141f461979SJustin Chadwell#endif /* MONITOR_TRAPS */ 4151f461979SJustin Chadwell 4162f370465SAntonio Nino Diaz /* --------------------------------------------------------------------- 417caa84939SJeenu Viswambharan * The following code handles secure monitor calls. 418a6ef4393SDouglas Raillard * Depending upon the execution state from where the SMC has been 419a6ef4393SDouglas Raillard * invoked, it frees some general purpose registers to perform the 420a6ef4393SDouglas Raillard * remaining tasks. They involve finding the runtime service handler 421a6ef4393SDouglas Raillard * that is the target of the SMC & switching to runtime stacks (SP_EL0) 422a6ef4393SDouglas Raillard * before calling the handler. 423caa84939SJeenu Viswambharan * 424a6ef4393SDouglas Raillard * Note that x30 has been explicitly saved and can be used here 425a6ef4393SDouglas Raillard * --------------------------------------------------------------------- 426caa84939SJeenu Viswambharan */ 427ccd81f1eSAndre Przywarafunc sync_exception_handler 428caa84939SJeenu Viswambharansmc_handler32: 429caa84939SJeenu Viswambharan /* Check whether aarch32 issued an SMC64 */ 430caa84939SJeenu Viswambharan tbnz x0, #FUNCID_CC_SHIFT, smc_prohibited 431caa84939SJeenu Viswambharan 432ccd81f1eSAndre Przywarasync_handler64: 4335283962eSAntonio Nino Diaz /* NOTE: The code below must preserve x0-x4 */ 4345283962eSAntonio Nino Diaz 435e290a8fcSAlexei Fedorov /* 436ed108b56SAlexei Fedorov * Save general purpose and ARMv8.3-PAuth registers (if enabled). 437ed108b56SAlexei Fedorov * If Secure Cycle Counter is not disabled in MDCR_EL3 when 438ed108b56SAlexei Fedorov * ARMv8.5-PMU is implemented, save PMCR_EL0 and disable Cycle Counter. 4397d33ffe4SDaniel Boulby * Also set the PSTATE to a known state. 440e290a8fcSAlexei Fedorov */ 44197215e0fSDaniel Boulby bl prepare_el3_entry 442e290a8fcSAlexei Fedorov 443b86048c4SAntonio Nino Diaz#if ENABLE_PAUTH 444ed108b56SAlexei Fedorov /* Load and program APIAKey firmware key */ 445ed108b56SAlexei Fedorov bl pauth_load_bl31_apiakey 446b86048c4SAntonio Nino Diaz#endif 4475283962eSAntonio Nino Diaz 448a6ef4393SDouglas Raillard /* 449a6ef4393SDouglas Raillard * Populate the parameters for the SMC handler. 450a6ef4393SDouglas Raillard * We already have x0-x4 in place. x5 will point to a cookie (not used 451a6ef4393SDouglas Raillard * now). x6 will point to the context structure (SP_EL3) and x7 will 452201ca5b6SDimitris Papastamos * contain flags we need to pass to the handler. 453caa84939SJeenu Viswambharan */ 454caa84939SJeenu Viswambharan mov x5, xzr 455caa84939SJeenu Viswambharan mov x6, sp 456caa84939SJeenu Viswambharan 457a6ef4393SDouglas Raillard /* 458a6ef4393SDouglas Raillard * Restore the saved C runtime stack value which will become the new 459a6ef4393SDouglas Raillard * SP_EL0 i.e. EL3 runtime stack. It was saved in the 'cpu_context' 460a6ef4393SDouglas Raillard * structure prior to the last ERET from EL3. 461caa84939SJeenu Viswambharan */ 462caa84939SJeenu Viswambharan ldr x12, [x6, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP] 463caa84939SJeenu Viswambharan 464caa84939SJeenu Viswambharan /* Switch to SP_EL0 */ 465ed108b56SAlexei Fedorov msr spsel, #MODE_SP_EL0 466caa84939SJeenu Viswambharan 467a6ef4393SDouglas Raillard /* 468e61713b0SManish Pandey * Save the SPSR_EL3 and ELR_EL3 in case there is a world 469a6ef4393SDouglas Raillard * switch during SMC handling. 470a6ef4393SDouglas Raillard * TODO: Revisit if all system registers can be saved later. 471caa84939SJeenu Viswambharan */ 472caa84939SJeenu Viswambharan mrs x16, spsr_el3 473caa84939SJeenu Viswambharan mrs x17, elr_el3 474caa84939SJeenu Viswambharan stp x16, x17, [x6, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3] 475e61713b0SManish Pandey 476e61713b0SManish Pandey /* Load SCR_EL3 */ 477e61713b0SManish Pandey mrs x18, scr_el3 478caa84939SJeenu Viswambharan 479ccd81f1eSAndre Przywara /* check for system register traps */ 480ccd81f1eSAndre Przywara mrs x16, esr_el3 481ccd81f1eSAndre Przywara ubfx x17, x16, #ESR_EC_SHIFT, #ESR_EC_LENGTH 482ccd81f1eSAndre Przywara cmp x17, #EC_AARCH64_SYS 483ccd81f1eSAndre Przywara b.eq sysreg_handler64 484ccd81f1eSAndre Przywara 4854693ff72SZelalem Aweke /* Clear flag register */ 4864693ff72SZelalem Aweke mov x7, xzr 4874693ff72SZelalem Aweke 4884693ff72SZelalem Aweke#if ENABLE_RME 4894693ff72SZelalem Aweke /* Copy SCR_EL3.NSE bit to the flag to indicate caller's security */ 4904693ff72SZelalem Aweke ubfx x7, x18, #SCR_NSE_SHIFT, 1 4914693ff72SZelalem Aweke 4924693ff72SZelalem Aweke /* 4934693ff72SZelalem Aweke * Shift copied SCR_EL3.NSE bit by 5 to create space for 4940fe7b9f2SOlivier Deprez * SCR_EL3.NS bit. Bit 5 of the flag corresponds to 4954693ff72SZelalem Aweke * the SCR_EL3.NSE bit. 4964693ff72SZelalem Aweke */ 4974693ff72SZelalem Aweke lsl x7, x7, #5 4984693ff72SZelalem Aweke#endif /* ENABLE_RME */ 4994693ff72SZelalem Aweke 500caa84939SJeenu Viswambharan /* Copy SCR_EL3.NS bit to the flag to indicate caller's security */ 501caa84939SJeenu Viswambharan bfi x7, x18, #0, #1 502caa84939SJeenu Viswambharan 5030fe7b9f2SOlivier Deprez /* 5040fe7b9f2SOlivier Deprez * Per SMCCCv1.3 a caller can set the SVE hint bit in the SMC FID 5050fe7b9f2SOlivier Deprez * passed through x0. Copy the SVE hint bit to flags and mask the 5060fe7b9f2SOlivier Deprez * bit in smc_fid passed to the standard service dispatcher. 5070fe7b9f2SOlivier Deprez * A service/dispatcher can retrieve the SVE hint bit state from 5080fe7b9f2SOlivier Deprez * flags using the appropriate helper. 5090fe7b9f2SOlivier Deprez */ 5100fe7b9f2SOlivier Deprez bfi x7, x0, #FUNCID_SVE_HINT_SHIFT, #FUNCID_SVE_HINT_MASK 5110fe7b9f2SOlivier Deprez bic x0, x0, #(FUNCID_SVE_HINT_MASK << FUNCID_SVE_HINT_SHIFT) 5120fe7b9f2SOlivier Deprez 513caa84939SJeenu Viswambharan mov sp, x12 514caa84939SJeenu Viswambharan 515cc485e27SMadhukar Pappireddy /* Get the unique owning entity number */ 516cc485e27SMadhukar Pappireddy ubfx x16, x0, #FUNCID_OEN_SHIFT, #FUNCID_OEN_WIDTH 517cc485e27SMadhukar Pappireddy ubfx x15, x0, #FUNCID_TYPE_SHIFT, #FUNCID_TYPE_WIDTH 518cc485e27SMadhukar Pappireddy orr x16, x16, x15, lsl #FUNCID_OEN_WIDTH 519cc485e27SMadhukar Pappireddy 520cc485e27SMadhukar Pappireddy /* Load descriptor index from array of indices */ 521c367b75eSMadhukar Pappireddy adrp x14, rt_svc_descs_indices 522c367b75eSMadhukar Pappireddy add x14, x14, :lo12:rt_svc_descs_indices 523cc485e27SMadhukar Pappireddy ldrb w15, [x14, x16] 524cc485e27SMadhukar Pappireddy 525cc485e27SMadhukar Pappireddy /* Any index greater than 127 is invalid. Check bit 7. */ 526cc485e27SMadhukar Pappireddy tbnz w15, 7, smc_unknown 527cc485e27SMadhukar Pappireddy 528cc485e27SMadhukar Pappireddy /* 529cc485e27SMadhukar Pappireddy * Get the descriptor using the index 530cc485e27SMadhukar Pappireddy * x11 = (base + off), w15 = index 531cc485e27SMadhukar Pappireddy * 532cc485e27SMadhukar Pappireddy * handler = (base + off) + (index << log2(size)) 533cc485e27SMadhukar Pappireddy */ 534cc485e27SMadhukar Pappireddy adr x11, (__RT_SVC_DESCS_START__ + RT_SVC_DESC_HANDLE) 535cc485e27SMadhukar Pappireddy lsl w10, w15, #RT_SVC_SIZE_LOG2 536cc485e27SMadhukar Pappireddy ldr x15, [x11, w10, uxtw] 537cc485e27SMadhukar Pappireddy 538a6ef4393SDouglas Raillard /* 539a6ef4393SDouglas Raillard * Call the Secure Monitor Call handler and then drop directly into 540a6ef4393SDouglas Raillard * el3_exit() which will program any remaining architectural state 541a6ef4393SDouglas Raillard * prior to issuing the ERET to the desired lower EL. 542caa84939SJeenu Viswambharan */ 543caa84939SJeenu Viswambharan#if DEBUG 544caa84939SJeenu Viswambharan cbz x15, rt_svc_fw_critical_error 545caa84939SJeenu Viswambharan#endif 546caa84939SJeenu Viswambharan blr x15 547caa84939SJeenu Viswambharan 548bbf8f6f9SYatharth Kochar b el3_exit 5494f6ad66aSAchin Gupta 550ccd81f1eSAndre Przywarasysreg_handler64: 551ccd81f1eSAndre Przywara mov x0, x16 /* ESR_EL3, containing syndrome information */ 552ccd81f1eSAndre Przywara mov x1, x6 /* lower EL's context */ 553ccd81f1eSAndre Przywara mov x19, x6 /* save context pointer for after the call */ 554ccd81f1eSAndre Przywara mov sp, x12 /* EL3 runtime stack, as loaded above */ 555ccd81f1eSAndre Przywara 556ccd81f1eSAndre Przywara /* int handle_sysreg_trap(uint64_t esr_el3, cpu_context_t *ctx); */ 557ccd81f1eSAndre Przywara bl handle_sysreg_trap 558ccd81f1eSAndre Przywara /* 559ccd81f1eSAndre Przywara * returns: 560ccd81f1eSAndre Przywara * -1: unhandled trap, panic 561ccd81f1eSAndre Przywara * 0: handled trap, return to the trapping instruction (repeating it) 562ccd81f1eSAndre Przywara * 1: handled trap, return to the next instruction 563ccd81f1eSAndre Przywara */ 564ccd81f1eSAndre Przywara 565ccd81f1eSAndre Przywara tst w0, w0 566*17d07a55SGovindraj Raja b.mi elx_panic /* negative return value: panic */ 567ccd81f1eSAndre Przywara b.eq 1f /* zero: do not change ELR_EL3 */ 568ccd81f1eSAndre Przywara 569ccd81f1eSAndre Przywara /* advance the PC to continue after the instruction */ 570ccd81f1eSAndre Przywara ldr x1, [x19, #CTX_EL3STATE_OFFSET + CTX_ELR_EL3] 571ccd81f1eSAndre Przywara add x1, x1, #4 572ccd81f1eSAndre Przywara str x1, [x19, #CTX_EL3STATE_OFFSET + CTX_ELR_EL3] 573ccd81f1eSAndre Przywara1: 574ccd81f1eSAndre Przywara b el3_exit 575ccd81f1eSAndre Przywara 576caa84939SJeenu Viswambharansmc_unknown: 577caa84939SJeenu Viswambharan /* 578cc485e27SMadhukar Pappireddy * Unknown SMC call. Populate return value with SMC_UNK and call 579cc485e27SMadhukar Pappireddy * el3_exit() which will restore the remaining architectural state 580cc485e27SMadhukar Pappireddy * i.e., SYS, GP and PAuth registers(if any) prior to issuing the ERET 581cc485e27SMadhukar Pappireddy * to the desired lower EL. 582caa84939SJeenu Viswambharan */ 5834abd7fa7SAntonio Nino Diaz mov x0, #SMC_UNK 584cc485e27SMadhukar Pappireddy str x0, [x6, #CTX_GPREGS_OFFSET + CTX_GPREG_X0] 585cc485e27SMadhukar Pappireddy b el3_exit 586caa84939SJeenu Viswambharan 587caa84939SJeenu Viswambharansmc_prohibited: 5883b8456bdSManish V Badarkhe restore_ptw_el1_sys_regs 5893b8456bdSManish V Badarkhe ldp x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28] 590c3260f9bSSoby Mathew ldr x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR] 5914abd7fa7SAntonio Nino Diaz mov x0, #SMC_UNK 592f461fe34SAnthony Steinhauser exception_return 593caa84939SJeenu Viswambharan 594ed108b56SAlexei Fedorov#if DEBUG 595caa84939SJeenu Viswambharanrt_svc_fw_critical_error: 596a6ef4393SDouglas Raillard /* Switch to SP_ELx */ 597ed108b56SAlexei Fedorov msr spsel, #MODE_SP_ELX 598a806dad5SJeenu Viswambharan no_ret report_unhandled_exception 599ed108b56SAlexei Fedorov#endif 600ccd81f1eSAndre Przywaraendfunc sync_exception_handler 6011f461979SJustin Chadwell 6021f461979SJustin Chadwell /* --------------------------------------------------------------------- 6031f461979SJustin Chadwell * The following code handles exceptions caused by BRK instructions. 6041f461979SJustin Chadwell * Following a BRK instruction, the only real valid cause of action is 6051f461979SJustin Chadwell * to print some information and panic, as the code that caused it is 6061f461979SJustin Chadwell * likely in an inconsistent internal state. 6071f461979SJustin Chadwell * 6081f461979SJustin Chadwell * This is initially intended to be used in conjunction with 6091f461979SJustin Chadwell * __builtin_trap. 6101f461979SJustin Chadwell * --------------------------------------------------------------------- 6111f461979SJustin Chadwell */ 6121f461979SJustin Chadwell#ifdef MONITOR_TRAPS 6131f461979SJustin Chadwellfunc brk_handler 6141f461979SJustin Chadwell /* Extract the ISS */ 6151f461979SJustin Chadwell mrs x10, esr_el3 6161f461979SJustin Chadwell ubfx x10, x10, #ESR_ISS_SHIFT, #ESR_ISS_LENGTH 6171f461979SJustin Chadwell 6181f461979SJustin Chadwell /* Ensure the console is initialized */ 6191f461979SJustin Chadwell bl plat_crash_console_init 6201f461979SJustin Chadwell 6211f461979SJustin Chadwell adr x4, brk_location 6221f461979SJustin Chadwell bl asm_print_str 6231f461979SJustin Chadwell mrs x4, elr_el3 6241f461979SJustin Chadwell bl asm_print_hex 6251f461979SJustin Chadwell bl asm_print_newline 6261f461979SJustin Chadwell 6271f461979SJustin Chadwell adr x4, brk_message 6281f461979SJustin Chadwell bl asm_print_str 6291f461979SJustin Chadwell mov x4, x10 6301f461979SJustin Chadwell mov x5, #28 6311f461979SJustin Chadwell bl asm_print_hex_bits 6321f461979SJustin Chadwell bl asm_print_newline 6331f461979SJustin Chadwell 6341f461979SJustin Chadwell no_ret plat_panic_handler 6351f461979SJustin Chadwellendfunc brk_handler 6361f461979SJustin Chadwell#endif /* MONITOR_TRAPS */ 637