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