blob: 062e5567750e3a6c39399d33e04a055742055113 [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
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700264static int kimage_normal_alloc(struct kimage **rimage, unsigned long entry,
Maneesh Soni72414d32005-06-25 14:58:28 -0700265 unsigned long nr_segments,
266 struct kexec_segment __user *segments)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700267{
268 int result;
269 struct kimage *image;
270
271 /* Allocate and initialize a controlling structure */
Vivek Goyaldabe7862014-08-08 14:25:45 -0700272 image = do_kimage_alloc_init();
273 if (!image)
274 return -ENOMEM;
275
276 image->start = entry;
277
278 result = copy_user_segment_list(image, nr_segments, segments);
Maneesh Soni72414d32005-06-25 14:58:28 -0700279 if (result)
Vivek Goyaldabe7862014-08-08 14:25:45 -0700280 goto out_free_image;
281
282 result = sanity_check_segment_list(image);
283 if (result)
284 goto out_free_image;
Maneesh Soni72414d32005-06-25 14:58:28 -0700285
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700286 /*
287 * Find a location for the control code buffer, and add it
288 * the vector of segments so that it's pages will also be
289 * counted as destination pages.
290 */
291 result = -ENOMEM;
292 image->control_code_page = kimage_alloc_control_pages(image,
Huang Ying163f6872008-08-15 00:40:22 -0700293 get_order(KEXEC_CONTROL_PAGE_SIZE));
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700294 if (!image->control_code_page) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -0700295 pr_err("Could not allocate control_code_buffer\n");
Vivek Goyaldabe7862014-08-08 14:25:45 -0700296 goto out_free_image;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700297 }
298
Huang Ying3ab83522008-07-25 19:45:07 -0700299 image->swap_page = kimage_alloc_control_pages(image, 0);
300 if (!image->swap_page) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -0700301 pr_err("Could not allocate swap buffer\n");
Vivek Goyaldabe7862014-08-08 14:25:45 -0700302 goto out_free_control_pages;
Huang Ying3ab83522008-07-25 19:45:07 -0700303 }
304
Zhang Yanfeib92e7e02013-02-27 17:03:29 -0800305 *rimage = image;
306 return 0;
Vivek Goyaldabe7862014-08-08 14:25:45 -0700307out_free_control_pages:
Zhang Yanfeib92e7e02013-02-27 17:03:29 -0800308 kimage_free_page_list(&image->control_pages);
Vivek Goyaldabe7862014-08-08 14:25:45 -0700309out_free_image:
Zhang Yanfeib92e7e02013-02-27 17:03:29 -0800310 kfree(image);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700311 return result;
312}
313
314static int kimage_crash_alloc(struct kimage **rimage, unsigned long entry,
Maneesh Soni72414d32005-06-25 14:58:28 -0700315 unsigned long nr_segments,
Alexey Dobriyan314b6a42005-06-27 22:29:33 -0700316 struct kexec_segment __user *segments)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700317{
318 int result;
319 struct kimage *image;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700320
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700321 /* Verify we have a valid entry point */
Vivek Goyaldabe7862014-08-08 14:25:45 -0700322 if ((entry < crashk_res.start) || (entry > crashk_res.end))
323 return -EADDRNOTAVAIL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700324
325 /* Allocate and initialize a controlling structure */
Vivek Goyaldabe7862014-08-08 14:25:45 -0700326 image = do_kimage_alloc_init();
327 if (!image)
328 return -ENOMEM;
329
330 image->start = entry;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700331
332 /* Enable the special crash kernel control page
333 * allocation policy.
334 */
335 image->control_page = crashk_res.start;
336 image->type = KEXEC_TYPE_CRASH;
337
Vivek Goyaldabe7862014-08-08 14:25:45 -0700338 result = copy_user_segment_list(image, nr_segments, segments);
339 if (result)
340 goto out_free_image;
Maneesh Soni72414d32005-06-25 14:58:28 -0700341
Vivek Goyaldabe7862014-08-08 14:25:45 -0700342 result = sanity_check_segment_list(image);
343 if (result)
344 goto out_free_image;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700345
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700346 /*
347 * Find a location for the control code buffer, and add
348 * the vector of segments so that it's pages will also be
349 * counted as destination pages.
350 */
351 result = -ENOMEM;
352 image->control_code_page = kimage_alloc_control_pages(image,
Huang Ying163f6872008-08-15 00:40:22 -0700353 get_order(KEXEC_CONTROL_PAGE_SIZE));
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700354 if (!image->control_code_page) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -0700355 pr_err("Could not allocate control_code_buffer\n");
Vivek Goyaldabe7862014-08-08 14:25:45 -0700356 goto out_free_image;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700357 }
358
Zhang Yanfei8c333ac2013-02-27 17:03:31 -0800359 *rimage = image;
360 return 0;
Maneesh Soni72414d32005-06-25 14:58:28 -0700361
Vivek Goyaldabe7862014-08-08 14:25:45 -0700362out_free_image:
Zhang Yanfei8c333ac2013-02-27 17:03:31 -0800363 kfree(image);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700364 return result;
365}
366
Maneesh Soni72414d32005-06-25 14:58:28 -0700367static int kimage_is_destination_range(struct kimage *image,
368 unsigned long start,
369 unsigned long end)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700370{
371 unsigned long i;
372
373 for (i = 0; i < image->nr_segments; i++) {
374 unsigned long mstart, mend;
Maneesh Soni72414d32005-06-25 14:58:28 -0700375
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700376 mstart = image->segment[i].mem;
Maneesh Soni72414d32005-06-25 14:58:28 -0700377 mend = mstart + image->segment[i].memsz;
378 if ((end > mstart) && (start < mend))
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700379 return 1;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700380 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700381
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700382 return 0;
383}
384
Al Viro9796fdd2005-10-21 03:22:03 -0400385static struct page *kimage_alloc_pages(gfp_t gfp_mask, unsigned int order)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700386{
387 struct page *pages;
Maneesh Soni72414d32005-06-25 14:58:28 -0700388
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700389 pages = alloc_pages(gfp_mask, order);
390 if (pages) {
391 unsigned int count, i;
392 pages->mapping = NULL;
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700393 set_page_private(pages, order);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700394 count = 1 << order;
Maneesh Soni72414d32005-06-25 14:58:28 -0700395 for (i = 0; i < count; i++)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700396 SetPageReserved(pages + i);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700397 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700398
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700399 return pages;
400}
401
402static void kimage_free_pages(struct page *page)
403{
404 unsigned int order, count, i;
Maneesh Soni72414d32005-06-25 14:58:28 -0700405
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700406 order = page_private(page);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700407 count = 1 << order;
Maneesh Soni72414d32005-06-25 14:58:28 -0700408 for (i = 0; i < count; i++)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700409 ClearPageReserved(page + i);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700410 __free_pages(page, order);
411}
412
413static void kimage_free_page_list(struct list_head *list)
414{
415 struct list_head *pos, *next;
Maneesh Soni72414d32005-06-25 14:58:28 -0700416
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700417 list_for_each_safe(pos, next, list) {
418 struct page *page;
419
420 page = list_entry(pos, struct page, lru);
421 list_del(&page->lru);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700422 kimage_free_pages(page);
423 }
424}
425
Maneesh Soni72414d32005-06-25 14:58:28 -0700426static struct page *kimage_alloc_normal_control_pages(struct kimage *image,
427 unsigned int order)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700428{
429 /* Control pages are special, they are the intermediaries
430 * that are needed while we copy the rest of the pages
431 * to their final resting place. As such they must
432 * not conflict with either the destination addresses
433 * or memory the kernel is already using.
434 *
435 * The only case where we really need more than one of
436 * these are for architectures where we cannot disable
437 * the MMU and must instead generate an identity mapped
438 * page table for all of the memory.
439 *
440 * At worst this runs in O(N) of the image size.
441 */
442 struct list_head extra_pages;
443 struct page *pages;
444 unsigned int count;
445
446 count = 1 << order;
447 INIT_LIST_HEAD(&extra_pages);
448
449 /* Loop while I can allocate a page and the page allocated
450 * is a destination page.
451 */
452 do {
453 unsigned long pfn, epfn, addr, eaddr;
Maneesh Soni72414d32005-06-25 14:58:28 -0700454
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700455 pages = kimage_alloc_pages(GFP_KERNEL, order);
456 if (!pages)
457 break;
458 pfn = page_to_pfn(pages);
459 epfn = pfn + count;
460 addr = pfn << PAGE_SHIFT;
461 eaddr = epfn << PAGE_SHIFT;
462 if ((epfn >= (KEXEC_CONTROL_MEMORY_LIMIT >> PAGE_SHIFT)) ||
Maneesh Soni72414d32005-06-25 14:58:28 -0700463 kimage_is_destination_range(image, addr, eaddr)) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700464 list_add(&pages->lru, &extra_pages);
465 pages = NULL;
466 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700467 } while (!pages);
468
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700469 if (pages) {
470 /* Remember the allocated page... */
471 list_add(&pages->lru, &image->control_pages);
472
473 /* Because the page is already in it's destination
474 * location we will never allocate another page at
475 * that address. Therefore kimage_alloc_pages
476 * will not return it (again) and we don't need
477 * to give it an entry in image->segment[].
478 */
479 }
480 /* Deal with the destination pages I have inadvertently allocated.
481 *
482 * Ideally I would convert multi-page allocations into single
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300483 * page allocations, and add everything to image->dest_pages.
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700484 *
485 * For now it is simpler to just free the pages.
486 */
487 kimage_free_page_list(&extra_pages);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700488
Maneesh Soni72414d32005-06-25 14:58:28 -0700489 return pages;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700490}
491
Maneesh Soni72414d32005-06-25 14:58:28 -0700492static struct page *kimage_alloc_crash_control_pages(struct kimage *image,
493 unsigned int order)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700494{
495 /* Control pages are special, they are the intermediaries
496 * that are needed while we copy the rest of the pages
497 * to their final resting place. As such they must
498 * not conflict with either the destination addresses
499 * or memory the kernel is already using.
500 *
501 * Control pages are also the only pags we must allocate
502 * when loading a crash kernel. All of the other pages
503 * are specified by the segments and we just memcpy
504 * into them directly.
505 *
506 * The only case where we really need more than one of
507 * these are for architectures where we cannot disable
508 * the MMU and must instead generate an identity mapped
509 * page table for all of the memory.
510 *
511 * Given the low demand this implements a very simple
512 * allocator that finds the first hole of the appropriate
513 * size in the reserved memory region, and allocates all
514 * of the memory up to and including the hole.
515 */
516 unsigned long hole_start, hole_end, size;
517 struct page *pages;
Maneesh Soni72414d32005-06-25 14:58:28 -0700518
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700519 pages = NULL;
520 size = (1 << order) << PAGE_SHIFT;
521 hole_start = (image->control_page + (size - 1)) & ~(size - 1);
522 hole_end = hole_start + size - 1;
Maneesh Soni72414d32005-06-25 14:58:28 -0700523 while (hole_end <= crashk_res.end) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700524 unsigned long i;
Maneesh Soni72414d32005-06-25 14:58:28 -0700525
Michael Holzheu3d214fa2011-10-30 15:16:36 +0100526 if (hole_end > KEXEC_CRASH_CONTROL_MEMORY_LIMIT)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700527 break;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700528 /* See if I overlap any of the segments */
Maneesh Soni72414d32005-06-25 14:58:28 -0700529 for (i = 0; i < image->nr_segments; i++) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700530 unsigned long mstart, mend;
Maneesh Soni72414d32005-06-25 14:58:28 -0700531
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700532 mstart = image->segment[i].mem;
533 mend = mstart + image->segment[i].memsz - 1;
534 if ((hole_end >= mstart) && (hole_start <= mend)) {
535 /* Advance the hole to the end of the segment */
536 hole_start = (mend + (size - 1)) & ~(size - 1);
537 hole_end = hole_start + size - 1;
538 break;
539 }
540 }
541 /* If I don't overlap any segments I have found my hole! */
542 if (i == image->nr_segments) {
543 pages = pfn_to_page(hole_start >> PAGE_SHIFT);
544 break;
545 }
546 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700547 if (pages)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700548 image->control_page = hole_end;
Maneesh Soni72414d32005-06-25 14:58:28 -0700549
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700550 return pages;
551}
552
553
Maneesh Soni72414d32005-06-25 14:58:28 -0700554struct page *kimage_alloc_control_pages(struct kimage *image,
555 unsigned int order)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700556{
557 struct page *pages = NULL;
Maneesh Soni72414d32005-06-25 14:58:28 -0700558
559 switch (image->type) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700560 case KEXEC_TYPE_DEFAULT:
561 pages = kimage_alloc_normal_control_pages(image, order);
562 break;
563 case KEXEC_TYPE_CRASH:
564 pages = kimage_alloc_crash_control_pages(image, order);
565 break;
566 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700567
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700568 return pages;
569}
570
571static int kimage_add_entry(struct kimage *image, kimage_entry_t entry)
572{
Maneesh Soni72414d32005-06-25 14:58:28 -0700573 if (*image->entry != 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700574 image->entry++;
Maneesh Soni72414d32005-06-25 14:58:28 -0700575
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700576 if (image->entry == image->last_entry) {
577 kimage_entry_t *ind_page;
578 struct page *page;
Maneesh Soni72414d32005-06-25 14:58:28 -0700579
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700580 page = kimage_alloc_page(image, GFP_KERNEL, KIMAGE_NO_DEST);
Maneesh Soni72414d32005-06-25 14:58:28 -0700581 if (!page)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700582 return -ENOMEM;
Maneesh Soni72414d32005-06-25 14:58:28 -0700583
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700584 ind_page = page_address(page);
585 *image->entry = virt_to_phys(ind_page) | IND_INDIRECTION;
586 image->entry = ind_page;
Maneesh Soni72414d32005-06-25 14:58:28 -0700587 image->last_entry = ind_page +
588 ((PAGE_SIZE/sizeof(kimage_entry_t)) - 1);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700589 }
590 *image->entry = entry;
591 image->entry++;
592 *image->entry = 0;
Maneesh Soni72414d32005-06-25 14:58:28 -0700593
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700594 return 0;
595}
596
Maneesh Soni72414d32005-06-25 14:58:28 -0700597static int kimage_set_destination(struct kimage *image,
598 unsigned long destination)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700599{
600 int result;
601
602 destination &= PAGE_MASK;
603 result = kimage_add_entry(image, destination | IND_DESTINATION);
Maneesh Soni72414d32005-06-25 14:58:28 -0700604 if (result == 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700605 image->destination = destination;
Maneesh Soni72414d32005-06-25 14:58:28 -0700606
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700607 return result;
608}
609
610
611static int kimage_add_page(struct kimage *image, unsigned long page)
612{
613 int result;
614
615 page &= PAGE_MASK;
616 result = kimage_add_entry(image, page | IND_SOURCE);
Maneesh Soni72414d32005-06-25 14:58:28 -0700617 if (result == 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700618 image->destination += PAGE_SIZE;
Maneesh Soni72414d32005-06-25 14:58:28 -0700619
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700620 return result;
621}
622
623
624static void kimage_free_extra_pages(struct kimage *image)
625{
626 /* Walk through and free any extra destination pages I may have */
627 kimage_free_page_list(&image->dest_pages);
628
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300629 /* Walk through and free any unusable pages I have cached */
Vivek Goyal7d3e2bc2014-08-08 14:25:43 -0700630 kimage_free_page_list(&image->unusable_pages);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700631
632}
WANG Cong7fccf032008-07-25 19:45:02 -0700633static void kimage_terminate(struct kimage *image)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700634{
Maneesh Soni72414d32005-06-25 14:58:28 -0700635 if (*image->entry != 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700636 image->entry++;
Maneesh Soni72414d32005-06-25 14:58:28 -0700637
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700638 *image->entry = IND_DONE;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700639}
640
641#define for_each_kimage_entry(image, ptr, entry) \
642 for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE); \
Fabian Fredericke1bebcf2014-06-06 14:37:09 -0700643 ptr = (entry & IND_INDIRECTION) ? \
644 phys_to_virt((entry & PAGE_MASK)) : ptr + 1)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700645
646static void kimage_free_entry(kimage_entry_t entry)
647{
648 struct page *page;
649
650 page = pfn_to_page(entry >> PAGE_SHIFT);
651 kimage_free_pages(page);
652}
653
654static void kimage_free(struct kimage *image)
655{
656 kimage_entry_t *ptr, entry;
657 kimage_entry_t ind = 0;
658
659 if (!image)
660 return;
Maneesh Soni72414d32005-06-25 14:58:28 -0700661
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700662 kimage_free_extra_pages(image);
663 for_each_kimage_entry(image, ptr, entry) {
664 if (entry & IND_INDIRECTION) {
665 /* Free the previous indirection page */
Maneesh Soni72414d32005-06-25 14:58:28 -0700666 if (ind & IND_INDIRECTION)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700667 kimage_free_entry(ind);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700668 /* Save this indirection page until we are
669 * done with it.
670 */
671 ind = entry;
Fabian Fredericke1bebcf2014-06-06 14:37:09 -0700672 } else if (entry & IND_SOURCE)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700673 kimage_free_entry(entry);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700674 }
675 /* Free the final indirection page */
Maneesh Soni72414d32005-06-25 14:58:28 -0700676 if (ind & IND_INDIRECTION)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700677 kimage_free_entry(ind);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700678
679 /* Handle any machine specific cleanup */
680 machine_kexec_cleanup(image);
681
682 /* Free the kexec control pages... */
683 kimage_free_page_list(&image->control_pages);
684 kfree(image);
685}
686
Maneesh Soni72414d32005-06-25 14:58:28 -0700687static kimage_entry_t *kimage_dst_used(struct kimage *image,
688 unsigned long page)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700689{
690 kimage_entry_t *ptr, entry;
691 unsigned long destination = 0;
692
693 for_each_kimage_entry(image, ptr, entry) {
Maneesh Soni72414d32005-06-25 14:58:28 -0700694 if (entry & IND_DESTINATION)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700695 destination = entry & PAGE_MASK;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700696 else if (entry & IND_SOURCE) {
Maneesh Soni72414d32005-06-25 14:58:28 -0700697 if (page == destination)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700698 return ptr;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700699 destination += PAGE_SIZE;
700 }
701 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700702
Alexey Dobriyan314b6a42005-06-27 22:29:33 -0700703 return NULL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700704}
705
Maneesh Soni72414d32005-06-25 14:58:28 -0700706static struct page *kimage_alloc_page(struct kimage *image,
Al Viro9796fdd2005-10-21 03:22:03 -0400707 gfp_t gfp_mask,
Maneesh Soni72414d32005-06-25 14:58:28 -0700708 unsigned long destination)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700709{
710 /*
711 * Here we implement safeguards to ensure that a source page
712 * is not copied to its destination page before the data on
713 * the destination page is no longer useful.
714 *
715 * To do this we maintain the invariant that a source page is
716 * either its own destination page, or it is not a
717 * destination page at all.
718 *
719 * That is slightly stronger than required, but the proof
720 * that no problems will not occur is trivial, and the
721 * implementation is simply to verify.
722 *
723 * When allocating all pages normally this algorithm will run
724 * in O(N) time, but in the worst case it will run in O(N^2)
725 * time. If the runtime is a problem the data structures can
726 * be fixed.
727 */
728 struct page *page;
729 unsigned long addr;
730
731 /*
732 * Walk through the list of destination pages, and see if I
733 * have a match.
734 */
735 list_for_each_entry(page, &image->dest_pages, lru) {
736 addr = page_to_pfn(page) << PAGE_SHIFT;
737 if (addr == destination) {
738 list_del(&page->lru);
739 return page;
740 }
741 }
742 page = NULL;
743 while (1) {
744 kimage_entry_t *old;
745
746 /* Allocate a page, if we run out of memory give up */
747 page = kimage_alloc_pages(gfp_mask, 0);
Maneesh Soni72414d32005-06-25 14:58:28 -0700748 if (!page)
Alexey Dobriyan314b6a42005-06-27 22:29:33 -0700749 return NULL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700750 /* If the page cannot be used file it away */
Maneesh Soni72414d32005-06-25 14:58:28 -0700751 if (page_to_pfn(page) >
752 (KEXEC_SOURCE_MEMORY_LIMIT >> PAGE_SHIFT)) {
Vivek Goyal7d3e2bc2014-08-08 14:25:43 -0700753 list_add(&page->lru, &image->unusable_pages);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700754 continue;
755 }
756 addr = page_to_pfn(page) << PAGE_SHIFT;
757
758 /* If it is the destination page we want use it */
759 if (addr == destination)
760 break;
761
762 /* If the page is not a destination page use it */
Maneesh Soni72414d32005-06-25 14:58:28 -0700763 if (!kimage_is_destination_range(image, addr,
764 addr + PAGE_SIZE))
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700765 break;
766
767 /*
768 * I know that the page is someones destination page.
769 * See if there is already a source page for this
770 * destination page. And if so swap the source pages.
771 */
772 old = kimage_dst_used(image, addr);
773 if (old) {
774 /* If so move it */
775 unsigned long old_addr;
776 struct page *old_page;
777
778 old_addr = *old & PAGE_MASK;
779 old_page = pfn_to_page(old_addr >> PAGE_SHIFT);
780 copy_highpage(page, old_page);
781 *old = addr | (*old & ~PAGE_MASK);
782
783 /* The old page I have found cannot be a
Jonathan Steelf9092f32008-09-22 13:57:45 -0700784 * destination page, so return it if it's
785 * gfp_flags honor the ones passed in.
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700786 */
Jonathan Steelf9092f32008-09-22 13:57:45 -0700787 if (!(gfp_mask & __GFP_HIGHMEM) &&
788 PageHighMem(old_page)) {
789 kimage_free_pages(old_page);
790 continue;
791 }
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700792 addr = old_addr;
793 page = old_page;
794 break;
Fabian Fredericke1bebcf2014-06-06 14:37:09 -0700795 } else {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700796 /* Place the page on the destination list I
797 * will use it later.
798 */
799 list_add(&page->lru, &image->dest_pages);
800 }
801 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700802
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700803 return page;
804}
805
806static int kimage_load_normal_segment(struct kimage *image,
Maneesh Soni72414d32005-06-25 14:58:28 -0700807 struct kexec_segment *segment)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700808{
809 unsigned long maddr;
Zhang Yanfei310faaa2013-04-30 15:28:21 -0700810 size_t ubytes, mbytes;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700811 int result;
Alexey Dobriyan314b6a42005-06-27 22:29:33 -0700812 unsigned char __user *buf;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700813
814 result = 0;
815 buf = segment->buf;
816 ubytes = segment->bufsz;
817 mbytes = segment->memsz;
818 maddr = segment->mem;
819
820 result = kimage_set_destination(image, maddr);
Maneesh Soni72414d32005-06-25 14:58:28 -0700821 if (result < 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700822 goto out;
Maneesh Soni72414d32005-06-25 14:58:28 -0700823
824 while (mbytes) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700825 struct page *page;
826 char *ptr;
827 size_t uchunk, mchunk;
Maneesh Soni72414d32005-06-25 14:58:28 -0700828
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700829 page = kimage_alloc_page(image, GFP_HIGHUSER, maddr);
Stephen Hemmingerc80544d2007-10-18 03:07:05 -0700830 if (!page) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700831 result = -ENOMEM;
832 goto out;
833 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700834 result = kimage_add_page(image, page_to_pfn(page)
835 << PAGE_SHIFT);
836 if (result < 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700837 goto out;
Maneesh Soni72414d32005-06-25 14:58:28 -0700838
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700839 ptr = kmap(page);
840 /* Start with a clear page */
Jan Beulich3ecb01d2010-10-26 14:22:27 -0700841 clear_page(ptr);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700842 ptr += maddr & ~PAGE_MASK;
Zhang Yanfei31c3a3f2013-04-30 15:28:23 -0700843 mchunk = min_t(size_t, mbytes,
844 PAGE_SIZE - (maddr & ~PAGE_MASK));
845 uchunk = min(ubytes, mchunk);
Maneesh Soni72414d32005-06-25 14:58:28 -0700846
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700847 result = copy_from_user(ptr, buf, uchunk);
848 kunmap(page);
849 if (result) {
Dan Carpenterf65a03f2010-08-10 18:03:31 -0700850 result = -EFAULT;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700851 goto out;
852 }
853 ubytes -= uchunk;
854 maddr += mchunk;
855 buf += mchunk;
856 mbytes -= mchunk;
857 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700858out:
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700859 return result;
860}
861
862static int kimage_load_crash_segment(struct kimage *image,
Maneesh Soni72414d32005-06-25 14:58:28 -0700863 struct kexec_segment *segment)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700864{
865 /* For crash dumps kernels we simply copy the data from
866 * user space to it's destination.
867 * We do things a page at a time for the sake of kmap.
868 */
869 unsigned long maddr;
Zhang Yanfei310faaa2013-04-30 15:28:21 -0700870 size_t ubytes, mbytes;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700871 int result;
Alexey Dobriyan314b6a42005-06-27 22:29:33 -0700872 unsigned char __user *buf;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700873
874 result = 0;
875 buf = segment->buf;
876 ubytes = segment->bufsz;
877 mbytes = segment->memsz;
878 maddr = segment->mem;
Maneesh Soni72414d32005-06-25 14:58:28 -0700879 while (mbytes) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700880 struct page *page;
881 char *ptr;
882 size_t uchunk, mchunk;
Maneesh Soni72414d32005-06-25 14:58:28 -0700883
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700884 page = pfn_to_page(maddr >> PAGE_SHIFT);
Stephen Hemmingerc80544d2007-10-18 03:07:05 -0700885 if (!page) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700886 result = -ENOMEM;
887 goto out;
888 }
889 ptr = kmap(page);
890 ptr += maddr & ~PAGE_MASK;
Zhang Yanfei31c3a3f2013-04-30 15:28:23 -0700891 mchunk = min_t(size_t, mbytes,
892 PAGE_SIZE - (maddr & ~PAGE_MASK));
893 uchunk = min(ubytes, mchunk);
894 if (mchunk > uchunk) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700895 /* Zero the trailing part of the page */
896 memset(ptr + uchunk, 0, mchunk - uchunk);
897 }
898 result = copy_from_user(ptr, buf, uchunk);
Zou Nan haia79561132006-12-07 09:51:35 -0800899 kexec_flush_icache_page(page);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700900 kunmap(page);
901 if (result) {
Dan Carpenterf65a03f2010-08-10 18:03:31 -0700902 result = -EFAULT;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700903 goto out;
904 }
905 ubytes -= uchunk;
906 maddr += mchunk;
907 buf += mchunk;
908 mbytes -= mchunk;
909 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700910out:
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700911 return result;
912}
913
914static int kimage_load_segment(struct kimage *image,
Maneesh Soni72414d32005-06-25 14:58:28 -0700915 struct kexec_segment *segment)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700916{
917 int result = -ENOMEM;
Maneesh Soni72414d32005-06-25 14:58:28 -0700918
919 switch (image->type) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700920 case KEXEC_TYPE_DEFAULT:
921 result = kimage_load_normal_segment(image, segment);
922 break;
923 case KEXEC_TYPE_CRASH:
924 result = kimage_load_crash_segment(image, segment);
925 break;
926 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700927
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700928 return result;
929}
930
931/*
932 * Exec Kernel system call: for obvious reasons only root may call it.
933 *
934 * This call breaks up into three pieces.
935 * - A generic part which loads the new kernel from the current
936 * address space, and very carefully places the data in the
937 * allocated pages.
938 *
939 * - A generic part that interacts with the kernel and tells all of
940 * the devices to shut down. Preventing on-going dmas, and placing
941 * the devices in a consistent state so a later kernel can
942 * reinitialize them.
943 *
944 * - A machine specific part that includes the syscall number
Geert Uytterhoeven002ace72013-09-15 11:35:37 +0200945 * and then copies the image to it's final destination. And
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700946 * jumps into the image at entry.
947 *
948 * kexec does not sync, or unmount filesystems so if you need
949 * that to happen you need to do that yourself.
950 */
Jeff Moyerc330dda2006-06-23 02:05:07 -0700951struct kimage *kexec_image;
952struct kimage *kexec_crash_image;
Kees Cook79847542014-01-23 15:55:59 -0800953int kexec_load_disabled;
Andrew Morton8c5a1cf2008-08-15 00:40:27 -0700954
955static DEFINE_MUTEX(kexec_mutex);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700956
Heiko Carstens754fe8d2009-01-14 14:14:09 +0100957SYSCALL_DEFINE4(kexec_load, unsigned long, entry, unsigned long, nr_segments,
958 struct kexec_segment __user *, segments, unsigned long, flags)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700959{
960 struct kimage **dest_image, *image;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700961 int result;
962
963 /* We only trust the superuser with rebooting the system. */
Kees Cook79847542014-01-23 15:55:59 -0800964 if (!capable(CAP_SYS_BOOT) || kexec_load_disabled)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700965 return -EPERM;
966
967 /*
968 * Verify we have a legal set of flags
969 * This leaves us room for future extensions.
970 */
971 if ((flags & KEXEC_FLAGS) != (flags & ~KEXEC_ARCH_MASK))
972 return -EINVAL;
973
974 /* Verify we are on the appropriate architecture */
975 if (((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH) &&
976 ((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH_DEFAULT))
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700977 return -EINVAL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700978
979 /* Put an artificial cap on the number
980 * of segments passed to kexec_load.
981 */
982 if (nr_segments > KEXEC_SEGMENT_MAX)
983 return -EINVAL;
984
985 image = NULL;
986 result = 0;
987
988 /* Because we write directly to the reserved memory
989 * region when loading crash kernels we need a mutex here to
990 * prevent multiple crash kernels from attempting to load
991 * simultaneously, and to prevent a crash kernel from loading
992 * over the top of a in use crash kernel.
993 *
994 * KISS: always take the mutex.
995 */
Andrew Morton8c5a1cf2008-08-15 00:40:27 -0700996 if (!mutex_trylock(&kexec_mutex))
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700997 return -EBUSY;
Maneesh Soni72414d32005-06-25 14:58:28 -0700998
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700999 dest_image = &kexec_image;
Maneesh Soni72414d32005-06-25 14:58:28 -07001000 if (flags & KEXEC_ON_CRASH)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001001 dest_image = &kexec_crash_image;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001002 if (nr_segments > 0) {
1003 unsigned long i;
Maneesh Soni72414d32005-06-25 14:58:28 -07001004
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001005 /* Loading another kernel to reboot into */
Maneesh Soni72414d32005-06-25 14:58:28 -07001006 if ((flags & KEXEC_ON_CRASH) == 0)
1007 result = kimage_normal_alloc(&image, entry,
1008 nr_segments, segments);
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001009 /* Loading another kernel to switch to if this one crashes */
1010 else if (flags & KEXEC_ON_CRASH) {
1011 /* Free any current crash dump kernel before
1012 * we corrupt it.
1013 */
1014 kimage_free(xchg(&kexec_crash_image, NULL));
Maneesh Soni72414d32005-06-25 14:58:28 -07001015 result = kimage_crash_alloc(&image, entry,
1016 nr_segments, segments);
Michael Holzheu558df722011-10-30 15:16:43 +01001017 crash_map_reserved_pages();
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001018 }
Maneesh Soni72414d32005-06-25 14:58:28 -07001019 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001020 goto out;
Maneesh Soni72414d32005-06-25 14:58:28 -07001021
Huang Ying3ab83522008-07-25 19:45:07 -07001022 if (flags & KEXEC_PRESERVE_CONTEXT)
1023 image->preserve_context = 1;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001024 result = machine_kexec_prepare(image);
Maneesh Soni72414d32005-06-25 14:58:28 -07001025 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001026 goto out;
Maneesh Soni72414d32005-06-25 14:58:28 -07001027
1028 for (i = 0; i < nr_segments; i++) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001029 result = kimage_load_segment(image, &image->segment[i]);
Maneesh Soni72414d32005-06-25 14:58:28 -07001030 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001031 goto out;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001032 }
WANG Cong7fccf032008-07-25 19:45:02 -07001033 kimage_terminate(image);
Michael Holzheu558df722011-10-30 15:16:43 +01001034 if (flags & KEXEC_ON_CRASH)
1035 crash_unmap_reserved_pages();
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001036 }
1037 /* Install the new kernel, and Uninstall the old */
1038 image = xchg(dest_image, image);
1039
Maneesh Soni72414d32005-06-25 14:58:28 -07001040out:
Andrew Morton8c5a1cf2008-08-15 00:40:27 -07001041 mutex_unlock(&kexec_mutex);
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001042 kimage_free(image);
Maneesh Soni72414d32005-06-25 14:58:28 -07001043
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001044 return result;
1045}
1046
Michael Holzheu558df722011-10-30 15:16:43 +01001047/*
1048 * Add and remove page tables for crashkernel memory
1049 *
1050 * Provide an empty default implementation here -- architecture
1051 * code may override this
1052 */
1053void __weak crash_map_reserved_pages(void)
1054{}
1055
1056void __weak crash_unmap_reserved_pages(void)
1057{}
1058
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001059#ifdef CONFIG_COMPAT
Heiko Carstensca2c4052014-03-04 17:13:42 +01001060COMPAT_SYSCALL_DEFINE4(kexec_load, compat_ulong_t, entry,
1061 compat_ulong_t, nr_segments,
1062 struct compat_kexec_segment __user *, segments,
1063 compat_ulong_t, flags)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001064{
1065 struct compat_kexec_segment in;
1066 struct kexec_segment out, __user *ksegments;
1067 unsigned long i, result;
1068
1069 /* Don't allow clients that don't understand the native
1070 * architecture to do anything.
1071 */
Maneesh Soni72414d32005-06-25 14:58:28 -07001072 if ((flags & KEXEC_ARCH_MASK) == KEXEC_ARCH_DEFAULT)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001073 return -EINVAL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001074
Maneesh Soni72414d32005-06-25 14:58:28 -07001075 if (nr_segments > KEXEC_SEGMENT_MAX)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001076 return -EINVAL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001077
1078 ksegments = compat_alloc_user_space(nr_segments * sizeof(out));
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001079 for (i = 0; i < nr_segments; i++) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001080 result = copy_from_user(&in, &segments[i], sizeof(in));
Maneesh Soni72414d32005-06-25 14:58:28 -07001081 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001082 return -EFAULT;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001083
1084 out.buf = compat_ptr(in.buf);
1085 out.bufsz = in.bufsz;
1086 out.mem = in.mem;
1087 out.memsz = in.memsz;
1088
1089 result = copy_to_user(&ksegments[i], &out, sizeof(out));
Maneesh Soni72414d32005-06-25 14:58:28 -07001090 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001091 return -EFAULT;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001092 }
1093
1094 return sys_kexec_load(entry, nr_segments, ksegments, flags);
1095}
1096#endif
1097
Alexander Nyberg6e274d12005-06-25 14:58:26 -07001098void crash_kexec(struct pt_regs *regs)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001099{
Andrew Morton8c5a1cf2008-08-15 00:40:27 -07001100 /* Take the kexec_mutex here to prevent sys_kexec_load
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001101 * running on one cpu from replacing the crash kernel
1102 * we are using after a panic on a different cpu.
1103 *
1104 * If the crash kernel was not located in a fixed area
1105 * of memory the xchg(&kexec_crash_image) would be
1106 * sufficient. But since I reuse the memory...
1107 */
Andrew Morton8c5a1cf2008-08-15 00:40:27 -07001108 if (mutex_trylock(&kexec_mutex)) {
David Wilderc0ce7d02006-06-23 15:29:34 -07001109 if (kexec_crash_image) {
Vivek Goyale996e582006-01-09 20:51:44 -08001110 struct pt_regs fixed_regs;
KOSAKI Motohiro0f4bd462009-12-22 03:15:43 +00001111
Vivek Goyale996e582006-01-09 20:51:44 -08001112 crash_setup_regs(&fixed_regs, regs);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001113 crash_save_vmcoreinfo();
Vivek Goyale996e582006-01-09 20:51:44 -08001114 machine_crash_shutdown(&fixed_regs);
David Wilderc0ce7d02006-06-23 15:29:34 -07001115 machine_kexec(kexec_crash_image);
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001116 }
Andrew Morton8c5a1cf2008-08-15 00:40:27 -07001117 mutex_unlock(&kexec_mutex);
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001118 }
1119}
Vivek Goyalcc571652006-01-09 20:51:41 -08001120
Amerigo Wang06a7f712009-12-15 16:47:46 -08001121size_t crash_get_memory_size(void)
1122{
Pavan Naregundie05bd332010-06-29 15:05:28 -07001123 size_t size = 0;
Amerigo Wang06a7f712009-12-15 16:47:46 -08001124 mutex_lock(&kexec_mutex);
Pavan Naregundie05bd332010-06-29 15:05:28 -07001125 if (crashk_res.end != crashk_res.start)
Joe Perches28f65c112011-06-09 09:13:32 -07001126 size = resource_size(&crashk_res);
Amerigo Wang06a7f712009-12-15 16:47:46 -08001127 mutex_unlock(&kexec_mutex);
1128 return size;
1129}
1130
Anton Blanchardc0bb9e42010-08-25 10:22:58 +10001131void __weak crash_free_reserved_phys_range(unsigned long begin,
1132 unsigned long end)
Amerigo Wang06a7f712009-12-15 16:47:46 -08001133{
1134 unsigned long addr;
1135
Jiang Liue07cee22013-04-29 15:06:58 -07001136 for (addr = begin; addr < end; addr += PAGE_SIZE)
1137 free_reserved_page(pfn_to_page(addr >> PAGE_SHIFT));
Amerigo Wang06a7f712009-12-15 16:47:46 -08001138}
1139
1140int crash_shrink_memory(unsigned long new_size)
1141{
1142 int ret = 0;
1143 unsigned long start, end;
Michael Holzheubec013c2012-01-12 17:20:15 -08001144 unsigned long old_size;
Michael Holzheu6480e5a2012-01-12 17:20:14 -08001145 struct resource *ram_res;
Amerigo Wang06a7f712009-12-15 16:47:46 -08001146
1147 mutex_lock(&kexec_mutex);
1148
1149 if (kexec_crash_image) {
1150 ret = -ENOENT;
1151 goto unlock;
1152 }
1153 start = crashk_res.start;
1154 end = crashk_res.end;
Michael Holzheubec013c2012-01-12 17:20:15 -08001155 old_size = (end == 0) ? 0 : end - start + 1;
1156 if (new_size >= old_size) {
1157 ret = (new_size == old_size) ? 0 : -EINVAL;
Amerigo Wang06a7f712009-12-15 16:47:46 -08001158 goto unlock;
1159 }
1160
Michael Holzheu6480e5a2012-01-12 17:20:14 -08001161 ram_res = kzalloc(sizeof(*ram_res), GFP_KERNEL);
1162 if (!ram_res) {
1163 ret = -ENOMEM;
1164 goto unlock;
1165 }
1166
Michael Holzheu558df722011-10-30 15:16:43 +01001167 start = roundup(start, KEXEC_CRASH_MEM_ALIGN);
1168 end = roundup(start + new_size, KEXEC_CRASH_MEM_ALIGN);
Amerigo Wang06a7f712009-12-15 16:47:46 -08001169
Michael Holzheu558df722011-10-30 15:16:43 +01001170 crash_map_reserved_pages();
Anton Blanchardc0bb9e42010-08-25 10:22:58 +10001171 crash_free_reserved_phys_range(end, crashk_res.end);
Amerigo Wang06a7f712009-12-15 16:47:46 -08001172
Pavan Naregundie05bd332010-06-29 15:05:28 -07001173 if ((start == end) && (crashk_res.parent != NULL))
Amerigo Wang06a7f712009-12-15 16:47:46 -08001174 release_resource(&crashk_res);
Michael Holzheu6480e5a2012-01-12 17:20:14 -08001175
1176 ram_res->start = end;
1177 ram_res->end = crashk_res.end;
1178 ram_res->flags = IORESOURCE_BUSY | IORESOURCE_MEM;
1179 ram_res->name = "System RAM";
1180
Vitaly Mayatskikh475f9aa62010-05-11 14:06:51 -07001181 crashk_res.end = end - 1;
Michael Holzheu6480e5a2012-01-12 17:20:14 -08001182
1183 insert_resource(&iomem_resource, ram_res);
Michael Holzheu558df722011-10-30 15:16:43 +01001184 crash_unmap_reserved_pages();
Amerigo Wang06a7f712009-12-15 16:47:46 -08001185
1186unlock:
1187 mutex_unlock(&kexec_mutex);
1188 return ret;
1189}
1190
Magnus Damm85916f82006-12-06 20:40:41 -08001191static u32 *append_elf_note(u32 *buf, char *name, unsigned type, void *data,
1192 size_t data_len)
1193{
1194 struct elf_note note;
1195
1196 note.n_namesz = strlen(name) + 1;
1197 note.n_descsz = data_len;
1198 note.n_type = type;
1199 memcpy(buf, &note, sizeof(note));
1200 buf += (sizeof(note) + 3)/4;
1201 memcpy(buf, name, note.n_namesz);
1202 buf += (note.n_namesz + 3)/4;
1203 memcpy(buf, data, note.n_descsz);
1204 buf += (note.n_descsz + 3)/4;
1205
1206 return buf;
1207}
1208
1209static void final_note(u32 *buf)
1210{
1211 struct elf_note note;
1212
1213 note.n_namesz = 0;
1214 note.n_descsz = 0;
1215 note.n_type = 0;
1216 memcpy(buf, &note, sizeof(note));
1217}
1218
1219void crash_save_cpu(struct pt_regs *regs, int cpu)
1220{
1221 struct elf_prstatus prstatus;
1222 u32 *buf;
1223
Rusty Russell4f4b6c12009-01-01 10:12:15 +10301224 if ((cpu < 0) || (cpu >= nr_cpu_ids))
Magnus Damm85916f82006-12-06 20:40:41 -08001225 return;
1226
1227 /* Using ELF notes here is opportunistic.
1228 * I need a well defined structure format
1229 * for the data I pass, and I need tags
1230 * on the data to indicate what information I have
1231 * squirrelled away. ELF notes happen to provide
1232 * all of that, so there is no need to invent something new.
1233 */
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001234 buf = (u32 *)per_cpu_ptr(crash_notes, cpu);
Magnus Damm85916f82006-12-06 20:40:41 -08001235 if (!buf)
1236 return;
1237 memset(&prstatus, 0, sizeof(prstatus));
1238 prstatus.pr_pid = current->pid;
Tejun Heo6cd61c0b2009-02-09 22:17:39 +09001239 elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
Simon Horman6672f762007-05-08 00:28:22 -07001240 buf = append_elf_note(buf, KEXEC_CORE_NOTE_NAME, NT_PRSTATUS,
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001241 &prstatus, sizeof(prstatus));
Magnus Damm85916f82006-12-06 20:40:41 -08001242 final_note(buf);
1243}
1244
Vivek Goyalcc571652006-01-09 20:51:41 -08001245static int __init crash_notes_memory_init(void)
1246{
1247 /* Allocate memory for saving cpu registers. */
1248 crash_notes = alloc_percpu(note_buf_t);
1249 if (!crash_notes) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001250 pr_warn("Kexec: Memory allocation for saving cpu register states failed\n");
Vivek Goyalcc571652006-01-09 20:51:41 -08001251 return -ENOMEM;
1252 }
1253 return 0;
1254}
Paul Gortmakerc96d6662014-04-03 14:48:35 -07001255subsys_initcall(crash_notes_memory_init);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001256
Bernhard Wallecba63c32007-10-18 23:40:58 -07001257
1258/*
1259 * parsing the "crashkernel" commandline
1260 *
1261 * this code is intended to be called from architecture specific code
1262 */
1263
1264
1265/*
1266 * This function parses command lines in the format
1267 *
1268 * crashkernel=ramsize-range:size[,...][@offset]
1269 *
1270 * The function returns 0 on success and -EINVAL on failure.
1271 */
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001272static int __init parse_crashkernel_mem(char *cmdline,
1273 unsigned long long system_ram,
1274 unsigned long long *crash_size,
1275 unsigned long long *crash_base)
Bernhard Wallecba63c32007-10-18 23:40:58 -07001276{
1277 char *cur = cmdline, *tmp;
1278
1279 /* for each entry of the comma-separated list */
1280 do {
1281 unsigned long long start, end = ULLONG_MAX, size;
1282
1283 /* get the start of the range */
1284 start = memparse(cur, &tmp);
1285 if (cur == tmp) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001286 pr_warn("crashkernel: Memory value expected\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001287 return -EINVAL;
1288 }
1289 cur = tmp;
1290 if (*cur != '-') {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001291 pr_warn("crashkernel: '-' expected\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001292 return -EINVAL;
1293 }
1294 cur++;
1295
1296 /* if no ':' is here, than we read the end */
1297 if (*cur != ':') {
1298 end = memparse(cur, &tmp);
1299 if (cur == tmp) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001300 pr_warn("crashkernel: Memory value expected\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001301 return -EINVAL;
1302 }
1303 cur = tmp;
1304 if (end <= start) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001305 pr_warn("crashkernel: end <= start\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001306 return -EINVAL;
1307 }
1308 }
1309
1310 if (*cur != ':') {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001311 pr_warn("crashkernel: ':' expected\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001312 return -EINVAL;
1313 }
1314 cur++;
1315
1316 size = memparse(cur, &tmp);
1317 if (cur == tmp) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001318 pr_warn("Memory value expected\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001319 return -EINVAL;
1320 }
1321 cur = tmp;
1322 if (size >= system_ram) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001323 pr_warn("crashkernel: invalid size\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001324 return -EINVAL;
1325 }
1326
1327 /* match ? */
Michael Ellermanbe089d792008-05-01 04:34:49 -07001328 if (system_ram >= start && system_ram < end) {
Bernhard Wallecba63c32007-10-18 23:40:58 -07001329 *crash_size = size;
1330 break;
1331 }
1332 } while (*cur++ == ',');
1333
1334 if (*crash_size > 0) {
Hidetoshi Seto11c7da42009-07-29 15:02:08 -07001335 while (*cur && *cur != ' ' && *cur != '@')
Bernhard Wallecba63c32007-10-18 23:40:58 -07001336 cur++;
1337 if (*cur == '@') {
1338 cur++;
1339 *crash_base = memparse(cur, &tmp);
1340 if (cur == tmp) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001341 pr_warn("Memory value expected after '@'\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001342 return -EINVAL;
1343 }
1344 }
1345 }
1346
1347 return 0;
1348}
1349
1350/*
1351 * That function parses "simple" (old) crashkernel command lines like
1352 *
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001353 * crashkernel=size[@offset]
Bernhard Wallecba63c32007-10-18 23:40:58 -07001354 *
1355 * It returns 0 on success and -EINVAL on failure.
1356 */
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001357static int __init parse_crashkernel_simple(char *cmdline,
1358 unsigned long long *crash_size,
1359 unsigned long long *crash_base)
Bernhard Wallecba63c32007-10-18 23:40:58 -07001360{
1361 char *cur = cmdline;
1362
1363 *crash_size = memparse(cmdline, &cur);
1364 if (cmdline == cur) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001365 pr_warn("crashkernel: memory value expected\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001366 return -EINVAL;
1367 }
1368
1369 if (*cur == '@')
1370 *crash_base = memparse(cur+1, &cur);
Zhenzhong Duaneaa3be62012-03-28 14:42:47 -07001371 else if (*cur != ' ' && *cur != '\0') {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001372 pr_warn("crashkernel: unrecognized char\n");
Zhenzhong Duaneaa3be62012-03-28 14:42:47 -07001373 return -EINVAL;
1374 }
Bernhard Wallecba63c32007-10-18 23:40:58 -07001375
1376 return 0;
1377}
1378
Yinghai Luadbc7422013-04-15 22:23:48 -07001379#define SUFFIX_HIGH 0
1380#define SUFFIX_LOW 1
1381#define SUFFIX_NULL 2
1382static __initdata char *suffix_tbl[] = {
1383 [SUFFIX_HIGH] = ",high",
1384 [SUFFIX_LOW] = ",low",
1385 [SUFFIX_NULL] = NULL,
1386};
1387
Bernhard Wallecba63c32007-10-18 23:40:58 -07001388/*
Yinghai Luadbc7422013-04-15 22:23:48 -07001389 * That function parses "suffix" crashkernel command lines like
1390 *
1391 * crashkernel=size,[high|low]
1392 *
1393 * It returns 0 on success and -EINVAL on failure.
Bernhard Wallecba63c32007-10-18 23:40:58 -07001394 */
Yinghai Luadbc7422013-04-15 22:23:48 -07001395static int __init parse_crashkernel_suffix(char *cmdline,
1396 unsigned long long *crash_size,
1397 unsigned long long *crash_base,
1398 const char *suffix)
1399{
1400 char *cur = cmdline;
1401
1402 *crash_size = memparse(cmdline, &cur);
1403 if (cmdline == cur) {
1404 pr_warn("crashkernel: memory value expected\n");
1405 return -EINVAL;
1406 }
1407
1408 /* check with suffix */
1409 if (strncmp(cur, suffix, strlen(suffix))) {
1410 pr_warn("crashkernel: unrecognized char\n");
1411 return -EINVAL;
1412 }
1413 cur += strlen(suffix);
1414 if (*cur != ' ' && *cur != '\0') {
1415 pr_warn("crashkernel: unrecognized char\n");
1416 return -EINVAL;
1417 }
1418
1419 return 0;
1420}
1421
1422static __init char *get_last_crashkernel(char *cmdline,
1423 const char *name,
1424 const char *suffix)
1425{
1426 char *p = cmdline, *ck_cmdline = NULL;
1427
1428 /* find crashkernel and use the last one if there are more */
1429 p = strstr(p, name);
1430 while (p) {
1431 char *end_p = strchr(p, ' ');
1432 char *q;
1433
1434 if (!end_p)
1435 end_p = p + strlen(p);
1436
1437 if (!suffix) {
1438 int i;
1439
1440 /* skip the one with any known suffix */
1441 for (i = 0; suffix_tbl[i]; i++) {
1442 q = end_p - strlen(suffix_tbl[i]);
1443 if (!strncmp(q, suffix_tbl[i],
1444 strlen(suffix_tbl[i])))
1445 goto next;
1446 }
1447 ck_cmdline = p;
1448 } else {
1449 q = end_p - strlen(suffix);
1450 if (!strncmp(q, suffix, strlen(suffix)))
1451 ck_cmdline = p;
1452 }
1453next:
1454 p = strstr(p+1, name);
1455 }
1456
1457 if (!ck_cmdline)
1458 return NULL;
1459
1460 return ck_cmdline;
1461}
1462
Yinghai Lu0212f912013-01-24 12:20:11 -08001463static int __init __parse_crashkernel(char *cmdline,
Bernhard Wallecba63c32007-10-18 23:40:58 -07001464 unsigned long long system_ram,
1465 unsigned long long *crash_size,
Yinghai Lu0212f912013-01-24 12:20:11 -08001466 unsigned long long *crash_base,
Yinghai Luadbc7422013-04-15 22:23:48 -07001467 const char *name,
1468 const char *suffix)
Bernhard Wallecba63c32007-10-18 23:40:58 -07001469{
Bernhard Wallecba63c32007-10-18 23:40:58 -07001470 char *first_colon, *first_space;
Yinghai Luadbc7422013-04-15 22:23:48 -07001471 char *ck_cmdline;
Bernhard Wallecba63c32007-10-18 23:40:58 -07001472
1473 BUG_ON(!crash_size || !crash_base);
1474 *crash_size = 0;
1475 *crash_base = 0;
1476
Yinghai Luadbc7422013-04-15 22:23:48 -07001477 ck_cmdline = get_last_crashkernel(cmdline, name, suffix);
Bernhard Wallecba63c32007-10-18 23:40:58 -07001478
1479 if (!ck_cmdline)
1480 return -EINVAL;
1481
Yinghai Lu0212f912013-01-24 12:20:11 -08001482 ck_cmdline += strlen(name);
Bernhard Wallecba63c32007-10-18 23:40:58 -07001483
Yinghai Luadbc7422013-04-15 22:23:48 -07001484 if (suffix)
1485 return parse_crashkernel_suffix(ck_cmdline, crash_size,
1486 crash_base, suffix);
Bernhard Wallecba63c32007-10-18 23:40:58 -07001487 /*
1488 * if the commandline contains a ':', then that's the extended
1489 * syntax -- if not, it must be the classic syntax
1490 */
1491 first_colon = strchr(ck_cmdline, ':');
1492 first_space = strchr(ck_cmdline, ' ');
1493 if (first_colon && (!first_space || first_colon < first_space))
1494 return parse_crashkernel_mem(ck_cmdline, system_ram,
1495 crash_size, crash_base);
Bernhard Wallecba63c32007-10-18 23:40:58 -07001496
Xishi Qiu80c74f62013-09-11 14:24:47 -07001497 return parse_crashkernel_simple(ck_cmdline, crash_size, crash_base);
Bernhard Wallecba63c32007-10-18 23:40:58 -07001498}
1499
Yinghai Luadbc7422013-04-15 22:23:48 -07001500/*
1501 * That function is the entry point for command line parsing and should be
1502 * called from the arch-specific code.
1503 */
Yinghai Lu0212f912013-01-24 12:20:11 -08001504int __init parse_crashkernel(char *cmdline,
1505 unsigned long long system_ram,
1506 unsigned long long *crash_size,
1507 unsigned long long *crash_base)
1508{
1509 return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
Yinghai Luadbc7422013-04-15 22:23:48 -07001510 "crashkernel=", NULL);
Yinghai Lu0212f912013-01-24 12:20:11 -08001511}
1512
Yinghai Lu55a20ee2013-04-15 22:23:47 -07001513int __init parse_crashkernel_high(char *cmdline,
1514 unsigned long long system_ram,
1515 unsigned long long *crash_size,
1516 unsigned long long *crash_base)
1517{
1518 return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
Yinghai Luadbc7422013-04-15 22:23:48 -07001519 "crashkernel=", suffix_tbl[SUFFIX_HIGH]);
Yinghai Lu55a20ee2013-04-15 22:23:47 -07001520}
1521
Yinghai Lu0212f912013-01-24 12:20:11 -08001522int __init parse_crashkernel_low(char *cmdline,
1523 unsigned long long system_ram,
1524 unsigned long long *crash_size,
1525 unsigned long long *crash_base)
1526{
1527 return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
Yinghai Luadbc7422013-04-15 22:23:48 -07001528 "crashkernel=", suffix_tbl[SUFFIX_LOW]);
Yinghai Lu0212f912013-01-24 12:20:11 -08001529}
Bernhard Wallecba63c32007-10-18 23:40:58 -07001530
Michael Holzheufa8ff292011-10-30 15:16:41 +01001531static void update_vmcoreinfo_note(void)
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001532{
Michael Holzheufa8ff292011-10-30 15:16:41 +01001533 u32 *buf = vmcoreinfo_note;
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001534
1535 if (!vmcoreinfo_size)
1536 return;
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001537 buf = append_elf_note(buf, VMCOREINFO_NOTE_NAME, 0, vmcoreinfo_data,
1538 vmcoreinfo_size);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001539 final_note(buf);
1540}
1541
Michael Holzheufa8ff292011-10-30 15:16:41 +01001542void crash_save_vmcoreinfo(void)
1543{
Vivek Goyal63dca8d2012-07-30 14:42:36 -07001544 vmcoreinfo_append_str("CRASHTIME=%ld\n", get_seconds());
Michael Holzheufa8ff292011-10-30 15:16:41 +01001545 update_vmcoreinfo_note();
1546}
1547
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001548void vmcoreinfo_append_str(const char *fmt, ...)
1549{
1550 va_list args;
1551 char buf[0x50];
Zhang Yanfei310faaa2013-04-30 15:28:21 -07001552 size_t r;
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001553
1554 va_start(args, fmt);
Chen Ganga19428e52014-01-27 17:07:13 -08001555 r = vscnprintf(buf, sizeof(buf), fmt, args);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001556 va_end(args);
1557
Zhang Yanfei31c3a3f2013-04-30 15:28:23 -07001558 r = min(r, vmcoreinfo_max_size - vmcoreinfo_size);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001559
1560 memcpy(&vmcoreinfo_data[vmcoreinfo_size], buf, r);
1561
1562 vmcoreinfo_size += r;
1563}
1564
1565/*
1566 * provide an empty default implementation here -- architecture
1567 * code may override this
1568 */
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -07001569void __weak arch_crash_save_vmcoreinfo(void)
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001570{}
1571
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -07001572unsigned long __weak paddr_vmcoreinfo_note(void)
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001573{
1574 return __pa((unsigned long)(char *)&vmcoreinfo_note);
1575}
1576
1577static int __init crash_save_vmcoreinfo_init(void)
1578{
Ken'ichi Ohmichibba1f602008-02-07 00:15:22 -08001579 VMCOREINFO_OSRELEASE(init_uts_ns.name.release);
1580 VMCOREINFO_PAGESIZE(PAGE_SIZE);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001581
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001582 VMCOREINFO_SYMBOL(init_uts_ns);
1583 VMCOREINFO_SYMBOL(node_online_map);
Will Deacond034cfa2012-03-28 14:42:47 -07001584#ifdef CONFIG_MMU
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001585 VMCOREINFO_SYMBOL(swapper_pg_dir);
Will Deacond034cfa2012-03-28 14:42:47 -07001586#endif
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001587 VMCOREINFO_SYMBOL(_stext);
Joonsoo Kimf1c40692013-04-29 15:07:37 -07001588 VMCOREINFO_SYMBOL(vmap_area_list);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001589
1590#ifndef CONFIG_NEED_MULTIPLE_NODES
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001591 VMCOREINFO_SYMBOL(mem_map);
1592 VMCOREINFO_SYMBOL(contig_page_data);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001593#endif
1594#ifdef CONFIG_SPARSEMEM
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001595 VMCOREINFO_SYMBOL(mem_section);
1596 VMCOREINFO_LENGTH(mem_section, NR_SECTION_ROOTS);
Ken'ichi Ohmichic76f8602008-02-07 00:15:20 -08001597 VMCOREINFO_STRUCT_SIZE(mem_section);
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001598 VMCOREINFO_OFFSET(mem_section, section_mem_map);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001599#endif
Ken'ichi Ohmichic76f8602008-02-07 00:15:20 -08001600 VMCOREINFO_STRUCT_SIZE(page);
1601 VMCOREINFO_STRUCT_SIZE(pglist_data);
1602 VMCOREINFO_STRUCT_SIZE(zone);
1603 VMCOREINFO_STRUCT_SIZE(free_area);
1604 VMCOREINFO_STRUCT_SIZE(list_head);
1605 VMCOREINFO_SIZE(nodemask_t);
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001606 VMCOREINFO_OFFSET(page, flags);
1607 VMCOREINFO_OFFSET(page, _count);
1608 VMCOREINFO_OFFSET(page, mapping);
1609 VMCOREINFO_OFFSET(page, lru);
Atsushi Kumagai8d670912013-02-27 17:03:25 -08001610 VMCOREINFO_OFFSET(page, _mapcount);
1611 VMCOREINFO_OFFSET(page, private);
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001612 VMCOREINFO_OFFSET(pglist_data, node_zones);
1613 VMCOREINFO_OFFSET(pglist_data, nr_zones);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001614#ifdef CONFIG_FLAT_NODE_MEM_MAP
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001615 VMCOREINFO_OFFSET(pglist_data, node_mem_map);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001616#endif
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001617 VMCOREINFO_OFFSET(pglist_data, node_start_pfn);
1618 VMCOREINFO_OFFSET(pglist_data, node_spanned_pages);
1619 VMCOREINFO_OFFSET(pglist_data, node_id);
1620 VMCOREINFO_OFFSET(zone, free_area);
1621 VMCOREINFO_OFFSET(zone, vm_stat);
1622 VMCOREINFO_OFFSET(zone, spanned_pages);
1623 VMCOREINFO_OFFSET(free_area, free_list);
1624 VMCOREINFO_OFFSET(list_head, next);
1625 VMCOREINFO_OFFSET(list_head, prev);
Atsushi Kumagai13ba3fc2013-04-29 15:07:40 -07001626 VMCOREINFO_OFFSET(vmap_area, va_start);
1627 VMCOREINFO_OFFSET(vmap_area, list);
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001628 VMCOREINFO_LENGTH(zone.free_area, MAX_ORDER);
Neil Horman04d491a2009-04-02 16:58:57 -07001629 log_buf_kexec_setup();
Ken'ichi Ohmichi83a08e72008-01-08 15:33:05 -08001630 VMCOREINFO_LENGTH(free_area.free_list, MIGRATE_TYPES);
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001631 VMCOREINFO_NUMBER(NR_FREE_PAGES);
Ken'ichi Ohmichi122c7a52008-04-28 02:13:04 -07001632 VMCOREINFO_NUMBER(PG_lru);
1633 VMCOREINFO_NUMBER(PG_private);
1634 VMCOREINFO_NUMBER(PG_swapcache);
Atsushi Kumagai8d670912013-02-27 17:03:25 -08001635 VMCOREINFO_NUMBER(PG_slab);
Mitsuhiro Tanino0d0bf662013-02-27 17:03:27 -08001636#ifdef CONFIG_MEMORY_FAILURE
1637 VMCOREINFO_NUMBER(PG_hwpoison);
1638#endif
Petr Tesarikb3acc562014-06-23 13:22:03 -07001639 VMCOREINFO_NUMBER(PG_head_mask);
Atsushi Kumagai8d670912013-02-27 17:03:25 -08001640 VMCOREINFO_NUMBER(PAGE_BUDDY_MAPCOUNT_VALUE);
David Rientjes3a1122d2014-07-30 19:05:55 -07001641#ifdef CONFIG_HUGETLBFS
Atsushi Kumagai8f1d26d2014-07-30 16:08:39 -07001642 VMCOREINFO_SYMBOL(free_huge_page);
David Rientjes3a1122d2014-07-30 19:05:55 -07001643#endif
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001644
1645 arch_crash_save_vmcoreinfo();
Michael Holzheufa8ff292011-10-30 15:16:41 +01001646 update_vmcoreinfo_note();
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001647
1648 return 0;
1649}
1650
Paul Gortmakerc96d6662014-04-03 14:48:35 -07001651subsys_initcall(crash_save_vmcoreinfo_init);
Huang Ying3ab83522008-07-25 19:45:07 -07001652
Huang Ying7ade3fc2008-08-15 00:40:21 -07001653/*
1654 * Move into place and start executing a preloaded standalone
1655 * executable. If nothing was preloaded return an error.
Huang Ying3ab83522008-07-25 19:45:07 -07001656 */
1657int kernel_kexec(void)
1658{
1659 int error = 0;
1660
Andrew Morton8c5a1cf2008-08-15 00:40:27 -07001661 if (!mutex_trylock(&kexec_mutex))
Huang Ying3ab83522008-07-25 19:45:07 -07001662 return -EBUSY;
1663 if (!kexec_image) {
1664 error = -EINVAL;
1665 goto Unlock;
1666 }
1667
Huang Ying3ab83522008-07-25 19:45:07 -07001668#ifdef CONFIG_KEXEC_JUMP
Huang Ying7ade3fc2008-08-15 00:40:21 -07001669 if (kexec_image->preserve_context) {
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01001670 lock_system_sleep();
Huang Ying89081d12008-07-25 19:45:10 -07001671 pm_prepare_console();
1672 error = freeze_processes();
1673 if (error) {
1674 error = -EBUSY;
1675 goto Restore_console;
1676 }
1677 suspend_console();
Alan Sternd1616302009-05-24 22:05:42 +02001678 error = dpm_suspend_start(PMSG_FREEZE);
Huang Ying89081d12008-07-25 19:45:10 -07001679 if (error)
1680 goto Resume_console;
Alan Sternd1616302009-05-24 22:05:42 +02001681 /* At this point, dpm_suspend_start() has been called,
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001682 * but *not* dpm_suspend_end(). We *must* call
1683 * dpm_suspend_end() now. Otherwise, drivers for
Huang Ying89081d12008-07-25 19:45:10 -07001684 * some devices (e.g. interrupt controllers) become
1685 * desynchronized with the actual state of the
1686 * hardware at resume time, and evil weirdness ensues.
1687 */
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001688 error = dpm_suspend_end(PMSG_FREEZE);
Huang Ying89081d12008-07-25 19:45:10 -07001689 if (error)
Rafael J. Wysocki749b0af2009-03-16 22:34:35 +01001690 goto Resume_devices;
1691 error = disable_nonboot_cpus();
1692 if (error)
1693 goto Enable_cpus;
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +01001694 local_irq_disable();
Rafael J. Wysocki2e711c02011-04-26 19:15:07 +02001695 error = syscore_suspend();
Rafael J. Wysocki770824b2009-02-22 18:38:50 +01001696 if (error)
Rafael J. Wysocki749b0af2009-03-16 22:34:35 +01001697 goto Enable_irqs;
Huang Ying7ade3fc2008-08-15 00:40:21 -07001698 } else
Huang Ying3ab83522008-07-25 19:45:07 -07001699#endif
Huang Ying7ade3fc2008-08-15 00:40:21 -07001700 {
Khalid Aziz4fc9bbf2013-11-27 15:19:25 -07001701 kexec_in_progress = true;
Huang Yingca195b72008-08-15 00:40:24 -07001702 kernel_restart_prepare(NULL);
Vivek Goyalc97102b2013-12-18 17:08:31 -08001703 migrate_to_reboot_cpu();
Srivatsa S. Bhat011e4b02014-05-27 16:25:34 +05301704
1705 /*
1706 * migrate_to_reboot_cpu() disables CPU hotplug assuming that
1707 * no further code needs to use CPU hotplug (which is true in
1708 * the reboot case). However, the kexec path depends on using
1709 * CPU hotplug again; so re-enable it here.
1710 */
1711 cpu_hotplug_enable();
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001712 pr_emerg("Starting new kernel\n");
Huang Ying3ab83522008-07-25 19:45:07 -07001713 machine_shutdown();
1714 }
1715
1716 machine_kexec(kexec_image);
1717
Huang Ying3ab83522008-07-25 19:45:07 -07001718#ifdef CONFIG_KEXEC_JUMP
Huang Ying7ade3fc2008-08-15 00:40:21 -07001719 if (kexec_image->preserve_context) {
Rafael J. Wysocki19234c02011-04-20 00:36:11 +02001720 syscore_resume();
Rafael J. Wysocki749b0af2009-03-16 22:34:35 +01001721 Enable_irqs:
Huang Ying3ab83522008-07-25 19:45:07 -07001722 local_irq_enable();
Rafael J. Wysocki749b0af2009-03-16 22:34:35 +01001723 Enable_cpus:
Huang Ying89081d12008-07-25 19:45:10 -07001724 enable_nonboot_cpus();
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001725 dpm_resume_start(PMSG_RESTORE);
Huang Ying89081d12008-07-25 19:45:10 -07001726 Resume_devices:
Alan Sternd1616302009-05-24 22:05:42 +02001727 dpm_resume_end(PMSG_RESTORE);
Huang Ying89081d12008-07-25 19:45:10 -07001728 Resume_console:
1729 resume_console();
1730 thaw_processes();
1731 Restore_console:
1732 pm_restore_console();
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01001733 unlock_system_sleep();
Huang Ying3ab83522008-07-25 19:45:07 -07001734 }
Huang Ying7ade3fc2008-08-15 00:40:21 -07001735#endif
Huang Ying3ab83522008-07-25 19:45:07 -07001736
1737 Unlock:
Andrew Morton8c5a1cf2008-08-15 00:40:27 -07001738 mutex_unlock(&kexec_mutex);
Huang Ying3ab83522008-07-25 19:45:07 -07001739 return error;
1740}