All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v6 0/4] Option to build big-endian with ELFv2 ABI
@ 2022-11-28  4:15 Nicholas Piggin
  2022-11-28  4:15 ` [PATCH v6 1/4] module: add module_elf_check_arch for module-specific checks Nicholas Piggin
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: Nicholas Piggin @ 2022-11-28  4:15 UTC (permalink / raw)
  To: linuxppc-dev; +Cc: Luis Chamberlain, Michal Suchánek, Nicholas Piggin

This is hopefully the final attempt. Luis was happy for the module
patch to go via the powerpc tree, so I've put the the ELFv2 for big
endian build patches into the series. Hopefully we can deprecate
the ELFv1 ABI 

Since v5, I cleaned up patch 2 as per Christophe's review. And patch
4 I removed the EXPERT depends so it's easier to test. It's marked as
experimental, but we should soon make it default and try to deprecate
the v1 ABI so we can eventually remove it.

Thanks,
Nick

Nicholas Piggin (4):
  module: add module_elf_check_arch for module-specific checks
  powerpc/64: Add module check for ELF ABI version
  powerpc/64: Add big-endian ELFv2 flavour to crypto VMX asm generation
  powerpc/64: Option to build big-endian with ELFv2 ABI

 arch/powerpc/Kconfig                   | 21 +++++++++++++++++++++
 arch/powerpc/kernel/module_64.c        | 10 ++++++++++
 arch/powerpc/platforms/Kconfig.cputype |  4 ++--
 drivers/crypto/vmx/Makefile            | 12 +++++++++++-
 drivers/crypto/vmx/ppc-xlate.pl        | 10 ++++++----
 include/linux/moduleloader.h           |  3 +++
 kernel/module/main.c                   | 10 ++++++++++
 7 files changed, 63 insertions(+), 7 deletions(-)

-- 
2.37.2


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v6 1/4] module: add module_elf_check_arch for module-specific checks
  2022-11-28  4:15 [PATCH v6 0/4] Option to build big-endian with ELFv2 ABI Nicholas Piggin
@ 2022-11-28  4:15 ` Nicholas Piggin
  2022-11-28  4:15 ` [PATCH v6 2/4] powerpc/64: Add module check for ELF ABI version Nicholas Piggin
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Nicholas Piggin @ 2022-11-28  4:15 UTC (permalink / raw)
  To: linuxppc-dev
  Cc: Luis Chamberlain, Nicholas Piggin, Jessica Yu, Michal Suchánek

The elf_check_arch() function is also used to test compatibility of
usermode binaries. Kernel modules may have more specific requirements,
for example powerpc would like to test for ABI version compatibility.

Add a weak module_elf_check_arch() that defaults to true, and call it
from elf_validity_check().

Cc: Michael Ellerman <mpe@ellerman.id.au>
Signed-off-by: Jessica Yu <jeyu@kernel.org>
[np: added changelog, adjust name, rebase]
Acked-by: Luis Chamberlain <mcgrof@kernel.org>
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
 include/linux/moduleloader.h |  3 +++
 kernel/module/main.c         | 10 ++++++++++
 2 files changed, 13 insertions(+)

diff --git a/include/linux/moduleloader.h b/include/linux/moduleloader.h
index 9e09d11ffe5b..7b4587a19189 100644
--- a/include/linux/moduleloader.h
+++ b/include/linux/moduleloader.h
@@ -13,6 +13,9 @@
  * must be implemented by each architecture.
  */
 
+/* arch may override to do additional checking of ELF header architecture */
+bool module_elf_check_arch(Elf_Ehdr *hdr);
+
 /* Adjust arch-specific sections.  Return 0 on success.  */
 int module_frob_arch_sections(Elf_Ehdr *hdr,
 			      Elf_Shdr *sechdrs,
diff --git a/kernel/module/main.c b/kernel/module/main.c
index d02d39c7174e..7b3f6fb0d428 100644
--- a/kernel/module/main.c
+++ b/kernel/module/main.c
@@ -1674,6 +1674,11 @@ static int elf_validity_check(struct load_info *info)
 		       info->hdr->e_machine);
 		goto no_exec;
 	}
+	if (!module_elf_check_arch(info->hdr)) {
+		pr_err("Invalid module architecture in ELF header: %u\n",
+		       info->hdr->e_machine);
+		goto no_exec;
+	}
 	if (info->hdr->e_shentsize != sizeof(Elf_Shdr)) {
 		pr_err("Invalid ELF section header size\n");
 		goto no_exec;
@@ -2247,6 +2252,11 @@ static void flush_module_icache(const struct module *mod)
 			   (unsigned long)mod->core_layout.base + mod->core_layout.size);
 }
 
+bool __weak module_elf_check_arch(Elf_Ehdr *hdr)
+{
+	return true;
+}
+
 int __weak module_frob_arch_sections(Elf_Ehdr *hdr,
 				     Elf_Shdr *sechdrs,
 				     char *secstrings,
-- 
2.37.2


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v6 2/4] powerpc/64: Add module check for ELF ABI version
  2022-11-28  4:15 [PATCH v6 0/4] Option to build big-endian with ELFv2 ABI Nicholas Piggin
  2022-11-28  4:15 ` [PATCH v6 1/4] module: add module_elf_check_arch for module-specific checks Nicholas Piggin
