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
6 * Created by: Hariprasad Nellitheertha (hari@in.ibm.com)
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/mem_encrypt.h>
30 #include <asm/pgtable.h>
34 /* List representing chunks of contiguous memory areas and their offsets in
37 static LIST_HEAD(vmcore_list);
39 /* Stores the pointer to the buffer containing kernel elf core headers. */
40 static char *elfcorebuf;
41 static size_t elfcorebuf_sz;
42 static size_t elfcorebuf_sz_orig;
44 static char *elfnotes_buf;
45 static size_t elfnotes_sz;
46 /* Size of all notes minus the device dump notes */
47 static size_t elfnotes_orig_sz;
49 /* Total size of vmcore file. */
50 static u64 vmcore_size;
52 static struct proc_dir_entry *proc_vmcore;
54 #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
55 /* Device Dump list and mutex to synchronize access to list */
56 static LIST_HEAD(vmcoredd_list);
57 static DEFINE_MUTEX(vmcoredd_mutex);
59 static bool vmcoredd_disabled;
60 core_param(novmcoredd, vmcoredd_disabled, bool, 0);
61 #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
63 /* Device Dump Size */
64 static size_t vmcoredd_orig_sz;
67 * Returns > 0 for RAM pages, 0 for non-RAM pages, < 0 on error
68 * The called function has to take care of module refcounting.
70 static int (*oldmem_pfn_is_ram)(unsigned long pfn);
72 int register_oldmem_pfn_is_ram(int (*fn)(unsigned long pfn))
74 if (oldmem_pfn_is_ram)
76 oldmem_pfn_is_ram = fn;
79 EXPORT_SYMBOL_GPL(register_oldmem_pfn_is_ram);
81 void unregister_oldmem_pfn_is_ram(void)
83 oldmem_pfn_is_ram = NULL;
86 EXPORT_SYMBOL_GPL(unregister_oldmem_pfn_is_ram);
88 static int pfn_is_ram(unsigned long pfn)
90 int (*fn)(unsigned long pfn);
91 /* pfn is ram unless fn() checks pagetype */
95 * Ask hypervisor if the pfn is really ram.
96 * A ballooned page contains no data and reading from such a page
97 * will cause high load in the hypervisor.
99 fn = oldmem_pfn_is_ram;
106 /* Reads a page from the oldmem device from given offset. */
107 ssize_t read_from_oldmem(char *buf, size_t count,
108 u64 *ppos, int userbuf,
111 unsigned long pfn, offset;
113 ssize_t read = 0, tmp;
118 offset = (unsigned long)(*ppos % PAGE_SIZE);
119 pfn = (unsigned long)(*ppos / PAGE_SIZE);
122 if (count > (PAGE_SIZE - offset))
123 nr_bytes = PAGE_SIZE - offset;
127 /* If pfn is not ram, return zeros for sparse dump files */
128 if (pfn_is_ram(pfn) == 0) {
131 memset(buf, 0, nr_bytes);
132 else if (clear_user(buf, nr_bytes))
136 tmp = copy_oldmem_page_encrypted(pfn, buf,
141 tmp = copy_oldmem_page(pfn, buf, nr_bytes,
159 * Architectures may override this function to allocate ELF header in 2nd kernel
161 int __weak elfcorehdr_alloc(unsigned long long *addr, unsigned long long *size)
167 * Architectures may override this function to free header
169 void __weak elfcorehdr_free(unsigned long long addr)
173 * Architectures may override this function to read from ELF header
175 ssize_t __weak elfcorehdr_read(char *buf, size_t count, u64 *ppos)
177 return read_from_oldmem(buf, count, ppos, 0, false);
181 * Architectures may override this function to read from notes sections
183 ssize_t __weak elfcorehdr_read_notes(char *buf, size_t count, u64 *ppos)
185 return read_from_oldmem(buf, count, ppos, 0, mem_encrypt_active());
189 * Architectures may override this function to map oldmem
191 int __weak remap_oldmem_pfn_range(struct vm_area_struct *vma,
192 unsigned long from, unsigned long pfn,
193 unsigned long size, pgprot_t prot)
195 prot = pgprot_encrypted(prot);
196 return remap_pfn_range(vma, from, pfn, size, prot);
200 * Architectures which support memory encryption override this.
203 copy_oldmem_page_encrypted(unsigned long pfn, char *buf, size_t csize,
204 unsigned long offset, int userbuf)
206 return copy_oldmem_page(pfn, buf, csize, offset, userbuf);
210 * Copy to either kernel or user space
212 static int copy_to(void *target, void *src, size_t size, int userbuf)
215 if (copy_to_user((char __user *) target, src, size))
218 memcpy(target, src, size);
223 #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
224 static int vmcoredd_copy_dumps(void *dst, u64 start, size_t size, int userbuf)
226 struct vmcoredd_node *dump;
232 mutex_lock(&vmcoredd_mutex);
233 list_for_each_entry(dump, &vmcoredd_list, list) {
234 if (start < offset + dump->size) {
235 tsz = min(offset + (u64)dump->size - start, (u64)size);
236 buf = dump->buf + start - offset;
237 if (copy_to(dst, buf, tsz, userbuf)) {
246 /* Leave now if buffer filled already */
250 offset += dump->size;
254 mutex_unlock(&vmcoredd_mutex);
259 static int vmcoredd_mmap_dumps(struct vm_area_struct *vma, unsigned long dst,
260 u64 start, size_t size)
262 struct vmcoredd_node *dump;
268 mutex_lock(&vmcoredd_mutex);
269 list_for_each_entry(dump, &vmcoredd_list, list) {
270 if (start < offset + dump->size) {
271 tsz = min(offset + (u64)dump->size - start, (u64)size);
272 buf = dump->buf + start - offset;
273 if (remap_vmalloc_range_partial(vma, dst, buf, 0,
283 /* Leave now if buffer filled already */
287 offset += dump->size;
291 mutex_unlock(&vmcoredd_mutex);
294 #endif /* CONFIG_MMU */
295 #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
297 /* Read from the ELF header and then the crash dump. On error, negative value is
298 * returned otherwise number of bytes read are returned.
300 static ssize_t __read_vmcore(char *buffer, size_t buflen, loff_t *fpos,
303 ssize_t acc = 0, tmp;
306 struct vmcore *m = NULL;
308 if (buflen == 0 || *fpos >= vmcore_size)
311 /* trim buflen to not go beyond EOF */
312 if (buflen > vmcore_size - *fpos)
313 buflen = vmcore_size - *fpos;
315 /* Read ELF core header */
316 if (*fpos < elfcorebuf_sz) {
317 tsz = min(elfcorebuf_sz - (size_t)*fpos, buflen);
318 if (copy_to(buffer, elfcorebuf + *fpos, tsz, userbuf))
325 /* leave now if filled buffer already */
330 /* Read Elf note segment */
331 if (*fpos < elfcorebuf_sz + elfnotes_sz) {
334 /* We add device dumps before other elf notes because the
335 * other elf notes may not fill the elf notes buffer
336 * completely and we will end up with zero-filled data
337 * between the elf notes and the device dumps. Tools will
338 * then try to decode this zero-filled data as valid notes
339 * and we don't want that. Hence, adding device dumps before
340 * the other elf notes ensure that zero-filled data can be
343 #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
344 /* Read device dumps */
345 if (*fpos < elfcorebuf_sz + vmcoredd_orig_sz) {
346 tsz = min(elfcorebuf_sz + vmcoredd_orig_sz -
347 (size_t)*fpos, buflen);
348 start = *fpos - elfcorebuf_sz;
349 if (vmcoredd_copy_dumps(buffer, start, tsz, userbuf))
357 /* leave now if filled buffer already */
361 #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
363 /* Read remaining elf notes */
364 tsz = min(elfcorebuf_sz + elfnotes_sz - (size_t)*fpos, buflen);
365 kaddr = elfnotes_buf + *fpos - elfcorebuf_sz - vmcoredd_orig_sz;
366 if (copy_to(buffer, kaddr, tsz, userbuf))
374 /* leave now if filled buffer already */
379 list_for_each_entry(m, &vmcore_list, list) {
380 if (*fpos < m->offset + m->size) {
381 tsz = (size_t)min_t(unsigned long long,
382 m->offset + m->size - *fpos,
384 start = m->paddr + *fpos - m->offset;
385 tmp = read_from_oldmem(buffer, tsz, &start,
386 userbuf, mem_encrypt_active());
394 /* leave now if filled buffer already */
403 static ssize_t read_vmcore(struct file *file, char __user *buffer,
404 size_t buflen, loff_t *fpos)
406 return __read_vmcore((__force char *) buffer, buflen, fpos, 1);
410 * The vmcore fault handler uses the page cache and fills data using the
411 * standard __vmcore_read() function.
413 * On s390 the fault handler is used for memory regions that can't be mapped
414 * directly with remap_pfn_range().
416 static vm_fault_t mmap_vmcore_fault(struct vm_fault *vmf)
419 struct address_space *mapping = vmf->vma->vm_file->f_mapping;
420 pgoff_t index = vmf->pgoff;
426 page = find_or_create_page(mapping, index, GFP_KERNEL);
429 if (!PageUptodate(page)) {
430 offset = (loff_t) index << PAGE_SHIFT;
431 buf = __va((page_to_pfn(page) << PAGE_SHIFT));
432 rc = __read_vmcore(buf, PAGE_SIZE, &offset, 0);
436 return vmf_error(rc);
438 SetPageUptodate(page);
444 return VM_FAULT_SIGBUS;
448 static const struct vm_operations_struct vmcore_mmap_ops = {
449 .fault = mmap_vmcore_fault,
453 * vmcore_alloc_buf - allocate buffer in vmalloc memory
454 * @sizez: size of buffer
456 * If CONFIG_MMU is defined, use vmalloc_user() to allow users to mmap
457 * the buffer to user-space by means of remap_vmalloc_range().
459 * If CONFIG_MMU is not defined, use vzalloc() since mmap_vmcore() is
460 * disabled and there's no need to allow users to mmap the buffer.
462 static inline char *vmcore_alloc_buf(size_t size)
465 return vmalloc_user(size);
467 return vzalloc(size);
472 * Disable mmap_vmcore() if CONFIG_MMU is not defined. MMU is
473 * essential for mmap_vmcore() in order to map physically
474 * non-contiguous objects (ELF header, ELF note segment and memory
475 * regions in the 1st kernel pointed to by PT_LOAD entries) into
476 * virtually contiguous user-space in ELF layout.
480 * remap_oldmem_pfn_checked - do remap_oldmem_pfn_range replacing all pages
481 * reported as not being ram with the zero page.
483 * @vma: vm_area_struct describing requested mapping
484 * @from: start remapping from
485 * @pfn: page frame number to start remapping to
486 * @size: remapping size
487 * @prot: protection bits
489 * Returns zero on success, -EAGAIN on failure.
491 static int remap_oldmem_pfn_checked(struct vm_area_struct *vma,
492 unsigned long from, unsigned long pfn,
493 unsigned long size, pgprot_t prot)
495 unsigned long map_size;
496 unsigned long pos_start, pos_end, pos;
497 unsigned long zeropage_pfn = my_zero_pfn(0);
501 pos_end = pfn + (size >> PAGE_SHIFT);
503 for (pos = pos_start; pos < pos_end; ++pos) {
504 if (!pfn_is_ram(pos)) {
506 * We hit a page which is not ram. Remap the continuous
507 * region between pos_start and pos-1 and replace
508 * the non-ram page at pos with the zero page.
510 if (pos > pos_start) {
511 /* Remap continuous region */
512 map_size = (pos - pos_start) << PAGE_SHIFT;
513 if (remap_oldmem_pfn_range(vma, from + len,
519 /* Remap the zero page */
520 if (remap_oldmem_pfn_range(vma, from + len,
528 if (pos > pos_start) {
530 map_size = (pos - pos_start) << PAGE_SHIFT;
531 if (remap_oldmem_pfn_range(vma, from + len, pos_start,
537 do_munmap(vma->vm_mm, from, len, NULL);
541 static int vmcore_remap_oldmem_pfn(struct vm_area_struct *vma,
542 unsigned long from, unsigned long pfn,
543 unsigned long size, pgprot_t prot)
546 * Check if oldmem_pfn_is_ram was registered to avoid
547 * looping over all pages without a reason.
549 if (oldmem_pfn_is_ram)
550 return remap_oldmem_pfn_checked(vma, from, pfn, size, prot);
552 return remap_oldmem_pfn_range(vma, from, pfn, size, prot);
555 static int mmap_vmcore(struct file *file, struct vm_area_struct *vma)
557 size_t size = vma->vm_end - vma->vm_start;
558 u64 start, end, len, tsz;
561 start = (u64)vma->vm_pgoff << PAGE_SHIFT;
564 if (size > vmcore_size || end > vmcore_size)
567 if (vma->vm_flags & (VM_WRITE | VM_EXEC))
570 vma->vm_flags &= ~(VM_MAYWRITE | VM_MAYEXEC);
571 vma->vm_flags |= VM_MIXEDMAP;
572 vma->vm_ops = &vmcore_mmap_ops;
576 if (start < elfcorebuf_sz) {
579 tsz = min(elfcorebuf_sz - (size_t)start, size);
580 pfn = __pa(elfcorebuf + start) >> PAGE_SHIFT;
581 if (remap_pfn_range(vma, vma->vm_start, pfn, tsz,
592 if (start < elfcorebuf_sz + elfnotes_sz) {
595 /* We add device dumps before other elf notes because the
596 * other elf notes may not fill the elf notes buffer
597 * completely and we will end up with zero-filled data
598 * between the elf notes and the device dumps. Tools will
599 * then try to decode this zero-filled data as valid notes
600 * and we don't want that. Hence, adding device dumps before
601 * the other elf notes ensure that zero-filled data can be
602 * avoided. This also ensures that the device dumps and
603 * other elf notes can be properly mmaped at page aligned
606 #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
607 /* Read device dumps */
608 if (start < elfcorebuf_sz + vmcoredd_orig_sz) {
611 tsz = min(elfcorebuf_sz + vmcoredd_orig_sz -
612 (size_t)start, size);
613 start_off = start - elfcorebuf_sz;
614 if (vmcoredd_mmap_dumps(vma, vma->vm_start + len,
622 /* leave now if filled buffer already */
626 #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
628 /* Read remaining elf notes */
629 tsz = min(elfcorebuf_sz + elfnotes_sz - (size_t)start, size);
630 kaddr = elfnotes_buf + start - elfcorebuf_sz - vmcoredd_orig_sz;
631 if (remap_vmalloc_range_partial(vma, vma->vm_start + len,
643 list_for_each_entry(m, &vmcore_list, list) {
644 if (start < m->offset + m->size) {
647 tsz = (size_t)min_t(unsigned long long,
648 m->offset + m->size - start, size);
649 paddr = m->paddr + start - m->offset;
650 if (vmcore_remap_oldmem_pfn(vma, vma->vm_start + len,
651 paddr >> PAGE_SHIFT, tsz,
665 do_munmap(vma->vm_mm, vma->vm_start, len, NULL);
669 static int mmap_vmcore(struct file *file, struct vm_area_struct *vma)
675 static const struct file_operations proc_vmcore_operations = {
677 .llseek = default_llseek,
681 static struct vmcore* __init get_new_element(void)
683 return kzalloc(sizeof(struct vmcore), GFP_KERNEL);
686 static u64 get_vmcore_size(size_t elfsz, size_t elfnotesegsz,
687 struct list_head *vc_list)
692 size = elfsz + elfnotesegsz;
693 list_for_each_entry(m, vc_list, list) {
700 * update_note_header_size_elf64 - update p_memsz member of each PT_NOTE entry
702 * @ehdr_ptr: ELF header
704 * This function updates p_memsz member of each PT_NOTE entry in the
705 * program header table pointed to by @ehdr_ptr to real size of ELF
708 static int __init update_note_header_size_elf64(const Elf64_Ehdr *ehdr_ptr)
711 Elf64_Phdr *phdr_ptr;
712 Elf64_Nhdr *nhdr_ptr;
714 phdr_ptr = (Elf64_Phdr *)(ehdr_ptr + 1);
715 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
717 u64 offset, max_sz, sz, real_sz = 0;
718 if (phdr_ptr->p_type != PT_NOTE)
720 max_sz = phdr_ptr->p_memsz;
721 offset = phdr_ptr->p_offset;
722 notes_section = kmalloc(max_sz, GFP_KERNEL);
725 rc = elfcorehdr_read_notes(notes_section, max_sz, &offset);
727 kfree(notes_section);
730 nhdr_ptr = notes_section;
731 while (nhdr_ptr->n_namesz != 0) {
732 sz = sizeof(Elf64_Nhdr) +
733 (((u64)nhdr_ptr->n_namesz + 3) & ~3) +
734 (((u64)nhdr_ptr->n_descsz + 3) & ~3);
735 if ((real_sz + sz) > max_sz) {
736 pr_warn("Warning: Exceeded p_memsz, dropping PT_NOTE entry n_namesz=0x%x, n_descsz=0x%x\n",
737 nhdr_ptr->n_namesz, nhdr_ptr->n_descsz);
741 nhdr_ptr = (Elf64_Nhdr*)((char*)nhdr_ptr + sz);
743 kfree(notes_section);
744 phdr_ptr->p_memsz = real_sz;
746 pr_warn("Warning: Zero PT_NOTE entries found\n");
754 * get_note_number_and_size_elf64 - get the number of PT_NOTE program
755 * headers and sum of real size of their ELF note segment headers and
758 * @ehdr_ptr: ELF header
759 * @nr_ptnote: buffer for the number of PT_NOTE program headers
760 * @sz_ptnote: buffer for size of unique PT_NOTE program header
762 * This function is used to merge multiple PT_NOTE program headers
763 * into a unique single one. The resulting unique entry will have
764 * @sz_ptnote in its phdr->p_mem.
766 * It is assumed that program headers with PT_NOTE type pointed to by
767 * @ehdr_ptr has already been updated by update_note_header_size_elf64
768 * and each of PT_NOTE program headers has actual ELF note segment
769 * size in its p_memsz member.
771 static int __init get_note_number_and_size_elf64(const Elf64_Ehdr *ehdr_ptr,
772 int *nr_ptnote, u64 *sz_ptnote)
775 Elf64_Phdr *phdr_ptr;
777 *nr_ptnote = *sz_ptnote = 0;
779 phdr_ptr = (Elf64_Phdr *)(ehdr_ptr + 1);
780 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
781 if (phdr_ptr->p_type != PT_NOTE)
784 *sz_ptnote += phdr_ptr->p_memsz;
791 * copy_notes_elf64 - copy ELF note segments in a given buffer
793 * @ehdr_ptr: ELF header
794 * @notes_buf: buffer into which ELF note segments are copied
796 * This function is used to copy ELF note segment in the 1st kernel
797 * into the buffer @notes_buf in the 2nd kernel. It is assumed that
798 * size of the buffer @notes_buf is equal to or larger than sum of the
799 * real ELF note segment headers and data.
801 * It is assumed that program headers with PT_NOTE type pointed to by
802 * @ehdr_ptr has already been updated by update_note_header_size_elf64
803 * and each of PT_NOTE program headers has actual ELF note segment
804 * size in its p_memsz member.
806 static int __init copy_notes_elf64(const Elf64_Ehdr *ehdr_ptr, char *notes_buf)
809 Elf64_Phdr *phdr_ptr;
811 phdr_ptr = (Elf64_Phdr*)(ehdr_ptr + 1);
813 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
815 if (phdr_ptr->p_type != PT_NOTE)
817 offset = phdr_ptr->p_offset;
818 rc = elfcorehdr_read_notes(notes_buf, phdr_ptr->p_memsz,
822 notes_buf += phdr_ptr->p_memsz;
828 /* Merges all the PT_NOTE headers into one. */
829 static int __init merge_note_headers_elf64(char *elfptr, size_t *elfsz,
830 char **notes_buf, size_t *notes_sz)
832 int i, nr_ptnote=0, rc=0;
834 Elf64_Ehdr *ehdr_ptr;
836 u64 phdr_sz = 0, note_off;
838 ehdr_ptr = (Elf64_Ehdr *)elfptr;
840 rc = update_note_header_size_elf64(ehdr_ptr);
844 rc = get_note_number_and_size_elf64(ehdr_ptr, &nr_ptnote, &phdr_sz);
848 *notes_sz = roundup(phdr_sz, PAGE_SIZE);
849 *notes_buf = vmcore_alloc_buf(*notes_sz);
853 rc = copy_notes_elf64(ehdr_ptr, *notes_buf);
857 /* Prepare merged PT_NOTE program header. */
858 phdr.p_type = PT_NOTE;
860 note_off = sizeof(Elf64_Ehdr) +
861 (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf64_Phdr);
862 phdr.p_offset = roundup(note_off, PAGE_SIZE);
863 phdr.p_vaddr = phdr.p_paddr = 0;
864 phdr.p_filesz = phdr.p_memsz = phdr_sz;
867 /* Add merged PT_NOTE program header*/
868 tmp = elfptr + sizeof(Elf64_Ehdr);
869 memcpy(tmp, &phdr, sizeof(phdr));
872 /* Remove unwanted PT_NOTE program headers. */
873 i = (nr_ptnote - 1) * sizeof(Elf64_Phdr);
875 memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf64_Ehdr)-sizeof(Elf64_Phdr)));
876 memset(elfptr + *elfsz, 0, i);
877 *elfsz = roundup(*elfsz, PAGE_SIZE);
879 /* Modify e_phnum to reflect merged headers. */
880 ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
882 /* Store the size of all notes. We need this to update the note
883 * header when the device dumps will be added.
885 elfnotes_orig_sz = phdr.p_memsz;
891 * update_note_header_size_elf32 - update p_memsz member of each PT_NOTE entry
893 * @ehdr_ptr: ELF header
895 * This function updates p_memsz member of each PT_NOTE entry in the
896 * program header table pointed to by @ehdr_ptr to real size of ELF
899 static int __init update_note_header_size_elf32(const Elf32_Ehdr *ehdr_ptr)
902 Elf32_Phdr *phdr_ptr;
903 Elf32_Nhdr *nhdr_ptr;
905 phdr_ptr = (Elf32_Phdr *)(ehdr_ptr + 1);
906 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
908 u64 offset, max_sz, sz, real_sz = 0;
909 if (phdr_ptr->p_type != PT_NOTE)
911 max_sz = phdr_ptr->p_memsz;
912 offset = phdr_ptr->p_offset;
913 notes_section = kmalloc(max_sz, GFP_KERNEL);
916 rc = elfcorehdr_read_notes(notes_section, max_sz, &offset);
918 kfree(notes_section);
921 nhdr_ptr = notes_section;
922 while (nhdr_ptr->n_namesz != 0) {
923 sz = sizeof(Elf32_Nhdr) +
924 (((u64)nhdr_ptr->n_namesz + 3) & ~3) +
925 (((u64)nhdr_ptr->n_descsz + 3) & ~3);
926 if ((real_sz + sz) > max_sz) {
927 pr_warn("Warning: Exceeded p_memsz, dropping PT_NOTE entry n_namesz=0x%x, n_descsz=0x%x\n",
928 nhdr_ptr->n_namesz, nhdr_ptr->n_descsz);
932 nhdr_ptr = (Elf32_Nhdr*)((char*)nhdr_ptr + sz);
934 kfree(notes_section);
935 phdr_ptr->p_memsz = real_sz;
937 pr_warn("Warning: Zero PT_NOTE entries found\n");
945 * get_note_number_and_size_elf32 - get the number of PT_NOTE program
946 * headers and sum of real size of their ELF note segment headers and
949 * @ehdr_ptr: ELF header
950 * @nr_ptnote: buffer for the number of PT_NOTE program headers
951 * @sz_ptnote: buffer for size of unique PT_NOTE program header
953 * This function is used to merge multiple PT_NOTE program headers
954 * into a unique single one. The resulting unique entry will have
955 * @sz_ptnote in its phdr->p_mem.
957 * It is assumed that program headers with PT_NOTE type pointed to by
958 * @ehdr_ptr has already been updated by update_note_header_size_elf32
959 * and each of PT_NOTE program headers has actual ELF note segment
960 * size in its p_memsz member.
962 static int __init get_note_number_and_size_elf32(const Elf32_Ehdr *ehdr_ptr,
963 int *nr_ptnote, u64 *sz_ptnote)
966 Elf32_Phdr *phdr_ptr;
968 *nr_ptnote = *sz_ptnote = 0;
970 phdr_ptr = (Elf32_Phdr *)(ehdr_ptr + 1);
971 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
972 if (phdr_ptr->p_type != PT_NOTE)
975 *sz_ptnote += phdr_ptr->p_memsz;
982 * copy_notes_elf32 - copy ELF note segments in a given buffer
984 * @ehdr_ptr: ELF header
985 * @notes_buf: buffer into which ELF note segments are copied
987 * This function is used to copy ELF note segment in the 1st kernel
988 * into the buffer @notes_buf in the 2nd kernel. It is assumed that
989 * size of the buffer @notes_buf is equal to or larger than sum of the
990 * real ELF note segment headers and data.
992 * It is assumed that program headers with PT_NOTE type pointed to by
993 * @ehdr_ptr has already been updated by update_note_header_size_elf32
994 * and each of PT_NOTE program headers has actual ELF note segment
995 * size in its p_memsz member.
997 static int __init copy_notes_elf32(const Elf32_Ehdr *ehdr_ptr, char *notes_buf)
1000 Elf32_Phdr *phdr_ptr;
1002 phdr_ptr = (Elf32_Phdr*)(ehdr_ptr + 1);
1004 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
1006 if (phdr_ptr->p_type != PT_NOTE)
1008 offset = phdr_ptr->p_offset;
1009 rc = elfcorehdr_read_notes(notes_buf, phdr_ptr->p_memsz,
1013 notes_buf += phdr_ptr->p_memsz;
1019 /* Merges all the PT_NOTE headers into one. */
1020 static int __init merge_note_headers_elf32(char *elfptr, size_t *elfsz,
1021 char **notes_buf, size_t *notes_sz)
1023 int i, nr_ptnote=0, rc=0;
1025 Elf32_Ehdr *ehdr_ptr;
1027 u64 phdr_sz = 0, note_off;
1029 ehdr_ptr = (Elf32_Ehdr *)elfptr;
1031 rc = update_note_header_size_elf32(ehdr_ptr);
1035 rc = get_note_number_and_size_elf32(ehdr_ptr, &nr_ptnote, &phdr_sz);
1039 *notes_sz = roundup(phdr_sz, PAGE_SIZE);
1040 *notes_buf = vmcore_alloc_buf(*notes_sz);
1044 rc = copy_notes_elf32(ehdr_ptr, *notes_buf);
1048 /* Prepare merged PT_NOTE program header. */
1049 phdr.p_type = PT_NOTE;
1051 note_off = sizeof(Elf32_Ehdr) +
1052 (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf32_Phdr);
1053 phdr.p_offset = roundup(note_off, PAGE_SIZE);
1054 phdr.p_vaddr = phdr.p_paddr = 0;
1055 phdr.p_filesz = phdr.p_memsz = phdr_sz;
1058 /* Add merged PT_NOTE program header*/
1059 tmp = elfptr + sizeof(Elf32_Ehdr);
1060 memcpy(tmp, &phdr, sizeof(phdr));
1061 tmp += sizeof(phdr);
1063 /* Remove unwanted PT_NOTE program headers. */
1064 i = (nr_ptnote - 1) * sizeof(Elf32_Phdr);
1065 *elfsz = *elfsz - i;
1066 memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf32_Ehdr)-sizeof(Elf32_Phdr)));
1067 memset(elfptr + *elfsz, 0, i);
1068 *elfsz = roundup(*elfsz, PAGE_SIZE);
1070 /* Modify e_phnum to reflect merged headers. */
1071 ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
1073 /* Store the size of all notes. We need this to update the note
1074 * header when the device dumps will be added.
1076 elfnotes_orig_sz = phdr.p_memsz;
1081 /* Add memory chunks represented by program headers to vmcore list. Also update
1082 * the new offset fields of exported program headers. */
1083 static int __init process_ptload_program_headers_elf64(char *elfptr,
1086 struct list_head *vc_list)
1089 Elf64_Ehdr *ehdr_ptr;
1090 Elf64_Phdr *phdr_ptr;
1094 ehdr_ptr = (Elf64_Ehdr *)elfptr;
1095 phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr)); /* PT_NOTE hdr */
1097 /* Skip Elf header, program headers and Elf note segment. */
1098 vmcore_off = elfsz + elfnotes_sz;
1100 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
1101 u64 paddr, start, end, size;
1103 if (phdr_ptr->p_type != PT_LOAD)
1106 paddr = phdr_ptr->p_offset;
1107 start = rounddown(paddr, PAGE_SIZE);
1108 end = roundup(paddr + phdr_ptr->p_memsz, PAGE_SIZE);
1111 /* Add this contiguous chunk of memory to vmcore list.*/
1112 new = get_new_element();
1117 list_add_tail(&new->list, vc_list);
1119 /* Update the program header offset. */
1120 phdr_ptr->p_offset = vmcore_off + (paddr - start);
1121 vmcore_off = vmcore_off + size;
1126 static int __init process_ptload_program_headers_elf32(char *elfptr,
1129 struct list_head *vc_list)
1132 Elf32_Ehdr *ehdr_ptr;
1133 Elf32_Phdr *phdr_ptr;
1137 ehdr_ptr = (Elf32_Ehdr *)elfptr;
1138 phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr)); /* PT_NOTE hdr */
1140 /* Skip Elf header, program headers and Elf note segment. */
1141 vmcore_off = elfsz + elfnotes_sz;
1143 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
1144 u64 paddr, start, end, size;
1146 if (phdr_ptr->p_type != PT_LOAD)
1149 paddr = phdr_ptr->p_offset;
1150 start = rounddown(paddr, PAGE_SIZE);
1151 end = roundup(paddr + phdr_ptr->p_memsz, PAGE_SIZE);
1154 /* Add this contiguous chunk of memory to vmcore list.*/
1155 new = get_new_element();
1160 list_add_tail(&new->list, vc_list);
1162 /* Update the program header offset */
1163 phdr_ptr->p_offset = vmcore_off + (paddr - start);
1164 vmcore_off = vmcore_off + size;
1169 /* Sets offset fields of vmcore elements. */
1170 static void set_vmcore_list_offsets(size_t elfsz, size_t elfnotes_sz,
1171 struct list_head *vc_list)
1176 /* Skip Elf header, program headers and Elf note segment. */
1177 vmcore_off = elfsz + elfnotes_sz;
1179 list_for_each_entry(m, vc_list, list) {
1180 m->offset = vmcore_off;
1181 vmcore_off += m->size;
1185 static void free_elfcorebuf(void)
1187 free_pages((unsigned long)elfcorebuf, get_order(elfcorebuf_sz_orig));
1189 vfree(elfnotes_buf);
1190 elfnotes_buf = NULL;
1193 static int __init parse_crash_elf64_headers(void)
1199 addr = elfcorehdr_addr;
1201 /* Read Elf header */
1202 rc = elfcorehdr_read((char *)&ehdr, sizeof(Elf64_Ehdr), &addr);
1206 /* Do some basic Verification. */
1207 if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
1208 (ehdr.e_type != ET_CORE) ||
1209 !vmcore_elf64_check_arch(&ehdr) ||
1210 ehdr.e_ident[EI_CLASS] != ELFCLASS64 ||
1211 ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
1212 ehdr.e_version != EV_CURRENT ||
1213 ehdr.e_ehsize != sizeof(Elf64_Ehdr) ||
1214 ehdr.e_phentsize != sizeof(Elf64_Phdr) ||
1215 ehdr.e_phnum == 0) {
1216 pr_warn("Warning: Core image elf header is not sane\n");
1220 /* Read in all elf headers. */
1221 elfcorebuf_sz_orig = sizeof(Elf64_Ehdr) +
1222 ehdr.e_phnum * sizeof(Elf64_Phdr);
1223 elfcorebuf_sz = elfcorebuf_sz_orig;
1224 elfcorebuf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
1225 get_order(elfcorebuf_sz_orig));
1228 addr = elfcorehdr_addr;
1229 rc = elfcorehdr_read(elfcorebuf, elfcorebuf_sz_orig, &addr);
1233 /* Merge all PT_NOTE headers into one. */
1234 rc = merge_note_headers_elf64(elfcorebuf, &elfcorebuf_sz,
1235 &elfnotes_buf, &elfnotes_sz);
1238 rc = process_ptload_program_headers_elf64(elfcorebuf, elfcorebuf_sz,
1239 elfnotes_sz, &vmcore_list);
1242 set_vmcore_list_offsets(elfcorebuf_sz, elfnotes_sz, &vmcore_list);
1249 static int __init parse_crash_elf32_headers(void)
1255 addr = elfcorehdr_addr;
1257 /* Read Elf header */
1258 rc = elfcorehdr_read((char *)&ehdr, sizeof(Elf32_Ehdr), &addr);
1262 /* Do some basic Verification. */
1263 if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
1264 (ehdr.e_type != ET_CORE) ||
1265 !vmcore_elf32_check_arch(&ehdr) ||
1266 ehdr.e_ident[EI_CLASS] != ELFCLASS32||
1267 ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
1268 ehdr.e_version != EV_CURRENT ||
1269 ehdr.e_ehsize != sizeof(Elf32_Ehdr) ||
1270 ehdr.e_phentsize != sizeof(Elf32_Phdr) ||
1271 ehdr.e_phnum == 0) {
1272 pr_warn("Warning: Core image elf header is not sane\n");
1276 /* Read in all elf headers. */
1277 elfcorebuf_sz_orig = sizeof(Elf32_Ehdr) + ehdr.e_phnum * sizeof(Elf32_Phdr);
1278 elfcorebuf_sz = elfcorebuf_sz_orig;
1279 elfcorebuf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
1280 get_order(elfcorebuf_sz_orig));
1283 addr = elfcorehdr_addr;
1284 rc = elfcorehdr_read(elfcorebuf, elfcorebuf_sz_orig, &addr);
1288 /* Merge all PT_NOTE headers into one. */
1289 rc = merge_note_headers_elf32(elfcorebuf, &elfcorebuf_sz,
1290 &elfnotes_buf, &elfnotes_sz);
1293 rc = process_ptload_program_headers_elf32(elfcorebuf, elfcorebuf_sz,
1294 elfnotes_sz, &vmcore_list);
1297 set_vmcore_list_offsets(elfcorebuf_sz, elfnotes_sz, &vmcore_list);
1304 static int __init parse_crash_elf_headers(void)
1306 unsigned char e_ident[EI_NIDENT];
1310 addr = elfcorehdr_addr;
1311 rc = elfcorehdr_read(e_ident, EI_NIDENT, &addr);
1314 if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) {
1315 pr_warn("Warning: Core image elf header not found\n");
1319 if (e_ident[EI_CLASS] == ELFCLASS64) {
1320 rc = parse_crash_elf64_headers();
1323 } else if (e_ident[EI_CLASS] == ELFCLASS32) {
1324 rc = parse_crash_elf32_headers();
1328 pr_warn("Warning: Core image elf header is not sane\n");
1332 /* Determine vmcore size. */
1333 vmcore_size = get_vmcore_size(elfcorebuf_sz, elfnotes_sz,
1339 #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
1341 * vmcoredd_write_header - Write vmcore device dump header at the
1342 * beginning of the dump's buffer.
1343 * @buf: Output buffer where the note is written
1345 * @size: Size of the dump
1347 * Fills beginning of the dump's buffer with vmcore device dump header.
1349 static void vmcoredd_write_header(void *buf, struct vmcoredd_data *data,
1352 struct vmcoredd_header *vdd_hdr = (struct vmcoredd_header *)buf;
1354 vdd_hdr->n_namesz = sizeof(vdd_hdr->name);
1355 vdd_hdr->n_descsz = size + sizeof(vdd_hdr->dump_name);
1356 vdd_hdr->n_type = NT_VMCOREDD;
1358 strncpy((char *)vdd_hdr->name, VMCOREDD_NOTE_NAME,
1359 sizeof(vdd_hdr->name));
1360 memcpy(vdd_hdr->dump_name, data->dump_name, sizeof(vdd_hdr->dump_name));
1364 * vmcoredd_update_program_headers - Update all Elf program headers
1365 * @elfptr: Pointer to elf header
1366 * @elfnotesz: Size of elf notes aligned to page size
1367 * @vmcoreddsz: Size of device dumps to be added to elf note header
1369 * Determine type of Elf header (Elf64 or Elf32) and update the elf note size.
1370 * Also update the offsets of all the program headers after the elf note header.
1372 static void vmcoredd_update_program_headers(char *elfptr, size_t elfnotesz,
1375 unsigned char *e_ident = (unsigned char *)elfptr;
1376 u64 start, end, size;
1380 vmcore_off = elfcorebuf_sz + elfnotesz;
1382 if (e_ident[EI_CLASS] == ELFCLASS64) {
1383 Elf64_Ehdr *ehdr = (Elf64_Ehdr *)elfptr;
1384 Elf64_Phdr *phdr = (Elf64_Phdr *)(elfptr + sizeof(Elf64_Ehdr));
1386 /* Update all program headers */
1387 for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
1388 if (phdr->p_type == PT_NOTE) {
1389 /* Update note size */
1390 phdr->p_memsz = elfnotes_orig_sz + vmcoreddsz;
1391 phdr->p_filesz = phdr->p_memsz;
1395 start = rounddown(phdr->p_offset, PAGE_SIZE);
1396 end = roundup(phdr->p_offset + phdr->p_memsz,
1399 phdr->p_offset = vmcore_off + (phdr->p_offset - start);
1403 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)elfptr;
1404 Elf32_Phdr *phdr = (Elf32_Phdr *)(elfptr + sizeof(Elf32_Ehdr));
1406 /* Update all program headers */
1407 for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
1408 if (phdr->p_type == PT_NOTE) {
1409 /* Update note size */
1410 phdr->p_memsz = elfnotes_orig_sz + vmcoreddsz;
1411 phdr->p_filesz = phdr->p_memsz;
1415 start = rounddown(phdr->p_offset, PAGE_SIZE);
1416 end = roundup(phdr->p_offset + phdr->p_memsz,
1419 phdr->p_offset = vmcore_off + (phdr->p_offset - start);
1426 * vmcoredd_update_size - Update the total size of the device dumps and update
1428 * @dump_size: Size of the current device dump to be added to total size
1430 * Update the total size of all the device dumps and update the Elf program
1431 * headers. Calculate the new offsets for the vmcore list and update the
1432 * total vmcore size.
1434 static void vmcoredd_update_size(size_t dump_size)
1436 vmcoredd_orig_sz += dump_size;
1437 elfnotes_sz = roundup(elfnotes_orig_sz, PAGE_SIZE) + vmcoredd_orig_sz;
1438 vmcoredd_update_program_headers(elfcorebuf, elfnotes_sz,
1441 /* Update vmcore list offsets */
1442 set_vmcore_list_offsets(elfcorebuf_sz, elfnotes_sz, &vmcore_list);
1444 vmcore_size = get_vmcore_size(elfcorebuf_sz, elfnotes_sz,
1446 proc_vmcore->size = vmcore_size;
1450 * vmcore_add_device_dump - Add a buffer containing device dump to vmcore
1453 * Allocate a buffer and invoke the calling driver's dump collect routine.
1454 * Write Elf note at the beginning of the buffer to indicate vmcore device
1455 * dump and add the dump to global list.
1457 int vmcore_add_device_dump(struct vmcoredd_data *data)
1459 struct vmcoredd_node *dump;
1464 if (vmcoredd_disabled) {
1465 pr_err_once("Device dump is disabled\n");
1469 if (!data || !strlen(data->dump_name) ||
1470 !data->vmcoredd_callback || !data->size)
1473 dump = vzalloc(sizeof(*dump));
1479 /* Keep size of the buffer page aligned so that it can be mmaped */
1480 data_size = roundup(sizeof(struct vmcoredd_header) + data->size,
1483 /* Allocate buffer for driver's to write their dumps */
1484 buf = vmcore_alloc_buf(data_size);
1490 vmcoredd_write_header(buf, data, data_size -
1491 sizeof(struct vmcoredd_header));
1493 /* Invoke the driver's dump collection routing */
1494 ret = data->vmcoredd_callback(data, buf +
1495 sizeof(struct vmcoredd_header));
1500 dump->size = data_size;
1502 /* Add the dump to driver sysfs list */
1503 mutex_lock(&vmcoredd_mutex);
1504 list_add_tail(&dump->list, &vmcoredd_list);
1505 mutex_unlock(&vmcoredd_mutex);
1507 vmcoredd_update_size(data_size);
1519 EXPORT_SYMBOL(vmcore_add_device_dump);
1520 #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
1522 /* Free all dumps in vmcore device dump list */
1523 static void vmcore_free_device_dumps(void)
1525 #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
1526 mutex_lock(&vmcoredd_mutex);
1527 while (!list_empty(&vmcoredd_list)) {
1528 struct vmcoredd_node *dump;
1530 dump = list_first_entry(&vmcoredd_list, struct vmcoredd_node,
1532 list_del(&dump->list);
1536 mutex_unlock(&vmcoredd_mutex);
1537 #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
1540 /* Init function for vmcore module. */
1541 static int __init vmcore_init(void)
1545 /* Allow architectures to allocate ELF header in 2nd kernel */
1546 rc = elfcorehdr_alloc(&elfcorehdr_addr, &elfcorehdr_size);
1550 * If elfcorehdr= has been passed in cmdline or created in 2nd kernel,
1551 * then capture the dump.
1553 if (!(is_vmcore_usable()))
1555 rc = parse_crash_elf_headers();
1557 pr_warn("Kdump: vmcore not initialized\n");
1560 elfcorehdr_free(elfcorehdr_addr);
1561 elfcorehdr_addr = ELFCORE_ADDR_ERR;
1563 proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations);
1565 proc_vmcore->size = vmcore_size;
1568 fs_initcall(vmcore_init);
1570 /* Cleanup function for vmcore module. */
1571 void vmcore_cleanup(void)
1574 proc_remove(proc_vmcore);
1578 /* clear the vmcore list. */
1579 while (!list_empty(&vmcore_list)) {
1582 m = list_first_entry(&vmcore_list, struct vmcore, list);
1588 /* clear vmcore device dump list */
1589 vmcore_free_device_dumps();