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/proc_fs.h>
18 #include <linux/highmem.h>
19 #include <linux/bootmem.h>
20 #include <linux/init.h>
21 #include <linux/crash_dump.h>
22 #include <linux/list.h>
23 #include <asm/uaccess.h>
26 /* List representing chunks of contiguous memory areas and their offsets in
29 static LIST_HEAD(vmcore_list);
31 /* Stores the pointer to the buffer containing kernel elf core headers. */
32 static char *elfcorebuf;
33 static size_t elfcorebuf_sz;
35 /* Total size of vmcore file. */
36 static u64 vmcore_size;
38 /* Stores the physical address of elf header of crash image. */
39 unsigned long long elfcorehdr_addr = ELFCORE_ADDR_MAX;
41 struct proc_dir_entry *proc_vmcore = NULL;
43 /* Reads a page from the oldmem device from given offset. */
44 static ssize_t read_from_oldmem(char *buf, size_t count,
45 loff_t *ppos, int userbuf)
47 unsigned long pfn, offset;
49 ssize_t read = 0, tmp;
54 offset = (unsigned long)(*ppos % PAGE_SIZE);
55 pfn = (unsigned long)(*ppos / PAGE_SIZE);
56 if (pfn > saved_max_pfn)
60 if (count > (PAGE_SIZE - offset))
61 nr_bytes = PAGE_SIZE - offset;
65 tmp = copy_oldmem_page(pfn, buf, nr_bytes, offset, userbuf);
79 /* Maps vmcore file offset to respective physical address in memroy. */
80 static u64 map_offset_to_paddr(loff_t offset, struct list_head *vc_list,
81 struct vmcore **m_ptr)
86 list_for_each_entry(m, vc_list, list) {
89 end = m->offset + m->size - 1;
90 if (offset >= start && offset <= end) {
91 paddr = m->paddr + offset - start;
100 /* Read from the ELF header and then the crash dump. On error, negative value is
101 * returned otherwise number of bytes read are returned.
103 static ssize_t read_vmcore(struct file *file, char __user *buffer,
104 size_t buflen, loff_t *fpos)
106 ssize_t acc = 0, tmp;
107 size_t tsz, nr_bytes;
109 struct vmcore *curr_m = NULL;
111 if (buflen == 0 || *fpos >= vmcore_size)
114 /* trim buflen to not go beyond EOF */
115 if (buflen > vmcore_size - *fpos)
116 buflen = vmcore_size - *fpos;
118 /* Read ELF core header */
119 if (*fpos < elfcorebuf_sz) {
120 tsz = elfcorebuf_sz - *fpos;
123 if (copy_to_user(buffer, elfcorebuf + *fpos, tsz))
130 /* leave now if filled buffer already */
135 start = map_offset_to_paddr(*fpos, &vmcore_list, &curr_m);
138 if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
141 /* Calculate left bytes in current memory segment. */
142 nr_bytes = (curr_m->size - (start - curr_m->paddr));
147 tmp = read_from_oldmem(buffer, tsz, &start, 1);
154 if (start >= (curr_m->paddr + curr_m->size)) {
155 if (curr_m->list.next == &vmcore_list)
157 curr_m = list_entry(curr_m->list.next,
158 struct vmcore, list);
159 start = curr_m->paddr;
161 if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
163 /* Calculate left bytes in current memory segment. */
164 nr_bytes = (curr_m->size - (start - curr_m->paddr));
171 static int open_vmcore(struct inode *inode, struct file *filp)
176 struct file_operations proc_vmcore_operations = {
181 static struct vmcore* __init get_new_element(void)
185 p = kmalloc(sizeof(*p), GFP_KERNEL);
187 memset(p, 0, sizeof(*p));
191 static u64 __init get_vmcore_size_elf64(char *elfptr)
195 Elf64_Ehdr *ehdr_ptr;
196 Elf64_Phdr *phdr_ptr;
198 ehdr_ptr = (Elf64_Ehdr *)elfptr;
199 phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr));
200 size = sizeof(Elf64_Ehdr) + ((ehdr_ptr->e_phnum) * sizeof(Elf64_Phdr));
201 for (i = 0; i < ehdr_ptr->e_phnum; i++) {
202 size += phdr_ptr->p_memsz;
208 static u64 __init get_vmcore_size_elf32(char *elfptr)
212 Elf32_Ehdr *ehdr_ptr;
213 Elf32_Phdr *phdr_ptr;
215 ehdr_ptr = (Elf32_Ehdr *)elfptr;
216 phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr));
217 size = sizeof(Elf32_Ehdr) + ((ehdr_ptr->e_phnum) * sizeof(Elf32_Phdr));
218 for (i = 0; i < ehdr_ptr->e_phnum; i++) {
219 size += phdr_ptr->p_memsz;
225 /* Merges all the PT_NOTE headers into one. */
226 static int __init merge_note_headers_elf64(char *elfptr, size_t *elfsz,
227 struct list_head *vc_list)
229 int i, nr_ptnote=0, rc=0;
231 Elf64_Ehdr *ehdr_ptr;
232 Elf64_Phdr phdr, *phdr_ptr;
233 Elf64_Nhdr *nhdr_ptr;
234 u64 phdr_sz = 0, note_off;
236 ehdr_ptr = (Elf64_Ehdr *)elfptr;
237 phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr));
238 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
242 u64 offset, max_sz, sz, real_sz = 0;
243 if (phdr_ptr->p_type != PT_NOTE)
246 max_sz = phdr_ptr->p_memsz;
247 offset = phdr_ptr->p_offset;
248 notes_section = kmalloc(max_sz, GFP_KERNEL);
251 rc = read_from_oldmem(notes_section, max_sz, &offset, 0);
253 kfree(notes_section);
256 nhdr_ptr = notes_section;
257 for (j = 0; j < max_sz; j += sz) {
258 if (nhdr_ptr->n_namesz == 0)
260 sz = sizeof(Elf64_Nhdr) +
261 ((nhdr_ptr->n_namesz + 3) & ~3) +
262 ((nhdr_ptr->n_descsz + 3) & ~3);
264 nhdr_ptr = (Elf64_Nhdr*)((char*)nhdr_ptr + sz);
267 /* Add this contiguous chunk of notes section to vmcore list.*/
268 new = get_new_element();
270 kfree(notes_section);
273 new->paddr = phdr_ptr->p_offset;
275 list_add_tail(&new->list, vc_list);
277 kfree(notes_section);
280 /* Prepare merged PT_NOTE program header. */
281 phdr.p_type = PT_NOTE;
283 note_off = sizeof(Elf64_Ehdr) +
284 (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf64_Phdr);
285 phdr.p_offset = note_off;
286 phdr.p_vaddr = phdr.p_paddr = 0;
287 phdr.p_filesz = phdr.p_memsz = phdr_sz;
290 /* Add merged PT_NOTE program header*/
291 tmp = elfptr + sizeof(Elf64_Ehdr);
292 memcpy(tmp, &phdr, sizeof(phdr));
295 /* Remove unwanted PT_NOTE program headers. */
296 i = (nr_ptnote - 1) * sizeof(Elf64_Phdr);
298 memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf64_Ehdr)-sizeof(Elf64_Phdr)));
300 /* Modify e_phnum to reflect merged headers. */
301 ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
306 /* Merges all the PT_NOTE headers into one. */
307 static int __init merge_note_headers_elf32(char *elfptr, size_t *elfsz,
308 struct list_head *vc_list)
310 int i, nr_ptnote=0, rc=0;
312 Elf32_Ehdr *ehdr_ptr;
313 Elf32_Phdr phdr, *phdr_ptr;
314 Elf32_Nhdr *nhdr_ptr;
315 u64 phdr_sz = 0, note_off;
317 ehdr_ptr = (Elf32_Ehdr *)elfptr;
318 phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr));
319 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
323 u64 offset, max_sz, sz, real_sz = 0;
324 if (phdr_ptr->p_type != PT_NOTE)
327 max_sz = phdr_ptr->p_memsz;
328 offset = phdr_ptr->p_offset;
329 notes_section = kmalloc(max_sz, GFP_KERNEL);
332 rc = read_from_oldmem(notes_section, max_sz, &offset, 0);
334 kfree(notes_section);
337 nhdr_ptr = notes_section;
338 for (j = 0; j < max_sz; j += sz) {
339 if (nhdr_ptr->n_namesz == 0)
341 sz = sizeof(Elf32_Nhdr) +
342 ((nhdr_ptr->n_namesz + 3) & ~3) +
343 ((nhdr_ptr->n_descsz + 3) & ~3);
345 nhdr_ptr = (Elf32_Nhdr*)((char*)nhdr_ptr + sz);
348 /* Add this contiguous chunk of notes section to vmcore list.*/
349 new = get_new_element();
351 kfree(notes_section);
354 new->paddr = phdr_ptr->p_offset;
356 list_add_tail(&new->list, vc_list);
358 kfree(notes_section);
361 /* Prepare merged PT_NOTE program header. */
362 phdr.p_type = PT_NOTE;
364 note_off = sizeof(Elf32_Ehdr) +
365 (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf32_Phdr);
366 phdr.p_offset = note_off;
367 phdr.p_vaddr = phdr.p_paddr = 0;
368 phdr.p_filesz = phdr.p_memsz = phdr_sz;
371 /* Add merged PT_NOTE program header*/
372 tmp = elfptr + sizeof(Elf32_Ehdr);
373 memcpy(tmp, &phdr, sizeof(phdr));
376 /* Remove unwanted PT_NOTE program headers. */
377 i = (nr_ptnote - 1) * sizeof(Elf32_Phdr);
379 memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf32_Ehdr)-sizeof(Elf32_Phdr)));
381 /* Modify e_phnum to reflect merged headers. */
382 ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
387 /* Add memory chunks represented by program headers to vmcore list. Also update
388 * the new offset fields of exported program headers. */
389 static int __init process_ptload_program_headers_elf64(char *elfptr,
391 struct list_head *vc_list)
394 Elf64_Ehdr *ehdr_ptr;
395 Elf64_Phdr *phdr_ptr;
399 ehdr_ptr = (Elf64_Ehdr *)elfptr;
400 phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr)); /* PT_NOTE hdr */
402 /* First program header is PT_NOTE header. */
403 vmcore_off = sizeof(Elf64_Ehdr) +
404 (ehdr_ptr->e_phnum) * sizeof(Elf64_Phdr) +
405 phdr_ptr->p_memsz; /* Note sections */
407 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
408 if (phdr_ptr->p_type != PT_LOAD)
411 /* Add this contiguous chunk of memory to vmcore list.*/
412 new = get_new_element();
415 new->paddr = phdr_ptr->p_offset;
416 new->size = phdr_ptr->p_memsz;
417 list_add_tail(&new->list, vc_list);
419 /* Update the program header offset. */
420 phdr_ptr->p_offset = vmcore_off;
421 vmcore_off = vmcore_off + phdr_ptr->p_memsz;
426 static int __init process_ptload_program_headers_elf32(char *elfptr,
428 struct list_head *vc_list)
431 Elf32_Ehdr *ehdr_ptr;
432 Elf32_Phdr *phdr_ptr;
436 ehdr_ptr = (Elf32_Ehdr *)elfptr;
437 phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr)); /* PT_NOTE hdr */
439 /* First program header is PT_NOTE header. */
440 vmcore_off = sizeof(Elf32_Ehdr) +
441 (ehdr_ptr->e_phnum) * sizeof(Elf32_Phdr) +
442 phdr_ptr->p_memsz; /* Note sections */
444 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
445 if (phdr_ptr->p_type != PT_LOAD)
448 /* Add this contiguous chunk of memory to vmcore list.*/
449 new = get_new_element();
452 new->paddr = phdr_ptr->p_offset;
453 new->size = phdr_ptr->p_memsz;
454 list_add_tail(&new->list, vc_list);
456 /* Update the program header offset */
457 phdr_ptr->p_offset = vmcore_off;
458 vmcore_off = vmcore_off + phdr_ptr->p_memsz;
463 /* Sets offset fields of vmcore elements. */
464 static void __init set_vmcore_list_offsets_elf64(char *elfptr,
465 struct list_head *vc_list)
468 Elf64_Ehdr *ehdr_ptr;
471 ehdr_ptr = (Elf64_Ehdr *)elfptr;
473 /* Skip Elf header and program headers. */
474 vmcore_off = sizeof(Elf64_Ehdr) +
475 (ehdr_ptr->e_phnum) * sizeof(Elf64_Phdr);
477 list_for_each_entry(m, vc_list, list) {
478 m->offset = vmcore_off;
479 vmcore_off += m->size;
483 /* Sets offset fields of vmcore elements. */
484 static void __init set_vmcore_list_offsets_elf32(char *elfptr,
485 struct list_head *vc_list)
488 Elf32_Ehdr *ehdr_ptr;
491 ehdr_ptr = (Elf32_Ehdr *)elfptr;
493 /* Skip Elf header and program headers. */
494 vmcore_off = sizeof(Elf32_Ehdr) +
495 (ehdr_ptr->e_phnum) * sizeof(Elf32_Phdr);
497 list_for_each_entry(m, vc_list, list) {
498 m->offset = vmcore_off;
499 vmcore_off += m->size;
503 static int __init parse_crash_elf64_headers(void)
509 addr = elfcorehdr_addr;
511 /* Read Elf header */
512 rc = read_from_oldmem((char*)&ehdr, sizeof(Elf64_Ehdr), &addr, 0);
516 /* Do some basic Verification. */
517 if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
518 (ehdr.e_type != ET_CORE) ||
519 !elf_check_arch(&ehdr) ||
520 ehdr.e_ident[EI_CLASS] != ELFCLASS64 ||
521 ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
522 ehdr.e_version != EV_CURRENT ||
523 ehdr.e_ehsize != sizeof(Elf64_Ehdr) ||
524 ehdr.e_phentsize != sizeof(Elf64_Phdr) ||
526 printk(KERN_WARNING "Warning: Core image elf header is not"
531 /* Read in all elf headers. */
532 elfcorebuf_sz = sizeof(Elf64_Ehdr) + ehdr.e_phnum * sizeof(Elf64_Phdr);
533 elfcorebuf = kmalloc(elfcorebuf_sz, GFP_KERNEL);
536 addr = elfcorehdr_addr;
537 rc = read_from_oldmem(elfcorebuf, elfcorebuf_sz, &addr, 0);
543 /* Merge all PT_NOTE headers into one. */
544 rc = merge_note_headers_elf64(elfcorebuf, &elfcorebuf_sz, &vmcore_list);
549 rc = process_ptload_program_headers_elf64(elfcorebuf, elfcorebuf_sz,
555 set_vmcore_list_offsets_elf64(elfcorebuf, &vmcore_list);
559 static int __init parse_crash_elf32_headers(void)
565 addr = elfcorehdr_addr;
567 /* Read Elf header */
568 rc = read_from_oldmem((char*)&ehdr, sizeof(Elf32_Ehdr), &addr, 0);
572 /* Do some basic Verification. */
573 if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
574 (ehdr.e_type != ET_CORE) ||
575 !elf_check_arch(&ehdr) ||
576 ehdr.e_ident[EI_CLASS] != ELFCLASS32||
577 ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
578 ehdr.e_version != EV_CURRENT ||
579 ehdr.e_ehsize != sizeof(Elf32_Ehdr) ||
580 ehdr.e_phentsize != sizeof(Elf32_Phdr) ||
582 printk(KERN_WARNING "Warning: Core image elf header is not"
587 /* Read in all elf headers. */
588 elfcorebuf_sz = sizeof(Elf32_Ehdr) + ehdr.e_phnum * sizeof(Elf32_Phdr);
589 elfcorebuf = kmalloc(elfcorebuf_sz, GFP_KERNEL);
592 addr = elfcorehdr_addr;
593 rc = read_from_oldmem(elfcorebuf, elfcorebuf_sz, &addr, 0);
599 /* Merge all PT_NOTE headers into one. */
600 rc = merge_note_headers_elf32(elfcorebuf, &elfcorebuf_sz, &vmcore_list);
605 rc = process_ptload_program_headers_elf32(elfcorebuf, elfcorebuf_sz,
611 set_vmcore_list_offsets_elf32(elfcorebuf, &vmcore_list);
615 static int __init parse_crash_elf_headers(void)
617 unsigned char e_ident[EI_NIDENT];
621 addr = elfcorehdr_addr;
622 rc = read_from_oldmem(e_ident, EI_NIDENT, &addr, 0);
625 if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) {
626 printk(KERN_WARNING "Warning: Core image elf header"
631 if (e_ident[EI_CLASS] == ELFCLASS64) {
632 rc = parse_crash_elf64_headers();
636 /* Determine vmcore size. */
637 vmcore_size = get_vmcore_size_elf64(elfcorebuf);
638 } else if (e_ident[EI_CLASS] == ELFCLASS32) {
639 rc = parse_crash_elf32_headers();
643 /* Determine vmcore size. */
644 vmcore_size = get_vmcore_size_elf32(elfcorebuf);
646 printk(KERN_WARNING "Warning: Core image elf header is not"
653 /* Init function for vmcore module. */
654 static int __init vmcore_init(void)
658 /* If elfcorehdr= has been passed in cmdline, then capture the dump.*/
659 if (!(elfcorehdr_addr < ELFCORE_ADDR_MAX))
661 rc = parse_crash_elf_headers();
663 printk(KERN_WARNING "Kdump: vmcore not initialized\n");
667 /* Initialize /proc/vmcore size if proc is already up. */
669 proc_vmcore->size = vmcore_size;
672 module_init(vmcore_init)