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 /* IRQ is the lowest priority flag, unconditionally unmask the rest. */
45 msr daifclr, #(8 | 4 | 1)
49 * Save/restore interrupts.
51 .macro save_and_disable_irq, flags
56 .macro restore_irq, flags
64 .macro disable_step_tsk, flgs, tmp
65 tbz \flgs, #TIF_SINGLESTEP, 9990f
67 bic \tmp, \tmp, #DBG_MDSCR_SS
69 isb // Synchronise with enable_dbg
73 /* call with daif masked */
74 .macro enable_step_tsk, flgs, tmp
75 tbz \flgs, #TIF_SINGLESTEP, 9990f
77 orr \tmp, \tmp, #DBG_MDSCR_SS
83 * RAS Error Synchronization barrier
86 #ifdef CONFIG_ARM64_RAS_EXTN
94 * Value prediction barrier
101 * Speculation barrier
104 alternative_if_not ARM64_HAS_SB
123 * Emit an entry into the exception table
125 .macro _asm_extable, from, to
126 .pushsection __ex_table, "a"
128 .long (\from - .), (\to - .)
132 #define USER(l, x...) \
134 _asm_extable 9999b, l
139 lr .req x30 // link register
150 * Select code when configured for BE.
152 #ifdef CONFIG_CPU_BIG_ENDIAN
153 #define CPU_BE(code...) code
155 #define CPU_BE(code...)
159 * Select code when configured for LE.
161 #ifdef CONFIG_CPU_BIG_ENDIAN
162 #define CPU_LE(code...)
164 #define CPU_LE(code...) code
168 * Define a macro that constructs a 64-bit value by concatenating two
169 * 32-bit registers. Note that on big endian systems the order of the
170 * registers is swapped.
172 #ifndef CONFIG_CPU_BIG_ENDIAN
173 .macro regs_to_64, rd, lbits, hbits
175 .macro regs_to_64, rd, hbits, lbits
177 orr \rd, \lbits, \hbits, lsl #32
181 * Pseudo-ops for PC-relative adr/ldr/str <reg>, <symbol> where
182 * <symbol> is within the range +/- 4 GB of the PC.
185 * @dst: destination register (64 bit wide)
186 * @sym: name of the symbol
188 .macro adr_l, dst, sym
190 add \dst, \dst, :lo12:\sym
194 * @dst: destination register (32 or 64 bit wide)
195 * @sym: name of the symbol
196 * @tmp: optional 64-bit scratch register to be used if <dst> is a
197 * 32-bit wide register, in which case it cannot be used to hold
200 .macro ldr_l, dst, sym, tmp=
203 ldr \dst, [\dst, :lo12:\sym]
206 ldr \dst, [\tmp, :lo12:\sym]
211 * @src: source register (32 or 64 bit wide)
212 * @sym: name of the symbol
213 * @tmp: mandatory 64-bit scratch register to calculate the address
214 * while <src> needs to be preserved.
216 .macro str_l, src, sym, tmp
218 str \src, [\tmp, :lo12:\sym]
222 * @dst: Result of per_cpu(sym, smp_processor_id()) (can be SP)
223 * @sym: The name of the per-cpu variable
224 * @tmp: scratch register
226 .macro adr_this_cpu, dst, sym, tmp
228 add \dst, \tmp, #:lo12:\sym
229 alternative_if_not ARM64_HAS_VIRT_HOST_EXTN
238 * @dst: Result of READ_ONCE(per_cpu(sym, smp_processor_id()))
239 * @sym: The name of the per-cpu variable
240 * @tmp: scratch register
242 .macro ldr_this_cpu dst, sym, tmp
244 alternative_if_not ARM64_HAS_VIRT_HOST_EXTN
249 ldr \dst, [\dst, \tmp]
253 * vma_vm_mm - get mm pointer from vma pointer (vma->vm_mm)
255 .macro vma_vm_mm, rd, rn
256 ldr \rd, [\rn, #VMA_VM_MM]
260 * read_ctr - read CTR_EL0. If the system has mismatched register fields,
261 * provide the system wide safe value from arm64_ftr_reg_ctrel0.sys_val
264 alternative_if_not ARM64_MISMATCHED_CACHE_TYPE
265 mrs \reg, ctr_el0 // read CTR
268 ldr_l \reg, arm64_ftr_reg_ctrel0 + ARM64_FTR_SYSVAL
274 * raw_dcache_line_size - get the minimum D-cache line size on this CPU
275 * from the CTR register.
277 .macro raw_dcache_line_size, reg, tmp
278 mrs \tmp, ctr_el0 // read CTR
279 ubfm \tmp, \tmp, #16, #19 // cache line size encoding
280 mov \reg, #4 // bytes per word
281 lsl \reg, \reg, \tmp // actual cache line size
285 * dcache_line_size - get the safe D-cache line size across all CPUs
287 .macro dcache_line_size, reg, tmp
289 ubfm \tmp, \tmp, #16, #19 // cache line size encoding
290 mov \reg, #4 // bytes per word
291 lsl \reg, \reg, \tmp // actual cache line size
295 * raw_icache_line_size - get the minimum I-cache line size on this CPU
296 * from the CTR register.
298 .macro raw_icache_line_size, reg, tmp
299 mrs \tmp, ctr_el0 // read CTR
300 and \tmp, \tmp, #0xf // cache line size encoding
301 mov \reg, #4 // bytes per word
302 lsl \reg, \reg, \tmp // actual cache line size
306 * icache_line_size - get the safe I-cache line size across all CPUs
308 .macro icache_line_size, reg, tmp
310 and \tmp, \tmp, #0xf // cache line size encoding
311 mov \reg, #4 // bytes per word
312 lsl \reg, \reg, \tmp // actual cache line size
316 * tcr_set_t0sz - update TCR.T0SZ so that we can load the ID map
318 .macro tcr_set_t0sz, valreg, t0sz
319 bfi \valreg, \t0sz, #TCR_T0SZ_OFFSET, #TCR_TxSZ_WIDTH
323 * tcr_set_t1sz - update TCR.T1SZ
325 .macro tcr_set_t1sz, valreg, t1sz
326 bfi \valreg, \t1sz, #TCR_T1SZ_OFFSET, #TCR_TxSZ_WIDTH
330 * tcr_compute_pa_size - set TCR.(I)PS to the highest supported
331 * ID_AA64MMFR0_EL1.PARange value
333 * tcr: register with the TCR_ELx value to be updated
334 * pos: IPS or PS bitfield position
335 * tmp{0,1}: temporary registers
337 .macro tcr_compute_pa_size, tcr, pos, tmp0, tmp1
338 mrs \tmp0, ID_AA64MMFR0_EL1
339 // Narrow PARange to fit the PS field in TCR_ELx
340 ubfx \tmp0, \tmp0, #ID_AA64MMFR0_PARANGE_SHIFT, #3
341 mov \tmp1, #ID_AA64MMFR0_PARANGE_MAX
343 csel \tmp0, \tmp1, \tmp0, hi
344 bfi \tcr, \tmp0, \pos, #3
348 * Macro to perform a data cache maintenance for the interval
349 * [kaddr, kaddr + size)
351 * op: operation passed to dc instruction
352 * domain: domain used in dsb instruciton
353 * kaddr: starting virtual address of the region
354 * size: size of the region
355 * Corrupts: kaddr, size, tmp1, tmp2
357 .macro __dcache_op_workaround_clean_cache, op, kaddr
358 alternative_if_not ARM64_WORKAROUND_CLEAN_CACHE
365 .macro dcache_by_line_op op, domain, kaddr, size, tmp1, tmp2
366 dcache_line_size \tmp1, \tmp2
367 add \size, \kaddr, \size
369 bic \kaddr, \kaddr, \tmp2
372 __dcache_op_workaround_clean_cache \op, \kaddr
375 __dcache_op_workaround_clean_cache \op, \kaddr
378 sys 3, c7, c12, 1, \kaddr // dc cvap
381 sys 3, c7, c13, 1, \kaddr // dc cvadp
388 add \kaddr, \kaddr, \tmp1
395 * Macro to perform an instruction cache maintenance for the interval
398 * start, end: virtual addresses describing the region
399 * label: A label to branch to on user fault.
400 * Corrupts: tmp1, tmp2
402 .macro invalidate_icache_by_line start, end, tmp1, tmp2, label
403 icache_line_size \tmp1, \tmp2
405 bic \tmp2, \start, \tmp2
407 USER(\label, ic ivau, \tmp2) // invalidate I line PoU
408 add \tmp2, \tmp2, \tmp1
416 * reset_pmuserenr_el0 - reset PMUSERENR_EL0 if PMUv3 present
418 .macro reset_pmuserenr_el0, tmpreg
419 mrs \tmpreg, id_aa64dfr0_el1
420 sbfx \tmpreg, \tmpreg, #ID_AA64DFR0_PMUVER_SHIFT, #4
421 cmp \tmpreg, #1 // Skip if no PMU present
423 msr pmuserenr_el0, xzr // Disable PMU access from EL0
428 * reset_amuserenr_el0 - reset AMUSERENR_EL0 if AMUv1 present
430 .macro reset_amuserenr_el0, tmpreg
431 mrs \tmpreg, id_aa64pfr0_el1 // Check ID_AA64PFR0_EL1
432 ubfx \tmpreg, \tmpreg, #ID_AA64PFR0_AMU_SHIFT, #4
433 cbz \tmpreg, .Lskip_\@ // Skip if no AMU present
434 msr_s SYS_AMUSERENR_EL0, xzr // Disable AMU access from EL0
438 * copy_page - copy src to dest using temp registers t1-t8
440 .macro copy_page dest:req src:req t1:req t2:req t3:req t4:req t5:req t6:req t7:req t8:req
441 9998: ldp \t1, \t2, [\src]
442 ldp \t3, \t4, [\src, #16]
443 ldp \t5, \t6, [\src, #32]
444 ldp \t7, \t8, [\src, #48]
446 stnp \t1, \t2, [\dest]
447 stnp \t3, \t4, [\dest, #16]
448 stnp \t5, \t6, [\dest, #32]
449 stnp \t7, \t8, [\dest, #48]
450 add \dest, \dest, #64
451 tst \src, #(PAGE_SIZE - 1)
456 * Annotate a function as being unsuitable for kprobes.
458 #ifdef CONFIG_KPROBES
459 #define NOKPROBE(x) \
460 .pushsection "_kprobe_blacklist", "aw"; \
468 #define EXPORT_SYMBOL_NOKASAN(name)
470 #define EXPORT_SYMBOL_NOKASAN(name) EXPORT_SYMBOL(name)
474 * Emit a 64-bit absolute little endian symbol reference in a way that
475 * ensures that it will be resolved at build time, even when building a
476 * PIE binary. This requires cooperation from the linker script, which
477 * must emit the lo32/hi32 halves individually.
485 * mov_q - move an immediate constant into a 64-bit register using
486 * between 2 and 4 movz/movk instructions (depending on the
487 * magnitude and sign of the operand)
489 .macro mov_q, reg, val
490 .if (((\val) >> 31) == 0 || ((\val) >> 31) == 0x1ffffffff)
491 movz \reg, :abs_g1_s:\val
493 .if (((\val) >> 47) == 0 || ((\val) >> 47) == 0x1ffff)
494 movz \reg, :abs_g2_s:\val
496 movz \reg, :abs_g3:\val
497 movk \reg, :abs_g2_nc:\val
499 movk \reg, :abs_g1_nc:\val
501 movk \reg, :abs_g0_nc:\val
505 * Return the current task_struct.
507 .macro get_current_task, rd
512 * Offset ttbr1 to allow for 48-bit kernel VAs set with 52-bit PTRS_PER_PGD.
513 * orr is used as it can cover the immediate value (and is idempotent).
514 * In future this may be nop'ed out when dealing with 52-bit kernel VAs.
515 * ttbr: Value of ttbr to set, modified.
517 .macro offset_ttbr1, ttbr, tmp
518 #ifdef CONFIG_ARM64_VA_BITS_52
519 mrs_s \tmp, SYS_ID_AA64MMFR2_EL1
520 and \tmp, \tmp, #(0xf << ID_AA64MMFR2_LVA_SHIFT)
521 cbnz \tmp, .Lskipoffs_\@
522 orr \ttbr, \ttbr, #TTBR1_BADDR_4852_OFFSET
528 * Perform the reverse of offset_ttbr1.
529 * bic is used as it can cover the immediate value and, in future, won't need
530 * to be nop'ed out when dealing with 52-bit kernel VAs.
532 .macro restore_ttbr1, ttbr
533 #ifdef CONFIG_ARM64_VA_BITS_52
534 bic \ttbr, \ttbr, #TTBR1_BADDR_4852_OFFSET
539 * Arrange a physical address in a TTBR register, taking care of 52-bit
542 * phys: physical address, preserved
543 * ttbr: returns the TTBR value
545 .macro phys_to_ttbr, ttbr, phys
546 #ifdef CONFIG_ARM64_PA_BITS_52
547 orr \ttbr, \phys, \phys, lsr #46
548 and \ttbr, \ttbr, #TTBR_BADDR_MASK_52
554 .macro phys_to_pte, pte, phys
555 #ifdef CONFIG_ARM64_PA_BITS_52
557 * We assume \phys is 64K aligned and this is guaranteed by only
558 * supporting this configuration with 64K pages.
560 orr \pte, \phys, \phys, lsr #36
561 and \pte, \pte, #PTE_ADDR_MASK
567 .macro pte_to_phys, phys, pte
568 #ifdef CONFIG_ARM64_PA_BITS_52
569 ubfiz \phys, \pte, #(48 - 16 - 12), #16
570 bfxil \phys, \pte, #16, #32
571 lsl \phys, \phys, #16
573 and \phys, \pte, #PTE_ADDR_MASK
578 * tcr_clear_errata_bits - Clear TCR bits that trigger an errata on this CPU.
580 .macro tcr_clear_errata_bits, tcr, tmp1, tmp2
581 #ifdef CONFIG_FUJITSU_ERRATUM_010001
584 mov_q \tmp2, MIDR_FUJITSU_ERRATUM_010001_MASK
585 and \tmp1, \tmp1, \tmp2
586 mov_q \tmp2, MIDR_FUJITSU_ERRATUM_010001
590 mov_q \tmp2, TCR_CLEAR_FUJITSU_ERRATUM_010001
591 bic \tcr, \tcr, \tmp2
593 #endif /* CONFIG_FUJITSU_ERRATUM_010001 */
597 * Errata workaround prior to disable MMU. Insert an ISB immediately prior
598 * to executing the MSR that will change SCTLR_ELn[M] from a value of 1 to 0.
600 .macro pre_disable_mmu_workaround
601 #ifdef CONFIG_QCOM_FALKOR_ERRATUM_E1041
607 * frame_push - Push @regcount callee saved registers to the stack,
608 * starting at x19, as well as x29/x30, and set x29 to
609 * the new value of sp. Add @extra bytes of stack space
612 .macro frame_push, regcount:req, extra
613 __frame st, \regcount, \extra
617 * frame_pop - Pop the callee saved registers from the stack that were
618 * pushed in the most recent call to frame_push, as well
619 * as x29/x30 and any extra stack space that may have been
626 .macro __frame_regs, reg1, reg2, op, num
627 .if .Lframe_regcount == \num
628 \op\()r \reg1, [sp, #(\num + 1) * 8]
629 .elseif .Lframe_regcount > \num
630 \op\()p \reg1, \reg2, [sp, #(\num + 1) * 8]
634 .macro __frame, op, regcount, extra=0
636 .if (\regcount) < 0 || (\regcount) > 10
637 .error "regcount should be in the range [0 ... 10]"
639 .if ((\extra) % 16) != 0
640 .error "extra should be a multiple of 16 bytes"
642 .ifdef .Lframe_regcount
643 .if .Lframe_regcount != -1
644 .error "frame_push/frame_pop may not be nested"
647 .set .Lframe_regcount, \regcount
648 .set .Lframe_extra, \extra
649 .set .Lframe_local_offset, ((\regcount + 3) / 2) * 16
650 stp x29, x30, [sp, #-.Lframe_local_offset - .Lframe_extra]!
654 __frame_regs x19, x20, \op, 1
655 __frame_regs x21, x22, \op, 3
656 __frame_regs x23, x24, \op, 5
657 __frame_regs x25, x26, \op, 7
658 __frame_regs x27, x28, \op, 9
661 .if .Lframe_regcount == -1
662 .error "frame_push/frame_pop may not be nested"
664 ldp x29, x30, [sp], #.Lframe_local_offset + .Lframe_extra
665 .set .Lframe_regcount, -1
670 * Check whether to yield to another runnable task from kernel mode NEON code
671 * (which runs with preemption disabled).
673 * if_will_cond_yield_neon
674 * // pre-yield patchup code
676 * // post-yield patchup code
677 * endif_yield_neon <label>
679 * where <label> is optional, and marks the point where execution will resume
680 * after a yield has been performed. If omitted, execution resumes right after
681 * the endif_yield_neon invocation. Note that the entire sequence, including
682 * the provided patchup code, will be omitted from the image if
683 * CONFIG_PREEMPTION is not defined.
685 * As a convenience, in the case where no patchup code is required, the above
686 * sequence may be abbreviated to
688 * cond_yield_neon <label>
690 * Note that the patchup code does not support assembler directives that change
691 * the output section, any use of such directives is undefined.
693 * The yield itself consists of the following:
694 * - Check whether the preempt count is exactly 1 and a reschedule is also
695 * needed. If so, calling of preempt_enable() in kernel_neon_end() will
696 * trigger a reschedule. If it is not the case, yielding is pointless.
697 * - Disable and re-enable kernel mode NEON, and branch to the yield fixup
700 * This macro sequence may clobber all CPU state that is not guaranteed by the
701 * AAPCS to be preserved across an ordinary function call.
704 .macro cond_yield_neon, lbl
705 if_will_cond_yield_neon
707 endif_yield_neon \lbl
710 .macro if_will_cond_yield_neon
711 #ifdef CONFIG_PREEMPTION
713 ldr x0, [x0, #TSK_TI_PREEMPT]
714 sub x0, x0, #PREEMPT_DISABLE_OFFSET
716 /* fall through to endif_yield_neon */
720 .section ".discard.cond_yield_neon", "ax"
724 .macro do_cond_yield_neon
729 .macro endif_yield_neon, lbl
740 * This macro emits a program property note section identifying
741 * architecture features which require special handling, mainly for
742 * use in assembly files included in the VDSO.
745 #define NT_GNU_PROPERTY_TYPE_0 5
746 #define GNU_PROPERTY_AARCH64_FEATURE_1_AND 0xc0000000
748 #define GNU_PROPERTY_AARCH64_FEATURE_1_BTI (1U << 0)
749 #define GNU_PROPERTY_AARCH64_FEATURE_1_PAC (1U << 1)
751 #ifdef CONFIG_ARM64_BTI_KERNEL
752 #define GNU_PROPERTY_AARCH64_FEATURE_1_DEFAULT \
753 ((GNU_PROPERTY_AARCH64_FEATURE_1_BTI | \
754 GNU_PROPERTY_AARCH64_FEATURE_1_PAC))
757 #ifdef GNU_PROPERTY_AARCH64_FEATURE_1_DEFAULT
758 .macro emit_aarch64_feature_1_and, feat=GNU_PROPERTY_AARCH64_FEATURE_1_DEFAULT
759 .pushsection .note.gnu.property, "a"
763 .long NT_GNU_PROPERTY_TYPE_0
767 3: .long GNU_PROPERTY_AARCH64_FEATURE_1_AND
771 * This is described with an array of char in the Linux API
772 * spec but the text and all other usage (including binutils,
773 * clang and GCC) treat this as a 32 bit value so no swizzling
774 * is required for big endian.
784 .macro emit_aarch64_feature_1_and, feat=0
787 #endif /* GNU_PROPERTY_AARCH64_FEATURE_1_DEFAULT */
789 #endif /* __ASM_ASSEMBLER_H */