blob: 1886b8b4467216009567cde9f21c09882f655b7f [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>
Julien Thierrye28cc022017-10-25 10:04:32 +010028#include <asm/debug-monitors.h>
Christopher Covington38fd94b2017-02-08 15:08:37 -050029#include <asm/mmu_context.h>
Geoff Levand5003dbd2016-04-27 17:47:10 +010030#include <asm/page.h>
Geoff Levand7b7293a2016-04-27 17:47:00 +010031#include <asm/pgtable-hwdef.h>
Catalin Marinas0be73202012-03-05 11:49:26 +000032#include <asm/ptrace.h>
Will Deacon2a283072014-04-29 19:04:06 +010033#include <asm/thread_info.h>
Catalin Marinas0be73202012-03-05 11:49:26 +000034
James Morse0fbeb312017-11-02 12:12:34 +000035 .macro save_and_disable_daif, flags
36 mrs \flags, daif
37 msr daifset, #0xf
38 .endm
39
40 .macro disable_daif
41 msr daifset, #0xf
42 .endm
43
44 .macro enable_daif
45 msr daifclr, #0xf
46 .endm
47
48 .macro restore_daif, flags:req
49 msr daif, \flags
50 .endm
51
James Morseb55a5a12017-11-02 12:12:39 +000052 /* Only on aarch64 pstate, PSR_D_BIT is different for aarch32 */
53 .macro inherit_daif, pstate:req, tmp:req
54 and \tmp, \pstate, #(PSR_D_BIT | PSR_A_BIT | PSR_I_BIT | PSR_F_BIT)
55 msr daif, \tmp
56 .endm
57
Catalin Marinas0be73202012-03-05 11:49:26 +000058/*
Catalin Marinas0be73202012-03-05 11:49:26 +000059 * Enable and disable interrupts.
60 */
61 .macro disable_irq
62 msr daifset, #2
63 .endm
64
65 .macro enable_irq
66 msr daifclr, #2
67 .endm
68
Catalin Marinas4b65a5d2016-07-01 16:53:00 +010069 .macro save_and_disable_irq, flags
70 mrs \flags, daif
71 msr daifset, #2
72 .endm
73
74 .macro restore_irq, flags
75 msr daif, \flags
76 .endm
77
Catalin Marinas0be73202012-03-05 11:49:26 +000078 .macro enable_dbg
79 msr daifclr, #8
80 .endm
81
Will Deacon2a283072014-04-29 19:04:06 +010082 .macro disable_step_tsk, flgs, tmp
83 tbz \flgs, #TIF_SINGLESTEP, 9990f
Catalin Marinas0be73202012-03-05 11:49:26 +000084 mrs \tmp, mdscr_el1
Julien Thierrye28cc022017-10-25 10:04:32 +010085 bic \tmp, \tmp, #DBG_MDSCR_SS
Catalin Marinas0be73202012-03-05 11:49:26 +000086 msr mdscr_el1, \tmp
Will Deacon2a283072014-04-29 19:04:06 +010087 isb // Synchronise with enable_dbg
889990:
Catalin Marinas0be73202012-03-05 11:49:26 +000089 .endm
90
James Morse84d0fb12017-11-02 12:12:38 +000091 /* call with daif masked */
Will Deacon2a283072014-04-29 19:04:06 +010092 .macro enable_step_tsk, flgs, tmp
93 tbz \flgs, #TIF_SINGLESTEP, 9990f
Catalin Marinas0be73202012-03-05 11:49:26 +000094 mrs \tmp, mdscr_el1
Julien Thierrye28cc022017-10-25 10:04:32 +010095 orr \tmp, \tmp, #DBG_MDSCR_SS
Catalin Marinas0be73202012-03-05 11:49:26 +000096 msr mdscr_el1, \tmp
Will Deacon2a283072014-04-29 19:04:06 +0100979990:
Catalin Marinas0be73202012-03-05 11:49:26 +000098 .endm
99
Will Deacon2a283072014-04-29 19:04:06 +0100100/*
101 * Enable both debug exceptions and interrupts. This is likely to be
102 * faster than two daifclr operations, since writes to this register
103 * are self-synchronising.
104 */
105 .macro enable_dbg_and_irq
106 msr daifclr, #(8 | 2)
Catalin Marinas0be73202012-03-05 11:49:26 +0000107 .endm
108
109/*
110 * SMP data memory barrier
111 */
112 .macro smp_dmb, opt
Catalin Marinas0be73202012-03-05 11:49:26 +0000113 dmb \opt
Catalin Marinas0be73202012-03-05 11:49:26 +0000114 .endm
115
Ard Biesheuvel6c94f272016-01-01 15:02:12 +0100116/*
Will Deaconf99a2502016-09-06 16:40:23 +0100117 * NOP sequence
118 */
119 .macro nops, num
120 .rept \num
121 nop
122 .endr
123 .endm
124
125/*
Ard Biesheuvel6c94f272016-01-01 15:02:12 +0100126 * Emit an entry into the exception table
127 */
128 .macro _asm_extable, from, to
129 .pushsection __ex_table, "a"
130 .align 3
131 .long (\from - .), (\to - .)
132 .popsection
133 .endm
134
Catalin Marinas0be73202012-03-05 11:49:26 +0000135#define USER(l, x...) \
1369999: x; \
Ard Biesheuvel6c94f272016-01-01 15:02:12 +0100137 _asm_extable 9999b, l
Catalin Marinas0be73202012-03-05 11:49:26 +0000138
139/*
140 * Register aliases.
141 */
142lr .req x30 // link register
Marc Zyngierdc637f12012-10-19 17:37:35 +0100143
144/*
145 * Vector entry
146 */
147 .macro ventry label
148 .align 7
149 b \label
150 .endm
Matthew Leache68beda2013-10-11 14:52:15 +0100151
152/*
153 * Select code when configured for BE.
154 */
155#ifdef CONFIG_CPU_BIG_ENDIAN
156#define CPU_BE(code...) code
157#else
158#define CPU_BE(code...)
159#endif
160
161/*
162 * Select code when configured for LE.
163 */
164#ifdef CONFIG_CPU_BIG_ENDIAN
165#define CPU_LE(code...)
166#else
167#define CPU_LE(code...) code
168#endif
169
Matthew Leach55b89542013-10-11 14:52:13 +0100170/*
171 * Define a macro that constructs a 64-bit value by concatenating two
172 * 32-bit registers. Note that on big endian systems the order of the
173 * registers is swapped.
174 */
175#ifndef CONFIG_CPU_BIG_ENDIAN
176 .macro regs_to_64, rd, lbits, hbits
177#else
178 .macro regs_to_64, rd, hbits, lbits
179#endif
180 orr \rd, \lbits, \hbits, lsl #32
181 .endm
Marc Zyngierf3e39272015-02-20 13:53:13 +0000182
Ard Biesheuvelb784a5d2015-03-04 19:45:38 +0100183/*
184 * Pseudo-ops for PC-relative adr/ldr/str <reg>, <symbol> where
Ard Biesheuvel41c066f2017-01-11 14:54:53 +0000185 * <symbol> is within the range +/- 4 GB of the PC when running
186 * in core kernel context. In module context, a movz/movk sequence
187 * is used, since modules may be loaded far away from the kernel
188 * when KASLR is in effect.
Ard Biesheuvelb784a5d2015-03-04 19:45:38 +0100189 */
190 /*
191 * @dst: destination register (64 bit wide)
192 * @sym: name of the symbol
Ard Biesheuvelb784a5d2015-03-04 19:45:38 +0100193 */
Ard Biesheuvel41c066f2017-01-11 14:54:53 +0000194 .macro adr_l, dst, sym
195#ifndef MODULE
Ard Biesheuvelb784a5d2015-03-04 19:45:38 +0100196 adrp \dst, \sym
197 add \dst, \dst, :lo12:\sym
Ard Biesheuvel41c066f2017-01-11 14:54:53 +0000198#else
199 movz \dst, #:abs_g3:\sym
200 movk \dst, #:abs_g2_nc:\sym
201 movk \dst, #:abs_g1_nc:\sym
202 movk \dst, #:abs_g0_nc:\sym
203#endif
Ard Biesheuvelb784a5d2015-03-04 19:45:38 +0100204 .endm
205
206 /*
207 * @dst: destination register (32 or 64 bit wide)
208 * @sym: name of the symbol
209 * @tmp: optional 64-bit scratch register to be used if <dst> is a
210 * 32-bit wide register, in which case it cannot be used to hold
211 * the address
212 */
213 .macro ldr_l, dst, sym, tmp=
Ard Biesheuvel41c066f2017-01-11 14:54:53 +0000214#ifndef MODULE
Ard Biesheuvelb784a5d2015-03-04 19:45:38 +0100215 .ifb \tmp
216 adrp \dst, \sym
217 ldr \dst, [\dst, :lo12:\sym]
218 .else
219 adrp \tmp, \sym
220 ldr \dst, [\tmp, :lo12:\sym]
221 .endif
Ard Biesheuvel41c066f2017-01-11 14:54:53 +0000222#else
223 .ifb \tmp
224 adr_l \dst, \sym
225 ldr \dst, [\dst]
226 .else
227 adr_l \tmp, \sym
228 ldr \dst, [\tmp]
229 .endif
230#endif
Ard Biesheuvelb784a5d2015-03-04 19:45:38 +0100231 .endm
232
233 /*
234 * @src: source register (32 or 64 bit wide)
235 * @sym: name of the symbol
236 * @tmp: mandatory 64-bit scratch register to calculate the address
237 * while <src> needs to be preserved.
238 */
239 .macro str_l, src, sym, tmp
Ard Biesheuvel41c066f2017-01-11 14:54:53 +0000240#ifndef MODULE
Ard Biesheuvelb784a5d2015-03-04 19:45:38 +0100241 adrp \tmp, \sym
242 str \src, [\tmp, :lo12:\sym]
Ard Biesheuvel41c066f2017-01-11 14:54:53 +0000243#else
244 adr_l \tmp, \sym
245 str \src, [\tmp]
246#endif
Ard Biesheuvelb784a5d2015-03-04 19:45:38 +0100247 .endm
248
James Morseaa4d5d32015-12-10 10:22:39 +0000249 /*
Ard Biesheuvel8ea41b12017-07-15 17:23:13 +0100250 * @dst: Result of per_cpu(sym, smp_processor_id()), can be SP for
251 * non-module code
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 Rutland1b7e2292016-11-03 20:23:12 +0000255 .macro adr_this_cpu, dst, sym, tmp
Ard Biesheuvel8ea41b12017-07-15 17:23:13 +0100256#ifndef MODULE
257 adrp \tmp, \sym
258 add \dst, \tmp, #:lo12:\sym
259#else
Mark Rutland1b7e2292016-11-03 20:23:12 +0000260 adr_l \dst, \sym
Ard Biesheuvel8ea41b12017-07-15 17:23:13 +0100261#endif
James Morseaa4d5d32015-12-10 10:22:39 +0000262 mrs \tmp, tpidr_el1
Mark Rutland1b7e2292016-11-03 20:23:12 +0000263 add \dst, \dst, \tmp
264 .endm
265
266 /*
267 * @dst: Result of READ_ONCE(per_cpu(sym, smp_processor_id()))
268 * @sym: The name of the per-cpu variable
269 * @tmp: scratch register
270 */
271 .macro ldr_this_cpu dst, sym, tmp
272 adr_l \dst, \sym
273 mrs \tmp, tpidr_el1
274 ldr \dst, [\dst, \tmp]
James Morseaa4d5d32015-12-10 10:22:39 +0000275 .endm
276
Ard Biesheuvel20791842015-10-08 20:02:03 +0100277/*
Geoff Levand7b7293a2016-04-27 17:47:00 +0100278 * vma_vm_mm - get mm pointer from vma pointer (vma->vm_mm)
279 */
280 .macro vma_vm_mm, rd, rn
281 ldr \rd, [\rn, #VMA_VM_MM]
282 .endm
283
284/*
285 * mmid - get context id from mm pointer (mm->context.id)
286 */
287 .macro mmid, rd, rn
288 ldr \rd, [\rn, #MM_CONTEXT_ID]
289 .endm
Suzuki K Poulose116c81f2016-09-09 14:07:16 +0100290/*
291 * read_ctr - read CTR_EL0. If the system has mismatched
292 * cache line sizes, provide the system wide safe value
293 * from arm64_ftr_reg_ctrel0.sys_val
294 */
295 .macro read_ctr, reg
296alternative_if_not ARM64_MISMATCHED_CACHE_LINE_SIZE
297 mrs \reg, ctr_el0 // read CTR
298 nop
299alternative_else
300 ldr_l \reg, arm64_ftr_reg_ctrel0 + ARM64_FTR_SYSVAL
301alternative_endif
302 .endm
303
Geoff Levand7b7293a2016-04-27 17:47:00 +0100304
305/*
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100306 * raw_dcache_line_size - get the minimum D-cache line size on this CPU
307 * from the CTR register.
Geoff Levand7b7293a2016-04-27 17:47:00 +0100308 */
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100309 .macro raw_dcache_line_size, reg, tmp
Geoff Levand7b7293a2016-04-27 17:47:00 +0100310 mrs \tmp, ctr_el0 // read CTR
311 ubfm \tmp, \tmp, #16, #19 // cache line size encoding
312 mov \reg, #4 // bytes per word
313 lsl \reg, \reg, \tmp // actual cache line size
314 .endm
315
316/*
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100317 * dcache_line_size - get the safe D-cache line size across all CPUs
Geoff Levand7b7293a2016-04-27 17:47:00 +0100318 */
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100319 .macro dcache_line_size, reg, tmp
Suzuki K Poulose116c81f2016-09-09 14:07:16 +0100320 read_ctr \tmp
321 ubfm \tmp, \tmp, #16, #19 // cache line size encoding
322 mov \reg, #4 // bytes per word
323 lsl \reg, \reg, \tmp // actual cache line size
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100324 .endm
325
326/*
327 * raw_icache_line_size - get the minimum I-cache line size on this CPU
328 * from the CTR register.
329 */
330 .macro raw_icache_line_size, reg, tmp
Geoff Levand7b7293a2016-04-27 17:47:00 +0100331 mrs \tmp, ctr_el0 // read CTR
332 and \tmp, \tmp, #0xf // cache line size encoding
333 mov \reg, #4 // bytes per word
334 lsl \reg, \reg, \tmp // actual cache line size
335 .endm
336
337/*
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100338 * icache_line_size - get the safe I-cache line size across all CPUs
339 */
340 .macro icache_line_size, reg, tmp
Suzuki K Poulose116c81f2016-09-09 14:07:16 +0100341 read_ctr \tmp
342 and \tmp, \tmp, #0xf // cache line size encoding
343 mov \reg, #4 // bytes per word
344 lsl \reg, \reg, \tmp // actual cache line size
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100345 .endm
346
347/*
Geoff Levand7b7293a2016-04-27 17:47:00 +0100348 * tcr_set_idmap_t0sz - update TCR.T0SZ so that we can load the ID map
349 */
350 .macro tcr_set_idmap_t0sz, valreg, tmpreg
351#ifndef CONFIG_ARM64_VA_BITS_48
352 ldr_l \tmpreg, idmap_t0sz
353 bfi \valreg, \tmpreg, #TCR_T0SZ_OFFSET, #TCR_TxSZ_WIDTH
354#endif
355 .endm
356
357/*
358 * Macro to perform a data cache maintenance for the interval
359 * [kaddr, kaddr + size)
360 *
361 * op: operation passed to dc instruction
362 * domain: domain used in dsb instruciton
363 * kaddr: starting virtual address of the region
364 * size: size of the region
365 * Corrupts: kaddr, size, tmp1, tmp2
366 */
367 .macro dcache_by_line_op op, domain, kaddr, size, tmp1, tmp2
368 dcache_line_size \tmp1, \tmp2
369 add \size, \kaddr, \size
370 sub \tmp2, \tmp1, #1
371 bic \kaddr, \kaddr, \tmp2
Andre Przywara823066d2016-06-28 18:07:29 +01003729998:
373 .if (\op == cvau || \op == cvac)
374alternative_if_not ARM64_WORKAROUND_CLEAN_CACHE
375 dc \op, \kaddr
376alternative_else
377 dc civac, \kaddr
378alternative_endif
Robin Murphyd50e0712017-07-25 11:55:42 +0100379 .elseif (\op == cvap)
380alternative_if ARM64_HAS_DCPOP
381 sys 3, c7, c12, 1, \kaddr // dc cvap
382alternative_else
383 dc cvac, \kaddr
384alternative_endif
Andre Przywara823066d2016-06-28 18:07:29 +0100385 .else
386 dc \op, \kaddr
387 .endif
Geoff Levand7b7293a2016-04-27 17:47:00 +0100388 add \kaddr, \kaddr, \tmp1
389 cmp \kaddr, \size
390 b.lo 9998b
391 dsb \domain
392 .endm
393
394/*
395 * reset_pmuserenr_el0 - reset PMUSERENR_EL0 if PMUv3 present
396 */
397 .macro reset_pmuserenr_el0, tmpreg
398 mrs \tmpreg, id_aa64dfr0_el1 // Check ID_AA64DFR0_EL1 PMUVer
399 sbfx \tmpreg, \tmpreg, #8, #4
400 cmp \tmpreg, #1 // Skip if no PMU present
401 b.lt 9000f
402 msr pmuserenr_el0, xzr // Disable PMU access from EL0
4039000:
404 .endm
405
406/*
Geoff Levand5003dbd2016-04-27 17:47:10 +0100407 * copy_page - copy src to dest using temp registers t1-t8
408 */
409 .macro copy_page dest:req src:req t1:req t2:req t3:req t4:req t5:req t6:req t7:req t8:req
4109998: ldp \t1, \t2, [\src]
411 ldp \t3, \t4, [\src, #16]
412 ldp \t5, \t6, [\src, #32]
413 ldp \t7, \t8, [\src, #48]
414 add \src, \src, #64
415 stnp \t1, \t2, [\dest]
416 stnp \t3, \t4, [\dest, #16]
417 stnp \t5, \t6, [\dest, #32]
418 stnp \t7, \t8, [\dest, #48]
419 add \dest, \dest, #64
420 tst \src, #(PAGE_SIZE - 1)
421 b.ne 9998b
422 .endm
423
424/*
Ard Biesheuvel20791842015-10-08 20:02:03 +0100425 * Annotate a function as position independent, i.e., safe to be called before
426 * the kernel virtual mapping is activated.
427 */
428#define ENDPIPROC(x) \
429 .globl __pi_##x; \
430 .type __pi_##x, %function; \
431 .set __pi_##x, x; \
432 .size __pi_##x, . - x; \
433 ENDPROC(x)
434
Mark Rutlanded84b4e2017-07-26 16:05:20 +0100435/*
436 * Annotate a function as being unsuitable for kprobes.
437 */
438#ifdef CONFIG_KPROBES
439#define NOKPROBE(x) \
440 .pushsection "_kprobe_blacklist", "aw"; \
441 .quad x; \
442 .popsection;
443#else
444#define NOKPROBE(x)
445#endif
Ard Biesheuvel6ad1fe52015-12-26 13:48:02 +0100446 /*
447 * Emit a 64-bit absolute little endian symbol reference in a way that
448 * ensures that it will be resolved at build time, even when building a
449 * PIE binary. This requires cooperation from the linker script, which
450 * must emit the lo32/hi32 halves individually.
451 */
452 .macro le64sym, sym
453 .long \sym\()_lo32
454 .long \sym\()_hi32
455 .endm
456
Ard Biesheuvel30b5ba52016-04-18 17:09:44 +0200457 /*
458 * mov_q - move an immediate constant into a 64-bit register using
459 * between 2 and 4 movz/movk instructions (depending on the
460 * magnitude and sign of the operand)
461 */
462 .macro mov_q, reg, val
463 .if (((\val) >> 31) == 0 || ((\val) >> 31) == 0x1ffffffff)
464 movz \reg, :abs_g1_s:\val
465 .else
466 .if (((\val) >> 47) == 0 || ((\val) >> 47) == 0x1ffff)
467 movz \reg, :abs_g2_s:\val
468 .else
469 movz \reg, :abs_g3:\val
470 movk \reg, :abs_g2_nc:\val
471 .endif
472 movk \reg, :abs_g1_nc:\val
473 .endif
474 movk \reg, :abs_g0_nc:\val
475 .endm
476
Catalin Marinasf33bcf02016-07-01 15:48:55 +0100477/*
Catalin Marinas4b65a5d2016-07-01 16:53:00 +0100478 * Return the current thread_info.
479 */
480 .macro get_thread_info, rd
481 mrs \rd, sp_el0
482 .endm
483
484/*
Christopher Covington38fd94b2017-02-08 15:08:37 -0500485 * Errata workaround prior to TTBR0_EL1 update
486 *
487 * val: TTBR value with new BADDR, preserved
488 * tmp0: temporary register, clobbered
489 * tmp1: other temporary register, clobbered
490 */
491 .macro pre_ttbr0_update_workaround, val, tmp0, tmp1
492#ifdef CONFIG_QCOM_FALKOR_ERRATUM_1003
493alternative_if ARM64_WORKAROUND_QCOM_FALKOR_E1003
494 mrs \tmp0, ttbr0_el1
495 mov \tmp1, #FALKOR_RESERVED_ASID
496 bfi \tmp0, \tmp1, #48, #16 // reserved ASID + old BADDR
497 msr ttbr0_el1, \tmp0
498 isb
499 bfi \tmp0, \val, #0, #48 // reserved ASID + new BADDR
500 msr ttbr0_el1, \tmp0
501 isb
502alternative_else_nop_endif
503#endif
504 .endm
505
506/*
Catalin Marinasf33bcf02016-07-01 15:48:55 +0100507 * Errata workaround post TTBR0_EL1 update.
508 */
509 .macro post_ttbr0_update_workaround
510#ifdef CONFIG_CAVIUM_ERRATUM_27456
511alternative_if ARM64_WORKAROUND_CAVIUM_27456
512 ic iallu
513 dsb nsh
514 isb
515alternative_else_nop_endif
516#endif
517 .endm
518
Marc Zyngierf3e39272015-02-20 13:53:13 +0000519#endif /* __ASM_ASSEMBLER_H */