]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - kernel/kexec.c
UBUNTU: Start new release
[mirror_ubuntu-artful-kernel.git] / kernel / kexec.c
CommitLineData
dc009d92 1/*
2965faa5 2 * kexec.c - kexec_load system call
dc009d92
EB
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
de90a6bc
MH
9#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
c59ede7b 11#include <linux/capability.h>
dc009d92
EB
12#include <linux/mm.h>
13#include <linux/file.h>
dc009d92 14#include <linux/kexec.h>
8c5a1cf0 15#include <linux/mutex.h>
dc009d92 16#include <linux/list.h>
dc009d92 17#include <linux/syscalls.h>
a43cac0d 18#include <linux/vmalloc.h>
2965faa5 19#include <linux/slab.h>
17c953d3 20#include <linux/module.h>
dc009d92 21
a43cac0d
DY
22#include "kexec_internal.h"
23
dabe7862
VG
24static int copy_user_segment_list(struct kimage *image,
25 unsigned long nr_segments,
26 struct kexec_segment __user *segments)
dc009d92 27{
dabe7862 28 int ret;
dc009d92 29 size_t segment_bytes;
dc009d92
EB
30
31 /* Read in the segments */
32 image->nr_segments = nr_segments;
33 segment_bytes = nr_segments * sizeof(*segments);
dabe7862
VG
34 ret = copy_from_user(image->segment, segments, segment_bytes);
35 if (ret)
36 ret = -EFAULT;
37
38 return ret;
39}
40
255aedd9
VG
41static 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)
dc009d92 45{
255aedd9 46 int ret;
dc009d92 47 struct kimage *image;
255aedd9
VG
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 }
dc009d92
EB
55
56 /* Allocate and initialize a controlling structure */
dabe7862
VG
57 image = do_kimage_alloc_init();
58 if (!image)
59 return -ENOMEM;
60
61 image->start = entry;
62
255aedd9
VG
63 ret = copy_user_segment_list(image, nr_segments, segments);
64 if (ret)
dabe7862
VG
65 goto out_free_image;
66
255aedd9
VG
67 ret = sanity_check_segment_list(image);
68 if (ret)
dabe7862 69 goto out_free_image;
72414d3f 70
255aedd9
VG
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
dc009d92
EB
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 */
255aedd9 82 ret = -ENOMEM;
dc009d92 83 image->control_code_page = kimage_alloc_control_pages(image,
163f6876 84 get_order(KEXEC_CONTROL_PAGE_SIZE));
dc009d92 85 if (!image->control_code_page) {
e1bebcf4 86 pr_err("Could not allocate control_code_buffer\n");
dabe7862 87 goto out_free_image;
dc009d92
EB
88 }
89
255aedd9
VG
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 }
3ab83521
HY
96 }
97
b92e7e0d
ZY
98 *rimage = image;
99 return 0;
dabe7862 100out_free_control_pages:
b92e7e0d 101 kimage_free_page_list(&image->control_pages);
dabe7862 102out_free_image:
b92e7e0d 103 kfree(image);
255aedd9 104 return ret;
dc009d92
EB
105}
106
dc009d92
EB
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
002ace78 121 * and then copies the image to it's final destination. And
dc009d92
EB
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 */
8c5a1cf0 127
754fe8d2
HC
128SYSCALL_DEFINE4(kexec_load, unsigned long, entry, unsigned long, nr_segments,
129 struct kexec_segment __user *, segments, unsigned long, flags)
dc009d92
EB
130{
131 struct kimage **dest_image, *image;
dc009d92
EB
132 int result;
133
134 /* We only trust the superuser with rebooting the system. */
17c953d3 135 if (!capable(CAP_SYS_BOOT) || kexec_load_disabled || secure_modules())
dc009d92
EB
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))
dc009d92 148 return -EINVAL;
dc009d92
EB
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 */
8c5a1cf0 167 if (!mutex_trylock(&kexec_mutex))
dc009d92 168 return -EBUSY;
72414d3f 169
dc009d92 170 dest_image = &kexec_image;
72414d3f 171 if (flags & KEXEC_ON_CRASH)
dc009d92 172 dest_image = &kexec_crash_image;
dc009d92
EB
173 if (nr_segments > 0) {
174 unsigned long i;
72414d3f 175
518a0c71
GL
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
dc009d92
EB
180 * we corrupt it.
181 */
518a0c71 182
dc009d92 183 kimage_free(xchg(&kexec_crash_image, NULL));
255aedd9
VG
184 result = kimage_alloc_init(&image, entry, nr_segments,
185 segments, flags);
558df720 186 crash_map_reserved_pages();
518a0c71
GL
187 } else {
188 /* Loading another kernel to reboot into. */
189
190 result = kimage_alloc_init(&image, entry, nr_segments,
191 segments, flags);
dc009d92 192 }
72414d3f 193 if (result)
dc009d92 194 goto out;
72414d3f 195
3ab83521
HY
196 if (flags & KEXEC_PRESERVE_CONTEXT)
197 image->preserve_context = 1;
dc009d92 198 result = machine_kexec_prepare(image);
72414d3f 199 if (result)
dc009d92 200 goto out;
72414d3f
MS
201
202 for (i = 0; i < nr_segments; i++) {
dc009d92 203 result = kimage_load_segment(image, &image->segment[i]);
72414d3f 204 if (result)
dc009d92 205 goto out;
dc009d92 206 }
7fccf032 207 kimage_terminate(image);
558df720
MH
208 if (flags & KEXEC_ON_CRASH)
209 crash_unmap_reserved_pages();
dc009d92
EB
210 }
211 /* Install the new kernel, and Uninstall the old */
212 image = xchg(dest_image, image);
213
72414d3f 214out:
8c5a1cf0 215 mutex_unlock(&kexec_mutex);
dc009d92 216 kimage_free(image);
72414d3f 217
dc009d92
EB
218 return result;
219}
220
221#ifdef CONFIG_COMPAT
ca2c405a
HC
222COMPAT_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)
dc009d92
EB
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 */
72414d3f 234 if ((flags & KEXEC_ARCH_MASK) == KEXEC_ARCH_DEFAULT)
dc009d92 235 return -EINVAL;
dc009d92 236
72414d3f 237 if (nr_segments > KEXEC_SEGMENT_MAX)
dc009d92 238 return -EINVAL;
dc009d92
EB
239
240 ksegments = compat_alloc_user_space(nr_segments * sizeof(out));
e1bebcf4 241 for (i = 0; i < nr_segments; i++) {
dc009d92 242 result = copy_from_user(&in, &segments[i], sizeof(in));
72414d3f 243 if (result)
dc009d92 244 return -EFAULT;
dc009d92
EB
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));
72414d3f 252 if (result)
dc009d92 253 return -EFAULT;
dc009d92
EB
254 }
255
256 return sys_kexec_load(entry, nr_segments, ksegments, flags);
257}
258#endif