blob: 4c5edc357923a1b6198c9f8122b90b73b9a5e38f [file] [log] [blame]
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001/*
Dave Young2965faa2015-09-09 15:38:55 -07002 * kexec.c - kexec_load system call
Eric W. Biedermandc009d92005-06-25 14:57:52 -07003 * Copyright (C) 2002-2004 Eric Biederman <ebiederm@xmission.com>
4 *
5 * This source code is licensed under the GNU General Public License,
6 * Version 2. See the file COPYING for more details.
7 */
8
Randy.Dunlapc59ede72006-01-11 12:17:46 -08009#include <linux/capability.h>
Eric W. Biedermandc009d92005-06-25 14:57:52 -070010#include <linux/mm.h>
11#include <linux/file.h>
Eric W. Biedermandc009d92005-06-25 14:57:52 -070012#include <linux/kexec.h>
Andrew Morton8c5a1cf2008-08-15 00:40:27 -070013#include <linux/mutex.h>
Eric W. Biedermandc009d92005-06-25 14:57:52 -070014#include <linux/list.h>
Eric W. Biedermandc009d92005-06-25 14:57:52 -070015#include <linux/syscalls.h>
Dave Younga43cac02015-09-09 15:38:51 -070016#include <linux/vmalloc.h>
Dave Young2965faa2015-09-09 15:38:55 -070017#include <linux/slab.h>
Alexander Nyberg6e274d12005-06-25 14:58:26 -070018
Dave Younga43cac02015-09-09 15:38:51 -070019#include "kexec_internal.h"
20
Vivek Goyaldabe7862014-08-08 14:25:45 -070021static int copy_user_segment_list(struct kimage *image,
22 unsigned long nr_segments,
23 struct kexec_segment __user *segments)
Eric W. Biedermandc009d92005-06-25 14:57:52 -070024{
Vivek Goyaldabe7862014-08-08 14:25:45 -070025 int ret;
Eric W. Biedermandc009d92005-06-25 14:57:52 -070026 size_t segment_bytes;
Eric W. Biedermandc009d92005-06-25 14:57:52 -070027
28 /* Read in the segments */
29 image->nr_segments = nr_segments;
30 segment_bytes = nr_segments * sizeof(*segments);
Vivek Goyaldabe7862014-08-08 14:25:45 -070031 ret = copy_from_user(image->segment, segments, segment_bytes);
32 if (ret)
33 ret = -EFAULT;
34
35 return ret;
36}
37
Vivek Goyal255aedd2014-08-08 14:25:48 -070038static int kimage_alloc_init(struct kimage **rimage, unsigned long entry,
39 unsigned long nr_segments,
40 struct kexec_segment __user *segments,
41 unsigned long flags)
Eric W. Biedermandc009d92005-06-25 14:57:52 -070042{
Vivek Goyal255aedd2014-08-08 14:25:48 -070043 int ret;
Eric W. Biedermandc009d92005-06-25 14:57:52 -070044 struct kimage *image;
Vivek Goyal255aedd2014-08-08 14:25:48 -070045 bool kexec_on_panic = flags & KEXEC_ON_CRASH;
46
47 if (kexec_on_panic) {
48 /* Verify we have a valid entry point */
49 if ((entry < crashk_res.start) || (entry > crashk_res.end))
50 return -EADDRNOTAVAIL;
51 }
Eric W. Biedermandc009d92005-06-25 14:57:52 -070052
53 /* Allocate and initialize a controlling structure */
Vivek Goyaldabe7862014-08-08 14:25:45 -070054 image = do_kimage_alloc_init();
55 if (!image)
56 return -ENOMEM;
57
58 image->start = entry;
59
Vivek Goyal255aedd2014-08-08 14:25:48 -070060 ret = copy_user_segment_list(image, nr_segments, segments);
61 if (ret)
Vivek Goyaldabe7862014-08-08 14:25:45 -070062 goto out_free_image;
63
Vivek Goyal255aedd2014-08-08 14:25:48 -070064 ret = sanity_check_segment_list(image);
65 if (ret)
Vivek Goyaldabe7862014-08-08 14:25:45 -070066 goto out_free_image;
Maneesh Soni72414d32005-06-25 14:58:28 -070067
Vivek Goyal255aedd2014-08-08 14:25:48 -070068 /* Enable the special crash kernel control page allocation policy. */
69 if (kexec_on_panic) {
70 image->control_page = crashk_res.start;
71 image->type = KEXEC_TYPE_CRASH;
72 }
73
Eric W. Biedermandc009d92005-06-25 14:57:52 -070074 /*
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.
78 */
Vivek Goyal255aedd2014-08-08 14:25:48 -070079 ret = -ENOMEM;
Eric W. Biedermandc009d92005-06-25 14:57:52 -070080 image->control_code_page = kimage_alloc_control_pages(image,
Huang Ying163f6872008-08-15 00:40:22 -070081 get_order(KEXEC_CONTROL_PAGE_SIZE));
Eric W. Biedermandc009d92005-06-25 14:57:52 -070082 if (!image->control_code_page) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -070083 pr_err("Could not allocate control_code_buffer\n");
Vivek Goyaldabe7862014-08-08 14:25:45 -070084 goto out_free_image;
Eric W. Biedermandc009d92005-06-25 14:57:52 -070085 }
86
Vivek Goyal255aedd2014-08-08 14:25:48 -070087 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;
92 }
Huang Ying3ab83522008-07-25 19:45:07 -070093 }
94
Zhang Yanfeib92e7e02013-02-27 17:03:29 -080095 *rimage = image;
96 return 0;
Vivek Goyaldabe7862014-08-08 14:25:45 -070097out_free_control_pages:
Zhang Yanfeib92e7e02013-02-27 17:03:29 -080098 kimage_free_page_list(&image->control_pages);
Vivek Goyaldabe7862014-08-08 14:25:45 -070099out_free_image:
Zhang Yanfeib92e7e02013-02-27 17:03:29 -0800100 kfree(image);
Vivek Goyal255aedd2014-08-08 14:25:48 -0700101 return ret;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700102}
103
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700104/*
105 * Exec Kernel system call: for obvious reasons only root may call it.
106 *
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
110 * allocated pages.
111 *
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
115 * reinitialize them.
116 *
117 * - A machine specific part that includes the syscall number
Geert Uytterhoeven002ace72013-09-15 11:35:37 +0200118 * and then copies the image to it's final destination. And
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700119 * jumps into the image at entry.
120 *
121 * kexec does not sync, or unmount filesystems so if you need
122 * that to happen you need to do that yourself.
123 */
Andrew Morton8c5a1cf2008-08-15 00:40:27 -0700124
Heiko Carstens754fe8d2009-01-14 14:14:09 +0100125SYSCALL_DEFINE4(kexec_load, unsigned long, entry, unsigned long, nr_segments,
126 struct kexec_segment __user *, segments, unsigned long, flags)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700127{
128 struct kimage **dest_image, *image;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700129 int result;
130
131 /* We only trust the superuser with rebooting the system. */
Kees Cook79847542014-01-23 15:55:59 -0800132 if (!capable(CAP_SYS_BOOT) || kexec_load_disabled)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700133 return -EPERM;
134
135 /*
136 * Verify we have a legal set of flags
137 * This leaves us room for future extensions.
138 */
139 if ((flags & KEXEC_FLAGS) != (flags & ~KEXEC_ARCH_MASK))
140 return -EINVAL;
141
142 /* Verify we are on the appropriate architecture */
143 if (((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH) &&
144 ((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH_DEFAULT))
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700145 return -EINVAL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700146
147 /* Put an artificial cap on the number
148 * of segments passed to kexec_load.
149 */
150 if (nr_segments > KEXEC_SEGMENT_MAX)
151 return -EINVAL;
152
153 image = NULL;
154 result = 0;
155
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.
161 *
162 * KISS: always take the mutex.
163 */
Andrew Morton8c5a1cf2008-08-15 00:40:27 -0700164 if (!mutex_trylock(&kexec_mutex))
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700165 return -EBUSY;
Maneesh Soni72414d32005-06-25 14:58:28 -0700166
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700167 dest_image = &kexec_image;
Maneesh Soni72414d32005-06-25 14:58:28 -0700168 if (flags & KEXEC_ON_CRASH)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700169 dest_image = &kexec_crash_image;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700170 if (nr_segments > 0) {
171 unsigned long i;
Maneesh Soni72414d32005-06-25 14:58:28 -0700172
Geoff Levand518a0c72015-02-17 13:45:53 -0800173 if (flags & KEXEC_ON_CRASH) {
174 /*
175 * Loading another kernel to switch to if this one
176 * crashes. Free any current crash dump kernel before
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700177 * we corrupt it.
178 */
Geoff Levand518a0c72015-02-17 13:45:53 -0800179
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700180 kimage_free(xchg(&kexec_crash_image, NULL));
Vivek Goyal255aedd2014-08-08 14:25:48 -0700181 result = kimage_alloc_init(&image, entry, nr_segments,
182 segments, flags);
Michael Holzheu558df722011-10-30 15:16:43 +0100183 crash_map_reserved_pages();
Geoff Levand518a0c72015-02-17 13:45:53 -0800184 } else {
185 /* Loading another kernel to reboot into. */
186
187 result = kimage_alloc_init(&image, entry, nr_segments,
188 segments, flags);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700189 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700190 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700191 goto out;
Maneesh Soni72414d32005-06-25 14:58:28 -0700192
Huang Ying3ab83522008-07-25 19:45:07 -0700193 if (flags & KEXEC_PRESERVE_CONTEXT)
194 image->preserve_context = 1;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700195 result = machine_kexec_prepare(image);
Maneesh Soni72414d32005-06-25 14:58:28 -0700196 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700197 goto out;
Maneesh Soni72414d32005-06-25 14:58:28 -0700198
199 for (i = 0; i < nr_segments; i++) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700200 result = kimage_load_segment(image, &image->segment[i]);
Maneesh Soni72414d32005-06-25 14:58:28 -0700201 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700202 goto out;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700203 }
WANG Cong7fccf032008-07-25 19:45:02 -0700204 kimage_terminate(image);
Michael Holzheu558df722011-10-30 15:16:43 +0100205 if (flags & KEXEC_ON_CRASH)
206 crash_unmap_reserved_pages();
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700207 }
208 /* Install the new kernel, and Uninstall the old */
209 image = xchg(dest_image, image);
210
Maneesh Soni72414d32005-06-25 14:58:28 -0700211out:
Andrew Morton8c5a1cf2008-08-15 00:40:27 -0700212 mutex_unlock(&kexec_mutex);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700213 kimage_free(image);
Maneesh Soni72414d32005-06-25 14:58:28 -0700214
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700215 return result;
216}
217
218#ifdef CONFIG_COMPAT
Heiko Carstensca2c4052014-03-04 17:13:42 +0100219COMPAT_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)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700223{
224 struct compat_kexec_segment in;
225 struct kexec_segment out, __user *ksegments;
226 unsigned long i, result;
227
228 /* Don't allow clients that don't understand the native
229 * architecture to do anything.
230 */
Maneesh Soni72414d32005-06-25 14:58:28 -0700231 if ((flags & KEXEC_ARCH_MASK) == KEXEC_ARCH_DEFAULT)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700232 return -EINVAL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700233
Maneesh Soni72414d32005-06-25 14:58:28 -0700234 if (nr_segments > KEXEC_SEGMENT_MAX)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700235 return -EINVAL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700236
237 ksegments = compat_alloc_user_space(nr_segments * sizeof(out));
Fabian Fredericke1bebcf2014-06-06 14:37:09 -0700238 for (i = 0; i < nr_segments; i++) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700239 result = copy_from_user(&in, &segments[i], sizeof(in));
Maneesh Soni72414d32005-06-25 14:58:28 -0700240 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700241 return -EFAULT;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700242
243 out.buf = compat_ptr(in.buf);
244 out.bufsz = in.bufsz;
245 out.mem = in.mem;
246 out.memsz = in.memsz;
247
248 result = copy_to_user(&ksegments[i], &out, sizeof(out));
Maneesh Soni72414d32005-06-25 14:58:28 -0700249 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700250 return -EFAULT;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700251 }
252
253 return sys_kexec_load(entry, nr_segments, ksegments, flags);
254}
255#endif