Commit | Line | Data |
---|---|---|
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 | ||
c59ede7b | 9 | #include <linux/capability.h> |
dc009d92 EB |
10 | #include <linux/mm.h> |
11 | #include <linux/file.h> | |
dc009d92 | 12 | #include <linux/kexec.h> |
8c5a1cf0 | 13 | #include <linux/mutex.h> |
dc009d92 | 14 | #include <linux/list.h> |
dc009d92 | 15 | #include <linux/syscalls.h> |
a43cac0d | 16 | #include <linux/vmalloc.h> |
2965faa5 | 17 | #include <linux/slab.h> |
dc009d92 | 18 | |
a43cac0d DY |
19 | #include "kexec_internal.h" |
20 | ||
dabe7862 VG |
21 | static int copy_user_segment_list(struct kimage *image, |
22 | unsigned long nr_segments, | |
23 | struct kexec_segment __user *segments) | |
dc009d92 | 24 | { |
dabe7862 | 25 | int ret; |
dc009d92 | 26 | size_t segment_bytes; |
dc009d92 EB |
27 | |
28 | /* Read in the segments */ | |
29 | image->nr_segments = nr_segments; | |
30 | segment_bytes = nr_segments * sizeof(*segments); | |
dabe7862 VG |
31 | ret = copy_from_user(image->segment, segments, segment_bytes); |
32 | if (ret) | |
33 | ret = -EFAULT; | |
34 | ||
35 | return ret; | |
36 | } | |
37 | ||
255aedd9 VG |
38 | static 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) | |
dc009d92 | 42 | { |
255aedd9 | 43 | int ret; |
dc009d92 | 44 | struct kimage *image; |
255aedd9 VG |
45 | 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 | } | |
dc009d92 EB |
52 | |
53 | /* Allocate and initialize a controlling structure */ | |
dabe7862 VG |
54 | image = do_kimage_alloc_init(); |
55 | if (!image) | |
56 | return -ENOMEM; | |
57 | ||
58 | image->start = entry; | |
59 | ||
255aedd9 VG |
60 | ret = copy_user_segment_list(image, nr_segments, segments); |
61 | if (ret) | |
dabe7862 VG |
62 | goto out_free_image; |
63 | ||
255aedd9 VG |
64 | ret = sanity_check_segment_list(image); |
65 | if (ret) | |
dabe7862 | 66 | goto out_free_image; |
72414d3f | 67 | |
255aedd9 VG |
68 | /* 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 | ||
dc009d92 EB |
74 | /* |
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 | */ | |
255aedd9 | 79 | ret = -ENOMEM; |
dc009d92 | 80 | image->control_code_page = kimage_alloc_control_pages(image, |
163f6876 | 81 | get_order(KEXEC_CONTROL_PAGE_SIZE)); |
dc009d92 | 82 | if (!image->control_code_page) { |
e1bebcf4 | 83 | pr_err("Could not allocate control_code_buffer\n"); |
dabe7862 | 84 | goto out_free_image; |
dc009d92 EB |
85 | } |
86 | ||
255aedd9 VG |
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; | |
92 | } | |
3ab83521 HY |
93 | } |
94 | ||
b92e7e0d ZY |
95 | *rimage = image; |
96 | return 0; | |
dabe7862 | 97 | out_free_control_pages: |
b92e7e0d | 98 | kimage_free_page_list(&image->control_pages); |
dabe7862 | 99 | out_free_image: |
b92e7e0d | 100 | kfree(image); |
255aedd9 | 101 | return ret; |
dc009d92 EB |
102 | } |
103 | ||
dc009d92 EB |
104 | /* |
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 | |
002ace78 | 118 | * and then copies the image to it's final destination. And |
dc009d92 EB |
119 | * 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 | */ | |
8c5a1cf0 | 124 | |
754fe8d2 HC |
125 | SYSCALL_DEFINE4(kexec_load, unsigned long, entry, unsigned long, nr_segments, |
126 | struct kexec_segment __user *, segments, unsigned long, flags) | |
dc009d92 EB |
127 | { |
128 | struct kimage **dest_image, *image; | |
dc009d92 EB |
129 | int result; |
130 | ||
131 | /* We only trust the superuser with rebooting the system. */ | |
7984754b | 132 | if (!capable(CAP_SYS_BOOT) || kexec_load_disabled) |
dc009d92 EB |
133 | 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)) | |
dc009d92 | 145 | return -EINVAL; |
dc009d92 EB |
146 | |
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 | */ | |
8c5a1cf0 | 164 | if (!mutex_trylock(&kexec_mutex)) |
dc009d92 | 165 | return -EBUSY; |
72414d3f | 166 | |
dc009d92 | 167 | dest_image = &kexec_image; |
72414d3f | 168 | if (flags & KEXEC_ON_CRASH) |
dc009d92 | 169 | dest_image = &kexec_crash_image; |
dc009d92 EB |
170 | if (nr_segments > 0) { |
171 | unsigned long i; | |
72414d3f | 172 | |
518a0c71 GL |
173 | 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 | |
dc009d92 EB |
177 | * we corrupt it. |
178 | */ | |
518a0c71 | 179 | |
dc009d92 | 180 | kimage_free(xchg(&kexec_crash_image, NULL)); |
255aedd9 VG |
181 | result = kimage_alloc_init(&image, entry, nr_segments, |
182 | segments, flags); | |
558df720 | 183 | crash_map_reserved_pages(); |
518a0c71 GL |
184 | } else { |
185 | /* Loading another kernel to reboot into. */ | |
186 | ||
187 | result = kimage_alloc_init(&image, entry, nr_segments, | |
188 | segments, flags); | |
dc009d92 | 189 | } |
72414d3f | 190 | if (result) |
dc009d92 | 191 | goto out; |
72414d3f | 192 | |
3ab83521 HY |
193 | if (flags & KEXEC_PRESERVE_CONTEXT) |
194 | image->preserve_context = 1; | |
dc009d92 | 195 | result = machine_kexec_prepare(image); |
72414d3f | 196 | if (result) |
dc009d92 | 197 | goto out; |
72414d3f MS |
198 | |
199 | for (i = 0; i < nr_segments; i++) { | |
dc009d92 | 200 | result = kimage_load_segment(image, &image->segment[i]); |
72414d3f | 201 | if (result) |
dc009d92 | 202 | goto out; |
dc009d92 | 203 | } |
7fccf032 | 204 | kimage_terminate(image); |
558df720 MH |
205 | if (flags & KEXEC_ON_CRASH) |
206 | crash_unmap_reserved_pages(); | |
dc009d92 EB |
207 | } |
208 | /* Install the new kernel, and Uninstall the old */ | |
209 | image = xchg(dest_image, image); | |
210 | ||
72414d3f | 211 | out: |
8c5a1cf0 | 212 | mutex_unlock(&kexec_mutex); |
dc009d92 | 213 | kimage_free(image); |
72414d3f | 214 | |
dc009d92 EB |
215 | return result; |
216 | } | |
217 | ||
218 | #ifdef CONFIG_COMPAT | |
ca2c405a HC |
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) | |
dc009d92 EB |
223 | { |
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 | */ | |
72414d3f | 231 | if ((flags & KEXEC_ARCH_MASK) == KEXEC_ARCH_DEFAULT) |
dc009d92 | 232 | return -EINVAL; |
dc009d92 | 233 | |
72414d3f | 234 | if (nr_segments > KEXEC_SEGMENT_MAX) |
dc009d92 | 235 | return -EINVAL; |
dc009d92 EB |
236 | |
237 | ksegments = compat_alloc_user_space(nr_segments * sizeof(out)); | |
e1bebcf4 | 238 | for (i = 0; i < nr_segments; i++) { |
dc009d92 | 239 | result = copy_from_user(&in, &segments[i], sizeof(in)); |
72414d3f | 240 | if (result) |
dc009d92 | 241 | return -EFAULT; |
dc009d92 EB |
242 | |
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)); | |
72414d3f | 249 | if (result) |
dc009d92 | 250 | return -EFAULT; |
dc009d92 EB |
251 | } |
252 | ||
253 | return sys_kexec_load(entry, nr_segments, ksegments, flags); | |
254 | } | |
255 | #endif |