~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/kernel/kexec.c

Version: ~ [ linux-5.16-rc3 ] ~ [ linux-5.15.5 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.82 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.162 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.218 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.256 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.291 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.293 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  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 
  9 #define pr_fmt(fmt)     "kexec: " fmt
 10 
 11 #include <linux/capability.h>
 12 #include <linux/mm.h>
 13 #include <linux/file.h>
 14 #include <linux/slab.h>
 15 #include <linux/fs.h>
 16 #include <linux/kexec.h>
 17 #include <linux/mutex.h>
 18 #include <linux/list.h>
 19 #include <linux/highmem.h>
 20 #include <linux/syscalls.h>
 21 #include <linux/reboot.h>
 22 #include <linux/ioport.h>
 23 #include <linux/hardirq.h>
 24 #include <linux/elf.h>
 25 #include <linux/elfcore.h>
 26 #include <linux/utsname.h>
 27 #include <linux/numa.h>
 28 #include <linux/suspend.h>
 29 #include <linux/device.h>
 30 #include <linux/freezer.h>
 31 #include <linux/pm.h>
 32 #include <linux/cpu.h>
 33 #include <linux/console.h>
 34 #include <linux/vmalloc.h>
 35 #include <linux/swap.h>
 36 #include <linux/syscore_ops.h>
 37 #include <linux/compiler.h>
 38 #include <linux/hugetlb.h>
 39 
 40 #include <asm/page.h>
 41 #include <asm/uaccess.h>
 42 #include <asm/io.h>
 43 #include <asm/sections.h>
 44 #include <linux/ccsecurity.h>
 45 
 46 #include <crypto/hash.h>
 47 #include <crypto/sha.h>
 48 
 49 /* Per cpu memory for storing cpu states in case of system crash. */
 50 note_buf_t __percpu *crash_notes;
 51 
 52 /* vmcoreinfo stuff */
 53 static unsigned char vmcoreinfo_data[VMCOREINFO_BYTES];
 54 u32 vmcoreinfo_note[VMCOREINFO_NOTE_SIZE/4];
 55 size_t vmcoreinfo_size;
 56 size_t vmcoreinfo_max_size = sizeof(vmcoreinfo_data);
 57 
 58 /* Flag to indicate we are going to kexec a new kernel */
 59 bool kexec_in_progress = false;
 60 
 61 /*
 62  * Declare these symbols weak so that if architecture provides a purgatory,
 63  * these will be overridden.
 64  */
 65 char __weak kexec_purgatory[0];
 66 size_t __weak kexec_purgatory_size = 0;
 67 
 68 #ifdef CONFIG_KEXEC_FILE
 69 static int kexec_calculate_store_digests(struct kimage *image);
 70 #endif
 71 
 72 /* Location of the reserved area for the crash kernel */
 73 struct resource crashk_res = {
 74         .name  = "Crash kernel",
 75         .start = 0,
 76         .end   = 0,
 77         .flags = IORESOURCE_BUSY | IORESOURCE_MEM
 78 };
 79 struct resource crashk_low_res = {
 80         .name  = "Crash kernel",
 81         .start = 0,
 82         .end   = 0,
 83         .flags = IORESOURCE_BUSY | IORESOURCE_MEM
 84 };
 85 
 86 int kexec_should_crash(struct task_struct *p)
 87 {
 88         /*
 89          * If crash_kexec_post_notifiers is enabled, don't run
 90          * crash_kexec() here yet, which must be run after panic
 91          * notifiers in panic().
 92          */
 93         if (crash_kexec_post_notifiers)
 94                 return 0;
 95         /*
 96          * There are 4 panic() calls in do_exit() path, each of which
 97          * corresponds to each of these 4 conditions.
 98          */
 99         if (in_interrupt() || !p->pid || is_global_init(p) || panic_on_oops)
100                 return 1;
101         return 0;
102 }
103 
104 /*
105  * When kexec transitions to the new kernel there is a one-to-one
106  * mapping between physical and virtual addresses.  On processors
107  * where you can disable the MMU this is trivial, and easy.  For
108  * others it is still a simple predictable page table to setup.
109  *
110  * In that environment kexec copies the new kernel to its final
111  * resting place.  This means I can only support memory whose
112  * physical address can fit in an unsigned long.  In particular
113  * addresses where (pfn << PAGE_SHIFT) > ULONG_MAX cannot be handled.
114  * If the assembly stub has more restrictive requirements
115  * KEXEC_SOURCE_MEMORY_LIMIT and KEXEC_DEST_MEMORY_LIMIT can be
116  * defined more restrictively in <asm/kexec.h>.
117  *
118  * The code for the transition from the current kernel to the
119  * the new kernel is placed in the control_code_buffer, whose size
120  * is given by KEXEC_CONTROL_PAGE_SIZE.  In the best case only a single
121  * page of memory is necessary, but some architectures require more.
122  * Because this memory must be identity mapped in the transition from
123  * virtual to physical addresses it must live in the range
124  * 0 - TASK_SIZE, as only the user space mappings are arbitrarily
125  * modifiable.
126  *
127  * The assembly stub in the control code buffer is passed a linked list
128  * of descriptor pages detailing the source pages of the new kernel,
129  * and the destination addresses of those source pages.  As this data
130  * structure is not used in the context of the current OS, it must
131  * be self-contained.
132  *
133  * The code has been made to work with highmem pages and will use a
134  * destination page in its final resting place (if it happens
135  * to allocate it).  The end product of this is that most of the
136  * physical address space, and most of RAM can be used.
137  *
138  * Future directions include:
139  *  - allocating a page table with the control code buffer identity
140  *    mapped, to simplify machine_kexec and make kexec_on_panic more
141  *    reliable.
142  */
143 
144 /*
145  * KIMAGE_NO_DEST is an impossible destination address..., for
146  * allocating pages whose destination address we do not care about.
147  */
148 #define KIMAGE_NO_DEST (-1UL)
149 
150 static int kimage_is_destination_range(struct kimage *image,
151                                        unsigned long start, unsigned long end);
152 static struct page *kimage_alloc_page(struct kimage *image,
153                                        gfp_t gfp_mask,
154                                        unsigned long dest);
155 
156 static int copy_user_segment_list(struct kimage *image,
157                                   unsigned long nr_segments,
158                                   struct kexec_segment __user *segments)
159 {
160         int ret;
161         size_t segment_bytes;
162 
163         /* Read in the segments */
164         image->nr_segments = nr_segments;
165         segment_bytes = nr_segments * sizeof(*segments);
166         ret = copy_from_user(image->segment, segments, segment_bytes);
167         if (ret)
168                 ret = -EFAULT;
169 
170         return ret;
171 }
172 
173 static int sanity_check_segment_list(struct kimage *image)
174 {
175         int result, i;
176         unsigned long nr_segments = image->nr_segments;
177 
178         /*
179          * Verify we have good destination addresses.  The caller is
180          * responsible for making certain we don't attempt to load
181          * the new image into invalid or reserved areas of RAM.  This
182          * just verifies it is an address we can use.
183          *
184          * Since the kernel does everything in page size chunks ensure
185          * the destination addresses are page aligned.  Too many
186          * special cases crop of when we don't do this.  The most
187          * insidious is getting overlapping destination addresses
188          * simply because addresses are changed to page size
189          * granularity.
190          */
191         result = -EADDRNOTAVAIL;
192         for (i = 0; i < nr_segments; i++) {
193                 unsigned long mstart, mend;
194 
195                 mstart = image->segment[i].mem;
196                 mend   = mstart + image->segment[i].memsz;
197                 if ((mstart & ~PAGE_MASK) || (mend & ~PAGE_MASK))
198                         return result;
199                 if (mend >= KEXEC_DESTINATION_MEMORY_LIMIT)
200                         return result;
201         }
202 
203         /* Verify our destination addresses do not overlap.
204          * If we alloed overlapping destination addresses
205          * through very weird things can happen with no
206          * easy explanation as one segment stops on another.
207          */
208         result = -EINVAL;
209         for (i = 0; i < nr_segments; i++) {
210                 unsigned long mstart, mend;
211                 unsigned long j;
212 
213                 mstart = image->segment[i].mem;
214                 mend   = mstart + image->segment[i].memsz;
215                 for (j = 0; j < i; j++) {
216                         unsigned long pstart, pend;
217                         pstart = image->segment[j].mem;
218                         pend   = pstart + image->segment[j].memsz;
219                         /* Do the segments overlap ? */
220                         if ((mend > pstart) && (mstart < pend))
221                                 return result;
222                 }
223         }
224 
225         /* Ensure our buffer sizes are strictly less than
226          * our memory sizes.  This should always be the case,
227          * and it is easier to check up front than to be surprised
228          * later on.
229          */
230         result = -EINVAL;
231         for (i = 0; i < nr_segments; i++) {
232                 if (image->segment[i].bufsz > image->segment[i].memsz)
233                         return result;
234         }
235 
236         /*
237          * Verify we have good destination addresses.  Normally
238          * the caller is responsible for making certain we don't
239          * attempt to load the new image into invalid or reserved
240          * areas of RAM.  But crash kernels are preloaded into a
241          * reserved area of ram.  We must ensure the addresses
242          * are in the reserved area otherwise preloading the
243          * kernel could corrupt things.
244          */
245 
246         if (image->type == KEXEC_TYPE_CRASH) {
247                 result = -EADDRNOTAVAIL;
248                 for (i = 0; i < nr_segments; i++) {
249                         unsigned long mstart, mend;
250 
251                         mstart = image->segment[i].mem;
252                         mend = mstart + image->segment[i].memsz - 1;
253                         /* Ensure we are within the crash kernel limits */
254                         if ((mstart < crashk_res.start) ||
255                             (mend > crashk_res.end))
256                                 return result;
257                 }
258         }
259 
260         return 0;
261 }
262 
263 static struct kimage *do_kimage_alloc_init(void)
264 {
265         struct kimage *image;
266 
267         /* Allocate a controlling structure */
268         image = kzalloc(sizeof(*image), GFP_KERNEL);
269         if (!image)
270                 return NULL;
271 
272         image->head = 0;
273         image->entry = &image->head;
274         image->last_entry = &image->head;
275         image->control_page = ~0; /* By default this does not apply */
276         image->type = KEXEC_TYPE_DEFAULT;
277 
278         /* Initialize the list of control pages */
279         INIT_LIST_HEAD(&image->control_pages);
280 
281         /* Initialize the list of destination pages */
282         INIT_LIST_HEAD(&image->dest_pages);
283 
284         /* Initialize the list of unusable pages */
285         INIT_LIST_HEAD(&image->unusable_pages);
286 
287         return image;
288 }
289 
290 static void kimage_free_page_list(struct list_head *list);
291 
292 static int kimage_alloc_init(struct kimage **rimage, unsigned long entry,
293                              unsigned long nr_segments,
294                              struct kexec_segment __user *segments,
295                              unsigned long flags)
296 {
297         int ret;
298         struct kimage *image;
299         bool kexec_on_panic = flags & KEXEC_ON_CRASH;
300 
301         if (kexec_on_panic) {
302                 /* Verify we have a valid entry point */
303                 if ((entry < crashk_res.start) || (entry > crashk_res.end))
304                         return -EADDRNOTAVAIL;
305         }
306 
307         /* Allocate and initialize a controlling structure */
308         image = do_kimage_alloc_init();
309         if (!image)
310                 return -ENOMEM;
311 
312         image->start = entry;
313 
314         ret = copy_user_segment_list(image, nr_segments, segments);
315         if (ret)
316                 goto out_free_image;
317 
318         ret = sanity_check_segment_list(image);
319         if (ret)
320                 goto out_free_image;
321 
322          /* Enable the special crash kernel control page allocation policy. */
323         if (kexec_on_panic) {
324                 image->control_page = crashk_res.start;
325                 image->type = KEXEC_TYPE_CRASH;
326         }
327 
328         /*
329          * Find a location for the control code buffer, and add it
330          * the vector of segments so that it's pages will also be
331          * counted as destination pages.
332          */
333         ret = -ENOMEM;
334         image->control_code_page = kimage_alloc_control_pages(image,
335                                            get_order(KEXEC_CONTROL_PAGE_SIZE));
336         if (!image->control_code_page) {
337                 pr_err("Could not allocate control_code_buffer\n");
338                 goto out_free_image;
339         }
340 
341         if (!kexec_on_panic) {
342                 image->swap_page = kimage_alloc_control_pages(image, 0);
343                 if (!image->swap_page) {
344                         pr_err("Could not allocate swap buffer\n");
345                         goto out_free_control_pages;
346                 }
347         }
348 
349         *rimage = image;
350         return 0;
351 out_free_control_pages:
352         kimage_free_page_list(&image->control_pages);
353 out_free_image:
354         kfree(image);
355         return ret;
356 }
357 
358 #ifdef CONFIG_KEXEC_FILE
359 static int copy_file_from_fd(int fd, void **buf, unsigned long *buf_len)
360 {
361         struct fd f = fdget(fd);
362         int ret;
363         struct kstat stat;
364         loff_t pos;
365         ssize_t bytes = 0;
366 
367         if (!f.file)
368                 return -EBADF;
369 
370         ret = vfs_getattr(&f.file->f_path, &stat);
371         if (ret)
372                 goto out;
373 
374         if (stat.size > INT_MAX) {
375                 ret = -EFBIG;
376                 goto out;
377         }
378 
379         /* Don't hand 0 to vmalloc, it whines. */
380         if (stat.size == 0) {
381                 ret = -EINVAL;
382                 goto out;
383         }
384 
385         *buf = vmalloc(stat.size);
386         if (!*buf) {
387                 ret = -ENOMEM;
388                 goto out;
389         }
390 
391         pos = 0;
392         while (pos < stat.size) {
393                 bytes = kernel_read(f.file, pos, (char *)(*buf) + pos,
394                                     stat.size - pos);
395                 if (bytes < 0) {
396                         vfree(*buf);
397                         ret = bytes;
398                         goto out;
399                 }
400 
401                 if (bytes == 0)
402                         break;
403                 pos += bytes;
404         }
405 
406         if (pos != stat.size) {
407                 ret = -EBADF;
408                 vfree(*buf);
409                 goto out;
410         }
411 
412         *buf_len = pos;
413 out:
414         fdput(f);
415         return ret;
416 }
417 
418 /* Architectures can provide this probe function */
419 int __weak arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
420                                          unsigned long buf_len)
421 {
422         return -ENOEXEC;
423 }
424 
425 void * __weak arch_kexec_kernel_image_load(struct kimage *image)
426 {
427         return ERR_PTR(-ENOEXEC);
428 }
429 
430 void __weak arch_kimage_file_post_load_cleanup(struct kimage *image)
431 {
432 }
433 
434 int __weak arch_kexec_kernel_verify_sig(struct kimage *image, void *buf,
435                                         unsigned long buf_len)
436 {
437         return -EKEYREJECTED;
438 }
439 
440 /* Apply relocations of type RELA */
441 int __weak
442 arch_kexec_apply_relocations_add(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
443                                  unsigned int relsec)
444 {
445         pr_err("RELA relocation unsupported.\n");
446         return -ENOEXEC;
447 }
448 
449 /* Apply relocations of type REL */
450 int __weak
451 arch_kexec_apply_relocations(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
452                              unsigned int relsec)
453 {
454         pr_err("REL relocation unsupported.\n");
455         return -ENOEXEC;
456 }
457 
458 /*
459  * Free up memory used by kernel, initrd, and command line. This is temporary
460  * memory allocation which is not needed any more after these buffers have
461  * been loaded into separate segments and have been copied elsewhere.
462  */
463 static void kimage_file_post_load_cleanup(struct kimage *image)
464 {
465         struct purgatory_info *pi = &image->purgatory_info;
466 
467         vfree(image->kernel_buf);
468         image->kernel_buf = NULL;
469 
470         vfree(image->initrd_buf);
471         image->initrd_buf = NULL;
472 
473         kfree(image->cmdline_buf);
474         image->cmdline_buf = NULL;
475 
476         vfree(pi->purgatory_buf);
477         pi->purgatory_buf = NULL;
478 
479         vfree(pi->sechdrs);
480         pi->sechdrs = NULL;
481 
482         /* See if architecture has anything to cleanup post load */
483         arch_kimage_file_post_load_cleanup(image);
484 
485         /*
486          * Above call should have called into bootloader to free up
487          * any data stored in kimage->image_loader_data. It should
488          * be ok now to free it up.
489          */
490         kfree(image->image_loader_data);
491         image->image_loader_data = NULL;
492 }
493 
494 /*
495  * In file mode list of segments is prepared by kernel. Copy relevant
496  * data from user space, do error checking, prepare segment list
497  */
498 static int
499 kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd,
500                              const char __user *cmdline_ptr,
501                              unsigned long cmdline_len, unsigned flags)
502 {
503         int ret = 0;
504         void *ldata;
505 
506         ret = copy_file_from_fd(kernel_fd, &image->kernel_buf,
507                                 &image->kernel_buf_len);
508         if (ret)
509                 return ret;
510 
511         /* Call arch image probe handlers */
512         ret = arch_kexec_kernel_image_probe(image, image->kernel_buf,
513                                             image->kernel_buf_len);
514 
515         if (ret)
516                 goto out;
517 
518 #ifdef CONFIG_KEXEC_VERIFY_SIG
519         ret = arch_kexec_kernel_verify_sig(image, image->kernel_buf,
520                                            image->kernel_buf_len);
521         if (ret) {
522                 pr_debug("kernel signature verification failed.\n");
523                 goto out;
524         }
525         pr_debug("kernel signature verification successful.\n");
526 #endif
527         /* It is possible that there no initramfs is being loaded */
528         if (!(flags & KEXEC_FILE_NO_INITRAMFS)) {
529                 ret = copy_file_from_fd(initrd_fd, &image->initrd_buf,
530                                         &image->initrd_buf_len);
531                 if (ret)
532                         goto out;
533         }
534 
535         if (cmdline_len) {
536                 image->cmdline_buf = kzalloc(cmdline_len, GFP_KERNEL);
537                 if (!image->cmdline_buf) {
538                         ret = -ENOMEM;
539                         goto out;
540                 }
541 
542                 ret = copy_from_user(image->cmdline_buf, cmdline_ptr,
543                                      cmdline_len);
544                 if (ret) {
545                         ret = -EFAULT;
546                         goto out;
547                 }
548 
549                 image->cmdline_buf_len = cmdline_len;
550 
551                 /* command line should be a string with last byte null */
552                 if (image->cmdline_buf[cmdline_len - 1] != '\0') {
553                         ret = -EINVAL;
554                         goto out;
555                 }
556         }
557 
558         /* Call arch image load handlers */
559         ldata = arch_kexec_kernel_image_load(image);
560 
561         if (IS_ERR(ldata)) {
562                 ret = PTR_ERR(ldata);
563                 goto out;
564         }
565 
566         image->image_loader_data = ldata;
567 out:
568         /* In case of error, free up all allocated memory in this function */
569         if (ret)
570                 kimage_file_post_load_cleanup(image);
571         return ret;
572 }
573 
574 static int
575 kimage_file_alloc_init(struct kimage **rimage, int kernel_fd,
576                        int initrd_fd, const char __user *cmdline_ptr,
577                        unsigned long cmdline_len, unsigned long flags)
578 {
579         int ret;
580         struct kimage *image;
581         bool kexec_on_panic = flags & KEXEC_FILE_ON_CRASH;
582 
583         image = do_kimage_alloc_init();
584         if (!image)
585                 return -ENOMEM;
586 
587         image->file_mode = 1;
588 
589         if (kexec_on_panic) {
590                 /* Enable special crash kernel control page alloc policy. */
591                 image->control_page = crashk_res.start;
592                 image->type = KEXEC_TYPE_CRASH;
593         }
594 
595         ret = kimage_file_prepare_segments(image, kernel_fd, initrd_fd,
596                                            cmdline_ptr, cmdline_len, flags);
597         if (ret)
598                 goto out_free_image;
599 
600         ret = sanity_check_segment_list(image);
601         if (ret)
602                 goto out_free_post_load_bufs;
603 
604         ret = -ENOMEM;
605         image->control_code_page = kimage_alloc_control_pages(image,
606                                            get_order(KEXEC_CONTROL_PAGE_SIZE));
607         if (!image->control_code_page) {
608                 pr_err("Could not allocate control_code_buffer\n");
609                 goto out_free_post_load_bufs;
610         }
611 
612         if (!kexec_on_panic) {
613                 image->swap_page = kimage_alloc_control_pages(image, 0);
614                 if (!image->swap_page) {
615                         pr_err("Could not allocate swap buffer\n");
616                         goto out_free_control_pages;
617                 }
618         }
619 
620         *rimage = image;
621         return 0;
622 out_free_control_pages:
623         kimage_free_page_list(&image->control_pages);
624 out_free_post_load_bufs:
625         kimage_file_post_load_cleanup(image);
626 out_free_image:
627         kfree(image);
628         return ret;
629 }
630 #else /* CONFIG_KEXEC_FILE */
631 static inline void kimage_file_post_load_cleanup(struct kimage *image) { }
632 #endif /* CONFIG_KEXEC_FILE */
633 
634 static int kimage_is_destination_range(struct kimage *image,
635                                         unsigned long start,
636                                         unsigned long end)
637 {
638         unsigned long i;
639 
640         for (i = 0; i < image->nr_segments; i++) {
641                 unsigned long mstart, mend;
642 
643                 mstart = image->segment[i].mem;
644                 mend = mstart + image->segment[i].memsz;
645                 if ((end > mstart) && (start < mend))
646                         return 1;
647         }
648 
649         return 0;
650 }
651 
652 static struct page *kimage_alloc_pages(gfp_t gfp_mask, unsigned int order)
653 {
654         struct page *pages;
655 
656         pages = alloc_pages(gfp_mask, order);
657         if (pages) {
658                 unsigned int count, i;
659                 pages->mapping = NULL;
660                 set_page_private(pages, order);
661                 count = 1 << order;
662                 for (i = 0; i < count; i++)
663                         SetPageReserved(pages + i);
664         }
665 
666         return pages;
667 }
668 
669 static void kimage_free_pages(struct page *page)
670 {
671         unsigned int order, count, i;
672 
673         order = page_private(page);
674         count = 1 << order;
675         for (i = 0; i < count; i++)
676                 ClearPageReserved(page + i);
677         __free_pages(page, order);
678 }
679 
680 static void kimage_free_page_list(struct list_head *list)
681 {
682         struct list_head *pos, *next;
683 
684         list_for_each_safe(pos, next, list) {
685                 struct page *page;
686 
687                 page = list_entry(pos, struct page, lru);
688                 list_del(&page->lru);
689                 kimage_free_pages(page);
690         }
691 }
692 
693 static struct page *kimage_alloc_normal_control_pages(struct kimage *image,
694                                                         unsigned int order)
695 {
696         /* Control pages are special, they are the intermediaries
697          * that are needed while we copy the rest of the pages
698          * to their final resting place.  As such they must
699          * not conflict with either the destination addresses
700          * or memory the kernel is already using.
701          *
702          * The only case where we really need more than one of
703          * these are for architectures where we cannot disable
704          * the MMU and must instead generate an identity mapped
705          * page table for all of the memory.
706          *
707          * At worst this runs in O(N) of the image size.
708          */
709         struct list_head extra_pages;
710         struct page *pages;
711         unsigned int count;
712 
713         count = 1 << order;
714         INIT_LIST_HEAD(&extra_pages);
715 
716         /* Loop while I can allocate a page and the page allocated
717          * is a destination page.
718          */
719         do {
720                 unsigned long pfn, epfn, addr, eaddr;
721 
722                 pages = kimage_alloc_pages(KEXEC_CONTROL_MEMORY_GFP, order);
723                 if (!pages)
724                         break;
725                 pfn   = page_to_pfn(pages);
726                 epfn  = pfn + count;
727                 addr  = pfn << PAGE_SHIFT;
728                 eaddr = epfn << PAGE_SHIFT;
729                 if ((epfn >= (KEXEC_CONTROL_MEMORY_LIMIT >> PAGE_SHIFT)) ||
730                               kimage_is_destination_range(image, addr, eaddr)) {
731                         list_add(&pages->lru, &extra_pages);
732                         pages = NULL;
733                 }
734         } while (!pages);
735 
736         if (pages) {
737                 /* Remember the allocated page... */
738                 list_add(&pages->lru, &image->control_pages);
739 
740                 /* Because the page is already in it's destination
741                  * location we will never allocate another page at
742                  * that address.  Therefore kimage_alloc_pages
743                  * will not return it (again) and we don't need
744                  * to give it an entry in image->segment[].
745                  */
746         }
747         /* Deal with the destination pages I have inadvertently allocated.
748          *
749          * Ideally I would convert multi-page allocations into single
750          * page allocations, and add everything to image->dest_pages.
751          *
752          * For now it is simpler to just free the pages.
753          */
754         kimage_free_page_list(&extra_pages);
755 
756         return pages;
757 }
758 
759 static struct page *kimage_alloc_crash_control_pages(struct kimage *image,
760                                                       unsigned int order)
761 {
762         /* Control pages are special, they are the intermediaries
763          * that are needed while we copy the rest of the pages
764          * to their final resting place.  As such they must
765          * not conflict with either the destination addresses
766          * or memory the kernel is already using.
767          *
768          * Control pages are also the only pags we must allocate
769          * when loading a crash kernel.  All of the other pages
770          * are specified by the segments and we just memcpy
771          * into them directly.
772          *
773          * The only case where we really need more than one of
774          * these are for architectures where we cannot disable
775          * the MMU and must instead generate an identity mapped
776          * page table for all of the memory.
777          *
778          * Given the low demand this implements a very simple
779          * allocator that finds the first hole of the appropriate
780          * size in the reserved memory region, and allocates all
781          * of the memory up to and including the hole.
782          */
783         unsigned long hole_start, hole_end, size;
784         struct page *pages;
785 
786         pages = NULL;
787         size = (1 << order) << PAGE_SHIFT;
788         hole_start = (image->control_page + (size - 1)) & ~(size - 1);
789         hole_end   = hole_start + size - 1;
790         while (hole_end <= crashk_res.end) {
791                 unsigned long i;
792 
793                 if (hole_end > KEXEC_CRASH_CONTROL_MEMORY_LIMIT)
794                         break;
795                 /* See if I overlap any of the segments */
796                 for (i = 0; i < image->nr_segments; i++) {
797                         unsigned long mstart, mend;
798 
799                         mstart = image->segment[i].mem;
800                         mend   = mstart + image->segment[i].memsz - 1;
801                         if ((hole_end >= mstart) && (hole_start <= mend)) {
802                                 /* Advance the hole to the end of the segment */
803                                 hole_start = (mend + (size - 1)) & ~(size - 1);
804                                 hole_end   = hole_start + size - 1;
805                                 break;
806                         }
807                 }
808                 /* If I don't overlap any segments I have found my hole! */
809                 if (i == image->nr_segments) {
810                         pages = pfn_to_page(hole_start >> PAGE_SHIFT);
811                         break;
812                 }
813         }
814         if (pages)
815                 image->control_page = hole_end;
816 
817         return pages;
818 }
819 
820 
821 struct page *kimage_alloc_control_pages(struct kimage *image,
822                                          unsigned int order)
823 {
824         struct page *pages = NULL;
825 
826         switch (image->type) {
827         case KEXEC_TYPE_DEFAULT:
828                 pages = kimage_alloc_normal_control_pages(image, order);
829                 break;
830         case KEXEC_TYPE_CRASH:
831                 pages = kimage_alloc_crash_control_pages(image, order);
832                 break;
833         }
834 
835         return pages;
836 }
837 
838 static int kimage_add_entry(struct kimage *image, kimage_entry_t entry)
839 {
840         if (*image->entry != 0)
841                 image->entry++;
842 
843         if (image->entry == image->last_entry) {
844                 kimage_entry_t *ind_page;
845                 struct page *page;
846 
847                 page = kimage_alloc_page(image, GFP_KERNEL, KIMAGE_NO_DEST);
848                 if (!page)
849                         return -ENOMEM;
850 
851                 ind_page = page_address(page);
852                 *image->entry = virt_to_phys(ind_page) | IND_INDIRECTION;
853                 image->entry = ind_page;
854                 image->last_entry = ind_page +
855                                       ((PAGE_SIZE/sizeof(kimage_entry_t)) - 1);
856         }
857         *image->entry = entry;
858         image->entry++;
859         *image->entry = 0;
860 
861         return 0;
862 }
863 
864 static int kimage_set_destination(struct kimage *image,
865                                    unsigned long destination)
866 {
867         int result;
868 
869         destination &= PAGE_MASK;
870         result = kimage_add_entry(image, destination | IND_DESTINATION);
871 
872         return result;
873 }
874 
875 
876 static int kimage_add_page(struct kimage *image, unsigned long page)
877 {
878         int result;
879 
880         page &= PAGE_MASK;
881         result = kimage_add_entry(image, page | IND_SOURCE);
882 
883         return result;
884 }
885 
886 
887 static void kimage_free_extra_pages(struct kimage *image)
888 {
889         /* Walk through and free any extra destination pages I may have */
890         kimage_free_page_list(&image->dest_pages);
891 
892         /* Walk through and free any unusable pages I have cached */
893         kimage_free_page_list(&image->unusable_pages);
894 
895 }
896 static void kimage_terminate(struct kimage *image)
897 {
898         if (*image->entry != 0)
899                 image->entry++;
900 
901         *image->entry = IND_DONE;
902 }
903 
904 #define for_each_kimage_entry(image, ptr, entry) \
905         for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE); \
906                 ptr = (entry & IND_INDIRECTION) ? \
907                         phys_to_virt((entry & PAGE_MASK)) : ptr + 1)
908 
909 static void kimage_free_entry(kimage_entry_t entry)
910 {
911         struct page *page;
912 
913         page = pfn_to_page(entry >> PAGE_SHIFT);
914         kimage_free_pages(page);
915 }
916 
917 static void kimage_free(struct kimage *image)
918 {
919         kimage_entry_t *ptr, entry;
920         kimage_entry_t ind = 0;
921 
922         if (!image)
923                 return;
924 
925         kimage_free_extra_pages(image);
926         for_each_kimage_entry(image, ptr, entry) {
927                 if (entry & IND_INDIRECTION) {
928                         /* Free the previous indirection page */
929                         if (ind & IND_INDIRECTION)
930                                 kimage_free_entry(ind);
931                         /* Save this indirection page until we are
932                          * done with it.
933                          */
934                         ind = entry;
935                 } else if (entry & IND_SOURCE)
936                         kimage_free_entry(entry);
937         }
938         /* Free the final indirection page */
939         if (ind & IND_INDIRECTION)
940                 kimage_free_entry(ind);
941 
942         /* Handle any machine specific cleanup */
943         machine_kexec_cleanup(image);
944 
945         /* Free the kexec control pages... */
946         kimage_free_page_list(&image->control_pages);
947 
948         /*
949          * Free up any temporary buffers allocated. This might hit if
950          * error occurred much later after buffer allocation.
951          */
952         if (image->file_mode)
953                 kimage_file_post_load_cleanup(image);
954 
955         kfree(image);
956 }
957 
958 static kimage_entry_t *kimage_dst_used(struct kimage *image,
959                                         unsigned long page)
960 {
961         kimage_entry_t *ptr, entry;
962         unsigned long destination = 0;
963 
964         for_each_kimage_entry(image, ptr, entry) {
965                 if (entry & IND_DESTINATION)
966                         destination = entry & PAGE_MASK;
967                 else if (entry & IND_SOURCE) {
968                         if (page == destination)
969                                 return ptr;
970                         destination += PAGE_SIZE;
971                 }
972         }
973 
974         return NULL;
975 }
976 
977 static struct page *kimage_alloc_page(struct kimage *image,
978                                         gfp_t gfp_mask,
979                                         unsigned long destination)
980 {
981         /*
982          * Here we implement safeguards to ensure that a source page
983          * is not copied to its destination page before the data on
984          * the destination page is no longer useful.
985          *
986          * To do this we maintain the invariant that a source page is
987          * either its own destination page, or it is not a
988          * destination page at all.
989          *
990          * That is slightly stronger than required, but the proof
991          * that no problems will not occur is trivial, and the
992          * implementation is simply to verify.
993          *
994          * When allocating all pages normally this algorithm will run
995          * in O(N) time, but in the worst case it will run in O(N^2)
996          * time.   If the runtime is a problem the data structures can
997          * be fixed.
998          */
999         struct page *page;
1000         unsigned long addr;
1001 
1002         /*
1003          * Walk through the list of destination pages, and see if I
1004          * have a match.
1005          */
1006         list_for_each_entry(page, &image->dest_pages, lru) {
1007                 addr = page_to_pfn(page) << PAGE_SHIFT;
1008                 if (addr == destination) {
1009                         list_del(&page->lru);
1010                         return page;
1011                 }
1012         }
1013         page = NULL;
1014         while (1) {
1015                 kimage_entry_t *old;
1016 
1017                 /* Allocate a page, if we run out of memory give up */
1018                 page = kimage_alloc_pages(gfp_mask, 0);
1019                 if (!page)
1020                         return NULL;
1021                 /* If the page cannot be used file it away */
1022                 if (page_to_pfn(page) >
1023                                 (KEXEC_SOURCE_MEMORY_LIMIT >> PAGE_SHIFT)) {
1024                         list_add(&page->lru, &image->unusable_pages);
1025                         continue;
1026                 }
1027                 addr = page_to_pfn(page) << PAGE_SHIFT;
1028 
1029                 /* If it is the destination page we want use it */
1030                 if (addr == destination)
1031                         break;
1032 
1033                 /* If the page is not a destination page use it */
1034                 if (!kimage_is_destination_range(image, addr,
1035                                                   addr + PAGE_SIZE))
1036                         break;
1037 
1038                 /*
1039                  * I know that the page is someones destination page.
1040                  * See if there is already a source page for this
1041                  * destination page.  And if so swap the source pages.
1042                  */
1043                 old = kimage_dst_used(image, addr);
1044                 if (old) {
1045                         /* If so move it */
1046                         unsigned long old_addr;
1047                         struct page *old_page;
1048 
1049                         old_addr = *old & PAGE_MASK;
1050                         old_page = pfn_to_page(old_addr >> PAGE_SHIFT);
1051                         copy_highpage(page, old_page);
1052                         *old = addr | (*old & ~PAGE_MASK);
1053 
1054                         /* The old page I have found cannot be a
1055                          * destination page, so return it if it's
1056                          * gfp_flags honor the ones passed in.
1057                          */
1058                         if (!(gfp_mask & __GFP_HIGHMEM) &&
1059                             PageHighMem(old_page)) {
1060                                 kimage_free_pages(old_page);
1061                                 continue;
1062                         }
1063                         addr = old_addr;
1064                         page = old_page;
1065                         break;
1066                 } else {
1067                         /* Place the page on the destination list I
1068                          * will use it later.
1069                          */
1070                         list_add(&page->lru, &image->dest_pages);
1071                 }
1072         }
1073 
1074         return page;
1075 }
1076 
1077 static int kimage_load_normal_segment(struct kimage *image,
1078                                          struct kexec_segment *segment)
1079 {
1080         unsigned long maddr;
1081         size_t ubytes, mbytes;
1082         int result;
1083         unsigned char __user *buf = NULL;
1084         unsigned char *kbuf = NULL;
1085 
1086         result = 0;
1087         if (image->file_mode)
1088                 kbuf = segment->kbuf;
1089         else
1090                 buf = segment->buf;
1091         ubytes = segment->bufsz;
1092         mbytes = segment->memsz;
1093         maddr = segment->mem;
1094 
1095         result = kimage_set_destination(image, maddr);
1096         if (result < 0)
1097                 goto out;
1098 
1099         while (mbytes) {
1100                 struct page *page;
1101                 char *ptr;
1102                 size_t uchunk, mchunk;
1103 
1104                 page = kimage_alloc_page(image, GFP_HIGHUSER, maddr);
1105                 if (!page) {
1106                         result  = -ENOMEM;
1107                         goto out;
1108                 }
1109                 result = kimage_add_page(image, page_to_pfn(page)
1110                                                                 << PAGE_SHIFT);
1111                 if (result < 0)
1112                         goto out;
1113 
1114                 ptr = kmap(page);
1115                 /* Start with a clear page */
1116                 clear_page(ptr);
1117                 ptr += maddr & ~PAGE_MASK;
1118                 mchunk = min_t(size_t, mbytes,
1119                                 PAGE_SIZE - (maddr & ~PAGE_MASK));
1120                 uchunk = min(ubytes, mchunk);
1121 
1122                 /* For file based kexec, source pages are in kernel memory */
1123                 if (image->file_mode)
1124                         memcpy(ptr, kbuf, uchunk);
1125                 else
1126                         result = copy_from_user(ptr, buf, uchunk);
1127                 kunmap(page);
1128                 if (result) {
1129                         result = -EFAULT;
1130                         goto out;
1131                 }
1132                 ubytes -= uchunk;
1133                 maddr  += mchunk;
1134                 if (image->file_mode)
1135                         kbuf += mchunk;
1136                 else
1137                         buf += mchunk;
1138                 mbytes -= mchunk;
1139         }
1140 out:
1141         return result;
1142 }
1143 
1144 static int kimage_load_crash_segment(struct kimage *image,
1145                                         struct kexec_segment *segment)
1146 {
1147         /* For crash dumps kernels we simply copy the data from
1148          * user space to it's destination.
1149          * We do things a page at a time for the sake of kmap.
1150          */
1151         unsigned long maddr;
1152         size_t ubytes, mbytes;
1153         int result;
1154         unsigned char __user *buf = NULL;
1155         unsigned char *kbuf = NULL;
1156 
1157         result = 0;
1158         if (image->file_mode)
1159                 kbuf = segment->kbuf;
1160         else
1161                 buf = segment->buf;
1162         ubytes = segment->bufsz;
1163         mbytes = segment->memsz;
1164         maddr = segment->mem;
1165         while (mbytes) {
1166                 struct page *page;
1167                 char *ptr;
1168                 size_t uchunk, mchunk;
1169 
1170                 page = pfn_to_page(maddr >> PAGE_SHIFT);
1171                 if (!page) {
1172                         result  = -ENOMEM;
1173                         goto out;
1174                 }
1175                 ptr = kmap(page);
1176                 ptr += maddr & ~PAGE_MASK;
1177                 mchunk = min_t(size_t, mbytes,
1178                                 PAGE_SIZE - (maddr & ~PAGE_MASK));
1179                 uchunk = min(ubytes, mchunk);
1180                 if (mchunk > uchunk) {
1181                         /* Zero the trailing part of the page */
1182                         memset(ptr + uchunk, 0, mchunk - uchunk);
1183                 }
1184 
1185                 /* For file based kexec, source pages are in kernel memory */
1186                 if (image->file_mode)
1187                         memcpy(ptr, kbuf, uchunk);
1188                 else
1189                         result = copy_from_user(ptr, buf, uchunk);
1190                 kexec_flush_icache_page(page);
1191                 kunmap(page);
1192                 if (result) {
1193                         result = -EFAULT;
1194                         goto out;
1195                 }
1196                 ubytes -= uchunk;
1197                 maddr  += mchunk;
1198                 if (image->file_mode)
1199                         kbuf += mchunk;
1200                 else
1201                         buf += mchunk;
1202                 mbytes -= mchunk;
1203         }
1204 out:
1205         return result;
1206 }
1207 
1208 static int kimage_load_segment(struct kimage *image,
1209                                 struct kexec_segment *segment)
1210 {
1211         int result = -ENOMEM;
1212 
1213         switch (image->type) {
1214         case KEXEC_TYPE_DEFAULT:
1215                 result = kimage_load_normal_segment(image, segment);
1216                 break;
1217         case KEXEC_TYPE_CRASH:
1218                 result = kimage_load_crash_segment(image, segment);
1219                 break;
1220         }
1221 
1222         return result;
1223 }
1224 
1225 /*
1226  * Exec Kernel system call: for obvious reasons only root may call it.
1227  *
1228  * This call breaks up into three pieces.
1229  * - A generic part which loads the new kernel from the current
1230  *   address space, and very carefully places the data in the
1231  *   allocated pages.
1232  *
1233  * - A generic part that interacts with the kernel and tells all of
1234  *   the devices to shut down.  Preventing on-going dmas, and placing
1235  *   the devices in a consistent state so a later kernel can
1236  *   reinitialize them.
1237  *
1238  * - A machine specific part that includes the syscall number
1239  *   and then copies the image to it's final destination.  And
1240  *   jumps into the image at entry.
1241  *
1242  * kexec does not sync, or unmount filesystems so if you need
1243  * that to happen you need to do that yourself.
1244  */
1245 struct kimage *kexec_image;
1246 struct kimage *kexec_crash_image;
1247 int kexec_load_disabled;
1248 
1249 static DEFINE_MUTEX(kexec_mutex);
1250 
1251 SYSCALL_DEFINE4(kexec_load, unsigned long, entry, unsigned long, nr_segments,
1252                 struct kexec_segment __user *, segments, unsigned long, flags)
1253 {
1254         struct kimage **dest_image, *image;
1255         int result;
1256 
1257         /* We only trust the superuser with rebooting the system. */
1258         if (!capable(CAP_SYS_BOOT) || kexec_load_disabled)
1259                 return -EPERM;
1260         if (!ccs_capable(CCS_SYS_KEXEC_LOAD))
1261                 return -EPERM;
1262 
1263         /*
1264          * Verify we have a legal set of flags
1265          * This leaves us room for future extensions.
1266          */
1267         if ((flags & KEXEC_FLAGS) != (flags & ~KEXEC_ARCH_MASK))
1268                 return -EINVAL;
1269 
1270         /* Verify we are on the appropriate architecture */
1271         if (((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH) &&
1272                 ((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH_DEFAULT))
1273                 return -EINVAL;
1274 
1275         /* Put an artificial cap on the number
1276          * of segments passed to kexec_load.
1277          */
1278         if (nr_segments > KEXEC_SEGMENT_MAX)
1279                 return -EINVAL;
1280 
1281         image = NULL;
1282         result = 0;
1283 
1284         /* Because we write directly to the reserved memory
1285          * region when loading crash kernels we need a mutex here to
1286          * prevent multiple crash  kernels from attempting to load
1287          * simultaneously, and to prevent a crash kernel from loading
1288          * over the top of a in use crash kernel.
1289          *
1290          * KISS: always take the mutex.
1291          */
1292         if (!mutex_trylock(&kexec_mutex))
1293                 return -EBUSY;
1294 
1295         dest_image = &kexec_image;
1296         if (flags & KEXEC_ON_CRASH)
1297                 dest_image = &kexec_crash_image;
1298         if (nr_segments > 0) {
1299                 unsigned long i;
1300 
1301                 if (flags & KEXEC_ON_CRASH) {
1302                         /*
1303                          * Loading another kernel to switch to if this one
1304                          * crashes.  Free any current crash dump kernel before
1305                          * we corrupt it.
1306                          */
1307 
1308                         kimage_free(xchg(&kexec_crash_image, NULL));
1309                         result = kimage_alloc_init(&image, entry, nr_segments,
1310                                                    segments, flags);
1311                         crash_map_reserved_pages();
1312                 } else {
1313                         /* Loading another kernel to reboot into. */
1314 
1315                         result = kimage_alloc_init(&image, entry, nr_segments,
1316                                                    segments, flags);
1317                 }
1318                 if (result)
1319                         goto out;
1320 
1321                 if (flags & KEXEC_PRESERVE_CONTEXT)
1322                         image->preserve_context = 1;
1323                 result = machine_kexec_prepare(image);
1324                 if (result)
1325                         goto out;
1326 
1327                 for (i = 0; i < nr_segments; i++) {
1328                         result = kimage_load_segment(image, &image->segment[i]);
1329                         if (result)
1330                                 goto out;
1331                 }
1332                 kimage_terminate(image);
1333                 if (flags & KEXEC_ON_CRASH)
1334                         crash_unmap_reserved_pages();
1335         }
1336         /* Install the new kernel, and  Uninstall the old */
1337         image = xchg(dest_image, image);
1338 
1339 out:
1340         mutex_unlock(&kexec_mutex);
1341         kimage_free(image);
1342 
1343         return result;
1344 }
1345 
1346 /*
1347  * Add and remove page tables for crashkernel memory
1348  *
1349  * Provide an empty default implementation here -- architecture
1350  * code may override this
1351  */
1352 void __weak crash_map_reserved_pages(void)
1353 {}
1354 
1355 void __weak crash_unmap_reserved_pages(void)
1356 {}
1357 
1358 #ifdef CONFIG_COMPAT
1359 COMPAT_SYSCALL_DEFINE4(kexec_load, compat_ulong_t, entry,
1360                        compat_ulong_t, nr_segments,
1361                        struct compat_kexec_segment __user *, segments,
1362                        compat_ulong_t, flags)
1363 {
1364         struct compat_kexec_segment in;
1365         struct kexec_segment out, __user *ksegments;
1366         unsigned long i, result;
1367 
1368         /* Don't allow clients that don't understand the native
1369          * architecture to do anything.
1370          */
1371         if ((flags & KEXEC_ARCH_MASK) == KEXEC_ARCH_DEFAULT)
1372                 return -EINVAL;
1373 
1374         if (nr_segments > KEXEC_SEGMENT_MAX)
1375                 return -EINVAL;
1376 
1377         ksegments = compat_alloc_user_space(nr_segments * sizeof(out));
1378         for (i = 0; i < nr_segments; i++) {
1379                 result = copy_from_user(&in, &segments[i], sizeof(in));
1380                 if (result)
1381                         return -EFAULT;
1382 
1383                 out.buf   = compat_ptr(in.buf);
1384                 out.bufsz = in.bufsz;
1385                 out.mem   = in.mem;
1386                 out.memsz = in.memsz;
1387 
1388                 result = copy_to_user(&ksegments[i], &out, sizeof(out));
1389                 if (result)
1390                         return -EFAULT;
1391         }
1392 
1393         return sys_kexec_load(entry, nr_segments, ksegments, flags);
1394 }
1395 #endif
1396 
1397 #ifdef CONFIG_KEXEC_FILE
1398 SYSCALL_DEFINE5(kexec_file_load, int, kernel_fd, int, initrd_fd,
1399                 unsigned long, cmdline_len, const char __user *, cmdline_ptr,
1400                 unsigned long, flags)
1401 {
1402         int ret = 0, i;
1403         struct kimage **dest_image, *image;
1404 
1405         /* We only trust the superuser with rebooting the system. */
1406         if (!capable(CAP_SYS_BOOT) || kexec_load_disabled)
1407                 return -EPERM;
1408 
1409         /* Make sure we have a legal set of flags */
1410         if (flags != (flags & KEXEC_FILE_FLAGS))
1411                 return -EINVAL;
1412 
1413         image = NULL;
1414 
1415         if (!mutex_trylock(&kexec_mutex))
1416                 return -EBUSY;
1417 
1418         dest_image = &kexec_image;
1419         if (flags & KEXEC_FILE_ON_CRASH)
1420                 dest_image = &kexec_crash_image;
1421 
1422         if (flags & KEXEC_FILE_UNLOAD)
1423                 goto exchange;
1424 
1425         /*
1426          * In case of crash, new kernel gets loaded in reserved region. It is
1427          * same memory where old crash kernel might be loaded. Free any
1428          * current crash dump kernel before we corrupt it.
1429          */
1430         if (flags & KEXEC_FILE_ON_CRASH)
1431                 kimage_free(xchg(&kexec_crash_image, NULL));
1432 
1433         ret = kimage_file_alloc_init(&image, kernel_fd, initrd_fd, cmdline_ptr,
1434                                      cmdline_len, flags);
1435         if (ret)
1436                 goto out;
1437 
1438         ret = machine_kexec_prepare(image);
1439         if (ret)
1440                 goto out;
1441 
1442         ret = kexec_calculate_store_digests(image);
1443         if (ret)
1444                 goto out;
1445 
1446         for (i = 0; i < image->nr_segments; i++) {
1447                 struct kexec_segment *ksegment;
1448 
1449                 ksegment = &image->segment[i];
1450                 pr_debug("Loading segment %d: buf=0x%p bufsz=0x%zx mem=0x%lx memsz=0x%zx\n",
1451                          i, ksegment->buf, ksegment->bufsz, ksegment->mem,
1452                          ksegment->memsz);
1453 
1454                 ret = kimage_load_segment(image, &image->segment[i]);
1455                 if (ret)
1456                         goto out;
1457         }
1458 
1459         kimage_terminate(image);
1460 
1461         /*
1462          * Free up any temporary buffers allocated which are not needed
1463          * after image has been loaded
1464          */
1465         kimage_file_post_load_cleanup(image);
1466 exchange:
1467         image = xchg(dest_image, image);
1468 out:
1469         mutex_unlock(&kexec_mutex);
1470         kimage_free(image);
1471         return ret;
1472 }
1473 
1474 #endif /* CONFIG_KEXEC_FILE */
1475 
1476 void crash_kexec(struct pt_regs *regs)
1477 {
1478         /* Take the kexec_mutex here to prevent sys_kexec_load
1479          * running on one cpu from replacing the crash kernel
1480          * we are using after a panic on a different cpu.
1481          *
1482          * If the crash kernel was not located in a fixed area
1483          * of memory the xchg(&kexec_crash_image) would be
1484          * sufficient.  But since I reuse the memory...
1485          */
1486         if (mutex_trylock(&kexec_mutex)) {
1487                 if (kexec_crash_image) {
1488                         struct pt_regs fixed_regs;
1489 
1490                         crash_setup_regs(&fixed_regs, regs);
1491                         crash_save_vmcoreinfo();
1492                         machine_crash_shutdown(&fixed_regs);
1493                         machine_kexec(kexec_crash_image);
1494                 }
1495                 mutex_unlock(&kexec_mutex);
1496         }
1497 }
1498 
1499 size_t crash_get_memory_size(void)
1500 {
1501         size_t size = 0;
1502         mutex_lock(&kexec_mutex);
1503         if (crashk_res.end != crashk_res.start)
1504                 size = resource_size(&crashk_res);
1505         mutex_unlock(&kexec_mutex);
1506         return size;
1507 }
1508 
1509 void __weak crash_free_reserved_phys_range(unsigned long begin,
1510                                            unsigned long end)
1511 {
1512         unsigned long addr;
1513 
1514         for (addr = begin; addr < end; addr += PAGE_SIZE)
1515                 free_reserved_page(pfn_to_page(addr >> PAGE_SHIFT));
1516 }
1517 
1518 int crash_shrink_memory(unsigned long new_size)
1519 {
1520         int ret = 0;
1521         unsigned long start, end;
1522         unsigned long old_size;
1523         struct resource *ram_res;
1524 
1525         mutex_lock(&kexec_mutex);
1526 
1527         if (kexec_crash_image) {
1528                 ret = -ENOENT;
1529                 goto unlock;
1530         }
1531         start = crashk_res.start;
1532         end = crashk_res.end;
1533         old_size = (end == 0) ? 0 : end - start + 1;
1534         if (new_size >= old_size) {
1535                 ret = (new_size == old_size) ? 0 : -EINVAL;
1536                 goto unlock;
1537         }
1538 
1539         ram_res = kzalloc(sizeof(*ram_res), GFP_KERNEL);
1540         if (!ram_res) {
1541                 ret = -ENOMEM;
1542                 goto unlock;
1543         }
1544 
1545         start = roundup(start, KEXEC_CRASH_MEM_ALIGN);
1546         end = roundup(start + new_size, KEXEC_CRASH_MEM_ALIGN);
1547 
1548         crash_map_reserved_pages();
1549         crash_free_reserved_phys_range(end, crashk_res.end);
1550 
1551         if ((start == end) && (crashk_res.parent != NULL))
1552                 release_resource(&crashk_res);
1553 
1554         ram_res->start = end;
1555         ram_res->end = crashk_res.end;
1556         ram_res->flags = IORESOURCE_BUSY | IORESOURCE_MEM;
1557         ram_res->name = "System RAM";
1558 
1559         crashk_res.end = end - 1;
1560 
1561         insert_resource(&iomem_resource, ram_res);
1562         crash_unmap_reserved_pages();
1563 
1564 unlock:
1565         mutex_unlock(&kexec_mutex);
1566         return ret;
1567 }
1568 
1569 static u32 *append_elf_note(u32 *buf, char *name, unsigned type, void *data,
1570                             size_t data_len)
1571 {
1572         struct elf_note note;
1573 
1574         note.n_namesz = strlen(name) + 1;
1575         note.n_descsz = data_len;
1576         note.n_type   = type;
1577         memcpy(buf, &note, sizeof(note));
1578         buf += (sizeof(note) + 3)/4;
1579         memcpy(buf, name, note.n_namesz);
1580         buf += (note.n_namesz + 3)/4;
1581         memcpy(buf, data, note.n_descsz);
1582         buf += (note.n_descsz + 3)/4;
1583 
1584         return buf;
1585 }
1586 
1587 static void final_note(u32 *buf)
1588 {
1589         struct elf_note note;
1590 
1591         note.n_namesz = 0;
1592         note.n_descsz = 0;
1593         note.n_type   = 0;
1594         memcpy(buf, &note, sizeof(note));
1595 }
1596 
1597 void crash_save_cpu(struct pt_regs *regs, int cpu)
1598 {
1599         struct elf_prstatus prstatus;
1600         u32 *buf;
1601 
1602         if ((cpu < 0) || (cpu >= nr_cpu_ids))
1603                 return;
1604 
1605         /* Using ELF notes here is opportunistic.
1606          * I need a well defined structure format
1607          * for the data I pass, and I need tags
1608          * on the data to indicate what information I have
1609          * squirrelled away.  ELF notes happen to provide
1610          * all of that, so there is no need to invent something new.
1611          */
1612         buf = (u32 *)per_cpu_ptr(crash_notes, cpu);
1613         if (!buf)
1614                 return;
1615         memset(&prstatus, 0, sizeof(prstatus));
1616         prstatus.pr_pid = current->pid;
1617         elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
1618         buf = append_elf_note(buf, KEXEC_CORE_NOTE_NAME, NT_PRSTATUS,
1619                               &prstatus, sizeof(prstatus));
1620         final_note(buf);
1621 }
1622 
1623 static int __init crash_notes_memory_init(void)
1624 {
1625         /* Allocate memory for saving cpu registers. */
1626         crash_notes = alloc_percpu(note_buf_t);
1627         if (!crash_notes) {
1628                 pr_warn("Kexec: Memory allocation for saving cpu register states failed\n");
1629                 return -ENOMEM;
1630         }
1631         return 0;
1632 }
1633 subsys_initcall(crash_notes_memory_init);
1634 
1635 
1636 /*
1637  * parsing the "crashkernel" commandline
1638  *
1639  * this code is intended to be called from architecture specific code
1640  */
1641 
1642 
1643 /*
1644  * This function parses command lines in the format
1645  *
1646  *   crashkernel=ramsize-range:size[,...][@offset]
1647  *
1648  * The function returns 0 on success and -EINVAL on failure.
1649  */
1650 static int __init parse_crashkernel_mem(char *cmdline,
1651                                         unsigned long long system_ram,
1652                                         unsigned long long *crash_size,
1653                                         unsigned long long *crash_base)
1654 {
1655         char *cur = cmdline, *tmp;
1656 
1657         /* for each entry of the comma-separated list */
1658         do {
1659                 unsigned long long start, end = ULLONG_MAX, size;
1660 
1661                 /* get the start of the range */
1662                 start = memparse(cur, &tmp);
1663                 if (cur == tmp) {
1664                         pr_warn("crashkernel: Memory value expected\n");
1665                         return -EINVAL;
1666                 }
1667                 cur = tmp;
1668                 if (*cur != '-') {
1669                         pr_warn("crashkernel: '-' expected\n");
1670                         return -EINVAL;
1671                 }
1672                 cur++;
1673 
1674                 /* if no ':' is here, than we read the end */
1675                 if (*cur != ':') {
1676                         end = memparse(cur, &tmp);
1677                         if (cur == tmp) {
1678                                 pr_warn("crashkernel: Memory value expected\n");
1679                                 return -EINVAL;
1680                         }
1681                         cur = tmp;
1682                         if (end <= start) {
1683                                 pr_warn("crashkernel: end <= start\n");
1684                                 return -EINVAL;
1685                         }
1686                 }
1687 
1688                 if (*cur != ':') {
1689                         pr_warn("crashkernel: ':' expected\n");
1690                         return -EINVAL;
1691                 }
1692                 cur++;
1693 
1694                 size = memparse(cur, &tmp);
1695                 if (cur == tmp) {
1696                         pr_warn("Memory value expected\n");
1697                         return -EINVAL;
1698                 }
1699                 cur = tmp;
1700                 if (size >= system_ram) {
1701                         pr_warn("crashkernel: invalid size\n");
1702                         return -EINVAL;
1703                 }
1704 
1705                 /* match ? */
1706                 if (system_ram >= start && system_ram < end) {
1707                         *crash_size = size;
1708                         break;
1709                 }
1710         } while (*cur++ == ',');
1711 
1712         if (*crash_size > 0) {
1713                 while (*cur && *cur != ' ' && *cur != '@')
1714                         cur++;
1715                 if (*cur == '@') {
1716                         cur++;
1717                         *crash_base = memparse(cur, &tmp);
1718                         if (cur == tmp) {
1719                                 pr_warn("Memory value expected after '@'\n");
1720                                 return -EINVAL;
1721                         }
1722                 }
1723         }
1724 
1725         return 0;
1726 }
1727 
1728 /*
1729  * That function parses "simple" (old) crashkernel command lines like
1730  *
1731  *      crashkernel=size[@offset]
1732  *
1733  * It returns 0 on success and -EINVAL on failure.
1734  */
1735 static int __init parse_crashkernel_simple(char *cmdline,
1736                                            unsigned long long *crash_size,
1737                                            unsigned long long *crash_base)
1738 {
1739         char *cur = cmdline;
1740 
1741         *crash_size = memparse(cmdline, &cur);
1742         if (cmdline == cur) {
1743                 pr_warn("crashkernel: memory value expected\n");
1744                 return -EINVAL;
1745         }
1746 
1747         if (*cur == '@')
1748                 *crash_base = memparse(cur+1, &cur);
1749         else if (*cur != ' ' && *cur != '\0') {
1750                 pr_warn("crashkernel: unrecognized char\n");
1751                 return -EINVAL;
1752         }
1753 
1754         return 0;
1755 }
1756 
1757 #define SUFFIX_HIGH 0
1758 #define SUFFIX_LOW  1
1759 #define SUFFIX_NULL 2
1760 static __initdata char *suffix_tbl[] = {
1761         [SUFFIX_HIGH] = ",high",
1762         [SUFFIX_LOW]  = ",low",
1763         [SUFFIX_NULL] = NULL,
1764 };
1765 
1766 /*
1767  * That function parses "suffix"  crashkernel command lines like
1768  *
1769  *      crashkernel=size,[high|low]
1770  *
1771  * It returns 0 on success and -EINVAL on failure.
1772  */
1773 static int __init parse_crashkernel_suffix(char *cmdline,
1774                                            unsigned long long   *crash_size,
1775                                            const char *suffix)
1776 {
1777         char *cur = cmdline;
1778 
1779         *crash_size = memparse(cmdline, &cur);
1780         if (cmdline == cur) {
1781                 pr_warn("crashkernel: memory value expected\n");
1782                 return -EINVAL;
1783         }
1784 
1785         /* check with suffix */
1786         if (strncmp(cur, suffix, strlen(suffix))) {
1787                 pr_warn("crashkernel: unrecognized char\n");
1788                 return -EINVAL;
1789         }
1790         cur += strlen(suffix);
1791         if (*cur != ' ' && *cur != '\0') {
1792                 pr_warn("crashkernel: unrecognized char\n");
1793                 return -EINVAL;
1794         }
1795 
1796         return 0;
1797 }
1798 
1799 static __init char *get_last_crashkernel(char *cmdline,
1800                              const char *name,
1801                              const char *suffix)
1802 {
1803         char *p = cmdline, *ck_cmdline = NULL;
1804 
1805         /* find crashkernel and use the last one if there are more */
1806         p = strstr(p, name);
1807         while (p) {
1808                 char *end_p = strchr(p, ' ');
1809                 char *q;
1810 
1811                 if (!end_p)
1812                         end_p = p + strlen(p);
1813 
1814                 if (!suffix) {
1815                         int i;
1816 
1817                         /* skip the one with any known suffix */
1818                         for (i = 0; suffix_tbl[i]; i++) {
1819                                 q = end_p - strlen(suffix_tbl[i]);
1820                                 if (!strncmp(q, suffix_tbl[i],
1821                                              strlen(suffix_tbl[i])))
1822                                         goto next;
1823                         }
1824                         ck_cmdline = p;
1825                 } else {
1826                         q = end_p - strlen(suffix);
1827                         if (!strncmp(q, suffix, strlen(suffix)))
1828                                 ck_cmdline = p;
1829                 }
1830 next:
1831                 p = strstr(p+1, name);
1832         }
1833 
1834         if (!ck_cmdline)
1835                 return NULL;
1836 
1837         return ck_cmdline;
1838 }
1839 
1840 static int __init __parse_crashkernel(char *cmdline,
1841                              unsigned long long system_ram,
1842                              unsigned long long *crash_size,
1843                              unsigned long long *crash_base,
1844                              const char *name,
1845                              const char *suffix)
1846 {
1847         char    *first_colon, *first_space;
1848         char    *ck_cmdline;
1849 
1850         BUG_ON(!crash_size || !crash_base);
1851         *crash_size = 0;
1852         *crash_base = 0;
1853 
1854         ck_cmdline = get_last_crashkernel(cmdline, name, suffix);
1855 
1856         if (!ck_cmdline)
1857                 return -EINVAL;
1858 
1859         ck_cmdline += strlen(name);
1860 
1861         if (suffix)
1862                 return parse_crashkernel_suffix(ck_cmdline, crash_size,
1863                                 suffix);
1864         /*
1865          * if the commandline contains a ':', then that's the extended
1866          * syntax -- if not, it must be the classic syntax
1867          */
1868         first_colon = strchr(ck_cmdline, ':');
1869         first_space = strchr(ck_cmdline, ' ');
1870         if (first_colon && (!first_space || first_colon < first_space))
1871                 return parse_crashkernel_mem(ck_cmdline, system_ram,
1872                                 crash_size, crash_base);
1873 
1874         return parse_crashkernel_simple(ck_cmdline, crash_size, crash_base);
1875 }
1876 
1877 /*
1878  * That function is the entry point for command line parsing and should be
1879  * called from the arch-specific code.
1880  */
1881 int __init parse_crashkernel(char *cmdline,
1882                              unsigned long long system_ram,
1883                              unsigned long long *crash_size,
1884                              unsigned long long *crash_base)
1885 {
1886         return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
1887                                         "crashkernel=", NULL);
1888 }
1889 
1890 int __init parse_crashkernel_high(char *cmdline,
1891                              unsigned long long system_ram,
1892                              unsigned long long *crash_size,
1893                              unsigned long long *crash_base)
1894 {
1895         return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
1896                                 "crashkernel=", suffix_tbl[SUFFIX_HIGH]);
1897 }
1898 
1899 int __init parse_crashkernel_low(char *cmdline,
1900                              unsigned long long system_ram,
1901                              unsigned long long *crash_size,
1902                              unsigned long long *crash_base)
1903 {
1904         return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
1905                                 "crashkernel=", suffix_tbl[SUFFIX_LOW]);
1906 }
1907 
1908 static void update_vmcoreinfo_note(void)
1909 {
1910         u32 *buf = vmcoreinfo_note;
1911 
1912         if (!vmcoreinfo_size)
1913                 return;
1914         buf = append_elf_note(buf, VMCOREINFO_NOTE_NAME, 0, vmcoreinfo_data,
1915                               vmcoreinfo_size);
1916         final_note(buf);
1917 }
1918 
1919 void crash_save_vmcoreinfo(void)
1920 {
1921         vmcoreinfo_append_str("CRASHTIME=%ld\n", get_seconds());
1922         update_vmcoreinfo_note();
1923 }
1924 
1925 void vmcoreinfo_append_str(const char *fmt, ...)
1926 {
1927         va_list args;
1928         char buf[0x50];
1929         size_t r;
1930 
1931         va_start(args, fmt);
1932         r = vscnprintf(buf, sizeof(buf), fmt, args);
1933         va_end(args);
1934 
1935         r = min(r, vmcoreinfo_max_size - vmcoreinfo_size);
1936 
1937         memcpy(&vmcoreinfo_data[vmcoreinfo_size], buf, r);
1938 
1939         vmcoreinfo_size += r;
1940 }
1941 
1942 /*
1943  * provide an empty default implementation here -- architecture
1944  * code may override this
1945  */
1946 void __weak arch_crash_save_vmcoreinfo(void)
1947 {}
1948 
1949 unsigned long __weak paddr_vmcoreinfo_note(void)
1950 {
1951         return __pa((unsigned long)(char *)&vmcoreinfo_note);
1952 }
1953 
1954 static int __init crash_save_vmcoreinfo_init(void)
1955 {
1956         VMCOREINFO_OSRELEASE(init_uts_ns.name.release);
1957         VMCOREINFO_PAGESIZE(PAGE_SIZE);
1958 
1959         VMCOREINFO_SYMBOL(init_uts_ns);
1960         VMCOREINFO_SYMBOL(node_online_map);
1961 #ifdef CONFIG_MMU
1962         VMCOREINFO_SYMBOL(swapper_pg_dir);
1963 #endif
1964         VMCOREINFO_SYMBOL(_stext);
1965         VMCOREINFO_SYMBOL(vmap_area_list);
1966 
1967 #ifndef CONFIG_NEED_MULTIPLE_NODES
1968         VMCOREINFO_SYMBOL(mem_map);
1969         VMCOREINFO_SYMBOL(contig_page_data);
1970 #endif
1971 #ifdef CONFIG_SPARSEMEM
1972         VMCOREINFO_SYMBOL(mem_section);
1973         VMCOREINFO_LENGTH(mem_section, NR_SECTION_ROOTS);
1974         VMCOREINFO_STRUCT_SIZE(mem_section);
1975         VMCOREINFO_OFFSET(mem_section, section_mem_map);
1976 #endif
1977         VMCOREINFO_STRUCT_SIZE(page);
1978         VMCOREINFO_STRUCT_SIZE(pglist_data);
1979         VMCOREINFO_STRUCT_SIZE(zone);
1980         VMCOREINFO_STRUCT_SIZE(free_area);
1981         VMCOREINFO_STRUCT_SIZE(list_head);
1982         VMCOREINFO_SIZE(nodemask_t);
1983         VMCOREINFO_OFFSET(page, flags);
1984         VMCOREINFO_OFFSET(page, _count);
1985         VMCOREINFO_OFFSET(page, mapping);
1986         VMCOREINFO_OFFSET(page, lru);
1987         VMCOREINFO_OFFSET(page, _mapcount);
1988         VMCOREINFO_OFFSET(page, private);
1989         VMCOREINFO_OFFSET(pglist_data, node_zones);
1990         VMCOREINFO_OFFSET(pglist_data, nr_zones);
1991 #ifdef CONFIG_FLAT_NODE_MEM_MAP
1992         VMCOREINFO_OFFSET(pglist_data, node_mem_map);
1993 #endif
1994         VMCOREINFO_OFFSET(pglist_data, node_start_pfn);
1995         VMCOREINFO_OFFSET(pglist_data, node_spanned_pages);
1996         VMCOREINFO_OFFSET(pglist_data, node_id);
1997         VMCOREINFO_OFFSET(zone, free_area);
1998         VMCOREINFO_OFFSET(zone, vm_stat);
1999         VMCOREINFO_OFFSET(zone, spanned_pages);
2000         VMCOREINFO_OFFSET(free_area, free_list);
2001         VMCOREINFO_OFFSET(list_head, next);
2002         VMCOREINFO_OFFSET(list_head, prev);
2003         VMCOREINFO_OFFSET(vmap_area, va_start);
2004         VMCOREINFO_OFFSET(vmap_area, list);
2005         VMCOREINFO_LENGTH(zone.free_area, MAX_ORDER);
2006         log_buf_kexec_setup();
2007         VMCOREINFO_LENGTH(free_area.free_list, MIGRATE_TYPES);
2008         VMCOREINFO_NUMBER(NR_FREE_PAGES);
2009         VMCOREINFO_NUMBER(PG_lru);
2010         VMCOREINFO_NUMBER(PG_private);
2011         VMCOREINFO_NUMBER(PG_swapcache);
2012         VMCOREINFO_NUMBER(PG_slab);
2013 #ifdef CONFIG_MEMORY_FAILURE
2014         VMCOREINFO_NUMBER(PG_hwpoison);
2015 #endif
2016         VMCOREINFO_NUMBER(PG_head_mask);
2017         VMCOREINFO_NUMBER(PAGE_BUDDY_MAPCOUNT_VALUE);
2018 #ifdef CONFIG_HUGETLBFS
2019         VMCOREINFO_SYMBOL(free_huge_page);
2020 #endif
2021 
2022         arch_crash_save_vmcoreinfo();
2023         update_vmcoreinfo_note();
2024 
2025         return 0;
2026 }
2027 
2028 subsys_initcall(crash_save_vmcoreinfo_init);
2029 
2030 #ifdef CONFIG_KEXEC_FILE
2031 static int locate_mem_hole_top_down(unsigned long start, unsigned long end,
2032                                     struct kexec_buf *kbuf)
2033 {
2034         struct kimage *image = kbuf->image;
2035         unsigned long temp_start, temp_end;
2036 
2037         temp_end = min(end, kbuf->buf_max);
2038         temp_start = temp_end - kbuf->memsz;
2039 
2040         do {
2041                 /* align down start */
2042                 temp_start = temp_start & (~(kbuf->buf_align - 1));
2043 
2044                 if (temp_start < start || temp_start < kbuf->buf_min)
2045                         return 0;
2046 
2047                 temp_end = temp_start + kbuf->memsz - 1;
2048 
2049                 /*
2050                  * Make sure this does not conflict with any of existing
2051                  * segments
2052                  */
2053                 if (kimage_is_destination_range(image, temp_start, temp_end)) {
2054                         temp_start = temp_start - PAGE_SIZE;
2055                         continue;
2056                 }
2057 
2058                 /* We found a suitable memory range */
2059                 break;
2060         } while (1);
2061 
2062         /* If we are here, we found a suitable memory range */
2063         kbuf->mem = temp_start;
2064 
2065         /* Success, stop navigating through remaining System RAM ranges */
2066         return 1;
2067 }
2068 
2069 static int locate_mem_hole_bottom_up(unsigned long start, unsigned long end,
2070                                      struct kexec_buf *kbuf)
2071 {
2072         struct kimage *image = kbuf->image;
2073         unsigned long temp_start, temp_end;
2074 
2075         temp_start = max(start, kbuf->buf_min);
2076 
2077         do {
2078                 temp_start = ALIGN(temp_start, kbuf->buf_align);
2079                 temp_end = temp_start + kbuf->memsz - 1;
2080 
2081                 if (temp_end > end || temp_end > kbuf->buf_max)
2082                         return 0;
2083                 /*
2084                  * Make sure this does not conflict with any of existing
2085                  * segments
2086                  */
2087                 if (kimage_is_destination_range(image, temp_start, temp_end)) {
2088                         temp_start = temp_start + PAGE_SIZE;
2089                         continue;
2090                 }
2091 
2092                 /* We found a suitable memory range */
2093                 break;
2094         } while (1);
2095 
2096         /* If we are here, we found a suitable memory range */
2097         kbuf->mem = temp_start;
2098 
2099         /* Success, stop navigating through remaining System RAM ranges */
2100         return 1;
2101 }
2102 
2103 static int locate_mem_hole_callback(u64 start, u64 end, void *arg)
2104 {
2105         struct kexec_buf *kbuf = (struct kexec_buf *)arg;
2106         unsigned long sz = end - start + 1;
2107 
2108         /* Returning 0 will take to next memory range */
2109         if (sz < kbuf->memsz)
2110                 return 0;
2111 
2112         if (end < kbuf->buf_min || start > kbuf->buf_max)
2113                 return 0;
2114 
2115         /*
2116          * Allocate memory top down with-in ram range. Otherwise bottom up
2117          * allocation.
2118          */
2119         if (kbuf->top_down)
2120                 return locate_mem_hole_top_down(start, end, kbuf);
2121         return locate_mem_hole_bottom_up(start, end, kbuf);
2122 }
2123 
2124 /*
2125  * Helper function for placing a buffer in a kexec segment. This assumes
2126  * that kexec_mutex is held.
2127  */
2128 int kexec_add_buffer(struct kimage *image, char *buffer, unsigned long bufsz,
2129                      unsigned long memsz, unsigned long buf_align,
2130                      unsigned long buf_min, unsigned long buf_max,
2131                      bool top_down, unsigned long *load_addr)
2132 {
2133 
2134         struct kexec_segment *ksegment;
2135         struct kexec_buf buf, *kbuf;
2136         int ret;
2137 
2138         /* Currently adding segment this way is allowed only in file mode */
2139         if (!image->file_mode)
2140                 return -EINVAL;
2141 
2142         if (image->nr_segments >= KEXEC_SEGMENT_MAX)
2143                 return -EINVAL;
2144 
2145         /*
2146          * Make sure we are not trying to add buffer after allocating
2147          * control pages. All segments need to be placed first before
2148          * any control pages are allocated. As control page allocation
2149          * logic goes through list of segments to make sure there are
2150          * no destination overlaps.
2151          */
2152         if (!list_empty(&image->control_pages)) {
2153                 WARN_ON(1);
2154                 return -EINVAL;
2155         }
2156 
2157         memset(&buf, 0, sizeof(struct kexec_buf));
2158         kbuf = &buf;
2159         kbuf->image = image;
2160         kbuf->buffer = buffer;
2161         kbuf->bufsz = bufsz;
2162 
2163         kbuf->memsz = ALIGN(memsz, PAGE_SIZE);
2164         kbuf->buf_align = max(buf_align, PAGE_SIZE);
2165         kbuf->buf_min = buf_min;
2166         kbuf->buf_max = buf_max;
2167         kbuf->top_down = top_down;
2168 
2169         /* Walk the RAM ranges and allocate a suitable range for the buffer */
2170         if (image->type == KEXEC_TYPE_CRASH)
2171                 ret = walk_iomem_res("Crash kernel",
2172                                      IORESOURCE_MEM | IORESOURCE_BUSY,
2173                                      crashk_res.start, crashk_res.end, kbuf,
2174                                      locate_mem_hole_callback);
2175         else
2176                 ret = walk_system_ram_res(0, -1, kbuf,
2177                                           locate_mem_hole_callback);
2178         if (ret != 1) {
2179                 /* A suitable memory range could not be found for buffer */
2180                 return -EADDRNOTAVAIL;
2181         }
2182 
2183         /* Found a suitable memory range */
2184         ksegment = &image->segment[image->nr_segments];
2185         ksegment->kbuf = kbuf->buffer;
2186         ksegment->bufsz = kbuf->bufsz;
2187         ksegment->mem = kbuf->mem;
2188         ksegment->memsz = kbuf->memsz;
2189         image->nr_segments++;
2190         *load_addr = ksegment->mem;
2191         return 0;
2192 }
2193 
2194 /* Calculate and store the digest of segments */
2195 static int kexec_calculate_store_digests(struct kimage *image)
2196 {
2197         struct crypto_shash *tfm;
2198         struct shash_desc *desc;
2199         int ret = 0, i, j, zero_buf_sz, sha_region_sz;
2200         size_t desc_size, nullsz;
2201         char *digest;
2202         void *zero_buf;
2203         struct kexec_sha_region *sha_regions;
2204         struct purgatory_info *pi = &image->purgatory_info;
2205 
2206         zero_buf = __va(page_to_pfn(ZERO_PAGE(0)) << PAGE_SHIFT);
2207         zero_buf_sz = PAGE_SIZE;
2208 
2209         tfm = crypto_alloc_shash("sha256", 0, 0);
2210         if (IS_ERR(tfm)) {
2211                 ret = PTR_ERR(tfm);
2212                 goto out;
2213         }
2214 
2215         desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
2216         desc = kzalloc(desc_size, GFP_KERNEL);
2217         if (!desc) {
2218                 ret = -ENOMEM;
2219                 goto out_free_tfm;
2220         }
2221 
2222         sha_region_sz = KEXEC_SEGMENT_MAX * sizeof(struct kexec_sha_region);
2223         sha_regions = vzalloc(sha_region_sz);
2224         if (!sha_regions)
2225                 goto out_free_desc;
2226 
2227         desc->tfm   = tfm;
2228         desc->flags = 0;
2229 
2230         ret = crypto_shash_init(desc);
2231         if (ret < 0)
2232                 goto out_free_sha_regions;
2233 
2234         digest = kzalloc(SHA256_DIGEST_SIZE, GFP_KERNEL);
2235         if (!digest) {
2236                 ret = -ENOMEM;
2237                 goto out_free_sha_regions;
2238         }
2239 
2240         for (j = i = 0; i < image->nr_segments; i++) {
2241                 struct kexec_segment *ksegment;
2242 
2243                 ksegment = &image->segment[i];
2244                 /*
2245                  * Skip purgatory as it will be modified once we put digest
2246                  * info in purgatory.
2247                  */
2248                 if (ksegment->kbuf == pi->purgatory_buf)
2249                         continue;
2250 
2251                 ret = crypto_shash_update(desc, ksegment->kbuf,
2252                                           ksegment->bufsz);
2253                 if (ret)
2254                         break;
2255 
2256                 /*
2257                  * Assume rest of the buffer is filled with zero and
2258                  * update digest accordingly.
2259                  */
2260                 nullsz = ksegment->memsz - ksegment->bufsz;
2261                 while (nullsz) {
2262                         unsigned long bytes = nullsz;
2263 
2264                         if (bytes > zero_buf_sz)
2265                                 bytes = zero_buf_sz;
2266                         ret = crypto_shash_update(desc, zero_buf, bytes);
2267                         if (ret)
2268                                 break;
2269                         nullsz -= bytes;
2270                 }
2271 
2272                 if (ret)
2273                         break;
2274 
2275                 sha_regions[j].start = ksegment->mem;
2276                 sha_regions[j].len = ksegment->memsz;
2277                 j++;
2278         }
2279 
2280         if (!ret) {
2281                 ret = crypto_shash_final(desc, digest);
2282                 if (ret)
2283                         goto out_free_digest;
2284                 ret = kexec_purgatory_get_set_symbol(image, "sha_regions",
2285                                                 sha_regions, sha_region_sz, 0);
2286                 if (ret)
2287                         goto out_free_digest;
2288 
2289                 ret = kexec_purgatory_get_set_symbol(image, "sha256_digest",
2290                                                 digest, SHA256_DIGEST_SIZE, 0);
2291                 if (ret)
2292                         goto out_free_digest;
2293         }
2294 
2295 out_free_digest:
2296         kfree(digest);
2297 out_free_sha_regions:
2298         vfree(sha_regions);
2299 out_free_desc:
2300         kfree(desc);
2301 out_free_tfm:
2302         kfree(tfm);
2303 out:
2304         return ret;
2305 }
2306 
2307 /* Actually load purgatory. Lot of code taken from kexec-tools */
2308 static int __kexec_load_purgatory(struct kimage *image, unsigned long min,
2309                                   unsigned long max, int top_down)
2310 {
2311         struct purgatory_info *pi = &image->purgatory_info;
2312         unsigned long align, buf_align, bss_align, buf_sz, bss_sz, bss_pad;
2313         unsigned long memsz, entry, load_addr, curr_load_addr, bss_addr, offset;
2314         unsigned char *buf_addr, *src;
2315         int i, ret = 0, entry_sidx = -1;
2316         const Elf_Shdr *sechdrs_c;
2317         Elf_Shdr *sechdrs = NULL;
2318         void *purgatory_buf = NULL;
2319 
2320         /*
2321          * sechdrs_c points to section headers in purgatory and are read
2322          * only. No modifications allowed.
2323          */
2324         sechdrs_c = (void *)pi->ehdr + pi->ehdr->e_shoff;
2325 
2326         /*
2327          * We can not modify sechdrs_c[] and its fields. It is read only.
2328          * Copy it over to a local copy where one can store some temporary
2329          * data and free it at the end. We need to modify ->sh_addr and
2330          * ->sh_offset fields to keep track of permanent and temporary
2331          * locations of sections.
2332          */
2333         sechdrs = vzalloc(pi->ehdr->e_shnum * sizeof(Elf_Shdr));
2334         if (!sechdrs)
2335                 return -ENOMEM;
2336 
2337         memcpy(sechdrs, sechdrs_c, pi->ehdr->e_shnum * sizeof(Elf_Shdr));
2338 
2339         /*
2340          * We seem to have multiple copies of sections. First copy is which
2341          * is embedded in kernel in read only section. Some of these sections
2342          * will be copied to a temporary buffer and relocated. And these
2343          * sections will finally be copied to their final destination at
2344          * segment load time.
2345          *
2346          * Use ->sh_offset to reflect section address in memory. It will
2347          * point to original read only copy if section is not allocatable.
2348          * Otherwise it will point to temporary copy which will be relocated.
2349          *
2350          * Use ->sh_addr to contain final address of the section where it
2351          * will go during execution time.
2352          */
2353         for (i = 0; i < pi->ehdr->e_shnum; i++) {
2354                 if (sechdrs[i].sh_type == SHT_NOBITS)
2355                         continue;
2356 
2357                 sechdrs[i].sh_offset = (unsigned long)pi->ehdr +
2358                                                 sechdrs[i].sh_offset;
2359         }
2360 
2361         /*
2362          * Identify entry point section and make entry relative to section
2363          * start.
2364          */
2365         entry = pi->ehdr->e_entry;
2366         for (i = 0; i < pi->ehdr->e_shnum; i++) {
2367                 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2368                         continue;
2369 
2370                 if (!(sechdrs[i].sh_flags & SHF_EXECINSTR))
2371                         continue;
2372 
2373                 /* Make entry section relative */
2374                 if (sechdrs[i].sh_addr <= pi->ehdr->e_entry &&
2375                     ((sechdrs[i].sh_addr + sechdrs[i].sh_size) >
2376                      pi->ehdr->e_entry)) {
2377                         entry_sidx = i;
2378                         entry -= sechdrs[i].sh_addr;
2379                         break;
2380                 }
2381         }
2382 
2383         /* Determine how much memory is needed to load relocatable object. */
2384         buf_align = 1;
2385         bss_align = 1;
2386         buf_sz = 0;
2387         bss_sz = 0;
2388 
2389         for (i = 0; i < pi->ehdr->e_shnum; i++) {
2390                 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2391                         continue;
2392 
2393                 align = sechdrs[i].sh_addralign;
2394                 if (sechdrs[i].sh_type != SHT_NOBITS) {
2395                         if (buf_align < align)
2396                                 buf_align = align;
2397                         buf_sz = ALIGN(buf_sz, align);
2398                         buf_sz += sechdrs[i].sh_size;
2399                 } else {
2400                         /* bss section */
2401                         if (bss_align < align)
2402                                 bss_align = align;
2403                         bss_sz = ALIGN(bss_sz, align);
2404                         bss_sz += sechdrs[i].sh_size;
2405                 }
2406         }
2407 
2408         /* Determine the bss padding required to align bss properly */
2409         bss_pad = 0;
2410         if (buf_sz & (bss_align - 1))
2411                 bss_pad = bss_align - (buf_sz & (bss_align - 1));
2412 
2413         memsz = buf_sz + bss_pad + bss_sz;
2414 
2415         /* Allocate buffer for purgatory */
2416         purgatory_buf = vzalloc(buf_sz);
2417         if (!purgatory_buf) {
2418                 ret = -ENOMEM;
2419                 goto out;
2420         }
2421 
2422         if (buf_align < bss_align)
2423                 buf_align = bss_align;
2424 
2425         /* Add buffer to segment list */
2426         ret = kexec_add_buffer(image, purgatory_buf, buf_sz, memsz,
2427                                 buf_align, min, max, top_down,
2428                                 &pi->purgatory_load_addr);
2429         if (ret)
2430                 goto out;
2431 
2432         /* Load SHF_ALLOC sections */
2433         buf_addr = purgatory_buf;
2434         load_addr = curr_load_addr = pi->purgatory_load_addr;
2435         bss_addr = load_addr + buf_sz + bss_pad;
2436 
2437         for (i = 0; i < pi->ehdr->e_shnum; i++) {
2438                 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2439                         continue;
2440 
2441                 align = sechdrs[i].sh_addralign;
2442                 if (sechdrs[i].sh_type != SHT_NOBITS) {
2443                         curr_load_addr = ALIGN(curr_load_addr, align);
2444                         offset = curr_load_addr - load_addr;
2445                         /* We already modifed ->sh_offset to keep src addr */
2446                         src = (char *) sechdrs[i].sh_offset;
2447                         memcpy(buf_addr + offset, src, sechdrs[i].sh_size);
2448 
2449                         /* Store load address and source address of section */
2450                         sechdrs[i].sh_addr = curr_load_addr;
2451 
2452                         /*
2453                          * This section got copied to temporary buffer. Update
2454                          * ->sh_offset accordingly.
2455                          */
2456                         sechdrs[i].sh_offset = (unsigned long)(buf_addr + offset);
2457 
2458                         /* Advance to the next address */
2459                         curr_load_addr += sechdrs[i].sh_size;
2460                 } else {
2461                         bss_addr = ALIGN(bss_addr, align);
2462                         sechdrs[i].sh_addr = bss_addr;
2463                         bss_addr += sechdrs[i].sh_size;
2464                 }
2465         }
2466 
2467         /* Update entry point based on load address of text section */
2468         if (entry_sidx >= 0)
2469                 entry += sechdrs[entry_sidx].sh_addr;
2470 
2471         /* Make kernel jump to purgatory after shutdown */
2472         image->start = entry;
2473 
2474         /* Used later to get/set symbol values */
2475         pi->sechdrs = sechdrs;
2476 
2477         /*
2478          * Used later to identify which section is purgatory and skip it
2479          * from checksumming.
2480          */
2481         pi->purgatory_buf = purgatory_buf;
2482         return ret;
2483 out:
2484         vfree(sechdrs);
2485         vfree(purgatory_buf);
2486         return ret;
2487 }
2488 
2489 static int kexec_apply_relocations(struct kimage *image)
2490 {
2491         int i, ret;
2492         struct purgatory_info *pi = &image->purgatory_info;
2493         Elf_Shdr *sechdrs = pi->sechdrs;
2494 
2495         /* Apply relocations */
2496         for (i = 0; i < pi->ehdr->e_shnum; i++) {
2497                 Elf_Shdr *section, *symtab;
2498 
2499                 if (sechdrs[i].sh_type != SHT_RELA &&
2500                     sechdrs[i].sh_type != SHT_REL)
2501                         continue;
2502 
2503                 /*
2504                  * For section of type SHT_RELA/SHT_REL,
2505                  * ->sh_link contains section header index of associated
2506                  * symbol table. And ->sh_info contains section header
2507                  * index of section to which relocations apply.
2508                  */
2509                 if (sechdrs[i].sh_info >= pi->ehdr->e_shnum ||
2510                     sechdrs[i].sh_link >= pi->ehdr->e_shnum)
2511                         return -ENOEXEC;
2512 
2513                 section = &sechdrs[sechdrs[i].sh_info];
2514                 symtab = &sechdrs[sechdrs[i].sh_link];
2515 
2516                 if (!(section->sh_flags & SHF_ALLOC))
2517                         continue;
2518 
2519                 /*
2520                  * symtab->sh_link contain section header index of associated
2521                  * string table.
2522                  */
2523                 if (symtab->sh_link >= pi->ehdr->e_shnum)
2524                         /* Invalid section number? */
2525                         continue;
2526 
2527                 /*
2528                  * Respective architecture needs to provide support for applying
2529                  * relocations of type SHT_RELA/SHT_REL.
2530                  */
2531                 if (sechdrs[i].sh_type == SHT_RELA)
2532                         ret = arch_kexec_apply_relocations_add(pi->ehdr,
2533                                                                sechdrs, i);
2534                 else if (sechdrs[i].sh_type == SHT_REL)
2535                         ret = arch_kexec_apply_relocations(pi->ehdr,
2536                                                            sechdrs, i);
2537                 if (ret)
2538                         return ret;
2539         }
2540 
2541         return 0;
2542 }
2543 
2544 /* Load relocatable purgatory object and relocate it appropriately */
2545 int kexec_load_purgatory(struct kimage *image, unsigned long min,
2546                          unsigned long max, int top_down,
2547                          unsigned long *load_addr)
2548 {
2549         struct purgatory_info *pi = &image->purgatory_info;
2550         int ret;
2551 
2552         if (kexec_purgatory_size <= 0)
2553                 return -EINVAL;
2554 
2555         if (kexec_purgatory_size < sizeof(Elf_Ehdr))
2556                 return -ENOEXEC;
2557 
2558         pi->ehdr = (Elf_Ehdr *)kexec_purgatory;
2559 
2560         if (memcmp(pi->ehdr->e_ident, ELFMAG, SELFMAG) != 0
2561             || pi->ehdr->e_type != ET_REL
2562             || !elf_check_arch(pi->ehdr)
2563             || pi->ehdr->e_shentsize != sizeof(Elf_Shdr))
2564                 return -ENOEXEC;
2565 
2566         if (pi->ehdr->e_shoff >= kexec_purgatory_size
2567             || (pi->ehdr->e_shnum * sizeof(Elf_Shdr) >
2568             kexec_purgatory_size - pi->ehdr->e_shoff))
2569                 return -ENOEXEC;
2570 
2571         ret = __kexec_load_purgatory(image, min, max, top_down);
2572         if (ret)
2573                 return ret;
2574 
2575         ret = kexec_apply_relocations(image);
2576         if (ret)
2577                 goto out;
2578 
2579         *load_addr = pi->purgatory_load_addr;
2580         return 0;
2581 out:
2582         vfree(pi->sechdrs);
2583         vfree(pi->purgatory_buf);
2584         return ret;
2585 }
2586 
2587 static Elf_Sym *kexec_purgatory_find_symbol(struct purgatory_info *pi,
2588                                             const char *name)
2589 {
2590         Elf_Sym *syms;
2591         Elf_Shdr *sechdrs;
2592         Elf_Ehdr *ehdr;
2593         int i, k;
2594         const char *strtab;
2595 
2596         if (!pi->sechdrs || !pi->ehdr)
2597                 return NULL;
2598 
2599         sechdrs = pi->sechdrs;
2600         ehdr = pi->ehdr;
2601 
2602         for (i = 0; i < ehdr->e_shnum; i++) {
2603                 if (sechdrs[i].sh_type != SHT_SYMTAB)
2604                         continue;
2605 
2606                 if (sechdrs[i].sh_link >= ehdr->e_shnum)
2607                         /* Invalid strtab section number */
2608                         continue;
2609                 strtab = (char *)sechdrs[sechdrs[i].sh_link].sh_offset;
2610                 syms = (Elf_Sym *)sechdrs[i].sh_offset;
2611 
2612                 /* Go through symbols for a match */
2613                 for (k = 0; k < sechdrs[i].sh_size/sizeof(Elf_Sym); k++) {
2614                         if (ELF_ST_BIND(syms[k].st_info) != STB_GLOBAL)
2615                                 continue;
2616 
2617                         if (strcmp(strtab + syms[k].st_name, name) != 0)
2618                                 continue;
2619 
2620                         if (syms[k].st_shndx == SHN_UNDEF ||
2621                             syms[k].st_shndx >= ehdr->e_shnum) {
2622                                 pr_debug("Symbol: %s has bad section index %d.\n",
2623                                                 name, syms[k].st_shndx);
2624                                 return NULL;
2625                         }
2626 
2627                         /* Found the symbol we are looking for */
2628                         return &syms[k];
2629                 }
2630         }
2631 
2632         return NULL;
2633 }
2634 
2635 void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name)
2636 {
2637         struct purgatory_info *pi = &image->purgatory_info;
2638         Elf_Sym *sym;
2639         Elf_Shdr *sechdr;
2640 
2641         sym = kexec_purgatory_find_symbol(pi, name);
2642         if (!sym)
2643                 return ERR_PTR(-EINVAL);
2644 
2645         sechdr = &pi->sechdrs[sym->st_shndx];
2646 
2647         /*
2648          * Returns the address where symbol will finally be loaded after
2649          * kexec_load_segment()
2650          */
2651         return (void *)(sechdr->sh_addr + sym->st_value);
2652 }
2653 
2654 /*
2655  * Get or set value of a symbol. If "get_value" is true, symbol value is
2656  * returned in buf otherwise symbol value is set based on value in buf.
2657  */
2658 int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name,
2659                                    void *buf, unsigned int size, bool get_value)
2660 {
2661         Elf_Sym *sym;
2662         Elf_Shdr *sechdrs;
2663         struct purgatory_info *pi = &image->purgatory_info;
2664         char *sym_buf;
2665 
2666         sym = kexec_purgatory_find_symbol(pi, name);
2667         if (!sym)
2668                 return -EINVAL;
2669 
2670         if (sym->st_size != size) {
2671                 pr_err("symbol %s size mismatch: expected %lu actual %u\n",
2672                        name, (unsigned long)sym->st_size, size);
2673                 return -EINVAL;
2674         }
2675 
2676         sechdrs = pi->sechdrs;
2677 
2678         if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
2679                 pr_err("symbol %s is in a bss section. Cannot %s\n", name,
2680                        get_value ? "get" : "set");
2681                 return -EINVAL;
2682         }
2683 
2684         sym_buf = (unsigned char *)sechdrs[sym->st_shndx].sh_offset +
2685                                         sym->st_value;
2686 
2687         if (get_value)
2688                 memcpy((void *)buf, sym_buf, size);
2689         else
2690                 memcpy((void *)sym_buf, buf, size);
2691 
2692         return 0;
2693 }
2694 #endif /* CONFIG_KEXEC_FILE */
2695 
2696 /*
2697  * Move into place and start executing a preloaded standalone
2698  * executable.  If nothing was preloaded return an error.
2699  */
2700 int kernel_kexec(void)
2701 {
2702         int error = 0;
2703 
2704         if (!mutex_trylock(&kexec_mutex))
2705                 return -EBUSY;
2706         if (!kexec_image) {
2707                 error = -EINVAL;
2708                 goto Unlock;
2709         }
2710 
2711 #ifdef CONFIG_KEXEC_JUMP
2712         if (kexec_image->preserve_context) {
2713                 lock_system_sleep();
2714                 pm_prepare_console();
2715                 error = freeze_processes();
2716                 if (error) {
2717                         error = -EBUSY;
2718                         goto Restore_console;
2719                 }
2720                 suspend_console();
2721                 error = dpm_suspend_start(PMSG_FREEZE);
2722                 if (error)
2723                         goto Resume_console;
2724                 /* At this point, dpm_suspend_start() has been called,
2725                  * but *not* dpm_suspend_end(). We *must* call
2726                  * dpm_suspend_end() now.  Otherwise, drivers for
2727                  * some devices (e.g. interrupt controllers) become
2728                  * desynchronized with the actual state of the
2729                  * hardware at resume time, and evil weirdness ensues.
2730                  */
2731                 error = dpm_suspend_end(PMSG_FREEZE);
2732                 if (error)
2733                         goto Resume_devices;
2734                 error = disable_nonboot_cpus();
2735                 if (error)
2736                         goto Enable_cpus;
2737                 local_irq_disable();
2738                 error = syscore_suspend();
2739                 if (error)
2740                         goto Enable_irqs;
2741         } else
2742 #endif
2743         {
2744                 kexec_in_progress = true;
2745                 kernel_restart_prepare(NULL);
2746                 migrate_to_reboot_cpu();
2747 
2748                 /*
2749                  * migrate_to_reboot_cpu() disables CPU hotplug assuming that
2750                  * no further code needs to use CPU hotplug (which is true in
2751                  * the reboot case). However, the kexec path depends on using
2752                  * CPU hotplug again; so re-enable it here.
2753                  */
2754                 cpu_hotplug_enable();
2755                 pr_emerg("Starting new kernel\n");
2756                 machine_shutdown();
2757         }
2758 
2759         machine_kexec(kexec_image);
2760 
2761 #ifdef CONFIG_KEXEC_JUMP
2762         if (kexec_image->preserve_context) {
2763                 syscore_resume();
2764  Enable_irqs:
2765                 local_irq_enable();
2766  Enable_cpus:
2767                 enable_nonboot_cpus();
2768                 dpm_resume_start(PMSG_RESTORE);
2769  Resume_devices:
2770                 dpm_resume_end(PMSG_RESTORE);
2771  Resume_console:
2772                 resume_console();
2773                 thaw_processes();
2774  Restore_console:
2775                 pm_restore_console();
2776                 unlock_system_sleep();
2777         }
2778 #endif
2779 
2780  Unlock:
2781         mutex_unlock(&kexec_mutex);
2782         return error;
2783 }
2784 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp