Skip to content

Commit be604c6

Browse files
keeswildea01
authored andcommitted
arm64: sysreg: Make mrs_s and msr_s macros work with Clang and LTO
Clang's integrated assembler does not allow assembly macros defined in one inline asm block using the .macro directive to be used across separate asm blocks. LLVM developers consider this a feature and not a bug, recommending code refactoring: https://bugs.llvm.org/show_bug.cgi?id=19749 As binutils doesn't allow macros to be redefined, this change uses UNDEFINE_MRS_S and UNDEFINE_MSR_S to define corresponding macros in-place and workaround gcc and clang limitations on redefining macros across different assembler blocks. Specifically, the current state after preprocessing looks like this: asm volatile(".macro mXX_s ... .endm"); void f() { asm volatile("mXX_s a, b"); } With GCC, it gives macro redefinition error because sysreg.h is included in multiple source files, and assembler code for all of them is later combined for LTO (I've seen an intermediate file with hundreds of identical definitions). With clang, it gives macro undefined error because clang doesn't allow sharing macros between inline asm statements. I also seem to remember catching another sort of undefined error with GCC due to reordering of macro definition asm statement and generated asm code for function that uses the macro. The solution with defining and undefining for each use, while certainly not elegant, satisfies both GCC and clang, LTO and non-LTO. Co-developed-by: Alex Matveev <alxmtvv@gmail.com> Co-developed-by: Yury Norov <ynorov@caviumnetworks.com> Co-developed-by: Sami Tolvanen <samitolvanen@google.com> Reviewed-by: Nick Desaulniers <ndesaulniers@google.com> Reviewed-by: Mark Rutland <mark.rutland@arm.com> Signed-off-by: Kees Cook <keescook@chromium.org> Signed-off-by: Will Deacon <will.deacon@arm.com>
1 parent 06a916f commit be604c6

3 files changed

Lines changed: 38 additions & 19 deletions

File tree

‎arch/arm64/include/asm/irqflags.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ static inline void arch_local_irq_enable(void)
4343
asm volatile(ALTERNATIVE(
4444
"msr daifclr, #2 // arch_local_irq_enable\n"
4545
"nop",
46-
"msr_s " __stringify(SYS_ICC_PMR_EL1) ",%0\n"
46+
__msr_s(SYS_ICC_PMR_EL1, "%0")
4747
"dsb sy",
4848
ARM64_HAS_IRQ_PRIO_MASKING)
4949
:
@@ -55,7 +55,7 @@ static inline void arch_local_irq_disable(void)
5555
{
5656
asm volatile(ALTERNATIVE(
5757
"msr daifset, #2 // arch_local_irq_disable",
58-
"msr_s " __stringify(SYS_ICC_PMR_EL1) ", %0",
58+
__msr_s(SYS_ICC_PMR_EL1, "%0"),
5959
ARM64_HAS_IRQ_PRIO_MASKING)
6060
:
6161
: "r" ((unsigned long) GIC_PRIO_IRQOFF)
@@ -86,7 +86,7 @@ static inline unsigned long arch_local_save_flags(void)
8686
"mov %0, %1\n"
8787
"nop\n"
8888
"nop",
89-
"mrs_s %0, " __stringify(SYS_ICC_PMR_EL1) "\n"
89+
__mrs_s("%0", SYS_ICC_PMR_EL1)
9090
"ands %1, %1, " __stringify(PSR_I_BIT) "\n"
9191
"csel %0, %0, %2, eq",
9292
ARM64_HAS_IRQ_PRIO_MASKING)
@@ -116,7 +116,7 @@ static inline void arch_local_irq_restore(unsigned long flags)
116116
asm volatile(ALTERNATIVE(
117117
"msr daif, %0\n"
118118
"nop",
119-
"msr_s " __stringify(SYS_ICC_PMR_EL1) ", %0\n"
119+
__msr_s(SYS_ICC_PMR_EL1, "%0")
120120
"dsb sy",
121121
ARM64_HAS_IRQ_PRIO_MASKING)
122122
: "+r" (flags)

‎arch/arm64/include/asm/kvm_hyp.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
({ \
3131
u64 reg; \
3232
asm volatile(ALTERNATIVE("mrs %0, " __stringify(r##nvh),\
33-
"mrs_s %0, " __stringify(r##vh),\
33+
__mrs_s("%0", r##vh), \
3434
ARM64_HAS_VIRT_HOST_EXTN) \
3535
: "=r" (reg)); \
3636
reg; \
@@ -40,7 +40,7 @@
4040
do { \
4141
u64 __val = (u64)(v); \
4242
asm volatile(ALTERNATIVE("msr " __stringify(r##nvh) ", %x0",\
43-
"msr_s " __stringify(r##vh) ", %x0",\
43+
__msr_s(r##vh, "%x0"), \
4444
ARM64_HAS_VIRT_HOST_EXTN) \
4545
: : "rZ" (__val)); \
4646
} while (0)

‎arch/arm64/include/asm/sysreg.h‎

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -760,20 +760,39 @@
760760
#include <linux/build_bug.h>
761761
#include <linux/types.h>
762762

763-
asm(
764-
" .irp num,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\n"
765-
" .equ .L__reg_num_x\\num, \\num\n"
766-
" .endr\n"
763+
#define __DEFINE_MRS_MSR_S_REGNUM \
764+
" .irp num,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\n" \
765+
" .equ .L__reg_num_x\\num, \\num\n" \
766+
" .endr\n" \
767767
" .equ .L__reg_num_xzr, 31\n"
768-
"\n"
769-
" .macro mrs_s, rt, sreg\n"
770-
__emit_inst(0xd5200000|(\\sreg)|(.L__reg_num_\\rt))
768+
769+
#define DEFINE_MRS_S \
770+
__DEFINE_MRS_MSR_S_REGNUM \
771+
" .macro mrs_s, rt, sreg\n" \
772+
__emit_inst(0xd5200000|(\\sreg)|(.L__reg_num_\\rt)) \
771773
" .endm\n"
772-
"\n"
773-
" .macro msr_s, sreg, rt\n"
774-
__emit_inst(0xd5000000|(\\sreg)|(.L__reg_num_\\rt))
774+
775+
#define DEFINE_MSR_S \
776+
__DEFINE_MRS_MSR_S_REGNUM \
777+
" .macro msr_s, sreg, rt\n" \
778+
__emit_inst(0xd5000000|(\\sreg)|(.L__reg_num_\\rt)) \
775779
" .endm\n"
776-
);
780+
781+
#define UNDEFINE_MRS_S \
782+
" .purgem mrs_s\n"
783+
784+
#define UNDEFINE_MSR_S \
785+
" .purgem msr_s\n"
786+
787+
#define __mrs_s(v, r) \
788+
DEFINE_MRS_S \
789+
" mrs_s " v ", " __stringify(r) "\n" \
790+
UNDEFINE_MRS_S
791+
792+
#define __msr_s(r, v) \
793+
DEFINE_MSR_S \
794+
" msr_s " __stringify(r) ", " v "\n" \
795+
UNDEFINE_MSR_S
777796

778797
/*
779798
* Unlike read_cpuid, calls to read_sysreg are never expected to be
@@ -801,13 +820,13 @@ asm(
801820
*/
802821
#define read_sysreg_s(r) ({ \
803822
u64 __val; \
804-
asm volatile("mrs_s %0, " __stringify(r) : "=r" (__val)); \
823+
asm volatile(__mrs_s("%0", r) : "=r" (__val)); \
805824
__val; \
806825
})
807826

808827
#define write_sysreg_s(v, r) do { \
809828
u64 __val = (u64)(v); \
810-
asm volatile("msr_s " __stringify(r) ", %x0" : : "rZ" (__val)); \
829+
asm volatile(__msr_s(r, "%x0") : : "rZ" (__val)); \
811830
} while (0)
812831

813832
/*

0 commit comments

Comments
 (0)