14f6ad66aSAchin Gupta/* 217d07a55SGovindraj 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 /* 43d87c0e27SManish Pandey * Save LR and make x30 available as most of the routines in vector entry 44d87c0e27SManish Pandey * need a free register 45d87c0e27SManish Pandey */ 46d87c0e27SManish Pandey .macro save_x30 47d87c0e27SManish Pandey str x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR] 48d87c0e27SManish Pandey .endm 49d87c0e27SManish Pandey 50d87c0e27SManish Pandey /* 5114c6016aSJeenu Viswambharan * Macro that prepares entry to EL3 upon taking an exception. 5214c6016aSJeenu Viswambharan * 5314c6016aSJeenu Viswambharan * With RAS_EXTENSION, this macro synchronizes pending errors with an ESB 5414c6016aSJeenu Viswambharan * instruction. When an error is thus synchronized, the handling is 5514c6016aSJeenu Viswambharan * delegated to platform EA handler. 5614c6016aSJeenu Viswambharan * 57c2d32a5fSMadhukar Pappireddy * Without RAS_EXTENSION, this macro synchronizes pending errors using 58c2d32a5fSMadhukar Pappireddy * a DSB, unmasks Asynchronous External Aborts and saves X30 before 59c2d32a5fSMadhukar Pappireddy * setting the flag CTX_IS_IN_EL3. 6014c6016aSJeenu Viswambharan */ 6114c6016aSJeenu Viswambharan .macro check_and_unmask_ea 6214c6016aSJeenu Viswambharan#if RAS_EXTENSION 6314c6016aSJeenu Viswambharan /* Synchronize pending External Aborts */ 6414c6016aSJeenu Viswambharan esb 6514c6016aSJeenu Viswambharan 6614c6016aSJeenu Viswambharan /* Unmask the SError interrupt */ 6714c6016aSJeenu Viswambharan msr daifclr, #DAIF_ABT_BIT 6814c6016aSJeenu Viswambharan 6914c6016aSJeenu Viswambharan /* Check for SErrors synchronized by the ESB instruction */ 7014c6016aSJeenu Viswambharan mrs x30, DISR_EL1 7114c6016aSJeenu Viswambharan tbz x30, #DISR_A_BIT, 1f 7214c6016aSJeenu Viswambharan 73e290a8fcSAlexei Fedorov /* 74ed108b56SAlexei Fedorov * Save general purpose and ARMv8.3-PAuth registers (if enabled). 75ed108b56SAlexei Fedorov * If Secure Cycle Counter is not disabled in MDCR_EL3 when 76ed108b56SAlexei Fedorov * ARMv8.5-PMU is implemented, save PMCR_EL0 and disable Cycle Counter. 777d33ffe4SDaniel Boulby * Also set the PSTATE to a known state. 78e290a8fcSAlexei Fedorov */ 7997215e0fSDaniel Boulby bl prepare_el3_entry 80e290a8fcSAlexei Fedorov 81df8f3188SJeenu Viswambharan bl handle_lower_el_ea_esb 8214c6016aSJeenu Viswambharan 83ed108b56SAlexei Fedorov /* Restore general purpose, PMCR_EL0 and ARMv8.3-PAuth registers */ 84ed108b56SAlexei Fedorov bl restore_gp_pmcr_pauth_regs 8514c6016aSJeenu Viswambharan1: 8614c6016aSJeenu Viswambharan#else 87c2d32a5fSMadhukar Pappireddy /* 88c2d32a5fSMadhukar Pappireddy * Note 1: The explicit DSB at the entry of various exception vectors 89c2d32a5fSMadhukar Pappireddy * for handling exceptions from lower ELs can inadvertently trigger an 90c2d32a5fSMadhukar Pappireddy * SError exception in EL3 due to pending asynchronous aborts in lower 91c2d32a5fSMadhukar Pappireddy * ELs. This will end up being handled by serror_sp_elx which will 92c2d32a5fSMadhukar Pappireddy * ultimately panic and die. 93c2d32a5fSMadhukar Pappireddy * The way to workaround is to update a flag to indicate if the exception 94c2d32a5fSMadhukar Pappireddy * truly came from EL3. This flag is allocated in the cpu_context 95c2d32a5fSMadhukar Pappireddy * structure and located at offset "CTX_EL3STATE_OFFSET + CTX_IS_IN_EL3" 96c2d32a5fSMadhukar Pappireddy * This is not a bullet proof solution to the problem at hand because 97c2d32a5fSMadhukar Pappireddy * we assume the instructions following "isb" that help to update the 98c2d32a5fSMadhukar Pappireddy * flag execute without causing further exceptions. 99c2d32a5fSMadhukar Pappireddy */ 100c2d32a5fSMadhukar Pappireddy 101c2d32a5fSMadhukar Pappireddy /* 10276a91d87SManish Pandey * For SoCs which do not implement RAS, use DSB as a barrier to 10376a91d87SManish Pandey * synchronize pending external aborts. 104c2d32a5fSMadhukar Pappireddy */ 105c2d32a5fSMadhukar Pappireddy dsb sy 106c2d32a5fSMadhukar Pappireddy 107c2d32a5fSMadhukar Pappireddy /* Unmask the SError interrupt */ 108c2d32a5fSMadhukar Pappireddy msr daifclr, #DAIF_ABT_BIT 109c2d32a5fSMadhukar Pappireddy 110c2d32a5fSMadhukar Pappireddy /* Use ISB for the above unmask operation to take effect immediately */ 111c2d32a5fSMadhukar Pappireddy isb 112c2d32a5fSMadhukar Pappireddy 113d87c0e27SManish Pandey /* Refer Note 1. */ 114c2d32a5fSMadhukar Pappireddy mov x30, #1 115c2d32a5fSMadhukar Pappireddy str x30, [sp, #CTX_EL3STATE_OFFSET + CTX_IS_IN_EL3] 116c2d32a5fSMadhukar Pappireddy dmb sy 117c2d32a5fSMadhukar Pappireddy#endif 11876a91d87SManish Pandey .endm 119c2d32a5fSMadhukar Pappireddy 120a6ef4393SDouglas Raillard /* --------------------------------------------------------------------- 121a6ef4393SDouglas Raillard * This macro handles Synchronous exceptions. 122a6ef4393SDouglas Raillard * Only SMC exceptions are supported. 123a6ef4393SDouglas Raillard * --------------------------------------------------------------------- 124dce74b89SAchin Gupta */ 125dce74b89SAchin Gupta .macro handle_sync_exception 126872be88aSdp-arm#if ENABLE_RUNTIME_INSTRUMENTATION 127872be88aSdp-arm /* 128a6ef4393SDouglas Raillard * Read the timestamp value and store it in per-cpu data. The value 129a6ef4393SDouglas Raillard * will be extracted from per-cpu data by the C level SMC handler and 130a6ef4393SDouglas Raillard * saved to the PMF timestamp region. 131872be88aSdp-arm */ 132872be88aSdp-arm mrs x30, cntpct_el0 133872be88aSdp-arm str x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29] 134872be88aSdp-arm mrs x29, tpidr_el3 135872be88aSdp-arm str x30, [x29, #CPU_DATA_PMF_TS0_OFFSET] 136872be88aSdp-arm ldr x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29] 137872be88aSdp-arm#endif 138872be88aSdp-arm 139dce74b89SAchin Gupta mrs x30, esr_el3 140dce74b89SAchin Gupta ubfx x30, x30, #ESR_EC_SHIFT, #ESR_EC_LENGTH 141dce74b89SAchin Gupta 142a6ef4393SDouglas Raillard /* Handle SMC exceptions separately from other synchronous exceptions */ 143dce74b89SAchin Gupta cmp x30, #EC_AARCH32_SMC 144dce74b89SAchin Gupta b.eq smc_handler32 145dce74b89SAchin Gupta 146dce74b89SAchin Gupta cmp x30, #EC_AARCH64_SMC 147ccd81f1eSAndre Przywara b.eq sync_handler64 148ccd81f1eSAndre Przywara 149ccd81f1eSAndre Przywara cmp x30, #EC_AARCH64_SYS 150ccd81f1eSAndre Przywara b.eq sync_handler64 151dce74b89SAchin Gupta 152df8f3188SJeenu Viswambharan /* Synchronous exceptions other than the above are assumed to be EA */ 1534d91838bSJulius Werner ldr x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR] 1546f7de9a8SManish Pandey b handle_lower_el_sync_ea 155dce74b89SAchin Gupta .endm 156dce74b89SAchin Gupta 157dce74b89SAchin Gupta 158a6ef4393SDouglas Raillard /* --------------------------------------------------------------------- 159a6ef4393SDouglas Raillard * This macro handles FIQ or IRQ interrupts i.e. EL3, S-EL1 and NS 160a6ef4393SDouglas Raillard * interrupts. 161a6ef4393SDouglas Raillard * --------------------------------------------------------------------- 162dce74b89SAchin Gupta */ 163dce74b89SAchin Gupta .macro handle_interrupt_exception label 1645283962eSAntonio Nino Diaz 165e290a8fcSAlexei Fedorov /* 166ed108b56SAlexei Fedorov * Save general purpose and ARMv8.3-PAuth registers (if enabled). 167ed108b56SAlexei Fedorov * If Secure Cycle Counter is not disabled in MDCR_EL3 when 168ed108b56SAlexei Fedorov * ARMv8.5-PMU is implemented, save PMCR_EL0 and disable Cycle Counter. 1697d33ffe4SDaniel Boulby * Also set the PSTATE to a known state. 170e290a8fcSAlexei Fedorov */ 17197215e0fSDaniel Boulby bl prepare_el3_entry 172e290a8fcSAlexei Fedorov 173b86048c4SAntonio Nino Diaz#if ENABLE_PAUTH 174ed108b56SAlexei Fedorov /* Load and program APIAKey firmware key */ 175ed108b56SAlexei Fedorov bl pauth_load_bl31_apiakey 176b86048c4SAntonio Nino Diaz#endif 1775283962eSAntonio Nino Diaz 178a6ef4393SDouglas Raillard /* Save the EL3 system registers needed to return from this exception */ 1795717aae1SAchin Gupta mrs x0, spsr_el3 1805717aae1SAchin Gupta mrs x1, elr_el3 1815717aae1SAchin Gupta stp x0, x1, [sp, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3] 1825717aae1SAchin Gupta 183dce74b89SAchin Gupta /* Switch to the runtime stack i.e. SP_EL0 */ 184dce74b89SAchin Gupta ldr x2, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP] 185dce74b89SAchin Gupta mov x20, sp 186ed108b56SAlexei Fedorov msr spsel, #MODE_SP_EL0 187dce74b89SAchin Gupta mov sp, x2 188dce74b89SAchin Gupta 189dce74b89SAchin Gupta /* 190a6ef4393SDouglas Raillard * Find out whether this is a valid interrupt type. 191a6ef4393SDouglas Raillard * If the interrupt controller reports a spurious interrupt then return 192a6ef4393SDouglas Raillard * to where we came from. 193dce74b89SAchin Gupta */ 1949865ac15SDan Handley bl plat_ic_get_pending_interrupt_type 195dce74b89SAchin Gupta cmp x0, #INTR_TYPE_INVAL 196dce74b89SAchin Gupta b.eq interrupt_exit_\label 197dce74b89SAchin Gupta 198dce74b89SAchin Gupta /* 199a6ef4393SDouglas Raillard * Get the registered handler for this interrupt type. 200a6ef4393SDouglas Raillard * A NULL return value could be 'cause of the following conditions: 2015717aae1SAchin Gupta * 202a6ef4393SDouglas Raillard * a. An interrupt of a type was routed correctly but a handler for its 203a6ef4393SDouglas Raillard * type was not registered. 2045717aae1SAchin Gupta * 205a6ef4393SDouglas Raillard * b. An interrupt of a type was not routed correctly so a handler for 206a6ef4393SDouglas Raillard * its type was not registered. 2075717aae1SAchin Gupta * 208a6ef4393SDouglas Raillard * c. An interrupt of a type was routed correctly to EL3, but was 209a6ef4393SDouglas Raillard * deasserted before its pending state could be read. Another 210a6ef4393SDouglas Raillard * interrupt of a different type pended at the same time and its 211a6ef4393SDouglas Raillard * type was reported as pending instead. However, a handler for this 212a6ef4393SDouglas Raillard * type was not registered. 2135717aae1SAchin Gupta * 214a6ef4393SDouglas Raillard * a. and b. can only happen due to a programming error. The 215a6ef4393SDouglas Raillard * occurrence of c. could be beyond the control of Trusted Firmware. 216a6ef4393SDouglas Raillard * It makes sense to return from this exception instead of reporting an 217a6ef4393SDouglas Raillard * error. 218dce74b89SAchin Gupta */ 219dce74b89SAchin Gupta bl get_interrupt_type_handler 2205717aae1SAchin Gupta cbz x0, interrupt_exit_\label 221dce74b89SAchin Gupta mov x21, x0 222dce74b89SAchin Gupta 223dce74b89SAchin Gupta mov x0, #INTR_ID_UNAVAILABLE 224dce74b89SAchin Gupta 225dce74b89SAchin Gupta /* Set the current security state in the 'flags' parameter */ 226dce74b89SAchin Gupta mrs x2, scr_el3 227dce74b89SAchin Gupta ubfx x1, x2, #0, #1 228dce74b89SAchin Gupta 229dce74b89SAchin Gupta /* Restore the reference to the 'handle' i.e. SP_EL3 */ 230dce74b89SAchin Gupta mov x2, x20 231dce74b89SAchin Gupta 232b460b8bfSSoby Mathew /* x3 will point to a cookie (not used now) */ 233b460b8bfSSoby Mathew mov x3, xzr 234b460b8bfSSoby Mathew 235dce74b89SAchin Gupta /* Call the interrupt type handler */ 236dce74b89SAchin Gupta blr x21 237dce74b89SAchin Gupta 238dce74b89SAchin Guptainterrupt_exit_\label: 239dce74b89SAchin Gupta /* Return from exception, possibly in a different security state */ 240dce74b89SAchin Gupta b el3_exit 241dce74b89SAchin Gupta 242dce74b89SAchin Gupta .endm 243dce74b89SAchin Gupta 244dce74b89SAchin Gupta 245e0ae9fabSSandrine Bailleuxvector_base runtime_exceptions 246e0ae9fabSSandrine Bailleux 247a6ef4393SDouglas Raillard /* --------------------------------------------------------------------- 248a6ef4393SDouglas Raillard * Current EL with SP_EL0 : 0x0 - 0x200 249a6ef4393SDouglas Raillard * --------------------------------------------------------------------- 2504f6ad66aSAchin Gupta */ 251e0ae9fabSSandrine Bailleuxvector_entry sync_exception_sp_el0 2521f461979SJustin Chadwell#ifdef MONITOR_TRAPS 2531f461979SJustin Chadwell stp x29, x30, [sp, #-16]! 2541f461979SJustin Chadwell 2551f461979SJustin Chadwell mrs x30, esr_el3 2561f461979SJustin Chadwell ubfx x30, x30, #ESR_EC_SHIFT, #ESR_EC_LENGTH 2571f461979SJustin Chadwell 2581f461979SJustin Chadwell /* Check for BRK */ 2591f461979SJustin Chadwell cmp x30, #EC_BRK 2601f461979SJustin Chadwell b.eq brk_handler 2611f461979SJustin Chadwell 2621f461979SJustin Chadwell ldp x29, x30, [sp], #16 2631f461979SJustin Chadwell#endif /* MONITOR_TRAPS */ 2641f461979SJustin Chadwell 265a6ef4393SDouglas Raillard /* We don't expect any synchronous exceptions from EL3 */ 2664d91838bSJulius Werner b report_unhandled_exception 267a9203edaSRoberto Vargasend_vector_entry sync_exception_sp_el0 2684f6ad66aSAchin Gupta 269e0ae9fabSSandrine Bailleuxvector_entry irq_sp_el0 270a6ef4393SDouglas Raillard /* 271a6ef4393SDouglas Raillard * EL3 code is non-reentrant. Any asynchronous exception is a serious 272a6ef4393SDouglas Raillard * error. Loop infinitely. 273a6ef4393SDouglas Raillard */ 2744d91838bSJulius Werner b report_unhandled_interrupt 275a9203edaSRoberto Vargasend_vector_entry irq_sp_el0 2764f6ad66aSAchin Gupta 277e0ae9fabSSandrine Bailleux 278e0ae9fabSSandrine Bailleuxvector_entry fiq_sp_el0 2794d91838bSJulius Werner b report_unhandled_interrupt 280a9203edaSRoberto Vargasend_vector_entry fiq_sp_el0 2814f6ad66aSAchin Gupta 282e0ae9fabSSandrine Bailleux 283e0ae9fabSSandrine Bailleuxvector_entry serror_sp_el0 284eaeaa4d0SJeenu Viswambharan no_ret plat_handle_el3_ea 285a9203edaSRoberto Vargasend_vector_entry serror_sp_el0 2864f6ad66aSAchin Gupta 287a6ef4393SDouglas Raillard /* --------------------------------------------------------------------- 288a6ef4393SDouglas Raillard * Current EL with SP_ELx: 0x200 - 0x400 289a6ef4393SDouglas Raillard * --------------------------------------------------------------------- 2904f6ad66aSAchin Gupta */ 291e0ae9fabSSandrine Bailleuxvector_entry sync_exception_sp_elx 292a6ef4393SDouglas Raillard /* 293a6ef4393SDouglas Raillard * This exception will trigger if anything went wrong during a previous 294a6ef4393SDouglas Raillard * exception entry or exit or while handling an earlier unexpected 295a6ef4393SDouglas Raillard * synchronous exception. There is a high probability that SP_EL3 is 296a6ef4393SDouglas Raillard * corrupted. 297caa84939SJeenu Viswambharan */ 2984d91838bSJulius Werner b report_unhandled_exception 299a9203edaSRoberto Vargasend_vector_entry sync_exception_sp_elx 3004f6ad66aSAchin Gupta 301e0ae9fabSSandrine Bailleuxvector_entry irq_sp_elx 3024d91838bSJulius Werner b report_unhandled_interrupt 303a9203edaSRoberto Vargasend_vector_entry irq_sp_elx 304a7934d69SJeenu Viswambharan 305e0ae9fabSSandrine Bailleuxvector_entry fiq_sp_elx 3064d91838bSJulius Werner b report_unhandled_interrupt 307a9203edaSRoberto Vargasend_vector_entry fiq_sp_elx 308a7934d69SJeenu Viswambharan 309e0ae9fabSSandrine Bailleuxvector_entry serror_sp_elx 310c2d32a5fSMadhukar Pappireddy#if !RAS_EXTENSION 31176a91d87SManish Pandey /* 31276a91d87SManish Pandey * This will trigger if the exception was taken due to SError in EL3 or 31376a91d87SManish Pandey * because of pending asynchronous external aborts from lower EL that got 31476a91d87SManish Pandey * triggered due to explicit synchronization in EL3. Refer Note 1. 31576a91d87SManish Pandey */ 31676a91d87SManish Pandey /* Assumes SP_EL3 on entry */ 317d87c0e27SManish Pandey save_x30 31876a91d87SManish Pandey ldr x30, [sp, #CTX_EL3STATE_OFFSET + CTX_IS_IN_EL3] 31976a91d87SManish Pandey cbnz x30, 1f 32076a91d87SManish Pandey 32176a91d87SManish Pandey /* Handle asynchronous external abort from lower EL */ 32276a91d87SManish Pandey b handle_lower_el_async_ea 32376a91d87SManish Pandey1: 324c2d32a5fSMadhukar Pappireddy#endif 325eaeaa4d0SJeenu Viswambharan no_ret plat_handle_el3_ea 326a9203edaSRoberto Vargasend_vector_entry serror_sp_elx 3274f6ad66aSAchin Gupta 328a6ef4393SDouglas Raillard /* --------------------------------------------------------------------- 32944804252SSandrine Bailleux * Lower EL using AArch64 : 0x400 - 0x600 330a6ef4393SDouglas Raillard * --------------------------------------------------------------------- 3314f6ad66aSAchin Gupta */ 332e0ae9fabSSandrine Bailleuxvector_entry sync_exception_aarch64 333a6ef4393SDouglas Raillard /* 334a6ef4393SDouglas Raillard * This exception vector will be the entry point for SMCs and traps 335a6ef4393SDouglas Raillard * that are unhandled at lower ELs most commonly. SP_EL3 should point 336a6ef4393SDouglas Raillard * to a valid cpu context where the general purpose and system register 337a6ef4393SDouglas Raillard * state can be saved. 338caa84939SJeenu Viswambharan */ 339d87c0e27SManish Pandey save_x30 3403b8456bdSManish V Badarkhe apply_at_speculative_wa 34114c6016aSJeenu Viswambharan check_and_unmask_ea 342caa84939SJeenu Viswambharan handle_sync_exception 343a9203edaSRoberto Vargasend_vector_entry sync_exception_aarch64 3444f6ad66aSAchin Gupta 345e0ae9fabSSandrine Bailleuxvector_entry irq_aarch64 346d87c0e27SManish Pandey save_x30 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 353d87c0e27SManish Pandey save_x30 3543b8456bdSManish V Badarkhe apply_at_speculative_wa 35514c6016aSJeenu Viswambharan check_and_unmask_ea 356dce74b89SAchin Gupta handle_interrupt_exception fiq_aarch64 357a9203edaSRoberto Vargasend_vector_entry fiq_aarch64 3584f6ad66aSAchin Gupta 359e0ae9fabSSandrine Bailleuxvector_entry serror_aarch64 360d87c0e27SManish Pandey save_x30 3613b8456bdSManish V Badarkhe apply_at_speculative_wa 362c2d32a5fSMadhukar Pappireddy#if RAS_EXTENSION 36376454abfSJeenu Viswambharan msr daifclr, #DAIF_ABT_BIT 364c2d32a5fSMadhukar Pappireddy#else 36576a91d87SManish Pandey check_and_unmask_ea 366c2d32a5fSMadhukar Pappireddy#endif 3676f7de9a8SManish Pandey b handle_lower_el_async_ea 3686f7de9a8SManish Pandey 369a9203edaSRoberto Vargasend_vector_entry serror_aarch64 3704f6ad66aSAchin Gupta 371a6ef4393SDouglas Raillard /* --------------------------------------------------------------------- 37244804252SSandrine Bailleux * Lower EL using AArch32 : 0x600 - 0x800 373a6ef4393SDouglas Raillard * --------------------------------------------------------------------- 3744f6ad66aSAchin Gupta */ 375e0ae9fabSSandrine Bailleuxvector_entry sync_exception_aarch32 376a6ef4393SDouglas Raillard /* 377a6ef4393SDouglas Raillard * This exception vector will be the entry point for SMCs and traps 378a6ef4393SDouglas Raillard * that are unhandled at lower ELs most commonly. SP_EL3 should point 379a6ef4393SDouglas Raillard * to a valid cpu context where the general purpose and system register 380a6ef4393SDouglas Raillard * state can be saved. 381caa84939SJeenu Viswambharan */ 382d87c0e27SManish Pandey save_x30 3833b8456bdSManish V Badarkhe apply_at_speculative_wa 38414c6016aSJeenu Viswambharan check_and_unmask_ea 385caa84939SJeenu Viswambharan handle_sync_exception 386a9203edaSRoberto Vargasend_vector_entry sync_exception_aarch32 3874f6ad66aSAchin Gupta 388e0ae9fabSSandrine Bailleuxvector_entry irq_aarch32 389d87c0e27SManish Pandey save_x30 3903b8456bdSManish V Badarkhe apply_at_speculative_wa 39114c6016aSJeenu Viswambharan check_and_unmask_ea 392dce74b89SAchin Gupta handle_interrupt_exception irq_aarch32 393a9203edaSRoberto Vargasend_vector_entry irq_aarch32 3944f6ad66aSAchin Gupta 395e0ae9fabSSandrine Bailleuxvector_entry fiq_aarch32 396d87c0e27SManish Pandey save_x30 3973b8456bdSManish V Badarkhe apply_at_speculative_wa 39814c6016aSJeenu Viswambharan check_and_unmask_ea 399dce74b89SAchin Gupta handle_interrupt_exception fiq_aarch32 400a9203edaSRoberto Vargasend_vector_entry fiq_aarch32 4014f6ad66aSAchin Gupta 402e0ae9fabSSandrine Bailleuxvector_entry serror_aarch32 403d87c0e27SManish Pandey save_x30 4043b8456bdSManish V Badarkhe apply_at_speculative_wa 405c2d32a5fSMadhukar Pappireddy#if RAS_EXTENSION 40676454abfSJeenu Viswambharan msr daifclr, #DAIF_ABT_BIT 407c2d32a5fSMadhukar Pappireddy#else 40876a91d87SManish Pandey check_and_unmask_ea 409c2d32a5fSMadhukar Pappireddy#endif 4106f7de9a8SManish Pandey b handle_lower_el_async_ea 4116f7de9a8SManish Pandey 412a9203edaSRoberto Vargasend_vector_entry serror_aarch32 413a7934d69SJeenu Viswambharan 4141f461979SJustin Chadwell#ifdef MONITOR_TRAPS 4151f461979SJustin Chadwell .section .rodata.brk_string, "aS" 4161f461979SJustin Chadwellbrk_location: 4171f461979SJustin Chadwell .asciz "Error at instruction 0x" 4181f461979SJustin Chadwellbrk_message: 4191f461979SJustin Chadwell .asciz "Unexpected BRK instruction with value 0x" 4201f461979SJustin Chadwell#endif /* MONITOR_TRAPS */ 4211f461979SJustin Chadwell 4222f370465SAntonio Nino Diaz /* --------------------------------------------------------------------- 423caa84939SJeenu Viswambharan * The following code handles secure monitor calls. 424a6ef4393SDouglas Raillard * Depending upon the execution state from where the SMC has been 425a6ef4393SDouglas Raillard * invoked, it frees some general purpose registers to perform the 426a6ef4393SDouglas Raillard * remaining tasks. They involve finding the runtime service handler 427a6ef4393SDouglas Raillard * that is the target of the SMC & switching to runtime stacks (SP_EL0) 428a6ef4393SDouglas Raillard * before calling the handler. 429caa84939SJeenu Viswambharan * 430a6ef4393SDouglas Raillard * Note that x30 has been explicitly saved and can be used here 431a6ef4393SDouglas Raillard * --------------------------------------------------------------------- 432caa84939SJeenu Viswambharan */ 433ccd81f1eSAndre Przywarafunc sync_exception_handler 434caa84939SJeenu Viswambharansmc_handler32: 435caa84939SJeenu Viswambharan /* Check whether aarch32 issued an SMC64 */ 436caa84939SJeenu Viswambharan tbnz x0, #FUNCID_CC_SHIFT, smc_prohibited 437caa84939SJeenu Viswambharan 438ccd81f1eSAndre Przywarasync_handler64: 4395283962eSAntonio Nino Diaz /* NOTE: The code below must preserve x0-x4 */ 4405283962eSAntonio Nino Diaz 441e290a8fcSAlexei Fedorov /* 442ed108b56SAlexei Fedorov * Save general purpose and ARMv8.3-PAuth registers (if enabled). 443ed108b56SAlexei Fedorov * If Secure Cycle Counter is not disabled in MDCR_EL3 when 444ed108b56SAlexei Fedorov * ARMv8.5-PMU is implemented, save PMCR_EL0 and disable Cycle Counter. 4457d33ffe4SDaniel Boulby * Also set the PSTATE to a known state. 446e290a8fcSAlexei Fedorov */ 44797215e0fSDaniel Boulby bl prepare_el3_entry 448e290a8fcSAlexei Fedorov 449b86048c4SAntonio Nino Diaz#if ENABLE_PAUTH 450ed108b56SAlexei Fedorov /* Load and program APIAKey firmware key */ 451ed108b56SAlexei Fedorov bl pauth_load_bl31_apiakey 452b86048c4SAntonio Nino Diaz#endif 4535283962eSAntonio Nino Diaz 454a6ef4393SDouglas Raillard /* 455a6ef4393SDouglas Raillard * Populate the parameters for the SMC handler. 456a6ef4393SDouglas Raillard * We already have x0-x4 in place. x5 will point to a cookie (not used 457a6ef4393SDouglas Raillard * now). x6 will point to the context structure (SP_EL3) and x7 will 458201ca5b6SDimitris Papastamos * contain flags we need to pass to the handler. 459caa84939SJeenu Viswambharan */ 460caa84939SJeenu Viswambharan mov x5, xzr 461caa84939SJeenu Viswambharan mov x6, sp 462caa84939SJeenu Viswambharan 463a6ef4393SDouglas Raillard /* 464a6ef4393SDouglas Raillard * Restore the saved C runtime stack value which will become the new 465a6ef4393SDouglas Raillard * SP_EL0 i.e. EL3 runtime stack. It was saved in the 'cpu_context' 466a6ef4393SDouglas Raillard * structure prior to the last ERET from EL3. 467caa84939SJeenu Viswambharan */ 468caa84939SJeenu Viswambharan ldr x12, [x6, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP] 469caa84939SJeenu Viswambharan 470caa84939SJeenu Viswambharan /* Switch to SP_EL0 */ 471ed108b56SAlexei Fedorov msr spsel, #MODE_SP_EL0 472caa84939SJeenu Viswambharan 473a6ef4393SDouglas Raillard /* 474e61713b0SManish Pandey * Save the SPSR_EL3 and ELR_EL3 in case there is a world 475a6ef4393SDouglas Raillard * switch during SMC handling. 476a6ef4393SDouglas Raillard * TODO: Revisit if all system registers can be saved later. 477caa84939SJeenu Viswambharan */ 478caa84939SJeenu Viswambharan mrs x16, spsr_el3 479caa84939SJeenu Viswambharan mrs x17, elr_el3 480caa84939SJeenu Viswambharan stp x16, x17, [x6, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3] 481e61713b0SManish Pandey 482e61713b0SManish Pandey /* Load SCR_EL3 */ 483e61713b0SManish Pandey mrs x18, scr_el3 484caa84939SJeenu Viswambharan 485ccd81f1eSAndre Przywara /* check for system register traps */ 486ccd81f1eSAndre Przywara mrs x16, esr_el3 487ccd81f1eSAndre Przywara ubfx x17, x16, #ESR_EC_SHIFT, #ESR_EC_LENGTH 488ccd81f1eSAndre Przywara cmp x17, #EC_AARCH64_SYS 489ccd81f1eSAndre Przywara b.eq sysreg_handler64 490ccd81f1eSAndre Przywara 4914693ff72SZelalem Aweke /* Clear flag register */ 4924693ff72SZelalem Aweke mov x7, xzr 4934693ff72SZelalem Aweke 4944693ff72SZelalem Aweke#if ENABLE_RME 4954693ff72SZelalem Aweke /* Copy SCR_EL3.NSE bit to the flag to indicate caller's security */ 4964693ff72SZelalem Aweke ubfx x7, x18, #SCR_NSE_SHIFT, 1 4974693ff72SZelalem Aweke 4984693ff72SZelalem Aweke /* 4994693ff72SZelalem Aweke * Shift copied SCR_EL3.NSE bit by 5 to create space for 5000fe7b9f2SOlivier Deprez * SCR_EL3.NS bit. Bit 5 of the flag corresponds to 5014693ff72SZelalem Aweke * the SCR_EL3.NSE bit. 5024693ff72SZelalem Aweke */ 5034693ff72SZelalem Aweke lsl x7, x7, #5 5044693ff72SZelalem Aweke#endif /* ENABLE_RME */ 5054693ff72SZelalem Aweke 506caa84939SJeenu Viswambharan /* Copy SCR_EL3.NS bit to the flag to indicate caller's security */ 507caa84939SJeenu Viswambharan bfi x7, x18, #0, #1 508caa84939SJeenu Viswambharan 509*f8a35797SJayanth Dodderi Chidanand mov sp, x12 510*f8a35797SJayanth Dodderi Chidanand 511*f8a35797SJayanth Dodderi Chidanand /* 512*f8a35797SJayanth Dodderi Chidanand * Per SMCCC documentation, bits [23:17] must be zero for Fast 513*f8a35797SJayanth Dodderi Chidanand * SMCs. Other values are reserved for future use. Ensure that 514*f8a35797SJayanth Dodderi Chidanand * these bits are zeroes, if not report as unknown SMC. 515*f8a35797SJayanth Dodderi Chidanand */ 516*f8a35797SJayanth Dodderi Chidanand tbz x0, #FUNCID_TYPE_SHIFT, 2f /* Skip check if its a Yield Call*/ 517*f8a35797SJayanth Dodderi Chidanand tst x0, #(FUNCID_FC_RESERVED_MASK << FUNCID_FC_RESERVED_SHIFT) 518*f8a35797SJayanth Dodderi Chidanand b.ne smc_unknown 519*f8a35797SJayanth Dodderi Chidanand 5200fe7b9f2SOlivier Deprez /* 5210fe7b9f2SOlivier Deprez * Per SMCCCv1.3 a caller can set the SVE hint bit in the SMC FID 5220fe7b9f2SOlivier Deprez * passed through x0. Copy the SVE hint bit to flags and mask the 5230fe7b9f2SOlivier Deprez * bit in smc_fid passed to the standard service dispatcher. 5240fe7b9f2SOlivier Deprez * A service/dispatcher can retrieve the SVE hint bit state from 5250fe7b9f2SOlivier Deprez * flags using the appropriate helper. 5260fe7b9f2SOlivier Deprez */ 527*f8a35797SJayanth Dodderi Chidanand2: 5280fe7b9f2SOlivier Deprez bfi x7, x0, #FUNCID_SVE_HINT_SHIFT, #FUNCID_SVE_HINT_MASK 5290fe7b9f2SOlivier Deprez bic x0, x0, #(FUNCID_SVE_HINT_MASK << FUNCID_SVE_HINT_SHIFT) 5300fe7b9f2SOlivier Deprez 531cc485e27SMadhukar Pappireddy /* Get the unique owning entity number */ 532cc485e27SMadhukar Pappireddy ubfx x16, x0, #FUNCID_OEN_SHIFT, #FUNCID_OEN_WIDTH 533cc485e27SMadhukar Pappireddy ubfx x15, x0, #FUNCID_TYPE_SHIFT, #FUNCID_TYPE_WIDTH 534cc485e27SMadhukar Pappireddy orr x16, x16, x15, lsl #FUNCID_OEN_WIDTH 535cc485e27SMadhukar Pappireddy 536cc485e27SMadhukar Pappireddy /* Load descriptor index from array of indices */ 537c367b75eSMadhukar Pappireddy adrp x14, rt_svc_descs_indices 538c367b75eSMadhukar Pappireddy add x14, x14, :lo12:rt_svc_descs_indices 539cc485e27SMadhukar Pappireddy ldrb w15, [x14, x16] 540cc485e27SMadhukar Pappireddy 541cc485e27SMadhukar Pappireddy /* Any index greater than 127 is invalid. Check bit 7. */ 542cc485e27SMadhukar Pappireddy tbnz w15, 7, smc_unknown 543cc485e27SMadhukar Pappireddy 544cc485e27SMadhukar Pappireddy /* 545cc485e27SMadhukar Pappireddy * Get the descriptor using the index 546cc485e27SMadhukar Pappireddy * x11 = (base + off), w15 = index 547cc485e27SMadhukar Pappireddy * 548cc485e27SMadhukar Pappireddy * handler = (base + off) + (index << log2(size)) 549cc485e27SMadhukar Pappireddy */ 550cc485e27SMadhukar Pappireddy adr x11, (__RT_SVC_DESCS_START__ + RT_SVC_DESC_HANDLE) 551cc485e27SMadhukar Pappireddy lsl w10, w15, #RT_SVC_SIZE_LOG2 552cc485e27SMadhukar Pappireddy ldr x15, [x11, w10, uxtw] 553cc485e27SMadhukar Pappireddy 554a6ef4393SDouglas Raillard /* 555a6ef4393SDouglas Raillard * Call the Secure Monitor Call handler and then drop directly into 556a6ef4393SDouglas Raillard * el3_exit() which will program any remaining architectural state 557a6ef4393SDouglas Raillard * prior to issuing the ERET to the desired lower EL. 558caa84939SJeenu Viswambharan */ 559caa84939SJeenu Viswambharan#if DEBUG 560caa84939SJeenu Viswambharan cbz x15, rt_svc_fw_critical_error 561caa84939SJeenu Viswambharan#endif 562caa84939SJeenu Viswambharan blr x15 563caa84939SJeenu Viswambharan 564bbf8f6f9SYatharth Kochar b el3_exit 5654f6ad66aSAchin Gupta 566ccd81f1eSAndre Przywarasysreg_handler64: 567ccd81f1eSAndre Przywara mov x0, x16 /* ESR_EL3, containing syndrome information */ 568ccd81f1eSAndre Przywara mov x1, x6 /* lower EL's context */ 569ccd81f1eSAndre Przywara mov x19, x6 /* save context pointer for after the call */ 570ccd81f1eSAndre Przywara mov sp, x12 /* EL3 runtime stack, as loaded above */ 571ccd81f1eSAndre Przywara 572ccd81f1eSAndre Przywara /* int handle_sysreg_trap(uint64_t esr_el3, cpu_context_t *ctx); */ 573ccd81f1eSAndre Przywara bl handle_sysreg_trap 574ccd81f1eSAndre Przywara /* 575ccd81f1eSAndre Przywara * returns: 576ccd81f1eSAndre Przywara * -1: unhandled trap, panic 577ccd81f1eSAndre Przywara * 0: handled trap, return to the trapping instruction (repeating it) 578ccd81f1eSAndre Przywara * 1: handled trap, return to the next instruction 579ccd81f1eSAndre Przywara */ 580ccd81f1eSAndre Przywara 581ccd81f1eSAndre Przywara tst w0, w0 58217d07a55SGovindraj Raja b.mi elx_panic /* negative return value: panic */ 583ccd81f1eSAndre Przywara b.eq 1f /* zero: do not change ELR_EL3 */ 584ccd81f1eSAndre Przywara 585ccd81f1eSAndre Przywara /* advance the PC to continue after the instruction */ 586ccd81f1eSAndre Przywara ldr x1, [x19, #CTX_EL3STATE_OFFSET + CTX_ELR_EL3] 587ccd81f1eSAndre Przywara add x1, x1, #4 588ccd81f1eSAndre Przywara str x1, [x19, #CTX_EL3STATE_OFFSET + CTX_ELR_EL3] 589ccd81f1eSAndre Przywara1: 590ccd81f1eSAndre Przywara b el3_exit 591ccd81f1eSAndre Przywara 592caa84939SJeenu Viswambharansmc_unknown: 593caa84939SJeenu Viswambharan /* 594cc485e27SMadhukar Pappireddy * Unknown SMC call. Populate return value with SMC_UNK and call 595cc485e27SMadhukar Pappireddy * el3_exit() which will restore the remaining architectural state 596cc485e27SMadhukar Pappireddy * i.e., SYS, GP and PAuth registers(if any) prior to issuing the ERET 597cc485e27SMadhukar Pappireddy * to the desired lower EL. 598caa84939SJeenu Viswambharan */ 5994abd7fa7SAntonio Nino Diaz mov x0, #SMC_UNK 600cc485e27SMadhukar Pappireddy str x0, [x6, #CTX_GPREGS_OFFSET + CTX_GPREG_X0] 601cc485e27SMadhukar Pappireddy b el3_exit 602caa84939SJeenu Viswambharan 603caa84939SJeenu Viswambharansmc_prohibited: 6043b8456bdSManish V Badarkhe restore_ptw_el1_sys_regs 6053b8456bdSManish V Badarkhe ldp x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28] 606c3260f9bSSoby Mathew ldr x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR] 6074abd7fa7SAntonio Nino Diaz mov x0, #SMC_UNK 608f461fe34SAnthony Steinhauser exception_return 609caa84939SJeenu Viswambharan 610ed108b56SAlexei Fedorov#if DEBUG 611caa84939SJeenu Viswambharanrt_svc_fw_critical_error: 612a6ef4393SDouglas Raillard /* Switch to SP_ELx */ 613ed108b56SAlexei Fedorov msr spsel, #MODE_SP_ELX 614a806dad5SJeenu Viswambharan no_ret report_unhandled_exception 615ed108b56SAlexei Fedorov#endif 616ccd81f1eSAndre Przywaraendfunc sync_exception_handler 6171f461979SJustin Chadwell 6181f461979SJustin Chadwell /* --------------------------------------------------------------------- 6191f461979SJustin Chadwell * The following code handles exceptions caused by BRK instructions. 6201f461979SJustin Chadwell * Following a BRK instruction, the only real valid cause of action is 6211f461979SJustin Chadwell * to print some information and panic, as the code that caused it is 6221f461979SJustin Chadwell * likely in an inconsistent internal state. 6231f461979SJustin Chadwell * 6241f461979SJustin Chadwell * This is initially intended to be used in conjunction with 6251f461979SJustin Chadwell * __builtin_trap. 6261f461979SJustin Chadwell * --------------------------------------------------------------------- 6271f461979SJustin Chadwell */ 6281f461979SJustin Chadwell#ifdef MONITOR_TRAPS 6291f461979SJustin Chadwellfunc brk_handler 6301f461979SJustin Chadwell /* Extract the ISS */ 6311f461979SJustin Chadwell mrs x10, esr_el3 6321f461979SJustin Chadwell ubfx x10, x10, #ESR_ISS_SHIFT, #ESR_ISS_LENGTH 6331f461979SJustin Chadwell 6341f461979SJustin Chadwell /* Ensure the console is initialized */ 6351f461979SJustin Chadwell bl plat_crash_console_init 6361f461979SJustin Chadwell 6371f461979SJustin Chadwell adr x4, brk_location 6381f461979SJustin Chadwell bl asm_print_str 6391f461979SJustin Chadwell mrs x4, elr_el3 6401f461979SJustin Chadwell bl asm_print_hex 6411f461979SJustin Chadwell bl asm_print_newline 6421f461979SJustin Chadwell 6431f461979SJustin Chadwell adr x4, brk_message 6441f461979SJustin Chadwell bl asm_print_str 6451f461979SJustin Chadwell mov x4, x10 6461f461979SJustin Chadwell mov x5, #28 6471f461979SJustin Chadwell bl asm_print_hex_bits 6481f461979SJustin Chadwell bl asm_print_newline 6491f461979SJustin Chadwell 6501f461979SJustin Chadwell no_ret plat_panic_handler 6511f461979SJustin Chadwellendfunc brk_handler 6521f461979SJustin Chadwell#endif /* MONITOR_TRAPS */ 653