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