kexec: A new system call to allow in kernel loading
v4 cover kexec: A new system call to allow in kernel loading
Hi,
This is V4 of the patchset. Previous versions were posted here.
V1: https://lkml.org/lkml/2013/11/20/540
V2: https://lkml.org/lkml/2014/1/27/331
V3: https://lkml.org/lkml/2014/6/3/432
Changes since v3:
- Took care of most of the review comments from V3.
- Stopped building purgatory for 32bit.
- If 64bit EFI is not enabled (EFI_64BIT) return error in kernel loading.
- If EFI OLD_MEMMAP is being used, do not do EFI setup and user space is expected to pass acpi_rsdp=<addr> param and boot second kernel in non efi mode.
- move machine_kexec.c code into kexec-bzimage64.c
- Renamed kexec-bzimage.c to kexec-bzimage64.c to reflect the fact that it is only 64bit bzimage loader.
This patch series is generated on top of 3.16.0-rc2.
This patch series does not do kernel signature verification yet. I plan to post another patch series for that. Now distributions are already signing PE/COFF bzImage with PKCS7 signature I plan to parse and verify those signatures.
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 hope these patches can be queued up for 3.17. Even without signature verification support, they provide new syscall functionality. But I wil leave it to maintainers to decide if they want signature verification support also be ready to merge before they merge this patchset.
Any feedback is welcome.
Thanks
Vivek
Vivek Goyal (15):
bin2c: Move bin2c in scripts/basic
kernel: Build bin2c based on config option CONFIG_BUILD_BIN2C
kexec: rename unusebale_pages to unusable_pages
kexec: Move segment verification code in a separate function
kexec: Use common function for kimage_normal_alloc() and
kimage_crash_alloc()
resource: Provide new functions to walk through resources
kexec: Make kexec_segment user buffer pointer a union
kexec: New syscall kexec_file_load() declaration
kexec: Implementation of new syscall kexec_file_load purgatory/sha256: Provide implementation of sha256 in purgaotory
context
purgatory: Core purgatory functionality
kexec: Load and Relocate purgatory at kernel load time
kexec-bzImage64: Support for loading bzImage using 64bit entry
kexec: Support for kexec on panic using new system call
kexec: Support kexec/kdump on EFI systems
arch/x86/Kbuild | 4 + arch/x86/Kconfig | 3 + arch/x86/Makefile | 8 + arch/x86/include/asm/crash.h | 9 + arch/x86/include/asm/kexec-bzimage64.h | 6 + arch/x86/include/asm/kexec.h | 40 + arch/x86/kernel/Makefile | 1 + arch/x86/kernel/crash.c | 563 ++++++++++++++ arch/x86/kernel/kexec-bzimage64.c | 532 +++++++++++++ arch/x86/kernel/machine_kexec_64.c | 228 ++++++ arch/x86/purgatory/Makefile | 30 + arch/x86/purgatory/entry64.S | 101 +++ arch/x86/purgatory/purgatory.c | 72 ++ arch/x86/purgatory/setup-x86_64.S | 58 ++ arch/x86/purgatory/sha256.c | 283 +++++++ arch/x86/purgatory/sha256.h | 22 + arch/x86/purgatory/stack.S | 19 + arch/x86/purgatory/string.c | 13 + arch/x86/syscalls/syscall_64.tbl | 1 + drivers/firmware/efi/runtime-map.c | 21 + include/linux/efi.h | 19 + include/linux/ioport.h | 6 + include/linux/kexec.h | 101 ++- include/linux/syscalls.h | 4 + include/uapi/linux/kexec.h | 11 + init/Kconfig | 5 + kernel/Makefile | 2 +- kernel/kexec.c | 1322 ++++++++++++++++++++++++++++---- kernel/resource.c | 101 ++- kernel/sys_ni.c | 1 + scripts/.gitignore | 1 - scripts/Makefile | 1 - scripts/basic/.gitignore | 1 + scripts/basic/Makefile | 1 + scripts/basic/bin2c.c | 35 + scripts/bin2c.c | 36 - 36 files changed, 3463 insertions(+), 198 deletions(-) create mode 100644 arch/x86/include/asm/crash.h create mode 100644 arch/x86/include/asm/kexec-bzimage64.h create mode 100644 arch/x86/kernel/kexec-bzimage64.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_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 arch/x86/purgatory/string.c create mode 100644 scripts/basic/bin2c.c delete mode 100644 scripts/bin2c.c
Signature · 7 lines
1.9.0 _______________________________________________ kexec mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kexec
PATCH v4 1/15 bin2c: Move bin2c in scripts/basic
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/.gitignore | 1 - scripts/Makefile | 1 - scripts/basic/.gitignore | 1 + scripts/basic/Makefile | 1 + scripts/basic/bin2c.c | 35 +++++++++++++++++++++++++++++++++++ scripts/bin2c.c | 36 ------------------------------------ 7 files changed, 38 insertions(+), 39 deletions(-) create mode 100644 scripts/basic/bin2c.c delete mode 100644 scripts/bin2c.c
kernel/Makefile +1 -1
@@ -105,7 +105,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/.gitignore +0 -1
@@ -4,7 +4,6 @@ conmakehash kallsyms pnmtologo -bin2c unifdef ihex2fw recordmcount
scripts/Makefile +0 -1
@@ -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/.gitignore +1 -0
@@ -1 +1,2 @@ fixdep +bin2c
scripts/basic/Makefile +1 -0
@@ -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 +35 -0
@@ -0,0 +1,35 @@ +/* +* 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
@@ -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.9.0 _______________________________________________ kexec mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kexec
PATCH v4 2/15 kernel: Build bin2c based on config option CONFIG_BUILD_BIN2C
In reply to Vivek Goyalcurrently 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
@@ -1576,6 +1576,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
@@ -783,8 +783,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
@@ -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.9.0 _______________________________________________ kexec mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kexec
PATCH v4 3/15 kexec: rename unusebale_pages to unusable_pages
In reply to Vivek GoyalLet's use the more common "unusable".
This patch was originally written and posted by Boris. I am including it in this patch series.
--- include/linux/kexec.h | 2 +- kernel/kexec.c | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-)
include/linux/kexec.h +1 -1
@@ -100,7 +100,7 @@ struct kimage {struct list_head control_pages;struct list_head dest_pages; -struct list_head unuseable_pages; +struct list_head unusable_pages;/* Address of next control page to allocate for crash kernels. */ unsigned long control_page;
kernel/kexec.c +3 -3
@@ -153,7 +153,7 @@ static int do_kimage_alloc(struct kimage **rimage, unsigned long entry,INIT_LIST_HEAD (&image->dest_pages);/* Initialize the list of unusable pages */ -INIT_LIST_HEAD (&image->unuseable_pages); +INIT_LIST_HEAD (&image->unusable_pages);/* Read in the segments */ image->nr_segments = nr_segments;
@@ -608,7 +608,7 @@ static void kimage_free_extra_pages(struct kimage *image)kimage_free_page_list (&image->dest_pages);/* Walk through and free any unusable pages I have cached */ -kimage_free_page_list (&image->unuseable_pages); +kimage_free_page_list (&image->unusable_pages); }static void kimage_terminate (struct kimage *image)
@@ -731,7 +731,7 @@ static struct page *kimage_alloc_page(struct kimage *image,/* If the page cannot be used file it away */ if (page_to_pfn (page) > (KEXEC_SOURCE_MEMORY_LIMIT >>PAGE_SHIFT )) { -list_add (&page->lru, &image->unuseable_pages); +list_add (&page->lru, &image->unusable_pages);continue ; } addr =page_to_pfn (page) <<PAGE_SHIFT ;
Signature · 7 lines
1.9.0 _______________________________________________ kexec mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kexec
PATCH v4 4/15 kexec: Move segment verification code in a separate function
In reply to Vivek GoyalPreviously 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, 100 insertions(+), 82 deletions(-)
kernel/kexec.c +100 -82
@@ -124,45 +124,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->unusable_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
@@ -184,9 +166,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.
@@ -207,7 +189,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; } }
@@ -219,18 +201,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->unusable_pages); + +return image; }static void kimage_free_page_list (struct list_head *list);
@@ -243,10 +268,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
@@ -258,22 +292,21 @@ static int kimage_normal_alloc(struct kimage **rimage, unsigned long entry, get_order(KEXEC_CONTROL_PAGE_SIZE));if (!image->control_code_page) {pr_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) {pr_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; }
@@ -283,19 +316,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.
@@ -303,25 +334,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
@@ -333,15 +352,14 @@ static int kimage_crash_alloc(struct kimage **rimage, unsigned long entry, get_order(KEXEC_CONTROL_PAGE_SIZE));if (!image->control_code_page) {pr_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.9.0 _______________________________________________ kexec mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kexec
PATCH v4 5/15 kexec: Use common function for kimage_normal_alloc() and kimage_crash_alloc()
In reply to Vivek Goyalkimage_normal_alloc() and kimage_crash_alloc() are doing lot of similar things and differ only little. So instead of having two separate functions create a common function kimage_alloc_init() and pass it the "flags" argument which tells whether it is normal kexec or kexec_on_panic. And this function should be able to deal with both the cases.
This consolidation also helps later where we can use a common function kimage_file_alloc_init() to handle normal and crash cases for new file based kexec syscall.
--- kernel/kexec.c | 105 +++++++++++++++++++-------------------------------------- 1 file changed, 34 insertions(+), 71 deletions(-)
kernel/kexec.c +34 -71
@@ -260,12 +260,20 @@ static struct kimage *do_kimage_alloc_init(void)static void kimage_free_page_list (struct list_head *list); -static int kimage_normal_alloc (struct kimage **rimage,unsigned long entry, -unsigned long nr_segments, -struct kexec_segment __user *segments) +static int kimage_alloc_init (struct kimage **rimage,unsigned long entry, +unsigned long nr_segments, +struct kexec_segment __user *segments, +unsigned long flags) { -int result; +int ret;struct kimage *image; +bool kexec_on_panic = flags &KEXEC_ON_CRASH ; + +if (kexec_on_panic) { +/* Verify we have a valid entry point */ +if ((entry < crashk_res.start) || (entry > crashk_res.end)) +return -EADDRNOTAVAIL ; + }/* Allocate and initialize a controlling structure */ image =do_kimage_alloc_init ();
@@ -274,20 +282,26 @@ static int kimage_normal_alloc(struct kimage **rimage, unsigned long entry, image->start = entry; - result =copy_user_segment_list (image, nr_segments, segments); -if (result) + ret =copy_user_segment_list (image, nr_segments, segments); +if (ret) goto out_free_image; - result =sanity_check_segment_list (image); -if (result) + ret =sanity_check_segment_list (image); +if (ret) goto out_free_image; +/* Enable the special crash kernel control page allocation policy. */ +if (kexec_on_panic) { + image->control_page = crashk_res.start; + image->type =KEXEC_TYPE_CRASH ; + } +/* * Find a location for the control code buffer, and add it * the vector of segments so that it's pages will also be * counted as destination pages. */ - result = -ENOMEM ; + ret = -ENOMEM ; image->control_code_page =kimage_alloc_control_pages (image,get_order (KEXEC_CONTROL_PAGE_SIZE ));if (!image->control_code_page) {
@@ -295,10 +309,12 @@ static int kimage_normal_alloc(struct kimage **rimage, unsigned long entry, goto out_free_image; } - image->swap_page =kimage_alloc_control_pages (image,0 ); -if (!image->swap_page) { -pr_err ("Could not allocate swap buffer\n" ); - goto out_free_control_pages; +if (!kexec_on_panic) { + image->swap_page =kimage_alloc_control_pages (image,0 ); +if (!image->swap_page) { +pr_err ("Could not allocate swap buffer\n" ); + goto out_free_control_pages; + } } *rimage = image;
@@ -307,60 +323,7 @@ out_free_control_pages:kimage_free_page_list (&image->control_pages); out_free_image:kfree (image); -return result; -} - -static int kimage_crash_alloc (struct kimage **rimage,unsigned long entry, -unsigned long nr_segments, -struct kexec_segment __user *segments) -{ -int result; -struct kimage *image; - -/* Verify we have a valid entry point */ -if ((entry < crashk_res.start) || (entry > crashk_res.end)) -return -EADDRNOTAVAIL ; - -/* Allocate and initialize a controlling structure */ - image =do_kimage_alloc_init (); -if (!image) -return -ENOMEM ; - - image->start = entry; - -/* Enable the special crash kernel control page -* allocation policy. -*/ - image->control_page = crashk_res.start; - image->type =KEXEC_TYPE_CRASH ; - - result =copy_user_segment_list (image, nr_segments, segments); -if (result) - 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 -* the vector of segments so that it's pages will also be -* counted as destination pages. -*/ - result = -ENOMEM ; - image->control_code_page =kimage_alloc_control_pages (image, -get_order (KEXEC_CONTROL_PAGE_SIZE )); -if (!image->control_code_page) { -pr_err ("Could not allocate control_code_buffer\n" ); - goto out_free_image; - } - - *rimage = image; -return 0 ; - -out_free_image: -kfree (image); -return result; +return ret; } static int kimage_is_destination_range(struct kimage *image,
@@ -1003,16 +966,16 @@ SYSCALL_DEFINE4(kexec_load, unsigned long, entry, unsigned long, nr_segments,/* Loading another kernel to reboot into */ if ((flags &KEXEC_ON_CRASH ) ==0 ) - result =kimage_normal_alloc (&image, entry, - nr_segments, segments); + result =kimage_alloc_init (&image, entry, nr_segments, + segments, flags);/* Loading another kernel to switch to if this one crashes */ else if (flags &KEXEC_ON_CRASH ) {/* Free any current crash dump kernel before * we corrupt it. */ kimage_free (xchg (&kexec_crash_image,NULL )); - result =kimage_crash_alloc (&image, entry, - nr_segments, segments); + result =kimage_alloc_init (&image, entry, nr_segments, + segments, flags);crash_map_reserved_pages (); }if (result)
Signature · 7 lines
1.9.0 _______________________________________________ kexec mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kexec
PATCH v4 6/15 resource: Provide new functions to walk through resources
In reply to Vivek GoyalI have added two more functions to walk through resources.
Currently 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_ram_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_iomem_res() which walks through all /proc/iomem resources and returns matches as asked by caller. Caller can specify "name" of resource, start and end and flags.
Got rid of find_next_system_ram_res() and instead implemented more generic find_next_iomem_res() which can be used to traverse top level children only based on an argument.
--- include/linux/ioport.h | 6 +++ kernel/resource.c | 101 ++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 98 insertions(+), 9 deletions(-)
include/linux/ioport.h +6 -0
@@ -237,6 +237,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_iomem_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 +92 -9
151 lines not on this page · raw message
Signature · 7 lines
1.9.0 _______________________________________________ kexec mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kexec
PATCH v4 7/15 kexec: Make kexec_segment user buffer pointer a union
In reply to Vivek GoyalSo 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
@@ -69,7 +69,18 @@ typedef unsigned long kimage_entry_t;#define IND_SOURCE 0x8struct 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.9.0 _______________________________________________ kexec mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kexec
PATCH v4 8/15 kexec: New syscall kexec_file_load() declaration
In reply to Vivek GoyalThis is the new syscall kexec_file_load() declaration/interface. I have reserved the syscall number only for x86_64 so far. Other architectures (including i386) can reserve syscall number when they enable the support for this new syscall.
--- arch/x86/syscalls/syscall_64.tbl | 1 + include/linux/syscalls.h | 4 ++++ kernel/kexec.c | 7 +++++++ kernel/sys_ni.c | 1 + 4 files changed, 13 insertions(+)
arch/x86/syscalls/syscall_64.tbl +1 -0
@@ -323,6 +323,7 @@ 314 common sched_setattr sys_sched_setattr 315 common sched_getattr sys_sched_getattr 316 common renameat2 sys_renameat2 +317 common kexec_file_load sys_kexec_file_load # # x32-specific system call numbers start at 512 to avoid cache impact
include/linux/syscalls.h +4 -0
@@ -317,6 +317,10 @@ 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, +unsigned long cmdline_len, +const char __user *cmdline_ptr, +unsigned long flags);asmlinkage long sys_exit (int error_code);asmlinkage long sys_exit_group (int error_code);
kernel/kexec.c +7 -0
@@ -1057,6 +1057,13 @@ COMPAT_SYSCALL_DEFINE4(kexec_load, compat_ulong_t, entry, }#endif +SYSCALL_DEFINE5 (kexec_file_load, int, kernel_fd, int, initrd_fd, + unsigned long, cmdline_len, const char __user *, cmdline_ptr, + unsigned long, flags) +{ +return -ENOSYS ; +} +void crash_kexec (struct pt_regs *regs) {/* Take the kexec_mutex here to prevent sys_kexec_load
kernel/sys_ni.c +1 -0
@@ -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 · 1 line
1.9.0
Re: [PATCH 08/15] kexec: New syscall kexec_file_load() declaration
Quoted 7 lines · Vivek Goyal
This is the new syscall
kexec_file_load()declaration/interface. I have reserved the syscall number only forx86_64so far. Other architectures (including i386) can reserve syscall number when they enable the support for this new syscall.Signed-off-by Vivek Goyal <[email protected]>
Hi Michael,
As per feedback last time, I enhanced the existing man page to include details of this new syscall. Here is the patch.
Thanks
Vivek
Subject: kexec_file_load() syscall man page
We already have man page for kexec_load() syscall. This patch adds details of kexec_file_load() to same man page.
--- man2/kexec_file_load.2 | 1 man2/kexec_load.2 | 55 +++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 50 insertions(+), 6 deletions(-)
man2/kexec_file_load.2 new file +1 -0
@@ -0,0 +1 @@ +.so man2/kexec_load.2
man2/kexec_load.2 +49 -6
83 lines not on this page · raw message
Re: [PATCH 08/15] kexec: New syscall kexec_file_load() declaration
man2/kexec_load.2 struct kexec_segment { · line 155
Quoted 13 lines · Vivek Goyal
Quoted 7 lines · Vivek Goyal
This is the new syscall
kexec_file_load()declaration/interface. I have reserved the syscall number only forx86_64so far. Other architectures (including i386) can reserve syscall number when they enable the support for this new syscall.Signed-off-by Vivek Goyal <[email protected]>+.BR
KEXEC_FILE_NO_INITRAMFSman2/kexec_load.2 +3
-0+Loading initrd/initramfs is optional. Specify this flag if no initramfs +is being loaded. If this flag is set, kernel will ignore the value passed +in
This seems pointless. Why not just pass -1 for initrd_fd to indicate that no initrd is needed?
--Andy
Re: [PATCH 08/15] kexec: New syscall kexec_file_load() declaration
Quoted 10 lines · Andy Lutomirski
Quoted 9 lines · Vivek Goyal
Quoted 7 lines · Vivek Goyal
This is the new syscall
kexec_file_load()declaration/interface. I have reserved the syscall number only forx86_64so far. Other architectures (including i386) can reserve syscall number when they enable the support for this new syscall.Signed-off-by Vivek Goyal <[email protected]>
Quoted 7 lines
man2/kexec_load.2 struct kexec_segment { · line 155
Quoted 4 lines
+.BR
KEXEC_FILE_NO_INITRAMFSman2/kexec_load.2 +3
-0+Loading initrd/initramfs is optional. Specify this flag if no initramfs +is being loaded. If this flag is set, kernel will ignore the value passed +inThis seems pointless. Why not just pass -1 for
initrd_fdto indicate that no initrd is needed?
I was not sure whether negative fd should be treated as error and system call should fail or it should be treated as user does not want to load initrd and system call succeeds.
I was concerned about the cases where application does an fd = open(), operation fails and fd contains -1. Caller does not check fd and passed it to kexec system call.
I thought that in such cases we should error out saying initrd fd is not valid. Instead of continuing and loading kernel without initrd. A user might be surprised.
This is little defensive programming. But I am open to change it if the perception is that above is not a valid concern.
Thanks
Vivek
Re: [PATCH 08/15] kexec: New syscall kexec_file_load() declaration
Quoted 27 lines · Vivek Goyal
Quoted 10 lines · Andy Lutomirski
Quoted 9 lines · Vivek Goyal
Quoted 7 lines · Vivek Goyal
This is the new syscall
kexec_file_load()declaration/interface. I have reserved the syscall number only forx86_64so far. Other architectures (including i386) can reserve syscall number when they enable the support for this new syscall.Signed-off-by Vivek Goyal <[email protected]>Quoted 7 lines
man2/kexec_load.2 struct kexec_segment { · line 155
Quoted 4 lines
+.BR
KEXEC_FILE_NO_INITRAMFSman2/kexec_load.2 +3
-0+Loading initrd/initramfs is optional. Specify this flag if no initramfs +is being loaded. If this flag is set, kernel will ignore the value passed +inThis seems pointless. Why not just pass -1 for
initrd_fdto indicate that no initrd is needed?I was not sure whether negative fd should be treated as error and system call should fail or it should be treated as user does not want to load initrd and system call succeeds.
I was concerned about the cases where application does an fd =
open(), operation fails and fd contains -1. Caller does not check fd and passed it to kexec system call.
Quoted 6 lines
I thought that in such cases we should error out saying initrd fd is not valid. Instead of continuing and loading kernel without initrd. A user might be surprised.
This is little defensive programming. But I am open to change it if the perception is that above is not a valid concern.
Your logic for using a flag rather than -1 sounds reasonable to me. The nearest precedent I can think of offhand is mmap(), which also takes a file descriptor argument for some use cases. However, if MAP_ANONYMOUS is specified, no file descriptor ir required. The treatment of the 'fd' argument in that case depends on the system. On Linux, the fd argument is just ignored. However, many other systems require 'fd' to be negative when MAP_ANONYMOUS is specified; one presumes as a kind of safety check.
Cheers,
Michael
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
PATCH v4 9/15 kexec: Implementation of new syscall kexec_file_load
In reply to Vivek GoyalPrevious patch provided the interface definition and this patch prvides implementation of new syscall.
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 | 45 ++++ include/linux/kexec.h | 53 ++++ include/uapi/linux/kexec.h | 11 + kernel/kexec.c | 478 ++++++++++++++++++++++++++++++++++++- 4 files changed, 582 insertions(+), 5 deletions(-)
arch/x86/kernel/machine_kexec_64.c +45 -0
@@ -22,6 +22,10 @@#include <asm/mmu_context.h> #include <asm/debugreg.h> +static struct kexec_file_ops *kexec_file_loaders[] = { +NULL , +}; +static void free_transition_pgtable (struct kimage *image) {free_page ((unsigned long )image->arch.pud);
@@ -283,3 +287,44 @@ void arch_crash_save_vmcoreinfo(void) (unsigned long)&_text - __START_KERNEL); } +/* 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 ; +struct kexec_file_ops *fops; + +for (i =0 ; i <ARRAY_SIZE (kexec_file_loaders); i++) { + fops = kexec_file_loaders[i]; +if (!fops || !fops->probe) +continue ; + + ret = fops->probe (buf, buf_len); +if (!ret) { + image->fops = fops; +return ret; + } + } + +return ret; +} + +void *arch_kexec_kernel_image_load (struct kimage *image) +{ +if (!image->fops || !image->fops->load) +return ERR_PTR (-ENOEXEC ); + +return image->fops->load (image, image->kernel_buf, + image->kernel_buf_len, image->initrd_buf, + image->initrd_buf_len, image->cmdline_buf, + image->cmdline_buf_len); +} + +int arch_kimage_file_post_load_cleanup (struct kimage *image) +{ +if (!image->fops || !image->fops->cleanup) +return 0 ; + +return image->fops->cleanup (image); +}
include/linux/kexec.h +53 -0
78 lines not on this page · raw message
include/uapi/linux/kexec.h +11 -0
@@ -13,6 +13,17 @@#define KEXEC_PRESERVE_CONTEXT 0x00000002#define KEXEC_ARCH_MASK 0xffff0000 +/* +* Kexec file load interface flags. +* KEXEC_FILE_UNLOAD : Unload already loaded kexec/kdump image. +* KEXEC_FILE_ON_CRASH : Load/unload operation belongs to kdump image. +* KEXEC_FILE_NO_INITRAMFS : No initramfs is being loaded. Ignore the initrd +* fd field. +*/ +#define KEXEC_FILE_UNLOAD 0x00000001 +#define KEXEC_FILE_ON_CRASH 0x00000002 +#define KEXEC_FILE_NO_INITRAMFS 0x00000004 +/* These values match the ELF architecture values. * Unless there is a good reason that should continue to be the case. */
kernel/kexec.c +473 -5
528 lines not on this page · raw message
Signature · 7 lines
1.9.0 _______________________________________________ kexec mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kexec
Andrew Morton
maintainerRe: [PATCH 09/15] kexec: Implementation of new syscall kexec_file_load
Quoted 25 lines · On Thu, 26 Jun 2014 16:33:38 -0400 Vivek Goyal <[email protected]> wrote:
Previous patch provided the interface definition and this patch prvides implementation of new syscall.
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.
...
kernel/kexec.c +2
-0@@ -6,6 +6,8 @@* Version 2. See the file COPYING for more details. */ +#define pr_fmt (fmt) "kexec: " fmt +#include <linux/capability.h> #include <linux/mm.h> #include <linux/file.h>
kernel/kexec.c out_free_image: · line 334
Quoted 7 lines
kernel/kexec.c +4
-0@@ -326,6 +328,215 @@ out_free_image:return ret; } +static int copy_file_from_fd (int fd,void **buf,unsigned long *buf_len) +{ +struct fd f =fdget (fd); +int ret =0 ;
unneeded initialisation.
Quoted 30 lines
kernel/kexec.c +30
-0+struct kstat stat; +loff_t pos; +ssize_t bytes =0 ; + +if (!f.file) +return -EBADF ; + + ret =vfs_getattr (&f.file->f_path, &stat); +if (ret) + goto out; + +if (stat.size >INT_MAX ) { + ret = -EFBIG ; + goto out; + } + +/* Don't hand 0 to vmalloc, it whines. */ +if (stat.size ==0 ) { + ret = -EINVAL ; + goto out; + } + + *buf =vmalloc (stat.size); +if (!*buf) { + ret = -ENOMEM ; + goto out; + } + + pos =0 ; +while (pos < stat.size) {
kernel/kexec.c out_free_image: · line 374
Quoted 10 lines
kernel/kexec.c +10
-0+ bytes =kernel_read (f.file, pos, (char *)(*buf) + pos, + stat.size - pos); +if (bytes <0 ) { +vfree (*buf); + ret = bytes; + goto out; + } + +if (bytes ==0 ) +break ;
Here we can get a short read: (pos < stat.size). Seems to me that it is risky to return this result to the caller as if all is well.
Quoted 11 lines
kernel/kexec.c +8
-0+ pos += bytes; + } + + *buf_len = pos; +out: +fdput (f); +return ret; +}
Re: [PATCH 09/15] kexec: Implementation of new syscall kexec_file_load
On Thu, Jun 26, 2014 at 01:58:26PM -0700, Andrew Morton wrote:
[..]
Quoted 6 lines
Quoted 6 lines
(quoted diff) +6
-0+ while (pos < stat.size) { + bytes = kernel_read(f.file, pos, (char *)(*buf) + pos, + stat.size - pos); + if (bytes < 0) { + vfree(*buf); + ret = bytes;
Quoted 8 lines
Review
Quoted 5 lines
(quoted diff) +5
-0+ goto out; + } + + if (bytes == 0) + break;Here we can get a short read: (pos < stat.size). Seems to me that it is risky to return this result to the caller as if all is well.
Hi Andrew,
That's a good point. Please find attached the patch which fixes both the issues.
Thanks
Vivek
Subject: kexec: Return error if file bytes are less then file size
If number of bytes read from file are not same as file size, return error.
--- kernel/kexec.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
kernel/kexec.c +7 -1
@@ -343,7 +343,7 @@ out_free_image:static int copy_file_from_fd (int fd,void **buf,unsigned long *buf_len) {struct fd f =fdget (fd); -int ret =0 ; +int ret;struct kstat stat;loff_t pos;ssize_t bytes =0 ;
@@ -387,6 +387,12 @@ static int copy_file_from_fd(int fd, voi pos += bytes; } +if (pos != stat.size) { + ret = -EBADF ; +vfree (*buf); + goto out; + } + *buf_len = pos; out:fdput (f);
_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
Re: [PATCH 09/15] kexec: Implementation of new syscall kexec_file_load
Quoted 18 lines · Vivek Goyal
On Thu, Jun 26, 2014 at 01:58:26PM -0700, Andrew Morton wrote:
[..]
Quoted 6 lines
Quoted 6 lines
(quoted diff) +6
-0+ while (pos < stat.size) { + bytes = kernel_read(f.file, pos, (char *)(*buf) + pos, + stat.size - pos); + if (bytes < 0) { + vfree(*buf); + ret = bytes;Quoted 8 lines
kernel/kexec.c static int copy_file_from_fd(int fd, voi
Quoted 5 lines
kernel/kexec.c +5
-0+ goto out; + } + +if (bytes ==0 ) +break ;Here we can get a short read: (pos < stat.size). Seems to me that it is risky to return this result to the caller as if all is well.
Quoted 10 lines
Hi Andrew,
That's a good point. Please find attached the patch which fixes both the issues.
Thanks
Vivek
Hi Andrew,
Based on your feedback, I wrote following patch. Does it look good to you. If yes, can you please include this one too. Do let me know if you want me to post it separately.
Thanks
Vivek
Quoted 25 lines
Subject: kexec: Return error if file bytes are less then file size
If number of bytes read from file are not same as file size, return error.
Signed-off-by Vivek Goyal <[email protected]>--- kernel/kexec.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)Index:
linux-2.6/kernel/kexec.c
===================================================================kernel/kexec.c +1
-1@@ -343,7 +343,7 @@ out_free_image:static int copy_file_from_fd (int fd,void **buf,unsigned long *buf_len) {struct fd f =fdget (fd); -int ret =0 ; +int ret;struct kstat stat;loff_t pos;ssize_t bytes =0 ;@@ -387,6 +387,12 @@ static int copy_file_from_fd(int fd, voi pos += bytes; }
kernel/kexec.c static int copy_file_from_fd(int fd, voi · line 398
Quoted 10 lines
kernel/kexec.c +6
-0+if (pos != stat.size) { + ret = -EBADF ; +vfree (*buf); + goto out; + } + *buf_len = pos; out:fdput (f);
_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
PATCH v4 10/15 purgatory/sha256: Provide implementation of sha256 in purgaotory context
In reply to Vivek GoyalNext two patches provide code for purgatory. This is a code which does not link against the kernel and runs stand alone. This code runs between two kernels. One of the primary purpose of this code is to verify the digest of newly loaded kernel and making sure it matches the digest computed at kernel load time.
We use sha256 for calculating digest of kexec segmetns. Purgatory can't use stanard crypto API as that API is not available in purgatory context.
Hence, I have copied code from crypto/sha256_generic.c and compiled it with purgaotry code so that it could be used. I could not #include sha256_generic.c file here as some of the function signature requiered little tweaking. Original functions work with crypto API but these ones don't
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.
This patch does not enable compiling of this code. That happens in next patch. I wanted to highlight this change in a separate patch for easy review.
--- arch/x86/purgatory/sha256.c | 283 ++++++++++++++++++++++++++++++++++++++++++++ arch/x86/purgatory/sha256.h | 22 ++++ 2 files changed, 305 insertions(+) create mode 100644 arch/x86/purgatory/sha256.c create mode 100644 arch/x86/purgatory/sha256.h
arch/x86/purgatory/sha256.c new file +283 -0
@@ -0,0 +1,283 @@ +/* +* SHA-256, as specified in +* http://csrc.nist.gov/groups/STM/cavp/documents/shs/sha256-384-512.pdf +* +* SHA-256 code by Jean-Luc Cooke <[email protected]>. +* +* Copyright (c) Jean-Luc Cooke <[email protected]> +* Copyright (c) Andrew McDonald <[email protected]> +* Copyright (c) 2002 James Morris <[email protected]> +* 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; either version 2 of the License, or (at your option) +* any later version. +*/ + +#include <linux/bitops.h> +#include <asm/byteorder.h> +#include "sha256.h" +#include "../boot/string.h" + +static inline u32 Ch (u32 x,u32 y,u32 z) +{ +return z ^ (x & (y ^ z)); +} + +static inline u32 Maj (u32 x,u32 y,u32 z) +{ +return (x & y) | (z & (x | y)); +} + +#define e0 (x) (ror32(x, 2) ^ ror32(x, 13) ^ ror32(x, 22)) +#define e1 (x) (ror32(x, 6) ^ ror32(x, 11) ^ ror32(x, 25)) +#define s0 (x) (ror32(x, 7) ^ ror32(x, 18) ^ (x >> 3)) +#define s1 (x) (ror32(x, 17) ^ ror32(x, 19) ^ (x >> 10)) + +static inline void LOAD_OP (int I ,u32 *W ,const u8 *input) +{ +W [I ] =__be32_to_cpu (((__be32 *)(input))[I ]); +} + +static inline void BLEND_OP (int I ,u32 *W ) +{ +W [I ] =s1 (W [I -2 ]) +W [I -7 ] +s0 (W [I -15 ]) +W [I -16 ]; +} + +static void sha256_transform (u32 *state,const u8 *input) +{ +u32 a, b, c, d, e, f, g, h, t1, t2; +u32 W [64 ]; +int i; + +/* load the input */ +for (i =0 ; i <16 ; i++) +LOAD_OP (i,W , input); + +/* now blend */ +for (i =16 ; i <64 ; i++) +BLEND_OP (i,W ); + +/* load the state into our registers */ + a = state[0 ]; b = state[1 ]; c = state[2 ]; d = state[3 ]; + e = state[4 ]; f = state[5 ]; g = state[6 ]; h = state[7 ]; + +/* now iterate */ + t1 = h +e1 (e) +Ch (e, f, g) +0x428a2f98 +W [0 ]; + t2 =e0 (a) +Maj (a, b, c); d += t1; h = t1 + t2; + t1 = g +e1 (d) +Ch (d, e, f) +0x71374491 +W [1 ]; + t2 =e0 (h) +Maj (h, a, b); c += t1; g = t1 + t2; + t1 = f +e1 (c) +Ch (c, d, e) +0xb5c0fbcf +W [2 ]; + t2 =e0 (g) +Maj (g, h, a); b += t1; f = t1 + t2; + t1 = e +e1 (b) +Ch (b, c, d) +0xe9b5dba5 +W [3 ]; + t2 =e0 (f) +Maj (f, g, h); a += t1; e = t1 + t2; + t1 = d +e1 (a) +Ch (a, b, c) +0x3956c25b +W [4 ]; + t2 =e0 (e) +Maj (e, f, g); h += t1; d = t1 + t2; + t1 = c +e1 (h) +Ch (h, a, b) +0x59f111f1 +W [5 ]; + t2 =e0 (d) +Maj (d, e, f); g += t1; c = t1 + t2; + t1 = b +e1 (g) +Ch (g, h, a) +0x923f82a4 +W [6 ]; + t2 =e0 (c) +Maj (c, d, e); f += t1; b = t1 + t2; + t1 = a +e1 (f) +Ch (f, g, h) +0xab1c5ed5 +W [7 ]; + t2 =e0 (b) +Maj (b, c, d); e += t1; a = t1 + t2; + + t1 = h +e1 (e) +Ch (e, f, g) +0xd807aa98 +W [8 ]; + t2 =e0 (a) +Maj (a, b, c); d += t1; h = t1 + t2; + t1 = g +e1 (d) +Ch (d, e, f) +0x12835b01 +W [9 ]; + t2 =e0 (h) +Maj (h, a, b); c += t1; g = t1 + t2; + t1 = f +e1 (c) +Ch (c, d, e) +0x243185be +W [10 ]; + t2 =e0 (g) +Maj (g, h, a); b += t1; f = t1 + t2; + t1 = e +e1 (b) +Ch (b, c, d) +0x550c7dc3 +W [11 ]; + t2 =e0 (f) +Maj (f, g, h); a += t1; e = t1 + t2; + t1 = d +e1 (a) +Ch (a, b, c) +0x72be5d74 +W [12 ]; + t2 =e0 (e) +Maj (e, f, g); h += t1; d = t1 + t2; + t1 = c +e1 (h) +Ch (h, a, b) +0x80deb1fe +W [13 ]; + t2 =e0 (d) +Maj (d, e, f); g += t1; c = t1 + t2; + t1 = b +e1 (g) +Ch (g, h, a) +0x9bdc06a7 +W [14 ]; + t2 =e0 (c) +Maj (c, d, e); f += t1; b = t1 + t2; + t1 = a +e1 (f) +Ch (f, g, h) +0xc19bf174 +W [15 ]; + t2 =e0 (b) +Maj (b, c, d); e += t1; a = t1+t2; + + t1 = h +e1 (e) +Ch (e, f, g) +0xe49b69c1 +W [16 ]; + t2 =e0 (a) +Maj (a, b, c); d += t1; h = t1+t2; + t1 = g +e1 (d) +Ch (d, e, f) +0xefbe4786 +W [17 ]; + t2 =e0 (h) +Maj (h, a, b); c += t1; g = t1+t2; + t1 = f +e1 (c) +Ch (c, d, e) +0x0fc19dc6 +W [18 ]; + t2 =e0 (g) +Maj (g, h, a); b += t1; f = t1+t2; + t1 = e +e1 (b) +Ch (b, c, d) +0x240ca1cc +W [19 ]; + t2 =e0 (f) +Maj (f, g, h); a += t1; e = t1+t2; + t1 = d +e1 (a) +Ch (a, b, c) +0x2de92c6f +W [20 ]; + t2 =e0 (e) +Maj (e, f, g); h += t1; d = t1+t2; + t1 = c +e1 (h) +Ch (h, a, b) +0x4a7484aa +W [21 ]; + t2 =e0 (d) +Maj (d, e, f); g += t1; c = t1+t2; + t1 = b +e1 (g) +Ch (g, h, a) +0x5cb0a9dc +W [22 ]; + t2 =e0 (c) +Maj (c, d, e); f += t1; b = t1+t2; + t1 = a +e1 (f) +Ch (f, g, h) +0x76f988da +W [23 ]; + t2 =e0 (b) +Maj (b, c, d); e += t1; a = t1+t2; + + t1 = h +e1 (e) +Ch (e, f, g) +0x983e5152 +W [24 ]; + t2 =e0 (a) +Maj (a, b, c); d += t1; h = t1+t2; + t1 = g +e1 (d) +Ch (d, e, f) +0xa831c66d +W [25 ]; + t2 =e0 (h) +Maj (h, a, b); c += t1; g = t1+t2; + t1 = f +e1 (c) +Ch (c, d, e) +0xb00327c8 +W [26 ]; + t2 =e0 (g) +Maj (g, h, a); b += t1; f = t1+t2; + t1 = e +e1 (b) +Ch (b, c, d) +0xbf597fc7 +W [27 ]; + t2 =e0 (f) +Maj (f, g, h); a += t1; e = t1+t2; + t1 = d +e1 (a) +Ch (a, b, c) +0xc6e00bf3 +W [28 ]; + t2 =e0 (e) +Maj (e, f, g); h += t1; d = t1+t2; + t1 = c +e1 (h) +Ch (h, a, b) +0xd5a79147 +W [29 ]; + t2 =e0 (d) +Maj (d, e, f); g += t1; c = t1+t2; + t1 = b +e1 (g) +Ch (g, h, a) +0x06ca6351 +W [30 ]; + t2 =e0 (c) +Maj (c, d, e); f += t1; b = t1+t2; + t1 = a +e1 (f) +Ch (f, g, h) +0x14292967 +W [31 ]; + t2 =e0 (b) +Maj (b, c, d); e += t1; a = t1+t2; + + t1 = h +e1 (e) +Ch (e, f, g) +0x27b70a85 +W [32 ]; + t2 =e0 (a) +Maj (a, b, c); d += t1; h = t1+t2; + t1 = g +e1 (d) +Ch (d, e, f) +0x2e1b2138 +W [33 ]; + t2 =e0 (h) +Maj (h, a, b); c += t1; g = t1+t2; + t1 = f +e1 (c) +Ch (c, d, e) +0x4d2c6dfc +W [34 ]; + t2 =e0 (g) +Maj (g, h, a); b += t1; f = t1+t2; + t1 = e +e1 (b) +Ch (b, c, d) +0x53380d13 +W [35 ]; + t2 =e0 (f) +Maj (f, g, h); a += t1; e = t1+t2; + t1 = d +e1 (a) +Ch (a, b, c) +0x650a7354 +W [36 ]; + t2 =e0 (e) +Maj (e, f, g); h += t1; d = t1+t2; + t1 = c +e1 (h) +Ch (h, a, b) +0x766a0abb +W [37 ]; + t2 =e0 (d) +Maj (d, e, f); g += t1; c = t1+t2; + t1 = b +e1 (g) +Ch (g, h, a) +0x81c2c92e +W [38 ]; + t2 =e0 (c) +Maj (c, d, e); f += t1; b = t1+t2; + t1 = a +e1 (f) +Ch (f, g, h) +0x92722c85 +W [39 ]; + t2 =e0 (b) +Maj (b, c, d); e += t1; a = t1+t2; + + t1 = h +e1 (e) +Ch (e, f, g) +0xa2bfe8a1 +W [40 ]; + t2 =e0 (a) +Maj (a, b, c); d += t1; h = t1+t2; + t1 = g +e1 (d) +Ch (d, e, f) +0xa81a664b +W [41 ]; + t2 =e0 (h) +Maj (h, a, b); c += t1; g = t1+t2; + t1 = f +e1 (c) +Ch (c, d, e) +0xc24b8b70 +W [42 ]; + t2 =e0 (g) +Maj (g, h, a); b += t1; f = t1+t2; + t1 = e +e1 (b) +Ch (b, c, d) +0xc76c51a3 +W [43 ]; + t2 =e0 (f) +Maj (f, g, h); a += t1; e = t1+t2; + t1 = d +e1 (a) +Ch (a, b, c) +0xd192e819 +W [44 ]; + t2 =e0 (e) +Maj (e, f, g); h += t1; d = t1+t2; + t1 = c +e1 (h) +Ch (h, a, b) +0xd6990624 +W [45 ]; + t2 =e0 (d) +Maj (d, e, f); g += t1; c = t1+t2; + t1 = b +e1 (g) +Ch (g, h, a) +0xf40e3585 +W [46 ]; + t2 =e0 (c) +Maj (c, d, e); f += t1; b = t1+t2; + t1 = a +e1 (f) +Ch (f, g, h) +0x106aa070 +W [47 ]; + t2 =e0 (b) +Maj (b, c, d); e += t1; a = t1+t2; + + t1 = h +e1 (e) +Ch (e, f, g) +0x19a4c116 +W [48 ]; + t2 =e0 (a) +Maj (a, b, c); d += t1; h = t1+t2; + t1 = g +e1 (d) +Ch (d, e, f) +0x1e376c08 +W [49 ]; + t2 =e0 (h) +Maj (h, a, b); c += t1; g = t1+t2; + t1 = f +e1 (c) +Ch (c, d, e) +0x2748774c +W [50 ]; + t2 =e0 (g) +Maj (g, h, a); b += t1; f = t1+t2; + t1 = e +e1 (b) +Ch (b, c, d) +0x34b0bcb5 +W [51 ]; + t2 =e0 (f) +Maj (f, g, h); a += t1; e = t1+t2; + t1 = d +e1 (a) +Ch (a, b, c) +0x391c0cb3 +W [52 ]; + t2 =e0 (e) +Maj (e, f, g); h += t1; d = t1+t2; + t1 = c +e1 (h) +Ch (h, a, b) +0x4ed8aa4a +W [53 ]; + t2 =e0 (d) +Maj (d, e, f); g += t1; c = t1+t2; + t1 = b +e1 (g) +Ch (g, h, a) +0x5b9cca4f +W [54 ]; + t2 =e0 (c) +Maj (c, d, e); f += t1; b = t1+t2; + t1 = a +e1 (f) +Ch (f, g, h) +0x682e6ff3 +W [55 ]; + t2 =e0 (b) +Maj (b, c, d); e += t1; a = t1+t2; + + t1 = h +e1 (e) +Ch (e, f, g) +0x748f82ee +W [56 ]; + t2 =e0 (a) +Maj (a, b, c); d += t1; h = t1+t2; + t1 = g +e1 (d) +Ch (d, e, f) +0x78a5636f +W [57 ]; + t2 =e0 (h) +Maj (h, a, b); c += t1; g = t1+t2; + t1 = f +e1 (c) +Ch (c, d, e) +0x84c87814 +W [58 ]; + t2 =e0 (g) +Maj (g, h, a); b += t1; f = t1+t2; + t1 = e +e1 (b) +Ch (b, c, d) +0x8cc70208 +W [59 ]; + t2 =e0 (f) +Maj (f, g, h); a += t1; e = t1+t2; + t1 = d +e1 (a) +Ch (a, b, c) +0x90befffa +W [60 ]; + t2 =e0 (e) +Maj (e, f, g); h += t1; d = t1+t2; + t1 = c +e1 (h) +Ch (h, a, b) +0xa4506ceb +W [61 ]; + t2 =e0 (d) +Maj (d, e, f); g += t1; c = t1+t2; + t1 = b +e1 (g) +Ch (g, h, a) +0xbef9a3f7 +W [62 ]; + t2 =e0 (c) +Maj (c, d, e); f += t1; b = t1+t2; + t1 = a +e1 (f) +Ch (f, g, h) +0xc67178f2 +W [63 ]; + t2 =e0 (b) +Maj (b, c, d); e += t1; a = t1+t2; + + state[0 ] += a; state[1 ] += b; state[2 ] += c; state[3 ] += d; + state[4 ] += e; state[5 ] += f; state[6 ] += g; state[7 ] += h; + +/* clear any sensitive info... */ + a = b = c = d = e = f = g = h = t1 = t2 =0 ; +memset (W ,0 ,64 *sizeof (u32)); +} + +int sha256_init (struct sha256_state *sctx) +{ + sctx->state[0 ] =SHA256_H0 ; + sctx->state[1 ] =SHA256_H1 ; + sctx->state[2 ] =SHA256_H2 ; + sctx->state[3 ] =SHA256_H3 ; + sctx->state[4 ] =SHA256_H4 ; + sctx->state[5 ] =SHA256_H5 ; + sctx->state[6 ] =SHA256_H6 ; + sctx->state[7 ] =SHA256_H7 ; + sctx->count =0 ; + +return 0 ; +} + +int sha256_update (struct sha256_state *sctx,const u8 *data,unsigned int len) +{ +unsigned int partial, done; +const u8 *src; + + partial = sctx->count &0x3f ; + sctx->count += len; + done =0 ; + src = data; + +if ((partial + len) >63 ) { +if (partial) { + done = -partial; +memcpy (sctx->buf + partial, data, done +64 ); + src = sctx->buf; + } + +do { +sha256_transform (sctx->state, src); + done +=64 ; + src = data + done; + }while (done +63 < len); + + partial =0 ; + } +memcpy (sctx->buf + partial, src, len - done); + +return 0 ; +} + +int sha256_final (struct sha256_state *sctx,u8 *out) +{ +__be32 *dst = (__be32 *)out; +__be64 bits; +unsigned int index, pad_len; +int i; +static const u8 padding[64 ] = {0x80 , }; + +/* Save number of bits */ + bits =cpu_to_be64 (sctx->count <<3 ); + +/* Pad out to 56 mod 64. */ + index = sctx->count &0x3f ; + pad_len = (index <56 ) ? (56 - index) : ((64 +56 ) - index); +sha256_update (sctx, padding, pad_len); + +/* Append length (before padding) */ +sha256_update (sctx, (const u8 *)&bits,sizeof (bits)); + +/* Store state in digest */ +for (i =0 ; i <8 ; i++) + dst[i] =cpu_to_be32 (sctx->state[i]); + +/* Zeroize sensitive information. */ +memset (sctx,0 ,sizeof (*sctx)); + +return 0 ; +}
arch/x86/purgatory/sha256.h new file +22 -0
@@ -0,0 +1,22 @@ +/* +* Copyright (C) 2014 Red Hat Inc. +* +* Author: Vivek Goyal <[email protected]> +* +* This source code is licensed under the GNU General Public License, +* Version 2. See the file COPYING for more details. +*/ + +#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 */
Signature · 7 lines
1.9.0 _______________________________________________ kexec mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kexec
PATCH v4 11/15 purgatory: Core purgatory functionality
In reply to Vivek GoyalCreate 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.
--- arch/x86/Kbuild | 4 ++ arch/x86/Makefile | 8 +++ arch/x86/purgatory/Makefile | 30 +++++++++++ arch/x86/purgatory/entry64.S | 101 ++++++++++++++++++++++++++++++++++++++ arch/x86/purgatory/purgatory.c | 72 +++++++++++++++++++++++++++ arch/x86/purgatory/setup-x86_64.S | 58 ++++++++++++++++++++++ arch/x86/purgatory/stack.S | 19 +++++++ arch/x86/purgatory/string.c | 13 +++++ 8 files changed, 305 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_64.S create mode 100644 arch/x86/purgatory/stack.S create mode 100644 arch/x86/purgatory/string.c
arch/x86/Kbuild +4 -0
@@ -16,3 +16,7 @@ obj-$(CONFIG_IA32_EMULATION) += ia32/ obj-y += platform/ obj-y += net/ + +ifeq ($(CONFIG_X86_64),y) +obj-$(CONFIG_KEXEC) += purgatory/ +endif
arch/x86/Makefile +8 -0
@@ -186,6 +186,14 @@ archscripts: scripts_basic archheaders: $(Q)$(MAKE) $(build)=arch/x86/syscalls all +archprepare: +ifeq ($(CONFIG_KEXEC),y) +# Build only for 64bit. No loaders for 32bit yet. + ifeq ($(CONFIG_X86_64),y) + $(Q)$(MAKE) $(build)=arch/x86/purgatory arch/x86/purgatory/kexec-purgatory.c + endif +endif + ### # Kernel objects
arch/x86/purgatory/Makefile new file +30 -0
@@ -0,0 +1,30 @@ +purgatory-y := purgatory.o stack.o setup-x86_$(BITS).o sha256.o entry64.o string.o + +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, use custom flags. + +KBUILD_CFLAGS := -fno-strict-aliasing -Wall -Wstrict-prototypes -fno-zero-initialized-in-bss -fno-builtin -ffreestanding -c -MD -Os -mcmodel=large + +$(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) + + +# No loaders for 32bits yet. +ifeq ($(CONFIG_X86_64),y) + obj-$(CONFIG_KEXEC) += kexec-purgatory.o +endif
arch/x86/purgatory/entry64.S new file +101 -0
101 lines not on this page · raw message
arch/x86/purgatory/purgatory.c new file +72 -0
72 lines not on this page · raw message
arch/x86/purgatory/setup-x86_64.S new file +58 -0
58 lines not on this page · raw message
arch/x86/purgatory/stack.S new file +19 -0
@@ -0,0 +1,19 @@ +/* + * purgatory: stack + * + * Copyright (C) 2014 Red Hat Inc. + * + * This source code is licensed under the GNU General Public License, + * Version 2. See the file COPYING for more details. + */ + + /* 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:
arch/x86/purgatory/string.c new file +13 -0
@@ -0,0 +1,13 @@ +/* +* Simple string functions. +* +* Copyright (C) 2014 Red Hat Inc. +* +* Author: +* Vivek Goyal <[email protected]> +* +* This source code is licensed under the GNU General Public License, +* Version 2. See the file COPYING for more details. +*/ + +#include "../boot/string.c"
Signature · 7 lines
1.9.0 _______________________________________________ kexec mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kexec
Re: [PATCH 11/15] purgatory: Core purgatory functionality
FYI, it looks like the following patch (committed in 8fc5b4d4121c95482b2583) adds a new requirement to use at least gcc 4.4 to build the kernel?
Quoted 31 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. 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.
Signed-off-by Vivek Goyal <[email protected]>--- arch/x86/Kbuild | 4 ++ arch/x86/Makefile | 8 +++ arch/x86/purgatory/Makefile | 30 +++++++++++ arch/x86/purgatory/entry64.S | 101 ++++++++++++++++++++++++++++++++++++++ arch/x86/purgatory/purgatory.c | 72 +++++++++++++++++++++++++++ arch/x86/purgatory/setup-x86_64.S | 58 ++++++++++++++++++++++ arch/x86/purgatory/stack.S | 19 +++++++ arch/x86/purgatory/string.c | 13 +++++ 8 files changed, 305 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_64.S create mode 100644 arch/x86/purgatory/stack.S create mode 100644 arch/x86/purgatory/string.c
[snip]
Quoted 9 lines
arch/x86/purgatory/Makefile new file +3
-0@@ -0,0 +1,30 @@ +purgatory-y := purgatory.o stack.o setup-x86_$(BITS).o sha256.o entry64.o string.o + +targets += $(purgatory-y)
arch/x86/purgatory/Makefile line 13
Quoted 10 lines
arch/x86/purgatory/Makefile new file +10
-0+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, use custom flags. + +KBUILD_CFLAGS := -fno-strict-aliasing -Wall -Wstrict-prototypes -fno-zero-initialized-in-bss -fno-builtin -ffreestanding -c -MD -Os -mcmodel=large
The above "-mcmodel=large" compiler flag produces the following output on GCC 4.1.2.
$ make modules_prepare ; gcc --version
CHK include/config/kernel.release
CHK include/generated/uapi/linux/version.h
CHK include/generated/utsrelease.h
CC arch/x86/purgatory/purgatory.o
arch/x86/purgatory/purgatory.c:1: sorry, unimplemented: code model ‘large’ not supported yet
make[1]: *** [arch/x86/purgatory/purgatory.o] Error 1
make: *** [archprepare] Error 2
gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-54)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.I did a quick search for a discussion the indicates this compiler is now officially too old to build the kernel but did not find one.
If this is required, maybe Documentation/Changes needs to be updated with the new minimum required version?
Cheers,
Shaun
H. Peter Anvin
reviewerRe: [PATCH 11/15] purgatory: Core purgatory functionality
Quoted 3 lines · Shaun Ruffell
FYI, it looks like the following patch (committed in
8fc5b4d4121c95482b2583) adds a new requirement to use at least gcc 4.4 to build the kernel?
Well, to build the kernel with that particular new functionality enabled, yes.
However...
Quoted 15 lines
The above "-mcmodel=large" compiler flag produces the following output on GCC 4.1.2.
$ make modules_prepare ; gcc --version CHK include/config/kernel.release CHK include/generated/uapi/linux/version.h CHK include/generated/utsrelease.h CC arch/x86/purgatory/purgatory.o arch/x86/purgatory/purgatory.c:1: sorry, unimplemented: code model ‘large’ not supported yet make[1]: *** [arch/x86/purgatory/purgatory.o] Error 1 make: *** [archprepare] Error 2 gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-54) Copyright (C) 2006 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Quoted 5 lines
I did a quick search for a discussion the indicates this compiler is now officially too old to build the kernel but did not find one.
If this is required, maybe
Documentation/Changesneeds to be updated with the new minimum required version?
... I'm having a very, very hard time to picture a reason why the "large" model would be necessary or even desirable for the purgatory code. The "small PIC" model seem to fit much better...
-hpa
_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
Re: [PATCH 11/15] purgatory: Core purgatory functionality
Quoted 28 lines · H. Peter Anvin
Quoted 3 lines · Shaun Ruffell
FYI, it looks like the following patch (committed in
8fc5b4d4121c95482b2583) adds a new requirement to use at least gcc 4.4 to build the kernel?Well, to build the kernel with that particular new functionality enabled, yes.
However...
Quoted 18 lines
The above "-mcmodel=large" compiler flag produces the following output on GCC 4.1.2.
$ make modules_prepare ; gcc --version CHK include/config/kernel.release CHK include/generated/uapi/linux/version.h CHK include/generated/utsrelease.h CC arch/x86/purgatory/purgatory.o arch/x86/purgatory/purgatory.c:1: sorry, unimplemented: code model ‘large’ not supported yet make[1]: *** [arch/x86/purgatory/purgatory.o] Error 1 make: *** [archprepare] Error 2 gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-54) Copyright (C) 2006 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.I did a quick search for a discussion the indicates this compiler is now officially too old to build the kernel but did not find one.
Quoted 6 lines
Quoted 2 lines
If this is required, maybe
Documentation/Changesneeds to be updated with the new minimum required version?... I'm having a very, very hard time to picture a reason why the "large" model would be necessary or even desirable for the purgatory code. The "small PIC" model seem to fit much better...
Hi hpa,
I took it because kexec-tools uses it and in one of the committs Eric gave following reasoning.
On x86_64 use -mcmodel=large so that the code is built without any 32bit assumptions. -mcmodel=medium and -mcmodel=small result int code that has 32bit relocations against variables that can live anywhere in the address space
We do want to load purgatory anywhere in the address space.
But if there are other ways to achieve the same thing, I will do that change.
So when you say "small PIC", I need to use -mcmodel=small and -fPIC?
I can give that a try.
Thanks
Vivek
_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
H. Peter Anvin
reviewerRe: [PATCH 11/15] purgatory: Core purgatory functionality
Quoted 11 lines · Vivek Goyal
Hi hpa,
I took it because kexec-tools uses it and in one of the committs Eric gave following reasoning.
On
x86_64use -mcmodel=large so that the code is built without any 32bit assumptions. -mcmodel=medium and -mcmodel=small result int code that has 32bit relocations against variables that can live anywhere in the address space
Quoted 7 lines
We do want to load purgatory anywhere in the address space.
But if there are other ways to achieve the same thing, I will do that change.
So when you say "small PIC", I need to use -mcmodel=small and -fPIC?
Actually -fPIE is probably better than -fPIC.
-mcmodel=large is incompatible with all other code out there, which means that even though it is supposed to work it will be poorly tested at best. So even despite the gcc version issue, using the small PIC model would be better.
-hpa
_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
Re: [PATCH 11/15] purgatory: Core purgatory functionality
Quoted 20 lines · H. Peter Anvin
Quoted 11 lines · Vivek Goyal
Hi hpa,
I took it because kexec-tools uses it and in one of the committs Eric gave following reasoning.
On
x86_64use -mcmodel=large so that the code is built without any 32bit assumptions. -mcmodel=medium and -mcmodel=small result int code that has 32bit relocations against variables that can live anywhere in the address spaceQuoted 7 lines
We do want to load purgatory anywhere in the address space.
But if there are other ways to achieve the same thing, I will do that change.
So when you say "small PIC", I need to use -mcmodel=small and -fPIC?
Quoted 6 lines
Actually -fPIE is probably better than -fPIC.
-mcmodel=large is incompatible with all other code out there, which means that even though it is supposed to work it will be poorly tested at best. So even despite the gcc version issue, using the small PIC model would be better.
Ok, I will give it a try.
Thanks
Vivek
_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
Re: [PATCH 11/15] purgatory: Core purgatory functionality
In reply to H. Peter AnvinQuoted 20 lines · H. Peter Anvin
Quoted 11 lines · Vivek Goyal
Hi hpa,
I took it because kexec-tools uses it and in one of the committs Eric gave following reasoning.
On
x86_64use -mcmodel=large so that the code is built without any 32bit assumptions. -mcmodel=medium and -mcmodel=small result int code that has 32bit relocations against variables that can live anywhere in the address spaceQuoted 7 lines
We do want to load purgatory anywhere in the address space.
But if there are other ways to achieve the same thing, I will do that change.
So when you say "small PIC", I need to use -mcmodel=small and -fPIC?
Quoted 6 lines
Actually -fPIE is probably better than -fPIC.
-mcmodel=large is incompatible with all other code out there, which means that even though it is supposed to work it will be poorly tested at best. So even despite the gcc version issue, using the small PIC model would be better.
Hi hpa,
I have not introduced a new config option for new system call. It compiles under existing config option CONFIG_KEXEC and I think that's the reason Shaun is running into this issue.
I passed -mcmodel=small and -fPIE for purgaotry build.
KBUILD_CFLAGS := -fno-strict-aliasing -Wall -Wstrict-prototypes -fno-zero-initialized-in-bss -fno-builtin -ffreestanding -c -MD -Os -mcmodel=small -fPIE
On the surface I see new reclocation types.
000000000022 003000000004 R_X86_64_PLT32 00000000000000d8 sha256_init - 4 000000000034 003f00000004 R_X86_64_PLT32 000000000000011a sha256_update - 4 000000000049 004100000004 R_X86_64_PLT32 0000000000001d4d sha256_final - 4
Current code does not take care of these. I am hoping these can be managed.
I also see undefined symbol.
64: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND _GLOBAL_OFFSET_TABLE_
And current purgatory relocation code does not expect any undefined symbols and errors out. IIUC, undefined symbols expect to be resolved externally by linking against somehing else. But in case of purgatory this is suppoed to be stand alone and does not link against anything else. So I am not sure how to take care of this.
I doubt that I can get this code working with -fPIE in short amount of time (Given my lack of knowledge in this area). So while we continue to explore it, will it make sense that I also work on a patch to hide all the new functionality behind a new config option (say CONFIG_KEXEC_FILE). That way existing users will not be impacted and only users of the new syscall will be expected to have newer gcc with -mcmodel=large.
And then I can continue to look into how to get rid of -mcmodel=large requirement.
Thanks
Vivek
_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
PATCH v4 12/15 kexec: Load and Relocate purgatory at kernel load time
In reply to Vivek GoyalLoad purgatory code in RAM and relocate it based on the location. Relocation code has been inspired by module relocation code and purgatory relocation code in kexec-tools.
Also compute the checksums of loaded kexec segments and store them in purgatory.
Arch independent code provides this functionality so that arch dependent bootloaders can make use of it.
Helper functions are provided to get/set symbol values in purgatory which are used by bootloaders later to set things like stack and entry point of second kernel etc.
--- arch/x86/Kconfig | 2 + arch/x86/kernel/machine_kexec_64.c | 142 ++++++++++ include/linux/kexec.h | 33 +++ kernel/kexec.c | 544 ++++++++++++++++++++++++++++++++++++- 4 files changed, 720 insertions(+), 1 deletion(-)
arch/x86/Kconfig +2 -0
@@ -1577,6 +1577,8 @@ source kernel/Kconfig.hz config KEXEC bool "kexec system call" select BUILD_BIN2C + select CRYPTO + 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/kernel/machine_kexec_64.c +142 -0
151 lines not on this page · raw message
include/linux/kexec.h +33 -0
@@ -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; };/*
@@ -189,6 +214,14 @@ extern int kexec_add_buffer(struct kimage *image, char *buffer, unsigned long *load_addr);extern struct page *kimage_alloc_control_pages (struct kimage *image,unsigned int order); +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 *);void crash_save_cpu (struct pt_regs *regs,int cpu);
kernel/kexec.c +543 -1
592 lines not on this page · raw message
Signature · 7 lines
1.9.0 _______________________________________________ kexec mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kexec
PATCH v4 13/15 kexec-bzImage64: Support for loading bzImage using 64bit entry
In reply to Vivek GoyalThis 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.
32bit mode entry can be implemented if somebody needs it.
--- arch/x86/include/asm/kexec-bzimage64.h | 6 + arch/x86/include/asm/kexec.h | 21 ++ arch/x86/kernel/Makefile | 1 + arch/x86/kernel/kexec-bzimage64.c | 375 +++++++++++++++++++++++++++++++++ arch/x86/kernel/machine_kexec_64.c | 3 +- 5 files changed, 405 insertions(+), 1 deletion(-) create mode 100644 arch/x86/include/asm/kexec-bzimage64.h create mode 100644 arch/x86/kernel/kexec-bzimage64.c
arch/x86/include/asm/kexec-bzimage64.h new file +6 -0
@@ -0,0 +1,6 @@ +#ifndef _ASM_KEXEC_BZIMAGE64_H +#define _ASM_KEXEC_BZIMAGE64_H + +extern struct kexec_file_ops kexec_bzImage64_ops; + +#endif /* _ASM_KEXE_BZIMAGE64_H */
arch/x86/include/asm/kexec.h +21 -0
@@ -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,6 +162,26 @@ 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 );
arch/x86/kernel/Makefile +1 -0
@@ -117,4 +117,5 @@ ifeq ($(CONFIG_X86_64),y) obj-$(CONFIG_PCI_MMCONFIG) += mmconf-fam10h_64.o obj-y += vsmp_64.o + obj-$(CONFIG_KEXEC) += kexec-bzimage64.o endif
arch/x86/kernel/kexec-bzimage64.c new file +375 -0
375 lines not on this page · raw message
arch/x86/kernel/machine_kexec_64.c +2 -1
@@ -23,9 +23,10 @@#include <asm/tlbflush.h> #include <asm/mmu_context.h> #include <asm/debugreg.h> +#include <asm/kexec-bzimage64.h> static struct kexec_file_ops *kexec_file_loaders[] = { -NULL , + &kexec_bzImage64_ops, };static void free_transition_pgtable (struct kimage *image)
Signature · 7 lines
1.9.0 _______________________________________________ kexec mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kexec
PATCH v4 14/15 kexec: Support for kexec on panic using new system call
In reply to Vivek GoyalThis patch adds support for loading a kexec on panic (kdump) kernel usning new system call.
It prepares ELF headers for memory areas to be dumped and for saved cpu registers. Also prepares the memory map for second kernel and limits its boot to reserved areas only.
--- arch/x86/include/asm/crash.h | 9 + arch/x86/include/asm/kexec.h | 19 ++ arch/x86/kernel/crash.c | 563 +++++++++++++++++++++++++++++++++++++ arch/x86/kernel/kexec-bzimage64.c | 55 +++- arch/x86/kernel/machine_kexec_64.c | 40 +++ kernel/kexec.c | 46 ++- 6 files changed, 713 insertions(+), 19 deletions(-) create mode 100644 arch/x86/include/asm/crash.h
arch/x86/include/asm/crash.h new file +9 -0
@@ -0,0 +1,9 @@ +#ifndef _ASM_X86_CRASH_H +#define _ASM_X86_CRASH_H + +int crash_load_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 +19 -0
@@ -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 (640 * 1024UL)/* 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 */ +void *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;
arch/x86/kernel/crash.c +563 -0
593 lines not on this page · raw message
arch/x86/kernel/kexec-bzimage64.c +45 -10
122 lines not on this page · raw message
arch/x86/kernel/machine_kexec_64.c +40 -0
@@ -178,6 +178,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;
@@ -191,6 +223,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 ; }
@@ -315,6 +352,9 @@ int arch_kexec_kernel_image_probe(struct kimage *image, void *buf,void *arch_kexec_kernel_image_load (struct kimage *image) { +vfree (image->arch.elf_headers); + image->arch.elf_headers =NULL ; +if (!image->fops || !image->fops->load)return ERR_PTR (-ENOEXEC );
kernel/kexec.c +37 -9
90 lines not on this page · raw message
Signature · 7 lines
1.9.0 _______________________________________________ kexec mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kexec
PATCH v4 15/15 kexec: Support kexec/kdump on EFI systems
In reply to Vivek GoyalThis patch does two thigns. It passes EFI run time mappings to second kernel in bootparams efi_info. Second kernel parse this info and create new mappings in second kernel. That means mappings in first and second kernel will be same. This paves the way to enable EFI in kexec kernel.
This patch also prepares and passes EFI setup data through bootparams. This contains bunch of information about various tables and their addresses.
These information gathering and passing has been written along the lines of what current kexec-tools is doing to make kexec work with UEFI.
--- arch/x86/kernel/kexec-bzimage64.c | 146 ++++++++++++++++++++++++++++++++++--- drivers/firmware/efi/runtime-map.c | 21 ++++++ include/linux/efi.h | 19 +++++ 3 files changed, 174 insertions(+), 12 deletions(-)
arch/x86/kernel/kexec-bzimage64.c +134 -12
@@ -18,10 +18,12 @@#include <linux/kexec.h> #include <linux/kernel.h> #include <linux/mm.h> +#include <linux/efi.h> #include <asm/bootparam.h> #include <asm/setup.h> #include <asm/crash.h> +#include <asm/efi.h> #define MAX_ELFCOREHDR_STR_LEN 30/* elfcorehdr=0x<64bit-value> */
@@ -90,7 +92,7 @@ static int setup_cmdline(struct kimage *image, struct boot_params *params,return 0 ; } -static int setup_memory_map_entries (struct boot_params *params) +static int setup_e820_entries (struct boot_params *params) {unsigned int nr_e820_entries;
@@ -107,8 +109,93 @@ static int setup_memory_map_entries(struct boot_params *params)return 0 ; } -static int setup_boot_parameters (struct kimage *image, -struct boot_params *params) +#ifdef CONFIG_EFI +static int setup_efi_info_memmap (struct boot_params *params, +unsigned long params_load_addr, +unsigned int efi_map_offset, +unsigned int efi_map_sz) +{ +void *efi_map = (void *)params + efi_map_offset; +unsigned long efi_map_phys_addr = params_load_addr + efi_map_offset; +struct efi_info *ei = ¶ms->efi_info; + +if (!efi_map_sz) +return 0 ; + +efi_runtime_map_copy (efi_map, efi_map_sz); + + ei->efi_memmap = efi_map_phys_addr &0xffffffff ; + ei->efi_memmap_hi = efi_map_phys_addr >>32 ; + ei->efi_memmap_size = efi_map_sz; + +return 0 ; +} + +static int +prepare_add_efi_setup_data (struct boot_params *params, +unsigned long params_load_addr, +unsigned int efi_setup_data_offset) +{ +unsigned long setup_data_phys; +struct setup_data *sd = (void *)params + efi_setup_data_offset; +struct efi_setup_data *esd = (void *)sd +sizeof (struct setup_data ); + + esd->fw_vendor = efi.fw_vendor; + esd->runtime = efi.runtime; + esd->tables = efi.config_table; + esd->smbios = efi.smbios; + + sd->type =SETUP_EFI ; + sd->len =sizeof (struct efi_setup_data ); + +/* Add setup data */ + setup_data_phys = params_load_addr + efi_setup_data_offset; + sd->next = params->hdr.setup_data; + params->hdr.setup_data = setup_data_phys; + +return 0 ; +} + +static int +setup_efi_state (struct boot_params *params,unsigned long params_load_addr, +unsigned int efi_map_offset,unsigned int efi_map_sz, +unsigned int efi_setup_data_offset) +{ +struct efi_info *current_ei = &boot_params.efi_info; +struct efi_info *ei = ¶ms->efi_info; + +if (!current_ei->efi_memmap_size) +return 0 ; + +/* +* If 1:1 mapping is not enabled, second kernel can not setup EFI +* and use EFI run time services. User space will have to pass +* acpi_rsdp=<addr> on kernel command line to make second kernel boot +* without efi. +*/ +if (efi_enabled (EFI_OLD_MEMMAP )) +return 0 ; + + ei->efi_loader_signature = current_ei->efi_loader_signature; + ei->efi_systab = current_ei->efi_systab; + ei->efi_systab_hi = current_ei->efi_systab_hi; + + ei->efi_memdesc_version = current_ei->efi_memdesc_version; + ei->efi_memdesc_size =get_efi_runtime_map_desc_size (); + +setup_efi_info_memmap (params, params_load_addr, efi_map_offset, + efi_map_sz); +prepare_add_efi_setup_data (params, params_load_addr, + efi_setup_data_offset); +return 0 ; +} +#endif /* CONFIG_EFI */ + +static int +setup_boot_parameters (struct kimage *image,struct boot_params *params, +unsigned long params_load_addr, +unsigned int efi_map_offset,unsigned int efi_map_sz, +unsigned int efi_setup_data_offset) {unsigned int nr_e820_entries;unsigned long long mem_k, start, end;
@@ -140,7 +227,7 @@ static int setup_boot_parameters(struct kimage *image,if (ret)return ret; }else -setup_memory_map_entries (params); +setup_e820_entries (params); nr_e820_entries = params->e820_entries;
@@ -161,6 +248,12 @@ static int setup_boot_parameters(struct kimage *image, } } +#ifdef CONFIG_EFI +/* Setup EFI state */ +setup_efi_state (params, params_load_addr, efi_map_offset, efi_map_sz, + efi_setup_data_offset); +#endif +/* Setup EDD info */ memcpy (params->eddbuf, boot_params.eddbuf,EDDMAXNR *sizeof (struct edd_info ));
@@ -214,6 +307,15 @@ int bzImage64_probe(const char *buf, unsigned long len)return ret; } +/* +* Can't handle 32bit EFI as it does not allow loading kernel +* above 4G. This should be handled by 32bit bzImage loader +*/ +if (efi_enabled (EFI_RUNTIME_SERVICES ) && !efi_enabled (EFI_64BIT )) { +pr_debug ("EFI is 32 bit. Can't load kernel above 4G.\n" ); +return ret; + } +/* I've got a bzImage */ pr_debug ("It's a relocatable bzImage64\n" ); ret =0 ;
@@ -229,7 +331,7 @@ void *bzImage64_load(struct kimage *image, char *kernel,struct setup_header *header;int setup_sects, kern16_size, ret =0 ; -unsigned long setup_header_size, params_cmdline_sz; +unsigned long setup_header_size, params_cmdline_sz, params_misc_sz;struct boot_params *params;unsigned long bootparam_load_addr, kernel_load_addr, initrd_load_addr;unsigned long purgatory_load_addr;
@@ -239,6 +341,7 @@ void *bzImage64_load(struct kimage *image, char *kernel,struct kexec_entry64_regs regs64;void *stack;unsigned int setup_hdr_offset = offsetof(struct boot_params , hdr); +unsigned int efi_map_offset, efi_map_sz, efi_setup_data_offset; header = (struct setup_header *)(kernel + setup_hdr_offset); setup_sects = header->setup_sects;
@@ -285,12 +388,29 @@ void *bzImage64_load(struct kimage *image, char *kernel,pr_debug ("Loaded purgatory at 0x%lx\n" , purgatory_load_addr); -/* Load Bootparams and cmdline */ + +/* +* Load Bootparams and cmdline and space for efi stuff. +* +* Allocate memory together for multiple data structures so +* that they all can go in single area/segment and we don't +* have to create separate segment for each. Keeps things +* little bit simple +*/ + efi_map_sz =get_efi_runtime_map_size (); + efi_map_sz =ALIGN (efi_map_sz,16 ); params_cmdline_sz =sizeof (struct boot_params ) + cmdline_len +MAX_ELFCOREHDR_STR_LEN ; - params =kzalloc (params_cmdline_sz,GFP_KERNEL ); + params_cmdline_sz =ALIGN (params_cmdline_sz,16 ); + params_misc_sz = params_cmdline_sz + efi_map_sz + +sizeof (struct setup_data ) + +sizeof (struct efi_setup_data ); + + params =kzalloc (params_misc_sz,GFP_KERNEL );if (!params)return ERR_PTR (-ENOMEM ); + efi_map_offset = params_cmdline_sz; + efi_setup_data_offset = efi_map_offset + efi_map_sz;/* Copy setup header onto bootparams. Documentation/x86/boot.txt */ setup_header_size =0x0202 + kernel[0x0201 ] - setup_hdr_offset;
@@ -298,13 +418,13 @@ void *bzImage64_load(struct kimage *image, char *kernel,/* Is there a limit on setup header size? */ memcpy (¶ms->hdr, (kernel + setup_hdr_offset), setup_header_size); - ret =kexec_add_buffer (image, (char *)params, params_cmdline_sz, - params_cmdline_sz,16 ,MIN_BOOTPARAM_ADDR , + ret =kexec_add_buffer (image, (char *)params, params_misc_sz, + params_misc_sz,16 ,MIN_BOOTPARAM_ADDR ,ULONG_MAX ,1 , &bootparam_load_addr);if (ret) goto out_free_params; -pr_debug ("Loaded boot_param and command line at 0x%lx bufsz=0x%lx memsz=0x%lx\n" , - bootparam_load_addr, params_cmdline_sz, params_cmdline_sz); +pr_debug ("Loaded boot_param, command line and misc at 0x%lx bufsz=0x%lx memsz=0x%lx\n" , + bootparam_load_addr, params_misc_sz, params_misc_sz);/* Load kernel */ kernel_buf = kernel + kern16_size;
@@ -365,7 +485,9 @@ void *bzImage64_load(struct kimage *image, char *kernel,if (ret) goto out_free_params; - ret =setup_boot_parameters (image, params); + ret =setup_boot_parameters (image, params, bootparam_load_addr, + efi_map_offset, efi_map_sz, + efi_setup_data_offset);if (ret) goto out_free_params;
drivers/firmware/efi/runtime-map.c +21 -0
@@ -138,6 +138,27 @@ add_sysfs_runtime_map_entry(struct kobject *kobj, int nr)return entry; } +int get_efi_runtime_map_size (void ) +{ +return nr_efi_runtime_map * efi_memdesc_size; +} + +int get_efi_runtime_map_desc_size (void ) +{ +return efi_memdesc_size; +} + +int efi_runtime_map_copy (void *buf,size_t bufsz) +{ +size_t sz =get_efi_runtime_map_size (); + +if (sz > bufsz) + sz = bufsz; + +memcpy (buf, efi_runtime_map, sz); +return 0 ; +} +void efi_runtime_map_setup (void *map,int nr_entries,u32 desc_size) { efi_runtime_map = map;
include/linux/efi.h +19 -0
@@ -1151,6 +1151,9 @@ int efivars_sysfs_init(void);#ifdef CONFIG_EFI_RUNTIME_MAP int efi_runtime_map_init (struct kobject *);void efi_runtime_map_setup (void *,int ,u32 ); +int get_efi_runtime_map_size (void ); +int get_efi_runtime_map_desc_size (void ); +int efi_runtime_map_copy (void *buf,size_t bufsz);#else static inline int efi_runtime_map_init (struct kobject *kobj) {
@@ -1159,6 +1162,22 @@ static inline int efi_runtime_map_init(struct kobject *kobj)static inline void efi_runtime_map_setup (void *map,int nr_entries,u32 desc_size) {} + +static inline int get_efi_runtime_map_size (void ) +{ +return 0 ; +} + +static inline int get_efi_runtime_map_desc_size (void ) +{ +return 0 ; +} + +static inline int efi_runtime_map_copy (void *buf,size_t bufsz) +{ +return 0 ; +} +#endif #endif /* _LINUX_EFI_H */
Signature · 1 line
1.9.0
Re: [PATCH 15/15] kexec: Support kexec/kdump on EFI systems
Quoted 19 lines · Vivek Goyal
This patch does two thigns. It passes EFI run time mappings to second kernel in bootparams
efi_info. Second kernel parse this info and create new mappings in second kernel. That means mappings in first and second kernel will be same. This paves the way to enable EFI in kexec kernel.This patch also prepares and passes EFI setup data through bootparams. This contains bunch of information about various tables and their addresses.
These information gathering and passing has been written along the lines of what current kexec-tools is doing to make kexec work with UEFI.
Signed-off-by Vivek Goyal <[email protected]>--- arch/x86/kernel/kexec-bzimage64.c | 146 ++++++++++++++++++++++++++++++++++--- drivers/firmware/efi/runtime-map.c | 21 ++++++ include/linux/efi.h | 19 +++++ 3 files changed, 174 insertions(+), 12 deletions(-)
[...]
Quoted 18 lines
drivers/firmware/efi/runtime-map.c +10
-0@@ -138,6 +138,27 @@ add_sysfs_runtime_map_entry(struct kobject *kobj, int nr)return entry; } +int get_efi_runtime_map_size (void ) +{ +return nr_efi_runtime_map * efi_memdesc_size; +} + +int get_efi_runtime_map_desc_size (void ) +{ +return efi_memdesc_size; +} +
drivers/firmware/efi/runtime-map.c add_sysfs_runtime_map_entry(struct kobject *kobj, int nr) · line 160
Quoted 10 lines
drivers/firmware/efi/runtime-map.c +10
-0+int efi_runtime_map_copy (void *buf,size_t bufsz) +{ +size_t sz =get_efi_runtime_map_size (); + +if (sz > bufsz) + sz = bufsz; + +memcpy (buf, efi_runtime_map, sz); +return 0 ; +}
Could we prefix these with efi_, e.g. efi_get_runtime_map_size() ?
Signature · 1 line
Matt Fleming, Intel Open Source Technology Center
Andrew Morton
maintainerRe: [PATCH 15/15] kexec: Support kexec/kdump on EFI systems
Quoted 14 lines · On Tue, 1 Jul 2014 20:46:05 +0100 Matt Fleming…
Quoted 14 lines
(quoted diff) +14
-0+int get_efi_runtime_map_size(void) +{ + return nr_efi_runtime_map * efi_memdesc_size; +} + +int get_efi_runtime_map_desc_size(void) +{ + return efi_memdesc_size; +} + +int efi_runtime_map_copy(void *buf, size_t bufsz) +{ + size_t sz = get_efi_runtime_map_size(); +
Quoted 8 lines
Review
Quoted 6 lines
(quoted diff) +6
-0+ if (sz > bufsz) + sz = bufsz; + + memcpy(buf, efi_runtime_map, sz); + return 0; +}Could we prefix these with
efi_, e.g.efi_get_runtime_map_size()?
This?
From: Andrew Morton <akpm-de/[email protected]>
Subject: kexec-support-kexec-kdump-on-efi-systems-fix
s/get_efi/efi_get/g, per Matt
---
arch/x86/kernel/kexec-bzimage64.c | 4 ++-- drivers/firmware/efi/runtime-map.c | 6 +++--- include/linux/efi.h | 8 ++++---- 3 files changed, 9 insertions(+), 9 deletions(-)
arch/x86/kernel/kexec-bzimage64.c renamed from arch/x86/kernel/kexec-bzimage64.c~kexec-support-kexec-kdump-on-efi-systems-fix +2 -2
@@ -181,7 +181,7 @@ setup_efi_state(struct boot_params *para ei->efi_systab_hi = current_ei->efi_systab_hi; ei->efi_memdesc_version = current_ei->efi_memdesc_version; - ei->efi_memdesc_size =get_efi_runtime_map_desc_size (); + ei->efi_memdesc_size =efi_get_runtime_map_desc_size ();setup_efi_info_memmap (params, params_load_addr, efi_map_offset, efi_map_sz);
@@ -397,7 +397,7 @@ void *bzImage64_load(struct kimage *imag* have to create separate segment for each. Keeps things * little bit simple */ - efi_map_sz =get_efi_runtime_map_size (); + efi_map_sz =efi_get_runtime_map_size (); efi_map_sz =ALIGN (efi_map_sz,16 ); params_cmdline_sz =sizeof (struct boot_params ) + cmdline_len +MAX_ELFCOREHDR_STR_LEN ;
drivers/firmware/efi/runtime-map.c renamed from drivers/firmware/efi/runtime-map.c~kexec-support-kexec-kdump-on-efi-systems-fix +3 -3
@@ -138,19 +138,19 @@ add_sysfs_runtime_map_entry(struct kobjereturn entry; } -int get_efi_runtime_map_size (void ) +int efi_get_runtime_map_size (void ) {return nr_efi_runtime_map * efi_memdesc_size; } -int get_efi_runtime_map_desc_size (void ) +int efi_get_runtime_map_desc_size (void ) {return efi_memdesc_size; }int efi_runtime_map_copy (void *buf,size_t bufsz) { -size_t sz =get_efi_runtime_map_size (); +size_t sz =efi_get_runtime_map_size ();if (sz > bufsz) sz = bufsz;
include/linux/efi.h renamed from include/linux/efi.h~kexec-support-kexec-kdump-on-efi-systems-fix +4 -4
@@ -1151,8 +1151,8 @@ int efivars_sysfs_init(void);#ifdef CONFIG_EFI_RUNTIME_MAP int efi_runtime_map_init (struct kobject *);void efi_runtime_map_setup (void *,int ,u32 ); -int get_efi_runtime_map_size (void ); -int get_efi_runtime_map_desc_size (void ); +int efi_get_runtime_map_size (void ); +int efi_get_runtime_map_desc_size (void );int efi_runtime_map_copy (void *buf,size_t bufsz);#else static inline int efi_runtime_map_init (struct kobject *kobj)
@@ -1163,12 +1163,12 @@ static inline int efi_runtime_map_init(sstatic inline void efi_runtime_map_setup (void *map,int nr_entries,u32 desc_size) {} -static inline int get_efi_runtime_map_size (void ) +static inline int efi_get_runtime_map_size (void ) {return 0 ; } -static inline int get_efi_runtime_map_desc_size (void ) +static inline int efi_get_runtime_map_desc_size (void ) {return 0 ; }
_
Re: [PATCH 15/15] kexec: Support kexec/kdump on EFI systems
Quoted 27 lines · Andrew Morton
Quoted 14 lines · On Tue, 1 Jul 2014 20:46:05 +0100 Matt Fleming…
Quoted 14 lines
(quoted diff) +14
-0+int get_efi_runtime_map_size(void) +{ + return nr_efi_runtime_map * efi_memdesc_size; +} + +int get_efi_runtime_map_desc_size(void) +{ + return efi_memdesc_size; +} + +int efi_runtime_map_copy(void *buf, size_t bufsz) +{ + size_t sz = get_efi_runtime_map_size(); +Quoted 8 lines
Review
Quoted 6 lines
(quoted diff) +6
-0+ if (sz > bufsz) + sz = bufsz; + + memcpy(buf, efi_runtime_map, sz); + return 0; +}Could we prefix these with
efi_, e.g.efi_get_runtime_map_size()?This?
Quoted 8 lines
From: Andrew Morton <akpm-de/[email protected]>
Subject: kexec-support-kexec-kdump-on-efi-systems-fixs/get_efi/efi_get/g, per Matt
Cc Vivek Goyal <[email protected]>Cc Matt Fleming <matt-HNK1S37rvNbeXh+fF434Mdi2O/[email protected]>Signed-off-by Andrew Morton <akpm-de/[email protected]>
Looks good to me. Thanks Andrew.
Vivek
Quoted 84 lines
---
arch/x86/kernel/kexec-bzimage64.c | 4 ++-- drivers/firmware/efi/runtime-map.c | 6 +++--- include/linux/efi.h | 8 ++++---- 3 files changed, 9 insertions(+), 9 deletions(-)diff -puN
arch/x86/kernel/kexec-bzimage64.c~kexec-support-kexec-kdump-on-efi-systems-fixarch/x86/kernel/kexec-bzimage64.carch/x86/kernel/kexec-bzimage64.c renamed from arch/x86/kernel/kexec-bzimage64.c~kexec-support-kexec-kdump-on-efi-systems-fix +2
-2@@ -181,7 +181,7 @@ setup_efi_state(struct boot_params *para ei->efi_systab_hi = current_ei->efi_systab_hi; ei->efi_memdesc_version = current_ei->efi_memdesc_version; - ei->efi_memdesc_size =get_efi_runtime_map_desc_size (); + ei->efi_memdesc_size =efi_get_runtime_map_desc_size ();setup_efi_info_memmap (params, params_load_addr, efi_map_offset, efi_map_sz);@@ -397,7 +397,7 @@ void *bzImage64_load(struct kimage *imag* have to create separate segment for each. Keeps things * little bit simple */ - efi_map_sz =get_efi_runtime_map_size (); + efi_map_sz =efi_get_runtime_map_size (); efi_map_sz =ALIGN (efi_map_sz,16 ); params_cmdline_sz =sizeof (struct boot_params ) + cmdline_len +MAX_ELFCOREHDR_STR_LEN ;diff -puN
drivers/firmware/efi/runtime-map.c~kexec-support-kexec-kdump-on-efi-systems-fixdrivers/firmware/efi/runtime-map.cdrivers/firmware/efi/runtime-map.c renamed from drivers/firmware/efi/runtime-map.c~kexec-support-kexec-kdump-on-efi-systems-fix +3
-3@@ -138,19 +138,19 @@ add_sysfs_runtime_map_entry(struct kobjereturn entry; } -int get_efi_runtime_map_size (void ) +int efi_get_runtime_map_size (void ) {return nr_efi_runtime_map * efi_memdesc_size; } -int get_efi_runtime_map_desc_size (void ) +int efi_get_runtime_map_desc_size (void ) {return efi_memdesc_size; }int efi_runtime_map_copy (void *buf,size_t bufsz) { -size_t sz =get_efi_runtime_map_size (); +size_t sz =efi_get_runtime_map_size ();if (sz > bufsz) sz = bufsz;diff -puN
include/linux/efi.h~kexec-support-kexec-kdump-on-efi-systems-fixinclude/linux/efi.hinclude/linux/efi.h renamed from include/linux/efi.h~kexec-support-kexec-kdump-on-efi-systems-fix +4
-4@@ -1151,8 +1151,8 @@ int efivars_sysfs_init(void);#ifdef CONFIG_EFI_RUNTIME_MAP int efi_runtime_map_init (struct kobject *);void efi_runtime_map_setup (void *,int ,u32 ); -int get_efi_runtime_map_size (void ); -int get_efi_runtime_map_desc_size (void ); +int efi_get_runtime_map_size (void ); +int efi_get_runtime_map_desc_size (void );int efi_runtime_map_copy (void *buf,size_t bufsz);#else static inline int efi_runtime_map_init (struct kobject *kobj)@@ -1163,12 +1163,12 @@ static inline int efi_runtime_map_init(sstatic inline void efi_runtime_map_setup (void *map,int nr_entries,u32 desc_size) {} -static inline int get_efi_runtime_map_size (void ) +static inline int efi_get_runtime_map_size (void ) {return 0 ; } -static inline int get_efi_runtime_map_desc_size (void ) +static inline int efi_get_runtime_map_desc_size (void ) {return 0 ; }_
Re: [PATCH 15/15] kexec: Support kexec/kdump on EFI systems
In reply to Andrew MortonQuoted 11 lines · Andrew Morton
This?
From: Andrew Morton <akpm-de/[email protected]>
Subject: kexec-support-kexec-kdump-on-efi-systems-fixs/get_efi/efi_get/g, per Matt
Cc Vivek Goyal <[email protected]>Cc Matt Fleming <matt-HNK1S37rvNbeXh+fF434Mdi2O/[email protected]>Signed-off-by Andrew Morton <akpm-de/[email protected]>
Yep, looks spot on. Thanks Andrew.
Signature · 1 line
Matt Fleming, Intel Open Source Technology Center
[PATCH 17/15] kexec-bzimage: Change EFI helper function names
In reply to Vivek GoyalMatt suggested to change helper function names newly introduced functions and prefix these with efi_.
--- arch/x86/kernel/kexec-bzimage64.c | 4 ++-- drivers/firmware/efi/runtime-map.c | 6 +++--- include/linux/efi.h | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-)
include/linux/efi.h +2 -2
@@ -1151,8 +1151,8 @@ int efivars_sysfs_init(void);#ifdef CONFIG_EFI_RUNTIME_MAP int efi_runtime_map_init (struct kobject *);void efi_runtime_map_setup (void *,int ,u32 ); -int get_efi_runtime_map_size (void ); -int get_efi_runtime_map_desc_size (void ); +int efi_get_runtime_map_size (void ); +int efi_get_runtime_map_desc_size (void );int efi_runtime_map_copy (void *buf,size_t bufsz);#else static inline int efi_runtime_map_init (struct kobject *kobj)
drivers/firmware/efi/runtime-map.c +3 -3
@@ -138,19 +138,19 @@ add_sysfs_runtime_map_entry(struct kobjereturn entry; } -int get_efi_runtime_map_size (void ) +int efi_get_runtime_map_size (void ) {return nr_efi_runtime_map * efi_memdesc_size; } -int get_efi_runtime_map_desc_size (void ) +int efi_get_runtime_map_desc_size (void ) {return efi_memdesc_size; }int efi_runtime_map_copy (void *buf,size_t bufsz) { -size_t sz =get_efi_runtime_map_size (); +size_t sz =efi_get_runtime_map_size ();if (sz > bufsz) sz = bufsz;
arch/x86/kernel/kexec-bzimage64.c +2 -2
@@ -181,7 +181,7 @@ setup_efi_state(struct boot_params *para ei->efi_systab_hi = current_ei->efi_systab_hi; ei->efi_memdesc_version = current_ei->efi_memdesc_version; - ei->efi_memdesc_size =get_efi_runtime_map_desc_size (); + ei->efi_memdesc_size =efi_get_runtime_map_desc_size ();setup_efi_info_memmap (params, params_load_addr, efi_map_offset, efi_map_sz);
@@ -397,7 +397,7 @@ void *bzImage64_load(struct kimage *imag* have to create separate segment for each. Keeps things * little bit simple */ - efi_map_sz =get_efi_runtime_map_size (); + efi_map_sz =efi_get_runtime_map_size (); efi_map_sz =ALIGN (efi_map_sz,16 ); params_cmdline_sz =sizeof (struct boot_params ) + cmdline_len +MAX_ELFCOREHDR_STR_LEN ;
Quoted 8 lines · Vivek Goyal
Hi,
This is V4 of the patchset. Previous versions were posted here.
V1: https://lkml.org/lkml/2013/11/20/540
V2: https://lkml.org/lkml/2014/1/27/331
V3: https://lkml.org/lkml/2014/6/3/432
I used following kexec-tools patch to test my changes.
Thanks
Vivek
kexec-tools: Provide an option to make use of new system call
This patch provides and option --kexec-file-syscall, to force use of new system call for kexec. Default is to continue to use old syscall.
--- kexec/arch/x86_64/kexec-bzImage64.c | 86 +++++++++++++++++++++++ kexec/kexec-syscall.h | 32 ++++++++ kexec/kexec.c | 132 +++++++++++++++++++++++++++++++++++- kexec/kexec.h | 11 ++- 4 files changed, 257 insertions(+), 4 deletions(-)
kexec/kexec.c +129 -3
@@ -51,6 +51,8 @@unsigned long long mem_min =0 ;unsigned long long mem_max =ULONG_MAX ;static unsigned long kexec_flags =0 ; +/* Flags for kexec file (fd) based syscall */ +static unsigned long kexec_file_flags =0 ;int kexec_debug =0 ;void dbgprint_mem_range (const char *prefix,struct memory_range *mr,int nr_mr)
@@ -787,6 +789,19 @@ static int my_load(const char *type, intreturn result; } +static int kexec_file_unload (unsigned long kexec_file_flags) +{ +int ret =0 ; + + ret =kexec_file_load (-1 ,-1 ,0 ,NULL , kexec_file_flags); +if (ret !=0 ) { +/* The unload failed, print some debugging information */ +fprintf (stderr,"kexec_file_load(unload) failed\n: %s\n" , +strerror (errno)); + } +return ret; +} +static int k_unload (unsigned long kexec_flags) {int result;
@@ -925,6 +940,7 @@ void usage(void)" (0 means it's not jump back or\n" " preserve context)\n" " to original kernel.\n" +" -s --kexec-file-syscall Use file based syscall for kexec operation\n" " -d, --debug Enable debugging to help spot a failure.\n" "\n" "Supported kernel file types and options: \n");
@@ -1072,6 +1088,82 @@ char *concat_cmdline(const char *base, creturn cmdline; } +/* New file based kexec system call related code */ +static int do_kexec_file_load (int fileind,int argc,char **argv, +unsigned long flags) { + +char *kernel; +int kernel_fd, i; +struct kexec_info info; +int ret =0 ; +char *kernel_buf; +off_t kernel_size; + +memset (&info,0 ,sizeof (info)); + info.segment =NULL ; + info.nr_segments =0 ; + info.entry =NULL ; + info.backup_start =0 ; + info.kexec_flags = flags; + + info.file_mode =1 ; + info.initrd_fd =-1 ; + +if (argc - fileind <=0 ) { +fprintf (stderr,"No kernel specified\n" ); +usage (); +return -1 ; + } + + kernel = argv[fileind]; + + kernel_fd =open (kernel,O_RDONLY ); +if (kernel_fd ==-1 ) { +fprintf (stderr,"Failed to open file %s:%s\n" , kernel, +strerror (errno)); +return -1 ; + } + +/* slurp in the input kernel */ + kernel_buf =slurp_decompress_file (kernel, &kernel_size); + +for (i =0 ; i < file_types; i++) { +if (file_type[i].probe (kernel_buf, kernel_size) >=0 ) +break ; + } + +if (i == file_types) { +fprintf (stderr,"Cannot determine the file type " "of %s\n" , + kernel); +return -1 ; + } + + ret = file_type[i].load (argc, argv, kernel_buf, kernel_size, &info); +if (ret <0 ) { +fprintf (stderr,"Cannot load %s\n" , kernel); +return ret; + } + +if (!is_kexec_file_load_implemented ()) { +fprintf (stderr,"syscall kexec_file_load not available.\n" ); +return -1 ; + } + +/* +* If there is no initramfs, set KEXEC_FILE_NO_INITRAMFS flag so that +* kernel does not return error with negative initrd_fd. +*/ +if (info.initrd_fd ==-1 ) + info.kexec_flags |=KEXEC_FILE_NO_INITRAMFS ; + + ret =kexec_file_load (kernel_fd, info.initrd_fd, info.command_line_len, + info.command_line, info.kexec_flags); +if (ret !=0 ) +fprintf (stderr,"kexec_file_load failed: %s\n" , +strerror (errno)); +return ret; +} +int main (int argc,char *argv[]) {
@@ -1083,6 +1175,7 @@ int main(int argc, char *argv[])int do_ifdown =0 ;int do_unload =0 ;int do_reuse_initrd =0 ; +int do_kexec_file_syscall =0 ;void *entry =0 ;char *type =0 ;char *endptr;
@@ -1095,6 +1188,23 @@ int main(int argc, char *argv[]) };static const char short_options[] =KEXEC_ALL_OPT_STR ; +/* +* First check if --use-kexec-file-syscall is set. That changes lot of +* things +*/ +while ((opt =getopt_long (argc, argv, short_options, + options,0 )) !=-1 ) { +switch (opt) { +case OPT_KEXEC_FILE_SYSCALL : + do_kexec_file_syscall =1 ; +break ; + } + } + +/* Reset getopt for the next pass. */ + opterr =1 ; + optind =1 ; +while ((opt =getopt_long (argc, argv, short_options, options,0 )) !=-1 ) {switch (opt) {
@@ -1127,6 +1237,8 @@ int main(int argc, char *argv[]) do_shutdown =0 ; do_sync =0 ; do_unload =1 ; +if (do_kexec_file_syscall) + kexec_file_flags |=KEXEC_FILE_UNLOAD ;break ;case OPT_EXEC : do_load =0 ;
@@ -1169,7 +1281,10 @@ int main(int argc, char *argv[]) do_exec =0 ; do_shutdown =0 ; do_sync =0 ; - kexec_flags =KEXEC_ON_CRASH ; +if (do_kexec_file_syscall) + kexec_file_flags |=KEXEC_FILE_ON_CRASH ; +else + kexec_flags =KEXEC_ON_CRASH ;break ;case OPT_MEM_MIN : mem_min =strtoul (optarg, &endptr,0 );
@@ -1194,6 +1309,9 @@ int main(int argc, char *argv[])case OPT_REUSE_INITRD : do_reuse_initrd =1 ;break ; +case OPT_KEXEC_FILE_SYSCALL : +/* We already parsed it. Nothing to do. */ +break ;default :break ; }
@@ -1238,10 +1356,18 @@ int main(int argc, char *argv[]) }if (do_unload) { - result =k_unload (kexec_flags); +if (do_kexec_file_syscall) + result =kexec_file_unload (kexec_file_flags); +else + result =k_unload (kexec_flags); }if (do_load && (result ==0 )) { - result =my_load (type, fileind, argc, argv, kexec_flags, entry); +if (do_kexec_file_syscall) + result =do_kexec_file_load (fileind, argc, argv, + kexec_file_flags); +else + result =my_load (type, fileind, argc, argv, + kexec_flags, entry); }/* Don't shutdown unless there is something to reboot to! */ if ((result ==0 ) && (do_shutdown || do_exec) && !kexec_loaded ()) {
kexec/kexec.h +10 -1
@@ -156,6 +156,13 @@ struct kexec_info {unsigned long kexec_flags;unsigned long backup_src_start;unsigned long backup_src_size; +/* Set to 1 if we are using kexec file syscall */ + unsigned long file_mode :1; + +/* Filled by kernel image processing code */ +int initrd_fd; +char *command_line; +int command_line_len; };struct arch_map_entry {
@@ -207,6 +214,7 @@ extern int file_types;#define OPT_UNLOAD 'u'#define OPT_TYPE 't'#define OPT_PANIC 'p' +#define OPT_KEXEC_FILE_SYSCALL 's'#define OPT_MEM_MIN 256#define OPT_MEM_MAX 257#define OPT_REUSE_INITRD 258
@@ -230,9 +238,10 @@ extern int file_types; { "mem-min", 1, 0, OPT_MEM_MIN }, \ { "mem-max", 1, 0, OPT_MEM_MAX }, \ { "reuseinitrd", 0, 0, OPT_REUSE_INITRD }, \ + { "kexec-file-syscall", 0, 0, OPT_KEXEC_FILE_SYSCALL }, \ { "debug", 0, 0, OPT_DEBUG }, \ -#define KEXEC_OPT_STR "h?vdfxluet:p" +#define KEXEC_OPT_STR "h?vdfxluet:ps" extern void dbgprint_mem_range(const char *prefix, struct memory_range *mr, int nr_mr);extern void die (const char *fmt, ...)
kexec/arch/x86_64/kexec-bzImage64.c +86 -0
98 lines not on this page · raw message
kexec/kexec-syscall.h +32 -0
@@ -53,6 +53,19 @@#endif #endif /*ifndef __NR_kexec_load*/ +#ifndef __NR_kexec_file_load + +#ifdef __x86_64__ +#define __NR_kexec_file_load 317 +#endif + +#ifndef __NR_kexec_file_load +/* system call not available for the arch */ +#define __NR_kexec_file_load 0xffffffff/* system call not available */ +#endif + +#endif /*ifndef __NR_kexec_file_load*/ +struct kexec_segment ; static inline long kexec_load(void *entry, unsigned long nr_segments,
@@ -61,10 +74,29 @@ static inline long kexec_load(void *entrreturn (long )syscall (__NR_kexec_load, entry, nr_segments, segments, flags); } +static inline int is_kexec_file_load_implemented (void ) { +if (__NR_kexec_file_load !=0xffffffff ) +return 1 ; +return 0 ; +} + +static inline long kexec_file_load (int kernel_fd,int initrd_fd, +unsigned long cmdline_len,const char *cmdline_ptr, +unsigned long flags) +{ +return (long )syscall (__NR_kexec_file_load, kernel_fd, initrd_fd, + cmdline_len, cmdline_ptr, flags); +} +#define KEXEC_ON_CRASH 0x00000001#define KEXEC_PRESERVE_CONTEXT 0x00000002#define KEXEC_ARCH_MASK 0xffff0000 +/* Flags for kexec file based system call */ +#define KEXEC_FILE_UNLOAD 0x00000001 +#define KEXEC_FILE_ON_CRASH 0x00000002 +#define KEXEC_FILE_NO_INITRAMFS 0x00000004 +/* These values match the ELF architecture values. * Unless there is a good reason that should continue to be the case. */
_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
Andrew Morton
maintainerQuoted 17 lines · On Thu, 26 Jun 2014 16:33:29 -0400 Vivek Goyal <[email protected]> wrote:
This patch series does not do kernel signature verification yet. I plan to post another patch series for that. Now distributions are already signing PE/COFF bzImage with PKCS7 signature I plan to parse and verify those signatures.
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.
Quoted 4 lines
I hope these patches can be queued up for 3.17. Even without signature verification support, they provide new syscall functionality. But I wil leave it to maintainers to decide if they want signature verification support also be ready to merge before they merge this patchset.
Well, this is an absolute ton of new code, much of it pretty complex. And I believe the entire point of this work is to enable image signature checking, but that hasn't been implemented yet?
In which case I'm thinking it would be unwise to merge these parts into mainline - if signature checking doesn't work or fails review or if you get hit by a bus then we'd be left with a large lump of rather useless code?
In which case I'm inclined to put this series into -next and keep it there pending completion of the signature checking part.
Borislav Petkov
maintainerQuoted 11 lines · Andrew Morton
Well, this is an absolute ton of new code, much of it pretty complex. And I believe the entire point of this work is to enable image signature checking, but that hasn't been implemented yet?
In which case I'm thinking it would be unwise to merge these parts into mainline - if signature checking doesn't work or fails review or if you get hit by a bus then we'd be left with a large lump of rather useless code?
In which case I'm inclined to put this series into -next and keep it there pending completion of the signature checking part.
Before we rush these in, it'd be nice if someone more experienced would take a look at the general approach of the whole handling with the purgatory and such. I certainly tried to give my best while reviewing but I'm too inexperienced in kexec and the whole booting of another kernel and all the intricacies of the process. It'll be optimal if Eric Biederman would find some free time for those.
AFAIK, hpa wanted to take a look too so can we please slow down a bit here first?
Thanks.
Signature · 5 lines
Regards/Gruss,
Boris.
Sent from a fat crate under my desk. Formatting is fine.
--Quoted 19 lines · Andrew Morton
Quoted 17 lines · On Thu, 26 Jun 2014 16:33:29 -0400 Vivek Goyal <[email protected]> wrote:
This patch series does not do kernel signature verification yet. I plan to post another patch series for that. Now distributions are already signing PE/COFF bzImage with PKCS7 signature I plan to parse and verify those signatures.
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.
Quoted 8 lines
Quoted 4 lines
I hope these patches can be queued up for 3.17. Even without signature verification support, they provide new syscall functionality. But I wil leave it to maintainers to decide if they want signature verification support also be ready to merge before they merge this patchset.
Well, this is an absolute ton of new code, much of it pretty complex. And I believe the entire point of this work is to enable image signature checking, but that hasn't been implemented yet?
I have a patchset which works. But it requires more work. I will do remaining work and clean it up and post for review.
Quoted 8 lines
In which case I'm thinking it would be unwise to merge these parts into mainline - if signature checking doesn't work or fails review or if you get hit by a bus then we'd be left with a large lump of rather useless code?
In which case I'm inclined to put this series into -next and keep it there pending completion of the signature checking part.
Agreed. Primary purpose of this patch series is to be able to do signature verification of kernel during kexec. So it will make sense to first have some sort of consensus on signature verification patches. Otherwise we might be stuck with this 3.5K lines of code if things go south w.r.t signature verification.
Thanks
Vivek
_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
[PATCH 16/15] kexec: Fix freeing up for image loader data loading
In reply to Vivek GoyalDuring testing I noticed a crash. Which in turn showed that there are problems with how I am freeing up image->image_loader_data.
In one case I am freeing up kimage->image_loader_data first and then calling up arch to free up which might have been contained in that structure. That's wrong.
I have done little cleanup and this should fix the issues around freeing up of loader data.
--- arch/x86/kernel/kexec-bzimage64.c | 4 ++-- arch/x86/kernel/machine_kexec_64.c | 2 +- include/linux/kexec.h | 2 +- kernel/kexec.c | 11 ++++++++--- 4 files changed, 12 insertions(+), 7 deletions(-)
arch/x86/kernel/machine_kexec_64.c +1 -1
@@ -369,7 +369,7 @@ int arch_kimage_file_post_load_cleanup(sif (!image->fops || !image->fops->cleanup)return 0 ; -return image->fops->cleanup (image); +return image->fops->cleanup (image->image_loader_data); }/*
include/linux/kexec.h +1 -1
@@ -190,7 +190,7 @@ typedef void *(kexec_load_t)(struct kima unsigned long kernel_len, char *initrd, unsigned long initrd_len, char *cmdline, unsigned long cmdline_len); -typedef int (kexec_cleanup_t )(struct kimage *image); +typedef int (kexec_cleanup_t )(void *loader_data);struct kexec_file_ops {kexec_probe_t *probe;
arch/x86/kernel/kexec-bzimage64.c +2 -2
@@ -512,9 +512,9 @@ out_free_params: }/* This cleanup function is called after various segments have been loaded */ -int bzImage64_cleanup (struct kimage *image) +int bzImage64_cleanup (void *loader_data) { -struct bzimage64_data *ldata = image->image_loader_data; +struct bzimage64_data *ldata = loader_data;if (!ldata)return 0 ;
kernel/kexec.c +8 -3
@@ -459,6 +459,14 @@ static void kimage_file_post_load_cleanu/* See if architecture has anything to cleanup post load */ arch_kimage_file_post_load_cleanup (image); + +/* +* Above call should have called into bootloader to free up +* any data stored in kimage->image_loader_data. It should +* be ok now to free it up. +*/ +kfree (image->image_loader_data); + image->image_loader_data =NULL ; }/*
@@ -584,7 +592,6 @@ out_free_control_pages:kimage_free_page_list (&image->control_pages); out_free_post_load_bufs:kimage_file_post_load_cleanup (image); -kfree (image->image_loader_data); out_free_image:kfree (image);return ret;
@@ -908,8 +915,6 @@ static void kimage_free(struct kimage *i/* Free the kexec control pages... */ kimage_free_page_list (&image->control_pages); -kfree (image->image_loader_data); -/* * Free up any temporary buffers allocated. This might hit if * error occurred much later after buffer allocation.
_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec