blob: bfdda316697de90cf4ad700d49ca783dd23bc0b3 [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/utsname.h>
25#include <linux/numa.h>
Huang Ying3ab83522008-07-25 19:45:07 -070026#include <linux/suspend.h>
27#include <linux/device.h>
Huang Ying89081d12008-07-25 19:45:10 -070028#include <linux/freezer.h>
29#include <linux/pm.h>
30#include <linux/cpu.h>
31#include <linux/console.h>
Luck, Tony5f41b8c2008-10-20 15:23:40 -070032#include <linux/vmalloc.h>
Amerigo Wang06a7f712009-12-15 16:47:46 -080033#include <linux/swap.h>
Rafael J. Wysocki19234c02011-04-20 00:36:11 +020034#include <linux/syscore_ops.h>
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -070035#include <linux/compiler.h>
Atsushi Kumagai8f1d26d2014-07-30 16:08:39 -070036#include <linux/hugetlb.h>
Alexander Nyberg6e274d12005-06-25 14:58:26 -070037
Eric W. Biedermandc009d92005-06-25 14:57:52 -070038#include <asm/page.h>
39#include <asm/uaccess.h>
40#include <asm/io.h>
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -070041#include <asm/sections.h>
Eric W. Biedermandc009d92005-06-25 14:57:52 -070042
Vivek Goyalcc571652006-01-09 20:51:41 -080043/* Per cpu memory for storing cpu states in case of system crash. */
Tejun Heo43cf38e2010-02-02 14:38:57 +090044note_buf_t __percpu *crash_notes;
Vivek Goyalcc571652006-01-09 20:51:41 -080045
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -070046/* vmcoreinfo stuff */
Dmitri Vorobievedb79a22009-04-02 16:58:58 -070047static unsigned char vmcoreinfo_data[VMCOREINFO_BYTES];
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -070048u32 vmcoreinfo_note[VMCOREINFO_NOTE_SIZE/4];
Ken'ichi Ohmichid7682812007-10-16 23:27:28 -070049size_t vmcoreinfo_size;
50size_t vmcoreinfo_max_size = sizeof(vmcoreinfo_data);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -070051
Khalid Aziz4fc9bbf2013-11-27 15:19:25 -070052/* Flag to indicate we are going to kexec a new kernel */
53bool kexec_in_progress = false;
54
Eric W. Biedermandc009d92005-06-25 14:57:52 -070055/* Location of the reserved area for the crash kernel */
56struct resource crashk_res = {
57 .name = "Crash kernel",
58 .start = 0,
59 .end = 0,
60 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
61};
Yinghai Lu0212f912013-01-24 12:20:11 -080062struct resource crashk_low_res = {
Yinghai Lu157752d2013-04-15 22:23:46 -070063 .name = "Crash kernel",
Yinghai Lu0212f912013-01-24 12:20:11 -080064 .start = 0,
65 .end = 0,
66 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
67};
Eric W. Biedermandc009d92005-06-25 14:57:52 -070068
Alexander Nyberg6e274d12005-06-25 14:58:26 -070069int kexec_should_crash(struct task_struct *p)
70{
Serge E. Hallynb460cbc2007-10-18 23:39:52 -070071 if (in_interrupt() || !p->pid || is_global_init(p) || panic_on_oops)
Alexander Nyberg6e274d12005-06-25 14:58:26 -070072 return 1;
73 return 0;
74}
75
Eric W. Biedermandc009d92005-06-25 14:57:52 -070076/*
77 * When kexec transitions to the new kernel there is a one-to-one
78 * mapping between physical and virtual addresses. On processors
79 * where you can disable the MMU this is trivial, and easy. For
80 * others it is still a simple predictable page table to setup.
81 *
82 * In that environment kexec copies the new kernel to its final
83 * resting place. This means I can only support memory whose
84 * physical address can fit in an unsigned long. In particular
85 * addresses where (pfn << PAGE_SHIFT) > ULONG_MAX cannot be handled.
86 * If the assembly stub has more restrictive requirements
87 * KEXEC_SOURCE_MEMORY_LIMIT and KEXEC_DEST_MEMORY_LIMIT can be
88 * defined more restrictively in <asm/kexec.h>.
89 *
90 * The code for the transition from the current kernel to the
91 * the new kernel is placed in the control_code_buffer, whose size
Huang Ying163f6872008-08-15 00:40:22 -070092 * is given by KEXEC_CONTROL_PAGE_SIZE. In the best case only a single
Eric W. Biedermandc009d92005-06-25 14:57:52 -070093 * page of memory is necessary, but some architectures require more.
94 * Because this memory must be identity mapped in the transition from
95 * virtual to physical addresses it must live in the range
96 * 0 - TASK_SIZE, as only the user space mappings are arbitrarily
97 * modifiable.
98 *
99 * The assembly stub in the control code buffer is passed a linked list
100 * of descriptor pages detailing the source pages of the new kernel,
101 * and the destination addresses of those source pages. As this data
102 * structure is not used in the context of the current OS, it must
103 * be self-contained.
104 *
105 * The code has been made to work with highmem pages and will use a
106 * destination page in its final resting place (if it happens
107 * to allocate it). The end product of this is that most of the
108 * physical address space, and most of RAM can be used.
109 *
110 * Future directions include:
111 * - allocating a page table with the control code buffer identity
112 * mapped, to simplify machine_kexec and make kexec_on_panic more
113 * reliable.
114 */
115
116/*
117 * KIMAGE_NO_DEST is an impossible destination address..., for
118 * allocating pages whose destination address we do not care about.
119 */
120#define KIMAGE_NO_DEST (-1UL)
121
Maneesh Soni72414d32005-06-25 14:58:28 -0700122static int kimage_is_destination_range(struct kimage *image,
123 unsigned long start, unsigned long end);
124static struct page *kimage_alloc_page(struct kimage *image,
Al Viro9796fdd2005-10-21 03:22:03 -0400125 gfp_t gfp_mask,
Maneesh Soni72414d32005-06-25 14:58:28 -0700126 unsigned long dest);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700127
Vivek Goyaldabe7862014-08-08 14:25:45 -0700128static int copy_user_segment_list(struct kimage *image,
129 unsigned long nr_segments,
130 struct kexec_segment __user *segments)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700131{
Vivek Goyaldabe7862014-08-08 14:25:45 -0700132 int ret;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700133 size_t segment_bytes;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700134
135 /* Read in the segments */
136 image->nr_segments = nr_segments;
137 segment_bytes = nr_segments * sizeof(*segments);
Vivek Goyaldabe7862014-08-08 14:25:45 -0700138 ret = copy_from_user(image->segment, segments, segment_bytes);
139 if (ret)
140 ret = -EFAULT;
141
142 return ret;
143}
144
145static int sanity_check_segment_list(struct kimage *image)
146{
147 int result, i;
148 unsigned long nr_segments = image->nr_segments;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700149
150 /*
151 * Verify we have good destination addresses. The caller is
152 * responsible for making certain we don't attempt to load
153 * the new image into invalid or reserved areas of RAM. This
154 * just verifies it is an address we can use.
155 *
156 * Since the kernel does everything in page size chunks ensure
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400157 * the destination addresses are page aligned. Too many
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700158 * special cases crop of when we don't do this. The most
159 * insidious is getting overlapping destination addresses
160 * simply because addresses are changed to page size
161 * granularity.
162 */
163 result = -EADDRNOTAVAIL;
164 for (i = 0; i < nr_segments; i++) {
165 unsigned long mstart, mend;
Maneesh Soni72414d32005-06-25 14:58:28 -0700166
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700167 mstart = image->segment[i].mem;
168 mend = mstart + image->segment[i].memsz;
169 if ((mstart & ~PAGE_MASK) || (mend & ~PAGE_MASK))
Vivek Goyaldabe7862014-08-08 14:25:45 -0700170 return result;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700171 if (mend >= KEXEC_DESTINATION_MEMORY_LIMIT)
Vivek Goyaldabe7862014-08-08 14:25:45 -0700172 return result;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700173 }
174
175 /* Verify our destination addresses do not overlap.
176 * If we alloed overlapping destination addresses
177 * through very weird things can happen with no
178 * easy explanation as one segment stops on another.
179 */
180 result = -EINVAL;
Maneesh Soni72414d32005-06-25 14:58:28 -0700181 for (i = 0; i < nr_segments; i++) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700182 unsigned long mstart, mend;
183 unsigned long j;
Maneesh Soni72414d32005-06-25 14:58:28 -0700184
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700185 mstart = image->segment[i].mem;
186 mend = mstart + image->segment[i].memsz;
Maneesh Soni72414d32005-06-25 14:58:28 -0700187 for (j = 0; j < i; j++) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700188 unsigned long pstart, pend;
189 pstart = image->segment[j].mem;
190 pend = pstart + image->segment[j].memsz;
191 /* Do the segments overlap ? */
192 if ((mend > pstart) && (mstart < pend))
Vivek Goyaldabe7862014-08-08 14:25:45 -0700193 return result;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700194 }
195 }
196
197 /* Ensure our buffer sizes are strictly less than
198 * our memory sizes. This should always be the case,
199 * and it is easier to check up front than to be surprised
200 * later on.
201 */
202 result = -EINVAL;
Maneesh Soni72414d32005-06-25 14:58:28 -0700203 for (i = 0; i < nr_segments; i++) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700204 if (image->segment[i].bufsz > image->segment[i].memsz)
Vivek Goyaldabe7862014-08-08 14:25:45 -0700205 return result;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700206 }
207
Vivek Goyaldabe7862014-08-08 14:25:45 -0700208 /*
209 * Verify we have good destination addresses. Normally
210 * the caller is responsible for making certain we don't
211 * attempt to load the new image into invalid or reserved
212 * areas of RAM. But crash kernels are preloaded into a
213 * reserved area of ram. We must ensure the addresses
214 * are in the reserved area otherwise preloading the
215 * kernel could corrupt things.
216 */
Maneesh Soni72414d32005-06-25 14:58:28 -0700217
Vivek Goyaldabe7862014-08-08 14:25:45 -0700218 if (image->type == KEXEC_TYPE_CRASH) {
219 result = -EADDRNOTAVAIL;
220 for (i = 0; i < nr_segments; i++) {
221 unsigned long mstart, mend;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700222
Vivek Goyaldabe7862014-08-08 14:25:45 -0700223 mstart = image->segment[i].mem;
224 mend = mstart + image->segment[i].memsz - 1;
225 /* Ensure we are within the crash kernel limits */
226 if ((mstart < crashk_res.start) ||
227 (mend > crashk_res.end))
228 return result;
229 }
230 }
231
232 return 0;
233}
234
235static struct kimage *do_kimage_alloc_init(void)
236{
237 struct kimage *image;
238
239 /* Allocate a controlling structure */
240 image = kzalloc(sizeof(*image), GFP_KERNEL);
241 if (!image)
242 return NULL;
243
244 image->head = 0;
245 image->entry = &image->head;
246 image->last_entry = &image->head;
247 image->control_page = ~0; /* By default this does not apply */
248 image->type = KEXEC_TYPE_DEFAULT;
249
250 /* Initialize the list of control pages */
251 INIT_LIST_HEAD(&image->control_pages);
252
253 /* Initialize the list of destination pages */
254 INIT_LIST_HEAD(&image->dest_pages);
255
256 /* Initialize the list of unusable pages */
257 INIT_LIST_HEAD(&image->unusable_pages);
258
259 return image;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700260}
261
Zhang Yanfeib92e7e02013-02-27 17:03:29 -0800262static void kimage_free_page_list(struct list_head *list);
263
Vivek Goyal255aedd2014-08-08 14:25:48 -0700264static int kimage_alloc_init(struct kimage **rimage, unsigned long entry,
265 unsigned long nr_segments,
266 struct kexec_segment __user *segments,
267 unsigned long flags)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700268{
Vivek Goyal255aedd2014-08-08 14:25:48 -0700269 int ret;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700270 struct kimage *image;
Vivek Goyal255aedd2014-08-08 14:25:48 -0700271 bool kexec_on_panic = flags & KEXEC_ON_CRASH;
272
273 if (kexec_on_panic) {
274 /* Verify we have a valid entry point */
275 if ((entry < crashk_res.start) || (entry > crashk_res.end))
276 return -EADDRNOTAVAIL;
277 }
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700278
279 /* Allocate and initialize a controlling structure */
Vivek Goyaldabe7862014-08-08 14:25:45 -0700280 image = do_kimage_alloc_init();
281 if (!image)
282 return -ENOMEM;
283
284 image->start = entry;
285
Vivek Goyal255aedd2014-08-08 14:25:48 -0700286 ret = copy_user_segment_list(image, nr_segments, segments);
287 if (ret)
Vivek Goyaldabe7862014-08-08 14:25:45 -0700288 goto out_free_image;
289
Vivek Goyal255aedd2014-08-08 14:25:48 -0700290 ret = sanity_check_segment_list(image);
291 if (ret)
Vivek Goyaldabe7862014-08-08 14:25:45 -0700292 goto out_free_image;
Maneesh Soni72414d32005-06-25 14:58:28 -0700293
Vivek Goyal255aedd2014-08-08 14:25:48 -0700294 /* Enable the special crash kernel control page allocation policy. */
295 if (kexec_on_panic) {
296 image->control_page = crashk_res.start;
297 image->type = KEXEC_TYPE_CRASH;
298 }
299
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700300 /*
301 * Find a location for the control code buffer, and add it
302 * the vector of segments so that it's pages will also be
303 * counted as destination pages.
304 */
Vivek Goyal255aedd2014-08-08 14:25:48 -0700305 ret = -ENOMEM;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700306 image->control_code_page = kimage_alloc_control_pages(image,
Huang Ying163f6872008-08-15 00:40:22 -0700307 get_order(KEXEC_CONTROL_PAGE_SIZE));
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700308 if (!image->control_code_page) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -0700309 pr_err("Could not allocate control_code_buffer\n");
Vivek Goyaldabe7862014-08-08 14:25:45 -0700310 goto out_free_image;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700311 }
312
Vivek Goyal255aedd2014-08-08 14:25:48 -0700313 if (!kexec_on_panic) {
314 image->swap_page = kimage_alloc_control_pages(image, 0);
315 if (!image->swap_page) {
316 pr_err("Could not allocate swap buffer\n");
317 goto out_free_control_pages;
318 }
Huang Ying3ab83522008-07-25 19:45:07 -0700319 }
320
Zhang Yanfeib92e7e02013-02-27 17:03:29 -0800321 *rimage = image;
322 return 0;
Vivek Goyaldabe7862014-08-08 14:25:45 -0700323out_free_control_pages:
Zhang Yanfeib92e7e02013-02-27 17:03:29 -0800324 kimage_free_page_list(&image->control_pages);
Vivek Goyaldabe7862014-08-08 14:25:45 -0700325out_free_image:
Zhang Yanfeib92e7e02013-02-27 17:03:29 -0800326 kfree(image);
Vivek Goyal255aedd2014-08-08 14:25:48 -0700327 return ret;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700328}
329
Maneesh Soni72414d32005-06-25 14:58:28 -0700330static int kimage_is_destination_range(struct kimage *image,
331 unsigned long start,
332 unsigned long end)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700333{
334 unsigned long i;
335
336 for (i = 0; i < image->nr_segments; i++) {
337 unsigned long mstart, mend;
Maneesh Soni72414d32005-06-25 14:58:28 -0700338
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700339 mstart = image->segment[i].mem;
Maneesh Soni72414d32005-06-25 14:58:28 -0700340 mend = mstart + image->segment[i].memsz;
341 if ((end > mstart) && (start < mend))
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700342 return 1;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700343 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700344
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700345 return 0;
346}
347
Al Viro9796fdd2005-10-21 03:22:03 -0400348static struct page *kimage_alloc_pages(gfp_t gfp_mask, unsigned int order)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700349{
350 struct page *pages;
Maneesh Soni72414d32005-06-25 14:58:28 -0700351
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700352 pages = alloc_pages(gfp_mask, order);
353 if (pages) {
354 unsigned int count, i;
355 pages->mapping = NULL;
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700356 set_page_private(pages, order);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700357 count = 1 << order;
Maneesh Soni72414d32005-06-25 14:58:28 -0700358 for (i = 0; i < count; i++)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700359 SetPageReserved(pages + i);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700360 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700361
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700362 return pages;
363}
364
365static void kimage_free_pages(struct page *page)
366{
367 unsigned int order, count, i;
Maneesh Soni72414d32005-06-25 14:58:28 -0700368
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700369 order = page_private(page);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700370 count = 1 << order;
Maneesh Soni72414d32005-06-25 14:58:28 -0700371 for (i = 0; i < count; i++)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700372 ClearPageReserved(page + i);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700373 __free_pages(page, order);
374}
375
376static void kimage_free_page_list(struct list_head *list)
377{
378 struct list_head *pos, *next;
Maneesh Soni72414d32005-06-25 14:58:28 -0700379
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700380 list_for_each_safe(pos, next, list) {
381 struct page *page;
382
383 page = list_entry(pos, struct page, lru);
384 list_del(&page->lru);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700385 kimage_free_pages(page);
386 }
387}
388
Maneesh Soni72414d32005-06-25 14:58:28 -0700389static struct page *kimage_alloc_normal_control_pages(struct kimage *image,
390 unsigned int order)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700391{
392 /* Control pages are special, they are the intermediaries
393 * that are needed while we copy the rest of the pages
394 * to their final resting place. As such they must
395 * not conflict with either the destination addresses
396 * or memory the kernel is already using.
397 *
398 * The only case where we really need more than one of
399 * these are for architectures where we cannot disable
400 * the MMU and must instead generate an identity mapped
401 * page table for all of the memory.
402 *
403 * At worst this runs in O(N) of the image size.
404 */
405 struct list_head extra_pages;
406 struct page *pages;
407 unsigned int count;
408
409 count = 1 << order;
410 INIT_LIST_HEAD(&extra_pages);
411
412 /* Loop while I can allocate a page and the page allocated
413 * is a destination page.
414 */
415 do {
416 unsigned long pfn, epfn, addr, eaddr;
Maneesh Soni72414d32005-06-25 14:58:28 -0700417
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700418 pages = kimage_alloc_pages(GFP_KERNEL, order);
419 if (!pages)
420 break;
421 pfn = page_to_pfn(pages);
422 epfn = pfn + count;
423 addr = pfn << PAGE_SHIFT;
424 eaddr = epfn << PAGE_SHIFT;
425 if ((epfn >= (KEXEC_CONTROL_MEMORY_LIMIT >> PAGE_SHIFT)) ||
Maneesh Soni72414d32005-06-25 14:58:28 -0700426 kimage_is_destination_range(image, addr, eaddr)) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700427 list_add(&pages->lru, &extra_pages);
428 pages = NULL;
429 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700430 } while (!pages);
431
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700432 if (pages) {
433 /* Remember the allocated page... */
434 list_add(&pages->lru, &image->control_pages);
435
436 /* Because the page is already in it's destination
437 * location we will never allocate another page at
438 * that address. Therefore kimage_alloc_pages
439 * will not return it (again) and we don't need
440 * to give it an entry in image->segment[].
441 */
442 }
443 /* Deal with the destination pages I have inadvertently allocated.
444 *
445 * Ideally I would convert multi-page allocations into single
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300446 * page allocations, and add everything to image->dest_pages.
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700447 *
448 * For now it is simpler to just free the pages.
449 */
450 kimage_free_page_list(&extra_pages);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700451
Maneesh Soni72414d32005-06-25 14:58:28 -0700452 return pages;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700453}
454
Maneesh Soni72414d32005-06-25 14:58:28 -0700455static struct page *kimage_alloc_crash_control_pages(struct kimage *image,
456 unsigned int order)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700457{
458 /* Control pages are special, they are the intermediaries
459 * that are needed while we copy the rest of the pages
460 * to their final resting place. As such they must
461 * not conflict with either the destination addresses
462 * or memory the kernel is already using.
463 *
464 * Control pages are also the only pags we must allocate
465 * when loading a crash kernel. All of the other pages
466 * are specified by the segments and we just memcpy
467 * into them directly.
468 *
469 * The only case where we really need more than one of
470 * these are for architectures where we cannot disable
471 * the MMU and must instead generate an identity mapped
472 * page table for all of the memory.
473 *
474 * Given the low demand this implements a very simple
475 * allocator that finds the first hole of the appropriate
476 * size in the reserved memory region, and allocates all
477 * of the memory up to and including the hole.
478 */
479 unsigned long hole_start, hole_end, size;
480 struct page *pages;
Maneesh Soni72414d32005-06-25 14:58:28 -0700481
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700482 pages = NULL;
483 size = (1 << order) << PAGE_SHIFT;
484 hole_start = (image->control_page + (size - 1)) & ~(size - 1);
485 hole_end = hole_start + size - 1;
Maneesh Soni72414d32005-06-25 14:58:28 -0700486 while (hole_end <= crashk_res.end) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700487 unsigned long i;
Maneesh Soni72414d32005-06-25 14:58:28 -0700488
Michael Holzheu3d214fa2011-10-30 15:16:36 +0100489 if (hole_end > KEXEC_CRASH_CONTROL_MEMORY_LIMIT)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700490 break;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700491 /* See if I overlap any of the segments */
Maneesh Soni72414d32005-06-25 14:58:28 -0700492 for (i = 0; i < image->nr_segments; i++) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700493 unsigned long mstart, mend;
Maneesh Soni72414d32005-06-25 14:58:28 -0700494
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700495 mstart = image->segment[i].mem;
496 mend = mstart + image->segment[i].memsz - 1;
497 if ((hole_end >= mstart) && (hole_start <= mend)) {
498 /* Advance the hole to the end of the segment */
499 hole_start = (mend + (size - 1)) & ~(size - 1);
500 hole_end = hole_start + size - 1;
501 break;
502 }
503 }
504 /* If I don't overlap any segments I have found my hole! */
505 if (i == image->nr_segments) {
506 pages = pfn_to_page(hole_start >> PAGE_SHIFT);
507 break;
508 }
509 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700510 if (pages)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700511 image->control_page = hole_end;
Maneesh Soni72414d32005-06-25 14:58:28 -0700512
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700513 return pages;
514}
515
516
Maneesh Soni72414d32005-06-25 14:58:28 -0700517struct page *kimage_alloc_control_pages(struct kimage *image,
518 unsigned int order)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700519{
520 struct page *pages = NULL;
Maneesh Soni72414d32005-06-25 14:58:28 -0700521
522 switch (image->type) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700523 case KEXEC_TYPE_DEFAULT:
524 pages = kimage_alloc_normal_control_pages(image, order);
525 break;
526 case KEXEC_TYPE_CRASH:
527 pages = kimage_alloc_crash_control_pages(image, order);
528 break;
529 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700530
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700531 return pages;
532}
533
534static int kimage_add_entry(struct kimage *image, kimage_entry_t entry)
535{
Maneesh Soni72414d32005-06-25 14:58:28 -0700536 if (*image->entry != 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700537 image->entry++;
Maneesh Soni72414d32005-06-25 14:58:28 -0700538
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700539 if (image->entry == image->last_entry) {
540 kimage_entry_t *ind_page;
541 struct page *page;
Maneesh Soni72414d32005-06-25 14:58:28 -0700542
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700543 page = kimage_alloc_page(image, GFP_KERNEL, KIMAGE_NO_DEST);
Maneesh Soni72414d32005-06-25 14:58:28 -0700544 if (!page)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700545 return -ENOMEM;
Maneesh Soni72414d32005-06-25 14:58:28 -0700546
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700547 ind_page = page_address(page);
548 *image->entry = virt_to_phys(ind_page) | IND_INDIRECTION;
549 image->entry = ind_page;
Maneesh Soni72414d32005-06-25 14:58:28 -0700550 image->last_entry = ind_page +
551 ((PAGE_SIZE/sizeof(kimage_entry_t)) - 1);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700552 }
553 *image->entry = entry;
554 image->entry++;
555 *image->entry = 0;
Maneesh Soni72414d32005-06-25 14:58:28 -0700556
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700557 return 0;
558}
559
Maneesh Soni72414d32005-06-25 14:58:28 -0700560static int kimage_set_destination(struct kimage *image,
561 unsigned long destination)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700562{
563 int result;
564
565 destination &= PAGE_MASK;
566 result = kimage_add_entry(image, destination | IND_DESTINATION);
Maneesh Soni72414d32005-06-25 14:58:28 -0700567 if (result == 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700568 image->destination = destination;
Maneesh Soni72414d32005-06-25 14:58:28 -0700569
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700570 return result;
571}
572
573
574static int kimage_add_page(struct kimage *image, unsigned long page)
575{
576 int result;
577
578 page &= PAGE_MASK;
579 result = kimage_add_entry(image, page | IND_SOURCE);
Maneesh Soni72414d32005-06-25 14:58:28 -0700580 if (result == 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700581 image->destination += PAGE_SIZE;
Maneesh Soni72414d32005-06-25 14:58:28 -0700582
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700583 return result;
584}
585
586
587static void kimage_free_extra_pages(struct kimage *image)
588{
589 /* Walk through and free any extra destination pages I may have */
590 kimage_free_page_list(&image->dest_pages);
591
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300592 /* Walk through and free any unusable pages I have cached */
Vivek Goyal7d3e2bc2014-08-08 14:25:43 -0700593 kimage_free_page_list(&image->unusable_pages);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700594
595}
WANG Cong7fccf032008-07-25 19:45:02 -0700596static void kimage_terminate(struct kimage *image)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700597{
Maneesh Soni72414d32005-06-25 14:58:28 -0700598 if (*image->entry != 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700599 image->entry++;
Maneesh Soni72414d32005-06-25 14:58:28 -0700600
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700601 *image->entry = IND_DONE;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700602}
603
604#define for_each_kimage_entry(image, ptr, entry) \
605 for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE); \
Fabian Fredericke1bebcf2014-06-06 14:37:09 -0700606 ptr = (entry & IND_INDIRECTION) ? \
607 phys_to_virt((entry & PAGE_MASK)) : ptr + 1)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700608
609static void kimage_free_entry(kimage_entry_t entry)
610{
611 struct page *page;
612
613 page = pfn_to_page(entry >> PAGE_SHIFT);
614 kimage_free_pages(page);
615}
616
617static void kimage_free(struct kimage *image)
618{
619 kimage_entry_t *ptr, entry;
620 kimage_entry_t ind = 0;
621
622 if (!image)
623 return;
Maneesh Soni72414d32005-06-25 14:58:28 -0700624
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700625 kimage_free_extra_pages(image);
626 for_each_kimage_entry(image, ptr, entry) {
627 if (entry & IND_INDIRECTION) {
628 /* Free the previous indirection page */
Maneesh Soni72414d32005-06-25 14:58:28 -0700629 if (ind & IND_INDIRECTION)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700630 kimage_free_entry(ind);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700631 /* Save this indirection page until we are
632 * done with it.
633 */
634 ind = entry;
Fabian Fredericke1bebcf2014-06-06 14:37:09 -0700635 } else if (entry & IND_SOURCE)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700636 kimage_free_entry(entry);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700637 }
638 /* Free the final indirection page */
Maneesh Soni72414d32005-06-25 14:58:28 -0700639 if (ind & IND_INDIRECTION)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700640 kimage_free_entry(ind);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700641
642 /* Handle any machine specific cleanup */
643 machine_kexec_cleanup(image);
644
645 /* Free the kexec control pages... */
646 kimage_free_page_list(&image->control_pages);
647 kfree(image);
648}
649
Maneesh Soni72414d32005-06-25 14:58:28 -0700650static kimage_entry_t *kimage_dst_used(struct kimage *image,
651 unsigned long page)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700652{
653 kimage_entry_t *ptr, entry;
654 unsigned long destination = 0;
655
656 for_each_kimage_entry(image, ptr, entry) {
Maneesh Soni72414d32005-06-25 14:58:28 -0700657 if (entry & IND_DESTINATION)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700658 destination = entry & PAGE_MASK;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700659 else if (entry & IND_SOURCE) {
Maneesh Soni72414d32005-06-25 14:58:28 -0700660 if (page == destination)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700661 return ptr;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700662 destination += PAGE_SIZE;
663 }
664 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700665
Alexey Dobriyan314b6a42005-06-27 22:29:33 -0700666 return NULL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700667}
668
Maneesh Soni72414d32005-06-25 14:58:28 -0700669static struct page *kimage_alloc_page(struct kimage *image,
Al Viro9796fdd2005-10-21 03:22:03 -0400670 gfp_t gfp_mask,
Maneesh Soni72414d32005-06-25 14:58:28 -0700671 unsigned long destination)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700672{
673 /*
674 * Here we implement safeguards to ensure that a source page
675 * is not copied to its destination page before the data on
676 * the destination page is no longer useful.
677 *
678 * To do this we maintain the invariant that a source page is
679 * either its own destination page, or it is not a
680 * destination page at all.
681 *
682 * That is slightly stronger than required, but the proof
683 * that no problems will not occur is trivial, and the
684 * implementation is simply to verify.
685 *
686 * When allocating all pages normally this algorithm will run
687 * in O(N) time, but in the worst case it will run in O(N^2)
688 * time. If the runtime is a problem the data structures can
689 * be fixed.
690 */
691 struct page *page;
692 unsigned long addr;
693
694 /*
695 * Walk through the list of destination pages, and see if I
696 * have a match.
697 */
698 list_for_each_entry(page, &image->dest_pages, lru) {
699 addr = page_to_pfn(page) << PAGE_SHIFT;
700 if (addr == destination) {
701 list_del(&page->lru);
702 return page;
703 }
704 }
705 page = NULL;
706 while (1) {
707 kimage_entry_t *old;
708
709 /* Allocate a page, if we run out of memory give up */
710 page = kimage_alloc_pages(gfp_mask, 0);
Maneesh Soni72414d32005-06-25 14:58:28 -0700711 if (!page)
Alexey Dobriyan314b6a42005-06-27 22:29:33 -0700712 return NULL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700713 /* If the page cannot be used file it away */
Maneesh Soni72414d32005-06-25 14:58:28 -0700714 if (page_to_pfn(page) >
715 (KEXEC_SOURCE_MEMORY_LIMIT >> PAGE_SHIFT)) {
Vivek Goyal7d3e2bc2014-08-08 14:25:43 -0700716 list_add(&page->lru, &image->unusable_pages);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700717 continue;
718 }
719 addr = page_to_pfn(page) << PAGE_SHIFT;
720
721 /* If it is the destination page we want use it */
722 if (addr == destination)
723 break;
724
725 /* If the page is not a destination page use it */
Maneesh Soni72414d32005-06-25 14:58:28 -0700726 if (!kimage_is_destination_range(image, addr,
727 addr + PAGE_SIZE))
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700728 break;
729
730 /*
731 * I know that the page is someones destination page.
732 * See if there is already a source page for this
733 * destination page. And if so swap the source pages.
734 */
735 old = kimage_dst_used(image, addr);
736 if (old) {
737 /* If so move it */
738 unsigned long old_addr;
739 struct page *old_page;
740
741 old_addr = *old & PAGE_MASK;
742 old_page = pfn_to_page(old_addr >> PAGE_SHIFT);
743 copy_highpage(page, old_page);
744 *old = addr | (*old & ~PAGE_MASK);
745
746 /* The old page I have found cannot be a
Jonathan Steelf9092f32008-09-22 13:57:45 -0700747 * destination page, so return it if it's
748 * gfp_flags honor the ones passed in.
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700749 */
Jonathan Steelf9092f32008-09-22 13:57:45 -0700750 if (!(gfp_mask & __GFP_HIGHMEM) &&
751 PageHighMem(old_page)) {
752 kimage_free_pages(old_page);
753 continue;
754 }
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700755 addr = old_addr;
756 page = old_page;
757 break;
Fabian Fredericke1bebcf2014-06-06 14:37:09 -0700758 } else {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700759 /* Place the page on the destination list I
760 * will use it later.
761 */
762 list_add(&page->lru, &image->dest_pages);
763 }
764 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700765
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700766 return page;
767}
768
769static int kimage_load_normal_segment(struct kimage *image,
Maneesh Soni72414d32005-06-25 14:58:28 -0700770 struct kexec_segment *segment)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700771{
772 unsigned long maddr;
Zhang Yanfei310faaa2013-04-30 15:28:21 -0700773 size_t ubytes, mbytes;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700774 int result;
Alexey Dobriyan314b6a42005-06-27 22:29:33 -0700775 unsigned char __user *buf;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700776
777 result = 0;
778 buf = segment->buf;
779 ubytes = segment->bufsz;
780 mbytes = segment->memsz;
781 maddr = segment->mem;
782
783 result = kimage_set_destination(image, maddr);
Maneesh Soni72414d32005-06-25 14:58:28 -0700784 if (result < 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700785 goto out;
Maneesh Soni72414d32005-06-25 14:58:28 -0700786
787 while (mbytes) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700788 struct page *page;
789 char *ptr;
790 size_t uchunk, mchunk;
Maneesh Soni72414d32005-06-25 14:58:28 -0700791
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700792 page = kimage_alloc_page(image, GFP_HIGHUSER, maddr);
Stephen Hemmingerc80544d2007-10-18 03:07:05 -0700793 if (!page) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700794 result = -ENOMEM;
795 goto out;
796 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700797 result = kimage_add_page(image, page_to_pfn(page)
798 << PAGE_SHIFT);
799 if (result < 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700800 goto out;
Maneesh Soni72414d32005-06-25 14:58:28 -0700801
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700802 ptr = kmap(page);
803 /* Start with a clear page */
Jan Beulich3ecb01d2010-10-26 14:22:27 -0700804 clear_page(ptr);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700805 ptr += maddr & ~PAGE_MASK;
Zhang Yanfei31c3a3f2013-04-30 15:28:23 -0700806 mchunk = min_t(size_t, mbytes,
807 PAGE_SIZE - (maddr & ~PAGE_MASK));
808 uchunk = min(ubytes, mchunk);
Maneesh Soni72414d32005-06-25 14:58:28 -0700809
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700810 result = copy_from_user(ptr, buf, uchunk);
811 kunmap(page);
812 if (result) {
Dan Carpenterf65a03f2010-08-10 18:03:31 -0700813 result = -EFAULT;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700814 goto out;
815 }
816 ubytes -= uchunk;
817 maddr += mchunk;
818 buf += mchunk;
819 mbytes -= mchunk;
820 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700821out:
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700822 return result;
823}
824
825static int kimage_load_crash_segment(struct kimage *image,
Maneesh Soni72414d32005-06-25 14:58:28 -0700826 struct kexec_segment *segment)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700827{
828 /* For crash dumps kernels we simply copy the data from
829 * user space to it's destination.
830 * We do things a page at a time for the sake of kmap.
831 */
832 unsigned long maddr;
Zhang Yanfei310faaa2013-04-30 15:28:21 -0700833 size_t ubytes, mbytes;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700834 int result;
Alexey Dobriyan314b6a42005-06-27 22:29:33 -0700835 unsigned char __user *buf;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700836
837 result = 0;
838 buf = segment->buf;
839 ubytes = segment->bufsz;
840 mbytes = segment->memsz;
841 maddr = segment->mem;
Maneesh Soni72414d32005-06-25 14:58:28 -0700842 while (mbytes) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700843 struct page *page;
844 char *ptr;
845 size_t uchunk, mchunk;
Maneesh Soni72414d32005-06-25 14:58:28 -0700846
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700847 page = pfn_to_page(maddr >> PAGE_SHIFT);
Stephen Hemmingerc80544d2007-10-18 03:07:05 -0700848 if (!page) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700849 result = -ENOMEM;
850 goto out;
851 }
852 ptr = kmap(page);
853 ptr += maddr & ~PAGE_MASK;
Zhang Yanfei31c3a3f2013-04-30 15:28:23 -0700854 mchunk = min_t(size_t, mbytes,
855 PAGE_SIZE - (maddr & ~PAGE_MASK));
856 uchunk = min(ubytes, mchunk);
857 if (mchunk > uchunk) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700858 /* Zero the trailing part of the page */
859 memset(ptr + uchunk, 0, mchunk - uchunk);
860 }
861 result = copy_from_user(ptr, buf, uchunk);
Zou Nan haia79561132006-12-07 09:51:35 -0800862 kexec_flush_icache_page(page);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700863 kunmap(page);
864 if (result) {
Dan Carpenterf65a03f2010-08-10 18:03:31 -0700865 result = -EFAULT;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700866 goto out;
867 }
868 ubytes -= uchunk;
869 maddr += mchunk;
870 buf += mchunk;
871 mbytes -= mchunk;
872 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700873out:
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700874 return result;
875}
876
877static int kimage_load_segment(struct kimage *image,
Maneesh Soni72414d32005-06-25 14:58:28 -0700878 struct kexec_segment *segment)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700879{
880 int result = -ENOMEM;
Maneesh Soni72414d32005-06-25 14:58:28 -0700881
882 switch (image->type) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700883 case KEXEC_TYPE_DEFAULT:
884 result = kimage_load_normal_segment(image, segment);
885 break;
886 case KEXEC_TYPE_CRASH:
887 result = kimage_load_crash_segment(image, segment);
888 break;
889 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700890
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700891 return result;
892}
893
894/*
895 * Exec Kernel system call: for obvious reasons only root may call it.
896 *
897 * This call breaks up into three pieces.
898 * - A generic part which loads the new kernel from the current
899 * address space, and very carefully places the data in the
900 * allocated pages.
901 *
902 * - A generic part that interacts with the kernel and tells all of
903 * the devices to shut down. Preventing on-going dmas, and placing
904 * the devices in a consistent state so a later kernel can
905 * reinitialize them.
906 *
907 * - A machine specific part that includes the syscall number
Geert Uytterhoeven002ace72013-09-15 11:35:37 +0200908 * and then copies the image to it's final destination. And
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700909 * jumps into the image at entry.
910 *
911 * kexec does not sync, or unmount filesystems so if you need
912 * that to happen you need to do that yourself.
913 */
Jeff Moyerc330dda2006-06-23 02:05:07 -0700914struct kimage *kexec_image;
915struct kimage *kexec_crash_image;
Kees Cook79847542014-01-23 15:55:59 -0800916int kexec_load_disabled;
Andrew Morton8c5a1cf2008-08-15 00:40:27 -0700917
918static DEFINE_MUTEX(kexec_mutex);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700919
Heiko Carstens754fe8d2009-01-14 14:14:09 +0100920SYSCALL_DEFINE4(kexec_load, unsigned long, entry, unsigned long, nr_segments,
921 struct kexec_segment __user *, segments, unsigned long, flags)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700922{
923 struct kimage **dest_image, *image;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700924 int result;
925
926 /* We only trust the superuser with rebooting the system. */
Kees Cook79847542014-01-23 15:55:59 -0800927 if (!capable(CAP_SYS_BOOT) || kexec_load_disabled)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700928 return -EPERM;
929
930 /*
931 * Verify we have a legal set of flags
932 * This leaves us room for future extensions.
933 */
934 if ((flags & KEXEC_FLAGS) != (flags & ~KEXEC_ARCH_MASK))
935 return -EINVAL;
936
937 /* Verify we are on the appropriate architecture */
938 if (((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH) &&
939 ((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH_DEFAULT))
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700940 return -EINVAL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700941
942 /* Put an artificial cap on the number
943 * of segments passed to kexec_load.
944 */
945 if (nr_segments > KEXEC_SEGMENT_MAX)
946 return -EINVAL;
947
948 image = NULL;
949 result = 0;
950
951 /* Because we write directly to the reserved memory
952 * region when loading crash kernels we need a mutex here to
953 * prevent multiple crash kernels from attempting to load
954 * simultaneously, and to prevent a crash kernel from loading
955 * over the top of a in use crash kernel.
956 *
957 * KISS: always take the mutex.
958 */
Andrew Morton8c5a1cf2008-08-15 00:40:27 -0700959 if (!mutex_trylock(&kexec_mutex))
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700960 return -EBUSY;
Maneesh Soni72414d32005-06-25 14:58:28 -0700961
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700962 dest_image = &kexec_image;
Maneesh Soni72414d32005-06-25 14:58:28 -0700963 if (flags & KEXEC_ON_CRASH)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700964 dest_image = &kexec_crash_image;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700965 if (nr_segments > 0) {
966 unsigned long i;
Maneesh Soni72414d32005-06-25 14:58:28 -0700967
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700968 /* Loading another kernel to reboot into */
Maneesh Soni72414d32005-06-25 14:58:28 -0700969 if ((flags & KEXEC_ON_CRASH) == 0)
Vivek Goyal255aedd2014-08-08 14:25:48 -0700970 result = kimage_alloc_init(&image, entry, nr_segments,
971 segments, flags);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700972 /* Loading another kernel to switch to if this one crashes */
973 else if (flags & KEXEC_ON_CRASH) {
974 /* Free any current crash dump kernel before
975 * we corrupt it.
976 */
977 kimage_free(xchg(&kexec_crash_image, NULL));
Vivek Goyal255aedd2014-08-08 14:25:48 -0700978 result = kimage_alloc_init(&image, entry, nr_segments,
979 segments, flags);
Michael Holzheu558df722011-10-30 15:16:43 +0100980 crash_map_reserved_pages();
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700981 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700982 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700983 goto out;
Maneesh Soni72414d32005-06-25 14:58:28 -0700984
Huang Ying3ab83522008-07-25 19:45:07 -0700985 if (flags & KEXEC_PRESERVE_CONTEXT)
986 image->preserve_context = 1;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700987 result = machine_kexec_prepare(image);
Maneesh Soni72414d32005-06-25 14:58:28 -0700988 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700989 goto out;
Maneesh Soni72414d32005-06-25 14:58:28 -0700990
991 for (i = 0; i < nr_segments; i++) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700992 result = kimage_load_segment(image, &image->segment[i]);
Maneesh Soni72414d32005-06-25 14:58:28 -0700993 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700994 goto out;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700995 }
WANG Cong7fccf032008-07-25 19:45:02 -0700996 kimage_terminate(image);
Michael Holzheu558df722011-10-30 15:16:43 +0100997 if (flags & KEXEC_ON_CRASH)
998 crash_unmap_reserved_pages();
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700999 }
1000 /* Install the new kernel, and Uninstall the old */
1001 image = xchg(dest_image, image);
1002
Maneesh Soni72414d32005-06-25 14:58:28 -07001003out:
Andrew Morton8c5a1cf2008-08-15 00:40:27 -07001004 mutex_unlock(&kexec_mutex);
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001005 kimage_free(image);
Maneesh Soni72414d32005-06-25 14:58:28 -07001006
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001007 return result;
1008}
1009
Michael Holzheu558df722011-10-30 15:16:43 +01001010/*
1011 * Add and remove page tables for crashkernel memory
1012 *
1013 * Provide an empty default implementation here -- architecture
1014 * code may override this
1015 */
1016void __weak crash_map_reserved_pages(void)
1017{}
1018
1019void __weak crash_unmap_reserved_pages(void)
1020{}
1021
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001022#ifdef CONFIG_COMPAT
Heiko Carstensca2c4052014-03-04 17:13:42 +01001023COMPAT_SYSCALL_DEFINE4(kexec_load, compat_ulong_t, entry,
1024 compat_ulong_t, nr_segments,
1025 struct compat_kexec_segment __user *, segments,
1026 compat_ulong_t, flags)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001027{
1028 struct compat_kexec_segment in;
1029 struct kexec_segment out, __user *ksegments;
1030 unsigned long i, result;
1031
1032 /* Don't allow clients that don't understand the native
1033 * architecture to do anything.
1034 */
Maneesh Soni72414d32005-06-25 14:58:28 -07001035 if ((flags & KEXEC_ARCH_MASK) == KEXEC_ARCH_DEFAULT)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001036 return -EINVAL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001037
Maneesh Soni72414d32005-06-25 14:58:28 -07001038 if (nr_segments > KEXEC_SEGMENT_MAX)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001039 return -EINVAL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001040
1041 ksegments = compat_alloc_user_space(nr_segments * sizeof(out));
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001042 for (i = 0; i < nr_segments; i++) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001043 result = copy_from_user(&in, &segments[i], sizeof(in));
Maneesh Soni72414d32005-06-25 14:58:28 -07001044 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001045 return -EFAULT;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001046
1047 out.buf = compat_ptr(in.buf);
1048 out.bufsz = in.bufsz;
1049 out.mem = in.mem;
1050 out.memsz = in.memsz;
1051
1052 result = copy_to_user(&ksegments[i], &out, sizeof(out));
Maneesh Soni72414d32005-06-25 14:58:28 -07001053 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001054 return -EFAULT;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001055 }
1056
1057 return sys_kexec_load(entry, nr_segments, ksegments, flags);
1058}
1059#endif
1060
Alexander Nyberg6e274d12005-06-25 14:58:26 -07001061void crash_kexec(struct pt_regs *regs)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001062{
Andrew Morton8c5a1cf2008-08-15 00:40:27 -07001063 /* Take the kexec_mutex here to prevent sys_kexec_load
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001064 * running on one cpu from replacing the crash kernel
1065 * we are using after a panic on a different cpu.
1066 *
1067 * If the crash kernel was not located in a fixed area
1068 * of memory the xchg(&kexec_crash_image) would be
1069 * sufficient. But since I reuse the memory...
1070 */
Andrew Morton8c5a1cf2008-08-15 00:40:27 -07001071 if (mutex_trylock(&kexec_mutex)) {
David Wilderc0ce7d02006-06-23 15:29:34 -07001072 if (kexec_crash_image) {
Vivek Goyale996e582006-01-09 20:51:44 -08001073 struct pt_regs fixed_regs;
KOSAKI Motohiro0f4bd462009-12-22 03:15:43 +00001074
Vivek Goyale996e582006-01-09 20:51:44 -08001075 crash_setup_regs(&fixed_regs, regs);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001076 crash_save_vmcoreinfo();
Vivek Goyale996e582006-01-09 20:51:44 -08001077 machine_crash_shutdown(&fixed_regs);
David Wilderc0ce7d02006-06-23 15:29:34 -07001078 machine_kexec(kexec_crash_image);
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001079 }
Andrew Morton8c5a1cf2008-08-15 00:40:27 -07001080 mutex_unlock(&kexec_mutex);
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001081 }
1082}
Vivek Goyalcc571652006-01-09 20:51:41 -08001083
Amerigo Wang06a7f712009-12-15 16:47:46 -08001084size_t crash_get_memory_size(void)
1085{
Pavan Naregundie05bd332010-06-29 15:05:28 -07001086 size_t size = 0;
Amerigo Wang06a7f712009-12-15 16:47:46 -08001087 mutex_lock(&kexec_mutex);
Pavan Naregundie05bd332010-06-29 15:05:28 -07001088 if (crashk_res.end != crashk_res.start)
Joe Perches28f65c112011-06-09 09:13:32 -07001089 size = resource_size(&crashk_res);
Amerigo Wang06a7f712009-12-15 16:47:46 -08001090 mutex_unlock(&kexec_mutex);
1091 return size;
1092}
1093
Anton Blanchardc0bb9e42010-08-25 10:22:58 +10001094void __weak crash_free_reserved_phys_range(unsigned long begin,
1095 unsigned long end)
Amerigo Wang06a7f712009-12-15 16:47:46 -08001096{
1097 unsigned long addr;
1098
Jiang Liue07cee22013-04-29 15:06:58 -07001099 for (addr = begin; addr < end; addr += PAGE_SIZE)
1100 free_reserved_page(pfn_to_page(addr >> PAGE_SHIFT));
Amerigo Wang06a7f712009-12-15 16:47:46 -08001101}
1102
1103int crash_shrink_memory(unsigned long new_size)
1104{
1105 int ret = 0;
1106 unsigned long start, end;
Michael Holzheubec013c2012-01-12 17:20:15 -08001107 unsigned long old_size;
Michael Holzheu6480e5a2012-01-12 17:20:14 -08001108 struct resource *ram_res;
Amerigo Wang06a7f712009-12-15 16:47:46 -08001109
1110 mutex_lock(&kexec_mutex);
1111
1112 if (kexec_crash_image) {
1113 ret = -ENOENT;
1114 goto unlock;
1115 }
1116 start = crashk_res.start;
1117 end = crashk_res.end;
Michael Holzheubec013c2012-01-12 17:20:15 -08001118 old_size = (end == 0) ? 0 : end - start + 1;
1119 if (new_size >= old_size) {
1120 ret = (new_size == old_size) ? 0 : -EINVAL;
Amerigo Wang06a7f712009-12-15 16:47:46 -08001121 goto unlock;
1122 }
1123
Michael Holzheu6480e5a2012-01-12 17:20:14 -08001124 ram_res = kzalloc(sizeof(*ram_res), GFP_KERNEL);
1125 if (!ram_res) {
1126 ret = -ENOMEM;
1127 goto unlock;
1128 }
1129
Michael Holzheu558df722011-10-30 15:16:43 +01001130 start = roundup(start, KEXEC_CRASH_MEM_ALIGN);
1131 end = roundup(start + new_size, KEXEC_CRASH_MEM_ALIGN);
Amerigo Wang06a7f712009-12-15 16:47:46 -08001132
Michael Holzheu558df722011-10-30 15:16:43 +01001133 crash_map_reserved_pages();
Anton Blanchardc0bb9e42010-08-25 10:22:58 +10001134 crash_free_reserved_phys_range(end, crashk_res.end);
Amerigo Wang06a7f712009-12-15 16:47:46 -08001135
Pavan Naregundie05bd332010-06-29 15:05:28 -07001136 if ((start == end) && (crashk_res.parent != NULL))
Amerigo Wang06a7f712009-12-15 16:47:46 -08001137 release_resource(&crashk_res);
Michael Holzheu6480e5a2012-01-12 17:20:14 -08001138
1139 ram_res->start = end;
1140 ram_res->end = crashk_res.end;
1141 ram_res->flags = IORESOURCE_BUSY | IORESOURCE_MEM;
1142 ram_res->name = "System RAM";
1143
Vitaly Mayatskikh475f9aa2010-05-11 14:06:51 -07001144 crashk_res.end = end - 1;
Michael Holzheu6480e5a2012-01-12 17:20:14 -08001145
1146 insert_resource(&iomem_resource, ram_res);
Michael Holzheu558df722011-10-30 15:16:43 +01001147 crash_unmap_reserved_pages();
Amerigo Wang06a7f712009-12-15 16:47:46 -08001148
1149unlock:
1150 mutex_unlock(&kexec_mutex);
1151 return ret;
1152}
1153
Magnus Damm85916f82006-12-06 20:40:41 -08001154static u32 *append_elf_note(u32 *buf, char *name, unsigned type, void *data,
1155 size_t data_len)
1156{
1157 struct elf_note note;
1158
1159 note.n_namesz = strlen(name) + 1;
1160 note.n_descsz = data_len;
1161 note.n_type = type;
1162 memcpy(buf, &note, sizeof(note));
1163 buf += (sizeof(note) + 3)/4;
1164 memcpy(buf, name, note.n_namesz);
1165 buf += (note.n_namesz + 3)/4;
1166 memcpy(buf, data, note.n_descsz);
1167 buf += (note.n_descsz + 3)/4;
1168
1169 return buf;
1170}
1171
1172static void final_note(u32 *buf)
1173{
1174 struct elf_note note;
1175
1176 note.n_namesz = 0;
1177 note.n_descsz = 0;
1178 note.n_type = 0;
1179 memcpy(buf, &note, sizeof(note));
1180}
1181
1182void crash_save_cpu(struct pt_regs *regs, int cpu)
1183{
1184 struct elf_prstatus prstatus;
1185 u32 *buf;
1186
Rusty Russell4f4b6c12009-01-01 10:12:15 +10301187 if ((cpu < 0) || (cpu >= nr_cpu_ids))
Magnus Damm85916f82006-12-06 20:40:41 -08001188 return;
1189
1190 /* Using ELF notes here is opportunistic.
1191 * I need a well defined structure format
1192 * for the data I pass, and I need tags
1193 * on the data to indicate what information I have
1194 * squirrelled away. ELF notes happen to provide
1195 * all of that, so there is no need to invent something new.
1196 */
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001197 buf = (u32 *)per_cpu_ptr(crash_notes, cpu);
Magnus Damm85916f82006-12-06 20:40:41 -08001198 if (!buf)
1199 return;
1200 memset(&prstatus, 0, sizeof(prstatus));
1201 prstatus.pr_pid = current->pid;
Tejun Heo6cd61c02009-02-09 22:17:39 +09001202 elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
Simon Horman6672f762007-05-08 00:28:22 -07001203 buf = append_elf_note(buf, KEXEC_CORE_NOTE_NAME, NT_PRSTATUS,
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001204 &prstatus, sizeof(prstatus));
Magnus Damm85916f82006-12-06 20:40:41 -08001205 final_note(buf);
1206}
1207
Vivek Goyalcc571652006-01-09 20:51:41 -08001208static int __init crash_notes_memory_init(void)
1209{
1210 /* Allocate memory for saving cpu registers. */
1211 crash_notes = alloc_percpu(note_buf_t);
1212 if (!crash_notes) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001213 pr_warn("Kexec: Memory allocation for saving cpu register states failed\n");
Vivek Goyalcc571652006-01-09 20:51:41 -08001214 return -ENOMEM;
1215 }
1216 return 0;
1217}
Paul Gortmakerc96d6662014-04-03 14:48:35 -07001218subsys_initcall(crash_notes_memory_init);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001219
Bernhard Wallecba63c32007-10-18 23:40:58 -07001220
1221/*
1222 * parsing the "crashkernel" commandline
1223 *
1224 * this code is intended to be called from architecture specific code
1225 */
1226
1227
1228/*
1229 * This function parses command lines in the format
1230 *
1231 * crashkernel=ramsize-range:size[,...][@offset]
1232 *
1233 * The function returns 0 on success and -EINVAL on failure.
1234 */
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001235static int __init parse_crashkernel_mem(char *cmdline,
1236 unsigned long long system_ram,
1237 unsigned long long *crash_size,
1238 unsigned long long *crash_base)
Bernhard Wallecba63c32007-10-18 23:40:58 -07001239{
1240 char *cur = cmdline, *tmp;
1241
1242 /* for each entry of the comma-separated list */
1243 do {
1244 unsigned long long start, end = ULLONG_MAX, size;
1245
1246 /* get the start of the range */
1247 start = memparse(cur, &tmp);
1248 if (cur == tmp) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001249 pr_warn("crashkernel: Memory value expected\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001250 return -EINVAL;
1251 }
1252 cur = tmp;
1253 if (*cur != '-') {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001254 pr_warn("crashkernel: '-' expected\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001255 return -EINVAL;
1256 }
1257 cur++;
1258
1259 /* if no ':' is here, than we read the end */
1260 if (*cur != ':') {
1261 end = memparse(cur, &tmp);
1262 if (cur == tmp) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001263 pr_warn("crashkernel: Memory value expected\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001264 return -EINVAL;
1265 }
1266 cur = tmp;
1267 if (end <= start) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001268 pr_warn("crashkernel: end <= start\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001269 return -EINVAL;
1270 }
1271 }
1272
1273 if (*cur != ':') {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001274 pr_warn("crashkernel: ':' expected\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001275 return -EINVAL;
1276 }
1277 cur++;
1278
1279 size = memparse(cur, &tmp);
1280 if (cur == tmp) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001281 pr_warn("Memory value expected\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001282 return -EINVAL;
1283 }
1284 cur = tmp;
1285 if (size >= system_ram) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001286 pr_warn("crashkernel: invalid size\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001287 return -EINVAL;
1288 }
1289
1290 /* match ? */
Michael Ellermanbe089d792008-05-01 04:34:49 -07001291 if (system_ram >= start && system_ram < end) {
Bernhard Wallecba63c32007-10-18 23:40:58 -07001292 *crash_size = size;
1293 break;
1294 }
1295 } while (*cur++ == ',');
1296
1297 if (*crash_size > 0) {
Hidetoshi Seto11c7da42009-07-29 15:02:08 -07001298 while (*cur && *cur != ' ' && *cur != '@')
Bernhard Wallecba63c32007-10-18 23:40:58 -07001299 cur++;
1300 if (*cur == '@') {
1301 cur++;
1302 *crash_base = memparse(cur, &tmp);
1303 if (cur == tmp) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001304 pr_warn("Memory value expected after '@'\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001305 return -EINVAL;
1306 }
1307 }
1308 }
1309
1310 return 0;
1311}
1312
1313/*
1314 * That function parses "simple" (old) crashkernel command lines like
1315 *
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001316 * crashkernel=size[@offset]
Bernhard Wallecba63c32007-10-18 23:40:58 -07001317 *
1318 * It returns 0 on success and -EINVAL on failure.
1319 */
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001320static int __init parse_crashkernel_simple(char *cmdline,
1321 unsigned long long *crash_size,
1322 unsigned long long *crash_base)
Bernhard Wallecba63c32007-10-18 23:40:58 -07001323{
1324 char *cur = cmdline;
1325
1326 *crash_size = memparse(cmdline, &cur);
1327 if (cmdline == cur) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001328 pr_warn("crashkernel: memory value expected\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001329 return -EINVAL;
1330 }
1331
1332 if (*cur == '@')
1333 *crash_base = memparse(cur+1, &cur);
Zhenzhong Duaneaa3be62012-03-28 14:42:47 -07001334 else if (*cur != ' ' && *cur != '\0') {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001335 pr_warn("crashkernel: unrecognized char\n");
Zhenzhong Duaneaa3be62012-03-28 14:42:47 -07001336 return -EINVAL;
1337 }
Bernhard Wallecba63c32007-10-18 23:40:58 -07001338
1339 return 0;
1340}
1341
Yinghai Luadbc7422013-04-15 22:23:48 -07001342#define SUFFIX_HIGH 0
1343#define SUFFIX_LOW 1
1344#define SUFFIX_NULL 2
1345static __initdata char *suffix_tbl[] = {
1346 [SUFFIX_HIGH] = ",high",
1347 [SUFFIX_LOW] = ",low",
1348 [SUFFIX_NULL] = NULL,
1349};
1350
Bernhard Wallecba63c32007-10-18 23:40:58 -07001351/*
Yinghai Luadbc7422013-04-15 22:23:48 -07001352 * That function parses "suffix" crashkernel command lines like
1353 *
1354 * crashkernel=size,[high|low]
1355 *
1356 * It returns 0 on success and -EINVAL on failure.
Bernhard Wallecba63c32007-10-18 23:40:58 -07001357 */
Yinghai Luadbc7422013-04-15 22:23:48 -07001358static int __init parse_crashkernel_suffix(char *cmdline,
1359 unsigned long long *crash_size,
1360 unsigned long long *crash_base,
1361 const char *suffix)
1362{
1363 char *cur = cmdline;
1364
1365 *crash_size = memparse(cmdline, &cur);
1366 if (cmdline == cur) {
1367 pr_warn("crashkernel: memory value expected\n");
1368 return -EINVAL;
1369 }
1370
1371 /* check with suffix */
1372 if (strncmp(cur, suffix, strlen(suffix))) {
1373 pr_warn("crashkernel: unrecognized char\n");
1374 return -EINVAL;
1375 }
1376 cur += strlen(suffix);
1377 if (*cur != ' ' && *cur != '\0') {
1378 pr_warn("crashkernel: unrecognized char\n");
1379 return -EINVAL;
1380 }
1381
1382 return 0;
1383}
1384
1385static __init char *get_last_crashkernel(char *cmdline,
1386 const char *name,
1387 const char *suffix)
1388{
1389 char *p = cmdline, *ck_cmdline = NULL;
1390
1391 /* find crashkernel and use the last one if there are more */
1392 p = strstr(p, name);
1393 while (p) {
1394 char *end_p = strchr(p, ' ');
1395 char *q;
1396
1397 if (!end_p)
1398 end_p = p + strlen(p);
1399
1400 if (!suffix) {
1401 int i;
1402
1403 /* skip the one with any known suffix */
1404 for (i = 0; suffix_tbl[i]; i++) {
1405 q = end_p - strlen(suffix_tbl[i]);
1406 if (!strncmp(q, suffix_tbl[i],
1407 strlen(suffix_tbl[i])))
1408 goto next;
1409 }
1410 ck_cmdline = p;
1411 } else {
1412 q = end_p - strlen(suffix);
1413 if (!strncmp(q, suffix, strlen(suffix)))
1414 ck_cmdline = p;
1415 }
1416next:
1417 p = strstr(p+1, name);
1418 }
1419
1420 if (!ck_cmdline)
1421 return NULL;
1422
1423 return ck_cmdline;
1424}
1425
Yinghai Lu0212f912013-01-24 12:20:11 -08001426static int __init __parse_crashkernel(char *cmdline,
Bernhard Wallecba63c32007-10-18 23:40:58 -07001427 unsigned long long system_ram,
1428 unsigned long long *crash_size,
Yinghai Lu0212f912013-01-24 12:20:11 -08001429 unsigned long long *crash_base,
Yinghai Luadbc7422013-04-15 22:23:48 -07001430 const char *name,
1431 const char *suffix)
Bernhard Wallecba63c32007-10-18 23:40:58 -07001432{
Bernhard Wallecba63c32007-10-18 23:40:58 -07001433 char *first_colon, *first_space;
Yinghai Luadbc7422013-04-15 22:23:48 -07001434 char *ck_cmdline;
Bernhard Wallecba63c32007-10-18 23:40:58 -07001435
1436 BUG_ON(!crash_size || !crash_base);
1437 *crash_size = 0;
1438 *crash_base = 0;
1439
Yinghai Luadbc7422013-04-15 22:23:48 -07001440 ck_cmdline = get_last_crashkernel(cmdline, name, suffix);
Bernhard Wallecba63c32007-10-18 23:40:58 -07001441
1442 if (!ck_cmdline)
1443 return -EINVAL;
1444
Yinghai Lu0212f912013-01-24 12:20:11 -08001445 ck_cmdline += strlen(name);
Bernhard Wallecba63c32007-10-18 23:40:58 -07001446
Yinghai Luadbc7422013-04-15 22:23:48 -07001447 if (suffix)
1448 return parse_crashkernel_suffix(ck_cmdline, crash_size,
1449 crash_base, suffix);
Bernhard Wallecba63c32007-10-18 23:40:58 -07001450 /*
1451 * if the commandline contains a ':', then that's the extended
1452 * syntax -- if not, it must be the classic syntax
1453 */
1454 first_colon = strchr(ck_cmdline, ':');
1455 first_space = strchr(ck_cmdline, ' ');
1456 if (first_colon && (!first_space || first_colon < first_space))
1457 return parse_crashkernel_mem(ck_cmdline, system_ram,
1458 crash_size, crash_base);
Bernhard Wallecba63c32007-10-18 23:40:58 -07001459
Xishi Qiu80c74f62013-09-11 14:24:47 -07001460 return parse_crashkernel_simple(ck_cmdline, crash_size, crash_base);
Bernhard Wallecba63c32007-10-18 23:40:58 -07001461}
1462
Yinghai Luadbc7422013-04-15 22:23:48 -07001463/*
1464 * That function is the entry point for command line parsing and should be
1465 * called from the arch-specific code.
1466 */
Yinghai Lu0212f912013-01-24 12:20:11 -08001467int __init parse_crashkernel(char *cmdline,
1468 unsigned long long system_ram,
1469 unsigned long long *crash_size,
1470 unsigned long long *crash_base)
1471{
1472 return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
Yinghai Luadbc7422013-04-15 22:23:48 -07001473 "crashkernel=", NULL);
Yinghai Lu0212f912013-01-24 12:20:11 -08001474}
1475
Yinghai Lu55a20ee2013-04-15 22:23:47 -07001476int __init parse_crashkernel_high(char *cmdline,
1477 unsigned long long system_ram,
1478 unsigned long long *crash_size,
1479 unsigned long long *crash_base)
1480{
1481 return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
Yinghai Luadbc7422013-04-15 22:23:48 -07001482 "crashkernel=", suffix_tbl[SUFFIX_HIGH]);
Yinghai Lu55a20ee2013-04-15 22:23:47 -07001483}
1484
Yinghai Lu0212f912013-01-24 12:20:11 -08001485int __init parse_crashkernel_low(char *cmdline,
1486 unsigned long long system_ram,
1487 unsigned long long *crash_size,
1488 unsigned long long *crash_base)
1489{
1490 return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
Yinghai Luadbc7422013-04-15 22:23:48 -07001491 "crashkernel=", suffix_tbl[SUFFIX_LOW]);
Yinghai Lu0212f912013-01-24 12:20:11 -08001492}
Bernhard Wallecba63c32007-10-18 23:40:58 -07001493
Michael Holzheufa8ff292011-10-30 15:16:41 +01001494static void update_vmcoreinfo_note(void)
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001495{
Michael Holzheufa8ff292011-10-30 15:16:41 +01001496 u32 *buf = vmcoreinfo_note;
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001497
1498 if (!vmcoreinfo_size)
1499 return;
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001500 buf = append_elf_note(buf, VMCOREINFO_NOTE_NAME, 0, vmcoreinfo_data,
1501 vmcoreinfo_size);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001502 final_note(buf);
1503}
1504
Michael Holzheufa8ff292011-10-30 15:16:41 +01001505void crash_save_vmcoreinfo(void)
1506{
Vivek Goyal63dca8d2012-07-30 14:42:36 -07001507 vmcoreinfo_append_str("CRASHTIME=%ld\n", get_seconds());
Michael Holzheufa8ff292011-10-30 15:16:41 +01001508 update_vmcoreinfo_note();
1509}
1510
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001511void vmcoreinfo_append_str(const char *fmt, ...)
1512{
1513 va_list args;
1514 char buf[0x50];
Zhang Yanfei310faaa2013-04-30 15:28:21 -07001515 size_t r;
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001516
1517 va_start(args, fmt);
Chen Ganga19428e2014-01-27 17:07:13 -08001518 r = vscnprintf(buf, sizeof(buf), fmt, args);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001519 va_end(args);
1520
Zhang Yanfei31c3a3f2013-04-30 15:28:23 -07001521 r = min(r, vmcoreinfo_max_size - vmcoreinfo_size);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001522
1523 memcpy(&vmcoreinfo_data[vmcoreinfo_size], buf, r);
1524
1525 vmcoreinfo_size += r;
1526}
1527
1528/*
1529 * provide an empty default implementation here -- architecture
1530 * code may override this
1531 */
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -07001532void __weak arch_crash_save_vmcoreinfo(void)
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001533{}
1534
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -07001535unsigned long __weak paddr_vmcoreinfo_note(void)
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001536{
1537 return __pa((unsigned long)(char *)&vmcoreinfo_note);
1538}
1539
1540static int __init crash_save_vmcoreinfo_init(void)
1541{
Ken'ichi Ohmichibba1f602008-02-07 00:15:22 -08001542 VMCOREINFO_OSRELEASE(init_uts_ns.name.release);
1543 VMCOREINFO_PAGESIZE(PAGE_SIZE);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001544
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001545 VMCOREINFO_SYMBOL(init_uts_ns);
1546 VMCOREINFO_SYMBOL(node_online_map);
Will Deacond034cfa2012-03-28 14:42:47 -07001547#ifdef CONFIG_MMU
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001548 VMCOREINFO_SYMBOL(swapper_pg_dir);
Will Deacond034cfa2012-03-28 14:42:47 -07001549#endif
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001550 VMCOREINFO_SYMBOL(_stext);
Joonsoo Kimf1c40692013-04-29 15:07:37 -07001551 VMCOREINFO_SYMBOL(vmap_area_list);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001552
1553#ifndef CONFIG_NEED_MULTIPLE_NODES
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001554 VMCOREINFO_SYMBOL(mem_map);
1555 VMCOREINFO_SYMBOL(contig_page_data);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001556#endif
1557#ifdef CONFIG_SPARSEMEM
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001558 VMCOREINFO_SYMBOL(mem_section);
1559 VMCOREINFO_LENGTH(mem_section, NR_SECTION_ROOTS);
Ken'ichi Ohmichic76f8602008-02-07 00:15:20 -08001560 VMCOREINFO_STRUCT_SIZE(mem_section);
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001561 VMCOREINFO_OFFSET(mem_section, section_mem_map);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001562#endif
Ken'ichi Ohmichic76f8602008-02-07 00:15:20 -08001563 VMCOREINFO_STRUCT_SIZE(page);
1564 VMCOREINFO_STRUCT_SIZE(pglist_data);
1565 VMCOREINFO_STRUCT_SIZE(zone);
1566 VMCOREINFO_STRUCT_SIZE(free_area);
1567 VMCOREINFO_STRUCT_SIZE(list_head);
1568 VMCOREINFO_SIZE(nodemask_t);
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001569 VMCOREINFO_OFFSET(page, flags);
1570 VMCOREINFO_OFFSET(page, _count);
1571 VMCOREINFO_OFFSET(page, mapping);
1572 VMCOREINFO_OFFSET(page, lru);
Atsushi Kumagai8d670912013-02-27 17:03:25 -08001573 VMCOREINFO_OFFSET(page, _mapcount);
1574 VMCOREINFO_OFFSET(page, private);
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001575 VMCOREINFO_OFFSET(pglist_data, node_zones);
1576 VMCOREINFO_OFFSET(pglist_data, nr_zones);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001577#ifdef CONFIG_FLAT_NODE_MEM_MAP
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001578 VMCOREINFO_OFFSET(pglist_data, node_mem_map);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001579#endif
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001580 VMCOREINFO_OFFSET(pglist_data, node_start_pfn);
1581 VMCOREINFO_OFFSET(pglist_data, node_spanned_pages);
1582 VMCOREINFO_OFFSET(pglist_data, node_id);
1583 VMCOREINFO_OFFSET(zone, free_area);
1584 VMCOREINFO_OFFSET(zone, vm_stat);
1585 VMCOREINFO_OFFSET(zone, spanned_pages);
1586 VMCOREINFO_OFFSET(free_area, free_list);
1587 VMCOREINFO_OFFSET(list_head, next);
1588 VMCOREINFO_OFFSET(list_head, prev);
Atsushi Kumagai13ba3fc2013-04-29 15:07:40 -07001589 VMCOREINFO_OFFSET(vmap_area, va_start);
1590 VMCOREINFO_OFFSET(vmap_area, list);
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001591 VMCOREINFO_LENGTH(zone.free_area, MAX_ORDER);
Neil Horman04d491a2009-04-02 16:58:57 -07001592 log_buf_kexec_setup();
Ken'ichi Ohmichi83a08e72008-01-08 15:33:05 -08001593 VMCOREINFO_LENGTH(free_area.free_list, MIGRATE_TYPES);
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001594 VMCOREINFO_NUMBER(NR_FREE_PAGES);
Ken'ichi Ohmichi122c7a52008-04-28 02:13:04 -07001595 VMCOREINFO_NUMBER(PG_lru);
1596 VMCOREINFO_NUMBER(PG_private);
1597 VMCOREINFO_NUMBER(PG_swapcache);
Atsushi Kumagai8d670912013-02-27 17:03:25 -08001598 VMCOREINFO_NUMBER(PG_slab);
Mitsuhiro Tanino0d0bf662013-02-27 17:03:27 -08001599#ifdef CONFIG_MEMORY_FAILURE
1600 VMCOREINFO_NUMBER(PG_hwpoison);
1601#endif
Petr Tesarikb3acc562014-06-23 13:22:03 -07001602 VMCOREINFO_NUMBER(PG_head_mask);
Atsushi Kumagai8d670912013-02-27 17:03:25 -08001603 VMCOREINFO_NUMBER(PAGE_BUDDY_MAPCOUNT_VALUE);
David Rientjes3a1122d2014-07-30 19:05:55 -07001604#ifdef CONFIG_HUGETLBFS
Atsushi Kumagai8f1d26d2014-07-30 16:08:39 -07001605 VMCOREINFO_SYMBOL(free_huge_page);
David Rientjes3a1122d2014-07-30 19:05:55 -07001606#endif
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001607
1608 arch_crash_save_vmcoreinfo();
Michael Holzheufa8ff292011-10-30 15:16:41 +01001609 update_vmcoreinfo_note();
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001610
1611 return 0;
1612}
1613
Paul Gortmakerc96d6662014-04-03 14:48:35 -07001614subsys_initcall(crash_save_vmcoreinfo_init);
Huang Ying3ab83522008-07-25 19:45:07 -07001615
Huang Ying7ade3fc2008-08-15 00:40:21 -07001616/*
1617 * Move into place and start executing a preloaded standalone
1618 * executable. If nothing was preloaded return an error.
Huang Ying3ab83522008-07-25 19:45:07 -07001619 */
1620int kernel_kexec(void)
1621{
1622 int error = 0;
1623
Andrew Morton8c5a1cf2008-08-15 00:40:27 -07001624 if (!mutex_trylock(&kexec_mutex))
Huang Ying3ab83522008-07-25 19:45:07 -07001625 return -EBUSY;
1626 if (!kexec_image) {
1627 error = -EINVAL;
1628 goto Unlock;
1629 }
1630
Huang Ying3ab83522008-07-25 19:45:07 -07001631#ifdef CONFIG_KEXEC_JUMP
Huang Ying7ade3fc2008-08-15 00:40:21 -07001632 if (kexec_image->preserve_context) {
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01001633 lock_system_sleep();
Huang Ying89081d12008-07-25 19:45:10 -07001634 pm_prepare_console();
1635 error = freeze_processes();
1636 if (error) {
1637 error = -EBUSY;
1638 goto Restore_console;
1639 }
1640 suspend_console();
Alan Sternd1616302009-05-24 22:05:42 +02001641 error = dpm_suspend_start(PMSG_FREEZE);
Huang Ying89081d12008-07-25 19:45:10 -07001642 if (error)
1643 goto Resume_console;
Alan Sternd1616302009-05-24 22:05:42 +02001644 /* At this point, dpm_suspend_start() has been called,
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001645 * but *not* dpm_suspend_end(). We *must* call
1646 * dpm_suspend_end() now. Otherwise, drivers for
Huang Ying89081d12008-07-25 19:45:10 -07001647 * some devices (e.g. interrupt controllers) become
1648 * desynchronized with the actual state of the
1649 * hardware at resume time, and evil weirdness ensues.
1650 */
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001651 error = dpm_suspend_end(PMSG_FREEZE);
Huang Ying89081d12008-07-25 19:45:10 -07001652 if (error)
Rafael J. Wysocki749b0af2009-03-16 22:34:35 +01001653 goto Resume_devices;
1654 error = disable_nonboot_cpus();
1655 if (error)
1656 goto Enable_cpus;
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +01001657 local_irq_disable();
Rafael J. Wysocki2e711c02011-04-26 19:15:07 +02001658 error = syscore_suspend();
Rafael J. Wysocki770824b2009-02-22 18:38:50 +01001659 if (error)
Rafael J. Wysocki749b0af2009-03-16 22:34:35 +01001660 goto Enable_irqs;
Huang Ying7ade3fc2008-08-15 00:40:21 -07001661 } else
Huang Ying3ab83522008-07-25 19:45:07 -07001662#endif
Huang Ying7ade3fc2008-08-15 00:40:21 -07001663 {
Khalid Aziz4fc9bbf2013-11-27 15:19:25 -07001664 kexec_in_progress = true;
Huang Yingca195b72008-08-15 00:40:24 -07001665 kernel_restart_prepare(NULL);
Vivek Goyalc97102b2013-12-18 17:08:31 -08001666 migrate_to_reboot_cpu();
Srivatsa S. Bhat011e4b02014-05-27 16:25:34 +05301667
1668 /*
1669 * migrate_to_reboot_cpu() disables CPU hotplug assuming that
1670 * no further code needs to use CPU hotplug (which is true in
1671 * the reboot case). However, the kexec path depends on using
1672 * CPU hotplug again; so re-enable it here.
1673 */
1674 cpu_hotplug_enable();
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001675 pr_emerg("Starting new kernel\n");
Huang Ying3ab83522008-07-25 19:45:07 -07001676 machine_shutdown();
1677 }
1678
1679 machine_kexec(kexec_image);
1680
Huang Ying3ab83522008-07-25 19:45:07 -07001681#ifdef CONFIG_KEXEC_JUMP
Huang Ying7ade3fc2008-08-15 00:40:21 -07001682 if (kexec_image->preserve_context) {
Rafael J. Wysocki19234c02011-04-20 00:36:11 +02001683 syscore_resume();
Rafael J. Wysocki749b0af2009-03-16 22:34:35 +01001684 Enable_irqs:
Huang Ying3ab83522008-07-25 19:45:07 -07001685 local_irq_enable();
Rafael J. Wysocki749b0af2009-03-16 22:34:35 +01001686 Enable_cpus:
Huang Ying89081d12008-07-25 19:45:10 -07001687 enable_nonboot_cpus();
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001688 dpm_resume_start(PMSG_RESTORE);
Huang Ying89081d12008-07-25 19:45:10 -07001689 Resume_devices:
Alan Sternd1616302009-05-24 22:05:42 +02001690 dpm_resume_end(PMSG_RESTORE);
Huang Ying89081d12008-07-25 19:45:10 -07001691 Resume_console:
1692 resume_console();
1693 thaw_processes();
1694 Restore_console:
1695 pm_restore_console();
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01001696 unlock_system_sleep();
Huang Ying3ab83522008-07-25 19:45:07 -07001697 }
Huang Ying7ade3fc2008-08-15 00:40:21 -07001698#endif
Huang Ying3ab83522008-07-25 19:45:07 -07001699
1700 Unlock:
Andrew Morton8c5a1cf2008-08-15 00:40:27 -07001701 mutex_unlock(&kexec_mutex);
Huang Ying3ab83522008-07-25 19:45:07 -07001702 return error;
1703}