Skip to contentSkip to conversation

kexec: A new system call to allow in kernel loading

RFC PATCH Superseded Vivek Goyal 5 participants 53 messages Jan 27, 2014 – May 27, 2014

Subsystems (6)
x86 Kbuild ABI/API Live update +2 more subsystems

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover kexec: A new system call to allow in kernel loading

Hi

This is V2 of new system call patches. Previous version was posted here.

https://lkml.org/lkml/2013/11/20/540

V2 primarily does following changes

- Creates a binary object (called purgatory) which runs between two kernels. This is a stand alone relocatable object (it is not linked with kernel) and it is loaded and relocated by kexec syscall.

- Provided kexec support for loading ELF of type ET_EXEC. This only works for kexec case and not kexec on panic case. More about it patch changelog.

- Took care of feedback received during first round.

Primary goal of this patchset is to prepare groundwork so that kernel image can be signed and signatures be verified during kexec load. This should help with two things.

- It should allow kexec/kdump on secureboot enabled machines.

- In general it can help even without secureboot. By being able to verify kernel image signature in kexec, it should help with avoiding module signing restrictions. Matthew Garret showed how to boot into a custom kernel, modify first kernel's memory and then jump back to old kernel and bypass any policy one wants to.

I have not taken care of signing part yet. First I want to get to a stage where all the required pieces of kexec are re-implemented in kernel. And then I want to look into signing part. Also only 64bit bzImage entry is supported, no EFI/UEFI support, no x86_32 support. Trying to first come up with minimum functionality which matters most.

Posting patches for early reiew. Your feedback and comments are welcome.

Thanks
Vivek

Vivek Goyal (11):
kexec: Move segment verification code in a separate function
resource: Provide new functions to walk through resources
bin2c: Move bin2c in scripts/basic
kernel: Build bin2c based on config option CONFIG_BUILD_BIN2C
kexec: Make kexec_segment user buffer pointer a union
kexec: A new system call, kexec_file_load, for in kernel kexec
kexec: Create a relocatable object called purgatory
kexec-bzImage: Support for loading bzImage using 64bit entry
kexec: Provide a function to add a segment at fixed address
kexec: Support for loading ELF x86_64 images
kexec: Support for Kexec on panic using new system call

 arch/x86/Kbuild                      |    1 +
 arch/x86/Kconfig                     |    2 +
 arch/x86/Makefile                    |    6 +
 arch/x86/include/asm/crash.h         |    9 +
 arch/x86/include/asm/kexec-bzimage.h |   11 +
 arch/x86/include/asm/kexec-elf.h     |   11 +
 arch/x86/include/asm/kexec.h         |   51 ++
 arch/x86/kernel/Makefile             |    3 +
 arch/x86/kernel/crash.c              |  574 ++++++++++++++
 arch/x86/kernel/kexec-bzimage.c      |  255 +++++++
 arch/x86/kernel/kexec-elf.c          |  231 ++++++
 arch/x86/kernel/machine_kexec.c      |  149 ++++
 arch/x86/kernel/machine_kexec_64.c   |  173 +++++
 arch/x86/purgatory/Makefile          |   35 +
 arch/x86/purgatory/entry64.S         |  111 +++
 arch/x86/purgatory/purgatory.c       |  103 +++
 arch/x86/purgatory/setup-x86_32.S    |   29 +
 arch/x86/purgatory/setup-x86_64.S    |   68 ++
 arch/x86/purgatory/sha256.c          |  315 ++++++++
 arch/x86/purgatory/sha256.h          |   33 +
 arch/x86/purgatory/stack.S           |   29 +
 arch/x86/syscalls/syscall_64.tbl     |    1 +
 include/linux/ioport.h               |    6 +
 include/linux/kexec.h                |  102 ++-
 include/linux/syscalls.h             |    3 +
 include/uapi/linux/kexec.h           |    4 +
 init/Kconfig                         |    5 +
 kernel/Makefile                      |    2 +-
 kernel/kexec.c                       | 1356 +++++++++++++++++++++++++++++++---
 kernel/resource.c                    |  108 ++-
 kernel/sys_ni.c                      |    1 +
 scripts/Makefile                     |    1 -
 scripts/basic/Makefile               |    1 +
 scripts/basic/bin2c.c                |   36 +
 scripts/bin2c.c                      |   36 -
 35 files changed, 3701 insertions(+), 160 deletions(-)
 create mode 100644 arch/x86/include/asm/crash.h
 create mode 100644 arch/x86/include/asm/kexec-bzimage.h
 create mode 100644 arch/x86/include/asm/kexec-elf.h
 create mode 100644 arch/x86/kernel/kexec-bzimage.c
 create mode 100644 arch/x86/kernel/kexec-elf.c
 create mode 100644 arch/x86/kernel/machine_kexec.c
 create mode 100644 arch/x86/purgatory/Makefile
 create mode 100644 arch/x86/purgatory/entry64.S
 create mode 100644 arch/x86/purgatory/purgatory.c
 create mode 100644 arch/x86/purgatory/setup-x86_32.S
 create mode 100644 arch/x86/purgatory/setup-x86_64.S
 create mode 100644 arch/x86/purgatory/sha256.c
 create mode 100644 arch/x86/purgatory/sha256.h
 create mode 100644 arch/x86/purgatory/stack.S
 create mode 100644 scripts/basic/bin2c.c
 delete mode 100644 scripts/bin2c.c
Signature · 7 lines
1.8.4.2


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

PATCH v2 1/11 kexec: Move segment verification code in a separate function

Previously do_kimage_alloc() will allocate a kimage structure, copy segment list from user space and then do the segment list sanity verification.

Break down this function in 3 parts. do_kimage_alloc_init() to do actual allocation and basic initialization of kimage structure. copy_user_segment_list() to copy segment list from user space and sanity_check_segment_list() to verify the sanity of segment list as passed by user space.

In later patches, I need to only allocate kimage and not copy segment list from user space. So breaking down in smaller functions enables re-use of code at other places.

---
 kernel/kexec.c | 182 ++++++++++++++++++++++++++++++++-------------------------
 1 file changed, 101 insertions(+), 81 deletions(-)
kernel/kexec.c +101 -81
index ac73878..c0944b2 100644
@@ -123,45 +123,27 @@ static struct page *kimage_alloc_page(struct kimage *image,
 				       gfp_t gfp_mask,
 				       unsigned long dest);
 
-static int do_kimage_alloc(struct kimage **rimage, unsigned long entry,
-	                    unsigned long nr_segments,
-                            struct kexec_segment __user *segments)
+static int copy_user_segment_list(struct kimage *image,
+				unsigned long nr_segments,
+				struct kexec_segment __user *segments)
 {
+	int ret;
 	size_t segment_bytes;
-	struct kimage *image;
-	unsigned long i;
-	int result;
-
-	/* Allocate a controlling structure */
-	result = -ENOMEM;
-	image = kzalloc(sizeof(*image), GFP_KERNEL);
-	if (!image)
-		goto out;
-
-	image->head = 0;
-	image->entry = &image->head;
-	image->last_entry = &image->head;
-	image->control_page = ~0; /* By default this does not apply */
-	image->start = entry;
-	image->type = KEXEC_TYPE_DEFAULT;
-
-	/* Initialize the list of control pages */
-	INIT_LIST_HEAD(&image->control_pages);
-
-	/* Initialize the list of destination pages */
-	INIT_LIST_HEAD(&image->dest_pages);
-
-	/* Initialize the list of unusable pages */
-	INIT_LIST_HEAD(&image->unuseable_pages);
 
 	/* Read in the segments */
 	image->nr_segments = nr_segments;
 	segment_bytes = nr_segments * sizeof(*segments);
-	result = copy_from_user(image->segment, segments, segment_bytes);
-	if (result) {
-		result = -EFAULT;
-		goto out;
-	}
+	ret = copy_from_user(image->segment, segments, segment_bytes);
+	if (ret)
+		ret = -EFAULT;
+
+	return ret;
+}
+
+static int sanity_check_segment_list(struct kimage *image)
+{
+	int result, i;
+	unsigned long nr_segments = image->nr_segments;
 
 	/*
 	 * Verify we have good destination addresses.  The caller is
@@ -183,9 +165,9 @@ static int do_kimage_alloc(struct kimage **rimage, unsigned long entry,
 		mstart = image->segment[i].mem;
 		mend   = mstart + image->segment[i].memsz;
 		if ((mstart & ~PAGE_MASK) || (mend & ~PAGE_MASK))
-			goto out;
+			return result;
 		if (mend >= KEXEC_DESTINATION_MEMORY_LIMIT)
-			goto out;
+			return result;
 	}
 
 	/* Verify our destination addresses do not overlap.
@@ -206,7 +188,7 @@ static int do_kimage_alloc(struct kimage **rimage, unsigned long entry,
 			pend   = pstart + image->segment[j].memsz;
 			/* Do the segments overlap ? */
 			if ((mend > pstart) && (mstart < pend))
-				goto out;
+				return result;
 		}
 	}
 
@@ -218,18 +200,61 @@ static int do_kimage_alloc(struct kimage **rimage, unsigned long entry,
 	result = -EINVAL;
 	for (i = 0; i < nr_segments; i++) {
 		if (image->segment[i].bufsz > image->segment[i].memsz)
-			goto out;
+			return result;
 	}
 
-	result = 0;
-out:
-	if (result == 0)
-		*rimage = image;
-	else
-		kfree(image);
+	/*
+	 * Verify we have good destination addresses.  Normally
+	 * the caller is responsible for making certain we don't
+	 * attempt to load the new image into invalid or reserved
+	 * areas of RAM.  But crash kernels are preloaded into a
+	 * reserved area of ram.  We must ensure the addresses
+	 * are in the reserved area otherwise preloading the
+	 * kernel could corrupt things.
+	 */
 
-	return result;
+	if (image->type == KEXEC_TYPE_CRASH) {
+		result = -EADDRNOTAVAIL;
+		for (i = 0; i < nr_segments; i++) {
+			unsigned long mstart, mend;
 
+			mstart = image->segment[i].mem;
+			mend = mstart + image->segment[i].memsz - 1;
+			/* Ensure we are within the crash kernel limits */
+			if ((mstart < crashk_res.start) ||
+			    (mend > crashk_res.end))
+				return result;
+		}
+	}
+
+	return 0;
+}
+
+static struct kimage *do_kimage_alloc_init(void)
+{
+	struct kimage *image;
+
+	/* Allocate a controlling structure */
+	image = kzalloc(sizeof(*image), GFP_KERNEL);
+	if (!image)
+		return NULL;
+
+	image->head = 0;
+	image->entry = &image->head;
+	image->last_entry = &image->head;
+	image->control_page = ~0; /* By default this does not apply */
+	image->type = KEXEC_TYPE_DEFAULT;
+
+	/* Initialize the list of control pages */
+	INIT_LIST_HEAD(&image->control_pages);
+
+	/* Initialize the list of destination pages */
+	INIT_LIST_HEAD(&image->dest_pages);
+
+	/* Initialize the list of unusable pages */
+	INIT_LIST_HEAD(&image->unuseable_pages);
+
+	return image;
 }
 
 static void kimage_free_page_list(struct list_head *list);
@@ -242,10 +267,19 @@ static int kimage_normal_alloc(struct kimage **rimage, unsigned long entry,
 	struct kimage *image;
 
 	/* Allocate and initialize a controlling structure */
-	image = NULL;
-	result = do_kimage_alloc(&image, entry, nr_segments, segments);
+	image = do_kimage_alloc_init();
+	if (!image)
+		return -ENOMEM;
+
+	image->start = entry;
+
+	result = copy_user_segment_list(image, nr_segments, segments);
 	if (result)
-		goto out;
+		goto out_free_image;
+
+	result = sanity_check_segment_list(image);
+	if (result)
+		goto out_free_image;
 
 	/*
 	 * Find a location for the control code buffer, and add it
@@ -257,22 +291,23 @@ static int kimage_normal_alloc(struct kimage **rimage, unsigned long entry,
 					   get_order(KEXEC_CONTROL_PAGE_SIZE));
 	if (!image->control_code_page) {
 		printk(KERN_ERR "Could not allocate control_code_buffer\n");
-		goto out_free;
+		goto out_free_image;
 	}
 
 	image->swap_page = kimage_alloc_control_pages(image, 0);
 	if (!image->swap_page) {
 		printk(KERN_ERR "Could not allocate swap buffer\n");
-		goto out_free;
+		goto out_free_control_pages;
 	}
 
 	*rimage = image;
 	return 0;
 
-out_free:
+
+out_free_control_pages:
 	kimage_free_page_list(&image->control_pages);
+out_free_image:
 	kfree(image);
-out:
 	return result;
 }
 
@@ -282,19 +317,17 @@ static int kimage_crash_alloc(struct kimage **rimage, unsigned long entry,
 {
 	int result;
 	struct kimage *image;
-	unsigned long i;
 
-	image = NULL;
 	/* Verify we have a valid entry point */
-	if ((entry < crashk_res.start) || (entry > crashk_res.end)) {
-		result = -EADDRNOTAVAIL;
-		goto out;
-	}
+	if ((entry < crashk_res.start) || (entry > crashk_res.end))
+		return -EADDRNOTAVAIL;
 
 	/* Allocate and initialize a controlling structure */
-	result = do_kimage_alloc(&image, entry, nr_segments, segments);
-	if (result)
-		goto out;
+	image = do_kimage_alloc_init();
+	if (!image)
+		return -ENOMEM;
+
+	image->start = entry;
 
 	/* Enable the special crash kernel control page
 	 * allocation policy.
@@ -302,25 +335,13 @@ static int kimage_crash_alloc(struct kimage **rimage, unsigned long entry,
 	image->control_page = crashk_res.start;
 	image->type = KEXEC_TYPE_CRASH;
 
-	/*
-	 * Verify we have good destination addresses.  Normally
-	 * the caller is responsible for making certain we don't
-	 * attempt to load the new image into invalid or reserved
-	 * areas of RAM.  But crash kernels are preloaded into a
-	 * reserved area of ram.  We must ensure the addresses
-	 * are in the reserved area otherwise preloading the
-	 * kernel could corrupt things.
-	 */
-	result = -EADDRNOTAVAIL;
-	for (i = 0; i < nr_segments; i++) {
-		unsigned long mstart, mend;
+	result = copy_user_segment_list(image, nr_segments, segments);
+	if (result)
+		goto out_free_image;
 
-		mstart = image->segment[i].mem;
-		mend = mstart + image->segment[i].memsz - 1;
-		/* Ensure we are within the crash kernel limits */
-		if ((mstart < crashk_res.start) || (mend > crashk_res.end))
-			goto out_free;
-	}
+	result = sanity_check_segment_list(image);
+	if (result)
+		goto out_free_image;
 
 	/*
 	 * Find a location for the control code buffer, and add
@@ -332,15 +353,14 @@ static int kimage_crash_alloc(struct kimage **rimage, unsigned long entry,
 					   get_order(KEXEC_CONTROL_PAGE_SIZE));
 	if (!image->control_code_page) {
 		printk(KERN_ERR "Could not allocate control_code_buffer\n");
-		goto out_free;
+		goto out_free_image;
 	}
 
 	*rimage = image;
 	return 0;
 
-out_free:
+out_free_image:
 	kfree(image);
-out:
 	return result;
 }
 
Signature · 7 lines
1.8.4.2


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

PATCH v2 2/11 resource: Provide new functions to walk through resources

In reply to Vivek Goyal

I have added two more functions to walk through resources. Current walk_system_ram_range() deals with pfn and /proc/iomem can contain partial pages. By dealing in pfn, callback function loses the info that last page of a memory range is a partial page and not the full page. So I implemented walk_system_ran_res() which returns u64 values to callback functions and now it properly return start and end address.

walk_system_ram_range() uses find_next_system_ram() to find the next ram resource. This in turn only travels through siblings of top level child and does not travers through all the nodes of the resoruce tree. I also need another function where I can walk through all the resources, for example figure out where "GART" aperture is. Figure out where ACPI memory is.

So I wrote another function walk_ram_res() which walks through all /proc/iomem resources and returns matches as asked by caller. Caller can specify "name" of resource, start and end.

---
 include/linux/ioport.h |   6 +++
 kernel/resource.c      | 108 +++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 110 insertions(+), 4 deletions(-)
include/linux/ioport.h +6 -0
index 89b7c24..0ebf8b0 100644
@@ -227,6 +227,12 @@ extern int iomem_is_exclusive(u64 addr);
 extern int
 walk_system_ram_range(unsigned long start_pfn, unsigned long nr_pages,
 		void *arg, int (*func)(unsigned long, unsigned long, void *));
+extern int
+walk_system_ram_res(u64 start, u64 end, void *arg,
+				int (*func)(u64, u64, void *));
+extern int
+walk_ram_res(char *name, unsigned long flags, u64 start, u64 end, void *arg,
+				int (*func)(u64, u64, void *));
 
 /* True if any part of r1 overlaps r2 */
 static inline bool resource_overlaps(struct resource *r1, struct resource *r2)
kernel/resource.c +104 -4

133 lines not on this page · raw message

Signature · 7 lines
1.8.4.2


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

PATCH v2 3/11 bin2c: Move bin2c in scripts/basic

In reply to Vivek Goyal

Kexec wants to use bin2c and it wants to use it really early in the build process. See arch/x86/purgatory/ code in later patches.

So move bin2c in scripts/basic so that it can be built very early and be usable by arch/x86/purgatory/

---
 kernel/Makefile        |  2 +-
 scripts/Makefile       |  1 -
 scripts/basic/Makefile |  1 +
 scripts/basic/bin2c.c  | 36 ++++++++++++++++++++++++++++++++++++
 scripts/bin2c.c        | 36 ------------------------------------
 5 files changed, 38 insertions(+), 38 deletions(-)
 create mode 100644 scripts/basic/bin2c.c
 delete mode 100644 scripts/bin2c.c
kernel/Makefile +1 -1
index bc010ee..dff602a 100644
@@ -102,7 +102,7 @@ targets += config_data.gz
 $(obj)/config_data.gz: $(KCONFIG_CONFIG) FORCE
 	$(call if_changed,gzip)
 
-      filechk_ikconfiggz = (echo "static const char kernel_config_data[] __used = MAGIC_START"; cat $< | scripts/bin2c; echo "MAGIC_END;")
+      filechk_ikconfiggz = (echo "static const char kernel_config_data[] __used = MAGIC_START"; cat $< | scripts/basic/bin2c; echo "MAGIC_END;")
 targets += config_data.h
 $(obj)/config_data.h: $(obj)/config_data.gz FORCE
 	$(call filechk,ikconfiggz)
scripts/Makefile +0 -1
index 01e7adb..62e6cc2 100644
@@ -13,7 +13,6 @@ HOST_EXTRACFLAGS += -I$(srctree)/tools/include
 hostprogs-$(CONFIG_KALLSYMS)     += kallsyms
 hostprogs-$(CONFIG_LOGO)         += pnmtologo
 hostprogs-$(CONFIG_VT)           += conmakehash
-hostprogs-$(CONFIG_IKCONFIG)     += bin2c
 hostprogs-$(BUILD_C_RECORDMCOUNT) += recordmcount
 hostprogs-$(CONFIG_BUILDTIME_EXTABLE_SORT) += sortextable
 hostprogs-$(CONFIG_ASN1)	 += asn1_compiler
scripts/basic/Makefile +1 -0
index 4fcef87..afbc1cd 100644
@@ -9,6 +9,7 @@
 # fixdep: 	 Used to generate dependency information during build process
 
 hostprogs-y	:= fixdep
+hostprogs-$(CONFIG_IKCONFIG)     += bin2c
 always		:= $(hostprogs-y)
 
 # fixdep is needed to compile other host programs
scripts/basic/bin2c.c new file +36 -0
new file mode 100644
index 0000000..96dd2bc
@@ -0,0 +1,36 @@
+/*
+ * Unloved program to convert a binary on stdin to a C include on stdout
+ *
+ * Jan 1999 Matt Mackall <[email protected]>
+ *
+ * This software may be used and distributed according to the terms
+ * of the GNU General Public License, incorporated herein by reference.
+ */
+
+#include <stdio.h>
+
+int main(int argc, char *argv[])
+{
+	int ch, total=0;
+
+	if (argc > 1)
+		printf("const char %s[] %s=\n",
+			argv[1], argc > 2 ? argv[2] : "");
+
+	do {
+		printf("\t\"");
+		while ((ch = getchar()) != EOF)
+		{
+			total++;
+			printf("\\x%02x",ch);
+			if (total % 16 == 0)
+				break;
+		}
+		printf("\"\n");
+	} while (ch != EOF);
+
+	if (argc > 1)
+		printf("\t;\n\nconst int %s_size = %d;\n", argv[1], total);
+
+	return 0;
+}
scripts/bin2c.c deleted +0 -36
deleted file mode 100644
index 96dd2bc..0000000
@@ -1,36 +0,0 @@
-/*
- * Unloved program to convert a binary on stdin to a C include on stdout
- *
- * Jan 1999 Matt Mackall <[email protected]>
- *
- * This software may be used and distributed according to the terms
- * of the GNU General Public License, incorporated herein by reference.
- */
-
-#include <stdio.h>
-
-int main(int argc, char *argv[])
-{
-	int ch, total=0;
-
-	if (argc > 1)
-		printf("const char %s[] %s=\n",
-			argv[1], argc > 2 ? argv[2] : "");
-
-	do {
-		printf("\t\"");
-		while ((ch = getchar()) != EOF)
-		{
-			total++;
-			printf("\\x%02x",ch);
-			if (total % 16 == 0)
-				break;
-		}
-		printf("\"\n");
-	} while (ch != EOF);
-
-	if (argc > 1)
-		printf("\t;\n\nconst int %s_size = %d;\n", argv[1], total);
-
-	return 0;
-}
Signature · 7 lines
1.8.4.2


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

Re: [PATCH 03/11] bin2c: Move bin2c in scripts/basic

Dne 27.1.2014 19:57, Vivek Goyal napsal(a):

Quoted 8 lines

Kexec wants to use bin2c and it wants to use it really early in the build process. See arch/x86/purgatory/ code in later patches.

So move bin2c in scripts/basic so that it can be built very early and be usable by arch/x86/purgatory/

---

[...]

scripts/basic/Makefile line 12

Quoted 9 lines
scripts/basic/Makefile +1 -0
index 4fcef87..afbc1cd 100644
@@ -9,6 +9,7 @@
 # fixdep: 	 Used to generate dependency information during build process
 
 hostprogs-y	:= fixdep
+hostprogs-$(CONFIG_IKCONFIG)     += bin2c

Is the CONFIG_IKCONFIG dependency still correct, now that you found another use of bin2c?

Thanks,
Michal

#5 lore raw

Re: [PATCH 03/11] bin2c: Move bin2c in scripts/basic

Quoted 17 lines · Michal Marek

Dne 27.1.2014 19:57, Vivek Goyal napsal(a):

Quoted 8 lines

Kexec wants to use bin2c and it wants to use it really early in the build process. See arch/x86/purgatory/ code in later patches.

So move bin2c in scripts/basic so that it can be built very early and be usable by arch/x86/purgatory/

---

[...]

Quoted 7 lines
scripts/basic/Makefile +0 -0
index 4fcef87..afbc1cd 100644
@@ -9,6 +9,7 @@
 # fixdep: 	 Used to generate dependency information during build process
 
Quoted 5 lines

scripts/basic/Makefile line 12

Quoted 2 lines
scripts/basic/Makefile +1 -0
 hostprogs-y	:= fixdep
+hostprogs-$(CONFIG_IKCONFIG)     += bin2c

Is the CONFIG_IKCONFIG dependency still correct, now that you found another use of bin2c?

In the next patch I changed depedency too. (CONFIG_BUILD_BIN2C). And now CONFIG_IKCONFIG and CONFIG_KEXEC will select CONFIG_BUILD_BIN2C.

Thanks
Vivek

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

#6 lore raw

Re: [PATCH 03/11] bin2c: Move bin2c in scripts/basic

Dne 27.1.2014 22:18, Vivek Goyal napsal(a):

Quoted 18 lines · Michal Marek
Quoted 17 lines

Dne 27.1.2014 19:57, Vivek Goyal napsal(a):

Quoted 8 lines

Kexec wants to use bin2c and it wants to use it really early in the build process. See arch/x86/purgatory/ code in later patches.

So move bin2c in scripts/basic so that it can be built very early and be usable by arch/x86/purgatory/

---

[...]

Quoted 7 lines
scripts/basic/Makefile +0 -0
index 4fcef87..afbc1cd 100644
@@ -9,6 +9,7 @@
 # fixdep: 	 Used to generate dependency information during build process
 
Quoted 8 lines
Quoted 5 lines

scripts/basic/Makefile line 12

Quoted 2 lines
scripts/basic/Makefile +1 -0
 hostprogs-y	:= fixdep
+hostprogs-$(CONFIG_IKCONFIG)     += bin2c

Is the CONFIG_IKCONFIG dependency still correct, now that you found another use of bin2c?

In the next patch I changed depedency too. (CONFIG_BUILD_BIN2C). And now CONFIG_IKCONFIG and CONFIG_KEXEC will select CONFIG_BUILD_BIN2C.

OK, then it's all fine. I only saw patch 03/11.

Thanks,
Michal

#7 lore raw

PATCH v2 4/11 kernel: Build bin2c based on config option CONFIG_BUILD_BIN2C

In reply to Vivek Goyal

currently bin2c builds only if CONFIG_IKCONFIG=y. But bin2c will now be used by kexec too. So make it compilation dependent on CONFIG_BUILD_BIN2C and this config option can be selected by CONFIG_KEXEC and CONFIG_IKCONFIG.

---
 arch/x86/Kconfig       | 1 +
 init/Kconfig           | 5 +++++
 scripts/basic/Makefile | 2 +-
 3 files changed, 7 insertions(+), 1 deletion(-)
arch/x86/Kconfig +1 -0
index 940e50e..aa5aeed 100644
@@ -1604,6 +1604,7 @@ source kernel/Kconfig.hz
 
 config KEXEC
 	bool "kexec system call"
+	select BUILD_BIN2C
 	---help---
 	  kexec is a system call that implements the ability to shutdown your
 	  current kernel, and to start another kernel.  It is like a reboot
init/Kconfig +5 -0
index 34a0a3b..32a1adb 100644
@@ -760,8 +760,13 @@ endchoice
 
 endmenu # "RCU Subsystem"
 
+config BUILD_BIN2C
+	bool
+	default n
+
 config IKCONFIG
 	tristate "Kernel .config support"
+	select BUILD_BIN2C
 	---help---
 	  This option enables the complete Linux kernel ".config" file
 	  contents to be saved in the kernel. It provides documentation
scripts/basic/Makefile +1 -1
index afbc1cd..ec10d93 100644
@@ -9,7 +9,7 @@
 # fixdep: 	 Used to generate dependency information during build process
 
 hostprogs-y	:= fixdep
-hostprogs-$(CONFIG_IKCONFIG)     += bin2c
+hostprogs-$(CONFIG_BUILD_BIN2C)     += bin2c
 always		:= $(hostprogs-y)
 
 # fixdep is needed to compile other host programs
Signature · 7 lines
1.8.4.2


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

PATCH v2 5/11 kexec: Make kexec_segment user buffer pointer a union

In reply to Vivek Goyal

So far kexec_segment->buf was always a user space pointer as user space passed the array of kexec_segment structures and kernel copied it.

But with new system call, list of kexec segments will be prepared by kernel and kexec_segment->buf will point to a kernel memory.

So while I was adding code where I made assumption that ->buf is pointing to kernel memory, sparse started giving warning.

Make ->buf a union. And where a user space pointer is expected, access it using ->buf and where a kernel space pointer is expected, access it using ->kbuf. That takes care of sparse warnings.

---
 include/linux/kexec.h | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
include/linux/kexec.h +12 -1
index 6d4066c..d8188b3 100644
@@ -69,7 +69,18 @@ typedef unsigned long kimage_entry_t;
 #define IND_SOURCE       0x8
 
 struct kexec_segment {
-	void __user *buf;
+	/*
+	 * This pointer can point to user memory if kexec_load() system
+	 * call is used or will point to kernel memory if
+	 * kexec_file_load() system call is used.
+	 *
+	 * Use ->buf when expecting to deal with user memory and use ->kbuf
+	 * when expecting to deal with kernel memory.
+	 */
+	union {
+		void __user *buf;
+		void *kbuf;
+	};
 	size_t bufsz;
 	unsigned long mem;
 	size_t memsz;
Signature · 7 lines
1.8.4.2


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

PATCH v2 6/11 kexec: A new system call, kexec_file_load, for in kernel kexec

In reply to Vivek Goyal

This patch implements the in kernel kexec functionality. It implements a new system call kexec_file_load. I think parameter list of this system call will change as I have not done the kernel image signature handling yet. I have been told that I might have to pass the detached signature and size as part of system call.

Previously segment list was prepared in user space. Now user space just passes kernel fd, initrd fd and command line and kernel will create a segment list internally.

This patch contains generic part of the code. Actual segment preparation and loading is done by arch and image specific loader. Which comes in next patch.

---
 arch/x86/kernel/machine_kexec_64.c |  50 ++++
 arch/x86/syscalls/syscall_64.tbl   |   1 +
 include/linux/kexec.h              |  55 +++++
 include/linux/syscalls.h           |   3 +
 include/uapi/linux/kexec.h         |   4 +
 kernel/kexec.c                     | 495 ++++++++++++++++++++++++++++++++++++-
 kernel/sys_ni.c                    |   1 +
 7 files changed, 605 insertions(+), 4 deletions(-)
arch/x86/kernel/machine_kexec_64.c +50 -0
index 4eabc16..c91d72a 100644
@@ -22,6 +22,13 @@
 #include <asm/mmu_context.h>
 #include <asm/debugreg.h>
 
+/* arch dependent functionality related to kexec file based syscall */
+static struct kexec_file_type kexec_file_type[]={
+	{"", NULL, NULL, NULL},
+};
+
+static int nr_file_types = sizeof(kexec_file_type)/sizeof(kexec_file_type[0]);
+
 static void free_transition_pgtable(struct kimage *image)
 {
 	free_page((unsigned long)image->arch.pud);
@@ -281,3 +288,46 @@ void arch_crash_save_vmcoreinfo(void)
 #endif
 }
 
+/* arch dependent functionality related to kexec file based syscall */
+
+int arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
+					unsigned long buf_len)
+{
+	int i, ret = -ENOEXEC;
+
+	for (i = 0; i < nr_file_types; i++) {
+		if (!kexec_file_type[i].probe)
+			continue;
+
+		ret = kexec_file_type[i].probe(buf, buf_len);
+		if (!ret) {
+			image->file_handler_idx = i;
+			return ret;
+		}
+	}
+
+	return ret;
+}
+
+void *arch_kexec_kernel_image_load(struct kimage *image, char *kernel,
+			unsigned long kernel_len, char *initrd,
+			unsigned long initrd_len, char *cmdline,
+			unsigned long cmdline_len)
+{
+	int idx = image->file_handler_idx;
+
+	if (idx < 0)
+		return ERR_PTR(-ENOEXEC);
+
+	return kexec_file_type[idx].load(image, kernel, kernel_len, initrd,
+					initrd_len, cmdline, cmdline_len);
+}
+
+int arch_kimage_file_post_load_cleanup(struct kimage *image)
+{
+	int idx = image->file_handler_idx;
+
+	if (kexec_file_type[idx].cleanup)
+		return kexec_file_type[idx].cleanup(image);
+	return 0;
+}
arch/x86/syscalls/syscall_64.tbl +1 -0
index a12bddc..3eec4d4 100644
@@ -322,6 +322,7 @@
 313	common	finit_module		sys_finit_module
 314	common	sched_setattr		sys_sched_setattr
 315	common	sched_getattr		sys_sched_getattr
+316	common	kexec_file_load		sys_kexec_file_load
 
 #
 # x32-specific system call numbers start at 512 to avoid cache impact
include/linux/kexec.h +55 -0

86 lines not on this page · raw message

include/linux/syscalls.h +3 -0
index 40ed9e9..db55884 100644
@@ -307,6 +307,9 @@ asmlinkage long sys_restart_syscall(void);
 asmlinkage long sys_kexec_load(unsigned long entry, unsigned long nr_segments,
 				struct kexec_segment __user *segments,
 				unsigned long flags);
+asmlinkage long sys_kexec_file_load(int kernel_fd, int initrd_fd,
+				const char __user * cmdline_ptr,
+				unsigned long cmdline_len, unsigned long flags);
 
 asmlinkage long sys_exit(int error_code);
 asmlinkage long sys_exit_group(int error_code);
include/uapi/linux/kexec.h +4 -0
index d6629d4..5fddb1b 100644
@@ -13,6 +13,10 @@
 #define KEXEC_PRESERVE_CONTEXT	0x00000002
 #define KEXEC_ARCH_MASK		0xffff0000
 
+/* Kexec file load interface flags */
+#define KEXEC_FILE_UNLOAD	0x00000001
+#define KEXEC_FILE_ON_CRASH	0x00000002
+
 /* These values match the ELF architecture values.
  * Unless there is a good reason that should continue to be the case.
  */
kernel/kexec.c +491 -4

545 lines not on this page · raw message

kernel/sys_ni.c +1 -0
index 7078052..7e1e13d 100644
@@ -25,6 +25,7 @@ cond_syscall(sys_swapon);
 cond_syscall(sys_swapoff);
 cond_syscall(sys_kexec_load);
 cond_syscall(compat_sys_kexec_load);
+cond_syscall(sys_kexec_file_load);
 cond_syscall(sys_init_module);
 cond_syscall(sys_finit_module);
 cond_syscall(sys_delete_module);
Signature · 7 lines
1.8.4.2


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

Re: [PATCH 06/11] kexec: A new system call, kexec_file_load, for in kernel kexec

Quoted 10 lines · Vivek Goyal

This patch implements the in kernel kexec functionality. It implements a new system call kexec_file_load. I think parameter list of this system call will change as I have not done the kernel image signature handling yet. I have been told that I might have to pass the detached signature and size as part of system call.

Previously segment list was prepared in user space. Now user space just passes kernel fd, initrd fd and command line and kernel will create a segment list internally.

Quoted 5 lines

This patch contains generic part of the code. Actual segment preparation and loading is done by arch and image specific loader. Which comes in next patch.

You might want to run it through checkpatch - some of them are actually, to my surprise, legitimate :)

