xref: /rk3399_ARM-atf/bl1/aarch64/bl1_exceptions.S (revision 1d6d6802dd547c8b378a9a47572ee72e68cceb3b)
16c595b3dSSandrine Bailleux/*
297215e0fSDaniel Boulby * Copyright (c) 2013-2022, ARM Limited and Contributors. All rights reserved.
36c595b3dSSandrine Bailleux *
482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause
56c595b3dSSandrine Bailleux */
66c595b3dSSandrine Bailleux
76c595b3dSSandrine Bailleux#include <arch.h>
86c595b3dSSandrine Bailleux#include <asm_macros.S>
909d40e0eSAntonio Nino Diaz#include <bl1/bl1.h>
1009d40e0eSAntonio Nino Diaz#include <common/bl_common.h>
117baff11fSYatharth Kochar#include <context.h>
126c595b3dSSandrine Bailleux
13e0ae9fabSSandrine Bailleux/* -----------------------------------------------------------------------------
14e0ae9fabSSandrine Bailleux * Very simple stackless exception handlers used by BL1.
15e0ae9fabSSandrine Bailleux * -----------------------------------------------------------------------------
16e0ae9fabSSandrine Bailleux */
176c595b3dSSandrine Bailleux	.globl	bl1_exceptions
186c595b3dSSandrine Bailleux
19e0ae9fabSSandrine Bailleuxvector_base bl1_exceptions
206c595b3dSSandrine Bailleux
216c595b3dSSandrine Bailleux	/* -----------------------------------------------------
2244804252SSandrine Bailleux	 * Current EL with SP0 : 0x0 - 0x200
236c595b3dSSandrine Bailleux	 * -----------------------------------------------------
246c595b3dSSandrine Bailleux	 */
25e0ae9fabSSandrine Bailleuxvector_entry SynchronousExceptionSP0
266c595b3dSSandrine Bailleux	mov	x0, #SYNC_EXCEPTION_SP_EL0
276c595b3dSSandrine Bailleux	bl	plat_report_exception
28a806dad5SJeenu Viswambharan	no_ret	plat_panic_handler
29a9203edaSRoberto Vargasend_vector_entry SynchronousExceptionSP0
306c595b3dSSandrine Bailleux
31e0ae9fabSSandrine Bailleuxvector_entry IrqSP0
326c595b3dSSandrine Bailleux	mov	x0, #IRQ_SP_EL0
336c595b3dSSandrine Bailleux	bl	plat_report_exception
34a806dad5SJeenu Viswambharan	no_ret	plat_panic_handler
35a9203edaSRoberto Vargasend_vector_entry IrqSP0
366c595b3dSSandrine Bailleux
37e0ae9fabSSandrine Bailleuxvector_entry FiqSP0
386c595b3dSSandrine Bailleux	mov	x0, #FIQ_SP_EL0
396c595b3dSSandrine Bailleux	bl	plat_report_exception
40a806dad5SJeenu Viswambharan	no_ret	plat_panic_handler
41a9203edaSRoberto Vargasend_vector_entry FiqSP0
426c595b3dSSandrine Bailleux
43e0ae9fabSSandrine Bailleuxvector_entry SErrorSP0
446c595b3dSSandrine Bailleux	mov	x0, #SERROR_SP_EL0
456c595b3dSSandrine Bailleux	bl	plat_report_exception
46a806dad5SJeenu Viswambharan	no_ret	plat_panic_handler
47a9203edaSRoberto Vargasend_vector_entry SErrorSP0
486c595b3dSSandrine Bailleux
496c595b3dSSandrine Bailleux	/* -----------------------------------------------------
5044804252SSandrine Bailleux	 * Current EL with SPx: 0x200 - 0x400
516c595b3dSSandrine Bailleux	 * -----------------------------------------------------
526c595b3dSSandrine Bailleux	 */
53e0ae9fabSSandrine Bailleuxvector_entry SynchronousExceptionSPx
546c595b3dSSandrine Bailleux	mov	x0, #SYNC_EXCEPTION_SP_ELX
556c595b3dSSandrine Bailleux	bl	plat_report_exception
56a806dad5SJeenu Viswambharan	no_ret	plat_panic_handler
57a9203edaSRoberto Vargasend_vector_entry SynchronousExceptionSPx
586c595b3dSSandrine Bailleux
59e0ae9fabSSandrine Bailleuxvector_entry IrqSPx
606c595b3dSSandrine Bailleux	mov	x0, #IRQ_SP_ELX
616c595b3dSSandrine Bailleux	bl	plat_report_exception
62a806dad5SJeenu Viswambharan	no_ret	plat_panic_handler
63a9203edaSRoberto Vargasend_vector_entry IrqSPx
646c595b3dSSandrine Bailleux
65e0ae9fabSSandrine Bailleuxvector_entry FiqSPx
666c595b3dSSandrine Bailleux	mov	x0, #FIQ_SP_ELX
676c595b3dSSandrine Bailleux	bl	plat_report_exception
68a806dad5SJeenu Viswambharan	no_ret	plat_panic_handler
69a9203edaSRoberto Vargasend_vector_entry FiqSPx
706c595b3dSSandrine Bailleux
71e0ae9fabSSandrine Bailleuxvector_entry SErrorSPx
726c595b3dSSandrine Bailleux	mov	x0, #SERROR_SP_ELX
736c595b3dSSandrine Bailleux	bl	plat_report_exception
74a806dad5SJeenu Viswambharan	no_ret	plat_panic_handler
75a9203edaSRoberto Vargasend_vector_entry SErrorSPx
766c595b3dSSandrine Bailleux
776c595b3dSSandrine Bailleux	/* -----------------------------------------------------
7844804252SSandrine Bailleux	 * Lower EL using AArch64 : 0x400 - 0x600
796c595b3dSSandrine Bailleux	 * -----------------------------------------------------
806c595b3dSSandrine Bailleux	 */
81e0ae9fabSSandrine Bailleuxvector_entry SynchronousExceptionA64
820c8d4fefSAchin Gupta	/* Enable the SError interrupt */
830c8d4fefSAchin Gupta	msr	daifclr, #DAIF_ABT_BIT
840c8d4fefSAchin Gupta
857baff11fSYatharth Kochar	str	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
867baff11fSYatharth Kochar
871fe4d453SSandrine Bailleux	/* Expect only SMC exceptions */
887baff11fSYatharth Kochar	mrs	x30, esr_el3
897baff11fSYatharth Kochar	ubfx	x30, x30, #ESR_EC_SHIFT, #ESR_EC_LENGTH
907baff11fSYatharth Kochar	cmp	x30, #EC_AARCH64_SMC
911fe4d453SSandrine Bailleux	b.ne	unexpected_sync_exception
9229fb905dSVikram Kanigiri
931fe4d453SSandrine Bailleux	b	smc_handler64
94a9203edaSRoberto Vargasend_vector_entry SynchronousExceptionA64
956c595b3dSSandrine Bailleux
96e0ae9fabSSandrine Bailleuxvector_entry IrqA64
976c595b3dSSandrine Bailleux	mov	x0, #IRQ_AARCH64
986c595b3dSSandrine Bailleux	bl	plat_report_exception
99a806dad5SJeenu Viswambharan	no_ret	plat_panic_handler
100a9203edaSRoberto Vargasend_vector_entry IrqA64
1016c595b3dSSandrine Bailleux
102e0ae9fabSSandrine Bailleuxvector_entry FiqA64
1036c595b3dSSandrine Bailleux	mov	x0, #FIQ_AARCH64
1046c595b3dSSandrine Bailleux	bl	plat_report_exception
105a806dad5SJeenu Viswambharan	no_ret	plat_panic_handler
106a9203edaSRoberto Vargasend_vector_entry FiqA64
1076c595b3dSSandrine Bailleux
108e0ae9fabSSandrine Bailleuxvector_entry SErrorA64
1096c595b3dSSandrine Bailleux	mov	x0, #SERROR_AARCH64
1106c595b3dSSandrine Bailleux	bl	plat_report_exception
111a806dad5SJeenu Viswambharan	no_ret	plat_panic_handler
112a9203edaSRoberto Vargasend_vector_entry SErrorA64
1136c595b3dSSandrine Bailleux
1146c595b3dSSandrine Bailleux	/* -----------------------------------------------------
11544804252SSandrine Bailleux	 * Lower EL using AArch32 : 0x600 - 0x800
1166c595b3dSSandrine Bailleux	 * -----------------------------------------------------
1176c595b3dSSandrine Bailleux	 */
118e0ae9fabSSandrine Bailleuxvector_entry SynchronousExceptionA32
1196c595b3dSSandrine Bailleux	mov	x0, #SYNC_EXCEPTION_AARCH32
1206c595b3dSSandrine Bailleux	bl	plat_report_exception
121a806dad5SJeenu Viswambharan	no_ret	plat_panic_handler
122a9203edaSRoberto Vargasend_vector_entry SynchronousExceptionA32
1236c595b3dSSandrine Bailleux
124e0ae9fabSSandrine Bailleuxvector_entry IrqA32
1256c595b3dSSandrine Bailleux	mov	x0, #IRQ_AARCH32
1266c595b3dSSandrine Bailleux	bl	plat_report_exception
127a806dad5SJeenu Viswambharan	no_ret	plat_panic_handler
128a9203edaSRoberto Vargasend_vector_entry IrqA32
1296c595b3dSSandrine Bailleux
130e0ae9fabSSandrine Bailleuxvector_entry FiqA32
1316c595b3dSSandrine Bailleux	mov	x0, #FIQ_AARCH32
1326c595b3dSSandrine Bailleux	bl	plat_report_exception
133a806dad5SJeenu Viswambharan	no_ret	plat_panic_handler
134a9203edaSRoberto Vargasend_vector_entry FiqA32
1356c595b3dSSandrine Bailleux
136e0ae9fabSSandrine Bailleuxvector_entry SErrorA32
1376c595b3dSSandrine Bailleux	mov	x0, #SERROR_AARCH32
1386c595b3dSSandrine Bailleux	bl	plat_report_exception
139a806dad5SJeenu Viswambharan	no_ret	plat_panic_handler
140a9203edaSRoberto Vargasend_vector_entry SErrorA32
1411fe4d453SSandrine Bailleux
1421fe4d453SSandrine Bailleux
1431fe4d453SSandrine Bailleuxfunc smc_handler64
14448bfb88eSYatharth Kochar
1457baff11fSYatharth Kochar	/* ----------------------------------------------
14648bfb88eSYatharth Kochar	 * Detect if this is a RUN_IMAGE or other SMC.
14748bfb88eSYatharth Kochar	 * ----------------------------------------------
14848bfb88eSYatharth Kochar	 */
14948bfb88eSYatharth Kochar	mov	x30, #BL1_SMC_RUN_IMAGE
15048bfb88eSYatharth Kochar	cmp	x30, x0
15148bfb88eSYatharth Kochar	b.ne	smc_handler
15248bfb88eSYatharth Kochar
15348bfb88eSYatharth Kochar	/* ------------------------------------------------
15448bfb88eSYatharth Kochar	 * Make sure only Secure world reaches here.
15548bfb88eSYatharth Kochar	 * ------------------------------------------------
15648bfb88eSYatharth Kochar	 */
15748bfb88eSYatharth Kochar	mrs	x30, scr_el3
15848bfb88eSYatharth Kochar	tst	x30, #SCR_NS_BIT
15948bfb88eSYatharth Kochar	b.ne	unexpected_sync_exception
16048bfb88eSYatharth Kochar
16148bfb88eSYatharth Kochar	/* ----------------------------------------------
16248bfb88eSYatharth Kochar	 * Handling RUN_IMAGE SMC. First switch back to
16348bfb88eSYatharth Kochar	 * SP_EL0 for the C runtime stack.
1647baff11fSYatharth Kochar	 * ----------------------------------------------
1657baff11fSYatharth Kochar	 */
1667baff11fSYatharth Kochar	ldr	x30, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
167ed108b56SAlexei Fedorov	msr	spsel, #MODE_SP_EL0
1687baff11fSYatharth Kochar	mov	sp, x30
1697baff11fSYatharth Kochar
1701fe4d453SSandrine Bailleux	/* ---------------------------------------------------------------------
171f3b4914bSYatharth Kochar	 * Pass EL3 control to next BL image.
17248bfb88eSYatharth Kochar	 * Here it expects X1 with the address of a entry_point_info_t
173f3b4914bSYatharth Kochar	 * structure describing the next BL image entrypoint.
1741fe4d453SSandrine Bailleux	 * ---------------------------------------------------------------------
1751fe4d453SSandrine Bailleux	 */
1761fe4d453SSandrine Bailleux	mov	x20, x1
1771fe4d453SSandrine Bailleux
1781fe4d453SSandrine Bailleux	mov	x0, x20
179f3b4914bSYatharth Kochar	bl	bl1_print_next_bl_ep_info
1801fe4d453SSandrine Bailleux
1811fe4d453SSandrine Bailleux	ldp	x0, x1, [x20, #ENTRY_POINT_INFO_PC_OFFSET]
1821fe4d453SSandrine Bailleux	msr	elr_el3, x0
1831fe4d453SSandrine Bailleux	msr	spsr_el3, x1
1841fe4d453SSandrine Bailleux	ubfx	x0, x1, #MODE_EL_SHIFT, #2
1851fe4d453SSandrine Bailleux	cmp	x0, #MODE_EL3
1861fe4d453SSandrine Bailleux	b.ne	unexpected_sync_exception
1871fe4d453SSandrine Bailleux
1881fe4d453SSandrine Bailleux	bl	disable_mmu_icache_el3
1891fe4d453SSandrine Bailleux	tlbi	alle3
1906bf0e079SAntonio Nino Diaz	dsb	ish /* ERET implies ISB, so it is not needed here */
1911fe4d453SSandrine Bailleux
19235e8c766SSandrine Bailleux#if SPIN_ON_BL1_EXIT
19335e8c766SSandrine Bailleux	bl	print_debug_loop_message
19435e8c766SSandrine Bailleuxdebug_loop:
19535e8c766SSandrine Bailleux	b	debug_loop
19635e8c766SSandrine Bailleux#endif
19735e8c766SSandrine Bailleux
198862b5dc2SSandrine Bailleux	mov	x0, x20
199e3f67124SJuan Castillo	bl	bl1_plat_prepare_exit
200e3f67124SJuan Castillo
2011fe4d453SSandrine Bailleux	ldp	x6, x7, [x20, #(ENTRY_POINT_INFO_ARGS_OFFSET + 0x30)]
2021fe4d453SSandrine Bailleux	ldp	x4, x5, [x20, #(ENTRY_POINT_INFO_ARGS_OFFSET + 0x20)]
2031fe4d453SSandrine Bailleux	ldp	x2, x3, [x20, #(ENTRY_POINT_INFO_ARGS_OFFSET + 0x10)]
2041fe4d453SSandrine Bailleux	ldp	x0, x1, [x20, #(ENTRY_POINT_INFO_ARGS_OFFSET + 0x0)]
205f461fe34SAnthony Steinhauser	exception_return
2061fe4d453SSandrine Bailleuxendfunc smc_handler64
2071fe4d453SSandrine Bailleux
2081fe4d453SSandrine Bailleuxunexpected_sync_exception:
2091fe4d453SSandrine Bailleux	mov	x0, #SYNC_EXCEPTION_AARCH64
2101fe4d453SSandrine Bailleux	bl	plat_report_exception
211a806dad5SJeenu Viswambharan	no_ret	plat_panic_handler
21248bfb88eSYatharth Kochar
21348bfb88eSYatharth Kochar	/* -----------------------------------------------------
21448bfb88eSYatharth Kochar	 * Save Secure/Normal world context and jump to
21548bfb88eSYatharth Kochar	 * BL1 SMC handler.
21648bfb88eSYatharth Kochar	 * -----------------------------------------------------
21748bfb88eSYatharth Kochar	 */
21848bfb88eSYatharth Kocharsmc_handler:
21948bfb88eSYatharth Kochar	/* -----------------------------------------------------
220ed108b56SAlexei Fedorov	 * Save x0-x29 and ARMv8.3-PAuth (if enabled) registers.
221*1d6d6802SBoyan Karatotev	 * Save PMCR_EL0 and disable Cycle Counter.
222085e80ecSAntonio Nino Diaz	 * TODO: Revisit to store only SMCCC specified registers.
22348bfb88eSYatharth Kochar	 * -----------------------------------------------------
22448bfb88eSYatharth Kochar	 */
22597215e0fSDaniel Boulby	bl	prepare_el3_entry
226e290a8fcSAlexei Fedorov
227530ceda5SAlexei Fedorov#if ENABLE_PAUTH
228530ceda5SAlexei Fedorov	/* -----------------------------------------------------
229530ceda5SAlexei Fedorov	 * Load and program stored APIAKey firmware key.
230530ceda5SAlexei Fedorov	 * Re-enable pointer authentication in EL3, as it was
231530ceda5SAlexei Fedorov	 * disabled before jumping to the next boot image.
232530ceda5SAlexei Fedorov	 * -----------------------------------------------------
233530ceda5SAlexei Fedorov	 */
234530ceda5SAlexei Fedorov	bl	pauth_load_bl1_apiakey_enable
235530ceda5SAlexei Fedorov#endif
236e290a8fcSAlexei Fedorov	/* -----------------------------------------------------
23748bfb88eSYatharth Kochar	 * Populate the parameters for the SMC handler. We
23848bfb88eSYatharth Kochar	 * already have x0-x4 in place. x5 will point to a
23948bfb88eSYatharth Kochar	 * cookie (not used now). x6 will point to the context
24048bfb88eSYatharth Kochar	 * structure (SP_EL3) and x7 will contain flags we need
24148bfb88eSYatharth Kochar	 * to pass to the handler.
24248bfb88eSYatharth Kochar	 * -----------------------------------------------------
24348bfb88eSYatharth Kochar	 */
24448bfb88eSYatharth Kochar	mov	x5, xzr
24548bfb88eSYatharth Kochar	mov	x6, sp
24648bfb88eSYatharth Kochar
24748bfb88eSYatharth Kochar	/* -----------------------------------------------------
24848bfb88eSYatharth Kochar	 * Restore the saved C runtime stack value which will
24948bfb88eSYatharth Kochar	 * become the new SP_EL0 i.e. EL3 runtime stack. It was
25048bfb88eSYatharth Kochar	 * saved in the 'cpu_context' structure prior to the last
25148bfb88eSYatharth Kochar	 * ERET from EL3.
25248bfb88eSYatharth Kochar	 * -----------------------------------------------------
25348bfb88eSYatharth Kochar	 */
25448bfb88eSYatharth Kochar	ldr	x12, [x6, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
25548bfb88eSYatharth Kochar
25648bfb88eSYatharth Kochar	/* ---------------------------------------------
25748bfb88eSYatharth Kochar	 * Switch back to SP_EL0 for the C runtime stack.
25848bfb88eSYatharth Kochar	 * ---------------------------------------------
25948bfb88eSYatharth Kochar	 */
260ed108b56SAlexei Fedorov	msr	spsel, #MODE_SP_EL0
26148bfb88eSYatharth Kochar	mov	sp, x12
26248bfb88eSYatharth Kochar
26348bfb88eSYatharth Kochar	/* -----------------------------------------------------
26448bfb88eSYatharth Kochar	 * Save the SPSR_EL3, ELR_EL3, & SCR_EL3 in case there
26548bfb88eSYatharth Kochar	 * is a world switch during SMC handling.
26648bfb88eSYatharth Kochar	 * -----------------------------------------------------
26748bfb88eSYatharth Kochar	 */
26848bfb88eSYatharth Kochar	mrs	x16, spsr_el3
26948bfb88eSYatharth Kochar	mrs	x17, elr_el3
27048bfb88eSYatharth Kochar	mrs	x18, scr_el3
27148bfb88eSYatharth Kochar	stp	x16, x17, [x6, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
27248bfb88eSYatharth Kochar	str	x18, [x6, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3]
27348bfb88eSYatharth Kochar
27448bfb88eSYatharth Kochar	/* Copy SCR_EL3.NS bit to the flag to indicate caller's security */
27548bfb88eSYatharth Kochar	bfi	x7, x18, #0, #1
27648bfb88eSYatharth Kochar
27748bfb88eSYatharth Kochar	/* -----------------------------------------------------
27848bfb88eSYatharth Kochar	 * Go to BL1 SMC handler.
27948bfb88eSYatharth Kochar	 * -----------------------------------------------------
28048bfb88eSYatharth Kochar	 */
28148bfb88eSYatharth Kochar	bl	bl1_smc_handler
28248bfb88eSYatharth Kochar
28348bfb88eSYatharth Kochar	/* -----------------------------------------------------
28448bfb88eSYatharth Kochar	 * Do the transition to next BL image.
28548bfb88eSYatharth Kochar	 * -----------------------------------------------------
28648bfb88eSYatharth Kochar	 */
28748bfb88eSYatharth Kochar	b	el3_exit
288