1 // SPDX-License-Identifier: GPL-2.0-only
3 * kexec: kexec_file_load system call
5 * Copyright (C) 2014 Red Hat Inc.
7 * Vivek Goyal <vgoyal@redhat.com>
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/capability.h>
14 #include <linux/file.h>
15 #include <linux/slab.h>
16 #include <linux/kexec.h>
17 #include <linux/memblock.h>
18 #include <linux/mutex.h>
19 #include <linux/list.h>
21 #include <linux/ima.h>
22 #include <crypto/hash.h>
23 #include <crypto/sha.h>
24 #include <linux/elf.h>
25 #include <linux/elfcore.h>
26 #include <linux/kernel.h>
27 #include <linux/syscalls.h>
28 #include <linux/vmalloc.h>
29 #include "kexec_internal.h"
31 static int kexec_calculate_store_digests(struct kimage *image);
34 * Currently this is the only default function that is exported as some
35 * architectures need it to do additional handlings.
36 * In the future, other default functions may be exported too if required.
38 int kexec_image_probe_default(struct kimage *image, void *buf,
39 unsigned long buf_len)
41 const struct kexec_file_ops * const *fops;
44 for (fops = &kexec_file_loaders[0]; *fops && (*fops)->probe; ++fops) {
45 ret = (*fops)->probe(buf, buf_len);
55 /* Architectures can provide this probe function */
56 int __weak arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
57 unsigned long buf_len)
59 return kexec_image_probe_default(image, buf, buf_len);
62 static void *kexec_image_load_default(struct kimage *image)
64 if (!image->fops || !image->fops->load)
65 return ERR_PTR(-ENOEXEC);
67 return image->fops->load(image, image->kernel_buf,
68 image->kernel_buf_len, image->initrd_buf,
69 image->initrd_buf_len, image->cmdline_buf,
70 image->cmdline_buf_len);
73 void * __weak arch_kexec_kernel_image_load(struct kimage *image)
75 return kexec_image_load_default(image);
78 int kexec_image_post_load_cleanup_default(struct kimage *image)
80 if (!image->fops || !image->fops->cleanup)
83 return image->fops->cleanup(image->image_loader_data);
86 int __weak arch_kimage_file_post_load_cleanup(struct kimage *image)
88 return kexec_image_post_load_cleanup_default(image);
91 #ifdef CONFIG_KEXEC_VERIFY_SIG
92 static int kexec_image_verify_sig_default(struct kimage *image, void *buf,
93 unsigned long buf_len)
95 if (!image->fops || !image->fops->verify_sig) {
96 pr_debug("kernel loader does not support signature verification.\n");
100 return image->fops->verify_sig(buf, buf_len);
103 int __weak arch_kexec_kernel_verify_sig(struct kimage *image, void *buf,
104 unsigned long buf_len)
106 return kexec_image_verify_sig_default(image, buf, buf_len);
111 * arch_kexec_apply_relocations_add - apply relocations of type RELA
112 * @pi: Purgatory to be relocated.
113 * @section: Section relocations applying to.
114 * @relsec: Section containing RELAs.
115 * @symtab: Corresponding symtab.
117 * Return: 0 on success, negative errno on error.
120 arch_kexec_apply_relocations_add(struct purgatory_info *pi, Elf_Shdr *section,
121 const Elf_Shdr *relsec, const Elf_Shdr *symtab)
123 pr_err("RELA relocation unsupported.\n");
128 * arch_kexec_apply_relocations - apply relocations of type REL
129 * @pi: Purgatory to be relocated.
130 * @section: Section relocations applying to.
131 * @relsec: Section containing RELs.
132 * @symtab: Corresponding symtab.
134 * Return: 0 on success, negative errno on error.
137 arch_kexec_apply_relocations(struct purgatory_info *pi, Elf_Shdr *section,
138 const Elf_Shdr *relsec, const Elf_Shdr *symtab)
140 pr_err("REL relocation unsupported.\n");
145 * Free up memory used by kernel, initrd, and command line. This is temporary
146 * memory allocation which is not needed any more after these buffers have
147 * been loaded into separate segments and have been copied elsewhere.
149 void kimage_file_post_load_cleanup(struct kimage *image)
151 struct purgatory_info *pi = &image->purgatory_info;
153 vfree(image->kernel_buf);
154 image->kernel_buf = NULL;
156 vfree(image->initrd_buf);
157 image->initrd_buf = NULL;
159 kfree(image->cmdline_buf);
160 image->cmdline_buf = NULL;
162 vfree(pi->purgatory_buf);
163 pi->purgatory_buf = NULL;
168 /* See if architecture has anything to cleanup post load */
169 arch_kimage_file_post_load_cleanup(image);
172 * Above call should have called into bootloader to free up
173 * any data stored in kimage->image_loader_data. It should
174 * be ok now to free it up.
176 kfree(image->image_loader_data);
177 image->image_loader_data = NULL;
181 * In file mode list of segments is prepared by kernel. Copy relevant
182 * data from user space, do error checking, prepare segment list
185 kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd,
186 const char __user *cmdline_ptr,
187 unsigned long cmdline_len, unsigned flags)
193 ret = kernel_read_file_from_fd(kernel_fd, &image->kernel_buf,
194 &size, INT_MAX, READING_KEXEC_IMAGE);
197 image->kernel_buf_len = size;
199 /* IMA needs to pass the measurement list to the next kernel. */
200 ima_add_kexec_buffer(image);
202 /* Call arch image probe handlers */
203 ret = arch_kexec_kernel_image_probe(image, image->kernel_buf,
204 image->kernel_buf_len);
208 #ifdef CONFIG_KEXEC_VERIFY_SIG
209 ret = arch_kexec_kernel_verify_sig(image, image->kernel_buf,
210 image->kernel_buf_len);
212 pr_debug("kernel signature verification failed.\n");
215 pr_debug("kernel signature verification successful.\n");
217 /* It is possible that there no initramfs is being loaded */
218 if (!(flags & KEXEC_FILE_NO_INITRAMFS)) {
219 ret = kernel_read_file_from_fd(initrd_fd, &image->initrd_buf,
221 READING_KEXEC_INITRAMFS);
224 image->initrd_buf_len = size;
228 image->cmdline_buf = memdup_user(cmdline_ptr, cmdline_len);
229 if (IS_ERR(image->cmdline_buf)) {
230 ret = PTR_ERR(image->cmdline_buf);
231 image->cmdline_buf = NULL;
235 image->cmdline_buf_len = cmdline_len;
237 /* command line should be a string with last byte null */
238 if (image->cmdline_buf[cmdline_len - 1] != '\0') {
244 /* Call arch image load handlers */
245 ldata = arch_kexec_kernel_image_load(image);
248 ret = PTR_ERR(ldata);
252 image->image_loader_data = ldata;
254 /* In case of error, free up all allocated memory in this function */
256 kimage_file_post_load_cleanup(image);
261 kimage_file_alloc_init(struct kimage **rimage, int kernel_fd,
262 int initrd_fd, const char __user *cmdline_ptr,
263 unsigned long cmdline_len, unsigned long flags)
266 struct kimage *image;
267 bool kexec_on_panic = flags & KEXEC_FILE_ON_CRASH;
269 image = do_kimage_alloc_init();
273 image->file_mode = 1;
275 if (kexec_on_panic) {
276 /* Enable special crash kernel control page alloc policy. */
277 image->control_page = crashk_res.start;
278 image->type = KEXEC_TYPE_CRASH;
281 ret = kimage_file_prepare_segments(image, kernel_fd, initrd_fd,
282 cmdline_ptr, cmdline_len, flags);
286 ret = sanity_check_segment_list(image);
288 goto out_free_post_load_bufs;
291 image->control_code_page = kimage_alloc_control_pages(image,
292 get_order(KEXEC_CONTROL_PAGE_SIZE));
293 if (!image->control_code_page) {
294 pr_err("Could not allocate control_code_buffer\n");
295 goto out_free_post_load_bufs;
298 if (!kexec_on_panic) {
299 image->swap_page = kimage_alloc_control_pages(image, 0);
300 if (!image->swap_page) {
301 pr_err("Could not allocate swap buffer\n");
302 goto out_free_control_pages;
308 out_free_control_pages:
309 kimage_free_page_list(&image->control_pages);
310 out_free_post_load_bufs:
311 kimage_file_post_load_cleanup(image);
317 SYSCALL_DEFINE5(kexec_file_load, int, kernel_fd, int, initrd_fd,
318 unsigned long, cmdline_len, const char __user *, cmdline_ptr,
319 unsigned long, flags)
322 struct kimage **dest_image, *image;
324 /* We only trust the superuser with rebooting the system. */
325 if (!capable(CAP_SYS_BOOT) || kexec_load_disabled)
328 /* Make sure we have a legal set of flags */
329 if (flags != (flags & KEXEC_FILE_FLAGS))
334 if (!mutex_trylock(&kexec_mutex))
337 dest_image = &kexec_image;
338 if (flags & KEXEC_FILE_ON_CRASH) {
339 dest_image = &kexec_crash_image;
340 if (kexec_crash_image)
341 arch_kexec_unprotect_crashkres();
344 if (flags & KEXEC_FILE_UNLOAD)
348 * In case of crash, new kernel gets loaded in reserved region. It is
349 * same memory where old crash kernel might be loaded. Free any
350 * current crash dump kernel before we corrupt it.
352 if (flags & KEXEC_FILE_ON_CRASH)
353 kimage_free(xchg(&kexec_crash_image, NULL));
355 ret = kimage_file_alloc_init(&image, kernel_fd, initrd_fd, cmdline_ptr,
360 ret = machine_kexec_prepare(image);
365 * Some architecture(like S390) may touch the crash memory before
366 * machine_kexec_prepare(), we must copy vmcoreinfo data after it.
368 ret = kimage_crash_copy_vmcoreinfo(image);
372 ret = kexec_calculate_store_digests(image);
376 for (i = 0; i < image->nr_segments; i++) {
377 struct kexec_segment *ksegment;
379 ksegment = &image->segment[i];
380 pr_debug("Loading segment %d: buf=0x%p bufsz=0x%zx mem=0x%lx memsz=0x%zx\n",
381 i, ksegment->buf, ksegment->bufsz, ksegment->mem,
384 ret = kimage_load_segment(image, &image->segment[i]);
389 kimage_terminate(image);
392 * Free up any temporary buffers allocated which are not needed
393 * after image has been loaded
395 kimage_file_post_load_cleanup(image);
397 image = xchg(dest_image, image);
399 if ((flags & KEXEC_FILE_ON_CRASH) && kexec_crash_image)
400 arch_kexec_protect_crashkres();
402 mutex_unlock(&kexec_mutex);
407 static int locate_mem_hole_top_down(unsigned long start, unsigned long end,
408 struct kexec_buf *kbuf)
410 struct kimage *image = kbuf->image;
411 unsigned long temp_start, temp_end;
413 temp_end = min(end, kbuf->buf_max);
414 temp_start = temp_end - kbuf->memsz;
417 /* align down start */
418 temp_start = temp_start & (~(kbuf->buf_align - 1));
420 if (temp_start < start || temp_start < kbuf->buf_min)
423 temp_end = temp_start + kbuf->memsz - 1;
426 * Make sure this does not conflict with any of existing
429 if (kimage_is_destination_range(image, temp_start, temp_end)) {
430 temp_start = temp_start - PAGE_SIZE;
434 /* We found a suitable memory range */
438 /* If we are here, we found a suitable memory range */
439 kbuf->mem = temp_start;
441 /* Success, stop navigating through remaining System RAM ranges */
445 static int locate_mem_hole_bottom_up(unsigned long start, unsigned long end,
446 struct kexec_buf *kbuf)
448 struct kimage *image = kbuf->image;
449 unsigned long temp_start, temp_end;
451 temp_start = max(start, kbuf->buf_min);
454 temp_start = ALIGN(temp_start, kbuf->buf_align);
455 temp_end = temp_start + kbuf->memsz - 1;
457 if (temp_end > end || temp_end > kbuf->buf_max)
460 * Make sure this does not conflict with any of existing
463 if (kimage_is_destination_range(image, temp_start, temp_end)) {
464 temp_start = temp_start + PAGE_SIZE;
468 /* We found a suitable memory range */
472 /* If we are here, we found a suitable memory range */
473 kbuf->mem = temp_start;
475 /* Success, stop navigating through remaining System RAM ranges */
479 static int locate_mem_hole_callback(struct resource *res, void *arg)
481 struct kexec_buf *kbuf = (struct kexec_buf *)arg;
482 u64 start = res->start, end = res->end;
483 unsigned long sz = end - start + 1;
485 /* Returning 0 will take to next memory range */
486 if (sz < kbuf->memsz)
489 if (end < kbuf->buf_min || start > kbuf->buf_max)
493 * Allocate memory top down with-in ram range. Otherwise bottom up
497 return locate_mem_hole_top_down(start, end, kbuf);
498 return locate_mem_hole_bottom_up(start, end, kbuf);
501 #ifdef CONFIG_ARCH_KEEP_MEMBLOCK
502 static int kexec_walk_memblock(struct kexec_buf *kbuf,
503 int (*func)(struct resource *, void *))
507 phys_addr_t mstart, mend;
508 struct resource res = { };
510 if (kbuf->image->type == KEXEC_TYPE_CRASH)
511 return func(&crashk_res, kbuf);
513 if (kbuf->top_down) {
514 for_each_free_mem_range_reverse(i, NUMA_NO_NODE, MEMBLOCK_NONE,
515 &mstart, &mend, NULL) {
517 * In memblock, end points to the first byte after the
518 * range while in kexec, end points to the last byte
523 ret = func(&res, kbuf);
528 for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE,
529 &mstart, &mend, NULL) {
531 * In memblock, end points to the first byte after the
532 * range while in kexec, end points to the last byte
537 ret = func(&res, kbuf);
546 static int kexec_walk_memblock(struct kexec_buf *kbuf,
547 int (*func)(struct resource *, void *))
554 * kexec_walk_resources - call func(data) on free memory regions
555 * @kbuf: Context info for the search. Also passed to @func.
556 * @func: Function to call for each memory region.
558 * Return: The memory walk will stop when func returns a non-zero value
559 * and that value will be returned. If all free regions are visited without
560 * func returning non-zero, then zero will be returned.
562 static int kexec_walk_resources(struct kexec_buf *kbuf,
563 int (*func)(struct resource *, void *))
565 if (kbuf->image->type == KEXEC_TYPE_CRASH)
566 return walk_iomem_res_desc(crashk_res.desc,
567 IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY,
568 crashk_res.start, crashk_res.end,
571 return walk_system_ram_res(0, ULONG_MAX, kbuf, func);
575 * kexec_locate_mem_hole - find free memory for the purgatory or the next kernel
576 * @kbuf: Parameters for the memory search.
578 * On success, kbuf->mem will have the start address of the memory region found.
580 * Return: 0 on success, negative errno on error.
582 int kexec_locate_mem_hole(struct kexec_buf *kbuf)
586 /* Arch knows where to place */
587 if (kbuf->mem != KEXEC_BUF_MEM_UNKNOWN)
590 if (!IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK))
591 ret = kexec_walk_resources(kbuf, locate_mem_hole_callback);
593 ret = kexec_walk_memblock(kbuf, locate_mem_hole_callback);
595 return ret == 1 ? 0 : -EADDRNOTAVAIL;
599 * kexec_add_buffer - place a buffer in a kexec segment
600 * @kbuf: Buffer contents and memory parameters.
602 * This function assumes that kexec_mutex is held.
603 * On successful return, @kbuf->mem will have the physical address of
604 * the buffer in memory.
606 * Return: 0 on success, negative errno on error.
608 int kexec_add_buffer(struct kexec_buf *kbuf)
611 struct kexec_segment *ksegment;
614 /* Currently adding segment this way is allowed only in file mode */
615 if (!kbuf->image->file_mode)
618 if (kbuf->image->nr_segments >= KEXEC_SEGMENT_MAX)
622 * Make sure we are not trying to add buffer after allocating
623 * control pages. All segments need to be placed first before
624 * any control pages are allocated. As control page allocation
625 * logic goes through list of segments to make sure there are
626 * no destination overlaps.
628 if (!list_empty(&kbuf->image->control_pages)) {
633 /* Ensure minimum alignment needed for segments. */
634 kbuf->memsz = ALIGN(kbuf->memsz, PAGE_SIZE);
635 kbuf->buf_align = max(kbuf->buf_align, PAGE_SIZE);
637 /* Walk the RAM ranges and allocate a suitable range for the buffer */
638 ret = kexec_locate_mem_hole(kbuf);
642 /* Found a suitable memory range */
643 ksegment = &kbuf->image->segment[kbuf->image->nr_segments];
644 ksegment->kbuf = kbuf->buffer;
645 ksegment->bufsz = kbuf->bufsz;
646 ksegment->mem = kbuf->mem;
647 ksegment->memsz = kbuf->memsz;
648 kbuf->image->nr_segments++;
652 /* Calculate and store the digest of segments */
653 static int kexec_calculate_store_digests(struct kimage *image)
655 struct crypto_shash *tfm;
656 struct shash_desc *desc;
657 int ret = 0, i, j, zero_buf_sz, sha_region_sz;
658 size_t desc_size, nullsz;
661 struct kexec_sha_region *sha_regions;
662 struct purgatory_info *pi = &image->purgatory_info;
664 if (!IS_ENABLED(CONFIG_ARCH_HAS_KEXEC_PURGATORY))
667 zero_buf = __va(page_to_pfn(ZERO_PAGE(0)) << PAGE_SHIFT);
668 zero_buf_sz = PAGE_SIZE;
670 tfm = crypto_alloc_shash("sha256", 0, 0);
676 desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
677 desc = kzalloc(desc_size, GFP_KERNEL);
683 sha_region_sz = KEXEC_SEGMENT_MAX * sizeof(struct kexec_sha_region);
684 sha_regions = vzalloc(sha_region_sz);
690 ret = crypto_shash_init(desc);
692 goto out_free_sha_regions;
694 digest = kzalloc(SHA256_DIGEST_SIZE, GFP_KERNEL);
697 goto out_free_sha_regions;
700 for (j = i = 0; i < image->nr_segments; i++) {
701 struct kexec_segment *ksegment;
703 ksegment = &image->segment[i];
705 * Skip purgatory as it will be modified once we put digest
708 if (ksegment->kbuf == pi->purgatory_buf)
711 ret = crypto_shash_update(desc, ksegment->kbuf,
717 * Assume rest of the buffer is filled with zero and
718 * update digest accordingly.
720 nullsz = ksegment->memsz - ksegment->bufsz;
722 unsigned long bytes = nullsz;
724 if (bytes > zero_buf_sz)
726 ret = crypto_shash_update(desc, zero_buf, bytes);
735 sha_regions[j].start = ksegment->mem;
736 sha_regions[j].len = ksegment->memsz;
741 ret = crypto_shash_final(desc, digest);
743 goto out_free_digest;
744 ret = kexec_purgatory_get_set_symbol(image, "purgatory_sha_regions",
745 sha_regions, sha_region_sz, 0);
747 goto out_free_digest;
749 ret = kexec_purgatory_get_set_symbol(image, "purgatory_sha256_digest",
750 digest, SHA256_DIGEST_SIZE, 0);
752 goto out_free_digest;
757 out_free_sha_regions:
767 #ifdef CONFIG_ARCH_HAS_KEXEC_PURGATORY
769 * kexec_purgatory_setup_kbuf - prepare buffer to load purgatory.
770 * @pi: Purgatory to be loaded.
771 * @kbuf: Buffer to setup.
773 * Allocates the memory needed for the buffer. Caller is responsible to free
774 * the memory after use.
776 * Return: 0 on success, negative errno on error.
778 static int kexec_purgatory_setup_kbuf(struct purgatory_info *pi,
779 struct kexec_buf *kbuf)
781 const Elf_Shdr *sechdrs;
782 unsigned long bss_align;
783 unsigned long bss_sz;
787 sechdrs = (void *)pi->ehdr + pi->ehdr->e_shoff;
788 kbuf->buf_align = bss_align = 1;
789 kbuf->bufsz = bss_sz = 0;
791 for (i = 0; i < pi->ehdr->e_shnum; i++) {
792 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
795 align = sechdrs[i].sh_addralign;
796 if (sechdrs[i].sh_type != SHT_NOBITS) {
797 if (kbuf->buf_align < align)
798 kbuf->buf_align = align;
799 kbuf->bufsz = ALIGN(kbuf->bufsz, align);
800 kbuf->bufsz += sechdrs[i].sh_size;
802 if (bss_align < align)
804 bss_sz = ALIGN(bss_sz, align);
805 bss_sz += sechdrs[i].sh_size;
808 kbuf->bufsz = ALIGN(kbuf->bufsz, bss_align);
809 kbuf->memsz = kbuf->bufsz + bss_sz;
810 if (kbuf->buf_align < bss_align)
811 kbuf->buf_align = bss_align;
813 kbuf->buffer = vzalloc(kbuf->bufsz);
816 pi->purgatory_buf = kbuf->buffer;
818 ret = kexec_add_buffer(kbuf);
824 vfree(pi->purgatory_buf);
825 pi->purgatory_buf = NULL;
830 * kexec_purgatory_setup_sechdrs - prepares the pi->sechdrs buffer.
831 * @pi: Purgatory to be loaded.
832 * @kbuf: Buffer prepared to store purgatory.
834 * Allocates the memory needed for the buffer. Caller is responsible to free
835 * the memory after use.
837 * Return: 0 on success, negative errno on error.
839 static int kexec_purgatory_setup_sechdrs(struct purgatory_info *pi,
840 struct kexec_buf *kbuf)
842 unsigned long bss_addr;
843 unsigned long offset;
848 * The section headers in kexec_purgatory are read-only. In order to
849 * have them modifiable make a temporary copy.
851 sechdrs = vzalloc(array_size(sizeof(Elf_Shdr), pi->ehdr->e_shnum));
854 memcpy(sechdrs, (void *)pi->ehdr + pi->ehdr->e_shoff,
855 pi->ehdr->e_shnum * sizeof(Elf_Shdr));
856 pi->sechdrs = sechdrs;
859 bss_addr = kbuf->mem + kbuf->bufsz;
860 kbuf->image->start = pi->ehdr->e_entry;
862 for (i = 0; i < pi->ehdr->e_shnum; i++) {
866 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
869 align = sechdrs[i].sh_addralign;
870 if (sechdrs[i].sh_type == SHT_NOBITS) {
871 bss_addr = ALIGN(bss_addr, align);
872 sechdrs[i].sh_addr = bss_addr;
873 bss_addr += sechdrs[i].sh_size;
877 offset = ALIGN(offset, align);
878 if (sechdrs[i].sh_flags & SHF_EXECINSTR &&
879 pi->ehdr->e_entry >= sechdrs[i].sh_addr &&
880 pi->ehdr->e_entry < (sechdrs[i].sh_addr
881 + sechdrs[i].sh_size)) {
882 kbuf->image->start -= sechdrs[i].sh_addr;
883 kbuf->image->start += kbuf->mem + offset;
886 src = (void *)pi->ehdr + sechdrs[i].sh_offset;
887 dst = pi->purgatory_buf + offset;
888 memcpy(dst, src, sechdrs[i].sh_size);
890 sechdrs[i].sh_addr = kbuf->mem + offset;
891 sechdrs[i].sh_offset = offset;
892 offset += sechdrs[i].sh_size;
898 static int kexec_apply_relocations(struct kimage *image)
901 struct purgatory_info *pi = &image->purgatory_info;
902 const Elf_Shdr *sechdrs;
904 sechdrs = (void *)pi->ehdr + pi->ehdr->e_shoff;
906 for (i = 0; i < pi->ehdr->e_shnum; i++) {
907 const Elf_Shdr *relsec;
908 const Elf_Shdr *symtab;
911 relsec = sechdrs + i;
913 if (relsec->sh_type != SHT_RELA &&
914 relsec->sh_type != SHT_REL)
918 * For section of type SHT_RELA/SHT_REL,
919 * ->sh_link contains section header index of associated
920 * symbol table. And ->sh_info contains section header
921 * index of section to which relocations apply.
923 if (relsec->sh_info >= pi->ehdr->e_shnum ||
924 relsec->sh_link >= pi->ehdr->e_shnum)
927 section = pi->sechdrs + relsec->sh_info;
928 symtab = sechdrs + relsec->sh_link;
930 if (!(section->sh_flags & SHF_ALLOC))
934 * symtab->sh_link contain section header index of associated
937 if (symtab->sh_link >= pi->ehdr->e_shnum)
938 /* Invalid section number? */
942 * Respective architecture needs to provide support for applying
943 * relocations of type SHT_RELA/SHT_REL.
945 if (relsec->sh_type == SHT_RELA)
946 ret = arch_kexec_apply_relocations_add(pi, section,
948 else if (relsec->sh_type == SHT_REL)
949 ret = arch_kexec_apply_relocations(pi, section,
959 * kexec_load_purgatory - Load and relocate the purgatory object.
960 * @image: Image to add the purgatory to.
961 * @kbuf: Memory parameters to use.
963 * Allocates the memory needed for image->purgatory_info.sechdrs and
964 * image->purgatory_info.purgatory_buf/kbuf->buffer. Caller is responsible
965 * to free the memory after use.
967 * Return: 0 on success, negative errno on error.
969 int kexec_load_purgatory(struct kimage *image, struct kexec_buf *kbuf)
971 struct purgatory_info *pi = &image->purgatory_info;
974 if (kexec_purgatory_size <= 0)
977 pi->ehdr = (const Elf_Ehdr *)kexec_purgatory;
979 ret = kexec_purgatory_setup_kbuf(pi, kbuf);
983 ret = kexec_purgatory_setup_sechdrs(pi, kbuf);
987 ret = kexec_apply_relocations(image);
996 vfree(pi->purgatory_buf);
997 pi->purgatory_buf = NULL;
1002 * kexec_purgatory_find_symbol - find a symbol in the purgatory
1003 * @pi: Purgatory to search in.
1004 * @name: Name of the symbol.
1006 * Return: pointer to symbol in read-only symtab on success, NULL on error.
1008 static const Elf_Sym *kexec_purgatory_find_symbol(struct purgatory_info *pi,
1011 const Elf_Shdr *sechdrs;
1012 const Elf_Ehdr *ehdr;
1013 const Elf_Sym *syms;
1021 sechdrs = (void *)ehdr + ehdr->e_shoff;
1023 for (i = 0; i < ehdr->e_shnum; i++) {
1024 if (sechdrs[i].sh_type != SHT_SYMTAB)
1027 if (sechdrs[i].sh_link >= ehdr->e_shnum)
1028 /* Invalid strtab section number */
1030 strtab = (void *)ehdr + sechdrs[sechdrs[i].sh_link].sh_offset;
1031 syms = (void *)ehdr + sechdrs[i].sh_offset;
1033 /* Go through symbols for a match */
1034 for (k = 0; k < sechdrs[i].sh_size/sizeof(Elf_Sym); k++) {
1035 if (ELF_ST_BIND(syms[k].st_info) != STB_GLOBAL)
1038 if (strcmp(strtab + syms[k].st_name, name) != 0)
1041 if (syms[k].st_shndx == SHN_UNDEF ||
1042 syms[k].st_shndx >= ehdr->e_shnum) {
1043 pr_debug("Symbol: %s has bad section index %d.\n",
1044 name, syms[k].st_shndx);
1048 /* Found the symbol we are looking for */
1056 void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name)
1058 struct purgatory_info *pi = &image->purgatory_info;
1062 sym = kexec_purgatory_find_symbol(pi, name);
1064 return ERR_PTR(-EINVAL);
1066 sechdr = &pi->sechdrs[sym->st_shndx];
1069 * Returns the address where symbol will finally be loaded after
1070 * kexec_load_segment()
1072 return (void *)(sechdr->sh_addr + sym->st_value);
1076 * Get or set value of a symbol. If "get_value" is true, symbol value is
1077 * returned in buf otherwise symbol value is set based on value in buf.
1079 int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name,
1080 void *buf, unsigned int size, bool get_value)
1082 struct purgatory_info *pi = &image->purgatory_info;
1087 sym = kexec_purgatory_find_symbol(pi, name);
1091 if (sym->st_size != size) {
1092 pr_err("symbol %s size mismatch: expected %lu actual %u\n",
1093 name, (unsigned long)sym->st_size, size);
1097 sec = pi->sechdrs + sym->st_shndx;
1099 if (sec->sh_type == SHT_NOBITS) {
1100 pr_err("symbol %s is in a bss section. Cannot %s\n", name,
1101 get_value ? "get" : "set");
1105 sym_buf = (char *)pi->purgatory_buf + sec->sh_offset + sym->st_value;
1108 memcpy((void *)buf, sym_buf, size);
1110 memcpy((void *)sym_buf, buf, size);
1114 #endif /* CONFIG_ARCH_HAS_KEXEC_PURGATORY */
1116 int crash_exclude_mem_range(struct crash_mem *mem,
1117 unsigned long long mstart, unsigned long long mend)
1120 unsigned long long start, end;
1121 struct crash_mem_range temp_range = {0, 0};
1123 for (i = 0; i < mem->nr_ranges; i++) {
1124 start = mem->ranges[i].start;
1125 end = mem->ranges[i].end;
1127 if (mstart > end || mend < start)
1130 /* Truncate any area outside of range */
1136 /* Found completely overlapping range */
1137 if (mstart == start && mend == end) {
1138 mem->ranges[i].start = 0;
1139 mem->ranges[i].end = 0;
1140 if (i < mem->nr_ranges - 1) {
1141 /* Shift rest of the ranges to left */
1142 for (j = i; j < mem->nr_ranges - 1; j++) {
1143 mem->ranges[j].start =
1144 mem->ranges[j+1].start;
1145 mem->ranges[j].end =
1146 mem->ranges[j+1].end;
1153 if (mstart > start && mend < end) {
1154 /* Split original range */
1155 mem->ranges[i].end = mstart - 1;
1156 temp_range.start = mend + 1;
1157 temp_range.end = end;
1158 } else if (mstart != start)
1159 mem->ranges[i].end = mstart - 1;
1161 mem->ranges[i].start = mend + 1;
1165 /* If a split happened, add the split to array */
1166 if (!temp_range.end)
1169 /* Split happened */
1170 if (i == mem->max_nr_ranges - 1)
1173 /* Location where new range should go */
1175 if (j < mem->nr_ranges) {
1176 /* Move over all ranges one slot towards the end */
1177 for (i = mem->nr_ranges - 1; i >= j; i--)
1178 mem->ranges[i + 1] = mem->ranges[i];
1181 mem->ranges[j].start = temp_range.start;
1182 mem->ranges[j].end = temp_range.end;
1187 int crash_prepare_elf64_headers(struct crash_mem *mem, int kernel_map,
1188 void **addr, unsigned long *sz)
1192 unsigned long nr_cpus = num_possible_cpus(), nr_phdr, elf_sz;
1194 unsigned int cpu, i;
1195 unsigned long long notes_addr;
1196 unsigned long mstart, mend;
1198 /* extra phdr for vmcoreinfo elf note */
1199 nr_phdr = nr_cpus + 1;
1200 nr_phdr += mem->nr_ranges;
1203 * kexec-tools creates an extra PT_LOAD phdr for kernel text mapping
1204 * area (for example, ffffffff80000000 - ffffffffa0000000 on x86_64).
1205 * I think this is required by tools like gdb. So same physical
1206 * memory will be mapped in two elf headers. One will contain kernel
1207 * text virtual addresses and other will have __va(physical) addresses.
1211 elf_sz = sizeof(Elf64_Ehdr) + nr_phdr * sizeof(Elf64_Phdr);
1212 elf_sz = ALIGN(elf_sz, ELF_CORE_HEADER_ALIGN);
1214 buf = vzalloc(elf_sz);
1218 ehdr = (Elf64_Ehdr *)buf;
1219 phdr = (Elf64_Phdr *)(ehdr + 1);
1220 memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
1221 ehdr->e_ident[EI_CLASS] = ELFCLASS64;
1222 ehdr->e_ident[EI_DATA] = ELFDATA2LSB;
1223 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
1224 ehdr->e_ident[EI_OSABI] = ELF_OSABI;
1225 memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD);
1226 ehdr->e_type = ET_CORE;
1227 ehdr->e_machine = ELF_ARCH;
1228 ehdr->e_version = EV_CURRENT;
1229 ehdr->e_phoff = sizeof(Elf64_Ehdr);
1230 ehdr->e_ehsize = sizeof(Elf64_Ehdr);
1231 ehdr->e_phentsize = sizeof(Elf64_Phdr);
1233 /* Prepare one phdr of type PT_NOTE for each present cpu */
1234 for_each_present_cpu(cpu) {
1235 phdr->p_type = PT_NOTE;
1236 notes_addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpu));
1237 phdr->p_offset = phdr->p_paddr = notes_addr;
1238 phdr->p_filesz = phdr->p_memsz = sizeof(note_buf_t);
1243 /* Prepare one PT_NOTE header for vmcoreinfo */
1244 phdr->p_type = PT_NOTE;
1245 phdr->p_offset = phdr->p_paddr = paddr_vmcoreinfo_note();
1246 phdr->p_filesz = phdr->p_memsz = VMCOREINFO_NOTE_SIZE;
1250 /* Prepare PT_LOAD type program header for kernel text region */
1252 phdr->p_type = PT_LOAD;
1253 phdr->p_flags = PF_R|PF_W|PF_X;
1254 phdr->p_vaddr = (Elf64_Addr)_text;
1255 phdr->p_filesz = phdr->p_memsz = _end - _text;
1256 phdr->p_offset = phdr->p_paddr = __pa_symbol(_text);
1261 /* Go through all the ranges in mem->ranges[] and prepare phdr */
1262 for (i = 0; i < mem->nr_ranges; i++) {
1263 mstart = mem->ranges[i].start;
1264 mend = mem->ranges[i].end;
1266 phdr->p_type = PT_LOAD;
1267 phdr->p_flags = PF_R|PF_W|PF_X;
1268 phdr->p_offset = mstart;
1270 phdr->p_paddr = mstart;
1271 phdr->p_vaddr = (unsigned long long) __va(mstart);
1272 phdr->p_filesz = phdr->p_memsz = mend - mstart + 1;
1276 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",
1277 phdr, phdr->p_vaddr, phdr->p_paddr, phdr->p_filesz,
1278 ehdr->e_phnum, phdr->p_offset);