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