9 #include <linux/capability.h>
12 #include <linux/slab.h>
14 #include <linux/kexec.h>
16 #include <linux/list.h>
19 #include <linux/reboot.h>
22 #include <linux/elf.h>
23 #include <linux/elfcore.h>
24 #include <linux/utsname.h>
27 #include <linux/device.h>
37 #include <asm/uaccess.h>
39 #include <asm/sections.h>
45 static unsigned char vmcoreinfo_data[VMCOREINFO_BYTES];
52 .name =
"Crash kernel",
109 #define KIMAGE_NO_DEST (-1UL)
111 static int kimage_is_destination_range(
struct kimage *
image,
112 unsigned long start,
unsigned long end);
113 static struct page *kimage_alloc_page(
struct kimage *
image,
117 static int do_kimage_alloc(
struct kimage **rimage,
unsigned long entry,
121 size_t segment_bytes;
122 struct kimage *
image;
133 image->entry = &image->head;
134 image->last_entry = &image->head;
135 image->control_page = ~0;
136 image->start =
entry;
137 image->type = KEXEC_TYPE_DEFAULT;
140 INIT_LIST_HEAD(&image->control_pages);
143 INIT_LIST_HEAD(&image->dest_pages);
146 INIT_LIST_HEAD(&image->unuseable_pages);
150 segment_bytes = nr_segments *
sizeof(*segments);
172 unsigned long mstart, mend;
174 mstart = image->segment[
i].mem;
175 mend = mstart + image->segment[
i].memsz;
189 unsigned long mstart, mend;
192 mstart = image->segment[
i].mem;
193 mend = mstart + image->segment[
i].memsz;
194 for (j = 0; j <
i; j++) {
196 pstart = image->segment[
j].mem;
197 pend = pstart + image->segment[
j].memsz;
199 if ((mend > pstart) && (mstart < pend))
211 if (image->segment[i].bufsz > image->segment[i].memsz)
226 static int kimage_normal_alloc(
struct kimage **rimage,
unsigned long entry,
227 unsigned long nr_segments,
231 struct kimage *
image;
235 result = do_kimage_alloc(&image, entry, nr_segments, segments);
249 if (!image->control_code_page) {
255 if (!image->swap_page) {
270 static int kimage_crash_alloc(
struct kimage **rimage,
unsigned long entry,
271 unsigned long nr_segments,
275 struct kimage *
image;
280 if ((entry < crashk_res.
start) || (entry > crashk_res.
end)) {
286 result = do_kimage_alloc(&image, entry, nr_segments, segments);
293 image->control_page = crashk_res.
start;
294 image->type = KEXEC_TYPE_CRASH;
307 unsigned long mstart, mend;
309 mstart = image->segment[
i].mem;
310 mend = mstart + image->segment[
i].memsz - 1;
312 if ((mstart < crashk_res.
start) || (mend > crashk_res.
end))
324 if (!image->control_code_page) {
339 static int kimage_is_destination_range(
struct kimage *image,
345 for (i = 0; i < image->nr_segments; i++) {
346 unsigned long mstart, mend;
348 mstart = image->segment[
i].mem;
349 mend = mstart + image->segment[
i].memsz;
350 if ((end > mstart) && (start < mend))
365 set_page_private(pages, order);
367 for (i = 0; i <
count; i++)
368 SetPageReserved(pages + i);
374 static void kimage_free_pages(
struct page *
page)
378 order = page_private(page);
380 for (i = 0; i <
count; i++)
381 ClearPageReserved(page + i);
394 kimage_free_pages(page);
398 static struct page *kimage_alloc_normal_control_pages(
struct kimage *image,
419 INIT_LIST_HEAD(&extra_pages);
425 unsigned long pfn, epfn,
addr, eaddr;
427 pages = kimage_alloc_pages(
GFP_KERNEL, order);
435 kimage_is_destination_range(image, addr, eaddr)) {
436 list_add(&pages->
lru, &extra_pages);
443 list_add(&pages->
lru, &image->control_pages);
459 kimage_free_page_list(&extra_pages);
464 static struct page *kimage_alloc_crash_control_pages(
struct kimage *image,
488 unsigned long hole_start, hole_end,
size;
492 size = (1 <<
order) << PAGE_SHIFT;
493 hole_start = (image->control_page + (size - 1)) & ~(size - 1);
494 hole_end = hole_start + size - 1;
495 while (hole_end <= crashk_res.
end) {
500 if (hole_end > crashk_res.
end)
503 for (i = 0; i < image->nr_segments; i++) {
504 unsigned long mstart, mend;
506 mstart = image->segment[
i].mem;
507 mend = mstart + image->segment[
i].memsz - 1;
508 if ((hole_end >= mstart) && (hole_start <= mend)) {
510 hole_start = (mend + (size - 1)) & ~(size - 1);
511 hole_end = hole_start + size - 1;
516 if (i == image->nr_segments) {
522 image->control_page = hole_end;
531 struct page *pages =
NULL;
533 switch (image->type) {
534 case KEXEC_TYPE_DEFAULT:
535 pages = kimage_alloc_normal_control_pages(image, order);
537 case KEXEC_TYPE_CRASH:
538 pages = kimage_alloc_crash_control_pages(image, order);
545 static int kimage_add_entry(
struct kimage *image, kimage_entry_t entry)
547 if (*image->entry != 0)
550 if (image->entry == image->last_entry) {
551 kimage_entry_t *ind_page;
559 *image->entry =
virt_to_phys(ind_page) | IND_INDIRECTION;
560 image->entry = ind_page;
561 image->last_entry = ind_page +
562 ((
PAGE_SIZE/
sizeof(kimage_entry_t)) - 1);
564 *image->entry =
entry;
571 static int kimage_set_destination(
struct kimage *image,
572 unsigned long destination)
577 result = kimage_add_entry(image, destination | IND_DESTINATION);
579 image->destination = destination;
585 static int kimage_add_page(
struct kimage *image,
unsigned long page)
590 result = kimage_add_entry(image, page | IND_SOURCE);
598 static void kimage_free_extra_pages(
struct kimage *image)
601 kimage_free_page_list(&image->dest_pages);
604 kimage_free_page_list(&image->unuseable_pages);
607 static void kimage_terminate(
struct kimage *image)
609 if (*image->entry != 0)
612 *image->entry = IND_DONE;
615 #define for_each_kimage_entry(image, ptr, entry) \
616 for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE); \
617 ptr = (entry & IND_INDIRECTION)? \
618 phys_to_virt((entry & PAGE_MASK)): ptr +1)
620 static void kimage_free_entry(kimage_entry_t entry)
625 kimage_free_pages(page);
628 static void kimage_free(
struct kimage *image)
631 kimage_entry_t
ind = 0;
636 kimage_free_extra_pages(image);
638 if (entry & IND_INDIRECTION) {
640 if (ind & IND_INDIRECTION)
641 kimage_free_entry(ind);
647 else if (entry & IND_SOURCE)
648 kimage_free_entry(entry);
651 if (ind & IND_INDIRECTION)
652 kimage_free_entry(ind);
658 kimage_free_page_list(&image->control_pages);
662 static kimage_entry_t *kimage_dst_used(
struct kimage *image,
666 unsigned long destination = 0;
669 if (entry & IND_DESTINATION)
671 else if (entry & IND_SOURCE) {
672 if (page == destination)
681 static struct page *kimage_alloc_page(
struct kimage *image,
683 unsigned long destination)
712 if (addr == destination) {
722 page = kimage_alloc_pages(gfp_mask, 0);
728 list_add(&page->
lru, &image->unuseable_pages);
734 if (addr == destination)
738 if (!kimage_is_destination_range(image, addr,
747 old = kimage_dst_used(image, addr);
750 unsigned long old_addr;
751 struct page *old_page;
755 copy_highpage(page, old_page);
756 *old = addr | (*old & ~PAGE_MASK);
763 PageHighMem(old_page)) {
764 kimage_free_pages(old_page);
775 list_add(&page->
lru, &image->dest_pages);
782 static int kimage_load_normal_segment(
struct kimage *image,
786 unsigned long ubytes, mbytes;
792 ubytes = segment->
bufsz;
793 mbytes = segment->
memsz;
794 maddr = segment->
mem;
796 result = kimage_set_destination(image, maddr);
803 size_t uchunk, mchunk;
818 ptr += maddr & ~PAGE_MASK;
819 mchunk =
PAGE_SIZE - (maddr & ~PAGE_MASK);
842 static int kimage_load_crash_segment(
struct kimage *image,
850 unsigned long ubytes, mbytes;
856 ubytes = segment->
bufsz;
857 mbytes = segment->
memsz;
858 maddr = segment->
mem;
862 size_t uchunk, mchunk;
870 ptr += maddr & ~PAGE_MASK;
871 mchunk =
PAGE_SIZE - (maddr & ~PAGE_MASK);
876 if (uchunk > ubytes) {
879 memset(ptr + uchunk, 0, mchunk - uchunk);
897 static int kimage_load_segment(
struct kimage *image,
902 switch (image->type) {
903 case KEXEC_TYPE_DEFAULT:
904 result = kimage_load_normal_segment(image, segment);
906 case KEXEC_TYPE_CRASH:
907 result = kimage_load_crash_segment(image, segment);
942 struct kimage **dest_image, *
image;
984 if (nr_segments > 0) {
988 if ((
flags & KEXEC_ON_CRASH) == 0)
989 result = kimage_normal_alloc(&image, entry,
990 nr_segments, segments);
992 else if (
flags & KEXEC_ON_CRASH) {
996 kimage_free(
xchg(&kexec_crash_image,
NULL));
997 result = kimage_crash_alloc(&image, entry,
998 nr_segments, segments);
1005 image->preserve_context = 1;
1011 result = kimage_load_segment(image, &image->segment[i]);
1015 kimage_terminate(image);
1016 if (
flags & KEXEC_ON_CRASH)
1020 image =
xchg(dest_image, image);
1041 #ifdef CONFIG_COMPAT
1042 asmlinkage long compat_sys_kexec_load(
unsigned long entry,
1043 unsigned long nr_segments,
1044 struct compat_kexec_segment __user *segments,
1045 unsigned long flags)
1047 struct compat_kexec_segment
in;
1066 out.buf = compat_ptr(
in.buf);
1067 out.bufsz =
in.bufsz;
1069 out.memsz =
in.memsz;
1091 if (kexec_crash_image) {
1094 crash_setup_regs(&fixed_regs, regs);
1107 if (crashk_res.
end != crashk_res.
start)
1108 size = resource_size(&crashk_res);
1119 ClearPageReserved(
pfn_to_page(addr >> PAGE_SHIFT));
1135 if (kexec_crash_image) {
1139 start = crashk_res.
start;
1140 end = crashk_res.
end;
1141 old_size = (end == 0) ? 0 : end - start + 1;
1142 if (new_size >= old_size) {
1147 ram_res = kzalloc(
sizeof(*ram_res),
GFP_KERNEL);
1159 if ((start == end) && (crashk_res.
parent !=
NULL))
1163 ram_res->
end = crashk_res.
end;
1165 ram_res->
name =
"System RAM";
1167 crashk_res.
end = end - 1;
1185 memcpy(buf, ¬e,
sizeof(note));
1186 buf += (
sizeof(
note) + 3)/4;
1188 buf += (
note.n_namesz + 3)/4;
1190 buf += (
note.n_descsz + 3)/4;
1195 static void final_note(
u32 *buf)
1202 memcpy(buf, ¬e,
sizeof(note));
1223 memset(&prstatus, 0,
sizeof(prstatus));
1225 elf_core_copy_kernel_regs(&prstatus.
pr_reg, regs);
1226 buf = append_elf_note(buf, KEXEC_CORE_NOTE_NAME,
NT_PRSTATUS,
1227 &prstatus,
sizeof(prstatus));
1231 static int __init crash_notes_memory_init(
void)
1236 printk(
"Kexec: Memory allocation for saving cpu register"
1237 " states failed\n");
1260 unsigned long long system_ram,
1261 unsigned long long *crash_size,
1262 unsigned long long *crash_base)
1264 char *
cur = cmdline, *
tmp;
1273 pr_warning(
"crashkernel: Memory value expected\n");
1288 "value expected\n");
1310 if (size >= system_ram) {
1316 if (system_ram >= start && system_ram < end) {
1320 }
while (*cur++ ==
',');
1322 if (*crash_size > 0) {
1323 while (*cur && *cur !=
' ' && *cur !=
'@')
1347 unsigned long long *crash_size,
1348 unsigned long long *crash_base)
1350 char *
cur = cmdline;
1352 *crash_size =
memparse(cmdline, &cur);
1353 if (cmdline == cur) {
1354 pr_warning(
"crashkernel: memory value expected\n");
1359 *crash_base =
memparse(cur+1, &cur);
1360 else if (*cur !=
' ' && *cur !=
'\0') {
1361 pr_warning(
"crashkernel: unrecognized char\n");
1373 unsigned long long system_ram,
1374 unsigned long long *crash_size,
1375 unsigned long long *crash_base)
1377 char *
p = cmdline, *ck_cmdline =
NULL;
1378 char *first_colon, *first_space;
1380 BUG_ON(!crash_size || !crash_base);
1385 p =
strstr(p,
"crashkernel=");
1388 p =
strstr(p+1,
"crashkernel=");
1400 first_colon =
strchr(ck_cmdline,
':');
1401 first_space =
strchr(ck_cmdline,
' ');
1402 if (first_colon && (!first_space || first_colon < first_space))
1403 return parse_crashkernel_mem(ck_cmdline, system_ram,
1404 crash_size, crash_base);
1406 return parse_crashkernel_simple(ck_cmdline, crash_size,
1413 static void update_vmcoreinfo_note(
void)
1419 buf = append_elf_note(buf, VMCOREINFO_NOTE_NAME, 0, vmcoreinfo_data,
1427 update_vmcoreinfo_note();
1437 r =
vsnprintf(buf,
sizeof(buf), fmt, args);
1443 memcpy(&vmcoreinfo_data[vmcoreinfo_size], buf, r);
1445 vmcoreinfo_size +=
r;
1460 static int __init crash_save_vmcoreinfo_init(
void)
1470 VMCOREINFO_SYMBOL(_stext);
1471 VMCOREINFO_SYMBOL(
vmlist);
1473 #ifndef CONFIG_NEED_MULTIPLE_NODES
1477 #ifdef CONFIG_SPARSEMEM
1478 VMCOREINFO_SYMBOL(mem_section);
1479 VMCOREINFO_LENGTH(mem_section, NR_SECTION_ROOTS);
1480 VMCOREINFO_STRUCT_SIZE(mem_section);
1481 VMCOREINFO_OFFSET(mem_section, section_mem_map);
1483 VMCOREINFO_STRUCT_SIZE(page);
1485 VMCOREINFO_STRUCT_SIZE(
zone);
1489 VMCOREINFO_OFFSET(page, flags);
1490 VMCOREINFO_OFFSET(page, _count);
1491 VMCOREINFO_OFFSET(page,
mapping);
1492 VMCOREINFO_OFFSET(page,
lru);
1495 #ifdef CONFIG_FLAT_NODE_MEM_MAP
1502 VMCOREINFO_OFFSET(
zone, vm_stat);
1503 VMCOREINFO_OFFSET(
zone, spanned_pages);
1504 VMCOREINFO_OFFSET(
free_area, free_list);
1509 log_buf_kexec_setup();
1512 VMCOREINFO_NUMBER(
PG_lru);
1517 update_vmcoreinfo_note();
1528 int kernel_kexec(
void)
1539 #ifdef CONFIG_KEXEC_JUMP
1540 if (kexec_image->preserve_context) {
1541 lock_system_sleep();
1546 goto Restore_console;
1551 goto Resume_console;
1561 goto Resume_devices;
1562 error = disable_nonboot_cpus();
1566 error = syscore_suspend();
1579 #ifdef CONFIG_KEXEC_JUMP
1580 if (kexec_image->preserve_context) {
1585 enable_nonboot_cpus();
1594 unlock_system_sleep();