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/kcore.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>
27 /* List representing chunks of contiguous memory areas and their offsets in
30 static LIST_HEAD(vmcore_list);
32 /* Stores the pointer to the buffer containing kernel elf core headers. */
33 static char *elfcorebuf;
34 static size_t elfcorebuf_sz;
36 /* Total size of vmcore file. */
37 static u64 vmcore_size;
39 static struct proc_dir_entry *proc_vmcore = NULL;
42 * Returns > 0 for RAM pages, 0 for non-RAM pages, < 0 on error
43 * The called function has to take care of module refcounting.
45 static int (*oldmem_pfn_is_ram)(unsigned long pfn);
47 int register_oldmem_pfn_is_ram(int (*fn)(unsigned long pfn))
49 if (oldmem_pfn_is_ram)
51 oldmem_pfn_is_ram = fn;
54 EXPORT_SYMBOL_GPL(register_oldmem_pfn_is_ram);
56 void unregister_oldmem_pfn_is_ram(void)
58 oldmem_pfn_is_ram = NULL;
61 EXPORT_SYMBOL_GPL(unregister_oldmem_pfn_is_ram);
63 static int pfn_is_ram(unsigned long pfn)
65 int (*fn)(unsigned long pfn);
66 /* pfn is ram unless fn() checks pagetype */
70 * Ask hypervisor if the pfn is really ram.
71 * A ballooned page contains no data and reading from such a page
72 * will cause high load in the hypervisor.
74 fn = oldmem_pfn_is_ram;
81 /* Reads a page from the oldmem device from given offset. */
82 static ssize_t read_from_oldmem(char *buf, size_t count,
83 u64 *ppos, int userbuf)
85 unsigned long pfn, offset;
87 ssize_t read = 0, tmp;
92 offset = (unsigned long)(*ppos % PAGE_SIZE);
93 pfn = (unsigned long)(*ppos / PAGE_SIZE);
96 if (count > (PAGE_SIZE - offset))
97 nr_bytes = PAGE_SIZE - offset;
101 /* If pfn is not ram, return zeros for sparse dump files */
102 if (pfn_is_ram(pfn) == 0)
103 memset(buf, 0, nr_bytes);
105 tmp = copy_oldmem_page(pfn, buf, nr_bytes,
121 /* Read from the ELF header and then the crash dump. On error, negative value is
122 * returned otherwise number of bytes read are returned.
124 static ssize_t read_vmcore(struct file *file, char __user *buffer,
125 size_t buflen, loff_t *fpos)
127 ssize_t acc = 0, tmp;
130 struct vmcore *m = NULL;
132 if (buflen == 0 || *fpos >= vmcore_size)
135 /* trim buflen to not go beyond EOF */
136 if (buflen > vmcore_size - *fpos)
137 buflen = vmcore_size - *fpos;
139 /* Read ELF core header */
140 if (*fpos < elfcorebuf_sz) {
141 tsz = elfcorebuf_sz - *fpos;
144 if (copy_to_user(buffer, elfcorebuf + *fpos, tsz))
151 /* leave now if filled buffer already */
156 list_for_each_entry(m, &vmcore_list, list) {
157 if (*fpos < m->offset + m->size) {
158 tsz = m->offset + m->size - *fpos;
161 start = m->paddr + *fpos - m->offset;
162 tmp = read_from_oldmem(buffer, tsz, &start, 1);
170 /* leave now if filled buffer already */
179 static const struct file_operations proc_vmcore_operations = {
181 .llseek = default_llseek,
184 static struct vmcore* __init get_new_element(void)
186 return kzalloc(sizeof(struct vmcore), GFP_KERNEL);
189 static u64 __init get_vmcore_size_elf64(char *elfptr)
193 Elf64_Ehdr *ehdr_ptr;
194 Elf64_Phdr *phdr_ptr;
196 ehdr_ptr = (Elf64_Ehdr *)elfptr;
197 phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr));
198 size = sizeof(Elf64_Ehdr) + ((ehdr_ptr->e_phnum) * sizeof(Elf64_Phdr));
199 for (i = 0; i < ehdr_ptr->e_phnum; i++) {
200 size += phdr_ptr->p_memsz;
206 static u64 __init get_vmcore_size_elf32(char *elfptr)
210 Elf32_Ehdr *ehdr_ptr;
211 Elf32_Phdr *phdr_ptr;
213 ehdr_ptr = (Elf32_Ehdr *)elfptr;
214 phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr));
215 size = sizeof(Elf32_Ehdr) + ((ehdr_ptr->e_phnum) * sizeof(Elf32_Phdr));
216 for (i = 0; i < ehdr_ptr->e_phnum; i++) {
217 size += phdr_ptr->p_memsz;
223 /* Merges all the PT_NOTE headers into one. */
224 static int __init merge_note_headers_elf64(char *elfptr, size_t *elfsz,
225 struct list_head *vc_list)
227 int i, nr_ptnote=0, rc=0;
229 Elf64_Ehdr *ehdr_ptr;
230 Elf64_Phdr phdr, *phdr_ptr;
231 Elf64_Nhdr *nhdr_ptr;
232 u64 phdr_sz = 0, note_off;
234 ehdr_ptr = (Elf64_Ehdr *)elfptr;
235 phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr));
236 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
240 u64 offset, max_sz, sz, real_sz = 0;
241 if (phdr_ptr->p_type != PT_NOTE)
244 max_sz = phdr_ptr->p_memsz;
245 offset = phdr_ptr->p_offset;
246 notes_section = kmalloc(max_sz, GFP_KERNEL);
249 rc = read_from_oldmem(notes_section, max_sz, &offset, 0);
251 kfree(notes_section);
254 nhdr_ptr = notes_section;
255 for (j = 0; j < max_sz; j += sz) {
256 if (nhdr_ptr->n_namesz == 0)
258 sz = sizeof(Elf64_Nhdr) +
259 ((nhdr_ptr->n_namesz + 3) & ~3) +
260 ((nhdr_ptr->n_descsz + 3) & ~3);
262 nhdr_ptr = (Elf64_Nhdr*)((char*)nhdr_ptr + sz);
265 /* Add this contiguous chunk of notes section to vmcore list.*/
266 new = get_new_element();
268 kfree(notes_section);
271 new->paddr = phdr_ptr->p_offset;
273 list_add_tail(&new->list, vc_list);
275 kfree(notes_section);
278 /* Prepare merged PT_NOTE program header. */
279 phdr.p_type = PT_NOTE;
281 note_off = sizeof(Elf64_Ehdr) +
282 (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf64_Phdr);
283 phdr.p_offset = note_off;
284 phdr.p_vaddr = phdr.p_paddr = 0;
285 phdr.p_filesz = phdr.p_memsz = phdr_sz;
288 /* Add merged PT_NOTE program header*/
289 tmp = elfptr + sizeof(Elf64_Ehdr);
290 memcpy(tmp, &phdr, sizeof(phdr));
293 /* Remove unwanted PT_NOTE program headers. */
294 i = (nr_ptnote - 1) * sizeof(Elf64_Phdr);
296 memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf64_Ehdr)-sizeof(Elf64_Phdr)));
298 /* Modify e_phnum to reflect merged headers. */
299 ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
304 /* Merges all the PT_NOTE headers into one. */
305 static int __init merge_note_headers_elf32(char *elfptr, size_t *elfsz,
306 struct list_head *vc_list)
308 int i, nr_ptnote=0, rc=0;
310 Elf32_Ehdr *ehdr_ptr;
311 Elf32_Phdr phdr, *phdr_ptr;
312 Elf32_Nhdr *nhdr_ptr;
313 u64 phdr_sz = 0, note_off;
315 ehdr_ptr = (Elf32_Ehdr *)elfptr;
316 phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr));
317 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
321 u64 offset, max_sz, sz, real_sz = 0;
322 if (phdr_ptr->p_type != PT_NOTE)
325 max_sz = phdr_ptr->p_memsz;
326 offset = phdr_ptr->p_offset;
327 notes_section = kmalloc(max_sz, GFP_KERNEL);
330 rc = read_from_oldmem(notes_section, max_sz, &offset, 0);
332 kfree(notes_section);
335 nhdr_ptr = notes_section;
336 for (j = 0; j < max_sz; j += sz) {
337 if (nhdr_ptr->n_namesz == 0)
339 sz = sizeof(Elf32_Nhdr) +
340 ((nhdr_ptr->n_namesz + 3) & ~3) +
341 ((nhdr_ptr->n_descsz + 3) & ~3);
343 nhdr_ptr = (Elf32_Nhdr*)((char*)nhdr_ptr + sz);
346 /* Add this contiguous chunk of notes section to vmcore list.*/
347 new = get_new_element();
349 kfree(notes_section);
352 new->paddr = phdr_ptr->p_offset;
354 list_add_tail(&new->list, vc_list);
356 kfree(notes_section);
359 /* Prepare merged PT_NOTE program header. */
360 phdr.p_type = PT_NOTE;
362 note_off = sizeof(Elf32_Ehdr) +
363 (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf32_Phdr);
364 phdr.p_offset = note_off;
365 phdr.p_vaddr = phdr.p_paddr = 0;
366 phdr.p_filesz = phdr.p_memsz = phdr_sz;
369 /* Add merged PT_NOTE program header*/
370 tmp = elfptr + sizeof(Elf32_Ehdr);
371 memcpy(tmp, &phdr, sizeof(phdr));
374 /* Remove unwanted PT_NOTE program headers. */
375 i = (nr_ptnote - 1) * sizeof(Elf32_Phdr);
377 memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf32_Ehdr)-sizeof(Elf32_Phdr)));
379 /* Modify e_phnum to reflect merged headers. */
380 ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
385 /* Add memory chunks represented by program headers to vmcore list. Also update
386 * the new offset fields of exported program headers. */
387 static int __init process_ptload_program_headers_elf64(char *elfptr,
389 struct list_head *vc_list)
392 Elf64_Ehdr *ehdr_ptr;
393 Elf64_Phdr *phdr_ptr;
397 ehdr_ptr = (Elf64_Ehdr *)elfptr;
398 phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr)); /* PT_NOTE hdr */
400 /* First program header is PT_NOTE header. */
401 vmcore_off = sizeof(Elf64_Ehdr) +
402 (ehdr_ptr->e_phnum) * sizeof(Elf64_Phdr) +
403 phdr_ptr->p_memsz; /* Note sections */
405 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
406 if (phdr_ptr->p_type != PT_LOAD)
409 /* Add this contiguous chunk of memory to vmcore list.*/
410 new = get_new_element();
413 new->paddr = phdr_ptr->p_offset;
414 new->size = phdr_ptr->p_memsz;
415 list_add_tail(&new->list, vc_list);
417 /* Update the program header offset. */
418 phdr_ptr->p_offset = vmcore_off;
419 vmcore_off = vmcore_off + phdr_ptr->p_memsz;
424 static int __init process_ptload_program_headers_elf32(char *elfptr,
426 struct list_head *vc_list)
429 Elf32_Ehdr *ehdr_ptr;
430 Elf32_Phdr *phdr_ptr;
434 ehdr_ptr = (Elf32_Ehdr *)elfptr;
435 phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr)); /* PT_NOTE hdr */
437 /* First program header is PT_NOTE header. */
438 vmcore_off = sizeof(Elf32_Ehdr) +
439 (ehdr_ptr->e_phnum) * sizeof(Elf32_Phdr) +
440 phdr_ptr->p_memsz; /* Note sections */
442 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
443 if (phdr_ptr->p_type != PT_LOAD)
446 /* Add this contiguous chunk of memory to vmcore list.*/
447 new = get_new_element();
450 new->paddr = phdr_ptr->p_offset;
451 new->size = phdr_ptr->p_memsz;
452 list_add_tail(&new->list, vc_list);
454 /* Update the program header offset */
455 phdr_ptr->p_offset = vmcore_off;
456 vmcore_off = vmcore_off + phdr_ptr->p_memsz;
461 /* Sets offset fields of vmcore elements. */
462 static void __init set_vmcore_list_offsets_elf64(char *elfptr,
463 struct list_head *vc_list)
466 Elf64_Ehdr *ehdr_ptr;
469 ehdr_ptr = (Elf64_Ehdr *)elfptr;
471 /* Skip Elf header and program headers. */
472 vmcore_off = sizeof(Elf64_Ehdr) +
473 (ehdr_ptr->e_phnum) * sizeof(Elf64_Phdr);
475 list_for_each_entry(m, vc_list, list) {
476 m->offset = vmcore_off;
477 vmcore_off += m->size;
481 /* Sets offset fields of vmcore elements. */
482 static void __init set_vmcore_list_offsets_elf32(char *elfptr,
483 struct list_head *vc_list)
486 Elf32_Ehdr *ehdr_ptr;
489 ehdr_ptr = (Elf32_Ehdr *)elfptr;
491 /* Skip Elf header and program headers. */
492 vmcore_off = sizeof(Elf32_Ehdr) +
493 (ehdr_ptr->e_phnum) * sizeof(Elf32_Phdr);
495 list_for_each_entry(m, vc_list, list) {
496 m->offset = vmcore_off;
497 vmcore_off += m->size;
501 static int __init parse_crash_elf64_headers(void)
507 addr = elfcorehdr_addr;
509 /* Read Elf header */
510 rc = read_from_oldmem((char*)&ehdr, sizeof(Elf64_Ehdr), &addr, 0);
514 /* Do some basic Verification. */
515 if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
516 (ehdr.e_type != ET_CORE) ||
517 !vmcore_elf64_check_arch(&ehdr) ||
518 ehdr.e_ident[EI_CLASS] != ELFCLASS64 ||
519 ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
520 ehdr.e_version != EV_CURRENT ||
521 ehdr.e_ehsize != sizeof(Elf64_Ehdr) ||
522 ehdr.e_phentsize != sizeof(Elf64_Phdr) ||
524 pr_warn("Warning: Core image elf header is not sane\n");
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 pr_warn("Warning: Core image elf header is not sane\n");
583 /* Read in all elf headers. */
584 elfcorebuf_sz = sizeof(Elf32_Ehdr) + ehdr.e_phnum * sizeof(Elf32_Phdr);
585 elfcorebuf = kmalloc(elfcorebuf_sz, GFP_KERNEL);
588 addr = elfcorehdr_addr;
589 rc = read_from_oldmem(elfcorebuf, elfcorebuf_sz, &addr, 0);
595 /* Merge all PT_NOTE headers into one. */
596 rc = merge_note_headers_elf32(elfcorebuf, &elfcorebuf_sz, &vmcore_list);
601 rc = process_ptload_program_headers_elf32(elfcorebuf, elfcorebuf_sz,
607 set_vmcore_list_offsets_elf32(elfcorebuf, &vmcore_list);
611 static int __init parse_crash_elf_headers(void)
613 unsigned char e_ident[EI_NIDENT];
617 addr = elfcorehdr_addr;
618 rc = read_from_oldmem(e_ident, EI_NIDENT, &addr, 0);
621 if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) {
622 pr_warn("Warning: Core image elf header not found\n");
626 if (e_ident[EI_CLASS] == ELFCLASS64) {
627 rc = parse_crash_elf64_headers();
631 /* Determine vmcore size. */
632 vmcore_size = get_vmcore_size_elf64(elfcorebuf);
633 } else if (e_ident[EI_CLASS] == ELFCLASS32) {
634 rc = parse_crash_elf32_headers();
638 /* Determine vmcore size. */
639 vmcore_size = get_vmcore_size_elf32(elfcorebuf);
641 pr_warn("Warning: Core image elf header is not sane\n");
647 /* Init function for vmcore module. */
648 static int __init vmcore_init(void)
652 /* If elfcorehdr= has been passed in cmdline, then capture the dump.*/
653 if (!(is_vmcore_usable()))
655 rc = parse_crash_elf_headers();
657 pr_warn("Kdump: vmcore not initialized\n");
661 proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations);
663 proc_vmcore->size = vmcore_size;
666 module_init(vmcore_init)
668 /* Cleanup function for vmcore module. */
669 void vmcore_cleanup(void)
671 struct list_head *pos, *next;
674 proc_remove(proc_vmcore);
678 /* clear the vmcore list. */
679 list_for_each_safe(pos, next, &vmcore_list) {
682 m = list_entry(pos, struct vmcore, list);
689 EXPORT_SYMBOL_GPL(vmcore_cleanup);