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 #include <linux/capability.h>
11 #include <linux/file.h>
12 #include <linux/slab.h>
14 #include <linux/kexec.h>
15 #include <linux/spinlock.h>
16 #include <linux/list.h>
17 #include <linux/highmem.h>
18 #include <linux/syscalls.h>
19 #include <linux/reboot.h>
20 #include <linux/ioport.h>
21 #include <linux/hardirq.h>
22 #include <linux/elf.h>
23 #include <linux/elfcore.h>
24 #include <linux/utsrelease.h>
25 #include <linux/utsname.h>
26 #include <linux/numa.h>
27 #include <linux/suspend.h>
28 #include <linux/device.h>
31 #include <asm/uaccess.h>
33 #include <asm/system.h>
34 #include <asm/sections.h>
36 /* Per cpu memory for storing cpu states in case of system crash. */
37 note_buf_t* crash_notes;
39 /* vmcoreinfo stuff */
40 unsigned char vmcoreinfo_data[VMCOREINFO_BYTES];
41 u32 vmcoreinfo_note[VMCOREINFO_NOTE_SIZE/4];
42 size_t vmcoreinfo_size;
43 size_t vmcoreinfo_max_size = sizeof(vmcoreinfo_data);
45 /* Location of the reserved area for the crash kernel */
46 struct resource crashk_res = {
47 .name = "Crash kernel",
50 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
53 int kexec_should_crash(struct task_struct *p)
55 if (in_interrupt() || !p->pid || is_global_init(p) || panic_on_oops)
61 * When kexec transitions to the new kernel there is a one-to-one
62 * mapping between physical and virtual addresses. On processors
63 * where you can disable the MMU this is trivial, and easy. For
64 * others it is still a simple predictable page table to setup.
66 * In that environment kexec copies the new kernel to its final
67 * resting place. This means I can only support memory whose
68 * physical address can fit in an unsigned long. In particular
69 * addresses where (pfn << PAGE_SHIFT) > ULONG_MAX cannot be handled.
70 * If the assembly stub has more restrictive requirements
71 * KEXEC_SOURCE_MEMORY_LIMIT and KEXEC_DEST_MEMORY_LIMIT can be
72 * defined more restrictively in <asm/kexec.h>.
74 * The code for the transition from the current kernel to the
75 * the new kernel is placed in the control_code_buffer, whose size
76 * is given by KEXEC_CONTROL_CODE_SIZE. In the best case only a single
77 * page of memory is necessary, but some architectures require more.
78 * Because this memory must be identity mapped in the transition from
79 * virtual to physical addresses it must live in the range
80 * 0 - TASK_SIZE, as only the user space mappings are arbitrarily
83 * The assembly stub in the control code buffer is passed a linked list
84 * of descriptor pages detailing the source pages of the new kernel,
85 * and the destination addresses of those source pages. As this data
86 * structure is not used in the context of the current OS, it must
89 * The code has been made to work with highmem pages and will use a
90 * destination page in its final resting place (if it happens
91 * to allocate it). The end product of this is that most of the
92 * physical address space, and most of RAM can be used.
94 * Future directions include:
95 * - allocating a page table with the control code buffer identity
96 * mapped, to simplify machine_kexec and make kexec_on_panic more
101 * KIMAGE_NO_DEST is an impossible destination address..., for
102 * allocating pages whose destination address we do not care about.
104 #define KIMAGE_NO_DEST (-1UL)
106 static int kimage_is_destination_range(struct kimage *image,
107 unsigned long start, unsigned long end);
108 static struct page *kimage_alloc_page(struct kimage *image,
112 static int do_kimage_alloc(struct kimage **rimage, unsigned long entry,
113 unsigned long nr_segments,
114 struct kexec_segment __user *segments)
116 size_t segment_bytes;
117 struct kimage *image;
121 /* Allocate a controlling structure */
123 image = kzalloc(sizeof(*image), GFP_KERNEL);
128 image->entry = &image->head;
129 image->last_entry = &image->head;
130 image->control_page = ~0; /* By default this does not apply */
131 image->start = entry;
132 image->type = KEXEC_TYPE_DEFAULT;
134 /* Initialize the list of control pages */
135 INIT_LIST_HEAD(&image->control_pages);
137 /* Initialize the list of destination pages */
138 INIT_LIST_HEAD(&image->dest_pages);
140 /* Initialize the list of unuseable pages */
141 INIT_LIST_HEAD(&image->unuseable_pages);
143 /* Read in the segments */
144 image->nr_segments = nr_segments;
145 segment_bytes = nr_segments * sizeof(*segments);
146 result = copy_from_user(image->segment, segments, segment_bytes);
151 * Verify we have good destination addresses. The caller is
152 * responsible for making certain we don't attempt to load
153 * the new image into invalid or reserved areas of RAM. This
154 * just verifies it is an address we can use.
156 * Since the kernel does everything in page size chunks ensure
157 * the destination addreses are page aligned. Too many
158 * special cases crop of when we don't do this. The most
159 * insidious is getting overlapping destination addresses
160 * simply because addresses are changed to page size
163 result = -EADDRNOTAVAIL;
164 for (i = 0; i < nr_segments; i++) {
165 unsigned long mstart, mend;
167 mstart = image->segment[i].mem;
168 mend = mstart + image->segment[i].memsz;
169 if ((mstart & ~PAGE_MASK) || (mend & ~PAGE_MASK))
171 if (mend >= KEXEC_DESTINATION_MEMORY_LIMIT)
175 /* Verify our destination addresses do not overlap.
176 * If we alloed overlapping destination addresses
177 * through very weird things can happen with no
178 * easy explanation as one segment stops on another.
181 for (i = 0; i < nr_segments; i++) {
182 unsigned long mstart, mend;
185 mstart = image->segment[i].mem;
186 mend = mstart + image->segment[i].memsz;
187 for (j = 0; j < i; j++) {
188 unsigned long pstart, pend;
189 pstart = image->segment[j].mem;
190 pend = pstart + image->segment[j].memsz;
191 /* Do the segments overlap ? */
192 if ((mend > pstart) && (mstart < pend))
197 /* Ensure our buffer sizes are strictly less than
198 * our memory sizes. This should always be the case,
199 * and it is easier to check up front than to be surprised
203 for (i = 0; i < nr_segments; i++) {
204 if (image->segment[i].bufsz > image->segment[i].memsz)
219 static int kimage_normal_alloc(struct kimage **rimage, unsigned long entry,
220 unsigned long nr_segments,
221 struct kexec_segment __user *segments)
224 struct kimage *image;
226 /* Allocate and initialize a controlling structure */
228 result = do_kimage_alloc(&image, entry, nr_segments, segments);
235 * Find a location for the control code buffer, and add it
236 * the vector of segments so that it's pages will also be
237 * counted as destination pages.
240 image->control_code_page = kimage_alloc_control_pages(image,
241 get_order(KEXEC_CONTROL_CODE_SIZE));
242 if (!image->control_code_page) {
243 printk(KERN_ERR "Could not allocate control_code_buffer\n");
247 image->swap_page = kimage_alloc_control_pages(image, 0);
248 if (!image->swap_page) {
249 printk(KERN_ERR "Could not allocate swap buffer\n");
263 static int kimage_crash_alloc(struct kimage **rimage, unsigned long entry,
264 unsigned long nr_segments,
265 struct kexec_segment __user *segments)
268 struct kimage *image;
272 /* Verify we have a valid entry point */
273 if ((entry < crashk_res.start) || (entry > crashk_res.end)) {
274 result = -EADDRNOTAVAIL;
278 /* Allocate and initialize a controlling structure */
279 result = do_kimage_alloc(&image, entry, nr_segments, segments);
283 /* Enable the special crash kernel control page
286 image->control_page = crashk_res.start;
287 image->type = KEXEC_TYPE_CRASH;
290 * Verify we have good destination addresses. Normally
291 * the caller is responsible for making certain we don't
292 * attempt to load the new image into invalid or reserved
293 * areas of RAM. But crash kernels are preloaded into a
294 * reserved area of ram. We must ensure the addresses
295 * are in the reserved area otherwise preloading the
296 * kernel could corrupt things.
298 result = -EADDRNOTAVAIL;
299 for (i = 0; i < nr_segments; i++) {
300 unsigned long mstart, mend;
302 mstart = image->segment[i].mem;
303 mend = mstart + image->segment[i].memsz - 1;
304 /* Ensure we are within the crash kernel limits */
305 if ((mstart < crashk_res.start) || (mend > crashk_res.end))
310 * Find a location for the control code buffer, and add
311 * the vector of segments so that it's pages will also be
312 * counted as destination pages.
315 image->control_code_page = kimage_alloc_control_pages(image,
316 get_order(KEXEC_CONTROL_CODE_SIZE));
317 if (!image->control_code_page) {
318 printk(KERN_ERR "Could not allocate control_code_buffer\n");
332 static int kimage_is_destination_range(struct kimage *image,
338 for (i = 0; i < image->nr_segments; i++) {
339 unsigned long mstart, mend;
341 mstart = image->segment[i].mem;
342 mend = mstart + image->segment[i].memsz;
343 if ((end > mstart) && (start < mend))
350 static struct page *kimage_alloc_pages(gfp_t gfp_mask, unsigned int order)
354 pages = alloc_pages(gfp_mask, order);
356 unsigned int count, i;
357 pages->mapping = NULL;
358 set_page_private(pages, order);
360 for (i = 0; i < count; i++)
361 SetPageReserved(pages + i);
367 static void kimage_free_pages(struct page *page)
369 unsigned int order, count, i;
371 order = page_private(page);
373 for (i = 0; i < count; i++)
374 ClearPageReserved(page + i);
375 __free_pages(page, order);
378 static void kimage_free_page_list(struct list_head *list)
380 struct list_head *pos, *next;
382 list_for_each_safe(pos, next, list) {
385 page = list_entry(pos, struct page, lru);
386 list_del(&page->lru);
387 kimage_free_pages(page);
391 static struct page *kimage_alloc_normal_control_pages(struct kimage *image,
394 /* Control pages are special, they are the intermediaries
395 * that are needed while we copy the rest of the pages
396 * to their final resting place. As such they must
397 * not conflict with either the destination addresses
398 * or memory the kernel is already using.
400 * The only case where we really need more than one of
401 * these are for architectures where we cannot disable
402 * the MMU and must instead generate an identity mapped
403 * page table for all of the memory.
405 * At worst this runs in O(N) of the image size.
407 struct list_head extra_pages;
412 INIT_LIST_HEAD(&extra_pages);
414 /* Loop while I can allocate a page and the page allocated
415 * is a destination page.
418 unsigned long pfn, epfn, addr, eaddr;
420 pages = kimage_alloc_pages(GFP_KERNEL, order);
423 pfn = page_to_pfn(pages);
425 addr = pfn << PAGE_SHIFT;
426 eaddr = epfn << PAGE_SHIFT;
427 if ((epfn >= (KEXEC_CONTROL_MEMORY_LIMIT >> PAGE_SHIFT)) ||
428 kimage_is_destination_range(image, addr, eaddr)) {
429 list_add(&pages->lru, &extra_pages);
435 /* Remember the allocated page... */
436 list_add(&pages->lru, &image->control_pages);
438 /* Because the page is already in it's destination
439 * location we will never allocate another page at
440 * that address. Therefore kimage_alloc_pages
441 * will not return it (again) and we don't need
442 * to give it an entry in image->segment[].
445 /* Deal with the destination pages I have inadvertently allocated.
447 * Ideally I would convert multi-page allocations into single
448 * page allocations, and add everyting to image->dest_pages.
450 * For now it is simpler to just free the pages.
452 kimage_free_page_list(&extra_pages);
457 static struct page *kimage_alloc_crash_control_pages(struct kimage *image,
460 /* Control pages are special, they are the intermediaries
461 * that are needed while we copy the rest of the pages
462 * to their final resting place. As such they must
463 * not conflict with either the destination addresses
464 * or memory the kernel is already using.
466 * Control pages are also the only pags we must allocate
467 * when loading a crash kernel. All of the other pages
468 * are specified by the segments and we just memcpy
469 * into them directly.
471 * The only case where we really need more than one of
472 * these are for architectures where we cannot disable
473 * the MMU and must instead generate an identity mapped
474 * page table for all of the memory.
476 * Given the low demand this implements a very simple
477 * allocator that finds the first hole of the appropriate
478 * size in the reserved memory region, and allocates all
479 * of the memory up to and including the hole.
481 unsigned long hole_start, hole_end, size;
485 size = (1 << order) << PAGE_SHIFT;
486 hole_start = (image->control_page + (size - 1)) & ~(size - 1);
487 hole_end = hole_start + size - 1;
488 while (hole_end <= crashk_res.end) {
491 if (hole_end > KEXEC_CONTROL_MEMORY_LIMIT)
493 if (hole_end > crashk_res.end)
495 /* See if I overlap any of the segments */
496 for (i = 0; i < image->nr_segments; i++) {
497 unsigned long mstart, mend;
499 mstart = image->segment[i].mem;
500 mend = mstart + image->segment[i].memsz - 1;
501 if ((hole_end >= mstart) && (hole_start <= mend)) {
502 /* Advance the hole to the end of the segment */
503 hole_start = (mend + (size - 1)) & ~(size - 1);
504 hole_end = hole_start + size - 1;
508 /* If I don't overlap any segments I have found my hole! */
509 if (i == image->nr_segments) {
510 pages = pfn_to_page(hole_start >> PAGE_SHIFT);
515 image->control_page = hole_end;
521 struct page *kimage_alloc_control_pages(struct kimage *image,
524 struct page *pages = NULL;
526 switch (image->type) {
527 case KEXEC_TYPE_DEFAULT:
528 pages = kimage_alloc_normal_control_pages(image, order);
530 case KEXEC_TYPE_CRASH:
531 pages = kimage_alloc_crash_control_pages(image, order);
538 static int kimage_add_entry(struct kimage *image, kimage_entry_t entry)
540 if (*image->entry != 0)
543 if (image->entry == image->last_entry) {
544 kimage_entry_t *ind_page;
547 page = kimage_alloc_page(image, GFP_KERNEL, KIMAGE_NO_DEST);
551 ind_page = page_address(page);
552 *image->entry = virt_to_phys(ind_page) | IND_INDIRECTION;
553 image->entry = ind_page;
554 image->last_entry = ind_page +
555 ((PAGE_SIZE/sizeof(kimage_entry_t)) - 1);
557 *image->entry = entry;
564 static int kimage_set_destination(struct kimage *image,
565 unsigned long destination)
569 destination &= PAGE_MASK;
570 result = kimage_add_entry(image, destination | IND_DESTINATION);
572 image->destination = destination;
578 static int kimage_add_page(struct kimage *image, unsigned long page)
583 result = kimage_add_entry(image, page | IND_SOURCE);
585 image->destination += PAGE_SIZE;
591 static void kimage_free_extra_pages(struct kimage *image)
593 /* Walk through and free any extra destination pages I may have */
594 kimage_free_page_list(&image->dest_pages);
596 /* Walk through and free any unuseable pages I have cached */
597 kimage_free_page_list(&image->unuseable_pages);
600 static void kimage_terminate(struct kimage *image)
602 if (*image->entry != 0)
605 *image->entry = IND_DONE;
608 #define for_each_kimage_entry(image, ptr, entry) \
609 for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE); \
610 ptr = (entry & IND_INDIRECTION)? \
611 phys_to_virt((entry & PAGE_MASK)): ptr +1)
613 static void kimage_free_entry(kimage_entry_t entry)
617 page = pfn_to_page(entry >> PAGE_SHIFT);
618 kimage_free_pages(page);
621 static void kimage_free(struct kimage *image)
623 kimage_entry_t *ptr, entry;
624 kimage_entry_t ind = 0;
629 kimage_free_extra_pages(image);
630 for_each_kimage_entry(image, ptr, entry) {
631 if (entry & IND_INDIRECTION) {
632 /* Free the previous indirection page */
633 if (ind & IND_INDIRECTION)
634 kimage_free_entry(ind);
635 /* Save this indirection page until we are
640 else if (entry & IND_SOURCE)
641 kimage_free_entry(entry);
643 /* Free the final indirection page */
644 if (ind & IND_INDIRECTION)
645 kimage_free_entry(ind);
647 /* Handle any machine specific cleanup */
648 machine_kexec_cleanup(image);
650 /* Free the kexec control pages... */
651 kimage_free_page_list(&image->control_pages);
655 static kimage_entry_t *kimage_dst_used(struct kimage *image,
658 kimage_entry_t *ptr, entry;
659 unsigned long destination = 0;
661 for_each_kimage_entry(image, ptr, entry) {
662 if (entry & IND_DESTINATION)
663 destination = entry & PAGE_MASK;
664 else if (entry & IND_SOURCE) {
665 if (page == destination)
667 destination += PAGE_SIZE;
674 static struct page *kimage_alloc_page(struct kimage *image,
676 unsigned long destination)
679 * Here we implement safeguards to ensure that a source page
680 * is not copied to its destination page before the data on
681 * the destination page is no longer useful.
683 * To do this we maintain the invariant that a source page is
684 * either its own destination page, or it is not a
685 * destination page at all.
687 * That is slightly stronger than required, but the proof
688 * that no problems will not occur is trivial, and the
689 * implementation is simply to verify.
691 * When allocating all pages normally this algorithm will run
692 * in O(N) time, but in the worst case it will run in O(N^2)
693 * time. If the runtime is a problem the data structures can
700 * Walk through the list of destination pages, and see if I
703 list_for_each_entry(page, &image->dest_pages, lru) {
704 addr = page_to_pfn(page) << PAGE_SHIFT;
705 if (addr == destination) {
706 list_del(&page->lru);
714 /* Allocate a page, if we run out of memory give up */
715 page = kimage_alloc_pages(gfp_mask, 0);
718 /* If the page cannot be used file it away */
719 if (page_to_pfn(page) >
720 (KEXEC_SOURCE_MEMORY_LIMIT >> PAGE_SHIFT)) {
721 list_add(&page->lru, &image->unuseable_pages);
724 addr = page_to_pfn(page) << PAGE_SHIFT;
726 /* If it is the destination page we want use it */
727 if (addr == destination)
730 /* If the page is not a destination page use it */
731 if (!kimage_is_destination_range(image, addr,
736 * I know that the page is someones destination page.
737 * See if there is already a source page for this
738 * destination page. And if so swap the source pages.
740 old = kimage_dst_used(image, addr);
743 unsigned long old_addr;
744 struct page *old_page;
746 old_addr = *old & PAGE_MASK;
747 old_page = pfn_to_page(old_addr >> PAGE_SHIFT);
748 copy_highpage(page, old_page);
749 *old = addr | (*old & ~PAGE_MASK);
751 /* The old page I have found cannot be a
752 * destination page, so return it.
759 /* Place the page on the destination list I
762 list_add(&page->lru, &image->dest_pages);
769 static int kimage_load_normal_segment(struct kimage *image,
770 struct kexec_segment *segment)
773 unsigned long ubytes, mbytes;
775 unsigned char __user *buf;
779 ubytes = segment->bufsz;
780 mbytes = segment->memsz;
781 maddr = segment->mem;
783 result = kimage_set_destination(image, maddr);
790 size_t uchunk, mchunk;
792 page = kimage_alloc_page(image, GFP_HIGHUSER, maddr);
797 result = kimage_add_page(image, page_to_pfn(page)
803 /* Start with a clear page */
804 memset(ptr, 0, PAGE_SIZE);
805 ptr += maddr & ~PAGE_MASK;
806 mchunk = PAGE_SIZE - (maddr & ~PAGE_MASK);
814 result = copy_from_user(ptr, buf, uchunk);
817 result = (result < 0) ? result : -EIO;
829 static int kimage_load_crash_segment(struct kimage *image,
830 struct kexec_segment *segment)
832 /* For crash dumps kernels we simply copy the data from
833 * user space to it's destination.
834 * We do things a page at a time for the sake of kmap.
837 unsigned long ubytes, mbytes;
839 unsigned char __user *buf;
843 ubytes = segment->bufsz;
844 mbytes = segment->memsz;
845 maddr = segment->mem;
849 size_t uchunk, mchunk;
851 page = pfn_to_page(maddr >> PAGE_SHIFT);
857 ptr += maddr & ~PAGE_MASK;
858 mchunk = PAGE_SIZE - (maddr & ~PAGE_MASK);
863 if (uchunk > ubytes) {
865 /* Zero the trailing part of the page */
866 memset(ptr + uchunk, 0, mchunk - uchunk);
868 result = copy_from_user(ptr, buf, uchunk);
869 kexec_flush_icache_page(page);
872 result = (result < 0) ? result : -EIO;
884 static int kimage_load_segment(struct kimage *image,
885 struct kexec_segment *segment)
887 int result = -ENOMEM;
889 switch (image->type) {
890 case KEXEC_TYPE_DEFAULT:
891 result = kimage_load_normal_segment(image, segment);
893 case KEXEC_TYPE_CRASH:
894 result = kimage_load_crash_segment(image, segment);
902 * Exec Kernel system call: for obvious reasons only root may call it.
904 * This call breaks up into three pieces.
905 * - A generic part which loads the new kernel from the current
906 * address space, and very carefully places the data in the
909 * - A generic part that interacts with the kernel and tells all of
910 * the devices to shut down. Preventing on-going dmas, and placing
911 * the devices in a consistent state so a later kernel can
914 * - A machine specific part that includes the syscall number
915 * and the copies the image to it's final destination. And
916 * jumps into the image at entry.
918 * kexec does not sync, or unmount filesystems so if you need
919 * that to happen you need to do that yourself.
921 struct kimage *kexec_image;
922 struct kimage *kexec_crash_image;
924 * A home grown binary mutex.
925 * Nothing can wait so this mutex is safe to use
926 * in interrupt context :)
928 static int kexec_lock;
930 asmlinkage long sys_kexec_load(unsigned long entry, unsigned long nr_segments,
931 struct kexec_segment __user *segments,
934 struct kimage **dest_image, *image;
938 /* We only trust the superuser with rebooting the system. */
939 if (!capable(CAP_SYS_BOOT))
943 * Verify we have a legal set of flags
944 * This leaves us room for future extensions.
946 if ((flags & KEXEC_FLAGS) != (flags & ~KEXEC_ARCH_MASK))
949 /* Verify we are on the appropriate architecture */
950 if (((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH) &&
951 ((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH_DEFAULT))
954 /* Put an artificial cap on the number
955 * of segments passed to kexec_load.
957 if (nr_segments > KEXEC_SEGMENT_MAX)
963 /* Because we write directly to the reserved memory
964 * region when loading crash kernels we need a mutex here to
965 * prevent multiple crash kernels from attempting to load
966 * simultaneously, and to prevent a crash kernel from loading
967 * over the top of a in use crash kernel.
969 * KISS: always take the mutex.
971 locked = xchg(&kexec_lock, 1);
975 dest_image = &kexec_image;
976 if (flags & KEXEC_ON_CRASH)
977 dest_image = &kexec_crash_image;
978 if (nr_segments > 0) {
981 /* Loading another kernel to reboot into */
982 if ((flags & KEXEC_ON_CRASH) == 0)
983 result = kimage_normal_alloc(&image, entry,
984 nr_segments, segments);
985 /* Loading another kernel to switch to if this one crashes */
986 else if (flags & KEXEC_ON_CRASH) {
987 /* Free any current crash dump kernel before
990 kimage_free(xchg(&kexec_crash_image, NULL));
991 result = kimage_crash_alloc(&image, entry,
992 nr_segments, segments);
997 if (flags & KEXEC_PRESERVE_CONTEXT)
998 image->preserve_context = 1;
999 result = machine_kexec_prepare(image);
1003 for (i = 0; i < nr_segments; i++) {
1004 result = kimage_load_segment(image, &image->segment[i]);
1008 kimage_terminate(image);
1010 /* Install the new kernel, and Uninstall the old */
1011 image = xchg(dest_image, image);
1014 locked = xchg(&kexec_lock, 0); /* Release the mutex */
1021 #ifdef CONFIG_COMPAT
1022 asmlinkage long compat_sys_kexec_load(unsigned long entry,
1023 unsigned long nr_segments,
1024 struct compat_kexec_segment __user *segments,
1025 unsigned long flags)
1027 struct compat_kexec_segment in;
1028 struct kexec_segment out, __user *ksegments;
1029 unsigned long i, result;
1031 /* Don't allow clients that don't understand the native
1032 * architecture to do anything.
1034 if ((flags & KEXEC_ARCH_MASK) == KEXEC_ARCH_DEFAULT)
1037 if (nr_segments > KEXEC_SEGMENT_MAX)
1040 ksegments = compat_alloc_user_space(nr_segments * sizeof(out));
1041 for (i=0; i < nr_segments; i++) {
1042 result = copy_from_user(&in, &segments[i], sizeof(in));
1046 out.buf = compat_ptr(in.buf);
1047 out.bufsz = in.bufsz;
1049 out.memsz = in.memsz;
1051 result = copy_to_user(&ksegments[i], &out, sizeof(out));
1056 return sys_kexec_load(entry, nr_segments, ksegments, flags);
1060 void crash_kexec(struct pt_regs *regs)
1065 /* Take the kexec_lock here to prevent sys_kexec_load
1066 * running on one cpu from replacing the crash kernel
1067 * we are using after a panic on a different cpu.
1069 * If the crash kernel was not located in a fixed area
1070 * of memory the xchg(&kexec_crash_image) would be
1071 * sufficient. But since I reuse the memory...
1073 locked = xchg(&kexec_lock, 1);
1075 if (kexec_crash_image) {
1076 struct pt_regs fixed_regs;
1077 crash_setup_regs(&fixed_regs, regs);
1078 crash_save_vmcoreinfo();
1079 machine_crash_shutdown(&fixed_regs);
1080 machine_kexec(kexec_crash_image);
1082 locked = xchg(&kexec_lock, 0);
1087 static u32 *append_elf_note(u32 *buf, char *name, unsigned type, void *data,
1090 struct elf_note note;
1092 note.n_namesz = strlen(name) + 1;
1093 note.n_descsz = data_len;
1095 memcpy(buf, ¬e, sizeof(note));
1096 buf += (sizeof(note) + 3)/4;
1097 memcpy(buf, name, note.n_namesz);
1098 buf += (note.n_namesz + 3)/4;
1099 memcpy(buf, data, note.n_descsz);
1100 buf += (note.n_descsz + 3)/4;
1105 static void final_note(u32 *buf)
1107 struct elf_note note;
1112 memcpy(buf, ¬e, sizeof(note));
1115 void crash_save_cpu(struct pt_regs *regs, int cpu)
1117 struct elf_prstatus prstatus;
1120 if ((cpu < 0) || (cpu >= NR_CPUS))
1123 /* Using ELF notes here is opportunistic.
1124 * I need a well defined structure format
1125 * for the data I pass, and I need tags
1126 * on the data to indicate what information I have
1127 * squirrelled away. ELF notes happen to provide
1128 * all of that, so there is no need to invent something new.
1130 buf = (u32*)per_cpu_ptr(crash_notes, cpu);
1133 memset(&prstatus, 0, sizeof(prstatus));
1134 prstatus.pr_pid = current->pid;
1135 elf_core_copy_regs(&prstatus.pr_reg, regs);
1136 buf = append_elf_note(buf, KEXEC_CORE_NOTE_NAME, NT_PRSTATUS,
1137 &prstatus, sizeof(prstatus));
1141 static int __init crash_notes_memory_init(void)
1143 /* Allocate memory for saving cpu registers. */
1144 crash_notes = alloc_percpu(note_buf_t);
1146 printk("Kexec: Memory allocation for saving cpu register"
1147 " states failed\n");
1152 module_init(crash_notes_memory_init)
1156 * parsing the "crashkernel" commandline
1158 * this code is intended to be called from architecture specific code
1163 * This function parses command lines in the format
1165 * crashkernel=ramsize-range:size[,...][@offset]
1167 * The function returns 0 on success and -EINVAL on failure.
1169 static int __init parse_crashkernel_mem(char *cmdline,
1170 unsigned long long system_ram,
1171 unsigned long long *crash_size,
1172 unsigned long long *crash_base)
1174 char *cur = cmdline, *tmp;
1176 /* for each entry of the comma-separated list */
1178 unsigned long long start, end = ULLONG_MAX, size;
1180 /* get the start of the range */
1181 start = memparse(cur, &tmp);
1183 pr_warning("crashkernel: Memory value expected\n");
1188 pr_warning("crashkernel: '-' expected\n");
1193 /* if no ':' is here, than we read the end */
1195 end = memparse(cur, &tmp);
1197 pr_warning("crashkernel: Memory "
1198 "value expected\n");
1203 pr_warning("crashkernel: end <= start\n");
1209 pr_warning("crashkernel: ':' expected\n");
1214 size = memparse(cur, &tmp);
1216 pr_warning("Memory value expected\n");
1220 if (size >= system_ram) {
1221 pr_warning("crashkernel: invalid size\n");
1226 if (system_ram >= start && system_ram < end) {
1230 } while (*cur++ == ',');
1232 if (*crash_size > 0) {
1233 while (*cur != ' ' && *cur != '@')
1237 *crash_base = memparse(cur, &tmp);
1239 pr_warning("Memory value expected "
1250 * That function parses "simple" (old) crashkernel command lines like
1252 * crashkernel=size[@offset]
1254 * It returns 0 on success and -EINVAL on failure.
1256 static int __init parse_crashkernel_simple(char *cmdline,
1257 unsigned long long *crash_size,
1258 unsigned long long *crash_base)
1260 char *cur = cmdline;
1262 *crash_size = memparse(cmdline, &cur);
1263 if (cmdline == cur) {
1264 pr_warning("crashkernel: memory value expected\n");
1269 *crash_base = memparse(cur+1, &cur);
1275 * That function is the entry point for command line parsing and should be
1276 * called from the arch-specific code.
1278 int __init parse_crashkernel(char *cmdline,
1279 unsigned long long system_ram,
1280 unsigned long long *crash_size,
1281 unsigned long long *crash_base)
1283 char *p = cmdline, *ck_cmdline = NULL;
1284 char *first_colon, *first_space;
1286 BUG_ON(!crash_size || !crash_base);
1290 /* find crashkernel and use the last one if there are more */
1291 p = strstr(p, "crashkernel=");
1294 p = strstr(p+1, "crashkernel=");
1300 ck_cmdline += 12; /* strlen("crashkernel=") */
1303 * if the commandline contains a ':', then that's the extended
1304 * syntax -- if not, it must be the classic syntax
1306 first_colon = strchr(ck_cmdline, ':');
1307 first_space = strchr(ck_cmdline, ' ');
1308 if (first_colon && (!first_space || first_colon < first_space))
1309 return parse_crashkernel_mem(ck_cmdline, system_ram,
1310 crash_size, crash_base);
1312 return parse_crashkernel_simple(ck_cmdline, crash_size,
1320 void crash_save_vmcoreinfo(void)
1324 if (!vmcoreinfo_size)
1327 vmcoreinfo_append_str("CRASHTIME=%ld", get_seconds());
1329 buf = (u32 *)vmcoreinfo_note;
1331 buf = append_elf_note(buf, VMCOREINFO_NOTE_NAME, 0, vmcoreinfo_data,
1337 void vmcoreinfo_append_str(const char *fmt, ...)
1343 va_start(args, fmt);
1344 r = vsnprintf(buf, sizeof(buf), fmt, args);
1347 if (r + vmcoreinfo_size > vmcoreinfo_max_size)
1348 r = vmcoreinfo_max_size - vmcoreinfo_size;
1350 memcpy(&vmcoreinfo_data[vmcoreinfo_size], buf, r);
1352 vmcoreinfo_size += r;
1356 * provide an empty default implementation here -- architecture
1357 * code may override this
1359 void __attribute__ ((weak)) arch_crash_save_vmcoreinfo(void)
1362 unsigned long __attribute__ ((weak)) paddr_vmcoreinfo_note(void)
1364 return __pa((unsigned long)(char *)&vmcoreinfo_note);
1367 static int __init crash_save_vmcoreinfo_init(void)
1369 VMCOREINFO_OSRELEASE(init_uts_ns.name.release);
1370 VMCOREINFO_PAGESIZE(PAGE_SIZE);
1372 VMCOREINFO_SYMBOL(init_uts_ns);
1373 VMCOREINFO_SYMBOL(node_online_map);
1374 VMCOREINFO_SYMBOL(swapper_pg_dir);
1375 VMCOREINFO_SYMBOL(_stext);
1377 #ifndef CONFIG_NEED_MULTIPLE_NODES
1378 VMCOREINFO_SYMBOL(mem_map);
1379 VMCOREINFO_SYMBOL(contig_page_data);
1381 #ifdef CONFIG_SPARSEMEM
1382 VMCOREINFO_SYMBOL(mem_section);
1383 VMCOREINFO_LENGTH(mem_section, NR_SECTION_ROOTS);
1384 VMCOREINFO_STRUCT_SIZE(mem_section);
1385 VMCOREINFO_OFFSET(mem_section, section_mem_map);
1387 VMCOREINFO_STRUCT_SIZE(page);
1388 VMCOREINFO_STRUCT_SIZE(pglist_data);
1389 VMCOREINFO_STRUCT_SIZE(zone);
1390 VMCOREINFO_STRUCT_SIZE(free_area);
1391 VMCOREINFO_STRUCT_SIZE(list_head);
1392 VMCOREINFO_SIZE(nodemask_t);
1393 VMCOREINFO_OFFSET(page, flags);
1394 VMCOREINFO_OFFSET(page, _count);
1395 VMCOREINFO_OFFSET(page, mapping);
1396 VMCOREINFO_OFFSET(page, lru);
1397 VMCOREINFO_OFFSET(pglist_data, node_zones);
1398 VMCOREINFO_OFFSET(pglist_data, nr_zones);
1399 #ifdef CONFIG_FLAT_NODE_MEM_MAP
1400 VMCOREINFO_OFFSET(pglist_data, node_mem_map);
1402 VMCOREINFO_OFFSET(pglist_data, node_start_pfn);
1403 VMCOREINFO_OFFSET(pglist_data, node_spanned_pages);
1404 VMCOREINFO_OFFSET(pglist_data, node_id);
1405 VMCOREINFO_OFFSET(zone, free_area);
1406 VMCOREINFO_OFFSET(zone, vm_stat);
1407 VMCOREINFO_OFFSET(zone, spanned_pages);
1408 VMCOREINFO_OFFSET(free_area, free_list);
1409 VMCOREINFO_OFFSET(list_head, next);
1410 VMCOREINFO_OFFSET(list_head, prev);
1411 VMCOREINFO_LENGTH(zone.free_area, MAX_ORDER);
1412 VMCOREINFO_LENGTH(free_area.free_list, MIGRATE_TYPES);
1413 VMCOREINFO_NUMBER(NR_FREE_PAGES);
1414 VMCOREINFO_NUMBER(PG_lru);
1415 VMCOREINFO_NUMBER(PG_private);
1416 VMCOREINFO_NUMBER(PG_swapcache);
1418 arch_crash_save_vmcoreinfo();
1423 module_init(crash_save_vmcoreinfo_init)
1426 * kernel_kexec - reboot the system
1428 * Move into place and start executing a preloaded standalone
1429 * executable. If nothing was preloaded return an error.
1431 int kernel_kexec(void)
1435 if (xchg(&kexec_lock, 1))
1442 if (kexec_image->preserve_context) {
1443 #ifdef CONFIG_KEXEC_JUMP
1444 local_irq_disable();
1445 save_processor_state();
1448 blocking_notifier_call_chain(&reboot_notifier_list,
1450 system_state = SYSTEM_RESTART;
1453 printk(KERN_EMERG "Starting new kernel\n");
1457 machine_kexec(kexec_image);
1459 if (kexec_image->preserve_context) {
1460 #ifdef CONFIG_KEXEC_JUMP
1461 restore_processor_state();
1467 xchg(&kexec_lock, 0);