diff options
author | Dave Young <dyoung@redhat.com> | 2015-09-09 18:38:51 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2015-09-10 16:29:01 -0400 |
commit | a43cac0d9dc2073ff2245a171429ddbe1accece7 (patch) | |
tree | 2d4c4e98d70b12a451d5b790cc6a369c7eefe85b /kernel/kexec.c | |
parent | a202fbbf56e819de83876827c4bf5da2bfbac5ec (diff) |
kexec: split kexec_file syscall code to kexec_file.c
Split kexec_file syscall related code to another file kernel/kexec_file.c
so that the #ifdef CONFIG_KEXEC_FILE in kexec.c can be dropped.
Sharing variables and functions are moved to kernel/kexec_internal.h per
suggestion from Vivek and Petr.
[akpm@linux-foundation.org: fix bisectability]
[akpm@linux-foundation.org: declare the various arch_kexec functions]
[akpm@linux-foundation.org: fix build]
Signed-off-by: Dave Young <dyoung@redhat.com>
Cc: Eric W. Biederman <ebiederm@xmission.com>
Cc: Vivek Goyal <vgoyal@redhat.com>
Cc: Petr Tesarik <ptesarik@suse.cz>
Cc: Theodore Ts'o <tytso@mit.edu>
Cc: Josh Boyer <jwboyer@fedoraproject.org>
Cc: David Howells <dhowells@redhat.com>
Cc: Geert Uytterhoeven <geert@linux-m68k.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'kernel/kexec.c')
-rw-r--r-- | kernel/kexec.c | 1056 |
1 files changed, 11 insertions, 1045 deletions
diff --git a/kernel/kexec.c b/kernel/kexec.c index a785c1015e25..2d73ecfa5505 100644 --- a/kernel/kexec.c +++ b/kernel/kexec.c | |||
@@ -28,10 +28,10 @@ | |||
28 | #include <linux/suspend.h> | 28 | #include <linux/suspend.h> |
29 | #include <linux/device.h> | 29 | #include <linux/device.h> |
30 | #include <linux/freezer.h> | 30 | #include <linux/freezer.h> |
31 | #include <linux/vmalloc.h> | ||
31 | #include <linux/pm.h> | 32 | #include <linux/pm.h> |
32 | #include <linux/cpu.h> | 33 | #include <linux/cpu.h> |
33 | #include <linux/console.h> | 34 | #include <linux/console.h> |
34 | #include <linux/vmalloc.h> | ||
35 | #include <linux/swap.h> | 35 | #include <linux/swap.h> |
36 | #include <linux/syscore_ops.h> | 36 | #include <linux/syscore_ops.h> |
37 | #include <linux/compiler.h> | 37 | #include <linux/compiler.h> |
@@ -44,6 +44,9 @@ | |||
44 | 44 | ||
45 | #include <crypto/hash.h> | 45 | #include <crypto/hash.h> |
46 | #include <crypto/sha.h> | 46 | #include <crypto/sha.h> |
47 | #include "kexec_internal.h" | ||
48 | |||
49 | DEFINE_MUTEX(kexec_mutex); | ||
47 | 50 | ||
48 | /* Per cpu memory for storing cpu states in case of system crash. */ | 51 | /* Per cpu memory for storing cpu states in case of system crash. */ |
49 | note_buf_t __percpu *crash_notes; | 52 | note_buf_t __percpu *crash_notes; |
@@ -57,16 +60,6 @@ size_t vmcoreinfo_max_size = sizeof(vmcoreinfo_data); | |||
57 | /* Flag to indicate we are going to kexec a new kernel */ | 60 | /* Flag to indicate we are going to kexec a new kernel */ |
58 | bool kexec_in_progress = false; | 61 | bool kexec_in_progress = false; |
59 | 62 | ||
60 | /* | ||
61 | * Declare these symbols weak so that if architecture provides a purgatory, | ||
62 | * these will be overridden. | ||
63 | */ | ||
64 | char __weak kexec_purgatory[0]; | ||
65 | size_t __weak kexec_purgatory_size = 0; | ||
66 | |||
67 | #ifdef CONFIG_KEXEC_FILE | ||
68 | static int kexec_calculate_store_digests(struct kimage *image); | ||
69 | #endif | ||
70 | 63 | ||
71 | /* Location of the reserved area for the crash kernel */ | 64 | /* Location of the reserved area for the crash kernel */ |
72 | struct resource crashk_res = { | 65 | struct resource crashk_res = { |
@@ -146,8 +139,6 @@ int kexec_should_crash(struct task_struct *p) | |||
146 | */ | 139 | */ |
147 | #define KIMAGE_NO_DEST (-1UL) | 140 | #define KIMAGE_NO_DEST (-1UL) |
148 | 141 | ||
149 | static int kimage_is_destination_range(struct kimage *image, | ||
150 | unsigned long start, unsigned long end); | ||
151 | static struct page *kimage_alloc_page(struct kimage *image, | 142 | static struct page *kimage_alloc_page(struct kimage *image, |
152 | gfp_t gfp_mask, | 143 | gfp_t gfp_mask, |
153 | unsigned long dest); | 144 | unsigned long dest); |
@@ -169,7 +160,7 @@ static int copy_user_segment_list(struct kimage *image, | |||
169 | return ret; | 160 | return ret; |
170 | } | 161 | } |
171 | 162 | ||
172 | static int sanity_check_segment_list(struct kimage *image) | 163 | int sanity_check_segment_list(struct kimage *image) |
173 | { | 164 | { |
174 | int result, i; | 165 | int result, i; |
175 | unsigned long nr_segments = image->nr_segments; | 166 | unsigned long nr_segments = image->nr_segments; |
@@ -259,7 +250,7 @@ static int sanity_check_segment_list(struct kimage *image) | |||
259 | return 0; | 250 | return 0; |
260 | } | 251 | } |
261 | 252 | ||
262 | static struct kimage *do_kimage_alloc_init(void) | 253 | struct kimage *do_kimage_alloc_init(void) |
263 | { | 254 | { |
264 | struct kimage *image; | 255 | struct kimage *image; |
265 | 256 | ||
@@ -286,8 +277,6 @@ static struct kimage *do_kimage_alloc_init(void) | |||
286 | return image; | 277 | return image; |
287 | } | 278 | } |
288 | 279 | ||
289 | static void kimage_free_page_list(struct list_head *list); | ||
290 | |||
291 | static int kimage_alloc_init(struct kimage **rimage, unsigned long entry, | 280 | static int kimage_alloc_init(struct kimage **rimage, unsigned long entry, |
292 | unsigned long nr_segments, | 281 | unsigned long nr_segments, |
293 | struct kexec_segment __user *segments, | 282 | struct kexec_segment __user *segments, |
@@ -354,283 +343,7 @@ out_free_image: | |||
354 | return ret; | 343 | return ret; |
355 | } | 344 | } |
356 | 345 | ||
357 | #ifdef CONFIG_KEXEC_FILE | 346 | int kimage_is_destination_range(struct kimage *image, |
358 | static int copy_file_from_fd(int fd, void **buf, unsigned long *buf_len) | ||
359 | { | ||
360 | struct fd f = fdget(fd); | ||
361 | int ret; | ||
362 | struct kstat stat; | ||
363 | loff_t pos; | ||
364 | ssize_t bytes = 0; | ||
365 | |||
366 | if (!f.file) | ||
367 | return -EBADF; | ||
368 | |||
369 | ret = vfs_getattr(&f.file->f_path, &stat); | ||
370 | if (ret) | ||
371 | goto out; | ||
372 | |||
373 | if (stat.size > INT_MAX) { | ||
374 | ret = -EFBIG; | ||
375 | goto out; | ||
376 | } | ||
377 | |||
378 | /* Don't hand 0 to vmalloc, it whines. */ | ||
379 | if (stat.size == 0) { | ||
380 | ret = -EINVAL; | ||
381 | goto out; | ||
382 | } | ||
383 | |||
384 | *buf = vmalloc(stat.size); | ||
385 | if (!*buf) { | ||
386 | ret = -ENOMEM; | ||
387 | goto out; | ||
388 | } | ||
389 | |||
390 | pos = 0; | ||
391 | while (pos < stat.size) { | ||
392 | bytes = kernel_read(f.file, pos, (char *)(*buf) + pos, | ||
393 | stat.size - pos); | ||
394 | if (bytes < 0) { | ||
395 | vfree(*buf); | ||
396 | ret = bytes; | ||
397 | goto out; | ||
398 | } | ||
399 | |||
400 | if (bytes == 0) | ||
401 | break; | ||
402 | pos += bytes; | ||
403 | } | ||
404 | |||
405 | if (pos != stat.size) { | ||
406 | ret = -EBADF; | ||
407 | vfree(*buf); | ||
408 | goto out; | ||
409 | } | ||
410 | |||
411 | *buf_len = pos; | ||
412 | out: | ||
413 | fdput(f); | ||
414 | return ret; | ||
415 | } | ||
416 | |||
417 | /* Architectures can provide this probe function */ | ||
418 | int __weak arch_kexec_kernel_image_probe(struct kimage *image, void *buf, | ||
419 | unsigned long buf_len) | ||
420 | { | ||
421 | return -ENOEXEC; | ||
422 | } | ||
423 | |||
424 | void * __weak arch_kexec_kernel_image_load(struct kimage *image) | ||
425 | { | ||
426 | return ERR_PTR(-ENOEXEC); | ||
427 | } | ||
428 | |||
429 | void __weak arch_kimage_file_post_load_cleanup(struct kimage *image) | ||
430 | { | ||
431 | } | ||
432 | |||
433 | int __weak arch_kexec_kernel_verify_sig(struct kimage *image, void *buf, | ||
434 | unsigned long buf_len) | ||
435 | { | ||
436 | return -EKEYREJECTED; | ||
437 | } | ||
438 | |||
439 | /* Apply relocations of type RELA */ | ||
440 | int __weak | ||
441 | arch_kexec_apply_relocations_add(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs, | ||
442 | unsigned int relsec) | ||
443 | { | ||
444 | pr_err("RELA relocation unsupported.\n"); | ||
445 | return -ENOEXEC; | ||
446 | } | ||
447 | |||
448 | /* Apply relocations of type REL */ | ||
449 | int __weak | ||
450 | arch_kexec_apply_relocations(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs, | ||
451 | unsigned int relsec) | ||
452 | { | ||
453 | pr_err("REL relocation unsupported.\n"); | ||
454 | return -ENOEXEC; | ||
455 | } | ||
456 | |||
457 | /* | ||
458 | * Free up memory used by kernel, initrd, and command line. This is temporary | ||
459 | * memory allocation which is not needed any more after these buffers have | ||
460 | * been loaded into separate segments and have been copied elsewhere. | ||
461 | */ | ||
462 | static void kimage_file_post_load_cleanup(struct kimage *image) | ||
463 | { | ||
464 | struct purgatory_info *pi = &image->purgatory_info; | ||
465 | |||
466 | vfree(image->kernel_buf); | ||
467 | image->kernel_buf = NULL; | ||
468 | |||
469 | vfree(image->initrd_buf); | ||
470 | image->initrd_buf = NULL; | ||
471 | |||
472 | kfree(image->cmdline_buf); | ||
473 | image->cmdline_buf = NULL; | ||
474 | |||
475 | vfree(pi->purgatory_buf); | ||
476 | pi->purgatory_buf = NULL; | ||
477 | |||
478 | vfree(pi->sechdrs); | ||
479 | pi->sechdrs = NULL; | ||
480 | |||
481 | /* See if architecture has anything to cleanup post load */ | ||
482 | arch_kimage_file_post_load_cleanup(image); | ||
483 | |||
484 | /* | ||
485 | * Above call should have called into bootloader to free up | ||
486 | * any data stored in kimage->image_loader_data. It should | ||
487 | * be ok now to free it up. | ||
488 | */ | ||
489 | kfree(image->image_loader_data); | ||
490 | image->image_loader_data = NULL; | ||
491 | } | ||
492 | |||
493 | /* | ||
494 | * In file mode list of segments is prepared by kernel. Copy relevant | ||
495 | * data from user space, do error checking, prepare segment list | ||
496 | */ | ||
497 | static int | ||
498 | kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd, | ||
499 | const char __user *cmdline_ptr, | ||
500 | unsigned long cmdline_len, unsigned flags) | ||
501 | { | ||
502 | int ret = 0; | ||
503 | void *ldata; | ||
504 | |||
505 | ret = copy_file_from_fd(kernel_fd, &image->kernel_buf, | ||
506 | &image->kernel_buf_len); | ||
507 | if (ret) | ||
508 | return ret; | ||
509 | |||
510 | /* Call arch image probe handlers */ | ||
511 | ret = arch_kexec_kernel_image_probe(image, image->kernel_buf, | ||
512 | image->kernel_buf_len); | ||
513 | |||
514 | if (ret) | ||
515 | goto out; | ||
516 | |||
517 | #ifdef CONFIG_KEXEC_VERIFY_SIG | ||
518 | ret = arch_kexec_kernel_verify_sig(image, image->kernel_buf, | ||
519 | image->kernel_buf_len); | ||
520 | if (ret) { | ||
521 | pr_debug("kernel signature verification failed.\n"); | ||
522 | goto out; | ||
523 | } | ||
524 | pr_debug("kernel signature verification successful.\n"); | ||
525 | #endif | ||
526 | /* It is possible that there no initramfs is being loaded */ | ||
527 | if (!(flags & KEXEC_FILE_NO_INITRAMFS)) { | ||
528 | ret = copy_file_from_fd(initrd_fd, &image->initrd_buf, | ||
529 | &image->initrd_buf_len); | ||
530 | if (ret) | ||
531 | goto out; | ||
532 | } | ||
533 | |||
534 | if (cmdline_len) { | ||
535 | image->cmdline_buf = kzalloc(cmdline_len, GFP_KERNEL); | ||
536 | if (!image->cmdline_buf) { | ||
537 | ret = -ENOMEM; | ||
538 | goto out; | ||
539 | } | ||
540 | |||
541 | ret = copy_from_user(image->cmdline_buf, cmdline_ptr, | ||
542 | cmdline_len); | ||
543 | if (ret) { | ||
544 | ret = -EFAULT; | ||
545 | goto out; | ||
546 | } | ||
547 | |||
548 | image->cmdline_buf_len = cmdline_len; | ||
549 | |||
550 | /* command line should be a string with last byte null */ | ||
551 | if (image->cmdline_buf[cmdline_len - 1] != '\0') { | ||
552 | ret = -EINVAL; | ||
553 | goto out; | ||
554 | } | ||
555 | } | ||
556 | |||
557 | /* Call arch image load handlers */ | ||
558 | ldata = arch_kexec_kernel_image_load(image); | ||
559 | |||
560 | if (IS_ERR(ldata)) { | ||
561 | ret = PTR_ERR(ldata); | ||
562 | goto out; | ||
563 | } | ||
564 | |||
565 | image->image_loader_data = ldata; | ||
566 | out: | ||
567 | /* In case of error, free up all allocated memory in this function */ | ||
568 | if (ret) | ||
569 | kimage_file_post_load_cleanup(image); | ||
570 | return ret; | ||
571 | } | ||
572 | |||
573 | static int | ||
574 | kimage_file_alloc_init(struct kimage **rimage, int kernel_fd, | ||
575 | int initrd_fd, const char __user *cmdline_ptr, | ||
576 | unsigned long cmdline_len, unsigned long flags) | ||
577 | { | ||
578 | int ret; | ||
579 | struct kimage *image; | ||
580 | bool kexec_on_panic = flags & KEXEC_FILE_ON_CRASH; | ||
581 | |||
582 | image = do_kimage_alloc_init(); | ||
583 | if (!image) | ||
584 | return -ENOMEM; | ||
585 | |||
586 | image->file_mode = 1; | ||
587 | |||
588 | if (kexec_on_panic) { | ||
589 | /* Enable special crash kernel control page alloc policy. */ | ||
590 | image->control_page = crashk_res.start; | ||
591 | image->type = KEXEC_TYPE_CRASH; | ||
592 | } | ||
593 | |||
594 | ret = kimage_file_prepare_segments(image, kernel_fd, initrd_fd, | ||
595 | cmdline_ptr, cmdline_len, flags); | ||
596 | if (ret) | ||
597 | goto out_free_image; | ||
598 | |||
599 | ret = sanity_check_segment_list(image); | ||
600 | if (ret) | ||
601 | goto out_free_post_load_bufs; | ||
602 | |||
603 | ret = -ENOMEM; | ||
604 | image->control_code_page = kimage_alloc_control_pages(image, | ||
605 | get_order(KEXEC_CONTROL_PAGE_SIZE)); | ||
606 | if (!image->control_code_page) { | ||
607 | pr_err("Could not allocate control_code_buffer\n"); | ||
608 | goto out_free_post_load_bufs; | ||
609 | } | ||
610 | |||
611 | if (!kexec_on_panic) { | ||
612 | image->swap_page = kimage_alloc_control_pages(image, 0); | ||
613 | if (!image->swap_page) { | ||
614 | pr_err("Could not allocate swap buffer\n"); | ||
615 | goto out_free_control_pages; | ||
616 | } | ||
617 | } | ||
618 | |||
619 | *rimage = image; | ||
620 | return 0; | ||
621 | out_free_control_pages: | ||
622 | kimage_free_page_list(&image->control_pages); | ||
623 | out_free_post_load_bufs: | ||
624 | kimage_file_post_load_cleanup(image); | ||
625 | out_free_image: | ||
626 | kfree(image); | ||
627 | return ret; | ||
628 | } | ||
629 | #else /* CONFIG_KEXEC_FILE */ | ||
630 | static inline void kimage_file_post_load_cleanup(struct kimage *image) { } | ||
631 | #endif /* CONFIG_KEXEC_FILE */ | ||
632 | |||
633 | static int kimage_is_destination_range(struct kimage *image, | ||
634 | unsigned long start, | 347 | unsigned long start, |
635 | unsigned long end) | 348 | unsigned long end) |
636 | { | 349 | { |
@@ -676,7 +389,7 @@ static void kimage_free_pages(struct page *page) | |||
676 | __free_pages(page, order); | 389 | __free_pages(page, order); |
677 | } | 390 | } |
678 | 391 | ||
679 | static void kimage_free_page_list(struct list_head *list) | 392 | void kimage_free_page_list(struct list_head *list) |
680 | { | 393 | { |
681 | struct list_head *pos, *next; | 394 | struct list_head *pos, *next; |
682 | 395 | ||
@@ -892,7 +605,7 @@ static void kimage_free_extra_pages(struct kimage *image) | |||
892 | kimage_free_page_list(&image->unusable_pages); | 605 | kimage_free_page_list(&image->unusable_pages); |
893 | 606 | ||
894 | } | 607 | } |
895 | static void kimage_terminate(struct kimage *image) | 608 | void kimage_terminate(struct kimage *image) |
896 | { | 609 | { |
897 | if (*image->entry != 0) | 610 | if (*image->entry != 0) |
898 | image->entry++; | 611 | image->entry++; |
@@ -913,7 +626,7 @@ static void kimage_free_entry(kimage_entry_t entry) | |||
913 | kimage_free_pages(page); | 626 | kimage_free_pages(page); |
914 | } | 627 | } |
915 | 628 | ||
916 | static void kimage_free(struct kimage *image) | 629 | void kimage_free(struct kimage *image) |
917 | { | 630 | { |
918 | kimage_entry_t *ptr, entry; | 631 | kimage_entry_t *ptr, entry; |
919 | kimage_entry_t ind = 0; | 632 | kimage_entry_t ind = 0; |
@@ -1204,7 +917,7 @@ out: | |||
1204 | return result; | 917 | return result; |
1205 | } | 918 | } |
1206 | 919 | ||
1207 | static int kimage_load_segment(struct kimage *image, | 920 | int kimage_load_segment(struct kimage *image, |
1208 | struct kexec_segment *segment) | 921 | struct kexec_segment *segment) |
1209 | { | 922 | { |
1210 | int result = -ENOMEM; | 923 | int result = -ENOMEM; |
@@ -1245,8 +958,6 @@ struct kimage *kexec_image; | |||
1245 | struct kimage *kexec_crash_image; | 958 | struct kimage *kexec_crash_image; |
1246 | int kexec_load_disabled; | 959 | int kexec_load_disabled; |
1247 | 960 | ||
1248 | static DEFINE_MUTEX(kexec_mutex); | ||
1249 | |||
1250 | SYSCALL_DEFINE4(kexec_load, unsigned long, entry, unsigned long, nr_segments, | 961 | SYSCALL_DEFINE4(kexec_load, unsigned long, entry, unsigned long, nr_segments, |
1251 | struct kexec_segment __user *, segments, unsigned long, flags) | 962 | struct kexec_segment __user *, segments, unsigned long, flags) |
1252 | { | 963 | { |
@@ -1391,85 +1102,6 @@ COMPAT_SYSCALL_DEFINE4(kexec_load, compat_ulong_t, entry, | |||
1391 | } | 1102 | } |
1392 | #endif | 1103 | #endif |
1393 | 1104 | ||
1394 | #ifdef CONFIG_KEXEC_FILE | ||
1395 | SYSCALL_DEFINE5(kexec_file_load, int, kernel_fd, int, initrd_fd, | ||
1396 | unsigned long, cmdline_len, const char __user *, cmdline_ptr, | ||
1397 | unsigned long, flags) | ||
1398 | { | ||
1399 | int ret = 0, i; | ||
1400 | struct kimage **dest_image, *image; | ||
1401 | |||
1402 | /* We only trust the superuser with rebooting the system. */ | ||
1403 | if (!capable(CAP_SYS_BOOT) || kexec_load_disabled) | ||
1404 | return -EPERM; | ||
1405 | |||
1406 | /* Make sure we have a legal set of flags */ | ||
1407 | if (flags != (flags & KEXEC_FILE_FLAGS)) | ||
1408 | return -EINVAL; | ||
1409 | |||
1410 | image = NULL; | ||
1411 | |||
1412 | if (!mutex_trylock(&kexec_mutex)) | ||
1413 | return -EBUSY; | ||
1414 | |||
1415 | dest_image = &kexec_image; | ||
1416 | if (flags & KEXEC_FILE_ON_CRASH) | ||
1417 | dest_image = &kexec_crash_image; | ||
1418 | |||
1419 | if (flags & KEXEC_FILE_UNLOAD) | ||
1420 | goto exchange; | ||
1421 | |||
1422 | /* | ||
1423 | * In case of crash, new kernel gets loaded in reserved region. It is | ||
1424 | * same memory where old crash kernel might be loaded. Free any | ||
1425 | * current crash dump kernel before we corrupt it. | ||
1426 | */ | ||
1427 | if (flags & KEXEC_FILE_ON_CRASH) | ||
1428 | kimage_free(xchg(&kexec_crash_image, NULL)); | ||
1429 | |||
1430 | ret = kimage_file_alloc_init(&image, kernel_fd, initrd_fd, cmdline_ptr, | ||
1431 | cmdline_len, flags); | ||
1432 | if (ret) | ||
1433 | goto out; | ||
1434 | |||
1435 | ret = machine_kexec_prepare(image); | ||
1436 | if (ret) | ||
1437 | goto out; | ||
1438 | |||
1439 | ret = kexec_calculate_store_digests(image); | ||
1440 | if (ret) | ||
1441 | goto out; | ||
1442 | |||
1443 | for (i = 0; i < image->nr_segments; i++) { | ||
1444 | struct kexec_segment *ksegment; | ||
1445 | |||
1446 | ksegment = &image->segment[i]; | ||
1447 | pr_debug("Loading segment %d: buf=0x%p bufsz=0x%zx mem=0x%lx memsz=0x%zx\n", | ||
1448 | i, ksegment->buf, ksegment->bufsz, ksegment->mem, | ||
1449 | ksegment->memsz); | ||
1450 | |||
1451 | ret = kimage_load_segment(image, &image->segment[i]); | ||
1452 | if (ret) | ||
1453 | goto out; | ||
1454 | } | ||
1455 | |||
1456 | kimage_terminate(image); | ||
1457 | |||
1458 | /* | ||
1459 | * Free up any temporary buffers allocated which are not needed | ||
1460 | * after image has been loaded | ||
1461 | */ | ||
1462 | kimage_file_post_load_cleanup(image); | ||
1463 | exchange: | ||
1464 | image = xchg(dest_image, image); | ||
1465 | out: | ||
1466 | mutex_unlock(&kexec_mutex); | ||
1467 | kimage_free(image); | ||
1468 | return ret; | ||
1469 | } | ||
1470 | |||
1471 | #endif /* CONFIG_KEXEC_FILE */ | ||
1472 | |||
1473 | void crash_kexec(struct pt_regs *regs) | 1105 | void crash_kexec(struct pt_regs *regs) |
1474 | { | 1106 | { |
1475 | /* Take the kexec_mutex here to prevent sys_kexec_load | 1107 | /* Take the kexec_mutex here to prevent sys_kexec_load |
@@ -2024,672 +1656,6 @@ static int __init crash_save_vmcoreinfo_init(void) | |||
2024 | 1656 | ||
2025 | subsys_initcall(crash_save_vmcoreinfo_init); | 1657 | subsys_initcall(crash_save_vmcoreinfo_init); |
2026 | 1658 | ||
2027 | #ifdef CONFIG_KEXEC_FILE | ||
2028 | static int locate_mem_hole_top_down(unsigned long start, unsigned long end, | ||
2029 | struct kexec_buf *kbuf) | ||
2030 | { | ||
2031 | struct kimage *image = kbuf->image; | ||
2032 | unsigned long temp_start, temp_end; | ||
2033 | |||
2034 | temp_end = min(end, kbuf->buf_max); | ||
2035 | temp_start = temp_end - kbuf->memsz; | ||
2036 | |||
2037 | do { | ||
2038 | /* align down start */ | ||
2039 | temp_start = temp_start & (~(kbuf->buf_align - 1)); | ||
2040 | |||
2041 | if (temp_start < start || temp_start < kbuf->buf_min) | ||
2042 | return 0; | ||
2043 | |||
2044 | temp_end = temp_start + kbuf->memsz - 1; | ||
2045 | |||
2046 | /* | ||
2047 | * Make sure this does not conflict with any of existing | ||
2048 | * segments | ||
2049 | */ | ||
2050 | if (kimage_is_destination_range(image, temp_start, temp_end)) { | ||
2051 | temp_start = temp_start - PAGE_SIZE; | ||
2052 | continue; | ||
2053 | } | ||
2054 | |||
2055 | /* We found a suitable memory range */ | ||
2056 | break; | ||
2057 | } while (1); | ||
2058 | |||
2059 | /* If we are here, we found a suitable memory range */ | ||
2060 | kbuf->mem = temp_start; | ||
2061 | |||
2062 | /* Success, stop navigating through remaining System RAM ranges */ | ||
2063 | return 1; | ||
2064 | } | ||
2065 | |||
2066 | static int locate_mem_hole_bottom_up(unsigned long start, unsigned long end, | ||
2067 | struct kexec_buf *kbuf) | ||
2068 | { | ||
2069 | struct kimage *image = kbuf->image; | ||
2070 | unsigned long temp_start, temp_end; | ||
2071 | |||
2072 | temp_start = max(start, kbuf->buf_min); | ||
2073 | |||
2074 | do { | ||
2075 | temp_start = ALIGN(temp_start, kbuf->buf_align); | ||
2076 | temp_end = temp_start + kbuf->memsz - 1; | ||
2077 | |||
2078 | if (temp_end > end || temp_end > kbuf->buf_max) | ||
2079 | return 0; | ||
2080 | /* | ||
2081 | * Make sure this does not conflict with any of existing | ||
2082 | * segments | ||
2083 | */ | ||
2084 | if (kimage_is_destination_range(image, temp_start, temp_end)) { | ||
2085 | temp_start = temp_start + PAGE_SIZE; | ||
2086 | continue; | ||
2087 | } | ||
2088 | |||
2089 | /* We found a suitable memory range */ | ||
2090 | break; | ||
2091 | } while (1); | ||
2092 | |||
2093 | /* If we are here, we found a suitable memory range */ | ||
2094 | kbuf->mem = temp_start; | ||
2095 | |||
2096 | /* Success, stop navigating through remaining System RAM ranges */ | ||
2097 | return 1; | ||
2098 | } | ||
2099 | |||
2100 | static int locate_mem_hole_callback(u64 start, u64 end, void *arg) | ||
2101 | { | ||
2102 | struct kexec_buf *kbuf = (struct kexec_buf *)arg; | ||
2103 | unsigned long sz = end - start + 1; | ||
2104 | |||
2105 | /* Returning 0 will take to next memory range */ | ||
2106 | if (sz < kbuf->memsz) | ||
2107 | return 0; | ||
2108 | |||
2109 | if (end < kbuf->buf_min || start > kbuf->buf_max) | ||
2110 | return 0; | ||
2111 | |||
2112 | /* | ||
2113 | * Allocate memory top down with-in ram range. Otherwise bottom up | ||
2114 | * allocation. | ||
2115 | */ | ||
2116 | if (kbuf->top_down) | ||
2117 | return locate_mem_hole_top_down(start, end, kbuf); | ||
2118 | return locate_mem_hole_bottom_up(start, end, kbuf); | ||
2119 | } | ||
2120 | |||
2121 | /* | ||
2122 | * Helper function for placing a buffer in a kexec segment. This assumes | ||
2123 | * that kexec_mutex is held. | ||
2124 | */ | ||
2125 | int kexec_add_buffer(struct kimage *image, char *buffer, unsigned long bufsz, | ||
2126 | unsigned long memsz, unsigned long buf_align, | ||
2127 | unsigned long buf_min, unsigned long buf_max, | ||
2128 | bool top_down, unsigned long *load_addr) | ||
2129 | { | ||
2130 | |||
2131 | struct kexec_segment *ksegment; | ||
2132 | struct kexec_buf buf, *kbuf; | ||
2133 | int ret; | ||
2134 | |||
2135 | /* Currently adding segment this way is allowed only in file mode */ | ||
2136 | if (!image->file_mode) | ||
2137 | return -EINVAL; | ||
2138 | |||
2139 | if (image->nr_segments >= KEXEC_SEGMENT_MAX) | ||
2140 | return -EINVAL; | ||
2141 | |||
2142 | /* | ||
2143 | * Make sure we are not trying to add buffer after allocating | ||
2144 | * control pages. All segments need to be placed first before | ||
2145 | * any control pages are allocated. As control page allocation | ||
2146 | * logic goes through list of segments to make sure there are | ||
2147 | * no destination overlaps. | ||
2148 | */ | ||
2149 | if (!list_empty(&image->control_pages)) { | ||
2150 | WARN_ON(1); | ||
2151 | return -EINVAL; | ||
2152 | } | ||
2153 | |||
2154 | memset(&buf, 0, sizeof(struct kexec_buf)); | ||
2155 | kbuf = &buf; | ||
2156 | kbuf->image = image; | ||
2157 | kbuf->buffer = buffer; | ||
2158 | kbuf->bufsz = bufsz; | ||
2159 | |||
2160 | kbuf->memsz = ALIGN(memsz, PAGE_SIZE); | ||
2161 | kbuf->buf_align = max(buf_align, PAGE_SIZE); | ||
2162 | kbuf->buf_min = buf_min; | ||
2163 | kbuf->buf_max = buf_max; | ||
2164 | kbuf->top_down = top_down; | ||
2165 | |||
2166 | /* Walk the RAM ranges and allocate a suitable range for the buffer */ | ||
2167 | if (image->type == KEXEC_TYPE_CRASH) | ||
2168 | ret = walk_iomem_res("Crash kernel", | ||
2169 | IORESOURCE_MEM | IORESOURCE_BUSY, | ||
2170 | crashk_res.start, crashk_res.end, kbuf, | ||
2171 | locate_mem_hole_callback); | ||
2172 | else | ||
2173 | ret = walk_system_ram_res(0, -1, kbuf, | ||
2174 | locate_mem_hole_callback); | ||
2175 | if (ret != 1) { | ||
2176 | /* A suitable memory range could not be found for buffer */ | ||
2177 | return -EADDRNOTAVAIL; | ||
2178 | } | ||
2179 | |||
2180 | /* Found a suitable memory range */ | ||
2181 | ksegment = &image->segment[image->nr_segments]; | ||
2182 | ksegment->kbuf = kbuf->buffer; | ||
2183 | ksegment->bufsz = kbuf->bufsz; | ||
2184 | ksegment->mem = kbuf->mem; | ||
2185 | ksegment->memsz = kbuf->memsz; | ||
2186 | image->nr_segments++; | ||
2187 | *load_addr = ksegment->mem; | ||
2188 | return 0; | ||
2189 | } | ||
2190 | |||
2191 | /* Calculate and store the digest of segments */ | ||
2192 | static int kexec_calculate_store_digests(struct kimage *image) | ||
2193 | { | ||
2194 | struct crypto_shash *tfm; | ||
2195 | struct shash_desc *desc; | ||
2196 | int ret = 0, i, j, zero_buf_sz, sha_region_sz; | ||
2197 | size_t desc_size, nullsz; | ||
2198 | char *digest; | ||
2199 | void *zero_buf; | ||
2200 | struct kexec_sha_region *sha_regions; | ||
2201 | struct purgatory_info *pi = &image->purgatory_info; | ||
2202 | |||
2203 | zero_buf = __va(page_to_pfn(ZERO_PAGE(0)) << PAGE_SHIFT); | ||
2204 | zero_buf_sz = PAGE_SIZE; | ||
2205 | |||
2206 | tfm = crypto_alloc_shash("sha256", 0, 0); | ||
2207 | if (IS_ERR(tfm)) { | ||
2208 | ret = PTR_ERR(tfm); | ||
2209 | goto out; | ||
2210 | } | ||
2211 | |||
2212 | desc_size = crypto_shash_descsize(tfm) + sizeof(*desc); | ||
2213 | desc = kzalloc(desc_size, GFP_KERNEL); | ||
2214 | if (!desc) { | ||
2215 | ret = -ENOMEM; | ||
2216 | goto out_free_tfm; | ||
2217 | } | ||
2218 | |||
2219 | sha_region_sz = KEXEC_SEGMENT_MAX * sizeof(struct kexec_sha_region); | ||
2220 | sha_regions = vzalloc(sha_region_sz); | ||
2221 | if (!sha_regions) | ||
2222 | goto out_free_desc; | ||
2223 | |||
2224 | desc->tfm = tfm; | ||
2225 | desc->flags = 0; | ||
2226 | |||
2227 | ret = crypto_shash_init(desc); | ||
2228 | if (ret < 0) | ||
2229 | goto out_free_sha_regions; | ||
2230 | |||
2231 | digest = kzalloc(SHA256_DIGEST_SIZE, GFP_KERNEL); | ||
2232 | if (!digest) { | ||
2233 | ret = -ENOMEM; | ||
2234 | goto out_free_sha_regions; | ||
2235 | } | ||
2236 | |||
2237 | for (j = i = 0; i < image->nr_segments; i++) { | ||
2238 | struct kexec_segment *ksegment; | ||
2239 | |||
2240 | ksegment = &image->segment[i]; | ||
2241 | /* | ||
2242 | * Skip purgatory as it will be modified once we put digest | ||
2243 | * info in purgatory. | ||
2244 | */ | ||
2245 | if (ksegment->kbuf == pi->purgatory_buf) | ||
2246 | continue; | ||
2247 | |||
2248 | ret = crypto_shash_update(desc, ksegment->kbuf, | ||
2249 | ksegment->bufsz); | ||
2250 | if (ret) | ||
2251 | break; | ||
2252 | |||
2253 | /* | ||
2254 | * Assume rest of the buffer is filled with zero and | ||
2255 | * update digest accordingly. | ||
2256 | */ | ||
2257 | nullsz = ksegment->memsz - ksegment->bufsz; | ||
2258 | while (nullsz) { | ||
2259 | unsigned long bytes = nullsz; | ||
2260 | |||
2261 | if (bytes > zero_buf_sz) | ||
2262 | bytes = zero_buf_sz; | ||
2263 | ret = crypto_shash_update(desc, zero_buf, bytes); | ||
2264 | if (ret) | ||
2265 | break; | ||
2266 | nullsz -= bytes; | ||
2267 | } | ||
2268 | |||
2269 | if (ret) | ||
2270 | break; | ||
2271 | |||
2272 | sha_regions[j].start = ksegment->mem; | ||
2273 | sha_regions[j].len = ksegment->memsz; | ||
2274 | j++; | ||
2275 | } | ||
2276 | |||
2277 | if (!ret) { | ||
2278 | ret = crypto_shash_final(desc, digest); | ||
2279 | if (ret) | ||
2280 | goto out_free_digest; | ||
2281 | ret = kexec_purgatory_get_set_symbol(image, "sha_regions", | ||
2282 | sha_regions, sha_region_sz, 0); | ||
2283 | if (ret) | ||
2284 | goto out_free_digest; | ||
2285 | |||
2286 | ret = kexec_purgatory_get_set_symbol(image, "sha256_digest", | ||
2287 | digest, SHA256_DIGEST_SIZE, 0); | ||
2288 | if (ret) | ||
2289 | goto out_free_digest; | ||
2290 | } | ||
2291 | |||
2292 | out_free_digest: | ||
2293 | kfree(digest); | ||
2294 | out_free_sha_regions: | ||
2295 | vfree(sha_regions); | ||
2296 | out_free_desc: | ||
2297 | kfree(desc); | ||
2298 | out_free_tfm: | ||
2299 | kfree(tfm); | ||
2300 | out: | ||
2301 | return ret; | ||
2302 | } | ||
2303 | |||
2304 | /* Actually load purgatory. Lot of code taken from kexec-tools */ | ||
2305 | static int __kexec_load_purgatory(struct kimage *image, unsigned long min, | ||
2306 | unsigned long max, int top_down) | ||
2307 | { | ||
2308 | struct purgatory_info *pi = &image->purgatory_info; | ||
2309 | unsigned long align, buf_align, bss_align, buf_sz, bss_sz, bss_pad; | ||
2310 | unsigned long memsz, entry, load_addr, curr_load_addr, bss_addr, offset; | ||
2311 | unsigned char *buf_addr, *src; | ||
2312 | int i, ret = 0, entry_sidx = -1; | ||
2313 | const Elf_Shdr *sechdrs_c; | ||
2314 | Elf_Shdr *sechdrs = NULL; | ||
2315 | void *purgatory_buf = NULL; | ||
2316 | |||
2317 | /* | ||
2318 | * sechdrs_c points to section headers in purgatory and are read | ||
2319 | * only. No modifications allowed. | ||
2320 | */ | ||
2321 | sechdrs_c = (void *)pi->ehdr + pi->ehdr->e_shoff; | ||
2322 | |||
2323 | /* | ||
2324 | * We can not modify sechdrs_c[] and its fields. It is read only. | ||
2325 | * Copy it over to a local copy where one can store some temporary | ||
2326 | * data and free it at the end. We need to modify ->sh_addr and | ||
2327 | * ->sh_offset fields to keep track of permanent and temporary | ||
2328 | * locations of sections. | ||
2329 | */ | ||
2330 | sechdrs = vzalloc(pi->ehdr->e_shnum * sizeof(Elf_Shdr)); | ||
2331 | if (!sechdrs) | ||
2332 | return -ENOMEM; | ||
2333 | |||
2334 | memcpy(sechdrs, sechdrs_c, pi->ehdr->e_shnum * sizeof(Elf_Shdr)); | ||
2335 | |||
2336 | /* | ||
2337 | * We seem to have multiple copies of sections. First copy is which | ||
2338 | * is embedded in kernel in read only section. Some of these sections | ||
2339 | * will be copied to a temporary buffer and relocated. And these | ||
2340 | * sections will finally be copied to their final destination at | ||
2341 | * segment load time. | ||
2342 | * | ||
2343 | * Use ->sh_offset to reflect section address in memory. It will | ||
2344 | * point to original read only copy if section is not allocatable. | ||
2345 | * Otherwise it will point to temporary copy which will be relocated. | ||
2346 | * | ||
2347 | * Use ->sh_addr to contain final address of the section where it | ||
2348 | * will go during execution time. | ||
2349 | */ | ||
2350 | for (i = 0; i < pi->ehdr->e_shnum; i++) { | ||
2351 | if (sechdrs[i].sh_type == SHT_NOBITS) | ||
2352 | continue; | ||
2353 | |||
2354 | sechdrs[i].sh_offset = (unsigned long)pi->ehdr + | ||
2355 | sechdrs[i].sh_offset; | ||
2356 | } | ||
2357 | |||
2358 | /* | ||
2359 | * Identify entry point section and make entry relative to section | ||
2360 | * start. | ||
2361 | */ | ||
2362 | entry = pi->ehdr->e_entry; | ||
2363 | for (i = 0; i < pi->ehdr->e_shnum; i++) { | ||
2364 | if (!(sechdrs[i].sh_flags & SHF_ALLOC)) | ||
2365 | continue; | ||
2366 | |||
2367 | if (!(sechdrs[i].sh_flags & SHF_EXECINSTR)) | ||
2368 | continue; | ||
2369 | |||
2370 | /* Make entry section relative */ | ||
2371 | if (sechdrs[i].sh_addr <= pi->ehdr->e_entry && | ||
2372 | ((sechdrs[i].sh_addr + sechdrs[i].sh_size) > | ||
2373 | pi->ehdr->e_entry)) { | ||
2374 | entry_sidx = i; | ||
2375 | entry -= sechdrs[i].sh_addr; | ||
2376 | break; | ||
2377 | } | ||
2378 | } | ||
2379 | |||
2380 | /* Determine how much memory is needed to load relocatable object. */ | ||
2381 | buf_align = 1; | ||
2382 | bss_align = 1; | ||
2383 | buf_sz = 0; | ||
2384 | bss_sz = 0; | ||
2385 | |||
2386 | for (i = 0; i < pi->ehdr->e_shnum; i++) { | ||
2387 | if (!(sechdrs[i].sh_flags & SHF_ALLOC)) | ||
2388 | continue; | ||
2389 | |||
2390 | align = sechdrs[i].sh_addralign; | ||
2391 | if (sechdrs[i].sh_type != SHT_NOBITS) { | ||
2392 | if (buf_align < align) | ||
2393 | buf_align = align; | ||
2394 | buf_sz = ALIGN(buf_sz, align); | ||
2395 | buf_sz += sechdrs[i].sh_size; | ||
2396 | } else { | ||
2397 | /* bss section */ | ||
2398 | if (bss_align < align) | ||
2399 | bss_align = align; | ||
2400 | bss_sz = ALIGN(bss_sz, align); | ||
2401 | bss_sz += sechdrs[i].sh_size; | ||
2402 | } | ||
2403 | } | ||
2404 | |||
2405 | /* Determine the bss padding required to align bss properly */ | ||
2406 | bss_pad = 0; | ||
2407 | if (buf_sz & (bss_align - 1)) | ||
2408 | bss_pad = bss_align - (buf_sz & (bss_align - 1)); | ||
2409 | |||
2410 | memsz = buf_sz + bss_pad + bss_sz; | ||
2411 | |||
2412 | /* Allocate buffer for purgatory */ | ||
2413 | purgatory_buf = vzalloc(buf_sz); | ||
2414 | if (!purgatory_buf) { | ||
2415 | ret = -ENOMEM; | ||
2416 | goto out; | ||
2417 | } | ||
2418 | |||
2419 | if (buf_align < bss_align) | ||
2420 | buf_align = bss_align; | ||
2421 | |||
2422 | /* Add buffer to segment list */ | ||
2423 | ret = kexec_add_buffer(image, purgatory_buf, buf_sz, memsz, | ||
2424 | buf_align, min, max, top_down, | ||
2425 | &pi->purgatory_load_addr); | ||
2426 | if (ret) | ||
2427 | goto out; | ||
2428 | |||
2429 | /* Load SHF_ALLOC sections */ | ||
2430 | buf_addr = purgatory_buf; | ||
2431 | load_addr = curr_load_addr = pi->purgatory_load_addr; | ||
2432 | bss_addr = load_addr + buf_sz + bss_pad; | ||
2433 | |||
2434 | for (i = 0; i < pi->ehdr->e_shnum; i++) { | ||
2435 | if (!(sechdrs[i].sh_flags & SHF_ALLOC)) | ||
2436 | continue; | ||
2437 | |||
2438 | align = sechdrs[i].sh_addralign; | ||
2439 | if (sechdrs[i].sh_type != SHT_NOBITS) { | ||
2440 | curr_load_addr = ALIGN(curr_load_addr, align); | ||
2441 | offset = curr_load_addr - load_addr; | ||
2442 | /* We already modifed ->sh_offset to keep src addr */ | ||
2443 | src = (char *) sechdrs[i].sh_offset; | ||
2444 | memcpy(buf_addr + offset, src, sechdrs[i].sh_size); | ||
2445 | |||
2446 | /* Store load address and source address of section */ | ||
2447 | sechdrs[i].sh_addr = curr_load_addr; | ||
2448 | |||
2449 | /* | ||
2450 | * This section got copied to temporary buffer. Update | ||
2451 | * ->sh_offset accordingly. | ||
2452 | */ | ||
2453 | sechdrs[i].sh_offset = (unsigned long)(buf_addr + offset); | ||
2454 | |||
2455 | /* Advance to the next address */ | ||
2456 | curr_load_addr += sechdrs[i].sh_size; | ||
2457 | } else { | ||
2458 | bss_addr = ALIGN(bss_addr, align); | ||
2459 | sechdrs[i].sh_addr = bss_addr; | ||
2460 | bss_addr += sechdrs[i].sh_size; | ||
2461 | } | ||
2462 | } | ||
2463 | |||
2464 | /* Update entry point based on load address of text section */ | ||
2465 | if (entry_sidx >= 0) | ||
2466 | entry += sechdrs[entry_sidx].sh_addr; | ||
2467 | |||
2468 | /* Make kernel jump to purgatory after shutdown */ | ||
2469 | image->start = entry; | ||
2470 | |||
2471 | /* Used later to get/set symbol values */ | ||
2472 | pi->sechdrs = sechdrs; | ||
2473 | |||
2474 | /* | ||
2475 | * Used later to identify which section is purgatory and skip it | ||
2476 | * from checksumming. | ||
2477 | */ | ||
2478 | pi->purgatory_buf = purgatory_buf; | ||
2479 | return ret; | ||
2480 | out: | ||
2481 | vfree(sechdrs); | ||
2482 | vfree(purgatory_buf); | ||
2483 | return ret; | ||
2484 | } | ||
2485 | |||
2486 | static int kexec_apply_relocations(struct kimage *image) | ||
2487 | { | ||
2488 | int i, ret; | ||
2489 | struct purgatory_info *pi = &image->purgatory_info; | ||
2490 | Elf_Shdr *sechdrs = pi->sechdrs; | ||
2491 | |||
2492 | /* Apply relocations */ | ||
2493 | for (i = 0; i < pi->ehdr->e_shnum; i++) { | ||
2494 | Elf_Shdr *section, *symtab; | ||
2495 | |||
2496 | if (sechdrs[i].sh_type != SHT_RELA && | ||
2497 | sechdrs[i].sh_type != SHT_REL) | ||
2498 | continue; | ||
2499 | |||
2500 | /* | ||
2501 | * For section of type SHT_RELA/SHT_REL, | ||
2502 | * ->sh_link contains section header index of associated | ||
2503 | * symbol table. And ->sh_info contains section header | ||
2504 | * index of section to which relocations apply. | ||
2505 | */ | ||
2506 | if (sechdrs[i].sh_info >= pi->ehdr->e_shnum || | ||
2507 | sechdrs[i].sh_link >= pi->ehdr->e_shnum) | ||
2508 | return -ENOEXEC; | ||
2509 | |||
2510 | section = &sechdrs[sechdrs[i].sh_info]; | ||
2511 | symtab = &sechdrs[sechdrs[i].sh_link]; | ||
2512 | |||
2513 | if (!(section->sh_flags & SHF_ALLOC)) | ||
2514 | continue; | ||
2515 | |||
2516 | /* | ||
2517 | * symtab->sh_link contain section header index of associated | ||
2518 | * string table. | ||
2519 | */ | ||
2520 | if (symtab->sh_link >= pi->ehdr->e_shnum) | ||
2521 | /* Invalid section number? */ | ||
2522 | continue; | ||
2523 | |||
2524 | /* | ||
2525 | * Respective architecture needs to provide support for applying | ||
2526 | * relocations of type SHT_RELA/SHT_REL. | ||
2527 | */ | ||
2528 | if (sechdrs[i].sh_type == SHT_RELA) | ||
2529 | ret = arch_kexec_apply_relocations_add(pi->ehdr, | ||
2530 | sechdrs, i); | ||
2531 | else if (sechdrs[i].sh_type == SHT_REL) | ||
2532 | ret = arch_kexec_apply_relocations(pi->ehdr, | ||
2533 | sechdrs, i); | ||
2534 | if (ret) | ||
2535 | return ret; | ||
2536 | } | ||
2537 | |||
2538 | return 0; | ||
2539 | } | ||
2540 | |||
2541 | /* Load relocatable purgatory object and relocate it appropriately */ | ||
2542 | int kexec_load_purgatory(struct kimage *image, unsigned long min, | ||
2543 | unsigned long max, int top_down, | ||
2544 | unsigned long *load_addr) | ||
2545 | { | ||
2546 | struct purgatory_info *pi = &image->purgatory_info; | ||
2547 | int ret; | ||
2548 | |||
2549 | if (kexec_purgatory_size <= 0) | ||
2550 | return -EINVAL; | ||
2551 | |||
2552 | if (kexec_purgatory_size < sizeof(Elf_Ehdr)) | ||
2553 | return -ENOEXEC; | ||
2554 | |||
2555 | pi->ehdr = (Elf_Ehdr *)kexec_purgatory; | ||
2556 | |||
2557 | if (memcmp(pi->ehdr->e_ident, ELFMAG, SELFMAG) != 0 | ||
2558 | || pi->ehdr->e_type != ET_REL | ||
2559 | || !elf_check_arch(pi->ehdr) | ||
2560 | || pi->ehdr->e_shentsize != sizeof(Elf_Shdr)) | ||
2561 | return -ENOEXEC; | ||
2562 | |||
2563 | if (pi->ehdr->e_shoff >= kexec_purgatory_size | ||
2564 | || (pi->ehdr->e_shnum * sizeof(Elf_Shdr) > | ||
2565 | kexec_purgatory_size - pi->ehdr->e_shoff)) | ||
2566 | return -ENOEXEC; | ||
2567 | |||
2568 | ret = __kexec_load_purgatory(image, min, max, top_down); | ||
2569 | if (ret) | ||
2570 | return ret; | ||
2571 | |||
2572 | ret = kexec_apply_relocations(image); | ||
2573 | if (ret) | ||
2574 | goto out; | ||
2575 | |||
2576 | *load_addr = pi->purgatory_load_addr; | ||
2577 | return 0; | ||
2578 | out: | ||
2579 | vfree(pi->sechdrs); | ||
2580 | vfree(pi->purgatory_buf); | ||
2581 | return ret; | ||
2582 | } | ||
2583 | |||
2584 | static Elf_Sym *kexec_purgatory_find_symbol(struct purgatory_info *pi, | ||
2585 | const char *name) | ||
2586 | { | ||
2587 | Elf_Sym *syms; | ||
2588 | Elf_Shdr *sechdrs; | ||
2589 | Elf_Ehdr *ehdr; | ||
2590 | int i, k; | ||
2591 | const char *strtab; | ||
2592 | |||
2593 | if (!pi->sechdrs || !pi->ehdr) | ||
2594 | return NULL; | ||
2595 | |||
2596 | sechdrs = pi->sechdrs; | ||
2597 | ehdr = pi->ehdr; | ||
2598 | |||
2599 | for (i = 0; i < ehdr->e_shnum; i++) { | ||
2600 | if (sechdrs[i].sh_type != SHT_SYMTAB) | ||
2601 | continue; | ||
2602 | |||
2603 | if (sechdrs[i].sh_link >= ehdr->e_shnum) | ||
2604 | /* Invalid strtab section number */ | ||
2605 | continue; | ||
2606 | strtab = (char *)sechdrs[sechdrs[i].sh_link].sh_offset; | ||
2607 | syms = (Elf_Sym *)sechdrs[i].sh_offset; | ||
2608 | |||
2609 | /* Go through symbols for a match */ | ||
2610 | for (k = 0; k < sechdrs[i].sh_size/sizeof(Elf_Sym); k++) { | ||
2611 | if (ELF_ST_BIND(syms[k].st_info) != STB_GLOBAL) | ||
2612 | continue; | ||
2613 | |||
2614 | if (strcmp(strtab + syms[k].st_name, name) != 0) | ||
2615 | continue; | ||
2616 | |||
2617 | if (syms[k].st_shndx == SHN_UNDEF || | ||
2618 | syms[k].st_shndx >= ehdr->e_shnum) { | ||
2619 | pr_debug("Symbol: %s has bad section index %d.\n", | ||
2620 | name, syms[k].st_shndx); | ||
2621 | return NULL; | ||
2622 | } | ||
2623 | |||
2624 | /* Found the symbol we are looking for */ | ||
2625 | return &syms[k]; | ||
2626 | } | ||
2627 | } | ||
2628 | |||
2629 | return NULL; | ||
2630 | } | ||
2631 | |||
2632 | void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name) | ||
2633 | { | ||
2634 | struct purgatory_info *pi = &image->purgatory_info; | ||
2635 | Elf_Sym *sym; | ||
2636 | Elf_Shdr *sechdr; | ||
2637 | |||
2638 | sym = kexec_purgatory_find_symbol(pi, name); | ||
2639 | if (!sym) | ||
2640 | return ERR_PTR(-EINVAL); | ||
2641 | |||
2642 | sechdr = &pi->sechdrs[sym->st_shndx]; | ||
2643 | |||
2644 | /* | ||
2645 | * Returns the address where symbol will finally be loaded after | ||
2646 | * kexec_load_segment() | ||
2647 | */ | ||
2648 | return (void *)(sechdr->sh_addr + sym->st_value); | ||
2649 | } | ||
2650 | |||
2651 | /* | ||
2652 | * Get or set value of a symbol. If "get_value" is true, symbol value is | ||
2653 | * returned in buf otherwise symbol value is set based on value in buf. | ||
2654 | */ | ||
2655 | int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name, | ||
2656 | void *buf, unsigned int size, bool get_value) | ||
2657 | { | ||
2658 | Elf_Sym *sym; | ||
2659 | Elf_Shdr *sechdrs; | ||
2660 | struct purgatory_info *pi = &image->purgatory_info; | ||
2661 | char *sym_buf; | ||
2662 | |||
2663 | sym = kexec_purgatory_find_symbol(pi, name); | ||
2664 | if (!sym) | ||
2665 | return -EINVAL; | ||
2666 | |||
2667 | if (sym->st_size != size) { | ||
2668 | pr_err("symbol %s size mismatch: expected %lu actual %u\n", | ||
2669 | name, (unsigned long)sym->st_size, size); | ||
2670 | return -EINVAL; | ||
2671 | } | ||
2672 | |||
2673 | sechdrs = pi->sechdrs; | ||
2674 | |||
2675 | if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) { | ||
2676 | pr_err("symbol %s is in a bss section. Cannot %s\n", name, | ||
2677 | get_value ? "get" : "set"); | ||
2678 | return -EINVAL; | ||
2679 | } | ||
2680 | |||
2681 | sym_buf = (unsigned char *)sechdrs[sym->st_shndx].sh_offset + | ||
2682 | sym->st_value; | ||
2683 | |||
2684 | if (get_value) | ||
2685 | memcpy((void *)buf, sym_buf, size); | ||
2686 | else | ||
2687 | memcpy((void *)sym_buf, buf, size); | ||
2688 | |||
2689 | return 0; | ||
2690 | } | ||
2691 | #endif /* CONFIG_KEXEC_FILE */ | ||
2692 | |||
2693 | /* | 1659 | /* |
2694 | * Move into place and start executing a preloaded standalone | 1660 | * Move into place and start executing a preloaded standalone |
2695 | * executable. If nothing was preloaded return an error. | 1661 | * executable. If nothing was preloaded return an error. |