16c595b3dSSandrine Bailleux/* 2e0ae9fabSSandrine Bailleux * Copyright (c) 2013-2016, ARM Limited and Contributors. All rights reserved. 36c595b3dSSandrine Bailleux * 46c595b3dSSandrine Bailleux * Redistribution and use in source and binary forms, with or without 56c595b3dSSandrine Bailleux * modification, are permitted provided that the following conditions are met: 66c595b3dSSandrine Bailleux * 76c595b3dSSandrine Bailleux * Redistributions of source code must retain the above copyright notice, this 86c595b3dSSandrine Bailleux * list of conditions and the following disclaimer. 96c595b3dSSandrine Bailleux * 106c595b3dSSandrine Bailleux * Redistributions in binary form must reproduce the above copyright notice, 116c595b3dSSandrine Bailleux * this list of conditions and the following disclaimer in the documentation 126c595b3dSSandrine Bailleux * and/or other materials provided with the distribution. 136c595b3dSSandrine Bailleux * 146c595b3dSSandrine Bailleux * Neither the name of ARM nor the names of its contributors may be used 156c595b3dSSandrine Bailleux * to endorse or promote products derived from this software without specific 166c595b3dSSandrine Bailleux * prior written permission. 176c595b3dSSandrine Bailleux * 186c595b3dSSandrine Bailleux * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 196c595b3dSSandrine Bailleux * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 206c595b3dSSandrine Bailleux * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 216c595b3dSSandrine Bailleux * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 226c595b3dSSandrine Bailleux * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 236c595b3dSSandrine Bailleux * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 246c595b3dSSandrine Bailleux * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 256c595b3dSSandrine Bailleux * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 266c595b3dSSandrine Bailleux * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 276c595b3dSSandrine Bailleux * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 286c595b3dSSandrine Bailleux * POSSIBILITY OF SUCH DAMAGE. 296c595b3dSSandrine Bailleux */ 306c595b3dSSandrine Bailleux 316c595b3dSSandrine Bailleux#include <arch.h> 326c595b3dSSandrine Bailleux#include <asm_macros.S> 3397043ac9SDan Handley#include <bl_common.h> 3448bfb88eSYatharth Kochar#include <bl1.h> 357baff11fSYatharth Kochar#include <context.h> 366c595b3dSSandrine Bailleux 37e0ae9fabSSandrine Bailleux/* ----------------------------------------------------------------------------- 38e0ae9fabSSandrine Bailleux * Very simple stackless exception handlers used by BL1. 39e0ae9fabSSandrine Bailleux * ----------------------------------------------------------------------------- 40e0ae9fabSSandrine Bailleux */ 416c595b3dSSandrine Bailleux .globl bl1_exceptions 426c595b3dSSandrine Bailleux 43e0ae9fabSSandrine Bailleuxvector_base bl1_exceptions 446c595b3dSSandrine Bailleux 456c595b3dSSandrine Bailleux /* ----------------------------------------------------- 4644804252SSandrine Bailleux * Current EL with SP0 : 0x0 - 0x200 476c595b3dSSandrine Bailleux * ----------------------------------------------------- 486c595b3dSSandrine Bailleux */ 49e0ae9fabSSandrine Bailleuxvector_entry SynchronousExceptionSP0 506c595b3dSSandrine Bailleux mov x0, #SYNC_EXCEPTION_SP_EL0 516c595b3dSSandrine Bailleux bl plat_report_exception 52*a806dad5SJeenu Viswambharan no_ret plat_panic_handler 536c595b3dSSandrine Bailleux check_vector_size SynchronousExceptionSP0 546c595b3dSSandrine Bailleux 55e0ae9fabSSandrine Bailleuxvector_entry IrqSP0 566c595b3dSSandrine Bailleux mov x0, #IRQ_SP_EL0 576c595b3dSSandrine Bailleux bl plat_report_exception 58*a806dad5SJeenu Viswambharan no_ret plat_panic_handler 596c595b3dSSandrine Bailleux check_vector_size IrqSP0 606c595b3dSSandrine Bailleux 61e0ae9fabSSandrine Bailleuxvector_entry FiqSP0 626c595b3dSSandrine Bailleux mov x0, #FIQ_SP_EL0 636c595b3dSSandrine Bailleux bl plat_report_exception 64*a806dad5SJeenu Viswambharan no_ret plat_panic_handler 656c595b3dSSandrine Bailleux check_vector_size FiqSP0 666c595b3dSSandrine Bailleux 67e0ae9fabSSandrine Bailleuxvector_entry SErrorSP0 686c595b3dSSandrine Bailleux mov x0, #SERROR_SP_EL0 696c595b3dSSandrine Bailleux bl plat_report_exception 70*a806dad5SJeenu Viswambharan no_ret plat_panic_handler 716c595b3dSSandrine Bailleux check_vector_size SErrorSP0 726c595b3dSSandrine Bailleux 736c595b3dSSandrine Bailleux /* ----------------------------------------------------- 7444804252SSandrine Bailleux * Current EL with SPx: 0x200 - 0x400 756c595b3dSSandrine Bailleux * ----------------------------------------------------- 766c595b3dSSandrine Bailleux */ 77e0ae9fabSSandrine Bailleuxvector_entry SynchronousExceptionSPx 786c595b3dSSandrine Bailleux mov x0, #SYNC_EXCEPTION_SP_ELX 796c595b3dSSandrine Bailleux bl plat_report_exception 80*a806dad5SJeenu Viswambharan no_ret plat_panic_handler 816c595b3dSSandrine Bailleux check_vector_size SynchronousExceptionSPx 826c595b3dSSandrine Bailleux 83e0ae9fabSSandrine Bailleuxvector_entry IrqSPx 846c595b3dSSandrine Bailleux mov x0, #IRQ_SP_ELX 856c595b3dSSandrine Bailleux bl plat_report_exception 86*a806dad5SJeenu Viswambharan no_ret plat_panic_handler 876c595b3dSSandrine Bailleux check_vector_size IrqSPx 886c595b3dSSandrine Bailleux 89e0ae9fabSSandrine Bailleuxvector_entry FiqSPx 906c595b3dSSandrine Bailleux mov x0, #FIQ_SP_ELX 916c595b3dSSandrine Bailleux bl plat_report_exception 92*a806dad5SJeenu Viswambharan no_ret plat_panic_handler 936c595b3dSSandrine Bailleux check_vector_size FiqSPx 946c595b3dSSandrine Bailleux 95e0ae9fabSSandrine Bailleuxvector_entry SErrorSPx 966c595b3dSSandrine Bailleux mov x0, #SERROR_SP_ELX 976c595b3dSSandrine Bailleux bl plat_report_exception 98*a806dad5SJeenu Viswambharan no_ret plat_panic_handler 996c595b3dSSandrine Bailleux check_vector_size SErrorSPx 1006c595b3dSSandrine Bailleux 1016c595b3dSSandrine Bailleux /* ----------------------------------------------------- 10244804252SSandrine Bailleux * Lower EL using AArch64 : 0x400 - 0x600 1036c595b3dSSandrine Bailleux * ----------------------------------------------------- 1046c595b3dSSandrine Bailleux */ 105e0ae9fabSSandrine Bailleuxvector_entry SynchronousExceptionA64 1060c8d4fefSAchin Gupta /* Enable the SError interrupt */ 1070c8d4fefSAchin Gupta msr daifclr, #DAIF_ABT_BIT 1080c8d4fefSAchin Gupta 1097baff11fSYatharth Kochar str x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR] 1107baff11fSYatharth Kochar 1111fe4d453SSandrine Bailleux /* Expect only SMC exceptions */ 1127baff11fSYatharth Kochar mrs x30, esr_el3 1137baff11fSYatharth Kochar ubfx x30, x30, #ESR_EC_SHIFT, #ESR_EC_LENGTH 1147baff11fSYatharth Kochar cmp x30, #EC_AARCH64_SMC 1151fe4d453SSandrine Bailleux b.ne unexpected_sync_exception 11629fb905dSVikram Kanigiri 1171fe4d453SSandrine Bailleux b smc_handler64 1186c595b3dSSandrine Bailleux check_vector_size SynchronousExceptionA64 1196c595b3dSSandrine Bailleux 120e0ae9fabSSandrine Bailleuxvector_entry IrqA64 1216c595b3dSSandrine Bailleux mov x0, #IRQ_AARCH64 1226c595b3dSSandrine Bailleux bl plat_report_exception 123*a806dad5SJeenu Viswambharan no_ret plat_panic_handler 1246c595b3dSSandrine Bailleux check_vector_size IrqA64 1256c595b3dSSandrine Bailleux 126e0ae9fabSSandrine Bailleuxvector_entry FiqA64 1276c595b3dSSandrine Bailleux mov x0, #FIQ_AARCH64 1286c595b3dSSandrine Bailleux bl plat_report_exception 129*a806dad5SJeenu Viswambharan no_ret plat_panic_handler 1306c595b3dSSandrine Bailleux check_vector_size FiqA64 1316c595b3dSSandrine Bailleux 132e0ae9fabSSandrine Bailleuxvector_entry SErrorA64 1336c595b3dSSandrine Bailleux mov x0, #SERROR_AARCH64 1346c595b3dSSandrine Bailleux bl plat_report_exception 135*a806dad5SJeenu Viswambharan no_ret plat_panic_handler 1366c595b3dSSandrine Bailleux check_vector_size SErrorA64 1376c595b3dSSandrine Bailleux 1386c595b3dSSandrine Bailleux /* ----------------------------------------------------- 13944804252SSandrine Bailleux * Lower EL using AArch32 : 0x600 - 0x800 1406c595b3dSSandrine Bailleux * ----------------------------------------------------- 1416c595b3dSSandrine Bailleux */ 142e0ae9fabSSandrine Bailleuxvector_entry SynchronousExceptionA32 1436c595b3dSSandrine Bailleux mov x0, #SYNC_EXCEPTION_AARCH32 1446c595b3dSSandrine Bailleux bl plat_report_exception 145*a806dad5SJeenu Viswambharan no_ret plat_panic_handler 1466c595b3dSSandrine Bailleux check_vector_size SynchronousExceptionA32 1476c595b3dSSandrine Bailleux 148e0ae9fabSSandrine Bailleuxvector_entry IrqA32 1496c595b3dSSandrine Bailleux mov x0, #IRQ_AARCH32 1506c595b3dSSandrine Bailleux bl plat_report_exception 151*a806dad5SJeenu Viswambharan no_ret plat_panic_handler 1526c595b3dSSandrine Bailleux check_vector_size IrqA32 1536c595b3dSSandrine Bailleux 154e0ae9fabSSandrine Bailleuxvector_entry FiqA32 1556c595b3dSSandrine Bailleux mov x0, #FIQ_AARCH32 1566c595b3dSSandrine Bailleux bl plat_report_exception 157*a806dad5SJeenu Viswambharan no_ret plat_panic_handler 1586c595b3dSSandrine Bailleux check_vector_size FiqA32 1596c595b3dSSandrine Bailleux 160e0ae9fabSSandrine Bailleuxvector_entry SErrorA32 1616c595b3dSSandrine Bailleux mov x0, #SERROR_AARCH32 1626c595b3dSSandrine Bailleux bl plat_report_exception 163*a806dad5SJeenu Viswambharan no_ret plat_panic_handler 1646c595b3dSSandrine Bailleux check_vector_size SErrorA32 1651fe4d453SSandrine Bailleux 1661fe4d453SSandrine Bailleux 1671fe4d453SSandrine Bailleuxfunc smc_handler64 16848bfb88eSYatharth Kochar 1697baff11fSYatharth Kochar /* ---------------------------------------------- 17048bfb88eSYatharth Kochar * Detect if this is a RUN_IMAGE or other SMC. 17148bfb88eSYatharth Kochar * ---------------------------------------------- 17248bfb88eSYatharth Kochar */ 17348bfb88eSYatharth Kochar mov x30, #BL1_SMC_RUN_IMAGE 17448bfb88eSYatharth Kochar cmp x30, x0 17548bfb88eSYatharth Kochar b.ne smc_handler 17648bfb88eSYatharth Kochar 17748bfb88eSYatharth Kochar /* ------------------------------------------------ 17848bfb88eSYatharth Kochar * Make sure only Secure world reaches here. 17948bfb88eSYatharth Kochar * ------------------------------------------------ 18048bfb88eSYatharth Kochar */ 18148bfb88eSYatharth Kochar mrs x30, scr_el3 18248bfb88eSYatharth Kochar tst x30, #SCR_NS_BIT 18348bfb88eSYatharth Kochar b.ne unexpected_sync_exception 18448bfb88eSYatharth Kochar 18548bfb88eSYatharth Kochar /* ---------------------------------------------- 18648bfb88eSYatharth Kochar * Handling RUN_IMAGE SMC. First switch back to 18748bfb88eSYatharth Kochar * SP_EL0 for the C runtime stack. 1887baff11fSYatharth Kochar * ---------------------------------------------- 1897baff11fSYatharth Kochar */ 1907baff11fSYatharth Kochar ldr x30, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP] 1917baff11fSYatharth Kochar msr spsel, #0 1927baff11fSYatharth Kochar mov sp, x30 1937baff11fSYatharth Kochar 1941fe4d453SSandrine Bailleux /* --------------------------------------------------------------------- 195f3b4914bSYatharth Kochar * Pass EL3 control to next BL image. 19648bfb88eSYatharth Kochar * Here it expects X1 with the address of a entry_point_info_t 197f3b4914bSYatharth Kochar * structure describing the next BL image entrypoint. 1981fe4d453SSandrine Bailleux * --------------------------------------------------------------------- 1991fe4d453SSandrine Bailleux */ 2001fe4d453SSandrine Bailleux mov x20, x1 2011fe4d453SSandrine Bailleux 2021fe4d453SSandrine Bailleux mov x0, x20 203f3b4914bSYatharth Kochar bl bl1_print_next_bl_ep_info 2041fe4d453SSandrine Bailleux 2051fe4d453SSandrine Bailleux ldp x0, x1, [x20, #ENTRY_POINT_INFO_PC_OFFSET] 2061fe4d453SSandrine Bailleux msr elr_el3, x0 2071fe4d453SSandrine Bailleux msr spsr_el3, x1 2081fe4d453SSandrine Bailleux ubfx x0, x1, #MODE_EL_SHIFT, #2 2091fe4d453SSandrine Bailleux cmp x0, #MODE_EL3 2101fe4d453SSandrine Bailleux b.ne unexpected_sync_exception 2111fe4d453SSandrine Bailleux 2121fe4d453SSandrine Bailleux bl disable_mmu_icache_el3 2131fe4d453SSandrine Bailleux tlbi alle3 2141fe4d453SSandrine Bailleux 21535e8c766SSandrine Bailleux#if SPIN_ON_BL1_EXIT 21635e8c766SSandrine Bailleux bl print_debug_loop_message 21735e8c766SSandrine Bailleuxdebug_loop: 21835e8c766SSandrine Bailleux b debug_loop 21935e8c766SSandrine Bailleux#endif 22035e8c766SSandrine Bailleux 221862b5dc2SSandrine Bailleux mov x0, x20 222e3f67124SJuan Castillo bl bl1_plat_prepare_exit 223e3f67124SJuan Castillo 2241fe4d453SSandrine Bailleux ldp x6, x7, [x20, #(ENTRY_POINT_INFO_ARGS_OFFSET + 0x30)] 2251fe4d453SSandrine Bailleux ldp x4, x5, [x20, #(ENTRY_POINT_INFO_ARGS_OFFSET + 0x20)] 2261fe4d453SSandrine Bailleux ldp x2, x3, [x20, #(ENTRY_POINT_INFO_ARGS_OFFSET + 0x10)] 2271fe4d453SSandrine Bailleux ldp x0, x1, [x20, #(ENTRY_POINT_INFO_ARGS_OFFSET + 0x0)] 2281fe4d453SSandrine Bailleux eret 2291fe4d453SSandrine Bailleuxendfunc smc_handler64 2301fe4d453SSandrine Bailleux 2311fe4d453SSandrine Bailleuxunexpected_sync_exception: 2321fe4d453SSandrine Bailleux mov x0, #SYNC_EXCEPTION_AARCH64 2331fe4d453SSandrine Bailleux bl plat_report_exception 234*a806dad5SJeenu Viswambharan no_ret plat_panic_handler 23548bfb88eSYatharth Kochar 23648bfb88eSYatharth Kochar /* ----------------------------------------------------- 23748bfb88eSYatharth Kochar * Save Secure/Normal world context and jump to 23848bfb88eSYatharth Kochar * BL1 SMC handler. 23948bfb88eSYatharth Kochar * ----------------------------------------------------- 24048bfb88eSYatharth Kochar */ 24148bfb88eSYatharth Kocharsmc_handler: 24248bfb88eSYatharth Kochar /* ----------------------------------------------------- 24348bfb88eSYatharth Kochar * Save the GP registers x0-x29. 24448bfb88eSYatharth Kochar * TODO: Revisit to store only SMCC specified registers. 24548bfb88eSYatharth Kochar * ----------------------------------------------------- 24648bfb88eSYatharth Kochar */ 24748bfb88eSYatharth Kochar bl save_gp_registers 24848bfb88eSYatharth Kochar 24948bfb88eSYatharth Kochar /* ----------------------------------------------------- 25048bfb88eSYatharth Kochar * Populate the parameters for the SMC handler. We 25148bfb88eSYatharth Kochar * already have x0-x4 in place. x5 will point to a 25248bfb88eSYatharth Kochar * cookie (not used now). x6 will point to the context 25348bfb88eSYatharth Kochar * structure (SP_EL3) and x7 will contain flags we need 25448bfb88eSYatharth Kochar * to pass to the handler. 25548bfb88eSYatharth Kochar * ----------------------------------------------------- 25648bfb88eSYatharth Kochar */ 25748bfb88eSYatharth Kochar mov x5, xzr 25848bfb88eSYatharth Kochar mov x6, sp 25948bfb88eSYatharth Kochar 26048bfb88eSYatharth Kochar /* ----------------------------------------------------- 26148bfb88eSYatharth Kochar * Restore the saved C runtime stack value which will 26248bfb88eSYatharth Kochar * become the new SP_EL0 i.e. EL3 runtime stack. It was 26348bfb88eSYatharth Kochar * saved in the 'cpu_context' structure prior to the last 26448bfb88eSYatharth Kochar * ERET from EL3. 26548bfb88eSYatharth Kochar * ----------------------------------------------------- 26648bfb88eSYatharth Kochar */ 26748bfb88eSYatharth Kochar ldr x12, [x6, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP] 26848bfb88eSYatharth Kochar 26948bfb88eSYatharth Kochar /* --------------------------------------------- 27048bfb88eSYatharth Kochar * Switch back to SP_EL0 for the C runtime stack. 27148bfb88eSYatharth Kochar * --------------------------------------------- 27248bfb88eSYatharth Kochar */ 27348bfb88eSYatharth Kochar msr spsel, #0 27448bfb88eSYatharth Kochar mov sp, x12 27548bfb88eSYatharth Kochar 27648bfb88eSYatharth Kochar /* ----------------------------------------------------- 27748bfb88eSYatharth Kochar * Save the SPSR_EL3, ELR_EL3, & SCR_EL3 in case there 27848bfb88eSYatharth Kochar * is a world switch during SMC handling. 27948bfb88eSYatharth Kochar * ----------------------------------------------------- 28048bfb88eSYatharth Kochar */ 28148bfb88eSYatharth Kochar mrs x16, spsr_el3 28248bfb88eSYatharth Kochar mrs x17, elr_el3 28348bfb88eSYatharth Kochar mrs x18, scr_el3 28448bfb88eSYatharth Kochar stp x16, x17, [x6, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3] 28548bfb88eSYatharth Kochar str x18, [x6, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3] 28648bfb88eSYatharth Kochar 28748bfb88eSYatharth Kochar /* Copy SCR_EL3.NS bit to the flag to indicate caller's security */ 28848bfb88eSYatharth Kochar bfi x7, x18, #0, #1 28948bfb88eSYatharth Kochar 29048bfb88eSYatharth Kochar /* ----------------------------------------------------- 29148bfb88eSYatharth Kochar * Go to BL1 SMC handler. 29248bfb88eSYatharth Kochar * ----------------------------------------------------- 29348bfb88eSYatharth Kochar */ 29448bfb88eSYatharth Kochar bl bl1_smc_handler 29548bfb88eSYatharth Kochar 29648bfb88eSYatharth Kochar /* ----------------------------------------------------- 29748bfb88eSYatharth Kochar * Do the transition to next BL image. 29848bfb88eSYatharth Kochar * ----------------------------------------------------- 29948bfb88eSYatharth Kochar */ 30048bfb88eSYatharth Kochar b el3_exit 301