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/export.h>
16 #include <linux/slab.h>
17 #include <linux/highmem.h>
18 #include <linux/printk.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 static struct proc_dir_entry *proc_vmcore = NULL;
41 * Returns > 0 for RAM pages, 0 for non-RAM pages, < 0 on error
42 * The called function has to take care of module refcounting.
44 static int (*oldmem_pfn_is_ram)(unsigned long pfn);
46 int register_oldmem_pfn_is_ram(int (*fn)(unsigned long pfn))
48 if (oldmem_pfn_is_ram)
50 oldmem_pfn_is_ram = fn;
53 EXPORT_SYMBOL_GPL(register_oldmem_pfn_is_ram);
55 void unregister_oldmem_pfn_is_ram(void)
57 oldmem_pfn_is_ram = NULL;
60 EXPORT_SYMBOL_GPL(unregister_oldmem_pfn_is_ram);
62 static int pfn_is_ram(unsigned long pfn)
64 int (*fn)(unsigned long pfn);
65 /* pfn is ram unless fn() checks pagetype */
69 * Ask hypervisor if the pfn is really ram.
70 * A ballooned page contains no data and reading from such a page
71 * will cause high load in the hypervisor.
73 fn = oldmem_pfn_is_ram;
80 /* Reads a page from the oldmem device from given offset. */
81 static ssize_t read_from_oldmem(char *buf, size_t count,
82 u64 *ppos, int userbuf)
84 unsigned long pfn, offset;
86 ssize_t read = 0, tmp;
91 offset = (unsigned long)(*ppos % PAGE_SIZE);
92 pfn = (unsigned long)(*ppos / PAGE_SIZE);
95 if (count > (PAGE_SIZE - offset))
96 nr_bytes = PAGE_SIZE - offset;
100 /* If pfn is not ram, return zeros for sparse dump files */
101 if (pfn_is_ram(pfn) == 0)
102 memset(buf, 0, nr_bytes);
104 tmp = copy_oldmem_page(pfn, buf, nr_bytes,
120 /* Maps vmcore file offset to respective physical address in memroy. */
121 static u64 map_offset_to_paddr(loff_t offset, struct list_head *vc_list,
122 struct vmcore **m_ptr)
127 list_for_each_entry(m, vc_list, list) {
130 end = m->offset + m->size - 1;
131 if (offset >= start && offset <= end) {
132 paddr = m->paddr + offset - start;
141 /* Read from the ELF header and then the crash dump. On error, negative value is
142 * returned otherwise number of bytes read are returned.
144 static ssize_t read_vmcore(struct file *file, char __user *buffer,
145 size_t buflen, loff_t *fpos)
147 ssize_t acc = 0, tmp;
150 struct vmcore *curr_m = NULL;
152 if (buflen == 0 || *fpos >= vmcore_size)
155 /* trim buflen to not go beyond EOF */
156 if (buflen > vmcore_size - *fpos)
157 buflen = vmcore_size - *fpos;
159 /* Read ELF core header */
160 if (*fpos < elfcorebuf_sz) {
161 tsz = elfcorebuf_sz - *fpos;
164 if (copy_to_user(buffer, elfcorebuf + *fpos, tsz))
171 /* leave now if filled buffer already */
176 start = map_offset_to_paddr(*fpos, &vmcore_list, &curr_m);
179 if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
182 /* Calculate left bytes in current memory segment. */
183 nr_bytes = (curr_m->size - (start - curr_m->paddr));
188 tmp = read_from_oldmem(buffer, tsz, &start, 1);
195 if (start >= (curr_m->paddr + curr_m->size)) {
196 if (curr_m->list.next == &vmcore_list)
198 curr_m = list_entry(curr_m->list.next,
199 struct vmcore, list);
200 start = curr_m->paddr;
202 if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
204 /* Calculate left bytes in current memory segment. */
205 nr_bytes = (curr_m->size - (start - curr_m->paddr));
212 static const struct file_operations proc_vmcore_operations = {
214 .llseek = default_llseek,
217 static struct vmcore* __init get_new_element(void)
219 return kzalloc(sizeof(struct vmcore), GFP_KERNEL);
222 static u64 __init get_vmcore_size_elf64(char *elfptr)
226 Elf64_Ehdr *ehdr_ptr;
227 Elf64_Phdr *phdr_ptr;
229 ehdr_ptr = (Elf64_Ehdr *)elfptr;
230 phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr));
231 size = sizeof(Elf64_Ehdr) + ((ehdr_ptr->e_phnum) * sizeof(Elf64_Phdr));
232 for (i = 0; i < ehdr_ptr->e_phnum; i++) {
233 size += phdr_ptr->p_memsz;
239 static u64 __init get_vmcore_size_elf32(char *elfptr)
243 Elf32_Ehdr *ehdr_ptr;
244 Elf32_Phdr *phdr_ptr;
246 ehdr_ptr = (Elf32_Ehdr *)elfptr;
247 phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr));
248 size = sizeof(Elf32_Ehdr) + ((ehdr_ptr->e_phnum) * sizeof(Elf32_Phdr));
249 for (i = 0; i < ehdr_ptr->e_phnum; i++) {
250 size += phdr_ptr->p_memsz;
256 /* Merges all the PT_NOTE headers into one. */
257 static int __init merge_note_headers_elf64(char *elfptr, size_t *elfsz,
258 struct list_head *vc_list)
260 int i, nr_ptnote=0, rc=0;
262 Elf64_Ehdr *ehdr_ptr;
263 Elf64_Phdr phdr, *phdr_ptr;
264 Elf64_Nhdr *nhdr_ptr;
265 u64 phdr_sz = 0, note_off;
267 ehdr_ptr = (Elf64_Ehdr *)elfptr;
268 phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr));
269 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
273 u64 offset, max_sz, sz, real_sz = 0;
274 if (phdr_ptr->p_type != PT_NOTE)
277 max_sz = phdr_ptr->p_memsz;
278 offset = phdr_ptr->p_offset;
279 notes_section = kmalloc(max_sz, GFP_KERNEL);
282 rc = read_from_oldmem(notes_section, max_sz, &offset, 0);
284 kfree(notes_section);
287 nhdr_ptr = notes_section;
288 for (j = 0; j < max_sz; j += sz) {
289 if (nhdr_ptr->n_namesz == 0)
291 sz = sizeof(Elf64_Nhdr) +
292 ((nhdr_ptr->n_namesz + 3) & ~3) +
293 ((nhdr_ptr->n_descsz + 3) & ~3);
295 nhdr_ptr = (Elf64_Nhdr*)((char*)nhdr_ptr + sz);
298 /* Add this contiguous chunk of notes section to vmcore list.*/
299 new = get_new_element();
301 kfree(notes_section);
304 new->paddr = phdr_ptr->p_offset;
306 list_add_tail(&new->list, vc_list);
308 kfree(notes_section);
311 /* Prepare merged PT_NOTE program header. */
312 phdr.p_type = PT_NOTE;
314 note_off = sizeof(Elf64_Ehdr) +
315 (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf64_Phdr);
316 phdr.p_offset = note_off;
317 phdr.p_vaddr = phdr.p_paddr = 0;
318 phdr.p_filesz = phdr.p_memsz = phdr_sz;
321 /* Add merged PT_NOTE program header*/
322 tmp = elfptr + sizeof(Elf64_Ehdr);
323 memcpy(tmp, &phdr, sizeof(phdr));
326 /* Remove unwanted PT_NOTE program headers. */
327 i = (nr_ptnote - 1) * sizeof(Elf64_Phdr);
329 memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf64_Ehdr)-sizeof(Elf64_Phdr)));
331 /* Modify e_phnum to reflect merged headers. */
332 ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
337 /* Merges all the PT_NOTE headers into one. */
338 static int __init merge_note_headers_elf32(char *elfptr, size_t *elfsz,
339 struct list_head *vc_list)
341 int i, nr_ptnote=0, rc=0;
343 Elf32_Ehdr *ehdr_ptr;
344 Elf32_Phdr phdr, *phdr_ptr;
345 Elf32_Nhdr *nhdr_ptr;
346 u64 phdr_sz = 0, note_off;
348 ehdr_ptr = (Elf32_Ehdr *)elfptr;
349 phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr));
350 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
354 u64 offset, max_sz, sz, real_sz = 0;
355 if (phdr_ptr->p_type != PT_NOTE)
358 max_sz = phdr_ptr->p_memsz;
359 offset = phdr_ptr->p_offset;
360 notes_section = kmalloc(max_sz, GFP_KERNEL);
363 rc = read_from_oldmem(notes_section, max_sz, &offset, 0);
365 kfree(notes_section);
368 nhdr_ptr = notes_section;
369 for (j = 0; j < max_sz; j += sz) {
370 if (nhdr_ptr->n_namesz == 0)
372 sz = sizeof(Elf32_Nhdr) +
373 ((nhdr_ptr->n_namesz + 3) & ~3) +
374 ((nhdr_ptr->n_descsz + 3) & ~3);
376 nhdr_ptr = (Elf32_Nhdr*)((char*)nhdr_ptr + sz);
379 /* Add this contiguous chunk of notes section to vmcore list.*/
380 new = get_new_element();
382 kfree(notes_section);
385 new->paddr = phdr_ptr->p_offset;
387 list_add_tail(&new->list, vc_list);
389 kfree(notes_section);
392 /* Prepare merged PT_NOTE program header. */
393 phdr.p_type = PT_NOTE;
395 note_off = sizeof(Elf32_Ehdr) +
396 (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf32_Phdr);
397 phdr.p_offset = note_off;
398 phdr.p_vaddr = phdr.p_paddr = 0;
399 phdr.p_filesz = phdr.p_memsz = phdr_sz;
402 /* Add merged PT_NOTE program header*/
403 tmp = elfptr + sizeof(Elf32_Ehdr);
404 memcpy(tmp, &phdr, sizeof(phdr));
407 /* Remove unwanted PT_NOTE program headers. */
408 i = (nr_ptnote - 1) * sizeof(Elf32_Phdr);
410 memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf32_Ehdr)-sizeof(Elf32_Phdr)));
412 /* Modify e_phnum to reflect merged headers. */
413 ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
418 /* Add memory chunks represented by program headers to vmcore list. Also update
419 * the new offset fields of exported program headers. */
420 static int __init process_ptload_program_headers_elf64(char *elfptr,
422 struct list_head *vc_list)
425 Elf64_Ehdr *ehdr_ptr;
426 Elf64_Phdr *phdr_ptr;
430 ehdr_ptr = (Elf64_Ehdr *)elfptr;
431 phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr)); /* PT_NOTE hdr */
433 /* First program header is PT_NOTE header. */
434 vmcore_off = sizeof(Elf64_Ehdr) +
435 (ehdr_ptr->e_phnum) * sizeof(Elf64_Phdr) +
436 phdr_ptr->p_memsz; /* Note sections */
438 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
439 if (phdr_ptr->p_type != PT_LOAD)
442 /* Add this contiguous chunk of memory to vmcore list.*/
443 new = get_new_element();
446 new->paddr = phdr_ptr->p_offset;
447 new->size = phdr_ptr->p_memsz;
448 list_add_tail(&new->list, vc_list);
450 /* Update the program header offset. */
451 phdr_ptr->p_offset = vmcore_off;
452 vmcore_off = vmcore_off + phdr_ptr->p_memsz;
457 static int __init process_ptload_program_headers_elf32(char *elfptr,
459 struct list_head *vc_list)
462 Elf32_Ehdr *ehdr_ptr;
463 Elf32_Phdr *phdr_ptr;
467 ehdr_ptr = (Elf32_Ehdr *)elfptr;
468 phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr)); /* PT_NOTE hdr */
470 /* First program header is PT_NOTE header. */
471 vmcore_off = sizeof(Elf32_Ehdr) +
472 (ehdr_ptr->e_phnum) * sizeof(Elf32_Phdr) +
473 phdr_ptr->p_memsz; /* Note sections */
475 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
476 if (phdr_ptr->p_type != PT_LOAD)
479 /* Add this contiguous chunk of memory to vmcore list.*/
480 new = get_new_element();
483 new->paddr = phdr_ptr->p_offset;
484 new->size = phdr_ptr->p_memsz;
485 list_add_tail(&new->list, vc_list);
487 /* Update the program header offset */
488 phdr_ptr->p_offset = vmcore_off;
489 vmcore_off = vmcore_off + phdr_ptr->p_memsz;
494 /* Sets offset fields of vmcore elements. */
495 static void __init set_vmcore_list_offsets_elf64(char *elfptr,
496 struct list_head *vc_list)
499 Elf64_Ehdr *ehdr_ptr;
502 ehdr_ptr = (Elf64_Ehdr *)elfptr;
504 /* Skip Elf header and program headers. */
505 vmcore_off = sizeof(Elf64_Ehdr) +
506 (ehdr_ptr->e_phnum) * sizeof(Elf64_Phdr);
508 list_for_each_entry(m, vc_list, list) {
509 m->offset = vmcore_off;
510 vmcore_off += m->size;
514 /* Sets offset fields of vmcore elements. */
515 static void __init set_vmcore_list_offsets_elf32(char *elfptr,
516 struct list_head *vc_list)
519 Elf32_Ehdr *ehdr_ptr;
522 ehdr_ptr = (Elf32_Ehdr *)elfptr;
524 /* Skip Elf header and program headers. */
525 vmcore_off = sizeof(Elf32_Ehdr) +
526 (ehdr_ptr->e_phnum) * sizeof(Elf32_Phdr);
528 list_for_each_entry(m, vc_list, list) {
529 m->offset = vmcore_off;
530 vmcore_off += m->size;
534 static int __init parse_crash_elf64_headers(void)
540 addr = elfcorehdr_addr;
542 /* Read Elf header */
543 rc = read_from_oldmem((char*)&ehdr, sizeof(Elf64_Ehdr), &addr, 0);
547 /* Do some basic Verification. */
548 if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
549 (ehdr.e_type != ET_CORE) ||
550 !vmcore_elf64_check_arch(&ehdr) ||
551 ehdr.e_ident[EI_CLASS] != ELFCLASS64 ||
552 ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
553 ehdr.e_version != EV_CURRENT ||
554 ehdr.e_ehsize != sizeof(Elf64_Ehdr) ||
555 ehdr.e_phentsize != sizeof(Elf64_Phdr) ||
557 pr_warn("Warning: Core image elf header is not sane\n");
561 /* Read in all elf headers. */
562 elfcorebuf_sz = sizeof(Elf64_Ehdr) + ehdr.e_phnum * sizeof(Elf64_Phdr);
563 elfcorebuf = kmalloc(elfcorebuf_sz, GFP_KERNEL);
566 addr = elfcorehdr_addr;
567 rc = read_from_oldmem(elfcorebuf, elfcorebuf_sz, &addr, 0);
573 /* Merge all PT_NOTE headers into one. */
574 rc = merge_note_headers_elf64(elfcorebuf, &elfcorebuf_sz, &vmcore_list);
579 rc = process_ptload_program_headers_elf64(elfcorebuf, elfcorebuf_sz,
585 set_vmcore_list_offsets_elf64(elfcorebuf, &vmcore_list);
589 static int __init parse_crash_elf32_headers(void)
595 addr = elfcorehdr_addr;
597 /* Read Elf header */
598 rc = read_from_oldmem((char*)&ehdr, sizeof(Elf32_Ehdr), &addr, 0);
602 /* Do some basic Verification. */
603 if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
604 (ehdr.e_type != ET_CORE) ||
605 !elf_check_arch(&ehdr) ||
606 ehdr.e_ident[EI_CLASS] != ELFCLASS32||
607 ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
608 ehdr.e_version != EV_CURRENT ||
609 ehdr.e_ehsize != sizeof(Elf32_Ehdr) ||
610 ehdr.e_phentsize != sizeof(Elf32_Phdr) ||
612 pr_warn("Warning: Core image elf header is not sane\n");
616 /* Read in all elf headers. */
617 elfcorebuf_sz = sizeof(Elf32_Ehdr) + ehdr.e_phnum * sizeof(Elf32_Phdr);
618 elfcorebuf = kmalloc(elfcorebuf_sz, GFP_KERNEL);
621 addr = elfcorehdr_addr;
622 rc = read_from_oldmem(elfcorebuf, elfcorebuf_sz, &addr, 0);
628 /* Merge all PT_NOTE headers into one. */
629 rc = merge_note_headers_elf32(elfcorebuf, &elfcorebuf_sz, &vmcore_list);
634 rc = process_ptload_program_headers_elf32(elfcorebuf, elfcorebuf_sz,
640 set_vmcore_list_offsets_elf32(elfcorebuf, &vmcore_list);
644 static int __init parse_crash_elf_headers(void)
646 unsigned char e_ident[EI_NIDENT];
650 addr = elfcorehdr_addr;
651 rc = read_from_oldmem(e_ident, EI_NIDENT, &addr, 0);
654 if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) {
655 pr_warn("Warning: Core image elf header not found\n");
659 if (e_ident[EI_CLASS] == ELFCLASS64) {
660 rc = parse_crash_elf64_headers();
664 /* Determine vmcore size. */
665 vmcore_size = get_vmcore_size_elf64(elfcorebuf);
666 } else if (e_ident[EI_CLASS] == ELFCLASS32) {
667 rc = parse_crash_elf32_headers();
671 /* Determine vmcore size. */
672 vmcore_size = get_vmcore_size_elf32(elfcorebuf);
674 pr_warn("Warning: Core image elf header is not sane\n");
680 /* Init function for vmcore module. */
681 static int __init vmcore_init(void)
685 /* If elfcorehdr= has been passed in cmdline, then capture the dump.*/
686 if (!(is_vmcore_usable()))
688 rc = parse_crash_elf_headers();
690 pr_warn("Kdump: vmcore not initialized\n");
694 proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations);
696 proc_vmcore->size = vmcore_size;
699 module_init(vmcore_init)
701 /* Cleanup function for vmcore module. */
702 void vmcore_cleanup(void)
704 struct list_head *pos, *next;
707 remove_proc_entry(proc_vmcore->name, proc_vmcore->parent);
711 /* clear the vmcore list. */
712 list_for_each_safe(pos, next, &vmcore_list) {
715 m = list_entry(pos, struct vmcore, list);
722 EXPORT_SYMBOL_GPL(vmcore_cleanup);