Linux-libre 4.19.116-gnu
[librecmc/linux-libre.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/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 <linux/mutex.h>
24 #include <linux/vmalloc.h>
25 #include <linux/pagemap.h>
26 #include <linux/uaccess.h>
27 #include <asm/io.h>
28 #include "internal.h"
29
30 /* List representing chunks of contiguous memory areas and their offsets in
31  * vmcore file.
32  */
33 static LIST_HEAD(vmcore_list);
34
35 /* Stores the pointer to the buffer containing kernel elf core headers. */
36 static char *elfcorebuf;
37 static size_t elfcorebuf_sz;
38 static size_t elfcorebuf_sz_orig;
39
40 static char *elfnotes_buf;
41 static size_t elfnotes_sz;
42 /* Size of all notes minus the device dump notes */
43 static size_t elfnotes_orig_sz;
44
45 /* Total size of vmcore file. */
46 static u64 vmcore_size;
47
48 static struct proc_dir_entry *proc_vmcore;
49
50 #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
51 /* Device Dump list and mutex to synchronize access to list */
52 static LIST_HEAD(vmcoredd_list);
53 static DEFINE_MUTEX(vmcoredd_mutex);
54 #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
55
56 /* Device Dump Size */
57 static size_t vmcoredd_orig_sz;
58
59 /*
60  * Returns > 0 for RAM pages, 0 for non-RAM pages, < 0 on error
61  * The called function has to take care of module refcounting.
62  */
63 static int (*oldmem_pfn_is_ram)(unsigned long pfn);
64
65 int register_oldmem_pfn_is_ram(int (*fn)(unsigned long pfn))
66 {
67         if (oldmem_pfn_is_ram)
68                 return -EBUSY;
69         oldmem_pfn_is_ram = fn;
70         return 0;
71 }
72 EXPORT_SYMBOL_GPL(register_oldmem_pfn_is_ram);
73
74 void unregister_oldmem_pfn_is_ram(void)
75 {
76         oldmem_pfn_is_ram = NULL;
77         wmb();
78 }
79 EXPORT_SYMBOL_GPL(unregister_oldmem_pfn_is_ram);
80
81 static int pfn_is_ram(unsigned long pfn)
82 {
83         int (*fn)(unsigned long pfn);
84         /* pfn is ram unless fn() checks pagetype */
85         int ret = 1;
86
87         /*
88          * Ask hypervisor if the pfn is really ram.
89          * A ballooned page contains no data and reading from such a page
90          * will cause high load in the hypervisor.
91          */
92         fn = oldmem_pfn_is_ram;
93         if (fn)
94                 ret = fn(pfn);
95
96         return ret;
97 }
98
99 /* Reads a page from the oldmem device from given offset. */
100 static ssize_t read_from_oldmem(char *buf, size_t count,
101                                 u64 *ppos, int userbuf)
102 {
103         unsigned long pfn, offset;
104         size_t nr_bytes;
105         ssize_t read = 0, tmp;
106
107         if (!count)
108                 return 0;
109
110         offset = (unsigned long)(*ppos % PAGE_SIZE);
111         pfn = (unsigned long)(*ppos / PAGE_SIZE);
112
113         do {
114                 if (count > (PAGE_SIZE - offset))
115                         nr_bytes = PAGE_SIZE - offset;
116                 else
117                         nr_bytes = count;
118
119                 /* If pfn is not ram, return zeros for sparse dump files */
120                 if (pfn_is_ram(pfn) == 0)
121                         memset(buf, 0, nr_bytes);
122                 else {
123                         tmp = copy_oldmem_page(pfn, buf, nr_bytes,
124                                                 offset, userbuf);
125                         if (tmp < 0)
126                                 return tmp;
127                 }
128                 *ppos += nr_bytes;
129                 count -= nr_bytes;
130                 buf += nr_bytes;
131                 read += nr_bytes;
132                 ++pfn;
133                 offset = 0;
134         } while (count);
135
136         return read;
137 }
138
139 /*
140  * Architectures may override this function to allocate ELF header in 2nd kernel
141  */
142 int __weak elfcorehdr_alloc(unsigned long long *addr, unsigned long long *size)
143 {
144         return 0;
145 }
146
147 /*
148  * Architectures may override this function to free header
149  */
150 void __weak elfcorehdr_free(unsigned long long addr)
151 {}
152
153 /*
154  * Architectures may override this function to read from ELF header
155  */
156 ssize_t __weak elfcorehdr_read(char *buf, size_t count, u64 *ppos)
157 {
158         return read_from_oldmem(buf, count, ppos, 0);
159 }
160
161 /*
162  * Architectures may override this function to read from notes sections
163  */
164 ssize_t __weak elfcorehdr_read_notes(char *buf, size_t count, u64 *ppos)
165 {
166         return read_from_oldmem(buf, count, ppos, 0);
167 }
168
169 /*
170  * Architectures may override this function to map oldmem
171  */
172 int __weak remap_oldmem_pfn_range(struct vm_area_struct *vma,
173                                   unsigned long from, unsigned long pfn,
174                                   unsigned long size, pgprot_t prot)
175 {
176         return remap_pfn_range(vma, from, pfn, size, prot);
177 }
178
179 /*
180  * Architectures which support memory encryption override this.
181  */
182 ssize_t __weak
183 copy_oldmem_page_encrypted(unsigned long pfn, char *buf, size_t csize,
184                            unsigned long offset, int userbuf)
185 {
186         return copy_oldmem_page(pfn, buf, csize, offset, userbuf);
187 }
188
189 /*
190  * Copy to either kernel or user space
191  */
192 static int copy_to(void *target, void *src, size_t size, int userbuf)
193 {
194         if (userbuf) {
195                 if (copy_to_user((char __user *) target, src, size))
196                         return -EFAULT;
197         } else {
198                 memcpy(target, src, size);
199         }
200         return 0;
201 }
202
203 #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
204 static int vmcoredd_copy_dumps(void *dst, u64 start, size_t size, int userbuf)
205 {
206         struct vmcoredd_node *dump;
207         u64 offset = 0;
208         int ret = 0;
209         size_t tsz;
210         char *buf;
211
212         mutex_lock(&vmcoredd_mutex);
213         list_for_each_entry(dump, &vmcoredd_list, list) {
214                 if (start < offset + dump->size) {
215                         tsz = min(offset + (u64)dump->size - start, (u64)size);
216                         buf = dump->buf + start - offset;
217                         if (copy_to(dst, buf, tsz, userbuf)) {
218                                 ret = -EFAULT;
219                                 goto out_unlock;
220                         }
221
222                         size -= tsz;
223                         start += tsz;
224                         dst += tsz;
225
226                         /* Leave now if buffer filled already */
227                         if (!size)
228                                 goto out_unlock;
229                 }
230                 offset += dump->size;
231         }
232
233 out_unlock:
234         mutex_unlock(&vmcoredd_mutex);
235         return ret;
236 }
237
238 #ifdef CONFIG_MMU
239 static int vmcoredd_mmap_dumps(struct vm_area_struct *vma, unsigned long dst,
240                                u64 start, size_t size)
241 {
242         struct vmcoredd_node *dump;
243         u64 offset = 0;
244         int ret = 0;
245         size_t tsz;
246         char *buf;
247
248         mutex_lock(&vmcoredd_mutex);
249         list_for_each_entry(dump, &vmcoredd_list, list) {
250                 if (start < offset + dump->size) {
251                         tsz = min(offset + (u64)dump->size - start, (u64)size);
252                         buf = dump->buf + start - offset;
253                         if (remap_vmalloc_range_partial(vma, dst, buf, tsz)) {
254                                 ret = -EFAULT;
255                                 goto out_unlock;
256                         }
257
258                         size -= tsz;
259                         start += tsz;
260                         dst += tsz;
261
262                         /* Leave now if buffer filled already */
263                         if (!size)
264                                 goto out_unlock;
265                 }
266                 offset += dump->size;
267         }
268
269 out_unlock:
270         mutex_unlock(&vmcoredd_mutex);
271         return ret;
272 }
273 #endif /* CONFIG_MMU */
274 #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
275
276 /* Read from the ELF header and then the crash dump. On error, negative value is
277  * returned otherwise number of bytes read are returned.
278  */
279 static ssize_t __read_vmcore(char *buffer, size_t buflen, loff_t *fpos,
280                              int userbuf)
281 {
282         ssize_t acc = 0, tmp;
283         size_t tsz;
284         u64 start;
285         struct vmcore *m = NULL;
286
287         if (buflen == 0 || *fpos >= vmcore_size)
288                 return 0;
289
290         /* trim buflen to not go beyond EOF */
291         if (buflen > vmcore_size - *fpos)
292                 buflen = vmcore_size - *fpos;
293
294         /* Read ELF core header */
295         if (*fpos < elfcorebuf_sz) {
296                 tsz = min(elfcorebuf_sz - (size_t)*fpos, buflen);
297                 if (copy_to(buffer, elfcorebuf + *fpos, tsz, userbuf))
298                         return -EFAULT;
299                 buflen -= tsz;
300                 *fpos += tsz;
301                 buffer += tsz;
302                 acc += tsz;
303
304                 /* leave now if filled buffer already */
305                 if (buflen == 0)
306                         return acc;
307         }
308
309         /* Read Elf note segment */
310         if (*fpos < elfcorebuf_sz + elfnotes_sz) {
311                 void *kaddr;
312
313                 /* We add device dumps before other elf notes because the
314                  * other elf notes may not fill the elf notes buffer
315                  * completely and we will end up with zero-filled data
316                  * between the elf notes and the device dumps. Tools will
317                  * then try to decode this zero-filled data as valid notes
318                  * and we don't want that. Hence, adding device dumps before
319                  * the other elf notes ensure that zero-filled data can be
320                  * avoided.
321                  */
322 #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
323                 /* Read device dumps */
324                 if (*fpos < elfcorebuf_sz + vmcoredd_orig_sz) {
325                         tsz = min(elfcorebuf_sz + vmcoredd_orig_sz -
326                                   (size_t)*fpos, buflen);
327                         start = *fpos - elfcorebuf_sz;
328                         if (vmcoredd_copy_dumps(buffer, start, tsz, userbuf))
329                                 return -EFAULT;
330
331                         buflen -= tsz;
332                         *fpos += tsz;
333                         buffer += tsz;
334                         acc += tsz;
335
336                         /* leave now if filled buffer already */
337                         if (!buflen)
338                                 return acc;
339                 }
340 #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
341
342                 /* Read remaining elf notes */
343                 tsz = min(elfcorebuf_sz + elfnotes_sz - (size_t)*fpos, buflen);
344                 kaddr = elfnotes_buf + *fpos - elfcorebuf_sz - vmcoredd_orig_sz;
345                 if (copy_to(buffer, kaddr, tsz, userbuf))
346                         return -EFAULT;
347
348                 buflen -= tsz;
349                 *fpos += tsz;
350                 buffer += tsz;
351                 acc += tsz;
352
353                 /* leave now if filled buffer already */
354                 if (buflen == 0)
355                         return acc;
356         }
357
358         list_for_each_entry(m, &vmcore_list, list) {
359                 if (*fpos < m->offset + m->size) {
360                         tsz = (size_t)min_t(unsigned long long,
361                                             m->offset + m->size - *fpos,
362                                             buflen);
363                         start = m->paddr + *fpos - m->offset;
364                         tmp = read_from_oldmem(buffer, tsz, &start, userbuf);
365                         if (tmp < 0)
366                                 return tmp;
367                         buflen -= tsz;
368                         *fpos += tsz;
369                         buffer += tsz;
370                         acc += tsz;
371
372                         /* leave now if filled buffer already */
373                         if (buflen == 0)
374                                 return acc;
375                 }
376         }
377
378         return acc;
379 }
380
381 static ssize_t read_vmcore(struct file *file, char __user *buffer,
382                            size_t buflen, loff_t *fpos)
383 {
384         return __read_vmcore((__force char *) buffer, buflen, fpos, 1);
385 }
386
387 /*
388  * The vmcore fault handler uses the page cache and fills data using the
389  * standard __vmcore_read() function.
390  *
391  * On s390 the fault handler is used for memory regions that can't be mapped
392  * directly with remap_pfn_range().
393  */
394 static vm_fault_t mmap_vmcore_fault(struct vm_fault *vmf)
395 {
396 #ifdef CONFIG_S390
397         struct address_space *mapping = vmf->vma->vm_file->f_mapping;
398         pgoff_t index = vmf->pgoff;
399         struct page *page;
400         loff_t offset;
401         char *buf;
402         int rc;
403
404         page = find_or_create_page(mapping, index, GFP_KERNEL);
405         if (!page)
406                 return VM_FAULT_OOM;
407         if (!PageUptodate(page)) {
408                 offset = (loff_t) index << PAGE_SHIFT;
409                 buf = __va((page_to_pfn(page) << PAGE_SHIFT));
410                 rc = __read_vmcore(buf, PAGE_SIZE, &offset, 0);
411                 if (rc < 0) {
412                         unlock_page(page);
413                         put_page(page);
414                         return (rc == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
415                 }
416                 SetPageUptodate(page);
417         }
418         unlock_page(page);
419         vmf->page = page;
420         return 0;
421 #else
422         return VM_FAULT_SIGBUS;
423 #endif
424 }
425
426 static const struct vm_operations_struct vmcore_mmap_ops = {
427         .fault = mmap_vmcore_fault,
428 };
429
430 /**
431  * vmcore_alloc_buf - allocate buffer in vmalloc memory
432  * @sizez: size of buffer
433  *
434  * If CONFIG_MMU is defined, use vmalloc_user() to allow users to mmap
435  * the buffer to user-space by means of remap_vmalloc_range().
436  *
437  * If CONFIG_MMU is not defined, use vzalloc() since mmap_vmcore() is
438  * disabled and there's no need to allow users to mmap the buffer.
439  */
440 static inline char *vmcore_alloc_buf(size_t size)
441 {
442 #ifdef CONFIG_MMU
443         return vmalloc_user(size);
444 #else
445         return vzalloc(size);
446 #endif
447 }
448
449 /*
450  * Disable mmap_vmcore() if CONFIG_MMU is not defined. MMU is
451  * essential for mmap_vmcore() in order to map physically
452  * non-contiguous objects (ELF header, ELF note segment and memory
453  * regions in the 1st kernel pointed to by PT_LOAD entries) into
454  * virtually contiguous user-space in ELF layout.
455  */
456 #ifdef CONFIG_MMU
457 /*
458  * remap_oldmem_pfn_checked - do remap_oldmem_pfn_range replacing all pages
459  * reported as not being ram with the zero page.
460  *
461  * @vma: vm_area_struct describing requested mapping
462  * @from: start remapping from
463  * @pfn: page frame number to start remapping to
464  * @size: remapping size
465  * @prot: protection bits
466  *
467  * Returns zero on success, -EAGAIN on failure.
468  */
469 static int remap_oldmem_pfn_checked(struct vm_area_struct *vma,
470                                     unsigned long from, unsigned long pfn,
471                                     unsigned long size, pgprot_t prot)
472 {
473         unsigned long map_size;
474         unsigned long pos_start, pos_end, pos;
475         unsigned long zeropage_pfn = my_zero_pfn(0);
476         size_t len = 0;
477
478         pos_start = pfn;
479         pos_end = pfn + (size >> PAGE_SHIFT);
480
481         for (pos = pos_start; pos < pos_end; ++pos) {
482                 if (!pfn_is_ram(pos)) {
483                         /*
484                          * We hit a page which is not ram. Remap the continuous
485                          * region between pos_start and pos-1 and replace
486                          * the non-ram page at pos with the zero page.
487                          */
488                         if (pos > pos_start) {
489                                 /* Remap continuous region */
490                                 map_size = (pos - pos_start) << PAGE_SHIFT;
491                                 if (remap_oldmem_pfn_range(vma, from + len,
492                                                            pos_start, map_size,
493                                                            prot))
494                                         goto fail;
495                                 len += map_size;
496                         }
497                         /* Remap the zero page */
498                         if (remap_oldmem_pfn_range(vma, from + len,
499                                                    zeropage_pfn,
500                                                    PAGE_SIZE, prot))
501                                 goto fail;
502                         len += PAGE_SIZE;
503                         pos_start = pos + 1;
504                 }
505         }
506         if (pos > pos_start) {
507                 /* Remap the rest */
508                 map_size = (pos - pos_start) << PAGE_SHIFT;
509                 if (remap_oldmem_pfn_range(vma, from + len, pos_start,
510                                            map_size, prot))
511                         goto fail;
512         }
513         return 0;
514 fail:
515         do_munmap(vma->vm_mm, from, len, NULL);
516         return -EAGAIN;
517 }
518
519 static int vmcore_remap_oldmem_pfn(struct vm_area_struct *vma,
520                             unsigned long from, unsigned long pfn,
521                             unsigned long size, pgprot_t prot)
522 {
523         /*
524          * Check if oldmem_pfn_is_ram was registered to avoid
525          * looping over all pages without a reason.
526          */
527         if (oldmem_pfn_is_ram)
528                 return remap_oldmem_pfn_checked(vma, from, pfn, size, prot);
529         else
530                 return remap_oldmem_pfn_range(vma, from, pfn, size, prot);
531 }
532
533 static int mmap_vmcore(struct file *file, struct vm_area_struct *vma)
534 {
535         size_t size = vma->vm_end - vma->vm_start;
536         u64 start, end, len, tsz;
537         struct vmcore *m;
538
539         start = (u64)vma->vm_pgoff << PAGE_SHIFT;
540         end = start + size;
541
542         if (size > vmcore_size || end > vmcore_size)
543                 return -EINVAL;
544
545         if (vma->vm_flags & (VM_WRITE | VM_EXEC))
546                 return -EPERM;
547
548         vma->vm_flags &= ~(VM_MAYWRITE | VM_MAYEXEC);
549         vma->vm_flags |= VM_MIXEDMAP;
550         vma->vm_ops = &vmcore_mmap_ops;
551
552         len = 0;
553
554         if (start < elfcorebuf_sz) {
555                 u64 pfn;
556
557                 tsz = min(elfcorebuf_sz - (size_t)start, size);
558                 pfn = __pa(elfcorebuf + start) >> PAGE_SHIFT;
559                 if (remap_pfn_range(vma, vma->vm_start, pfn, tsz,
560                                     vma->vm_page_prot))
561                         return -EAGAIN;
562                 size -= tsz;
563                 start += tsz;
564                 len += tsz;
565
566                 if (size == 0)
567                         return 0;
568         }
569
570         if (start < elfcorebuf_sz + elfnotes_sz) {
571                 void *kaddr;
572
573                 /* We add device dumps before other elf notes because the
574                  * other elf notes may not fill the elf notes buffer
575                  * completely and we will end up with zero-filled data
576                  * between the elf notes and the device dumps. Tools will
577                  * then try to decode this zero-filled data as valid notes
578                  * and we don't want that. Hence, adding device dumps before
579                  * the other elf notes ensure that zero-filled data can be
580                  * avoided. This also ensures that the device dumps and
581                  * other elf notes can be properly mmaped at page aligned
582                  * address.
583                  */
584 #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
585                 /* Read device dumps */
586                 if (start < elfcorebuf_sz + vmcoredd_orig_sz) {
587                         u64 start_off;
588
589                         tsz = min(elfcorebuf_sz + vmcoredd_orig_sz -
590                                   (size_t)start, size);
591                         start_off = start - elfcorebuf_sz;
592                         if (vmcoredd_mmap_dumps(vma, vma->vm_start + len,
593                                                 start_off, tsz))
594                                 goto fail;
595
596                         size -= tsz;
597                         start += tsz;
598                         len += tsz;
599
600                         /* leave now if filled buffer already */
601                         if (!size)
602                                 return 0;
603                 }
604 #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
605
606                 /* Read remaining elf notes */
607                 tsz = min(elfcorebuf_sz + elfnotes_sz - (size_t)start, size);
608                 kaddr = elfnotes_buf + start - elfcorebuf_sz - vmcoredd_orig_sz;
609                 if (remap_vmalloc_range_partial(vma, vma->vm_start + len,
610                                                 kaddr, tsz))
611                         goto fail;
612
613                 size -= tsz;
614                 start += tsz;
615                 len += tsz;
616
617                 if (size == 0)
618                         return 0;
619         }
620
621         list_for_each_entry(m, &vmcore_list, list) {
622                 if (start < m->offset + m->size) {
623                         u64 paddr = 0;
624
625                         tsz = (size_t)min_t(unsigned long long,
626                                             m->offset + m->size - start, size);
627                         paddr = m->paddr + start - m->offset;
628                         if (vmcore_remap_oldmem_pfn(vma, vma->vm_start + len,
629                                                     paddr >> PAGE_SHIFT, tsz,
630                                                     vma->vm_page_prot))
631                                 goto fail;
632                         size -= tsz;
633                         start += tsz;
634                         len += tsz;
635
636                         if (size == 0)
637                                 return 0;
638                 }
639         }
640
641         return 0;
642 fail:
643         do_munmap(vma->vm_mm, vma->vm_start, len, NULL);
644         return -EAGAIN;
645 }
646 #else
647 static int mmap_vmcore(struct file *file, struct vm_area_struct *vma)
648 {
649         return -ENOSYS;
650 }
651 #endif
652
653 static const struct file_operations proc_vmcore_operations = {
654         .read           = read_vmcore,
655         .llseek         = default_llseek,
656         .mmap           = mmap_vmcore,
657 };
658
659 static struct vmcore* __init get_new_element(void)
660 {
661         return kzalloc(sizeof(struct vmcore), GFP_KERNEL);
662 }
663
664 static u64 get_vmcore_size(size_t elfsz, size_t elfnotesegsz,
665                            struct list_head *vc_list)
666 {
667         u64 size;
668         struct vmcore *m;
669
670         size = elfsz + elfnotesegsz;
671         list_for_each_entry(m, vc_list, list) {
672                 size += m->size;
673         }
674         return size;
675 }
676
677 /**
678  * update_note_header_size_elf64 - update p_memsz member of each PT_NOTE entry
679  *
680  * @ehdr_ptr: ELF header
681  *
682  * This function updates p_memsz member of each PT_NOTE entry in the
683  * program header table pointed to by @ehdr_ptr to real size of ELF
684  * note segment.
685  */
686 static int __init update_note_header_size_elf64(const Elf64_Ehdr *ehdr_ptr)
687 {
688         int i, rc=0;
689         Elf64_Phdr *phdr_ptr;
690         Elf64_Nhdr *nhdr_ptr;
691
692         phdr_ptr = (Elf64_Phdr *)(ehdr_ptr + 1);
693         for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
694                 void *notes_section;
695                 u64 offset, max_sz, sz, real_sz = 0;
696                 if (phdr_ptr->p_type != PT_NOTE)
697                         continue;
698                 max_sz = phdr_ptr->p_memsz;
699                 offset = phdr_ptr->p_offset;
700                 notes_section = kmalloc(max_sz, GFP_KERNEL);
701                 if (!notes_section)
702                         return -ENOMEM;
703                 rc = elfcorehdr_read_notes(notes_section, max_sz, &offset);
704                 if (rc < 0) {
705                         kfree(notes_section);
706                         return rc;
707                 }
708                 nhdr_ptr = notes_section;
709                 while (nhdr_ptr->n_namesz != 0) {
710                         sz = sizeof(Elf64_Nhdr) +
711                                 (((u64)nhdr_ptr->n_namesz + 3) & ~3) +
712                                 (((u64)nhdr_ptr->n_descsz + 3) & ~3);
713                         if ((real_sz + sz) > max_sz) {
714                                 pr_warn("Warning: Exceeded p_memsz, dropping PT_NOTE entry n_namesz=0x%x, n_descsz=0x%x\n",
715                                         nhdr_ptr->n_namesz, nhdr_ptr->n_descsz);
716                                 break;
717                         }
718                         real_sz += sz;
719                         nhdr_ptr = (Elf64_Nhdr*)((char*)nhdr_ptr + sz);
720                 }
721                 kfree(notes_section);
722                 phdr_ptr->p_memsz = real_sz;
723                 if (real_sz == 0) {
724                         pr_warn("Warning: Zero PT_NOTE entries found\n");
725                 }
726         }
727
728         return 0;
729 }
730
731 /**
732  * get_note_number_and_size_elf64 - get the number of PT_NOTE program
733  * headers and sum of real size of their ELF note segment headers and
734  * data.
735  *
736  * @ehdr_ptr: ELF header
737  * @nr_ptnote: buffer for the number of PT_NOTE program headers
738  * @sz_ptnote: buffer for size of unique PT_NOTE program header
739  *
740  * This function is used to merge multiple PT_NOTE program headers
741  * into a unique single one. The resulting unique entry will have
742  * @sz_ptnote in its phdr->p_mem.
743  *
744  * It is assumed that program headers with PT_NOTE type pointed to by
745  * @ehdr_ptr has already been updated by update_note_header_size_elf64
746  * and each of PT_NOTE program headers has actual ELF note segment
747  * size in its p_memsz member.
748  */
749 static int __init get_note_number_and_size_elf64(const Elf64_Ehdr *ehdr_ptr,
750                                                  int *nr_ptnote, u64 *sz_ptnote)
751 {
752         int i;
753         Elf64_Phdr *phdr_ptr;
754
755         *nr_ptnote = *sz_ptnote = 0;
756
757         phdr_ptr = (Elf64_Phdr *)(ehdr_ptr + 1);
758         for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
759                 if (phdr_ptr->p_type != PT_NOTE)
760                         continue;
761                 *nr_ptnote += 1;
762                 *sz_ptnote += phdr_ptr->p_memsz;
763         }
764
765         return 0;
766 }
767
768 /**
769  * copy_notes_elf64 - copy ELF note segments in a given buffer
770  *
771  * @ehdr_ptr: ELF header
772  * @notes_buf: buffer into which ELF note segments are copied
773  *
774  * This function is used to copy ELF note segment in the 1st kernel
775  * into the buffer @notes_buf in the 2nd kernel. It is assumed that
776  * size of the buffer @notes_buf is equal to or larger than sum of the
777  * real ELF note segment headers and data.
778  *
779  * It is assumed that program headers with PT_NOTE type pointed to by
780  * @ehdr_ptr has already been updated by update_note_header_size_elf64
781  * and each of PT_NOTE program headers has actual ELF note segment
782  * size in its p_memsz member.
783  */
784 static int __init copy_notes_elf64(const Elf64_Ehdr *ehdr_ptr, char *notes_buf)
785 {
786         int i, rc=0;
787         Elf64_Phdr *phdr_ptr;
788
789         phdr_ptr = (Elf64_Phdr*)(ehdr_ptr + 1);
790
791         for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
792                 u64 offset;
793                 if (phdr_ptr->p_type != PT_NOTE)
794                         continue;
795                 offset = phdr_ptr->p_offset;
796                 rc = elfcorehdr_read_notes(notes_buf, phdr_ptr->p_memsz,
797                                            &offset);
798                 if (rc < 0)
799                         return rc;
800                 notes_buf += phdr_ptr->p_memsz;
801         }
802
803         return 0;
804 }
805
806 /* Merges all the PT_NOTE headers into one. */
807 static int __init merge_note_headers_elf64(char *elfptr, size_t *elfsz,
808                                            char **notes_buf, size_t *notes_sz)
809 {
810         int i, nr_ptnote=0, rc=0;
811         char *tmp;
812         Elf64_Ehdr *ehdr_ptr;
813         Elf64_Phdr phdr;
814         u64 phdr_sz = 0, note_off;
815
816         ehdr_ptr = (Elf64_Ehdr *)elfptr;
817
818         rc = update_note_header_size_elf64(ehdr_ptr);
819         if (rc < 0)
820                 return rc;
821
822         rc = get_note_number_and_size_elf64(ehdr_ptr, &nr_ptnote, &phdr_sz);
823         if (rc < 0)
824                 return rc;
825
826         *notes_sz = roundup(phdr_sz, PAGE_SIZE);
827         *notes_buf = vmcore_alloc_buf(*notes_sz);
828         if (!*notes_buf)
829                 return -ENOMEM;
830
831         rc = copy_notes_elf64(ehdr_ptr, *notes_buf);
832         if (rc < 0)
833                 return rc;
834
835         /* Prepare merged PT_NOTE program header. */
836         phdr.p_type    = PT_NOTE;
837         phdr.p_flags   = 0;
838         note_off = sizeof(Elf64_Ehdr) +
839                         (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf64_Phdr);
840         phdr.p_offset  = roundup(note_off, PAGE_SIZE);
841         phdr.p_vaddr   = phdr.p_paddr = 0;
842         phdr.p_filesz  = phdr.p_memsz = phdr_sz;
843         phdr.p_align   = 0;
844
845         /* Add merged PT_NOTE program header*/
846         tmp = elfptr + sizeof(Elf64_Ehdr);
847         memcpy(tmp, &phdr, sizeof(phdr));
848         tmp += sizeof(phdr);
849
850         /* Remove unwanted PT_NOTE program headers. */
851         i = (nr_ptnote - 1) * sizeof(Elf64_Phdr);
852         *elfsz = *elfsz - i;
853         memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf64_Ehdr)-sizeof(Elf64_Phdr)));
854         memset(elfptr + *elfsz, 0, i);
855         *elfsz = roundup(*elfsz, PAGE_SIZE);
856
857         /* Modify e_phnum to reflect merged headers. */
858         ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
859
860         /* Store the size of all notes.  We need this to update the note
861          * header when the device dumps will be added.
862          */
863         elfnotes_orig_sz = phdr.p_memsz;
864
865         return 0;
866 }
867
868 /**
869  * update_note_header_size_elf32 - update p_memsz member of each PT_NOTE entry
870  *
871  * @ehdr_ptr: ELF header
872  *
873  * This function updates p_memsz member of each PT_NOTE entry in the
874  * program header table pointed to by @ehdr_ptr to real size of ELF
875  * note segment.
876  */
877 static int __init update_note_header_size_elf32(const Elf32_Ehdr *ehdr_ptr)
878 {
879         int i, rc=0;
880         Elf32_Phdr *phdr_ptr;
881         Elf32_Nhdr *nhdr_ptr;
882
883         phdr_ptr = (Elf32_Phdr *)(ehdr_ptr + 1);
884         for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
885                 void *notes_section;
886                 u64 offset, max_sz, sz, real_sz = 0;
887                 if (phdr_ptr->p_type != PT_NOTE)
888                         continue;
889                 max_sz = phdr_ptr->p_memsz;
890                 offset = phdr_ptr->p_offset;
891                 notes_section = kmalloc(max_sz, GFP_KERNEL);
892                 if (!notes_section)
893                         return -ENOMEM;
894                 rc = elfcorehdr_read_notes(notes_section, max_sz, &offset);
895                 if (rc < 0) {
896                         kfree(notes_section);
897                         return rc;
898                 }
899                 nhdr_ptr = notes_section;
900                 while (nhdr_ptr->n_namesz != 0) {
901                         sz = sizeof(Elf32_Nhdr) +
902                                 (((u64)nhdr_ptr->n_namesz + 3) & ~3) +
903                                 (((u64)nhdr_ptr->n_descsz + 3) & ~3);
904                         if ((real_sz + sz) > max_sz) {
905                                 pr_warn("Warning: Exceeded p_memsz, dropping PT_NOTE entry n_namesz=0x%x, n_descsz=0x%x\n",
906                                         nhdr_ptr->n_namesz, nhdr_ptr->n_descsz);
907                                 break;
908                         }
909                         real_sz += sz;
910                         nhdr_ptr = (Elf32_Nhdr*)((char*)nhdr_ptr + sz);
911                 }
912                 kfree(notes_section);
913                 phdr_ptr->p_memsz = real_sz;
914                 if (real_sz == 0) {
915                         pr_warn("Warning: Zero PT_NOTE entries found\n");
916                 }
917         }
918
919         return 0;
920 }
921
922 /**
923  * get_note_number_and_size_elf32 - get the number of PT_NOTE program
924  * headers and sum of real size of their ELF note segment headers and
925  * data.
926  *
927  * @ehdr_ptr: ELF header
928  * @nr_ptnote: buffer for the number of PT_NOTE program headers
929  * @sz_ptnote: buffer for size of unique PT_NOTE program header
930  *
931  * This function is used to merge multiple PT_NOTE program headers
932  * into a unique single one. The resulting unique entry will have
933  * @sz_ptnote in its phdr->p_mem.
934  *
935  * It is assumed that program headers with PT_NOTE type pointed to by
936  * @ehdr_ptr has already been updated by update_note_header_size_elf32
937  * and each of PT_NOTE program headers has actual ELF note segment
938  * size in its p_memsz member.
939  */
940 static int __init get_note_number_and_size_elf32(const Elf32_Ehdr *ehdr_ptr,
941                                                  int *nr_ptnote, u64 *sz_ptnote)
942 {
943         int i;
944         Elf32_Phdr *phdr_ptr;
945
946         *nr_ptnote = *sz_ptnote = 0;
947
948         phdr_ptr = (Elf32_Phdr *)(ehdr_ptr + 1);
949         for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
950                 if (phdr_ptr->p_type != PT_NOTE)
951                         continue;
952                 *nr_ptnote += 1;
953                 *sz_ptnote += phdr_ptr->p_memsz;
954         }
955
956         return 0;
957 }
958
959 /**
960  * copy_notes_elf32 - copy ELF note segments in a given buffer
961  *
962  * @ehdr_ptr: ELF header
963  * @notes_buf: buffer into which ELF note segments are copied
964  *
965  * This function is used to copy ELF note segment in the 1st kernel
966  * into the buffer @notes_buf in the 2nd kernel. It is assumed that
967  * size of the buffer @notes_buf is equal to or larger than sum of the
968  * real ELF note segment headers and data.
969  *
970  * It is assumed that program headers with PT_NOTE type pointed to by
971  * @ehdr_ptr has already been updated by update_note_header_size_elf32
972  * and each of PT_NOTE program headers has actual ELF note segment
973  * size in its p_memsz member.
974  */
975 static int __init copy_notes_elf32(const Elf32_Ehdr *ehdr_ptr, char *notes_buf)
976 {
977         int i, rc=0;
978         Elf32_Phdr *phdr_ptr;
979
980         phdr_ptr = (Elf32_Phdr*)(ehdr_ptr + 1);
981
982         for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
983                 u64 offset;
984                 if (phdr_ptr->p_type != PT_NOTE)
985                         continue;
986                 offset = phdr_ptr->p_offset;
987                 rc = elfcorehdr_read_notes(notes_buf, phdr_ptr->p_memsz,
988                                            &offset);
989                 if (rc < 0)
990                         return rc;
991                 notes_buf += phdr_ptr->p_memsz;
992         }
993
994         return 0;
995 }
996
997 /* Merges all the PT_NOTE headers into one. */
998 static int __init merge_note_headers_elf32(char *elfptr, size_t *elfsz,
999                                            char **notes_buf, size_t *notes_sz)
1000 {
1001         int i, nr_ptnote=0, rc=0;
1002         char *tmp;
1003         Elf32_Ehdr *ehdr_ptr;
1004         Elf32_Phdr phdr;
1005         u64 phdr_sz = 0, note_off;
1006
1007         ehdr_ptr = (Elf32_Ehdr *)elfptr;
1008
1009         rc = update_note_header_size_elf32(ehdr_ptr);
1010         if (rc < 0)
1011                 return rc;
1012
1013         rc = get_note_number_and_size_elf32(ehdr_ptr, &nr_ptnote, &phdr_sz);
1014         if (rc < 0)
1015                 return rc;
1016
1017         *notes_sz = roundup(phdr_sz, PAGE_SIZE);
1018         *notes_buf = vmcore_alloc_buf(*notes_sz);
1019         if (!*notes_buf)
1020                 return -ENOMEM;
1021
1022         rc = copy_notes_elf32(ehdr_ptr, *notes_buf);
1023         if (rc < 0)
1024                 return rc;
1025
1026         /* Prepare merged PT_NOTE program header. */
1027         phdr.p_type    = PT_NOTE;
1028         phdr.p_flags   = 0;
1029         note_off = sizeof(Elf32_Ehdr) +
1030                         (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf32_Phdr);
1031         phdr.p_offset  = roundup(note_off, PAGE_SIZE);
1032         phdr.p_vaddr   = phdr.p_paddr = 0;
1033         phdr.p_filesz  = phdr.p_memsz = phdr_sz;
1034         phdr.p_align   = 0;
1035
1036         /* Add merged PT_NOTE program header*/
1037         tmp = elfptr + sizeof(Elf32_Ehdr);
1038         memcpy(tmp, &phdr, sizeof(phdr));
1039         tmp += sizeof(phdr);
1040
1041         /* Remove unwanted PT_NOTE program headers. */
1042         i = (nr_ptnote - 1) * sizeof(Elf32_Phdr);
1043         *elfsz = *elfsz - i;
1044         memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf32_Ehdr)-sizeof(Elf32_Phdr)));
1045         memset(elfptr + *elfsz, 0, i);
1046         *elfsz = roundup(*elfsz, PAGE_SIZE);
1047
1048         /* Modify e_phnum to reflect merged headers. */
1049         ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
1050
1051         /* Store the size of all notes.  We need this to update the note
1052          * header when the device dumps will be added.
1053          */
1054         elfnotes_orig_sz = phdr.p_memsz;
1055
1056         return 0;
1057 }
1058
1059 /* Add memory chunks represented by program headers to vmcore list. Also update
1060  * the new offset fields of exported program headers. */
1061 static int __init process_ptload_program_headers_elf64(char *elfptr,
1062                                                 size_t elfsz,
1063                                                 size_t elfnotes_sz,
1064                                                 struct list_head *vc_list)
1065 {
1066         int i;
1067         Elf64_Ehdr *ehdr_ptr;
1068         Elf64_Phdr *phdr_ptr;
1069         loff_t vmcore_off;
1070         struct vmcore *new;
1071
1072         ehdr_ptr = (Elf64_Ehdr *)elfptr;
1073         phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr)); /* PT_NOTE hdr */
1074
1075         /* Skip Elf header, program headers and Elf note segment. */
1076         vmcore_off = elfsz + elfnotes_sz;
1077
1078         for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
1079                 u64 paddr, start, end, size;
1080
1081                 if (phdr_ptr->p_type != PT_LOAD)
1082                         continue;
1083
1084                 paddr = phdr_ptr->p_offset;
1085                 start = rounddown(paddr, PAGE_SIZE);
1086                 end = roundup(paddr + phdr_ptr->p_memsz, PAGE_SIZE);
1087                 size = end - start;
1088
1089                 /* Add this contiguous chunk of memory to vmcore list.*/
1090                 new = get_new_element();
1091                 if (!new)
1092                         return -ENOMEM;
1093                 new->paddr = start;
1094                 new->size = size;
1095                 list_add_tail(&new->list, vc_list);
1096
1097                 /* Update the program header offset. */
1098                 phdr_ptr->p_offset = vmcore_off + (paddr - start);
1099                 vmcore_off = vmcore_off + size;
1100         }
1101         return 0;
1102 }
1103
1104 static int __init process_ptload_program_headers_elf32(char *elfptr,
1105                                                 size_t elfsz,
1106                                                 size_t elfnotes_sz,
1107                                                 struct list_head *vc_list)
1108 {
1109         int i;
1110         Elf32_Ehdr *ehdr_ptr;
1111         Elf32_Phdr *phdr_ptr;
1112         loff_t vmcore_off;
1113         struct vmcore *new;
1114
1115         ehdr_ptr = (Elf32_Ehdr *)elfptr;
1116         phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr)); /* PT_NOTE hdr */
1117
1118         /* Skip Elf header, program headers and Elf note segment. */
1119         vmcore_off = elfsz + elfnotes_sz;
1120
1121         for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
1122                 u64 paddr, start, end, size;
1123
1124                 if (phdr_ptr->p_type != PT_LOAD)
1125                         continue;
1126
1127                 paddr = phdr_ptr->p_offset;
1128                 start = rounddown(paddr, PAGE_SIZE);
1129                 end = roundup(paddr + phdr_ptr->p_memsz, PAGE_SIZE);
1130                 size = end - start;
1131
1132                 /* Add this contiguous chunk of memory to vmcore list.*/
1133                 new = get_new_element();
1134                 if (!new)
1135                         return -ENOMEM;
1136                 new->paddr = start;
1137                 new->size = size;
1138                 list_add_tail(&new->list, vc_list);
1139
1140                 /* Update the program header offset */
1141                 phdr_ptr->p_offset = vmcore_off + (paddr - start);
1142                 vmcore_off = vmcore_off + size;
1143         }
1144         return 0;
1145 }
1146
1147 /* Sets offset fields of vmcore elements. */
1148 static void set_vmcore_list_offsets(size_t elfsz, size_t elfnotes_sz,
1149                                     struct list_head *vc_list)
1150 {
1151         loff_t vmcore_off;
1152         struct vmcore *m;
1153
1154         /* Skip Elf header, program headers and Elf note segment. */
1155         vmcore_off = elfsz + elfnotes_sz;
1156
1157         list_for_each_entry(m, vc_list, list) {
1158                 m->offset = vmcore_off;
1159                 vmcore_off += m->size;
1160         }
1161 }
1162
1163 static void free_elfcorebuf(void)
1164 {
1165         free_pages((unsigned long)elfcorebuf, get_order(elfcorebuf_sz_orig));
1166         elfcorebuf = NULL;
1167         vfree(elfnotes_buf);
1168         elfnotes_buf = NULL;
1169 }
1170
1171 static int __init parse_crash_elf64_headers(void)
1172 {
1173         int rc=0;
1174         Elf64_Ehdr ehdr;
1175         u64 addr;
1176
1177         addr = elfcorehdr_addr;
1178
1179         /* Read Elf header */
1180         rc = elfcorehdr_read((char *)&ehdr, sizeof(Elf64_Ehdr), &addr);
1181         if (rc < 0)
1182                 return rc;
1183
1184         /* Do some basic Verification. */
1185         if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
1186                 (ehdr.e_type != ET_CORE) ||
1187                 !vmcore_elf64_check_arch(&ehdr) ||
1188                 ehdr.e_ident[EI_CLASS] != ELFCLASS64 ||
1189                 ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
1190                 ehdr.e_version != EV_CURRENT ||
1191                 ehdr.e_ehsize != sizeof(Elf64_Ehdr) ||
1192                 ehdr.e_phentsize != sizeof(Elf64_Phdr) ||
1193                 ehdr.e_phnum == 0) {
1194                 pr_warn("Warning: Core image elf header is not sane\n");
1195                 return -EINVAL;
1196         }
1197
1198         /* Read in all elf headers. */
1199         elfcorebuf_sz_orig = sizeof(Elf64_Ehdr) +
1200                                 ehdr.e_phnum * sizeof(Elf64_Phdr);
1201         elfcorebuf_sz = elfcorebuf_sz_orig;
1202         elfcorebuf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
1203                                               get_order(elfcorebuf_sz_orig));
1204         if (!elfcorebuf)
1205                 return -ENOMEM;
1206         addr = elfcorehdr_addr;
1207         rc = elfcorehdr_read(elfcorebuf, elfcorebuf_sz_orig, &addr);
1208         if (rc < 0)
1209                 goto fail;
1210
1211         /* Merge all PT_NOTE headers into one. */
1212         rc = merge_note_headers_elf64(elfcorebuf, &elfcorebuf_sz,
1213                                       &elfnotes_buf, &elfnotes_sz);
1214         if (rc)
1215                 goto fail;
1216         rc = process_ptload_program_headers_elf64(elfcorebuf, elfcorebuf_sz,
1217                                                   elfnotes_sz, &vmcore_list);
1218         if (rc)
1219                 goto fail;
1220         set_vmcore_list_offsets(elfcorebuf_sz, elfnotes_sz, &vmcore_list);
1221         return 0;
1222 fail:
1223         free_elfcorebuf();
1224         return rc;
1225 }
1226
1227 static int __init parse_crash_elf32_headers(void)
1228 {
1229         int rc=0;
1230         Elf32_Ehdr ehdr;
1231         u64 addr;
1232
1233         addr = elfcorehdr_addr;
1234
1235         /* Read Elf header */
1236         rc = elfcorehdr_read((char *)&ehdr, sizeof(Elf32_Ehdr), &addr);
1237         if (rc < 0)
1238                 return rc;
1239
1240         /* Do some basic Verification. */
1241         if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
1242                 (ehdr.e_type != ET_CORE) ||
1243                 !vmcore_elf32_check_arch(&ehdr) ||
1244                 ehdr.e_ident[EI_CLASS] != ELFCLASS32||
1245                 ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
1246                 ehdr.e_version != EV_CURRENT ||
1247                 ehdr.e_ehsize != sizeof(Elf32_Ehdr) ||
1248                 ehdr.e_phentsize != sizeof(Elf32_Phdr) ||
1249                 ehdr.e_phnum == 0) {
1250                 pr_warn("Warning: Core image elf header is not sane\n");
1251                 return -EINVAL;
1252         }
1253
1254         /* Read in all elf headers. */
1255         elfcorebuf_sz_orig = sizeof(Elf32_Ehdr) + ehdr.e_phnum * sizeof(Elf32_Phdr);
1256         elfcorebuf_sz = elfcorebuf_sz_orig;
1257         elfcorebuf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
1258                                               get_order(elfcorebuf_sz_orig));
1259         if (!elfcorebuf)
1260                 return -ENOMEM;
1261         addr = elfcorehdr_addr;
1262         rc = elfcorehdr_read(elfcorebuf, elfcorebuf_sz_orig, &addr);
1263         if (rc < 0)
1264                 goto fail;
1265
1266         /* Merge all PT_NOTE headers into one. */
1267         rc = merge_note_headers_elf32(elfcorebuf, &elfcorebuf_sz,
1268                                       &elfnotes_buf, &elfnotes_sz);
1269         if (rc)
1270                 goto fail;
1271         rc = process_ptload_program_headers_elf32(elfcorebuf, elfcorebuf_sz,
1272                                                   elfnotes_sz, &vmcore_list);
1273         if (rc)
1274                 goto fail;
1275         set_vmcore_list_offsets(elfcorebuf_sz, elfnotes_sz, &vmcore_list);
1276         return 0;
1277 fail:
1278         free_elfcorebuf();
1279         return rc;
1280 }
1281
1282 static int __init parse_crash_elf_headers(void)
1283 {
1284         unsigned char e_ident[EI_NIDENT];
1285         u64 addr;
1286         int rc=0;
1287
1288         addr = elfcorehdr_addr;
1289         rc = elfcorehdr_read(e_ident, EI_NIDENT, &addr);
1290         if (rc < 0)
1291                 return rc;
1292         if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) {
1293                 pr_warn("Warning: Core image elf header not found\n");
1294                 return -EINVAL;
1295         }
1296
1297         if (e_ident[EI_CLASS] == ELFCLASS64) {
1298                 rc = parse_crash_elf64_headers();
1299                 if (rc)
1300                         return rc;
1301         } else if (e_ident[EI_CLASS] == ELFCLASS32) {
1302                 rc = parse_crash_elf32_headers();
1303                 if (rc)
1304                         return rc;
1305         } else {
1306                 pr_warn("Warning: Core image elf header is not sane\n");
1307                 return -EINVAL;
1308         }
1309
1310         /* Determine vmcore size. */
1311         vmcore_size = get_vmcore_size(elfcorebuf_sz, elfnotes_sz,
1312                                       &vmcore_list);
1313
1314         return 0;
1315 }
1316
1317 #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
1318 /**
1319  * vmcoredd_write_header - Write vmcore device dump header at the
1320  * beginning of the dump's buffer.
1321  * @buf: Output buffer where the note is written
1322  * @data: Dump info
1323  * @size: Size of the dump
1324  *
1325  * Fills beginning of the dump's buffer with vmcore device dump header.
1326  */
1327 static void vmcoredd_write_header(void *buf, struct vmcoredd_data *data,
1328                                   u32 size)
1329 {
1330         struct vmcoredd_header *vdd_hdr = (struct vmcoredd_header *)buf;
1331
1332         vdd_hdr->n_namesz = sizeof(vdd_hdr->name);
1333         vdd_hdr->n_descsz = size + sizeof(vdd_hdr->dump_name);
1334         vdd_hdr->n_type = NT_VMCOREDD;
1335
1336         strncpy((char *)vdd_hdr->name, VMCOREDD_NOTE_NAME,
1337                 sizeof(vdd_hdr->name));
1338         memcpy(vdd_hdr->dump_name, data->dump_name, sizeof(vdd_hdr->dump_name));
1339 }
1340
1341 /**
1342  * vmcoredd_update_program_headers - Update all Elf program headers
1343  * @elfptr: Pointer to elf header
1344  * @elfnotesz: Size of elf notes aligned to page size
1345  * @vmcoreddsz: Size of device dumps to be added to elf note header
1346  *
1347  * Determine type of Elf header (Elf64 or Elf32) and update the elf note size.
1348  * Also update the offsets of all the program headers after the elf note header.
1349  */
1350 static void vmcoredd_update_program_headers(char *elfptr, size_t elfnotesz,
1351                                             size_t vmcoreddsz)
1352 {
1353         unsigned char *e_ident = (unsigned char *)elfptr;
1354         u64 start, end, size;
1355         loff_t vmcore_off;
1356         u32 i;
1357
1358         vmcore_off = elfcorebuf_sz + elfnotesz;
1359
1360         if (e_ident[EI_CLASS] == ELFCLASS64) {
1361                 Elf64_Ehdr *ehdr = (Elf64_Ehdr *)elfptr;
1362                 Elf64_Phdr *phdr = (Elf64_Phdr *)(elfptr + sizeof(Elf64_Ehdr));
1363
1364                 /* Update all program headers */
1365                 for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
1366                         if (phdr->p_type == PT_NOTE) {
1367                                 /* Update note size */
1368                                 phdr->p_memsz = elfnotes_orig_sz + vmcoreddsz;
1369                                 phdr->p_filesz = phdr->p_memsz;
1370                                 continue;
1371                         }
1372
1373                         start = rounddown(phdr->p_offset, PAGE_SIZE);
1374                         end = roundup(phdr->p_offset + phdr->p_memsz,
1375                                       PAGE_SIZE);
1376                         size = end - start;
1377                         phdr->p_offset = vmcore_off + (phdr->p_offset - start);
1378                         vmcore_off += size;
1379                 }
1380         } else {
1381                 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)elfptr;
1382                 Elf32_Phdr *phdr = (Elf32_Phdr *)(elfptr + sizeof(Elf32_Ehdr));
1383
1384                 /* Update all program headers */
1385                 for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
1386                         if (phdr->p_type == PT_NOTE) {
1387                                 /* Update note size */
1388                                 phdr->p_memsz = elfnotes_orig_sz + vmcoreddsz;
1389                                 phdr->p_filesz = phdr->p_memsz;
1390                                 continue;
1391                         }
1392
1393                         start = rounddown(phdr->p_offset, PAGE_SIZE);
1394                         end = roundup(phdr->p_offset + phdr->p_memsz,
1395                                       PAGE_SIZE);
1396                         size = end - start;
1397                         phdr->p_offset = vmcore_off + (phdr->p_offset - start);
1398                         vmcore_off += size;
1399                 }
1400         }
1401 }
1402
1403 /**
1404  * vmcoredd_update_size - Update the total size of the device dumps and update
1405  * Elf header
1406  * @dump_size: Size of the current device dump to be added to total size
1407  *
1408  * Update the total size of all the device dumps and update the Elf program
1409  * headers. Calculate the new offsets for the vmcore list and update the
1410  * total vmcore size.
1411  */
1412 static void vmcoredd_update_size(size_t dump_size)
1413 {
1414         vmcoredd_orig_sz += dump_size;
1415         elfnotes_sz = roundup(elfnotes_orig_sz, PAGE_SIZE) + vmcoredd_orig_sz;
1416         vmcoredd_update_program_headers(elfcorebuf, elfnotes_sz,
1417                                         vmcoredd_orig_sz);
1418
1419         /* Update vmcore list offsets */
1420         set_vmcore_list_offsets(elfcorebuf_sz, elfnotes_sz, &vmcore_list);
1421
1422         vmcore_size = get_vmcore_size(elfcorebuf_sz, elfnotes_sz,
1423                                       &vmcore_list);
1424         proc_vmcore->size = vmcore_size;
1425 }
1426
1427 /**
1428  * vmcore_add_device_dump - Add a buffer containing device dump to vmcore
1429  * @data: dump info.
1430  *
1431  * Allocate a buffer and invoke the calling driver's dump collect routine.
1432  * Write Elf note at the beginning of the buffer to indicate vmcore device
1433  * dump and add the dump to global list.
1434  */
1435 int vmcore_add_device_dump(struct vmcoredd_data *data)
1436 {
1437         struct vmcoredd_node *dump;
1438         void *buf = NULL;
1439         size_t data_size;
1440         int ret;
1441
1442         if (!data || !strlen(data->dump_name) ||
1443             !data->vmcoredd_callback || !data->size)
1444                 return -EINVAL;
1445
1446         dump = vzalloc(sizeof(*dump));
1447         if (!dump) {
1448                 ret = -ENOMEM;
1449                 goto out_err;
1450         }
1451
1452         /* Keep size of the buffer page aligned so that it can be mmaped */
1453         data_size = roundup(sizeof(struct vmcoredd_header) + data->size,
1454                             PAGE_SIZE);
1455
1456         /* Allocate buffer for driver's to write their dumps */
1457         buf = vmcore_alloc_buf(data_size);
1458         if (!buf) {
1459                 ret = -ENOMEM;
1460                 goto out_err;
1461         }
1462
1463         vmcoredd_write_header(buf, data, data_size -
1464                               sizeof(struct vmcoredd_header));
1465
1466         /* Invoke the driver's dump collection routing */
1467         ret = data->vmcoredd_callback(data, buf +
1468                                       sizeof(struct vmcoredd_header));
1469         if (ret)
1470                 goto out_err;
1471
1472         dump->buf = buf;
1473         dump->size = data_size;
1474
1475         /* Add the dump to driver sysfs list */
1476         mutex_lock(&vmcoredd_mutex);
1477         list_add_tail(&dump->list, &vmcoredd_list);
1478         mutex_unlock(&vmcoredd_mutex);
1479
1480         vmcoredd_update_size(data_size);
1481         return 0;
1482
1483 out_err:
1484         if (buf)
1485                 vfree(buf);
1486
1487         if (dump)
1488                 vfree(dump);
1489
1490         return ret;
1491 }
1492 EXPORT_SYMBOL(vmcore_add_device_dump);
1493 #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
1494
1495 /* Free all dumps in vmcore device dump list */
1496 static void vmcore_free_device_dumps(void)
1497 {
1498 #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
1499         mutex_lock(&vmcoredd_mutex);
1500         while (!list_empty(&vmcoredd_list)) {
1501                 struct vmcoredd_node *dump;
1502
1503                 dump = list_first_entry(&vmcoredd_list, struct vmcoredd_node,
1504                                         list);
1505                 list_del(&dump->list);
1506                 vfree(dump->buf);
1507                 vfree(dump);
1508         }
1509         mutex_unlock(&vmcoredd_mutex);
1510 #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
1511 }
1512
1513 /* Init function for vmcore module. */
1514 static int __init vmcore_init(void)
1515 {
1516         int rc = 0;
1517
1518         /* Allow architectures to allocate ELF header in 2nd kernel */
1519         rc = elfcorehdr_alloc(&elfcorehdr_addr, &elfcorehdr_size);
1520         if (rc)
1521                 return rc;
1522         /*
1523          * If elfcorehdr= has been passed in cmdline or created in 2nd kernel,
1524          * then capture the dump.
1525          */
1526         if (!(is_vmcore_usable()))
1527                 return rc;
1528         rc = parse_crash_elf_headers();
1529         if (rc) {
1530                 pr_warn("Kdump: vmcore not initialized\n");
1531                 return rc;
1532         }
1533         elfcorehdr_free(elfcorehdr_addr);
1534         elfcorehdr_addr = ELFCORE_ADDR_ERR;
1535
1536         proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations);
1537         if (proc_vmcore)
1538                 proc_vmcore->size = vmcore_size;
1539         return 0;
1540 }
1541 fs_initcall(vmcore_init);
1542
1543 /* Cleanup function for vmcore module. */
1544 void vmcore_cleanup(void)
1545 {
1546         if (proc_vmcore) {
1547                 proc_remove(proc_vmcore);
1548                 proc_vmcore = NULL;
1549         }
1550
1551         /* clear the vmcore list. */
1552         while (!list_empty(&vmcore_list)) {
1553                 struct vmcore *m;
1554
1555                 m = list_first_entry(&vmcore_list, struct vmcore, list);
1556                 list_del(&m->list);
1557                 kfree(m);
1558         }
1559         free_elfcorebuf();
1560
1561         /* clear vmcore device dump list */
1562         vmcore_free_device_dumps();
1563 }