@ 2022-11-28  4:15 ` Nicholas Piggin
  2022-11-28  4:15 ` [PATCH v6 3/4] powerpc/64: Add big-endian ELFv2 flavour to crypto VMX asm generation Nicholas Piggin
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Nicholas Piggin @ 2022-11-28  4:15 UTC (permalink / raw)
  To: linuxppc-dev
  Cc: Luis Chamberlain, Nicholas Piggin, Jessica Yu, Michal Suchánek

Override the generic module ELF check to provide a check for the ELF ABI
version. This becomes important if we allow big-endian ELF ABI V2 builds
but it doesn't hurt to check now.

Cc: Jessica Yu <jeyu@kernel.org>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
[np: split patch, added changelog, adjust to Jessica's proposal]
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
 arch/powerpc/kernel/module_64.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/arch/powerpc/kernel/module_64.c b/arch/powerpc/kernel/module_64.c
index 7e45dc98df8a..ff045644f13f 100644
--- a/arch/powerpc/kernel/module_64.c
+++ b/arch/powerpc/kernel/module_64.c
@@ -31,6 +31,16 @@
    this, and makes other things simpler.  Anton?
    --RR.  */
 
+bool module_elf_check_arch(Elf_Ehdr *hdr)
+{
+	unsigned long abi_level = hdr->e_flags & 0x3;
+
+	if (IS_ENABLED(CONFIG_PPC64_ELF_ABI_V2))
+		return abi_level == 2;
+	else
+		return abi_level < 2;
+}
+
 #ifdef CONFIG_PPC64_ELF_ABI_V2
 
 static func_desc_t func_desc(unsigned long addr)
-- 
2.37.2


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v6 3/4] powerpc/64: Add big-endian ELFv2 flavour to crypto VMX asm generation
  2022-11-28  4:15 [PATCH v6 0/4] Option to build big-endian with ELFv2 ABI Nicholas Piggin
  2022-11-28  4:15 ` [PATCH v6 1/4] module: add module_elf_check_arch for module-specific checks Nicholas Piggin
  2022-11-28  4:15 ` [PATCH v6 2/4] powerpc/64: Add module check for ELF ABI version Nicholas Piggin
@ 2022-11-28  4:15 ` Nicholas Piggin
  2022-11-28  4:15 ` [PATCH v6 4/4] powerpc/64: Option to build big-endian with ELFv2 ABI Nicholas Piggin
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Nicholas Piggin @ 2022-11-28  4:15 UTC (permalink / raw)
  To: linuxppc-dev; +Cc: Luis Chamberlain, Michal Suchánek, Nicholas Piggin

This allows asm generation for big-endian ELFv2 builds.

Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
 drivers/crypto/vmx/Makefile     | 12 +++++++++++-
 drivers/crypto/vmx/ppc-xlate.pl | 10 ++++++----
 2 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/drivers/crypto/vmx/Makefile b/drivers/crypto/vmx/Makefile
index 2560cfea1dec..e33c7238e7f8 100644
--- a/drivers/crypto/vmx/Makefile
+++ b/drivers/crypto/vmx/Makefile
@@ -2,8 +2,18 @@
 obj-$(CONFIG_CRYPTO_DEV_VMX_ENCRYPT) += vmx-crypto.o
 vmx-crypto-objs := vmx.o aesp8-ppc.o ghashp8-ppc.o aes.o aes_cbc.o aes_ctr.o aes_xts.o ghash.o
 
