Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 1 | /* |
Anthony Steinhauser | 0f7e601 | 2020-01-07 15:44:06 -0800 | [diff] [blame] | 2 | * Copyright (c) 2013-2020, ARM Limited and Contributors. All rights reserved. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 5 | */ |
Antonio Nino Diaz | 5eb8837 | 2018-11-08 10:20:19 +0000 | [diff] [blame] | 6 | #ifndef ASM_MACROS_S |
| 7 | #define ASM_MACROS_S |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 8 | |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 9 | #include <arch.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 10 | #include <common/asm_macros_common.S> |
| 11 | #include <lib/spinlock.h> |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 12 | |
Jeenu Viswambharan | 58e8148 | 2018-04-27 15:06:57 +0100 | [diff] [blame] | 13 | /* |
| 14 | * TLBI instruction with type specifier that implements the workaround for |
Soby Mathew | 16d006b | 2019-05-03 13:17:56 +0100 | [diff] [blame] | 15 | * errata 813419 of Cortex-A57 or errata 1286807 of Cortex-A76. |
Jeenu Viswambharan | 58e8148 | 2018-04-27 15:06:57 +0100 | [diff] [blame] | 16 | */ |
Soby Mathew | 16d006b | 2019-05-03 13:17:56 +0100 | [diff] [blame] | 17 | #if ERRATA_A57_813419 || ERRATA_A76_1286807 |
Jeenu Viswambharan | 58e8148 | 2018-04-27 15:06:57 +0100 | [diff] [blame] | 18 | #define TLB_INVALIDATE(_type) \ |
| 19 | tlbi _type; \ |
| 20 | dsb ish; \ |
| 21 | tlbi _type |
| 22 | #else |
| 23 | #define TLB_INVALIDATE(_type) \ |
| 24 | tlbi _type |
| 25 | #endif |
| 26 | |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 27 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 28 | .macro func_prologue |
| 29 | stp x29, x30, [sp, #-0x10]! |
| 30 | mov x29,sp |
| 31 | .endm |
| 32 | |
| 33 | .macro func_epilogue |
| 34 | ldp x29, x30, [sp], #0x10 |
| 35 | .endm |
| 36 | |
| 37 | |
| 38 | .macro dcache_line_size reg, tmp |
Achin Gupta | 07f4e07 | 2014-02-02 12:02:23 +0000 | [diff] [blame] | 39 | mrs \tmp, ctr_el0 |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 40 | ubfx \tmp, \tmp, #16, #4 |
Achin Gupta | 07f4e07 | 2014-02-02 12:02:23 +0000 | [diff] [blame] | 41 | mov \reg, #4 |
| 42 | lsl \reg, \reg, \tmp |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 43 | .endm |
| 44 | |
| 45 | |
| 46 | .macro icache_line_size reg, tmp |
Achin Gupta | 07f4e07 | 2014-02-02 12:02:23 +0000 | [diff] [blame] | 47 | mrs \tmp, ctr_el0 |
| 48 | and \tmp, \tmp, #0xf |
| 49 | mov \reg, #4 |
| 50 | lsl \reg, \reg, \tmp |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 51 | .endm |
| 52 | |
| 53 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 54 | .macro smc_check label |
Andrew Thoelke | f977ed8 | 2014-04-28 12:32:02 +0100 | [diff] [blame] | 55 | mrs x0, esr_el3 |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 56 | ubfx x0, x0, #ESR_EC_SHIFT, #ESR_EC_LENGTH |
| 57 | cmp x0, #EC_AARCH64_SMC |
| 58 | b.ne $label |
| 59 | .endm |
| 60 | |
Sandrine Bailleux | 9e6ad6c | 2016-05-24 16:56:03 +0100 | [diff] [blame] | 61 | /* |
| 62 | * Declare the exception vector table, enforcing it is aligned on a |
| 63 | * 2KB boundary, as required by the ARMv8 architecture. |
Sandrine Bailleux | 618ba99 | 2016-05-24 16:22:59 +0100 | [diff] [blame] | 64 | * Use zero bytes as the fill value to be stored in the padding bytes |
| 65 | * so that it inserts illegal AArch64 instructions. This increases |
| 66 | * security, robustness and potentially facilitates debugging. |
Sandrine Bailleux | 9e6ad6c | 2016-05-24 16:56:03 +0100 | [diff] [blame] | 67 | */ |
Antonio Nino Diaz | c41f206 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 68 | .macro vector_base label, section_name=.vectors |
| 69 | .section \section_name, "ax" |
Sandrine Bailleux | 618ba99 | 2016-05-24 16:22:59 +0100 | [diff] [blame] | 70 | .align 11, 0 |
Sandrine Bailleux | 9e6ad6c | 2016-05-24 16:56:03 +0100 | [diff] [blame] | 71 | \label: |
| 72 | .endm |
| 73 | |
| 74 | /* |
| 75 | * Create an entry in the exception vector table, enforcing it is |
| 76 | * aligned on a 128-byte boundary, as required by the ARMv8 architecture. |
Sandrine Bailleux | 618ba99 | 2016-05-24 16:22:59 +0100 | [diff] [blame] | 77 | * Use zero bytes as the fill value to be stored in the padding bytes |
| 78 | * so that it inserts illegal AArch64 instructions. This increases |
| 79 | * security, robustness and potentially facilitates debugging. |
Sandrine Bailleux | 9e6ad6c | 2016-05-24 16:56:03 +0100 | [diff] [blame] | 80 | */ |
Antonio Nino Diaz | c41f206 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 81 | .macro vector_entry label, section_name=.vectors |
Douglas Raillard | efa50b5 | 2017-08-07 16:20:46 +0100 | [diff] [blame] | 82 | .cfi_sections .debug_frame |
Antonio Nino Diaz | c41f206 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 83 | .section \section_name, "ax" |
Sandrine Bailleux | 618ba99 | 2016-05-24 16:22:59 +0100 | [diff] [blame] | 84 | .align 7, 0 |
Douglas Raillard | efa50b5 | 2017-08-07 16:20:46 +0100 | [diff] [blame] | 85 | .type \label, %function |
Douglas Raillard | efa50b5 | 2017-08-07 16:20:46 +0100 | [diff] [blame] | 86 | .cfi_startproc |
Sandrine Bailleux | 9e6ad6c | 2016-05-24 16:56:03 +0100 | [diff] [blame] | 87 | \label: |
| 88 | .endm |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 89 | |
Jeenu Viswambharan | a7934d6 | 2014-02-07 15:53:18 +0000 | [diff] [blame] | 90 | /* |
Roberto Vargas | 95f30ab | 2018-04-17 11:31:43 +0100 | [diff] [blame] | 91 | * Add the bytes until fill the full exception vector, whose size is always |
| 92 | * 32 instructions. If there are more than 32 instructions in the |
| 93 | * exception vector then an error is emitted. |
| 94 | */ |
| 95 | .macro end_vector_entry label |
| 96 | .cfi_endproc |
| 97 | .fill \label + (32 * 4) - . |
| 98 | .endm |
| 99 | |
| 100 | /* |
Soby Mathew | b0082d2 | 2015-04-09 13:40:55 +0100 | [diff] [blame] | 101 | * This macro calculates the base address of the current CPU's MP stack |
| 102 | * using the plat_my_core_pos() index, the name of the stack storage |
| 103 | * and the size of each stack |
| 104 | * Out: X0 = physical address of stack base |
| 105 | * Clobber: X30, X1, X2 |
| 106 | */ |
| 107 | .macro get_my_mp_stack _name, _size |
Soby Mathew | fcaf1bd | 2018-10-12 16:40:28 +0100 | [diff] [blame] | 108 | bl plat_my_core_pos |
| 109 | adrp x2, (\_name + \_size) |
| 110 | add x2, x2, :lo12:(\_name + \_size) |
Soby Mathew | b0082d2 | 2015-04-09 13:40:55 +0100 | [diff] [blame] | 111 | mov x1, #\_size |
| 112 | madd x0, x0, x1, x2 |
| 113 | .endm |
| 114 | |
| 115 | /* |
Andrew Thoelke | 65668f9 | 2014-03-20 10:48:23 +0000 | [diff] [blame] | 116 | * This macro calculates the base address of a UP stack using the |
| 117 | * name of the stack storage and the size of the stack |
| 118 | * Out: X0 = physical address of stack base |
| 119 | */ |
| 120 | .macro get_up_stack _name, _size |
Soby Mathew | fcaf1bd | 2018-10-12 16:40:28 +0100 | [diff] [blame] | 121 | adrp x0, (\_name + \_size) |
| 122 | add x0, x0, :lo12:(\_name + \_size) |
Andrew Thoelke | 65668f9 | 2014-03-20 10:48:23 +0000 | [diff] [blame] | 123 | .endm |
Soby Mathew | 066f713 | 2014-07-14 16:57:23 +0100 | [diff] [blame] | 124 | |
| 125 | /* |
| 126 | * Helper macro to generate the best mov/movk combinations according |
| 127 | * the value to be moved. The 16 bits from '_shift' are tested and |
| 128 | * if not zero, they are moved into '_reg' without affecting |
| 129 | * other bits. |
| 130 | */ |
| 131 | .macro _mov_imm16 _reg, _val, _shift |
| 132 | .if (\_val >> \_shift) & 0xffff |
| 133 | .if (\_val & (1 << \_shift - 1)) |
| 134 | movk \_reg, (\_val >> \_shift) & 0xffff, LSL \_shift |
| 135 | .else |
| 136 | mov \_reg, \_val & (0xffff << \_shift) |
| 137 | .endif |
| 138 | .endif |
| 139 | .endm |
| 140 | |
| 141 | /* |
| 142 | * Helper macro to load arbitrary values into 32 or 64-bit registers |
| 143 | * which generates the best mov/movk combinations. Many base addresses |
| 144 | * are 64KB aligned the macro will eliminate updating bits 15:0 in |
| 145 | * that case |
| 146 | */ |
| 147 | .macro mov_imm _reg, _val |
| 148 | .if (\_val) == 0 |
| 149 | mov \_reg, #0 |
| 150 | .else |
| 151 | _mov_imm16 \_reg, (\_val), 0 |
| 152 | _mov_imm16 \_reg, (\_val), 16 |
| 153 | _mov_imm16 \_reg, (\_val), 32 |
| 154 | _mov_imm16 \_reg, (\_val), 48 |
| 155 | .endif |
| 156 | .endm |
Dan Handley | ea59668 | 2015-04-01 17:34:24 +0100 | [diff] [blame] | 157 | |
Jeenu Viswambharan | 68aef10 | 2016-11-30 15:21:11 +0000 | [diff] [blame] | 158 | /* |
| 159 | * Macro to mark instances where we're jumping to a function and don't |
| 160 | * expect a return. To provide the function being jumped to with |
| 161 | * additional information, we use 'bl' instruction to jump rather than |
| 162 | * 'b'. |
| 163 | * |
| 164 | * Debuggers infer the location of a call from where LR points to, which |
| 165 | * is usually the instruction after 'bl'. If this macro expansion |
| 166 | * happens to be the last location in a function, that'll cause the LR |
| 167 | * to point a location beyond the function, thereby misleading debugger |
| 168 | * back trace. We therefore insert a 'nop' after the function call for |
| 169 | * debug builds, unless 'skip_nop' parameter is non-zero. |
| 170 | */ |
| 171 | .macro no_ret _func:req, skip_nop=0 |
| 172 | bl \_func |
| 173 | #if DEBUG |
| 174 | .ifeq \skip_nop |
| 175 | nop |
| 176 | .endif |
| 177 | #endif |
| 178 | .endm |
| 179 | |
Jeenu Viswambharan | 54ec86a | 2017-01-19 14:23:36 +0000 | [diff] [blame] | 180 | /* |
| 181 | * Reserve space for a spin lock in assembly file. |
| 182 | */ |
| 183 | .macro define_asm_spinlock _name:req |
| 184 | .align SPINLOCK_ASM_ALIGN |
| 185 | \_name: |
| 186 | .space SPINLOCK_ASM_SIZE |
| 187 | .endm |
| 188 | |
Manish Pandey | 3531ac7 | 2023-04-27 10:02:35 +0100 | [diff] [blame] | 189 | /* |
| 190 | * With RAS extension executes esb instruction, else NOP |
| 191 | */ |
Jeenu Viswambharan | 9a7ce2f | 2018-04-04 16:07:11 +0100 | [diff] [blame] | 192 | .macro esb |
| 193 | .inst 0xd503221f |
| 194 | .endm |
Alexei Fedorov | 90f2e88 | 2019-05-24 12:17:09 +0100 | [diff] [blame] | 195 | |
| 196 | /* |
| 197 | * Helper macro to read system register value into x0 |
| 198 | */ |
| 199 | .macro read reg:req |
| 200 | #if ENABLE_BTI |
| 201 | bti j |
| 202 | #endif |
| 203 | mrs x0, \reg |
| 204 | ret |
| 205 | .endm |
| 206 | |
| 207 | /* |
| 208 | * Helper macro to write value from x1 to system register |
| 209 | */ |
| 210 | .macro write reg:req |
| 211 | #if ENABLE_BTI |
| 212 | bti j |
Jeenu Viswambharan | 9a7ce2f | 2018-04-04 16:07:11 +0100 | [diff] [blame] | 213 | #endif |
Alexei Fedorov | 90f2e88 | 2019-05-24 12:17:09 +0100 | [diff] [blame] | 214 | msr \reg, x1 |
| 215 | ret |
| 216 | .endm |
Jeenu Viswambharan | 9a7ce2f | 2018-04-04 16:07:11 +0100 | [diff] [blame] | 217 | |
Anthony Steinhauser | 0f7e601 | 2020-01-07 15:44:06 -0800 | [diff] [blame] | 218 | /* |
Andre Przywara | c735f1c | 2022-11-25 14:10:13 +0000 | [diff] [blame] | 219 | * The "sb" instruction was introduced later into the architecture, |
| 220 | * so not all toolchains understand it. Some deny its usage unless |
| 221 | * a supported processor is specified on the build command line. |
| 222 | * Use sb's system register encoding to work around this, we already |
| 223 | * guard the sb execution with a feature flag. |
| 224 | */ |
| 225 | |
| 226 | .macro sb_barrier_insn |
| 227 | msr SYSREG_SB, xzr |
| 228 | .endm |
| 229 | |
| 230 | /* |
Bipin Ravi | 9ee6dd4 | 2022-10-13 17:25:51 -0500 | [diff] [blame] | 231 | * Macro for using speculation barrier instruction introduced by |
| 232 | * FEAT_SB, if it's enabled. |
| 233 | */ |
| 234 | .macro speculation_barrier |
| 235 | #if ENABLE_FEAT_SB |
Andre Przywara | c735f1c | 2022-11-25 14:10:13 +0000 | [diff] [blame] | 236 | sb_barrier_insn |
Bipin Ravi | 9ee6dd4 | 2022-10-13 17:25:51 -0500 | [diff] [blame] | 237 | #else |
| 238 | dsb sy |
| 239 | isb |
| 240 | #endif |
| 241 | .endm |
| 242 | |
| 243 | /* |
Chris Kay | 08fec33 | 2021-03-09 13:34:35 +0000 | [diff] [blame] | 244 | * Macro for mitigating against speculative execution beyond ERET. Uses the |
| 245 | * speculation barrier instruction introduced by FEAT_SB, if it's enabled. |
Anthony Steinhauser | 0f7e601 | 2020-01-07 15:44:06 -0800 | [diff] [blame] | 246 | */ |
| 247 | .macro exception_return |
| 248 | eret |
Chris Kay | 08fec33 | 2021-03-09 13:34:35 +0000 | [diff] [blame] | 249 | #if ENABLE_FEAT_SB |
Andre Przywara | c735f1c | 2022-11-25 14:10:13 +0000 | [diff] [blame] | 250 | sb_barrier_insn |
Madhukar Pappireddy | bfe7bb6 | 2020-03-10 18:04:59 -0500 | [diff] [blame] | 251 | #else |
| 252 | dsb nsh |
Anthony Steinhauser | 0f7e601 | 2020-01-07 15:44:06 -0800 | [diff] [blame] | 253 | isb |
Madhukar Pappireddy | bfe7bb6 | 2020-03-10 18:04:59 -0500 | [diff] [blame] | 254 | #endif |
Anthony Steinhauser | 0f7e601 | 2020-01-07 15:44:06 -0800 | [diff] [blame] | 255 | .endm |
| 256 | |
Antonio Nino Diaz | 5eb8837 | 2018-11-08 10:20:19 +0000 | [diff] [blame] | 257 | #endif /* ASM_MACROS_S */ |