Skip to contentSkip to conversation

kexec: add generic support for elf kernel images

PATCH Superseded Sven Schnelle 2 participants 18 messages Jul 15, 2019 – Aug 21, 2019

Subsystems (4)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover kexec: add generic support for elf kernel images

Changes to v3:
- add support for 32-bit ELF files

Changes to v2:
- use git format-patch -C

Changes to v1:
- split up patch into smaller pieces
- rebase onto powerpc/next
- remove unused variable in kexec_elf_load()

Changes to RFC version:
- remove unused Elf_Rel macro
- remove section header parsing
- remove PURGATORY_STACK_SIZE
- change order of elf_*_to_cpu() functions
- remove elf_addr_to_cpu macro

Sven Schnelle (7):
kexec: add KEXEC_ELF
kexec_elf: change order of elf_*_to_cpu() functions
kexec_elf: remove parsing of section headers
kexec_elf: remove PURGATORY_STACK_SIZE
kexec_elf: remove Elf_Rel macro
kexec_elf: remove unused variable in kexec_elf_load()
kexec_elf: support 32 bit ELF files

 arch/Kconfig                       |   3 +
 arch/powerpc/Kconfig               |   1 +
 arch/powerpc/kernel/kexec_elf_64.c | 551 +----------------------------
 include/linux/kexec.h              |  23 ++
 kernel/Makefile                    |   1 +
 kernel/kexec_elf.c                 | 418 ++++++++++++++++++++++
 6 files changed, 456 insertions(+), 541 deletions(-)
 create mode 100644 kernel/kexec_elf.c
Signature · 7 lines
2.20.1


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
#1 lore raw

PATCH v4 1/7 kexec: add KEXEC_ELF

Right now powerpc provides an implementation to read elf files with the kexec_file() syscall. Make that available as a public kexec interface so it can be re-used on other architectures.

---
 arch/Kconfig                                  |   3 +
 arch/powerpc/Kconfig                          |   1 +
 arch/powerpc/kernel/kexec_elf_64.c            | 551 +-----------------
 include/linux/kexec.h                         |  24 +
 kernel/Makefile                               |   1 +
 .../kexec_elf_64.c => kernel/kexec_elf.c      | 199 ++-----
 6 files changed, 75 insertions(+), 704 deletions(-)
 copy arch/powerpc/kernel/kexec_elf_64.c => kernel/kexec_elf.c (71%)
arch/Kconfig +3 -0
index c47b328eada0..30694aca4316 100644
@@ -18,6 +18,9 @@ config KEXEC_CORE
 	select CRASH_CORE
 	bool
 
+config KEXEC_ELF
+	bool
+
 config HAVE_IMA_KEXEC
 	bool
 
arch/powerpc/Kconfig +1 -0
index 12cee37f15c4..addc2dad78e0 100644
@@ -510,6 +510,7 @@ config KEXEC_FILE
 	select KEXEC_CORE
 	select HAVE_IMA_KEXEC
 	select BUILD_BIN2C
+	select KEXEC_ELF
 	depends on PPC64
 	depends on CRYPTO=y
 	depends on CRYPTO_SHA256=y
arch/powerpc/kernel/kexec_elf_64.c +10 -541

602 lines not on this page · raw message

include/linux/kexec.h +24 -0
index b9b1bc5f9669..da2a6b1d69e7 100644
@@ -216,6 +216,30 @@ extern int crash_prepare_elf64_headers(struct crash_mem *mem, int kernel_map,
 				       void **addr, unsigned long *sz);
 #endif /* CONFIG_KEXEC_FILE */
 
