2 * kexec: kexec_file_load system call
4 * Copyright (C) 2014 Red Hat Inc.
6 * Vivek Goyal <vgoyal@redhat.com>
8 * This source code is licensed under the GNU General Public License,
9 * Version 2. See the file COPYING for more details.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/capability.h>
16 #include <linux/file.h>
17 #include <linux/slab.h>
18 #include <linux/kexec.h>
19 #include <linux/mutex.h>
20 #include <linux/list.h>
22 #include <linux/ima.h>
23 #include <crypto/hash.h>
24 #include <crypto/sha.h>
25 #include <linux/elf.h>
26 #include <linux/elfcore.h>
27 #include <linux/kernel.h>
28 #include <linux/kexec.h>
29 #include <linux/slab.h>
30 #include <linux/syscalls.h>
31 #include <linux/vmalloc.h>
32 #include "kexec_internal.h"
34 static int kexec_calculate_store_digests(struct kimage *image);
37 * Currently this is the only default function that is exported as some
38 * architectures need it to do additional handlings.
39 * In the future, other default functions may be exported too if required.
41 int kexec_image_probe_default(struct kimage *image, void *buf,
42 unsigned long buf_len)
44 const struct kexec_file_ops * const *fops;
47 for (fops = &kexec_file_loaders[0]; *fops && (*fops)->probe; ++fops) {
48 ret = (*fops)->probe(buf, buf_len);
58 /* Architectures can provide this probe function */
59 int __weak arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
60 unsigned long buf_len)
62 return kexec_image_probe_default(image, buf, buf_len);
65 static void *kexec_image_load_default(struct kimage *image)
67 if (!image->fops || !image->fops->load)
68 return ERR_PTR(-ENOEXEC);
70 return image->fops->load(image, image->kernel_buf,
71 image->kernel_buf_len, image->initrd_buf,
72 image->initrd_buf_len, image->cmdline_buf,
73 image->cmdline_buf_len);
76 void * __weak arch_kexec_kernel_image_load(struct kimage *image)
78 return kexec_image_load_default(image);
81 static int kexec_image_post_load_cleanup_default(struct kimage *image)
83 if (!image->fops || !image->fops->cleanup)
86 return image->fops->cleanup(image->image_loader_data);
89 int __weak arch_kimage_file_post_load_cleanup(struct kimage *image)
91 return kexec_image_post_load_cleanup_default(image);
94 #ifdef CONFIG_KEXEC_VERIFY_SIG
95 static int kexec_image_verify_sig_default(struct kimage *image, void *buf,
96 unsigned long buf_len)
98 if (!image->fops || !image->fops->verify_sig) {
99 pr_debug("kernel loader does not support signature verification.\n");
100 return -EKEYREJECTED;
103 return image->fops->verify_sig(buf, buf_len);
106 int __weak arch_kexec_kernel_verify_sig(struct kimage *image, void *buf,
107 unsigned long buf_len)
109 return kexec_image_verify_sig_default(image, buf, buf_len);
113 /* Apply relocations of type RELA */
115 arch_kexec_apply_relocations_add(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
118 pr_err("RELA relocation unsupported.\n");
122 /* Apply relocations of type REL */
124 arch_kexec_apply_relocations(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
127 pr_err("REL relocation unsupported.\n");
132 * Free up memory used by kernel, initrd, and command line. This is temporary
133 * memory allocation which is not needed any more after these buffers have
134 * been loaded into separate segments and have been copied elsewhere.
136 void kimage_file_post_load_cleanup(struct kimage *image)
138 struct purgatory_info *pi = &image->purgatory_info;
140 vfree(image->kernel_buf);
141 image->kernel_buf = NULL;
143 vfree(image->initrd_buf);
144 image->initrd_buf = NULL;
146 kfree(image->cmdline_buf);
147 image->cmdline_buf = NULL;
149 vfree(pi->purgatory_buf);
150 pi->purgatory_buf = NULL;
155 /* See if architecture has anything to cleanup post load */
156 arch_kimage_file_post_load_cleanup(image);
159 * Above call should have called into bootloader to free up
160 * any data stored in kimage->image_loader_data. It should
161 * be ok now to free it up.
163 kfree(image->image_loader_data);
164 image->image_loader_data = NULL;
168 * In file mode list of segments is prepared by kernel. Copy relevant
169 * data from user space, do error checking, prepare segment list
172 kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd,
173 const char __user *cmdline_ptr,
174 unsigned long cmdline_len, unsigned flags)
180 ret = kernel_read_file_from_fd(kernel_fd, &image->kernel_buf,
181 &size, INT_MAX, READING_KEXEC_IMAGE);
184 image->kernel_buf_len = size;
186 /* IMA needs to pass the measurement list to the next kernel. */
187 ima_add_kexec_buffer(image);
189 /* Call arch image probe handlers */
190 ret = arch_kexec_kernel_image_probe(image, image->kernel_buf,
191 image->kernel_buf_len);
195 #ifdef CONFIG_KEXEC_VERIFY_SIG
196 ret = arch_kexec_kernel_verify_sig(image, image->kernel_buf,
197 image->kernel_buf_len);
199 pr_debug("kernel signature verification failed.\n");
202 pr_debug("kernel signature verification successful.\n");
204 /* It is possible that there no initramfs is being loaded */
205 if (!(flags & KEXEC_FILE_NO_INITRAMFS)) {
206 ret = kernel_read_file_from_fd(initrd_fd, &image->initrd_buf,
208 READING_KEXEC_INITRAMFS);
211 image->initrd_buf_len = size;
215 image->cmdline_buf = memdup_user(cmdline_ptr, cmdline_len);
216 if (IS_ERR(image->cmdline_buf)) {
217 ret = PTR_ERR(image->cmdline_buf);
218 image->cmdline_buf = NULL;
222 image->cmdline_buf_len = cmdline_len;
224 /* command line should be a string with last byte null */
225 if (image->cmdline_buf[cmdline_len - 1] != '\0') {
231 /* Call arch image load handlers */
232 ldata = arch_kexec_kernel_image_load(image);
235 ret = PTR_ERR(ldata);
239 image->image_loader_data = ldata;
241 /* In case of error, free up all allocated memory in this function */
243 kimage_file_post_load_cleanup(image);
248 kimage_file_alloc_init(struct kimage **rimage, int kernel_fd,
249 int initrd_fd, const char __user *cmdline_ptr,
250 unsigned long cmdline_len, unsigned long flags)
253 struct kimage *image;
254 bool kexec_on_panic = flags & KEXEC_FILE_ON_CRASH;
256 image = do_kimage_alloc_init();
260 image->file_mode = 1;
262 if (kexec_on_panic) {
263 /* Enable special crash kernel control page alloc policy. */
264 image->control_page = crashk_res.start;
265 image->type = KEXEC_TYPE_CRASH;
268 ret = kimage_file_prepare_segments(image, kernel_fd, initrd_fd,
269 cmdline_ptr, cmdline_len, flags);
273 ret = sanity_check_segment_list(image);
275 goto out_free_post_load_bufs;
278 image->control_code_page = kimage_alloc_control_pages(image,
279 get_order(KEXEC_CONTROL_PAGE_SIZE));
280 if (!image->control_code_page) {
281 pr_err("Could not allocate control_code_buffer\n");
282 goto out_free_post_load_bufs;
285 if (!kexec_on_panic) {
286 image->swap_page = kimage_alloc_control_pages(image, 0);
287 if (!image->swap_page) {
288 pr_err("Could not allocate swap buffer\n");
289 goto out_free_control_pages;
295 out_free_control_pages:
296 kimage_free_page_list(&image->control_pages);
297 out_free_post_load_bufs:
298 kimage_file_post_load_cleanup(image);
304 SYSCALL_DEFINE5(kexec_file_load, int, kernel_fd, int, initrd_fd,
305 unsigned long, cmdline_len, const char __user *, cmdline_ptr,
306 unsigned long, flags)
309 struct kimage **dest_image, *image;
311 /* We only trust the superuser with rebooting the system. */
312 if (!capable(CAP_SYS_BOOT) || kexec_load_disabled)
315 /* Make sure we have a legal set of flags */
316 if (flags != (flags & KEXEC_FILE_FLAGS))
321 if (!mutex_trylock(&kexec_mutex))
324 dest_image = &kexec_image;
325 if (flags & KEXEC_FILE_ON_CRASH) {
326 dest_image = &kexec_crash_image;
327 if (kexec_crash_image)
328 arch_kexec_unprotect_crashkres();
331 if (flags & KEXEC_FILE_UNLOAD)
335 * In case of crash, new kernel gets loaded in reserved region. It is
336 * same memory where old crash kernel might be loaded. Free any
337 * current crash dump kernel before we corrupt it.
339 if (flags & KEXEC_FILE_ON_CRASH)
340 kimage_free(xchg(&kexec_crash_image, NULL));
342 ret = kimage_file_alloc_init(&image, kernel_fd, initrd_fd, cmdline_ptr,
347 ret = machine_kexec_prepare(image);
352 * Some architecture(like S390) may touch the crash memory before
353 * machine_kexec_prepare(), we must copy vmcoreinfo data after it.
355 ret = kimage_crash_copy_vmcoreinfo(image);
359 ret = kexec_calculate_store_digests(image);
363 for (i = 0; i < image->nr_segments; i++) {
364 struct kexec_segment *ksegment;
366 ksegment = &image->segment[i];
367 pr_debug("Loading segment %d: buf=0x%p bufsz=0x%zx mem=0x%lx memsz=0x%zx\n",
368 i, ksegment->buf, ksegment->bufsz, ksegment->mem,
371 ret = kimage_load_segment(image, &image->segment[i]);
376 kimage_terminate(image);
379 * Free up any temporary buffers allocated which are not needed
380 * after image has been loaded
382 kimage_file_post_load_cleanup(image);
384 image = xchg(dest_image, image);
386 if ((flags & KEXEC_FILE_ON_CRASH) && kexec_crash_image)
387 arch_kexec_protect_crashkres();
389 mutex_unlock(&kexec_mutex);
394 static int locate_mem_hole_top_down(unsigned long start, unsigned long end,
395 struct kexec_buf *kbuf)
397 struct kimage *image = kbuf->image;
398 unsigned long temp_start, temp_end;
400 temp_end = min(end, kbuf->buf_max);
401 temp_start = temp_end - kbuf->memsz;
404 /* align down start */
405 temp_start = temp_start & (~(kbuf->buf_align - 1));
407 if (temp_start < start || temp_start < kbuf->buf_min)
410 temp_end = temp_start + kbuf->memsz - 1;
413 * Make sure this does not conflict with any of existing
416 if (kimage_is_destination_range(image, temp_start, temp_end)) {
417 temp_start = temp_start - PAGE_SIZE;
421 /* We found a suitable memory range */
425 /* If we are here, we found a suitable memory range */
426 kbuf->mem = temp_start;
428 /* Success, stop navigating through remaining System RAM ranges */
432 static int locate_mem_hole_bottom_up(unsigned long start, unsigned long end,
433 struct kexec_buf *kbuf)
435 struct kimage *image = kbuf->image;
436 unsigned long temp_start, temp_end;
438 temp_start = max(start, kbuf->buf_min);
441 temp_start = ALIGN(temp_start, kbuf->buf_align);
442 temp_end = temp_start + kbuf->memsz - 1;
444 if (temp_end > end || temp_end > kbuf->buf_max)
447 * Make sure this does not conflict with any of existing
450 if (kimage_is_destination_range(image, temp_start, temp_end)) {
451 temp_start = temp_start + PAGE_SIZE;
455 /* We found a suitable memory range */
459 /* If we are here, we found a suitable memory range */
460 kbuf->mem = temp_start;
462 /* Success, stop navigating through remaining System RAM ranges */
466 static int locate_mem_hole_callback(struct resource *res, void *arg)
468 struct kexec_buf *kbuf = (struct kexec_buf *)arg;
469 u64 start = res->start, end = res->end;
470 unsigned long sz = end - start + 1;
472 /* Returning 0 will take to next memory range */
473 if (sz < kbuf->memsz)
476 if (end < kbuf->buf_min || start > kbuf->buf_max)
480 * Allocate memory top down with-in ram range. Otherwise bottom up
484 return locate_mem_hole_top_down(start, end, kbuf);
485 return locate_mem_hole_bottom_up(start, end, kbuf);
489 * arch_kexec_walk_mem - call func(data) on free memory regions
490 * @kbuf: Context info for the search. Also passed to @func.
491 * @func: Function to call for each memory region.
493 * Return: The memory walk will stop when func returns a non-zero value
494 * and that value will be returned. If all free regions are visited without
495 * func returning non-zero, then zero will be returned.
497 int __weak arch_kexec_walk_mem(struct kexec_buf *kbuf,
498 int (*func)(struct resource *, void *))
500 if (kbuf->image->type == KEXEC_TYPE_CRASH)
501 return walk_iomem_res_desc(crashk_res.desc,
502 IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY,
503 crashk_res.start, crashk_res.end,
506 return walk_system_ram_res(0, ULONG_MAX, kbuf, func);
510 * kexec_locate_mem_hole - find free memory for the purgatory or the next kernel
511 * @kbuf: Parameters for the memory search.
513 * On success, kbuf->mem will have the start address of the memory region found.
515 * Return: 0 on success, negative errno on error.
517 int kexec_locate_mem_hole(struct kexec_buf *kbuf)
521 ret = arch_kexec_walk_mem(kbuf, locate_mem_hole_callback);
523 return ret == 1 ? 0 : -EADDRNOTAVAIL;
527 * kexec_add_buffer - place a buffer in a kexec segment
528 * @kbuf: Buffer contents and memory parameters.
530 * This function assumes that kexec_mutex is held.
531 * On successful return, @kbuf->mem will have the physical address of
532 * the buffer in memory.
534 * Return: 0 on success, negative errno on error.
536 int kexec_add_buffer(struct kexec_buf *kbuf)
539 struct kexec_segment *ksegment;
542 /* Currently adding segment this way is allowed only in file mode */
543 if (!kbuf->image->file_mode)
546 if (kbuf->image->nr_segments >= KEXEC_SEGMENT_MAX)
550 * Make sure we are not trying to add buffer after allocating
551 * control pages. All segments need to be placed first before
552 * any control pages are allocated. As control page allocation
553 * logic goes through list of segments to make sure there are
554 * no destination overlaps.
556 if (!list_empty(&kbuf->image->control_pages)) {
561 /* Ensure minimum alignment needed for segments. */
562 kbuf->memsz = ALIGN(kbuf->memsz, PAGE_SIZE);
563 kbuf->buf_align = max(kbuf->buf_align, PAGE_SIZE);
565 /* Walk the RAM ranges and allocate a suitable range for the buffer */
566 ret = kexec_locate_mem_hole(kbuf);
570 /* Found a suitable memory range */
571 ksegment = &kbuf->image->segment[kbuf->image->nr_segments];
572 ksegment->kbuf = kbuf->buffer;
573 ksegment->bufsz = kbuf->bufsz;
574 ksegment->mem = kbuf->mem;
575 ksegment->memsz = kbuf->memsz;
576 kbuf->image->nr_segments++;
580 /* Calculate and store the digest of segments */
581 static int kexec_calculate_store_digests(struct kimage *image)
583 struct crypto_shash *tfm;
584 struct shash_desc *desc;
585 int ret = 0, i, j, zero_buf_sz, sha_region_sz;
586 size_t desc_size, nullsz;
589 struct kexec_sha_region *sha_regions;
590 struct purgatory_info *pi = &image->purgatory_info;
592 if (!IS_ENABLED(CONFIG_ARCH_HAS_KEXEC_PURGATORY))
595 zero_buf = __va(page_to_pfn(ZERO_PAGE(0)) << PAGE_SHIFT);
596 zero_buf_sz = PAGE_SIZE;
598 tfm = crypto_alloc_shash("sha256", 0, 0);
604 desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
605 desc = kzalloc(desc_size, GFP_KERNEL);
611 sha_region_sz = KEXEC_SEGMENT_MAX * sizeof(struct kexec_sha_region);
612 sha_regions = vzalloc(sha_region_sz);
619 ret = crypto_shash_init(desc);
621 goto out_free_sha_regions;
623 digest = kzalloc(SHA256_DIGEST_SIZE, GFP_KERNEL);
626 goto out_free_sha_regions;
629 for (j = i = 0; i < image->nr_segments; i++) {
630 struct kexec_segment *ksegment;
632 ksegment = &image->segment[i];
634 * Skip purgatory as it will be modified once we put digest
637 if (ksegment->kbuf == pi->purgatory_buf)
640 ret = crypto_shash_update(desc, ksegment->kbuf,
646 * Assume rest of the buffer is filled with zero and
647 * update digest accordingly.
649 nullsz = ksegment->memsz - ksegment->bufsz;
651 unsigned long bytes = nullsz;
653 if (bytes > zero_buf_sz)
655 ret = crypto_shash_update(desc, zero_buf, bytes);
664 sha_regions[j].start = ksegment->mem;
665 sha_regions[j].len = ksegment->memsz;
670 ret = crypto_shash_final(desc, digest);
672 goto out_free_digest;
673 ret = kexec_purgatory_get_set_symbol(image, "purgatory_sha_regions",
674 sha_regions, sha_region_sz, 0);
676 goto out_free_digest;
678 ret = kexec_purgatory_get_set_symbol(image, "purgatory_sha256_digest",
679 digest, SHA256_DIGEST_SIZE, 0);
681 goto out_free_digest;
686 out_free_sha_regions:
696 #ifdef CONFIG_ARCH_HAS_KEXEC_PURGATORY
697 /* Actually load purgatory. Lot of code taken from kexec-tools */
698 static int __kexec_load_purgatory(struct kimage *image, unsigned long min,
699 unsigned long max, int top_down)
701 struct purgatory_info *pi = &image->purgatory_info;
702 unsigned long align, bss_align, bss_sz, bss_pad;
703 unsigned long entry, load_addr, curr_load_addr, bss_addr, offset;
704 unsigned char *buf_addr, *src;
705 int i, ret = 0, entry_sidx = -1;
706 const Elf_Shdr *sechdrs_c;
707 Elf_Shdr *sechdrs = NULL;
708 struct kexec_buf kbuf = { .image = image, .bufsz = 0, .buf_align = 1,
709 .buf_min = min, .buf_max = max,
710 .top_down = top_down };
713 * sechdrs_c points to section headers in purgatory and are read
714 * only. No modifications allowed.
716 sechdrs_c = (void *)pi->ehdr + pi->ehdr->e_shoff;
719 * We can not modify sechdrs_c[] and its fields. It is read only.
720 * Copy it over to a local copy where one can store some temporary
721 * data and free it at the end. We need to modify ->sh_addr and
722 * ->sh_offset fields to keep track of permanent and temporary
723 * locations of sections.
725 sechdrs = vzalloc(pi->ehdr->e_shnum * sizeof(Elf_Shdr));
729 memcpy(sechdrs, sechdrs_c, pi->ehdr->e_shnum * sizeof(Elf_Shdr));
732 * We seem to have multiple copies of sections. First copy is which
733 * is embedded in kernel in read only section. Some of these sections
734 * will be copied to a temporary buffer and relocated. And these
735 * sections will finally be copied to their final destination at
738 * Use ->sh_offset to reflect section address in memory. It will
739 * point to original read only copy if section is not allocatable.
740 * Otherwise it will point to temporary copy which will be relocated.
742 * Use ->sh_addr to contain final address of the section where it
743 * will go during execution time.
745 for (i = 0; i < pi->ehdr->e_shnum; i++) {
746 if (sechdrs[i].sh_type == SHT_NOBITS)
749 sechdrs[i].sh_offset = (unsigned long)pi->ehdr +
750 sechdrs[i].sh_offset;
754 * Identify entry point section and make entry relative to section
757 entry = pi->ehdr->e_entry;
758 for (i = 0; i < pi->ehdr->e_shnum; i++) {
759 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
762 if (!(sechdrs[i].sh_flags & SHF_EXECINSTR))
765 /* Make entry section relative */
766 if (sechdrs[i].sh_addr <= pi->ehdr->e_entry &&
767 ((sechdrs[i].sh_addr + sechdrs[i].sh_size) >
768 pi->ehdr->e_entry)) {
770 entry -= sechdrs[i].sh_addr;
775 /* Determine how much memory is needed to load relocatable object. */
779 for (i = 0; i < pi->ehdr->e_shnum; i++) {
780 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
783 align = sechdrs[i].sh_addralign;
784 if (sechdrs[i].sh_type != SHT_NOBITS) {
785 if (kbuf.buf_align < align)
786 kbuf.buf_align = align;
787 kbuf.bufsz = ALIGN(kbuf.bufsz, align);
788 kbuf.bufsz += sechdrs[i].sh_size;
791 if (bss_align < align)
793 bss_sz = ALIGN(bss_sz, align);
794 bss_sz += sechdrs[i].sh_size;
798 /* Determine the bss padding required to align bss properly */
800 if (kbuf.bufsz & (bss_align - 1))
801 bss_pad = bss_align - (kbuf.bufsz & (bss_align - 1));
803 kbuf.memsz = kbuf.bufsz + bss_pad + bss_sz;
805 /* Allocate buffer for purgatory */
806 kbuf.buffer = vzalloc(kbuf.bufsz);
812 if (kbuf.buf_align < bss_align)
813 kbuf.buf_align = bss_align;
815 /* Add buffer to segment list */
816 ret = kexec_add_buffer(&kbuf);
819 pi->purgatory_load_addr = kbuf.mem;
821 /* Load SHF_ALLOC sections */
822 buf_addr = kbuf.buffer;
823 load_addr = curr_load_addr = pi->purgatory_load_addr;
824 bss_addr = load_addr + kbuf.bufsz + bss_pad;
826 for (i = 0; i < pi->ehdr->e_shnum; i++) {
827 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
830 align = sechdrs[i].sh_addralign;
831 if (sechdrs[i].sh_type != SHT_NOBITS) {
832 curr_load_addr = ALIGN(curr_load_addr, align);
833 offset = curr_load_addr - load_addr;
834 /* We already modifed ->sh_offset to keep src addr */
835 src = (char *) sechdrs[i].sh_offset;
836 memcpy(buf_addr + offset, src, sechdrs[i].sh_size);
838 /* Store load address and source address of section */
839 sechdrs[i].sh_addr = curr_load_addr;
842 * This section got copied to temporary buffer. Update
843 * ->sh_offset accordingly.
845 sechdrs[i].sh_offset = (unsigned long)(buf_addr + offset);
847 /* Advance to the next address */
848 curr_load_addr += sechdrs[i].sh_size;
850 bss_addr = ALIGN(bss_addr, align);
851 sechdrs[i].sh_addr = bss_addr;
852 bss_addr += sechdrs[i].sh_size;
856 /* Update entry point based on load address of text section */
858 entry += sechdrs[entry_sidx].sh_addr;
860 /* Make kernel jump to purgatory after shutdown */
861 image->start = entry;
863 /* Used later to get/set symbol values */
864 pi->sechdrs = sechdrs;
867 * Used later to identify which section is purgatory and skip it
870 pi->purgatory_buf = kbuf.buffer;
878 static int kexec_apply_relocations(struct kimage *image)
881 struct purgatory_info *pi = &image->purgatory_info;
882 Elf_Shdr *sechdrs = pi->sechdrs;
884 /* Apply relocations */
885 for (i = 0; i < pi->ehdr->e_shnum; i++) {
886 Elf_Shdr *section, *symtab;
888 if (sechdrs[i].sh_type != SHT_RELA &&
889 sechdrs[i].sh_type != SHT_REL)
893 * For section of type SHT_RELA/SHT_REL,
894 * ->sh_link contains section header index of associated
895 * symbol table. And ->sh_info contains section header
896 * index of section to which relocations apply.
898 if (sechdrs[i].sh_info >= pi->ehdr->e_shnum ||
899 sechdrs[i].sh_link >= pi->ehdr->e_shnum)
902 section = &sechdrs[sechdrs[i].sh_info];
903 symtab = &sechdrs[sechdrs[i].sh_link];
905 if (!(section->sh_flags & SHF_ALLOC))
909 * symtab->sh_link contain section header index of associated
912 if (symtab->sh_link >= pi->ehdr->e_shnum)
913 /* Invalid section number? */
917 * Respective architecture needs to provide support for applying
918 * relocations of type SHT_RELA/SHT_REL.
920 if (sechdrs[i].sh_type == SHT_RELA)
921 ret = arch_kexec_apply_relocations_add(pi->ehdr,
923 else if (sechdrs[i].sh_type == SHT_REL)
924 ret = arch_kexec_apply_relocations(pi->ehdr,
933 /* Load relocatable purgatory object and relocate it appropriately */
934 int kexec_load_purgatory(struct kimage *image, unsigned long min,
935 unsigned long max, int top_down,
936 unsigned long *load_addr)
938 struct purgatory_info *pi = &image->purgatory_info;
941 if (kexec_purgatory_size <= 0)
944 pi->ehdr = (const Elf_Ehdr *)kexec_purgatory;
946 ret = __kexec_load_purgatory(image, min, max, top_down);
950 ret = kexec_apply_relocations(image);
954 *load_addr = pi->purgatory_load_addr;
960 vfree(pi->purgatory_buf);
961 pi->purgatory_buf = NULL;
966 * kexec_purgatory_find_symbol - find a symbol in the purgatory
967 * @pi: Purgatory to search in.
968 * @name: Name of the symbol.
970 * Return: pointer to symbol in read-only symtab on success, NULL on error.
972 static const Elf_Sym *kexec_purgatory_find_symbol(struct purgatory_info *pi,
975 const Elf_Shdr *sechdrs;
976 const Elf_Ehdr *ehdr;
985 sechdrs = (void *)ehdr + ehdr->e_shoff;
987 for (i = 0; i < ehdr->e_shnum; i++) {
988 if (sechdrs[i].sh_type != SHT_SYMTAB)
991 if (sechdrs[i].sh_link >= ehdr->e_shnum)
992 /* Invalid strtab section number */
994 strtab = (void *)ehdr + sechdrs[sechdrs[i].sh_link].sh_offset;
995 syms = (void *)ehdr + sechdrs[i].sh_offset;
997 /* Go through symbols for a match */
998 for (k = 0; k < sechdrs[i].sh_size/sizeof(Elf_Sym); k++) {
999 if (ELF_ST_BIND(syms[k].st_info) != STB_GLOBAL)
1002 if (strcmp(strtab + syms[k].st_name, name) != 0)
1005 if (syms[k].st_shndx == SHN_UNDEF ||
1006 syms[k].st_shndx >= ehdr->e_shnum) {
1007 pr_debug("Symbol: %s has bad section index %d.\n",
1008 name, syms[k].st_shndx);
1012 /* Found the symbol we are looking for */
1020 void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name)
1022 struct purgatory_info *pi = &image->purgatory_info;
1026 sym = kexec_purgatory_find_symbol(pi, name);
1028 return ERR_PTR(-EINVAL);
1030 sechdr = &pi->sechdrs[sym->st_shndx];
1033 * Returns the address where symbol will finally be loaded after
1034 * kexec_load_segment()
1036 return (void *)(sechdr->sh_addr + sym->st_value);
1040 * Get or set value of a symbol. If "get_value" is true, symbol value is
1041 * returned in buf otherwise symbol value is set based on value in buf.
1043 int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name,
1044 void *buf, unsigned int size, bool get_value)
1046 struct purgatory_info *pi = &image->purgatory_info;
1051 sym = kexec_purgatory_find_symbol(pi, name);
1055 if (sym->st_size != size) {
1056 pr_err("symbol %s size mismatch: expected %lu actual %u\n",
1057 name, (unsigned long)sym->st_size, size);
1061 sec = pi->sechdrs + sym->st_shndx;
1063 if (sec->sh_type == SHT_NOBITS) {
1064 pr_err("symbol %s is in a bss section. Cannot %s\n", name,
1065 get_value ? "get" : "set");
1069 sym_buf = (char *)sec->sh_offset + sym->st_value;
1072 memcpy((void *)buf, sym_buf, size);
1074 memcpy((void *)sym_buf, buf, size);
1078 #endif /* CONFIG_ARCH_HAS_KEXEC_PURGATORY */
1080 int crash_exclude_mem_range(struct crash_mem *mem,
1081 unsigned long long mstart, unsigned long long mend)
1084 unsigned long long start, end;
1085 struct crash_mem_range temp_range = {0, 0};
1087 for (i = 0; i < mem->nr_ranges; i++) {
1088 start = mem->ranges[i].start;
1089 end = mem->ranges[i].end;
1091 if (mstart > end || mend < start)
1094 /* Truncate any area outside of range */
1100 /* Found completely overlapping range */
1101 if (mstart == start && mend == end) {
1102 mem->ranges[i].start = 0;
1103 mem->ranges[i].end = 0;
1104 if (i < mem->nr_ranges - 1) {
1105 /* Shift rest of the ranges to left */
1106 for (j = i; j < mem->nr_ranges - 1; j++) {
1107 mem->ranges[j].start =
1108 mem->ranges[j+1].start;
1109 mem->ranges[j].end =
1110 mem->ranges[j+1].end;
1117 if (mstart > start && mend < end) {
1118 /* Split original range */
1119 mem->ranges[i].end = mstart - 1;
1120 temp_range.start = mend + 1;
1121 temp_range.end = end;
1122 } else if (mstart != start)
1123 mem->ranges[i].end = mstart - 1;
1125 mem->ranges[i].start = mend + 1;
1129 /* If a split happened, add the split to array */
1130 if (!temp_range.end)
1133 /* Split happened */
1134 if (i == mem->max_nr_ranges - 1)
1137 /* Location where new range should go */
1139 if (j < mem->nr_ranges) {
1140 /* Move over all ranges one slot towards the end */
1141 for (i = mem->nr_ranges - 1; i >= j; i--)
1142 mem->ranges[i + 1] = mem->ranges[i];
1145 mem->ranges[j].start = temp_range.start;
1146 mem->ranges[j].end = temp_range.end;
1151 int crash_prepare_elf64_headers(struct crash_mem *mem, int kernel_map,
1152 void **addr, unsigned long *sz)
1156 unsigned long nr_cpus = num_possible_cpus(), nr_phdr, elf_sz;
1158 unsigned int cpu, i;
1159 unsigned long long notes_addr;
1160 unsigned long mstart, mend;
1162 /* extra phdr for vmcoreinfo elf note */
1163 nr_phdr = nr_cpus + 1;
1164 nr_phdr += mem->nr_ranges;
1167 * kexec-tools creates an extra PT_LOAD phdr for kernel text mapping
1168 * area (for example, ffffffff80000000 - ffffffffa0000000 on x86_64).
1169 * I think this is required by tools like gdb. So same physical
1170 * memory will be mapped in two elf headers. One will contain kernel
1171 * text virtual addresses and other will have __va(physical) addresses.
1175 elf_sz = sizeof(Elf64_Ehdr) + nr_phdr * sizeof(Elf64_Phdr);
1176 elf_sz = ALIGN(elf_sz, ELF_CORE_HEADER_ALIGN);
1178 buf = vzalloc(elf_sz);
1182 ehdr = (Elf64_Ehdr *)buf;
1183 phdr = (Elf64_Phdr *)(ehdr + 1);
1184 memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
1185 ehdr->e_ident[EI_CLASS] = ELFCLASS64;
1186 ehdr->e_ident[EI_DATA] = ELFDATA2LSB;
1187 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
1188 ehdr->e_ident[EI_OSABI] = ELF_OSABI;
1189 memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD);
1190 ehdr->e_type = ET_CORE;
1191 ehdr->e_machine = ELF_ARCH;
1192 ehdr->e_version = EV_CURRENT;
1193 ehdr->e_phoff = sizeof(Elf64_Ehdr);
1194 ehdr->e_ehsize = sizeof(Elf64_Ehdr);
1195 ehdr->e_phentsize = sizeof(Elf64_Phdr);
1197 /* Prepare one phdr of type PT_NOTE for each present cpu */
1198 for_each_present_cpu(cpu) {
1199 phdr->p_type = PT_NOTE;
1200 notes_addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpu));
1201 phdr->p_offset = phdr->p_paddr = notes_addr;
1202 phdr->p_filesz = phdr->p_memsz = sizeof(note_buf_t);
1207 /* Prepare one PT_NOTE header for vmcoreinfo */
1208 phdr->p_type = PT_NOTE;
1209 phdr->p_offset = phdr->p_paddr = paddr_vmcoreinfo_note();
1210 phdr->p_filesz = phdr->p_memsz = VMCOREINFO_NOTE_SIZE;
1214 /* Prepare PT_LOAD type program header for kernel text region */
1216 phdr->p_type = PT_LOAD;
1217 phdr->p_flags = PF_R|PF_W|PF_X;
1218 phdr->p_vaddr = (Elf64_Addr)_text;
1219 phdr->p_filesz = phdr->p_memsz = _end - _text;
1220 phdr->p_offset = phdr->p_paddr = __pa_symbol(_text);
1225 /* Go through all the ranges in mem->ranges[] and prepare phdr */
1226 for (i = 0; i < mem->nr_ranges; i++) {
1227 mstart = mem->ranges[i].start;
1228 mend = mem->ranges[i].end;
1230 phdr->p_type = PT_LOAD;
1231 phdr->p_flags = PF_R|PF_W|PF_X;
1232 phdr->p_offset = mstart;
1234 phdr->p_paddr = mstart;
1235 phdr->p_vaddr = (unsigned long long) __va(mstart);
1236 phdr->p_filesz = phdr->p_memsz = mend - mstart + 1;
1240 pr_debug("Crash PT_LOAD elf header. phdr=%p vaddr=0x%llx, paddr=0x%llx, sz=0x%llx e_phnum=%d p_offset=0x%llx\n",
1241 phdr, phdr->p_vaddr, phdr->p_paddr, phdr->p_filesz,
1242 ehdr->e_phnum, phdr->p_offset);