ia64/linux-2.6.18-xen.hg

view kernel/kexec.c @ 912:dd42cdb0ab89

[IA64] Build blktap2 driver by default in x86 builds.

add CONFIG_XEN_BLKDEV_TAP2=y to buildconfigs/linux-defconfig_xen_ia64.

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
author Isaku Yamahata <yamahata@valinux.co.jp>
date Mon Jun 29 12:09:16 2009 +0900 (2009-06-29)
parents 6bb7f500d5e4
children
line source
1 /*
2 * kexec.c - kexec system call
3 * Copyright (C) 2002-2004 Eric Biederman <ebiederm@xmission.com>
4 *
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/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/syscalls.h>
21 #include <linux/ioport.h>
22 #include <linux/hardirq.h>
24 #include <asm/page.h>
25 #include <asm/uaccess.h>
26 #include <asm/io.h>
27 #include <asm/system.h>
28 #include <asm/semaphore.h>
30 /* Per cpu memory for storing cpu states in case of system crash. */
31 note_buf_t* crash_notes;
33 /* Location of the reserved area for the crash kernel */
34 struct resource crashk_res = {
35 .name = "Crash kernel",
36 .start = 0,
37 .end = 0,
38 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
39 };
41 int kexec_should_crash(struct task_struct *p)
42 {
43 if (in_interrupt() || !p->pid || p->pid == 1 || panic_on_oops)
44 return 1;
45 return 0;
46 }
48 /*
49 * When kexec transitions to the new kernel there is a one-to-one
50 * mapping between physical and virtual addresses. On processors
51 * where you can disable the MMU this is trivial, and easy. For
52 * others it is still a simple predictable page table to setup.
53 *
54 * In that environment kexec copies the new kernel to its final
55 * resting place. This means I can only support memory whose
56 * physical address can fit in an unsigned long. In particular
57 * addresses where (pfn << PAGE_SHIFT) > ULONG_MAX cannot be handled.
58 * If the assembly stub has more restrictive requirements
59 * KEXEC_SOURCE_MEMORY_LIMIT and KEXEC_DEST_MEMORY_LIMIT can be
60 * defined more restrictively in <asm/kexec.h>.
61 *
62 * The code for the transition from the current kernel to the
63 * the new kernel is placed in the control_code_buffer, whose size
64 * is given by KEXEC_CONTROL_CODE_SIZE. In the best case only a single
65 * page of memory is necessary, but some architectures require more.
66 * Because this memory must be identity mapped in the transition from
67 * virtual to physical addresses it must live in the range
68 * 0 - TASK_SIZE, as only the user space mappings are arbitrarily
69 * modifiable.
70 *
71 * The assembly stub in the control code buffer is passed a linked list
72 * of descriptor pages detailing the source pages of the new kernel,
73 * and the destination addresses of those source pages. As this data
74 * structure is not used in the context of the current OS, it must
75 * be self-contained.
76 *
77 * The code has been made to work with highmem pages and will use a
78 * destination page in its final resting place (if it happens
79 * to allocate it). The end product of this is that most of the
80 * physical address space, and most of RAM can be used.
81 *
82 * Future directions include:
83 * - allocating a page table with the control code buffer identity
84 * mapped, to simplify machine_kexec and make kexec_on_panic more
85 * reliable.
86 */
88 /*
89 * KIMAGE_NO_DEST is an impossible destination address..., for
90 * allocating pages whose destination address we do not care about.
91 */
92 #define KIMAGE_NO_DEST (-1UL)
94 static int kimage_is_destination_range(struct kimage *image,
95 unsigned long start, unsigned long end);
96 static struct page *kimage_alloc_page(struct kimage *image,
97 gfp_t gfp_mask,
98 unsigned long dest);
100 static int do_kimage_alloc(struct kimage **rimage, unsigned long entry,
101 unsigned long nr_segments,
102 struct kexec_segment __user *segments)
103 {
104 size_t segment_bytes;
105 struct kimage *image;
106 unsigned long i;
107 int result;
109 /* Allocate a controlling structure */
110 result = -ENOMEM;
111 image = kmalloc(sizeof(*image), GFP_KERNEL);
112 if (!image)
113 goto out;
115 memset(image, 0, sizeof(*image));
116 image->head = 0;
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);
136 if (result)
137 goto out;
139 /*
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.
144 *
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
150 * granularity.
151 */
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))
159 goto out;
160 if (mend >= KEXEC_DESTINATION_MEMORY_LIMIT)
161 goto out;
162 }
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.
168 */
169 result = -EINVAL;
170 for (i = 0; i < nr_segments; i++) {
171 unsigned long mstart, mend;
172 unsigned long j;
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))
182 goto out;
183 }
184 }
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
189 * later on.
190 */
191 result = -EINVAL;
192 for (i = 0; i < nr_segments; i++) {
193 if (image->segment[i].bufsz > image->segment[i].memsz)
194 goto out;
195 }
197 result = 0;
198 out:
199 if (result == 0)
200 *rimage = image;
201 else
202 kfree(image);
204 return result;
206 }
208 static int kimage_normal_alloc(struct kimage **rimage, unsigned long entry,
209 unsigned long nr_segments,
210 struct kexec_segment __user *segments)
211 {
212 int result;
213 struct kimage *image;
215 /* Allocate and initialize a controlling structure */
216 image = NULL;
217 result = do_kimage_alloc(&image, entry, nr_segments, segments);
218 if (result)
219 goto out;
221 *rimage = image;
223 /*
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.
227 */
228 result = -ENOMEM;
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");
233 goto out;
234 }
236 result = 0;
237 out:
238 if (result == 0)
239 *rimage = image;
240 else
241 kfree(image);
243 return result;
244 }
246 static int kimage_crash_alloc(struct kimage **rimage, unsigned long entry,
247 unsigned long nr_segments,
248 struct kexec_segment __user *segments)
249 {
250 int result;
251 struct kimage *image;
252 unsigned long i;
254 image = NULL;
255 /* Verify we have a valid entry point */
256 if ((entry < crashk_res.start) || (entry > crashk_res.end)) {
257 result = -EADDRNOTAVAIL;
258 goto out;
259 }
261 /* Allocate and initialize a controlling structure */
262 result = do_kimage_alloc(&image, entry, nr_segments, segments);
263 if (result)
264 goto out;
266 /* Enable the special crash kernel control page
267 * allocation policy.
268 */
269 image->control_page = crashk_res.start;
270 image->type = KEXEC_TYPE_CRASH;
272 /*
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.
280 */
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))
289 goto out;
290 }
292 /*
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.
296 */
297 result = -ENOMEM;
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");
302 goto out;
303 }
305 result = 0;
306 out:
307 if (result == 0)
308 *rimage = image;
309 else
310 kfree(image);
312 return result;
313 }
315 static int kimage_is_destination_range(struct kimage *image,
316 unsigned long start,
317 unsigned long end)
318 {
319 unsigned long i;
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))
327 return 1;
328 }
330 return 0;
331 }
333 static struct page *kimage_alloc_pages(gfp_t gfp_mask, unsigned int order, unsigned long limit)
334 {
335 struct page *pages;
337 pages = alloc_pages(gfp_mask, order);
338 if (pages) {
339 unsigned int count, i;
340 #ifdef CONFIG_XEN
341 int address_bits;
343 if (limit == ~0UL)
344 address_bits = BITS_PER_LONG;
345 else
346 address_bits = long_log2(limit);
348 if (xen_limit_pages_to_max_mfn(pages, order, address_bits) < 0) {
349 __free_pages(pages, order);
350 return NULL;
351 }
352 #endif
353 pages->mapping = NULL;
354 set_page_private(pages, order);
355 count = 1 << order;
356 for (i = 0; i < count; i++)
357 SetPageReserved(pages + i);
358 }
360 return pages;
361 }
363 static void kimage_free_pages(struct page *page)
364 {
365 unsigned int order, count, i;
367 order = page_private(page);
368 count = 1 << order;
369 for (i = 0; i < count; i++)
370 ClearPageReserved(page + i);
371 __free_pages(page, order);
372 }
374 static void kimage_free_page_list(struct list_head *list)
375 {
376 struct list_head *pos, *next;
378 list_for_each_safe(pos, next, list) {
379 struct page *page;
381 page = list_entry(pos, struct page, lru);
382 list_del(&page->lru);
383 kimage_free_pages(page);
384 }
385 }
387 static struct page *kimage_alloc_normal_control_pages(struct kimage *image,
388 unsigned int order)
389 {
390 /* Control pages are special, they are the intermediaries
391 * that are needed while we copy the rest of the pages
392 * to their final resting place. As such they must
393 * not conflict with either the destination addresses
394 * or memory the kernel is already using.
395 *
396 * The only case where we really need more than one of
397 * these are for architectures where we cannot disable
398 * the MMU and must instead generate an identity mapped
399 * page table for all of the memory.
400 *
401 * At worst this runs in O(N) of the image size.
402 */
403 struct list_head extra_pages;
404 struct page *pages;
405 unsigned int count;
407 count = 1 << order;
408 INIT_LIST_HEAD(&extra_pages);
410 /* Loop while I can allocate a page and the page allocated
411 * is a destination page.
412 */
413 do {
414 unsigned long pfn, epfn, addr, eaddr;
416 pages = kimage_alloc_pages(GFP_KERNEL, order, KEXEC_CONTROL_MEMORY_LIMIT);
417 if (!pages)
418 break;
419 pfn = kexec_page_to_pfn(pages);
420 epfn = pfn + count;
421 addr = pfn << PAGE_SHIFT;
422 eaddr = epfn << PAGE_SHIFT;
423 if ((epfn >= (KEXEC_CONTROL_MEMORY_LIMIT >> PAGE_SHIFT)) ||
424 kimage_is_destination_range(image, addr, eaddr)) {
425 list_add(&pages->lru, &extra_pages);
426 pages = NULL;
427 }
428 } while (!pages);
430 if (pages) {
431 /* Remember the allocated page... */
432 list_add(&pages->lru, &image->control_pages);
434 /* Because the page is already in it's destination
435 * location we will never allocate another page at
436 * that address. Therefore kimage_alloc_pages
437 * will not return it (again) and we don't need
438 * to give it an entry in image->segment[].
439 */
440 }
441 /* Deal with the destination pages I have inadvertently allocated.
442 *
443 * Ideally I would convert multi-page allocations into single
444 * page allocations, and add everyting to image->dest_pages.
445 *
446 * For now it is simpler to just free the pages.
447 */
448 kimage_free_page_list(&extra_pages);
450 return pages;
451 }
453 #ifndef CONFIG_XEN
454 static struct page *kimage_alloc_crash_control_pages(struct kimage *image,
455 unsigned int order)
456 {
457 /* Control pages are special, they are the intermediaries
458 * that are needed while we copy the rest of the pages
459 * to their final resting place. As such they must
460 * not conflict with either the destination addresses
461 * or memory the kernel is already using.
462 *
463 * Control pages are also the only pags we must allocate
464 * when loading a crash kernel. All of the other pages
465 * are specified by the segments and we just memcpy
466 * into them directly.
467 *
468 * The only case where we really need more than one of
469 * these are for architectures where we cannot disable
470 * the MMU and must instead generate an identity mapped
471 * page table for all of the memory.
472 *
473 * Given the low demand this implements a very simple
474 * allocator that finds the first hole of the appropriate
475 * size in the reserved memory region, and allocates all
476 * of the memory up to and including the hole.
477 */
478 unsigned long hole_start, hole_end, size;
479 struct page *pages;
481 pages = NULL;
482 size = (1 << order) << PAGE_SHIFT;
483 hole_start = (image->control_page + (size - 1)) & ~(size - 1);
484 hole_end = hole_start + size - 1;
485 while (hole_end <= crashk_res.end) {
486 unsigned long i;
488 if (hole_end > KEXEC_CONTROL_MEMORY_LIMIT)
489 break;
490 if (hole_end > crashk_res.end)
491 break;
492 /* See if I overlap any of the segments */
493 for (i = 0; i < image->nr_segments; i++) {
494 unsigned long mstart, mend;
496 mstart = image->segment[i].mem;
497 mend = mstart + image->segment[i].memsz - 1;
498 if ((hole_end >= mstart) && (hole_start <= mend)) {
499 /* Advance the hole to the end of the segment */
500 hole_start = (mend + (size - 1)) & ~(size - 1);
501 hole_end = hole_start + size - 1;
502 break;
503 }
504 }
505 /* If I don't overlap any segments I have found my hole! */
506 if (i == image->nr_segments) {
507 pages = kexec_pfn_to_page(hole_start >> PAGE_SHIFT);
508 break;
509 }
510 }
511 if (pages)
512 image->control_page = hole_end;
514 return pages;
515 }
518 struct page *kimage_alloc_control_pages(struct kimage *image,
519 unsigned int order)
520 {
521 struct page *pages = NULL;
523 switch (image->type) {
524 case KEXEC_TYPE_DEFAULT:
525 pages = kimage_alloc_normal_control_pages(image, order);
526 break;
527 case KEXEC_TYPE_CRASH:
528 pages = kimage_alloc_crash_control_pages(image, order);
529 break;
530 }
532 return pages;
533 }
534 #else /* !CONFIG_XEN */
535 struct page *kimage_alloc_control_pages(struct kimage *image,
536 unsigned int order)
537 {
538 return kimage_alloc_normal_control_pages(image, order);
539 }
540 #endif
542 static int kimage_add_entry(struct kimage *image, kimage_entry_t entry)
543 {
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 = kexec_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);
560 }
561 *image->entry = entry;
562 image->entry++;
563 *image->entry = 0;
565 return 0;
566 }
568 static int kimage_set_destination(struct kimage *image,
569 unsigned long destination)
570 {
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;
579 }
582 static int kimage_add_page(struct kimage *image, unsigned long page)
583 {
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;
592 }
595 static void kimage_free_extra_pages(struct kimage *image)
596 {
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);
603 }
604 static int kimage_terminate(struct kimage *image)
605 {
606 if (*image->entry != 0)
607 image->entry++;
609 *image->entry = IND_DONE;
611 return 0;
612 }
614 #define for_each_kimage_entry(image, ptr, entry) \
615 for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE); \
616 ptr = (entry & IND_INDIRECTION)? \
617 kexec_phys_to_virt((entry & PAGE_MASK)): ptr +1)
619 static void kimage_free_entry(kimage_entry_t entry)
620 {
621 struct page *page;
623 page = kexec_pfn_to_page(entry >> PAGE_SHIFT);
624 kimage_free_pages(page);
625 }
627 static void kimage_free(struct kimage *image)
628 {
629 kimage_entry_t *ptr, entry;
630 kimage_entry_t ind = 0;
632 if (!image)
633 return;
635 #ifdef CONFIG_XEN
636 xen_machine_kexec_unload(image);
637 #endif
639 kimage_free_extra_pages(image);
640 for_each_kimage_entry(image, ptr, entry) {
641 if (entry & IND_INDIRECTION) {
642 /* Free the previous indirection page */
643 if (ind & IND_INDIRECTION)
644 kimage_free_entry(ind);
645 /* Save this indirection page until we are
646 * done with it.
647 */
648 ind = entry;
649 }
650 else if (entry & IND_SOURCE)
651 kimage_free_entry(entry);
652 }
653 /* Free the final indirection page */
654 if (ind & IND_INDIRECTION)
655 kimage_free_entry(ind);
657 /* Handle any machine specific cleanup */
658 machine_kexec_cleanup(image);
660 /* Free the kexec control pages... */
661 kimage_free_page_list(&image->control_pages);
662 kfree(image);
663 }
665 static kimage_entry_t *kimage_dst_used(struct kimage *image,
666 unsigned long page)
667 {
668 kimage_entry_t *ptr, entry;
669 unsigned long destination = 0;
671 for_each_kimage_entry(image, ptr, entry) {
672 if (entry & IND_DESTINATION)
673 destination = entry & PAGE_MASK;
674 else if (entry & IND_SOURCE) {
675 if (page == destination)
676 return ptr;
677 destination += PAGE_SIZE;
678 }
679 }
681 return NULL;
682 }
684 static struct page *kimage_alloc_page(struct kimage *image,
685 gfp_t gfp_mask,
686 unsigned long destination)
687 {
688 /*
689 * Here we implement safeguards to ensure that a source page
690 * is not copied to its destination page before the data on
691 * the destination page is no longer useful.
692 *
693 * To do this we maintain the invariant that a source page is
694 * either its own destination page, or it is not a
695 * destination page at all.
696 *
697 * That is slightly stronger than required, but the proof
698 * that no problems will not occur is trivial, and the
699 * implementation is simply to verify.
700 *
701 * When allocating all pages normally this algorithm will run
702 * in O(N) time, but in the worst case it will run in O(N^2)
703 * time. If the runtime is a problem the data structures can
704 * be fixed.
705 */
706 struct page *page;
707 unsigned long addr;
709 /*
710 * Walk through the list of destination pages, and see if I
711 * have a match.
712 */
713 list_for_each_entry(page, &image->dest_pages, lru) {
714 addr = kexec_page_to_pfn(page) << PAGE_SHIFT;
715 if (addr == destination) {
716 list_del(&page->lru);
717 return page;
718 }
719 }
720 page = NULL;
721 while (1) {
722 kimage_entry_t *old;
724 /* Allocate a page, if we run out of memory give up */
725 page = kimage_alloc_pages(gfp_mask, 0, KEXEC_SOURCE_MEMORY_LIMIT);
726 if (!page)
727 return NULL;
728 /* If the page cannot be used file it away */
729 if (kexec_page_to_pfn(page) >
730 (KEXEC_SOURCE_MEMORY_LIMIT >> PAGE_SHIFT)) {
731 list_add(&page->lru, &image->unuseable_pages);
732 continue;
733 }
734 addr = kexec_page_to_pfn(page) << PAGE_SHIFT;
736 /* If it is the destination page we want use it */
737 if (addr == destination)
738 break;
740 /* If the page is not a destination page use it */
741 if (!kimage_is_destination_range(image, addr,
742 addr + PAGE_SIZE))
743 break;
745 /*
746 * I know that the page is someones destination page.
747 * See if there is already a source page for this
748 * destination page. And if so swap the source pages.
749 */
750 old = kimage_dst_used(image, addr);
751 if (old) {
752 /* If so move it */
753 unsigned long old_addr;
754 struct page *old_page;
756 old_addr = *old & PAGE_MASK;
757 old_page = kexec_pfn_to_page(old_addr >> PAGE_SHIFT);
758 copy_highpage(page, old_page);
759 *old = addr | (*old & ~PAGE_MASK);
761 /* The old page I have found cannot be a
762 * destination page, so return it.
763 */
764 addr = old_addr;
765 page = old_page;
766 break;
767 }
768 else {
769 /* Place the page on the destination list I
770 * will use it later.
771 */
772 list_add(&page->lru, &image->dest_pages);
773 }
774 }
776 return page;
777 }
779 static int kimage_load_normal_segment(struct kimage *image,
780 struct kexec_segment *segment)
781 {
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 == 0) {
804 result = -ENOMEM;
805 goto out;
806 }
807 result = kimage_add_page(image, kexec_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;
829 }
830 ubytes -= uchunk;
831 maddr += mchunk;
832 buf += mchunk;
833 mbytes -= mchunk;
834 }
835 out:
836 return result;
837 }
839 #ifndef CONFIG_XEN
840 static int kimage_load_crash_segment(struct kimage *image,
841 struct kexec_segment *segment)
842 {
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.
846 */
847 unsigned long maddr;
848 unsigned long ubytes, mbytes;
849 int result;
850 unsigned char __user *buf;
852 result = 0;
853 buf = segment->buf;
854 ubytes = segment->bufsz;
855 mbytes = segment->memsz;
856 maddr = segment->mem;
857 while (mbytes) {
858 struct page *page;
859 char *ptr;
860 size_t uchunk, mchunk;
862 page = kexec_pfn_to_page(maddr >> PAGE_SHIFT);
863 if (page == 0) {
864 result = -ENOMEM;
865 goto out;
866 }
867 ptr = kmap(page);
868 ptr += maddr & ~PAGE_MASK;
869 mchunk = PAGE_SIZE - (maddr & ~PAGE_MASK);
870 if (mchunk > mbytes)
871 mchunk = mbytes;
873 uchunk = mchunk;
874 if (uchunk > ubytes) {
875 uchunk = ubytes;
876 /* Zero the trailing part of the page */
877 memset(ptr + uchunk, 0, mchunk - uchunk);
878 }
879 result = copy_from_user(ptr, buf, uchunk);
880 kexec_flush_icache_page(page);
881 kunmap(page);
882 if (result) {
883 result = (result < 0) ? result : -EIO;
884 goto out;
885 }
886 ubytes -= uchunk;
887 maddr += mchunk;
888 buf += mchunk;
889 mbytes -= mchunk;
890 }
891 out:
892 return result;
893 }
895 static int kimage_load_segment(struct kimage *image,
896 struct kexec_segment *segment)
897 {
898 int result = -ENOMEM;
900 switch (image->type) {
901 case KEXEC_TYPE_DEFAULT:
902 result = kimage_load_normal_segment(image, segment);
903 break;
904 case KEXEC_TYPE_CRASH:
905 result = kimage_load_crash_segment(image, segment);
906 break;
907 }
909 return result;
910 }
911 #else /* CONFIG_XEN */
912 static int kimage_load_segment(struct kimage *image,
913 struct kexec_segment *segment)
914 {
915 return kimage_load_normal_segment(image, segment);
916 }
917 #endif
919 /*
920 * Exec Kernel system call: for obvious reasons only root may call it.
921 *
922 * This call breaks up into three pieces.
923 * - A generic part which loads the new kernel from the current
924 * address space, and very carefully places the data in the
925 * allocated pages.
926 *
927 * - A generic part that interacts with the kernel and tells all of
928 * the devices to shut down. Preventing on-going dmas, and placing
929 * the devices in a consistent state so a later kernel can
930 * reinitialize them.
931 *
932 * - A machine specific part that includes the syscall number
933 * and the copies the image to it's final destination. And
934 * jumps into the image at entry.
935 *
936 * kexec does not sync, or unmount filesystems so if you need
937 * that to happen you need to do that yourself.
938 */
939 struct kimage *kexec_image;
940 struct kimage *kexec_crash_image;
941 /*
942 * A home grown binary mutex.
943 * Nothing can wait so this mutex is safe to use
944 * in interrupt context :)
945 */
946 static int kexec_lock;
948 asmlinkage long sys_kexec_load(unsigned long entry, unsigned long nr_segments,
949 struct kexec_segment __user *segments,
950 unsigned long flags)
951 {
952 struct kimage **dest_image, *image;
953 int locked;
954 int result;
956 /* We only trust the superuser with rebooting the system. */
957 if (!capable(CAP_SYS_BOOT))
958 return -EPERM;
960 /*
961 * Verify we have a legal set of flags
962 * This leaves us room for future extensions.
963 */
964 if ((flags & KEXEC_FLAGS) != (flags & ~KEXEC_ARCH_MASK))
965 return -EINVAL;
967 /* Verify we are on the appropriate architecture */
968 if (((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH) &&
969 ((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH_DEFAULT))
970 return -EINVAL;
972 /* Put an artificial cap on the number
973 * of segments passed to kexec_load.
974 */
975 if (nr_segments > KEXEC_SEGMENT_MAX)
976 return -EINVAL;
978 image = NULL;
979 result = 0;
981 /* Because we write directly to the reserved memory
982 * region when loading crash kernels we need a mutex here to
983 * prevent multiple crash kernels from attempting to load
984 * simultaneously, and to prevent a crash kernel from loading
985 * over the top of a in use crash kernel.
986 *
987 * KISS: always take the mutex.
988 */
989 locked = xchg(&kexec_lock, 1);
990 if (locked)
991 return -EBUSY;
993 dest_image = &kexec_image;
994 if (flags & KEXEC_ON_CRASH)
995 dest_image = &kexec_crash_image;
996 if (nr_segments > 0) {
997 unsigned long i;
999 /* Loading another kernel to reboot into */
1000 if ((flags & KEXEC_ON_CRASH) == 0)
1001 result = kimage_normal_alloc(&image, entry,
1002 nr_segments, segments);
1003 /* Loading another kernel to switch to if this one crashes */
1004 else if (flags & KEXEC_ON_CRASH) {
1005 /* Free any current crash dump kernel before
1006 * we corrupt it.
1007 */
1008 kimage_free(xchg(&kexec_crash_image, NULL));
1009 result = kimage_crash_alloc(&image, entry,
1010 nr_segments, segments);
1012 if (result)
1013 goto out;
1015 result = machine_kexec_prepare(image);
1016 if (result)
1017 goto out;
1019 for (i = 0; i < nr_segments; i++) {
1020 result = kimage_load_segment(image, &image->segment[i]);
1021 if (result)
1022 goto out;
1024 result = kimage_terminate(image);
1025 if (result)
1026 goto out;
1028 #ifdef CONFIG_XEN
1029 if (image) {
1030 result = xen_machine_kexec_load(image);
1031 if (result)
1032 goto out;
1034 #endif
1035 /* Install the new kernel, and Uninstall the old */
1036 image = xchg(dest_image, image);
1038 out:
1039 xchg(&kexec_lock, 0); /* Release the mutex */
1040 kimage_free(image);
1042 return result;
1045 #ifdef CONFIG_COMPAT
1046 asmlinkage long compat_sys_kexec_load(unsigned long entry,
1047 unsigned long nr_segments,
1048 struct compat_kexec_segment __user *segments,
1049 unsigned long flags)
1051 struct compat_kexec_segment in;
1052 struct kexec_segment out, __user *ksegments;
1053 unsigned long i, result;
1055 /* Don't allow clients that don't understand the native
1056 * architecture to do anything.
1057 */
1058 if ((flags & KEXEC_ARCH_MASK) == KEXEC_ARCH_DEFAULT)
1059 return -EINVAL;
1061 if (nr_segments > KEXEC_SEGMENT_MAX)
1062 return -EINVAL;
1064 ksegments = compat_alloc_user_space(nr_segments * sizeof(out));
1065 for (i=0; i < nr_segments; i++) {
1066 result = copy_from_user(&in, &segments[i], sizeof(in));
1067 if (result)
1068 return -EFAULT;
1070 out.buf = compat_ptr(in.buf);
1071 out.bufsz = in.bufsz;
1072 out.mem = in.mem;
1073 out.memsz = in.memsz;
1075 result = copy_to_user(&ksegments[i], &out, sizeof(out));
1076 if (result)
1077 return -EFAULT;
1080 return sys_kexec_load(entry, nr_segments, ksegments, flags);
1082 #endif
1084 void crash_kexec(struct pt_regs *regs)
1086 int locked;
1088 /* Take the kexec_lock here to prevent sys_kexec_load
1089 * running on one cpu from replacing the crash kernel
1090 * we are using after a panic on a different cpu.
1092 * If the crash kernel was not located in a fixed area
1093 * of memory the xchg(&kexec_crash_image) would be
1094 * sufficient. But since I reuse the memory...
1095 */
1096 locked = xchg(&kexec_lock, 1);
1097 if (!locked) {
1098 if (kexec_crash_image) {
1099 struct pt_regs fixed_regs;
1100 crash_setup_regs(&fixed_regs, regs);
1101 machine_crash_shutdown(&fixed_regs);
1102 machine_kexec(kexec_crash_image);
1104 xchg(&kexec_lock, 0);
1108 static int __init crash_notes_memory_init(void)
1110 /* Allocate memory for saving cpu registers. */
1111 crash_notes = alloc_percpu(note_buf_t);
1112 if (!crash_notes) {
1113 printk("Kexec: Memory allocation for saving cpu register"
1114 " states failed\n");
1115 return -ENOMEM;
1117 return 0;
1119 module_init(crash_notes_memory_init)