+#ifdef CONFIG_KEXEC_ELF
+struct kexec_elf_info {
+	/*
+	 * Where the ELF binary contents are kept.
+	 * Memory managed by the user of the struct.
+	 */
+	const char *buffer;
+
+	const struct elfhdr *ehdr;
+	const struct elf_phdr *proghdrs;
+	struct elf_shdr *sechdrs;
+};
+
+int kexec_build_elf_info(const char *buf, size_t len, struct elfhdr *ehdr,
+			       struct kexec_elf_info *elf_info);
+
+int kexec_elf_load(struct kimage *image, struct elfhdr *ehdr,
+			 struct kexec_elf_info *elf_info,
+			 struct kexec_buf *kbuf,
+			 unsigned long *lowest_load_addr);
+
+void kexec_free_elf_info(struct kexec_elf_info *elf_info);
+int kexec_elf_probe(const char *buf, unsigned long len);
+#endif
 struct kimage {
 	kimage_entry_t head;
 	kimage_entry_t *entry;
kernel/Makefile +1 -0
index 33824f0385b3..7062306de9b7 100644
@@ -64,6 +64,7 @@ obj-$(CONFIG_CRASH_CORE) += crash_core.o
 obj-$(CONFIG_KEXEC_CORE) += kexec_core.o
 obj-$(CONFIG_KEXEC) += kexec.o
 obj-$(CONFIG_KEXEC_FILE) += kexec_file.o
+obj-$(CONFIG_KEXEC_ELF) += kexec_elf.o
 obj-$(CONFIG_BACKTRACE_SELF_TEST) += backtracetest.o
 obj-$(CONFIG_COMPAT) += compat.o
 obj-$(CONFIG_CGROUPS) += cgroup/
kernel/kexec_elf.c renamed from arch/powerpc/kernel/kexec_elf_64.c +36 -163

305 lines not on this page · raw message

Signature · 7 lines
2.20.1


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
Reviewed-by on v4 1/7
#2 lore raw

Re: [PATCH v4 1/7] kexec: add KEXEC_ELF

Hello Sven,

Just a few small comments below. Regardless of them:

Quoted 2 lines · Sven Schnelle

Right now powerpc provides an implementation to read elf files with the kexec_file() syscall. Make that available as a public

Nit: the syscall is kexec_file_load()

Quoted 12 lines

kexec interface so it can be re-used on other architectures.

---
 arch/Kconfig                                  |   3 +
 arch/powerpc/Kconfig                          |   1 +
 arch/powerpc/kernel/kexec_elf_64.c            | 551 +-----------------
 include/linux/kexec.h                         |  24 +
 kernel/Makefile                               |   1 +
 .../kexec_elf_64.c => kernel/kexec_elf.c      | 199 ++-----
 6 files changed, 75 insertions(+), 704 deletions(-)
 copy arch/powerpc/kernel/kexec_elf_64.c => kernel/kexec_elf.c (71%)

<snip>

Quoted 26 lines
arch/powerpc/kernel/kexec_elf_64.c +1 -9
index ba4f18a43ee8..30bd57a93c17 100644
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0-only
 /*
  * Load ELF vmlinux file for the kexec_file_load syscall.
  *
@@ -10,15 +11,6 @@
  * Based on kexec-tools' kexec-elf-exec.c and kexec-elf-ppc64.c.
  * Heavily modified for the kernel by
  * Thiago Jung Bauermann <[email protected]>.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation (version 2 of the License).
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
  */
 
 #define pr_fmt(fmt)	"kexec_elf: " fmt

arch/powerpc/kernel/kexec_elf_64.c line 32

Quoted 2 lines
arch/powerpc/kernel/kexec_elf_64.c +0 -0
@@ -39,532 +31,6 @@
 #define Elf_Rel		Elf64_Rel
 #endif /* Elf_Rel */

Perhaps this patch could remove the #define for elf_addr_to_cpu since it's not used anymore in this file?

Quoted 21 lines
kernel/kexec_elf.c renamed from arch/powerpc/kernel/kexec_elf_64.c +0 -13
similarity index 71%
copy from arch/powerpc/kernel/kexec_elf_64.c
copy to kernel/kexec_elf.c
index ba4f18a43ee8..6e9f52171ede 100644
@@ -1,33 +1,10 @@
-/*
- * Load ELF vmlinux file for the kexec_file_load syscall.
- *
- * Copyright (C) 2004  Adam Litke ([email protected])
- * Copyright (C) 2004  IBM Corp.
- * Copyright (C) 2005  R Sharada ([email protected])
- * Copyright (C) 2006  Mohan Kumar M ([email protected])
- * Copyright (C) 2016  IBM Corporation
- *
- * Based on kexec-tools' kexec-elf-exec.c and kexec-elf-ppc64.c.
- * Heavily modified for the kernel by
- * Thiago Jung Bauermann <[email protected]>.
- *

kernel/kexec_elf.c line 1

Quoted 10 lines
kernel/kexec_elf.c renamed from arch/powerpc/kernel/kexec_elf_64.c +1 -9
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation (version 2 of the License).
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- */
+// SPDX-License-Identifier: GPL-2.0-only

I may be wrong, but my understanding of the SPDX license identifier is that it substitutes the license text (i.e., the last two paragraphs above), but not the copyright statements. Is it ok to have a file with a SPDX license identifier but no copyright statement?

--
Thiago Jung Bauermann
IBM Linux Technology Center

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#3 lore raw

Re: [PATCH v4 1/7] kexec: add KEXEC_ELF

Quoted 28 lines · Thiago Jung Bauermann
Quoted 28 lines
kernel/kexec_elf.c renamed from arch/powerpc/kernel/kexec_elf_64.c +0 -20
similarity index 71%
copy from arch/powerpc/kernel/kexec_elf_64.c
copy to kernel/kexec_elf.c
index ba4f18a43ee8..6e9f52171ede 100644
@@ -1,33 +1,10 @@
-/*
- * Load ELF vmlinux file for the kexec_file_load syscall.
- *
- * Copyright (C) 2004  Adam Litke ([email protected])
- * Copyright (C) 2004  IBM Corp.
- * Copyright (C) 2005  R Sharada ([email protected])
- * Copyright (C) 2006  Mohan Kumar M ([email protected])
- * Copyright (C) 2016  IBM Corporation
- *
- * Based on kexec-tools' kexec-elf-exec.c and kexec-elf-ppc64.c.
- * Heavily modified for the kernel by
- * Thiago Jung Bauermann <[email protected]>.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation (version 2 of the License).
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Quoted 8 lines

kernel/kexec_elf.c line 1

Quoted 3 lines
kernel/kexec_elf.c renamed from arch/powerpc/kernel/kexec_elf_64.c +1 -2
- * GNU General Public License for more details.
- */
+// SPDX-License-Identifier: GPL-2.0-only

I may be wrong, but my understanding of the SPDX license identifier is that it substitutes the license text (i.e., the last two paragraphs above), but not the copyright statements. Is it ok to have a file with a SPDX license identifier but no copyright statement?

Answering my own question: I just came accross commit b24413180f56 ("License cleanup: add SPDX GPL-2.0 license identifier to files with no license") which adds SPDX license identifiers to a lot of files without any copyright statement so I conclude that it is indeed ok to not have copyright statements in a file.

In this instance the new file is heavily based on the old one though, so IMHO it makes sense for it to inherit the copyright statements from the original file.

--
Thiago Jung Bauermann
IBM Linux Technology Center

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#4 lore raw

PATCH v4 2/7 kexec_elf: change order of elf_*_to_cpu() functions

In reply to Sven Schnelle

Change the order to have a 64/32/16 order, no functional change.

---
 kernel/kexec_elf.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
kernel/kexec_elf.c +6 -6
index 6e9f52171ede..76e7df64d715 100644
@@ -31,22 +31,22 @@ static uint64_t elf64_to_cpu(const struct elfhdr *ehdr, uint64_t value)
 	return value;
 }
 
