allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / fs / proc / vmcore.c
bloba38ef31651dc8a07345f200bde1d644668969bb9
1 /*
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
8 */
10 #include <linux/mm.h>
11 #include <linux/proc_fs.h>
12 #include <linux/user.h>
13 #include <linux/a.out.h>
14 #include <linux/elf.h>
15 #include <linux/elfcore.h>
16 #include <linux/highmem.h>
17 #include <linux/bootmem.h>
18 #include <linux/init.h>
19 #include <linux/crash_dump.h>
20 #include <linux/list.h>
21 #include <asm/uaccess.h>
22 #include <asm/io.h>
24 /* List representing chunks of contiguous memory areas and their offsets in
25 * vmcore file.
27 static LIST_HEAD(vmcore_list);
29 /* Stores the pointer to the buffer containing kernel elf core headers. */
30 static char *elfcorebuf;
31 static size_t elfcorebuf_sz;
33 /* Total size of vmcore file. */
34 static u64 vmcore_size;
36 /* Stores the physical address of elf header of crash image. */
37 unsigned long long elfcorehdr_addr = ELFCORE_ADDR_MAX;
39 struct proc_dir_entry *proc_vmcore = NULL;
41 /* Reads a page from the oldmem device from given offset. */
42 static ssize_t read_from_oldmem(char *buf, size_t count,
43 u64 *ppos, int userbuf)
45 unsigned long pfn, offset;
46 size_t nr_bytes;
47 ssize_t read = 0, tmp;
49 if (!count)
50 return 0;
52 offset = (unsigned long)(*ppos % PAGE_SIZE);
53 pfn = (unsigned long)(*ppos / PAGE_SIZE);
54 if (pfn > saved_max_pfn)
55 return -EINVAL;
57 do {
58 if (count > (PAGE_SIZE - offset))
59 nr_bytes = PAGE_SIZE - offset;
60 else
61 nr_bytes = count;
63 tmp = copy_oldmem_page(pfn, buf, nr_bytes, offset, userbuf);
64 if (tmp < 0)
65 return tmp;
66 *ppos += nr_bytes;
67 count -= nr_bytes;
68 buf += nr_bytes;
69 read += nr_bytes;
70 ++pfn;
71 offset = 0;
72 } while (count);
74 return read;
77 /* Maps vmcore file offset to respective physical address in memroy. */
78 static u64 map_offset_to_paddr(loff_t offset, struct list_head *vc_list,
79 struct vmcore **m_ptr)
81 struct vmcore *m;
82 u64 paddr;
84 list_for_each_entry(m, vc_list, list) {
85 u64 start, end;
86 start = m->offset;
87 end = m->offset + m->size - 1;
88 if (offset >= start && offset <= end) {
89 paddr = m->paddr + offset - start;
90 *m_ptr = m;
91 return paddr;
94 *m_ptr = NULL;
95 return 0;
98 /* Read from the ELF header and then the crash dump. On error, negative value is
99 * returned otherwise number of bytes read are returned.
101 static ssize_t read_vmcore(struct file *file, char __user *buffer,
102 size_t buflen, loff_t *fpos)
104 ssize_t acc = 0, tmp;
105 size_t tsz;
106 u64 start, nr_bytes;
107 struct vmcore *curr_m = NULL;
109 if (buflen == 0 || *fpos >= vmcore_size)
110 return 0;
112 /* trim buflen to not go beyond EOF */
113 if (buflen > vmcore_size - *fpos)
114 buflen = vmcore_size - *fpos;
116 /* Read ELF core header */
117 if (*fpos < elfcorebuf_sz) {
118 tsz = elfcorebuf_sz - *fpos;
119 if (buflen < tsz)
120 tsz = buflen;
121 if (copy_to_user(buffer, elfcorebuf + *fpos, tsz))
122 return -EFAULT;
123 buflen -= tsz;
124 *fpos += tsz;
125 buffer += tsz;
126 acc += tsz;
128 /* leave now if filled buffer already */
129 if (buflen == 0)
130 return acc;
133 start = map_offset_to_paddr(*fpos, &vmcore_list, &curr_m);
134 if (!curr_m)
135 return -EINVAL;
136 if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
137 tsz = buflen;
139 /* Calculate left bytes in current memory segment. */
140 nr_bytes = (curr_m->size - (start - curr_m->paddr));
141 if (tsz > nr_bytes)
142 tsz = nr_bytes;
144 while (buflen) {
145 tmp = read_from_oldmem(buffer, tsz, &start, 1);
146 if (tmp < 0)
147 return tmp;
148 buflen -= tsz;
149 *fpos += tsz;
150 buffer += tsz;
151 acc += tsz;
152 if (start >= (curr_m->paddr + curr_m->size)) {
153 if (curr_m->list.next == &vmcore_list)
154 return acc; /*EOF*/
155 curr_m = list_entry(curr_m->list.next,
156 struct vmcore, list);
157 start = curr_m->paddr;
159 if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
160 tsz = buflen;
161 /* Calculate left bytes in current memory segment. */
162 nr_bytes = (curr_m->size - (start - curr_m->paddr));
163 if (tsz > nr_bytes)
164 tsz = nr_bytes;
166 return acc;
169 static int open_vmcore(struct inode *inode, struct file *filp)
171 return 0;
174 const struct file_operations proc_vmcore_operations = {
175 .read = read_vmcore,
176 .open = open_vmcore,
179 static struct vmcore* __init get_new_element(void)
181 return kzalloc(sizeof(struct vmcore), GFP_KERNEL);
184 static u64 __init get_vmcore_size_elf64(char *elfptr)
186 int i;
187 u64 size;
188 Elf64_Ehdr *ehdr_ptr;
189 Elf64_Phdr *phdr_ptr;
191 ehdr_ptr = (Elf64_Ehdr *)elfptr;
192 phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr));
193 size = sizeof(Elf64_Ehdr) + ((ehdr_ptr->e_phnum) * sizeof(Elf64_Phdr));
194 for (i = 0; i < ehdr_ptr->e_phnum; i++) {
195 size += phdr_ptr->p_memsz;
196 phdr_ptr++;
198 return size;
201 static u64 __init get_vmcore_size_elf32(char *elfptr)
203 int i;
204 u64 size;
205 Elf32_Ehdr *ehdr_ptr;
206 Elf32_Phdr *phdr_ptr;
208 ehdr_ptr = (Elf32_Ehdr *)elfptr;
209 phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr));
210 size = sizeof(Elf32_Ehdr) + ((ehdr_ptr->e_phnum) * sizeof(Elf32_Phdr));
211 for (i = 0; i < ehdr_ptr->e_phnum; i++) {
212 size += phdr_ptr->p_memsz;
213 phdr_ptr++;
215 return size;
218 /* Merges all the PT_NOTE headers into one. */
219 static int __init merge_note_headers_elf64(char *elfptr, size_t *elfsz,
220 struct list_head *vc_list)
222 int i, nr_ptnote=0, rc=0;
223 char *tmp;
224 Elf64_Ehdr *ehdr_ptr;
225 Elf64_Phdr phdr, *phdr_ptr;
226 Elf64_Nhdr *nhdr_ptr;
227 u64 phdr_sz = 0, note_off;
229 ehdr_ptr = (Elf64_Ehdr *)elfptr;
230 phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr));
231 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
232 int j;
233 void *notes_section;
234 struct vmcore *new;
235 u64 offset, max_sz, sz, real_sz = 0;
236 if (phdr_ptr->p_type != PT_NOTE)
237 continue;
238 nr_ptnote++;
239 max_sz = phdr_ptr->p_memsz;
240 offset = phdr_ptr->p_offset;
241 notes_section = kmalloc(max_sz, GFP_KERNEL);
242 if (!notes_section)
243 return -ENOMEM;
244 rc = read_from_oldmem(notes_section, max_sz, &offset, 0);
245 if (rc < 0) {
246 kfree(notes_section);
247 return rc;
249 nhdr_ptr = notes_section;
250 for (j = 0; j < max_sz; j += sz) {
251 if (nhdr_ptr->n_namesz == 0)
252 break;
253 sz = sizeof(Elf64_Nhdr) +
254 ((nhdr_ptr->n_namesz + 3) & ~3) +
255 ((nhdr_ptr->n_descsz + 3) & ~3);
256 real_sz += sz;
257 nhdr_ptr = (Elf64_Nhdr*)((char*)nhdr_ptr + sz);
260 /* Add this contiguous chunk of notes section to vmcore list.*/
261 new = get_new_element();
262 if (!new) {
263 kfree(notes_section);
264 return -ENOMEM;
266 new->paddr = phdr_ptr->p_offset;
267 new->size = real_sz;
268 list_add_tail(&new->list, vc_list);
269 phdr_sz += real_sz;
270 kfree(notes_section);
273 /* Prepare merged PT_NOTE program header. */
274 phdr.p_type = PT_NOTE;
275 phdr.p_flags = 0;
276 note_off = sizeof(Elf64_Ehdr) +
277 (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf64_Phdr);
278 phdr.p_offset = note_off;
279 phdr.p_vaddr = phdr.p_paddr = 0;
280 phdr.p_filesz = phdr.p_memsz = phdr_sz;
281 phdr.p_align = 0;
283 /* Add merged PT_NOTE program header*/
284 tmp = elfptr + sizeof(Elf64_Ehdr);
285 memcpy(tmp, &phdr, sizeof(phdr));
286 tmp += sizeof(phdr);
288 /* Remove unwanted PT_NOTE program headers. */
289 i = (nr_ptnote - 1) * sizeof(Elf64_Phdr);
290 *elfsz = *elfsz - i;
291 memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf64_Ehdr)-sizeof(Elf64_Phdr)));
293 /* Modify e_phnum to reflect merged headers. */
294 ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
296 return 0;
299 /* Merges all the PT_NOTE headers into one. */
300 static int __init merge_note_headers_elf32(char *elfptr, size_t *elfsz,
301 struct list_head *vc_list)
303 int i, nr_ptnote=0, rc=0;
304 char *tmp;
305 Elf32_Ehdr *ehdr_ptr;
306 Elf32_Phdr phdr, *phdr_ptr;
307 Elf32_Nhdr *nhdr_ptr;
308 u64 phdr_sz = 0, note_off;
310 ehdr_ptr = (Elf32_Ehdr *)elfptr;
311 phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr));
312 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
313 int j;
314 void *notes_section;
315 struct vmcore *new;
316 u64 offset, max_sz, sz, real_sz = 0;
317 if (phdr_ptr->p_type != PT_NOTE)
318 continue;
319 nr_ptnote++;
320 max_sz = phdr_ptr->p_memsz;
321 offset = phdr_ptr->p_offset;
322 notes_section = kmalloc(max_sz, GFP_KERNEL);
323 if (!notes_section)
324 return -ENOMEM;
325 rc = read_from_oldmem(notes_section, max_sz, &offset, 0);
326 if (rc < 0) {
327 kfree(notes_section);
328 return rc;
330 nhdr_ptr = notes_section;
331 for (j = 0; j < max_sz; j += sz) {
332 if (nhdr_ptr->n_namesz == 0)
333 break;
334 sz = sizeof(Elf32_Nhdr) +
335 ((nhdr_ptr->n_namesz + 3) & ~3) +
336 ((nhdr_ptr->n_descsz + 3) & ~3);
337 real_sz += sz;
338 nhdr_ptr = (Elf32_Nhdr*)((char*)nhdr_ptr + sz);
341 /* Add this contiguous chunk of notes section to vmcore list.*/
342 new = get_new_element();
343 if (!new) {
344 kfree(notes_section);
345 return -ENOMEM;
347 new->paddr = phdr_ptr->p_offset;
348 new->size = real_sz;
349 list_add_tail(&new->list, vc_list);
350 phdr_sz += real_sz;
351 kfree(notes_section);
354 /* Prepare merged PT_NOTE program header. */
355 phdr.p_type = PT_NOTE;
356 phdr.p_flags = 0;
357 note_off = sizeof(Elf32_Ehdr) +
358 (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf32_Phdr);
359 phdr.p_offset = note_off;
360 phdr.p_vaddr = phdr.p_paddr = 0;
361 phdr.p_filesz = phdr.p_memsz = phdr_sz;
362 phdr.p_align = 0;
364 /* Add merged PT_NOTE program header*/
365 tmp = elfptr + sizeof(Elf32_Ehdr);
366 memcpy(tmp, &phdr, sizeof(phdr));
367 tmp += sizeof(phdr);
369 /* Remove unwanted PT_NOTE program headers. */
370 i = (nr_ptnote - 1) * sizeof(Elf32_Phdr);
371 *elfsz = *elfsz - i;
372 memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf32_Ehdr)-sizeof(Elf32_Phdr)));
374 /* Modify e_phnum to reflect merged headers. */
375 ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
377 return 0;
380 /* Add memory chunks represented by program headers to vmcore list. Also update
381 * the new offset fields of exported program headers. */
382 static int __init process_ptload_program_headers_elf64(char *elfptr,
383 size_t elfsz,
384 struct list_head *vc_list)
386 int i;
387 Elf64_Ehdr *ehdr_ptr;
388 Elf64_Phdr *phdr_ptr;
389 loff_t vmcore_off;
390 struct vmcore *new;
392 ehdr_ptr = (Elf64_Ehdr *)elfptr;
393 phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr)); /* PT_NOTE hdr */
395 /* First program header is PT_NOTE header. */
396 vmcore_off = sizeof(Elf64_Ehdr) +
397 (ehdr_ptr->e_phnum) * sizeof(Elf64_Phdr) +
398 phdr_ptr->p_memsz; /* Note sections */
400 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
401 if (phdr_ptr->p_type != PT_LOAD)
402 continue;
404 /* Add this contiguous chunk of memory to vmcore list.*/
405 new = get_new_element();
406 if (!new)
407 return -ENOMEM;
408 new->paddr = phdr_ptr->p_offset;
409 new->size = phdr_ptr->p_memsz;
410 list_add_tail(&new->list, vc_list);
412 /* Update the program header offset. */
413 phdr_ptr->p_offset = vmcore_off;
414 vmcore_off = vmcore_off + phdr_ptr->p_memsz;
416 return 0;
419 static int __init process_ptload_program_headers_elf32(char *elfptr,
420 size_t elfsz,
421 struct list_head *vc_list)
423 int i;
424 Elf32_Ehdr *ehdr_ptr;
425 Elf32_Phdr *phdr_ptr;
426 loff_t vmcore_off;
427 struct vmcore *new;
429 ehdr_ptr = (Elf32_Ehdr *)elfptr;
430 phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr)); /* PT_NOTE hdr */
432 /* First program header is PT_NOTE header. */
433 vmcore_off = sizeof(Elf32_Ehdr) +
434 (ehdr_ptr->e_phnum) * sizeof(Elf32_Phdr) +
435 phdr_ptr->p_memsz; /* Note sections */
437 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
438 if (phdr_ptr->p_type != PT_LOAD)
439 continue;
441 /* Add this contiguous chunk of memory to vmcore list.*/
442 new = get_new_element();
443 if (!new)
444 return -ENOMEM;
445 new->paddr = phdr_ptr->p_offset;
446 new->size = phdr_ptr->p_memsz;
447 list_add_tail(&new->list, vc_list);
449 /* Update the program header offset */
450 phdr_ptr->p_offset = vmcore_off;
451 vmcore_off = vmcore_off + phdr_ptr->p_memsz;
453 return 0;
456 /* Sets offset fields of vmcore elements. */
457 static void __init set_vmcore_list_offsets_elf64(char *elfptr,
458 struct list_head *vc_list)
460 loff_t vmcore_off;
461 Elf64_Ehdr *ehdr_ptr;
462 struct vmcore *m;
464 ehdr_ptr = (Elf64_Ehdr *)elfptr;
466 /* Skip Elf header and program headers. */
467 vmcore_off = sizeof(Elf64_Ehdr) +
468 (ehdr_ptr->e_phnum) * sizeof(Elf64_Phdr);
470 list_for_each_entry(m, vc_list, list) {
471 m->offset = vmcore_off;
472 vmcore_off += m->size;
476 /* Sets offset fields of vmcore elements. */
477 static void __init set_vmcore_list_offsets_elf32(char *elfptr,
478 struct list_head *vc_list)
480 loff_t vmcore_off;
481 Elf32_Ehdr *ehdr_ptr;
482 struct vmcore *m;
484 ehdr_ptr = (Elf32_Ehdr *)elfptr;
486 /* Skip Elf header and program headers. */
487 vmcore_off = sizeof(Elf32_Ehdr) +
488 (ehdr_ptr->e_phnum) * sizeof(Elf32_Phdr);
490 list_for_each_entry(m, vc_list, list) {
491 m->offset = vmcore_off;
492 vmcore_off += m->size;
496 static int __init parse_crash_elf64_headers(void)
498 int rc=0;
499 Elf64_Ehdr ehdr;
500 u64 addr;
502 addr = elfcorehdr_addr;
504 /* Read Elf header */
505 rc = read_from_oldmem((char*)&ehdr, sizeof(Elf64_Ehdr), &addr, 0);
506 if (rc < 0)
507 return rc;
509 /* Do some basic Verification. */
510 if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
511 (ehdr.e_type != ET_CORE) ||
512 !vmcore_elf_check_arch(&ehdr) ||
513 ehdr.e_ident[EI_CLASS] != ELFCLASS64 ||
514 ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
515 ehdr.e_version != EV_CURRENT ||
516 ehdr.e_ehsize != sizeof(Elf64_Ehdr) ||
517 ehdr.e_phentsize != sizeof(Elf64_Phdr) ||
518 ehdr.e_phnum == 0) {
519 printk(KERN_WARNING "Warning: Core image elf header is not"
520 "sane\n");
521 return -EINVAL;
524 /* Read in all elf headers. */
525 elfcorebuf_sz = sizeof(Elf64_Ehdr) + ehdr.e_phnum * sizeof(Elf64_Phdr);
526 elfcorebuf = kmalloc(elfcorebuf_sz, GFP_KERNEL);
527 if (!elfcorebuf)
528 return -ENOMEM;
529 addr = elfcorehdr_addr;
530 rc = read_from_oldmem(elfcorebuf, elfcorebuf_sz, &addr, 0);
531 if (rc < 0) {
532 kfree(elfcorebuf);
533 return rc;
536 /* Merge all PT_NOTE headers into one. */
537 rc = merge_note_headers_elf64(elfcorebuf, &elfcorebuf_sz, &vmcore_list);
538 if (rc) {
539 kfree(elfcorebuf);
540 return rc;
542 rc = process_ptload_program_headers_elf64(elfcorebuf, elfcorebuf_sz,
543 &vmcore_list);
544 if (rc) {
545 kfree(elfcorebuf);
546 return rc;
548 set_vmcore_list_offsets_elf64(elfcorebuf, &vmcore_list);
549 return 0;
552 static int __init parse_crash_elf32_headers(void)
554 int rc=0;
555 Elf32_Ehdr ehdr;
556 u64 addr;
558 addr = elfcorehdr_addr;
560 /* Read Elf header */
561 rc = read_from_oldmem((char*)&ehdr, sizeof(Elf32_Ehdr), &addr, 0);
562 if (rc < 0)
563 return rc;
565 /* Do some basic Verification. */
566 if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
567 (ehdr.e_type != ET_CORE) ||
568 !elf_check_arch(&ehdr) ||
569 ehdr.e_ident[EI_CLASS] != ELFCLASS32||
570 ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
571 ehdr.e_version != EV_CURRENT ||
572 ehdr.e_ehsize != sizeof(Elf32_Ehdr) ||
573 ehdr.e_phentsize != sizeof(Elf32_Phdr) ||
574 ehdr.e_phnum == 0) {
575 printk(KERN_WARNING "Warning: Core image elf header is not"
576 "sane\n");
577 return -EINVAL;
580 /* Read in all elf headers. */
581 elfcorebuf_sz = sizeof(Elf32_Ehdr) + ehdr.e_phnum * sizeof(Elf32_Phdr);
582 elfcorebuf = kmalloc(elfcorebuf_sz, GFP_KERNEL);
583 if (!elfcorebuf)
584 return -ENOMEM;
585 addr = elfcorehdr_addr;
586 rc = read_from_oldmem(elfcorebuf, elfcorebuf_sz, &addr, 0);
587 if (rc < 0) {
588 kfree(elfcorebuf);
589 return rc;
592 /* Merge all PT_NOTE headers into one. */
593 rc = merge_note_headers_elf32(elfcorebuf, &elfcorebuf_sz, &vmcore_list);
594 if (rc) {
595 kfree(elfcorebuf);
596 return rc;
598 rc = process_ptload_program_headers_elf32(elfcorebuf, elfcorebuf_sz,
599 &vmcore_list);
600 if (rc) {
601 kfree(elfcorebuf);
602 return rc;
604 set_vmcore_list_offsets_elf32(elfcorebuf, &vmcore_list);
605 return 0;
608 static int __init parse_crash_elf_headers(void)
610 unsigned char e_ident[EI_NIDENT];
611 u64 addr;
612 int rc=0;
614 addr = elfcorehdr_addr;
615 rc = read_from_oldmem(e_ident, EI_NIDENT, &addr, 0);
616 if (rc < 0)
617 return rc;
618 if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) {
619 printk(KERN_WARNING "Warning: Core image elf header"
620 " not found\n");
621 return -EINVAL;
624 if (e_ident[EI_CLASS] == ELFCLASS64) {
625 rc = parse_crash_elf64_headers();
626 if (rc)
627 return rc;
629 /* Determine vmcore size. */
630 vmcore_size = get_vmcore_size_elf64(elfcorebuf);
631 } else if (e_ident[EI_CLASS] == ELFCLASS32) {
632 rc = parse_crash_elf32_headers();
633 if (rc)
634 return rc;
636 /* Determine vmcore size. */
637 vmcore_size = get_vmcore_size_elf32(elfcorebuf);
638 } else {
639 printk(KERN_WARNING "Warning: Core image elf header is not"
640 " sane\n");
641 return -EINVAL;
643 return 0;
646 /* Init function for vmcore module. */
647 static int __init vmcore_init(void)
649 int rc = 0;
651 /* If elfcorehdr= has been passed in cmdline, then capture the dump.*/
652 if (!(elfcorehdr_addr < ELFCORE_ADDR_MAX))
653 return rc;
654 rc = parse_crash_elf_headers();
655 if (rc) {
656 printk(KERN_WARNING "Kdump: vmcore not initialized\n");
657 return rc;
660 /* Initialize /proc/vmcore size if proc is already up. */
661 if (proc_vmcore)
662 proc_vmcore->size = vmcore_size;
663 return 0;
665 module_init(vmcore_init)