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/a.out.h>
14 #include <linux/elf.h>
15 #include <linux/elfcore.h>
16 #include <linux/highmem.h>
17 #include <linux/bootmem.h>
18 #include <linux/init.h>
19 #include <linux/crash_dump.h>
20 #include <linux/list.h>
21 #include <asm/uaccess.h>
24 /* List representing chunks of contiguous memory areas and their offsets in
27 static LIST_HEAD(vmcore_list
);
29 /* Stores the pointer to the buffer containing kernel elf core headers. */
30 static char *elfcorebuf
;
31 static size_t elfcorebuf_sz
;
33 /* Total size of vmcore file. */
34 static u64 vmcore_size
;
36 /* Stores the physical address of elf header of crash image. */
37 unsigned long long elfcorehdr_addr
= ELFCORE_ADDR_MAX
;
39 struct proc_dir_entry
*proc_vmcore
= NULL
;
41 /* Reads a page from the oldmem device from given offset. */
42 static ssize_t
read_from_oldmem(char *buf
, size_t count
,
43 u64
*ppos
, int userbuf
)
45 unsigned long pfn
, offset
;
47 ssize_t read
= 0, tmp
;
52 offset
= (unsigned long)(*ppos
% PAGE_SIZE
);
53 pfn
= (unsigned long)(*ppos
/ PAGE_SIZE
);
54 if (pfn
> saved_max_pfn
)
58 if (count
> (PAGE_SIZE
- offset
))
59 nr_bytes
= PAGE_SIZE
- offset
;
63 tmp
= copy_oldmem_page(pfn
, buf
, nr_bytes
, offset
, userbuf
);
77 /* Maps vmcore file offset to respective physical address in memroy. */
78 static u64
map_offset_to_paddr(loff_t offset
, struct list_head
*vc_list
,
79 struct vmcore
**m_ptr
)
84 list_for_each_entry(m
, vc_list
, list
) {
87 end
= m
->offset
+ m
->size
- 1;
88 if (offset
>= start
&& offset
<= end
) {
89 paddr
= m
->paddr
+ offset
- start
;
98 /* Read from the ELF header and then the crash dump. On error, negative value is
99 * returned otherwise number of bytes read are returned.
101 static ssize_t
read_vmcore(struct file
*file
, char __user
*buffer
,
102 size_t buflen
, loff_t
*fpos
)
104 ssize_t acc
= 0, tmp
;
107 struct vmcore
*curr_m
= NULL
;
109 if (buflen
== 0 || *fpos
>= vmcore_size
)
112 /* trim buflen to not go beyond EOF */
113 if (buflen
> vmcore_size
- *fpos
)
114 buflen
= vmcore_size
- *fpos
;
116 /* Read ELF core header */
117 if (*fpos
< elfcorebuf_sz
) {
118 tsz
= elfcorebuf_sz
- *fpos
;
121 if (copy_to_user(buffer
, elfcorebuf
+ *fpos
, tsz
))
128 /* leave now if filled buffer already */
133 start
= map_offset_to_paddr(*fpos
, &vmcore_list
, &curr_m
);
136 if ((tsz
= (PAGE_SIZE
- (start
& ~PAGE_MASK
))) > buflen
)
139 /* Calculate left bytes in current memory segment. */
140 nr_bytes
= (curr_m
->size
- (start
- curr_m
->paddr
));
145 tmp
= read_from_oldmem(buffer
, tsz
, &start
, 1);
152 if (start
>= (curr_m
->paddr
+ curr_m
->size
)) {
153 if (curr_m
->list
.next
== &vmcore_list
)
155 curr_m
= list_entry(curr_m
->list
.next
,
156 struct vmcore
, list
);
157 start
= curr_m
->paddr
;
159 if ((tsz
= (PAGE_SIZE
- (start
& ~PAGE_MASK
))) > buflen
)
161 /* Calculate left bytes in current memory segment. */
162 nr_bytes
= (curr_m
->size
- (start
- curr_m
->paddr
));
169 static int open_vmcore(struct inode
*inode
, struct file
*filp
)
174 const struct file_operations proc_vmcore_operations
= {
179 static struct vmcore
* __init
get_new_element(void)
181 return kzalloc(sizeof(struct vmcore
), GFP_KERNEL
);
184 static u64 __init
get_vmcore_size_elf64(char *elfptr
)
188 Elf64_Ehdr
*ehdr_ptr
;
189 Elf64_Phdr
*phdr_ptr
;
191 ehdr_ptr
= (Elf64_Ehdr
*)elfptr
;
192 phdr_ptr
= (Elf64_Phdr
*)(elfptr
+ sizeof(Elf64_Ehdr
));
193 size
= sizeof(Elf64_Ehdr
) + ((ehdr_ptr
->e_phnum
) * sizeof(Elf64_Phdr
));
194 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++) {
195 size
+= phdr_ptr
->p_memsz
;
201 static u64 __init
get_vmcore_size_elf32(char *elfptr
)
205 Elf32_Ehdr
*ehdr_ptr
;
206 Elf32_Phdr
*phdr_ptr
;
208 ehdr_ptr
= (Elf32_Ehdr
*)elfptr
;
209 phdr_ptr
= (Elf32_Phdr
*)(elfptr
+ sizeof(Elf32_Ehdr
));
210 size
= sizeof(Elf32_Ehdr
) + ((ehdr_ptr
->e_phnum
) * sizeof(Elf32_Phdr
));
211 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++) {
212 size
+= phdr_ptr
->p_memsz
;
218 /* Merges all the PT_NOTE headers into one. */
219 static int __init
merge_note_headers_elf64(char *elfptr
, size_t *elfsz
,
220 struct list_head
*vc_list
)
222 int i
, nr_ptnote
=0, rc
=0;
224 Elf64_Ehdr
*ehdr_ptr
;
225 Elf64_Phdr phdr
, *phdr_ptr
;
226 Elf64_Nhdr
*nhdr_ptr
;
227 u64 phdr_sz
= 0, note_off
;
229 ehdr_ptr
= (Elf64_Ehdr
*)elfptr
;
230 phdr_ptr
= (Elf64_Phdr
*)(elfptr
+ sizeof(Elf64_Ehdr
));
231 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++, phdr_ptr
++) {
235 u64 offset
, max_sz
, sz
, real_sz
= 0;
236 if (phdr_ptr
->p_type
!= PT_NOTE
)
239 max_sz
= phdr_ptr
->p_memsz
;
240 offset
= phdr_ptr
->p_offset
;
241 notes_section
= kmalloc(max_sz
, GFP_KERNEL
);
244 rc
= read_from_oldmem(notes_section
, max_sz
, &offset
, 0);
246 kfree(notes_section
);
249 nhdr_ptr
= notes_section
;
250 for (j
= 0; j
< max_sz
; j
+= sz
) {
251 if (nhdr_ptr
->n_namesz
== 0)
253 sz
= sizeof(Elf64_Nhdr
) +
254 ((nhdr_ptr
->n_namesz
+ 3) & ~3) +
255 ((nhdr_ptr
->n_descsz
+ 3) & ~3);
257 nhdr_ptr
= (Elf64_Nhdr
*)((char*)nhdr_ptr
+ sz
);
260 /* Add this contiguous chunk of notes section to vmcore list.*/
261 new = get_new_element();
263 kfree(notes_section
);
266 new->paddr
= phdr_ptr
->p_offset
;
268 list_add_tail(&new->list
, vc_list
);
270 kfree(notes_section
);
273 /* Prepare merged PT_NOTE program header. */
274 phdr
.p_type
= PT_NOTE
;
276 note_off
= sizeof(Elf64_Ehdr
) +
277 (ehdr_ptr
->e_phnum
- nr_ptnote
+1) * sizeof(Elf64_Phdr
);
278 phdr
.p_offset
= note_off
;
279 phdr
.p_vaddr
= phdr
.p_paddr
= 0;
280 phdr
.p_filesz
= phdr
.p_memsz
= phdr_sz
;
283 /* Add merged PT_NOTE program header*/
284 tmp
= elfptr
+ sizeof(Elf64_Ehdr
);
285 memcpy(tmp
, &phdr
, sizeof(phdr
));
288 /* Remove unwanted PT_NOTE program headers. */
289 i
= (nr_ptnote
- 1) * sizeof(Elf64_Phdr
);
291 memmove(tmp
, tmp
+i
, ((*elfsz
)-sizeof(Elf64_Ehdr
)-sizeof(Elf64_Phdr
)));
293 /* Modify e_phnum to reflect merged headers. */
294 ehdr_ptr
->e_phnum
= ehdr_ptr
->e_phnum
- nr_ptnote
+ 1;
299 /* Merges all the PT_NOTE headers into one. */
300 static int __init
merge_note_headers_elf32(char *elfptr
, size_t *elfsz
,
301 struct list_head
*vc_list
)
303 int i
, nr_ptnote
=0, rc
=0;
305 Elf32_Ehdr
*ehdr_ptr
;
306 Elf32_Phdr phdr
, *phdr_ptr
;
307 Elf32_Nhdr
*nhdr_ptr
;
308 u64 phdr_sz
= 0, note_off
;
310 ehdr_ptr
= (Elf32_Ehdr
*)elfptr
;
311 phdr_ptr
= (Elf32_Phdr
*)(elfptr
+ sizeof(Elf32_Ehdr
));
312 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++, phdr_ptr
++) {
316 u64 offset
, max_sz
, sz
, real_sz
= 0;
317 if (phdr_ptr
->p_type
!= PT_NOTE
)
320 max_sz
= phdr_ptr
->p_memsz
;
321 offset
= phdr_ptr
->p_offset
;
322 notes_section
= kmalloc(max_sz
, GFP_KERNEL
);
325 rc
= read_from_oldmem(notes_section
, max_sz
, &offset
, 0);
327 kfree(notes_section
);
330 nhdr_ptr
= notes_section
;
331 for (j
= 0; j
< max_sz
; j
+= sz
) {
332 if (nhdr_ptr
->n_namesz
== 0)
334 sz
= sizeof(Elf32_Nhdr
) +
335 ((nhdr_ptr
->n_namesz
+ 3) & ~3) +
336 ((nhdr_ptr
->n_descsz
+ 3) & ~3);
338 nhdr_ptr
= (Elf32_Nhdr
*)((char*)nhdr_ptr
+ sz
);
341 /* Add this contiguous chunk of notes section to vmcore list.*/
342 new = get_new_element();
344 kfree(notes_section
);
347 new->paddr
= phdr_ptr
->p_offset
;
349 list_add_tail(&new->list
, vc_list
);
351 kfree(notes_section
);
354 /* Prepare merged PT_NOTE program header. */
355 phdr
.p_type
= PT_NOTE
;
357 note_off
= sizeof(Elf32_Ehdr
) +
358 (ehdr_ptr
->e_phnum
- nr_ptnote
+1) * sizeof(Elf32_Phdr
);
359 phdr
.p_offset
= note_off
;
360 phdr
.p_vaddr
= phdr
.p_paddr
= 0;
361 phdr
.p_filesz
= phdr
.p_memsz
= phdr_sz
;
364 /* Add merged PT_NOTE program header*/
365 tmp
= elfptr
+ sizeof(Elf32_Ehdr
);
366 memcpy(tmp
, &phdr
, sizeof(phdr
));
369 /* Remove unwanted PT_NOTE program headers. */
370 i
= (nr_ptnote
- 1) * sizeof(Elf32_Phdr
);
372 memmove(tmp
, tmp
+i
, ((*elfsz
)-sizeof(Elf32_Ehdr
)-sizeof(Elf32_Phdr
)));
374 /* Modify e_phnum to reflect merged headers. */
375 ehdr_ptr
->e_phnum
= ehdr_ptr
->e_phnum
- nr_ptnote
+ 1;
380 /* Add memory chunks represented by program headers to vmcore list. Also update
381 * the new offset fields of exported program headers. */
382 static int __init
process_ptload_program_headers_elf64(char *elfptr
,
384 struct list_head
*vc_list
)
387 Elf64_Ehdr
*ehdr_ptr
;
388 Elf64_Phdr
*phdr_ptr
;
392 ehdr_ptr
= (Elf64_Ehdr
*)elfptr
;
393 phdr_ptr
= (Elf64_Phdr
*)(elfptr
+ sizeof(Elf64_Ehdr
)); /* PT_NOTE hdr */
395 /* First program header is PT_NOTE header. */
396 vmcore_off
= sizeof(Elf64_Ehdr
) +
397 (ehdr_ptr
->e_phnum
) * sizeof(Elf64_Phdr
) +
398 phdr_ptr
->p_memsz
; /* Note sections */
400 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++, phdr_ptr
++) {
401 if (phdr_ptr
->p_type
!= PT_LOAD
)
404 /* Add this contiguous chunk of memory to vmcore list.*/
405 new = get_new_element();
408 new->paddr
= phdr_ptr
->p_offset
;
409 new->size
= phdr_ptr
->p_memsz
;
410 list_add_tail(&new->list
, vc_list
);
412 /* Update the program header offset. */
413 phdr_ptr
->p_offset
= vmcore_off
;
414 vmcore_off
= vmcore_off
+ phdr_ptr
->p_memsz
;
419 static int __init
process_ptload_program_headers_elf32(char *elfptr
,
421 struct list_head
*vc_list
)
424 Elf32_Ehdr
*ehdr_ptr
;
425 Elf32_Phdr
*phdr_ptr
;
429 ehdr_ptr
= (Elf32_Ehdr
*)elfptr
;
430 phdr_ptr
= (Elf32_Phdr
*)(elfptr
+ sizeof(Elf32_Ehdr
)); /* PT_NOTE hdr */
432 /* First program header is PT_NOTE header. */
433 vmcore_off
= sizeof(Elf32_Ehdr
) +
434 (ehdr_ptr
->e_phnum
) * sizeof(Elf32_Phdr
) +
435 phdr_ptr
->p_memsz
; /* Note sections */
437 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++, phdr_ptr
++) {
438 if (phdr_ptr
->p_type
!= PT_LOAD
)
441 /* Add this contiguous chunk of memory to vmcore list.*/
442 new = get_new_element();
445 new->paddr
= phdr_ptr
->p_offset
;
446 new->size
= phdr_ptr
->p_memsz
;
447 list_add_tail(&new->list
, vc_list
);
449 /* Update the program header offset */
450 phdr_ptr
->p_offset
= vmcore_off
;
451 vmcore_off
= vmcore_off
+ phdr_ptr
->p_memsz
;
456 /* Sets offset fields of vmcore elements. */
457 static void __init
set_vmcore_list_offsets_elf64(char *elfptr
,
458 struct list_head
*vc_list
)
461 Elf64_Ehdr
*ehdr_ptr
;
464 ehdr_ptr
= (Elf64_Ehdr
*)elfptr
;
466 /* Skip Elf header and program headers. */
467 vmcore_off
= sizeof(Elf64_Ehdr
) +
468 (ehdr_ptr
->e_phnum
) * sizeof(Elf64_Phdr
);
470 list_for_each_entry(m
, vc_list
, list
) {
471 m
->offset
= vmcore_off
;
472 vmcore_off
+= m
->size
;
476 /* Sets offset fields of vmcore elements. */
477 static void __init
set_vmcore_list_offsets_elf32(char *elfptr
,
478 struct list_head
*vc_list
)
481 Elf32_Ehdr
*ehdr_ptr
;
484 ehdr_ptr
= (Elf32_Ehdr
*)elfptr
;
486 /* Skip Elf header and program headers. */
487 vmcore_off
= sizeof(Elf32_Ehdr
) +
488 (ehdr_ptr
->e_phnum
) * sizeof(Elf32_Phdr
);
490 list_for_each_entry(m
, vc_list
, list
) {
491 m
->offset
= vmcore_off
;
492 vmcore_off
+= m
->size
;
496 static int __init
parse_crash_elf64_headers(void)
502 addr
= elfcorehdr_addr
;
504 /* Read Elf header */
505 rc
= read_from_oldmem((char*)&ehdr
, sizeof(Elf64_Ehdr
), &addr
, 0);
509 /* Do some basic Verification. */
510 if (memcmp(ehdr
.e_ident
, ELFMAG
, SELFMAG
) != 0 ||
511 (ehdr
.e_type
!= ET_CORE
) ||
512 !vmcore_elf_check_arch(&ehdr
) ||
513 ehdr
.e_ident
[EI_CLASS
] != ELFCLASS64
||
514 ehdr
.e_ident
[EI_VERSION
] != EV_CURRENT
||
515 ehdr
.e_version
!= EV_CURRENT
||
516 ehdr
.e_ehsize
!= sizeof(Elf64_Ehdr
) ||
517 ehdr
.e_phentsize
!= sizeof(Elf64_Phdr
) ||
519 printk(KERN_WARNING
"Warning: Core image elf header is not"
524 /* Read in all elf headers. */
525 elfcorebuf_sz
= sizeof(Elf64_Ehdr
) + ehdr
.e_phnum
* sizeof(Elf64_Phdr
);
526 elfcorebuf
= kmalloc(elfcorebuf_sz
, GFP_KERNEL
);
529 addr
= elfcorehdr_addr
;
530 rc
= read_from_oldmem(elfcorebuf
, elfcorebuf_sz
, &addr
, 0);
536 /* Merge all PT_NOTE headers into one. */
537 rc
= merge_note_headers_elf64(elfcorebuf
, &elfcorebuf_sz
, &vmcore_list
);
542 rc
= process_ptload_program_headers_elf64(elfcorebuf
, elfcorebuf_sz
,
548 set_vmcore_list_offsets_elf64(elfcorebuf
, &vmcore_list
);
552 static int __init
parse_crash_elf32_headers(void)
558 addr
= elfcorehdr_addr
;
560 /* Read Elf header */
561 rc
= read_from_oldmem((char*)&ehdr
, sizeof(Elf32_Ehdr
), &addr
, 0);
565 /* Do some basic Verification. */
566 if (memcmp(ehdr
.e_ident
, ELFMAG
, SELFMAG
) != 0 ||
567 (ehdr
.e_type
!= ET_CORE
) ||
568 !elf_check_arch(&ehdr
) ||
569 ehdr
.e_ident
[EI_CLASS
] != ELFCLASS32
||
570 ehdr
.e_ident
[EI_VERSION
] != EV_CURRENT
||
571 ehdr
.e_version
!= EV_CURRENT
||
572 ehdr
.e_ehsize
!= sizeof(Elf32_Ehdr
) ||
573 ehdr
.e_phentsize
!= sizeof(Elf32_Phdr
) ||
575 printk(KERN_WARNING
"Warning: Core image elf header is not"
580 /* Read in all elf headers. */
581 elfcorebuf_sz
= sizeof(Elf32_Ehdr
) + ehdr
.e_phnum
* sizeof(Elf32_Phdr
);
582 elfcorebuf
= kmalloc(elfcorebuf_sz
, GFP_KERNEL
);
585 addr
= elfcorehdr_addr
;
586 rc
= read_from_oldmem(elfcorebuf
, elfcorebuf_sz
, &addr
, 0);
592 /* Merge all PT_NOTE headers into one. */
593 rc
= merge_note_headers_elf32(elfcorebuf
, &elfcorebuf_sz
, &vmcore_list
);
598 rc
= process_ptload_program_headers_elf32(elfcorebuf
, elfcorebuf_sz
,
604 set_vmcore_list_offsets_elf32(elfcorebuf
, &vmcore_list
);
608 static int __init
parse_crash_elf_headers(void)
610 unsigned char e_ident
[EI_NIDENT
];
614 addr
= elfcorehdr_addr
;
615 rc
= read_from_oldmem(e_ident
, EI_NIDENT
, &addr
, 0);
618 if (memcmp(e_ident
, ELFMAG
, SELFMAG
) != 0) {
619 printk(KERN_WARNING
"Warning: Core image elf header"
624 if (e_ident
[EI_CLASS
] == ELFCLASS64
) {
625 rc
= parse_crash_elf64_headers();
629 /* Determine vmcore size. */
630 vmcore_size
= get_vmcore_size_elf64(elfcorebuf
);
631 } else if (e_ident
[EI_CLASS
] == ELFCLASS32
) {
632 rc
= parse_crash_elf32_headers();
636 /* Determine vmcore size. */
637 vmcore_size
= get_vmcore_size_elf32(elfcorebuf
);
639 printk(KERN_WARNING
"Warning: Core image elf header is not"
646 /* Init function for vmcore module. */
647 static int __init
vmcore_init(void)
651 /* If elfcorehdr= has been passed in cmdline, then capture the dump.*/
652 if (!(elfcorehdr_addr
< ELFCORE_ADDR_MAX
))
654 rc
= parse_crash_elf_headers();
656 printk(KERN_WARNING
"Kdump: vmcore not initialized\n");
660 /* Initialize /proc/vmcore size if proc is already up. */
662 proc_vmcore
->size
= vmcore_size
;
665 module_init(vmcore_init
)