+ifeq ($(CONFIG_CPU_LITTLE_ENDIAN),y)
+override flavour := linux-ppc64le
+else
+ifdef CONFIG_PPC64_ELF_ABI_V2
+override flavour := linux-ppc64-elfv2
+else
+override flavour := linux-ppc64
+endif
+endif
+
 quiet_cmd_perl = PERL    $@
-      cmd_perl = $(PERL) $< $(if $(CONFIG_CPU_LITTLE_ENDIAN), linux-ppc64le, linux-ppc64) > $@
+      cmd_perl = $(PERL) $< $(flavour) > $@
 
 targets += aesp8-ppc.S ghashp8-ppc.S
 
diff --git a/drivers/crypto/vmx/ppc-xlate.pl b/drivers/crypto/vmx/ppc-xlate.pl
index 36db2ef09e5b..b583898c11ae 100644
--- a/drivers/crypto/vmx/ppc-xlate.pl
+++ b/drivers/crypto/vmx/ppc-xlate.pl
@@ -9,6 +9,8 @@ open STDOUT,">$output" || die "can't open $output: $!";
 
 my %GLOBALS;
 my $dotinlocallabels=($flavour=~/linux/)?1:0;
+my $elfv2abi=(($flavour =~ /linux-ppc64le/) or ($flavour =~ /linux-ppc64-elfv2/))?1:0;
+my $dotfunctions=($elfv2abi=~1)?0:1;
 
 ################################################################
 # directives which need special treatment on different platforms
