1 // SPDX-License-Identifier: GPL-2.0-only
3 * kexec: kexec_file_load system call
5 * Copyright (C) 2014 Red Hat Inc.
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/sha2.h>
24 #include <linux/elf.h>
25 #include <linux/elfcore.h>
26 #include <linux/kernel.h>
27 #include <linux/kernel_read_file.h>
28 #include <linux/syscalls.h>
29 #include <linux/vmalloc.h>
30 #include "kexec_internal.h"
32 static int kexec_calculate_store_digests(struct kimage *image);
35 * Currently this is the only default function that is exported as some
36 * architectures need it to do additional handlings.
37 * In the future, other default functions may be exported too if required.
39 int kexec_image_probe_default(struct kimage *image, void *buf,
40 unsigned long buf_len)
42 const struct kexec_file_ops * const *fops;
45 for (fops = &kexec_file_loaders[0]; *fops && (*fops)->probe; ++fops) {
46 ret = (*fops)->probe(buf, buf_len);
56 /* Architectures can provide this probe function */
57 int __weak arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
58 unsigned long buf_len)
60 return kexec_image_probe_default(image, buf, buf_len);
63 static void *kexec_image_load_default(struct kimage *image)
65 if (!image->fops || !image->fops->load)
66 return ERR_PTR(-ENOEXEC);
68 return image->fops->load(image, image->kernel_buf,
69 image->kernel_buf_len, image->initrd_buf,
70 image->initrd_buf_len, image->cmdline_buf,
71 image->cmdline_buf_len);
74 void * __weak arch_kexec_kernel_image_load(struct kimage *image)
76 return kexec_image_load_default(image);
79 int kexec_image_post_load_cleanup_default(struct kimage *image)
81 if (!image->fops || !image->fops->cleanup)
84 return image->fops->cleanup(image->image_loader_data);
87 int __weak arch_kimage_file_post_load_cleanup(struct kimage *image)
89 return kexec_image_post_load_cleanup_default(image);
92 #ifdef CONFIG_KEXEC_SIG
93 static int kexec_image_verify_sig_default(struct kimage *image, void *buf,
94 unsigned long buf_len)
96 if (!image->fops || !image->fops->verify_sig) {
97 pr_debug("kernel loader does not support signature verification.\n");
101 return image->fops->verify_sig(buf, buf_len);
104 int __weak arch_kexec_kernel_verify_sig(struct kimage *image, void *buf,
105 unsigned long buf_len)
107 return kexec_image_verify_sig_default(image, buf, buf_len);
112 * Free up memory used by kernel, initrd, and command line. This is temporary
113 * memory allocation which is not needed any more after these buffers have
114 * been loaded into separate segments and have been copied elsewhere.
116 void kimage_file_post_load_cleanup(struct kimage *image)
118 struct purgatory_info *pi = &image->purgatory_info;
120 vfree(image->kernel_buf);
121 image->kernel_buf = NULL;
123 vfree(image->initrd_buf);
124 image->initrd_buf = NULL;
126 kfree(image->cmdline_buf);
127 image->cmdline_buf = NULL;
129 vfree(pi->purgatory_buf);
130 pi->purgatory_buf = NULL;
135 #ifdef CONFIG_IMA_KEXEC
136 vfree(image->ima_buffer);
137 image->ima_buffer = NULL;
138 #endif /* CONFIG_IMA_KEXEC */
140 /* See if architecture has anything to cleanup post load */
141 arch_kimage_file_post_load_cleanup(image);
144 * Above call should have called into bootloader to free up
145 * any data stored in kimage->image_loader_data. It should
146 * be ok now to free it up.
148 kfree(image->image_loader_data);
149 image->image_loader_data = NULL;
152 #ifdef CONFIG_KEXEC_SIG
154 kimage_validate_signature(struct kimage *image)
158 ret = arch_kexec_kernel_verify_sig(image, image->kernel_buf,
159 image->kernel_buf_len);
162 if (IS_ENABLED(CONFIG_KEXEC_SIG_FORCE)) {
163 pr_notice("Enforced kernel signature verification failed (%d).\n", ret);
168 * If IMA is guaranteed to appraise a signature on the kexec
169 * image, permit it even if the kernel is otherwise locked
172 if (!ima_appraise_signature(READING_KEXEC_IMAGE) &&
173 security_locked_down(LOCKDOWN_KEXEC))
176 pr_debug("kernel signature verification failed (%d).\n", ret);
184 * In file mode list of segments is prepared by kernel. Copy relevant
185 * data from user space, do error checking, prepare segment list
188 kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd,
189 const char __user *cmdline_ptr,
190 unsigned long cmdline_len, unsigned flags)
195 ret = kernel_read_file_from_fd(kernel_fd, 0, &image->kernel_buf,
196 INT_MAX, NULL, READING_KEXEC_IMAGE);
199 image->kernel_buf_len = ret;
201 /* Call arch image probe handlers */
202 ret = arch_kexec_kernel_image_probe(image, image->kernel_buf,
203 image->kernel_buf_len);
207 #ifdef CONFIG_KEXEC_SIG
208 ret = kimage_validate_signature(image);
213 /* It is possible that there no initramfs is being loaded */
214 if (!(flags & KEXEC_FILE_NO_INITRAMFS)) {
215 ret = kernel_read_file_from_fd(initrd_fd, 0, &image->initrd_buf,
217 READING_KEXEC_INITRAMFS);
220 image->initrd_buf_len = ret;
225 image->cmdline_buf = memdup_user(cmdline_ptr, cmdline_len);
226 if (IS_ERR(image->cmdline_buf)) {
227 ret = PTR_ERR(image->cmdline_buf);
228 image->cmdline_buf = NULL;
232 image->cmdline_buf_len = cmdline_len;
234 /* command line should be a string with last byte null */
235 if (image->cmdline_buf[cmdline_len - 1] != '\0') {
240 ima_kexec_cmdline(kernel_fd, image->cmdline_buf,
241 image->cmdline_buf_len - 1);
244 /* IMA needs to pass the measurement list to the next kernel. */
245 ima_add_kexec_buffer(image);
247 /* Call arch image load handlers */
248 ldata = arch_kexec_kernel_image_load(image);
251 ret = PTR_ERR(ldata);
255 image->image_loader_data = ldata;
257 /* In case of error, free up all allocated memory in this function */
259 kimage_file_post_load_cleanup(image);
264 kimage_file_alloc_init(struct kimage **rimage, int kernel_fd,
265 int initrd_fd, const char __user *cmdline_ptr,
266 unsigned long cmdline_len, unsigned long flags)
269 struct kimage *image;
270 bool kexec_on_panic = flags & KEXEC_FILE_ON_CRASH;
272 image = do_kimage_alloc_init();
276 image->file_mode = 1;
278 if (kexec_on_panic) {
279 /* Enable special crash kernel control page alloc policy. */
280 image->control_page = crashk_res.start;
281 image->type = KEXEC_TYPE_CRASH;
284 ret = kimage_file_prepare_segments(image, kernel_fd, initrd_fd,
285 cmdline_ptr, cmdline_len, flags);
289 ret = sanity_check_segment_list(image);
291 goto out_free_post_load_bufs;
294 image->control_code_page = kimage_alloc_control_pages(image,
295 get_order(KEXEC_CONTROL_PAGE_SIZE));
296 if (!image->control_code_page) {
297 pr_err("Could not allocate control_code_buffer\n");
298 goto out_free_post_load_bufs;
301 if (!kexec_on_panic) {
302 image->swap_page = kimage_alloc_control_pages(image, 0);
303 if (!image->swap_page) {
304 pr_err("Could not allocate swap buffer\n");
305 goto out_free_control_pages;
311 out_free_control_pages:
312 kimage_free_page_list(&image->control_pages);
313 out_free_post_load_bufs:
314 kimage_file_post_load_cleanup(image);
320 SYSCALL_DEFINE5(kexec_file_load, int, kernel_fd, int, initrd_fd,
321 unsigned long, cmdline_len, const char __user *, cmdline_ptr,
322 unsigned long, flags)
325 struct kimage **dest_image, *image;
327 /* We only trust the superuser with rebooting the system. */
328 if (!capable(CAP_SYS_BOOT) || kexec_load_disabled)
331 /* Make sure we have a legal set of flags */
332 if (flags != (flags & KEXEC_FILE_FLAGS))
337 if (!mutex_trylock(&kexec_mutex))
340 dest_image = &kexec_image;
341 if (flags & KEXEC_FILE_ON_CRASH) {
342 dest_image = &kexec_crash_image;
343 if (kexec_crash_image)
344 arch_kexec_unprotect_crashkres();
347 if (flags & KEXEC_FILE_UNLOAD)
351 * In case of crash, new kernel gets loaded in reserved region. It is
352 * same memory where old crash kernel might be loaded. Free any
353 * current crash dump kernel before we corrupt it.
355 if (flags & KEXEC_FILE_ON_CRASH)
356 kimage_free(xchg(&kexec_crash_image, NULL));
358 ret = kimage_file_alloc_init(&image, kernel_fd, initrd_fd, cmdline_ptr,
363 ret = machine_kexec_prepare(image);
368 * Some architecture(like S390) may touch the crash memory before
369 * machine_kexec_prepare(), we must copy vmcoreinfo data after it.
371 ret = kimage_crash_copy_vmcoreinfo(image);
375 ret = kexec_calculate_store_digests(image);
379 for (i = 0; i < image->nr_segments; i++) {
380 struct kexec_segment *ksegment;
382 ksegment = &image->segment[i];
383 pr_debug("Loading segment %d: buf=0x%p bufsz=0x%zx mem=0x%lx memsz=0x%zx\n",
384 i, ksegment->buf, ksegment->bufsz, ksegment->mem,
387 ret = kimage_load_segment(image, &image->segment[i]);
392 kimage_terminate(image);
394 ret = machine_kexec_post_load(image);
399 * Free up any temporary buffers allocated which are not needed
400 * after image has been loaded
402 kimage_file_post_load_cleanup(image);
404 image = xchg(dest_image, image);
406 if ((flags & KEXEC_FILE_ON_CRASH) && kexec_crash_image)
407 arch_kexec_protect_crashkres();
409 mutex_unlock(&kexec_mutex);
414 static int locate_mem_hole_top_down(unsigned long start, unsigned long end,
415 struct kexec_buf *kbuf)
417 struct kimage *image = kbuf->image;
418 unsigned long temp_start, temp_end;
420 temp_end = min(end, kbuf->buf_max);
421 temp_start = temp_end - kbuf->memsz;
424 /* align down start */
425 temp_start = temp_start & (~(kbuf->buf_align - 1));
427 if (temp_start < start || temp_start < kbuf->buf_min)
430 temp_end = temp_start + kbuf->memsz - 1;
433 * Make sure this does not conflict with any of existing
436 if (kimage_is_destination_range(image, temp_start, temp_end)) {
437 temp_start = temp_start - PAGE_SIZE;
441 /* We found a suitable memory range */
445 /* If we are here, we found a suitable memory range */
446 kbuf->mem = temp_start;
448 /* Success, stop navigating through remaining System RAM ranges */
452 static int locate_mem_hole_bottom_up(unsigned long start, unsigned long end,
453 struct kexec_buf *kbuf)
455 struct kimage *image = kbuf->image;
456 unsigned long temp_start, temp_end;
458 temp_start = max(start, kbuf->buf_min);
461 temp_start = ALIGN(temp_start, kbuf->buf_align);
462 temp_end = temp_start + kbuf->memsz - 1;
464 if (temp_end > end || temp_end > kbuf->buf_max)
467 * Make sure this does not conflict with any of existing
470 if (kimage_is_destination_range(image, temp_start, temp_end)) {
471 temp_start = temp_start + PAGE_SIZE;
475 /* We found a suitable memory range */
479 /* If we are here, we found a suitable memory range */
480 kbuf->mem = temp_start;
482 /* Success, stop navigating through remaining System RAM ranges */
486 static int locate_mem_hole_callback(struct resource *res, void *arg)
488 struct kexec_buf *kbuf = (struct kexec_buf *)arg;
489 u64 start = res->start, end = res->end;
490 unsigned long sz = end - start + 1;
492 /* Returning 0 will take to next memory range */
494 /* Don't use memory that will be detected and handled by a driver. */
495 if (res->flags & IORESOURCE_SYSRAM_DRIVER_MANAGED)
498 if (sz < kbuf->memsz)
501 if (end < kbuf->buf_min || start > kbuf->buf_max)
505 * Allocate memory top down with-in ram range. Otherwise bottom up
509 return locate_mem_hole_top_down(start, end, kbuf);
510 return locate_mem_hole_bottom_up(start, end, kbuf);
513 #ifdef CONFIG_ARCH_KEEP_MEMBLOCK
514 static int kexec_walk_memblock(struct kexec_buf *kbuf,
515 int (*func)(struct resource *, void *))
519 phys_addr_t mstart, mend;
520 struct resource res = { };
522 if (kbuf->image->type == KEXEC_TYPE_CRASH)
523 return func(&crashk_res, kbuf);
526 * Using MEMBLOCK_NONE will properly skip MEMBLOCK_DRIVER_MANAGED. See
527 * IORESOURCE_SYSRAM_DRIVER_MANAGED handling in
528 * locate_mem_hole_callback().
530 if (kbuf->top_down) {
531 for_each_free_mem_range_reverse(i, NUMA_NO_NODE, MEMBLOCK_NONE,
532 &mstart, &mend, NULL) {
534 * In memblock, end points to the first byte after the
535 * range while in kexec, end points to the last byte
540 ret = func(&res, kbuf);
545 for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE,
546 &mstart, &mend, NULL) {
548 * In memblock, end points to the first byte after the
549 * range while in kexec, end points to the last byte
554 ret = func(&res, kbuf);
563 static int kexec_walk_memblock(struct kexec_buf *kbuf,
564 int (*func)(struct resource *, void *))
571 * kexec_walk_resources - call func(data) on free memory regions
572 * @kbuf: Context info for the search. Also passed to @func.
573 * @func: Function to call for each memory region.
575 * Return: The memory walk will stop when func returns a non-zero value
576 * and that value will be returned. If all free regions are visited without
577 * func returning non-zero, then zero will be returned.
579 static int kexec_walk_resources(struct kexec_buf *kbuf,
580 int (*func)(struct resource *, void *))
582 if (kbuf->image->type == KEXEC_TYPE_CRASH)
583 return walk_iomem_res_desc(crashk_res.desc,
584 IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY,
585 crashk_res.start, crashk_res.end,
588 return walk_system_ram_res(0, ULONG_MAX, kbuf, func);
592 * kexec_locate_mem_hole - find free memory for the purgatory or the next kernel
593 * @kbuf: Parameters for the memory search.
595 * On success, kbuf->mem will have the start address of the memory region found.
597 * Return: 0 on success, negative errno on error.
599 int kexec_locate_mem_hole(struct kexec_buf *kbuf)
603 /* Arch knows where to place */
604 if (kbuf->mem != KEXEC_BUF_MEM_UNKNOWN)
607 if (!IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK))
608 ret = kexec_walk_resources(kbuf, locate_mem_hole_callback);
610 ret = kexec_walk_memblock(kbuf, locate_mem_hole_callback);
612 return ret == 1 ? 0 : -EADDRNOTAVAIL;
616 * arch_kexec_locate_mem_hole - Find free memory to place the segments.
617 * @kbuf: Parameters for the memory search.
619 * On success, kbuf->mem will have the start address of the memory region found.
621 * Return: 0 on success, negative errno on error.
623 int __weak arch_kexec_locate_mem_hole(struct kexec_buf *kbuf)
625 return kexec_locate_mem_hole(kbuf);
629 * kexec_add_buffer - place a buffer in a kexec segment
630 * @kbuf: Buffer contents and memory parameters.
632 * This function assumes that kexec_mutex is held.
633 * On successful return, @kbuf->mem will have the physical address of
634 * the buffer in memory.
636 * Return: 0 on success, negative errno on error.
638 int kexec_add_buffer(struct kexec_buf *kbuf)
640 struct kexec_segment *ksegment;
643 /* Currently adding segment this way is allowed only in file mode */
644 if (!kbuf->image->file_mode)
647 if (kbuf->image->nr_segments >= KEXEC_SEGMENT_MAX)
651 * Make sure we are not trying to add buffer after allocating
652 * control pages. All segments need to be placed first before
653 * any control pages are allocated. As control page allocation
654 * logic goes through list of segments to make sure there are
655 * no destination overlaps.
657 if (!list_empty(&kbuf->image->control_pages)) {
662 /* Ensure minimum alignment needed for segments. */
663 kbuf->memsz = ALIGN(kbuf->memsz, PAGE_SIZE);
664 kbuf->buf_align = max(kbuf->buf_align, PAGE_SIZE);
666 /* Walk the RAM ranges and allocate a suitable range for the buffer */
667 ret = arch_kexec_locate_mem_hole(kbuf);
671 /* Found a suitable memory range */
672 ksegment = &kbuf->image->segment[kbuf->image->nr_segments];
673 ksegment->kbuf = kbuf->buffer;
674 ksegment->bufsz = kbuf->bufsz;
675 ksegment->mem = kbuf->mem;
676 ksegment->memsz = kbuf->memsz;
677 kbuf->image->nr_segments++;
681 /* Calculate and store the digest of segments */
682 static int kexec_calculate_store_digests(struct kimage *image)
684 struct crypto_shash *tfm;
685 struct shash_desc *desc;
686 int ret = 0, i, j, zero_buf_sz, sha_region_sz;
687 size_t desc_size, nullsz;
690 struct kexec_sha_region *sha_regions;
691 struct purgatory_info *pi = &image->purgatory_info;
693 if (!IS_ENABLED(CONFIG_ARCH_HAS_KEXEC_PURGATORY))
696 zero_buf = __va(page_to_pfn(ZERO_PAGE(0)) << PAGE_SHIFT);
697 zero_buf_sz = PAGE_SIZE;
699 tfm = crypto_alloc_shash("sha256", 0, 0);
705 desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
706 desc = kzalloc(desc_size, GFP_KERNEL);
712 sha_region_sz = KEXEC_SEGMENT_MAX * sizeof(struct kexec_sha_region);
713 sha_regions = vzalloc(sha_region_sz);
721 ret = crypto_shash_init(desc);
723 goto out_free_sha_regions;
725 digest = kzalloc(SHA256_DIGEST_SIZE, GFP_KERNEL);
728 goto out_free_sha_regions;
731 for (j = i = 0; i < image->nr_segments; i++) {
732 struct kexec_segment *ksegment;
734 ksegment = &image->segment[i];
736 * Skip purgatory as it will be modified once we put digest
739 if (ksegment->kbuf == pi->purgatory_buf)
742 ret = crypto_shash_update(desc, ksegment->kbuf,
748 * Assume rest of the buffer is filled with zero and
749 * update digest accordingly.
751 nullsz = ksegment->memsz - ksegment->bufsz;
753 unsigned long bytes = nullsz;
755 if (bytes > zero_buf_sz)
757 ret = crypto_shash_update(desc, zero_buf, bytes);
766 sha_regions[j].start = ksegment->mem;
767 sha_regions[j].len = ksegment->memsz;
772 ret = crypto_shash_final(desc, digest);
774 goto out_free_digest;
775 ret = kexec_purgatory_get_set_symbol(image, "purgatory_sha_regions",
776 sha_regions, sha_region_sz, 0);
778 goto out_free_digest;
780 ret = kexec_purgatory_get_set_symbol(image, "purgatory_sha256_digest",
781 digest, SHA256_DIGEST_SIZE, 0);
783 goto out_free_digest;
788 out_free_sha_regions:
798 #ifdef CONFIG_ARCH_HAS_KEXEC_PURGATORY
800 * kexec_purgatory_setup_kbuf - prepare buffer to load purgatory.
801 * @pi: Purgatory to be loaded.
802 * @kbuf: Buffer to setup.
804 * Allocates the memory needed for the buffer. Caller is responsible to free
805 * the memory after use.
807 * Return: 0 on success, negative errno on error.
809 static int kexec_purgatory_setup_kbuf(struct purgatory_info *pi,
810 struct kexec_buf *kbuf)
812 const Elf_Shdr *sechdrs;
813 unsigned long bss_align;
814 unsigned long bss_sz;
818 sechdrs = (void *)pi->ehdr + pi->ehdr->e_shoff;
819 kbuf->buf_align = bss_align = 1;
820 kbuf->bufsz = bss_sz = 0;
822 for (i = 0; i < pi->ehdr->e_shnum; i++) {
823 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
826 align = sechdrs[i].sh_addralign;
827 if (sechdrs[i].sh_type != SHT_NOBITS) {
828 if (kbuf->buf_align < align)
829 kbuf->buf_align = align;
830 kbuf->bufsz = ALIGN(kbuf->bufsz, align);
831 kbuf->bufsz += sechdrs[i].sh_size;
833 if (bss_align < align)
835 bss_sz = ALIGN(bss_sz, align);
836 bss_sz += sechdrs[i].sh_size;
839 kbuf->bufsz = ALIGN(kbuf->bufsz, bss_align);
840 kbuf->memsz = kbuf->bufsz + bss_sz;
841 if (kbuf->buf_align < bss_align)
842 kbuf->buf_align = bss_align;
844 kbuf->buffer = vzalloc(kbuf->bufsz);
847 pi->purgatory_buf = kbuf->buffer;
849 ret = kexec_add_buffer(kbuf);
855 vfree(pi->purgatory_buf);
856 pi->purgatory_buf = NULL;
861 * kexec_purgatory_setup_sechdrs - prepares the pi->sechdrs buffer.
862 * @pi: Purgatory to be loaded.
863 * @kbuf: Buffer prepared to store purgatory.
865 * Allocates the memory needed for the buffer. Caller is responsible to free
866 * the memory after use.
868 * Return: 0 on success, negative errno on error.
870 static int kexec_purgatory_setup_sechdrs(struct purgatory_info *pi,
871 struct kexec_buf *kbuf)
873 unsigned long bss_addr;
874 unsigned long offset;
879 * The section headers in kexec_purgatory are read-only. In order to
880 * have them modifiable make a temporary copy.
882 sechdrs = vzalloc(array_size(sizeof(Elf_Shdr), pi->ehdr->e_shnum));
885 memcpy(sechdrs, (void *)pi->ehdr + pi->ehdr->e_shoff,
886 pi->ehdr->e_shnum * sizeof(Elf_Shdr));
887 pi->sechdrs = sechdrs;
890 bss_addr = kbuf->mem + kbuf->bufsz;
891 kbuf->image->start = pi->ehdr->e_entry;
893 for (i = 0; i < pi->ehdr->e_shnum; i++) {
897 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
900 align = sechdrs[i].sh_addralign;
901 if (sechdrs[i].sh_type == SHT_NOBITS) {
902 bss_addr = ALIGN(bss_addr, align);
903 sechdrs[i].sh_addr = bss_addr;
904 bss_addr += sechdrs[i].sh_size;
908 offset = ALIGN(offset, align);
909 if (sechdrs[i].sh_flags & SHF_EXECINSTR &&
910 pi->ehdr->e_entry >= sechdrs[i].sh_addr &&
911 pi->ehdr->e_entry < (sechdrs[i].sh_addr
912 + sechdrs[i].sh_size)) {
913 kbuf->image->start -= sechdrs[i].sh_addr;
914 kbuf->image->start += kbuf->mem + offset;
917 src = (void *)pi->ehdr + sechdrs[i].sh_offset;
918 dst = pi->purgatory_buf + offset;
919 memcpy(dst, src, sechdrs[i].sh_size);
921 sechdrs[i].sh_addr = kbuf->mem + offset;
922 sechdrs[i].sh_offset = offset;
923 offset += sechdrs[i].sh_size;
929 static int kexec_apply_relocations(struct kimage *image)
932 struct purgatory_info *pi = &image->purgatory_info;
933 const Elf_Shdr *sechdrs;
935 sechdrs = (void *)pi->ehdr + pi->ehdr->e_shoff;
937 for (i = 0; i < pi->ehdr->e_shnum; i++) {
938 const Elf_Shdr *relsec;
939 const Elf_Shdr *symtab;
942 relsec = sechdrs + i;
944 if (relsec->sh_type != SHT_RELA &&
945 relsec->sh_type != SHT_REL)
949 * For section of type SHT_RELA/SHT_REL,
950 * ->sh_link contains section header index of associated
951 * symbol table. And ->sh_info contains section header
952 * index of section to which relocations apply.
954 if (relsec->sh_info >= pi->ehdr->e_shnum ||
955 relsec->sh_link >= pi->ehdr->e_shnum)
958 section = pi->sechdrs + relsec->sh_info;
959 symtab = sechdrs + relsec->sh_link;
961 if (!(section->sh_flags & SHF_ALLOC))
965 * symtab->sh_link contain section header index of associated
968 if (symtab->sh_link >= pi->ehdr->e_shnum)
969 /* Invalid section number? */
973 * Respective architecture needs to provide support for applying
974 * relocations of type SHT_RELA/SHT_REL.
976 if (relsec->sh_type == SHT_RELA)
977 ret = arch_kexec_apply_relocations_add(pi, section,
979 else if (relsec->sh_type == SHT_REL)
980 ret = arch_kexec_apply_relocations(pi, section,
990 * kexec_load_purgatory - Load and relocate the purgatory object.
991 * @image: Image to add the purgatory to.
992 * @kbuf: Memory parameters to use.
994 * Allocates the memory needed for image->purgatory_info.sechdrs and
995 * image->purgatory_info.purgatory_buf/kbuf->buffer. Caller is responsible
996 * to free the memory after use.
998 * Return: 0 on success, negative errno on error.
1000 int kexec_load_purgatory(struct kimage *image, struct kexec_buf *kbuf)
1002 struct purgatory_info *pi = &image->purgatory_info;
1005 if (kexec_purgatory_size <= 0)
1008 pi->ehdr = (const Elf_Ehdr *)kexec_purgatory;
1010 ret = kexec_purgatory_setup_kbuf(pi, kbuf);
1014 ret = kexec_purgatory_setup_sechdrs(pi, kbuf);
1018 ret = kexec_apply_relocations(image);
1027 vfree(pi->purgatory_buf);
1028 pi->purgatory_buf = NULL;
1033 * kexec_purgatory_find_symbol - find a symbol in the purgatory
1034 * @pi: Purgatory to search in.
1035 * @name: Name of the symbol.
1037 * Return: pointer to symbol in read-only symtab on success, NULL on error.
1039 static const Elf_Sym *kexec_purgatory_find_symbol(struct purgatory_info *pi,
1042 const Elf_Shdr *sechdrs;
1043 const Elf_Ehdr *ehdr;
1044 const Elf_Sym *syms;
1052 sechdrs = (void *)ehdr + ehdr->e_shoff;
1054 for (i = 0; i < ehdr->e_shnum; i++) {
1055 if (sechdrs[i].sh_type != SHT_SYMTAB)
1058 if (sechdrs[i].sh_link >= ehdr->e_shnum)
1059 /* Invalid strtab section number */
1061 strtab = (void *)ehdr + sechdrs[sechdrs[i].sh_link].sh_offset;
1062 syms = (void *)ehdr + sechdrs[i].sh_offset;
1064 /* Go through symbols for a match */
1065 for (k = 0; k < sechdrs[i].sh_size/sizeof(Elf_Sym); k++) {
1066 if (ELF_ST_BIND(syms[k].st_info) != STB_GLOBAL)
1069 if (strcmp(strtab + syms[k].st_name, name) != 0)
1072 if (syms[k].st_shndx == SHN_UNDEF ||
1073 syms[k].st_shndx >= ehdr->e_shnum) {
1074 pr_debug("Symbol: %s has bad section index %d.\n",
1075 name, syms[k].st_shndx);
1079 /* Found the symbol we are looking for */
1087 void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name)
1089 struct purgatory_info *pi = &image->purgatory_info;
1093 sym = kexec_purgatory_find_symbol(pi, name);
1095 return ERR_PTR(-EINVAL);
1097 sechdr = &pi->sechdrs[sym->st_shndx];
1100 * Returns the address where symbol will finally be loaded after
1101 * kexec_load_segment()
1103 return (void *)(sechdr->sh_addr + sym->st_value);
1107 * Get or set value of a symbol. If "get_value" is true, symbol value is
1108 * returned in buf otherwise symbol value is set based on value in buf.
1110 int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name,
1111 void *buf, unsigned int size, bool get_value)
1113 struct purgatory_info *pi = &image->purgatory_info;
1118 sym = kexec_purgatory_find_symbol(pi, name);
1122 if (sym->st_size != size) {
1123 pr_err("symbol %s size mismatch: expected %lu actual %u\n",
1124 name, (unsigned long)sym->st_size, size);
1128 sec = pi->sechdrs + sym->st_shndx;
1130 if (sec->sh_type == SHT_NOBITS) {
1131 pr_err("symbol %s is in a bss section. Cannot %s\n", name,
1132 get_value ? "get" : "set");
1136 sym_buf = (char *)pi->purgatory_buf + sec->sh_offset + sym->st_value;
1139 memcpy((void *)buf, sym_buf, size);
1141 memcpy((void *)sym_buf, buf, size);
1145 #endif /* CONFIG_ARCH_HAS_KEXEC_PURGATORY */
1147 int crash_exclude_mem_range(struct crash_mem *mem,
1148 unsigned long long mstart, unsigned long long mend)
1151 unsigned long long start, end, p_start, p_end;
1152 struct crash_mem_range temp_range = {0, 0};
1154 for (i = 0; i < mem->nr_ranges; i++) {
1155 start = mem->ranges[i].start;
1156 end = mem->ranges[i].end;
1160 if (mstart > end || mend < start)
1163 /* Truncate any area outside of range */
1169 /* Found completely overlapping range */
1170 if (p_start == start && p_end == end) {
1171 mem->ranges[i].start = 0;
1172 mem->ranges[i].end = 0;
1173 if (i < mem->nr_ranges - 1) {
1174 /* Shift rest of the ranges to left */
1175 for (j = i; j < mem->nr_ranges - 1; j++) {
1176 mem->ranges[j].start =
1177 mem->ranges[j+1].start;
1178 mem->ranges[j].end =
1179 mem->ranges[j+1].end;
1183 * Continue to check if there are another overlapping ranges
1184 * from the current position because of shifting the above
1195 if (p_start > start && p_end < end) {
1196 /* Split original range */
1197 mem->ranges[i].end = p_start - 1;
1198 temp_range.start = p_end + 1;
1199 temp_range.end = end;
1200 } else if (p_start != start)
1201 mem->ranges[i].end = p_start - 1;
1203 mem->ranges[i].start = p_end + 1;
1207 /* If a split happened, add the split to array */
1208 if (!temp_range.end)
1211 /* Split happened */
1212 if (i == mem->max_nr_ranges - 1)
1215 /* Location where new range should go */
1217 if (j < mem->nr_ranges) {
1218 /* Move over all ranges one slot towards the end */
1219 for (i = mem->nr_ranges - 1; i >= j; i--)
1220 mem->ranges[i + 1] = mem->ranges[i];
1223 mem->ranges[j].start = temp_range.start;
1224 mem->ranges[j].end = temp_range.end;
1229 int crash_prepare_elf64_headers(struct crash_mem *mem, int need_kernel_map,
1230 void **addr, unsigned long *sz)
1234 unsigned long nr_cpus = num_possible_cpus(), nr_phdr, elf_sz;
1236 unsigned int cpu, i;
1237 unsigned long long notes_addr;
1238 unsigned long mstart, mend;
1240 /* extra phdr for vmcoreinfo ELF note */
1241 nr_phdr = nr_cpus + 1;
1242 nr_phdr += mem->nr_ranges;
1245 * kexec-tools creates an extra PT_LOAD phdr for kernel text mapping
1246 * area (for example, ffffffff80000000 - ffffffffa0000000 on x86_64).
1247 * I think this is required by tools like gdb. So same physical
1248 * memory will be mapped in two ELF headers. One will contain kernel
1249 * text virtual addresses and other will have __va(physical) addresses.
1253 elf_sz = sizeof(Elf64_Ehdr) + nr_phdr * sizeof(Elf64_Phdr);
1254 elf_sz = ALIGN(elf_sz, ELF_CORE_HEADER_ALIGN);
1256 buf = vzalloc(elf_sz);
1260 ehdr = (Elf64_Ehdr *)buf;
1261 phdr = (Elf64_Phdr *)(ehdr + 1);
1262 memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
1263 ehdr->e_ident[EI_CLASS] = ELFCLASS64;
1264 ehdr->e_ident[EI_DATA] = ELFDATA2LSB;
1265 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
1266 ehdr->e_ident[EI_OSABI] = ELF_OSABI;
1267 memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD);
1268 ehdr->e_type = ET_CORE;
1269 ehdr->e_machine = ELF_ARCH;
1270 ehdr->e_version = EV_CURRENT;
1271 ehdr->e_phoff = sizeof(Elf64_Ehdr);
1272 ehdr->e_ehsize = sizeof(Elf64_Ehdr);
1273 ehdr->e_phentsize = sizeof(Elf64_Phdr);
1275 /* Prepare one phdr of type PT_NOTE for each present CPU */
1276 for_each_present_cpu(cpu) {
1277 phdr->p_type = PT_NOTE;
1278 notes_addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpu));
1279 phdr->p_offset = phdr->p_paddr = notes_addr;
1280 phdr->p_filesz = phdr->p_memsz = sizeof(note_buf_t);
1285 /* Prepare one PT_NOTE header for vmcoreinfo */
1286 phdr->p_type = PT_NOTE;
1287 phdr->p_offset = phdr->p_paddr = paddr_vmcoreinfo_note();
1288 phdr->p_filesz = phdr->p_memsz = VMCOREINFO_NOTE_SIZE;
1292 /* Prepare PT_LOAD type program header for kernel text region */
1293 if (need_kernel_map) {
1294 phdr->p_type = PT_LOAD;
1295 phdr->p_flags = PF_R|PF_W|PF_X;
1296 phdr->p_vaddr = (unsigned long) _text;
1297 phdr->p_filesz = phdr->p_memsz = _end - _text;
1298 phdr->p_offset = phdr->p_paddr = __pa_symbol(_text);
1303 /* Go through all the ranges in mem->ranges[] and prepare phdr */
1304 for (i = 0; i < mem->nr_ranges; i++) {
1305 mstart = mem->ranges[i].start;
1306 mend = mem->ranges[i].end;
1308 phdr->p_type = PT_LOAD;
1309 phdr->p_flags = PF_R|PF_W|PF_X;
1310 phdr->p_offset = mstart;
1312 phdr->p_paddr = mstart;
1313 phdr->p_vaddr = (unsigned long) __va(mstart);
1314 phdr->p_filesz = phdr->p_memsz = mend - mstart + 1;
1317 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",
1318 phdr, phdr->p_vaddr, phdr->p_paddr, phdr->p_filesz,
1319 ehdr->e_phnum, phdr->p_offset);