1f5478dedSAntonio Nino Diaz/* 242d4d3baSArvind Ram Prakash * Copyright (c) 2015-2023, 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. 35f5478dedSAntonio Nino Diaz * --------------------------------------------------------------------- 36f5478dedSAntonio Nino Diaz */ 37f5478dedSAntonio Nino Diaz mov x1, #(SCTLR_I_BIT | SCTLR_A_BIT | SCTLR_SA_BIT) 38f5478dedSAntonio Nino Diaz mrs x0, sctlr_el3 39f5478dedSAntonio Nino Diaz orr x0, x0, x1 40f5478dedSAntonio Nino Diaz msr sctlr_el3, x0 41f5478dedSAntonio Nino Diaz isb 42f5478dedSAntonio Nino Diaz 43f5478dedSAntonio Nino Diaz#ifdef IMAGE_BL31 44f5478dedSAntonio Nino Diaz /* --------------------------------------------------------------------- 45f5478dedSAntonio Nino Diaz * Initialise the per-cpu cache pointer to the CPU. 46f5478dedSAntonio Nino Diaz * This is done early to enable crash reporting to have access to crash 47f5478dedSAntonio Nino Diaz * stack. Since crash reporting depends on cpu_data to report the 48f5478dedSAntonio Nino Diaz * unhandled exception, not doing so can lead to recursive exceptions 49f5478dedSAntonio Nino Diaz * due to a NULL TPIDR_EL3. 50f5478dedSAntonio Nino Diaz * --------------------------------------------------------------------- 51f5478dedSAntonio Nino Diaz */ 52f5478dedSAntonio Nino Diaz bl init_cpu_data_ptr 53f5478dedSAntonio Nino Diaz#endif /* IMAGE_BL31 */ 54f5478dedSAntonio Nino Diaz 55f5478dedSAntonio Nino Diaz /* --------------------------------------------------------------------- 56f5478dedSAntonio Nino Diaz * Initialise SCR_EL3, setting all fields rather than relying on hw. 57f5478dedSAntonio Nino Diaz * All fields are architecturally UNKNOWN on reset. The following fields 58f5478dedSAntonio Nino Diaz * do not change during the TF lifetime. The remaining fields are set to 59f5478dedSAntonio Nino Diaz * zero here but are updated ahead of transitioning to a lower EL in the 60f5478dedSAntonio Nino Diaz * function cm_init_context_common(). 61f5478dedSAntonio Nino Diaz * 62f5478dedSAntonio Nino Diaz * SCR_EL3.SIF: Set to one to disable instruction fetches from 63f5478dedSAntonio Nino Diaz * Non-secure memory. 64f5478dedSAntonio Nino Diaz * 65f5478dedSAntonio Nino Diaz * SCR_EL3.EA: Set to one to route External Aborts and SError Interrupts 66f5478dedSAntonio Nino Diaz * to EL3 when executing at any EL. 67*8815cdafSManish Pandey * 68*8815cdafSManish Pandey * SCR_EL3.EEL2: Set to one if S-EL2 is present and enabled. 69*8815cdafSManish Pandey * 70*8815cdafSManish Pandey * NOTE: Modifying EEL2 bit along with EA bit ensures that we mitigate 71*8815cdafSManish Pandey * against ERRATA_V2_3099206. 72f5478dedSAntonio Nino Diaz * --------------------------------------------------------------------- 73f5478dedSAntonio Nino Diaz */ 74f0c96a2eSBoyan Karatotev mov_imm x0, (SCR_RESET_VAL | SCR_EA_BIT | SCR_SIF_BIT) 75*8815cdafSManish Pandey#if IMAGE_BL31 && defined(SPD_spmd) && SPMD_SPM_AT_SEL2 76*8815cdafSManish Pandey mrs x1, id_aa64pfr0_el1 77*8815cdafSManish Pandey and x1, x1, #(ID_AA64PFR0_SEL2_MASK << ID_AA64PFR0_SEL2_SHIFT) 78*8815cdafSManish Pandey cbz x1, 1f 79*8815cdafSManish Pandey orr x0, x0, #SCR_EEL2_BIT 80*8815cdafSManish Pandey#endif 81*8815cdafSManish Pandey1: 82f5478dedSAntonio Nino Diaz msr scr_el3, x0 83f5478dedSAntonio Nino Diaz 84f5478dedSAntonio Nino Diaz /* --------------------------------------------------------------------- 85f5478dedSAntonio Nino Diaz * Initialise MDCR_EL3, setting all fields rather than relying on hw. 86f5478dedSAntonio Nino Diaz * Some fields are architecturally UNKNOWN on reset. 87f5478dedSAntonio Nino Diaz * 88f5478dedSAntonio Nino Diaz * MDCR_EL3.SDD: Set to one to disable AArch64 Secure self-hosted debug. 89f5478dedSAntonio Nino Diaz * Debug exceptions, other than Breakpoint Instruction exceptions, are 90f5478dedSAntonio Nino Diaz * disabled from all ELs in Secure state. 91f5478dedSAntonio Nino Diaz * 92f5478dedSAntonio Nino Diaz * MDCR_EL3.SPD32: Set to 0b10 to disable AArch32 Secure self-hosted 93f5478dedSAntonio Nino Diaz * privileged debug from S-EL1. 94f5478dedSAntonio Nino Diaz * 95f5478dedSAntonio Nino Diaz * MDCR_EL3.TDOSA: Set to zero so that EL2 and EL2 System register 96f5478dedSAntonio Nino Diaz * access to the powerdown debug registers do not trap to EL3. 97f5478dedSAntonio Nino Diaz * 98f5478dedSAntonio Nino Diaz * MDCR_EL3.TDA: Set to zero to allow EL0, EL1 and EL2 access to the 99f5478dedSAntonio Nino Diaz * debug registers, other than those registers that are controlled by 100f5478dedSAntonio Nino Diaz * MDCR_EL3.TDOSA. 101f5478dedSAntonio Nino Diaz */ 102ed4fc6f0SAntonio Nino Diaz mov_imm x0, ((MDCR_EL3_RESET_VAL | MDCR_SDD_BIT | \ 103c73686a1SBoyan Karatotev MDCR_SPD32(MDCR_SPD32_DISABLE)) & \ 104ece8f7d7SBoyan Karatotev ~(MDCR_TDOSA_BIT | MDCR_TDA_BIT)) 105ed4fc6f0SAntonio Nino Diaz 106f5478dedSAntonio Nino Diaz msr mdcr_el3, x0 107f5478dedSAntonio Nino Diaz 108f5478dedSAntonio Nino Diaz /* --------------------------------------------------------------------- 109f5478dedSAntonio Nino Diaz * Enable External Aborts and SError Interrupts now that the exception 110f5478dedSAntonio Nino Diaz * vectors have been setup. 111f5478dedSAntonio Nino Diaz * --------------------------------------------------------------------- 112f5478dedSAntonio Nino Diaz */ 113f5478dedSAntonio Nino Diaz msr daifclr, #DAIF_ABT_BIT 114f5478dedSAntonio Nino Diaz 115f5478dedSAntonio Nino Diaz /* --------------------------------------------------------------------- 116f5478dedSAntonio Nino Diaz * Initialise CPTR_EL3, setting all fields rather than relying on hw. 117f5478dedSAntonio Nino Diaz * All fields are architecturally UNKNOWN on reset. 118f0c96a2eSBoyan Karatotev * --------------------------------------------------------------------- 119f5478dedSAntonio Nino Diaz */ 120f0c96a2eSBoyan Karatotev mov_imm x0, CPTR_EL3_RESET_VAL 121f5478dedSAntonio Nino Diaz msr cptr_el3, x0 122f5478dedSAntonio Nino Diaz 123f5478dedSAntonio Nino Diaz /* 124f5478dedSAntonio Nino Diaz * If Data Independent Timing (DIT) functionality is implemented, 1257d33ffe4SDaniel Boulby * always enable DIT in EL3. 1267d33ffe4SDaniel Boulby * First assert that the FEAT_DIT build flag matches the feature id 1277d33ffe4SDaniel Boulby * register value for DIT. 128f5478dedSAntonio Nino Diaz */ 1297d33ffe4SDaniel Boulby#if ENABLE_FEAT_DIT 13088727fc3SAndre Przywara#if ENABLE_ASSERTIONS || ENABLE_FEAT_DIT > 1 131f5478dedSAntonio Nino Diaz mrs x0, id_aa64pfr0_el1 132f5478dedSAntonio Nino Diaz ubfx x0, x0, #ID_AA64PFR0_DIT_SHIFT, #ID_AA64PFR0_DIT_LENGTH 13388727fc3SAndre Przywara#if ENABLE_FEAT_DIT > 1 13488727fc3SAndre Przywara cbz x0, 1f 13588727fc3SAndre Przywara#else 136f5478dedSAntonio Nino Diaz cmp x0, #ID_AA64PFR0_DIT_SUPPORTED 1377d33ffe4SDaniel Boulby ASM_ASSERT(eq) 13888727fc3SAndre Przywara#endif 13988727fc3SAndre Przywara 1407d33ffe4SDaniel Boulby#endif /* ENABLE_ASSERTIONS */ 141f5478dedSAntonio Nino Diaz mov x0, #DIT_BIT 142f5478dedSAntonio Nino Diaz msr DIT, x0 14388727fc3SAndre Przywara1: 1447d33ffe4SDaniel Boulby#endif 145f5478dedSAntonio Nino Diaz .endm 146f5478dedSAntonio Nino Diaz 147f5478dedSAntonio Nino Diaz/* ----------------------------------------------------------------------------- 148f5478dedSAntonio Nino Diaz * This is the super set of actions that need to be performed during a cold boot 149f5478dedSAntonio Nino Diaz * or a warm boot in EL3. This code is shared by BL1 and BL31. 150f5478dedSAntonio Nino Diaz * 151f5478dedSAntonio Nino Diaz * This macro will always perform reset handling, architectural initialisations 152f5478dedSAntonio Nino Diaz * and stack setup. The rest of the actions are optional because they might not 153f5478dedSAntonio Nino Diaz * be needed, depending on the context in which this macro is called. This is 154f5478dedSAntonio Nino Diaz * why this macro is parameterised ; each parameter allows to enable/disable 155f5478dedSAntonio Nino Diaz * some actions. 156f5478dedSAntonio Nino Diaz * 157f5478dedSAntonio Nino Diaz * _init_sctlr: 158f5478dedSAntonio Nino Diaz * Whether the macro needs to initialise SCTLR_EL3, including configuring 159f5478dedSAntonio Nino Diaz * the endianness of data accesses. 160f5478dedSAntonio Nino Diaz * 161f5478dedSAntonio Nino Diaz * _warm_boot_mailbox: 162f5478dedSAntonio Nino Diaz * Whether the macro needs to detect the type of boot (cold/warm). The 163f5478dedSAntonio Nino Diaz * detection is based on the platform entrypoint address : if it is zero 164f5478dedSAntonio Nino Diaz * then it is a cold boot, otherwise it is a warm boot. In the latter case, 165f5478dedSAntonio Nino Diaz * this macro jumps on the platform entrypoint address. 166f5478dedSAntonio Nino Diaz * 167f5478dedSAntonio Nino Diaz * _secondary_cold_boot: 168f5478dedSAntonio Nino Diaz * Whether the macro needs to identify the CPU that is calling it: primary 169f5478dedSAntonio Nino Diaz * CPU or secondary CPU. The primary CPU will be allowed to carry on with 170f5478dedSAntonio Nino Diaz * the platform initialisations, while the secondaries will be put in a 171f5478dedSAntonio Nino Diaz * platform-specific state in the meantime. 172f5478dedSAntonio Nino Diaz * 173f5478dedSAntonio Nino Diaz * If the caller knows this macro will only be called by the primary CPU 174f5478dedSAntonio Nino Diaz * then this parameter can be defined to 0 to skip this step. 175f5478dedSAntonio Nino Diaz * 176f5478dedSAntonio Nino Diaz * _init_memory: 177f5478dedSAntonio Nino Diaz * Whether the macro needs to initialise the memory. 178f5478dedSAntonio Nino Diaz * 179f5478dedSAntonio Nino Diaz * _init_c_runtime: 180f5478dedSAntonio Nino Diaz * Whether the macro needs to initialise the C runtime environment. 181f5478dedSAntonio Nino Diaz * 182f5478dedSAntonio Nino Diaz * _exception_vectors: 183f5478dedSAntonio Nino Diaz * Address of the exception vectors to program in the VBAR_EL3 register. 184da90359bSManish Pandey * 185da90359bSManish Pandey * _pie_fixup_size: 186da90359bSManish Pandey * Size of memory region to fixup Global Descriptor Table (GDT). 187da90359bSManish Pandey * 188da90359bSManish Pandey * A non-zero value is expected when firmware needs GDT to be fixed-up. 189da90359bSManish Pandey * 190f5478dedSAntonio Nino Diaz * ----------------------------------------------------------------------------- 191f5478dedSAntonio Nino Diaz */ 192f5478dedSAntonio Nino Diaz .macro el3_entrypoint_common \ 193f5478dedSAntonio Nino Diaz _init_sctlr, _warm_boot_mailbox, _secondary_cold_boot, \ 194da90359bSManish Pandey _init_memory, _init_c_runtime, _exception_vectors, \ 195da90359bSManish Pandey _pie_fixup_size 196f5478dedSAntonio Nino Diaz 197f5478dedSAntonio Nino Diaz .if \_init_sctlr 198f5478dedSAntonio Nino Diaz /* ------------------------------------------------------------- 199f5478dedSAntonio Nino Diaz * This is the initialisation of SCTLR_EL3 and so must ensure 200f5478dedSAntonio Nino Diaz * that all fields are explicitly set rather than relying on hw. 201f5478dedSAntonio Nino Diaz * Some fields reset to an IMPLEMENTATION DEFINED value and 202f5478dedSAntonio Nino Diaz * others are architecturally UNKNOWN on reset. 203f5478dedSAntonio Nino Diaz * 204f5478dedSAntonio Nino Diaz * SCTLR.EE: Set the CPU endianness before doing anything that 205f5478dedSAntonio Nino Diaz * might involve memory reads or writes. Set to zero to select 206f5478dedSAntonio Nino Diaz * Little Endian. 207f5478dedSAntonio Nino Diaz * 208f5478dedSAntonio Nino Diaz * SCTLR_EL3.WXN: For the EL3 translation regime, this field can 209f5478dedSAntonio Nino Diaz * force all memory regions that are writeable to be treated as 210f5478dedSAntonio Nino Diaz * XN (Execute-never). Set to zero so that this control has no 211f5478dedSAntonio Nino Diaz * effect on memory access permissions. 212f5478dedSAntonio Nino Diaz * 213f5478dedSAntonio Nino Diaz * SCTLR_EL3.SA: Set to zero to disable Stack Alignment check. 214f5478dedSAntonio Nino Diaz * 215f5478dedSAntonio Nino Diaz * SCTLR_EL3.A: Set to zero to disable Alignment fault checking. 216f5478dedSAntonio Nino Diaz * 217f5478dedSAntonio Nino Diaz * SCTLR.DSSBS: Set to zero to disable speculation store bypass 218f5478dedSAntonio Nino Diaz * safe behaviour upon exception entry to EL3. 219f5478dedSAntonio Nino Diaz * ------------------------------------------------------------- 220f5478dedSAntonio Nino Diaz */ 221f5478dedSAntonio Nino Diaz mov_imm x0, (SCTLR_RESET_VAL & ~(SCTLR_EE_BIT | SCTLR_WXN_BIT \ 222f5478dedSAntonio Nino Diaz | SCTLR_SA_BIT | SCTLR_A_BIT | SCTLR_DSSBS_BIT)) 223970a4a8dSManish Pandey#if ENABLE_FEAT_RAS 2246597fcf1SManish Pandey /* If FEAT_RAS is present assume FEAT_IESB is also present */ 2256597fcf1SManish Pandey orr x0, x0, #SCTLR_IESB_BIT 2266597fcf1SManish Pandey#endif 227f5478dedSAntonio Nino Diaz msr sctlr_el3, x0 228f5478dedSAntonio Nino Diaz isb 229f5478dedSAntonio Nino Diaz .endif /* _init_sctlr */ 230f5478dedSAntonio Nino Diaz 231f5478dedSAntonio Nino Diaz .if \_warm_boot_mailbox 232f5478dedSAntonio Nino Diaz /* ------------------------------------------------------------- 233f5478dedSAntonio Nino Diaz * This code will be executed for both warm and cold resets. 234f5478dedSAntonio Nino Diaz * Now is the time to distinguish between the two. 235f5478dedSAntonio Nino Diaz * Query the platform entrypoint address and if it is not zero 236f5478dedSAntonio Nino Diaz * then it means it is a warm boot so jump to this address. 237f5478dedSAntonio Nino Diaz * ------------------------------------------------------------- 238f5478dedSAntonio Nino Diaz */ 239f5478dedSAntonio Nino Diaz bl plat_get_my_entrypoint 240f5478dedSAntonio Nino Diaz cbz x0, do_cold_boot 241f5478dedSAntonio Nino Diaz br x0 242f5478dedSAntonio Nino Diaz 243f5478dedSAntonio Nino Diaz do_cold_boot: 244f5478dedSAntonio Nino Diaz .endif /* _warm_boot_mailbox */ 245f5478dedSAntonio Nino Diaz 246da90359bSManish Pandey .if \_pie_fixup_size 247da90359bSManish Pandey#if ENABLE_PIE 248da90359bSManish Pandey /* 249da90359bSManish Pandey * ------------------------------------------------------------ 250da90359bSManish Pandey * If PIE is enabled fixup the Global descriptor Table only 251da90359bSManish Pandey * once during primary core cold boot path. 252da90359bSManish Pandey * 253da90359bSManish Pandey * Compile time base address, required for fixup, is calculated 254da90359bSManish Pandey * using "pie_fixup" label present within first page. 255da90359bSManish Pandey * ------------------------------------------------------------ 256da90359bSManish Pandey */ 257da90359bSManish Pandey pie_fixup: 258da90359bSManish Pandey ldr x0, =pie_fixup 259d7b5f408SJimmy Brisson and x0, x0, #~(PAGE_SIZE_MASK) 260da90359bSManish Pandey mov_imm x1, \_pie_fixup_size 261da90359bSManish Pandey add x1, x1, x0 262da90359bSManish Pandey bl fixup_gdt_reloc 263da90359bSManish Pandey#endif /* ENABLE_PIE */ 264da90359bSManish Pandey .endif /* _pie_fixup_size */ 265da90359bSManish Pandey 266f5478dedSAntonio Nino Diaz /* --------------------------------------------------------------------- 267f5478dedSAntonio Nino Diaz * Set the exception vectors. 268f5478dedSAntonio Nino Diaz * --------------------------------------------------------------------- 269f5478dedSAntonio Nino Diaz */ 270f5478dedSAntonio Nino Diaz adr x0, \_exception_vectors 271f5478dedSAntonio Nino Diaz msr vbar_el3, x0 272f5478dedSAntonio Nino Diaz isb 273f5478dedSAntonio Nino Diaz 2746c09af9fSZelalem Aweke#if !(defined(IMAGE_BL2) && ENABLE_RME) 275f5478dedSAntonio Nino Diaz /* --------------------------------------------------------------------- 276f5478dedSAntonio Nino Diaz * It is a cold boot. 277f5478dedSAntonio Nino Diaz * Perform any processor specific actions upon reset e.g. cache, TLB 278f5478dedSAntonio Nino Diaz * invalidations etc. 279f5478dedSAntonio Nino Diaz * --------------------------------------------------------------------- 280f5478dedSAntonio Nino Diaz */ 281f5478dedSAntonio Nino Diaz bl reset_handler 2826c09af9fSZelalem Aweke#endif 283f5478dedSAntonio Nino Diaz 284f5478dedSAntonio Nino Diaz el3_arch_init_common 285f5478dedSAntonio Nino Diaz 286f5478dedSAntonio Nino Diaz .if \_secondary_cold_boot 287f5478dedSAntonio Nino Diaz /* ------------------------------------------------------------- 288f5478dedSAntonio Nino Diaz * Check if this is a primary or secondary CPU cold boot. 289f5478dedSAntonio Nino Diaz * The primary CPU will set up the platform while the 290f5478dedSAntonio Nino Diaz * secondaries are placed in a platform-specific state until the 291f5478dedSAntonio Nino Diaz * primary CPU performs the necessary actions to bring them out 292f5478dedSAntonio Nino Diaz * of that state and allows entry into the OS. 293f5478dedSAntonio Nino Diaz * ------------------------------------------------------------- 294f5478dedSAntonio Nino Diaz */ 295f5478dedSAntonio Nino Diaz bl plat_is_my_cpu_primary 296f5478dedSAntonio Nino Diaz cbnz w0, do_primary_cold_boot 297f5478dedSAntonio Nino Diaz 298f5478dedSAntonio Nino Diaz /* This is a cold boot on a secondary CPU */ 299f5478dedSAntonio Nino Diaz bl plat_secondary_cold_boot_setup 300f5478dedSAntonio Nino Diaz /* plat_secondary_cold_boot_setup() is not supposed to return */ 301f5478dedSAntonio Nino Diaz bl el3_panic 302f5478dedSAntonio Nino Diaz 303f5478dedSAntonio Nino Diaz do_primary_cold_boot: 304f5478dedSAntonio Nino Diaz .endif /* _secondary_cold_boot */ 305f5478dedSAntonio Nino Diaz 306f5478dedSAntonio Nino Diaz /* --------------------------------------------------------------------- 307f5478dedSAntonio Nino Diaz * Initialize memory now. Secondary CPU initialization won't get to this 308f5478dedSAntonio Nino Diaz * point. 309f5478dedSAntonio Nino Diaz * --------------------------------------------------------------------- 310f5478dedSAntonio Nino Diaz */ 311f5478dedSAntonio Nino Diaz 312f5478dedSAntonio Nino Diaz .if \_init_memory 313f5478dedSAntonio Nino Diaz bl platform_mem_init 314f5478dedSAntonio Nino Diaz .endif /* _init_memory */ 315f5478dedSAntonio Nino Diaz 316f5478dedSAntonio Nino Diaz /* --------------------------------------------------------------------- 317f5478dedSAntonio Nino Diaz * Init C runtime environment: 318f5478dedSAntonio Nino Diaz * - Zero-initialise the NOBITS sections. There are 2 of them: 319f5478dedSAntonio Nino Diaz * - the .bss section; 320f5478dedSAntonio Nino Diaz * - the coherent memory section (if any). 321f5478dedSAntonio Nino Diaz * - Relocate the data section from ROM to RAM, if required. 322f5478dedSAntonio Nino Diaz * --------------------------------------------------------------------- 323f5478dedSAntonio Nino Diaz */ 324f5478dedSAntonio Nino Diaz .if \_init_c_runtime 3256c09af9fSZelalem Aweke#if defined(IMAGE_BL31) || (defined(IMAGE_BL2) && \ 32642d4d3baSArvind Ram Prakash ((RESET_TO_BL2 && BL2_INV_DCACHE) || ENABLE_RME)) 327f5478dedSAntonio Nino Diaz /* ------------------------------------------------------------- 328f5478dedSAntonio Nino Diaz * Invalidate the RW memory used by the BL31 image. This 329f5478dedSAntonio Nino Diaz * includes the data and NOBITS sections. This is done to 330f5478dedSAntonio Nino Diaz * safeguard against possible corruption of this memory by 331f5478dedSAntonio Nino Diaz * dirty cache lines in a system cache as a result of use by 332596d20d9SZelalem Aweke * an earlier boot loader stage. If PIE is enabled however, 333596d20d9SZelalem Aweke * RO sections including the GOT may be modified during 334596d20d9SZelalem Aweke * pie fixup. Therefore, to be on the safe side, invalidate 335596d20d9SZelalem Aweke * the entire image region if PIE is enabled. 336f5478dedSAntonio Nino Diaz * ------------------------------------------------------------- 337f5478dedSAntonio Nino Diaz */ 338596d20d9SZelalem Aweke#if ENABLE_PIE 339596d20d9SZelalem Aweke#if SEPARATE_CODE_AND_RODATA 340596d20d9SZelalem Aweke adrp x0, __TEXT_START__ 341596d20d9SZelalem Aweke add x0, x0, :lo12:__TEXT_START__ 342596d20d9SZelalem Aweke#else 343596d20d9SZelalem Aweke adrp x0, __RO_START__ 344596d20d9SZelalem Aweke add x0, x0, :lo12:__RO_START__ 345596d20d9SZelalem Aweke#endif /* SEPARATE_CODE_AND_RODATA */ 346596d20d9SZelalem Aweke#else 347f5478dedSAntonio Nino Diaz adrp x0, __RW_START__ 348f5478dedSAntonio Nino Diaz add x0, x0, :lo12:__RW_START__ 349596d20d9SZelalem Aweke#endif /* ENABLE_PIE */ 350f5478dedSAntonio Nino Diaz adrp x1, __RW_END__ 351f5478dedSAntonio Nino Diaz add x1, x1, :lo12:__RW_END__ 352f5478dedSAntonio Nino Diaz sub x1, x1, x0 353f5478dedSAntonio Nino Diaz bl inv_dcache_range 354f8578e64SSamuel Holland#if defined(IMAGE_BL31) && SEPARATE_NOBITS_REGION 355f8578e64SSamuel Holland adrp x0, __NOBITS_START__ 356f8578e64SSamuel Holland add x0, x0, :lo12:__NOBITS_START__ 357f8578e64SSamuel Holland adrp x1, __NOBITS_END__ 358f8578e64SSamuel Holland add x1, x1, :lo12:__NOBITS_END__ 359f8578e64SSamuel Holland sub x1, x1, x0 360f8578e64SSamuel Holland bl inv_dcache_range 361f8578e64SSamuel Holland#endif 36296a8ed14SJiafei Pan#if defined(IMAGE_BL2) && SEPARATE_BL2_NOLOAD_REGION 36396a8ed14SJiafei Pan adrp x0, __BL2_NOLOAD_START__ 36496a8ed14SJiafei Pan add x0, x0, :lo12:__BL2_NOLOAD_START__ 36596a8ed14SJiafei Pan adrp x1, __BL2_NOLOAD_END__ 36696a8ed14SJiafei Pan add x1, x1, :lo12:__BL2_NOLOAD_END__ 36796a8ed14SJiafei Pan sub x1, x1, x0 36896a8ed14SJiafei Pan bl inv_dcache_range 36996a8ed14SJiafei Pan#endif 370f5478dedSAntonio Nino Diaz#endif 371f5478dedSAntonio Nino Diaz adrp x0, __BSS_START__ 372f5478dedSAntonio Nino Diaz add x0, x0, :lo12:__BSS_START__ 373f5478dedSAntonio Nino Diaz 374f5478dedSAntonio Nino Diaz adrp x1, __BSS_END__ 375f5478dedSAntonio Nino Diaz add x1, x1, :lo12:__BSS_END__ 376f5478dedSAntonio Nino Diaz sub x1, x1, x0 377f5478dedSAntonio Nino Diaz bl zeromem 378f5478dedSAntonio Nino Diaz 379f5478dedSAntonio Nino Diaz#if USE_COHERENT_MEM 380f5478dedSAntonio Nino Diaz adrp x0, __COHERENT_RAM_START__ 381f5478dedSAntonio Nino Diaz add x0, x0, :lo12:__COHERENT_RAM_START__ 382f5478dedSAntonio Nino Diaz adrp x1, __COHERENT_RAM_END_UNALIGNED__ 383f5478dedSAntonio Nino Diaz add x1, x1, :lo12: __COHERENT_RAM_END_UNALIGNED__ 384f5478dedSAntonio Nino Diaz sub x1, x1, x0 385f5478dedSAntonio Nino Diaz bl zeromem 386f5478dedSAntonio Nino Diaz#endif 387f5478dedSAntonio Nino Diaz 38842d4d3baSArvind Ram Prakash#if defined(IMAGE_BL1) || \ 38942d4d3baSArvind Ram Prakash (defined(IMAGE_BL2) && RESET_TO_BL2 && BL2_IN_XIP_MEM) 390f5478dedSAntonio Nino Diaz adrp x0, __DATA_RAM_START__ 391f5478dedSAntonio Nino Diaz add x0, x0, :lo12:__DATA_RAM_START__ 392f5478dedSAntonio Nino Diaz adrp x1, __DATA_ROM_START__ 393f5478dedSAntonio Nino Diaz add x1, x1, :lo12:__DATA_ROM_START__ 394f5478dedSAntonio Nino Diaz adrp x2, __DATA_RAM_END__ 395f5478dedSAntonio Nino Diaz add x2, x2, :lo12:__DATA_RAM_END__ 396f5478dedSAntonio Nino Diaz sub x2, x2, x0 397f5478dedSAntonio Nino Diaz bl memcpy16 398f5478dedSAntonio Nino Diaz#endif 399f5478dedSAntonio Nino Diaz .endif /* _init_c_runtime */ 400f5478dedSAntonio Nino Diaz 401f5478dedSAntonio Nino Diaz /* --------------------------------------------------------------------- 402f5478dedSAntonio Nino Diaz * Use SP_EL0 for the C runtime stack. 403f5478dedSAntonio Nino Diaz * --------------------------------------------------------------------- 404f5478dedSAntonio Nino Diaz */ 405f5478dedSAntonio Nino Diaz msr spsel, #0 406f5478dedSAntonio Nino Diaz 407f5478dedSAntonio Nino Diaz /* --------------------------------------------------------------------- 408f5478dedSAntonio Nino Diaz * Allocate a stack whose memory will be marked as Normal-IS-WBWA when 409f5478dedSAntonio Nino Diaz * the MMU is enabled. There is no risk of reading stale stack memory 410f5478dedSAntonio Nino Diaz * after enabling the MMU as only the primary CPU is running at the 411f5478dedSAntonio Nino Diaz * moment. 412f5478dedSAntonio Nino Diaz * --------------------------------------------------------------------- 413f5478dedSAntonio Nino Diaz */ 414f5478dedSAntonio Nino Diaz bl plat_set_my_stack 415f5478dedSAntonio Nino Diaz 416f5478dedSAntonio Nino Diaz#if STACK_PROTECTOR_ENABLED 417f5478dedSAntonio Nino Diaz .if \_init_c_runtime 418f5478dedSAntonio Nino Diaz bl update_stack_protector_canary 419f5478dedSAntonio Nino Diaz .endif /* _init_c_runtime */ 420f5478dedSAntonio Nino Diaz#endif 421f5478dedSAntonio Nino Diaz .endm 422f5478dedSAntonio Nino Diaz 4233b8456bdSManish V Badarkhe .macro apply_at_speculative_wa 4243b8456bdSManish V Badarkhe#if ERRATA_SPECULATIVE_AT 4253b8456bdSManish V Badarkhe /* 426d87c0e27SManish Pandey * This function expects x30 has been saved. 427d87c0e27SManish Pandey * Also, save x29 which will be used in the called function. 4283b8456bdSManish V Badarkhe */ 429d87c0e27SManish Pandey str x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29] 4303b8456bdSManish V Badarkhe bl save_and_update_ptw_el1_sys_regs 431d87c0e27SManish Pandey ldr x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29] 4323b8456bdSManish V Badarkhe#endif 4333b8456bdSManish V Badarkhe .endm 4343b8456bdSManish V Badarkhe 4353b8456bdSManish V Badarkhe .macro restore_ptw_el1_sys_regs 4363b8456bdSManish V Badarkhe#if ERRATA_SPECULATIVE_AT 4373b8456bdSManish V Badarkhe /* ----------------------------------------------------------- 4383b8456bdSManish V Badarkhe * In case of ERRATA_SPECULATIVE_AT, must follow below order 4393b8456bdSManish V Badarkhe * to ensure that page table walk is not enabled until 4403b8456bdSManish V Badarkhe * restoration of all EL1 system registers. TCR_EL1 register 4413b8456bdSManish V Badarkhe * should be updated at the end which restores previous page 4423b8456bdSManish V Badarkhe * table walk setting of stage1 i.e.(TCR_EL1.EPDx) bits. ISB 4433b8456bdSManish V Badarkhe * ensures that CPU does below steps in order. 4443b8456bdSManish V Badarkhe * 4453b8456bdSManish V Badarkhe * 1. Ensure all other system registers are written before 4463b8456bdSManish V Badarkhe * updating SCTLR_EL1 using ISB. 4473b8456bdSManish V Badarkhe * 2. Restore SCTLR_EL1 register. 4483b8456bdSManish V Badarkhe * 3. Ensure SCTLR_EL1 written successfully using ISB. 4493b8456bdSManish V Badarkhe * 4. Restore TCR_EL1 register. 4503b8456bdSManish V Badarkhe * ----------------------------------------------------------- 4513b8456bdSManish V Badarkhe */ 4523b8456bdSManish V Badarkhe isb 4533b8456bdSManish V Badarkhe ldp x28, x29, [sp, #CTX_EL1_SYSREGS_OFFSET + CTX_SCTLR_EL1] 4543b8456bdSManish V Badarkhe msr sctlr_el1, x28 4553b8456bdSManish V Badarkhe isb 4563b8456bdSManish V Badarkhe msr tcr_el1, x29 4573b8456bdSManish V Badarkhe#endif 4583b8456bdSManish V Badarkhe .endm 4593b8456bdSManish V Badarkhe 460461c0a5dSElizabeth Ho/* ----------------------------------------------------------------- 461461c0a5dSElizabeth Ho * The below macro reads SCR_EL3 from the context structure to 462461c0a5dSElizabeth Ho * determine the security state of the context upon ERET. 463461c0a5dSElizabeth Ho * ------------------------------------------------------------------ 464461c0a5dSElizabeth Ho */ 465461c0a5dSElizabeth Ho .macro get_security_state _ret:req, _scr_reg:req 466461c0a5dSElizabeth Ho ubfx \_ret, \_scr_reg, #SCR_NSE_SHIFT, #1 467461c0a5dSElizabeth Ho cmp \_ret, #1 468461c0a5dSElizabeth Ho beq realm_state 469461c0a5dSElizabeth Ho bfi \_ret, \_scr_reg, #0, #1 470461c0a5dSElizabeth Ho b end 471461c0a5dSElizabeth Ho realm_state: 472461c0a5dSElizabeth Ho mov \_ret, #2 473461c0a5dSElizabeth Ho end: 474461c0a5dSElizabeth Ho .endm 475461c0a5dSElizabeth Ho 476f5478dedSAntonio Nino Diaz#endif /* EL3_COMMON_MACROS_S */ 477