blob: 148849c106047dd353a8ad4c44036c0e721d80b1 [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>
Suzuki K Pouloseb8c32082018-03-26 15:12:49 +010028#include <asm/cputype.h>
Geoff Levand5003dbd2016-04-27 17:47:10 +010029#include <asm/page.h>
Geoff Levand7b7293a2016-04-27 17:47:00 +010030#include <asm/pgtable-hwdef.h>
Catalin Marinas0be73202012-03-05 11:49:26 +000031#include <asm/ptrace.h>
Will Deacon2a283072014-04-29 19:04:06 +010032#include <asm/thread_info.h>
Catalin Marinas0be73202012-03-05 11:49:26 +000033
34/*
Catalin Marinas0be73202012-03-05 11:49:26 +000035 * Enable and disable interrupts.
36 */
37 .macro disable_irq
38 msr daifset, #2
39 .endm
40
41 .macro enable_irq
42 msr daifclr, #2
43 .endm
44
Catalin Marinas005bf1a2016-07-01 16:53:00 +010045 .macro save_and_disable_irq, flags
46 mrs \flags, daif
47 msr daifset, #2
48 .endm
49
50 .macro restore_irq, flags
51 msr daif, \flags
52 .endm
53
Catalin Marinas0be73202012-03-05 11:49:26 +000054/*
Catalin Marinas0be73202012-03-05 11:49:26 +000055 * Enable and disable debug exceptions.
56 */
57 .macro disable_dbg
58 msr daifset, #8
59 .endm
60
61 .macro enable_dbg
62 msr daifclr, #8
63 .endm
64
Will Deacon2a283072014-04-29 19:04:06 +010065 .macro disable_step_tsk, flgs, tmp
66 tbz \flgs, #TIF_SINGLESTEP, 9990f
Catalin Marinas0be73202012-03-05 11:49:26 +000067 mrs \tmp, mdscr_el1
68 bic \tmp, \tmp, #1
69 msr mdscr_el1, \tmp
Will Deacon2a283072014-04-29 19:04:06 +010070 isb // Synchronise with enable_dbg
719990:
Catalin Marinas0be73202012-03-05 11:49:26 +000072 .endm
73
Will Deacon2a283072014-04-29 19:04:06 +010074 .macro enable_step_tsk, flgs, tmp
75 tbz \flgs, #TIF_SINGLESTEP, 9990f
76 disable_dbg
Catalin Marinas0be73202012-03-05 11:49:26 +000077 mrs \tmp, mdscr_el1
78 orr \tmp, \tmp, #1
79 msr mdscr_el1, \tmp
Will Deacon2a283072014-04-29 19:04:06 +0100809990:
Catalin Marinas0be73202012-03-05 11:49:26 +000081 .endm
82
Will Deacon2a283072014-04-29 19:04:06 +010083/*
84 * Enable both debug exceptions and interrupts. This is likely to be
85 * faster than two daifclr operations, since writes to this register
86 * are self-synchronising.
87 */
88 .macro enable_dbg_and_irq
89 msr daifclr, #(8 | 2)
Catalin Marinas0be73202012-03-05 11:49:26 +000090 .endm
91
92/*
93 * SMP data memory barrier
94 */
95 .macro smp_dmb, opt
Catalin Marinas0be73202012-03-05 11:49:26 +000096 dmb \opt
Catalin Marinas0be73202012-03-05 11:49:26 +000097 .endm
98
Ard Biesheuvel6c94f272016-01-01 15:02:12 +010099/*
Mark Rutlandafc09542018-04-12 12:10:57 +0100100 * Value prediction barrier
101 */
102 .macro csdb
103 hint #20
104 .endm
105
106/*
Mark Rutlandf3ed64a2018-04-12 12:11:02 +0100107 * Sanitise a 64-bit bounded index wrt speculation, returning zero if out
108 * of bounds.
109 */
110 .macro mask_nospec64, idx, limit, tmp
111 sub \tmp, \idx, \limit
112 bic \tmp, \tmp, \idx
113 and \idx, \idx, \tmp, asr #63
114 csdb
115 .endm
116
117/*
Will Deaconf99a2502016-09-06 16:40:23 +0100118 * NOP sequence
119 */
120 .macro nops, num
121 .rept \num
122 nop
123 .endr
124 .endm
125
126/*
Ard Biesheuvel6c94f272016-01-01 15:02:12 +0100127 * Emit an entry into the exception table
128 */
129 .macro _asm_extable, from, to
130 .pushsection __ex_table, "a"
131 .align 3
132 .long (\from - .), (\to - .)
133 .popsection
134 .endm
135
Catalin Marinas0be73202012-03-05 11:49:26 +0000136#define USER(l, x...) \
1379999: x; \
Ard Biesheuvel6c94f272016-01-01 15:02:12 +0100138 _asm_extable 9999b, l
Catalin Marinas0be73202012-03-05 11:49:26 +0000139
140/*
141 * Register aliases.
142 */
143lr .req x30 // link register
Marc Zyngierdc637f12012-10-19 17:37:35 +0100144
145/*
146 * Vector entry
147 */
148 .macro ventry label
149 .align 7
150 b \label
151 .endm
Matthew Leache68beda2013-10-11 14:52:15 +0100152
153/*
154 * Select code when configured for BE.
155 */
156#ifdef CONFIG_CPU_BIG_ENDIAN
157#define CPU_BE(code...) code
158#else
159#define CPU_BE(code...)
160#endif
161
162/*
163 * Select code when configured for LE.
164 */
165#ifdef CONFIG_CPU_BIG_ENDIAN
166#define CPU_LE(code...)
167#else
168#define CPU_LE(code...) code
169#endif
170
Matthew Leach55b89542013-10-11 14:52:13 +0100171/*
172 * Define a macro that constructs a 64-bit value by concatenating two
173 * 32-bit registers. Note that on big endian systems the order of the
174 * registers is swapped.
175 */
176#ifndef CONFIG_CPU_BIG_ENDIAN
177 .macro regs_to_64, rd, lbits, hbits
178#else
179 .macro regs_to_64, rd, hbits, lbits
180#endif
181 orr \rd, \lbits, \hbits, lsl #32
182 .endm
Marc Zyngierf3e39272015-02-20 13:53:13 +0000183
Ard Biesheuvelb784a5d2015-03-04 19:45:38 +0100184/*
185 * Pseudo-ops for PC-relative adr/ldr/str <reg>, <symbol> where
Ard Biesheuvelf88f06e2017-01-11 14:54:53 +0000186 * <symbol> is within the range +/- 4 GB of the PC when running
187 * in core kernel context. In module context, a movz/movk sequence
188 * is used, since modules may be loaded far away from the kernel
189 * when KASLR is in effect.
Ard Biesheuvelb784a5d2015-03-04 19:45:38 +0100190 */
191 /*
192 * @dst: destination register (64 bit wide)
193 * @sym: name of the symbol
Ard Biesheuvelb784a5d2015-03-04 19:45:38 +0100194 */
Ard Biesheuvelf88f06e2017-01-11 14:54:53 +0000195 .macro adr_l, dst, sym
196#ifndef MODULE
Ard Biesheuvelb784a5d2015-03-04 19:45:38 +0100197 adrp \dst, \sym
198 add \dst, \dst, :lo12:\sym
Ard Biesheuvelf88f06e2017-01-11 14:54:53 +0000199#else
200 movz \dst, #:abs_g3:\sym
201 movk \dst, #:abs_g2_nc:\sym
202 movk \dst, #:abs_g1_nc:\sym
203 movk \dst, #:abs_g0_nc:\sym
204#endif
Ard Biesheuvelb784a5d2015-03-04 19:45:38 +0100205 .endm
206
207 /*
208 * @dst: destination register (32 or 64 bit wide)
209 * @sym: name of the symbol
210 * @tmp: optional 64-bit scratch register to be used if <dst> is a
211 * 32-bit wide register, in which case it cannot be used to hold
212 * the address
213 */
214 .macro ldr_l, dst, sym, tmp=
Ard Biesheuvelf88f06e2017-01-11 14:54:53 +0000215#ifndef MODULE
Ard Biesheuvelb784a5d2015-03-04 19:45:38 +0100216 .ifb \tmp
217 adrp \dst, \sym
218 ldr \dst, [\dst, :lo12:\sym]
219 .else
220 adrp \tmp, \sym
221 ldr \dst, [\tmp, :lo12:\sym]
222 .endif
Ard Biesheuvelf88f06e2017-01-11 14:54:53 +0000223#else
224 .ifb \tmp
225 adr_l \dst, \sym
226 ldr \dst, [\dst]
227 .else
228 adr_l \tmp, \sym
229 ldr \dst, [\tmp]
230 .endif
231#endif
Ard Biesheuvelb784a5d2015-03-04 19:45:38 +0100232 .endm
233
234 /*
235 * @src: source register (32 or 64 bit wide)
236 * @sym: name of the symbol
237 * @tmp: mandatory 64-bit scratch register to calculate the address
238 * while <src> needs to be preserved.
239 */
240 .macro str_l, src, sym, tmp
Ard Biesheuvelf88f06e2017-01-11 14:54:53 +0000241#ifndef MODULE
Ard Biesheuvelb784a5d2015-03-04 19:45:38 +0100242 adrp \tmp, \sym
243 str \src, [\tmp, :lo12:\sym]
Ard Biesheuvelf88f06e2017-01-11 14:54:53 +0000244#else
245 adr_l \tmp, \sym
246 str \src, [\tmp]
247#endif
Ard Biesheuvelb784a5d2015-03-04 19:45:38 +0100248 .endm
249
James Morseaa4d5d32015-12-10 10:22:39 +0000250 /*
Mark Rutland8b6c9c92016-11-03 20:23:12 +0000251 * @dst: Result of per_cpu(sym, smp_processor_id())
James Morseaa4d5d32015-12-10 10:22:39 +0000252 * @sym: The name of the per-cpu variable
James Morseaa4d5d32015-12-10 10:22:39 +0000253 * @tmp: scratch register
254 */
Mark Rutland8b6c9c92016-11-03 20:23:12 +0000255 .macro adr_this_cpu, dst, sym, tmp
256 adr_l \dst, \sym
James Morseeea59022018-07-20 10:56:16 +0100257alternative_if_not ARM64_HAS_VIRT_HOST_EXTN
James Morseaa4d5d32015-12-10 10:22:39 +0000258 mrs \tmp, tpidr_el1
James Morseeea59022018-07-20 10:56:16 +0100259alternative_else
260 mrs \tmp, tpidr_el2
261alternative_endif
Mark Rutlandc488ae42018-07-20 10:56:12 +0100262 add \dst, \dst, \tmp
263 .endm
264
265 /*
266 * @dst: Result of READ_ONCE(per_cpu(sym, smp_processor_id()))
267 * @sym: The name of the per-cpu variable
268 * @tmp: scratch register
269 */
270 .macro ldr_this_cpu dst, sym, tmp
271 adr_l \dst, \sym
James Morseeea59022018-07-20 10:56:16 +0100272alternative_if_not ARM64_HAS_VIRT_HOST_EXTN
Mark Rutlandc488ae42018-07-20 10:56:12 +0100273 mrs \tmp, tpidr_el1
James Morseeea59022018-07-20 10:56:16 +0100274alternative_else
275 mrs \tmp, tpidr_el2
276alternative_endif
Mark Rutlandc488ae42018-07-20 10:56:12 +0100277 ldr \dst, [\dst, \tmp]
James Morseaa4d5d32015-12-10 10:22:39 +0000278 .endm
279
Ard Biesheuvel20791842015-10-08 20:02:03 +0100280/*
Geoff Levand7b7293a2016-04-27 17:47:00 +0100281 * vma_vm_mm - get mm pointer from vma pointer (vma->vm_mm)
282 */
283 .macro vma_vm_mm, rd, rn
284 ldr \rd, [\rn, #VMA_VM_MM]
285 .endm
286
287/*
288 * mmid - get context id from mm pointer (mm->context.id)
289 */
290 .macro mmid, rd, rn
291 ldr \rd, [\rn, #MM_CONTEXT_ID]
292 .endm
Suzuki K Poulose116c81f2016-09-09 14:07:16 +0100293/*
294 * read_ctr - read CTR_EL0. If the system has mismatched
295 * cache line sizes, provide the system wide safe value
296 * from arm64_ftr_reg_ctrel0.sys_val
297 */
298 .macro read_ctr, reg
299alternative_if_not ARM64_MISMATCHED_CACHE_LINE_SIZE
300 mrs \reg, ctr_el0 // read CTR
301 nop
302alternative_else
303 ldr_l \reg, arm64_ftr_reg_ctrel0 + ARM64_FTR_SYSVAL
304alternative_endif
305 .endm
306
Geoff Levand7b7293a2016-04-27 17:47:00 +0100307
308/*
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100309 * raw_dcache_line_size - get the minimum D-cache line size on this CPU
310 * from the CTR register.
Geoff Levand7b7293a2016-04-27 17:47:00 +0100311 */
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100312 .macro raw_dcache_line_size, reg, tmp
Geoff Levand7b7293a2016-04-27 17:47:00 +0100313 mrs \tmp, ctr_el0 // read CTR
314 ubfm \tmp, \tmp, #16, #19 // cache line size encoding
315 mov \reg, #4 // bytes per word
316 lsl \reg, \reg, \tmp // actual cache line size
317 .endm
318
319/*
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100320 * dcache_line_size - get the safe D-cache line size across all CPUs
Geoff Levand7b7293a2016-04-27 17:47:00 +0100321 */
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100322 .macro dcache_line_size, reg, tmp
Suzuki K Poulose116c81f2016-09-09 14:07:16 +0100323 read_ctr \tmp
324 ubfm \tmp, \tmp, #16, #19 // cache line size encoding
325 mov \reg, #4 // bytes per word
326 lsl \reg, \reg, \tmp // actual cache line size
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100327 .endm
328
329/*
330 * raw_icache_line_size - get the minimum I-cache line size on this CPU
331 * from the CTR register.
332 */
333 .macro raw_icache_line_size, reg, tmp
Geoff Levand7b7293a2016-04-27 17:47:00 +0100334 mrs \tmp, ctr_el0 // read CTR
335 and \tmp, \tmp, #0xf // cache line size encoding
336 mov \reg, #4 // bytes per word
337 lsl \reg, \reg, \tmp // actual cache line size
338 .endm
339
340/*
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100341 * icache_line_size - get the safe I-cache line size across all CPUs
342 */
343 .macro icache_line_size, reg, tmp
Suzuki K Poulose116c81f2016-09-09 14:07:16 +0100344 read_ctr \tmp
345 and \tmp, \tmp, #0xf // cache line size encoding
346 mov \reg, #4 // bytes per word
347 lsl \reg, \reg, \tmp // actual cache line size
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100348 .endm
349
350/*
Geoff Levand7b7293a2016-04-27 17:47:00 +0100351 * tcr_set_idmap_t0sz - update TCR.T0SZ so that we can load the ID map
352 */
353 .macro tcr_set_idmap_t0sz, valreg, tmpreg
354#ifndef CONFIG_ARM64_VA_BITS_48
355 ldr_l \tmpreg, idmap_t0sz
356 bfi \valreg, \tmpreg, #TCR_T0SZ_OFFSET, #TCR_TxSZ_WIDTH
357#endif
358 .endm
359
360/*
361 * Macro to perform a data cache maintenance for the interval
362 * [kaddr, kaddr + size)
363 *
364 * op: operation passed to dc instruction
365 * domain: domain used in dsb instruciton
366 * kaddr: starting virtual address of the region
367 * size: size of the region
368 * Corrupts: kaddr, size, tmp1, tmp2
369 */
370 .macro dcache_by_line_op op, domain, kaddr, size, tmp1, tmp2
371 dcache_line_size \tmp1, \tmp2
372 add \size, \kaddr, \size
373 sub \tmp2, \tmp1, #1
374 bic \kaddr, \kaddr, \tmp2
Andre Przywara823066d2016-06-28 18:07:29 +01003759998:
376 .if (\op == cvau || \op == cvac)
377alternative_if_not ARM64_WORKAROUND_CLEAN_CACHE
378 dc \op, \kaddr
379alternative_else
380 dc civac, \kaddr
381alternative_endif
382 .else
383 dc \op, \kaddr
384 .endif
Geoff Levand7b7293a2016-04-27 17:47:00 +0100385 add \kaddr, \kaddr, \tmp1
386 cmp \kaddr, \size
387 b.lo 9998b
388 dsb \domain
389 .endm
390
391/*
392 * reset_pmuserenr_el0 - reset PMUSERENR_EL0 if PMUv3 present
393 */
394 .macro reset_pmuserenr_el0, tmpreg
395 mrs \tmpreg, id_aa64dfr0_el1 // Check ID_AA64DFR0_EL1 PMUVer
396 sbfx \tmpreg, \tmpreg, #8, #4
397 cmp \tmpreg, #1 // Skip if no PMU present
398 b.lt 9000f
399 msr pmuserenr_el0, xzr // Disable PMU access from EL0
4009000:
401 .endm
402
403/*
Geoff Levand5003dbd2016-04-27 17:47:10 +0100404 * copy_page - copy src to dest using temp registers t1-t8
405 */
406 .macro copy_page dest:req src:req t1:req t2:req t3:req t4:req t5:req t6:req t7:req t8:req
4079998: ldp \t1, \t2, [\src]
408 ldp \t3, \t4, [\src, #16]
409 ldp \t5, \t6, [\src, #32]
410 ldp \t7, \t8, [\src, #48]
411 add \src, \src, #64
412 stnp \t1, \t2, [\dest]
413 stnp \t3, \t4, [\dest, #16]
414 stnp \t5, \t6, [\dest, #32]
415 stnp \t7, \t8, [\dest, #48]
416 add \dest, \dest, #64
417 tst \src, #(PAGE_SIZE - 1)
418 b.ne 9998b
419 .endm
420
421/*
Ard Biesheuvel20791842015-10-08 20:02:03 +0100422 * Annotate a function as position independent, i.e., safe to be called before
423 * the kernel virtual mapping is activated.
424 */
425#define ENDPIPROC(x) \
426 .globl __pi_##x; \
427 .type __pi_##x, %function; \
428 .set __pi_##x, x; \
429 .size __pi_##x, . - x; \
430 ENDPROC(x)
431
Ard Biesheuvel6ad1fe52015-12-26 13:48:02 +0100432 /*
433 * Emit a 64-bit absolute little endian symbol reference in a way that
434 * ensures that it will be resolved at build time, even when building a
435 * PIE binary. This requires cooperation from the linker script, which
436 * must emit the lo32/hi32 halves individually.
437 */
438 .macro le64sym, sym
439 .long \sym\()_lo32
440 .long \sym\()_hi32
441 .endm
442
Ard Biesheuvel30b5ba52016-04-18 17:09:44 +0200443 /*
444 * mov_q - move an immediate constant into a 64-bit register using
445 * between 2 and 4 movz/movk instructions (depending on the
446 * magnitude and sign of the operand)
447 */
448 .macro mov_q, reg, val
449 .if (((\val) >> 31) == 0 || ((\val) >> 31) == 0x1ffffffff)
450 movz \reg, :abs_g1_s:\val
451 .else
452 .if (((\val) >> 47) == 0 || ((\val) >> 47) == 0x1ffff)
453 movz \reg, :abs_g2_s:\val
454 .else
455 movz \reg, :abs_g3:\val
456 movk \reg, :abs_g2_nc:\val
457 .endif
458 movk \reg, :abs_g1_nc:\val
459 .endif
460 movk \reg, :abs_g0_nc:\val
461 .endm
462
Catalin Marinas23759132016-07-01 15:48:55 +0100463/*
Catalin Marinas005bf1a2016-07-01 16:53:00 +0100464 * Return the current thread_info.
465 */
466 .macro get_thread_info, rd
467 mrs \rd, sp_el0
468 .endm
469
Will Deacon4025fe12018-04-03 12:09:20 +0100470 .macro pte_to_phys, phys, pte
471 and \phys, \pte, #(((1 << (48 - PAGE_SHIFT)) - 1) << PAGE_SHIFT)
472 .endm
Mark Rutland965924e2018-04-12 12:11:11 +0100473
Suzuki K Pouloseb8c32082018-03-26 15:12:49 +0100474/*
475 * Check the MIDR_EL1 of the current CPU for a given model and a range of
476 * variant/revision. See asm/cputype.h for the macros used below.
477 *
478 * model: MIDR_CPU_MODEL of CPU
479 * rv_min: Minimum of MIDR_CPU_VAR_REV()
480 * rv_max: Maximum of MIDR_CPU_VAR_REV()
481 * res: Result register.
482 * tmp1, tmp2, tmp3: Temporary registers
483 *
484 * Corrupts: res, tmp1, tmp2, tmp3
485 * Returns: 0, if the CPU id doesn't match. Non-zero otherwise
486 */
487 .macro cpu_midr_match model, rv_min, rv_max, res, tmp1, tmp2, tmp3
488 mrs \res, midr_el1
489 mov_q \tmp1, (MIDR_REVISION_MASK | MIDR_VARIANT_MASK)
490 mov_q \tmp2, MIDR_CPU_MODEL_MASK
491 and \tmp3, \res, \tmp2 // Extract model
492 and \tmp1, \res, \tmp1 // rev & variant
493 mov_q \tmp2, \model
494 cmp \tmp3, \tmp2
495 cset \res, eq
496 cbz \res, .Ldone\@ // Model matches ?
497
498 .if (\rv_min != 0) // Skip min check if rv_min == 0
499 mov_q \tmp3, \rv_min
500 cmp \tmp1, \tmp3
501 cset \res, ge
502 .endif // \rv_min != 0
503 /* Skip rv_max check if rv_min == rv_max && rv_min != 0 */
504 .if ((\rv_min != \rv_max) || \rv_min == 0)
505 mov_q \tmp2, \rv_max
506 cmp \tmp1, \tmp2
507 cset \tmp2, le
508 and \res, \res, \tmp2
509 .endif
510.Ldone\@:
511 .endm
512
Marc Zyngierf3e39272015-02-20 13:53:13 +0000513#endif /* __ASM_ASSEMBLER_H */