-static uint16_t elf16_to_cpu(const struct elfhdr *ehdr, uint16_t value)
+static uint32_t elf32_to_cpu(const struct elfhdr *ehdr, uint32_t value)
 {
 	if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB)
-		value = le16_to_cpu(value);
+		value = le32_to_cpu(value);
 	else if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB)
-		value = be16_to_cpu(value);
+		value = be32_to_cpu(value);
 
 	return value;
 }
 
-static uint32_t elf32_to_cpu(const struct elfhdr *ehdr, uint32_t value)
+static uint16_t elf16_to_cpu(const struct elfhdr *ehdr, uint16_t value)
 {
 	if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB)
-		value = le32_to_cpu(value);
+		value = le16_to_cpu(value);
 	else if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB)
-		value = be32_to_cpu(value);
+		value = be16_to_cpu(value);
 
 	return value;
 }
Signature · 7 lines
2.20.1


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
Reviewed-by on v4 2/7
#5 lore raw

Re: [PATCH v4 2/7] kexec_elf: change order of elf_*_to_cpu() functions

Quoted 3 lines · Sven Schnelle

Change the order to have a 64/32/16 order, no functional change.

Signature · 7 lines
Thiago Jung Bauermann
IBM Linux Technology Center

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
#6 lore raw

PATCH v4 3/7 kexec_elf: remove parsing of section headers

