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>
26 #include <asm/uaccess.h>
28 #include <asm/system.h>
29 #include <asm/semaphore.h>
31 /* Per cpu memory for storing cpu states in case of system crash. */
32 note_buf_t
* crash_notes
;
34 /* Location of the reserved area for the crash kernel */
35 struct resource crashk_res
= {
36 .name
= "Crash kernel",
39 .flags
= IORESOURCE_BUSY
| IORESOURCE_MEM
42 int kexec_should_crash(struct task_struct
*p
)
44 if (in_interrupt() || !p
->pid
|| is_init(p
) || panic_on_oops
)
50 * When kexec transitions to the new kernel there is a one-to-one
51 * mapping between physical and virtual addresses. On processors
52 * where you can disable the MMU this is trivial, and easy. For
53 * others it is still a simple predictable page table to setup.
55 * In that environment kexec copies the new kernel to its final
56 * resting place. This means I can only support memory whose
57 * physical address can fit in an unsigned long. In particular
58 * addresses where (pfn << PAGE_SHIFT) > ULONG_MAX cannot be handled.
59 * If the assembly stub has more restrictive requirements
60 * KEXEC_SOURCE_MEMORY_LIMIT and KEXEC_DEST_MEMORY_LIMIT can be
61 * defined more restrictively in <asm/kexec.h>.
63 * The code for the transition from the current kernel to the
64 * the new kernel is placed in the control_code_buffer, whose size
65 * is given by KEXEC_CONTROL_CODE_SIZE. In the best case only a single
66 * page of memory is necessary, but some architectures require more.
67 * Because this memory must be identity mapped in the transition from
68 * virtual to physical addresses it must live in the range
69 * 0 - TASK_SIZE, as only the user space mappings are arbitrarily
72 * The assembly stub in the control code buffer is passed a linked list
73 * of descriptor pages detailing the source pages of the new kernel,
74 * and the destination addresses of those source pages. As this data
75 * structure is not used in the context of the current OS, it must
78 * The code has been made to work with highmem pages and will use a
79 * destination page in its final resting place (if it happens
80 * to allocate it). The end product of this is that most of the
81 * physical address space, and most of RAM can be used.
83 * Future directions include:
84 * - allocating a page table with the control code buffer identity
85 * mapped, to simplify machine_kexec and make kexec_on_panic more
90 * KIMAGE_NO_DEST is an impossible destination address..., for
91 * allocating pages whose destination address we do not care about.
93 #define KIMAGE_NO_DEST (-1UL)
95 static int kimage_is_destination_range(struct kimage
*image
,
96 unsigned long start
, unsigned long end
);
97 static struct page
*kimage_alloc_page(struct kimage
*image
,
101 static int do_kimage_alloc(struct kimage
**rimage
, unsigned long entry
,
102 unsigned long nr_segments
,
103 struct kexec_segment __user
*segments
)
105 size_t segment_bytes
;
106 struct kimage
*image
;
110 /* Allocate a controlling structure */
112 image
= kzalloc(sizeof(*image
), GFP_KERNEL
);
117 image
->entry
= &image
->head
;
118 image
->last_entry
= &image
->head
;
119 image
->control_page
= ~0; /* By default this does not apply */
120 image
->start
= entry
;
121 image
->type
= KEXEC_TYPE_DEFAULT
;
123 /* Initialize the list of control pages */
124 INIT_LIST_HEAD(&image
->control_pages
);
126 /* Initialize the list of destination pages */
127 INIT_LIST_HEAD(&image
->dest_pages
);
129 /* Initialize the list of unuseable pages */
130 INIT_LIST_HEAD(&image
->unuseable_pages
);
132 /* Read in the segments */
133 image
->nr_segments
= nr_segments
;
134 segment_bytes
= nr_segments
* sizeof(*segments
);
135 result
= copy_from_user(image
->segment
, segments
, segment_bytes
);
140 * Verify we have good destination addresses. The caller is
141 * responsible for making certain we don't attempt to load
142 * the new image into invalid or reserved areas of RAM. This
143 * just verifies it is an address we can use.
145 * Since the kernel does everything in page size chunks ensure
146 * the destination addreses are page aligned. Too many
147 * special cases crop of when we don't do this. The most
148 * insidious is getting overlapping destination addresses
149 * simply because addresses are changed to page size
152 result
= -EADDRNOTAVAIL
;
153 for (i
= 0; i
< nr_segments
; i
++) {
154 unsigned long mstart
, mend
;
156 mstart
= image
->segment
[i
].mem
;
157 mend
= mstart
+ image
->segment
[i
].memsz
;
158 if ((mstart
& ~PAGE_MASK
) || (mend
& ~PAGE_MASK
))
160 if (mend
>= KEXEC_DESTINATION_MEMORY_LIMIT
)
164 /* Verify our destination addresses do not overlap.
165 * If we alloed overlapping destination addresses
166 * through very weird things can happen with no
167 * easy explanation as one segment stops on another.
170 for (i
= 0; i
< nr_segments
; i
++) {
171 unsigned long mstart
, mend
;
174 mstart
= image
->segment
[i
].mem
;
175 mend
= mstart
+ image
->segment
[i
].memsz
;
176 for (j
= 0; j
< i
; j
++) {
177 unsigned long pstart
, pend
;
178 pstart
= image
->segment
[j
].mem
;
179 pend
= pstart
+ image
->segment
[j
].memsz
;
180 /* Do the segments overlap ? */
181 if ((mend
> pstart
) && (mstart
< pend
))
186 /* Ensure our buffer sizes are strictly less than
187 * our memory sizes. This should always be the case,
188 * and it is easier to check up front than to be surprised
192 for (i
= 0; i
< nr_segments
; i
++) {
193 if (image
->segment
[i
].bufsz
> image
->segment
[i
].memsz
)
208 static int kimage_normal_alloc(struct kimage
**rimage
, unsigned long entry
,
209 unsigned long nr_segments
,
210 struct kexec_segment __user
*segments
)
213 struct kimage
*image
;
215 /* Allocate and initialize a controlling structure */
217 result
= do_kimage_alloc(&image
, entry
, nr_segments
, segments
);
224 * Find a location for the control code buffer, and add it
225 * the vector of segments so that it's pages will also be
226 * counted as destination pages.
229 image
->control_code_page
= kimage_alloc_control_pages(image
,
230 get_order(KEXEC_CONTROL_CODE_SIZE
));
231 if (!image
->control_code_page
) {
232 printk(KERN_ERR
"Could not allocate control_code_buffer\n");
246 static int kimage_crash_alloc(struct kimage
**rimage
, unsigned long entry
,
247 unsigned long nr_segments
,
248 struct kexec_segment __user
*segments
)
251 struct kimage
*image
;
255 /* Verify we have a valid entry point */
256 if ((entry
< crashk_res
.start
) || (entry
> crashk_res
.end
)) {
257 result
= -EADDRNOTAVAIL
;
261 /* Allocate and initialize a controlling structure */
262 result
= do_kimage_alloc(&image
, entry
, nr_segments
, segments
);
266 /* Enable the special crash kernel control page
269 image
->control_page
= crashk_res
.start
;
270 image
->type
= KEXEC_TYPE_CRASH
;
273 * Verify we have good destination addresses. Normally
274 * the caller is responsible for making certain we don't
275 * attempt to load the new image into invalid or reserved
276 * areas of RAM. But crash kernels are preloaded into a
277 * reserved area of ram. We must ensure the addresses
278 * are in the reserved area otherwise preloading the
279 * kernel could corrupt things.
281 result
= -EADDRNOTAVAIL
;
282 for (i
= 0; i
< nr_segments
; i
++) {
283 unsigned long mstart
, mend
;
285 mstart
= image
->segment
[i
].mem
;
286 mend
= mstart
+ image
->segment
[i
].memsz
- 1;
287 /* Ensure we are within the crash kernel limits */
288 if ((mstart
< crashk_res
.start
) || (mend
> crashk_res
.end
))
293 * Find a location for the control code buffer, and add
294 * the vector of segments so that it's pages will also be
295 * counted as destination pages.
298 image
->control_code_page
= kimage_alloc_control_pages(image
,
299 get_order(KEXEC_CONTROL_CODE_SIZE
));
300 if (!image
->control_code_page
) {
301 printk(KERN_ERR
"Could not allocate control_code_buffer\n");
315 static int kimage_is_destination_range(struct kimage
*image
,
321 for (i
= 0; i
< image
->nr_segments
; i
++) {
322 unsigned long mstart
, mend
;
324 mstart
= image
->segment
[i
].mem
;
325 mend
= mstart
+ image
->segment
[i
].memsz
;
326 if ((end
> mstart
) && (start
< mend
))
333 static struct page
*kimage_alloc_pages(gfp_t gfp_mask
, unsigned int order
)
337 pages
= alloc_pages(gfp_mask
, order
);
339 unsigned int count
, i
;
340 pages
->mapping
= NULL
;
341 set_page_private(pages
, order
);
343 for (i
= 0; i
< count
; i
++)
344 SetPageReserved(pages
+ i
);
350 static void kimage_free_pages(struct page
*page
)
352 unsigned int order
, count
, i
;
354 order
= page_private(page
);
356 for (i
= 0; i
< count
; i
++)
357 ClearPageReserved(page
+ i
);
358 __free_pages(page
, order
);
361 static void kimage_free_page_list(struct list_head
*list
)
363 struct list_head
*pos
, *next
;
365 list_for_each_safe(pos
, next
, list
) {
368 page
= list_entry(pos
, struct page
, lru
);
369 list_del(&page
->lru
);
370 kimage_free_pages(page
);
374 static struct page
*kimage_alloc_normal_control_pages(struct kimage
*image
,
377 /* Control pages are special, they are the intermediaries
378 * that are needed while we copy the rest of the pages
379 * to their final resting place. As such they must
380 * not conflict with either the destination addresses
381 * or memory the kernel is already using.
383 * The only case where we really need more than one of
384 * these are for architectures where we cannot disable
385 * the MMU and must instead generate an identity mapped
386 * page table for all of the memory.
388 * At worst this runs in O(N) of the image size.
390 struct list_head extra_pages
;
395 INIT_LIST_HEAD(&extra_pages
);
397 /* Loop while I can allocate a page and the page allocated
398 * is a destination page.
401 unsigned long pfn
, epfn
, addr
, eaddr
;
403 pages
= kimage_alloc_pages(GFP_KERNEL
, order
);
406 pfn
= page_to_pfn(pages
);
408 addr
= pfn
<< PAGE_SHIFT
;
409 eaddr
= epfn
<< PAGE_SHIFT
;
410 if ((epfn
>= (KEXEC_CONTROL_MEMORY_LIMIT
>> PAGE_SHIFT
)) ||
411 kimage_is_destination_range(image
, addr
, eaddr
)) {
412 list_add(&pages
->lru
, &extra_pages
);
418 /* Remember the allocated page... */
419 list_add(&pages
->lru
, &image
->control_pages
);
421 /* Because the page is already in it's destination
422 * location we will never allocate another page at
423 * that address. Therefore kimage_alloc_pages
424 * will not return it (again) and we don't need
425 * to give it an entry in image->segment[].
428 /* Deal with the destination pages I have inadvertently allocated.
430 * Ideally I would convert multi-page allocations into single
431 * page allocations, and add everyting to image->dest_pages.
433 * For now it is simpler to just free the pages.
435 kimage_free_page_list(&extra_pages
);
440 static struct page
*kimage_alloc_crash_control_pages(struct kimage
*image
,
443 /* Control pages are special, they are the intermediaries
444 * that are needed while we copy the rest of the pages
445 * to their final resting place. As such they must
446 * not conflict with either the destination addresses
447 * or memory the kernel is already using.
449 * Control pages are also the only pags we must allocate
450 * when loading a crash kernel. All of the other pages
451 * are specified by the segments and we just memcpy
452 * into them directly.
454 * The only case where we really need more than one of
455 * these are for architectures where we cannot disable
456 * the MMU and must instead generate an identity mapped
457 * page table for all of the memory.
459 * Given the low demand this implements a very simple
460 * allocator that finds the first hole of the appropriate
461 * size in the reserved memory region, and allocates all
462 * of the memory up to and including the hole.
464 unsigned long hole_start
, hole_end
, size
;
468 size
= (1 << order
) << PAGE_SHIFT
;
469 hole_start
= (image
->control_page
+ (size
- 1)) & ~(size
- 1);
470 hole_end
= hole_start
+ size
- 1;
471 while (hole_end
<= crashk_res
.end
) {
474 if (hole_end
> KEXEC_CONTROL_MEMORY_LIMIT
)
476 if (hole_end
> crashk_res
.end
)
478 /* See if I overlap any of the segments */
479 for (i
= 0; i
< image
->nr_segments
; i
++) {
480 unsigned long mstart
, mend
;
482 mstart
= image
->segment
[i
].mem
;
483 mend
= mstart
+ image
->segment
[i
].memsz
- 1;
484 if ((hole_end
>= mstart
) && (hole_start
<= mend
)) {
485 /* Advance the hole to the end of the segment */
486 hole_start
= (mend
+ (size
- 1)) & ~(size
- 1);
487 hole_end
= hole_start
+ size
- 1;
491 /* If I don't overlap any segments I have found my hole! */
492 if (i
== image
->nr_segments
) {
493 pages
= pfn_to_page(hole_start
>> PAGE_SHIFT
);
498 image
->control_page
= hole_end
;
504 struct page
*kimage_alloc_control_pages(struct kimage
*image
,
507 struct page
*pages
= NULL
;
509 switch (image
->type
) {
510 case KEXEC_TYPE_DEFAULT
:
511 pages
= kimage_alloc_normal_control_pages(image
, order
);
513 case KEXEC_TYPE_CRASH
:
514 pages
= kimage_alloc_crash_control_pages(image
, order
);
521 static int kimage_add_entry(struct kimage
*image
, kimage_entry_t entry
)
523 if (*image
->entry
!= 0)
526 if (image
->entry
== image
->last_entry
) {
527 kimage_entry_t
*ind_page
;
530 page
= kimage_alloc_page(image
, GFP_KERNEL
, KIMAGE_NO_DEST
);
534 ind_page
= page_address(page
);
535 *image
->entry
= virt_to_phys(ind_page
) | IND_INDIRECTION
;
536 image
->entry
= ind_page
;
537 image
->last_entry
= ind_page
+
538 ((PAGE_SIZE
/sizeof(kimage_entry_t
)) - 1);
540 *image
->entry
= entry
;
547 static int kimage_set_destination(struct kimage
*image
,
548 unsigned long destination
)
552 destination
&= PAGE_MASK
;
553 result
= kimage_add_entry(image
, destination
| IND_DESTINATION
);
555 image
->destination
= destination
;
561 static int kimage_add_page(struct kimage
*image
, unsigned long page
)
566 result
= kimage_add_entry(image
, page
| IND_SOURCE
);
568 image
->destination
+= PAGE_SIZE
;
574 static void kimage_free_extra_pages(struct kimage
*image
)
576 /* Walk through and free any extra destination pages I may have */
577 kimage_free_page_list(&image
->dest_pages
);
579 /* Walk through and free any unuseable pages I have cached */
580 kimage_free_page_list(&image
->unuseable_pages
);
583 static int kimage_terminate(struct kimage
*image
)
585 if (*image
->entry
!= 0)
588 *image
->entry
= IND_DONE
;
593 #define for_each_kimage_entry(image, ptr, entry) \
594 for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE); \
595 ptr = (entry & IND_INDIRECTION)? \
596 phys_to_virt((entry & PAGE_MASK)): ptr +1)
598 static void kimage_free_entry(kimage_entry_t entry
)
602 page
= pfn_to_page(entry
>> PAGE_SHIFT
);
603 kimage_free_pages(page
);
606 static void kimage_free(struct kimage
*image
)
608 kimage_entry_t
*ptr
, entry
;
609 kimage_entry_t ind
= 0;
614 kimage_free_extra_pages(image
);
615 for_each_kimage_entry(image
, ptr
, entry
) {
616 if (entry
& IND_INDIRECTION
) {
617 /* Free the previous indirection page */
618 if (ind
& IND_INDIRECTION
)
619 kimage_free_entry(ind
);
620 /* Save this indirection page until we are
625 else if (entry
& IND_SOURCE
)
626 kimage_free_entry(entry
);
628 /* Free the final indirection page */
629 if (ind
& IND_INDIRECTION
)
630 kimage_free_entry(ind
);
632 /* Handle any machine specific cleanup */
633 machine_kexec_cleanup(image
);
635 /* Free the kexec control pages... */
636 kimage_free_page_list(&image
->control_pages
);
640 static kimage_entry_t
*kimage_dst_used(struct kimage
*image
,
643 kimage_entry_t
*ptr
, entry
;
644 unsigned long destination
= 0;
646 for_each_kimage_entry(image
, ptr
, entry
) {
647 if (entry
& IND_DESTINATION
)
648 destination
= entry
& PAGE_MASK
;
649 else if (entry
& IND_SOURCE
) {
650 if (page
== destination
)
652 destination
+= PAGE_SIZE
;
659 static struct page
*kimage_alloc_page(struct kimage
*image
,
661 unsigned long destination
)
664 * Here we implement safeguards to ensure that a source page
665 * is not copied to its destination page before the data on
666 * the destination page is no longer useful.
668 * To do this we maintain the invariant that a source page is
669 * either its own destination page, or it is not a
670 * destination page at all.
672 * That is slightly stronger than required, but the proof
673 * that no problems will not occur is trivial, and the
674 * implementation is simply to verify.
676 * When allocating all pages normally this algorithm will run
677 * in O(N) time, but in the worst case it will run in O(N^2)
678 * time. If the runtime is a problem the data structures can
685 * Walk through the list of destination pages, and see if I
688 list_for_each_entry(page
, &image
->dest_pages
, lru
) {
689 addr
= page_to_pfn(page
) << PAGE_SHIFT
;
690 if (addr
== destination
) {
691 list_del(&page
->lru
);
699 /* Allocate a page, if we run out of memory give up */
700 page
= kimage_alloc_pages(gfp_mask
, 0);
703 /* If the page cannot be used file it away */
704 if (page_to_pfn(page
) >
705 (KEXEC_SOURCE_MEMORY_LIMIT
>> PAGE_SHIFT
)) {
706 list_add(&page
->lru
, &image
->unuseable_pages
);
709 addr
= page_to_pfn(page
) << PAGE_SHIFT
;
711 /* If it is the destination page we want use it */
712 if (addr
== destination
)
715 /* If the page is not a destination page use it */
716 if (!kimage_is_destination_range(image
, addr
,
721 * I know that the page is someones destination page.
722 * See if there is already a source page for this
723 * destination page. And if so swap the source pages.
725 old
= kimage_dst_used(image
, addr
);
728 unsigned long old_addr
;
729 struct page
*old_page
;
731 old_addr
= *old
& PAGE_MASK
;
732 old_page
= pfn_to_page(old_addr
>> PAGE_SHIFT
);
733 copy_highpage(page
, old_page
);
734 *old
= addr
| (*old
& ~PAGE_MASK
);
736 /* The old page I have found cannot be a
737 * destination page, so return it.
744 /* Place the page on the destination list I
747 list_add(&page
->lru
, &image
->dest_pages
);
754 static int kimage_load_normal_segment(struct kimage
*image
,
755 struct kexec_segment
*segment
)
758 unsigned long ubytes
, mbytes
;
760 unsigned char __user
*buf
;
764 ubytes
= segment
->bufsz
;
765 mbytes
= segment
->memsz
;
766 maddr
= segment
->mem
;
768 result
= kimage_set_destination(image
, maddr
);
775 size_t uchunk
, mchunk
;
777 page
= kimage_alloc_page(image
, GFP_HIGHUSER
, maddr
);
782 result
= kimage_add_page(image
, page_to_pfn(page
)
788 /* Start with a clear page */
789 memset(ptr
, 0, PAGE_SIZE
);
790 ptr
+= maddr
& ~PAGE_MASK
;
791 mchunk
= PAGE_SIZE
- (maddr
& ~PAGE_MASK
);
799 result
= copy_from_user(ptr
, buf
, uchunk
);
802 result
= (result
< 0) ? result
: -EIO
;
814 static int kimage_load_crash_segment(struct kimage
*image
,
815 struct kexec_segment
*segment
)
817 /* For crash dumps kernels we simply copy the data from
818 * user space to it's destination.
819 * We do things a page at a time for the sake of kmap.
822 unsigned long ubytes
, mbytes
;
824 unsigned char __user
*buf
;
828 ubytes
= segment
->bufsz
;
829 mbytes
= segment
->memsz
;
830 maddr
= segment
->mem
;
834 size_t uchunk
, mchunk
;
836 page
= pfn_to_page(maddr
>> PAGE_SHIFT
);
842 ptr
+= maddr
& ~PAGE_MASK
;
843 mchunk
= PAGE_SIZE
- (maddr
& ~PAGE_MASK
);
848 if (uchunk
> ubytes
) {
850 /* Zero the trailing part of the page */
851 memset(ptr
+ uchunk
, 0, mchunk
- uchunk
);
853 result
= copy_from_user(ptr
, buf
, uchunk
);
854 kexec_flush_icache_page(page
);
857 result
= (result
< 0) ? result
: -EIO
;
869 static int kimage_load_segment(struct kimage
*image
,
870 struct kexec_segment
*segment
)
872 int result
= -ENOMEM
;
874 switch (image
->type
) {
875 case KEXEC_TYPE_DEFAULT
:
876 result
= kimage_load_normal_segment(image
, segment
);
878 case KEXEC_TYPE_CRASH
:
879 result
= kimage_load_crash_segment(image
, segment
);
887 * Exec Kernel system call: for obvious reasons only root may call it.
889 * This call breaks up into three pieces.
890 * - A generic part which loads the new kernel from the current
891 * address space, and very carefully places the data in the
894 * - A generic part that interacts with the kernel and tells all of
895 * the devices to shut down. Preventing on-going dmas, and placing
896 * the devices in a consistent state so a later kernel can
899 * - A machine specific part that includes the syscall number
900 * and the copies the image to it's final destination. And
901 * jumps into the image at entry.
903 * kexec does not sync, or unmount filesystems so if you need
904 * that to happen you need to do that yourself.
906 struct kimage
*kexec_image
;
907 struct kimage
*kexec_crash_image
;
909 * A home grown binary mutex.
910 * Nothing can wait so this mutex is safe to use
911 * in interrupt context :)
913 static int kexec_lock
;
915 asmlinkage
long sys_kexec_load(unsigned long entry
, unsigned long nr_segments
,
916 struct kexec_segment __user
*segments
,
919 struct kimage
**dest_image
, *image
;
923 /* We only trust the superuser with rebooting the system. */
924 if (!capable(CAP_SYS_BOOT
))
928 * Verify we have a legal set of flags
929 * This leaves us room for future extensions.
931 if ((flags
& KEXEC_FLAGS
) != (flags
& ~KEXEC_ARCH_MASK
))
934 /* Verify we are on the appropriate architecture */
935 if (((flags
& KEXEC_ARCH_MASK
) != KEXEC_ARCH
) &&
936 ((flags
& KEXEC_ARCH_MASK
) != KEXEC_ARCH_DEFAULT
))
939 /* Put an artificial cap on the number
940 * of segments passed to kexec_load.
942 if (nr_segments
> KEXEC_SEGMENT_MAX
)
948 /* Because we write directly to the reserved memory
949 * region when loading crash kernels we need a mutex here to
950 * prevent multiple crash kernels from attempting to load
951 * simultaneously, and to prevent a crash kernel from loading
952 * over the top of a in use crash kernel.
954 * KISS: always take the mutex.
956 locked
= xchg(&kexec_lock
, 1);
960 dest_image
= &kexec_image
;
961 if (flags
& KEXEC_ON_CRASH
)
962 dest_image
= &kexec_crash_image
;
963 if (nr_segments
> 0) {
966 /* Loading another kernel to reboot into */
967 if ((flags
& KEXEC_ON_CRASH
) == 0)
968 result
= kimage_normal_alloc(&image
, entry
,
969 nr_segments
, segments
);
970 /* Loading another kernel to switch to if this one crashes */
971 else if (flags
& KEXEC_ON_CRASH
) {
972 /* Free any current crash dump kernel before
975 kimage_free(xchg(&kexec_crash_image
, NULL
));
976 result
= kimage_crash_alloc(&image
, entry
,
977 nr_segments
, segments
);
982 result
= machine_kexec_prepare(image
);
986 for (i
= 0; i
< nr_segments
; i
++) {
987 result
= kimage_load_segment(image
, &image
->segment
[i
]);
991 result
= kimage_terminate(image
);
995 /* Install the new kernel, and Uninstall the old */
996 image
= xchg(dest_image
, image
);
999 locked
= xchg(&kexec_lock
, 0); /* Release the mutex */
1006 #ifdef CONFIG_COMPAT
1007 asmlinkage
long compat_sys_kexec_load(unsigned long entry
,
1008 unsigned long nr_segments
,
1009 struct compat_kexec_segment __user
*segments
,
1010 unsigned long flags
)
1012 struct compat_kexec_segment in
;
1013 struct kexec_segment out
, __user
*ksegments
;
1014 unsigned long i
, result
;
1016 /* Don't allow clients that don't understand the native
1017 * architecture to do anything.
1019 if ((flags
& KEXEC_ARCH_MASK
) == KEXEC_ARCH_DEFAULT
)
1022 if (nr_segments
> KEXEC_SEGMENT_MAX
)
1025 ksegments
= compat_alloc_user_space(nr_segments
* sizeof(out
));
1026 for (i
=0; i
< nr_segments
; i
++) {
1027 result
= copy_from_user(&in
, &segments
[i
], sizeof(in
));
1031 out
.buf
= compat_ptr(in
.buf
);
1032 out
.bufsz
= in
.bufsz
;
1034 out
.memsz
= in
.memsz
;
1036 result
= copy_to_user(&ksegments
[i
], &out
, sizeof(out
));
1041 return sys_kexec_load(entry
, nr_segments
, ksegments
, flags
);
1045 void crash_kexec(struct pt_regs
*regs
)
1050 /* Take the kexec_lock here to prevent sys_kexec_load
1051 * running on one cpu from replacing the crash kernel
1052 * we are using after a panic on a different cpu.
1054 * If the crash kernel was not located in a fixed area
1055 * of memory the xchg(&kexec_crash_image) would be
1056 * sufficient. But since I reuse the memory...
1058 locked
= xchg(&kexec_lock
, 1);
1060 if (kexec_crash_image
) {
1061 struct pt_regs fixed_regs
;
1062 crash_setup_regs(&fixed_regs
, regs
);
1063 machine_crash_shutdown(&fixed_regs
);
1064 machine_kexec(kexec_crash_image
);
1066 locked
= xchg(&kexec_lock
, 0);
1071 static u32
*append_elf_note(u32
*buf
, char *name
, unsigned type
, void *data
,
1074 struct elf_note note
;
1076 note
.n_namesz
= strlen(name
) + 1;
1077 note
.n_descsz
= data_len
;
1079 memcpy(buf
, ¬e
, sizeof(note
));
1080 buf
+= (sizeof(note
) + 3)/4;
1081 memcpy(buf
, name
, note
.n_namesz
);
1082 buf
+= (note
.n_namesz
+ 3)/4;
1083 memcpy(buf
, data
, note
.n_descsz
);
1084 buf
+= (note
.n_descsz
+ 3)/4;
1089 static void final_note(u32
*buf
)
1091 struct elf_note note
;
1096 memcpy(buf
, ¬e
, sizeof(note
));
1099 void crash_save_cpu(struct pt_regs
*regs
, int cpu
)
1101 struct elf_prstatus prstatus
;
1104 if ((cpu
< 0) || (cpu
>= NR_CPUS
))
1107 /* Using ELF notes here is opportunistic.
1108 * I need a well defined structure format
1109 * for the data I pass, and I need tags
1110 * on the data to indicate what information I have
1111 * squirrelled away. ELF notes happen to provide
1112 * all of that, so there is no need to invent something new.
1114 buf
= (u32
*)per_cpu_ptr(crash_notes
, cpu
);
1117 memset(&prstatus
, 0, sizeof(prstatus
));
1118 prstatus
.pr_pid
= current
->pid
;
1119 elf_core_copy_regs(&prstatus
.pr_reg
, regs
);
1120 buf
= append_elf_note(buf
, KEXEC_CORE_NOTE_NAME
, NT_PRSTATUS
,
1121 &prstatus
, sizeof(prstatus
));
1125 static int __init
crash_notes_memory_init(void)
1127 /* Allocate memory for saving cpu registers. */
1128 crash_notes
= alloc_percpu(note_buf_t
);
1130 printk("Kexec: Memory allocation for saving cpu register"
1131 " states failed\n");
1136 module_init(crash_notes_memory_init
)