blob: aef265325cd3596db981ed2515efec42f255804e [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
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>
12#include <linux/slab.h>
13#include <linux/fs.h>
14#include <linux/kexec.h>
Andrew Morton8c5a1cf2008-08-15 00:40:27 -070015#include <linux/mutex.h>
Eric W. Biedermandc009d92005-06-25 14:57:52 -070016#include <linux/list.h>
17#include <linux/highmem.h>
18#include <linux/syscalls.h>
19#include <linux/reboot.h>
Eric W. Biedermandc009d92005-06-25 14:57:52 -070020#include <linux/ioport.h>
Alexander Nyberg6e274d12005-06-25 14:58:26 -070021#include <linux/hardirq.h>
Magnus Damm85916f82006-12-06 20:40:41 -080022#include <linux/elf.h>
23#include <linux/elfcore.h>
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -070024#include <linux/utsrelease.h>
25#include <linux/utsname.h>
26#include <linux/numa.h>
Huang Ying3ab83522008-07-25 19:45:07 -070027#include <linux/suspend.h>
28#include <linux/device.h>
Huang Ying89081d12008-07-25 19:45:10 -070029#include <linux/freezer.h>
30#include <linux/pm.h>
31#include <linux/cpu.h>
32#include <linux/console.h>
Alexander Nyberg6e274d12005-06-25 14:58:26 -070033
Eric W. Biedermandc009d92005-06-25 14:57:52 -070034#include <asm/page.h>
35#include <asm/uaccess.h>
36#include <asm/io.h>
37#include <asm/system.h>
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -070038#include <asm/sections.h>
Eric W. Biedermandc009d92005-06-25 14:57:52 -070039
Vivek Goyalcc571652006-01-09 20:51:41 -080040/* Per cpu memory for storing cpu states in case of system crash. */
41note_buf_t* crash_notes;
42
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -070043/* vmcoreinfo stuff */
44unsigned char vmcoreinfo_data[VMCOREINFO_BYTES];
45u32 vmcoreinfo_note[VMCOREINFO_NOTE_SIZE/4];
Ken'ichi Ohmichid7682812007-10-16 23:27:28 -070046size_t vmcoreinfo_size;
47size_t vmcoreinfo_max_size = sizeof(vmcoreinfo_data);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -070048
Eric W. Biedermandc009d92005-06-25 14:57:52 -070049/* Location of the reserved area for the crash kernel */
50struct resource crashk_res = {
51 .name = "Crash kernel",
52 .start = 0,
53 .end = 0,
54 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
55};
56
Alexander Nyberg6e274d12005-06-25 14:58:26 -070057int kexec_should_crash(struct task_struct *p)
58{
Serge E. Hallynb460cbc2007-10-18 23:39:52 -070059 if (in_interrupt() || !p->pid || is_global_init(p) || panic_on_oops)
Alexander Nyberg6e274d12005-06-25 14:58:26 -070060 return 1;
61 return 0;
62}
63
Eric W. Biedermandc009d92005-06-25 14:57:52 -070064/*
65 * When kexec transitions to the new kernel there is a one-to-one
66 * mapping between physical and virtual addresses. On processors
67 * where you can disable the MMU this is trivial, and easy. For
68 * others it is still a simple predictable page table to setup.
69 *
70 * In that environment kexec copies the new kernel to its final
71 * resting place. This means I can only support memory whose
72 * physical address can fit in an unsigned long. In particular
73 * addresses where (pfn << PAGE_SHIFT) > ULONG_MAX cannot be handled.
74 * If the assembly stub has more restrictive requirements
75 * KEXEC_SOURCE_MEMORY_LIMIT and KEXEC_DEST_MEMORY_LIMIT can be
76 * defined more restrictively in <asm/kexec.h>.
77 *
78 * The code for the transition from the current kernel to the
79 * the new kernel is placed in the control_code_buffer, whose size
Huang Ying163f6872008-08-15 00:40:22 -070080 * is given by KEXEC_CONTROL_PAGE_SIZE. In the best case only a single
Eric W. Biedermandc009d92005-06-25 14:57:52 -070081 * page of memory is necessary, but some architectures require more.
82 * Because this memory must be identity mapped in the transition from
83 * virtual to physical addresses it must live in the range
84 * 0 - TASK_SIZE, as only the user space mappings are arbitrarily
85 * modifiable.
86 *
87 * The assembly stub in the control code buffer is passed a linked list
88 * of descriptor pages detailing the source pages of the new kernel,
89 * and the destination addresses of those source pages. As this data
90 * structure is not used in the context of the current OS, it must
91 * be self-contained.
92 *
93 * The code has been made to work with highmem pages and will use a
94 * destination page in its final resting place (if it happens
95 * to allocate it). The end product of this is that most of the
96 * physical address space, and most of RAM can be used.
97 *
98 * Future directions include:
99 * - allocating a page table with the control code buffer identity
100 * mapped, to simplify machine_kexec and make kexec_on_panic more
101 * reliable.
102 */
103
104/*
105 * KIMAGE_NO_DEST is an impossible destination address..., for
106 * allocating pages whose destination address we do not care about.
107 */
108#define KIMAGE_NO_DEST (-1UL)
109
Maneesh Soni72414d32005-06-25 14:58:28 -0700110static int kimage_is_destination_range(struct kimage *image,
111 unsigned long start, unsigned long end);
112static struct page *kimage_alloc_page(struct kimage *image,
Al Viro9796fdd2005-10-21 03:22:03 -0400113 gfp_t gfp_mask,
Maneesh Soni72414d32005-06-25 14:58:28 -0700114 unsigned long dest);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700115
116static int do_kimage_alloc(struct kimage **rimage, unsigned long entry,
Maneesh Soni72414d32005-06-25 14:58:28 -0700117 unsigned long nr_segments,
118 struct kexec_segment __user *segments)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700119{
120 size_t segment_bytes;
121 struct kimage *image;
122 unsigned long i;
123 int result;
124
125 /* Allocate a controlling structure */
126 result = -ENOMEM;
Burman Yan4668edc2006-12-06 20:38:51 -0800127 image = kzalloc(sizeof(*image), GFP_KERNEL);
Maneesh Soni72414d32005-06-25 14:58:28 -0700128 if (!image)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700129 goto out;
Maneesh Soni72414d32005-06-25 14:58:28 -0700130
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700131 image->head = 0;
132 image->entry = &image->head;
133 image->last_entry = &image->head;
134 image->control_page = ~0; /* By default this does not apply */
135 image->start = entry;
136 image->type = KEXEC_TYPE_DEFAULT;
137
138 /* Initialize the list of control pages */
139 INIT_LIST_HEAD(&image->control_pages);
140
141 /* Initialize the list of destination pages */
142 INIT_LIST_HEAD(&image->dest_pages);
143
144 /* Initialize the list of unuseable pages */
145 INIT_LIST_HEAD(&image->unuseable_pages);
146
147 /* Read in the segments */
148 image->nr_segments = nr_segments;
149 segment_bytes = nr_segments * sizeof(*segments);
150 result = copy_from_user(image->segment, segments, segment_bytes);
151 if (result)
152 goto out;
153
154 /*
155 * Verify we have good destination addresses. The caller is
156 * responsible for making certain we don't attempt to load
157 * the new image into invalid or reserved areas of RAM. This
158 * just verifies it is an address we can use.
159 *
160 * Since the kernel does everything in page size chunks ensure
161 * the destination addreses are page aligned. Too many
162 * special cases crop of when we don't do this. The most
163 * insidious is getting overlapping destination addresses
164 * simply because addresses are changed to page size
165 * granularity.
166 */
167 result = -EADDRNOTAVAIL;
168 for (i = 0; i < nr_segments; i++) {
169 unsigned long mstart, mend;
Maneesh Soni72414d32005-06-25 14:58:28 -0700170
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700171 mstart = image->segment[i].mem;
172 mend = mstart + image->segment[i].memsz;
173 if ((mstart & ~PAGE_MASK) || (mend & ~PAGE_MASK))
174 goto out;
175 if (mend >= KEXEC_DESTINATION_MEMORY_LIMIT)
176 goto out;
177 }
178
179 /* Verify our destination addresses do not overlap.
180 * If we alloed overlapping destination addresses
181 * through very weird things can happen with no
182 * easy explanation as one segment stops on another.
183 */
184 result = -EINVAL;
Maneesh Soni72414d32005-06-25 14:58:28 -0700185 for (i = 0; i < nr_segments; i++) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700186 unsigned long mstart, mend;
187 unsigned long j;
Maneesh Soni72414d32005-06-25 14:58:28 -0700188
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700189 mstart = image->segment[i].mem;
190 mend = mstart + image->segment[i].memsz;
Maneesh Soni72414d32005-06-25 14:58:28 -0700191 for (j = 0; j < i; j++) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700192 unsigned long pstart, pend;
193 pstart = image->segment[j].mem;
194 pend = pstart + image->segment[j].memsz;
195 /* Do the segments overlap ? */
196 if ((mend > pstart) && (mstart < pend))
197 goto out;
198 }
199 }
200
201 /* Ensure our buffer sizes are strictly less than
202 * our memory sizes. This should always be the case,
203 * and it is easier to check up front than to be surprised
204 * later on.
205 */
206 result = -EINVAL;
Maneesh Soni72414d32005-06-25 14:58:28 -0700207 for (i = 0; i < nr_segments; i++) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700208 if (image->segment[i].bufsz > image->segment[i].memsz)
209 goto out;
210 }
211
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700212 result = 0;
Maneesh Soni72414d32005-06-25 14:58:28 -0700213out:
214 if (result == 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700215 *rimage = image;
Maneesh Soni72414d32005-06-25 14:58:28 -0700216 else
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700217 kfree(image);
Maneesh Soni72414d32005-06-25 14:58:28 -0700218
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700219 return result;
220
221}
222
223static int kimage_normal_alloc(struct kimage **rimage, unsigned long entry,
Maneesh Soni72414d32005-06-25 14:58:28 -0700224 unsigned long nr_segments,
225 struct kexec_segment __user *segments)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700226{
227 int result;
228 struct kimage *image;
229
230 /* Allocate and initialize a controlling structure */
231 image = NULL;
232 result = do_kimage_alloc(&image, entry, nr_segments, segments);
Maneesh Soni72414d32005-06-25 14:58:28 -0700233 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700234 goto out;
Maneesh Soni72414d32005-06-25 14:58:28 -0700235
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700236 *rimage = image;
237
238 /*
239 * Find a location for the control code buffer, and add it
240 * the vector of segments so that it's pages will also be
241 * counted as destination pages.
242 */
243 result = -ENOMEM;
244 image->control_code_page = kimage_alloc_control_pages(image,
Huang Ying163f6872008-08-15 00:40:22 -0700245 get_order(KEXEC_CONTROL_PAGE_SIZE));
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700246 if (!image->control_code_page) {
247 printk(KERN_ERR "Could not allocate control_code_buffer\n");
248 goto out;
249 }
250
Huang Ying3ab83522008-07-25 19:45:07 -0700251 image->swap_page = kimage_alloc_control_pages(image, 0);
252 if (!image->swap_page) {
253 printk(KERN_ERR "Could not allocate swap buffer\n");
254 goto out;
255 }
256
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700257 result = 0;
258 out:
Maneesh Soni72414d32005-06-25 14:58:28 -0700259 if (result == 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700260 *rimage = image;
Maneesh Soni72414d32005-06-25 14:58:28 -0700261 else
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700262 kfree(image);
Maneesh Soni72414d32005-06-25 14:58:28 -0700263
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700264 return result;
265}
266
267static int kimage_crash_alloc(struct kimage **rimage, unsigned long entry,
Maneesh Soni72414d32005-06-25 14:58:28 -0700268 unsigned long nr_segments,
Alexey Dobriyan314b6a42005-06-27 22:29:33 -0700269 struct kexec_segment __user *segments)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700270{
271 int result;
272 struct kimage *image;
273 unsigned long i;
274
275 image = NULL;
276 /* Verify we have a valid entry point */
277 if ((entry < crashk_res.start) || (entry > crashk_res.end)) {
278 result = -EADDRNOTAVAIL;
279 goto out;
280 }
281
282 /* Allocate and initialize a controlling structure */
283 result = do_kimage_alloc(&image, entry, nr_segments, segments);
Maneesh Soni72414d32005-06-25 14:58:28 -0700284 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700285 goto out;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700286
287 /* Enable the special crash kernel control page
288 * allocation policy.
289 */
290 image->control_page = crashk_res.start;
291 image->type = KEXEC_TYPE_CRASH;
292
293 /*
294 * Verify we have good destination addresses. Normally
295 * the caller is responsible for making certain we don't
296 * attempt to load the new image into invalid or reserved
297 * areas of RAM. But crash kernels are preloaded into a
298 * reserved area of ram. We must ensure the addresses
299 * are in the reserved area otherwise preloading the
300 * kernel could corrupt things.
301 */
302 result = -EADDRNOTAVAIL;
303 for (i = 0; i < nr_segments; i++) {
304 unsigned long mstart, mend;
Maneesh Soni72414d32005-06-25 14:58:28 -0700305
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700306 mstart = image->segment[i].mem;
Vivek Goyal50cccc62005-06-25 14:57:55 -0700307 mend = mstart + image->segment[i].memsz - 1;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700308 /* Ensure we are within the crash kernel limits */
309 if ((mstart < crashk_res.start) || (mend > crashk_res.end))
310 goto out;
311 }
312
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700313 /*
314 * Find a location for the control code buffer, and add
315 * the vector of segments so that it's pages will also be
316 * counted as destination pages.
317 */
318 result = -ENOMEM;
319 image->control_code_page = kimage_alloc_control_pages(image,
Huang Ying163f6872008-08-15 00:40:22 -0700320 get_order(KEXEC_CONTROL_PAGE_SIZE));
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700321 if (!image->control_code_page) {
322 printk(KERN_ERR "Could not allocate control_code_buffer\n");
323 goto out;
324 }
325
326 result = 0;
Maneesh Soni72414d32005-06-25 14:58:28 -0700327out:
328 if (result == 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700329 *rimage = image;
Maneesh Soni72414d32005-06-25 14:58:28 -0700330 else
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700331 kfree(image);
Maneesh Soni72414d32005-06-25 14:58:28 -0700332
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700333 return result;
334}
335
Maneesh Soni72414d32005-06-25 14:58:28 -0700336static int kimage_is_destination_range(struct kimage *image,
337 unsigned long start,
338 unsigned long end)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700339{
340 unsigned long i;
341
342 for (i = 0; i < image->nr_segments; i++) {
343 unsigned long mstart, mend;
Maneesh Soni72414d32005-06-25 14:58:28 -0700344
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700345 mstart = image->segment[i].mem;
Maneesh Soni72414d32005-06-25 14:58:28 -0700346 mend = mstart + image->segment[i].memsz;
347 if ((end > mstart) && (start < mend))
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700348 return 1;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700349 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700350
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700351 return 0;
352}
353
Al Viro9796fdd2005-10-21 03:22:03 -0400354static struct page *kimage_alloc_pages(gfp_t gfp_mask, unsigned int order)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700355{
356 struct page *pages;
Maneesh Soni72414d32005-06-25 14:58:28 -0700357
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700358 pages = alloc_pages(gfp_mask, order);
359 if (pages) {
360 unsigned int count, i;
361 pages->mapping = NULL;
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700362 set_page_private(pages, order);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700363 count = 1 << order;
Maneesh Soni72414d32005-06-25 14:58:28 -0700364 for (i = 0; i < count; i++)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700365 SetPageReserved(pages + i);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700366 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700367
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700368 return pages;
369}
370
371static void kimage_free_pages(struct page *page)
372{
373 unsigned int order, count, i;
Maneesh Soni72414d32005-06-25 14:58:28 -0700374
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700375 order = page_private(page);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700376 count = 1 << order;
Maneesh Soni72414d32005-06-25 14:58:28 -0700377 for (i = 0; i < count; i++)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700378 ClearPageReserved(page + i);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700379 __free_pages(page, order);
380}
381
382static void kimage_free_page_list(struct list_head *list)
383{
384 struct list_head *pos, *next;
Maneesh Soni72414d32005-06-25 14:58:28 -0700385
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700386 list_for_each_safe(pos, next, list) {
387 struct page *page;
388
389 page = list_entry(pos, struct page, lru);
390 list_del(&page->lru);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700391 kimage_free_pages(page);
392 }
393}
394
Maneesh Soni72414d32005-06-25 14:58:28 -0700395static struct page *kimage_alloc_normal_control_pages(struct kimage *image,
396 unsigned int order)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700397{
398 /* Control pages are special, they are the intermediaries
399 * that are needed while we copy the rest of the pages
400 * to their final resting place. As such they must
401 * not conflict with either the destination addresses
402 * or memory the kernel is already using.
403 *
404 * The only case where we really need more than one of
405 * these are for architectures where we cannot disable
406 * the MMU and must instead generate an identity mapped
407 * page table for all of the memory.
408 *
409 * At worst this runs in O(N) of the image size.
410 */
411 struct list_head extra_pages;
412 struct page *pages;
413 unsigned int count;
414
415 count = 1 << order;
416 INIT_LIST_HEAD(&extra_pages);
417
418 /* Loop while I can allocate a page and the page allocated
419 * is a destination page.
420 */
421 do {
422 unsigned long pfn, epfn, addr, eaddr;
Maneesh Soni72414d32005-06-25 14:58:28 -0700423
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700424 pages = kimage_alloc_pages(GFP_KERNEL, order);
425 if (!pages)
426 break;
427 pfn = page_to_pfn(pages);
428 epfn = pfn + count;
429 addr = pfn << PAGE_SHIFT;
430 eaddr = epfn << PAGE_SHIFT;
431 if ((epfn >= (KEXEC_CONTROL_MEMORY_LIMIT >> PAGE_SHIFT)) ||
Maneesh Soni72414d32005-06-25 14:58:28 -0700432 kimage_is_destination_range(image, addr, eaddr)) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700433 list_add(&pages->lru, &extra_pages);
434 pages = NULL;
435 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700436 } while (!pages);
437
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700438 if (pages) {
439 /* Remember the allocated page... */
440 list_add(&pages->lru, &image->control_pages);
441
442 /* Because the page is already in it's destination
443 * location we will never allocate another page at
444 * that address. Therefore kimage_alloc_pages
445 * will not return it (again) and we don't need
446 * to give it an entry in image->segment[].
447 */
448 }
449 /* Deal with the destination pages I have inadvertently allocated.
450 *
451 * Ideally I would convert multi-page allocations into single
452 * page allocations, and add everyting to image->dest_pages.
453 *
454 * For now it is simpler to just free the pages.
455 */
456 kimage_free_page_list(&extra_pages);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700457
Maneesh Soni72414d32005-06-25 14:58:28 -0700458 return pages;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700459}
460
Maneesh Soni72414d32005-06-25 14:58:28 -0700461static struct page *kimage_alloc_crash_control_pages(struct kimage *image,
462 unsigned int order)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700463{
464 /* Control pages are special, they are the intermediaries
465 * that are needed while we copy the rest of the pages
466 * to their final resting place. As such they must
467 * not conflict with either the destination addresses
468 * or memory the kernel is already using.
469 *
470 * Control pages are also the only pags we must allocate
471 * when loading a crash kernel. All of the other pages
472 * are specified by the segments and we just memcpy
473 * into them directly.
474 *
475 * The only case where we really need more than one of
476 * these are for architectures where we cannot disable
477 * the MMU and must instead generate an identity mapped
478 * page table for all of the memory.
479 *
480 * Given the low demand this implements a very simple
481 * allocator that finds the first hole of the appropriate
482 * size in the reserved memory region, and allocates all
483 * of the memory up to and including the hole.
484 */
485 unsigned long hole_start, hole_end, size;
486 struct page *pages;
Maneesh Soni72414d32005-06-25 14:58:28 -0700487
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700488 pages = NULL;
489 size = (1 << order) << PAGE_SHIFT;
490 hole_start = (image->control_page + (size - 1)) & ~(size - 1);
491 hole_end = hole_start + size - 1;
Maneesh Soni72414d32005-06-25 14:58:28 -0700492 while (hole_end <= crashk_res.end) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700493 unsigned long i;
Maneesh Soni72414d32005-06-25 14:58:28 -0700494
495 if (hole_end > KEXEC_CONTROL_MEMORY_LIMIT)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700496 break;
Maneesh Soni72414d32005-06-25 14:58:28 -0700497 if (hole_end > crashk_res.end)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700498 break;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700499 /* See if I overlap any of the segments */
Maneesh Soni72414d32005-06-25 14:58:28 -0700500 for (i = 0; i < image->nr_segments; i++) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700501 unsigned long mstart, mend;
Maneesh Soni72414d32005-06-25 14:58:28 -0700502
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700503 mstart = image->segment[i].mem;
504 mend = mstart + image->segment[i].memsz - 1;
505 if ((hole_end >= mstart) && (hole_start <= mend)) {
506 /* Advance the hole to the end of the segment */
507 hole_start = (mend + (size - 1)) & ~(size - 1);
508 hole_end = hole_start + size - 1;
509 break;
510 }
511 }
512 /* If I don't overlap any segments I have found my hole! */
513 if (i == image->nr_segments) {
514 pages = pfn_to_page(hole_start >> PAGE_SHIFT);
515 break;
516 }
517 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700518 if (pages)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700519 image->control_page = hole_end;
Maneesh Soni72414d32005-06-25 14:58:28 -0700520
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700521 return pages;
522}
523
524
Maneesh Soni72414d32005-06-25 14:58:28 -0700525struct page *kimage_alloc_control_pages(struct kimage *image,
526 unsigned int order)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700527{
528 struct page *pages = NULL;
Maneesh Soni72414d32005-06-25 14:58:28 -0700529
530 switch (image->type) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700531 case KEXEC_TYPE_DEFAULT:
532 pages = kimage_alloc_normal_control_pages(image, order);
533 break;
534 case KEXEC_TYPE_CRASH:
535 pages = kimage_alloc_crash_control_pages(image, order);
536 break;
537 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700538
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700539 return pages;
540}
541
542static int kimage_add_entry(struct kimage *image, kimage_entry_t entry)
543{
Maneesh Soni72414d32005-06-25 14:58:28 -0700544 if (*image->entry != 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700545 image->entry++;
Maneesh Soni72414d32005-06-25 14:58:28 -0700546
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700547 if (image->entry == image->last_entry) {
548 kimage_entry_t *ind_page;
549 struct page *page;
Maneesh Soni72414d32005-06-25 14:58:28 -0700550
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700551 page = kimage_alloc_page(image, GFP_KERNEL, KIMAGE_NO_DEST);
Maneesh Soni72414d32005-06-25 14:58:28 -0700552 if (!page)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700553 return -ENOMEM;
Maneesh Soni72414d32005-06-25 14:58:28 -0700554
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700555 ind_page = page_address(page);
556 *image->entry = virt_to_phys(ind_page) | IND_INDIRECTION;
557 image->entry = ind_page;
Maneesh Soni72414d32005-06-25 14:58:28 -0700558 image->last_entry = ind_page +
559 ((PAGE_SIZE/sizeof(kimage_entry_t)) - 1);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700560 }
561 *image->entry = entry;
562 image->entry++;
563 *image->entry = 0;
Maneesh Soni72414d32005-06-25 14:58:28 -0700564
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700565 return 0;
566}
567
Maneesh Soni72414d32005-06-25 14:58:28 -0700568static int kimage_set_destination(struct kimage *image,
569 unsigned long destination)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700570{
571 int result;
572
573 destination &= PAGE_MASK;
574 result = kimage_add_entry(image, destination | IND_DESTINATION);
Maneesh Soni72414d32005-06-25 14:58:28 -0700575 if (result == 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700576 image->destination = destination;
Maneesh Soni72414d32005-06-25 14:58:28 -0700577
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700578 return result;
579}
580
581
582static int kimage_add_page(struct kimage *image, unsigned long page)
583{
584 int result;
585
586 page &= PAGE_MASK;
587 result = kimage_add_entry(image, page | IND_SOURCE);
Maneesh Soni72414d32005-06-25 14:58:28 -0700588 if (result == 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700589 image->destination += PAGE_SIZE;
Maneesh Soni72414d32005-06-25 14:58:28 -0700590
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700591 return result;
592}
593
594
595static void kimage_free_extra_pages(struct kimage *image)
596{
597 /* Walk through and free any extra destination pages I may have */
598 kimage_free_page_list(&image->dest_pages);
599
600 /* Walk through and free any unuseable pages I have cached */
601 kimage_free_page_list(&image->unuseable_pages);
602
603}
WANG Cong7fccf032008-07-25 19:45:02 -0700604static void kimage_terminate(struct kimage *image)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700605{
Maneesh Soni72414d32005-06-25 14:58:28 -0700606 if (*image->entry != 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700607 image->entry++;
Maneesh Soni72414d32005-06-25 14:58:28 -0700608
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700609 *image->entry = IND_DONE;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700610}
611
612#define for_each_kimage_entry(image, ptr, entry) \
613 for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE); \
614 ptr = (entry & IND_INDIRECTION)? \
615 phys_to_virt((entry & PAGE_MASK)): ptr +1)
616
617static void kimage_free_entry(kimage_entry_t entry)
618{
619 struct page *page;
620
621 page = pfn_to_page(entry >> PAGE_SHIFT);
622 kimage_free_pages(page);
623}
624
625static void kimage_free(struct kimage *image)
626{
627 kimage_entry_t *ptr, entry;
628 kimage_entry_t ind = 0;
629
630 if (!image)
631 return;
Maneesh Soni72414d32005-06-25 14:58:28 -0700632
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700633 kimage_free_extra_pages(image);
634 for_each_kimage_entry(image, ptr, entry) {
635 if (entry & IND_INDIRECTION) {
636 /* Free the previous indirection page */
Maneesh Soni72414d32005-06-25 14:58:28 -0700637 if (ind & IND_INDIRECTION)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700638 kimage_free_entry(ind);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700639 /* Save this indirection page until we are
640 * done with it.
641 */
642 ind = entry;
643 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700644 else if (entry & IND_SOURCE)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700645 kimage_free_entry(entry);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700646 }
647 /* Free the final indirection page */
Maneesh Soni72414d32005-06-25 14:58:28 -0700648 if (ind & IND_INDIRECTION)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700649 kimage_free_entry(ind);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700650
651 /* Handle any machine specific cleanup */
652 machine_kexec_cleanup(image);
653
654 /* Free the kexec control pages... */
655 kimage_free_page_list(&image->control_pages);
656 kfree(image);
657}
658
Maneesh Soni72414d32005-06-25 14:58:28 -0700659static kimage_entry_t *kimage_dst_used(struct kimage *image,
660 unsigned long page)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700661{
662 kimage_entry_t *ptr, entry;
663 unsigned long destination = 0;
664
665 for_each_kimage_entry(image, ptr, entry) {
Maneesh Soni72414d32005-06-25 14:58:28 -0700666 if (entry & IND_DESTINATION)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700667 destination = entry & PAGE_MASK;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700668 else if (entry & IND_SOURCE) {
Maneesh Soni72414d32005-06-25 14:58:28 -0700669 if (page == destination)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700670 return ptr;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700671 destination += PAGE_SIZE;
672 }
673 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700674
Alexey Dobriyan314b6a42005-06-27 22:29:33 -0700675 return NULL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700676}
677
Maneesh Soni72414d32005-06-25 14:58:28 -0700678static struct page *kimage_alloc_page(struct kimage *image,
Al Viro9796fdd2005-10-21 03:22:03 -0400679 gfp_t gfp_mask,
Maneesh Soni72414d32005-06-25 14:58:28 -0700680 unsigned long destination)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700681{
682 /*
683 * Here we implement safeguards to ensure that a source page
684 * is not copied to its destination page before the data on
685 * the destination page is no longer useful.
686 *
687 * To do this we maintain the invariant that a source page is
688 * either its own destination page, or it is not a
689 * destination page at all.
690 *
691 * That is slightly stronger than required, but the proof
692 * that no problems will not occur is trivial, and the
693 * implementation is simply to verify.
694 *
695 * When allocating all pages normally this algorithm will run
696 * in O(N) time, but in the worst case it will run in O(N^2)
697 * time. If the runtime is a problem the data structures can
698 * be fixed.
699 */
700 struct page *page;
701 unsigned long addr;
702
703 /*
704 * Walk through the list of destination pages, and see if I
705 * have a match.
706 */
707 list_for_each_entry(page, &image->dest_pages, lru) {
708 addr = page_to_pfn(page) << PAGE_SHIFT;
709 if (addr == destination) {
710 list_del(&page->lru);
711 return page;
712 }
713 }
714 page = NULL;
715 while (1) {
716 kimage_entry_t *old;
717
718 /* Allocate a page, if we run out of memory give up */
719 page = kimage_alloc_pages(gfp_mask, 0);
Maneesh Soni72414d32005-06-25 14:58:28 -0700720 if (!page)
Alexey Dobriyan314b6a42005-06-27 22:29:33 -0700721 return NULL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700722 /* If the page cannot be used file it away */
Maneesh Soni72414d32005-06-25 14:58:28 -0700723 if (page_to_pfn(page) >
724 (KEXEC_SOURCE_MEMORY_LIMIT >> PAGE_SHIFT)) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700725 list_add(&page->lru, &image->unuseable_pages);
726 continue;
727 }
728 addr = page_to_pfn(page) << PAGE_SHIFT;
729
730 /* If it is the destination page we want use it */
731 if (addr == destination)
732 break;
733
734 /* If the page is not a destination page use it */
Maneesh Soni72414d32005-06-25 14:58:28 -0700735 if (!kimage_is_destination_range(image, addr,
736 addr + PAGE_SIZE))
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700737 break;
738
739 /*
740 * I know that the page is someones destination page.
741 * See if there is already a source page for this
742 * destination page. And if so swap the source pages.
743 */
744 old = kimage_dst_used(image, addr);
745 if (old) {
746 /* If so move it */
747 unsigned long old_addr;
748 struct page *old_page;
749
750 old_addr = *old & PAGE_MASK;
751 old_page = pfn_to_page(old_addr >> PAGE_SHIFT);
752 copy_highpage(page, old_page);
753 *old = addr | (*old & ~PAGE_MASK);
754
755 /* The old page I have found cannot be a
Jonathan Steelf9092f32008-09-22 13:57:45 -0700756 * destination page, so return it if it's
757 * gfp_flags honor the ones passed in.
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700758 */
Jonathan Steelf9092f32008-09-22 13:57:45 -0700759 if (!(gfp_mask & __GFP_HIGHMEM) &&
760 PageHighMem(old_page)) {
761 kimage_free_pages(old_page);
762 continue;
763 }
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700764 addr = old_addr;
765 page = old_page;
766 break;
767 }
768 else {
769 /* Place the page on the destination list I
770 * will use it later.
771 */
772 list_add(&page->lru, &image->dest_pages);
773 }
774 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700775
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700776 return page;
777}
778
779static int kimage_load_normal_segment(struct kimage *image,
Maneesh Soni72414d32005-06-25 14:58:28 -0700780 struct kexec_segment *segment)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700781{
782 unsigned long maddr;
783 unsigned long ubytes, mbytes;
784 int result;
Alexey Dobriyan314b6a42005-06-27 22:29:33 -0700785 unsigned char __user *buf;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700786
787 result = 0;
788 buf = segment->buf;
789 ubytes = segment->bufsz;
790 mbytes = segment->memsz;
791 maddr = segment->mem;
792
793 result = kimage_set_destination(image, maddr);
Maneesh Soni72414d32005-06-25 14:58:28 -0700794 if (result < 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700795 goto out;
Maneesh Soni72414d32005-06-25 14:58:28 -0700796
797 while (mbytes) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700798 struct page *page;
799 char *ptr;
800 size_t uchunk, mchunk;
Maneesh Soni72414d32005-06-25 14:58:28 -0700801
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700802 page = kimage_alloc_page(image, GFP_HIGHUSER, maddr);
Stephen Hemmingerc80544d2007-10-18 03:07:05 -0700803 if (!page) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700804 result = -ENOMEM;
805 goto out;
806 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700807 result = kimage_add_page(image, page_to_pfn(page)
808 << PAGE_SHIFT);
809 if (result < 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700810 goto out;
Maneesh Soni72414d32005-06-25 14:58:28 -0700811
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700812 ptr = kmap(page);
813 /* Start with a clear page */
814 memset(ptr, 0, PAGE_SIZE);
815 ptr += maddr & ~PAGE_MASK;
816 mchunk = PAGE_SIZE - (maddr & ~PAGE_MASK);
Maneesh Soni72414d32005-06-25 14:58:28 -0700817 if (mchunk > mbytes)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700818 mchunk = mbytes;
Maneesh Soni72414d32005-06-25 14:58:28 -0700819
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700820 uchunk = mchunk;
Maneesh Soni72414d32005-06-25 14:58:28 -0700821 if (uchunk > ubytes)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700822 uchunk = ubytes;
Maneesh Soni72414d32005-06-25 14:58:28 -0700823
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700824 result = copy_from_user(ptr, buf, uchunk);
825 kunmap(page);
826 if (result) {
827 result = (result < 0) ? result : -EIO;
828 goto out;
829 }
830 ubytes -= uchunk;
831 maddr += mchunk;
832 buf += mchunk;
833 mbytes -= mchunk;
834 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700835out:
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700836 return result;
837}
838
839static int kimage_load_crash_segment(struct kimage *image,
Maneesh Soni72414d32005-06-25 14:58:28 -0700840 struct kexec_segment *segment)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700841{
842 /* For crash dumps kernels we simply copy the data from
843 * user space to it's destination.
844 * We do things a page at a time for the sake of kmap.
845 */
846 unsigned long maddr;
847 unsigned long ubytes, mbytes;
848 int result;
Alexey Dobriyan314b6a42005-06-27 22:29:33 -0700849 unsigned char __user *buf;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700850
851 result = 0;
852 buf = segment->buf;
853 ubytes = segment->bufsz;
854 mbytes = segment->memsz;
855 maddr = segment->mem;
Maneesh Soni72414d32005-06-25 14:58:28 -0700856 while (mbytes) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700857 struct page *page;
858 char *ptr;
859 size_t uchunk, mchunk;
Maneesh Soni72414d32005-06-25 14:58:28 -0700860
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700861 page = pfn_to_page(maddr >> PAGE_SHIFT);
Stephen Hemmingerc80544d2007-10-18 03:07:05 -0700862 if (!page) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700863 result = -ENOMEM;
864 goto out;
865 }
866 ptr = kmap(page);
867 ptr += maddr & ~PAGE_MASK;
868 mchunk = PAGE_SIZE - (maddr & ~PAGE_MASK);
Maneesh Soni72414d32005-06-25 14:58:28 -0700869 if (mchunk > mbytes)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700870 mchunk = mbytes;
Maneesh Soni72414d32005-06-25 14:58:28 -0700871
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700872 uchunk = mchunk;
873 if (uchunk > ubytes) {
874 uchunk = ubytes;
875 /* Zero the trailing part of the page */
876 memset(ptr + uchunk, 0, mchunk - uchunk);
877 }
878 result = copy_from_user(ptr, buf, uchunk);
Zou Nan haia79561132006-12-07 09:51:35 -0800879 kexec_flush_icache_page(page);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700880 kunmap(page);
881 if (result) {
882 result = (result < 0) ? result : -EIO;
883 goto out;
884 }
885 ubytes -= uchunk;
886 maddr += mchunk;
887 buf += mchunk;
888 mbytes -= mchunk;
889 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700890out:
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700891 return result;
892}
893
894static int kimage_load_segment(struct kimage *image,
Maneesh Soni72414d32005-06-25 14:58:28 -0700895 struct kexec_segment *segment)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700896{
897 int result = -ENOMEM;
Maneesh Soni72414d32005-06-25 14:58:28 -0700898
899 switch (image->type) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700900 case KEXEC_TYPE_DEFAULT:
901 result = kimage_load_normal_segment(image, segment);
902 break;
903 case KEXEC_TYPE_CRASH:
904 result = kimage_load_crash_segment(image, segment);
905 break;
906 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700907
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700908 return result;
909}
910
911/*
912 * Exec Kernel system call: for obvious reasons only root may call it.
913 *
914 * This call breaks up into three pieces.
915 * - A generic part which loads the new kernel from the current
916 * address space, and very carefully places the data in the
917 * allocated pages.
918 *
919 * - A generic part that interacts with the kernel and tells all of
920 * the devices to shut down. Preventing on-going dmas, and placing
921 * the devices in a consistent state so a later kernel can
922 * reinitialize them.
923 *
924 * - A machine specific part that includes the syscall number
925 * and the copies the image to it's final destination. And
926 * jumps into the image at entry.
927 *
928 * kexec does not sync, or unmount filesystems so if you need
929 * that to happen you need to do that yourself.
930 */
Jeff Moyerc330dda2006-06-23 02:05:07 -0700931struct kimage *kexec_image;
932struct kimage *kexec_crash_image;
Andrew Morton8c5a1cf2008-08-15 00:40:27 -0700933
934static DEFINE_MUTEX(kexec_mutex);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700935
Maneesh Soni72414d32005-06-25 14:58:28 -0700936asmlinkage long sys_kexec_load(unsigned long entry, unsigned long nr_segments,
937 struct kexec_segment __user *segments,
938 unsigned long flags)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700939{
940 struct kimage **dest_image, *image;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700941 int result;
942
943 /* We only trust the superuser with rebooting the system. */
944 if (!capable(CAP_SYS_BOOT))
945 return -EPERM;
946
947 /*
948 * Verify we have a legal set of flags
949 * This leaves us room for future extensions.
950 */
951 if ((flags & KEXEC_FLAGS) != (flags & ~KEXEC_ARCH_MASK))
952 return -EINVAL;
953
954 /* Verify we are on the appropriate architecture */
955 if (((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH) &&
956 ((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH_DEFAULT))
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700957 return -EINVAL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700958
959 /* Put an artificial cap on the number
960 * of segments passed to kexec_load.
961 */
962 if (nr_segments > KEXEC_SEGMENT_MAX)
963 return -EINVAL;
964
965 image = NULL;
966 result = 0;
967
968 /* Because we write directly to the reserved memory
969 * region when loading crash kernels we need a mutex here to
970 * prevent multiple crash kernels from attempting to load
971 * simultaneously, and to prevent a crash kernel from loading
972 * over the top of a in use crash kernel.
973 *
974 * KISS: always take the mutex.
975 */
Andrew Morton8c5a1cf2008-08-15 00:40:27 -0700976 if (!mutex_trylock(&kexec_mutex))
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700977 return -EBUSY;
Maneesh Soni72414d32005-06-25 14:58:28 -0700978
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700979 dest_image = &kexec_image;
Maneesh Soni72414d32005-06-25 14:58:28 -0700980 if (flags & KEXEC_ON_CRASH)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700981 dest_image = &kexec_crash_image;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700982 if (nr_segments > 0) {
983 unsigned long i;
Maneesh Soni72414d32005-06-25 14:58:28 -0700984
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700985 /* Loading another kernel to reboot into */
Maneesh Soni72414d32005-06-25 14:58:28 -0700986 if ((flags & KEXEC_ON_CRASH) == 0)
987 result = kimage_normal_alloc(&image, entry,
988 nr_segments, segments);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700989 /* Loading another kernel to switch to if this one crashes */
990 else if (flags & KEXEC_ON_CRASH) {
991 /* Free any current crash dump kernel before
992 * we corrupt it.
993 */
994 kimage_free(xchg(&kexec_crash_image, NULL));
Maneesh Soni72414d32005-06-25 14:58:28 -0700995 result = kimage_crash_alloc(&image, entry,
996 nr_segments, segments);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700997 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700998 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700999 goto out;
Maneesh Soni72414d32005-06-25 14:58:28 -07001000
Huang Ying3ab83522008-07-25 19:45:07 -07001001 if (flags & KEXEC_PRESERVE_CONTEXT)
1002 image->preserve_context = 1;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001003 result = machine_kexec_prepare(image);
Maneesh Soni72414d32005-06-25 14:58:28 -07001004 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001005 goto out;
Maneesh Soni72414d32005-06-25 14:58:28 -07001006
1007 for (i = 0; i < nr_segments; i++) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001008 result = kimage_load_segment(image, &image->segment[i]);
Maneesh Soni72414d32005-06-25 14:58:28 -07001009 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001010 goto out;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001011 }
WANG Cong7fccf032008-07-25 19:45:02 -07001012 kimage_terminate(image);
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001013 }
1014 /* Install the new kernel, and Uninstall the old */
1015 image = xchg(dest_image, image);
1016
Maneesh Soni72414d32005-06-25 14:58:28 -07001017out:
Andrew Morton8c5a1cf2008-08-15 00:40:27 -07001018 mutex_unlock(&kexec_mutex);
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001019 kimage_free(image);
Maneesh Soni72414d32005-06-25 14:58:28 -07001020
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001021 return result;
1022}
1023
1024#ifdef CONFIG_COMPAT
1025asmlinkage long compat_sys_kexec_load(unsigned long entry,
Maneesh Soni72414d32005-06-25 14:58:28 -07001026 unsigned long nr_segments,
1027 struct compat_kexec_segment __user *segments,
1028 unsigned long flags)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001029{
1030 struct compat_kexec_segment in;
1031 struct kexec_segment out, __user *ksegments;
1032 unsigned long i, result;
1033
1034 /* Don't allow clients that don't understand the native
1035 * architecture to do anything.
1036 */
Maneesh Soni72414d32005-06-25 14:58:28 -07001037 if ((flags & KEXEC_ARCH_MASK) == KEXEC_ARCH_DEFAULT)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001038 return -EINVAL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001039
Maneesh Soni72414d32005-06-25 14:58:28 -07001040 if (nr_segments > KEXEC_SEGMENT_MAX)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001041 return -EINVAL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001042
1043 ksegments = compat_alloc_user_space(nr_segments * sizeof(out));
1044 for (i=0; i < nr_segments; i++) {
1045 result = copy_from_user(&in, &segments[i], sizeof(in));
Maneesh Soni72414d32005-06-25 14:58:28 -07001046 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001047 return -EFAULT;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001048
1049 out.buf = compat_ptr(in.buf);
1050 out.bufsz = in.bufsz;
1051 out.mem = in.mem;
1052 out.memsz = in.memsz;
1053
1054 result = copy_to_user(&ksegments[i], &out, sizeof(out));
Maneesh Soni72414d32005-06-25 14:58:28 -07001055 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001056 return -EFAULT;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001057 }
1058
1059 return sys_kexec_load(entry, nr_segments, ksegments, flags);
1060}
1061#endif
1062
Alexander Nyberg6e274d12005-06-25 14:58:26 -07001063void crash_kexec(struct pt_regs *regs)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001064{
Andrew Morton8c5a1cf2008-08-15 00:40:27 -07001065 /* Take the kexec_mutex here to prevent sys_kexec_load
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001066 * running on one cpu from replacing the crash kernel
1067 * we are using after a panic on a different cpu.
1068 *
1069 * If the crash kernel was not located in a fixed area
1070 * of memory the xchg(&kexec_crash_image) would be
1071 * sufficient. But since I reuse the memory...
1072 */
Andrew Morton8c5a1cf2008-08-15 00:40:27 -07001073 if (mutex_trylock(&kexec_mutex)) {
David Wilderc0ce7d02006-06-23 15:29:34 -07001074 if (kexec_crash_image) {
Vivek Goyale996e582006-01-09 20:51:44 -08001075 struct pt_regs fixed_regs;
1076 crash_setup_regs(&fixed_regs, regs);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001077 crash_save_vmcoreinfo();
Vivek Goyale996e582006-01-09 20:51:44 -08001078 machine_crash_shutdown(&fixed_regs);
David Wilderc0ce7d02006-06-23 15:29:34 -07001079 machine_kexec(kexec_crash_image);
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001080 }
Andrew Morton8c5a1cf2008-08-15 00:40:27 -07001081 mutex_unlock(&kexec_mutex);
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001082 }
1083}
Vivek Goyalcc571652006-01-09 20:51:41 -08001084
Magnus Damm85916f82006-12-06 20:40:41 -08001085static u32 *append_elf_note(u32 *buf, char *name, unsigned type, void *data,
1086 size_t data_len)
1087{
1088 struct elf_note note;
1089
1090 note.n_namesz = strlen(name) + 1;
1091 note.n_descsz = data_len;
1092 note.n_type = type;
1093 memcpy(buf, &note, sizeof(note));
1094 buf += (sizeof(note) + 3)/4;
1095 memcpy(buf, name, note.n_namesz);
1096 buf += (note.n_namesz + 3)/4;
1097 memcpy(buf, data, note.n_descsz);
1098 buf += (note.n_descsz + 3)/4;
1099
1100 return buf;
1101}
1102
1103static void final_note(u32 *buf)
1104{
1105 struct elf_note note;
1106
1107 note.n_namesz = 0;
1108 note.n_descsz = 0;
1109 note.n_type = 0;
1110 memcpy(buf, &note, sizeof(note));
1111}
1112
1113void crash_save_cpu(struct pt_regs *regs, int cpu)
1114{
1115 struct elf_prstatus prstatus;
1116 u32 *buf;
1117
1118 if ((cpu < 0) || (cpu >= NR_CPUS))
1119 return;
1120
1121 /* Using ELF notes here is opportunistic.
1122 * I need a well defined structure format
1123 * for the data I pass, and I need tags
1124 * on the data to indicate what information I have
1125 * squirrelled away. ELF notes happen to provide
1126 * all of that, so there is no need to invent something new.
1127 */
1128 buf = (u32*)per_cpu_ptr(crash_notes, cpu);
1129 if (!buf)
1130 return;
1131 memset(&prstatus, 0, sizeof(prstatus));
1132 prstatus.pr_pid = current->pid;
1133 elf_core_copy_regs(&prstatus.pr_reg, regs);
Simon Horman6672f762007-05-08 00:28:22 -07001134 buf = append_elf_note(buf, KEXEC_CORE_NOTE_NAME, NT_PRSTATUS,
1135 &prstatus, sizeof(prstatus));
Magnus Damm85916f82006-12-06 20:40:41 -08001136 final_note(buf);
1137}
1138
Vivek Goyalcc571652006-01-09 20:51:41 -08001139static int __init crash_notes_memory_init(void)
1140{
1141 /* Allocate memory for saving cpu registers. */
1142 crash_notes = alloc_percpu(note_buf_t);
1143 if (!crash_notes) {
1144 printk("Kexec: Memory allocation for saving cpu register"
1145 " states failed\n");
1146 return -ENOMEM;
1147 }
1148 return 0;
1149}
1150module_init(crash_notes_memory_init)
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001151
Bernhard Wallecba63c32007-10-18 23:40:58 -07001152
1153/*
1154 * parsing the "crashkernel" commandline
1155 *
1156 * this code is intended to be called from architecture specific code
1157 */
1158
1159
1160/*
1161 * This function parses command lines in the format
1162 *
1163 * crashkernel=ramsize-range:size[,...][@offset]
1164 *
1165 * The function returns 0 on success and -EINVAL on failure.
1166 */
1167static int __init parse_crashkernel_mem(char *cmdline,
1168 unsigned long long system_ram,
1169 unsigned long long *crash_size,
1170 unsigned long long *crash_base)
1171{
1172 char *cur = cmdline, *tmp;
1173
1174 /* for each entry of the comma-separated list */
1175 do {
1176 unsigned long long start, end = ULLONG_MAX, size;
1177
1178 /* get the start of the range */
1179 start = memparse(cur, &tmp);
1180 if (cur == tmp) {
1181 pr_warning("crashkernel: Memory value expected\n");
1182 return -EINVAL;
1183 }
1184 cur = tmp;
1185 if (*cur != '-') {
1186 pr_warning("crashkernel: '-' expected\n");
1187 return -EINVAL;
1188 }
1189 cur++;
1190
1191 /* if no ':' is here, than we read the end */
1192 if (*cur != ':') {
1193 end = memparse(cur, &tmp);
1194 if (cur == tmp) {
1195 pr_warning("crashkernel: Memory "
1196 "value expected\n");
1197 return -EINVAL;
1198 }
1199 cur = tmp;
1200 if (end <= start) {
1201 pr_warning("crashkernel: end <= start\n");
1202 return -EINVAL;
1203 }
1204 }
1205
1206 if (*cur != ':') {
1207 pr_warning("crashkernel: ':' expected\n");
1208 return -EINVAL;
1209 }
1210 cur++;
1211
1212 size = memparse(cur, &tmp);
1213 if (cur == tmp) {
1214 pr_warning("Memory value expected\n");
1215 return -EINVAL;
1216 }
1217 cur = tmp;
1218 if (size >= system_ram) {
1219 pr_warning("crashkernel: invalid size\n");
1220 return -EINVAL;
1221 }
1222
1223 /* match ? */
Michael Ellermanbe089d792008-05-01 04:34:49 -07001224 if (system_ram >= start && system_ram < end) {
Bernhard Wallecba63c32007-10-18 23:40:58 -07001225 *crash_size = size;
1226 break;
1227 }
1228 } while (*cur++ == ',');
1229
1230 if (*crash_size > 0) {
1231 while (*cur != ' ' && *cur != '@')
1232 cur++;
1233 if (*cur == '@') {
1234 cur++;
1235 *crash_base = memparse(cur, &tmp);
1236 if (cur == tmp) {
1237 pr_warning("Memory value expected "
1238 "after '@'\n");
1239 return -EINVAL;
1240 }
1241 }
1242 }
1243
1244 return 0;
1245}
1246
1247/*
1248 * That function parses "simple" (old) crashkernel command lines like
1249 *
1250 * crashkernel=size[@offset]
1251 *
1252 * It returns 0 on success and -EINVAL on failure.
1253 */
1254static int __init parse_crashkernel_simple(char *cmdline,
1255 unsigned long long *crash_size,
1256 unsigned long long *crash_base)
1257{
1258 char *cur = cmdline;
1259
1260 *crash_size = memparse(cmdline, &cur);
1261 if (cmdline == cur) {
1262 pr_warning("crashkernel: memory value expected\n");
1263 return -EINVAL;
1264 }
1265
1266 if (*cur == '@')
1267 *crash_base = memparse(cur+1, &cur);
1268
1269 return 0;
1270}
1271
1272/*
1273 * That function is the entry point for command line parsing and should be
1274 * called from the arch-specific code.
1275 */
1276int __init parse_crashkernel(char *cmdline,
1277 unsigned long long system_ram,
1278 unsigned long long *crash_size,
1279 unsigned long long *crash_base)
1280{
1281 char *p = cmdline, *ck_cmdline = NULL;
1282 char *first_colon, *first_space;
1283
1284 BUG_ON(!crash_size || !crash_base);
1285 *crash_size = 0;
1286 *crash_base = 0;
1287
1288 /* find crashkernel and use the last one if there are more */
1289 p = strstr(p, "crashkernel=");
1290 while (p) {
1291 ck_cmdline = p;
1292 p = strstr(p+1, "crashkernel=");
1293 }
1294
1295 if (!ck_cmdline)
1296 return -EINVAL;
1297
1298 ck_cmdline += 12; /* strlen("crashkernel=") */
1299
1300 /*
1301 * if the commandline contains a ':', then that's the extended
1302 * syntax -- if not, it must be the classic syntax
1303 */
1304 first_colon = strchr(ck_cmdline, ':');
1305 first_space = strchr(ck_cmdline, ' ');
1306 if (first_colon && (!first_space || first_colon < first_space))
1307 return parse_crashkernel_mem(ck_cmdline, system_ram,
1308 crash_size, crash_base);
1309 else
1310 return parse_crashkernel_simple(ck_cmdline, crash_size,
1311 crash_base);
1312
1313 return 0;
1314}
1315
1316
1317
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001318void crash_save_vmcoreinfo(void)
1319{
1320 u32 *buf;
1321
1322 if (!vmcoreinfo_size)
1323 return;
1324
Ken'ichi Ohmichid7682812007-10-16 23:27:28 -07001325 vmcoreinfo_append_str("CRASHTIME=%ld", get_seconds());
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001326
1327 buf = (u32 *)vmcoreinfo_note;
1328
1329 buf = append_elf_note(buf, VMCOREINFO_NOTE_NAME, 0, vmcoreinfo_data,
1330 vmcoreinfo_size);
1331
1332 final_note(buf);
1333}
1334
1335void vmcoreinfo_append_str(const char *fmt, ...)
1336{
1337 va_list args;
1338 char buf[0x50];
1339 int r;
1340
1341 va_start(args, fmt);
1342 r = vsnprintf(buf, sizeof(buf), fmt, args);
1343 va_end(args);
1344
1345 if (r + vmcoreinfo_size > vmcoreinfo_max_size)
1346 r = vmcoreinfo_max_size - vmcoreinfo_size;
1347
1348 memcpy(&vmcoreinfo_data[vmcoreinfo_size], buf, r);
1349
1350 vmcoreinfo_size += r;
1351}
1352
1353/*
1354 * provide an empty default implementation here -- architecture
1355 * code may override this
1356 */
1357void __attribute__ ((weak)) arch_crash_save_vmcoreinfo(void)
1358{}
1359
1360unsigned long __attribute__ ((weak)) paddr_vmcoreinfo_note(void)
1361{
1362 return __pa((unsigned long)(char *)&vmcoreinfo_note);
1363}
1364
1365static int __init crash_save_vmcoreinfo_init(void)
1366{
Ken'ichi Ohmichibba1f602008-02-07 00:15:22 -08001367 VMCOREINFO_OSRELEASE(init_uts_ns.name.release);
1368 VMCOREINFO_PAGESIZE(PAGE_SIZE);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001369
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001370 VMCOREINFO_SYMBOL(init_uts_ns);
1371 VMCOREINFO_SYMBOL(node_online_map);
1372 VMCOREINFO_SYMBOL(swapper_pg_dir);
1373 VMCOREINFO_SYMBOL(_stext);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001374
1375#ifndef CONFIG_NEED_MULTIPLE_NODES
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001376 VMCOREINFO_SYMBOL(mem_map);
1377 VMCOREINFO_SYMBOL(contig_page_data);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001378#endif
1379#ifdef CONFIG_SPARSEMEM
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001380 VMCOREINFO_SYMBOL(mem_section);
1381 VMCOREINFO_LENGTH(mem_section, NR_SECTION_ROOTS);
Ken'ichi Ohmichic76f8602008-02-07 00:15:20 -08001382 VMCOREINFO_STRUCT_SIZE(mem_section);
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001383 VMCOREINFO_OFFSET(mem_section, section_mem_map);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001384#endif
Ken'ichi Ohmichic76f8602008-02-07 00:15:20 -08001385 VMCOREINFO_STRUCT_SIZE(page);
1386 VMCOREINFO_STRUCT_SIZE(pglist_data);
1387 VMCOREINFO_STRUCT_SIZE(zone);
1388 VMCOREINFO_STRUCT_SIZE(free_area);
1389 VMCOREINFO_STRUCT_SIZE(list_head);
1390 VMCOREINFO_SIZE(nodemask_t);
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001391 VMCOREINFO_OFFSET(page, flags);
1392 VMCOREINFO_OFFSET(page, _count);
1393 VMCOREINFO_OFFSET(page, mapping);
1394 VMCOREINFO_OFFSET(page, lru);
1395 VMCOREINFO_OFFSET(pglist_data, node_zones);
1396 VMCOREINFO_OFFSET(pglist_data, nr_zones);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001397#ifdef CONFIG_FLAT_NODE_MEM_MAP
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001398 VMCOREINFO_OFFSET(pglist_data, node_mem_map);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001399#endif
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001400 VMCOREINFO_OFFSET(pglist_data, node_start_pfn);
1401 VMCOREINFO_OFFSET(pglist_data, node_spanned_pages);
1402 VMCOREINFO_OFFSET(pglist_data, node_id);
1403 VMCOREINFO_OFFSET(zone, free_area);
1404 VMCOREINFO_OFFSET(zone, vm_stat);
1405 VMCOREINFO_OFFSET(zone, spanned_pages);
1406 VMCOREINFO_OFFSET(free_area, free_list);
1407 VMCOREINFO_OFFSET(list_head, next);
1408 VMCOREINFO_OFFSET(list_head, prev);
1409 VMCOREINFO_LENGTH(zone.free_area, MAX_ORDER);
Ken'ichi Ohmichi83a08e72008-01-08 15:33:05 -08001410 VMCOREINFO_LENGTH(free_area.free_list, MIGRATE_TYPES);
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001411 VMCOREINFO_NUMBER(NR_FREE_PAGES);
Ken'ichi Ohmichi122c7a52008-04-28 02:13:04 -07001412 VMCOREINFO_NUMBER(PG_lru);
1413 VMCOREINFO_NUMBER(PG_private);
1414 VMCOREINFO_NUMBER(PG_swapcache);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001415
1416 arch_crash_save_vmcoreinfo();
1417
1418 return 0;
1419}
1420
1421module_init(crash_save_vmcoreinfo_init)
Huang Ying3ab83522008-07-25 19:45:07 -07001422
Huang Ying7ade3fc2008-08-15 00:40:21 -07001423/*
1424 * Move into place and start executing a preloaded standalone
1425 * executable. If nothing was preloaded return an error.
Huang Ying3ab83522008-07-25 19:45:07 -07001426 */
1427int kernel_kexec(void)
1428{
1429 int error = 0;
1430
Andrew Morton8c5a1cf2008-08-15 00:40:27 -07001431 if (!mutex_trylock(&kexec_mutex))
Huang Ying3ab83522008-07-25 19:45:07 -07001432 return -EBUSY;
1433 if (!kexec_image) {
1434 error = -EINVAL;
1435 goto Unlock;
1436 }
1437
Huang Ying3ab83522008-07-25 19:45:07 -07001438#ifdef CONFIG_KEXEC_JUMP
Huang Ying7ade3fc2008-08-15 00:40:21 -07001439 if (kexec_image->preserve_context) {
Huang Ying89081d12008-07-25 19:45:10 -07001440 mutex_lock(&pm_mutex);
1441 pm_prepare_console();
1442 error = freeze_processes();
1443 if (error) {
1444 error = -EBUSY;
1445 goto Restore_console;
1446 }
1447 suspend_console();
1448 error = device_suspend(PMSG_FREEZE);
1449 if (error)
1450 goto Resume_console;
1451 error = disable_nonboot_cpus();
1452 if (error)
1453 goto Resume_devices;
Huang Ying73bd9c72008-08-15 00:40:24 -07001454 device_pm_lock();
Huang Ying3ab83522008-07-25 19:45:07 -07001455 local_irq_disable();
Huang Ying89081d12008-07-25 19:45:10 -07001456 /* At this point, device_suspend() has been called,
1457 * but *not* device_power_down(). We *must*
1458 * device_power_down() now. Otherwise, drivers for
1459 * some devices (e.g. interrupt controllers) become
1460 * desynchronized with the actual state of the
1461 * hardware at resume time, and evil weirdness ensues.
1462 */
1463 error = device_power_down(PMSG_FREEZE);
1464 if (error)
1465 goto Enable_irqs;
Huang Ying7ade3fc2008-08-15 00:40:21 -07001466 } else
Huang Ying3ab83522008-07-25 19:45:07 -07001467#endif
Huang Ying7ade3fc2008-08-15 00:40:21 -07001468 {
Huang Yingca195b72008-08-15 00:40:24 -07001469 kernel_restart_prepare(NULL);
Huang Ying3ab83522008-07-25 19:45:07 -07001470 printk(KERN_EMERG "Starting new kernel\n");
1471 machine_shutdown();
1472 }
1473
1474 machine_kexec(kexec_image);
1475
Huang Ying3ab83522008-07-25 19:45:07 -07001476#ifdef CONFIG_KEXEC_JUMP
Huang Ying7ade3fc2008-08-15 00:40:21 -07001477 if (kexec_image->preserve_context) {
Huang Ying89081d12008-07-25 19:45:10 -07001478 device_power_up(PMSG_RESTORE);
1479 Enable_irqs:
Huang Ying3ab83522008-07-25 19:45:07 -07001480 local_irq_enable();
Huang Ying73bd9c72008-08-15 00:40:24 -07001481 device_pm_unlock();
Huang Ying89081d12008-07-25 19:45:10 -07001482 enable_nonboot_cpus();
1483 Resume_devices:
1484 device_resume(PMSG_RESTORE);
1485 Resume_console:
1486 resume_console();
1487 thaw_processes();
1488 Restore_console:
1489 pm_restore_console();
1490 mutex_unlock(&pm_mutex);
Huang Ying3ab83522008-07-25 19:45:07 -07001491 }
Huang Ying7ade3fc2008-08-15 00:40:21 -07001492#endif
Huang Ying3ab83522008-07-25 19:45:07 -07001493
1494 Unlock:
Andrew Morton8c5a1cf2008-08-15 00:40:27 -07001495 mutex_unlock(&kexec_mutex);
Huang Ying3ab83522008-07-25 19:45:07 -07001496 return error;
1497}