]> git.karo-electronics.de Git - linux-beck.git/blob - fs/proc/vmcore.c
fs/proc: clean up printks
[linux-beck.git] / fs / proc / vmcore.c
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
7  *
8  */
9
10 #include <linux/mm.h>
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>
24 #include <asm/io.h>
25
26 /* List representing chunks of contiguous memory areas and their offsets in
27  * vmcore file.
28  */
29 static LIST_HEAD(vmcore_list);
30
31 /* Stores the pointer to the buffer containing kernel elf core headers. */
32 static char *elfcorebuf;
33 static size_t elfcorebuf_sz;
34
35 /* Total size of vmcore file. */
36 static u64 vmcore_size;
37
38 static struct proc_dir_entry *proc_vmcore = NULL;
39
40 /*
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.
43  */
44 static int (*oldmem_pfn_is_ram)(unsigned long pfn);
45
46 int register_oldmem_pfn_is_ram(int (*fn)(unsigned long pfn))
47 {
48         if (oldmem_pfn_is_ram)
49                 return -EBUSY;
50         oldmem_pfn_is_ram = fn;
51         return 0;
52 }
53 EXPORT_SYMBOL_GPL(register_oldmem_pfn_is_ram);
54
55 void unregister_oldmem_pfn_is_ram(void)
56 {
57         oldmem_pfn_is_ram = NULL;
58         wmb();
59 }
60 EXPORT_SYMBOL_GPL(unregister_oldmem_pfn_is_ram);
61
62 static int pfn_is_ram(unsigned long pfn)
63 {
64         int (*fn)(unsigned long pfn);
65         /* pfn is ram unless fn() checks pagetype */
66         int ret = 1;
67
68         /*
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.
72          */
73         fn = oldmem_pfn_is_ram;
74         if (fn)
75                 ret = fn(pfn);
76
77         return ret;
78 }
79
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)
83 {
84         unsigned long pfn, offset;
85         size_t nr_bytes;
86         ssize_t read = 0, tmp;
87
88         if (!count)
89                 return 0;
90
91         offset = (unsigned long)(*ppos % PAGE_SIZE);
92         pfn = (unsigned long)(*ppos / PAGE_SIZE);
93
94         do {
95                 if (count > (PAGE_SIZE - offset))
96                         nr_bytes = PAGE_SIZE - offset;
97                 else
98                         nr_bytes = count;
99
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);
103                 else {
104                         tmp = copy_oldmem_page(pfn, buf, nr_bytes,
105                                                 offset, userbuf);
106                         if (tmp < 0)
107                                 return tmp;
108                 }
109                 *ppos += nr_bytes;
110                 count -= nr_bytes;
111                 buf += nr_bytes;
112                 read += nr_bytes;
113                 ++pfn;
114                 offset = 0;
115         } while (count);
116
117         return read;
118 }
119
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)
123 {
124         struct vmcore *m;
125         u64 paddr;
126
127         list_for_each_entry(m, vc_list, list) {
128                 u64 start, end;
129                 start = m->offset;
130                 end = m->offset + m->size - 1;
131                 if (offset >= start && offset <= end) {
132                         paddr = m->paddr + offset - start;
133                         *m_ptr = m;
134                         return paddr;
135                 }
136         }
137         *m_ptr = NULL;
138         return 0;
139 }
140
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.
143  */
144 static ssize_t read_vmcore(struct file *file, char __user *buffer,
145                                 size_t buflen, loff_t *fpos)
146 {
147         ssize_t acc = 0, tmp;
148         size_t tsz;
149         u64 start, nr_bytes;
150         struct vmcore *curr_m = NULL;
151
152         if (buflen == 0 || *fpos >= vmcore_size)
153                 return 0;
154
155         /* trim buflen to not go beyond EOF */
156         if (buflen > vmcore_size - *fpos)
157                 buflen = vmcore_size - *fpos;
158
159         /* Read ELF core header */
160         if (*fpos < elfcorebuf_sz) {
161                 tsz = elfcorebuf_sz - *fpos;
162                 if (buflen < tsz)
163                         tsz = buflen;
164                 if (copy_to_user(buffer, elfcorebuf + *fpos, tsz))
165                         return -EFAULT;
166                 buflen -= tsz;
167                 *fpos += tsz;
168                 buffer += tsz;
169                 acc += tsz;
170
171                 /* leave now if filled buffer already */
172                 if (buflen == 0)
173                         return acc;
174         }
175
176         start = map_offset_to_paddr(*fpos, &vmcore_list, &curr_m);
177         if (!curr_m)
178                 return -EINVAL;
179         if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
180                 tsz = buflen;
181
182         /* Calculate left bytes in current memory segment. */
183         nr_bytes = (curr_m->size - (start - curr_m->paddr));
184         if (tsz > nr_bytes)
185                 tsz = nr_bytes;
186
187         while (buflen) {
188                 tmp = read_from_oldmem(buffer, tsz, &start, 1);
189                 if (tmp < 0)
190                         return tmp;
191                 buflen -= tsz;
192                 *fpos += tsz;
193                 buffer += tsz;
194                 acc += tsz;
195                 if (start >= (curr_m->paddr + curr_m->size)) {
196                         if (curr_m->list.next == &vmcore_list)
197                                 return acc;     /*EOF*/
198                         curr_m = list_entry(curr_m->list.next,
199                                                 struct vmcore, list);
200                         start = curr_m->paddr;
201                 }
202                 if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
203                         tsz = buflen;
204                 /* Calculate left bytes in current memory segment. */
205                 nr_bytes = (curr_m->size - (start - curr_m->paddr));
206                 if (tsz > nr_bytes)
207                         tsz = nr_bytes;
208         }
209         return acc;
210 }
211
212 static const struct file_operations proc_vmcore_operations = {
213         .read           = read_vmcore,
214         .llseek         = default_llseek,
215 };
216
217 static struct vmcore* __init get_new_element(void)
218 {
219         return kzalloc(sizeof(struct vmcore), GFP_KERNEL);
220 }
221
222 static u64 __init get_vmcore_size_elf64(char *elfptr)
223 {
224         int i;
225         u64 size;
226         Elf64_Ehdr *ehdr_ptr;
227         Elf64_Phdr *phdr_ptr;
228
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;
234                 phdr_ptr++;
235         }
236         return size;
237 }
238
239 static u64 __init get_vmcore_size_elf32(char *elfptr)
240 {
241         int i;
242         u64 size;
243         Elf32_Ehdr *ehdr_ptr;
244         Elf32_Phdr *phdr_ptr;
245
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;
251                 phdr_ptr++;
252         }
253         return size;
254 }
255
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)
259 {
260         int i, nr_ptnote=0, rc=0;
261         char *tmp;
262         Elf64_Ehdr *ehdr_ptr;
263         Elf64_Phdr phdr, *phdr_ptr;
264         Elf64_Nhdr *nhdr_ptr;
265         u64 phdr_sz = 0, note_off;
266
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++) {
270                 int j;
271                 void *notes_section;
272                 struct vmcore *new;
273                 u64 offset, max_sz, sz, real_sz = 0;
274                 if (phdr_ptr->p_type != PT_NOTE)
275                         continue;
276                 nr_ptnote++;
277                 max_sz = phdr_ptr->p_memsz;
278                 offset = phdr_ptr->p_offset;
279                 notes_section = kmalloc(max_sz, GFP_KERNEL);
280                 if (!notes_section)
281                         return -ENOMEM;
282                 rc = read_from_oldmem(notes_section, max_sz, &offset, 0);
283                 if (rc < 0) {
284                         kfree(notes_section);
285                         return rc;
286                 }
287                 nhdr_ptr = notes_section;
288                 for (j = 0; j < max_sz; j += sz) {
289                         if (nhdr_ptr->n_namesz == 0)
290                                 break;
291                         sz = sizeof(Elf64_Nhdr) +
292                                 ((nhdr_ptr->n_namesz + 3) & ~3) +
293                                 ((nhdr_ptr->n_descsz + 3) & ~3);
294                         real_sz += sz;
295                         nhdr_ptr = (Elf64_Nhdr*)((char*)nhdr_ptr + sz);
296                 }
297
298                 /* Add this contiguous chunk of notes section to vmcore list.*/
299                 new = get_new_element();
300                 if (!new) {
301                         kfree(notes_section);
302                         return -ENOMEM;
303                 }
304                 new->paddr = phdr_ptr->p_offset;
305                 new->size = real_sz;
306                 list_add_tail(&new->list, vc_list);
307                 phdr_sz += real_sz;
308                 kfree(notes_section);
309         }
310
311         /* Prepare merged PT_NOTE program header. */
312         phdr.p_type    = PT_NOTE;
313         phdr.p_flags   = 0;
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;
319         phdr.p_align   = 0;
320
321         /* Add merged PT_NOTE program header*/
322         tmp = elfptr + sizeof(Elf64_Ehdr);
323         memcpy(tmp, &phdr, sizeof(phdr));
324         tmp += sizeof(phdr);
325
326         /* Remove unwanted PT_NOTE program headers. */
327         i = (nr_ptnote - 1) * sizeof(Elf64_Phdr);
328         *elfsz = *elfsz - i;
329         memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf64_Ehdr)-sizeof(Elf64_Phdr)));
330
331         /* Modify e_phnum to reflect merged headers. */
332         ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
333
334         return 0;
335 }
336
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)
340 {
341         int i, nr_ptnote=0, rc=0;
342         char *tmp;
343         Elf32_Ehdr *ehdr_ptr;
344         Elf32_Phdr phdr, *phdr_ptr;
345         Elf32_Nhdr *nhdr_ptr;
346         u64 phdr_sz = 0, note_off;
347
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++) {
351                 int j;
352                 void *notes_section;
353                 struct vmcore *new;
354                 u64 offset, max_sz, sz, real_sz = 0;
355                 if (phdr_ptr->p_type != PT_NOTE)
356                         continue;
357                 nr_ptnote++;
358                 max_sz = phdr_ptr->p_memsz;
359                 offset = phdr_ptr->p_offset;
360                 notes_section = kmalloc(max_sz, GFP_KERNEL);
361                 if (!notes_section)
362                         return -ENOMEM;
363                 rc = read_from_oldmem(notes_section, max_sz, &offset, 0);
364                 if (rc < 0) {
365                         kfree(notes_section);
366                         return rc;
367                 }
368                 nhdr_ptr = notes_section;
369                 for (j = 0; j < max_sz; j += sz) {
370                         if (nhdr_ptr->n_namesz == 0)
371                                 break;
372                         sz = sizeof(Elf32_Nhdr) +
373                                 ((nhdr_ptr->n_namesz + 3) & ~3) +
374                                 ((nhdr_ptr->n_descsz + 3) & ~3);
375                         real_sz += sz;
376                         nhdr_ptr = (Elf32_Nhdr*)((char*)nhdr_ptr + sz);
377                 }
378
379                 /* Add this contiguous chunk of notes section to vmcore list.*/
380                 new = get_new_element();
381                 if (!new) {
382                         kfree(notes_section);
383                         return -ENOMEM;
384                 }
385                 new->paddr = phdr_ptr->p_offset;
386                 new->size = real_sz;
387                 list_add_tail(&new->list, vc_list);
388                 phdr_sz += real_sz;
389                 kfree(notes_section);
390         }
391
392         /* Prepare merged PT_NOTE program header. */
393         phdr.p_type    = PT_NOTE;
394         phdr.p_flags   = 0;
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;
400         phdr.p_align   = 0;
401
402         /* Add merged PT_NOTE program header*/
403         tmp = elfptr + sizeof(Elf32_Ehdr);
404         memcpy(tmp, &phdr, sizeof(phdr));
405         tmp += sizeof(phdr);
406
407         /* Remove unwanted PT_NOTE program headers. */
408         i = (nr_ptnote - 1) * sizeof(Elf32_Phdr);
409         *elfsz = *elfsz - i;
410         memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf32_Ehdr)-sizeof(Elf32_Phdr)));
411
412         /* Modify e_phnum to reflect merged headers. */
413         ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
414
415         return 0;
416 }
417
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,
421                                                 size_t elfsz,
422                                                 struct list_head *vc_list)
423 {
424         int i;
425         Elf64_Ehdr *ehdr_ptr;
426         Elf64_Phdr *phdr_ptr;
427         loff_t vmcore_off;
428         struct vmcore *new;
429
430         ehdr_ptr = (Elf64_Ehdr *)elfptr;
431         phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr)); /* PT_NOTE hdr */
432
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 */
437
438         for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
439                 if (phdr_ptr->p_type != PT_LOAD)
440                         continue;
441
442                 /* Add this contiguous chunk of memory to vmcore list.*/
443                 new = get_new_element();
444                 if (!new)
445                         return -ENOMEM;
446                 new->paddr = phdr_ptr->p_offset;
447                 new->size = phdr_ptr->p_memsz;
448                 list_add_tail(&new->list, vc_list);
449
450                 /* Update the program header offset. */
451                 phdr_ptr->p_offset = vmcore_off;
452                 vmcore_off = vmcore_off + phdr_ptr->p_memsz;
453         }
454         return 0;
455 }
456
457 static int __init process_ptload_program_headers_elf32(char *elfptr,
458                                                 size_t elfsz,
459                                                 struct list_head *vc_list)
460 {
461         int i;
462         Elf32_Ehdr *ehdr_ptr;
463         Elf32_Phdr *phdr_ptr;
464         loff_t vmcore_off;
465         struct vmcore *new;
466
467         ehdr_ptr = (Elf32_Ehdr *)elfptr;
468         phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr)); /* PT_NOTE hdr */
469
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 */
474
475         for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
476                 if (phdr_ptr->p_type != PT_LOAD)
477                         continue;
478
479                 /* Add this contiguous chunk of memory to vmcore list.*/
480                 new = get_new_element();
481                 if (!new)
482                         return -ENOMEM;
483                 new->paddr = phdr_ptr->p_offset;
484                 new->size = phdr_ptr->p_memsz;
485                 list_add_tail(&new->list, vc_list);
486
487                 /* Update the program header offset */
488                 phdr_ptr->p_offset = vmcore_off;
489                 vmcore_off = vmcore_off + phdr_ptr->p_memsz;
490         }
491         return 0;
492 }
493
494 /* Sets offset fields of vmcore elements. */
495 static void __init set_vmcore_list_offsets_elf64(char *elfptr,
496                                                 struct list_head *vc_list)
497 {
498         loff_t vmcore_off;
499         Elf64_Ehdr *ehdr_ptr;
500         struct vmcore *m;
501
502         ehdr_ptr = (Elf64_Ehdr *)elfptr;
503
504         /* Skip Elf header and program headers. */
505         vmcore_off = sizeof(Elf64_Ehdr) +
506                         (ehdr_ptr->e_phnum) * sizeof(Elf64_Phdr);
507
508         list_for_each_entry(m, vc_list, list) {
509                 m->offset = vmcore_off;
510                 vmcore_off += m->size;
511         }
512 }
513
514 /* Sets offset fields of vmcore elements. */
515 static void __init set_vmcore_list_offsets_elf32(char *elfptr,
516                                                 struct list_head *vc_list)
517 {
518         loff_t vmcore_off;
519         Elf32_Ehdr *ehdr_ptr;
520         struct vmcore *m;
521
522         ehdr_ptr = (Elf32_Ehdr *)elfptr;
523
524         /* Skip Elf header and program headers. */
525         vmcore_off = sizeof(Elf32_Ehdr) +
526                         (ehdr_ptr->e_phnum) * sizeof(Elf32_Phdr);
527
528         list_for_each_entry(m, vc_list, list) {
529                 m->offset = vmcore_off;
530                 vmcore_off += m->size;
531         }
532 }
533
534 static int __init parse_crash_elf64_headers(void)
535 {
536         int rc=0;
537         Elf64_Ehdr ehdr;
538         u64 addr;
539
540         addr = elfcorehdr_addr;
541
542         /* Read Elf header */
543         rc = read_from_oldmem((char*)&ehdr, sizeof(Elf64_Ehdr), &addr, 0);
544         if (rc < 0)
545                 return rc;
546
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) ||
556                 ehdr.e_phnum == 0) {
557                 pr_warn("Warning: Core image elf header is not sane\n");
558                 return -EINVAL;
559         }
560
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);
564         if (!elfcorebuf)
565                 return -ENOMEM;
566         addr = elfcorehdr_addr;
567         rc = read_from_oldmem(elfcorebuf, elfcorebuf_sz, &addr, 0);
568         if (rc < 0) {
569                 kfree(elfcorebuf);
570                 return rc;
571         }
572
573         /* Merge all PT_NOTE headers into one. */
574         rc = merge_note_headers_elf64(elfcorebuf, &elfcorebuf_sz, &vmcore_list);
575         if (rc) {
576                 kfree(elfcorebuf);
577                 return rc;
578         }
579         rc = process_ptload_program_headers_elf64(elfcorebuf, elfcorebuf_sz,
580                                                         &vmcore_list);
581         if (rc) {
582                 kfree(elfcorebuf);
583                 return rc;
584         }
585         set_vmcore_list_offsets_elf64(elfcorebuf, &vmcore_list);
586         return 0;
587 }
588
589 static int __init parse_crash_elf32_headers(void)
590 {
591         int rc=0;
592         Elf32_Ehdr ehdr;
593         u64 addr;
594
595         addr = elfcorehdr_addr;
596
597         /* Read Elf header */
598         rc = read_from_oldmem((char*)&ehdr, sizeof(Elf32_Ehdr), &addr, 0);
599         if (rc < 0)
600                 return rc;
601
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) ||
611                 ehdr.e_phnum == 0) {
612                 pr_warn("Warning: Core image elf header is not sane\n");
613                 return -EINVAL;
614         }
615
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);
619         if (!elfcorebuf)
620                 return -ENOMEM;
621         addr = elfcorehdr_addr;
622         rc = read_from_oldmem(elfcorebuf, elfcorebuf_sz, &addr, 0);
623         if (rc < 0) {
624                 kfree(elfcorebuf);
625                 return rc;
626         }
627
628         /* Merge all PT_NOTE headers into one. */
629         rc = merge_note_headers_elf32(elfcorebuf, &elfcorebuf_sz, &vmcore_list);
630         if (rc) {
631                 kfree(elfcorebuf);
632                 return rc;
633         }
634         rc = process_ptload_program_headers_elf32(elfcorebuf, elfcorebuf_sz,
635                                                                 &vmcore_list);
636         if (rc) {
637                 kfree(elfcorebuf);
638                 return rc;
639         }
640         set_vmcore_list_offsets_elf32(elfcorebuf, &vmcore_list);
641         return 0;
642 }
643
644 static int __init parse_crash_elf_headers(void)
645 {
646         unsigned char e_ident[EI_NIDENT];
647         u64 addr;
648         int rc=0;
649
650         addr = elfcorehdr_addr;
651         rc = read_from_oldmem(e_ident, EI_NIDENT, &addr, 0);
652         if (rc < 0)
653                 return rc;
654         if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) {
655                 pr_warn("Warning: Core image elf header not found\n");
656                 return -EINVAL;
657         }
658
659         if (e_ident[EI_CLASS] == ELFCLASS64) {
660                 rc = parse_crash_elf64_headers();
661                 if (rc)
662                         return rc;
663
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();
668                 if (rc)
669                         return rc;
670
671                 /* Determine vmcore size. */
672                 vmcore_size = get_vmcore_size_elf32(elfcorebuf);
673         } else {
674                 pr_warn("Warning: Core image elf header is not sane\n");
675                 return -EINVAL;
676         }
677         return 0;
678 }
679
680 /* Init function for vmcore module. */
681 static int __init vmcore_init(void)
682 {
683         int rc = 0;
684
685         /* If elfcorehdr= has been passed in cmdline, then capture the dump.*/
686         if (!(is_vmcore_usable()))
687                 return rc;
688         rc = parse_crash_elf_headers();
689         if (rc) {
690                 pr_warn("Kdump: vmcore not initialized\n");
691                 return rc;
692         }
693
694         proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations);
695         if (proc_vmcore)
696                 proc_vmcore->size = vmcore_size;
697         return 0;
698 }
699 module_init(vmcore_init)
700
701 /* Cleanup function for vmcore module. */
702 void vmcore_cleanup(void)
703 {
704         struct list_head *pos, *next;
705
706         if (proc_vmcore) {
707                 remove_proc_entry(proc_vmcore->name, proc_vmcore->parent);
708                 proc_vmcore = NULL;
709         }
710
711         /* clear the vmcore list. */
712         list_for_each_safe(pos, next, &vmcore_list) {
713                 struct vmcore *m;
714
715                 m = list_entry(pos, struct vmcore, list);
716                 list_del(&m->list);
717                 kfree(m);
718         }
719         kfree(elfcorebuf);
720         elfcorebuf = NULL;
721 }
722 EXPORT_SYMBOL_GPL(vmcore_cleanup);