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