Catalin Marinas | 0be7320 | 2012-03-05 11:49:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Based on arch/arm/include/asm/assembler.h |
| 3 | * |
| 4 | * Copyright (C) 1996-2000 Russell King |
| 5 | * Copyright (C) 2012 ARM Ltd. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | #ifndef __ASSEMBLY__ |
| 20 | #error "Only include this from assembly code" |
| 21 | #endif |
| 22 | |
Marc Zyngier | f3e3927 | 2015-02-20 13:53:13 +0000 | [diff] [blame] | 23 | #ifndef __ASM_ASSEMBLER_H |
| 24 | #define __ASM_ASSEMBLER_H |
| 25 | |
Catalin Marinas | 0be7320 | 2012-03-05 11:49:26 +0000 | [diff] [blame] | 26 | #include <asm/ptrace.h> |
Will Deacon | 2a28307 | 2014-04-29 19:04:06 +0100 | [diff] [blame] | 27 | #include <asm/thread_info.h> |
Catalin Marinas | 0be7320 | 2012-03-05 11:49:26 +0000 | [diff] [blame] | 28 | |
| 29 | /* |
Catalin Marinas | 0be7320 | 2012-03-05 11:49:26 +0000 | [diff] [blame] | 30 | * Enable and disable interrupts. |
| 31 | */ |
| 32 | .macro disable_irq |
| 33 | msr daifset, #2 |
| 34 | .endm |
| 35 | |
| 36 | .macro enable_irq |
| 37 | msr daifclr, #2 |
| 38 | .endm |
| 39 | |
| 40 | /* |
Catalin Marinas | 0be7320 | 2012-03-05 11:49:26 +0000 | [diff] [blame] | 41 | * Enable and disable debug exceptions. |
| 42 | */ |
| 43 | .macro disable_dbg |
| 44 | msr daifset, #8 |
| 45 | .endm |
| 46 | |
| 47 | .macro enable_dbg |
| 48 | msr daifclr, #8 |
| 49 | .endm |
| 50 | |
Will Deacon | 2a28307 | 2014-04-29 19:04:06 +0100 | [diff] [blame] | 51 | .macro disable_step_tsk, flgs, tmp |
| 52 | tbz \flgs, #TIF_SINGLESTEP, 9990f |
Catalin Marinas | 0be7320 | 2012-03-05 11:49:26 +0000 | [diff] [blame] | 53 | mrs \tmp, mdscr_el1 |
| 54 | bic \tmp, \tmp, #1 |
| 55 | msr mdscr_el1, \tmp |
Will Deacon | 2a28307 | 2014-04-29 19:04:06 +0100 | [diff] [blame] | 56 | isb // Synchronise with enable_dbg |
| 57 | 9990: |
Catalin Marinas | 0be7320 | 2012-03-05 11:49:26 +0000 | [diff] [blame] | 58 | .endm |
| 59 | |
Will Deacon | 2a28307 | 2014-04-29 19:04:06 +0100 | [diff] [blame] | 60 | .macro enable_step_tsk, flgs, tmp |
| 61 | tbz \flgs, #TIF_SINGLESTEP, 9990f |
| 62 | disable_dbg |
Catalin Marinas | 0be7320 | 2012-03-05 11:49:26 +0000 | [diff] [blame] | 63 | mrs \tmp, mdscr_el1 |
| 64 | orr \tmp, \tmp, #1 |
| 65 | msr mdscr_el1, \tmp |
Will Deacon | 2a28307 | 2014-04-29 19:04:06 +0100 | [diff] [blame] | 66 | 9990: |
Catalin Marinas | 0be7320 | 2012-03-05 11:49:26 +0000 | [diff] [blame] | 67 | .endm |
| 68 | |
Will Deacon | 2a28307 | 2014-04-29 19:04:06 +0100 | [diff] [blame] | 69 | /* |
| 70 | * Enable both debug exceptions and interrupts. This is likely to be |
| 71 | * faster than two daifclr operations, since writes to this register |
| 72 | * are self-synchronising. |
| 73 | */ |
| 74 | .macro enable_dbg_and_irq |
| 75 | msr daifclr, #(8 | 2) |
Catalin Marinas | 0be7320 | 2012-03-05 11:49:26 +0000 | [diff] [blame] | 76 | .endm |
| 77 | |
| 78 | /* |
| 79 | * SMP data memory barrier |
| 80 | */ |
| 81 | .macro smp_dmb, opt |
Catalin Marinas | 0be7320 | 2012-03-05 11:49:26 +0000 | [diff] [blame] | 82 | dmb \opt |
Catalin Marinas | 0be7320 | 2012-03-05 11:49:26 +0000 | [diff] [blame] | 83 | .endm |
| 84 | |
Ard Biesheuvel | 6c94f27 | 2016-01-01 15:02:12 +0100 | [diff] [blame] | 85 | /* |
| 86 | * Emit an entry into the exception table |
| 87 | */ |
| 88 | .macro _asm_extable, from, to |
| 89 | .pushsection __ex_table, "a" |
| 90 | .align 3 |
| 91 | .long (\from - .), (\to - .) |
| 92 | .popsection |
| 93 | .endm |
| 94 | |
Catalin Marinas | 0be7320 | 2012-03-05 11:49:26 +0000 | [diff] [blame] | 95 | #define USER(l, x...) \ |
| 96 | 9999: x; \ |
Ard Biesheuvel | 6c94f27 | 2016-01-01 15:02:12 +0100 | [diff] [blame] | 97 | _asm_extable 9999b, l |
Catalin Marinas | 0be7320 | 2012-03-05 11:49:26 +0000 | [diff] [blame] | 98 | |
| 99 | /* |
| 100 | * Register aliases. |
| 101 | */ |
| 102 | lr .req x30 // link register |
Marc Zyngier | dc637f1 | 2012-10-19 17:37:35 +0100 | [diff] [blame] | 103 | |
| 104 | /* |
| 105 | * Vector entry |
| 106 | */ |
| 107 | .macro ventry label |
| 108 | .align 7 |
| 109 | b \label |
| 110 | .endm |
Matthew Leach | e68beda | 2013-10-11 14:52:15 +0100 | [diff] [blame] | 111 | |
| 112 | /* |
| 113 | * Select code when configured for BE. |
| 114 | */ |
| 115 | #ifdef CONFIG_CPU_BIG_ENDIAN |
| 116 | #define CPU_BE(code...) code |
| 117 | #else |
| 118 | #define CPU_BE(code...) |
| 119 | #endif |
| 120 | |
| 121 | /* |
| 122 | * Select code when configured for LE. |
| 123 | */ |
| 124 | #ifdef CONFIG_CPU_BIG_ENDIAN |
| 125 | #define CPU_LE(code...) |
| 126 | #else |
| 127 | #define CPU_LE(code...) code |
| 128 | #endif |
| 129 | |
Matthew Leach | 55b8954 | 2013-10-11 14:52:13 +0100 | [diff] [blame] | 130 | /* |
| 131 | * Define a macro that constructs a 64-bit value by concatenating two |
| 132 | * 32-bit registers. Note that on big endian systems the order of the |
| 133 | * registers is swapped. |
| 134 | */ |
| 135 | #ifndef CONFIG_CPU_BIG_ENDIAN |
| 136 | .macro regs_to_64, rd, lbits, hbits |
| 137 | #else |
| 138 | .macro regs_to_64, rd, hbits, lbits |
| 139 | #endif |
| 140 | orr \rd, \lbits, \hbits, lsl #32 |
| 141 | .endm |
Marc Zyngier | f3e3927 | 2015-02-20 13:53:13 +0000 | [diff] [blame] | 142 | |
Ard Biesheuvel | b784a5d | 2015-03-04 19:45:38 +0100 | [diff] [blame] | 143 | /* |
| 144 | * Pseudo-ops for PC-relative adr/ldr/str <reg>, <symbol> where |
| 145 | * <symbol> is within the range +/- 4 GB of the PC. |
| 146 | */ |
| 147 | /* |
| 148 | * @dst: destination register (64 bit wide) |
| 149 | * @sym: name of the symbol |
| 150 | * @tmp: optional scratch register to be used if <dst> == sp, which |
| 151 | * is not allowed in an adrp instruction |
| 152 | */ |
| 153 | .macro adr_l, dst, sym, tmp= |
| 154 | .ifb \tmp |
| 155 | adrp \dst, \sym |
| 156 | add \dst, \dst, :lo12:\sym |
| 157 | .else |
| 158 | adrp \tmp, \sym |
| 159 | add \dst, \tmp, :lo12:\sym |
| 160 | .endif |
| 161 | .endm |
| 162 | |
| 163 | /* |
| 164 | * @dst: destination register (32 or 64 bit wide) |
| 165 | * @sym: name of the symbol |
| 166 | * @tmp: optional 64-bit scratch register to be used if <dst> is a |
| 167 | * 32-bit wide register, in which case it cannot be used to hold |
| 168 | * the address |
| 169 | */ |
| 170 | .macro ldr_l, dst, sym, tmp= |
| 171 | .ifb \tmp |
| 172 | adrp \dst, \sym |
| 173 | ldr \dst, [\dst, :lo12:\sym] |
| 174 | .else |
| 175 | adrp \tmp, \sym |
| 176 | ldr \dst, [\tmp, :lo12:\sym] |
| 177 | .endif |
| 178 | .endm |
| 179 | |
| 180 | /* |
| 181 | * @src: source register (32 or 64 bit wide) |
| 182 | * @sym: name of the symbol |
| 183 | * @tmp: mandatory 64-bit scratch register to calculate the address |
| 184 | * while <src> needs to be preserved. |
| 185 | */ |
| 186 | .macro str_l, src, sym, tmp |
| 187 | adrp \tmp, \sym |
| 188 | str \src, [\tmp, :lo12:\sym] |
| 189 | .endm |
| 190 | |
James Morse | aa4d5d3 | 2015-12-10 10:22:39 +0000 | [diff] [blame] | 191 | /* |
| 192 | * @sym: The name of the per-cpu variable |
| 193 | * @reg: Result of per_cpu(sym, smp_processor_id()) |
| 194 | * @tmp: scratch register |
| 195 | */ |
| 196 | .macro this_cpu_ptr, sym, reg, tmp |
| 197 | adr_l \reg, \sym |
| 198 | mrs \tmp, tpidr_el1 |
| 199 | add \reg, \reg, \tmp |
| 200 | .endm |
| 201 | |
Ard Biesheuvel | 2079184 | 2015-10-08 20:02:03 +0100 | [diff] [blame] | 202 | /* |
| 203 | * Annotate a function as position independent, i.e., safe to be called before |
| 204 | * the kernel virtual mapping is activated. |
| 205 | */ |
| 206 | #define ENDPIPROC(x) \ |
| 207 | .globl __pi_##x; \ |
| 208 | .type __pi_##x, %function; \ |
| 209 | .set __pi_##x, x; \ |
| 210 | .size __pi_##x, . - x; \ |
| 211 | ENDPROC(x) |
| 212 | |
Ard Biesheuvel | 6ad1fe5 | 2015-12-26 13:48:02 +0100 | [diff] [blame] | 213 | /* |
| 214 | * Emit a 64-bit absolute little endian symbol reference in a way that |
| 215 | * ensures that it will be resolved at build time, even when building a |
| 216 | * PIE binary. This requires cooperation from the linker script, which |
| 217 | * must emit the lo32/hi32 halves individually. |
| 218 | */ |
| 219 | .macro le64sym, sym |
| 220 | .long \sym\()_lo32 |
| 221 | .long \sym\()_hi32 |
| 222 | .endm |
| 223 | |
Ard Biesheuvel | 30b5ba5 | 2016-04-18 17:09:44 +0200 | [diff] [blame^] | 224 | /* |
| 225 | * mov_q - move an immediate constant into a 64-bit register using |
| 226 | * between 2 and 4 movz/movk instructions (depending on the |
| 227 | * magnitude and sign of the operand) |
| 228 | */ |
| 229 | .macro mov_q, reg, val |
| 230 | .if (((\val) >> 31) == 0 || ((\val) >> 31) == 0x1ffffffff) |
| 231 | movz \reg, :abs_g1_s:\val |
| 232 | .else |
| 233 | .if (((\val) >> 47) == 0 || ((\val) >> 47) == 0x1ffff) |
| 234 | movz \reg, :abs_g2_s:\val |
| 235 | .else |
| 236 | movz \reg, :abs_g3:\val |
| 237 | movk \reg, :abs_g2_nc:\val |
| 238 | .endif |
| 239 | movk \reg, :abs_g1_nc:\val |
| 240 | .endif |
| 241 | movk \reg, :abs_g0_nc:\val |
| 242 | .endm |
| 243 | |
Marc Zyngier | f3e3927 | 2015-02-20 13:53:13 +0000 | [diff] [blame] | 244 | #endif /* __ASM_ASSEMBLER_H */ |