blob: a4aa22241dd18ebcb439ef53a9ac231ba1a1c53b [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
Catalin Marinas0be73202012-03-05 11:49:26 +000052/*
Catalin Marinas0be73202012-03-05 11:49:26 +000053 * Enable and disable interrupts.
54 */
55 .macro disable_irq
56 msr daifset, #2
57 .endm
58
59 .macro enable_irq
60 msr daifclr, #2
61 .endm
62
Catalin Marinas4b65a5d2016-07-01 16:53:00 +010063 .macro save_and_disable_irq, flags
64 mrs \flags, daif
65 msr daifset, #2
66 .endm
67
68 .macro restore_irq, flags
69 msr daif, \flags
70 .endm
71
Catalin Marinas0be73202012-03-05 11:49:26 +000072/*
Catalin Marinas0be73202012-03-05 11:49:26 +000073 * Enable and disable debug exceptions.
74 */
75 .macro disable_dbg
76 msr daifset, #8
77 .endm
78
79 .macro enable_dbg
80 msr daifclr, #8
81 .endm
82
Will Deacon2a283072014-04-29 19:04:06 +010083 .macro disable_step_tsk, flgs, tmp
84 tbz \flgs, #TIF_SINGLESTEP, 9990f
Catalin Marinas0be73202012-03-05 11:49:26 +000085 mrs \tmp, mdscr_el1
Julien Thierrye28cc022017-10-25 10:04:32 +010086 bic \tmp, \tmp, #DBG_MDSCR_SS
Catalin Marinas0be73202012-03-05 11:49:26 +000087 msr mdscr_el1, \tmp
Will Deacon2a283072014-04-29 19:04:06 +010088 isb // Synchronise with enable_dbg
899990:
Catalin Marinas0be73202012-03-05 11:49:26 +000090 .endm
91
Will Deacon2a283072014-04-29 19:04:06 +010092 .macro enable_step_tsk, flgs, tmp
93 tbz \flgs, #TIF_SINGLESTEP, 9990f
94 disable_dbg
Catalin Marinas0be73202012-03-05 11:49:26 +000095 mrs \tmp, mdscr_el1
Julien Thierrye28cc022017-10-25 10:04:32 +010096 orr \tmp, \tmp, #DBG_MDSCR_SS
Catalin Marinas0be73202012-03-05 11:49:26 +000097 msr mdscr_el1, \tmp
Will Deacon2a283072014-04-29 19:04:06 +0100989990:
Catalin Marinas0be73202012-03-05 11:49:26 +000099 .endm
100
Will Deacon2a283072014-04-29 19:04:06 +0100101/*
102 * Enable both debug exceptions and interrupts. This is likely to be
103 * faster than two daifclr operations, since writes to this register
104 * are self-synchronising.
105 */
106 .macro enable_dbg_and_irq
107 msr daifclr, #(8 | 2)
Catalin Marinas0be73202012-03-05 11:49:26 +0000108 .endm
109
110/*
111 * SMP data memory barrier
112 */
113 .macro smp_dmb, opt
Catalin Marinas0be73202012-03-05 11:49:26 +0000114 dmb \opt
Catalin Marinas0be73202012-03-05 11:49:26 +0000115 .endm
116
Ard Biesheuvel6c94f272016-01-01 15:02:12 +0100117/*
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 Biesheuvel41c066f2017-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 Biesheuvel41c066f2017-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 Biesheuvel41c066f2017-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 Biesheuvel41c066f2017-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 Biesheuvel41c066f2017-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 Biesheuvel41c066f2017-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 Biesheuvel41c066f2017-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 /*
Ard Biesheuvel8ea41b12017-07-15 17:23:13 +0100251 * @dst: Result of per_cpu(sym, smp_processor_id()), can be SP for
252 * non-module code
James Morseaa4d5d32015-12-10 10:22:39 +0000253 * @sym: The name of the per-cpu variable
James Morseaa4d5d32015-12-10 10:22:39 +0000254 * @tmp: scratch register
255 */
Mark Rutland1b7e2292016-11-03 20:23:12 +0000256 .macro adr_this_cpu, dst, sym, tmp
Ard Biesheuvel8ea41b12017-07-15 17:23:13 +0100257#ifndef MODULE
258 adrp \tmp, \sym
259 add \dst, \tmp, #:lo12:\sym
260#else
Mark Rutland1b7e2292016-11-03 20:23:12 +0000261 adr_l \dst, \sym
Ard Biesheuvel8ea41b12017-07-15 17:23:13 +0100262#endif
James Morseaa4d5d32015-12-10 10:22:39 +0000263 mrs \tmp, tpidr_el1
Mark Rutland1b7e2292016-11-03 20:23:12 +0000264 add \dst, \dst, \tmp
265 .endm
266
267 /*
268 * @dst: Result of READ_ONCE(per_cpu(sym, smp_processor_id()))
269 * @sym: The name of the per-cpu variable
270 * @tmp: scratch register
271 */
272 .macro ldr_this_cpu dst, sym, tmp
273 adr_l \dst, \sym
274 mrs \tmp, tpidr_el1
275 ldr \dst, [\dst, \tmp]
James Morseaa4d5d32015-12-10 10:22:39 +0000276 .endm
277
Ard Biesheuvel20791842015-10-08 20:02:03 +0100278/*
Geoff Levand7b7293a2016-04-27 17:47:00 +0100279 * vma_vm_mm - get mm pointer from vma pointer (vma->vm_mm)
280 */
281 .macro vma_vm_mm, rd, rn
282 ldr \rd, [\rn, #VMA_VM_MM]
283 .endm
284
285/*
286 * mmid - get context id from mm pointer (mm->context.id)
287 */
288 .macro mmid, rd, rn
289 ldr \rd, [\rn, #MM_CONTEXT_ID]
290 .endm
Suzuki K Poulose116c81f2016-09-09 14:07:16 +0100291/*
292 * read_ctr - read CTR_EL0. If the system has mismatched
293 * cache line sizes, provide the system wide safe value
294 * from arm64_ftr_reg_ctrel0.sys_val
295 */
296 .macro read_ctr, reg
297alternative_if_not ARM64_MISMATCHED_CACHE_LINE_SIZE
298 mrs \reg, ctr_el0 // read CTR
299 nop
300alternative_else
301 ldr_l \reg, arm64_ftr_reg_ctrel0 + ARM64_FTR_SYSVAL
302alternative_endif
303 .endm
304
Geoff Levand7b7293a2016-04-27 17:47:00 +0100305
306/*
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100307 * raw_dcache_line_size - get the minimum D-cache line size on this CPU
308 * from the CTR register.
Geoff Levand7b7293a2016-04-27 17:47:00 +0100309 */
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100310 .macro raw_dcache_line_size, reg, tmp
Geoff Levand7b7293a2016-04-27 17:47:00 +0100311 mrs \tmp, ctr_el0 // read CTR
312 ubfm \tmp, \tmp, #16, #19 // cache line size encoding
313 mov \reg, #4 // bytes per word
314 lsl \reg, \reg, \tmp // actual cache line size
315 .endm
316
317/*
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100318 * dcache_line_size - get the safe D-cache line size across all CPUs
Geoff Levand7b7293a2016-04-27 17:47:00 +0100319 */
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100320 .macro dcache_line_size, reg, tmp
Suzuki K Poulose116c81f2016-09-09 14:07:16 +0100321 read_ctr \tmp
322 ubfm \tmp, \tmp, #16, #19 // cache line size encoding
323 mov \reg, #4 // bytes per word
324 lsl \reg, \reg, \tmp // actual cache line size
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100325 .endm
326
327/*
328 * raw_icache_line_size - get the minimum I-cache line size on this CPU
329 * from the CTR register.
330 */
331 .macro raw_icache_line_size, reg, tmp
Geoff Levand7b7293a2016-04-27 17:47:00 +0100332 mrs \tmp, ctr_el0 // read CTR
333 and \tmp, \tmp, #0xf // cache line size encoding
334 mov \reg, #4 // bytes per word
335 lsl \reg, \reg, \tmp // actual cache line size
336 .endm
337
338/*
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100339 * icache_line_size - get the safe I-cache line size across all CPUs
340 */
341 .macro icache_line_size, reg, tmp
Suzuki K Poulose116c81f2016-09-09 14:07:16 +0100342 read_ctr \tmp
343 and \tmp, \tmp, #0xf // cache line size encoding
344 mov \reg, #4 // bytes per word
345 lsl \reg, \reg, \tmp // actual cache line size
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100346 .endm
347
348/*
Geoff Levand7b7293a2016-04-27 17:47:00 +0100349 * tcr_set_idmap_t0sz - update TCR.T0SZ so that we can load the ID map
350 */
351 .macro tcr_set_idmap_t0sz, valreg, tmpreg
352#ifndef CONFIG_ARM64_VA_BITS_48
353 ldr_l \tmpreg, idmap_t0sz
354 bfi \valreg, \tmpreg, #TCR_T0SZ_OFFSET, #TCR_TxSZ_WIDTH
355#endif
356 .endm
357
358/*
359 * Macro to perform a data cache maintenance for the interval
360 * [kaddr, kaddr + size)
361 *
362 * op: operation passed to dc instruction
363 * domain: domain used in dsb instruciton
364 * kaddr: starting virtual address of the region
365 * size: size of the region
366 * Corrupts: kaddr, size, tmp1, tmp2
367 */
368 .macro dcache_by_line_op op, domain, kaddr, size, tmp1, tmp2
369 dcache_line_size \tmp1, \tmp2
370 add \size, \kaddr, \size
371 sub \tmp2, \tmp1, #1
372 bic \kaddr, \kaddr, \tmp2
Andre Przywara823066d2016-06-28 18:07:29 +01003739998:
374 .if (\op == cvau || \op == cvac)
375alternative_if_not ARM64_WORKAROUND_CLEAN_CACHE
376 dc \op, \kaddr
377alternative_else
378 dc civac, \kaddr
379alternative_endif
Robin Murphyd50e0712017-07-25 11:55:42 +0100380 .elseif (\op == cvap)
381alternative_if ARM64_HAS_DCPOP
382 sys 3, c7, c12, 1, \kaddr // dc cvap
383alternative_else
384 dc cvac, \kaddr
385alternative_endif
Andre Przywara823066d2016-06-28 18:07:29 +0100386 .else
387 dc \op, \kaddr
388 .endif
Geoff Levand7b7293a2016-04-27 17:47:00 +0100389 add \kaddr, \kaddr, \tmp1
390 cmp \kaddr, \size
391 b.lo 9998b
392 dsb \domain
393 .endm
394
395/*
396 * reset_pmuserenr_el0 - reset PMUSERENR_EL0 if PMUv3 present
397 */
398 .macro reset_pmuserenr_el0, tmpreg
399 mrs \tmpreg, id_aa64dfr0_el1 // Check ID_AA64DFR0_EL1 PMUVer
400 sbfx \tmpreg, \tmpreg, #8, #4
401 cmp \tmpreg, #1 // Skip if no PMU present
402 b.lt 9000f
403 msr pmuserenr_el0, xzr // Disable PMU access from EL0
4049000:
405 .endm
406
407/*
Geoff Levand5003dbd2016-04-27 17:47:10 +0100408 * copy_page - copy src to dest using temp registers t1-t8
409 */
410 .macro copy_page dest:req src:req t1:req t2:req t3:req t4:req t5:req t6:req t7:req t8:req
4119998: ldp \t1, \t2, [\src]
412 ldp \t3, \t4, [\src, #16]
413 ldp \t5, \t6, [\src, #32]
414 ldp \t7, \t8, [\src, #48]
415 add \src, \src, #64
416 stnp \t1, \t2, [\dest]
417 stnp \t3, \t4, [\dest, #16]
418 stnp \t5, \t6, [\dest, #32]
419 stnp \t7, \t8, [\dest, #48]
420 add \dest, \dest, #64
421 tst \src, #(PAGE_SIZE - 1)
422 b.ne 9998b
423 .endm
424
425/*
Ard Biesheuvel20791842015-10-08 20:02:03 +0100426 * Annotate a function as position independent, i.e., safe to be called before
427 * the kernel virtual mapping is activated.
428 */
429#define ENDPIPROC(x) \
430 .globl __pi_##x; \
431 .type __pi_##x, %function; \
432 .set __pi_##x, x; \
433 .size __pi_##x, . - x; \
434 ENDPROC(x)
435
Mark Rutlanded84b4e2017-07-26 16:05:20 +0100436/*
437 * Annotate a function as being unsuitable for kprobes.
438 */
439#ifdef CONFIG_KPROBES
440#define NOKPROBE(x) \
441 .pushsection "_kprobe_blacklist", "aw"; \
442 .quad x; \
443 .popsection;
444#else
445#define NOKPROBE(x)
446#endif
Ard Biesheuvel6ad1fe52015-12-26 13:48:02 +0100447 /*
448 * Emit a 64-bit absolute little endian symbol reference in a way that
449 * ensures that it will be resolved at build time, even when building a
450 * PIE binary. This requires cooperation from the linker script, which
451 * must emit the lo32/hi32 halves individually.
452 */
453 .macro le64sym, sym
454 .long \sym\()_lo32
455 .long \sym\()_hi32
456 .endm
457
Ard Biesheuvel30b5ba52016-04-18 17:09:44 +0200458 /*
459 * mov_q - move an immediate constant into a 64-bit register using
460 * between 2 and 4 movz/movk instructions (depending on the
461 * magnitude and sign of the operand)
462 */
463 .macro mov_q, reg, val
464 .if (((\val) >> 31) == 0 || ((\val) >> 31) == 0x1ffffffff)
465 movz \reg, :abs_g1_s:\val
466 .else
467 .if (((\val) >> 47) == 0 || ((\val) >> 47) == 0x1ffff)
468 movz \reg, :abs_g2_s:\val
469 .else
470 movz \reg, :abs_g3:\val
471 movk \reg, :abs_g2_nc:\val
472 .endif
473 movk \reg, :abs_g1_nc:\val
474 .endif
475 movk \reg, :abs_g0_nc:\val
476 .endm
477
Catalin Marinasf33bcf02016-07-01 15:48:55 +0100478/*
Catalin Marinas4b65a5d2016-07-01 16:53:00 +0100479 * Return the current thread_info.
480 */
481 .macro get_thread_info, rd
482 mrs \rd, sp_el0
483 .endm
484
485/*
Christopher Covington38fd94b2017-02-08 15:08:37 -0500486 * Errata workaround prior to TTBR0_EL1 update
487 *
488 * val: TTBR value with new BADDR, preserved
489 * tmp0: temporary register, clobbered
490 * tmp1: other temporary register, clobbered
491 */
492 .macro pre_ttbr0_update_workaround, val, tmp0, tmp1
493#ifdef CONFIG_QCOM_FALKOR_ERRATUM_1003
494alternative_if ARM64_WORKAROUND_QCOM_FALKOR_E1003
495 mrs \tmp0, ttbr0_el1
496 mov \tmp1, #FALKOR_RESERVED_ASID
497 bfi \tmp0, \tmp1, #48, #16 // reserved ASID + old BADDR
498 msr ttbr0_el1, \tmp0
499 isb
500 bfi \tmp0, \val, #0, #48 // reserved ASID + new BADDR
501 msr ttbr0_el1, \tmp0
502 isb
503alternative_else_nop_endif
504#endif
505 .endm
506
507/*
Catalin Marinasf33bcf02016-07-01 15:48:55 +0100508 * Errata workaround post TTBR0_EL1 update.
509 */
510 .macro post_ttbr0_update_workaround
511#ifdef CONFIG_CAVIUM_ERRATUM_27456
512alternative_if ARM64_WORKAROUND_CAVIUM_27456
513 ic iallu
514 dsb nsh
515 isb
516alternative_else_nop_endif
517#endif
518 .endm
519
Marc Zyngierf3e39272015-02-20 13:53:13 +0000520#endif /* __ASM_ASSEMBLER_H */