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/asm-bug.h>
19 #include <asm/cpufeature.h>
20 #include <asm/cputype.h>
21 #include <asm/debug-monitors.h>
23 #include <asm/pgtable-hwdef.h>
24 #include <asm/ptrace.h>
25 #include <asm/thread_info.h>
27 .macro save_and_disable_daif, flags
40 .macro restore_daif, flags:req
44 /* IRQ is the lowest priority flag, unconditionally unmask the rest. */
46 msr daifclr, #(8 | 4 | 1)
50 * Save/restore interrupts.
52 .macro save_and_disable_irq, flags
57 .macro restore_irq, flags
65 .macro disable_step_tsk, flgs, tmp
66 tbz \flgs, #TIF_SINGLESTEP, 9990f
68 bic \tmp, \tmp, #DBG_MDSCR_SS
70 isb // Synchronise with enable_dbg
74 /* call with daif masked */
75 .macro enable_step_tsk, flgs, tmp
76 tbz \flgs, #TIF_SINGLESTEP, 9990f
78 orr \tmp, \tmp, #DBG_MDSCR_SS
84 * RAS Error Synchronization barrier
87 #ifdef CONFIG_ARM64_RAS_EXTN
95 * Value prediction barrier
102 * Speculation barrier
105 alternative_if_not ARM64_HAS_SB
124 * Emit an entry into the exception table
126 .macro _asm_extable, from, to
127 .pushsection __ex_table, "a"
129 .long (\from - .), (\to - .)
133 #define USER(l, x...) \
135 _asm_extable 9999b, l
140 lr .req x30 // link register
151 * Select code when configured for BE.
153 #ifdef CONFIG_CPU_BIG_ENDIAN
154 #define CPU_BE(code...) code
156 #define CPU_BE(code...)
160 * Select code when configured for LE.
162 #ifdef CONFIG_CPU_BIG_ENDIAN
163 #define CPU_LE(code...)
165 #define CPU_LE(code...) code
169 * Define a macro that constructs a 64-bit value by concatenating two
170 * 32-bit registers. Note that on big endian systems the order of the
171 * registers is swapped.
173 #ifndef CONFIG_CPU_BIG_ENDIAN
174 .macro regs_to_64, rd, lbits, hbits
176 .macro regs_to_64, rd, hbits, lbits
178 orr \rd, \lbits, \hbits, lsl #32
182 * Pseudo-ops for PC-relative adr/ldr/str <reg>, <symbol> where
183 * <symbol> is within the range +/- 4 GB of the PC.
186 * @dst: destination register (64 bit wide)
187 * @sym: name of the symbol
189 .macro adr_l, dst, sym
191 add \dst, \dst, :lo12:\sym
195 * @dst: destination register (32 or 64 bit wide)
196 * @sym: name of the symbol
197 * @tmp: optional 64-bit scratch register to be used if <dst> is a
198 * 32-bit wide register, in which case it cannot be used to hold
201 .macro ldr_l, dst, sym, tmp=
204 ldr \dst, [\dst, :lo12:\sym]
207 ldr \dst, [\tmp, :lo12:\sym]
212 * @src: source register (32 or 64 bit wide)
213 * @sym: name of the symbol
214 * @tmp: mandatory 64-bit scratch register to calculate the address
215 * while <src> needs to be preserved.
217 .macro str_l, src, sym, tmp
219 str \src, [\tmp, :lo12:\sym]
223 * @dst: destination register
225 #if defined(__KVM_NVHE_HYPERVISOR__) || defined(__KVM_VHE_HYPERVISOR__)
226 .macro this_cpu_offset, dst
230 .macro this_cpu_offset, dst
231 alternative_if_not ARM64_HAS_VIRT_HOST_EXTN
240 * @dst: Result of per_cpu(sym, smp_processor_id()) (can be SP)
241 * @sym: The name of the per-cpu variable
242 * @tmp: scratch register
244 .macro adr_this_cpu, dst, sym, tmp
246 add \dst, \tmp, #:lo12:\sym
252 * @dst: Result of READ_ONCE(per_cpu(sym, smp_processor_id()))
253 * @sym: The name of the per-cpu variable
254 * @tmp: scratch register
256 .macro ldr_this_cpu dst, sym, tmp
259 ldr \dst, [\dst, \tmp]
263 * vma_vm_mm - get mm pointer from vma pointer (vma->vm_mm)
265 .macro vma_vm_mm, rd, rn
266 ldr \rd, [\rn, #VMA_VM_MM]
270 * read_ctr - read CTR_EL0. If the system has mismatched register fields,
271 * provide the system wide safe value from arm64_ftr_reg_ctrel0.sys_val
274 #ifndef __KVM_NVHE_HYPERVISOR__
275 alternative_if_not ARM64_MISMATCHED_CACHE_TYPE
276 mrs \reg, ctr_el0 // read CTR
279 ldr_l \reg, arm64_ftr_reg_ctrel0 + ARM64_FTR_SYSVAL
282 alternative_if_not ARM64_KVM_PROTECTED_MODE
284 alternative_else_nop_endif
285 alternative_cb kvm_compute_final_ctr_el0
287 movk \reg, #0, lsl #16
288 movk \reg, #0, lsl #32
289 movk \reg, #0, lsl #48
296 * raw_dcache_line_size - get the minimum D-cache line size on this CPU
297 * from the CTR register.
299 .macro raw_dcache_line_size, reg, tmp
300 mrs \tmp, ctr_el0 // read CTR
301 ubfm \tmp, \tmp, #16, #19 // cache line size encoding
302 mov \reg, #4 // bytes per word
303 lsl \reg, \reg, \tmp // actual cache line size
307 * dcache_line_size - get the safe D-cache line size across all CPUs
309 .macro dcache_line_size, reg, tmp
311 ubfm \tmp, \tmp, #16, #19 // cache line size encoding
312 mov \reg, #4 // bytes per word
313 lsl \reg, \reg, \tmp // actual cache line size
317 * raw_icache_line_size - get the minimum I-cache line size on this CPU
318 * from the CTR register.
320 .macro raw_icache_line_size, reg, tmp
321 mrs \tmp, ctr_el0 // read CTR
322 and \tmp, \tmp, #0xf // cache line size encoding
323 mov \reg, #4 // bytes per word
324 lsl \reg, \reg, \tmp // actual cache line size
328 * icache_line_size - get the safe I-cache line size across all CPUs
330 .macro icache_line_size, reg, tmp
332 and \tmp, \tmp, #0xf // cache line size encoding
333 mov \reg, #4 // bytes per word
334 lsl \reg, \reg, \tmp // actual cache line size
338 * tcr_set_t0sz - update TCR.T0SZ so that we can load the ID map
340 .macro tcr_set_t0sz, valreg, t0sz
341 bfi \valreg, \t0sz, #TCR_T0SZ_OFFSET, #TCR_TxSZ_WIDTH
345 * tcr_set_t1sz - update TCR.T1SZ
347 .macro tcr_set_t1sz, valreg, t1sz
348 bfi \valreg, \t1sz, #TCR_T1SZ_OFFSET, #TCR_TxSZ_WIDTH
352 * tcr_compute_pa_size - set TCR.(I)PS to the highest supported
353 * ID_AA64MMFR0_EL1.PARange value
355 * tcr: register with the TCR_ELx value to be updated
356 * pos: IPS or PS bitfield position
357 * tmp{0,1}: temporary registers
359 .macro tcr_compute_pa_size, tcr, pos, tmp0, tmp1
360 mrs \tmp0, ID_AA64MMFR0_EL1
361 // Narrow PARange to fit the PS field in TCR_ELx
362 ubfx \tmp0, \tmp0, #ID_AA64MMFR0_PARANGE_SHIFT, #3
363 mov \tmp1, #ID_AA64MMFR0_PARANGE_MAX
365 csel \tmp0, \tmp1, \tmp0, hi
366 bfi \tcr, \tmp0, \pos, #3
370 * Macro to perform a data cache maintenance for the interval
371 * [kaddr, kaddr + size)
373 * op: operation passed to dc instruction
374 * domain: domain used in dsb instruciton
375 * kaddr: starting virtual address of the region
376 * size: size of the region
377 * Corrupts: kaddr, size, tmp1, tmp2
379 .macro __dcache_op_workaround_clean_cache, op, kaddr
380 alternative_if_not ARM64_WORKAROUND_CLEAN_CACHE
387 .macro dcache_by_line_op op, domain, kaddr, size, tmp1, tmp2
388 dcache_line_size \tmp1, \tmp2
389 add \size, \kaddr, \size
391 bic \kaddr, \kaddr, \tmp2
394 __dcache_op_workaround_clean_cache \op, \kaddr
397 __dcache_op_workaround_clean_cache \op, \kaddr
400 sys 3, c7, c12, 1, \kaddr // dc cvap
403 sys 3, c7, c13, 1, \kaddr // dc cvadp
410 add \kaddr, \kaddr, \tmp1
417 * Macro to perform an instruction cache maintenance for the interval
420 * start, end: virtual addresses describing the region
421 * label: A label to branch to on user fault.
422 * Corrupts: tmp1, tmp2
424 .macro invalidate_icache_by_line start, end, tmp1, tmp2, label
425 icache_line_size \tmp1, \tmp2
427 bic \tmp2, \start, \tmp2
429 USER(\label, ic ivau, \tmp2) // invalidate I line PoU
430 add \tmp2, \tmp2, \tmp1
438 * reset_pmuserenr_el0 - reset PMUSERENR_EL0 if PMUv3 present
440 .macro reset_pmuserenr_el0, tmpreg
441 mrs \tmpreg, id_aa64dfr0_el1
442 sbfx \tmpreg, \tmpreg, #ID_AA64DFR0_PMUVER_SHIFT, #4
443 cmp \tmpreg, #1 // Skip if no PMU present
445 msr pmuserenr_el0, xzr // Disable PMU access from EL0
450 * reset_amuserenr_el0 - reset AMUSERENR_EL0 if AMUv1 present
452 .macro reset_amuserenr_el0, tmpreg
453 mrs \tmpreg, id_aa64pfr0_el1 // Check ID_AA64PFR0_EL1
454 ubfx \tmpreg, \tmpreg, #ID_AA64PFR0_AMU_SHIFT, #4
455 cbz \tmpreg, .Lskip_\@ // Skip if no AMU present
456 msr_s SYS_AMUSERENR_EL0, xzr // Disable AMU access from EL0
460 * copy_page - copy src to dest using temp registers t1-t8
462 .macro copy_page dest:req src:req t1:req t2:req t3:req t4:req t5:req t6:req t7:req t8:req
463 9998: ldp \t1, \t2, [\src]
464 ldp \t3, \t4, [\src, #16]
465 ldp \t5, \t6, [\src, #32]
466 ldp \t7, \t8, [\src, #48]
468 stnp \t1, \t2, [\dest]
469 stnp \t3, \t4, [\dest, #16]
470 stnp \t5, \t6, [\dest, #32]
471 stnp \t7, \t8, [\dest, #48]
472 add \dest, \dest, #64
473 tst \src, #(PAGE_SIZE - 1)
478 * Annotate a function as being unsuitable for kprobes.
480 #ifdef CONFIG_KPROBES
481 #define NOKPROBE(x) \
482 .pushsection "_kprobe_blacklist", "aw"; \
489 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
490 #define EXPORT_SYMBOL_NOKASAN(name)
492 #define EXPORT_SYMBOL_NOKASAN(name) EXPORT_SYMBOL(name)
496 * Emit a 64-bit absolute little endian symbol reference in a way that
497 * ensures that it will be resolved at build time, even when building a
498 * PIE binary. This requires cooperation from the linker script, which
499 * must emit the lo32/hi32 halves individually.
507 * mov_q - move an immediate constant into a 64-bit register using
508 * between 2 and 4 movz/movk instructions (depending on the
509 * magnitude and sign of the operand)
511 .macro mov_q, reg, val
512 .if (((\val) >> 31) == 0 || ((\val) >> 31) == 0x1ffffffff)
513 movz \reg, :abs_g1_s:\val
515 .if (((\val) >> 47) == 0 || ((\val) >> 47) == 0x1ffff)
516 movz \reg, :abs_g2_s:\val
518 movz \reg, :abs_g3:\val
519 movk \reg, :abs_g2_nc:\val
521 movk \reg, :abs_g1_nc:\val
523 movk \reg, :abs_g0_nc:\val
527 * Return the current task_struct.
529 .macro get_current_task, rd
534 * Offset ttbr1 to allow for 48-bit kernel VAs set with 52-bit PTRS_PER_PGD.
535 * orr is used as it can cover the immediate value (and is idempotent).
536 * In future this may be nop'ed out when dealing with 52-bit kernel VAs.
537 * ttbr: Value of ttbr to set, modified.
539 .macro offset_ttbr1, ttbr, tmp
540 #ifdef CONFIG_ARM64_VA_BITS_52
541 mrs_s \tmp, SYS_ID_AA64MMFR2_EL1
542 and \tmp, \tmp, #(0xf << ID_AA64MMFR2_LVA_SHIFT)
543 cbnz \tmp, .Lskipoffs_\@
544 orr \ttbr, \ttbr, #TTBR1_BADDR_4852_OFFSET
550 * Perform the reverse of offset_ttbr1.
551 * bic is used as it can cover the immediate value and, in future, won't need
552 * to be nop'ed out when dealing with 52-bit kernel VAs.
554 .macro restore_ttbr1, ttbr
555 #ifdef CONFIG_ARM64_VA_BITS_52
556 bic \ttbr, \ttbr, #TTBR1_BADDR_4852_OFFSET
561 * Arrange a physical address in a TTBR register, taking care of 52-bit
564 * phys: physical address, preserved
565 * ttbr: returns the TTBR value
567 .macro phys_to_ttbr, ttbr, phys
568 #ifdef CONFIG_ARM64_PA_BITS_52
569 orr \ttbr, \phys, \phys, lsr #46
570 and \ttbr, \ttbr, #TTBR_BADDR_MASK_52
576 .macro phys_to_pte, pte, phys
577 #ifdef CONFIG_ARM64_PA_BITS_52
579 * We assume \phys is 64K aligned and this is guaranteed by only
580 * supporting this configuration with 64K pages.
582 orr \pte, \phys, \phys, lsr #36
583 and \pte, \pte, #PTE_ADDR_MASK
589 .macro pte_to_phys, phys, pte
590 #ifdef CONFIG_ARM64_PA_BITS_52
591 ubfiz \phys, \pte, #(48 - 16 - 12), #16
592 bfxil \phys, \pte, #16, #32
593 lsl \phys, \phys, #16
595 and \phys, \pte, #PTE_ADDR_MASK
600 * tcr_clear_errata_bits - Clear TCR bits that trigger an errata on this CPU.
602 .macro tcr_clear_errata_bits, tcr, tmp1, tmp2
603 #ifdef CONFIG_FUJITSU_ERRATUM_010001
606 mov_q \tmp2, MIDR_FUJITSU_ERRATUM_010001_MASK
607 and \tmp1, \tmp1, \tmp2
608 mov_q \tmp2, MIDR_FUJITSU_ERRATUM_010001
612 mov_q \tmp2, TCR_CLEAR_FUJITSU_ERRATUM_010001
613 bic \tcr, \tcr, \tmp2
615 #endif /* CONFIG_FUJITSU_ERRATUM_010001 */
619 * Errata workaround prior to disable MMU. Insert an ISB immediately prior
620 * to executing the MSR that will change SCTLR_ELn[M] from a value of 1 to 0.
622 .macro pre_disable_mmu_workaround
623 #ifdef CONFIG_QCOM_FALKOR_ERRATUM_E1041
629 * frame_push - Push @regcount callee saved registers to the stack,
630 * starting at x19, as well as x29/x30, and set x29 to
631 * the new value of sp. Add @extra bytes of stack space
634 .macro frame_push, regcount:req, extra
635 __frame st, \regcount, \extra
639 * frame_pop - Pop the callee saved registers from the stack that were
640 * pushed in the most recent call to frame_push, as well
641 * as x29/x30 and any extra stack space that may have been
648 .macro __frame_regs, reg1, reg2, op, num
649 .if .Lframe_regcount == \num
650 \op\()r \reg1, [sp, #(\num + 1) * 8]
651 .elseif .Lframe_regcount > \num
652 \op\()p \reg1, \reg2, [sp, #(\num + 1) * 8]
656 .macro __frame, op, regcount, extra=0
658 .if (\regcount) < 0 || (\regcount) > 10
659 .error "regcount should be in the range [0 ... 10]"
661 .if ((\extra) % 16) != 0
662 .error "extra should be a multiple of 16 bytes"
664 .ifdef .Lframe_regcount
665 .if .Lframe_regcount != -1
666 .error "frame_push/frame_pop may not be nested"
669 .set .Lframe_regcount, \regcount
670 .set .Lframe_extra, \extra
671 .set .Lframe_local_offset, ((\regcount + 3) / 2) * 16
672 stp x29, x30, [sp, #-.Lframe_local_offset - .Lframe_extra]!
676 __frame_regs x19, x20, \op, 1
677 __frame_regs x21, x22, \op, 3
678 __frame_regs x23, x24, \op, 5
679 __frame_regs x25, x26, \op, 7
680 __frame_regs x27, x28, \op, 9
683 .if .Lframe_regcount == -1
684 .error "frame_push/frame_pop may not be nested"
686 ldp x29, x30, [sp], #.Lframe_local_offset + .Lframe_extra
687 .set .Lframe_regcount, -1
692 * Set SCTLR_ELx to the @reg value, and invalidate the local icache
693 * in the process. This is called when setting the MMU on.
695 .macro set_sctlr, sreg, reg
699 * Invalidate the local I-cache so that any instructions fetched
700 * speculatively from the PoC are discarded, since they may have
701 * been dynamically patched at the PoU.
708 .macro set_sctlr_el1, reg
709 set_sctlr sctlr_el1, \reg
712 .macro set_sctlr_el2, reg
713 set_sctlr sctlr_el2, \reg
717 * Check whether to yield to another runnable task from kernel mode NEON code
718 * (which runs with preemption disabled).
720 * if_will_cond_yield_neon
721 * // pre-yield patchup code
723 * // post-yield patchup code
724 * endif_yield_neon <label>
726 * where <label> is optional, and marks the point where execution will resume
727 * after a yield has been performed. If omitted, execution resumes right after
728 * the endif_yield_neon invocation. Note that the entire sequence, including
729 * the provided patchup code, will be omitted from the image if
730 * CONFIG_PREEMPTION is not defined.
732 * As a convenience, in the case where no patchup code is required, the above
733 * sequence may be abbreviated to
735 * cond_yield_neon <label>
737 * Note that the patchup code does not support assembler directives that change
738 * the output section, any use of such directives is undefined.
740 * The yield itself consists of the following:
741 * - Check whether the preempt count is exactly 1 and a reschedule is also
742 * needed. If so, calling of preempt_enable() in kernel_neon_end() will
743 * trigger a reschedule. If it is not the case, yielding is pointless.
744 * - Disable and re-enable kernel mode NEON, and branch to the yield fixup
747 * This macro sequence may clobber all CPU state that is not guaranteed by the
748 * AAPCS to be preserved across an ordinary function call.
751 .macro cond_yield_neon, lbl
752 if_will_cond_yield_neon
754 endif_yield_neon \lbl
757 .macro if_will_cond_yield_neon
758 #ifdef CONFIG_PREEMPTION
760 ldr x0, [x0, #TSK_TI_PREEMPT]
761 sub x0, x0, #PREEMPT_DISABLE_OFFSET
763 /* fall through to endif_yield_neon */
767 .section ".discard.cond_yield_neon", "ax"
771 .macro do_cond_yield_neon
776 .macro endif_yield_neon, lbl
787 * Check whether preempt-disabled code should yield as soon as it
788 * is able. This is the case if re-enabling preemption a single
789 * time results in a preempt count of zero, and the TIF_NEED_RESCHED
790 * flag is set. (Note that the latter is stored negated in the
791 * top word of the thread_info::preempt_count field)
793 .macro cond_yield, lbl:req, tmp:req
794 #ifdef CONFIG_PREEMPTION
795 get_current_task \tmp
796 ldr \tmp, [\tmp, #TSK_TI_PREEMPT]
797 sub \tmp, \tmp, #PREEMPT_DISABLE_OFFSET
803 * This macro emits a program property note section identifying
804 * architecture features which require special handling, mainly for
805 * use in assembly files included in the VDSO.
808 #define NT_GNU_PROPERTY_TYPE_0 5
809 #define GNU_PROPERTY_AARCH64_FEATURE_1_AND 0xc0000000
811 #define GNU_PROPERTY_AARCH64_FEATURE_1_BTI (1U << 0)
812 #define GNU_PROPERTY_AARCH64_FEATURE_1_PAC (1U << 1)
814 #ifdef CONFIG_ARM64_BTI_KERNEL
815 #define GNU_PROPERTY_AARCH64_FEATURE_1_DEFAULT \
816 ((GNU_PROPERTY_AARCH64_FEATURE_1_BTI | \
817 GNU_PROPERTY_AARCH64_FEATURE_1_PAC))
820 #ifdef GNU_PROPERTY_AARCH64_FEATURE_1_DEFAULT
821 .macro emit_aarch64_feature_1_and, feat=GNU_PROPERTY_AARCH64_FEATURE_1_DEFAULT
822 .pushsection .note.gnu.property, "a"
826 .long NT_GNU_PROPERTY_TYPE_0
830 3: .long GNU_PROPERTY_AARCH64_FEATURE_1_AND
834 * This is described with an array of char in the Linux API
835 * spec but the text and all other usage (including binutils,
836 * clang and GCC) treat this as a 32 bit value so no swizzling
837 * is required for big endian.
847 .macro emit_aarch64_feature_1_and, feat=0
850 #endif /* GNU_PROPERTY_AARCH64_FEATURE_1_DEFAULT */
852 #endif /* __ASM_ASSEMBLER_H */