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 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 loff_t *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;
104 size_t tsz, nr_bytes;
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 static int open_vmcore(struct inode *inode, struct file *filp)
173 struct file_operations proc_vmcore_operations = {
178 static struct vmcore* __init get_new_element(void)
182 p = kmalloc(sizeof(*p), GFP_KERNEL);
184 memset(p, 0, sizeof(*p));
188 static u64 __init get_vmcore_size_elf64(char *elfptr)
192 Elf64_Ehdr *ehdr_ptr;
193 Elf64_Phdr *phdr_ptr;
195 ehdr_ptr = (Elf64_Ehdr *)elfptr;
196 phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr));
197 size = sizeof(Elf64_Ehdr) + ((ehdr_ptr->e_phnum) * sizeof(Elf64_Phdr));
198 for (i = 0; i < ehdr_ptr->e_phnum; i++) {
199 size += phdr_ptr->p_memsz;
205 /* Merges all the PT_NOTE headers into one. */
206 static int __init merge_note_headers_elf64(char *elfptr, size_t *elfsz,
207 struct list_head *vc_list)
209 int i, nr_ptnote=0, rc=0;
211 Elf64_Ehdr *ehdr_ptr;
212 Elf64_Phdr phdr, *phdr_ptr;
213 Elf64_Nhdr *nhdr_ptr;
214 u64 phdr_sz = 0, note_off;
216 ehdr_ptr = (Elf64_Ehdr *)elfptr;
217 phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr));
218 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
222 u64 offset, max_sz, sz, real_sz = 0;
223 if (phdr_ptr->p_type != PT_NOTE)
226 max_sz = phdr_ptr->p_memsz;
227 offset = phdr_ptr->p_offset;
228 notes_section = kmalloc(max_sz, GFP_KERNEL);
231 rc = read_from_oldmem(notes_section, max_sz, &offset, 0);
233 kfree(notes_section);
236 nhdr_ptr = notes_section;
237 for (j = 0; j < max_sz; j += sz) {
238 if (nhdr_ptr->n_namesz == 0)
240 sz = sizeof(Elf64_Nhdr) +
241 ((nhdr_ptr->n_namesz + 3) & ~3) +
242 ((nhdr_ptr->n_descsz + 3) & ~3);
244 nhdr_ptr = (Elf64_Nhdr*)((char*)nhdr_ptr + sz);
247 /* Add this contiguous chunk of notes section to vmcore list.*/
248 new = get_new_element();
250 kfree(notes_section);
253 new->paddr = phdr_ptr->p_offset;
255 list_add_tail(&new->list, vc_list);
257 kfree(notes_section);
260 /* Prepare merged PT_NOTE program header. */
261 phdr.p_type = PT_NOTE;
263 note_off = sizeof(Elf64_Ehdr) +
264 (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf64_Phdr);
265 phdr.p_offset = note_off;
266 phdr.p_vaddr = phdr.p_paddr = 0;
267 phdr.p_filesz = phdr.p_memsz = phdr_sz;
270 /* Add merged PT_NOTE program header*/
271 tmp = elfptr + sizeof(Elf64_Ehdr);
272 memcpy(tmp, &phdr, sizeof(phdr));
275 /* Remove unwanted PT_NOTE program headers. */
276 i = (nr_ptnote - 1) * sizeof(Elf64_Phdr);
278 memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf64_Ehdr)-sizeof(Elf64_Phdr)));
280 /* Modify e_phnum to reflect merged headers. */
281 ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
286 /* Add memory chunks represented by program headers to vmcore list. Also update
287 * the new offset fields of exported program headers. */
288 static int __init process_ptload_program_headers_elf64(char *elfptr,
290 struct list_head *vc_list)
293 Elf64_Ehdr *ehdr_ptr;
294 Elf64_Phdr *phdr_ptr;
298 ehdr_ptr = (Elf64_Ehdr *)elfptr;
299 phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr)); /* PT_NOTE hdr */
301 /* First program header is PT_NOTE header. */
302 vmcore_off = sizeof(Elf64_Ehdr) +
303 (ehdr_ptr->e_phnum) * sizeof(Elf64_Phdr) +
304 phdr_ptr->p_memsz; /* Note sections */
306 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
307 if (phdr_ptr->p_type != PT_LOAD)
310 /* Add this contiguous chunk of memory to vmcore list.*/
311 new = get_new_element();
314 new->paddr = phdr_ptr->p_offset;
315 new->size = phdr_ptr->p_memsz;
316 list_add_tail(&new->list, vc_list);
318 /* Update the program header offset. */
319 phdr_ptr->p_offset = vmcore_off;
320 vmcore_off = vmcore_off + phdr_ptr->p_memsz;
325 /* Sets offset fields of vmcore elements. */
326 static void __init set_vmcore_list_offsets_elf64(char *elfptr,
327 struct list_head *vc_list)
330 Elf64_Ehdr *ehdr_ptr;
333 ehdr_ptr = (Elf64_Ehdr *)elfptr;
335 /* Skip Elf header and program headers. */
336 vmcore_off = sizeof(Elf64_Ehdr) +
337 (ehdr_ptr->e_phnum) * sizeof(Elf64_Phdr);
339 list_for_each_entry(m, vc_list, list) {
340 m->offset = vmcore_off;
341 vmcore_off += m->size;
345 static int __init parse_crash_elf64_headers(void)
351 addr = elfcorehdr_addr;
353 /* Read Elf header */
354 rc = read_from_oldmem((char*)&ehdr, sizeof(Elf64_Ehdr), &addr, 0);
358 /* Do some basic Verification. */
359 if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
360 (ehdr.e_type != ET_CORE) ||
361 !elf_check_arch(&ehdr) ||
362 ehdr.e_ident[EI_CLASS] != ELFCLASS64 ||
363 ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
364 ehdr.e_version != EV_CURRENT ||
365 ehdr.e_ehsize != sizeof(Elf64_Ehdr) ||
366 ehdr.e_phentsize != sizeof(Elf64_Phdr) ||
368 printk(KERN_WARNING "Warning: Core image elf header is not"
373 /* Read in all elf headers. */
374 elfcorebuf_sz = sizeof(Elf64_Ehdr) + ehdr.e_phnum * sizeof(Elf64_Phdr);
375 elfcorebuf = kmalloc(elfcorebuf_sz, GFP_KERNEL);
378 addr = elfcorehdr_addr;
379 rc = read_from_oldmem(elfcorebuf, elfcorebuf_sz, &addr, 0);
385 /* Merge all PT_NOTE headers into one. */
386 rc = merge_note_headers_elf64(elfcorebuf, &elfcorebuf_sz, &vmcore_list);
391 rc = process_ptload_program_headers_elf64(elfcorebuf, elfcorebuf_sz,
397 set_vmcore_list_offsets_elf64(elfcorebuf, &vmcore_list);
401 static int __init parse_crash_elf_headers(void)
403 unsigned char e_ident[EI_NIDENT];
407 addr = elfcorehdr_addr;
408 rc = read_from_oldmem(e_ident, EI_NIDENT, &addr, 0);
411 if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) {
412 printk(KERN_WARNING "Warning: Core image elf header"
417 if (e_ident[EI_CLASS] == ELFCLASS64) {
418 rc = parse_crash_elf64_headers();
422 /* Determine vmcore size. */
423 vmcore_size = get_vmcore_size_elf64(elfcorebuf);
425 printk(KERN_WARNING "Warning: Core image elf header is not"
432 /* Init function for vmcore module. */
433 static int __init vmcore_init(void)
437 /* If elfcorehdr= has been passed in cmdline, then capture the dump.*/
438 if (!(elfcorehdr_addr < ELFCORE_ADDR_MAX))
440 rc = parse_crash_elf_headers();
442 printk(KERN_WARNING "Kdump: vmcore not initialized\n");
446 /* Initialize /proc/vmcore size if proc is already up. */
448 proc_vmcore->size = vmcore_size;
451 module_init(vmcore_init)