1 // SPDX-License-Identifier: GPL-2.0-only
3 * kexec.c - kexec_load system call
4 * Copyright (C) 2002-2004 Eric Biederman <ebiederm@xmission.com>
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 #include <linux/capability.h>
11 #include <linux/file.h>
12 #include <linux/security.h>
13 #include <linux/kexec.h>
14 #include <linux/mutex.h>
15 #include <linux/list.h>
16 #include <linux/syscalls.h>
17 #include <linux/vmalloc.h>
18 #include <linux/slab.h>
20 #include "kexec_internal.h"
22 static int kimage_alloc_init(struct kimage **rimage, unsigned long entry,
23 unsigned long nr_segments,
24 struct kexec_segment *segments,
29 bool kexec_on_panic = flags & KEXEC_ON_CRASH;
32 /* Verify we have a valid entry point */
33 if ((entry < phys_to_boot_phys(crashk_res.start)) ||
34 (entry > phys_to_boot_phys(crashk_res.end)))
35 return -EADDRNOTAVAIL;
38 /* Allocate and initialize a controlling structure */
39 image = do_kimage_alloc_init();
44 image->nr_segments = nr_segments;
45 memcpy(image->segment, segments, nr_segments * sizeof(*segments));
48 /* Enable special crash kernel control page alloc policy. */
49 image->control_page = crashk_res.start;
50 image->type = KEXEC_TYPE_CRASH;
53 ret = sanity_check_segment_list(image);
58 * Find a location for the control code buffer, and add it
59 * the vector of segments so that it's pages will also be
60 * counted as destination pages.
63 image->control_code_page = kimage_alloc_control_pages(image,
64 get_order(KEXEC_CONTROL_PAGE_SIZE));
65 if (!image->control_code_page) {
66 pr_err("Could not allocate control_code_buffer\n");
70 if (!kexec_on_panic) {
71 image->swap_page = kimage_alloc_control_pages(image, 0);
72 if (!image->swap_page) {
73 pr_err("Could not allocate swap buffer\n");
74 goto out_free_control_pages;
80 out_free_control_pages:
81 kimage_free_page_list(&image->control_pages);
87 static int do_kexec_load(unsigned long entry, unsigned long nr_segments,
88 struct kexec_segment *segments, unsigned long flags)
90 struct kimage **dest_image, *image;
95 * Because we write directly to the reserved memory region when loading
96 * crash kernels we need a serialization here to prevent multiple crash
97 * kernels from attempting to load simultaneously.
102 if (flags & KEXEC_ON_CRASH) {
103 dest_image = &kexec_crash_image;
104 if (kexec_crash_image)
105 arch_kexec_unprotect_crashkres();
107 dest_image = &kexec_image;
110 if (nr_segments == 0) {
111 /* Uninstall image */
112 kimage_free(xchg(dest_image, NULL));
116 if (flags & KEXEC_ON_CRASH) {
118 * Loading another kernel to switch to if this one
119 * crashes. Free any current crash dump kernel before
122 kimage_free(xchg(&kexec_crash_image, NULL));
125 ret = kimage_alloc_init(&image, entry, nr_segments, segments, flags);
129 if (flags & KEXEC_PRESERVE_CONTEXT)
130 image->preserve_context = 1;
132 ret = machine_kexec_prepare(image);
137 * Some architecture(like S390) may touch the crash memory before
138 * machine_kexec_prepare(), we must copy vmcoreinfo data after it.
140 ret = kimage_crash_copy_vmcoreinfo(image);
144 for (i = 0; i < nr_segments; i++) {
145 ret = kimage_load_segment(image, &image->segment[i]);
150 kimage_terminate(image);
152 ret = machine_kexec_post_load(image);
156 /* Install the new kernel and uninstall the old */
157 image = xchg(dest_image, image);
160 if ((flags & KEXEC_ON_CRASH) && kexec_crash_image)
161 arch_kexec_protect_crashkres();
170 * Exec Kernel system call: for obvious reasons only root may call it.
172 * This call breaks up into three pieces.
173 * - A generic part which loads the new kernel from the current
174 * address space, and very carefully places the data in the
177 * - A generic part that interacts with the kernel and tells all of
178 * the devices to shut down. Preventing on-going dmas, and placing
179 * the devices in a consistent state so a later kernel can
182 * - A machine specific part that includes the syscall number
183 * and then copies the image to it's final destination. And
184 * jumps into the image at entry.
186 * kexec does not sync, or unmount filesystems so if you need
187 * that to happen you need to do that yourself.
190 static inline int kexec_load_check(unsigned long nr_segments,
195 /* We only trust the superuser with rebooting the system. */
196 if (!capable(CAP_SYS_BOOT) || kexec_load_disabled)
199 /* Permit LSMs and IMA to fail the kexec */
200 result = security_kernel_load_data(LOADING_KEXEC_IMAGE, false);
205 * kexec can be used to circumvent module loading restrictions, so
206 * prevent loading in that case
208 result = security_locked_down(LOCKDOWN_KEXEC);
213 * Verify we have a legal set of flags
214 * This leaves us room for future extensions.
216 if ((flags & KEXEC_FLAGS) != (flags & ~KEXEC_ARCH_MASK))
219 /* Put an artificial cap on the number
220 * of segments passed to kexec_load.
222 if (nr_segments > KEXEC_SEGMENT_MAX)
228 SYSCALL_DEFINE4(kexec_load, unsigned long, entry, unsigned long, nr_segments,
229 struct kexec_segment __user *, segments, unsigned long, flags)
231 struct kexec_segment *ksegments;
232 unsigned long result;
234 result = kexec_load_check(nr_segments, flags);
238 /* Verify we are on the appropriate architecture */
239 if (((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH) &&
240 ((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH_DEFAULT))
243 ksegments = memdup_user(segments, nr_segments * sizeof(ksegments[0]));
244 if (IS_ERR(ksegments))
245 return PTR_ERR(ksegments);
247 result = do_kexec_load(entry, nr_segments, ksegments, flags);
254 COMPAT_SYSCALL_DEFINE4(kexec_load, compat_ulong_t, entry,
255 compat_ulong_t, nr_segments,
256 struct compat_kexec_segment __user *, segments,
257 compat_ulong_t, flags)
259 struct compat_kexec_segment in;
260 struct kexec_segment *ksegments;
261 unsigned long i, result;
263 result = kexec_load_check(nr_segments, flags);
267 /* Don't allow clients that don't understand the native
268 * architecture to do anything.
270 if ((flags & KEXEC_ARCH_MASK) == KEXEC_ARCH_DEFAULT)
273 ksegments = kmalloc_array(nr_segments, sizeof(ksegments[0]),
278 for (i = 0; i < nr_segments; i++) {
279 result = copy_from_user(&in, &segments[i], sizeof(in));
283 ksegments[i].buf = compat_ptr(in.buf);
284 ksegments[i].bufsz = in.bufsz;
285 ksegments[i].mem = in.mem;
286 ksegments[i].memsz = in.memsz;
289 result = do_kexec_load(entry, nr_segments, ksegments, flags);