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