@@ -40,7 +42,7 @@ my $globl = sub {
 };
 my $text = sub {
     my $ret = ($flavour =~ /aix/) ? ".csect\t.text[PR],7" : ".text";
-    $ret = ".abiversion	2\n".$ret	if ($flavour =~ /linux.*64le/);
+    $ret = ".abiversion	2\n".$ret	if ($elfv2abi);
     $ret;
 };
 my $machine = sub {
@@ -56,8 +58,8 @@ my $size = sub {
     if ($flavour =~ /linux/)
     {	shift;
 	my $name = shift; $name =~ s|^[\.\_]||;
-	my $ret  = ".size	$name,.-".($flavour=~/64$/?".":"").$name;
-	$ret .= "\n.size	.$name,.-.$name" if ($flavour=~/64$/);
+	my $ret  = ".size	$name,.-".($dotfunctions?".":"").$name;
+	$ret .= "\n.size	.$name,.-.$name" if ($dotfunctions);
 	$ret;
     }
     else
@@ -142,7 +144,7 @@ my $vmr = sub {
 
 # Some ABIs specify vrsave, special-purpose register #256, as reserved
 # for system use.
-my $no_vrsave = ($flavour =~ /linux-ppc64le/);
+my $no_vrsave = ($elfv2abi);
 my $mtspr = sub {
     my ($f,$idx,$ra) = @_;
     if ($idx == 256 && $no_vrsave) {
-- 
2.37.2


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v6 4/4] powerpc/64: Option to build big-endian with ELFv2 ABI
  2022-11-28  4:15 [PATCH v6 0/4] Option to build big-endian with ELFv2 ABI Nicholas Piggin
                   ` (2 preceding siblings ...)
  2022-11-28  4:15 ` [PATCH v6 3/4] powerpc/64: Add big-endian ELFv2 flavour to crypto VMX asm generation Nicholas Piggin
@ 2022-11-28  4:15 ` Nicholas Piggin
  2022-11-29  3:02 ` [PATCH v6 0/4] " Joel Stanley
  2022-12-08 12:39 ` Michael Ellerman
  5 siblings, 0 replies; 7+ messages in thread
From: Nicholas Piggin @ 2022-11-28  4:15 UTC (permalink / raw)
  To: linuxppc-dev; +Cc: Luis Chamberlain, Michal Suchánek, Nicholas Piggin

Provide an option to build big-endian kernels using the ELFv2 ABI. This
works on GCC only for now. Clang is rumored to support this, but core
build files need updating first, at least.

This gives big-endian kernels useful advantages of the ELFv2 ABI, e.g.,
less stack usage, -mprofile-kernel support, better compatibility with
eBPF tools.

BE+ELFv2 is not officially supported by the GNU toolchain, but it works
fine in testing and has been used by some userspace for some time (e.g.,
Void Linux).

Tested-by: Michal Suchánek <msuchanek@suse.de>
Reviewed-by: Segher Boessenkool <segher@kernel.crashing.org>
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
 arch/powerpc/Kconfig                   | 21 +++++++++++++++++++++
 arch/powerpc/platforms/Kconfig.cputype |  4 ++--
 2 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig
index 2ca5418457ed..2d0d80bcc24a 100644
--- a/arch/powerpc/Kconfig
+++ b/arch/powerpc/Kconfig
@@ -1,6 +1,9 @@
 # SPDX-License-Identifier: GPL-2.0
 source "arch/powerpc/platforms/Kconfig.cputype"
 
+config CC_HAS_ELFV2
+	def_bool PPC64 && $(cc-option, -mabi=elfv2)
+
 config 32BIT
 	bool
 	default y if PPC32
@@ -583,6 +586,24 @@ config KEXEC_FILE
 config ARCH_HAS_KEXEC_PURGATORY
 	def_bool KEXEC_FILE
 
+config PPC64_BIG_ENDIAN_ELF_ABI_V2
+	bool "Build big-endian kernel using ELF ABI V2 (EXPERIMENTAL)"
+	depends on PPC64 && CPU_BIG_ENDIAN
+	depends on CC_HAS_ELFV2
+	depends on LD_IS_BFD && LD_VERSION >= 22400
+	default n
+	help
+	  This builds the kernel image using the "Power Architecture 64-Bit ELF
+	  V2 ABI Specification", which has a reduced stack overhead and faster
+	  function calls. This internal kernel ABI option does not affect
+          userspace compatibility.
+
+	  The V2 ABI is standard for 64-bit little-endian, but for big-endian
+	  it is less well tested by kernel and toolchain. However some distros
+	  build userspace this way, and it can produce a functioning kernel.
+
+	  This requires GCC and binutils 2.24 or newer.
+
 config RELOCATABLE
 	bool "Build a relocatable kernel"
 	depends on PPC64 || (FLATMEM && (44x || PPC_85xx))
diff --git a/arch/powerpc/platforms/Kconfig.cputype b/arch/powerpc/platforms/Kconfig.cputype
index 0c4eed9aea80..6e94d45f3baa 100644
--- a/arch/powerpc/platforms/Kconfig.cputype
+++ b/arch/powerpc/platforms/Kconfig.cputype
@@ -575,10 +575,10 @@ config CPU_LITTLE_ENDIAN
 endchoice
 
 config PPC64_ELF_ABI_V1
-	def_bool PPC64 && CPU_BIG_ENDIAN
+	def_bool PPC64 && (CPU_BIG_ENDIAN && !PPC64_BIG_ENDIAN_ELF_ABI_V2)
 
 config PPC64_ELF_ABI_V2
-	def_bool PPC64 && CPU_LITTLE_ENDIAN
+	def_bool PPC64 && !PPC64_ELF_ABI_V1
 
 config PPC64_BOOT_WRAPPER
 	def_bool n
-- 
2.37.2


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v6 0/4] Option to build big-endian with ELFv2 ABI
  2022-11-28  4:15 [PATCH v6 0/4] Option to build big-endian with ELFv2 ABI Nicholas Piggin
                   ` (3 preceding siblings ...)
  2022-11-28  4:15 ` [PATCH v6 4/4] powerpc/64: Option to build big-endian with ELFv2 ABI Nicholas Piggin
@ 2022-11-29  3:02 ` Joel Stanley
  2022-12-08 12:39 ` Michael Ellerman
  5 siblings, 0 replies; 7+ messages in thread
From: Joel Stanley @ 2022-11-29  3:02 UTC (permalink / raw)
  To: Nicholas Piggin; +Cc: Luis Chamberlain, Michal Suchánek, linuxppc-dev

On Mon, 28 Nov 2022 at 04:16, Nicholas Piggin <npiggin@gmail.com> wrote:
>
> This is hopefully the final attempt. Luis was happy for the module
> patch to go via the powerpc tree, so I've put the the ELFv2 for big
> endian build patches into the series. Hopefully we can deprecate
> the ELFv1 ABI
>
> Since v5, I cleaned up patch 2 as per Christophe's review. And patch
> 4 I removed the EXPERT depends so it's easier to test. It's marked as
> experimental, but we should soon make it default and try to deprecate
> the v1 ABI so we can eventually remove it.

Reviewed-by: Joel Stanley <joel@jms.id.au>

I did some builds and boot tested them in qemu fine.

>
> Thanks,
> Nick
>
> Nicholas Piggin (4):
>   module: add module_elf_check_arch for module-specific checks
>   powerpc/64: Add module check for ELF ABI version
>   powerpc/64: Add big-endian ELFv2 flavour to crypto VMX asm generation
>   powerpc/64: Option to build big-endian with ELFv2 ABI
>
>  arch/powerpc/Kconfig                   | 21 +++++++++++++++++++++
>  arch/powerpc/kernel/module_64.c        | 10 ++++++++++
>  arch/powerpc/platforms/Kconfig.cputype |  4 ++--
>  drivers/crypto/vmx/Makefile            | 12 +++++++++++-
>  drivers/crypto/vmx/ppc-xlate.pl        | 10 ++++++----
>  include/linux/moduleloader.h           |  3 +++
>  kernel/module/main.c                   | 10 ++++++++++
>  7 files changed, 63 insertions(+), 7 deletions(-)
>
> --
> 2.37.2
>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v6 0/4] Option to build big-endian with ELFv2 ABI
  2022-11-28  4:15 [PATCH v6 0/4] Option to build big-endian with ELFv2 ABI Nicholas Piggin
                   ` (4 preceding siblings ...)
  2022-11-29  3:02 ` [PATCH v6 0/4] " Joel Stanley
@ 2022-12-08 12:39 ` Michael Ellerman
  5 siblings, 0 replies; 7+ messages in thread
From: Michael Ellerman @ 2022-12-08 12:39 UTC (permalink / raw)
  To: Nicholas Piggin, linuxppc-dev; +Cc: Luis Chamberlain, Michal Suchánek

On Mon, 28 Nov 2022 14:15:35 +1000, Nicholas Piggin wrote:
> This is hopefully the final attempt. Luis was happy for the module
> patch to go via the powerpc tree, so I've put the the ELFv2 for big
> endian build patches into the series. Hopefully we can deprecate
> the ELFv1 ABI
> 
> Since v5, I cleaned up patch 2 as per Christophe's review. And patch
> 4 I removed the EXPERT depends so it's easier to test. It's marked as
> experimental, but we should soon make it default and try to deprecate
> the v1 ABI so we can eventually remove it.
> 
> [...]

Applied to powerpc/next.

[1/4] module: add module_elf_check_arch for module-specific checks
      https://git.kernel.org/powerpc/c/f9231a996e229c13d23f907352c2cea84bd1c30a
[2/4] powerpc/64: Add module check for ELF ABI version
      https://git.kernel.org/powerpc/c/de3d098dd1fc635535e3689c5d4aa0684242adde
[3/4] powerpc/64: Add big-endian ELFv2 flavour to crypto VMX asm generation
      https://git.kernel.org/powerpc/c/505ea33089dcfc3ee3201b0fcb94751165805413
[4/4] powerpc/64: Option to build big-endian with ELFv2 ABI
      https://git.kernel.org/powerpc/c/5017b45946722bdd20ac255c9ae7273b78d1f12e

cheers

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-12-08 13:01 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-28  4:15 [PATCH v6 0/4] Option to build big-endian with ELFv2 ABI Nicholas Piggin
2022-11-28  4:15 ` [PATCH v6 1/4] module: add module_elf_check_arch for module-specific checks Nicholas Piggin
2022-11-28  4:15 ` [PATCH v6 2/4] powerpc/64: Add module check for ELF ABI version Nicholas Piggin
2022-11-28  4:15 ` [PATCH v6 3/4] powerpc/64: Add big-endian ELFv2 flavour to crypto VMX asm generation Nicholas Piggin
2022-11-28  4:15 ` [PATCH v6 4/4] powerpc/64: Option to build big-endian with ELFv2 ABI Nicholas Piggin
2022-11-29  3:02 ` [PATCH v6 0/4] " Joel Stanley
2022-12-08 12:39 ` Michael Ellerman

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.