Re: [PATCH v3] arm64: sysreg: make mrs_s and msr_s macros work with Clang and LTO
- Date: Mon, 15 Apr 2019 18:06:08 +0100
- From: Mark Rutland <mark.rutland@xxxxxxx>
- Subject: Re: [PATCH v3] arm64: sysreg: make mrs_s and msr_s macros work with Clang and LTO
On Mon, Apr 15, 2019 at 07:38:21AM -0700, Kees Cook wrote:
> From: Alex Matveev <alxmtvv@xxxxxxxxx>
> 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:
> 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.
> Signed-off-by: Alex Matveev <alxmtvv@xxxxxxxxx>
> Signed-off-by: Yury Norov <ynorov@xxxxxxxxxxxxxxxxxx>
> Signed-off-by: Sami Tolvanen <samitolvanen@xxxxxxxxxx>
> Signed-off-by: Kees Cook <keescook@xxxxxxxxxxxx>
> v3: split out patch as stand-alone, added more uses in irqflags,
> updated commit log, based on discussion in
> arch/arm64/include/asm/irqflags.h | 12 +++++--
> arch/arm64/include/asm/kvm_hyp.h | 8 +++--
> arch/arm64/include/asm/sysreg.h | 55 +++++++++++++++++++++----------
> 3 files changed, 53 insertions(+), 22 deletions(-)
> diff --git a/arch/arm64/include/asm/irqflags.h b/arch/arm64/include/asm/irqflags.h
> index 43d8366c1e87..06d3987d1546 100644
> --- a/arch/arm64/include/asm/irqflags.h
> +++ b/arch/arm64/include/asm/irqflags.h
> @@ -43,7 +43,9 @@ static inline void arch_local_irq_enable(void)
> asm volatile(ALTERNATIVE(
> "msr daifclr, #2 // arch_local_irq_enable\n"
> + DEFINE_MSR_S
> "msr_s " __stringify(SYS_ICC_PMR_EL1) ",%0\n"
> + UNDEFINE_MSR_S
If we do need this, can we wrap this in a larger CPP macro that does the
whole sequence of defining, using, and undefining the asm macros?
It would be nice if we could simply rely on a more recent binutils these
days, which supports the generic S<op0>_<op1>_<cn>_<Cm>_<op2> sysreg
definition. That would mean we could get rid of the whole msr_s/mrs_s
hack by turning that into a CPP macro which built that name.
It looks like binutils has been able to do that since September 2014...
Are folk using toolchains older than that to compile kernels?