2 * kexec.c - kexec system call
3 * Copyright (C) 2002-2004 Eric Biederman <ebiederm@xmission.com>
5 * This source code is licensed under the GNU General Public License,
6 * Version 2. See the file COPYING for more details.
9 #define pr_fmt(fmt) "kexec: " fmt
11 #include <linux/capability.h>
13 #include <linux/file.h>
14 #include <linux/slab.h>
16 #include <linux/kexec.h>
17 #include <linux/mutex.h>
18 #include <linux/list.h>
19 #include <linux/highmem.h>
20 #include <linux/syscalls.h>
21 #include <linux/reboot.h>
22 #include <linux/ioport.h>
23 #include <linux/hardirq.h>
24 #include <linux/elf.h>
25 #include <linux/elfcore.h>
26 #include <linux/utsname.h>
27 #include <linux/numa.h>
28 #include <linux/suspend.h>
29 #include <linux/device.h>
30 #include <linux/freezer.h>
32 #include <linux/cpu.h>
33 #include <linux/console.h>
34 #include <linux/vmalloc.h>
35 #include <linux/swap.h>
36 #include <linux/syscore_ops.h>
37 #include <linux/compiler.h>
38 #include <linux/hugetlb.h>
41 #include <asm/uaccess.h>
43 #include <asm/sections.h>
45 #include <crypto/hash.h>
46 #include <crypto/sha.h>
48 /* Per cpu memory for storing cpu states in case of system crash. */
49 note_buf_t __percpu *crash_notes;
51 /* vmcoreinfo stuff */
52 static unsigned char vmcoreinfo_data[VMCOREINFO_BYTES];
53 u32 vmcoreinfo_note[VMCOREINFO_NOTE_SIZE/4];
54 size_t vmcoreinfo_size;
55 size_t vmcoreinfo_max_size = sizeof(vmcoreinfo_data);
57 /* Flag to indicate we are going to kexec a new kernel */
58 bool kexec_in_progress = false;
61 * Declare these symbols weak so that if architecture provides a purgatory,
62 * these will be overridden.
64 char __weak kexec_purgatory[0];
65 size_t __weak kexec_purgatory_size = 0;
67 static int kexec_calculate_store_digests(struct kimage *image);
69 /* Location of the reserved area for the crash kernel */
70 struct resource crashk_res = {
71 .name = "Crash kernel",
74 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
76 struct resource crashk_low_res = {
77 .name = "Crash kernel",
80 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
83 int kexec_should_crash(struct task_struct *p)
85 if (in_interrupt() || !p->pid || is_global_init(p) || panic_on_oops)
91 * When kexec transitions to the new kernel there is a one-to-one
92 * mapping between physical and virtual addresses. On processors
93 * where you can disable the MMU this is trivial, and easy. For
94 * others it is still a simple predictable page table to setup.
96 * In that environment kexec copies the new kernel to its final
97 * resting place. This means I can only support memory whose
98 * physical address can fit in an unsigned long. In particular
99 * addresses where (pfn << PAGE_SHIFT) > ULONG_MAX cannot be handled.
100 * If the assembly stub has more restrictive requirements
101 * KEXEC_SOURCE_MEMORY_LIMIT and KEXEC_DEST_MEMORY_LIMIT can be
102 * defined more restrictively in <asm/kexec.h>.
104 * The code for the transition from the current kernel to the
105 * the new kernel is placed in the control_code_buffer, whose size
106 * is given by KEXEC_CONTROL_PAGE_SIZE. In the best case only a single
107 * page of memory is necessary, but some architectures require more.
108 * Because this memory must be identity mapped in the transition from
109 * virtual to physical addresses it must live in the range
110 * 0 - TASK_SIZE, as only the user space mappings are arbitrarily
113 * The assembly stub in the control code buffer is passed a linked list
114 * of descriptor pages detailing the source pages of the new kernel,
115 * and the destination addresses of those source pages. As this data
116 * structure is not used in the context of the current OS, it must
119 * The code has been made to work with highmem pages and will use a
120 * destination page in its final resting place (if it happens
121 * to allocate it). The end product of this is that most of the
122 * physical address space, and most of RAM can be used.
124 * Future directions include:
125 * - allocating a page table with the control code buffer identity
126 * mapped, to simplify machine_kexec and make kexec_on_panic more
131 * KIMAGE_NO_DEST is an impossible destination address..., for
132 * allocating pages whose destination address we do not care about.
134 #define KIMAGE_NO_DEST (-1UL)
136 static int kimage_is_destination_range(struct kimage *image,
137 unsigned long start, unsigned long end);
138 static struct page *kimage_alloc_page(struct kimage *image,
142 static int copy_user_segment_list(struct kimage *image,
143 unsigned long nr_segments,
144 struct kexec_segment __user *segments)
147 size_t segment_bytes;
149 /* Read in the segments */
150 image->nr_segments = nr_segments;
151 segment_bytes = nr_segments * sizeof(*segments);
152 ret = copy_from_user(image->segment, segments, segment_bytes);
159 static int sanity_check_segment_list(struct kimage *image)
162 unsigned long nr_segments = image->nr_segments;
165 * Verify we have good destination addresses. The caller is
166 * responsible for making certain we don't attempt to load
167 * the new image into invalid or reserved areas of RAM. This
168 * just verifies it is an address we can use.
170 * Since the kernel does everything in page size chunks ensure
171 * the destination addresses are page aligned. Too many
172 * special cases crop of when we don't do this. The most
173 * insidious is getting overlapping destination addresses
174 * simply because addresses are changed to page size
177 result = -EADDRNOTAVAIL;
178 for (i = 0; i < nr_segments; i++) {
179 unsigned long mstart, mend;
181 mstart = image->segment[i].mem;
182 mend = mstart + image->segment[i].memsz;
183 if ((mstart & ~PAGE_MASK) || (mend & ~PAGE_MASK))
185 if (mend >= KEXEC_DESTINATION_MEMORY_LIMIT)
189 /* Verify our destination addresses do not overlap.
190 * If we alloed overlapping destination addresses
191 * through very weird things can happen with no
192 * easy explanation as one segment stops on another.
195 for (i = 0; i < nr_segments; i++) {
196 unsigned long mstart, mend;
199 mstart = image->segment[i].mem;
200 mend = mstart + image->segment[i].memsz;
201 for (j = 0; j < i; j++) {
202 unsigned long pstart, pend;
203 pstart = image->segment[j].mem;
204 pend = pstart + image->segment[j].memsz;
205 /* Do the segments overlap ? */
206 if ((mend > pstart) && (mstart < pend))
211 /* Ensure our buffer sizes are strictly less than
212 * our memory sizes. This should always be the case,
213 * and it is easier to check up front than to be surprised
217 for (i = 0; i < nr_segments; i++) {
218 if (image->segment[i].bufsz > image->segment[i].memsz)
223 * Verify we have good destination addresses. Normally
224 * the caller is responsible for making certain we don't
225 * attempt to load the new image into invalid or reserved
226 * areas of RAM. But crash kernels are preloaded into a
227 * reserved area of ram. We must ensure the addresses
228 * are in the reserved area otherwise preloading the
229 * kernel could corrupt things.
232 if (image->type == KEXEC_TYPE_CRASH) {
233 result = -EADDRNOTAVAIL;
234 for (i = 0; i < nr_segments; i++) {
235 unsigned long mstart, mend;
237 mstart = image->segment[i].mem;
238 mend = mstart + image->segment[i].memsz - 1;
239 /* Ensure we are within the crash kernel limits */
240 if ((mstart < crashk_res.start) ||
241 (mend > crashk_res.end))
249 static struct kimage *do_kimage_alloc_init(void)
251 struct kimage *image;
253 /* Allocate a controlling structure */
254 image = kzalloc(sizeof(*image), GFP_KERNEL);
259 image->entry = &image->head;
260 image->last_entry = &image->head;
261 image->control_page = ~0; /* By default this does not apply */
262 image->type = KEXEC_TYPE_DEFAULT;
264 /* Initialize the list of control pages */
265 INIT_LIST_HEAD(&image->control_pages);
267 /* Initialize the list of destination pages */
268 INIT_LIST_HEAD(&image->dest_pages);
270 /* Initialize the list of unusable pages */
271 INIT_LIST_HEAD(&image->unusable_pages);
276 static void kimage_free_page_list(struct list_head *list);
278 static int kimage_alloc_init(struct kimage **rimage, unsigned long entry,
279 unsigned long nr_segments,
280 struct kexec_segment __user *segments,
284 struct kimage *image;
285 bool kexec_on_panic = flags & KEXEC_ON_CRASH;
287 if (kexec_on_panic) {
288 /* Verify we have a valid entry point */
289 if ((entry < crashk_res.start) || (entry > crashk_res.end))
290 return -EADDRNOTAVAIL;
293 /* Allocate and initialize a controlling structure */
294 image = do_kimage_alloc_init();
298 image->start = entry;
300 ret = copy_user_segment_list(image, nr_segments, segments);
304 ret = sanity_check_segment_list(image);
308 /* Enable the special crash kernel control page allocation policy. */
309 if (kexec_on_panic) {
310 image->control_page = crashk_res.start;
311 image->type = KEXEC_TYPE_CRASH;
315 * Find a location for the control code buffer, and add it
316 * the vector of segments so that it's pages will also be
317 * counted as destination pages.
320 image->control_code_page = kimage_alloc_control_pages(image,
321 get_order(KEXEC_CONTROL_PAGE_SIZE));
322 if (!image->control_code_page) {
323 pr_err("Could not allocate control_code_buffer\n");
327 if (!kexec_on_panic) {
328 image->swap_page = kimage_alloc_control_pages(image, 0);
329 if (!image->swap_page) {
330 pr_err("Could not allocate swap buffer\n");
331 goto out_free_control_pages;
337 out_free_control_pages:
338 kimage_free_page_list(&image->control_pages);
344 static int copy_file_from_fd(int fd, void **buf, unsigned long *buf_len)
346 struct fd f = fdget(fd);
355 ret = vfs_getattr(&f.file->f_path, &stat);
359 if (stat.size > INT_MAX) {
364 /* Don't hand 0 to vmalloc, it whines. */
365 if (stat.size == 0) {
370 *buf = vmalloc(stat.size);
377 while (pos < stat.size) {
378 bytes = kernel_read(f.file, pos, (char *)(*buf) + pos,
391 if (pos != stat.size) {
403 /* Architectures can provide this probe function */
404 int __weak arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
405 unsigned long buf_len)
410 void * __weak arch_kexec_kernel_image_load(struct kimage *image)
412 return ERR_PTR(-ENOEXEC);
415 void __weak arch_kimage_file_post_load_cleanup(struct kimage *image)
419 /* Apply relocations of type RELA */
421 arch_kexec_apply_relocations_add(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
424 pr_err("RELA relocation unsupported.\n");
428 /* Apply relocations of type REL */
430 arch_kexec_apply_relocations(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
433 pr_err("REL relocation unsupported.\n");
438 * Free up memory used by kernel, initrd, and comand line. This is temporary
439 * memory allocation which is not needed any more after these buffers have
440 * been loaded into separate segments and have been copied elsewhere.
442 static void kimage_file_post_load_cleanup(struct kimage *image)
444 struct purgatory_info *pi = &image->purgatory_info;
446 vfree(image->kernel_buf);
447 image->kernel_buf = NULL;
449 vfree(image->initrd_buf);
450 image->initrd_buf = NULL;
452 kfree(image->cmdline_buf);
453 image->cmdline_buf = NULL;
455 vfree(pi->purgatory_buf);
456 pi->purgatory_buf = NULL;
461 /* See if architecture has anything to cleanup post load */
462 arch_kimage_file_post_load_cleanup(image);
466 * In file mode list of segments is prepared by kernel. Copy relevant
467 * data from user space, do error checking, prepare segment list
470 kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd,
471 const char __user *cmdline_ptr,
472 unsigned long cmdline_len, unsigned flags)
477 ret = copy_file_from_fd(kernel_fd, &image->kernel_buf,
478 &image->kernel_buf_len);
482 /* Call arch image probe handlers */
483 ret = arch_kexec_kernel_image_probe(image, image->kernel_buf,
484 image->kernel_buf_len);
489 /* It is possible that there no initramfs is being loaded */
490 if (!(flags & KEXEC_FILE_NO_INITRAMFS)) {
491 ret = copy_file_from_fd(initrd_fd, &image->initrd_buf,
492 &image->initrd_buf_len);
498 image->cmdline_buf = kzalloc(cmdline_len, GFP_KERNEL);
499 if (!image->cmdline_buf) {
504 ret = copy_from_user(image->cmdline_buf, cmdline_ptr,
511 image->cmdline_buf_len = cmdline_len;
513 /* command line should be a string with last byte null */
514 if (image->cmdline_buf[cmdline_len - 1] != '\0') {
520 /* Call arch image load handlers */
521 ldata = arch_kexec_kernel_image_load(image);
524 ret = PTR_ERR(ldata);
528 image->image_loader_data = ldata;
530 /* In case of error, free up all allocated memory in this function */
532 kimage_file_post_load_cleanup(image);
537 kimage_file_alloc_init(struct kimage **rimage, int kernel_fd,
538 int initrd_fd, const char __user *cmdline_ptr,
539 unsigned long cmdline_len, unsigned long flags)
542 struct kimage *image;
544 image = do_kimage_alloc_init();
548 image->file_mode = 1;
550 ret = kimage_file_prepare_segments(image, kernel_fd, initrd_fd,
551 cmdline_ptr, cmdline_len, flags);
555 ret = sanity_check_segment_list(image);
557 goto out_free_post_load_bufs;
560 image->control_code_page = kimage_alloc_control_pages(image,
561 get_order(KEXEC_CONTROL_PAGE_SIZE));
562 if (!image->control_code_page) {
563 pr_err("Could not allocate control_code_buffer\n");
564 goto out_free_post_load_bufs;
567 image->swap_page = kimage_alloc_control_pages(image, 0);
568 if (!image->swap_page) {
569 pr_err(KERN_ERR "Could not allocate swap buffer\n");
570 goto out_free_control_pages;
575 out_free_control_pages:
576 kimage_free_page_list(&image->control_pages);
577 out_free_post_load_bufs:
578 kimage_file_post_load_cleanup(image);
579 kfree(image->image_loader_data);
585 static int kimage_is_destination_range(struct kimage *image,
591 for (i = 0; i < image->nr_segments; i++) {
592 unsigned long mstart, mend;
594 mstart = image->segment[i].mem;
595 mend = mstart + image->segment[i].memsz;
596 if ((end > mstart) && (start < mend))
603 static struct page *kimage_alloc_pages(gfp_t gfp_mask, unsigned int order)
607 pages = alloc_pages(gfp_mask, order);
609 unsigned int count, i;
610 pages->mapping = NULL;
611 set_page_private(pages, order);
613 for (i = 0; i < count; i++)
614 SetPageReserved(pages + i);
620 static void kimage_free_pages(struct page *page)
622 unsigned int order, count, i;
624 order = page_private(page);
626 for (i = 0; i < count; i++)
627 ClearPageReserved(page + i);
628 __free_pages(page, order);
631 static void kimage_free_page_list(struct list_head *list)
633 struct list_head *pos, *next;
635 list_for_each_safe(pos, next, list) {
638 page = list_entry(pos, struct page, lru);
639 list_del(&page->lru);
640 kimage_free_pages(page);
644 static struct page *kimage_alloc_normal_control_pages(struct kimage *image,
647 /* Control pages are special, they are the intermediaries
648 * that are needed while we copy the rest of the pages
649 * to their final resting place. As such they must
650 * not conflict with either the destination addresses
651 * or memory the kernel is already using.
653 * The only case where we really need more than one of
654 * these are for architectures where we cannot disable
655 * the MMU and must instead generate an identity mapped
656 * page table for all of the memory.
658 * At worst this runs in O(N) of the image size.
660 struct list_head extra_pages;
665 INIT_LIST_HEAD(&extra_pages);
667 /* Loop while I can allocate a page and the page allocated
668 * is a destination page.
671 unsigned long pfn, epfn, addr, eaddr;
673 pages = kimage_alloc_pages(GFP_KERNEL, order);
676 pfn = page_to_pfn(pages);
678 addr = pfn << PAGE_SHIFT;
679 eaddr = epfn << PAGE_SHIFT;
680 if ((epfn >= (KEXEC_CONTROL_MEMORY_LIMIT >> PAGE_SHIFT)) ||
681 kimage_is_destination_range(image, addr, eaddr)) {
682 list_add(&pages->lru, &extra_pages);
688 /* Remember the allocated page... */
689 list_add(&pages->lru, &image->control_pages);
691 /* Because the page is already in it's destination
692 * location we will never allocate another page at
693 * that address. Therefore kimage_alloc_pages
694 * will not return it (again) and we don't need
695 * to give it an entry in image->segment[].
698 /* Deal with the destination pages I have inadvertently allocated.
700 * Ideally I would convert multi-page allocations into single
701 * page allocations, and add everything to image->dest_pages.
703 * For now it is simpler to just free the pages.
705 kimage_free_page_list(&extra_pages);
710 static struct page *kimage_alloc_crash_control_pages(struct kimage *image,
713 /* Control pages are special, they are the intermediaries
714 * that are needed while we copy the rest of the pages
715 * to their final resting place. As such they must
716 * not conflict with either the destination addresses
717 * or memory the kernel is already using.
719 * Control pages are also the only pags we must allocate
720 * when loading a crash kernel. All of the other pages
721 * are specified by the segments and we just memcpy
722 * into them directly.
724 * The only case where we really need more than one of
725 * these are for architectures where we cannot disable
726 * the MMU and must instead generate an identity mapped
727 * page table for all of the memory.
729 * Given the low demand this implements a very simple
730 * allocator that finds the first hole of the appropriate
731 * size in the reserved memory region, and allocates all
732 * of the memory up to and including the hole.
734 unsigned long hole_start, hole_end, size;
738 size = (1 << order) << PAGE_SHIFT;
739 hole_start = (image->control_page + (size - 1)) & ~(size - 1);
740 hole_end = hole_start + size - 1;
741 while (hole_end <= crashk_res.end) {
744 if (hole_end > KEXEC_CRASH_CONTROL_MEMORY_LIMIT)
746 /* See if I overlap any of the segments */
747 for (i = 0; i < image->nr_segments; i++) {
748 unsigned long mstart, mend;
750 mstart = image->segment[i].mem;
751 mend = mstart + image->segment[i].memsz - 1;
752 if ((hole_end >= mstart) && (hole_start <= mend)) {
753 /* Advance the hole to the end of the segment */
754 hole_start = (mend + (size - 1)) & ~(size - 1);
755 hole_end = hole_start + size - 1;
759 /* If I don't overlap any segments I have found my hole! */
760 if (i == image->nr_segments) {
761 pages = pfn_to_page(hole_start >> PAGE_SHIFT);
766 image->control_page = hole_end;
772 struct page *kimage_alloc_control_pages(struct kimage *image,
775 struct page *pages = NULL;
777 switch (image->type) {
778 case KEXEC_TYPE_DEFAULT:
779 pages = kimage_alloc_normal_control_pages(image, order);
781 case KEXEC_TYPE_CRASH:
782 pages = kimage_alloc_crash_control_pages(image, order);
789 static int kimage_add_entry(struct kimage *image, kimage_entry_t entry)
791 if (*image->entry != 0)
794 if (image->entry == image->last_entry) {
795 kimage_entry_t *ind_page;
798 page = kimage_alloc_page(image, GFP_KERNEL, KIMAGE_NO_DEST);
802 ind_page = page_address(page);
803 *image->entry = virt_to_phys(ind_page) | IND_INDIRECTION;
804 image->entry = ind_page;
805 image->last_entry = ind_page +
806 ((PAGE_SIZE/sizeof(kimage_entry_t)) - 1);
808 *image->entry = entry;
815 static int kimage_set_destination(struct kimage *image,
816 unsigned long destination)
820 destination &= PAGE_MASK;
821 result = kimage_add_entry(image, destination | IND_DESTINATION);
823 image->destination = destination;
829 static int kimage_add_page(struct kimage *image, unsigned long page)
834 result = kimage_add_entry(image, page | IND_SOURCE);
836 image->destination += PAGE_SIZE;
842 static void kimage_free_extra_pages(struct kimage *image)
844 /* Walk through and free any extra destination pages I may have */
845 kimage_free_page_list(&image->dest_pages);
847 /* Walk through and free any unusable pages I have cached */
848 kimage_free_page_list(&image->unusable_pages);
851 static void kimage_terminate(struct kimage *image)
853 if (*image->entry != 0)
856 *image->entry = IND_DONE;
859 #define for_each_kimage_entry(image, ptr, entry) \
860 for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE); \
861 ptr = (entry & IND_INDIRECTION) ? \
862 phys_to_virt((entry & PAGE_MASK)) : ptr + 1)
864 static void kimage_free_entry(kimage_entry_t entry)
868 page = pfn_to_page(entry >> PAGE_SHIFT);
869 kimage_free_pages(page);
872 static void kimage_free(struct kimage *image)
874 kimage_entry_t *ptr, entry;
875 kimage_entry_t ind = 0;
880 kimage_free_extra_pages(image);
881 for_each_kimage_entry(image, ptr, entry) {
882 if (entry & IND_INDIRECTION) {
883 /* Free the previous indirection page */
884 if (ind & IND_INDIRECTION)
885 kimage_free_entry(ind);
886 /* Save this indirection page until we are
890 } else if (entry & IND_SOURCE)
891 kimage_free_entry(entry);
893 /* Free the final indirection page */
894 if (ind & IND_INDIRECTION)
895 kimage_free_entry(ind);
897 /* Handle any machine specific cleanup */
898 machine_kexec_cleanup(image);
900 /* Free the kexec control pages... */
901 kimage_free_page_list(&image->control_pages);
903 kfree(image->image_loader_data);
906 * Free up any temporary buffers allocated. This might hit if
907 * error occurred much later after buffer allocation.
909 if (image->file_mode)
910 kimage_file_post_load_cleanup(image);
915 static kimage_entry_t *kimage_dst_used(struct kimage *image,
918 kimage_entry_t *ptr, entry;
919 unsigned long destination = 0;
921 for_each_kimage_entry(image, ptr, entry) {
922 if (entry & IND_DESTINATION)
923 destination = entry & PAGE_MASK;
924 else if (entry & IND_SOURCE) {
925 if (page == destination)
927 destination += PAGE_SIZE;
934 static struct page *kimage_alloc_page(struct kimage *image,
936 unsigned long destination)
939 * Here we implement safeguards to ensure that a source page
940 * is not copied to its destination page before the data on
941 * the destination page is no longer useful.
943 * To do this we maintain the invariant that a source page is
944 * either its own destination page, or it is not a
945 * destination page at all.
947 * That is slightly stronger than required, but the proof
948 * that no problems will not occur is trivial, and the
949 * implementation is simply to verify.
951 * When allocating all pages normally this algorithm will run
952 * in O(N) time, but in the worst case it will run in O(N^2)
953 * time. If the runtime is a problem the data structures can
960 * Walk through the list of destination pages, and see if I
963 list_for_each_entry(page, &image->dest_pages, lru) {
964 addr = page_to_pfn(page) << PAGE_SHIFT;
965 if (addr == destination) {
966 list_del(&page->lru);
974 /* Allocate a page, if we run out of memory give up */
975 page = kimage_alloc_pages(gfp_mask, 0);
978 /* If the page cannot be used file it away */
979 if (page_to_pfn(page) >
980 (KEXEC_SOURCE_MEMORY_LIMIT >> PAGE_SHIFT)) {
981 list_add(&page->lru, &image->unusable_pages);
984 addr = page_to_pfn(page) << PAGE_SHIFT;
986 /* If it is the destination page we want use it */
987 if (addr == destination)
990 /* If the page is not a destination page use it */
991 if (!kimage_is_destination_range(image, addr,
996 * I know that the page is someones destination page.
997 * See if there is already a source page for this
998 * destination page. And if so swap the source pages.
1000 old = kimage_dst_used(image, addr);
1003 unsigned long old_addr;
1004 struct page *old_page;
1006 old_addr = *old & PAGE_MASK;
1007 old_page = pfn_to_page(old_addr >> PAGE_SHIFT);
1008 copy_highpage(page, old_page);
1009 *old = addr | (*old & ~PAGE_MASK);
1011 /* The old page I have found cannot be a
1012 * destination page, so return it if it's
1013 * gfp_flags honor the ones passed in.
1015 if (!(gfp_mask & __GFP_HIGHMEM) &&
1016 PageHighMem(old_page)) {
1017 kimage_free_pages(old_page);
1024 /* Place the page on the destination list I
1025 * will use it later.
1027 list_add(&page->lru, &image->dest_pages);
1034 static int kimage_load_normal_segment(struct kimage *image,
1035 struct kexec_segment *segment)
1037 unsigned long maddr;
1038 size_t ubytes, mbytes;
1040 unsigned char __user *buf = NULL;
1041 unsigned char *kbuf = NULL;
1044 if (image->file_mode)
1045 kbuf = segment->kbuf;
1048 ubytes = segment->bufsz;
1049 mbytes = segment->memsz;
1050 maddr = segment->mem;
1052 result = kimage_set_destination(image, maddr);
1059 size_t uchunk, mchunk;
1061 page = kimage_alloc_page(image, GFP_HIGHUSER, maddr);
1066 result = kimage_add_page(image, page_to_pfn(page)
1072 /* Start with a clear page */
1074 ptr += maddr & ~PAGE_MASK;
1075 mchunk = min_t(size_t, mbytes,
1076 PAGE_SIZE - (maddr & ~PAGE_MASK));
1077 uchunk = min(ubytes, mchunk);
1079 /* For file based kexec, source pages are in kernel memory */
1080 if (image->file_mode)
1081 memcpy(ptr, kbuf, uchunk);
1083 result = copy_from_user(ptr, buf, uchunk);
1091 if (image->file_mode)
1101 static int kimage_load_crash_segment(struct kimage *image,
1102 struct kexec_segment *segment)
1104 /* For crash dumps kernels we simply copy the data from
1105 * user space to it's destination.
1106 * We do things a page at a time for the sake of kmap.
1108 unsigned long maddr;
1109 size_t ubytes, mbytes;
1111 unsigned char __user *buf;
1115 ubytes = segment->bufsz;
1116 mbytes = segment->memsz;
1117 maddr = segment->mem;
1121 size_t uchunk, mchunk;
1123 page = pfn_to_page(maddr >> PAGE_SHIFT);
1129 ptr += maddr & ~PAGE_MASK;
1130 mchunk = min_t(size_t, mbytes,
1131 PAGE_SIZE - (maddr & ~PAGE_MASK));
1132 uchunk = min(ubytes, mchunk);
1133 if (mchunk > uchunk) {
1134 /* Zero the trailing part of the page */
1135 memset(ptr + uchunk, 0, mchunk - uchunk);
1137 result = copy_from_user(ptr, buf, uchunk);
1138 kexec_flush_icache_page(page);
1153 static int kimage_load_segment(struct kimage *image,
1154 struct kexec_segment *segment)
1156 int result = -ENOMEM;
1158 switch (image->type) {
1159 case KEXEC_TYPE_DEFAULT:
1160 result = kimage_load_normal_segment(image, segment);
1162 case KEXEC_TYPE_CRASH:
1163 result = kimage_load_crash_segment(image, segment);
1171 * Exec Kernel system call: for obvious reasons only root may call it.
1173 * This call breaks up into three pieces.
1174 * - A generic part which loads the new kernel from the current
1175 * address space, and very carefully places the data in the
1178 * - A generic part that interacts with the kernel and tells all of
1179 * the devices to shut down. Preventing on-going dmas, and placing
1180 * the devices in a consistent state so a later kernel can
1181 * reinitialize them.
1183 * - A machine specific part that includes the syscall number
1184 * and then copies the image to it's final destination. And
1185 * jumps into the image at entry.
1187 * kexec does not sync, or unmount filesystems so if you need
1188 * that to happen you need to do that yourself.
1190 struct kimage *kexec_image;
1191 struct kimage *kexec_crash_image;
1192 int kexec_load_disabled;
1194 static DEFINE_MUTEX(kexec_mutex);
1196 SYSCALL_DEFINE4(kexec_load, unsigned long, entry, unsigned long, nr_segments,
1197 struct kexec_segment __user *, segments, unsigned long, flags)
1199 struct kimage **dest_image, *image;
1202 /* We only trust the superuser with rebooting the system. */
1203 if (!capable(CAP_SYS_BOOT) || kexec_load_disabled)
1207 * Verify we have a legal set of flags
1208 * This leaves us room for future extensions.
1210 if ((flags & KEXEC_FLAGS) != (flags & ~KEXEC_ARCH_MASK))
1213 /* Verify we are on the appropriate architecture */
1214 if (((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH) &&
1215 ((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH_DEFAULT))
1218 /* Put an artificial cap on the number
1219 * of segments passed to kexec_load.
1221 if (nr_segments > KEXEC_SEGMENT_MAX)
1227 /* Because we write directly to the reserved memory
1228 * region when loading crash kernels we need a mutex here to
1229 * prevent multiple crash kernels from attempting to load
1230 * simultaneously, and to prevent a crash kernel from loading
1231 * over the top of a in use crash kernel.
1233 * KISS: always take the mutex.
1235 if (!mutex_trylock(&kexec_mutex))
1238 dest_image = &kexec_image;
1239 if (flags & KEXEC_ON_CRASH)
1240 dest_image = &kexec_crash_image;
1241 if (nr_segments > 0) {
1244 /* Loading another kernel to reboot into */
1245 if ((flags & KEXEC_ON_CRASH) == 0)
1246 result = kimage_alloc_init(&image, entry, nr_segments,
1248 /* Loading another kernel to switch to if this one crashes */
1249 else if (flags & KEXEC_ON_CRASH) {
1250 /* Free any current crash dump kernel before
1253 kimage_free(xchg(&kexec_crash_image, NULL));
1254 result = kimage_alloc_init(&image, entry, nr_segments,
1256 crash_map_reserved_pages();
1261 if (flags & KEXEC_PRESERVE_CONTEXT)
1262 image->preserve_context = 1;
1263 result = machine_kexec_prepare(image);
1267 for (i = 0; i < nr_segments; i++) {
1268 result = kimage_load_segment(image, &image->segment[i]);
1272 kimage_terminate(image);
1273 if (flags & KEXEC_ON_CRASH)
1274 crash_unmap_reserved_pages();
1276 /* Install the new kernel, and Uninstall the old */
1277 image = xchg(dest_image, image);
1280 mutex_unlock(&kexec_mutex);
1287 * Add and remove page tables for crashkernel memory
1289 * Provide an empty default implementation here -- architecture
1290 * code may override this
1292 void __weak crash_map_reserved_pages(void)
1295 void __weak crash_unmap_reserved_pages(void)
1298 #ifdef CONFIG_COMPAT
1299 COMPAT_SYSCALL_DEFINE4(kexec_load, compat_ulong_t, entry,
1300 compat_ulong_t, nr_segments,
1301 struct compat_kexec_segment __user *, segments,
1302 compat_ulong_t, flags)
1304 struct compat_kexec_segment in;
1305 struct kexec_segment out, __user *ksegments;
1306 unsigned long i, result;
1308 /* Don't allow clients that don't understand the native
1309 * architecture to do anything.
1311 if ((flags & KEXEC_ARCH_MASK) == KEXEC_ARCH_DEFAULT)
1314 if (nr_segments > KEXEC_SEGMENT_MAX)
1317 ksegments = compat_alloc_user_space(nr_segments * sizeof(out));
1318 for (i = 0; i < nr_segments; i++) {
1319 result = copy_from_user(&in, &segments[i], sizeof(in));
1323 out.buf = compat_ptr(in.buf);
1324 out.bufsz = in.bufsz;
1326 out.memsz = in.memsz;
1328 result = copy_to_user(&ksegments[i], &out, sizeof(out));
1333 return sys_kexec_load(entry, nr_segments, ksegments, flags);
1337 SYSCALL_DEFINE5(kexec_file_load, int, kernel_fd, int, initrd_fd,
1338 unsigned long, cmdline_len, const char __user *, cmdline_ptr,
1339 unsigned long, flags)
1342 struct kimage **dest_image, *image;
1344 /* We only trust the superuser with rebooting the system. */
1345 if (!capable(CAP_SYS_BOOT) || kexec_load_disabled)
1348 /* Make sure we have a legal set of flags */
1349 if (flags != (flags & KEXEC_FILE_FLAGS))
1354 if (!mutex_trylock(&kexec_mutex))
1357 dest_image = &kexec_image;
1358 if (flags & KEXEC_FILE_ON_CRASH)
1359 dest_image = &kexec_crash_image;
1361 if (flags & KEXEC_FILE_UNLOAD)
1365 * In case of crash, new kernel gets loaded in reserved region. It is
1366 * same memory where old crash kernel might be loaded. Free any
1367 * current crash dump kernel before we corrupt it.
1369 if (flags & KEXEC_FILE_ON_CRASH)
1370 kimage_free(xchg(&kexec_crash_image, NULL));
1372 ret = kimage_file_alloc_init(&image, kernel_fd, initrd_fd, cmdline_ptr,
1373 cmdline_len, flags);
1377 ret = machine_kexec_prepare(image);
1381 ret = kexec_calculate_store_digests(image);
1385 for (i = 0; i < image->nr_segments; i++) {
1386 struct kexec_segment *ksegment;
1388 ksegment = &image->segment[i];
1389 pr_debug("Loading segment %d: buf=0x%p bufsz=0x%zx mem=0x%lx memsz=0x%zx\n",
1390 i, ksegment->buf, ksegment->bufsz, ksegment->mem,
1393 ret = kimage_load_segment(image, &image->segment[i]);
1398 kimage_terminate(image);
1401 * Free up any temporary buffers allocated which are not needed
1402 * after image has been loaded
1404 kimage_file_post_load_cleanup(image);
1406 image = xchg(dest_image, image);
1408 mutex_unlock(&kexec_mutex);
1413 void crash_kexec(struct pt_regs *regs)
1415 /* Take the kexec_mutex here to prevent sys_kexec_load
1416 * running on one cpu from replacing the crash kernel
1417 * we are using after a panic on a different cpu.
1419 * If the crash kernel was not located in a fixed area
1420 * of memory the xchg(&kexec_crash_image) would be
1421 * sufficient. But since I reuse the memory...
1423 if (mutex_trylock(&kexec_mutex)) {
1424 if (kexec_crash_image) {
1425 struct pt_regs fixed_regs;
1427 crash_setup_regs(&fixed_regs, regs);
1428 crash_save_vmcoreinfo();
1429 machine_crash_shutdown(&fixed_regs);
1430 machine_kexec(kexec_crash_image);
1432 mutex_unlock(&kexec_mutex);
1436 size_t crash_get_memory_size(void)
1439 mutex_lock(&kexec_mutex);
1440 if (crashk_res.end != crashk_res.start)
1441 size = resource_size(&crashk_res);
1442 mutex_unlock(&kexec_mutex);
1446 void __weak crash_free_reserved_phys_range(unsigned long begin,
1451 for (addr = begin; addr < end; addr += PAGE_SIZE)
1452 free_reserved_page(pfn_to_page(addr >> PAGE_SHIFT));
1455 int crash_shrink_memory(unsigned long new_size)
1458 unsigned long start, end;
1459 unsigned long old_size;
1460 struct resource *ram_res;
1462 mutex_lock(&kexec_mutex);
1464 if (kexec_crash_image) {
1468 start = crashk_res.start;
1469 end = crashk_res.end;
1470 old_size = (end == 0) ? 0 : end - start + 1;
1471 if (new_size >= old_size) {
1472 ret = (new_size == old_size) ? 0 : -EINVAL;
1476 ram_res = kzalloc(sizeof(*ram_res), GFP_KERNEL);
1482 start = roundup(start, KEXEC_CRASH_MEM_ALIGN);
1483 end = roundup(start + new_size, KEXEC_CRASH_MEM_ALIGN);
1485 crash_map_reserved_pages();
1486 crash_free_reserved_phys_range(end, crashk_res.end);
1488 if ((start == end) && (crashk_res.parent != NULL))
1489 release_resource(&crashk_res);
1491 ram_res->start = end;
1492 ram_res->end = crashk_res.end;
1493 ram_res->flags = IORESOURCE_BUSY | IORESOURCE_MEM;
1494 ram_res->name = "System RAM";
1496 crashk_res.end = end - 1;
1498 insert_resource(&iomem_resource, ram_res);
1499 crash_unmap_reserved_pages();
1502 mutex_unlock(&kexec_mutex);
1506 static u32 *append_elf_note(u32 *buf, char *name, unsigned type, void *data,
1509 struct elf_note note;
1511 note.n_namesz = strlen(name) + 1;
1512 note.n_descsz = data_len;
1514 memcpy(buf, ¬e, sizeof(note));
1515 buf += (sizeof(note) + 3)/4;
1516 memcpy(buf, name, note.n_namesz);
1517 buf += (note.n_namesz + 3)/4;
1518 memcpy(buf, data, note.n_descsz);
1519 buf += (note.n_descsz + 3)/4;
1524 static void final_note(u32 *buf)
1526 struct elf_note note;
1531 memcpy(buf, ¬e, sizeof(note));
1534 void crash_save_cpu(struct pt_regs *regs, int cpu)
1536 struct elf_prstatus prstatus;
1539 if ((cpu < 0) || (cpu >= nr_cpu_ids))
1542 /* Using ELF notes here is opportunistic.
1543 * I need a well defined structure format
1544 * for the data I pass, and I need tags
1545 * on the data to indicate what information I have
1546 * squirrelled away. ELF notes happen to provide
1547 * all of that, so there is no need to invent something new.
1549 buf = (u32 *)per_cpu_ptr(crash_notes, cpu);
1552 memset(&prstatus, 0, sizeof(prstatus));
1553 prstatus.pr_pid = current->pid;
1554 elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
1555 buf = append_elf_note(buf, KEXEC_CORE_NOTE_NAME, NT_PRSTATUS,
1556 &prstatus, sizeof(prstatus));
1560 static int __init crash_notes_memory_init(void)
1562 /* Allocate memory for saving cpu registers. */
1563 crash_notes = alloc_percpu(note_buf_t);
1565 pr_warn("Kexec: Memory allocation for saving cpu register states failed\n");
1570 subsys_initcall(crash_notes_memory_init);
1574 * parsing the "crashkernel" commandline
1576 * this code is intended to be called from architecture specific code
1581 * This function parses command lines in the format
1583 * crashkernel=ramsize-range:size[,...][@offset]
1585 * The function returns 0 on success and -EINVAL on failure.
1587 static int __init parse_crashkernel_mem(char *cmdline,
1588 unsigned long long system_ram,
1589 unsigned long long *crash_size,
1590 unsigned long long *crash_base)
1592 char *cur = cmdline, *tmp;
1594 /* for each entry of the comma-separated list */
1596 unsigned long long start, end = ULLONG_MAX, size;
1598 /* get the start of the range */
1599 start = memparse(cur, &tmp);
1601 pr_warn("crashkernel: Memory value expected\n");
1606 pr_warn("crashkernel: '-' expected\n");
1611 /* if no ':' is here, than we read the end */
1613 end = memparse(cur, &tmp);
1615 pr_warn("crashkernel: Memory value expected\n");
1620 pr_warn("crashkernel: end <= start\n");
1626 pr_warn("crashkernel: ':' expected\n");
1631 size = memparse(cur, &tmp);
1633 pr_warn("Memory value expected\n");
1637 if (size >= system_ram) {
1638 pr_warn("crashkernel: invalid size\n");
1643 if (system_ram >= start && system_ram < end) {
1647 } while (*cur++ == ',');
1649 if (*crash_size > 0) {
1650 while (*cur && *cur != ' ' && *cur != '@')
1654 *crash_base = memparse(cur, &tmp);
1656 pr_warn("Memory value expected after '@'\n");
1666 * That function parses "simple" (old) crashkernel command lines like
1668 * crashkernel=size[@offset]
1670 * It returns 0 on success and -EINVAL on failure.
1672 static int __init parse_crashkernel_simple(char *cmdline,
1673 unsigned long long *crash_size,
1674 unsigned long long *crash_base)
1676 char *cur = cmdline;
1678 *crash_size = memparse(cmdline, &cur);
1679 if (cmdline == cur) {
1680 pr_warn("crashkernel: memory value expected\n");
1685 *crash_base = memparse(cur+1, &cur);
1686 else if (*cur != ' ' && *cur != '\0') {
1687 pr_warn("crashkernel: unrecognized char\n");
1694 #define SUFFIX_HIGH 0
1695 #define SUFFIX_LOW 1
1696 #define SUFFIX_NULL 2
1697 static __initdata char *suffix_tbl[] = {
1698 [SUFFIX_HIGH] = ",high",
1699 [SUFFIX_LOW] = ",low",
1700 [SUFFIX_NULL] = NULL,
1704 * That function parses "suffix" crashkernel command lines like
1706 * crashkernel=size,[high|low]
1708 * It returns 0 on success and -EINVAL on failure.
1710 static int __init parse_crashkernel_suffix(char *cmdline,
1711 unsigned long long *crash_size,
1712 unsigned long long *crash_base,
1715 char *cur = cmdline;
1717 *crash_size = memparse(cmdline, &cur);
1718 if (cmdline == cur) {
1719 pr_warn("crashkernel: memory value expected\n");
1723 /* check with suffix */
1724 if (strncmp(cur, suffix, strlen(suffix))) {
1725 pr_warn("crashkernel: unrecognized char\n");
1728 cur += strlen(suffix);
1729 if (*cur != ' ' && *cur != '\0') {
1730 pr_warn("crashkernel: unrecognized char\n");
1737 static __init char *get_last_crashkernel(char *cmdline,
1741 char *p = cmdline, *ck_cmdline = NULL;
1743 /* find crashkernel and use the last one if there are more */
1744 p = strstr(p, name);
1746 char *end_p = strchr(p, ' ');
1750 end_p = p + strlen(p);
1755 /* skip the one with any known suffix */
1756 for (i = 0; suffix_tbl[i]; i++) {
1757 q = end_p - strlen(suffix_tbl[i]);
1758 if (!strncmp(q, suffix_tbl[i],
1759 strlen(suffix_tbl[i])))
1764 q = end_p - strlen(suffix);
1765 if (!strncmp(q, suffix, strlen(suffix)))
1769 p = strstr(p+1, name);
1778 static int __init __parse_crashkernel(char *cmdline,
1779 unsigned long long system_ram,
1780 unsigned long long *crash_size,
1781 unsigned long long *crash_base,
1785 char *first_colon, *first_space;
1788 BUG_ON(!crash_size || !crash_base);
1792 ck_cmdline = get_last_crashkernel(cmdline, name, suffix);
1797 ck_cmdline += strlen(name);
1800 return parse_crashkernel_suffix(ck_cmdline, crash_size,
1801 crash_base, suffix);
1803 * if the commandline contains a ':', then that's the extended
1804 * syntax -- if not, it must be the classic syntax
1806 first_colon = strchr(ck_cmdline, ':');
1807 first_space = strchr(ck_cmdline, ' ');
1808 if (first_colon && (!first_space || first_colon < first_space))
1809 return parse_crashkernel_mem(ck_cmdline, system_ram,
1810 crash_size, crash_base);
1812 return parse_crashkernel_simple(ck_cmdline, crash_size, crash_base);
1816 * That function is the entry point for command line parsing and should be
1817 * called from the arch-specific code.
1819 int __init parse_crashkernel(char *cmdline,
1820 unsigned long long system_ram,
1821 unsigned long long *crash_size,
1822 unsigned long long *crash_base)
1824 return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
1825 "crashkernel=", NULL);
1828 int __init parse_crashkernel_high(char *cmdline,
1829 unsigned long long system_ram,
1830 unsigned long long *crash_size,
1831 unsigned long long *crash_base)
1833 return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
1834 "crashkernel=", suffix_tbl[SUFFIX_HIGH]);
1837 int __init parse_crashkernel_low(char *cmdline,
1838 unsigned long long system_ram,
1839 unsigned long long *crash_size,
1840 unsigned long long *crash_base)
1842 return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
1843 "crashkernel=", suffix_tbl[SUFFIX_LOW]);
1846 static void update_vmcoreinfo_note(void)
1848 u32 *buf = vmcoreinfo_note;
1850 if (!vmcoreinfo_size)
1852 buf = append_elf_note(buf, VMCOREINFO_NOTE_NAME, 0, vmcoreinfo_data,
1857 void crash_save_vmcoreinfo(void)
1859 vmcoreinfo_append_str("CRASHTIME=%ld\n", get_seconds());
1860 update_vmcoreinfo_note();
1863 void vmcoreinfo_append_str(const char *fmt, ...)
1869 va_start(args, fmt);
1870 r = vscnprintf(buf, sizeof(buf), fmt, args);
1873 r = min(r, vmcoreinfo_max_size - vmcoreinfo_size);
1875 memcpy(&vmcoreinfo_data[vmcoreinfo_size], buf, r);
1877 vmcoreinfo_size += r;
1881 * provide an empty default implementation here -- architecture
1882 * code may override this
1884 void __weak arch_crash_save_vmcoreinfo(void)
1887 unsigned long __weak paddr_vmcoreinfo_note(void)
1889 return __pa((unsigned long)(char *)&vmcoreinfo_note);
1892 static int __init crash_save_vmcoreinfo_init(void)
1894 VMCOREINFO_OSRELEASE(init_uts_ns.name.release);
1895 VMCOREINFO_PAGESIZE(PAGE_SIZE);
1897 VMCOREINFO_SYMBOL(init_uts_ns);
1898 VMCOREINFO_SYMBOL(node_online_map);
1900 VMCOREINFO_SYMBOL(swapper_pg_dir);
1902 VMCOREINFO_SYMBOL(_stext);
1903 VMCOREINFO_SYMBOL(vmap_area_list);
1905 #ifndef CONFIG_NEED_MULTIPLE_NODES
1906 VMCOREINFO_SYMBOL(mem_map);
1907 VMCOREINFO_SYMBOL(contig_page_data);
1909 #ifdef CONFIG_SPARSEMEM
1910 VMCOREINFO_SYMBOL(mem_section);
1911 VMCOREINFO_LENGTH(mem_section, NR_SECTION_ROOTS);
1912 VMCOREINFO_STRUCT_SIZE(mem_section);
1913 VMCOREINFO_OFFSET(mem_section, section_mem_map);
1915 VMCOREINFO_STRUCT_SIZE(page);
1916 VMCOREINFO_STRUCT_SIZE(pglist_data);
1917 VMCOREINFO_STRUCT_SIZE(zone);
1918 VMCOREINFO_STRUCT_SIZE(free_area);
1919 VMCOREINFO_STRUCT_SIZE(list_head);
1920 VMCOREINFO_SIZE(nodemask_t);
1921 VMCOREINFO_OFFSET(page, flags);
1922 VMCOREINFO_OFFSET(page, _count);
1923 VMCOREINFO_OFFSET(page, mapping);
1924 VMCOREINFO_OFFSET(page, lru);
1925 VMCOREINFO_OFFSET(page, _mapcount);
1926 VMCOREINFO_OFFSET(page, private);
1927 VMCOREINFO_OFFSET(pglist_data, node_zones);
1928 VMCOREINFO_OFFSET(pglist_data, nr_zones);
1929 #ifdef CONFIG_FLAT_NODE_MEM_MAP
1930 VMCOREINFO_OFFSET(pglist_data, node_mem_map);
1932 VMCOREINFO_OFFSET(pglist_data, node_start_pfn);
1933 VMCOREINFO_OFFSET(pglist_data, node_spanned_pages);
1934 VMCOREINFO_OFFSET(pglist_data, node_id);
1935 VMCOREINFO_OFFSET(zone, free_area);
1936 VMCOREINFO_OFFSET(zone, vm_stat);
1937 VMCOREINFO_OFFSET(zone, spanned_pages);
1938 VMCOREINFO_OFFSET(free_area, free_list);
1939 VMCOREINFO_OFFSET(list_head, next);
1940 VMCOREINFO_OFFSET(list_head, prev);
1941 VMCOREINFO_OFFSET(vmap_area, va_start);
1942 VMCOREINFO_OFFSET(vmap_area, list);
1943 VMCOREINFO_LENGTH(zone.free_area, MAX_ORDER);
1944 log_buf_kexec_setup();
1945 VMCOREINFO_LENGTH(free_area.free_list, MIGRATE_TYPES);
1946 VMCOREINFO_NUMBER(NR_FREE_PAGES);
1947 VMCOREINFO_NUMBER(PG_lru);
1948 VMCOREINFO_NUMBER(PG_private);
1949 VMCOREINFO_NUMBER(PG_swapcache);
1950 VMCOREINFO_NUMBER(PG_slab);
1951 #ifdef CONFIG_MEMORY_FAILURE
1952 VMCOREINFO_NUMBER(PG_hwpoison);
1954 VMCOREINFO_NUMBER(PG_head_mask);
1955 VMCOREINFO_NUMBER(PAGE_BUDDY_MAPCOUNT_VALUE);
1956 #ifdef CONFIG_HUGETLBFS
1957 VMCOREINFO_SYMBOL(free_huge_page);
1960 arch_crash_save_vmcoreinfo();
1961 update_vmcoreinfo_note();
1966 subsys_initcall(crash_save_vmcoreinfo_init);
1968 static int __kexec_add_segment(struct kimage *image, char *buf,
1969 unsigned long bufsz, unsigned long mem,
1970 unsigned long memsz)
1972 struct kexec_segment *ksegment;
1974 ksegment = &image->segment[image->nr_segments];
1975 ksegment->kbuf = buf;
1976 ksegment->bufsz = bufsz;
1977 ksegment->mem = mem;
1978 ksegment->memsz = memsz;
1979 image->nr_segments++;
1984 static int locate_mem_hole_top_down(unsigned long start, unsigned long end,
1985 struct kexec_buf *kbuf)
1987 struct kimage *image = kbuf->image;
1988 unsigned long temp_start, temp_end;
1990 temp_end = min(end, kbuf->buf_max);
1991 temp_start = temp_end - kbuf->memsz;
1994 /* align down start */
1995 temp_start = temp_start & (~(kbuf->buf_align - 1));
1997 if (temp_start < start || temp_start < kbuf->buf_min)
2000 temp_end = temp_start + kbuf->memsz - 1;
2003 * Make sure this does not conflict with any of existing
2006 if (kimage_is_destination_range(image, temp_start, temp_end)) {
2007 temp_start = temp_start - PAGE_SIZE;
2011 /* We found a suitable memory range */
2015 /* If we are here, we found a suitable memory range */
2016 __kexec_add_segment(image, kbuf->buffer, kbuf->bufsz, temp_start,
2019 /* Success, stop navigating through remaining System RAM ranges */
2023 static int locate_mem_hole_bottom_up(unsigned long start, unsigned long end,
2024 struct kexec_buf *kbuf)
2026 struct kimage *image = kbuf->image;
2027 unsigned long temp_start, temp_end;
2029 temp_start = max(start, kbuf->buf_min);
2032 temp_start = ALIGN(temp_start, kbuf->buf_align);
2033 temp_end = temp_start + kbuf->memsz - 1;
2035 if (temp_end > end || temp_end > kbuf->buf_max)
2038 * Make sure this does not conflict with any of existing
2041 if (kimage_is_destination_range(image, temp_start, temp_end)) {
2042 temp_start = temp_start + PAGE_SIZE;
2046 /* We found a suitable memory range */
2050 /* If we are here, we found a suitable memory range */
2051 __kexec_add_segment(image, kbuf->buffer, kbuf->bufsz, temp_start,
2054 /* Success, stop navigating through remaining System RAM ranges */
2058 static int locate_mem_hole_callback(u64 start, u64 end, void *arg)
2060 struct kexec_buf *kbuf = (struct kexec_buf *)arg;
2061 unsigned long sz = end - start + 1;
2063 /* Returning 0 will take to next memory range */
2064 if (sz < kbuf->memsz)
2067 if (end < kbuf->buf_min || start > kbuf->buf_max)
2071 * Allocate memory top down with-in ram range. Otherwise bottom up
2075 return locate_mem_hole_top_down(start, end, kbuf);
2076 return locate_mem_hole_bottom_up(start, end, kbuf);
2080 * Helper function for placing a buffer in a kexec segment. This assumes
2081 * that kexec_mutex is held.
2083 int kexec_add_buffer(struct kimage *image, char *buffer, unsigned long bufsz,
2084 unsigned long memsz, unsigned long buf_align,
2085 unsigned long buf_min, unsigned long buf_max,
2086 bool top_down, unsigned long *load_addr)
2089 struct kexec_segment *ksegment;
2090 struct kexec_buf buf, *kbuf;
2093 /* Currently adding segment this way is allowed only in file mode */
2094 if (!image->file_mode)
2097 if (image->nr_segments >= KEXEC_SEGMENT_MAX)
2101 * Make sure we are not trying to add buffer after allocating
2102 * control pages. All segments need to be placed first before
2103 * any control pages are allocated. As control page allocation
2104 * logic goes through list of segments to make sure there are
2105 * no destination overlaps.
2107 if (!list_empty(&image->control_pages)) {
2112 memset(&buf, 0, sizeof(struct kexec_buf));
2114 kbuf->image = image;
2115 kbuf->buffer = buffer;
2116 kbuf->bufsz = bufsz;
2118 kbuf->memsz = ALIGN(memsz, PAGE_SIZE);
2119 kbuf->buf_align = max(buf_align, PAGE_SIZE);
2120 kbuf->buf_min = buf_min;
2121 kbuf->buf_max = buf_max;
2122 kbuf->top_down = top_down;
2124 /* Walk the RAM ranges and allocate a suitable range for the buffer */
2125 ret = walk_system_ram_res(0, -1, kbuf, locate_mem_hole_callback);
2127 /* A suitable memory range could not be found for buffer */
2128 return -EADDRNOTAVAIL;
2131 /* Found a suitable memory range */
2132 ksegment = &image->segment[image->nr_segments - 1];
2133 *load_addr = ksegment->mem;
2137 /* Calculate and store the digest of segments */
2138 static int kexec_calculate_store_digests(struct kimage *image)
2140 struct crypto_shash *tfm;
2141 struct shash_desc *desc;
2142 int ret = 0, i, j, zero_buf_sz, sha_region_sz;
2143 size_t desc_size, nullsz;
2146 struct kexec_sha_region *sha_regions;
2147 struct purgatory_info *pi = &image->purgatory_info;
2149 zero_buf = __va(page_to_pfn(ZERO_PAGE(0)) << PAGE_SHIFT);
2150 zero_buf_sz = PAGE_SIZE;
2152 tfm = crypto_alloc_shash("sha256", 0, 0);
2158 desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
2159 desc = kzalloc(desc_size, GFP_KERNEL);
2165 sha_region_sz = KEXEC_SEGMENT_MAX * sizeof(struct kexec_sha_region);
2166 sha_regions = vzalloc(sha_region_sz);
2173 ret = crypto_shash_init(desc);
2175 goto out_free_sha_regions;
2177 digest = kzalloc(SHA256_DIGEST_SIZE, GFP_KERNEL);
2180 goto out_free_sha_regions;
2183 for (j = i = 0; i < image->nr_segments; i++) {
2184 struct kexec_segment *ksegment;
2186 ksegment = &image->segment[i];
2188 * Skip purgatory as it will be modified once we put digest
2189 * info in purgatory.
2191 if (ksegment->kbuf == pi->purgatory_buf)
2194 ret = crypto_shash_update(desc, ksegment->kbuf,
2200 * Assume rest of the buffer is filled with zero and
2201 * update digest accordingly.
2203 nullsz = ksegment->memsz - ksegment->bufsz;
2205 unsigned long bytes = nullsz;
2207 if (bytes > zero_buf_sz)
2208 bytes = zero_buf_sz;
2209 ret = crypto_shash_update(desc, zero_buf, bytes);
2218 sha_regions[j].start = ksegment->mem;
2219 sha_regions[j].len = ksegment->memsz;
2224 ret = crypto_shash_final(desc, digest);
2226 goto out_free_digest;
2227 ret = kexec_purgatory_get_set_symbol(image, "sha_regions",
2228 sha_regions, sha_region_sz, 0);
2230 goto out_free_digest;
2232 ret = kexec_purgatory_get_set_symbol(image, "sha256_digest",
2233 digest, SHA256_DIGEST_SIZE, 0);
2235 goto out_free_digest;
2240 out_free_sha_regions:
2250 /* Actually load purgatory. Lot of code taken from kexec-tools */
2251 static int __kexec_load_purgatory(struct kimage *image, unsigned long min,
2252 unsigned long max, int top_down)
2254 struct purgatory_info *pi = &image->purgatory_info;
2255 unsigned long align, buf_align, bss_align, buf_sz, bss_sz, bss_pad;
2256 unsigned long memsz, entry, load_addr, curr_load_addr, bss_addr, offset;
2257 unsigned char *buf_addr, *src;
2258 int i, ret = 0, entry_sidx = -1;
2259 const Elf_Shdr *sechdrs_c;
2260 Elf_Shdr *sechdrs = NULL;
2261 void *purgatory_buf = NULL;
2264 * sechdrs_c points to section headers in purgatory and are read
2265 * only. No modifications allowed.
2267 sechdrs_c = (void *)pi->ehdr + pi->ehdr->e_shoff;
2270 * We can not modify sechdrs_c[] and its fields. It is read only.
2271 * Copy it over to a local copy where one can store some temporary
2272 * data and free it at the end. We need to modify ->sh_addr and
2273 * ->sh_offset fields to keep track of permanent and temporary
2274 * locations of sections.
2276 sechdrs = vzalloc(pi->ehdr->e_shnum * sizeof(Elf_Shdr));
2280 memcpy(sechdrs, sechdrs_c, pi->ehdr->e_shnum * sizeof(Elf_Shdr));
2283 * We seem to have multiple copies of sections. First copy is which
2284 * is embedded in kernel in read only section. Some of these sections
2285 * will be copied to a temporary buffer and relocated. And these
2286 * sections will finally be copied to their final destination at
2287 * segment load time.
2289 * Use ->sh_offset to reflect section address in memory. It will
2290 * point to original read only copy if section is not allocatable.
2291 * Otherwise it will point to temporary copy which will be relocated.
2293 * Use ->sh_addr to contain final address of the section where it
2294 * will go during execution time.
2296 for (i = 0; i < pi->ehdr->e_shnum; i++) {
2297 if (sechdrs[i].sh_type == SHT_NOBITS)
2300 sechdrs[i].sh_offset = (unsigned long)pi->ehdr +
2301 sechdrs[i].sh_offset;
2305 * Identify entry point section and make entry relative to section
2308 entry = pi->ehdr->e_entry;
2309 for (i = 0; i < pi->ehdr->e_shnum; i++) {
2310 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2313 if (!(sechdrs[i].sh_flags & SHF_EXECINSTR))
2316 /* Make entry section relative */
2317 if (sechdrs[i].sh_addr <= pi->ehdr->e_entry &&
2318 ((sechdrs[i].sh_addr + sechdrs[i].sh_size) >
2319 pi->ehdr->e_entry)) {
2321 entry -= sechdrs[i].sh_addr;
2326 /* Determine how much memory is needed to load relocatable object. */
2332 for (i = 0; i < pi->ehdr->e_shnum; i++) {
2333 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2336 align = sechdrs[i].sh_addralign;
2337 if (sechdrs[i].sh_type != SHT_NOBITS) {
2338 if (buf_align < align)
2340 buf_sz = ALIGN(buf_sz, align);
2341 buf_sz += sechdrs[i].sh_size;
2344 if (bss_align < align)
2346 bss_sz = ALIGN(bss_sz, align);
2347 bss_sz += sechdrs[i].sh_size;
2351 /* Determine the bss padding required to align bss properly */
2353 if (buf_sz & (bss_align - 1))
2354 bss_pad = bss_align - (buf_sz & (bss_align - 1));
2356 memsz = buf_sz + bss_pad + bss_sz;
2358 /* Allocate buffer for purgatory */
2359 purgatory_buf = vzalloc(buf_sz);
2360 if (!purgatory_buf) {
2365 if (buf_align < bss_align)
2366 buf_align = bss_align;
2368 /* Add buffer to segment list */
2369 ret = kexec_add_buffer(image, purgatory_buf, buf_sz, memsz,
2370 buf_align, min, max, top_down,
2371 &pi->purgatory_load_addr);
2375 /* Load SHF_ALLOC sections */
2376 buf_addr = purgatory_buf;
2377 load_addr = curr_load_addr = pi->purgatory_load_addr;
2378 bss_addr = load_addr + buf_sz + bss_pad;
2380 for (i = 0; i < pi->ehdr->e_shnum; i++) {
2381 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2384 align = sechdrs[i].sh_addralign;
2385 if (sechdrs[i].sh_type != SHT_NOBITS) {
2386 curr_load_addr = ALIGN(curr_load_addr, align);
2387 offset = curr_load_addr - load_addr;
2388 /* We already modifed ->sh_offset to keep src addr */
2389 src = (char *) sechdrs[i].sh_offset;
2390 memcpy(buf_addr + offset, src, sechdrs[i].sh_size);
2392 /* Store load address and source address of section */
2393 sechdrs[i].sh_addr = curr_load_addr;
2396 * This section got copied to temporary buffer. Update
2397 * ->sh_offset accordingly.
2399 sechdrs[i].sh_offset = (unsigned long)(buf_addr + offset);
2401 /* Advance to the next address */
2402 curr_load_addr += sechdrs[i].sh_size;
2404 bss_addr = ALIGN(bss_addr, align);
2405 sechdrs[i].sh_addr = bss_addr;
2406 bss_addr += sechdrs[i].sh_size;
2410 /* Update entry point based on load address of text section */
2411 if (entry_sidx >= 0)
2412 entry += sechdrs[entry_sidx].sh_addr;
2414 /* Make kernel jump to purgatory after shutdown */
2415 image->start = entry;
2417 /* Used later to get/set symbol values */
2418 pi->sechdrs = sechdrs;
2421 * Used later to identify which section is purgatory and skip it
2422 * from checksumming.
2424 pi->purgatory_buf = purgatory_buf;
2428 vfree(purgatory_buf);
2432 static int kexec_apply_relocations(struct kimage *image)
2435 struct purgatory_info *pi = &image->purgatory_info;
2436 Elf_Shdr *sechdrs = pi->sechdrs;
2438 /* Apply relocations */
2439 for (i = 0; i < pi->ehdr->e_shnum; i++) {
2440 Elf_Shdr *section, *symtab;
2442 if (sechdrs[i].sh_type != SHT_RELA &&
2443 sechdrs[i].sh_type != SHT_REL)
2447 * For section of type SHT_RELA/SHT_REL,
2448 * ->sh_link contains section header index of associated
2449 * symbol table. And ->sh_info contains section header
2450 * index of section to which relocations apply.
2452 if (sechdrs[i].sh_info >= pi->ehdr->e_shnum ||
2453 sechdrs[i].sh_link >= pi->ehdr->e_shnum)
2456 section = &sechdrs[sechdrs[i].sh_info];
2457 symtab = &sechdrs[sechdrs[i].sh_link];
2459 if (!(section->sh_flags & SHF_ALLOC))
2463 * symtab->sh_link contain section header index of associated
2466 if (symtab->sh_link >= pi->ehdr->e_shnum)
2467 /* Invalid section number? */
2471 * Respective archicture needs to provide support for applying
2472 * relocations of type SHT_RELA/SHT_REL.
2474 if (sechdrs[i].sh_type == SHT_RELA)
2475 ret = arch_kexec_apply_relocations_add(pi->ehdr,
2477 else if (sechdrs[i].sh_type == SHT_REL)
2478 ret = arch_kexec_apply_relocations(pi->ehdr,
2487 /* Load relocatable purgatory object and relocate it appropriately */
2488 int kexec_load_purgatory(struct kimage *image, unsigned long min,
2489 unsigned long max, int top_down,
2490 unsigned long *load_addr)
2492 struct purgatory_info *pi = &image->purgatory_info;
2495 if (kexec_purgatory_size <= 0)
2498 if (kexec_purgatory_size < sizeof(Elf_Ehdr))
2501 pi->ehdr = (Elf_Ehdr *)kexec_purgatory;
2503 if (memcmp(pi->ehdr->e_ident, ELFMAG, SELFMAG) != 0
2504 || pi->ehdr->e_type != ET_REL
2505 || !elf_check_arch(pi->ehdr)
2506 || pi->ehdr->e_shentsize != sizeof(Elf_Shdr))
2509 if (pi->ehdr->e_shoff >= kexec_purgatory_size
2510 || (pi->ehdr->e_shnum * sizeof(Elf_Shdr) >
2511 kexec_purgatory_size - pi->ehdr->e_shoff))
2514 ret = __kexec_load_purgatory(image, min, max, top_down);
2518 ret = kexec_apply_relocations(image);
2522 *load_addr = pi->purgatory_load_addr;
2526 vfree(pi->purgatory_buf);
2530 static Elf_Sym *kexec_purgatory_find_symbol(struct purgatory_info *pi,
2539 if (!pi->sechdrs || !pi->ehdr)
2542 sechdrs = pi->sechdrs;
2545 for (i = 0; i < ehdr->e_shnum; i++) {
2546 if (sechdrs[i].sh_type != SHT_SYMTAB)
2549 if (sechdrs[i].sh_link >= ehdr->e_shnum)
2550 /* Invalid strtab section number */
2552 strtab = (char *)sechdrs[sechdrs[i].sh_link].sh_offset;
2553 syms = (Elf_Sym *)sechdrs[i].sh_offset;
2555 /* Go through symbols for a match */
2556 for (k = 0; k < sechdrs[i].sh_size/sizeof(Elf_Sym); k++) {
2557 if (ELF_ST_BIND(syms[k].st_info) != STB_GLOBAL)
2560 if (strcmp(strtab + syms[k].st_name, name) != 0)
2563 if (syms[k].st_shndx == SHN_UNDEF ||
2564 syms[k].st_shndx >= ehdr->e_shnum) {
2565 pr_debug("Symbol: %s has bad section index %d.\n",
2566 name, syms[k].st_shndx);
2570 /* Found the symbol we are looking for */
2578 void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name)
2580 struct purgatory_info *pi = &image->purgatory_info;
2584 sym = kexec_purgatory_find_symbol(pi, name);
2586 return ERR_PTR(-EINVAL);
2588 sechdr = &pi->sechdrs[sym->st_shndx];
2591 * Returns the address where symbol will finally be loaded after
2592 * kexec_load_segment()
2594 return (void *)(sechdr->sh_addr + sym->st_value);
2598 * Get or set value of a symbol. If "get_value" is true, symbol value is
2599 * returned in buf otherwise symbol value is set based on value in buf.
2601 int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name,
2602 void *buf, unsigned int size, bool get_value)
2606 struct purgatory_info *pi = &image->purgatory_info;
2609 sym = kexec_purgatory_find_symbol(pi, name);
2613 if (sym->st_size != size) {
2614 pr_err("symbol %s size mismatch: expected %lu actual %u\n",
2615 name, (unsigned long)sym->st_size, size);
2619 sechdrs = pi->sechdrs;
2621 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
2622 pr_err("symbol %s is in a bss section. Cannot %s\n", name,
2623 get_value ? "get" : "set");
2627 sym_buf = (unsigned char *)sechdrs[sym->st_shndx].sh_offset +
2631 memcpy((void *)buf, sym_buf, size);
2633 memcpy((void *)sym_buf, buf, size);
2639 * Move into place and start executing a preloaded standalone
2640 * executable. If nothing was preloaded return an error.
2642 int kernel_kexec(void)
2646 if (!mutex_trylock(&kexec_mutex))
2653 #ifdef CONFIG_KEXEC_JUMP
2654 if (kexec_image->preserve_context) {
2655 lock_system_sleep();
2656 pm_prepare_console();
2657 error = freeze_processes();
2660 goto Restore_console;
2663 error = dpm_suspend_start(PMSG_FREEZE);
2665 goto Resume_console;
2666 /* At this point, dpm_suspend_start() has been called,
2667 * but *not* dpm_suspend_end(). We *must* call
2668 * dpm_suspend_end() now. Otherwise, drivers for
2669 * some devices (e.g. interrupt controllers) become
2670 * desynchronized with the actual state of the
2671 * hardware at resume time, and evil weirdness ensues.
2673 error = dpm_suspend_end(PMSG_FREEZE);
2675 goto Resume_devices;
2676 error = disable_nonboot_cpus();
2679 local_irq_disable();
2680 error = syscore_suspend();
2686 kexec_in_progress = true;
2687 kernel_restart_prepare(NULL);
2688 migrate_to_reboot_cpu();
2691 * migrate_to_reboot_cpu() disables CPU hotplug assuming that
2692 * no further code needs to use CPU hotplug (which is true in
2693 * the reboot case). However, the kexec path depends on using
2694 * CPU hotplug again; so re-enable it here.
2696 cpu_hotplug_enable();
2697 pr_emerg("Starting new kernel\n");
2701 machine_kexec(kexec_image);
2703 #ifdef CONFIG_KEXEC_JUMP
2704 if (kexec_image->preserve_context) {
2709 enable_nonboot_cpus();
2710 dpm_resume_start(PMSG_RESTORE);
2712 dpm_resume_end(PMSG_RESTORE);
2717 pm_restore_console();
2718 unlock_system_sleep();
2723 mutex_unlock(&kexec_mutex);