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 const struct file_operations proc_vmcore_operations
= {
172 static struct vmcore
* __init
get_new_element(void)
176 p
= kmalloc(sizeof(*p
), GFP_KERNEL
);
178 memset(p
, 0, sizeof(*p
));
182 static u64 __init
get_vmcore_size_elf64(char *elfptr
)
186 Elf64_Ehdr
*ehdr_ptr
;
187 Elf64_Phdr
*phdr_ptr
;
189 ehdr_ptr
= (Elf64_Ehdr
*)elfptr
;
190 phdr_ptr
= (Elf64_Phdr
*)(elfptr
+ sizeof(Elf64_Ehdr
));
191 size
= sizeof(Elf64_Ehdr
) + ((ehdr_ptr
->e_phnum
) * sizeof(Elf64_Phdr
));
192 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++) {
193 size
+= phdr_ptr
->p_memsz
;
199 static u64 __init
get_vmcore_size_elf32(char *elfptr
)
203 Elf32_Ehdr
*ehdr_ptr
;
204 Elf32_Phdr
*phdr_ptr
;
206 ehdr_ptr
= (Elf32_Ehdr
*)elfptr
;
207 phdr_ptr
= (Elf32_Phdr
*)(elfptr
+ sizeof(Elf32_Ehdr
));
208 size
= sizeof(Elf32_Ehdr
) + ((ehdr_ptr
->e_phnum
) * sizeof(Elf32_Phdr
));
209 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++) {
210 size
+= phdr_ptr
->p_memsz
;
216 /* Merges all the PT_NOTE headers into one. */
217 static int __init
merge_note_headers_elf64(char *elfptr
, size_t *elfsz
,
218 struct list_head
*vc_list
)
220 int i
, nr_ptnote
=0, rc
=0;
222 Elf64_Ehdr
*ehdr_ptr
;
223 Elf64_Phdr phdr
, *phdr_ptr
;
224 Elf64_Nhdr
*nhdr_ptr
;
225 u64 phdr_sz
= 0, note_off
;
227 ehdr_ptr
= (Elf64_Ehdr
*)elfptr
;
228 phdr_ptr
= (Elf64_Phdr
*)(elfptr
+ sizeof(Elf64_Ehdr
));
229 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++, phdr_ptr
++) {
233 u64 offset
, max_sz
, sz
, real_sz
= 0;
234 if (phdr_ptr
->p_type
!= PT_NOTE
)
237 max_sz
= phdr_ptr
->p_memsz
;
238 offset
= phdr_ptr
->p_offset
;
239 notes_section
= kmalloc(max_sz
, GFP_KERNEL
);
242 rc
= read_from_oldmem(notes_section
, max_sz
, &offset
, 0);
244 kfree(notes_section
);
247 nhdr_ptr
= notes_section
;
248 for (j
= 0; j
< max_sz
; j
+= sz
) {
249 if (nhdr_ptr
->n_namesz
== 0)
251 sz
= sizeof(Elf64_Nhdr
) +
252 ((nhdr_ptr
->n_namesz
+ 3) & ~3) +
253 ((nhdr_ptr
->n_descsz
+ 3) & ~3);
255 nhdr_ptr
= (Elf64_Nhdr
*)((char*)nhdr_ptr
+ sz
);
258 /* Add this contiguous chunk of notes section to vmcore list.*/
259 new = get_new_element();
261 kfree(notes_section
);
264 new->paddr
= phdr_ptr
->p_offset
;
266 list_add_tail(&new->list
, vc_list
);
268 kfree(notes_section
);
271 /* Prepare merged PT_NOTE program header. */
272 phdr
.p_type
= PT_NOTE
;
274 note_off
= sizeof(Elf64_Ehdr
) +
275 (ehdr_ptr
->e_phnum
- nr_ptnote
+1) * sizeof(Elf64_Phdr
);
276 phdr
.p_offset
= note_off
;
277 phdr
.p_vaddr
= phdr
.p_paddr
= 0;
278 phdr
.p_filesz
= phdr
.p_memsz
= phdr_sz
;
281 /* Add merged PT_NOTE program header*/
282 tmp
= elfptr
+ sizeof(Elf64_Ehdr
);
283 memcpy(tmp
, &phdr
, sizeof(phdr
));
286 /* Remove unwanted PT_NOTE program headers. */
287 i
= (nr_ptnote
- 1) * sizeof(Elf64_Phdr
);
289 memmove(tmp
, tmp
+i
, ((*elfsz
)-sizeof(Elf64_Ehdr
)-sizeof(Elf64_Phdr
)));
291 /* Modify e_phnum to reflect merged headers. */
292 ehdr_ptr
->e_phnum
= ehdr_ptr
->e_phnum
- nr_ptnote
+ 1;
297 /* Merges all the PT_NOTE headers into one. */
298 static int __init
merge_note_headers_elf32(char *elfptr
, size_t *elfsz
,
299 struct list_head
*vc_list
)
301 int i
, nr_ptnote
=0, rc
=0;
303 Elf32_Ehdr
*ehdr_ptr
;
304 Elf32_Phdr phdr
, *phdr_ptr
;
305 Elf32_Nhdr
*nhdr_ptr
;
306 u64 phdr_sz
= 0, note_off
;
308 ehdr_ptr
= (Elf32_Ehdr
*)elfptr
;
309 phdr_ptr
= (Elf32_Phdr
*)(elfptr
+ sizeof(Elf32_Ehdr
));
310 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++, phdr_ptr
++) {
314 u64 offset
, max_sz
, sz
, real_sz
= 0;
315 if (phdr_ptr
->p_type
!= PT_NOTE
)
318 max_sz
= phdr_ptr
->p_memsz
;
319 offset
= phdr_ptr
->p_offset
;
320 notes_section
= kmalloc(max_sz
, GFP_KERNEL
);
323 rc
= read_from_oldmem(notes_section
, max_sz
, &offset
, 0);
325 kfree(notes_section
);
328 nhdr_ptr
= notes_section
;
329 for (j
= 0; j
< max_sz
; j
+= sz
) {
330 if (nhdr_ptr
->n_namesz
== 0)
332 sz
= sizeof(Elf32_Nhdr
) +
333 ((nhdr_ptr
->n_namesz
+ 3) & ~3) +
334 ((nhdr_ptr
->n_descsz
+ 3) & ~3);
336 nhdr_ptr
= (Elf32_Nhdr
*)((char*)nhdr_ptr
+ sz
);
339 /* Add this contiguous chunk of notes section to vmcore list.*/
340 new = get_new_element();
342 kfree(notes_section
);
345 new->paddr
= phdr_ptr
->p_offset
;
347 list_add_tail(&new->list
, vc_list
);
349 kfree(notes_section
);
352 /* Prepare merged PT_NOTE program header. */
353 phdr
.p_type
= PT_NOTE
;
355 note_off
= sizeof(Elf32_Ehdr
) +
356 (ehdr_ptr
->e_phnum
- nr_ptnote
+1) * sizeof(Elf32_Phdr
);
357 phdr
.p_offset
= note_off
;
358 phdr
.p_vaddr
= phdr
.p_paddr
= 0;
359 phdr
.p_filesz
= phdr
.p_memsz
= phdr_sz
;
362 /* Add merged PT_NOTE program header*/
363 tmp
= elfptr
+ sizeof(Elf32_Ehdr
);
364 memcpy(tmp
, &phdr
, sizeof(phdr
));
367 /* Remove unwanted PT_NOTE program headers. */
368 i
= (nr_ptnote
- 1) * sizeof(Elf32_Phdr
);
370 memmove(tmp
, tmp
+i
, ((*elfsz
)-sizeof(Elf32_Ehdr
)-sizeof(Elf32_Phdr
)));
372 /* Modify e_phnum to reflect merged headers. */
373 ehdr_ptr
->e_phnum
= ehdr_ptr
->e_phnum
- nr_ptnote
+ 1;
378 /* Add memory chunks represented by program headers to vmcore list. Also update
379 * the new offset fields of exported program headers. */
380 static int __init
process_ptload_program_headers_elf64(char *elfptr
,
382 struct list_head
*vc_list
)
385 Elf64_Ehdr
*ehdr_ptr
;
386 Elf64_Phdr
*phdr_ptr
;
390 ehdr_ptr
= (Elf64_Ehdr
*)elfptr
;
391 phdr_ptr
= (Elf64_Phdr
*)(elfptr
+ sizeof(Elf64_Ehdr
)); /* PT_NOTE hdr */
393 /* First program header is PT_NOTE header. */
394 vmcore_off
= sizeof(Elf64_Ehdr
) +
395 (ehdr_ptr
->e_phnum
) * sizeof(Elf64_Phdr
) +
396 phdr_ptr
->p_memsz
; /* Note sections */
398 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++, phdr_ptr
++) {
399 if (phdr_ptr
->p_type
!= PT_LOAD
)
402 /* Add this contiguous chunk of memory to vmcore list.*/
403 new = get_new_element();
406 new->paddr
= phdr_ptr
->p_offset
;
407 new->size
= phdr_ptr
->p_memsz
;
408 list_add_tail(&new->list
, vc_list
);
410 /* Update the program header offset. */
411 phdr_ptr
->p_offset
= vmcore_off
;
412 vmcore_off
= vmcore_off
+ phdr_ptr
->p_memsz
;
417 static int __init
process_ptload_program_headers_elf32(char *elfptr
,
419 struct list_head
*vc_list
)
422 Elf32_Ehdr
*ehdr_ptr
;
423 Elf32_Phdr
*phdr_ptr
;
427 ehdr_ptr
= (Elf32_Ehdr
*)elfptr
;
428 phdr_ptr
= (Elf32_Phdr
*)(elfptr
+ sizeof(Elf32_Ehdr
)); /* PT_NOTE hdr */
430 /* First program header is PT_NOTE header. */
431 vmcore_off
= sizeof(Elf32_Ehdr
) +
432 (ehdr_ptr
->e_phnum
) * sizeof(Elf32_Phdr
) +
433 phdr_ptr
->p_memsz
; /* Note sections */
435 for (i
= 0; i
< ehdr_ptr
->e_phnum
; i
++, phdr_ptr
++) {
436 if (phdr_ptr
->p_type
!= PT_LOAD
)
439 /* Add this contiguous chunk of memory to vmcore list.*/
440 new = get_new_element();
443 new->paddr
= phdr_ptr
->p_offset
;
444 new->size
= phdr_ptr
->p_memsz
;
445 list_add_tail(&new->list
, vc_list
);
447 /* Update the program header offset */
448 phdr_ptr
->p_offset
= vmcore_off
;
449 vmcore_off
= vmcore_off
+ phdr_ptr
->p_memsz
;
454 /* Sets offset fields of vmcore elements. */
455 static void __init
set_vmcore_list_offsets_elf64(char *elfptr
,
456 struct list_head
*vc_list
)
459 Elf64_Ehdr
*ehdr_ptr
;
462 ehdr_ptr
= (Elf64_Ehdr
*)elfptr
;
464 /* Skip Elf header and program headers. */
465 vmcore_off
= sizeof(Elf64_Ehdr
) +
466 (ehdr_ptr
->e_phnum
) * sizeof(Elf64_Phdr
);
468 list_for_each_entry(m
, vc_list
, list
) {
469 m
->offset
= vmcore_off
;
470 vmcore_off
+= m
->size
;
474 /* Sets offset fields of vmcore elements. */
475 static void __init
set_vmcore_list_offsets_elf32(char *elfptr
,
476 struct list_head
*vc_list
)
479 Elf32_Ehdr
*ehdr_ptr
;
482 ehdr_ptr
= (Elf32_Ehdr
*)elfptr
;
484 /* Skip Elf header and program headers. */
485 vmcore_off
= sizeof(Elf32_Ehdr
) +
486 (ehdr_ptr
->e_phnum
) * sizeof(Elf32_Phdr
);
488 list_for_each_entry(m
, vc_list
, list
) {
489 m
->offset
= vmcore_off
;
490 vmcore_off
+= m
->size
;
494 static int __init
parse_crash_elf64_headers(void)
500 addr
= elfcorehdr_addr
;
502 /* Read Elf header */
503 rc
= read_from_oldmem((char*)&ehdr
, sizeof(Elf64_Ehdr
), &addr
, 0);
507 /* Do some basic Verification. */
508 if (memcmp(ehdr
.e_ident
, ELFMAG
, SELFMAG
) != 0 ||
509 (ehdr
.e_type
!= ET_CORE
) ||
510 !vmcore_elf_check_arch(&ehdr
) ||
511 ehdr
.e_ident
[EI_CLASS
] != ELFCLASS64
||
512 ehdr
.e_ident
[EI_VERSION
] != EV_CURRENT
||
513 ehdr
.e_version
!= EV_CURRENT
||
514 ehdr
.e_ehsize
!= sizeof(Elf64_Ehdr
) ||
515 ehdr
.e_phentsize
!= sizeof(Elf64_Phdr
) ||
517 printk(KERN_WARNING
"Warning: Core image elf header is not"
522 /* Read in all elf headers. */
523 elfcorebuf_sz
= sizeof(Elf64_Ehdr
) + ehdr
.e_phnum
* sizeof(Elf64_Phdr
);
524 elfcorebuf
= kmalloc(elfcorebuf_sz
, GFP_KERNEL
);
527 addr
= elfcorehdr_addr
;
528 rc
= read_from_oldmem(elfcorebuf
, elfcorebuf_sz
, &addr
, 0);
534 /* Merge all PT_NOTE headers into one. */
535 rc
= merge_note_headers_elf64(elfcorebuf
, &elfcorebuf_sz
, &vmcore_list
);
540 rc
= process_ptload_program_headers_elf64(elfcorebuf
, elfcorebuf_sz
,
546 set_vmcore_list_offsets_elf64(elfcorebuf
, &vmcore_list
);
550 static int __init
parse_crash_elf32_headers(void)
556 addr
= elfcorehdr_addr
;
558 /* Read Elf header */
559 rc
= read_from_oldmem((char*)&ehdr
, sizeof(Elf32_Ehdr
), &addr
, 0);
563 /* Do some basic Verification. */
564 if (memcmp(ehdr
.e_ident
, ELFMAG
, SELFMAG
) != 0 ||
565 (ehdr
.e_type
!= ET_CORE
) ||
566 !elf_check_arch(&ehdr
) ||
567 ehdr
.e_ident
[EI_CLASS
] != ELFCLASS32
||
568 ehdr
.e_ident
[EI_VERSION
] != EV_CURRENT
||
569 ehdr
.e_version
!= EV_CURRENT
||
570 ehdr
.e_ehsize
!= sizeof(Elf32_Ehdr
) ||
571 ehdr
.e_phentsize
!= sizeof(Elf32_Phdr
) ||
573 printk(KERN_WARNING
"Warning: Core image elf header is not"
578 /* Read in all elf headers. */
579 elfcorebuf_sz
= sizeof(Elf32_Ehdr
) + ehdr
.e_phnum
* sizeof(Elf32_Phdr
);
580 elfcorebuf
= kmalloc(elfcorebuf_sz
, GFP_KERNEL
);
583 addr
= elfcorehdr_addr
;
584 rc
= read_from_oldmem(elfcorebuf
, elfcorebuf_sz
, &addr
, 0);
590 /* Merge all PT_NOTE headers into one. */
591 rc
= merge_note_headers_elf32(elfcorebuf
, &elfcorebuf_sz
, &vmcore_list
);
596 rc
= process_ptload_program_headers_elf32(elfcorebuf
, elfcorebuf_sz
,
602 set_vmcore_list_offsets_elf32(elfcorebuf
, &vmcore_list
);
606 static int __init
parse_crash_elf_headers(void)
608 unsigned char e_ident
[EI_NIDENT
];
612 addr
= elfcorehdr_addr
;
613 rc
= read_from_oldmem(e_ident
, EI_NIDENT
, &addr
, 0);
616 if (memcmp(e_ident
, ELFMAG
, SELFMAG
) != 0) {
617 printk(KERN_WARNING
"Warning: Core image elf header"
622 if (e_ident
[EI_CLASS
] == ELFCLASS64
) {
623 rc
= parse_crash_elf64_headers();
627 /* Determine vmcore size. */
628 vmcore_size
= get_vmcore_size_elf64(elfcorebuf
);
629 } else if (e_ident
[EI_CLASS
] == ELFCLASS32
) {
630 rc
= parse_crash_elf32_headers();
634 /* Determine vmcore size. */
635 vmcore_size
= get_vmcore_size_elf32(elfcorebuf
);
637 printk(KERN_WARNING
"Warning: Core image elf header is not"
644 /* Init function for vmcore module. */
645 static int __init
vmcore_init(void)
649 /* If elfcorehdr= has been passed in cmdline, then capture the dump.*/
650 if (!(elfcorehdr_addr
< ELFCORE_ADDR_MAX
))
652 rc
= parse_crash_elf_headers();
654 printk(KERN_WARNING
"Kdump: vmcore not initialized\n");
658 /* Initialize /proc/vmcore size if proc is already up. */
660 proc_vmcore
->size
= vmcore_size
;
663 module_init(vmcore_init
)