]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - kernel/kexec.c
UBUNTU: Start new release
[mirror_ubuntu-artful-kernel.git] / kernel / kexec.c
1 /*
2 * kexec.c - kexec_load system call
3 * 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
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
11 #include <linux/capability.h>
12 #include <linux/mm.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>
21
22 #include "kexec_internal.h"
23
24 static int copy_user_segment_list(struct kimage *image,
25 unsigned long nr_segments,
26 struct kexec_segment __user *segments)
27 {
28 int ret;
29 size_t segment_bytes;
30
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);
35 if (ret)
36 ret = -EFAULT;
37
38 return ret;
39 }
40
41 static int kimage_alloc_init(struct kimage **rimage, unsigned long entry,
42 unsigned long nr_segments,
43 struct kexec_segment __user *segments,
44 unsigned long flags)
45 {
46 int ret;
47 struct kimage *image;
48 bool kexec_on_panic = flags & KEXEC_ON_CRASH;
49
50 if (kexec_on_panic) {
51 /* Verify we have a valid entry point */
52 if ((entry < crashk_res.start) || (entry > crashk_res.end))
53 return -EADDRNOTAVAIL;
54 }
55
56 /* Allocate and initialize a controlling structure */
57 image = do_kimage_alloc_init();
58 if (!image)
59 return -ENOMEM;
60
61 image->start = entry;
62
63 ret = copy_user_segment_list(image, nr_segments, segments);
64 if (ret)
65 goto out_free_image;
66
67 ret = sanity_check_segment_list(image);
68 if (ret)
69 goto out_free_image;
70
71 /* Enable the special crash kernel control page allocation policy. */
72 if (kexec_on_panic) {
73 image->control_page = crashk_res.start;
74 image->type = KEXEC_TYPE_CRASH;
75 }
76
77 /*
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.
81 */
82 ret = -ENOMEM;
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");
87 goto out_free_image;
88 }
89
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;
95 }
96 }
97
98 *rimage = image;
99 return 0;
100 out_free_control_pages:
101 kimage_free_page_list(&image->control_pages);
102 out_free_image:
103 kfree(image);
104 return ret;
105 }
106
107 /*
108 * Exec Kernel system call: for obvious reasons only root may call it.
109 *
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
113 * allocated pages.
114 *
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
118 * reinitialize them.
119 *
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.
123 *
124 * kexec does not sync, or unmount filesystems so if you need
125 * that to happen you need to do that yourself.
126 */
127
128 SYSCALL_DEFINE4(kexec_load, unsigned long, entry, unsigned long, nr_segments,
129 struct kexec_segment __user *, segments, unsigned long, flags)
130 {
131 struct kimage **dest_image, *image;
132 int result;
133
134 /* We only trust the superuser with rebooting the system. */
135 if (!capable(CAP_SYS_BOOT) || kexec_load_disabled || secure_modules())
136 return -EPERM;
137
138 /*
139 * Verify we have a legal set of flags
140 * This leaves us room for future extensions.
141 */
142 if ((flags & KEXEC_FLAGS) != (flags & ~KEXEC_ARCH_MASK))
143 return -EINVAL;
144
145 /* Verify we are on the appropriate architecture */
146 if (((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH) &&
147 ((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH_DEFAULT))
148 return -EINVAL;
149
150 /* Put an artificial cap on the number
151 * of segments passed to kexec_load.
152 */
153 if (nr_segments > KEXEC_SEGMENT_MAX)
154 return -EINVAL;
155
156 image = NULL;
157 result = 0;
158
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.
164 *
165 * KISS: always take the mutex.
166 */
167 if (!mutex_trylock(&kexec_mutex))
168 return -EBUSY;
169
170 dest_image = &kexec_image;
171 if (flags & KEXEC_ON_CRASH)
172 dest_image = &kexec_crash_image;
173 if (nr_segments > 0) {
174 unsigned long i;
175
176 if (flags & KEXEC_ON_CRASH) {
177 /*
178 * Loading another kernel to switch to if this one
179 * crashes. Free any current crash dump kernel before
180 * we corrupt it.
181 */
182
183 kimage_free(xchg(&kexec_crash_image, NULL));
184 result = kimage_alloc_init(&image, entry, nr_segments,
185 segments, flags);
186 crash_map_reserved_pages();
187 } else {
188 /* Loading another kernel to reboot into. */
189
190 result = kimage_alloc_init(&image, entry, nr_segments,
191 segments, flags);
192 }
193 if (result)
194 goto out;
195
196 if (flags & KEXEC_PRESERVE_CONTEXT)
197 image->preserve_context = 1;
198 result = machine_kexec_prepare(image);
199 if (result)
200 goto out;
201
202 for (i = 0; i < nr_segments; i++) {
203 result = kimage_load_segment(image, &image->segment[i]);
204 if (result)
205 goto out;
206 }
207 kimage_terminate(image);
208 if (flags & KEXEC_ON_CRASH)
209 crash_unmap_reserved_pages();
210 }
211 /* Install the new kernel, and Uninstall the old */
212 image = xchg(dest_image, image);
213
214 out:
215 mutex_unlock(&kexec_mutex);
216 kimage_free(image);
217
218 return result;
219 }
220
221 #ifdef CONFIG_COMPAT
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)
226 {
227 struct compat_kexec_segment in;
228 struct kexec_segment out, __user *ksegments;
229 unsigned long i, result;
230
231 /* Don't allow clients that don't understand the native
232 * architecture to do anything.
233 */
234 if ((flags & KEXEC_ARCH_MASK) == KEXEC_ARCH_DEFAULT)
235 return -EINVAL;
236
237 if (nr_segments > KEXEC_SEGMENT_MAX)
238 return -EINVAL;
239
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));
243 if (result)
244 return -EFAULT;
245
246 out.buf = compat_ptr(in.buf);
247 out.bufsz = in.bufsz;
248 out.mem = in.mem;
249 out.memsz = in.memsz;
250
251 result = copy_to_user(&ksegments[i], &out, sizeof(out));
252 if (result)
253 return -EFAULT;
254 }
255
256 return sys_kexec_load(entry, nr_segments, ksegments, flags);
257 }
258 #endif