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/vmalloc.h>
24 #include <asm/uaccess.h>
28 /* List representing chunks of contiguous memory areas and their offsets in
31 static LIST_HEAD(vmcore_list
);
33 /* Stores the pointer to the buffer containing kernel elf core headers. */
34 static char *elfcorebuf
;
35 static size_t elfcorebuf_sz
;
36 static size_t elfcorebuf_sz_orig
;
38 static char *elfnotes_buf
;
39 static size_t elfnotes_sz
;
41 /* Total size of vmcore file. */
42 static u64 vmcore_size
;
44 static struct proc_dir_entry
*proc_vmcore
= NULL
;
47 * Returns > 0 for RAM pages, 0 for non-RAM pages, < 0 on error
48 * The called function has to take care of module refcounting.
50 static int (*oldmem_pfn_is_ram
)(unsigned long pfn
);
52 int register_oldmem_pfn_is_ram(int (*fn
)(unsigned long pfn
))
54 if (oldmem_pfn_is_ram
)
56 oldmem_pfn_is_ram
= fn
;
59 EXPORT_SYMBOL_GPL(register_oldmem_pfn_is_ram
);
61 void unregister_oldmem_pfn_is_ram(void)
63 oldmem_pfn_is_ram
= NULL
;
66 EXPORT_SYMBOL_GPL(unregister_oldmem_pfn_is_ram
);
68 static int pfn_is_ram(unsigned long pfn
)
70 int (*fn
)(unsigned long pfn
);
71 /* pfn is ram unless fn() checks pagetype */
75 * Ask hypervisor if the pfn is really ram.
76 * A ballooned page contains no data and reading from such a page
77 * will cause high load in the hypervisor.
79 fn
= oldmem_pfn_is_ram
;
86 /* Reads a page from the oldmem device from given offset. */
87 static ssize_t
read_from_oldmem(char *buf
, size_t count
,
88 u64
*ppos
, int userbuf
)
90 unsigned long pfn
, offset
;
92 ssize_t read
= 0, tmp
;
97 offset
= (unsigned long)(*ppos
% PAGE_SIZE
);
98 pfn
= (unsigned long)(*ppos
/ PAGE_SIZE
);
101 if (count
> (PAGE_SIZE
- offset
))
102 nr_bytes
= PAGE_SIZE
- offset
;
106 /* If pfn is not ram, return zeros for sparse dump files */
107 if (pfn_is_ram(pfn
) == 0)
108 memset(buf
, 0, nr_bytes
);
110 tmp
= copy_oldmem_page(pfn
, buf
, nr_bytes
,
126 /* Read from the ELF header and then the crash dump. On error, negative value is
127 * returned otherwise number of bytes read are returned.
129 static ssize_t
read_vmcore(struct file
*file
, char __user
*buffer
,
130 size_t buflen
, loff_t
*fpos
)
132 ssize_t acc
= 0, tmp
;
135 struct vmcore
*m
= NULL
;
137 if (buflen
== 0 || *fpos
>= vmcore_size
)
140 /* trim buflen to not go beyond EOF */
141 if (buflen
> vmcore_size
- *fpos
)
142 buflen
= vmcore_size
- *fpos
;
144 /* Read ELF core header */
145 if (*fpos
< elfcorebuf_sz
) {
146 tsz
= min(elfcorebuf_sz
- (size_t)*fpos
, buflen
);
147 if (copy_to_user(buffer
, elfcorebuf
+ *fpos
, tsz
))
154 /* leave now if filled buffer already */
159 /* Read Elf note segment */
160 if (*fpos
< elfcorebuf_sz
+ elfnotes_sz
) {
163 tsz
= min(elfcorebuf_sz
+ elfnotes_sz
- (size_t)*fpos
, buflen
);
164 kaddr
= elfnotes_buf
+ *fpos
- elfcorebuf_sz
;
165 if (copy_to_user(buffer
, kaddr
, tsz
))
172 /* leave now if filled buffer already */
177 list_for_each_entry(m
, &vmcore_list
, list
) {
178 if (*fpos
< m
->offset
+ m
->size
) {
179 tsz
= min_t(size_t, m
->offset
+ m
->size
- *fpos
, buflen
);
180 start
= m
->paddr
+ *fpos
- m
->offset
;
181 tmp
= read_from_oldmem(buffer
, tsz
, &start
, 1);
189 /* leave now if filled buffer already */
199 * alloc_elfnotes_buf - allocate buffer for ELF note segment in
202 * @notes_sz: size of buffer
204 * If CONFIG_MMU is defined, use vmalloc_user() to allow users to mmap
205 * the buffer to user-space by means of remap_vmalloc_range().
207 * If CONFIG_MMU is not defined, use vzalloc() since mmap_vmcore() is
208 * disabled and there's no need to allow users to mmap the buffer.
210 static inline char *alloc_elfnotes_buf(size_t notes_sz
)
213 return vmalloc_user(notes_sz
);
215 return vzalloc(notes_sz
);
220 * Disable mmap_vmcore() if CONFIG_MMU is not defined. MMU is
221 * essential for mmap_vmcore() in order to map physically
222 * non-contiguous objects (ELF header, ELF note segment and memory
223 * regions in the 1st kernel pointed to by PT_LOAD entries) into
224 * virtually contiguous user-space in ELF layout.
227 static int mmap_vmcore(struct file
*file
, struct vm_area_struct
*vma
)
229 size_t size
= vma
->vm_end
- vma
->vm_start
;
230 u64 start
, end
, len
, tsz
;
233 start
= (u64
)vma
->vm_pgoff
<< PAGE_SHIFT
;
236 if (size
> vmcore_size
|| end
> vmcore_size
)
239 if (vma
->vm_flags
& (VM_WRITE
| VM_EXEC
))
242 vma
->vm_flags
&= ~(VM_MAYWRITE
| VM_MAYEXEC
);
243 vma
->vm_flags
|= VM_MIXEDMAP
;
247 if (start
< elfcorebuf_sz
) {
250 tsz
= min(elfcorebuf_sz
- (size_t)start
, size
);
251 pfn
= __pa(elfcorebuf
+ start
) >> PAGE_SHIFT
;
252 if (remap_pfn_range(vma
, vma
->vm_start
, pfn
, tsz
,
263 if (start
< elfcorebuf_sz
+ elfnotes_sz
) {
266 tsz
= min(elfcorebuf_sz
+ elfnotes_sz
- (size_t)start
, size
);
267 kaddr
= elfnotes_buf
+ start
- elfcorebuf_sz
;
268 if (remap_vmalloc_range_partial(vma
, vma
->vm_start
+ len
,
279 list_for_each_entry(m
, &vmcore_list
, list
) {
280 if (start
< m
->offset
+ m
->size
) {
283 tsz
= min_t(size_t, m
->offset
+ m
->size
- start
, size
);
284 paddr
= m
->paddr
+ start
- m
->offset
;
285 if (remap_pfn_range(vma
, vma
->vm_start
+ len
,
286 paddr
>> PAGE_SHIFT
, tsz
,
300 do_munmap(vma
->vm_mm
, vma
->vm_start
, len
);
304 static int mmap_vmcore(struct file
*file
, struct vm_area_struct
*vma
)
310 static const struct file_operations proc_vmcore_operations
= {
312 .llseek
= default_llseek
,
316 static struct vmcore
* __init
get_new_element(void)
318 return kzalloc(sizeof(struct vmcore
), GFP_KERNEL
);
321 static u64 __init
get_vmcore_size(size_t elfsz
, size_t elfnotesegsz
,
322 struct list_head
*vc_list
)
327 size
= elfsz
+ elfnotesegsz
;
328 list_for_each_entry(m
, vc_list
, list
) {
335 * update_note_header_size_elf64 - update p_memsz member of each PT_NOTE entry
337 * @ehdr_ptr: ELF header
339 * This function updates p_memsz member of each PT_NOTE entry in the
340 * program header table pointed to by @ehdr_ptr to real size of ELF
343 static int __init
update_note_header_size_elf64(const Elf64_Ehdr
*ehdr_ptr
)
346 Elf64_Phdr
*phdr_ptr
;
347 Elf64_Nhdr
*nhdr_ptr
;
349 phdr_ptr
= (Elf64_Phdr
*)(ehdr_ptr
+ 1);
350 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++, phdr_ptr
++) {
352 u64 offset
, max_sz
, sz
, real_sz
= 0;
353 if (phdr_ptr
->p_type
!= PT_NOTE
)
355 max_sz
= phdr_ptr
->p_memsz
;
356 offset
= phdr_ptr
->p_offset
;
357 notes_section
= kmalloc(max_sz
, GFP_KERNEL
);
360 rc
= read_from_oldmem(notes_section
, max_sz
, &offset
, 0);
362 kfree(notes_section
);
365 nhdr_ptr
= notes_section
;
366 while (real_sz
< max_sz
) {
367 if (nhdr_ptr
->n_namesz
== 0)
369 sz
= sizeof(Elf64_Nhdr
) +
370 ((nhdr_ptr
->n_namesz
+ 3) & ~3) +
371 ((nhdr_ptr
->n_descsz
+ 3) & ~3);
373 nhdr_ptr
= (Elf64_Nhdr
*)((char*)nhdr_ptr
+ sz
);
375 kfree(notes_section
);
376 phdr_ptr
->p_memsz
= real_sz
;
383 * get_note_number_and_size_elf64 - get the number of PT_NOTE program
384 * headers and sum of real size of their ELF note segment headers and
387 * @ehdr_ptr: ELF header
388 * @nr_ptnote: buffer for the number of PT_NOTE program headers
389 * @sz_ptnote: buffer for size of unique PT_NOTE program header
391 * This function is used to merge multiple PT_NOTE program headers
392 * into a unique single one. The resulting unique entry will have
393 * @sz_ptnote in its phdr->p_mem.
395 * It is assumed that program headers with PT_NOTE type pointed to by
396 * @ehdr_ptr has already been updated by update_note_header_size_elf64
397 * and each of PT_NOTE program headers has actual ELF note segment
398 * size in its p_memsz member.
400 static int __init
get_note_number_and_size_elf64(const Elf64_Ehdr
*ehdr_ptr
,
401 int *nr_ptnote
, u64
*sz_ptnote
)
404 Elf64_Phdr
*phdr_ptr
;
406 *nr_ptnote
= *sz_ptnote
= 0;
408 phdr_ptr
= (Elf64_Phdr
*)(ehdr_ptr
+ 1);
409 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++, phdr_ptr
++) {
410 if (phdr_ptr
->p_type
!= PT_NOTE
)
413 *sz_ptnote
+= phdr_ptr
->p_memsz
;
420 * copy_notes_elf64 - copy ELF note segments in a given buffer
422 * @ehdr_ptr: ELF header
423 * @notes_buf: buffer into which ELF note segments are copied
425 * This function is used to copy ELF note segment in the 1st kernel
426 * into the buffer @notes_buf in the 2nd kernel. It is assumed that
427 * size of the buffer @notes_buf is equal to or larger than sum of the
428 * real ELF note segment headers and data.
430 * It is assumed that program headers with PT_NOTE type pointed to by
431 * @ehdr_ptr has already been updated by update_note_header_size_elf64
432 * and each of PT_NOTE program headers has actual ELF note segment
433 * size in its p_memsz member.
435 static int __init
copy_notes_elf64(const Elf64_Ehdr
*ehdr_ptr
, char *notes_buf
)
438 Elf64_Phdr
*phdr_ptr
;
440 phdr_ptr
= (Elf64_Phdr
*)(ehdr_ptr
+ 1);
442 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++, phdr_ptr
++) {
444 if (phdr_ptr
->p_type
!= PT_NOTE
)
446 offset
= phdr_ptr
->p_offset
;
447 rc
= read_from_oldmem(notes_buf
, phdr_ptr
->p_memsz
, &offset
, 0);
450 notes_buf
+= phdr_ptr
->p_memsz
;
456 /* Merges all the PT_NOTE headers into one. */
457 static int __init
merge_note_headers_elf64(char *elfptr
, size_t *elfsz
,
458 char **notes_buf
, size_t *notes_sz
)
460 int i
, nr_ptnote
=0, rc
=0;
462 Elf64_Ehdr
*ehdr_ptr
;
464 u64 phdr_sz
= 0, note_off
;
466 ehdr_ptr
= (Elf64_Ehdr
*)elfptr
;
468 rc
= update_note_header_size_elf64(ehdr_ptr
);
472 rc
= get_note_number_and_size_elf64(ehdr_ptr
, &nr_ptnote
, &phdr_sz
);
476 *notes_sz
= roundup(phdr_sz
, PAGE_SIZE
);
477 *notes_buf
= alloc_elfnotes_buf(*notes_sz
);
481 rc
= copy_notes_elf64(ehdr_ptr
, *notes_buf
);
485 /* Prepare merged PT_NOTE program header. */
486 phdr
.p_type
= PT_NOTE
;
488 note_off
= sizeof(Elf64_Ehdr
) +
489 (ehdr_ptr
->e_phnum
- nr_ptnote
+1) * sizeof(Elf64_Phdr
);
490 phdr
.p_offset
= roundup(note_off
, PAGE_SIZE
);
491 phdr
.p_vaddr
= phdr
.p_paddr
= 0;
492 phdr
.p_filesz
= phdr
.p_memsz
= phdr_sz
;
495 /* Add merged PT_NOTE program header*/
496 tmp
= elfptr
+ sizeof(Elf64_Ehdr
);
497 memcpy(tmp
, &phdr
, sizeof(phdr
));
500 /* Remove unwanted PT_NOTE program headers. */
501 i
= (nr_ptnote
- 1) * sizeof(Elf64_Phdr
);
503 memmove(tmp
, tmp
+i
, ((*elfsz
)-sizeof(Elf64_Ehdr
)-sizeof(Elf64_Phdr
)));
504 memset(elfptr
+ *elfsz
, 0, i
);
505 *elfsz
= roundup(*elfsz
, PAGE_SIZE
);
507 /* Modify e_phnum to reflect merged headers. */
508 ehdr_ptr
->e_phnum
= ehdr_ptr
->e_phnum
- nr_ptnote
+ 1;
514 * update_note_header_size_elf32 - update p_memsz member of each PT_NOTE entry
516 * @ehdr_ptr: ELF header
518 * This function updates p_memsz member of each PT_NOTE entry in the
519 * program header table pointed to by @ehdr_ptr to real size of ELF
522 static int __init
update_note_header_size_elf32(const Elf32_Ehdr
*ehdr_ptr
)
525 Elf32_Phdr
*phdr_ptr
;
526 Elf32_Nhdr
*nhdr_ptr
;
528 phdr_ptr
= (Elf32_Phdr
*)(ehdr_ptr
+ 1);
529 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++, phdr_ptr
++) {
531 u64 offset
, max_sz
, sz
, real_sz
= 0;
532 if (phdr_ptr
->p_type
!= PT_NOTE
)
534 max_sz
= phdr_ptr
->p_memsz
;
535 offset
= phdr_ptr
->p_offset
;
536 notes_section
= kmalloc(max_sz
, GFP_KERNEL
);
539 rc
= read_from_oldmem(notes_section
, max_sz
, &offset
, 0);
541 kfree(notes_section
);
544 nhdr_ptr
= notes_section
;
545 while (real_sz
< max_sz
) {
546 if (nhdr_ptr
->n_namesz
== 0)
548 sz
= sizeof(Elf32_Nhdr
) +
549 ((nhdr_ptr
->n_namesz
+ 3) & ~3) +
550 ((nhdr_ptr
->n_descsz
+ 3) & ~3);
552 nhdr_ptr
= (Elf32_Nhdr
*)((char*)nhdr_ptr
+ sz
);
554 kfree(notes_section
);
555 phdr_ptr
->p_memsz
= real_sz
;
562 * get_note_number_and_size_elf32 - get the number of PT_NOTE program
563 * headers and sum of real size of their ELF note segment headers and
566 * @ehdr_ptr: ELF header
567 * @nr_ptnote: buffer for the number of PT_NOTE program headers
568 * @sz_ptnote: buffer for size of unique PT_NOTE program header
570 * This function is used to merge multiple PT_NOTE program headers
571 * into a unique single one. The resulting unique entry will have
572 * @sz_ptnote in its phdr->p_mem.
574 * It is assumed that program headers with PT_NOTE type pointed to by
575 * @ehdr_ptr has already been updated by update_note_header_size_elf32
576 * and each of PT_NOTE program headers has actual ELF note segment
577 * size in its p_memsz member.
579 static int __init
get_note_number_and_size_elf32(const Elf32_Ehdr
*ehdr_ptr
,
580 int *nr_ptnote
, u64
*sz_ptnote
)
583 Elf32_Phdr
*phdr_ptr
;
585 *nr_ptnote
= *sz_ptnote
= 0;
587 phdr_ptr
= (Elf32_Phdr
*)(ehdr_ptr
+ 1);
588 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++, phdr_ptr
++) {
589 if (phdr_ptr
->p_type
!= PT_NOTE
)
592 *sz_ptnote
+= phdr_ptr
->p_memsz
;
599 * copy_notes_elf32 - copy ELF note segments in a given buffer
601 * @ehdr_ptr: ELF header
602 * @notes_buf: buffer into which ELF note segments are copied
604 * This function is used to copy ELF note segment in the 1st kernel
605 * into the buffer @notes_buf in the 2nd kernel. It is assumed that
606 * size of the buffer @notes_buf is equal to or larger than sum of the
607 * real ELF note segment headers and data.
609 * It is assumed that program headers with PT_NOTE type pointed to by
610 * @ehdr_ptr has already been updated by update_note_header_size_elf32
611 * and each of PT_NOTE program headers has actual ELF note segment
612 * size in its p_memsz member.
614 static int __init
copy_notes_elf32(const Elf32_Ehdr
*ehdr_ptr
, char *notes_buf
)
617 Elf32_Phdr
*phdr_ptr
;
619 phdr_ptr
= (Elf32_Phdr
*)(ehdr_ptr
+ 1);
621 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++, phdr_ptr
++) {
623 if (phdr_ptr
->p_type
!= PT_NOTE
)
625 offset
= phdr_ptr
->p_offset
;
626 rc
= read_from_oldmem(notes_buf
, phdr_ptr
->p_memsz
, &offset
, 0);
629 notes_buf
+= phdr_ptr
->p_memsz
;
635 /* Merges all the PT_NOTE headers into one. */
636 static int __init
merge_note_headers_elf32(char *elfptr
, size_t *elfsz
,
637 char **notes_buf
, size_t *notes_sz
)
639 int i
, nr_ptnote
=0, rc
=0;
641 Elf32_Ehdr
*ehdr_ptr
;
643 u64 phdr_sz
= 0, note_off
;
645 ehdr_ptr
= (Elf32_Ehdr
*)elfptr
;
647 rc
= update_note_header_size_elf32(ehdr_ptr
);
651 rc
= get_note_number_and_size_elf32(ehdr_ptr
, &nr_ptnote
, &phdr_sz
);
655 *notes_sz
= roundup(phdr_sz
, PAGE_SIZE
);
656 *notes_buf
= alloc_elfnotes_buf(*notes_sz
);
660 rc
= copy_notes_elf32(ehdr_ptr
, *notes_buf
);
664 /* Prepare merged PT_NOTE program header. */
665 phdr
.p_type
= PT_NOTE
;
667 note_off
= sizeof(Elf32_Ehdr
) +
668 (ehdr_ptr
->e_phnum
- nr_ptnote
+1) * sizeof(Elf32_Phdr
);
669 phdr
.p_offset
= roundup(note_off
, PAGE_SIZE
);
670 phdr
.p_vaddr
= phdr
.p_paddr
= 0;
671 phdr
.p_filesz
= phdr
.p_memsz
= phdr_sz
;
674 /* Add merged PT_NOTE program header*/
675 tmp
= elfptr
+ sizeof(Elf32_Ehdr
);
676 memcpy(tmp
, &phdr
, sizeof(phdr
));
679 /* Remove unwanted PT_NOTE program headers. */
680 i
= (nr_ptnote
- 1) * sizeof(Elf32_Phdr
);
682 memmove(tmp
, tmp
+i
, ((*elfsz
)-sizeof(Elf32_Ehdr
)-sizeof(Elf32_Phdr
)));
683 memset(elfptr
+ *elfsz
, 0, i
);
684 *elfsz
= roundup(*elfsz
, PAGE_SIZE
);
686 /* Modify e_phnum to reflect merged headers. */
687 ehdr_ptr
->e_phnum
= ehdr_ptr
->e_phnum
- nr_ptnote
+ 1;
692 /* Add memory chunks represented by program headers to vmcore list. Also update
693 * the new offset fields of exported program headers. */
694 static int __init
process_ptload_program_headers_elf64(char *elfptr
,
697 struct list_head
*vc_list
)
700 Elf64_Ehdr
*ehdr_ptr
;
701 Elf64_Phdr
*phdr_ptr
;
705 ehdr_ptr
= (Elf64_Ehdr
*)elfptr
;
706 phdr_ptr
= (Elf64_Phdr
*)(elfptr
+ sizeof(Elf64_Ehdr
)); /* PT_NOTE hdr */
708 /* Skip Elf header, program headers and Elf note segment. */
709 vmcore_off
= elfsz
+ elfnotes_sz
;
711 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++, phdr_ptr
++) {
712 u64 paddr
, start
, end
, size
;
714 if (phdr_ptr
->p_type
!= PT_LOAD
)
717 paddr
= phdr_ptr
->p_offset
;
718 start
= rounddown(paddr
, PAGE_SIZE
);
719 end
= roundup(paddr
+ phdr_ptr
->p_memsz
, PAGE_SIZE
);
722 /* Add this contiguous chunk of memory to vmcore list.*/
723 new = get_new_element();
728 list_add_tail(&new->list
, vc_list
);
730 /* Update the program header offset. */
731 phdr_ptr
->p_offset
= vmcore_off
+ (paddr
- start
);
732 vmcore_off
= vmcore_off
+ size
;
737 static int __init
process_ptload_program_headers_elf32(char *elfptr
,
740 struct list_head
*vc_list
)
743 Elf32_Ehdr
*ehdr_ptr
;
744 Elf32_Phdr
*phdr_ptr
;
748 ehdr_ptr
= (Elf32_Ehdr
*)elfptr
;
749 phdr_ptr
= (Elf32_Phdr
*)(elfptr
+ sizeof(Elf32_Ehdr
)); /* PT_NOTE hdr */
751 /* Skip Elf header, program headers and Elf note segment. */
752 vmcore_off
= elfsz
+ elfnotes_sz
;
754 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++, phdr_ptr
++) {
755 u64 paddr
, start
, end
, size
;
757 if (phdr_ptr
->p_type
!= PT_LOAD
)
760 paddr
= phdr_ptr
->p_offset
;
761 start
= rounddown(paddr
, PAGE_SIZE
);
762 end
= roundup(paddr
+ phdr_ptr
->p_memsz
, PAGE_SIZE
);
765 /* Add this contiguous chunk of memory to vmcore list.*/
766 new = get_new_element();
771 list_add_tail(&new->list
, vc_list
);
773 /* Update the program header offset */
774 phdr_ptr
->p_offset
= vmcore_off
+ (paddr
- start
);
775 vmcore_off
= vmcore_off
+ size
;
780 /* Sets offset fields of vmcore elements. */
781 static void __init
set_vmcore_list_offsets(size_t elfsz
, size_t elfnotes_sz
,
782 struct list_head
*vc_list
)
787 /* Skip Elf header, program headers and Elf note segment. */
788 vmcore_off
= elfsz
+ elfnotes_sz
;
790 list_for_each_entry(m
, vc_list
, list
) {
791 m
->offset
= vmcore_off
;
792 vmcore_off
+= m
->size
;
796 static void free_elfcorebuf(void)
798 free_pages((unsigned long)elfcorebuf
, get_order(elfcorebuf_sz_orig
));
804 static int __init
parse_crash_elf64_headers(void)
810 addr
= elfcorehdr_addr
;
812 /* Read Elf header */
813 rc
= read_from_oldmem((char*)&ehdr
, sizeof(Elf64_Ehdr
), &addr
, 0);
817 /* Do some basic Verification. */
818 if (memcmp(ehdr
.e_ident
, ELFMAG
, SELFMAG
) != 0 ||
819 (ehdr
.e_type
!= ET_CORE
) ||
820 !vmcore_elf64_check_arch(&ehdr
) ||
821 ehdr
.e_ident
[EI_CLASS
] != ELFCLASS64
||
822 ehdr
.e_ident
[EI_VERSION
] != EV_CURRENT
||
823 ehdr
.e_version
!= EV_CURRENT
||
824 ehdr
.e_ehsize
!= sizeof(Elf64_Ehdr
) ||
825 ehdr
.e_phentsize
!= sizeof(Elf64_Phdr
) ||
827 pr_warn("Warning: Core image elf header is not sane\n");
831 /* Read in all elf headers. */
832 elfcorebuf_sz_orig
= sizeof(Elf64_Ehdr
) +
833 ehdr
.e_phnum
* sizeof(Elf64_Phdr
);
834 elfcorebuf_sz
= elfcorebuf_sz_orig
;
835 elfcorebuf
= (void *)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
,
836 get_order(elfcorebuf_sz_orig
));
839 addr
= elfcorehdr_addr
;
840 rc
= read_from_oldmem(elfcorebuf
, elfcorebuf_sz_orig
, &addr
, 0);
844 /* Merge all PT_NOTE headers into one. */
845 rc
= merge_note_headers_elf64(elfcorebuf
, &elfcorebuf_sz
,
846 &elfnotes_buf
, &elfnotes_sz
);
849 rc
= process_ptload_program_headers_elf64(elfcorebuf
, elfcorebuf_sz
,
850 elfnotes_sz
, &vmcore_list
);
853 set_vmcore_list_offsets(elfcorebuf_sz
, elfnotes_sz
, &vmcore_list
);
860 static int __init
parse_crash_elf32_headers(void)
866 addr
= elfcorehdr_addr
;
868 /* Read Elf header */
869 rc
= read_from_oldmem((char*)&ehdr
, sizeof(Elf32_Ehdr
), &addr
, 0);
873 /* Do some basic Verification. */
874 if (memcmp(ehdr
.e_ident
, ELFMAG
, SELFMAG
) != 0 ||
875 (ehdr
.e_type
!= ET_CORE
) ||
876 !elf_check_arch(&ehdr
) ||
877 ehdr
.e_ident
[EI_CLASS
] != ELFCLASS32
||
878 ehdr
.e_ident
[EI_VERSION
] != EV_CURRENT
||
879 ehdr
.e_version
!= EV_CURRENT
||
880 ehdr
.e_ehsize
!= sizeof(Elf32_Ehdr
) ||
881 ehdr
.e_phentsize
!= sizeof(Elf32_Phdr
) ||
883 pr_warn("Warning: Core image elf header is not sane\n");
887 /* Read in all elf headers. */
888 elfcorebuf_sz_orig
= sizeof(Elf32_Ehdr
) + ehdr
.e_phnum
* sizeof(Elf32_Phdr
);
889 elfcorebuf_sz
= elfcorebuf_sz_orig
;
890 elfcorebuf
= (void *)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
,
891 get_order(elfcorebuf_sz_orig
));
894 addr
= elfcorehdr_addr
;
895 rc
= read_from_oldmem(elfcorebuf
, elfcorebuf_sz_orig
, &addr
, 0);
899 /* Merge all PT_NOTE headers into one. */
900 rc
= merge_note_headers_elf32(elfcorebuf
, &elfcorebuf_sz
,
901 &elfnotes_buf
, &elfnotes_sz
);
904 rc
= process_ptload_program_headers_elf32(elfcorebuf
, elfcorebuf_sz
,
905 elfnotes_sz
, &vmcore_list
);
908 set_vmcore_list_offsets(elfcorebuf_sz
, elfnotes_sz
, &vmcore_list
);
915 static int __init
parse_crash_elf_headers(void)
917 unsigned char e_ident
[EI_NIDENT
];
921 addr
= elfcorehdr_addr
;
922 rc
= read_from_oldmem(e_ident
, EI_NIDENT
, &addr
, 0);
925 if (memcmp(e_ident
, ELFMAG
, SELFMAG
) != 0) {
926 pr_warn("Warning: Core image elf header not found\n");
930 if (e_ident
[EI_CLASS
] == ELFCLASS64
) {
931 rc
= parse_crash_elf64_headers();
934 } else if (e_ident
[EI_CLASS
] == ELFCLASS32
) {
935 rc
= parse_crash_elf32_headers();
939 pr_warn("Warning: Core image elf header is not sane\n");
943 /* Determine vmcore size. */
944 vmcore_size
= get_vmcore_size(elfcorebuf_sz
, elfnotes_sz
,
950 /* Init function for vmcore module. */
951 static int __init
vmcore_init(void)
955 /* If elfcorehdr= has been passed in cmdline, then capture the dump.*/
956 if (!(is_vmcore_usable()))
958 rc
= parse_crash_elf_headers();
960 pr_warn("Kdump: vmcore not initialized\n");
964 proc_vmcore
= proc_create("vmcore", S_IRUSR
, NULL
, &proc_vmcore_operations
);
966 proc_vmcore
->size
= vmcore_size
;
969 module_init(vmcore_init
)
971 /* Cleanup function for vmcore module. */
972 void vmcore_cleanup(void)
974 struct list_head
*pos
, *next
;
977 proc_remove(proc_vmcore
);
981 /* clear the vmcore list. */
982 list_for_each_safe(pos
, next
, &vmcore_list
) {
985 m
= list_entry(pos
, struct vmcore
, list
);
991 EXPORT_SYMBOL_GPL(vmcore_cleanup
);