14f6ad66aSAchin Gupta/* 2e0ae9fabSSandrine Bailleux * Copyright (c) 2013-2016, ARM Limited and Contributors. All rights reserved. 34f6ad66aSAchin Gupta * 44f6ad66aSAchin Gupta * Redistribution and use in source and binary forms, with or without 54f6ad66aSAchin Gupta * modification, are permitted provided that the following conditions are met: 64f6ad66aSAchin Gupta * 74f6ad66aSAchin Gupta * Redistributions of source code must retain the above copyright notice, this 84f6ad66aSAchin Gupta * list of conditions and the following disclaimer. 94f6ad66aSAchin Gupta * 104f6ad66aSAchin Gupta * Redistributions in binary form must reproduce the above copyright notice, 114f6ad66aSAchin Gupta * this list of conditions and the following disclaimer in the documentation 124f6ad66aSAchin Gupta * and/or other materials provided with the distribution. 134f6ad66aSAchin Gupta * 144f6ad66aSAchin Gupta * Neither the name of ARM nor the names of its contributors may be used 154f6ad66aSAchin Gupta * to endorse or promote products derived from this software without specific 164f6ad66aSAchin Gupta * prior written permission. 174f6ad66aSAchin Gupta * 184f6ad66aSAchin Gupta * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 194f6ad66aSAchin Gupta * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 204f6ad66aSAchin Gupta * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 214f6ad66aSAchin Gupta * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 224f6ad66aSAchin Gupta * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 234f6ad66aSAchin Gupta * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 244f6ad66aSAchin Gupta * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 254f6ad66aSAchin Gupta * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 264f6ad66aSAchin Gupta * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 274f6ad66aSAchin Gupta * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 284f6ad66aSAchin Gupta * POSSIBILITY OF SUCH DAMAGE. 294f6ad66aSAchin Gupta */ 304f6ad66aSAchin Gupta 314f6ad66aSAchin Gupta#include <arch.h> 3235e98e55SDan Handley#include <asm_macros.S> 3397043ac9SDan Handley#include <context.h> 34*872be88aSdp-arm#include <cpu_data.h> 35dce74b89SAchin Gupta#include <interrupt_mgmt.h> 365f0cdb05SDan Handley#include <platform_def.h> 3797043ac9SDan Handley#include <runtime_svc.h> 384f6ad66aSAchin Gupta 394f6ad66aSAchin Gupta .globl runtime_exceptions 404f6ad66aSAchin Gupta 41dce74b89SAchin Gupta /* ----------------------------------------------------- 4244804252SSandrine Bailleux * Handle SMC exceptions separately from other sync. 43dce74b89SAchin Gupta * exceptions. 44dce74b89SAchin Gupta * ----------------------------------------------------- 45dce74b89SAchin Gupta */ 46dce74b89SAchin Gupta .macro handle_sync_exception 470c8d4fefSAchin Gupta /* Enable the SError interrupt */ 480c8d4fefSAchin Gupta msr daifclr, #DAIF_ABT_BIT 490c8d4fefSAchin Gupta 50dce74b89SAchin Gupta str x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR] 51*872be88aSdp-arm 52*872be88aSdp-arm#if ENABLE_RUNTIME_INSTRUMENTATION 53*872be88aSdp-arm 54*872be88aSdp-arm /* 55*872be88aSdp-arm * Read the timestamp value and store it in per-cpu data. 56*872be88aSdp-arm * The value will be extracted from per-cpu data by the 57*872be88aSdp-arm * C level SMC handler and saved to the PMF timestamp region. 58*872be88aSdp-arm */ 59*872be88aSdp-arm mrs x30, cntpct_el0 60*872be88aSdp-arm str x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29] 61*872be88aSdp-arm mrs x29, tpidr_el3 62*872be88aSdp-arm str x30, [x29, #CPU_DATA_PMF_TS0_OFFSET] 63*872be88aSdp-arm ldr x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29] 64*872be88aSdp-arm#endif 65*872be88aSdp-arm 66dce74b89SAchin Gupta mrs x30, esr_el3 67dce74b89SAchin Gupta ubfx x30, x30, #ESR_EC_SHIFT, #ESR_EC_LENGTH 68dce74b89SAchin Gupta 69dce74b89SAchin Gupta cmp x30, #EC_AARCH32_SMC 70dce74b89SAchin Gupta b.eq smc_handler32 71dce74b89SAchin Gupta 72dce74b89SAchin Gupta cmp x30, #EC_AARCH64_SMC 73dce74b89SAchin Gupta b.eq smc_handler64 74dce74b89SAchin Gupta 75dce74b89SAchin Gupta /* ----------------------------------------------------- 76dce74b89SAchin Gupta * The following code handles any synchronous exception 77dce74b89SAchin Gupta * that is not an SMC. 78dce74b89SAchin Gupta * ----------------------------------------------------- 79dce74b89SAchin Gupta */ 80dce74b89SAchin Gupta 81626ed510SSoby Mathew bl report_unhandled_exception 82dce74b89SAchin Gupta .endm 83dce74b89SAchin Gupta 84dce74b89SAchin Gupta 85dce74b89SAchin Gupta /* ----------------------------------------------------- 86dce74b89SAchin Gupta * This macro handles FIQ or IRQ interrupts i.e. EL3, 87dce74b89SAchin Gupta * S-EL1 and NS interrupts. 88dce74b89SAchin Gupta * ----------------------------------------------------- 89dce74b89SAchin Gupta */ 90dce74b89SAchin Gupta .macro handle_interrupt_exception label 910c8d4fefSAchin Gupta /* Enable the SError interrupt */ 920c8d4fefSAchin Gupta msr daifclr, #DAIF_ABT_BIT 930c8d4fefSAchin Gupta 94dce74b89SAchin Gupta str x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR] 95dce74b89SAchin Gupta bl save_gp_registers 96dce74b89SAchin Gupta 975717aae1SAchin Gupta /* 985717aae1SAchin Gupta * Save the EL3 system registers needed to return from 995717aae1SAchin Gupta * this exception. 1005717aae1SAchin Gupta */ 1015717aae1SAchin Gupta mrs x0, spsr_el3 1025717aae1SAchin Gupta mrs x1, elr_el3 1035717aae1SAchin Gupta stp x0, x1, [sp, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3] 1045717aae1SAchin Gupta 105dce74b89SAchin Gupta /* Switch to the runtime stack i.e. SP_EL0 */ 106dce74b89SAchin Gupta ldr x2, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP] 107dce74b89SAchin Gupta mov x20, sp 108dce74b89SAchin Gupta msr spsel, #0 109dce74b89SAchin Gupta mov sp, x2 110dce74b89SAchin Gupta 111dce74b89SAchin Gupta /* 112dce74b89SAchin Gupta * Find out whether this is a valid interrupt type. If the 113dce74b89SAchin Gupta * interrupt controller reports a spurious interrupt then 114dce74b89SAchin Gupta * return to where we came from. 115dce74b89SAchin Gupta */ 1169865ac15SDan Handley bl plat_ic_get_pending_interrupt_type 117dce74b89SAchin Gupta cmp x0, #INTR_TYPE_INVAL 118dce74b89SAchin Gupta b.eq interrupt_exit_\label 119dce74b89SAchin Gupta 120dce74b89SAchin Gupta /* 121dce74b89SAchin Gupta * Get the registered handler for this interrupt type. A 1225717aae1SAchin Gupta * NULL return value could be 'cause of the following 1235717aae1SAchin Gupta * conditions: 1245717aae1SAchin Gupta * 1255717aae1SAchin Gupta * a. An interrupt of a type was routed correctly but a 1265717aae1SAchin Gupta * handler for its type was not registered. 1275717aae1SAchin Gupta * 1285717aae1SAchin Gupta * b. An interrupt of a type was not routed correctly so 1295717aae1SAchin Gupta * a handler for its type was not registered. 1305717aae1SAchin Gupta * 1315717aae1SAchin Gupta * c. An interrupt of a type was routed correctly to EL3, 1325717aae1SAchin Gupta * but was deasserted before its pending state could 1335717aae1SAchin Gupta * be read. Another interrupt of a different type pended 1345717aae1SAchin Gupta * at the same time and its type was reported as pending 1355717aae1SAchin Gupta * instead. However, a handler for this type was not 1365717aae1SAchin Gupta * registered. 1375717aae1SAchin Gupta * 1385717aae1SAchin Gupta * a. and b. can only happen due to a programming error. 1395717aae1SAchin Gupta * The occurrence of c. could be beyond the control of 1405717aae1SAchin Gupta * Trusted Firmware. It makes sense to return from this 1415717aae1SAchin Gupta * exception instead of reporting an error. 142dce74b89SAchin Gupta */ 143dce74b89SAchin Gupta bl get_interrupt_type_handler 1445717aae1SAchin Gupta cbz x0, interrupt_exit_\label 145dce74b89SAchin Gupta mov x21, x0 146dce74b89SAchin Gupta 147dce74b89SAchin Gupta mov x0, #INTR_ID_UNAVAILABLE 148dce74b89SAchin Gupta 149dce74b89SAchin Gupta /* Set the current security state in the 'flags' parameter */ 150dce74b89SAchin Gupta mrs x2, scr_el3 151dce74b89SAchin Gupta ubfx x1, x2, #0, #1 152dce74b89SAchin Gupta 153dce74b89SAchin Gupta /* Restore the reference to the 'handle' i.e. SP_EL3 */ 154dce74b89SAchin Gupta mov x2, x20 155dce74b89SAchin Gupta 156b460b8bfSSoby Mathew /* x3 will point to a cookie (not used now) */ 157b460b8bfSSoby Mathew mov x3, xzr 158b460b8bfSSoby Mathew 159dce74b89SAchin Gupta /* Call the interrupt type handler */ 160dce74b89SAchin Gupta blr x21 161dce74b89SAchin Gupta 162dce74b89SAchin Guptainterrupt_exit_\label: 163dce74b89SAchin Gupta /* Return from exception, possibly in a different security state */ 164dce74b89SAchin Gupta b el3_exit 165dce74b89SAchin Gupta 166dce74b89SAchin Gupta .endm 167dce74b89SAchin Gupta 168dce74b89SAchin Gupta 169c3260f9bSSoby Mathew .macro save_x18_to_x29_sp_el0 170c3260f9bSSoby Mathew stp x18, x19, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X18] 171c3260f9bSSoby Mathew stp x20, x21, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X20] 172c3260f9bSSoby Mathew stp x22, x23, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X22] 173c3260f9bSSoby Mathew stp x24, x25, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X24] 174c3260f9bSSoby Mathew stp x26, x27, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X26] 175c3260f9bSSoby Mathew stp x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28] 176c3260f9bSSoby Mathew mrs x18, sp_el0 177c3260f9bSSoby Mathew str x18, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_SP_EL0] 178c3260f9bSSoby Mathew .endm 179c3260f9bSSoby Mathew 180e0ae9fabSSandrine Bailleux 181e0ae9fabSSandrine Bailleuxvector_base runtime_exceptions 182e0ae9fabSSandrine Bailleux 1834f6ad66aSAchin Gupta /* ----------------------------------------------------- 18444804252SSandrine Bailleux * Current EL with _sp_el0 : 0x0 - 0x200 1854f6ad66aSAchin Gupta * ----------------------------------------------------- 1864f6ad66aSAchin Gupta */ 187e0ae9fabSSandrine Bailleuxvector_entry sync_exception_sp_el0 188caa84939SJeenu Viswambharan /* ----------------------------------------------------- 189caa84939SJeenu Viswambharan * We don't expect any synchronous exceptions from EL3 190caa84939SJeenu Viswambharan * ----------------------------------------------------- 191caa84939SJeenu Viswambharan */ 192626ed510SSoby Mathew bl report_unhandled_exception 193a7934d69SJeenu Viswambharan check_vector_size sync_exception_sp_el0 1944f6ad66aSAchin Gupta 195caa84939SJeenu Viswambharan /* ----------------------------------------------------- 196caa84939SJeenu Viswambharan * EL3 code is non-reentrant. Any asynchronous exception 197caa84939SJeenu Viswambharan * is a serious error. Loop infinitely. 198caa84939SJeenu Viswambharan * ----------------------------------------------------- 199caa84939SJeenu Viswambharan */ 200e0ae9fabSSandrine Bailleuxvector_entry irq_sp_el0 201626ed510SSoby Mathew bl report_unhandled_interrupt 202a7934d69SJeenu Viswambharan check_vector_size irq_sp_el0 2034f6ad66aSAchin Gupta 204e0ae9fabSSandrine Bailleux 205e0ae9fabSSandrine Bailleuxvector_entry fiq_sp_el0 206626ed510SSoby Mathew bl report_unhandled_interrupt 207a7934d69SJeenu Viswambharan check_vector_size fiq_sp_el0 2084f6ad66aSAchin Gupta 209e0ae9fabSSandrine Bailleux 210e0ae9fabSSandrine Bailleuxvector_entry serror_sp_el0 211626ed510SSoby Mathew bl report_unhandled_exception 212a7934d69SJeenu Viswambharan check_vector_size serror_sp_el0 2134f6ad66aSAchin Gupta 2144f6ad66aSAchin Gupta /* ----------------------------------------------------- 21544804252SSandrine Bailleux * Current EL with SPx: 0x200 - 0x400 2164f6ad66aSAchin Gupta * ----------------------------------------------------- 2174f6ad66aSAchin Gupta */ 218e0ae9fabSSandrine Bailleux 219e0ae9fabSSandrine Bailleuxvector_entry sync_exception_sp_elx 220caa84939SJeenu Viswambharan /* ----------------------------------------------------- 221caa84939SJeenu Viswambharan * This exception will trigger if anything went wrong 222caa84939SJeenu Viswambharan * during a previous exception entry or exit or while 223caa84939SJeenu Viswambharan * handling an earlier unexpected synchronous exception. 224a43d431bSSoby Mathew * There is a high probability that SP_EL3 is corrupted. 225caa84939SJeenu Viswambharan * ----------------------------------------------------- 226caa84939SJeenu Viswambharan */ 227626ed510SSoby Mathew bl report_unhandled_exception 228a7934d69SJeenu Viswambharan check_vector_size sync_exception_sp_elx 2294f6ad66aSAchin Gupta 230e0ae9fabSSandrine Bailleuxvector_entry irq_sp_elx 231626ed510SSoby Mathew bl report_unhandled_interrupt 232a7934d69SJeenu Viswambharan check_vector_size irq_sp_elx 233a7934d69SJeenu Viswambharan 234e0ae9fabSSandrine Bailleuxvector_entry fiq_sp_elx 235626ed510SSoby Mathew bl report_unhandled_interrupt 236a7934d69SJeenu Viswambharan check_vector_size fiq_sp_elx 237a7934d69SJeenu Viswambharan 238e0ae9fabSSandrine Bailleuxvector_entry serror_sp_elx 239626ed510SSoby Mathew bl report_unhandled_exception 240a7934d69SJeenu Viswambharan check_vector_size serror_sp_elx 2414f6ad66aSAchin Gupta 2424f6ad66aSAchin Gupta /* ----------------------------------------------------- 24344804252SSandrine Bailleux * Lower EL using AArch64 : 0x400 - 0x600 2444f6ad66aSAchin Gupta * ----------------------------------------------------- 2454f6ad66aSAchin Gupta */ 246e0ae9fabSSandrine Bailleuxvector_entry sync_exception_aarch64 247caa84939SJeenu Viswambharan /* ----------------------------------------------------- 248caa84939SJeenu Viswambharan * This exception vector will be the entry point for 249caa84939SJeenu Viswambharan * SMCs and traps that are unhandled at lower ELs most 250caa84939SJeenu Viswambharan * commonly. SP_EL3 should point to a valid cpu context 251caa84939SJeenu Viswambharan * where the general purpose and system register state 252caa84939SJeenu Viswambharan * can be saved. 253caa84939SJeenu Viswambharan * ----------------------------------------------------- 254caa84939SJeenu Viswambharan */ 255caa84939SJeenu Viswambharan handle_sync_exception 256a7934d69SJeenu Viswambharan check_vector_size sync_exception_aarch64 2574f6ad66aSAchin Gupta 258caa84939SJeenu Viswambharan /* ----------------------------------------------------- 259caa84939SJeenu Viswambharan * Asynchronous exceptions from lower ELs are not 260caa84939SJeenu Viswambharan * currently supported. Report their occurrence. 261caa84939SJeenu Viswambharan * ----------------------------------------------------- 262caa84939SJeenu Viswambharan */ 263e0ae9fabSSandrine Bailleuxvector_entry irq_aarch64 264dce74b89SAchin Gupta handle_interrupt_exception irq_aarch64 265a7934d69SJeenu Viswambharan check_vector_size irq_aarch64 2664f6ad66aSAchin Gupta 267e0ae9fabSSandrine Bailleuxvector_entry fiq_aarch64 268dce74b89SAchin Gupta handle_interrupt_exception fiq_aarch64 269a7934d69SJeenu Viswambharan check_vector_size fiq_aarch64 2704f6ad66aSAchin Gupta 271e0ae9fabSSandrine Bailleuxvector_entry serror_aarch64 272626ed510SSoby Mathew bl report_unhandled_exception 273a7934d69SJeenu Viswambharan check_vector_size serror_aarch64 2744f6ad66aSAchin Gupta 2754f6ad66aSAchin Gupta /* ----------------------------------------------------- 27644804252SSandrine Bailleux * Lower EL using AArch32 : 0x600 - 0x800 2774f6ad66aSAchin Gupta * ----------------------------------------------------- 2784f6ad66aSAchin Gupta */ 279e0ae9fabSSandrine Bailleuxvector_entry sync_exception_aarch32 280caa84939SJeenu Viswambharan /* ----------------------------------------------------- 281caa84939SJeenu Viswambharan * This exception vector will be the entry point for 282caa84939SJeenu Viswambharan * SMCs and traps that are unhandled at lower ELs most 283caa84939SJeenu Viswambharan * commonly. SP_EL3 should point to a valid cpu context 284caa84939SJeenu Viswambharan * where the general purpose and system register state 285caa84939SJeenu Viswambharan * can be saved. 286caa84939SJeenu Viswambharan * ----------------------------------------------------- 287caa84939SJeenu Viswambharan */ 288caa84939SJeenu Viswambharan handle_sync_exception 289a7934d69SJeenu Viswambharan check_vector_size sync_exception_aarch32 2904f6ad66aSAchin Gupta 291caa84939SJeenu Viswambharan /* ----------------------------------------------------- 292caa84939SJeenu Viswambharan * Asynchronous exceptions from lower ELs are not 293caa84939SJeenu Viswambharan * currently supported. Report their occurrence. 294caa84939SJeenu Viswambharan * ----------------------------------------------------- 295caa84939SJeenu Viswambharan */ 296e0ae9fabSSandrine Bailleuxvector_entry irq_aarch32 297dce74b89SAchin Gupta handle_interrupt_exception irq_aarch32 298a7934d69SJeenu Viswambharan check_vector_size irq_aarch32 2994f6ad66aSAchin Gupta 300e0ae9fabSSandrine Bailleuxvector_entry fiq_aarch32 301dce74b89SAchin Gupta handle_interrupt_exception fiq_aarch32 302a7934d69SJeenu Viswambharan check_vector_size fiq_aarch32 3034f6ad66aSAchin Gupta 304e0ae9fabSSandrine Bailleuxvector_entry serror_aarch32 305626ed510SSoby Mathew bl report_unhandled_exception 306a7934d69SJeenu Viswambharan check_vector_size serror_aarch32 307a7934d69SJeenu Viswambharan 308caa84939SJeenu Viswambharan 309caa84939SJeenu Viswambharan /* ----------------------------------------------------- 310caa84939SJeenu Viswambharan * The following code handles secure monitor calls. 311caa84939SJeenu Viswambharan * Depending upon the execution state from where the SMC 312caa84939SJeenu Viswambharan * has been invoked, it frees some general purpose 313caa84939SJeenu Viswambharan * registers to perform the remaining tasks. They 314caa84939SJeenu Viswambharan * involve finding the runtime service handler that is 315caa84939SJeenu Viswambharan * the target of the SMC & switching to runtime stacks 316caa84939SJeenu Viswambharan * (SP_EL0) before calling the handler. 317caa84939SJeenu Viswambharan * 318caa84939SJeenu Viswambharan * Note that x30 has been explicitly saved and can be 319caa84939SJeenu Viswambharan * used here 320caa84939SJeenu Viswambharan * ----------------------------------------------------- 321caa84939SJeenu Viswambharan */ 3220a30cf54SAndrew Thoelkefunc smc_handler 323caa84939SJeenu Viswambharansmc_handler32: 324caa84939SJeenu Viswambharan /* Check whether aarch32 issued an SMC64 */ 325caa84939SJeenu Viswambharan tbnz x0, #FUNCID_CC_SHIFT, smc_prohibited 326caa84939SJeenu Viswambharan 327caa84939SJeenu Viswambharan /* ----------------------------------------------------- 328caa84939SJeenu Viswambharan * Since we're are coming from aarch32, x8-x18 need to 329caa84939SJeenu Viswambharan * be saved as per SMC32 calling convention. If a lower 330caa84939SJeenu Viswambharan * EL in aarch64 is making an SMC32 call then it must 331caa84939SJeenu Viswambharan * have saved x8-x17 already therein. 332caa84939SJeenu Viswambharan * ----------------------------------------------------- 333caa84939SJeenu Viswambharan */ 334caa84939SJeenu Viswambharan stp x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8] 335caa84939SJeenu Viswambharan stp x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10] 336caa84939SJeenu Viswambharan stp x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12] 337caa84939SJeenu Viswambharan stp x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14] 338caa84939SJeenu Viswambharan stp x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16] 339caa84939SJeenu Viswambharan 340caa84939SJeenu Viswambharan /* x4-x7, x18, sp_el0 are saved below */ 341caa84939SJeenu Viswambharan 342caa84939SJeenu Viswambharansmc_handler64: 343caa84939SJeenu Viswambharan /* ----------------------------------------------------- 344caa84939SJeenu Viswambharan * Populate the parameters for the SMC handler. We 345caa84939SJeenu Viswambharan * already have x0-x4 in place. x5 will point to a 346caa84939SJeenu Viswambharan * cookie (not used now). x6 will point to the context 347caa84939SJeenu Viswambharan * structure (SP_EL3) and x7 will contain flags we need 348caa84939SJeenu Viswambharan * to pass to the handler Hence save x5-x7. Note that x4 349caa84939SJeenu Viswambharan * only needs to be preserved for AArch32 callers but we 350caa84939SJeenu Viswambharan * do it for AArch64 callers as well for convenience 351caa84939SJeenu Viswambharan * ----------------------------------------------------- 352caa84939SJeenu Viswambharan */ 353caa84939SJeenu Viswambharan stp x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4] 354caa84939SJeenu Viswambharan stp x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6] 355caa84939SJeenu Viswambharan 356c3260f9bSSoby Mathew /* Save rest of the gpregs and sp_el0*/ 357c3260f9bSSoby Mathew save_x18_to_x29_sp_el0 358c3260f9bSSoby Mathew 359caa84939SJeenu Viswambharan mov x5, xzr 360caa84939SJeenu Viswambharan mov x6, sp 361caa84939SJeenu Viswambharan 362caa84939SJeenu Viswambharan /* Get the unique owning entity number */ 363caa84939SJeenu Viswambharan ubfx x16, x0, #FUNCID_OEN_SHIFT, #FUNCID_OEN_WIDTH 364caa84939SJeenu Viswambharan ubfx x15, x0, #FUNCID_TYPE_SHIFT, #FUNCID_TYPE_WIDTH 365caa84939SJeenu Viswambharan orr x16, x16, x15, lsl #FUNCID_OEN_WIDTH 366caa84939SJeenu Viswambharan 367caa84939SJeenu Viswambharan adr x11, (__RT_SVC_DESCS_START__ + RT_SVC_DESC_HANDLE) 368caa84939SJeenu Viswambharan 369caa84939SJeenu Viswambharan /* Load descriptor index from array of indices */ 370caa84939SJeenu Viswambharan adr x14, rt_svc_descs_indices 371caa84939SJeenu Viswambharan ldrb w15, [x14, x16] 372caa84939SJeenu Viswambharan 373caa84939SJeenu Viswambharan /* ----------------------------------------------------- 374caa84939SJeenu Viswambharan * Restore the saved C runtime stack value which will 375caa84939SJeenu Viswambharan * become the new SP_EL0 i.e. EL3 runtime stack. It was 376caa84939SJeenu Viswambharan * saved in the 'cpu_context' structure prior to the last 377caa84939SJeenu Viswambharan * ERET from EL3. 378caa84939SJeenu Viswambharan * ----------------------------------------------------- 379caa84939SJeenu Viswambharan */ 380caa84939SJeenu Viswambharan ldr x12, [x6, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP] 381caa84939SJeenu Viswambharan 382caa84939SJeenu Viswambharan /* 383caa84939SJeenu Viswambharan * Any index greater than 127 is invalid. Check bit 7 for 384caa84939SJeenu Viswambharan * a valid index 385caa84939SJeenu Viswambharan */ 386caa84939SJeenu Viswambharan tbnz w15, 7, smc_unknown 387caa84939SJeenu Viswambharan 388caa84939SJeenu Viswambharan /* Switch to SP_EL0 */ 389caa84939SJeenu Viswambharan msr spsel, #0 390caa84939SJeenu Viswambharan 391caa84939SJeenu Viswambharan /* ----------------------------------------------------- 392caa84939SJeenu Viswambharan * Get the descriptor using the index 393caa84939SJeenu Viswambharan * x11 = (base + off), x15 = index 394caa84939SJeenu Viswambharan * 395caa84939SJeenu Viswambharan * handler = (base + off) + (index << log2(size)) 396caa84939SJeenu Viswambharan * ----------------------------------------------------- 397caa84939SJeenu Viswambharan */ 398caa84939SJeenu Viswambharan lsl w10, w15, #RT_SVC_SIZE_LOG2 399caa84939SJeenu Viswambharan ldr x15, [x11, w10, uxtw] 400caa84939SJeenu Viswambharan 401caa84939SJeenu Viswambharan /* ----------------------------------------------------- 402caa84939SJeenu Viswambharan * Save the SPSR_EL3, ELR_EL3, & SCR_EL3 in case there 403caa84939SJeenu Viswambharan * is a world switch during SMC handling. 404caa84939SJeenu Viswambharan * TODO: Revisit if all system registers can be saved 405caa84939SJeenu Viswambharan * later. 406caa84939SJeenu Viswambharan * ----------------------------------------------------- 407caa84939SJeenu Viswambharan */ 408caa84939SJeenu Viswambharan mrs x16, spsr_el3 409caa84939SJeenu Viswambharan mrs x17, elr_el3 410caa84939SJeenu Viswambharan mrs x18, scr_el3 411caa84939SJeenu Viswambharan stp x16, x17, [x6, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3] 412b51da821SAchin Gupta str x18, [x6, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3] 413caa84939SJeenu Viswambharan 414caa84939SJeenu Viswambharan /* Copy SCR_EL3.NS bit to the flag to indicate caller's security */ 415caa84939SJeenu Viswambharan bfi x7, x18, #0, #1 416caa84939SJeenu Viswambharan 417caa84939SJeenu Viswambharan mov sp, x12 418caa84939SJeenu Viswambharan 419caa84939SJeenu Viswambharan /* ----------------------------------------------------- 420caa84939SJeenu Viswambharan * Call the Secure Monitor Call handler and then drop 421caa84939SJeenu Viswambharan * directly into el3_exit() which will program any 422caa84939SJeenu Viswambharan * remaining architectural state prior to issuing the 423caa84939SJeenu Viswambharan * ERET to the desired lower EL. 424caa84939SJeenu Viswambharan * ----------------------------------------------------- 425caa84939SJeenu Viswambharan */ 426caa84939SJeenu Viswambharan#if DEBUG 427caa84939SJeenu Viswambharan cbz x15, rt_svc_fw_critical_error 428caa84939SJeenu Viswambharan#endif 429caa84939SJeenu Viswambharan blr x15 430caa84939SJeenu Viswambharan 431bbf8f6f9SYatharth Kochar b el3_exit 4324f6ad66aSAchin Gupta 433caa84939SJeenu Viswambharansmc_unknown: 434caa84939SJeenu Viswambharan /* 435caa84939SJeenu Viswambharan * Here we restore x4-x18 regardless of where we came from. AArch32 436caa84939SJeenu Viswambharan * callers will find the registers contents unchanged, but AArch64 437caa84939SJeenu Viswambharan * callers will find the registers modified (with stale earlier NS 438caa84939SJeenu Viswambharan * content). Either way, we aren't leaking any secure information 439caa84939SJeenu Viswambharan * through them 440caa84939SJeenu Viswambharan */ 441a43d431bSSoby Mathew mov w0, #SMC_UNK 442a43d431bSSoby Mathew b restore_gp_registers_callee_eret 443caa84939SJeenu Viswambharan 444caa84939SJeenu Viswambharansmc_prohibited: 445c3260f9bSSoby Mathew ldr x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR] 446caa84939SJeenu Viswambharan mov w0, #SMC_UNK 447caa84939SJeenu Viswambharan eret 448caa84939SJeenu Viswambharan 449caa84939SJeenu Viswambharanrt_svc_fw_critical_error: 450a43d431bSSoby Mathew msr spsel, #1 /* Switch to SP_ELx */ 451626ed510SSoby Mathew bl report_unhandled_exception 4528b779620SKévin Petitendfunc smc_handler 453