blob: ee5d8d969f937814d46a9b6b7f5507d0bf982f60 [file] [log] [blame]
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +01001/*
Boyan Karatotev1dcba8f2024-11-19 11:27:01 +00002 * Copyright (c) 2015-2025, Arm Limited and Contributors. All rights reserved.
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +01003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +01005 */
6
Antonio Nino Diaz5eb88372018-11-08 10:20:19 +00007#ifndef EL3_COMMON_MACROS_S
8#define EL3_COMMON_MACROS_S
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +01009
10#include <arch.h>
11#include <asm_macros.S>
Daniel Boulby928747f2021-05-25 18:09:34 +010012#include <assert_macros.S>
Manish V Badarkhee07e8082020-07-23 12:43:25 +010013#include <context.h>
Varun Wadekar5dc9e9c2020-05-16 20:59:30 -070014#include <lib/xlat_tables/xlat_tables_defs.h>
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +010015
16 /*
17 * Helper macro to initialise EL3 registers we care about.
18 */
Dimitris Papastamos446f7f12017-11-30 14:53:53 +000019 .macro el3_arch_init_common
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +010020 /* ---------------------------------------------------------------------
David Cunadofee86532017-04-13 22:38:29 +010021 * SCTLR_EL3 has already been initialised - read current value before
22 * modifying.
23 *
24 * SCTLR_EL3.I: Enable the instruction cache.
25 *
Qixiang Xu3cc39172018-03-05 09:31:11 +080026 * SCTLR_EL3.SA: Enable Stack Alignment check. A SP alignment fault
David Cunadofee86532017-04-13 22:38:29 +010027 * exception is generated if a load or store instruction executed at
28 * EL3 uses the SP as the base address and the SP is not aligned to a
29 * 16-byte boundary.
30 *
31 * SCTLR_EL3.A: Enable Alignment fault checking. All instructions that
32 * load or store one or more registers have an alignment check that the
33 * address being accessed is aligned to the size of the data element(s)
34 * being accessed.
Boyan Karatotev7a5246e2025-03-26 15:54:55 +000035 *
36 * SCTLR_EL3.BT: PAuth instructions are compatible with bti jc
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +010037 * ---------------------------------------------------------------------
38 */
Boyan Karatotev7a5246e2025-03-26 15:54:55 +000039 mov_imm x1, (SCTLR_I_BIT | SCTLR_A_BIT | SCTLR_SA_BIT)
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +010040 mrs x0, sctlr_el3
Boyan Karatotev7a5246e2025-03-26 15:54:55 +000041#if ENABLE_BTI
42 bic x0, x0, #SCTLR_BT_BIT
43#endif
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +010044 orr x0, x0, x1
45 msr sctlr_el3, x0
46 isb
47
John Powell9c726562025-03-10 20:09:03 -050048#if ENABLE_FEAT_SCTLR2
49#if ENABLE_FEAT_SCTLR2 > 1
50 is_feat_sctlr2_present_asm x1
51 beq feat_sctlr2_not_supported\@
52#endif
53 mov x1, #SCTLR2_RESET_VAL
54 msr SCTLR2_EL3, x1
55feat_sctlr2_not_supported\@:
56#endif
57
Masahiro Yamada441bfdd2016-12-25 23:36:24 +090058#ifdef IMAGE_BL31
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +010059 /* ---------------------------------------------------------------------
60 * Initialise the per-cpu cache pointer to the CPU.
61 * This is done early to enable crash reporting to have access to crash
62 * stack. Since crash reporting depends on cpu_data to report the
63 * unhandled exception, not doing so can lead to recursive exceptions
64 * due to a NULL TPIDR_EL3.
65 * ---------------------------------------------------------------------
66 */
Boyan Karatotev97476aa2024-11-19 11:27:01 +000067 bl plat_my_core_pos
Boyan Karatotev2844d762025-03-18 10:31:22 +000068 /* index into the cpu_data */
69 mov_imm x1, CPU_DATA_SIZE
70 mul x0, x0, x1
71 adr_l x1, percpu_data
72 add x0, x0, x1
Boyan Karatotev97476aa2024-11-19 11:27:01 +000073 msr tpidr_el3, x0
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +010074#endif /* IMAGE_BL31 */
75
76 /* ---------------------------------------------------------------------
David Cunadofee86532017-04-13 22:38:29 +010077 * Initialise SCR_EL3, setting all fields rather than relying on hw.
78 * All fields are architecturally UNKNOWN on reset. The following fields
79 * do not change during the TF lifetime. The remaining fields are set to
80 * zero here but are updated ahead of transitioning to a lower EL in the
81 * function cm_init_context_common().
82 *
Manish Pandey71af7f12024-01-29 21:17:33 +000083 * SCR_EL3.EEL2: Set to one if S-EL2 is present and enabled.
84 *
85 * NOTE: Modifying EEL2 bit along with EA bit ensures that we mitigate
86 * against ERRATA_V2_3099206.
Gerald Lejeune632d6df2016-03-22 09:29:23 +010087 * ---------------------------------------------------------------------
88 */
Jayanth Dodderi Chidanandb4590652023-08-08 16:10:16 +010089 mov_imm x0, SCR_RESET_VAL
Manish Pandey71af7f12024-01-29 21:17:33 +000090#if IMAGE_BL31 && defined(SPD_spmd) && SPMD_SPM_AT_SEL2
Jayanth Dodderi Chidanandb4590652023-08-08 16:10:16 +010091 mrs x1, id_aa64pfr0_el1
92 and x1, x1, #(ID_AA64PFR0_SEL2_MASK << ID_AA64PFR0_SEL2_SHIFT)
93 cbz x1, 1f
94 orr x0, x0, #SCR_EEL2_BIT
Manish Pandey71af7f12024-01-29 21:17:33 +000095#endif
961:
Gerald Lejeune632d6df2016-03-22 09:29:23 +010097 msr scr_el3, x0
David Cunado5f55e282016-10-31 17:37:34 +000098
99 /* ---------------------------------------------------------------------
David Cunadofee86532017-04-13 22:38:29 +0100100 * Initialise MDCR_EL3, setting all fields rather than relying on hw.
101 * Some fields are architecturally UNKNOWN on reset.
David Cunado5f55e282016-10-31 17:37:34 +0000102 */
Jayanth Dodderi Chidanandb4590652023-08-08 16:10:16 +0100103 mov_imm x0, MDCR_EL3_RESET_VAL
dp-arm595d0d52017-02-08 11:51:50 +0000104 msr mdcr_el3, x0
David Cunado5f55e282016-10-31 17:37:34 +0000105
Gerald Lejeune632d6df2016-03-22 09:29:23 +0100106 /* ---------------------------------------------------------------------
David Cunadofee86532017-04-13 22:38:29 +0100107 * Initialise CPTR_EL3, setting all fields rather than relying on hw.
108 * All fields are architecturally UNKNOWN on reset.
Boyan Karatotev8ae58f02023-04-20 11:00:50 +0100109 * ---------------------------------------------------------------------
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100110 */
Boyan Karatotev8ae58f02023-04-20 11:00:50 +0100111 mov_imm x0, CPTR_EL3_RESET_VAL
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100112 msr cptr_el3, x0
Sathees Balya0911df12018-12-06 13:33:24 +0000113
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100114 .endm
115
116/* -----------------------------------------------------------------------------
117 * This is the super set of actions that need to be performed during a cold boot
Juan Castillo7d199412015-12-14 09:35:25 +0000118 * or a warm boot in EL3. This code is shared by BL1 and BL31.
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100119 *
120 * This macro will always perform reset handling, architectural initialisations
121 * and stack setup. The rest of the actions are optional because they might not
122 * be needed, depending on the context in which this macro is called. This is
123 * why this macro is parameterised ; each parameter allows to enable/disable
124 * some actions.
125 *
David Cunadofee86532017-04-13 22:38:29 +0100126 * _init_sctlr:
127 * Whether the macro needs to initialise SCTLR_EL3, including configuring
128 * the endianness of data accesses.
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100129 *
130 * _warm_boot_mailbox:
131 * Whether the macro needs to detect the type of boot (cold/warm). The
132 * detection is based on the platform entrypoint address : if it is zero
133 * then it is a cold boot, otherwise it is a warm boot. In the latter case,
134 * this macro jumps on the platform entrypoint address.
135 *
136 * _secondary_cold_boot:
137 * Whether the macro needs to identify the CPU that is calling it: primary
138 * CPU or secondary CPU. The primary CPU will be allowed to carry on with
139 * the platform initialisations, while the secondaries will be put in a
140 * platform-specific state in the meantime.
141 *
142 * If the caller knows this macro will only be called by the primary CPU
143 * then this parameter can be defined to 0 to skip this step.
144 *
145 * _init_memory:
146 * Whether the macro needs to initialise the memory.
147 *
148 * _init_c_runtime:
149 * Whether the macro needs to initialise the C runtime environment.
150 *
151 * _exception_vectors:
152 * Address of the exception vectors to program in the VBAR_EL3 register.
Manish Pandeyc8257682019-11-26 11:34:17 +0000153 *
154 * _pie_fixup_size:
155 * Size of memory region to fixup Global Descriptor Table (GDT).
156 *
157 * A non-zero value is expected when firmware needs GDT to be fixed-up.
158 *
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100159 * -----------------------------------------------------------------------------
160 */
161 .macro el3_entrypoint_common \
David Cunadofee86532017-04-13 22:38:29 +0100162 _init_sctlr, _warm_boot_mailbox, _secondary_cold_boot, \
Manish Pandeyc8257682019-11-26 11:34:17 +0000163 _init_memory, _init_c_runtime, _exception_vectors, \
164 _pie_fixup_size
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100165
David Cunadofee86532017-04-13 22:38:29 +0100166 .if \_init_sctlr
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100167 /* -------------------------------------------------------------
David Cunadofee86532017-04-13 22:38:29 +0100168 * This is the initialisation of SCTLR_EL3 and so must ensure
169 * that all fields are explicitly set rather than relying on hw.
170 * Some fields reset to an IMPLEMENTATION DEFINED value and
171 * others are architecturally UNKNOWN on reset.
172 *
173 * SCTLR.EE: Set the CPU endianness before doing anything that
174 * might involve memory reads or writes. Set to zero to select
175 * Little Endian.
176 *
177 * SCTLR_EL3.WXN: For the EL3 translation regime, this field can
178 * force all memory regions that are writeable to be treated as
179 * XN (Execute-never). Set to zero so that this control has no
180 * effect on memory access permissions.
181 *
Qixiang Xu3cc39172018-03-05 09:31:11 +0800182 * SCTLR_EL3.SA: Set to zero to disable Stack Alignment check.
David Cunadofee86532017-04-13 22:38:29 +0100183 *
184 * SCTLR_EL3.A: Set to zero to disable Alignment fault checking.
Jeenu Viswambharanaa00aff2018-11-15 11:38:03 +0000185 *
186 * SCTLR.DSSBS: Set to zero to disable speculation store bypass
187 * safe behaviour upon exception entry to EL3.
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100188 * -------------------------------------------------------------
189 */
David Cunadofee86532017-04-13 22:38:29 +0100190 mov_imm x0, (SCTLR_RESET_VAL & ~(SCTLR_EE_BIT | SCTLR_WXN_BIT \
Jeenu Viswambharanaa00aff2018-11-15 11:38:03 +0000191 | SCTLR_SA_BIT | SCTLR_A_BIT | SCTLR_DSSBS_BIT))
Manish Pandey514a3012023-10-10 13:53:25 +0100192#if ENABLE_FEAT_RAS
Manish Pandey6b5721f2023-06-26 17:46:14 +0100193 /* If FEAT_RAS is present assume FEAT_IESB is also present */
194 orr x0, x0, #SCTLR_IESB_BIT
195#endif
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100196 msr sctlr_el3, x0
197 isb
David Cunadofee86532017-04-13 22:38:29 +0100198 .endif /* _init_sctlr */
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100199
200 .if \_warm_boot_mailbox
201 /* -------------------------------------------------------------
202 * This code will be executed for both warm and cold resets.
203 * Now is the time to distinguish between the two.
204 * Query the platform entrypoint address and if it is not zero
205 * then it means it is a warm boot so jump to this address.
206 * -------------------------------------------------------------
207 */
Soby Mathew3700a922015-07-13 11:21:11 +0100208 bl plat_get_my_entrypoint
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100209 cbz x0, do_cold_boot
210 br x0
211
212 do_cold_boot:
213 .endif /* _warm_boot_mailbox */
214
Manish Pandeyc8257682019-11-26 11:34:17 +0000215 .if \_pie_fixup_size
216#if ENABLE_PIE
217 /*
218 * ------------------------------------------------------------
219 * If PIE is enabled fixup the Global descriptor Table only
220 * once during primary core cold boot path.
221 *
222 * Compile time base address, required for fixup, is calculated
223 * using "pie_fixup" label present within first page.
224 * ------------------------------------------------------------
225 */
226 pie_fixup:
227 ldr x0, =pie_fixup
Jimmy Brissoned202072020-08-04 16:18:52 -0500228 and x0, x0, #~(PAGE_SIZE_MASK)
Manish Pandeyc8257682019-11-26 11:34:17 +0000229 mov_imm x1, \_pie_fixup_size
230 add x1, x1, x0
231 bl fixup_gdt_reloc
232#endif /* ENABLE_PIE */
233 .endif /* _pie_fixup_size */
234
Antonio Nino Diaz4357b412016-02-23 12:04:58 +0000235 /* ---------------------------------------------------------------------
Dimitris Papastamos446f7f12017-11-30 14:53:53 +0000236 * Set the exception vectors.
237 * ---------------------------------------------------------------------
238 */
239 adr x0, \_exception_vectors
240 msr vbar_el3, x0
241 isb
242
Boyan Karatotev1dcba8f2024-11-19 11:27:01 +0000243 call_reset_handler
Antonio Nino Diaz4357b412016-02-23 12:04:58 +0000244
Dimitris Papastamos446f7f12017-11-30 14:53:53 +0000245 el3_arch_init_common
Antonio Nino Diaz4357b412016-02-23 12:04:58 +0000246
Jayanth Dodderi Chidanandb4590652023-08-08 16:10:16 +0100247 /* ---------------------------------------------------------------------
248 * Set the el3 execution context(i.e. root_context).
249 * ---------------------------------------------------------------------
250 */
251 setup_el3_execution_context
252
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100253 .if \_secondary_cold_boot
254 /* -------------------------------------------------------------
Antonio Nino Diaz4357b412016-02-23 12:04:58 +0000255 * Check if this is a primary or secondary CPU cold boot.
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100256 * The primary CPU will set up the platform while the
257 * secondaries are placed in a platform-specific state until the
258 * primary CPU performs the necessary actions to bring them out
259 * of that state and allows entry into the OS.
260 * -------------------------------------------------------------
261 */
Soby Mathew3700a922015-07-13 11:21:11 +0100262 bl plat_is_my_cpu_primary
Soby Matheweb3bbf12015-06-08 12:32:50 +0100263 cbnz w0, do_primary_cold_boot
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100264
265 /* This is a cold boot on a secondary CPU */
266 bl plat_secondary_cold_boot_setup
267 /* plat_secondary_cold_boot_setup() is not supposed to return */
Antonio Nino Diaz1f21bcf2016-02-01 13:57:25 +0000268 bl el3_panic
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100269
270 do_primary_cold_boot:
271 .endif /* _secondary_cold_boot */
272
273 /* ---------------------------------------------------------------------
Antonio Nino Diaz4357b412016-02-23 12:04:58 +0000274 * Initialize memory now. Secondary CPU initialization won't get to this
275 * point.
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100276 * ---------------------------------------------------------------------
277 */
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100278
279 .if \_init_memory
280 bl platform_mem_init
281 .endif /* _init_memory */
282
283 /* ---------------------------------------------------------------------
284 * Init C runtime environment:
285 * - Zero-initialise the NOBITS sections. There are 2 of them:
286 * - the .bss section;
287 * - the coherent memory section (if any).
288 * - Relocate the data section from ROM to RAM, if required.
289 * ---------------------------------------------------------------------
290 */
291 .if \_init_c_runtime
Zelalem Aweke688fbf72021-07-09 11:37:10 -0500292#if defined(IMAGE_BL31) || (defined(IMAGE_BL2) && \
Arvind Ram Prakash11b9b492022-11-22 14:41:00 -0600293 ((RESET_TO_BL2 && BL2_INV_DCACHE) || ENABLE_RME))
Achin Guptae9c4a642015-09-11 16:03:13 +0100294 /* -------------------------------------------------------------
295 * Invalidate the RW memory used by the BL31 image. This
296 * includes the data and NOBITS sections. This is done to
297 * safeguard against possible corruption of this memory by
298 * dirty cache lines in a system cache as a result of use by
Zelalem Awekeb0d69e82021-10-15 17:25:52 -0500299 * an earlier boot loader stage. If PIE is enabled however,
300 * RO sections including the GOT may be modified during
301 * pie fixup. Therefore, to be on the safe side, invalidate
302 * the entire image region if PIE is enabled.
Achin Guptae9c4a642015-09-11 16:03:13 +0100303 * -------------------------------------------------------------
304 */
Zelalem Awekeb0d69e82021-10-15 17:25:52 -0500305#if ENABLE_PIE
306#if SEPARATE_CODE_AND_RODATA
307 adrp x0, __TEXT_START__
308 add x0, x0, :lo12:__TEXT_START__
309#else
310 adrp x0, __RO_START__
311 add x0, x0, :lo12:__RO_START__
312#endif /* SEPARATE_CODE_AND_RODATA */
313#else
Soby Mathewfcaf1bd2018-10-12 16:40:28 +0100314 adrp x0, __RW_START__
315 add x0, x0, :lo12:__RW_START__
Zelalem Awekeb0d69e82021-10-15 17:25:52 -0500316#endif /* ENABLE_PIE */
Soby Mathewfcaf1bd2018-10-12 16:40:28 +0100317 adrp x1, __RW_END__
318 add x1, x1, :lo12:__RW_END__
Achin Guptae9c4a642015-09-11 16:03:13 +0100319 sub x1, x1, x0
320 bl inv_dcache_range
Samuel Holland31a14e12018-10-17 21:40:18 -0500321#if defined(IMAGE_BL31) && SEPARATE_NOBITS_REGION
322 adrp x0, __NOBITS_START__
323 add x0, x0, :lo12:__NOBITS_START__
324 adrp x1, __NOBITS_END__
325 add x1, x1, :lo12:__NOBITS_END__
326 sub x1, x1, x0
327 bl inv_dcache_range
328#endif
Jiafei Pan0824b452022-02-24 10:47:33 +0800329#if defined(IMAGE_BL2) && SEPARATE_BL2_NOLOAD_REGION
330 adrp x0, __BL2_NOLOAD_START__
331 add x0, x0, :lo12:__BL2_NOLOAD_START__
332 adrp x1, __BL2_NOLOAD_END__
333 add x1, x1, :lo12:__BL2_NOLOAD_END__
334 sub x1, x1, x0
335 bl inv_dcache_range
336#endif
Roberto Vargase0e99462017-10-30 14:43:43 +0000337#endif
Soby Mathewfcaf1bd2018-10-12 16:40:28 +0100338 adrp x0, __BSS_START__
339 add x0, x0, :lo12:__BSS_START__
Achin Guptae9c4a642015-09-11 16:03:13 +0100340
Soby Mathewfcaf1bd2018-10-12 16:40:28 +0100341 adrp x1, __BSS_END__
342 add x1, x1, :lo12:__BSS_END__
343 sub x1, x1, x0
Douglas Raillard21362a92016-12-02 13:51:54 +0000344 bl zeromem
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100345
346#if USE_COHERENT_MEM
Soby Mathewfcaf1bd2018-10-12 16:40:28 +0100347 adrp x0, __COHERENT_RAM_START__
348 add x0, x0, :lo12:__COHERENT_RAM_START__
349 adrp x1, __COHERENT_RAM_END_UNALIGNED__
350 add x1, x1, :lo12: __COHERENT_RAM_END_UNALIGNED__
351 sub x1, x1, x0
Douglas Raillard21362a92016-12-02 13:51:54 +0000352 bl zeromem
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100353#endif
354
Arvind Ram Prakash11b9b492022-11-22 14:41:00 -0600355#if defined(IMAGE_BL1) || \
Ye Li97267752022-08-26 13:48:31 +0800356 (defined(IMAGE_BL2) && RESET_TO_BL2 && BL2_IN_XIP_MEM) || \
357 (defined(IMAGE_BL31) && SEPARATE_RWDATA_REGION)
358
Soby Mathewfcaf1bd2018-10-12 16:40:28 +0100359 adrp x0, __DATA_RAM_START__
360 add x0, x0, :lo12:__DATA_RAM_START__
361 adrp x1, __DATA_ROM_START__
362 add x1, x1, :lo12:__DATA_ROM_START__
363 adrp x2, __DATA_RAM_END__
364 add x2, x2, :lo12:__DATA_RAM_END__
365 sub x2, x2, x0
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100366 bl memcpy16
367#endif
368 .endif /* _init_c_runtime */
369
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100370 /* ---------------------------------------------------------------------
371 * Use SP_EL0 for the C runtime stack.
372 * ---------------------------------------------------------------------
373 */
374 msr spsel, #0
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100375
376 /* ---------------------------------------------------------------------
377 * Allocate a stack whose memory will be marked as Normal-IS-WBWA when
378 * the MMU is enabled. There is no risk of reading stale stack memory
379 * after enabling the MMU as only the primary CPU is running at the
380 * moment.
381 * ---------------------------------------------------------------------
382 */
Soby Mathew3700a922015-07-13 11:21:11 +0100383 bl plat_set_my_stack
Douglas Raillard306593d2017-02-24 18:14:15 +0000384
385#if STACK_PROTECTOR_ENABLED
386 .if \_init_c_runtime
387 bl update_stack_protector_canary
388 .endif /* _init_c_runtime */
389#endif
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100390 .endm
391
Manish V Badarkhee07e8082020-07-23 12:43:25 +0100392 .macro apply_at_speculative_wa
393#if ERRATA_SPECULATIVE_AT
394 /*
Manish Pandey66a056e2023-01-11 21:41:07 +0000395 * This function expects x30 has been saved.
396 * Also, save x29 which will be used in the called function.
Manish V Badarkhee07e8082020-07-23 12:43:25 +0100397 */
Manish Pandey66a056e2023-01-11 21:41:07 +0000398 str x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29]
Manish V Badarkhee07e8082020-07-23 12:43:25 +0100399 bl save_and_update_ptw_el1_sys_regs
Manish Pandey66a056e2023-01-11 21:41:07 +0000400 ldr x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29]
Manish V Badarkhee07e8082020-07-23 12:43:25 +0100401#endif
402 .endm
403
404 .macro restore_ptw_el1_sys_regs
405#if ERRATA_SPECULATIVE_AT
406 /* -----------------------------------------------------------
407 * In case of ERRATA_SPECULATIVE_AT, must follow below order
408 * to ensure that page table walk is not enabled until
409 * restoration of all EL1 system registers. TCR_EL1 register
410 * should be updated at the end which restores previous page
411 * table walk setting of stage1 i.e.(TCR_EL1.EPDx) bits. ISB
412 * ensures that CPU does below steps in order.
413 *
414 * 1. Ensure all other system registers are written before
415 * updating SCTLR_EL1 using ISB.
416 * 2. Restore SCTLR_EL1 register.
417 * 3. Ensure SCTLR_EL1 written successfully using ISB.
418 * 4. Restore TCR_EL1 register.
419 * -----------------------------------------------------------
420 */
421 isb
Jayanth Dodderi Chidanand3a71df62024-06-05 11:13:05 +0100422 ldp x28, x29, [sp, #CTX_ERRATA_SPEC_AT_OFFSET + CTX_ERRATA_SPEC_AT_SCTLR_EL1]
Manish V Badarkhee07e8082020-07-23 12:43:25 +0100423 msr sctlr_el1, x28
424 isb
425 msr tcr_el1, x29
426#endif
427 .endm
428
Elizabeth Ho4fc00d22023-07-18 14:10:25 +0100429/* -----------------------------------------------------------------
430 * The below macro reads SCR_EL3 from the context structure to
431 * determine the security state of the context upon ERET.
432 * ------------------------------------------------------------------
433 */
434 .macro get_security_state _ret:req, _scr_reg:req
435 ubfx \_ret, \_scr_reg, #SCR_NSE_SHIFT, #1
436 cmp \_ret, #1
437 beq realm_state
438 bfi \_ret, \_scr_reg, #0, #1
439 b end
440 realm_state:
441 mov \_ret, #2
442 end:
443 .endm
444
Jayanth Dodderi Chidanandb4590652023-08-08 16:10:16 +0100445/*-----------------------------------------------------------------------------
446 * Helper macro to configure EL3 registers we care about, while executing
447 * at EL3/Root world. Root world has its own execution environment and
448 * needs to have its settings configured to be independent of other worlds.
449 * -----------------------------------------------------------------------------
450 */
451 .macro setup_el3_execution_context
452
453 /* ---------------------------------------------------------------------
454 * The following registers need to be part of separate root context
455 * as their values are of importance during EL3 execution.
456 * Hence these registers are overwritten to their intital values,
457 * irrespective of whichever world they return from to ensure EL3 has a
458 * consistent execution context throughout the lifetime of TF-A.
459 *
460 * DAIF.A: Enable External Aborts and SError Interrupts at EL3.
461 *
462 * MDCR_EL3.SDD: Set to one to disable AArch64 Secure self-hosted debug.
463 * Debug exceptions, other than Breakpoint Instruction exceptions, are
464 * disabled from all ELs in Secure state.
465 *
466 * SCR_EL3.EA: Set to one to enable SError interrupts at EL3.
467 *
468 * SCR_EL3.SIF: Set to one to disable instruction fetches from
469 * Non-secure memory.
470 *
471 * PMCR_EL0.DP: Set to one so that the cycle counter,
472 * PMCCNTR_EL0 does not count when event counting is prohibited.
473 * Necessary on PMUv3 <= p7 where MDCR_EL3.{SCCD,MCCD} are not
474 * available.
475 *
Boyan Karatotev90b7b752024-11-15 15:03:02 +0000476 * CPTR_EL3.EZ: Set to one so that accesses to ZCR_EL3 do not trap
477 * CPTR_EL3.TFP: Set to zero so that advanced SIMD operations don't trap
478 * CPTR_EL3.ESM: Set to one so that SME related registers don't trap
479 *
Jayanth Dodderi Chidanandb4590652023-08-08 16:10:16 +0100480 * PSTATE.DIT: Set to one to enable the Data Independent Timing (DIT)
481 * functionality, if implemented in EL3.
482 * ---------------------------------------------------------------------
483 */
484 msr daifclr, #DAIF_ABT_BIT
485
486 mrs x15, mdcr_el3
487 orr x15, x15, #MDCR_SDD_BIT
488 msr mdcr_el3, x15
489
490 mrs x15, scr_el3
491 orr x15, x15, #SCR_EA_BIT
492 orr x15, x15, #SCR_SIF_BIT
493 msr scr_el3, x15
494
495 mrs x15, pmcr_el0
496 orr x15, x15, #PMCR_EL0_DP_BIT
497 msr pmcr_el0, x15
498
Boyan Karatotev90b7b752024-11-15 15:03:02 +0000499 mrs x15, cptr_el3
500 orr x15, x15, #CPTR_EZ_BIT
501 orr x15, x15, #ESM_BIT
502 bic x15, x15, #TFP_BIT
503 msr cptr_el3, x15
504
Jayanth Dodderi Chidanandb4590652023-08-08 16:10:16 +0100505#if ENABLE_FEAT_DIT
506#if ENABLE_FEAT_DIT > 1
507 mrs x15, id_aa64pfr0_el1
508 ubfx x15, x15, #ID_AA64PFR0_DIT_SHIFT, #ID_AA64PFR0_DIT_LENGTH
509 cbz x15, 1f
510#endif
511 mov x15, #DIT_BIT
512 msr DIT, x15
513 1:
514#endif
515
516 isb
517 .endm
518
Antonio Nino Diaz5eb88372018-11-08 10:20:19 +0000519#endif /* EL3_COMMON_MACROS_S */