el2_common_macros.S 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. * Copyright (c) 2021-2024, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #ifndef EL2_COMMON_MACROS_S
  7. #define EL2_COMMON_MACROS_S
  8. #include <arch.h>
  9. #include <asm_macros.S>
  10. #include <context.h>
  11. #include <lib/xlat_tables/xlat_tables_defs.h>
  12. #include <platform_def.h>
  13. /*
  14. * Helper macro to initialise system registers at EL2.
  15. */
  16. .macro el2_arch_init_common
  17. /* ---------------------------------------------------------------------
  18. * SCTLR_EL2 has already been initialised - read current value before
  19. * modifying.
  20. *
  21. * SCTLR_EL2.I: Enable the instruction cache.
  22. *
  23. * SCTLR_EL2.SA: Enable Stack Alignment check. A SP alignment fault
  24. * exception is generated if a load or store instruction executed at
  25. * EL2 uses the SP as the base address and the SP is not aligned to a
  26. * 16-byte boundary.
  27. *
  28. * SCTLR_EL2.A: Enable Alignment fault checking. All instructions that
  29. * load or store one or more registers have an alignment check that the
  30. * address being accessed is aligned to the size of the data element(s)
  31. * being accessed.
  32. * ---------------------------------------------------------------------
  33. */
  34. mov x1, #(SCTLR_I_BIT | SCTLR_A_BIT | SCTLR_SA_BIT)
  35. mrs x0, sctlr_el2
  36. orr x0, x0, x1
  37. msr sctlr_el2, x0
  38. isb
  39. /* ---------------------------------------------------------------------
  40. * Initialise HCR_EL2, setting all fields rather than relying on HW.
  41. * All fields are architecturally UNKNOWN on reset. The following fields
  42. * do not change during the TF lifetime. The remaining fields are set to
  43. * zero here but are updated ahead of transitioning to a lower EL in the
  44. * function cm_init_context_common().
  45. *
  46. * HCR_EL2.TWE: Set to zero so that execution of WFE instructions at
  47. * EL2, EL1 and EL0 are not trapped to EL2.
  48. *
  49. * HCR_EL2.TWI: Set to zero so that execution of WFI instructions at
  50. * EL2, EL1 and EL0 are not trapped to EL2.
  51. *
  52. * HCR_EL2.HCD: Set to zero to enable HVC calls at EL1 and above,
  53. * from both Security states and both Execution states.
  54. *
  55. * HCR_EL2.TEA: Set to one to route External Aborts and SError
  56. * Interrupts to EL2 when executing at any EL.
  57. *
  58. * HCR_EL2.{API,APK}: For Armv8.3 pointer authentication feature,
  59. * disable traps to EL2 when accessing key registers or using
  60. * pointer authentication instructions from lower ELs.
  61. * ---------------------------------------------------------------------
  62. */
  63. mov_imm x0, ((HCR_RESET_VAL | HCR_TEA_BIT) \
  64. & ~(HCR_TWE_BIT | HCR_TWI_BIT | HCR_HCD_BIT))
  65. #if CTX_INCLUDE_PAUTH_REGS
  66. /*
  67. * If the pointer authentication registers are saved during world
  68. * switches, enable pointer authentication everywhere, as it is safe to
  69. * do so.
  70. */
  71. orr x0, x0, #(HCR_API_BIT | HCR_APK_BIT)
  72. #endif /* CTX_INCLUDE_PAUTH_REGS */
  73. msr hcr_el2, x0
  74. /* ---------------------------------------------------------------------
  75. * Initialise MDCR_EL2, setting all fields rather than relying on
  76. * hw. Some fields are architecturally UNKNOWN on reset.
  77. *
  78. * MDCR_EL2.TDOSA: Set to zero so that EL2 and EL2 System register
  79. * access to the powerdown debug registers do not trap to EL2.
  80. *
  81. * MDCR_EL2.TDA: Set to zero to allow EL0, EL1 and EL2 access to the
  82. * debug registers, other than those registers that are controlled by
  83. * MDCR_EL2.TDOSA.
  84. *
  85. * MDCR_EL2.TPM: Set to zero so that EL0, EL1, and EL2 System
  86. * register accesses to all Performance Monitors registers do not trap
  87. * to EL2.
  88. *
  89. * MDCR_EL2.HPMD: Set to zero so that event counting by the program-
  90. * mable counters PMEVCNTR<n>_EL0 is prohibited in Secure state. If
  91. * ARMv8.2 Debug is not implemented this bit does not have any effect
  92. * on the counters unless there is support for the implementation
  93. * defined authentication interface
  94. * ExternalSecureNoninvasiveDebugEnabled().
  95. * ---------------------------------------------------------------------
  96. */
  97. mov_imm x0, ((MDCR_EL2_RESET_VAL | \
  98. MDCR_SPD32(MDCR_SPD32_DISABLE)) \
  99. & ~(MDCR_EL2_HPMD_BIT | MDCR_TDOSA_BIT | \
  100. MDCR_TDA_BIT | MDCR_TPM_BIT))
  101. msr mdcr_el2, x0
  102. /* ---------------------------------------------------------------------
  103. * Initialise PMCR_EL0 setting all fields rather than relying
  104. * on hw. Some fields are architecturally UNKNOWN on reset.
  105. *
  106. * PMCR_EL0.DP: Set to one so that the cycle counter,
  107. * PMCCNTR_EL0 does not count when event counting is prohibited.
  108. *
  109. * PMCR_EL0.X: Set to zero to disable export of events.
  110. *
  111. * PMCR_EL0.D: Set to zero so that, when enabled, PMCCNTR_EL0
  112. * counts on every clock cycle.
  113. * ---------------------------------------------------------------------
  114. */
  115. mov_imm x0, ((PMCR_EL0_RESET_VAL | PMCR_EL0_DP_BIT) & \
  116. ~(PMCR_EL0_X_BIT | PMCR_EL0_D_BIT))
  117. msr pmcr_el0, x0
  118. /* ---------------------------------------------------------------------
  119. * Enable External Aborts and SError Interrupts now that the exception
  120. * vectors have been setup.
  121. * ---------------------------------------------------------------------
  122. */
  123. msr daifclr, #DAIF_ABT_BIT
  124. /* ---------------------------------------------------------------------
  125. * Initialise CPTR_EL2, setting all fields rather than relying on hw.
  126. * All fields are architecturally UNKNOWN on reset.
  127. *
  128. * CPTR_EL2.TCPAC: Set to zero so that any accesses to CPACR_EL1 do
  129. * not trap to EL2.
  130. *
  131. * CPTR_EL2.TTA: Set to zero so that System register accesses to the
  132. * trace registers do not trap to EL2.
  133. *
  134. * CPTR_EL2.TFP: Set to zero so that accesses to the V- or Z- registers
  135. * by Advanced SIMD, floating-point or SVE instructions (if implemented)
  136. * do not trap to EL2.
  137. */
  138. mov_imm x0, (CPTR_EL2_RESET_VAL & ~(TCPAC_BIT | TTA_BIT | TFP_BIT))
  139. msr cptr_el2, x0
  140. /*
  141. * If Data Independent Timing (DIT) functionality is implemented,
  142. * always enable DIT in EL2
  143. */
  144. mrs x0, id_aa64pfr0_el1
  145. ubfx x0, x0, #ID_AA64PFR0_DIT_SHIFT, #ID_AA64PFR0_DIT_LENGTH
  146. cmp x0, #DIT_IMPLEMENTED
  147. bne 1f
  148. mov x0, #DIT_BIT
  149. msr DIT, x0
  150. 1:
  151. .endm
  152. /* -----------------------------------------------------------------------------
  153. * This is the super set of actions that need to be performed during a cold boot
  154. * or a warm boot in EL2. This code is shared by BL1 and BL31.
  155. *
  156. * This macro will always perform reset handling, architectural initialisations
  157. * and stack setup. The rest of the actions are optional because they might not
  158. * be needed, depending on the context in which this macro is called. This is
  159. * why this macro is parameterised ; each parameter allows to enable/disable
  160. * some actions.
  161. *
  162. * _init_sctlr:
  163. * Whether the macro needs to initialise SCTLR_EL2, including configuring
  164. * the endianness of data accesses.
  165. *
  166. * _warm_boot_mailbox:
  167. * Whether the macro needs to detect the type of boot (cold/warm). The
  168. * detection is based on the platform entrypoint address : if it is zero
  169. * then it is a cold boot, otherwise it is a warm boot. In the latter case,
  170. * this macro jumps on the platform entrypoint address.
  171. *
  172. * _secondary_cold_boot:
  173. * Whether the macro needs to identify the CPU that is calling it: primary
  174. * CPU or secondary CPU. The primary CPU will be allowed to carry on with
  175. * the platform initialisations, while the secondaries will be put in a
  176. * platform-specific state in the meantime.
  177. *
  178. * If the caller knows this macro will only be called by the primary CPU
  179. * then this parameter can be defined to 0 to skip this step.
  180. *
  181. * _init_memory:
  182. * Whether the macro needs to initialise the memory.
  183. *
  184. * _init_c_runtime:
  185. * Whether the macro needs to initialise the C runtime environment.
  186. *
  187. * _exception_vectors:
  188. * Address of the exception vectors to program in the VBAR_EL2 register.
  189. *
  190. * _pie_fixup_size:
  191. * Size of memory region to fixup Global Descriptor Table (GDT).
  192. *
  193. * A non-zero value is expected when firmware needs GDT to be fixed-up.
  194. *
  195. * -----------------------------------------------------------------------------
  196. */
  197. .macro el2_entrypoint_common \
  198. _init_sctlr, _warm_boot_mailbox, _secondary_cold_boot, \
  199. _init_memory, _init_c_runtime, _exception_vectors, \
  200. _pie_fixup_size
  201. .if \_init_sctlr
  202. /* -------------------------------------------------------------
  203. * This is the initialisation of SCTLR_EL2 and so must ensure
  204. * that all fields are explicitly set rather than relying on hw.
  205. * Some fields reset to an IMPLEMENTATION DEFINED value and
  206. * others are architecturally UNKNOWN on reset.
  207. *
  208. * SCTLR.EE: Set the CPU endianness before doing anything that
  209. * might involve memory reads or writes. Set to zero to select
  210. * Little Endian.
  211. *
  212. * SCTLR_EL2.WXN: For the EL2 translation regime, this field can
  213. * force all memory regions that are writeable to be treated as
  214. * XN (Execute-never). Set to zero so that this control has no
  215. * effect on memory access permissions.
  216. *
  217. * SCTLR_EL2.SA: Set to zero to disable Stack Alignment check.
  218. *
  219. * SCTLR_EL2.A: Set to zero to disable Alignment fault checking.
  220. *
  221. * SCTLR.DSSBS: Set to zero to disable speculation store bypass
  222. * safe behaviour upon exception entry to EL2.
  223. * -------------------------------------------------------------
  224. */
  225. mov_imm x0, (SCTLR_RESET_VAL & ~(SCTLR_EE_BIT | SCTLR_WXN_BIT \
  226. | SCTLR_SA_BIT | SCTLR_A_BIT | SCTLR_DSSBS_BIT))
  227. msr sctlr_el2, x0
  228. isb
  229. .endif /* _init_sctlr */
  230. .if \_warm_boot_mailbox
  231. /* -------------------------------------------------------------
  232. * This code will be executed for both warm and cold resets.
  233. * Now is the time to distinguish between the two.
  234. * Query the platform entrypoint address and if it is not zero
  235. * then it means it is a warm boot so jump to this address.
  236. * -------------------------------------------------------------
  237. */
  238. bl plat_get_my_entrypoint
  239. cbz x0, do_cold_boot
  240. br x0
  241. do_cold_boot:
  242. .endif /* _warm_boot_mailbox */
  243. .if \_pie_fixup_size
  244. #if ENABLE_PIE
  245. /*
  246. * ------------------------------------------------------------
  247. * If PIE is enabled fixup the Global descriptor Table only
  248. * once during primary core cold boot path.
  249. *
  250. * Compile time base address, required for fixup, is calculated
  251. * using "pie_fixup" label present within first page.
  252. * ------------------------------------------------------------
  253. */
  254. pie_fixup:
  255. ldr x0, =pie_fixup
  256. and x0, x0, #~(PAGE_SIZE_MASK)
  257. mov_imm x1, \_pie_fixup_size
  258. add x1, x1, x0
  259. bl fixup_gdt_reloc
  260. #endif /* ENABLE_PIE */
  261. .endif /* _pie_fixup_size */
  262. /* ---------------------------------------------------------------------
  263. * Set the exception vectors.
  264. * ---------------------------------------------------------------------
  265. */
  266. adr x0, \_exception_vectors
  267. msr vbar_el2, x0
  268. isb
  269. /* ---------------------------------------------------------------------
  270. * It is a cold boot.
  271. * Perform any processor specific actions upon reset e.g. cache, TLB
  272. * invalidations etc.
  273. * ---------------------------------------------------------------------
  274. */
  275. bl reset_handler
  276. el2_arch_init_common
  277. .if \_secondary_cold_boot
  278. /* -------------------------------------------------------------
  279. * Check if this is a primary or secondary CPU cold boot.
  280. * The primary CPU will set up the platform while the
  281. * secondaries are placed in a platform-specific state until the
  282. * primary CPU performs the necessary actions to bring them out
  283. * of that state and allows entry into the OS.
  284. * -------------------------------------------------------------
  285. */
  286. bl plat_is_my_cpu_primary
  287. cbnz w0, do_primary_cold_boot
  288. /* This is a cold boot on a secondary CPU */
  289. bl plat_secondary_cold_boot_setup
  290. /* plat_secondary_cold_boot_setup() is not supposed to return */
  291. bl el2_panic
  292. do_primary_cold_boot:
  293. .endif /* _secondary_cold_boot */
  294. /* ---------------------------------------------------------------------
  295. * Initialize memory now. Secondary CPU initialization won't get to this
  296. * point.
  297. * ---------------------------------------------------------------------
  298. */
  299. .if \_init_memory
  300. bl platform_mem_init
  301. .endif /* _init_memory */
  302. /* ---------------------------------------------------------------------
  303. * Init C runtime environment:
  304. * - Zero-initialise the NOBITS sections. There are 2 of them:
  305. * - the .bss section;
  306. * - the coherent memory section (if any).
  307. * - Relocate the data section from ROM to RAM, if required.
  308. * ---------------------------------------------------------------------
  309. */
  310. .if \_init_c_runtime
  311. adrp x0, __BSS_START__
  312. add x0, x0, :lo12:__BSS_START__
  313. adrp x1, __BSS_END__
  314. add x1, x1, :lo12:__BSS_END__
  315. sub x1, x1, x0
  316. bl zeromem
  317. #if defined(IMAGE_BL1) || (defined(IMAGE_BL2) && \
  318. RESET_TO_BL2 && BL2_IN_XIP_MEM)
  319. adrp x0, __DATA_RAM_START__
  320. add x0, x0, :lo12:__DATA_RAM_START__
  321. adrp x1, __DATA_ROM_START__
  322. add x1, x1, :lo12:__DATA_ROM_START__
  323. adrp x2, __DATA_RAM_END__
  324. add x2, x2, :lo12:__DATA_RAM_END__
  325. sub x2, x2, x0
  326. bl memcpy16
  327. #endif
  328. .endif /* _init_c_runtime */
  329. /* ---------------------------------------------------------------------
  330. * Use SP_EL0 for the C runtime stack.
  331. * ---------------------------------------------------------------------
  332. */
  333. msr spsel, #0
  334. /* ---------------------------------------------------------------------
  335. * Allocate a stack whose memory will be marked as Normal-IS-WBWA when
  336. * the MMU is enabled. There is no risk of reading stale stack memory
  337. * after enabling the MMU as only the primary CPU is running at the
  338. * moment.
  339. * ---------------------------------------------------------------------
  340. */
  341. bl plat_set_my_stack
  342. #if STACK_PROTECTOR_ENABLED
  343. .if \_init_c_runtime
  344. bl update_stack_protector_canary
  345. .endif /* _init_c_runtime */
  346. #endif
  347. .endm
  348. .macro apply_at_speculative_wa
  349. #if ERRATA_SPECULATIVE_AT
  350. /*
  351. * This function expects x30 has been saved.
  352. * Also, save x29 which will be used in the called function.
  353. */
  354. str x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29]
  355. bl save_and_update_ptw_el1_sys_regs
  356. ldr x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29]
  357. #endif
  358. .endm
  359. .macro restore_ptw_el1_sys_regs
  360. #if ERRATA_SPECULATIVE_AT
  361. /* -----------------------------------------------------------
  362. * In case of ERRATA_SPECULATIVE_AT, must follow below order
  363. * to ensure that page table walk is not enabled until
  364. * restoration of all EL1 system registers. TCR_EL1 register
  365. * should be updated at the end which restores previous page
  366. * table walk setting of stage1 i.e.(TCR_EL1.EPDx) bits. ISB
  367. * ensures that CPU does below steps in order.
  368. *
  369. * 1. Ensure all other system registers are written before
  370. * updating SCTLR_EL1 using ISB.
  371. * 2. Restore SCTLR_EL1 register.
  372. * 3. Ensure SCTLR_EL1 written successfully using ISB.
  373. * 4. Restore TCR_EL1 register.
  374. * -----------------------------------------------------------
  375. */
  376. isb
  377. ldp x28, x29, [sp, #CTX_EL1_SYSREGS_OFFSET + CTX_SCTLR_EL1]
  378. msr sctlr_el1, x28
  379. isb
  380. msr tcr_el1, x29
  381. #endif
  382. .endm
  383. #endif /* EL2_COMMON_MACROS_S */