blob: de1441656efdacf2d86a5db5226fb6e7f1469655 [file] [log] [blame]
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001/*
2 * kexec.c - kexec 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#include <linux/mm.h>
10#include <linux/file.h>
11#include <linux/slab.h>
12#include <linux/fs.h>
13#include <linux/kexec.h>
14#include <linux/spinlock.h>
15#include <linux/list.h>
16#include <linux/highmem.h>
17#include <linux/syscalls.h>
18#include <linux/reboot.h>
19#include <linux/syscalls.h>
20#include <linux/ioport.h>
Alexander Nyberg6e274d12005-06-25 14:58:26 -070021#include <linux/hardirq.h>
22
Eric W. Biedermandc009d92005-06-25 14:57:52 -070023#include <asm/page.h>
24#include <asm/uaccess.h>
25#include <asm/io.h>
26#include <asm/system.h>
27#include <asm/semaphore.h>
28
Vivek Goyalcc571652006-01-09 20:51:41 -080029/* Per cpu memory for storing cpu states in case of system crash. */
30note_buf_t* crash_notes;
31
Eric W. Biedermandc009d92005-06-25 14:57:52 -070032/* Location of the reserved area for the crash kernel */
33struct resource crashk_res = {
34 .name = "Crash kernel",
35 .start = 0,
36 .end = 0,
37 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
38};
39
Alexander Nyberg6e274d12005-06-25 14:58:26 -070040int kexec_should_crash(struct task_struct *p)
41{
42 if (in_interrupt() || !p->pid || p->pid == 1 || panic_on_oops)
43 return 1;
44 return 0;
45}
46
Eric W. Biedermandc009d92005-06-25 14:57:52 -070047/*
48 * When kexec transitions to the new kernel there is a one-to-one
49 * mapping between physical and virtual addresses. On processors
50 * where you can disable the MMU this is trivial, and easy. For
51 * others it is still a simple predictable page table to setup.
52 *
53 * In that environment kexec copies the new kernel to its final
54 * resting place. This means I can only support memory whose
55 * physical address can fit in an unsigned long. In particular
56 * addresses where (pfn << PAGE_SHIFT) > ULONG_MAX cannot be handled.
57 * If the assembly stub has more restrictive requirements
58 * KEXEC_SOURCE_MEMORY_LIMIT and KEXEC_DEST_MEMORY_LIMIT can be
59 * defined more restrictively in <asm/kexec.h>.
60 *
61 * The code for the transition from the current kernel to the
62 * the new kernel is placed in the control_code_buffer, whose size
63 * is given by KEXEC_CONTROL_CODE_SIZE. In the best case only a single
64 * page of memory is necessary, but some architectures require more.
65 * Because this memory must be identity mapped in the transition from
66 * virtual to physical addresses it must live in the range
67 * 0 - TASK_SIZE, as only the user space mappings are arbitrarily
68 * modifiable.
69 *
70 * The assembly stub in the control code buffer is passed a linked list
71 * of descriptor pages detailing the source pages of the new kernel,
72 * and the destination addresses of those source pages. As this data
73 * structure is not used in the context of the current OS, it must
74 * be self-contained.
75 *
76 * The code has been made to work with highmem pages and will use a
77 * destination page in its final resting place (if it happens
78 * to allocate it). The end product of this is that most of the
79 * physical address space, and most of RAM can be used.
80 *
81 * Future directions include:
82 * - allocating a page table with the control code buffer identity
83 * mapped, to simplify machine_kexec and make kexec_on_panic more
84 * reliable.
85 */
86
87/*
88 * KIMAGE_NO_DEST is an impossible destination address..., for
89 * allocating pages whose destination address we do not care about.
90 */
91#define KIMAGE_NO_DEST (-1UL)
92
Maneesh Soni72414d32005-06-25 14:58:28 -070093static int kimage_is_destination_range(struct kimage *image,
94 unsigned long start, unsigned long end);
95static struct page *kimage_alloc_page(struct kimage *image,
Al Viro9796fdd2005-10-21 03:22:03 -040096 gfp_t gfp_mask,
Maneesh Soni72414d32005-06-25 14:58:28 -070097 unsigned long dest);
Eric W. Biedermandc009d92005-06-25 14:57:52 -070098
99static int do_kimage_alloc(struct kimage **rimage, unsigned long entry,
Maneesh Soni72414d32005-06-25 14:58:28 -0700100 unsigned long nr_segments,
101 struct kexec_segment __user *segments)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700102{
103 size_t segment_bytes;
104 struct kimage *image;
105 unsigned long i;
106 int result;
107
108 /* Allocate a controlling structure */
109 result = -ENOMEM;
110 image = kmalloc(sizeof(*image), GFP_KERNEL);
Maneesh Soni72414d32005-06-25 14:58:28 -0700111 if (!image)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700112 goto out;
Maneesh Soni72414d32005-06-25 14:58:28 -0700113
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700114 memset(image, 0, sizeof(*image));
115 image->head = 0;
116 image->entry = &image->head;
117 image->last_entry = &image->head;
118 image->control_page = ~0; /* By default this does not apply */
119 image->start = entry;
120 image->type = KEXEC_TYPE_DEFAULT;
121
122 /* Initialize the list of control pages */
123 INIT_LIST_HEAD(&image->control_pages);
124
125 /* Initialize the list of destination pages */
126 INIT_LIST_HEAD(&image->dest_pages);
127
128 /* Initialize the list of unuseable pages */
129 INIT_LIST_HEAD(&image->unuseable_pages);
130
131 /* Read in the segments */
132 image->nr_segments = nr_segments;
133 segment_bytes = nr_segments * sizeof(*segments);
134 result = copy_from_user(image->segment, segments, segment_bytes);
135 if (result)
136 goto out;
137
138 /*
139 * Verify we have good destination addresses. The caller is
140 * responsible for making certain we don't attempt to load
141 * the new image into invalid or reserved areas of RAM. This
142 * just verifies it is an address we can use.
143 *
144 * Since the kernel does everything in page size chunks ensure
145 * the destination addreses are page aligned. Too many
146 * special cases crop of when we don't do this. The most
147 * insidious is getting overlapping destination addresses
148 * simply because addresses are changed to page size
149 * granularity.
150 */
151 result = -EADDRNOTAVAIL;
152 for (i = 0; i < nr_segments; i++) {
153 unsigned long mstart, mend;
Maneesh Soni72414d32005-06-25 14:58:28 -0700154
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700155 mstart = image->segment[i].mem;
156 mend = mstart + image->segment[i].memsz;
157 if ((mstart & ~PAGE_MASK) || (mend & ~PAGE_MASK))
158 goto out;
159 if (mend >= KEXEC_DESTINATION_MEMORY_LIMIT)
160 goto out;
161 }
162
163 /* Verify our destination addresses do not overlap.
164 * If we alloed overlapping destination addresses
165 * through very weird things can happen with no
166 * easy explanation as one segment stops on another.
167 */
168 result = -EINVAL;
Maneesh Soni72414d32005-06-25 14:58:28 -0700169 for (i = 0; i < nr_segments; i++) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700170 unsigned long mstart, mend;
171 unsigned long j;
Maneesh Soni72414d32005-06-25 14:58:28 -0700172
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700173 mstart = image->segment[i].mem;
174 mend = mstart + image->segment[i].memsz;
Maneesh Soni72414d32005-06-25 14:58:28 -0700175 for (j = 0; j < i; j++) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700176 unsigned long pstart, pend;
177 pstart = image->segment[j].mem;
178 pend = pstart + image->segment[j].memsz;
179 /* Do the segments overlap ? */
180 if ((mend > pstart) && (mstart < pend))
181 goto out;
182 }
183 }
184
185 /* Ensure our buffer sizes are strictly less than
186 * our memory sizes. This should always be the case,
187 * and it is easier to check up front than to be surprised
188 * later on.
189 */
190 result = -EINVAL;
Maneesh Soni72414d32005-06-25 14:58:28 -0700191 for (i = 0; i < nr_segments; i++) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700192 if (image->segment[i].bufsz > image->segment[i].memsz)
193 goto out;
194 }
195
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700196 result = 0;
Maneesh Soni72414d32005-06-25 14:58:28 -0700197out:
198 if (result == 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700199 *rimage = image;
Maneesh Soni72414d32005-06-25 14:58:28 -0700200 else
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700201 kfree(image);
Maneesh Soni72414d32005-06-25 14:58:28 -0700202
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700203 return result;
204
205}
206
207static int kimage_normal_alloc(struct kimage **rimage, unsigned long entry,
Maneesh Soni72414d32005-06-25 14:58:28 -0700208 unsigned long nr_segments,
209 struct kexec_segment __user *segments)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700210{
211 int result;
212 struct kimage *image;
213
214 /* Allocate and initialize a controlling structure */
215 image = NULL;
216 result = do_kimage_alloc(&image, entry, nr_segments, segments);
Maneesh Soni72414d32005-06-25 14:58:28 -0700217 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700218 goto out;
Maneesh Soni72414d32005-06-25 14:58:28 -0700219
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700220 *rimage = image;
221
222 /*
223 * Find a location for the control code buffer, and add it
224 * the vector of segments so that it's pages will also be
225 * counted as destination pages.
226 */
227 result = -ENOMEM;
228 image->control_code_page = kimage_alloc_control_pages(image,
Maneesh Soni72414d32005-06-25 14:58:28 -0700229 get_order(KEXEC_CONTROL_CODE_SIZE));
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700230 if (!image->control_code_page) {
231 printk(KERN_ERR "Could not allocate control_code_buffer\n");
232 goto out;
233 }
234
235 result = 0;
236 out:
Maneesh Soni72414d32005-06-25 14:58:28 -0700237 if (result == 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700238 *rimage = image;
Maneesh Soni72414d32005-06-25 14:58:28 -0700239 else
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700240 kfree(image);
Maneesh Soni72414d32005-06-25 14:58:28 -0700241
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700242 return result;
243}
244
245static int kimage_crash_alloc(struct kimage **rimage, unsigned long entry,
Maneesh Soni72414d32005-06-25 14:58:28 -0700246 unsigned long nr_segments,
Alexey Dobriyan314b6a42005-06-27 22:29:33 -0700247 struct kexec_segment __user *segments)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700248{
249 int result;
250 struct kimage *image;
251 unsigned long i;
252
253 image = NULL;
254 /* Verify we have a valid entry point */
255 if ((entry < crashk_res.start) || (entry > crashk_res.end)) {
256 result = -EADDRNOTAVAIL;
257 goto out;
258 }
259
260 /* Allocate and initialize a controlling structure */
261 result = do_kimage_alloc(&image, entry, nr_segments, segments);
Maneesh Soni72414d32005-06-25 14:58:28 -0700262 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700263 goto out;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700264
265 /* Enable the special crash kernel control page
266 * allocation policy.
267 */
268 image->control_page = crashk_res.start;
269 image->type = KEXEC_TYPE_CRASH;
270
271 /*
272 * Verify we have good destination addresses. Normally
273 * the caller is responsible for making certain we don't
274 * attempt to load the new image into invalid or reserved
275 * areas of RAM. But crash kernels are preloaded into a
276 * reserved area of ram. We must ensure the addresses
277 * are in the reserved area otherwise preloading the
278 * kernel could corrupt things.
279 */
280 result = -EADDRNOTAVAIL;
281 for (i = 0; i < nr_segments; i++) {
282 unsigned long mstart, mend;
Maneesh Soni72414d32005-06-25 14:58:28 -0700283
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700284 mstart = image->segment[i].mem;
Vivek Goyal50cccc62005-06-25 14:57:55 -0700285 mend = mstart + image->segment[i].memsz - 1;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700286 /* Ensure we are within the crash kernel limits */
287 if ((mstart < crashk_res.start) || (mend > crashk_res.end))
288 goto out;
289 }
290
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700291 /*
292 * Find a location for the control code buffer, and add
293 * the vector of segments so that it's pages will also be
294 * counted as destination pages.
295 */
296 result = -ENOMEM;
297 image->control_code_page = kimage_alloc_control_pages(image,
Maneesh Soni72414d32005-06-25 14:58:28 -0700298 get_order(KEXEC_CONTROL_CODE_SIZE));
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700299 if (!image->control_code_page) {
300 printk(KERN_ERR "Could not allocate control_code_buffer\n");
301 goto out;
302 }
303
304 result = 0;
Maneesh Soni72414d32005-06-25 14:58:28 -0700305out:
306 if (result == 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700307 *rimage = image;
Maneesh Soni72414d32005-06-25 14:58:28 -0700308 else
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700309 kfree(image);
Maneesh Soni72414d32005-06-25 14:58:28 -0700310
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700311 return result;
312}
313
Maneesh Soni72414d32005-06-25 14:58:28 -0700314static int kimage_is_destination_range(struct kimage *image,
315 unsigned long start,
316 unsigned long end)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700317{
318 unsigned long i;
319
320 for (i = 0; i < image->nr_segments; i++) {
321 unsigned long mstart, mend;
Maneesh Soni72414d32005-06-25 14:58:28 -0700322
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700323 mstart = image->segment[i].mem;
Maneesh Soni72414d32005-06-25 14:58:28 -0700324 mend = mstart + image->segment[i].memsz;
325 if ((end > mstart) && (start < mend))
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700326 return 1;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700327 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700328
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700329 return 0;
330}
331
Al Viro9796fdd2005-10-21 03:22:03 -0400332static struct page *kimage_alloc_pages(gfp_t gfp_mask, unsigned int order)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700333{
334 struct page *pages;
Maneesh Soni72414d32005-06-25 14:58:28 -0700335
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700336 pages = alloc_pages(gfp_mask, order);
337 if (pages) {
338 unsigned int count, i;
339 pages->mapping = NULL;
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700340 set_page_private(pages, order);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700341 count = 1 << order;
Maneesh Soni72414d32005-06-25 14:58:28 -0700342 for (i = 0; i < count; i++)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700343 SetPageReserved(pages + i);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700344 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700345
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700346 return pages;
347}
348
349static void kimage_free_pages(struct page *page)
350{
351 unsigned int order, count, i;
Maneesh Soni72414d32005-06-25 14:58:28 -0700352
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700353 order = page_private(page);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700354 count = 1 << order;
Maneesh Soni72414d32005-06-25 14:58:28 -0700355 for (i = 0; i < count; i++)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700356 ClearPageReserved(page + i);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700357 __free_pages(page, order);
358}
359
360static void kimage_free_page_list(struct list_head *list)
361{
362 struct list_head *pos, *next;
Maneesh Soni72414d32005-06-25 14:58:28 -0700363
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700364 list_for_each_safe(pos, next, list) {
365 struct page *page;
366
367 page = list_entry(pos, struct page, lru);
368 list_del(&page->lru);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700369 kimage_free_pages(page);
370 }
371}
372
Maneesh Soni72414d32005-06-25 14:58:28 -0700373static struct page *kimage_alloc_normal_control_pages(struct kimage *image,
374 unsigned int order)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700375{
376 /* Control pages are special, they are the intermediaries
377 * that are needed while we copy the rest of the pages
378 * to their final resting place. As such they must
379 * not conflict with either the destination addresses
380 * or memory the kernel is already using.
381 *
382 * The only case where we really need more than one of
383 * these are for architectures where we cannot disable
384 * the MMU and must instead generate an identity mapped
385 * page table for all of the memory.
386 *
387 * At worst this runs in O(N) of the image size.
388 */
389 struct list_head extra_pages;
390 struct page *pages;
391 unsigned int count;
392
393 count = 1 << order;
394 INIT_LIST_HEAD(&extra_pages);
395
396 /* Loop while I can allocate a page and the page allocated
397 * is a destination page.
398 */
399 do {
400 unsigned long pfn, epfn, addr, eaddr;
Maneesh Soni72414d32005-06-25 14:58:28 -0700401
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700402 pages = kimage_alloc_pages(GFP_KERNEL, order);
403 if (!pages)
404 break;
405 pfn = page_to_pfn(pages);
406 epfn = pfn + count;
407 addr = pfn << PAGE_SHIFT;
408 eaddr = epfn << PAGE_SHIFT;
409 if ((epfn >= (KEXEC_CONTROL_MEMORY_LIMIT >> PAGE_SHIFT)) ||
Maneesh Soni72414d32005-06-25 14:58:28 -0700410 kimage_is_destination_range(image, addr, eaddr)) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700411 list_add(&pages->lru, &extra_pages);
412 pages = NULL;
413 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700414 } while (!pages);
415
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700416 if (pages) {
417 /* Remember the allocated page... */
418 list_add(&pages->lru, &image->control_pages);
419
420 /* Because the page is already in it's destination
421 * location we will never allocate another page at
422 * that address. Therefore kimage_alloc_pages
423 * will not return it (again) and we don't need
424 * to give it an entry in image->segment[].
425 */
426 }
427 /* Deal with the destination pages I have inadvertently allocated.
428 *
429 * Ideally I would convert multi-page allocations into single
430 * page allocations, and add everyting to image->dest_pages.
431 *
432 * For now it is simpler to just free the pages.
433 */
434 kimage_free_page_list(&extra_pages);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700435
Maneesh Soni72414d32005-06-25 14:58:28 -0700436 return pages;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700437}
438
Maneesh Soni72414d32005-06-25 14:58:28 -0700439static struct page *kimage_alloc_crash_control_pages(struct kimage *image,
440 unsigned int order)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700441{
442 /* Control pages are special, they are the intermediaries
443 * that are needed while we copy the rest of the pages
444 * to their final resting place. As such they must
445 * not conflict with either the destination addresses
446 * or memory the kernel is already using.
447 *
448 * Control pages are also the only pags we must allocate
449 * when loading a crash kernel. All of the other pages
450 * are specified by the segments and we just memcpy
451 * into them directly.
452 *
453 * The only case where we really need more than one of
454 * these are for architectures where we cannot disable
455 * the MMU and must instead generate an identity mapped
456 * page table for all of the memory.
457 *
458 * Given the low demand this implements a very simple
459 * allocator that finds the first hole of the appropriate
460 * size in the reserved memory region, and allocates all
461 * of the memory up to and including the hole.
462 */
463 unsigned long hole_start, hole_end, size;
464 struct page *pages;
Maneesh Soni72414d32005-06-25 14:58:28 -0700465
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700466 pages = NULL;
467 size = (1 << order) << PAGE_SHIFT;
468 hole_start = (image->control_page + (size - 1)) & ~(size - 1);
469 hole_end = hole_start + size - 1;
Maneesh Soni72414d32005-06-25 14:58:28 -0700470 while (hole_end <= crashk_res.end) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700471 unsigned long i;
Maneesh Soni72414d32005-06-25 14:58:28 -0700472
473 if (hole_end > KEXEC_CONTROL_MEMORY_LIMIT)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700474 break;
Maneesh Soni72414d32005-06-25 14:58:28 -0700475 if (hole_end > crashk_res.end)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700476 break;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700477 /* See if I overlap any of the segments */
Maneesh Soni72414d32005-06-25 14:58:28 -0700478 for (i = 0; i < image->nr_segments; i++) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700479 unsigned long mstart, mend;
Maneesh Soni72414d32005-06-25 14:58:28 -0700480
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700481 mstart = image->segment[i].mem;
482 mend = mstart + image->segment[i].memsz - 1;
483 if ((hole_end >= mstart) && (hole_start <= mend)) {
484 /* Advance the hole to the end of the segment */
485 hole_start = (mend + (size - 1)) & ~(size - 1);
486 hole_end = hole_start + size - 1;
487 break;
488 }
489 }
490 /* If I don't overlap any segments I have found my hole! */
491 if (i == image->nr_segments) {
492 pages = pfn_to_page(hole_start >> PAGE_SHIFT);
493 break;
494 }
495 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700496 if (pages)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700497 image->control_page = hole_end;
Maneesh Soni72414d32005-06-25 14:58:28 -0700498
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700499 return pages;
500}
501
502
Maneesh Soni72414d32005-06-25 14:58:28 -0700503struct page *kimage_alloc_control_pages(struct kimage *image,
504 unsigned int order)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700505{
506 struct page *pages = NULL;
Maneesh Soni72414d32005-06-25 14:58:28 -0700507
508 switch (image->type) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700509 case KEXEC_TYPE_DEFAULT:
510 pages = kimage_alloc_normal_control_pages(image, order);
511 break;
512 case KEXEC_TYPE_CRASH:
513 pages = kimage_alloc_crash_control_pages(image, order);
514 break;
515 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700516
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700517 return pages;
518}
519
520static int kimage_add_entry(struct kimage *image, kimage_entry_t entry)
521{
Maneesh Soni72414d32005-06-25 14:58:28 -0700522 if (*image->entry != 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700523 image->entry++;
Maneesh Soni72414d32005-06-25 14:58:28 -0700524
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700525 if (image->entry == image->last_entry) {
526 kimage_entry_t *ind_page;
527 struct page *page;
Maneesh Soni72414d32005-06-25 14:58:28 -0700528
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700529 page = kimage_alloc_page(image, GFP_KERNEL, KIMAGE_NO_DEST);
Maneesh Soni72414d32005-06-25 14:58:28 -0700530 if (!page)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700531 return -ENOMEM;
Maneesh Soni72414d32005-06-25 14:58:28 -0700532
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700533 ind_page = page_address(page);
534 *image->entry = virt_to_phys(ind_page) | IND_INDIRECTION;
535 image->entry = ind_page;
Maneesh Soni72414d32005-06-25 14:58:28 -0700536 image->last_entry = ind_page +
537 ((PAGE_SIZE/sizeof(kimage_entry_t)) - 1);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700538 }
539 *image->entry = entry;
540 image->entry++;
541 *image->entry = 0;
Maneesh Soni72414d32005-06-25 14:58:28 -0700542
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700543 return 0;
544}
545
Maneesh Soni72414d32005-06-25 14:58:28 -0700546static int kimage_set_destination(struct kimage *image,
547 unsigned long destination)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700548{
549 int result;
550
551 destination &= PAGE_MASK;
552 result = kimage_add_entry(image, destination | IND_DESTINATION);
Maneesh Soni72414d32005-06-25 14:58:28 -0700553 if (result == 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700554 image->destination = destination;
Maneesh Soni72414d32005-06-25 14:58:28 -0700555
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700556 return result;
557}
558
559
560static int kimage_add_page(struct kimage *image, unsigned long page)
561{
562 int result;
563
564 page &= PAGE_MASK;
565 result = kimage_add_entry(image, page | IND_SOURCE);
Maneesh Soni72414d32005-06-25 14:58:28 -0700566 if (result == 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700567 image->destination += PAGE_SIZE;
Maneesh Soni72414d32005-06-25 14:58:28 -0700568
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700569 return result;
570}
571
572
573static void kimage_free_extra_pages(struct kimage *image)
574{
575 /* Walk through and free any extra destination pages I may have */
576 kimage_free_page_list(&image->dest_pages);
577
578 /* Walk through and free any unuseable pages I have cached */
579 kimage_free_page_list(&image->unuseable_pages);
580
581}
582static int kimage_terminate(struct kimage *image)
583{
Maneesh Soni72414d32005-06-25 14:58:28 -0700584 if (*image->entry != 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700585 image->entry++;
Maneesh Soni72414d32005-06-25 14:58:28 -0700586
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700587 *image->entry = IND_DONE;
Maneesh Soni72414d32005-06-25 14:58:28 -0700588
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700589 return 0;
590}
591
592#define for_each_kimage_entry(image, ptr, entry) \
593 for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE); \
594 ptr = (entry & IND_INDIRECTION)? \
595 phys_to_virt((entry & PAGE_MASK)): ptr +1)
596
597static void kimage_free_entry(kimage_entry_t entry)
598{
599 struct page *page;
600
601 page = pfn_to_page(entry >> PAGE_SHIFT);
602 kimage_free_pages(page);
603}
604
605static void kimage_free(struct kimage *image)
606{
607 kimage_entry_t *ptr, entry;
608 kimage_entry_t ind = 0;
609
610 if (!image)
611 return;
Maneesh Soni72414d32005-06-25 14:58:28 -0700612
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700613 kimage_free_extra_pages(image);
614 for_each_kimage_entry(image, ptr, entry) {
615 if (entry & IND_INDIRECTION) {
616 /* Free the previous indirection page */
Maneesh Soni72414d32005-06-25 14:58:28 -0700617 if (ind & IND_INDIRECTION)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700618 kimage_free_entry(ind);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700619 /* Save this indirection page until we are
620 * done with it.
621 */
622 ind = entry;
623 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700624 else if (entry & IND_SOURCE)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700625 kimage_free_entry(entry);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700626 }
627 /* Free the final indirection page */
Maneesh Soni72414d32005-06-25 14:58:28 -0700628 if (ind & IND_INDIRECTION)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700629 kimage_free_entry(ind);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700630
631 /* Handle any machine specific cleanup */
632 machine_kexec_cleanup(image);
633
634 /* Free the kexec control pages... */
635 kimage_free_page_list(&image->control_pages);
636 kfree(image);
637}
638
Maneesh Soni72414d32005-06-25 14:58:28 -0700639static kimage_entry_t *kimage_dst_used(struct kimage *image,
640 unsigned long page)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700641{
642 kimage_entry_t *ptr, entry;
643 unsigned long destination = 0;
644
645 for_each_kimage_entry(image, ptr, entry) {
Maneesh Soni72414d32005-06-25 14:58:28 -0700646 if (entry & IND_DESTINATION)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700647 destination = entry & PAGE_MASK;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700648 else if (entry & IND_SOURCE) {
Maneesh Soni72414d32005-06-25 14:58:28 -0700649 if (page == destination)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700650 return ptr;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700651 destination += PAGE_SIZE;
652 }
653 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700654
Alexey Dobriyan314b6a42005-06-27 22:29:33 -0700655 return NULL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700656}
657
Maneesh Soni72414d32005-06-25 14:58:28 -0700658static struct page *kimage_alloc_page(struct kimage *image,
Al Viro9796fdd2005-10-21 03:22:03 -0400659 gfp_t gfp_mask,
Maneesh Soni72414d32005-06-25 14:58:28 -0700660 unsigned long destination)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700661{
662 /*
663 * Here we implement safeguards to ensure that a source page
664 * is not copied to its destination page before the data on
665 * the destination page is no longer useful.
666 *
667 * To do this we maintain the invariant that a source page is
668 * either its own destination page, or it is not a
669 * destination page at all.
670 *
671 * That is slightly stronger than required, but the proof
672 * that no problems will not occur is trivial, and the
673 * implementation is simply to verify.
674 *
675 * When allocating all pages normally this algorithm will run
676 * in O(N) time, but in the worst case it will run in O(N^2)
677 * time. If the runtime is a problem the data structures can
678 * be fixed.
679 */
680 struct page *page;
681 unsigned long addr;
682
683 /*
684 * Walk through the list of destination pages, and see if I
685 * have a match.
686 */
687 list_for_each_entry(page, &image->dest_pages, lru) {
688 addr = page_to_pfn(page) << PAGE_SHIFT;
689 if (addr == destination) {
690 list_del(&page->lru);
691 return page;
692 }
693 }
694 page = NULL;
695 while (1) {
696 kimage_entry_t *old;
697
698 /* Allocate a page, if we run out of memory give up */
699 page = kimage_alloc_pages(gfp_mask, 0);
Maneesh Soni72414d32005-06-25 14:58:28 -0700700 if (!page)
Alexey Dobriyan314b6a42005-06-27 22:29:33 -0700701 return NULL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700702 /* If the page cannot be used file it away */
Maneesh Soni72414d32005-06-25 14:58:28 -0700703 if (page_to_pfn(page) >
704 (KEXEC_SOURCE_MEMORY_LIMIT >> PAGE_SHIFT)) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700705 list_add(&page->lru, &image->unuseable_pages);
706 continue;
707 }
708 addr = page_to_pfn(page) << PAGE_SHIFT;
709
710 /* If it is the destination page we want use it */
711 if (addr == destination)
712 break;
713
714 /* If the page is not a destination page use it */
Maneesh Soni72414d32005-06-25 14:58:28 -0700715 if (!kimage_is_destination_range(image, addr,
716 addr + PAGE_SIZE))
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700717 break;
718
719 /*
720 * I know that the page is someones destination page.
721 * See if there is already a source page for this
722 * destination page. And if so swap the source pages.
723 */
724 old = kimage_dst_used(image, addr);
725 if (old) {
726 /* If so move it */
727 unsigned long old_addr;
728 struct page *old_page;
729
730 old_addr = *old & PAGE_MASK;
731 old_page = pfn_to_page(old_addr >> PAGE_SHIFT);
732 copy_highpage(page, old_page);
733 *old = addr | (*old & ~PAGE_MASK);
734
735 /* The old page I have found cannot be a
736 * destination page, so return it.
737 */
738 addr = old_addr;
739 page = old_page;
740 break;
741 }
742 else {
743 /* Place the page on the destination list I
744 * will use it later.
745 */
746 list_add(&page->lru, &image->dest_pages);
747 }
748 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700749
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700750 return page;
751}
752
753static int kimage_load_normal_segment(struct kimage *image,
Maneesh Soni72414d32005-06-25 14:58:28 -0700754 struct kexec_segment *segment)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700755{
756 unsigned long maddr;
757 unsigned long ubytes, mbytes;
758 int result;
Alexey Dobriyan314b6a42005-06-27 22:29:33 -0700759 unsigned char __user *buf;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700760
761 result = 0;
762 buf = segment->buf;
763 ubytes = segment->bufsz;
764 mbytes = segment->memsz;
765 maddr = segment->mem;
766
767 result = kimage_set_destination(image, maddr);
Maneesh Soni72414d32005-06-25 14:58:28 -0700768 if (result < 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700769 goto out;
Maneesh Soni72414d32005-06-25 14:58:28 -0700770
771 while (mbytes) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700772 struct page *page;
773 char *ptr;
774 size_t uchunk, mchunk;
Maneesh Soni72414d32005-06-25 14:58:28 -0700775
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700776 page = kimage_alloc_page(image, GFP_HIGHUSER, maddr);
777 if (page == 0) {
778 result = -ENOMEM;
779 goto out;
780 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700781 result = kimage_add_page(image, page_to_pfn(page)
782 << PAGE_SHIFT);
783 if (result < 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700784 goto out;
Maneesh Soni72414d32005-06-25 14:58:28 -0700785
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700786 ptr = kmap(page);
787 /* Start with a clear page */
788 memset(ptr, 0, PAGE_SIZE);
789 ptr += maddr & ~PAGE_MASK;
790 mchunk = PAGE_SIZE - (maddr & ~PAGE_MASK);
Maneesh Soni72414d32005-06-25 14:58:28 -0700791 if (mchunk > mbytes)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700792 mchunk = mbytes;
Maneesh Soni72414d32005-06-25 14:58:28 -0700793
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700794 uchunk = mchunk;
Maneesh Soni72414d32005-06-25 14:58:28 -0700795 if (uchunk > ubytes)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700796 uchunk = ubytes;
Maneesh Soni72414d32005-06-25 14:58:28 -0700797
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700798 result = copy_from_user(ptr, buf, uchunk);
799 kunmap(page);
800 if (result) {
801 result = (result < 0) ? result : -EIO;
802 goto out;
803 }
804 ubytes -= uchunk;
805 maddr += mchunk;
806 buf += mchunk;
807 mbytes -= mchunk;
808 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700809out:
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700810 return result;
811}
812
813static int kimage_load_crash_segment(struct kimage *image,
Maneesh Soni72414d32005-06-25 14:58:28 -0700814 struct kexec_segment *segment)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700815{
816 /* For crash dumps kernels we simply copy the data from
817 * user space to it's destination.
818 * We do things a page at a time for the sake of kmap.
819 */
820 unsigned long maddr;
821 unsigned long ubytes, mbytes;
822 int result;
Alexey Dobriyan314b6a42005-06-27 22:29:33 -0700823 unsigned char __user *buf;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700824
825 result = 0;
826 buf = segment->buf;
827 ubytes = segment->bufsz;
828 mbytes = segment->memsz;
829 maddr = segment->mem;
Maneesh Soni72414d32005-06-25 14:58:28 -0700830 while (mbytes) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700831 struct page *page;
832 char *ptr;
833 size_t uchunk, mchunk;
Maneesh Soni72414d32005-06-25 14:58:28 -0700834
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700835 page = pfn_to_page(maddr >> PAGE_SHIFT);
836 if (page == 0) {
837 result = -ENOMEM;
838 goto out;
839 }
840 ptr = kmap(page);
841 ptr += maddr & ~PAGE_MASK;
842 mchunk = PAGE_SIZE - (maddr & ~PAGE_MASK);
Maneesh Soni72414d32005-06-25 14:58:28 -0700843 if (mchunk > mbytes)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700844 mchunk = mbytes;
Maneesh Soni72414d32005-06-25 14:58:28 -0700845
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700846 uchunk = mchunk;
847 if (uchunk > ubytes) {
848 uchunk = ubytes;
849 /* Zero the trailing part of the page */
850 memset(ptr + uchunk, 0, mchunk - uchunk);
851 }
852 result = copy_from_user(ptr, buf, uchunk);
853 kunmap(page);
854 if (result) {
855 result = (result < 0) ? result : -EIO;
856 goto out;
857 }
858 ubytes -= uchunk;
859 maddr += mchunk;
860 buf += mchunk;
861 mbytes -= mchunk;
862 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700863out:
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700864 return result;
865}
866
867static int kimage_load_segment(struct kimage *image,
Maneesh Soni72414d32005-06-25 14:58:28 -0700868 struct kexec_segment *segment)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700869{
870 int result = -ENOMEM;
Maneesh Soni72414d32005-06-25 14:58:28 -0700871
872 switch (image->type) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700873 case KEXEC_TYPE_DEFAULT:
874 result = kimage_load_normal_segment(image, segment);
875 break;
876 case KEXEC_TYPE_CRASH:
877 result = kimage_load_crash_segment(image, segment);
878 break;
879 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700880
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700881 return result;
882}
883
884/*
885 * Exec Kernel system call: for obvious reasons only root may call it.
886 *
887 * This call breaks up into three pieces.
888 * - A generic part which loads the new kernel from the current
889 * address space, and very carefully places the data in the
890 * allocated pages.
891 *
892 * - A generic part that interacts with the kernel and tells all of
893 * the devices to shut down. Preventing on-going dmas, and placing
894 * the devices in a consistent state so a later kernel can
895 * reinitialize them.
896 *
897 * - A machine specific part that includes the syscall number
898 * and the copies the image to it's final destination. And
899 * jumps into the image at entry.
900 *
901 * kexec does not sync, or unmount filesystems so if you need
902 * that to happen you need to do that yourself.
903 */
904struct kimage *kexec_image = NULL;
905static struct kimage *kexec_crash_image = NULL;
906/*
907 * A home grown binary mutex.
908 * Nothing can wait so this mutex is safe to use
909 * in interrupt context :)
910 */
911static int kexec_lock = 0;
912
Maneesh Soni72414d32005-06-25 14:58:28 -0700913asmlinkage long sys_kexec_load(unsigned long entry, unsigned long nr_segments,
914 struct kexec_segment __user *segments,
915 unsigned long flags)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700916{
917 struct kimage **dest_image, *image;
918 int locked;
919 int result;
920
921 /* We only trust the superuser with rebooting the system. */
922 if (!capable(CAP_SYS_BOOT))
923 return -EPERM;
924
925 /*
926 * Verify we have a legal set of flags
927 * This leaves us room for future extensions.
928 */
929 if ((flags & KEXEC_FLAGS) != (flags & ~KEXEC_ARCH_MASK))
930 return -EINVAL;
931
932 /* Verify we are on the appropriate architecture */
933 if (((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH) &&
934 ((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH_DEFAULT))
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700935 return -EINVAL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700936
937 /* Put an artificial cap on the number
938 * of segments passed to kexec_load.
939 */
940 if (nr_segments > KEXEC_SEGMENT_MAX)
941 return -EINVAL;
942
943 image = NULL;
944 result = 0;
945
946 /* Because we write directly to the reserved memory
947 * region when loading crash kernels we need a mutex here to
948 * prevent multiple crash kernels from attempting to load
949 * simultaneously, and to prevent a crash kernel from loading
950 * over the top of a in use crash kernel.
951 *
952 * KISS: always take the mutex.
953 */
954 locked = xchg(&kexec_lock, 1);
Maneesh Soni72414d32005-06-25 14:58:28 -0700955 if (locked)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700956 return -EBUSY;
Maneesh Soni72414d32005-06-25 14:58:28 -0700957
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700958 dest_image = &kexec_image;
Maneesh Soni72414d32005-06-25 14:58:28 -0700959 if (flags & KEXEC_ON_CRASH)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700960 dest_image = &kexec_crash_image;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700961 if (nr_segments > 0) {
962 unsigned long i;
Maneesh Soni72414d32005-06-25 14:58:28 -0700963
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700964 /* Loading another kernel to reboot into */
Maneesh Soni72414d32005-06-25 14:58:28 -0700965 if ((flags & KEXEC_ON_CRASH) == 0)
966 result = kimage_normal_alloc(&image, entry,
967 nr_segments, segments);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700968 /* Loading another kernel to switch to if this one crashes */
969 else if (flags & KEXEC_ON_CRASH) {
970 /* Free any current crash dump kernel before
971 * we corrupt it.
972 */
973 kimage_free(xchg(&kexec_crash_image, NULL));
Maneesh Soni72414d32005-06-25 14:58:28 -0700974 result = kimage_crash_alloc(&image, entry,
975 nr_segments, segments);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700976 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700977 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700978 goto out;
Maneesh Soni72414d32005-06-25 14:58:28 -0700979
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700980 result = machine_kexec_prepare(image);
Maneesh Soni72414d32005-06-25 14:58:28 -0700981 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700982 goto out;
Maneesh Soni72414d32005-06-25 14:58:28 -0700983
984 for (i = 0; i < nr_segments; i++) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700985 result = kimage_load_segment(image, &image->segment[i]);
Maneesh Soni72414d32005-06-25 14:58:28 -0700986 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700987 goto out;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700988 }
989 result = kimage_terminate(image);
Maneesh Soni72414d32005-06-25 14:58:28 -0700990 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700991 goto out;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700992 }
993 /* Install the new kernel, and Uninstall the old */
994 image = xchg(dest_image, image);
995
Maneesh Soni72414d32005-06-25 14:58:28 -0700996out:
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700997 xchg(&kexec_lock, 0); /* Release the mutex */
998 kimage_free(image);
Maneesh Soni72414d32005-06-25 14:58:28 -0700999
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001000 return result;
1001}
1002
1003#ifdef CONFIG_COMPAT
1004asmlinkage long compat_sys_kexec_load(unsigned long entry,
Maneesh Soni72414d32005-06-25 14:58:28 -07001005 unsigned long nr_segments,
1006 struct compat_kexec_segment __user *segments,
1007 unsigned long flags)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001008{
1009 struct compat_kexec_segment in;
1010 struct kexec_segment out, __user *ksegments;
1011 unsigned long i, result;
1012
1013 /* Don't allow clients that don't understand the native
1014 * architecture to do anything.
1015 */
Maneesh Soni72414d32005-06-25 14:58:28 -07001016 if ((flags & KEXEC_ARCH_MASK) == KEXEC_ARCH_DEFAULT)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001017 return -EINVAL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001018
Maneesh Soni72414d32005-06-25 14:58:28 -07001019 if (nr_segments > KEXEC_SEGMENT_MAX)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001020 return -EINVAL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001021
1022 ksegments = compat_alloc_user_space(nr_segments * sizeof(out));
1023 for (i=0; i < nr_segments; i++) {
1024 result = copy_from_user(&in, &segments[i], sizeof(in));
Maneesh Soni72414d32005-06-25 14:58:28 -07001025 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001026 return -EFAULT;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001027
1028 out.buf = compat_ptr(in.buf);
1029 out.bufsz = in.bufsz;
1030 out.mem = in.mem;
1031 out.memsz = in.memsz;
1032
1033 result = copy_to_user(&ksegments[i], &out, sizeof(out));
Maneesh Soni72414d32005-06-25 14:58:28 -07001034 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001035 return -EFAULT;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001036 }
1037
1038 return sys_kexec_load(entry, nr_segments, ksegments, flags);
1039}
1040#endif
1041
Alexander Nyberg6e274d12005-06-25 14:58:26 -07001042void crash_kexec(struct pt_regs *regs)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001043{
1044 struct kimage *image;
1045 int locked;
1046
1047
1048 /* Take the kexec_lock here to prevent sys_kexec_load
1049 * running on one cpu from replacing the crash kernel
1050 * we are using after a panic on a different cpu.
1051 *
1052 * If the crash kernel was not located in a fixed area
1053 * of memory the xchg(&kexec_crash_image) would be
1054 * sufficient. But since I reuse the memory...
1055 */
1056 locked = xchg(&kexec_lock, 1);
1057 if (!locked) {
1058 image = xchg(&kexec_crash_image, NULL);
1059 if (image) {
Vivek Goyale996e582006-01-09 20:51:44 -08001060 struct pt_regs fixed_regs;
1061 crash_setup_regs(&fixed_regs, regs);
1062 machine_crash_shutdown(&fixed_regs);
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001063 machine_kexec(image);
1064 }
1065 xchg(&kexec_lock, 0);
1066 }
1067}
Vivek Goyalcc571652006-01-09 20:51:41 -08001068
1069static int __init crash_notes_memory_init(void)
1070{
1071 /* Allocate memory for saving cpu registers. */
1072 crash_notes = alloc_percpu(note_buf_t);
1073 if (!crash_notes) {
1074 printk("Kexec: Memory allocation for saving cpu register"
1075 " states failed\n");
1076 return -ENOMEM;
1077 }
1078 return 0;
1079}
1080module_init(crash_notes_memory_init)