blob: 0926f2a3ed03122565339310ae540ae9f0a924b9 [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
Vivek Goyalcb105252014-08-08 14:25:57 -07009#define pr_fmt(fmt) "kexec: " fmt
10
Randy.Dunlapc59ede72006-01-11 12:17:46 -080011#include <linux/capability.h>
Eric W. Biedermandc009d92005-06-25 14:57:52 -070012#include <linux/mm.h>
13#include <linux/file.h>
14#include <linux/slab.h>
15#include <linux/fs.h>
16#include <linux/kexec.h>
Andrew Morton8c5a1cf2008-08-15 00:40:27 -070017#include <linux/mutex.h>
Eric W. Biedermandc009d92005-06-25 14:57:52 -070018#include <linux/list.h>
19#include <linux/highmem.h>
20#include <linux/syscalls.h>
21#include <linux/reboot.h>
Eric W. Biedermandc009d92005-06-25 14:57:52 -070022#include <linux/ioport.h>
Alexander Nyberg6e274d12005-06-25 14:58:26 -070023#include <linux/hardirq.h>
Magnus Damm85916f82006-12-06 20:40:41 -080024#include <linux/elf.h>
25#include <linux/elfcore.h>
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -070026#include <linux/utsname.h>
27#include <linux/numa.h>
Huang Ying3ab83522008-07-25 19:45:07 -070028#include <linux/suspend.h>
29#include <linux/device.h>
Huang Ying89081d12008-07-25 19:45:10 -070030#include <linux/freezer.h>
31#include <linux/pm.h>
32#include <linux/cpu.h>
33#include <linux/console.h>
Luck, Tony5f41b8c2008-10-20 15:23:40 -070034#include <linux/vmalloc.h>
Amerigo Wang06a7f712009-12-15 16:47:46 -080035#include <linux/swap.h>
Rafael J. Wysocki19234c02011-04-20 00:36:11 +020036#include <linux/syscore_ops.h>
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -070037#include <linux/compiler.h>
Atsushi Kumagai8f1d26d2014-07-30 16:08:39 -070038#include <linux/hugetlb.h>
Alexander Nyberg6e274d12005-06-25 14:58:26 -070039
Eric W. Biedermandc009d92005-06-25 14:57:52 -070040#include <asm/page.h>
41#include <asm/uaccess.h>
42#include <asm/io.h>
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -070043#include <asm/sections.h>
Eric W. Biedermandc009d92005-06-25 14:57:52 -070044
Vivek Goyal12db5562014-08-08 14:26:04 -070045#include <crypto/hash.h>
46#include <crypto/sha.h>
47
Vivek Goyalcc571652006-01-09 20:51:41 -080048/* Per cpu memory for storing cpu states in case of system crash. */
Tejun Heo43cf38e2010-02-02 14:38:57 +090049note_buf_t __percpu *crash_notes;
Vivek Goyalcc571652006-01-09 20:51:41 -080050
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -070051/* vmcoreinfo stuff */
Dmitri Vorobievedb79a22009-04-02 16:58:58 -070052static unsigned char vmcoreinfo_data[VMCOREINFO_BYTES];
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -070053u32 vmcoreinfo_note[VMCOREINFO_NOTE_SIZE/4];
Ken'ichi Ohmichid7682812007-10-16 23:27:28 -070054size_t vmcoreinfo_size;
55size_t vmcoreinfo_max_size = sizeof(vmcoreinfo_data);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -070056
Khalid Aziz4fc9bbf2013-11-27 15:19:25 -070057/* Flag to indicate we are going to kexec a new kernel */
58bool kexec_in_progress = false;
59
Vivek Goyal12db5562014-08-08 14:26:04 -070060/*
61 * Declare these symbols weak so that if architecture provides a purgatory,
62 * these will be overridden.
63 */
64char __weak kexec_purgatory[0];
65size_t __weak kexec_purgatory_size = 0;
66
67static int kexec_calculate_store_digests(struct kimage *image);
68
Eric W. Biedermandc009d92005-06-25 14:57:52 -070069/* Location of the reserved area for the crash kernel */
70struct resource crashk_res = {
71 .name = "Crash kernel",
72 .start = 0,
73 .end = 0,
74 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
75};
Yinghai Lu0212f912013-01-24 12:20:11 -080076struct resource crashk_low_res = {
Yinghai Lu157752d2013-04-15 22:23:46 -070077 .name = "Crash kernel",
Yinghai Lu0212f912013-01-24 12:20:11 -080078 .start = 0,
79 .end = 0,
80 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
81};
Eric W. Biedermandc009d92005-06-25 14:57:52 -070082
Alexander Nyberg6e274d12005-06-25 14:58:26 -070083int kexec_should_crash(struct task_struct *p)
84{
Serge E. Hallynb460cbc2007-10-18 23:39:52 -070085 if (in_interrupt() || !p->pid || is_global_init(p) || panic_on_oops)
Alexander Nyberg6e274d12005-06-25 14:58:26 -070086 return 1;
87 return 0;
88}
89
Eric W. Biedermandc009d92005-06-25 14:57:52 -070090/*
91 * When kexec transitions to the new kernel there is a one-to-one
92 * mapping between physical and virtual addresses. On processors
93 * where you can disable the MMU this is trivial, and easy. For
94 * others it is still a simple predictable page table to setup.
95 *
96 * In that environment kexec copies the new kernel to its final
97 * resting place. This means I can only support memory whose
98 * physical address can fit in an unsigned long. In particular
99 * addresses where (pfn << PAGE_SHIFT) > ULONG_MAX cannot be handled.
100 * If the assembly stub has more restrictive requirements
101 * KEXEC_SOURCE_MEMORY_LIMIT and KEXEC_DEST_MEMORY_LIMIT can be
102 * defined more restrictively in <asm/kexec.h>.
103 *
104 * The code for the transition from the current kernel to the
105 * the new kernel is placed in the control_code_buffer, whose size
Huang Ying163f6872008-08-15 00:40:22 -0700106 * is given by KEXEC_CONTROL_PAGE_SIZE. In the best case only a single
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700107 * page of memory is necessary, but some architectures require more.
108 * Because this memory must be identity mapped in the transition from
109 * virtual to physical addresses it must live in the range
110 * 0 - TASK_SIZE, as only the user space mappings are arbitrarily
111 * modifiable.
112 *
113 * The assembly stub in the control code buffer is passed a linked list
114 * of descriptor pages detailing the source pages of the new kernel,
115 * and the destination addresses of those source pages. As this data
116 * structure is not used in the context of the current OS, it must
117 * be self-contained.
118 *
119 * The code has been made to work with highmem pages and will use a
120 * destination page in its final resting place (if it happens
121 * to allocate it). The end product of this is that most of the
122 * physical address space, and most of RAM can be used.
123 *
124 * Future directions include:
125 * - allocating a page table with the control code buffer identity
126 * mapped, to simplify machine_kexec and make kexec_on_panic more
127 * reliable.
128 */
129
130/*
131 * KIMAGE_NO_DEST is an impossible destination address..., for
132 * allocating pages whose destination address we do not care about.
133 */
134#define KIMAGE_NO_DEST (-1UL)
135
Maneesh Soni72414d32005-06-25 14:58:28 -0700136static int kimage_is_destination_range(struct kimage *image,
137 unsigned long start, unsigned long end);
138static struct page *kimage_alloc_page(struct kimage *image,
Al Viro9796fdd2005-10-21 03:22:03 -0400139 gfp_t gfp_mask,
Maneesh Soni72414d32005-06-25 14:58:28 -0700140 unsigned long dest);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700141
Vivek Goyaldabe7862014-08-08 14:25:45 -0700142static int copy_user_segment_list(struct kimage *image,
143 unsigned long nr_segments,
144 struct kexec_segment __user *segments)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700145{
Vivek Goyaldabe7862014-08-08 14:25:45 -0700146 int ret;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700147 size_t segment_bytes;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700148
149 /* Read in the segments */
150 image->nr_segments = nr_segments;
151 segment_bytes = nr_segments * sizeof(*segments);
Vivek Goyaldabe7862014-08-08 14:25:45 -0700152 ret = copy_from_user(image->segment, segments, segment_bytes);
153 if (ret)
154 ret = -EFAULT;
155
156 return ret;
157}
158
159static int sanity_check_segment_list(struct kimage *image)
160{
161 int result, i;
162 unsigned long nr_segments = image->nr_segments;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700163
164 /*
165 * Verify we have good destination addresses. The caller is
166 * responsible for making certain we don't attempt to load
167 * the new image into invalid or reserved areas of RAM. This
168 * just verifies it is an address we can use.
169 *
170 * Since the kernel does everything in page size chunks ensure
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400171 * the destination addresses are page aligned. Too many
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700172 * special cases crop of when we don't do this. The most
173 * insidious is getting overlapping destination addresses
174 * simply because addresses are changed to page size
175 * granularity.
176 */
177 result = -EADDRNOTAVAIL;
178 for (i = 0; i < nr_segments; i++) {
179 unsigned long mstart, mend;
Maneesh Soni72414d32005-06-25 14:58:28 -0700180
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700181 mstart = image->segment[i].mem;
182 mend = mstart + image->segment[i].memsz;
183 if ((mstart & ~PAGE_MASK) || (mend & ~PAGE_MASK))
Vivek Goyaldabe7862014-08-08 14:25:45 -0700184 return result;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700185 if (mend >= KEXEC_DESTINATION_MEMORY_LIMIT)
Vivek Goyaldabe7862014-08-08 14:25:45 -0700186 return result;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700187 }
188
189 /* Verify our destination addresses do not overlap.
190 * If we alloed overlapping destination addresses
191 * through very weird things can happen with no
192 * easy explanation as one segment stops on another.
193 */
194 result = -EINVAL;
Maneesh Soni72414d32005-06-25 14:58:28 -0700195 for (i = 0; i < nr_segments; i++) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700196 unsigned long mstart, mend;
197 unsigned long j;
Maneesh Soni72414d32005-06-25 14:58:28 -0700198
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700199 mstart = image->segment[i].mem;
200 mend = mstart + image->segment[i].memsz;
Maneesh Soni72414d32005-06-25 14:58:28 -0700201 for (j = 0; j < i; j++) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700202 unsigned long pstart, pend;
203 pstart = image->segment[j].mem;
204 pend = pstart + image->segment[j].memsz;
205 /* Do the segments overlap ? */
206 if ((mend > pstart) && (mstart < pend))
Vivek Goyaldabe7862014-08-08 14:25:45 -0700207 return result;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700208 }
209 }
210
211 /* Ensure our buffer sizes are strictly less than
212 * our memory sizes. This should always be the case,
213 * and it is easier to check up front than to be surprised
214 * later on.
215 */
216 result = -EINVAL;
Maneesh Soni72414d32005-06-25 14:58:28 -0700217 for (i = 0; i < nr_segments; i++) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700218 if (image->segment[i].bufsz > image->segment[i].memsz)
Vivek Goyaldabe7862014-08-08 14:25:45 -0700219 return result;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700220 }
221
Vivek Goyaldabe7862014-08-08 14:25:45 -0700222 /*
223 * Verify we have good destination addresses. Normally
224 * the caller is responsible for making certain we don't
225 * attempt to load the new image into invalid or reserved
226 * areas of RAM. But crash kernels are preloaded into a
227 * reserved area of ram. We must ensure the addresses
228 * are in the reserved area otherwise preloading the
229 * kernel could corrupt things.
230 */
Maneesh Soni72414d32005-06-25 14:58:28 -0700231
Vivek Goyaldabe7862014-08-08 14:25:45 -0700232 if (image->type == KEXEC_TYPE_CRASH) {
233 result = -EADDRNOTAVAIL;
234 for (i = 0; i < nr_segments; i++) {
235 unsigned long mstart, mend;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700236
Vivek Goyaldabe7862014-08-08 14:25:45 -0700237 mstart = image->segment[i].mem;
238 mend = mstart + image->segment[i].memsz - 1;
239 /* Ensure we are within the crash kernel limits */
240 if ((mstart < crashk_res.start) ||
241 (mend > crashk_res.end))
242 return result;
243 }
244 }
245
246 return 0;
247}
248
249static struct kimage *do_kimage_alloc_init(void)
250{
251 struct kimage *image;
252
253 /* Allocate a controlling structure */
254 image = kzalloc(sizeof(*image), GFP_KERNEL);
255 if (!image)
256 return NULL;
257
258 image->head = 0;
259 image->entry = &image->head;
260 image->last_entry = &image->head;
261 image->control_page = ~0; /* By default this does not apply */
262 image->type = KEXEC_TYPE_DEFAULT;
263
264 /* Initialize the list of control pages */
265 INIT_LIST_HEAD(&image->control_pages);
266
267 /* Initialize the list of destination pages */
268 INIT_LIST_HEAD(&image->dest_pages);
269
270 /* Initialize the list of unusable pages */
271 INIT_LIST_HEAD(&image->unusable_pages);
272
273 return image;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700274}
275
Zhang Yanfeib92e7e02013-02-27 17:03:29 -0800276static void kimage_free_page_list(struct list_head *list);
277
Vivek Goyal255aedd2014-08-08 14:25:48 -0700278static int kimage_alloc_init(struct kimage **rimage, unsigned long entry,
279 unsigned long nr_segments,
280 struct kexec_segment __user *segments,
281 unsigned long flags)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700282{
Vivek Goyal255aedd2014-08-08 14:25:48 -0700283 int ret;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700284 struct kimage *image;
Vivek Goyal255aedd2014-08-08 14:25:48 -0700285 bool kexec_on_panic = flags & KEXEC_ON_CRASH;
286
287 if (kexec_on_panic) {
288 /* Verify we have a valid entry point */
289 if ((entry < crashk_res.start) || (entry > crashk_res.end))
290 return -EADDRNOTAVAIL;
291 }
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700292
293 /* Allocate and initialize a controlling structure */
Vivek Goyaldabe7862014-08-08 14:25:45 -0700294 image = do_kimage_alloc_init();
295 if (!image)
296 return -ENOMEM;
297
298 image->start = entry;
299
Vivek Goyal255aedd2014-08-08 14:25:48 -0700300 ret = copy_user_segment_list(image, nr_segments, segments);
301 if (ret)
Vivek Goyaldabe7862014-08-08 14:25:45 -0700302 goto out_free_image;
303
Vivek Goyal255aedd2014-08-08 14:25:48 -0700304 ret = sanity_check_segment_list(image);
305 if (ret)
Vivek Goyaldabe7862014-08-08 14:25:45 -0700306 goto out_free_image;
Maneesh Soni72414d32005-06-25 14:58:28 -0700307
Vivek Goyal255aedd2014-08-08 14:25:48 -0700308 /* Enable the special crash kernel control page allocation policy. */
309 if (kexec_on_panic) {
310 image->control_page = crashk_res.start;
311 image->type = KEXEC_TYPE_CRASH;
312 }
313
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700314 /*
315 * Find a location for the control code buffer, and add it
316 * the vector of segments so that it's pages will also be
317 * counted as destination pages.
318 */
Vivek Goyal255aedd2014-08-08 14:25:48 -0700319 ret = -ENOMEM;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700320 image->control_code_page = kimage_alloc_control_pages(image,
Huang Ying163f6872008-08-15 00:40:22 -0700321 get_order(KEXEC_CONTROL_PAGE_SIZE));
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700322 if (!image->control_code_page) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -0700323 pr_err("Could not allocate control_code_buffer\n");
Vivek Goyaldabe7862014-08-08 14:25:45 -0700324 goto out_free_image;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700325 }
326
Vivek Goyal255aedd2014-08-08 14:25:48 -0700327 if (!kexec_on_panic) {
328 image->swap_page = kimage_alloc_control_pages(image, 0);
329 if (!image->swap_page) {
330 pr_err("Could not allocate swap buffer\n");
331 goto out_free_control_pages;
332 }
Huang Ying3ab83522008-07-25 19:45:07 -0700333 }
334
Zhang Yanfeib92e7e02013-02-27 17:03:29 -0800335 *rimage = image;
336 return 0;
Vivek Goyaldabe7862014-08-08 14:25:45 -0700337out_free_control_pages:
Zhang Yanfeib92e7e02013-02-27 17:03:29 -0800338 kimage_free_page_list(&image->control_pages);
Vivek Goyaldabe7862014-08-08 14:25:45 -0700339out_free_image:
Zhang Yanfeib92e7e02013-02-27 17:03:29 -0800340 kfree(image);
Vivek Goyal255aedd2014-08-08 14:25:48 -0700341 return ret;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700342}
343
Vivek Goyalcb105252014-08-08 14:25:57 -0700344static int copy_file_from_fd(int fd, void **buf, unsigned long *buf_len)
345{
346 struct fd f = fdget(fd);
347 int ret;
348 struct kstat stat;
349 loff_t pos;
350 ssize_t bytes = 0;
351
352 if (!f.file)
353 return -EBADF;
354
355 ret = vfs_getattr(&f.file->f_path, &stat);
356 if (ret)
357 goto out;
358
359 if (stat.size > INT_MAX) {
360 ret = -EFBIG;
361 goto out;
362 }
363
364 /* Don't hand 0 to vmalloc, it whines. */
365 if (stat.size == 0) {
366 ret = -EINVAL;
367 goto out;
368 }
369
370 *buf = vmalloc(stat.size);
371 if (!*buf) {
372 ret = -ENOMEM;
373 goto out;
374 }
375
376 pos = 0;
377 while (pos < stat.size) {
378 bytes = kernel_read(f.file, pos, (char *)(*buf) + pos,
379 stat.size - pos);
380 if (bytes < 0) {
381 vfree(*buf);
382 ret = bytes;
383 goto out;
384 }
385
386 if (bytes == 0)
387 break;
388 pos += bytes;
389 }
390
391 if (pos != stat.size) {
392 ret = -EBADF;
393 vfree(*buf);
394 goto out;
395 }
396
397 *buf_len = pos;
398out:
399 fdput(f);
400 return ret;
401}
402
403/* Architectures can provide this probe function */
404int __weak arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
405 unsigned long buf_len)
406{
407 return -ENOEXEC;
408}
409
410void * __weak arch_kexec_kernel_image_load(struct kimage *image)
411{
412 return ERR_PTR(-ENOEXEC);
413}
414
415void __weak arch_kimage_file_post_load_cleanup(struct kimage *image)
416{
417}
418
Vivek Goyal12db5562014-08-08 14:26:04 -0700419/* Apply relocations of type RELA */
420int __weak
421arch_kexec_apply_relocations_add(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
422 unsigned int relsec)
423{
424 pr_err("RELA relocation unsupported.\n");
425 return -ENOEXEC;
426}
427
428/* Apply relocations of type REL */
429int __weak
430arch_kexec_apply_relocations(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
431 unsigned int relsec)
432{
433 pr_err("REL relocation unsupported.\n");
434 return -ENOEXEC;
435}
436
Vivek Goyalcb105252014-08-08 14:25:57 -0700437/*
438 * Free up memory used by kernel, initrd, and comand line. This is temporary
439 * memory allocation which is not needed any more after these buffers have
440 * been loaded into separate segments and have been copied elsewhere.
441 */
442static void kimage_file_post_load_cleanup(struct kimage *image)
443{
Vivek Goyal12db5562014-08-08 14:26:04 -0700444 struct purgatory_info *pi = &image->purgatory_info;
445
Vivek Goyalcb105252014-08-08 14:25:57 -0700446 vfree(image->kernel_buf);
447 image->kernel_buf = NULL;
448
449 vfree(image->initrd_buf);
450 image->initrd_buf = NULL;
451
452 kfree(image->cmdline_buf);
453 image->cmdline_buf = NULL;
454
Vivek Goyal12db5562014-08-08 14:26:04 -0700455 vfree(pi->purgatory_buf);
456 pi->purgatory_buf = NULL;
457
458 vfree(pi->sechdrs);
459 pi->sechdrs = NULL;
460
Vivek Goyalcb105252014-08-08 14:25:57 -0700461 /* See if architecture has anything to cleanup post load */
462 arch_kimage_file_post_load_cleanup(image);
Vivek Goyal27f48d32014-08-08 14:26:06 -0700463
464 /*
465 * Above call should have called into bootloader to free up
466 * any data stored in kimage->image_loader_data. It should
467 * be ok now to free it up.
468 */
469 kfree(image->image_loader_data);
470 image->image_loader_data = NULL;
Vivek Goyalcb105252014-08-08 14:25:57 -0700471}
472
473/*
474 * In file mode list of segments is prepared by kernel. Copy relevant
475 * data from user space, do error checking, prepare segment list
476 */
477static int
478kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd,
479 const char __user *cmdline_ptr,
480 unsigned long cmdline_len, unsigned flags)
481{
482 int ret = 0;
483 void *ldata;
484
485 ret = copy_file_from_fd(kernel_fd, &image->kernel_buf,
486 &image->kernel_buf_len);
487 if (ret)
488 return ret;
489
490 /* Call arch image probe handlers */
491 ret = arch_kexec_kernel_image_probe(image, image->kernel_buf,
492 image->kernel_buf_len);
493
494 if (ret)
495 goto out;
496
497 /* It is possible that there no initramfs is being loaded */
498 if (!(flags & KEXEC_FILE_NO_INITRAMFS)) {
499 ret = copy_file_from_fd(initrd_fd, &image->initrd_buf,
500 &image->initrd_buf_len);
501 if (ret)
502 goto out;
503 }
504
505 if (cmdline_len) {
506 image->cmdline_buf = kzalloc(cmdline_len, GFP_KERNEL);
507 if (!image->cmdline_buf) {
508 ret = -ENOMEM;
509 goto out;
510 }
511
512 ret = copy_from_user(image->cmdline_buf, cmdline_ptr,
513 cmdline_len);
514 if (ret) {
515 ret = -EFAULT;
516 goto out;
517 }
518
519 image->cmdline_buf_len = cmdline_len;
520
521 /* command line should be a string with last byte null */
522 if (image->cmdline_buf[cmdline_len - 1] != '\0') {
523 ret = -EINVAL;
524 goto out;
525 }
526 }
527
528 /* Call arch image load handlers */
529 ldata = arch_kexec_kernel_image_load(image);
530
531 if (IS_ERR(ldata)) {
532 ret = PTR_ERR(ldata);
533 goto out;
534 }
535
536 image->image_loader_data = ldata;
537out:
538 /* In case of error, free up all allocated memory in this function */
539 if (ret)
540 kimage_file_post_load_cleanup(image);
541 return ret;
542}
543
544static int
545kimage_file_alloc_init(struct kimage **rimage, int kernel_fd,
546 int initrd_fd, const char __user *cmdline_ptr,
547 unsigned long cmdline_len, unsigned long flags)
548{
549 int ret;
550 struct kimage *image;
551
552 image = do_kimage_alloc_init();
553 if (!image)
554 return -ENOMEM;
555
556 image->file_mode = 1;
557
558 ret = kimage_file_prepare_segments(image, kernel_fd, initrd_fd,
559 cmdline_ptr, cmdline_len, flags);
560 if (ret)
561 goto out_free_image;
562
563 ret = sanity_check_segment_list(image);
564 if (ret)
565 goto out_free_post_load_bufs;
566
567 ret = -ENOMEM;
568 image->control_code_page = kimage_alloc_control_pages(image,
569 get_order(KEXEC_CONTROL_PAGE_SIZE));
570 if (!image->control_code_page) {
571 pr_err("Could not allocate control_code_buffer\n");
572 goto out_free_post_load_bufs;
573 }
574
575 image->swap_page = kimage_alloc_control_pages(image, 0);
576 if (!image->swap_page) {
577 pr_err(KERN_ERR "Could not allocate swap buffer\n");
578 goto out_free_control_pages;
579 }
580
581 *rimage = image;
582 return 0;
583out_free_control_pages:
584 kimage_free_page_list(&image->control_pages);
585out_free_post_load_bufs:
586 kimage_file_post_load_cleanup(image);
Vivek Goyalcb105252014-08-08 14:25:57 -0700587out_free_image:
588 kfree(image);
589 return ret;
590}
591
Maneesh Soni72414d32005-06-25 14:58:28 -0700592static int kimage_is_destination_range(struct kimage *image,
593 unsigned long start,
594 unsigned long end)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700595{
596 unsigned long i;
597
598 for (i = 0; i < image->nr_segments; i++) {
599 unsigned long mstart, mend;
Maneesh Soni72414d32005-06-25 14:58:28 -0700600
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700601 mstart = image->segment[i].mem;
Maneesh Soni72414d32005-06-25 14:58:28 -0700602 mend = mstart + image->segment[i].memsz;
603 if ((end > mstart) && (start < mend))
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700604 return 1;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700605 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700606
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700607 return 0;
608}
609
Al Viro9796fdd2005-10-21 03:22:03 -0400610static struct page *kimage_alloc_pages(gfp_t gfp_mask, unsigned int order)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700611{
612 struct page *pages;
Maneesh Soni72414d32005-06-25 14:58:28 -0700613
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700614 pages = alloc_pages(gfp_mask, order);
615 if (pages) {
616 unsigned int count, i;
617 pages->mapping = NULL;
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700618 set_page_private(pages, order);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700619 count = 1 << order;
Maneesh Soni72414d32005-06-25 14:58:28 -0700620 for (i = 0; i < count; i++)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700621 SetPageReserved(pages + i);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700622 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700623
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700624 return pages;
625}
626
627static void kimage_free_pages(struct page *page)
628{
629 unsigned int order, count, i;
Maneesh Soni72414d32005-06-25 14:58:28 -0700630
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700631 order = page_private(page);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700632 count = 1 << order;
Maneesh Soni72414d32005-06-25 14:58:28 -0700633 for (i = 0; i < count; i++)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700634 ClearPageReserved(page + i);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700635 __free_pages(page, order);
636}
637
638static void kimage_free_page_list(struct list_head *list)
639{
640 struct list_head *pos, *next;
Maneesh Soni72414d32005-06-25 14:58:28 -0700641
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700642 list_for_each_safe(pos, next, list) {
643 struct page *page;
644
645 page = list_entry(pos, struct page, lru);
646 list_del(&page->lru);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700647 kimage_free_pages(page);
648 }
649}
650
Maneesh Soni72414d32005-06-25 14:58:28 -0700651static struct page *kimage_alloc_normal_control_pages(struct kimage *image,
652 unsigned int order)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700653{
654 /* Control pages are special, they are the intermediaries
655 * that are needed while we copy the rest of the pages
656 * to their final resting place. As such they must
657 * not conflict with either the destination addresses
658 * or memory the kernel is already using.
659 *
660 * The only case where we really need more than one of
661 * these are for architectures where we cannot disable
662 * the MMU and must instead generate an identity mapped
663 * page table for all of the memory.
664 *
665 * At worst this runs in O(N) of the image size.
666 */
667 struct list_head extra_pages;
668 struct page *pages;
669 unsigned int count;
670
671 count = 1 << order;
672 INIT_LIST_HEAD(&extra_pages);
673
674 /* Loop while I can allocate a page and the page allocated
675 * is a destination page.
676 */
677 do {
678 unsigned long pfn, epfn, addr, eaddr;
Maneesh Soni72414d32005-06-25 14:58:28 -0700679
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700680 pages = kimage_alloc_pages(GFP_KERNEL, order);
681 if (!pages)
682 break;
683 pfn = page_to_pfn(pages);
684 epfn = pfn + count;
685 addr = pfn << PAGE_SHIFT;
686 eaddr = epfn << PAGE_SHIFT;
687 if ((epfn >= (KEXEC_CONTROL_MEMORY_LIMIT >> PAGE_SHIFT)) ||
Maneesh Soni72414d32005-06-25 14:58:28 -0700688 kimage_is_destination_range(image, addr, eaddr)) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700689 list_add(&pages->lru, &extra_pages);
690 pages = NULL;
691 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700692 } while (!pages);
693
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700694 if (pages) {
695 /* Remember the allocated page... */
696 list_add(&pages->lru, &image->control_pages);
697
698 /* Because the page is already in it's destination
699 * location we will never allocate another page at
700 * that address. Therefore kimage_alloc_pages
701 * will not return it (again) and we don't need
702 * to give it an entry in image->segment[].
703 */
704 }
705 /* Deal with the destination pages I have inadvertently allocated.
706 *
707 * Ideally I would convert multi-page allocations into single
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300708 * page allocations, and add everything to image->dest_pages.
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700709 *
710 * For now it is simpler to just free the pages.
711 */
712 kimage_free_page_list(&extra_pages);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700713
Maneesh Soni72414d32005-06-25 14:58:28 -0700714 return pages;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700715}
716
Maneesh Soni72414d32005-06-25 14:58:28 -0700717static struct page *kimage_alloc_crash_control_pages(struct kimage *image,
718 unsigned int order)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700719{
720 /* Control pages are special, they are the intermediaries
721 * that are needed while we copy the rest of the pages
722 * to their final resting place. As such they must
723 * not conflict with either the destination addresses
724 * or memory the kernel is already using.
725 *
726 * Control pages are also the only pags we must allocate
727 * when loading a crash kernel. All of the other pages
728 * are specified by the segments and we just memcpy
729 * into them directly.
730 *
731 * The only case where we really need more than one of
732 * these are for architectures where we cannot disable
733 * the MMU and must instead generate an identity mapped
734 * page table for all of the memory.
735 *
736 * Given the low demand this implements a very simple
737 * allocator that finds the first hole of the appropriate
738 * size in the reserved memory region, and allocates all
739 * of the memory up to and including the hole.
740 */
741 unsigned long hole_start, hole_end, size;
742 struct page *pages;
Maneesh Soni72414d32005-06-25 14:58:28 -0700743
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700744 pages = NULL;
745 size = (1 << order) << PAGE_SHIFT;
746 hole_start = (image->control_page + (size - 1)) & ~(size - 1);
747 hole_end = hole_start + size - 1;
Maneesh Soni72414d32005-06-25 14:58:28 -0700748 while (hole_end <= crashk_res.end) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700749 unsigned long i;
Maneesh Soni72414d32005-06-25 14:58:28 -0700750
Michael Holzheu3d214fa2011-10-30 15:16:36 +0100751 if (hole_end > KEXEC_CRASH_CONTROL_MEMORY_LIMIT)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700752 break;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700753 /* See if I overlap any of the segments */
Maneesh Soni72414d32005-06-25 14:58:28 -0700754 for (i = 0; i < image->nr_segments; i++) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700755 unsigned long mstart, mend;
Maneesh Soni72414d32005-06-25 14:58:28 -0700756
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700757 mstart = image->segment[i].mem;
758 mend = mstart + image->segment[i].memsz - 1;
759 if ((hole_end >= mstart) && (hole_start <= mend)) {
760 /* Advance the hole to the end of the segment */
761 hole_start = (mend + (size - 1)) & ~(size - 1);
762 hole_end = hole_start + size - 1;
763 break;
764 }
765 }
766 /* If I don't overlap any segments I have found my hole! */
767 if (i == image->nr_segments) {
768 pages = pfn_to_page(hole_start >> PAGE_SHIFT);
769 break;
770 }
771 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700772 if (pages)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700773 image->control_page = hole_end;
Maneesh Soni72414d32005-06-25 14:58:28 -0700774
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700775 return pages;
776}
777
778
Maneesh Soni72414d32005-06-25 14:58:28 -0700779struct page *kimage_alloc_control_pages(struct kimage *image,
780 unsigned int order)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700781{
782 struct page *pages = NULL;
Maneesh Soni72414d32005-06-25 14:58:28 -0700783
784 switch (image->type) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700785 case KEXEC_TYPE_DEFAULT:
786 pages = kimage_alloc_normal_control_pages(image, order);
787 break;
788 case KEXEC_TYPE_CRASH:
789 pages = kimage_alloc_crash_control_pages(image, order);
790 break;
791 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700792
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700793 return pages;
794}
795
796static int kimage_add_entry(struct kimage *image, kimage_entry_t entry)
797{
Maneesh Soni72414d32005-06-25 14:58:28 -0700798 if (*image->entry != 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700799 image->entry++;
Maneesh Soni72414d32005-06-25 14:58:28 -0700800
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700801 if (image->entry == image->last_entry) {
802 kimage_entry_t *ind_page;
803 struct page *page;
Maneesh Soni72414d32005-06-25 14:58:28 -0700804
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700805 page = kimage_alloc_page(image, GFP_KERNEL, KIMAGE_NO_DEST);
Maneesh Soni72414d32005-06-25 14:58:28 -0700806 if (!page)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700807 return -ENOMEM;
Maneesh Soni72414d32005-06-25 14:58:28 -0700808
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700809 ind_page = page_address(page);
810 *image->entry = virt_to_phys(ind_page) | IND_INDIRECTION;
811 image->entry = ind_page;
Maneesh Soni72414d32005-06-25 14:58:28 -0700812 image->last_entry = ind_page +
813 ((PAGE_SIZE/sizeof(kimage_entry_t)) - 1);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700814 }
815 *image->entry = entry;
816 image->entry++;
817 *image->entry = 0;
Maneesh Soni72414d32005-06-25 14:58:28 -0700818
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700819 return 0;
820}
821
Maneesh Soni72414d32005-06-25 14:58:28 -0700822static int kimage_set_destination(struct kimage *image,
823 unsigned long destination)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700824{
825 int result;
826
827 destination &= PAGE_MASK;
828 result = kimage_add_entry(image, destination | IND_DESTINATION);
Maneesh Soni72414d32005-06-25 14:58:28 -0700829 if (result == 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700830 image->destination = destination;
Maneesh Soni72414d32005-06-25 14:58:28 -0700831
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700832 return result;
833}
834
835
836static int kimage_add_page(struct kimage *image, unsigned long page)
837{
838 int result;
839
840 page &= PAGE_MASK;
841 result = kimage_add_entry(image, page | IND_SOURCE);
Maneesh Soni72414d32005-06-25 14:58:28 -0700842 if (result == 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700843 image->destination += PAGE_SIZE;
Maneesh Soni72414d32005-06-25 14:58:28 -0700844
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700845 return result;
846}
847
848
849static void kimage_free_extra_pages(struct kimage *image)
850{
851 /* Walk through and free any extra destination pages I may have */
852 kimage_free_page_list(&image->dest_pages);
853
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300854 /* Walk through and free any unusable pages I have cached */
Vivek Goyal7d3e2bc2014-08-08 14:25:43 -0700855 kimage_free_page_list(&image->unusable_pages);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700856
857}
WANG Cong7fccf032008-07-25 19:45:02 -0700858static void kimage_terminate(struct kimage *image)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700859{
Maneesh Soni72414d32005-06-25 14:58:28 -0700860 if (*image->entry != 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700861 image->entry++;
Maneesh Soni72414d32005-06-25 14:58:28 -0700862
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700863 *image->entry = IND_DONE;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700864}
865
866#define for_each_kimage_entry(image, ptr, entry) \
867 for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE); \
Fabian Fredericke1bebcf2014-06-06 14:37:09 -0700868 ptr = (entry & IND_INDIRECTION) ? \
869 phys_to_virt((entry & PAGE_MASK)) : ptr + 1)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700870
871static void kimage_free_entry(kimage_entry_t entry)
872{
873 struct page *page;
874
875 page = pfn_to_page(entry >> PAGE_SHIFT);
876 kimage_free_pages(page);
877}
878
879static void kimage_free(struct kimage *image)
880{
881 kimage_entry_t *ptr, entry;
882 kimage_entry_t ind = 0;
883
884 if (!image)
885 return;
Maneesh Soni72414d32005-06-25 14:58:28 -0700886
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700887 kimage_free_extra_pages(image);
888 for_each_kimage_entry(image, ptr, entry) {
889 if (entry & IND_INDIRECTION) {
890 /* Free the previous indirection page */
Maneesh Soni72414d32005-06-25 14:58:28 -0700891 if (ind & IND_INDIRECTION)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700892 kimage_free_entry(ind);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700893 /* Save this indirection page until we are
894 * done with it.
895 */
896 ind = entry;
Fabian Fredericke1bebcf2014-06-06 14:37:09 -0700897 } else if (entry & IND_SOURCE)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700898 kimage_free_entry(entry);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700899 }
900 /* Free the final indirection page */
Maneesh Soni72414d32005-06-25 14:58:28 -0700901 if (ind & IND_INDIRECTION)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700902 kimage_free_entry(ind);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700903
904 /* Handle any machine specific cleanup */
905 machine_kexec_cleanup(image);
906
907 /* Free the kexec control pages... */
908 kimage_free_page_list(&image->control_pages);
Vivek Goyalcb105252014-08-08 14:25:57 -0700909
Vivek Goyalcb105252014-08-08 14:25:57 -0700910 /*
911 * Free up any temporary buffers allocated. This might hit if
912 * error occurred much later after buffer allocation.
913 */
914 if (image->file_mode)
915 kimage_file_post_load_cleanup(image);
916
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700917 kfree(image);
918}
919
Maneesh Soni72414d32005-06-25 14:58:28 -0700920static kimage_entry_t *kimage_dst_used(struct kimage *image,
921 unsigned long page)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700922{
923 kimage_entry_t *ptr, entry;
924 unsigned long destination = 0;
925
926 for_each_kimage_entry(image, ptr, entry) {
Maneesh Soni72414d32005-06-25 14:58:28 -0700927 if (entry & IND_DESTINATION)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700928 destination = entry & PAGE_MASK;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700929 else if (entry & IND_SOURCE) {
Maneesh Soni72414d32005-06-25 14:58:28 -0700930 if (page == destination)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700931 return ptr;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700932 destination += PAGE_SIZE;
933 }
934 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700935
Alexey Dobriyan314b6a42005-06-27 22:29:33 -0700936 return NULL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700937}
938
Maneesh Soni72414d32005-06-25 14:58:28 -0700939static struct page *kimage_alloc_page(struct kimage *image,
Al Viro9796fdd2005-10-21 03:22:03 -0400940 gfp_t gfp_mask,
Maneesh Soni72414d32005-06-25 14:58:28 -0700941 unsigned long destination)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700942{
943 /*
944 * Here we implement safeguards to ensure that a source page
945 * is not copied to its destination page before the data on
946 * the destination page is no longer useful.
947 *
948 * To do this we maintain the invariant that a source page is
949 * either its own destination page, or it is not a
950 * destination page at all.
951 *
952 * That is slightly stronger than required, but the proof
953 * that no problems will not occur is trivial, and the
954 * implementation is simply to verify.
955 *
956 * When allocating all pages normally this algorithm will run
957 * in O(N) time, but in the worst case it will run in O(N^2)
958 * time. If the runtime is a problem the data structures can
959 * be fixed.
960 */
961 struct page *page;
962 unsigned long addr;
963
964 /*
965 * Walk through the list of destination pages, and see if I
966 * have a match.
967 */
968 list_for_each_entry(page, &image->dest_pages, lru) {
969 addr = page_to_pfn(page) << PAGE_SHIFT;
970 if (addr == destination) {
971 list_del(&page->lru);
972 return page;
973 }
974 }
975 page = NULL;
976 while (1) {
977 kimage_entry_t *old;
978
979 /* Allocate a page, if we run out of memory give up */
980 page = kimage_alloc_pages(gfp_mask, 0);
Maneesh Soni72414d32005-06-25 14:58:28 -0700981 if (!page)
Alexey Dobriyan314b6a42005-06-27 22:29:33 -0700982 return NULL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700983 /* If the page cannot be used file it away */
Maneesh Soni72414d32005-06-25 14:58:28 -0700984 if (page_to_pfn(page) >
985 (KEXEC_SOURCE_MEMORY_LIMIT >> PAGE_SHIFT)) {
Vivek Goyal7d3e2bc2014-08-08 14:25:43 -0700986 list_add(&page->lru, &image->unusable_pages);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700987 continue;
988 }
989 addr = page_to_pfn(page) << PAGE_SHIFT;
990
991 /* If it is the destination page we want use it */
992 if (addr == destination)
993 break;
994
995 /* If the page is not a destination page use it */
Maneesh Soni72414d32005-06-25 14:58:28 -0700996 if (!kimage_is_destination_range(image, addr,
997 addr + PAGE_SIZE))
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700998 break;
999
1000 /*
1001 * I know that the page is someones destination page.
1002 * See if there is already a source page for this
1003 * destination page. And if so swap the source pages.
1004 */
1005 old = kimage_dst_used(image, addr);
1006 if (old) {
1007 /* If so move it */
1008 unsigned long old_addr;
1009 struct page *old_page;
1010
1011 old_addr = *old & PAGE_MASK;
1012 old_page = pfn_to_page(old_addr >> PAGE_SHIFT);
1013 copy_highpage(page, old_page);
1014 *old = addr | (*old & ~PAGE_MASK);
1015
1016 /* The old page I have found cannot be a
Jonathan Steelf9092f32008-09-22 13:57:45 -07001017 * destination page, so return it if it's
1018 * gfp_flags honor the ones passed in.
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001019 */
Jonathan Steelf9092f32008-09-22 13:57:45 -07001020 if (!(gfp_mask & __GFP_HIGHMEM) &&
1021 PageHighMem(old_page)) {
1022 kimage_free_pages(old_page);
1023 continue;
1024 }
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001025 addr = old_addr;
1026 page = old_page;
1027 break;
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001028 } else {
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001029 /* Place the page on the destination list I
1030 * will use it later.
1031 */
1032 list_add(&page->lru, &image->dest_pages);
1033 }
1034 }
Maneesh Soni72414d32005-06-25 14:58:28 -07001035
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001036 return page;
1037}
1038
1039static int kimage_load_normal_segment(struct kimage *image,
Maneesh Soni72414d32005-06-25 14:58:28 -07001040 struct kexec_segment *segment)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001041{
1042 unsigned long maddr;
Zhang Yanfei310faaa2013-04-30 15:28:21 -07001043 size_t ubytes, mbytes;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001044 int result;
Vivek Goyalcb105252014-08-08 14:25:57 -07001045 unsigned char __user *buf = NULL;
1046 unsigned char *kbuf = NULL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001047
1048 result = 0;
Vivek Goyalcb105252014-08-08 14:25:57 -07001049 if (image->file_mode)
1050 kbuf = segment->kbuf;
1051 else
1052 buf = segment->buf;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001053 ubytes = segment->bufsz;
1054 mbytes = segment->memsz;
1055 maddr = segment->mem;
1056
1057 result = kimage_set_destination(image, maddr);
Maneesh Soni72414d32005-06-25 14:58:28 -07001058 if (result < 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001059 goto out;
Maneesh Soni72414d32005-06-25 14:58:28 -07001060
1061 while (mbytes) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001062 struct page *page;
1063 char *ptr;
1064 size_t uchunk, mchunk;
Maneesh Soni72414d32005-06-25 14:58:28 -07001065
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001066 page = kimage_alloc_page(image, GFP_HIGHUSER, maddr);
Stephen Hemmingerc80544d2007-10-18 03:07:05 -07001067 if (!page) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001068 result = -ENOMEM;
1069 goto out;
1070 }
Maneesh Soni72414d32005-06-25 14:58:28 -07001071 result = kimage_add_page(image, page_to_pfn(page)
1072 << PAGE_SHIFT);
1073 if (result < 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001074 goto out;
Maneesh Soni72414d32005-06-25 14:58:28 -07001075
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001076 ptr = kmap(page);
1077 /* Start with a clear page */
Jan Beulich3ecb01d2010-10-26 14:22:27 -07001078 clear_page(ptr);
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001079 ptr += maddr & ~PAGE_MASK;
Zhang Yanfei31c3a3f2013-04-30 15:28:23 -07001080 mchunk = min_t(size_t, mbytes,
1081 PAGE_SIZE - (maddr & ~PAGE_MASK));
1082 uchunk = min(ubytes, mchunk);
Maneesh Soni72414d32005-06-25 14:58:28 -07001083
Vivek Goyalcb105252014-08-08 14:25:57 -07001084 /* For file based kexec, source pages are in kernel memory */
1085 if (image->file_mode)
1086 memcpy(ptr, kbuf, uchunk);
1087 else
1088 result = copy_from_user(ptr, buf, uchunk);
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001089 kunmap(page);
1090 if (result) {
Dan Carpenterf65a03f2010-08-10 18:03:31 -07001091 result = -EFAULT;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001092 goto out;
1093 }
1094 ubytes -= uchunk;
1095 maddr += mchunk;
Vivek Goyalcb105252014-08-08 14:25:57 -07001096 if (image->file_mode)
1097 kbuf += mchunk;
1098 else
1099 buf += mchunk;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001100 mbytes -= mchunk;
1101 }
Maneesh Soni72414d32005-06-25 14:58:28 -07001102out:
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001103 return result;
1104}
1105
1106static int kimage_load_crash_segment(struct kimage *image,
Maneesh Soni72414d32005-06-25 14:58:28 -07001107 struct kexec_segment *segment)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001108{
1109 /* For crash dumps kernels we simply copy the data from
1110 * user space to it's destination.
1111 * We do things a page at a time for the sake of kmap.
1112 */
1113 unsigned long maddr;
Zhang Yanfei310faaa2013-04-30 15:28:21 -07001114 size_t ubytes, mbytes;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001115 int result;
Alexey Dobriyan314b6a42005-06-27 22:29:33 -07001116 unsigned char __user *buf;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001117
1118 result = 0;
1119 buf = segment->buf;
1120 ubytes = segment->bufsz;
1121 mbytes = segment->memsz;
1122 maddr = segment->mem;
Maneesh Soni72414d32005-06-25 14:58:28 -07001123 while (mbytes) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001124 struct page *page;
1125 char *ptr;
1126 size_t uchunk, mchunk;
Maneesh Soni72414d32005-06-25 14:58:28 -07001127
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001128 page = pfn_to_page(maddr >> PAGE_SHIFT);
Stephen Hemmingerc80544d2007-10-18 03:07:05 -07001129 if (!page) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001130 result = -ENOMEM;
1131 goto out;
1132 }
1133 ptr = kmap(page);
1134 ptr += maddr & ~PAGE_MASK;
Zhang Yanfei31c3a3f2013-04-30 15:28:23 -07001135 mchunk = min_t(size_t, mbytes,
1136 PAGE_SIZE - (maddr & ~PAGE_MASK));
1137 uchunk = min(ubytes, mchunk);
1138 if (mchunk > uchunk) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001139 /* Zero the trailing part of the page */
1140 memset(ptr + uchunk, 0, mchunk - uchunk);
1141 }
1142 result = copy_from_user(ptr, buf, uchunk);
Zou Nan haia79561132006-12-07 09:51:35 -08001143 kexec_flush_icache_page(page);
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001144 kunmap(page);
1145 if (result) {
Dan Carpenterf65a03f2010-08-10 18:03:31 -07001146 result = -EFAULT;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001147 goto out;
1148 }
1149 ubytes -= uchunk;
1150 maddr += mchunk;
Vivek Goyal12db5562014-08-08 14:26:04 -07001151 buf += mchunk;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001152 mbytes -= mchunk;
1153 }
Maneesh Soni72414d32005-06-25 14:58:28 -07001154out:
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001155 return result;
1156}
1157
1158static int kimage_load_segment(struct kimage *image,
Maneesh Soni72414d32005-06-25 14:58:28 -07001159 struct kexec_segment *segment)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001160{
1161 int result = -ENOMEM;
Maneesh Soni72414d32005-06-25 14:58:28 -07001162
1163 switch (image->type) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001164 case KEXEC_TYPE_DEFAULT:
1165 result = kimage_load_normal_segment(image, segment);
1166 break;
1167 case KEXEC_TYPE_CRASH:
1168 result = kimage_load_crash_segment(image, segment);
1169 break;
1170 }
Maneesh Soni72414d32005-06-25 14:58:28 -07001171
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001172 return result;
1173}
1174
1175/*
1176 * Exec Kernel system call: for obvious reasons only root may call it.
1177 *
1178 * This call breaks up into three pieces.
1179 * - A generic part which loads the new kernel from the current
1180 * address space, and very carefully places the data in the
1181 * allocated pages.
1182 *
1183 * - A generic part that interacts with the kernel and tells all of
1184 * the devices to shut down. Preventing on-going dmas, and placing
1185 * the devices in a consistent state so a later kernel can
1186 * reinitialize them.
1187 *
1188 * - A machine specific part that includes the syscall number
Geert Uytterhoeven002ace72013-09-15 11:35:37 +02001189 * and then copies the image to it's final destination. And
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001190 * jumps into the image at entry.
1191 *
1192 * kexec does not sync, or unmount filesystems so if you need
1193 * that to happen you need to do that yourself.
1194 */
Jeff Moyerc330dda2006-06-23 02:05:07 -07001195struct kimage *kexec_image;
1196struct kimage *kexec_crash_image;
Kees Cook79847542014-01-23 15:55:59 -08001197int kexec_load_disabled;
Andrew Morton8c5a1cf2008-08-15 00:40:27 -07001198
1199static DEFINE_MUTEX(kexec_mutex);
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001200
Heiko Carstens754fe8d2009-01-14 14:14:09 +01001201SYSCALL_DEFINE4(kexec_load, unsigned long, entry, unsigned long, nr_segments,
1202 struct kexec_segment __user *, segments, unsigned long, flags)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001203{
1204 struct kimage **dest_image, *image;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001205 int result;
1206
1207 /* We only trust the superuser with rebooting the system. */
Kees Cook79847542014-01-23 15:55:59 -08001208 if (!capable(CAP_SYS_BOOT) || kexec_load_disabled)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001209 return -EPERM;
1210
1211 /*
1212 * Verify we have a legal set of flags
1213 * This leaves us room for future extensions.
1214 */
1215 if ((flags & KEXEC_FLAGS) != (flags & ~KEXEC_ARCH_MASK))
1216 return -EINVAL;
1217
1218 /* Verify we are on the appropriate architecture */
1219 if (((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH) &&
1220 ((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH_DEFAULT))
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001221 return -EINVAL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001222
1223 /* Put an artificial cap on the number
1224 * of segments passed to kexec_load.
1225 */
1226 if (nr_segments > KEXEC_SEGMENT_MAX)
1227 return -EINVAL;
1228
1229 image = NULL;
1230 result = 0;
1231
1232 /* Because we write directly to the reserved memory
1233 * region when loading crash kernels we need a mutex here to
1234 * prevent multiple crash kernels from attempting to load
1235 * simultaneously, and to prevent a crash kernel from loading
1236 * over the top of a in use crash kernel.
1237 *
1238 * KISS: always take the mutex.
1239 */
Andrew Morton8c5a1cf2008-08-15 00:40:27 -07001240 if (!mutex_trylock(&kexec_mutex))
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001241 return -EBUSY;
Maneesh Soni72414d32005-06-25 14:58:28 -07001242
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001243 dest_image = &kexec_image;
Maneesh Soni72414d32005-06-25 14:58:28 -07001244 if (flags & KEXEC_ON_CRASH)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001245 dest_image = &kexec_crash_image;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001246 if (nr_segments > 0) {
1247 unsigned long i;
Maneesh Soni72414d32005-06-25 14:58:28 -07001248
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001249 /* Loading another kernel to reboot into */
Maneesh Soni72414d32005-06-25 14:58:28 -07001250 if ((flags & KEXEC_ON_CRASH) == 0)
Vivek Goyal255aedd2014-08-08 14:25:48 -07001251 result = kimage_alloc_init(&image, entry, nr_segments,
1252 segments, flags);
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001253 /* Loading another kernel to switch to if this one crashes */
1254 else if (flags & KEXEC_ON_CRASH) {
1255 /* Free any current crash dump kernel before
1256 * we corrupt it.
1257 */
1258 kimage_free(xchg(&kexec_crash_image, NULL));
Vivek Goyal255aedd2014-08-08 14:25:48 -07001259 result = kimage_alloc_init(&image, entry, nr_segments,
1260 segments, flags);
Michael Holzheu558df722011-10-30 15:16:43 +01001261 crash_map_reserved_pages();
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001262 }
Maneesh Soni72414d32005-06-25 14:58:28 -07001263 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001264 goto out;
Maneesh Soni72414d32005-06-25 14:58:28 -07001265
Huang Ying3ab83522008-07-25 19:45:07 -07001266 if (flags & KEXEC_PRESERVE_CONTEXT)
1267 image->preserve_context = 1;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001268 result = machine_kexec_prepare(image);
Maneesh Soni72414d32005-06-25 14:58:28 -07001269 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001270 goto out;
Maneesh Soni72414d32005-06-25 14:58:28 -07001271
1272 for (i = 0; i < nr_segments; i++) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001273 result = kimage_load_segment(image, &image->segment[i]);
Maneesh Soni72414d32005-06-25 14:58:28 -07001274 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001275 goto out;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001276 }
WANG Cong7fccf032008-07-25 19:45:02 -07001277 kimage_terminate(image);
Michael Holzheu558df722011-10-30 15:16:43 +01001278 if (flags & KEXEC_ON_CRASH)
1279 crash_unmap_reserved_pages();
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001280 }
1281 /* Install the new kernel, and Uninstall the old */
1282 image = xchg(dest_image, image);
1283
Maneesh Soni72414d32005-06-25 14:58:28 -07001284out:
Andrew Morton8c5a1cf2008-08-15 00:40:27 -07001285 mutex_unlock(&kexec_mutex);
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001286 kimage_free(image);
Maneesh Soni72414d32005-06-25 14:58:28 -07001287
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001288 return result;
1289}
1290
Michael Holzheu558df722011-10-30 15:16:43 +01001291/*
1292 * Add and remove page tables for crashkernel memory
1293 *
1294 * Provide an empty default implementation here -- architecture
1295 * code may override this
1296 */
1297void __weak crash_map_reserved_pages(void)
1298{}
1299
1300void __weak crash_unmap_reserved_pages(void)
1301{}
1302
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001303#ifdef CONFIG_COMPAT
Heiko Carstensca2c4052014-03-04 17:13:42 +01001304COMPAT_SYSCALL_DEFINE4(kexec_load, compat_ulong_t, entry,
1305 compat_ulong_t, nr_segments,
1306 struct compat_kexec_segment __user *, segments,
1307 compat_ulong_t, flags)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001308{
1309 struct compat_kexec_segment in;
1310 struct kexec_segment out, __user *ksegments;
1311 unsigned long i, result;
1312
1313 /* Don't allow clients that don't understand the native
1314 * architecture to do anything.
1315 */
Maneesh Soni72414d32005-06-25 14:58:28 -07001316 if ((flags & KEXEC_ARCH_MASK) == KEXEC_ARCH_DEFAULT)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001317 return -EINVAL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001318
Maneesh Soni72414d32005-06-25 14:58:28 -07001319 if (nr_segments > KEXEC_SEGMENT_MAX)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001320 return -EINVAL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001321
1322 ksegments = compat_alloc_user_space(nr_segments * sizeof(out));
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001323 for (i = 0; i < nr_segments; i++) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001324 result = copy_from_user(&in, &segments[i], sizeof(in));
Maneesh Soni72414d32005-06-25 14:58:28 -07001325 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001326 return -EFAULT;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001327
1328 out.buf = compat_ptr(in.buf);
1329 out.bufsz = in.bufsz;
1330 out.mem = in.mem;
1331 out.memsz = in.memsz;
1332
1333 result = copy_to_user(&ksegments[i], &out, sizeof(out));
Maneesh Soni72414d32005-06-25 14:58:28 -07001334 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001335 return -EFAULT;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001336 }
1337
1338 return sys_kexec_load(entry, nr_segments, ksegments, flags);
1339}
1340#endif
1341
Vivek Goyalf0895682014-08-08 14:25:55 -07001342SYSCALL_DEFINE5(kexec_file_load, int, kernel_fd, int, initrd_fd,
1343 unsigned long, cmdline_len, const char __user *, cmdline_ptr,
1344 unsigned long, flags)
1345{
Vivek Goyalcb105252014-08-08 14:25:57 -07001346 int ret = 0, i;
1347 struct kimage **dest_image, *image;
1348
1349 /* We only trust the superuser with rebooting the system. */
1350 if (!capable(CAP_SYS_BOOT) || kexec_load_disabled)
1351 return -EPERM;
1352
1353 /* Make sure we have a legal set of flags */
1354 if (flags != (flags & KEXEC_FILE_FLAGS))
1355 return -EINVAL;
1356
1357 image = NULL;
1358
1359 if (!mutex_trylock(&kexec_mutex))
1360 return -EBUSY;
1361
1362 dest_image = &kexec_image;
1363 if (flags & KEXEC_FILE_ON_CRASH)
1364 dest_image = &kexec_crash_image;
1365
1366 if (flags & KEXEC_FILE_UNLOAD)
1367 goto exchange;
1368
1369 /*
1370 * In case of crash, new kernel gets loaded in reserved region. It is
1371 * same memory where old crash kernel might be loaded. Free any
1372 * current crash dump kernel before we corrupt it.
1373 */
1374 if (flags & KEXEC_FILE_ON_CRASH)
1375 kimage_free(xchg(&kexec_crash_image, NULL));
1376
1377 ret = kimage_file_alloc_init(&image, kernel_fd, initrd_fd, cmdline_ptr,
1378 cmdline_len, flags);
1379 if (ret)
1380 goto out;
1381
1382 ret = machine_kexec_prepare(image);
1383 if (ret)
1384 goto out;
1385
Vivek Goyal12db5562014-08-08 14:26:04 -07001386 ret = kexec_calculate_store_digests(image);
1387 if (ret)
1388 goto out;
1389
Vivek Goyalcb105252014-08-08 14:25:57 -07001390 for (i = 0; i < image->nr_segments; i++) {
1391 struct kexec_segment *ksegment;
1392
1393 ksegment = &image->segment[i];
1394 pr_debug("Loading segment %d: buf=0x%p bufsz=0x%zx mem=0x%lx memsz=0x%zx\n",
1395 i, ksegment->buf, ksegment->bufsz, ksegment->mem,
1396 ksegment->memsz);
1397
1398 ret = kimage_load_segment(image, &image->segment[i]);
1399 if (ret)
1400 goto out;
1401 }
1402
1403 kimage_terminate(image);
1404
1405 /*
1406 * Free up any temporary buffers allocated which are not needed
1407 * after image has been loaded
1408 */
1409 kimage_file_post_load_cleanup(image);
1410exchange:
1411 image = xchg(dest_image, image);
1412out:
1413 mutex_unlock(&kexec_mutex);
1414 kimage_free(image);
1415 return ret;
Vivek Goyalf0895682014-08-08 14:25:55 -07001416}
1417
Alexander Nyberg6e274d12005-06-25 14:58:26 -07001418void crash_kexec(struct pt_regs *regs)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001419{
Andrew Morton8c5a1cf2008-08-15 00:40:27 -07001420 /* Take the kexec_mutex here to prevent sys_kexec_load
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001421 * running on one cpu from replacing the crash kernel
1422 * we are using after a panic on a different cpu.
1423 *
1424 * If the crash kernel was not located in a fixed area
1425 * of memory the xchg(&kexec_crash_image) would be
1426 * sufficient. But since I reuse the memory...
1427 */
Andrew Morton8c5a1cf2008-08-15 00:40:27 -07001428 if (mutex_trylock(&kexec_mutex)) {
David Wilderc0ce7d02006-06-23 15:29:34 -07001429 if (kexec_crash_image) {
Vivek Goyale996e582006-01-09 20:51:44 -08001430 struct pt_regs fixed_regs;
KOSAKI Motohiro0f4bd462009-12-22 03:15:43 +00001431
Vivek Goyale996e582006-01-09 20:51:44 -08001432 crash_setup_regs(&fixed_regs, regs);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001433 crash_save_vmcoreinfo();
Vivek Goyale996e582006-01-09 20:51:44 -08001434 machine_crash_shutdown(&fixed_regs);
David Wilderc0ce7d02006-06-23 15:29:34 -07001435 machine_kexec(kexec_crash_image);
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001436 }
Andrew Morton8c5a1cf2008-08-15 00:40:27 -07001437 mutex_unlock(&kexec_mutex);
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001438 }
1439}
Vivek Goyalcc571652006-01-09 20:51:41 -08001440
Amerigo Wang06a7f712009-12-15 16:47:46 -08001441size_t crash_get_memory_size(void)
1442{
Pavan Naregundie05bd332010-06-29 15:05:28 -07001443 size_t size = 0;
Amerigo Wang06a7f712009-12-15 16:47:46 -08001444 mutex_lock(&kexec_mutex);
Pavan Naregundie05bd332010-06-29 15:05:28 -07001445 if (crashk_res.end != crashk_res.start)
Joe Perches28f65c112011-06-09 09:13:32 -07001446 size = resource_size(&crashk_res);
Amerigo Wang06a7f712009-12-15 16:47:46 -08001447 mutex_unlock(&kexec_mutex);
1448 return size;
1449}
1450
Anton Blanchardc0bb9e42010-08-25 10:22:58 +10001451void __weak crash_free_reserved_phys_range(unsigned long begin,
1452 unsigned long end)
Amerigo Wang06a7f712009-12-15 16:47:46 -08001453{
1454 unsigned long addr;
1455
Jiang Liue07cee22013-04-29 15:06:58 -07001456 for (addr = begin; addr < end; addr += PAGE_SIZE)
1457 free_reserved_page(pfn_to_page(addr >> PAGE_SHIFT));
Amerigo Wang06a7f712009-12-15 16:47:46 -08001458}
1459
1460int crash_shrink_memory(unsigned long new_size)
1461{
1462 int ret = 0;
1463 unsigned long start, end;
Michael Holzheubec013c2012-01-12 17:20:15 -08001464 unsigned long old_size;
Michael Holzheu6480e5a2012-01-12 17:20:14 -08001465 struct resource *ram_res;
Amerigo Wang06a7f712009-12-15 16:47:46 -08001466
1467 mutex_lock(&kexec_mutex);
1468
1469 if (kexec_crash_image) {
1470 ret = -ENOENT;
1471 goto unlock;
1472 }
1473 start = crashk_res.start;
1474 end = crashk_res.end;
Michael Holzheubec013c2012-01-12 17:20:15 -08001475 old_size = (end == 0) ? 0 : end - start + 1;
1476 if (new_size >= old_size) {
1477 ret = (new_size == old_size) ? 0 : -EINVAL;
Amerigo Wang06a7f712009-12-15 16:47:46 -08001478 goto unlock;
1479 }
1480
Michael Holzheu6480e5a2012-01-12 17:20:14 -08001481 ram_res = kzalloc(sizeof(*ram_res), GFP_KERNEL);
1482 if (!ram_res) {
1483 ret = -ENOMEM;
1484 goto unlock;
1485 }
1486
Michael Holzheu558df722011-10-30 15:16:43 +01001487 start = roundup(start, KEXEC_CRASH_MEM_ALIGN);
1488 end = roundup(start + new_size, KEXEC_CRASH_MEM_ALIGN);
Amerigo Wang06a7f712009-12-15 16:47:46 -08001489
Michael Holzheu558df722011-10-30 15:16:43 +01001490 crash_map_reserved_pages();
Anton Blanchardc0bb9e42010-08-25 10:22:58 +10001491 crash_free_reserved_phys_range(end, crashk_res.end);
Amerigo Wang06a7f712009-12-15 16:47:46 -08001492
Pavan Naregundie05bd332010-06-29 15:05:28 -07001493 if ((start == end) && (crashk_res.parent != NULL))
Amerigo Wang06a7f712009-12-15 16:47:46 -08001494 release_resource(&crashk_res);
Michael Holzheu6480e5a2012-01-12 17:20:14 -08001495
1496 ram_res->start = end;
1497 ram_res->end = crashk_res.end;
1498 ram_res->flags = IORESOURCE_BUSY | IORESOURCE_MEM;
1499 ram_res->name = "System RAM";
1500
Vitaly Mayatskikh475f9aa62010-05-11 14:06:51 -07001501 crashk_res.end = end - 1;
Michael Holzheu6480e5a2012-01-12 17:20:14 -08001502
1503 insert_resource(&iomem_resource, ram_res);
Michael Holzheu558df722011-10-30 15:16:43 +01001504 crash_unmap_reserved_pages();
Amerigo Wang06a7f712009-12-15 16:47:46 -08001505
1506unlock:
1507 mutex_unlock(&kexec_mutex);
1508 return ret;
1509}
1510
Magnus Damm85916f82006-12-06 20:40:41 -08001511static u32 *append_elf_note(u32 *buf, char *name, unsigned type, void *data,
1512 size_t data_len)
1513{
1514 struct elf_note note;
1515
1516 note.n_namesz = strlen(name) + 1;
1517 note.n_descsz = data_len;
1518 note.n_type = type;
1519 memcpy(buf, &note, sizeof(note));
1520 buf += (sizeof(note) + 3)/4;
1521 memcpy(buf, name, note.n_namesz);
1522 buf += (note.n_namesz + 3)/4;
1523 memcpy(buf, data, note.n_descsz);
1524 buf += (note.n_descsz + 3)/4;
1525
1526 return buf;
1527}
1528
1529static void final_note(u32 *buf)
1530{
1531 struct elf_note note;
1532
1533 note.n_namesz = 0;
1534 note.n_descsz = 0;
1535 note.n_type = 0;
1536 memcpy(buf, &note, sizeof(note));
1537}
1538
1539void crash_save_cpu(struct pt_regs *regs, int cpu)
1540{
1541 struct elf_prstatus prstatus;
1542 u32 *buf;
1543
Rusty Russell4f4b6c12009-01-01 10:12:15 +10301544 if ((cpu < 0) || (cpu >= nr_cpu_ids))
Magnus Damm85916f82006-12-06 20:40:41 -08001545 return;
1546
1547 /* Using ELF notes here is opportunistic.
1548 * I need a well defined structure format
1549 * for the data I pass, and I need tags
1550 * on the data to indicate what information I have
1551 * squirrelled away. ELF notes happen to provide
1552 * all of that, so there is no need to invent something new.
1553 */
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001554 buf = (u32 *)per_cpu_ptr(crash_notes, cpu);
Magnus Damm85916f82006-12-06 20:40:41 -08001555 if (!buf)
1556 return;
1557 memset(&prstatus, 0, sizeof(prstatus));
1558 prstatus.pr_pid = current->pid;
Tejun Heo6cd61c0b2009-02-09 22:17:39 +09001559 elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
Simon Horman6672f762007-05-08 00:28:22 -07001560 buf = append_elf_note(buf, KEXEC_CORE_NOTE_NAME, NT_PRSTATUS,
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001561 &prstatus, sizeof(prstatus));
Magnus Damm85916f82006-12-06 20:40:41 -08001562 final_note(buf);
1563}
1564
Vivek Goyalcc571652006-01-09 20:51:41 -08001565static int __init crash_notes_memory_init(void)
1566{
1567 /* Allocate memory for saving cpu registers. */
1568 crash_notes = alloc_percpu(note_buf_t);
1569 if (!crash_notes) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001570 pr_warn("Kexec: Memory allocation for saving cpu register states failed\n");
Vivek Goyalcc571652006-01-09 20:51:41 -08001571 return -ENOMEM;
1572 }
1573 return 0;
1574}
Paul Gortmakerc96d6662014-04-03 14:48:35 -07001575subsys_initcall(crash_notes_memory_init);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001576
Bernhard Wallecba63c32007-10-18 23:40:58 -07001577
1578/*
1579 * parsing the "crashkernel" commandline
1580 *
1581 * this code is intended to be called from architecture specific code
1582 */
1583
1584
1585/*
1586 * This function parses command lines in the format
1587 *
1588 * crashkernel=ramsize-range:size[,...][@offset]
1589 *
1590 * The function returns 0 on success and -EINVAL on failure.
1591 */
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001592static int __init parse_crashkernel_mem(char *cmdline,
1593 unsigned long long system_ram,
1594 unsigned long long *crash_size,
1595 unsigned long long *crash_base)
Bernhard Wallecba63c32007-10-18 23:40:58 -07001596{
1597 char *cur = cmdline, *tmp;
1598
1599 /* for each entry of the comma-separated list */
1600 do {
1601 unsigned long long start, end = ULLONG_MAX, size;
1602
1603 /* get the start of the range */
1604 start = memparse(cur, &tmp);
1605 if (cur == tmp) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001606 pr_warn("crashkernel: Memory value expected\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001607 return -EINVAL;
1608 }
1609 cur = tmp;
1610 if (*cur != '-') {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001611 pr_warn("crashkernel: '-' expected\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001612 return -EINVAL;
1613 }
1614 cur++;
1615
1616 /* if no ':' is here, than we read the end */
1617 if (*cur != ':') {
1618 end = memparse(cur, &tmp);
1619 if (cur == tmp) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001620 pr_warn("crashkernel: Memory value expected\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001621 return -EINVAL;
1622 }
1623 cur = tmp;
1624 if (end <= start) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001625 pr_warn("crashkernel: end <= start\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001626 return -EINVAL;
1627 }
1628 }
1629
1630 if (*cur != ':') {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001631 pr_warn("crashkernel: ':' expected\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001632 return -EINVAL;
1633 }
1634 cur++;
1635
1636 size = memparse(cur, &tmp);
1637 if (cur == tmp) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001638 pr_warn("Memory value expected\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001639 return -EINVAL;
1640 }
1641 cur = tmp;
1642 if (size >= system_ram) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001643 pr_warn("crashkernel: invalid size\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001644 return -EINVAL;
1645 }
1646
1647 /* match ? */
Michael Ellermanbe089d792008-05-01 04:34:49 -07001648 if (system_ram >= start && system_ram < end) {
Bernhard Wallecba63c32007-10-18 23:40:58 -07001649 *crash_size = size;
1650 break;
1651 }
1652 } while (*cur++ == ',');
1653
1654 if (*crash_size > 0) {
Hidetoshi Seto11c7da42009-07-29 15:02:08 -07001655 while (*cur && *cur != ' ' && *cur != '@')
Bernhard Wallecba63c32007-10-18 23:40:58 -07001656 cur++;
1657 if (*cur == '@') {
1658 cur++;
1659 *crash_base = memparse(cur, &tmp);
1660 if (cur == tmp) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001661 pr_warn("Memory value expected after '@'\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001662 return -EINVAL;
1663 }
1664 }
1665 }
1666
1667 return 0;
1668}
1669
1670/*
1671 * That function parses "simple" (old) crashkernel command lines like
1672 *
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001673 * crashkernel=size[@offset]
Bernhard Wallecba63c32007-10-18 23:40:58 -07001674 *
1675 * It returns 0 on success and -EINVAL on failure.
1676 */
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001677static int __init parse_crashkernel_simple(char *cmdline,
1678 unsigned long long *crash_size,
1679 unsigned long long *crash_base)
Bernhard Wallecba63c32007-10-18 23:40:58 -07001680{
1681 char *cur = cmdline;
1682
1683 *crash_size = memparse(cmdline, &cur);
1684 if (cmdline == cur) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001685 pr_warn("crashkernel: memory value expected\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001686 return -EINVAL;
1687 }
1688
1689 if (*cur == '@')
1690 *crash_base = memparse(cur+1, &cur);
Zhenzhong Duaneaa3be62012-03-28 14:42:47 -07001691 else if (*cur != ' ' && *cur != '\0') {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001692 pr_warn("crashkernel: unrecognized char\n");
Zhenzhong Duaneaa3be62012-03-28 14:42:47 -07001693 return -EINVAL;
1694 }
Bernhard Wallecba63c32007-10-18 23:40:58 -07001695
1696 return 0;
1697}
1698
Yinghai Luadbc7422013-04-15 22:23:48 -07001699#define SUFFIX_HIGH 0
1700#define SUFFIX_LOW 1
1701#define SUFFIX_NULL 2
1702static __initdata char *suffix_tbl[] = {
1703 [SUFFIX_HIGH] = ",high",
1704 [SUFFIX_LOW] = ",low",
1705 [SUFFIX_NULL] = NULL,
1706};
1707
Bernhard Wallecba63c32007-10-18 23:40:58 -07001708/*
Yinghai Luadbc7422013-04-15 22:23:48 -07001709 * That function parses "suffix" crashkernel command lines like
1710 *
1711 * crashkernel=size,[high|low]
1712 *
1713 * It returns 0 on success and -EINVAL on failure.
Bernhard Wallecba63c32007-10-18 23:40:58 -07001714 */
Yinghai Luadbc7422013-04-15 22:23:48 -07001715static int __init parse_crashkernel_suffix(char *cmdline,
1716 unsigned long long *crash_size,
1717 unsigned long long *crash_base,
1718 const char *suffix)
1719{
1720 char *cur = cmdline;
1721
1722 *crash_size = memparse(cmdline, &cur);
1723 if (cmdline == cur) {
1724 pr_warn("crashkernel: memory value expected\n");
1725 return -EINVAL;
1726 }
1727
1728 /* check with suffix */
1729 if (strncmp(cur, suffix, strlen(suffix))) {
1730 pr_warn("crashkernel: unrecognized char\n");
1731 return -EINVAL;
1732 }
1733 cur += strlen(suffix);
1734 if (*cur != ' ' && *cur != '\0') {
1735 pr_warn("crashkernel: unrecognized char\n");
1736 return -EINVAL;
1737 }
1738
1739 return 0;
1740}
1741
1742static __init char *get_last_crashkernel(char *cmdline,
1743 const char *name,
1744 const char *suffix)
1745{
1746 char *p = cmdline, *ck_cmdline = NULL;
1747
1748 /* find crashkernel and use the last one if there are more */
1749 p = strstr(p, name);
1750 while (p) {
1751 char *end_p = strchr(p, ' ');
1752 char *q;
1753
1754 if (!end_p)
1755 end_p = p + strlen(p);
1756
1757 if (!suffix) {
1758 int i;
1759
1760 /* skip the one with any known suffix */
1761 for (i = 0; suffix_tbl[i]; i++) {
1762 q = end_p - strlen(suffix_tbl[i]);
1763 if (!strncmp(q, suffix_tbl[i],
1764 strlen(suffix_tbl[i])))
1765 goto next;
1766 }
1767 ck_cmdline = p;
1768 } else {
1769 q = end_p - strlen(suffix);
1770 if (!strncmp(q, suffix, strlen(suffix)))
1771 ck_cmdline = p;
1772 }
1773next:
1774 p = strstr(p+1, name);
1775 }
1776
1777 if (!ck_cmdline)
1778 return NULL;
1779
1780 return ck_cmdline;
1781}
1782
Yinghai Lu0212f912013-01-24 12:20:11 -08001783static int __init __parse_crashkernel(char *cmdline,
Bernhard Wallecba63c32007-10-18 23:40:58 -07001784 unsigned long long system_ram,
1785 unsigned long long *crash_size,
Yinghai Lu0212f912013-01-24 12:20:11 -08001786 unsigned long long *crash_base,
Yinghai Luadbc7422013-04-15 22:23:48 -07001787 const char *name,
1788 const char *suffix)
Bernhard Wallecba63c32007-10-18 23:40:58 -07001789{
Bernhard Wallecba63c32007-10-18 23:40:58 -07001790 char *first_colon, *first_space;
Yinghai Luadbc7422013-04-15 22:23:48 -07001791 char *ck_cmdline;
Bernhard Wallecba63c32007-10-18 23:40:58 -07001792
1793 BUG_ON(!crash_size || !crash_base);
1794 *crash_size = 0;
1795 *crash_base = 0;
1796
Yinghai Luadbc7422013-04-15 22:23:48 -07001797 ck_cmdline = get_last_crashkernel(cmdline, name, suffix);
Bernhard Wallecba63c32007-10-18 23:40:58 -07001798
1799 if (!ck_cmdline)
1800 return -EINVAL;
1801
Yinghai Lu0212f912013-01-24 12:20:11 -08001802 ck_cmdline += strlen(name);
Bernhard Wallecba63c32007-10-18 23:40:58 -07001803
Yinghai Luadbc7422013-04-15 22:23:48 -07001804 if (suffix)
1805 return parse_crashkernel_suffix(ck_cmdline, crash_size,
1806 crash_base, suffix);
Bernhard Wallecba63c32007-10-18 23:40:58 -07001807 /*
1808 * if the commandline contains a ':', then that's the extended
1809 * syntax -- if not, it must be the classic syntax
1810 */
1811 first_colon = strchr(ck_cmdline, ':');
1812 first_space = strchr(ck_cmdline, ' ');
1813 if (first_colon && (!first_space || first_colon < first_space))
1814 return parse_crashkernel_mem(ck_cmdline, system_ram,
1815 crash_size, crash_base);
Bernhard Wallecba63c32007-10-18 23:40:58 -07001816
Xishi Qiu80c74f62013-09-11 14:24:47 -07001817 return parse_crashkernel_simple(ck_cmdline, crash_size, crash_base);
Bernhard Wallecba63c32007-10-18 23:40:58 -07001818}
1819
Yinghai Luadbc7422013-04-15 22:23:48 -07001820/*
1821 * That function is the entry point for command line parsing and should be
1822 * called from the arch-specific code.
1823 */
Yinghai Lu0212f912013-01-24 12:20:11 -08001824int __init parse_crashkernel(char *cmdline,
1825 unsigned long long system_ram,
1826 unsigned long long *crash_size,
1827 unsigned long long *crash_base)
1828{
1829 return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
Yinghai Luadbc7422013-04-15 22:23:48 -07001830 "crashkernel=", NULL);
Yinghai Lu0212f912013-01-24 12:20:11 -08001831}
1832
Yinghai Lu55a20ee2013-04-15 22:23:47 -07001833int __init parse_crashkernel_high(char *cmdline,
1834 unsigned long long system_ram,
1835 unsigned long long *crash_size,
1836 unsigned long long *crash_base)
1837{
1838 return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
Yinghai Luadbc7422013-04-15 22:23:48 -07001839 "crashkernel=", suffix_tbl[SUFFIX_HIGH]);
Yinghai Lu55a20ee2013-04-15 22:23:47 -07001840}
1841
Yinghai Lu0212f912013-01-24 12:20:11 -08001842int __init parse_crashkernel_low(char *cmdline,
1843 unsigned long long system_ram,
1844 unsigned long long *crash_size,
1845 unsigned long long *crash_base)
1846{
1847 return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
Yinghai Luadbc7422013-04-15 22:23:48 -07001848 "crashkernel=", suffix_tbl[SUFFIX_LOW]);
Yinghai Lu0212f912013-01-24 12:20:11 -08001849}
Bernhard Wallecba63c32007-10-18 23:40:58 -07001850
Michael Holzheufa8ff292011-10-30 15:16:41 +01001851static void update_vmcoreinfo_note(void)
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001852{
Michael Holzheufa8ff292011-10-30 15:16:41 +01001853 u32 *buf = vmcoreinfo_note;
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001854
1855 if (!vmcoreinfo_size)
1856 return;
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001857 buf = append_elf_note(buf, VMCOREINFO_NOTE_NAME, 0, vmcoreinfo_data,
1858 vmcoreinfo_size);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001859 final_note(buf);
1860}
1861
Michael Holzheufa8ff292011-10-30 15:16:41 +01001862void crash_save_vmcoreinfo(void)
1863{
Vivek Goyal63dca8d2012-07-30 14:42:36 -07001864 vmcoreinfo_append_str("CRASHTIME=%ld\n", get_seconds());
Michael Holzheufa8ff292011-10-30 15:16:41 +01001865 update_vmcoreinfo_note();
1866}
1867
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001868void vmcoreinfo_append_str(const char *fmt, ...)
1869{
1870 va_list args;
1871 char buf[0x50];
Zhang Yanfei310faaa2013-04-30 15:28:21 -07001872 size_t r;
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001873
1874 va_start(args, fmt);
Chen Ganga19428e52014-01-27 17:07:13 -08001875 r = vscnprintf(buf, sizeof(buf), fmt, args);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001876 va_end(args);
1877
Zhang Yanfei31c3a3f2013-04-30 15:28:23 -07001878 r = min(r, vmcoreinfo_max_size - vmcoreinfo_size);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001879
1880 memcpy(&vmcoreinfo_data[vmcoreinfo_size], buf, r);
1881
1882 vmcoreinfo_size += r;
1883}
1884
1885/*
1886 * provide an empty default implementation here -- architecture
1887 * code may override this
1888 */
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -07001889void __weak arch_crash_save_vmcoreinfo(void)
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001890{}
1891
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -07001892unsigned long __weak paddr_vmcoreinfo_note(void)
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001893{
1894 return __pa((unsigned long)(char *)&vmcoreinfo_note);
1895}
1896
1897static int __init crash_save_vmcoreinfo_init(void)
1898{
Ken'ichi Ohmichibba1f602008-02-07 00:15:22 -08001899 VMCOREINFO_OSRELEASE(init_uts_ns.name.release);
1900 VMCOREINFO_PAGESIZE(PAGE_SIZE);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001901
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001902 VMCOREINFO_SYMBOL(init_uts_ns);
1903 VMCOREINFO_SYMBOL(node_online_map);
Will Deacond034cfa2012-03-28 14:42:47 -07001904#ifdef CONFIG_MMU
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001905 VMCOREINFO_SYMBOL(swapper_pg_dir);
Will Deacond034cfa2012-03-28 14:42:47 -07001906#endif
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001907 VMCOREINFO_SYMBOL(_stext);
Joonsoo Kimf1c40692013-04-29 15:07:37 -07001908 VMCOREINFO_SYMBOL(vmap_area_list);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001909
1910#ifndef CONFIG_NEED_MULTIPLE_NODES
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001911 VMCOREINFO_SYMBOL(mem_map);
1912 VMCOREINFO_SYMBOL(contig_page_data);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001913#endif
1914#ifdef CONFIG_SPARSEMEM
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001915 VMCOREINFO_SYMBOL(mem_section);
1916 VMCOREINFO_LENGTH(mem_section, NR_SECTION_ROOTS);
Ken'ichi Ohmichic76f8602008-02-07 00:15:20 -08001917 VMCOREINFO_STRUCT_SIZE(mem_section);
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001918 VMCOREINFO_OFFSET(mem_section, section_mem_map);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001919#endif
Ken'ichi Ohmichic76f8602008-02-07 00:15:20 -08001920 VMCOREINFO_STRUCT_SIZE(page);
1921 VMCOREINFO_STRUCT_SIZE(pglist_data);
1922 VMCOREINFO_STRUCT_SIZE(zone);
1923 VMCOREINFO_STRUCT_SIZE(free_area);
1924 VMCOREINFO_STRUCT_SIZE(list_head);
1925 VMCOREINFO_SIZE(nodemask_t);
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001926 VMCOREINFO_OFFSET(page, flags);
1927 VMCOREINFO_OFFSET(page, _count);
1928 VMCOREINFO_OFFSET(page, mapping);
1929 VMCOREINFO_OFFSET(page, lru);
Atsushi Kumagai8d670912013-02-27 17:03:25 -08001930 VMCOREINFO_OFFSET(page, _mapcount);
1931 VMCOREINFO_OFFSET(page, private);
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001932 VMCOREINFO_OFFSET(pglist_data, node_zones);
1933 VMCOREINFO_OFFSET(pglist_data, nr_zones);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001934#ifdef CONFIG_FLAT_NODE_MEM_MAP
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001935 VMCOREINFO_OFFSET(pglist_data, node_mem_map);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001936#endif
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001937 VMCOREINFO_OFFSET(pglist_data, node_start_pfn);
1938 VMCOREINFO_OFFSET(pglist_data, node_spanned_pages);
1939 VMCOREINFO_OFFSET(pglist_data, node_id);
1940 VMCOREINFO_OFFSET(zone, free_area);
1941 VMCOREINFO_OFFSET(zone, vm_stat);
1942 VMCOREINFO_OFFSET(zone, spanned_pages);
1943 VMCOREINFO_OFFSET(free_area, free_list);
1944 VMCOREINFO_OFFSET(list_head, next);
1945 VMCOREINFO_OFFSET(list_head, prev);
Atsushi Kumagai13ba3fc2013-04-29 15:07:40 -07001946 VMCOREINFO_OFFSET(vmap_area, va_start);
1947 VMCOREINFO_OFFSET(vmap_area, list);
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001948 VMCOREINFO_LENGTH(zone.free_area, MAX_ORDER);
Neil Horman04d491a2009-04-02 16:58:57 -07001949 log_buf_kexec_setup();
Ken'ichi Ohmichi83a08e72008-01-08 15:33:05 -08001950 VMCOREINFO_LENGTH(free_area.free_list, MIGRATE_TYPES);
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001951 VMCOREINFO_NUMBER(NR_FREE_PAGES);
Ken'ichi Ohmichi122c7a52008-04-28 02:13:04 -07001952 VMCOREINFO_NUMBER(PG_lru);
1953 VMCOREINFO_NUMBER(PG_private);
1954 VMCOREINFO_NUMBER(PG_swapcache);
Atsushi Kumagai8d670912013-02-27 17:03:25 -08001955 VMCOREINFO_NUMBER(PG_slab);
Mitsuhiro Tanino0d0bf662013-02-27 17:03:27 -08001956#ifdef CONFIG_MEMORY_FAILURE
1957 VMCOREINFO_NUMBER(PG_hwpoison);
1958#endif
Petr Tesarikb3acc562014-06-23 13:22:03 -07001959 VMCOREINFO_NUMBER(PG_head_mask);
Atsushi Kumagai8d670912013-02-27 17:03:25 -08001960 VMCOREINFO_NUMBER(PAGE_BUDDY_MAPCOUNT_VALUE);
David Rientjes3a1122d2014-07-30 19:05:55 -07001961#ifdef CONFIG_HUGETLBFS
Atsushi Kumagai8f1d26d2014-07-30 16:08:39 -07001962 VMCOREINFO_SYMBOL(free_huge_page);
David Rientjes3a1122d2014-07-30 19:05:55 -07001963#endif
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001964
1965 arch_crash_save_vmcoreinfo();
Michael Holzheufa8ff292011-10-30 15:16:41 +01001966 update_vmcoreinfo_note();
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001967
1968 return 0;
1969}
1970
Paul Gortmakerc96d6662014-04-03 14:48:35 -07001971subsys_initcall(crash_save_vmcoreinfo_init);
Huang Ying3ab83522008-07-25 19:45:07 -07001972
Vivek Goyalcb105252014-08-08 14:25:57 -07001973static int __kexec_add_segment(struct kimage *image, char *buf,
1974 unsigned long bufsz, unsigned long mem,
1975 unsigned long memsz)
1976{
1977 struct kexec_segment *ksegment;
1978
1979 ksegment = &image->segment[image->nr_segments];
1980 ksegment->kbuf = buf;
1981 ksegment->bufsz = bufsz;
1982 ksegment->mem = mem;
1983 ksegment->memsz = memsz;
1984 image->nr_segments++;
1985
1986 return 0;
1987}
1988
1989static int locate_mem_hole_top_down(unsigned long start, unsigned long end,
1990 struct kexec_buf *kbuf)
1991{
1992 struct kimage *image = kbuf->image;
1993 unsigned long temp_start, temp_end;
1994
1995 temp_end = min(end, kbuf->buf_max);
1996 temp_start = temp_end - kbuf->memsz;
1997
1998 do {
1999 /* align down start */
2000 temp_start = temp_start & (~(kbuf->buf_align - 1));
2001
2002 if (temp_start < start || temp_start < kbuf->buf_min)
2003 return 0;
2004
2005 temp_end = temp_start + kbuf->memsz - 1;
2006
2007 /*
2008 * Make sure this does not conflict with any of existing
2009 * segments
2010 */
2011 if (kimage_is_destination_range(image, temp_start, temp_end)) {
2012 temp_start = temp_start - PAGE_SIZE;
2013 continue;
2014 }
2015
2016 /* We found a suitable memory range */
2017 break;
2018 } while (1);
2019
2020 /* If we are here, we found a suitable memory range */
2021 __kexec_add_segment(image, kbuf->buffer, kbuf->bufsz, temp_start,
2022 kbuf->memsz);
2023
2024 /* Success, stop navigating through remaining System RAM ranges */
2025 return 1;
2026}
2027
2028static int locate_mem_hole_bottom_up(unsigned long start, unsigned long end,
2029 struct kexec_buf *kbuf)
2030{
2031 struct kimage *image = kbuf->image;
2032 unsigned long temp_start, temp_end;
2033
2034 temp_start = max(start, kbuf->buf_min);
2035
2036 do {
2037 temp_start = ALIGN(temp_start, kbuf->buf_align);
2038 temp_end = temp_start + kbuf->memsz - 1;
2039
2040 if (temp_end > end || temp_end > kbuf->buf_max)
2041 return 0;
2042 /*
2043 * Make sure this does not conflict with any of existing
2044 * segments
2045 */
2046 if (kimage_is_destination_range(image, temp_start, temp_end)) {
2047 temp_start = temp_start + PAGE_SIZE;
2048 continue;
2049 }
2050
2051 /* We found a suitable memory range */
2052 break;
2053 } while (1);
2054
2055 /* If we are here, we found a suitable memory range */
2056 __kexec_add_segment(image, kbuf->buffer, kbuf->bufsz, temp_start,
2057 kbuf->memsz);
2058
2059 /* Success, stop navigating through remaining System RAM ranges */
2060 return 1;
2061}
2062
2063static int locate_mem_hole_callback(u64 start, u64 end, void *arg)
2064{
2065 struct kexec_buf *kbuf = (struct kexec_buf *)arg;
2066 unsigned long sz = end - start + 1;
2067
2068 /* Returning 0 will take to next memory range */
2069 if (sz < kbuf->memsz)
2070 return 0;
2071
2072 if (end < kbuf->buf_min || start > kbuf->buf_max)
2073 return 0;
2074
2075 /*
2076 * Allocate memory top down with-in ram range. Otherwise bottom up
2077 * allocation.
2078 */
2079 if (kbuf->top_down)
2080 return locate_mem_hole_top_down(start, end, kbuf);
2081 return locate_mem_hole_bottom_up(start, end, kbuf);
2082}
2083
2084/*
2085 * Helper function for placing a buffer in a kexec segment. This assumes
2086 * that kexec_mutex is held.
2087 */
2088int kexec_add_buffer(struct kimage *image, char *buffer, unsigned long bufsz,
2089 unsigned long memsz, unsigned long buf_align,
2090 unsigned long buf_min, unsigned long buf_max,
2091 bool top_down, unsigned long *load_addr)
2092{
2093
2094 struct kexec_segment *ksegment;
2095 struct kexec_buf buf, *kbuf;
2096 int ret;
2097
2098 /* Currently adding segment this way is allowed only in file mode */
2099 if (!image->file_mode)
2100 return -EINVAL;
2101
2102 if (image->nr_segments >= KEXEC_SEGMENT_MAX)
2103 return -EINVAL;
2104
2105 /*
2106 * Make sure we are not trying to add buffer after allocating
2107 * control pages. All segments need to be placed first before
2108 * any control pages are allocated. As control page allocation
2109 * logic goes through list of segments to make sure there are
2110 * no destination overlaps.
2111 */
2112 if (!list_empty(&image->control_pages)) {
2113 WARN_ON(1);
2114 return -EINVAL;
2115 }
2116
2117 memset(&buf, 0, sizeof(struct kexec_buf));
2118 kbuf = &buf;
2119 kbuf->image = image;
2120 kbuf->buffer = buffer;
2121 kbuf->bufsz = bufsz;
2122
2123 kbuf->memsz = ALIGN(memsz, PAGE_SIZE);
2124 kbuf->buf_align = max(buf_align, PAGE_SIZE);
2125 kbuf->buf_min = buf_min;
2126 kbuf->buf_max = buf_max;
2127 kbuf->top_down = top_down;
2128
2129 /* Walk the RAM ranges and allocate a suitable range for the buffer */
2130 ret = walk_system_ram_res(0, -1, kbuf, locate_mem_hole_callback);
2131 if (ret != 1) {
2132 /* A suitable memory range could not be found for buffer */
2133 return -EADDRNOTAVAIL;
2134 }
2135
2136 /* Found a suitable memory range */
2137 ksegment = &image->segment[image->nr_segments - 1];
2138 *load_addr = ksegment->mem;
2139 return 0;
2140}
2141
Vivek Goyal12db5562014-08-08 14:26:04 -07002142/* Calculate and store the digest of segments */
2143static int kexec_calculate_store_digests(struct kimage *image)
2144{
2145 struct crypto_shash *tfm;
2146 struct shash_desc *desc;
2147 int ret = 0, i, j, zero_buf_sz, sha_region_sz;
2148 size_t desc_size, nullsz;
2149 char *digest;
2150 void *zero_buf;
2151 struct kexec_sha_region *sha_regions;
2152 struct purgatory_info *pi = &image->purgatory_info;
2153
2154 zero_buf = __va(page_to_pfn(ZERO_PAGE(0)) << PAGE_SHIFT);
2155 zero_buf_sz = PAGE_SIZE;
2156
2157 tfm = crypto_alloc_shash("sha256", 0, 0);
2158 if (IS_ERR(tfm)) {
2159 ret = PTR_ERR(tfm);
2160 goto out;
2161 }
2162
2163 desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
2164 desc = kzalloc(desc_size, GFP_KERNEL);
2165 if (!desc) {
2166 ret = -ENOMEM;
2167 goto out_free_tfm;
2168 }
2169
2170 sha_region_sz = KEXEC_SEGMENT_MAX * sizeof(struct kexec_sha_region);
2171 sha_regions = vzalloc(sha_region_sz);
2172 if (!sha_regions)
2173 goto out_free_desc;
2174
2175 desc->tfm = tfm;
2176 desc->flags = 0;
2177
2178 ret = crypto_shash_init(desc);
2179 if (ret < 0)
2180 goto out_free_sha_regions;
2181
2182 digest = kzalloc(SHA256_DIGEST_SIZE, GFP_KERNEL);
2183 if (!digest) {
2184 ret = -ENOMEM;
2185 goto out_free_sha_regions;
2186 }
2187
2188 for (j = i = 0; i < image->nr_segments; i++) {
2189 struct kexec_segment *ksegment;
2190
2191 ksegment = &image->segment[i];
2192 /*
2193 * Skip purgatory as it will be modified once we put digest
2194 * info in purgatory.
2195 */
2196 if (ksegment->kbuf == pi->purgatory_buf)
2197 continue;
2198
2199 ret = crypto_shash_update(desc, ksegment->kbuf,
2200 ksegment->bufsz);
2201 if (ret)
2202 break;
2203
2204 /*
2205 * Assume rest of the buffer is filled with zero and
2206 * update digest accordingly.
2207 */
2208 nullsz = ksegment->memsz - ksegment->bufsz;
2209 while (nullsz) {
2210 unsigned long bytes = nullsz;
2211
2212 if (bytes > zero_buf_sz)
2213 bytes = zero_buf_sz;
2214 ret = crypto_shash_update(desc, zero_buf, bytes);
2215 if (ret)
2216 break;
2217 nullsz -= bytes;
2218 }
2219
2220 if (ret)
2221 break;
2222
2223 sha_regions[j].start = ksegment->mem;
2224 sha_regions[j].len = ksegment->memsz;
2225 j++;
2226 }
2227
2228 if (!ret) {
2229 ret = crypto_shash_final(desc, digest);
2230 if (ret)
2231 goto out_free_digest;
2232 ret = kexec_purgatory_get_set_symbol(image, "sha_regions",
2233 sha_regions, sha_region_sz, 0);
2234 if (ret)
2235 goto out_free_digest;
2236
2237 ret = kexec_purgatory_get_set_symbol(image, "sha256_digest",
2238 digest, SHA256_DIGEST_SIZE, 0);
2239 if (ret)
2240 goto out_free_digest;
2241 }
2242
2243out_free_digest:
2244 kfree(digest);
2245out_free_sha_regions:
2246 vfree(sha_regions);
2247out_free_desc:
2248 kfree(desc);
2249out_free_tfm:
2250 kfree(tfm);
2251out:
2252 return ret;
2253}
2254
2255/* Actually load purgatory. Lot of code taken from kexec-tools */
2256static int __kexec_load_purgatory(struct kimage *image, unsigned long min,
2257 unsigned long max, int top_down)
2258{
2259 struct purgatory_info *pi = &image->purgatory_info;
2260 unsigned long align, buf_align, bss_align, buf_sz, bss_sz, bss_pad;
2261 unsigned long memsz, entry, load_addr, curr_load_addr, bss_addr, offset;
2262 unsigned char *buf_addr, *src;
2263 int i, ret = 0, entry_sidx = -1;
2264 const Elf_Shdr *sechdrs_c;
2265 Elf_Shdr *sechdrs = NULL;
2266 void *purgatory_buf = NULL;
2267
2268 /*
2269 * sechdrs_c points to section headers in purgatory and are read
2270 * only. No modifications allowed.
2271 */
2272 sechdrs_c = (void *)pi->ehdr + pi->ehdr->e_shoff;
2273
2274 /*
2275 * We can not modify sechdrs_c[] and its fields. It is read only.
2276 * Copy it over to a local copy where one can store some temporary
2277 * data and free it at the end. We need to modify ->sh_addr and
2278 * ->sh_offset fields to keep track of permanent and temporary
2279 * locations of sections.
2280 */
2281 sechdrs = vzalloc(pi->ehdr->e_shnum * sizeof(Elf_Shdr));
2282 if (!sechdrs)
2283 return -ENOMEM;
2284
2285 memcpy(sechdrs, sechdrs_c, pi->ehdr->e_shnum * sizeof(Elf_Shdr));
2286
2287 /*
2288 * We seem to have multiple copies of sections. First copy is which
2289 * is embedded in kernel in read only section. Some of these sections
2290 * will be copied to a temporary buffer and relocated. And these
2291 * sections will finally be copied to their final destination at
2292 * segment load time.
2293 *
2294 * Use ->sh_offset to reflect section address in memory. It will
2295 * point to original read only copy if section is not allocatable.
2296 * Otherwise it will point to temporary copy which will be relocated.
2297 *
2298 * Use ->sh_addr to contain final address of the section where it
2299 * will go during execution time.
2300 */
2301 for (i = 0; i < pi->ehdr->e_shnum; i++) {
2302 if (sechdrs[i].sh_type == SHT_NOBITS)
2303 continue;
2304
2305 sechdrs[i].sh_offset = (unsigned long)pi->ehdr +
2306 sechdrs[i].sh_offset;
2307 }
2308
2309 /*
2310 * Identify entry point section and make entry relative to section
2311 * start.
2312 */
2313 entry = pi->ehdr->e_entry;
2314 for (i = 0; i < pi->ehdr->e_shnum; i++) {
2315 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2316 continue;
2317
2318 if (!(sechdrs[i].sh_flags & SHF_EXECINSTR))
2319 continue;
2320
2321 /* Make entry section relative */
2322 if (sechdrs[i].sh_addr <= pi->ehdr->e_entry &&
2323 ((sechdrs[i].sh_addr + sechdrs[i].sh_size) >
2324 pi->ehdr->e_entry)) {
2325 entry_sidx = i;
2326 entry -= sechdrs[i].sh_addr;
2327 break;
2328 }
2329 }
2330
2331 /* Determine how much memory is needed to load relocatable object. */
2332 buf_align = 1;
2333 bss_align = 1;
2334 buf_sz = 0;
2335 bss_sz = 0;
2336
2337 for (i = 0; i < pi->ehdr->e_shnum; i++) {
2338 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2339 continue;
2340
2341 align = sechdrs[i].sh_addralign;
2342 if (sechdrs[i].sh_type != SHT_NOBITS) {
2343 if (buf_align < align)
2344 buf_align = align;
2345 buf_sz = ALIGN(buf_sz, align);
2346 buf_sz += sechdrs[i].sh_size;
2347 } else {
2348 /* bss section */
2349 if (bss_align < align)
2350 bss_align = align;
2351 bss_sz = ALIGN(bss_sz, align);
2352 bss_sz += sechdrs[i].sh_size;
2353 }
2354 }
2355
2356 /* Determine the bss padding required to align bss properly */
2357 bss_pad = 0;
2358 if (buf_sz & (bss_align - 1))
2359 bss_pad = bss_align - (buf_sz & (bss_align - 1));
2360
2361 memsz = buf_sz + bss_pad + bss_sz;
2362
2363 /* Allocate buffer for purgatory */
2364 purgatory_buf = vzalloc(buf_sz);
2365 if (!purgatory_buf) {
2366 ret = -ENOMEM;
2367 goto out;
2368 }
2369
2370 if (buf_align < bss_align)
2371 buf_align = bss_align;
2372
2373 /* Add buffer to segment list */
2374 ret = kexec_add_buffer(image, purgatory_buf, buf_sz, memsz,
2375 buf_align, min, max, top_down,
2376 &pi->purgatory_load_addr);
2377 if (ret)
2378 goto out;
2379
2380 /* Load SHF_ALLOC sections */
2381 buf_addr = purgatory_buf;
2382 load_addr = curr_load_addr = pi->purgatory_load_addr;
2383 bss_addr = load_addr + buf_sz + bss_pad;
2384
2385 for (i = 0; i < pi->ehdr->e_shnum; i++) {
2386 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2387 continue;
2388
2389 align = sechdrs[i].sh_addralign;
2390 if (sechdrs[i].sh_type != SHT_NOBITS) {
2391 curr_load_addr = ALIGN(curr_load_addr, align);
2392 offset = curr_load_addr - load_addr;
2393 /* We already modifed ->sh_offset to keep src addr */
2394 src = (char *) sechdrs[i].sh_offset;
2395 memcpy(buf_addr + offset, src, sechdrs[i].sh_size);
2396
2397 /* Store load address and source address of section */
2398 sechdrs[i].sh_addr = curr_load_addr;
2399
2400 /*
2401 * This section got copied to temporary buffer. Update
2402 * ->sh_offset accordingly.
2403 */
2404 sechdrs[i].sh_offset = (unsigned long)(buf_addr + offset);
2405
2406 /* Advance to the next address */
2407 curr_load_addr += sechdrs[i].sh_size;
2408 } else {
2409 bss_addr = ALIGN(bss_addr, align);
2410 sechdrs[i].sh_addr = bss_addr;
2411 bss_addr += sechdrs[i].sh_size;
2412 }
2413 }
2414
2415 /* Update entry point based on load address of text section */
2416 if (entry_sidx >= 0)
2417 entry += sechdrs[entry_sidx].sh_addr;
2418
2419 /* Make kernel jump to purgatory after shutdown */
2420 image->start = entry;
2421
2422 /* Used later to get/set symbol values */
2423 pi->sechdrs = sechdrs;
2424
2425 /*
2426 * Used later to identify which section is purgatory and skip it
2427 * from checksumming.
2428 */
2429 pi->purgatory_buf = purgatory_buf;
2430 return ret;
2431out:
2432 vfree(sechdrs);
2433 vfree(purgatory_buf);
2434 return ret;
2435}
2436
2437static int kexec_apply_relocations(struct kimage *image)
2438{
2439 int i, ret;
2440 struct purgatory_info *pi = &image->purgatory_info;
2441 Elf_Shdr *sechdrs = pi->sechdrs;
2442
2443 /* Apply relocations */
2444 for (i = 0; i < pi->ehdr->e_shnum; i++) {
2445 Elf_Shdr *section, *symtab;
2446
2447 if (sechdrs[i].sh_type != SHT_RELA &&
2448 sechdrs[i].sh_type != SHT_REL)
2449 continue;
2450
2451 /*
2452 * For section of type SHT_RELA/SHT_REL,
2453 * ->sh_link contains section header index of associated
2454 * symbol table. And ->sh_info contains section header
2455 * index of section to which relocations apply.
2456 */
2457 if (sechdrs[i].sh_info >= pi->ehdr->e_shnum ||
2458 sechdrs[i].sh_link >= pi->ehdr->e_shnum)
2459 return -ENOEXEC;
2460
2461 section = &sechdrs[sechdrs[i].sh_info];
2462 symtab = &sechdrs[sechdrs[i].sh_link];
2463
2464 if (!(section->sh_flags & SHF_ALLOC))
2465 continue;
2466
2467 /*
2468 * symtab->sh_link contain section header index of associated
2469 * string table.
2470 */
2471 if (symtab->sh_link >= pi->ehdr->e_shnum)
2472 /* Invalid section number? */
2473 continue;
2474
2475 /*
2476 * Respective archicture needs to provide support for applying
2477 * relocations of type SHT_RELA/SHT_REL.
2478 */
2479 if (sechdrs[i].sh_type == SHT_RELA)
2480 ret = arch_kexec_apply_relocations_add(pi->ehdr,
2481 sechdrs, i);
2482 else if (sechdrs[i].sh_type == SHT_REL)
2483 ret = arch_kexec_apply_relocations(pi->ehdr,
2484 sechdrs, i);
2485 if (ret)
2486 return ret;
2487 }
2488
2489 return 0;
2490}
2491
2492/* Load relocatable purgatory object and relocate it appropriately */
2493int kexec_load_purgatory(struct kimage *image, unsigned long min,
2494 unsigned long max, int top_down,
2495 unsigned long *load_addr)
2496{
2497 struct purgatory_info *pi = &image->purgatory_info;
2498 int ret;
2499
2500 if (kexec_purgatory_size <= 0)
2501 return -EINVAL;
2502
2503 if (kexec_purgatory_size < sizeof(Elf_Ehdr))
2504 return -ENOEXEC;
2505
2506 pi->ehdr = (Elf_Ehdr *)kexec_purgatory;
2507
2508 if (memcmp(pi->ehdr->e_ident, ELFMAG, SELFMAG) != 0
2509 || pi->ehdr->e_type != ET_REL
2510 || !elf_check_arch(pi->ehdr)
2511 || pi->ehdr->e_shentsize != sizeof(Elf_Shdr))
2512 return -ENOEXEC;
2513
2514 if (pi->ehdr->e_shoff >= kexec_purgatory_size
2515 || (pi->ehdr->e_shnum * sizeof(Elf_Shdr) >
2516 kexec_purgatory_size - pi->ehdr->e_shoff))
2517 return -ENOEXEC;
2518
2519 ret = __kexec_load_purgatory(image, min, max, top_down);
2520 if (ret)
2521 return ret;
2522
2523 ret = kexec_apply_relocations(image);
2524 if (ret)
2525 goto out;
2526
2527 *load_addr = pi->purgatory_load_addr;
2528 return 0;
2529out:
2530 vfree(pi->sechdrs);
2531 vfree(pi->purgatory_buf);
2532 return ret;
2533}
2534
2535static Elf_Sym *kexec_purgatory_find_symbol(struct purgatory_info *pi,
2536 const char *name)
2537{
2538 Elf_Sym *syms;
2539 Elf_Shdr *sechdrs;
2540 Elf_Ehdr *ehdr;
2541 int i, k;
2542 const char *strtab;
2543
2544 if (!pi->sechdrs || !pi->ehdr)
2545 return NULL;
2546
2547 sechdrs = pi->sechdrs;
2548 ehdr = pi->ehdr;
2549
2550 for (i = 0; i < ehdr->e_shnum; i++) {
2551 if (sechdrs[i].sh_type != SHT_SYMTAB)
2552 continue;
2553
2554 if (sechdrs[i].sh_link >= ehdr->e_shnum)
2555 /* Invalid strtab section number */
2556 continue;
2557 strtab = (char *)sechdrs[sechdrs[i].sh_link].sh_offset;
2558 syms = (Elf_Sym *)sechdrs[i].sh_offset;
2559
2560 /* Go through symbols for a match */
2561 for (k = 0; k < sechdrs[i].sh_size/sizeof(Elf_Sym); k++) {
2562 if (ELF_ST_BIND(syms[k].st_info) != STB_GLOBAL)
2563 continue;
2564
2565 if (strcmp(strtab + syms[k].st_name, name) != 0)
2566 continue;
2567
2568 if (syms[k].st_shndx == SHN_UNDEF ||
2569 syms[k].st_shndx >= ehdr->e_shnum) {
2570 pr_debug("Symbol: %s has bad section index %d.\n",
2571 name, syms[k].st_shndx);
2572 return NULL;
2573 }
2574
2575 /* Found the symbol we are looking for */
2576 return &syms[k];
2577 }
2578 }
2579
2580 return NULL;
2581}
2582
2583void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name)
2584{
2585 struct purgatory_info *pi = &image->purgatory_info;
2586 Elf_Sym *sym;
2587 Elf_Shdr *sechdr;
2588
2589 sym = kexec_purgatory_find_symbol(pi, name);
2590 if (!sym)
2591 return ERR_PTR(-EINVAL);
2592
2593 sechdr = &pi->sechdrs[sym->st_shndx];
2594
2595 /*
2596 * Returns the address where symbol will finally be loaded after
2597 * kexec_load_segment()
2598 */
2599 return (void *)(sechdr->sh_addr + sym->st_value);
2600}
2601
2602/*
2603 * Get or set value of a symbol. If "get_value" is true, symbol value is
2604 * returned in buf otherwise symbol value is set based on value in buf.
2605 */
2606int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name,
2607 void *buf, unsigned int size, bool get_value)
2608{
2609 Elf_Sym *sym;
2610 Elf_Shdr *sechdrs;
2611 struct purgatory_info *pi = &image->purgatory_info;
2612 char *sym_buf;
2613
2614 sym = kexec_purgatory_find_symbol(pi, name);
2615 if (!sym)
2616 return -EINVAL;
2617
2618 if (sym->st_size != size) {
2619 pr_err("symbol %s size mismatch: expected %lu actual %u\n",
2620 name, (unsigned long)sym->st_size, size);
2621 return -EINVAL;
2622 }
2623
2624 sechdrs = pi->sechdrs;
2625
2626 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
2627 pr_err("symbol %s is in a bss section. Cannot %s\n", name,
2628 get_value ? "get" : "set");
2629 return -EINVAL;
2630 }
2631
2632 sym_buf = (unsigned char *)sechdrs[sym->st_shndx].sh_offset +
2633 sym->st_value;
2634
2635 if (get_value)
2636 memcpy((void *)buf, sym_buf, size);
2637 else
2638 memcpy((void *)sym_buf, buf, size);
2639
2640 return 0;
2641}
Vivek Goyalcb105252014-08-08 14:25:57 -07002642
Huang Ying7ade3fc2008-08-15 00:40:21 -07002643/*
2644 * Move into place and start executing a preloaded standalone
2645 * executable. If nothing was preloaded return an error.
Huang Ying3ab83522008-07-25 19:45:07 -07002646 */
2647int kernel_kexec(void)
2648{
2649 int error = 0;
2650
Andrew Morton8c5a1cf2008-08-15 00:40:27 -07002651 if (!mutex_trylock(&kexec_mutex))
Huang Ying3ab83522008-07-25 19:45:07 -07002652 return -EBUSY;
2653 if (!kexec_image) {
2654 error = -EINVAL;
2655 goto Unlock;
2656 }
2657
Huang Ying3ab83522008-07-25 19:45:07 -07002658#ifdef CONFIG_KEXEC_JUMP
Huang Ying7ade3fc2008-08-15 00:40:21 -07002659 if (kexec_image->preserve_context) {
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01002660 lock_system_sleep();
Huang Ying89081d12008-07-25 19:45:10 -07002661 pm_prepare_console();
2662 error = freeze_processes();
2663 if (error) {
2664 error = -EBUSY;
2665 goto Restore_console;
2666 }
2667 suspend_console();
Alan Sternd1616302009-05-24 22:05:42 +02002668 error = dpm_suspend_start(PMSG_FREEZE);
Huang Ying89081d12008-07-25 19:45:10 -07002669 if (error)
2670 goto Resume_console;
Alan Sternd1616302009-05-24 22:05:42 +02002671 /* At this point, dpm_suspend_start() has been called,
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01002672 * but *not* dpm_suspend_end(). We *must* call
2673 * dpm_suspend_end() now. Otherwise, drivers for
Huang Ying89081d12008-07-25 19:45:10 -07002674 * some devices (e.g. interrupt controllers) become
2675 * desynchronized with the actual state of the
2676 * hardware at resume time, and evil weirdness ensues.
2677 */
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01002678 error = dpm_suspend_end(PMSG_FREEZE);
Huang Ying89081d12008-07-25 19:45:10 -07002679 if (error)
Rafael J. Wysocki749b0af2009-03-16 22:34:35 +01002680 goto Resume_devices;
2681 error = disable_nonboot_cpus();
2682 if (error)
2683 goto Enable_cpus;
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +01002684 local_irq_disable();
Rafael J. Wysocki2e711c02011-04-26 19:15:07 +02002685 error = syscore_suspend();
Rafael J. Wysocki770824b2009-02-22 18:38:50 +01002686 if (error)
Rafael J. Wysocki749b0af2009-03-16 22:34:35 +01002687 goto Enable_irqs;
Huang Ying7ade3fc2008-08-15 00:40:21 -07002688 } else
Huang Ying3ab83522008-07-25 19:45:07 -07002689#endif
Huang Ying7ade3fc2008-08-15 00:40:21 -07002690 {
Khalid Aziz4fc9bbf2013-11-27 15:19:25 -07002691 kexec_in_progress = true;
Huang Yingca195b72008-08-15 00:40:24 -07002692 kernel_restart_prepare(NULL);
Vivek Goyalc97102b2013-12-18 17:08:31 -08002693 migrate_to_reboot_cpu();
Srivatsa S. Bhat011e4b02014-05-27 16:25:34 +05302694
2695 /*
2696 * migrate_to_reboot_cpu() disables CPU hotplug assuming that
2697 * no further code needs to use CPU hotplug (which is true in
2698 * the reboot case). However, the kexec path depends on using
2699 * CPU hotplug again; so re-enable it here.
2700 */
2701 cpu_hotplug_enable();
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07002702 pr_emerg("Starting new kernel\n");
Huang Ying3ab83522008-07-25 19:45:07 -07002703 machine_shutdown();
2704 }
2705
2706 machine_kexec(kexec_image);
2707
Huang Ying3ab83522008-07-25 19:45:07 -07002708#ifdef CONFIG_KEXEC_JUMP
Huang Ying7ade3fc2008-08-15 00:40:21 -07002709 if (kexec_image->preserve_context) {
Rafael J. Wysocki19234c02011-04-20 00:36:11 +02002710 syscore_resume();
Rafael J. Wysocki749b0af2009-03-16 22:34:35 +01002711 Enable_irqs:
Huang Ying3ab83522008-07-25 19:45:07 -07002712 local_irq_enable();
Rafael J. Wysocki749b0af2009-03-16 22:34:35 +01002713 Enable_cpus:
Huang Ying89081d12008-07-25 19:45:10 -07002714 enable_nonboot_cpus();
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01002715 dpm_resume_start(PMSG_RESTORE);
Huang Ying89081d12008-07-25 19:45:10 -07002716 Resume_devices:
Alan Sternd1616302009-05-24 22:05:42 +02002717 dpm_resume_end(PMSG_RESTORE);
Huang Ying89081d12008-07-25 19:45:10 -07002718 Resume_console:
2719 resume_console();
2720 thaw_processes();
2721 Restore_console:
2722 pm_restore_console();
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01002723 unlock_system_sleep();
Huang Ying3ab83522008-07-25 19:45:07 -07002724 }
Huang Ying7ade3fc2008-08-15 00:40:21 -07002725#endif
Huang Ying3ab83522008-07-25 19:45:07 -07002726
2727 Unlock:
Andrew Morton8c5a1cf2008-08-15 00:40:27 -07002728 mutex_unlock(&kexec_mutex);
Huang Ying3ab83522008-07-25 19:45:07 -07002729 return error;
2730}