ACPI: thinkpad-acpi: preserve radio state across shutdown
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / kexec.c
blobcba525a35fb4a7f34e7b65ea31448782f1739990
1 /*
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.
7 */
9 #include <linux/capability.h>
10 #include <linux/mm.h>
11 #include <linux/file.h>
12 #include <linux/slab.h>
13 #include <linux/fs.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>
30 #include <linux/pm.h>
31 #include <linux/cpu.h>
32 #include <linux/console.h>
34 #include <asm/page.h>
35 #include <asm/uaccess.h>
36 #include <asm/io.h>
37 #include <asm/system.h>
38 #include <asm/sections.h>
40 /* Per cpu memory for storing cpu states in case of system crash. */
41 note_buf_t* crash_notes;
43 /* vmcoreinfo stuff */
44 unsigned char vmcoreinfo_data[VMCOREINFO_BYTES];
45 u32 vmcoreinfo_note[VMCOREINFO_NOTE_SIZE/4];
46 size_t vmcoreinfo_size;
47 size_t vmcoreinfo_max_size = sizeof(vmcoreinfo_data);
49 /* Location of the reserved area for the crash kernel */
50 struct resource crashk_res = {
51 .name = "Crash kernel",
52 .start = 0,
53 .end = 0,
54 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
57 int kexec_should_crash(struct task_struct *p)
59 if (in_interrupt() || !p->pid || is_global_init(p) || panic_on_oops)
60 return 1;
61 return 0;
65 * When kexec transitions to the new kernel there is a one-to-one
66 * mapping between physical and virtual addresses. On processors
67 * where you can disable the MMU this is trivial, and easy. For
68 * others it is still a simple predictable page table to setup.
70 * In that environment kexec copies the new kernel to its final
71 * resting place. This means I can only support memory whose
72 * physical address can fit in an unsigned long. In particular
73 * addresses where (pfn << PAGE_SHIFT) > ULONG_MAX cannot be handled.
74 * If the assembly stub has more restrictive requirements
75 * KEXEC_SOURCE_MEMORY_LIMIT and KEXEC_DEST_MEMORY_LIMIT can be
76 * defined more restrictively in <asm/kexec.h>.
78 * The code for the transition from the current kernel to the
79 * the new kernel is placed in the control_code_buffer, whose size
80 * is given by KEXEC_CONTROL_PAGE_SIZE. In the best case only a single
81 * page of memory is necessary, but some architectures require more.
82 * Because this memory must be identity mapped in the transition from
83 * virtual to physical addresses it must live in the range
84 * 0 - TASK_SIZE, as only the user space mappings are arbitrarily
85 * modifiable.
87 * The assembly stub in the control code buffer is passed a linked list
88 * of descriptor pages detailing the source pages of the new kernel,
89 * and the destination addresses of those source pages. As this data
90 * structure is not used in the context of the current OS, it must
91 * be self-contained.
93 * The code has been made to work with highmem pages and will use a
94 * destination page in its final resting place (if it happens
95 * to allocate it). The end product of this is that most of the
96 * physical address space, and most of RAM can be used.
98 * Future directions include:
99 * - allocating a page table with the control code buffer identity
100 * mapped, to simplify machine_kexec and make kexec_on_panic more
101 * reliable.
105 * KIMAGE_NO_DEST is an impossible destination address..., for
106 * allocating pages whose destination address we do not care about.
108 #define KIMAGE_NO_DEST (-1UL)
110 static int kimage_is_destination_range(struct kimage *image,
111 unsigned long start, unsigned long end);
112 static struct page *kimage_alloc_page(struct kimage *image,
113 gfp_t gfp_mask,
114 unsigned long dest);
116 static int do_kimage_alloc(struct kimage **rimage, unsigned long entry,
117 unsigned long nr_segments,
118 struct kexec_segment __user *segments)
120 size_t segment_bytes;
121 struct kimage *image;
122 unsigned long i;
123 int result;
125 /* Allocate a controlling structure */
126 result = -ENOMEM;
127 image = kzalloc(sizeof(*image), GFP_KERNEL);
128 if (!image)
129 goto out;
131 image->head = 0;
132 image->entry = &image->head;
133 image->last_entry = &image->head;
134 image->control_page = ~0; /* By default this does not apply */
135 image->start = entry;
136 image->type = KEXEC_TYPE_DEFAULT;
138 /* Initialize the list of control pages */
139 INIT_LIST_HEAD(&image->control_pages);
141 /* Initialize the list of destination pages */
142 INIT_LIST_HEAD(&image->dest_pages);
144 /* Initialize the list of unuseable pages */
145 INIT_LIST_HEAD(&image->unuseable_pages);
147 /* Read in the segments */
148 image->nr_segments = nr_segments;
149 segment_bytes = nr_segments * sizeof(*segments);
150 result = copy_from_user(image->segment, segments, segment_bytes);
151 if (result)
152 goto out;
155 * Verify we have good destination addresses. The caller is
156 * responsible for making certain we don't attempt to load
157 * the new image into invalid or reserved areas of RAM. This
158 * just verifies it is an address we can use.
160 * Since the kernel does everything in page size chunks ensure
161 * the destination addreses are page aligned. Too many
162 * special cases crop of when we don't do this. The most
163 * insidious is getting overlapping destination addresses
164 * simply because addresses are changed to page size
165 * granularity.
167 result = -EADDRNOTAVAIL;
168 for (i = 0; i < nr_segments; i++) {
169 unsigned long mstart, mend;
171 mstart = image->segment[i].mem;
172 mend = mstart + image->segment[i].memsz;
173 if ((mstart & ~PAGE_MASK) || (mend & ~PAGE_MASK))
174 goto out;
175 if (mend >= KEXEC_DESTINATION_MEMORY_LIMIT)
176 goto out;
179 /* Verify our destination addresses do not overlap.
180 * If we alloed overlapping destination addresses
181 * through very weird things can happen with no
182 * easy explanation as one segment stops on another.
184 result = -EINVAL;
185 for (i = 0; i < nr_segments; i++) {
186 unsigned long mstart, mend;
187 unsigned long j;
189 mstart = image->segment[i].mem;
190 mend = mstart + image->segment[i].memsz;
191 for (j = 0; j < i; j++) {
192 unsigned long pstart, pend;
193 pstart = image->segment[j].mem;
194 pend = pstart + image->segment[j].memsz;
195 /* Do the segments overlap ? */
196 if ((mend > pstart) && (mstart < pend))
197 goto out;
201 /* Ensure our buffer sizes are strictly less than
202 * our memory sizes. This should always be the case,
203 * and it is easier to check up front than to be surprised
204 * later on.
206 result = -EINVAL;
207 for (i = 0; i < nr_segments; i++) {
208 if (image->segment[i].bufsz > image->segment[i].memsz)
209 goto out;
212 result = 0;
213 out:
214 if (result == 0)
215 *rimage = image;
216 else
217 kfree(image);
219 return result;
223 static int kimage_normal_alloc(struct kimage **rimage, unsigned long entry,
224 unsigned long nr_segments,
225 struct kexec_segment __user *segments)
227 int result;
228 struct kimage *image;
230 /* Allocate and initialize a controlling structure */
231 image = NULL;
232 result = do_kimage_alloc(&image, entry, nr_segments, segments);
233 if (result)
234 goto out;
236 *rimage = image;
239 * Find a location for the control code buffer, and add it
240 * the vector of segments so that it's pages will also be
241 * counted as destination pages.
243 result = -ENOMEM;
244 image->control_code_page = kimage_alloc_control_pages(image,
245 get_order(KEXEC_CONTROL_PAGE_SIZE));
246 if (!image->control_code_page) {
247 printk(KERN_ERR "Could not allocate control_code_buffer\n");
248 goto out;
251 image->swap_page = kimage_alloc_control_pages(image, 0);
252 if (!image->swap_page) {
253 printk(KERN_ERR "Could not allocate swap buffer\n");
254 goto out;
257 result = 0;
258 out:
259 if (result == 0)
260 *rimage = image;
261 else
262 kfree(image);
264 return result;
267 static int kimage_crash_alloc(struct kimage **rimage, unsigned long entry,
268 unsigned long nr_segments,
269 struct kexec_segment __user *segments)
271 int result;
272 struct kimage *image;
273 unsigned long i;
275 image = NULL;
276 /* Verify we have a valid entry point */
277 if ((entry < crashk_res.start) || (entry > crashk_res.end)) {
278 result = -EADDRNOTAVAIL;
279 goto out;
282 /* Allocate and initialize a controlling structure */
283 result = do_kimage_alloc(&image, entry, nr_segments, segments);
284 if (result)
285 goto out;
287 /* Enable the special crash kernel control page
288 * allocation policy.
290 image->control_page = crashk_res.start;
291 image->type = KEXEC_TYPE_CRASH;
294 * Verify we have good destination addresses. Normally
295 * the caller is responsible for making certain we don't
296 * attempt to load the new image into invalid or reserved
297 * areas of RAM. But crash kernels are preloaded into a
298 * reserved area of ram. We must ensure the addresses
299 * are in the reserved area otherwise preloading the
300 * kernel could corrupt things.
302 result = -EADDRNOTAVAIL;
303 for (i = 0; i < nr_segments; i++) {
304 unsigned long mstart, mend;
306 mstart = image->segment[i].mem;
307 mend = mstart + image->segment[i].memsz - 1;
308 /* Ensure we are within the crash kernel limits */
309 if ((mstart < crashk_res.start) || (mend > crashk_res.end))
310 goto out;
314 * Find a location for the control code buffer, and add
315 * the vector of segments so that it's pages will also be
316 * counted as destination pages.
318 result = -ENOMEM;
319 image->control_code_page = kimage_alloc_control_pages(image,
320 get_order(KEXEC_CONTROL_PAGE_SIZE));
321 if (!image->control_code_page) {
322 printk(KERN_ERR "Could not allocate control_code_buffer\n");
323 goto out;
326 result = 0;
327 out:
328 if (result == 0)
329 *rimage = image;
330 else
331 kfree(image);
333 return result;
336 static int kimage_is_destination_range(struct kimage *image,
337 unsigned long start,
338 unsigned long end)
340 unsigned long i;
342 for (i = 0; i < image->nr_segments; i++) {
343 unsigned long mstart, mend;
345 mstart = image->segment[i].mem;
346 mend = mstart + image->segment[i].memsz;
347 if ((end > mstart) && (start < mend))
348 return 1;
351 return 0;
354 static struct page *kimage_alloc_pages(gfp_t gfp_mask, unsigned int order)
356 struct page *pages;
358 pages = alloc_pages(gfp_mask, order);
359 if (pages) {
360 unsigned int count, i;
361 pages->mapping = NULL;
362 set_page_private(pages, order);
363 count = 1 << order;
364 for (i = 0; i < count; i++)
365 SetPageReserved(pages + i);
368 return pages;
371 static void kimage_free_pages(struct page *page)
373 unsigned int order, count, i;
375 order = page_private(page);
376 count = 1 << order;
377 for (i = 0; i < count; i++)
378 ClearPageReserved(page + i);
379 __free_pages(page, order);
382 static void kimage_free_page_list(struct list_head *list)
384 struct list_head *pos, *next;
386 list_for_each_safe(pos, next, list) {
387 struct page *page;
389 page = list_entry(pos, struct page, lru);
390 list_del(&page->lru);
391 kimage_free_pages(page);
395 static struct page *kimage_alloc_normal_control_pages(struct kimage *image,
396 unsigned int order)
398 /* Control pages are special, they are the intermediaries
399 * that are needed while we copy the rest of the pages
400 * to their final resting place. As such they must
401 * not conflict with either the destination addresses
402 * or memory the kernel is already using.
404 * The only case where we really need more than one of
405 * these are for architectures where we cannot disable
406 * the MMU and must instead generate an identity mapped
407 * page table for all of the memory.
409 * At worst this runs in O(N) of the image size.
411 struct list_head extra_pages;
412 struct page *pages;
413 unsigned int count;
415 count = 1 << order;
416 INIT_LIST_HEAD(&extra_pages);
418 /* Loop while I can allocate a page and the page allocated
419 * is a destination page.
421 do {
422 unsigned long pfn, epfn, addr, eaddr;
424 pages = kimage_alloc_pages(GFP_KERNEL, order);
425 if (!pages)
426 break;
427 pfn = page_to_pfn(pages);
428 epfn = pfn + count;
429 addr = pfn << PAGE_SHIFT;
430 eaddr = epfn << PAGE_SHIFT;
431 if ((epfn >= (KEXEC_CONTROL_MEMORY_LIMIT >> PAGE_SHIFT)) ||
432 kimage_is_destination_range(image, addr, eaddr)) {
433 list_add(&pages->lru, &extra_pages);
434 pages = NULL;
436 } while (!pages);
438 if (pages) {
439 /* Remember the allocated page... */
440 list_add(&pages->lru, &image->control_pages);
442 /* Because the page is already in it's destination
443 * location we will never allocate another page at
444 * that address. Therefore kimage_alloc_pages
445 * will not return it (again) and we don't need
446 * to give it an entry in image->segment[].
449 /* Deal with the destination pages I have inadvertently allocated.
451 * Ideally I would convert multi-page allocations into single
452 * page allocations, and add everyting to image->dest_pages.
454 * For now it is simpler to just free the pages.
456 kimage_free_page_list(&extra_pages);
458 return pages;
461 static struct page *kimage_alloc_crash_control_pages(struct kimage *image,
462 unsigned int order)
464 /* Control pages are special, they are the intermediaries
465 * that are needed while we copy the rest of the pages
466 * to their final resting place. As such they must
467 * not conflict with either the destination addresses
468 * or memory the kernel is already using.
470 * Control pages are also the only pags we must allocate
471 * when loading a crash kernel. All of the other pages
472 * are specified by the segments and we just memcpy
473 * into them directly.
475 * The only case where we really need more than one of
476 * these are for architectures where we cannot disable
477 * the MMU and must instead generate an identity mapped
478 * page table for all of the memory.
480 * Given the low demand this implements a very simple
481 * allocator that finds the first hole of the appropriate
482 * size in the reserved memory region, and allocates all
483 * of the memory up to and including the hole.
485 unsigned long hole_start, hole_end, size;
486 struct page *pages;
488 pages = NULL;
489 size = (1 << order) << PAGE_SHIFT;
490 hole_start = (image->control_page + (size - 1)) & ~(size - 1);
491 hole_end = hole_start + size - 1;
492 while (hole_end <= crashk_res.end) {
493 unsigned long i;
495 if (hole_end > KEXEC_CONTROL_MEMORY_LIMIT)
496 break;
497 if (hole_end > crashk_res.end)
498 break;
499 /* See if I overlap any of the segments */
500 for (i = 0; i < image->nr_segments; i++) {
501 unsigned long mstart, mend;
503 mstart = image->segment[i].mem;
504 mend = mstart + image->segment[i].memsz - 1;
505 if ((hole_end >= mstart) && (hole_start <= mend)) {
506 /* Advance the hole to the end of the segment */
507 hole_start = (mend + (size - 1)) & ~(size - 1);
508 hole_end = hole_start + size - 1;
509 break;
512 /* If I don't overlap any segments I have found my hole! */
513 if (i == image->nr_segments) {
514 pages = pfn_to_page(hole_start >> PAGE_SHIFT);
515 break;
518 if (pages)
519 image->control_page = hole_end;
521 return pages;
525 struct page *kimage_alloc_control_pages(struct kimage *image,
526 unsigned int order)
528 struct page *pages = NULL;
530 switch (image->type) {
531 case KEXEC_TYPE_DEFAULT:
532 pages = kimage_alloc_normal_control_pages(image, order);
533 break;
534 case KEXEC_TYPE_CRASH:
535 pages = kimage_alloc_crash_control_pages(image, order);
536 break;
539 return pages;
542 static int kimage_add_entry(struct kimage *image, kimage_entry_t entry)
544 if (*image->entry != 0)
545 image->entry++;
547 if (image->entry == image->last_entry) {
548 kimage_entry_t *ind_page;
549 struct page *page;
551 page = kimage_alloc_page(image, GFP_KERNEL, KIMAGE_NO_DEST);
552 if (!page)
553 return -ENOMEM;
555 ind_page = page_address(page);
556 *image->entry = virt_to_phys(ind_page) | IND_INDIRECTION;
557 image->entry = ind_page;
558 image->last_entry = ind_page +
559 ((PAGE_SIZE/sizeof(kimage_entry_t)) - 1);
561 *image->entry = entry;
562 image->entry++;
563 *image->entry = 0;
565 return 0;
568 static int kimage_set_destination(struct kimage *image,
569 unsigned long destination)
571 int result;
573 destination &= PAGE_MASK;
574 result = kimage_add_entry(image, destination | IND_DESTINATION);
575 if (result == 0)
576 image->destination = destination;
578 return result;
582 static int kimage_add_page(struct kimage *image, unsigned long page)
584 int result;
586 page &= PAGE_MASK;
587 result = kimage_add_entry(image, page | IND_SOURCE);
588 if (result == 0)
589 image->destination += PAGE_SIZE;
591 return result;
595 static void kimage_free_extra_pages(struct kimage *image)
597 /* Walk through and free any extra destination pages I may have */
598 kimage_free_page_list(&image->dest_pages);
600 /* Walk through and free any unuseable pages I have cached */
601 kimage_free_page_list(&image->unuseable_pages);
604 static void kimage_terminate(struct kimage *image)
606 if (*image->entry != 0)
607 image->entry++;
609 *image->entry = IND_DONE;
612 #define for_each_kimage_entry(image, ptr, entry) \
613 for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE); \
614 ptr = (entry & IND_INDIRECTION)? \
615 phys_to_virt((entry & PAGE_MASK)): ptr +1)
617 static void kimage_free_entry(kimage_entry_t entry)
619 struct page *page;
621 page = pfn_to_page(entry >> PAGE_SHIFT);
622 kimage_free_pages(page);
625 static void kimage_free(struct kimage *image)
627 kimage_entry_t *ptr, entry;
628 kimage_entry_t ind = 0;
630 if (!image)
631 return;
633 kimage_free_extra_pages(image);
634 for_each_kimage_entry(image, ptr, entry) {
635 if (entry & IND_INDIRECTION) {
636 /* Free the previous indirection page */
637 if (ind & IND_INDIRECTION)
638 kimage_free_entry(ind);
639 /* Save this indirection page until we are
640 * done with it.
642 ind = entry;
644 else if (entry & IND_SOURCE)
645 kimage_free_entry(entry);
647 /* Free the final indirection page */
648 if (ind & IND_INDIRECTION)
649 kimage_free_entry(ind);
651 /* Handle any machine specific cleanup */
652 machine_kexec_cleanup(image);
654 /* Free the kexec control pages... */
655 kimage_free_page_list(&image->control_pages);
656 kfree(image);
659 static kimage_entry_t *kimage_dst_used(struct kimage *image,
660 unsigned long page)
662 kimage_entry_t *ptr, entry;
663 unsigned long destination = 0;
665 for_each_kimage_entry(image, ptr, entry) {
666 if (entry & IND_DESTINATION)
667 destination = entry & PAGE_MASK;
668 else if (entry & IND_SOURCE) {
669 if (page == destination)
670 return ptr;
671 destination += PAGE_SIZE;
675 return NULL;
678 static struct page *kimage_alloc_page(struct kimage *image,
679 gfp_t gfp_mask,
680 unsigned long destination)
683 * Here we implement safeguards to ensure that a source page
684 * is not copied to its destination page before the data on
685 * the destination page is no longer useful.
687 * To do this we maintain the invariant that a source page is
688 * either its own destination page, or it is not a
689 * destination page at all.
691 * That is slightly stronger than required, but the proof
692 * that no problems will not occur is trivial, and the
693 * implementation is simply to verify.
695 * When allocating all pages normally this algorithm will run
696 * in O(N) time, but in the worst case it will run in O(N^2)
697 * time. If the runtime is a problem the data structures can
698 * be fixed.
700 struct page *page;
701 unsigned long addr;
704 * Walk through the list of destination pages, and see if I
705 * have a match.
707 list_for_each_entry(page, &image->dest_pages, lru) {
708 addr = page_to_pfn(page) << PAGE_SHIFT;
709 if (addr == destination) {
710 list_del(&page->lru);
711 return page;
714 page = NULL;
715 while (1) {
716 kimage_entry_t *old;
718 /* Allocate a page, if we run out of memory give up */
719 page = kimage_alloc_pages(gfp_mask, 0);
720 if (!page)
721 return NULL;
722 /* If the page cannot be used file it away */
723 if (page_to_pfn(page) >
724 (KEXEC_SOURCE_MEMORY_LIMIT >> PAGE_SHIFT)) {
725 list_add(&page->lru, &image->unuseable_pages);
726 continue;
728 addr = page_to_pfn(page) << PAGE_SHIFT;
730 /* If it is the destination page we want use it */
731 if (addr == destination)
732 break;
734 /* If the page is not a destination page use it */
735 if (!kimage_is_destination_range(image, addr,
736 addr + PAGE_SIZE))
737 break;
740 * I know that the page is someones destination page.
741 * See if there is already a source page for this
742 * destination page. And if so swap the source pages.
744 old = kimage_dst_used(image, addr);
745 if (old) {
746 /* If so move it */
747 unsigned long old_addr;
748 struct page *old_page;
750 old_addr = *old & PAGE_MASK;
751 old_page = pfn_to_page(old_addr >> PAGE_SHIFT);
752 copy_highpage(page, old_page);
753 *old = addr | (*old & ~PAGE_MASK);
755 /* The old page I have found cannot be a
756 * destination page, so return it if it's
757 * gfp_flags honor the ones passed in.
759 if (!(gfp_mask & __GFP_HIGHMEM) &&
760 PageHighMem(old_page)) {
761 kimage_free_pages(old_page);
762 continue;
764 addr = old_addr;
765 page = old_page;
766 break;
768 else {
769 /* Place the page on the destination list I
770 * will use it later.
772 list_add(&page->lru, &image->dest_pages);
776 return page;
779 static int kimage_load_normal_segment(struct kimage *image,
780 struct kexec_segment *segment)
782 unsigned long maddr;
783 unsigned long ubytes, mbytes;
784 int result;
785 unsigned char __user *buf;
787 result = 0;
788 buf = segment->buf;
789 ubytes = segment->bufsz;
790 mbytes = segment->memsz;
791 maddr = segment->mem;
793 result = kimage_set_destination(image, maddr);
794 if (result < 0)
795 goto out;
797 while (mbytes) {
798 struct page *page;
799 char *ptr;
800 size_t uchunk, mchunk;
802 page = kimage_alloc_page(image, GFP_HIGHUSER, maddr);
803 if (!page) {
804 result = -ENOMEM;
805 goto out;
807 result = kimage_add_page(image, page_to_pfn(page)
808 << PAGE_SHIFT);
809 if (result < 0)
810 goto out;
812 ptr = kmap(page);
813 /* Start with a clear page */
814 memset(ptr, 0, PAGE_SIZE);
815 ptr += maddr & ~PAGE_MASK;
816 mchunk = PAGE_SIZE - (maddr & ~PAGE_MASK);
817 if (mchunk > mbytes)
818 mchunk = mbytes;
820 uchunk = mchunk;
821 if (uchunk > ubytes)
822 uchunk = ubytes;
824 result = copy_from_user(ptr, buf, uchunk);
825 kunmap(page);
826 if (result) {
827 result = (result < 0) ? result : -EIO;
828 goto out;
830 ubytes -= uchunk;
831 maddr += mchunk;
832 buf += mchunk;
833 mbytes -= mchunk;
835 out:
836 return result;
839 static int kimage_load_crash_segment(struct kimage *image,
840 struct kexec_segment *segment)
842 /* For crash dumps kernels we simply copy the data from
843 * user space to it's destination.
844 * We do things a page at a time for the sake of kmap.
846 unsigned long maddr;
847 unsigned long ubytes, mbytes;
848 int result;
849 unsigned char __user *buf;
851 result = 0;
852 buf = segment->buf;
853 ubytes = segment->bufsz;
854 mbytes = segment->memsz;
855 maddr = segment->mem;
856 while (mbytes) {
857 struct page *page;
858 char *ptr;
859 size_t uchunk, mchunk;
861 page = pfn_to_page(maddr >> PAGE_SHIFT);
862 if (!page) {
863 result = -ENOMEM;
864 goto out;
866 ptr = kmap(page);
867 ptr += maddr & ~PAGE_MASK;
868 mchunk = PAGE_SIZE - (maddr & ~PAGE_MASK);
869 if (mchunk > mbytes)
870 mchunk = mbytes;
872 uchunk = mchunk;
873 if (uchunk > ubytes) {
874 uchunk = ubytes;
875 /* Zero the trailing part of the page */
876 memset(ptr + uchunk, 0, mchunk - uchunk);
878 result = copy_from_user(ptr, buf, uchunk);
879 kexec_flush_icache_page(page);
880 kunmap(page);
881 if (result) {
882 result = (result < 0) ? result : -EIO;
883 goto out;
885 ubytes -= uchunk;
886 maddr += mchunk;
887 buf += mchunk;
888 mbytes -= mchunk;
890 out:
891 return result;
894 static int kimage_load_segment(struct kimage *image,
895 struct kexec_segment *segment)
897 int result = -ENOMEM;
899 switch (image->type) {
900 case KEXEC_TYPE_DEFAULT:
901 result = kimage_load_normal_segment(image, segment);
902 break;
903 case KEXEC_TYPE_CRASH:
904 result = kimage_load_crash_segment(image, segment);
905 break;
908 return result;
912 * Exec Kernel system call: for obvious reasons only root may call it.
914 * This call breaks up into three pieces.
915 * - A generic part which loads the new kernel from the current
916 * address space, and very carefully places the data in the
917 * allocated pages.
919 * - A generic part that interacts with the kernel and tells all of
920 * the devices to shut down. Preventing on-going dmas, and placing
921 * the devices in a consistent state so a later kernel can
922 * reinitialize them.
924 * - A machine specific part that includes the syscall number
925 * and the copies the image to it's final destination. And
926 * jumps into the image at entry.
928 * kexec does not sync, or unmount filesystems so if you need
929 * that to happen you need to do that yourself.
931 struct kimage *kexec_image;
932 struct kimage *kexec_crash_image;
934 static DEFINE_MUTEX(kexec_mutex);
936 SYSCALL_DEFINE4(kexec_load, unsigned long, entry, unsigned long, nr_segments,
937 struct kexec_segment __user *, segments, unsigned long, flags)
939 struct kimage **dest_image, *image;
940 int result;
942 /* We only trust the superuser with rebooting the system. */
943 if (!capable(CAP_SYS_BOOT))
944 return -EPERM;
947 * Verify we have a legal set of flags
948 * This leaves us room for future extensions.
950 if ((flags & KEXEC_FLAGS) != (flags & ~KEXEC_ARCH_MASK))
951 return -EINVAL;
953 /* Verify we are on the appropriate architecture */
954 if (((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH) &&
955 ((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH_DEFAULT))
956 return -EINVAL;
958 /* Put an artificial cap on the number
959 * of segments passed to kexec_load.
961 if (nr_segments > KEXEC_SEGMENT_MAX)
962 return -EINVAL;
964 image = NULL;
965 result = 0;
967 /* Because we write directly to the reserved memory
968 * region when loading crash kernels we need a mutex here to
969 * prevent multiple crash kernels from attempting to load
970 * simultaneously, and to prevent a crash kernel from loading
971 * over the top of a in use crash kernel.
973 * KISS: always take the mutex.
975 if (!mutex_trylock(&kexec_mutex))
976 return -EBUSY;
978 dest_image = &kexec_image;
979 if (flags & KEXEC_ON_CRASH)
980 dest_image = &kexec_crash_image;
981 if (nr_segments > 0) {
982 unsigned long i;
984 /* Loading another kernel to reboot into */
985 if ((flags & KEXEC_ON_CRASH) == 0)
986 result = kimage_normal_alloc(&image, entry,
987 nr_segments, segments);
988 /* Loading another kernel to switch to if this one crashes */
989 else if (flags & KEXEC_ON_CRASH) {
990 /* Free any current crash dump kernel before
991 * we corrupt it.
993 kimage_free(xchg(&kexec_crash_image, NULL));
994 result = kimage_crash_alloc(&image, entry,
995 nr_segments, segments);
997 if (result)
998 goto out;
1000 if (flags & KEXEC_PRESERVE_CONTEXT)
1001 image->preserve_context = 1;
1002 result = machine_kexec_prepare(image);
1003 if (result)
1004 goto out;
1006 for (i = 0; i < nr_segments; i++) {
1007 result = kimage_load_segment(image, &image->segment[i]);
1008 if (result)
1009 goto out;
1011 kimage_terminate(image);
1013 /* Install the new kernel, and Uninstall the old */
1014 image = xchg(dest_image, image);
1016 out:
1017 mutex_unlock(&kexec_mutex);
1018 kimage_free(image);
1020 return result;
1023 #ifdef CONFIG_COMPAT
1024 asmlinkage long compat_sys_kexec_load(unsigned long entry,
1025 unsigned long nr_segments,
1026 struct compat_kexec_segment __user *segments,
1027 unsigned long flags)
1029 struct compat_kexec_segment in;
1030 struct kexec_segment out, __user *ksegments;
1031 unsigned long i, result;
1033 /* Don't allow clients that don't understand the native
1034 * architecture to do anything.
1036 if ((flags & KEXEC_ARCH_MASK) == KEXEC_ARCH_DEFAULT)
1037 return -EINVAL;
1039 if (nr_segments > KEXEC_SEGMENT_MAX)
1040 return -EINVAL;
1042 ksegments = compat_alloc_user_space(nr_segments * sizeof(out));
1043 for (i=0; i < nr_segments; i++) {
1044 result = copy_from_user(&in, &segments[i], sizeof(in));
1045 if (result)
1046 return -EFAULT;
1048 out.buf = compat_ptr(in.buf);
1049 out.bufsz = in.bufsz;
1050 out.mem = in.mem;
1051 out.memsz = in.memsz;
1053 result = copy_to_user(&ksegments[i], &out, sizeof(out));
1054 if (result)
1055 return -EFAULT;
1058 return sys_kexec_load(entry, nr_segments, ksegments, flags);
1060 #endif
1062 void crash_kexec(struct pt_regs *regs)
1064 /* Take the kexec_mutex here to prevent sys_kexec_load
1065 * running on one cpu from replacing the crash kernel
1066 * we are using after a panic on a different cpu.
1068 * If the crash kernel was not located in a fixed area
1069 * of memory the xchg(&kexec_crash_image) would be
1070 * sufficient. But since I reuse the memory...
1072 if (mutex_trylock(&kexec_mutex)) {
1073 if (kexec_crash_image) {
1074 struct pt_regs fixed_regs;
1075 crash_setup_regs(&fixed_regs, regs);
1076 crash_save_vmcoreinfo();
1077 machine_crash_shutdown(&fixed_regs);
1078 machine_kexec(kexec_crash_image);
1080 mutex_unlock(&kexec_mutex);
1084 static u32 *append_elf_note(u32 *buf, char *name, unsigned type, void *data,
1085 size_t data_len)
1087 struct elf_note note;
1089 note.n_namesz = strlen(name) + 1;
1090 note.n_descsz = data_len;
1091 note.n_type = type;
1092 memcpy(buf, &note, sizeof(note));
1093 buf += (sizeof(note) + 3)/4;
1094 memcpy(buf, name, note.n_namesz);
1095 buf += (note.n_namesz + 3)/4;
1096 memcpy(buf, data, note.n_descsz);
1097 buf += (note.n_descsz + 3)/4;
1099 return buf;
1102 static void final_note(u32 *buf)
1104 struct elf_note note;
1106 note.n_namesz = 0;
1107 note.n_descsz = 0;
1108 note.n_type = 0;
1109 memcpy(buf, &note, sizeof(note));
1112 void crash_save_cpu(struct pt_regs *regs, int cpu)
1114 struct elf_prstatus prstatus;
1115 u32 *buf;
1117 if ((cpu < 0) || (cpu >= NR_CPUS))
1118 return;
1120 /* Using ELF notes here is opportunistic.
1121 * I need a well defined structure format
1122 * for the data I pass, and I need tags
1123 * on the data to indicate what information I have
1124 * squirrelled away. ELF notes happen to provide
1125 * all of that, so there is no need to invent something new.
1127 buf = (u32*)per_cpu_ptr(crash_notes, cpu);
1128 if (!buf)
1129 return;
1130 memset(&prstatus, 0, sizeof(prstatus));
1131 prstatus.pr_pid = current->pid;
1132 elf_core_copy_regs(&prstatus.pr_reg, regs);
1133 buf = append_elf_note(buf, KEXEC_CORE_NOTE_NAME, NT_PRSTATUS,
1134 &prstatus, sizeof(prstatus));
1135 final_note(buf);
1138 static int __init crash_notes_memory_init(void)
1140 /* Allocate memory for saving cpu registers. */
1141 crash_notes = alloc_percpu(note_buf_t);
1142 if (!crash_notes) {
1143 printk("Kexec: Memory allocation for saving cpu register"
1144 " states failed\n");
1145 return -ENOMEM;
1147 return 0;
1149 module_init(crash_notes_memory_init)
1153 * parsing the "crashkernel" commandline
1155 * this code is intended to be called from architecture specific code
1160 * This function parses command lines in the format
1162 * crashkernel=ramsize-range:size[,...][@offset]
1164 * The function returns 0 on success and -EINVAL on failure.
1166 static int __init parse_crashkernel_mem(char *cmdline,
1167 unsigned long long system_ram,
1168 unsigned long long *crash_size,
1169 unsigned long long *crash_base)
1171 char *cur = cmdline, *tmp;
1173 /* for each entry of the comma-separated list */
1174 do {
1175 unsigned long long start, end = ULLONG_MAX, size;
1177 /* get the start of the range */
1178 start = memparse(cur, &tmp);
1179 if (cur == tmp) {
1180 pr_warning("crashkernel: Memory value expected\n");
1181 return -EINVAL;
1183 cur = tmp;
1184 if (*cur != '-') {
1185 pr_warning("crashkernel: '-' expected\n");
1186 return -EINVAL;
1188 cur++;
1190 /* if no ':' is here, than we read the end */
1191 if (*cur != ':') {
1192 end = memparse(cur, &tmp);
1193 if (cur == tmp) {
1194 pr_warning("crashkernel: Memory "
1195 "value expected\n");
1196 return -EINVAL;
1198 cur = tmp;
1199 if (end <= start) {
1200 pr_warning("crashkernel: end <= start\n");
1201 return -EINVAL;
1205 if (*cur != ':') {
1206 pr_warning("crashkernel: ':' expected\n");
1207 return -EINVAL;
1209 cur++;
1211 size = memparse(cur, &tmp);
1212 if (cur == tmp) {
1213 pr_warning("Memory value expected\n");
1214 return -EINVAL;
1216 cur = tmp;
1217 if (size >= system_ram) {
1218 pr_warning("crashkernel: invalid size\n");
1219 return -EINVAL;
1222 /* match ? */
1223 if (system_ram >= start && system_ram < end) {
1224 *crash_size = size;
1225 break;
1227 } while (*cur++ == ',');
1229 if (*crash_size > 0) {
1230 while (*cur != ' ' && *cur != '@')
1231 cur++;
1232 if (*cur == '@') {
1233 cur++;
1234 *crash_base = memparse(cur, &tmp);
1235 if (cur == tmp) {
1236 pr_warning("Memory value expected "
1237 "after '@'\n");
1238 return -EINVAL;
1243 return 0;
1247 * That function parses "simple" (old) crashkernel command lines like
1249 * crashkernel=size[@offset]
1251 * It returns 0 on success and -EINVAL on failure.
1253 static int __init parse_crashkernel_simple(char *cmdline,
1254 unsigned long long *crash_size,
1255 unsigned long long *crash_base)
1257 char *cur = cmdline;
1259 *crash_size = memparse(cmdline, &cur);
1260 if (cmdline == cur) {
1261 pr_warning("crashkernel: memory value expected\n");
1262 return -EINVAL;
1265 if (*cur == '@')
1266 *crash_base = memparse(cur+1, &cur);
1268 return 0;
1272 * That function is the entry point for command line parsing and should be
1273 * called from the arch-specific code.
1275 int __init parse_crashkernel(char *cmdline,
1276 unsigned long long system_ram,
1277 unsigned long long *crash_size,
1278 unsigned long long *crash_base)
1280 char *p = cmdline, *ck_cmdline = NULL;
1281 char *first_colon, *first_space;
1283 BUG_ON(!crash_size || !crash_base);
1284 *crash_size = 0;
1285 *crash_base = 0;
1287 /* find crashkernel and use the last one if there are more */
1288 p = strstr(p, "crashkernel=");
1289 while (p) {
1290 ck_cmdline = p;
1291 p = strstr(p+1, "crashkernel=");
1294 if (!ck_cmdline)
1295 return -EINVAL;
1297 ck_cmdline += 12; /* strlen("crashkernel=") */
1300 * if the commandline contains a ':', then that's the extended
1301 * syntax -- if not, it must be the classic syntax
1303 first_colon = strchr(ck_cmdline, ':');
1304 first_space = strchr(ck_cmdline, ' ');
1305 if (first_colon && (!first_space || first_colon < first_space))
1306 return parse_crashkernel_mem(ck_cmdline, system_ram,
1307 crash_size, crash_base);
1308 else
1309 return parse_crashkernel_simple(ck_cmdline, crash_size,
1310 crash_base);
1312 return 0;
1317 void crash_save_vmcoreinfo(void)
1319 u32 *buf;
1321 if (!vmcoreinfo_size)
1322 return;
1324 vmcoreinfo_append_str("CRASHTIME=%ld", get_seconds());
1326 buf = (u32 *)vmcoreinfo_note;
1328 buf = append_elf_note(buf, VMCOREINFO_NOTE_NAME, 0, vmcoreinfo_data,
1329 vmcoreinfo_size);
1331 final_note(buf);
1334 void vmcoreinfo_append_str(const char *fmt, ...)
1336 va_list args;
1337 char buf[0x50];
1338 int r;
1340 va_start(args, fmt);
1341 r = vsnprintf(buf, sizeof(buf), fmt, args);
1342 va_end(args);
1344 if (r + vmcoreinfo_size > vmcoreinfo_max_size)
1345 r = vmcoreinfo_max_size - vmcoreinfo_size;
1347 memcpy(&vmcoreinfo_data[vmcoreinfo_size], buf, r);
1349 vmcoreinfo_size += r;
1353 * provide an empty default implementation here -- architecture
1354 * code may override this
1356 void __attribute__ ((weak)) arch_crash_save_vmcoreinfo(void)
1359 unsigned long __attribute__ ((weak)) paddr_vmcoreinfo_note(void)
1361 return __pa((unsigned long)(char *)&vmcoreinfo_note);
1364 static int __init crash_save_vmcoreinfo_init(void)
1366 VMCOREINFO_OSRELEASE(init_uts_ns.name.release);
1367 VMCOREINFO_PAGESIZE(PAGE_SIZE);
1369 VMCOREINFO_SYMBOL(init_uts_ns);
1370 VMCOREINFO_SYMBOL(node_online_map);
1371 VMCOREINFO_SYMBOL(swapper_pg_dir);
1372 VMCOREINFO_SYMBOL(_stext);
1374 #ifndef CONFIG_NEED_MULTIPLE_NODES
1375 VMCOREINFO_SYMBOL(mem_map);
1376 VMCOREINFO_SYMBOL(contig_page_data);
1377 #endif
1378 #ifdef CONFIG_SPARSEMEM
1379 VMCOREINFO_SYMBOL(mem_section);
1380 VMCOREINFO_LENGTH(mem_section, NR_SECTION_ROOTS);
1381 VMCOREINFO_STRUCT_SIZE(mem_section);
1382 VMCOREINFO_OFFSET(mem_section, section_mem_map);
1383 #endif
1384 VMCOREINFO_STRUCT_SIZE(page);
1385 VMCOREINFO_STRUCT_SIZE(pglist_data);
1386 VMCOREINFO_STRUCT_SIZE(zone);
1387 VMCOREINFO_STRUCT_SIZE(free_area);
1388 VMCOREINFO_STRUCT_SIZE(list_head);
1389 VMCOREINFO_SIZE(nodemask_t);
1390 VMCOREINFO_OFFSET(page, flags);
1391 VMCOREINFO_OFFSET(page, _count);
1392 VMCOREINFO_OFFSET(page, mapping);
1393 VMCOREINFO_OFFSET(page, lru);
1394 VMCOREINFO_OFFSET(pglist_data, node_zones);
1395 VMCOREINFO_OFFSET(pglist_data, nr_zones);
1396 #ifdef CONFIG_FLAT_NODE_MEM_MAP
1397 VMCOREINFO_OFFSET(pglist_data, node_mem_map);
1398 #endif
1399 VMCOREINFO_OFFSET(pglist_data, node_start_pfn);
1400 VMCOREINFO_OFFSET(pglist_data, node_spanned_pages);
1401 VMCOREINFO_OFFSET(pglist_data, node_id);
1402 VMCOREINFO_OFFSET(zone, free_area);
1403 VMCOREINFO_OFFSET(zone, vm_stat);
1404 VMCOREINFO_OFFSET(zone, spanned_pages);
1405 VMCOREINFO_OFFSET(free_area, free_list);
1406 VMCOREINFO_OFFSET(list_head, next);
1407 VMCOREINFO_OFFSET(list_head, prev);
1408 VMCOREINFO_LENGTH(zone.free_area, MAX_ORDER);
1409 VMCOREINFO_LENGTH(free_area.free_list, MIGRATE_TYPES);
1410 VMCOREINFO_NUMBER(NR_FREE_PAGES);
1411 VMCOREINFO_NUMBER(PG_lru);
1412 VMCOREINFO_NUMBER(PG_private);
1413 VMCOREINFO_NUMBER(PG_swapcache);
1415 arch_crash_save_vmcoreinfo();
1417 return 0;
1420 module_init(crash_save_vmcoreinfo_init)
1423 * Move into place and start executing a preloaded standalone
1424 * executable. If nothing was preloaded return an error.
1426 int kernel_kexec(void)
1428 int error = 0;
1430 if (!mutex_trylock(&kexec_mutex))
1431 return -EBUSY;
1432 if (!kexec_image) {
1433 error = -EINVAL;
1434 goto Unlock;
1437 #ifdef CONFIG_KEXEC_JUMP
1438 if (kexec_image->preserve_context) {
1439 mutex_lock(&pm_mutex);
1440 pm_prepare_console();
1441 error = freeze_processes();
1442 if (error) {
1443 error = -EBUSY;
1444 goto Restore_console;
1446 suspend_console();
1447 error = device_suspend(PMSG_FREEZE);
1448 if (error)
1449 goto Resume_console;
1450 error = disable_nonboot_cpus();
1451 if (error)
1452 goto Resume_devices;
1453 device_pm_lock();
1454 local_irq_disable();
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);
1463 if (error)
1464 goto Enable_irqs;
1465 } else
1466 #endif
1468 kernel_restart_prepare(NULL);
1469 printk(KERN_EMERG "Starting new kernel\n");
1470 machine_shutdown();
1473 machine_kexec(kexec_image);
1475 #ifdef CONFIG_KEXEC_JUMP
1476 if (kexec_image->preserve_context) {
1477 device_power_up(PMSG_RESTORE);
1478 Enable_irqs:
1479 local_irq_enable();
1480 device_pm_unlock();
1481 enable_nonboot_cpus();
1482 Resume_devices:
1483 device_resume(PMSG_RESTORE);
1484 Resume_console:
1485 resume_console();
1486 thaw_processes();
1487 Restore_console:
1488 pm_restore_console();
1489 mutex_unlock(&pm_mutex);
1491 #endif
1493 Unlock:
1494 mutex_unlock(&kexec_mutex);
1495 return error;