diff mbox series

[1/2] arm64: unify asm-arch manipulation

Message ID 20230125182201.800076-2-mark.rutland@arm.com (mailing list archive)
State New, archived
Headers show
Series arm64: pointer auth cleanup | expand

Commit Message

Mark Rutland Jan. 25, 2023, 6:22 p.m. UTC
Assemblers will reject instructions not supported by a target
architecture version, and so we must explicitly tell the assembler the
latest architecture version for which we want to assemble instructions
from.

We've added a few AS_HAS_ARMV8_<N> definitions for this, in addition to
an inconsistently named AS_HAS_PAC definition, from which arm64's
top-level Makefile determines the architecture version that we intend to
target, and generates the `asm-arch` variable.

To make this a bit clearer and easier to maintain, this patch reworks
the Makefile to determine asm-arch in a single if-else-endif chain.
AS_HAS_PAC, which is defined when the assembler supports
`-march=armv8.3-a`, is renamed to AS_HAS_ARMV8_3.

As the logic for armv8.3-a is lifted out of the block handling pointer
authentication, `asm-arch` may now be set to armv8.3-a regardless of
whether support for pointer authentication is selected. This means that
it will be possible to assemble armv8.3-a instructions even if we didn't
intend to, but this is consistent with our handling of other
architecture versions, and the compiler won't generate armv8.3-a
instructions regardless.

For the moment there's no need for an CONFIG_AS_HAS_ARMV8_1, as the code
for LSE atomics and LDAPR use individual `.arch_extension` entries and
do not require the baseline asm arch to be bumped to armv8.1-a. The
other armv8.1-a features (e.g. PAN) do not require assembler support.

There should be no functional change as a result of this patch.

Signed-off-by: Mark Rutland <mark.rutland@arm.com>
Cc: Ard Biesheuvel <ardb@kernel.org>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Will Deacon <will@kernel.org>
---
 arch/arm64/Kconfig  |  4 ++--
 arch/arm64/Makefile | 37 ++++++++++++++++++-------------------
 2 files changed, 20 insertions(+), 21 deletions(-)

Comments

Ard Biesheuvel Jan. 26, 2023, 8:31 a.m. UTC | #1
On Wed, 25 Jan 2023 at 19:22, Mark Rutland <mark.rutland@arm.com> wrote:
>
> Assemblers will reject instructions not supported by a target
> architecture version, and so we must explicitly tell the assembler the
> latest architecture version for which we want to assemble instructions
> from.
>
> We've added a few AS_HAS_ARMV8_<N> definitions for this, in addition to
> an inconsistently named AS_HAS_PAC definition, from which arm64's
> top-level Makefile determines the architecture version that we intend to
> target, and generates the `asm-arch` variable.
>
> To make this a bit clearer and easier to maintain, this patch reworks
> the Makefile to determine asm-arch in a single if-else-endif chain.
> AS_HAS_PAC, which is defined when the assembler supports
> `-march=armv8.3-a`, is renamed to AS_HAS_ARMV8_3.
>
> As the logic for armv8.3-a is lifted out of the block handling pointer
> authentication, `asm-arch` may now be set to armv8.3-a regardless of
> whether support for pointer authentication is selected. This means that
> it will be possible to assemble armv8.3-a instructions even if we didn't
> intend to, but this is consistent with our handling of other
> architecture versions, and the compiler won't generate armv8.3-a
> instructions regardless.
>
> For the moment there's no need for an CONFIG_AS_HAS_ARMV8_1, as the code
> for LSE atomics and LDAPR use individual `.arch_extension` entries and
> do not require the baseline asm arch to be bumped to armv8.1-a. The
> other armv8.1-a features (e.g. PAN) do not require assembler support.
>
> There should be no functional change as a result of this patch.
>
> Signed-off-by: Mark Rutland <mark.rutland@arm.com>
> Cc: Ard Biesheuvel <ardb@kernel.org>
> Cc: Catalin Marinas <catalin.marinas@arm.com>
> Cc: Will Deacon <will@kernel.org>

Reviewed-by: Ard Biesheuvel <ardb@kernel.org>

