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 /* Stores the physical address of elf header of crash image. */
36 unsigned long long elfcorehdr_addr
= ELFCORE_ADDR_MAX
;
38 struct proc_dir_entry
*proc_vmcore
= NULL
;
40 /* Reads a page from the oldmem device from given offset. */
41 static ssize_t
read_from_oldmem(char *buf
, size_t count
,
42 u64
*ppos
, int userbuf
)
44 unsigned long pfn
, offset
;
46 ssize_t read
= 0, tmp
;
51 offset
= (unsigned long)(*ppos
% PAGE_SIZE
);
52 pfn
= (unsigned long)(*ppos
/ PAGE_SIZE
);
53 if (pfn
> saved_max_pfn
)
57 if (count
> (PAGE_SIZE
- offset
))
58 nr_bytes
= PAGE_SIZE
- offset
;
62 tmp
= copy_oldmem_page(pfn
, buf
, nr_bytes
, offset
, userbuf
);
76 /* Maps vmcore file offset to respective physical address in memroy. */
77 static u64
map_offset_to_paddr(loff_t offset
, struct list_head
*vc_list
,
78 struct vmcore
**m_ptr
)
83 list_for_each_entry(m
, vc_list
, list
) {
86 end
= m
->offset
+ m
->size
- 1;
87 if (offset
>= start
&& offset
<= end
) {
88 paddr
= m
->paddr
+ offset
- start
;
97 /* Read from the ELF header and then the crash dump. On error, negative value is
98 * returned otherwise number of bytes read are returned.
100 static ssize_t
read_vmcore(struct file
*file
, char __user
*buffer
,
101 size_t buflen
, loff_t
*fpos
)
103 ssize_t acc
= 0, tmp
;
106 struct vmcore
*curr_m
= NULL
;
108 if (buflen
== 0 || *fpos
>= vmcore_size
)
111 /* trim buflen to not go beyond EOF */
112 if (buflen
> vmcore_size
- *fpos
)
113 buflen
= vmcore_size
- *fpos
;
115 /* Read ELF core header */
116 if (*fpos
< elfcorebuf_sz
) {
117 tsz
= elfcorebuf_sz
- *fpos
;
120 if (copy_to_user(buffer
, elfcorebuf
+ *fpos
, tsz
))
127 /* leave now if filled buffer already */
132 start
= map_offset_to_paddr(*fpos
, &vmcore_list
, &curr_m
);
135 if ((tsz
= (PAGE_SIZE
- (start
& ~PAGE_MASK
))) > buflen
)
138 /* Calculate left bytes in current memory segment. */
139 nr_bytes
= (curr_m
->size
- (start
- curr_m
->paddr
));
144 tmp
= read_from_oldmem(buffer
, tsz
, &start
, 1);
151 if (start
>= (curr_m
->paddr
+ curr_m
->size
)) {
152 if (curr_m
->list
.next
== &vmcore_list
)
154 curr_m
= list_entry(curr_m
->list
.next
,
155 struct vmcore
, list
);
156 start
= curr_m
->paddr
;
158 if ((tsz
= (PAGE_SIZE
- (start
& ~PAGE_MASK
))) > buflen
)
160 /* Calculate left bytes in current memory segment. */
161 nr_bytes
= (curr_m
->size
- (start
- curr_m
->paddr
));
168 static int open_vmcore(struct inode
*inode
, struct file
*filp
)
173 const struct file_operations proc_vmcore_operations
= {
178 static struct vmcore
* __init
get_new_element(void)
182 p
= kmalloc(sizeof(*p
), GFP_KERNEL
);
184 memset(p
, 0, sizeof(*p
));
188 static u64 __init
get_vmcore_size_elf64(char *elfptr
)
192 Elf64_Ehdr
*ehdr_ptr
;
193 Elf64_Phdr
*phdr_ptr
;
195 ehdr_ptr
= (Elf64_Ehdr
*)elfptr
;
196 phdr_ptr
= (Elf64_Phdr
*)(elfptr
+ sizeof(Elf64_Ehdr
));
197 size
= sizeof(Elf64_Ehdr
) + ((ehdr_ptr
->e_phnum
) * sizeof(Elf64_Phdr
));
198 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++) {
199 size
+= phdr_ptr
->p_memsz
;
205 static u64 __init
get_vmcore_size_elf32(char *elfptr
)
209 Elf32_Ehdr
*ehdr_ptr
;
210 Elf32_Phdr
*phdr_ptr
;
212 ehdr_ptr
= (Elf32_Ehdr
*)elfptr
;
213 phdr_ptr
= (Elf32_Phdr
*)(elfptr
+ sizeof(Elf32_Ehdr
));
214 size
= sizeof(Elf32_Ehdr
) + ((ehdr_ptr
->e_phnum
) * sizeof(Elf32_Phdr
));
215 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++) {
216 size
+= phdr_ptr
->p_memsz
;
222 /* Merges all the PT_NOTE headers into one. */
223 static int __init
merge_note_headers_elf64(char *elfptr
, size_t *elfsz
,
224 struct list_head
*vc_list
)
226 int i
, nr_ptnote
=0, rc
=0;
228 Elf64_Ehdr
*ehdr_ptr
;
229 Elf64_Phdr phdr
, *phdr_ptr
;
230 Elf64_Nhdr
*nhdr_ptr
;
231 u64 phdr_sz
= 0, note_off
;
233 ehdr_ptr
= (Elf64_Ehdr
*)elfptr
;
234 phdr_ptr
= (Elf64_Phdr
*)(elfptr
+ sizeof(Elf64_Ehdr
));
235 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++, phdr_ptr
++) {
239 u64 offset
, max_sz
, sz
, real_sz
= 0;
240 if (phdr_ptr
->p_type
!= PT_NOTE
)
243 max_sz
= phdr_ptr
->p_memsz
;
244 offset
= phdr_ptr
->p_offset
;
245 notes_section
= kmalloc(max_sz
, GFP_KERNEL
);
248 rc
= read_from_oldmem(notes_section
, max_sz
, &offset
, 0);
250 kfree(notes_section
);
253 nhdr_ptr
= notes_section
;
254 for (j
= 0; j
< max_sz
; j
+= sz
) {
255 if (nhdr_ptr
->n_namesz
== 0)
257 sz
= sizeof(Elf64_Nhdr
) +
258 ((nhdr_ptr
->n_namesz
+ 3) & ~3) +
259 ((nhdr_ptr
->n_descsz
+ 3) & ~3);
261 nhdr_ptr
= (Elf64_Nhdr
*)((char*)nhdr_ptr
+ sz
);
264 /* Add this contiguous chunk of notes section to vmcore list.*/
265 new = get_new_element();
267 kfree(notes_section
);
270 new->paddr
= phdr_ptr
->p_offset
;
272 list_add_tail(&new->list
, vc_list
);
274 kfree(notes_section
);
277 /* Prepare merged PT_NOTE program header. */
278 phdr
.p_type
= PT_NOTE
;
280 note_off
= sizeof(Elf64_Ehdr
) +
281 (ehdr_ptr
->e_phnum
- nr_ptnote
+1) * sizeof(Elf64_Phdr
);
282 phdr
.p_offset
= note_off
;
283 phdr
.p_vaddr
= phdr
.p_paddr
= 0;
284 phdr
.p_filesz
= phdr
.p_memsz
= phdr_sz
;
287 /* Add merged PT_NOTE program header*/
288 tmp
= elfptr
+ sizeof(Elf64_Ehdr
);
289 memcpy(tmp
, &phdr
, sizeof(phdr
));
292 /* Remove unwanted PT_NOTE program headers. */
293 i
= (nr_ptnote
- 1) * sizeof(Elf64_Phdr
);
295 memmove(tmp
, tmp
+i
, ((*elfsz
)-sizeof(Elf64_Ehdr
)-sizeof(Elf64_Phdr
)));
297 /* Modify e_phnum to reflect merged headers. */
298 ehdr_ptr
->e_phnum
= ehdr_ptr
->e_phnum
- nr_ptnote
+ 1;
303 /* Merges all the PT_NOTE headers into one. */
304 static int __init
merge_note_headers_elf32(char *elfptr
, size_t *elfsz
,
305 struct list_head
*vc_list
)
307 int i
, nr_ptnote
=0, rc
=0;
309 Elf32_Ehdr
*ehdr_ptr
;
310 Elf32_Phdr phdr
, *phdr_ptr
;
311 Elf32_Nhdr
*nhdr_ptr
;
312 u64 phdr_sz
= 0, note_off
;
314 ehdr_ptr
= (Elf32_Ehdr
*)elfptr
;
315 phdr_ptr
= (Elf32_Phdr
*)(elfptr
+ sizeof(Elf32_Ehdr
));
316 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++, phdr_ptr
++) {
320 u64 offset
, max_sz
, sz
, real_sz
= 0;
321 if (phdr_ptr
->p_type
!= PT_NOTE
)
324 max_sz
= phdr_ptr
->p_memsz
;
325 offset
= phdr_ptr
->p_offset
;
326 notes_section
= kmalloc(max_sz
, GFP_KERNEL
);
329 rc
= read_from_oldmem(notes_section
, max_sz
, &offset
, 0);
331 kfree(notes_section
);
334 nhdr_ptr
= notes_section
;
335 for (j
= 0; j
< max_sz
; j
+= sz
) {
336 if (nhdr_ptr
->n_namesz
== 0)
338 sz
= sizeof(Elf32_Nhdr
) +
339 ((nhdr_ptr
->n_namesz
+ 3) & ~3) +
340 ((nhdr_ptr
->n_descsz
+ 3) & ~3);
342 nhdr_ptr
= (Elf32_Nhdr
*)((char*)nhdr_ptr
+ sz
);
345 /* Add this contiguous chunk of notes section to vmcore list.*/
346 new = get_new_element();
348 kfree(notes_section
);
351 new->paddr
= phdr_ptr
->p_offset
;
353 list_add_tail(&new->list
, vc_list
);
355 kfree(notes_section
);
358 /* Prepare merged PT_NOTE program header. */
359 phdr
.p_type
= PT_NOTE
;
361 note_off
= sizeof(Elf32_Ehdr
) +
362 (ehdr_ptr
->e_phnum
- nr_ptnote
+1) * sizeof(Elf32_Phdr
);
363 phdr
.p_offset
= note_off
;
364 phdr
.p_vaddr
= phdr
.p_paddr
= 0;
365 phdr
.p_filesz
= phdr
.p_memsz
= phdr_sz
;
368 /* Add merged PT_NOTE program header*/
369 tmp
= elfptr
+ sizeof(Elf32_Ehdr
);
370 memcpy(tmp
, &phdr
, sizeof(phdr
));
373 /* Remove unwanted PT_NOTE program headers. */
374 i
= (nr_ptnote
- 1) * sizeof(Elf32_Phdr
);
376 memmove(tmp
, tmp
+i
, ((*elfsz
)-sizeof(Elf32_Ehdr
)-sizeof(Elf32_Phdr
)));
378 /* Modify e_phnum to reflect merged headers. */
379 ehdr_ptr
->e_phnum
= ehdr_ptr
->e_phnum
- nr_ptnote
+ 1;
384 /* Add memory chunks represented by program headers to vmcore list. Also update
385 * the new offset fields of exported program headers. */
386 static int __init
process_ptload_program_headers_elf64(char *elfptr
,
388 struct list_head
*vc_list
)
391 Elf64_Ehdr
*ehdr_ptr
;
392 Elf64_Phdr
*phdr_ptr
;
396 ehdr_ptr
= (Elf64_Ehdr
*)elfptr
;
397 phdr_ptr
= (Elf64_Phdr
*)(elfptr
+ sizeof(Elf64_Ehdr
)); /* PT_NOTE hdr */
399 /* First program header is PT_NOTE header. */
400 vmcore_off
= sizeof(Elf64_Ehdr
) +
401 (ehdr_ptr
->e_phnum
) * sizeof(Elf64_Phdr
) +
402 phdr_ptr
->p_memsz
; /* Note sections */
404 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++, phdr_ptr
++) {
405 if (phdr_ptr
->p_type
!= PT_LOAD
)
408 /* Add this contiguous chunk of memory to vmcore list.*/
409 new = get_new_element();
412 new->paddr
= phdr_ptr
->p_offset
;
413 new->size
= phdr_ptr
->p_memsz
;
414 list_add_tail(&new->list
, vc_list
);
416 /* Update the program header offset. */
417 phdr_ptr
->p_offset
= vmcore_off
;
418 vmcore_off
= vmcore_off
+ phdr_ptr
->p_memsz
;
423 static int __init
process_ptload_program_headers_elf32(char *elfptr
,
425 struct list_head
*vc_list
)
428 Elf32_Ehdr
*ehdr_ptr
;
429 Elf32_Phdr
*phdr_ptr
;
433 ehdr_ptr
= (Elf32_Ehdr
*)elfptr
;
434 phdr_ptr
= (Elf32_Phdr
*)(elfptr
+ sizeof(Elf32_Ehdr
)); /* PT_NOTE hdr */
436 /* First program header is PT_NOTE header. */
437 vmcore_off
= sizeof(Elf32_Ehdr
) +
438 (ehdr_ptr
->e_phnum
) * sizeof(Elf32_Phdr
) +
439 phdr_ptr
->p_memsz
; /* Note sections */
441 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++, phdr_ptr
++) {
442 if (phdr_ptr
->p_type
!= PT_LOAD
)
445 /* Add this contiguous chunk of memory to vmcore list.*/
446 new = get_new_element();
449 new->paddr
= phdr_ptr
->p_offset
;
450 new->size
= phdr_ptr
->p_memsz
;
451 list_add_tail(&new->list
, vc_list
);
453 /* Update the program header offset */
454 phdr_ptr
->p_offset
= vmcore_off
;
455 vmcore_off
= vmcore_off
+ phdr_ptr
->p_memsz
;
460 /* Sets offset fields of vmcore elements. */
461 static void __init
set_vmcore_list_offsets_elf64(char *elfptr
,
462 struct list_head
*vc_list
)
465 Elf64_Ehdr
*ehdr_ptr
;
468 ehdr_ptr
= (Elf64_Ehdr
*)elfptr
;
470 /* Skip Elf header and program headers. */
471 vmcore_off
= sizeof(Elf64_Ehdr
) +
472 (ehdr_ptr
->e_phnum
) * sizeof(Elf64_Phdr
);
474 list_for_each_entry(m
, vc_list
, list
) {
475 m
->offset
= vmcore_off
;
476 vmcore_off
+= m
->size
;
480 /* Sets offset fields of vmcore elements. */
481 static void __init
set_vmcore_list_offsets_elf32(char *elfptr
,
482 struct list_head
*vc_list
)
485 Elf32_Ehdr
*ehdr_ptr
;
488 ehdr_ptr
= (Elf32_Ehdr
*)elfptr
;
490 /* Skip Elf header and program headers. */
491 vmcore_off
= sizeof(Elf32_Ehdr
) +
492 (ehdr_ptr
->e_phnum
) * sizeof(Elf32_Phdr
);
494 list_for_each_entry(m
, vc_list
, list
) {
495 m
->offset
= vmcore_off
;
496 vmcore_off
+= m
->size
;
500 static int __init
parse_crash_elf64_headers(void)
506 addr
= elfcorehdr_addr
;
508 /* Read Elf header */
509 rc
= read_from_oldmem((char*)&ehdr
, sizeof(Elf64_Ehdr
), &addr
, 0);
513 /* Do some basic Verification. */
514 if (memcmp(ehdr
.e_ident
, ELFMAG
, SELFMAG
) != 0 ||
515 (ehdr
.e_type
!= ET_CORE
) ||
516 !vmcore_elf_check_arch(&ehdr
) ||
517 ehdr
.e_ident
[EI_CLASS
] != ELFCLASS64
||
518 ehdr
.e_ident
[EI_VERSION
] != EV_CURRENT
||
519 ehdr
.e_version
!= EV_CURRENT
||
520 ehdr
.e_ehsize
!= sizeof(Elf64_Ehdr
) ||
521 ehdr
.e_phentsize
!= sizeof(Elf64_Phdr
) ||
523 printk(KERN_WARNING
"Warning: Core image elf header is not"
528 /* Read in all elf headers. */
529 elfcorebuf_sz
= sizeof(Elf64_Ehdr
) + ehdr
.e_phnum
* sizeof(Elf64_Phdr
);
530 elfcorebuf
= kmalloc(elfcorebuf_sz
, GFP_KERNEL
);
533 addr
= elfcorehdr_addr
;
534 rc
= read_from_oldmem(elfcorebuf
, elfcorebuf_sz
, &addr
, 0);
540 /* Merge all PT_NOTE headers into one. */
541 rc
= merge_note_headers_elf64(elfcorebuf
, &elfcorebuf_sz
, &vmcore_list
);
546 rc
= process_ptload_program_headers_elf64(elfcorebuf
, elfcorebuf_sz
,
552 set_vmcore_list_offsets_elf64(elfcorebuf
, &vmcore_list
);
556 static int __init
parse_crash_elf32_headers(void)
562 addr
= elfcorehdr_addr
;
564 /* Read Elf header */
565 rc
= read_from_oldmem((char*)&ehdr
, sizeof(Elf32_Ehdr
), &addr
, 0);
569 /* Do some basic Verification. */
570 if (memcmp(ehdr
.e_ident
, ELFMAG
, SELFMAG
) != 0 ||
571 (ehdr
.e_type
!= ET_CORE
) ||
572 !elf_check_arch(&ehdr
) ||
573 ehdr
.e_ident
[EI_CLASS
] != ELFCLASS32
||
574 ehdr
.e_ident
[EI_VERSION
] != EV_CURRENT
||
575 ehdr
.e_version
!= EV_CURRENT
||
576 ehdr
.e_ehsize
!= sizeof(Elf32_Ehdr
) ||
577 ehdr
.e_phentsize
!= sizeof(Elf32_Phdr
) ||
579 printk(KERN_WARNING
"Warning: Core image elf header is not"
584 /* Read in all elf headers. */
585 elfcorebuf_sz
= sizeof(Elf32_Ehdr
) + ehdr
.e_phnum
* sizeof(Elf32_Phdr
);
586 elfcorebuf
= kmalloc(elfcorebuf_sz
, GFP_KERNEL
);
589 addr
= elfcorehdr_addr
;
590 rc
= read_from_oldmem(elfcorebuf
, elfcorebuf_sz
, &addr
, 0);
596 /* Merge all PT_NOTE headers into one. */
597 rc
= merge_note_headers_elf32(elfcorebuf
, &elfcorebuf_sz
, &vmcore_list
);
602 rc
= process_ptload_program_headers_elf32(elfcorebuf
, elfcorebuf_sz
,
608 set_vmcore_list_offsets_elf32(elfcorebuf
, &vmcore_list
);
612 static int __init
parse_crash_elf_headers(void)
614 unsigned char e_ident
[EI_NIDENT
];
618 addr
= elfcorehdr_addr
;
619 rc
= read_from_oldmem(e_ident
, EI_NIDENT
, &addr
, 0);
622 if (memcmp(e_ident
, ELFMAG
, SELFMAG
) != 0) {
623 printk(KERN_WARNING
"Warning: Core image elf header"
628 if (e_ident
[EI_CLASS
] == ELFCLASS64
) {
629 rc
= parse_crash_elf64_headers();
633 /* Determine vmcore size. */
634 vmcore_size
= get_vmcore_size_elf64(elfcorebuf
);
635 } else if (e_ident
[EI_CLASS
] == ELFCLASS32
) {
636 rc
= parse_crash_elf32_headers();
640 /* Determine vmcore size. */
641 vmcore_size
= get_vmcore_size_elf32(elfcorebuf
);
643 printk(KERN_WARNING
"Warning: Core image elf header is not"
650 /* Init function for vmcore module. */
651 static int __init
vmcore_init(void)
655 /* If elfcorehdr= has been passed in cmdline, then capture the dump.*/
656 if (!(elfcorehdr_addr
< ELFCORE_ADDR_MAX
))
658 rc
= parse_crash_elf_headers();
660 printk(KERN_WARNING
"Kdump: vmcore not initialized\n");
664 /* Initialize /proc/vmcore size if proc is already up. */
666 proc_vmcore
->size
= vmcore_size
;
669 module_init(vmcore_init
)