1 // SPDX-License-Identifier: GPL-2.0-only
3 * fs/proc/vmcore.c Interface for accessing the crash
4 * dump from the system's previous life.
5 * Heavily borrowed from fs/proc/kcore.c
7 * Copyright (C) IBM Corporation, 2004. All rights reserved
12 #include <linux/kcore.h>
13 #include <linux/user.h>
14 #include <linux/elf.h>
15 #include <linux/elfcore.h>
16 #include <linux/export.h>
17 #include <linux/slab.h>
18 #include <linux/highmem.h>
19 #include <linux/printk.h>
20 #include <linux/memblock.h>
21 #include <linux/init.h>
22 #include <linux/crash_dump.h>
23 #include <linux/list.h>
24 #include <linux/moduleparam.h>
25 #include <linux/mutex.h>
26 #include <linux/vmalloc.h>
27 #include <linux/pagemap.h>
28 #include <linux/uaccess.h>
29 #include <linux/cc_platform.h>
33 /* List representing chunks of contiguous memory areas and their offsets in
36 static LIST_HEAD(vmcore_list);
38 /* Stores the pointer to the buffer containing kernel elf core headers. */
39 static char *elfcorebuf;
40 static size_t elfcorebuf_sz;
41 static size_t elfcorebuf_sz_orig;
43 static char *elfnotes_buf;
44 static size_t elfnotes_sz;
45 /* Size of all notes minus the device dump notes */
46 static size_t elfnotes_orig_sz;
48 /* Total size of vmcore file. */
49 static u64 vmcore_size;
51 static struct proc_dir_entry *proc_vmcore;
53 #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
54 /* Device Dump list and mutex to synchronize access to list */
55 static LIST_HEAD(vmcoredd_list);
56 static DEFINE_MUTEX(vmcoredd_mutex);
58 static bool vmcoredd_disabled;
59 core_param(novmcoredd, vmcoredd_disabled, bool, 0);
60 #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
62 /* Device Dump Size */
63 static size_t vmcoredd_orig_sz;
65 static DECLARE_RWSEM(vmcore_cb_rwsem);
66 /* List of registered vmcore callbacks. */
67 static LIST_HEAD(vmcore_cb_list);
68 /* Whether the vmcore has been opened once. */
69 static bool vmcore_opened;
71 void register_vmcore_cb(struct vmcore_cb *cb)
73 down_write(&vmcore_cb_rwsem);
74 INIT_LIST_HEAD(&cb->next);
75 list_add_tail(&cb->next, &vmcore_cb_list);
77 * Registering a vmcore callback after the vmcore was opened is
78 * very unusual (e.g., manual driver loading).
81 pr_warn_once("Unexpected vmcore callback registration\n");
82 up_write(&vmcore_cb_rwsem);
84 EXPORT_SYMBOL_GPL(register_vmcore_cb);
86 void unregister_vmcore_cb(struct vmcore_cb *cb)
88 down_write(&vmcore_cb_rwsem);
91 * Unregistering a vmcore callback after the vmcore was opened is
92 * very unusual (e.g., forced driver removal), but we cannot stop
96 pr_warn_once("Unexpected vmcore callback unregistration\n");
97 up_write(&vmcore_cb_rwsem);
99 EXPORT_SYMBOL_GPL(unregister_vmcore_cb);
101 static bool pfn_is_ram(unsigned long pfn)
103 struct vmcore_cb *cb;
106 lockdep_assert_held_read(&vmcore_cb_rwsem);
108 list_for_each_entry(cb, &vmcore_cb_list, next) {
109 if (unlikely(!cb->pfn_is_ram))
111 ret = cb->pfn_is_ram(cb, pfn);
119 static int open_vmcore(struct inode *inode, struct file *file)
121 down_read(&vmcore_cb_rwsem);
122 vmcore_opened = true;
123 up_read(&vmcore_cb_rwsem);
128 /* Reads a page from the oldmem device from given offset. */
129 ssize_t read_from_oldmem(char *buf, size_t count,
130 u64 *ppos, int userbuf,
133 unsigned long pfn, offset;
135 ssize_t read = 0, tmp;
140 offset = (unsigned long)(*ppos % PAGE_SIZE);
141 pfn = (unsigned long)(*ppos / PAGE_SIZE);
143 down_read(&vmcore_cb_rwsem);
145 if (count > (PAGE_SIZE - offset))
146 nr_bytes = PAGE_SIZE - offset;
150 /* If pfn is not ram, return zeros for sparse dump files */
151 if (!pfn_is_ram(pfn)) {
154 memset(buf, 0, nr_bytes);
155 else if (clear_user(buf, nr_bytes))
159 tmp = copy_oldmem_page_encrypted(pfn, buf,
164 tmp = copy_oldmem_page(pfn, buf, nr_bytes,
168 up_read(&vmcore_cb_rwsem);
180 up_read(&vmcore_cb_rwsem);
185 * Architectures may override this function to allocate ELF header in 2nd kernel
187 int __weak elfcorehdr_alloc(unsigned long long *addr, unsigned long long *size)
193 * Architectures may override this function to free header
195 void __weak elfcorehdr_free(unsigned long long addr)
199 * Architectures may override this function to read from ELF header
201 ssize_t __weak elfcorehdr_read(char *buf, size_t count, u64 *ppos)
203 return read_from_oldmem(buf, count, ppos, 0, false);
207 * Architectures may override this function to read from notes sections
209 ssize_t __weak elfcorehdr_read_notes(char *buf, size_t count, u64 *ppos)
211 return read_from_oldmem(buf, count, ppos, 0, cc_platform_has(CC_ATTR_MEM_ENCRYPT));
215 * Architectures may override this function to map oldmem
217 int __weak remap_oldmem_pfn_range(struct vm_area_struct *vma,
218 unsigned long from, unsigned long pfn,
219 unsigned long size, pgprot_t prot)
221 prot = pgprot_encrypted(prot);
222 return remap_pfn_range(vma, from, pfn, size, prot);
226 * Architectures which support memory encryption override this.
229 copy_oldmem_page_encrypted(unsigned long pfn, char *buf, size_t csize,
230 unsigned long offset, int userbuf)
232 return copy_oldmem_page(pfn, buf, csize, offset, userbuf);
236 * Copy to either kernel or user space
238 static int copy_to(void *target, void *src, size_t size, int userbuf)
241 if (copy_to_user((char __user *) target, src, size))
244 memcpy(target, src, size);
249 #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
250 static int vmcoredd_copy_dumps(void *dst, u64 start, size_t size, int userbuf)
252 struct vmcoredd_node *dump;
258 mutex_lock(&vmcoredd_mutex);
259 list_for_each_entry(dump, &vmcoredd_list, list) {
260 if (start < offset + dump->size) {
261 tsz = min(offset + (u64)dump->size - start, (u64)size);
262 buf = dump->buf + start - offset;
263 if (copy_to(dst, buf, tsz, userbuf)) {
272 /* Leave now if buffer filled already */
276 offset += dump->size;
280 mutex_unlock(&vmcoredd_mutex);
285 static int vmcoredd_mmap_dumps(struct vm_area_struct *vma, unsigned long dst,
286 u64 start, size_t size)
288 struct vmcoredd_node *dump;
294 mutex_lock(&vmcoredd_mutex);
295 list_for_each_entry(dump, &vmcoredd_list, list) {
296 if (start < offset + dump->size) {
297 tsz = min(offset + (u64)dump->size - start, (u64)size);
298 buf = dump->buf + start - offset;
299 if (remap_vmalloc_range_partial(vma, dst, buf, 0,
309 /* Leave now if buffer filled already */
313 offset += dump->size;
317 mutex_unlock(&vmcoredd_mutex);
320 #endif /* CONFIG_MMU */
321 #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
323 /* Read from the ELF header and then the crash dump. On error, negative value is
324 * returned otherwise number of bytes read are returned.
326 static ssize_t __read_vmcore(char *buffer, size_t buflen, loff_t *fpos,
329 ssize_t acc = 0, tmp;
332 struct vmcore *m = NULL;
334 if (buflen == 0 || *fpos >= vmcore_size)
337 /* trim buflen to not go beyond EOF */
338 if (buflen > vmcore_size - *fpos)
339 buflen = vmcore_size - *fpos;
341 /* Read ELF core header */
342 if (*fpos < elfcorebuf_sz) {
343 tsz = min(elfcorebuf_sz - (size_t)*fpos, buflen);
344 if (copy_to(buffer, elfcorebuf + *fpos, tsz, userbuf))
351 /* leave now if filled buffer already */
356 /* Read Elf note segment */
357 if (*fpos < elfcorebuf_sz + elfnotes_sz) {
360 /* We add device dumps before other elf notes because the
361 * other elf notes may not fill the elf notes buffer
362 * completely and we will end up with zero-filled data
363 * between the elf notes and the device dumps. Tools will
364 * then try to decode this zero-filled data as valid notes
365 * and we don't want that. Hence, adding device dumps before
366 * the other elf notes ensure that zero-filled data can be
369 #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
370 /* Read device dumps */
371 if (*fpos < elfcorebuf_sz + vmcoredd_orig_sz) {
372 tsz = min(elfcorebuf_sz + vmcoredd_orig_sz -
373 (size_t)*fpos, buflen);
374 start = *fpos - elfcorebuf_sz;
375 if (vmcoredd_copy_dumps(buffer, start, tsz, userbuf))
383 /* leave now if filled buffer already */
387 #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
389 /* Read remaining elf notes */
390 tsz = min(elfcorebuf_sz + elfnotes_sz - (size_t)*fpos, buflen);
391 kaddr = elfnotes_buf + *fpos - elfcorebuf_sz - vmcoredd_orig_sz;
392 if (copy_to(buffer, kaddr, tsz, userbuf))
400 /* leave now if filled buffer already */
405 list_for_each_entry(m, &vmcore_list, list) {
406 if (*fpos < m->offset + m->size) {
407 tsz = (size_t)min_t(unsigned long long,
408 m->offset + m->size - *fpos,
410 start = m->paddr + *fpos - m->offset;
411 tmp = read_from_oldmem(buffer, tsz, &start,
412 userbuf, cc_platform_has(CC_ATTR_MEM_ENCRYPT));
420 /* leave now if filled buffer already */
429 static ssize_t read_vmcore(struct file *file, char __user *buffer,
430 size_t buflen, loff_t *fpos)
432 return __read_vmcore((__force char *) buffer, buflen, fpos, 1);
436 * The vmcore fault handler uses the page cache and fills data using the
437 * standard __vmcore_read() function.
439 * On s390 the fault handler is used for memory regions that can't be mapped
440 * directly with remap_pfn_range().
442 static vm_fault_t mmap_vmcore_fault(struct vm_fault *vmf)
445 struct address_space *mapping = vmf->vma->vm_file->f_mapping;
446 pgoff_t index = vmf->pgoff;
452 page = find_or_create_page(mapping, index, GFP_KERNEL);
455 if (!PageUptodate(page)) {
456 offset = (loff_t) index << PAGE_SHIFT;
457 buf = __va((page_to_pfn(page) << PAGE_SHIFT));
458 rc = __read_vmcore(buf, PAGE_SIZE, &offset, 0);
462 return vmf_error(rc);
464 SetPageUptodate(page);
470 return VM_FAULT_SIGBUS;
474 static const struct vm_operations_struct vmcore_mmap_ops = {
475 .fault = mmap_vmcore_fault,
479 * vmcore_alloc_buf - allocate buffer in vmalloc memory
480 * @sizez: size of buffer
482 * If CONFIG_MMU is defined, use vmalloc_user() to allow users to mmap
483 * the buffer to user-space by means of remap_vmalloc_range().
485 * If CONFIG_MMU is not defined, use vzalloc() since mmap_vmcore() is
486 * disabled and there's no need to allow users to mmap the buffer.
488 static inline char *vmcore_alloc_buf(size_t size)
491 return vmalloc_user(size);
493 return vzalloc(size);
498 * Disable mmap_vmcore() if CONFIG_MMU is not defined. MMU is
499 * essential for mmap_vmcore() in order to map physically
500 * non-contiguous objects (ELF header, ELF note segment and memory
501 * regions in the 1st kernel pointed to by PT_LOAD entries) into
502 * virtually contiguous user-space in ELF layout.
506 * remap_oldmem_pfn_checked - do remap_oldmem_pfn_range replacing all pages
507 * reported as not being ram with the zero page.
509 * @vma: vm_area_struct describing requested mapping
510 * @from: start remapping from
511 * @pfn: page frame number to start remapping to
512 * @size: remapping size
513 * @prot: protection bits
515 * Returns zero on success, -EAGAIN on failure.
517 static int remap_oldmem_pfn_checked(struct vm_area_struct *vma,
518 unsigned long from, unsigned long pfn,
519 unsigned long size, pgprot_t prot)
521 unsigned long map_size;
522 unsigned long pos_start, pos_end, pos;
523 unsigned long zeropage_pfn = my_zero_pfn(0);
527 pos_end = pfn + (size >> PAGE_SHIFT);
529 for (pos = pos_start; pos < pos_end; ++pos) {
530 if (!pfn_is_ram(pos)) {
532 * We hit a page which is not ram. Remap the continuous
533 * region between pos_start and pos-1 and replace
534 * the non-ram page at pos with the zero page.
536 if (pos > pos_start) {
537 /* Remap continuous region */
538 map_size = (pos - pos_start) << PAGE_SHIFT;
539 if (remap_oldmem_pfn_range(vma, from + len,
545 /* Remap the zero page */
546 if (remap_oldmem_pfn_range(vma, from + len,
554 if (pos > pos_start) {
556 map_size = (pos - pos_start) << PAGE_SHIFT;
557 if (remap_oldmem_pfn_range(vma, from + len, pos_start,
563 do_munmap(vma->vm_mm, from, len, NULL);
567 static int vmcore_remap_oldmem_pfn(struct vm_area_struct *vma,
568 unsigned long from, unsigned long pfn,
569 unsigned long size, pgprot_t prot)
574 * Check if oldmem_pfn_is_ram was registered to avoid
575 * looping over all pages without a reason.
577 down_read(&vmcore_cb_rwsem);
578 if (!list_empty(&vmcore_cb_list))
579 ret = remap_oldmem_pfn_checked(vma, from, pfn, size, prot);
581 ret = remap_oldmem_pfn_range(vma, from, pfn, size, prot);
582 up_read(&vmcore_cb_rwsem);
586 static int mmap_vmcore(struct file *file, struct vm_area_struct *vma)
588 size_t size = vma->vm_end - vma->vm_start;
589 u64 start, end, len, tsz;
592 start = (u64)vma->vm_pgoff << PAGE_SHIFT;
595 if (size > vmcore_size || end > vmcore_size)
598 if (vma->vm_flags & (VM_WRITE | VM_EXEC))
601 vma->vm_flags &= ~(VM_MAYWRITE | VM_MAYEXEC);
602 vma->vm_flags |= VM_MIXEDMAP;
603 vma->vm_ops = &vmcore_mmap_ops;
607 if (start < elfcorebuf_sz) {
610 tsz = min(elfcorebuf_sz - (size_t)start, size);
611 pfn = __pa(elfcorebuf + start) >> PAGE_SHIFT;
612 if (remap_pfn_range(vma, vma->vm_start, pfn, tsz,
623 if (start < elfcorebuf_sz + elfnotes_sz) {
626 /* We add device dumps before other elf notes because the
627 * other elf notes may not fill the elf notes buffer
628 * completely and we will end up with zero-filled data
629 * between the elf notes and the device dumps. Tools will
630 * then try to decode this zero-filled data as valid notes
631 * and we don't want that. Hence, adding device dumps before
632 * the other elf notes ensure that zero-filled data can be
633 * avoided. This also ensures that the device dumps and
634 * other elf notes can be properly mmaped at page aligned
637 #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
638 /* Read device dumps */
639 if (start < elfcorebuf_sz + vmcoredd_orig_sz) {
642 tsz = min(elfcorebuf_sz + vmcoredd_orig_sz -
643 (size_t)start, size);
644 start_off = start - elfcorebuf_sz;
645 if (vmcoredd_mmap_dumps(vma, vma->vm_start + len,
653 /* leave now if filled buffer already */
657 #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
659 /* Read remaining elf notes */
660 tsz = min(elfcorebuf_sz + elfnotes_sz - (size_t)start, size);
661 kaddr = elfnotes_buf + start - elfcorebuf_sz - vmcoredd_orig_sz;
662 if (remap_vmalloc_range_partial(vma, vma->vm_start + len,
674 list_for_each_entry(m, &vmcore_list, list) {
675 if (start < m->offset + m->size) {
678 tsz = (size_t)min_t(unsigned long long,
679 m->offset + m->size - start, size);
680 paddr = m->paddr + start - m->offset;
681 if (vmcore_remap_oldmem_pfn(vma, vma->vm_start + len,
682 paddr >> PAGE_SHIFT, tsz,
696 do_munmap(vma->vm_mm, vma->vm_start, len, NULL);
700 static int mmap_vmcore(struct file *file, struct vm_area_struct *vma)
706 static const struct proc_ops vmcore_proc_ops = {
707 .proc_open = open_vmcore,
708 .proc_read = read_vmcore,
709 .proc_lseek = default_llseek,
710 .proc_mmap = mmap_vmcore,
713 static struct vmcore* __init get_new_element(void)
715 return kzalloc(sizeof(struct vmcore), GFP_KERNEL);
718 static u64 get_vmcore_size(size_t elfsz, size_t elfnotesegsz,
719 struct list_head *vc_list)
724 size = elfsz + elfnotesegsz;
725 list_for_each_entry(m, vc_list, list) {
732 * update_note_header_size_elf64 - update p_memsz member of each PT_NOTE entry
734 * @ehdr_ptr: ELF header
736 * This function updates p_memsz member of each PT_NOTE entry in the
737 * program header table pointed to by @ehdr_ptr to real size of ELF
740 static int __init update_note_header_size_elf64(const Elf64_Ehdr *ehdr_ptr)
743 Elf64_Phdr *phdr_ptr;
744 Elf64_Nhdr *nhdr_ptr;
746 phdr_ptr = (Elf64_Phdr *)(ehdr_ptr + 1);
747 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
749 u64 offset, max_sz, sz, real_sz = 0;
750 if (phdr_ptr->p_type != PT_NOTE)
752 max_sz = phdr_ptr->p_memsz;
753 offset = phdr_ptr->p_offset;
754 notes_section = kmalloc(max_sz, GFP_KERNEL);
757 rc = elfcorehdr_read_notes(notes_section, max_sz, &offset);
759 kfree(notes_section);
762 nhdr_ptr = notes_section;
763 while (nhdr_ptr->n_namesz != 0) {
764 sz = sizeof(Elf64_Nhdr) +
765 (((u64)nhdr_ptr->n_namesz + 3) & ~3) +
766 (((u64)nhdr_ptr->n_descsz + 3) & ~3);
767 if ((real_sz + sz) > max_sz) {
768 pr_warn("Warning: Exceeded p_memsz, dropping PT_NOTE entry n_namesz=0x%x, n_descsz=0x%x\n",
769 nhdr_ptr->n_namesz, nhdr_ptr->n_descsz);
773 nhdr_ptr = (Elf64_Nhdr*)((char*)nhdr_ptr + sz);
775 kfree(notes_section);
776 phdr_ptr->p_memsz = real_sz;
778 pr_warn("Warning: Zero PT_NOTE entries found\n");
786 * get_note_number_and_size_elf64 - get the number of PT_NOTE program
787 * headers and sum of real size of their ELF note segment headers and
790 * @ehdr_ptr: ELF header
791 * @nr_ptnote: buffer for the number of PT_NOTE program headers
792 * @sz_ptnote: buffer for size of unique PT_NOTE program header
794 * This function is used to merge multiple PT_NOTE program headers
795 * into a unique single one. The resulting unique entry will have
796 * @sz_ptnote in its phdr->p_mem.
798 * It is assumed that program headers with PT_NOTE type pointed to by
799 * @ehdr_ptr has already been updated by update_note_header_size_elf64
800 * and each of PT_NOTE program headers has actual ELF note segment
801 * size in its p_memsz member.
803 static int __init get_note_number_and_size_elf64(const Elf64_Ehdr *ehdr_ptr,
804 int *nr_ptnote, u64 *sz_ptnote)
807 Elf64_Phdr *phdr_ptr;
809 *nr_ptnote = *sz_ptnote = 0;
811 phdr_ptr = (Elf64_Phdr *)(ehdr_ptr + 1);
812 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
813 if (phdr_ptr->p_type != PT_NOTE)
816 *sz_ptnote += phdr_ptr->p_memsz;
823 * copy_notes_elf64 - copy ELF note segments in a given buffer
825 * @ehdr_ptr: ELF header
826 * @notes_buf: buffer into which ELF note segments are copied
828 * This function is used to copy ELF note segment in the 1st kernel
829 * into the buffer @notes_buf in the 2nd kernel. It is assumed that
830 * size of the buffer @notes_buf is equal to or larger than sum of the
831 * real ELF note segment headers and data.
833 * It is assumed that program headers with PT_NOTE type pointed to by
834 * @ehdr_ptr has already been updated by update_note_header_size_elf64
835 * and each of PT_NOTE program headers has actual ELF note segment
836 * size in its p_memsz member.
838 static int __init copy_notes_elf64(const Elf64_Ehdr *ehdr_ptr, char *notes_buf)
841 Elf64_Phdr *phdr_ptr;
843 phdr_ptr = (Elf64_Phdr*)(ehdr_ptr + 1);
845 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
847 if (phdr_ptr->p_type != PT_NOTE)
849 offset = phdr_ptr->p_offset;
850 rc = elfcorehdr_read_notes(notes_buf, phdr_ptr->p_memsz,
854 notes_buf += phdr_ptr->p_memsz;
860 /* Merges all the PT_NOTE headers into one. */
861 static int __init merge_note_headers_elf64(char *elfptr, size_t *elfsz,
862 char **notes_buf, size_t *notes_sz)
864 int i, nr_ptnote=0, rc=0;
866 Elf64_Ehdr *ehdr_ptr;
868 u64 phdr_sz = 0, note_off;
870 ehdr_ptr = (Elf64_Ehdr *)elfptr;
872 rc = update_note_header_size_elf64(ehdr_ptr);
876 rc = get_note_number_and_size_elf64(ehdr_ptr, &nr_ptnote, &phdr_sz);
880 *notes_sz = roundup(phdr_sz, PAGE_SIZE);
881 *notes_buf = vmcore_alloc_buf(*notes_sz);
885 rc = copy_notes_elf64(ehdr_ptr, *notes_buf);
889 /* Prepare merged PT_NOTE program header. */
890 phdr.p_type = PT_NOTE;
892 note_off = sizeof(Elf64_Ehdr) +
893 (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf64_Phdr);
894 phdr.p_offset = roundup(note_off, PAGE_SIZE);
895 phdr.p_vaddr = phdr.p_paddr = 0;
896 phdr.p_filesz = phdr.p_memsz = phdr_sz;
899 /* Add merged PT_NOTE program header*/
900 tmp = elfptr + sizeof(Elf64_Ehdr);
901 memcpy(tmp, &phdr, sizeof(phdr));
904 /* Remove unwanted PT_NOTE program headers. */
905 i = (nr_ptnote - 1) * sizeof(Elf64_Phdr);
907 memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf64_Ehdr)-sizeof(Elf64_Phdr)));
908 memset(elfptr + *elfsz, 0, i);
909 *elfsz = roundup(*elfsz, PAGE_SIZE);
911 /* Modify e_phnum to reflect merged headers. */
912 ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
914 /* Store the size of all notes. We need this to update the note
915 * header when the device dumps will be added.
917 elfnotes_orig_sz = phdr.p_memsz;
923 * update_note_header_size_elf32 - update p_memsz member of each PT_NOTE entry
925 * @ehdr_ptr: ELF header
927 * This function updates p_memsz member of each PT_NOTE entry in the
928 * program header table pointed to by @ehdr_ptr to real size of ELF
931 static int __init update_note_header_size_elf32(const Elf32_Ehdr *ehdr_ptr)
934 Elf32_Phdr *phdr_ptr;
935 Elf32_Nhdr *nhdr_ptr;
937 phdr_ptr = (Elf32_Phdr *)(ehdr_ptr + 1);
938 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
940 u64 offset, max_sz, sz, real_sz = 0;
941 if (phdr_ptr->p_type != PT_NOTE)
943 max_sz = phdr_ptr->p_memsz;
944 offset = phdr_ptr->p_offset;
945 notes_section = kmalloc(max_sz, GFP_KERNEL);
948 rc = elfcorehdr_read_notes(notes_section, max_sz, &offset);
950 kfree(notes_section);
953 nhdr_ptr = notes_section;
954 while (nhdr_ptr->n_namesz != 0) {
955 sz = sizeof(Elf32_Nhdr) +
956 (((u64)nhdr_ptr->n_namesz + 3) & ~3) +
957 (((u64)nhdr_ptr->n_descsz + 3) & ~3);
958 if ((real_sz + sz) > max_sz) {
959 pr_warn("Warning: Exceeded p_memsz, dropping PT_NOTE entry n_namesz=0x%x, n_descsz=0x%x\n",
960 nhdr_ptr->n_namesz, nhdr_ptr->n_descsz);
964 nhdr_ptr = (Elf32_Nhdr*)((char*)nhdr_ptr + sz);
966 kfree(notes_section);
967 phdr_ptr->p_memsz = real_sz;
969 pr_warn("Warning: Zero PT_NOTE entries found\n");
977 * get_note_number_and_size_elf32 - get the number of PT_NOTE program
978 * headers and sum of real size of their ELF note segment headers and
981 * @ehdr_ptr: ELF header
982 * @nr_ptnote: buffer for the number of PT_NOTE program headers
983 * @sz_ptnote: buffer for size of unique PT_NOTE program header
985 * This function is used to merge multiple PT_NOTE program headers
986 * into a unique single one. The resulting unique entry will have
987 * @sz_ptnote in its phdr->p_mem.
989 * It is assumed that program headers with PT_NOTE type pointed to by
990 * @ehdr_ptr has already been updated by update_note_header_size_elf32
991 * and each of PT_NOTE program headers has actual ELF note segment
992 * size in its p_memsz member.
994 static int __init get_note_number_and_size_elf32(const Elf32_Ehdr *ehdr_ptr,
995 int *nr_ptnote, u64 *sz_ptnote)
998 Elf32_Phdr *phdr_ptr;
1000 *nr_ptnote = *sz_ptnote = 0;
1002 phdr_ptr = (Elf32_Phdr *)(ehdr_ptr + 1);
1003 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
1004 if (phdr_ptr->p_type != PT_NOTE)
1007 *sz_ptnote += phdr_ptr->p_memsz;
1014 * copy_notes_elf32 - copy ELF note segments in a given buffer
1016 * @ehdr_ptr: ELF header
1017 * @notes_buf: buffer into which ELF note segments are copied
1019 * This function is used to copy ELF note segment in the 1st kernel
1020 * into the buffer @notes_buf in the 2nd kernel. It is assumed that
1021 * size of the buffer @notes_buf is equal to or larger than sum of the
1022 * real ELF note segment headers and data.
1024 * It is assumed that program headers with PT_NOTE type pointed to by
1025 * @ehdr_ptr has already been updated by update_note_header_size_elf32
1026 * and each of PT_NOTE program headers has actual ELF note segment
1027 * size in its p_memsz member.
1029 static int __init copy_notes_elf32(const Elf32_Ehdr *ehdr_ptr, char *notes_buf)
1032 Elf32_Phdr *phdr_ptr;
1034 phdr_ptr = (Elf32_Phdr*)(ehdr_ptr + 1);
1036 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
1038 if (phdr_ptr->p_type != PT_NOTE)
1040 offset = phdr_ptr->p_offset;
1041 rc = elfcorehdr_read_notes(notes_buf, phdr_ptr->p_memsz,
1045 notes_buf += phdr_ptr->p_memsz;
1051 /* Merges all the PT_NOTE headers into one. */
1052 static int __init merge_note_headers_elf32(char *elfptr, size_t *elfsz,
1053 char **notes_buf, size_t *notes_sz)
1055 int i, nr_ptnote=0, rc=0;
1057 Elf32_Ehdr *ehdr_ptr;
1059 u64 phdr_sz = 0, note_off;
1061 ehdr_ptr = (Elf32_Ehdr *)elfptr;
1063 rc = update_note_header_size_elf32(ehdr_ptr);
1067 rc = get_note_number_and_size_elf32(ehdr_ptr, &nr_ptnote, &phdr_sz);
1071 *notes_sz = roundup(phdr_sz, PAGE_SIZE);
1072 *notes_buf = vmcore_alloc_buf(*notes_sz);
1076 rc = copy_notes_elf32(ehdr_ptr, *notes_buf);
1080 /* Prepare merged PT_NOTE program header. */
1081 phdr.p_type = PT_NOTE;
1083 note_off = sizeof(Elf32_Ehdr) +
1084 (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf32_Phdr);
1085 phdr.p_offset = roundup(note_off, PAGE_SIZE);
1086 phdr.p_vaddr = phdr.p_paddr = 0;
1087 phdr.p_filesz = phdr.p_memsz = phdr_sz;
1090 /* Add merged PT_NOTE program header*/
1091 tmp = elfptr + sizeof(Elf32_Ehdr);
1092 memcpy(tmp, &phdr, sizeof(phdr));
1093 tmp += sizeof(phdr);
1095 /* Remove unwanted PT_NOTE program headers. */
1096 i = (nr_ptnote - 1) * sizeof(Elf32_Phdr);
1097 *elfsz = *elfsz - i;
1098 memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf32_Ehdr)-sizeof(Elf32_Phdr)));
1099 memset(elfptr + *elfsz, 0, i);
1100 *elfsz = roundup(*elfsz, PAGE_SIZE);
1102 /* Modify e_phnum to reflect merged headers. */
1103 ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
1105 /* Store the size of all notes. We need this to update the note
1106 * header when the device dumps will be added.
1108 elfnotes_orig_sz = phdr.p_memsz;
1113 /* Add memory chunks represented by program headers to vmcore list. Also update
1114 * the new offset fields of exported program headers. */
1115 static int __init process_ptload_program_headers_elf64(char *elfptr,
1118 struct list_head *vc_list)
1121 Elf64_Ehdr *ehdr_ptr;
1122 Elf64_Phdr *phdr_ptr;
1126 ehdr_ptr = (Elf64_Ehdr *)elfptr;
1127 phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr)); /* PT_NOTE hdr */
1129 /* Skip Elf header, program headers and Elf note segment. */
1130 vmcore_off = elfsz + elfnotes_sz;
1132 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
1133 u64 paddr, start, end, size;
1135 if (phdr_ptr->p_type != PT_LOAD)
1138 paddr = phdr_ptr->p_offset;
1139 start = rounddown(paddr, PAGE_SIZE);
1140 end = roundup(paddr + phdr_ptr->p_memsz, PAGE_SIZE);
1143 /* Add this contiguous chunk of memory to vmcore list.*/
1144 new = get_new_element();
1149 list_add_tail(&new->list, vc_list);
1151 /* Update the program header offset. */
1152 phdr_ptr->p_offset = vmcore_off + (paddr - start);
1153 vmcore_off = vmcore_off + size;
1158 static int __init process_ptload_program_headers_elf32(char *elfptr,
1161 struct list_head *vc_list)
1164 Elf32_Ehdr *ehdr_ptr;
1165 Elf32_Phdr *phdr_ptr;
1169 ehdr_ptr = (Elf32_Ehdr *)elfptr;
1170 phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr)); /* PT_NOTE hdr */
1172 /* Skip Elf header, program headers and Elf note segment. */
1173 vmcore_off = elfsz + elfnotes_sz;
1175 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
1176 u64 paddr, start, end, size;
1178 if (phdr_ptr->p_type != PT_LOAD)
1181 paddr = phdr_ptr->p_offset;
1182 start = rounddown(paddr, PAGE_SIZE);
1183 end = roundup(paddr + phdr_ptr->p_memsz, PAGE_SIZE);
1186 /* Add this contiguous chunk of memory to vmcore list.*/
1187 new = get_new_element();
1192 list_add_tail(&new->list, vc_list);
1194 /* Update the program header offset */
1195 phdr_ptr->p_offset = vmcore_off + (paddr - start);
1196 vmcore_off = vmcore_off + size;
1201 /* Sets offset fields of vmcore elements. */
1202 static void set_vmcore_list_offsets(size_t elfsz, size_t elfnotes_sz,
1203 struct list_head *vc_list)
1208 /* Skip Elf header, program headers and Elf note segment. */
1209 vmcore_off = elfsz + elfnotes_sz;
1211 list_for_each_entry(m, vc_list, list) {
1212 m->offset = vmcore_off;
1213 vmcore_off += m->size;
1217 static void free_elfcorebuf(void)
1219 free_pages((unsigned long)elfcorebuf, get_order(elfcorebuf_sz_orig));
1221 vfree(elfnotes_buf);
1222 elfnotes_buf = NULL;
1225 static int __init parse_crash_elf64_headers(void)
1231 addr = elfcorehdr_addr;
1233 /* Read Elf header */
1234 rc = elfcorehdr_read((char *)&ehdr, sizeof(Elf64_Ehdr), &addr);
1238 /* Do some basic Verification. */
1239 if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
1240 (ehdr.e_type != ET_CORE) ||
1241 !vmcore_elf64_check_arch(&ehdr) ||
1242 ehdr.e_ident[EI_CLASS] != ELFCLASS64 ||
1243 ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
1244 ehdr.e_version != EV_CURRENT ||
1245 ehdr.e_ehsize != sizeof(Elf64_Ehdr) ||
1246 ehdr.e_phentsize != sizeof(Elf64_Phdr) ||
1247 ehdr.e_phnum == 0) {
1248 pr_warn("Warning: Core image elf header is not sane\n");
1252 /* Read in all elf headers. */
1253 elfcorebuf_sz_orig = sizeof(Elf64_Ehdr) +
1254 ehdr.e_phnum * sizeof(Elf64_Phdr);
1255 elfcorebuf_sz = elfcorebuf_sz_orig;
1256 elfcorebuf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
1257 get_order(elfcorebuf_sz_orig));
1260 addr = elfcorehdr_addr;
1261 rc = elfcorehdr_read(elfcorebuf, elfcorebuf_sz_orig, &addr);
1265 /* Merge all PT_NOTE headers into one. */
1266 rc = merge_note_headers_elf64(elfcorebuf, &elfcorebuf_sz,
1267 &elfnotes_buf, &elfnotes_sz);
1270 rc = process_ptload_program_headers_elf64(elfcorebuf, elfcorebuf_sz,
1271 elfnotes_sz, &vmcore_list);
1274 set_vmcore_list_offsets(elfcorebuf_sz, elfnotes_sz, &vmcore_list);
1281 static int __init parse_crash_elf32_headers(void)
1287 addr = elfcorehdr_addr;
1289 /* Read Elf header */
1290 rc = elfcorehdr_read((char *)&ehdr, sizeof(Elf32_Ehdr), &addr);
1294 /* Do some basic Verification. */
1295 if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
1296 (ehdr.e_type != ET_CORE) ||
1297 !vmcore_elf32_check_arch(&ehdr) ||
1298 ehdr.e_ident[EI_CLASS] != ELFCLASS32||
1299 ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
1300 ehdr.e_version != EV_CURRENT ||
1301 ehdr.e_ehsize != sizeof(Elf32_Ehdr) ||
1302 ehdr.e_phentsize != sizeof(Elf32_Phdr) ||
1303 ehdr.e_phnum == 0) {
1304 pr_warn("Warning: Core image elf header is not sane\n");
1308 /* Read in all elf headers. */
1309 elfcorebuf_sz_orig = sizeof(Elf32_Ehdr) + ehdr.e_phnum * sizeof(Elf32_Phdr);
1310 elfcorebuf_sz = elfcorebuf_sz_orig;
1311 elfcorebuf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
1312 get_order(elfcorebuf_sz_orig));
1315 addr = elfcorehdr_addr;
1316 rc = elfcorehdr_read(elfcorebuf, elfcorebuf_sz_orig, &addr);
1320 /* Merge all PT_NOTE headers into one. */
1321 rc = merge_note_headers_elf32(elfcorebuf, &elfcorebuf_sz,
1322 &elfnotes_buf, &elfnotes_sz);
1325 rc = process_ptload_program_headers_elf32(elfcorebuf, elfcorebuf_sz,
1326 elfnotes_sz, &vmcore_list);
1329 set_vmcore_list_offsets(elfcorebuf_sz, elfnotes_sz, &vmcore_list);
1336 static int __init parse_crash_elf_headers(void)
1338 unsigned char e_ident[EI_NIDENT];
1342 addr = elfcorehdr_addr;
1343 rc = elfcorehdr_read(e_ident, EI_NIDENT, &addr);
1346 if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) {
1347 pr_warn("Warning: Core image elf header not found\n");
1351 if (e_ident[EI_CLASS] == ELFCLASS64) {
1352 rc = parse_crash_elf64_headers();
1355 } else if (e_ident[EI_CLASS] == ELFCLASS32) {
1356 rc = parse_crash_elf32_headers();
1360 pr_warn("Warning: Core image elf header is not sane\n");
1364 /* Determine vmcore size. */
1365 vmcore_size = get_vmcore_size(elfcorebuf_sz, elfnotes_sz,
1371 #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
1373 * vmcoredd_write_header - Write vmcore device dump header at the
1374 * beginning of the dump's buffer.
1375 * @buf: Output buffer where the note is written
1377 * @size: Size of the dump
1379 * Fills beginning of the dump's buffer with vmcore device dump header.
1381 static void vmcoredd_write_header(void *buf, struct vmcoredd_data *data,
1384 struct vmcoredd_header *vdd_hdr = (struct vmcoredd_header *)buf;
1386 vdd_hdr->n_namesz = sizeof(vdd_hdr->name);
1387 vdd_hdr->n_descsz = size + sizeof(vdd_hdr->dump_name);
1388 vdd_hdr->n_type = NT_VMCOREDD;
1390 strncpy((char *)vdd_hdr->name, VMCOREDD_NOTE_NAME,
1391 sizeof(vdd_hdr->name));
1392 memcpy(vdd_hdr->dump_name, data->dump_name, sizeof(vdd_hdr->dump_name));
1396 * vmcoredd_update_program_headers - Update all Elf program headers
1397 * @elfptr: Pointer to elf header
1398 * @elfnotesz: Size of elf notes aligned to page size
1399 * @vmcoreddsz: Size of device dumps to be added to elf note header
1401 * Determine type of Elf header (Elf64 or Elf32) and update the elf note size.
1402 * Also update the offsets of all the program headers after the elf note header.
1404 static void vmcoredd_update_program_headers(char *elfptr, size_t elfnotesz,
1407 unsigned char *e_ident = (unsigned char *)elfptr;
1408 u64 start, end, size;
1412 vmcore_off = elfcorebuf_sz + elfnotesz;
1414 if (e_ident[EI_CLASS] == ELFCLASS64) {
1415 Elf64_Ehdr *ehdr = (Elf64_Ehdr *)elfptr;
1416 Elf64_Phdr *phdr = (Elf64_Phdr *)(elfptr + sizeof(Elf64_Ehdr));
1418 /* Update all program headers */
1419 for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
1420 if (phdr->p_type == PT_NOTE) {
1421 /* Update note size */
1422 phdr->p_memsz = elfnotes_orig_sz + vmcoreddsz;
1423 phdr->p_filesz = phdr->p_memsz;
1427 start = rounddown(phdr->p_offset, PAGE_SIZE);
1428 end = roundup(phdr->p_offset + phdr->p_memsz,
1431 phdr->p_offset = vmcore_off + (phdr->p_offset - start);
1435 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)elfptr;
1436 Elf32_Phdr *phdr = (Elf32_Phdr *)(elfptr + sizeof(Elf32_Ehdr));
1438 /* Update all program headers */
1439 for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
1440 if (phdr->p_type == PT_NOTE) {
1441 /* Update note size */
1442 phdr->p_memsz = elfnotes_orig_sz + vmcoreddsz;
1443 phdr->p_filesz = phdr->p_memsz;
1447 start = rounddown(phdr->p_offset, PAGE_SIZE);
1448 end = roundup(phdr->p_offset + phdr->p_memsz,
1451 phdr->p_offset = vmcore_off + (phdr->p_offset - start);
1458 * vmcoredd_update_size - Update the total size of the device dumps and update
1460 * @dump_size: Size of the current device dump to be added to total size
1462 * Update the total size of all the device dumps and update the Elf program
1463 * headers. Calculate the new offsets for the vmcore list and update the
1464 * total vmcore size.
1466 static void vmcoredd_update_size(size_t dump_size)
1468 vmcoredd_orig_sz += dump_size;
1469 elfnotes_sz = roundup(elfnotes_orig_sz, PAGE_SIZE) + vmcoredd_orig_sz;
1470 vmcoredd_update_program_headers(elfcorebuf, elfnotes_sz,
1473 /* Update vmcore list offsets */
1474 set_vmcore_list_offsets(elfcorebuf_sz, elfnotes_sz, &vmcore_list);
1476 vmcore_size = get_vmcore_size(elfcorebuf_sz, elfnotes_sz,
1478 proc_vmcore->size = vmcore_size;
1482 * vmcore_add_device_dump - Add a buffer containing device dump to vmcore
1485 * Allocate a buffer and invoke the calling driver's dump collect routine.
1486 * Write Elf note at the beginning of the buffer to indicate vmcore device
1487 * dump and add the dump to global list.
1489 int vmcore_add_device_dump(struct vmcoredd_data *data)
1491 struct vmcoredd_node *dump;
1496 if (vmcoredd_disabled) {
1497 pr_err_once("Device dump is disabled\n");
1501 if (!data || !strlen(data->dump_name) ||
1502 !data->vmcoredd_callback || !data->size)
1505 dump = vzalloc(sizeof(*dump));
1511 /* Keep size of the buffer page aligned so that it can be mmaped */
1512 data_size = roundup(sizeof(struct vmcoredd_header) + data->size,
1515 /* Allocate buffer for driver's to write their dumps */
1516 buf = vmcore_alloc_buf(data_size);
1522 vmcoredd_write_header(buf, data, data_size -
1523 sizeof(struct vmcoredd_header));
1525 /* Invoke the driver's dump collection routing */
1526 ret = data->vmcoredd_callback(data, buf +
1527 sizeof(struct vmcoredd_header));
1532 dump->size = data_size;
1534 /* Add the dump to driver sysfs list */
1535 mutex_lock(&vmcoredd_mutex);
1536 list_add_tail(&dump->list, &vmcoredd_list);
1537 mutex_unlock(&vmcoredd_mutex);
1539 vmcoredd_update_size(data_size);
1548 EXPORT_SYMBOL(vmcore_add_device_dump);
1549 #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
1551 /* Free all dumps in vmcore device dump list */
1552 static void vmcore_free_device_dumps(void)
1554 #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
1555 mutex_lock(&vmcoredd_mutex);
1556 while (!list_empty(&vmcoredd_list)) {
1557 struct vmcoredd_node *dump;
1559 dump = list_first_entry(&vmcoredd_list, struct vmcoredd_node,
1561 list_del(&dump->list);
1565 mutex_unlock(&vmcoredd_mutex);
1566 #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
1569 /* Init function for vmcore module. */
1570 static int __init vmcore_init(void)
1574 /* Allow architectures to allocate ELF header in 2nd kernel */
1575 rc = elfcorehdr_alloc(&elfcorehdr_addr, &elfcorehdr_size);
1579 * If elfcorehdr= has been passed in cmdline or created in 2nd kernel,
1580 * then capture the dump.
1582 if (!(is_vmcore_usable()))
1584 rc = parse_crash_elf_headers();
1586 pr_warn("Kdump: vmcore not initialized\n");
1589 elfcorehdr_free(elfcorehdr_addr);
1590 elfcorehdr_addr = ELFCORE_ADDR_ERR;
1592 proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &vmcore_proc_ops);
1594 proc_vmcore->size = vmcore_size;
1597 fs_initcall(vmcore_init);
1599 /* Cleanup function for vmcore module. */
1600 void vmcore_cleanup(void)
1603 proc_remove(proc_vmcore);
1607 /* clear the vmcore list. */
1608 while (!list_empty(&vmcore_list)) {
1611 m = list_first_entry(&vmcore_list, struct vmcore, list);
1617 /* clear vmcore device dump list */
1618 vmcore_free_device_dumps();