1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * arch/arm/include/asm/assembler.h
5 * Copyright (C) 1996-2000 Russell King
7 * This file contains arm architecture specific defines
8 * for the different processors.
10 * Do not include any C declarations in this file - it is included by
13 #ifndef __ASM_ASSEMBLER_H__
14 #define __ASM_ASSEMBLER_H__
17 #error "Only include this from assembly code"
20 #include <asm/ptrace.h>
21 #include <asm/opcodes-virt.h>
22 #include <asm/asm-offsets.h>
24 #include <asm/thread_info.h>
25 #include <asm/uaccess-asm.h>
30 * Endian independent macros for shifting bytes within registers.
35 #define get_byte_0 lsl #0
36 #define get_byte_1 lsr #8
37 #define get_byte_2 lsr #16
38 #define get_byte_3 lsr #24
39 #define put_byte_0 lsl #0
40 #define put_byte_1 lsl #8
41 #define put_byte_2 lsl #16
42 #define put_byte_3 lsl #24
46 #define get_byte_0 lsr #24
47 #define get_byte_1 lsr #16
48 #define get_byte_2 lsr #8
49 #define get_byte_3 lsl #0
50 #define put_byte_0 lsl #24
51 #define put_byte_1 lsl #16
52 #define put_byte_2 lsl #8
53 #define put_byte_3 lsl #0
56 /* Select code for any configuration running in BE8 mode */
57 #ifdef CONFIG_CPU_ENDIAN_BE8
58 #define ARM_BE8(code...) code
60 #define ARM_BE8(code...)
64 * Data preload for architectures that support it
66 #if __LINUX_ARM_ARCH__ >= 5
67 #define PLD(code...) code
73 * This can be used to enable code to cacheline align the destination
74 * pointer when bulk writing to memory. Experiments on StrongARM and
75 * XScale didn't show this a worthwhile thing to do when the cache is not
76 * set to write-allocate (this would need further testing on XScale when WA
79 * On Feroceon there is much to gain however, regardless of cache mode.
81 #ifdef CONFIG_CPU_FEROCEON
82 #define CALGN(code...) code
84 #define CALGN(code...)
87 #define IMM12_MASK 0xfff
89 /* the frame pointer used for stack unwinding */
91 THUMB( fpreg .req r7 )
94 * Enable and disable interrupts
96 #if __LINUX_ARM_ARCH__ >= 6
97 .macro disable_irq_notrace
101 .macro enable_irq_notrace
105 .macro disable_irq_notrace
106 msr cpsr_c, #PSR_I_BIT | SVC_MODE
109 .macro enable_irq_notrace
110 msr cpsr_c, #SVC_MODE
114 #if __LINUX_ARM_ARCH__ < 7
116 mcr p15, 0, r0, c7, c10, 4
120 mcr p15, 0, r0, c7, c5, 4
124 .macro asm_trace_hardirqs_off, save=1
125 #if defined(CONFIG_TRACE_IRQFLAGS)
127 stmdb sp!, {r0-r3, ip, lr}
129 bl trace_hardirqs_off
131 ldmia sp!, {r0-r3, ip, lr}
136 .macro asm_trace_hardirqs_on, cond=al, save=1
137 #if defined(CONFIG_TRACE_IRQFLAGS)
139 * actually the registers should be pushed and pop'd conditionally, but
140 * after bl the flags are certainly clobbered
143 stmdb sp!, {r0-r3, ip, lr}
145 bl\cond trace_hardirqs_on
147 ldmia sp!, {r0-r3, ip, lr}
152 .macro disable_irq, save=1
154 asm_trace_hardirqs_off \save
158 asm_trace_hardirqs_on
162 * Save the current IRQ state and disable IRQs. Note that this macro
163 * assumes FIQs are enabled, and that the processor is in SVC mode.
165 .macro save_and_disable_irqs, oldcpsr
166 #ifdef CONFIG_CPU_V7M
167 mrs \oldcpsr, primask
174 .macro save_and_disable_irqs_notrace, oldcpsr
175 #ifdef CONFIG_CPU_V7M
176 mrs \oldcpsr, primask
184 * Restore interrupt state previously stored in a register. We don't
185 * guarantee that this will preserve the flags.
187 .macro restore_irqs_notrace, oldcpsr
188 #ifdef CONFIG_CPU_V7M
189 msr primask, \oldcpsr
195 .macro restore_irqs, oldcpsr
196 tst \oldcpsr, #PSR_I_BIT
197 asm_trace_hardirqs_on cond=eq
198 restore_irqs_notrace \oldcpsr
202 * Assembly version of "adr rd, BSYM(sym)". This should only be used to
203 * reference local symbols in the same assembly file which are to be
204 * resolved by the assembler. Other usage is undefined.
206 .irp c,,eq,ne,cs,cc,mi,pl,vs,vc,hi,ls,ge,lt,gt,le,hs,lo
207 .macro badr\c, rd, sym
208 #ifdef CONFIG_THUMB2_KERNEL
217 * Get current thread_info.
219 .macro get_thread_info, rd
220 /* thread_info is the first member of struct task_struct */
225 * Increment/decrement the preempt count.
227 #ifdef CONFIG_PREEMPT_COUNT
228 .macro inc_preempt_count, ti, tmp
229 ldr \tmp, [\ti, #TI_PREEMPT] @ get preempt count
230 add \tmp, \tmp, #1 @ increment it
231 str \tmp, [\ti, #TI_PREEMPT]
234 .macro dec_preempt_count, ti, tmp
235 ldr \tmp, [\ti, #TI_PREEMPT] @ get preempt count
236 sub \tmp, \tmp, #1 @ decrement it
237 str \tmp, [\ti, #TI_PREEMPT]
240 .macro dec_preempt_count_ti, ti, tmp
242 dec_preempt_count \ti, \tmp
245 .macro inc_preempt_count, ti, tmp
248 .macro dec_preempt_count, ti, tmp
251 .macro dec_preempt_count_ti, ti, tmp
255 #define USERL(l, x...) \
257 .pushsection __ex_table,"a"; \
262 #define USER(x...) USERL(9001f, x)
265 #define ALT_SMP(instr...) \
268 * Note: if you get assembler errors from ALT_UP() when building with
269 * CONFIG_THUMB2_KERNEL, you almost certainly need to use
270 * ALT_SMP( W(instr) ... )
272 #define ALT_UP(instr...) \
273 .pushsection ".alt.smp.init", "a" ;\
277 .if . - 9997b == 2 ;\
280 .if . - 9997b != 4 ;\
281 .error "ALT_UP() content must assemble to exactly 4 bytes";\
284 #define ALT_UP_B(label) \
285 .pushsection ".alt.smp.init", "a" ;\
288 W(b) . + (label - 9998b) ;\
291 #define ALT_SMP(instr...)
292 #define ALT_UP(instr...) instr
293 #define ALT_UP_B(label) b label
297 * this_cpu_offset - load the per-CPU offset of this CPU into
300 .macro this_cpu_offset, rd:req
302 ALT_SMP(mrc p15, 0, \rd, c13, c0, 4)
307 .L1_\@: ldr_va \rd, __per_cpu_offset
317 * set_current - store the task pointer of this CPU's current task
319 .macro set_current, rn:req, tmp:req
320 #if defined(CONFIG_CURRENT_POINTER_IN_TPIDRURO) || defined(CONFIG_SMP)
321 9998: mcr p15, 0, \rn, c13, c0, 3 @ set TPIDRURO register
325 .L0_\@: str_va \rn, __current, \tmp
331 str_va \rn, __current, \tmp
336 * get_current - load the task pointer of this CPU's current task
338 .macro get_current, rd:req
339 #if defined(CONFIG_CURRENT_POINTER_IN_TPIDRURO) || defined(CONFIG_SMP)
340 9998: mrc p15, 0, \rd, c13, c0, 3 @ get TPIDRURO register
344 .L0_\@: ldr_va \rd, __current
350 ldr_va \rd, __current
355 * reload_current - reload the task pointer of this CPU's current task
356 * into the TLS register
358 .macro reload_current, t1:req, t2:req
359 #if defined(CONFIG_CURRENT_POINTER_IN_TPIDRURO) || defined(CONFIG_SMP)
364 ldr_this_cpu \t1, __entry_task, \t1, \t2
365 mcr p15, 0, \t1, c13, c0, 3 @ store in TPIDRURO
371 * Instruction barrier
374 #if __LINUX_ARM_ARCH__ >= 7
376 #elif __LINUX_ARM_ARCH__ == 6
377 mcr p15, 0, r0, c7, c5, 4
382 * SMP data memory barrier
386 #if __LINUX_ARM_ARCH__ >= 7
392 #elif __LINUX_ARM_ARCH__ == 6
393 ALT_SMP(mcr p15, 0, r0, c7, c10, 5) @ dmb
395 #error Incompatible SMP platform
406 * Raw SMP data memory barrier
408 .macro __smp_dmb mode
409 #if __LINUX_ARM_ARCH__ >= 7
415 #elif __LINUX_ARM_ARCH__ == 6
416 mcr p15, 0, r0, c7, c10, 5 @ dmb
418 .error "Incompatible SMP platform"
422 #if defined(CONFIG_CPU_V7M)
424 * setmode is used to assert to be in svc mode during boot. For v7-M
425 * this is done in __v7m_setup, so setmode can be empty here.
427 .macro setmode, mode, reg
429 #elif defined(CONFIG_THUMB2_KERNEL)
430 .macro setmode, mode, reg
435 .macro setmode, mode, reg
441 * Helper macro to enter SVC mode cleanly and mask interrupts. reg is
442 * a scratch register for the macro to overwrite.
444 * This macro is intended for forcing the CPU into SVC mode at boot time.
445 * you cannot return to the original mode.
447 .macro safe_svcmode_maskall reg:req
448 #if __LINUX_ARM_ARCH__ >= 6 && !defined(CONFIG_CPU_V7M)
450 eor \reg, \reg, #HYP_MODE
452 bic \reg , \reg , #MODE_MASK
453 orr \reg , \reg , #PSR_I_BIT | PSR_F_BIT | SVC_MODE
454 THUMB( orr \reg , \reg , #PSR_T_BIT )
456 orr \reg, \reg, #PSR_A_BIT
465 * workaround for possibly broken pre-v6 hardware
466 * (akita, Sharp Zaurus C-1000, PXA270-based)
468 setmode PSR_F_BIT | PSR_I_BIT | SVC_MODE, \reg
473 * STRT/LDRT access macros with ARM and Thumb-2 variants
475 #ifdef CONFIG_THUMB2_KERNEL
477 .macro usraccoff, instr, reg, ptr, inc, off, cond, abort, t=TUSER()
480 \instr\()b\t\cond\().w \reg, [\ptr, #\off]
482 \instr\t\cond\().w \reg, [\ptr, #\off]
484 .error "Unsupported inc macro argument"
487 .pushsection __ex_table,"a"
493 .macro usracc, instr, reg, ptr, inc, cond, rept, abort
494 @ explicit IT instruction needed because of the label
495 @ introduced by the USER macro
502 .error "Unsupported rept macro argument"
506 @ Slightly optimised to avoid incrementing the pointer twice
507 usraccoff \instr, \reg, \ptr, \inc, 0, \cond, \abort
509 usraccoff \instr, \reg, \ptr, \inc, \inc, \cond, \abort
512 add\cond \ptr, #\rept * \inc
515 #else /* !CONFIG_THUMB2_KERNEL */
517 .macro usracc, instr, reg, ptr, inc, cond, rept, abort, t=TUSER()
521 \instr\()b\t\cond \reg, [\ptr], #\inc
523 \instr\t\cond \reg, [\ptr], #\inc
525 .error "Unsupported inc macro argument"
528 .pushsection __ex_table,"a"
535 #endif /* CONFIG_THUMB2_KERNEL */
537 .macro strusr, reg, ptr, inc, cond=al, rept=1, abort=9001f
538 usracc str, \reg, \ptr, \inc, \cond, \rept, \abort
541 .macro ldrusr, reg, ptr, inc, cond=al, rept=1, abort=9001f
542 usracc ldr, \reg, \ptr, \inc, \cond, \rept, \abort
545 /* Utility macro for declaring string literals */
546 .macro string name:req, string
547 .type \name , #object
550 .size \name , . - \name
553 .irp c,,eq,ne,cs,cc,mi,pl,vs,vc,hi,ls,ge,lt,gt,le,hs,lo
555 #if __LINUX_ARM_ARCH__ < 6
569 #ifdef CONFIG_THUMB2_KERNEL
574 .macro bug, msg, line
575 #ifdef CONFIG_THUMB2_KERNEL
580 #ifdef CONFIG_DEBUG_BUGVERBOSE
581 .pushsection .rodata.str, "aMS", %progbits, 1
584 .pushsection __bug_table, "aw"
592 #ifdef CONFIG_KPROBES
593 #define _ASM_NOKPROBE(entry) \
594 .pushsection "_kprobe_blacklist", "aw" ; \
599 #define _ASM_NOKPROBE(entry)
602 .macro __adldst_l, op, reg, sym, tmp, c
603 .if __LINUX_ARM_ARCH__ < 7
607 .La\@: .long \sym - .Lpc\@
613 movw\c \tmp, #:lower16:\sym - .Lpc\@
614 movt\c \tmp, #:upper16:\sym - .Lpc\@
617 #ifndef CONFIG_THUMB2_KERNEL
618 .set .Lpc\@, . + 8 // PC bias
622 \op\c \reg, [pc, \tmp]
625 .Lb\@: add\c \tmp, \tmp, pc
627 * In Thumb-2 builds, the PC bias depends on whether we are currently
628 * emitting into a .arm or a .thumb section. The size of the add opcode
629 * above will be 2 bytes when emitting in Thumb mode and 4 bytes when
630 * emitting in ARM mode, so let's use this to account for the bias.
632 .set .Lpc\@, . + (. - .Lb\@)
641 * mov_l - move a constant value or [relocated] address into a register
643 .macro mov_l, dst:req, imm:req, cond
644 .if __LINUX_ARM_ARCH__ < 7
647 movw\cond \dst, #:lower16:\imm
648 movt\cond \dst, #:upper16:\imm
653 * adr_l - adr pseudo-op with unlimited range
655 * @dst: destination register
656 * @sym: name of the symbol
657 * @cond: conditional opcode suffix
659 .macro adr_l, dst:req, sym:req, cond
660 __adldst_l add, \dst, \sym, \dst, \cond
664 * ldr_l - ldr <literal> pseudo-op with unlimited range
666 * @dst: destination register
667 * @sym: name of the symbol
668 * @cond: conditional opcode suffix
670 .macro ldr_l, dst:req, sym:req, cond
671 __adldst_l ldr, \dst, \sym, \dst, \cond
675 * str_l - str <literal> pseudo-op with unlimited range
677 * @src: source register
678 * @sym: name of the symbol
679 * @tmp: mandatory scratch register
680 * @cond: conditional opcode suffix
682 .macro str_l, src:req, sym:req, tmp:req, cond
683 __adldst_l str, \src, \sym, \tmp, \cond
686 .macro __ldst_va, op, reg, tmp, sym, cond, offset
687 #if __LINUX_ARM_ARCH__ >= 7 || \
688 !defined(CONFIG_ARM_HAS_GROUP_RELOCS) || \
689 (defined(MODULE) && defined(CONFIG_ARM_MODULE_PLTS))
690 mov_l \tmp, \sym, \cond
693 * Avoid a literal load, by emitting a sequence of ADD/LDR instructions
694 * with the appropriate relocations. The combined sequence has a range
695 * of -/+ 256 MiB, which should be sufficient for the core kernel and
696 * for modules loaded into the module region.
699 .reloc .L0_\@, R_ARM_ALU_PC_G0_NC, \sym
700 .reloc .L1_\@, R_ARM_ALU_PC_G1_NC, \sym
701 .reloc .L2_\@, R_ARM_LDR_PC_G2, \sym
702 .L0_\@: sub\cond \tmp, pc, #8 - \offset
703 .L1_\@: sub\cond \tmp, \tmp, #4 - \offset
706 \op\cond \reg, [\tmp, #\offset]
710 * ldr_va - load a 32-bit word from the virtual address of \sym
712 .macro ldr_va, rd:req, sym:req, cond, tmp, offset=0
714 __ldst_va ldr, \rd, \tmp, \sym, \cond, \offset
716 __ldst_va ldr, \rd, \rd, \sym, \cond, \offset
721 * str_va - store a 32-bit word to the virtual address of \sym
723 .macro str_va, rn:req, sym:req, tmp:req, cond
724 __ldst_va str, \rn, \tmp, \sym, \cond, 0
728 * ldr_this_cpu_armv6 - Load a 32-bit word from the per-CPU variable 'sym',
729 * without using a temp register. Supported in ARM mode
732 .macro ldr_this_cpu_armv6, rd:req, sym:req
735 .reloc .L0_\@, R_ARM_ALU_PC_G0_NC, \sym
736 .reloc .L1_\@, R_ARM_ALU_PC_G1_NC, \sym
737 .reloc .L2_\@, R_ARM_LDR_PC_G2, \sym
739 .L0_\@: sub \rd, \rd, #4
740 .L1_\@: sub \rd, \rd, #0
741 .L2_\@: ldr \rd, [\rd, #4]
745 * ldr_this_cpu - Load a 32-bit word from the per-CPU variable 'sym'
746 * into register 'rd', which may be the stack pointer,
747 * using 't1' and 't2' as general temp registers. These
748 * are permitted to overlap with 'rd' if != sp
750 .macro ldr_this_cpu, rd:req, sym:req, t1:req, t2:req
752 ldr_va \rd, \sym, tmp=\t1
753 #elif __LINUX_ARM_ARCH__ >= 7 || \
754 !defined(CONFIG_ARM_HAS_GROUP_RELOCS) || \
755 (defined(MODULE) && defined(CONFIG_ARM_MODULE_PLTS))
760 ldr_this_cpu_armv6 \rd, \sym
765 * rev_l - byte-swap a 32-bit value
767 * @val: source/destination register
768 * @tmp: scratch register
770 .macro rev_l, val:req, tmp:req
771 .if __LINUX_ARM_ARCH__ < 6
772 eor \tmp, \val, \val, ror #16
773 bic \tmp, \tmp, #0x00ff0000
774 mov \val, \val, ror #8
775 eor \val, \val, \tmp, lsr #8
782 * bl_r - branch and link to register
784 * @dst: target to branch to
785 * @c: conditional opcode suffix
787 .macro bl_r, dst:req, c
788 .if __LINUX_ARM_ARCH__ < 6
796 #endif /* __ASM_ASSEMBLER_H__ */