1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Based on arch/arm/include/asm/assembler.h, arch/arm/mm/proc-macros.S
5 * Copyright (C) 1996-2000 Russell King
6 * Copyright (C) 2012 ARM Ltd.
9 #error "Only include this from assembly code"
12 #ifndef __ASM_ASSEMBLER_H
13 #define __ASM_ASSEMBLER_H
15 #include <asm-generic/export.h>
17 #include <asm/asm-offsets.h>
18 #include <asm/cpufeature.h>
19 #include <asm/cputype.h>
20 #include <asm/debug-monitors.h>
22 #include <asm/pgtable-hwdef.h>
23 #include <asm/ptrace.h>
24 #include <asm/thread_info.h>
26 .macro save_and_disable_daif
, flags
39 .macro restore_daif
, flags
:req
43 /* Only on aarch64 pstate, PSR_D_BIT is different for aarch32 */
44 .macro inherit_daif
, pstate
:req
, tmp
:req
45 and \tmp
, \pstate
, #(PSR_D_BIT | PSR_A_BIT | PSR_I_BIT | PSR_F_BIT)
49 /* IRQ is the lowest priority flag, unconditionally unmask the rest. */
51 msr daifclr
, #(8 | 4 | 1)
55 * Save/restore interrupts.
57 .macro save_and_disable_irq
, flags
62 .macro restore_irq
, flags
70 .macro disable_step_tsk
, flgs
, tmp
71 tbz
\flgs
, #TIF_SINGLESTEP, 9990f
73 bic
\tmp
, \tmp
, #DBG_MDSCR_SS
75 isb
// Synchronise with enable_dbg
79 /* call with daif masked */
80 .macro enable_step_tsk
, flgs
, tmp
81 tbz
\flgs
, #TIF_SINGLESTEP, 9990f
83 orr
\tmp
, \tmp
, #DBG_MDSCR_SS
89 * SMP data memory barrier
96 * RAS Error Synchronization barrier
103 * Value prediction barrier
110 * Speculation barrier
113 alternative_if_not ARM64_HAS_SB
123 * Sanitise a 64-bit bounded index wrt speculation, returning zero if out
126 .macro mask_nospec64
, idx
, limit
, tmp
127 sub
\tmp
, \idx
, \limit
129 and \idx
, \idx
, \tmp
, asr
#63
143 * Emit an entry into the exception table
145 .macro _asm_extable
, from
, to
146 .pushsection __ex_table
, "a"
148 .long (\from
- .), (\to
- .)
152 #define USER(l, x...) \
154 _asm_extable 9999b, l
159 lr
.req x30
// link register
170 * Select code when configured for BE.
172 #ifdef CONFIG_CPU_BIG_ENDIAN
173 #define CPU_BE(code...) code
175 #define CPU_BE(code...)
179 * Select code when configured for LE.
181 #ifdef CONFIG_CPU_BIG_ENDIAN
182 #define CPU_LE(code...)
184 #define CPU_LE(code...) code
188 * Define a macro that constructs a 64-bit value by concatenating two
189 * 32-bit registers. Note that on big endian systems the order of the
190 * registers is swapped.
192 #ifndef CONFIG_CPU_BIG_ENDIAN
193 .macro regs_to_64
, rd
, lbits
, hbits
195 .macro regs_to_64
, rd
, hbits
, lbits
197 orr
\rd
, \lbits
, \hbits
, lsl
#32
201 * Pseudo-ops for PC-relative adr/ldr/str <reg>, <symbol> where
202 * <symbol> is within the range +/- 4 GB of the PC.
205 * @dst: destination register (64 bit wide)
206 * @sym: name of the symbol
208 .macro adr_l
, dst
, sym
210 add \dst
, \dst
, :lo12
:\sym
214 * @dst: destination register (32 or 64 bit wide)
215 * @sym: name of the symbol
216 * @tmp: optional 64-bit scratch register to be used if <dst> is a
217 * 32-bit wide register, in which case it cannot be used to hold
220 .macro ldr_l
, dst
, sym
, tmp
=
223 ldr \dst
, [\dst
, :lo12
:\sym
]
226 ldr \dst
, [\tmp
, :lo12
:\sym
]
231 * @src: source register (32 or 64 bit wide)
232 * @sym: name of the symbol
233 * @tmp: mandatory 64-bit scratch register to calculate the address
234 * while <src> needs to be preserved.
236 .macro str_l
, src
, sym
, tmp
238 str \src
, [\tmp
, :lo12
:\sym
]
242 * @dst: Result of per_cpu(sym, smp_processor_id()) (can be SP)
243 * @sym: The name of the per-cpu variable
244 * @tmp: scratch register
246 .macro adr_this_cpu
, dst
, sym
, tmp
248 add \dst
, \tmp
, #:lo12:\sym
249 alternative_if_not ARM64_HAS_VIRT_HOST_EXTN
258 * @dst: Result of READ_ONCE(per_cpu(sym, smp_processor_id()))
259 * @sym: The name of the per-cpu variable
260 * @tmp: scratch register
262 .macro ldr_this_cpu dst
, sym
, tmp
264 alternative_if_not ARM64_HAS_VIRT_HOST_EXTN
269 ldr \dst
, [\dst
, \tmp
]
273 * vma_vm_mm - get mm pointer from vma pointer (vma->vm_mm)
275 .macro vma_vm_mm
, rd
, rn
276 ldr
\rd
, [\rn
, #VMA_VM_MM]
280 * mmid - get context id from mm pointer (mm->context.id)
283 ldr
\rd
, [\rn
, #MM_CONTEXT_ID]
286 * read_ctr - read CTR_EL0. If the system has mismatched register fields,
287 * provide the system wide safe value from arm64_ftr_reg_ctrel0.sys_val
290 alternative_if_not ARM64_MISMATCHED_CACHE_TYPE
291 mrs
\reg
, ctr_el0
// read CTR
294 ldr_l
\reg
, arm64_ftr_reg_ctrel0
+ ARM64_FTR_SYSVAL
300 * raw_dcache_line_size - get the minimum D-cache line size on this CPU
301 * from the CTR register.
303 .macro raw_dcache_line_size
, reg
, tmp
304 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
311 * dcache_line_size - get the safe D-cache line size across all CPUs
313 .macro dcache_line_size
, reg
, 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
321 * raw_icache_line_size - get the minimum I-cache line size on this CPU
322 * from the CTR register.
324 .macro raw_icache_line_size
, reg
, tmp
325 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
332 * icache_line_size - get the safe I-cache line size across all CPUs
334 .macro icache_line_size
, reg
, 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
342 * tcr_set_t0sz - update TCR.T0SZ so that we can load the ID map
344 .macro tcr_set_t0sz
, valreg
, t0sz
345 bfi
\valreg
, \t0sz
, #TCR_T0SZ_OFFSET, #TCR_TxSZ_WIDTH
349 * tcr_compute_pa_size - set TCR.(I)PS to the highest supported
350 * ID_AA64MMFR0_EL1.PARange value
352 * tcr: register with the TCR_ELx value to be updated
353 * pos: IPS or PS bitfield position
354 * tmp{0,1}: temporary registers
356 .macro tcr_compute_pa_size
, tcr
, pos
, tmp0
, tmp1
357 mrs
\tmp
0, ID_AA64MMFR0_EL1
358 // Narrow PARange to fit the PS field in TCR_ELx
359 ubfx
\tmp
0, \tmp
0, #ID_AA64MMFR0_PARANGE_SHIFT, #3
360 mov
\tmp
1, #ID_AA64MMFR0_PARANGE_MAX
362 csel
\tmp
0, \tmp
1, \tmp
0, hi
363 bfi
\tcr
, \tmp
0, \pos
, #3
367 * Macro to perform a data cache maintenance for the interval
368 * [kaddr, kaddr + size)
370 * op: operation passed to dc instruction
371 * domain: domain used in dsb instruciton
372 * kaddr: starting virtual address of the region
373 * size: size of the region
374 * Corrupts: kaddr, size, tmp1, tmp2
376 .macro __dcache_op_workaround_clean_cache
, op
, kaddr
377 alternative_if_not ARM64_WORKAROUND_CLEAN_CACHE
384 .macro dcache_by_line_op op
, domain
, kaddr
, size
, tmp1
, tmp2
385 dcache_line_size
\tmp
1, \tmp
2
386 add \size
, \kaddr
, \size
388 bic \kaddr
, \kaddr
, \tmp
2
391 __dcache_op_workaround_clean_cache \op
, \kaddr
394 __dcache_op_workaround_clean_cache \op
, \kaddr
397 sys
3, c7
, c12
, 1, \kaddr
// dc cvap
400 sys
3, c7
, c13
, 1, \kaddr
// dc cvadp
407 add \kaddr
, \kaddr
, \tmp
1
414 * Macro to perform an instruction cache maintenance for the interval
417 * start, end: virtual addresses describing the region
418 * label: A label to branch to on user fault.
419 * Corrupts: tmp1, tmp2
421 .macro invalidate_icache_by_line start
, end
, tmp1
, tmp2
, label
422 icache_line_size
\tmp
1, \tmp
2
424 bic
\tmp
2, \start
, \tmp
2
426 USER(\label
, ic ivau
, \tmp
2) // invalidate I line PoU
427 add
\tmp
2, \tmp
2, \tmp
1
435 * reset_pmuserenr_el0 - reset PMUSERENR_EL0 if PMUv3 present
437 .macro reset_pmuserenr_el0
, tmpreg
438 mrs
\tmpreg
, id_aa64dfr0_el1
439 sbfx
\tmpreg
, \tmpreg
, #ID_AA64DFR0_PMUVER_SHIFT, #4
440 cmp
\tmpreg
, #1 // Skip if no PMU present
442 msr pmuserenr_el0
, xzr
// Disable PMU access from EL0
447 * copy_page - copy src to dest using temp registers t1-t8
449 .macro copy_page dest
:req src
:req t1
:req t2
:req t3
:req t4
:req t5
:req t6
:req t7
:req t8
:req
450 9998: ldp
\t1, \t2, [\src
]
451 ldp
\t3, \t4, [\src
, #16]
452 ldp
\t5, \t6, [\src
, #32]
453 ldp
\t7, \t8, [\src
, #48]
455 stnp
\t1, \t2, [\dest
]
456 stnp
\t3, \t4, [\dest
, #16]
457 stnp
\t5, \t6, [\dest
, #32]
458 stnp
\t7, \t8, [\dest
, #48]
459 add \dest
, \dest
, #64
460 tst \src
, #(PAGE_SIZE - 1)
465 * Annotate a function as position independent, i.e., safe to be called before
466 * the kernel virtual mapping is activated.
468 #define ENDPIPROC(x) \
470 .type __pi_##x, %function; \
472 .size __pi_##x, . - x; \
476 * Annotate a function as being unsuitable for kprobes.
478 #ifdef CONFIG_KPROBES
479 #define NOKPROBE(x) \
480 .pushsection "_kprobe_blacklist", "aw"; \
488 #define EXPORT_SYMBOL_NOKASAN(name)
490 #define EXPORT_SYMBOL_NOKASAN(name) EXPORT_SYMBOL(name)
494 * Emit a 64-bit absolute little endian symbol reference in a way that
495 * ensures that it will be resolved at build time, even when building a
496 * PIE binary. This requires cooperation from the linker script, which
497 * must emit the lo32/hi32 halves individually.
505 * mov_q - move an immediate constant into a 64-bit register using
506 * between 2 and 4 movz/movk instructions (depending on the
507 * magnitude and sign of the operand)
509 .macro mov_q
, reg
, val
510 .if (((\val
) >> 31) == 0 || ((\val
) >> 31) == 0x1ffffffff)
511 movz
\reg
, :abs_g1_s
:\val
513 .if (((\val
) >> 47) == 0 || ((\val
) >> 47) == 0x1ffff)
514 movz
\reg
, :abs_g2_s
:\val
516 movz
\reg
, :abs_g3
:\val
517 movk
\reg
, :abs_g2_nc
:\val
519 movk
\reg
, :abs_g1_nc
:\val
521 movk
\reg
, :abs_g0_nc
:\val
525 * Return the current task_struct.
527 .macro get_current_task
, rd
532 * Offset ttbr1 to allow for 48-bit kernel VAs set with 52-bit PTRS_PER_PGD.
533 * orr is used as it can cover the immediate value (and is idempotent).
534 * In future this may be nop'ed out when dealing with 52-bit kernel VAs.
535 * ttbr: Value of ttbr to set, modified.
537 .macro offset_ttbr1
, ttbr
538 #ifdef CONFIG_ARM64_USER_VA_BITS_52
539 orr
\ttbr
, \ttbr
, #TTBR1_BADDR_4852_OFFSET
544 * Perform the reverse of offset_ttbr1.
545 * bic is used as it can cover the immediate value and, in future, won't need
546 * to be nop'ed out when dealing with 52-bit kernel VAs.
548 .macro restore_ttbr1
, ttbr
549 #ifdef CONFIG_ARM64_USER_VA_BITS_52
550 bic
\ttbr
, \ttbr
, #TTBR1_BADDR_4852_OFFSET
555 * Arrange a physical address in a TTBR register, taking care of 52-bit
558 * phys: physical address, preserved
559 * ttbr: returns the TTBR value
561 .macro phys_to_ttbr
, ttbr
, phys
562 #ifdef CONFIG_ARM64_PA_BITS_52
563 orr
\ttbr
, \phys
, \phys
, lsr
#46
564 and \ttbr
, \ttbr
, #TTBR_BADDR_MASK_52
570 .macro phys_to_pte
, pte
, phys
571 #ifdef CONFIG_ARM64_PA_BITS_52
573 * We assume \phys is 64K aligned and this is guaranteed by only
574 * supporting this configuration with 64K pages.
576 orr \pte
, \phys
, \phys
, lsr
#36
577 and \pte
, \pte
, #PTE_ADDR_MASK
583 .macro pte_to_phys
, phys
, pte
584 #ifdef CONFIG_ARM64_PA_BITS_52
585 ubfiz \phys
, \pte
, #(48 - 16 - 12), #16
586 bfxil \phys
, \pte
, #16, #32
587 lsl \phys
, \phys
, #16
589 and \phys
, \pte
, #PTE_ADDR_MASK
594 * tcr_clear_errata_bits - Clear TCR bits that trigger an errata on this CPU.
596 .macro tcr_clear_errata_bits
, tcr
, tmp1
, tmp2
597 #ifdef CONFIG_FUJITSU_ERRATUM_010001
600 mov_q
\tmp
2, MIDR_FUJITSU_ERRATUM_010001_MASK
601 and \tmp
1, \tmp
1, \tmp
2
602 mov_q
\tmp
2, MIDR_FUJITSU_ERRATUM_010001
606 mov_q
\tmp
2, TCR_CLEAR_FUJITSU_ERRATUM_010001
607 bic
\tcr
, \tcr
, \tmp
2
609 #endif /* CONFIG_FUJITSU_ERRATUM_010001 */
613 * Errata workaround prior to disable MMU. Insert an ISB immediately prior
614 * to executing the MSR that will change SCTLR_ELn[M] from a value of 1 to 0.
616 .macro pre_disable_mmu_workaround
617 #ifdef CONFIG_QCOM_FALKOR_ERRATUM_E1041
623 * frame_push - Push @regcount callee saved registers to the stack,
624 * starting at x19, as well as x29/x30, and set x29 to
625 * the new value of sp. Add @extra bytes of stack space
628 .macro frame_push
, regcount
:req
, extra
629 __frame st
, \regcount
, \extra
633 * frame_pop - Pop the callee saved registers from the stack that were
634 * pushed in the most recent call to frame_push, as well
635 * as x29/x30 and any extra stack space that may have been
642 .macro __frame_regs
, reg1
, reg2
, op
, num
643 .if .Lframe_regcount
== \num
644 \op\
()r
\reg
1, [sp
, #(\num + 1) * 8]
645 .elseif
.Lframe_regcount
> \num
646 \op\
()p
\reg
1, \reg
2, [sp
, #(\num + 1) * 8]
650 .macro __frame
, op
, regcount
, extra
=0
652 .if (\regcount
) < 0 || (\regcount
) > 10
653 .error
"regcount should be in the range [0 ... 10]"
655 .if ((\extra
) % 16) != 0
656 .error
"extra should be a multiple of 16 bytes"
658 .ifdef
.Lframe_regcount
659 .if .Lframe_regcount
!= -1
660 .error
"frame_push/frame_pop may not be nested"
663 .set
.Lframe_regcount
, \regcount
664 .set
.Lframe_extra
, \extra
665 .set
.Lframe_local_offset
, ((\regcount
+ 3) / 2) * 16
666 stp x29
, x30
, [sp
, #-.Lframe_local_offset - .Lframe_extra]!
670 __frame_regs x19
, x20
, \op
, 1
671 __frame_regs x21
, x22
, \op
, 3
672 __frame_regs x23
, x24
, \op
, 5
673 __frame_regs x25
, x26
, \op
, 7
674 __frame_regs x27
, x28
, \op
, 9
677 .if .Lframe_regcount
== -1
678 .error
"frame_push/frame_pop may not be nested"
680 ldp x29
, x30
, [sp
], #.Lframe_local_offset + .Lframe_extra
681 .set
.Lframe_regcount
, -1
686 * Check whether to yield to another runnable task from kernel mode NEON code
687 * (which runs with preemption disabled).
689 * if_will_cond_yield_neon
690 * // pre-yield patchup code
692 * // post-yield patchup code
693 * endif_yield_neon <label>
695 * where <label> is optional, and marks the point where execution will resume
696 * after a yield has been performed. If omitted, execution resumes right after
697 * the endif_yield_neon invocation. Note that the entire sequence, including
698 * the provided patchup code, will be omitted from the image if CONFIG_PREEMPT
701 * As a convenience, in the case where no patchup code is required, the above
702 * sequence may be abbreviated to
704 * cond_yield_neon <label>
706 * Note that the patchup code does not support assembler directives that change
707 * the output section, any use of such directives is undefined.
709 * The yield itself consists of the following:
710 * - Check whether the preempt count is exactly 1 and a reschedule is also
711 * needed. If so, calling of preempt_enable() in kernel_neon_end() will
712 * trigger a reschedule. If it is not the case, yielding is pointless.
713 * - Disable and re-enable kernel mode NEON, and branch to the yield fixup
716 * This macro sequence may clobber all CPU state that is not guaranteed by the
717 * AAPCS to be preserved across an ordinary function call.
720 .macro cond_yield_neon
, lbl
721 if_will_cond_yield_neon
723 endif_yield_neon \lbl
726 .macro if_will_cond_yield_neon
727 #ifdef CONFIG_PREEMPT
729 ldr x0
, [x0
, #TSK_TI_PREEMPT]
730 sub x0
, x0
, #PREEMPT_DISABLE_OFFSET
732 /* fall through to endif_yield_neon */
736 .section
".discard.cond_yield_neon", "ax"
740 .macro do_cond_yield_neon
745 .macro endif_yield_neon
, lbl
755 #endif /* __ASM_ASSEMBLER_H */