blob: 8bc63fc98752992a9571b14dbe1116241ebc3726 [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 .macro enable_dbg
73 msr daifclr, #8
74 .endm
75
Will Deacon2a283072014-04-29 19:04:06 +010076 .macro disable_step_tsk, flgs, tmp
77 tbz \flgs, #TIF_SINGLESTEP, 9990f
Catalin Marinas0be73202012-03-05 11:49:26 +000078 mrs \tmp, mdscr_el1
Julien Thierrye28cc022017-10-25 10:04:32 +010079 bic \tmp, \tmp, #DBG_MDSCR_SS
Catalin Marinas0be73202012-03-05 11:49:26 +000080 msr mdscr_el1, \tmp
Will Deacon2a283072014-04-29 19:04:06 +010081 isb // Synchronise with enable_dbg
829990:
Catalin Marinas0be73202012-03-05 11:49:26 +000083 .endm
84
James Morse84d0fb12017-11-02 12:12:38 +000085 /* call with daif masked */
Will Deacon2a283072014-04-29 19:04:06 +010086 .macro enable_step_tsk, flgs, tmp
87 tbz \flgs, #TIF_SINGLESTEP, 9990f
Catalin Marinas0be73202012-03-05 11:49:26 +000088 mrs \tmp, mdscr_el1
Julien Thierrye28cc022017-10-25 10:04:32 +010089 orr \tmp, \tmp, #DBG_MDSCR_SS
Catalin Marinas0be73202012-03-05 11:49:26 +000090 msr mdscr_el1, \tmp
Will Deacon2a283072014-04-29 19:04:06 +0100919990:
Catalin Marinas0be73202012-03-05 11:49:26 +000092 .endm
93
Will Deacon2a283072014-04-29 19:04:06 +010094/*
95 * Enable both debug exceptions and interrupts. This is likely to be
96 * faster than two daifclr operations, since writes to this register
97 * are self-synchronising.
98 */
99 .macro enable_dbg_and_irq
100 msr daifclr, #(8 | 2)
Catalin Marinas0be73202012-03-05 11:49:26 +0000101 .endm
102
103/*
104 * SMP data memory barrier
105 */
106 .macro smp_dmb, opt
Catalin Marinas0be73202012-03-05 11:49:26 +0000107 dmb \opt
Catalin Marinas0be73202012-03-05 11:49:26 +0000108 .endm
109
Ard Biesheuvel6c94f272016-01-01 15:02:12 +0100110/*
Will Deaconf99a2502016-09-06 16:40:23 +0100111 * NOP sequence
112 */
113 .macro nops, num
114 .rept \num
115 nop
116 .endr
117 .endm
118
119/*
Ard Biesheuvel6c94f272016-01-01 15:02:12 +0100120 * Emit an entry into the exception table
121 */
122 .macro _asm_extable, from, to
123 .pushsection __ex_table, "a"
124 .align 3
125 .long (\from - .), (\to - .)
126 .popsection
127 .endm
128
Catalin Marinas0be73202012-03-05 11:49:26 +0000129#define USER(l, x...) \
1309999: x; \
Ard Biesheuvel6c94f272016-01-01 15:02:12 +0100131 _asm_extable 9999b, l
Catalin Marinas0be73202012-03-05 11:49:26 +0000132
133/*
134 * Register aliases.
135 */
136lr .req x30 // link register
Marc Zyngierdc637f12012-10-19 17:37:35 +0100137
138/*
139 * Vector entry
140 */
141 .macro ventry label
142 .align 7
143 b \label
144 .endm
Matthew Leache68beda2013-10-11 14:52:15 +0100145
146/*
147 * Select code when configured for BE.
148 */
149#ifdef CONFIG_CPU_BIG_ENDIAN
150#define CPU_BE(code...) code
151#else
152#define CPU_BE(code...)
153#endif
154
155/*
156 * Select code when configured for LE.
157 */
158#ifdef CONFIG_CPU_BIG_ENDIAN
159#define CPU_LE(code...)
160#else
161#define CPU_LE(code...) code
162#endif
163
Matthew Leach55b89542013-10-11 14:52:13 +0100164/*
165 * Define a macro that constructs a 64-bit value by concatenating two
166 * 32-bit registers. Note that on big endian systems the order of the
167 * registers is swapped.
168 */
169#ifndef CONFIG_CPU_BIG_ENDIAN
170 .macro regs_to_64, rd, lbits, hbits
171#else
172 .macro regs_to_64, rd, hbits, lbits
173#endif
174 orr \rd, \lbits, \hbits, lsl #32
175 .endm
Marc Zyngierf3e39272015-02-20 13:53:13 +0000176
Ard Biesheuvelb784a5d2015-03-04 19:45:38 +0100177/*
178 * Pseudo-ops for PC-relative adr/ldr/str <reg>, <symbol> where
Ard Biesheuvel41c066f2017-01-11 14:54:53 +0000179 * <symbol> is within the range +/- 4 GB of the PC when running
180 * in core kernel context. In module context, a movz/movk sequence
181 * is used, since modules may be loaded far away from the kernel
182 * when KASLR is in effect.
Ard Biesheuvelb784a5d2015-03-04 19:45:38 +0100183 */
184 /*
185 * @dst: destination register (64 bit wide)
186 * @sym: name of the symbol
Ard Biesheuvelb784a5d2015-03-04 19:45:38 +0100187 */
Ard Biesheuvel41c066f2017-01-11 14:54:53 +0000188 .macro adr_l, dst, sym
189#ifndef MODULE
Ard Biesheuvelb784a5d2015-03-04 19:45:38 +0100190 adrp \dst, \sym
191 add \dst, \dst, :lo12:\sym
Ard Biesheuvel41c066f2017-01-11 14:54:53 +0000192#else
193 movz \dst, #:abs_g3:\sym
194 movk \dst, #:abs_g2_nc:\sym
195 movk \dst, #:abs_g1_nc:\sym
196 movk \dst, #:abs_g0_nc:\sym
197#endif
Ard Biesheuvelb784a5d2015-03-04 19:45:38 +0100198 .endm
199
200 /*
201 * @dst: destination register (32 or 64 bit wide)
202 * @sym: name of the symbol
203 * @tmp: optional 64-bit scratch register to be used if <dst> is a
204 * 32-bit wide register, in which case it cannot be used to hold
205 * the address
206 */
207 .macro ldr_l, dst, sym, tmp=
Ard Biesheuvel41c066f2017-01-11 14:54:53 +0000208#ifndef MODULE
Ard Biesheuvelb784a5d2015-03-04 19:45:38 +0100209 .ifb \tmp
210 adrp \dst, \sym
211 ldr \dst, [\dst, :lo12:\sym]
212 .else
213 adrp \tmp, \sym
214 ldr \dst, [\tmp, :lo12:\sym]
215 .endif
Ard Biesheuvel41c066f2017-01-11 14:54:53 +0000216#else
217 .ifb \tmp
218 adr_l \dst, \sym
219 ldr \dst, [\dst]
220 .else
221 adr_l \tmp, \sym
222 ldr \dst, [\tmp]
223 .endif
224#endif
Ard Biesheuvelb784a5d2015-03-04 19:45:38 +0100225 .endm
226
227 /*
228 * @src: source register (32 or 64 bit wide)
229 * @sym: name of the symbol
230 * @tmp: mandatory 64-bit scratch register to calculate the address
231 * while <src> needs to be preserved.
232 */
233 .macro str_l, src, sym, tmp
Ard Biesheuvel41c066f2017-01-11 14:54:53 +0000234#ifndef MODULE
Ard Biesheuvelb784a5d2015-03-04 19:45:38 +0100235 adrp \tmp, \sym
236 str \src, [\tmp, :lo12:\sym]
Ard Biesheuvel41c066f2017-01-11 14:54:53 +0000237#else
238 adr_l \tmp, \sym
239 str \src, [\tmp]
240#endif
Ard Biesheuvelb784a5d2015-03-04 19:45:38 +0100241 .endm
242
James Morseaa4d5d32015-12-10 10:22:39 +0000243 /*
Ard Biesheuvel8ea41b12017-07-15 17:23:13 +0100244 * @dst: Result of per_cpu(sym, smp_processor_id()), can be SP for
245 * non-module code
James Morseaa4d5d32015-12-10 10:22:39 +0000246 * @sym: The name of the per-cpu variable
James Morseaa4d5d32015-12-10 10:22:39 +0000247 * @tmp: scratch register
248 */
Mark Rutland1b7e2292016-11-03 20:23:12 +0000249 .macro adr_this_cpu, dst, sym, tmp
Ard Biesheuvel8ea41b12017-07-15 17:23:13 +0100250#ifndef MODULE
251 adrp \tmp, \sym
252 add \dst, \tmp, #:lo12:\sym
253#else
Mark Rutland1b7e2292016-11-03 20:23:12 +0000254 adr_l \dst, \sym
Ard Biesheuvel8ea41b12017-07-15 17:23:13 +0100255#endif
James Morseaa4d5d32015-12-10 10:22:39 +0000256 mrs \tmp, tpidr_el1
Mark Rutland1b7e2292016-11-03 20:23:12 +0000257 add \dst, \dst, \tmp
258 .endm
259
260 /*
261 * @dst: Result of READ_ONCE(per_cpu(sym, smp_processor_id()))
262 * @sym: The name of the per-cpu variable
263 * @tmp: scratch register
264 */
265 .macro ldr_this_cpu dst, sym, tmp
266 adr_l \dst, \sym
267 mrs \tmp, tpidr_el1
268 ldr \dst, [\dst, \tmp]
James Morseaa4d5d32015-12-10 10:22:39 +0000269 .endm
270
Ard Biesheuvel20791842015-10-08 20:02:03 +0100271/*
Geoff Levand7b7293a2016-04-27 17:47:00 +0100272 * vma_vm_mm - get mm pointer from vma pointer (vma->vm_mm)
273 */
274 .macro vma_vm_mm, rd, rn
275 ldr \rd, [\rn, #VMA_VM_MM]
276 .endm
277
278/*
279 * mmid - get context id from mm pointer (mm->context.id)
280 */
281 .macro mmid, rd, rn
282 ldr \rd, [\rn, #MM_CONTEXT_ID]
283 .endm
Suzuki K Poulose116c81f2016-09-09 14:07:16 +0100284/*
285 * read_ctr - read CTR_EL0. If the system has mismatched
286 * cache line sizes, provide the system wide safe value
287 * from arm64_ftr_reg_ctrel0.sys_val
288 */
289 .macro read_ctr, reg
290alternative_if_not ARM64_MISMATCHED_CACHE_LINE_SIZE
291 mrs \reg, ctr_el0 // read CTR
292 nop
293alternative_else
294 ldr_l \reg, arm64_ftr_reg_ctrel0 + ARM64_FTR_SYSVAL
295alternative_endif
296 .endm
297
Geoff Levand7b7293a2016-04-27 17:47:00 +0100298
299/*
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100300 * raw_dcache_line_size - get the minimum D-cache line size on this CPU
301 * from the CTR register.
Geoff Levand7b7293a2016-04-27 17:47:00 +0100302 */
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100303 .macro raw_dcache_line_size, reg, tmp
Geoff Levand7b7293a2016-04-27 17:47:00 +0100304 mrs \tmp, ctr_el0 // read CTR
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
308 .endm
309
310/*
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100311 * dcache_line_size - get the safe D-cache line size across all CPUs
Geoff Levand7b7293a2016-04-27 17:47:00 +0100312 */
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100313 .macro dcache_line_size, reg, tmp
Suzuki K Poulose116c81f2016-09-09 14:07:16 +0100314 read_ctr \tmp
315 ubfm \tmp, \tmp, #16, #19 // cache line size encoding
316 mov \reg, #4 // bytes per word
317 lsl \reg, \reg, \tmp // actual cache line size
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100318 .endm
319
320/*
321 * raw_icache_line_size - get the minimum I-cache line size on this CPU
322 * from the CTR register.
323 */
324 .macro raw_icache_line_size, reg, tmp
Geoff Levand7b7293a2016-04-27 17:47:00 +0100325 mrs \tmp, ctr_el0 // read CTR
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
329 .endm
330
331/*
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100332 * icache_line_size - get the safe I-cache line size across all CPUs
333 */
334 .macro icache_line_size, reg, tmp
Suzuki K Poulose116c81f2016-09-09 14:07:16 +0100335 read_ctr \tmp
336 and \tmp, \tmp, #0xf // cache line size encoding
337 mov \reg, #4 // bytes per word
338 lsl \reg, \reg, \tmp // actual cache line size
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100339 .endm
340
341/*
Geoff Levand7b7293a2016-04-27 17:47:00 +0100342 * tcr_set_idmap_t0sz - update TCR.T0SZ so that we can load the ID map
343 */
344 .macro tcr_set_idmap_t0sz, valreg, tmpreg
345#ifndef CONFIG_ARM64_VA_BITS_48
346 ldr_l \tmpreg, idmap_t0sz
347 bfi \valreg, \tmpreg, #TCR_T0SZ_OFFSET, #TCR_TxSZ_WIDTH
348#endif
349 .endm
350
351/*
352 * Macro to perform a data cache maintenance for the interval
353 * [kaddr, kaddr + size)
354 *
355 * op: operation passed to dc instruction
356 * domain: domain used in dsb instruciton
357 * kaddr: starting virtual address of the region
358 * size: size of the region
359 * Corrupts: kaddr, size, tmp1, tmp2
360 */
361 .macro dcache_by_line_op op, domain, kaddr, size, tmp1, tmp2
362 dcache_line_size \tmp1, \tmp2
363 add \size, \kaddr, \size
364 sub \tmp2, \tmp1, #1
365 bic \kaddr, \kaddr, \tmp2
Andre Przywara823066d2016-06-28 18:07:29 +01003669998:
367 .if (\op == cvau || \op == cvac)
368alternative_if_not ARM64_WORKAROUND_CLEAN_CACHE
369 dc \op, \kaddr
370alternative_else
371 dc civac, \kaddr
372alternative_endif
Robin Murphyd50e0712017-07-25 11:55:42 +0100373 .elseif (\op == cvap)
374alternative_if ARM64_HAS_DCPOP
375 sys 3, c7, c12, 1, \kaddr // dc cvap
376alternative_else
377 dc cvac, \kaddr
378alternative_endif
Andre Przywara823066d2016-06-28 18:07:29 +0100379 .else
380 dc \op, \kaddr
381 .endif
Geoff Levand7b7293a2016-04-27 17:47:00 +0100382 add \kaddr, \kaddr, \tmp1
383 cmp \kaddr, \size
384 b.lo 9998b
385 dsb \domain
386 .endm
387
388/*
389 * reset_pmuserenr_el0 - reset PMUSERENR_EL0 if PMUv3 present
390 */
391 .macro reset_pmuserenr_el0, tmpreg
392 mrs \tmpreg, id_aa64dfr0_el1 // Check ID_AA64DFR0_EL1 PMUVer
393 sbfx \tmpreg, \tmpreg, #8, #4
394 cmp \tmpreg, #1 // Skip if no PMU present
395 b.lt 9000f
396 msr pmuserenr_el0, xzr // Disable PMU access from EL0
3979000:
398 .endm
399
400/*
Geoff Levand5003dbd2016-04-27 17:47:10 +0100401 * copy_page - copy src to dest using temp registers t1-t8
402 */
403 .macro copy_page dest:req src:req t1:req t2:req t3:req t4:req t5:req t6:req t7:req t8:req
4049998: ldp \t1, \t2, [\src]
405 ldp \t3, \t4, [\src, #16]
406 ldp \t5, \t6, [\src, #32]
407 ldp \t7, \t8, [\src, #48]
408 add \src, \src, #64
409 stnp \t1, \t2, [\dest]
410 stnp \t3, \t4, [\dest, #16]
411 stnp \t5, \t6, [\dest, #32]
412 stnp \t7, \t8, [\dest, #48]
413 add \dest, \dest, #64
414 tst \src, #(PAGE_SIZE - 1)
415 b.ne 9998b
416 .endm
417
418/*
Ard Biesheuvel20791842015-10-08 20:02:03 +0100419 * Annotate a function as position independent, i.e., safe to be called before
420 * the kernel virtual mapping is activated.
421 */
422#define ENDPIPROC(x) \
423 .globl __pi_##x; \
424 .type __pi_##x, %function; \
425 .set __pi_##x, x; \
426 .size __pi_##x, . - x; \
427 ENDPROC(x)
428
Mark Rutlanded84b4e2017-07-26 16:05:20 +0100429/*
430 * Annotate a function as being unsuitable for kprobes.
431 */
432#ifdef CONFIG_KPROBES
433#define NOKPROBE(x) \
434 .pushsection "_kprobe_blacklist", "aw"; \
435 .quad x; \
436 .popsection;
437#else
438#define NOKPROBE(x)
439#endif
Ard Biesheuvel6ad1fe52015-12-26 13:48:02 +0100440 /*
441 * Emit a 64-bit absolute little endian symbol reference in a way that
442 * ensures that it will be resolved at build time, even when building a
443 * PIE binary. This requires cooperation from the linker script, which
444 * must emit the lo32/hi32 halves individually.
445 */
446 .macro le64sym, sym
447 .long \sym\()_lo32
448 .long \sym\()_hi32
449 .endm
450
Ard Biesheuvel30b5ba52016-04-18 17:09:44 +0200451 /*
452 * mov_q - move an immediate constant into a 64-bit register using
453 * between 2 and 4 movz/movk instructions (depending on the
454 * magnitude and sign of the operand)
455 */
456 .macro mov_q, reg, val
457 .if (((\val) >> 31) == 0 || ((\val) >> 31) == 0x1ffffffff)
458 movz \reg, :abs_g1_s:\val
459 .else
460 .if (((\val) >> 47) == 0 || ((\val) >> 47) == 0x1ffff)
461 movz \reg, :abs_g2_s:\val
462 .else
463 movz \reg, :abs_g3:\val
464 movk \reg, :abs_g2_nc:\val
465 .endif
466 movk \reg, :abs_g1_nc:\val
467 .endif
468 movk \reg, :abs_g0_nc:\val
469 .endm
470
Catalin Marinasf33bcf02016-07-01 15:48:55 +0100471/*
Catalin Marinas4b65a5d2016-07-01 16:53:00 +0100472 * Return the current thread_info.
473 */
474 .macro get_thread_info, rd
475 mrs \rd, sp_el0
476 .endm
477
478/*
Christopher Covington38fd94b2017-02-08 15:08:37 -0500479 * Errata workaround prior to TTBR0_EL1 update
480 *
481 * val: TTBR value with new BADDR, preserved
482 * tmp0: temporary register, clobbered
483 * tmp1: other temporary register, clobbered
484 */
485 .macro pre_ttbr0_update_workaround, val, tmp0, tmp1
486#ifdef CONFIG_QCOM_FALKOR_ERRATUM_1003
487alternative_if ARM64_WORKAROUND_QCOM_FALKOR_E1003
488 mrs \tmp0, ttbr0_el1
489 mov \tmp1, #FALKOR_RESERVED_ASID
490 bfi \tmp0, \tmp1, #48, #16 // reserved ASID + old BADDR
491 msr ttbr0_el1, \tmp0
492 isb
493 bfi \tmp0, \val, #0, #48 // reserved ASID + new BADDR
494 msr ttbr0_el1, \tmp0
495 isb
496alternative_else_nop_endif
497#endif
498 .endm
499
500/*
Catalin Marinasf33bcf02016-07-01 15:48:55 +0100501 * Errata workaround post TTBR0_EL1 update.
502 */
503 .macro post_ttbr0_update_workaround
504#ifdef CONFIG_CAVIUM_ERRATUM_27456
505alternative_if ARM64_WORKAROUND_CAVIUM_27456
506 ic iallu
507 dsb nsh
508 isb
509alternative_else_nop_endif
510#endif
511 .endm
512
Marc Zyngierf3e39272015-02-20 13:53:13 +0000513#endif /* __ASM_ASSEMBLER_H */