> ---
>  arch/arm64/Kconfig  |  4 ++--
>  arch/arm64/Makefile | 37 ++++++++++++++++++-------------------
>  2 files changed, 20 insertions(+), 21 deletions(-)
>
> diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig
> index 03934808b2ed0..be760ad7715df 100644
> --- a/arch/arm64/Kconfig
> +++ b/arch/arm64/Kconfig
> @@ -1818,7 +1818,7 @@ config ARM64_PTR_AUTH_KERNEL
>         bool "Use pointer authentication for kernel"
>         default y
>         depends on ARM64_PTR_AUTH
> -       depends on (CC_HAS_SIGN_RETURN_ADDRESS || CC_HAS_BRANCH_PROT_PAC_RET) && AS_HAS_PAC
> +       depends on (CC_HAS_SIGN_RETURN_ADDRESS || CC_HAS_BRANCH_PROT_PAC_RET) && AS_HAS_ARMV8_3
>         # Modern compilers insert a .note.gnu.property section note for PAC
>         # which is only understood by binutils starting with version 2.33.1.
>         depends on LD_IS_LLD || LD_VERSION >= 23301 || (CC_IS_GCC && GCC_VERSION < 90100)
> @@ -1843,7 +1843,7 @@ config CC_HAS_SIGN_RETURN_ADDRESS
>         # GCC 7, 8
>         def_bool $(cc-option,-msign-return-address=all)
>
> -config AS_HAS_PAC
> +config AS_HAS_ARMV8_3
>         def_bool $(cc-option,-Wa$(comma)-march=armv8.3-a)
>
>  config AS_HAS_CFI_NEGATE_RA_STATE
> diff --git a/arch/arm64/Makefile b/arch/arm64/Makefile
> index d62bd221828f7..e176eb76345b5 100644
> --- a/arch/arm64/Makefile
> +++ b/arch/arm64/Makefile
> @@ -63,11 +63,6 @@ stack_protector_prepare: prepare0
>                                         include/generated/asm-offsets.h))
>  endif
>
> -ifeq ($(CONFIG_AS_HAS_ARMV8_2), y)
> -# make sure to pass the newest target architecture to -march.
> -asm-arch := armv8.2-a
> -endif
> -
>  # Ensure that if the compiler supports branch protection we default it
>  # off, this will be overridden if we are using branch protection.
>  branch-prot-flags-y += $(call cc-option,-mbranch-protection=none)
> @@ -88,25 +83,29 @@ branch-prot-flags-$(CONFIG_CC_HAS_BRANCH_PROT_PAC_RET_BTI) := -mbranch-protectio
>  else
>  branch-prot-flags-$(CONFIG_CC_HAS_BRANCH_PROT_PAC_RET) := -mbranch-protection=$(PACRET-y)
>  endif
> -# -march=armv8.3-a enables the non-nops instructions for PAC, to avoid the
> -# compiler to generate them and consequently to break the single image contract
> -# we pass it only to the assembler. This option is utilized only in case of non
> -# integrated assemblers.
> -ifeq ($(CONFIG_AS_HAS_PAC), y)
> -asm-arch := armv8.3-a
> -endif
>  endif
>
>  KBUILD_CFLAGS += $(branch-prot-flags-y)
>
> -ifeq ($(CONFIG_AS_HAS_ARMV8_4), y)
> -# make sure to pass the newest target architecture to -march.
> -asm-arch := armv8.4-a
> -endif
> -
> +# Tell the assembler to support instructions from the latest target
> +# architecture.
> +#
> +# For non-integrated assemblers we'll pass this on the command line, and for
> +# integrated assemblers we'll define ARM64_ASM_ARCH and ARM64_ASM_PREAMBLE for
> +# inline usage.
> +#
> +# We cannot pass the same arch flag to the compiler as this would allow it to
> +# freely generate instructions which are not supported by earlier architecture
> +# versions, which would prevent a single kernel image from working on earlier
> +# hardware.
>  ifeq ($(CONFIG_AS_HAS_ARMV8_5), y)
> -# make sure to pass the newest target architecture to -march.
> -asm-arch := armv8.5-a
> +  asm-arch := armv8.5-a
> +else ifeq ($(CONFIG_AS_HAS_ARMV8_4), y)
> +  asm-arch := armv8.4-a
> +else ifeq ($(CONFIG_AS_HAS_ARMV8_3), y)
> +  asm-arch := armv8.3-a
> +else ifeq ($(CONFIG_AS_HAS_ARMV8_2), y)
> +  asm-arch := armv8.2-a
>  endif
>
>  ifdef asm-arch
> --
> 2.30.2
>
Mark Brown Jan. 30, 2023, 6:31 p.m. UTC | #2
On Wed, Jan 25, 2023 at 06:22:00PM +0000, Mark Rutland wrote:
> Assemblers will reject instructions not supported by a target
> architecture version, and so we must explicitly tell the assembler the
> latest architecture version for which we want to assemble instructions
> from.

