GNU Linux-libre 4.9.337-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/vmalloc.h>
24 #include <linux/pagemap.h>
25 #include <asm/uaccess.h>
26 #include <asm/io.h>
27 #include "internal.h"
28
29 /* List representing chunks of contiguous memory areas and their offsets in
30  * vmcore file.
31  */
32 static LIST_HEAD(vmcore_list);
33
34 /* Stores the pointer to the buffer containing kernel elf core headers. */
35 static char *elfcorebuf;
36 static size_t elfcorebuf_sz;
37 static size_t elfcorebuf_sz_orig;
38
39 static char *elfnotes_buf;
40 static size_t elfnotes_sz;
41
42 /* Total size of vmcore file. */
43 static u64 vmcore_size;
44
45 static struct proc_dir_entry *proc_vmcore;
46
47 /*
48  * Returns > 0 for RAM pages, 0 for non-RAM pages, < 0 on error
49  * The called function has to take care of module refcounting.
50  */
51 static int (*oldmem_pfn_is_ram)(unsigned long pfn);
52
53 int register_oldmem_pfn_is_ram(int (*fn)(unsigned long pfn))
54 {
55         if (oldmem_pfn_is_ram)
56                 return -EBUSY;
57         oldmem_pfn_is_ram = fn;
58         return 0;
59 }
60 EXPORT_SYMBOL_GPL(register_oldmem_pfn_is_ram);
61
62 void unregister_oldmem_pfn_is_ram(void)
63 {
64         oldmem_pfn_is_ram = NULL;
65         wmb();
66 }
67 EXPORT_SYMBOL_GPL(unregister_oldmem_pfn_is_ram);
68
69 static int pfn_is_ram(unsigned long pfn)
70 {
71         int (*fn)(unsigned long pfn);
72         /* pfn is ram unless fn() checks pagetype */
73         int ret = 1;
74
75         /*
76          * Ask hypervisor if the pfn is really ram.
77          * A ballooned page contains no data and reading from such a page
78          * will cause high load in the hypervisor.
79          */
80         fn = oldmem_pfn_is_ram;
81         if (fn)
82                 ret = fn(pfn);
83
84         return ret;
85 }
86
87 /* Reads a page from the oldmem device from given offset. */
88 static ssize_t read_from_oldmem(char *buf, size_t count,
89                                 u64 *ppos, int userbuf)
90 {
91         unsigned long pfn, offset;
92         size_t nr_bytes;
93         ssize_t read = 0, tmp;
94
95         if (!count)
96                 return 0;
97
98         offset = (unsigned long)(*ppos % PAGE_SIZE);
99         pfn = (unsigned long)(*ppos / PAGE_SIZE);
100
101         do {
102                 if (count > (PAGE_SIZE - offset))
103                         nr_bytes = PAGE_SIZE - offset;
104                 else
105                         nr_bytes = count;
106
107                 /* If pfn is not ram, return zeros for sparse dump files */
108                 if (pfn_is_ram(pfn) == 0) {
109                         tmp = 0;
110                         if (!userbuf)
111                                 memset(buf, 0, nr_bytes);
112                         else if (clear_user(buf, nr_bytes))
113                                 tmp = -EFAULT;
114                 } else {
115                         tmp = copy_oldmem_page(pfn, buf, nr_bytes,
116                                                 offset, userbuf);
117                 }
118                 if (tmp < 0)
119                         return tmp;
120
121                 *ppos += nr_bytes;
122                 count -= nr_bytes;
123                 buf += nr_bytes;
124                 read += nr_bytes;
125                 ++pfn;
126                 offset = 0;
127         } while (count);
128
129         return read;
130 }
131
132 /*
133  * Architectures may override this function to allocate ELF header in 2nd kernel
134  */
135 int __weak elfcorehdr_alloc(unsigned long long *addr, unsigned long long *size)
136 {
137         return 0;
138 }
139
140 /*
141  * Architectures may override this function to free header
142  */
143 void __weak elfcorehdr_free(unsigned long long addr)
144 {}
145
146 /*
147  * Architectures may override this function to read from ELF header
148  */
149 ssize_t __weak elfcorehdr_read(char *buf, size_t count, u64 *ppos)
150 {
151         return read_from_oldmem(buf, count, ppos, 0);
152 }
153
154 /*
155  * Architectures may override this function to read from notes sections
156  */
157 ssize_t __weak elfcorehdr_read_notes(char *buf, size_t count, u64 *ppos)
158 {
159         return read_from_oldmem(buf, count, ppos, 0);
160 }
161
162 /*
163  * Architectures may override this function to map oldmem
164  */
165 int __weak remap_oldmem_pfn_range(struct vm_area_struct *vma,
166                                   unsigned long from, unsigned long pfn,
167                                   unsigned long size, pgprot_t prot)
168 {
169         return remap_pfn_range(vma, from, pfn, size, prot);
170 }
171
172 /*
173  * Architectures which support memory encryption override this.
174  */
175 ssize_t __weak
176 copy_oldmem_page_encrypted(unsigned long pfn, char *buf, size_t csize,
177                            unsigned long offset, int userbuf)
178 {
179         return copy_oldmem_page(pfn, buf, csize, offset, userbuf);
180 }
181
182 /*
183  * Copy to either kernel or user space
184  */
185 static int copy_to(void *target, void *src, size_t size, int userbuf)
186 {
187         if (userbuf) {
188                 if (copy_to_user((char __user *) target, src, size))
189                         return -EFAULT;
190         } else {
191                 memcpy(target, src, size);
192         }
193         return 0;
194 }
195
196 /* Read from the ELF header and then the crash dump. On error, negative value is
197  * returned otherwise number of bytes read are returned.
198  */
199 static ssize_t __read_vmcore(char *buffer, size_t buflen, loff_t *fpos,
200                              int userbuf)
201 {
202         ssize_t acc = 0, tmp;
203         size_t tsz;
204         u64 start;
205         struct vmcore *m = NULL;
206
207         if (buflen == 0 || *fpos >= vmcore_size)
208                 return 0;
209
210         /* trim buflen to not go beyond EOF */
211         if (buflen > vmcore_size - *fpos)
212                 buflen = vmcore_size - *fpos;
213
214         /* Read ELF core header */
215         if (*fpos < elfcorebuf_sz) {
216                 tsz = min(elfcorebuf_sz - (size_t)*fpos, buflen);
217                 if (copy_to(buffer, elfcorebuf + *fpos, tsz, userbuf))
218                         return -EFAULT;
219                 buflen -= tsz;
220                 *fpos += tsz;
221                 buffer += tsz;
222                 acc += tsz;
223
224                 /* leave now if filled buffer already */
225                 if (buflen == 0)
226                         return acc;
227         }
228
229         /* Read Elf note segment */
230         if (*fpos < elfcorebuf_sz + elfnotes_sz) {
231                 void *kaddr;
232
233                 tsz = min(elfcorebuf_sz + elfnotes_sz - (size_t)*fpos, buflen);
234                 kaddr = elfnotes_buf + *fpos - elfcorebuf_sz;
235                 if (copy_to(buffer, kaddr, tsz, userbuf))
236                         return -EFAULT;
237                 buflen -= tsz;
238                 *fpos += tsz;
239                 buffer += tsz;
240                 acc += tsz;
241
242                 /* leave now if filled buffer already */
243                 if (buflen == 0)
244                         return acc;
245         }
246
247         list_for_each_entry(m, &vmcore_list, list) {
248                 if (*fpos < m->offset + m->size) {
249                         tsz = (size_t)min_t(unsigned long long,
250                                             m->offset + m->size - *fpos,
251                                             buflen);
252                         start = m->paddr + *fpos - m->offset;
253                         tmp = read_from_oldmem(buffer, tsz, &start, userbuf);
254                         if (tmp < 0)
255                                 return tmp;
256                         buflen -= tsz;
257                         *fpos += tsz;
258                         buffer += tsz;
259                         acc += tsz;
260
261                         /* leave now if filled buffer already */
262                         if (buflen == 0)
263                                 return acc;
264                 }
265         }
266
267         return acc;
268 }
269
270 static ssize_t read_vmcore(struct file *file, char __user *buffer,
271                            size_t buflen, loff_t *fpos)
272 {
273         return __read_vmcore((__force char *) buffer, buflen, fpos, 1);
274 }
275
276 /*
277  * The vmcore fault handler uses the page cache and fills data using the
278  * standard __vmcore_read() function.
279  *
280  * On s390 the fault handler is used for memory regions that can't be mapped
281  * directly with remap_pfn_range().
282  */
283 static int mmap_vmcore_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
284 {
285 #ifdef CONFIG_S390
286         struct address_space *mapping = vma->vm_file->f_mapping;
287         pgoff_t index = vmf->pgoff;
288         struct page *page;
289         loff_t offset;
290         char *buf;
291         int rc;
292
293         page = find_or_create_page(mapping, index, GFP_KERNEL);
294         if (!page)
295                 return VM_FAULT_OOM;
296         if (!PageUptodate(page)) {
297                 offset = (loff_t) index << PAGE_SHIFT;
298                 buf = __va((page_to_pfn(page) << PAGE_SHIFT));
299                 rc = __read_vmcore(buf, PAGE_SIZE, &offset, 0);
300                 if (rc < 0) {
301                         unlock_page(page);
302                         put_page(page);
303                         return (rc == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
304                 }
305                 SetPageUptodate(page);
306         }
307         unlock_page(page);
308         vmf->page = page;
309         return 0;
310 #else
311         return VM_FAULT_SIGBUS;
312 #endif
313 }
314
315 static const struct vm_operations_struct vmcore_mmap_ops = {
316         .fault = mmap_vmcore_fault,
317 };
318
319 /**
320  * alloc_elfnotes_buf - allocate buffer for ELF note segment in
321  *                      vmalloc memory
322  *
323  * @notes_sz: size of buffer
324  *
325  * If CONFIG_MMU is defined, use vmalloc_user() to allow users to mmap
326  * the buffer to user-space by means of remap_vmalloc_range().
327  *
328  * If CONFIG_MMU is not defined, use vzalloc() since mmap_vmcore() is
329  * disabled and there's no need to allow users to mmap the buffer.
330  */
331 static inline char *alloc_elfnotes_buf(size_t notes_sz)
332 {
333 #ifdef CONFIG_MMU
334         return vmalloc_user(notes_sz);
335 #else
336         return vzalloc(notes_sz);
337 #endif
338 }
339
340 /*
341  * Disable mmap_vmcore() if CONFIG_MMU is not defined. MMU is
342  * essential for mmap_vmcore() in order to map physically
343  * non-contiguous objects (ELF header, ELF note segment and memory
344  * regions in the 1st kernel pointed to by PT_LOAD entries) into
345  * virtually contiguous user-space in ELF layout.
346  */
347 #ifdef CONFIG_MMU
348 /*
349  * remap_oldmem_pfn_checked - do remap_oldmem_pfn_range replacing all pages
350  * reported as not being ram with the zero page.
351  *
352  * @vma: vm_area_struct describing requested mapping
353  * @from: start remapping from
354  * @pfn: page frame number to start remapping to
355  * @size: remapping size
356  * @prot: protection bits
357  *
358  * Returns zero on success, -EAGAIN on failure.
359  */
360 static int remap_oldmem_pfn_checked(struct vm_area_struct *vma,
361                                     unsigned long from, unsigned long pfn,
362                                     unsigned long size, pgprot_t prot)
363 {
364         unsigned long map_size;
365         unsigned long pos_start, pos_end, pos;
366         unsigned long zeropage_pfn = my_zero_pfn(0);
367         size_t len = 0;
368
369         pos_start = pfn;
370         pos_end = pfn + (size >> PAGE_SHIFT);
371
372         for (pos = pos_start; pos < pos_end; ++pos) {
373                 if (!pfn_is_ram(pos)) {
374                         /*
375                          * We hit a page which is not ram. Remap the continuous
376                          * region between pos_start and pos-1 and replace
377                          * the non-ram page at pos with the zero page.
378                          */
379                         if (pos > pos_start) {
380                                 /* Remap continuous region */
381                                 map_size = (pos - pos_start) << PAGE_SHIFT;
382                                 if (remap_oldmem_pfn_range(vma, from + len,
383                                                            pos_start, map_size,
384                                                            prot))
385                                         goto fail;
386                                 len += map_size;
387                         }
388                         /* Remap the zero page */
389                         if (remap_oldmem_pfn_range(vma, from + len,
390                                                    zeropage_pfn,
391                                                    PAGE_SIZE, prot))
392                                 goto fail;
393                         len += PAGE_SIZE;
394                         pos_start = pos + 1;
395                 }
396         }
397         if (pos > pos_start) {
398                 /* Remap the rest */
399                 map_size = (pos - pos_start) << PAGE_SHIFT;
400                 if (remap_oldmem_pfn_range(vma, from + len, pos_start,
401                                            map_size, prot))
402                         goto fail;
403         }
404         return 0;
405 fail:
406         do_munmap(vma->vm_mm, from, len);
407         return -EAGAIN;
408 }
409
410 static int vmcore_remap_oldmem_pfn(struct vm_area_struct *vma,
411                             unsigned long from, unsigned long pfn,
412                             unsigned long size, pgprot_t prot)
413 {
414         /*
415          * Check if oldmem_pfn_is_ram was registered to avoid
416          * looping over all pages without a reason.
417          */
418         if (oldmem_pfn_is_ram)
419                 return remap_oldmem_pfn_checked(vma, from, pfn, size, prot);
420         else
421                 return remap_oldmem_pfn_range(vma, from, pfn, size, prot);
422 }
423
424 static int mmap_vmcore(struct file *file, struct vm_area_struct *vma)
425 {
426         size_t size = vma->vm_end - vma->vm_start;
427         u64 start, end, len, tsz;
428         struct vmcore *m;
429
430         start = (u64)vma->vm_pgoff << PAGE_SHIFT;
431         end = start + size;
432
433         if (size > vmcore_size || end > vmcore_size)
434                 return -EINVAL;
435
436         if (vma->vm_flags & (VM_WRITE | VM_EXEC))
437                 return -EPERM;
438
439         vma->vm_flags &= ~(VM_MAYWRITE | VM_MAYEXEC);
440         vma->vm_flags |= VM_MIXEDMAP;
441         vma->vm_ops = &vmcore_mmap_ops;
442
443         len = 0;
444
445         if (start < elfcorebuf_sz) {
446                 u64 pfn;
447
448                 tsz = min(elfcorebuf_sz - (size_t)start, size);
449                 pfn = __pa(elfcorebuf + start) >> PAGE_SHIFT;
450                 if (remap_pfn_range(vma, vma->vm_start, pfn, tsz,
451                                     vma->vm_page_prot))
452                         return -EAGAIN;
453                 size -= tsz;
454                 start += tsz;
455                 len += tsz;
456
457                 if (size == 0)
458                         return 0;
459         }
460
461         if (start < elfcorebuf_sz + elfnotes_sz) {
462                 void *kaddr;
463
464                 tsz = min(elfcorebuf_sz + elfnotes_sz - (size_t)start, size);
465                 kaddr = elfnotes_buf + start - elfcorebuf_sz;
466                 if (remap_vmalloc_range_partial(vma, vma->vm_start + len,
467                                                 kaddr, 0, tsz))
468                         goto fail;
469                 size -= tsz;
470                 start += tsz;
471                 len += tsz;
472
473                 if (size == 0)
474                         return 0;
475         }
476
477         list_for_each_entry(m, &vmcore_list, list) {
478                 if (start < m->offset + m->size) {
479                         u64 paddr = 0;
480
481                         tsz = (size_t)min_t(unsigned long long,
482                                             m->offset + m->size - start, size);
483                         paddr = m->paddr + start - m->offset;
484                         if (vmcore_remap_oldmem_pfn(vma, vma->vm_start + len,
485                                                     paddr >> PAGE_SHIFT, tsz,
486                                                     vma->vm_page_prot))
487                                 goto fail;
488                         size -= tsz;
489                         start += tsz;
490                         len += tsz;
491
492                         if (size == 0)
493                                 return 0;
494                 }
495         }
496
497         return 0;
498 fail:
499         do_munmap(vma->vm_mm, vma->vm_start, len);
500         return -EAGAIN;
501 }
502 #else
503 static int mmap_vmcore(struct file *file, struct vm_area_struct *vma)
504 {
505         return -ENOSYS;
506 }
507 #endif
508
509 static const struct file_operations proc_vmcore_operations = {
510         .read           = read_vmcore,
511         .llseek         = default_llseek,
512         .mmap           = mmap_vmcore,
513 };
514
515 static struct vmcore* __init get_new_element(void)
516 {
517         return kzalloc(sizeof(struct vmcore), GFP_KERNEL);
518 }
519
520 static u64 __init get_vmcore_size(size_t elfsz, size_t elfnotesegsz,
521                                   struct list_head *vc_list)
522 {
523         u64 size;
524         struct vmcore *m;
525
526         size = elfsz + elfnotesegsz;
527         list_for_each_entry(m, vc_list, list) {
528                 size += m->size;
529         }
530         return size;
531 }
532
533 /**
534  * update_note_header_size_elf64 - update p_memsz member of each PT_NOTE entry
535  *
536  * @ehdr_ptr: ELF header
537  *
538  * This function updates p_memsz member of each PT_NOTE entry in the
539  * program header table pointed to by @ehdr_ptr to real size of ELF
540  * note segment.
541  */
542 static int __init update_note_header_size_elf64(const Elf64_Ehdr *ehdr_ptr)
543 {
544         int i, rc=0;
545         Elf64_Phdr *phdr_ptr;
546         Elf64_Nhdr *nhdr_ptr;
547
548         phdr_ptr = (Elf64_Phdr *)(ehdr_ptr + 1);
549         for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
550                 void *notes_section;
551                 u64 offset, max_sz, sz, real_sz = 0;
552                 if (phdr_ptr->p_type != PT_NOTE)
553                         continue;
554                 max_sz = phdr_ptr->p_memsz;
555                 offset = phdr_ptr->p_offset;
556                 notes_section = kmalloc(max_sz, GFP_KERNEL);
557                 if (!notes_section)
558                         return -ENOMEM;
559                 rc = elfcorehdr_read_notes(notes_section, max_sz, &offset);
560                 if (rc < 0) {
561                         kfree(notes_section);
562                         return rc;
563                 }
564                 nhdr_ptr = notes_section;
565                 while (nhdr_ptr->n_namesz != 0) {
566                         sz = sizeof(Elf64_Nhdr) +
567                                 (((u64)nhdr_ptr->n_namesz + 3) & ~3) +
568                                 (((u64)nhdr_ptr->n_descsz + 3) & ~3);
569                         if ((real_sz + sz) > max_sz) {
570                                 pr_warn("Warning: Exceeded p_memsz, dropping PT_NOTE entry n_namesz=0x%x, n_descsz=0x%x\n",
571                                         nhdr_ptr->n_namesz, nhdr_ptr->n_descsz);
572                                 break;
573                         }
574                         real_sz += sz;
575                         nhdr_ptr = (Elf64_Nhdr*)((char*)nhdr_ptr + sz);
576                 }
577                 kfree(notes_section);
578                 phdr_ptr->p_memsz = real_sz;
579                 if (real_sz == 0) {
580                         pr_warn("Warning: Zero PT_NOTE entries found\n");
581                 }
582         }
583
584         return 0;
585 }
586
587 /**
588  * get_note_number_and_size_elf64 - get the number of PT_NOTE program
589  * headers and sum of real size of their ELF note segment headers and
590  * data.
591  *
592  * @ehdr_ptr: ELF header
593  * @nr_ptnote: buffer for the number of PT_NOTE program headers
594  * @sz_ptnote: buffer for size of unique PT_NOTE program header
595  *
596  * This function is used to merge multiple PT_NOTE program headers
597  * into a unique single one. The resulting unique entry will have
598  * @sz_ptnote in its phdr->p_mem.
599  *
600  * It is assumed that program headers with PT_NOTE type pointed to by
601  * @ehdr_ptr has already been updated by update_note_header_size_elf64
602  * and each of PT_NOTE program headers has actual ELF note segment
603  * size in its p_memsz member.
604  */
605 static int __init get_note_number_and_size_elf64(const Elf64_Ehdr *ehdr_ptr,
606                                                  int *nr_ptnote, u64 *sz_ptnote)
607 {
608         int i;
609         Elf64_Phdr *phdr_ptr;
610
611         *nr_ptnote = *sz_ptnote = 0;
612
613         phdr_ptr = (Elf64_Phdr *)(ehdr_ptr + 1);
614         for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
615                 if (phdr_ptr->p_type != PT_NOTE)
616                         continue;
617                 *nr_ptnote += 1;
618                 *sz_ptnote += phdr_ptr->p_memsz;
619         }
620
621         return 0;
622 }
623
624 /**
625  * copy_notes_elf64 - copy ELF note segments in a given buffer
626  *
627  * @ehdr_ptr: ELF header
628  * @notes_buf: buffer into which ELF note segments are copied
629  *
630  * This function is used to copy ELF note segment in the 1st kernel
631  * into the buffer @notes_buf in the 2nd kernel. It is assumed that
632  * size of the buffer @notes_buf is equal to or larger than sum of the
633  * real ELF note segment headers and data.
634  *
635  * It is assumed that program headers with PT_NOTE type pointed to by
636  * @ehdr_ptr has already been updated by update_note_header_size_elf64
637  * and each of PT_NOTE program headers has actual ELF note segment
638  * size in its p_memsz member.
639  */
640 static int __init copy_notes_elf64(const Elf64_Ehdr *ehdr_ptr, char *notes_buf)
641 {
642         int i, rc=0;
643         Elf64_Phdr *phdr_ptr;
644
645         phdr_ptr = (Elf64_Phdr*)(ehdr_ptr + 1);
646
647         for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
648                 u64 offset;
649                 if (phdr_ptr->p_type != PT_NOTE)
650                         continue;
651                 offset = phdr_ptr->p_offset;
652                 rc = elfcorehdr_read_notes(notes_buf, phdr_ptr->p_memsz,
653                                            &offset);
654                 if (rc < 0)
655                         return rc;
656                 notes_buf += phdr_ptr->p_memsz;
657         }
658
659         return 0;
660 }
661
662 /* Merges all the PT_NOTE headers into one. */
663 static int __init merge_note_headers_elf64(char *elfptr, size_t *elfsz,
664                                            char **notes_buf, size_t *notes_sz)
665 {
666         int i, nr_ptnote=0, rc=0;
667         char *tmp;
668         Elf64_Ehdr *ehdr_ptr;
669         Elf64_Phdr phdr;
670         u64 phdr_sz = 0, note_off;
671
672         ehdr_ptr = (Elf64_Ehdr *)elfptr;
673
674         rc = update_note_header_size_elf64(ehdr_ptr);
675         if (rc < 0)
676                 return rc;
677
678         rc = get_note_number_and_size_elf64(ehdr_ptr, &nr_ptnote, &phdr_sz);
679         if (rc < 0)
680                 return rc;
681
682         *notes_sz = roundup(phdr_sz, PAGE_SIZE);
683         *notes_buf = alloc_elfnotes_buf(*notes_sz);
684         if (!*notes_buf)
685                 return -ENOMEM;
686
687         rc = copy_notes_elf64(ehdr_ptr, *notes_buf);
688         if (rc < 0)
689                 return rc;
690
691         /* Prepare merged PT_NOTE program header. */
692         phdr.p_type    = PT_NOTE;
693         phdr.p_flags   = 0;
694         note_off = sizeof(Elf64_Ehdr) +
695                         (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf64_Phdr);
696         phdr.p_offset  = roundup(note_off, PAGE_SIZE);
697         phdr.p_vaddr   = phdr.p_paddr = 0;
698         phdr.p_filesz  = phdr.p_memsz = phdr_sz;
699         phdr.p_align   = 0;
700
701         /* Add merged PT_NOTE program header*/
702         tmp = elfptr + sizeof(Elf64_Ehdr);
703         memcpy(tmp, &phdr, sizeof(phdr));
704         tmp += sizeof(phdr);
705
706         /* Remove unwanted PT_NOTE program headers. */
707         i = (nr_ptnote - 1) * sizeof(Elf64_Phdr);
708         *elfsz = *elfsz - i;
709         memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf64_Ehdr)-sizeof(Elf64_Phdr)));
710         memset(elfptr + *elfsz, 0, i);
711         *elfsz = roundup(*elfsz, PAGE_SIZE);
712
713         /* Modify e_phnum to reflect merged headers. */
714         ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
715
716         return 0;
717 }
718
719 /**
720  * update_note_header_size_elf32 - update p_memsz member of each PT_NOTE entry
721  *
722  * @ehdr_ptr: ELF header
723  *
724  * This function updates p_memsz member of each PT_NOTE entry in the
725  * program header table pointed to by @ehdr_ptr to real size of ELF
726  * note segment.
727  */
728 static int __init update_note_header_size_elf32(const Elf32_Ehdr *ehdr_ptr)
729 {
730         int i, rc=0;
731         Elf32_Phdr *phdr_ptr;
732         Elf32_Nhdr *nhdr_ptr;
733
734         phdr_ptr = (Elf32_Phdr *)(ehdr_ptr + 1);
735         for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
736                 void *notes_section;
737                 u64 offset, max_sz, sz, real_sz = 0;
738                 if (phdr_ptr->p_type != PT_NOTE)
739                         continue;
740                 max_sz = phdr_ptr->p_memsz;
741                 offset = phdr_ptr->p_offset;
742                 notes_section = kmalloc(max_sz, GFP_KERNEL);
743                 if (!notes_section)
744                         return -ENOMEM;
745                 rc = elfcorehdr_read_notes(notes_section, max_sz, &offset);
746                 if (rc < 0) {
747                         kfree(notes_section);
748                         return rc;
749                 }
750                 nhdr_ptr = notes_section;
751                 while (nhdr_ptr->n_namesz != 0) {
752                         sz = sizeof(Elf32_Nhdr) +
753                                 (((u64)nhdr_ptr->n_namesz + 3) & ~3) +
754                                 (((u64)nhdr_ptr->n_descsz + 3) & ~3);
755                         if ((real_sz + sz) > max_sz) {
756                                 pr_warn("Warning: Exceeded p_memsz, dropping PT_NOTE entry n_namesz=0x%x, n_descsz=0x%x\n",
757                                         nhdr_ptr->n_namesz, nhdr_ptr->n_descsz);
758                                 break;
759                         }
760                         real_sz += sz;
761                         nhdr_ptr = (Elf32_Nhdr*)((char*)nhdr_ptr + sz);
762                 }
763                 kfree(notes_section);
764                 phdr_ptr->p_memsz = real_sz;
765                 if (real_sz == 0) {
766                         pr_warn("Warning: Zero PT_NOTE entries found\n");
767                 }
768         }
769
770         return 0;
771 }
772
773 /**
774  * get_note_number_and_size_elf32 - get the number of PT_NOTE program
775  * headers and sum of real size of their ELF note segment headers and
776  * data.
777  *
778  * @ehdr_ptr: ELF header
779  * @nr_ptnote: buffer for the number of PT_NOTE program headers
780  * @sz_ptnote: buffer for size of unique PT_NOTE program header
781  *
782  * This function is used to merge multiple PT_NOTE program headers
783  * into a unique single one. The resulting unique entry will have
784  * @sz_ptnote in its phdr->p_mem.
785  *
786  * It is assumed that program headers with PT_NOTE type pointed to by
787  * @ehdr_ptr has already been updated by update_note_header_size_elf32
788  * and each of PT_NOTE program headers has actual ELF note segment
789  * size in its p_memsz member.
790  */
791 static int __init get_note_number_and_size_elf32(const Elf32_Ehdr *ehdr_ptr,
792                                                  int *nr_ptnote, u64 *sz_ptnote)
793 {
794         int i;
795         Elf32_Phdr *phdr_ptr;
796
797         *nr_ptnote = *sz_ptnote = 0;
798
799         phdr_ptr = (Elf32_Phdr *)(ehdr_ptr + 1);
800         for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
801                 if (phdr_ptr->p_type != PT_NOTE)
802                         continue;
803                 *nr_ptnote += 1;
804                 *sz_ptnote += phdr_ptr->p_memsz;
805         }
806
807         return 0;
808 }
809
810 /**
811  * copy_notes_elf32 - copy ELF note segments in a given buffer
812  *
813  * @ehdr_ptr: ELF header
814  * @notes_buf: buffer into which ELF note segments are copied
815  *
816  * This function is used to copy ELF note segment in the 1st kernel
817  * into the buffer @notes_buf in the 2nd kernel. It is assumed that
818  * size of the buffer @notes_buf is equal to or larger than sum of the
819  * real ELF note segment headers and data.
820  *
821  * It is assumed that program headers with PT_NOTE type pointed to by
822  * @ehdr_ptr has already been updated by update_note_header_size_elf32
823  * and each of PT_NOTE program headers has actual ELF note segment
824  * size in its p_memsz member.
825  */
826 static int __init copy_notes_elf32(const Elf32_Ehdr *ehdr_ptr, char *notes_buf)
827 {
828         int i, rc=0;
829         Elf32_Phdr *phdr_ptr;
830
831         phdr_ptr = (Elf32_Phdr*)(ehdr_ptr + 1);
832
833         for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
834                 u64 offset;
835                 if (phdr_ptr->p_type != PT_NOTE)
836                         continue;
837                 offset = phdr_ptr->p_offset;
838                 rc = elfcorehdr_read_notes(notes_buf, phdr_ptr->p_memsz,
839                                            &offset);
840                 if (rc < 0)
841                         return rc;
842                 notes_buf += phdr_ptr->p_memsz;
843         }
844
845         return 0;
846 }
847
848 /* Merges all the PT_NOTE headers into one. */
849 static int __init merge_note_headers_elf32(char *elfptr, size_t *elfsz,
850                                            char **notes_buf, size_t *notes_sz)
851 {
852         int i, nr_ptnote=0, rc=0;
853         char *tmp;
854         Elf32_Ehdr *ehdr_ptr;
855         Elf32_Phdr phdr;
856         u64 phdr_sz = 0, note_off;
857
858         ehdr_ptr = (Elf32_Ehdr *)elfptr;
859
860         rc = update_note_header_size_elf32(ehdr_ptr);
861         if (rc < 0)
862                 return rc;
863
864         rc = get_note_number_and_size_elf32(ehdr_ptr, &nr_ptnote, &phdr_sz);
865         if (rc < 0)
866                 return rc;
867
868         *notes_sz = roundup(phdr_sz, PAGE_SIZE);
869         *notes_buf = alloc_elfnotes_buf(*notes_sz);
870         if (!*notes_buf)
871                 return -ENOMEM;
872
873         rc = copy_notes_elf32(ehdr_ptr, *notes_buf);
874         if (rc < 0)
875                 return rc;
876
877         /* Prepare merged PT_NOTE program header. */
878         phdr.p_type    = PT_NOTE;
879         phdr.p_flags   = 0;
880         note_off = sizeof(Elf32_Ehdr) +
881                         (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf32_Phdr);
882         phdr.p_offset  = roundup(note_off, PAGE_SIZE);
883         phdr.p_vaddr   = phdr.p_paddr = 0;
884         phdr.p_filesz  = phdr.p_memsz = phdr_sz;
885         phdr.p_align   = 0;
886
887         /* Add merged PT_NOTE program header*/
888         tmp = elfptr + sizeof(Elf32_Ehdr);
889         memcpy(tmp, &phdr, sizeof(phdr));
890         tmp += sizeof(phdr);
891
892         /* Remove unwanted PT_NOTE program headers. */
893         i = (nr_ptnote - 1) * sizeof(Elf32_Phdr);
894         *elfsz = *elfsz - i;
895         memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf32_Ehdr)-sizeof(Elf32_Phdr)));
896         memset(elfptr + *elfsz, 0, i);
897         *elfsz = roundup(*elfsz, PAGE_SIZE);
898
899         /* Modify e_phnum to reflect merged headers. */
900         ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
901
902         return 0;
903 }
904
905 /* Add memory chunks represented by program headers to vmcore list. Also update
906  * the new offset fields of exported program headers. */
907 static int __init process_ptload_program_headers_elf64(char *elfptr,
908                                                 size_t elfsz,
909                                                 size_t elfnotes_sz,
910                                                 struct list_head *vc_list)
911 {
912         int i;
913         Elf64_Ehdr *ehdr_ptr;
914         Elf64_Phdr *phdr_ptr;
915         loff_t vmcore_off;
916         struct vmcore *new;
917
918         ehdr_ptr = (Elf64_Ehdr *)elfptr;
919         phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr)); /* PT_NOTE hdr */
920
921         /* Skip Elf header, program headers and Elf note segment. */
922         vmcore_off = elfsz + elfnotes_sz;
923
924         for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
925                 u64 paddr, start, end, size;
926
927                 if (phdr_ptr->p_type != PT_LOAD)
928                         continue;
929
930                 paddr = phdr_ptr->p_offset;
931                 start = rounddown(paddr, PAGE_SIZE);
932                 end = roundup(paddr + phdr_ptr->p_memsz, PAGE_SIZE);
933                 size = end - start;
934
935                 /* Add this contiguous chunk of memory to vmcore list.*/
936                 new = get_new_element();
937                 if (!new)
938                         return -ENOMEM;
939                 new->paddr = start;
940                 new->size = size;
941                 list_add_tail(&new->list, vc_list);
942
943                 /* Update the program header offset. */
944                 phdr_ptr->p_offset = vmcore_off + (paddr - start);
945                 vmcore_off = vmcore_off + size;
946         }
947         return 0;
948 }
949
950 static int __init process_ptload_program_headers_elf32(char *elfptr,
951                                                 size_t elfsz,
952                                                 size_t elfnotes_sz,
953                                                 struct list_head *vc_list)
954 {
955         int i;
956         Elf32_Ehdr *ehdr_ptr;
957         Elf32_Phdr *phdr_ptr;
958         loff_t vmcore_off;
959         struct vmcore *new;
960
961         ehdr_ptr = (Elf32_Ehdr *)elfptr;
962         phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr)); /* PT_NOTE hdr */
963
964         /* Skip Elf header, program headers and Elf note segment. */
965         vmcore_off = elfsz + elfnotes_sz;
966
967         for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
968                 u64 paddr, start, end, size;
969
970                 if (phdr_ptr->p_type != PT_LOAD)
971                         continue;
972
973                 paddr = phdr_ptr->p_offset;
974                 start = rounddown(paddr, PAGE_SIZE);
975                 end = roundup(paddr + phdr_ptr->p_memsz, PAGE_SIZE);
976                 size = end - start;
977
978                 /* Add this contiguous chunk of memory to vmcore list.*/
979                 new = get_new_element();
980                 if (!new)
981                         return -ENOMEM;
982                 new->paddr = start;
983                 new->size = size;
984                 list_add_tail(&new->list, vc_list);
985
986                 /* Update the program header offset */
987                 phdr_ptr->p_offset = vmcore_off + (paddr - start);
988                 vmcore_off = vmcore_off + size;
989         }
990         return 0;
991 }
992
993 /* Sets offset fields of vmcore elements. */
994 static void __init set_vmcore_list_offsets(size_t elfsz, size_t elfnotes_sz,
995                                            struct list_head *vc_list)
996 {
997         loff_t vmcore_off;
998         struct vmcore *m;
999
1000         /* Skip Elf header, program headers and Elf note segment. */
1001         vmcore_off = elfsz + elfnotes_sz;
1002
1003         list_for_each_entry(m, vc_list, list) {
1004                 m->offset = vmcore_off;
1005                 vmcore_off += m->size;
1006         }
1007 }
1008
1009 static void free_elfcorebuf(void)
1010 {
1011         free_pages((unsigned long)elfcorebuf, get_order(elfcorebuf_sz_orig));
1012         elfcorebuf = NULL;
1013         vfree(elfnotes_buf);
1014         elfnotes_buf = NULL;
1015 }
1016
1017 static int __init parse_crash_elf64_headers(void)
1018 {
1019         int rc=0;
1020         Elf64_Ehdr ehdr;
1021         u64 addr;
1022
1023         addr = elfcorehdr_addr;
1024
1025         /* Read Elf header */
1026         rc = elfcorehdr_read((char *)&ehdr, sizeof(Elf64_Ehdr), &addr);
1027         if (rc < 0)
1028                 return rc;
1029
1030         /* Do some basic Verification. */
1031         if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
1032                 (ehdr.e_type != ET_CORE) ||
1033                 !vmcore_elf64_check_arch(&ehdr) ||
1034                 ehdr.e_ident[EI_CLASS] != ELFCLASS64 ||
1035                 ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
1036                 ehdr.e_version != EV_CURRENT ||
1037                 ehdr.e_ehsize != sizeof(Elf64_Ehdr) ||
1038                 ehdr.e_phentsize != sizeof(Elf64_Phdr) ||
1039                 ehdr.e_phnum == 0) {
1040                 pr_warn("Warning: Core image elf header is not sane\n");
1041                 return -EINVAL;
1042         }
1043
1044         /* Read in all elf headers. */
1045         elfcorebuf_sz_orig = sizeof(Elf64_Ehdr) +
1046                                 ehdr.e_phnum * sizeof(Elf64_Phdr);
1047         elfcorebuf_sz = elfcorebuf_sz_orig;
1048         elfcorebuf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
1049                                               get_order(elfcorebuf_sz_orig));
1050         if (!elfcorebuf)
1051                 return -ENOMEM;
1052         addr = elfcorehdr_addr;
1053         rc = elfcorehdr_read(elfcorebuf, elfcorebuf_sz_orig, &addr);
1054         if (rc < 0)
1055                 goto fail;
1056
1057         /* Merge all PT_NOTE headers into one. */
1058         rc = merge_note_headers_elf64(elfcorebuf, &elfcorebuf_sz,
1059                                       &elfnotes_buf, &elfnotes_sz);
1060         if (rc)
1061                 goto fail;
1062         rc = process_ptload_program_headers_elf64(elfcorebuf, elfcorebuf_sz,
1063                                                   elfnotes_sz, &vmcore_list);
1064         if (rc)
1065                 goto fail;
1066         set_vmcore_list_offsets(elfcorebuf_sz, elfnotes_sz, &vmcore_list);
1067         return 0;
1068 fail:
1069         free_elfcorebuf();
1070         return rc;
1071 }
1072
1073 static int __init parse_crash_elf32_headers(void)
1074 {
1075         int rc=0;
1076         Elf32_Ehdr ehdr;
1077         u64 addr;
1078
1079         addr = elfcorehdr_addr;
1080
1081         /* Read Elf header */
1082         rc = elfcorehdr_read((char *)&ehdr, sizeof(Elf32_Ehdr), &addr);
1083         if (rc < 0)
1084                 return rc;
1085
1086         /* Do some basic Verification. */
1087         if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
1088                 (ehdr.e_type != ET_CORE) ||
1089                 !vmcore_elf32_check_arch(&ehdr) ||
1090                 ehdr.e_ident[EI_CLASS] != ELFCLASS32||
1091                 ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
1092                 ehdr.e_version != EV_CURRENT ||
1093                 ehdr.e_ehsize != sizeof(Elf32_Ehdr) ||
1094                 ehdr.e_phentsize != sizeof(Elf32_Phdr) ||
1095                 ehdr.e_phnum == 0) {
1096                 pr_warn("Warning: Core image elf header is not sane\n");
1097                 return -EINVAL;
1098         }
1099
1100         /* Read in all elf headers. */
1101         elfcorebuf_sz_orig = sizeof(Elf32_Ehdr) + ehdr.e_phnum * sizeof(Elf32_Phdr);
1102         elfcorebuf_sz = elfcorebuf_sz_orig;
1103         elfcorebuf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
1104                                               get_order(elfcorebuf_sz_orig));
1105         if (!elfcorebuf)
1106                 return -ENOMEM;
1107         addr = elfcorehdr_addr;
1108         rc = elfcorehdr_read(elfcorebuf, elfcorebuf_sz_orig, &addr);
1109         if (rc < 0)
1110                 goto fail;
1111
1112         /* Merge all PT_NOTE headers into one. */
1113         rc = merge_note_headers_elf32(elfcorebuf, &elfcorebuf_sz,
1114                                       &elfnotes_buf, &elfnotes_sz);
1115         if (rc)
1116                 goto fail;
1117         rc = process_ptload_program_headers_elf32(elfcorebuf, elfcorebuf_sz,
1118                                                   elfnotes_sz, &vmcore_list);
1119         if (rc)
1120                 goto fail;
1121         set_vmcore_list_offsets(elfcorebuf_sz, elfnotes_sz, &vmcore_list);
1122         return 0;
1123 fail:
1124         free_elfcorebuf();
1125         return rc;
1126 }
1127
1128 static int __init parse_crash_elf_headers(void)
1129 {
1130         unsigned char e_ident[EI_NIDENT];
1131         u64 addr;
1132         int rc=0;
1133
1134         addr = elfcorehdr_addr;
1135         rc = elfcorehdr_read(e_ident, EI_NIDENT, &addr);
1136         if (rc < 0)
1137                 return rc;
1138         if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) {
1139                 pr_warn("Warning: Core image elf header not found\n");
1140                 return -EINVAL;
1141         }
1142
1143         if (e_ident[EI_CLASS] == ELFCLASS64) {
1144                 rc = parse_crash_elf64_headers();
1145                 if (rc)
1146                         return rc;
1147         } else if (e_ident[EI_CLASS] == ELFCLASS32) {
1148                 rc = parse_crash_elf32_headers();
1149                 if (rc)
1150                         return rc;
1151         } else {
1152                 pr_warn("Warning: Core image elf header is not sane\n");
1153                 return -EINVAL;
1154         }
1155
1156         /* Determine vmcore size. */
1157         vmcore_size = get_vmcore_size(elfcorebuf_sz, elfnotes_sz,
1158                                       &vmcore_list);
1159
1160         return 0;
1161 }
1162
1163 /* Init function for vmcore module. */
1164 static int __init vmcore_init(void)
1165 {
1166         int rc = 0;
1167
1168         /* Allow architectures to allocate ELF header in 2nd kernel */
1169         rc = elfcorehdr_alloc(&elfcorehdr_addr, &elfcorehdr_size);
1170         if (rc)
1171                 return rc;
1172         /*
1173          * If elfcorehdr= has been passed in cmdline or created in 2nd kernel,
1174          * then capture the dump.
1175          */
1176         if (!(is_vmcore_usable()))
1177                 return rc;
1178         rc = parse_crash_elf_headers();
1179         if (rc) {
1180                 pr_warn("Kdump: vmcore not initialized\n");
1181                 return rc;
1182         }
1183         elfcorehdr_free(elfcorehdr_addr);
1184         elfcorehdr_addr = ELFCORE_ADDR_ERR;
1185
1186         proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations);
1187         if (proc_vmcore)
1188                 proc_vmcore->size = vmcore_size;
1189         return 0;
1190 }
1191 fs_initcall(vmcore_init);
1192
1193 /* Cleanup function for vmcore module. */
1194 void vmcore_cleanup(void)
1195 {
1196         struct list_head *pos, *next;
1197
1198         if (proc_vmcore) {
1199                 proc_remove(proc_vmcore);
1200                 proc_vmcore = NULL;
1201         }
1202
1203         /* clear the vmcore list. */
1204         list_for_each_safe(pos, next, &vmcore_list) {
1205                 struct vmcore *m;
1206
1207                 m = list_entry(pos, struct vmcore, list);
1208                 list_del(&m->list);
1209                 kfree(m);
1210         }
1211         free_elfcorebuf();
1212 }