1f5478dedSAntonio Nino Diaz/* 20d020822SBoyan Karatotev * Copyright (c) 2015-2025, Arm Limited and Contributors. All rights reserved. 3f5478dedSAntonio Nino Diaz * 4f5478dedSAntonio Nino Diaz * SPDX-License-Identifier: BSD-3-Clause 5f5478dedSAntonio Nino Diaz */ 6f5478dedSAntonio Nino Diaz 7f5478dedSAntonio Nino Diaz#ifndef EL3_COMMON_MACROS_S 8f5478dedSAntonio Nino Diaz#define EL3_COMMON_MACROS_S 9f5478dedSAntonio Nino Diaz 10f5478dedSAntonio Nino Diaz#include <arch.h> 11f5478dedSAntonio Nino Diaz#include <asm_macros.S> 127d33ffe4SDaniel Boulby#include <assert_macros.S> 133b8456bdSManish V Badarkhe#include <context.h> 141a04b2e5SVarun Wadekar#include <lib/xlat_tables/xlat_tables_defs.h> 15f5478dedSAntonio Nino Diaz 16f5478dedSAntonio Nino Diaz /* 17f5478dedSAntonio Nino Diaz * Helper macro to initialise EL3 registers we care about. 18f5478dedSAntonio Nino Diaz */ 19f5478dedSAntonio Nino Diaz .macro el3_arch_init_common 20f5478dedSAntonio Nino Diaz /* --------------------------------------------------------------------- 21f5478dedSAntonio Nino Diaz * SCTLR_EL3 has already been initialised - read current value before 22f5478dedSAntonio Nino Diaz * modifying. 23f5478dedSAntonio Nino Diaz * 24f5478dedSAntonio Nino Diaz * SCTLR_EL3.I: Enable the instruction cache. 25f5478dedSAntonio Nino Diaz * 26f5478dedSAntonio Nino Diaz * SCTLR_EL3.SA: Enable Stack Alignment check. A SP alignment fault 27f5478dedSAntonio Nino Diaz * exception is generated if a load or store instruction executed at 28f5478dedSAntonio Nino Diaz * EL3 uses the SP as the base address and the SP is not aligned to a 29f5478dedSAntonio Nino Diaz * 16-byte boundary. 30f5478dedSAntonio Nino Diaz * 31f5478dedSAntonio Nino Diaz * SCTLR_EL3.A: Enable Alignment fault checking. All instructions that 32f5478dedSAntonio Nino Diaz * load or store one or more registers have an alignment check that the 33f5478dedSAntonio Nino Diaz * address being accessed is aligned to the size of the data element(s) 34f5478dedSAntonio Nino Diaz * being accessed. 35*10ecd580SBoyan Karatotev * 36*10ecd580SBoyan Karatotev * SCTLR_EL3.BT: PAuth instructions are compatible with bti jc 37f5478dedSAntonio Nino Diaz * --------------------------------------------------------------------- 38f5478dedSAntonio Nino Diaz */ 39*10ecd580SBoyan Karatotev mov_imm x1, (SCTLR_I_BIT | SCTLR_A_BIT | SCTLR_SA_BIT) 40f5478dedSAntonio Nino Diaz mrs x0, sctlr_el3 41*10ecd580SBoyan Karatotev#if ENABLE_BTI 42*10ecd580SBoyan Karatotev bic x0, x0, #SCTLR_BT_BIT 43*10ecd580SBoyan Karatotev#endif 44f5478dedSAntonio Nino Diaz orr x0, x0, x1 45f5478dedSAntonio Nino Diaz msr sctlr_el3, x0 46f5478dedSAntonio Nino Diaz isb 47f5478dedSAntonio Nino Diaz 48f5478dedSAntonio Nino Diaz#ifdef IMAGE_BL31 49f5478dedSAntonio Nino Diaz /* --------------------------------------------------------------------- 50f5478dedSAntonio Nino Diaz * Initialise the per-cpu cache pointer to the CPU. 51f5478dedSAntonio Nino Diaz * This is done early to enable crash reporting to have access to crash 52f5478dedSAntonio Nino Diaz * stack. Since crash reporting depends on cpu_data to report the 53f5478dedSAntonio Nino Diaz * unhandled exception, not doing so can lead to recursive exceptions 54f5478dedSAntonio Nino Diaz * due to a NULL TPIDR_EL3. 55f5478dedSAntonio Nino Diaz * --------------------------------------------------------------------- 56f5478dedSAntonio Nino Diaz */ 57b07c317fSBoyan Karatotev bl plat_my_core_pos 58b07c317fSBoyan Karatotev bl _cpu_data_by_index 59b07c317fSBoyan Karatotev msr tpidr_el3, x0 60f5478dedSAntonio Nino Diaz#endif /* IMAGE_BL31 */ 61f5478dedSAntonio Nino Diaz 62f5478dedSAntonio Nino Diaz /* --------------------------------------------------------------------- 63f5478dedSAntonio Nino Diaz * Initialise SCR_EL3, setting all fields rather than relying on hw. 64f5478dedSAntonio Nino Diaz * All fields are architecturally UNKNOWN on reset. The following fields 65f5478dedSAntonio Nino Diaz * do not change during the TF lifetime. The remaining fields are set to 66f5478dedSAntonio Nino Diaz * zero here but are updated ahead of transitioning to a lower EL in the 67f5478dedSAntonio Nino Diaz * function cm_init_context_common(). 68f5478dedSAntonio Nino Diaz * 698815cdafSManish Pandey * SCR_EL3.EEL2: Set to one if S-EL2 is present and enabled. 708815cdafSManish Pandey * 718815cdafSManish Pandey * NOTE: Modifying EEL2 bit along with EA bit ensures that we mitigate 728815cdafSManish Pandey * against ERRATA_V2_3099206. 73f5478dedSAntonio Nino Diaz * --------------------------------------------------------------------- 74f5478dedSAntonio Nino Diaz */ 7540e5f7a5SJayanth Dodderi Chidanand mov_imm x0, SCR_RESET_VAL 768815cdafSManish Pandey#if IMAGE_BL31 && defined(SPD_spmd) && SPMD_SPM_AT_SEL2 778815cdafSManish Pandey mrs x1, id_aa64pfr0_el1 788815cdafSManish Pandey and x1, x1, #(ID_AA64PFR0_SEL2_MASK << ID_AA64PFR0_SEL2_SHIFT) 798815cdafSManish Pandey cbz x1, 1f 808815cdafSManish Pandey orr x0, x0, #SCR_EEL2_BIT 818815cdafSManish Pandey#endif 828815cdafSManish Pandey1: 83f5478dedSAntonio Nino Diaz msr scr_el3, x0 84f5478dedSAntonio Nino Diaz 85f5478dedSAntonio Nino Diaz /* --------------------------------------------------------------------- 86f5478dedSAntonio Nino Diaz * Initialise MDCR_EL3, setting all fields rather than relying on hw. 87f5478dedSAntonio Nino Diaz * Some fields are architecturally UNKNOWN on reset. 88f5478dedSAntonio Nino Diaz */ 8940e5f7a5SJayanth Dodderi Chidanand mov_imm x0, MDCR_EL3_RESET_VAL 90f5478dedSAntonio Nino Diaz msr mdcr_el3, x0 91f5478dedSAntonio Nino Diaz 92f5478dedSAntonio Nino Diaz /* --------------------------------------------------------------------- 93f5478dedSAntonio Nino Diaz * Initialise CPTR_EL3, setting all fields rather than relying on hw. 94f5478dedSAntonio Nino Diaz * All fields are architecturally UNKNOWN on reset. 95f0c96a2eSBoyan Karatotev * --------------------------------------------------------------------- 96f5478dedSAntonio Nino Diaz */ 97f0c96a2eSBoyan Karatotev mov_imm x0, CPTR_EL3_RESET_VAL 98f5478dedSAntonio Nino Diaz msr cptr_el3, x0 99f5478dedSAntonio Nino Diaz 100f5478dedSAntonio Nino Diaz .endm 101f5478dedSAntonio Nino Diaz 102f5478dedSAntonio Nino Diaz/* ----------------------------------------------------------------------------- 103f5478dedSAntonio Nino Diaz * This is the super set of actions that need to be performed during a cold boot 104f5478dedSAntonio Nino Diaz * or a warm boot in EL3. This code is shared by BL1 and BL31. 105f5478dedSAntonio Nino Diaz * 106f5478dedSAntonio Nino Diaz * This macro will always perform reset handling, architectural initialisations 107f5478dedSAntonio Nino Diaz * and stack setup. The rest of the actions are optional because they might not 108f5478dedSAntonio Nino Diaz * be needed, depending on the context in which this macro is called. This is 109f5478dedSAntonio Nino Diaz * why this macro is parameterised ; each parameter allows to enable/disable 110f5478dedSAntonio Nino Diaz * some actions. 111f5478dedSAntonio Nino Diaz * 112f5478dedSAntonio Nino Diaz * _init_sctlr: 113f5478dedSAntonio Nino Diaz * Whether the macro needs to initialise SCTLR_EL3, including configuring 114f5478dedSAntonio Nino Diaz * the endianness of data accesses. 115f5478dedSAntonio Nino Diaz * 116f5478dedSAntonio Nino Diaz * _warm_boot_mailbox: 117f5478dedSAntonio Nino Diaz * Whether the macro needs to detect the type of boot (cold/warm). The 118f5478dedSAntonio Nino Diaz * detection is based on the platform entrypoint address : if it is zero 119f5478dedSAntonio Nino Diaz * then it is a cold boot, otherwise it is a warm boot. In the latter case, 120f5478dedSAntonio Nino Diaz * this macro jumps on the platform entrypoint address. 121f5478dedSAntonio Nino Diaz * 122f5478dedSAntonio Nino Diaz * _secondary_cold_boot: 123f5478dedSAntonio Nino Diaz * Whether the macro needs to identify the CPU that is calling it: primary 124f5478dedSAntonio Nino Diaz * CPU or secondary CPU. The primary CPU will be allowed to carry on with 125f5478dedSAntonio Nino Diaz * the platform initialisations, while the secondaries will be put in a 126f5478dedSAntonio Nino Diaz * platform-specific state in the meantime. 127f5478dedSAntonio Nino Diaz * 128f5478dedSAntonio Nino Diaz * If the caller knows this macro will only be called by the primary CPU 129f5478dedSAntonio Nino Diaz * then this parameter can be defined to 0 to skip this step. 130f5478dedSAntonio Nino Diaz * 131f5478dedSAntonio Nino Diaz * _init_memory: 132f5478dedSAntonio Nino Diaz * Whether the macro needs to initialise the memory. 133f5478dedSAntonio Nino Diaz * 134f5478dedSAntonio Nino Diaz * _init_c_runtime: 135f5478dedSAntonio Nino Diaz * Whether the macro needs to initialise the C runtime environment. 136f5478dedSAntonio Nino Diaz * 137f5478dedSAntonio Nino Diaz * _exception_vectors: 138f5478dedSAntonio Nino Diaz * Address of the exception vectors to program in the VBAR_EL3 register. 139da90359bSManish Pandey * 140da90359bSManish Pandey * _pie_fixup_size: 141da90359bSManish Pandey * Size of memory region to fixup Global Descriptor Table (GDT). 142da90359bSManish Pandey * 143da90359bSManish Pandey * A non-zero value is expected when firmware needs GDT to be fixed-up. 144da90359bSManish Pandey * 145f5478dedSAntonio Nino Diaz * ----------------------------------------------------------------------------- 146f5478dedSAntonio Nino Diaz */ 147f5478dedSAntonio Nino Diaz .macro el3_entrypoint_common \ 148f5478dedSAntonio Nino Diaz _init_sctlr, _warm_boot_mailbox, _secondary_cold_boot, \ 149da90359bSManish Pandey _init_memory, _init_c_runtime, _exception_vectors, \ 150da90359bSManish Pandey _pie_fixup_size 151f5478dedSAntonio Nino Diaz 152f5478dedSAntonio Nino Diaz .if \_init_sctlr 153f5478dedSAntonio Nino Diaz /* ------------------------------------------------------------- 154f5478dedSAntonio Nino Diaz * This is the initialisation of SCTLR_EL3 and so must ensure 155f5478dedSAntonio Nino Diaz * that all fields are explicitly set rather than relying on hw. 156f5478dedSAntonio Nino Diaz * Some fields reset to an IMPLEMENTATION DEFINED value and 157f5478dedSAntonio Nino Diaz * others are architecturally UNKNOWN on reset. 158f5478dedSAntonio Nino Diaz * 159f5478dedSAntonio Nino Diaz * SCTLR.EE: Set the CPU endianness before doing anything that 160f5478dedSAntonio Nino Diaz * might involve memory reads or writes. Set to zero to select 161f5478dedSAntonio Nino Diaz * Little Endian. 162f5478dedSAntonio Nino Diaz * 163f5478dedSAntonio Nino Diaz * SCTLR_EL3.WXN: For the EL3 translation regime, this field can 164f5478dedSAntonio Nino Diaz * force all memory regions that are writeable to be treated as 165f5478dedSAntonio Nino Diaz * XN (Execute-never). Set to zero so that this control has no 166f5478dedSAntonio Nino Diaz * effect on memory access permissions. 167f5478dedSAntonio Nino Diaz * 168f5478dedSAntonio Nino Diaz * SCTLR_EL3.SA: Set to zero to disable Stack Alignment check. 169f5478dedSAntonio Nino Diaz * 170f5478dedSAntonio Nino Diaz * SCTLR_EL3.A: Set to zero to disable Alignment fault checking. 171f5478dedSAntonio Nino Diaz * 172f5478dedSAntonio Nino Diaz * SCTLR.DSSBS: Set to zero to disable speculation store bypass 173f5478dedSAntonio Nino Diaz * safe behaviour upon exception entry to EL3. 174f5478dedSAntonio Nino Diaz * ------------------------------------------------------------- 175f5478dedSAntonio Nino Diaz */ 176f5478dedSAntonio Nino Diaz mov_imm x0, (SCTLR_RESET_VAL & ~(SCTLR_EE_BIT | SCTLR_WXN_BIT \ 177f5478dedSAntonio Nino Diaz | SCTLR_SA_BIT | SCTLR_A_BIT | SCTLR_DSSBS_BIT)) 178970a4a8dSManish Pandey#if ENABLE_FEAT_RAS 1796597fcf1SManish Pandey /* If FEAT_RAS is present assume FEAT_IESB is also present */ 1806597fcf1SManish Pandey orr x0, x0, #SCTLR_IESB_BIT 1816597fcf1SManish Pandey#endif 182f5478dedSAntonio Nino Diaz msr sctlr_el3, x0 183f5478dedSAntonio Nino Diaz isb 184f5478dedSAntonio Nino Diaz .endif /* _init_sctlr */ 185f5478dedSAntonio Nino Diaz 186f5478dedSAntonio Nino Diaz .if \_warm_boot_mailbox 187f5478dedSAntonio Nino Diaz /* ------------------------------------------------------------- 188f5478dedSAntonio Nino Diaz * This code will be executed for both warm and cold resets. 189f5478dedSAntonio Nino Diaz * Now is the time to distinguish between the two. 190f5478dedSAntonio Nino Diaz * Query the platform entrypoint address and if it is not zero 191f5478dedSAntonio Nino Diaz * then it means it is a warm boot so jump to this address. 192f5478dedSAntonio Nino Diaz * ------------------------------------------------------------- 193f5478dedSAntonio Nino Diaz */ 194f5478dedSAntonio Nino Diaz bl plat_get_my_entrypoint 195f5478dedSAntonio Nino Diaz cbz x0, do_cold_boot 196f5478dedSAntonio Nino Diaz br x0 197f5478dedSAntonio Nino Diaz 198f5478dedSAntonio Nino Diaz do_cold_boot: 199f5478dedSAntonio Nino Diaz .endif /* _warm_boot_mailbox */ 200f5478dedSAntonio Nino Diaz 201da90359bSManish Pandey .if \_pie_fixup_size 202da90359bSManish Pandey#if ENABLE_PIE 203da90359bSManish Pandey /* 204da90359bSManish Pandey * ------------------------------------------------------------ 205da90359bSManish Pandey * If PIE is enabled fixup the Global descriptor Table only 206da90359bSManish Pandey * once during primary core cold boot path. 207da90359bSManish Pandey * 208da90359bSManish Pandey * Compile time base address, required for fixup, is calculated 209da90359bSManish Pandey * using "pie_fixup" label present within first page. 210da90359bSManish Pandey * ------------------------------------------------------------ 211da90359bSManish Pandey */ 212da90359bSManish Pandey pie_fixup: 213da90359bSManish Pandey ldr x0, =pie_fixup 214d7b5f408SJimmy Brisson and x0, x0, #~(PAGE_SIZE_MASK) 215da90359bSManish Pandey mov_imm x1, \_pie_fixup_size 216da90359bSManish Pandey add x1, x1, x0 217da90359bSManish Pandey bl fixup_gdt_reloc 218da90359bSManish Pandey#endif /* ENABLE_PIE */ 219da90359bSManish Pandey .endif /* _pie_fixup_size */ 220da90359bSManish Pandey 221f5478dedSAntonio Nino Diaz /* --------------------------------------------------------------------- 222f5478dedSAntonio Nino Diaz * Set the exception vectors. 223f5478dedSAntonio Nino Diaz * --------------------------------------------------------------------- 224f5478dedSAntonio Nino Diaz */ 225f5478dedSAntonio Nino Diaz adr x0, \_exception_vectors 226f5478dedSAntonio Nino Diaz msr vbar_el3, x0 227f5478dedSAntonio Nino Diaz isb 228f5478dedSAntonio Nino Diaz 2290d020822SBoyan Karatotev call_reset_handler 230f5478dedSAntonio Nino Diaz 231f5478dedSAntonio Nino Diaz el3_arch_init_common 232f5478dedSAntonio Nino Diaz 23340e5f7a5SJayanth Dodderi Chidanand /* --------------------------------------------------------------------- 23440e5f7a5SJayanth Dodderi Chidanand * Set the el3 execution context(i.e. root_context). 23540e5f7a5SJayanth Dodderi Chidanand * --------------------------------------------------------------------- 23640e5f7a5SJayanth Dodderi Chidanand */ 23740e5f7a5SJayanth Dodderi Chidanand setup_el3_execution_context 23840e5f7a5SJayanth Dodderi Chidanand 239f5478dedSAntonio Nino Diaz .if \_secondary_cold_boot 240f5478dedSAntonio Nino Diaz /* ------------------------------------------------------------- 241f5478dedSAntonio Nino Diaz * Check if this is a primary or secondary CPU cold boot. 242f5478dedSAntonio Nino Diaz * The primary CPU will set up the platform while the 243f5478dedSAntonio Nino Diaz * secondaries are placed in a platform-specific state until the 244f5478dedSAntonio Nino Diaz * primary CPU performs the necessary actions to bring them out 245f5478dedSAntonio Nino Diaz * of that state and allows entry into the OS. 246f5478dedSAntonio Nino Diaz * ------------------------------------------------------------- 247f5478dedSAntonio Nino Diaz */ 248f5478dedSAntonio Nino Diaz bl plat_is_my_cpu_primary 249f5478dedSAntonio Nino Diaz cbnz w0, do_primary_cold_boot 250f5478dedSAntonio Nino Diaz 251f5478dedSAntonio Nino Diaz /* This is a cold boot on a secondary CPU */ 252f5478dedSAntonio Nino Diaz bl plat_secondary_cold_boot_setup 253f5478dedSAntonio Nino Diaz /* plat_secondary_cold_boot_setup() is not supposed to return */ 254f5478dedSAntonio Nino Diaz bl el3_panic 255f5478dedSAntonio Nino Diaz 256f5478dedSAntonio Nino Diaz do_primary_cold_boot: 257f5478dedSAntonio Nino Diaz .endif /* _secondary_cold_boot */ 258f5478dedSAntonio Nino Diaz 259f5478dedSAntonio Nino Diaz /* --------------------------------------------------------------------- 260f5478dedSAntonio Nino Diaz * Initialize memory now. Secondary CPU initialization won't get to this 261f5478dedSAntonio Nino Diaz * point. 262f5478dedSAntonio Nino Diaz * --------------------------------------------------------------------- 263f5478dedSAntonio Nino Diaz */ 264f5478dedSAntonio Nino Diaz 265f5478dedSAntonio Nino Diaz .if \_init_memory 266f5478dedSAntonio Nino Diaz bl platform_mem_init 267f5478dedSAntonio Nino Diaz .endif /* _init_memory */ 268f5478dedSAntonio Nino Diaz 269f5478dedSAntonio Nino Diaz /* --------------------------------------------------------------------- 270f5478dedSAntonio Nino Diaz * Init C runtime environment: 271f5478dedSAntonio Nino Diaz * - Zero-initialise the NOBITS sections. There are 2 of them: 272f5478dedSAntonio Nino Diaz * - the .bss section; 273f5478dedSAntonio Nino Diaz * - the coherent memory section (if any). 274f5478dedSAntonio Nino Diaz * - Relocate the data section from ROM to RAM, if required. 275f5478dedSAntonio Nino Diaz * --------------------------------------------------------------------- 276f5478dedSAntonio Nino Diaz */ 277f5478dedSAntonio Nino Diaz .if \_init_c_runtime 2786c09af9fSZelalem Aweke#if defined(IMAGE_BL31) || (defined(IMAGE_BL2) && \ 27942d4d3baSArvind Ram Prakash ((RESET_TO_BL2 && BL2_INV_DCACHE) || ENABLE_RME)) 280f5478dedSAntonio Nino Diaz /* ------------------------------------------------------------- 281f5478dedSAntonio Nino Diaz * Invalidate the RW memory used by the BL31 image. This 282f5478dedSAntonio Nino Diaz * includes the data and NOBITS sections. This is done to 283f5478dedSAntonio Nino Diaz * safeguard against possible corruption of this memory by 284f5478dedSAntonio Nino Diaz * dirty cache lines in a system cache as a result of use by 285596d20d9SZelalem Aweke * an earlier boot loader stage. If PIE is enabled however, 286596d20d9SZelalem Aweke * RO sections including the GOT may be modified during 287596d20d9SZelalem Aweke * pie fixup. Therefore, to be on the safe side, invalidate 288596d20d9SZelalem Aweke * the entire image region if PIE is enabled. 289f5478dedSAntonio Nino Diaz * ------------------------------------------------------------- 290f5478dedSAntonio Nino Diaz */ 291596d20d9SZelalem Aweke#if ENABLE_PIE 292596d20d9SZelalem Aweke#if SEPARATE_CODE_AND_RODATA 293596d20d9SZelalem Aweke adrp x0, __TEXT_START__ 294596d20d9SZelalem Aweke add x0, x0, :lo12:__TEXT_START__ 295596d20d9SZelalem Aweke#else 296596d20d9SZelalem Aweke adrp x0, __RO_START__ 297596d20d9SZelalem Aweke add x0, x0, :lo12:__RO_START__ 298596d20d9SZelalem Aweke#endif /* SEPARATE_CODE_AND_RODATA */ 299596d20d9SZelalem Aweke#else 300f5478dedSAntonio Nino Diaz adrp x0, __RW_START__ 301f5478dedSAntonio Nino Diaz add x0, x0, :lo12:__RW_START__ 302596d20d9SZelalem Aweke#endif /* ENABLE_PIE */ 303f5478dedSAntonio Nino Diaz adrp x1, __RW_END__ 304f5478dedSAntonio Nino Diaz add x1, x1, :lo12:__RW_END__ 305f5478dedSAntonio Nino Diaz sub x1, x1, x0 306f5478dedSAntonio Nino Diaz bl inv_dcache_range 307f8578e64SSamuel Holland#if defined(IMAGE_BL31) && SEPARATE_NOBITS_REGION 308f8578e64SSamuel Holland adrp x0, __NOBITS_START__ 309f8578e64SSamuel Holland add x0, x0, :lo12:__NOBITS_START__ 310f8578e64SSamuel Holland adrp x1, __NOBITS_END__ 311f8578e64SSamuel Holland add x1, x1, :lo12:__NOBITS_END__ 312f8578e64SSamuel Holland sub x1, x1, x0 313f8578e64SSamuel Holland bl inv_dcache_range 314f8578e64SSamuel Holland#endif 31596a8ed14SJiafei Pan#if defined(IMAGE_BL2) && SEPARATE_BL2_NOLOAD_REGION 31696a8ed14SJiafei Pan adrp x0, __BL2_NOLOAD_START__ 31796a8ed14SJiafei Pan add x0, x0, :lo12:__BL2_NOLOAD_START__ 31896a8ed14SJiafei Pan adrp x1, __BL2_NOLOAD_END__ 31996a8ed14SJiafei Pan add x1, x1, :lo12:__BL2_NOLOAD_END__ 32096a8ed14SJiafei Pan sub x1, x1, x0 32196a8ed14SJiafei Pan bl inv_dcache_range 32296a8ed14SJiafei Pan#endif 323f5478dedSAntonio Nino Diaz#endif 324f5478dedSAntonio Nino Diaz adrp x0, __BSS_START__ 325f5478dedSAntonio Nino Diaz add x0, x0, :lo12:__BSS_START__ 326f5478dedSAntonio Nino Diaz 327f5478dedSAntonio Nino Diaz adrp x1, __BSS_END__ 328f5478dedSAntonio Nino Diaz add x1, x1, :lo12:__BSS_END__ 329f5478dedSAntonio Nino Diaz sub x1, x1, x0 330f5478dedSAntonio Nino Diaz bl zeromem 331f5478dedSAntonio Nino Diaz 332f5478dedSAntonio Nino Diaz#if USE_COHERENT_MEM 333f5478dedSAntonio Nino Diaz adrp x0, __COHERENT_RAM_START__ 334f5478dedSAntonio Nino Diaz add x0, x0, :lo12:__COHERENT_RAM_START__ 335f5478dedSAntonio Nino Diaz adrp x1, __COHERENT_RAM_END_UNALIGNED__ 336f5478dedSAntonio Nino Diaz add x1, x1, :lo12: __COHERENT_RAM_END_UNALIGNED__ 337f5478dedSAntonio Nino Diaz sub x1, x1, x0 338f5478dedSAntonio Nino Diaz bl zeromem 339f5478dedSAntonio Nino Diaz#endif 340f5478dedSAntonio Nino Diaz 34142d4d3baSArvind Ram Prakash#if defined(IMAGE_BL1) || \ 34286acbbe2SYe Li (defined(IMAGE_BL2) && RESET_TO_BL2 && BL2_IN_XIP_MEM) || \ 34386acbbe2SYe Li (defined(IMAGE_BL31) && SEPARATE_RWDATA_REGION) 34486acbbe2SYe Li 345f5478dedSAntonio Nino Diaz adrp x0, __DATA_RAM_START__ 346f5478dedSAntonio Nino Diaz add x0, x0, :lo12:__DATA_RAM_START__ 347f5478dedSAntonio Nino Diaz adrp x1, __DATA_ROM_START__ 348f5478dedSAntonio Nino Diaz add x1, x1, :lo12:__DATA_ROM_START__ 349f5478dedSAntonio Nino Diaz adrp x2, __DATA_RAM_END__ 350f5478dedSAntonio Nino Diaz add x2, x2, :lo12:__DATA_RAM_END__ 351f5478dedSAntonio Nino Diaz sub x2, x2, x0 352f5478dedSAntonio Nino Diaz bl memcpy16 353f5478dedSAntonio Nino Diaz#endif 354f5478dedSAntonio Nino Diaz .endif /* _init_c_runtime */ 355f5478dedSAntonio Nino Diaz 356f5478dedSAntonio Nino Diaz /* --------------------------------------------------------------------- 357f5478dedSAntonio Nino Diaz * Use SP_EL0 for the C runtime stack. 358f5478dedSAntonio Nino Diaz * --------------------------------------------------------------------- 359f5478dedSAntonio Nino Diaz */ 360f5478dedSAntonio Nino Diaz msr spsel, #0 361f5478dedSAntonio Nino Diaz 362f5478dedSAntonio Nino Diaz /* --------------------------------------------------------------------- 363f5478dedSAntonio Nino Diaz * Allocate a stack whose memory will be marked as Normal-IS-WBWA when 364f5478dedSAntonio Nino Diaz * the MMU is enabled. There is no risk of reading stale stack memory 365f5478dedSAntonio Nino Diaz * after enabling the MMU as only the primary CPU is running at the 366f5478dedSAntonio Nino Diaz * moment. 367f5478dedSAntonio Nino Diaz * --------------------------------------------------------------------- 368f5478dedSAntonio Nino Diaz */ 369f5478dedSAntonio Nino Diaz bl plat_set_my_stack 370f5478dedSAntonio Nino Diaz 371f5478dedSAntonio Nino Diaz#if STACK_PROTECTOR_ENABLED 372f5478dedSAntonio Nino Diaz .if \_init_c_runtime 373f5478dedSAntonio Nino Diaz bl update_stack_protector_canary 374f5478dedSAntonio Nino Diaz .endif /* _init_c_runtime */ 375f5478dedSAntonio Nino Diaz#endif 376f5478dedSAntonio Nino Diaz .endm 377f5478dedSAntonio Nino Diaz 3783b8456bdSManish V Badarkhe .macro apply_at_speculative_wa 3793b8456bdSManish V Badarkhe#if ERRATA_SPECULATIVE_AT 3803b8456bdSManish V Badarkhe /* 381d87c0e27SManish Pandey * This function expects x30 has been saved. 382d87c0e27SManish Pandey * Also, save x29 which will be used in the called function. 3833b8456bdSManish V Badarkhe */ 384d87c0e27SManish Pandey str x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29] 3853b8456bdSManish V Badarkhe bl save_and_update_ptw_el1_sys_regs 386d87c0e27SManish Pandey ldr x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29] 3873b8456bdSManish V Badarkhe#endif 3883b8456bdSManish V Badarkhe .endm 3893b8456bdSManish V Badarkhe 3903b8456bdSManish V Badarkhe .macro restore_ptw_el1_sys_regs 3913b8456bdSManish V Badarkhe#if ERRATA_SPECULATIVE_AT 3923b8456bdSManish V Badarkhe /* ----------------------------------------------------------- 3933b8456bdSManish V Badarkhe * In case of ERRATA_SPECULATIVE_AT, must follow below order 3943b8456bdSManish V Badarkhe * to ensure that page table walk is not enabled until 3953b8456bdSManish V Badarkhe * restoration of all EL1 system registers. TCR_EL1 register 3963b8456bdSManish V Badarkhe * should be updated at the end which restores previous page 3973b8456bdSManish V Badarkhe * table walk setting of stage1 i.e.(TCR_EL1.EPDx) bits. ISB 3983b8456bdSManish V Badarkhe * ensures that CPU does below steps in order. 3993b8456bdSManish V Badarkhe * 4003b8456bdSManish V Badarkhe * 1. Ensure all other system registers are written before 4013b8456bdSManish V Badarkhe * updating SCTLR_EL1 using ISB. 4023b8456bdSManish V Badarkhe * 2. Restore SCTLR_EL1 register. 4033b8456bdSManish V Badarkhe * 3. Ensure SCTLR_EL1 written successfully using ISB. 4043b8456bdSManish V Badarkhe * 4. Restore TCR_EL1 register. 4053b8456bdSManish V Badarkhe * ----------------------------------------------------------- 4063b8456bdSManish V Badarkhe */ 4073b8456bdSManish V Badarkhe isb 40859b7c0a0SJayanth Dodderi Chidanand ldp x28, x29, [sp, #CTX_ERRATA_SPEC_AT_OFFSET + CTX_ERRATA_SPEC_AT_SCTLR_EL1] 4093b8456bdSManish V Badarkhe msr sctlr_el1, x28 4103b8456bdSManish V Badarkhe isb 4113b8456bdSManish V Badarkhe msr tcr_el1, x29 4123b8456bdSManish V Badarkhe#endif 4133b8456bdSManish V Badarkhe .endm 4143b8456bdSManish V Badarkhe 415461c0a5dSElizabeth Ho/* ----------------------------------------------------------------- 416461c0a5dSElizabeth Ho * The below macro reads SCR_EL3 from the context structure to 417461c0a5dSElizabeth Ho * determine the security state of the context upon ERET. 418461c0a5dSElizabeth Ho * ------------------------------------------------------------------ 419461c0a5dSElizabeth Ho */ 420461c0a5dSElizabeth Ho .macro get_security_state _ret:req, _scr_reg:req 421461c0a5dSElizabeth Ho ubfx \_ret, \_scr_reg, #SCR_NSE_SHIFT, #1 422461c0a5dSElizabeth Ho cmp \_ret, #1 423461c0a5dSElizabeth Ho beq realm_state 424461c0a5dSElizabeth Ho bfi \_ret, \_scr_reg, #0, #1 425461c0a5dSElizabeth Ho b end 426461c0a5dSElizabeth Ho realm_state: 427461c0a5dSElizabeth Ho mov \_ret, #2 428461c0a5dSElizabeth Ho end: 429461c0a5dSElizabeth Ho .endm 430461c0a5dSElizabeth Ho 43140e5f7a5SJayanth Dodderi Chidanand/*----------------------------------------------------------------------------- 43240e5f7a5SJayanth Dodderi Chidanand * Helper macro to configure EL3 registers we care about, while executing 43340e5f7a5SJayanth Dodderi Chidanand * at EL3/Root world. Root world has its own execution environment and 43440e5f7a5SJayanth Dodderi Chidanand * needs to have its settings configured to be independent of other worlds. 43540e5f7a5SJayanth Dodderi Chidanand * ----------------------------------------------------------------------------- 43640e5f7a5SJayanth Dodderi Chidanand */ 43740e5f7a5SJayanth Dodderi Chidanand .macro setup_el3_execution_context 43840e5f7a5SJayanth Dodderi Chidanand 43940e5f7a5SJayanth Dodderi Chidanand /* --------------------------------------------------------------------- 44040e5f7a5SJayanth Dodderi Chidanand * The following registers need to be part of separate root context 44140e5f7a5SJayanth Dodderi Chidanand * as their values are of importance during EL3 execution. 44240e5f7a5SJayanth Dodderi Chidanand * Hence these registers are overwritten to their intital values, 44340e5f7a5SJayanth Dodderi Chidanand * irrespective of whichever world they return from to ensure EL3 has a 44440e5f7a5SJayanth Dodderi Chidanand * consistent execution context throughout the lifetime of TF-A. 44540e5f7a5SJayanth Dodderi Chidanand * 44640e5f7a5SJayanth Dodderi Chidanand * DAIF.A: Enable External Aborts and SError Interrupts at EL3. 44740e5f7a5SJayanth Dodderi Chidanand * 44840e5f7a5SJayanth Dodderi Chidanand * MDCR_EL3.SDD: Set to one to disable AArch64 Secure self-hosted debug. 44940e5f7a5SJayanth Dodderi Chidanand * Debug exceptions, other than Breakpoint Instruction exceptions, are 45040e5f7a5SJayanth Dodderi Chidanand * disabled from all ELs in Secure state. 45140e5f7a5SJayanth Dodderi Chidanand * 45240e5f7a5SJayanth Dodderi Chidanand * SCR_EL3.EA: Set to one to enable SError interrupts at EL3. 45340e5f7a5SJayanth Dodderi Chidanand * 45440e5f7a5SJayanth Dodderi Chidanand * SCR_EL3.SIF: Set to one to disable instruction fetches from 45540e5f7a5SJayanth Dodderi Chidanand * Non-secure memory. 45640e5f7a5SJayanth Dodderi Chidanand * 45740e5f7a5SJayanth Dodderi Chidanand * PMCR_EL0.DP: Set to one so that the cycle counter, 45840e5f7a5SJayanth Dodderi Chidanand * PMCCNTR_EL0 does not count when event counting is prohibited. 45940e5f7a5SJayanth Dodderi Chidanand * Necessary on PMUv3 <= p7 where MDCR_EL3.{SCCD,MCCD} are not 46040e5f7a5SJayanth Dodderi Chidanand * available. 46140e5f7a5SJayanth Dodderi Chidanand * 4620a580b51SBoyan Karatotev * CPTR_EL3.EZ: Set to one so that accesses to ZCR_EL3 do not trap 4630a580b51SBoyan Karatotev * CPTR_EL3.TFP: Set to zero so that advanced SIMD operations don't trap 4640a580b51SBoyan Karatotev * CPTR_EL3.ESM: Set to one so that SME related registers don't trap 4650a580b51SBoyan Karatotev * 46640e5f7a5SJayanth Dodderi Chidanand * PSTATE.DIT: Set to one to enable the Data Independent Timing (DIT) 46740e5f7a5SJayanth Dodderi Chidanand * functionality, if implemented in EL3. 46840e5f7a5SJayanth Dodderi Chidanand * --------------------------------------------------------------------- 46940e5f7a5SJayanth Dodderi Chidanand */ 47040e5f7a5SJayanth Dodderi Chidanand msr daifclr, #DAIF_ABT_BIT 47140e5f7a5SJayanth Dodderi Chidanand 47240e5f7a5SJayanth Dodderi Chidanand mrs x15, mdcr_el3 47340e5f7a5SJayanth Dodderi Chidanand orr x15, x15, #MDCR_SDD_BIT 47440e5f7a5SJayanth Dodderi Chidanand msr mdcr_el3, x15 47540e5f7a5SJayanth Dodderi Chidanand 47640e5f7a5SJayanth Dodderi Chidanand mrs x15, scr_el3 47740e5f7a5SJayanth Dodderi Chidanand orr x15, x15, #SCR_EA_BIT 47840e5f7a5SJayanth Dodderi Chidanand orr x15, x15, #SCR_SIF_BIT 47940e5f7a5SJayanth Dodderi Chidanand msr scr_el3, x15 48040e5f7a5SJayanth Dodderi Chidanand 48140e5f7a5SJayanth Dodderi Chidanand mrs x15, pmcr_el0 48240e5f7a5SJayanth Dodderi Chidanand orr x15, x15, #PMCR_EL0_DP_BIT 48340e5f7a5SJayanth Dodderi Chidanand msr pmcr_el0, x15 48440e5f7a5SJayanth Dodderi Chidanand 4850a580b51SBoyan Karatotev mrs x15, cptr_el3 4860a580b51SBoyan Karatotev orr x15, x15, #CPTR_EZ_BIT 4870a580b51SBoyan Karatotev orr x15, x15, #ESM_BIT 4880a580b51SBoyan Karatotev bic x15, x15, #TFP_BIT 4890a580b51SBoyan Karatotev msr cptr_el3, x15 4900a580b51SBoyan Karatotev 49140e5f7a5SJayanth Dodderi Chidanand#if ENABLE_FEAT_DIT 49240e5f7a5SJayanth Dodderi Chidanand#if ENABLE_FEAT_DIT > 1 49340e5f7a5SJayanth Dodderi Chidanand mrs x15, id_aa64pfr0_el1 49440e5f7a5SJayanth Dodderi Chidanand ubfx x15, x15, #ID_AA64PFR0_DIT_SHIFT, #ID_AA64PFR0_DIT_LENGTH 49540e5f7a5SJayanth Dodderi Chidanand cbz x15, 1f 49640e5f7a5SJayanth Dodderi Chidanand#endif 49740e5f7a5SJayanth Dodderi Chidanand mov x15, #DIT_BIT 49840e5f7a5SJayanth Dodderi Chidanand msr DIT, x15 49940e5f7a5SJayanth Dodderi Chidanand 1: 50040e5f7a5SJayanth Dodderi Chidanand#endif 50140e5f7a5SJayanth Dodderi Chidanand 50240e5f7a5SJayanth Dodderi Chidanand isb 50340e5f7a5SJayanth Dodderi Chidanand .endm 50440e5f7a5SJayanth Dodderi Chidanand 505f5478dedSAntonio Nino Diaz#endif /* EL3_COMMON_MACROS_S */ 506