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/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 #ifdef CONFIG_KEXEC_SIG
33 static bool sig_enforce = IS_ENABLED(CONFIG_KEXEC_SIG_FORCE);
35 void set_kexec_sig_enforced(void)
41 static int kexec_calculate_store_digests(struct kimage *image);
44 * Currently this is the only default function that is exported as some
45 * architectures need it to do additional handlings.
46 * In the future, other default functions may be exported too if required.
48 int kexec_image_probe_default(struct kimage *image, void *buf,
49 unsigned long buf_len)
51 const struct kexec_file_ops * const *fops;
54 for (fops = &kexec_file_loaders[0]; *fops && (*fops)->probe; ++fops) {
55 ret = (*fops)->probe(buf, buf_len);
65 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 int kexec_image_post_load_cleanup_default(struct kimage *image)
78 if (!image->fops || !image->fops->cleanup)
81 return image->fops->cleanup(image->image_loader_data);
85 * Free up memory used by kernel, initrd, and command line. This is temporary
86 * memory allocation which is not needed any more after these buffers have
87 * been loaded into separate segments and have been copied elsewhere.
89 void kimage_file_post_load_cleanup(struct kimage *image)
91 struct purgatory_info *pi = &image->purgatory_info;
93 vfree(image->kernel_buf);
94 image->kernel_buf = NULL;
96 vfree(image->initrd_buf);
97 image->initrd_buf = NULL;
99 kfree(image->cmdline_buf);
100 image->cmdline_buf = NULL;
102 vfree(pi->purgatory_buf);
103 pi->purgatory_buf = NULL;
108 #ifdef CONFIG_IMA_KEXEC
109 vfree(image->ima_buffer);
110 image->ima_buffer = NULL;
111 #endif /* CONFIG_IMA_KEXEC */
113 /* See if architecture has anything to cleanup post load */
114 arch_kimage_file_post_load_cleanup(image);
117 * Above call should have called into bootloader to free up
118 * any data stored in kimage->image_loader_data. It should
119 * be ok now to free it up.
121 kfree(image->image_loader_data);
122 image->image_loader_data = NULL;
125 #ifdef CONFIG_KEXEC_SIG
126 #ifdef CONFIG_SIGNED_PE_FILE_VERIFICATION
127 int kexec_kernel_verify_pe_sig(const char *kernel, unsigned long kernel_len)
131 ret = verify_pefile_signature(kernel, kernel_len,
132 VERIFY_USE_SECONDARY_KEYRING,
133 VERIFYING_KEXEC_PE_SIGNATURE);
134 if (ret == -ENOKEY && IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING)) {
135 ret = verify_pefile_signature(kernel, kernel_len,
136 VERIFY_USE_PLATFORM_KEYRING,
137 VERIFYING_KEXEC_PE_SIGNATURE);
143 static int kexec_image_verify_sig(struct kimage *image, void *buf,
144 unsigned long buf_len)
146 if (!image->fops || !image->fops->verify_sig) {
147 pr_debug("kernel loader does not support signature verification.\n");
148 return -EKEYREJECTED;
151 return image->fops->verify_sig(buf, buf_len);
155 kimage_validate_signature(struct kimage *image)
159 ret = kexec_image_verify_sig(image, image->kernel_buf,
160 image->kernel_buf_len);
164 pr_notice("Enforced kernel signature verification failed (%d).\n", ret);
169 * If IMA is guaranteed to appraise a signature on the kexec
170 * image, permit it even if the kernel is otherwise locked
173 if (!ima_appraise_signature(READING_KEXEC_IMAGE) &&
174 security_locked_down(LOCKDOWN_KEXEC))
177 pr_debug("kernel signature verification failed (%d).\n", ret);
185 * In file mode list of segments is prepared by kernel. Copy relevant
186 * data from user space, do error checking, prepare segment list
189 kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd,
190 const char __user *cmdline_ptr,
191 unsigned long cmdline_len, unsigned flags)
196 ret = kernel_read_file_from_fd(kernel_fd, 0, &image->kernel_buf,
197 INT_MAX, NULL, READING_KEXEC_IMAGE);
200 image->kernel_buf_len = ret;
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_SIG
209 ret = kimage_validate_signature(image);
214 /* It is possible that there no initramfs is being loaded */
215 if (!(flags & KEXEC_FILE_NO_INITRAMFS)) {
216 ret = kernel_read_file_from_fd(initrd_fd, 0, &image->initrd_buf,
218 READING_KEXEC_INITRAMFS);
221 image->initrd_buf_len = ret;
226 image->cmdline_buf = memdup_user(cmdline_ptr, cmdline_len);
227 if (IS_ERR(image->cmdline_buf)) {
228 ret = PTR_ERR(image->cmdline_buf);
229 image->cmdline_buf = NULL;
233 image->cmdline_buf_len = cmdline_len;
235 /* command line should be a string with last byte null */
236 if (image->cmdline_buf[cmdline_len - 1] != '\0') {
241 ima_kexec_cmdline(kernel_fd, image->cmdline_buf,
242 image->cmdline_buf_len - 1);
245 /* IMA needs to pass the measurement list to the next kernel. */
246 ima_add_kexec_buffer(image);
248 /* Call arch image load handlers */
249 ldata = arch_kexec_kernel_image_load(image);
252 ret = PTR_ERR(ldata);
256 image->image_loader_data = ldata;
258 /* In case of error, free up all allocated memory in this function */
260 kimage_file_post_load_cleanup(image);
265 kimage_file_alloc_init(struct kimage **rimage, int kernel_fd,
266 int initrd_fd, const char __user *cmdline_ptr,
267 unsigned long cmdline_len, unsigned long flags)
270 struct kimage *image;
271 bool kexec_on_panic = flags & KEXEC_FILE_ON_CRASH;
273 image = do_kimage_alloc_init();
277 image->file_mode = 1;
279 if (kexec_on_panic) {
280 /* Enable special crash kernel control page alloc policy. */
281 image->control_page = crashk_res.start;
282 image->type = KEXEC_TYPE_CRASH;
285 ret = kimage_file_prepare_segments(image, kernel_fd, initrd_fd,
286 cmdline_ptr, cmdline_len, flags);
290 ret = sanity_check_segment_list(image);
292 goto out_free_post_load_bufs;
295 image->control_code_page = kimage_alloc_control_pages(image,
296 get_order(KEXEC_CONTROL_PAGE_SIZE));
297 if (!image->control_code_page) {
298 pr_err("Could not allocate control_code_buffer\n");
299 goto out_free_post_load_bufs;
302 if (!kexec_on_panic) {
303 image->swap_page = kimage_alloc_control_pages(image, 0);
304 if (!image->swap_page) {
305 pr_err("Could not allocate swap buffer\n");
306 goto out_free_control_pages;
312 out_free_control_pages:
313 kimage_free_page_list(&image->control_pages);
314 out_free_post_load_bufs:
315 kimage_file_post_load_cleanup(image);
321 SYSCALL_DEFINE5(kexec_file_load, int, kernel_fd, int, initrd_fd,
322 unsigned long, cmdline_len, const char __user *, cmdline_ptr,
323 unsigned long, flags)
326 struct kimage **dest_image, *image;
328 /* We only trust the superuser with rebooting the system. */
329 if (!capable(CAP_SYS_BOOT) || kexec_load_disabled)
332 /* Make sure we have a legal set of flags */
333 if (flags != (flags & KEXEC_FILE_FLAGS))
338 if (!mutex_trylock(&kexec_mutex))
341 dest_image = &kexec_image;
342 if (flags & KEXEC_FILE_ON_CRASH) {
343 dest_image = &kexec_crash_image;
344 if (kexec_crash_image)
345 arch_kexec_unprotect_crashkres();
348 if (flags & KEXEC_FILE_UNLOAD)
352 * In case of crash, new kernel gets loaded in reserved region. It is
353 * same memory where old crash kernel might be loaded. Free any
354 * current crash dump kernel before we corrupt it.
356 if (flags & KEXEC_FILE_ON_CRASH)
357 kimage_free(xchg(&kexec_crash_image, NULL));
359 ret = kimage_file_alloc_init(&image, kernel_fd, initrd_fd, cmdline_ptr,
364 ret = machine_kexec_prepare(image);
369 * Some architecture(like S390) may touch the crash memory before
370 * machine_kexec_prepare(), we must copy vmcoreinfo data after it.
372 ret = kimage_crash_copy_vmcoreinfo(image);
376 ret = kexec_calculate_store_digests(image);
380 for (i = 0; i < image->nr_segments; i++) {
381 struct kexec_segment *ksegment;
383 ksegment = &image->segment[i];
384 pr_debug("Loading segment %d: buf=0x%p bufsz=0x%zx mem=0x%lx memsz=0x%zx\n",
385 i, ksegment->buf, ksegment->bufsz, ksegment->mem,
388 ret = kimage_load_segment(image, &image->segment[i]);
393 kimage_terminate(image);
395 ret = machine_kexec_post_load(image);
400 * Free up any temporary buffers allocated which are not needed
401 * after image has been loaded
403 kimage_file_post_load_cleanup(image);
405 image = xchg(dest_image, image);
407 if ((flags & KEXEC_FILE_ON_CRASH) && kexec_crash_image)
408 arch_kexec_protect_crashkres();
410 mutex_unlock(&kexec_mutex);
415 static int locate_mem_hole_top_down(unsigned long start, unsigned long end,
416 struct kexec_buf *kbuf)
418 struct kimage *image = kbuf->image;
419 unsigned long temp_start, temp_end;
421 temp_end = min(end, kbuf->buf_max);
422 temp_start = temp_end - kbuf->memsz;
425 /* align down start */
426 temp_start = temp_start & (~(kbuf->buf_align - 1));
428 if (temp_start < start || temp_start < kbuf->buf_min)
431 temp_end = temp_start + kbuf->memsz - 1;
434 * Make sure this does not conflict with any of existing
437 if (kimage_is_destination_range(image, temp_start, temp_end)) {
438 temp_start = temp_start - PAGE_SIZE;
442 /* We found a suitable memory range */
446 /* If we are here, we found a suitable memory range */
447 kbuf->mem = temp_start;
449 /* Success, stop navigating through remaining System RAM ranges */
453 static int locate_mem_hole_bottom_up(unsigned long start, unsigned long end,
454 struct kexec_buf *kbuf)
456 struct kimage *image = kbuf->image;
457 unsigned long temp_start, temp_end;
459 temp_start = max(start, kbuf->buf_min);
462 temp_start = ALIGN(temp_start, kbuf->buf_align);
463 temp_end = temp_start + kbuf->memsz - 1;
465 if (temp_end > end || temp_end > kbuf->buf_max)
468 * Make sure this does not conflict with any of existing
471 if (kimage_is_destination_range(image, temp_start, temp_end)) {
472 temp_start = temp_start + PAGE_SIZE;
476 /* We found a suitable memory range */
480 /* If we are here, we found a suitable memory range */
481 kbuf->mem = temp_start;
483 /* Success, stop navigating through remaining System RAM ranges */
487 static int locate_mem_hole_callback(struct resource *res, void *arg)
489 struct kexec_buf *kbuf = (struct kexec_buf *)arg;
490 u64 start = res->start, end = res->end;
491 unsigned long sz = end - start + 1;
493 /* Returning 0 will take to next memory range */
495 /* Don't use memory that will be detected and handled by a driver. */
496 if (res->flags & IORESOURCE_SYSRAM_DRIVER_MANAGED)
499 if (sz < kbuf->memsz)
502 if (end < kbuf->buf_min || start > kbuf->buf_max)
506 * Allocate memory top down with-in ram range. Otherwise bottom up
510 return locate_mem_hole_top_down(start, end, kbuf);
511 return locate_mem_hole_bottom_up(start, end, kbuf);
514 #ifdef CONFIG_ARCH_KEEP_MEMBLOCK
515 static int kexec_walk_memblock(struct kexec_buf *kbuf,
516 int (*func)(struct resource *, void *))
520 phys_addr_t mstart, mend;
521 struct resource res = { };
523 if (kbuf->image->type == KEXEC_TYPE_CRASH)
524 return func(&crashk_res, kbuf);
527 * Using MEMBLOCK_NONE will properly skip MEMBLOCK_DRIVER_MANAGED. See
528 * IORESOURCE_SYSRAM_DRIVER_MANAGED handling in
529 * locate_mem_hole_callback().
531 if (kbuf->top_down) {
532 for_each_free_mem_range_reverse(i, NUMA_NO_NODE, MEMBLOCK_NONE,
533 &mstart, &mend, NULL) {
535 * In memblock, end points to the first byte after the
536 * range while in kexec, end points to the last byte
541 ret = func(&res, kbuf);
546 for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE,
547 &mstart, &mend, NULL) {
549 * In memblock, end points to the first byte after the
550 * range while in kexec, end points to the last byte
555 ret = func(&res, kbuf);
564 static int kexec_walk_memblock(struct kexec_buf *kbuf,
565 int (*func)(struct resource *, void *))
572 * kexec_walk_resources - call func(data) on free memory regions
573 * @kbuf: Context info for the search. Also passed to @func.
574 * @func: Function to call for each memory region.
576 * Return: The memory walk will stop when func returns a non-zero value
577 * and that value will be returned. If all free regions are visited without
578 * func returning non-zero, then zero will be returned.
580 static int kexec_walk_resources(struct kexec_buf *kbuf,
581 int (*func)(struct resource *, void *))
583 if (kbuf->image->type == KEXEC_TYPE_CRASH)
584 return walk_iomem_res_desc(crashk_res.desc,
585 IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY,
586 crashk_res.start, crashk_res.end,
589 return walk_system_ram_res(0, ULONG_MAX, kbuf, func);
593 * kexec_locate_mem_hole - find free memory for the purgatory or the next kernel
594 * @kbuf: Parameters for the memory search.
596 * On success, kbuf->mem will have the start address of the memory region found.
598 * Return: 0 on success, negative errno on error.
600 int kexec_locate_mem_hole(struct kexec_buf *kbuf)
604 /* Arch knows where to place */
605 if (kbuf->mem != KEXEC_BUF_MEM_UNKNOWN)
608 if (!IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK))
609 ret = kexec_walk_resources(kbuf, locate_mem_hole_callback);
611 ret = kexec_walk_memblock(kbuf, locate_mem_hole_callback);
613 return ret == 1 ? 0 : -EADDRNOTAVAIL;
617 * kexec_add_buffer - place a buffer in a kexec segment
618 * @kbuf: Buffer contents and memory parameters.
620 * This function assumes that kexec_mutex is held.
621 * On successful return, @kbuf->mem will have the physical address of
622 * the buffer in memory.
624 * Return: 0 on success, negative errno on error.
626 int kexec_add_buffer(struct kexec_buf *kbuf)
628 struct kexec_segment *ksegment;
631 /* Currently adding segment this way is allowed only in file mode */
632 if (!kbuf->image->file_mode)
635 if (kbuf->image->nr_segments >= KEXEC_SEGMENT_MAX)
639 * Make sure we are not trying to add buffer after allocating
640 * control pages. All segments need to be placed first before
641 * any control pages are allocated. As control page allocation
642 * logic goes through list of segments to make sure there are
643 * no destination overlaps.
645 if (!list_empty(&kbuf->image->control_pages)) {
650 /* Ensure minimum alignment needed for segments. */
651 kbuf->memsz = ALIGN(kbuf->memsz, PAGE_SIZE);
652 kbuf->buf_align = max(kbuf->buf_align, PAGE_SIZE);
654 /* Walk the RAM ranges and allocate a suitable range for the buffer */
655 ret = arch_kexec_locate_mem_hole(kbuf);
659 /* Found a suitable memory range */
660 ksegment = &kbuf->image->segment[kbuf->image->nr_segments];
661 ksegment->kbuf = kbuf->buffer;
662 ksegment->bufsz = kbuf->bufsz;
663 ksegment->mem = kbuf->mem;
664 ksegment->memsz = kbuf->memsz;
665 kbuf->image->nr_segments++;
669 /* Calculate and store the digest of segments */
670 static int kexec_calculate_store_digests(struct kimage *image)
672 struct crypto_shash *tfm;
673 struct shash_desc *desc;
674 int ret = 0, i, j, zero_buf_sz, sha_region_sz;
675 size_t desc_size, nullsz;
678 struct kexec_sha_region *sha_regions;
679 struct purgatory_info *pi = &image->purgatory_info;
681 if (!IS_ENABLED(CONFIG_ARCH_HAS_KEXEC_PURGATORY))
684 zero_buf = __va(page_to_pfn(ZERO_PAGE(0)) << PAGE_SHIFT);
685 zero_buf_sz = PAGE_SIZE;
687 tfm = crypto_alloc_shash("sha256", 0, 0);
693 desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
694 desc = kzalloc(desc_size, GFP_KERNEL);
700 sha_region_sz = KEXEC_SEGMENT_MAX * sizeof(struct kexec_sha_region);
701 sha_regions = vzalloc(sha_region_sz);
709 ret = crypto_shash_init(desc);
711 goto out_free_sha_regions;
713 digest = kzalloc(SHA256_DIGEST_SIZE, GFP_KERNEL);
716 goto out_free_sha_regions;
719 for (j = i = 0; i < image->nr_segments; i++) {
720 struct kexec_segment *ksegment;
722 ksegment = &image->segment[i];
724 * Skip purgatory as it will be modified once we put digest
727 if (ksegment->kbuf == pi->purgatory_buf)
730 ret = crypto_shash_update(desc, ksegment->kbuf,
736 * Assume rest of the buffer is filled with zero and
737 * update digest accordingly.
739 nullsz = ksegment->memsz - ksegment->bufsz;
741 unsigned long bytes = nullsz;
743 if (bytes > zero_buf_sz)
745 ret = crypto_shash_update(desc, zero_buf, bytes);
754 sha_regions[j].start = ksegment->mem;
755 sha_regions[j].len = ksegment->memsz;
760 ret = crypto_shash_final(desc, digest);
762 goto out_free_digest;
763 ret = kexec_purgatory_get_set_symbol(image, "purgatory_sha_regions",
764 sha_regions, sha_region_sz, 0);
766 goto out_free_digest;
768 ret = kexec_purgatory_get_set_symbol(image, "purgatory_sha256_digest",
769 digest, SHA256_DIGEST_SIZE, 0);
771 goto out_free_digest;
776 out_free_sha_regions:
786 #ifdef CONFIG_ARCH_HAS_KEXEC_PURGATORY
788 * kexec_purgatory_setup_kbuf - prepare buffer to load purgatory.
789 * @pi: Purgatory to be loaded.
790 * @kbuf: Buffer to setup.
792 * Allocates the memory needed for the buffer. Caller is responsible to free
793 * the memory after use.
795 * Return: 0 on success, negative errno on error.
797 static int kexec_purgatory_setup_kbuf(struct purgatory_info *pi,
798 struct kexec_buf *kbuf)
800 const Elf_Shdr *sechdrs;
801 unsigned long bss_align;
802 unsigned long bss_sz;
806 sechdrs = (void *)pi->ehdr + pi->ehdr->e_shoff;
807 kbuf->buf_align = bss_align = 1;
808 kbuf->bufsz = bss_sz = 0;
810 for (i = 0; i < pi->ehdr->e_shnum; i++) {
811 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
814 align = sechdrs[i].sh_addralign;
815 if (sechdrs[i].sh_type != SHT_NOBITS) {
816 if (kbuf->buf_align < align)
817 kbuf->buf_align = align;
818 kbuf->bufsz = ALIGN(kbuf->bufsz, align);
819 kbuf->bufsz += sechdrs[i].sh_size;
821 if (bss_align < align)
823 bss_sz = ALIGN(bss_sz, align);
824 bss_sz += sechdrs[i].sh_size;
827 kbuf->bufsz = ALIGN(kbuf->bufsz, bss_align);
828 kbuf->memsz = kbuf->bufsz + bss_sz;
829 if (kbuf->buf_align < bss_align)
830 kbuf->buf_align = bss_align;
832 kbuf->buffer = vzalloc(kbuf->bufsz);
835 pi->purgatory_buf = kbuf->buffer;
837 ret = kexec_add_buffer(kbuf);
843 vfree(pi->purgatory_buf);
844 pi->purgatory_buf = NULL;
849 * kexec_purgatory_setup_sechdrs - prepares the pi->sechdrs buffer.
850 * @pi: Purgatory to be loaded.
851 * @kbuf: Buffer prepared to store purgatory.
853 * Allocates the memory needed for the buffer. Caller is responsible to free
854 * the memory after use.
856 * Return: 0 on success, negative errno on error.
858 static int kexec_purgatory_setup_sechdrs(struct purgatory_info *pi,
859 struct kexec_buf *kbuf)
861 unsigned long bss_addr;
862 unsigned long offset;
867 * The section headers in kexec_purgatory are read-only. In order to
868 * have them modifiable make a temporary copy.
870 sechdrs = vzalloc(array_size(sizeof(Elf_Shdr), pi->ehdr->e_shnum));
873 memcpy(sechdrs, (void *)pi->ehdr + pi->ehdr->e_shoff,
874 pi->ehdr->e_shnum * sizeof(Elf_Shdr));
875 pi->sechdrs = sechdrs;
878 bss_addr = kbuf->mem + kbuf->bufsz;
879 kbuf->image->start = pi->ehdr->e_entry;
881 for (i = 0; i < pi->ehdr->e_shnum; i++) {
885 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
888 align = sechdrs[i].sh_addralign;
889 if (sechdrs[i].sh_type == SHT_NOBITS) {
890 bss_addr = ALIGN(bss_addr, align);
891 sechdrs[i].sh_addr = bss_addr;
892 bss_addr += sechdrs[i].sh_size;
896 offset = ALIGN(offset, align);
897 if (sechdrs[i].sh_flags & SHF_EXECINSTR &&
898 pi->ehdr->e_entry >= sechdrs[i].sh_addr &&
899 pi->ehdr->e_entry < (sechdrs[i].sh_addr
900 + sechdrs[i].sh_size)) {
901 kbuf->image->start -= sechdrs[i].sh_addr;
902 kbuf->image->start += kbuf->mem + offset;
905 src = (void *)pi->ehdr + sechdrs[i].sh_offset;
906 dst = pi->purgatory_buf + offset;
907 memcpy(dst, src, sechdrs[i].sh_size);
909 sechdrs[i].sh_addr = kbuf->mem + offset;
910 sechdrs[i].sh_offset = offset;
911 offset += sechdrs[i].sh_size;
917 static int kexec_apply_relocations(struct kimage *image)
920 struct purgatory_info *pi = &image->purgatory_info;
921 const Elf_Shdr *sechdrs;
923 sechdrs = (void *)pi->ehdr + pi->ehdr->e_shoff;
925 for (i = 0; i < pi->ehdr->e_shnum; i++) {
926 const Elf_Shdr *relsec;
927 const Elf_Shdr *symtab;
930 relsec = sechdrs + i;
932 if (relsec->sh_type != SHT_RELA &&
933 relsec->sh_type != SHT_REL)
937 * For section of type SHT_RELA/SHT_REL,
938 * ->sh_link contains section header index of associated
939 * symbol table. And ->sh_info contains section header
940 * index of section to which relocations apply.
942 if (relsec->sh_info >= pi->ehdr->e_shnum ||
943 relsec->sh_link >= pi->ehdr->e_shnum)
946 section = pi->sechdrs + relsec->sh_info;
947 symtab = sechdrs + relsec->sh_link;
949 if (!(section->sh_flags & SHF_ALLOC))
953 * symtab->sh_link contain section header index of associated
956 if (symtab->sh_link >= pi->ehdr->e_shnum)
957 /* Invalid section number? */
961 * Respective architecture needs to provide support for applying
962 * relocations of type SHT_RELA/SHT_REL.
964 if (relsec->sh_type == SHT_RELA)
965 ret = arch_kexec_apply_relocations_add(pi, section,
967 else if (relsec->sh_type == SHT_REL)
968 ret = arch_kexec_apply_relocations(pi, section,
978 * kexec_load_purgatory - Load and relocate the purgatory object.
979 * @image: Image to add the purgatory to.
980 * @kbuf: Memory parameters to use.
982 * Allocates the memory needed for image->purgatory_info.sechdrs and
983 * image->purgatory_info.purgatory_buf/kbuf->buffer. Caller is responsible
984 * to free the memory after use.
986 * Return: 0 on success, negative errno on error.
988 int kexec_load_purgatory(struct kimage *image, struct kexec_buf *kbuf)
990 struct purgatory_info *pi = &image->purgatory_info;
993 if (kexec_purgatory_size <= 0)
996 pi->ehdr = (const Elf_Ehdr *)kexec_purgatory;
998 ret = kexec_purgatory_setup_kbuf(pi, kbuf);
1002 ret = kexec_purgatory_setup_sechdrs(pi, kbuf);
1006 ret = kexec_apply_relocations(image);
1015 vfree(pi->purgatory_buf);
1016 pi->purgatory_buf = NULL;
1021 * kexec_purgatory_find_symbol - find a symbol in the purgatory
1022 * @pi: Purgatory to search in.
1023 * @name: Name of the symbol.
1025 * Return: pointer to symbol in read-only symtab on success, NULL on error.
1027 static const Elf_Sym *kexec_purgatory_find_symbol(struct purgatory_info *pi,
1030 const Elf_Shdr *sechdrs;
1031 const Elf_Ehdr *ehdr;
1032 const Elf_Sym *syms;
1040 sechdrs = (void *)ehdr + ehdr->e_shoff;
1042 for (i = 0; i < ehdr->e_shnum; i++) {
1043 if (sechdrs[i].sh_type != SHT_SYMTAB)
1046 if (sechdrs[i].sh_link >= ehdr->e_shnum)
1047 /* Invalid strtab section number */
1049 strtab = (void *)ehdr + sechdrs[sechdrs[i].sh_link].sh_offset;
1050 syms = (void *)ehdr + sechdrs[i].sh_offset;
1052 /* Go through symbols for a match */
1053 for (k = 0; k < sechdrs[i].sh_size/sizeof(Elf_Sym); k++) {
1054 if (ELF_ST_BIND(syms[k].st_info) != STB_GLOBAL)
1057 if (strcmp(strtab + syms[k].st_name, name) != 0)
1060 if (syms[k].st_shndx == SHN_UNDEF ||
1061 syms[k].st_shndx >= ehdr->e_shnum) {
1062 pr_debug("Symbol: %s has bad section index %d.\n",
1063 name, syms[k].st_shndx);
1067 /* Found the symbol we are looking for */
1075 void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name)
1077 struct purgatory_info *pi = &image->purgatory_info;
1081 sym = kexec_purgatory_find_symbol(pi, name);
1083 return ERR_PTR(-EINVAL);
1085 sechdr = &pi->sechdrs[sym->st_shndx];
1088 * Returns the address where symbol will finally be loaded after
1089 * kexec_load_segment()
1091 return (void *)(sechdr->sh_addr + sym->st_value);
1095 * Get or set value of a symbol. If "get_value" is true, symbol value is
1096 * returned in buf otherwise symbol value is set based on value in buf.
1098 int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name,
1099 void *buf, unsigned int size, bool get_value)
1101 struct purgatory_info *pi = &image->purgatory_info;
1106 sym = kexec_purgatory_find_symbol(pi, name);
1110 if (sym->st_size != size) {
1111 pr_err("symbol %s size mismatch: expected %lu actual %u\n",
1112 name, (unsigned long)sym->st_size, size);
1116 sec = pi->sechdrs + sym->st_shndx;
1118 if (sec->sh_type == SHT_NOBITS) {
1119 pr_err("symbol %s is in a bss section. Cannot %s\n", name,
1120 get_value ? "get" : "set");
1124 sym_buf = (char *)pi->purgatory_buf + sec->sh_offset + sym->st_value;
1127 memcpy((void *)buf, sym_buf, size);
1129 memcpy((void *)sym_buf, buf, size);
1133 #endif /* CONFIG_ARCH_HAS_KEXEC_PURGATORY */
1135 int crash_exclude_mem_range(struct crash_mem *mem,
1136 unsigned long long mstart, unsigned long long mend)
1139 unsigned long long start, end, p_start, p_end;
1140 struct crash_mem_range temp_range = {0, 0};
1142 for (i = 0; i < mem->nr_ranges; i++) {
1143 start = mem->ranges[i].start;
1144 end = mem->ranges[i].end;
1148 if (mstart > end || mend < start)
1151 /* Truncate any area outside of range */
1157 /* Found completely overlapping range */
1158 if (p_start == start && p_end == end) {
1159 mem->ranges[i].start = 0;
1160 mem->ranges[i].end = 0;
1161 if (i < mem->nr_ranges - 1) {
1162 /* Shift rest of the ranges to left */
1163 for (j = i; j < mem->nr_ranges - 1; j++) {
1164 mem->ranges[j].start =
1165 mem->ranges[j+1].start;
1166 mem->ranges[j].end =
1167 mem->ranges[j+1].end;
1171 * Continue to check if there are another overlapping ranges
1172 * from the current position because of shifting the above
1183 if (p_start > start && p_end < end) {
1184 /* Split original range */
1185 mem->ranges[i].end = p_start - 1;
1186 temp_range.start = p_end + 1;
1187 temp_range.end = end;
1188 } else if (p_start != start)
1189 mem->ranges[i].end = p_start - 1;
1191 mem->ranges[i].start = p_end + 1;
1195 /* If a split happened, add the split to array */
1196 if (!temp_range.end)
1199 /* Split happened */
1200 if (i == mem->max_nr_ranges - 1)
1203 /* Location where new range should go */
1205 if (j < mem->nr_ranges) {
1206 /* Move over all ranges one slot towards the end */
1207 for (i = mem->nr_ranges - 1; i >= j; i--)
1208 mem->ranges[i + 1] = mem->ranges[i];
1211 mem->ranges[j].start = temp_range.start;
1212 mem->ranges[j].end = temp_range.end;
1217 int crash_prepare_elf64_headers(struct crash_mem *mem, int need_kernel_map,
1218 void **addr, unsigned long *sz)
1222 unsigned long nr_cpus = num_possible_cpus(), nr_phdr, elf_sz;
1224 unsigned int cpu, i;
1225 unsigned long long notes_addr;
1226 unsigned long mstart, mend;
1228 /* extra phdr for vmcoreinfo ELF note */
1229 nr_phdr = nr_cpus + 1;
1230 nr_phdr += mem->nr_ranges;
1233 * kexec-tools creates an extra PT_LOAD phdr for kernel text mapping
1234 * area (for example, ffffffff80000000 - ffffffffa0000000 on x86_64).
1235 * I think this is required by tools like gdb. So same physical
1236 * memory will be mapped in two ELF headers. One will contain kernel
1237 * text virtual addresses and other will have __va(physical) addresses.
1241 elf_sz = sizeof(Elf64_Ehdr) + nr_phdr * sizeof(Elf64_Phdr);
1242 elf_sz = ALIGN(elf_sz, ELF_CORE_HEADER_ALIGN);
1244 buf = vzalloc(elf_sz);
1248 ehdr = (Elf64_Ehdr *)buf;
1249 phdr = (Elf64_Phdr *)(ehdr + 1);
1250 memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
1251 ehdr->e_ident[EI_CLASS] = ELFCLASS64;
1252 ehdr->e_ident[EI_DATA] = ELFDATA2LSB;
1253 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
1254 ehdr->e_ident[EI_OSABI] = ELF_OSABI;
1255 memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD);
1256 ehdr->e_type = ET_CORE;
1257 ehdr->e_machine = ELF_ARCH;
1258 ehdr->e_version = EV_CURRENT;
1259 ehdr->e_phoff = sizeof(Elf64_Ehdr);
1260 ehdr->e_ehsize = sizeof(Elf64_Ehdr);
1261 ehdr->e_phentsize = sizeof(Elf64_Phdr);
1263 /* Prepare one phdr of type PT_NOTE for each present CPU */
1264 for_each_present_cpu(cpu) {
1265 phdr->p_type = PT_NOTE;
1266 notes_addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpu));
1267 phdr->p_offset = phdr->p_paddr = notes_addr;
1268 phdr->p_filesz = phdr->p_memsz = sizeof(note_buf_t);
1273 /* Prepare one PT_NOTE header for vmcoreinfo */
1274 phdr->p_type = PT_NOTE;
1275 phdr->p_offset = phdr->p_paddr = paddr_vmcoreinfo_note();
1276 phdr->p_filesz = phdr->p_memsz = VMCOREINFO_NOTE_SIZE;
1280 /* Prepare PT_LOAD type program header for kernel text region */
1281 if (need_kernel_map) {
1282 phdr->p_type = PT_LOAD;
1283 phdr->p_flags = PF_R|PF_W|PF_X;
1284 phdr->p_vaddr = (unsigned long) _text;
1285 phdr->p_filesz = phdr->p_memsz = _end - _text;
1286 phdr->p_offset = phdr->p_paddr = __pa_symbol(_text);
1291 /* Go through all the ranges in mem->ranges[] and prepare phdr */
1292 for (i = 0; i < mem->nr_ranges; i++) {
1293 mstart = mem->ranges[i].start;
1294 mend = mem->ranges[i].end;
1296 phdr->p_type = PT_LOAD;
1297 phdr->p_flags = PF_R|PF_W|PF_X;
1298 phdr->p_offset = mstart;
1300 phdr->p_paddr = mstart;
1301 phdr->p_vaddr = (unsigned long) __va(mstart);
1302 phdr->p_filesz = phdr->p_memsz = mend - mstart + 1;
1305 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",
1306 phdr, phdr->p_vaddr, phdr->p_paddr, phdr->p_filesz,
1307 ehdr->e_phnum, phdr->p_offset);