Skip to content

Commit fd86365

Browse files
ardbiesheuvelgregkh
authored andcommitted
ARM: assembler: introduce adr_l, ldr_l and str_l macros
commit 0b16746 upstream. Like arm64, ARM supports position independent code sequences that produce symbol references with a greater reach than the ordinary adr/ldr instructions. Since on ARM, the adrl pseudo-instruction is only supported in ARM mode (and not at all when using Clang), having a adr_l macro like we do on arm64 is useful, and increases symmetry as well. Currently, we use open coded instruction sequences involving literals and arithmetic operations. Instead, we can use movw/movt pairs on v7 CPUs, circumventing the D-cache entirely. E.g., on v7+ CPUs, we can emit a PC-relative reference as follows: movw <reg>, #:lower16:<sym> - (1f + 8) movt <reg>, #:upper16:<sym> - (1f + 8) 1: add <reg>, <reg>, pc For older CPUs, we can emit the literal into a subsection, allowing it to be emitted out of line while retaining the ability to perform arithmetic on label offsets. E.g., on pre-v7 CPUs, we can emit a PC-relative reference as follows: ldr <reg>, 2f 1: add <reg>, <reg>, pc .subsection 1 2: .long <sym> - (1b + 8) .previous This is allowed by the assembler because, unlike ordinary sections, subsections are combined into a single section in the object file, and so the label references are not true cross-section references that are visible as relocations. (Subsections have been available in binutils since 2004 at least, so they should not cause any issues with older toolchains.) So use the above to implement the macros mov_l, adr_l, ldr_l and str_l, all of which will use movw/movt pairs on v7 and later CPUs, and use PC-relative literals otherwise. Reviewed-by: Nicolas Pitre <nico@fluxnic.net> Reviewed-by: Linus Walleij <linus.walleij@linaro.org> Signed-off-by: Ard Biesheuvel <ardb@kernel.org> Signed-off-by: Nick Desaulniers <ndesaulniers@google.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 917220f commit fd86365

1 file changed

Lines changed: 84 additions & 0 deletions

File tree

‎arch/arm/include/asm/assembler.h‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,4 +494,88 @@ THUMB( orr \reg , \reg , #PSR_T_BIT )
494494
#define _ASM_NOKPROBE(entry)
495495
#endif
496496

497+
.macro __adldst_l, op, reg, sym, tmp, c
498+
.if __LINUX_ARM_ARCH__ < 7
499+
ldr\c \tmp, .La\@
500+
.subsection 1
501+
.align 2
502+
.La\@: .long \sym - .Lpc\@
503+
.previous
504+
.else
505+
.ifnb \c
506+
THUMB( ittt \c )
507+
.endif
508+
movw\c \tmp, #:lower16:\sym - .Lpc\@
509+
movt\c \tmp, #:upper16:\sym - .Lpc\@
510+
.endif
511+
512+
#ifndef CONFIG_THUMB2_KERNEL
513+
.set .Lpc\@, . + 8 // PC bias
514+
.ifc \op, add
515+
add\c \reg, \tmp, pc
516+
.else
517+
\op\c \reg, [pc, \tmp]
518+
.endif
519+
#else
520+
.Lb\@: add\c \tmp, \tmp, pc
521+
/*
522+
* In Thumb-2 builds, the PC bias depends on whether we are currently
523+
* emitting into a .arm or a .thumb section. The size of the add opcode
524+
* above will be 2 bytes when emitting in Thumb mode and 4 bytes when
525+
* emitting in ARM mode, so let's use this to account for the bias.
526+
*/
527+
.set .Lpc\@, . + (. - .Lb\@)
528+
529+
.ifnc \op, add
530+
\op\c \reg, [\tmp]
531+
.endif
532+
#endif
533+
.endm
534+
535+
/*
536+
* mov_l - move a constant value or [relocated] address into a register
537+
*/
538+
.macro mov_l, dst:req, imm:req
539+
.if __LINUX_ARM_ARCH__ < 7
540+
ldr \dst, =\imm
541+
.else
542+
movw \dst, #:lower16:\imm
543+
movt \dst, #:upper16:\imm
544+
.endif
545+
.endm
546+
547+
/*
548+
* adr_l - adr pseudo-op with unlimited range
549+
*
550+
* @dst: destination register
551+
* @sym: name of the symbol
552+
* @cond: conditional opcode suffix
553+
*/
554+
.macro adr_l, dst:req, sym:req, cond
555+
__adldst_l add, \dst, \sym, \dst, \cond
556+
.endm
557+
558+
/*
559+
* ldr_l - ldr <literal> pseudo-op with unlimited range
560+
*
561+
* @dst: destination register
562+
* @sym: name of the symbol
563+
* @cond: conditional opcode suffix
564+
*/
565+
.macro ldr_l, dst:req, sym:req, cond
566+
__adldst_l ldr, \dst, \sym, \dst, \cond
567+
.endm
568+
569+
/*
570+
* str_l - str <literal> pseudo-op with unlimited range
571+
*
572+
* @src: source register
573+
* @sym: name of the symbol
574+
* @tmp: mandatory scratch register
575+
* @cond: conditional opcode suffix
576+
*/
577+
.macro str_l, src:req, sym:req, tmp:req, cond
578+
__adldst_l str, \src, \sym, \tmp, \cond
579+
.endm
580+
497581
#endif /* __ASM_ASSEMBLER_H__ */

0 commit comments

Comments
 (0)