Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 1 | /* |
Dan Handley | ea59668 | 2015-04-01 17:34:24 +0100 | [diff] [blame] | 2 | * Copyright (c) 2013-2015, ARM Limited and Contributors. All rights reserved. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are met: |
| 6 | * |
| 7 | * Redistributions of source code must retain the above copyright notice, this |
| 8 | * list of conditions and the following disclaimer. |
| 9 | * |
| 10 | * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * Neither the name of ARM nor the names of its contributors may be used |
| 15 | * to endorse or promote products derived from this software without specific |
| 16 | * prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | * POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
Dan Handley | ea59668 | 2015-04-01 17:34:24 +0100 | [diff] [blame] | 30 | #ifndef __ASM_MACROS_S__ |
| 31 | #define __ASM_MACROS_S__ |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 32 | |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 33 | #include <arch.h> |
| 34 | |
| 35 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 36 | .macro func_prologue |
| 37 | stp x29, x30, [sp, #-0x10]! |
| 38 | mov x29,sp |
| 39 | .endm |
| 40 | |
| 41 | .macro func_epilogue |
| 42 | ldp x29, x30, [sp], #0x10 |
| 43 | .endm |
| 44 | |
| 45 | |
| 46 | .macro dcache_line_size reg, tmp |
Achin Gupta | 07f4e07 | 2014-02-02 12:02:23 +0000 | [diff] [blame] | 47 | mrs \tmp, ctr_el0 |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 48 | ubfx \tmp, \tmp, #16, #4 |
Achin Gupta | 07f4e07 | 2014-02-02 12:02:23 +0000 | [diff] [blame] | 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 | |
| 54 | .macro icache_line_size reg, tmp |
Achin Gupta | 07f4e07 | 2014-02-02 12:02:23 +0000 | [diff] [blame] | 55 | mrs \tmp, ctr_el0 |
| 56 | and \tmp, \tmp, #0xf |
| 57 | mov \reg, #4 |
| 58 | lsl \reg, \reg, \tmp |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 59 | .endm |
| 60 | |
| 61 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 62 | .macro smc_check label |
Andrew Thoelke | f977ed8 | 2014-04-28 12:32:02 +0100 | [diff] [blame] | 63 | mrs x0, esr_el3 |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 64 | ubfx x0, x0, #ESR_EC_SHIFT, #ESR_EC_LENGTH |
| 65 | cmp x0, #EC_AARCH64_SMC |
| 66 | b.ne $label |
| 67 | .endm |
| 68 | |
Sandrine Bailleux | 9e6ad6c | 2016-05-24 16:56:03 +0100 | [diff] [blame] | 69 | /* |
| 70 | * Declare the exception vector table, enforcing it is aligned on a |
| 71 | * 2KB boundary, as required by the ARMv8 architecture. |
Sandrine Bailleux | 618ba99 | 2016-05-24 16:22:59 +0100 | [diff] [blame] | 72 | * Use zero bytes as the fill value to be stored in the padding bytes |
| 73 | * so that it inserts illegal AArch64 instructions. This increases |
| 74 | * security, robustness and potentially facilitates debugging. |
Sandrine Bailleux | 9e6ad6c | 2016-05-24 16:56:03 +0100 | [diff] [blame] | 75 | */ |
| 76 | .macro vector_base label |
| 77 | .section .vectors, "ax" |
Sandrine Bailleux | 618ba99 | 2016-05-24 16:22:59 +0100 | [diff] [blame] | 78 | .align 11, 0 |
Sandrine Bailleux | 9e6ad6c | 2016-05-24 16:56:03 +0100 | [diff] [blame] | 79 | \label: |
| 80 | .endm |
| 81 | |
| 82 | /* |
| 83 | * Create an entry in the exception vector table, enforcing it is |
| 84 | * aligned on a 128-byte boundary, as required by the ARMv8 architecture. |
Sandrine Bailleux | 618ba99 | 2016-05-24 16:22:59 +0100 | [diff] [blame] | 85 | * Use zero bytes as the fill value to be stored in the padding bytes |
| 86 | * so that it inserts illegal AArch64 instructions. This increases |
| 87 | * security, robustness and potentially facilitates debugging. |
Sandrine Bailleux | 9e6ad6c | 2016-05-24 16:56:03 +0100 | [diff] [blame] | 88 | */ |
| 89 | .macro vector_entry label |
| 90 | .section .vectors, "ax" |
Sandrine Bailleux | 618ba99 | 2016-05-24 16:22:59 +0100 | [diff] [blame] | 91 | .align 7, 0 |
Sandrine Bailleux | 9e6ad6c | 2016-05-24 16:56:03 +0100 | [diff] [blame] | 92 | \label: |
| 93 | .endm |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 94 | |
Jeenu Viswambharan | a7934d6 | 2014-02-07 15:53:18 +0000 | [diff] [blame] | 95 | /* |
Sandrine Bailleux | 9e6ad6c | 2016-05-24 16:56:03 +0100 | [diff] [blame] | 96 | * This macro verifies that the given vector doesn't exceed the |
Jeenu Viswambharan | a7934d6 | 2014-02-07 15:53:18 +0000 | [diff] [blame] | 97 | * architectural limit of 32 instructions. This is meant to be placed |
Sandrine Bailleux | 9e6ad6c | 2016-05-24 16:56:03 +0100 | [diff] [blame] | 98 | * immediately after the last instruction in the vector. It takes the |
Jeenu Viswambharan | a7934d6 | 2014-02-07 15:53:18 +0000 | [diff] [blame] | 99 | * vector entry as the parameter |
| 100 | */ |
| 101 | .macro check_vector_size since |
| 102 | .if (. - \since) > (32 * 4) |
| 103 | .error "Vector exceeds 32 instructions" |
| 104 | .endif |
| 105 | .endm |
Andrew Thoelke | 38bde41 | 2014-03-18 13:46:55 +0000 | [diff] [blame] | 106 | |
| 107 | /* |
| 108 | * This macro is used to create a function label and place the |
| 109 | * code into a separate text section based on the function name |
| 110 | * to enable elimination of unused code during linking |
| 111 | */ |
| 112 | .macro func _name |
| 113 | .section .text.\_name, "ax" |
| 114 | .type \_name, %function |
Kévin Petit | a877c25 | 2015-03-24 14:03:57 +0000 | [diff] [blame] | 115 | .func \_name |
Andrew Thoelke | 38bde41 | 2014-03-18 13:46:55 +0000 | [diff] [blame] | 116 | \_name: |
| 117 | .endm |
Andrew Thoelke | 65668f9 | 2014-03-20 10:48:23 +0000 | [diff] [blame] | 118 | |
Kévin Petit | a877c25 | 2015-03-24 14:03:57 +0000 | [diff] [blame] | 119 | /* |
| 120 | * This macro is used to mark the end of a function. |
| 121 | */ |
| 122 | .macro endfunc _name |
| 123 | .endfunc |
| 124 | .size \_name, . - \_name |
| 125 | .endm |
| 126 | |
Andrew Thoelke | 65668f9 | 2014-03-20 10:48:23 +0000 | [diff] [blame] | 127 | /* |
Soby Mathew | 70716d6 | 2015-07-13 16:26:11 +0100 | [diff] [blame] | 128 | * Theses macros are used to create function labels for deprecated |
Soby Mathew | 18a6204 | 2015-10-26 14:29:21 +0000 | [diff] [blame] | 129 | * APIs. If ERROR_DEPRECATED is non zero, the callers of these APIs |
Soby Mathew | 70716d6 | 2015-07-13 16:26:11 +0100 | [diff] [blame] | 130 | * will fail to link and cause build failure. |
| 131 | */ |
Soby Mathew | 18a6204 | 2015-10-26 14:29:21 +0000 | [diff] [blame] | 132 | #if ERROR_DEPRECATED |
Soby Mathew | 70716d6 | 2015-07-13 16:26:11 +0100 | [diff] [blame] | 133 | .macro func_deprecated _name |
| 134 | func deprecated\_name |
| 135 | .endm |
| 136 | |
| 137 | .macro endfunc_deprecated _name |
| 138 | endfunc deprecated\_name |
| 139 | .endm |
| 140 | #else |
| 141 | .macro func_deprecated _name |
| 142 | func \_name |
| 143 | .endm |
| 144 | |
| 145 | .macro endfunc_deprecated _name |
| 146 | endfunc \_name |
| 147 | .endm |
| 148 | #endif |
| 149 | |
| 150 | /* |
Soby Mathew | bfdbecf | 2016-06-09 17:16:35 +0100 | [diff] [blame] | 151 | * Helper assembler macro to count trailing zeros. The output is |
| 152 | * populated in the `TZ_COUNT` symbol. |
| 153 | */ |
| 154 | .macro count_tz _value, _tz_count |
| 155 | .if \_value |
| 156 | count_tz "(\_value >> 1)", "(\_tz_count + 1)" |
| 157 | .else |
| 158 | .equ TZ_COUNT, (\_tz_count - 1) |
| 159 | .endif |
| 160 | .endm |
| 161 | |
| 162 | /* |
Andrew Thoelke | 65668f9 | 2014-03-20 10:48:23 +0000 | [diff] [blame] | 163 | * This macro declares an array of 1 or more stacks, properly |
| 164 | * aligned and in the requested section |
| 165 | */ |
Soby Mathew | bfdbecf | 2016-06-09 17:16:35 +0100 | [diff] [blame] | 166 | #define DEFAULT_STACK_ALIGN (1 << 6) /* In case the caller doesnt provide alignment */ |
Andrew Thoelke | 65668f9 | 2014-03-20 10:48:23 +0000 | [diff] [blame] | 167 | |
Soby Mathew | bfdbecf | 2016-06-09 17:16:35 +0100 | [diff] [blame] | 168 | .macro declare_stack _name, _section, _size, _count, _align=DEFAULT_STACK_ALIGN |
| 169 | count_tz \_align, 0 |
| 170 | .if (\_align - (1 << TZ_COUNT)) |
| 171 | .error "Incorrect stack alignment specified (Must be a power of 2)." |
| 172 | .endif |
| 173 | .if ((\_size & ((1 << TZ_COUNT) - 1)) <> 0) |
Andrew Thoelke | 65668f9 | 2014-03-20 10:48:23 +0000 | [diff] [blame] | 174 | .error "Stack size not correctly aligned" |
| 175 | .endif |
| 176 | .section \_section, "aw", %nobits |
Soby Mathew | bfdbecf | 2016-06-09 17:16:35 +0100 | [diff] [blame] | 177 | .align TZ_COUNT |
Andrew Thoelke | 65668f9 | 2014-03-20 10:48:23 +0000 | [diff] [blame] | 178 | \_name: |
| 179 | .space ((\_count) * (\_size)), 0 |
| 180 | .endm |
| 181 | |
Soby Mathew | 981487a | 2015-07-13 14:10:57 +0100 | [diff] [blame] | 182 | #if ENABLE_PLAT_COMPAT |
Andrew Thoelke | 65668f9 | 2014-03-20 10:48:23 +0000 | [diff] [blame] | 183 | /* |
| 184 | * This macro calculates the base address of an MP stack using the |
| 185 | * platform_get_core_pos() index, the name of the stack storage and |
| 186 | * the size of each stack |
| 187 | * In: X0 = MPIDR of CPU whose stack is wanted |
| 188 | * Out: X0 = physical address of stack base |
| 189 | * Clobber: X30, X1, X2 |
| 190 | */ |
| 191 | .macro get_mp_stack _name, _size |
| 192 | bl platform_get_core_pos |
| 193 | ldr x2, =(\_name + \_size) |
| 194 | mov x1, #\_size |
| 195 | madd x0, x0, x1, x2 |
| 196 | .endm |
Soby Mathew | 981487a | 2015-07-13 14:10:57 +0100 | [diff] [blame] | 197 | #endif |
Andrew Thoelke | 65668f9 | 2014-03-20 10:48:23 +0000 | [diff] [blame] | 198 | |
| 199 | /* |
Soby Mathew | b0082d2 | 2015-04-09 13:40:55 +0100 | [diff] [blame] | 200 | * This macro calculates the base address of the current CPU's MP stack |
| 201 | * using the plat_my_core_pos() index, the name of the stack storage |
| 202 | * and the size of each stack |
| 203 | * Out: X0 = physical address of stack base |
| 204 | * Clobber: X30, X1, X2 |
| 205 | */ |
| 206 | .macro get_my_mp_stack _name, _size |
| 207 | bl plat_my_core_pos |
| 208 | ldr x2, =(\_name + \_size) |
| 209 | mov x1, #\_size |
| 210 | madd x0, x0, x1, x2 |
| 211 | .endm |
| 212 | |
| 213 | /* |
Andrew Thoelke | 65668f9 | 2014-03-20 10:48:23 +0000 | [diff] [blame] | 214 | * This macro calculates the base address of a UP stack using the |
| 215 | * name of the stack storage and the size of the stack |
| 216 | * Out: X0 = physical address of stack base |
| 217 | */ |
| 218 | .macro get_up_stack _name, _size |
| 219 | ldr x0, =(\_name + \_size) |
| 220 | .endm |
Soby Mathew | 066f713 | 2014-07-14 16:57:23 +0100 | [diff] [blame] | 221 | |
| 222 | /* |
| 223 | * Helper macro to generate the best mov/movk combinations according |
| 224 | * the value to be moved. The 16 bits from '_shift' are tested and |
| 225 | * if not zero, they are moved into '_reg' without affecting |
| 226 | * other bits. |
| 227 | */ |
| 228 | .macro _mov_imm16 _reg, _val, _shift |
| 229 | .if (\_val >> \_shift) & 0xffff |
| 230 | .if (\_val & (1 << \_shift - 1)) |
| 231 | movk \_reg, (\_val >> \_shift) & 0xffff, LSL \_shift |
| 232 | .else |
| 233 | mov \_reg, \_val & (0xffff << \_shift) |
| 234 | .endif |
| 235 | .endif |
| 236 | .endm |
| 237 | |
| 238 | /* |
| 239 | * Helper macro to load arbitrary values into 32 or 64-bit registers |
| 240 | * which generates the best mov/movk combinations. Many base addresses |
| 241 | * are 64KB aligned the macro will eliminate updating bits 15:0 in |
| 242 | * that case |
| 243 | */ |
| 244 | .macro mov_imm _reg, _val |
| 245 | .if (\_val) == 0 |
| 246 | mov \_reg, #0 |
| 247 | .else |
| 248 | _mov_imm16 \_reg, (\_val), 0 |
| 249 | _mov_imm16 \_reg, (\_val), 16 |
| 250 | _mov_imm16 \_reg, (\_val), 32 |
| 251 | _mov_imm16 \_reg, (\_val), 48 |
| 252 | .endif |
| 253 | .endm |
Dan Handley | ea59668 | 2015-04-01 17:34:24 +0100 | [diff] [blame] | 254 | |
| 255 | #endif /* __ASM_MACROS_S__ */ |