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

~ [ 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