Just some minor nitpicks below...

Quoted 9 lines
---
 arch/x86/kernel/machine_kexec_64.c |  50 ++++
 arch/x86/syscalls/syscall_64.tbl   |   1 +
 include/linux/kexec.h              |  55 +++++
 include/linux/syscalls.h           |   3 +
 include/uapi/linux/kexec.h         |   4 +
 kernel/kexec.c                     | 495 ++++++++++++++++++++++++++++++++++++-
 kernel/sys_ni.c                    |   1 +
 7 files changed, 605 insertions(+), 4 deletions(-)

...

Quoted 35 lines
include/linux/kexec.h +21 -0
index d8188b3..51b56cd 100644
@@ -121,13 +121,58 @@ struct kimage {
 #define KEXEC_TYPE_DEFAULT 0
 #define KEXEC_TYPE_CRASH   1
 	unsigned int preserve_context : 1;
+	/* If set, we are using file mode kexec syscall */
+	unsigned int file_mode : 1;
 
 #ifdef ARCH_HAS_KIMAGE_ARCH
 	struct kimage_arch arch;
 #endif
+
+	/* Additional Fields for file based kexec syscall */
+	void *kernel_buf;
+	unsigned long kernel_buf_len;
+
+	void *initrd_buf;
+	unsigned long initrd_buf_len;
+
+	char *cmdline_buf;
+	unsigned long cmdline_buf_len;
+
+	/* index of file handler in array */
+	int file_handler_idx;
+
+	/* Image loader handling the kernel can store a pointer here */
+	void * image_loader_data;
 };
 
+/*
+ * Keeps a track of buffer parameters as provided by caller for requesting
+ * memory placement of buffer.

include/linux/kexec.h struct kimage { · line 160

Quoted 10 lines
include/linux/kexec.h +10 -0
+ */
+struct kexec_buf {
+	struct kimage *image;
+	char *buffer;
+	unsigned long bufsz;
+	unsigned long memsz;
+	unsigned long buf_align;
+	unsigned long buf_min;
+	unsigned long buf_max;
+	int top_down;		/* allocate from top of memory hole */

Looks like this wants to be a bool.

...

include/uapi/linux/kexec.h line 18

Quoted 11 lines
include/uapi/linux/kexec.h +3 -0
index d6629d4..5fddb1b 100644
@@ -13,6 +13,10 @@
 #define KEXEC_PRESERVE_CONTEXT	0x00000002
 #define KEXEC_ARCH_MASK		0xffff0000
 
+/* Kexec file load interface flags */
+#define KEXEC_FILE_UNLOAD	0x00000001
+#define KEXEC_FILE_ON_CRASH	0x00000002

BIT()

Quoted 9 lines
include/uapi/linux/kexec.h +1 -0
+
 /* These values match the ELF architecture values.
  * Unless there is a good reason that should continue to be the case.
  */
kernel/kexec.c +0 -0
index c0944b2..b28578a 100644

kernel/kexec.c static struct page *kimage_alloc_page(struct kimage *image, · line 129

Quoted 7 lines
kernel/kexec.c +4 -0
@@ -123,6 +123,11 @@ static struct page *kimage_alloc_page(struct kimage *image,
 				       gfp_t gfp_mask,
 				       unsigned long dest);
 
+void kimage_set_start_addr(struct kimage *image, unsigned long start)
+{
+	image->start = start;
+}

Why a separate function? It is used only once in the next patch.

...

Quoted 46 lines
kernel/kexec.c +46 -0
+static int kimage_file_prepare_segments(struct kimage *image, int kernel_fd,
+		int initrd_fd, const char __user *cmdline_ptr,
+		unsigned long cmdline_len)
+{
+	int ret = 0;
+	void *ldata;
+
+	ret = copy_file_from_fd(kernel_fd, &image->kernel_buf,
+					&image->kernel_buf_len);
+	if (ret)
+		goto out;
+
+	/* Call arch image probe handlers */
+	ret = arch_kexec_kernel_image_probe(image, image->kernel_buf,
+						image->kernel_buf_len);
+
+	if (ret)
+		goto out;
+
+	ret = copy_file_from_fd(initrd_fd, &image->initrd_buf,
+					&image->initrd_buf_len);
+	if (ret)
+		goto out;
+
+	image->cmdline_buf = vzalloc(cmdline_len);
+	if (!image->cmdline_buf)
+		goto out;
+
+	ret = copy_from_user(image->cmdline_buf, cmdline_ptr, cmdline_len);
+	if (ret) {
+		ret = -EFAULT;
+		goto out;
+	}
+
+	image->cmdline_buf_len = cmdline_len;
+
+	/* command line should be a string with last byte null */
+	if (image->cmdline_buf[cmdline_len - 1] != '\0') {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	/* Call arch image load handlers */
+	ldata = arch_kexec_kernel_image_load(image,
+			image->kernel_buf, image->kernel_buf_len,
+			image->initrd_buf, image->initrd_buf_len,

kernel/kexec.c static struct kimage *do_kimage_alloc_init(void) · line 426

Quoted 10 lines
kernel/kexec.c +10 -0
+			image->cmdline_buf, image->cmdline_buf_len);
+
+	if (IS_ERR(ldata)) {
+		ret = PTR_ERR(ldata);
+		goto out;
+	}
+
+	image->image_loader_data = ldata;
+out:
+	return ret;

You probably want to drop this "out:" label and simply return the error value directly in each error path above for simplicity.

Quoted 13 lines
kernel/kexec.c +13 -0
+static int kimage_file_normal_alloc(struct kimage **rimage, int kernel_fd,
+		int initrd_fd, const char __user *cmdline_ptr,
+		unsigned long cmdline_len)
+{
+	int result;
+	struct kimage *image;
+
+	/* Allocate and initialize a controlling structure */
+	image = do_kimage_alloc_init();
+	if (!image)
+		return -ENOMEM;
+
+	image->file_mode = 1;

kernel/kexec.c static struct kimage *do_kimage_alloc_init(void) · line 451

Quoted 10 lines
kernel/kexec.c +10 -0
+	image->file_handler_idx = -1;
+
+	result = kimage_file_prepare_segments(image, kernel_fd, initrd_fd,
+			cmdline_ptr, cmdline_len);
+	if (result)
+		goto out_free_image;
+
+	result = sanity_check_segment_list(image);
+	if (result)
+		goto out_free_post_load_bufs;

Dunno, it could probably be a larger restructuring effort but if you do load a segment and sanity-check it right after loading, instead of loading all of them first and then iterating over them, you could save yourself some work in the error case when a segment fails the check.

...

Quoted 38 lines
kernel/kexec.c +38 -0
+int kexec_add_buffer(struct kimage *image, char *buffer,
+		unsigned long bufsz, unsigned long memsz,
+		unsigned long buf_align, unsigned long buf_min,
+		unsigned long buf_max, int top_down, unsigned long *load_addr)
+{
+
+	unsigned long nr_segments = image->nr_segments, new_nr_segments;
+	struct kexec_segment *ksegment;
+	struct kexec_buf *kbuf;
+
+	/* Currently adding segment this way is allowed only in file mode */
+	if (!image->file_mode)
+		return -EINVAL;
+
+	if (nr_segments >= KEXEC_SEGMENT_MAX)
+		return -EINVAL;
+
+	/*
+	 * Make sure we are not trying to add buffer after allocating
+	 * control pages. All segments need to be placed first before
+	 * any control pages are allocated. As control page allocation
+	 * logic goes through list of segments to make sure there are
+	 * no destination overlaps.
+	 */
+	WARN_ONCE(!list_empty(&image->control_pages), "Adding kexec buffer"
+			" after allocating control pages\n");
+
+	kbuf = kzalloc(sizeof(struct kexec_buf), GFP_KERNEL);
+	if (!kbuf)
+		return -ENOMEM;
+
+	kbuf->image = image;
+	kbuf->buffer = buffer;
+	kbuf->bufsz = bufsz;
+	/* Align memsz to next page boundary */
+	kbuf->memsz = ALIGN(memsz, PAGE_SIZE);
+
+	/* Align to atleast page size boundary */

kernel/kexec.c static int __init crash_save_vmcoreinfo_init(void) · line 2121

Quoted 10 lines
kernel/kexec.c +10 -0
+	kbuf->buf_align = max(buf_align, PAGE_SIZE);
+	kbuf->buf_min = buf_min;
+	kbuf->buf_max = buf_max;
+	kbuf->top_down = top_down;
+
+	/* Walk the RAM ranges and allocate a suitable range for the buffer */
+	walk_system_ram_res(0, -1, kbuf, walk_ram_range_callback);
+
+	kbuf->image = NULL;
+	kfree(kbuf);

This is freed after kzalloc'ing it a bit earlier, why not make it a stack variable for simplicity? struct kexec_buf doesn't seem that large...

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
#11 lore raw

Re: [PATCH 06/11] kexec: A new system call, kexec_file_load, for in kernel kexec

On Fri, Feb 21, 2014 at 03:59:10PM +0100, Borislav Petkov wrote:

[..]

Quoted 2 lines

You might want to run it through checkpatch - some of them are actually, to my surprise, legitimate :)

Thanks for having a look at the patches. I will run patches through checkpatch before next posting.

[..]

Quoted 11 lines

include/linux/kexec.h struct kimage { · line 160

Quoted 9 lines
include/linux/kexec.h +9 -0
+struct kexec_buf {
+	struct kimage *image;
+	char *buffer;
+	unsigned long bufsz;
+	unsigned long memsz;
+	unsigned long buf_align;
+	unsigned long buf_min;
+	unsigned long buf_max;
+	int top_down;		/* allocate from top of memory hole */

Looks like this wants to be a bool.

Will change.

Quoted 11 lines

...

Quoted 8 lines
include/uapi/linux/kexec.h +0 -0
index d6629d4..5fddb1b 100644
@@ -13,6 +13,10 @@
 #define KEXEC_PRESERVE_CONTEXT	0x00000002
 #define KEXEC_ARCH_MASK		0xffff0000
 
Quoted 5 lines

include/uapi/linux/kexec.h line 18

Quoted 3 lines
include/uapi/linux/kexec.h +3 -0
+/* Kexec file load interface flags */
+#define KEXEC_FILE_UNLOAD	0x00000001
+#define KEXEC_FILE_ON_CRASH	0x00000002

BIT()

What's that?

Quoted 13 lines
Quoted 13 lines
include/uapi/linux/kexec.h +1 -0
+
 /* These values match the ELF architecture values.
  * Unless there is a good reason that should continue to be the case.
  */
kernel/kexec.c +0 -0
index c0944b2..b28578a 100644
@@ -123,6 +123,11 @@ static struct page *kimage_alloc_page(struct kimage *image,
 				       gfp_t gfp_mask,
 				       unsigned long dest);
 
Quoted 6 lines

kernel/kexec.c static struct page *kimage_alloc_page(struct kimage *image, · line 129

Quoted 4 lines
kernel/kexec.c +4 -0
+void kimage_set_start_addr(struct kimage *image, unsigned long start)
+{
+	image->start = start;
+}

Why a separate function? It is used only once in the next patch.

Right now there is only one user. But once other image loader support comes along or other arches support file based kexec, they can make use of same function.

This is a pretty important modification as it decides what's the starting point of next kernel image. I wanted to make it a function callable by users who wanted to modify it instead of of letting them directly modify image->start.

[..]

Quoted 9 lines
Quoted 9 lines
kernel/kexec.c +9 -0
+	/* Call arch image load handlers */
+	ldata = arch_kexec_kernel_image_load(image,
+			image->kernel_buf, image->kernel_buf_len,
+			image->initrd_buf, image->initrd_buf_len,
+			image->cmdline_buf, image->cmdline_buf_len);
+
+	if (IS_ERR(ldata)) {
+		ret = PTR_ERR(ldata);
+		goto out;
Quoted 9 lines

kernel/kexec.c static struct kimage *do_kimage_alloc_init(void) · line 426

Quoted 5 lines
kernel/kexec.c +5 -0
+	}
+
+	image->image_loader_data = ldata;
+out:
+	return ret;

You probably want to drop this "out:" label and simply return the error value directly in each error path above for simplicity.

Ok, will do. I don't seem to be doing anything in "out" except return ret. So will get rid of this label and return early in error path.

Quoted 20 lines
Quoted 20 lines
kernel/kexec.c +20 -0
+static int kimage_file_normal_alloc(struct kimage **rimage, int kernel_fd,
+		int initrd_fd, const char __user *cmdline_ptr,
+		unsigned long cmdline_len)
+{
+	int result;
+	struct kimage *image;
+
+	/* Allocate and initialize a controlling structure */
+	image = do_kimage_alloc_init();
+	if (!image)
+		return -ENOMEM;
+
+	image->file_mode = 1;
+	image->file_handler_idx = -1;
+
+	result = kimage_file_prepare_segments(image, kernel_fd, initrd_fd,
+			cmdline_ptr, cmdline_len);
+	if (result)
+		goto out_free_image;
+
Quoted 9 lines

kernel/kexec.c static struct kimage *do_kimage_alloc_init(void) · line 451

Quoted 3 lines
kernel/kexec.c +3 -0
+	result = sanity_check_segment_list(image);
+	if (result)
+		goto out_free_post_load_bufs;

Dunno, it could probably be a larger restructuring effort but if you do load a segment and sanity-check it right after loading, instead of loading all of them first and then iterating over them, you could save yourself some work in the error case when a segment fails the check.

Could be. I am trying to reuse exisitng code. In current code, user space passes a list of segments and then kernel calls this function to make sure all the segments passed in make sense.

Here also arch dependent part of kernel is preparing a list of segments wanted and then generic code is reusing the existing function to make sure segment list is sane. I like code reuse part of it.

Also, segment loading has not taken place yet. They are loaded later in kimage_load_segment().

[..]

Quoted 9 lines

kernel/kexec.c static int __init crash_save_vmcoreinfo_init(void) · line 2121

Quoted 5 lines
kernel/kexec.c +5 -0
+	/* Walk the RAM ranges and allocate a suitable range for the buffer */
+	walk_system_ram_res(0, -1, kbuf, walk_ram_range_callback);
+
+	kbuf->image = NULL;
+	kfree(kbuf);

This is freed after kzalloc'ing it a bit earlier, why not make it a stack variable for simplicity? struct kexec_buf doesn't seem that large...

Ya, I was not sure whether to make it stack variable or allocate dynamically. It seems to be roughly 60bytes in size on 64bit. I guess I will convert it into a stack variable.

Thanks
Vivek

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

#12 lore raw

Re: [PATCH 06/11] kexec: A new system call, kexec_file_load, for in kernel kexec

Quoted 14 lines · On Mon, 24 Feb 2014 11:41:31 -0500 Vivek Goyal <[email protected]> wrote:

On Fri, Feb 21, 2014 at 03:59:10PM +0100, Borislav Petkov wrote:

[...]

Quoted 11 lines

...

Quoted 8 lines
include/uapi/linux/kexec.h +0 -0
index d6629d4..5fddb1b 100644
@@ -13,6 +13,10 @@
 #define KEXEC_PRESERVE_CONTEXT	0x00000002
 #define KEXEC_ARCH_MASK		0xffff0000
 
Quoted 7 lines
Quoted 5 lines

include/uapi/linux/kexec.h line 18

Quoted 3 lines
include/uapi/linux/kexec.h +3 -0
+/* Kexec file load interface flags */
+#define KEXEC_FILE_UNLOAD	0x00000001
+#define KEXEC_FILE_ON_CRASH	0x00000002

BIT()

What's that?

#define BIT(nr) (1UL << (nr))

For my part I'm not convinced it's a better way to do it, unless Borislav also wanted to suggest adding an enum for the bit number values...

Petr T

maintainer
#13 lore raw

Re: [PATCH 06/11] kexec: A new system call, kexec_file_load, for in kernel kexec

Quoted 5 lines · Petr Tesarik

#define BIT(nr) (1UL << (nr))

For my part I'm not convinced it's a better way to do it, unless Borislav also wanted to suggest adding an enum for the bit number values...

Well,

#define KEXEC_FILE_UNLOAD BIT(1)

is much more readable.

The enum thing is also nice to have but it's not my personal preference.

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
maintainer
#14 lore raw

Re: [PATCH 06/11] kexec: A new system call, kexec_file_load, for in kernel kexec

In reply to Vivek Goyal
Quoted 8 lines · Vivek Goyal

Right now there is only one user. But once other image loader support comes along or other arches support file based kexec, they can make use of same function.

This is a pretty important modification as it decides what's the starting point of next kernel image. I wanted to make it a function callable by users who wanted to modify it instead of of letting them directly modify image->start.

But are you expecting any other way to set image->start by the other arches/image loaders?

Quoted 7 lines

Could be. I am trying to reuse exisitng code. In current code, user space passes a list of segments and then kernel calls this function to make sure all the segments passed in make sense.

Here also arch dependent part of kernel is preparing a list of segments wanted and then generic code is reusing the existing function to make sure segment list is sane. I like code reuse part of it.

Right, ok, this was just an idea. Maybe not worth it.

Thanks.

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
#15 lore raw

Re: [PATCH 06/11] kexec: A new system call, kexec_file_load, for in kernel kexec

Quoted 5 lines · Borislav Petkov
Quoted 4 lines · Vivek Goyal

Right now there is only one user. But once other image loader support comes along or other arches support file based kexec, they can make use of same function.

Quoted 7 lines
Quoted 4 lines

This is a pretty important modification as it decides what's the starting point of next kernel image. I wanted to make it a function callable by users who wanted to modify it instead of of letting them directly modify image->start.

But are you expecting any other way to set image->start by the other arches/image loaders?

No. I am expecting other loaders to just call this function to set the start of kernel image.

Thanks
Vivek

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

#16 lore raw

PATCH v2 7/11 kexec: Create a relocatable object called purgatory

In reply to Vivek Goyal

Create a stand alone relocatable object purgatory which runs between two kernels. This name, concept and some code has been taken from kexec-tools. Idea is that this code runs after a crash and it runs in minimal environment. So keep it separate from rest of the kernel and in long term we will have to practically do no maintenance of this code.

This code also has the logic to do verify sha256 hashes of various segments which have been loaded into memory. So first we verify that the kernel we are jumping to is fine and has not been corrupted and make progress only if checsums are verified.

This code also takes care of copying some memory contents to backup region.

sha256 hash related code has been taken from crypto/sha256_generic.c. I could not call into functions exported by sha256_generic.c directly as we don't link against the kernel. Purgatory is a stand alone object.

Also sha256_generic.c is supposed to work with higher level crypto abstrations and API and there was no point in importing all that in purgatory. So instead of doing #include on sha256_generic.c I just copied relevant portions of code into arch/x86/purgatory/sha256.c. Now we shouldn't have to touch this code at all. Do let me know if there are better ways to handle it.

---
 arch/x86/Kbuild                    |   1 +
 arch/x86/Kconfig                   |   1 +
 arch/x86/Makefile                  |   6 +
 arch/x86/kernel/machine_kexec_64.c |  82 +++++++
 arch/x86/purgatory/Makefile        |  35 +++
 arch/x86/purgatory/entry64.S       | 111 +++++++++
 arch/x86/purgatory/purgatory.c     | 103 ++++++++
 arch/x86/purgatory/setup-x86_32.S  |  29 +++
 arch/x86/purgatory/setup-x86_64.S  |  68 ++++++
 arch/x86/purgatory/sha256.c        | 315 ++++++++++++++++++++++++
 arch/x86/purgatory/sha256.h        |  33 +++
 arch/x86/purgatory/stack.S         |  29 +++
 include/linux/kexec.h              |  31 +++
 kernel/kexec.c                     | 481 +++++++++++++++++++++++++++++++++++++
 14 files changed, 1325 insertions(+)
 create mode 100644 arch/x86/purgatory/Makefile
 create mode 100644 arch/x86/purgatory/entry64.S
 create mode 100644 arch/x86/purgatory/purgatory.c
 create mode 100644 arch/x86/purgatory/setup-x86_32.S
 create mode 100644 arch/x86/purgatory/setup-x86_64.S
 create mode 100644 arch/x86/purgatory/sha256.c
 create mode 100644 arch/x86/purgatory/sha256.h
 create mode 100644 arch/x86/purgatory/stack.S
arch/x86/Kbuild +1 -0
index e5287d8..faaeee7 100644
@@ -16,3 +16,4 @@ obj-$(CONFIG_IA32_EMULATION) += ia32/
 
 obj-y += platform/
 obj-y += net/
+obj-$(CONFIG_KEXEC) += purgatory/
arch/x86/Kconfig +1 -0
index aa5aeed..532cc0b 100644
@@ -1605,6 +1605,7 @@ source kernel/Kconfig.hz
 config KEXEC
 	bool "kexec system call"
 	select BUILD_BIN2C
+	select CRYPTO_SHA256
 	---help---
 	  kexec is a system call that implements the ability to shutdown your
 	  current kernel, and to start another kernel.  It is like a reboot
arch/x86/Makefile +6 -0
index 13b22e0..fedcd16 100644
@@ -160,6 +160,11 @@ archscripts: scripts_basic
 archheaders:
 	$(Q)$(MAKE) $(build)=arch/x86/syscalls all
 
+archprepare:
+ifeq ($(CONFIG_KEXEC),y)
+	$(Q)$(MAKE) $(build)=arch/x86/purgatory arch/x86/purgatory/kexec-purgatory.c
+endif
+
 ###
 # Kernel objects
 
@@ -223,6 +228,7 @@ archclean:
 	$(Q)rm -rf $(objtree)/arch/x86_64
 	$(Q)$(MAKE) $(clean)=$(boot)
 	$(Q)$(MAKE) $(clean)=arch/x86/tools
+	$(Q)$(MAKE) $(clean)=arch/x86/purgatory
 
 PHONY += kvmconfig
 kvmconfig:
arch/x86/kernel/machine_kexec_64.c +82 -0

85 lines not on this page · raw message

arch/x86/purgatory/Makefile new file +35 -0
new file mode 100644
index 0000000..c83c557
@@ -0,0 +1,35 @@
+ifeq ($(CONFIG_X86_64),y)
+	purgatory-y := purgatory.o entry64.o stack.o setup-x86_64.o sha256.o
+else
+	purgatory-y := purgatory.o stack.o sha256.o setup-x86_32.o
+endif
+
+targets += $(purgatory-y)
+PURGATORY_OBJS = $(addprefix $(obj)/,$(purgatory-y))
+
+LDFLAGS_purgatory.ro := -e purgatory_start -r --no-undefined -nostdlib -z nodefaultlib
+targets += purgatory.ro
+
+# Default KBUILD_CFLAGS can have -pg option set when FTRACE is enabled. That
+# in turn leaves some undefined symbols like __fentry__ in purgatory and not
+# sure how to relocate those. Like kexec-tools, custom flags.
+
+ifeq ($(CONFIG_X86_64),y)
+KBUILD_CFLAGS	:= -fno-strict-aliasing -Wall -Wstrict-prototypes -fno-zero-initialized-in-bss -mcmodel=large -Os -fno-builtin -ffreestanding -c -MD
+else
+KBUILD_CFLAGS	:= -fno-strict-aliasing -Wall -Wstrict-prototypes -fno-zero-initialized-in-bss -Os -fno-builtin -ffreestanding -c -MD -m32
+endif
+
+$(obj)/purgatory.ro: $(PURGATORY_OBJS) FORCE
+		$(call if_changed,ld)
+
+targets += kexec-purgatory.c
+
+quiet_cmd_bin2c = BIN2C   $@
+      cmd_bin2c = cat $(obj)/purgatory.ro | $(srctree)/scripts/basic/bin2c kexec_purgatory > $(obj)/kexec-purgatory.c
+
+$(obj)/kexec-purgatory.c: $(obj)/purgatory.ro FORCE
+	$(call if_changed,bin2c)
+
+
+obj-$(CONFIG_KEXEC)	+= kexec-purgatory.o
arch/x86/purgatory/entry64.S new file +111 -0

111 lines not on this page · raw message

arch/x86/purgatory/purgatory.c new file +103 -0

103 lines not on this page · raw message

arch/x86/purgatory/setup-x86_32.S new file +29 -0
new file mode 100644
index 0000000..a9d5aa5
@@ -0,0 +1,29 @@
+/*
+ * purgatory:  setup code
+ *
+ * Copyright (C) 2014 Red Hat Inc.
+ *
+ * This code has been taken from kexec-tools.
+ *
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+	.text
+	.globl purgatory_start
+	.balign 16
+purgatory_start:
+	.code32
+
+	/* This is just a stub. Write code when 32bit support comes along */
+	call purgatory
arch/x86/purgatory/setup-x86_64.S new file +68 -0

68 lines not on this page · raw message

arch/x86/purgatory/sha256.c new file +315 -0

315 lines not on this page · raw message

arch/x86/purgatory/sha256.h new file +33 -0
new file mode 100644
index 0000000..74df8f4
@@ -0,0 +1,33 @@
+/*
+ *  Copyright (C) 2014 Red Hat Inc.
+ *
+ *  Author: Vivek Goyal <[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; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  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.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#ifndef SHA256_H
+#define SHA256_H
+
+
+#include <linux/types.h>
+#include <crypto/sha.h>
+
+extern int sha256_init(struct sha256_state *sctx);
+extern int sha256_update(struct sha256_state *sctx, const u8 *input,
+				unsigned int length);
+extern int sha256_final(struct sha256_state *sctx, u8* hash);
+
+#endif /* SHA256_H */
arch/x86/purgatory/stack.S new file +29 -0
new file mode 100644
index 0000000..aff1fa9
@@ -0,0 +1,29 @@
+/*
+ * purgatory:  stack
+ *
+ * Copyright (C) 2014 Red Hat Inc.
+ *
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+	/* A stack for the loaded kernel.
+	 * Seperate and in the data section so it can be prepopulated.
+	 */
+	.data
+	.balign 4096
+	.globl stack, stack_end
+
+stack:
+	.skip 4096
+stack_end:
include/linux/kexec.h +31 -0
index 51b56cd..d391ed7 100644
@@ -10,6 +10,7 @@
 #include <linux/ioport.h>
 #include <linux/elfcore.h>
 #include <linux/elf.h>
+#include <linux/module.h>
 #include <asm/kexec.h>
 
 /* Verify architecture specific macros are defined */
@@ -95,6 +96,27 @@ struct compat_kexec_segment {
 };
 #endif
 
+struct kexec_sha_region {
+	unsigned long start;
+	unsigned long len;
+};
+
+struct purgatory_info {
+	/* Pointer to elf header of read only purgatory */
+	Elf_Ehdr *ehdr;
+
+	/* Pointer to purgatory sechdrs which are modifiable */
+	Elf_Shdr *sechdrs;
+	/*
+	 * Temporary buffer location where purgatory is loaded and relocated
+	 * This memory can be freed post image load
+	 */
+	void *purgatory_buf;
+
+	/* Address where purgatory is finally loaded and is executed from */
+	unsigned long purgatory_load_addr;
+};
+
 struct kimage {
 	kimage_entry_t head;
 	kimage_entry_t *entry;
@@ -143,6 +165,9 @@ struct kimage {
 
 	/* Image loader handling the kernel can store a pointer here */
 	void * image_loader_data;
+
+	/* Information for loading purgatory */
+	struct purgatory_info purgatory_info;
 };
 
 /*
@@ -197,6 +222,12 @@ extern asmlinkage long compat_sys_kexec_load(unsigned long entry,
 extern struct page *kimage_alloc_control_pages(struct kimage *image,
 						unsigned int order);
 extern void kimage_set_start_addr(struct kimage *image, unsigned long start);
+extern int kexec_load_purgatory(struct kimage *image, unsigned long min,
+		unsigned long max, int top_down, unsigned long *load_addr);
+extern int kexec_purgatory_get_set_symbol(struct kimage *image,
+		const char *name, void *buf, unsigned int size, bool get_value);
+extern void *kexec_purgatory_get_symbol_addr(struct kimage *image,
+					const char *name);
 
 extern void crash_kexec(struct pt_regs *);
 int kexec_should_crash(struct task_struct *);
kernel/kexec.c +481 -0

517 lines not on this page · raw message

Signature · 7 lines
1.8.4.2


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

Re: [PATCH 07/11] kexec: Create a relocatable object called purgatory

Quoted 18 lines · Vivek Goyal
arch/x86/purgatory/purgatory.c new file +18 -0
+
+/**
+ * memcpy - Copy one area of memory to another
+ * @dest: Where to copy to
+ * @src: Where to copy from
+ * @count: The size of the area.
+ */
+static void *memcpy(void *dest, const void *src, unsigned long count)
+{
+	char *tmp = dest;
+	const char *s = src;
+
+	while (count--)
+		*tmp++ = *s++;
+	return dest;
+}
+
+static int memcmp(const void *cs, const void *ct, size_t count)

arch/x86/purgatory/purgatory.c line 65

Quoted 10 lines
arch/x86/purgatory/purgatory.c new file +10 -0
+{
+	const unsigned char *su1, *su2;
+	int res = 0;
+
+	for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
+		if ((res = *su1 - *su2) != 0)
+			break;
+	return res;
+}
+

<bikeshed>

There multiple implementations of memcpy(), memcmp() and memset() in this patchset, and they make my eyes want to bleed (especially memcmp()). Can we centralize there, and perhaps even share code with the stuff in arch/x86/boot already?

</bikeshed>

-hpa

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

#18 lore raw

Re: [PATCH 07/11] kexec: Create a relocatable object called purgatory

Quoted 32 lines · H. Peter Anvin
Quoted 18 lines · Vivek Goyal
arch/x86/purgatory/purgatory.c new file +18 -0
+
+/**
+ * memcpy - Copy one area of memory to another
+ * @dest: Where to copy to
+ * @src: Where to copy from
+ * @count: The size of the area.
+ */
+static void *memcpy(void *dest, const void *src, unsigned long count)
+{
+	char *tmp = dest;
+	const char *s = src;
+
+	while (count--)
+		*tmp++ = *s++;
+	return dest;
+}
+
+static int memcmp(const void *cs, const void *ct, size_t count)

arch/x86/purgatory/purgatory.c line 65

Quoted 10 lines
arch/x86/purgatory/purgatory.c new file +10 -0
+{
+	const unsigned char *su1, *su2;
+	int res = 0;
+
+	for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
+		if ((res = *su1 - *su2) != 0)
+			break;
+	return res;
+}
+

<bikeshed>

Quoted 6 lines

There multiple implementations of memcpy(), memcmp() and memset() in this patchset, and they make my eyes want to bleed (especially memcmp()). Can we centralize there, and perhaps even share code with the stuff in arch/x86/boot already?

</bikeshed>

Hi hpa,

There is multiple implementation of memcpy() only (sha256.c and purgatory.c). I will merge the two and make them use single definition of memcpy().

I can't see multiple implementation of memcpy() and memcmp() in purgatory code.

W.r.t sharing the code with arch/x86/boot/, I am not sure how to do it.

I see two implementations of memcpy() under arch/x86/boot.

One is in copy.S. This is assembly code and looks like is supposed to run in 16bit mode. (code16).

Other one is in compressed/misc.c and there are two definitions, one for 32bit and one fore 64bit.

I am not sure why there is a need to write memcpy() in assembly when C will do just fine for my case. I don't have to write two versions of memcpy() and use it both for 32bit and 64bit.

So I can just make all the purgatory code share same version of memcpy(), memcmp() and memset(), is that fine. I have taken implementations of these functions from lib/string.c

Thanks
Vivek

Quoted 2 lines

-hpa

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

#19 lore raw

Re: [PATCH 07/11] kexec: Create a relocatable object called purgatory

Quoted 3 lines · Vivek Goyal

W.r.t sharing the code with arch/x86/boot/, I am not sure how to do it.

Pretty much we have been doing #includes (a bit sad, I know)... there are already a lot of them between arch/x86/boot, arch/x86/boot/compressed, and arch/x86/realmode. In that sense collecting these "limited environments" together and have these kinds of stuff together in one place seems like a good idea.

Does purgatory move large amounts of data around? If so, we probably *do* want to use rep movsl, but otherwise you're definitely right, using C code makes more sense.

Quoted 7 lines

I see two implementations of memcpy() under arch/x86/boot.

One is in copy.S. This is assembly code and looks like is supposed to run in 16bit mode. (code16).

Other one is in compressed/misc.c and there are two definitions, one for 32bit and one fore 64bit.

They are basically the 16-, 32-, and 64-bit variants of the same code.

Quoted 3 lines

I am not sure why there is a need to write memcpy() in assembly when C will do just fine for my case. I don't have to write two versions of memcpy() and use it both for 32bit and 64bit.

The point would be to use the ones we already have.

Quoted 3 lines

So I can just make all the purgatory code share same version of memcpy(), memcmp() and memset(), is that fine. I have taken implementations of these functions from lib/string.c

It depends on if you care about performance. For memcpy() and memset() in particular, the CPU has internally optimized versions of these that beats C at least on any newer silicon.

-hpa

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

#20 lore raw

Re: [PATCH 07/11] kexec: Create a relocatable object called purgatory

Quoted 11 lines · H. Peter Anvin
Quoted 3 lines · Vivek Goyal

W.r.t sharing the code with arch/x86/boot/, I am not sure how to do it.

Pretty much we have been doing #includes (a bit sad, I know)... there are already a lot of them between arch/x86/boot, arch/x86/boot/compressed, and arch/x86/realmode. In that sense collecting these "limited environments" together and have these kinds of stuff together in one place seems like a good idea.

Quoted 3 lines

Does purgatory move large amounts of data around? If so, we probably *do* want to use rep movsl, but otherwise you're definitely right, using C code makes more sense.

No, I don't move lots of data around in purgatory. We just copy backup region and that's 640KB of data.

So we don't copy a lot and it is not a performance sensitive path.

Hence I would like to keep it simple. That is have C version of memcpy() in purgatory() which works both for 32bit and 64bit and reuse that across purgatory code.

Please let me know if you don't like the idea and you still think there should be a shared implementation between arch/x86/boot/ and arch/x86/purgatory/.

Thanks
Vivek

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

#21 lore raw

Re: [PATCH 07/11] kexec: Create a relocatable object called purgatory

Quoted 5 lines · Vivek Goyal

Please let me know if you don't like the idea and you still think there should be a shared implementation between arch/x86/boot/ and arch/x86/purgatory/.

That is what I would *prefer*. There are some other string functions in arch/x86/boot/string.c which also ought to be sharable (and are in places already.)

-hpa

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

#22 lore raw

Re: [PATCH 07/11] kexec: Create a relocatable object called purgatory

Quoted 11 lines · H. Peter Anvin
Quoted 5 lines · Vivek Goyal

Please let me know if you don't like the idea and you still think there should be a shared implementation between arch/x86/boot/ and arch/x86/purgatory/.

That is what I would *prefer*. There are some other string functions in arch/x86/boot/string.c which also ought to be sharable (and are in places already.)

Ok, I will look into it. Possibly move optimized 32bit and 64bit version of memcpy in arch/x86/boot/string.c and include it in purgaotry files.

Or we could define new C version of routines in boot/string.c and leave optimized versions where they are.

Thanks
Vivek

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

maintainer
#23 lore raw

Re: [PATCH 07/11] kexec: Create a relocatable object called purgatory

In reply to Vivek Goyal
Quoted 2 lines · Vivek Goyal

Create a stand alone relocatable object purgatory which runs between two kernels. This name, concept and some code has been taken from kexec-tools.

... and the concept probably originates from Dante's "Divine Comedy" :-P

Quoted 15 lines

Idea is that this code runs after a crash and it runs in minimal environment. So keep it separate from rest of the kernel and in long term we will have to practically do no maintenance of this code.

This code also has the logic to do verify sha256 hashes of various segments which have been loaded into memory. So first we verify that the kernel we are jumping to is fine and has not been corrupted and make progress only if checsums are verified.

This code also takes care of copying some memory contents to backup region.

sha256 hash related code has been taken from crypto/sha256_generic.c. I could not call into functions exported by sha256_generic.c directly as we don't link against the kernel. Purgatory is a stand alone object.

Quoted 6 lines

Also sha256_generic.c is supposed to work with higher level crypto abstrations and API and there was no point in importing all that in purgatory. So instead of doing #include on sha256_generic.c I just copied relevant portions of code into arch/x86/purgatory/sha256.c. Now we shouldn't have to touch this code at all. Do let me know if there are better ways to handle it.

Ok, but what about configurable encryption algorithms? Maybe there are people who don't want to use sha-2 and prefer something else instead.

Quoted 1 line

Also checkpatch:

...
total: 429 errors, 5 warnings, 1409 lines checked

arch/x86/Makefile archscripts: scripts_basic · line 166

Quoted 12 lines
arch/x86/Makefile +4 -0
index 13b22e0..fedcd16 100644
@@ -160,6 +160,11 @@ archscripts: scripts_basic
 archheaders:
 	$(Q)$(MAKE) $(build)=arch/x86/syscalls all
 
+archprepare:
+ifeq ($(CONFIG_KEXEC),y)
+	$(Q)$(MAKE) $(build)=arch/x86/purgatory arch/x86/purgatory/kexec-purgatory.c
+endif

I wonder if this could be put into arch/x86/boot/ and built there too... But hpa said that already.

...

Quoted 16 lines
arch/x86/purgatory/entry64.S new file +10 -0
new file mode 100644
index 0000000..e405c0f
@@ -0,0 +1,111 @@
+/*
+ * Copyright (C) 2003,2004  Eric Biederman ([email protected])
+ * Copyright (C) 2014  Red Hat Inc.
+
+ * Author(s): Vivek Goyal <[email protected]>
+ *
+ * This code has been taken from kexec-tools.
+ *
+ * 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

arch/x86/purgatory/entry64.S line 20

Quoted 10 lines
arch/x86/purgatory/entry64.S new file +10 -0
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

Yeah, can we drop this boilerplate gunk and refer to COPYING instead.

Quoted 35 lines
arch/x86/purgatory/entry64.S new file +35 -0
+ */
+
+	.text
+	.balign 16
+	.code64
+	.globl entry64, entry64_regs
+
+
+entry64:
+	/* Setup a gdt that should be preserved */
+	lgdt gdt(%rip)
+
+	/* load the data segments */
+	movl    $0x18, %eax     /* data segment */
+	movl    %eax, %ds
+	movl    %eax, %es
+	movl    %eax, %ss
+	movl    %eax, %fs
+	movl    %eax, %gs
+
+	/* Setup new stack */
+	leaq    stack_init(%rip), %rsp
+	pushq   $0x10 /* CS */
+	leaq    new_cs_exit(%rip), %rax
+	pushq   %rax
+	lretq
+new_cs_exit:
+
+	/* Load the registers */
+	movq	rax(%rip), %rax
+	movq	rbx(%rip), %rbx
+	movq	rcx(%rip), %rcx
+	movq	rdx(%rip), %rdx
+	movq	rsi(%rip), %rsi
+	movq	rdi(%rip), %rdi

arch/x86/purgatory/entry64.S line 65

Quoted 10 lines
arch/x86/purgatory/entry64.S new file +10 -0
+	movq    rsp(%rip), %rsp
+	movq	rbp(%rip), %rbp
+	movq	r8(%rip), %r8
+	movq	r9(%rip), %r9
+	movq	r10(%rip), %r10
+	movq	r11(%rip), %r11
+	movq	r12(%rip), %r12
+	movq	r13(%rip), %r13
+	movq	r14(%rip), %r14
+	movq	r15(%rip), %r15

Huh, is the purpose to simply clear the arch registers here?

xor %reg,%reg

?

If so, you don't need the entry64_regs and below definitions at all.

Quoted 1 line

From the looks of it, though, something's populating those regs before

we jump to rip below...

Quoted 63 lines
arch/x86/purgatory/entry64.S new file +46 -0
+
+	/* Jump to the new code... */
+	jmpq	*rip(%rip)
+
+	.section ".rodata"
+	.balign 4
+entry64_regs:
+rax:	.quad 0x00000000
+rbx:	.quad 0x00000000
+rcx:	.quad 0x00000000
+rdx:	.quad 0x00000000
+rsi:	.quad 0x00000000
+rdi:	.quad 0x00000000
+rsp:	.quad 0x00000000
+rbp:	.quad 0x00000000
+r8:	.quad 0x00000000
+r9:	.quad 0x00000000
+r10:	.quad 0x00000000
+r11:	.quad 0x00000000
+r12:	.quad 0x00000000
+r13:	.quad 0x00000000
+r14:	.quad 0x00000000
+r15:	.quad 0x00000000
+rip:	.quad 0x00000000
+	.size entry64_regs, . - entry64_regs
+
+	/* GDT */
+	.section ".rodata"
+	.balign 16
+gdt:
+	/* 0x00 unusable segment
+	 * 0x08 unused
+	 * so use them as gdt ptr
+	 */
+	.word gdt_end - gdt - 1
+	.quad gdt
+	.word 0, 0, 0
+
+	/* 0x10 4GB flat code segment */
+	.word 0xFFFF, 0x0000, 0x9A00, 0x00AF
+
+	/* 0x18 4GB flat data segment */
+	.word 0xFFFF, 0x0000, 0x9200, 0x00CF
+gdt_end:
+stack:	.quad   0, 0
+stack_init:
arch/x86/purgatory/purgatory.c new file +11 -0
new file mode 100644
index 0000000..375cfb7
@@ -0,0 +1,103 @@
+/*
+ * purgatory: Runs between two kernels
+ *
+ * Copyright (C) 2013 Red Hat Inc.
+ *
+ * Author:
+ *
+ * Vivek Goyal <[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

arch/x86/purgatory/purgatory.c line 21

Quoted 10 lines
arch/x86/purgatory/purgatory.c new file +10 -0
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

Ditto for the boilerplate gunk.

Quoted 66 lines
arch/x86/purgatory/purgatory.c new file +66 -0
+ */
+
+#include "sha256.h"
+
+struct sha_region {
+	unsigned long start;
+	unsigned long len;
+};
+
+unsigned long backup_dest = 0;
+unsigned long backup_src = 0;
+unsigned long backup_sz = 0;
+
+u8 sha256_digest[SHA256_DIGEST_SIZE] = { 0 };
+
+struct sha_region sha_regions[16] = {};
+
+/**
+ * memcpy - Copy one area of memory to another
+ * @dest: Where to copy to
+ * @src: Where to copy from
+ * @count: The size of the area.
+ */
+static void *memcpy(void *dest, const void *src, unsigned long count)
+{
+	char *tmp = dest;
+	const char *s = src;
+
+	while (count--)
+		*tmp++ = *s++;
+	return dest;
+}
+
+static int memcmp(const void *cs, const void *ct, size_t count)
+{
+	const unsigned char *su1, *su2;
+	int res = 0;
+
+	for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
+		if ((res = *su1 - *su2) != 0)
+			break;
+	return res;
+}
+
+static int copy_backup_region(void)
+{
+	if (backup_dest)
+		memcpy((void *)backup_dest, (void *)backup_src, backup_sz);
+
+	return 0;
+}
+
+int verify_sha256_digest(void)
+{
+	struct sha_region *ptr, *end;
+	u8 digest[SHA256_DIGEST_SIZE];
+	struct sha256_state sctx;
+
+	sha256_init(&sctx);
+	end = &sha_regions[sizeof(sha_regions)/sizeof(sha_regions[0])];
+	for (ptr = sha_regions; ptr < end; ptr++)
+		sha256_update(&sctx, (uint8_t *)(ptr->start), ptr->len);
+
+	sha256_final(&sctx, digest);
+
+	if (memcmp(digest, sha256_digest, sizeof(digest)) != 0)

arch/x86/purgatory/purgatory.c line 97

Quoted 10 lines
arch/x86/purgatory/purgatory.c new file +10 -0
+		return 1;
+
+        return 0;
+}
+
+void purgatory(void)
+{
+	int ret;
+
+	ret = verify_sha256_digest();

Yeah, again, hardcoding sha256 is kinda jucky. We probably should link the needed crypto API stuff stuff and support multiple encryption algos.

arch/x86/purgatory/purgatory.c line 102

Quoted 5 lines
arch/x86/purgatory/purgatory.c new file +5 -0
+	if (ret) {
+		/* loop forever */
+		for(;;);
+	}
+	copy_backup_region();

What is this thing supposed to do? I see in patch 11/11 arch_update_purgatory() does some preparations for KEXEC_TYPE_CRASH.

Quoted 16 lines
arch/x86/purgatory/purgatory.c new file +1 -0
+}
arch/x86/purgatory/setup-x86_32.S new file +9 -0
new file mode 100644
index 0000000..a9d5aa5
@@ -0,0 +1,29 @@
+/*
+ * purgatory:  setup code
+ *
+ * Copyright (C) 2014 Red Hat Inc.
+ *
+ * This code has been taken from kexec-tools.
+ *
+ * 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

arch/x86/purgatory/setup-x86_32.S line 19

Quoted 10 lines
arch/x86/purgatory/setup-x86_32.S new file +10 -0
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

Ditto on the boilerplate.

arch/x86/purgatory/setup-x86_32.S line 28

Quoted 9 lines
arch/x86/purgatory/setup-x86_32.S new file +9 -0
+ */
+
+	.text
+	.globl purgatory_start
+	.balign 16
+purgatory_start:
+	.code32
+
+	/* This is just a stub. Write code when 32bit support comes along */

I'm guessing we want to support 32-bit secure boot with kexec at some point...

Quoted 17 lines
arch/x86/purgatory/setup-x86_32.S new file +1 -0
+	call purgatory
arch/x86/purgatory/setup-x86_64.S new file +10 -0
new file mode 100644
index 0000000..d23bc54
@@ -0,0 +1,68 @@
+/*
+ * purgatory:  setup code
+ *
+ * Copyright (C) 2003,2004  Eric Biederman ([email protected])
+ * Copyright (C) 2014 Red Hat Inc.
+ *
+ * This code has been taken from kexec-tools.
+ *
+ * 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

arch/x86/purgatory/setup-x86_64.S line 20

Quoted 10 lines
arch/x86/purgatory/setup-x86_64.S new file +10 -0
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

Boilerplate gunk.

...

Bah, that's a huge patch - it could use some splitting.

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
#24 lore raw

Re: [PATCH 07/11] kexec: Create a relocatable object called purgatory

On Wed, Feb 26, 2014 at 05:00:08PM +0100, Borislav Petkov wrote:

[..]

Quoted 9 lines
Quoted 6 lines

Also sha256_generic.c is supposed to work with higher level crypto abstrations and API and there was no point in importing all that in purgatory. So instead of doing #include on sha256_generic.c I just copied relevant portions of code into arch/x86/purgatory/sha256.c. Now we shouldn't have to touch this code at all. Do let me know if there are better ways to handle it.

Ok, but what about configurable encryption algorithms? Maybe there are people who don't want to use sha-2 and prefer something else instead.

We have been using sha256 for last 7-8 years in kexec-tools and nobody asked for changing hash algorithm so far.

So yes, somebody wanting to use a different algorithm is a possibility but I don't think it is likely in near future.

This patchset is already very big. I would rather make it work with sha256 and down the line one can make it more generic if they feel the need. This is not a user space API/ABI and one should be able to change it without breaking any user space applications.

So I really don't feel the need of making it more complicated, pull in all the crypto API in purgaotry to support other kind of hash algorithms in purgatory.

Quoted 7 lines
Quoted 2 lines

Also checkpatch:

...
total: 429 errors, 5 warnings, 1409 lines checked

Will run checkpatch.

Quoted 9 lines
Quoted 9 lines
arch/x86/Makefile +0 -0
index 13b22e0..fedcd16 100644
@@ -160,6 +160,11 @@ archscripts: scripts_basic
 archheaders:
 	$(Q)$(MAKE) $(build)=arch/x86/syscalls all
 
Quoted 7 lines

arch/x86/Makefile archscripts: scripts_basic · line 166

Quoted 4 lines
arch/x86/Makefile +4 -0
+archprepare:
+ifeq ($(CONFIG_KEXEC),y)
+	$(Q)$(MAKE) $(build)=arch/x86/purgatory arch/x86/purgatory/kexec-purgatory.c
+endif

I wonder if this could be put into arch/x86/boot/ and built there too... But hpa said that already.

I thought hpa wanted to shared memcpy(), memset() and memcmp() functions from arch/x86/. He did nto ask to move arch/x86/purgatory/ in arch/x86/boot.

I personally felt that arch/x86/boot/ has all the code to build kernel and purgaotry code does not deal with it. So I felt it is better to create arch/x86/purgatory.

hpa, what do you think. Is there any strong reason that purgatory/ dir
should be under arch/x86/boot/ and not under arch/x86/.

[..]

Quoted 5 lines

arch/x86/Makefile archscripts: scripts_basic · line 169

Quoted 3 lines
arch/x86/Makefile +3 -0
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

Yeah, can we drop this boilerplate gunk and refer to COPYING instead.

Ok. Will do.

[..]

Quoted 20 lines
Quoted 7 lines
arch/x86/Makefile +7 -0
+	/* Load the registers */
+	movq	rax(%rip), %rax
+	movq	rbx(%rip), %rbx
+	movq	rcx(%rip), %rcx
+	movq	rdx(%rip), %rdx
+	movq	rsi(%rip), %rsi
+	movq	rdi(%rip), %rdi

arch/x86/Makefile archscripts: scripts_basic

Quoted 10 lines
arch/x86/Makefile +10 -0
+	movq    rsp(%rip), %rsp
+	movq	rbp(%rip), %rbp
+	movq	r8(%rip), %r8
+	movq	r9(%rip), %r9
+	movq	r10(%rip), %r10
+	movq	r11(%rip), %r11
+	movq	r12(%rip), %r12
+	movq	r13(%rip), %r13
+	movq	r14(%rip), %r14
+	movq	r15(%rip), %r15

Huh, is the purpose to simply clear the arch registers here?

Quoted 1 line

xor %reg,%reg

No. One can modify purgatory object symbol entry64_regs dynamically from outside and purpose of this code is to load values into registers. At compile time value of entry64_regs is 0 so it kind of gives the impression that we are just trying to zero registers.

At kernel load time, we set values of some of those registers. Stack and kernel entry point is one of those. Look at arch/x86/kernel/kexec-bzimage.c

        regs64.rbx = 0; /* Bootstrap Processor */
        regs64.rsi = bootparam_load_addr;
        regs64.rip = kernel_load_addr + 0x200;
	regs64.rsp = (unsigned long)stack;
	ret = kexec_purgatory_get_set_symbol(image, "entry64_regs", &regs64,
                                        sizeof(regs64), 0);

[..]

Quoted 19 lines
Quoted 19 lines
arch/x86/Makefile +19 -0
+int verify_sha256_digest(void)
+{
+	struct sha_region *ptr, *end;
+	u8 digest[SHA256_DIGEST_SIZE];
+	struct sha256_state sctx;
+
+	sha256_init(&sctx);
+	end = &sha_regions[sizeof(sha_regions)/sizeof(sha_regions[0])];
+	for (ptr = sha_regions; ptr < end; ptr++)
+		sha256_update(&sctx, (uint8_t *)(ptr->start), ptr->len);
+
+	sha256_final(&sctx, digest);
+
+	if (memcmp(digest, sha256_digest, sizeof(digest)) != 0)
+		return 1;
+
+        return 0;
+}
+
Quoted 8 lines

arch/x86/Makefile

Quoted 5 lines
arch/x86/Makefile +5 -0
+void purgatory(void)
+{
+	int ret;
+
+	ret = verify_sha256_digest();

Yeah, again, hardcoding sha256 is kinda jucky. We probably should link the needed crypto API stuff stuff and support multiple encryption algos.

There is no easy way to link to kernel crypto API as purgatory object does not link against kernel. Most likely it will have to be either a common library against which both kernel and purgatory link or use all the #include magic. I really don't want to make things complicated here.

The purpose of this isolated code in a directory is that write it once and almost never touch it again.

Quoted 9 lines

arch/x86/Makefile

Quoted 6 lines
arch/x86/Makefile +5 -0
+	if (ret) {
+		/* loop forever */
+		for(;;);
+	}
+	copy_backup_region();

What is this thing supposed to do? I see in patch 11/11 arch_update_purgatory() does some preparations for KEXEC_TYPE_CRASH.

Kdump kernel runs from reserved region of memory. But we also found on x86, that it still needed first 640KB of memory to boot. So before jumping to second kernel, we copy first 640KB in reserved region and let second kernel use first 640KB. We call this concept as backup region as we are backing up an area of memory so that second kernel does not overwrite it.

For more details on backup region have a look at this paper.

http://lse.sourceforge.net/kdump/documentation/ols2oo5-kdump-paper.pdf

[..]

Quoted 4 lines
Quoted 4 lines

+ */

arch/x86/Makefile +3 -0
+
+	.text
+	.globl purgatory_start
Quoted 8 lines

arch/x86/Makefile

Quoted 5 lines
arch/x86/Makefile +5 -0
+	.balign 16
+purgatory_start:
+	.code32
+
+	/* This is just a stub. Write code when 32bit support comes along */

I'm guessing we want to support 32-bit secure boot with kexec at some point...

Yep. Right now these patches support 64bit kernel only and I put some code for 32bit so that there are no compilation failures.

Though 32bit is becoming less relevant with every passing day, still supporting it on 32bit is a good idea. It can happen down the line tough.

[..]

Quoted 1 line

Bah, that's a huge patch - it could use some splitting.

I will see if I can find a way to split this patch. Thanks for reviewing it.

Thanks
Vivek

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

maintainer
#25 lore raw

Re: [PATCH 07/11] kexec: Create a relocatable object called purgatory

Quoted 6 lines · Vivek Goyal

We have been using sha256 for last 7-8 years in kexec-tools and nobody asked for changing hash algorithm so far.

So yes, somebody wanting to use a different algorithm is a possibility but I don't think it is likely in near future.

Quoted 8 lines

This patchset is already very big. I would rather make it work with sha256 and down the line one can make it more generic if they feel the need. This is not a user space API/ABI and one should be able to change it without breaking any user space applications.

So I really don't feel the need of making it more complicated, pull in all the crypto API in purgaotry to support other kind of hash algorithms in purgatory.

Ok, makes sense. Right, if someone needs it, someone could add that support fairly easily.

Quoted 9 lines

No. One can modify purgatory object symbol entry64_regs dynamically from outside and purpose of this code is to load values into registers. At compile time value of entry64_regs is 0 so it kind of gives the impression that we are just trying to zero registers.

At kernel load time, we set values of some of those registers. Stack and kernel entry point is one of those. Look at arch/x86/kernel/kexec-bzimage.c

Quoted 6 lines
        regs64.rbx = 0; /* Bootstrap Processor */
        regs64.rsi = bootparam_load_addr;
        regs64.rip = kernel_load_addr + 0x200;
	regs64.rsp = (unsigned long)stack;
	ret = kexec_purgatory_get_set_symbol(image, "entry64_regs", &regs64,
                                        sizeof(regs64), 0);

Ok, thanks for explaining.

Quoted 9 lines

Kdump kernel runs from reserved region of memory. But we also found on x86, that it still needed first 640KB of memory to boot. So before jumping to second kernel, we copy first 640KB in reserved region and let second kernel use first 640KB. We call this concept as backup region as we are backing up an area of memory so that second kernel does not overwrite it.

For more details on backup region have a look at this paper.

http://lse.sourceforge.net/kdump/documentation/ols2oo5-kdump-paper.pdf

That's valuable info, can we put some of it in a comment in the code somewhere?

Quoted 5 lines

Yep. Right now these patches support 64bit kernel only and I put some code for 32bit so that there are no compilation failures.

Though 32bit is becoming less relevant with every passing day, still supporting it on 32bit is a good idea. It can happen down the line tough.

Ok.

Thanks.

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
#26 lore raw

PATCH v2 8/11 kexec-bzImage: Support for loading bzImage using 64bit entry

In reply to Vivek Goyal

This is loader specific code which can load bzImage and set it up for 64bit entry. This does not take care of 32bit entry or real mode entry yet.

---
 arch/x86/include/asm/kexec-bzimage.h |  11 ++
 arch/x86/include/asm/kexec.h         |  30 +++++
 arch/x86/kernel/Makefile             |   2 +
 arch/x86/kernel/kexec-bzimage.c      | 234 +++++++++++++++++++++++++++++++++++
 arch/x86/kernel/machine_kexec.c      | 136 ++++++++++++++++++++
 arch/x86/kernel/machine_kexec_64.c   |   3 +-
 6 files changed, 415 insertions(+), 1 deletion(-)
 create mode 100644 arch/x86/include/asm/kexec-bzimage.h
 create mode 100644 arch/x86/kernel/kexec-bzimage.c
 create mode 100644 arch/x86/kernel/machine_kexec.c
arch/x86/include/asm/kexec-bzimage.h new file +11 -0
new file mode 100644
index 0000000..9e83961
@@ -0,0 +1,11 @@
+#ifndef _ASM_BZIMAGE_H
+#define _ASM_BZIMAGE_H
+
+extern int bzImage64_probe(const char *buf, unsigned long len);
+extern void *bzImage64_load(struct kimage *image, char *kernel,
+		unsigned long kernel_len, char *initrd,
+		unsigned long initrd_len, char *cmdline,
+		unsigned long cmdline_len);
+extern int bzImage64_cleanup(struct kimage *image);
+
+#endif  /* _ASM_BZIMAGE_H */
arch/x86/include/asm/kexec.h +30 -0
index 17483a4..9bd6fec 100644
@@ -23,6 +23,7 @@
 
 #include <asm/page.h>
 #include <asm/ptrace.h>
+#include <asm/bootparam.h>
 
 /*
  * KEXEC_SOURCE_MEMORY_LIMIT maximum page get_free_page can return.
@@ -161,11 +162,40 @@ struct kimage_arch {
 	pmd_t *pmd;
 	pte_t *pte;
 };
+
+struct kexec_entry64_regs {
+	uint64_t rax;
+	uint64_t rbx;
+	uint64_t rcx;
+	uint64_t rdx;
+	uint64_t rsi;
+	uint64_t rdi;
+	uint64_t rsp;
+	uint64_t rbp;
+	uint64_t r8;
+	uint64_t r9;
+	uint64_t r10;
+	uint64_t r11;
+	uint64_t r12;
+	uint64_t r13;
+	uint64_t r14;
+	uint64_t r15;
+	uint64_t rip;
+};
 #endif
 
 typedef void crash_vmclear_fn(void);
 extern crash_vmclear_fn __rcu *crash_vmclear_loaded_vmcss;
 
+extern int kexec_setup_initrd(struct boot_params *boot_params,
+		unsigned long initrd_load_addr, unsigned long initrd_len);
+extern int kexec_setup_cmdline(struct boot_params *boot_params,
+		unsigned long bootparams_load_addr,
+		unsigned long cmdline_offset, char *cmdline,
+		unsigned long cmdline_len);
+extern int kexec_setup_boot_parameters(struct boot_params *params);
+
+
 #endif /* __ASSEMBLY__ */
 
 #endif /* _ASM_X86_KEXEC_H */
arch/x86/kernel/Makefile +2 -0
index cb648c8..fa9981d 100644
@@ -67,8 +67,10 @@ obj-$(CONFIG_DYNAMIC_FTRACE)	+= ftrace.o
 obj-$(CONFIG_FUNCTION_GRAPH_TRACER) += ftrace.o
 obj-$(CONFIG_FTRACE_SYSCALLS)	+= ftrace.o
 obj-$(CONFIG_X86_TSC)		+= trace_clock.o
+obj-$(CONFIG_KEXEC)		+= machine_kexec.o
 obj-$(CONFIG_KEXEC)		+= machine_kexec_$(BITS).o
 obj-$(CONFIG_KEXEC)		+= relocate_kernel_$(BITS).o crash.o
+obj-$(CONFIG_KEXEC)		+= kexec-bzimage.o
 obj-$(CONFIG_CRASH_DUMP)	+= crash_dump_$(BITS).o
 obj-y				+= kprobes/
 obj-$(CONFIG_MODULES)		+= module.o
arch/x86/kernel/kexec-bzimage.c new file +234 -0

234 lines not on this page · raw message

arch/x86/kernel/machine_kexec.c new file +136 -0

136 lines not on this page · raw message

arch/x86/kernel/machine_kexec_64.c +2 -1
index 8866c5e..37df7d3 100644
@@ -21,10 +21,11 @@
 #include <asm/tlbflush.h>
 #include <asm/mmu_context.h>
 #include <asm/debugreg.h>
+#include <asm/kexec-bzimage.h>
 
 /* arch dependent functionality related to kexec file based syscall */
 static struct kexec_file_type kexec_file_type[]={
-	{"", NULL, NULL, NULL},
+	{"bzImage64", bzImage64_probe, bzImage64_load, bzImage64_cleanup},
 };
 
 static int nr_file_types = sizeof(kexec_file_type)/sizeof(kexec_file_type[0]);
Signature · 7 lines
1.8.4.2


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

Re: [PATCH 08/11] kexec-bzImage: Support for loading bzImage using 64bit entry

Quoted 3 lines · Vivek Goyal

This is loader specific code which can load bzImage and set it up for 64bit entry. This does not take care of 32bit entry or real mode entry yet.

Is there any use in that? Real mode entry especially is more than a bit scary when coming from another kernel already...

-hpa

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

#28 lore raw

Re: [PATCH 08/11] kexec-bzImage: Support for loading bzImage using 64bit entry

Quoted 7 lines · H. Peter Anvin
Quoted 3 lines · Vivek Goyal

This is loader specific code which can load bzImage and set it up for 64bit entry. This does not take care of 32bit entry or real mode entry yet.

Is there any use in that? Real mode entry especially is more than a bit scary when coming from another kernel already...

I think 64bit entry should be good for x86_64. When we start supporting this new system call on x86(32bit), then we can implement 32bit entry support (which should not be hard).

I have no plans to implement 16bit entry support. I just wanted to mention explicitly what is currently supported.

Thanks
Vivek

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

maintainer
#29 lore raw

Re: [PATCH 08/11] kexec-bzImage: Support for loading bzImage using 64bit entry

In reply to Vivek Goyal
Quoted 6 lines · Vivek Goyal

This is loader specific code which can load bzImage and set it up for 64bit entry. This does not take care of 32bit entry or real mode entry yet.

---

checkpatch: total: 4 errors, 2 warnings, 450 lines checked

...

arch/x86/kernel/Makefile obj-$(CONFIG_DYNAMIC_FTRACE) += ftrace.o · line 73

Quoted 12 lines
arch/x86/kernel/Makefile +2 -0
index cb648c8..fa9981d 100644
@@ -67,8 +67,10 @@ obj-$(CONFIG_DYNAMIC_FTRACE)	+= ftrace.o
 obj-$(CONFIG_FUNCTION_GRAPH_TRACER) += ftrace.o
 obj-$(CONFIG_FTRACE_SYSCALLS)	+= ftrace.o
 obj-$(CONFIG_X86_TSC)		+= trace_clock.o
+obj-$(CONFIG_KEXEC)		+= machine_kexec.o
 obj-$(CONFIG_KEXEC)		+= machine_kexec_$(BITS).o
 obj-$(CONFIG_KEXEC)		+= relocate_kernel_$(BITS).o crash.o
+obj-$(CONFIG_KEXEC)		+= kexec-bzimage.o

Maybe use less obj-$(CONFIG_KEXEC) lines here.

Quoted 19 lines
arch/x86/kernel/Makefile +0 -0
 obj-$(CONFIG_CRASH_DUMP)	+= crash_dump_$(BITS).o
 obj-y				+= kprobes/
 obj-$(CONFIG_MODULES)		+= module.o
arch/x86/kernel/kexec-bzimage.c new file +10 -0
new file mode 100644
index 0000000..cbfcd00
@@ -0,0 +1,234 @@
+#include <linux/string.h>
+#include <linux/printk.h>
+#include <linux/errno.h>
+#include <linux/slab.h>
+#include <linux/kexec.h>
+#include <linux/kernel.h>
+#include <linux/mm.h>
+
+#include <asm/bootparam.h>
+#include <asm/setup.h>

arch/x86/kernel/kexec-bzimage.c line 20

Quoted 10 lines
arch/x86/kernel/kexec-bzimage.c new file +10 -0
+
+#ifdef CONFIG_X86_64
+
+struct bzimage64_data {
+	/*
+	 * Temporary buffer to hold bootparams buffer. This should be
+	 * freed once the bootparam segment has been loaded.
+	 */
+	void *bootparams_buf;
+};

Why a struct if it is going to have only one member?

arch/x86/kernel/kexec-bzimage.c line 27

Quoted 7 lines
arch/x86/kernel/kexec-bzimage.c new file +7 -0
+
+int bzImage64_probe(const char *buf, unsigned long len)
+{
+	int ret = -ENOEXEC;
+	struct setup_header *header;
+
+	if (len < 2 * 512) {

What's 2*512? Two sectors?

arch/x86/kernel/kexec-bzimage.c line 32

Quoted 5 lines
arch/x86/kernel/kexec-bzimage.c new file +5 -0
+		pr_debug("File is too short to be a bzImage\n");
+		return ret;
+	}
+
+	header = (struct setup_header *)(buf + 0x1F1);

0x1F1 should need at least a comment or "offsetof(struct boot_params, hdr)" or both, which would be best. :-)

arch/x86/kernel/kexec-bzimage.c line 34

Quoted 2 lines
arch/x86/kernel/kexec-bzimage.c new file +2 -0
+	if (memcmp((char *)&header->header, "HdrS", 4) != 0) {
+		pr_debug("Not a bzImage\n");

Actually, I think that means that there is no real mode kernel header there, or we're using an old boot protocol version:

Documentation/x86/boot.txt

arch/x86/kernel/kexec-bzimage.c line 39

Quoted 5 lines
arch/x86/kernel/kexec-bzimage.c new file +5 -0
+		return ret;
+	}
+
+	if (header->boot_flag != 0xAA55) {
+                /* No x86 boot sector present */

Comment is kinda redundant here :)

arch/x86/kernel/kexec-bzimage.c line 45

Quoted 6 lines
arch/x86/kernel/kexec-bzimage.c new file +6 -0
+		pr_debug("No x86 boot sector present\n");
+		return ret;
+	}
+
+	if (header->version < 0x020C) {
+                /* Must be at least protocol version 2.12 */

Ditto.

arch/x86/kernel/kexec-bzimage.c line 50

Quoted 5 lines
arch/x86/kernel/kexec-bzimage.c new file +5 -0
+		pr_debug("Must be at least protocol version 2.12\n");
+		return ret;
+	}
+
+	if ((header->loadflags & 1) == 0) {

That must be LOADED_HIGH bit. Why does this bit mean it is a bzImage?

Ok, I see it in boot.txt:

"...

	When loading a zImage kernel ((loadflags & 0x01) == 0).

"

arch/x86/kernel/kexec-bzimage.c line 57

Quoted 7 lines
arch/x86/kernel/kexec-bzimage.c new file +7 -0
+		/* Not a bzImage */
+		pr_debug("zImage not a bzImage\n");
+		return ret;
+	}
+
+	if ((header->xloadflags & 3) != 3) {
+	/* XLF_KERNEL_64 and XLF_CAN_BE_LOADED_ABOVE_4G should be set */

Use those defines in the code please instead of naked numbers.

Quoted 20 lines
arch/x86/kernel/kexec-bzimage.c new file +20 -0
+		pr_debug("Not a relocatable bzImage64\n");
+		return ret;
+	}
+
+        /* I've got a bzImage */
+	pr_debug("It's a relocatable bzImage64\n");
+	ret = 0;
+
+	return ret;
+}
+
+void *bzImage64_load(struct kimage *image, char *kernel,
+		unsigned long kernel_len,
+		char *initrd, unsigned long initrd_len,
+		char *cmdline, unsigned long cmdline_len)
+{
+
+	struct setup_header *header;
+	int setup_sects, kern16_size, ret = 0;
+	unsigned long setup_header_size, params_cmdline_sz;

arch/x86/kernel/kexec-bzimage.c line 87

Quoted 10 lines
arch/x86/kernel/kexec-bzimage.c new file +10 -0
+	struct boot_params *params;
+	unsigned long bootparam_load_addr, kernel_load_addr, initrd_load_addr;
+	unsigned long purgatory_load_addr;
+	unsigned long kernel_bufsz, kernel_memsz, kernel_align;
+	char *kernel_buf;
+	struct bzimage64_data *ldata;
+	struct kexec_entry64_regs regs64;
+	void *stack;
+
+	header = (struct setup_header *)(kernel + 0x1F1);

See above.

Quoted 15 lines
arch/x86/kernel/kexec-bzimage.c new file +15 -0
+	setup_sects = header->setup_sects;
+	if (setup_sects == 0)
+		setup_sects = 4;
+
+	kern16_size = (setup_sects + 1) * 512;
+	if (kernel_len < kern16_size) {
+		pr_debug("bzImage truncated\n");
+		return ERR_PTR(-ENOEXEC);
+	}
+
+	if (cmdline_len > header->cmdline_size) {
+		pr_debug("Kernel command line too long\n");
+		return ERR_PTR(-EINVAL);
+	}
+

arch/x86/kernel/kexec-bzimage.c line 112

Quoted 10 lines
arch/x86/kernel/kexec-bzimage.c new file +10 -0
+	/* Allocate loader specific data */
+	ldata = kzalloc(sizeof(struct bzimage64_data), GFP_KERNEL);
+	if (!ldata)
+		return ERR_PTR(-ENOMEM);
+
+	/*
+	 * Load purgatory. For 64bit entry point, purgatory  code can be
+	 * anywhere.
+	 */
+	ret = kexec_load_purgatory(image, 0x3000, -1, 1, &purgatory_load_addr);

Some defines like MIN_<something> and MAX_<something> could be more readable here.

Quoted 7 lines
arch/x86/kernel/kexec-bzimage.c new file +7 -0
+	if (ret) {
+		pr_debug("Loading purgatory failed\n");
+		goto out_free_loader_data;
+	}
+
+	pr_debug("Loaded purgatory at 0x%lx\n", purgatory_load_addr);
+

arch/x86/kernel/kexec-bzimage.c line 129

Quoted 10 lines
arch/x86/kernel/kexec-bzimage.c new file +10 -0
+	/* Load Bootparams and cmdline */
+	params_cmdline_sz = sizeof(struct boot_params) + cmdline_len;
+	params = kzalloc(params_cmdline_sz, GFP_KERNEL);
+	if (!params) {
+		ret = -ENOMEM;
+		goto out_free_loader_data;
+	}
+
+	/* Copy setup header onto bootparams. */
+	setup_header_size = 0x0202 + kernel[0x0201] - 0x1F1;

More magic numbers :-\ Ok, I'm not going to comment on the rest of them below but you get the idea - it would be much better to have descriptive defines here instead of naked numbers.

arch/x86/kernel/kexec-bzimage.c line 135

Quoted 6 lines
arch/x86/kernel/kexec-bzimage.c new file +6 -0
+
+	/* Is there a limit on setup header size? */
+	memcpy(&params->hdr, (kernel + 0x1F1), setup_header_size);
+	ret = kexec_add_buffer(image, (char *)params, params_cmdline_sz,
+			params_cmdline_sz, 16, 0x3000, -1, 1,
+			&bootparam_load_addr);

Normally we do arg alignment below the opening brace of the function. Ditto for a bunch of call sites below.

...

Quoted 14 lines
arch/x86/kernel/machine_kexec.c new file +8 -0
new file mode 100644
index 0000000..ac55890
@@ -0,0 +1,136 @@
+/*
+ * handle transition of Linux booting another kernel
+ *
+ * Copyright (C) 2014 Red Hat Inc.
+ * Authors:
+ * 	Vivek Goyal <[email protected]>
+ *
+ * This source code is licensed under the GNU General Public License,

arch/x86/kernel/machine_kexec.c line 18

Quoted 10 lines
arch/x86/kernel/machine_kexec.c new file +10 -0
+ * Version 2.  See the file COPYING for more details.
+ */
+
+#include <linux/kernel.h>
+#include <linux/string.h>
+#include <asm/bootparam.h>
+#include <asm/setup.h>
+
+/*
+ * Common code for x86 and x86_64 used for kexec.

I think you mean i386 by x86, right?

Quoted 38 lines
arch/x86/kernel/machine_kexec.c new file +38 -0
+ *
+ * For the time being it compiles only for x86_64 as there are no image
+ * loaders implemented * for x86. This #ifdef can be removed once somebody
+ * decides to write an image loader on CONFIG_X86_32.
+ */
+
+#ifdef CONFIG_X86_64
+
+int kexec_setup_initrd(struct boot_params *boot_params,
+		unsigned long initrd_load_addr, unsigned long initrd_len)
+{
+	boot_params->hdr.ramdisk_image = initrd_load_addr & 0xffffffffUL;
+	boot_params->hdr.ramdisk_size = initrd_len & 0xffffffffUL;
+
+	boot_params->ext_ramdisk_image = initrd_load_addr >> 32;
+	boot_params->ext_ramdisk_size = initrd_len >> 32;
+
+	return 0;
+}
+
+int kexec_setup_cmdline(struct boot_params *boot_params,
+		unsigned long bootparams_load_addr,
+		unsigned long cmdline_offset, char *cmdline,
+		unsigned long cmdline_len)
+{
+	char *cmdline_ptr = ((char *)boot_params) + cmdline_offset;
+	unsigned long cmdline_ptr_phys;
+	uint32_t cmdline_low_32, cmdline_ext_32;
+
+	memcpy(cmdline_ptr, cmdline, cmdline_len);
+	cmdline_ptr[cmdline_len - 1] = '\0';
+
+	cmdline_ptr_phys = bootparams_load_addr + cmdline_offset;
+	cmdline_low_32 = cmdline_ptr_phys & 0xffffffffUL;
+	cmdline_ext_32 = cmdline_ptr_phys >> 32;
+
+	boot_params->hdr.cmd_line_ptr = cmdline_low_32;
+	if (cmdline_ext_32)

arch/x86/kernel/machine_kexec.c line 66

Quoted 10 lines
arch/x86/kernel/machine_kexec.c new file +10 -0
+		boot_params->ext_cmd_line_ptr = cmdline_ext_32;
+
+	return 0;
+}
+
+static int setup_memory_map_entries(struct boot_params *params)
+{
+	unsigned int nr_e820_entries;
+
+	/* TODO: What about EFI */

Do you mean by that what do_add_efi_memmap() does? We add the efi entries only when add_efi_memmap is supplied on the cmdline, see 200001eb140ea.

arch/x86/kernel/machine_kexec.c line 76

Quoted 10 lines
arch/x86/kernel/machine_kexec.c new file +10 -0
+	nr_e820_entries = e820_saved.nr_map;
+	if (nr_e820_entries > E820MAX)
+		nr_e820_entries = E820MAX;
+
+	params->e820_entries = nr_e820_entries;
+	memcpy(&params->e820_map, &e820_saved.map,
+			nr_e820_entries * sizeof(struct e820entry));
+
+	return 0;
+}

Thanks.

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
#30 lore raw

Re: [PATCH 08/11] kexec-bzImage: Support for loading bzImage using 64bit entry

On Thu, Feb 27, 2014 at 10:36:29PM +0100, Borislav Petkov wrote:

Hi Boris,

Thanks for taking time to review this large patchset. Please find my comments inline.

[..]

Quoted 6 lines
Quoted 6 lines
arch/x86/kernel/Makefile +0 -0
index cb648c8..fa9981d 100644
@@ -67,8 +67,10 @@ obj-$(CONFIG_DYNAMIC_FTRACE)	+= ftrace.o
 obj-$(CONFIG_FUNCTION_GRAPH_TRACER) += ftrace.o
Quoted 8 lines

arch/x86/kernel/Makefile obj-$(CONFIG_DYNAMIC_FTRACE) += ftrace.o · line 73

Quoted 6 lines
arch/x86/kernel/Makefile +2 -0
 obj-$(CONFIG_FTRACE_SYSCALLS)	+= ftrace.o
 obj-$(CONFIG_X86_TSC)		+= trace_clock.o
+obj-$(CONFIG_KEXEC)		+= machine_kexec.o
 obj-$(CONFIG_KEXEC)		+= machine_kexec_$(BITS).o
 obj-$(CONFIG_KEXEC)		+= relocate_kernel_$(BITS).o crash.o
+obj-$(CONFIG_KEXEC)		+= kexec-bzimage.o

Maybe use less obj-$(CONFIG_KEXEC) lines here.

Ok, Will put some of them in single line.

[..]

Quoted 9 lines

arch/x86/kernel/Makefile obj-$(CONFIG_DYNAMIC_FTRACE) += ftrace.o

Quoted 7 lines
arch/x86/kernel/Makefile +7 -0
+struct bzimage64_data {
+	/*
+	 * Temporary buffer to hold bootparams buffer. This should be
+	 * freed once the bootparam segment has been loaded.
+	 */
+	void *bootparams_buf;
+};

Why a struct if it is going to have only one member?

Well, I had started with a generic idea of bootloader being able to store some data in image and retrieve it later. Finally it turned out to be only one field in current implementation.

But I still like it as it allows storing more data down the line. There is no other place where we can store bootloader specific data. So this is the mechanism I created.

Quoted 10 lines

arch/x86/kernel/Makefile

Quoted 8 lines
arch/x86/kernel/Makefile +7 -0
+
+int bzImage64_probe(const char *buf, unsigned long len)
+{
+	int ret = -ENOEXEC;
+	struct setup_header *header;
+
+	if (len < 2 * 512) {

What's 2*512? Two sectors?

Yep, two sectors.

Quoted 9 lines

arch/x86/kernel/Makefile

Quoted 6 lines
arch/x86/kernel/Makefile +5 -0
+		pr_debug("File is too short to be a bzImage\n");
+		return ret;
+	}
+
+	header = (struct setup_header *)(buf + 0x1F1);

0x1F1 should need at least a comment or "offsetof(struct boot_params, hdr)" or both, which would be best. :-)

Ok, I will put a comment.

Quoted 6 lines

arch/x86/kernel/Makefile

Quoted 3 lines
arch/x86/kernel/Makefile +2 -0
+	if (memcmp((char *)&header->header, "HdrS", 4) != 0) {
+		pr_debug("Not a bzImage\n");

Actually, I think that means that there is no real mode kernel header there, or we're using an old boot protocol version:

So Documentation/x86/boot.txt says following.

If the "HdrS" (0x53726448) magic number is not found at offset 0x202, the boot protocol version is "old". Loading an old kernel, the following parameters should be assumed:

Image type = zImage
initrd not supported
Real-mode kernel must be located at 0x90000.

So if it is old version then it is zImage (and not bzImage). So pr_debug() message seems to be correct that image one is trying to load is not bzImage and this loader will not handle this image.

Quoted 10 lines

Documentation/x86/boot.txt

arch/x86/kernel/Makefile

Quoted 5 lines
arch/x86/kernel/Makefile +5 -0
+		return ret;
+	}
+
+	if (header->boot_flag != 0xAA55) {
+                /* No x86 boot sector present */

Comment is kinda redundant here :)

Ok, will get rid of it.

Quoted 9 lines

arch/x86/kernel/Makefile

Quoted 7 lines
arch/x86/kernel/Makefile +6 -0
+		pr_debug("No x86 boot sector present\n");
+		return ret;
+	}
+
+	if (header->version < 0x020C) {
+                /* Must be at least protocol version 2.12 */

Ditto.

Will get rid of it.

Quoted 8 lines

arch/x86/kernel/Makefile

Quoted 6 lines
arch/x86/kernel/Makefile +5 -0
+		pr_debug("Must be at least protocol version 2.12\n");
+		return ret;
+	}
+
+	if ((header->loadflags & 1) == 0) {

That must be LOADED_HIGH bit. Why does this bit mean it is a bzImage?

Yep, that's LOADED_HIGH check. I think if LOADED_HIGH is not set, then that means protected mode code needs to be loaded at 0x10000 and I think that also means that it is zImage and not bzImage.

Atleast this loader does not handle anything which is that old where protected mode code needs to be loaded at 0x10000.

Quoted 7 lines

Ok, I see it in boot.txt:

"...

	When loading a zImage kernel ((loadflags & 0x01) == 0).

"

Quoted 9 lines

arch/x86/kernel/Makefile

Quoted 7 lines
arch/x86/kernel/Makefile +7 -0
+		/* Not a bzImage */
+		pr_debug("zImage not a bzImage\n");
+		return ret;
+	}
+
+	if ((header->xloadflags & 3) != 3) {
+	/* XLF_KERNEL_64 and XLF_CAN_BE_LOADED_ABOVE_4G should be set */

Use those defines in the code please instead of naked numbers.

Agreed. Using these defines is more readable. Will change it.

[..]

Quoted 13 lines
Quoted 13 lines
arch/x86/kernel/Makefile +13 -0
+void *bzImage64_load(struct kimage *image, char *kernel,
+		unsigned long kernel_len,
+		char *initrd, unsigned long initrd_len,
+		char *cmdline, unsigned long cmdline_len)
+{
+
+	struct setup_header *header;
+	int setup_sects, kern16_size, ret = 0;
+	unsigned long setup_header_size, params_cmdline_sz;
+	struct boot_params *params;
+	unsigned long bootparam_load_addr, kernel_load_addr, initrd_load_addr;
+	unsigned long purgatory_load_addr;
+	unsigned long kernel_bufsz, kernel_memsz, kernel_align;
Quoted 8 lines

arch/x86/kernel/Makefile

Quoted 6 lines
arch/x86/kernel/Makefile +6 -0
+	char *kernel_buf;
+	struct bzimage64_data *ldata;
+	struct kexec_entry64_regs regs64;
+	void *stack;
+
+	header = (struct setup_header *)(kernel + 0x1F1);

See above.

Will comment or use offsetof().

[..]

Quoted 5 lines
Quoted 5 lines
arch/x86/kernel/Makefile +5 -0
+	/* Allocate loader specific data */
+	ldata = kzalloc(sizeof(struct bzimage64_data), GFP_KERNEL);
+	if (!ldata)
+		return ERR_PTR(-ENOMEM);
+
Quoted 8 lines

arch/x86/kernel/Makefile

Quoted 5 lines
arch/x86/kernel/Makefile +5 -0
+	/*
+	 * Load purgatory. For 64bit entry point, purgatory  code can be
+	 * anywhere.
+	 */
+	ret = kexec_load_purgatory(image, 0x3000, -1, 1, &purgatory_load_addr);

Some defines like MIN_<something> and MAX_<something> could be more readable here.

Ok, atleast will use MAX_<something> for "-1". I might define one internally to this file for representing minimum address of 0x3000.

Quoted 14 lines
Quoted 14 lines
arch/x86/kernel/Makefile +13 -0
+	if (ret) {
+		pr_debug("Loading purgatory failed\n");
+		goto out_free_loader_data;
+	}
+
+	pr_debug("Loaded purgatory at 0x%lx\n", purgatory_load_addr);
+
+	/* Load Bootparams and cmdline */
+	params_cmdline_sz = sizeof(struct boot_params) + cmdline_len;
+	params = kzalloc(params_cmdline_sz, GFP_KERNEL);
+	if (!params) {
+		ret = -ENOMEM;
+		goto out_free_loader_data;
Quoted 8 lines

arch/x86/kernel/Makefile

Quoted 4 lines
arch/x86/kernel/Makefile +4 -0
+	}
+
+	/* Copy setup header onto bootparams. */
+	setup_header_size = 0x0202 + kernel[0x0201] - 0x1F1;

More magic numbers :-\ Ok, I'm not going to comment on the rest of them below but you get the idea - it would be much better to have descriptive defines here instead of naked numbers.

That's how boot.txt defines it. Look at 64-bit BOOT PROTOCOL.

0x0202 + byte value at offset 0x0201

Now one can argue that create some new defines to represent those magic numbers and include that file in kexec-bzimage loader. I will see what can I do.

Quoted 10 lines

arch/x86/kernel/Makefile

Quoted 7 lines
arch/x86/kernel/Makefile +6 -0
+
+	/* Is there a limit on setup header size? */
+	memcpy(&params->hdr, (kernel + 0x1F1), setup_header_size);
+	ret = kexec_add_buffer(image, (char *)params, params_cmdline_sz,
+			params_cmdline_sz, 16, 0x3000, -1, 1,
+			&bootparam_load_addr);

Normally we do arg alignment below the opening brace of the function. Ditto for a bunch of call sites below.

Hmm.., I have seen all kinds of alignments. But anyway, if you prefer that, I will change atleast some of them.

[..]

Quoted 9 lines

arch/x86/kernel/Makefile

Quoted 7 lines
arch/x86/kernel/Makefile +7 -0
+#include <linux/kernel.h>
+#include <linux/string.h>
+#include <asm/bootparam.h>
+#include <asm/setup.h>
+
+/*
+ * Common code for x86 and x86_64 used for kexec.

I think you mean i386 by x86, right?

Yes.

[..]

Quoted 9 lines

arch/x86/kernel/Makefile

Quoted 5 lines
arch/x86/kernel/Makefile +5 -0
+static int setup_memory_map_entries(struct boot_params *params)
+{
+	unsigned int nr_e820_entries;
+
+	/* TODO: What about EFI */

Do you mean by that what do_add_efi_memmap() does? We add the efi entries only when add_efi_memmap is supplied on the cmdline, see 200001eb140ea.

I meant in general I have not done any EFI handling. I am not even sure what needs to be done for EFI. I am just going through memory map and filling up E820 map in bootparams so that second kernel knows what are the memory areas available.

At some point of time we need to start passing EFI memory map to second kernel. (All the new code you and dave young have added to make kexec work on EFI systems).

I wanted to that in a separate patch. This patch series is alredy very big. bzImage signing and verification also I am planning to post in a separate series.

Thanks
Vivek

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

maintainer
#31 lore raw

Re: [PATCH 08/11] kexec-bzImage: Support for loading bzImage using 64bit entry

Quoted 1 line · Vivek Goyal

Thanks for taking time to review this large patchset.

Sure, np. Thanks for your patience in explaining stuff which is unclear to me.

Quoted 10 lines
Quoted 9 lines

arch/x86/kernel/kexec-bzimage.c line 20

Quoted 7 lines
arch/x86/kernel/kexec-bzimage.c new file +7 -0
+struct bzimage64_data {
+	/*
+	 * Temporary buffer to hold bootparams buffer. This should be
+	 * freed once the bootparam segment has been loaded.
+	 */
+	void *bootparams_buf;
+};

Why a struct if it is going to have only one member?

Quoted 7 lines

Well, I had started with a generic idea of bootloader being able to store some data in image and retrieve it later. Finally it turned out to be only one field in current implementation.

But I still like it as it allows storing more data down the line. There is no other place where we can store bootloader specific data. So this is the mechanism I created.

Right, but people would ask so you probably should hold down this intention in a comment somewhere.

Quoted 3 lines
Quoted 1 line

What's 2*512? Two sectors?

Yep, two sectors.

Also a comment or a nicely-named define then.

Quoted 7 lines

So Documentation/x86/boot.txt says following.

If the "HdrS" (0x53726448) magic number is not found at offset 0x202, the boot protocol version is "old". Loading an old kernel, the following parameters should be assumed:

Quoted 7 lines

Image type = zImage
initrd not supported
Real-mode kernel must be located at 0x90000.

So if it is old version then it is zImage (and not bzImage). So pr_debug() message seems to be correct that image one is trying to load is not bzImage and this loader will not handle this image.

Ok, old == zImage, got it.

Quoted 3 lines

Yep, that's LOADED_HIGH check. I think if LOADED_HIGH is not set, then that means protected mode code needs to be loaded at 0x10000 and I think that also means that it is zImage and not bzImage.

Yes, hpa confirmed this in IRC just now.

Quoted 7 lines

That's how boot.txt defines it. Look at 64-bit BOOT PROTOCOL.

0x0202 + byte value at offset 0x0201

Now one can argue that create some new defines to represent those magic numbers and include that file in kexec-bzimage loader. I will see what can I do.

Yeah, I was simply commenting it with the innocent onlooker hat on. FWIW, referring to boot.txt in a comment should be helpful enough for people who don't have an idea where to look, methinks.

Quoted 2 lines

Hmm.., I have seen all kinds of alignments. But anyway, if you prefer that, I will change atleast some of them.

It is most readable this way, but this is only my opinion so consider it only as a suggestion.

Quoted 8 lines

I meant in general I have not done any EFI handling. I am not even sure what needs to be done for EFI. I am just going through memory map and filling up E820 map in bootparams so that second kernel knows what are the memory areas available.

At some point of time we need to start passing EFI memory map to second kernel. (All the new code you and dave young have added to make kexec work on EFI systems).

Right, the efi runtime pieces are already in sysfs but you'd need to put them somewhere for the second kernel to find out. Maybe make it part of the bzImage or supply it separately...

Quoted 3 lines

I wanted to that in a separate patch. This patch series is alredy very big. bzImage signing and verification also I am planning to post in a separate series.

Right.

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
#32 lore raw

Re: [PATCH 08/11] kexec-bzImage: Support for loading bzImage using 64bit entry

Quoted 9 lines · Borislav Petkov
Quoted 9 lines

If the "HdrS" (0x53726448) magic number is not found at offset 0x202, the boot protocol version is "old". Loading an old kernel, the following parameters should be assumed:

Image type = zImage
initrd not supported
Real-mode kernel must be located at 0x90000.

Quoted 6 lines
Quoted 3 lines

So if it is old version then it is zImage (and not bzImage). So pr_debug() message seems to be correct that image one is trying to load is not bzImage and this loader will not handle this image.

Ok, old == zImage, got it.

No, old ⊂ zImage, but you can have a zImage with a newer protocol version, too. There are no load flags in the old protocol.

These days the old protocol is interesting mostly because of non-Linux images using the same boot protocol.

-hpa

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

#33 lore raw

Re: [PATCH 08/11] kexec-bzImage: Support for loading bzImage using 64bit entry

In reply to Borislav Petkov

On Wed, Mar 05, 2014 at 05:37:22PM +0100, Borislav Petkov wrote:

[..]

Quoted 14 lines
Quoted 14 lines
Quoted 9 lines

arch/x86/kernel/kexec-bzimage.c line 20

Quoted 7 lines
arch/x86/kernel/kexec-bzimage.c new file +7 -0
+struct bzimage64_data {
+	/*
+	 * Temporary buffer to hold bootparams buffer. This should be
+	 * freed once the bootparam segment has been loaded.
+	 */
+	void *bootparams_buf;
+};

Why a struct if it is going to have only one member?

Well, I had started with a generic idea of bootloader being able to store some data in image and retrieve it later. Finally it turned out to be only one field in current implementation.

Quoted 6 lines
Quoted 3 lines

But I still like it as it allows storing more data down the line. There is no other place where we can store bootloader specific data. So this is the mechanism I created.

Right, but people would ask so you probably should hold down this intention in a comment somewhere.

Ok, I will put a comment.

[..]

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

What's 2*512? Two sectors?

Yep, two sectors.

Also a comment or a nicely-named define then.

Ok, will do.

[..]

Quoted 11 lines
Quoted 7 lines

That's how boot.txt defines it. Look at 64-bit BOOT PROTOCOL.

0x0202 + byte value at offset 0x0201

Now one can argue that create some new defines to represent those magic numbers and include that file in kexec-bzimage loader. I will see what can I do.

Yeah, I was simply commenting it with the innocent onlooker hat on. FWIW, referring to boot.txt in a comment should be helpful enough for people who don't have an idea where to look, methinks.

Ok, will put a reference to boot.txt.

[..]

Quoted 8 lines
Quoted 4 lines

At some point of time we need to start passing EFI memory map to second kernel. (All the new code you and dave young have added to make kexec work on EFI systems).

Right, the efi runtime pieces are already in sysfs but you'd need to put them somewhere for the second kernel to find out. Maybe make it part of the bzImage or supply it separately...

I thought Dave passed relevant information on bootparams and some in cmdline (I have not looked at details). Once the EFI support is in, kernel implementaion should do the same thing as kexec-tools is doing to pass EFI mapping information to second kernel.

Thanks
Vivek

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

maintainer
#34 lore raw

Re: [PATCH 08/11] kexec-bzImage: Support for loading bzImage using 64bit entry

Quoted 4 lines · Vivek Goyal

I thought Dave passed relevant information on bootparams and some in cmdline (I have not looked at details). Once the EFI support is in, kernel implementaion should do the same thing as kexec-tools is doing to pass EFI mapping information to second kernel.

Right, he reads the runtime map from sysfs:

Documentation/ABI/testing/sysfs-firmware-efi-runtime-map

How exactly he prepares it with kexec-tools I don't remember anymore - I just skimmed over that part at the time.

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
#35 lore raw

PATCH v2 9/11 kexec: Provide a function to add a segment at fixed address

In reply to Vivek Goyal

kexec_add_buffer() can find a suitable range of memory for user buffer and add it to list of segments. But ELF loader will require that a buffer can be loaded at the address it has been compiled for (ET_EXEC type executables). So we need a helper function which can see if requested memory is valid and available and add a segment accordiingly. This patch provides that helper function. It will be used by elf loader in later patch.

---
 include/linux/kexec.h |  3 +++
 kernel/kexec.c        | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 68 insertions(+)
include/linux/kexec.h +3 -0
index d391ed7..2fb052c 100644
@@ -208,6 +208,9 @@ extern asmlinkage long sys_kexec_load(unsigned long entry,
 					struct kexec_segment __user *segments,
 					unsigned long flags);
 extern int kernel_kexec(void);
+extern int kexec_add_segment(struct kimage *image, char *buffer,
+			unsigned long bufsz, unsigned long memsz,
+			unsigned long base);
 extern int kexec_add_buffer(struct kimage *image, char *buffer,
 			unsigned long bufsz, unsigned long memsz,
 			unsigned long buf_align, unsigned long buf_min,
kernel/kexec.c +65 -0

71 lines not on this page · raw message

Signature · 7 lines
1.8.4.2


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

Re: [PATCH 09/11] kexec: Provide a function to add a segment at fixed address

Quoted 49 lines · Vivek Goyal

kexec_add_buffer() can find a suitable range of memory for user buffer and add it to list of segments. But ELF loader will require that a buffer can be loaded at the address it has been compiled for (ET_EXEC type executables). So we need a helper function which can see if requested memory is valid and available and add a segment accordiingly. This patch provides that helper function. It will be used by elf loader in later patch.

---
 include/linux/kexec.h |  3 +++
 kernel/kexec.c        | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 68 insertions(+)
include/linux/kexec.h +3 -0
index d391ed7..2fb052c 100644
@@ -208,6 +208,9 @@ extern asmlinkage long sys_kexec_load(unsigned long entry,
 					struct kexec_segment __user *segments,
 					unsigned long flags);
 extern int kernel_kexec(void);
+extern int kexec_add_segment(struct kimage *image, char *buffer,
+			unsigned long bufsz, unsigned long memsz,
+			unsigned long base);
 extern int kexec_add_buffer(struct kimage *image, char *buffer,
 			unsigned long bufsz, unsigned long memsz,
 			unsigned long buf_align, unsigned long buf_min,
kernel/kexec.c +14 -0
index 20169a4..9e4718b 100644
@@ -2002,6 +2002,71 @@ static int __kexec_add_segment(struct kimage *image, char *buf,
 	return 0;
 }
 
+static int validate_ram_range_callback(u64 start, u64 end, void *arg)
+{
+	struct kexec_segment *ksegment = arg;
+	u64 mstart = ksegment->mem;
+	u64 mend = ksegment->mem + ksegment->memsz - 1;
+
+	/* Found a valid range. Stop going through more ranges */
+	if (mstart >= start && mend <= end)
+		return 1;
+
+	/* Range did not match. Go to next one */
+	return 0;
+}
+

kernel/kexec.c static int __kexec_add_segment(struct kimage *image, char *buf, · line 2028

Quoted 10 lines
kernel/kexec.c +10 -0
+/* Add a kexec segment at fixed address provided by caller */
+int kexec_add_segment(struct kimage *image, char *buffer, unsigned long bufsz,
+			unsigned long memsz, unsigned long base)
+{
+	struct kexec_segment ksegment;
+	int ret;
+
+	/* Currently adding segment this way is allowed only in file mode */
+	if (!image->file_mode)
+		return -EINVAL;

Why the guard? On a quick scan, I don't see this function called by something else except on the kexec_file_load path...

kernel/kexec.c static int __kexec_add_segment(struct kimage *image, char *buf, · line 2040

Quoted 12 lines
kernel/kexec.c +12 -0
+
+	if (image->nr_segments >= KEXEC_SEGMENT_MAX)
+		return -EINVAL;
+
+	/*
+	 * Make sure we are not trying to add segment after allocating
+	 * control pages. All segments need to be placed first before
+	 * any control pages are allocated. As control page allocation
+	 * logic goes through list of segments to make sure there are
+	 * no destination overlaps.
+	 */
+	WARN_ONCE(!list_empty(&image->control_pages), "Adding kexec segment"

Maybe say at which address here:

... "Adding a kexec segment at address 0x%lx.."

for a bit more helpful info.

kernel/kexec.c static int __kexec_add_segment(struct kimage *image, char *buf, · line 2049

Quoted 9 lines
kernel/kexec.c +9 -0
+			" after allocating control pages\n");
+
+	if (bufsz > memsz)
+		return -EINVAL;
+	if (memsz == 0)
+		return -EINVAL;
+
+	/* Align memsz to next page boundary */
+	memsz = ALIGN(memsz, PAGE_SIZE);

We even have PAGE_ALIGN for that.

kernel/kexec.c static int __kexec_add_segment(struct kimage *image, char *buf, · line 2052

Quoted 3 lines
kernel/kexec.c +3 -0
+
+	/* Make sure base is atleast page size aligned */
+	if (base & (PAGE_SIZE - 1))

PAGE_ALIGNED even :)

kernel/kexec.c static int __kexec_add_segment(struct kimage *image, char *buf, · line 2064

Quoted 12 lines
kernel/kexec.c +12 -0
+		return -EINVAL;
+
+	memset(&ksegment, 0, sizeof(struct kexec_segment));
+	ksegment.mem = base;
+	ksegment.memsz = memsz;
+
+	/* Validate memory range */
+	ret = walk_system_ram_res(base, base + memsz - 1, &ksegment,
+				validate_ram_range_callback);
+
+	/* If a valid range is found, 1 is returned */
+	if (ret != 1)

That's the retval of validate_ram_range_callback, right? So

	if (!ret)

And shouldn't the convention be the opposite? 0 on success, !0 on error?

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
#37 lore raw

Re: [PATCH 09/11] kexec: Provide a function to add a segment at fixed address

On Thu, Feb 27, 2014 at 10:52:32PM +0100, Borislav Petkov wrote:

[..]

Quoted 5 lines
Quoted 5 lines
kernel/kexec.c +5 -0
+/* Add a kexec segment at fixed address provided by caller */
+int kexec_add_segment(struct kimage *image, char *buffer, unsigned long bufsz,
+			unsigned long memsz, unsigned long base)
+{
+	struct kexec_segment ksegment;
Quoted 8 lines

kernel/kexec.c static int __kexec_add_segment(struct kimage *image, char *buf, · line 2028

Quoted 5 lines
kernel/kexec.c +5 -0
+	int ret;
+
+	/* Currently adding segment this way is allowed only in file mode */
+	if (!image->file_mode)
+		return -EINVAL;

Why the guard? On a quick scan, I don't see this function called by something else except on the kexec_file_load path...

This is more of future proofing it. I have been putting this check to catch any accidental errors if somebody ends up calling this function from old mode.

But I am not very particular about it. If you don't like it, I can get rid of it.

Quoted 16 lines

kernel/kexec.c static int __kexec_add_segment(struct kimage *image, char *buf, · line 2040

Quoted 13 lines
kernel/kexec.c +12 -0
+
+	if (image->nr_segments >= KEXEC_SEGMENT_MAX)
+		return -EINVAL;
+
+	/*
+	 * Make sure we are not trying to add segment after allocating
+	 * control pages. All segments need to be placed first before
+	 * any control pages are allocated. As control page allocation
+	 * logic goes through list of segments to make sure there are
+	 * no destination overlaps.
+	 */
+	WARN_ONCE(!list_empty(&image->control_pages), "Adding kexec segment"

Maybe say at which address here:

Quoted 3 lines

... "Adding a kexec segment at address 0x%lx.."

for a bit more helpful info.

I think address does not matter here. You can't add a segemnt after you have allocated a control page. So I am not sure how printing address will help.

Quoted 12 lines

kernel/kexec.c static int __kexec_add_segment(struct kimage *image, char *buf, · line 2049

Quoted 10 lines
kernel/kexec.c +9 -0
+			" after allocating control pages\n");
+
+	if (bufsz > memsz)
+		return -EINVAL;
+	if (memsz == 0)
+		return -EINVAL;
+
+	/* Align memsz to next page boundary */
+	memsz = ALIGN(memsz, PAGE_SIZE);

We even have PAGE_ALIGN for that.

Ok, there is not much difference between two, but I can use PAGE_ALIGN().

Quoted 6 lines

kernel/kexec.c static int __kexec_add_segment(struct kimage *image, char *buf, · line 2052

Quoted 4 lines
kernel/kexec.c +3 -0
+
+	/* Make sure base is atleast page size aligned */
+	if (base & (PAGE_SIZE - 1))

PAGE_ALIGNED even :)

Will use it. :-)

Quoted 16 lines

kernel/kexec.c static int __kexec_add_segment(struct kimage *image, char *buf, · line 2064

Quoted 13 lines
kernel/kexec.c +12 -0
+		return -EINVAL;
+
+	memset(&ksegment, 0, sizeof(struct kexec_segment));
+	ksegment.mem = base;
+	ksegment.memsz = memsz;
+
+	/* Validate memory range */
+	ret = walk_system_ram_res(base, base + memsz - 1, &ksegment,
+				validate_ram_range_callback);
+
+	/* If a valid range is found, 1 is returned */
+	if (ret != 1)

That's the retval of validate_ram_range_callback, right? So

Quoted 3 lines
	if (!ret)

And shouldn't the convention be the opposite? 0 on success, !0 on error?

Ok, this one is little twisted.

walk_system_ram_res() stops calling callback function if callback function returned non zero code.

So in this case, once we have found the range to be valid, we don't want to continue to loop and look at any more ranges. So we return "1". If we return "0" for success, outer loop of walk_system_ram_res() will continue with next ranges.

Given the fact that "0" is interpreted as success by walk_system_ram_res() and it continues with next set of ranges, I could not use 0 as final measure of success. Negative returns are errors. So I thought of using postive values of ret to indicate whether range was found or not.

If there are better ways to handle it, I am all for it.

Thanks
Vivek

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

maintainer
#38 lore raw

Re: [PATCH 09/11] kexec: Provide a function to add a segment at fixed address

Quoted 6 lines · Vivek Goyal

This is more of future proofing it. I have been putting this check to catch any accidental errors if somebody ends up calling this function from old mode.

But I am not very particular about it. If you don't like it, I can get rid of it.

Yeah, it doesn't hurt to be overly cautious - I guess it can be removed later when this code settles.

Quoted 3 lines

I think address does not matter here. You can't add a segemnt after you have allocated a control page. So I am not sure how printing address will help.

Ok, so what's the urgency of that warning? The "can't add a segment" thing sounds kinda final to me and that everything breaks if we do add a segment after all, so maybe it should error out with -EINVAL and caller should stop adding segments if we have allocated the control page..?

IOW, how is that error message supposed to help me when I see it as a user?

Quoted 1 line

Ok, there is not much difference between two, but I can use PAGE_ALIGN().

Yeah, they're the same thing but the name PAGE_ALIGN is more descriptive :-)

Quoted 8 lines
Quoted 5 lines

That's the retval of validate_ram_range_callback, right? So

	if (!ret)

And shouldn't the convention be the opposite? 0 on success, !0 on error?

Ok, this one is little twisted.

Quoted 7 lines

walk_system_ram_res() stops calling callback function if callback function returned non zero code.

So in this case, once we have found the range to be valid, we don't want to continue to loop and look at any more ranges. So we return "1". If we return "0" for success, outer loop of walk_system_ram_res() will continue with next ranges.

Huh, I was only talking about flipping that logic, in walk_system_ram_res():

	ret = (*func)(res.start, res.end, arg);
	if (!res)
		break;

This way you still can return negative values as errors.

Quoted 3 lines

Given the fact that "0" is interpreted as success by walk_system_ram_res() and it continues with next set of ranges, I could not use 0 as final measure of success. Negative returns are errors. So I thought of using

And?

When the loop finishes, you will have the last negative error in ret...

Besides, in load_crashdump_segments() you have:

        ret = walk_system_ram_res(KEXEC_BACKUP_SRC_START, KEXEC_BACKUP_SRC_END,
                                image, determine_backup_region);

        /* Zero or postive return values are ok */
        if (ret < 0)
                return ret;

So 0 is ok, as you say.

Also:

        /* Validate memory range */
        ret = walk_system_ram_res(base, base + memsz - 1, &ksegment,
                                validate_ram_range_callback);

        /* If a valid range is found, 1 is returned */
        if (ret != 1)
                return -EINVAL;

Now this looks a bit fragile - only 1 is ok? Normally we do it like this:

	if (ret)
		return ret;

	return __kexec_add_segment(...)

and this way you can propagate the error value up without rewriting it here.

Am I missing something here?

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
#39 lore raw

Re: [PATCH 09/11] kexec: Provide a function to add a segment at fixed address

On Mon, Mar 10, 2014 at 11:01:43AM +0100, Borislav Petkov wrote:

[..]

Quoted 11 lines
Quoted 3 lines

I think address does not matter here. You can't add a segemnt after you have allocated a control page. So I am not sure how printing address will help.

Ok, so what's the urgency of that warning? The "can't add a segment" thing sounds kinda final to me and that everything breaks if we do add a segment after all, so maybe it should error out with -EINVAL and caller should stop adding segments if we have allocated the control page..?

IOW, how is that error message supposed to help me when I see it as a user?

Actually this message is for kernel developer. It is an error for somebody who has written an kernel image loader.

I think returning -EINVAL is also a good idea. Though it will travel all the way back to user and user again can't do anything about it.

I think printing a warning backtrace is still a good idea so that we know which loader is doing wrong thing and issue can be reported on lkml.

[..]

Quoted 16 lines
Quoted 8 lines
Quoted 5 lines

That's the retval of validate_ram_range_callback, right? So

	if (!ret)

And shouldn't the convention be the opposite? 0 on success, !0 on error?

Ok, this one is little twisted.

Quoted 7 lines

walk_system_ram_res() stops calling callback function if callback function returned non zero code.

So in this case, once we have found the range to be valid, we don't want to continue to loop and look at any more ranges. So we return "1". If we return "0" for success, outer loop of walk_system_ram_res() will continue with next ranges.

Quoted 4 lines

Huh, I was only talking about flipping that logic, in walk_system_ram_res():

	ret = (*func)(res.start, res.end, arg);
	if (!res)
	    ^^^

You mean (!ret) ?

Quoted 3 lines
		break;

This way you still can return negative values as errors.

If I flip the logic in walk_system_ram_res() will outer loop not stop after one round.

walk_system_ram_res() {
	while ((find_next_system_ram(&res, "System RAM") >= 0) {
		ret = (*func)(res.start, res.end, arg);
		if (!ret)
			break;
	}
}

Or are you suggesting that called function should return 0 to break out of loop otherwise return negative values to continue the loop? But that's counter intutive. If "func" returns error, then we should break out of while() loop and not call "func" again with any more qualifiying ranges.

Quoted 8 lines
Quoted 4 lines

Given the fact that "0" is interpreted as success by walk_system_ram_res() and it continues with next set of ranges, I could not use 0 as final measure of success. Negative returns are errors. So I thought of using

And?

When the loop finishes, you will have the last negative error in ret...

Yep, Any non-zero value (negative or positive) will be in "ret" when loop finishes and will be returned to caller. If final return value is negative caller knows an error occurred. If final return value is positive, caller knows that *callback* function succeeded.

Quoted 11 lines

Besides, in load_crashdump_segments() you have:

        ret = walk_system_ram_res(KEXEC_BACKUP_SRC_START, KEXEC_BACKUP_SRC_END,
                                image, determine_backup_region);

        /* Zero or postive return values are ok */
        if (ret < 0)
                return ret;

So 0 is ok, as you say.

I kept 0 as ok because I am not enforcing that one found a memory range with-in 0-640K.

So if return value is 1, we know that we found a RAM memory resource in the range of 0-640K. If return value is 0, we did not find a RAM memory resource in the range of 0-640K. I thought may be there are systems which don't have memory in that range and system can still boot. And that's why I said "0" is ok.

I don't want to enforce that one *has* to have a memory range with-in first 640K otherwise kexec will fail. If there is none, that means there is no backup region.

Quoted 11 lines

Also:

        /* Validate memory range */
        ret = walk_system_ram_res(base, base + memsz - 1, &ksegment,
                                validate_ram_range_callback);

        /* If a valid range is found, 1 is returned */
        if (ret != 1)
                return -EINVAL;
Quoted 1 line

Now this looks a bit fragile - only 1 is ok? Normally we do it like this:

Only "1" is ok here because I want to make sure that memory range I am looking for is present and valid. If not, we can't go ahead and it is an error. (This is different from backup region case).

I can't use 0 as measure of success because walk_system_ram_res() can return 0 even called function did not succeed. For example, assume somebody calls walk_system_ram_res() with start=0, end=-1 so that one walks through all the memory ranges available in the system. Now callback function probably is loking for a certain range say val1-val2. If it finds that range in all the ranges, will be return 1, otherwise continue to return 0 till we have gone through all the ranges.

So in above case, if 0 is returned, that does not mean callback function found what it was looking for.

Now one can argue that why are you going through all the memory ranges. Just search for very narrow area to begin with. Say start=0xval1 and end=0xval2 and now after first range callback function should be able to determine whether it found the value or not.

But this now becomes dependent on what start and end values were passed to walk_system_ram_res().

So I am not taking return "0" as measure of success of callback function. It could just mean that we went through all the overlapping ranges which matched search criterion and still callback function did not find what it was looking for.

Thanks
Vivek

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

#40 lore raw

PATCH v2 10/11 kexec: Support for loading ELF x86_64 images

In reply to Vivek Goyal

This patch provides support for kexec for loading ELF x86_64 images. I have tested it with loading vmlinux and it worked.

---
 arch/x86/include/asm/kexec-elf.h   |  11 ++
 arch/x86/kernel/Makefile           |   1 +
 arch/x86/kernel/kexec-elf.c        | 231 +++++++++++++++++++++++++++++++++++++
 arch/x86/kernel/machine_kexec_64.c |   2 +
 4 files changed, 245 insertions(+)
 create mode 100644 arch/x86/include/asm/kexec-elf.h
 create mode 100644 arch/x86/kernel/kexec-elf.c
arch/x86/include/asm/kexec-elf.h new file +11 -0
new file mode 100644
index 0000000..afef382
@@ -0,0 +1,11 @@
+#ifndef _ASM_KEXEC_ELF_H
+#define _ASM_KEXEC_ELF_H
+
+extern int elf_x86_64_probe(const char *buf, unsigned long len);
+extern void *elf_x86_64_load(struct kimage *image, char *kernel,
+		unsigned long kernel_len, char *initrd,
+		unsigned long initrd_len, char *cmdline,
+		unsigned long cmdline_len);
+extern int elf_x86_64_cleanup(struct kimage *image);
+
+#endif  /* _ASM_KEXEC_ELF_H */
arch/x86/kernel/Makefile +1 -0
index fa9981d..2d77de7 100644
@@ -71,6 +71,7 @@ obj-$(CONFIG_KEXEC)		+= machine_kexec.o
 obj-$(CONFIG_KEXEC)		+= machine_kexec_$(BITS).o
 obj-$(CONFIG_KEXEC)		+= relocate_kernel_$(BITS).o crash.o
 obj-$(CONFIG_KEXEC)		+= kexec-bzimage.o
+obj-$(CONFIG_KEXEC)		+= kexec-elf.o
 obj-$(CONFIG_CRASH_DUMP)	+= crash_dump_$(BITS).o
 obj-y				+= kprobes/
 obj-$(CONFIG_MODULES)		+= module.o
arch/x86/kernel/kexec-elf.c new file +231 -0

231 lines not on this page · raw message

arch/x86/kernel/machine_kexec_64.c +2 -0
index 37df7d3..e35bcaf 100644
@@ -22,10 +22,12 @@
 #include <asm/mmu_context.h>
 #include <asm/debugreg.h>
 #include <asm/kexec-bzimage.h>
+#include <asm/kexec-elf.h>
 
 /* arch dependent functionality related to kexec file based syscall */
 static struct kexec_file_type kexec_file_type[]={
 	{"bzImage64", bzImage64_probe, bzImage64_load, bzImage64_cleanup},
+	{"elf-x86_64", elf_x86_64_probe, elf_x86_64_load, elf_x86_64_cleanup},
 };
 
 static int nr_file_types = sizeof(kexec_file_type)/sizeof(kexec_file_type[0]);
Signature · 7 lines
1.8.4.2


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

Re: [PATCH 10/11] kexec: Support for loading ELF x86_64 images

Quoted 2 lines · Vivek Goyal

This patch provides support for kexec for loading ELF x86_64 images. I have tested it with loading vmlinux and it worked.

Can you please enlighten me what the use case for ELF kernel images is? bzImage I understand but what produces ELF images?

I see that kexec_file_load() can receive ELF segments too but why are we doing that?

Quoted 32 lines
---
 arch/x86/include/asm/kexec-elf.h   |  11 ++
 arch/x86/kernel/Makefile           |   1 +
 arch/x86/kernel/kexec-elf.c        | 231 +++++++++++++++++++++++++++++++++++++
 arch/x86/kernel/machine_kexec_64.c |   2 +
 4 files changed, 245 insertions(+)
 create mode 100644 arch/x86/include/asm/kexec-elf.h
 create mode 100644 arch/x86/kernel/kexec-elf.c
arch/x86/include/asm/kexec-elf.h new file +11 -0
new file mode 100644
index 0000000..afef382
@@ -0,0 +1,11 @@
+#ifndef _ASM_KEXEC_ELF_H
+#define _ASM_KEXEC_ELF_H
+
+extern int elf_x86_64_probe(const char *buf, unsigned long len);
+extern void *elf_x86_64_load(struct kimage *image, char *kernel,
+		unsigned long kernel_len, char *initrd,
+		unsigned long initrd_len, char *cmdline,
+		unsigned long cmdline_len);
+extern int elf_x86_64_cleanup(struct kimage *image);
+
+#endif  /* _ASM_KEXEC_ELF_H */
arch/x86/kernel/Makefile +0 -0
index fa9981d..2d77de7 100644

arch/x86/kernel/Makefile obj-$(CONFIG_KEXEC) += machine_kexec.o · line 74

Quoted 4 lines
arch/x86/kernel/Makefile +1 -0
@@ -71,6 +71,7 @@ obj-$(CONFIG_KEXEC)		+= machine_kexec.o
 obj-$(CONFIG_KEXEC)		+= machine_kexec_$(BITS).o
 obj-$(CONFIG_KEXEC)		+= relocate_kernel_$(BITS).o crash.o
 obj-$(CONFIG_KEXEC)		+= kexec-bzimage.o
+obj-$(CONFIG_KEXEC)		+= kexec-elf.o

It looks like kexec could slowly grow its own dir now:

arch/x86/kexec/

or so.

Quoted 46 lines
arch/x86/kernel/Makefile +0 -0
 obj-$(CONFIG_CRASH_DUMP)	+= crash_dump_$(BITS).o
 obj-y				+= kprobes/
 obj-$(CONFIG_MODULES)		+= module.o
arch/x86/kernel/kexec-elf.c new file +37 -0
new file mode 100644
index 0000000..ff1017c
@@ -0,0 +1,231 @@
+#include <linux/string.h>
+#include <linux/printk.h>
+#include <linux/errno.h>
+#include <linux/slab.h>
+#include <linux/kexec.h>
+#include <linux/kernel.h>
+#include <linux/mm.h>
+
+#include <asm/bootparam.h>
+#include <asm/setup.h>
+
+#ifdef CONFIG_X86_64
+
+struct elf_x86_64_data {
+	/*
+	 * Temporary buffer to hold bootparams buffer. This should be
+	 * freed once the bootparam segment has been loaded.
+	 */
+	void *bootparams_buf;
+};
+
+int elf_x86_64_probe(const char *buf, unsigned long len)
+{
+	int ret = -ENOEXEC;
+	Elf_Ehdr *ehdr;
+
+	if (len < sizeof(Elf_Ehdr)) {
+		pr_debug("File is too short to be an ELF executable.\n");
+		return ret;
+	}
+
+	ehdr = (Elf_Ehdr *)buf;
+
+	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0
+	    || ehdr->e_type != ET_EXEC || !elf_check_arch(ehdr)
+	    || ehdr->e_phentsize != sizeof(Elf_Phdr))
+		return -ENOEXEC;

arch/x86/kernel/kexec-elf.c line 47

Quoted 10 lines
arch/x86/kernel/kexec-elf.c new file +10 -0
+
+	if (ehdr->e_phoff >= len
+	    || (ehdr->e_phnum * sizeof(Elf_Phdr) > len - ehdr->e_phoff))
+		return -ENOEXEC;
+
+        /* I've got a bzImage */
+	pr_debug("It's an elf_x86_64 image.\n");
+	ret = 0;
+
+	return ret;

I think you can drop 'ret' here and return the error vals directly.

Quoted 6 lines
arch/x86/kernel/kexec-elf.c new file +6 -0
+}
+
+static int elf_exec_load(struct kimage *image, char *kernel)
+{
+	Elf_Ehdr *ehdr;
+	Elf_Phdr *phdrs;

arch/x86/kernel/kexec-elf.c line 63

Quoted 10 lines
arch/x86/kernel/kexec-elf.c new file +10 -0
+	int i, ret;
+	size_t filesz;
+	char *buffer;
+
+	ehdr = (Elf_Ehdr *)kernel;
+	phdrs = (void *)ehdr + ehdr->e_phoff;
+
+	for (i = 0; i < ehdr->e_phnum; i++) {
+		if (phdrs[i].p_type != PT_LOAD)
+			continue;

newline

Quoted 19 lines
arch/x86/kernel/kexec-elf.c new file +19 -0
+		filesz = phdrs[i].p_filesz;
+		if (filesz > phdrs[i].p_memsz)
+			filesz = phdrs[i].p_memsz;
+
+		buffer = (char *)ehdr + phdrs[i].p_offset;
+		ret = kexec_add_segment(image, buffer, filesz, phdrs[i].p_memsz,
+						phdrs[i].p_paddr);
+		if (ret)
+			break;
+	}
+
+	return ret;
+}
+
+/* Fill in fields which are usually present in bzImage */
+static int init_linux_parameters(struct boot_params *params)
+{
+	/*
+	 * FIXME: It is odd that the information which comes from kernel

arch/x86/kernel/kexec-elf.c line 92

Quoted 10 lines
arch/x86/kernel/kexec-elf.c new file +10 -0
+	 * has to be faked by loading kernel. I guess it is limitation of
+	 * ELF format. Right now keeping it same as kexec-tools
+	 * implementation. But this most likely needs fixing.
+	 */
+	memcpy(&params->hdr.header, "HdrS", 4);
+	params->hdr.version = 0x0206;
+	params->hdr.initrd_addr_max = 0x37FFFFFF;
+	params->hdr.cmdline_size = 2048;
+	return 0;
+}

Why a separate function? Its body is small enough to be merged into elf_x86_64_load.

arch/x86/kernel/kexec-elf.c line 98

Quoted 6 lines
arch/x86/kernel/kexec-elf.c new file +6 -0
+
+void *elf_x86_64_load(struct kimage *image, char *kernel,
+		unsigned long kernel_len,
+		char *initrd, unsigned long initrd_len,
+		char *cmdline, unsigned long cmdline_len)
+{

Btw, this functionality below looks very similar to the one in bzImage64_load(). Can we share some of it?

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
#42 lore raw

Re: [PATCH 10/11] kexec: Support for loading ELF x86_64 images

Quoted 6 lines · Borislav Petkov
Quoted 2 lines · Vivek Goyal

This patch provides support for kexec for loading ELF x86_64 images. I have tested it with loading vmlinux and it worked.

Can you please enlighten me what the use case for ELF kernel images is? bzImage I understand but what produces ELF images?

Before bzImage is generated, we produce an ELF vmlinux (linux-2.6/vmlinux). And then this vmlinux is processed further to produce bzImage.

In theory you can load this vmlinux and boot into it using kexec system call.

In general, Eric wanted to support ELF images hence I put in this patch.

Quoted 3 lines

I see that kexec_file_load() can receive ELF segments too but why are we doing that?

So kexec_file_load() does not know what kind of file it is. Whether an ELF file or an bzImage file. It will just call all the image loaders and see if some loader recognizes the file and claims it.

So idea is what kind of image one should be able to load using this new system call and kexec into.

My primary use case is bzImage. Others think that it should be generic enough to be able to handle ELF too.

Quoted 40 lines
Quoted 33 lines
---
 arch/x86/include/asm/kexec-elf.h   |  11 ++
 arch/x86/kernel/Makefile           |   1 +
 arch/x86/kernel/kexec-elf.c        | 231 +++++++++++++++++++++++++++++++++++++
 arch/x86/kernel/machine_kexec_64.c |   2 +
 4 files changed, 245 insertions(+)
 create mode 100644 arch/x86/include/asm/kexec-elf.h
 create mode 100644 arch/x86/kernel/kexec-elf.c
arch/x86/include/asm/kexec-elf.h new file +11 -0
new file mode 100644
index 0000000..afef382
@@ -0,0 +1,11 @@
+#ifndef _ASM_KEXEC_ELF_H
+#define _ASM_KEXEC_ELF_H
+
+extern int elf_x86_64_probe(const char *buf, unsigned long len);
+extern void *elf_x86_64_load(struct kimage *image, char *kernel,
+		unsigned long kernel_len, char *initrd,
+		unsigned long initrd_len, char *cmdline,
+		unsigned long cmdline_len);
+extern int elf_x86_64_cleanup(struct kimage *image);
+
+#endif  /* _ASM_KEXEC_ELF_H */
arch/x86/kernel/Makefile +0 -0
index fa9981d..2d77de7 100644

arch/x86/kernel/Makefile obj-$(CONFIG_KEXEC) += machine_kexec.o · line 74

Quoted 4 lines
arch/x86/kernel/Makefile +1 -0
@@ -71,6 +71,7 @@ obj-$(CONFIG_KEXEC)		+= machine_kexec.o
 obj-$(CONFIG_KEXEC)		+= machine_kexec_$(BITS).o
 obj-$(CONFIG_KEXEC)		+= relocate_kernel_$(BITS).o crash.o
 obj-$(CONFIG_KEXEC)		+= kexec-bzimage.o
+obj-$(CONFIG_KEXEC)		+= kexec-elf.o

It looks like kexec could slowly grow its own dir now:

Quoted 1 line

arch/x86/kexec/

I was rather thinking of arch/x86/kernel/kexec. But that's for some other day. Not part of this patchset. This is alredy too big and I don't want to make any changes which are nice to have and bloat the patch size.

Quoted 53 lines

or so.

Quoted 50 lines
arch/x86/kernel/Makefile +0 -0
 obj-$(CONFIG_CRASH_DUMP)	+= crash_dump_$(BITS).o
 obj-y				+= kprobes/
 obj-$(CONFIG_MODULES)		+= module.o
arch/x86/kernel/kexec-elf.c new file +41 -0
new file mode 100644
index 0000000..ff1017c
@@ -0,0 +1,231 @@
+#include <linux/string.h>
+#include <linux/printk.h>
+#include <linux/errno.h>
+#include <linux/slab.h>
+#include <linux/kexec.h>
+#include <linux/kernel.h>
+#include <linux/mm.h>
+
+#include <asm/bootparam.h>
+#include <asm/setup.h>
+
+#ifdef CONFIG_X86_64
+
+struct elf_x86_64_data {
+	/*
+	 * Temporary buffer to hold bootparams buffer. This should be
+	 * freed once the bootparam segment has been loaded.
+	 */
+	void *bootparams_buf;
+};
+
+int elf_x86_64_probe(const char *buf, unsigned long len)
+{
+	int ret = -ENOEXEC;
+	Elf_Ehdr *ehdr;
+
+	if (len < sizeof(Elf_Ehdr)) {
+		pr_debug("File is too short to be an ELF executable.\n");
+		return ret;
+	}
+
+	ehdr = (Elf_Ehdr *)buf;
+
+	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0
+	    || ehdr->e_type != ET_EXEC || !elf_check_arch(ehdr)
+	    || ehdr->e_phentsize != sizeof(Elf_Phdr))
+		return -ENOEXEC;
+
+	if (ehdr->e_phoff >= len
+	    || (ehdr->e_phnum * sizeof(Elf_Phdr) > len - ehdr->e_phoff))
+		return -ENOEXEC;
Quoted 8 lines

arch/x86/kernel/kexec-elf.c line 47

Quoted 6 lines
arch/x86/kernel/kexec-elf.c new file +6 -0
+
+        /* I've got a bzImage */
+	pr_debug("It's an elf_x86_64 image.\n");
+	ret = 0;
+
+	return ret;

I think you can drop 'ret' here and return the error vals directly.

Yep, will simplify this one.

Quoted 11 lines
Quoted 11 lines
arch/x86/kernel/kexec-elf.c new file +10 -0
+}
+
+static int elf_exec_load(struct kimage *image, char *kernel)
+{
+	Elf_Ehdr *ehdr;
+	Elf_Phdr *phdrs;
+	int i, ret;
+	size_t filesz;
+	char *buffer;
+
Quoted 8 lines

arch/x86/kernel/kexec-elf.c line 63

Quoted 6 lines
arch/x86/kernel/kexec-elf.c new file +6 -0
+	ehdr = (Elf_Ehdr *)kernel;
+	phdrs = (void *)ehdr + ehdr->e_phoff;
+
+	for (i = 0; i < ehdr->e_phnum; i++) {
+		if (phdrs[i].p_type != PT_LOAD)
+			continue;

newline

What's that?

Quoted 25 lines
Quoted 25 lines
arch/x86/kernel/kexec-elf.c new file +24 -0
+		filesz = phdrs[i].p_filesz;
+		if (filesz > phdrs[i].p_memsz)
+			filesz = phdrs[i].p_memsz;
+
+		buffer = (char *)ehdr + phdrs[i].p_offset;
+		ret = kexec_add_segment(image, buffer, filesz, phdrs[i].p_memsz,
+						phdrs[i].p_paddr);
+		if (ret)
+			break;
+	}
+
+	return ret;
+}
+
+/* Fill in fields which are usually present in bzImage */
+static int init_linux_parameters(struct boot_params *params)
+{
+	/*
+	 * FIXME: It is odd that the information which comes from kernel
+	 * has to be faked by loading kernel. I guess it is limitation of
+	 * ELF format. Right now keeping it same as kexec-tools
+	 * implementation. But this most likely needs fixing.
+	 */
+	memcpy(&params->hdr.header, "HdrS", 4);
Quoted 8 lines

arch/x86/kernel/kexec-elf.c line 92

Quoted 5 lines
arch/x86/kernel/kexec-elf.c new file +5 -0
+	params->hdr.version = 0x0206;
+	params->hdr.initrd_addr_max = 0x37FFFFFF;
+	params->hdr.cmdline_size = 2048;
+	return 0;
+}

Why a separate function? Its body is small enough to be merged into elf_x86_64_load.

Actually this logic shows the limitation of ELF format kernel image. This information should be exported by the image so that loader can do some verifications. But instead loader is hardcoding this info and faking things.

For example, it should be kernel which tells what's the maximum command line size it can handle and then loader can return error if user specified command line size is greater than what new kernel can handle.

Similarly, what's the max address initrd can be loaded at.

Actually I have copied this code from kexec-tools. And I am wondering if some of this is

I am not sure why we set hdr.version and hdr.header. Are there any assumptions in booth path kernel is making. May be some other code down the line is parsing it or it is completely redundant. I think I will play with removing setting of hdr.version and hdr.header and see how does it go.

so I put it in a separate function because user space code had it that way. Also because I did not like this part of the code and this looks like a limitation of ELF format, I wanted to isolate it in a separate function so that it is easy to spot it.

Quoted 10 lines

arch/x86/kernel/kexec-elf.c line 98

Quoted 7 lines
arch/x86/kernel/kexec-elf.c new file +6 -0
+
+void *elf_x86_64_load(struct kimage *image, char *kernel,
+		unsigned long kernel_len,
+		char *initrd, unsigned long initrd_len,
+		char *cmdline, unsigned long cmdline_len)
+{

Btw, this functionality below looks very similar to the one in bzImage64_load(). Can we share some of it?

It looks similar but values with which some of the functions are called are different. I will see if there are some obivious candidate to share the code. It is not a lot of code to begin with.

Thanks
Vivek

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

maintainer
#43 lore raw

Re: [PATCH 10/11] kexec: Support for loading ELF x86_64 images

Quoted 3 lines · Vivek Goyal

I was rather thinking of arch/x86/kernel/kexec. But that's for some other day. Not part of this patchset. This is alredy too big and I don't want to make any changes which are nice to have and bloat the patch size.

Ok.

Btw, we were talking about the "kernel" part of the path. And, as Ingo said, it is all kernel so arch/x86/kexec should be perfectly fine.

...

Quoted 7 lines
Quoted 5 lines

arch/x86/kernel/kexec-elf.c line 63

Quoted 3 lines
arch/x86/kernel/kexec-elf.c new file +3 -0
+	for (i = 0; i < ehdr->e_phnum; i++) {
+		if (phdrs[i].p_type != PT_LOAD)
+			continue;

newline

What's that?

Ah, that's: "please add an empty line here" for better readability.

Quoted 30 lines
Quoted 10 lines
Quoted 10 lines
arch/x86/kernel/kexec-elf.c new file +10 -0
+/* Fill in fields which are usually present in bzImage */
+static int init_linux_parameters(struct boot_params *params)
+{
+	/*
+	 * FIXME: It is odd that the information which comes from kernel
+	 * has to be faked by loading kernel. I guess it is limitation of
+	 * ELF format. Right now keeping it same as kexec-tools
+	 * implementation. But this most likely needs fixing.
+	 */
+	memcpy(&params->hdr.header, "HdrS", 4);
Quoted 8 lines

arch/x86/kernel/kexec-elf.c line 92

Quoted 5 lines
arch/x86/kernel/kexec-elf.c new file +5 -0
+	params->hdr.version = 0x0206;
+	params->hdr.initrd_addr_max = 0x37FFFFFF;
+	params->hdr.cmdline_size = 2048;
+	return 0;
+}

Why a separate function? Its body is small enough to be merged into elf_x86_64_load.

Actually this logic shows the limitation of ELF format kernel image. This information should be exported by the image so that loader can do some verifications. But instead loader is hardcoding this info and faking things.

For example, it should be kernel which tells what's the maximum command line size it can handle and then loader can return error if user specified command line size is greater than what new kernel can handle.

Similarly, what's the max address initrd can be loaded at.

Quoted 8 lines

Actually I have copied this code from kexec-tools. And I am wondering if some of this is

I am not sure why we set hdr.version and hdr.header. Are there any assumptions in booth path kernel is making. May be some other code down the line is parsing it or it is completely redundant. I think I will play with removing setting of hdr.version and hdr.header and see how does it go.

Well, this is mandated by the boot protocol, no?

"If the "HdrS" (0x53726448) magic number is not found at offset 0x202, the boot protocol version is "old". Loading an old kernel, the following parameters should be assumed:

Image type = zImage
initrd not supported
Real-mode kernel must be located at 0x90000."

About version 0x0206:

Field name:     cmdline_size
Type:           read
Offset/size:    0x238/4
Protocol:       2.06+

The maximum size of the command line without the terminating zero. This means that the command line can contain at most cmdline_size characters. With protocol version 2.05 and earlier, the maximum size was 255.

So according to the protocol, cmdline_size should be set by kexec_file_load and not hardcoded to 2K, if we're mandating protocol version 2.06.

Maybe hpa can comment on all this.

Quoted 4 lines

so I put it in a separate function because user space code had it that way. Also because I did not like this part of the code and this looks like a limitation of ELF format, I wanted to isolate it in a separate function so that it is easy to spot it.

Right.

Thanks.

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
maintainer
#44 lore raw

Re: [PATCH 10/11] kexec: Support for loading ELF x86_64 images

Quoted 15 lines · Borislav Petkov
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
arch/x86/kernel/kexec-elf.c new file +15 -0
+/* Fill in fields which are usually present in bzImage */
+static int init_linux_parameters(struct boot_params *params)
+{
+	/*
+	 * FIXME: It is odd that the information which comes from kernel
+	 * has to be faked by loading kernel. I guess it is limitation of
+	 * ELF format. Right now keeping it same as kexec-tools
+	 * implementation. But this most likely needs fixing.
+	 */
+	memcpy(&params->hdr.header, "HdrS", 4);
+	params->hdr.version = 0x0206;
+	params->hdr.initrd_addr_max = 0x37FFFFFF;
+	params->hdr.cmdline_size = 2048;
+	return 0;
+}

...

Quoted 17 lines

Well, this is mandated by the boot protocol, no?

"If the "HdrS" (0x53726448) magic number is not found at offset 0x202, the boot protocol version is "old". Loading an old kernel, the following parameters should be assumed:

Image type = zImage
initrd not supported
Real-mode kernel must be located at 0x90000."

About version 0x0206:

Field name:     cmdline_size
Type:           read
Offset/size:    0x238/4
Protocol:       2.06+
Quoted 8 lines

The maximum size of the command line without the terminating zero. This means that the command line can contain at most cmdline_size characters. With protocol version 2.05 and earlier, the maximum size was 255.

So according to the protocol, cmdline_size should be set by kexec_file_load and not hardcoded to 2K, if we're mandating protocol version 2.06.

Btw, all those fields above are of type "read", which means,

"For each field, some are information from the kernel to the bootloader ("read"),... "

Now, if we assume that the first kernel is the bootloader and it loads the second kernel, then the first kernel shouldn't be setting all those fields *at* *all* because it violates the boot protocol. And that's a no-no.

Generally, how important is that use case for loading ELF images? If no one is actually using it, why even bother?

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
#45 lore raw

Re: [PATCH 10/11] kexec: Support for loading ELF x86_64 images

On Fri, Mar 07, 2014 at 07:39:59PM +0100, Borislav Petkov wrote:

[..]

Quoted 28 lines
Quoted 17 lines

Well, this is mandated by the boot protocol, no?

"If the "HdrS" (0x53726448) magic number is not found at offset 0x202, the boot protocol version is "old". Loading an old kernel, the following parameters should be assumed:

Image type = zImage
initrd not supported
Real-mode kernel must be located at 0x90000."

About version 0x0206:

Field name:     cmdline_size
Type:           read
Offset/size:    0x238/4
Protocol:       2.06+
Quoted 8 lines

The maximum size of the command line without the terminating zero. This means that the command line can contain at most cmdline_size characters. With protocol version 2.05 and earlier, the maximum size was 255.

So according to the protocol, cmdline_size should be set by kexec_file_load and not hardcoded to 2K, if we're mandating protocol version 2.06.

Btw, all those fields above are of type "read", which means,

Quoted 7 lines

"For each field, some are information from the kernel to the bootloader ("read"),... "

Now, if we assume that the first kernel is the bootloader and it loads the second kernel, then the first kernel shouldn't be setting all those fields *at* *all* because it violates the boot protocol. And that's a no-no.

Yes, that's a read only information for bootloader.

Quoted 4 lines

Generally, how important is that use case for loading ELF images? If no one is actually using it, why even bother?

Distributions use bzImage for booting. Personally I have not seen anybody using ELF vmliux for booting.

When I posted V1 of patches, Eric Biederman had said that without ELF loader support this patchset should not go in. He said that ELF is very generic and if we are implementing a new system call we should support ELF loader.

So I have put a basic ELF loader in patches to show that it can work. Not that I am very keen on it.

I will gladly remove it if others are comfortable with the idea that we don't need an ELF loader at this point of time.

Thanks
Vivek

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

maintainer
#46 lore raw

Re: [PATCH 10/11] kexec: Support for loading ELF x86_64 images

Quoted 4 lines · Vivek Goyal

When I posted V1 of patches, Eric Biederman had said that without ELF loader support this patchset should not go in. He said that ELF is very generic and if we are implementing a new system call we should support ELF loader.

But why, if nobody uses it?

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
#47 lore raw

Re: [PATCH 10/11] kexec: Support for loading ELF x86_64 images

Quoted 7 lines · Borislav Petkov
Quoted 4 lines · Vivek Goyal

When I posted V1 of patches, Eric Biederman had said that without ELF loader support this patchset should not go in. He said that ELF is very generic and if we are implementing a new system call we should support ELF loader.

But why, if nobody uses it?

Eric, can you please help answer this one.

Thanks
Vivek

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

#48 lore raw

PATCH v2 11/11 kexec: Support for Kexec on panic using new system call

In reply to Vivek Goyal

This patch adds support for loading a kexec on panic (kdump) kernel usning new system call. Right now this primarily works with bzImage loader only. But changes to ELF loader should be minimal as all the core infrastrcture is there.

Only thing preventing making ELF load in crash reseved memory is that kernel vmlinux is of type ET_EXEC and it expects to be loaded at address it has been compiled for. At that location current kernel is already running. One first needs to make vmlinux fully relocatable and export it is type ET_DYN and then modify this ELF loader to support images of type ET_DYN.

I am leaving it as a future TODO item.

---
 arch/x86/include/asm/crash.h       |   9 +
 arch/x86/include/asm/kexec.h       |  25 +-
 arch/x86/kernel/crash.c            | 574 +++++++++++++++++++++++++++++++++++++
 arch/x86/kernel/kexec-bzimage.c    |  27 +-
 arch/x86/kernel/kexec-elf.c        |   4 +-
 arch/x86/kernel/machine_kexec.c    |  21 +-
 arch/x86/kernel/machine_kexec_64.c |  38 +++
 kernel/kexec.c                     |  83 +++++-
 8 files changed, 763 insertions(+), 18 deletions(-)
 create mode 100644 arch/x86/include/asm/crash.h
arch/x86/include/asm/crash.h new file +9 -0
new file mode 100644
index 0000000..2dd2eb8
@@ -0,0 +1,9 @@
+#ifndef _ASM_X86_CRASH_H
+#define _ASM_X86_CRASH_H
+
+int load_crashdump_segments(struct kimage *image);
+int crash_copy_backup_region(struct kimage *image);
+int crash_setup_memmap_entries(struct kimage *image,
+		struct boot_params *params);
+
+#endif /* _ASM_X86_CRASH_H */
arch/x86/include/asm/kexec.h +23 -2
index 9bd6fec..a330d85 100644
@@ -25,6 +25,8 @@
 #include <asm/ptrace.h>
 #include <asm/bootparam.h>
 
+struct kimage;
+
 /*
  * KEXEC_SOURCE_MEMORY_LIMIT maximum page get_free_page can return.
  * I.e. Maximum page that is mapped directly into kernel memory,
@@ -62,6 +64,10 @@
 # define KEXEC_ARCH KEXEC_ARCH_X86_64
 #endif
 
+/* Memory to backup during crash kdump */
+#define KEXEC_BACKUP_SRC_START	(0UL)
+#define KEXEC_BACKUP_SRC_END	(655360UL)	/* 640K */
+
 /*
  * CPU does not save ss and sp on stack if execution is already
  * running in kernel mode at the time of NMI occurrence. This code
@@ -161,8 +167,21 @@ struct kimage_arch {
 	pud_t *pud;
 	pmd_t *pmd;
 	pte_t *pte;
+	/* Details of backup region */
+	unsigned long backup_src_start;
+	unsigned long backup_src_sz;
+
+	/* Physical address of backup segment */
+	unsigned long backup_load_addr;
+
+	/* Core ELF header buffer */
+	unsigned long elf_headers;
+	unsigned long elf_headers_sz;
+	unsigned long elf_load_addr;
 };
+#endif /* CONFIG_X86_32 */
 
+#ifdef CONFIG_X86_64
 struct kexec_entry64_regs {
 	uint64_t rax;
 	uint64_t rbx;
@@ -189,11 +208,13 @@ extern crash_vmclear_fn __rcu *crash_vmclear_loaded_vmcss;
 
 extern int kexec_setup_initrd(struct boot_params *boot_params,
 		unsigned long initrd_load_addr, unsigned long initrd_len);
-extern int kexec_setup_cmdline(struct boot_params *boot_params,
+extern int kexec_setup_cmdline(struct kimage *image,
+		struct boot_params *boot_params,
 		unsigned long bootparams_load_addr,
 		unsigned long cmdline_offset, char *cmdline,
 		unsigned long cmdline_len);
-extern int kexec_setup_boot_parameters(struct boot_params *params);
+extern int kexec_setup_boot_parameters(struct kimage *image,
+					struct boot_params *params);
 
 
 #endif /* __ASSEMBLY__ */
arch/x86/kernel/crash.c +574 -0

595 lines not on this page · raw message

arch/x86/kernel/kexec-bzimage.c +24 -3
index cbfcd00..f66188d 100644
@@ -8,6 +8,9 @@
 
 #include <asm/bootparam.h>
 #include <asm/setup.h>
+#include <asm/crash.h>
+
+#define MAX_ELFCOREHDR_STR_LEN	30 	/* elfcorehdr=0x<64bit-value> */
 
 #ifdef CONFIG_X86_64
 
@@ -100,11 +103,28 @@ void *bzImage64_load(struct kimage *image, char *kernel,
 		return ERR_PTR(-EINVAL);
 	}
 
+	/*
+	 * In case of crash dump, we will append elfcorehdr=<addr> to
+	 * command line. Make sure it does not overflow
+	 */
+	if (cmdline_len + MAX_ELFCOREHDR_STR_LEN > header->cmdline_size) {
+		ret = -EINVAL;
+		pr_debug("Kernel command line too long\n");
+		return ERR_PTR(-EINVAL);
+	}
+
 	/* Allocate loader specific data */
 	ldata = kzalloc(sizeof(struct bzimage64_data), GFP_KERNEL);
 	if (!ldata)
 		return ERR_PTR(-ENOMEM);
 
+	/* Allocate and load backup region */
+	if (image->type == KEXEC_TYPE_CRASH) {
+		ret = load_crashdump_segments(image);
+		if (ret)
+			goto out_free_loader_data;
+	}
+
 	/*
 	 * Load purgatory. For 64bit entry point, purgatory  code can be
 	 * anywhere.
@@ -118,7 +138,8 @@ void *bzImage64_load(struct kimage *image, char *kernel,
 	pr_debug("Loaded purgatory at 0x%lx\n", purgatory_load_addr);
 
 	/* Load Bootparams and cmdline */
-	params_cmdline_sz = sizeof(struct boot_params) + cmdline_len;
+	params_cmdline_sz = sizeof(struct boot_params) + cmdline_len +
+				MAX_ELFCOREHDR_STR_LEN;
 	params = kzalloc(params_cmdline_sz, GFP_KERNEL);
 	if (!params) {
 		ret = -ENOMEM;
@@ -167,7 +188,7 @@ void *bzImage64_load(struct kimage *image, char *kernel,
 			goto out_free_params;
 	}
 
-	ret = kexec_setup_cmdline(params, bootparam_load_addr,
+	ret = kexec_setup_cmdline(image, params, bootparam_load_addr,
 			sizeof(struct boot_params), cmdline, cmdline_len);
 	if (ret)
 		goto out_free_params;
@@ -198,7 +219,7 @@ void *bzImage64_load(struct kimage *image, char *kernel,
 	if (ret)
 		goto out_free_params;
 
-	ret = kexec_setup_boot_parameters(params);
+	ret = kexec_setup_boot_parameters(image, params);
 	if (ret)
 		goto out_free_params;
 
arch/x86/kernel/kexec-elf.c +2 -2
index ff1017c..e7ce2d0 100644
@@ -164,7 +164,7 @@ void *elf_x86_64_load(struct kimage *image, char *kernel,
 			goto out_free_params;
 	}
 
-	ret = kexec_setup_cmdline(params, bootparam_load_addr,
+	ret = kexec_setup_cmdline(image, params, bootparam_load_addr,
 			sizeof(struct boot_params), cmdline, cmdline_len);
 	if (ret)
 		goto out_free_params;
@@ -195,7 +195,7 @@ void *elf_x86_64_load(struct kimage *image, char *kernel,
 	if (ret)
 		goto out_free_params;
 
-	ret = kexec_setup_boot_parameters(params);
+	ret = kexec_setup_boot_parameters(image, params);
 	if (ret)
 		goto out_free_params;
 
arch/x86/kernel/machine_kexec.c +17 -4
index ac55890..26d474b 100644
@@ -10,9 +10,11 @@
  */
 
 #include <linux/kernel.h>
+#include <linux/kexec.h>
 #include <linux/string.h>
 #include <asm/bootparam.h>
 #include <asm/setup.h>
+#include <asm/crash.h>
 
 /*
  * Common code for x86 and x86_64 used for kexec.
@@ -36,18 +38,24 @@ int kexec_setup_initrd(struct boot_params *boot_params,
 	return 0;
 }
 
-int kexec_setup_cmdline(struct boot_params *boot_params,
+int kexec_setup_cmdline(struct kimage *image, struct boot_params *boot_params,
 		unsigned long bootparams_load_addr,
 		unsigned long cmdline_offset, char *cmdline,
 		unsigned long cmdline_len)
 {
 	char *cmdline_ptr = ((char *)boot_params) + cmdline_offset;
-	unsigned long cmdline_ptr_phys;
+	unsigned long cmdline_ptr_phys, len;
 	uint32_t cmdline_low_32, cmdline_ext_32;
 
 	memcpy(cmdline_ptr, cmdline, cmdline_len);
+	if (image->type == KEXEC_TYPE_CRASH) {
+		len = sprintf(cmdline_ptr + cmdline_len - 1,
+			" elfcorehdr=0x%lx", image->arch.elf_load_addr);
+		cmdline_len += len;
+	}
 	cmdline_ptr[cmdline_len - 1] = '\0';
 
+	pr_debug("Final command line is:%s\n", cmdline_ptr);
 	cmdline_ptr_phys = bootparams_load_addr + cmdline_offset;
 	cmdline_low_32 = cmdline_ptr_phys & 0xffffffffUL;
 	cmdline_ext_32 = cmdline_ptr_phys >> 32;
@@ -75,7 +83,8 @@ static int setup_memory_map_entries(struct boot_params *params)
 	return 0;
 }
 
-int kexec_setup_boot_parameters(struct boot_params *params)
+int kexec_setup_boot_parameters(struct kimage *image,
+				struct boot_params *params)
 {
 	unsigned int nr_e820_entries;
 	unsigned long long mem_k, start, end;
@@ -102,7 +111,11 @@ int kexec_setup_boot_parameters(struct boot_params *params)
 	/* Default sysdesc table */
 	params->sys_desc_table.length = 0;
 
-	setup_memory_map_entries(params);
+	if (image->type == KEXEC_TYPE_CRASH)
+		crash_setup_memmap_entries(image, params);
+	else
+		setup_memory_map_entries(params);
+
 	nr_e820_entries = params->e820_entries;
 
 	for(i = 0; i < nr_e820_entries; i++) {
arch/x86/kernel/machine_kexec_64.c +38 -0
index e35bcaf..e852a51 100644
@@ -181,6 +181,38 @@ static void load_segments(void)
 		);
 }
 
+/* Update purgatory as needed after various image segments have been prepared */
+static int arch_update_purgatory(struct kimage *image)
+{
+	int ret = 0;
+
+	if (!image->file_mode)
+		return 0;
+
+	/* Setup copying of backup region */
+	if (image->type == KEXEC_TYPE_CRASH) {
+		ret = kexec_purgatory_get_set_symbol(image, "backup_dest",
+				&image->arch.backup_load_addr,
+				sizeof(image->arch.backup_load_addr), 0);
+		if (ret)
+			return ret;
+
+		ret = kexec_purgatory_get_set_symbol(image, "backup_src",
+				&image->arch.backup_src_start,
+				sizeof(image->arch.backup_src_start), 0);
+		if (ret)
+			return ret;
+
+		ret = kexec_purgatory_get_set_symbol(image, "backup_sz",
+				&image->arch.backup_src_sz,
+				sizeof(image->arch.backup_src_sz), 0);
+		if (ret)
+			return ret;
+	}
+
+	return ret;
+}
+
 int machine_kexec_prepare(struct kimage *image)
 {
 	unsigned long start_pgtable;
@@ -194,6 +226,11 @@ int machine_kexec_prepare(struct kimage *image)
 	if (result)
 		return result;
 
+	/* update purgatory as needed */
+	result = arch_update_purgatory(image);
+	if (result)
+		return result;
+
 	return 0;
 }
 
@@ -330,6 +367,7 @@ int arch_kimage_file_post_load_cleanup(struct kimage *image)
 {
 	int idx = image->file_handler_idx;
 
+	vfree((void *)image->arch.elf_headers);
 	if (kexec_file_type[idx].cleanup)
 		return kexec_file_type[idx].cleanup(image);
 	return 0;
kernel/kexec.c +76 -7

127 lines not on this page · raw message

Signature · 7 lines
1.8.4.2


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

Re: [PATCH 11/11] kexec: Support for Kexec on panic using new system call

Quoted 12 lines · Vivek Goyal

This patch adds support for loading a kexec on panic (kdump) kernel usning new system call. Right now this primarily works with bzImage loader only. But changes to ELF loader should be minimal as all the core infrastrcture is there.

Only thing preventing making ELF load in crash reseved memory is that kernel vmlinux is of type ET_EXEC and it expects to be loaded at address it has been compiled for. At that location current kernel is already running. One first needs to make vmlinux fully relocatable and export it is type ET_DYN and then modify this ELF loader to support images of type ET_DYN.

Quoted 3 lines

I am leaving it as a future TODO item.

checkpatch: total: 2 errors, 10 warnings, 977 lines checked

Quoted 14 lines
arch/x86/include/asm/kexec.h +2 -0
index 9bd6fec..a330d85 100644
@@ -25,6 +25,8 @@
 #include <asm/ptrace.h>
 #include <asm/bootparam.h>
 
+struct kimage;
+
 /*
  * KEXEC_SOURCE_MEMORY_LIMIT maximum page get_free_page can return.
  * I.e. Maximum page that is mapped directly into kernel memory,

arch/x86/include/asm/kexec.h line 69

Quoted 6 lines
arch/x86/include/asm/kexec.h +3 -0
@@ -62,6 +64,10 @@
 # define KEXEC_ARCH KEXEC_ARCH_X86_64
 #endif
 
+/* Memory to backup during crash kdump */
+#define KEXEC_BACKUP_SRC_START	(0UL)
+#define KEXEC_BACKUP_SRC_END	(655360UL)	/* 640K */

I guess

#define KEXEC_BACKUP_SRC_END (640 * 1024UL)

should be more clear.

Quoted 65 lines
arch/x86/include/asm/kexec.h +17 -2
 /*
  * CPU does not save ss and sp on stack if execution is already
  * running in kernel mode at the time of NMI occurrence. This code
@@ -161,8 +167,21 @@ struct kimage_arch {
 	pud_t *pud;
 	pmd_t *pmd;
 	pte_t *pte;
+	/* Details of backup region */
+	unsigned long backup_src_start;
+	unsigned long backup_src_sz;
+
+	/* Physical address of backup segment */
+	unsigned long backup_load_addr;
+
+	/* Core ELF header buffer */
+	unsigned long elf_headers;
+	unsigned long elf_headers_sz;
+	unsigned long elf_load_addr;
 };
+#endif /* CONFIG_X86_32 */
 
+#ifdef CONFIG_X86_64
 struct kexec_entry64_regs {
 	uint64_t rax;
 	uint64_t rbx;
@@ -189,11 +208,13 @@ extern crash_vmclear_fn __rcu *crash_vmclear_loaded_vmcss;
 
 extern int kexec_setup_initrd(struct boot_params *boot_params,
 		unsigned long initrd_load_addr, unsigned long initrd_len);
-extern int kexec_setup_cmdline(struct boot_params *boot_params,
+extern int kexec_setup_cmdline(struct kimage *image,
+		struct boot_params *boot_params,
 		unsigned long bootparams_load_addr,
 		unsigned long cmdline_offset, char *cmdline,
 		unsigned long cmdline_len);
-extern int kexec_setup_boot_parameters(struct boot_params *params);
+extern int kexec_setup_boot_parameters(struct kimage *image,
+					struct boot_params *params);
 
 
 #endif /* __ASSEMBLY__ */
arch/x86/kernel/crash.c +4 -0
index a57902e..8eabde4 100644
@@ -4,6 +4,9 @@
  * Created by: Hariprasad Nellitheertha ([email protected])
  *
  * Copyright (C) IBM Corporation, 2004. All rights reserved.
+ * Copyright (C) Red Hat Inc., 2014. All rights reserved.
+ * Authors:
+ * 	Vivek Goyal <[email protected]>
  *
  */
 
@@ -16,6 +19,7 @@
 #include <linux/elf.h>
 #include <linux/elfcore.h>
 #include <linux/module.h>
+#include <linux/slab.h>
 
 #include <asm/processor.h>
 #include <asm/hardirq.h>
@@ -28,6 +32,45 @@
 #include <asm/reboot.h>

arch/x86/kernel/crash.c line 42

Quoted 10 lines
arch/x86/kernel/crash.c +8 -0
 #include <asm/virtext.h>
 
+/* Alignment required for elf header segment */
+#define ELF_CORE_HEADER_ALIGN   4096
+
+/* This primarily reprsents number of split ranges due to exclusion */
+#define CRASH_MAX_RANGES	16
+
+struct crash_mem_range {
+	unsigned long long start, end;

u64?

Quoted 15 lines
arch/x86/kernel/crash.c +15 -0
+};
+
+struct crash_mem {
+	unsigned int nr_ranges;
+	struct crash_mem_range ranges[CRASH_MAX_RANGES];
+};
+
+/* Misc data about ram ranges needed to prepare elf headers */
+struct crash_elf_data {
+	struct kimage *image;
+	/*
+	 * Total number of ram ranges we have after various ajustments for
+	 * GART, crash reserved region etc.
+	 */
+	unsigned int max_nr_ranges;

arch/x86/kernel/crash.c line 67

Quoted 10 lines
arch/x86/kernel/crash.c +10 -0
+	unsigned long gart_start, gart_end;
+
+	/* Pointer to elf header */
+	void *ehdr;
+	/* Pointer to next phdr */
+	void *bufp;
+	struct crash_mem mem;
+};
+
+/* Used while prepareing memory map entries for second kernel */

s/prepareing/preparing/

Quoted 29 lines
arch/x86/kernel/crash.c +22 -0
+struct crash_memmap_data {
+	struct boot_params *params;
+	/* Type of memory */
+	unsigned int type;
+};
+
 int in_crash_kexec;
 
 /*
@@ -137,3 +180,534 @@ void native_machine_crash_shutdown(struct pt_regs *regs)
 #endif
 	crash_save_cpu(regs, safe_smp_processor_id());
 }
+
+#ifdef CONFIG_X86_64
+
+static int get_nr_ram_ranges_callback(unsigned long start_pfn,
+				unsigned long nr_pfn, void *arg)
+{
+	int *nr_ranges = arg;
+
+	(*nr_ranges)++;
+	return 0;
+}
+
+static int get_gart_ranges_callback(u64 start, u64 end, void *arg)
+{
+	struct crash_elf_data *ced = arg;
+

arch/x86/kernel/crash.c void native_machine_crash_shutdown(struct pt_regs *regs) · line 208

Quoted 10 lines
arch/x86/kernel/crash.c +10 -0
+	ced->gart_start = start;
+	ced->gart_end = end;
+
+	/* Not expecting more than 1 gart aperture */
+	return 1;
+}
+
+
+/* Gather all the required information to prepare elf headers for ram regions */
+static int fill_up_ced(struct crash_elf_data *ced, struct kimage *image)

All other functions have nice, spelled out names but not this one :)

Why not fill_up_crash_elf_data()?

Quoted 7 lines
arch/x86/kernel/crash.c +7 -0
+{
+	unsigned int nr_ranges = 0;
+
+	ced->image = image;
+
+	walk_system_ram_range(0, -1, &nr_ranges,
+				get_nr_ram_ranges_callback);

arch/x86/kernel/crash.c void native_machine_crash_shutdown(struct pt_regs *regs) · line 225

Quoted 10 lines
arch/x86/kernel/crash.c +10 -0
+
+	ced->max_nr_ranges = nr_ranges;
+
+	/*
+	 * We don't create ELF headers for GART aperture as an attempt
+	 * to dump this memory in second kernel leads to hang/crash.
+	 * If gart aperture is present, one needs to exclude that region
+	 * and that could lead to need of extra phdr.
+	 */
+

superfluous newline.

Quoted 19 lines
arch/x86/kernel/crash.c +19 -0
+	walk_ram_res("GART", IORESOURCE_MEM, 0, -1,
+				ced, get_gart_ranges_callback);
+
+	/*
+	 * If we have gart region, excluding that could potentially split
+	 * a memory range, resulting in extra header. Account for  that.
+	 */
+	if (ced->gart_end)
+		ced->max_nr_ranges++;
+
+	/* Exclusion of crash region could split memory ranges */
+	ced->max_nr_ranges++;
+
+	/* If crashk_low_res is there, another range split possible */
+	if (crashk_low_res.end != 0)
+		ced->max_nr_ranges++;
+
+	return 0;
+}

...

Quoted 29 lines
arch/x86/kernel/crash.c +29 -0
+int load_crashdump_segments(struct kimage *image)
+{
+	unsigned long src_start, src_sz;
+	unsigned long elf_addr, elf_sz;
+	int ret;
+
+	/*
+	 * Determine and load a segment for backup area. First 640K RAM
+	 * region is backup source
+	 */
+
+	ret = walk_system_ram_res(KEXEC_BACKUP_SRC_START, KEXEC_BACKUP_SRC_END,
+				image, determine_backup_region);
+
+	/* Zero or postive return values are ok */
+	if (ret < 0)
+		return ret;
+
+	src_start = image->arch.backup_src_start;
+	src_sz = image->arch.backup_src_sz;
+
+	/* Add backup segment. */
+	if (src_sz) {
+		ret = kexec_add_buffer(image, __va(src_start), src_sz, src_sz,
+					PAGE_SIZE, 0, -1, 0,
+					&image->arch.backup_load_addr);
+		if (ret)
+			return ret;
+	}

arch/x86/kernel/crash.c void native_machine_crash_shutdown(struct pt_regs *regs) · line 704

Quoted 10 lines
arch/x86/kernel/crash.c +10 -0
+
+	/* Prepare elf headers and add a segment */
+	ret = prepare_elf_headers(image, &elf_addr, &elf_sz);
+	if (ret)
+		return ret;
+
+	image->arch.elf_headers = elf_addr;
+	image->arch.elf_headers_sz = elf_sz;
+
+	ret = kexec_add_buffer(image, (char *)elf_addr, elf_sz, elf_sz,

For some reason, my compiler complains here:

arch/x86/kernel/crash.c: In function ‘load_crashdump_segments’:
arch/x86/kernel/crash.c:704:6: warning: ‘elf_sz’ may be used uninitialized in this function [-Wuninitialized] arch/x86/kernel/crash.c:704:24: warning: ‘elf_addr’ may be used uninitialized in this function [-Wuninitialized]

It is likely bogus, though.

...

Quoted 10 lines
arch/x86/kernel/crash.c +2 -2
-int kexec_setup_cmdline(struct boot_params *boot_params,
+int kexec_setup_cmdline(struct kimage *image, struct boot_params *boot_params,
 		unsigned long bootparams_load_addr,
 		unsigned long cmdline_offset, char *cmdline,
 		unsigned long cmdline_len)
 {
 	char *cmdline_ptr = ((char *)boot_params) + cmdline_offset;
-	unsigned long cmdline_ptr_phys;
+	unsigned long cmdline_ptr_phys, len;
 	uint32_t cmdline_low_32, cmdline_ext_32;

arch/x86/kernel/crash.c

Quoted 10 lines
arch/x86/kernel/crash.c +6 -0
 
 	memcpy(cmdline_ptr, cmdline, cmdline_len);
+	if (image->type == KEXEC_TYPE_CRASH) {
+		len = sprintf(cmdline_ptr + cmdline_len - 1,
+			" elfcorehdr=0x%lx", image->arch.elf_load_addr);
+		cmdline_len += len;
+	}
 	cmdline_ptr[cmdline_len - 1] = '\0';
 
+	pr_debug("Final command line is:%s\n", cmdline_ptr);

one space after ":"

The rest looks ok to me, but that doesn't mean a whole lot considering my very limited kexec knowledge.

Thanks.

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
#50 lore raw

Re: [PATCH 11/11] kexec: Support for Kexec on panic using new system call

On Fri, Feb 28, 2014 at 06:28:57PM +0100, Borislav Petkov wrote:

[..]

Quoted 9 lines

arch/x86/include/asm/kexec.h line 69

Quoted 3 lines
arch/x86/include/asm/kexec.h +3 -0
+/* Memory to backup during crash kdump */
+#define KEXEC_BACKUP_SRC_START	(0UL)
+#define KEXEC_BACKUP_SRC_END	(655360UL)	/* 640K */

I guess

#define KEXEC_BACKUP_SRC_END (640 * 1024UL)

should be more clear.

Will Change.

[..]

Quoted 10 lines

arch/x86/kernel/crash.c line 42

Quoted 8 lines
arch/x86/kernel/crash.c +8 -0
+/* Alignment required for elf header segment */
+#define ELF_CORE_HEADER_ALIGN   4096
+
+/* This primarily reprsents number of split ranges due to exclusion */
+#define CRASH_MAX_RANGES	16
+
+struct crash_mem_range {
+	unsigned long long start, end;

u64?

Ok, that's shorter. Can use that.

[..]

Quoted 4 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+/* Used while prepareing memory map entries for second kernel */

s/prepareing/preparing/

Yep typo. Will fix.

[..]

Quoted 17 lines
Quoted 4 lines
arch/x86/kernel/crash.c +4 -0
+static int get_gart_ranges_callback(u64 start, u64 end, void *arg)
+{
+	struct crash_elf_data *ced = arg;
+

arch/x86/kernel/crash.c void native_machine_crash_shutdown(struct pt_regs *regs) · line 208

Quoted 10 lines
arch/x86/kernel/crash.c +10 -0
+	ced->gart_start = start;
+	ced->gart_end = end;
+
+	/* Not expecting more than 1 gart aperture */
+	return 1;
+}
+
+
+/* Gather all the required information to prepare elf headers for ram regions */
+static int fill_up_ced(struct crash_elf_data *ced, struct kimage *image)

All other functions have nice, spelled out names but not this one :)

Quoted 1 line

Why not fill_up_crash_elf_data()?

Will change it.

Quoted 12 lines
Quoted 12 lines
arch/x86/kernel/crash.c +11 -0
+{
+	unsigned int nr_ranges = 0;
+
+	ced->image = image;
+
+	walk_system_ram_range(0, -1, &nr_ranges,
+				get_nr_ram_ranges_callback);
+
+	ced->max_nr_ranges = nr_ranges;
+
+	/*
Quoted 8 lines

arch/x86/kernel/crash.c void native_machine_crash_shutdown(struct pt_regs *regs) · line 225

Quoted 6 lines
arch/x86/kernel/crash.c +6 -0
+	 * We don't create ELF headers for GART aperture as an attempt
+	 * to dump this memory in second kernel leads to hang/crash.
+	 * If gart aperture is present, one needs to exclude that region
+	 * and that could lead to need of extra phdr.
+	 */
+

superfluous newline.

Will remove.

[..]

Quoted 42 lines
Quoted 29 lines
arch/x86/kernel/crash.c +29 -0
+int load_crashdump_segments(struct kimage *image)
+{
+	unsigned long src_start, src_sz;
+	unsigned long elf_addr, elf_sz;
+	int ret;
+
+	/*
+	 * Determine and load a segment for backup area. First 640K RAM
+	 * region is backup source
+	 */
+
+	ret = walk_system_ram_res(KEXEC_BACKUP_SRC_START, KEXEC_BACKUP_SRC_END,
+				image, determine_backup_region);
+
+	/* Zero or postive return values are ok */
+	if (ret < 0)
+		return ret;
+
+	src_start = image->arch.backup_src_start;
+	src_sz = image->arch.backup_src_sz;
+
+	/* Add backup segment. */
+	if (src_sz) {
+		ret = kexec_add_buffer(image, __va(src_start), src_sz, src_sz,
+					PAGE_SIZE, 0, -1, 0,
+					&image->arch.backup_load_addr);
+		if (ret)
+			return ret;
+	}

arch/x86/kernel/crash.c void native_machine_crash_shutdown(struct pt_regs *regs) · line 704

Quoted 10 lines
arch/x86/kernel/crash.c +10 -0
+
+	/* Prepare elf headers and add a segment */
+	ret = prepare_elf_headers(image, &elf_addr, &elf_sz);
+	if (ret)
+		return ret;
+
+	image->arch.elf_headers = elf_addr;
+	image->arch.elf_headers_sz = elf_sz;
+
+	ret = kexec_add_buffer(image, (char *)elf_addr, elf_sz, elf_sz,

For some reason, my compiler complains here:

Quoted 5 lines

arch/x86/kernel/crash.c: In function ‘load_crashdump_segments’:
arch/x86/kernel/crash.c:704:6: warning: ‘elf_sz’ may be used uninitialized in this function [-Wuninitialized] arch/x86/kernel/crash.c:704:24: warning: ‘elf_addr’ may be used uninitialized in this function [-Wuninitialized]

It is likely bogus, though.

Hmm..., I did not see these warnings with my setup. elf_addr and elf_sz will be initialized by prepare_elf_headers(). Not sure why compiler is complaining.

[..]

Quoted 3 lines

arch/x86/kernel/machine_kexec.c int kexec_setup_initrd(struct boot_params *boot_params, · line 58

Quoted 1 line
arch/x86/kernel/machine_kexec.c +1 -0
+	pr_debug("Final command line is:%s\n", cmdline_ptr);

one space after ":"

Ok. will do.

Quoted 3 lines

The rest looks ok to me, but that doesn't mean a whole lot considering my very limited kexec knowledge.

Thanks for review. We need many eyes on this patch set. I will make changes and post another version for review.

Thanks
Vivek

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

maintainer
#51 lore raw
In reply to Vivek Goyal

Hi Vivek,

Quoted 3 lines · Vivek Goyal

Hi

This is V2 of new system call patches. Previous version was posted here.

just a quick ping here, are you working on a v3? Anything I can help with?

Thanks.

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
#52 lore raw
Quoted 8 lines · Borislav Petkov

Hi Vivek,

Quoted 3 lines · Vivek Goyal

Hi

This is V2 of new system call patches. Previous version was posted here.

just a quick ping here, are you working on a v3? Anything I can help with?

Hi Boris,

Yes, I am working on V3. I have been doing some more work to complete the series. Specifically make the patches work with UEFI system and also wrote code for verifying PKCS7 signature of bzImage. Signature verification code I will post separately.

I am just battling a weared race condition where nested kdump seems to fail on UEFI system sometimes. Some checksum issue. Hopefully I will be able to resolve soon and repost patches for review.

Thanks
Vivek

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