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)
183 p
= kmalloc(sizeof(*p
), GFP_KERNEL
);
185 memset(p
, 0, sizeof(*p
));
189 static u64 __init
get_vmcore_size_elf64(char *elfptr
)
193 Elf64_Ehdr
*ehdr_ptr
;
194 Elf64_Phdr
*phdr_ptr
;
196 ehdr_ptr
= (Elf64_Ehdr
*)elfptr
;
197 phdr_ptr
= (Elf64_Phdr
*)(elfptr
+ sizeof(Elf64_Ehdr
));
198 size
= sizeof(Elf64_Ehdr
) + ((ehdr_ptr
->e_phnum
) * sizeof(Elf64_Phdr
));
199 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++) {
200 size
+= phdr_ptr
->p_memsz
;
206 static u64 __init
get_vmcore_size_elf32(char *elfptr
)
210 Elf32_Ehdr
*ehdr_ptr
;
211 Elf32_Phdr
*phdr_ptr
;
213 ehdr_ptr
= (Elf32_Ehdr
*)elfptr
;
214 phdr_ptr
= (Elf32_Phdr
*)(elfptr
+ sizeof(Elf32_Ehdr
));
215 size
= sizeof(Elf32_Ehdr
) + ((ehdr_ptr
->e_phnum
) * sizeof(Elf32_Phdr
));
216 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++) {
217 size
+= phdr_ptr
->p_memsz
;
223 /* Merges all the PT_NOTE headers into one. */
224 static int __init
merge_note_headers_elf64(char *elfptr
, size_t *elfsz
,
225 struct list_head
*vc_list
)
227 int i
, nr_ptnote
=0, rc
=0;
229 Elf64_Ehdr
*ehdr_ptr
;
230 Elf64_Phdr phdr
, *phdr_ptr
;
231 Elf64_Nhdr
*nhdr_ptr
;
232 u64 phdr_sz
= 0, note_off
;
234 ehdr_ptr
= (Elf64_Ehdr
*)elfptr
;
235 phdr_ptr
= (Elf64_Phdr
*)(elfptr
+ sizeof(Elf64_Ehdr
));
236 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++, phdr_ptr
++) {
240 u64 offset
, max_sz
, sz
, real_sz
= 0;
241 if (phdr_ptr
->p_type
!= PT_NOTE
)
244 max_sz
= phdr_ptr
->p_memsz
;
245 offset
= phdr_ptr
->p_offset
;
246 notes_section
= kmalloc(max_sz
, GFP_KERNEL
);
249 rc
= read_from_oldmem(notes_section
, max_sz
, &offset
, 0);
251 kfree(notes_section
);
254 nhdr_ptr
= notes_section
;
255 for (j
= 0; j
< max_sz
; j
+= sz
) {
256 if (nhdr_ptr
->n_namesz
== 0)
258 sz
= sizeof(Elf64_Nhdr
) +
259 ((nhdr_ptr
->n_namesz
+ 3) & ~3) +
260 ((nhdr_ptr
->n_descsz
+ 3) & ~3);
262 nhdr_ptr
= (Elf64_Nhdr
*)((char*)nhdr_ptr
+ sz
);
265 /* Add this contiguous chunk of notes section to vmcore list.*/
266 new = get_new_element();
268 kfree(notes_section
);
271 new->paddr
= phdr_ptr
->p_offset
;
273 list_add_tail(&new->list
, vc_list
);
275 kfree(notes_section
);
278 /* Prepare merged PT_NOTE program header. */
279 phdr
.p_type
= PT_NOTE
;
281 note_off
= sizeof(Elf64_Ehdr
) +
282 (ehdr_ptr
->e_phnum
- nr_ptnote
+1) * sizeof(Elf64_Phdr
);
283 phdr
.p_offset
= note_off
;
284 phdr
.p_vaddr
= phdr
.p_paddr
= 0;
285 phdr
.p_filesz
= phdr
.p_memsz
= phdr_sz
;
288 /* Add merged PT_NOTE program header*/
289 tmp
= elfptr
+ sizeof(Elf64_Ehdr
);
290 memcpy(tmp
, &phdr
, sizeof(phdr
));
293 /* Remove unwanted PT_NOTE program headers. */
294 i
= (nr_ptnote
- 1) * sizeof(Elf64_Phdr
);
296 memmove(tmp
, tmp
+i
, ((*elfsz
)-sizeof(Elf64_Ehdr
)-sizeof(Elf64_Phdr
)));
298 /* Modify e_phnum to reflect merged headers. */
299 ehdr_ptr
->e_phnum
= ehdr_ptr
->e_phnum
- nr_ptnote
+ 1;
304 /* Merges all the PT_NOTE headers into one. */
305 static int __init
merge_note_headers_elf32(char *elfptr
, size_t *elfsz
,
306 struct list_head
*vc_list
)
308 int i
, nr_ptnote
=0, rc
=0;
310 Elf32_Ehdr
*ehdr_ptr
;
311 Elf32_Phdr phdr
, *phdr_ptr
;
312 Elf32_Nhdr
*nhdr_ptr
;
313 u64 phdr_sz
= 0, note_off
;
315 ehdr_ptr
= (Elf32_Ehdr
*)elfptr
;
316 phdr_ptr
= (Elf32_Phdr
*)(elfptr
+ sizeof(Elf32_Ehdr
));
317 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++, phdr_ptr
++) {
321 u64 offset
, max_sz
, sz
, real_sz
= 0;
322 if (phdr_ptr
->p_type
!= PT_NOTE
)
325 max_sz
= phdr_ptr
->p_memsz
;
326 offset
= phdr_ptr
->p_offset
;
327 notes_section
= kmalloc(max_sz
, GFP_KERNEL
);
330 rc
= read_from_oldmem(notes_section
, max_sz
, &offset
, 0);
332 kfree(notes_section
);
335 nhdr_ptr
= notes_section
;
336 for (j
= 0; j
< max_sz
; j
+= sz
) {
337 if (nhdr_ptr
->n_namesz
== 0)
339 sz
= sizeof(Elf32_Nhdr
) +
340 ((nhdr_ptr
->n_namesz
+ 3) & ~3) +
341 ((nhdr_ptr
->n_descsz
+ 3) & ~3);
343 nhdr_ptr
= (Elf32_Nhdr
*)((char*)nhdr_ptr
+ sz
);
346 /* Add this contiguous chunk of notes section to vmcore list.*/
347 new = get_new_element();
349 kfree(notes_section
);
352 new->paddr
= phdr_ptr
->p_offset
;
354 list_add_tail(&new->list
, vc_list
);
356 kfree(notes_section
);
359 /* Prepare merged PT_NOTE program header. */
360 phdr
.p_type
= PT_NOTE
;
362 note_off
= sizeof(Elf32_Ehdr
) +
363 (ehdr_ptr
->e_phnum
- nr_ptnote
+1) * sizeof(Elf32_Phdr
);
364 phdr
.p_offset
= note_off
;
365 phdr
.p_vaddr
= phdr
.p_paddr
= 0;
366 phdr
.p_filesz
= phdr
.p_memsz
= phdr_sz
;
369 /* Add merged PT_NOTE program header*/
370 tmp
= elfptr
+ sizeof(Elf32_Ehdr
);
371 memcpy(tmp
, &phdr
, sizeof(phdr
));
374 /* Remove unwanted PT_NOTE program headers. */
375 i
= (nr_ptnote
- 1) * sizeof(Elf32_Phdr
);
377 memmove(tmp
, tmp
+i
, ((*elfsz
)-sizeof(Elf32_Ehdr
)-sizeof(Elf32_Phdr
)));
379 /* Modify e_phnum to reflect merged headers. */
380 ehdr_ptr
->e_phnum
= ehdr_ptr
->e_phnum
- nr_ptnote
+ 1;
385 /* Add memory chunks represented by program headers to vmcore list. Also update
386 * the new offset fields of exported program headers. */
387 static int __init
process_ptload_program_headers_elf64(char *elfptr
,
389 struct list_head
*vc_list
)
392 Elf64_Ehdr
*ehdr_ptr
;
393 Elf64_Phdr
*phdr_ptr
;
397 ehdr_ptr
= (Elf64_Ehdr
*)elfptr
;
398 phdr_ptr
= (Elf64_Phdr
*)(elfptr
+ sizeof(Elf64_Ehdr
)); /* PT_NOTE hdr */
400 /* First program header is PT_NOTE header. */
401 vmcore_off
= sizeof(Elf64_Ehdr
) +
402 (ehdr_ptr
->e_phnum
) * sizeof(Elf64_Phdr
) +
403 phdr_ptr
->p_memsz
; /* Note sections */
405 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++, phdr_ptr
++) {
406 if (phdr_ptr
->p_type
!= PT_LOAD
)
409 /* Add this contiguous chunk of memory to vmcore list.*/
410 new = get_new_element();
413 new->paddr
= phdr_ptr
->p_offset
;
414 new->size
= phdr_ptr
->p_memsz
;
415 list_add_tail(&new->list
, vc_list
);
417 /* Update the program header offset. */
418 phdr_ptr
->p_offset
= vmcore_off
;
419 vmcore_off
= vmcore_off
+ phdr_ptr
->p_memsz
;
424 static int __init
process_ptload_program_headers_elf32(char *elfptr
,
426 struct list_head
*vc_list
)
429 Elf32_Ehdr
*ehdr_ptr
;
430 Elf32_Phdr
*phdr_ptr
;
434 ehdr_ptr
= (Elf32_Ehdr
*)elfptr
;
435 phdr_ptr
= (Elf32_Phdr
*)(elfptr
+ sizeof(Elf32_Ehdr
)); /* PT_NOTE hdr */
437 /* First program header is PT_NOTE header. */
438 vmcore_off
= sizeof(Elf32_Ehdr
) +
439 (ehdr_ptr
->e_phnum
) * sizeof(Elf32_Phdr
) +
440 phdr_ptr
->p_memsz
; /* Note sections */
442 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++, phdr_ptr
++) {
443 if (phdr_ptr
->p_type
!= PT_LOAD
)
446 /* Add this contiguous chunk of memory to vmcore list.*/
447 new = get_new_element();
450 new->paddr
= phdr_ptr
->p_offset
;
451 new->size
= phdr_ptr
->p_memsz
;
452 list_add_tail(&new->list
, vc_list
);
454 /* Update the program header offset */
455 phdr_ptr
->p_offset
= vmcore_off
;
456 vmcore_off
= vmcore_off
+ phdr_ptr
->p_memsz
;
461 /* Sets offset fields of vmcore elements. */
462 static void __init
set_vmcore_list_offsets_elf64(char *elfptr
,
463 struct list_head
*vc_list
)
466 Elf64_Ehdr
*ehdr_ptr
;
469 ehdr_ptr
= (Elf64_Ehdr
*)elfptr
;
471 /* Skip Elf header and program headers. */
472 vmcore_off
= sizeof(Elf64_Ehdr
) +
473 (ehdr_ptr
->e_phnum
) * sizeof(Elf64_Phdr
);
475 list_for_each_entry(m
, vc_list
, list
) {
476 m
->offset
= vmcore_off
;
477 vmcore_off
+= m
->size
;
481 /* Sets offset fields of vmcore elements. */
482 static void __init
set_vmcore_list_offsets_elf32(char *elfptr
,
483 struct list_head
*vc_list
)
486 Elf32_Ehdr
*ehdr_ptr
;
489 ehdr_ptr
= (Elf32_Ehdr
*)elfptr
;
491 /* Skip Elf header and program headers. */
492 vmcore_off
= sizeof(Elf32_Ehdr
) +
493 (ehdr_ptr
->e_phnum
) * sizeof(Elf32_Phdr
);
495 list_for_each_entry(m
, vc_list
, list
) {
496 m
->offset
= vmcore_off
;
497 vmcore_off
+= m
->size
;
501 static int __init
parse_crash_elf64_headers(void)
507 addr
= elfcorehdr_addr
;
509 /* Read Elf header */
510 rc
= read_from_oldmem((char*)&ehdr
, sizeof(Elf64_Ehdr
), &addr
, 0);
514 /* Do some basic Verification. */
515 if (memcmp(ehdr
.e_ident
, ELFMAG
, SELFMAG
) != 0 ||
516 (ehdr
.e_type
!= ET_CORE
) ||
517 !vmcore_elf_check_arch(&ehdr
) ||
518 ehdr
.e_ident
[EI_CLASS
] != ELFCLASS64
||
519 ehdr
.e_ident
[EI_VERSION
] != EV_CURRENT
||
520 ehdr
.e_version
!= EV_CURRENT
||
521 ehdr
.e_ehsize
!= sizeof(Elf64_Ehdr
) ||
522 ehdr
.e_phentsize
!= sizeof(Elf64_Phdr
) ||
524 printk(KERN_WARNING
"Warning: Core image elf header is not"
529 /* Read in all elf headers. */
530 elfcorebuf_sz
= sizeof(Elf64_Ehdr
) + ehdr
.e_phnum
* sizeof(Elf64_Phdr
);
531 elfcorebuf
= kmalloc(elfcorebuf_sz
, GFP_KERNEL
);
534 addr
= elfcorehdr_addr
;
535 rc
= read_from_oldmem(elfcorebuf
, elfcorebuf_sz
, &addr
, 0);
541 /* Merge all PT_NOTE headers into one. */
542 rc
= merge_note_headers_elf64(elfcorebuf
, &elfcorebuf_sz
, &vmcore_list
);
547 rc
= process_ptload_program_headers_elf64(elfcorebuf
, elfcorebuf_sz
,
553 set_vmcore_list_offsets_elf64(elfcorebuf
, &vmcore_list
);
557 static int __init
parse_crash_elf32_headers(void)
563 addr
= elfcorehdr_addr
;
565 /* Read Elf header */
566 rc
= read_from_oldmem((char*)&ehdr
, sizeof(Elf32_Ehdr
), &addr
, 0);
570 /* Do some basic Verification. */
571 if (memcmp(ehdr
.e_ident
, ELFMAG
, SELFMAG
) != 0 ||
572 (ehdr
.e_type
!= ET_CORE
) ||
573 !elf_check_arch(&ehdr
) ||
574 ehdr
.e_ident
[EI_CLASS
] != ELFCLASS32
||
575 ehdr
.e_ident
[EI_VERSION
] != EV_CURRENT
||
576 ehdr
.e_version
!= EV_CURRENT
||
577 ehdr
.e_ehsize
!= sizeof(Elf32_Ehdr
) ||
578 ehdr
.e_phentsize
!= sizeof(Elf32_Phdr
) ||
580 printk(KERN_WARNING
"Warning: Core image elf header is not"
585 /* Read in all elf headers. */
586 elfcorebuf_sz
= sizeof(Elf32_Ehdr
) + ehdr
.e_phnum
* sizeof(Elf32_Phdr
);
587 elfcorebuf
= kmalloc(elfcorebuf_sz
, GFP_KERNEL
);
590 addr
= elfcorehdr_addr
;
591 rc
= read_from_oldmem(elfcorebuf
, elfcorebuf_sz
, &addr
, 0);
597 /* Merge all PT_NOTE headers into one. */
598 rc
= merge_note_headers_elf32(elfcorebuf
, &elfcorebuf_sz
, &vmcore_list
);
603 rc
= process_ptload_program_headers_elf32(elfcorebuf
, elfcorebuf_sz
,
609 set_vmcore_list_offsets_elf32(elfcorebuf
, &vmcore_list
);
613 static int __init
parse_crash_elf_headers(void)
615 unsigned char e_ident
[EI_NIDENT
];
619 addr
= elfcorehdr_addr
;
620 rc
= read_from_oldmem(e_ident
, EI_NIDENT
, &addr
, 0);
623 if (memcmp(e_ident
, ELFMAG
, SELFMAG
) != 0) {
624 printk(KERN_WARNING
"Warning: Core image elf header"
629 if (e_ident
[EI_CLASS
] == ELFCLASS64
) {
630 rc
= parse_crash_elf64_headers();
634 /* Determine vmcore size. */
635 vmcore_size
= get_vmcore_size_elf64(elfcorebuf
);
636 } else if (e_ident
[EI_CLASS
] == ELFCLASS32
) {
637 rc
= parse_crash_elf32_headers();
641 /* Determine vmcore size. */
642 vmcore_size
= get_vmcore_size_elf32(elfcorebuf
);
644 printk(KERN_WARNING
"Warning: Core image elf header is not"
651 /* Init function for vmcore module. */
652 static int __init
vmcore_init(void)
656 /* If elfcorehdr= has been passed in cmdline, then capture the dump.*/
657 if (!(elfcorehdr_addr
< ELFCORE_ADDR_MAX
))
659 rc
= parse_crash_elf_headers();
661 printk(KERN_WARNING
"Kdump: vmcore not initialized\n");
665 /* Initialize /proc/vmcore size if proc is already up. */
667 proc_vmcore
->size
= vmcore_size
;
670 module_init(vmcore_init
)