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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/capability.h>
13 #include <linux/file.h>
14 #include <linux/kexec.h>
15 #include <linux/mutex.h>
16 #include <linux/list.h>
17 #include <linux/syscalls.h>
18 #include <linux/vmalloc.h>
19 #include <linux/slab.h>
20 #include <linux/module.h>
22 #include "kexec_internal.h"
24 static int copy_user_segment_list(struct kimage
*image
,
25 unsigned long nr_segments
,
26 struct kexec_segment __user
*segments
)
31 /* Read in the segments */
32 image
->nr_segments
= nr_segments
;
33 segment_bytes
= nr_segments
* sizeof(*segments
);
34 ret
= copy_from_user(image
->segment
, segments
, segment_bytes
);
41 static int kimage_alloc_init(struct kimage
**rimage
, unsigned long entry
,
42 unsigned long nr_segments
,
43 struct kexec_segment __user
*segments
,
48 bool kexec_on_panic
= flags
& KEXEC_ON_CRASH
;
51 /* Verify we have a valid entry point */
52 if ((entry
< crashk_res
.start
) || (entry
> crashk_res
.end
))
53 return -EADDRNOTAVAIL
;
56 /* Allocate and initialize a controlling structure */
57 image
= do_kimage_alloc_init();
63 ret
= copy_user_segment_list(image
, nr_segments
, segments
);
67 ret
= sanity_check_segment_list(image
);
71 /* Enable the special crash kernel control page allocation policy. */
73 image
->control_page
= crashk_res
.start
;
74 image
->type
= KEXEC_TYPE_CRASH
;
78 * Find a location for the control code buffer, and add it
79 * the vector of segments so that it's pages will also be
80 * counted as destination pages.
83 image
->control_code_page
= kimage_alloc_control_pages(image
,
84 get_order(KEXEC_CONTROL_PAGE_SIZE
));
85 if (!image
->control_code_page
) {
86 pr_err("Could not allocate control_code_buffer\n");
90 if (!kexec_on_panic
) {
91 image
->swap_page
= kimage_alloc_control_pages(image
, 0);
92 if (!image
->swap_page
) {
93 pr_err("Could not allocate swap buffer\n");
94 goto out_free_control_pages
;
100 out_free_control_pages
:
101 kimage_free_page_list(&image
->control_pages
);
108 * Exec Kernel system call: for obvious reasons only root may call it.
110 * This call breaks up into three pieces.
111 * - A generic part which loads the new kernel from the current
112 * address space, and very carefully places the data in the
115 * - A generic part that interacts with the kernel and tells all of
116 * the devices to shut down. Preventing on-going dmas, and placing
117 * the devices in a consistent state so a later kernel can
120 * - A machine specific part that includes the syscall number
121 * and then copies the image to it's final destination. And
122 * jumps into the image at entry.
124 * kexec does not sync, or unmount filesystems so if you need
125 * that to happen you need to do that yourself.
128 SYSCALL_DEFINE4(kexec_load
, unsigned long, entry
, unsigned long, nr_segments
,
129 struct kexec_segment __user
*, segments
, unsigned long, flags
)
131 struct kimage
**dest_image
, *image
;
134 /* We only trust the superuser with rebooting the system. */
135 if (!capable(CAP_SYS_BOOT
) || kexec_load_disabled
|| secure_modules())
139 * Verify we have a legal set of flags
140 * This leaves us room for future extensions.
142 if ((flags
& KEXEC_FLAGS
) != (flags
& ~KEXEC_ARCH_MASK
))
145 /* Verify we are on the appropriate architecture */
146 if (((flags
& KEXEC_ARCH_MASK
) != KEXEC_ARCH
) &&
147 ((flags
& KEXEC_ARCH_MASK
) != KEXEC_ARCH_DEFAULT
))
150 /* Put an artificial cap on the number
151 * of segments passed to kexec_load.
153 if (nr_segments
> KEXEC_SEGMENT_MAX
)
159 /* Because we write directly to the reserved memory
160 * region when loading crash kernels we need a mutex here to
161 * prevent multiple crash kernels from attempting to load
162 * simultaneously, and to prevent a crash kernel from loading
163 * over the top of a in use crash kernel.
165 * KISS: always take the mutex.
167 if (!mutex_trylock(&kexec_mutex
))
170 dest_image
= &kexec_image
;
171 if (flags
& KEXEC_ON_CRASH
)
172 dest_image
= &kexec_crash_image
;
173 if (nr_segments
> 0) {
176 if (flags
& KEXEC_ON_CRASH
) {
178 * Loading another kernel to switch to if this one
179 * crashes. Free any current crash dump kernel before
183 kimage_free(xchg(&kexec_crash_image
, NULL
));
184 result
= kimage_alloc_init(&image
, entry
, nr_segments
,
186 crash_map_reserved_pages();
188 /* Loading another kernel to reboot into. */
190 result
= kimage_alloc_init(&image
, entry
, nr_segments
,
196 if (flags
& KEXEC_PRESERVE_CONTEXT
)
197 image
->preserve_context
= 1;
198 result
= machine_kexec_prepare(image
);
202 for (i
= 0; i
< nr_segments
; i
++) {
203 result
= kimage_load_segment(image
, &image
->segment
[i
]);
207 kimage_terminate(image
);
208 if (flags
& KEXEC_ON_CRASH
)
209 crash_unmap_reserved_pages();
211 /* Install the new kernel, and Uninstall the old */
212 image
= xchg(dest_image
, image
);
215 mutex_unlock(&kexec_mutex
);
222 COMPAT_SYSCALL_DEFINE4(kexec_load
, compat_ulong_t
, entry
,
223 compat_ulong_t
, nr_segments
,
224 struct compat_kexec_segment __user
*, segments
,
225 compat_ulong_t
, flags
)
227 struct compat_kexec_segment in
;
228 struct kexec_segment out
, __user
*ksegments
;
229 unsigned long i
, result
;
231 /* Don't allow clients that don't understand the native
232 * architecture to do anything.
234 if ((flags
& KEXEC_ARCH_MASK
) == KEXEC_ARCH_DEFAULT
)
237 if (nr_segments
> KEXEC_SEGMENT_MAX
)
240 ksegments
= compat_alloc_user_space(nr_segments
* sizeof(out
));
241 for (i
= 0; i
< nr_segments
; i
++) {
242 result
= copy_from_user(&in
, &segments
[i
], sizeof(in
));
246 out
.buf
= compat_ptr(in
.buf
);
247 out
.bufsz
= in
.bufsz
;
249 out
.memsz
= in
.memsz
;
251 result
= copy_to_user(&ksegments
[i
], &out
, sizeof(out
));
256 return sys_kexec_load(entry
, nr_segments
, ksegments
, flags
);