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/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 * Copy to either kernel or user space
182 static int copy_to(void *target, void *src, size_t size, int userbuf)
185 if (copy_to_user((char __user *) target, src, size))
188 memcpy(target, src, size);
193 #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
194 static int vmcoredd_copy_dumps(void *dst, u64 start, size_t size, int userbuf)
196 struct vmcoredd_node *dump;
202 mutex_lock(&vmcoredd_mutex);
203 list_for_each_entry(dump, &vmcoredd_list, list) {
204 if (start < offset + dump->size) {
205 tsz = min(offset + (u64)dump->size - start, (u64)size);
206 buf = dump->buf + start - offset;
207 if (copy_to(dst, buf, tsz, userbuf)) {
216 /* Leave now if buffer filled already */
220 offset += dump->size;
224 mutex_unlock(&vmcoredd_mutex);
229 static int vmcoredd_mmap_dumps(struct vm_area_struct *vma, unsigned long dst,
230 u64 start, size_t size)
232 struct vmcoredd_node *dump;
238 mutex_lock(&vmcoredd_mutex);
239 list_for_each_entry(dump, &vmcoredd_list, list) {
240 if (start < offset + dump->size) {
241 tsz = min(offset + (u64)dump->size - start, (u64)size);
242 buf = dump->buf + start - offset;
243 if (remap_vmalloc_range_partial(vma, dst, buf, tsz)) {
252 /* Leave now if buffer filled already */
256 offset += dump->size;
260 mutex_unlock(&vmcoredd_mutex);
263 #endif /* CONFIG_MMU */
264 #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
266 /* Read from the ELF header and then the crash dump. On error, negative value is
267 * returned otherwise number of bytes read are returned.
269 static ssize_t __read_vmcore(char *buffer, size_t buflen, loff_t *fpos,
272 ssize_t acc = 0, tmp;
275 struct vmcore *m = NULL;
277 if (buflen == 0 || *fpos >= vmcore_size)
280 /* trim buflen to not go beyond EOF */
281 if (buflen > vmcore_size - *fpos)
282 buflen = vmcore_size - *fpos;
284 /* Read ELF core header */
285 if (*fpos < elfcorebuf_sz) {
286 tsz = min(elfcorebuf_sz - (size_t)*fpos, buflen);
287 if (copy_to(buffer, elfcorebuf + *fpos, tsz, userbuf))
294 /* leave now if filled buffer already */
299 /* Read Elf note segment */
300 if (*fpos < elfcorebuf_sz + elfnotes_sz) {
303 /* We add device dumps before other elf notes because the
304 * other elf notes may not fill the elf notes buffer
305 * completely and we will end up with zero-filled data
306 * between the elf notes and the device dumps. Tools will
307 * then try to decode this zero-filled data as valid notes
308 * and we don't want that. Hence, adding device dumps before
309 * the other elf notes ensure that zero-filled data can be
312 #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
313 /* Read device dumps */
314 if (*fpos < elfcorebuf_sz + vmcoredd_orig_sz) {
315 tsz = min(elfcorebuf_sz + vmcoredd_orig_sz -
316 (size_t)*fpos, buflen);
317 start = *fpos - elfcorebuf_sz;
318 if (vmcoredd_copy_dumps(buffer, start, tsz, userbuf))
326 /* leave now if filled buffer already */
330 #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
332 /* Read remaining elf notes */
333 tsz = min(elfcorebuf_sz + elfnotes_sz - (size_t)*fpos, buflen);
334 kaddr = elfnotes_buf + *fpos - elfcorebuf_sz - vmcoredd_orig_sz;
335 if (copy_to(buffer, kaddr, tsz, userbuf))
343 /* leave now if filled buffer already */
348 list_for_each_entry(m, &vmcore_list, list) {
349 if (*fpos < m->offset + m->size) {
350 tsz = (size_t)min_t(unsigned long long,
351 m->offset + m->size - *fpos,
353 start = m->paddr + *fpos - m->offset;
354 tmp = read_from_oldmem(buffer, tsz, &start, userbuf);
362 /* leave now if filled buffer already */
371 static ssize_t read_vmcore(struct file *file, char __user *buffer,
372 size_t buflen, loff_t *fpos)
374 return __read_vmcore((__force char *) buffer, buflen, fpos, 1);
378 * The vmcore fault handler uses the page cache and fills data using the
379 * standard __vmcore_read() function.
381 * On s390 the fault handler is used for memory regions that can't be mapped
382 * directly with remap_pfn_range().
384 static vm_fault_t mmap_vmcore_fault(struct vm_fault *vmf)
387 struct address_space *mapping = vmf->vma->vm_file->f_mapping;
388 pgoff_t index = vmf->pgoff;
394 page = find_or_create_page(mapping, index, GFP_KERNEL);
397 if (!PageUptodate(page)) {
398 offset = (loff_t) index << PAGE_SHIFT;
399 buf = __va((page_to_pfn(page) << PAGE_SHIFT));
400 rc = __read_vmcore(buf, PAGE_SIZE, &offset, 0);
404 return (rc == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
406 SetPageUptodate(page);
412 return VM_FAULT_SIGBUS;
416 static const struct vm_operations_struct vmcore_mmap_ops = {
417 .fault = mmap_vmcore_fault,
421 * vmcore_alloc_buf - allocate buffer in vmalloc memory
422 * @sizez: size of buffer
424 * If CONFIG_MMU is defined, use vmalloc_user() to allow users to mmap
425 * the buffer to user-space by means of remap_vmalloc_range().
427 * If CONFIG_MMU is not defined, use vzalloc() since mmap_vmcore() is
428 * disabled and there's no need to allow users to mmap the buffer.
430 static inline char *vmcore_alloc_buf(size_t size)
433 return vmalloc_user(size);
435 return vzalloc(size);
440 * Disable mmap_vmcore() if CONFIG_MMU is not defined. MMU is
441 * essential for mmap_vmcore() in order to map physically
442 * non-contiguous objects (ELF header, ELF note segment and memory
443 * regions in the 1st kernel pointed to by PT_LOAD entries) into
444 * virtually contiguous user-space in ELF layout.
448 * remap_oldmem_pfn_checked - do remap_oldmem_pfn_range replacing all pages
449 * reported as not being ram with the zero page.
451 * @vma: vm_area_struct describing requested mapping
452 * @from: start remapping from
453 * @pfn: page frame number to start remapping to
454 * @size: remapping size
455 * @prot: protection bits
457 * Returns zero on success, -EAGAIN on failure.
459 static int remap_oldmem_pfn_checked(struct vm_area_struct *vma,
460 unsigned long from, unsigned long pfn,
461 unsigned long size, pgprot_t prot)
463 unsigned long map_size;
464 unsigned long pos_start, pos_end, pos;
465 unsigned long zeropage_pfn = my_zero_pfn(0);
469 pos_end = pfn + (size >> PAGE_SHIFT);
471 for (pos = pos_start; pos < pos_end; ++pos) {
472 if (!pfn_is_ram(pos)) {
474 * We hit a page which is not ram. Remap the continuous
475 * region between pos_start and pos-1 and replace
476 * the non-ram page at pos with the zero page.
478 if (pos > pos_start) {
479 /* Remap continuous region */
480 map_size = (pos - pos_start) << PAGE_SHIFT;
481 if (remap_oldmem_pfn_range(vma, from + len,
487 /* Remap the zero page */
488 if (remap_oldmem_pfn_range(vma, from + len,
496 if (pos > pos_start) {
498 map_size = (pos - pos_start) << PAGE_SHIFT;
499 if (remap_oldmem_pfn_range(vma, from + len, pos_start,
505 do_munmap(vma->vm_mm, from, len, NULL);
509 static int vmcore_remap_oldmem_pfn(struct vm_area_struct *vma,
510 unsigned long from, unsigned long pfn,
511 unsigned long size, pgprot_t prot)
514 * Check if oldmem_pfn_is_ram was registered to avoid
515 * looping over all pages without a reason.
517 if (oldmem_pfn_is_ram)
518 return remap_oldmem_pfn_checked(vma, from, pfn, size, prot);
520 return remap_oldmem_pfn_range(vma, from, pfn, size, prot);
523 static int mmap_vmcore(struct file *file, struct vm_area_struct *vma)
525 size_t size = vma->vm_end - vma->vm_start;
526 u64 start, end, len, tsz;
529 start = (u64)vma->vm_pgoff << PAGE_SHIFT;
532 if (size > vmcore_size || end > vmcore_size)
535 if (vma->vm_flags & (VM_WRITE | VM_EXEC))
538 vma->vm_flags &= ~(VM_MAYWRITE | VM_MAYEXEC);
539 vma->vm_flags |= VM_MIXEDMAP;
540 vma->vm_ops = &vmcore_mmap_ops;
544 if (start < elfcorebuf_sz) {
547 tsz = min(elfcorebuf_sz - (size_t)start, size);
548 pfn = __pa(elfcorebuf + start) >> PAGE_SHIFT;
549 if (remap_pfn_range(vma, vma->vm_start, pfn, tsz,
560 if (start < elfcorebuf_sz + elfnotes_sz) {
563 /* We add device dumps before other elf notes because the
564 * other elf notes may not fill the elf notes buffer
565 * completely and we will end up with zero-filled data
566 * between the elf notes and the device dumps. Tools will
567 * then try to decode this zero-filled data as valid notes
568 * and we don't want that. Hence, adding device dumps before
569 * the other elf notes ensure that zero-filled data can be
570 * avoided. This also ensures that the device dumps and
571 * other elf notes can be properly mmaped at page aligned
574 #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
575 /* Read device dumps */
576 if (start < elfcorebuf_sz + vmcoredd_orig_sz) {
579 tsz = min(elfcorebuf_sz + vmcoredd_orig_sz -
580 (size_t)start, size);
581 start_off = start - elfcorebuf_sz;
582 if (vmcoredd_mmap_dumps(vma, vma->vm_start + len,
590 /* leave now if filled buffer already */
594 #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
596 /* Read remaining elf notes */
597 tsz = min(elfcorebuf_sz + elfnotes_sz - (size_t)start, size);
598 kaddr = elfnotes_buf + start - elfcorebuf_sz - vmcoredd_orig_sz;
599 if (remap_vmalloc_range_partial(vma, vma->vm_start + len,
611 list_for_each_entry(m, &vmcore_list, list) {
612 if (start < m->offset + m->size) {
615 tsz = (size_t)min_t(unsigned long long,
616 m->offset + m->size - start, size);
617 paddr = m->paddr + start - m->offset;
618 if (vmcore_remap_oldmem_pfn(vma, vma->vm_start + len,
619 paddr >> PAGE_SHIFT, tsz,
633 do_munmap(vma->vm_mm, vma->vm_start, len, NULL);
637 static int mmap_vmcore(struct file *file, struct vm_area_struct *vma)
643 static const struct file_operations proc_vmcore_operations = {
645 .llseek = default_llseek,
649 static struct vmcore* __init get_new_element(void)
651 return kzalloc(sizeof(struct vmcore), GFP_KERNEL);
654 static u64 get_vmcore_size(size_t elfsz, size_t elfnotesegsz,
655 struct list_head *vc_list)
660 size = elfsz + elfnotesegsz;
661 list_for_each_entry(m, vc_list, list) {
668 * update_note_header_size_elf64 - update p_memsz member of each PT_NOTE entry
670 * @ehdr_ptr: ELF header
672 * This function updates p_memsz member of each PT_NOTE entry in the
673 * program header table pointed to by @ehdr_ptr to real size of ELF
676 static int __init update_note_header_size_elf64(const Elf64_Ehdr *ehdr_ptr)
679 Elf64_Phdr *phdr_ptr;
680 Elf64_Nhdr *nhdr_ptr;
682 phdr_ptr = (Elf64_Phdr *)(ehdr_ptr + 1);
683 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
685 u64 offset, max_sz, sz, real_sz = 0;
686 if (phdr_ptr->p_type != PT_NOTE)
688 max_sz = phdr_ptr->p_memsz;
689 offset = phdr_ptr->p_offset;
690 notes_section = kmalloc(max_sz, GFP_KERNEL);
693 rc = elfcorehdr_read_notes(notes_section, max_sz, &offset);
695 kfree(notes_section);
698 nhdr_ptr = notes_section;
699 while (nhdr_ptr->n_namesz != 0) {
700 sz = sizeof(Elf64_Nhdr) +
701 (((u64)nhdr_ptr->n_namesz + 3) & ~3) +
702 (((u64)nhdr_ptr->n_descsz + 3) & ~3);
703 if ((real_sz + sz) > max_sz) {
704 pr_warn("Warning: Exceeded p_memsz, dropping PT_NOTE entry n_namesz=0x%x, n_descsz=0x%x\n",
705 nhdr_ptr->n_namesz, nhdr_ptr->n_descsz);
709 nhdr_ptr = (Elf64_Nhdr*)((char*)nhdr_ptr + sz);
711 kfree(notes_section);
712 phdr_ptr->p_memsz = real_sz;
714 pr_warn("Warning: Zero PT_NOTE entries found\n");
722 * get_note_number_and_size_elf64 - get the number of PT_NOTE program
723 * headers and sum of real size of their ELF note segment headers and
726 * @ehdr_ptr: ELF header
727 * @nr_ptnote: buffer for the number of PT_NOTE program headers
728 * @sz_ptnote: buffer for size of unique PT_NOTE program header
730 * This function is used to merge multiple PT_NOTE program headers
731 * into a unique single one. The resulting unique entry will have
732 * @sz_ptnote in its phdr->p_mem.
734 * It is assumed that program headers with PT_NOTE type pointed to by
735 * @ehdr_ptr has already been updated by update_note_header_size_elf64
736 * and each of PT_NOTE program headers has actual ELF note segment
737 * size in its p_memsz member.
739 static int __init get_note_number_and_size_elf64(const Elf64_Ehdr *ehdr_ptr,
740 int *nr_ptnote, u64 *sz_ptnote)
743 Elf64_Phdr *phdr_ptr;
745 *nr_ptnote = *sz_ptnote = 0;
747 phdr_ptr = (Elf64_Phdr *)(ehdr_ptr + 1);
748 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
749 if (phdr_ptr->p_type != PT_NOTE)
752 *sz_ptnote += phdr_ptr->p_memsz;
759 * copy_notes_elf64 - copy ELF note segments in a given buffer
761 * @ehdr_ptr: ELF header
762 * @notes_buf: buffer into which ELF note segments are copied
764 * This function is used to copy ELF note segment in the 1st kernel
765 * into the buffer @notes_buf in the 2nd kernel. It is assumed that
766 * size of the buffer @notes_buf is equal to or larger than sum of the
767 * real ELF note segment headers and data.
769 * It is assumed that program headers with PT_NOTE type pointed to by
770 * @ehdr_ptr has already been updated by update_note_header_size_elf64
771 * and each of PT_NOTE program headers has actual ELF note segment
772 * size in its p_memsz member.
774 static int __init copy_notes_elf64(const Elf64_Ehdr *ehdr_ptr, char *notes_buf)
777 Elf64_Phdr *phdr_ptr;
779 phdr_ptr = (Elf64_Phdr*)(ehdr_ptr + 1);
781 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
783 if (phdr_ptr->p_type != PT_NOTE)
785 offset = phdr_ptr->p_offset;
786 rc = elfcorehdr_read_notes(notes_buf, phdr_ptr->p_memsz,
790 notes_buf += phdr_ptr->p_memsz;
796 /* Merges all the PT_NOTE headers into one. */
797 static int __init merge_note_headers_elf64(char *elfptr, size_t *elfsz,
798 char **notes_buf, size_t *notes_sz)
800 int i, nr_ptnote=0, rc=0;
802 Elf64_Ehdr *ehdr_ptr;
804 u64 phdr_sz = 0, note_off;
806 ehdr_ptr = (Elf64_Ehdr *)elfptr;
808 rc = update_note_header_size_elf64(ehdr_ptr);
812 rc = get_note_number_and_size_elf64(ehdr_ptr, &nr_ptnote, &phdr_sz);
816 *notes_sz = roundup(phdr_sz, PAGE_SIZE);
817 *notes_buf = vmcore_alloc_buf(*notes_sz);
821 rc = copy_notes_elf64(ehdr_ptr, *notes_buf);
825 /* Prepare merged PT_NOTE program header. */
826 phdr.p_type = PT_NOTE;
828 note_off = sizeof(Elf64_Ehdr) +
829 (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf64_Phdr);
830 phdr.p_offset = roundup(note_off, PAGE_SIZE);
831 phdr.p_vaddr = phdr.p_paddr = 0;
832 phdr.p_filesz = phdr.p_memsz = phdr_sz;
835 /* Add merged PT_NOTE program header*/
836 tmp = elfptr + sizeof(Elf64_Ehdr);
837 memcpy(tmp, &phdr, sizeof(phdr));
840 /* Remove unwanted PT_NOTE program headers. */
841 i = (nr_ptnote - 1) * sizeof(Elf64_Phdr);
843 memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf64_Ehdr)-sizeof(Elf64_Phdr)));
844 memset(elfptr + *elfsz, 0, i);
845 *elfsz = roundup(*elfsz, PAGE_SIZE);
847 /* Modify e_phnum to reflect merged headers. */
848 ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
850 /* Store the size of all notes. We need this to update the note
851 * header when the device dumps will be added.
853 elfnotes_orig_sz = phdr.p_memsz;
859 * update_note_header_size_elf32 - update p_memsz member of each PT_NOTE entry
861 * @ehdr_ptr: ELF header
863 * This function updates p_memsz member of each PT_NOTE entry in the
864 * program header table pointed to by @ehdr_ptr to real size of ELF
867 static int __init update_note_header_size_elf32(const Elf32_Ehdr *ehdr_ptr)
870 Elf32_Phdr *phdr_ptr;
871 Elf32_Nhdr *nhdr_ptr;
873 phdr_ptr = (Elf32_Phdr *)(ehdr_ptr + 1);
874 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
876 u64 offset, max_sz, sz, real_sz = 0;
877 if (phdr_ptr->p_type != PT_NOTE)
879 max_sz = phdr_ptr->p_memsz;
880 offset = phdr_ptr->p_offset;
881 notes_section = kmalloc(max_sz, GFP_KERNEL);
884 rc = elfcorehdr_read_notes(notes_section, max_sz, &offset);
886 kfree(notes_section);
889 nhdr_ptr = notes_section;
890 while (nhdr_ptr->n_namesz != 0) {
891 sz = sizeof(Elf32_Nhdr) +
892 (((u64)nhdr_ptr->n_namesz + 3) & ~3) +
893 (((u64)nhdr_ptr->n_descsz + 3) & ~3);
894 if ((real_sz + sz) > max_sz) {
895 pr_warn("Warning: Exceeded p_memsz, dropping PT_NOTE entry n_namesz=0x%x, n_descsz=0x%x\n",
896 nhdr_ptr->n_namesz, nhdr_ptr->n_descsz);
900 nhdr_ptr = (Elf32_Nhdr*)((char*)nhdr_ptr + sz);
902 kfree(notes_section);
903 phdr_ptr->p_memsz = real_sz;
905 pr_warn("Warning: Zero PT_NOTE entries found\n");
913 * get_note_number_and_size_elf32 - get the number of PT_NOTE program
914 * headers and sum of real size of their ELF note segment headers and
917 * @ehdr_ptr: ELF header
918 * @nr_ptnote: buffer for the number of PT_NOTE program headers
919 * @sz_ptnote: buffer for size of unique PT_NOTE program header
921 * This function is used to merge multiple PT_NOTE program headers
922 * into a unique single one. The resulting unique entry will have
923 * @sz_ptnote in its phdr->p_mem.
925 * It is assumed that program headers with PT_NOTE type pointed to by
926 * @ehdr_ptr has already been updated by update_note_header_size_elf32
927 * and each of PT_NOTE program headers has actual ELF note segment
928 * size in its p_memsz member.
930 static int __init get_note_number_and_size_elf32(const Elf32_Ehdr *ehdr_ptr,
931 int *nr_ptnote, u64 *sz_ptnote)
934 Elf32_Phdr *phdr_ptr;
936 *nr_ptnote = *sz_ptnote = 0;
938 phdr_ptr = (Elf32_Phdr *)(ehdr_ptr + 1);
939 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
940 if (phdr_ptr->p_type != PT_NOTE)
943 *sz_ptnote += phdr_ptr->p_memsz;
950 * copy_notes_elf32 - copy ELF note segments in a given buffer
952 * @ehdr_ptr: ELF header
953 * @notes_buf: buffer into which ELF note segments are copied
955 * This function is used to copy ELF note segment in the 1st kernel
956 * into the buffer @notes_buf in the 2nd kernel. It is assumed that
957 * size of the buffer @notes_buf is equal to or larger than sum of the
958 * real ELF note segment headers and data.
960 * It is assumed that program headers with PT_NOTE type pointed to by
961 * @ehdr_ptr has already been updated by update_note_header_size_elf32
962 * and each of PT_NOTE program headers has actual ELF note segment
963 * size in its p_memsz member.
965 static int __init copy_notes_elf32(const Elf32_Ehdr *ehdr_ptr, char *notes_buf)
968 Elf32_Phdr *phdr_ptr;
970 phdr_ptr = (Elf32_Phdr*)(ehdr_ptr + 1);
972 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
974 if (phdr_ptr->p_type != PT_NOTE)
976 offset = phdr_ptr->p_offset;
977 rc = elfcorehdr_read_notes(notes_buf, phdr_ptr->p_memsz,
981 notes_buf += phdr_ptr->p_memsz;
987 /* Merges all the PT_NOTE headers into one. */
988 static int __init merge_note_headers_elf32(char *elfptr, size_t *elfsz,
989 char **notes_buf, size_t *notes_sz)
991 int i, nr_ptnote=0, rc=0;
993 Elf32_Ehdr *ehdr_ptr;
995 u64 phdr_sz = 0, note_off;
997 ehdr_ptr = (Elf32_Ehdr *)elfptr;
999 rc = update_note_header_size_elf32(ehdr_ptr);
1003 rc = get_note_number_and_size_elf32(ehdr_ptr, &nr_ptnote, &phdr_sz);
1007 *notes_sz = roundup(phdr_sz, PAGE_SIZE);
1008 *notes_buf = vmcore_alloc_buf(*notes_sz);
1012 rc = copy_notes_elf32(ehdr_ptr, *notes_buf);
1016 /* Prepare merged PT_NOTE program header. */
1017 phdr.p_type = PT_NOTE;
1019 note_off = sizeof(Elf32_Ehdr) +
1020 (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf32_Phdr);
1021 phdr.p_offset = roundup(note_off, PAGE_SIZE);
1022 phdr.p_vaddr = phdr.p_paddr = 0;
1023 phdr.p_filesz = phdr.p_memsz = phdr_sz;
1026 /* Add merged PT_NOTE program header*/
1027 tmp = elfptr + sizeof(Elf32_Ehdr);
1028 memcpy(tmp, &phdr, sizeof(phdr));
1029 tmp += sizeof(phdr);
1031 /* Remove unwanted PT_NOTE program headers. */
1032 i = (nr_ptnote - 1) * sizeof(Elf32_Phdr);
1033 *elfsz = *elfsz - i;
1034 memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf32_Ehdr)-sizeof(Elf32_Phdr)));
1035 memset(elfptr + *elfsz, 0, i);
1036 *elfsz = roundup(*elfsz, PAGE_SIZE);
1038 /* Modify e_phnum to reflect merged headers. */
1039 ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
1041 /* Store the size of all notes. We need this to update the note
1042 * header when the device dumps will be added.
1044 elfnotes_orig_sz = phdr.p_memsz;
1049 /* Add memory chunks represented by program headers to vmcore list. Also update
1050 * the new offset fields of exported program headers. */
1051 static int __init process_ptload_program_headers_elf64(char *elfptr,
1054 struct list_head *vc_list)
1057 Elf64_Ehdr *ehdr_ptr;
1058 Elf64_Phdr *phdr_ptr;
1062 ehdr_ptr = (Elf64_Ehdr *)elfptr;
1063 phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr)); /* PT_NOTE hdr */
1065 /* Skip Elf header, program headers and Elf note segment. */
1066 vmcore_off = elfsz + elfnotes_sz;
1068 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
1069 u64 paddr, start, end, size;
1071 if (phdr_ptr->p_type != PT_LOAD)
1074 paddr = phdr_ptr->p_offset;
1075 start = rounddown(paddr, PAGE_SIZE);
1076 end = roundup(paddr + phdr_ptr->p_memsz, PAGE_SIZE);
1079 /* Add this contiguous chunk of memory to vmcore list.*/
1080 new = get_new_element();
1085 list_add_tail(&new->list, vc_list);
1087 /* Update the program header offset. */
1088 phdr_ptr->p_offset = vmcore_off + (paddr - start);
1089 vmcore_off = vmcore_off + size;
1094 static int __init process_ptload_program_headers_elf32(char *elfptr,
1097 struct list_head *vc_list)
1100 Elf32_Ehdr *ehdr_ptr;
1101 Elf32_Phdr *phdr_ptr;
1105 ehdr_ptr = (Elf32_Ehdr *)elfptr;
1106 phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr)); /* PT_NOTE hdr */
1108 /* Skip Elf header, program headers and Elf note segment. */
1109 vmcore_off = elfsz + elfnotes_sz;
1111 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
1112 u64 paddr, start, end, size;
1114 if (phdr_ptr->p_type != PT_LOAD)
1117 paddr = phdr_ptr->p_offset;
1118 start = rounddown(paddr, PAGE_SIZE);
1119 end = roundup(paddr + phdr_ptr->p_memsz, PAGE_SIZE);
1122 /* Add this contiguous chunk of memory to vmcore list.*/
1123 new = get_new_element();
1128 list_add_tail(&new->list, vc_list);
1130 /* Update the program header offset */
1131 phdr_ptr->p_offset = vmcore_off + (paddr - start);
1132 vmcore_off = vmcore_off + size;
1137 /* Sets offset fields of vmcore elements. */
1138 static void set_vmcore_list_offsets(size_t elfsz, size_t elfnotes_sz,
1139 struct list_head *vc_list)
1144 /* Skip Elf header, program headers and Elf note segment. */
1145 vmcore_off = elfsz + elfnotes_sz;
1147 list_for_each_entry(m, vc_list, list) {
1148 m->offset = vmcore_off;
1149 vmcore_off += m->size;
1153 static void free_elfcorebuf(void)
1155 free_pages((unsigned long)elfcorebuf, get_order(elfcorebuf_sz_orig));
1157 vfree(elfnotes_buf);
1158 elfnotes_buf = NULL;
1161 static int __init parse_crash_elf64_headers(void)
1167 addr = elfcorehdr_addr;
1169 /* Read Elf header */
1170 rc = elfcorehdr_read((char *)&ehdr, sizeof(Elf64_Ehdr), &addr);
1174 /* Do some basic Verification. */
1175 if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
1176 (ehdr.e_type != ET_CORE) ||
1177 !vmcore_elf64_check_arch(&ehdr) ||
1178 ehdr.e_ident[EI_CLASS] != ELFCLASS64 ||
1179 ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
1180 ehdr.e_version != EV_CURRENT ||
1181 ehdr.e_ehsize != sizeof(Elf64_Ehdr) ||
1182 ehdr.e_phentsize != sizeof(Elf64_Phdr) ||
1183 ehdr.e_phnum == 0) {
1184 pr_warn("Warning: Core image elf header is not sane\n");
1188 /* Read in all elf headers. */
1189 elfcorebuf_sz_orig = sizeof(Elf64_Ehdr) +
1190 ehdr.e_phnum * sizeof(Elf64_Phdr);
1191 elfcorebuf_sz = elfcorebuf_sz_orig;
1192 elfcorebuf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
1193 get_order(elfcorebuf_sz_orig));
1196 addr = elfcorehdr_addr;
1197 rc = elfcorehdr_read(elfcorebuf, elfcorebuf_sz_orig, &addr);
1201 /* Merge all PT_NOTE headers into one. */
1202 rc = merge_note_headers_elf64(elfcorebuf, &elfcorebuf_sz,
1203 &elfnotes_buf, &elfnotes_sz);
1206 rc = process_ptload_program_headers_elf64(elfcorebuf, elfcorebuf_sz,
1207 elfnotes_sz, &vmcore_list);
1210 set_vmcore_list_offsets(elfcorebuf_sz, elfnotes_sz, &vmcore_list);
1217 static int __init parse_crash_elf32_headers(void)
1223 addr = elfcorehdr_addr;
1225 /* Read Elf header */
1226 rc = elfcorehdr_read((char *)&ehdr, sizeof(Elf32_Ehdr), &addr);
1230 /* Do some basic Verification. */
1231 if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
1232 (ehdr.e_type != ET_CORE) ||
1233 !vmcore_elf32_check_arch(&ehdr) ||
1234 ehdr.e_ident[EI_CLASS] != ELFCLASS32||
1235 ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
1236 ehdr.e_version != EV_CURRENT ||
1237 ehdr.e_ehsize != sizeof(Elf32_Ehdr) ||
1238 ehdr.e_phentsize != sizeof(Elf32_Phdr) ||
1239 ehdr.e_phnum == 0) {
1240 pr_warn("Warning: Core image elf header is not sane\n");
1244 /* Read in all elf headers. */
1245 elfcorebuf_sz_orig = sizeof(Elf32_Ehdr) + ehdr.e_phnum * sizeof(Elf32_Phdr);
1246 elfcorebuf_sz = elfcorebuf_sz_orig;
1247 elfcorebuf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
1248 get_order(elfcorebuf_sz_orig));
1251 addr = elfcorehdr_addr;
1252 rc = elfcorehdr_read(elfcorebuf, elfcorebuf_sz_orig, &addr);
1256 /* Merge all PT_NOTE headers into one. */
1257 rc = merge_note_headers_elf32(elfcorebuf, &elfcorebuf_sz,
1258 &elfnotes_buf, &elfnotes_sz);
1261 rc = process_ptload_program_headers_elf32(elfcorebuf, elfcorebuf_sz,
1262 elfnotes_sz, &vmcore_list);
1265 set_vmcore_list_offsets(elfcorebuf_sz, elfnotes_sz, &vmcore_list);
1272 static int __init parse_crash_elf_headers(void)
1274 unsigned char e_ident[EI_NIDENT];
1278 addr = elfcorehdr_addr;
1279 rc = elfcorehdr_read(e_ident, EI_NIDENT, &addr);
1282 if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) {
1283 pr_warn("Warning: Core image elf header not found\n");
1287 if (e_ident[EI_CLASS] == ELFCLASS64) {
1288 rc = parse_crash_elf64_headers();
1291 } else if (e_ident[EI_CLASS] == ELFCLASS32) {
1292 rc = parse_crash_elf32_headers();
1296 pr_warn("Warning: Core image elf header is not sane\n");
1300 /* Determine vmcore size. */
1301 vmcore_size = get_vmcore_size(elfcorebuf_sz, elfnotes_sz,
1307 #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
1309 * vmcoredd_write_header - Write vmcore device dump header at the
1310 * beginning of the dump's buffer.
1311 * @buf: Output buffer where the note is written
1313 * @size: Size of the dump
1315 * Fills beginning of the dump's buffer with vmcore device dump header.
1317 static void vmcoredd_write_header(void *buf, struct vmcoredd_data *data,
1320 struct vmcoredd_header *vdd_hdr = (struct vmcoredd_header *)buf;
1322 vdd_hdr->n_namesz = sizeof(vdd_hdr->name);
1323 vdd_hdr->n_descsz = size + sizeof(vdd_hdr->dump_name);
1324 vdd_hdr->n_type = NT_VMCOREDD;
1326 strncpy((char *)vdd_hdr->name, VMCOREDD_NOTE_NAME,
1327 sizeof(vdd_hdr->name));
1328 memcpy(vdd_hdr->dump_name, data->dump_name, sizeof(vdd_hdr->dump_name));
1332 * vmcoredd_update_program_headers - Update all Elf program headers
1333 * @elfptr: Pointer to elf header
1334 * @elfnotesz: Size of elf notes aligned to page size
1335 * @vmcoreddsz: Size of device dumps to be added to elf note header
1337 * Determine type of Elf header (Elf64 or Elf32) and update the elf note size.
1338 * Also update the offsets of all the program headers after the elf note header.
1340 static void vmcoredd_update_program_headers(char *elfptr, size_t elfnotesz,
1343 unsigned char *e_ident = (unsigned char *)elfptr;
1344 u64 start, end, size;
1348 vmcore_off = elfcorebuf_sz + elfnotesz;
1350 if (e_ident[EI_CLASS] == ELFCLASS64) {
1351 Elf64_Ehdr *ehdr = (Elf64_Ehdr *)elfptr;
1352 Elf64_Phdr *phdr = (Elf64_Phdr *)(elfptr + sizeof(Elf64_Ehdr));
1354 /* Update all program headers */
1355 for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
1356 if (phdr->p_type == PT_NOTE) {
1357 /* Update note size */
1358 phdr->p_memsz = elfnotes_orig_sz + vmcoreddsz;
1359 phdr->p_filesz = phdr->p_memsz;
1363 start = rounddown(phdr->p_offset, PAGE_SIZE);
1364 end = roundup(phdr->p_offset + phdr->p_memsz,
1367 phdr->p_offset = vmcore_off + (phdr->p_offset - start);
1371 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)elfptr;
1372 Elf32_Phdr *phdr = (Elf32_Phdr *)(elfptr + sizeof(Elf32_Ehdr));
1374 /* Update all program headers */
1375 for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
1376 if (phdr->p_type == PT_NOTE) {
1377 /* Update note size */
1378 phdr->p_memsz = elfnotes_orig_sz + vmcoreddsz;
1379 phdr->p_filesz = phdr->p_memsz;
1383 start = rounddown(phdr->p_offset, PAGE_SIZE);
1384 end = roundup(phdr->p_offset + phdr->p_memsz,
1387 phdr->p_offset = vmcore_off + (phdr->p_offset - start);
1394 * vmcoredd_update_size - Update the total size of the device dumps and update
1396 * @dump_size: Size of the current device dump to be added to total size
1398 * Update the total size of all the device dumps and update the Elf program
1399 * headers. Calculate the new offsets for the vmcore list and update the
1400 * total vmcore size.
1402 static void vmcoredd_update_size(size_t dump_size)
1404 vmcoredd_orig_sz += dump_size;
1405 elfnotes_sz = roundup(elfnotes_orig_sz, PAGE_SIZE) + vmcoredd_orig_sz;
1406 vmcoredd_update_program_headers(elfcorebuf, elfnotes_sz,
1409 /* Update vmcore list offsets */
1410 set_vmcore_list_offsets(elfcorebuf_sz, elfnotes_sz, &vmcore_list);
1412 vmcore_size = get_vmcore_size(elfcorebuf_sz, elfnotes_sz,
1414 proc_vmcore->size = vmcore_size;
1418 * vmcore_add_device_dump - Add a buffer containing device dump to vmcore
1421 * Allocate a buffer and invoke the calling driver's dump collect routine.
1422 * Write Elf note at the beginning of the buffer to indicate vmcore device
1423 * dump and add the dump to global list.
1425 int vmcore_add_device_dump(struct vmcoredd_data *data)
1427 struct vmcoredd_node *dump;
1432 if (!data || !strlen(data->dump_name) ||
1433 !data->vmcoredd_callback || !data->size)
1436 dump = vzalloc(sizeof(*dump));
1442 /* Keep size of the buffer page aligned so that it can be mmaped */
1443 data_size = roundup(sizeof(struct vmcoredd_header) + data->size,
1446 /* Allocate buffer for driver's to write their dumps */
1447 buf = vmcore_alloc_buf(data_size);
1453 vmcoredd_write_header(buf, data, data_size -
1454 sizeof(struct vmcoredd_header));
1456 /* Invoke the driver's dump collection routing */
1457 ret = data->vmcoredd_callback(data, buf +
1458 sizeof(struct vmcoredd_header));
1463 dump->size = data_size;
1465 /* Add the dump to driver sysfs list */
1466 mutex_lock(&vmcoredd_mutex);
1467 list_add_tail(&dump->list, &vmcoredd_list);
1468 mutex_unlock(&vmcoredd_mutex);
1470 vmcoredd_update_size(data_size);
1482 EXPORT_SYMBOL(vmcore_add_device_dump);
1483 #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
1485 /* Free all dumps in vmcore device dump list */
1486 static void vmcore_free_device_dumps(void)
1488 #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
1489 mutex_lock(&vmcoredd_mutex);
1490 while (!list_empty(&vmcoredd_list)) {
1491 struct vmcoredd_node *dump;
1493 dump = list_first_entry(&vmcoredd_list, struct vmcoredd_node,
1495 list_del(&dump->list);
1499 mutex_unlock(&vmcoredd_mutex);
1500 #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
1503 /* Init function for vmcore module. */
1504 static int __init vmcore_init(void)
1508 /* Allow architectures to allocate ELF header in 2nd kernel */
1509 rc = elfcorehdr_alloc(&elfcorehdr_addr, &elfcorehdr_size);
1513 * If elfcorehdr= has been passed in cmdline or created in 2nd kernel,
1514 * then capture the dump.
1516 if (!(is_vmcore_usable()))
1518 rc = parse_crash_elf_headers();
1520 pr_warn("Kdump: vmcore not initialized\n");
1523 elfcorehdr_free(elfcorehdr_addr);
1524 elfcorehdr_addr = ELFCORE_ADDR_ERR;
1526 proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations);
1528 proc_vmcore->size = vmcore_size;
1531 fs_initcall(vmcore_init);
1533 /* Cleanup function for vmcore module. */
1534 void vmcore_cleanup(void)
1537 proc_remove(proc_vmcore);
1541 /* clear the vmcore list. */
1542 while (!list_empty(&vmcore_list)) {
1545 m = list_first_entry(&vmcore_list, struct vmcore, list);
1551 /* clear vmcore device dump list */
1552 vmcore_free_device_dumps();