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 #ifdef CONFIG_KEXEC_FILE
68 static int kexec_calculate_store_digests(struct kimage
*image
);
71 /* Location of the reserved area for the crash kernel */
72 struct resource crashk_res
= {
73 .name
= "Crash kernel",
76 .flags
= IORESOURCE_BUSY
| IORESOURCE_MEM
78 struct resource crashk_low_res
= {
79 .name
= "Crash kernel",
82 .flags
= IORESOURCE_BUSY
| IORESOURCE_MEM
85 int kexec_should_crash(struct task_struct
*p
)
88 * If crash_kexec_post_notifiers is enabled, don't run
89 * crash_kexec() here yet, which must be run after panic
90 * notifiers in panic().
92 if (crash_kexec_post_notifiers
)
95 * There are 4 panic() calls in do_exit() path, each of which
96 * corresponds to each of these 4 conditions.
98 if (in_interrupt() || !p
->pid
|| is_global_init(p
) || panic_on_oops
)
104 * When kexec transitions to the new kernel there is a one-to-one
105 * mapping between physical and virtual addresses. On processors
106 * where you can disable the MMU this is trivial, and easy. For
107 * others it is still a simple predictable page table to setup.
109 * In that environment kexec copies the new kernel to its final
110 * resting place. This means I can only support memory whose
111 * physical address can fit in an unsigned long. In particular
112 * addresses where (pfn << PAGE_SHIFT) > ULONG_MAX cannot be handled.
113 * If the assembly stub has more restrictive requirements
114 * KEXEC_SOURCE_MEMORY_LIMIT and KEXEC_DEST_MEMORY_LIMIT can be
115 * defined more restrictively in <asm/kexec.h>.
117 * The code for the transition from the current kernel to the
118 * the new kernel is placed in the control_code_buffer, whose size
119 * is given by KEXEC_CONTROL_PAGE_SIZE. In the best case only a single
120 * page of memory is necessary, but some architectures require more.
121 * Because this memory must be identity mapped in the transition from
122 * virtual to physical addresses it must live in the range
123 * 0 - TASK_SIZE, as only the user space mappings are arbitrarily
126 * The assembly stub in the control code buffer is passed a linked list
127 * of descriptor pages detailing the source pages of the new kernel,
128 * and the destination addresses of those source pages. As this data
129 * structure is not used in the context of the current OS, it must
132 * The code has been made to work with highmem pages and will use a
133 * destination page in its final resting place (if it happens
134 * to allocate it). The end product of this is that most of the
135 * physical address space, and most of RAM can be used.
137 * Future directions include:
138 * - allocating a page table with the control code buffer identity
139 * mapped, to simplify machine_kexec and make kexec_on_panic more
144 * KIMAGE_NO_DEST is an impossible destination address..., for
145 * allocating pages whose destination address we do not care about.
147 #define KIMAGE_NO_DEST (-1UL)
149 static int kimage_is_destination_range(struct kimage
*image
,
150 unsigned long start
, unsigned long end
);
151 static struct page
*kimage_alloc_page(struct kimage
*image
,
155 static int copy_user_segment_list(struct kimage
*image
,
156 unsigned long nr_segments
,
157 struct kexec_segment __user
*segments
)
160 size_t segment_bytes
;
162 /* Read in the segments */
163 image
->nr_segments
= nr_segments
;
164 segment_bytes
= nr_segments
* sizeof(*segments
);
165 ret
= copy_from_user(image
->segment
, segments
, segment_bytes
);
172 static int sanity_check_segment_list(struct kimage
*image
)
175 unsigned long nr_segments
= image
->nr_segments
;
178 * Verify we have good destination addresses. The caller is
179 * responsible for making certain we don't attempt to load
180 * the new image into invalid or reserved areas of RAM. This
181 * just verifies it is an address we can use.
183 * Since the kernel does everything in page size chunks ensure
184 * the destination addresses are page aligned. Too many
185 * special cases crop of when we don't do this. The most
186 * insidious is getting overlapping destination addresses
187 * simply because addresses are changed to page size
190 result
= -EADDRNOTAVAIL
;
191 for (i
= 0; i
< nr_segments
; i
++) {
192 unsigned long mstart
, mend
;
194 mstart
= image
->segment
[i
].mem
;
195 mend
= mstart
+ image
->segment
[i
].memsz
;
196 if ((mstart
& ~PAGE_MASK
) || (mend
& ~PAGE_MASK
))
198 if (mend
>= KEXEC_DESTINATION_MEMORY_LIMIT
)
202 /* Verify our destination addresses do not overlap.
203 * If we alloed overlapping destination addresses
204 * through very weird things can happen with no
205 * easy explanation as one segment stops on another.
208 for (i
= 0; i
< nr_segments
; i
++) {
209 unsigned long mstart
, mend
;
212 mstart
= image
->segment
[i
].mem
;
213 mend
= mstart
+ image
->segment
[i
].memsz
;
214 for (j
= 0; j
< i
; j
++) {
215 unsigned long pstart
, pend
;
216 pstart
= image
->segment
[j
].mem
;
217 pend
= pstart
+ image
->segment
[j
].memsz
;
218 /* Do the segments overlap ? */
219 if ((mend
> pstart
) && (mstart
< pend
))
224 /* Ensure our buffer sizes are strictly less than
225 * our memory sizes. This should always be the case,
226 * and it is easier to check up front than to be surprised
230 for (i
= 0; i
< nr_segments
; i
++) {
231 if (image
->segment
[i
].bufsz
> image
->segment
[i
].memsz
)
236 * Verify we have good destination addresses. Normally
237 * the caller is responsible for making certain we don't
238 * attempt to load the new image into invalid or reserved
239 * areas of RAM. But crash kernels are preloaded into a
240 * reserved area of ram. We must ensure the addresses
241 * are in the reserved area otherwise preloading the
242 * kernel could corrupt things.
245 if (image
->type
== KEXEC_TYPE_CRASH
) {
246 result
= -EADDRNOTAVAIL
;
247 for (i
= 0; i
< nr_segments
; i
++) {
248 unsigned long mstart
, mend
;
250 mstart
= image
->segment
[i
].mem
;
251 mend
= mstart
+ image
->segment
[i
].memsz
- 1;
252 /* Ensure we are within the crash kernel limits */
253 if ((mstart
< crashk_res
.start
) ||
254 (mend
> crashk_res
.end
))
262 static struct kimage
*do_kimage_alloc_init(void)
264 struct kimage
*image
;
266 /* Allocate a controlling structure */
267 image
= kzalloc(sizeof(*image
), GFP_KERNEL
);
272 image
->entry
= &image
->head
;
273 image
->last_entry
= &image
->head
;
274 image
->control_page
= ~0; /* By default this does not apply */
275 image
->type
= KEXEC_TYPE_DEFAULT
;
277 /* Initialize the list of control pages */
278 INIT_LIST_HEAD(&image
->control_pages
);
280 /* Initialize the list of destination pages */
281 INIT_LIST_HEAD(&image
->dest_pages
);
283 /* Initialize the list of unusable pages */
284 INIT_LIST_HEAD(&image
->unusable_pages
);
289 static void kimage_free_page_list(struct list_head
*list
);
291 static int kimage_alloc_init(struct kimage
**rimage
, unsigned long entry
,
292 unsigned long nr_segments
,
293 struct kexec_segment __user
*segments
,
297 struct kimage
*image
;
298 bool kexec_on_panic
= flags
& KEXEC_ON_CRASH
;
300 if (kexec_on_panic
) {
301 /* Verify we have a valid entry point */
302 if ((entry
< crashk_res
.start
) || (entry
> crashk_res
.end
))
303 return -EADDRNOTAVAIL
;
306 /* Allocate and initialize a controlling structure */
307 image
= do_kimage_alloc_init();
311 image
->start
= entry
;
313 ret
= copy_user_segment_list(image
, nr_segments
, segments
);
317 ret
= sanity_check_segment_list(image
);
321 /* Enable the special crash kernel control page allocation policy. */
322 if (kexec_on_panic
) {
323 image
->control_page
= crashk_res
.start
;
324 image
->type
= KEXEC_TYPE_CRASH
;
328 * Find a location for the control code buffer, and add it
329 * the vector of segments so that it's pages will also be
330 * counted as destination pages.
333 image
->control_code_page
= kimage_alloc_control_pages(image
,
334 get_order(KEXEC_CONTROL_PAGE_SIZE
));
335 if (!image
->control_code_page
) {
336 pr_err("Could not allocate control_code_buffer\n");
340 if (!kexec_on_panic
) {
341 image
->swap_page
= kimage_alloc_control_pages(image
, 0);
342 if (!image
->swap_page
) {
343 pr_err("Could not allocate swap buffer\n");
344 goto out_free_control_pages
;
350 out_free_control_pages
:
351 kimage_free_page_list(&image
->control_pages
);
357 #ifdef CONFIG_KEXEC_FILE
358 static int copy_file_from_fd(int fd
, void **buf
, unsigned long *buf_len
)
360 struct fd f
= fdget(fd
);
369 ret
= vfs_getattr(&f
.file
->f_path
, &stat
);
373 if (stat
.size
> INT_MAX
) {
378 /* Don't hand 0 to vmalloc, it whines. */
379 if (stat
.size
== 0) {
384 *buf
= vmalloc(stat
.size
);
391 while (pos
< stat
.size
) {
392 bytes
= kernel_read(f
.file
, pos
, (char *)(*buf
) + pos
,
405 if (pos
!= stat
.size
) {
417 /* Architectures can provide this probe function */
418 int __weak
arch_kexec_kernel_image_probe(struct kimage
*image
, void *buf
,
419 unsigned long buf_len
)
424 void * __weak
arch_kexec_kernel_image_load(struct kimage
*image
)
426 return ERR_PTR(-ENOEXEC
);
429 void __weak
arch_kimage_file_post_load_cleanup(struct kimage
*image
)
433 int __weak
arch_kexec_kernel_verify_sig(struct kimage
*image
, void *buf
,
434 unsigned long buf_len
)
436 return -EKEYREJECTED
;
439 /* Apply relocations of type RELA */
441 arch_kexec_apply_relocations_add(const Elf_Ehdr
*ehdr
, Elf_Shdr
*sechdrs
,
444 pr_err("RELA relocation unsupported.\n");
448 /* Apply relocations of type REL */
450 arch_kexec_apply_relocations(const Elf_Ehdr
*ehdr
, Elf_Shdr
*sechdrs
,
453 pr_err("REL relocation unsupported.\n");
458 * Free up memory used by kernel, initrd, and command line. This is temporary
459 * memory allocation which is not needed any more after these buffers have
460 * been loaded into separate segments and have been copied elsewhere.
462 static void kimage_file_post_load_cleanup(struct kimage
*image
)
464 struct purgatory_info
*pi
= &image
->purgatory_info
;
466 vfree(image
->kernel_buf
);
467 image
->kernel_buf
= NULL
;
469 vfree(image
->initrd_buf
);
470 image
->initrd_buf
= NULL
;
472 kfree(image
->cmdline_buf
);
473 image
->cmdline_buf
= NULL
;
475 vfree(pi
->purgatory_buf
);
476 pi
->purgatory_buf
= NULL
;
481 /* See if architecture has anything to cleanup post load */
482 arch_kimage_file_post_load_cleanup(image
);
485 * Above call should have called into bootloader to free up
486 * any data stored in kimage->image_loader_data. It should
487 * be ok now to free it up.
489 kfree(image
->image_loader_data
);
490 image
->image_loader_data
= NULL
;
494 * In file mode list of segments is prepared by kernel. Copy relevant
495 * data from user space, do error checking, prepare segment list
498 kimage_file_prepare_segments(struct kimage
*image
, int kernel_fd
, int initrd_fd
,
499 const char __user
*cmdline_ptr
,
500 unsigned long cmdline_len
, unsigned flags
)
505 ret
= copy_file_from_fd(kernel_fd
, &image
->kernel_buf
,
506 &image
->kernel_buf_len
);
510 /* Call arch image probe handlers */
511 ret
= arch_kexec_kernel_image_probe(image
, image
->kernel_buf
,
512 image
->kernel_buf_len
);
517 #ifdef CONFIG_KEXEC_VERIFY_SIG
518 ret
= arch_kexec_kernel_verify_sig(image
, image
->kernel_buf
,
519 image
->kernel_buf_len
);
521 pr_debug("kernel signature verification failed.\n");
524 pr_debug("kernel signature verification successful.\n");
526 /* It is possible that there no initramfs is being loaded */
527 if (!(flags
& KEXEC_FILE_NO_INITRAMFS
)) {
528 ret
= copy_file_from_fd(initrd_fd
, &image
->initrd_buf
,
529 &image
->initrd_buf_len
);
535 image
->cmdline_buf
= kzalloc(cmdline_len
, GFP_KERNEL
);
536 if (!image
->cmdline_buf
) {
541 ret
= copy_from_user(image
->cmdline_buf
, cmdline_ptr
,
548 image
->cmdline_buf_len
= cmdline_len
;
550 /* command line should be a string with last byte null */
551 if (image
->cmdline_buf
[cmdline_len
- 1] != '\0') {
557 /* Call arch image load handlers */
558 ldata
= arch_kexec_kernel_image_load(image
);
561 ret
= PTR_ERR(ldata
);
565 image
->image_loader_data
= ldata
;
567 /* In case of error, free up all allocated memory in this function */
569 kimage_file_post_load_cleanup(image
);
574 kimage_file_alloc_init(struct kimage
**rimage
, int kernel_fd
,
575 int initrd_fd
, const char __user
*cmdline_ptr
,
576 unsigned long cmdline_len
, unsigned long flags
)
579 struct kimage
*image
;
580 bool kexec_on_panic
= flags
& KEXEC_FILE_ON_CRASH
;
582 image
= do_kimage_alloc_init();
586 image
->file_mode
= 1;
588 if (kexec_on_panic
) {
589 /* Enable special crash kernel control page alloc policy. */
590 image
->control_page
= crashk_res
.start
;
591 image
->type
= KEXEC_TYPE_CRASH
;
594 ret
= kimage_file_prepare_segments(image
, kernel_fd
, initrd_fd
,
595 cmdline_ptr
, cmdline_len
, flags
);
599 ret
= sanity_check_segment_list(image
);
601 goto out_free_post_load_bufs
;
604 image
->control_code_page
= kimage_alloc_control_pages(image
,
605 get_order(KEXEC_CONTROL_PAGE_SIZE
));
606 if (!image
->control_code_page
) {
607 pr_err("Could not allocate control_code_buffer\n");
608 goto out_free_post_load_bufs
;
611 if (!kexec_on_panic
) {
612 image
->swap_page
= kimage_alloc_control_pages(image
, 0);
613 if (!image
->swap_page
) {
614 pr_err("Could not allocate swap buffer\n");
615 goto out_free_control_pages
;
621 out_free_control_pages
:
622 kimage_free_page_list(&image
->control_pages
);
623 out_free_post_load_bufs
:
624 kimage_file_post_load_cleanup(image
);
629 #else /* CONFIG_KEXEC_FILE */
630 static inline void kimage_file_post_load_cleanup(struct kimage
*image
) { }
631 #endif /* CONFIG_KEXEC_FILE */
633 static int kimage_is_destination_range(struct kimage
*image
,
639 for (i
= 0; i
< image
->nr_segments
; i
++) {
640 unsigned long mstart
, mend
;
642 mstart
= image
->segment
[i
].mem
;
643 mend
= mstart
+ image
->segment
[i
].memsz
;
644 if ((end
> mstart
) && (start
< mend
))
651 static struct page
*kimage_alloc_pages(gfp_t gfp_mask
, unsigned int order
)
655 pages
= alloc_pages(gfp_mask
, order
);
657 unsigned int count
, i
;
658 pages
->mapping
= NULL
;
659 set_page_private(pages
, order
);
661 for (i
= 0; i
< count
; i
++)
662 SetPageReserved(pages
+ i
);
668 static void kimage_free_pages(struct page
*page
)
670 unsigned int order
, count
, i
;
672 order
= page_private(page
);
674 for (i
= 0; i
< count
; i
++)
675 ClearPageReserved(page
+ i
);
676 __free_pages(page
, order
);
679 static void kimage_free_page_list(struct list_head
*list
)
681 struct list_head
*pos
, *next
;
683 list_for_each_safe(pos
, next
, list
) {
686 page
= list_entry(pos
, struct page
, lru
);
687 list_del(&page
->lru
);
688 kimage_free_pages(page
);
692 static struct page
*kimage_alloc_normal_control_pages(struct kimage
*image
,
695 /* Control pages are special, they are the intermediaries
696 * that are needed while we copy the rest of the pages
697 * to their final resting place. As such they must
698 * not conflict with either the destination addresses
699 * or memory the kernel is already using.
701 * The only case where we really need more than one of
702 * these are for architectures where we cannot disable
703 * the MMU and must instead generate an identity mapped
704 * page table for all of the memory.
706 * At worst this runs in O(N) of the image size.
708 struct list_head extra_pages
;
713 INIT_LIST_HEAD(&extra_pages
);
715 /* Loop while I can allocate a page and the page allocated
716 * is a destination page.
719 unsigned long pfn
, epfn
, addr
, eaddr
;
721 pages
= kimage_alloc_pages(KEXEC_CONTROL_MEMORY_GFP
, order
);
724 pfn
= page_to_pfn(pages
);
726 addr
= pfn
<< PAGE_SHIFT
;
727 eaddr
= epfn
<< PAGE_SHIFT
;
728 if ((epfn
>= (KEXEC_CONTROL_MEMORY_LIMIT
>> PAGE_SHIFT
)) ||
729 kimage_is_destination_range(image
, addr
, eaddr
)) {
730 list_add(&pages
->lru
, &extra_pages
);
736 /* Remember the allocated page... */
737 list_add(&pages
->lru
, &image
->control_pages
);
739 /* Because the page is already in it's destination
740 * location we will never allocate another page at
741 * that address. Therefore kimage_alloc_pages
742 * will not return it (again) and we don't need
743 * to give it an entry in image->segment[].
746 /* Deal with the destination pages I have inadvertently allocated.
748 * Ideally I would convert multi-page allocations into single
749 * page allocations, and add everything to image->dest_pages.
751 * For now it is simpler to just free the pages.
753 kimage_free_page_list(&extra_pages
);
758 static struct page
*kimage_alloc_crash_control_pages(struct kimage
*image
,
761 /* Control pages are special, they are the intermediaries
762 * that are needed while we copy the rest of the pages
763 * to their final resting place. As such they must
764 * not conflict with either the destination addresses
765 * or memory the kernel is already using.
767 * Control pages are also the only pags we must allocate
768 * when loading a crash kernel. All of the other pages
769 * are specified by the segments and we just memcpy
770 * into them directly.
772 * The only case where we really need more than one of
773 * these are for architectures where we cannot disable
774 * the MMU and must instead generate an identity mapped
775 * page table for all of the memory.
777 * Given the low demand this implements a very simple
778 * allocator that finds the first hole of the appropriate
779 * size in the reserved memory region, and allocates all
780 * of the memory up to and including the hole.
782 unsigned long hole_start
, hole_end
, size
;
786 size
= (1 << order
) << PAGE_SHIFT
;
787 hole_start
= (image
->control_page
+ (size
- 1)) & ~(size
- 1);
788 hole_end
= hole_start
+ size
- 1;
789 while (hole_end
<= crashk_res
.end
) {
792 if (hole_end
> KEXEC_CRASH_CONTROL_MEMORY_LIMIT
)
794 /* See if I overlap any of the segments */
795 for (i
= 0; i
< image
->nr_segments
; i
++) {
796 unsigned long mstart
, mend
;
798 mstart
= image
->segment
[i
].mem
;
799 mend
= mstart
+ image
->segment
[i
].memsz
- 1;
800 if ((hole_end
>= mstart
) && (hole_start
<= mend
)) {
801 /* Advance the hole to the end of the segment */
802 hole_start
= (mend
+ (size
- 1)) & ~(size
- 1);
803 hole_end
= hole_start
+ size
- 1;
807 /* If I don't overlap any segments I have found my hole! */
808 if (i
== image
->nr_segments
) {
809 pages
= pfn_to_page(hole_start
>> PAGE_SHIFT
);
814 image
->control_page
= hole_end
;
820 struct page
*kimage_alloc_control_pages(struct kimage
*image
,
823 struct page
*pages
= NULL
;
825 switch (image
->type
) {
826 case KEXEC_TYPE_DEFAULT
:
827 pages
= kimage_alloc_normal_control_pages(image
, order
);
829 case KEXEC_TYPE_CRASH
:
830 pages
= kimage_alloc_crash_control_pages(image
, order
);
837 static int kimage_add_entry(struct kimage
*image
, kimage_entry_t entry
)
839 if (*image
->entry
!= 0)
842 if (image
->entry
== image
->last_entry
) {
843 kimage_entry_t
*ind_page
;
846 page
= kimage_alloc_page(image
, GFP_KERNEL
, KIMAGE_NO_DEST
);
850 ind_page
= page_address(page
);
851 *image
->entry
= virt_to_phys(ind_page
) | IND_INDIRECTION
;
852 image
->entry
= ind_page
;
853 image
->last_entry
= ind_page
+
854 ((PAGE_SIZE
/sizeof(kimage_entry_t
)) - 1);
856 *image
->entry
= entry
;
863 static int kimage_set_destination(struct kimage
*image
,
864 unsigned long destination
)
868 destination
&= PAGE_MASK
;
869 result
= kimage_add_entry(image
, destination
| IND_DESTINATION
);
875 static int kimage_add_page(struct kimage
*image
, unsigned long page
)
880 result
= kimage_add_entry(image
, page
| IND_SOURCE
);
886 static void kimage_free_extra_pages(struct kimage
*image
)
888 /* Walk through and free any extra destination pages I may have */
889 kimage_free_page_list(&image
->dest_pages
);
891 /* Walk through and free any unusable pages I have cached */
892 kimage_free_page_list(&image
->unusable_pages
);
895 static void kimage_terminate(struct kimage
*image
)
897 if (*image
->entry
!= 0)
900 *image
->entry
= IND_DONE
;
903 #define for_each_kimage_entry(image, ptr, entry) \
904 for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE); \
905 ptr = (entry & IND_INDIRECTION) ? \
906 phys_to_virt((entry & PAGE_MASK)) : ptr + 1)
908 static void kimage_free_entry(kimage_entry_t entry
)
912 page
= pfn_to_page(entry
>> PAGE_SHIFT
);
913 kimage_free_pages(page
);
916 static void kimage_free(struct kimage
*image
)
918 kimage_entry_t
*ptr
, entry
;
919 kimage_entry_t ind
= 0;
924 kimage_free_extra_pages(image
);
925 for_each_kimage_entry(image
, ptr
, entry
) {
926 if (entry
& IND_INDIRECTION
) {
927 /* Free the previous indirection page */
928 if (ind
& IND_INDIRECTION
)
929 kimage_free_entry(ind
);
930 /* Save this indirection page until we are
934 } else if (entry
& IND_SOURCE
)
935 kimage_free_entry(entry
);
937 /* Free the final indirection page */
938 if (ind
& IND_INDIRECTION
)
939 kimage_free_entry(ind
);
941 /* Handle any machine specific cleanup */
942 machine_kexec_cleanup(image
);
944 /* Free the kexec control pages... */
945 kimage_free_page_list(&image
->control_pages
);
948 * Free up any temporary buffers allocated. This might hit if
949 * error occurred much later after buffer allocation.
951 if (image
->file_mode
)
952 kimage_file_post_load_cleanup(image
);
957 static kimage_entry_t
*kimage_dst_used(struct kimage
*image
,
960 kimage_entry_t
*ptr
, entry
;
961 unsigned long destination
= 0;
963 for_each_kimage_entry(image
, ptr
, entry
) {
964 if (entry
& IND_DESTINATION
)
965 destination
= entry
& PAGE_MASK
;
966 else if (entry
& IND_SOURCE
) {
967 if (page
== destination
)
969 destination
+= PAGE_SIZE
;
976 static struct page
*kimage_alloc_page(struct kimage
*image
,
978 unsigned long destination
)
981 * Here we implement safeguards to ensure that a source page
982 * is not copied to its destination page before the data on
983 * the destination page is no longer useful.
985 * To do this we maintain the invariant that a source page is
986 * either its own destination page, or it is not a
987 * destination page at all.
989 * That is slightly stronger than required, but the proof
990 * that no problems will not occur is trivial, and the
991 * implementation is simply to verify.
993 * When allocating all pages normally this algorithm will run
994 * in O(N) time, but in the worst case it will run in O(N^2)
995 * time. If the runtime is a problem the data structures can
1002 * Walk through the list of destination pages, and see if I
1005 list_for_each_entry(page
, &image
->dest_pages
, lru
) {
1006 addr
= page_to_pfn(page
) << PAGE_SHIFT
;
1007 if (addr
== destination
) {
1008 list_del(&page
->lru
);
1014 kimage_entry_t
*old
;
1016 /* Allocate a page, if we run out of memory give up */
1017 page
= kimage_alloc_pages(gfp_mask
, 0);
1020 /* If the page cannot be used file it away */
1021 if (page_to_pfn(page
) >
1022 (KEXEC_SOURCE_MEMORY_LIMIT
>> PAGE_SHIFT
)) {
1023 list_add(&page
->lru
, &image
->unusable_pages
);
1026 addr
= page_to_pfn(page
) << PAGE_SHIFT
;
1028 /* If it is the destination page we want use it */
1029 if (addr
== destination
)
1032 /* If the page is not a destination page use it */
1033 if (!kimage_is_destination_range(image
, addr
,
1038 * I know that the page is someones destination page.
1039 * See if there is already a source page for this
1040 * destination page. And if so swap the source pages.
1042 old
= kimage_dst_used(image
, addr
);
1045 unsigned long old_addr
;
1046 struct page
*old_page
;
1048 old_addr
= *old
& PAGE_MASK
;
1049 old_page
= pfn_to_page(old_addr
>> PAGE_SHIFT
);
1050 copy_highpage(page
, old_page
);
1051 *old
= addr
| (*old
& ~PAGE_MASK
);
1053 /* The old page I have found cannot be a
1054 * destination page, so return it if it's
1055 * gfp_flags honor the ones passed in.
1057 if (!(gfp_mask
& __GFP_HIGHMEM
) &&
1058 PageHighMem(old_page
)) {
1059 kimage_free_pages(old_page
);
1066 /* Place the page on the destination list I
1067 * will use it later.
1069 list_add(&page
->lru
, &image
->dest_pages
);
1076 static int kimage_load_normal_segment(struct kimage
*image
,
1077 struct kexec_segment
*segment
)
1079 unsigned long maddr
;
1080 size_t ubytes
, mbytes
;
1082 unsigned char __user
*buf
= NULL
;
1083 unsigned char *kbuf
= NULL
;
1086 if (image
->file_mode
)
1087 kbuf
= segment
->kbuf
;
1090 ubytes
= segment
->bufsz
;
1091 mbytes
= segment
->memsz
;
1092 maddr
= segment
->mem
;
1094 result
= kimage_set_destination(image
, maddr
);
1101 size_t uchunk
, mchunk
;
1103 page
= kimage_alloc_page(image
, GFP_HIGHUSER
, maddr
);
1108 result
= kimage_add_page(image
, page_to_pfn(page
)
1114 /* Start with a clear page */
1116 ptr
+= maddr
& ~PAGE_MASK
;
1117 mchunk
= min_t(size_t, mbytes
,
1118 PAGE_SIZE
- (maddr
& ~PAGE_MASK
));
1119 uchunk
= min(ubytes
, mchunk
);
1121 /* For file based kexec, source pages are in kernel memory */
1122 if (image
->file_mode
)
1123 memcpy(ptr
, kbuf
, uchunk
);
1125 result
= copy_from_user(ptr
, buf
, uchunk
);
1133 if (image
->file_mode
)
1143 static int kimage_load_crash_segment(struct kimage
*image
,
1144 struct kexec_segment
*segment
)
1146 /* For crash dumps kernels we simply copy the data from
1147 * user space to it's destination.
1148 * We do things a page at a time for the sake of kmap.
1150 unsigned long maddr
;
1151 size_t ubytes
, mbytes
;
1153 unsigned char __user
*buf
= NULL
;
1154 unsigned char *kbuf
= NULL
;
1157 if (image
->file_mode
)
1158 kbuf
= segment
->kbuf
;
1161 ubytes
= segment
->bufsz
;
1162 mbytes
= segment
->memsz
;
1163 maddr
= segment
->mem
;
1167 size_t uchunk
, mchunk
;
1169 page
= pfn_to_page(maddr
>> PAGE_SHIFT
);
1175 ptr
+= maddr
& ~PAGE_MASK
;
1176 mchunk
= min_t(size_t, mbytes
,
1177 PAGE_SIZE
- (maddr
& ~PAGE_MASK
));
1178 uchunk
= min(ubytes
, mchunk
);
1179 if (mchunk
> uchunk
) {
1180 /* Zero the trailing part of the page */
1181 memset(ptr
+ uchunk
, 0, mchunk
- uchunk
);
1184 /* For file based kexec, source pages are in kernel memory */
1185 if (image
->file_mode
)
1186 memcpy(ptr
, kbuf
, uchunk
);
1188 result
= copy_from_user(ptr
, buf
, uchunk
);
1189 kexec_flush_icache_page(page
);
1197 if (image
->file_mode
)
1207 static int kimage_load_segment(struct kimage
*image
,
1208 struct kexec_segment
*segment
)
1210 int result
= -ENOMEM
;
1212 switch (image
->type
) {
1213 case KEXEC_TYPE_DEFAULT
:
1214 result
= kimage_load_normal_segment(image
, segment
);
1216 case KEXEC_TYPE_CRASH
:
1217 result
= kimage_load_crash_segment(image
, segment
);
1225 * Exec Kernel system call: for obvious reasons only root may call it.
1227 * This call breaks up into three pieces.
1228 * - A generic part which loads the new kernel from the current
1229 * address space, and very carefully places the data in the
1232 * - A generic part that interacts with the kernel and tells all of
1233 * the devices to shut down. Preventing on-going dmas, and placing
1234 * the devices in a consistent state so a later kernel can
1235 * reinitialize them.
1237 * - A machine specific part that includes the syscall number
1238 * and then copies the image to it's final destination. And
1239 * jumps into the image at entry.
1241 * kexec does not sync, or unmount filesystems so if you need
1242 * that to happen you need to do that yourself.
1244 struct kimage
*kexec_image
;
1245 struct kimage
*kexec_crash_image
;
1246 int kexec_load_disabled
;
1248 static DEFINE_MUTEX(kexec_mutex
);
1250 SYSCALL_DEFINE4(kexec_load
, unsigned long, entry
, unsigned long, nr_segments
,
1251 struct kexec_segment __user
*, segments
, unsigned long, flags
)
1253 struct kimage
**dest_image
, *image
;
1256 /* We only trust the superuser with rebooting the system. */
1257 if (!capable(CAP_SYS_BOOT
) || kexec_load_disabled
)
1261 * Verify we have a legal set of flags
1262 * This leaves us room for future extensions.
1264 if ((flags
& KEXEC_FLAGS
) != (flags
& ~KEXEC_ARCH_MASK
))
1267 /* Verify we are on the appropriate architecture */
1268 if (((flags
& KEXEC_ARCH_MASK
) != KEXEC_ARCH
) &&
1269 ((flags
& KEXEC_ARCH_MASK
) != KEXEC_ARCH_DEFAULT
))
1272 /* Put an artificial cap on the number
1273 * of segments passed to kexec_load.
1275 if (nr_segments
> KEXEC_SEGMENT_MAX
)
1281 /* Because we write directly to the reserved memory
1282 * region when loading crash kernels we need a mutex here to
1283 * prevent multiple crash kernels from attempting to load
1284 * simultaneously, and to prevent a crash kernel from loading
1285 * over the top of a in use crash kernel.
1287 * KISS: always take the mutex.
1289 if (!mutex_trylock(&kexec_mutex
))
1292 dest_image
= &kexec_image
;
1293 if (flags
& KEXEC_ON_CRASH
)
1294 dest_image
= &kexec_crash_image
;
1295 if (nr_segments
> 0) {
1298 if (flags
& KEXEC_ON_CRASH
) {
1300 * Loading another kernel to switch to if this one
1301 * crashes. Free any current crash dump kernel before
1305 kimage_free(xchg(&kexec_crash_image
, NULL
));
1306 result
= kimage_alloc_init(&image
, entry
, nr_segments
,
1308 crash_map_reserved_pages();
1310 /* Loading another kernel to reboot into. */
1312 result
= kimage_alloc_init(&image
, entry
, nr_segments
,
1318 if (flags
& KEXEC_PRESERVE_CONTEXT
)
1319 image
->preserve_context
= 1;
1320 result
= machine_kexec_prepare(image
);
1324 for (i
= 0; i
< nr_segments
; i
++) {
1325 result
= kimage_load_segment(image
, &image
->segment
[i
]);
1329 kimage_terminate(image
);
1330 if (flags
& KEXEC_ON_CRASH
)
1331 crash_unmap_reserved_pages();
1333 /* Install the new kernel, and Uninstall the old */
1334 image
= xchg(dest_image
, image
);
1337 mutex_unlock(&kexec_mutex
);
1344 * Add and remove page tables for crashkernel memory
1346 * Provide an empty default implementation here -- architecture
1347 * code may override this
1349 void __weak
crash_map_reserved_pages(void)
1352 void __weak
crash_unmap_reserved_pages(void)
1355 #ifdef CONFIG_COMPAT
1356 COMPAT_SYSCALL_DEFINE4(kexec_load
, compat_ulong_t
, entry
,
1357 compat_ulong_t
, nr_segments
,
1358 struct compat_kexec_segment __user
*, segments
,
1359 compat_ulong_t
, flags
)
1361 struct compat_kexec_segment in
;
1362 struct kexec_segment out
, __user
*ksegments
;
1363 unsigned long i
, result
;
1365 /* Don't allow clients that don't understand the native
1366 * architecture to do anything.
1368 if ((flags
& KEXEC_ARCH_MASK
) == KEXEC_ARCH_DEFAULT
)
1371 if (nr_segments
> KEXEC_SEGMENT_MAX
)
1374 ksegments
= compat_alloc_user_space(nr_segments
* sizeof(out
));
1375 for (i
= 0; i
< nr_segments
; i
++) {
1376 result
= copy_from_user(&in
, &segments
[i
], sizeof(in
));
1380 out
.buf
= compat_ptr(in
.buf
);
1381 out
.bufsz
= in
.bufsz
;
1383 out
.memsz
= in
.memsz
;
1385 result
= copy_to_user(&ksegments
[i
], &out
, sizeof(out
));
1390 return sys_kexec_load(entry
, nr_segments
, ksegments
, flags
);
1394 #ifdef CONFIG_KEXEC_FILE
1395 SYSCALL_DEFINE5(kexec_file_load
, int, kernel_fd
, int, initrd_fd
,
1396 unsigned long, cmdline_len
, const char __user
*, cmdline_ptr
,
1397 unsigned long, flags
)
1400 struct kimage
**dest_image
, *image
;
1402 /* We only trust the superuser with rebooting the system. */
1403 if (!capable(CAP_SYS_BOOT
) || kexec_load_disabled
)
1406 /* Make sure we have a legal set of flags */
1407 if (flags
!= (flags
& KEXEC_FILE_FLAGS
))
1412 if (!mutex_trylock(&kexec_mutex
))
1415 dest_image
= &kexec_image
;
1416 if (flags
& KEXEC_FILE_ON_CRASH
)
1417 dest_image
= &kexec_crash_image
;
1419 if (flags
& KEXEC_FILE_UNLOAD
)
1423 * In case of crash, new kernel gets loaded in reserved region. It is
1424 * same memory where old crash kernel might be loaded. Free any
1425 * current crash dump kernel before we corrupt it.
1427 if (flags
& KEXEC_FILE_ON_CRASH
)
1428 kimage_free(xchg(&kexec_crash_image
, NULL
));
1430 ret
= kimage_file_alloc_init(&image
, kernel_fd
, initrd_fd
, cmdline_ptr
,
1431 cmdline_len
, flags
);
1435 ret
= machine_kexec_prepare(image
);
1439 ret
= kexec_calculate_store_digests(image
);
1443 for (i
= 0; i
< image
->nr_segments
; i
++) {
1444 struct kexec_segment
*ksegment
;
1446 ksegment
= &image
->segment
[i
];
1447 pr_debug("Loading segment %d: buf=0x%p bufsz=0x%zx mem=0x%lx memsz=0x%zx\n",
1448 i
, ksegment
->buf
, ksegment
->bufsz
, ksegment
->mem
,
1451 ret
= kimage_load_segment(image
, &image
->segment
[i
]);
1456 kimage_terminate(image
);
1459 * Free up any temporary buffers allocated which are not needed
1460 * after image has been loaded
1462 kimage_file_post_load_cleanup(image
);
1464 image
= xchg(dest_image
, image
);
1466 mutex_unlock(&kexec_mutex
);
1471 #endif /* CONFIG_KEXEC_FILE */
1473 void crash_kexec(struct pt_regs
*regs
)
1475 /* Take the kexec_mutex here to prevent sys_kexec_load
1476 * running on one cpu from replacing the crash kernel
1477 * we are using after a panic on a different cpu.
1479 * If the crash kernel was not located in a fixed area
1480 * of memory the xchg(&kexec_crash_image) would be
1481 * sufficient. But since I reuse the memory...
1483 if (mutex_trylock(&kexec_mutex
)) {
1484 if (kexec_crash_image
) {
1485 struct pt_regs fixed_regs
;
1487 crash_setup_regs(&fixed_regs
, regs
);
1488 crash_save_vmcoreinfo();
1489 machine_crash_shutdown(&fixed_regs
);
1490 machine_kexec(kexec_crash_image
);
1492 mutex_unlock(&kexec_mutex
);
1496 size_t crash_get_memory_size(void)
1499 mutex_lock(&kexec_mutex
);
1500 if (crashk_res
.end
!= crashk_res
.start
)
1501 size
= resource_size(&crashk_res
);
1502 mutex_unlock(&kexec_mutex
);
1506 void __weak
crash_free_reserved_phys_range(unsigned long begin
,
1511 for (addr
= begin
; addr
< end
; addr
+= PAGE_SIZE
)
1512 free_reserved_page(pfn_to_page(addr
>> PAGE_SHIFT
));
1515 int crash_shrink_memory(unsigned long new_size
)
1518 unsigned long start
, end
;
1519 unsigned long old_size
;
1520 struct resource
*ram_res
;
1522 mutex_lock(&kexec_mutex
);
1524 if (kexec_crash_image
) {
1528 start
= crashk_res
.start
;
1529 end
= crashk_res
.end
;
1530 old_size
= (end
== 0) ? 0 : end
- start
+ 1;
1531 if (new_size
>= old_size
) {
1532 ret
= (new_size
== old_size
) ? 0 : -EINVAL
;
1536 ram_res
= kzalloc(sizeof(*ram_res
), GFP_KERNEL
);
1542 start
= roundup(start
, KEXEC_CRASH_MEM_ALIGN
);
1543 end
= roundup(start
+ new_size
, KEXEC_CRASH_MEM_ALIGN
);
1545 crash_map_reserved_pages();
1546 crash_free_reserved_phys_range(end
, crashk_res
.end
);
1548 if ((start
== end
) && (crashk_res
.parent
!= NULL
))
1549 release_resource(&crashk_res
);
1551 ram_res
->start
= end
;
1552 ram_res
->end
= crashk_res
.end
;
1553 ram_res
->flags
= IORESOURCE_BUSY
| IORESOURCE_MEM
;
1554 ram_res
->name
= "System RAM";
1556 crashk_res
.end
= end
- 1;
1558 insert_resource(&iomem_resource
, ram_res
);
1559 crash_unmap_reserved_pages();
1562 mutex_unlock(&kexec_mutex
);
1566 static u32
*append_elf_note(u32
*buf
, char *name
, unsigned type
, void *data
,
1569 struct elf_note note
;
1571 note
.n_namesz
= strlen(name
) + 1;
1572 note
.n_descsz
= data_len
;
1574 memcpy(buf
, ¬e
, sizeof(note
));
1575 buf
+= (sizeof(note
) + 3)/4;
1576 memcpy(buf
, name
, note
.n_namesz
);
1577 buf
+= (note
.n_namesz
+ 3)/4;
1578 memcpy(buf
, data
, note
.n_descsz
);
1579 buf
+= (note
.n_descsz
+ 3)/4;
1584 static void final_note(u32
*buf
)
1586 struct elf_note note
;
1591 memcpy(buf
, ¬e
, sizeof(note
));
1594 void crash_save_cpu(struct pt_regs
*regs
, int cpu
)
1596 struct elf_prstatus prstatus
;
1599 if ((cpu
< 0) || (cpu
>= nr_cpu_ids
))
1602 /* Using ELF notes here is opportunistic.
1603 * I need a well defined structure format
1604 * for the data I pass, and I need tags
1605 * on the data to indicate what information I have
1606 * squirrelled away. ELF notes happen to provide
1607 * all of that, so there is no need to invent something new.
1609 buf
= (u32
*)per_cpu_ptr(crash_notes
, cpu
);
1612 memset(&prstatus
, 0, sizeof(prstatus
));
1613 prstatus
.pr_pid
= current
->pid
;
1614 elf_core_copy_kernel_regs(&prstatus
.pr_reg
, regs
);
1615 buf
= append_elf_note(buf
, KEXEC_CORE_NOTE_NAME
, NT_PRSTATUS
,
1616 &prstatus
, sizeof(prstatus
));
1620 static int __init
crash_notes_memory_init(void)
1622 /* Allocate memory for saving cpu registers. */
1623 crash_notes
= alloc_percpu(note_buf_t
);
1625 pr_warn("Kexec: Memory allocation for saving cpu register states failed\n");
1630 subsys_initcall(crash_notes_memory_init
);
1634 * parsing the "crashkernel" commandline
1636 * this code is intended to be called from architecture specific code
1641 * This function parses command lines in the format
1643 * crashkernel=ramsize-range:size[,...][@offset]
1645 * The function returns 0 on success and -EINVAL on failure.
1647 static int __init
parse_crashkernel_mem(char *cmdline
,
1648 unsigned long long system_ram
,
1649 unsigned long long *crash_size
,
1650 unsigned long long *crash_base
)
1652 char *cur
= cmdline
, *tmp
;
1654 /* for each entry of the comma-separated list */
1656 unsigned long long start
, end
= ULLONG_MAX
, size
;
1658 /* get the start of the range */
1659 start
= memparse(cur
, &tmp
);
1661 pr_warn("crashkernel: Memory value expected\n");
1666 pr_warn("crashkernel: '-' expected\n");
1671 /* if no ':' is here, than we read the end */
1673 end
= memparse(cur
, &tmp
);
1675 pr_warn("crashkernel: Memory value expected\n");
1680 pr_warn("crashkernel: end <= start\n");
1686 pr_warn("crashkernel: ':' expected\n");
1691 size
= memparse(cur
, &tmp
);
1693 pr_warn("Memory value expected\n");
1697 if (size
>= system_ram
) {
1698 pr_warn("crashkernel: invalid size\n");
1703 if (system_ram
>= start
&& system_ram
< end
) {
1707 } while (*cur
++ == ',');
1709 if (*crash_size
> 0) {
1710 while (*cur
&& *cur
!= ' ' && *cur
!= '@')
1714 *crash_base
= memparse(cur
, &tmp
);
1716 pr_warn("Memory value expected after '@'\n");
1726 * That function parses "simple" (old) crashkernel command lines like
1728 * crashkernel=size[@offset]
1730 * It returns 0 on success and -EINVAL on failure.
1732 static int __init
parse_crashkernel_simple(char *cmdline
,
1733 unsigned long long *crash_size
,
1734 unsigned long long *crash_base
)
1736 char *cur
= cmdline
;
1738 *crash_size
= memparse(cmdline
, &cur
);
1739 if (cmdline
== cur
) {
1740 pr_warn("crashkernel: memory value expected\n");
1745 *crash_base
= memparse(cur
+1, &cur
);
1746 else if (*cur
!= ' ' && *cur
!= '\0') {
1747 pr_warn("crashkernel: unrecognized char\n");
1754 #define SUFFIX_HIGH 0
1755 #define SUFFIX_LOW 1
1756 #define SUFFIX_NULL 2
1757 static __initdata
char *suffix_tbl
[] = {
1758 [SUFFIX_HIGH
] = ",high",
1759 [SUFFIX_LOW
] = ",low",
1760 [SUFFIX_NULL
] = NULL
,
1764 * That function parses "suffix" crashkernel command lines like
1766 * crashkernel=size,[high|low]
1768 * It returns 0 on success and -EINVAL on failure.
1770 static int __init
parse_crashkernel_suffix(char *cmdline
,
1771 unsigned long long *crash_size
,
1774 char *cur
= cmdline
;
1776 *crash_size
= memparse(cmdline
, &cur
);
1777 if (cmdline
== cur
) {
1778 pr_warn("crashkernel: memory value expected\n");
1782 /* check with suffix */
1783 if (strncmp(cur
, suffix
, strlen(suffix
))) {
1784 pr_warn("crashkernel: unrecognized char\n");
1787 cur
+= strlen(suffix
);
1788 if (*cur
!= ' ' && *cur
!= '\0') {
1789 pr_warn("crashkernel: unrecognized char\n");
1796 static __init
char *get_last_crashkernel(char *cmdline
,
1800 char *p
= cmdline
, *ck_cmdline
= NULL
;
1802 /* find crashkernel and use the last one if there are more */
1803 p
= strstr(p
, name
);
1805 char *end_p
= strchr(p
, ' ');
1809 end_p
= p
+ strlen(p
);
1814 /* skip the one with any known suffix */
1815 for (i
= 0; suffix_tbl
[i
]; i
++) {
1816 q
= end_p
- strlen(suffix_tbl
[i
]);
1817 if (!strncmp(q
, suffix_tbl
[i
],
1818 strlen(suffix_tbl
[i
])))
1823 q
= end_p
- strlen(suffix
);
1824 if (!strncmp(q
, suffix
, strlen(suffix
)))
1828 p
= strstr(p
+1, name
);
1837 static int __init
__parse_crashkernel(char *cmdline
,
1838 unsigned long long system_ram
,
1839 unsigned long long *crash_size
,
1840 unsigned long long *crash_base
,
1844 char *first_colon
, *first_space
;
1847 BUG_ON(!crash_size
|| !crash_base
);
1851 ck_cmdline
= get_last_crashkernel(cmdline
, name
, suffix
);
1856 ck_cmdline
+= strlen(name
);
1859 return parse_crashkernel_suffix(ck_cmdline
, crash_size
,
1862 * if the commandline contains a ':', then that's the extended
1863 * syntax -- if not, it must be the classic syntax
1865 first_colon
= strchr(ck_cmdline
, ':');
1866 first_space
= strchr(ck_cmdline
, ' ');
1867 if (first_colon
&& (!first_space
|| first_colon
< first_space
))
1868 return parse_crashkernel_mem(ck_cmdline
, system_ram
,
1869 crash_size
, crash_base
);
1871 return parse_crashkernel_simple(ck_cmdline
, crash_size
, crash_base
);
1875 * That function is the entry point for command line parsing and should be
1876 * called from the arch-specific code.
1878 int __init
parse_crashkernel(char *cmdline
,
1879 unsigned long long system_ram
,
1880 unsigned long long *crash_size
,
1881 unsigned long long *crash_base
)
1883 return __parse_crashkernel(cmdline
, system_ram
, crash_size
, crash_base
,
1884 "crashkernel=", NULL
);
1887 int __init
parse_crashkernel_high(char *cmdline
,
1888 unsigned long long system_ram
,
1889 unsigned long long *crash_size
,
1890 unsigned long long *crash_base
)
1892 return __parse_crashkernel(cmdline
, system_ram
, crash_size
, crash_base
,
1893 "crashkernel=", suffix_tbl
[SUFFIX_HIGH
]);
1896 int __init
parse_crashkernel_low(char *cmdline
,
1897 unsigned long long system_ram
,
1898 unsigned long long *crash_size
,
1899 unsigned long long *crash_base
)
1901 return __parse_crashkernel(cmdline
, system_ram
, crash_size
, crash_base
,
1902 "crashkernel=", suffix_tbl
[SUFFIX_LOW
]);
1905 static void update_vmcoreinfo_note(void)
1907 u32
*buf
= vmcoreinfo_note
;
1909 if (!vmcoreinfo_size
)
1911 buf
= append_elf_note(buf
, VMCOREINFO_NOTE_NAME
, 0, vmcoreinfo_data
,
1916 void crash_save_vmcoreinfo(void)
1918 vmcoreinfo_append_str("CRASHTIME=%ld\n", get_seconds());
1919 update_vmcoreinfo_note();
1922 void vmcoreinfo_append_str(const char *fmt
, ...)
1928 va_start(args
, fmt
);
1929 r
= vscnprintf(buf
, sizeof(buf
), fmt
, args
);
1932 r
= min(r
, vmcoreinfo_max_size
- vmcoreinfo_size
);
1934 memcpy(&vmcoreinfo_data
[vmcoreinfo_size
], buf
, r
);
1936 vmcoreinfo_size
+= r
;
1940 * provide an empty default implementation here -- architecture
1941 * code may override this
1943 void __weak
arch_crash_save_vmcoreinfo(void)
1946 unsigned long __weak
paddr_vmcoreinfo_note(void)
1948 return __pa((unsigned long)(char *)&vmcoreinfo_note
);
1951 static int __init
crash_save_vmcoreinfo_init(void)
1953 VMCOREINFO_OSRELEASE(init_uts_ns
.name
.release
);
1954 VMCOREINFO_PAGESIZE(PAGE_SIZE
);
1956 VMCOREINFO_SYMBOL(init_uts_ns
);
1957 VMCOREINFO_SYMBOL(node_online_map
);
1959 VMCOREINFO_SYMBOL(swapper_pg_dir
);
1961 VMCOREINFO_SYMBOL(_stext
);
1962 VMCOREINFO_SYMBOL(vmap_area_list
);
1964 #ifndef CONFIG_NEED_MULTIPLE_NODES
1965 VMCOREINFO_SYMBOL(mem_map
);
1966 VMCOREINFO_SYMBOL(contig_page_data
);
1968 #ifdef CONFIG_SPARSEMEM
1969 VMCOREINFO_SYMBOL(mem_section
);
1970 VMCOREINFO_LENGTH(mem_section
, NR_SECTION_ROOTS
);
1971 VMCOREINFO_STRUCT_SIZE(mem_section
);
1972 VMCOREINFO_OFFSET(mem_section
, section_mem_map
);
1974 VMCOREINFO_STRUCT_SIZE(page
);
1975 VMCOREINFO_STRUCT_SIZE(pglist_data
);
1976 VMCOREINFO_STRUCT_SIZE(zone
);
1977 VMCOREINFO_STRUCT_SIZE(free_area
);
1978 VMCOREINFO_STRUCT_SIZE(list_head
);
1979 VMCOREINFO_SIZE(nodemask_t
);
1980 VMCOREINFO_OFFSET(page
, flags
);
1981 VMCOREINFO_OFFSET(page
, _count
);
1982 VMCOREINFO_OFFSET(page
, mapping
);
1983 VMCOREINFO_OFFSET(page
, lru
);
1984 VMCOREINFO_OFFSET(page
, _mapcount
);
1985 VMCOREINFO_OFFSET(page
, private);
1986 VMCOREINFO_OFFSET(pglist_data
, node_zones
);
1987 VMCOREINFO_OFFSET(pglist_data
, nr_zones
);
1988 #ifdef CONFIG_FLAT_NODE_MEM_MAP
1989 VMCOREINFO_OFFSET(pglist_data
, node_mem_map
);
1991 VMCOREINFO_OFFSET(pglist_data
, node_start_pfn
);
1992 VMCOREINFO_OFFSET(pglist_data
, node_spanned_pages
);
1993 VMCOREINFO_OFFSET(pglist_data
, node_id
);
1994 VMCOREINFO_OFFSET(zone
, free_area
);
1995 VMCOREINFO_OFFSET(zone
, vm_stat
);
1996 VMCOREINFO_OFFSET(zone
, spanned_pages
);
1997 VMCOREINFO_OFFSET(free_area
, free_list
);
1998 VMCOREINFO_OFFSET(list_head
, next
);
1999 VMCOREINFO_OFFSET(list_head
, prev
);
2000 VMCOREINFO_OFFSET(vmap_area
, va_start
);
2001 VMCOREINFO_OFFSET(vmap_area
, list
);
2002 VMCOREINFO_LENGTH(zone
.free_area
, MAX_ORDER
);
2003 log_buf_kexec_setup();
2004 VMCOREINFO_LENGTH(free_area
.free_list
, MIGRATE_TYPES
);
2005 VMCOREINFO_NUMBER(NR_FREE_PAGES
);
2006 VMCOREINFO_NUMBER(PG_lru
);
2007 VMCOREINFO_NUMBER(PG_private
);
2008 VMCOREINFO_NUMBER(PG_swapcache
);
2009 VMCOREINFO_NUMBER(PG_slab
);
2010 #ifdef CONFIG_MEMORY_FAILURE
2011 VMCOREINFO_NUMBER(PG_hwpoison
);
2013 VMCOREINFO_NUMBER(PG_head_mask
);
2014 VMCOREINFO_NUMBER(PAGE_BUDDY_MAPCOUNT_VALUE
);
2015 #ifdef CONFIG_HUGETLBFS
2016 VMCOREINFO_SYMBOL(free_huge_page
);
2019 arch_crash_save_vmcoreinfo();
2020 update_vmcoreinfo_note();
2025 subsys_initcall(crash_save_vmcoreinfo_init
);
2027 #ifdef CONFIG_KEXEC_FILE
2028 static int locate_mem_hole_top_down(unsigned long start
, unsigned long end
,
2029 struct kexec_buf
*kbuf
)
2031 struct kimage
*image
= kbuf
->image
;
2032 unsigned long temp_start
, temp_end
;
2034 temp_end
= min(end
, kbuf
->buf_max
);
2035 temp_start
= temp_end
- kbuf
->memsz
;
2038 /* align down start */
2039 temp_start
= temp_start
& (~(kbuf
->buf_align
- 1));
2041 if (temp_start
< start
|| temp_start
< kbuf
->buf_min
)
2044 temp_end
= temp_start
+ kbuf
->memsz
- 1;
2047 * Make sure this does not conflict with any of existing
2050 if (kimage_is_destination_range(image
, temp_start
, temp_end
)) {
2051 temp_start
= temp_start
- PAGE_SIZE
;
2055 /* We found a suitable memory range */
2059 /* If we are here, we found a suitable memory range */
2060 kbuf
->mem
= temp_start
;
2062 /* Success, stop navigating through remaining System RAM ranges */
2066 static int locate_mem_hole_bottom_up(unsigned long start
, unsigned long end
,
2067 struct kexec_buf
*kbuf
)
2069 struct kimage
*image
= kbuf
->image
;
2070 unsigned long temp_start
, temp_end
;
2072 temp_start
= max(start
, kbuf
->buf_min
);
2075 temp_start
= ALIGN(temp_start
, kbuf
->buf_align
);
2076 temp_end
= temp_start
+ kbuf
->memsz
- 1;
2078 if (temp_end
> end
|| temp_end
> kbuf
->buf_max
)
2081 * Make sure this does not conflict with any of existing
2084 if (kimage_is_destination_range(image
, temp_start
, temp_end
)) {
2085 temp_start
= temp_start
+ PAGE_SIZE
;
2089 /* We found a suitable memory range */
2093 /* If we are here, we found a suitable memory range */
2094 kbuf
->mem
= temp_start
;
2096 /* Success, stop navigating through remaining System RAM ranges */
2100 static int locate_mem_hole_callback(u64 start
, u64 end
, void *arg
)
2102 struct kexec_buf
*kbuf
= (struct kexec_buf
*)arg
;
2103 unsigned long sz
= end
- start
+ 1;
2105 /* Returning 0 will take to next memory range */
2106 if (sz
< kbuf
->memsz
)
2109 if (end
< kbuf
->buf_min
|| start
> kbuf
->buf_max
)
2113 * Allocate memory top down with-in ram range. Otherwise bottom up
2117 return locate_mem_hole_top_down(start
, end
, kbuf
);
2118 return locate_mem_hole_bottom_up(start
, end
, kbuf
);
2122 * Helper function for placing a buffer in a kexec segment. This assumes
2123 * that kexec_mutex is held.
2125 int kexec_add_buffer(struct kimage
*image
, char *buffer
, unsigned long bufsz
,
2126 unsigned long memsz
, unsigned long buf_align
,
2127 unsigned long buf_min
, unsigned long buf_max
,
2128 bool top_down
, unsigned long *load_addr
)
2131 struct kexec_segment
*ksegment
;
2132 struct kexec_buf buf
, *kbuf
;
2135 /* Currently adding segment this way is allowed only in file mode */
2136 if (!image
->file_mode
)
2139 if (image
->nr_segments
>= KEXEC_SEGMENT_MAX
)
2143 * Make sure we are not trying to add buffer after allocating
2144 * control pages. All segments need to be placed first before
2145 * any control pages are allocated. As control page allocation
2146 * logic goes through list of segments to make sure there are
2147 * no destination overlaps.
2149 if (!list_empty(&image
->control_pages
)) {
2154 memset(&buf
, 0, sizeof(struct kexec_buf
));
2156 kbuf
->image
= image
;
2157 kbuf
->buffer
= buffer
;
2158 kbuf
->bufsz
= bufsz
;
2160 kbuf
->memsz
= ALIGN(memsz
, PAGE_SIZE
);
2161 kbuf
->buf_align
= max(buf_align
, PAGE_SIZE
);
2162 kbuf
->buf_min
= buf_min
;
2163 kbuf
->buf_max
= buf_max
;
2164 kbuf
->top_down
= top_down
;
2166 /* Walk the RAM ranges and allocate a suitable range for the buffer */
2167 if (image
->type
== KEXEC_TYPE_CRASH
)
2168 ret
= walk_iomem_res("Crash kernel",
2169 IORESOURCE_MEM
| IORESOURCE_BUSY
,
2170 crashk_res
.start
, crashk_res
.end
, kbuf
,
2171 locate_mem_hole_callback
);
2173 ret
= walk_system_ram_res(0, -1, kbuf
,
2174 locate_mem_hole_callback
);
2176 /* A suitable memory range could not be found for buffer */
2177 return -EADDRNOTAVAIL
;
2180 /* Found a suitable memory range */
2181 ksegment
= &image
->segment
[image
->nr_segments
];
2182 ksegment
->kbuf
= kbuf
->buffer
;
2183 ksegment
->bufsz
= kbuf
->bufsz
;
2184 ksegment
->mem
= kbuf
->mem
;
2185 ksegment
->memsz
= kbuf
->memsz
;
2186 image
->nr_segments
++;
2187 *load_addr
= ksegment
->mem
;
2191 /* Calculate and store the digest of segments */
2192 static int kexec_calculate_store_digests(struct kimage
*image
)
2194 struct crypto_shash
*tfm
;
2195 struct shash_desc
*desc
;
2196 int ret
= 0, i
, j
, zero_buf_sz
, sha_region_sz
;
2197 size_t desc_size
, nullsz
;
2200 struct kexec_sha_region
*sha_regions
;
2201 struct purgatory_info
*pi
= &image
->purgatory_info
;
2203 zero_buf
= __va(page_to_pfn(ZERO_PAGE(0)) << PAGE_SHIFT
);
2204 zero_buf_sz
= PAGE_SIZE
;
2206 tfm
= crypto_alloc_shash("sha256", 0, 0);
2212 desc_size
= crypto_shash_descsize(tfm
) + sizeof(*desc
);
2213 desc
= kzalloc(desc_size
, GFP_KERNEL
);
2219 sha_region_sz
= KEXEC_SEGMENT_MAX
* sizeof(struct kexec_sha_region
);
2220 sha_regions
= vzalloc(sha_region_sz
);
2227 ret
= crypto_shash_init(desc
);
2229 goto out_free_sha_regions
;
2231 digest
= kzalloc(SHA256_DIGEST_SIZE
, GFP_KERNEL
);
2234 goto out_free_sha_regions
;
2237 for (j
= i
= 0; i
< image
->nr_segments
; i
++) {
2238 struct kexec_segment
*ksegment
;
2240 ksegment
= &image
->segment
[i
];
2242 * Skip purgatory as it will be modified once we put digest
2243 * info in purgatory.
2245 if (ksegment
->kbuf
== pi
->purgatory_buf
)
2248 ret
= crypto_shash_update(desc
, ksegment
->kbuf
,
2254 * Assume rest of the buffer is filled with zero and
2255 * update digest accordingly.
2257 nullsz
= ksegment
->memsz
- ksegment
->bufsz
;
2259 unsigned long bytes
= nullsz
;
2261 if (bytes
> zero_buf_sz
)
2262 bytes
= zero_buf_sz
;
2263 ret
= crypto_shash_update(desc
, zero_buf
, bytes
);
2272 sha_regions
[j
].start
= ksegment
->mem
;
2273 sha_regions
[j
].len
= ksegment
->memsz
;
2278 ret
= crypto_shash_final(desc
, digest
);
2280 goto out_free_digest
;
2281 ret
= kexec_purgatory_get_set_symbol(image
, "sha_regions",
2282 sha_regions
, sha_region_sz
, 0);
2284 goto out_free_digest
;
2286 ret
= kexec_purgatory_get_set_symbol(image
, "sha256_digest",
2287 digest
, SHA256_DIGEST_SIZE
, 0);
2289 goto out_free_digest
;
2294 out_free_sha_regions
:
2304 /* Actually load purgatory. Lot of code taken from kexec-tools */
2305 static int __kexec_load_purgatory(struct kimage
*image
, unsigned long min
,
2306 unsigned long max
, int top_down
)
2308 struct purgatory_info
*pi
= &image
->purgatory_info
;
2309 unsigned long align
, buf_align
, bss_align
, buf_sz
, bss_sz
, bss_pad
;
2310 unsigned long memsz
, entry
, load_addr
, curr_load_addr
, bss_addr
, offset
;
2311 unsigned char *buf_addr
, *src
;
2312 int i
, ret
= 0, entry_sidx
= -1;
2313 const Elf_Shdr
*sechdrs_c
;
2314 Elf_Shdr
*sechdrs
= NULL
;
2315 void *purgatory_buf
= NULL
;
2318 * sechdrs_c points to section headers in purgatory and are read
2319 * only. No modifications allowed.
2321 sechdrs_c
= (void *)pi
->ehdr
+ pi
->ehdr
->e_shoff
;
2324 * We can not modify sechdrs_c[] and its fields. It is read only.
2325 * Copy it over to a local copy where one can store some temporary
2326 * data and free it at the end. We need to modify ->sh_addr and
2327 * ->sh_offset fields to keep track of permanent and temporary
2328 * locations of sections.
2330 sechdrs
= vzalloc(pi
->ehdr
->e_shnum
* sizeof(Elf_Shdr
));
2334 memcpy(sechdrs
, sechdrs_c
, pi
->ehdr
->e_shnum
* sizeof(Elf_Shdr
));
2337 * We seem to have multiple copies of sections. First copy is which
2338 * is embedded in kernel in read only section. Some of these sections
2339 * will be copied to a temporary buffer and relocated. And these
2340 * sections will finally be copied to their final destination at
2341 * segment load time.
2343 * Use ->sh_offset to reflect section address in memory. It will
2344 * point to original read only copy if section is not allocatable.
2345 * Otherwise it will point to temporary copy which will be relocated.
2347 * Use ->sh_addr to contain final address of the section where it
2348 * will go during execution time.
2350 for (i
= 0; i
< pi
->ehdr
->e_shnum
; i
++) {
2351 if (sechdrs
[i
].sh_type
== SHT_NOBITS
)
2354 sechdrs
[i
].sh_offset
= (unsigned long)pi
->ehdr
+
2355 sechdrs
[i
].sh_offset
;
2359 * Identify entry point section and make entry relative to section
2362 entry
= pi
->ehdr
->e_entry
;
2363 for (i
= 0; i
< pi
->ehdr
->e_shnum
; i
++) {
2364 if (!(sechdrs
[i
].sh_flags
& SHF_ALLOC
))
2367 if (!(sechdrs
[i
].sh_flags
& SHF_EXECINSTR
))
2370 /* Make entry section relative */
2371 if (sechdrs
[i
].sh_addr
<= pi
->ehdr
->e_entry
&&
2372 ((sechdrs
[i
].sh_addr
+ sechdrs
[i
].sh_size
) >
2373 pi
->ehdr
->e_entry
)) {
2375 entry
-= sechdrs
[i
].sh_addr
;
2380 /* Determine how much memory is needed to load relocatable object. */
2386 for (i
= 0; i
< pi
->ehdr
->e_shnum
; i
++) {
2387 if (!(sechdrs
[i
].sh_flags
& SHF_ALLOC
))
2390 align
= sechdrs
[i
].sh_addralign
;
2391 if (sechdrs
[i
].sh_type
!= SHT_NOBITS
) {
2392 if (buf_align
< align
)
2394 buf_sz
= ALIGN(buf_sz
, align
);
2395 buf_sz
+= sechdrs
[i
].sh_size
;
2398 if (bss_align
< align
)
2400 bss_sz
= ALIGN(bss_sz
, align
);
2401 bss_sz
+= sechdrs
[i
].sh_size
;
2405 /* Determine the bss padding required to align bss properly */
2407 if (buf_sz
& (bss_align
- 1))
2408 bss_pad
= bss_align
- (buf_sz
& (bss_align
- 1));
2410 memsz
= buf_sz
+ bss_pad
+ bss_sz
;
2412 /* Allocate buffer for purgatory */
2413 purgatory_buf
= vzalloc(buf_sz
);
2414 if (!purgatory_buf
) {
2419 if (buf_align
< bss_align
)
2420 buf_align
= bss_align
;
2422 /* Add buffer to segment list */
2423 ret
= kexec_add_buffer(image
, purgatory_buf
, buf_sz
, memsz
,
2424 buf_align
, min
, max
, top_down
,
2425 &pi
->purgatory_load_addr
);
2429 /* Load SHF_ALLOC sections */
2430 buf_addr
= purgatory_buf
;
2431 load_addr
= curr_load_addr
= pi
->purgatory_load_addr
;
2432 bss_addr
= load_addr
+ buf_sz
+ bss_pad
;
2434 for (i
= 0; i
< pi
->ehdr
->e_shnum
; i
++) {
2435 if (!(sechdrs
[i
].sh_flags
& SHF_ALLOC
))
2438 align
= sechdrs
[i
].sh_addralign
;
2439 if (sechdrs
[i
].sh_type
!= SHT_NOBITS
) {
2440 curr_load_addr
= ALIGN(curr_load_addr
, align
);
2441 offset
= curr_load_addr
- load_addr
;
2442 /* We already modifed ->sh_offset to keep src addr */
2443 src
= (char *) sechdrs
[i
].sh_offset
;
2444 memcpy(buf_addr
+ offset
, src
, sechdrs
[i
].sh_size
);
2446 /* Store load address and source address of section */
2447 sechdrs
[i
].sh_addr
= curr_load_addr
;
2450 * This section got copied to temporary buffer. Update
2451 * ->sh_offset accordingly.
2453 sechdrs
[i
].sh_offset
= (unsigned long)(buf_addr
+ offset
);
2455 /* Advance to the next address */
2456 curr_load_addr
+= sechdrs
[i
].sh_size
;
2458 bss_addr
= ALIGN(bss_addr
, align
);
2459 sechdrs
[i
].sh_addr
= bss_addr
;
2460 bss_addr
+= sechdrs
[i
].sh_size
;
2464 /* Update entry point based on load address of text section */
2465 if (entry_sidx
>= 0)
2466 entry
+= sechdrs
[entry_sidx
].sh_addr
;
2468 /* Make kernel jump to purgatory after shutdown */
2469 image
->start
= entry
;
2471 /* Used later to get/set symbol values */
2472 pi
->sechdrs
= sechdrs
;
2475 * Used later to identify which section is purgatory and skip it
2476 * from checksumming.
2478 pi
->purgatory_buf
= purgatory_buf
;
2482 vfree(purgatory_buf
);
2486 static int kexec_apply_relocations(struct kimage
*image
)
2489 struct purgatory_info
*pi
= &image
->purgatory_info
;
2490 Elf_Shdr
*sechdrs
= pi
->sechdrs
;
2492 /* Apply relocations */
2493 for (i
= 0; i
< pi
->ehdr
->e_shnum
; i
++) {
2494 Elf_Shdr
*section
, *symtab
;
2496 if (sechdrs
[i
].sh_type
!= SHT_RELA
&&
2497 sechdrs
[i
].sh_type
!= SHT_REL
)
2501 * For section of type SHT_RELA/SHT_REL,
2502 * ->sh_link contains section header index of associated
2503 * symbol table. And ->sh_info contains section header
2504 * index of section to which relocations apply.
2506 if (sechdrs
[i
].sh_info
>= pi
->ehdr
->e_shnum
||
2507 sechdrs
[i
].sh_link
>= pi
->ehdr
->e_shnum
)
2510 section
= &sechdrs
[sechdrs
[i
].sh_info
];
2511 symtab
= &sechdrs
[sechdrs
[i
].sh_link
];
2513 if (!(section
->sh_flags
& SHF_ALLOC
))
2517 * symtab->sh_link contain section header index of associated
2520 if (symtab
->sh_link
>= pi
->ehdr
->e_shnum
)
2521 /* Invalid section number? */
2525 * Respective architecture needs to provide support for applying
2526 * relocations of type SHT_RELA/SHT_REL.
2528 if (sechdrs
[i
].sh_type
== SHT_RELA
)
2529 ret
= arch_kexec_apply_relocations_add(pi
->ehdr
,
2531 else if (sechdrs
[i
].sh_type
== SHT_REL
)
2532 ret
= arch_kexec_apply_relocations(pi
->ehdr
,
2541 /* Load relocatable purgatory object and relocate it appropriately */
2542 int kexec_load_purgatory(struct kimage
*image
, unsigned long min
,
2543 unsigned long max
, int top_down
,
2544 unsigned long *load_addr
)
2546 struct purgatory_info
*pi
= &image
->purgatory_info
;
2549 if (kexec_purgatory_size
<= 0)
2552 if (kexec_purgatory_size
< sizeof(Elf_Ehdr
))
2555 pi
->ehdr
= (Elf_Ehdr
*)kexec_purgatory
;
2557 if (memcmp(pi
->ehdr
->e_ident
, ELFMAG
, SELFMAG
) != 0
2558 || pi
->ehdr
->e_type
!= ET_REL
2559 || !elf_check_arch(pi
->ehdr
)
2560 || pi
->ehdr
->e_shentsize
!= sizeof(Elf_Shdr
))
2563 if (pi
->ehdr
->e_shoff
>= kexec_purgatory_size
2564 || (pi
->ehdr
->e_shnum
* sizeof(Elf_Shdr
) >
2565 kexec_purgatory_size
- pi
->ehdr
->e_shoff
))
2568 ret
= __kexec_load_purgatory(image
, min
, max
, top_down
);
2572 ret
= kexec_apply_relocations(image
);
2576 *load_addr
= pi
->purgatory_load_addr
;
2580 vfree(pi
->purgatory_buf
);
2584 static Elf_Sym
*kexec_purgatory_find_symbol(struct purgatory_info
*pi
,
2593 if (!pi
->sechdrs
|| !pi
->ehdr
)
2596 sechdrs
= pi
->sechdrs
;
2599 for (i
= 0; i
< ehdr
->e_shnum
; i
++) {
2600 if (sechdrs
[i
].sh_type
!= SHT_SYMTAB
)
2603 if (sechdrs
[i
].sh_link
>= ehdr
->e_shnum
)
2604 /* Invalid strtab section number */
2606 strtab
= (char *)sechdrs
[sechdrs
[i
].sh_link
].sh_offset
;
2607 syms
= (Elf_Sym
*)sechdrs
[i
].sh_offset
;
2609 /* Go through symbols for a match */
2610 for (k
= 0; k
< sechdrs
[i
].sh_size
/sizeof(Elf_Sym
); k
++) {
2611 if (ELF_ST_BIND(syms
[k
].st_info
) != STB_GLOBAL
)
2614 if (strcmp(strtab
+ syms
[k
].st_name
, name
) != 0)
2617 if (syms
[k
].st_shndx
== SHN_UNDEF
||
2618 syms
[k
].st_shndx
>= ehdr
->e_shnum
) {
2619 pr_debug("Symbol: %s has bad section index %d.\n",
2620 name
, syms
[k
].st_shndx
);
2624 /* Found the symbol we are looking for */
2632 void *kexec_purgatory_get_symbol_addr(struct kimage
*image
, const char *name
)
2634 struct purgatory_info
*pi
= &image
->purgatory_info
;
2638 sym
= kexec_purgatory_find_symbol(pi
, name
);
2640 return ERR_PTR(-EINVAL
);
2642 sechdr
= &pi
->sechdrs
[sym
->st_shndx
];
2645 * Returns the address where symbol will finally be loaded after
2646 * kexec_load_segment()
2648 return (void *)(sechdr
->sh_addr
+ sym
->st_value
);
2652 * Get or set value of a symbol. If "get_value" is true, symbol value is
2653 * returned in buf otherwise symbol value is set based on value in buf.
2655 int kexec_purgatory_get_set_symbol(struct kimage
*image
, const char *name
,
2656 void *buf
, unsigned int size
, bool get_value
)
2660 struct purgatory_info
*pi
= &image
->purgatory_info
;
2663 sym
= kexec_purgatory_find_symbol(pi
, name
);
2667 if (sym
->st_size
!= size
) {
2668 pr_err("symbol %s size mismatch: expected %lu actual %u\n",
2669 name
, (unsigned long)sym
->st_size
, size
);
2673 sechdrs
= pi
->sechdrs
;
2675 if (sechdrs
[sym
->st_shndx
].sh_type
== SHT_NOBITS
) {
2676 pr_err("symbol %s is in a bss section. Cannot %s\n", name
,
2677 get_value
? "get" : "set");
2681 sym_buf
= (unsigned char *)sechdrs
[sym
->st_shndx
].sh_offset
+
2685 memcpy((void *)buf
, sym_buf
, size
);
2687 memcpy((void *)sym_buf
, buf
, size
);
2691 #endif /* CONFIG_KEXEC_FILE */
2694 * Move into place and start executing a preloaded standalone
2695 * executable. If nothing was preloaded return an error.
2697 int kernel_kexec(void)
2701 if (!mutex_trylock(&kexec_mutex
))
2708 #ifdef CONFIG_KEXEC_JUMP
2709 if (kexec_image
->preserve_context
) {
2710 lock_system_sleep();
2711 pm_prepare_console();
2712 error
= freeze_processes();
2715 goto Restore_console
;
2718 error
= dpm_suspend_start(PMSG_FREEZE
);
2720 goto Resume_console
;
2721 /* At this point, dpm_suspend_start() has been called,
2722 * but *not* dpm_suspend_end(). We *must* call
2723 * dpm_suspend_end() now. Otherwise, drivers for
2724 * some devices (e.g. interrupt controllers) become
2725 * desynchronized with the actual state of the
2726 * hardware at resume time, and evil weirdness ensues.
2728 error
= dpm_suspend_end(PMSG_FREEZE
);
2730 goto Resume_devices
;
2731 error
= disable_nonboot_cpus();
2734 local_irq_disable();
2735 error
= syscore_suspend();
2741 kexec_in_progress
= true;
2742 kernel_restart_prepare(NULL
);
2743 migrate_to_reboot_cpu();
2746 * migrate_to_reboot_cpu() disables CPU hotplug assuming that
2747 * no further code needs to use CPU hotplug (which is true in
2748 * the reboot case). However, the kexec path depends on using
2749 * CPU hotplug again; so re-enable it here.
2751 cpu_hotplug_enable();
2752 pr_emerg("Starting new kernel\n");
2756 machine_kexec(kexec_image
);
2758 #ifdef CONFIG_KEXEC_JUMP
2759 if (kexec_image
->preserve_context
) {
2764 enable_nonboot_cpus();
2765 dpm_resume_start(PMSG_RESTORE
);
2767 dpm_resume_end(PMSG_RESTORE
);
2772 pm_restore_console();
2773 unlock_system_sleep();
2778 mutex_unlock(&kexec_mutex
);