blob: 128a9ca06c8adc12dbc1c137bf327880f709dec5 [file] [log] [blame]
Catalin Marinas0be73202012-03-05 11:49:26 +00001/*
Geoff Levand7b7293a2016-04-27 17:47:00 +01002 * Based on arch/arm/include/asm/assembler.h, arch/arm/mm/proc-macros.S
Catalin Marinas0be73202012-03-05 11:49:26 +00003 *
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 Zyngierf3e39272015-02-20 13:53:13 +000023#ifndef __ASM_ASSEMBLER_H
24#define __ASM_ASSEMBLER_H
25
Geoff Levand7b7293a2016-04-27 17:47:00 +010026#include <asm/asm-offsets.h>
Andre Przywara823066d2016-06-28 18:07:29 +010027#include <asm/cpufeature.h>
Geoff Levand5003dbd2016-04-27 17:47:10 +010028#include <asm/page.h>
Geoff Levand7b7293a2016-04-27 17:47:00 +010029#include <asm/pgtable-hwdef.h>
Catalin Marinas0be73202012-03-05 11:49:26 +000030#include <asm/ptrace.h>
Will Deacon2a283072014-04-29 19:04:06 +010031#include <asm/thread_info.h>
Catalin Marinas0be73202012-03-05 11:49:26 +000032
33/*
Catalin Marinas0be73202012-03-05 11:49:26 +000034 * Enable and disable interrupts.
35 */
36 .macro disable_irq
37 msr daifset, #2
38 .endm
39
40 .macro enable_irq
41 msr daifclr, #2
42 .endm
43
44/*
Catalin Marinas0be73202012-03-05 11:49:26 +000045 * Enable and disable debug exceptions.
46 */
47 .macro disable_dbg
48 msr daifset, #8
49 .endm
50
51 .macro enable_dbg
52 msr daifclr, #8
53 .endm
54
Will Deacon2a283072014-04-29 19:04:06 +010055 .macro disable_step_tsk, flgs, tmp
56 tbz \flgs, #TIF_SINGLESTEP, 9990f
Catalin Marinas0be73202012-03-05 11:49:26 +000057 mrs \tmp, mdscr_el1
58 bic \tmp, \tmp, #1
59 msr mdscr_el1, \tmp
Will Deacon2a283072014-04-29 19:04:06 +010060 isb // Synchronise with enable_dbg
619990:
Catalin Marinas0be73202012-03-05 11:49:26 +000062 .endm
63
Will Deacon2a283072014-04-29 19:04:06 +010064 .macro enable_step_tsk, flgs, tmp
65 tbz \flgs, #TIF_SINGLESTEP, 9990f
66 disable_dbg
Catalin Marinas0be73202012-03-05 11:49:26 +000067 mrs \tmp, mdscr_el1
68 orr \tmp, \tmp, #1
69 msr mdscr_el1, \tmp
Will Deacon2a283072014-04-29 19:04:06 +0100709990:
Catalin Marinas0be73202012-03-05 11:49:26 +000071 .endm
72
Will Deacon2a283072014-04-29 19:04:06 +010073/*
74 * Enable both debug exceptions and interrupts. This is likely to be
75 * faster than two daifclr operations, since writes to this register
76 * are self-synchronising.
77 */
78 .macro enable_dbg_and_irq
79 msr daifclr, #(8 | 2)
Catalin Marinas0be73202012-03-05 11:49:26 +000080 .endm
81
82/*
83 * SMP data memory barrier
84 */
85 .macro smp_dmb, opt
Catalin Marinas0be73202012-03-05 11:49:26 +000086 dmb \opt
Catalin Marinas0be73202012-03-05 11:49:26 +000087 .endm
88
Ard Biesheuvel6c94f272016-01-01 15:02:12 +010089/*
Will Deaconf99a2502016-09-06 16:40:23 +010090 * NOP sequence
91 */
92 .macro nops, num
93 .rept \num
94 nop
95 .endr
96 .endm
97
98/*
Ard Biesheuvel6c94f272016-01-01 15:02:12 +010099 * Emit an entry into the exception table
100 */
101 .macro _asm_extable, from, to
102 .pushsection __ex_table, "a"
103 .align 3
104 .long (\from - .), (\to - .)
105 .popsection
106 .endm
107
Catalin Marinas0be73202012-03-05 11:49:26 +0000108#define USER(l, x...) \
1099999: x; \
Ard Biesheuvel6c94f272016-01-01 15:02:12 +0100110 _asm_extable 9999b, l
Catalin Marinas0be73202012-03-05 11:49:26 +0000111
112/*
113 * Register aliases.
114 */
115lr .req x30 // link register
Marc Zyngierdc637f12012-10-19 17:37:35 +0100116
117/*
118 * Vector entry
119 */
120 .macro ventry label
121 .align 7
122 b \label
123 .endm
Matthew Leache68beda2013-10-11 14:52:15 +0100124
125/*
126 * Select code when configured for BE.
127 */
128#ifdef CONFIG_CPU_BIG_ENDIAN
129#define CPU_BE(code...) code
130#else
131#define CPU_BE(code...)
132#endif
133
134/*
135 * Select code when configured for LE.
136 */
137#ifdef CONFIG_CPU_BIG_ENDIAN
138#define CPU_LE(code...)
139#else
140#define CPU_LE(code...) code
141#endif
142
Matthew Leach55b89542013-10-11 14:52:13 +0100143/*
144 * Define a macro that constructs a 64-bit value by concatenating two
145 * 32-bit registers. Note that on big endian systems the order of the
146 * registers is swapped.
147 */
148#ifndef CONFIG_CPU_BIG_ENDIAN
149 .macro regs_to_64, rd, lbits, hbits
150#else
151 .macro regs_to_64, rd, hbits, lbits
152#endif
153 orr \rd, \lbits, \hbits, lsl #32
154 .endm
Marc Zyngierf3e39272015-02-20 13:53:13 +0000155
Ard Biesheuvelb784a5d2015-03-04 19:45:38 +0100156/*
157 * Pseudo-ops for PC-relative adr/ldr/str <reg>, <symbol> where
158 * <symbol> is within the range +/- 4 GB of the PC.
159 */
160 /*
161 * @dst: destination register (64 bit wide)
162 * @sym: name of the symbol
163 * @tmp: optional scratch register to be used if <dst> == sp, which
164 * is not allowed in an adrp instruction
165 */
166 .macro adr_l, dst, sym, tmp=
167 .ifb \tmp
168 adrp \dst, \sym
169 add \dst, \dst, :lo12:\sym
170 .else
171 adrp \tmp, \sym
172 add \dst, \tmp, :lo12:\sym
173 .endif
174 .endm
175
176 /*
177 * @dst: destination register (32 or 64 bit wide)
178 * @sym: name of the symbol
179 * @tmp: optional 64-bit scratch register to be used if <dst> is a
180 * 32-bit wide register, in which case it cannot be used to hold
181 * the address
182 */
183 .macro ldr_l, dst, sym, tmp=
184 .ifb \tmp
185 adrp \dst, \sym
186 ldr \dst, [\dst, :lo12:\sym]
187 .else
188 adrp \tmp, \sym
189 ldr \dst, [\tmp, :lo12:\sym]
190 .endif
191 .endm
192
193 /*
194 * @src: source register (32 or 64 bit wide)
195 * @sym: name of the symbol
196 * @tmp: mandatory 64-bit scratch register to calculate the address
197 * while <src> needs to be preserved.
198 */
199 .macro str_l, src, sym, tmp
200 adrp \tmp, \sym
201 str \src, [\tmp, :lo12:\sym]
202 .endm
203
James Morseaa4d5d32015-12-10 10:22:39 +0000204 /*
Mark Rutland1b7e2292016-11-03 20:23:12 +0000205 * @dst: Result of per_cpu(sym, smp_processor_id())
James Morseaa4d5d32015-12-10 10:22:39 +0000206 * @sym: The name of the per-cpu variable
James Morseaa4d5d32015-12-10 10:22:39 +0000207 * @tmp: scratch register
208 */
Mark Rutland1b7e2292016-11-03 20:23:12 +0000209 .macro adr_this_cpu, dst, sym, tmp
210 adr_l \dst, \sym
James Morseaa4d5d32015-12-10 10:22:39 +0000211 mrs \tmp, tpidr_el1
Mark Rutland1b7e2292016-11-03 20:23:12 +0000212 add \dst, \dst, \tmp
213 .endm
214
215 /*
216 * @dst: Result of READ_ONCE(per_cpu(sym, smp_processor_id()))
217 * @sym: The name of the per-cpu variable
218 * @tmp: scratch register
219 */
220 .macro ldr_this_cpu dst, sym, tmp
221 adr_l \dst, \sym
222 mrs \tmp, tpidr_el1
223 ldr \dst, [\dst, \tmp]
James Morseaa4d5d32015-12-10 10:22:39 +0000224 .endm
225
Ard Biesheuvel20791842015-10-08 20:02:03 +0100226/*
Geoff Levand7b7293a2016-04-27 17:47:00 +0100227 * vma_vm_mm - get mm pointer from vma pointer (vma->vm_mm)
228 */
229 .macro vma_vm_mm, rd, rn
230 ldr \rd, [\rn, #VMA_VM_MM]
231 .endm
232
233/*
234 * mmid - get context id from mm pointer (mm->context.id)
235 */
236 .macro mmid, rd, rn
237 ldr \rd, [\rn, #MM_CONTEXT_ID]
238 .endm
Suzuki K Poulose116c81f2016-09-09 14:07:16 +0100239/*
240 * read_ctr - read CTR_EL0. If the system has mismatched
241 * cache line sizes, provide the system wide safe value
242 * from arm64_ftr_reg_ctrel0.sys_val
243 */
244 .macro read_ctr, reg
245alternative_if_not ARM64_MISMATCHED_CACHE_LINE_SIZE
246 mrs \reg, ctr_el0 // read CTR
247 nop
248alternative_else
249 ldr_l \reg, arm64_ftr_reg_ctrel0 + ARM64_FTR_SYSVAL
250alternative_endif
251 .endm
252
Geoff Levand7b7293a2016-04-27 17:47:00 +0100253
254/*
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100255 * raw_dcache_line_size - get the minimum D-cache line size on this CPU
256 * from the CTR register.
Geoff Levand7b7293a2016-04-27 17:47:00 +0100257 */
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100258 .macro raw_dcache_line_size, reg, tmp
Geoff Levand7b7293a2016-04-27 17:47:00 +0100259 mrs \tmp, ctr_el0 // read CTR
260 ubfm \tmp, \tmp, #16, #19 // cache line size encoding
261 mov \reg, #4 // bytes per word
262 lsl \reg, \reg, \tmp // actual cache line size
263 .endm
264
265/*
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100266 * dcache_line_size - get the safe D-cache line size across all CPUs
Geoff Levand7b7293a2016-04-27 17:47:00 +0100267 */
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100268 .macro dcache_line_size, reg, tmp
Suzuki K Poulose116c81f2016-09-09 14:07:16 +0100269 read_ctr \tmp
270 ubfm \tmp, \tmp, #16, #19 // cache line size encoding
271 mov \reg, #4 // bytes per word
272 lsl \reg, \reg, \tmp // actual cache line size
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100273 .endm
274
275/*
276 * raw_icache_line_size - get the minimum I-cache line size on this CPU
277 * from the CTR register.
278 */
279 .macro raw_icache_line_size, reg, tmp
Geoff Levand7b7293a2016-04-27 17:47:00 +0100280 mrs \tmp, ctr_el0 // read CTR
281 and \tmp, \tmp, #0xf // cache line size encoding
282 mov \reg, #4 // bytes per word
283 lsl \reg, \reg, \tmp // actual cache line size
284 .endm
285
286/*
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100287 * icache_line_size - get the safe I-cache line size across all CPUs
288 */
289 .macro icache_line_size, reg, tmp
Suzuki K Poulose116c81f2016-09-09 14:07:16 +0100290 read_ctr \tmp
291 and \tmp, \tmp, #0xf // cache line size encoding
292 mov \reg, #4 // bytes per word
293 lsl \reg, \reg, \tmp // actual cache line size
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100294 .endm
295
296/*
Geoff Levand7b7293a2016-04-27 17:47:00 +0100297 * tcr_set_idmap_t0sz - update TCR.T0SZ so that we can load the ID map
298 */
299 .macro tcr_set_idmap_t0sz, valreg, tmpreg
300#ifndef CONFIG_ARM64_VA_BITS_48
301 ldr_l \tmpreg, idmap_t0sz
302 bfi \valreg, \tmpreg, #TCR_T0SZ_OFFSET, #TCR_TxSZ_WIDTH
303#endif
304 .endm
305
306/*
307 * Macro to perform a data cache maintenance for the interval
308 * [kaddr, kaddr + size)
309 *
310 * op: operation passed to dc instruction
311 * domain: domain used in dsb instruciton
312 * kaddr: starting virtual address of the region
313 * size: size of the region
314 * Corrupts: kaddr, size, tmp1, tmp2
315 */
316 .macro dcache_by_line_op op, domain, kaddr, size, tmp1, tmp2
317 dcache_line_size \tmp1, \tmp2
318 add \size, \kaddr, \size
319 sub \tmp2, \tmp1, #1
320 bic \kaddr, \kaddr, \tmp2
Andre Przywara823066d2016-06-28 18:07:29 +01003219998:
322 .if (\op == cvau || \op == cvac)
323alternative_if_not ARM64_WORKAROUND_CLEAN_CACHE
324 dc \op, \kaddr
325alternative_else
326 dc civac, \kaddr
327alternative_endif
328 .else
329 dc \op, \kaddr
330 .endif
Geoff Levand7b7293a2016-04-27 17:47:00 +0100331 add \kaddr, \kaddr, \tmp1
332 cmp \kaddr, \size
333 b.lo 9998b
334 dsb \domain
335 .endm
336
337/*
338 * reset_pmuserenr_el0 - reset PMUSERENR_EL0 if PMUv3 present
339 */
340 .macro reset_pmuserenr_el0, tmpreg
341 mrs \tmpreg, id_aa64dfr0_el1 // Check ID_AA64DFR0_EL1 PMUVer
342 sbfx \tmpreg, \tmpreg, #8, #4
343 cmp \tmpreg, #1 // Skip if no PMU present
344 b.lt 9000f
345 msr pmuserenr_el0, xzr // Disable PMU access from EL0
3469000:
347 .endm
348
349/*
Geoff Levand5003dbd2016-04-27 17:47:10 +0100350 * copy_page - copy src to dest using temp registers t1-t8
351 */
352 .macro copy_page dest:req src:req t1:req t2:req t3:req t4:req t5:req t6:req t7:req t8:req
3539998: ldp \t1, \t2, [\src]
354 ldp \t3, \t4, [\src, #16]
355 ldp \t5, \t6, [\src, #32]
356 ldp \t7, \t8, [\src, #48]
357 add \src, \src, #64
358 stnp \t1, \t2, [\dest]
359 stnp \t3, \t4, [\dest, #16]
360 stnp \t5, \t6, [\dest, #32]
361 stnp \t7, \t8, [\dest, #48]
362 add \dest, \dest, #64
363 tst \src, #(PAGE_SIZE - 1)
364 b.ne 9998b
365 .endm
366
367/*
Ard Biesheuvel20791842015-10-08 20:02:03 +0100368 * Annotate a function as position independent, i.e., safe to be called before
369 * the kernel virtual mapping is activated.
370 */
371#define ENDPIPROC(x) \
372 .globl __pi_##x; \
373 .type __pi_##x, %function; \
374 .set __pi_##x, x; \
375 .size __pi_##x, . - x; \
376 ENDPROC(x)
377
Ard Biesheuvel6ad1fe52015-12-26 13:48:02 +0100378 /*
379 * Emit a 64-bit absolute little endian symbol reference in a way that
380 * ensures that it will be resolved at build time, even when building a
381 * PIE binary. This requires cooperation from the linker script, which
382 * must emit the lo32/hi32 halves individually.
383 */
384 .macro le64sym, sym
385 .long \sym\()_lo32
386 .long \sym\()_hi32
387 .endm
388
Ard Biesheuvel30b5ba52016-04-18 17:09:44 +0200389 /*
390 * mov_q - move an immediate constant into a 64-bit register using
391 * between 2 and 4 movz/movk instructions (depending on the
392 * magnitude and sign of the operand)
393 */
394 .macro mov_q, reg, val
395 .if (((\val) >> 31) == 0 || ((\val) >> 31) == 0x1ffffffff)
396 movz \reg, :abs_g1_s:\val
397 .else
398 .if (((\val) >> 47) == 0 || ((\val) >> 47) == 0x1ffff)
399 movz \reg, :abs_g2_s:\val
400 .else
401 movz \reg, :abs_g3:\val
402 movk \reg, :abs_g2_nc:\val
403 .endif
404 movk \reg, :abs_g1_nc:\val
405 .endif
406 movk \reg, :abs_g0_nc:\val
407 .endm
408
Marc Zyngierf3e39272015-02-20 13:53:13 +0000409#endif /* __ASM_ASSEMBLER_H */