blob: 35dcac4b5c1c23474e423de65949c38c5d2805d8 [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
Vivek Goyal74ca3172014-08-29 15:18:46 -070067#ifdef CONFIG_KEXEC_FILE
Vivek Goyal12db5562014-08-08 14:26:04 -070068static int kexec_calculate_store_digests(struct kimage *image);
Vivek Goyal74ca3172014-08-29 15:18:46 -070069#endif
Vivek Goyal12db5562014-08-08 14:26:04 -070070
Eric W. Biedermandc009d92005-06-25 14:57:52 -070071/* Location of the reserved area for the crash kernel */
72struct resource crashk_res = {
73 .name = "Crash kernel",
74 .start = 0,
75 .end = 0,
76 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
77};
Yinghai Lu0212f912013-01-24 12:20:11 -080078struct resource crashk_low_res = {
Yinghai Lu157752d2013-04-15 22:23:46 -070079 .name = "Crash kernel",
Yinghai Lu0212f912013-01-24 12:20:11 -080080 .start = 0,
81 .end = 0,
82 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
83};
Eric W. Biedermandc009d92005-06-25 14:57:52 -070084
Alexander Nyberg6e274d12005-06-25 14:58:26 -070085int kexec_should_crash(struct task_struct *p)
86{
Serge E. Hallynb460cbc2007-10-18 23:39:52 -070087 if (in_interrupt() || !p->pid || is_global_init(p) || panic_on_oops)
Alexander Nyberg6e274d12005-06-25 14:58:26 -070088 return 1;
89 return 0;
90}
91
Eric W. Biedermandc009d92005-06-25 14:57:52 -070092/*
93 * When kexec transitions to the new kernel there is a one-to-one
94 * mapping between physical and virtual addresses. On processors
95 * where you can disable the MMU this is trivial, and easy. For
96 * others it is still a simple predictable page table to setup.
97 *
98 * In that environment kexec copies the new kernel to its final
99 * resting place. This means I can only support memory whose
100 * physical address can fit in an unsigned long. In particular
101 * addresses where (pfn << PAGE_SHIFT) > ULONG_MAX cannot be handled.
102 * If the assembly stub has more restrictive requirements
103 * KEXEC_SOURCE_MEMORY_LIMIT and KEXEC_DEST_MEMORY_LIMIT can be
104 * defined more restrictively in <asm/kexec.h>.
105 *
106 * The code for the transition from the current kernel to the
107 * the new kernel is placed in the control_code_buffer, whose size
Huang Ying163f6872008-08-15 00:40:22 -0700108 * is given by KEXEC_CONTROL_PAGE_SIZE. In the best case only a single
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700109 * page of memory is necessary, but some architectures require more.
110 * Because this memory must be identity mapped in the transition from
111 * virtual to physical addresses it must live in the range
112 * 0 - TASK_SIZE, as only the user space mappings are arbitrarily
113 * modifiable.
114 *
115 * The assembly stub in the control code buffer is passed a linked list
116 * of descriptor pages detailing the source pages of the new kernel,
117 * and the destination addresses of those source pages. As this data
118 * structure is not used in the context of the current OS, it must
119 * be self-contained.
120 *
121 * The code has been made to work with highmem pages and will use a
122 * destination page in its final resting place (if it happens
123 * to allocate it). The end product of this is that most of the
124 * physical address space, and most of RAM can be used.
125 *
126 * Future directions include:
127 * - allocating a page table with the control code buffer identity
128 * mapped, to simplify machine_kexec and make kexec_on_panic more
129 * reliable.
130 */
131
132/*
133 * KIMAGE_NO_DEST is an impossible destination address..., for
134 * allocating pages whose destination address we do not care about.
135 */
136#define KIMAGE_NO_DEST (-1UL)
137
Maneesh Soni72414d32005-06-25 14:58:28 -0700138static int kimage_is_destination_range(struct kimage *image,
139 unsigned long start, unsigned long end);
140static struct page *kimage_alloc_page(struct kimage *image,
Al Viro9796fdd2005-10-21 03:22:03 -0400141 gfp_t gfp_mask,
Maneesh Soni72414d32005-06-25 14:58:28 -0700142 unsigned long dest);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700143
Vivek Goyaldabe7862014-08-08 14:25:45 -0700144static int copy_user_segment_list(struct kimage *image,
145 unsigned long nr_segments,
146 struct kexec_segment __user *segments)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700147{
Vivek Goyaldabe7862014-08-08 14:25:45 -0700148 int ret;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700149 size_t segment_bytes;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700150
151 /* Read in the segments */
152 image->nr_segments = nr_segments;
153 segment_bytes = nr_segments * sizeof(*segments);
Vivek Goyaldabe7862014-08-08 14:25:45 -0700154 ret = copy_from_user(image->segment, segments, segment_bytes);
155 if (ret)
156 ret = -EFAULT;
157
158 return ret;
159}
160
161static int sanity_check_segment_list(struct kimage *image)
162{
163 int result, i;
164 unsigned long nr_segments = image->nr_segments;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700165
166 /*
167 * Verify we have good destination addresses. The caller is
168 * responsible for making certain we don't attempt to load
169 * the new image into invalid or reserved areas of RAM. This
170 * just verifies it is an address we can use.
171 *
172 * Since the kernel does everything in page size chunks ensure
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400173 * the destination addresses are page aligned. Too many
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700174 * special cases crop of when we don't do this. The most
175 * insidious is getting overlapping destination addresses
176 * simply because addresses are changed to page size
177 * granularity.
178 */
179 result = -EADDRNOTAVAIL;
180 for (i = 0; i < nr_segments; i++) {
181 unsigned long mstart, mend;
Maneesh Soni72414d32005-06-25 14:58:28 -0700182
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700183 mstart = image->segment[i].mem;
184 mend = mstart + image->segment[i].memsz;
185 if ((mstart & ~PAGE_MASK) || (mend & ~PAGE_MASK))
Vivek Goyaldabe7862014-08-08 14:25:45 -0700186 return result;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700187 if (mend >= KEXEC_DESTINATION_MEMORY_LIMIT)
Vivek Goyaldabe7862014-08-08 14:25:45 -0700188 return result;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700189 }
190
191 /* Verify our destination addresses do not overlap.
192 * If we alloed overlapping destination addresses
193 * through very weird things can happen with no
194 * easy explanation as one segment stops on another.
195 */
196 result = -EINVAL;
Maneesh Soni72414d32005-06-25 14:58:28 -0700197 for (i = 0; i < nr_segments; i++) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700198 unsigned long mstart, mend;
199 unsigned long j;
Maneesh Soni72414d32005-06-25 14:58:28 -0700200
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700201 mstart = image->segment[i].mem;
202 mend = mstart + image->segment[i].memsz;
Maneesh Soni72414d32005-06-25 14:58:28 -0700203 for (j = 0; j < i; j++) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700204 unsigned long pstart, pend;
205 pstart = image->segment[j].mem;
206 pend = pstart + image->segment[j].memsz;
207 /* Do the segments overlap ? */
208 if ((mend > pstart) && (mstart < pend))
Vivek Goyaldabe7862014-08-08 14:25:45 -0700209 return result;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700210 }
211 }
212
213 /* Ensure our buffer sizes are strictly less than
214 * our memory sizes. This should always be the case,
215 * and it is easier to check up front than to be surprised
216 * later on.
217 */
218 result = -EINVAL;
Maneesh Soni72414d32005-06-25 14:58:28 -0700219 for (i = 0; i < nr_segments; i++) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700220 if (image->segment[i].bufsz > image->segment[i].memsz)
Vivek Goyaldabe7862014-08-08 14:25:45 -0700221 return result;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700222 }
223
Vivek Goyaldabe7862014-08-08 14:25:45 -0700224 /*
225 * Verify we have good destination addresses. Normally
226 * the caller is responsible for making certain we don't
227 * attempt to load the new image into invalid or reserved
228 * areas of RAM. But crash kernels are preloaded into a
229 * reserved area of ram. We must ensure the addresses
230 * are in the reserved area otherwise preloading the
231 * kernel could corrupt things.
232 */
Maneesh Soni72414d32005-06-25 14:58:28 -0700233
Vivek Goyaldabe7862014-08-08 14:25:45 -0700234 if (image->type == KEXEC_TYPE_CRASH) {
235 result = -EADDRNOTAVAIL;
236 for (i = 0; i < nr_segments; i++) {
237 unsigned long mstart, mend;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700238
Vivek Goyaldabe7862014-08-08 14:25:45 -0700239 mstart = image->segment[i].mem;
240 mend = mstart + image->segment[i].memsz - 1;
241 /* Ensure we are within the crash kernel limits */
242 if ((mstart < crashk_res.start) ||
243 (mend > crashk_res.end))
244 return result;
245 }
246 }
247
248 return 0;
249}
250
251static struct kimage *do_kimage_alloc_init(void)
252{
253 struct kimage *image;
254
255 /* Allocate a controlling structure */
256 image = kzalloc(sizeof(*image), GFP_KERNEL);
257 if (!image)
258 return NULL;
259
260 image->head = 0;
261 image->entry = &image->head;
262 image->last_entry = &image->head;
263 image->control_page = ~0; /* By default this does not apply */
264 image->type = KEXEC_TYPE_DEFAULT;
265
266 /* Initialize the list of control pages */
267 INIT_LIST_HEAD(&image->control_pages);
268
269 /* Initialize the list of destination pages */
270 INIT_LIST_HEAD(&image->dest_pages);
271
272 /* Initialize the list of unusable pages */
273 INIT_LIST_HEAD(&image->unusable_pages);
274
275 return image;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700276}
277
Zhang Yanfeib92e7e02013-02-27 17:03:29 -0800278static void kimage_free_page_list(struct list_head *list);
279
Vivek Goyal255aedd2014-08-08 14:25:48 -0700280static int kimage_alloc_init(struct kimage **rimage, unsigned long entry,
281 unsigned long nr_segments,
282 struct kexec_segment __user *segments,
283 unsigned long flags)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700284{
Vivek Goyal255aedd2014-08-08 14:25:48 -0700285 int ret;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700286 struct kimage *image;
Vivek Goyal255aedd2014-08-08 14:25:48 -0700287 bool kexec_on_panic = flags & KEXEC_ON_CRASH;
288
289 if (kexec_on_panic) {
290 /* Verify we have a valid entry point */
291 if ((entry < crashk_res.start) || (entry > crashk_res.end))
292 return -EADDRNOTAVAIL;
293 }
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700294
295 /* Allocate and initialize a controlling structure */
Vivek Goyaldabe7862014-08-08 14:25:45 -0700296 image = do_kimage_alloc_init();
297 if (!image)
298 return -ENOMEM;
299
300 image->start = entry;
301
Vivek Goyal255aedd2014-08-08 14:25:48 -0700302 ret = copy_user_segment_list(image, nr_segments, segments);
303 if (ret)
Vivek Goyaldabe7862014-08-08 14:25:45 -0700304 goto out_free_image;
305
Vivek Goyal255aedd2014-08-08 14:25:48 -0700306 ret = sanity_check_segment_list(image);
307 if (ret)
Vivek Goyaldabe7862014-08-08 14:25:45 -0700308 goto out_free_image;
Maneesh Soni72414d32005-06-25 14:58:28 -0700309
Vivek Goyal255aedd2014-08-08 14:25:48 -0700310 /* Enable the special crash kernel control page allocation policy. */
311 if (kexec_on_panic) {
312 image->control_page = crashk_res.start;
313 image->type = KEXEC_TYPE_CRASH;
314 }
315
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700316 /*
317 * Find a location for the control code buffer, and add it
318 * the vector of segments so that it's pages will also be
319 * counted as destination pages.
320 */
Vivek Goyal255aedd2014-08-08 14:25:48 -0700321 ret = -ENOMEM;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700322 image->control_code_page = kimage_alloc_control_pages(image,
Huang Ying163f6872008-08-15 00:40:22 -0700323 get_order(KEXEC_CONTROL_PAGE_SIZE));
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700324 if (!image->control_code_page) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -0700325 pr_err("Could not allocate control_code_buffer\n");
Vivek Goyaldabe7862014-08-08 14:25:45 -0700326 goto out_free_image;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700327 }
328
Vivek Goyal255aedd2014-08-08 14:25:48 -0700329 if (!kexec_on_panic) {
330 image->swap_page = kimage_alloc_control_pages(image, 0);
331 if (!image->swap_page) {
332 pr_err("Could not allocate swap buffer\n");
333 goto out_free_control_pages;
334 }
Huang Ying3ab83522008-07-25 19:45:07 -0700335 }
336
Zhang Yanfeib92e7e02013-02-27 17:03:29 -0800337 *rimage = image;
338 return 0;
Vivek Goyaldabe7862014-08-08 14:25:45 -0700339out_free_control_pages:
Zhang Yanfeib92e7e02013-02-27 17:03:29 -0800340 kimage_free_page_list(&image->control_pages);
Vivek Goyaldabe7862014-08-08 14:25:45 -0700341out_free_image:
Zhang Yanfeib92e7e02013-02-27 17:03:29 -0800342 kfree(image);
Vivek Goyal255aedd2014-08-08 14:25:48 -0700343 return ret;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700344}
345
Vivek Goyal74ca3172014-08-29 15:18:46 -0700346#ifdef CONFIG_KEXEC_FILE
Vivek Goyalcb105252014-08-08 14:25:57 -0700347static int copy_file_from_fd(int fd, void **buf, unsigned long *buf_len)
348{
349 struct fd f = fdget(fd);
350 int ret;
351 struct kstat stat;
352 loff_t pos;
353 ssize_t bytes = 0;
354
355 if (!f.file)
356 return -EBADF;
357
358 ret = vfs_getattr(&f.file->f_path, &stat);
359 if (ret)
360 goto out;
361
362 if (stat.size > INT_MAX) {
363 ret = -EFBIG;
364 goto out;
365 }
366
367 /* Don't hand 0 to vmalloc, it whines. */
368 if (stat.size == 0) {
369 ret = -EINVAL;
370 goto out;
371 }
372
373 *buf = vmalloc(stat.size);
374 if (!*buf) {
375 ret = -ENOMEM;
376 goto out;
377 }
378
379 pos = 0;
380 while (pos < stat.size) {
381 bytes = kernel_read(f.file, pos, (char *)(*buf) + pos,
382 stat.size - pos);
383 if (bytes < 0) {
384 vfree(*buf);
385 ret = bytes;
386 goto out;
387 }
388
389 if (bytes == 0)
390 break;
391 pos += bytes;
392 }
393
394 if (pos != stat.size) {
395 ret = -EBADF;
396 vfree(*buf);
397 goto out;
398 }
399
400 *buf_len = pos;
401out:
402 fdput(f);
403 return ret;
404}
405
406/* Architectures can provide this probe function */
407int __weak arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
408 unsigned long buf_len)
409{
410 return -ENOEXEC;
411}
412
413void * __weak arch_kexec_kernel_image_load(struct kimage *image)
414{
415 return ERR_PTR(-ENOEXEC);
416}
417
418void __weak arch_kimage_file_post_load_cleanup(struct kimage *image)
419{
420}
421
Vivek Goyal8e7d8382014-08-08 14:26:13 -0700422int __weak arch_kexec_kernel_verify_sig(struct kimage *image, void *buf,
423 unsigned long buf_len)
424{
425 return -EKEYREJECTED;
426}
427
Vivek Goyal12db5562014-08-08 14:26:04 -0700428/* Apply relocations of type RELA */
429int __weak
430arch_kexec_apply_relocations_add(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
431 unsigned int relsec)
432{
433 pr_err("RELA relocation unsupported.\n");
434 return -ENOEXEC;
435}
436
437/* Apply relocations of type REL */
438int __weak
439arch_kexec_apply_relocations(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
440 unsigned int relsec)
441{
442 pr_err("REL relocation unsupported.\n");
443 return -ENOEXEC;
444}
445
Vivek Goyalcb105252014-08-08 14:25:57 -0700446/*
447 * Free up memory used by kernel, initrd, and comand line. This is temporary
448 * memory allocation which is not needed any more after these buffers have
449 * been loaded into separate segments and have been copied elsewhere.
450 */
451static void kimage_file_post_load_cleanup(struct kimage *image)
452{
Vivek Goyal12db5562014-08-08 14:26:04 -0700453 struct purgatory_info *pi = &image->purgatory_info;
454
Vivek Goyalcb105252014-08-08 14:25:57 -0700455 vfree(image->kernel_buf);
456 image->kernel_buf = NULL;
457
458 vfree(image->initrd_buf);
459 image->initrd_buf = NULL;
460
461 kfree(image->cmdline_buf);
462 image->cmdline_buf = NULL;
463
Vivek Goyal12db5562014-08-08 14:26:04 -0700464 vfree(pi->purgatory_buf);
465 pi->purgatory_buf = NULL;
466
467 vfree(pi->sechdrs);
468 pi->sechdrs = NULL;
469
Vivek Goyalcb105252014-08-08 14:25:57 -0700470 /* See if architecture has anything to cleanup post load */
471 arch_kimage_file_post_load_cleanup(image);
Vivek Goyal27f48d32014-08-08 14:26:06 -0700472
473 /*
474 * Above call should have called into bootloader to free up
475 * any data stored in kimage->image_loader_data. It should
476 * be ok now to free it up.
477 */
478 kfree(image->image_loader_data);
479 image->image_loader_data = NULL;
Vivek Goyalcb105252014-08-08 14:25:57 -0700480}
481
482/*
483 * In file mode list of segments is prepared by kernel. Copy relevant
484 * data from user space, do error checking, prepare segment list
485 */
486static int
487kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd,
488 const char __user *cmdline_ptr,
489 unsigned long cmdline_len, unsigned flags)
490{
491 int ret = 0;
492 void *ldata;
493
494 ret = copy_file_from_fd(kernel_fd, &image->kernel_buf,
495 &image->kernel_buf_len);
496 if (ret)
497 return ret;
498
499 /* Call arch image probe handlers */
500 ret = arch_kexec_kernel_image_probe(image, image->kernel_buf,
501 image->kernel_buf_len);
502
503 if (ret)
504 goto out;
505
Vivek Goyal8e7d8382014-08-08 14:26:13 -0700506#ifdef CONFIG_KEXEC_VERIFY_SIG
507 ret = arch_kexec_kernel_verify_sig(image, image->kernel_buf,
508 image->kernel_buf_len);
509 if (ret) {
510 pr_debug("kernel signature verification failed.\n");
511 goto out;
512 }
513 pr_debug("kernel signature verification successful.\n");
514#endif
Vivek Goyalcb105252014-08-08 14:25:57 -0700515 /* It is possible that there no initramfs is being loaded */
516 if (!(flags & KEXEC_FILE_NO_INITRAMFS)) {
517 ret = copy_file_from_fd(initrd_fd, &image->initrd_buf,
518 &image->initrd_buf_len);
519 if (ret)
520 goto out;
521 }
522
523 if (cmdline_len) {
524 image->cmdline_buf = kzalloc(cmdline_len, GFP_KERNEL);
525 if (!image->cmdline_buf) {
526 ret = -ENOMEM;
527 goto out;
528 }
529
530 ret = copy_from_user(image->cmdline_buf, cmdline_ptr,
531 cmdline_len);
532 if (ret) {
533 ret = -EFAULT;
534 goto out;
535 }
536
537 image->cmdline_buf_len = cmdline_len;
538
539 /* command line should be a string with last byte null */
540 if (image->cmdline_buf[cmdline_len - 1] != '\0') {
541 ret = -EINVAL;
542 goto out;
543 }
544 }
545
546 /* Call arch image load handlers */
547 ldata = arch_kexec_kernel_image_load(image);
548
549 if (IS_ERR(ldata)) {
550 ret = PTR_ERR(ldata);
551 goto out;
552 }
553
554 image->image_loader_data = ldata;
555out:
556 /* In case of error, free up all allocated memory in this function */
557 if (ret)
558 kimage_file_post_load_cleanup(image);
559 return ret;
560}
561
562static int
563kimage_file_alloc_init(struct kimage **rimage, int kernel_fd,
564 int initrd_fd, const char __user *cmdline_ptr,
565 unsigned long cmdline_len, unsigned long flags)
566{
567 int ret;
568 struct kimage *image;
Vivek Goyaldd5f7262014-08-08 14:26:09 -0700569 bool kexec_on_panic = flags & KEXEC_FILE_ON_CRASH;
Vivek Goyalcb105252014-08-08 14:25:57 -0700570
571 image = do_kimage_alloc_init();
572 if (!image)
573 return -ENOMEM;
574
575 image->file_mode = 1;
576
Vivek Goyaldd5f7262014-08-08 14:26:09 -0700577 if (kexec_on_panic) {
578 /* Enable special crash kernel control page alloc policy. */
579 image->control_page = crashk_res.start;
580 image->type = KEXEC_TYPE_CRASH;
581 }
582
Vivek Goyalcb105252014-08-08 14:25:57 -0700583 ret = kimage_file_prepare_segments(image, kernel_fd, initrd_fd,
584 cmdline_ptr, cmdline_len, flags);
585 if (ret)
586 goto out_free_image;
587
588 ret = sanity_check_segment_list(image);
589 if (ret)
590 goto out_free_post_load_bufs;
591
592 ret = -ENOMEM;
593 image->control_code_page = kimage_alloc_control_pages(image,
594 get_order(KEXEC_CONTROL_PAGE_SIZE));
595 if (!image->control_code_page) {
596 pr_err("Could not allocate control_code_buffer\n");
597 goto out_free_post_load_bufs;
598 }
599
Vivek Goyaldd5f7262014-08-08 14:26:09 -0700600 if (!kexec_on_panic) {
601 image->swap_page = kimage_alloc_control_pages(image, 0);
602 if (!image->swap_page) {
Masanari Iidad5393952014-12-12 16:57:41 -0800603 pr_err("Could not allocate swap buffer\n");
Vivek Goyaldd5f7262014-08-08 14:26:09 -0700604 goto out_free_control_pages;
605 }
Vivek Goyalcb105252014-08-08 14:25:57 -0700606 }
607
608 *rimage = image;
609 return 0;
610out_free_control_pages:
611 kimage_free_page_list(&image->control_pages);
612out_free_post_load_bufs:
613 kimage_file_post_load_cleanup(image);
Vivek Goyalcb105252014-08-08 14:25:57 -0700614out_free_image:
615 kfree(image);
616 return ret;
617}
Vivek Goyal74ca3172014-08-29 15:18:46 -0700618#else /* CONFIG_KEXEC_FILE */
619static inline void kimage_file_post_load_cleanup(struct kimage *image) { }
620#endif /* CONFIG_KEXEC_FILE */
Vivek Goyalcb105252014-08-08 14:25:57 -0700621
Maneesh Soni72414d32005-06-25 14:58:28 -0700622static int kimage_is_destination_range(struct kimage *image,
623 unsigned long start,
624 unsigned long end)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700625{
626 unsigned long i;
627
628 for (i = 0; i < image->nr_segments; i++) {
629 unsigned long mstart, mend;
Maneesh Soni72414d32005-06-25 14:58:28 -0700630
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700631 mstart = image->segment[i].mem;
Maneesh Soni72414d32005-06-25 14:58:28 -0700632 mend = mstart + image->segment[i].memsz;
633 if ((end > mstart) && (start < mend))
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700634 return 1;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700635 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700636
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700637 return 0;
638}
639
Al Viro9796fdd2005-10-21 03:22:03 -0400640static struct page *kimage_alloc_pages(gfp_t gfp_mask, unsigned int order)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700641{
642 struct page *pages;
Maneesh Soni72414d32005-06-25 14:58:28 -0700643
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700644 pages = alloc_pages(gfp_mask, order);
645 if (pages) {
646 unsigned int count, i;
647 pages->mapping = NULL;
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700648 set_page_private(pages, order);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700649 count = 1 << order;
Maneesh Soni72414d32005-06-25 14:58:28 -0700650 for (i = 0; i < count; i++)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700651 SetPageReserved(pages + i);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700652 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700653
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700654 return pages;
655}
656
657static void kimage_free_pages(struct page *page)
658{
659 unsigned int order, count, i;
Maneesh Soni72414d32005-06-25 14:58:28 -0700660
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700661 order = page_private(page);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700662 count = 1 << order;
Maneesh Soni72414d32005-06-25 14:58:28 -0700663 for (i = 0; i < count; i++)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700664 ClearPageReserved(page + i);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700665 __free_pages(page, order);
666}
667
668static void kimage_free_page_list(struct list_head *list)
669{
670 struct list_head *pos, *next;
Maneesh Soni72414d32005-06-25 14:58:28 -0700671
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700672 list_for_each_safe(pos, next, list) {
673 struct page *page;
674
675 page = list_entry(pos, struct page, lru);
676 list_del(&page->lru);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700677 kimage_free_pages(page);
678 }
679}
680
Maneesh Soni72414d32005-06-25 14:58:28 -0700681static struct page *kimage_alloc_normal_control_pages(struct kimage *image,
682 unsigned int order)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700683{
684 /* Control pages are special, they are the intermediaries
685 * that are needed while we copy the rest of the pages
686 * to their final resting place. As such they must
687 * not conflict with either the destination addresses
688 * or memory the kernel is already using.
689 *
690 * The only case where we really need more than one of
691 * these are for architectures where we cannot disable
692 * the MMU and must instead generate an identity mapped
693 * page table for all of the memory.
694 *
695 * At worst this runs in O(N) of the image size.
696 */
697 struct list_head extra_pages;
698 struct page *pages;
699 unsigned int count;
700
701 count = 1 << order;
702 INIT_LIST_HEAD(&extra_pages);
703
704 /* Loop while I can allocate a page and the page allocated
705 * is a destination page.
706 */
707 do {
708 unsigned long pfn, epfn, addr, eaddr;
Maneesh Soni72414d32005-06-25 14:58:28 -0700709
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700710 pages = kimage_alloc_pages(GFP_KERNEL, order);
711 if (!pages)
712 break;
713 pfn = page_to_pfn(pages);
714 epfn = pfn + count;
715 addr = pfn << PAGE_SHIFT;
716 eaddr = epfn << PAGE_SHIFT;
717 if ((epfn >= (KEXEC_CONTROL_MEMORY_LIMIT >> PAGE_SHIFT)) ||
Maneesh Soni72414d32005-06-25 14:58:28 -0700718 kimage_is_destination_range(image, addr, eaddr)) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700719 list_add(&pages->lru, &extra_pages);
720 pages = NULL;
721 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700722 } while (!pages);
723
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700724 if (pages) {
725 /* Remember the allocated page... */
726 list_add(&pages->lru, &image->control_pages);
727
728 /* Because the page is already in it's destination
729 * location we will never allocate another page at
730 * that address. Therefore kimage_alloc_pages
731 * will not return it (again) and we don't need
732 * to give it an entry in image->segment[].
733 */
734 }
735 /* Deal with the destination pages I have inadvertently allocated.
736 *
737 * Ideally I would convert multi-page allocations into single
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300738 * page allocations, and add everything to image->dest_pages.
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700739 *
740 * For now it is simpler to just free the pages.
741 */
742 kimage_free_page_list(&extra_pages);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700743
Maneesh Soni72414d32005-06-25 14:58:28 -0700744 return pages;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700745}
746
Maneesh Soni72414d32005-06-25 14:58:28 -0700747static struct page *kimage_alloc_crash_control_pages(struct kimage *image,
748 unsigned int order)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700749{
750 /* Control pages are special, they are the intermediaries
751 * that are needed while we copy the rest of the pages
752 * to their final resting place. As such they must
753 * not conflict with either the destination addresses
754 * or memory the kernel is already using.
755 *
756 * Control pages are also the only pags we must allocate
757 * when loading a crash kernel. All of the other pages
758 * are specified by the segments and we just memcpy
759 * into them directly.
760 *
761 * The only case where we really need more than one of
762 * these are for architectures where we cannot disable
763 * the MMU and must instead generate an identity mapped
764 * page table for all of the memory.
765 *
766 * Given the low demand this implements a very simple
767 * allocator that finds the first hole of the appropriate
768 * size in the reserved memory region, and allocates all
769 * of the memory up to and including the hole.
770 */
771 unsigned long hole_start, hole_end, size;
772 struct page *pages;
Maneesh Soni72414d32005-06-25 14:58:28 -0700773
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700774 pages = NULL;
775 size = (1 << order) << PAGE_SHIFT;
776 hole_start = (image->control_page + (size - 1)) & ~(size - 1);
777 hole_end = hole_start + size - 1;
Maneesh Soni72414d32005-06-25 14:58:28 -0700778 while (hole_end <= crashk_res.end) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700779 unsigned long i;
Maneesh Soni72414d32005-06-25 14:58:28 -0700780
Michael Holzheu3d214fa2011-10-30 15:16:36 +0100781 if (hole_end > KEXEC_CRASH_CONTROL_MEMORY_LIMIT)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700782 break;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700783 /* See if I overlap any of the segments */
Maneesh Soni72414d32005-06-25 14:58:28 -0700784 for (i = 0; i < image->nr_segments; i++) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700785 unsigned long mstart, mend;
Maneesh Soni72414d32005-06-25 14:58:28 -0700786
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700787 mstart = image->segment[i].mem;
788 mend = mstart + image->segment[i].memsz - 1;
789 if ((hole_end >= mstart) && (hole_start <= mend)) {
790 /* Advance the hole to the end of the segment */
791 hole_start = (mend + (size - 1)) & ~(size - 1);
792 hole_end = hole_start + size - 1;
793 break;
794 }
795 }
796 /* If I don't overlap any segments I have found my hole! */
797 if (i == image->nr_segments) {
798 pages = pfn_to_page(hole_start >> PAGE_SHIFT);
799 break;
800 }
801 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700802 if (pages)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700803 image->control_page = hole_end;
Maneesh Soni72414d32005-06-25 14:58:28 -0700804
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700805 return pages;
806}
807
808
Maneesh Soni72414d32005-06-25 14:58:28 -0700809struct page *kimage_alloc_control_pages(struct kimage *image,
810 unsigned int order)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700811{
812 struct page *pages = NULL;
Maneesh Soni72414d32005-06-25 14:58:28 -0700813
814 switch (image->type) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700815 case KEXEC_TYPE_DEFAULT:
816 pages = kimage_alloc_normal_control_pages(image, order);
817 break;
818 case KEXEC_TYPE_CRASH:
819 pages = kimage_alloc_crash_control_pages(image, order);
820 break;
821 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700822
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700823 return pages;
824}
825
826static int kimage_add_entry(struct kimage *image, kimage_entry_t entry)
827{
Maneesh Soni72414d32005-06-25 14:58:28 -0700828 if (*image->entry != 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700829 image->entry++;
Maneesh Soni72414d32005-06-25 14:58:28 -0700830
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700831 if (image->entry == image->last_entry) {
832 kimage_entry_t *ind_page;
833 struct page *page;
Maneesh Soni72414d32005-06-25 14:58:28 -0700834
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700835 page = kimage_alloc_page(image, GFP_KERNEL, KIMAGE_NO_DEST);
Maneesh Soni72414d32005-06-25 14:58:28 -0700836 if (!page)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700837 return -ENOMEM;
Maneesh Soni72414d32005-06-25 14:58:28 -0700838
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700839 ind_page = page_address(page);
840 *image->entry = virt_to_phys(ind_page) | IND_INDIRECTION;
841 image->entry = ind_page;
Maneesh Soni72414d32005-06-25 14:58:28 -0700842 image->last_entry = ind_page +
843 ((PAGE_SIZE/sizeof(kimage_entry_t)) - 1);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700844 }
845 *image->entry = entry;
846 image->entry++;
847 *image->entry = 0;
Maneesh Soni72414d32005-06-25 14:58:28 -0700848
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700849 return 0;
850}
851
Maneesh Soni72414d32005-06-25 14:58:28 -0700852static int kimage_set_destination(struct kimage *image,
853 unsigned long destination)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700854{
855 int result;
856
857 destination &= PAGE_MASK;
858 result = kimage_add_entry(image, destination | IND_DESTINATION);
Maneesh Soni72414d32005-06-25 14:58:28 -0700859
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700860 return result;
861}
862
863
864static int kimage_add_page(struct kimage *image, unsigned long page)
865{
866 int result;
867
868 page &= PAGE_MASK;
869 result = kimage_add_entry(image, page | IND_SOURCE);
Maneesh Soni72414d32005-06-25 14:58:28 -0700870
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700871 return result;
872}
873
874
875static void kimage_free_extra_pages(struct kimage *image)
876{
877 /* Walk through and free any extra destination pages I may have */
878 kimage_free_page_list(&image->dest_pages);
879
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300880 /* Walk through and free any unusable pages I have cached */
Vivek Goyal7d3e2bc2014-08-08 14:25:43 -0700881 kimage_free_page_list(&image->unusable_pages);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700882
883}
WANG Cong7fccf032008-07-25 19:45:02 -0700884static void kimage_terminate(struct kimage *image)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700885{
Maneesh Soni72414d32005-06-25 14:58:28 -0700886 if (*image->entry != 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700887 image->entry++;
Maneesh Soni72414d32005-06-25 14:58:28 -0700888
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700889 *image->entry = IND_DONE;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700890}
891
892#define for_each_kimage_entry(image, ptr, entry) \
893 for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE); \
Fabian Fredericke1bebcf2014-06-06 14:37:09 -0700894 ptr = (entry & IND_INDIRECTION) ? \
895 phys_to_virt((entry & PAGE_MASK)) : ptr + 1)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700896
897static void kimage_free_entry(kimage_entry_t entry)
898{
899 struct page *page;
900
901 page = pfn_to_page(entry >> PAGE_SHIFT);
902 kimage_free_pages(page);
903}
904
905static void kimage_free(struct kimage *image)
906{
907 kimage_entry_t *ptr, entry;
908 kimage_entry_t ind = 0;
909
910 if (!image)
911 return;
Maneesh Soni72414d32005-06-25 14:58:28 -0700912
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700913 kimage_free_extra_pages(image);
914 for_each_kimage_entry(image, ptr, entry) {
915 if (entry & IND_INDIRECTION) {
916 /* Free the previous indirection page */
Maneesh Soni72414d32005-06-25 14:58:28 -0700917 if (ind & IND_INDIRECTION)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700918 kimage_free_entry(ind);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700919 /* Save this indirection page until we are
920 * done with it.
921 */
922 ind = entry;
Fabian Fredericke1bebcf2014-06-06 14:37:09 -0700923 } else if (entry & IND_SOURCE)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700924 kimage_free_entry(entry);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700925 }
926 /* Free the final indirection page */
Maneesh Soni72414d32005-06-25 14:58:28 -0700927 if (ind & IND_INDIRECTION)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700928 kimage_free_entry(ind);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700929
930 /* Handle any machine specific cleanup */
931 machine_kexec_cleanup(image);
932
933 /* Free the kexec control pages... */
934 kimage_free_page_list(&image->control_pages);
Vivek Goyalcb105252014-08-08 14:25:57 -0700935
Vivek Goyalcb105252014-08-08 14:25:57 -0700936 /*
937 * Free up any temporary buffers allocated. This might hit if
938 * error occurred much later after buffer allocation.
939 */
940 if (image->file_mode)
941 kimage_file_post_load_cleanup(image);
942
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700943 kfree(image);
944}
945
Maneesh Soni72414d32005-06-25 14:58:28 -0700946static kimage_entry_t *kimage_dst_used(struct kimage *image,
947 unsigned long page)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700948{
949 kimage_entry_t *ptr, entry;
950 unsigned long destination = 0;
951
952 for_each_kimage_entry(image, ptr, entry) {
Maneesh Soni72414d32005-06-25 14:58:28 -0700953 if (entry & IND_DESTINATION)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700954 destination = entry & PAGE_MASK;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700955 else if (entry & IND_SOURCE) {
Maneesh Soni72414d32005-06-25 14:58:28 -0700956 if (page == destination)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700957 return ptr;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700958 destination += PAGE_SIZE;
959 }
960 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700961
Alexey Dobriyan314b6a42005-06-27 22:29:33 -0700962 return NULL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700963}
964
Maneesh Soni72414d32005-06-25 14:58:28 -0700965static struct page *kimage_alloc_page(struct kimage *image,
Al Viro9796fdd2005-10-21 03:22:03 -0400966 gfp_t gfp_mask,
Maneesh Soni72414d32005-06-25 14:58:28 -0700967 unsigned long destination)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700968{
969 /*
970 * Here we implement safeguards to ensure that a source page
971 * is not copied to its destination page before the data on
972 * the destination page is no longer useful.
973 *
974 * To do this we maintain the invariant that a source page is
975 * either its own destination page, or it is not a
976 * destination page at all.
977 *
978 * That is slightly stronger than required, but the proof
979 * that no problems will not occur is trivial, and the
980 * implementation is simply to verify.
981 *
982 * When allocating all pages normally this algorithm will run
983 * in O(N) time, but in the worst case it will run in O(N^2)
984 * time. If the runtime is a problem the data structures can
985 * be fixed.
986 */
987 struct page *page;
988 unsigned long addr;
989
990 /*
991 * Walk through the list of destination pages, and see if I
992 * have a match.
993 */
994 list_for_each_entry(page, &image->dest_pages, lru) {
995 addr = page_to_pfn(page) << PAGE_SHIFT;
996 if (addr == destination) {
997 list_del(&page->lru);
998 return page;
999 }
1000 }
1001 page = NULL;
1002 while (1) {
1003 kimage_entry_t *old;
1004
1005 /* Allocate a page, if we run out of memory give up */
1006 page = kimage_alloc_pages(gfp_mask, 0);
Maneesh Soni72414d32005-06-25 14:58:28 -07001007 if (!page)
Alexey Dobriyan314b6a42005-06-27 22:29:33 -07001008 return NULL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001009 /* If the page cannot be used file it away */
Maneesh Soni72414d32005-06-25 14:58:28 -07001010 if (page_to_pfn(page) >
1011 (KEXEC_SOURCE_MEMORY_LIMIT >> PAGE_SHIFT)) {
Vivek Goyal7d3e2bc2014-08-08 14:25:43 -07001012 list_add(&page->lru, &image->unusable_pages);
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001013 continue;
1014 }
1015 addr = page_to_pfn(page) << PAGE_SHIFT;
1016
1017 /* If it is the destination page we want use it */
1018 if (addr == destination)
1019 break;
1020
1021 /* If the page is not a destination page use it */
Maneesh Soni72414d32005-06-25 14:58:28 -07001022 if (!kimage_is_destination_range(image, addr,
1023 addr + PAGE_SIZE))
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001024 break;
1025
1026 /*
1027 * I know that the page is someones destination page.
1028 * See if there is already a source page for this
1029 * destination page. And if so swap the source pages.
1030 */
1031 old = kimage_dst_used(image, addr);
1032 if (old) {
1033 /* If so move it */
1034 unsigned long old_addr;
1035 struct page *old_page;
1036
1037 old_addr = *old & PAGE_MASK;
1038 old_page = pfn_to_page(old_addr >> PAGE_SHIFT);
1039 copy_highpage(page, old_page);
1040 *old = addr | (*old & ~PAGE_MASK);
1041
1042 /* The old page I have found cannot be a
Jonathan Steelf9092f32008-09-22 13:57:45 -07001043 * destination page, so return it if it's
1044 * gfp_flags honor the ones passed in.
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001045 */
Jonathan Steelf9092f32008-09-22 13:57:45 -07001046 if (!(gfp_mask & __GFP_HIGHMEM) &&
1047 PageHighMem(old_page)) {
1048 kimage_free_pages(old_page);
1049 continue;
1050 }
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001051 addr = old_addr;
1052 page = old_page;
1053 break;
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001054 } else {
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001055 /* Place the page on the destination list I
1056 * will use it later.
1057 */
1058 list_add(&page->lru, &image->dest_pages);
1059 }
1060 }
Maneesh Soni72414d32005-06-25 14:58:28 -07001061
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001062 return page;
1063}
1064
1065static int kimage_load_normal_segment(struct kimage *image,
Maneesh Soni72414d32005-06-25 14:58:28 -07001066 struct kexec_segment *segment)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001067{
1068 unsigned long maddr;
Zhang Yanfei310faaa2013-04-30 15:28:21 -07001069 size_t ubytes, mbytes;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001070 int result;
Vivek Goyalcb105252014-08-08 14:25:57 -07001071 unsigned char __user *buf = NULL;
1072 unsigned char *kbuf = NULL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001073
1074 result = 0;
Vivek Goyalcb105252014-08-08 14:25:57 -07001075 if (image->file_mode)
1076 kbuf = segment->kbuf;
1077 else
1078 buf = segment->buf;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001079 ubytes = segment->bufsz;
1080 mbytes = segment->memsz;
1081 maddr = segment->mem;
1082
1083 result = kimage_set_destination(image, maddr);
Maneesh Soni72414d32005-06-25 14:58:28 -07001084 if (result < 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001085 goto out;
Maneesh Soni72414d32005-06-25 14:58:28 -07001086
1087 while (mbytes) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001088 struct page *page;
1089 char *ptr;
1090 size_t uchunk, mchunk;
Maneesh Soni72414d32005-06-25 14:58:28 -07001091
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001092 page = kimage_alloc_page(image, GFP_HIGHUSER, maddr);
Stephen Hemmingerc80544d2007-10-18 03:07:05 -07001093 if (!page) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001094 result = -ENOMEM;
1095 goto out;
1096 }
Maneesh Soni72414d32005-06-25 14:58:28 -07001097 result = kimage_add_page(image, page_to_pfn(page)
1098 << PAGE_SHIFT);
1099 if (result < 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001100 goto out;
Maneesh Soni72414d32005-06-25 14:58:28 -07001101
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001102 ptr = kmap(page);
1103 /* Start with a clear page */
Jan Beulich3ecb01d2010-10-26 14:22:27 -07001104 clear_page(ptr);
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001105 ptr += maddr & ~PAGE_MASK;
Zhang Yanfei31c3a3f2013-04-30 15:28:23 -07001106 mchunk = min_t(size_t, mbytes,
1107 PAGE_SIZE - (maddr & ~PAGE_MASK));
1108 uchunk = min(ubytes, mchunk);
Maneesh Soni72414d32005-06-25 14:58:28 -07001109
Vivek Goyalcb105252014-08-08 14:25:57 -07001110 /* For file based kexec, source pages are in kernel memory */
1111 if (image->file_mode)
1112 memcpy(ptr, kbuf, uchunk);
1113 else
1114 result = copy_from_user(ptr, buf, uchunk);
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001115 kunmap(page);
1116 if (result) {
Dan Carpenterf65a03f2010-08-10 18:03:31 -07001117 result = -EFAULT;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001118 goto out;
1119 }
1120 ubytes -= uchunk;
1121 maddr += mchunk;
Vivek Goyalcb105252014-08-08 14:25:57 -07001122 if (image->file_mode)
1123 kbuf += mchunk;
1124 else
1125 buf += mchunk;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001126 mbytes -= mchunk;
1127 }
Maneesh Soni72414d32005-06-25 14:58:28 -07001128out:
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001129 return result;
1130}
1131
1132static int kimage_load_crash_segment(struct kimage *image,
Maneesh Soni72414d32005-06-25 14:58:28 -07001133 struct kexec_segment *segment)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001134{
1135 /* For crash dumps kernels we simply copy the data from
1136 * user space to it's destination.
1137 * We do things a page at a time for the sake of kmap.
1138 */
1139 unsigned long maddr;
Zhang Yanfei310faaa2013-04-30 15:28:21 -07001140 size_t ubytes, mbytes;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001141 int result;
Vivek Goyaldd5f7262014-08-08 14:26:09 -07001142 unsigned char __user *buf = NULL;
1143 unsigned char *kbuf = NULL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001144
1145 result = 0;
Vivek Goyaldd5f7262014-08-08 14:26:09 -07001146 if (image->file_mode)
1147 kbuf = segment->kbuf;
1148 else
1149 buf = segment->buf;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001150 ubytes = segment->bufsz;
1151 mbytes = segment->memsz;
1152 maddr = segment->mem;
Maneesh Soni72414d32005-06-25 14:58:28 -07001153 while (mbytes) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001154 struct page *page;
1155 char *ptr;
1156 size_t uchunk, mchunk;
Maneesh Soni72414d32005-06-25 14:58:28 -07001157
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001158 page = pfn_to_page(maddr >> PAGE_SHIFT);
Stephen Hemmingerc80544d2007-10-18 03:07:05 -07001159 if (!page) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001160 result = -ENOMEM;
1161 goto out;
1162 }
1163 ptr = kmap(page);
1164 ptr += maddr & ~PAGE_MASK;
Zhang Yanfei31c3a3f2013-04-30 15:28:23 -07001165 mchunk = min_t(size_t, mbytes,
1166 PAGE_SIZE - (maddr & ~PAGE_MASK));
1167 uchunk = min(ubytes, mchunk);
1168 if (mchunk > uchunk) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001169 /* Zero the trailing part of the page */
1170 memset(ptr + uchunk, 0, mchunk - uchunk);
1171 }
Vivek Goyaldd5f7262014-08-08 14:26:09 -07001172
1173 /* For file based kexec, source pages are in kernel memory */
1174 if (image->file_mode)
1175 memcpy(ptr, kbuf, uchunk);
1176 else
1177 result = copy_from_user(ptr, buf, uchunk);
Zou Nan haia79561132006-12-07 09:51:35 -08001178 kexec_flush_icache_page(page);
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001179 kunmap(page);
1180 if (result) {
Dan Carpenterf65a03f2010-08-10 18:03:31 -07001181 result = -EFAULT;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001182 goto out;
1183 }
1184 ubytes -= uchunk;
1185 maddr += mchunk;
Vivek Goyaldd5f7262014-08-08 14:26:09 -07001186 if (image->file_mode)
1187 kbuf += mchunk;
1188 else
1189 buf += mchunk;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001190 mbytes -= mchunk;
1191 }
Maneesh Soni72414d32005-06-25 14:58:28 -07001192out:
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001193 return result;
1194}
1195
1196static int kimage_load_segment(struct kimage *image,
Maneesh Soni72414d32005-06-25 14:58:28 -07001197 struct kexec_segment *segment)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001198{
1199 int result = -ENOMEM;
Maneesh Soni72414d32005-06-25 14:58:28 -07001200
1201 switch (image->type) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001202 case KEXEC_TYPE_DEFAULT:
1203 result = kimage_load_normal_segment(image, segment);
1204 break;
1205 case KEXEC_TYPE_CRASH:
1206 result = kimage_load_crash_segment(image, segment);
1207 break;
1208 }
Maneesh Soni72414d32005-06-25 14:58:28 -07001209
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001210 return result;
1211}
1212
1213/*
1214 * Exec Kernel system call: for obvious reasons only root may call it.
1215 *
1216 * This call breaks up into three pieces.
1217 * - A generic part which loads the new kernel from the current
1218 * address space, and very carefully places the data in the
1219 * allocated pages.
1220 *
1221 * - A generic part that interacts with the kernel and tells all of
1222 * the devices to shut down. Preventing on-going dmas, and placing
1223 * the devices in a consistent state so a later kernel can
1224 * reinitialize them.
1225 *
1226 * - A machine specific part that includes the syscall number
Geert Uytterhoeven002ace72013-09-15 11:35:37 +02001227 * and then copies the image to it's final destination. And
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001228 * jumps into the image at entry.
1229 *
1230 * kexec does not sync, or unmount filesystems so if you need
1231 * that to happen you need to do that yourself.
1232 */
Jeff Moyerc330dda2006-06-23 02:05:07 -07001233struct kimage *kexec_image;
1234struct kimage *kexec_crash_image;
Kees Cook79847542014-01-23 15:55:59 -08001235int kexec_load_disabled;
Andrew Morton8c5a1cf2008-08-15 00:40:27 -07001236
1237static DEFINE_MUTEX(kexec_mutex);
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001238
Heiko Carstens754fe8d2009-01-14 14:14:09 +01001239SYSCALL_DEFINE4(kexec_load, unsigned long, entry, unsigned long, nr_segments,
1240 struct kexec_segment __user *, segments, unsigned long, flags)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001241{
1242 struct kimage **dest_image, *image;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001243 int result;
1244
1245 /* We only trust the superuser with rebooting the system. */
Kees Cook79847542014-01-23 15:55:59 -08001246 if (!capable(CAP_SYS_BOOT) || kexec_load_disabled)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001247 return -EPERM;
1248
1249 /*
1250 * Verify we have a legal set of flags
1251 * This leaves us room for future extensions.
1252 */
1253 if ((flags & KEXEC_FLAGS) != (flags & ~KEXEC_ARCH_MASK))
1254 return -EINVAL;
1255
1256 /* Verify we are on the appropriate architecture */
1257 if (((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH) &&
1258 ((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH_DEFAULT))
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001259 return -EINVAL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001260
1261 /* Put an artificial cap on the number
1262 * of segments passed to kexec_load.
1263 */
1264 if (nr_segments > KEXEC_SEGMENT_MAX)
1265 return -EINVAL;
1266
1267 image = NULL;
1268 result = 0;
1269
1270 /* Because we write directly to the reserved memory
1271 * region when loading crash kernels we need a mutex here to
1272 * prevent multiple crash kernels from attempting to load
1273 * simultaneously, and to prevent a crash kernel from loading
1274 * over the top of a in use crash kernel.
1275 *
1276 * KISS: always take the mutex.
1277 */
Andrew Morton8c5a1cf2008-08-15 00:40:27 -07001278 if (!mutex_trylock(&kexec_mutex))
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001279 return -EBUSY;
Maneesh Soni72414d32005-06-25 14:58:28 -07001280
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001281 dest_image = &kexec_image;
Maneesh Soni72414d32005-06-25 14:58:28 -07001282 if (flags & KEXEC_ON_CRASH)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001283 dest_image = &kexec_crash_image;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001284 if (nr_segments > 0) {
1285 unsigned long i;
Maneesh Soni72414d32005-06-25 14:58:28 -07001286
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001287 /* Loading another kernel to reboot into */
Maneesh Soni72414d32005-06-25 14:58:28 -07001288 if ((flags & KEXEC_ON_CRASH) == 0)
Vivek Goyal255aedd2014-08-08 14:25:48 -07001289 result = kimage_alloc_init(&image, entry, nr_segments,
1290 segments, flags);
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001291 /* Loading another kernel to switch to if this one crashes */
1292 else if (flags & KEXEC_ON_CRASH) {
1293 /* Free any current crash dump kernel before
1294 * we corrupt it.
1295 */
1296 kimage_free(xchg(&kexec_crash_image, NULL));
Vivek Goyal255aedd2014-08-08 14:25:48 -07001297 result = kimage_alloc_init(&image, entry, nr_segments,
1298 segments, flags);
Michael Holzheu558df722011-10-30 15:16:43 +01001299 crash_map_reserved_pages();
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001300 }
Maneesh Soni72414d32005-06-25 14:58:28 -07001301 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001302 goto out;
Maneesh Soni72414d32005-06-25 14:58:28 -07001303
Huang Ying3ab83522008-07-25 19:45:07 -07001304 if (flags & KEXEC_PRESERVE_CONTEXT)
1305 image->preserve_context = 1;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001306 result = machine_kexec_prepare(image);
Maneesh Soni72414d32005-06-25 14:58:28 -07001307 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001308 goto out;
Maneesh Soni72414d32005-06-25 14:58:28 -07001309
1310 for (i = 0; i < nr_segments; i++) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001311 result = kimage_load_segment(image, &image->segment[i]);
Maneesh Soni72414d32005-06-25 14:58:28 -07001312 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001313 goto out;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001314 }
WANG Cong7fccf032008-07-25 19:45:02 -07001315 kimage_terminate(image);
Michael Holzheu558df722011-10-30 15:16:43 +01001316 if (flags & KEXEC_ON_CRASH)
1317 crash_unmap_reserved_pages();
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001318 }
1319 /* Install the new kernel, and Uninstall the old */
1320 image = xchg(dest_image, image);
1321
Maneesh Soni72414d32005-06-25 14:58:28 -07001322out:
Andrew Morton8c5a1cf2008-08-15 00:40:27 -07001323 mutex_unlock(&kexec_mutex);
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001324 kimage_free(image);
Maneesh Soni72414d32005-06-25 14:58:28 -07001325
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001326 return result;
1327}
1328
Michael Holzheu558df722011-10-30 15:16:43 +01001329/*
1330 * Add and remove page tables for crashkernel memory
1331 *
1332 * Provide an empty default implementation here -- architecture
1333 * code may override this
1334 */
1335void __weak crash_map_reserved_pages(void)
1336{}
1337
1338void __weak crash_unmap_reserved_pages(void)
1339{}
1340
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001341#ifdef CONFIG_COMPAT
Heiko Carstensca2c4052014-03-04 17:13:42 +01001342COMPAT_SYSCALL_DEFINE4(kexec_load, compat_ulong_t, entry,
1343 compat_ulong_t, nr_segments,
1344 struct compat_kexec_segment __user *, segments,
1345 compat_ulong_t, flags)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001346{
1347 struct compat_kexec_segment in;
1348 struct kexec_segment out, __user *ksegments;
1349 unsigned long i, result;
1350
1351 /* Don't allow clients that don't understand the native
1352 * architecture to do anything.
1353 */
Maneesh Soni72414d32005-06-25 14:58:28 -07001354 if ((flags & KEXEC_ARCH_MASK) == KEXEC_ARCH_DEFAULT)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001355 return -EINVAL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001356
Maneesh Soni72414d32005-06-25 14:58:28 -07001357 if (nr_segments > KEXEC_SEGMENT_MAX)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001358 return -EINVAL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001359
1360 ksegments = compat_alloc_user_space(nr_segments * sizeof(out));
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001361 for (i = 0; i < nr_segments; i++) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001362 result = copy_from_user(&in, &segments[i], sizeof(in));
Maneesh Soni72414d32005-06-25 14:58:28 -07001363 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001364 return -EFAULT;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001365
1366 out.buf = compat_ptr(in.buf);
1367 out.bufsz = in.bufsz;
1368 out.mem = in.mem;
1369 out.memsz = in.memsz;
1370
1371 result = copy_to_user(&ksegments[i], &out, sizeof(out));
Maneesh Soni72414d32005-06-25 14:58:28 -07001372 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001373 return -EFAULT;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001374 }
1375
1376 return sys_kexec_load(entry, nr_segments, ksegments, flags);
1377}
1378#endif
1379
Vivek Goyal74ca3172014-08-29 15:18:46 -07001380#ifdef CONFIG_KEXEC_FILE
Vivek Goyalf0895682014-08-08 14:25:55 -07001381SYSCALL_DEFINE5(kexec_file_load, int, kernel_fd, int, initrd_fd,
1382 unsigned long, cmdline_len, const char __user *, cmdline_ptr,
1383 unsigned long, flags)
1384{
Vivek Goyalcb105252014-08-08 14:25:57 -07001385 int ret = 0, i;
1386 struct kimage **dest_image, *image;
1387
1388 /* We only trust the superuser with rebooting the system. */
1389 if (!capable(CAP_SYS_BOOT) || kexec_load_disabled)
1390 return -EPERM;
1391
1392 /* Make sure we have a legal set of flags */
1393 if (flags != (flags & KEXEC_FILE_FLAGS))
1394 return -EINVAL;
1395
1396 image = NULL;
1397
1398 if (!mutex_trylock(&kexec_mutex))
1399 return -EBUSY;
1400
1401 dest_image = &kexec_image;
1402 if (flags & KEXEC_FILE_ON_CRASH)
1403 dest_image = &kexec_crash_image;
1404
1405 if (flags & KEXEC_FILE_UNLOAD)
1406 goto exchange;
1407
1408 /*
1409 * In case of crash, new kernel gets loaded in reserved region. It is
1410 * same memory where old crash kernel might be loaded. Free any
1411 * current crash dump kernel before we corrupt it.
1412 */
1413 if (flags & KEXEC_FILE_ON_CRASH)
1414 kimage_free(xchg(&kexec_crash_image, NULL));
1415
1416 ret = kimage_file_alloc_init(&image, kernel_fd, initrd_fd, cmdline_ptr,
1417 cmdline_len, flags);
1418 if (ret)
1419 goto out;
1420
1421 ret = machine_kexec_prepare(image);
1422 if (ret)
1423 goto out;
1424
Vivek Goyal12db5562014-08-08 14:26:04 -07001425 ret = kexec_calculate_store_digests(image);
1426 if (ret)
1427 goto out;
1428
Vivek Goyalcb105252014-08-08 14:25:57 -07001429 for (i = 0; i < image->nr_segments; i++) {
1430 struct kexec_segment *ksegment;
1431
1432 ksegment = &image->segment[i];
1433 pr_debug("Loading segment %d: buf=0x%p bufsz=0x%zx mem=0x%lx memsz=0x%zx\n",
1434 i, ksegment->buf, ksegment->bufsz, ksegment->mem,
1435 ksegment->memsz);
1436
1437 ret = kimage_load_segment(image, &image->segment[i]);
1438 if (ret)
1439 goto out;
1440 }
1441
1442 kimage_terminate(image);
1443
1444 /*
1445 * Free up any temporary buffers allocated which are not needed
1446 * after image has been loaded
1447 */
1448 kimage_file_post_load_cleanup(image);
1449exchange:
1450 image = xchg(dest_image, image);
1451out:
1452 mutex_unlock(&kexec_mutex);
1453 kimage_free(image);
1454 return ret;
Vivek Goyalf0895682014-08-08 14:25:55 -07001455}
1456
Vivek Goyal74ca3172014-08-29 15:18:46 -07001457#endif /* CONFIG_KEXEC_FILE */
1458
Alexander Nyberg6e274d12005-06-25 14:58:26 -07001459void crash_kexec(struct pt_regs *regs)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001460{
Andrew Morton8c5a1cf2008-08-15 00:40:27 -07001461 /* Take the kexec_mutex here to prevent sys_kexec_load
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001462 * running on one cpu from replacing the crash kernel
1463 * we are using after a panic on a different cpu.
1464 *
1465 * If the crash kernel was not located in a fixed area
1466 * of memory the xchg(&kexec_crash_image) would be
1467 * sufficient. But since I reuse the memory...
1468 */
Andrew Morton8c5a1cf2008-08-15 00:40:27 -07001469 if (mutex_trylock(&kexec_mutex)) {
David Wilderc0ce7d02006-06-23 15:29:34 -07001470 if (kexec_crash_image) {
Vivek Goyale996e582006-01-09 20:51:44 -08001471 struct pt_regs fixed_regs;
KOSAKI Motohiro0f4bd462009-12-22 03:15:43 +00001472
Vivek Goyale996e582006-01-09 20:51:44 -08001473 crash_setup_regs(&fixed_regs, regs);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001474 crash_save_vmcoreinfo();
Vivek Goyale996e582006-01-09 20:51:44 -08001475 machine_crash_shutdown(&fixed_regs);
David Wilderc0ce7d02006-06-23 15:29:34 -07001476 machine_kexec(kexec_crash_image);
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001477 }
Andrew Morton8c5a1cf2008-08-15 00:40:27 -07001478 mutex_unlock(&kexec_mutex);
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001479 }
1480}
Vivek Goyalcc571652006-01-09 20:51:41 -08001481
Amerigo Wang06a7f712009-12-15 16:47:46 -08001482size_t crash_get_memory_size(void)
1483{
Pavan Naregundie05bd332010-06-29 15:05:28 -07001484 size_t size = 0;
Amerigo Wang06a7f712009-12-15 16:47:46 -08001485 mutex_lock(&kexec_mutex);
Pavan Naregundie05bd332010-06-29 15:05:28 -07001486 if (crashk_res.end != crashk_res.start)
Joe Perches28f65c112011-06-09 09:13:32 -07001487 size = resource_size(&crashk_res);
Amerigo Wang06a7f712009-12-15 16:47:46 -08001488 mutex_unlock(&kexec_mutex);
1489 return size;
1490}
1491
Anton Blanchardc0bb9e42010-08-25 10:22:58 +10001492void __weak crash_free_reserved_phys_range(unsigned long begin,
1493 unsigned long end)
Amerigo Wang06a7f712009-12-15 16:47:46 -08001494{
1495 unsigned long addr;
1496
Jiang Liue07cee22013-04-29 15:06:58 -07001497 for (addr = begin; addr < end; addr += PAGE_SIZE)
1498 free_reserved_page(pfn_to_page(addr >> PAGE_SHIFT));
Amerigo Wang06a7f712009-12-15 16:47:46 -08001499}
1500
1501int crash_shrink_memory(unsigned long new_size)
1502{
1503 int ret = 0;
1504 unsigned long start, end;
Michael Holzheubec013c2012-01-12 17:20:15 -08001505 unsigned long old_size;
Michael Holzheu6480e5a2012-01-12 17:20:14 -08001506 struct resource *ram_res;
Amerigo Wang06a7f712009-12-15 16:47:46 -08001507
1508 mutex_lock(&kexec_mutex);
1509
1510 if (kexec_crash_image) {
1511 ret = -ENOENT;
1512 goto unlock;
1513 }
1514 start = crashk_res.start;
1515 end = crashk_res.end;
Michael Holzheubec013c2012-01-12 17:20:15 -08001516 old_size = (end == 0) ? 0 : end - start + 1;
1517 if (new_size >= old_size) {
1518 ret = (new_size == old_size) ? 0 : -EINVAL;
Amerigo Wang06a7f712009-12-15 16:47:46 -08001519 goto unlock;
1520 }
1521
Michael Holzheu6480e5a2012-01-12 17:20:14 -08001522 ram_res = kzalloc(sizeof(*ram_res), GFP_KERNEL);
1523 if (!ram_res) {
1524 ret = -ENOMEM;
1525 goto unlock;
1526 }
1527
Michael Holzheu558df722011-10-30 15:16:43 +01001528 start = roundup(start, KEXEC_CRASH_MEM_ALIGN);
1529 end = roundup(start + new_size, KEXEC_CRASH_MEM_ALIGN);
Amerigo Wang06a7f712009-12-15 16:47:46 -08001530
Michael Holzheu558df722011-10-30 15:16:43 +01001531 crash_map_reserved_pages();
Anton Blanchardc0bb9e42010-08-25 10:22:58 +10001532 crash_free_reserved_phys_range(end, crashk_res.end);
Amerigo Wang06a7f712009-12-15 16:47:46 -08001533
Pavan Naregundie05bd332010-06-29 15:05:28 -07001534 if ((start == end) && (crashk_res.parent != NULL))
Amerigo Wang06a7f712009-12-15 16:47:46 -08001535 release_resource(&crashk_res);
Michael Holzheu6480e5a2012-01-12 17:20:14 -08001536
1537 ram_res->start = end;
1538 ram_res->end = crashk_res.end;
1539 ram_res->flags = IORESOURCE_BUSY | IORESOURCE_MEM;
1540 ram_res->name = "System RAM";
1541
Vitaly Mayatskikh475f9aa62010-05-11 14:06:51 -07001542 crashk_res.end = end - 1;
Michael Holzheu6480e5a2012-01-12 17:20:14 -08001543
1544 insert_resource(&iomem_resource, ram_res);
Michael Holzheu558df722011-10-30 15:16:43 +01001545 crash_unmap_reserved_pages();
Amerigo Wang06a7f712009-12-15 16:47:46 -08001546
1547unlock:
1548 mutex_unlock(&kexec_mutex);
1549 return ret;
1550}
1551
Magnus Damm85916f82006-12-06 20:40:41 -08001552static u32 *append_elf_note(u32 *buf, char *name, unsigned type, void *data,
1553 size_t data_len)
1554{
1555 struct elf_note note;
1556
1557 note.n_namesz = strlen(name) + 1;
1558 note.n_descsz = data_len;
1559 note.n_type = type;
1560 memcpy(buf, &note, sizeof(note));
1561 buf += (sizeof(note) + 3)/4;
1562 memcpy(buf, name, note.n_namesz);
1563 buf += (note.n_namesz + 3)/4;
1564 memcpy(buf, data, note.n_descsz);
1565 buf += (note.n_descsz + 3)/4;
1566
1567 return buf;
1568}
1569
1570static void final_note(u32 *buf)
1571{
1572 struct elf_note note;
1573
1574 note.n_namesz = 0;
1575 note.n_descsz = 0;
1576 note.n_type = 0;
1577 memcpy(buf, &note, sizeof(note));
1578}
1579
1580void crash_save_cpu(struct pt_regs *regs, int cpu)
1581{
1582 struct elf_prstatus prstatus;
1583 u32 *buf;
1584
Rusty Russell4f4b6c12009-01-01 10:12:15 +10301585 if ((cpu < 0) || (cpu >= nr_cpu_ids))
Magnus Damm85916f82006-12-06 20:40:41 -08001586 return;
1587
1588 /* Using ELF notes here is opportunistic.
1589 * I need a well defined structure format
1590 * for the data I pass, and I need tags
1591 * on the data to indicate what information I have
1592 * squirrelled away. ELF notes happen to provide
1593 * all of that, so there is no need to invent something new.
1594 */
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001595 buf = (u32 *)per_cpu_ptr(crash_notes, cpu);
Magnus Damm85916f82006-12-06 20:40:41 -08001596 if (!buf)
1597 return;
1598 memset(&prstatus, 0, sizeof(prstatus));
1599 prstatus.pr_pid = current->pid;
Tejun Heo6cd61c0b2009-02-09 22:17:39 +09001600 elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
Simon Horman6672f762007-05-08 00:28:22 -07001601 buf = append_elf_note(buf, KEXEC_CORE_NOTE_NAME, NT_PRSTATUS,
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001602 &prstatus, sizeof(prstatus));
Magnus Damm85916f82006-12-06 20:40:41 -08001603 final_note(buf);
1604}
1605
Vivek Goyalcc571652006-01-09 20:51:41 -08001606static int __init crash_notes_memory_init(void)
1607{
1608 /* Allocate memory for saving cpu registers. */
1609 crash_notes = alloc_percpu(note_buf_t);
1610 if (!crash_notes) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001611 pr_warn("Kexec: Memory allocation for saving cpu register states failed\n");
Vivek Goyalcc571652006-01-09 20:51:41 -08001612 return -ENOMEM;
1613 }
1614 return 0;
1615}
Paul Gortmakerc96d6662014-04-03 14:48:35 -07001616subsys_initcall(crash_notes_memory_init);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001617
Bernhard Wallecba63c32007-10-18 23:40:58 -07001618
1619/*
1620 * parsing the "crashkernel" commandline
1621 *
1622 * this code is intended to be called from architecture specific code
1623 */
1624
1625
1626/*
1627 * This function parses command lines in the format
1628 *
1629 * crashkernel=ramsize-range:size[,...][@offset]
1630 *
1631 * The function returns 0 on success and -EINVAL on failure.
1632 */
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001633static int __init parse_crashkernel_mem(char *cmdline,
1634 unsigned long long system_ram,
1635 unsigned long long *crash_size,
1636 unsigned long long *crash_base)
Bernhard Wallecba63c32007-10-18 23:40:58 -07001637{
1638 char *cur = cmdline, *tmp;
1639
1640 /* for each entry of the comma-separated list */
1641 do {
1642 unsigned long long start, end = ULLONG_MAX, size;
1643
1644 /* get the start of the range */
1645 start = memparse(cur, &tmp);
1646 if (cur == tmp) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001647 pr_warn("crashkernel: Memory value expected\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001648 return -EINVAL;
1649 }
1650 cur = tmp;
1651 if (*cur != '-') {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001652 pr_warn("crashkernel: '-' expected\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001653 return -EINVAL;
1654 }
1655 cur++;
1656
1657 /* if no ':' is here, than we read the end */
1658 if (*cur != ':') {
1659 end = memparse(cur, &tmp);
1660 if (cur == tmp) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001661 pr_warn("crashkernel: Memory value expected\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001662 return -EINVAL;
1663 }
1664 cur = tmp;
1665 if (end <= start) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001666 pr_warn("crashkernel: end <= start\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001667 return -EINVAL;
1668 }
1669 }
1670
1671 if (*cur != ':') {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001672 pr_warn("crashkernel: ':' expected\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001673 return -EINVAL;
1674 }
1675 cur++;
1676
1677 size = memparse(cur, &tmp);
1678 if (cur == tmp) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001679 pr_warn("Memory value expected\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001680 return -EINVAL;
1681 }
1682 cur = tmp;
1683 if (size >= system_ram) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001684 pr_warn("crashkernel: invalid size\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001685 return -EINVAL;
1686 }
1687
1688 /* match ? */
Michael Ellermanbe089d792008-05-01 04:34:49 -07001689 if (system_ram >= start && system_ram < end) {
Bernhard Wallecba63c32007-10-18 23:40:58 -07001690 *crash_size = size;
1691 break;
1692 }
1693 } while (*cur++ == ',');
1694
1695 if (*crash_size > 0) {
Hidetoshi Seto11c7da42009-07-29 15:02:08 -07001696 while (*cur && *cur != ' ' && *cur != '@')
Bernhard Wallecba63c32007-10-18 23:40:58 -07001697 cur++;
1698 if (*cur == '@') {
1699 cur++;
1700 *crash_base = memparse(cur, &tmp);
1701 if (cur == tmp) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001702 pr_warn("Memory value expected after '@'\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001703 return -EINVAL;
1704 }
1705 }
1706 }
1707
1708 return 0;
1709}
1710
1711/*
1712 * That function parses "simple" (old) crashkernel command lines like
1713 *
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001714 * crashkernel=size[@offset]
Bernhard Wallecba63c32007-10-18 23:40:58 -07001715 *
1716 * It returns 0 on success and -EINVAL on failure.
1717 */
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001718static int __init parse_crashkernel_simple(char *cmdline,
1719 unsigned long long *crash_size,
1720 unsigned long long *crash_base)
Bernhard Wallecba63c32007-10-18 23:40:58 -07001721{
1722 char *cur = cmdline;
1723
1724 *crash_size = memparse(cmdline, &cur);
1725 if (cmdline == cur) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001726 pr_warn("crashkernel: memory value expected\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001727 return -EINVAL;
1728 }
1729
1730 if (*cur == '@')
1731 *crash_base = memparse(cur+1, &cur);
Zhenzhong Duaneaa3be62012-03-28 14:42:47 -07001732 else if (*cur != ' ' && *cur != '\0') {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001733 pr_warn("crashkernel: unrecognized char\n");
Zhenzhong Duaneaa3be62012-03-28 14:42:47 -07001734 return -EINVAL;
1735 }
Bernhard Wallecba63c32007-10-18 23:40:58 -07001736
1737 return 0;
1738}
1739
Yinghai Luadbc7422013-04-15 22:23:48 -07001740#define SUFFIX_HIGH 0
1741#define SUFFIX_LOW 1
1742#define SUFFIX_NULL 2
1743static __initdata char *suffix_tbl[] = {
1744 [SUFFIX_HIGH] = ",high",
1745 [SUFFIX_LOW] = ",low",
1746 [SUFFIX_NULL] = NULL,
1747};
1748
Bernhard Wallecba63c32007-10-18 23:40:58 -07001749/*
Yinghai Luadbc7422013-04-15 22:23:48 -07001750 * That function parses "suffix" crashkernel command lines like
1751 *
1752 * crashkernel=size,[high|low]
1753 *
1754 * It returns 0 on success and -EINVAL on failure.
Bernhard Wallecba63c32007-10-18 23:40:58 -07001755 */
Yinghai Luadbc7422013-04-15 22:23:48 -07001756static int __init parse_crashkernel_suffix(char *cmdline,
1757 unsigned long long *crash_size,
Yinghai Luadbc7422013-04-15 22:23:48 -07001758 const char *suffix)
1759{
1760 char *cur = cmdline;
1761
1762 *crash_size = memparse(cmdline, &cur);
1763 if (cmdline == cur) {
1764 pr_warn("crashkernel: memory value expected\n");
1765 return -EINVAL;
1766 }
1767
1768 /* check with suffix */
1769 if (strncmp(cur, suffix, strlen(suffix))) {
1770 pr_warn("crashkernel: unrecognized char\n");
1771 return -EINVAL;
1772 }
1773 cur += strlen(suffix);
1774 if (*cur != ' ' && *cur != '\0') {
1775 pr_warn("crashkernel: unrecognized char\n");
1776 return -EINVAL;
1777 }
1778
1779 return 0;
1780}
1781
1782static __init char *get_last_crashkernel(char *cmdline,
1783 const char *name,
1784 const char *suffix)
1785{
1786 char *p = cmdline, *ck_cmdline = NULL;
1787
1788 /* find crashkernel and use the last one if there are more */
1789 p = strstr(p, name);
1790 while (p) {
1791 char *end_p = strchr(p, ' ');
1792 char *q;
1793
1794 if (!end_p)
1795 end_p = p + strlen(p);
1796
1797 if (!suffix) {
1798 int i;
1799
1800 /* skip the one with any known suffix */
1801 for (i = 0; suffix_tbl[i]; i++) {
1802 q = end_p - strlen(suffix_tbl[i]);
1803 if (!strncmp(q, suffix_tbl[i],
1804 strlen(suffix_tbl[i])))
1805 goto next;
1806 }
1807 ck_cmdline = p;
1808 } else {
1809 q = end_p - strlen(suffix);
1810 if (!strncmp(q, suffix, strlen(suffix)))
1811 ck_cmdline = p;
1812 }
1813next:
1814 p = strstr(p+1, name);
1815 }
1816
1817 if (!ck_cmdline)
1818 return NULL;
1819
1820 return ck_cmdline;
1821}
1822
Yinghai Lu0212f912013-01-24 12:20:11 -08001823static int __init __parse_crashkernel(char *cmdline,
Bernhard Wallecba63c32007-10-18 23:40:58 -07001824 unsigned long long system_ram,
1825 unsigned long long *crash_size,
Yinghai Lu0212f912013-01-24 12:20:11 -08001826 unsigned long long *crash_base,
Yinghai Luadbc7422013-04-15 22:23:48 -07001827 const char *name,
1828 const char *suffix)
Bernhard Wallecba63c32007-10-18 23:40:58 -07001829{
Bernhard Wallecba63c32007-10-18 23:40:58 -07001830 char *first_colon, *first_space;
Yinghai Luadbc7422013-04-15 22:23:48 -07001831 char *ck_cmdline;
Bernhard Wallecba63c32007-10-18 23:40:58 -07001832
1833 BUG_ON(!crash_size || !crash_base);
1834 *crash_size = 0;
1835 *crash_base = 0;
1836
Yinghai Luadbc7422013-04-15 22:23:48 -07001837 ck_cmdline = get_last_crashkernel(cmdline, name, suffix);
Bernhard Wallecba63c32007-10-18 23:40:58 -07001838
1839 if (!ck_cmdline)
1840 return -EINVAL;
1841
Yinghai Lu0212f912013-01-24 12:20:11 -08001842 ck_cmdline += strlen(name);
Bernhard Wallecba63c32007-10-18 23:40:58 -07001843
Yinghai Luadbc7422013-04-15 22:23:48 -07001844 if (suffix)
1845 return parse_crashkernel_suffix(ck_cmdline, crash_size,
Baoquan He36f3f502014-10-13 15:53:44 -07001846 suffix);
Bernhard Wallecba63c32007-10-18 23:40:58 -07001847 /*
1848 * if the commandline contains a ':', then that's the extended
1849 * syntax -- if not, it must be the classic syntax
1850 */
1851 first_colon = strchr(ck_cmdline, ':');
1852 first_space = strchr(ck_cmdline, ' ');
1853 if (first_colon && (!first_space || first_colon < first_space))
1854 return parse_crashkernel_mem(ck_cmdline, system_ram,
1855 crash_size, crash_base);
Bernhard Wallecba63c32007-10-18 23:40:58 -07001856
Xishi Qiu80c74f62013-09-11 14:24:47 -07001857 return parse_crashkernel_simple(ck_cmdline, crash_size, crash_base);
Bernhard Wallecba63c32007-10-18 23:40:58 -07001858}
1859
Yinghai Luadbc7422013-04-15 22:23:48 -07001860/*
1861 * That function is the entry point for command line parsing and should be
1862 * called from the arch-specific code.
1863 */
Yinghai Lu0212f912013-01-24 12:20:11 -08001864int __init parse_crashkernel(char *cmdline,
1865 unsigned long long system_ram,
1866 unsigned long long *crash_size,
1867 unsigned long long *crash_base)
1868{
1869 return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
Yinghai Luadbc7422013-04-15 22:23:48 -07001870 "crashkernel=", NULL);
Yinghai Lu0212f912013-01-24 12:20:11 -08001871}
1872
Yinghai Lu55a20ee2013-04-15 22:23:47 -07001873int __init parse_crashkernel_high(char *cmdline,
1874 unsigned long long system_ram,
1875 unsigned long long *crash_size,
1876 unsigned long long *crash_base)
1877{
1878 return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
Yinghai Luadbc7422013-04-15 22:23:48 -07001879 "crashkernel=", suffix_tbl[SUFFIX_HIGH]);
Yinghai Lu55a20ee2013-04-15 22:23:47 -07001880}
1881
Yinghai Lu0212f912013-01-24 12:20:11 -08001882int __init parse_crashkernel_low(char *cmdline,
1883 unsigned long long system_ram,
1884 unsigned long long *crash_size,
1885 unsigned long long *crash_base)
1886{
1887 return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
Yinghai Luadbc7422013-04-15 22:23:48 -07001888 "crashkernel=", suffix_tbl[SUFFIX_LOW]);
Yinghai Lu0212f912013-01-24 12:20:11 -08001889}
Bernhard Wallecba63c32007-10-18 23:40:58 -07001890
Michael Holzheufa8ff292011-10-30 15:16:41 +01001891static void update_vmcoreinfo_note(void)
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001892{
Michael Holzheufa8ff292011-10-30 15:16:41 +01001893 u32 *buf = vmcoreinfo_note;
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001894
1895 if (!vmcoreinfo_size)
1896 return;
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001897 buf = append_elf_note(buf, VMCOREINFO_NOTE_NAME, 0, vmcoreinfo_data,
1898 vmcoreinfo_size);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001899 final_note(buf);
1900}
1901
Michael Holzheufa8ff292011-10-30 15:16:41 +01001902void crash_save_vmcoreinfo(void)
1903{
Vivek Goyal63dca8d2012-07-30 14:42:36 -07001904 vmcoreinfo_append_str("CRASHTIME=%ld\n", get_seconds());
Michael Holzheufa8ff292011-10-30 15:16:41 +01001905 update_vmcoreinfo_note();
1906}
1907
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001908void vmcoreinfo_append_str(const char *fmt, ...)
1909{
1910 va_list args;
1911 char buf[0x50];
Zhang Yanfei310faaa2013-04-30 15:28:21 -07001912 size_t r;
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001913
1914 va_start(args, fmt);
Chen Ganga19428e52014-01-27 17:07:13 -08001915 r = vscnprintf(buf, sizeof(buf), fmt, args);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001916 va_end(args);
1917
Zhang Yanfei31c3a3f2013-04-30 15:28:23 -07001918 r = min(r, vmcoreinfo_max_size - vmcoreinfo_size);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001919
1920 memcpy(&vmcoreinfo_data[vmcoreinfo_size], buf, r);
1921
1922 vmcoreinfo_size += r;
1923}
1924
1925/*
1926 * provide an empty default implementation here -- architecture
1927 * code may override this
1928 */
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -07001929void __weak arch_crash_save_vmcoreinfo(void)
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001930{}
1931
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -07001932unsigned long __weak paddr_vmcoreinfo_note(void)
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001933{
1934 return __pa((unsigned long)(char *)&vmcoreinfo_note);
1935}
1936
1937static int __init crash_save_vmcoreinfo_init(void)
1938{
Ken'ichi Ohmichibba1f602008-02-07 00:15:22 -08001939 VMCOREINFO_OSRELEASE(init_uts_ns.name.release);
1940 VMCOREINFO_PAGESIZE(PAGE_SIZE);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001941
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001942 VMCOREINFO_SYMBOL(init_uts_ns);
1943 VMCOREINFO_SYMBOL(node_online_map);
Will Deacond034cfa2012-03-28 14:42:47 -07001944#ifdef CONFIG_MMU
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001945 VMCOREINFO_SYMBOL(swapper_pg_dir);
Will Deacond034cfa2012-03-28 14:42:47 -07001946#endif
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001947 VMCOREINFO_SYMBOL(_stext);
Joonsoo Kimf1c40692013-04-29 15:07:37 -07001948 VMCOREINFO_SYMBOL(vmap_area_list);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001949
1950#ifndef CONFIG_NEED_MULTIPLE_NODES
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001951 VMCOREINFO_SYMBOL(mem_map);
1952 VMCOREINFO_SYMBOL(contig_page_data);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001953#endif
1954#ifdef CONFIG_SPARSEMEM
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001955 VMCOREINFO_SYMBOL(mem_section);
1956 VMCOREINFO_LENGTH(mem_section, NR_SECTION_ROOTS);
Ken'ichi Ohmichic76f8602008-02-07 00:15:20 -08001957 VMCOREINFO_STRUCT_SIZE(mem_section);
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001958 VMCOREINFO_OFFSET(mem_section, section_mem_map);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001959#endif
Ken'ichi Ohmichic76f8602008-02-07 00:15:20 -08001960 VMCOREINFO_STRUCT_SIZE(page);
1961 VMCOREINFO_STRUCT_SIZE(pglist_data);
1962 VMCOREINFO_STRUCT_SIZE(zone);
1963 VMCOREINFO_STRUCT_SIZE(free_area);
1964 VMCOREINFO_STRUCT_SIZE(list_head);
1965 VMCOREINFO_SIZE(nodemask_t);
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001966 VMCOREINFO_OFFSET(page, flags);
1967 VMCOREINFO_OFFSET(page, _count);
1968 VMCOREINFO_OFFSET(page, mapping);
1969 VMCOREINFO_OFFSET(page, lru);
Atsushi Kumagai8d670912013-02-27 17:03:25 -08001970 VMCOREINFO_OFFSET(page, _mapcount);
1971 VMCOREINFO_OFFSET(page, private);
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001972 VMCOREINFO_OFFSET(pglist_data, node_zones);
1973 VMCOREINFO_OFFSET(pglist_data, nr_zones);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001974#ifdef CONFIG_FLAT_NODE_MEM_MAP
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001975 VMCOREINFO_OFFSET(pglist_data, node_mem_map);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001976#endif
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001977 VMCOREINFO_OFFSET(pglist_data, node_start_pfn);
1978 VMCOREINFO_OFFSET(pglist_data, node_spanned_pages);
1979 VMCOREINFO_OFFSET(pglist_data, node_id);
1980 VMCOREINFO_OFFSET(zone, free_area);
1981 VMCOREINFO_OFFSET(zone, vm_stat);
1982 VMCOREINFO_OFFSET(zone, spanned_pages);
1983 VMCOREINFO_OFFSET(free_area, free_list);
1984 VMCOREINFO_OFFSET(list_head, next);
1985 VMCOREINFO_OFFSET(list_head, prev);
Atsushi Kumagai13ba3fc2013-04-29 15:07:40 -07001986 VMCOREINFO_OFFSET(vmap_area, va_start);
1987 VMCOREINFO_OFFSET(vmap_area, list);
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001988 VMCOREINFO_LENGTH(zone.free_area, MAX_ORDER);
Neil Horman04d491a2009-04-02 16:58:57 -07001989 log_buf_kexec_setup();
Ken'ichi Ohmichi83a08e72008-01-08 15:33:05 -08001990 VMCOREINFO_LENGTH(free_area.free_list, MIGRATE_TYPES);
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001991 VMCOREINFO_NUMBER(NR_FREE_PAGES);
Ken'ichi Ohmichi122c7a52008-04-28 02:13:04 -07001992 VMCOREINFO_NUMBER(PG_lru);
1993 VMCOREINFO_NUMBER(PG_private);
1994 VMCOREINFO_NUMBER(PG_swapcache);
Atsushi Kumagai8d670912013-02-27 17:03:25 -08001995 VMCOREINFO_NUMBER(PG_slab);
Mitsuhiro Tanino0d0bf662013-02-27 17:03:27 -08001996#ifdef CONFIG_MEMORY_FAILURE
1997 VMCOREINFO_NUMBER(PG_hwpoison);
1998#endif
Petr Tesarikb3acc562014-06-23 13:22:03 -07001999 VMCOREINFO_NUMBER(PG_head_mask);
Atsushi Kumagai8d670912013-02-27 17:03:25 -08002000 VMCOREINFO_NUMBER(PAGE_BUDDY_MAPCOUNT_VALUE);
David Rientjes3a1122d2014-07-30 19:05:55 -07002001#ifdef CONFIG_HUGETLBFS
Atsushi Kumagai8f1d26d2014-07-30 16:08:39 -07002002 VMCOREINFO_SYMBOL(free_huge_page);
David Rientjes3a1122d2014-07-30 19:05:55 -07002003#endif
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07002004
2005 arch_crash_save_vmcoreinfo();
Michael Holzheufa8ff292011-10-30 15:16:41 +01002006 update_vmcoreinfo_note();
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07002007
2008 return 0;
2009}
2010
Paul Gortmakerc96d6662014-04-03 14:48:35 -07002011subsys_initcall(crash_save_vmcoreinfo_init);
Huang Ying3ab83522008-07-25 19:45:07 -07002012
Vivek Goyal74ca3172014-08-29 15:18:46 -07002013#ifdef CONFIG_KEXEC_FILE
Vivek Goyalcb105252014-08-08 14:25:57 -07002014static int locate_mem_hole_top_down(unsigned long start, unsigned long end,
2015 struct kexec_buf *kbuf)
2016{
2017 struct kimage *image = kbuf->image;
2018 unsigned long temp_start, temp_end;
2019
2020 temp_end = min(end, kbuf->buf_max);
2021 temp_start = temp_end - kbuf->memsz;
2022
2023 do {
2024 /* align down start */
2025 temp_start = temp_start & (~(kbuf->buf_align - 1));
2026
2027 if (temp_start < start || temp_start < kbuf->buf_min)
2028 return 0;
2029
2030 temp_end = temp_start + kbuf->memsz - 1;
2031
2032 /*
2033 * Make sure this does not conflict with any of existing
2034 * segments
2035 */
2036 if (kimage_is_destination_range(image, temp_start, temp_end)) {
2037 temp_start = temp_start - PAGE_SIZE;
2038 continue;
2039 }
2040
2041 /* We found a suitable memory range */
2042 break;
2043 } while (1);
2044
2045 /* If we are here, we found a suitable memory range */
Baoquan He669280a2014-10-13 15:53:40 -07002046 kbuf->mem = temp_start;
Vivek Goyalcb105252014-08-08 14:25:57 -07002047
2048 /* Success, stop navigating through remaining System RAM ranges */
2049 return 1;
2050}
2051
2052static int locate_mem_hole_bottom_up(unsigned long start, unsigned long end,
2053 struct kexec_buf *kbuf)
2054{
2055 struct kimage *image = kbuf->image;
2056 unsigned long temp_start, temp_end;
2057
2058 temp_start = max(start, kbuf->buf_min);
2059
2060 do {
2061 temp_start = ALIGN(temp_start, kbuf->buf_align);
2062 temp_end = temp_start + kbuf->memsz - 1;
2063
2064 if (temp_end > end || temp_end > kbuf->buf_max)
2065 return 0;
2066 /*
2067 * Make sure this does not conflict with any of existing
2068 * segments
2069 */
2070 if (kimage_is_destination_range(image, temp_start, temp_end)) {
2071 temp_start = temp_start + PAGE_SIZE;
2072 continue;
2073 }
2074
2075 /* We found a suitable memory range */
2076 break;
2077 } while (1);
2078
2079 /* If we are here, we found a suitable memory range */
Baoquan He669280a2014-10-13 15:53:40 -07002080 kbuf->mem = temp_start;
Vivek Goyalcb105252014-08-08 14:25:57 -07002081
2082 /* Success, stop navigating through remaining System RAM ranges */
2083 return 1;
2084}
2085
2086static int locate_mem_hole_callback(u64 start, u64 end, void *arg)
2087{
2088 struct kexec_buf *kbuf = (struct kexec_buf *)arg;
2089 unsigned long sz = end - start + 1;
2090
2091 /* Returning 0 will take to next memory range */
2092 if (sz < kbuf->memsz)
2093 return 0;
2094
2095 if (end < kbuf->buf_min || start > kbuf->buf_max)
2096 return 0;
2097
2098 /*
2099 * Allocate memory top down with-in ram range. Otherwise bottom up
2100 * allocation.
2101 */
2102 if (kbuf->top_down)
2103 return locate_mem_hole_top_down(start, end, kbuf);
2104 return locate_mem_hole_bottom_up(start, end, kbuf);
2105}
2106
2107/*
2108 * Helper function for placing a buffer in a kexec segment. This assumes
2109 * that kexec_mutex is held.
2110 */
2111int kexec_add_buffer(struct kimage *image, char *buffer, unsigned long bufsz,
2112 unsigned long memsz, unsigned long buf_align,
2113 unsigned long buf_min, unsigned long buf_max,
2114 bool top_down, unsigned long *load_addr)
2115{
2116
2117 struct kexec_segment *ksegment;
2118 struct kexec_buf buf, *kbuf;
2119 int ret;
2120
2121 /* Currently adding segment this way is allowed only in file mode */
2122 if (!image->file_mode)
2123 return -EINVAL;
2124
2125 if (image->nr_segments >= KEXEC_SEGMENT_MAX)
2126 return -EINVAL;
2127
2128 /*
2129 * Make sure we are not trying to add buffer after allocating
2130 * control pages. All segments need to be placed first before
2131 * any control pages are allocated. As control page allocation
2132 * logic goes through list of segments to make sure there are
2133 * no destination overlaps.
2134 */
2135 if (!list_empty(&image->control_pages)) {
2136 WARN_ON(1);
2137 return -EINVAL;
2138 }
2139
2140 memset(&buf, 0, sizeof(struct kexec_buf));
2141 kbuf = &buf;
2142 kbuf->image = image;
2143 kbuf->buffer = buffer;
2144 kbuf->bufsz = bufsz;
2145
2146 kbuf->memsz = ALIGN(memsz, PAGE_SIZE);
2147 kbuf->buf_align = max(buf_align, PAGE_SIZE);
2148 kbuf->buf_min = buf_min;
2149 kbuf->buf_max = buf_max;
2150 kbuf->top_down = top_down;
2151
2152 /* Walk the RAM ranges and allocate a suitable range for the buffer */
Vivek Goyaldd5f7262014-08-08 14:26:09 -07002153 if (image->type == KEXEC_TYPE_CRASH)
2154 ret = walk_iomem_res("Crash kernel",
2155 IORESOURCE_MEM | IORESOURCE_BUSY,
2156 crashk_res.start, crashk_res.end, kbuf,
2157 locate_mem_hole_callback);
2158 else
2159 ret = walk_system_ram_res(0, -1, kbuf,
2160 locate_mem_hole_callback);
Vivek Goyalcb105252014-08-08 14:25:57 -07002161 if (ret != 1) {
2162 /* A suitable memory range could not be found for buffer */
2163 return -EADDRNOTAVAIL;
2164 }
2165
2166 /* Found a suitable memory range */
Baoquan He669280a2014-10-13 15:53:40 -07002167 ksegment = &image->segment[image->nr_segments];
2168 ksegment->kbuf = kbuf->buffer;
2169 ksegment->bufsz = kbuf->bufsz;
2170 ksegment->mem = kbuf->mem;
2171 ksegment->memsz = kbuf->memsz;
2172 image->nr_segments++;
Vivek Goyalcb105252014-08-08 14:25:57 -07002173 *load_addr = ksegment->mem;
2174 return 0;
2175}
2176
Vivek Goyal12db5562014-08-08 14:26:04 -07002177/* Calculate and store the digest of segments */
2178static int kexec_calculate_store_digests(struct kimage *image)
2179{
2180 struct crypto_shash *tfm;
2181 struct shash_desc *desc;
2182 int ret = 0, i, j, zero_buf_sz, sha_region_sz;
2183 size_t desc_size, nullsz;
2184 char *digest;
2185 void *zero_buf;
2186 struct kexec_sha_region *sha_regions;
2187 struct purgatory_info *pi = &image->purgatory_info;
2188
2189 zero_buf = __va(page_to_pfn(ZERO_PAGE(0)) << PAGE_SHIFT);
2190 zero_buf_sz = PAGE_SIZE;
2191
2192 tfm = crypto_alloc_shash("sha256", 0, 0);
2193 if (IS_ERR(tfm)) {
2194 ret = PTR_ERR(tfm);
2195 goto out;
2196 }
2197
2198 desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
2199 desc = kzalloc(desc_size, GFP_KERNEL);
2200 if (!desc) {
2201 ret = -ENOMEM;
2202 goto out_free_tfm;
2203 }
2204
2205 sha_region_sz = KEXEC_SEGMENT_MAX * sizeof(struct kexec_sha_region);
2206 sha_regions = vzalloc(sha_region_sz);
2207 if (!sha_regions)
2208 goto out_free_desc;
2209
2210 desc->tfm = tfm;
2211 desc->flags = 0;
2212
2213 ret = crypto_shash_init(desc);
2214 if (ret < 0)
2215 goto out_free_sha_regions;
2216
2217 digest = kzalloc(SHA256_DIGEST_SIZE, GFP_KERNEL);
2218 if (!digest) {
2219 ret = -ENOMEM;
2220 goto out_free_sha_regions;
2221 }
2222
2223 for (j = i = 0; i < image->nr_segments; i++) {
2224 struct kexec_segment *ksegment;
2225
2226 ksegment = &image->segment[i];
2227 /*
2228 * Skip purgatory as it will be modified once we put digest
2229 * info in purgatory.
2230 */
2231 if (ksegment->kbuf == pi->purgatory_buf)
2232 continue;
2233
2234 ret = crypto_shash_update(desc, ksegment->kbuf,
2235 ksegment->bufsz);
2236 if (ret)
2237 break;
2238
2239 /*
2240 * Assume rest of the buffer is filled with zero and
2241 * update digest accordingly.
2242 */
2243 nullsz = ksegment->memsz - ksegment->bufsz;
2244 while (nullsz) {
2245 unsigned long bytes = nullsz;
2246
2247 if (bytes > zero_buf_sz)
2248 bytes = zero_buf_sz;
2249 ret = crypto_shash_update(desc, zero_buf, bytes);
2250 if (ret)
2251 break;
2252 nullsz -= bytes;
2253 }
2254
2255 if (ret)
2256 break;
2257
2258 sha_regions[j].start = ksegment->mem;
2259 sha_regions[j].len = ksegment->memsz;
2260 j++;
2261 }
2262
2263 if (!ret) {
2264 ret = crypto_shash_final(desc, digest);
2265 if (ret)
2266 goto out_free_digest;
2267 ret = kexec_purgatory_get_set_symbol(image, "sha_regions",
2268 sha_regions, sha_region_sz, 0);
2269 if (ret)
2270 goto out_free_digest;
2271
2272 ret = kexec_purgatory_get_set_symbol(image, "sha256_digest",
2273 digest, SHA256_DIGEST_SIZE, 0);
2274 if (ret)
2275 goto out_free_digest;
2276 }
2277
2278out_free_digest:
2279 kfree(digest);
2280out_free_sha_regions:
2281 vfree(sha_regions);
2282out_free_desc:
2283 kfree(desc);
2284out_free_tfm:
2285 kfree(tfm);
2286out:
2287 return ret;
2288}
2289
2290/* Actually load purgatory. Lot of code taken from kexec-tools */
2291static int __kexec_load_purgatory(struct kimage *image, unsigned long min,
2292 unsigned long max, int top_down)
2293{
2294 struct purgatory_info *pi = &image->purgatory_info;
2295 unsigned long align, buf_align, bss_align, buf_sz, bss_sz, bss_pad;
2296 unsigned long memsz, entry, load_addr, curr_load_addr, bss_addr, offset;
2297 unsigned char *buf_addr, *src;
2298 int i, ret = 0, entry_sidx = -1;
2299 const Elf_Shdr *sechdrs_c;
2300 Elf_Shdr *sechdrs = NULL;
2301 void *purgatory_buf = NULL;
2302
2303 /*
2304 * sechdrs_c points to section headers in purgatory and are read
2305 * only. No modifications allowed.
2306 */
2307 sechdrs_c = (void *)pi->ehdr + pi->ehdr->e_shoff;
2308
2309 /*
2310 * We can not modify sechdrs_c[] and its fields. It is read only.
2311 * Copy it over to a local copy where one can store some temporary
2312 * data and free it at the end. We need to modify ->sh_addr and
2313 * ->sh_offset fields to keep track of permanent and temporary
2314 * locations of sections.
2315 */
2316 sechdrs = vzalloc(pi->ehdr->e_shnum * sizeof(Elf_Shdr));
2317 if (!sechdrs)
2318 return -ENOMEM;
2319
2320 memcpy(sechdrs, sechdrs_c, pi->ehdr->e_shnum * sizeof(Elf_Shdr));
2321
2322 /*
2323 * We seem to have multiple copies of sections. First copy is which
2324 * is embedded in kernel in read only section. Some of these sections
2325 * will be copied to a temporary buffer and relocated. And these
2326 * sections will finally be copied to their final destination at
2327 * segment load time.
2328 *
2329 * Use ->sh_offset to reflect section address in memory. It will
2330 * point to original read only copy if section is not allocatable.
2331 * Otherwise it will point to temporary copy which will be relocated.
2332 *
2333 * Use ->sh_addr to contain final address of the section where it
2334 * will go during execution time.
2335 */
2336 for (i = 0; i < pi->ehdr->e_shnum; i++) {
2337 if (sechdrs[i].sh_type == SHT_NOBITS)
2338 continue;
2339
2340 sechdrs[i].sh_offset = (unsigned long)pi->ehdr +
2341 sechdrs[i].sh_offset;
2342 }
2343
2344 /*
2345 * Identify entry point section and make entry relative to section
2346 * start.
2347 */
2348 entry = pi->ehdr->e_entry;
2349 for (i = 0; i < pi->ehdr->e_shnum; i++) {
2350 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2351 continue;
2352
2353 if (!(sechdrs[i].sh_flags & SHF_EXECINSTR))
2354 continue;
2355
2356 /* Make entry section relative */
2357 if (sechdrs[i].sh_addr <= pi->ehdr->e_entry &&
2358 ((sechdrs[i].sh_addr + sechdrs[i].sh_size) >
2359 pi->ehdr->e_entry)) {
2360 entry_sidx = i;
2361 entry -= sechdrs[i].sh_addr;
2362 break;
2363 }
2364 }
2365
2366 /* Determine how much memory is needed to load relocatable object. */
2367 buf_align = 1;
2368 bss_align = 1;
2369 buf_sz = 0;
2370 bss_sz = 0;
2371
2372 for (i = 0; i < pi->ehdr->e_shnum; i++) {
2373 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2374 continue;
2375
2376 align = sechdrs[i].sh_addralign;
2377 if (sechdrs[i].sh_type != SHT_NOBITS) {
2378 if (buf_align < align)
2379 buf_align = align;
2380 buf_sz = ALIGN(buf_sz, align);
2381 buf_sz += sechdrs[i].sh_size;
2382 } else {
2383 /* bss section */
2384 if (bss_align < align)
2385 bss_align = align;
2386 bss_sz = ALIGN(bss_sz, align);
2387 bss_sz += sechdrs[i].sh_size;
2388 }
2389 }
2390
2391 /* Determine the bss padding required to align bss properly */
2392 bss_pad = 0;
2393 if (buf_sz & (bss_align - 1))
2394 bss_pad = bss_align - (buf_sz & (bss_align - 1));
2395
2396 memsz = buf_sz + bss_pad + bss_sz;
2397
2398 /* Allocate buffer for purgatory */
2399 purgatory_buf = vzalloc(buf_sz);
2400 if (!purgatory_buf) {
2401 ret = -ENOMEM;
2402 goto out;
2403 }
2404
2405 if (buf_align < bss_align)
2406 buf_align = bss_align;
2407
2408 /* Add buffer to segment list */
2409 ret = kexec_add_buffer(image, purgatory_buf, buf_sz, memsz,
2410 buf_align, min, max, top_down,
2411 &pi->purgatory_load_addr);
2412 if (ret)
2413 goto out;
2414
2415 /* Load SHF_ALLOC sections */
2416 buf_addr = purgatory_buf;
2417 load_addr = curr_load_addr = pi->purgatory_load_addr;
2418 bss_addr = load_addr + buf_sz + bss_pad;
2419
2420 for (i = 0; i < pi->ehdr->e_shnum; i++) {
2421 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2422 continue;
2423
2424 align = sechdrs[i].sh_addralign;
2425 if (sechdrs[i].sh_type != SHT_NOBITS) {
2426 curr_load_addr = ALIGN(curr_load_addr, align);
2427 offset = curr_load_addr - load_addr;
2428 /* We already modifed ->sh_offset to keep src addr */
2429 src = (char *) sechdrs[i].sh_offset;
2430 memcpy(buf_addr + offset, src, sechdrs[i].sh_size);
2431
2432 /* Store load address and source address of section */
2433 sechdrs[i].sh_addr = curr_load_addr;
2434
2435 /*
2436 * This section got copied to temporary buffer. Update
2437 * ->sh_offset accordingly.
2438 */
2439 sechdrs[i].sh_offset = (unsigned long)(buf_addr + offset);
2440
2441 /* Advance to the next address */
2442 curr_load_addr += sechdrs[i].sh_size;
2443 } else {
2444 bss_addr = ALIGN(bss_addr, align);
2445 sechdrs[i].sh_addr = bss_addr;
2446 bss_addr += sechdrs[i].sh_size;
2447 }
2448 }
2449
2450 /* Update entry point based on load address of text section */
2451 if (entry_sidx >= 0)
2452 entry += sechdrs[entry_sidx].sh_addr;
2453
2454 /* Make kernel jump to purgatory after shutdown */
2455 image->start = entry;
2456
2457 /* Used later to get/set symbol values */
2458 pi->sechdrs = sechdrs;
2459
2460 /*
2461 * Used later to identify which section is purgatory and skip it
2462 * from checksumming.
2463 */
2464 pi->purgatory_buf = purgatory_buf;
2465 return ret;
2466out:
2467 vfree(sechdrs);
2468 vfree(purgatory_buf);
2469 return ret;
2470}
2471
2472static int kexec_apply_relocations(struct kimage *image)
2473{
2474 int i, ret;
2475 struct purgatory_info *pi = &image->purgatory_info;
2476 Elf_Shdr *sechdrs = pi->sechdrs;
2477
2478 /* Apply relocations */
2479 for (i = 0; i < pi->ehdr->e_shnum; i++) {
2480 Elf_Shdr *section, *symtab;
2481
2482 if (sechdrs[i].sh_type != SHT_RELA &&
2483 sechdrs[i].sh_type != SHT_REL)
2484 continue;
2485
2486 /*
2487 * For section of type SHT_RELA/SHT_REL,
2488 * ->sh_link contains section header index of associated
2489 * symbol table. And ->sh_info contains section header
2490 * index of section to which relocations apply.
2491 */
2492 if (sechdrs[i].sh_info >= pi->ehdr->e_shnum ||
2493 sechdrs[i].sh_link >= pi->ehdr->e_shnum)
2494 return -ENOEXEC;
2495
2496 section = &sechdrs[sechdrs[i].sh_info];
2497 symtab = &sechdrs[sechdrs[i].sh_link];
2498
2499 if (!(section->sh_flags & SHF_ALLOC))
2500 continue;
2501
2502 /*
2503 * symtab->sh_link contain section header index of associated
2504 * string table.
2505 */
2506 if (symtab->sh_link >= pi->ehdr->e_shnum)
2507 /* Invalid section number? */
2508 continue;
2509
2510 /*
Borislav Petkovedb0ec02015-01-25 19:50:34 +01002511 * Respective architecture needs to provide support for applying
Vivek Goyal12db5562014-08-08 14:26:04 -07002512 * relocations of type SHT_RELA/SHT_REL.
2513 */
2514 if (sechdrs[i].sh_type == SHT_RELA)
2515 ret = arch_kexec_apply_relocations_add(pi->ehdr,
2516 sechdrs, i);
2517 else if (sechdrs[i].sh_type == SHT_REL)
2518 ret = arch_kexec_apply_relocations(pi->ehdr,
2519 sechdrs, i);
2520 if (ret)
2521 return ret;
2522 }
2523
2524 return 0;
2525}
2526
2527/* Load relocatable purgatory object and relocate it appropriately */
2528int kexec_load_purgatory(struct kimage *image, unsigned long min,
2529 unsigned long max, int top_down,
2530 unsigned long *load_addr)
2531{
2532 struct purgatory_info *pi = &image->purgatory_info;
2533 int ret;
2534
2535 if (kexec_purgatory_size <= 0)
2536 return -EINVAL;
2537
2538 if (kexec_purgatory_size < sizeof(Elf_Ehdr))
2539 return -ENOEXEC;
2540
2541 pi->ehdr = (Elf_Ehdr *)kexec_purgatory;
2542
2543 if (memcmp(pi->ehdr->e_ident, ELFMAG, SELFMAG) != 0
2544 || pi->ehdr->e_type != ET_REL
2545 || !elf_check_arch(pi->ehdr)
2546 || pi->ehdr->e_shentsize != sizeof(Elf_Shdr))
2547 return -ENOEXEC;
2548
2549 if (pi->ehdr->e_shoff >= kexec_purgatory_size
2550 || (pi->ehdr->e_shnum * sizeof(Elf_Shdr) >
2551 kexec_purgatory_size - pi->ehdr->e_shoff))
2552 return -ENOEXEC;
2553
2554 ret = __kexec_load_purgatory(image, min, max, top_down);
2555 if (ret)
2556 return ret;
2557
2558 ret = kexec_apply_relocations(image);
2559 if (ret)
2560 goto out;
2561
2562 *load_addr = pi->purgatory_load_addr;
2563 return 0;
2564out:
2565 vfree(pi->sechdrs);
2566 vfree(pi->purgatory_buf);
2567 return ret;
2568}
2569
2570static Elf_Sym *kexec_purgatory_find_symbol(struct purgatory_info *pi,
2571 const char *name)
2572{
2573 Elf_Sym *syms;
2574 Elf_Shdr *sechdrs;
2575 Elf_Ehdr *ehdr;
2576 int i, k;
2577 const char *strtab;
2578
2579 if (!pi->sechdrs || !pi->ehdr)
2580 return NULL;
2581
2582 sechdrs = pi->sechdrs;
2583 ehdr = pi->ehdr;
2584
2585 for (i = 0; i < ehdr->e_shnum; i++) {
2586 if (sechdrs[i].sh_type != SHT_SYMTAB)
2587 continue;
2588
2589 if (sechdrs[i].sh_link >= ehdr->e_shnum)
2590 /* Invalid strtab section number */
2591 continue;
2592 strtab = (char *)sechdrs[sechdrs[i].sh_link].sh_offset;
2593 syms = (Elf_Sym *)sechdrs[i].sh_offset;
2594
2595 /* Go through symbols for a match */
2596 for (k = 0; k < sechdrs[i].sh_size/sizeof(Elf_Sym); k++) {
2597 if (ELF_ST_BIND(syms[k].st_info) != STB_GLOBAL)
2598 continue;
2599
2600 if (strcmp(strtab + syms[k].st_name, name) != 0)
2601 continue;
2602
2603 if (syms[k].st_shndx == SHN_UNDEF ||
2604 syms[k].st_shndx >= ehdr->e_shnum) {
2605 pr_debug("Symbol: %s has bad section index %d.\n",
2606 name, syms[k].st_shndx);
2607 return NULL;
2608 }
2609
2610 /* Found the symbol we are looking for */
2611 return &syms[k];
2612 }
2613 }
2614
2615 return NULL;
2616}
2617
2618void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name)
2619{
2620 struct purgatory_info *pi = &image->purgatory_info;
2621 Elf_Sym *sym;
2622 Elf_Shdr *sechdr;
2623
2624 sym = kexec_purgatory_find_symbol(pi, name);
2625 if (!sym)
2626 return ERR_PTR(-EINVAL);
2627
2628 sechdr = &pi->sechdrs[sym->st_shndx];
2629
2630 /*
2631 * Returns the address where symbol will finally be loaded after
2632 * kexec_load_segment()
2633 */
2634 return (void *)(sechdr->sh_addr + sym->st_value);
2635}
2636
2637/*
2638 * Get or set value of a symbol. If "get_value" is true, symbol value is
2639 * returned in buf otherwise symbol value is set based on value in buf.
2640 */
2641int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name,
2642 void *buf, unsigned int size, bool get_value)
2643{
2644 Elf_Sym *sym;
2645 Elf_Shdr *sechdrs;
2646 struct purgatory_info *pi = &image->purgatory_info;
2647 char *sym_buf;
2648
2649 sym = kexec_purgatory_find_symbol(pi, name);
2650 if (!sym)
2651 return -EINVAL;
2652
2653 if (sym->st_size != size) {
2654 pr_err("symbol %s size mismatch: expected %lu actual %u\n",
2655 name, (unsigned long)sym->st_size, size);
2656 return -EINVAL;
2657 }
2658
2659 sechdrs = pi->sechdrs;
2660
2661 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
2662 pr_err("symbol %s is in a bss section. Cannot %s\n", name,
2663 get_value ? "get" : "set");
2664 return -EINVAL;
2665 }
2666
2667 sym_buf = (unsigned char *)sechdrs[sym->st_shndx].sh_offset +
2668 sym->st_value;
2669
2670 if (get_value)
2671 memcpy((void *)buf, sym_buf, size);
2672 else
2673 memcpy((void *)sym_buf, buf, size);
2674
2675 return 0;
2676}
Vivek Goyal74ca3172014-08-29 15:18:46 -07002677#endif /* CONFIG_KEXEC_FILE */
Vivek Goyalcb105252014-08-08 14:25:57 -07002678
Huang Ying7ade3fc2008-08-15 00:40:21 -07002679/*
2680 * Move into place and start executing a preloaded standalone
2681 * executable. If nothing was preloaded return an error.
Huang Ying3ab83522008-07-25 19:45:07 -07002682 */
2683int kernel_kexec(void)
2684{
2685 int error = 0;
2686
Andrew Morton8c5a1cf2008-08-15 00:40:27 -07002687 if (!mutex_trylock(&kexec_mutex))
Huang Ying3ab83522008-07-25 19:45:07 -07002688 return -EBUSY;
2689 if (!kexec_image) {
2690 error = -EINVAL;
2691 goto Unlock;
2692 }
2693
Huang Ying3ab83522008-07-25 19:45:07 -07002694#ifdef CONFIG_KEXEC_JUMP
Huang Ying7ade3fc2008-08-15 00:40:21 -07002695 if (kexec_image->preserve_context) {
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01002696 lock_system_sleep();
Huang Ying89081d12008-07-25 19:45:10 -07002697 pm_prepare_console();
2698 error = freeze_processes();
2699 if (error) {
2700 error = -EBUSY;
2701 goto Restore_console;
2702 }
2703 suspend_console();
Alan Sternd1616302009-05-24 22:05:42 +02002704 error = dpm_suspend_start(PMSG_FREEZE);
Huang Ying89081d12008-07-25 19:45:10 -07002705 if (error)
2706 goto Resume_console;
Alan Sternd1616302009-05-24 22:05:42 +02002707 /* At this point, dpm_suspend_start() has been called,
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01002708 * but *not* dpm_suspend_end(). We *must* call
2709 * dpm_suspend_end() now. Otherwise, drivers for
Huang Ying89081d12008-07-25 19:45:10 -07002710 * some devices (e.g. interrupt controllers) become
2711 * desynchronized with the actual state of the
2712 * hardware at resume time, and evil weirdness ensues.
2713 */
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01002714 error = dpm_suspend_end(PMSG_FREEZE);
Huang Ying89081d12008-07-25 19:45:10 -07002715 if (error)
Rafael J. Wysocki749b0af2009-03-16 22:34:35 +01002716 goto Resume_devices;
2717 error = disable_nonboot_cpus();
2718 if (error)
2719 goto Enable_cpus;
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +01002720 local_irq_disable();
Rafael J. Wysocki2e711c02011-04-26 19:15:07 +02002721 error = syscore_suspend();
Rafael J. Wysocki770824b2009-02-22 18:38:50 +01002722 if (error)
Rafael J. Wysocki749b0af2009-03-16 22:34:35 +01002723 goto Enable_irqs;
Huang Ying7ade3fc2008-08-15 00:40:21 -07002724 } else
Huang Ying3ab83522008-07-25 19:45:07 -07002725#endif
Huang Ying7ade3fc2008-08-15 00:40:21 -07002726 {
Khalid Aziz4fc9bbf2013-11-27 15:19:25 -07002727 kexec_in_progress = true;
Huang Yingca195b72008-08-15 00:40:24 -07002728 kernel_restart_prepare(NULL);
Vivek Goyalc97102b2013-12-18 17:08:31 -08002729 migrate_to_reboot_cpu();
Srivatsa S. Bhat011e4b02014-05-27 16:25:34 +05302730
2731 /*
2732 * migrate_to_reboot_cpu() disables CPU hotplug assuming that
2733 * no further code needs to use CPU hotplug (which is true in
2734 * the reboot case). However, the kexec path depends on using
2735 * CPU hotplug again; so re-enable it here.
2736 */
2737 cpu_hotplug_enable();
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07002738 pr_emerg("Starting new kernel\n");
Huang Ying3ab83522008-07-25 19:45:07 -07002739 machine_shutdown();
2740 }
2741
2742 machine_kexec(kexec_image);
2743
Huang Ying3ab83522008-07-25 19:45:07 -07002744#ifdef CONFIG_KEXEC_JUMP
Huang Ying7ade3fc2008-08-15 00:40:21 -07002745 if (kexec_image->preserve_context) {
Rafael J. Wysocki19234c02011-04-20 00:36:11 +02002746 syscore_resume();
Rafael J. Wysocki749b0af2009-03-16 22:34:35 +01002747 Enable_irqs:
Huang Ying3ab83522008-07-25 19:45:07 -07002748 local_irq_enable();
Rafael J. Wysocki749b0af2009-03-16 22:34:35 +01002749 Enable_cpus:
Huang Ying89081d12008-07-25 19:45:10 -07002750 enable_nonboot_cpus();
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01002751 dpm_resume_start(PMSG_RESTORE);
Huang Ying89081d12008-07-25 19:45:10 -07002752 Resume_devices:
Alan Sternd1616302009-05-24 22:05:42 +02002753 dpm_resume_end(PMSG_RESTORE);
Huang Ying89081d12008-07-25 19:45:10 -07002754 Resume_console:
2755 resume_console();
2756 thaw_processes();
2757 Restore_console:
2758 pm_restore_console();
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01002759 unlock_system_sleep();
Huang Ying3ab83522008-07-25 19:45:07 -07002760 }
Huang Ying7ade3fc2008-08-15 00:40:21 -07002761#endif
Huang Ying3ab83522008-07-25 19:45:07 -07002762
2763 Unlock:
Andrew Morton8c5a1cf2008-08-15 00:40:27 -07002764 mutex_unlock(&kexec_mutex);
Huang Ying3ab83522008-07-25 19:45:07 -07002765 return error;
2766}