14f6ad66aSAchin Gupta/* 2e83b0cadSDan Handley * Copyright (c) 2013-2014, 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> 34dce74b89SAchin Gupta#include <interrupt_mgmt.h> 355f0cdb05SDan Handley#include <platform_def.h> 3697043ac9SDan Handley#include <runtime_svc.h> 374f6ad66aSAchin Gupta 384f6ad66aSAchin Gupta .globl runtime_exceptions 39caa84939SJeenu Viswambharan .globl el3_exit 404f6ad66aSAchin Gupta 41dce74b89SAchin Gupta /* ----------------------------------------------------- 42dce74b89SAchin Gupta * Handle SMC exceptions seperately from other sync. 43dce74b89SAchin Gupta * exceptions. 44dce74b89SAchin Gupta * ----------------------------------------------------- 45dce74b89SAchin Gupta */ 46dce74b89SAchin Gupta .macro handle_sync_exception 47dce74b89SAchin Gupta str x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR] 48dce74b89SAchin Gupta mrs x30, esr_el3 49dce74b89SAchin Gupta ubfx x30, x30, #ESR_EC_SHIFT, #ESR_EC_LENGTH 50dce74b89SAchin Gupta 51dce74b89SAchin Gupta cmp x30, #EC_AARCH32_SMC 52dce74b89SAchin Gupta b.eq smc_handler32 53dce74b89SAchin Gupta 54dce74b89SAchin Gupta cmp x30, #EC_AARCH64_SMC 55dce74b89SAchin Gupta b.eq smc_handler64 56dce74b89SAchin Gupta 57dce74b89SAchin Gupta /* ----------------------------------------------------- 58dce74b89SAchin Gupta * The following code handles any synchronous exception 59dce74b89SAchin Gupta * that is not an SMC. 60dce74b89SAchin Gupta * ----------------------------------------------------- 61dce74b89SAchin Gupta */ 62dce74b89SAchin Gupta 63dce74b89SAchin Gupta bl dump_state_and_die 64dce74b89SAchin Gupta .endm 65dce74b89SAchin Gupta 66dce74b89SAchin Gupta 67dce74b89SAchin Gupta /* ----------------------------------------------------- 68dce74b89SAchin Gupta * This macro handles FIQ or IRQ interrupts i.e. EL3, 69dce74b89SAchin Gupta * S-EL1 and NS interrupts. 70dce74b89SAchin Gupta * ----------------------------------------------------- 71dce74b89SAchin Gupta */ 72dce74b89SAchin Gupta .macro handle_interrupt_exception label 73dce74b89SAchin Gupta str x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR] 74dce74b89SAchin Gupta bl save_gp_registers 75dce74b89SAchin Gupta 76dce74b89SAchin Gupta /* Switch to the runtime stack i.e. SP_EL0 */ 77dce74b89SAchin Gupta ldr x2, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP] 78dce74b89SAchin Gupta mov x20, sp 79dce74b89SAchin Gupta msr spsel, #0 80dce74b89SAchin Gupta mov sp, x2 81dce74b89SAchin Gupta 82dce74b89SAchin Gupta /* 83dce74b89SAchin Gupta * Find out whether this is a valid interrupt type. If the 84dce74b89SAchin Gupta * interrupt controller reports a spurious interrupt then 85dce74b89SAchin Gupta * return to where we came from. 86dce74b89SAchin Gupta */ 879865ac15SDan Handley bl plat_ic_get_pending_interrupt_type 88dce74b89SAchin Gupta cmp x0, #INTR_TYPE_INVAL 89dce74b89SAchin Gupta b.eq interrupt_exit_\label 90dce74b89SAchin Gupta 91dce74b89SAchin Gupta /* 92dce74b89SAchin Gupta * Get the registered handler for this interrupt type. A 93dce74b89SAchin Gupta * NULL return value implies that an interrupt was generated 94dce74b89SAchin Gupta * for which there is no handler registered or the interrupt 95dce74b89SAchin Gupta * was routed incorrectly. This is a problem of the framework 96dce74b89SAchin Gupta * so report it as an error. 97dce74b89SAchin Gupta */ 98dce74b89SAchin Gupta bl get_interrupt_type_handler 99dce74b89SAchin Gupta cbz x0, interrupt_error_\label 100dce74b89SAchin Gupta mov x21, x0 101dce74b89SAchin Gupta 102dce74b89SAchin Gupta mov x0, #INTR_ID_UNAVAILABLE 103dce74b89SAchin Gupta#if IMF_READ_INTERRUPT_ID 104dce74b89SAchin Gupta /* 105dce74b89SAchin Gupta * Read the id of the highest priority pending interrupt. If 106dce74b89SAchin Gupta * no interrupt is asserted then return to where we came from. 107dce74b89SAchin Gupta */ 1089865ac15SDan Handley bl plat_ic_get_pending_interrupt_id 109dce74b89SAchin Gupta cmp x0, #INTR_ID_UNAVAILABLE 110dce74b89SAchin Gupta b.eq interrupt_exit_\label 111dce74b89SAchin Gupta#endif 112dce74b89SAchin Gupta 113dce74b89SAchin Gupta /* 114dce74b89SAchin Gupta * Save the EL3 system registers needed to return from 115dce74b89SAchin Gupta * this exception. 116dce74b89SAchin Gupta */ 117dce74b89SAchin Gupta mrs x3, spsr_el3 118dce74b89SAchin Gupta mrs x4, elr_el3 119dce74b89SAchin Gupta stp x3, x4, [x20, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3] 120dce74b89SAchin Gupta 121dce74b89SAchin Gupta /* Set the current security state in the 'flags' parameter */ 122dce74b89SAchin Gupta mrs x2, scr_el3 123dce74b89SAchin Gupta ubfx x1, x2, #0, #1 124dce74b89SAchin Gupta 125dce74b89SAchin Gupta /* Restore the reference to the 'handle' i.e. SP_EL3 */ 126dce74b89SAchin Gupta mov x2, x20 127dce74b89SAchin Gupta 128*b460b8bfSSoby Mathew /* x3 will point to a cookie (not used now) */ 129*b460b8bfSSoby Mathew mov x3, xzr 130*b460b8bfSSoby Mathew 131dce74b89SAchin Gupta /* Call the interrupt type handler */ 132dce74b89SAchin Gupta blr x21 133dce74b89SAchin Gupta 134dce74b89SAchin Guptainterrupt_exit_\label: 135dce74b89SAchin Gupta /* Return from exception, possibly in a different security state */ 136dce74b89SAchin Gupta b el3_exit 137dce74b89SAchin Gupta 138dce74b89SAchin Gupta /* 139dce74b89SAchin Gupta * This label signifies a problem with the interrupt management 140dce74b89SAchin Gupta * framework where it is not safe to go back to the instruction 141dce74b89SAchin Gupta * where the interrupt was generated. 142dce74b89SAchin Gupta */ 143dce74b89SAchin Guptainterrupt_error_\label: 144dce74b89SAchin Gupta bl dump_intr_state_and_die 145dce74b89SAchin Gupta .endm 146dce74b89SAchin Gupta 147dce74b89SAchin Gupta 148c3260f9bSSoby Mathew .macro save_x18_to_x29_sp_el0 149c3260f9bSSoby Mathew stp x18, x19, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X18] 150c3260f9bSSoby Mathew stp x20, x21, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X20] 151c3260f9bSSoby Mathew stp x22, x23, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X22] 152c3260f9bSSoby Mathew stp x24, x25, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X24] 153c3260f9bSSoby Mathew stp x26, x27, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X26] 154c3260f9bSSoby Mathew stp x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28] 155c3260f9bSSoby Mathew mrs x18, sp_el0 156c3260f9bSSoby Mathew str x18, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_SP_EL0] 157c3260f9bSSoby Mathew .endm 158c3260f9bSSoby Mathew 159b739f22aSAchin Gupta .section .vectors, "ax"; .align 11 1604f6ad66aSAchin Gupta 1614f6ad66aSAchin Gupta .align 7 1624f6ad66aSAchin Guptaruntime_exceptions: 1634f6ad66aSAchin Gupta /* ----------------------------------------------------- 1644f6ad66aSAchin Gupta * Current EL with _sp_el0 : 0x0 - 0x180 1654f6ad66aSAchin Gupta * ----------------------------------------------------- 1664f6ad66aSAchin Gupta */ 1674f6ad66aSAchin Guptasync_exception_sp_el0: 168caa84939SJeenu Viswambharan /* ----------------------------------------------------- 169caa84939SJeenu Viswambharan * We don't expect any synchronous exceptions from EL3 170caa84939SJeenu Viswambharan * ----------------------------------------------------- 171caa84939SJeenu Viswambharan */ 172a43d431bSSoby Mathew bl dump_state_and_die 173a7934d69SJeenu Viswambharan check_vector_size sync_exception_sp_el0 1744f6ad66aSAchin Gupta 1754f6ad66aSAchin Gupta .align 7 176caa84939SJeenu Viswambharan /* ----------------------------------------------------- 177caa84939SJeenu Viswambharan * EL3 code is non-reentrant. Any asynchronous exception 178caa84939SJeenu Viswambharan * is a serious error. Loop infinitely. 179caa84939SJeenu Viswambharan * ----------------------------------------------------- 180caa84939SJeenu Viswambharan */ 1814f6ad66aSAchin Guptairq_sp_el0: 182a43d431bSSoby Mathew bl dump_intr_state_and_die 183a7934d69SJeenu Viswambharan check_vector_size irq_sp_el0 1844f6ad66aSAchin Gupta 1854f6ad66aSAchin Gupta .align 7 1864f6ad66aSAchin Guptafiq_sp_el0: 187a43d431bSSoby Mathew bl dump_intr_state_and_die 188a7934d69SJeenu Viswambharan check_vector_size fiq_sp_el0 1894f6ad66aSAchin Gupta 1904f6ad66aSAchin Gupta .align 7 1914f6ad66aSAchin Guptaserror_sp_el0: 192a43d431bSSoby Mathew bl dump_state_and_die 193a7934d69SJeenu Viswambharan check_vector_size serror_sp_el0 1944f6ad66aSAchin Gupta 1954f6ad66aSAchin Gupta /* ----------------------------------------------------- 1964f6ad66aSAchin Gupta * Current EL with SPx: 0x200 - 0x380 1974f6ad66aSAchin Gupta * ----------------------------------------------------- 1984f6ad66aSAchin Gupta */ 1994f6ad66aSAchin Gupta .align 7 2004f6ad66aSAchin Guptasync_exception_sp_elx: 201caa84939SJeenu Viswambharan /* ----------------------------------------------------- 202caa84939SJeenu Viswambharan * This exception will trigger if anything went wrong 203caa84939SJeenu Viswambharan * during a previous exception entry or exit or while 204caa84939SJeenu Viswambharan * handling an earlier unexpected synchronous exception. 205a43d431bSSoby Mathew * There is a high probability that SP_EL3 is corrupted. 206caa84939SJeenu Viswambharan * ----------------------------------------------------- 207caa84939SJeenu Viswambharan */ 208a43d431bSSoby Mathew bl dump_state_and_die 209a7934d69SJeenu Viswambharan check_vector_size sync_exception_sp_elx 2104f6ad66aSAchin Gupta 2114f6ad66aSAchin Gupta .align 7 2124f6ad66aSAchin Guptairq_sp_elx: 213a43d431bSSoby Mathew bl dump_intr_state_and_die 214a7934d69SJeenu Viswambharan check_vector_size irq_sp_elx 215a7934d69SJeenu Viswambharan 2164f6ad66aSAchin Gupta .align 7 2174f6ad66aSAchin Guptafiq_sp_elx: 218a43d431bSSoby Mathew bl dump_intr_state_and_die 219a7934d69SJeenu Viswambharan check_vector_size fiq_sp_elx 220a7934d69SJeenu Viswambharan 2214f6ad66aSAchin Gupta .align 7 2224f6ad66aSAchin Guptaserror_sp_elx: 223a43d431bSSoby Mathew bl dump_state_and_die 224a7934d69SJeenu Viswambharan check_vector_size serror_sp_elx 2254f6ad66aSAchin Gupta 2264f6ad66aSAchin Gupta /* ----------------------------------------------------- 2274f6ad66aSAchin Gupta * Lower EL using AArch64 : 0x400 - 0x580 2284f6ad66aSAchin Gupta * ----------------------------------------------------- 2294f6ad66aSAchin Gupta */ 2304f6ad66aSAchin Gupta .align 7 2314f6ad66aSAchin Guptasync_exception_aarch64: 232caa84939SJeenu Viswambharan /* ----------------------------------------------------- 233caa84939SJeenu Viswambharan * This exception vector will be the entry point for 234caa84939SJeenu Viswambharan * SMCs and traps that are unhandled at lower ELs most 235caa84939SJeenu Viswambharan * commonly. SP_EL3 should point to a valid cpu context 236caa84939SJeenu Viswambharan * where the general purpose and system register state 237caa84939SJeenu Viswambharan * can be saved. 238caa84939SJeenu Viswambharan * ----------------------------------------------------- 239caa84939SJeenu Viswambharan */ 240caa84939SJeenu Viswambharan handle_sync_exception 241a7934d69SJeenu Viswambharan check_vector_size sync_exception_aarch64 2424f6ad66aSAchin Gupta 2434f6ad66aSAchin Gupta .align 7 244caa84939SJeenu Viswambharan /* ----------------------------------------------------- 245caa84939SJeenu Viswambharan * Asynchronous exceptions from lower ELs are not 246caa84939SJeenu Viswambharan * currently supported. Report their occurrence. 247caa84939SJeenu Viswambharan * ----------------------------------------------------- 248caa84939SJeenu Viswambharan */ 2494f6ad66aSAchin Guptairq_aarch64: 250dce74b89SAchin Gupta handle_interrupt_exception irq_aarch64 251a7934d69SJeenu Viswambharan check_vector_size irq_aarch64 2524f6ad66aSAchin Gupta 2534f6ad66aSAchin Gupta .align 7 2544f6ad66aSAchin Guptafiq_aarch64: 255dce74b89SAchin Gupta handle_interrupt_exception fiq_aarch64 256a7934d69SJeenu Viswambharan check_vector_size fiq_aarch64 2574f6ad66aSAchin Gupta 2584f6ad66aSAchin Gupta .align 7 2594f6ad66aSAchin Guptaserror_aarch64: 260a43d431bSSoby Mathew bl dump_state_and_die 261a7934d69SJeenu Viswambharan check_vector_size serror_aarch64 2624f6ad66aSAchin Gupta 2634f6ad66aSAchin Gupta /* ----------------------------------------------------- 2644f6ad66aSAchin Gupta * Lower EL using AArch32 : 0x600 - 0x780 2654f6ad66aSAchin Gupta * ----------------------------------------------------- 2664f6ad66aSAchin Gupta */ 2674f6ad66aSAchin Gupta .align 7 2684f6ad66aSAchin Guptasync_exception_aarch32: 269caa84939SJeenu Viswambharan /* ----------------------------------------------------- 270caa84939SJeenu Viswambharan * This exception vector will be the entry point for 271caa84939SJeenu Viswambharan * SMCs and traps that are unhandled at lower ELs most 272caa84939SJeenu Viswambharan * commonly. SP_EL3 should point to a valid cpu context 273caa84939SJeenu Viswambharan * where the general purpose and system register state 274caa84939SJeenu Viswambharan * can be saved. 275caa84939SJeenu Viswambharan * ----------------------------------------------------- 276caa84939SJeenu Viswambharan */ 277caa84939SJeenu Viswambharan handle_sync_exception 278a7934d69SJeenu Viswambharan check_vector_size sync_exception_aarch32 2794f6ad66aSAchin Gupta 2804f6ad66aSAchin Gupta .align 7 281caa84939SJeenu Viswambharan /* ----------------------------------------------------- 282caa84939SJeenu Viswambharan * Asynchronous exceptions from lower ELs are not 283caa84939SJeenu Viswambharan * currently supported. Report their occurrence. 284caa84939SJeenu Viswambharan * ----------------------------------------------------- 285caa84939SJeenu Viswambharan */ 2864f6ad66aSAchin Guptairq_aarch32: 287dce74b89SAchin Gupta handle_interrupt_exception irq_aarch32 288a7934d69SJeenu Viswambharan check_vector_size irq_aarch32 2894f6ad66aSAchin Gupta 2904f6ad66aSAchin Gupta .align 7 2914f6ad66aSAchin Guptafiq_aarch32: 292dce74b89SAchin Gupta handle_interrupt_exception fiq_aarch32 293a7934d69SJeenu Viswambharan check_vector_size fiq_aarch32 2944f6ad66aSAchin Gupta 2954f6ad66aSAchin Gupta .align 7 2964f6ad66aSAchin Guptaserror_aarch32: 297a43d431bSSoby Mathew bl dump_state_and_die 298a7934d69SJeenu Viswambharan check_vector_size serror_aarch32 299a7934d69SJeenu Viswambharan 300caa84939SJeenu Viswambharan .align 7 301caa84939SJeenu Viswambharan 302caa84939SJeenu Viswambharan /* ----------------------------------------------------- 303caa84939SJeenu Viswambharan * The following code handles secure monitor calls. 304caa84939SJeenu Viswambharan * Depending upon the execution state from where the SMC 305caa84939SJeenu Viswambharan * has been invoked, it frees some general purpose 306caa84939SJeenu Viswambharan * registers to perform the remaining tasks. They 307caa84939SJeenu Viswambharan * involve finding the runtime service handler that is 308caa84939SJeenu Viswambharan * the target of the SMC & switching to runtime stacks 309caa84939SJeenu Viswambharan * (SP_EL0) before calling the handler. 310caa84939SJeenu Viswambharan * 311caa84939SJeenu Viswambharan * Note that x30 has been explicitly saved and can be 312caa84939SJeenu Viswambharan * used here 313caa84939SJeenu Viswambharan * ----------------------------------------------------- 314caa84939SJeenu Viswambharan */ 3150a30cf54SAndrew Thoelkefunc smc_handler 316caa84939SJeenu Viswambharansmc_handler32: 317caa84939SJeenu Viswambharan /* Check whether aarch32 issued an SMC64 */ 318caa84939SJeenu Viswambharan tbnz x0, #FUNCID_CC_SHIFT, smc_prohibited 319caa84939SJeenu Viswambharan 320caa84939SJeenu Viswambharan /* ----------------------------------------------------- 321caa84939SJeenu Viswambharan * Since we're are coming from aarch32, x8-x18 need to 322caa84939SJeenu Viswambharan * be saved as per SMC32 calling convention. If a lower 323caa84939SJeenu Viswambharan * EL in aarch64 is making an SMC32 call then it must 324caa84939SJeenu Viswambharan * have saved x8-x17 already therein. 325caa84939SJeenu Viswambharan * ----------------------------------------------------- 326caa84939SJeenu Viswambharan */ 327caa84939SJeenu Viswambharan stp x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8] 328caa84939SJeenu Viswambharan stp x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10] 329caa84939SJeenu Viswambharan stp x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12] 330caa84939SJeenu Viswambharan stp x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14] 331caa84939SJeenu Viswambharan stp x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16] 332caa84939SJeenu Viswambharan 333caa84939SJeenu Viswambharan /* x4-x7, x18, sp_el0 are saved below */ 334caa84939SJeenu Viswambharan 335caa84939SJeenu Viswambharansmc_handler64: 336caa84939SJeenu Viswambharan /* ----------------------------------------------------- 337caa84939SJeenu Viswambharan * Populate the parameters for the SMC handler. We 338caa84939SJeenu Viswambharan * already have x0-x4 in place. x5 will point to a 339caa84939SJeenu Viswambharan * cookie (not used now). x6 will point to the context 340caa84939SJeenu Viswambharan * structure (SP_EL3) and x7 will contain flags we need 341caa84939SJeenu Viswambharan * to pass to the handler Hence save x5-x7. Note that x4 342caa84939SJeenu Viswambharan * only needs to be preserved for AArch32 callers but we 343caa84939SJeenu Viswambharan * do it for AArch64 callers as well for convenience 344caa84939SJeenu Viswambharan * ----------------------------------------------------- 345caa84939SJeenu Viswambharan */ 346caa84939SJeenu Viswambharan stp x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4] 347caa84939SJeenu Viswambharan stp x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6] 348caa84939SJeenu Viswambharan 349c3260f9bSSoby Mathew /* Save rest of the gpregs and sp_el0*/ 350c3260f9bSSoby Mathew save_x18_to_x29_sp_el0 351c3260f9bSSoby Mathew 352caa84939SJeenu Viswambharan mov x5, xzr 353caa84939SJeenu Viswambharan mov x6, sp 354caa84939SJeenu Viswambharan 355caa84939SJeenu Viswambharan /* Get the unique owning entity number */ 356caa84939SJeenu Viswambharan ubfx x16, x0, #FUNCID_OEN_SHIFT, #FUNCID_OEN_WIDTH 357caa84939SJeenu Viswambharan ubfx x15, x0, #FUNCID_TYPE_SHIFT, #FUNCID_TYPE_WIDTH 358caa84939SJeenu Viswambharan orr x16, x16, x15, lsl #FUNCID_OEN_WIDTH 359caa84939SJeenu Viswambharan 360caa84939SJeenu Viswambharan adr x11, (__RT_SVC_DESCS_START__ + RT_SVC_DESC_HANDLE) 361caa84939SJeenu Viswambharan 362caa84939SJeenu Viswambharan /* Load descriptor index from array of indices */ 363caa84939SJeenu Viswambharan adr x14, rt_svc_descs_indices 364caa84939SJeenu Viswambharan ldrb w15, [x14, x16] 365caa84939SJeenu Viswambharan 366caa84939SJeenu Viswambharan /* ----------------------------------------------------- 367caa84939SJeenu Viswambharan * Restore the saved C runtime stack value which will 368caa84939SJeenu Viswambharan * become the new SP_EL0 i.e. EL3 runtime stack. It was 369caa84939SJeenu Viswambharan * saved in the 'cpu_context' structure prior to the last 370caa84939SJeenu Viswambharan * ERET from EL3. 371caa84939SJeenu Viswambharan * ----------------------------------------------------- 372caa84939SJeenu Viswambharan */ 373caa84939SJeenu Viswambharan ldr x12, [x6, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP] 374caa84939SJeenu Viswambharan 375caa84939SJeenu Viswambharan /* 376caa84939SJeenu Viswambharan * Any index greater than 127 is invalid. Check bit 7 for 377caa84939SJeenu Viswambharan * a valid index 378caa84939SJeenu Viswambharan */ 379caa84939SJeenu Viswambharan tbnz w15, 7, smc_unknown 380caa84939SJeenu Viswambharan 381caa84939SJeenu Viswambharan /* Switch to SP_EL0 */ 382caa84939SJeenu Viswambharan msr spsel, #0 383caa84939SJeenu Viswambharan 384caa84939SJeenu Viswambharan /* ----------------------------------------------------- 385caa84939SJeenu Viswambharan * Get the descriptor using the index 386caa84939SJeenu Viswambharan * x11 = (base + off), x15 = index 387caa84939SJeenu Viswambharan * 388caa84939SJeenu Viswambharan * handler = (base + off) + (index << log2(size)) 389caa84939SJeenu Viswambharan * ----------------------------------------------------- 390caa84939SJeenu Viswambharan */ 391caa84939SJeenu Viswambharan lsl w10, w15, #RT_SVC_SIZE_LOG2 392caa84939SJeenu Viswambharan ldr x15, [x11, w10, uxtw] 393caa84939SJeenu Viswambharan 394caa84939SJeenu Viswambharan /* ----------------------------------------------------- 395caa84939SJeenu Viswambharan * Save the SPSR_EL3, ELR_EL3, & SCR_EL3 in case there 396caa84939SJeenu Viswambharan * is a world switch during SMC handling. 397caa84939SJeenu Viswambharan * TODO: Revisit if all system registers can be saved 398caa84939SJeenu Viswambharan * later. 399caa84939SJeenu Viswambharan * ----------------------------------------------------- 400caa84939SJeenu Viswambharan */ 401caa84939SJeenu Viswambharan mrs x16, spsr_el3 402caa84939SJeenu Viswambharan mrs x17, elr_el3 403caa84939SJeenu Viswambharan mrs x18, scr_el3 404caa84939SJeenu Viswambharan stp x16, x17, [x6, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3] 405caa84939SJeenu Viswambharan stp x18, xzr, [x6, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3] 406caa84939SJeenu Viswambharan 407caa84939SJeenu Viswambharan /* Copy SCR_EL3.NS bit to the flag to indicate caller's security */ 408caa84939SJeenu Viswambharan bfi x7, x18, #0, #1 409caa84939SJeenu Viswambharan 410caa84939SJeenu Viswambharan mov sp, x12 411caa84939SJeenu Viswambharan 412caa84939SJeenu Viswambharan /* ----------------------------------------------------- 413caa84939SJeenu Viswambharan * Call the Secure Monitor Call handler and then drop 414caa84939SJeenu Viswambharan * directly into el3_exit() which will program any 415caa84939SJeenu Viswambharan * remaining architectural state prior to issuing the 416caa84939SJeenu Viswambharan * ERET to the desired lower EL. 417caa84939SJeenu Viswambharan * ----------------------------------------------------- 418caa84939SJeenu Viswambharan */ 419caa84939SJeenu Viswambharan#if DEBUG 420caa84939SJeenu Viswambharan cbz x15, rt_svc_fw_critical_error 421caa84939SJeenu Viswambharan#endif 422caa84939SJeenu Viswambharan blr x15 423caa84939SJeenu Viswambharan 424caa84939SJeenu Viswambharan /* ----------------------------------------------------- 425caa84939SJeenu Viswambharan * This routine assumes that the SP_EL3 is pointing to 426caa84939SJeenu Viswambharan * a valid context structure from where the gp regs and 427caa84939SJeenu Viswambharan * other special registers can be retrieved. 4280a30cf54SAndrew Thoelke * 4290a30cf54SAndrew Thoelke * Keep it in the same section as smc_handler as this 4300a30cf54SAndrew Thoelke * function uses a fall-through to el3_exit 431caa84939SJeenu Viswambharan * ----------------------------------------------------- 432caa84939SJeenu Viswambharan */ 433caa84939SJeenu Viswambharanel3_exit: ; .type el3_exit, %function 434caa84939SJeenu Viswambharan /* ----------------------------------------------------- 435caa84939SJeenu Viswambharan * Save the current SP_EL0 i.e. the EL3 runtime stack 436caa84939SJeenu Viswambharan * which will be used for handling the next SMC. Then 437caa84939SJeenu Viswambharan * switch to SP_EL3 438caa84939SJeenu Viswambharan * ----------------------------------------------------- 439caa84939SJeenu Viswambharan */ 440caa84939SJeenu Viswambharan mov x17, sp 441caa84939SJeenu Viswambharan msr spsel, #1 442caa84939SJeenu Viswambharan str x17, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP] 443caa84939SJeenu Viswambharan 444caa84939SJeenu Viswambharan /* ----------------------------------------------------- 445caa84939SJeenu Viswambharan * Restore SPSR_EL3, ELR_EL3 and SCR_EL3 prior to ERET 446caa84939SJeenu Viswambharan * ----------------------------------------------------- 447caa84939SJeenu Viswambharan */ 448caa84939SJeenu Viswambharan ldp x18, xzr, [sp, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3] 449caa84939SJeenu Viswambharan ldp x16, x17, [sp, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3] 450caa84939SJeenu Viswambharan msr scr_el3, x18 451caa84939SJeenu Viswambharan msr spsr_el3, x16 452caa84939SJeenu Viswambharan msr elr_el3, x17 453caa84939SJeenu Viswambharan 454caa84939SJeenu Viswambharan /* Restore saved general purpose registers and return */ 455a43d431bSSoby Mathew b restore_gp_registers_eret 4564f6ad66aSAchin Gupta 457caa84939SJeenu Viswambharansmc_unknown: 458caa84939SJeenu Viswambharan /* 459caa84939SJeenu Viswambharan * Here we restore x4-x18 regardless of where we came from. AArch32 460caa84939SJeenu Viswambharan * callers will find the registers contents unchanged, but AArch64 461caa84939SJeenu Viswambharan * callers will find the registers modified (with stale earlier NS 462caa84939SJeenu Viswambharan * content). Either way, we aren't leaking any secure information 463caa84939SJeenu Viswambharan * through them 464caa84939SJeenu Viswambharan */ 465a43d431bSSoby Mathew mov w0, #SMC_UNK 466a43d431bSSoby Mathew b restore_gp_registers_callee_eret 467caa84939SJeenu Viswambharan 468caa84939SJeenu Viswambharansmc_prohibited: 469c3260f9bSSoby Mathew ldr x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR] 470caa84939SJeenu Viswambharan mov w0, #SMC_UNK 471caa84939SJeenu Viswambharan eret 472caa84939SJeenu Viswambharan 473caa84939SJeenu Viswambharanrt_svc_fw_critical_error: 474a43d431bSSoby Mathew msr spsel, #1 /* Switch to SP_ELx */ 475a43d431bSSoby Mathew bl dump_state_and_die 476caa84939SJeenu Viswambharan 477caa84939SJeenu Viswambharan /* ----------------------------------------------------- 478caa84939SJeenu Viswambharan * The following functions are used to saved and restore 479c3260f9bSSoby Mathew * all the general pupose registers. Ideally we would 480c3260f9bSSoby Mathew * only save and restore the callee saved registers when 481c3260f9bSSoby Mathew * a world switch occurs but that type of implementation 482c3260f9bSSoby Mathew * is more complex. So currently we will always save and 483c3260f9bSSoby Mathew * restore these registers on entry and exit of EL3. 484caa84939SJeenu Viswambharan * These are not macros to ensure their invocation fits 485caa84939SJeenu Viswambharan * within the 32 instructions per exception vector. 486caa84939SJeenu Viswambharan * ----------------------------------------------------- 487caa84939SJeenu Viswambharan */ 488c3260f9bSSoby Mathewfunc save_gp_registers 489caa84939SJeenu Viswambharan stp x0, x1, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0] 490caa84939SJeenu Viswambharan stp x2, x3, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X2] 491caa84939SJeenu Viswambharan stp x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4] 492caa84939SJeenu Viswambharan stp x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6] 493caa84939SJeenu Viswambharan stp x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8] 494caa84939SJeenu Viswambharan stp x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10] 495caa84939SJeenu Viswambharan stp x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12] 496caa84939SJeenu Viswambharan stp x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14] 497caa84939SJeenu Viswambharan stp x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16] 498c3260f9bSSoby Mathew save_x18_to_x29_sp_el0 499caa84939SJeenu Viswambharan ret 500caa84939SJeenu Viswambharan 501a43d431bSSoby Mathewfunc restore_gp_registers_eret 502caa84939SJeenu Viswambharan ldp x0, x1, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0] 503caa84939SJeenu Viswambharan ldp x2, x3, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X2] 504caa84939SJeenu Viswambharan 505a43d431bSSoby Mathewrestore_gp_registers_callee_eret: 506caa84939SJeenu Viswambharan ldp x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4] 507caa84939SJeenu Viswambharan ldp x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6] 508caa84939SJeenu Viswambharan ldp x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8] 509caa84939SJeenu Viswambharan ldp x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10] 510caa84939SJeenu Viswambharan ldp x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12] 511caa84939SJeenu Viswambharan ldp x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14] 512c3260f9bSSoby Mathew ldp x18, x19, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X18] 513c3260f9bSSoby Mathew ldp x20, x21, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X20] 514c3260f9bSSoby Mathew ldp x22, x23, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X22] 515c3260f9bSSoby Mathew ldp x24, x25, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X24] 516c3260f9bSSoby Mathew ldp x26, x27, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X26] 517c3260f9bSSoby Mathew ldp x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28] 518a43d431bSSoby Mathew ldp x30, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR] 519a43d431bSSoby Mathew msr sp_el0, x17 520a43d431bSSoby Mathew ldp x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16] 521a43d431bSSoby Mathew eret 522