In reply to Sven Schnelle

We're not using them, so we can drop the parsing.

---
 include/linux/kexec.h |   1 -
 kernel/kexec_elf.c    | 137 ------------------------------------------
 2 files changed, 138 deletions(-)
include/linux/kexec.h +0 -1
index da2a6b1d69e7..f0b809258ed3 100644
@@ -226,7 +226,6 @@ struct kexec_elf_info {
 
 	const struct elfhdr *ehdr;
 	const struct elf_phdr *proghdrs;
-	struct elf_shdr *sechdrs;
 };
 
 int kexec_build_elf_info(const char *buf, size_t len, struct elfhdr *ehdr,
kernel/kexec_elf.c +0 -137

155 lines not on this page · raw message

Signature · 7 lines
2.20.1


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
Reviewed-by on v4 3/7
#7 lore raw

Re: [PATCH v4 3/7] kexec_elf: remove parsing of section headers

Quoted 3 lines · Sven Schnelle

We're not using them, so we can drop the parsing.

Signature · 8 lines
Thiago Jung Bauermann
IBM Linux Technology Center


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
#8 lore raw

PATCH v4 4/7 kexec_elf: remove PURGATORY_STACK_SIZE

In reply to Sven Schnelle

It's not used anywhere so just drop it.

---
 kernel/kexec_elf.c | 2 --
 1 file changed, 2 deletions(-)
kernel/kexec_elf.c +0 -2
index effe9dc0b055..70d31b8feeae 100644
@@ -8,8 +8,6 @@
 #include <linux/slab.h>
 #include <linux/types.h>
 
-#define PURGATORY_STACK_SIZE	(16 * 1024)
-
 #define elf_addr_to_cpu	elf64_to_cpu
 
 #ifndef Elf_Rel
Signature · 7 lines
2.20.1


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
#9 lore raw

Re: [PATCH v4 4/7] kexec_elf: remove PURGATORY_STACK_SIZE

Quoted 12 lines · Sven Schnelle

It's not used anywhere so just drop it.

---
 kernel/kexec_elf.c | 2 --
 1 file changed, 2 deletions(-)
kernel/kexec_elf.c +0 -0
index effe9dc0b055..70d31b8feeae 100644

kernel/kexec_elf.c line 13

Quoted 8 lines
kernel/kexec_elf.c +0 -2
@@ -8,8 +8,6 @@
 #include <linux/slab.h>
 #include <linux/types.h>
 
-#define PURGATORY_STACK_SIZE	(16 * 1024)
-
 #define elf_addr_to_cpu	elf64_to_cpu
 
 #ifndef Elf_Rel

Can you remove it from the file in arch/powerpc as well?

Signature · 7 lines
Thiago Jung Bauermann
IBM Linux Technology Center

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
#10 lore raw

Re: [PATCH v4 4/7] kexec_elf: remove PURGATORY_STACK_SIZE

Quoted 17 lines · Thiago Jung Bauermann
Quoted 15 lines · Sven Schnelle

It's not used anywhere so just drop it.

---
 kernel/kexec_elf.c | 2 --
 1 file changed, 2 deletions(-)
kernel/kexec_elf.c +0 -0
index effe9dc0b055..70d31b8feeae 100644
@@ -8,8 +8,6 @@
 #include <linux/slab.h>
 #include <linux/types.h>
 
Quoted 7 lines

kernel/kexec_elf.c line 13

Quoted 5 lines
kernel/kexec_elf.c +0 -2
-#define PURGATORY_STACK_SIZE	(16 * 1024)
-
 #define elf_addr_to_cpu	elf64_to_cpu
 
 #ifndef Elf_Rel

Can you remove it from the file in arch/powerpc as well?

Sorry, forgot to add:

Signature · 8 lines
Thiago Jung Bauermann
IBM Linux Technology Center


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
#11 lore raw

PATCH v4 5/7 kexec_elf: remove Elf_Rel macro

In reply to Sven Schnelle

It wasn't used anywhere, so lets drop it.

---
 kernel/kexec_elf.c | 4 ----
 1 file changed, 4 deletions(-)
kernel/kexec_elf.c +0 -4
index 70d31b8feeae..e346659af324 100644
@@ -10,10 +10,6 @@
 
 #define elf_addr_to_cpu	elf64_to_cpu
 
-#ifndef Elf_Rel
-#define Elf_Rel		Elf64_Rel
-#endif /* Elf_Rel */
-
 static inline bool elf_is_elf_file(const struct elfhdr *ehdr)
 {
 	return memcmp(ehdr->e_ident, ELFMAG, SELFMAG) == 0;
Signature · 7 lines
2.20.1


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
Reviewed-by on v4 5/7
#12 lore raw

Re: [PATCH v4 5/7] kexec_elf: remove Elf_Rel macro

Quoted 13 lines · Sven Schnelle

It wasn't used anywhere, so lets drop it.

---
 kernel/kexec_elf.c | 4 ----
 1 file changed, 4 deletions(-)
kernel/kexec_elf.c +0 -0
index 70d31b8feeae..e346659af324 100644

kernel/kexec_elf.c line 15

Quoted 10 lines
kernel/kexec_elf.c +0 -4
@@ -10,10 +10,6 @@
 
 #define elf_addr_to_cpu	elf64_to_cpu
 
-#ifndef Elf_Rel
-#define Elf_Rel		Elf64_Rel
-#endif /* Elf_Rel */
-
 static inline bool elf_is_elf_file(const struct elfhdr *ehdr)
 {
 	return memcmp(ehdr->e_ident, ELFMAG, SELFMAG) == 0;

Could you remove this one from the file in arch/powerpc as well? Perhaps this and the previous patch could be placed before patch 1, so that this change can be done only once.

In any case:

--
Thiago Jung Bauermann
IBM Linux Technology Center

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#13 lore raw

PATCH v4 6/7 kexec_elf: remove unused variable in kexec_elf_load()

In reply to Sven Schnelle

base was never assigned, so we can remove it.

---
 kernel/kexec_elf.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
kernel/kexec_elf.c +2 -5
index e346659af324..9421eebbacf0 100644
@@ -350,7 +350,7 @@ int kexec_elf_load(struct kimage *image, struct elfhdr *ehdr,
 			 struct kexec_buf *kbuf,
 			 unsigned long *lowest_load_addr)
 {
-	unsigned long base = 0, lowest_addr = UINT_MAX;
+	unsigned long lowest_addr = UINT_MAX;
 	int ret;
 	size_t i;
 
@@ -372,7 +372,7 @@ int kexec_elf_load(struct kimage *image, struct elfhdr *ehdr,
 		kbuf->bufsz = size;
 		kbuf->memsz = phdr->p_memsz;
 		kbuf->buf_align = phdr->p_align;
-		kbuf->buf_min = phdr->p_paddr + base;
+		kbuf->buf_min = phdr->p_paddr;
 		ret = kexec_add_buffer(kbuf);
 		if (ret)
 			goto out;
@@ -382,9 +382,6 @@ int kexec_elf_load(struct kimage *image, struct elfhdr *ehdr,
 			lowest_addr = load_addr;
 	}
 
-	/* Update entry point to reflect new load address. */
-	ehdr->e_entry += base;
-
 	*lowest_load_addr = lowest_addr;
 	ret = 0;
  out:
Signature · 7 lines
2.20.1


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
Reviewed-by on v4 6/7
#14 lore raw

Re: [PATCH v4 6/7] kexec_elf: remove unused variable in kexec_elf_load()

Quoted 31 lines · Sven Schnelle

base was never assigned, so we can remove it.

---
 kernel/kexec_elf.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
kernel/kexec_elf.c +2 -2
index e346659af324..9421eebbacf0 100644
@@ -350,7 +350,7 @@ int kexec_elf_load(struct kimage *image, struct elfhdr *ehdr,
 			 struct kexec_buf *kbuf,
 			 unsigned long *lowest_load_addr)
 {
-	unsigned long base = 0, lowest_addr = UINT_MAX;
+	unsigned long lowest_addr = UINT_MAX;
 	int ret;
 	size_t i;
 
@@ -372,7 +372,7 @@ int kexec_elf_load(struct kimage *image, struct elfhdr *ehdr,
 		kbuf->bufsz = size;
 		kbuf->memsz = phdr->p_memsz;
 		kbuf->buf_align = phdr->p_align;
-		kbuf->buf_min = phdr->p_paddr + base;
+		kbuf->buf_min = phdr->p_paddr;
 		ret = kexec_add_buffer(kbuf);
 		if (ret)
 			goto out;

kernel/kexec_elf.c int kexec_elf_load(struct kimage *image, struct elfhdr *ehdr, · line 387

Quoted 9 lines
kernel/kexec_elf.c +0 -3
@@ -382,9 +382,6 @@ int kexec_elf_load(struct kimage *image, struct elfhdr *ehdr,
 			lowest_addr = load_addr;
 	}
 
-	/* Update entry point to reflect new load address. */
-	ehdr->e_entry += base;
-
 	*lowest_load_addr = lowest_addr;
 	ret = 0;
  out:
Signature · 8 lines
Thiago Jung Bauermann
IBM Linux Technology Center


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
#15 lore raw

PATCH v4 7/7 kexec_elf: support 32 bit ELF files

In reply to Sven Schnelle

The powerpc version only supported 64 bit. Add some code to switch decoding of fields during runtime so we can kexec a 32 bit kernel from a 64 bit kernel and vice versa.

---
 kernel/kexec_elf.c | 57 ++++++++++++++++++++++++++++++++++------------
 1 file changed, 42 insertions(+), 15 deletions(-)
kernel/kexec_elf.c +42 -15
index 9421eebbacf0..a39d01154829 100644
@@ -8,8 +8,6 @@
 #include <linux/slab.h>
 #include <linux/types.h>
 
-#define elf_addr_to_cpu	elf64_to_cpu
-
 static inline bool elf_is_elf_file(const struct elfhdr *ehdr)
 {
 	return memcmp(ehdr->e_ident, ELFMAG, SELFMAG) == 0;
@@ -139,9 +137,6 @@ static int elf_read_ehdr(const char *buf, size_t len, struct elfhdr *ehdr)
 	ehdr->e_type      = elf16_to_cpu(ehdr, buf_ehdr->e_type);
 	ehdr->e_machine   = elf16_to_cpu(ehdr, buf_ehdr->e_machine);
 	ehdr->e_version   = elf32_to_cpu(ehdr, buf_ehdr->e_version);
-	ehdr->e_entry     = elf_addr_to_cpu(ehdr, buf_ehdr->e_entry);
-	ehdr->e_phoff     = elf_addr_to_cpu(ehdr, buf_ehdr->e_phoff);
-	ehdr->e_shoff     = elf_addr_to_cpu(ehdr, buf_ehdr->e_shoff);
 	ehdr->e_flags     = elf32_to_cpu(ehdr, buf_ehdr->e_flags);
 	ehdr->e_phentsize = elf16_to_cpu(ehdr, buf_ehdr->e_phentsize);
 	ehdr->e_phnum     = elf16_to_cpu(ehdr, buf_ehdr->e_phnum);
@@ -149,6 +144,24 @@ static int elf_read_ehdr(const char *buf, size_t len, struct elfhdr *ehdr)
 	ehdr->e_shnum     = elf16_to_cpu(ehdr, buf_ehdr->e_shnum);
 	ehdr->e_shstrndx  = elf16_to_cpu(ehdr, buf_ehdr->e_shstrndx);
 
+	switch (ehdr->e_ident[EI_CLASS]) {
+	case ELFCLASS64:
+		ehdr->e_entry     = elf64_to_cpu(ehdr, buf_ehdr->e_entry);
+		ehdr->e_phoff     = elf64_to_cpu(ehdr, buf_ehdr->e_phoff);
+		ehdr->e_shoff     = elf64_to_cpu(ehdr, buf_ehdr->e_shoff);
+		break;
+
+	case ELFCLASS32:
+		ehdr->e_entry     = elf32_to_cpu(ehdr, buf_ehdr->e_entry);
+		ehdr->e_phoff     = elf32_to_cpu(ehdr, buf_ehdr->e_phoff);
+		ehdr->e_shoff     = elf32_to_cpu(ehdr, buf_ehdr->e_shoff);
+		break;
+
+	default:
+		pr_debug("Unknown ELF class.\n");
+		return -EINVAL;
+	}
+
 	return elf_is_ehdr_sane(ehdr, len) ? 0 : -ENOEXEC;
 }
 
@@ -179,6 +192,7 @@ static int elf_read_phdr(const char *buf, size_t len,
 {
 	/* Override the const in proghdrs, we are the ones doing the loading. */
 	struct elf_phdr *phdr = (struct elf_phdr *) &elf_info->proghdrs[idx];
+	const struct elfhdr *ehdr = elf_info->ehdr;
 	const char *pbuf;
 	struct elf_phdr *buf_phdr;
 
@@ -186,18 +200,31 @@ static int elf_read_phdr(const char *buf, size_t len,
 	buf_phdr = (struct elf_phdr *) pbuf;
 
 	phdr->p_type   = elf32_to_cpu(elf_info->ehdr, buf_phdr->p_type);
-	phdr->p_offset = elf_addr_to_cpu(elf_info->ehdr, buf_phdr->p_offset);
-	phdr->p_paddr  = elf_addr_to_cpu(elf_info->ehdr, buf_phdr->p_paddr);
-	phdr->p_vaddr  = elf_addr_to_cpu(elf_info->ehdr, buf_phdr->p_vaddr);
 	phdr->p_flags  = elf32_to_cpu(elf_info->ehdr, buf_phdr->p_flags);
 
-	/*
-	 * The following fields have a type equivalent to Elf_Addr
-	 * both in 32 bit and 64 bit ELF.
-	 */
-	phdr->p_filesz = elf_addr_to_cpu(elf_info->ehdr, buf_phdr->p_filesz);
-	phdr->p_memsz  = elf_addr_to_cpu(elf_info->ehdr, buf_phdr->p_memsz);
-	phdr->p_align  = elf_addr_to_cpu(elf_info->ehdr, buf_phdr->p_align);
+	switch (ehdr->e_ident[EI_CLASS]) {
+	case ELFCLASS64:
+		phdr->p_offset = elf64_to_cpu(ehdr, buf_phdr->p_offset);
+		phdr->p_paddr  = elf64_to_cpu(ehdr, buf_phdr->p_paddr);
+		phdr->p_vaddr  = elf64_to_cpu(ehdr, buf_phdr->p_vaddr);
+		phdr->p_filesz = elf64_to_cpu(ehdr, buf_phdr->p_filesz);
+		phdr->p_memsz  = elf64_to_cpu(ehdr, buf_phdr->p_memsz);
+		phdr->p_align  = elf64_to_cpu(ehdr, buf_phdr->p_align);
+		break;
+
+	case ELFCLASS32:
+		phdr->p_offset = elf32_to_cpu(ehdr, buf_phdr->p_offset);
+		phdr->p_paddr  = elf32_to_cpu(ehdr, buf_phdr->p_paddr);
+		phdr->p_vaddr  = elf32_to_cpu(ehdr, buf_phdr->p_vaddr);
+		phdr->p_filesz = elf32_to_cpu(ehdr, buf_phdr->p_filesz);
+		phdr->p_memsz  = elf32_to_cpu(ehdr, buf_phdr->p_memsz);
+		phdr->p_align  = elf32_to_cpu(ehdr, buf_phdr->p_align);
+		break;
+
+	default:
+		pr_debug("Unknown ELF class.\n");
+		return -EINVAL;
+	}
 
 	return elf_is_phdr_sane(phdr, len) ? 0 : -ENOEXEC;
 }
Signature · 7 lines
2.20.1


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
Reviewed-by on v4 7/7
#16 lore raw

Re: [PATCH v4 7/7] kexec_elf: support 32 bit ELF files

Quoted 6 lines · Sven Schnelle

The powerpc version only supported 64 bit. Add some code to switch decoding of fields during runtime so we can kexec a 32 bit kernel from a 64 bit kernel and vice versa.

Signature · 7 lines
Thiago Jung Bauermann
IBM Linux Technology Center

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
Tested-by on the v4 series
#17 lore raw
In reply to Sven Schnelle
Quoted 27 lines · Sven Schnelle

Changes to v3:
- add support for 32-bit ELF files

Changes to v2:
- use git format-patch -C

Changes to v1:
- split up patch into smaller pieces
- rebase onto powerpc/next
- remove unused variable in kexec_elf_load()

Changes to RFC version:
- remove unused Elf_Rel macro
- remove section header parsing
- remove PURGATORY_STACK_SIZE
- change order of elf_*_to_cpu() functions
- remove elf_addr_to_cpu macro

Sven Schnelle (7):
kexec: add KEXEC_ELF
kexec_elf: change order of elf_*_to_cpu() functions
kexec_elf: remove parsing of section headers
kexec_elf: remove PURGATORY_STACK_SIZE
kexec_elf: remove Elf_Rel macro
kexec_elf: remove unused variable in kexec_elf_load()
kexec_elf: support 32 bit ELF files

Quoted 8 lines
 arch/Kconfig                       |   3 +
 arch/powerpc/Kconfig               |   1 +
 arch/powerpc/kernel/kexec_elf_64.c | 551 +----------------------------
 include/linux/kexec.h              |  23 ++
 kernel/Makefile                    |   1 +
 kernel/kexec_elf.c                 | 418 ++++++++++++++++++++++
 6 files changed, 456 insertions(+), 541 deletions(-)
 create mode 100644 kernel/kexec_elf.c

The series applies on v5.1 but not newer kernels, so it needs to be rebased.

I tested with v5.1 in ppc64le kexecing to both little-endian and big-endian kernels, and also in ppc64 kexecing to both big-endian and little-endian kernels so:

--
Thiago Jung Bauermann
IBM Linux Technology Center

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec