14f6ad66aSAchin Gupta/* 2e0ae9fabSSandrine Bailleux * Copyright (c) 2013-2016, ARM Limited and Contributors. All rights reserved. 34f6ad66aSAchin Gupta * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 54f6ad66aSAchin Gupta */ 64f6ad66aSAchin Gupta 74f6ad66aSAchin Gupta#include <arch.h> 835e98e55SDan Handley#include <asm_macros.S> 997043ac9SDan Handley#include <context.h> 10872be88aSdp-arm#include <cpu_data.h> 11dce74b89SAchin Gupta#include <interrupt_mgmt.h> 125f0cdb05SDan Handley#include <platform_def.h> 1397043ac9SDan Handley#include <runtime_svc.h> 144f6ad66aSAchin Gupta 154f6ad66aSAchin Gupta .globl runtime_exceptions 164f6ad66aSAchin Gupta 17*f62ad322SDimitris Papastamos .globl sync_exception_sp_el0 18*f62ad322SDimitris Papastamos .globl irq_sp_el0 19*f62ad322SDimitris Papastamos .globl fiq_sp_el0 20*f62ad322SDimitris Papastamos .globl serror_sp_el0 21*f62ad322SDimitris Papastamos 22*f62ad322SDimitris Papastamos .globl sync_exception_sp_elx 23*f62ad322SDimitris Papastamos .globl irq_sp_elx 24*f62ad322SDimitris Papastamos .globl fiq_sp_elx 25*f62ad322SDimitris Papastamos .globl serror_sp_elx 26*f62ad322SDimitris Papastamos 27*f62ad322SDimitris Papastamos .globl sync_exception_aarch64 28*f62ad322SDimitris Papastamos .globl irq_aarch64 29*f62ad322SDimitris Papastamos .globl fiq_aarch64 30*f62ad322SDimitris Papastamos .globl serror_aarch64 31*f62ad322SDimitris Papastamos 32*f62ad322SDimitris Papastamos .globl sync_exception_aarch32 33*f62ad322SDimitris Papastamos .globl irq_aarch32 34*f62ad322SDimitris Papastamos .globl fiq_aarch32 35*f62ad322SDimitris Papastamos .globl serror_aarch32 36*f62ad322SDimitris Papastamos 37a6ef4393SDouglas Raillard /* --------------------------------------------------------------------- 38a6ef4393SDouglas Raillard * This macro handles Synchronous exceptions. 39a6ef4393SDouglas Raillard * Only SMC exceptions are supported. 40a6ef4393SDouglas Raillard * --------------------------------------------------------------------- 41dce74b89SAchin Gupta */ 42dce74b89SAchin Gupta .macro handle_sync_exception 430c8d4fefSAchin Gupta /* Enable the SError interrupt */ 440c8d4fefSAchin Gupta msr daifclr, #DAIF_ABT_BIT 450c8d4fefSAchin Gupta 46dce74b89SAchin Gupta str x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR] 47872be88aSdp-arm 48872be88aSdp-arm#if ENABLE_RUNTIME_INSTRUMENTATION 49872be88aSdp-arm /* 50a6ef4393SDouglas Raillard * Read the timestamp value and store it in per-cpu data. The value 51a6ef4393SDouglas Raillard * will be extracted from per-cpu data by the C level SMC handler and 52a6ef4393SDouglas Raillard * saved to the PMF timestamp region. 53872be88aSdp-arm */ 54872be88aSdp-arm mrs x30, cntpct_el0 55872be88aSdp-arm str x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29] 56872be88aSdp-arm mrs x29, tpidr_el3 57872be88aSdp-arm str x30, [x29, #CPU_DATA_PMF_TS0_OFFSET] 58872be88aSdp-arm ldr x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29] 59872be88aSdp-arm#endif 60872be88aSdp-arm 61dce74b89SAchin Gupta mrs x30, esr_el3 62dce74b89SAchin Gupta ubfx x30, x30, #ESR_EC_SHIFT, #ESR_EC_LENGTH 63dce74b89SAchin Gupta 64a6ef4393SDouglas Raillard /* Handle SMC exceptions separately from other synchronous exceptions */ 65dce74b89SAchin Gupta cmp x30, #EC_AARCH32_SMC 66dce74b89SAchin Gupta b.eq smc_handler32 67dce74b89SAchin Gupta 68dce74b89SAchin Gupta cmp x30, #EC_AARCH64_SMC 69dce74b89SAchin Gupta b.eq smc_handler64 70dce74b89SAchin Gupta 71a6ef4393SDouglas Raillard /* Other kinds of synchronous exceptions are not handled */ 724d91838bSJulius Werner ldr x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR] 734d91838bSJulius Werner b report_unhandled_exception 74dce74b89SAchin Gupta .endm 75dce74b89SAchin Gupta 76dce74b89SAchin Gupta 77a6ef4393SDouglas Raillard /* --------------------------------------------------------------------- 78a6ef4393SDouglas Raillard * This macro handles FIQ or IRQ interrupts i.e. EL3, S-EL1 and NS 79a6ef4393SDouglas Raillard * interrupts. 80a6ef4393SDouglas Raillard * --------------------------------------------------------------------- 81dce74b89SAchin Gupta */ 82dce74b89SAchin Gupta .macro handle_interrupt_exception label 830c8d4fefSAchin Gupta /* Enable the SError interrupt */ 840c8d4fefSAchin Gupta msr daifclr, #DAIF_ABT_BIT 850c8d4fefSAchin Gupta 86dce74b89SAchin Gupta str x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR] 87dce74b89SAchin Gupta bl save_gp_registers 88dce74b89SAchin Gupta 89a6ef4393SDouglas Raillard /* Save the EL3 system registers needed to return from this exception */ 905717aae1SAchin Gupta mrs x0, spsr_el3 915717aae1SAchin Gupta mrs x1, elr_el3 925717aae1SAchin Gupta stp x0, x1, [sp, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3] 935717aae1SAchin Gupta 94dce74b89SAchin Gupta /* Switch to the runtime stack i.e. SP_EL0 */ 95dce74b89SAchin Gupta ldr x2, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP] 96dce74b89SAchin Gupta mov x20, sp 97dce74b89SAchin Gupta msr spsel, #0 98dce74b89SAchin Gupta mov sp, x2 99dce74b89SAchin Gupta 100dce74b89SAchin Gupta /* 101a6ef4393SDouglas Raillard * Find out whether this is a valid interrupt type. 102a6ef4393SDouglas Raillard * If the interrupt controller reports a spurious interrupt then return 103a6ef4393SDouglas Raillard * to where we came from. 104dce74b89SAchin Gupta */ 1059865ac15SDan Handley bl plat_ic_get_pending_interrupt_type 106dce74b89SAchin Gupta cmp x0, #INTR_TYPE_INVAL 107dce74b89SAchin Gupta b.eq interrupt_exit_\label 108dce74b89SAchin Gupta 109dce74b89SAchin Gupta /* 110a6ef4393SDouglas Raillard * Get the registered handler for this interrupt type. 111a6ef4393SDouglas Raillard * A NULL return value could be 'cause of the following conditions: 1125717aae1SAchin Gupta * 113a6ef4393SDouglas Raillard * a. An interrupt of a type was routed correctly but a handler for its 114a6ef4393SDouglas Raillard * type was not registered. 1155717aae1SAchin Gupta * 116a6ef4393SDouglas Raillard * b. An interrupt of a type was not routed correctly so a handler for 117a6ef4393SDouglas Raillard * its type was not registered. 1185717aae1SAchin Gupta * 119a6ef4393SDouglas Raillard * c. An interrupt of a type was routed correctly to EL3, but was 120a6ef4393SDouglas Raillard * deasserted before its pending state could be read. Another 121a6ef4393SDouglas Raillard * interrupt of a different type pended at the same time and its 122a6ef4393SDouglas Raillard * type was reported as pending instead. However, a handler for this 123a6ef4393SDouglas Raillard * type was not registered. 1245717aae1SAchin Gupta * 125a6ef4393SDouglas Raillard * a. and b. can only happen due to a programming error. The 126a6ef4393SDouglas Raillard * occurrence of c. could be beyond the control of Trusted Firmware. 127a6ef4393SDouglas Raillard * It makes sense to return from this exception instead of reporting an 128a6ef4393SDouglas Raillard * error. 129dce74b89SAchin Gupta */ 130dce74b89SAchin Gupta bl get_interrupt_type_handler 1315717aae1SAchin Gupta cbz x0, interrupt_exit_\label 132dce74b89SAchin Gupta mov x21, x0 133dce74b89SAchin Gupta 134dce74b89SAchin Gupta mov x0, #INTR_ID_UNAVAILABLE 135dce74b89SAchin Gupta 136dce74b89SAchin Gupta /* Set the current security state in the 'flags' parameter */ 137dce74b89SAchin Gupta mrs x2, scr_el3 138dce74b89SAchin Gupta ubfx x1, x2, #0, #1 139dce74b89SAchin Gupta 140dce74b89SAchin Gupta /* Restore the reference to the 'handle' i.e. SP_EL3 */ 141dce74b89SAchin Gupta mov x2, x20 142dce74b89SAchin Gupta 143b460b8bfSSoby Mathew /* x3 will point to a cookie (not used now) */ 144b460b8bfSSoby Mathew mov x3, xzr 145b460b8bfSSoby Mathew 146dce74b89SAchin Gupta /* Call the interrupt type handler */ 147dce74b89SAchin Gupta blr x21 148dce74b89SAchin Gupta 149dce74b89SAchin Guptainterrupt_exit_\label: 150dce74b89SAchin Gupta /* Return from exception, possibly in a different security state */ 151dce74b89SAchin Gupta b el3_exit 152dce74b89SAchin Gupta 153dce74b89SAchin Gupta .endm 154dce74b89SAchin Gupta 155dce74b89SAchin Gupta 156c3260f9bSSoby Mathew .macro save_x18_to_x29_sp_el0 157c3260f9bSSoby Mathew stp x18, x19, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X18] 158c3260f9bSSoby Mathew stp x20, x21, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X20] 159c3260f9bSSoby Mathew stp x22, x23, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X22] 160c3260f9bSSoby Mathew stp x24, x25, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X24] 161c3260f9bSSoby Mathew stp x26, x27, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X26] 162c3260f9bSSoby Mathew stp x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28] 163c3260f9bSSoby Mathew mrs x18, sp_el0 164c3260f9bSSoby Mathew str x18, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_SP_EL0] 165c3260f9bSSoby Mathew .endm 166c3260f9bSSoby Mathew 167e0ae9fabSSandrine Bailleux 168e0ae9fabSSandrine Bailleuxvector_base runtime_exceptions 169e0ae9fabSSandrine Bailleux 170a6ef4393SDouglas Raillard /* --------------------------------------------------------------------- 171a6ef4393SDouglas Raillard * Current EL with SP_EL0 : 0x0 - 0x200 172a6ef4393SDouglas Raillard * --------------------------------------------------------------------- 1734f6ad66aSAchin Gupta */ 174e0ae9fabSSandrine Bailleuxvector_entry sync_exception_sp_el0 175a6ef4393SDouglas Raillard /* We don't expect any synchronous exceptions from EL3 */ 1764d91838bSJulius Werner b report_unhandled_exception 177a7934d69SJeenu Viswambharan check_vector_size sync_exception_sp_el0 1784f6ad66aSAchin Gupta 179e0ae9fabSSandrine Bailleuxvector_entry irq_sp_el0 180a6ef4393SDouglas Raillard /* 181a6ef4393SDouglas Raillard * EL3 code is non-reentrant. Any asynchronous exception is a serious 182a6ef4393SDouglas Raillard * error. Loop infinitely. 183a6ef4393SDouglas Raillard */ 1844d91838bSJulius Werner b report_unhandled_interrupt 185a7934d69SJeenu Viswambharan check_vector_size irq_sp_el0 1864f6ad66aSAchin Gupta 187e0ae9fabSSandrine Bailleux 188e0ae9fabSSandrine Bailleuxvector_entry fiq_sp_el0 1894d91838bSJulius Werner b report_unhandled_interrupt 190a7934d69SJeenu Viswambharan check_vector_size fiq_sp_el0 1914f6ad66aSAchin Gupta 192e0ae9fabSSandrine Bailleux 193e0ae9fabSSandrine Bailleuxvector_entry serror_sp_el0 1944d91838bSJulius Werner b report_unhandled_exception 195a7934d69SJeenu Viswambharan check_vector_size serror_sp_el0 1964f6ad66aSAchin Gupta 197a6ef4393SDouglas Raillard /* --------------------------------------------------------------------- 198a6ef4393SDouglas Raillard * Current EL with SP_ELx: 0x200 - 0x400 199a6ef4393SDouglas Raillard * --------------------------------------------------------------------- 2004f6ad66aSAchin Gupta */ 201e0ae9fabSSandrine Bailleuxvector_entry sync_exception_sp_elx 202a6ef4393SDouglas Raillard /* 203a6ef4393SDouglas Raillard * This exception will trigger if anything went wrong during a previous 204a6ef4393SDouglas Raillard * exception entry or exit or while handling an earlier unexpected 205a6ef4393SDouglas Raillard * synchronous exception. There is a high probability that SP_EL3 is 206a6ef4393SDouglas Raillard * corrupted. 207caa84939SJeenu Viswambharan */ 2084d91838bSJulius Werner b report_unhandled_exception 209a7934d69SJeenu Viswambharan check_vector_size sync_exception_sp_elx 2104f6ad66aSAchin Gupta 211e0ae9fabSSandrine Bailleuxvector_entry irq_sp_elx 2124d91838bSJulius Werner b report_unhandled_interrupt 213a7934d69SJeenu Viswambharan check_vector_size irq_sp_elx 214a7934d69SJeenu Viswambharan 215e0ae9fabSSandrine Bailleuxvector_entry fiq_sp_elx 2164d91838bSJulius Werner b report_unhandled_interrupt 217a7934d69SJeenu Viswambharan check_vector_size fiq_sp_elx 218a7934d69SJeenu Viswambharan 219e0ae9fabSSandrine Bailleuxvector_entry serror_sp_elx 2204d91838bSJulius Werner b report_unhandled_exception 221a7934d69SJeenu Viswambharan check_vector_size serror_sp_elx 2224f6ad66aSAchin Gupta 223a6ef4393SDouglas Raillard /* --------------------------------------------------------------------- 22444804252SSandrine Bailleux * Lower EL using AArch64 : 0x400 - 0x600 225a6ef4393SDouglas Raillard * --------------------------------------------------------------------- 2264f6ad66aSAchin Gupta */ 227e0ae9fabSSandrine Bailleuxvector_entry sync_exception_aarch64 228a6ef4393SDouglas Raillard /* 229a6ef4393SDouglas Raillard * This exception vector will be the entry point for SMCs and traps 230a6ef4393SDouglas Raillard * that are unhandled at lower ELs most commonly. SP_EL3 should point 231a6ef4393SDouglas Raillard * to a valid cpu context where the general purpose and system register 232a6ef4393SDouglas Raillard * state can be saved. 233caa84939SJeenu Viswambharan */ 234caa84939SJeenu Viswambharan handle_sync_exception 235a7934d69SJeenu Viswambharan check_vector_size sync_exception_aarch64 2364f6ad66aSAchin Gupta 237e0ae9fabSSandrine Bailleuxvector_entry irq_aarch64 238dce74b89SAchin Gupta handle_interrupt_exception irq_aarch64 239a7934d69SJeenu Viswambharan check_vector_size irq_aarch64 2404f6ad66aSAchin Gupta 241e0ae9fabSSandrine Bailleuxvector_entry fiq_aarch64 242dce74b89SAchin Gupta handle_interrupt_exception fiq_aarch64 243a7934d69SJeenu Viswambharan check_vector_size fiq_aarch64 2444f6ad66aSAchin Gupta 245e0ae9fabSSandrine Bailleuxvector_entry serror_aarch64 246a6ef4393SDouglas Raillard /* 247a6ef4393SDouglas Raillard * SError exceptions from lower ELs are not currently supported. 248a6ef4393SDouglas Raillard * Report their occurrence. 249a6ef4393SDouglas Raillard */ 2504d91838bSJulius Werner b report_unhandled_exception 251a7934d69SJeenu Viswambharan check_vector_size serror_aarch64 2524f6ad66aSAchin Gupta 253a6ef4393SDouglas Raillard /* --------------------------------------------------------------------- 25444804252SSandrine Bailleux * Lower EL using AArch32 : 0x600 - 0x800 255a6ef4393SDouglas Raillard * --------------------------------------------------------------------- 2564f6ad66aSAchin Gupta */ 257e0ae9fabSSandrine Bailleuxvector_entry sync_exception_aarch32 258a6ef4393SDouglas Raillard /* 259a6ef4393SDouglas Raillard * This exception vector will be the entry point for SMCs and traps 260a6ef4393SDouglas Raillard * that are unhandled at lower ELs most commonly. SP_EL3 should point 261a6ef4393SDouglas Raillard * to a valid cpu context where the general purpose and system register 262a6ef4393SDouglas Raillard * state can be saved. 263caa84939SJeenu Viswambharan */ 264caa84939SJeenu Viswambharan handle_sync_exception 265a7934d69SJeenu Viswambharan check_vector_size sync_exception_aarch32 2664f6ad66aSAchin Gupta 267e0ae9fabSSandrine Bailleuxvector_entry irq_aarch32 268dce74b89SAchin Gupta handle_interrupt_exception irq_aarch32 269a7934d69SJeenu Viswambharan check_vector_size irq_aarch32 2704f6ad66aSAchin Gupta 271e0ae9fabSSandrine Bailleuxvector_entry fiq_aarch32 272dce74b89SAchin Gupta handle_interrupt_exception fiq_aarch32 273a7934d69SJeenu Viswambharan check_vector_size fiq_aarch32 2744f6ad66aSAchin Gupta 275e0ae9fabSSandrine Bailleuxvector_entry serror_aarch32 276a6ef4393SDouglas Raillard /* 277a6ef4393SDouglas Raillard * SError exceptions from lower ELs are not currently supported. 278a6ef4393SDouglas Raillard * Report their occurrence. 279a6ef4393SDouglas Raillard */ 2804d91838bSJulius Werner b report_unhandled_exception 281a7934d69SJeenu Viswambharan check_vector_size serror_aarch32 282a7934d69SJeenu Viswambharan 283caa84939SJeenu Viswambharan 284a6ef4393SDouglas Raillard /* --------------------------------------------------------------------- 285caa84939SJeenu Viswambharan * The following code handles secure monitor calls. 286a6ef4393SDouglas Raillard * Depending upon the execution state from where the SMC has been 287a6ef4393SDouglas Raillard * invoked, it frees some general purpose registers to perform the 288a6ef4393SDouglas Raillard * remaining tasks. They involve finding the runtime service handler 289a6ef4393SDouglas Raillard * that is the target of the SMC & switching to runtime stacks (SP_EL0) 290a6ef4393SDouglas Raillard * before calling the handler. 291caa84939SJeenu Viswambharan * 292a6ef4393SDouglas Raillard * Note that x30 has been explicitly saved and can be used here 293a6ef4393SDouglas Raillard * --------------------------------------------------------------------- 294caa84939SJeenu Viswambharan */ 2950a30cf54SAndrew Thoelkefunc smc_handler 296caa84939SJeenu Viswambharansmc_handler32: 297caa84939SJeenu Viswambharan /* Check whether aarch32 issued an SMC64 */ 298caa84939SJeenu Viswambharan tbnz x0, #FUNCID_CC_SHIFT, smc_prohibited 299caa84939SJeenu Viswambharan 300a6ef4393SDouglas Raillard /* 301a6ef4393SDouglas Raillard * Since we're are coming from aarch32, x8-x18 need to be saved as per 302a6ef4393SDouglas Raillard * SMC32 calling convention. If a lower EL in aarch64 is making an 303a6ef4393SDouglas Raillard * SMC32 call then it must have saved x8-x17 already therein. 304caa84939SJeenu Viswambharan */ 305caa84939SJeenu Viswambharan stp x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8] 306caa84939SJeenu Viswambharan stp x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10] 307caa84939SJeenu Viswambharan stp x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12] 308caa84939SJeenu Viswambharan stp x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14] 309caa84939SJeenu Viswambharan stp x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16] 310caa84939SJeenu Viswambharan 311caa84939SJeenu Viswambharan /* x4-x7, x18, sp_el0 are saved below */ 312caa84939SJeenu Viswambharan 313caa84939SJeenu Viswambharansmc_handler64: 314a6ef4393SDouglas Raillard /* 315a6ef4393SDouglas Raillard * Populate the parameters for the SMC handler. 316a6ef4393SDouglas Raillard * We already have x0-x4 in place. x5 will point to a cookie (not used 317a6ef4393SDouglas Raillard * now). x6 will point to the context structure (SP_EL3) and x7 will 318a6ef4393SDouglas Raillard * contain flags we need to pass to the handler Hence save x5-x7. 319a6ef4393SDouglas Raillard * 320a6ef4393SDouglas Raillard * Note: x4 only needs to be preserved for AArch32 callers but we do it 321a6ef4393SDouglas Raillard * for AArch64 callers as well for convenience 322caa84939SJeenu Viswambharan */ 323caa84939SJeenu Viswambharan stp x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4] 324caa84939SJeenu Viswambharan stp x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6] 325caa84939SJeenu Viswambharan 326c3260f9bSSoby Mathew /* Save rest of the gpregs and sp_el0*/ 327c3260f9bSSoby Mathew save_x18_to_x29_sp_el0 328c3260f9bSSoby Mathew 329caa84939SJeenu Viswambharan mov x5, xzr 330caa84939SJeenu Viswambharan mov x6, sp 331caa84939SJeenu Viswambharan 332caa84939SJeenu Viswambharan /* Get the unique owning entity number */ 333caa84939SJeenu Viswambharan ubfx x16, x0, #FUNCID_OEN_SHIFT, #FUNCID_OEN_WIDTH 334caa84939SJeenu Viswambharan ubfx x15, x0, #FUNCID_TYPE_SHIFT, #FUNCID_TYPE_WIDTH 335caa84939SJeenu Viswambharan orr x16, x16, x15, lsl #FUNCID_OEN_WIDTH 336caa84939SJeenu Viswambharan 337caa84939SJeenu Viswambharan adr x11, (__RT_SVC_DESCS_START__ + RT_SVC_DESC_HANDLE) 338caa84939SJeenu Viswambharan 339caa84939SJeenu Viswambharan /* Load descriptor index from array of indices */ 340caa84939SJeenu Viswambharan adr x14, rt_svc_descs_indices 341caa84939SJeenu Viswambharan ldrb w15, [x14, x16] 342caa84939SJeenu Viswambharan 343a6ef4393SDouglas Raillard /* 344a6ef4393SDouglas Raillard * Restore the saved C runtime stack value which will become the new 345a6ef4393SDouglas Raillard * SP_EL0 i.e. EL3 runtime stack. It was saved in the 'cpu_context' 346a6ef4393SDouglas Raillard * structure prior to the last ERET from EL3. 347caa84939SJeenu Viswambharan */ 348caa84939SJeenu Viswambharan ldr x12, [x6, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP] 349caa84939SJeenu Viswambharan 350caa84939SJeenu Viswambharan /* 351caa84939SJeenu Viswambharan * Any index greater than 127 is invalid. Check bit 7 for 352caa84939SJeenu Viswambharan * a valid index 353caa84939SJeenu Viswambharan */ 354caa84939SJeenu Viswambharan tbnz w15, 7, smc_unknown 355caa84939SJeenu Viswambharan 356caa84939SJeenu Viswambharan /* Switch to SP_EL0 */ 357caa84939SJeenu Viswambharan msr spsel, #0 358caa84939SJeenu Viswambharan 359a6ef4393SDouglas Raillard /* 360caa84939SJeenu Viswambharan * Get the descriptor using the index 361caa84939SJeenu Viswambharan * x11 = (base + off), x15 = index 362caa84939SJeenu Viswambharan * 363caa84939SJeenu Viswambharan * handler = (base + off) + (index << log2(size)) 364caa84939SJeenu Viswambharan */ 365caa84939SJeenu Viswambharan lsl w10, w15, #RT_SVC_SIZE_LOG2 366caa84939SJeenu Viswambharan ldr x15, [x11, w10, uxtw] 367caa84939SJeenu Viswambharan 368a6ef4393SDouglas Raillard /* 369a6ef4393SDouglas Raillard * Save the SPSR_EL3, ELR_EL3, & SCR_EL3 in case there is a world 370a6ef4393SDouglas Raillard * switch during SMC handling. 371a6ef4393SDouglas Raillard * TODO: Revisit if all system registers can be saved later. 372caa84939SJeenu Viswambharan */ 373caa84939SJeenu Viswambharan mrs x16, spsr_el3 374caa84939SJeenu Viswambharan mrs x17, elr_el3 375caa84939SJeenu Viswambharan mrs x18, scr_el3 376caa84939SJeenu Viswambharan stp x16, x17, [x6, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3] 377b51da821SAchin Gupta str x18, [x6, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3] 378caa84939SJeenu Viswambharan 379caa84939SJeenu Viswambharan /* Copy SCR_EL3.NS bit to the flag to indicate caller's security */ 380caa84939SJeenu Viswambharan bfi x7, x18, #0, #1 381caa84939SJeenu Viswambharan 382caa84939SJeenu Viswambharan mov sp, x12 383caa84939SJeenu Viswambharan 384a6ef4393SDouglas Raillard /* 385a6ef4393SDouglas Raillard * Call the Secure Monitor Call handler and then drop directly into 386a6ef4393SDouglas Raillard * el3_exit() which will program any remaining architectural state 387a6ef4393SDouglas Raillard * prior to issuing the ERET to the desired lower EL. 388caa84939SJeenu Viswambharan */ 389caa84939SJeenu Viswambharan#if DEBUG 390caa84939SJeenu Viswambharan cbz x15, rt_svc_fw_critical_error 391caa84939SJeenu Viswambharan#endif 392caa84939SJeenu Viswambharan blr x15 393caa84939SJeenu Viswambharan 394bbf8f6f9SYatharth Kochar b el3_exit 3954f6ad66aSAchin Gupta 396caa84939SJeenu Viswambharansmc_unknown: 397caa84939SJeenu Viswambharan /* 398caa84939SJeenu Viswambharan * Here we restore x4-x18 regardless of where we came from. AArch32 399caa84939SJeenu Viswambharan * callers will find the registers contents unchanged, but AArch64 400caa84939SJeenu Viswambharan * callers will find the registers modified (with stale earlier NS 401caa84939SJeenu Viswambharan * content). Either way, we aren't leaking any secure information 402a6ef4393SDouglas Raillard * through them. 403caa84939SJeenu Viswambharan */ 404a43d431bSSoby Mathew mov w0, #SMC_UNK 405a43d431bSSoby Mathew b restore_gp_registers_callee_eret 406caa84939SJeenu Viswambharan 407caa84939SJeenu Viswambharansmc_prohibited: 408c3260f9bSSoby Mathew ldr x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR] 409caa84939SJeenu Viswambharan mov w0, #SMC_UNK 410caa84939SJeenu Viswambharan eret 411caa84939SJeenu Viswambharan 412caa84939SJeenu Viswambharanrt_svc_fw_critical_error: 413a6ef4393SDouglas Raillard /* Switch to SP_ELx */ 414a6ef4393SDouglas Raillard msr spsel, #1 415a806dad5SJeenu Viswambharan no_ret report_unhandled_exception 4168b779620SKévin Petitendfunc smc_handler 417