Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.git] / kernel / kexec.c
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  */
8
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 <generated/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>
33 #include <linux/vmalloc.h>
34 #include <linux/swap.h>
35 #include <linux/syscore_ops.h>
36
37 #include <asm/page.h>
38 #include <asm/uaccess.h>
39 #include <asm/io.h>
40 #include <asm/sections.h>
41
42 #ifndef CONFIG_XEN
43 /* Per cpu memory for storing cpu states in case of system crash. */
44 note_buf_t __percpu *crash_notes;
45 #endif
46
47 /* vmcoreinfo stuff */
48 static unsigned char vmcoreinfo_data[VMCOREINFO_BYTES];
49 u32
50 #if defined(CONFIG_XEN) && defined(CONFIG_X86)
51 __page_aligned_bss
52 #endif
53 vmcoreinfo_note[VMCOREINFO_NOTE_SIZE/4];
54 size_t vmcoreinfo_size;
55 size_t vmcoreinfo_max_size = sizeof(vmcoreinfo_data);
56
57 /* Location of the reserved area for the crash kernel */
58 struct resource crashk_res = {
59         .name  = "Crash kernel",
60         .start = 0,
61         .end   = 0,
62         .flags = IORESOURCE_BUSY | IORESOURCE_MEM
63 };
64
65 int kexec_should_crash(struct task_struct *p)
66 {
67         if (in_interrupt() || !p->pid || is_global_init(p) || panic_on_oops)
68                 return 1;
69         return 0;
70 }
71
72 /*
73  * When kexec transitions to the new kernel there is a one-to-one
74  * mapping between physical and virtual addresses.  On processors
75  * where you can disable the MMU this is trivial, and easy.  For
76  * others it is still a simple predictable page table to setup.
77  *
78  * In that environment kexec copies the new kernel to its final
79  * resting place.  This means I can only support memory whose
80  * physical address can fit in an unsigned long.  In particular
81  * addresses where (pfn << PAGE_SHIFT) > ULONG_MAX cannot be handled.
82  * If the assembly stub has more restrictive requirements
83  * KEXEC_SOURCE_MEMORY_LIMIT and KEXEC_DEST_MEMORY_LIMIT can be
84  * defined more restrictively in <asm/kexec.h>.
85  *
86  * The code for the transition from the current kernel to the
87  * the new kernel is placed in the control_code_buffer, whose size
88  * is given by KEXEC_CONTROL_PAGE_SIZE.  In the best case only a single
89  * page of memory is necessary, but some architectures require more.
90  * Because this memory must be identity mapped in the transition from
91  * virtual to physical addresses it must live in the range
92  * 0 - TASK_SIZE, as only the user space mappings are arbitrarily
93  * modifiable.
94  *
95  * The assembly stub in the control code buffer is passed a linked list
96  * of descriptor pages detailing the source pages of the new kernel,
97  * and the destination addresses of those source pages.  As this data
98  * structure is not used in the context of the current OS, it must
99  * be self-contained.
100  *
101  * The code has been made to work with highmem pages and will use a
102  * destination page in its final resting place (if it happens
103  * to allocate it).  The end product of this is that most of the
104  * physical address space, and most of RAM can be used.
105  *
106  * Future directions include:
107  *  - allocating a page table with the control code buffer identity
108  *    mapped, to simplify machine_kexec and make kexec_on_panic more
109  *    reliable.
110  */
111
112 /*
113  * KIMAGE_NO_DEST is an impossible destination address..., for
114  * allocating pages whose destination address we do not care about.
115  */
116 #define KIMAGE_NO_DEST (-1UL)
117
118 static int kimage_is_destination_range(struct kimage *image,
119                                        unsigned long start, unsigned long end);
120 static struct page *kimage_alloc_page(struct kimage *image,
121                                        gfp_t gfp_mask,
122                                        unsigned long dest);
123
124 static int do_kimage_alloc(struct kimage **rimage, unsigned long entry,
125                             unsigned long nr_segments,
126                             struct kexec_segment __user *segments)
127 {
128         size_t segment_bytes;
129         struct kimage *image;
130         unsigned long i;
131         int result;
132
133         /* Allocate a controlling structure */
134         result = -ENOMEM;
135         image = kzalloc(sizeof(*image), GFP_KERNEL);
136         if (!image)
137                 goto out;
138
139         image->head = 0;
140         image->entry = &image->head;
141         image->last_entry = &image->head;
142         image->control_page = ~0; /* By default this does not apply */
143         image->start = entry;
144         image->type = KEXEC_TYPE_DEFAULT;
145
146         /* Initialize the list of control pages */
147         INIT_LIST_HEAD(&image->control_pages);
148
149         /* Initialize the list of destination pages */
150         INIT_LIST_HEAD(&image->dest_pages);
151
152         /* Initialize the list of unusable pages */
153         INIT_LIST_HEAD(&image->unuseable_pages);
154
155         /* Read in the segments */
156         image->nr_segments = nr_segments;
157         segment_bytes = nr_segments * sizeof(*segments);
158         result = copy_from_user(image->segment, segments, segment_bytes);
159         if (result) {
160                 result = -EFAULT;
161                 goto out;
162         }
163
164         /*
165          * Verify we have good destination addresses.  The caller is
166          * responsible for making certain we don't attempt to load
167          * the new image into invalid or reserved areas of RAM.  This
168          * just verifies it is an address we can use.
169          *
170          * Since the kernel does everything in page size chunks ensure
171          * the destination addresses are page aligned.  Too many
172          * special cases crop of when we don't do this.  The most
173          * insidious is getting overlapping destination addresses
174          * simply because addresses are changed to page size
175          * granularity.
176          */
177         result = -EADDRNOTAVAIL;
178         for (i = 0; i < nr_segments; i++) {
179                 unsigned long mstart, mend;
180
181                 mstart = image->segment[i].mem;
182                 mend   = mstart + image->segment[i].memsz;
183                 if ((mstart & ~PAGE_MASK) || (mend & ~PAGE_MASK))
184                         goto out;
185                 if (mend >= KEXEC_DESTINATION_MEMORY_LIMIT)
186                         goto out;
187         }
188
189         /* Verify our destination addresses do not overlap.
190          * If we alloed overlapping destination addresses
191          * through very weird things can happen with no
192          * easy explanation as one segment stops on another.
193          */
194         result = -EINVAL;
195         for (i = 0; i < nr_segments; i++) {
196                 unsigned long mstart, mend;
197                 unsigned long j;
198
199                 mstart = image->segment[i].mem;
200                 mend   = mstart + image->segment[i].memsz;
201                 for (j = 0; j < i; j++) {
202                         unsigned long pstart, pend;
203                         pstart = image->segment[j].mem;
204                         pend   = pstart + image->segment[j].memsz;
205                         /* Do the segments overlap ? */
206                         if ((mend > pstart) && (mstart < pend))
207                                 goto out;
208                 }
209         }
210
211         /* Ensure our buffer sizes are strictly less than
212          * our memory sizes.  This should always be the case,
213          * and it is easier to check up front than to be surprised
214          * later on.
215          */
216         result = -EINVAL;
217         for (i = 0; i < nr_segments; i++) {
218                 if (image->segment[i].bufsz > image->segment[i].memsz)
219                         goto out;
220         }
221
222         result = 0;
223 out:
224         if (result == 0)
225                 *rimage = image;
226         else
227                 kfree(image);
228
229         return result;
230
231 }
232
233 static int kimage_normal_alloc(struct kimage **rimage, unsigned long entry,
234                                 unsigned long nr_segments,
235                                 struct kexec_segment __user *segments)
236 {
237         int result;
238         struct kimage *image;
239
240         /* Allocate and initialize a controlling structure */
241         image = NULL;
242         result = do_kimage_alloc(&image, entry, nr_segments, segments);
243         if (result)
244                 goto out;
245
246         *rimage = image;
247
248         /*
249          * Find a location for the control code buffer, and add it
250          * the vector of segments so that it's pages will also be
251          * counted as destination pages.
252          */
253         result = -ENOMEM;
254         image->control_code_page = kimage_alloc_control_pages(image,
255                                            get_order(KEXEC_CONTROL_PAGE_SIZE));
256         if (!image->control_code_page) {
257                 printk(KERN_ERR "Could not allocate control_code_buffer\n");
258                 goto out;
259         }
260
261         image->swap_page = kimage_alloc_control_pages(image, 0);
262         if (!image->swap_page) {
263                 printk(KERN_ERR "Could not allocate swap buffer\n");
264                 goto out;
265         }
266
267         result = 0;
268  out:
269         if (result == 0)
270                 *rimage = image;
271         else
272                 kfree(image);
273
274         return result;
275 }
276
277 static int kimage_crash_alloc(struct kimage **rimage, unsigned long entry,
278                                 unsigned long nr_segments,
279                                 struct kexec_segment __user *segments)
280 {
281         int result;
282         struct kimage *image;
283         unsigned long i;
284
285         image = NULL;
286         /* Verify we have a valid entry point */
287         if ((entry < crashk_res.start) || (entry > crashk_res.end)) {
288                 result = -EADDRNOTAVAIL;
289                 goto out;
290         }
291
292         /* Allocate and initialize a controlling structure */
293         result = do_kimage_alloc(&image, entry, nr_segments, segments);
294         if (result)
295                 goto out;
296
297         /* Enable the special crash kernel control page
298          * allocation policy.
299          */
300         image->control_page = crashk_res.start;
301         image->type = KEXEC_TYPE_CRASH;
302
303         /*
304          * Verify we have good destination addresses.  Normally
305          * the caller is responsible for making certain we don't
306          * attempt to load the new image into invalid or reserved
307          * areas of RAM.  But crash kernels are preloaded into a
308          * reserved area of ram.  We must ensure the addresses
309          * are in the reserved area otherwise preloading the
310          * kernel could corrupt things.
311          */
312         result = -EADDRNOTAVAIL;
313         for (i = 0; i < nr_segments; i++) {
314                 unsigned long mstart, mend;
315
316                 mstart = image->segment[i].mem;
317                 mend = mstart + image->segment[i].memsz - 1;
318                 /* Ensure we are within the crash kernel limits */
319                 if ((mstart < crashk_res.start) || (mend > crashk_res.end))
320                         goto out;
321         }
322
323         /*
324          * Find a location for the control code buffer, and add
325          * the vector of segments so that it's pages will also be
326          * counted as destination pages.
327          */
328         result = -ENOMEM;
329         image->control_code_page = kimage_alloc_control_pages(image,
330                                            get_order(KEXEC_CONTROL_PAGE_SIZE));
331         if (!image->control_code_page) {
332                 printk(KERN_ERR "Could not allocate control_code_buffer\n");
333                 goto out;
334         }
335
336         result = 0;
337 out:
338         if (result == 0)
339                 *rimage = image;
340         else
341                 kfree(image);
342
343         return result;
344 }
345
346 static int kimage_is_destination_range(struct kimage *image,
347                                         unsigned long start,
348                                         unsigned long end)
349 {
350         unsigned long i;
351
352         for (i = 0; i < image->nr_segments; i++) {
353                 unsigned long mstart, mend;
354
355                 mstart = image->segment[i].mem;
356                 mend = mstart + image->segment[i].memsz;
357                 if ((end > mstart) && (start < mend))
358                         return 1;
359         }
360
361         return 0;
362 }
363
364 static struct page *kimage_alloc_pages(gfp_t gfp_mask, unsigned int order, unsigned long limit)
365 {
366         struct page *pages;
367
368         pages = alloc_pages(gfp_mask, order);
369         if (pages) {
370                 unsigned int count, i;
371 #ifdef CONFIG_XEN
372                 int address_bits;
373
374                 if (limit == ~0UL)
375                         address_bits = BITS_PER_LONG;
376                 else
377                         address_bits = ilog2(limit);
378
379                 if (xen_limit_pages_to_max_mfn(pages, order, address_bits) < 0) {
380                         __free_pages(pages, order);
381                         return NULL;
382                 }
383 #endif
384                 pages->mapping = NULL;
385                 set_page_private(pages, order);
386                 count = 1 << order;
387                 for (i = 0; i < count; i++)
388                         SetPageReserved(pages + i);
389         }
390
391         return pages;
392 }
393
394 static void kimage_free_pages(struct page *page)
395 {
396         unsigned int order, count, i;
397
398         order = page_private(page);
399         count = 1 << order;
400         for (i = 0; i < count; i++)
401                 ClearPageReserved(page + i);
402         __free_pages(page, order);
403 }
404
405 static void kimage_free_page_list(struct list_head *list)
406 {
407         struct list_head *pos, *next;
408
409         list_for_each_safe(pos, next, list) {
410                 struct page *page;
411
412                 page = list_entry(pos, struct page, lru);
413                 list_del(&page->lru);
414                 kimage_free_pages(page);
415         }
416 }
417
418 static struct page *kimage_alloc_normal_control_pages(struct kimage *image,
419                                                         unsigned int order)
420 {
421         /* Control pages are special, they are the intermediaries
422          * that are needed while we copy the rest of the pages
423          * to their final resting place.  As such they must
424          * not conflict with either the destination addresses
425          * or memory the kernel is already using.
426          *
427          * The only case where we really need more than one of
428          * these are for architectures where we cannot disable
429          * the MMU and must instead generate an identity mapped
430          * page table for all of the memory.
431          *
432          * At worst this runs in O(N) of the image size.
433          */
434         struct list_head extra_pages;
435         struct page *pages;
436         unsigned int count;
437
438         count = 1 << order;
439         INIT_LIST_HEAD(&extra_pages);
440
441         /* Loop while I can allocate a page and the page allocated
442          * is a destination page.
443          */
444         do {
445                 unsigned long pfn, epfn, addr, eaddr;
446
447                 pages = kimage_alloc_pages(GFP_KERNEL, order, KEXEC_CONTROL_MEMORY_LIMIT);
448                 if (!pages)
449                         break;
450                 pfn   = kexec_page_to_pfn(pages);
451                 epfn  = pfn + count;
452                 addr  = pfn << PAGE_SHIFT;
453                 eaddr = epfn << PAGE_SHIFT;
454                 if ((epfn >= (KEXEC_CONTROL_MEMORY_LIMIT >> PAGE_SHIFT)) ||
455                               kimage_is_destination_range(image, addr, eaddr)) {
456                         list_add(&pages->lru, &extra_pages);
457                         pages = NULL;
458                 }
459         } while (!pages);
460
461         if (pages) {
462                 /* Remember the allocated page... */
463                 list_add(&pages->lru, &image->control_pages);
464
465                 /* Because the page is already in it's destination
466                  * location we will never allocate another page at
467                  * that address.  Therefore kimage_alloc_pages
468                  * will not return it (again) and we don't need
469                  * to give it an entry in image->segment[].
470                  */
471         }
472         /* Deal with the destination pages I have inadvertently allocated.
473          *
474          * Ideally I would convert multi-page allocations into single
475          * page allocations, and add everything to image->dest_pages.
476          *
477          * For now it is simpler to just free the pages.
478          */
479         kimage_free_page_list(&extra_pages);
480
481         return pages;
482 }
483
484 #ifndef CONFIG_XEN
485 static struct page *kimage_alloc_crash_control_pages(struct kimage *image,
486                                                       unsigned int order)
487 {
488         /* Control pages are special, they are the intermediaries
489          * that are needed while we copy the rest of the pages
490          * to their final resting place.  As such they must
491          * not conflict with either the destination addresses
492          * or memory the kernel is already using.
493          *
494          * Control pages are also the only pags we must allocate
495          * when loading a crash kernel.  All of the other pages
496          * are specified by the segments and we just memcpy
497          * into them directly.
498          *
499          * The only case where we really need more than one of
500          * these are for architectures where we cannot disable
501          * the MMU and must instead generate an identity mapped
502          * page table for all of the memory.
503          *
504          * Given the low demand this implements a very simple
505          * allocator that finds the first hole of the appropriate
506          * size in the reserved memory region, and allocates all
507          * of the memory up to and including the hole.
508          */
509         unsigned long hole_start, hole_end, size;
510         struct page *pages;
511
512         pages = NULL;
513         size = (1 << order) << PAGE_SHIFT;
514         hole_start = (image->control_page + (size - 1)) & ~(size - 1);
515         hole_end   = hole_start + size - 1;
516         while (hole_end <= crashk_res.end) {
517                 unsigned long i;
518
519                 if (hole_end > KEXEC_CRASH_CONTROL_MEMORY_LIMIT)
520                         break;
521                 if (hole_end > crashk_res.end)
522                         break;
523                 /* See if I overlap any of the segments */
524                 for (i = 0; i < image->nr_segments; i++) {
525                         unsigned long mstart, mend;
526
527                         mstart = image->segment[i].mem;
528                         mend   = mstart + image->segment[i].memsz - 1;
529                         if ((hole_end >= mstart) && (hole_start <= mend)) {
530                                 /* Advance the hole to the end of the segment */
531                                 hole_start = (mend + (size - 1)) & ~(size - 1);
532                                 hole_end   = hole_start + size - 1;
533                                 break;
534                         }
535                 }
536                 /* If I don't overlap any segments I have found my hole! */
537                 if (i == image->nr_segments) {
538                         pages = kexec_pfn_to_page(hole_start >> PAGE_SHIFT);
539                         break;
540                 }
541         }
542         if (pages)
543                 image->control_page = hole_end;
544
545         return pages;
546 }
547
548
549 struct page *kimage_alloc_control_pages(struct kimage *image,
550                                          unsigned int order)
551 {
552         struct page *pages = NULL;
553
554         switch (image->type) {
555         case KEXEC_TYPE_DEFAULT:
556                 pages = kimage_alloc_normal_control_pages(image, order);
557                 break;
558         case KEXEC_TYPE_CRASH:
559                 pages = kimage_alloc_crash_control_pages(image, order);
560                 break;
561         }
562
563         return pages;
564 }
565 #else /* !CONFIG_XEN */
566 struct page *kimage_alloc_control_pages(struct kimage *image,
567                                          unsigned int order)
568 {
569         return kimage_alloc_normal_control_pages(image, order);
570 }
571 #endif
572
573 static int kimage_add_entry(struct kimage *image, kimage_entry_t entry)
574 {
575         if (*image->entry != 0)
576                 image->entry++;
577
578         if (image->entry == image->last_entry) {
579                 kimage_entry_t *ind_page;
580                 struct page *page;
581
582                 page = kimage_alloc_page(image, GFP_KERNEL, KIMAGE_NO_DEST);
583                 if (!page)
584                         return -ENOMEM;
585
586                 ind_page = page_address(page);
587                 *image->entry = kexec_virt_to_phys(ind_page) | IND_INDIRECTION;
588                 image->entry = ind_page;
589                 image->last_entry = ind_page +
590                                       ((PAGE_SIZE/sizeof(kimage_entry_t)) - 1);
591         }
592         *image->entry = entry;
593         image->entry++;
594         *image->entry = 0;
595
596         return 0;
597 }
598
599 static int kimage_set_destination(struct kimage *image,
600                                    unsigned long destination)
601 {
602         int result;
603
604         destination &= PAGE_MASK;
605         result = kimage_add_entry(image, destination | IND_DESTINATION);
606         if (result == 0)
607                 image->destination = destination;
608
609         return result;
610 }
611
612
613 static int kimage_add_page(struct kimage *image, unsigned long page)
614 {
615         int result;
616
617         page &= PAGE_MASK;
618         result = kimage_add_entry(image, page | IND_SOURCE);
619         if (result == 0)
620                 image->destination += PAGE_SIZE;
621
622         return result;
623 }
624
625
626 static void kimage_free_extra_pages(struct kimage *image)
627 {
628         /* Walk through and free any extra destination pages I may have */
629         kimage_free_page_list(&image->dest_pages);
630
631         /* Walk through and free any unusable pages I have cached */
632         kimage_free_page_list(&image->unuseable_pages);
633
634 }
635 static void kimage_terminate(struct kimage *image)
636 {
637         if (*image->entry != 0)
638                 image->entry++;
639
640         *image->entry = IND_DONE;
641 }
642
643 #define for_each_kimage_entry(image, ptr, entry) \
644         for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE); \
645                 ptr = (entry & IND_INDIRECTION)? \
646                         kexec_phys_to_virt((entry & PAGE_MASK)): ptr +1)
647
648 static void kimage_free_entry(kimage_entry_t entry)
649 {
650         struct page *page;
651
652         page = kexec_pfn_to_page(entry >> PAGE_SHIFT);
653         kimage_free_pages(page);
654 }
655
656 static void kimage_free(struct kimage *image)
657 {
658         kimage_entry_t *ptr, entry;
659         kimage_entry_t ind = 0;
660
661         if (!image)
662                 return;
663
664 #ifdef CONFIG_XEN
665         xen_machine_kexec_unload(image);
666 #endif
667
668         kimage_free_extra_pages(image);
669         for_each_kimage_entry(image, ptr, entry) {
670                 if (entry & IND_INDIRECTION) {
671                         /* Free the previous indirection page */
672                         if (ind & IND_INDIRECTION)
673                                 kimage_free_entry(ind);
674                         /* Save this indirection page until we are
675                          * done with it.
676                          */
677                         ind = entry;
678                 }
679                 else if (entry & IND_SOURCE)
680                         kimage_free_entry(entry);
681         }
682         /* Free the final indirection page */
683         if (ind & IND_INDIRECTION)
684                 kimage_free_entry(ind);
685
686         /* Handle any machine specific cleanup */
687         machine_kexec_cleanup(image);
688
689         /* Free the kexec control pages... */
690         kimage_free_page_list(&image->control_pages);
691         kfree(image);
692 }
693
694 static kimage_entry_t *kimage_dst_used(struct kimage *image,
695                                         unsigned long page)
696 {
697         kimage_entry_t *ptr, entry;
698         unsigned long destination = 0;
699
700         for_each_kimage_entry(image, ptr, entry) {
701                 if (entry & IND_DESTINATION)
702                         destination = entry & PAGE_MASK;
703                 else if (entry & IND_SOURCE) {
704                         if (page == destination)
705                                 return ptr;
706                         destination += PAGE_SIZE;
707                 }
708         }
709
710         return NULL;
711 }
712
713 static struct page *kimage_alloc_page(struct kimage *image,
714                                         gfp_t gfp_mask,
715                                         unsigned long destination)
716 {
717         /*
718          * Here we implement safeguards to ensure that a source page
719          * is not copied to its destination page before the data on
720          * the destination page is no longer useful.
721          *
722          * To do this we maintain the invariant that a source page is
723          * either its own destination page, or it is not a
724          * destination page at all.
725          *
726          * That is slightly stronger than required, but the proof
727          * that no problems will not occur is trivial, and the
728          * implementation is simply to verify.
729          *
730          * When allocating all pages normally this algorithm will run
731          * in O(N) time, but in the worst case it will run in O(N^2)
732          * time.   If the runtime is a problem the data structures can
733          * be fixed.
734          */
735         struct page *page;
736         unsigned long addr;
737
738         /*
739          * Walk through the list of destination pages, and see if I
740          * have a match.
741          */
742         list_for_each_entry(page, &image->dest_pages, lru) {
743                 addr = kexec_page_to_pfn(page) << PAGE_SHIFT;
744                 if (addr == destination) {
745                         list_del(&page->lru);
746                         return page;
747                 }
748         }
749         page = NULL;
750         while (1) {
751                 kimage_entry_t *old;
752
753                 /* Allocate a page, if we run out of memory give up */
754                 page = kimage_alloc_pages(gfp_mask, 0, KEXEC_SOURCE_MEMORY_LIMIT);
755                 if (!page)
756                         return NULL;
757                 /* If the page cannot be used file it away */
758                 if (kexec_page_to_pfn(page) >
759                                 (KEXEC_SOURCE_MEMORY_LIMIT >> PAGE_SHIFT)) {
760                         list_add(&page->lru, &image->unuseable_pages);
761                         continue;
762                 }
763                 addr = kexec_page_to_pfn(page) << PAGE_SHIFT;
764
765                 /* If it is the destination page we want use it */
766                 if (addr == destination)
767                         break;
768
769                 /* If the page is not a destination page use it */
770                 if (!kimage_is_destination_range(image, addr,
771                                                   addr + PAGE_SIZE))
772                         break;
773
774                 /*
775                  * I know that the page is someones destination page.
776                  * See if there is already a source page for this
777                  * destination page.  And if so swap the source pages.
778                  */
779                 old = kimage_dst_used(image, addr);
780                 if (old) {
781                         /* If so move it */
782                         unsigned long old_addr;
783                         struct page *old_page;
784
785                         old_addr = *old & PAGE_MASK;
786                         old_page = kexec_pfn_to_page(old_addr >> PAGE_SHIFT);
787                         copy_highpage(page, old_page);
788                         *old = addr | (*old & ~PAGE_MASK);
789
790                         /* The old page I have found cannot be a
791                          * destination page, so return it if it's
792                          * gfp_flags honor the ones passed in.
793                          */
794                         if (!(gfp_mask & __GFP_HIGHMEM) &&
795                             PageHighMem(old_page)) {
796                                 kimage_free_pages(old_page);
797                                 continue;
798                         }
799                         addr = old_addr;
800                         page = old_page;
801                         break;
802                 }
803                 else {
804                         /* Place the page on the destination list I
805                          * will use it later.
806                          */
807                         list_add(&page->lru, &image->dest_pages);
808                 }
809         }
810
811         return page;
812 }
813
814 static int kimage_load_normal_segment(struct kimage *image,
815                                          struct kexec_segment *segment)
816 {
817         unsigned long maddr;
818         unsigned long ubytes, mbytes;
819         int result;
820         unsigned char __user *buf;
821
822         result = 0;
823         buf = segment->buf;
824         ubytes = segment->bufsz;
825         mbytes = segment->memsz;
826         maddr = segment->mem;
827
828         result = kimage_set_destination(image, maddr);
829         if (result < 0)
830                 goto out;
831
832         while (mbytes) {
833                 struct page *page;
834                 char *ptr;
835                 size_t uchunk, mchunk;
836
837                 page = kimage_alloc_page(image, GFP_HIGHUSER, maddr);
838                 if (!page) {
839                         result  = -ENOMEM;
840                         goto out;
841                 }
842                 result = kimage_add_page(image, kexec_page_to_pfn(page)
843                                                                 << PAGE_SHIFT);
844                 if (result < 0)
845                         goto out;
846
847                 ptr = kmap(page);
848                 /* Start with a clear page */
849                 clear_page(ptr);
850                 ptr += maddr & ~PAGE_MASK;
851                 mchunk = PAGE_SIZE - (maddr & ~PAGE_MASK);
852                 if (mchunk > mbytes)
853                         mchunk = mbytes;
854
855                 uchunk = mchunk;
856                 if (uchunk > ubytes)
857                         uchunk = ubytes;
858
859                 result = copy_from_user(ptr, buf, uchunk);
860                 kunmap(page);
861                 if (result) {
862                         result = -EFAULT;
863                         goto out;
864                 }
865                 ubytes -= uchunk;
866                 maddr  += mchunk;
867                 buf    += mchunk;
868                 mbytes -= mchunk;
869         }
870 out:
871         return result;
872 }
873
874 #ifndef CONFIG_XEN
875 static int kimage_load_crash_segment(struct kimage *image,
876                                         struct kexec_segment *segment)
877 {
878         /* For crash dumps kernels we simply copy the data from
879          * user space to it's destination.
880          * We do things a page at a time for the sake of kmap.
881          */
882         unsigned long maddr;
883         unsigned long ubytes, mbytes;
884         int result;
885         unsigned char __user *buf;
886
887         result = 0;
888         buf = segment->buf;
889         ubytes = segment->bufsz;
890         mbytes = segment->memsz;
891         maddr = segment->mem;
892         while (mbytes) {
893                 struct page *page;
894                 char *ptr;
895                 size_t uchunk, mchunk;
896
897                 page = kexec_pfn_to_page(maddr >> PAGE_SHIFT);
898                 if (!page) {
899                         result  = -ENOMEM;
900                         goto out;
901                 }
902                 ptr = kmap(page);
903                 ptr += maddr & ~PAGE_MASK;
904                 mchunk = PAGE_SIZE - (maddr & ~PAGE_MASK);
905                 if (mchunk > mbytes)
906                         mchunk = mbytes;
907
908                 uchunk = mchunk;
909                 if (uchunk > ubytes) {
910                         uchunk = ubytes;
911                         /* Zero the trailing part of the page */
912                         memset(ptr + uchunk, 0, mchunk - uchunk);
913                 }
914                 result = copy_from_user(ptr, buf, uchunk);
915                 kexec_flush_icache_page(page);
916                 kunmap(page);
917                 if (result) {
918                         result = -EFAULT;
919                         goto out;
920                 }
921                 ubytes -= uchunk;
922                 maddr  += mchunk;
923                 buf    += mchunk;
924                 mbytes -= mchunk;
925         }
926 out:
927         return result;
928 }
929
930 static int kimage_load_segment(struct kimage *image,
931                                 struct kexec_segment *segment)
932 {
933         int result = -ENOMEM;
934
935         switch (image->type) {
936         case KEXEC_TYPE_DEFAULT:
937                 result = kimage_load_normal_segment(image, segment);
938                 break;
939         case KEXEC_TYPE_CRASH:
940                 result = kimage_load_crash_segment(image, segment);
941                 break;
942         }
943
944         return result;
945 }
946 #else /* CONFIG_XEN */
947 static int kimage_load_segment(struct kimage *image,
948                                 struct kexec_segment *segment)
949 {
950         return kimage_load_normal_segment(image, segment);
951 }
952 #endif
953
954 /*
955  * Exec Kernel system call: for obvious reasons only root may call it.
956  *
957  * This call breaks up into three pieces.
958  * - A generic part which loads the new kernel from the current
959  *   address space, and very carefully places the data in the
960  *   allocated pages.
961  *
962  * - A generic part that interacts with the kernel and tells all of
963  *   the devices to shut down.  Preventing on-going dmas, and placing
964  *   the devices in a consistent state so a later kernel can
965  *   reinitialize them.
966  *
967  * - A machine specific part that includes the syscall number
968  *   and the copies the image to it's final destination.  And
969  *   jumps into the image at entry.
970  *
971  * kexec does not sync, or unmount filesystems so if you need
972  * that to happen you need to do that yourself.
973  */
974 struct kimage *kexec_image;
975 struct kimage *kexec_crash_image;
976
977 static DEFINE_MUTEX(kexec_mutex);
978
979 SYSCALL_DEFINE4(kexec_load, unsigned long, entry, unsigned long, nr_segments,
980                 struct kexec_segment __user *, segments, unsigned long, flags)
981 {
982         struct kimage **dest_image, *image;
983         int result;
984
985         /* We only trust the superuser with rebooting the system. */
986         if (!capable(CAP_SYS_BOOT))
987                 return -EPERM;
988
989         /*
990          * Verify we have a legal set of flags
991          * This leaves us room for future extensions.
992          */
993         if ((flags & KEXEC_FLAGS) != (flags & ~KEXEC_ARCH_MASK))
994                 return -EINVAL;
995
996         /* Verify we are on the appropriate architecture */
997         if (((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH) &&
998                 ((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH_DEFAULT))
999                 return -EINVAL;
1000
1001         /* Put an artificial cap on the number
1002          * of segments passed to kexec_load.
1003          */
1004         if (nr_segments > KEXEC_SEGMENT_MAX)
1005                 return -EINVAL;
1006
1007         image = NULL;
1008         result = 0;
1009
1010         /* Because we write directly to the reserved memory
1011          * region when loading crash kernels we need a mutex here to
1012          * prevent multiple crash  kernels from attempting to load
1013          * simultaneously, and to prevent a crash kernel from loading
1014          * over the top of a in use crash kernel.
1015          *
1016          * KISS: always take the mutex.
1017          */
1018         if (!mutex_trylock(&kexec_mutex))
1019                 return -EBUSY;
1020
1021         dest_image = &kexec_image;
1022         if (flags & KEXEC_ON_CRASH)
1023                 dest_image = &kexec_crash_image;
1024         if (nr_segments > 0) {
1025                 unsigned long i;
1026
1027                 /* Loading another kernel to reboot into */
1028                 if ((flags & KEXEC_ON_CRASH) == 0)
1029                         result = kimage_normal_alloc(&image, entry,
1030                                                         nr_segments, segments);
1031                 /* Loading another kernel to switch to if this one crashes */
1032                 else if (flags & KEXEC_ON_CRASH) {
1033                         /* Free any current crash dump kernel before
1034                          * we corrupt it.
1035                          */
1036                         kimage_free(xchg(&kexec_crash_image, NULL));
1037                         result = kimage_crash_alloc(&image, entry,
1038                                                      nr_segments, segments);
1039                         crash_map_reserved_pages();
1040                 }
1041                 if (result)
1042                         goto out;
1043
1044                 if (flags & KEXEC_PRESERVE_CONTEXT)
1045                         image->preserve_context = 1;
1046                 result = machine_kexec_prepare(image);
1047                 if (result)
1048                         goto out;
1049
1050                 for (i = 0; i < nr_segments; i++) {
1051                         result = kimage_load_segment(image, &image->segment[i]);
1052                         if (result)
1053                                 goto out;
1054                 }
1055                 kimage_terminate(image);
1056                 if (flags & KEXEC_ON_CRASH)
1057                         crash_unmap_reserved_pages();
1058         }
1059 #ifdef CONFIG_XEN
1060         if (image) {
1061                 result = xen_machine_kexec_load(image);
1062                 if (result)
1063                         goto out;
1064         }
1065 #endif
1066         /* Install the new kernel, and  Uninstall the old */
1067         image = xchg(dest_image, image);
1068
1069 out:
1070         mutex_unlock(&kexec_mutex);
1071         kimage_free(image);
1072
1073         return result;
1074 }
1075
1076 /*
1077  * Add and remove page tables for crashkernel memory
1078  *
1079  * Provide an empty default implementation here -- architecture
1080  * code may override this
1081  */
1082 void __weak crash_map_reserved_pages(void)
1083 {}
1084
1085 void __weak crash_unmap_reserved_pages(void)
1086 {}
1087
1088 #ifdef CONFIG_COMPAT
1089 asmlinkage long compat_sys_kexec_load(unsigned long entry,
1090                                 unsigned long nr_segments,
1091                                 struct compat_kexec_segment __user *segments,
1092                                 unsigned long flags)
1093 {
1094         struct compat_kexec_segment in;
1095         struct kexec_segment out, __user *ksegments;
1096         unsigned long i, result;
1097
1098         /* Don't allow clients that don't understand the native
1099          * architecture to do anything.
1100          */
1101         if ((flags & KEXEC_ARCH_MASK) == KEXEC_ARCH_DEFAULT)
1102                 return -EINVAL;
1103
1104         if (nr_segments > KEXEC_SEGMENT_MAX)
1105                 return -EINVAL;
1106
1107         ksegments = compat_alloc_user_space(nr_segments * sizeof(out));
1108         for (i=0; i < nr_segments; i++) {
1109                 result = copy_from_user(&in, &segments[i], sizeof(in));
1110                 if (result)
1111                         return -EFAULT;
1112
1113                 out.buf   = compat_ptr(in.buf);
1114                 out.bufsz = in.bufsz;
1115                 out.mem   = in.mem;
1116                 out.memsz = in.memsz;
1117
1118                 result = copy_to_user(&ksegments[i], &out, sizeof(out));
1119                 if (result)
1120                         return -EFAULT;
1121         }
1122
1123         return sys_kexec_load(entry, nr_segments, ksegments, flags);
1124 }
1125 #endif
1126
1127 void crash_kexec(struct pt_regs *regs)
1128 {
1129         /* Take the kexec_mutex here to prevent sys_kexec_load
1130          * running on one cpu from replacing the crash kernel
1131          * we are using after a panic on a different cpu.
1132          *
1133          * If the crash kernel was not located in a fixed area
1134          * of memory the xchg(&kexec_crash_image) would be
1135          * sufficient.  But since I reuse the memory...
1136          */
1137         if (mutex_trylock(&kexec_mutex)) {
1138                 if (kexec_crash_image) {
1139                         struct pt_regs fixed_regs;
1140
1141                         crash_setup_regs(&fixed_regs, regs);
1142                         crash_save_vmcoreinfo();
1143                         machine_crash_shutdown(&fixed_regs);
1144                         machine_kexec(kexec_crash_image);
1145                 }
1146                 mutex_unlock(&kexec_mutex);
1147         }
1148 }
1149
1150 size_t crash_get_memory_size(void)
1151 {
1152         size_t size = 0;
1153         mutex_lock(&kexec_mutex);
1154         if (crashk_res.end != crashk_res.start)
1155                 size = resource_size(&crashk_res);
1156         mutex_unlock(&kexec_mutex);
1157         return size;
1158 }
1159
1160 #ifndef CONFIG_XEN
1161 void __weak crash_free_reserved_phys_range(unsigned long begin,
1162                                            unsigned long end)
1163 {
1164         unsigned long addr;
1165
1166         for (addr = begin; addr < end; addr += PAGE_SIZE) {
1167                 ClearPageReserved(pfn_to_page(addr >> PAGE_SHIFT));
1168                 init_page_count(pfn_to_page(addr >> PAGE_SHIFT));
1169                 free_page((unsigned long)__va(addr));
1170                 totalram_pages++;
1171         }
1172 }
1173
1174 int crash_shrink_memory(unsigned long new_size)
1175 {
1176         int ret = 0;
1177         unsigned long start, end;
1178         unsigned long old_size;
1179         struct resource *ram_res;
1180
1181         mutex_lock(&kexec_mutex);
1182
1183         if (kexec_crash_image) {
1184                 ret = -ENOENT;
1185                 goto unlock;
1186         }
1187         start = crashk_res.start;
1188         end = crashk_res.end;
1189         old_size = (end == 0) ? 0 : end - start + 1;
1190         if (new_size >= old_size) {
1191                 ret = (new_size == old_size) ? 0 : -EINVAL;
1192                 goto unlock;
1193         }
1194
1195         ram_res = kzalloc(sizeof(*ram_res), GFP_KERNEL);
1196         if (!ram_res) {
1197                 ret = -ENOMEM;
1198                 goto unlock;
1199         }
1200
1201         start = roundup(start, KEXEC_CRASH_MEM_ALIGN);
1202         end = roundup(start + new_size, KEXEC_CRASH_MEM_ALIGN);
1203
1204         crash_map_reserved_pages();
1205         crash_free_reserved_phys_range(end, crashk_res.end);
1206
1207         if ((start == end) && (crashk_res.parent != NULL))
1208                 release_resource(&crashk_res);
1209
1210         ram_res->start = end;
1211         ram_res->end = crashk_res.end;
1212         ram_res->flags = IORESOURCE_BUSY | IORESOURCE_MEM;
1213         ram_res->name = "System RAM";
1214
1215         crashk_res.end = end - 1;
1216
1217         insert_resource(&iomem_resource, ram_res);
1218         crash_unmap_reserved_pages();
1219
1220 unlock:
1221         mutex_unlock(&kexec_mutex);
1222         return ret;
1223 }
1224 #endif /* !CONFIG_XEN */
1225
1226 static u32 *append_elf_note(u32 *buf, char *name, unsigned type, void *data,
1227                             size_t data_len)
1228 {
1229         struct elf_note note;
1230
1231         note.n_namesz = strlen(name) + 1;
1232         note.n_descsz = data_len;
1233         note.n_type   = type;
1234         memcpy(buf, &note, sizeof(note));
1235         buf += (sizeof(note) + 3)/4;
1236         memcpy(buf, name, note.n_namesz);
1237         buf += (note.n_namesz + 3)/4;
1238         memcpy(buf, data, note.n_descsz);
1239         buf += (note.n_descsz + 3)/4;
1240
1241         return buf;
1242 }
1243
1244 static void final_note(u32 *buf)
1245 {
1246         struct elf_note note;
1247
1248         note.n_namesz = 0;
1249         note.n_descsz = 0;
1250         note.n_type   = 0;
1251         memcpy(buf, &note, sizeof(note));
1252 }
1253
1254 #ifndef CONFIG_XEN
1255 void crash_save_cpu(struct pt_regs *regs, int cpu)
1256 {
1257         struct elf_prstatus prstatus;
1258         u32 *buf;
1259
1260         if ((cpu < 0) || (cpu >= nr_cpu_ids))
1261                 return;
1262
1263         /* Using ELF notes here is opportunistic.
1264          * I need a well defined structure format
1265          * for the data I pass, and I need tags
1266          * on the data to indicate what information I have
1267          * squirrelled away.  ELF notes happen to provide
1268          * all of that, so there is no need to invent something new.
1269          */
1270         buf = (u32*)per_cpu_ptr(crash_notes, cpu);
1271         if (!buf)
1272                 return;
1273         memset(&prstatus, 0, sizeof(prstatus));
1274         prstatus.pr_pid = current->pid;
1275         elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
1276         buf = append_elf_note(buf, KEXEC_CORE_NOTE_NAME, NT_PRSTATUS,
1277                               &prstatus, sizeof(prstatus));
1278         final_note(buf);
1279 }
1280 #endif
1281
1282 static int __init crash_notes_memory_init(void)
1283 {
1284 #ifndef CONFIG_XEN
1285         /* Allocate memory for saving cpu registers. */
1286         crash_notes = alloc_percpu(note_buf_t);
1287         if (!crash_notes) {
1288                 printk("Kexec: Memory allocation for saving cpu register"
1289                 " states failed\n");
1290                 return -ENOMEM;
1291         }
1292 #endif
1293         return 0;
1294 }
1295 module_init(crash_notes_memory_init)
1296
1297
1298 #ifndef CONFIG_XEN
1299 /*
1300  * parsing the "crashkernel" commandline
1301  *
1302  * this code is intended to be called from architecture specific code
1303  */
1304
1305
1306 /*
1307  * This function parses command lines in the format
1308  *
1309  *   crashkernel=ramsize-range:size[,...][@offset]
1310  *
1311  * The function returns 0 on success and -EINVAL on failure.
1312  */
1313 static int __init parse_crashkernel_mem(char                    *cmdline,
1314                                         unsigned long long      system_ram,
1315                                         unsigned long long      *crash_size,
1316                                         unsigned long long      *crash_base)
1317 {
1318         char *cur = cmdline, *tmp;
1319
1320         /* for each entry of the comma-separated list */
1321         do {
1322                 unsigned long long start, end = ULLONG_MAX, size;
1323
1324                 /* get the start of the range */
1325                 start = memparse(cur, &tmp);
1326                 if (cur == tmp) {
1327                         pr_warning("crashkernel: Memory value expected\n");
1328                         return -EINVAL;
1329                 }
1330                 cur = tmp;
1331                 if (*cur != '-') {
1332                         pr_warning("crashkernel: '-' expected\n");
1333                         return -EINVAL;
1334                 }
1335                 cur++;
1336
1337                 /* if no ':' is here, than we read the end */
1338                 if (*cur != ':') {
1339                         end = memparse(cur, &tmp);
1340                         if (cur == tmp) {
1341                                 pr_warning("crashkernel: Memory "
1342                                                 "value expected\n");
1343                                 return -EINVAL;
1344                         }
1345                         cur = tmp;
1346                         if (end <= start) {
1347                                 pr_warning("crashkernel: end <= start\n");
1348                                 return -EINVAL;
1349                         }
1350                 }
1351
1352                 if (*cur != ':') {
1353                         pr_warning("crashkernel: ':' expected\n");
1354                         return -EINVAL;
1355                 }
1356                 cur++;
1357
1358                 size = memparse(cur, &tmp);
1359                 if (cur == tmp) {
1360                         pr_warning("Memory value expected\n");
1361                         return -EINVAL;
1362                 }
1363                 cur = tmp;
1364                 if (size >= system_ram) {
1365                         pr_warning("crashkernel: invalid size\n");
1366                         return -EINVAL;
1367                 }
1368
1369                 /* match ? */
1370                 if (system_ram >= start && system_ram < end) {
1371                         *crash_size = size;
1372                         break;
1373                 }
1374         } while (*cur++ == ',');
1375
1376         if (*crash_size > 0) {
1377                 while (*cur && *cur != ' ' && *cur != '@')
1378                         cur++;
1379                 if (*cur == '@') {
1380                         cur++;
1381                         *crash_base = memparse(cur, &tmp);
1382                         if (cur == tmp) {
1383                                 pr_warning("Memory value expected "
1384                                                 "after '@'\n");
1385                                 return -EINVAL;
1386                         }
1387                 }
1388         }
1389
1390         return 0;
1391 }
1392
1393 /*
1394  * That function parses "simple" (old) crashkernel command lines like
1395  *
1396  *      crashkernel=size[@offset]
1397  *
1398  * It returns 0 on success and -EINVAL on failure.
1399  */
1400 static int __init parse_crashkernel_simple(char                 *cmdline,
1401                                            unsigned long long   *crash_size,
1402                                            unsigned long long   *crash_base)
1403 {
1404         char *cur = cmdline;
1405
1406         *crash_size = memparse(cmdline, &cur);
1407         if (cmdline == cur) {
1408                 pr_warning("crashkernel: memory value expected\n");
1409                 return -EINVAL;
1410         }
1411
1412         if (*cur == '@')
1413                 *crash_base = memparse(cur+1, &cur);
1414         else if (*cur != ' ' && *cur != '\0') {
1415                 pr_warning("crashkernel: unrecognized char\n");
1416                 return -EINVAL;
1417         }
1418
1419         return 0;
1420 }
1421
1422 /*
1423  * That function is the entry point for command line parsing and should be
1424  * called from the arch-specific code.
1425  */
1426 int __init parse_crashkernel(char                *cmdline,
1427                              unsigned long long system_ram,
1428                              unsigned long long *crash_size,
1429                              unsigned long long *crash_base)
1430 {
1431         char    *p = cmdline, *ck_cmdline = NULL;
1432         char    *first_colon, *first_space;
1433
1434         BUG_ON(!crash_size || !crash_base);
1435         *crash_size = 0;
1436         *crash_base = 0;
1437
1438         /* find crashkernel and use the last one if there are more */
1439         p = strstr(p, "crashkernel=");
1440         while (p) {
1441                 ck_cmdline = p;
1442                 p = strstr(p+1, "crashkernel=");
1443         }
1444
1445         if (!ck_cmdline)
1446                 return -EINVAL;
1447
1448         ck_cmdline += 12; /* strlen("crashkernel=") */
1449
1450         /*
1451          * if the commandline contains a ':', then that's the extended
1452          * syntax -- if not, it must be the classic syntax
1453          */
1454         first_colon = strchr(ck_cmdline, ':');
1455         first_space = strchr(ck_cmdline, ' ');
1456         if (first_colon && (!first_space || first_colon < first_space))
1457                 return parse_crashkernel_mem(ck_cmdline, system_ram,
1458                                 crash_size, crash_base);
1459         else
1460                 return parse_crashkernel_simple(ck_cmdline, crash_size,
1461                                 crash_base);
1462
1463         return 0;
1464 }
1465 #endif
1466
1467 static void update_vmcoreinfo_note(void)
1468 {
1469         u32 *buf = vmcoreinfo_note;
1470
1471         if (!vmcoreinfo_size)
1472                 return;
1473         buf = append_elf_note(buf, VMCOREINFO_NOTE_NAME, 0, vmcoreinfo_data,
1474                               vmcoreinfo_size);
1475         final_note(buf);
1476 }
1477
1478 void crash_save_vmcoreinfo(void)
1479 {
1480         vmcoreinfo_append_str("CRASHTIME=%ld", get_seconds());
1481         update_vmcoreinfo_note();
1482 }
1483
1484 void vmcoreinfo_append_str(const char *fmt, ...)
1485 {
1486         va_list args;
1487         char buf[0x50];
1488         int r;
1489
1490         va_start(args, fmt);
1491         r = vsnprintf(buf, sizeof(buf), fmt, args);
1492         va_end(args);
1493
1494         if (r + vmcoreinfo_size > vmcoreinfo_max_size)
1495                 r = vmcoreinfo_max_size - vmcoreinfo_size;
1496
1497         memcpy(&vmcoreinfo_data[vmcoreinfo_size], buf, r);
1498
1499         vmcoreinfo_size += r;
1500 }
1501
1502 /*
1503  * provide an empty default implementation here -- architecture
1504  * code may override this
1505  */
1506 void __attribute__ ((weak)) arch_crash_save_vmcoreinfo(void)
1507 {}
1508
1509 unsigned long __attribute__ ((weak)) paddr_vmcoreinfo_note(void)
1510 {
1511         return __pa((unsigned long)(char *)&vmcoreinfo_note);
1512 }
1513
1514 static int __init crash_save_vmcoreinfo_init(void)
1515 {
1516         VMCOREINFO_OSRELEASE(init_uts_ns.name.release);
1517         VMCOREINFO_PAGESIZE(PAGE_SIZE);
1518
1519         VMCOREINFO_SYMBOL(init_uts_ns);
1520         VMCOREINFO_SYMBOL(node_online_map);
1521 #ifdef CONFIG_MMU
1522 # ifndef CONFIG_X86_XEN
1523         VMCOREINFO_SYMBOL(swapper_pg_dir);
1524 # else
1525 /*
1526  * Since for x86-32 Xen swapper_pg_dir is a pointer rather than an array,
1527  * make the value stored consistent with native (i.e. the base address of
1528  * the page directory).
1529  */
1530 #  define swapper_pg_dir *swapper_pg_dir
1531         VMCOREINFO_SYMBOL(swapper_pg_dir);
1532 #  undef swapper_pg_dir
1533 # endif
1534 #endif
1535         VMCOREINFO_SYMBOL(_stext);
1536         VMCOREINFO_SYMBOL(vmlist);
1537
1538 #ifndef CONFIG_NEED_MULTIPLE_NODES
1539         VMCOREINFO_SYMBOL(mem_map);
1540         VMCOREINFO_SYMBOL(contig_page_data);
1541 #endif
1542 #ifdef CONFIG_SPARSEMEM
1543         VMCOREINFO_SYMBOL(mem_section);
1544         VMCOREINFO_LENGTH(mem_section, NR_SECTION_ROOTS);
1545         VMCOREINFO_STRUCT_SIZE(mem_section);
1546         VMCOREINFO_OFFSET(mem_section, section_mem_map);
1547 #endif
1548         VMCOREINFO_STRUCT_SIZE(page);
1549         VMCOREINFO_STRUCT_SIZE(pglist_data);
1550         VMCOREINFO_STRUCT_SIZE(zone);
1551         VMCOREINFO_STRUCT_SIZE(free_area);
1552         VMCOREINFO_STRUCT_SIZE(list_head);
1553         VMCOREINFO_SIZE(nodemask_t);
1554         VMCOREINFO_OFFSET(page, flags);
1555         VMCOREINFO_OFFSET(page, _count);
1556         VMCOREINFO_OFFSET(page, mapping);
1557         VMCOREINFO_OFFSET(page, lru);
1558         VMCOREINFO_OFFSET(pglist_data, node_zones);
1559         VMCOREINFO_OFFSET(pglist_data, nr_zones);
1560 #ifdef CONFIG_FLAT_NODE_MEM_MAP
1561         VMCOREINFO_OFFSET(pglist_data, node_mem_map);
1562 #endif
1563         VMCOREINFO_OFFSET(pglist_data, node_start_pfn);
1564         VMCOREINFO_OFFSET(pglist_data, node_spanned_pages);
1565         VMCOREINFO_OFFSET(pglist_data, node_id);
1566         VMCOREINFO_OFFSET(zone, free_area);
1567         VMCOREINFO_OFFSET(zone, vm_stat);
1568         VMCOREINFO_OFFSET(zone, spanned_pages);
1569         VMCOREINFO_OFFSET(free_area, free_list);
1570         VMCOREINFO_OFFSET(list_head, next);
1571         VMCOREINFO_OFFSET(list_head, prev);
1572         VMCOREINFO_OFFSET(vm_struct, addr);
1573         VMCOREINFO_LENGTH(zone.free_area, MAX_ORDER);
1574         log_buf_kexec_setup();
1575         VMCOREINFO_LENGTH(free_area.free_list, MIGRATE_TYPES);
1576         VMCOREINFO_NUMBER(NR_FREE_PAGES);
1577         VMCOREINFO_NUMBER(PG_lru);
1578         VMCOREINFO_NUMBER(PG_private);
1579         VMCOREINFO_NUMBER(PG_swapcache);
1580
1581         arch_crash_save_vmcoreinfo();
1582         update_vmcoreinfo_note();
1583
1584         return 0;
1585 }
1586
1587 module_init(crash_save_vmcoreinfo_init)
1588
1589 /*
1590  * Move into place and start executing a preloaded standalone
1591  * executable.  If nothing was preloaded return an error.
1592  */
1593 int kernel_kexec(void)
1594 {
1595         int error = 0;
1596
1597         if (!mutex_trylock(&kexec_mutex))
1598                 return -EBUSY;
1599         if (!kexec_image) {
1600                 error = -EINVAL;
1601                 goto Unlock;
1602         }
1603
1604 #ifdef CONFIG_KEXEC_JUMP
1605         if (kexec_image->preserve_context) {
1606                 lock_system_sleep();
1607                 pm_prepare_console();
1608                 error = freeze_processes();
1609                 if (error) {
1610                         error = -EBUSY;
1611                         goto Restore_console;
1612                 }
1613                 suspend_console();
1614                 error = dpm_suspend_start(PMSG_FREEZE);
1615                 if (error)
1616                         goto Resume_console;
1617                 /* At this point, dpm_suspend_start() has been called,
1618                  * but *not* dpm_suspend_end(). We *must* call
1619                  * dpm_suspend_end() now.  Otherwise, drivers for
1620                  * some devices (e.g. interrupt controllers) become
1621                  * desynchronized with the actual state of the
1622                  * hardware at resume time, and evil weirdness ensues.
1623                  */
1624                 error = dpm_suspend_end(PMSG_FREEZE);
1625                 if (error)
1626                         goto Resume_devices;
1627                 error = disable_nonboot_cpus();
1628                 if (error)
1629                         goto Enable_cpus;
1630                 local_irq_disable();
1631                 error = syscore_suspend();
1632                 if (error)
1633                         goto Enable_irqs;
1634         } else
1635 #endif
1636         {
1637                 kernel_restart_prepare(NULL);
1638                 printk(KERN_EMERG "Starting new kernel\n");
1639                 machine_shutdown();
1640         }
1641
1642         machine_kexec(kexec_image);
1643
1644 #ifdef CONFIG_KEXEC_JUMP
1645         if (kexec_image->preserve_context) {
1646                 syscore_resume();
1647  Enable_irqs:
1648                 local_irq_enable();
1649  Enable_cpus:
1650                 enable_nonboot_cpus();
1651                 dpm_resume_start(PMSG_RESTORE);
1652  Resume_devices:
1653                 dpm_resume_end(PMSG_RESTORE);
1654  Resume_console:
1655                 resume_console();
1656                 thaw_processes();
1657  Restore_console:
1658                 pm_restore_console();
1659                 unlock_system_sleep();
1660         }
1661 #endif
1662
1663  Unlock:
1664         mutex_unlock(&kexec_mutex);
1665         return error;
1666 }