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_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;
180 #ifdef CONFIG_KEXEC_SIG
182 kimage_validate_signature(struct kimage *image)
187 ret = arch_kexec_kernel_verify_sig(image, image->kernel_buf,
188 image->kernel_buf_len);
193 /* Certain verification errors are non-fatal if we're not
194 * checking errors, provided we aren't mandating that there
195 * must be a valid signature.
198 reason = "kexec of unsigned image";
201 reason = "kexec of image with unsupported crypto";
204 reason = "kexec of image with unavailable key";
206 if (IS_ENABLED(CONFIG_KEXEC_SIG_FORCE)) {
207 pr_notice("%s rejected\n", reason);
211 return security_locked_down(LOCKDOWN_KEXEC);
213 /* All other errors are fatal, including nomem, unparseable
214 * signatures and signature check failures - even if signatures
218 pr_notice("kernel signature verification failed (%d).\n", ret);
226 * In file mode list of segments is prepared by kernel. Copy relevant
227 * data from user space, do error checking, prepare segment list
230 kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd,
231 const char __user *cmdline_ptr,
232 unsigned long cmdline_len, unsigned flags)
238 ret = kernel_read_file_from_fd(kernel_fd, &image->kernel_buf,
239 &size, INT_MAX, READING_KEXEC_IMAGE);
242 image->kernel_buf_len = size;
244 /* IMA needs to pass the measurement list to the next kernel. */
245 ima_add_kexec_buffer(image);
247 /* Call arch image probe handlers */
248 ret = arch_kexec_kernel_image_probe(image, image->kernel_buf,
249 image->kernel_buf_len);
253 #ifdef CONFIG_KEXEC_SIG
254 ret = kimage_validate_signature(image);
259 /* It is possible that there no initramfs is being loaded */
260 if (!(flags & KEXEC_FILE_NO_INITRAMFS)) {
261 ret = kernel_read_file_from_fd(initrd_fd, &image->initrd_buf,
263 READING_KEXEC_INITRAMFS);
266 image->initrd_buf_len = size;
270 image->cmdline_buf = memdup_user(cmdline_ptr, cmdline_len);
271 if (IS_ERR(image->cmdline_buf)) {
272 ret = PTR_ERR(image->cmdline_buf);
273 image->cmdline_buf = NULL;
277 image->cmdline_buf_len = cmdline_len;
279 /* command line should be a string with last byte null */
280 if (image->cmdline_buf[cmdline_len - 1] != '\0') {
286 /* Call arch image load handlers */
287 ldata = arch_kexec_kernel_image_load(image);
290 ret = PTR_ERR(ldata);
294 image->image_loader_data = ldata;
296 /* In case of error, free up all allocated memory in this function */
298 kimage_file_post_load_cleanup(image);
303 kimage_file_alloc_init(struct kimage **rimage, int kernel_fd,
304 int initrd_fd, const char __user *cmdline_ptr,
305 unsigned long cmdline_len, unsigned long flags)
308 struct kimage *image;
309 bool kexec_on_panic = flags & KEXEC_FILE_ON_CRASH;
311 image = do_kimage_alloc_init();
315 image->file_mode = 1;
317 if (kexec_on_panic) {
318 /* Enable special crash kernel control page alloc policy. */
319 image->control_page = crashk_res.start;
320 image->type = KEXEC_TYPE_CRASH;
323 ret = kimage_file_prepare_segments(image, kernel_fd, initrd_fd,
324 cmdline_ptr, cmdline_len, flags);
328 ret = sanity_check_segment_list(image);
330 goto out_free_post_load_bufs;
333 image->control_code_page = kimage_alloc_control_pages(image,
334 get_order(KEXEC_CONTROL_PAGE_SIZE));
335 if (!image->control_code_page) {
336 pr_err("Could not allocate control_code_buffer\n");
337 goto out_free_post_load_bufs;
340 if (!kexec_on_panic) {
341 image->swap_page = kimage_alloc_control_pages(image, 0);
342 if (!image->swap_page) {
343 pr_err("Could not allocate swap buffer\n");
344 goto out_free_control_pages;
350 out_free_control_pages:
351 kimage_free_page_list(&image->control_pages);
352 out_free_post_load_bufs:
353 kimage_file_post_load_cleanup(image);
359 SYSCALL_DEFINE5(kexec_file_load, int, kernel_fd, int, initrd_fd,
360 unsigned long, cmdline_len, const char __user *, cmdline_ptr,
361 unsigned long, flags)
364 struct kimage **dest_image, *image;
366 /* We only trust the superuser with rebooting the system. */
367 if (!capable(CAP_SYS_BOOT) || kexec_load_disabled)
370 /* Make sure we have a legal set of flags */
371 if (flags != (flags & KEXEC_FILE_FLAGS))
376 if (!mutex_trylock(&kexec_mutex))
379 dest_image = &kexec_image;
380 if (flags & KEXEC_FILE_ON_CRASH) {
381 dest_image = &kexec_crash_image;
382 if (kexec_crash_image)
383 arch_kexec_unprotect_crashkres();
386 if (flags & KEXEC_FILE_UNLOAD)
390 * In case of crash, new kernel gets loaded in reserved region. It is
391 * same memory where old crash kernel might be loaded. Free any
392 * current crash dump kernel before we corrupt it.
394 if (flags & KEXEC_FILE_ON_CRASH)
395 kimage_free(xchg(&kexec_crash_image, NULL));
397 ret = kimage_file_alloc_init(&image, kernel_fd, initrd_fd, cmdline_ptr,
402 ret = machine_kexec_prepare(image);
407 * Some architecture(like S390) may touch the crash memory before
408 * machine_kexec_prepare(), we must copy vmcoreinfo data after it.
410 ret = kimage_crash_copy_vmcoreinfo(image);
414 ret = kexec_calculate_store_digests(image);
418 for (i = 0; i < image->nr_segments; i++) {
419 struct kexec_segment *ksegment;
421 ksegment = &image->segment[i];
422 pr_debug("Loading segment %d: buf=0x%p bufsz=0x%zx mem=0x%lx memsz=0x%zx\n",
423 i, ksegment->buf, ksegment->bufsz, ksegment->mem,
426 ret = kimage_load_segment(image, &image->segment[i]);
431 kimage_terminate(image);
434 * Free up any temporary buffers allocated which are not needed
435 * after image has been loaded
437 kimage_file_post_load_cleanup(image);
439 image = xchg(dest_image, image);
441 if ((flags & KEXEC_FILE_ON_CRASH) && kexec_crash_image)
442 arch_kexec_protect_crashkres();
444 mutex_unlock(&kexec_mutex);
449 static int locate_mem_hole_top_down(unsigned long start, unsigned long end,
450 struct kexec_buf *kbuf)
452 struct kimage *image = kbuf->image;
453 unsigned long temp_start, temp_end;
455 temp_end = min(end, kbuf->buf_max);
456 temp_start = temp_end - kbuf->memsz;
459 /* align down start */
460 temp_start = temp_start & (~(kbuf->buf_align - 1));
462 if (temp_start < start || temp_start < kbuf->buf_min)
465 temp_end = temp_start + kbuf->memsz - 1;
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_bottom_up(unsigned long start, unsigned long end,
488 struct kexec_buf *kbuf)
490 struct kimage *image = kbuf->image;
491 unsigned long temp_start, temp_end;
493 temp_start = max(start, kbuf->buf_min);
496 temp_start = ALIGN(temp_start, kbuf->buf_align);
497 temp_end = temp_start + kbuf->memsz - 1;
499 if (temp_end > end || temp_end > kbuf->buf_max)
502 * Make sure this does not conflict with any of existing
505 if (kimage_is_destination_range(image, temp_start, temp_end)) {
506 temp_start = temp_start + PAGE_SIZE;
510 /* We found a suitable memory range */
514 /* If we are here, we found a suitable memory range */
515 kbuf->mem = temp_start;
517 /* Success, stop navigating through remaining System RAM ranges */
521 static int locate_mem_hole_callback(struct resource *res, void *arg)
523 struct kexec_buf *kbuf = (struct kexec_buf *)arg;
524 u64 start = res->start, end = res->end;
525 unsigned long sz = end - start + 1;
527 /* Returning 0 will take to next memory range */
528 if (sz < kbuf->memsz)
531 if (end < kbuf->buf_min || start > kbuf->buf_max)
535 * Allocate memory top down with-in ram range. Otherwise bottom up
539 return locate_mem_hole_top_down(start, end, kbuf);
540 return locate_mem_hole_bottom_up(start, end, kbuf);
543 #ifdef CONFIG_ARCH_KEEP_MEMBLOCK
544 static int kexec_walk_memblock(struct kexec_buf *kbuf,
545 int (*func)(struct resource *, void *))
549 phys_addr_t mstart, mend;
550 struct resource res = { };
552 if (kbuf->image->type == KEXEC_TYPE_CRASH)
553 return func(&crashk_res, kbuf);
555 if (kbuf->top_down) {
556 for_each_free_mem_range_reverse(i, NUMA_NO_NODE, MEMBLOCK_NONE,
557 &mstart, &mend, NULL) {
559 * In memblock, end points to the first byte after the
560 * range while in kexec, end points to the last byte
565 ret = func(&res, kbuf);
570 for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE,
571 &mstart, &mend, NULL) {
573 * In memblock, end points to the first byte after the
574 * range while in kexec, end points to the last byte
579 ret = func(&res, kbuf);
588 static int kexec_walk_memblock(struct kexec_buf *kbuf,
589 int (*func)(struct resource *, void *))
596 * kexec_walk_resources - call func(data) on free memory regions
597 * @kbuf: Context info for the search. Also passed to @func.
598 * @func: Function to call for each memory region.
600 * Return: The memory walk will stop when func returns a non-zero value
601 * and that value will be returned. If all free regions are visited without
602 * func returning non-zero, then zero will be returned.
604 static int kexec_walk_resources(struct kexec_buf *kbuf,
605 int (*func)(struct resource *, void *))
607 if (kbuf->image->type == KEXEC_TYPE_CRASH)
608 return walk_iomem_res_desc(crashk_res.desc,
609 IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY,
610 crashk_res.start, crashk_res.end,
613 return walk_system_ram_res(0, ULONG_MAX, kbuf, func);
617 * kexec_locate_mem_hole - find free memory for the purgatory or the next kernel
618 * @kbuf: Parameters for the memory search.
620 * On success, kbuf->mem will have the start address of the memory region found.
622 * Return: 0 on success, negative errno on error.
624 int kexec_locate_mem_hole(struct kexec_buf *kbuf)
628 /* Arch knows where to place */
629 if (kbuf->mem != KEXEC_BUF_MEM_UNKNOWN)
632 if (!IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK))
633 ret = kexec_walk_resources(kbuf, locate_mem_hole_callback);
635 ret = kexec_walk_memblock(kbuf, locate_mem_hole_callback);
637 return ret == 1 ? 0 : -EADDRNOTAVAIL;
641 * kexec_add_buffer - place a buffer in a kexec segment
642 * @kbuf: Buffer contents and memory parameters.
644 * This function assumes that kexec_mutex is held.
645 * On successful return, @kbuf->mem will have the physical address of
646 * the buffer in memory.
648 * Return: 0 on success, negative errno on error.
650 int kexec_add_buffer(struct kexec_buf *kbuf)
653 struct kexec_segment *ksegment;
656 /* Currently adding segment this way is allowed only in file mode */
657 if (!kbuf->image->file_mode)
660 if (kbuf->image->nr_segments >= KEXEC_SEGMENT_MAX)
664 * Make sure we are not trying to add buffer after allocating
665 * control pages. All segments need to be placed first before
666 * any control pages are allocated. As control page allocation
667 * logic goes through list of segments to make sure there are
668 * no destination overlaps.
670 if (!list_empty(&kbuf->image->control_pages)) {
675 /* Ensure minimum alignment needed for segments. */
676 kbuf->memsz = ALIGN(kbuf->memsz, PAGE_SIZE);
677 kbuf->buf_align = max(kbuf->buf_align, PAGE_SIZE);
679 /* Walk the RAM ranges and allocate a suitable range for the buffer */
680 ret = kexec_locate_mem_hole(kbuf);
684 /* Found a suitable memory range */
685 ksegment = &kbuf->image->segment[kbuf->image->nr_segments];
686 ksegment->kbuf = kbuf->buffer;
687 ksegment->bufsz = kbuf->bufsz;
688 ksegment->mem = kbuf->mem;
689 ksegment->memsz = kbuf->memsz;
690 kbuf->image->nr_segments++;
694 /* Calculate and store the digest of segments */
695 static int kexec_calculate_store_digests(struct kimage *image)
697 struct crypto_shash *tfm;
698 struct shash_desc *desc;
699 int ret = 0, i, j, zero_buf_sz, sha_region_sz;
700 size_t desc_size, nullsz;
703 struct kexec_sha_region *sha_regions;
704 struct purgatory_info *pi = &image->purgatory_info;
706 if (!IS_ENABLED(CONFIG_ARCH_HAS_KEXEC_PURGATORY))
709 zero_buf = __va(page_to_pfn(ZERO_PAGE(0)) << PAGE_SHIFT);
710 zero_buf_sz = PAGE_SIZE;
712 tfm = crypto_alloc_shash("sha256", 0, 0);
718 desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
719 desc = kzalloc(desc_size, GFP_KERNEL);
725 sha_region_sz = KEXEC_SEGMENT_MAX * sizeof(struct kexec_sha_region);
726 sha_regions = vzalloc(sha_region_sz);
732 ret = crypto_shash_init(desc);
734 goto out_free_sha_regions;
736 digest = kzalloc(SHA256_DIGEST_SIZE, GFP_KERNEL);
739 goto out_free_sha_regions;
742 for (j = i = 0; i < image->nr_segments; i++) {
743 struct kexec_segment *ksegment;
745 ksegment = &image->segment[i];
747 * Skip purgatory as it will be modified once we put digest
750 if (ksegment->kbuf == pi->purgatory_buf)
753 ret = crypto_shash_update(desc, ksegment->kbuf,
759 * Assume rest of the buffer is filled with zero and
760 * update digest accordingly.
762 nullsz = ksegment->memsz - ksegment->bufsz;
764 unsigned long bytes = nullsz;
766 if (bytes > zero_buf_sz)
768 ret = crypto_shash_update(desc, zero_buf, bytes);
777 sha_regions[j].start = ksegment->mem;
778 sha_regions[j].len = ksegment->memsz;
783 ret = crypto_shash_final(desc, digest);
785 goto out_free_digest;
786 ret = kexec_purgatory_get_set_symbol(image, "purgatory_sha_regions",
787 sha_regions, sha_region_sz, 0);
789 goto out_free_digest;
791 ret = kexec_purgatory_get_set_symbol(image, "purgatory_sha256_digest",
792 digest, SHA256_DIGEST_SIZE, 0);
794 goto out_free_digest;
799 out_free_sha_regions:
809 #ifdef CONFIG_ARCH_HAS_KEXEC_PURGATORY
811 * kexec_purgatory_setup_kbuf - prepare buffer to load purgatory.
812 * @pi: Purgatory to be loaded.
813 * @kbuf: Buffer to setup.
815 * Allocates the memory needed for the buffer. Caller is responsible to free
816 * the memory after use.
818 * Return: 0 on success, negative errno on error.
820 static int kexec_purgatory_setup_kbuf(struct purgatory_info *pi,
821 struct kexec_buf *kbuf)
823 const Elf_Shdr *sechdrs;
824 unsigned long bss_align;
825 unsigned long bss_sz;
829 sechdrs = (void *)pi->ehdr + pi->ehdr->e_shoff;
830 kbuf->buf_align = bss_align = 1;
831 kbuf->bufsz = bss_sz = 0;
833 for (i = 0; i < pi->ehdr->e_shnum; i++) {
834 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
837 align = sechdrs[i].sh_addralign;
838 if (sechdrs[i].sh_type != SHT_NOBITS) {
839 if (kbuf->buf_align < align)
840 kbuf->buf_align = align;
841 kbuf->bufsz = ALIGN(kbuf->bufsz, align);
842 kbuf->bufsz += sechdrs[i].sh_size;
844 if (bss_align < align)
846 bss_sz = ALIGN(bss_sz, align);
847 bss_sz += sechdrs[i].sh_size;
850 kbuf->bufsz = ALIGN(kbuf->bufsz, bss_align);
851 kbuf->memsz = kbuf->bufsz + bss_sz;
852 if (kbuf->buf_align < bss_align)
853 kbuf->buf_align = bss_align;
855 kbuf->buffer = vzalloc(kbuf->bufsz);
858 pi->purgatory_buf = kbuf->buffer;
860 ret = kexec_add_buffer(kbuf);
866 vfree(pi->purgatory_buf);
867 pi->purgatory_buf = NULL;
872 * kexec_purgatory_setup_sechdrs - prepares the pi->sechdrs buffer.
873 * @pi: Purgatory to be loaded.
874 * @kbuf: Buffer prepared to store purgatory.
876 * Allocates the memory needed for the buffer. Caller is responsible to free
877 * the memory after use.
879 * Return: 0 on success, negative errno on error.
881 static int kexec_purgatory_setup_sechdrs(struct purgatory_info *pi,
882 struct kexec_buf *kbuf)
884 unsigned long bss_addr;
885 unsigned long offset;
890 * The section headers in kexec_purgatory are read-only. In order to
891 * have them modifiable make a temporary copy.
893 sechdrs = vzalloc(array_size(sizeof(Elf_Shdr), pi->ehdr->e_shnum));
896 memcpy(sechdrs, (void *)pi->ehdr + pi->ehdr->e_shoff,
897 pi->ehdr->e_shnum * sizeof(Elf_Shdr));
898 pi->sechdrs = sechdrs;
901 bss_addr = kbuf->mem + kbuf->bufsz;
902 kbuf->image->start = pi->ehdr->e_entry;
904 for (i = 0; i < pi->ehdr->e_shnum; i++) {
908 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
911 align = sechdrs[i].sh_addralign;
912 if (sechdrs[i].sh_type == SHT_NOBITS) {
913 bss_addr = ALIGN(bss_addr, align);
914 sechdrs[i].sh_addr = bss_addr;
915 bss_addr += sechdrs[i].sh_size;
919 offset = ALIGN(offset, align);
920 if (sechdrs[i].sh_flags & SHF_EXECINSTR &&
921 pi->ehdr->e_entry >= sechdrs[i].sh_addr &&
922 pi->ehdr->e_entry < (sechdrs[i].sh_addr
923 + sechdrs[i].sh_size)) {
924 kbuf->image->start -= sechdrs[i].sh_addr;
925 kbuf->image->start += kbuf->mem + offset;
928 src = (void *)pi->ehdr + sechdrs[i].sh_offset;
929 dst = pi->purgatory_buf + offset;
930 memcpy(dst, src, sechdrs[i].sh_size);
932 sechdrs[i].sh_addr = kbuf->mem + offset;
933 sechdrs[i].sh_offset = offset;
934 offset += sechdrs[i].sh_size;
940 static int kexec_apply_relocations(struct kimage *image)
943 struct purgatory_info *pi = &image->purgatory_info;
944 const Elf_Shdr *sechdrs;
946 sechdrs = (void *)pi->ehdr + pi->ehdr->e_shoff;
948 for (i = 0; i < pi->ehdr->e_shnum; i++) {
949 const Elf_Shdr *relsec;
950 const Elf_Shdr *symtab;
953 relsec = sechdrs + i;
955 if (relsec->sh_type != SHT_RELA &&
956 relsec->sh_type != SHT_REL)
960 * For section of type SHT_RELA/SHT_REL,
961 * ->sh_link contains section header index of associated
962 * symbol table. And ->sh_info contains section header
963 * index of section to which relocations apply.
965 if (relsec->sh_info >= pi->ehdr->e_shnum ||
966 relsec->sh_link >= pi->ehdr->e_shnum)
969 section = pi->sechdrs + relsec->sh_info;
970 symtab = sechdrs + relsec->sh_link;
972 if (!(section->sh_flags & SHF_ALLOC))
976 * symtab->sh_link contain section header index of associated
979 if (symtab->sh_link >= pi->ehdr->e_shnum)
980 /* Invalid section number? */
984 * Respective architecture needs to provide support for applying
985 * relocations of type SHT_RELA/SHT_REL.
987 if (relsec->sh_type == SHT_RELA)
988 ret = arch_kexec_apply_relocations_add(pi, section,
990 else if (relsec->sh_type == SHT_REL)
991 ret = arch_kexec_apply_relocations(pi, section,
1001 * kexec_load_purgatory - Load and relocate the purgatory object.
1002 * @image: Image to add the purgatory to.
1003 * @kbuf: Memory parameters to use.
1005 * Allocates the memory needed for image->purgatory_info.sechdrs and
1006 * image->purgatory_info.purgatory_buf/kbuf->buffer. Caller is responsible
1007 * to free the memory after use.
1009 * Return: 0 on success, negative errno on error.
1011 int kexec_load_purgatory(struct kimage *image, struct kexec_buf *kbuf)
1013 struct purgatory_info *pi = &image->purgatory_info;
1016 if (kexec_purgatory_size <= 0)
1019 pi->ehdr = (const Elf_Ehdr *)kexec_purgatory;
1021 ret = kexec_purgatory_setup_kbuf(pi, kbuf);
1025 ret = kexec_purgatory_setup_sechdrs(pi, kbuf);
1029 ret = kexec_apply_relocations(image);
1038 vfree(pi->purgatory_buf);
1039 pi->purgatory_buf = NULL;
1044 * kexec_purgatory_find_symbol - find a symbol in the purgatory
1045 * @pi: Purgatory to search in.
1046 * @name: Name of the symbol.
1048 * Return: pointer to symbol in read-only symtab on success, NULL on error.
1050 static const Elf_Sym *kexec_purgatory_find_symbol(struct purgatory_info *pi,
1053 const Elf_Shdr *sechdrs;
1054 const Elf_Ehdr *ehdr;
1055 const Elf_Sym *syms;
1063 sechdrs = (void *)ehdr + ehdr->e_shoff;
1065 for (i = 0; i < ehdr->e_shnum; i++) {
1066 if (sechdrs[i].sh_type != SHT_SYMTAB)
1069 if (sechdrs[i].sh_link >= ehdr->e_shnum)
1070 /* Invalid strtab section number */
1072 strtab = (void *)ehdr + sechdrs[sechdrs[i].sh_link].sh_offset;
1073 syms = (void *)ehdr + sechdrs[i].sh_offset;
1075 /* Go through symbols for a match */
1076 for (k = 0; k < sechdrs[i].sh_size/sizeof(Elf_Sym); k++) {
1077 if (ELF_ST_BIND(syms[k].st_info) != STB_GLOBAL)
1080 if (strcmp(strtab + syms[k].st_name, name) != 0)
1083 if (syms[k].st_shndx == SHN_UNDEF ||
1084 syms[k].st_shndx >= ehdr->e_shnum) {
1085 pr_debug("Symbol: %s has bad section index %d.\n",
1086 name, syms[k].st_shndx);
1090 /* Found the symbol we are looking for */
1098 void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name)
1100 struct purgatory_info *pi = &image->purgatory_info;
1104 sym = kexec_purgatory_find_symbol(pi, name);
1106 return ERR_PTR(-EINVAL);
1108 sechdr = &pi->sechdrs[sym->st_shndx];
1111 * Returns the address where symbol will finally be loaded after
1112 * kexec_load_segment()
1114 return (void *)(sechdr->sh_addr + sym->st_value);
1118 * Get or set value of a symbol. If "get_value" is true, symbol value is
1119 * returned in buf otherwise symbol value is set based on value in buf.
1121 int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name,
1122 void *buf, unsigned int size, bool get_value)
1124 struct purgatory_info *pi = &image->purgatory_info;
1129 sym = kexec_purgatory_find_symbol(pi, name);
1133 if (sym->st_size != size) {
1134 pr_err("symbol %s size mismatch: expected %lu actual %u\n",
1135 name, (unsigned long)sym->st_size, size);
1139 sec = pi->sechdrs + sym->st_shndx;
1141 if (sec->sh_type == SHT_NOBITS) {
1142 pr_err("symbol %s is in a bss section. Cannot %s\n", name,
1143 get_value ? "get" : "set");
1147 sym_buf = (char *)pi->purgatory_buf + sec->sh_offset + sym->st_value;
1150 memcpy((void *)buf, sym_buf, size);
1152 memcpy((void *)sym_buf, buf, size);
1156 #endif /* CONFIG_ARCH_HAS_KEXEC_PURGATORY */
1158 int crash_exclude_mem_range(struct crash_mem *mem,
1159 unsigned long long mstart, unsigned long long mend)
1162 unsigned long long start, end;
1163 struct crash_mem_range temp_range = {0, 0};
1165 for (i = 0; i < mem->nr_ranges; i++) {
1166 start = mem->ranges[i].start;
1167 end = mem->ranges[i].end;
1169 if (mstart > end || mend < start)
1172 /* Truncate any area outside of range */
1178 /* Found completely overlapping range */
1179 if (mstart == start && mend == end) {
1180 mem->ranges[i].start = 0;
1181 mem->ranges[i].end = 0;
1182 if (i < mem->nr_ranges - 1) {
1183 /* Shift rest of the ranges to left */
1184 for (j = i; j < mem->nr_ranges - 1; j++) {
1185 mem->ranges[j].start =
1186 mem->ranges[j+1].start;
1187 mem->ranges[j].end =
1188 mem->ranges[j+1].end;
1195 if (mstart > start && mend < end) {
1196 /* Split original range */
1197 mem->ranges[i].end = mstart - 1;
1198 temp_range.start = mend + 1;
1199 temp_range.end = end;
1200 } else if (mstart != start)
1201 mem->ranges[i].end = mstart - 1;
1203 mem->ranges[i].start = mend + 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 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 */
1294 phdr->p_type = PT_LOAD;
1295 phdr->p_flags = PF_R|PF_W|PF_X;
1296 phdr->p_vaddr = (Elf64_Addr)_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 long) __va(mstart);
1314 phdr->p_filesz = phdr->p_memsz = mend - mstart + 1;
1318 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",
1319 phdr, phdr->p_vaddr, phdr->p_paddr, phdr->p_filesz,
1320 ehdr->e_phnum, phdr->p_offset);