Reviewed-by: Mark Brown <broonie@kernel.org>
diff mbox series

Patch

diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig
index 03934808b2ed0..be760ad7715df 100644
--- a/arch/arm64/Kconfig
+++ b/arch/arm64/Kconfig
@@ -1818,7 +1818,7 @@  config ARM64_PTR_AUTH_KERNEL
 	bool "Use pointer authentication for kernel"
 	default y
 	depends on ARM64_PTR_AUTH
-	depends on (CC_HAS_SIGN_RETURN_ADDRESS || CC_HAS_BRANCH_PROT_PAC_RET) && AS_HAS_PAC
+	depends on (CC_HAS_SIGN_RETURN_ADDRESS || CC_HAS_BRANCH_PROT_PAC_RET) && AS_HAS_ARMV8_3
 	# Modern compilers insert a .note.gnu.property section note for PAC
 	# which is only understood by binutils starting with version 2.33.1.
 	depends on LD_IS_LLD || LD_VERSION >= 23301 || (CC_IS_GCC && GCC_VERSION < 90100)
@@ -1843,7 +1843,7 @@  config CC_HAS_SIGN_RETURN_ADDRESS
 	# GCC 7, 8
 	def_bool $(cc-option,-msign-return-address=all)
 
-config AS_HAS_PAC
+config AS_HAS_ARMV8_3
 	def_bool $(cc-option,-Wa$(comma)-march=armv8.3-a)
 
 config AS_HAS_CFI_NEGATE_RA_STATE
diff --git a/arch/arm64/Makefile b/arch/arm64/Makefile
index d62bd221828f7..e176eb76345b5 100644
--- a/arch/arm64/Makefile
+++ b/arch/arm64/Makefile
@@ -63,11 +63,6 @@  stack_protector_prepare: prepare0
 					include/generated/asm-offsets.h))
 endif
 
-ifeq ($(CONFIG_AS_HAS_ARMV8_2), y)
-# make sure to pass the newest target architecture to -march.
-asm-arch := armv8.2-a
-endif
-
 # Ensure that if the compiler supports branch protection we default it
 # off, this will be overridden if we are using branch protection.
 branch-prot-flags-y += $(call cc-option,-mbranch-protection=none)
@@ -88,25 +83,29 @@  branch-prot-flags-$(CONFIG_CC_HAS_BRANCH_PROT_PAC_RET_BTI) := -mbranch-protectio
 else
 branch-prot-flags-$(CONFIG_CC_HAS_BRANCH_PROT_PAC_RET) := -mbranch-protection=$(PACRET-y)
 endif
-# -march=armv8.3-a enables the non-nops instructions for PAC, to avoid the
-# compiler to generate them and consequently to break the single image contract
-# we pass it only to the assembler. This option is utilized only in case of non
-# integrated assemblers.
-ifeq ($(CONFIG_AS_HAS_PAC), y)
-asm-arch := armv8.3-a
-endif
 endif
 
 KBUILD_CFLAGS += $(branch-prot-flags-y)
 
-ifeq ($(CONFIG_AS_HAS_ARMV8_4), y)
-# make sure to pass the newest target architecture to -march.
-asm-arch := armv8.4-a
-endif
-
+# Tell the assembler to support instructions from the latest target
+# architecture.
+#
+# For non-integrated assemblers we'll pass this on the command line, and for
+# integrated assemblers we'll define ARM64_ASM_ARCH and ARM64_ASM_PREAMBLE for
+# inline usage.
+#
+# We cannot pass the same arch flag to the compiler as this would allow it to
+# freely generate instructions which are not supported by earlier architecture
+# versions, which would prevent a single kernel image from working on earlier
+# hardware.
 ifeq ($(CONFIG_AS_HAS_ARMV8_5), y)
-# make sure to pass the newest target architecture to -march.
-asm-arch := armv8.5-a
+  asm-arch := armv8.5-a
+else ifeq ($(CONFIG_AS_HAS_ARMV8_4), y)
+  asm-arch := armv8.4-a
+else ifeq ($(CONFIG_AS_HAS_ARMV8_3), y)
+  asm-arch := armv8.3-a
+else ifeq ($(CONFIG_AS_HAS_ARMV8_2), y)
+  asm-arch := armv8.2-a
 endif
 
 ifdef asm-arch