2 * kexec.c - kexec_load system call
3 * Copyright (C) 2002-2004 Eric Biederman <ebiederm@xmission.com>
5 * This source code is licensed under the GNU General Public License,
6 * Version 2. See the file COPYING for more details.
9 #include <linux/capability.h>
11 #include <linux/file.h>
12 #include <linux/kexec.h>
13 #include <linux/mutex.h>
14 #include <linux/list.h>
15 #include <linux/syscalls.h>
16 #include <linux/vmalloc.h>
17 #include <linux/slab.h>
19 #include "kexec_internal.h"
21 static int copy_user_segment_list(struct kimage *image,
22 unsigned long nr_segments,
23 struct kexec_segment __user *segments)
28 /* Read in the segments */
29 image->nr_segments = nr_segments;
30 segment_bytes = nr_segments * sizeof(*segments);
31 ret = copy_from_user(image->segment, segments, segment_bytes);
38 static int kimage_alloc_init(struct kimage **rimage, unsigned long entry,
39 unsigned long nr_segments,
40 struct kexec_segment __user *segments,
45 bool kexec_on_panic = flags & KEXEC_ON_CRASH;
48 /* Verify we have a valid entry point */
49 if ((entry < crashk_res.start) || (entry > crashk_res.end))
50 return -EADDRNOTAVAIL;
53 /* Allocate and initialize a controlling structure */
54 image = do_kimage_alloc_init();
60 ret = copy_user_segment_list(image, nr_segments, segments);
64 ret = sanity_check_segment_list(image);
68 /* Enable the special crash kernel control page allocation policy. */
70 image->control_page = crashk_res.start;
71 image->type = KEXEC_TYPE_CRASH;
75 * Find a location for the control code buffer, and add it
76 * the vector of segments so that it's pages will also be
77 * counted as destination pages.
80 image->control_code_page = kimage_alloc_control_pages(image,
81 get_order(KEXEC_CONTROL_PAGE_SIZE));
82 if (!image->control_code_page) {
83 pr_err("Could not allocate control_code_buffer\n");
87 if (!kexec_on_panic) {
88 image->swap_page = kimage_alloc_control_pages(image, 0);
89 if (!image->swap_page) {
90 pr_err("Could not allocate swap buffer\n");
91 goto out_free_control_pages;
97 out_free_control_pages:
98 kimage_free_page_list(&image->control_pages);
105 * Exec Kernel system call: for obvious reasons only root may call it.
107 * This call breaks up into three pieces.
108 * - A generic part which loads the new kernel from the current
109 * address space, and very carefully places the data in the
112 * - A generic part that interacts with the kernel and tells all of
113 * the devices to shut down. Preventing on-going dmas, and placing
114 * the devices in a consistent state so a later kernel can
117 * - A machine specific part that includes the syscall number
118 * and then copies the image to it's final destination. And
119 * jumps into the image at entry.
121 * kexec does not sync, or unmount filesystems so if you need
122 * that to happen you need to do that yourself.
125 SYSCALL_DEFINE4(kexec_load, unsigned long, entry, unsigned long, nr_segments,
126 struct kexec_segment __user *, segments, unsigned long, flags)
128 struct kimage **dest_image, *image;
131 /* We only trust the superuser with rebooting the system. */
132 if (!capable(CAP_SYS_BOOT) || kexec_load_disabled)
136 * Verify we have a legal set of flags
137 * This leaves us room for future extensions.
139 if ((flags & KEXEC_FLAGS) != (flags & ~KEXEC_ARCH_MASK))
142 /* Verify we are on the appropriate architecture */
143 if (((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH) &&
144 ((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH_DEFAULT))
147 /* Put an artificial cap on the number
148 * of segments passed to kexec_load.
150 if (nr_segments > KEXEC_SEGMENT_MAX)
156 /* Because we write directly to the reserved memory
157 * region when loading crash kernels we need a mutex here to
158 * prevent multiple crash kernels from attempting to load
159 * simultaneously, and to prevent a crash kernel from loading
160 * over the top of a in use crash kernel.
162 * KISS: always take the mutex.
164 if (!mutex_trylock(&kexec_mutex))
167 dest_image = &kexec_image;
168 if (flags & KEXEC_ON_CRASH)
169 dest_image = &kexec_crash_image;
170 if (nr_segments > 0) {
173 if (flags & KEXEC_ON_CRASH) {
175 * Loading another kernel to switch to if this one
176 * crashes. Free any current crash dump kernel before
180 kimage_free(xchg(&kexec_crash_image, NULL));
181 result = kimage_alloc_init(&image, entry, nr_segments,
183 crash_map_reserved_pages();
185 /* Loading another kernel to reboot into. */
187 result = kimage_alloc_init(&image, entry, nr_segments,
193 if (flags & KEXEC_PRESERVE_CONTEXT)
194 image->preserve_context = 1;
195 result = machine_kexec_prepare(image);
199 for (i = 0; i < nr_segments; i++) {
200 result = kimage_load_segment(image, &image->segment[i]);
204 kimage_terminate(image);
205 if (flags & KEXEC_ON_CRASH)
206 crash_unmap_reserved_pages();
208 /* Install the new kernel, and Uninstall the old */
209 image = xchg(dest_image, image);
212 mutex_unlock(&kexec_mutex);
219 COMPAT_SYSCALL_DEFINE4(kexec_load, compat_ulong_t, entry,
220 compat_ulong_t, nr_segments,
221 struct compat_kexec_segment __user *, segments,
222 compat_ulong_t, flags)
224 struct compat_kexec_segment in;
225 struct kexec_segment out, __user *ksegments;
226 unsigned long i, result;
228 /* Don't allow clients that don't understand the native
229 * architecture to do anything.
231 if ((flags & KEXEC_ARCH_MASK) == KEXEC_ARCH_DEFAULT)
234 if (nr_segments > KEXEC_SEGMENT_MAX)
237 ksegments = compat_alloc_user_space(nr_segments * sizeof(out));
238 for (i = 0; i < nr_segments; i++) {
239 result = copy_from_user(&in, &segments[i], sizeof(in));
243 out.buf = compat_ptr(in.buf);
244 out.bufsz = in.bufsz;
246 out.memsz = in.memsz;
248 result = copy_to_user(&ksegments[i], &out, sizeof(out));
253 return sys_kexec_load(entry, nr_segments, ksegments, flags);