2 * fs/proc/vmcore.c Interface for accessing the crash
3 * dump from the system's previous life.
4 * Heavily borrowed from fs/proc/kcore.c
6 * Copyright (C) IBM Corporation, 2004. All rights reserved
11 #include <linux/kcore.h>
12 #include <linux/user.h>
13 #include <linux/elf.h>
14 #include <linux/elfcore.h>
15 #include <linux/export.h>
16 #include <linux/slab.h>
17 #include <linux/highmem.h>
18 #include <linux/printk.h>
19 #include <linux/memblock.h>
20 #include <linux/init.h>
21 #include <linux/crash_dump.h>
22 #include <linux/list.h>
23 #include <linux/mutex.h>
24 #include <linux/vmalloc.h>
25 #include <linux/pagemap.h>
26 #include <linux/uaccess.h>
27 #include <linux/mem_encrypt.h>
28 #include <asm/pgtable.h>
32 /* List representing chunks of contiguous memory areas and their offsets in
35 static LIST_HEAD(vmcore_list);
37 /* Stores the pointer to the buffer containing kernel elf core headers. */
38 static char *elfcorebuf;
39 static size_t elfcorebuf_sz;
40 static size_t elfcorebuf_sz_orig;
42 static char *elfnotes_buf;
43 static size_t elfnotes_sz;
44 /* Size of all notes minus the device dump notes */
45 static size_t elfnotes_orig_sz;
47 /* Total size of vmcore file. */
48 static u64 vmcore_size;
50 static struct proc_dir_entry *proc_vmcore;
52 #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
53 /* Device Dump list and mutex to synchronize access to list */
54 static LIST_HEAD(vmcoredd_list);
55 static DEFINE_MUTEX(vmcoredd_mutex);
56 #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
58 /* Device Dump Size */
59 static size_t vmcoredd_orig_sz;
62 * Returns > 0 for RAM pages, 0 for non-RAM pages, < 0 on error
63 * The called function has to take care of module refcounting.
65 static int (*oldmem_pfn_is_ram)(unsigned long pfn);
67 int register_oldmem_pfn_is_ram(int (*fn)(unsigned long pfn))
69 if (oldmem_pfn_is_ram)
71 oldmem_pfn_is_ram = fn;
74 EXPORT_SYMBOL_GPL(register_oldmem_pfn_is_ram);
76 void unregister_oldmem_pfn_is_ram(void)
78 oldmem_pfn_is_ram = NULL;
81 EXPORT_SYMBOL_GPL(unregister_oldmem_pfn_is_ram);
83 static int pfn_is_ram(unsigned long pfn)
85 int (*fn)(unsigned long pfn);
86 /* pfn is ram unless fn() checks pagetype */
90 * Ask hypervisor if the pfn is really ram.
91 * A ballooned page contains no data and reading from such a page
92 * will cause high load in the hypervisor.
94 fn = oldmem_pfn_is_ram;
101 /* Reads a page from the oldmem device from given offset. */
102 static ssize_t read_from_oldmem(char *buf, size_t count,
103 u64 *ppos, int userbuf,
106 unsigned long pfn, offset;
108 ssize_t read = 0, tmp;
113 offset = (unsigned long)(*ppos % PAGE_SIZE);
114 pfn = (unsigned long)(*ppos / PAGE_SIZE);
117 if (count > (PAGE_SIZE - offset))
118 nr_bytes = PAGE_SIZE - offset;
122 /* If pfn is not ram, return zeros for sparse dump files */
123 if (pfn_is_ram(pfn) == 0)
124 memset(buf, 0, nr_bytes);
127 tmp = copy_oldmem_page_encrypted(pfn, buf,
132 tmp = copy_oldmem_page(pfn, buf, nr_bytes,
150 * Architectures may override this function to allocate ELF header in 2nd kernel
152 int __weak elfcorehdr_alloc(unsigned long long *addr, unsigned long long *size)
158 * Architectures may override this function to free header
160 void __weak elfcorehdr_free(unsigned long long addr)
164 * Architectures may override this function to read from ELF header
166 ssize_t __weak elfcorehdr_read(char *buf, size_t count, u64 *ppos)
168 return read_from_oldmem(buf, count, ppos, 0, false);
172 * Architectures may override this function to read from notes sections
174 ssize_t __weak elfcorehdr_read_notes(char *buf, size_t count, u64 *ppos)
176 return read_from_oldmem(buf, count, ppos, 0, sme_active());
180 * Architectures may override this function to map oldmem
182 int __weak remap_oldmem_pfn_range(struct vm_area_struct *vma,
183 unsigned long from, unsigned long pfn,
184 unsigned long size, pgprot_t prot)
186 prot = pgprot_encrypted(prot);
187 return remap_pfn_range(vma, from, pfn, size, prot);
191 * Architectures which support memory encryption override this.
194 copy_oldmem_page_encrypted(unsigned long pfn, char *buf, size_t csize,
195 unsigned long offset, int userbuf)
197 return copy_oldmem_page(pfn, buf, csize, offset, userbuf);
201 * Copy to either kernel or user space
203 static int copy_to(void *target, void *src, size_t size, int userbuf)
206 if (copy_to_user((char __user *) target, src, size))
209 memcpy(target, src, size);
214 #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
215 static int vmcoredd_copy_dumps(void *dst, u64 start, size_t size, int userbuf)
217 struct vmcoredd_node *dump;
223 mutex_lock(&vmcoredd_mutex);
224 list_for_each_entry(dump, &vmcoredd_list, list) {
225 if (start < offset + dump->size) {
226 tsz = min(offset + (u64)dump->size - start, (u64)size);
227 buf = dump->buf + start - offset;
228 if (copy_to(dst, buf, tsz, userbuf)) {
237 /* Leave now if buffer filled already */
241 offset += dump->size;
245 mutex_unlock(&vmcoredd_mutex);
250 static int vmcoredd_mmap_dumps(struct vm_area_struct *vma, unsigned long dst,
251 u64 start, size_t size)
253 struct vmcoredd_node *dump;
259 mutex_lock(&vmcoredd_mutex);
260 list_for_each_entry(dump, &vmcoredd_list, list) {
261 if (start < offset + dump->size) {
262 tsz = min(offset + (u64)dump->size - start, (u64)size);
263 buf = dump->buf + start - offset;
264 if (remap_vmalloc_range_partial(vma, dst, buf, tsz)) {
273 /* Leave now if buffer filled already */
277 offset += dump->size;
281 mutex_unlock(&vmcoredd_mutex);
284 #endif /* CONFIG_MMU */
285 #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
287 /* Read from the ELF header and then the crash dump. On error, negative value is
288 * returned otherwise number of bytes read are returned.
290 static ssize_t __read_vmcore(char *buffer, size_t buflen, loff_t *fpos,
293 ssize_t acc = 0, tmp;
296 struct vmcore *m = NULL;
298 if (buflen == 0 || *fpos >= vmcore_size)
301 /* trim buflen to not go beyond EOF */
302 if (buflen > vmcore_size - *fpos)
303 buflen = vmcore_size - *fpos;
305 /* Read ELF core header */
306 if (*fpos < elfcorebuf_sz) {
307 tsz = min(elfcorebuf_sz - (size_t)*fpos, buflen);
308 if (copy_to(buffer, elfcorebuf + *fpos, tsz, userbuf))
315 /* leave now if filled buffer already */
320 /* Read Elf note segment */
321 if (*fpos < elfcorebuf_sz + elfnotes_sz) {
324 /* We add device dumps before other elf notes because the
325 * other elf notes may not fill the elf notes buffer
326 * completely and we will end up with zero-filled data
327 * between the elf notes and the device dumps. Tools will
328 * then try to decode this zero-filled data as valid notes
329 * and we don't want that. Hence, adding device dumps before
330 * the other elf notes ensure that zero-filled data can be
333 #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
334 /* Read device dumps */
335 if (*fpos < elfcorebuf_sz + vmcoredd_orig_sz) {
336 tsz = min(elfcorebuf_sz + vmcoredd_orig_sz -
337 (size_t)*fpos, buflen);
338 start = *fpos - elfcorebuf_sz;
339 if (vmcoredd_copy_dumps(buffer, start, tsz, userbuf))
347 /* leave now if filled buffer already */
351 #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
353 /* Read remaining elf notes */
354 tsz = min(elfcorebuf_sz + elfnotes_sz - (size_t)*fpos, buflen);
355 kaddr = elfnotes_buf + *fpos - elfcorebuf_sz - vmcoredd_orig_sz;
356 if (copy_to(buffer, kaddr, tsz, userbuf))
364 /* leave now if filled buffer already */
369 list_for_each_entry(m, &vmcore_list, list) {
370 if (*fpos < m->offset + m->size) {
371 tsz = (size_t)min_t(unsigned long long,
372 m->offset + m->size - *fpos,
374 start = m->paddr + *fpos - m->offset;
375 tmp = read_from_oldmem(buffer, tsz, &start,
376 userbuf, sme_active());
384 /* leave now if filled buffer already */
393 static ssize_t read_vmcore(struct file *file, char __user *buffer,
394 size_t buflen, loff_t *fpos)
396 return __read_vmcore((__force char *) buffer, buflen, fpos, 1);
400 * The vmcore fault handler uses the page cache and fills data using the
401 * standard __vmcore_read() function.
403 * On s390 the fault handler is used for memory regions that can't be mapped
404 * directly with remap_pfn_range().
406 static vm_fault_t mmap_vmcore_fault(struct vm_fault *vmf)
409 struct address_space *mapping = vmf->vma->vm_file->f_mapping;
410 pgoff_t index = vmf->pgoff;
416 page = find_or_create_page(mapping, index, GFP_KERNEL);
419 if (!PageUptodate(page)) {
420 offset = (loff_t) index << PAGE_SHIFT;
421 buf = __va((page_to_pfn(page) << PAGE_SHIFT));
422 rc = __read_vmcore(buf, PAGE_SIZE, &offset, 0);
426 return vmf_error(rc);
428 SetPageUptodate(page);
434 return VM_FAULT_SIGBUS;
438 static const struct vm_operations_struct vmcore_mmap_ops = {
439 .fault = mmap_vmcore_fault,
443 * vmcore_alloc_buf - allocate buffer in vmalloc memory
444 * @sizez: size of buffer
446 * If CONFIG_MMU is defined, use vmalloc_user() to allow users to mmap
447 * the buffer to user-space by means of remap_vmalloc_range().
449 * If CONFIG_MMU is not defined, use vzalloc() since mmap_vmcore() is
450 * disabled and there's no need to allow users to mmap the buffer.
452 static inline char *vmcore_alloc_buf(size_t size)
455 return vmalloc_user(size);
457 return vzalloc(size);
462 * Disable mmap_vmcore() if CONFIG_MMU is not defined. MMU is
463 * essential for mmap_vmcore() in order to map physically
464 * non-contiguous objects (ELF header, ELF note segment and memory
465 * regions in the 1st kernel pointed to by PT_LOAD entries) into
466 * virtually contiguous user-space in ELF layout.
470 * remap_oldmem_pfn_checked - do remap_oldmem_pfn_range replacing all pages
471 * reported as not being ram with the zero page.
473 * @vma: vm_area_struct describing requested mapping
474 * @from: start remapping from
475 * @pfn: page frame number to start remapping to
476 * @size: remapping size
477 * @prot: protection bits
479 * Returns zero on success, -EAGAIN on failure.
481 static int remap_oldmem_pfn_checked(struct vm_area_struct *vma,
482 unsigned long from, unsigned long pfn,
483 unsigned long size, pgprot_t prot)
485 unsigned long map_size;
486 unsigned long pos_start, pos_end, pos;
487 unsigned long zeropage_pfn = my_zero_pfn(0);
491 pos_end = pfn + (size >> PAGE_SHIFT);
493 for (pos = pos_start; pos < pos_end; ++pos) {
494 if (!pfn_is_ram(pos)) {
496 * We hit a page which is not ram. Remap the continuous
497 * region between pos_start and pos-1 and replace
498 * the non-ram page at pos with the zero page.
500 if (pos > pos_start) {
501 /* Remap continuous region */
502 map_size = (pos - pos_start) << PAGE_SHIFT;
503 if (remap_oldmem_pfn_range(vma, from + len,
509 /* Remap the zero page */
510 if (remap_oldmem_pfn_range(vma, from + len,
518 if (pos > pos_start) {
520 map_size = (pos - pos_start) << PAGE_SHIFT;
521 if (remap_oldmem_pfn_range(vma, from + len, pos_start,
527 do_munmap(vma->vm_mm, from, len, NULL);
531 static int vmcore_remap_oldmem_pfn(struct vm_area_struct *vma,
532 unsigned long from, unsigned long pfn,
533 unsigned long size, pgprot_t prot)
536 * Check if oldmem_pfn_is_ram was registered to avoid
537 * looping over all pages without a reason.
539 if (oldmem_pfn_is_ram)
540 return remap_oldmem_pfn_checked(vma, from, pfn, size, prot);
542 return remap_oldmem_pfn_range(vma, from, pfn, size, prot);
545 static int mmap_vmcore(struct file *file, struct vm_area_struct *vma)
547 size_t size = vma->vm_end - vma->vm_start;
548 u64 start, end, len, tsz;
551 start = (u64)vma->vm_pgoff << PAGE_SHIFT;
554 if (size > vmcore_size || end > vmcore_size)
557 if (vma->vm_flags & (VM_WRITE | VM_EXEC))
560 vma->vm_flags &= ~(VM_MAYWRITE | VM_MAYEXEC);
561 vma->vm_flags |= VM_MIXEDMAP;
562 vma->vm_ops = &vmcore_mmap_ops;
566 if (start < elfcorebuf_sz) {
569 tsz = min(elfcorebuf_sz - (size_t)start, size);
570 pfn = __pa(elfcorebuf + start) >> PAGE_SHIFT;
571 if (remap_pfn_range(vma, vma->vm_start, pfn, tsz,
582 if (start < elfcorebuf_sz + elfnotes_sz) {
585 /* We add device dumps before other elf notes because the
586 * other elf notes may not fill the elf notes buffer
587 * completely and we will end up with zero-filled data
588 * between the elf notes and the device dumps. Tools will
589 * then try to decode this zero-filled data as valid notes
590 * and we don't want that. Hence, adding device dumps before
591 * the other elf notes ensure that zero-filled data can be
592 * avoided. This also ensures that the device dumps and
593 * other elf notes can be properly mmaped at page aligned
596 #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
597 /* Read device dumps */
598 if (start < elfcorebuf_sz + vmcoredd_orig_sz) {
601 tsz = min(elfcorebuf_sz + vmcoredd_orig_sz -
602 (size_t)start, size);
603 start_off = start - elfcorebuf_sz;
604 if (vmcoredd_mmap_dumps(vma, vma->vm_start + len,
612 /* leave now if filled buffer already */
616 #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
618 /* Read remaining elf notes */
619 tsz = min(elfcorebuf_sz + elfnotes_sz - (size_t)start, size);
620 kaddr = elfnotes_buf + start - elfcorebuf_sz - vmcoredd_orig_sz;
621 if (remap_vmalloc_range_partial(vma, vma->vm_start + len,
633 list_for_each_entry(m, &vmcore_list, list) {
634 if (start < m->offset + m->size) {
637 tsz = (size_t)min_t(unsigned long long,
638 m->offset + m->size - start, size);
639 paddr = m->paddr + start - m->offset;
640 if (vmcore_remap_oldmem_pfn(vma, vma->vm_start + len,
641 paddr >> PAGE_SHIFT, tsz,
655 do_munmap(vma->vm_mm, vma->vm_start, len, NULL);
659 static int mmap_vmcore(struct file *file, struct vm_area_struct *vma)
665 static const struct file_operations proc_vmcore_operations = {
667 .llseek = default_llseek,
671 static struct vmcore* __init get_new_element(void)
673 return kzalloc(sizeof(struct vmcore), GFP_KERNEL);
676 static u64 get_vmcore_size(size_t elfsz, size_t elfnotesegsz,
677 struct list_head *vc_list)
682 size = elfsz + elfnotesegsz;
683 list_for_each_entry(m, vc_list, list) {
690 * update_note_header_size_elf64 - update p_memsz member of each PT_NOTE entry
692 * @ehdr_ptr: ELF header
694 * This function updates p_memsz member of each PT_NOTE entry in the
695 * program header table pointed to by @ehdr_ptr to real size of ELF
698 static int __init update_note_header_size_elf64(const Elf64_Ehdr *ehdr_ptr)
701 Elf64_Phdr *phdr_ptr;
702 Elf64_Nhdr *nhdr_ptr;
704 phdr_ptr = (Elf64_Phdr *)(ehdr_ptr + 1);
705 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
707 u64 offset, max_sz, sz, real_sz = 0;
708 if (phdr_ptr->p_type != PT_NOTE)
710 max_sz = phdr_ptr->p_memsz;
711 offset = phdr_ptr->p_offset;
712 notes_section = kmalloc(max_sz, GFP_KERNEL);
715 rc = elfcorehdr_read_notes(notes_section, max_sz, &offset);
717 kfree(notes_section);
720 nhdr_ptr = notes_section;
721 while (nhdr_ptr->n_namesz != 0) {
722 sz = sizeof(Elf64_Nhdr) +
723 (((u64)nhdr_ptr->n_namesz + 3) & ~3) +
724 (((u64)nhdr_ptr->n_descsz + 3) & ~3);
725 if ((real_sz + sz) > max_sz) {
726 pr_warn("Warning: Exceeded p_memsz, dropping PT_NOTE entry n_namesz=0x%x, n_descsz=0x%x\n",
727 nhdr_ptr->n_namesz, nhdr_ptr->n_descsz);
731 nhdr_ptr = (Elf64_Nhdr*)((char*)nhdr_ptr + sz);
733 kfree(notes_section);
734 phdr_ptr->p_memsz = real_sz;
736 pr_warn("Warning: Zero PT_NOTE entries found\n");
744 * get_note_number_and_size_elf64 - get the number of PT_NOTE program
745 * headers and sum of real size of their ELF note segment headers and
748 * @ehdr_ptr: ELF header
749 * @nr_ptnote: buffer for the number of PT_NOTE program headers
750 * @sz_ptnote: buffer for size of unique PT_NOTE program header
752 * This function is used to merge multiple PT_NOTE program headers
753 * into a unique single one. The resulting unique entry will have
754 * @sz_ptnote in its phdr->p_mem.
756 * It is assumed that program headers with PT_NOTE type pointed to by
757 * @ehdr_ptr has already been updated by update_note_header_size_elf64
758 * and each of PT_NOTE program headers has actual ELF note segment
759 * size in its p_memsz member.
761 static int __init get_note_number_and_size_elf64(const Elf64_Ehdr *ehdr_ptr,
762 int *nr_ptnote, u64 *sz_ptnote)
765 Elf64_Phdr *phdr_ptr;
767 *nr_ptnote = *sz_ptnote = 0;
769 phdr_ptr = (Elf64_Phdr *)(ehdr_ptr + 1);
770 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
771 if (phdr_ptr->p_type != PT_NOTE)
774 *sz_ptnote += phdr_ptr->p_memsz;
781 * copy_notes_elf64 - copy ELF note segments in a given buffer
783 * @ehdr_ptr: ELF header
784 * @notes_buf: buffer into which ELF note segments are copied
786 * This function is used to copy ELF note segment in the 1st kernel
787 * into the buffer @notes_buf in the 2nd kernel. It is assumed that
788 * size of the buffer @notes_buf is equal to or larger than sum of the
789 * real ELF note segment headers and data.
791 * It is assumed that program headers with PT_NOTE type pointed to by
792 * @ehdr_ptr has already been updated by update_note_header_size_elf64
793 * and each of PT_NOTE program headers has actual ELF note segment
794 * size in its p_memsz member.
796 static int __init copy_notes_elf64(const Elf64_Ehdr *ehdr_ptr, char *notes_buf)
799 Elf64_Phdr *phdr_ptr;
801 phdr_ptr = (Elf64_Phdr*)(ehdr_ptr + 1);
803 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
805 if (phdr_ptr->p_type != PT_NOTE)
807 offset = phdr_ptr->p_offset;
808 rc = elfcorehdr_read_notes(notes_buf, phdr_ptr->p_memsz,
812 notes_buf += phdr_ptr->p_memsz;
818 /* Merges all the PT_NOTE headers into one. */
819 static int __init merge_note_headers_elf64(char *elfptr, size_t *elfsz,
820 char **notes_buf, size_t *notes_sz)
822 int i, nr_ptnote=0, rc=0;
824 Elf64_Ehdr *ehdr_ptr;
826 u64 phdr_sz = 0, note_off;
828 ehdr_ptr = (Elf64_Ehdr *)elfptr;
830 rc = update_note_header_size_elf64(ehdr_ptr);
834 rc = get_note_number_and_size_elf64(ehdr_ptr, &nr_ptnote, &phdr_sz);
838 *notes_sz = roundup(phdr_sz, PAGE_SIZE);
839 *notes_buf = vmcore_alloc_buf(*notes_sz);
843 rc = copy_notes_elf64(ehdr_ptr, *notes_buf);
847 /* Prepare merged PT_NOTE program header. */
848 phdr.p_type = PT_NOTE;
850 note_off = sizeof(Elf64_Ehdr) +
851 (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf64_Phdr);
852 phdr.p_offset = roundup(note_off, PAGE_SIZE);
853 phdr.p_vaddr = phdr.p_paddr = 0;
854 phdr.p_filesz = phdr.p_memsz = phdr_sz;
857 /* Add merged PT_NOTE program header*/
858 tmp = elfptr + sizeof(Elf64_Ehdr);
859 memcpy(tmp, &phdr, sizeof(phdr));
862 /* Remove unwanted PT_NOTE program headers. */
863 i = (nr_ptnote - 1) * sizeof(Elf64_Phdr);
865 memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf64_Ehdr)-sizeof(Elf64_Phdr)));
866 memset(elfptr + *elfsz, 0, i);
867 *elfsz = roundup(*elfsz, PAGE_SIZE);
869 /* Modify e_phnum to reflect merged headers. */
870 ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
872 /* Store the size of all notes. We need this to update the note
873 * header when the device dumps will be added.
875 elfnotes_orig_sz = phdr.p_memsz;
881 * update_note_header_size_elf32 - update p_memsz member of each PT_NOTE entry
883 * @ehdr_ptr: ELF header
885 * This function updates p_memsz member of each PT_NOTE entry in the
886 * program header table pointed to by @ehdr_ptr to real size of ELF
889 static int __init update_note_header_size_elf32(const Elf32_Ehdr *ehdr_ptr)
892 Elf32_Phdr *phdr_ptr;
893 Elf32_Nhdr *nhdr_ptr;
895 phdr_ptr = (Elf32_Phdr *)(ehdr_ptr + 1);
896 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
898 u64 offset, max_sz, sz, real_sz = 0;
899 if (phdr_ptr->p_type != PT_NOTE)
901 max_sz = phdr_ptr->p_memsz;
902 offset = phdr_ptr->p_offset;
903 notes_section = kmalloc(max_sz, GFP_KERNEL);
906 rc = elfcorehdr_read_notes(notes_section, max_sz, &offset);
908 kfree(notes_section);
911 nhdr_ptr = notes_section;
912 while (nhdr_ptr->n_namesz != 0) {
913 sz = sizeof(Elf32_Nhdr) +
914 (((u64)nhdr_ptr->n_namesz + 3) & ~3) +
915 (((u64)nhdr_ptr->n_descsz + 3) & ~3);
916 if ((real_sz + sz) > max_sz) {
917 pr_warn("Warning: Exceeded p_memsz, dropping PT_NOTE entry n_namesz=0x%x, n_descsz=0x%x\n",
918 nhdr_ptr->n_namesz, nhdr_ptr->n_descsz);
922 nhdr_ptr = (Elf32_Nhdr*)((char*)nhdr_ptr + sz);
924 kfree(notes_section);
925 phdr_ptr->p_memsz = real_sz;
927 pr_warn("Warning: Zero PT_NOTE entries found\n");
935 * get_note_number_and_size_elf32 - get the number of PT_NOTE program
936 * headers and sum of real size of their ELF note segment headers and
939 * @ehdr_ptr: ELF header
940 * @nr_ptnote: buffer for the number of PT_NOTE program headers
941 * @sz_ptnote: buffer for size of unique PT_NOTE program header
943 * This function is used to merge multiple PT_NOTE program headers
944 * into a unique single one. The resulting unique entry will have
945 * @sz_ptnote in its phdr->p_mem.
947 * It is assumed that program headers with PT_NOTE type pointed to by
948 * @ehdr_ptr has already been updated by update_note_header_size_elf32
949 * and each of PT_NOTE program headers has actual ELF note segment
950 * size in its p_memsz member.
952 static int __init get_note_number_and_size_elf32(const Elf32_Ehdr *ehdr_ptr,
953 int *nr_ptnote, u64 *sz_ptnote)
956 Elf32_Phdr *phdr_ptr;
958 *nr_ptnote = *sz_ptnote = 0;
960 phdr_ptr = (Elf32_Phdr *)(ehdr_ptr + 1);
961 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
962 if (phdr_ptr->p_type != PT_NOTE)
965 *sz_ptnote += phdr_ptr->p_memsz;
972 * copy_notes_elf32 - copy ELF note segments in a given buffer
974 * @ehdr_ptr: ELF header
975 * @notes_buf: buffer into which ELF note segments are copied
977 * This function is used to copy ELF note segment in the 1st kernel
978 * into the buffer @notes_buf in the 2nd kernel. It is assumed that
979 * size of the buffer @notes_buf is equal to or larger than sum of the
980 * real ELF note segment headers and data.
982 * It is assumed that program headers with PT_NOTE type pointed to by
983 * @ehdr_ptr has already been updated by update_note_header_size_elf32
984 * and each of PT_NOTE program headers has actual ELF note segment
985 * size in its p_memsz member.
987 static int __init copy_notes_elf32(const Elf32_Ehdr *ehdr_ptr, char *notes_buf)
990 Elf32_Phdr *phdr_ptr;
992 phdr_ptr = (Elf32_Phdr*)(ehdr_ptr + 1);
994 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
996 if (phdr_ptr->p_type != PT_NOTE)
998 offset = phdr_ptr->p_offset;
999 rc = elfcorehdr_read_notes(notes_buf, phdr_ptr->p_memsz,
1003 notes_buf += phdr_ptr->p_memsz;
1009 /* Merges all the PT_NOTE headers into one. */
1010 static int __init merge_note_headers_elf32(char *elfptr, size_t *elfsz,
1011 char **notes_buf, size_t *notes_sz)
1013 int i, nr_ptnote=0, rc=0;
1015 Elf32_Ehdr *ehdr_ptr;
1017 u64 phdr_sz = 0, note_off;
1019 ehdr_ptr = (Elf32_Ehdr *)elfptr;
1021 rc = update_note_header_size_elf32(ehdr_ptr);
1025 rc = get_note_number_and_size_elf32(ehdr_ptr, &nr_ptnote, &phdr_sz);
1029 *notes_sz = roundup(phdr_sz, PAGE_SIZE);
1030 *notes_buf = vmcore_alloc_buf(*notes_sz);
1034 rc = copy_notes_elf32(ehdr_ptr, *notes_buf);
1038 /* Prepare merged PT_NOTE program header. */
1039 phdr.p_type = PT_NOTE;
1041 note_off = sizeof(Elf32_Ehdr) +
1042 (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf32_Phdr);
1043 phdr.p_offset = roundup(note_off, PAGE_SIZE);
1044 phdr.p_vaddr = phdr.p_paddr = 0;
1045 phdr.p_filesz = phdr.p_memsz = phdr_sz;
1048 /* Add merged PT_NOTE program header*/
1049 tmp = elfptr + sizeof(Elf32_Ehdr);
1050 memcpy(tmp, &phdr, sizeof(phdr));
1051 tmp += sizeof(phdr);
1053 /* Remove unwanted PT_NOTE program headers. */
1054 i = (nr_ptnote - 1) * sizeof(Elf32_Phdr);
1055 *elfsz = *elfsz - i;
1056 memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf32_Ehdr)-sizeof(Elf32_Phdr)));
1057 memset(elfptr + *elfsz, 0, i);
1058 *elfsz = roundup(*elfsz, PAGE_SIZE);
1060 /* Modify e_phnum to reflect merged headers. */
1061 ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
1063 /* Store the size of all notes. We need this to update the note
1064 * header when the device dumps will be added.
1066 elfnotes_orig_sz = phdr.p_memsz;
1071 /* Add memory chunks represented by program headers to vmcore list. Also update
1072 * the new offset fields of exported program headers. */
1073 static int __init process_ptload_program_headers_elf64(char *elfptr,
1076 struct list_head *vc_list)
1079 Elf64_Ehdr *ehdr_ptr;
1080 Elf64_Phdr *phdr_ptr;
1084 ehdr_ptr = (Elf64_Ehdr *)elfptr;
1085 phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr)); /* PT_NOTE hdr */
1087 /* Skip Elf header, program headers and Elf note segment. */
1088 vmcore_off = elfsz + elfnotes_sz;
1090 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
1091 u64 paddr, start, end, size;
1093 if (phdr_ptr->p_type != PT_LOAD)
1096 paddr = phdr_ptr->p_offset;
1097 start = rounddown(paddr, PAGE_SIZE);
1098 end = roundup(paddr + phdr_ptr->p_memsz, PAGE_SIZE);
1101 /* Add this contiguous chunk of memory to vmcore list.*/
1102 new = get_new_element();
1107 list_add_tail(&new->list, vc_list);
1109 /* Update the program header offset. */
1110 phdr_ptr->p_offset = vmcore_off + (paddr - start);
1111 vmcore_off = vmcore_off + size;
1116 static int __init process_ptload_program_headers_elf32(char *elfptr,
1119 struct list_head *vc_list)
1122 Elf32_Ehdr *ehdr_ptr;
1123 Elf32_Phdr *phdr_ptr;
1127 ehdr_ptr = (Elf32_Ehdr *)elfptr;
1128 phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr)); /* PT_NOTE hdr */
1130 /* Skip Elf header, program headers and Elf note segment. */
1131 vmcore_off = elfsz + elfnotes_sz;
1133 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
1134 u64 paddr, start, end, size;
1136 if (phdr_ptr->p_type != PT_LOAD)
1139 paddr = phdr_ptr->p_offset;
1140 start = rounddown(paddr, PAGE_SIZE);
1141 end = roundup(paddr + phdr_ptr->p_memsz, PAGE_SIZE);
1144 /* Add this contiguous chunk of memory to vmcore list.*/
1145 new = get_new_element();
1150 list_add_tail(&new->list, vc_list);
1152 /* Update the program header offset */
1153 phdr_ptr->p_offset = vmcore_off + (paddr - start);
1154 vmcore_off = vmcore_off + size;
1159 /* Sets offset fields of vmcore elements. */
1160 static void set_vmcore_list_offsets(size_t elfsz, size_t elfnotes_sz,
1161 struct list_head *vc_list)
1166 /* Skip Elf header, program headers and Elf note segment. */
1167 vmcore_off = elfsz + elfnotes_sz;
1169 list_for_each_entry(m, vc_list, list) {
1170 m->offset = vmcore_off;
1171 vmcore_off += m->size;
1175 static void free_elfcorebuf(void)
1177 free_pages((unsigned long)elfcorebuf, get_order(elfcorebuf_sz_orig));
1179 vfree(elfnotes_buf);
1180 elfnotes_buf = NULL;
1183 static int __init parse_crash_elf64_headers(void)
1189 addr = elfcorehdr_addr;
1191 /* Read Elf header */
1192 rc = elfcorehdr_read((char *)&ehdr, sizeof(Elf64_Ehdr), &addr);
1196 /* Do some basic Verification. */
1197 if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
1198 (ehdr.e_type != ET_CORE) ||
1199 !vmcore_elf64_check_arch(&ehdr) ||
1200 ehdr.e_ident[EI_CLASS] != ELFCLASS64 ||
1201 ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
1202 ehdr.e_version != EV_CURRENT ||
1203 ehdr.e_ehsize != sizeof(Elf64_Ehdr) ||
1204 ehdr.e_phentsize != sizeof(Elf64_Phdr) ||
1205 ehdr.e_phnum == 0) {
1206 pr_warn("Warning: Core image elf header is not sane\n");
1210 /* Read in all elf headers. */
1211 elfcorebuf_sz_orig = sizeof(Elf64_Ehdr) +
1212 ehdr.e_phnum * sizeof(Elf64_Phdr);
1213 elfcorebuf_sz = elfcorebuf_sz_orig;
1214 elfcorebuf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
1215 get_order(elfcorebuf_sz_orig));
1218 addr = elfcorehdr_addr;
1219 rc = elfcorehdr_read(elfcorebuf, elfcorebuf_sz_orig, &addr);
1223 /* Merge all PT_NOTE headers into one. */
1224 rc = merge_note_headers_elf64(elfcorebuf, &elfcorebuf_sz,
1225 &elfnotes_buf, &elfnotes_sz);
1228 rc = process_ptload_program_headers_elf64(elfcorebuf, elfcorebuf_sz,
1229 elfnotes_sz, &vmcore_list);
1232 set_vmcore_list_offsets(elfcorebuf_sz, elfnotes_sz, &vmcore_list);
1239 static int __init parse_crash_elf32_headers(void)
1245 addr = elfcorehdr_addr;
1247 /* Read Elf header */
1248 rc = elfcorehdr_read((char *)&ehdr, sizeof(Elf32_Ehdr), &addr);
1252 /* Do some basic Verification. */
1253 if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
1254 (ehdr.e_type != ET_CORE) ||
1255 !vmcore_elf32_check_arch(&ehdr) ||
1256 ehdr.e_ident[EI_CLASS] != ELFCLASS32||
1257 ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
1258 ehdr.e_version != EV_CURRENT ||
1259 ehdr.e_ehsize != sizeof(Elf32_Ehdr) ||
1260 ehdr.e_phentsize != sizeof(Elf32_Phdr) ||
1261 ehdr.e_phnum == 0) {
1262 pr_warn("Warning: Core image elf header is not sane\n");
1266 /* Read in all elf headers. */
1267 elfcorebuf_sz_orig = sizeof(Elf32_Ehdr) + ehdr.e_phnum * sizeof(Elf32_Phdr);
1268 elfcorebuf_sz = elfcorebuf_sz_orig;
1269 elfcorebuf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
1270 get_order(elfcorebuf_sz_orig));
1273 addr = elfcorehdr_addr;
1274 rc = elfcorehdr_read(elfcorebuf, elfcorebuf_sz_orig, &addr);
1278 /* Merge all PT_NOTE headers into one. */
1279 rc = merge_note_headers_elf32(elfcorebuf, &elfcorebuf_sz,
1280 &elfnotes_buf, &elfnotes_sz);
1283 rc = process_ptload_program_headers_elf32(elfcorebuf, elfcorebuf_sz,
1284 elfnotes_sz, &vmcore_list);
1287 set_vmcore_list_offsets(elfcorebuf_sz, elfnotes_sz, &vmcore_list);
1294 static int __init parse_crash_elf_headers(void)
1296 unsigned char e_ident[EI_NIDENT];
1300 addr = elfcorehdr_addr;
1301 rc = elfcorehdr_read(e_ident, EI_NIDENT, &addr);
1304 if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) {
1305 pr_warn("Warning: Core image elf header not found\n");
1309 if (e_ident[EI_CLASS] == ELFCLASS64) {
1310 rc = parse_crash_elf64_headers();
1313 } else if (e_ident[EI_CLASS] == ELFCLASS32) {
1314 rc = parse_crash_elf32_headers();
1318 pr_warn("Warning: Core image elf header is not sane\n");
1322 /* Determine vmcore size. */
1323 vmcore_size = get_vmcore_size(elfcorebuf_sz, elfnotes_sz,
1329 #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
1331 * vmcoredd_write_header - Write vmcore device dump header at the
1332 * beginning of the dump's buffer.
1333 * @buf: Output buffer where the note is written
1335 * @size: Size of the dump
1337 * Fills beginning of the dump's buffer with vmcore device dump header.
1339 static void vmcoredd_write_header(void *buf, struct vmcoredd_data *data,
1342 struct vmcoredd_header *vdd_hdr = (struct vmcoredd_header *)buf;
1344 vdd_hdr->n_namesz = sizeof(vdd_hdr->name);
1345 vdd_hdr->n_descsz = size + sizeof(vdd_hdr->dump_name);
1346 vdd_hdr->n_type = NT_VMCOREDD;
1348 strncpy((char *)vdd_hdr->name, VMCOREDD_NOTE_NAME,
1349 sizeof(vdd_hdr->name));
1350 memcpy(vdd_hdr->dump_name, data->dump_name, sizeof(vdd_hdr->dump_name));
1354 * vmcoredd_update_program_headers - Update all Elf program headers
1355 * @elfptr: Pointer to elf header
1356 * @elfnotesz: Size of elf notes aligned to page size
1357 * @vmcoreddsz: Size of device dumps to be added to elf note header
1359 * Determine type of Elf header (Elf64 or Elf32) and update the elf note size.
1360 * Also update the offsets of all the program headers after the elf note header.
1362 static void vmcoredd_update_program_headers(char *elfptr, size_t elfnotesz,
1365 unsigned char *e_ident = (unsigned char *)elfptr;
1366 u64 start, end, size;
1370 vmcore_off = elfcorebuf_sz + elfnotesz;
1372 if (e_ident[EI_CLASS] == ELFCLASS64) {
1373 Elf64_Ehdr *ehdr = (Elf64_Ehdr *)elfptr;
1374 Elf64_Phdr *phdr = (Elf64_Phdr *)(elfptr + sizeof(Elf64_Ehdr));
1376 /* Update all program headers */
1377 for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
1378 if (phdr->p_type == PT_NOTE) {
1379 /* Update note size */
1380 phdr->p_memsz = elfnotes_orig_sz + vmcoreddsz;
1381 phdr->p_filesz = phdr->p_memsz;
1385 start = rounddown(phdr->p_offset, PAGE_SIZE);
1386 end = roundup(phdr->p_offset + phdr->p_memsz,
1389 phdr->p_offset = vmcore_off + (phdr->p_offset - start);
1393 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)elfptr;
1394 Elf32_Phdr *phdr = (Elf32_Phdr *)(elfptr + sizeof(Elf32_Ehdr));
1396 /* Update all program headers */
1397 for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
1398 if (phdr->p_type == PT_NOTE) {
1399 /* Update note size */
1400 phdr->p_memsz = elfnotes_orig_sz + vmcoreddsz;
1401 phdr->p_filesz = phdr->p_memsz;
1405 start = rounddown(phdr->p_offset, PAGE_SIZE);
1406 end = roundup(phdr->p_offset + phdr->p_memsz,
1409 phdr->p_offset = vmcore_off + (phdr->p_offset - start);
1416 * vmcoredd_update_size - Update the total size of the device dumps and update
1418 * @dump_size: Size of the current device dump to be added to total size
1420 * Update the total size of all the device dumps and update the Elf program
1421 * headers. Calculate the new offsets for the vmcore list and update the
1422 * total vmcore size.
1424 static void vmcoredd_update_size(size_t dump_size)
1426 vmcoredd_orig_sz += dump_size;
1427 elfnotes_sz = roundup(elfnotes_orig_sz, PAGE_SIZE) + vmcoredd_orig_sz;
1428 vmcoredd_update_program_headers(elfcorebuf, elfnotes_sz,
1431 /* Update vmcore list offsets */
1432 set_vmcore_list_offsets(elfcorebuf_sz, elfnotes_sz, &vmcore_list);
1434 vmcore_size = get_vmcore_size(elfcorebuf_sz, elfnotes_sz,
1436 proc_vmcore->size = vmcore_size;
1440 * vmcore_add_device_dump - Add a buffer containing device dump to vmcore
1443 * Allocate a buffer and invoke the calling driver's dump collect routine.
1444 * Write Elf note at the beginning of the buffer to indicate vmcore device
1445 * dump and add the dump to global list.
1447 int vmcore_add_device_dump(struct vmcoredd_data *data)
1449 struct vmcoredd_node *dump;
1454 if (!data || !strlen(data->dump_name) ||
1455 !data->vmcoredd_callback || !data->size)
1458 dump = vzalloc(sizeof(*dump));
1464 /* Keep size of the buffer page aligned so that it can be mmaped */
1465 data_size = roundup(sizeof(struct vmcoredd_header) + data->size,
1468 /* Allocate buffer for driver's to write their dumps */
1469 buf = vmcore_alloc_buf(data_size);
1475 vmcoredd_write_header(buf, data, data_size -
1476 sizeof(struct vmcoredd_header));
1478 /* Invoke the driver's dump collection routing */
1479 ret = data->vmcoredd_callback(data, buf +
1480 sizeof(struct vmcoredd_header));
1485 dump->size = data_size;
1487 /* Add the dump to driver sysfs list */
1488 mutex_lock(&vmcoredd_mutex);
1489 list_add_tail(&dump->list, &vmcoredd_list);
1490 mutex_unlock(&vmcoredd_mutex);
1492 vmcoredd_update_size(data_size);
1504 EXPORT_SYMBOL(vmcore_add_device_dump);
1505 #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
1507 /* Free all dumps in vmcore device dump list */
1508 static void vmcore_free_device_dumps(void)
1510 #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
1511 mutex_lock(&vmcoredd_mutex);
1512 while (!list_empty(&vmcoredd_list)) {
1513 struct vmcoredd_node *dump;
1515 dump = list_first_entry(&vmcoredd_list, struct vmcoredd_node,
1517 list_del(&dump->list);
1521 mutex_unlock(&vmcoredd_mutex);
1522 #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
1525 /* Init function for vmcore module. */
1526 static int __init vmcore_init(void)
1530 /* Allow architectures to allocate ELF header in 2nd kernel */
1531 rc = elfcorehdr_alloc(&elfcorehdr_addr, &elfcorehdr_size);
1535 * If elfcorehdr= has been passed in cmdline or created in 2nd kernel,
1536 * then capture the dump.
1538 if (!(is_vmcore_usable()))
1540 rc = parse_crash_elf_headers();
1542 pr_warn("Kdump: vmcore not initialized\n");
1545 elfcorehdr_free(elfcorehdr_addr);
1546 elfcorehdr_addr = ELFCORE_ADDR_ERR;
1548 proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations);
1550 proc_vmcore->size = vmcore_size;
1553 fs_initcall(vmcore_init);
1555 /* Cleanup function for vmcore module. */
1556 void vmcore_cleanup(void)
1559 proc_remove(proc_vmcore);
1563 /* clear the vmcore list. */
1564 while (!list_empty(&vmcore_list)) {
1567 m = list_first_entry(&vmcore_list, struct vmcore, list);
1573 /* clear vmcore device dump list */
1574 vmcore_free_device_dumps();