blob: 3128a9ca57019945ac1ffacc42d23008dd308ed9 [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
35/*
Catalin Marinas0be73202012-03-05 11:49:26 +000036 * Enable and disable interrupts.
37 */
38 .macro disable_irq
39 msr daifset, #2
40 .endm
41
42 .macro enable_irq
43 msr daifclr, #2
44 .endm
45
Catalin Marinas4b65a5d2016-07-01 16:53:00 +010046 .macro save_and_disable_irq, flags
47 mrs \flags, daif
48 msr daifset, #2
49 .endm
50
51 .macro restore_irq, flags
52 msr daif, \flags
53 .endm
54
Catalin Marinas0be73202012-03-05 11:49:26 +000055/*
Catalin Marinas0be73202012-03-05 11:49:26 +000056 * Enable and disable debug exceptions.
57 */
58 .macro disable_dbg
59 msr daifset, #8
60 .endm
61
62 .macro enable_dbg
63 msr daifclr, #8
64 .endm
65
Will Deacon2a283072014-04-29 19:04:06 +010066 .macro disable_step_tsk, flgs, tmp
67 tbz \flgs, #TIF_SINGLESTEP, 9990f
Catalin Marinas0be73202012-03-05 11:49:26 +000068 mrs \tmp, mdscr_el1
Julien Thierrye28cc022017-10-25 10:04:32 +010069 bic \tmp, \tmp, #DBG_MDSCR_SS
Catalin Marinas0be73202012-03-05 11:49:26 +000070 msr mdscr_el1, \tmp
Will Deacon2a283072014-04-29 19:04:06 +010071 isb // Synchronise with enable_dbg
729990:
Catalin Marinas0be73202012-03-05 11:49:26 +000073 .endm
74
Will Deacon2a283072014-04-29 19:04:06 +010075 .macro enable_step_tsk, flgs, tmp
76 tbz \flgs, #TIF_SINGLESTEP, 9990f
77 disable_dbg
Catalin Marinas0be73202012-03-05 11:49:26 +000078 mrs \tmp, mdscr_el1
Julien Thierrye28cc022017-10-25 10:04:32 +010079 orr \tmp, \tmp, #DBG_MDSCR_SS
Catalin Marinas0be73202012-03-05 11:49:26 +000080 msr mdscr_el1, \tmp
Will Deacon2a283072014-04-29 19:04:06 +0100819990:
Catalin Marinas0be73202012-03-05 11:49:26 +000082 .endm
83
Will Deacon2a283072014-04-29 19:04:06 +010084/*
85 * Enable both debug exceptions and interrupts. This is likely to be
86 * faster than two daifclr operations, since writes to this register
87 * are self-synchronising.
88 */
89 .macro enable_dbg_and_irq
90 msr daifclr, #(8 | 2)
Catalin Marinas0be73202012-03-05 11:49:26 +000091 .endm
92
93/*
94 * SMP data memory barrier
95 */
96 .macro smp_dmb, opt
Catalin Marinas0be73202012-03-05 11:49:26 +000097 dmb \opt
Catalin Marinas0be73202012-03-05 11:49:26 +000098 .endm
99
Ard Biesheuvel6c94f272016-01-01 15:02:12 +0100100/*
Will Deaconf99a2502016-09-06 16:40:23 +0100101 * NOP sequence
102 */
103 .macro nops, num
104 .rept \num
105 nop
106 .endr
107 .endm
108
109/*
Ard Biesheuvel6c94f272016-01-01 15:02:12 +0100110 * Emit an entry into the exception table
111 */
112 .macro _asm_extable, from, to
113 .pushsection __ex_table, "a"
114 .align 3
115 .long (\from - .), (\to - .)
116 .popsection
117 .endm
118
Catalin Marinas0be73202012-03-05 11:49:26 +0000119#define USER(l, x...) \
1209999: x; \
Ard Biesheuvel6c94f272016-01-01 15:02:12 +0100121 _asm_extable 9999b, l
Catalin Marinas0be73202012-03-05 11:49:26 +0000122
123/*
124 * Register aliases.
125 */
126lr .req x30 // link register
Marc Zyngierdc637f12012-10-19 17:37:35 +0100127
128/*
129 * Vector entry
130 */
131 .macro ventry label
132 .align 7
133 b \label
134 .endm
Matthew Leache68beda2013-10-11 14:52:15 +0100135
136/*
137 * Select code when configured for BE.
138 */
139#ifdef CONFIG_CPU_BIG_ENDIAN
140#define CPU_BE(code...) code
141#else
142#define CPU_BE(code...)
143#endif
144
145/*
146 * Select code when configured for LE.
147 */
148#ifdef CONFIG_CPU_BIG_ENDIAN
149#define CPU_LE(code...)
150#else
151#define CPU_LE(code...) code
152#endif
153
Matthew Leach55b89542013-10-11 14:52:13 +0100154/*
155 * Define a macro that constructs a 64-bit value by concatenating two
156 * 32-bit registers. Note that on big endian systems the order of the
157 * registers is swapped.
158 */
159#ifndef CONFIG_CPU_BIG_ENDIAN
160 .macro regs_to_64, rd, lbits, hbits
161#else
162 .macro regs_to_64, rd, hbits, lbits
163#endif
164 orr \rd, \lbits, \hbits, lsl #32
165 .endm
Marc Zyngierf3e39272015-02-20 13:53:13 +0000166
Ard Biesheuvelb784a5d2015-03-04 19:45:38 +0100167/*
168 * Pseudo-ops for PC-relative adr/ldr/str <reg>, <symbol> where
Ard Biesheuvel41c066f2017-01-11 14:54:53 +0000169 * <symbol> is within the range +/- 4 GB of the PC when running
170 * in core kernel context. In module context, a movz/movk sequence
171 * is used, since modules may be loaded far away from the kernel
172 * when KASLR is in effect.
Ard Biesheuvelb784a5d2015-03-04 19:45:38 +0100173 */
174 /*
175 * @dst: destination register (64 bit wide)
176 * @sym: name of the symbol
Ard Biesheuvelb784a5d2015-03-04 19:45:38 +0100177 */
Ard Biesheuvel41c066f2017-01-11 14:54:53 +0000178 .macro adr_l, dst, sym
179#ifndef MODULE
Ard Biesheuvelb784a5d2015-03-04 19:45:38 +0100180 adrp \dst, \sym
181 add \dst, \dst, :lo12:\sym
Ard Biesheuvel41c066f2017-01-11 14:54:53 +0000182#else
183 movz \dst, #:abs_g3:\sym
184 movk \dst, #:abs_g2_nc:\sym
185 movk \dst, #:abs_g1_nc:\sym
186 movk \dst, #:abs_g0_nc:\sym
187#endif
Ard Biesheuvelb784a5d2015-03-04 19:45:38 +0100188 .endm
189
190 /*
191 * @dst: destination register (32 or 64 bit wide)
192 * @sym: name of the symbol
193 * @tmp: optional 64-bit scratch register to be used if <dst> is a
194 * 32-bit wide register, in which case it cannot be used to hold
195 * the address
196 */
197 .macro ldr_l, dst, sym, tmp=
Ard Biesheuvel41c066f2017-01-11 14:54:53 +0000198#ifndef MODULE
Ard Biesheuvelb784a5d2015-03-04 19:45:38 +0100199 .ifb \tmp
200 adrp \dst, \sym
201 ldr \dst, [\dst, :lo12:\sym]
202 .else
203 adrp \tmp, \sym
204 ldr \dst, [\tmp, :lo12:\sym]
205 .endif
Ard Biesheuvel41c066f2017-01-11 14:54:53 +0000206#else
207 .ifb \tmp
208 adr_l \dst, \sym
209 ldr \dst, [\dst]
210 .else
211 adr_l \tmp, \sym
212 ldr \dst, [\tmp]
213 .endif
214#endif
Ard Biesheuvelb784a5d2015-03-04 19:45:38 +0100215 .endm
216
217 /*
218 * @src: source register (32 or 64 bit wide)
219 * @sym: name of the symbol
220 * @tmp: mandatory 64-bit scratch register to calculate the address
221 * while <src> needs to be preserved.
222 */
223 .macro str_l, src, sym, tmp
Ard Biesheuvel41c066f2017-01-11 14:54:53 +0000224#ifndef MODULE
Ard Biesheuvelb784a5d2015-03-04 19:45:38 +0100225 adrp \tmp, \sym
226 str \src, [\tmp, :lo12:\sym]
Ard Biesheuvel41c066f2017-01-11 14:54:53 +0000227#else
228 adr_l \tmp, \sym
229 str \src, [\tmp]
230#endif
Ard Biesheuvelb784a5d2015-03-04 19:45:38 +0100231 .endm
232
James Morseaa4d5d32015-12-10 10:22:39 +0000233 /*
Ard Biesheuvel8ea41b12017-07-15 17:23:13 +0100234 * @dst: Result of per_cpu(sym, smp_processor_id()), can be SP for
235 * non-module code
James Morseaa4d5d32015-12-10 10:22:39 +0000236 * @sym: The name of the per-cpu variable
James Morseaa4d5d32015-12-10 10:22:39 +0000237 * @tmp: scratch register
238 */
Mark Rutland1b7e2292016-11-03 20:23:12 +0000239 .macro adr_this_cpu, dst, sym, tmp
Ard Biesheuvel8ea41b12017-07-15 17:23:13 +0100240#ifndef MODULE
241 adrp \tmp, \sym
242 add \dst, \tmp, #:lo12:\sym
243#else
Mark Rutland1b7e2292016-11-03 20:23:12 +0000244 adr_l \dst, \sym
Ard Biesheuvel8ea41b12017-07-15 17:23:13 +0100245#endif
James Morseaa4d5d32015-12-10 10:22:39 +0000246 mrs \tmp, tpidr_el1
Mark Rutland1b7e2292016-11-03 20:23:12 +0000247 add \dst, \dst, \tmp
248 .endm
249
250 /*
251 * @dst: Result of READ_ONCE(per_cpu(sym, smp_processor_id()))
252 * @sym: The name of the per-cpu variable
253 * @tmp: scratch register
254 */
255 .macro ldr_this_cpu dst, sym, tmp
256 adr_l \dst, \sym
257 mrs \tmp, tpidr_el1
258 ldr \dst, [\dst, \tmp]
James Morseaa4d5d32015-12-10 10:22:39 +0000259 .endm
260
Ard Biesheuvel20791842015-10-08 20:02:03 +0100261/*
Geoff Levand7b7293a2016-04-27 17:47:00 +0100262 * vma_vm_mm - get mm pointer from vma pointer (vma->vm_mm)
263 */
264 .macro vma_vm_mm, rd, rn
265 ldr \rd, [\rn, #VMA_VM_MM]
266 .endm
267
268/*
269 * mmid - get context id from mm pointer (mm->context.id)
270 */
271 .macro mmid, rd, rn
272 ldr \rd, [\rn, #MM_CONTEXT_ID]
273 .endm
Suzuki K Poulose116c81f2016-09-09 14:07:16 +0100274/*
275 * read_ctr - read CTR_EL0. If the system has mismatched
276 * cache line sizes, provide the system wide safe value
277 * from arm64_ftr_reg_ctrel0.sys_val
278 */
279 .macro read_ctr, reg
280alternative_if_not ARM64_MISMATCHED_CACHE_LINE_SIZE
281 mrs \reg, ctr_el0 // read CTR
282 nop
283alternative_else
284 ldr_l \reg, arm64_ftr_reg_ctrel0 + ARM64_FTR_SYSVAL
285alternative_endif
286 .endm
287
Geoff Levand7b7293a2016-04-27 17:47:00 +0100288
289/*
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100290 * raw_dcache_line_size - get the minimum D-cache line size on this CPU
291 * from the CTR register.
Geoff Levand7b7293a2016-04-27 17:47:00 +0100292 */
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100293 .macro raw_dcache_line_size, reg, tmp
Geoff Levand7b7293a2016-04-27 17:47:00 +0100294 mrs \tmp, ctr_el0 // read CTR
295 ubfm \tmp, \tmp, #16, #19 // cache line size encoding
296 mov \reg, #4 // bytes per word
297 lsl \reg, \reg, \tmp // actual cache line size
298 .endm
299
300/*
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100301 * dcache_line_size - get the safe D-cache line size across all CPUs
Geoff Levand7b7293a2016-04-27 17:47:00 +0100302 */
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100303 .macro dcache_line_size, reg, tmp
Suzuki K Poulose116c81f2016-09-09 14:07:16 +0100304 read_ctr \tmp
305 ubfm \tmp, \tmp, #16, #19 // cache line size encoding
306 mov \reg, #4 // bytes per word
307 lsl \reg, \reg, \tmp // actual cache line size
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100308 .endm
309
310/*
311 * raw_icache_line_size - get the minimum I-cache line size on this CPU
312 * from the CTR register.
313 */
314 .macro raw_icache_line_size, reg, tmp
Geoff Levand7b7293a2016-04-27 17:47:00 +0100315 mrs \tmp, ctr_el0 // read CTR
316 and \tmp, \tmp, #0xf // cache line size encoding
317 mov \reg, #4 // bytes per word
318 lsl \reg, \reg, \tmp // actual cache line size
319 .endm
320
321/*
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100322 * icache_line_size - get the safe I-cache line size across all CPUs
323 */
324 .macro icache_line_size, reg, tmp
Suzuki K Poulose116c81f2016-09-09 14:07:16 +0100325 read_ctr \tmp
326 and \tmp, \tmp, #0xf // cache line size encoding
327 mov \reg, #4 // bytes per word
328 lsl \reg, \reg, \tmp // actual cache line size
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100329 .endm
330
331/*
Geoff Levand7b7293a2016-04-27 17:47:00 +0100332 * tcr_set_idmap_t0sz - update TCR.T0SZ so that we can load the ID map
333 */
334 .macro tcr_set_idmap_t0sz, valreg, tmpreg
335#ifndef CONFIG_ARM64_VA_BITS_48
336 ldr_l \tmpreg, idmap_t0sz
337 bfi \valreg, \tmpreg, #TCR_T0SZ_OFFSET, #TCR_TxSZ_WIDTH
338#endif
339 .endm
340
341/*
342 * Macro to perform a data cache maintenance for the interval
343 * [kaddr, kaddr + size)
344 *
345 * op: operation passed to dc instruction
346 * domain: domain used in dsb instruciton
347 * kaddr: starting virtual address of the region
348 * size: size of the region
349 * Corrupts: kaddr, size, tmp1, tmp2
350 */
351 .macro dcache_by_line_op op, domain, kaddr, size, tmp1, tmp2
352 dcache_line_size \tmp1, \tmp2
353 add \size, \kaddr, \size
354 sub \tmp2, \tmp1, #1
355 bic \kaddr, \kaddr, \tmp2
Andre Przywara823066d2016-06-28 18:07:29 +01003569998:
357 .if (\op == cvau || \op == cvac)
358alternative_if_not ARM64_WORKAROUND_CLEAN_CACHE
359 dc \op, \kaddr
360alternative_else
361 dc civac, \kaddr
362alternative_endif
Robin Murphyd50e0712017-07-25 11:55:42 +0100363 .elseif (\op == cvap)
364alternative_if ARM64_HAS_DCPOP
365 sys 3, c7, c12, 1, \kaddr // dc cvap
366alternative_else
367 dc cvac, \kaddr
368alternative_endif
Andre Przywara823066d2016-06-28 18:07:29 +0100369 .else
370 dc \op, \kaddr
371 .endif
Geoff Levand7b7293a2016-04-27 17:47:00 +0100372 add \kaddr, \kaddr, \tmp1
373 cmp \kaddr, \size
374 b.lo 9998b
375 dsb \domain
376 .endm
377
378/*
379 * reset_pmuserenr_el0 - reset PMUSERENR_EL0 if PMUv3 present
380 */
381 .macro reset_pmuserenr_el0, tmpreg
382 mrs \tmpreg, id_aa64dfr0_el1 // Check ID_AA64DFR0_EL1 PMUVer
383 sbfx \tmpreg, \tmpreg, #8, #4
384 cmp \tmpreg, #1 // Skip if no PMU present
385 b.lt 9000f
386 msr pmuserenr_el0, xzr // Disable PMU access from EL0
3879000:
388 .endm
389
390/*
Geoff Levand5003dbd2016-04-27 17:47:10 +0100391 * copy_page - copy src to dest using temp registers t1-t8
392 */
393 .macro copy_page dest:req src:req t1:req t2:req t3:req t4:req t5:req t6:req t7:req t8:req
3949998: ldp \t1, \t2, [\src]
395 ldp \t3, \t4, [\src, #16]
396 ldp \t5, \t6, [\src, #32]
397 ldp \t7, \t8, [\src, #48]
398 add \src, \src, #64
399 stnp \t1, \t2, [\dest]
400 stnp \t3, \t4, [\dest, #16]
401 stnp \t5, \t6, [\dest, #32]
402 stnp \t7, \t8, [\dest, #48]
403 add \dest, \dest, #64
404 tst \src, #(PAGE_SIZE - 1)
405 b.ne 9998b
406 .endm
407
408/*
Ard Biesheuvel20791842015-10-08 20:02:03 +0100409 * Annotate a function as position independent, i.e., safe to be called before
410 * the kernel virtual mapping is activated.
411 */
412#define ENDPIPROC(x) \
413 .globl __pi_##x; \
414 .type __pi_##x, %function; \
415 .set __pi_##x, x; \
416 .size __pi_##x, . - x; \
417 ENDPROC(x)
418
Mark Rutlanded84b4e2017-07-26 16:05:20 +0100419/*
420 * Annotate a function as being unsuitable for kprobes.
421 */
422#ifdef CONFIG_KPROBES
423#define NOKPROBE(x) \
424 .pushsection "_kprobe_blacklist", "aw"; \
425 .quad x; \
426 .popsection;
427#else
428#define NOKPROBE(x)
429#endif
Ard Biesheuvel6ad1fe52015-12-26 13:48:02 +0100430 /*
431 * Emit a 64-bit absolute little endian symbol reference in a way that
432 * ensures that it will be resolved at build time, even when building a
433 * PIE binary. This requires cooperation from the linker script, which
434 * must emit the lo32/hi32 halves individually.
435 */
436 .macro le64sym, sym
437 .long \sym\()_lo32
438 .long \sym\()_hi32
439 .endm
440
Ard Biesheuvel30b5ba52016-04-18 17:09:44 +0200441 /*
442 * mov_q - move an immediate constant into a 64-bit register using
443 * between 2 and 4 movz/movk instructions (depending on the
444 * magnitude and sign of the operand)
445 */
446 .macro mov_q, reg, val
447 .if (((\val) >> 31) == 0 || ((\val) >> 31) == 0x1ffffffff)
448 movz \reg, :abs_g1_s:\val
449 .else
450 .if (((\val) >> 47) == 0 || ((\val) >> 47) == 0x1ffff)
451 movz \reg, :abs_g2_s:\val
452 .else
453 movz \reg, :abs_g3:\val
454 movk \reg, :abs_g2_nc:\val
455 .endif
456 movk \reg, :abs_g1_nc:\val
457 .endif
458 movk \reg, :abs_g0_nc:\val
459 .endm
460
Catalin Marinasf33bcf02016-07-01 15:48:55 +0100461/*
Catalin Marinas4b65a5d2016-07-01 16:53:00 +0100462 * Return the current thread_info.
463 */
464 .macro get_thread_info, rd
465 mrs \rd, sp_el0
466 .endm
467
468/*
Christopher Covington38fd94b2017-02-08 15:08:37 -0500469 * Errata workaround prior to TTBR0_EL1 update
470 *
471 * val: TTBR value with new BADDR, preserved
472 * tmp0: temporary register, clobbered
473 * tmp1: other temporary register, clobbered
474 */
475 .macro pre_ttbr0_update_workaround, val, tmp0, tmp1
476#ifdef CONFIG_QCOM_FALKOR_ERRATUM_1003
477alternative_if ARM64_WORKAROUND_QCOM_FALKOR_E1003
478 mrs \tmp0, ttbr0_el1
479 mov \tmp1, #FALKOR_RESERVED_ASID
480 bfi \tmp0, \tmp1, #48, #16 // reserved ASID + old BADDR
481 msr ttbr0_el1, \tmp0
482 isb
483 bfi \tmp0, \val, #0, #48 // reserved ASID + new BADDR
484 msr ttbr0_el1, \tmp0
485 isb
486alternative_else_nop_endif
487#endif
488 .endm
489
490/*
Catalin Marinasf33bcf02016-07-01 15:48:55 +0100491 * Errata workaround post TTBR0_EL1 update.
492 */
493 .macro post_ttbr0_update_workaround
494#ifdef CONFIG_CAVIUM_ERRATUM_27456
495alternative_if ARM64_WORKAROUND_CAVIUM_27456
496 ic iallu
497 dsb nsh
498 isb
499alternative_else_nop_endif
500#endif
501 .endm
502
Marc Zyngierf3e39272015-02-20 13:53:13 +0000503#endif /* __ASM_ASSEMBLER_H */