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
5 * Created by: Hariprasad Nellitheertha (hari@in.ibm.com)
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/bootmem.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>
30 /* List representing chunks of contiguous memory areas and their offsets in
33 static LIST_HEAD(vmcore_list);
35 /* Stores the pointer to the buffer containing kernel elf core headers. */
36 static char *elfcorebuf;
37 static size_t elfcorebuf_sz;
38 static size_t elfcorebuf_sz_orig;
40 static char *elfnotes_buf;
41 static size_t elfnotes_sz;
42 /* Size of all notes minus the device dump notes */
43 static size_t elfnotes_orig_sz;
45 /* Total size of vmcore file. */
46 static u64 vmcore_size;
48 static struct proc_dir_entry *proc_vmcore;
50 #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
51 /* Device Dump list and mutex to synchronize access to list */
52 static LIST_HEAD(vmcoredd_list);
53 static DEFINE_MUTEX(vmcoredd_mutex);
54 #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
56 /* Device Dump Size */
57 static size_t vmcoredd_orig_sz;
60 * Returns > 0 for RAM pages, 0 for non-RAM pages, < 0 on error
61 * The called function has to take care of module refcounting.
63 static int (*oldmem_pfn_is_ram)(unsigned long pfn);
65 int register_oldmem_pfn_is_ram(int (*fn)(unsigned long pfn))
67 if (oldmem_pfn_is_ram)
69 oldmem_pfn_is_ram = fn;
72 EXPORT_SYMBOL_GPL(register_oldmem_pfn_is_ram);
74 void unregister_oldmem_pfn_is_ram(void)
76 oldmem_pfn_is_ram = NULL;
79 EXPORT_SYMBOL_GPL(unregister_oldmem_pfn_is_ram);
81 static int pfn_is_ram(unsigned long pfn)
83 int (*fn)(unsigned long pfn);
84 /* pfn is ram unless fn() checks pagetype */
88 * Ask hypervisor if the pfn is really ram.
89 * A ballooned page contains no data and reading from such a page
90 * will cause high load in the hypervisor.
92 fn = oldmem_pfn_is_ram;
99 /* Reads a page from the oldmem device from given offset. */
100 static ssize_t read_from_oldmem(char *buf, size_t count,
101 u64 *ppos, int userbuf)
103 unsigned long pfn, offset;
105 ssize_t read = 0, tmp;
110 offset = (unsigned long)(*ppos % PAGE_SIZE);
111 pfn = (unsigned long)(*ppos / PAGE_SIZE);
114 if (count > (PAGE_SIZE - offset))
115 nr_bytes = PAGE_SIZE - offset;
119 /* If pfn is not ram, return zeros for sparse dump files */
120 if (pfn_is_ram(pfn) == 0)
121 memset(buf, 0, nr_bytes);
123 tmp = copy_oldmem_page(pfn, buf, nr_bytes,
140 * Architectures may override this function to allocate ELF header in 2nd kernel
142 int __weak elfcorehdr_alloc(unsigned long long *addr, unsigned long long *size)
148 * Architectures may override this function to free header
150 void __weak elfcorehdr_free(unsigned long long addr)
154 * Architectures may override this function to read from ELF header
156 ssize_t __weak elfcorehdr_read(char *buf, size_t count, u64 *ppos)
158 return read_from_oldmem(buf, count, ppos, 0);
162 * Architectures may override this function to read from notes sections
164 ssize_t __weak elfcorehdr_read_notes(char *buf, size_t count, u64 *ppos)
166 return read_from_oldmem(buf, count, ppos, 0);
170 * Architectures may override this function to map oldmem
172 int __weak remap_oldmem_pfn_range(struct vm_area_struct *vma,
173 unsigned long from, unsigned long pfn,
174 unsigned long size, pgprot_t prot)
176 return remap_pfn_range(vma, from, pfn, size, prot);
180 * Architectures which support memory encryption override this.
183 copy_oldmem_page_encrypted(unsigned long pfn, char *buf, size_t csize,
184 unsigned long offset, int userbuf)
186 return copy_oldmem_page(pfn, buf, csize, offset, userbuf);
190 * Copy to either kernel or user space
192 static int copy_to(void *target, void *src, size_t size, int userbuf)
195 if (copy_to_user((char __user *) target, src, size))
198 memcpy(target, src, size);
203 #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
204 static int vmcoredd_copy_dumps(void *dst, u64 start, size_t size, int userbuf)
206 struct vmcoredd_node *dump;
212 mutex_lock(&vmcoredd_mutex);
213 list_for_each_entry(dump, &vmcoredd_list, list) {
214 if (start < offset + dump->size) {
215 tsz = min(offset + (u64)dump->size - start, (u64)size);
216 buf = dump->buf + start - offset;
217 if (copy_to(dst, buf, tsz, userbuf)) {
226 /* Leave now if buffer filled already */
230 offset += dump->size;
234 mutex_unlock(&vmcoredd_mutex);
239 static int vmcoredd_mmap_dumps(struct vm_area_struct *vma, unsigned long dst,
240 u64 start, size_t size)
242 struct vmcoredd_node *dump;
248 mutex_lock(&vmcoredd_mutex);
249 list_for_each_entry(dump, &vmcoredd_list, list) {
250 if (start < offset + dump->size) {
251 tsz = min(offset + (u64)dump->size - start, (u64)size);
252 buf = dump->buf + start - offset;
253 if (remap_vmalloc_range_partial(vma, dst, buf, 0,
263 /* Leave now if buffer filled already */
267 offset += dump->size;
271 mutex_unlock(&vmcoredd_mutex);
274 #endif /* CONFIG_MMU */
275 #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
277 /* Read from the ELF header and then the crash dump. On error, negative value is
278 * returned otherwise number of bytes read are returned.
280 static ssize_t __read_vmcore(char *buffer, size_t buflen, loff_t *fpos,
283 ssize_t acc = 0, tmp;
286 struct vmcore *m = NULL;
288 if (buflen == 0 || *fpos >= vmcore_size)
291 /* trim buflen to not go beyond EOF */
292 if (buflen > vmcore_size - *fpos)
293 buflen = vmcore_size - *fpos;
295 /* Read ELF core header */
296 if (*fpos < elfcorebuf_sz) {
297 tsz = min(elfcorebuf_sz - (size_t)*fpos, buflen);
298 if (copy_to(buffer, elfcorebuf + *fpos, tsz, userbuf))
305 /* leave now if filled buffer already */
310 /* Read Elf note segment */
311 if (*fpos < elfcorebuf_sz + elfnotes_sz) {
314 /* We add device dumps before other elf notes because the
315 * other elf notes may not fill the elf notes buffer
316 * completely and we will end up with zero-filled data
317 * between the elf notes and the device dumps. Tools will
318 * then try to decode this zero-filled data as valid notes
319 * and we don't want that. Hence, adding device dumps before
320 * the other elf notes ensure that zero-filled data can be
323 #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
324 /* Read device dumps */
325 if (*fpos < elfcorebuf_sz + vmcoredd_orig_sz) {
326 tsz = min(elfcorebuf_sz + vmcoredd_orig_sz -
327 (size_t)*fpos, buflen);
328 start = *fpos - elfcorebuf_sz;
329 if (vmcoredd_copy_dumps(buffer, start, tsz, userbuf))
337 /* leave now if filled buffer already */
341 #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
343 /* Read remaining elf notes */
344 tsz = min(elfcorebuf_sz + elfnotes_sz - (size_t)*fpos, buflen);
345 kaddr = elfnotes_buf + *fpos - elfcorebuf_sz - vmcoredd_orig_sz;
346 if (copy_to(buffer, kaddr, tsz, userbuf))
354 /* leave now if filled buffer already */
359 list_for_each_entry(m, &vmcore_list, list) {
360 if (*fpos < m->offset + m->size) {
361 tsz = (size_t)min_t(unsigned long long,
362 m->offset + m->size - *fpos,
364 start = m->paddr + *fpos - m->offset;
365 tmp = read_from_oldmem(buffer, tsz, &start, userbuf);
373 /* leave now if filled buffer already */
382 static ssize_t read_vmcore(struct file *file, char __user *buffer,
383 size_t buflen, loff_t *fpos)
385 return __read_vmcore((__force char *) buffer, buflen, fpos, 1);
389 * The vmcore fault handler uses the page cache and fills data using the
390 * standard __vmcore_read() function.
392 * On s390 the fault handler is used for memory regions that can't be mapped
393 * directly with remap_pfn_range().
395 static vm_fault_t mmap_vmcore_fault(struct vm_fault *vmf)
398 struct address_space *mapping = vmf->vma->vm_file->f_mapping;
399 pgoff_t index = vmf->pgoff;
405 page = find_or_create_page(mapping, index, GFP_KERNEL);
408 if (!PageUptodate(page)) {
409 offset = (loff_t) index << PAGE_SHIFT;
410 buf = __va((page_to_pfn(page) << PAGE_SHIFT));
411 rc = __read_vmcore(buf, PAGE_SIZE, &offset, 0);
415 return (rc == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
417 SetPageUptodate(page);
423 return VM_FAULT_SIGBUS;
427 static const struct vm_operations_struct vmcore_mmap_ops = {
428 .fault = mmap_vmcore_fault,
432 * vmcore_alloc_buf - allocate buffer in vmalloc memory
433 * @sizez: size of buffer
435 * If CONFIG_MMU is defined, use vmalloc_user() to allow users to mmap
436 * the buffer to user-space by means of remap_vmalloc_range().
438 * If CONFIG_MMU is not defined, use vzalloc() since mmap_vmcore() is
439 * disabled and there's no need to allow users to mmap the buffer.
441 static inline char *vmcore_alloc_buf(size_t size)
444 return vmalloc_user(size);
446 return vzalloc(size);
451 * Disable mmap_vmcore() if CONFIG_MMU is not defined. MMU is
452 * essential for mmap_vmcore() in order to map physically
453 * non-contiguous objects (ELF header, ELF note segment and memory
454 * regions in the 1st kernel pointed to by PT_LOAD entries) into
455 * virtually contiguous user-space in ELF layout.
459 * remap_oldmem_pfn_checked - do remap_oldmem_pfn_range replacing all pages
460 * reported as not being ram with the zero page.
462 * @vma: vm_area_struct describing requested mapping
463 * @from: start remapping from
464 * @pfn: page frame number to start remapping to
465 * @size: remapping size
466 * @prot: protection bits
468 * Returns zero on success, -EAGAIN on failure.
470 static int remap_oldmem_pfn_checked(struct vm_area_struct *vma,
471 unsigned long from, unsigned long pfn,
472 unsigned long size, pgprot_t prot)
474 unsigned long map_size;
475 unsigned long pos_start, pos_end, pos;
476 unsigned long zeropage_pfn = my_zero_pfn(0);
480 pos_end = pfn + (size >> PAGE_SHIFT);
482 for (pos = pos_start; pos < pos_end; ++pos) {
483 if (!pfn_is_ram(pos)) {
485 * We hit a page which is not ram. Remap the continuous
486 * region between pos_start and pos-1 and replace
487 * the non-ram page at pos with the zero page.
489 if (pos > pos_start) {
490 /* Remap continuous region */
491 map_size = (pos - pos_start) << PAGE_SHIFT;
492 if (remap_oldmem_pfn_range(vma, from + len,
498 /* Remap the zero page */
499 if (remap_oldmem_pfn_range(vma, from + len,
507 if (pos > pos_start) {
509 map_size = (pos - pos_start) << PAGE_SHIFT;
510 if (remap_oldmem_pfn_range(vma, from + len, pos_start,
516 do_munmap(vma->vm_mm, from, len, NULL);
520 static int vmcore_remap_oldmem_pfn(struct vm_area_struct *vma,
521 unsigned long from, unsigned long pfn,
522 unsigned long size, pgprot_t prot)
525 * Check if oldmem_pfn_is_ram was registered to avoid
526 * looping over all pages without a reason.
528 if (oldmem_pfn_is_ram)
529 return remap_oldmem_pfn_checked(vma, from, pfn, size, prot);
531 return remap_oldmem_pfn_range(vma, from, pfn, size, prot);
534 static int mmap_vmcore(struct file *file, struct vm_area_struct *vma)
536 size_t size = vma->vm_end - vma->vm_start;
537 u64 start, end, len, tsz;
540 start = (u64)vma->vm_pgoff << PAGE_SHIFT;
543 if (size > vmcore_size || end > vmcore_size)
546 if (vma->vm_flags & (VM_WRITE | VM_EXEC))
549 vma->vm_flags &= ~(VM_MAYWRITE | VM_MAYEXEC);
550 vma->vm_flags |= VM_MIXEDMAP;
551 vma->vm_ops = &vmcore_mmap_ops;
555 if (start < elfcorebuf_sz) {
558 tsz = min(elfcorebuf_sz - (size_t)start, size);
559 pfn = __pa(elfcorebuf + start) >> PAGE_SHIFT;
560 if (remap_pfn_range(vma, vma->vm_start, pfn, tsz,
571 if (start < elfcorebuf_sz + elfnotes_sz) {
574 /* We add device dumps before other elf notes because the
575 * other elf notes may not fill the elf notes buffer
576 * completely and we will end up with zero-filled data
577 * between the elf notes and the device dumps. Tools will
578 * then try to decode this zero-filled data as valid notes
579 * and we don't want that. Hence, adding device dumps before
580 * the other elf notes ensure that zero-filled data can be
581 * avoided. This also ensures that the device dumps and
582 * other elf notes can be properly mmaped at page aligned
585 #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
586 /* Read device dumps */
587 if (start < elfcorebuf_sz + vmcoredd_orig_sz) {
590 tsz = min(elfcorebuf_sz + vmcoredd_orig_sz -
591 (size_t)start, size);
592 start_off = start - elfcorebuf_sz;
593 if (vmcoredd_mmap_dumps(vma, vma->vm_start + len,
601 /* leave now if filled buffer already */
605 #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
607 /* Read remaining elf notes */
608 tsz = min(elfcorebuf_sz + elfnotes_sz - (size_t)start, size);
609 kaddr = elfnotes_buf + start - elfcorebuf_sz - vmcoredd_orig_sz;
610 if (remap_vmalloc_range_partial(vma, vma->vm_start + len,
622 list_for_each_entry(m, &vmcore_list, list) {
623 if (start < m->offset + m->size) {
626 tsz = (size_t)min_t(unsigned long long,
627 m->offset + m->size - start, size);
628 paddr = m->paddr + start - m->offset;
629 if (vmcore_remap_oldmem_pfn(vma, vma->vm_start + len,
630 paddr >> PAGE_SHIFT, tsz,
644 do_munmap(vma->vm_mm, vma->vm_start, len, NULL);
648 static int mmap_vmcore(struct file *file, struct vm_area_struct *vma)
654 static const struct file_operations proc_vmcore_operations = {
656 .llseek = default_llseek,
660 static struct vmcore* __init get_new_element(void)
662 return kzalloc(sizeof(struct vmcore), GFP_KERNEL);
665 static u64 get_vmcore_size(size_t elfsz, size_t elfnotesegsz,
666 struct list_head *vc_list)
671 size = elfsz + elfnotesegsz;
672 list_for_each_entry(m, vc_list, list) {
679 * update_note_header_size_elf64 - update p_memsz member of each PT_NOTE entry
681 * @ehdr_ptr: ELF header
683 * This function updates p_memsz member of each PT_NOTE entry in the
684 * program header table pointed to by @ehdr_ptr to real size of ELF
687 static int __init update_note_header_size_elf64(const Elf64_Ehdr *ehdr_ptr)
690 Elf64_Phdr *phdr_ptr;
691 Elf64_Nhdr *nhdr_ptr;
693 phdr_ptr = (Elf64_Phdr *)(ehdr_ptr + 1);
694 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
696 u64 offset, max_sz, sz, real_sz = 0;
697 if (phdr_ptr->p_type != PT_NOTE)
699 max_sz = phdr_ptr->p_memsz;
700 offset = phdr_ptr->p_offset;
701 notes_section = kmalloc(max_sz, GFP_KERNEL);
704 rc = elfcorehdr_read_notes(notes_section, max_sz, &offset);
706 kfree(notes_section);
709 nhdr_ptr = notes_section;
710 while (nhdr_ptr->n_namesz != 0) {
711 sz = sizeof(Elf64_Nhdr) +
712 (((u64)nhdr_ptr->n_namesz + 3) & ~3) +
713 (((u64)nhdr_ptr->n_descsz + 3) & ~3);
714 if ((real_sz + sz) > max_sz) {
715 pr_warn("Warning: Exceeded p_memsz, dropping PT_NOTE entry n_namesz=0x%x, n_descsz=0x%x\n",
716 nhdr_ptr->n_namesz, nhdr_ptr->n_descsz);
720 nhdr_ptr = (Elf64_Nhdr*)((char*)nhdr_ptr + sz);
722 kfree(notes_section);
723 phdr_ptr->p_memsz = real_sz;
725 pr_warn("Warning: Zero PT_NOTE entries found\n");
733 * get_note_number_and_size_elf64 - get the number of PT_NOTE program
734 * headers and sum of real size of their ELF note segment headers and
737 * @ehdr_ptr: ELF header
738 * @nr_ptnote: buffer for the number of PT_NOTE program headers
739 * @sz_ptnote: buffer for size of unique PT_NOTE program header
741 * This function is used to merge multiple PT_NOTE program headers
742 * into a unique single one. The resulting unique entry will have
743 * @sz_ptnote in its phdr->p_mem.
745 * It is assumed that program headers with PT_NOTE type pointed to by
746 * @ehdr_ptr has already been updated by update_note_header_size_elf64
747 * and each of PT_NOTE program headers has actual ELF note segment
748 * size in its p_memsz member.
750 static int __init get_note_number_and_size_elf64(const Elf64_Ehdr *ehdr_ptr,
751 int *nr_ptnote, u64 *sz_ptnote)
754 Elf64_Phdr *phdr_ptr;
756 *nr_ptnote = *sz_ptnote = 0;
758 phdr_ptr = (Elf64_Phdr *)(ehdr_ptr + 1);
759 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
760 if (phdr_ptr->p_type != PT_NOTE)
763 *sz_ptnote += phdr_ptr->p_memsz;
770 * copy_notes_elf64 - copy ELF note segments in a given buffer
772 * @ehdr_ptr: ELF header
773 * @notes_buf: buffer into which ELF note segments are copied
775 * This function is used to copy ELF note segment in the 1st kernel
776 * into the buffer @notes_buf in the 2nd kernel. It is assumed that
777 * size of the buffer @notes_buf is equal to or larger than sum of the
778 * real ELF note segment headers and data.
780 * It is assumed that program headers with PT_NOTE type pointed to by
781 * @ehdr_ptr has already been updated by update_note_header_size_elf64
782 * and each of PT_NOTE program headers has actual ELF note segment
783 * size in its p_memsz member.
785 static int __init copy_notes_elf64(const Elf64_Ehdr *ehdr_ptr, char *notes_buf)
788 Elf64_Phdr *phdr_ptr;
790 phdr_ptr = (Elf64_Phdr*)(ehdr_ptr + 1);
792 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
794 if (phdr_ptr->p_type != PT_NOTE)
796 offset = phdr_ptr->p_offset;
797 rc = elfcorehdr_read_notes(notes_buf, phdr_ptr->p_memsz,
801 notes_buf += phdr_ptr->p_memsz;
807 /* Merges all the PT_NOTE headers into one. */
808 static int __init merge_note_headers_elf64(char *elfptr, size_t *elfsz,
809 char **notes_buf, size_t *notes_sz)
811 int i, nr_ptnote=0, rc=0;
813 Elf64_Ehdr *ehdr_ptr;
815 u64 phdr_sz = 0, note_off;
817 ehdr_ptr = (Elf64_Ehdr *)elfptr;
819 rc = update_note_header_size_elf64(ehdr_ptr);
823 rc = get_note_number_and_size_elf64(ehdr_ptr, &nr_ptnote, &phdr_sz);
827 *notes_sz = roundup(phdr_sz, PAGE_SIZE);
828 *notes_buf = vmcore_alloc_buf(*notes_sz);
832 rc = copy_notes_elf64(ehdr_ptr, *notes_buf);
836 /* Prepare merged PT_NOTE program header. */
837 phdr.p_type = PT_NOTE;
839 note_off = sizeof(Elf64_Ehdr) +
840 (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf64_Phdr);
841 phdr.p_offset = roundup(note_off, PAGE_SIZE);
842 phdr.p_vaddr = phdr.p_paddr = 0;
843 phdr.p_filesz = phdr.p_memsz = phdr_sz;
846 /* Add merged PT_NOTE program header*/
847 tmp = elfptr + sizeof(Elf64_Ehdr);
848 memcpy(tmp, &phdr, sizeof(phdr));
851 /* Remove unwanted PT_NOTE program headers. */
852 i = (nr_ptnote - 1) * sizeof(Elf64_Phdr);
854 memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf64_Ehdr)-sizeof(Elf64_Phdr)));
855 memset(elfptr + *elfsz, 0, i);
856 *elfsz = roundup(*elfsz, PAGE_SIZE);
858 /* Modify e_phnum to reflect merged headers. */
859 ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
861 /* Store the size of all notes. We need this to update the note
862 * header when the device dumps will be added.
864 elfnotes_orig_sz = phdr.p_memsz;
870 * update_note_header_size_elf32 - update p_memsz member of each PT_NOTE entry
872 * @ehdr_ptr: ELF header
874 * This function updates p_memsz member of each PT_NOTE entry in the
875 * program header table pointed to by @ehdr_ptr to real size of ELF
878 static int __init update_note_header_size_elf32(const Elf32_Ehdr *ehdr_ptr)
881 Elf32_Phdr *phdr_ptr;
882 Elf32_Nhdr *nhdr_ptr;
884 phdr_ptr = (Elf32_Phdr *)(ehdr_ptr + 1);
885 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
887 u64 offset, max_sz, sz, real_sz = 0;
888 if (phdr_ptr->p_type != PT_NOTE)
890 max_sz = phdr_ptr->p_memsz;
891 offset = phdr_ptr->p_offset;
892 notes_section = kmalloc(max_sz, GFP_KERNEL);
895 rc = elfcorehdr_read_notes(notes_section, max_sz, &offset);
897 kfree(notes_section);
900 nhdr_ptr = notes_section;
901 while (nhdr_ptr->n_namesz != 0) {
902 sz = sizeof(Elf32_Nhdr) +
903 (((u64)nhdr_ptr->n_namesz + 3) & ~3) +
904 (((u64)nhdr_ptr->n_descsz + 3) & ~3);
905 if ((real_sz + sz) > max_sz) {
906 pr_warn("Warning: Exceeded p_memsz, dropping PT_NOTE entry n_namesz=0x%x, n_descsz=0x%x\n",
907 nhdr_ptr->n_namesz, nhdr_ptr->n_descsz);
911 nhdr_ptr = (Elf32_Nhdr*)((char*)nhdr_ptr + sz);
913 kfree(notes_section);
914 phdr_ptr->p_memsz = real_sz;
916 pr_warn("Warning: Zero PT_NOTE entries found\n");
924 * get_note_number_and_size_elf32 - get the number of PT_NOTE program
925 * headers and sum of real size of their ELF note segment headers and
928 * @ehdr_ptr: ELF header
929 * @nr_ptnote: buffer for the number of PT_NOTE program headers
930 * @sz_ptnote: buffer for size of unique PT_NOTE program header
932 * This function is used to merge multiple PT_NOTE program headers
933 * into a unique single one. The resulting unique entry will have
934 * @sz_ptnote in its phdr->p_mem.
936 * It is assumed that program headers with PT_NOTE type pointed to by
937 * @ehdr_ptr has already been updated by update_note_header_size_elf32
938 * and each of PT_NOTE program headers has actual ELF note segment
939 * size in its p_memsz member.
941 static int __init get_note_number_and_size_elf32(const Elf32_Ehdr *ehdr_ptr,
942 int *nr_ptnote, u64 *sz_ptnote)
945 Elf32_Phdr *phdr_ptr;
947 *nr_ptnote = *sz_ptnote = 0;
949 phdr_ptr = (Elf32_Phdr *)(ehdr_ptr + 1);
950 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
951 if (phdr_ptr->p_type != PT_NOTE)
954 *sz_ptnote += phdr_ptr->p_memsz;
961 * copy_notes_elf32 - copy ELF note segments in a given buffer
963 * @ehdr_ptr: ELF header
964 * @notes_buf: buffer into which ELF note segments are copied
966 * This function is used to copy ELF note segment in the 1st kernel
967 * into the buffer @notes_buf in the 2nd kernel. It is assumed that
968 * size of the buffer @notes_buf is equal to or larger than sum of the
969 * real ELF note segment headers and data.
971 * It is assumed that program headers with PT_NOTE type pointed to by
972 * @ehdr_ptr has already been updated by update_note_header_size_elf32
973 * and each of PT_NOTE program headers has actual ELF note segment
974 * size in its p_memsz member.
976 static int __init copy_notes_elf32(const Elf32_Ehdr *ehdr_ptr, char *notes_buf)
979 Elf32_Phdr *phdr_ptr;
981 phdr_ptr = (Elf32_Phdr*)(ehdr_ptr + 1);
983 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
985 if (phdr_ptr->p_type != PT_NOTE)
987 offset = phdr_ptr->p_offset;
988 rc = elfcorehdr_read_notes(notes_buf, phdr_ptr->p_memsz,
992 notes_buf += phdr_ptr->p_memsz;
998 /* Merges all the PT_NOTE headers into one. */
999 static int __init merge_note_headers_elf32(char *elfptr, size_t *elfsz,
1000 char **notes_buf, size_t *notes_sz)
1002 int i, nr_ptnote=0, rc=0;
1004 Elf32_Ehdr *ehdr_ptr;
1006 u64 phdr_sz = 0, note_off;
1008 ehdr_ptr = (Elf32_Ehdr *)elfptr;
1010 rc = update_note_header_size_elf32(ehdr_ptr);
1014 rc = get_note_number_and_size_elf32(ehdr_ptr, &nr_ptnote, &phdr_sz);
1018 *notes_sz = roundup(phdr_sz, PAGE_SIZE);
1019 *notes_buf = vmcore_alloc_buf(*notes_sz);
1023 rc = copy_notes_elf32(ehdr_ptr, *notes_buf);
1027 /* Prepare merged PT_NOTE program header. */
1028 phdr.p_type = PT_NOTE;
1030 note_off = sizeof(Elf32_Ehdr) +
1031 (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf32_Phdr);
1032 phdr.p_offset = roundup(note_off, PAGE_SIZE);
1033 phdr.p_vaddr = phdr.p_paddr = 0;
1034 phdr.p_filesz = phdr.p_memsz = phdr_sz;
1037 /* Add merged PT_NOTE program header*/
1038 tmp = elfptr + sizeof(Elf32_Ehdr);
1039 memcpy(tmp, &phdr, sizeof(phdr));
1040 tmp += sizeof(phdr);
1042 /* Remove unwanted PT_NOTE program headers. */
1043 i = (nr_ptnote - 1) * sizeof(Elf32_Phdr);
1044 *elfsz = *elfsz - i;
1045 memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf32_Ehdr)-sizeof(Elf32_Phdr)));
1046 memset(elfptr + *elfsz, 0, i);
1047 *elfsz = roundup(*elfsz, PAGE_SIZE);
1049 /* Modify e_phnum to reflect merged headers. */
1050 ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
1052 /* Store the size of all notes. We need this to update the note
1053 * header when the device dumps will be added.
1055 elfnotes_orig_sz = phdr.p_memsz;
1060 /* Add memory chunks represented by program headers to vmcore list. Also update
1061 * the new offset fields of exported program headers. */
1062 static int __init process_ptload_program_headers_elf64(char *elfptr,
1065 struct list_head *vc_list)
1068 Elf64_Ehdr *ehdr_ptr;
1069 Elf64_Phdr *phdr_ptr;
1073 ehdr_ptr = (Elf64_Ehdr *)elfptr;
1074 phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr)); /* PT_NOTE hdr */
1076 /* Skip Elf header, program headers and Elf note segment. */
1077 vmcore_off = elfsz + elfnotes_sz;
1079 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
1080 u64 paddr, start, end, size;
1082 if (phdr_ptr->p_type != PT_LOAD)
1085 paddr = phdr_ptr->p_offset;
1086 start = rounddown(paddr, PAGE_SIZE);
1087 end = roundup(paddr + phdr_ptr->p_memsz, PAGE_SIZE);
1090 /* Add this contiguous chunk of memory to vmcore list.*/
1091 new = get_new_element();
1096 list_add_tail(&new->list, vc_list);
1098 /* Update the program header offset. */
1099 phdr_ptr->p_offset = vmcore_off + (paddr - start);
1100 vmcore_off = vmcore_off + size;
1105 static int __init process_ptload_program_headers_elf32(char *elfptr,
1108 struct list_head *vc_list)
1111 Elf32_Ehdr *ehdr_ptr;
1112 Elf32_Phdr *phdr_ptr;
1116 ehdr_ptr = (Elf32_Ehdr *)elfptr;
1117 phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr)); /* PT_NOTE hdr */
1119 /* Skip Elf header, program headers and Elf note segment. */
1120 vmcore_off = elfsz + elfnotes_sz;
1122 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
1123 u64 paddr, start, end, size;
1125 if (phdr_ptr->p_type != PT_LOAD)
1128 paddr = phdr_ptr->p_offset;
1129 start = rounddown(paddr, PAGE_SIZE);
1130 end = roundup(paddr + phdr_ptr->p_memsz, PAGE_SIZE);
1133 /* Add this contiguous chunk of memory to vmcore list.*/
1134 new = get_new_element();
1139 list_add_tail(&new->list, vc_list);
1141 /* Update the program header offset */
1142 phdr_ptr->p_offset = vmcore_off + (paddr - start);
1143 vmcore_off = vmcore_off + size;
1148 /* Sets offset fields of vmcore elements. */
1149 static void set_vmcore_list_offsets(size_t elfsz, size_t elfnotes_sz,
1150 struct list_head *vc_list)
1155 /* Skip Elf header, program headers and Elf note segment. */
1156 vmcore_off = elfsz + elfnotes_sz;
1158 list_for_each_entry(m, vc_list, list) {
1159 m->offset = vmcore_off;
1160 vmcore_off += m->size;
1164 static void free_elfcorebuf(void)
1166 free_pages((unsigned long)elfcorebuf, get_order(elfcorebuf_sz_orig));
1168 vfree(elfnotes_buf);
1169 elfnotes_buf = NULL;
1172 static int __init parse_crash_elf64_headers(void)
1178 addr = elfcorehdr_addr;
1180 /* Read Elf header */
1181 rc = elfcorehdr_read((char *)&ehdr, sizeof(Elf64_Ehdr), &addr);
1185 /* Do some basic Verification. */
1186 if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
1187 (ehdr.e_type != ET_CORE) ||
1188 !vmcore_elf64_check_arch(&ehdr) ||
1189 ehdr.e_ident[EI_CLASS] != ELFCLASS64 ||
1190 ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
1191 ehdr.e_version != EV_CURRENT ||
1192 ehdr.e_ehsize != sizeof(Elf64_Ehdr) ||
1193 ehdr.e_phentsize != sizeof(Elf64_Phdr) ||
1194 ehdr.e_phnum == 0) {
1195 pr_warn("Warning: Core image elf header is not sane\n");
1199 /* Read in all elf headers. */
1200 elfcorebuf_sz_orig = sizeof(Elf64_Ehdr) +
1201 ehdr.e_phnum * sizeof(Elf64_Phdr);
1202 elfcorebuf_sz = elfcorebuf_sz_orig;
1203 elfcorebuf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
1204 get_order(elfcorebuf_sz_orig));
1207 addr = elfcorehdr_addr;
1208 rc = elfcorehdr_read(elfcorebuf, elfcorebuf_sz_orig, &addr);
1212 /* Merge all PT_NOTE headers into one. */
1213 rc = merge_note_headers_elf64(elfcorebuf, &elfcorebuf_sz,
1214 &elfnotes_buf, &elfnotes_sz);
1217 rc = process_ptload_program_headers_elf64(elfcorebuf, elfcorebuf_sz,
1218 elfnotes_sz, &vmcore_list);
1221 set_vmcore_list_offsets(elfcorebuf_sz, elfnotes_sz, &vmcore_list);
1228 static int __init parse_crash_elf32_headers(void)
1234 addr = elfcorehdr_addr;
1236 /* Read Elf header */
1237 rc = elfcorehdr_read((char *)&ehdr, sizeof(Elf32_Ehdr), &addr);
1241 /* Do some basic Verification. */
1242 if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
1243 (ehdr.e_type != ET_CORE) ||
1244 !vmcore_elf32_check_arch(&ehdr) ||
1245 ehdr.e_ident[EI_CLASS] != ELFCLASS32||
1246 ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
1247 ehdr.e_version != EV_CURRENT ||
1248 ehdr.e_ehsize != sizeof(Elf32_Ehdr) ||
1249 ehdr.e_phentsize != sizeof(Elf32_Phdr) ||
1250 ehdr.e_phnum == 0) {
1251 pr_warn("Warning: Core image elf header is not sane\n");
1255 /* Read in all elf headers. */
1256 elfcorebuf_sz_orig = sizeof(Elf32_Ehdr) + ehdr.e_phnum * sizeof(Elf32_Phdr);
1257 elfcorebuf_sz = elfcorebuf_sz_orig;
1258 elfcorebuf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
1259 get_order(elfcorebuf_sz_orig));
1262 addr = elfcorehdr_addr;
1263 rc = elfcorehdr_read(elfcorebuf, elfcorebuf_sz_orig, &addr);
1267 /* Merge all PT_NOTE headers into one. */
1268 rc = merge_note_headers_elf32(elfcorebuf, &elfcorebuf_sz,
1269 &elfnotes_buf, &elfnotes_sz);
1272 rc = process_ptload_program_headers_elf32(elfcorebuf, elfcorebuf_sz,
1273 elfnotes_sz, &vmcore_list);
1276 set_vmcore_list_offsets(elfcorebuf_sz, elfnotes_sz, &vmcore_list);
1283 static int __init parse_crash_elf_headers(void)
1285 unsigned char e_ident[EI_NIDENT];
1289 addr = elfcorehdr_addr;
1290 rc = elfcorehdr_read(e_ident, EI_NIDENT, &addr);
1293 if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) {
1294 pr_warn("Warning: Core image elf header not found\n");
1298 if (e_ident[EI_CLASS] == ELFCLASS64) {
1299 rc = parse_crash_elf64_headers();
1302 } else if (e_ident[EI_CLASS] == ELFCLASS32) {
1303 rc = parse_crash_elf32_headers();
1307 pr_warn("Warning: Core image elf header is not sane\n");
1311 /* Determine vmcore size. */
1312 vmcore_size = get_vmcore_size(elfcorebuf_sz, elfnotes_sz,
1318 #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
1320 * vmcoredd_write_header - Write vmcore device dump header at the
1321 * beginning of the dump's buffer.
1322 * @buf: Output buffer where the note is written
1324 * @size: Size of the dump
1326 * Fills beginning of the dump's buffer with vmcore device dump header.
1328 static void vmcoredd_write_header(void *buf, struct vmcoredd_data *data,
1331 struct vmcoredd_header *vdd_hdr = (struct vmcoredd_header *)buf;
1333 vdd_hdr->n_namesz = sizeof(vdd_hdr->name);
1334 vdd_hdr->n_descsz = size + sizeof(vdd_hdr->dump_name);
1335 vdd_hdr->n_type = NT_VMCOREDD;
1337 strncpy((char *)vdd_hdr->name, VMCOREDD_NOTE_NAME,
1338 sizeof(vdd_hdr->name));
1339 memcpy(vdd_hdr->dump_name, data->dump_name, sizeof(vdd_hdr->dump_name));
1343 * vmcoredd_update_program_headers - Update all Elf program headers
1344 * @elfptr: Pointer to elf header
1345 * @elfnotesz: Size of elf notes aligned to page size
1346 * @vmcoreddsz: Size of device dumps to be added to elf note header
1348 * Determine type of Elf header (Elf64 or Elf32) and update the elf note size.
1349 * Also update the offsets of all the program headers after the elf note header.
1351 static void vmcoredd_update_program_headers(char *elfptr, size_t elfnotesz,
1354 unsigned char *e_ident = (unsigned char *)elfptr;
1355 u64 start, end, size;
1359 vmcore_off = elfcorebuf_sz + elfnotesz;
1361 if (e_ident[EI_CLASS] == ELFCLASS64) {
1362 Elf64_Ehdr *ehdr = (Elf64_Ehdr *)elfptr;
1363 Elf64_Phdr *phdr = (Elf64_Phdr *)(elfptr + sizeof(Elf64_Ehdr));
1365 /* Update all program headers */
1366 for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
1367 if (phdr->p_type == PT_NOTE) {
1368 /* Update note size */
1369 phdr->p_memsz = elfnotes_orig_sz + vmcoreddsz;
1370 phdr->p_filesz = phdr->p_memsz;
1374 start = rounddown(phdr->p_offset, PAGE_SIZE);
1375 end = roundup(phdr->p_offset + phdr->p_memsz,
1378 phdr->p_offset = vmcore_off + (phdr->p_offset - start);
1382 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)elfptr;
1383 Elf32_Phdr *phdr = (Elf32_Phdr *)(elfptr + sizeof(Elf32_Ehdr));
1385 /* Update all program headers */
1386 for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
1387 if (phdr->p_type == PT_NOTE) {
1388 /* Update note size */
1389 phdr->p_memsz = elfnotes_orig_sz + vmcoreddsz;
1390 phdr->p_filesz = phdr->p_memsz;
1394 start = rounddown(phdr->p_offset, PAGE_SIZE);
1395 end = roundup(phdr->p_offset + phdr->p_memsz,
1398 phdr->p_offset = vmcore_off + (phdr->p_offset - start);
1405 * vmcoredd_update_size - Update the total size of the device dumps and update
1407 * @dump_size: Size of the current device dump to be added to total size
1409 * Update the total size of all the device dumps and update the Elf program
1410 * headers. Calculate the new offsets for the vmcore list and update the
1411 * total vmcore size.
1413 static void vmcoredd_update_size(size_t dump_size)
1415 vmcoredd_orig_sz += dump_size;
1416 elfnotes_sz = roundup(elfnotes_orig_sz, PAGE_SIZE) + vmcoredd_orig_sz;
1417 vmcoredd_update_program_headers(elfcorebuf, elfnotes_sz,
1420 /* Update vmcore list offsets */
1421 set_vmcore_list_offsets(elfcorebuf_sz, elfnotes_sz, &vmcore_list);
1423 vmcore_size = get_vmcore_size(elfcorebuf_sz, elfnotes_sz,
1425 proc_vmcore->size = vmcore_size;
1429 * vmcore_add_device_dump - Add a buffer containing device dump to vmcore
1432 * Allocate a buffer and invoke the calling driver's dump collect routine.
1433 * Write Elf note at the beginning of the buffer to indicate vmcore device
1434 * dump and add the dump to global list.
1436 int vmcore_add_device_dump(struct vmcoredd_data *data)
1438 struct vmcoredd_node *dump;
1443 if (!data || !strlen(data->dump_name) ||
1444 !data->vmcoredd_callback || !data->size)
1447 dump = vzalloc(sizeof(*dump));
1453 /* Keep size of the buffer page aligned so that it can be mmaped */
1454 data_size = roundup(sizeof(struct vmcoredd_header) + data->size,
1457 /* Allocate buffer for driver's to write their dumps */
1458 buf = vmcore_alloc_buf(data_size);
1464 vmcoredd_write_header(buf, data, data_size -
1465 sizeof(struct vmcoredd_header));
1467 /* Invoke the driver's dump collection routing */
1468 ret = data->vmcoredd_callback(data, buf +
1469 sizeof(struct vmcoredd_header));
1474 dump->size = data_size;
1476 /* Add the dump to driver sysfs list */
1477 mutex_lock(&vmcoredd_mutex);
1478 list_add_tail(&dump->list, &vmcoredd_list);
1479 mutex_unlock(&vmcoredd_mutex);
1481 vmcoredd_update_size(data_size);
1493 EXPORT_SYMBOL(vmcore_add_device_dump);
1494 #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
1496 /* Free all dumps in vmcore device dump list */
1497 static void vmcore_free_device_dumps(void)
1499 #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
1500 mutex_lock(&vmcoredd_mutex);
1501 while (!list_empty(&vmcoredd_list)) {
1502 struct vmcoredd_node *dump;
1504 dump = list_first_entry(&vmcoredd_list, struct vmcoredd_node,
1506 list_del(&dump->list);
1510 mutex_unlock(&vmcoredd_mutex);
1511 #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
1514 /* Init function for vmcore module. */
1515 static int __init vmcore_init(void)
1519 /* Allow architectures to allocate ELF header in 2nd kernel */
1520 rc = elfcorehdr_alloc(&elfcorehdr_addr, &elfcorehdr_size);
1524 * If elfcorehdr= has been passed in cmdline or created in 2nd kernel,
1525 * then capture the dump.
1527 if (!(is_vmcore_usable()))
1529 rc = parse_crash_elf_headers();
1531 pr_warn("Kdump: vmcore not initialized\n");
1534 elfcorehdr_free(elfcorehdr_addr);
1535 elfcorehdr_addr = ELFCORE_ADDR_ERR;
1537 proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations);
1539 proc_vmcore->size = vmcore_size;
1542 fs_initcall(vmcore_init);
1544 /* Cleanup function for vmcore module. */
1545 void vmcore_cleanup(void)
1548 proc_remove(proc_vmcore);
1552 /* clear the vmcore list. */
1553 while (!list_empty(&vmcore_list)) {
1556 m = list_first_entry(&vmcore_list, struct vmcore, list);
1562 /* clear vmcore device dump list */
1563 vmcore_free_device_dumps();