blob: a785c1015e25bf1ecacd3a6d92956e3e630e7f37 [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{
HATAYAMA Daisuke5375b702015-06-30 14:57:46 -070087 /*
88 * If crash_kexec_post_notifiers is enabled, don't run
89 * crash_kexec() here yet, which must be run after panic
90 * notifiers in panic().
91 */
92 if (crash_kexec_post_notifiers)
93 return 0;
94 /*
95 * There are 4 panic() calls in do_exit() path, each of which
96 * corresponds to each of these 4 conditions.
97 */
Serge E. Hallynb460cbc2007-10-18 23:39:52 -070098 if (in_interrupt() || !p->pid || is_global_init(p) || panic_on_oops)
Alexander Nyberg6e274d12005-06-25 14:58:26 -070099 return 1;
100 return 0;
101}
102
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700103/*
104 * When kexec transitions to the new kernel there is a one-to-one
105 * mapping between physical and virtual addresses. On processors
106 * where you can disable the MMU this is trivial, and easy. For
107 * others it is still a simple predictable page table to setup.
108 *
109 * In that environment kexec copies the new kernel to its final
110 * resting place. This means I can only support memory whose
111 * physical address can fit in an unsigned long. In particular
112 * addresses where (pfn << PAGE_SHIFT) > ULONG_MAX cannot be handled.
113 * If the assembly stub has more restrictive requirements
114 * KEXEC_SOURCE_MEMORY_LIMIT and KEXEC_DEST_MEMORY_LIMIT can be
115 * defined more restrictively in <asm/kexec.h>.
116 *
117 * The code for the transition from the current kernel to the
118 * the new kernel is placed in the control_code_buffer, whose size
Huang Ying163f6872008-08-15 00:40:22 -0700119 * is given by KEXEC_CONTROL_PAGE_SIZE. In the best case only a single
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700120 * page of memory is necessary, but some architectures require more.
121 * Because this memory must be identity mapped in the transition from
122 * virtual to physical addresses it must live in the range
123 * 0 - TASK_SIZE, as only the user space mappings are arbitrarily
124 * modifiable.
125 *
126 * The assembly stub in the control code buffer is passed a linked list
127 * of descriptor pages detailing the source pages of the new kernel,
128 * and the destination addresses of those source pages. As this data
129 * structure is not used in the context of the current OS, it must
130 * be self-contained.
131 *
132 * The code has been made to work with highmem pages and will use a
133 * destination page in its final resting place (if it happens
134 * to allocate it). The end product of this is that most of the
135 * physical address space, and most of RAM can be used.
136 *
137 * Future directions include:
138 * - allocating a page table with the control code buffer identity
139 * mapped, to simplify machine_kexec and make kexec_on_panic more
140 * reliable.
141 */
142
143/*
144 * KIMAGE_NO_DEST is an impossible destination address..., for
145 * allocating pages whose destination address we do not care about.
146 */
147#define KIMAGE_NO_DEST (-1UL)
148
Maneesh Soni72414d32005-06-25 14:58:28 -0700149static int kimage_is_destination_range(struct kimage *image,
150 unsigned long start, unsigned long end);
151static struct page *kimage_alloc_page(struct kimage *image,
Al Viro9796fdd2005-10-21 03:22:03 -0400152 gfp_t gfp_mask,
Maneesh Soni72414d32005-06-25 14:58:28 -0700153 unsigned long dest);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700154
Vivek Goyaldabe7862014-08-08 14:25:45 -0700155static int copy_user_segment_list(struct kimage *image,
156 unsigned long nr_segments,
157 struct kexec_segment __user *segments)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700158{
Vivek Goyaldabe7862014-08-08 14:25:45 -0700159 int ret;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700160 size_t segment_bytes;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700161
162 /* Read in the segments */
163 image->nr_segments = nr_segments;
164 segment_bytes = nr_segments * sizeof(*segments);
Vivek Goyaldabe7862014-08-08 14:25:45 -0700165 ret = copy_from_user(image->segment, segments, segment_bytes);
166 if (ret)
167 ret = -EFAULT;
168
169 return ret;
170}
171
172static int sanity_check_segment_list(struct kimage *image)
173{
174 int result, i;
175 unsigned long nr_segments = image->nr_segments;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700176
177 /*
178 * Verify we have good destination addresses. The caller is
179 * responsible for making certain we don't attempt to load
180 * the new image into invalid or reserved areas of RAM. This
181 * just verifies it is an address we can use.
182 *
183 * Since the kernel does everything in page size chunks ensure
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400184 * the destination addresses are page aligned. Too many
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700185 * special cases crop of when we don't do this. The most
186 * insidious is getting overlapping destination addresses
187 * simply because addresses are changed to page size
188 * granularity.
189 */
190 result = -EADDRNOTAVAIL;
191 for (i = 0; i < nr_segments; i++) {
192 unsigned long mstart, mend;
Maneesh Soni72414d32005-06-25 14:58:28 -0700193
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700194 mstart = image->segment[i].mem;
195 mend = mstart + image->segment[i].memsz;
196 if ((mstart & ~PAGE_MASK) || (mend & ~PAGE_MASK))
Vivek Goyaldabe7862014-08-08 14:25:45 -0700197 return result;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700198 if (mend >= KEXEC_DESTINATION_MEMORY_LIMIT)
Vivek Goyaldabe7862014-08-08 14:25:45 -0700199 return result;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700200 }
201
202 /* Verify our destination addresses do not overlap.
203 * If we alloed overlapping destination addresses
204 * through very weird things can happen with no
205 * easy explanation as one segment stops on another.
206 */
207 result = -EINVAL;
Maneesh Soni72414d32005-06-25 14:58:28 -0700208 for (i = 0; i < nr_segments; i++) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700209 unsigned long mstart, mend;
210 unsigned long j;
Maneesh Soni72414d32005-06-25 14:58:28 -0700211
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700212 mstart = image->segment[i].mem;
213 mend = mstart + image->segment[i].memsz;
Maneesh Soni72414d32005-06-25 14:58:28 -0700214 for (j = 0; j < i; j++) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700215 unsigned long pstart, pend;
216 pstart = image->segment[j].mem;
217 pend = pstart + image->segment[j].memsz;
218 /* Do the segments overlap ? */
219 if ((mend > pstart) && (mstart < pend))
Vivek Goyaldabe7862014-08-08 14:25:45 -0700220 return result;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700221 }
222 }
223
224 /* Ensure our buffer sizes are strictly less than
225 * our memory sizes. This should always be the case,
226 * and it is easier to check up front than to be surprised
227 * later on.
228 */
229 result = -EINVAL;
Maneesh Soni72414d32005-06-25 14:58:28 -0700230 for (i = 0; i < nr_segments; i++) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700231 if (image->segment[i].bufsz > image->segment[i].memsz)
Vivek Goyaldabe7862014-08-08 14:25:45 -0700232 return result;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700233 }
234
Vivek Goyaldabe7862014-08-08 14:25:45 -0700235 /*
236 * Verify we have good destination addresses. Normally
237 * the caller is responsible for making certain we don't
238 * attempt to load the new image into invalid or reserved
239 * areas of RAM. But crash kernels are preloaded into a
240 * reserved area of ram. We must ensure the addresses
241 * are in the reserved area otherwise preloading the
242 * kernel could corrupt things.
243 */
Maneesh Soni72414d32005-06-25 14:58:28 -0700244
Vivek Goyaldabe7862014-08-08 14:25:45 -0700245 if (image->type == KEXEC_TYPE_CRASH) {
246 result = -EADDRNOTAVAIL;
247 for (i = 0; i < nr_segments; i++) {
248 unsigned long mstart, mend;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700249
Vivek Goyaldabe7862014-08-08 14:25:45 -0700250 mstart = image->segment[i].mem;
251 mend = mstart + image->segment[i].memsz - 1;
252 /* Ensure we are within the crash kernel limits */
253 if ((mstart < crashk_res.start) ||
254 (mend > crashk_res.end))
255 return result;
256 }
257 }
258
259 return 0;
260}
261
262static struct kimage *do_kimage_alloc_init(void)
263{
264 struct kimage *image;
265
266 /* Allocate a controlling structure */
267 image = kzalloc(sizeof(*image), GFP_KERNEL);
268 if (!image)
269 return NULL;
270
271 image->head = 0;
272 image->entry = &image->head;
273 image->last_entry = &image->head;
274 image->control_page = ~0; /* By default this does not apply */
275 image->type = KEXEC_TYPE_DEFAULT;
276
277 /* Initialize the list of control pages */
278 INIT_LIST_HEAD(&image->control_pages);
279
280 /* Initialize the list of destination pages */
281 INIT_LIST_HEAD(&image->dest_pages);
282
283 /* Initialize the list of unusable pages */
284 INIT_LIST_HEAD(&image->unusable_pages);
285
286 return image;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700287}
288
Zhang Yanfeib92e7e02013-02-27 17:03:29 -0800289static void kimage_free_page_list(struct list_head *list);
290
Vivek Goyal255aedd2014-08-08 14:25:48 -0700291static int kimage_alloc_init(struct kimage **rimage, unsigned long entry,
292 unsigned long nr_segments,
293 struct kexec_segment __user *segments,
294 unsigned long flags)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700295{
Vivek Goyal255aedd2014-08-08 14:25:48 -0700296 int ret;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700297 struct kimage *image;
Vivek Goyal255aedd2014-08-08 14:25:48 -0700298 bool kexec_on_panic = flags & KEXEC_ON_CRASH;
299
300 if (kexec_on_panic) {
301 /* Verify we have a valid entry point */
302 if ((entry < crashk_res.start) || (entry > crashk_res.end))
303 return -EADDRNOTAVAIL;
304 }
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700305
306 /* Allocate and initialize a controlling structure */
Vivek Goyaldabe7862014-08-08 14:25:45 -0700307 image = do_kimage_alloc_init();
308 if (!image)
309 return -ENOMEM;
310
311 image->start = entry;
312
Vivek Goyal255aedd2014-08-08 14:25:48 -0700313 ret = copy_user_segment_list(image, nr_segments, segments);
314 if (ret)
Vivek Goyaldabe7862014-08-08 14:25:45 -0700315 goto out_free_image;
316
Vivek Goyal255aedd2014-08-08 14:25:48 -0700317 ret = sanity_check_segment_list(image);
318 if (ret)
Vivek Goyaldabe7862014-08-08 14:25:45 -0700319 goto out_free_image;
Maneesh Soni72414d32005-06-25 14:58:28 -0700320
Vivek Goyal255aedd2014-08-08 14:25:48 -0700321 /* Enable the special crash kernel control page allocation policy. */
322 if (kexec_on_panic) {
323 image->control_page = crashk_res.start;
324 image->type = KEXEC_TYPE_CRASH;
325 }
326
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700327 /*
328 * Find a location for the control code buffer, and add it
329 * the vector of segments so that it's pages will also be
330 * counted as destination pages.
331 */
Vivek Goyal255aedd2014-08-08 14:25:48 -0700332 ret = -ENOMEM;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700333 image->control_code_page = kimage_alloc_control_pages(image,
Huang Ying163f6872008-08-15 00:40:22 -0700334 get_order(KEXEC_CONTROL_PAGE_SIZE));
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700335 if (!image->control_code_page) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -0700336 pr_err("Could not allocate control_code_buffer\n");
Vivek Goyaldabe7862014-08-08 14:25:45 -0700337 goto out_free_image;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700338 }
339
Vivek Goyal255aedd2014-08-08 14:25:48 -0700340 if (!kexec_on_panic) {
341 image->swap_page = kimage_alloc_control_pages(image, 0);
342 if (!image->swap_page) {
343 pr_err("Could not allocate swap buffer\n");
344 goto out_free_control_pages;
345 }
Huang Ying3ab83522008-07-25 19:45:07 -0700346 }
347
Zhang Yanfeib92e7e02013-02-27 17:03:29 -0800348 *rimage = image;
349 return 0;
Vivek Goyaldabe7862014-08-08 14:25:45 -0700350out_free_control_pages:
Zhang Yanfeib92e7e02013-02-27 17:03:29 -0800351 kimage_free_page_list(&image->control_pages);
Vivek Goyaldabe7862014-08-08 14:25:45 -0700352out_free_image:
Zhang Yanfeib92e7e02013-02-27 17:03:29 -0800353 kfree(image);
Vivek Goyal255aedd2014-08-08 14:25:48 -0700354 return ret;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700355}
356
Vivek Goyal74ca3172014-08-29 15:18:46 -0700357#ifdef CONFIG_KEXEC_FILE
Vivek Goyalcb105252014-08-08 14:25:57 -0700358static int copy_file_from_fd(int fd, void **buf, unsigned long *buf_len)
359{
360 struct fd f = fdget(fd);
361 int ret;
362 struct kstat stat;
363 loff_t pos;
364 ssize_t bytes = 0;
365
366 if (!f.file)
367 return -EBADF;
368
369 ret = vfs_getattr(&f.file->f_path, &stat);
370 if (ret)
371 goto out;
372
373 if (stat.size > INT_MAX) {
374 ret = -EFBIG;
375 goto out;
376 }
377
378 /* Don't hand 0 to vmalloc, it whines. */
379 if (stat.size == 0) {
380 ret = -EINVAL;
381 goto out;
382 }
383
384 *buf = vmalloc(stat.size);
385 if (!*buf) {
386 ret = -ENOMEM;
387 goto out;
388 }
389
390 pos = 0;
391 while (pos < stat.size) {
392 bytes = kernel_read(f.file, pos, (char *)(*buf) + pos,
393 stat.size - pos);
394 if (bytes < 0) {
395 vfree(*buf);
396 ret = bytes;
397 goto out;
398 }
399
400 if (bytes == 0)
401 break;
402 pos += bytes;
403 }
404
405 if (pos != stat.size) {
406 ret = -EBADF;
407 vfree(*buf);
408 goto out;
409 }
410
411 *buf_len = pos;
412out:
413 fdput(f);
414 return ret;
415}
416
417/* Architectures can provide this probe function */
418int __weak arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
419 unsigned long buf_len)
420{
421 return -ENOEXEC;
422}
423
424void * __weak arch_kexec_kernel_image_load(struct kimage *image)
425{
426 return ERR_PTR(-ENOEXEC);
427}
428
429void __weak arch_kimage_file_post_load_cleanup(struct kimage *image)
430{
431}
432
Vivek Goyal8e7d8382014-08-08 14:26:13 -0700433int __weak arch_kexec_kernel_verify_sig(struct kimage *image, void *buf,
434 unsigned long buf_len)
435{
436 return -EKEYREJECTED;
437}
438
Vivek Goyal12db5562014-08-08 14:26:04 -0700439/* Apply relocations of type RELA */
440int __weak
441arch_kexec_apply_relocations_add(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
442 unsigned int relsec)
443{
444 pr_err("RELA relocation unsupported.\n");
445 return -ENOEXEC;
446}
447
448/* Apply relocations of type REL */
449int __weak
450arch_kexec_apply_relocations(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
451 unsigned int relsec)
452{
453 pr_err("REL relocation unsupported.\n");
454 return -ENOEXEC;
455}
456
Vivek Goyalcb105252014-08-08 14:25:57 -0700457/*
Alexander Kuleshovad699342015-02-17 13:45:47 -0800458 * Free up memory used by kernel, initrd, and command line. This is temporary
Vivek Goyalcb105252014-08-08 14:25:57 -0700459 * memory allocation which is not needed any more after these buffers have
460 * been loaded into separate segments and have been copied elsewhere.
461 */
462static void kimage_file_post_load_cleanup(struct kimage *image)
463{
Vivek Goyal12db5562014-08-08 14:26:04 -0700464 struct purgatory_info *pi = &image->purgatory_info;
465
Vivek Goyalcb105252014-08-08 14:25:57 -0700466 vfree(image->kernel_buf);
467 image->kernel_buf = NULL;
468
469 vfree(image->initrd_buf);
470 image->initrd_buf = NULL;
471
472 kfree(image->cmdline_buf);
473 image->cmdline_buf = NULL;
474
Vivek Goyal12db5562014-08-08 14:26:04 -0700475 vfree(pi->purgatory_buf);
476 pi->purgatory_buf = NULL;
477
478 vfree(pi->sechdrs);
479 pi->sechdrs = NULL;
480
Vivek Goyalcb105252014-08-08 14:25:57 -0700481 /* See if architecture has anything to cleanup post load */
482 arch_kimage_file_post_load_cleanup(image);
Vivek Goyal27f48d32014-08-08 14:26:06 -0700483
484 /*
485 * Above call should have called into bootloader to free up
486 * any data stored in kimage->image_loader_data. It should
487 * be ok now to free it up.
488 */
489 kfree(image->image_loader_data);
490 image->image_loader_data = NULL;
Vivek Goyalcb105252014-08-08 14:25:57 -0700491}
492
493/*
494 * In file mode list of segments is prepared by kernel. Copy relevant
495 * data from user space, do error checking, prepare segment list
496 */
497static int
498kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd,
499 const char __user *cmdline_ptr,
500 unsigned long cmdline_len, unsigned flags)
501{
502 int ret = 0;
503 void *ldata;
504
505 ret = copy_file_from_fd(kernel_fd, &image->kernel_buf,
506 &image->kernel_buf_len);
507 if (ret)
508 return ret;
509
510 /* Call arch image probe handlers */
511 ret = arch_kexec_kernel_image_probe(image, image->kernel_buf,
512 image->kernel_buf_len);
513
514 if (ret)
515 goto out;
516
Vivek Goyal8e7d8382014-08-08 14:26:13 -0700517#ifdef CONFIG_KEXEC_VERIFY_SIG
518 ret = arch_kexec_kernel_verify_sig(image, image->kernel_buf,
519 image->kernel_buf_len);
520 if (ret) {
521 pr_debug("kernel signature verification failed.\n");
522 goto out;
523 }
524 pr_debug("kernel signature verification successful.\n");
525#endif
Vivek Goyalcb105252014-08-08 14:25:57 -0700526 /* It is possible that there no initramfs is being loaded */
527 if (!(flags & KEXEC_FILE_NO_INITRAMFS)) {
528 ret = copy_file_from_fd(initrd_fd, &image->initrd_buf,
529 &image->initrd_buf_len);
530 if (ret)
531 goto out;
532 }
533
534 if (cmdline_len) {
535 image->cmdline_buf = kzalloc(cmdline_len, GFP_KERNEL);
536 if (!image->cmdline_buf) {
537 ret = -ENOMEM;
538 goto out;
539 }
540
541 ret = copy_from_user(image->cmdline_buf, cmdline_ptr,
542 cmdline_len);
543 if (ret) {
544 ret = -EFAULT;
545 goto out;
546 }
547
548 image->cmdline_buf_len = cmdline_len;
549
550 /* command line should be a string with last byte null */
551 if (image->cmdline_buf[cmdline_len - 1] != '\0') {
552 ret = -EINVAL;
553 goto out;
554 }
555 }
556
557 /* Call arch image load handlers */
558 ldata = arch_kexec_kernel_image_load(image);
559
560 if (IS_ERR(ldata)) {
561 ret = PTR_ERR(ldata);
562 goto out;
563 }
564
565 image->image_loader_data = ldata;
566out:
567 /* In case of error, free up all allocated memory in this function */
568 if (ret)
569 kimage_file_post_load_cleanup(image);
570 return ret;
571}
572
573static int
574kimage_file_alloc_init(struct kimage **rimage, int kernel_fd,
575 int initrd_fd, const char __user *cmdline_ptr,
576 unsigned long cmdline_len, unsigned long flags)
577{
578 int ret;
579 struct kimage *image;
Vivek Goyaldd5f7262014-08-08 14:26:09 -0700580 bool kexec_on_panic = flags & KEXEC_FILE_ON_CRASH;
Vivek Goyalcb105252014-08-08 14:25:57 -0700581
582 image = do_kimage_alloc_init();
583 if (!image)
584 return -ENOMEM;
585
586 image->file_mode = 1;
587
Vivek Goyaldd5f7262014-08-08 14:26:09 -0700588 if (kexec_on_panic) {
589 /* Enable special crash kernel control page alloc policy. */
590 image->control_page = crashk_res.start;
591 image->type = KEXEC_TYPE_CRASH;
592 }
593
Vivek Goyalcb105252014-08-08 14:25:57 -0700594 ret = kimage_file_prepare_segments(image, kernel_fd, initrd_fd,
595 cmdline_ptr, cmdline_len, flags);
596 if (ret)
597 goto out_free_image;
598
599 ret = sanity_check_segment_list(image);
600 if (ret)
601 goto out_free_post_load_bufs;
602
603 ret = -ENOMEM;
604 image->control_code_page = kimage_alloc_control_pages(image,
605 get_order(KEXEC_CONTROL_PAGE_SIZE));
606 if (!image->control_code_page) {
607 pr_err("Could not allocate control_code_buffer\n");
608 goto out_free_post_load_bufs;
609 }
610
Vivek Goyaldd5f7262014-08-08 14:26:09 -0700611 if (!kexec_on_panic) {
612 image->swap_page = kimage_alloc_control_pages(image, 0);
613 if (!image->swap_page) {
Masanari Iidad5393952014-12-12 16:57:41 -0800614 pr_err("Could not allocate swap buffer\n");
Vivek Goyaldd5f7262014-08-08 14:26:09 -0700615 goto out_free_control_pages;
616 }
Vivek Goyalcb105252014-08-08 14:25:57 -0700617 }
618
619 *rimage = image;
620 return 0;
621out_free_control_pages:
622 kimage_free_page_list(&image->control_pages);
623out_free_post_load_bufs:
624 kimage_file_post_load_cleanup(image);
Vivek Goyalcb105252014-08-08 14:25:57 -0700625out_free_image:
626 kfree(image);
627 return ret;
628}
Vivek Goyal74ca3172014-08-29 15:18:46 -0700629#else /* CONFIG_KEXEC_FILE */
630static inline void kimage_file_post_load_cleanup(struct kimage *image) { }
631#endif /* CONFIG_KEXEC_FILE */
Vivek Goyalcb105252014-08-08 14:25:57 -0700632
Maneesh Soni72414d32005-06-25 14:58:28 -0700633static int kimage_is_destination_range(struct kimage *image,
634 unsigned long start,
635 unsigned long end)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700636{
637 unsigned long i;
638
639 for (i = 0; i < image->nr_segments; i++) {
640 unsigned long mstart, mend;
Maneesh Soni72414d32005-06-25 14:58:28 -0700641
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700642 mstart = image->segment[i].mem;
Maneesh Soni72414d32005-06-25 14:58:28 -0700643 mend = mstart + image->segment[i].memsz;
644 if ((end > mstart) && (start < mend))
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700645 return 1;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700646 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700647
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700648 return 0;
649}
650
Al Viro9796fdd2005-10-21 03:22:03 -0400651static struct page *kimage_alloc_pages(gfp_t gfp_mask, unsigned int order)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700652{
653 struct page *pages;
Maneesh Soni72414d32005-06-25 14:58:28 -0700654
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700655 pages = alloc_pages(gfp_mask, order);
656 if (pages) {
657 unsigned int count, i;
658 pages->mapping = NULL;
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700659 set_page_private(pages, order);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700660 count = 1 << order;
Maneesh Soni72414d32005-06-25 14:58:28 -0700661 for (i = 0; i < count; i++)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700662 SetPageReserved(pages + i);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700663 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700664
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700665 return pages;
666}
667
668static void kimage_free_pages(struct page *page)
669{
670 unsigned int order, count, i;
Maneesh Soni72414d32005-06-25 14:58:28 -0700671
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700672 order = page_private(page);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700673 count = 1 << order;
Maneesh Soni72414d32005-06-25 14:58:28 -0700674 for (i = 0; i < count; i++)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700675 ClearPageReserved(page + i);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700676 __free_pages(page, order);
677}
678
679static void kimage_free_page_list(struct list_head *list)
680{
681 struct list_head *pos, *next;
Maneesh Soni72414d32005-06-25 14:58:28 -0700682
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700683 list_for_each_safe(pos, next, list) {
684 struct page *page;
685
686 page = list_entry(pos, struct page, lru);
687 list_del(&page->lru);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700688 kimage_free_pages(page);
689 }
690}
691
Maneesh Soni72414d32005-06-25 14:58:28 -0700692static struct page *kimage_alloc_normal_control_pages(struct kimage *image,
693 unsigned int order)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700694{
695 /* Control pages are special, they are the intermediaries
696 * that are needed while we copy the rest of the pages
697 * to their final resting place. As such they must
698 * not conflict with either the destination addresses
699 * or memory the kernel is already using.
700 *
701 * The only case where we really need more than one of
702 * these are for architectures where we cannot disable
703 * the MMU and must instead generate an identity mapped
704 * page table for all of the memory.
705 *
706 * At worst this runs in O(N) of the image size.
707 */
708 struct list_head extra_pages;
709 struct page *pages;
710 unsigned int count;
711
712 count = 1 << order;
713 INIT_LIST_HEAD(&extra_pages);
714
715 /* Loop while I can allocate a page and the page allocated
716 * is a destination page.
717 */
718 do {
719 unsigned long pfn, epfn, addr, eaddr;
Maneesh Soni72414d32005-06-25 14:58:28 -0700720
Martin Schwidefsky7e01b5a2015-04-16 14:47:33 +0200721 pages = kimage_alloc_pages(KEXEC_CONTROL_MEMORY_GFP, order);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700722 if (!pages)
723 break;
724 pfn = page_to_pfn(pages);
725 epfn = pfn + count;
726 addr = pfn << PAGE_SHIFT;
727 eaddr = epfn << PAGE_SHIFT;
728 if ((epfn >= (KEXEC_CONTROL_MEMORY_LIMIT >> PAGE_SHIFT)) ||
Maneesh Soni72414d32005-06-25 14:58:28 -0700729 kimage_is_destination_range(image, addr, eaddr)) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700730 list_add(&pages->lru, &extra_pages);
731 pages = NULL;
732 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700733 } while (!pages);
734
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700735 if (pages) {
736 /* Remember the allocated page... */
737 list_add(&pages->lru, &image->control_pages);
738
739 /* Because the page is already in it's destination
740 * location we will never allocate another page at
741 * that address. Therefore kimage_alloc_pages
742 * will not return it (again) and we don't need
743 * to give it an entry in image->segment[].
744 */
745 }
746 /* Deal with the destination pages I have inadvertently allocated.
747 *
748 * Ideally I would convert multi-page allocations into single
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300749 * page allocations, and add everything to image->dest_pages.
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700750 *
751 * For now it is simpler to just free the pages.
752 */
753 kimage_free_page_list(&extra_pages);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700754
Maneesh Soni72414d32005-06-25 14:58:28 -0700755 return pages;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700756}
757
Maneesh Soni72414d32005-06-25 14:58:28 -0700758static struct page *kimage_alloc_crash_control_pages(struct kimage *image,
759 unsigned int order)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700760{
761 /* Control pages are special, they are the intermediaries
762 * that are needed while we copy the rest of the pages
763 * to their final resting place. As such they must
764 * not conflict with either the destination addresses
765 * or memory the kernel is already using.
766 *
767 * Control pages are also the only pags we must allocate
768 * when loading a crash kernel. All of the other pages
769 * are specified by the segments and we just memcpy
770 * into them directly.
771 *
772 * The only case where we really need more than one of
773 * these are for architectures where we cannot disable
774 * the MMU and must instead generate an identity mapped
775 * page table for all of the memory.
776 *
777 * Given the low demand this implements a very simple
778 * allocator that finds the first hole of the appropriate
779 * size in the reserved memory region, and allocates all
780 * of the memory up to and including the hole.
781 */
782 unsigned long hole_start, hole_end, size;
783 struct page *pages;
Maneesh Soni72414d32005-06-25 14:58:28 -0700784
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700785 pages = NULL;
786 size = (1 << order) << PAGE_SHIFT;
787 hole_start = (image->control_page + (size - 1)) & ~(size - 1);
788 hole_end = hole_start + size - 1;
Maneesh Soni72414d32005-06-25 14:58:28 -0700789 while (hole_end <= crashk_res.end) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700790 unsigned long i;
Maneesh Soni72414d32005-06-25 14:58:28 -0700791
Michael Holzheu3d214fa2011-10-30 15:16:36 +0100792 if (hole_end > KEXEC_CRASH_CONTROL_MEMORY_LIMIT)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700793 break;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700794 /* See if I overlap any of the segments */
Maneesh Soni72414d32005-06-25 14:58:28 -0700795 for (i = 0; i < image->nr_segments; i++) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700796 unsigned long mstart, mend;
Maneesh Soni72414d32005-06-25 14:58:28 -0700797
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700798 mstart = image->segment[i].mem;
799 mend = mstart + image->segment[i].memsz - 1;
800 if ((hole_end >= mstart) && (hole_start <= mend)) {
801 /* Advance the hole to the end of the segment */
802 hole_start = (mend + (size - 1)) & ~(size - 1);
803 hole_end = hole_start + size - 1;
804 break;
805 }
806 }
807 /* If I don't overlap any segments I have found my hole! */
808 if (i == image->nr_segments) {
809 pages = pfn_to_page(hole_start >> PAGE_SHIFT);
810 break;
811 }
812 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700813 if (pages)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700814 image->control_page = hole_end;
Maneesh Soni72414d32005-06-25 14:58:28 -0700815
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700816 return pages;
817}
818
819
Maneesh Soni72414d32005-06-25 14:58:28 -0700820struct page *kimage_alloc_control_pages(struct kimage *image,
821 unsigned int order)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700822{
823 struct page *pages = NULL;
Maneesh Soni72414d32005-06-25 14:58:28 -0700824
825 switch (image->type) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700826 case KEXEC_TYPE_DEFAULT:
827 pages = kimage_alloc_normal_control_pages(image, order);
828 break;
829 case KEXEC_TYPE_CRASH:
830 pages = kimage_alloc_crash_control_pages(image, order);
831 break;
832 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700833
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700834 return pages;
835}
836
837static int kimage_add_entry(struct kimage *image, kimage_entry_t entry)
838{
Maneesh Soni72414d32005-06-25 14:58:28 -0700839 if (*image->entry != 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700840 image->entry++;
Maneesh Soni72414d32005-06-25 14:58:28 -0700841
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700842 if (image->entry == image->last_entry) {
843 kimage_entry_t *ind_page;
844 struct page *page;
Maneesh Soni72414d32005-06-25 14:58:28 -0700845
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700846 page = kimage_alloc_page(image, GFP_KERNEL, KIMAGE_NO_DEST);
Maneesh Soni72414d32005-06-25 14:58:28 -0700847 if (!page)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700848 return -ENOMEM;
Maneesh Soni72414d32005-06-25 14:58:28 -0700849
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700850 ind_page = page_address(page);
851 *image->entry = virt_to_phys(ind_page) | IND_INDIRECTION;
852 image->entry = ind_page;
Maneesh Soni72414d32005-06-25 14:58:28 -0700853 image->last_entry = ind_page +
854 ((PAGE_SIZE/sizeof(kimage_entry_t)) - 1);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700855 }
856 *image->entry = entry;
857 image->entry++;
858 *image->entry = 0;
Maneesh Soni72414d32005-06-25 14:58:28 -0700859
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700860 return 0;
861}
862
Maneesh Soni72414d32005-06-25 14:58:28 -0700863static int kimage_set_destination(struct kimage *image,
864 unsigned long destination)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700865{
866 int result;
867
868 destination &= PAGE_MASK;
869 result = kimage_add_entry(image, destination | IND_DESTINATION);
Maneesh Soni72414d32005-06-25 14:58:28 -0700870
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700871 return result;
872}
873
874
875static int kimage_add_page(struct kimage *image, unsigned long page)
876{
877 int result;
878
879 page &= PAGE_MASK;
880 result = kimage_add_entry(image, page | IND_SOURCE);
Maneesh Soni72414d32005-06-25 14:58:28 -0700881
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700882 return result;
883}
884
885
886static void kimage_free_extra_pages(struct kimage *image)
887{
888 /* Walk through and free any extra destination pages I may have */
889 kimage_free_page_list(&image->dest_pages);
890
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300891 /* Walk through and free any unusable pages I have cached */
Vivek Goyal7d3e2bc2014-08-08 14:25:43 -0700892 kimage_free_page_list(&image->unusable_pages);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700893
894}
WANG Cong7fccf032008-07-25 19:45:02 -0700895static void kimage_terminate(struct kimage *image)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700896{
Maneesh Soni72414d32005-06-25 14:58:28 -0700897 if (*image->entry != 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700898 image->entry++;
Maneesh Soni72414d32005-06-25 14:58:28 -0700899
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700900 *image->entry = IND_DONE;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700901}
902
903#define for_each_kimage_entry(image, ptr, entry) \
904 for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE); \
Fabian Fredericke1bebcf2014-06-06 14:37:09 -0700905 ptr = (entry & IND_INDIRECTION) ? \
906 phys_to_virt((entry & PAGE_MASK)) : ptr + 1)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700907
908static void kimage_free_entry(kimage_entry_t entry)
909{
910 struct page *page;
911
912 page = pfn_to_page(entry >> PAGE_SHIFT);
913 kimage_free_pages(page);
914}
915
916static void kimage_free(struct kimage *image)
917{
918 kimage_entry_t *ptr, entry;
919 kimage_entry_t ind = 0;
920
921 if (!image)
922 return;
Maneesh Soni72414d32005-06-25 14:58:28 -0700923
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700924 kimage_free_extra_pages(image);
925 for_each_kimage_entry(image, ptr, entry) {
926 if (entry & IND_INDIRECTION) {
927 /* Free the previous indirection page */
Maneesh Soni72414d32005-06-25 14:58:28 -0700928 if (ind & IND_INDIRECTION)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700929 kimage_free_entry(ind);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700930 /* Save this indirection page until we are
931 * done with it.
932 */
933 ind = entry;
Fabian Fredericke1bebcf2014-06-06 14:37:09 -0700934 } else if (entry & IND_SOURCE)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700935 kimage_free_entry(entry);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700936 }
937 /* Free the final indirection page */
Maneesh Soni72414d32005-06-25 14:58:28 -0700938 if (ind & IND_INDIRECTION)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700939 kimage_free_entry(ind);
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700940
941 /* Handle any machine specific cleanup */
942 machine_kexec_cleanup(image);
943
944 /* Free the kexec control pages... */
945 kimage_free_page_list(&image->control_pages);
Vivek Goyalcb105252014-08-08 14:25:57 -0700946
Vivek Goyalcb105252014-08-08 14:25:57 -0700947 /*
948 * Free up any temporary buffers allocated. This might hit if
949 * error occurred much later after buffer allocation.
950 */
951 if (image->file_mode)
952 kimage_file_post_load_cleanup(image);
953
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700954 kfree(image);
955}
956
Maneesh Soni72414d32005-06-25 14:58:28 -0700957static kimage_entry_t *kimage_dst_used(struct kimage *image,
958 unsigned long page)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700959{
960 kimage_entry_t *ptr, entry;
961 unsigned long destination = 0;
962
963 for_each_kimage_entry(image, ptr, entry) {
Maneesh Soni72414d32005-06-25 14:58:28 -0700964 if (entry & IND_DESTINATION)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700965 destination = entry & PAGE_MASK;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700966 else if (entry & IND_SOURCE) {
Maneesh Soni72414d32005-06-25 14:58:28 -0700967 if (page == destination)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700968 return ptr;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700969 destination += PAGE_SIZE;
970 }
971 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700972
Alexey Dobriyan314b6a42005-06-27 22:29:33 -0700973 return NULL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700974}
975
Maneesh Soni72414d32005-06-25 14:58:28 -0700976static struct page *kimage_alloc_page(struct kimage *image,
Al Viro9796fdd2005-10-21 03:22:03 -0400977 gfp_t gfp_mask,
Maneesh Soni72414d32005-06-25 14:58:28 -0700978 unsigned long destination)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700979{
980 /*
981 * Here we implement safeguards to ensure that a source page
982 * is not copied to its destination page before the data on
983 * the destination page is no longer useful.
984 *
985 * To do this we maintain the invariant that a source page is
986 * either its own destination page, or it is not a
987 * destination page at all.
988 *
989 * That is slightly stronger than required, but the proof
990 * that no problems will not occur is trivial, and the
991 * implementation is simply to verify.
992 *
993 * When allocating all pages normally this algorithm will run
994 * in O(N) time, but in the worst case it will run in O(N^2)
995 * time. If the runtime is a problem the data structures can
996 * be fixed.
997 */
998 struct page *page;
999 unsigned long addr;
1000
1001 /*
1002 * Walk through the list of destination pages, and see if I
1003 * have a match.
1004 */
1005 list_for_each_entry(page, &image->dest_pages, lru) {
1006 addr = page_to_pfn(page) << PAGE_SHIFT;
1007 if (addr == destination) {
1008 list_del(&page->lru);
1009 return page;
1010 }
1011 }
1012 page = NULL;
1013 while (1) {
1014 kimage_entry_t *old;
1015
1016 /* Allocate a page, if we run out of memory give up */
1017 page = kimage_alloc_pages(gfp_mask, 0);
Maneesh Soni72414d32005-06-25 14:58:28 -07001018 if (!page)
Alexey Dobriyan314b6a42005-06-27 22:29:33 -07001019 return NULL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001020 /* If the page cannot be used file it away */
Maneesh Soni72414d32005-06-25 14:58:28 -07001021 if (page_to_pfn(page) >
1022 (KEXEC_SOURCE_MEMORY_LIMIT >> PAGE_SHIFT)) {
Vivek Goyal7d3e2bc2014-08-08 14:25:43 -07001023 list_add(&page->lru, &image->unusable_pages);
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001024 continue;
1025 }
1026 addr = page_to_pfn(page) << PAGE_SHIFT;
1027
1028 /* If it is the destination page we want use it */
1029 if (addr == destination)
1030 break;
1031
1032 /* If the page is not a destination page use it */
Maneesh Soni72414d32005-06-25 14:58:28 -07001033 if (!kimage_is_destination_range(image, addr,
1034 addr + PAGE_SIZE))
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001035 break;
1036
1037 /*
1038 * I know that the page is someones destination page.
1039 * See if there is already a source page for this
1040 * destination page. And if so swap the source pages.
1041 */
1042 old = kimage_dst_used(image, addr);
1043 if (old) {
1044 /* If so move it */
1045 unsigned long old_addr;
1046 struct page *old_page;
1047
1048 old_addr = *old & PAGE_MASK;
1049 old_page = pfn_to_page(old_addr >> PAGE_SHIFT);
1050 copy_highpage(page, old_page);
1051 *old = addr | (*old & ~PAGE_MASK);
1052
1053 /* The old page I have found cannot be a
Jonathan Steelf9092f32008-09-22 13:57:45 -07001054 * destination page, so return it if it's
1055 * gfp_flags honor the ones passed in.
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001056 */
Jonathan Steelf9092f32008-09-22 13:57:45 -07001057 if (!(gfp_mask & __GFP_HIGHMEM) &&
1058 PageHighMem(old_page)) {
1059 kimage_free_pages(old_page);
1060 continue;
1061 }
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001062 addr = old_addr;
1063 page = old_page;
1064 break;
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001065 } else {
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001066 /* Place the page on the destination list I
1067 * will use it later.
1068 */
1069 list_add(&page->lru, &image->dest_pages);
1070 }
1071 }
Maneesh Soni72414d32005-06-25 14:58:28 -07001072
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001073 return page;
1074}
1075
1076static int kimage_load_normal_segment(struct kimage *image,
Maneesh Soni72414d32005-06-25 14:58:28 -07001077 struct kexec_segment *segment)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001078{
1079 unsigned long maddr;
Zhang Yanfei310faaa2013-04-30 15:28:21 -07001080 size_t ubytes, mbytes;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001081 int result;
Vivek Goyalcb105252014-08-08 14:25:57 -07001082 unsigned char __user *buf = NULL;
1083 unsigned char *kbuf = NULL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001084
1085 result = 0;
Vivek Goyalcb105252014-08-08 14:25:57 -07001086 if (image->file_mode)
1087 kbuf = segment->kbuf;
1088 else
1089 buf = segment->buf;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001090 ubytes = segment->bufsz;
1091 mbytes = segment->memsz;
1092 maddr = segment->mem;
1093
1094 result = kimage_set_destination(image, maddr);
Maneesh Soni72414d32005-06-25 14:58:28 -07001095 if (result < 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001096 goto out;
Maneesh Soni72414d32005-06-25 14:58:28 -07001097
1098 while (mbytes) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001099 struct page *page;
1100 char *ptr;
1101 size_t uchunk, mchunk;
Maneesh Soni72414d32005-06-25 14:58:28 -07001102
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001103 page = kimage_alloc_page(image, GFP_HIGHUSER, maddr);
Stephen Hemmingerc80544d2007-10-18 03:07:05 -07001104 if (!page) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001105 result = -ENOMEM;
1106 goto out;
1107 }
Maneesh Soni72414d32005-06-25 14:58:28 -07001108 result = kimage_add_page(image, page_to_pfn(page)
1109 << PAGE_SHIFT);
1110 if (result < 0)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001111 goto out;
Maneesh Soni72414d32005-06-25 14:58:28 -07001112
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001113 ptr = kmap(page);
1114 /* Start with a clear page */
Jan Beulich3ecb01d2010-10-26 14:22:27 -07001115 clear_page(ptr);
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001116 ptr += maddr & ~PAGE_MASK;
Zhang Yanfei31c3a3f2013-04-30 15:28:23 -07001117 mchunk = min_t(size_t, mbytes,
1118 PAGE_SIZE - (maddr & ~PAGE_MASK));
1119 uchunk = min(ubytes, mchunk);
Maneesh Soni72414d32005-06-25 14:58:28 -07001120
Vivek Goyalcb105252014-08-08 14:25:57 -07001121 /* For file based kexec, source pages are in kernel memory */
1122 if (image->file_mode)
1123 memcpy(ptr, kbuf, uchunk);
1124 else
1125 result = copy_from_user(ptr, buf, uchunk);
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001126 kunmap(page);
1127 if (result) {
Dan Carpenterf65a03f2010-08-10 18:03:31 -07001128 result = -EFAULT;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001129 goto out;
1130 }
1131 ubytes -= uchunk;
1132 maddr += mchunk;
Vivek Goyalcb105252014-08-08 14:25:57 -07001133 if (image->file_mode)
1134 kbuf += mchunk;
1135 else
1136 buf += mchunk;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001137 mbytes -= mchunk;
1138 }
Maneesh Soni72414d32005-06-25 14:58:28 -07001139out:
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001140 return result;
1141}
1142
1143static int kimage_load_crash_segment(struct kimage *image,
Maneesh Soni72414d32005-06-25 14:58:28 -07001144 struct kexec_segment *segment)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001145{
1146 /* For crash dumps kernels we simply copy the data from
1147 * user space to it's destination.
1148 * We do things a page at a time for the sake of kmap.
1149 */
1150 unsigned long maddr;
Zhang Yanfei310faaa2013-04-30 15:28:21 -07001151 size_t ubytes, mbytes;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001152 int result;
Vivek Goyaldd5f7262014-08-08 14:26:09 -07001153 unsigned char __user *buf = NULL;
1154 unsigned char *kbuf = NULL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001155
1156 result = 0;
Vivek Goyaldd5f7262014-08-08 14:26:09 -07001157 if (image->file_mode)
1158 kbuf = segment->kbuf;
1159 else
1160 buf = segment->buf;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001161 ubytes = segment->bufsz;
1162 mbytes = segment->memsz;
1163 maddr = segment->mem;
Maneesh Soni72414d32005-06-25 14:58:28 -07001164 while (mbytes) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001165 struct page *page;
1166 char *ptr;
1167 size_t uchunk, mchunk;
Maneesh Soni72414d32005-06-25 14:58:28 -07001168
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001169 page = pfn_to_page(maddr >> PAGE_SHIFT);
Stephen Hemmingerc80544d2007-10-18 03:07:05 -07001170 if (!page) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001171 result = -ENOMEM;
1172 goto out;
1173 }
1174 ptr = kmap(page);
1175 ptr += maddr & ~PAGE_MASK;
Zhang Yanfei31c3a3f2013-04-30 15:28:23 -07001176 mchunk = min_t(size_t, mbytes,
1177 PAGE_SIZE - (maddr & ~PAGE_MASK));
1178 uchunk = min(ubytes, mchunk);
1179 if (mchunk > uchunk) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001180 /* Zero the trailing part of the page */
1181 memset(ptr + uchunk, 0, mchunk - uchunk);
1182 }
Vivek Goyaldd5f7262014-08-08 14:26:09 -07001183
1184 /* For file based kexec, source pages are in kernel memory */
1185 if (image->file_mode)
1186 memcpy(ptr, kbuf, uchunk);
1187 else
1188 result = copy_from_user(ptr, buf, uchunk);
Zou Nan haia79561132006-12-07 09:51:35 -08001189 kexec_flush_icache_page(page);
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001190 kunmap(page);
1191 if (result) {
Dan Carpenterf65a03f2010-08-10 18:03:31 -07001192 result = -EFAULT;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001193 goto out;
1194 }
1195 ubytes -= uchunk;
1196 maddr += mchunk;
Vivek Goyaldd5f7262014-08-08 14:26:09 -07001197 if (image->file_mode)
1198 kbuf += mchunk;
1199 else
1200 buf += mchunk;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001201 mbytes -= mchunk;
1202 }
Maneesh Soni72414d32005-06-25 14:58:28 -07001203out:
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001204 return result;
1205}
1206
1207static int kimage_load_segment(struct kimage *image,
Maneesh Soni72414d32005-06-25 14:58:28 -07001208 struct kexec_segment *segment)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001209{
1210 int result = -ENOMEM;
Maneesh Soni72414d32005-06-25 14:58:28 -07001211
1212 switch (image->type) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001213 case KEXEC_TYPE_DEFAULT:
1214 result = kimage_load_normal_segment(image, segment);
1215 break;
1216 case KEXEC_TYPE_CRASH:
1217 result = kimage_load_crash_segment(image, segment);
1218 break;
1219 }
Maneesh Soni72414d32005-06-25 14:58:28 -07001220
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001221 return result;
1222}
1223
1224/*
1225 * Exec Kernel system call: for obvious reasons only root may call it.
1226 *
1227 * This call breaks up into three pieces.
1228 * - A generic part which loads the new kernel from the current
1229 * address space, and very carefully places the data in the
1230 * allocated pages.
1231 *
1232 * - A generic part that interacts with the kernel and tells all of
1233 * the devices to shut down. Preventing on-going dmas, and placing
1234 * the devices in a consistent state so a later kernel can
1235 * reinitialize them.
1236 *
1237 * - A machine specific part that includes the syscall number
Geert Uytterhoeven002ace72013-09-15 11:35:37 +02001238 * and then copies the image to it's final destination. And
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001239 * jumps into the image at entry.
1240 *
1241 * kexec does not sync, or unmount filesystems so if you need
1242 * that to happen you need to do that yourself.
1243 */
Jeff Moyerc330dda2006-06-23 02:05:07 -07001244struct kimage *kexec_image;
1245struct kimage *kexec_crash_image;
Kees Cook79847542014-01-23 15:55:59 -08001246int kexec_load_disabled;
Andrew Morton8c5a1cf2008-08-15 00:40:27 -07001247
1248static DEFINE_MUTEX(kexec_mutex);
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001249
Heiko Carstens754fe8d2009-01-14 14:14:09 +01001250SYSCALL_DEFINE4(kexec_load, unsigned long, entry, unsigned long, nr_segments,
1251 struct kexec_segment __user *, segments, unsigned long, flags)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001252{
1253 struct kimage **dest_image, *image;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001254 int result;
1255
1256 /* We only trust the superuser with rebooting the system. */
Kees Cook79847542014-01-23 15:55:59 -08001257 if (!capable(CAP_SYS_BOOT) || kexec_load_disabled)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001258 return -EPERM;
1259
1260 /*
1261 * Verify we have a legal set of flags
1262 * This leaves us room for future extensions.
1263 */
1264 if ((flags & KEXEC_FLAGS) != (flags & ~KEXEC_ARCH_MASK))
1265 return -EINVAL;
1266
1267 /* Verify we are on the appropriate architecture */
1268 if (((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH) &&
1269 ((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH_DEFAULT))
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001270 return -EINVAL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001271
1272 /* Put an artificial cap on the number
1273 * of segments passed to kexec_load.
1274 */
1275 if (nr_segments > KEXEC_SEGMENT_MAX)
1276 return -EINVAL;
1277
1278 image = NULL;
1279 result = 0;
1280
1281 /* Because we write directly to the reserved memory
1282 * region when loading crash kernels we need a mutex here to
1283 * prevent multiple crash kernels from attempting to load
1284 * simultaneously, and to prevent a crash kernel from loading
1285 * over the top of a in use crash kernel.
1286 *
1287 * KISS: always take the mutex.
1288 */
Andrew Morton8c5a1cf2008-08-15 00:40:27 -07001289 if (!mutex_trylock(&kexec_mutex))
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001290 return -EBUSY;
Maneesh Soni72414d32005-06-25 14:58:28 -07001291
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001292 dest_image = &kexec_image;
Maneesh Soni72414d32005-06-25 14:58:28 -07001293 if (flags & KEXEC_ON_CRASH)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001294 dest_image = &kexec_crash_image;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001295 if (nr_segments > 0) {
1296 unsigned long i;
Maneesh Soni72414d32005-06-25 14:58:28 -07001297
Geoff Levand518a0c72015-02-17 13:45:53 -08001298 if (flags & KEXEC_ON_CRASH) {
1299 /*
1300 * Loading another kernel to switch to if this one
1301 * crashes. Free any current crash dump kernel before
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001302 * we corrupt it.
1303 */
Geoff Levand518a0c72015-02-17 13:45:53 -08001304
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001305 kimage_free(xchg(&kexec_crash_image, NULL));
Vivek Goyal255aedd2014-08-08 14:25:48 -07001306 result = kimage_alloc_init(&image, entry, nr_segments,
1307 segments, flags);
Michael Holzheu558df722011-10-30 15:16:43 +01001308 crash_map_reserved_pages();
Geoff Levand518a0c72015-02-17 13:45:53 -08001309 } else {
1310 /* Loading another kernel to reboot into. */
1311
1312 result = kimage_alloc_init(&image, entry, nr_segments,
1313 segments, flags);
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001314 }
Maneesh Soni72414d32005-06-25 14:58:28 -07001315 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001316 goto out;
Maneesh Soni72414d32005-06-25 14:58:28 -07001317
Huang Ying3ab83522008-07-25 19:45:07 -07001318 if (flags & KEXEC_PRESERVE_CONTEXT)
1319 image->preserve_context = 1;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001320 result = machine_kexec_prepare(image);
Maneesh Soni72414d32005-06-25 14:58:28 -07001321 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001322 goto out;
Maneesh Soni72414d32005-06-25 14:58:28 -07001323
1324 for (i = 0; i < nr_segments; i++) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001325 result = kimage_load_segment(image, &image->segment[i]);
Maneesh Soni72414d32005-06-25 14:58:28 -07001326 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001327 goto out;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001328 }
WANG Cong7fccf032008-07-25 19:45:02 -07001329 kimage_terminate(image);
Michael Holzheu558df722011-10-30 15:16:43 +01001330 if (flags & KEXEC_ON_CRASH)
1331 crash_unmap_reserved_pages();
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001332 }
1333 /* Install the new kernel, and Uninstall the old */
1334 image = xchg(dest_image, image);
1335
Maneesh Soni72414d32005-06-25 14:58:28 -07001336out:
Andrew Morton8c5a1cf2008-08-15 00:40:27 -07001337 mutex_unlock(&kexec_mutex);
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001338 kimage_free(image);
Maneesh Soni72414d32005-06-25 14:58:28 -07001339
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001340 return result;
1341}
1342
Michael Holzheu558df722011-10-30 15:16:43 +01001343/*
1344 * Add and remove page tables for crashkernel memory
1345 *
1346 * Provide an empty default implementation here -- architecture
1347 * code may override this
1348 */
1349void __weak crash_map_reserved_pages(void)
1350{}
1351
1352void __weak crash_unmap_reserved_pages(void)
1353{}
1354
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001355#ifdef CONFIG_COMPAT
Heiko Carstensca2c4052014-03-04 17:13:42 +01001356COMPAT_SYSCALL_DEFINE4(kexec_load, compat_ulong_t, entry,
1357 compat_ulong_t, nr_segments,
1358 struct compat_kexec_segment __user *, segments,
1359 compat_ulong_t, flags)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001360{
1361 struct compat_kexec_segment in;
1362 struct kexec_segment out, __user *ksegments;
1363 unsigned long i, result;
1364
1365 /* Don't allow clients that don't understand the native
1366 * architecture to do anything.
1367 */
Maneesh Soni72414d32005-06-25 14:58:28 -07001368 if ((flags & KEXEC_ARCH_MASK) == KEXEC_ARCH_DEFAULT)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001369 return -EINVAL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001370
Maneesh Soni72414d32005-06-25 14:58:28 -07001371 if (nr_segments > KEXEC_SEGMENT_MAX)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001372 return -EINVAL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001373
1374 ksegments = compat_alloc_user_space(nr_segments * sizeof(out));
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001375 for (i = 0; i < nr_segments; i++) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001376 result = copy_from_user(&in, &segments[i], sizeof(in));
Maneesh Soni72414d32005-06-25 14:58:28 -07001377 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001378 return -EFAULT;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001379
1380 out.buf = compat_ptr(in.buf);
1381 out.bufsz = in.bufsz;
1382 out.mem = in.mem;
1383 out.memsz = in.memsz;
1384
1385 result = copy_to_user(&ksegments[i], &out, sizeof(out));
Maneesh Soni72414d32005-06-25 14:58:28 -07001386 if (result)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001387 return -EFAULT;
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001388 }
1389
1390 return sys_kexec_load(entry, nr_segments, ksegments, flags);
1391}
1392#endif
1393
Vivek Goyal74ca3172014-08-29 15:18:46 -07001394#ifdef CONFIG_KEXEC_FILE
Vivek Goyalf0895682014-08-08 14:25:55 -07001395SYSCALL_DEFINE5(kexec_file_load, int, kernel_fd, int, initrd_fd,
1396 unsigned long, cmdline_len, const char __user *, cmdline_ptr,
1397 unsigned long, flags)
1398{
Vivek Goyalcb105252014-08-08 14:25:57 -07001399 int ret = 0, i;
1400 struct kimage **dest_image, *image;
1401
1402 /* We only trust the superuser with rebooting the system. */
1403 if (!capable(CAP_SYS_BOOT) || kexec_load_disabled)
1404 return -EPERM;
1405
1406 /* Make sure we have a legal set of flags */
1407 if (flags != (flags & KEXEC_FILE_FLAGS))
1408 return -EINVAL;
1409
1410 image = NULL;
1411
1412 if (!mutex_trylock(&kexec_mutex))
1413 return -EBUSY;
1414
1415 dest_image = &kexec_image;
1416 if (flags & KEXEC_FILE_ON_CRASH)
1417 dest_image = &kexec_crash_image;
1418
1419 if (flags & KEXEC_FILE_UNLOAD)
1420 goto exchange;
1421
1422 /*
1423 * In case of crash, new kernel gets loaded in reserved region. It is
1424 * same memory where old crash kernel might be loaded. Free any
1425 * current crash dump kernel before we corrupt it.
1426 */
1427 if (flags & KEXEC_FILE_ON_CRASH)
1428 kimage_free(xchg(&kexec_crash_image, NULL));
1429
1430 ret = kimage_file_alloc_init(&image, kernel_fd, initrd_fd, cmdline_ptr,
1431 cmdline_len, flags);
1432 if (ret)
1433 goto out;
1434
1435 ret = machine_kexec_prepare(image);
1436 if (ret)
1437 goto out;
1438
Vivek Goyal12db5562014-08-08 14:26:04 -07001439 ret = kexec_calculate_store_digests(image);
1440 if (ret)
1441 goto out;
1442
Vivek Goyalcb105252014-08-08 14:25:57 -07001443 for (i = 0; i < image->nr_segments; i++) {
1444 struct kexec_segment *ksegment;
1445
1446 ksegment = &image->segment[i];
1447 pr_debug("Loading segment %d: buf=0x%p bufsz=0x%zx mem=0x%lx memsz=0x%zx\n",
1448 i, ksegment->buf, ksegment->bufsz, ksegment->mem,
1449 ksegment->memsz);
1450
1451 ret = kimage_load_segment(image, &image->segment[i]);
1452 if (ret)
1453 goto out;
1454 }
1455
1456 kimage_terminate(image);
1457
1458 /*
1459 * Free up any temporary buffers allocated which are not needed
1460 * after image has been loaded
1461 */
1462 kimage_file_post_load_cleanup(image);
1463exchange:
1464 image = xchg(dest_image, image);
1465out:
1466 mutex_unlock(&kexec_mutex);
1467 kimage_free(image);
1468 return ret;
Vivek Goyalf0895682014-08-08 14:25:55 -07001469}
1470
Vivek Goyal74ca3172014-08-29 15:18:46 -07001471#endif /* CONFIG_KEXEC_FILE */
1472
Alexander Nyberg6e274d12005-06-25 14:58:26 -07001473void crash_kexec(struct pt_regs *regs)
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001474{
Andrew Morton8c5a1cf2008-08-15 00:40:27 -07001475 /* Take the kexec_mutex here to prevent sys_kexec_load
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001476 * running on one cpu from replacing the crash kernel
1477 * we are using after a panic on a different cpu.
1478 *
1479 * If the crash kernel was not located in a fixed area
1480 * of memory the xchg(&kexec_crash_image) would be
1481 * sufficient. But since I reuse the memory...
1482 */
Andrew Morton8c5a1cf2008-08-15 00:40:27 -07001483 if (mutex_trylock(&kexec_mutex)) {
David Wilderc0ce7d02006-06-23 15:29:34 -07001484 if (kexec_crash_image) {
Vivek Goyale996e582006-01-09 20:51:44 -08001485 struct pt_regs fixed_regs;
KOSAKI Motohiro0f4bd462009-12-22 03:15:43 +00001486
Vivek Goyale996e582006-01-09 20:51:44 -08001487 crash_setup_regs(&fixed_regs, regs);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001488 crash_save_vmcoreinfo();
Vivek Goyale996e582006-01-09 20:51:44 -08001489 machine_crash_shutdown(&fixed_regs);
David Wilderc0ce7d02006-06-23 15:29:34 -07001490 machine_kexec(kexec_crash_image);
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001491 }
Andrew Morton8c5a1cf2008-08-15 00:40:27 -07001492 mutex_unlock(&kexec_mutex);
Eric W. Biedermandc009d92005-06-25 14:57:52 -07001493 }
1494}
Vivek Goyalcc571652006-01-09 20:51:41 -08001495
Amerigo Wang06a7f712009-12-15 16:47:46 -08001496size_t crash_get_memory_size(void)
1497{
Pavan Naregundie05bd332010-06-29 15:05:28 -07001498 size_t size = 0;
Amerigo Wang06a7f712009-12-15 16:47:46 -08001499 mutex_lock(&kexec_mutex);
Pavan Naregundie05bd332010-06-29 15:05:28 -07001500 if (crashk_res.end != crashk_res.start)
Joe Perches28f65c112011-06-09 09:13:32 -07001501 size = resource_size(&crashk_res);
Amerigo Wang06a7f712009-12-15 16:47:46 -08001502 mutex_unlock(&kexec_mutex);
1503 return size;
1504}
1505
Anton Blanchardc0bb9e42010-08-25 10:22:58 +10001506void __weak crash_free_reserved_phys_range(unsigned long begin,
1507 unsigned long end)
Amerigo Wang06a7f712009-12-15 16:47:46 -08001508{
1509 unsigned long addr;
1510
Jiang Liue07cee22013-04-29 15:06:58 -07001511 for (addr = begin; addr < end; addr += PAGE_SIZE)
1512 free_reserved_page(pfn_to_page(addr >> PAGE_SHIFT));
Amerigo Wang06a7f712009-12-15 16:47:46 -08001513}
1514
1515int crash_shrink_memory(unsigned long new_size)
1516{
1517 int ret = 0;
1518 unsigned long start, end;
Michael Holzheubec013c2012-01-12 17:20:15 -08001519 unsigned long old_size;
Michael Holzheu6480e5a2012-01-12 17:20:14 -08001520 struct resource *ram_res;
Amerigo Wang06a7f712009-12-15 16:47:46 -08001521
1522 mutex_lock(&kexec_mutex);
1523
1524 if (kexec_crash_image) {
1525 ret = -ENOENT;
1526 goto unlock;
1527 }
1528 start = crashk_res.start;
1529 end = crashk_res.end;
Michael Holzheubec013c2012-01-12 17:20:15 -08001530 old_size = (end == 0) ? 0 : end - start + 1;
1531 if (new_size >= old_size) {
1532 ret = (new_size == old_size) ? 0 : -EINVAL;
Amerigo Wang06a7f712009-12-15 16:47:46 -08001533 goto unlock;
1534 }
1535
Michael Holzheu6480e5a2012-01-12 17:20:14 -08001536 ram_res = kzalloc(sizeof(*ram_res), GFP_KERNEL);
1537 if (!ram_res) {
1538 ret = -ENOMEM;
1539 goto unlock;
1540 }
1541
Michael Holzheu558df722011-10-30 15:16:43 +01001542 start = roundup(start, KEXEC_CRASH_MEM_ALIGN);
1543 end = roundup(start + new_size, KEXEC_CRASH_MEM_ALIGN);
Amerigo Wang06a7f712009-12-15 16:47:46 -08001544
Michael Holzheu558df722011-10-30 15:16:43 +01001545 crash_map_reserved_pages();
Anton Blanchardc0bb9e42010-08-25 10:22:58 +10001546 crash_free_reserved_phys_range(end, crashk_res.end);
Amerigo Wang06a7f712009-12-15 16:47:46 -08001547
Pavan Naregundie05bd332010-06-29 15:05:28 -07001548 if ((start == end) && (crashk_res.parent != NULL))
Amerigo Wang06a7f712009-12-15 16:47:46 -08001549 release_resource(&crashk_res);
Michael Holzheu6480e5a2012-01-12 17:20:14 -08001550
1551 ram_res->start = end;
1552 ram_res->end = crashk_res.end;
1553 ram_res->flags = IORESOURCE_BUSY | IORESOURCE_MEM;
1554 ram_res->name = "System RAM";
1555
Vitaly Mayatskikh475f9aa2010-05-11 14:06:51 -07001556 crashk_res.end = end - 1;
Michael Holzheu6480e5a2012-01-12 17:20:14 -08001557
1558 insert_resource(&iomem_resource, ram_res);
Michael Holzheu558df722011-10-30 15:16:43 +01001559 crash_unmap_reserved_pages();
Amerigo Wang06a7f712009-12-15 16:47:46 -08001560
1561unlock:
1562 mutex_unlock(&kexec_mutex);
1563 return ret;
1564}
1565
Magnus Damm85916f82006-12-06 20:40:41 -08001566static u32 *append_elf_note(u32 *buf, char *name, unsigned type, void *data,
1567 size_t data_len)
1568{
1569 struct elf_note note;
1570
1571 note.n_namesz = strlen(name) + 1;
1572 note.n_descsz = data_len;
1573 note.n_type = type;
1574 memcpy(buf, &note, sizeof(note));
1575 buf += (sizeof(note) + 3)/4;
1576 memcpy(buf, name, note.n_namesz);
1577 buf += (note.n_namesz + 3)/4;
1578 memcpy(buf, data, note.n_descsz);
1579 buf += (note.n_descsz + 3)/4;
1580
1581 return buf;
1582}
1583
1584static void final_note(u32 *buf)
1585{
1586 struct elf_note note;
1587
1588 note.n_namesz = 0;
1589 note.n_descsz = 0;
1590 note.n_type = 0;
1591 memcpy(buf, &note, sizeof(note));
1592}
1593
1594void crash_save_cpu(struct pt_regs *regs, int cpu)
1595{
1596 struct elf_prstatus prstatus;
1597 u32 *buf;
1598
Rusty Russell4f4b6c12009-01-01 10:12:15 +10301599 if ((cpu < 0) || (cpu >= nr_cpu_ids))
Magnus Damm85916f82006-12-06 20:40:41 -08001600 return;
1601
1602 /* Using ELF notes here is opportunistic.
1603 * I need a well defined structure format
1604 * for the data I pass, and I need tags
1605 * on the data to indicate what information I have
1606 * squirrelled away. ELF notes happen to provide
1607 * all of that, so there is no need to invent something new.
1608 */
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001609 buf = (u32 *)per_cpu_ptr(crash_notes, cpu);
Magnus Damm85916f82006-12-06 20:40:41 -08001610 if (!buf)
1611 return;
1612 memset(&prstatus, 0, sizeof(prstatus));
1613 prstatus.pr_pid = current->pid;
Tejun Heo6cd61c02009-02-09 22:17:39 +09001614 elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
Simon Horman6672f762007-05-08 00:28:22 -07001615 buf = append_elf_note(buf, KEXEC_CORE_NOTE_NAME, NT_PRSTATUS,
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001616 &prstatus, sizeof(prstatus));
Magnus Damm85916f82006-12-06 20:40:41 -08001617 final_note(buf);
1618}
1619
Vivek Goyalcc571652006-01-09 20:51:41 -08001620static int __init crash_notes_memory_init(void)
1621{
1622 /* Allocate memory for saving cpu registers. */
1623 crash_notes = alloc_percpu(note_buf_t);
1624 if (!crash_notes) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001625 pr_warn("Kexec: Memory allocation for saving cpu register states failed\n");
Vivek Goyalcc571652006-01-09 20:51:41 -08001626 return -ENOMEM;
1627 }
1628 return 0;
1629}
Paul Gortmakerc96d6662014-04-03 14:48:35 -07001630subsys_initcall(crash_notes_memory_init);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001631
Bernhard Wallecba63c32007-10-18 23:40:58 -07001632
1633/*
1634 * parsing the "crashkernel" commandline
1635 *
1636 * this code is intended to be called from architecture specific code
1637 */
1638
1639
1640/*
1641 * This function parses command lines in the format
1642 *
1643 * crashkernel=ramsize-range:size[,...][@offset]
1644 *
1645 * The function returns 0 on success and -EINVAL on failure.
1646 */
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001647static int __init parse_crashkernel_mem(char *cmdline,
1648 unsigned long long system_ram,
1649 unsigned long long *crash_size,
1650 unsigned long long *crash_base)
Bernhard Wallecba63c32007-10-18 23:40:58 -07001651{
1652 char *cur = cmdline, *tmp;
1653
1654 /* for each entry of the comma-separated list */
1655 do {
1656 unsigned long long start, end = ULLONG_MAX, size;
1657
1658 /* get the start of the range */
1659 start = 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 (*cur != '-') {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001666 pr_warn("crashkernel: '-' expected\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001667 return -EINVAL;
1668 }
1669 cur++;
1670
1671 /* if no ':' is here, than we read the end */
1672 if (*cur != ':') {
1673 end = memparse(cur, &tmp);
1674 if (cur == tmp) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001675 pr_warn("crashkernel: Memory value expected\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001676 return -EINVAL;
1677 }
1678 cur = tmp;
1679 if (end <= start) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001680 pr_warn("crashkernel: end <= start\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001681 return -EINVAL;
1682 }
1683 }
1684
1685 if (*cur != ':') {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001686 pr_warn("crashkernel: ':' expected\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001687 return -EINVAL;
1688 }
1689 cur++;
1690
1691 size = memparse(cur, &tmp);
1692 if (cur == tmp) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001693 pr_warn("Memory value expected\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001694 return -EINVAL;
1695 }
1696 cur = tmp;
1697 if (size >= system_ram) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001698 pr_warn("crashkernel: invalid size\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001699 return -EINVAL;
1700 }
1701
1702 /* match ? */
Michael Ellermanbe089d792008-05-01 04:34:49 -07001703 if (system_ram >= start && system_ram < end) {
Bernhard Wallecba63c32007-10-18 23:40:58 -07001704 *crash_size = size;
1705 break;
1706 }
1707 } while (*cur++ == ',');
1708
1709 if (*crash_size > 0) {
Hidetoshi Seto11c7da42009-07-29 15:02:08 -07001710 while (*cur && *cur != ' ' && *cur != '@')
Bernhard Wallecba63c32007-10-18 23:40:58 -07001711 cur++;
1712 if (*cur == '@') {
1713 cur++;
1714 *crash_base = memparse(cur, &tmp);
1715 if (cur == tmp) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001716 pr_warn("Memory value expected after '@'\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001717 return -EINVAL;
1718 }
1719 }
1720 }
1721
1722 return 0;
1723}
1724
1725/*
1726 * That function parses "simple" (old) crashkernel command lines like
1727 *
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001728 * crashkernel=size[@offset]
Bernhard Wallecba63c32007-10-18 23:40:58 -07001729 *
1730 * It returns 0 on success and -EINVAL on failure.
1731 */
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001732static int __init parse_crashkernel_simple(char *cmdline,
1733 unsigned long long *crash_size,
1734 unsigned long long *crash_base)
Bernhard Wallecba63c32007-10-18 23:40:58 -07001735{
1736 char *cur = cmdline;
1737
1738 *crash_size = memparse(cmdline, &cur);
1739 if (cmdline == cur) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001740 pr_warn("crashkernel: memory value expected\n");
Bernhard Wallecba63c32007-10-18 23:40:58 -07001741 return -EINVAL;
1742 }
1743
1744 if (*cur == '@')
1745 *crash_base = memparse(cur+1, &cur);
Zhenzhong Duaneaa3be62012-03-28 14:42:47 -07001746 else if (*cur != ' ' && *cur != '\0') {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07001747 pr_warn("crashkernel: unrecognized char\n");
Zhenzhong Duaneaa3be62012-03-28 14:42:47 -07001748 return -EINVAL;
1749 }
Bernhard Wallecba63c32007-10-18 23:40:58 -07001750
1751 return 0;
1752}
1753
Yinghai Luadbc7422013-04-15 22:23:48 -07001754#define SUFFIX_HIGH 0
1755#define SUFFIX_LOW 1
1756#define SUFFIX_NULL 2
1757static __initdata char *suffix_tbl[] = {
1758 [SUFFIX_HIGH] = ",high",
1759 [SUFFIX_LOW] = ",low",
1760 [SUFFIX_NULL] = NULL,
1761};
1762
Bernhard Wallecba63c32007-10-18 23:40:58 -07001763/*
Yinghai Luadbc7422013-04-15 22:23:48 -07001764 * That function parses "suffix" crashkernel command lines like
1765 *
1766 * crashkernel=size,[high|low]
1767 *
1768 * It returns 0 on success and -EINVAL on failure.
Bernhard Wallecba63c32007-10-18 23:40:58 -07001769 */
Yinghai Luadbc7422013-04-15 22:23:48 -07001770static int __init parse_crashkernel_suffix(char *cmdline,
1771 unsigned long long *crash_size,
Yinghai Luadbc7422013-04-15 22:23:48 -07001772 const char *suffix)
1773{
1774 char *cur = cmdline;
1775
1776 *crash_size = memparse(cmdline, &cur);
1777 if (cmdline == cur) {
1778 pr_warn("crashkernel: memory value expected\n");
1779 return -EINVAL;
1780 }
1781
1782 /* check with suffix */
1783 if (strncmp(cur, suffix, strlen(suffix))) {
1784 pr_warn("crashkernel: unrecognized char\n");
1785 return -EINVAL;
1786 }
1787 cur += strlen(suffix);
1788 if (*cur != ' ' && *cur != '\0') {
1789 pr_warn("crashkernel: unrecognized char\n");
1790 return -EINVAL;
1791 }
1792
1793 return 0;
1794}
1795
1796static __init char *get_last_crashkernel(char *cmdline,
1797 const char *name,
1798 const char *suffix)
1799{
1800 char *p = cmdline, *ck_cmdline = NULL;
1801
1802 /* find crashkernel and use the last one if there are more */
1803 p = strstr(p, name);
1804 while (p) {
1805 char *end_p = strchr(p, ' ');
1806 char *q;
1807
1808 if (!end_p)
1809 end_p = p + strlen(p);
1810
1811 if (!suffix) {
1812 int i;
1813
1814 /* skip the one with any known suffix */
1815 for (i = 0; suffix_tbl[i]; i++) {
1816 q = end_p - strlen(suffix_tbl[i]);
1817 if (!strncmp(q, suffix_tbl[i],
1818 strlen(suffix_tbl[i])))
1819 goto next;
1820 }
1821 ck_cmdline = p;
1822 } else {
1823 q = end_p - strlen(suffix);
1824 if (!strncmp(q, suffix, strlen(suffix)))
1825 ck_cmdline = p;
1826 }
1827next:
1828 p = strstr(p+1, name);
1829 }
1830
1831 if (!ck_cmdline)
1832 return NULL;
1833
1834 return ck_cmdline;
1835}
1836
Yinghai Lu0212f912013-01-24 12:20:11 -08001837static int __init __parse_crashkernel(char *cmdline,
Bernhard Wallecba63c32007-10-18 23:40:58 -07001838 unsigned long long system_ram,
1839 unsigned long long *crash_size,
Yinghai Lu0212f912013-01-24 12:20:11 -08001840 unsigned long long *crash_base,
Yinghai Luadbc7422013-04-15 22:23:48 -07001841 const char *name,
1842 const char *suffix)
Bernhard Wallecba63c32007-10-18 23:40:58 -07001843{
Bernhard Wallecba63c32007-10-18 23:40:58 -07001844 char *first_colon, *first_space;
Yinghai Luadbc7422013-04-15 22:23:48 -07001845 char *ck_cmdline;
Bernhard Wallecba63c32007-10-18 23:40:58 -07001846
1847 BUG_ON(!crash_size || !crash_base);
1848 *crash_size = 0;
1849 *crash_base = 0;
1850
Yinghai Luadbc7422013-04-15 22:23:48 -07001851 ck_cmdline = get_last_crashkernel(cmdline, name, suffix);
Bernhard Wallecba63c32007-10-18 23:40:58 -07001852
1853 if (!ck_cmdline)
1854 return -EINVAL;
1855
Yinghai Lu0212f912013-01-24 12:20:11 -08001856 ck_cmdline += strlen(name);
Bernhard Wallecba63c32007-10-18 23:40:58 -07001857
Yinghai Luadbc7422013-04-15 22:23:48 -07001858 if (suffix)
1859 return parse_crashkernel_suffix(ck_cmdline, crash_size,
Baoquan He36f3f502014-10-13 15:53:44 -07001860 suffix);
Bernhard Wallecba63c32007-10-18 23:40:58 -07001861 /*
1862 * if the commandline contains a ':', then that's the extended
1863 * syntax -- if not, it must be the classic syntax
1864 */
1865 first_colon = strchr(ck_cmdline, ':');
1866 first_space = strchr(ck_cmdline, ' ');
1867 if (first_colon && (!first_space || first_colon < first_space))
1868 return parse_crashkernel_mem(ck_cmdline, system_ram,
1869 crash_size, crash_base);
Bernhard Wallecba63c32007-10-18 23:40:58 -07001870
Xishi Qiu80c74f62013-09-11 14:24:47 -07001871 return parse_crashkernel_simple(ck_cmdline, crash_size, crash_base);
Bernhard Wallecba63c32007-10-18 23:40:58 -07001872}
1873
Yinghai Luadbc7422013-04-15 22:23:48 -07001874/*
1875 * That function is the entry point for command line parsing and should be
1876 * called from the arch-specific code.
1877 */
Yinghai Lu0212f912013-01-24 12:20:11 -08001878int __init parse_crashkernel(char *cmdline,
1879 unsigned long long system_ram,
1880 unsigned long long *crash_size,
1881 unsigned long long *crash_base)
1882{
1883 return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
Yinghai Luadbc7422013-04-15 22:23:48 -07001884 "crashkernel=", NULL);
Yinghai Lu0212f912013-01-24 12:20:11 -08001885}
1886
Yinghai Lu55a20ee2013-04-15 22:23:47 -07001887int __init parse_crashkernel_high(char *cmdline,
1888 unsigned long long system_ram,
1889 unsigned long long *crash_size,
1890 unsigned long long *crash_base)
1891{
1892 return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
Yinghai Luadbc7422013-04-15 22:23:48 -07001893 "crashkernel=", suffix_tbl[SUFFIX_HIGH]);
Yinghai Lu55a20ee2013-04-15 22:23:47 -07001894}
1895
Yinghai Lu0212f912013-01-24 12:20:11 -08001896int __init parse_crashkernel_low(char *cmdline,
1897 unsigned long long system_ram,
1898 unsigned long long *crash_size,
1899 unsigned long long *crash_base)
1900{
1901 return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
Yinghai Luadbc7422013-04-15 22:23:48 -07001902 "crashkernel=", suffix_tbl[SUFFIX_LOW]);
Yinghai Lu0212f912013-01-24 12:20:11 -08001903}
Bernhard Wallecba63c32007-10-18 23:40:58 -07001904
Michael Holzheufa8ff292011-10-30 15:16:41 +01001905static void update_vmcoreinfo_note(void)
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001906{
Michael Holzheufa8ff292011-10-30 15:16:41 +01001907 u32 *buf = vmcoreinfo_note;
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001908
1909 if (!vmcoreinfo_size)
1910 return;
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001911 buf = append_elf_note(buf, VMCOREINFO_NOTE_NAME, 0, vmcoreinfo_data,
1912 vmcoreinfo_size);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001913 final_note(buf);
1914}
1915
Michael Holzheufa8ff292011-10-30 15:16:41 +01001916void crash_save_vmcoreinfo(void)
1917{
Vivek Goyal63dca8d2012-07-30 14:42:36 -07001918 vmcoreinfo_append_str("CRASHTIME=%ld\n", get_seconds());
Michael Holzheufa8ff292011-10-30 15:16:41 +01001919 update_vmcoreinfo_note();
1920}
1921
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001922void vmcoreinfo_append_str(const char *fmt, ...)
1923{
1924 va_list args;
1925 char buf[0x50];
Zhang Yanfei310faaa2013-04-30 15:28:21 -07001926 size_t r;
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001927
1928 va_start(args, fmt);
Chen Ganga19428e2014-01-27 17:07:13 -08001929 r = vscnprintf(buf, sizeof(buf), fmt, args);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001930 va_end(args);
1931
Zhang Yanfei31c3a3f2013-04-30 15:28:23 -07001932 r = min(r, vmcoreinfo_max_size - vmcoreinfo_size);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001933
1934 memcpy(&vmcoreinfo_data[vmcoreinfo_size], buf, r);
1935
1936 vmcoreinfo_size += r;
1937}
1938
1939/*
1940 * provide an empty default implementation here -- architecture
1941 * code may override this
1942 */
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -07001943void __weak arch_crash_save_vmcoreinfo(void)
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001944{}
1945
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -07001946unsigned long __weak paddr_vmcoreinfo_note(void)
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001947{
1948 return __pa((unsigned long)(char *)&vmcoreinfo_note);
1949}
1950
1951static int __init crash_save_vmcoreinfo_init(void)
1952{
Ken'ichi Ohmichibba1f602008-02-07 00:15:22 -08001953 VMCOREINFO_OSRELEASE(init_uts_ns.name.release);
1954 VMCOREINFO_PAGESIZE(PAGE_SIZE);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001955
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001956 VMCOREINFO_SYMBOL(init_uts_ns);
1957 VMCOREINFO_SYMBOL(node_online_map);
Will Deacond034cfa2012-03-28 14:42:47 -07001958#ifdef CONFIG_MMU
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001959 VMCOREINFO_SYMBOL(swapper_pg_dir);
Will Deacond034cfa2012-03-28 14:42:47 -07001960#endif
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001961 VMCOREINFO_SYMBOL(_stext);
Joonsoo Kimf1c40692013-04-29 15:07:37 -07001962 VMCOREINFO_SYMBOL(vmap_area_list);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001963
1964#ifndef CONFIG_NEED_MULTIPLE_NODES
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001965 VMCOREINFO_SYMBOL(mem_map);
1966 VMCOREINFO_SYMBOL(contig_page_data);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001967#endif
1968#ifdef CONFIG_SPARSEMEM
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001969 VMCOREINFO_SYMBOL(mem_section);
1970 VMCOREINFO_LENGTH(mem_section, NR_SECTION_ROOTS);
Ken'ichi Ohmichic76f8602008-02-07 00:15:20 -08001971 VMCOREINFO_STRUCT_SIZE(mem_section);
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001972 VMCOREINFO_OFFSET(mem_section, section_mem_map);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001973#endif
Ken'ichi Ohmichic76f8602008-02-07 00:15:20 -08001974 VMCOREINFO_STRUCT_SIZE(page);
1975 VMCOREINFO_STRUCT_SIZE(pglist_data);
1976 VMCOREINFO_STRUCT_SIZE(zone);
1977 VMCOREINFO_STRUCT_SIZE(free_area);
1978 VMCOREINFO_STRUCT_SIZE(list_head);
1979 VMCOREINFO_SIZE(nodemask_t);
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001980 VMCOREINFO_OFFSET(page, flags);
1981 VMCOREINFO_OFFSET(page, _count);
1982 VMCOREINFO_OFFSET(page, mapping);
1983 VMCOREINFO_OFFSET(page, lru);
Atsushi Kumagai8d670912013-02-27 17:03:25 -08001984 VMCOREINFO_OFFSET(page, _mapcount);
1985 VMCOREINFO_OFFSET(page, private);
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001986 VMCOREINFO_OFFSET(pglist_data, node_zones);
1987 VMCOREINFO_OFFSET(pglist_data, nr_zones);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001988#ifdef CONFIG_FLAT_NODE_MEM_MAP
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001989 VMCOREINFO_OFFSET(pglist_data, node_mem_map);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07001990#endif
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07001991 VMCOREINFO_OFFSET(pglist_data, node_start_pfn);
1992 VMCOREINFO_OFFSET(pglist_data, node_spanned_pages);
1993 VMCOREINFO_OFFSET(pglist_data, node_id);
1994 VMCOREINFO_OFFSET(zone, free_area);
1995 VMCOREINFO_OFFSET(zone, vm_stat);
1996 VMCOREINFO_OFFSET(zone, spanned_pages);
1997 VMCOREINFO_OFFSET(free_area, free_list);
1998 VMCOREINFO_OFFSET(list_head, next);
1999 VMCOREINFO_OFFSET(list_head, prev);
Atsushi Kumagai13ba3fc2013-04-29 15:07:40 -07002000 VMCOREINFO_OFFSET(vmap_area, va_start);
2001 VMCOREINFO_OFFSET(vmap_area, list);
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07002002 VMCOREINFO_LENGTH(zone.free_area, MAX_ORDER);
Neil Horman04d491a2009-04-02 16:58:57 -07002003 log_buf_kexec_setup();
Ken'ichi Ohmichi83a08e72008-01-08 15:33:05 -08002004 VMCOREINFO_LENGTH(free_area.free_list, MIGRATE_TYPES);
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -07002005 VMCOREINFO_NUMBER(NR_FREE_PAGES);
Ken'ichi Ohmichi122c7a52008-04-28 02:13:04 -07002006 VMCOREINFO_NUMBER(PG_lru);
2007 VMCOREINFO_NUMBER(PG_private);
2008 VMCOREINFO_NUMBER(PG_swapcache);
Atsushi Kumagai8d670912013-02-27 17:03:25 -08002009 VMCOREINFO_NUMBER(PG_slab);
Mitsuhiro Tanino0d0bf662013-02-27 17:03:27 -08002010#ifdef CONFIG_MEMORY_FAILURE
2011 VMCOREINFO_NUMBER(PG_hwpoison);
2012#endif
Petr Tesarikb3acc562014-06-23 13:22:03 -07002013 VMCOREINFO_NUMBER(PG_head_mask);
Atsushi Kumagai8d670912013-02-27 17:03:25 -08002014 VMCOREINFO_NUMBER(PAGE_BUDDY_MAPCOUNT_VALUE);
David Rientjes3a1122d2014-07-30 19:05:55 -07002015#ifdef CONFIG_HUGETLBFS
Atsushi Kumagai8f1d26d2014-07-30 16:08:39 -07002016 VMCOREINFO_SYMBOL(free_huge_page);
David Rientjes3a1122d2014-07-30 19:05:55 -07002017#endif
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07002018
2019 arch_crash_save_vmcoreinfo();
Michael Holzheufa8ff292011-10-30 15:16:41 +01002020 update_vmcoreinfo_note();
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -07002021
2022 return 0;
2023}
2024
Paul Gortmakerc96d6662014-04-03 14:48:35 -07002025subsys_initcall(crash_save_vmcoreinfo_init);
Huang Ying3ab83522008-07-25 19:45:07 -07002026
Vivek Goyal74ca3172014-08-29 15:18:46 -07002027#ifdef CONFIG_KEXEC_FILE
Vivek Goyalcb105252014-08-08 14:25:57 -07002028static int locate_mem_hole_top_down(unsigned long start, unsigned long end,
2029 struct kexec_buf *kbuf)
2030{
2031 struct kimage *image = kbuf->image;
2032 unsigned long temp_start, temp_end;
2033
2034 temp_end = min(end, kbuf->buf_max);
2035 temp_start = temp_end - kbuf->memsz;
2036
2037 do {
2038 /* align down start */
2039 temp_start = temp_start & (~(kbuf->buf_align - 1));
2040
2041 if (temp_start < start || temp_start < kbuf->buf_min)
2042 return 0;
2043
2044 temp_end = temp_start + kbuf->memsz - 1;
2045
2046 /*
2047 * Make sure this does not conflict with any of existing
2048 * segments
2049 */
2050 if (kimage_is_destination_range(image, temp_start, temp_end)) {
2051 temp_start = temp_start - PAGE_SIZE;
2052 continue;
2053 }
2054
2055 /* We found a suitable memory range */
2056 break;
2057 } while (1);
2058
2059 /* If we are here, we found a suitable memory range */
Baoquan He669280a2014-10-13 15:53:40 -07002060 kbuf->mem = temp_start;
Vivek Goyalcb105252014-08-08 14:25:57 -07002061
2062 /* Success, stop navigating through remaining System RAM ranges */
2063 return 1;
2064}
2065
2066static int locate_mem_hole_bottom_up(unsigned long start, unsigned long end,
2067 struct kexec_buf *kbuf)
2068{
2069 struct kimage *image = kbuf->image;
2070 unsigned long temp_start, temp_end;
2071
2072 temp_start = max(start, kbuf->buf_min);
2073
2074 do {
2075 temp_start = ALIGN(temp_start, kbuf->buf_align);
2076 temp_end = temp_start + kbuf->memsz - 1;
2077
2078 if (temp_end > end || temp_end > kbuf->buf_max)
2079 return 0;
2080 /*
2081 * Make sure this does not conflict with any of existing
2082 * segments
2083 */
2084 if (kimage_is_destination_range(image, temp_start, temp_end)) {
2085 temp_start = temp_start + PAGE_SIZE;
2086 continue;
2087 }
2088
2089 /* We found a suitable memory range */
2090 break;
2091 } while (1);
2092
2093 /* If we are here, we found a suitable memory range */
Baoquan He669280a2014-10-13 15:53:40 -07002094 kbuf->mem = temp_start;
Vivek Goyalcb105252014-08-08 14:25:57 -07002095
2096 /* Success, stop navigating through remaining System RAM ranges */
2097 return 1;
2098}
2099
2100static int locate_mem_hole_callback(u64 start, u64 end, void *arg)
2101{
2102 struct kexec_buf *kbuf = (struct kexec_buf *)arg;
2103 unsigned long sz = end - start + 1;
2104
2105 /* Returning 0 will take to next memory range */
2106 if (sz < kbuf->memsz)
2107 return 0;
2108
2109 if (end < kbuf->buf_min || start > kbuf->buf_max)
2110 return 0;
2111
2112 /*
2113 * Allocate memory top down with-in ram range. Otherwise bottom up
2114 * allocation.
2115 */
2116 if (kbuf->top_down)
2117 return locate_mem_hole_top_down(start, end, kbuf);
2118 return locate_mem_hole_bottom_up(start, end, kbuf);
2119}
2120
2121/*
2122 * Helper function for placing a buffer in a kexec segment. This assumes
2123 * that kexec_mutex is held.
2124 */
2125int kexec_add_buffer(struct kimage *image, char *buffer, unsigned long bufsz,
2126 unsigned long memsz, unsigned long buf_align,
2127 unsigned long buf_min, unsigned long buf_max,
2128 bool top_down, unsigned long *load_addr)
2129{
2130
2131 struct kexec_segment *ksegment;
2132 struct kexec_buf buf, *kbuf;
2133 int ret;
2134
2135 /* Currently adding segment this way is allowed only in file mode */
2136 if (!image->file_mode)
2137 return -EINVAL;
2138
2139 if (image->nr_segments >= KEXEC_SEGMENT_MAX)
2140 return -EINVAL;
2141
2142 /*
2143 * Make sure we are not trying to add buffer after allocating
2144 * control pages. All segments need to be placed first before
2145 * any control pages are allocated. As control page allocation
2146 * logic goes through list of segments to make sure there are
2147 * no destination overlaps.
2148 */
2149 if (!list_empty(&image->control_pages)) {
2150 WARN_ON(1);
2151 return -EINVAL;
2152 }
2153
2154 memset(&buf, 0, sizeof(struct kexec_buf));
2155 kbuf = &buf;
2156 kbuf->image = image;
2157 kbuf->buffer = buffer;
2158 kbuf->bufsz = bufsz;
2159
2160 kbuf->memsz = ALIGN(memsz, PAGE_SIZE);
2161 kbuf->buf_align = max(buf_align, PAGE_SIZE);
2162 kbuf->buf_min = buf_min;
2163 kbuf->buf_max = buf_max;
2164 kbuf->top_down = top_down;
2165
2166 /* Walk the RAM ranges and allocate a suitable range for the buffer */
Vivek Goyaldd5f7262014-08-08 14:26:09 -07002167 if (image->type == KEXEC_TYPE_CRASH)
2168 ret = walk_iomem_res("Crash kernel",
2169 IORESOURCE_MEM | IORESOURCE_BUSY,
2170 crashk_res.start, crashk_res.end, kbuf,
2171 locate_mem_hole_callback);
2172 else
2173 ret = walk_system_ram_res(0, -1, kbuf,
2174 locate_mem_hole_callback);
Vivek Goyalcb105252014-08-08 14:25:57 -07002175 if (ret != 1) {
2176 /* A suitable memory range could not be found for buffer */
2177 return -EADDRNOTAVAIL;
2178 }
2179
2180 /* Found a suitable memory range */
Baoquan He669280a2014-10-13 15:53:40 -07002181 ksegment = &image->segment[image->nr_segments];
2182 ksegment->kbuf = kbuf->buffer;
2183 ksegment->bufsz = kbuf->bufsz;
2184 ksegment->mem = kbuf->mem;
2185 ksegment->memsz = kbuf->memsz;
2186 image->nr_segments++;
Vivek Goyalcb105252014-08-08 14:25:57 -07002187 *load_addr = ksegment->mem;
2188 return 0;
2189}
2190
Vivek Goyal12db5562014-08-08 14:26:04 -07002191/* Calculate and store the digest of segments */
2192static int kexec_calculate_store_digests(struct kimage *image)
2193{
2194 struct crypto_shash *tfm;
2195 struct shash_desc *desc;
2196 int ret = 0, i, j, zero_buf_sz, sha_region_sz;
2197 size_t desc_size, nullsz;
2198 char *digest;
2199 void *zero_buf;
2200 struct kexec_sha_region *sha_regions;
2201 struct purgatory_info *pi = &image->purgatory_info;
2202
2203 zero_buf = __va(page_to_pfn(ZERO_PAGE(0)) << PAGE_SHIFT);
2204 zero_buf_sz = PAGE_SIZE;
2205
2206 tfm = crypto_alloc_shash("sha256", 0, 0);
2207 if (IS_ERR(tfm)) {
2208 ret = PTR_ERR(tfm);
2209 goto out;
2210 }
2211
2212 desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
2213 desc = kzalloc(desc_size, GFP_KERNEL);
2214 if (!desc) {
2215 ret = -ENOMEM;
2216 goto out_free_tfm;
2217 }
2218
2219 sha_region_sz = KEXEC_SEGMENT_MAX * sizeof(struct kexec_sha_region);
2220 sha_regions = vzalloc(sha_region_sz);
2221 if (!sha_regions)
2222 goto out_free_desc;
2223
2224 desc->tfm = tfm;
2225 desc->flags = 0;
2226
2227 ret = crypto_shash_init(desc);
2228 if (ret < 0)
2229 goto out_free_sha_regions;
2230
2231 digest = kzalloc(SHA256_DIGEST_SIZE, GFP_KERNEL);
2232 if (!digest) {
2233 ret = -ENOMEM;
2234 goto out_free_sha_regions;
2235 }
2236
2237 for (j = i = 0; i < image->nr_segments; i++) {
2238 struct kexec_segment *ksegment;
2239
2240 ksegment = &image->segment[i];
2241 /*
2242 * Skip purgatory as it will be modified once we put digest
2243 * info in purgatory.
2244 */
2245 if (ksegment->kbuf == pi->purgatory_buf)
2246 continue;
2247
2248 ret = crypto_shash_update(desc, ksegment->kbuf,
2249 ksegment->bufsz);
2250 if (ret)
2251 break;
2252
2253 /*
2254 * Assume rest of the buffer is filled with zero and
2255 * update digest accordingly.
2256 */
2257 nullsz = ksegment->memsz - ksegment->bufsz;
2258 while (nullsz) {
2259 unsigned long bytes = nullsz;
2260
2261 if (bytes > zero_buf_sz)
2262 bytes = zero_buf_sz;
2263 ret = crypto_shash_update(desc, zero_buf, bytes);
2264 if (ret)
2265 break;
2266 nullsz -= bytes;
2267 }
2268
2269 if (ret)
2270 break;
2271
2272 sha_regions[j].start = ksegment->mem;
2273 sha_regions[j].len = ksegment->memsz;
2274 j++;
2275 }
2276
2277 if (!ret) {
2278 ret = crypto_shash_final(desc, digest);
2279 if (ret)
2280 goto out_free_digest;
2281 ret = kexec_purgatory_get_set_symbol(image, "sha_regions",
2282 sha_regions, sha_region_sz, 0);
2283 if (ret)
2284 goto out_free_digest;
2285
2286 ret = kexec_purgatory_get_set_symbol(image, "sha256_digest",
2287 digest, SHA256_DIGEST_SIZE, 0);
2288 if (ret)
2289 goto out_free_digest;
2290 }
2291
2292out_free_digest:
2293 kfree(digest);
2294out_free_sha_regions:
2295 vfree(sha_regions);
2296out_free_desc:
2297 kfree(desc);
2298out_free_tfm:
2299 kfree(tfm);
2300out:
2301 return ret;
2302}
2303
2304/* Actually load purgatory. Lot of code taken from kexec-tools */
2305static int __kexec_load_purgatory(struct kimage *image, unsigned long min,
2306 unsigned long max, int top_down)
2307{
2308 struct purgatory_info *pi = &image->purgatory_info;
2309 unsigned long align, buf_align, bss_align, buf_sz, bss_sz, bss_pad;
2310 unsigned long memsz, entry, load_addr, curr_load_addr, bss_addr, offset;
2311 unsigned char *buf_addr, *src;
2312 int i, ret = 0, entry_sidx = -1;
2313 const Elf_Shdr *sechdrs_c;
2314 Elf_Shdr *sechdrs = NULL;
2315 void *purgatory_buf = NULL;
2316
2317 /*
2318 * sechdrs_c points to section headers in purgatory and are read
2319 * only. No modifications allowed.
2320 */
2321 sechdrs_c = (void *)pi->ehdr + pi->ehdr->e_shoff;
2322
2323 /*
2324 * We can not modify sechdrs_c[] and its fields. It is read only.
2325 * Copy it over to a local copy where one can store some temporary
2326 * data and free it at the end. We need to modify ->sh_addr and
2327 * ->sh_offset fields to keep track of permanent and temporary
2328 * locations of sections.
2329 */
2330 sechdrs = vzalloc(pi->ehdr->e_shnum * sizeof(Elf_Shdr));
2331 if (!sechdrs)
2332 return -ENOMEM;
2333
2334 memcpy(sechdrs, sechdrs_c, pi->ehdr->e_shnum * sizeof(Elf_Shdr));
2335
2336 /*
2337 * We seem to have multiple copies of sections. First copy is which
2338 * is embedded in kernel in read only section. Some of these sections
2339 * will be copied to a temporary buffer and relocated. And these
2340 * sections will finally be copied to their final destination at
2341 * segment load time.
2342 *
2343 * Use ->sh_offset to reflect section address in memory. It will
2344 * point to original read only copy if section is not allocatable.
2345 * Otherwise it will point to temporary copy which will be relocated.
2346 *
2347 * Use ->sh_addr to contain final address of the section where it
2348 * will go during execution time.
2349 */
2350 for (i = 0; i < pi->ehdr->e_shnum; i++) {
2351 if (sechdrs[i].sh_type == SHT_NOBITS)
2352 continue;
2353
2354 sechdrs[i].sh_offset = (unsigned long)pi->ehdr +
2355 sechdrs[i].sh_offset;
2356 }
2357
2358 /*
2359 * Identify entry point section and make entry relative to section
2360 * start.
2361 */
2362 entry = pi->ehdr->e_entry;
2363 for (i = 0; i < pi->ehdr->e_shnum; i++) {
2364 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2365 continue;
2366
2367 if (!(sechdrs[i].sh_flags & SHF_EXECINSTR))
2368 continue;
2369
2370 /* Make entry section relative */
2371 if (sechdrs[i].sh_addr <= pi->ehdr->e_entry &&
2372 ((sechdrs[i].sh_addr + sechdrs[i].sh_size) >
2373 pi->ehdr->e_entry)) {
2374 entry_sidx = i;
2375 entry -= sechdrs[i].sh_addr;
2376 break;
2377 }
2378 }
2379
2380 /* Determine how much memory is needed to load relocatable object. */
2381 buf_align = 1;
2382 bss_align = 1;
2383 buf_sz = 0;
2384 bss_sz = 0;
2385
2386 for (i = 0; i < pi->ehdr->e_shnum; i++) {
2387 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2388 continue;
2389
2390 align = sechdrs[i].sh_addralign;
2391 if (sechdrs[i].sh_type != SHT_NOBITS) {
2392 if (buf_align < align)
2393 buf_align = align;
2394 buf_sz = ALIGN(buf_sz, align);
2395 buf_sz += sechdrs[i].sh_size;
2396 } else {
2397 /* bss section */
2398 if (bss_align < align)
2399 bss_align = align;
2400 bss_sz = ALIGN(bss_sz, align);
2401 bss_sz += sechdrs[i].sh_size;
2402 }
2403 }
2404
2405 /* Determine the bss padding required to align bss properly */
2406 bss_pad = 0;
2407 if (buf_sz & (bss_align - 1))
2408 bss_pad = bss_align - (buf_sz & (bss_align - 1));
2409
2410 memsz = buf_sz + bss_pad + bss_sz;
2411
2412 /* Allocate buffer for purgatory */
2413 purgatory_buf = vzalloc(buf_sz);
2414 if (!purgatory_buf) {
2415 ret = -ENOMEM;
2416 goto out;
2417 }
2418
2419 if (buf_align < bss_align)
2420 buf_align = bss_align;
2421
2422 /* Add buffer to segment list */
2423 ret = kexec_add_buffer(image, purgatory_buf, buf_sz, memsz,
2424 buf_align, min, max, top_down,
2425 &pi->purgatory_load_addr);
2426 if (ret)
2427 goto out;
2428
2429 /* Load SHF_ALLOC sections */
2430 buf_addr = purgatory_buf;
2431 load_addr = curr_load_addr = pi->purgatory_load_addr;
2432 bss_addr = load_addr + buf_sz + bss_pad;
2433
2434 for (i = 0; i < pi->ehdr->e_shnum; i++) {
2435 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2436 continue;
2437
2438 align = sechdrs[i].sh_addralign;
2439 if (sechdrs[i].sh_type != SHT_NOBITS) {
2440 curr_load_addr = ALIGN(curr_load_addr, align);
2441 offset = curr_load_addr - load_addr;
2442 /* We already modifed ->sh_offset to keep src addr */
2443 src = (char *) sechdrs[i].sh_offset;
2444 memcpy(buf_addr + offset, src, sechdrs[i].sh_size);
2445
2446 /* Store load address and source address of section */
2447 sechdrs[i].sh_addr = curr_load_addr;
2448
2449 /*
2450 * This section got copied to temporary buffer. Update
2451 * ->sh_offset accordingly.
2452 */
2453 sechdrs[i].sh_offset = (unsigned long)(buf_addr + offset);
2454
2455 /* Advance to the next address */
2456 curr_load_addr += sechdrs[i].sh_size;
2457 } else {
2458 bss_addr = ALIGN(bss_addr, align);
2459 sechdrs[i].sh_addr = bss_addr;
2460 bss_addr += sechdrs[i].sh_size;
2461 }
2462 }
2463
2464 /* Update entry point based on load address of text section */
2465 if (entry_sidx >= 0)
2466 entry += sechdrs[entry_sidx].sh_addr;
2467
2468 /* Make kernel jump to purgatory after shutdown */
2469 image->start = entry;
2470
2471 /* Used later to get/set symbol values */
2472 pi->sechdrs = sechdrs;
2473
2474 /*
2475 * Used later to identify which section is purgatory and skip it
2476 * from checksumming.
2477 */
2478 pi->purgatory_buf = purgatory_buf;
2479 return ret;
2480out:
2481 vfree(sechdrs);
2482 vfree(purgatory_buf);
2483 return ret;
2484}
2485
2486static int kexec_apply_relocations(struct kimage *image)
2487{
2488 int i, ret;
2489 struct purgatory_info *pi = &image->purgatory_info;
2490 Elf_Shdr *sechdrs = pi->sechdrs;
2491
2492 /* Apply relocations */
2493 for (i = 0; i < pi->ehdr->e_shnum; i++) {
2494 Elf_Shdr *section, *symtab;
2495
2496 if (sechdrs[i].sh_type != SHT_RELA &&
2497 sechdrs[i].sh_type != SHT_REL)
2498 continue;
2499
2500 /*
2501 * For section of type SHT_RELA/SHT_REL,
2502 * ->sh_link contains section header index of associated
2503 * symbol table. And ->sh_info contains section header
2504 * index of section to which relocations apply.
2505 */
2506 if (sechdrs[i].sh_info >= pi->ehdr->e_shnum ||
2507 sechdrs[i].sh_link >= pi->ehdr->e_shnum)
2508 return -ENOEXEC;
2509
2510 section = &sechdrs[sechdrs[i].sh_info];
2511 symtab = &sechdrs[sechdrs[i].sh_link];
2512
2513 if (!(section->sh_flags & SHF_ALLOC))
2514 continue;
2515
2516 /*
2517 * symtab->sh_link contain section header index of associated
2518 * string table.
2519 */
2520 if (symtab->sh_link >= pi->ehdr->e_shnum)
2521 /* Invalid section number? */
2522 continue;
2523
2524 /*
Borislav Petkovedb0ec02015-01-25 19:50:34 +01002525 * Respective architecture needs to provide support for applying
Vivek Goyal12db5562014-08-08 14:26:04 -07002526 * relocations of type SHT_RELA/SHT_REL.
2527 */
2528 if (sechdrs[i].sh_type == SHT_RELA)
2529 ret = arch_kexec_apply_relocations_add(pi->ehdr,
2530 sechdrs, i);
2531 else if (sechdrs[i].sh_type == SHT_REL)
2532 ret = arch_kexec_apply_relocations(pi->ehdr,
2533 sechdrs, i);
2534 if (ret)
2535 return ret;
2536 }
2537
2538 return 0;
2539}
2540
2541/* Load relocatable purgatory object and relocate it appropriately */
2542int kexec_load_purgatory(struct kimage *image, unsigned long min,
2543 unsigned long max, int top_down,
2544 unsigned long *load_addr)
2545{
2546 struct purgatory_info *pi = &image->purgatory_info;
2547 int ret;
2548
2549 if (kexec_purgatory_size <= 0)
2550 return -EINVAL;
2551
2552 if (kexec_purgatory_size < sizeof(Elf_Ehdr))
2553 return -ENOEXEC;
2554
2555 pi->ehdr = (Elf_Ehdr *)kexec_purgatory;
2556
2557 if (memcmp(pi->ehdr->e_ident, ELFMAG, SELFMAG) != 0
2558 || pi->ehdr->e_type != ET_REL
2559 || !elf_check_arch(pi->ehdr)
2560 || pi->ehdr->e_shentsize != sizeof(Elf_Shdr))
2561 return -ENOEXEC;
2562
2563 if (pi->ehdr->e_shoff >= kexec_purgatory_size
2564 || (pi->ehdr->e_shnum * sizeof(Elf_Shdr) >
2565 kexec_purgatory_size - pi->ehdr->e_shoff))
2566 return -ENOEXEC;
2567
2568 ret = __kexec_load_purgatory(image, min, max, top_down);
2569 if (ret)
2570 return ret;
2571
2572 ret = kexec_apply_relocations(image);
2573 if (ret)
2574 goto out;
2575
2576 *load_addr = pi->purgatory_load_addr;
2577 return 0;
2578out:
2579 vfree(pi->sechdrs);
2580 vfree(pi->purgatory_buf);
2581 return ret;
2582}
2583
2584static Elf_Sym *kexec_purgatory_find_symbol(struct purgatory_info *pi,
2585 const char *name)
2586{
2587 Elf_Sym *syms;
2588 Elf_Shdr *sechdrs;
2589 Elf_Ehdr *ehdr;
2590 int i, k;
2591 const char *strtab;
2592
2593 if (!pi->sechdrs || !pi->ehdr)
2594 return NULL;
2595
2596 sechdrs = pi->sechdrs;
2597 ehdr = pi->ehdr;
2598
2599 for (i = 0; i < ehdr->e_shnum; i++) {
2600 if (sechdrs[i].sh_type != SHT_SYMTAB)
2601 continue;
2602
2603 if (sechdrs[i].sh_link >= ehdr->e_shnum)
2604 /* Invalid strtab section number */
2605 continue;
2606 strtab = (char *)sechdrs[sechdrs[i].sh_link].sh_offset;
2607 syms = (Elf_Sym *)sechdrs[i].sh_offset;
2608
2609 /* Go through symbols for a match */
2610 for (k = 0; k < sechdrs[i].sh_size/sizeof(Elf_Sym); k++) {
2611 if (ELF_ST_BIND(syms[k].st_info) != STB_GLOBAL)
2612 continue;
2613
2614 if (strcmp(strtab + syms[k].st_name, name) != 0)
2615 continue;
2616
2617 if (syms[k].st_shndx == SHN_UNDEF ||
2618 syms[k].st_shndx >= ehdr->e_shnum) {
2619 pr_debug("Symbol: %s has bad section index %d.\n",
2620 name, syms[k].st_shndx);
2621 return NULL;
2622 }
2623
2624 /* Found the symbol we are looking for */
2625 return &syms[k];
2626 }
2627 }
2628
2629 return NULL;
2630}
2631
2632void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name)
2633{
2634 struct purgatory_info *pi = &image->purgatory_info;
2635 Elf_Sym *sym;
2636 Elf_Shdr *sechdr;
2637
2638 sym = kexec_purgatory_find_symbol(pi, name);
2639 if (!sym)
2640 return ERR_PTR(-EINVAL);
2641
2642 sechdr = &pi->sechdrs[sym->st_shndx];
2643
2644 /*
2645 * Returns the address where symbol will finally be loaded after
2646 * kexec_load_segment()
2647 */
2648 return (void *)(sechdr->sh_addr + sym->st_value);
2649}
2650
2651/*
2652 * Get or set value of a symbol. If "get_value" is true, symbol value is
2653 * returned in buf otherwise symbol value is set based on value in buf.
2654 */
2655int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name,
2656 void *buf, unsigned int size, bool get_value)
2657{
2658 Elf_Sym *sym;
2659 Elf_Shdr *sechdrs;
2660 struct purgatory_info *pi = &image->purgatory_info;
2661 char *sym_buf;
2662
2663 sym = kexec_purgatory_find_symbol(pi, name);
2664 if (!sym)
2665 return -EINVAL;
2666
2667 if (sym->st_size != size) {
2668 pr_err("symbol %s size mismatch: expected %lu actual %u\n",
2669 name, (unsigned long)sym->st_size, size);
2670 return -EINVAL;
2671 }
2672
2673 sechdrs = pi->sechdrs;
2674
2675 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
2676 pr_err("symbol %s is in a bss section. Cannot %s\n", name,
2677 get_value ? "get" : "set");
2678 return -EINVAL;
2679 }
2680
2681 sym_buf = (unsigned char *)sechdrs[sym->st_shndx].sh_offset +
2682 sym->st_value;
2683
2684 if (get_value)
2685 memcpy((void *)buf, sym_buf, size);
2686 else
2687 memcpy((void *)sym_buf, buf, size);
2688
2689 return 0;
2690}
Vivek Goyal74ca3172014-08-29 15:18:46 -07002691#endif /* CONFIG_KEXEC_FILE */
Vivek Goyalcb105252014-08-08 14:25:57 -07002692
Huang Ying7ade3fc2008-08-15 00:40:21 -07002693/*
2694 * Move into place and start executing a preloaded standalone
2695 * executable. If nothing was preloaded return an error.
Huang Ying3ab83522008-07-25 19:45:07 -07002696 */
2697int kernel_kexec(void)
2698{
2699 int error = 0;
2700
Andrew Morton8c5a1cf2008-08-15 00:40:27 -07002701 if (!mutex_trylock(&kexec_mutex))
Huang Ying3ab83522008-07-25 19:45:07 -07002702 return -EBUSY;
2703 if (!kexec_image) {
2704 error = -EINVAL;
2705 goto Unlock;
2706 }
2707
Huang Ying3ab83522008-07-25 19:45:07 -07002708#ifdef CONFIG_KEXEC_JUMP
Huang Ying7ade3fc2008-08-15 00:40:21 -07002709 if (kexec_image->preserve_context) {
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01002710 lock_system_sleep();
Huang Ying89081d12008-07-25 19:45:10 -07002711 pm_prepare_console();
2712 error = freeze_processes();
2713 if (error) {
2714 error = -EBUSY;
2715 goto Restore_console;
2716 }
2717 suspend_console();
Alan Sternd1616302009-05-24 22:05:42 +02002718 error = dpm_suspend_start(PMSG_FREEZE);
Huang Ying89081d12008-07-25 19:45:10 -07002719 if (error)
2720 goto Resume_console;
Alan Sternd1616302009-05-24 22:05:42 +02002721 /* At this point, dpm_suspend_start() has been called,
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01002722 * but *not* dpm_suspend_end(). We *must* call
2723 * dpm_suspend_end() now. Otherwise, drivers for
Huang Ying89081d12008-07-25 19:45:10 -07002724 * some devices (e.g. interrupt controllers) become
2725 * desynchronized with the actual state of the
2726 * hardware at resume time, and evil weirdness ensues.
2727 */
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01002728 error = dpm_suspend_end(PMSG_FREEZE);
Huang Ying89081d12008-07-25 19:45:10 -07002729 if (error)
Rafael J. Wysocki749b0af2009-03-16 22:34:35 +01002730 goto Resume_devices;
2731 error = disable_nonboot_cpus();
2732 if (error)
2733 goto Enable_cpus;
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +01002734 local_irq_disable();
Rafael J. Wysocki2e711c02011-04-26 19:15:07 +02002735 error = syscore_suspend();
Rafael J. Wysocki770824b2009-02-22 18:38:50 +01002736 if (error)
Rafael J. Wysocki749b0af2009-03-16 22:34:35 +01002737 goto Enable_irqs;
Huang Ying7ade3fc2008-08-15 00:40:21 -07002738 } else
Huang Ying3ab83522008-07-25 19:45:07 -07002739#endif
Huang Ying7ade3fc2008-08-15 00:40:21 -07002740 {
Khalid Aziz4fc9bbf2013-11-27 15:19:25 -07002741 kexec_in_progress = true;
Huang Yingca195b72008-08-15 00:40:24 -07002742 kernel_restart_prepare(NULL);
Vivek Goyalc97102b2013-12-18 17:08:31 -08002743 migrate_to_reboot_cpu();
Srivatsa S. Bhat011e4b02014-05-27 16:25:34 +05302744
2745 /*
2746 * migrate_to_reboot_cpu() disables CPU hotplug assuming that
2747 * no further code needs to use CPU hotplug (which is true in
2748 * the reboot case). However, the kexec path depends on using
2749 * CPU hotplug again; so re-enable it here.
2750 */
2751 cpu_hotplug_enable();
Fabian Fredericke1bebcf2014-06-06 14:37:09 -07002752 pr_emerg("Starting new kernel\n");
Huang Ying3ab83522008-07-25 19:45:07 -07002753 machine_shutdown();
2754 }
2755
2756 machine_kexec(kexec_image);
2757
Huang Ying3ab83522008-07-25 19:45:07 -07002758#ifdef CONFIG_KEXEC_JUMP
Huang Ying7ade3fc2008-08-15 00:40:21 -07002759 if (kexec_image->preserve_context) {
Rafael J. Wysocki19234c02011-04-20 00:36:11 +02002760 syscore_resume();
Rafael J. Wysocki749b0af2009-03-16 22:34:35 +01002761 Enable_irqs:
Huang Ying3ab83522008-07-25 19:45:07 -07002762 local_irq_enable();
Rafael J. Wysocki749b0af2009-03-16 22:34:35 +01002763 Enable_cpus:
Huang Ying89081d12008-07-25 19:45:10 -07002764 enable_nonboot_cpus();
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01002765 dpm_resume_start(PMSG_RESTORE);
Huang Ying89081d12008-07-25 19:45:10 -07002766 Resume_devices:
Alan Sternd1616302009-05-24 22:05:42 +02002767 dpm_resume_end(PMSG_RESTORE);
Huang Ying89081d12008-07-25 19:45:10 -07002768 Resume_console:
2769 resume_console();
2770 thaw_processes();
2771 Restore_console:
2772 pm_restore_console();
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01002773 unlock_system_sleep();
Huang Ying3ab83522008-07-25 19:45:07 -07002774 }
Huang Ying7ade3fc2008-08-15 00:40:21 -07002775#endif
Huang Ying3ab83522008-07-25 19:45:07 -07002776
2777 Unlock:
Andrew Morton8c5a1cf2008-08-15 00:40:27 -07002778 mutex_unlock(&kexec_mutex);
Huang Ying3ab83522008-07-25 19:45:07 -07002779 return error;
2780}