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 * 539202d519SManish Pandey * With RAS_FFH_SUPPORT, this macro synchronizes pending errors with an 549202d519SManish Pandey * ESB instruction. When an error is thus synchronized, the handling is 5514c6016aSJeenu Viswambharan * delegated to platform EA handler. 5614c6016aSJeenu Viswambharan * 579202d519SManish Pandey * Without RAS_FFH_SUPPORT, 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 629202d519SManish Pandey#if RAS_FFH_SUPPORT 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). 751d6d6802SBoyan Karatotev * Also save PMCR_EL0 and 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 111d87c0e27SManish Pandey /* Refer Note 1. */ 112c2d32a5fSMadhukar Pappireddy mov x30, #1 113c2d32a5fSMadhukar Pappireddy str x30, [sp, #CTX_EL3STATE_OFFSET + CTX_IS_IN_EL3] 114c2d32a5fSMadhukar Pappireddy dmb sy 115c2d32a5fSMadhukar Pappireddy#endif 11676a91d87SManish Pandey .endm 117c2d32a5fSMadhukar Pappireddy 118a6ef4393SDouglas Raillard /* --------------------------------------------------------------------- 119a6ef4393SDouglas Raillard * This macro handles Synchronous exceptions. 120a6ef4393SDouglas Raillard * Only SMC exceptions are supported. 121a6ef4393SDouglas Raillard * --------------------------------------------------------------------- 122dce74b89SAchin Gupta */ 123dce74b89SAchin Gupta .macro handle_sync_exception 124872be88aSdp-arm#if ENABLE_RUNTIME_INSTRUMENTATION 125872be88aSdp-arm /* 126a6ef4393SDouglas Raillard * Read the timestamp value and store it in per-cpu data. The value 127a6ef4393SDouglas Raillard * will be extracted from per-cpu data by the C level SMC handler and 128a6ef4393SDouglas Raillard * saved to the PMF timestamp region. 129872be88aSdp-arm */ 130872be88aSdp-arm mrs x30, cntpct_el0 131872be88aSdp-arm str x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29] 132872be88aSdp-arm mrs x29, tpidr_el3 133872be88aSdp-arm str x30, [x29, #CPU_DATA_PMF_TS0_OFFSET] 134872be88aSdp-arm ldr x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29] 135872be88aSdp-arm#endif 136872be88aSdp-arm 137dce74b89SAchin Gupta mrs x30, esr_el3 138dce74b89SAchin Gupta ubfx x30, x30, #ESR_EC_SHIFT, #ESR_EC_LENGTH 139dce74b89SAchin Gupta 140a6ef4393SDouglas Raillard /* Handle SMC exceptions separately from other synchronous exceptions */ 141dce74b89SAchin Gupta cmp x30, #EC_AARCH32_SMC 142dce74b89SAchin Gupta b.eq smc_handler32 143dce74b89SAchin Gupta 144dce74b89SAchin Gupta cmp x30, #EC_AARCH64_SMC 145ccd81f1eSAndre Przywara b.eq sync_handler64 146ccd81f1eSAndre Przywara 147ccd81f1eSAndre Przywara cmp x30, #EC_AARCH64_SYS 148ccd81f1eSAndre Przywara b.eq sync_handler64 149dce74b89SAchin Gupta 150df8f3188SJeenu Viswambharan /* Synchronous exceptions other than the above are assumed to be EA */ 1514d91838bSJulius Werner ldr x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR] 1526f7de9a8SManish Pandey b handle_lower_el_sync_ea 153dce74b89SAchin Gupta .endm 154dce74b89SAchin Gupta 155dce74b89SAchin Gupta 156a6ef4393SDouglas Raillard /* --------------------------------------------------------------------- 157a6ef4393SDouglas Raillard * This macro handles FIQ or IRQ interrupts i.e. EL3, S-EL1 and NS 158a6ef4393SDouglas Raillard * interrupts. 159a6ef4393SDouglas Raillard * --------------------------------------------------------------------- 160dce74b89SAchin Gupta */ 161dce74b89SAchin Gupta .macro handle_interrupt_exception label 1625283962eSAntonio Nino Diaz 163e290a8fcSAlexei Fedorov /* 164ed108b56SAlexei Fedorov * Save general purpose and ARMv8.3-PAuth registers (if enabled). 1651d6d6802SBoyan Karatotev * Also save PMCR_EL0 and set the PSTATE to a known state. 166e290a8fcSAlexei Fedorov */ 16797215e0fSDaniel Boulby bl prepare_el3_entry 168e290a8fcSAlexei Fedorov 169b86048c4SAntonio Nino Diaz#if ENABLE_PAUTH 170ed108b56SAlexei Fedorov /* Load and program APIAKey firmware key */ 171ed108b56SAlexei Fedorov bl pauth_load_bl31_apiakey 172b86048c4SAntonio Nino Diaz#endif 1735283962eSAntonio Nino Diaz 174a6ef4393SDouglas Raillard /* Save the EL3 system registers needed to return from this exception */ 1755717aae1SAchin Gupta mrs x0, spsr_el3 1765717aae1SAchin Gupta mrs x1, elr_el3 1775717aae1SAchin Gupta stp x0, x1, [sp, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3] 1785717aae1SAchin Gupta 179dce74b89SAchin Gupta /* Switch to the runtime stack i.e. SP_EL0 */ 180dce74b89SAchin Gupta ldr x2, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP] 181dce74b89SAchin Gupta mov x20, sp 182ed108b56SAlexei Fedorov msr spsel, #MODE_SP_EL0 183dce74b89SAchin Gupta mov sp, x2 184dce74b89SAchin Gupta 185dce74b89SAchin Gupta /* 186a6ef4393SDouglas Raillard * Find out whether this is a valid interrupt type. 187a6ef4393SDouglas Raillard * If the interrupt controller reports a spurious interrupt then return 188a6ef4393SDouglas Raillard * to where we came from. 189dce74b89SAchin Gupta */ 1909865ac15SDan Handley bl plat_ic_get_pending_interrupt_type 191dce74b89SAchin Gupta cmp x0, #INTR_TYPE_INVAL 192dce74b89SAchin Gupta b.eq interrupt_exit_\label 193dce74b89SAchin Gupta 194dce74b89SAchin Gupta /* 195a6ef4393SDouglas Raillard * Get the registered handler for this interrupt type. 196a6ef4393SDouglas Raillard * A NULL return value could be 'cause of the following conditions: 1975717aae1SAchin Gupta * 198a6ef4393SDouglas Raillard * a. An interrupt of a type was routed correctly but a handler for its 199a6ef4393SDouglas Raillard * type was not registered. 2005717aae1SAchin Gupta * 201a6ef4393SDouglas Raillard * b. An interrupt of a type was not routed correctly so a handler for 202a6ef4393SDouglas Raillard * its type was not registered. 2035717aae1SAchin Gupta * 204a6ef4393SDouglas Raillard * c. An interrupt of a type was routed correctly to EL3, but was 205a6ef4393SDouglas Raillard * deasserted before its pending state could be read. Another 206a6ef4393SDouglas Raillard * interrupt of a different type pended at the same time and its 207a6ef4393SDouglas Raillard * type was reported as pending instead. However, a handler for this 208a6ef4393SDouglas Raillard * type was not registered. 2095717aae1SAchin Gupta * 210a6ef4393SDouglas Raillard * a. and b. can only happen due to a programming error. The 211a6ef4393SDouglas Raillard * occurrence of c. could be beyond the control of Trusted Firmware. 212a6ef4393SDouglas Raillard * It makes sense to return from this exception instead of reporting an 213a6ef4393SDouglas Raillard * error. 214dce74b89SAchin Gupta */ 215dce74b89SAchin Gupta bl get_interrupt_type_handler 2165717aae1SAchin Gupta cbz x0, interrupt_exit_\label 217dce74b89SAchin Gupta mov x21, x0 218dce74b89SAchin Gupta 219dce74b89SAchin Gupta mov x0, #INTR_ID_UNAVAILABLE 220dce74b89SAchin Gupta 221dce74b89SAchin Gupta /* Set the current security state in the 'flags' parameter */ 222dce74b89SAchin Gupta mrs x2, scr_el3 223dce74b89SAchin Gupta ubfx x1, x2, #0, #1 224dce74b89SAchin Gupta 225dce74b89SAchin Gupta /* Restore the reference to the 'handle' i.e. SP_EL3 */ 226dce74b89SAchin Gupta mov x2, x20 227dce74b89SAchin Gupta 228b460b8bfSSoby Mathew /* x3 will point to a cookie (not used now) */ 229b460b8bfSSoby Mathew mov x3, xzr 230b460b8bfSSoby Mathew 231dce74b89SAchin Gupta /* Call the interrupt type handler */ 232dce74b89SAchin Gupta blr x21 233dce74b89SAchin Gupta 234dce74b89SAchin Guptainterrupt_exit_\label: 235dce74b89SAchin Gupta /* Return from exception, possibly in a different security state */ 236dce74b89SAchin Gupta b el3_exit 237dce74b89SAchin Gupta 238dce74b89SAchin Gupta .endm 239dce74b89SAchin Gupta 240dce74b89SAchin Gupta 241e0ae9fabSSandrine Bailleuxvector_base runtime_exceptions 242e0ae9fabSSandrine Bailleux 243a6ef4393SDouglas Raillard /* --------------------------------------------------------------------- 244a6ef4393SDouglas Raillard * Current EL with SP_EL0 : 0x0 - 0x200 245a6ef4393SDouglas Raillard * --------------------------------------------------------------------- 2464f6ad66aSAchin Gupta */ 247e0ae9fabSSandrine Bailleuxvector_entry sync_exception_sp_el0 2481f461979SJustin Chadwell#ifdef MONITOR_TRAPS 2491f461979SJustin Chadwell stp x29, x30, [sp, #-16]! 2501f461979SJustin Chadwell 2511f461979SJustin Chadwell mrs x30, esr_el3 2521f461979SJustin Chadwell ubfx x30, x30, #ESR_EC_SHIFT, #ESR_EC_LENGTH 2531f461979SJustin Chadwell 2541f461979SJustin Chadwell /* Check for BRK */ 2551f461979SJustin Chadwell cmp x30, #EC_BRK 2561f461979SJustin Chadwell b.eq brk_handler 2571f461979SJustin Chadwell 2581f461979SJustin Chadwell ldp x29, x30, [sp], #16 2591f461979SJustin Chadwell#endif /* MONITOR_TRAPS */ 2601f461979SJustin Chadwell 261a6ef4393SDouglas Raillard /* We don't expect any synchronous exceptions from EL3 */ 2624d91838bSJulius Werner b report_unhandled_exception 263a9203edaSRoberto Vargasend_vector_entry sync_exception_sp_el0 2644f6ad66aSAchin Gupta 265e0ae9fabSSandrine Bailleuxvector_entry irq_sp_el0 266a6ef4393SDouglas Raillard /* 267a6ef4393SDouglas Raillard * EL3 code is non-reentrant. Any asynchronous exception is a serious 268a6ef4393SDouglas Raillard * error. Loop infinitely. 269a6ef4393SDouglas Raillard */ 2704d91838bSJulius Werner b report_unhandled_interrupt 271a9203edaSRoberto Vargasend_vector_entry irq_sp_el0 2724f6ad66aSAchin Gupta 273e0ae9fabSSandrine Bailleux 274e0ae9fabSSandrine Bailleuxvector_entry fiq_sp_el0 2754d91838bSJulius Werner b report_unhandled_interrupt 276a9203edaSRoberto Vargasend_vector_entry fiq_sp_el0 2774f6ad66aSAchin Gupta 278e0ae9fabSSandrine Bailleux 279e0ae9fabSSandrine Bailleuxvector_entry serror_sp_el0 280eaeaa4d0SJeenu Viswambharan no_ret plat_handle_el3_ea 281a9203edaSRoberto Vargasend_vector_entry serror_sp_el0 2824f6ad66aSAchin Gupta 283a6ef4393SDouglas Raillard /* --------------------------------------------------------------------- 284a6ef4393SDouglas Raillard * Current EL with SP_ELx: 0x200 - 0x400 285a6ef4393SDouglas Raillard * --------------------------------------------------------------------- 2864f6ad66aSAchin Gupta */ 287e0ae9fabSSandrine Bailleuxvector_entry sync_exception_sp_elx 288a6ef4393SDouglas Raillard /* 289a6ef4393SDouglas Raillard * This exception will trigger if anything went wrong during a previous 290a6ef4393SDouglas Raillard * exception entry or exit or while handling an earlier unexpected 291a6ef4393SDouglas Raillard * synchronous exception. There is a high probability that SP_EL3 is 292a6ef4393SDouglas Raillard * corrupted. 293caa84939SJeenu Viswambharan */ 2944d91838bSJulius Werner b report_unhandled_exception 295a9203edaSRoberto Vargasend_vector_entry sync_exception_sp_elx 2964f6ad66aSAchin Gupta 297e0ae9fabSSandrine Bailleuxvector_entry irq_sp_elx 2984d91838bSJulius Werner b report_unhandled_interrupt 299a9203edaSRoberto Vargasend_vector_entry irq_sp_elx 300a7934d69SJeenu Viswambharan 301e0ae9fabSSandrine Bailleuxvector_entry fiq_sp_elx 3024d91838bSJulius Werner b report_unhandled_interrupt 303a9203edaSRoberto Vargasend_vector_entry fiq_sp_elx 304a7934d69SJeenu Viswambharan 305e0ae9fabSSandrine Bailleuxvector_entry serror_sp_elx 3069202d519SManish Pandey#if !RAS_FFH_SUPPORT 30776a91d87SManish Pandey /* 30876a91d87SManish Pandey * This will trigger if the exception was taken due to SError in EL3 or 30976a91d87SManish Pandey * because of pending asynchronous external aborts from lower EL that got 31076a91d87SManish Pandey * triggered due to explicit synchronization in EL3. Refer Note 1. 31176a91d87SManish Pandey */ 31276a91d87SManish Pandey /* Assumes SP_EL3 on entry */ 313d87c0e27SManish Pandey save_x30 31476a91d87SManish Pandey ldr x30, [sp, #CTX_EL3STATE_OFFSET + CTX_IS_IN_EL3] 31576a91d87SManish Pandey cbnz x30, 1f 31676a91d87SManish Pandey 31776a91d87SManish Pandey /* Handle asynchronous external abort from lower EL */ 31876a91d87SManish Pandey b handle_lower_el_async_ea 31976a91d87SManish Pandey1: 320c2d32a5fSMadhukar Pappireddy#endif 321eaeaa4d0SJeenu Viswambharan no_ret plat_handle_el3_ea 322a9203edaSRoberto Vargasend_vector_entry serror_sp_elx 3234f6ad66aSAchin Gupta 324a6ef4393SDouglas Raillard /* --------------------------------------------------------------------- 32544804252SSandrine Bailleux * Lower EL using AArch64 : 0x400 - 0x600 326a6ef4393SDouglas Raillard * --------------------------------------------------------------------- 3274f6ad66aSAchin Gupta */ 328e0ae9fabSSandrine Bailleuxvector_entry sync_exception_aarch64 329a6ef4393SDouglas Raillard /* 330a6ef4393SDouglas Raillard * This exception vector will be the entry point for SMCs and traps 331a6ef4393SDouglas Raillard * that are unhandled at lower ELs most commonly. SP_EL3 should point 332a6ef4393SDouglas Raillard * to a valid cpu context where the general purpose and system register 333a6ef4393SDouglas Raillard * state can be saved. 334caa84939SJeenu Viswambharan */ 335d87c0e27SManish Pandey save_x30 3363b8456bdSManish V Badarkhe apply_at_speculative_wa 33714c6016aSJeenu Viswambharan check_and_unmask_ea 338caa84939SJeenu Viswambharan handle_sync_exception 339a9203edaSRoberto Vargasend_vector_entry sync_exception_aarch64 3404f6ad66aSAchin Gupta 341e0ae9fabSSandrine Bailleuxvector_entry irq_aarch64 342d87c0e27SManish Pandey save_x30 3433b8456bdSManish V Badarkhe apply_at_speculative_wa 34414c6016aSJeenu Viswambharan check_and_unmask_ea 345dce74b89SAchin Gupta handle_interrupt_exception irq_aarch64 346a9203edaSRoberto Vargasend_vector_entry irq_aarch64 3474f6ad66aSAchin Gupta 348e0ae9fabSSandrine Bailleuxvector_entry fiq_aarch64 349d87c0e27SManish Pandey save_x30 3503b8456bdSManish V Badarkhe apply_at_speculative_wa 35114c6016aSJeenu Viswambharan check_and_unmask_ea 352dce74b89SAchin Gupta handle_interrupt_exception fiq_aarch64 353a9203edaSRoberto Vargasend_vector_entry fiq_aarch64 3544f6ad66aSAchin Gupta 355e0ae9fabSSandrine Bailleuxvector_entry serror_aarch64 356d87c0e27SManish Pandey save_x30 3573b8456bdSManish V Badarkhe apply_at_speculative_wa 3589202d519SManish Pandey#if RAS_FFH_SUPPORT 35976454abfSJeenu Viswambharan msr daifclr, #DAIF_ABT_BIT 360c2d32a5fSMadhukar Pappireddy#else 36176a91d87SManish Pandey check_and_unmask_ea 362c2d32a5fSMadhukar Pappireddy#endif 3636f7de9a8SManish Pandey b handle_lower_el_async_ea 3646f7de9a8SManish Pandey 365a9203edaSRoberto Vargasend_vector_entry serror_aarch64 3664f6ad66aSAchin Gupta 367a6ef4393SDouglas Raillard /* --------------------------------------------------------------------- 36844804252SSandrine Bailleux * Lower EL using AArch32 : 0x600 - 0x800 369a6ef4393SDouglas Raillard * --------------------------------------------------------------------- 3704f6ad66aSAchin Gupta */ 371e0ae9fabSSandrine Bailleuxvector_entry sync_exception_aarch32 372a6ef4393SDouglas Raillard /* 373a6ef4393SDouglas Raillard * This exception vector will be the entry point for SMCs and traps 374a6ef4393SDouglas Raillard * that are unhandled at lower ELs most commonly. SP_EL3 should point 375a6ef4393SDouglas Raillard * to a valid cpu context where the general purpose and system register 376a6ef4393SDouglas Raillard * state can be saved. 377caa84939SJeenu Viswambharan */ 378d87c0e27SManish Pandey save_x30 3793b8456bdSManish V Badarkhe apply_at_speculative_wa 38014c6016aSJeenu Viswambharan check_and_unmask_ea 381caa84939SJeenu Viswambharan handle_sync_exception 382a9203edaSRoberto Vargasend_vector_entry sync_exception_aarch32 3834f6ad66aSAchin Gupta 384e0ae9fabSSandrine Bailleuxvector_entry irq_aarch32 385d87c0e27SManish Pandey save_x30 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 392d87c0e27SManish Pandey save_x30 3933b8456bdSManish V Badarkhe apply_at_speculative_wa 39414c6016aSJeenu Viswambharan check_and_unmask_ea 395dce74b89SAchin Gupta handle_interrupt_exception fiq_aarch32 396a9203edaSRoberto Vargasend_vector_entry fiq_aarch32 3974f6ad66aSAchin Gupta 398e0ae9fabSSandrine Bailleuxvector_entry serror_aarch32 399d87c0e27SManish Pandey save_x30 4003b8456bdSManish V Badarkhe apply_at_speculative_wa 4019202d519SManish Pandey#if RAS_FFH_SUPPORT 40276454abfSJeenu Viswambharan msr daifclr, #DAIF_ABT_BIT 403c2d32a5fSMadhukar Pappireddy#else 40476a91d87SManish Pandey check_and_unmask_ea 405c2d32a5fSMadhukar Pappireddy#endif 4066f7de9a8SManish Pandey b handle_lower_el_async_ea 4076f7de9a8SManish Pandey 408a9203edaSRoberto Vargasend_vector_entry serror_aarch32 409a7934d69SJeenu Viswambharan 4101f461979SJustin Chadwell#ifdef MONITOR_TRAPS 4111f461979SJustin Chadwell .section .rodata.brk_string, "aS" 4121f461979SJustin Chadwellbrk_location: 4131f461979SJustin Chadwell .asciz "Error at instruction 0x" 4141f461979SJustin Chadwellbrk_message: 4151f461979SJustin Chadwell .asciz "Unexpected BRK instruction with value 0x" 4161f461979SJustin Chadwell#endif /* MONITOR_TRAPS */ 4171f461979SJustin Chadwell 4182f370465SAntonio Nino Diaz /* --------------------------------------------------------------------- 419caa84939SJeenu Viswambharan * The following code handles secure monitor calls. 420a6ef4393SDouglas Raillard * Depending upon the execution state from where the SMC has been 421a6ef4393SDouglas Raillard * invoked, it frees some general purpose registers to perform the 422a6ef4393SDouglas Raillard * remaining tasks. They involve finding the runtime service handler 423a6ef4393SDouglas Raillard * that is the target of the SMC & switching to runtime stacks (SP_EL0) 424a6ef4393SDouglas Raillard * before calling the handler. 425caa84939SJeenu Viswambharan * 426a6ef4393SDouglas Raillard * Note that x30 has been explicitly saved and can be used here 427a6ef4393SDouglas Raillard * --------------------------------------------------------------------- 428caa84939SJeenu Viswambharan */ 429ccd81f1eSAndre Przywarafunc sync_exception_handler 430caa84939SJeenu Viswambharansmc_handler32: 431caa84939SJeenu Viswambharan /* Check whether aarch32 issued an SMC64 */ 432caa84939SJeenu Viswambharan tbnz x0, #FUNCID_CC_SHIFT, smc_prohibited 433caa84939SJeenu Viswambharan 434ccd81f1eSAndre Przywarasync_handler64: 4355283962eSAntonio Nino Diaz /* NOTE: The code below must preserve x0-x4 */ 4365283962eSAntonio Nino Diaz 437e290a8fcSAlexei Fedorov /* 438ed108b56SAlexei Fedorov * Save general purpose and ARMv8.3-PAuth registers (if enabled). 4391d6d6802SBoyan Karatotev * Also save PMCR_EL0 and 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 503f8a35797SJayanth Dodderi Chidanand mov sp, x12 504f8a35797SJayanth Dodderi Chidanand 505f8a35797SJayanth Dodderi Chidanand /* 506f8a35797SJayanth Dodderi Chidanand * Per SMCCC documentation, bits [23:17] must be zero for Fast 507f8a35797SJayanth Dodderi Chidanand * SMCs. Other values are reserved for future use. Ensure that 508f8a35797SJayanth Dodderi Chidanand * these bits are zeroes, if not report as unknown SMC. 509f8a35797SJayanth Dodderi Chidanand */ 510f8a35797SJayanth Dodderi Chidanand tbz x0, #FUNCID_TYPE_SHIFT, 2f /* Skip check if its a Yield Call*/ 511f8a35797SJayanth Dodderi Chidanand tst x0, #(FUNCID_FC_RESERVED_MASK << FUNCID_FC_RESERVED_SHIFT) 512f8a35797SJayanth Dodderi Chidanand b.ne smc_unknown 513f8a35797SJayanth Dodderi Chidanand 5140fe7b9f2SOlivier Deprez /* 5150fe7b9f2SOlivier Deprez * Per SMCCCv1.3 a caller can set the SVE hint bit in the SMC FID 5160fe7b9f2SOlivier Deprez * passed through x0. Copy the SVE hint bit to flags and mask the 5170fe7b9f2SOlivier Deprez * bit in smc_fid passed to the standard service dispatcher. 5180fe7b9f2SOlivier Deprez * A service/dispatcher can retrieve the SVE hint bit state from 5190fe7b9f2SOlivier Deprez * flags using the appropriate helper. 5200fe7b9f2SOlivier Deprez */ 521f8a35797SJayanth Dodderi Chidanand2: 522*b2d85178SOlivier Deprez and x16, x0, #(FUNCID_SVE_HINT_MASK << FUNCID_SVE_HINT_SHIFT) 523*b2d85178SOlivier Deprez orr x7, x7, x16 5240fe7b9f2SOlivier Deprez bic x0, x0, #(FUNCID_SVE_HINT_MASK << FUNCID_SVE_HINT_SHIFT) 5250fe7b9f2SOlivier Deprez 526cc485e27SMadhukar Pappireddy /* Get the unique owning entity number */ 527cc485e27SMadhukar Pappireddy ubfx x16, x0, #FUNCID_OEN_SHIFT, #FUNCID_OEN_WIDTH 528cc485e27SMadhukar Pappireddy ubfx x15, x0, #FUNCID_TYPE_SHIFT, #FUNCID_TYPE_WIDTH 529cc485e27SMadhukar Pappireddy orr x16, x16, x15, lsl #FUNCID_OEN_WIDTH 530cc485e27SMadhukar Pappireddy 531cc485e27SMadhukar Pappireddy /* Load descriptor index from array of indices */ 532c367b75eSMadhukar Pappireddy adrp x14, rt_svc_descs_indices 533c367b75eSMadhukar Pappireddy add x14, x14, :lo12:rt_svc_descs_indices 534cc485e27SMadhukar Pappireddy ldrb w15, [x14, x16] 535cc485e27SMadhukar Pappireddy 536cc485e27SMadhukar Pappireddy /* Any index greater than 127 is invalid. Check bit 7. */ 537cc485e27SMadhukar Pappireddy tbnz w15, 7, smc_unknown 538cc485e27SMadhukar Pappireddy 539cc485e27SMadhukar Pappireddy /* 540cc485e27SMadhukar Pappireddy * Get the descriptor using the index 541cc485e27SMadhukar Pappireddy * x11 = (base + off), w15 = index 542cc485e27SMadhukar Pappireddy * 543cc485e27SMadhukar Pappireddy * handler = (base + off) + (index << log2(size)) 544cc485e27SMadhukar Pappireddy */ 545cc485e27SMadhukar Pappireddy adr x11, (__RT_SVC_DESCS_START__ + RT_SVC_DESC_HANDLE) 546cc485e27SMadhukar Pappireddy lsl w10, w15, #RT_SVC_SIZE_LOG2 547cc485e27SMadhukar Pappireddy ldr x15, [x11, w10, uxtw] 548cc485e27SMadhukar Pappireddy 549a6ef4393SDouglas Raillard /* 550a6ef4393SDouglas Raillard * Call the Secure Monitor Call handler and then drop directly into 551a6ef4393SDouglas Raillard * el3_exit() which will program any remaining architectural state 552a6ef4393SDouglas Raillard * prior to issuing the ERET to the desired lower EL. 553caa84939SJeenu Viswambharan */ 554caa84939SJeenu Viswambharan#if DEBUG 555caa84939SJeenu Viswambharan cbz x15, rt_svc_fw_critical_error 556caa84939SJeenu Viswambharan#endif 557caa84939SJeenu Viswambharan blr x15 558caa84939SJeenu Viswambharan 559bbf8f6f9SYatharth Kochar b el3_exit 5604f6ad66aSAchin Gupta 561ccd81f1eSAndre Przywarasysreg_handler64: 562ccd81f1eSAndre Przywara mov x0, x16 /* ESR_EL3, containing syndrome information */ 563ccd81f1eSAndre Przywara mov x1, x6 /* lower EL's context */ 564ccd81f1eSAndre Przywara mov x19, x6 /* save context pointer for after the call */ 565ccd81f1eSAndre Przywara mov sp, x12 /* EL3 runtime stack, as loaded above */ 566ccd81f1eSAndre Przywara 567ccd81f1eSAndre Przywara /* int handle_sysreg_trap(uint64_t esr_el3, cpu_context_t *ctx); */ 568ccd81f1eSAndre Przywara bl handle_sysreg_trap 569ccd81f1eSAndre Przywara /* 570ccd81f1eSAndre Przywara * returns: 571ccd81f1eSAndre Przywara * -1: unhandled trap, panic 572ccd81f1eSAndre Przywara * 0: handled trap, return to the trapping instruction (repeating it) 573ccd81f1eSAndre Przywara * 1: handled trap, return to the next instruction 574ccd81f1eSAndre Przywara */ 575ccd81f1eSAndre Przywara 576ccd81f1eSAndre Przywara tst w0, w0 57717d07a55SGovindraj Raja b.mi elx_panic /* negative return value: panic */ 578ccd81f1eSAndre Przywara b.eq 1f /* zero: do not change ELR_EL3 */ 579ccd81f1eSAndre Przywara 580ccd81f1eSAndre Przywara /* advance the PC to continue after the instruction */ 581ccd81f1eSAndre Przywara ldr x1, [x19, #CTX_EL3STATE_OFFSET + CTX_ELR_EL3] 582ccd81f1eSAndre Przywara add x1, x1, #4 583ccd81f1eSAndre Przywara str x1, [x19, #CTX_EL3STATE_OFFSET + CTX_ELR_EL3] 584ccd81f1eSAndre Przywara1: 585ccd81f1eSAndre Przywara b el3_exit 586ccd81f1eSAndre Przywara 587caa84939SJeenu Viswambharansmc_unknown: 588caa84939SJeenu Viswambharan /* 589cc485e27SMadhukar Pappireddy * Unknown SMC call. Populate return value with SMC_UNK and call 590cc485e27SMadhukar Pappireddy * el3_exit() which will restore the remaining architectural state 591cc485e27SMadhukar Pappireddy * i.e., SYS, GP and PAuth registers(if any) prior to issuing the ERET 592cc485e27SMadhukar Pappireddy * to the desired lower EL. 593caa84939SJeenu Viswambharan */ 5944abd7fa7SAntonio Nino Diaz mov x0, #SMC_UNK 595cc485e27SMadhukar Pappireddy str x0, [x6, #CTX_GPREGS_OFFSET + CTX_GPREG_X0] 596cc485e27SMadhukar Pappireddy b el3_exit 597caa84939SJeenu Viswambharan 598caa84939SJeenu Viswambharansmc_prohibited: 5993b8456bdSManish V Badarkhe restore_ptw_el1_sys_regs 6003b8456bdSManish V Badarkhe ldp x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28] 601c3260f9bSSoby Mathew ldr x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR] 6024abd7fa7SAntonio Nino Diaz mov x0, #SMC_UNK 603f461fe34SAnthony Steinhauser exception_return 604caa84939SJeenu Viswambharan 605ed108b56SAlexei Fedorov#if DEBUG 606caa84939SJeenu Viswambharanrt_svc_fw_critical_error: 607a6ef4393SDouglas Raillard /* Switch to SP_ELx */ 608ed108b56SAlexei Fedorov msr spsel, #MODE_SP_ELX 609a806dad5SJeenu Viswambharan no_ret report_unhandled_exception 610ed108b56SAlexei Fedorov#endif 611ccd81f1eSAndre Przywaraendfunc sync_exception_handler 6121f461979SJustin Chadwell 6131f461979SJustin Chadwell /* --------------------------------------------------------------------- 6141f461979SJustin Chadwell * The following code handles exceptions caused by BRK instructions. 6151f461979SJustin Chadwell * Following a BRK instruction, the only real valid cause of action is 6161f461979SJustin Chadwell * to print some information and panic, as the code that caused it is 6171f461979SJustin Chadwell * likely in an inconsistent internal state. 6181f461979SJustin Chadwell * 6191f461979SJustin Chadwell * This is initially intended to be used in conjunction with 6201f461979SJustin Chadwell * __builtin_trap. 6211f461979SJustin Chadwell * --------------------------------------------------------------------- 6221f461979SJustin Chadwell */ 6231f461979SJustin Chadwell#ifdef MONITOR_TRAPS 6241f461979SJustin Chadwellfunc brk_handler 6251f461979SJustin Chadwell /* Extract the ISS */ 6261f461979SJustin Chadwell mrs x10, esr_el3 6271f461979SJustin Chadwell ubfx x10, x10, #ESR_ISS_SHIFT, #ESR_ISS_LENGTH 6281f461979SJustin Chadwell 6291f461979SJustin Chadwell /* Ensure the console is initialized */ 6301f461979SJustin Chadwell bl plat_crash_console_init 6311f461979SJustin Chadwell 6321f461979SJustin Chadwell adr x4, brk_location 6331f461979SJustin Chadwell bl asm_print_str 6341f461979SJustin Chadwell mrs x4, elr_el3 6351f461979SJustin Chadwell bl asm_print_hex 6361f461979SJustin Chadwell bl asm_print_newline 6371f461979SJustin Chadwell 6381f461979SJustin Chadwell adr x4, brk_message 6391f461979SJustin Chadwell bl asm_print_str 6401f461979SJustin Chadwell mov x4, x10 6411f461979SJustin Chadwell mov x5, #28 6421f461979SJustin Chadwell bl asm_print_hex_bits 6431f461979SJustin Chadwell bl asm_print_newline 6441f461979SJustin Chadwell 6451f461979SJustin Chadwell no_ret plat_panic_handler 6461f461979SJustin Chadwellendfunc brk_handler 6471f461979SJustin Chadwell#endif /* MONITOR_TRAPS */ 648