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