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