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