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