123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
- #include <linux/capability.h>
- #include <linux/mm.h>
- #include <linux/file.h>
- #include <linux/kexec.h>
- #include <linux/mutex.h>
- #include <linux/list.h>
- #include <linux/syscalls.h>
- #include <linux/vmalloc.h>
- #include <linux/slab.h>
- #include "kexec_internal.h"
- static int copy_user_segment_list(struct kimage *image,
- unsigned long nr_segments,
- struct kexec_segment __user *segments)
- {
- int ret;
- size_t segment_bytes;
-
- image->nr_segments = nr_segments;
- segment_bytes = nr_segments * sizeof(*segments);
- ret = copy_from_user(image->segment, segments, segment_bytes);
- if (ret)
- ret = -EFAULT;
- return ret;
- }
- static int kimage_alloc_init(struct kimage **rimage, unsigned long entry,
- unsigned long nr_segments,
- struct kexec_segment __user *segments,
- unsigned long flags)
- {
- int ret;
- struct kimage *image;
- bool kexec_on_panic = flags & KEXEC_ON_CRASH;
- if (kexec_on_panic) {
-
- if ((entry < phys_to_boot_phys(crashk_res.start)) ||
- (entry > phys_to_boot_phys(crashk_res.end)))
- return -EADDRNOTAVAIL;
- }
-
- image = do_kimage_alloc_init();
- if (!image)
- return -ENOMEM;
- image->start = entry;
- ret = copy_user_segment_list(image, nr_segments, segments);
- if (ret)
- goto out_free_image;
- if (kexec_on_panic) {
-
- image->control_page = crashk_res.start;
- image->type = KEXEC_TYPE_CRASH;
- }
- ret = sanity_check_segment_list(image);
- if (ret)
- goto out_free_image;
-
- ret = -ENOMEM;
- image->control_code_page = kimage_alloc_control_pages(image,
- get_order(KEXEC_CONTROL_PAGE_SIZE));
- if (!image->control_code_page) {
- pr_err("Could not allocate control_code_buffer\n");
- goto out_free_image;
- }
- if (!kexec_on_panic) {
- image->swap_page = kimage_alloc_control_pages(image, 0);
- if (!image->swap_page) {
- pr_err("Could not allocate swap buffer\n");
- goto out_free_control_pages;
- }
- }
- *rimage = image;
- return 0;
- out_free_control_pages:
- kimage_free_page_list(&image->control_pages);
- out_free_image:
- kfree(image);
- return ret;
- }
- static int do_kexec_load(unsigned long entry, unsigned long nr_segments,
- struct kexec_segment __user *segments, unsigned long flags)
- {
- struct kimage **dest_image, *image;
- unsigned long i;
- int ret;
- if (flags & KEXEC_ON_CRASH) {
- dest_image = &kexec_crash_image;
- if (kexec_crash_image)
- arch_kexec_unprotect_crashkres();
- } else {
- dest_image = &kexec_image;
- }
- if (nr_segments == 0) {
-
- kimage_free(xchg(dest_image, NULL));
- return 0;
- }
- if (flags & KEXEC_ON_CRASH) {
-
- kimage_free(xchg(&kexec_crash_image, NULL));
- }
- ret = kimage_alloc_init(&image, entry, nr_segments, segments, flags);
- if (ret)
- return ret;
- if (flags & KEXEC_PRESERVE_CONTEXT)
- image->preserve_context = 1;
- ret = machine_kexec_prepare(image);
- if (ret)
- goto out;
- for (i = 0; i < nr_segments; i++) {
- ret = kimage_load_segment(image, &image->segment[i]);
- if (ret)
- goto out;
- }
- kimage_terminate(image);
-
- image = xchg(dest_image, image);
- out:
- if ((flags & KEXEC_ON_CRASH) && kexec_crash_image)
- arch_kexec_protect_crashkres();
- kimage_free(image);
- return ret;
- }
- SYSCALL_DEFINE4(kexec_load, unsigned long, entry, unsigned long, nr_segments,
- struct kexec_segment __user *, segments, unsigned long, flags)
- {
- int result;
-
- if (!capable(CAP_SYS_BOOT) || kexec_load_disabled)
- return -EPERM;
-
- if ((flags & KEXEC_FLAGS) != (flags & ~KEXEC_ARCH_MASK))
- return -EINVAL;
-
- if (((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH) &&
- ((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH_DEFAULT))
- return -EINVAL;
-
- if (nr_segments > KEXEC_SEGMENT_MAX)
- return -EINVAL;
-
- if (!mutex_trylock(&kexec_mutex))
- return -EBUSY;
- result = do_kexec_load(entry, nr_segments, segments, flags);
- mutex_unlock(&kexec_mutex);
- return result;
- }
- #ifdef CONFIG_COMPAT
- COMPAT_SYSCALL_DEFINE4(kexec_load, compat_ulong_t, entry,
- compat_ulong_t, nr_segments,
- struct compat_kexec_segment __user *, segments,
- compat_ulong_t, flags)
- {
- struct compat_kexec_segment in;
- struct kexec_segment out, __user *ksegments;
- unsigned long i, result;
-
- if ((flags & KEXEC_ARCH_MASK) == KEXEC_ARCH_DEFAULT)
- return -EINVAL;
- if (nr_segments > KEXEC_SEGMENT_MAX)
- return -EINVAL;
- ksegments = compat_alloc_user_space(nr_segments * sizeof(out));
- for (i = 0; i < nr_segments; i++) {
- result = copy_from_user(&in, &segments[i], sizeof(in));
- if (result)
- return -EFAULT;
- out.buf = compat_ptr(in.buf);
- out.bufsz = in.bufsz;
- out.mem = in.mem;
- out.memsz = in.memsz;
- result = copy_to_user(&ksegments[i], &out, sizeof(out));
- if (result)
- return -EFAULT;
- }
- return sys_kexec_load(entry, nr_segments, ksegments, flags);
- }
- #endif
|