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>
27 * Provide a wxN alias for each wN register so what we can paste a xN
28 * reference after a 'w' to obtain the 32-bit version.
30 .irp n,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30
34 .macro save_and_disable_daif, flags
47 .macro restore_daif, flags:req
51 /* IRQ is the lowest priority flag, unconditionally unmask the rest. */
53 msr daifclr, #(8 | 4 | 1)
57 * Save/restore interrupts.
59 .macro save_and_disable_irq, flags
64 .macro restore_irq, flags
72 .macro disable_step_tsk, flgs, tmp
73 tbz \flgs, #TIF_SINGLESTEP, 9990f
75 bic \tmp, \tmp, #DBG_MDSCR_SS
77 isb // Synchronise with enable_dbg
81 /* call with daif masked */
82 .macro enable_step_tsk, flgs, tmp
83 tbz \flgs, #TIF_SINGLESTEP, 9990f
85 orr \tmp, \tmp, #DBG_MDSCR_SS
91 * RAS Error Synchronization barrier
94 #ifdef CONFIG_ARM64_RAS_EXTN
102 * Value prediction barrier
109 * Speculation barrier
112 alternative_if_not ARM64_HAS_SB
131 * Emit an entry into the exception table
133 .macro _asm_extable, from, to
134 .pushsection __ex_table, "a"
136 .long (\from - .), (\to - .)
140 #define USER(l, x...) \
142 _asm_extable 9999b, l
147 lr .req x30 // link register
158 * Select code when configured for BE.
160 #ifdef CONFIG_CPU_BIG_ENDIAN
161 #define CPU_BE(code...) code
163 #define CPU_BE(code...)
167 * Select code when configured for LE.
169 #ifdef CONFIG_CPU_BIG_ENDIAN
170 #define CPU_LE(code...)
172 #define CPU_LE(code...) code
176 * Define a macro that constructs a 64-bit value by concatenating two
177 * 32-bit registers. Note that on big endian systems the order of the
178 * registers is swapped.
180 #ifndef CONFIG_CPU_BIG_ENDIAN
181 .macro regs_to_64, rd, lbits, hbits
183 .macro regs_to_64, rd, hbits, lbits
185 orr \rd, \lbits, \hbits, lsl #32
189 * Pseudo-ops for PC-relative adr/ldr/str <reg>, <symbol> where
190 * <symbol> is within the range +/- 4 GB of the PC.
193 * @dst: destination register (64 bit wide)
194 * @sym: name of the symbol
196 .macro adr_l, dst, sym
198 add \dst, \dst, :lo12:\sym
202 * @dst: destination register (32 or 64 bit wide)
203 * @sym: name of the symbol
204 * @tmp: optional 64-bit scratch register to be used if <dst> is a
205 * 32-bit wide register, in which case it cannot be used to hold
208 .macro ldr_l, dst, sym, tmp=
211 ldr \dst, [\dst, :lo12:\sym]
214 ldr \dst, [\tmp, :lo12:\sym]
219 * @src: source register (32 or 64 bit wide)
220 * @sym: name of the symbol
221 * @tmp: mandatory 64-bit scratch register to calculate the address
222 * while <src> needs to be preserved.
224 .macro str_l, src, sym, tmp
226 str \src, [\tmp, :lo12:\sym]
230 * @dst: destination register
232 #if defined(__KVM_NVHE_HYPERVISOR__) || defined(__KVM_VHE_HYPERVISOR__)
233 .macro this_cpu_offset, dst
237 .macro this_cpu_offset, dst
238 alternative_if_not ARM64_HAS_VIRT_HOST_EXTN
247 * @dst: Result of per_cpu(sym, smp_processor_id()) (can be SP)
248 * @sym: The name of the per-cpu variable
249 * @tmp: scratch register
251 .macro adr_this_cpu, dst, sym, tmp
253 add \dst, \tmp, #:lo12:\sym
259 * @dst: Result of READ_ONCE(per_cpu(sym, smp_processor_id()))
260 * @sym: The name of the per-cpu variable
261 * @tmp: scratch register
263 .macro ldr_this_cpu dst, sym, tmp
266 ldr \dst, [\dst, \tmp]
270 * vma_vm_mm - get mm pointer from vma pointer (vma->vm_mm)
272 .macro vma_vm_mm, rd, rn
273 ldr \rd, [\rn, #VMA_VM_MM]
277 * read_ctr - read CTR_EL0. If the system has mismatched register fields,
278 * provide the system wide safe value from arm64_ftr_reg_ctrel0.sys_val
281 alternative_if_not ARM64_MISMATCHED_CACHE_TYPE
282 mrs \reg, ctr_el0 // read CTR
285 ldr_l \reg, arm64_ftr_reg_ctrel0 + ARM64_FTR_SYSVAL
291 * raw_dcache_line_size - get the minimum D-cache line size on this CPU
292 * from the CTR register.
294 .macro raw_dcache_line_size, reg, tmp
295 mrs \tmp, ctr_el0 // read CTR
296 ubfm \tmp, \tmp, #16, #19 // cache line size encoding
297 mov \reg, #4 // bytes per word
298 lsl \reg, \reg, \tmp // actual cache line size
302 * dcache_line_size - get the safe D-cache line size across all CPUs
304 .macro dcache_line_size, reg, tmp
306 ubfm \tmp, \tmp, #16, #19 // cache line size encoding
307 mov \reg, #4 // bytes per word
308 lsl \reg, \reg, \tmp // actual cache line size
312 * raw_icache_line_size - get the minimum I-cache line size on this CPU
313 * from the CTR register.
315 .macro raw_icache_line_size, reg, tmp
316 mrs \tmp, ctr_el0 // read CTR
317 and \tmp, \tmp, #0xf // cache line size encoding
318 mov \reg, #4 // bytes per word
319 lsl \reg, \reg, \tmp // actual cache line size
323 * icache_line_size - get the safe I-cache line size across all CPUs
325 .macro icache_line_size, reg, tmp
327 and \tmp, \tmp, #0xf // cache line size encoding
328 mov \reg, #4 // bytes per word
329 lsl \reg, \reg, \tmp // actual cache line size
333 * tcr_set_t0sz - update TCR.T0SZ so that we can load the ID map
335 .macro tcr_set_t0sz, valreg, t0sz
336 bfi \valreg, \t0sz, #TCR_T0SZ_OFFSET, #TCR_TxSZ_WIDTH
340 * tcr_set_t1sz - update TCR.T1SZ
342 .macro tcr_set_t1sz, valreg, t1sz
343 bfi \valreg, \t1sz, #TCR_T1SZ_OFFSET, #TCR_TxSZ_WIDTH
347 * tcr_compute_pa_size - set TCR.(I)PS to the highest supported
348 * ID_AA64MMFR0_EL1.PARange value
350 * tcr: register with the TCR_ELx value to be updated
351 * pos: IPS or PS bitfield position
352 * tmp{0,1}: temporary registers
354 .macro tcr_compute_pa_size, tcr, pos, tmp0, tmp1
355 mrs \tmp0, ID_AA64MMFR0_EL1
356 // Narrow PARange to fit the PS field in TCR_ELx
357 ubfx \tmp0, \tmp0, #ID_AA64MMFR0_PARANGE_SHIFT, #3
358 mov \tmp1, #ID_AA64MMFR0_PARANGE_MAX
360 csel \tmp0, \tmp1, \tmp0, hi
361 bfi \tcr, \tmp0, \pos, #3
365 * Macro to perform a data cache maintenance for the interval
366 * [kaddr, kaddr + size)
368 * op: operation passed to dc instruction
369 * domain: domain used in dsb instruciton
370 * kaddr: starting virtual address of the region
371 * size: size of the region
372 * Corrupts: kaddr, size, tmp1, tmp2
374 .macro __dcache_op_workaround_clean_cache, op, kaddr
375 alternative_if_not ARM64_WORKAROUND_CLEAN_CACHE
382 .macro dcache_by_line_op op, domain, kaddr, size, tmp1, tmp2
383 dcache_line_size \tmp1, \tmp2
384 add \size, \kaddr, \size
386 bic \kaddr, \kaddr, \tmp2
389 __dcache_op_workaround_clean_cache \op, \kaddr
392 __dcache_op_workaround_clean_cache \op, \kaddr
395 sys 3, c7, c12, 1, \kaddr // dc cvap
398 sys 3, c7, c13, 1, \kaddr // dc cvadp
405 add \kaddr, \kaddr, \tmp1
412 * Macro to perform an instruction cache maintenance for the interval
415 * start, end: virtual addresses describing the region
416 * label: A label to branch to on user fault.
417 * Corrupts: tmp1, tmp2
419 .macro invalidate_icache_by_line start, end, tmp1, tmp2, label
420 icache_line_size \tmp1, \tmp2
422 bic \tmp2, \start, \tmp2
424 USER(\label, ic ivau, \tmp2) // invalidate I line PoU
425 add \tmp2, \tmp2, \tmp1
433 * reset_pmuserenr_el0 - reset PMUSERENR_EL0 if PMUv3 present
435 .macro reset_pmuserenr_el0, tmpreg
436 mrs \tmpreg, id_aa64dfr0_el1
437 sbfx \tmpreg, \tmpreg, #ID_AA64DFR0_PMUVER_SHIFT, #4
438 cmp \tmpreg, #1 // Skip if no PMU present
440 msr pmuserenr_el0, xzr // Disable PMU access from EL0
445 * reset_amuserenr_el0 - reset AMUSERENR_EL0 if AMUv1 present
447 .macro reset_amuserenr_el0, tmpreg
448 mrs \tmpreg, id_aa64pfr0_el1 // Check ID_AA64PFR0_EL1
449 ubfx \tmpreg, \tmpreg, #ID_AA64PFR0_AMU_SHIFT, #4
450 cbz \tmpreg, .Lskip_\@ // Skip if no AMU present
451 msr_s SYS_AMUSERENR_EL0, xzr // Disable AMU access from EL0
455 * copy_page - copy src to dest using temp registers t1-t8
457 .macro copy_page dest:req src:req t1:req t2:req t3:req t4:req t5:req t6:req t7:req t8:req
458 9998: ldp \t1, \t2, [\src]
459 ldp \t3, \t4, [\src, #16]
460 ldp \t5, \t6, [\src, #32]
461 ldp \t7, \t8, [\src, #48]
463 stnp \t1, \t2, [\dest]
464 stnp \t3, \t4, [\dest, #16]
465 stnp \t5, \t6, [\dest, #32]
466 stnp \t7, \t8, [\dest, #48]
467 add \dest, \dest, #64
468 tst \src, #(PAGE_SIZE - 1)
473 * Annotate a function as being unsuitable for kprobes.
475 #ifdef CONFIG_KPROBES
476 #define NOKPROBE(x) \
477 .pushsection "_kprobe_blacklist", "aw"; \
484 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
485 #define EXPORT_SYMBOL_NOKASAN(name)
487 #define EXPORT_SYMBOL_NOKASAN(name) EXPORT_SYMBOL(name)
491 * Emit a 64-bit absolute little endian symbol reference in a way that
492 * ensures that it will be resolved at build time, even when building a
493 * PIE binary. This requires cooperation from the linker script, which
494 * must emit the lo32/hi32 halves individually.
502 * mov_q - move an immediate constant into a 64-bit register using
503 * between 2 and 4 movz/movk instructions (depending on the
504 * magnitude and sign of the operand)
506 .macro mov_q, reg, val
507 .if (((\val) >> 31) == 0 || ((\val) >> 31) == 0x1ffffffff)
508 movz \reg, :abs_g1_s:\val
510 .if (((\val) >> 47) == 0 || ((\val) >> 47) == 0x1ffff)
511 movz \reg, :abs_g2_s:\val
513 movz \reg, :abs_g3:\val
514 movk \reg, :abs_g2_nc:\val
516 movk \reg, :abs_g1_nc:\val
518 movk \reg, :abs_g0_nc:\val
522 * Return the current task_struct.
524 .macro get_current_task, rd
529 * Offset ttbr1 to allow for 48-bit kernel VAs set with 52-bit PTRS_PER_PGD.
530 * orr is used as it can cover the immediate value (and is idempotent).
531 * In future this may be nop'ed out when dealing with 52-bit kernel VAs.
532 * ttbr: Value of ttbr to set, modified.
534 .macro offset_ttbr1, ttbr, tmp
535 #ifdef CONFIG_ARM64_VA_BITS_52
536 mrs_s \tmp, SYS_ID_AA64MMFR2_EL1
537 and \tmp, \tmp, #(0xf << ID_AA64MMFR2_LVA_SHIFT)
538 cbnz \tmp, .Lskipoffs_\@
539 orr \ttbr, \ttbr, #TTBR1_BADDR_4852_OFFSET
545 * Perform the reverse of offset_ttbr1.
546 * bic is used as it can cover the immediate value and, in future, won't need
547 * to be nop'ed out when dealing with 52-bit kernel VAs.
549 .macro restore_ttbr1, ttbr
550 #ifdef CONFIG_ARM64_VA_BITS_52
551 bic \ttbr, \ttbr, #TTBR1_BADDR_4852_OFFSET
556 * Arrange a physical address in a TTBR register, taking care of 52-bit
559 * phys: physical address, preserved
560 * ttbr: returns the TTBR value
562 .macro phys_to_ttbr, ttbr, phys
563 #ifdef CONFIG_ARM64_PA_BITS_52
564 orr \ttbr, \phys, \phys, lsr #46
565 and \ttbr, \ttbr, #TTBR_BADDR_MASK_52
571 .macro phys_to_pte, pte, phys
572 #ifdef CONFIG_ARM64_PA_BITS_52
574 * We assume \phys is 64K aligned and this is guaranteed by only
575 * supporting this configuration with 64K pages.
577 orr \pte, \phys, \phys, lsr #36
578 and \pte, \pte, #PTE_ADDR_MASK
584 .macro pte_to_phys, phys, pte
585 #ifdef CONFIG_ARM64_PA_BITS_52
586 ubfiz \phys, \pte, #(48 - 16 - 12), #16
587 bfxil \phys, \pte, #16, #32
588 lsl \phys, \phys, #16
590 and \phys, \pte, #PTE_ADDR_MASK
595 * tcr_clear_errata_bits - Clear TCR bits that trigger an errata on this CPU.
597 .macro tcr_clear_errata_bits, tcr, tmp1, tmp2
598 #ifdef CONFIG_FUJITSU_ERRATUM_010001
601 mov_q \tmp2, MIDR_FUJITSU_ERRATUM_010001_MASK
602 and \tmp1, \tmp1, \tmp2
603 mov_q \tmp2, MIDR_FUJITSU_ERRATUM_010001
607 mov_q \tmp2, TCR_CLEAR_FUJITSU_ERRATUM_010001
608 bic \tcr, \tcr, \tmp2
610 #endif /* CONFIG_FUJITSU_ERRATUM_010001 */
614 * Errata workaround prior to disable MMU. Insert an ISB immediately prior
615 * to executing the MSR that will change SCTLR_ELn[M] from a value of 1 to 0.
617 .macro pre_disable_mmu_workaround
618 #ifdef CONFIG_QCOM_FALKOR_ERRATUM_E1041
624 * frame_push - Push @regcount callee saved registers to the stack,
625 * starting at x19, as well as x29/x30, and set x29 to
626 * the new value of sp. Add @extra bytes of stack space
629 .macro frame_push, regcount:req, extra
630 __frame st, \regcount, \extra
634 * frame_pop - Pop the callee saved registers from the stack that were
635 * pushed in the most recent call to frame_push, as well
636 * as x29/x30 and any extra stack space that may have been
643 .macro __frame_regs, reg1, reg2, op, num
644 .if .Lframe_regcount == \num
645 \op\()r \reg1, [sp, #(\num + 1) * 8]
646 .elseif .Lframe_regcount > \num
647 \op\()p \reg1, \reg2, [sp, #(\num + 1) * 8]
651 .macro __frame, op, regcount, extra=0
653 .if (\regcount) < 0 || (\regcount) > 10
654 .error "regcount should be in the range [0 ... 10]"
656 .if ((\extra) % 16) != 0
657 .error "extra should be a multiple of 16 bytes"
659 .ifdef .Lframe_regcount
660 .if .Lframe_regcount != -1
661 .error "frame_push/frame_pop may not be nested"
664 .set .Lframe_regcount, \regcount
665 .set .Lframe_extra, \extra
666 .set .Lframe_local_offset, ((\regcount + 3) / 2) * 16
667 stp x29, x30, [sp, #-.Lframe_local_offset - .Lframe_extra]!
671 __frame_regs x19, x20, \op, 1
672 __frame_regs x21, x22, \op, 3
673 __frame_regs x23, x24, \op, 5
674 __frame_regs x25, x26, \op, 7
675 __frame_regs x27, x28, \op, 9
678 .if .Lframe_regcount == -1
679 .error "frame_push/frame_pop may not be nested"
681 ldp x29, x30, [sp], #.Lframe_local_offset + .Lframe_extra
682 .set .Lframe_regcount, -1
687 * Set SCTLR_EL1 to the passed value, and invalidate the local icache
688 * in the process. This is called when setting the MMU on.
690 .macro set_sctlr_el1, reg
694 * Invalidate the local I-cache so that any instructions fetched
695 * speculatively from the PoC are discarded, since they may have
696 * been dynamically patched at the PoU.
704 * Check whether preempt-disabled code should yield as soon as it
705 * is able. This is the case if re-enabling preemption a single
706 * time results in a preempt count of zero, and the TIF_NEED_RESCHED
707 * flag is set. (Note that the latter is stored negated in the
708 * top word of the thread_info::preempt_count field)
710 .macro cond_yield, lbl:req, tmp:req
711 #ifdef CONFIG_PREEMPTION
712 get_current_task \tmp
713 ldr \tmp, [\tmp, #TSK_TI_PREEMPT]
714 sub \tmp, \tmp, #PREEMPT_DISABLE_OFFSET
720 * This macro emits a program property note section identifying
721 * architecture features which require special handling, mainly for
722 * use in assembly files included in the VDSO.
725 #define NT_GNU_PROPERTY_TYPE_0 5
726 #define GNU_PROPERTY_AARCH64_FEATURE_1_AND 0xc0000000
728 #define GNU_PROPERTY_AARCH64_FEATURE_1_BTI (1U << 0)
729 #define GNU_PROPERTY_AARCH64_FEATURE_1_PAC (1U << 1)
731 #ifdef CONFIG_ARM64_BTI_KERNEL
732 #define GNU_PROPERTY_AARCH64_FEATURE_1_DEFAULT \
733 ((GNU_PROPERTY_AARCH64_FEATURE_1_BTI | \
734 GNU_PROPERTY_AARCH64_FEATURE_1_PAC))
737 #ifdef GNU_PROPERTY_AARCH64_FEATURE_1_DEFAULT
738 .macro emit_aarch64_feature_1_and, feat=GNU_PROPERTY_AARCH64_FEATURE_1_DEFAULT
739 .pushsection .note.gnu.property, "a"
743 .long NT_GNU_PROPERTY_TYPE_0
747 3: .long GNU_PROPERTY_AARCH64_FEATURE_1_AND
751 * This is described with an array of char in the Linux API
752 * spec but the text and all other usage (including binutils,
753 * clang and GCC) treat this as a 32 bit value so no swizzling
754 * is required for big endian.
764 .macro emit_aarch64_feature_1_and, feat=0
767 #endif /* GNU_PROPERTY_AARCH64_FEATURE_1_DEFAULT */
769 #endif /* __ASM_ASSEMBLER_H */