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/proc_fs.h>
12 #include <linux/user.h>
13 #include <linux/elf.h>
14 #include <linux/elfcore.h>
15 #include <linux/highmem.h>
16 #include <linux/bootmem.h>
17 #include <linux/init.h>
18 #include <linux/crash_dump.h>
19 #include <linux/list.h>
20 #include <asm/uaccess.h>
23 /* List representing chunks of contiguous memory areas and their offsets in
26 static LIST_HEAD(vmcore_list
);
28 /* Stores the pointer to the buffer containing kernel elf core headers. */
29 static char *elfcorebuf
;
30 static size_t elfcorebuf_sz
;
32 /* Total size of vmcore file. */
33 static u64 vmcore_size
;
35 struct proc_dir_entry
*proc_vmcore
= NULL
;
37 /* Reads a page from the oldmem device from given offset. */
38 static ssize_t
read_from_oldmem(char *buf
, size_t count
,
39 u64
*ppos
, int userbuf
)
41 unsigned long pfn
, offset
;
43 ssize_t read
= 0, tmp
;
48 offset
= (unsigned long)(*ppos
% PAGE_SIZE
);
49 pfn
= (unsigned long)(*ppos
/ PAGE_SIZE
);
50 if (pfn
> saved_max_pfn
)
54 if (count
> (PAGE_SIZE
- offset
))
55 nr_bytes
= PAGE_SIZE
- offset
;
59 tmp
= copy_oldmem_page(pfn
, buf
, nr_bytes
, offset
, userbuf
);
73 /* Maps vmcore file offset to respective physical address in memroy. */
74 static u64
map_offset_to_paddr(loff_t offset
, struct list_head
*vc_list
,
75 struct vmcore
**m_ptr
)
80 list_for_each_entry(m
, vc_list
, list
) {
83 end
= m
->offset
+ m
->size
- 1;
84 if (offset
>= start
&& offset
<= end
) {
85 paddr
= m
->paddr
+ offset
- start
;
94 /* Read from the ELF header and then the crash dump. On error, negative value is
95 * returned otherwise number of bytes read are returned.
97 static ssize_t
read_vmcore(struct file
*file
, char __user
*buffer
,
98 size_t buflen
, loff_t
*fpos
)
100 ssize_t acc
= 0, tmp
;
103 struct vmcore
*curr_m
= NULL
;
105 if (buflen
== 0 || *fpos
>= vmcore_size
)
108 /* trim buflen to not go beyond EOF */
109 if (buflen
> vmcore_size
- *fpos
)
110 buflen
= vmcore_size
- *fpos
;
112 /* Read ELF core header */
113 if (*fpos
< elfcorebuf_sz
) {
114 tsz
= elfcorebuf_sz
- *fpos
;
117 if (copy_to_user(buffer
, elfcorebuf
+ *fpos
, tsz
))
124 /* leave now if filled buffer already */
129 start
= map_offset_to_paddr(*fpos
, &vmcore_list
, &curr_m
);
132 if ((tsz
= (PAGE_SIZE
- (start
& ~PAGE_MASK
))) > buflen
)
135 /* Calculate left bytes in current memory segment. */
136 nr_bytes
= (curr_m
->size
- (start
- curr_m
->paddr
));
141 tmp
= read_from_oldmem(buffer
, tsz
, &start
, 1);
148 if (start
>= (curr_m
->paddr
+ curr_m
->size
)) {
149 if (curr_m
->list
.next
== &vmcore_list
)
151 curr_m
= list_entry(curr_m
->list
.next
,
152 struct vmcore
, list
);
153 start
= curr_m
->paddr
;
155 if ((tsz
= (PAGE_SIZE
- (start
& ~PAGE_MASK
))) > buflen
)
157 /* Calculate left bytes in current memory segment. */
158 nr_bytes
= (curr_m
->size
- (start
- curr_m
->paddr
));
165 const struct file_operations proc_vmcore_operations
= {
169 static struct vmcore
* __init
get_new_element(void)
173 p
= kmalloc(sizeof(*p
), GFP_KERNEL
);
175 memset(p
, 0, sizeof(*p
));
179 static u64 __init
get_vmcore_size_elf64(char *elfptr
)
183 Elf64_Ehdr
*ehdr_ptr
;
184 Elf64_Phdr
*phdr_ptr
;
186 ehdr_ptr
= (Elf64_Ehdr
*)elfptr
;
187 phdr_ptr
= (Elf64_Phdr
*)(elfptr
+ sizeof(Elf64_Ehdr
));
188 size
= sizeof(Elf64_Ehdr
) + ((ehdr_ptr
->e_phnum
) * sizeof(Elf64_Phdr
));
189 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++) {
190 size
+= phdr_ptr
->p_memsz
;
196 static u64 __init
get_vmcore_size_elf32(char *elfptr
)
200 Elf32_Ehdr
*ehdr_ptr
;
201 Elf32_Phdr
*phdr_ptr
;
203 ehdr_ptr
= (Elf32_Ehdr
*)elfptr
;
204 phdr_ptr
= (Elf32_Phdr
*)(elfptr
+ sizeof(Elf32_Ehdr
));
205 size
= sizeof(Elf32_Ehdr
) + ((ehdr_ptr
->e_phnum
) * sizeof(Elf32_Phdr
));
206 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++) {
207 size
+= phdr_ptr
->p_memsz
;
213 /* Merges all the PT_NOTE headers into one. */
214 static int __init
merge_note_headers_elf64(char *elfptr
, size_t *elfsz
,
215 struct list_head
*vc_list
)
217 int i
, nr_ptnote
=0, rc
=0;
219 Elf64_Ehdr
*ehdr_ptr
;
220 Elf64_Phdr phdr
, *phdr_ptr
;
221 Elf64_Nhdr
*nhdr_ptr
;
222 u64 phdr_sz
= 0, note_off
;
224 ehdr_ptr
= (Elf64_Ehdr
*)elfptr
;
225 phdr_ptr
= (Elf64_Phdr
*)(elfptr
+ sizeof(Elf64_Ehdr
));
226 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++, phdr_ptr
++) {
230 u64 offset
, max_sz
, sz
, real_sz
= 0;
231 if (phdr_ptr
->p_type
!= PT_NOTE
)
234 max_sz
= phdr_ptr
->p_memsz
;
235 offset
= phdr_ptr
->p_offset
;
236 notes_section
= kmalloc(max_sz
, GFP_KERNEL
);
239 rc
= read_from_oldmem(notes_section
, max_sz
, &offset
, 0);
241 kfree(notes_section
);
244 nhdr_ptr
= notes_section
;
245 for (j
= 0; j
< max_sz
; j
+= sz
) {
246 if (nhdr_ptr
->n_namesz
== 0)
248 sz
= sizeof(Elf64_Nhdr
) +
249 ((nhdr_ptr
->n_namesz
+ 3) & ~3) +
250 ((nhdr_ptr
->n_descsz
+ 3) & ~3);
252 nhdr_ptr
= (Elf64_Nhdr
*)((char*)nhdr_ptr
+ sz
);
255 /* Add this contiguous chunk of notes section to vmcore list.*/
256 new = get_new_element();
258 kfree(notes_section
);
261 new->paddr
= phdr_ptr
->p_offset
;
263 list_add_tail(&new->list
, vc_list
);
265 kfree(notes_section
);
268 /* Prepare merged PT_NOTE program header. */
269 phdr
.p_type
= PT_NOTE
;
271 note_off
= sizeof(Elf64_Ehdr
) +
272 (ehdr_ptr
->e_phnum
- nr_ptnote
+1) * sizeof(Elf64_Phdr
);
273 phdr
.p_offset
= note_off
;
274 phdr
.p_vaddr
= phdr
.p_paddr
= 0;
275 phdr
.p_filesz
= phdr
.p_memsz
= phdr_sz
;
278 /* Add merged PT_NOTE program header*/
279 tmp
= elfptr
+ sizeof(Elf64_Ehdr
);
280 memcpy(tmp
, &phdr
, sizeof(phdr
));
283 /* Remove unwanted PT_NOTE program headers. */
284 i
= (nr_ptnote
- 1) * sizeof(Elf64_Phdr
);
286 memmove(tmp
, tmp
+i
, ((*elfsz
)-sizeof(Elf64_Ehdr
)-sizeof(Elf64_Phdr
)));
288 /* Modify e_phnum to reflect merged headers. */
289 ehdr_ptr
->e_phnum
= ehdr_ptr
->e_phnum
- nr_ptnote
+ 1;
294 /* Merges all the PT_NOTE headers into one. */
295 static int __init
merge_note_headers_elf32(char *elfptr
, size_t *elfsz
,
296 struct list_head
*vc_list
)
298 int i
, nr_ptnote
=0, rc
=0;
300 Elf32_Ehdr
*ehdr_ptr
;
301 Elf32_Phdr phdr
, *phdr_ptr
;
302 Elf32_Nhdr
*nhdr_ptr
;
303 u64 phdr_sz
= 0, note_off
;
305 ehdr_ptr
= (Elf32_Ehdr
*)elfptr
;
306 phdr_ptr
= (Elf32_Phdr
*)(elfptr
+ sizeof(Elf32_Ehdr
));
307 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++, phdr_ptr
++) {
311 u64 offset
, max_sz
, sz
, real_sz
= 0;
312 if (phdr_ptr
->p_type
!= PT_NOTE
)
315 max_sz
= phdr_ptr
->p_memsz
;
316 offset
= phdr_ptr
->p_offset
;
317 notes_section
= kmalloc(max_sz
, GFP_KERNEL
);
320 rc
= read_from_oldmem(notes_section
, max_sz
, &offset
, 0);
322 kfree(notes_section
);
325 nhdr_ptr
= notes_section
;
326 for (j
= 0; j
< max_sz
; j
+= sz
) {
327 if (nhdr_ptr
->n_namesz
== 0)
329 sz
= sizeof(Elf32_Nhdr
) +
330 ((nhdr_ptr
->n_namesz
+ 3) & ~3) +
331 ((nhdr_ptr
->n_descsz
+ 3) & ~3);
333 nhdr_ptr
= (Elf32_Nhdr
*)((char*)nhdr_ptr
+ sz
);
336 /* Add this contiguous chunk of notes section to vmcore list.*/
337 new = get_new_element();
339 kfree(notes_section
);
342 new->paddr
= phdr_ptr
->p_offset
;
344 list_add_tail(&new->list
, vc_list
);
346 kfree(notes_section
);
349 /* Prepare merged PT_NOTE program header. */
350 phdr
.p_type
= PT_NOTE
;
352 note_off
= sizeof(Elf32_Ehdr
) +
353 (ehdr_ptr
->e_phnum
- nr_ptnote
+1) * sizeof(Elf32_Phdr
);
354 phdr
.p_offset
= note_off
;
355 phdr
.p_vaddr
= phdr
.p_paddr
= 0;
356 phdr
.p_filesz
= phdr
.p_memsz
= phdr_sz
;
359 /* Add merged PT_NOTE program header*/
360 tmp
= elfptr
+ sizeof(Elf32_Ehdr
);
361 memcpy(tmp
, &phdr
, sizeof(phdr
));
364 /* Remove unwanted PT_NOTE program headers. */
365 i
= (nr_ptnote
- 1) * sizeof(Elf32_Phdr
);
367 memmove(tmp
, tmp
+i
, ((*elfsz
)-sizeof(Elf32_Ehdr
)-sizeof(Elf32_Phdr
)));
369 /* Modify e_phnum to reflect merged headers. */
370 ehdr_ptr
->e_phnum
= ehdr_ptr
->e_phnum
- nr_ptnote
+ 1;
375 /* Add memory chunks represented by program headers to vmcore list. Also update
376 * the new offset fields of exported program headers. */
377 static int __init
process_ptload_program_headers_elf64(char *elfptr
,
379 struct list_head
*vc_list
)
382 Elf64_Ehdr
*ehdr_ptr
;
383 Elf64_Phdr
*phdr_ptr
;
387 ehdr_ptr
= (Elf64_Ehdr
*)elfptr
;
388 phdr_ptr
= (Elf64_Phdr
*)(elfptr
+ sizeof(Elf64_Ehdr
)); /* PT_NOTE hdr */
390 /* First program header is PT_NOTE header. */
391 vmcore_off
= sizeof(Elf64_Ehdr
) +
392 (ehdr_ptr
->e_phnum
) * sizeof(Elf64_Phdr
) +
393 phdr_ptr
->p_memsz
; /* Note sections */
395 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++, phdr_ptr
++) {
396 if (phdr_ptr
->p_type
!= PT_LOAD
)
399 /* Add this contiguous chunk of memory to vmcore list.*/
400 new = get_new_element();
403 new->paddr
= phdr_ptr
->p_offset
;
404 new->size
= phdr_ptr
->p_memsz
;
405 list_add_tail(&new->list
, vc_list
);
407 /* Update the program header offset. */
408 phdr_ptr
->p_offset
= vmcore_off
;
409 vmcore_off
= vmcore_off
+ phdr_ptr
->p_memsz
;
414 static int __init
process_ptload_program_headers_elf32(char *elfptr
,
416 struct list_head
*vc_list
)
419 Elf32_Ehdr
*ehdr_ptr
;
420 Elf32_Phdr
*phdr_ptr
;
424 ehdr_ptr
= (Elf32_Ehdr
*)elfptr
;
425 phdr_ptr
= (Elf32_Phdr
*)(elfptr
+ sizeof(Elf32_Ehdr
)); /* PT_NOTE hdr */
427 /* First program header is PT_NOTE header. */
428 vmcore_off
= sizeof(Elf32_Ehdr
) +
429 (ehdr_ptr
->e_phnum
) * sizeof(Elf32_Phdr
) +
430 phdr_ptr
->p_memsz
; /* Note sections */
432 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++, phdr_ptr
++) {
433 if (phdr_ptr
->p_type
!= PT_LOAD
)
436 /* Add this contiguous chunk of memory to vmcore list.*/
437 new = get_new_element();
440 new->paddr
= phdr_ptr
->p_offset
;
441 new->size
= phdr_ptr
->p_memsz
;
442 list_add_tail(&new->list
, vc_list
);
444 /* Update the program header offset */
445 phdr_ptr
->p_offset
= vmcore_off
;
446 vmcore_off
= vmcore_off
+ phdr_ptr
->p_memsz
;
451 /* Sets offset fields of vmcore elements. */
452 static void __init
set_vmcore_list_offsets_elf64(char *elfptr
,
453 struct list_head
*vc_list
)
456 Elf64_Ehdr
*ehdr_ptr
;
459 ehdr_ptr
= (Elf64_Ehdr
*)elfptr
;
461 /* Skip Elf header and program headers. */
462 vmcore_off
= sizeof(Elf64_Ehdr
) +
463 (ehdr_ptr
->e_phnum
) * sizeof(Elf64_Phdr
);
465 list_for_each_entry(m
, vc_list
, list
) {
466 m
->offset
= vmcore_off
;
467 vmcore_off
+= m
->size
;
471 /* Sets offset fields of vmcore elements. */
472 static void __init
set_vmcore_list_offsets_elf32(char *elfptr
,
473 struct list_head
*vc_list
)
476 Elf32_Ehdr
*ehdr_ptr
;
479 ehdr_ptr
= (Elf32_Ehdr
*)elfptr
;
481 /* Skip Elf header and program headers. */
482 vmcore_off
= sizeof(Elf32_Ehdr
) +
483 (ehdr_ptr
->e_phnum
) * sizeof(Elf32_Phdr
);
485 list_for_each_entry(m
, vc_list
, list
) {
486 m
->offset
= vmcore_off
;
487 vmcore_off
+= m
->size
;
491 static int __init
parse_crash_elf64_headers(void)
497 addr
= elfcorehdr_addr
;
499 /* Read Elf header */
500 rc
= read_from_oldmem((char*)&ehdr
, sizeof(Elf64_Ehdr
), &addr
, 0);
504 /* Do some basic Verification. */
505 if (memcmp(ehdr
.e_ident
, ELFMAG
, SELFMAG
) != 0 ||
506 (ehdr
.e_type
!= ET_CORE
) ||
507 !vmcore_elf_check_arch(&ehdr
) ||
508 ehdr
.e_ident
[EI_CLASS
] != ELFCLASS64
||
509 ehdr
.e_ident
[EI_VERSION
] != EV_CURRENT
||
510 ehdr
.e_version
!= EV_CURRENT
||
511 ehdr
.e_ehsize
!= sizeof(Elf64_Ehdr
) ||
512 ehdr
.e_phentsize
!= sizeof(Elf64_Phdr
) ||
514 printk(KERN_WARNING
"Warning: Core image elf header is not"
519 /* Read in all elf headers. */
520 elfcorebuf_sz
= sizeof(Elf64_Ehdr
) + ehdr
.e_phnum
* sizeof(Elf64_Phdr
);
521 elfcorebuf
= kmalloc(elfcorebuf_sz
, GFP_KERNEL
);
524 addr
= elfcorehdr_addr
;
525 rc
= read_from_oldmem(elfcorebuf
, elfcorebuf_sz
, &addr
, 0);
531 /* Merge all PT_NOTE headers into one. */
532 rc
= merge_note_headers_elf64(elfcorebuf
, &elfcorebuf_sz
, &vmcore_list
);
537 rc
= process_ptload_program_headers_elf64(elfcorebuf
, elfcorebuf_sz
,
543 set_vmcore_list_offsets_elf64(elfcorebuf
, &vmcore_list
);
547 static int __init
parse_crash_elf32_headers(void)
553 addr
= elfcorehdr_addr
;
555 /* Read Elf header */
556 rc
= read_from_oldmem((char*)&ehdr
, sizeof(Elf32_Ehdr
), &addr
, 0);
560 /* Do some basic Verification. */
561 if (memcmp(ehdr
.e_ident
, ELFMAG
, SELFMAG
) != 0 ||
562 (ehdr
.e_type
!= ET_CORE
) ||
563 !elf_check_arch(&ehdr
) ||
564 ehdr
.e_ident
[EI_CLASS
] != ELFCLASS32
||
565 ehdr
.e_ident
[EI_VERSION
] != EV_CURRENT
||
566 ehdr
.e_version
!= EV_CURRENT
||
567 ehdr
.e_ehsize
!= sizeof(Elf32_Ehdr
) ||
568 ehdr
.e_phentsize
!= sizeof(Elf32_Phdr
) ||
570 printk(KERN_WARNING
"Warning: Core image elf header is not"
575 /* Read in all elf headers. */
576 elfcorebuf_sz
= sizeof(Elf32_Ehdr
) + ehdr
.e_phnum
* sizeof(Elf32_Phdr
);
577 elfcorebuf
= kmalloc(elfcorebuf_sz
, GFP_KERNEL
);
580 addr
= elfcorehdr_addr
;
581 rc
= read_from_oldmem(elfcorebuf
, elfcorebuf_sz
, &addr
, 0);
587 /* Merge all PT_NOTE headers into one. */
588 rc
= merge_note_headers_elf32(elfcorebuf
, &elfcorebuf_sz
, &vmcore_list
);
593 rc
= process_ptload_program_headers_elf32(elfcorebuf
, elfcorebuf_sz
,
599 set_vmcore_list_offsets_elf32(elfcorebuf
, &vmcore_list
);
603 static int __init
parse_crash_elf_headers(void)
605 unsigned char e_ident
[EI_NIDENT
];
609 addr
= elfcorehdr_addr
;
610 rc
= read_from_oldmem(e_ident
, EI_NIDENT
, &addr
, 0);
613 if (memcmp(e_ident
, ELFMAG
, SELFMAG
) != 0) {
614 printk(KERN_WARNING
"Warning: Core image elf header"
619 if (e_ident
[EI_CLASS
] == ELFCLASS64
) {
620 rc
= parse_crash_elf64_headers();
624 /* Determine vmcore size. */
625 vmcore_size
= get_vmcore_size_elf64(elfcorebuf
);
626 } else if (e_ident
[EI_CLASS
] == ELFCLASS32
) {
627 rc
= parse_crash_elf32_headers();
631 /* Determine vmcore size. */
632 vmcore_size
= get_vmcore_size_elf32(elfcorebuf
);
634 printk(KERN_WARNING
"Warning: Core image elf header is not"
641 /* Init function for vmcore module. */
642 static int __init
vmcore_init(void)
646 /* If elfcorehdr= has been passed in cmdline, then capture the dump.*/
647 if (!(is_vmcore_usable()))
649 rc
= parse_crash_elf_headers();
651 printk(KERN_WARNING
"Kdump: vmcore not initialized\n");
655 /* Initialize /proc/vmcore size if proc is already up. */
657 proc_vmcore
->size
= vmcore_size
;
660 module_init(vmcore_init
)