Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 1 | /* |
Jeenu Viswambharan | 9a7ce2f | 2018-04-04 16:07:11 +0100 | [diff] [blame] | 2 | * Copyright (c) 2013-2018, 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 | */ |
Dan Handley | ea59668 | 2015-04-01 17:34:24 +0100 | [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> |
Soby Mathew | b9ff2fd | 2016-07-08 15:26:35 +0100 | [diff] [blame] | 10 | #include <asm_macros_common.S> |
Jeenu Viswambharan | 54ec86a | 2017-01-19 14:23:36 +0000 | [diff] [blame] | 11 | #include <spinlock.h> |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 12 | |
| 13 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 14 | .macro func_prologue |
| 15 | stp x29, x30, [sp, #-0x10]! |
| 16 | mov x29,sp |
| 17 | .endm |
| 18 | |
| 19 | .macro func_epilogue |
| 20 | ldp x29, x30, [sp], #0x10 |
| 21 | .endm |
| 22 | |
| 23 | |
| 24 | .macro dcache_line_size reg, tmp |
Achin Gupta | 07f4e07 | 2014-02-02 12:02:23 +0000 | [diff] [blame] | 25 | mrs \tmp, ctr_el0 |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 26 | ubfx \tmp, \tmp, #16, #4 |
Achin Gupta | 07f4e07 | 2014-02-02 12:02:23 +0000 | [diff] [blame] | 27 | mov \reg, #4 |
| 28 | lsl \reg, \reg, \tmp |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 29 | .endm |
| 30 | |
| 31 | |
| 32 | .macro icache_line_size reg, tmp |
Achin Gupta | 07f4e07 | 2014-02-02 12:02:23 +0000 | [diff] [blame] | 33 | mrs \tmp, ctr_el0 |
| 34 | and \tmp, \tmp, #0xf |
| 35 | mov \reg, #4 |
| 36 | lsl \reg, \reg, \tmp |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 37 | .endm |
| 38 | |
| 39 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 40 | .macro smc_check label |
Andrew Thoelke | f977ed8 | 2014-04-28 12:32:02 +0100 | [diff] [blame] | 41 | mrs x0, esr_el3 |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 42 | ubfx x0, x0, #ESR_EC_SHIFT, #ESR_EC_LENGTH |
| 43 | cmp x0, #EC_AARCH64_SMC |
| 44 | b.ne $label |
| 45 | .endm |
| 46 | |
Sandrine Bailleux | 9e6ad6c | 2016-05-24 16:56:03 +0100 | [diff] [blame] | 47 | /* |
| 48 | * Declare the exception vector table, enforcing it is aligned on a |
| 49 | * 2KB boundary, as required by the ARMv8 architecture. |
Sandrine Bailleux | 618ba99 | 2016-05-24 16:22:59 +0100 | [diff] [blame] | 50 | * Use zero bytes as the fill value to be stored in the padding bytes |
| 51 | * so that it inserts illegal AArch64 instructions. This increases |
| 52 | * security, robustness and potentially facilitates debugging. |
Sandrine Bailleux | 9e6ad6c | 2016-05-24 16:56:03 +0100 | [diff] [blame] | 53 | */ |
Antonio Nino Diaz | c41f206 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 54 | .macro vector_base label, section_name=.vectors |
| 55 | .section \section_name, "ax" |
Sandrine Bailleux | 618ba99 | 2016-05-24 16:22:59 +0100 | [diff] [blame] | 56 | .align 11, 0 |
Sandrine Bailleux | 9e6ad6c | 2016-05-24 16:56:03 +0100 | [diff] [blame] | 57 | \label: |
| 58 | .endm |
| 59 | |
| 60 | /* |
| 61 | * Create an entry in the exception vector table, enforcing it is |
| 62 | * aligned on a 128-byte boundary, as required by the ARMv8 architecture. |
Sandrine Bailleux | 618ba99 | 2016-05-24 16:22:59 +0100 | [diff] [blame] | 63 | * Use zero bytes as the fill value to be stored in the padding bytes |
| 64 | * so that it inserts illegal AArch64 instructions. This increases |
| 65 | * security, robustness and potentially facilitates debugging. |
Sandrine Bailleux | 9e6ad6c | 2016-05-24 16:56:03 +0100 | [diff] [blame] | 66 | */ |
Antonio Nino Diaz | c41f206 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 67 | .macro vector_entry label, section_name=.vectors |
Douglas Raillard | efa50b5 | 2017-08-07 16:20:46 +0100 | [diff] [blame] | 68 | .cfi_sections .debug_frame |
Antonio Nino Diaz | c41f206 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 69 | .section \section_name, "ax" |
Sandrine Bailleux | 618ba99 | 2016-05-24 16:22:59 +0100 | [diff] [blame] | 70 | .align 7, 0 |
Douglas Raillard | efa50b5 | 2017-08-07 16:20:46 +0100 | [diff] [blame] | 71 | .type \label, %function |
| 72 | .func \label |
| 73 | .cfi_startproc |
Sandrine Bailleux | 9e6ad6c | 2016-05-24 16:56:03 +0100 | [diff] [blame] | 74 | \label: |
| 75 | .endm |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 76 | |
Jeenu Viswambharan | a7934d6 | 2014-02-07 15:53:18 +0000 | [diff] [blame] | 77 | /* |
Sandrine Bailleux | 9e6ad6c | 2016-05-24 16:56:03 +0100 | [diff] [blame] | 78 | * This macro verifies that the given vector doesn't exceed the |
Jeenu Viswambharan | a7934d6 | 2014-02-07 15:53:18 +0000 | [diff] [blame] | 79 | * architectural limit of 32 instructions. This is meant to be placed |
Sandrine Bailleux | 9e6ad6c | 2016-05-24 16:56:03 +0100 | [diff] [blame] | 80 | * immediately after the last instruction in the vector. It takes the |
Jeenu Viswambharan | a7934d6 | 2014-02-07 15:53:18 +0000 | [diff] [blame] | 81 | * vector entry as the parameter |
| 82 | */ |
| 83 | .macro check_vector_size since |
Douglas Raillard | efa50b5 | 2017-08-07 16:20:46 +0100 | [diff] [blame] | 84 | .endfunc |
| 85 | .cfi_endproc |
Jeenu Viswambharan | a7934d6 | 2014-02-07 15:53:18 +0000 | [diff] [blame] | 86 | .if (. - \since) > (32 * 4) |
| 87 | .error "Vector exceeds 32 instructions" |
| 88 | .endif |
| 89 | .endm |
Andrew Thoelke | 38bde41 | 2014-03-18 13:46:55 +0000 | [diff] [blame] | 90 | |
Soby Mathew | 981487a | 2015-07-13 14:10:57 +0100 | [diff] [blame] | 91 | #if ENABLE_PLAT_COMPAT |
Andrew Thoelke | 65668f9 | 2014-03-20 10:48:23 +0000 | [diff] [blame] | 92 | /* |
| 93 | * This macro calculates the base address of an MP stack using the |
| 94 | * platform_get_core_pos() index, the name of the stack storage and |
| 95 | * the size of each stack |
| 96 | * In: X0 = MPIDR of CPU whose stack is wanted |
| 97 | * Out: X0 = physical address of stack base |
| 98 | * Clobber: X30, X1, X2 |
| 99 | */ |
| 100 | .macro get_mp_stack _name, _size |
| 101 | bl platform_get_core_pos |
| 102 | ldr x2, =(\_name + \_size) |
| 103 | mov x1, #\_size |
| 104 | madd x0, x0, x1, x2 |
| 105 | .endm |
Soby Mathew | 981487a | 2015-07-13 14:10:57 +0100 | [diff] [blame] | 106 | #endif |
Andrew Thoelke | 65668f9 | 2014-03-20 10:48:23 +0000 | [diff] [blame] | 107 | |
| 108 | /* |
Soby Mathew | b0082d2 | 2015-04-09 13:40:55 +0100 | [diff] [blame] | 109 | * This macro calculates the base address of the current CPU's MP stack |
| 110 | * using the plat_my_core_pos() index, the name of the stack storage |
| 111 | * and the size of each stack |
| 112 | * Out: X0 = physical address of stack base |
| 113 | * Clobber: X30, X1, X2 |
| 114 | */ |
| 115 | .macro get_my_mp_stack _name, _size |
| 116 | bl plat_my_core_pos |
| 117 | ldr x2, =(\_name + \_size) |
| 118 | mov x1, #\_size |
| 119 | madd x0, x0, x1, x2 |
| 120 | .endm |
| 121 | |
| 122 | /* |
Andrew Thoelke | 65668f9 | 2014-03-20 10:48:23 +0000 | [diff] [blame] | 123 | * This macro calculates the base address of a UP stack using the |
| 124 | * name of the stack storage and the size of the stack |
| 125 | * Out: X0 = physical address of stack base |
| 126 | */ |
| 127 | .macro get_up_stack _name, _size |
| 128 | ldr x0, =(\_name + \_size) |
| 129 | .endm |
Soby Mathew | 066f713 | 2014-07-14 16:57:23 +0100 | [diff] [blame] | 130 | |
| 131 | /* |
| 132 | * Helper macro to generate the best mov/movk combinations according |
| 133 | * the value to be moved. The 16 bits from '_shift' are tested and |
| 134 | * if not zero, they are moved into '_reg' without affecting |
| 135 | * other bits. |
| 136 | */ |
| 137 | .macro _mov_imm16 _reg, _val, _shift |
| 138 | .if (\_val >> \_shift) & 0xffff |
| 139 | .if (\_val & (1 << \_shift - 1)) |
| 140 | movk \_reg, (\_val >> \_shift) & 0xffff, LSL \_shift |
| 141 | .else |
| 142 | mov \_reg, \_val & (0xffff << \_shift) |
| 143 | .endif |
| 144 | .endif |
| 145 | .endm |
| 146 | |
| 147 | /* |
| 148 | * Helper macro to load arbitrary values into 32 or 64-bit registers |
| 149 | * which generates the best mov/movk combinations. Many base addresses |
| 150 | * are 64KB aligned the macro will eliminate updating bits 15:0 in |
| 151 | * that case |
| 152 | */ |
| 153 | .macro mov_imm _reg, _val |
| 154 | .if (\_val) == 0 |
| 155 | mov \_reg, #0 |
| 156 | .else |
| 157 | _mov_imm16 \_reg, (\_val), 0 |
| 158 | _mov_imm16 \_reg, (\_val), 16 |
| 159 | _mov_imm16 \_reg, (\_val), 32 |
| 160 | _mov_imm16 \_reg, (\_val), 48 |
| 161 | .endif |
| 162 | .endm |
Dan Handley | ea59668 | 2015-04-01 17:34:24 +0100 | [diff] [blame] | 163 | |
Jeenu Viswambharan | 68aef10 | 2016-11-30 15:21:11 +0000 | [diff] [blame] | 164 | /* |
| 165 | * Macro to mark instances where we're jumping to a function and don't |
| 166 | * expect a return. To provide the function being jumped to with |
| 167 | * additional information, we use 'bl' instruction to jump rather than |
| 168 | * 'b'. |
| 169 | * |
| 170 | * Debuggers infer the location of a call from where LR points to, which |
| 171 | * is usually the instruction after 'bl'. If this macro expansion |
| 172 | * happens to be the last location in a function, that'll cause the LR |
| 173 | * to point a location beyond the function, thereby misleading debugger |
| 174 | * back trace. We therefore insert a 'nop' after the function call for |
| 175 | * debug builds, unless 'skip_nop' parameter is non-zero. |
| 176 | */ |
| 177 | .macro no_ret _func:req, skip_nop=0 |
| 178 | bl \_func |
| 179 | #if DEBUG |
| 180 | .ifeq \skip_nop |
| 181 | nop |
| 182 | .endif |
| 183 | #endif |
| 184 | .endm |
| 185 | |
Jeenu Viswambharan | 54ec86a | 2017-01-19 14:23:36 +0000 | [diff] [blame] | 186 | /* |
| 187 | * Reserve space for a spin lock in assembly file. |
| 188 | */ |
| 189 | .macro define_asm_spinlock _name:req |
| 190 | .align SPINLOCK_ASM_ALIGN |
| 191 | \_name: |
| 192 | .space SPINLOCK_ASM_SIZE |
| 193 | .endm |
| 194 | |
Jeenu Viswambharan | 9a7ce2f | 2018-04-04 16:07:11 +0100 | [diff] [blame] | 195 | #if RAS_EXTENSION |
| 196 | .macro esb |
| 197 | .inst 0xd503221f |
| 198 | .endm |
| 199 | #endif |
| 200 | |
Dan Handley | ea59668 | 2015-04-01 17:34:24 +0100 | [diff] [blame] | 201 | #endif /* __ASM_MACROS_S__ */ |