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 <asm/uaccess.h>
27 /* List representing chunks of contiguous memory areas and their offsets in
30 static LIST_HEAD(vmcore_list
);
32 /* Stores the pointer to the buffer containing kernel elf core headers. */
33 static char *elfcorebuf
;
34 static size_t elfcorebuf_sz
;
36 /* Total size of vmcore file. */
37 static u64 vmcore_size
;
39 static struct proc_dir_entry
*proc_vmcore
= NULL
;
42 * Returns > 0 for RAM pages, 0 for non-RAM pages, < 0 on error
43 * The called function has to take care of module refcounting.
45 static int (*oldmem_pfn_is_ram
)(unsigned long pfn
);
47 int register_oldmem_pfn_is_ram(int (*fn
)(unsigned long pfn
))
49 if (oldmem_pfn_is_ram
)
51 oldmem_pfn_is_ram
= fn
;
54 EXPORT_SYMBOL_GPL(register_oldmem_pfn_is_ram
);
56 void unregister_oldmem_pfn_is_ram(void)
58 oldmem_pfn_is_ram
= NULL
;
61 EXPORT_SYMBOL_GPL(unregister_oldmem_pfn_is_ram
);
63 static int pfn_is_ram(unsigned long pfn
)
65 int (*fn
)(unsigned long pfn
);
66 /* pfn is ram unless fn() checks pagetype */
70 * Ask hypervisor if the pfn is really ram.
71 * A ballooned page contains no data and reading from such a page
72 * will cause high load in the hypervisor.
74 fn
= oldmem_pfn_is_ram
;
81 /* Reads a page from the oldmem device from given offset. */
82 static ssize_t
read_from_oldmem(char *buf
, size_t count
,
83 u64
*ppos
, int userbuf
)
85 unsigned long pfn
, offset
;
87 ssize_t read
= 0, tmp
;
92 offset
= (unsigned long)(*ppos
% PAGE_SIZE
);
93 pfn
= (unsigned long)(*ppos
/ PAGE_SIZE
);
96 if (count
> (PAGE_SIZE
- offset
))
97 nr_bytes
= PAGE_SIZE
- offset
;
101 /* If pfn is not ram, return zeros for sparse dump files */
102 if (pfn_is_ram(pfn
) == 0)
103 memset(buf
, 0, nr_bytes
);
105 tmp
= copy_oldmem_page(pfn
, buf
, nr_bytes
,
121 /* Maps vmcore file offset to respective physical address in memroy. */
122 static u64
map_offset_to_paddr(loff_t offset
, struct list_head
*vc_list
,
123 struct vmcore
**m_ptr
)
128 list_for_each_entry(m
, vc_list
, list
) {
131 end
= m
->offset
+ m
->size
- 1;
132 if (offset
>= start
&& offset
<= end
) {
133 paddr
= m
->paddr
+ offset
- start
;
142 /* Read from the ELF header and then the crash dump. On error, negative value is
143 * returned otherwise number of bytes read are returned.
145 static ssize_t
read_vmcore(struct file
*file
, char __user
*buffer
,
146 size_t buflen
, loff_t
*fpos
)
148 ssize_t acc
= 0, tmp
;
151 struct vmcore
*curr_m
= NULL
;
153 if (buflen
== 0 || *fpos
>= vmcore_size
)
156 /* trim buflen to not go beyond EOF */
157 if (buflen
> vmcore_size
- *fpos
)
158 buflen
= vmcore_size
- *fpos
;
160 /* Read ELF core header */
161 if (*fpos
< elfcorebuf_sz
) {
162 tsz
= elfcorebuf_sz
- *fpos
;
165 if (copy_to_user(buffer
, elfcorebuf
+ *fpos
, tsz
))
172 /* leave now if filled buffer already */
177 start
= map_offset_to_paddr(*fpos
, &vmcore_list
, &curr_m
);
182 tsz
= min_t(size_t, buflen
, PAGE_SIZE
- (start
& ~PAGE_MASK
));
184 /* Calculate left bytes in current memory segment. */
185 nr_bytes
= (curr_m
->size
- (start
- curr_m
->paddr
));
189 tmp
= read_from_oldmem(buffer
, tsz
, &start
, 1);
196 if (start
>= (curr_m
->paddr
+ curr_m
->size
)) {
197 if (curr_m
->list
.next
== &vmcore_list
)
199 curr_m
= list_entry(curr_m
->list
.next
,
200 struct vmcore
, list
);
201 start
= curr_m
->paddr
;
207 static const struct file_operations proc_vmcore_operations
= {
209 .llseek
= default_llseek
,
212 static struct vmcore
* __init
get_new_element(void)
214 return kzalloc(sizeof(struct vmcore
), GFP_KERNEL
);
217 static u64 __init
get_vmcore_size_elf64(char *elfptr
)
221 Elf64_Ehdr
*ehdr_ptr
;
222 Elf64_Phdr
*phdr_ptr
;
224 ehdr_ptr
= (Elf64_Ehdr
*)elfptr
;
225 phdr_ptr
= (Elf64_Phdr
*)(elfptr
+ sizeof(Elf64_Ehdr
));
226 size
= sizeof(Elf64_Ehdr
) + ((ehdr_ptr
->e_phnum
) * sizeof(Elf64_Phdr
));
227 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++) {
228 size
+= phdr_ptr
->p_memsz
;
234 static u64 __init
get_vmcore_size_elf32(char *elfptr
)
238 Elf32_Ehdr
*ehdr_ptr
;
239 Elf32_Phdr
*phdr_ptr
;
241 ehdr_ptr
= (Elf32_Ehdr
*)elfptr
;
242 phdr_ptr
= (Elf32_Phdr
*)(elfptr
+ sizeof(Elf32_Ehdr
));
243 size
= sizeof(Elf32_Ehdr
) + ((ehdr_ptr
->e_phnum
) * sizeof(Elf32_Phdr
));
244 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++) {
245 size
+= phdr_ptr
->p_memsz
;
251 /* Merges all the PT_NOTE headers into one. */
252 static int __init
merge_note_headers_elf64(char *elfptr
, size_t *elfsz
,
253 struct list_head
*vc_list
)
255 int i
, nr_ptnote
=0, rc
=0;
257 Elf64_Ehdr
*ehdr_ptr
;
258 Elf64_Phdr phdr
, *phdr_ptr
;
259 Elf64_Nhdr
*nhdr_ptr
;
260 u64 phdr_sz
= 0, note_off
;
262 ehdr_ptr
= (Elf64_Ehdr
*)elfptr
;
263 phdr_ptr
= (Elf64_Phdr
*)(elfptr
+ sizeof(Elf64_Ehdr
));
264 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++, phdr_ptr
++) {
268 u64 offset
, max_sz
, sz
, real_sz
= 0;
269 if (phdr_ptr
->p_type
!= PT_NOTE
)
272 max_sz
= phdr_ptr
->p_memsz
;
273 offset
= phdr_ptr
->p_offset
;
274 notes_section
= kmalloc(max_sz
, GFP_KERNEL
);
277 rc
= read_from_oldmem(notes_section
, max_sz
, &offset
, 0);
279 kfree(notes_section
);
282 nhdr_ptr
= notes_section
;
283 for (j
= 0; j
< max_sz
; j
+= sz
) {
284 if (nhdr_ptr
->n_namesz
== 0)
286 sz
= sizeof(Elf64_Nhdr
) +
287 ((nhdr_ptr
->n_namesz
+ 3) & ~3) +
288 ((nhdr_ptr
->n_descsz
+ 3) & ~3);
290 nhdr_ptr
= (Elf64_Nhdr
*)((char*)nhdr_ptr
+ sz
);
293 /* Add this contiguous chunk of notes section to vmcore list.*/
294 new = get_new_element();
296 kfree(notes_section
);
299 new->paddr
= phdr_ptr
->p_offset
;
301 list_add_tail(&new->list
, vc_list
);
303 kfree(notes_section
);
306 /* Prepare merged PT_NOTE program header. */
307 phdr
.p_type
= PT_NOTE
;
309 note_off
= sizeof(Elf64_Ehdr
) +
310 (ehdr_ptr
->e_phnum
- nr_ptnote
+1) * sizeof(Elf64_Phdr
);
311 phdr
.p_offset
= note_off
;
312 phdr
.p_vaddr
= phdr
.p_paddr
= 0;
313 phdr
.p_filesz
= phdr
.p_memsz
= phdr_sz
;
316 /* Add merged PT_NOTE program header*/
317 tmp
= elfptr
+ sizeof(Elf64_Ehdr
);
318 memcpy(tmp
, &phdr
, sizeof(phdr
));
321 /* Remove unwanted PT_NOTE program headers. */
322 i
= (nr_ptnote
- 1) * sizeof(Elf64_Phdr
);
324 memmove(tmp
, tmp
+i
, ((*elfsz
)-sizeof(Elf64_Ehdr
)-sizeof(Elf64_Phdr
)));
326 /* Modify e_phnum to reflect merged headers. */
327 ehdr_ptr
->e_phnum
= ehdr_ptr
->e_phnum
- nr_ptnote
+ 1;
332 /* Merges all the PT_NOTE headers into one. */
333 static int __init
merge_note_headers_elf32(char *elfptr
, size_t *elfsz
,
334 struct list_head
*vc_list
)
336 int i
, nr_ptnote
=0, rc
=0;
338 Elf32_Ehdr
*ehdr_ptr
;
339 Elf32_Phdr phdr
, *phdr_ptr
;
340 Elf32_Nhdr
*nhdr_ptr
;
341 u64 phdr_sz
= 0, note_off
;
343 ehdr_ptr
= (Elf32_Ehdr
*)elfptr
;
344 phdr_ptr
= (Elf32_Phdr
*)(elfptr
+ sizeof(Elf32_Ehdr
));
345 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++, phdr_ptr
++) {
349 u64 offset
, max_sz
, sz
, real_sz
= 0;
350 if (phdr_ptr
->p_type
!= PT_NOTE
)
353 max_sz
= phdr_ptr
->p_memsz
;
354 offset
= phdr_ptr
->p_offset
;
355 notes_section
= kmalloc(max_sz
, GFP_KERNEL
);
358 rc
= read_from_oldmem(notes_section
, max_sz
, &offset
, 0);
360 kfree(notes_section
);
363 nhdr_ptr
= notes_section
;
364 for (j
= 0; j
< max_sz
; j
+= sz
) {
365 if (nhdr_ptr
->n_namesz
== 0)
367 sz
= sizeof(Elf32_Nhdr
) +
368 ((nhdr_ptr
->n_namesz
+ 3) & ~3) +
369 ((nhdr_ptr
->n_descsz
+ 3) & ~3);
371 nhdr_ptr
= (Elf32_Nhdr
*)((char*)nhdr_ptr
+ sz
);
374 /* Add this contiguous chunk of notes section to vmcore list.*/
375 new = get_new_element();
377 kfree(notes_section
);
380 new->paddr
= phdr_ptr
->p_offset
;
382 list_add_tail(&new->list
, vc_list
);
384 kfree(notes_section
);
387 /* Prepare merged PT_NOTE program header. */
388 phdr
.p_type
= PT_NOTE
;
390 note_off
= sizeof(Elf32_Ehdr
) +
391 (ehdr_ptr
->e_phnum
- nr_ptnote
+1) * sizeof(Elf32_Phdr
);
392 phdr
.p_offset
= note_off
;
393 phdr
.p_vaddr
= phdr
.p_paddr
= 0;
394 phdr
.p_filesz
= phdr
.p_memsz
= phdr_sz
;
397 /* Add merged PT_NOTE program header*/
398 tmp
= elfptr
+ sizeof(Elf32_Ehdr
);
399 memcpy(tmp
, &phdr
, sizeof(phdr
));
402 /* Remove unwanted PT_NOTE program headers. */
403 i
= (nr_ptnote
- 1) * sizeof(Elf32_Phdr
);
405 memmove(tmp
, tmp
+i
, ((*elfsz
)-sizeof(Elf32_Ehdr
)-sizeof(Elf32_Phdr
)));
407 /* Modify e_phnum to reflect merged headers. */
408 ehdr_ptr
->e_phnum
= ehdr_ptr
->e_phnum
- nr_ptnote
+ 1;
413 /* Add memory chunks represented by program headers to vmcore list. Also update
414 * the new offset fields of exported program headers. */
415 static int __init
process_ptload_program_headers_elf64(char *elfptr
,
417 struct list_head
*vc_list
)
420 Elf64_Ehdr
*ehdr_ptr
;
421 Elf64_Phdr
*phdr_ptr
;
425 ehdr_ptr
= (Elf64_Ehdr
*)elfptr
;
426 phdr_ptr
= (Elf64_Phdr
*)(elfptr
+ sizeof(Elf64_Ehdr
)); /* PT_NOTE hdr */
428 /* First program header is PT_NOTE header. */
429 vmcore_off
= sizeof(Elf64_Ehdr
) +
430 (ehdr_ptr
->e_phnum
) * sizeof(Elf64_Phdr
) +
431 phdr_ptr
->p_memsz
; /* Note sections */
433 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++, phdr_ptr
++) {
434 if (phdr_ptr
->p_type
!= PT_LOAD
)
437 /* Add this contiguous chunk of memory to vmcore list.*/
438 new = get_new_element();
441 new->paddr
= phdr_ptr
->p_offset
;
442 new->size
= phdr_ptr
->p_memsz
;
443 list_add_tail(&new->list
, vc_list
);
445 /* Update the program header offset. */
446 phdr_ptr
->p_offset
= vmcore_off
;
447 vmcore_off
= vmcore_off
+ phdr_ptr
->p_memsz
;
452 static int __init
process_ptload_program_headers_elf32(char *elfptr
,
454 struct list_head
*vc_list
)
457 Elf32_Ehdr
*ehdr_ptr
;
458 Elf32_Phdr
*phdr_ptr
;
462 ehdr_ptr
= (Elf32_Ehdr
*)elfptr
;
463 phdr_ptr
= (Elf32_Phdr
*)(elfptr
+ sizeof(Elf32_Ehdr
)); /* PT_NOTE hdr */
465 /* First program header is PT_NOTE header. */
466 vmcore_off
= sizeof(Elf32_Ehdr
) +
467 (ehdr_ptr
->e_phnum
) * sizeof(Elf32_Phdr
) +
468 phdr_ptr
->p_memsz
; /* Note sections */
470 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++, phdr_ptr
++) {
471 if (phdr_ptr
->p_type
!= PT_LOAD
)
474 /* Add this contiguous chunk of memory to vmcore list.*/
475 new = get_new_element();
478 new->paddr
= phdr_ptr
->p_offset
;
479 new->size
= phdr_ptr
->p_memsz
;
480 list_add_tail(&new->list
, vc_list
);
482 /* Update the program header offset */
483 phdr_ptr
->p_offset
= vmcore_off
;
484 vmcore_off
= vmcore_off
+ phdr_ptr
->p_memsz
;
489 /* Sets offset fields of vmcore elements. */
490 static void __init
set_vmcore_list_offsets_elf64(char *elfptr
,
491 struct list_head
*vc_list
)
494 Elf64_Ehdr
*ehdr_ptr
;
497 ehdr_ptr
= (Elf64_Ehdr
*)elfptr
;
499 /* Skip Elf header and program headers. */
500 vmcore_off
= sizeof(Elf64_Ehdr
) +
501 (ehdr_ptr
->e_phnum
) * sizeof(Elf64_Phdr
);
503 list_for_each_entry(m
, vc_list
, list
) {
504 m
->offset
= vmcore_off
;
505 vmcore_off
+= m
->size
;
509 /* Sets offset fields of vmcore elements. */
510 static void __init
set_vmcore_list_offsets_elf32(char *elfptr
,
511 struct list_head
*vc_list
)
514 Elf32_Ehdr
*ehdr_ptr
;
517 ehdr_ptr
= (Elf32_Ehdr
*)elfptr
;
519 /* Skip Elf header and program headers. */
520 vmcore_off
= sizeof(Elf32_Ehdr
) +
521 (ehdr_ptr
->e_phnum
) * sizeof(Elf32_Phdr
);
523 list_for_each_entry(m
, vc_list
, list
) {
524 m
->offset
= vmcore_off
;
525 vmcore_off
+= m
->size
;
529 static int __init
parse_crash_elf64_headers(void)
535 addr
= elfcorehdr_addr
;
537 /* Read Elf header */
538 rc
= read_from_oldmem((char*)&ehdr
, sizeof(Elf64_Ehdr
), &addr
, 0);
542 /* Do some basic Verification. */
543 if (memcmp(ehdr
.e_ident
, ELFMAG
, SELFMAG
) != 0 ||
544 (ehdr
.e_type
!= ET_CORE
) ||
545 !vmcore_elf64_check_arch(&ehdr
) ||
546 ehdr
.e_ident
[EI_CLASS
] != ELFCLASS64
||
547 ehdr
.e_ident
[EI_VERSION
] != EV_CURRENT
||
548 ehdr
.e_version
!= EV_CURRENT
||
549 ehdr
.e_ehsize
!= sizeof(Elf64_Ehdr
) ||
550 ehdr
.e_phentsize
!= sizeof(Elf64_Phdr
) ||
552 pr_warn("Warning: Core image elf header is not sane\n");
556 /* Read in all elf headers. */
557 elfcorebuf_sz
= sizeof(Elf64_Ehdr
) + ehdr
.e_phnum
* sizeof(Elf64_Phdr
);
558 elfcorebuf
= kmalloc(elfcorebuf_sz
, GFP_KERNEL
);
561 addr
= elfcorehdr_addr
;
562 rc
= read_from_oldmem(elfcorebuf
, elfcorebuf_sz
, &addr
, 0);
568 /* Merge all PT_NOTE headers into one. */
569 rc
= merge_note_headers_elf64(elfcorebuf
, &elfcorebuf_sz
, &vmcore_list
);
574 rc
= process_ptload_program_headers_elf64(elfcorebuf
, elfcorebuf_sz
,
580 set_vmcore_list_offsets_elf64(elfcorebuf
, &vmcore_list
);
584 static int __init
parse_crash_elf32_headers(void)
590 addr
= elfcorehdr_addr
;
592 /* Read Elf header */
593 rc
= read_from_oldmem((char*)&ehdr
, sizeof(Elf32_Ehdr
), &addr
, 0);
597 /* Do some basic Verification. */
598 if (memcmp(ehdr
.e_ident
, ELFMAG
, SELFMAG
) != 0 ||
599 (ehdr
.e_type
!= ET_CORE
) ||
600 !elf_check_arch(&ehdr
) ||
601 ehdr
.e_ident
[EI_CLASS
] != ELFCLASS32
||
602 ehdr
.e_ident
[EI_VERSION
] != EV_CURRENT
||
603 ehdr
.e_version
!= EV_CURRENT
||
604 ehdr
.e_ehsize
!= sizeof(Elf32_Ehdr
) ||
605 ehdr
.e_phentsize
!= sizeof(Elf32_Phdr
) ||
607 pr_warn("Warning: Core image elf header is not sane\n");
611 /* Read in all elf headers. */
612 elfcorebuf_sz
= sizeof(Elf32_Ehdr
) + ehdr
.e_phnum
* sizeof(Elf32_Phdr
);
613 elfcorebuf
= kmalloc(elfcorebuf_sz
, GFP_KERNEL
);
616 addr
= elfcorehdr_addr
;
617 rc
= read_from_oldmem(elfcorebuf
, elfcorebuf_sz
, &addr
, 0);
623 /* Merge all PT_NOTE headers into one. */
624 rc
= merge_note_headers_elf32(elfcorebuf
, &elfcorebuf_sz
, &vmcore_list
);
629 rc
= process_ptload_program_headers_elf32(elfcorebuf
, elfcorebuf_sz
,
635 set_vmcore_list_offsets_elf32(elfcorebuf
, &vmcore_list
);
639 static int __init
parse_crash_elf_headers(void)
641 unsigned char e_ident
[EI_NIDENT
];
645 addr
= elfcorehdr_addr
;
646 rc
= read_from_oldmem(e_ident
, EI_NIDENT
, &addr
, 0);
649 if (memcmp(e_ident
, ELFMAG
, SELFMAG
) != 0) {
650 pr_warn("Warning: Core image elf header not found\n");
654 if (e_ident
[EI_CLASS
] == ELFCLASS64
) {
655 rc
= parse_crash_elf64_headers();
659 /* Determine vmcore size. */
660 vmcore_size
= get_vmcore_size_elf64(elfcorebuf
);
661 } else if (e_ident
[EI_CLASS
] == ELFCLASS32
) {
662 rc
= parse_crash_elf32_headers();
666 /* Determine vmcore size. */
667 vmcore_size
= get_vmcore_size_elf32(elfcorebuf
);
669 pr_warn("Warning: Core image elf header is not sane\n");
675 /* Init function for vmcore module. */
676 static int __init
vmcore_init(void)
680 /* If elfcorehdr= has been passed in cmdline, then capture the dump.*/
681 if (!(is_vmcore_usable()))
683 rc
= parse_crash_elf_headers();
685 pr_warn("Kdump: vmcore not initialized\n");
689 proc_vmcore
= proc_create("vmcore", S_IRUSR
, NULL
, &proc_vmcore_operations
);
691 proc_vmcore
->size
= vmcore_size
;
694 module_init(vmcore_init
)
696 /* Cleanup function for vmcore module. */
697 void vmcore_cleanup(void)
699 struct list_head
*pos
, *next
;
702 proc_remove(proc_vmcore
);
706 /* clear the vmcore list. */
707 list_for_each_safe(pos
, next
, &vmcore_list
) {
710 m
= list_entry(pos
, struct vmcore
, list
);
717 EXPORT_SYMBOL_GPL(vmcore_cleanup
);