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
10 #include <linux/config.h>
12 #include <linux/proc_fs.h>
13 #include <linux/user.h>
14 #include <linux/a.out.h>
15 #include <linux/elf.h>
16 #include <linux/elfcore.h>
17 #include <linux/highmem.h>
18 #include <linux/bootmem.h>
19 #include <linux/init.h>
20 #include <linux/crash_dump.h>
21 #include <linux/list.h>
22 #include <asm/uaccess.h>
25 /* List representing chunks of contiguous memory areas and their offsets in
28 static LIST_HEAD(vmcore_list);
30 /* Stores the pointer to the buffer containing kernel elf core headers. */
31 static char *elfcorebuf;
32 static size_t elfcorebuf_sz;
34 /* Total size of vmcore file. */
35 static u64 vmcore_size;
37 /* Stores the physical address of elf header of crash image. */
38 unsigned long long elfcorehdr_addr = ELFCORE_ADDR_MAX;
40 struct proc_dir_entry *proc_vmcore = NULL;
42 /* Reads a page from the oldmem device from given offset. */
43 static ssize_t read_from_oldmem(char *buf, size_t count,
44 u64 *ppos, int userbuf)
46 unsigned long pfn, offset;
48 ssize_t read = 0, tmp;
53 offset = (unsigned long)(*ppos % PAGE_SIZE);
54 pfn = (unsigned long)(*ppos / PAGE_SIZE);
55 if (pfn > saved_max_pfn)
59 if (count > (PAGE_SIZE - offset))
60 nr_bytes = PAGE_SIZE - offset;
64 tmp = copy_oldmem_page(pfn, buf, nr_bytes, offset, userbuf);
78 /* Maps vmcore file offset to respective physical address in memroy. */
79 static u64 map_offset_to_paddr(loff_t offset, struct list_head *vc_list,
80 struct vmcore **m_ptr)
85 list_for_each_entry(m, vc_list, list) {
88 end = m->offset + m->size - 1;
89 if (offset >= start && offset <= end) {
90 paddr = m->paddr + offset - start;
99 /* Read from the ELF header and then the crash dump. On error, negative value is
100 * returned otherwise number of bytes read are returned.
102 static ssize_t read_vmcore(struct file *file, char __user *buffer,
103 size_t buflen, loff_t *fpos)
105 ssize_t acc = 0, tmp;
106 size_t tsz, nr_bytes;
108 struct vmcore *curr_m = NULL;
110 if (buflen == 0 || *fpos >= vmcore_size)
113 /* trim buflen to not go beyond EOF */
114 if (buflen > vmcore_size - *fpos)
115 buflen = vmcore_size - *fpos;
117 /* Read ELF core header */
118 if (*fpos < elfcorebuf_sz) {
119 tsz = elfcorebuf_sz - *fpos;
122 if (copy_to_user(buffer, elfcorebuf + *fpos, tsz))
129 /* leave now if filled buffer already */
134 start = map_offset_to_paddr(*fpos, &vmcore_list, &curr_m);
137 if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
140 /* Calculate left bytes in current memory segment. */
141 nr_bytes = (curr_m->size - (start - curr_m->paddr));
146 tmp = read_from_oldmem(buffer, tsz, &start, 1);
153 if (start >= (curr_m->paddr + curr_m->size)) {
154 if (curr_m->list.next == &vmcore_list)
156 curr_m = list_entry(curr_m->list.next,
157 struct vmcore, list);
158 start = curr_m->paddr;
160 if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
162 /* Calculate left bytes in current memory segment. */
163 nr_bytes = (curr_m->size - (start - curr_m->paddr));
170 static int open_vmcore(struct inode *inode, struct file *filp)
175 struct file_operations proc_vmcore_operations = {
180 static struct vmcore* __init get_new_element(void)
184 p = kmalloc(sizeof(*p), GFP_KERNEL);
186 memset(p, 0, sizeof(*p));
190 static u64 __init get_vmcore_size_elf64(char *elfptr)
194 Elf64_Ehdr *ehdr_ptr;
195 Elf64_Phdr *phdr_ptr;
197 ehdr_ptr = (Elf64_Ehdr *)elfptr;
198 phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr));
199 size = sizeof(Elf64_Ehdr) + ((ehdr_ptr->e_phnum) * sizeof(Elf64_Phdr));
200 for (i = 0; i < ehdr_ptr->e_phnum; i++) {
201 size += phdr_ptr->p_memsz;
207 static u64 __init get_vmcore_size_elf32(char *elfptr)
211 Elf32_Ehdr *ehdr_ptr;
212 Elf32_Phdr *phdr_ptr;
214 ehdr_ptr = (Elf32_Ehdr *)elfptr;
215 phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr));
216 size = sizeof(Elf32_Ehdr) + ((ehdr_ptr->e_phnum) * sizeof(Elf32_Phdr));
217 for (i = 0; i < ehdr_ptr->e_phnum; i++) {
218 size += phdr_ptr->p_memsz;
224 /* Merges all the PT_NOTE headers into one. */
225 static int __init merge_note_headers_elf64(char *elfptr, size_t *elfsz,
226 struct list_head *vc_list)
228 int i, nr_ptnote=0, rc=0;
230 Elf64_Ehdr *ehdr_ptr;
231 Elf64_Phdr phdr, *phdr_ptr;
232 Elf64_Nhdr *nhdr_ptr;
233 u64 phdr_sz = 0, note_off;
235 ehdr_ptr = (Elf64_Ehdr *)elfptr;
236 phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr));
237 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
241 u64 offset, max_sz, sz, real_sz = 0;
242 if (phdr_ptr->p_type != PT_NOTE)
245 max_sz = phdr_ptr->p_memsz;
246 offset = phdr_ptr->p_offset;
247 notes_section = kmalloc(max_sz, GFP_KERNEL);
250 rc = read_from_oldmem(notes_section, max_sz, &offset, 0);
252 kfree(notes_section);
255 nhdr_ptr = notes_section;
256 for (j = 0; j < max_sz; j += sz) {
257 if (nhdr_ptr->n_namesz == 0)
259 sz = sizeof(Elf64_Nhdr) +
260 ((nhdr_ptr->n_namesz + 3) & ~3) +
261 ((nhdr_ptr->n_descsz + 3) & ~3);
263 nhdr_ptr = (Elf64_Nhdr*)((char*)nhdr_ptr + sz);
266 /* Add this contiguous chunk of notes section to vmcore list.*/
267 new = get_new_element();
269 kfree(notes_section);
272 new->paddr = phdr_ptr->p_offset;
274 list_add_tail(&new->list, vc_list);
276 kfree(notes_section);
279 /* Prepare merged PT_NOTE program header. */
280 phdr.p_type = PT_NOTE;
282 note_off = sizeof(Elf64_Ehdr) +
283 (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf64_Phdr);
284 phdr.p_offset = note_off;
285 phdr.p_vaddr = phdr.p_paddr = 0;
286 phdr.p_filesz = phdr.p_memsz = phdr_sz;
289 /* Add merged PT_NOTE program header*/
290 tmp = elfptr + sizeof(Elf64_Ehdr);
291 memcpy(tmp, &phdr, sizeof(phdr));
294 /* Remove unwanted PT_NOTE program headers. */
295 i = (nr_ptnote - 1) * sizeof(Elf64_Phdr);
297 memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf64_Ehdr)-sizeof(Elf64_Phdr)));
299 /* Modify e_phnum to reflect merged headers. */
300 ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
305 /* Merges all the PT_NOTE headers into one. */
306 static int __init merge_note_headers_elf32(char *elfptr, size_t *elfsz,
307 struct list_head *vc_list)
309 int i, nr_ptnote=0, rc=0;
311 Elf32_Ehdr *ehdr_ptr;
312 Elf32_Phdr phdr, *phdr_ptr;
313 Elf32_Nhdr *nhdr_ptr;
314 u64 phdr_sz = 0, note_off;
316 ehdr_ptr = (Elf32_Ehdr *)elfptr;
317 phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr));
318 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
322 u64 offset, max_sz, sz, real_sz = 0;
323 if (phdr_ptr->p_type != PT_NOTE)
326 max_sz = phdr_ptr->p_memsz;
327 offset = phdr_ptr->p_offset;
328 notes_section = kmalloc(max_sz, GFP_KERNEL);
331 rc = read_from_oldmem(notes_section, max_sz, &offset, 0);
333 kfree(notes_section);
336 nhdr_ptr = notes_section;
337 for (j = 0; j < max_sz; j += sz) {
338 if (nhdr_ptr->n_namesz == 0)
340 sz = sizeof(Elf32_Nhdr) +
341 ((nhdr_ptr->n_namesz + 3) & ~3) +
342 ((nhdr_ptr->n_descsz + 3) & ~3);
344 nhdr_ptr = (Elf32_Nhdr*)((char*)nhdr_ptr + sz);
347 /* Add this contiguous chunk of notes section to vmcore list.*/
348 new = get_new_element();
350 kfree(notes_section);
353 new->paddr = phdr_ptr->p_offset;
355 list_add_tail(&new->list, vc_list);
357 kfree(notes_section);
360 /* Prepare merged PT_NOTE program header. */
361 phdr.p_type = PT_NOTE;
363 note_off = sizeof(Elf32_Ehdr) +
364 (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf32_Phdr);
365 phdr.p_offset = note_off;
366 phdr.p_vaddr = phdr.p_paddr = 0;
367 phdr.p_filesz = phdr.p_memsz = phdr_sz;
370 /* Add merged PT_NOTE program header*/
371 tmp = elfptr + sizeof(Elf32_Ehdr);
372 memcpy(tmp, &phdr, sizeof(phdr));
375 /* Remove unwanted PT_NOTE program headers. */
376 i = (nr_ptnote - 1) * sizeof(Elf32_Phdr);
378 memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf32_Ehdr)-sizeof(Elf32_Phdr)));
380 /* Modify e_phnum to reflect merged headers. */
381 ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
386 /* Add memory chunks represented by program headers to vmcore list. Also update
387 * the new offset fields of exported program headers. */
388 static int __init process_ptload_program_headers_elf64(char *elfptr,
390 struct list_head *vc_list)
393 Elf64_Ehdr *ehdr_ptr;
394 Elf64_Phdr *phdr_ptr;
398 ehdr_ptr = (Elf64_Ehdr *)elfptr;
399 phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr)); /* PT_NOTE hdr */
401 /* First program header is PT_NOTE header. */
402 vmcore_off = sizeof(Elf64_Ehdr) +
403 (ehdr_ptr->e_phnum) * sizeof(Elf64_Phdr) +
404 phdr_ptr->p_memsz; /* Note sections */
406 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
407 if (phdr_ptr->p_type != PT_LOAD)
410 /* Add this contiguous chunk of memory to vmcore list.*/
411 new = get_new_element();
414 new->paddr = phdr_ptr->p_offset;
415 new->size = phdr_ptr->p_memsz;
416 list_add_tail(&new->list, vc_list);
418 /* Update the program header offset. */
419 phdr_ptr->p_offset = vmcore_off;
420 vmcore_off = vmcore_off + phdr_ptr->p_memsz;
425 static int __init process_ptload_program_headers_elf32(char *elfptr,
427 struct list_head *vc_list)
430 Elf32_Ehdr *ehdr_ptr;
431 Elf32_Phdr *phdr_ptr;
435 ehdr_ptr = (Elf32_Ehdr *)elfptr;
436 phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr)); /* PT_NOTE hdr */
438 /* First program header is PT_NOTE header. */
439 vmcore_off = sizeof(Elf32_Ehdr) +
440 (ehdr_ptr->e_phnum) * sizeof(Elf32_Phdr) +
441 phdr_ptr->p_memsz; /* Note sections */
443 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
444 if (phdr_ptr->p_type != PT_LOAD)
447 /* Add this contiguous chunk of memory to vmcore list.*/
448 new = get_new_element();
451 new->paddr = phdr_ptr->p_offset;
452 new->size = phdr_ptr->p_memsz;
453 list_add_tail(&new->list, vc_list);
455 /* Update the program header offset */
456 phdr_ptr->p_offset = vmcore_off;
457 vmcore_off = vmcore_off + phdr_ptr->p_memsz;
462 /* Sets offset fields of vmcore elements. */
463 static void __init set_vmcore_list_offsets_elf64(char *elfptr,
464 struct list_head *vc_list)
467 Elf64_Ehdr *ehdr_ptr;
470 ehdr_ptr = (Elf64_Ehdr *)elfptr;
472 /* Skip Elf header and program headers. */
473 vmcore_off = sizeof(Elf64_Ehdr) +
474 (ehdr_ptr->e_phnum) * sizeof(Elf64_Phdr);
476 list_for_each_entry(m, vc_list, list) {
477 m->offset = vmcore_off;
478 vmcore_off += m->size;
482 /* Sets offset fields of vmcore elements. */
483 static void __init set_vmcore_list_offsets_elf32(char *elfptr,
484 struct list_head *vc_list)
487 Elf32_Ehdr *ehdr_ptr;
490 ehdr_ptr = (Elf32_Ehdr *)elfptr;
492 /* Skip Elf header and program headers. */
493 vmcore_off = sizeof(Elf32_Ehdr) +
494 (ehdr_ptr->e_phnum) * sizeof(Elf32_Phdr);
496 list_for_each_entry(m, vc_list, list) {
497 m->offset = vmcore_off;
498 vmcore_off += m->size;
502 static int __init parse_crash_elf64_headers(void)
508 addr = elfcorehdr_addr;
510 /* Read Elf header */
511 rc = read_from_oldmem((char*)&ehdr, sizeof(Elf64_Ehdr), &addr, 0);
515 /* Do some basic Verification. */
516 if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
517 (ehdr.e_type != ET_CORE) ||
518 !elf_check_arch(&ehdr) ||
519 ehdr.e_ident[EI_CLASS] != ELFCLASS64 ||
520 ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
521 ehdr.e_version != EV_CURRENT ||
522 ehdr.e_ehsize != sizeof(Elf64_Ehdr) ||
523 ehdr.e_phentsize != sizeof(Elf64_Phdr) ||
525 printk(KERN_WARNING "Warning: Core image elf header is not"
530 /* Read in all elf headers. */
531 elfcorebuf_sz = sizeof(Elf64_Ehdr) + ehdr.e_phnum * sizeof(Elf64_Phdr);
532 elfcorebuf = kmalloc(elfcorebuf_sz, GFP_KERNEL);
535 addr = elfcorehdr_addr;
536 rc = read_from_oldmem(elfcorebuf, elfcorebuf_sz, &addr, 0);
542 /* Merge all PT_NOTE headers into one. */
543 rc = merge_note_headers_elf64(elfcorebuf, &elfcorebuf_sz, &vmcore_list);
548 rc = process_ptload_program_headers_elf64(elfcorebuf, elfcorebuf_sz,
554 set_vmcore_list_offsets_elf64(elfcorebuf, &vmcore_list);
558 static int __init parse_crash_elf32_headers(void)
564 addr = elfcorehdr_addr;
566 /* Read Elf header */
567 rc = read_from_oldmem((char*)&ehdr, sizeof(Elf32_Ehdr), &addr, 0);
571 /* Do some basic Verification. */
572 if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
573 (ehdr.e_type != ET_CORE) ||
574 !elf_check_arch(&ehdr) ||
575 ehdr.e_ident[EI_CLASS] != ELFCLASS32||
576 ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
577 ehdr.e_version != EV_CURRENT ||
578 ehdr.e_ehsize != sizeof(Elf32_Ehdr) ||
579 ehdr.e_phentsize != sizeof(Elf32_Phdr) ||
581 printk(KERN_WARNING "Warning: Core image elf header is not"
586 /* Read in all elf headers. */
587 elfcorebuf_sz = sizeof(Elf32_Ehdr) + ehdr.e_phnum * sizeof(Elf32_Phdr);
588 elfcorebuf = kmalloc(elfcorebuf_sz, GFP_KERNEL);
591 addr = elfcorehdr_addr;
592 rc = read_from_oldmem(elfcorebuf, elfcorebuf_sz, &addr, 0);
598 /* Merge all PT_NOTE headers into one. */
599 rc = merge_note_headers_elf32(elfcorebuf, &elfcorebuf_sz, &vmcore_list);
604 rc = process_ptload_program_headers_elf32(elfcorebuf, elfcorebuf_sz,
610 set_vmcore_list_offsets_elf32(elfcorebuf, &vmcore_list);
614 static int __init parse_crash_elf_headers(void)
616 unsigned char e_ident[EI_NIDENT];
620 addr = elfcorehdr_addr;
621 rc = read_from_oldmem(e_ident, EI_NIDENT, &addr, 0);
624 if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) {
625 printk(KERN_WARNING "Warning: Core image elf header"
630 if (e_ident[EI_CLASS] == ELFCLASS64) {
631 rc = parse_crash_elf64_headers();
635 /* Determine vmcore size. */
636 vmcore_size = get_vmcore_size_elf64(elfcorebuf);
637 } else if (e_ident[EI_CLASS] == ELFCLASS32) {
638 rc = parse_crash_elf32_headers();
642 /* Determine vmcore size. */
643 vmcore_size = get_vmcore_size_elf32(elfcorebuf);
645 printk(KERN_WARNING "Warning: Core image elf header is not"
652 /* Init function for vmcore module. */
653 static int __init vmcore_init(void)
657 /* If elfcorehdr= has been passed in cmdline, then capture the dump.*/
658 if (!(elfcorehdr_addr < ELFCORE_ADDR_MAX))
660 rc = parse_crash_elf_headers();
662 printk(KERN_WARNING "Kdump: vmcore not initialized\n");
666 /* Initialize /proc/vmcore size if proc is already up. */
668 proc_vmcore->size = vmcore_size;
671 module_init(vmcore_init)