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/mutex.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>
29 #include <linux/freezer.h>
31 #include <linux/cpu.h>
32 #include <linux/console.h>
33 #include <linux/vmalloc.h>
36 #include <asm/uaccess.h>
38 #include <asm/system.h>
39 #include <asm/sections.h>
41 /* Per cpu memory for storing cpu states in case of system crash. */
42 note_buf_t
* crash_notes
;
44 /* vmcoreinfo stuff */
45 static unsigned char vmcoreinfo_data
[VMCOREINFO_BYTES
];
46 u32 vmcoreinfo_note
[VMCOREINFO_NOTE_SIZE
/4];
47 size_t vmcoreinfo_size
;
48 size_t vmcoreinfo_max_size
= sizeof(vmcoreinfo_data
);
50 /* Location of the reserved area for the crash kernel */
51 struct resource crashk_res
= {
52 .name
= "Crash kernel",
55 .flags
= IORESOURCE_BUSY
| IORESOURCE_MEM
58 int kexec_should_crash(struct task_struct
*p
)
60 if (in_interrupt() || !p
->pid
|| is_global_init(p
) || panic_on_oops
)
66 * When kexec transitions to the new kernel there is a one-to-one
67 * mapping between physical and virtual addresses. On processors
68 * where you can disable the MMU this is trivial, and easy. For
69 * others it is still a simple predictable page table to setup.
71 * In that environment kexec copies the new kernel to its final
72 * resting place. This means I can only support memory whose
73 * physical address can fit in an unsigned long. In particular
74 * addresses where (pfn << PAGE_SHIFT) > ULONG_MAX cannot be handled.
75 * If the assembly stub has more restrictive requirements
76 * KEXEC_SOURCE_MEMORY_LIMIT and KEXEC_DEST_MEMORY_LIMIT can be
77 * defined more restrictively in <asm/kexec.h>.
79 * The code for the transition from the current kernel to the
80 * the new kernel is placed in the control_code_buffer, whose size
81 * is given by KEXEC_CONTROL_PAGE_SIZE. In the best case only a single
82 * page of memory is necessary, but some architectures require more.
83 * Because this memory must be identity mapped in the transition from
84 * virtual to physical addresses it must live in the range
85 * 0 - TASK_SIZE, as only the user space mappings are arbitrarily
88 * The assembly stub in the control code buffer is passed a linked list
89 * of descriptor pages detailing the source pages of the new kernel,
90 * and the destination addresses of those source pages. As this data
91 * structure is not used in the context of the current OS, it must
94 * The code has been made to work with highmem pages and will use a
95 * destination page in its final resting place (if it happens
96 * to allocate it). The end product of this is that most of the
97 * physical address space, and most of RAM can be used.
99 * Future directions include:
100 * - allocating a page table with the control code buffer identity
101 * mapped, to simplify machine_kexec and make kexec_on_panic more
106 * KIMAGE_NO_DEST is an impossible destination address..., for
107 * allocating pages whose destination address we do not care about.
109 #define KIMAGE_NO_DEST (-1UL)
111 static int kimage_is_destination_range(struct kimage
*image
,
112 unsigned long start
, unsigned long end
);
113 static struct page
*kimage_alloc_page(struct kimage
*image
,
117 static int do_kimage_alloc(struct kimage
**rimage
, unsigned long entry
,
118 unsigned long nr_segments
,
119 struct kexec_segment __user
*segments
)
121 size_t segment_bytes
;
122 struct kimage
*image
;
126 /* Allocate a controlling structure */
128 image
= kzalloc(sizeof(*image
), GFP_KERNEL
);
133 image
->entry
= &image
->head
;
134 image
->last_entry
= &image
->head
;
135 image
->control_page
= ~0; /* By default this does not apply */
136 image
->start
= entry
;
137 image
->type
= KEXEC_TYPE_DEFAULT
;
139 /* Initialize the list of control pages */
140 INIT_LIST_HEAD(&image
->control_pages
);
142 /* Initialize the list of destination pages */
143 INIT_LIST_HEAD(&image
->dest_pages
);
145 /* Initialize the list of unuseable pages */
146 INIT_LIST_HEAD(&image
->unuseable_pages
);
148 /* Read in the segments */
149 image
->nr_segments
= nr_segments
;
150 segment_bytes
= nr_segments
* sizeof(*segments
);
151 result
= copy_from_user(image
->segment
, segments
, segment_bytes
);
156 * Verify we have good destination addresses. The caller is
157 * responsible for making certain we don't attempt to load
158 * the new image into invalid or reserved areas of RAM. This
159 * just verifies it is an address we can use.
161 * Since the kernel does everything in page size chunks ensure
162 * the destination addreses are page aligned. Too many
163 * special cases crop of when we don't do this. The most
164 * insidious is getting overlapping destination addresses
165 * simply because addresses are changed to page size
168 result
= -EADDRNOTAVAIL
;
169 for (i
= 0; i
< nr_segments
; i
++) {
170 unsigned long mstart
, mend
;
172 mstart
= image
->segment
[i
].mem
;
173 mend
= mstart
+ image
->segment
[i
].memsz
;
174 if ((mstart
& ~PAGE_MASK
) || (mend
& ~PAGE_MASK
))
176 if (mend
>= KEXEC_DESTINATION_MEMORY_LIMIT
)
180 /* Verify our destination addresses do not overlap.
181 * If we alloed overlapping destination addresses
182 * through very weird things can happen with no
183 * easy explanation as one segment stops on another.
186 for (i
= 0; i
< nr_segments
; i
++) {
187 unsigned long mstart
, mend
;
190 mstart
= image
->segment
[i
].mem
;
191 mend
= mstart
+ image
->segment
[i
].memsz
;
192 for (j
= 0; j
< i
; j
++) {
193 unsigned long pstart
, pend
;
194 pstart
= image
->segment
[j
].mem
;
195 pend
= pstart
+ image
->segment
[j
].memsz
;
196 /* Do the segments overlap ? */
197 if ((mend
> pstart
) && (mstart
< pend
))
202 /* Ensure our buffer sizes are strictly less than
203 * our memory sizes. This should always be the case,
204 * and it is easier to check up front than to be surprised
208 for (i
= 0; i
< nr_segments
; i
++) {
209 if (image
->segment
[i
].bufsz
> image
->segment
[i
].memsz
)
224 static int kimage_normal_alloc(struct kimage
**rimage
, unsigned long entry
,
225 unsigned long nr_segments
,
226 struct kexec_segment __user
*segments
)
229 struct kimage
*image
;
231 /* Allocate and initialize a controlling structure */
233 result
= do_kimage_alloc(&image
, entry
, nr_segments
, segments
);
240 * Find a location for the control code buffer, and add it
241 * the vector of segments so that it's pages will also be
242 * counted as destination pages.
245 image
->control_code_page
= kimage_alloc_control_pages(image
,
246 get_order(KEXEC_CONTROL_PAGE_SIZE
));
247 if (!image
->control_code_page
) {
248 printk(KERN_ERR
"Could not allocate control_code_buffer\n");
252 image
->swap_page
= kimage_alloc_control_pages(image
, 0);
253 if (!image
->swap_page
) {
254 printk(KERN_ERR
"Could not allocate swap buffer\n");
268 static int kimage_crash_alloc(struct kimage
**rimage
, unsigned long entry
,
269 unsigned long nr_segments
,
270 struct kexec_segment __user
*segments
)
273 struct kimage
*image
;
277 /* Verify we have a valid entry point */
278 if ((entry
< crashk_res
.start
) || (entry
> crashk_res
.end
)) {
279 result
= -EADDRNOTAVAIL
;
283 /* Allocate and initialize a controlling structure */
284 result
= do_kimage_alloc(&image
, entry
, nr_segments
, segments
);
288 /* Enable the special crash kernel control page
291 image
->control_page
= crashk_res
.start
;
292 image
->type
= KEXEC_TYPE_CRASH
;
295 * Verify we have good destination addresses. Normally
296 * the caller is responsible for making certain we don't
297 * attempt to load the new image into invalid or reserved
298 * areas of RAM. But crash kernels are preloaded into a
299 * reserved area of ram. We must ensure the addresses
300 * are in the reserved area otherwise preloading the
301 * kernel could corrupt things.
303 result
= -EADDRNOTAVAIL
;
304 for (i
= 0; i
< nr_segments
; i
++) {
305 unsigned long mstart
, mend
;
307 mstart
= image
->segment
[i
].mem
;
308 mend
= mstart
+ image
->segment
[i
].memsz
- 1;
309 /* Ensure we are within the crash kernel limits */
310 if ((mstart
< crashk_res
.start
) || (mend
> crashk_res
.end
))
315 * Find a location for the control code buffer, and add
316 * the vector of segments so that it's pages will also be
317 * counted as destination pages.
320 image
->control_code_page
= kimage_alloc_control_pages(image
,
321 get_order(KEXEC_CONTROL_PAGE_SIZE
));
322 if (!image
->control_code_page
) {
323 printk(KERN_ERR
"Could not allocate control_code_buffer\n");
337 static int kimage_is_destination_range(struct kimage
*image
,
343 for (i
= 0; i
< image
->nr_segments
; i
++) {
344 unsigned long mstart
, mend
;
346 mstart
= image
->segment
[i
].mem
;
347 mend
= mstart
+ image
->segment
[i
].memsz
;
348 if ((end
> mstart
) && (start
< mend
))
355 static struct page
*kimage_alloc_pages(gfp_t gfp_mask
, unsigned int order
)
359 pages
= alloc_pages(gfp_mask
, order
);
361 unsigned int count
, i
;
362 pages
->mapping
= NULL
;
363 set_page_private(pages
, order
);
365 for (i
= 0; i
< count
; i
++)
366 SetPageReserved(pages
+ i
);
372 static void kimage_free_pages(struct page
*page
)
374 unsigned int order
, count
, i
;
376 order
= page_private(page
);
378 for (i
= 0; i
< count
; i
++)
379 ClearPageReserved(page
+ i
);
380 __free_pages(page
, order
);
383 static void kimage_free_page_list(struct list_head
*list
)
385 struct list_head
*pos
, *next
;
387 list_for_each_safe(pos
, next
, list
) {
390 page
= list_entry(pos
, struct page
, lru
);
391 list_del(&page
->lru
);
392 kimage_free_pages(page
);
396 static struct page
*kimage_alloc_normal_control_pages(struct kimage
*image
,
399 /* Control pages are special, they are the intermediaries
400 * that are needed while we copy the rest of the pages
401 * to their final resting place. As such they must
402 * not conflict with either the destination addresses
403 * or memory the kernel is already using.
405 * The only case where we really need more than one of
406 * these are for architectures where we cannot disable
407 * the MMU and must instead generate an identity mapped
408 * page table for all of the memory.
410 * At worst this runs in O(N) of the image size.
412 struct list_head extra_pages
;
417 INIT_LIST_HEAD(&extra_pages
);
419 /* Loop while I can allocate a page and the page allocated
420 * is a destination page.
423 unsigned long pfn
, epfn
, addr
, eaddr
;
425 pages
= kimage_alloc_pages(GFP_KERNEL
, order
);
428 pfn
= page_to_pfn(pages
);
430 addr
= pfn
<< PAGE_SHIFT
;
431 eaddr
= epfn
<< PAGE_SHIFT
;
432 if ((epfn
>= (KEXEC_CONTROL_MEMORY_LIMIT
>> PAGE_SHIFT
)) ||
433 kimage_is_destination_range(image
, addr
, eaddr
)) {
434 list_add(&pages
->lru
, &extra_pages
);
440 /* Remember the allocated page... */
441 list_add(&pages
->lru
, &image
->control_pages
);
443 /* Because the page is already in it's destination
444 * location we will never allocate another page at
445 * that address. Therefore kimage_alloc_pages
446 * will not return it (again) and we don't need
447 * to give it an entry in image->segment[].
450 /* Deal with the destination pages I have inadvertently allocated.
452 * Ideally I would convert multi-page allocations into single
453 * page allocations, and add everyting to image->dest_pages.
455 * For now it is simpler to just free the pages.
457 kimage_free_page_list(&extra_pages
);
462 static struct page
*kimage_alloc_crash_control_pages(struct kimage
*image
,
465 /* Control pages are special, they are the intermediaries
466 * that are needed while we copy the rest of the pages
467 * to their final resting place. As such they must
468 * not conflict with either the destination addresses
469 * or memory the kernel is already using.
471 * Control pages are also the only pags we must allocate
472 * when loading a crash kernel. All of the other pages
473 * are specified by the segments and we just memcpy
474 * into them directly.
476 * The only case where we really need more than one of
477 * these are for architectures where we cannot disable
478 * the MMU and must instead generate an identity mapped
479 * page table for all of the memory.
481 * Given the low demand this implements a very simple
482 * allocator that finds the first hole of the appropriate
483 * size in the reserved memory region, and allocates all
484 * of the memory up to and including the hole.
486 unsigned long hole_start
, hole_end
, size
;
490 size
= (1 << order
) << PAGE_SHIFT
;
491 hole_start
= (image
->control_page
+ (size
- 1)) & ~(size
- 1);
492 hole_end
= hole_start
+ size
- 1;
493 while (hole_end
<= crashk_res
.end
) {
496 if (hole_end
> KEXEC_CONTROL_MEMORY_LIMIT
)
498 if (hole_end
> crashk_res
.end
)
500 /* See if I overlap any of the segments */
501 for (i
= 0; i
< image
->nr_segments
; i
++) {
502 unsigned long mstart
, mend
;
504 mstart
= image
->segment
[i
].mem
;
505 mend
= mstart
+ image
->segment
[i
].memsz
- 1;
506 if ((hole_end
>= mstart
) && (hole_start
<= mend
)) {
507 /* Advance the hole to the end of the segment */
508 hole_start
= (mend
+ (size
- 1)) & ~(size
- 1);
509 hole_end
= hole_start
+ size
- 1;
513 /* If I don't overlap any segments I have found my hole! */
514 if (i
== image
->nr_segments
) {
515 pages
= pfn_to_page(hole_start
>> PAGE_SHIFT
);
520 image
->control_page
= hole_end
;
526 struct page
*kimage_alloc_control_pages(struct kimage
*image
,
529 struct page
*pages
= NULL
;
531 switch (image
->type
) {
532 case KEXEC_TYPE_DEFAULT
:
533 pages
= kimage_alloc_normal_control_pages(image
, order
);
535 case KEXEC_TYPE_CRASH
:
536 pages
= kimage_alloc_crash_control_pages(image
, order
);
543 static int kimage_add_entry(struct kimage
*image
, kimage_entry_t entry
)
545 if (*image
->entry
!= 0)
548 if (image
->entry
== image
->last_entry
) {
549 kimage_entry_t
*ind_page
;
552 page
= kimage_alloc_page(image
, GFP_KERNEL
, KIMAGE_NO_DEST
);
556 ind_page
= page_address(page
);
557 *image
->entry
= virt_to_phys(ind_page
) | IND_INDIRECTION
;
558 image
->entry
= ind_page
;
559 image
->last_entry
= ind_page
+
560 ((PAGE_SIZE
/sizeof(kimage_entry_t
)) - 1);
562 *image
->entry
= entry
;
569 static int kimage_set_destination(struct kimage
*image
,
570 unsigned long destination
)
574 destination
&= PAGE_MASK
;
575 result
= kimage_add_entry(image
, destination
| IND_DESTINATION
);
577 image
->destination
= destination
;
583 static int kimage_add_page(struct kimage
*image
, unsigned long page
)
588 result
= kimage_add_entry(image
, page
| IND_SOURCE
);
590 image
->destination
+= PAGE_SIZE
;
596 static void kimage_free_extra_pages(struct kimage
*image
)
598 /* Walk through and free any extra destination pages I may have */
599 kimage_free_page_list(&image
->dest_pages
);
601 /* Walk through and free any unuseable pages I have cached */
602 kimage_free_page_list(&image
->unuseable_pages
);
605 static void kimage_terminate(struct kimage
*image
)
607 if (*image
->entry
!= 0)
610 *image
->entry
= IND_DONE
;
613 #define for_each_kimage_entry(image, ptr, entry) \
614 for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE); \
615 ptr = (entry & IND_INDIRECTION)? \
616 phys_to_virt((entry & PAGE_MASK)): ptr +1)
618 static void kimage_free_entry(kimage_entry_t entry
)
622 page
= pfn_to_page(entry
>> PAGE_SHIFT
);
623 kimage_free_pages(page
);
626 static void kimage_free(struct kimage
*image
)
628 kimage_entry_t
*ptr
, entry
;
629 kimage_entry_t ind
= 0;
634 kimage_free_extra_pages(image
);
635 for_each_kimage_entry(image
, ptr
, entry
) {
636 if (entry
& IND_INDIRECTION
) {
637 /* Free the previous indirection page */
638 if (ind
& IND_INDIRECTION
)
639 kimage_free_entry(ind
);
640 /* Save this indirection page until we are
645 else if (entry
& IND_SOURCE
)
646 kimage_free_entry(entry
);
648 /* Free the final indirection page */
649 if (ind
& IND_INDIRECTION
)
650 kimage_free_entry(ind
);
652 /* Handle any machine specific cleanup */
653 machine_kexec_cleanup(image
);
655 /* Free the kexec control pages... */
656 kimage_free_page_list(&image
->control_pages
);
660 static kimage_entry_t
*kimage_dst_used(struct kimage
*image
,
663 kimage_entry_t
*ptr
, entry
;
664 unsigned long destination
= 0;
666 for_each_kimage_entry(image
, ptr
, entry
) {
667 if (entry
& IND_DESTINATION
)
668 destination
= entry
& PAGE_MASK
;
669 else if (entry
& IND_SOURCE
) {
670 if (page
== destination
)
672 destination
+= PAGE_SIZE
;
679 static struct page
*kimage_alloc_page(struct kimage
*image
,
681 unsigned long destination
)
684 * Here we implement safeguards to ensure that a source page
685 * is not copied to its destination page before the data on
686 * the destination page is no longer useful.
688 * To do this we maintain the invariant that a source page is
689 * either its own destination page, or it is not a
690 * destination page at all.
692 * That is slightly stronger than required, but the proof
693 * that no problems will not occur is trivial, and the
694 * implementation is simply to verify.
696 * When allocating all pages normally this algorithm will run
697 * in O(N) time, but in the worst case it will run in O(N^2)
698 * time. If the runtime is a problem the data structures can
705 * Walk through the list of destination pages, and see if I
708 list_for_each_entry(page
, &image
->dest_pages
, lru
) {
709 addr
= page_to_pfn(page
) << PAGE_SHIFT
;
710 if (addr
== destination
) {
711 list_del(&page
->lru
);
719 /* Allocate a page, if we run out of memory give up */
720 page
= kimage_alloc_pages(gfp_mask
, 0);
723 /* If the page cannot be used file it away */
724 if (page_to_pfn(page
) >
725 (KEXEC_SOURCE_MEMORY_LIMIT
>> PAGE_SHIFT
)) {
726 list_add(&page
->lru
, &image
->unuseable_pages
);
729 addr
= page_to_pfn(page
) << PAGE_SHIFT
;
731 /* If it is the destination page we want use it */
732 if (addr
== destination
)
735 /* If the page is not a destination page use it */
736 if (!kimage_is_destination_range(image
, addr
,
741 * I know that the page is someones destination page.
742 * See if there is already a source page for this
743 * destination page. And if so swap the source pages.
745 old
= kimage_dst_used(image
, addr
);
748 unsigned long old_addr
;
749 struct page
*old_page
;
751 old_addr
= *old
& PAGE_MASK
;
752 old_page
= pfn_to_page(old_addr
>> PAGE_SHIFT
);
753 copy_highpage(page
, old_page
);
754 *old
= addr
| (*old
& ~PAGE_MASK
);
756 /* The old page I have found cannot be a
757 * destination page, so return it if it's
758 * gfp_flags honor the ones passed in.
760 if (!(gfp_mask
& __GFP_HIGHMEM
) &&
761 PageHighMem(old_page
)) {
762 kimage_free_pages(old_page
);
770 /* Place the page on the destination list I
773 list_add(&page
->lru
, &image
->dest_pages
);
780 static int kimage_load_normal_segment(struct kimage
*image
,
781 struct kexec_segment
*segment
)
784 unsigned long ubytes
, mbytes
;
786 unsigned char __user
*buf
;
790 ubytes
= segment
->bufsz
;
791 mbytes
= segment
->memsz
;
792 maddr
= segment
->mem
;
794 result
= kimage_set_destination(image
, maddr
);
801 size_t uchunk
, mchunk
;
803 page
= kimage_alloc_page(image
, GFP_HIGHUSER
, maddr
);
808 result
= kimage_add_page(image
, page_to_pfn(page
)
814 /* Start with a clear page */
815 memset(ptr
, 0, PAGE_SIZE
);
816 ptr
+= maddr
& ~PAGE_MASK
;
817 mchunk
= PAGE_SIZE
- (maddr
& ~PAGE_MASK
);
825 result
= copy_from_user(ptr
, buf
, uchunk
);
828 result
= (result
< 0) ? result
: -EIO
;
840 static int kimage_load_crash_segment(struct kimage
*image
,
841 struct kexec_segment
*segment
)
843 /* For crash dumps kernels we simply copy the data from
844 * user space to it's destination.
845 * We do things a page at a time for the sake of kmap.
848 unsigned long ubytes
, mbytes
;
850 unsigned char __user
*buf
;
854 ubytes
= segment
->bufsz
;
855 mbytes
= segment
->memsz
;
856 maddr
= segment
->mem
;
860 size_t uchunk
, mchunk
;
862 page
= pfn_to_page(maddr
>> PAGE_SHIFT
);
868 ptr
+= maddr
& ~PAGE_MASK
;
869 mchunk
= PAGE_SIZE
- (maddr
& ~PAGE_MASK
);
874 if (uchunk
> ubytes
) {
876 /* Zero the trailing part of the page */
877 memset(ptr
+ uchunk
, 0, mchunk
- uchunk
);
879 result
= copy_from_user(ptr
, buf
, uchunk
);
880 kexec_flush_icache_page(page
);
883 result
= (result
< 0) ? result
: -EIO
;
895 static int kimage_load_segment(struct kimage
*image
,
896 struct kexec_segment
*segment
)
898 int result
= -ENOMEM
;
900 switch (image
->type
) {
901 case KEXEC_TYPE_DEFAULT
:
902 result
= kimage_load_normal_segment(image
, segment
);
904 case KEXEC_TYPE_CRASH
:
905 result
= kimage_load_crash_segment(image
, segment
);
913 * Exec Kernel system call: for obvious reasons only root may call it.
915 * This call breaks up into three pieces.
916 * - A generic part which loads the new kernel from the current
917 * address space, and very carefully places the data in the
920 * - A generic part that interacts with the kernel and tells all of
921 * the devices to shut down. Preventing on-going dmas, and placing
922 * the devices in a consistent state so a later kernel can
925 * - A machine specific part that includes the syscall number
926 * and the copies the image to it's final destination. And
927 * jumps into the image at entry.
929 * kexec does not sync, or unmount filesystems so if you need
930 * that to happen you need to do that yourself.
932 struct kimage
*kexec_image
;
933 struct kimage
*kexec_crash_image
;
935 static DEFINE_MUTEX(kexec_mutex
);
937 SYSCALL_DEFINE4(kexec_load
, unsigned long, entry
, unsigned long, nr_segments
,
938 struct kexec_segment __user
*, segments
, unsigned long, flags
)
940 struct kimage
**dest_image
, *image
;
943 /* We only trust the superuser with rebooting the system. */
944 if (!capable(CAP_SYS_BOOT
))
948 * Verify we have a legal set of flags
949 * This leaves us room for future extensions.
951 if ((flags
& KEXEC_FLAGS
) != (flags
& ~KEXEC_ARCH_MASK
))
954 /* Verify we are on the appropriate architecture */
955 if (((flags
& KEXEC_ARCH_MASK
) != KEXEC_ARCH
) &&
956 ((flags
& KEXEC_ARCH_MASK
) != KEXEC_ARCH_DEFAULT
))
959 /* Put an artificial cap on the number
960 * of segments passed to kexec_load.
962 if (nr_segments
> KEXEC_SEGMENT_MAX
)
968 /* Because we write directly to the reserved memory
969 * region when loading crash kernels we need a mutex here to
970 * prevent multiple crash kernels from attempting to load
971 * simultaneously, and to prevent a crash kernel from loading
972 * over the top of a in use crash kernel.
974 * KISS: always take the mutex.
976 if (!mutex_trylock(&kexec_mutex
))
979 dest_image
= &kexec_image
;
980 if (flags
& KEXEC_ON_CRASH
)
981 dest_image
= &kexec_crash_image
;
982 if (nr_segments
> 0) {
985 /* Loading another kernel to reboot into */
986 if ((flags
& KEXEC_ON_CRASH
) == 0)
987 result
= kimage_normal_alloc(&image
, entry
,
988 nr_segments
, segments
);
989 /* Loading another kernel to switch to if this one crashes */
990 else if (flags
& KEXEC_ON_CRASH
) {
991 /* Free any current crash dump kernel before
994 kimage_free(xchg(&kexec_crash_image
, NULL
));
995 result
= kimage_crash_alloc(&image
, entry
,
996 nr_segments
, segments
);
1001 if (flags
& KEXEC_PRESERVE_CONTEXT
)
1002 image
->preserve_context
= 1;
1003 result
= machine_kexec_prepare(image
);
1007 for (i
= 0; i
< nr_segments
; i
++) {
1008 result
= kimage_load_segment(image
, &image
->segment
[i
]);
1012 kimage_terminate(image
);
1014 /* Install the new kernel, and Uninstall the old */
1015 image
= xchg(dest_image
, image
);
1018 mutex_unlock(&kexec_mutex
);
1024 #ifdef CONFIG_COMPAT
1025 asmlinkage
long compat_sys_kexec_load(unsigned long entry
,
1026 unsigned long nr_segments
,
1027 struct compat_kexec_segment __user
*segments
,
1028 unsigned long flags
)
1030 struct compat_kexec_segment in
;
1031 struct kexec_segment out
, __user
*ksegments
;
1032 unsigned long i
, result
;
1034 /* Don't allow clients that don't understand the native
1035 * architecture to do anything.
1037 if ((flags
& KEXEC_ARCH_MASK
) == KEXEC_ARCH_DEFAULT
)
1040 if (nr_segments
> KEXEC_SEGMENT_MAX
)
1043 ksegments
= compat_alloc_user_space(nr_segments
* sizeof(out
));
1044 for (i
=0; i
< nr_segments
; i
++) {
1045 result
= copy_from_user(&in
, &segments
[i
], sizeof(in
));
1049 out
.buf
= compat_ptr(in
.buf
);
1050 out
.bufsz
= in
.bufsz
;
1052 out
.memsz
= in
.memsz
;
1054 result
= copy_to_user(&ksegments
[i
], &out
, sizeof(out
));
1059 return sys_kexec_load(entry
, nr_segments
, ksegments
, flags
);
1063 void crash_kexec(struct pt_regs
*regs
)
1065 /* Take the kexec_mutex 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 if (mutex_trylock(&kexec_mutex
)) {
1074 if (kexec_crash_image
) {
1075 struct pt_regs fixed_regs
;
1076 crash_setup_regs(&fixed_regs
, regs
);
1077 crash_save_vmcoreinfo();
1078 machine_crash_shutdown(&fixed_regs
);
1079 machine_kexec(kexec_crash_image
);
1081 mutex_unlock(&kexec_mutex
);
1085 static u32
*append_elf_note(u32
*buf
, char *name
, unsigned type
, void *data
,
1088 struct elf_note note
;
1090 note
.n_namesz
= strlen(name
) + 1;
1091 note
.n_descsz
= data_len
;
1093 memcpy(buf
, ¬e
, sizeof(note
));
1094 buf
+= (sizeof(note
) + 3)/4;
1095 memcpy(buf
, name
, note
.n_namesz
);
1096 buf
+= (note
.n_namesz
+ 3)/4;
1097 memcpy(buf
, data
, note
.n_descsz
);
1098 buf
+= (note
.n_descsz
+ 3)/4;
1103 static void final_note(u32
*buf
)
1105 struct elf_note note
;
1110 memcpy(buf
, ¬e
, sizeof(note
));
1113 void crash_save_cpu(struct pt_regs
*regs
, int cpu
)
1115 struct elf_prstatus prstatus
;
1118 if ((cpu
< 0) || (cpu
>= nr_cpu_ids
))
1121 /* Using ELF notes here is opportunistic.
1122 * I need a well defined structure format
1123 * for the data I pass, and I need tags
1124 * on the data to indicate what information I have
1125 * squirrelled away. ELF notes happen to provide
1126 * all of that, so there is no need to invent something new.
1128 buf
= (u32
*)per_cpu_ptr(crash_notes
, cpu
);
1131 memset(&prstatus
, 0, sizeof(prstatus
));
1132 prstatus
.pr_pid
= current
->pid
;
1133 elf_core_copy_kernel_regs(&prstatus
.pr_reg
, regs
);
1134 buf
= append_elf_note(buf
, KEXEC_CORE_NOTE_NAME
, NT_PRSTATUS
,
1135 &prstatus
, sizeof(prstatus
));
1139 static int __init
crash_notes_memory_init(void)
1141 /* Allocate memory for saving cpu registers. */
1142 crash_notes
= alloc_percpu(note_buf_t
);
1144 printk("Kexec: Memory allocation for saving cpu register"
1145 " states failed\n");
1150 module_init(crash_notes_memory_init
)
1154 * parsing the "crashkernel" commandline
1156 * this code is intended to be called from architecture specific code
1161 * This function parses command lines in the format
1163 * crashkernel=ramsize-range:size[,...][@offset]
1165 * The function returns 0 on success and -EINVAL on failure.
1167 static int __init
parse_crashkernel_mem(char *cmdline
,
1168 unsigned long long system_ram
,
1169 unsigned long long *crash_size
,
1170 unsigned long long *crash_base
)
1172 char *cur
= cmdline
, *tmp
;
1174 /* for each entry of the comma-separated list */
1176 unsigned long long start
, end
= ULLONG_MAX
, size
;
1178 /* get the start of the range */
1179 start
= memparse(cur
, &tmp
);
1181 pr_warning("crashkernel: Memory value expected\n");
1186 pr_warning("crashkernel: '-' expected\n");
1191 /* if no ':' is here, than we read the end */
1193 end
= memparse(cur
, &tmp
);
1195 pr_warning("crashkernel: Memory "
1196 "value expected\n");
1201 pr_warning("crashkernel: end <= start\n");
1207 pr_warning("crashkernel: ':' expected\n");
1212 size
= memparse(cur
, &tmp
);
1214 pr_warning("Memory value expected\n");
1218 if (size
>= system_ram
) {
1219 pr_warning("crashkernel: invalid size\n");
1224 if (system_ram
>= start
&& system_ram
< end
) {
1228 } while (*cur
++ == ',');
1230 if (*crash_size
> 0) {
1231 while (*cur
!= ' ' && *cur
!= '@')
1235 *crash_base
= memparse(cur
, &tmp
);
1237 pr_warning("Memory value expected "
1248 * That function parses "simple" (old) crashkernel command lines like
1250 * crashkernel=size[@offset]
1252 * It returns 0 on success and -EINVAL on failure.
1254 static int __init
parse_crashkernel_simple(char *cmdline
,
1255 unsigned long long *crash_size
,
1256 unsigned long long *crash_base
)
1258 char *cur
= cmdline
;
1260 *crash_size
= memparse(cmdline
, &cur
);
1261 if (cmdline
== cur
) {
1262 pr_warning("crashkernel: memory value expected\n");
1267 *crash_base
= memparse(cur
+1, &cur
);
1273 * That function is the entry point for command line parsing and should be
1274 * called from the arch-specific code.
1276 int __init
parse_crashkernel(char *cmdline
,
1277 unsigned long long system_ram
,
1278 unsigned long long *crash_size
,
1279 unsigned long long *crash_base
)
1281 char *p
= cmdline
, *ck_cmdline
= NULL
;
1282 char *first_colon
, *first_space
;
1284 BUG_ON(!crash_size
|| !crash_base
);
1288 /* find crashkernel and use the last one if there are more */
1289 p
= strstr(p
, "crashkernel=");
1292 p
= strstr(p
+1, "crashkernel=");
1298 ck_cmdline
+= 12; /* strlen("crashkernel=") */
1301 * if the commandline contains a ':', then that's the extended
1302 * syntax -- if not, it must be the classic syntax
1304 first_colon
= strchr(ck_cmdline
, ':');
1305 first_space
= strchr(ck_cmdline
, ' ');
1306 if (first_colon
&& (!first_space
|| first_colon
< first_space
))
1307 return parse_crashkernel_mem(ck_cmdline
, system_ram
,
1308 crash_size
, crash_base
);
1310 return parse_crashkernel_simple(ck_cmdline
, crash_size
,
1318 void crash_save_vmcoreinfo(void)
1322 if (!vmcoreinfo_size
)
1325 vmcoreinfo_append_str("CRASHTIME=%ld", get_seconds());
1327 buf
= (u32
*)vmcoreinfo_note
;
1329 buf
= append_elf_note(buf
, VMCOREINFO_NOTE_NAME
, 0, vmcoreinfo_data
,
1335 void vmcoreinfo_append_str(const char *fmt
, ...)
1341 va_start(args
, fmt
);
1342 r
= vsnprintf(buf
, sizeof(buf
), fmt
, args
);
1345 if (r
+ vmcoreinfo_size
> vmcoreinfo_max_size
)
1346 r
= vmcoreinfo_max_size
- vmcoreinfo_size
;
1348 memcpy(&vmcoreinfo_data
[vmcoreinfo_size
], buf
, r
);
1350 vmcoreinfo_size
+= r
;
1354 * provide an empty default implementation here -- architecture
1355 * code may override this
1357 void __attribute__ ((weak
)) arch_crash_save_vmcoreinfo(void)
1360 unsigned long __attribute__ ((weak
)) paddr_vmcoreinfo_note(void)
1362 return __pa((unsigned long)(char *)&vmcoreinfo_note
);
1365 static int __init
crash_save_vmcoreinfo_init(void)
1367 VMCOREINFO_OSRELEASE(init_uts_ns
.name
.release
);
1368 VMCOREINFO_PAGESIZE(PAGE_SIZE
);
1370 VMCOREINFO_SYMBOL(init_uts_ns
);
1371 VMCOREINFO_SYMBOL(node_online_map
);
1372 VMCOREINFO_SYMBOL(swapper_pg_dir
);
1373 VMCOREINFO_SYMBOL(_stext
);
1374 VMCOREINFO_SYMBOL(vmlist
);
1376 #ifndef CONFIG_NEED_MULTIPLE_NODES
1377 VMCOREINFO_SYMBOL(mem_map
);
1378 VMCOREINFO_SYMBOL(contig_page_data
);
1380 #ifdef CONFIG_SPARSEMEM
1381 VMCOREINFO_SYMBOL(mem_section
);
1382 VMCOREINFO_LENGTH(mem_section
, NR_SECTION_ROOTS
);
1383 VMCOREINFO_STRUCT_SIZE(mem_section
);
1384 VMCOREINFO_OFFSET(mem_section
, section_mem_map
);
1386 VMCOREINFO_STRUCT_SIZE(page
);
1387 VMCOREINFO_STRUCT_SIZE(pglist_data
);
1388 VMCOREINFO_STRUCT_SIZE(zone
);
1389 VMCOREINFO_STRUCT_SIZE(free_area
);
1390 VMCOREINFO_STRUCT_SIZE(list_head
);
1391 VMCOREINFO_SIZE(nodemask_t
);
1392 VMCOREINFO_OFFSET(page
, flags
);
1393 VMCOREINFO_OFFSET(page
, _count
);
1394 VMCOREINFO_OFFSET(page
, mapping
);
1395 VMCOREINFO_OFFSET(page
, lru
);
1396 VMCOREINFO_OFFSET(pglist_data
, node_zones
);
1397 VMCOREINFO_OFFSET(pglist_data
, nr_zones
);
1398 #ifdef CONFIG_FLAT_NODE_MEM_MAP
1399 VMCOREINFO_OFFSET(pglist_data
, node_mem_map
);
1401 VMCOREINFO_OFFSET(pglist_data
, node_start_pfn
);
1402 VMCOREINFO_OFFSET(pglist_data
, node_spanned_pages
);
1403 VMCOREINFO_OFFSET(pglist_data
, node_id
);
1404 VMCOREINFO_OFFSET(zone
, free_area
);
1405 VMCOREINFO_OFFSET(zone
, vm_stat
);
1406 VMCOREINFO_OFFSET(zone
, spanned_pages
);
1407 VMCOREINFO_OFFSET(free_area
, free_list
);
1408 VMCOREINFO_OFFSET(list_head
, next
);
1409 VMCOREINFO_OFFSET(list_head
, prev
);
1410 VMCOREINFO_OFFSET(vm_struct
, addr
);
1411 VMCOREINFO_LENGTH(zone
.free_area
, MAX_ORDER
);
1412 log_buf_kexec_setup();
1413 VMCOREINFO_LENGTH(free_area
.free_list
, MIGRATE_TYPES
);
1414 VMCOREINFO_NUMBER(NR_FREE_PAGES
);
1415 VMCOREINFO_NUMBER(PG_lru
);
1416 VMCOREINFO_NUMBER(PG_private
);
1417 VMCOREINFO_NUMBER(PG_swapcache
);
1419 arch_crash_save_vmcoreinfo();
1424 module_init(crash_save_vmcoreinfo_init
)
1427 * Move into place and start executing a preloaded standalone
1428 * executable. If nothing was preloaded return an error.
1430 int kernel_kexec(void)
1434 if (!mutex_trylock(&kexec_mutex
))
1441 #ifdef CONFIG_KEXEC_JUMP
1442 if (kexec_image
->preserve_context
) {
1443 mutex_lock(&pm_mutex
);
1444 pm_prepare_console();
1445 error
= freeze_processes();
1448 goto Restore_console
;
1451 error
= device_suspend(PMSG_FREEZE
);
1453 goto Resume_console
;
1455 /* At this point, device_suspend() has been called,
1456 * but *not* device_power_down(). We *must*
1457 * device_power_down() now. Otherwise, drivers for
1458 * some devices (e.g. interrupt controllers) become
1459 * desynchronized with the actual state of the
1460 * hardware at resume time, and evil weirdness ensues.
1462 error
= device_power_down(PMSG_FREEZE
);
1464 goto Resume_devices
;
1465 error
= disable_nonboot_cpus();
1468 local_irq_disable();
1469 /* Suspend system devices */
1470 error
= sysdev_suspend(PMSG_FREEZE
);
1476 kernel_restart_prepare(NULL
);
1477 printk(KERN_EMERG
"Starting new kernel\n");
1481 machine_kexec(kexec_image
);
1483 #ifdef CONFIG_KEXEC_JUMP
1484 if (kexec_image
->preserve_context
) {
1489 enable_nonboot_cpus();
1490 device_power_up(PMSG_RESTORE
);
1493 device_resume(PMSG_RESTORE
);
1498 pm_restore_console();
1499 mutex_unlock(&pm_mutex
);
1504 mutex_unlock(&kexec_mutex
);