blob: 3fe90443c1bb8b8ad04dbcad10a08862448fd527 [file] [log] [blame]
Vivek Goyal666bfdd2005-06-25 14:58:21 -07001/*
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
Vivek Goyal666bfdd2005-06-25 14:58:21 -070010#include <linux/mm.h>
David Howells2f96b8c2013-04-12 00:10:25 +010011#include <linux/kcore.h>
Vivek Goyal666bfdd2005-06-25 14:58:21 -070012#include <linux/user.h>
Vivek Goyal666bfdd2005-06-25 14:58:21 -070013#include <linux/elf.h>
14#include <linux/elfcore.h>
Paul Gortmakerafeacc82011-05-26 16:00:52 -040015#include <linux/export.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Vivek Goyal666bfdd2005-06-25 14:58:21 -070017#include <linux/highmem.h>
Andrew Morton87ebdc02013-02-27 17:03:16 -080018#include <linux/printk.h>
Mike Rapoport57c8a662018-10-30 15:09:49 -070019#include <linux/memblock.h>
Vivek Goyal666bfdd2005-06-25 14:58:21 -070020#include <linux/init.h>
21#include <linux/crash_dump.h>
22#include <linux/list.h>
Rahul Lakkireddy27242732018-05-02 15:17:17 +053023#include <linux/mutex.h>
HATAYAMA Daisuke83086972013-07-03 15:02:23 -070024#include <linux/vmalloc.h>
Michael Holzheu9cb21812013-09-11 14:24:51 -070025#include <linux/pagemap.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080026#include <linux/uaccess.h>
Lianbo Jiang992b6492018-09-30 16:37:41 +080027#include <linux/mem_encrypt.h>
28#include <asm/pgtable.h>
Vivek Goyal666bfdd2005-06-25 14:58:21 -070029#include <asm/io.h>
David Howells2f96b8c2013-04-12 00:10:25 +010030#include "internal.h"
Vivek Goyal666bfdd2005-06-25 14:58:21 -070031
32/* List representing chunks of contiguous memory areas and their offsets in
33 * vmcore file.
34 */
35static LIST_HEAD(vmcore_list);
36
37/* Stores the pointer to the buffer containing kernel elf core headers. */
38static char *elfcorebuf;
39static size_t elfcorebuf_sz;
HATAYAMA Daisukef2bdacd2013-07-03 15:02:14 -070040static size_t elfcorebuf_sz_orig;
Vivek Goyal666bfdd2005-06-25 14:58:21 -070041
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -070042static char *elfnotes_buf;
43static size_t elfnotes_sz;
Rahul Lakkireddy7efe48d2018-05-02 15:17:18 +053044/* Size of all notes minus the device dump notes */
45static size_t elfnotes_orig_sz;
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -070046
Vivek Goyal666bfdd2005-06-25 14:58:21 -070047/* Total size of vmcore file. */
48static u64 vmcore_size;
49
Fabian Fredericka05e16ad2014-06-06 14:37:04 -070050static struct proc_dir_entry *proc_vmcore;
Vivek Goyal666bfdd2005-06-25 14:58:21 -070051
Rahul Lakkireddy27242732018-05-02 15:17:17 +053052#ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
53/* Device Dump list and mutex to synchronize access to list */
54static LIST_HEAD(vmcoredd_list);
55static DEFINE_MUTEX(vmcoredd_mutex);
56#endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
57
Rahul Lakkireddy7efe48d2018-05-02 15:17:18 +053058/* Device Dump Size */
59static size_t vmcoredd_orig_sz;
60
Olaf Hering997c1362011-05-26 16:25:54 -070061/*
62 * Returns > 0 for RAM pages, 0 for non-RAM pages, < 0 on error
63 * The called function has to take care of module refcounting.
64 */
65static int (*oldmem_pfn_is_ram)(unsigned long pfn);
66
67int register_oldmem_pfn_is_ram(int (*fn)(unsigned long pfn))
68{
69 if (oldmem_pfn_is_ram)
70 return -EBUSY;
71 oldmem_pfn_is_ram = fn;
72 return 0;
73}
74EXPORT_SYMBOL_GPL(register_oldmem_pfn_is_ram);
75
76void unregister_oldmem_pfn_is_ram(void)
77{
78 oldmem_pfn_is_ram = NULL;
79 wmb();
80}
81EXPORT_SYMBOL_GPL(unregister_oldmem_pfn_is_ram);
82
83static int pfn_is_ram(unsigned long pfn)
84{
85 int (*fn)(unsigned long pfn);
86 /* pfn is ram unless fn() checks pagetype */
87 int ret = 1;
88
89 /*
90 * Ask hypervisor if the pfn is really ram.
91 * A ballooned page contains no data and reading from such a page
92 * will cause high load in the hypervisor.
93 */
94 fn = oldmem_pfn_is_ram;
95 if (fn)
96 ret = fn(pfn);
97
98 return ret;
99}
100
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700101/* Reads a page from the oldmem device from given offset. */
102static ssize_t read_from_oldmem(char *buf, size_t count,
Lianbo Jiang992b6492018-09-30 16:37:41 +0800103 u64 *ppos, int userbuf,
104 bool encrypted)
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700105{
106 unsigned long pfn, offset;
107 size_t nr_bytes;
108 ssize_t read = 0, tmp;
109
110 if (!count)
111 return 0;
112
113 offset = (unsigned long)(*ppos % PAGE_SIZE);
114 pfn = (unsigned long)(*ppos / PAGE_SIZE);
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700115
116 do {
117 if (count > (PAGE_SIZE - offset))
118 nr_bytes = PAGE_SIZE - offset;
119 else
120 nr_bytes = count;
121
Olaf Hering997c1362011-05-26 16:25:54 -0700122 /* If pfn is not ram, return zeros for sparse dump files */
123 if (pfn_is_ram(pfn) == 0)
124 memset(buf, 0, nr_bytes);
125 else {
Lianbo Jiang992b6492018-09-30 16:37:41 +0800126 if (encrypted)
127 tmp = copy_oldmem_page_encrypted(pfn, buf,
128 nr_bytes,
129 offset,
130 userbuf);
131 else
132 tmp = copy_oldmem_page(pfn, buf, nr_bytes,
133 offset, userbuf);
134
Olaf Hering997c1362011-05-26 16:25:54 -0700135 if (tmp < 0)
136 return tmp;
137 }
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700138 *ppos += nr_bytes;
139 count -= nr_bytes;
140 buf += nr_bytes;
141 read += nr_bytes;
142 ++pfn;
143 offset = 0;
144 } while (count);
145
146 return read;
147}
148
Michael Holzheube8a8d02013-09-11 14:24:49 -0700149/*
150 * Architectures may override this function to allocate ELF header in 2nd kernel
151 */
152int __weak elfcorehdr_alloc(unsigned long long *addr, unsigned long long *size)
153{
154 return 0;
155}
156
157/*
158 * Architectures may override this function to free header
159 */
160void __weak elfcorehdr_free(unsigned long long addr)
161{}
162
163/*
164 * Architectures may override this function to read from ELF header
165 */
166ssize_t __weak elfcorehdr_read(char *buf, size_t count, u64 *ppos)
167{
Lianbo Jiang992b6492018-09-30 16:37:41 +0800168 return read_from_oldmem(buf, count, ppos, 0, false);
Michael Holzheube8a8d02013-09-11 14:24:49 -0700169}
170
171/*
172 * Architectures may override this function to read from notes sections
173 */
174ssize_t __weak elfcorehdr_read_notes(char *buf, size_t count, u64 *ppos)
175{
Lianbo Jiang992b6492018-09-30 16:37:41 +0800176 return read_from_oldmem(buf, count, ppos, 0, sme_active());
Michael Holzheube8a8d02013-09-11 14:24:49 -0700177}
178
Michael Holzheu9cb21812013-09-11 14:24:51 -0700179/*
180 * Architectures may override this function to map oldmem
181 */
182int __weak remap_oldmem_pfn_range(struct vm_area_struct *vma,
183 unsigned long from, unsigned long pfn,
184 unsigned long size, pgprot_t prot)
185{
Lianbo Jiang992b6492018-09-30 16:37:41 +0800186 prot = pgprot_encrypted(prot);
Michael Holzheu9cb21812013-09-11 14:24:51 -0700187 return remap_pfn_range(vma, from, pfn, size, prot);
188}
189
190/*
Borislav Petkovcf089612018-10-08 10:05:20 +0200191 * Architectures which support memory encryption override this.
192 */
193ssize_t __weak
194copy_oldmem_page_encrypted(unsigned long pfn, char *buf, size_t csize,
195 unsigned long offset, int userbuf)
196{
197 return copy_oldmem_page(pfn, buf, csize, offset, userbuf);
198}
199
200/*
Michael Holzheu9cb21812013-09-11 14:24:51 -0700201 * Copy to either kernel or user space
202 */
203static int copy_to(void *target, void *src, size_t size, int userbuf)
204{
205 if (userbuf) {
206 if (copy_to_user((char __user *) target, src, size))
207 return -EFAULT;
208 } else {
209 memcpy(target, src, size);
210 }
211 return 0;
212}
213
Rahul Lakkireddy7efe48d2018-05-02 15:17:18 +0530214#ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
215static int vmcoredd_copy_dumps(void *dst, u64 start, size_t size, int userbuf)
216{
217 struct vmcoredd_node *dump;
218 u64 offset = 0;
219 int ret = 0;
220 size_t tsz;
221 char *buf;
222
223 mutex_lock(&vmcoredd_mutex);
224 list_for_each_entry(dump, &vmcoredd_list, list) {
225 if (start < offset + dump->size) {
226 tsz = min(offset + (u64)dump->size - start, (u64)size);
227 buf = dump->buf + start - offset;
228 if (copy_to(dst, buf, tsz, userbuf)) {
229 ret = -EFAULT;
230 goto out_unlock;
231 }
232
233 size -= tsz;
234 start += tsz;
235 dst += tsz;
236
237 /* Leave now if buffer filled already */
238 if (!size)
239 goto out_unlock;
240 }
241 offset += dump->size;
242 }
243
244out_unlock:
245 mutex_unlock(&vmcoredd_mutex);
246 return ret;
247}
248
Arnd Bergmanna2036a12018-08-23 17:00:55 -0700249#ifdef CONFIG_MMU
Rahul Lakkireddy7efe48d2018-05-02 15:17:18 +0530250static int vmcoredd_mmap_dumps(struct vm_area_struct *vma, unsigned long dst,
251 u64 start, size_t size)
252{
253 struct vmcoredd_node *dump;
254 u64 offset = 0;
255 int ret = 0;
256 size_t tsz;
257 char *buf;
258
259 mutex_lock(&vmcoredd_mutex);
260 list_for_each_entry(dump, &vmcoredd_list, list) {
261 if (start < offset + dump->size) {
262 tsz = min(offset + (u64)dump->size - start, (u64)size);
263 buf = dump->buf + start - offset;
264 if (remap_vmalloc_range_partial(vma, dst, buf, tsz)) {
265 ret = -EFAULT;
266 goto out_unlock;
267 }
268
269 size -= tsz;
270 start += tsz;
271 dst += tsz;
272
273 /* Leave now if buffer filled already */
274 if (!size)
275 goto out_unlock;
276 }
277 offset += dump->size;
278 }
279
280out_unlock:
281 mutex_unlock(&vmcoredd_mutex);
282 return ret;
283}
Arnd Bergmanna2036a12018-08-23 17:00:55 -0700284#endif /* CONFIG_MMU */
Rahul Lakkireddy7efe48d2018-05-02 15:17:18 +0530285#endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
286
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700287/* Read from the ELF header and then the crash dump. On error, negative value is
288 * returned otherwise number of bytes read are returned.
289 */
Michael Holzheu9cb21812013-09-11 14:24:51 -0700290static ssize_t __read_vmcore(char *buffer, size_t buflen, loff_t *fpos,
291 int userbuf)
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700292{
293 ssize_t acc = 0, tmp;
Vivek Goyal80e8ff62006-04-10 22:54:10 -0700294 size_t tsz;
HATAYAMA Daisukeb27eb182013-07-03 15:02:13 -0700295 u64 start;
296 struct vmcore *m = NULL;
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700297
298 if (buflen == 0 || *fpos >= vmcore_size)
299 return 0;
300
301 /* trim buflen to not go beyond EOF */
302 if (buflen > vmcore_size - *fpos)
303 buflen = vmcore_size - *fpos;
304
305 /* Read ELF core header */
306 if (*fpos < elfcorebuf_sz) {
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700307 tsz = min(elfcorebuf_sz - (size_t)*fpos, buflen);
Michael Holzheu9cb21812013-09-11 14:24:51 -0700308 if (copy_to(buffer, elfcorebuf + *fpos, tsz, userbuf))
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700309 return -EFAULT;
310 buflen -= tsz;
311 *fpos += tsz;
312 buffer += tsz;
313 acc += tsz;
314
315 /* leave now if filled buffer already */
316 if (buflen == 0)
317 return acc;
318 }
319
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700320 /* Read Elf note segment */
321 if (*fpos < elfcorebuf_sz + elfnotes_sz) {
322 void *kaddr;
323
Rahul Lakkireddy7efe48d2018-05-02 15:17:18 +0530324 /* We add device dumps before other elf notes because the
325 * other elf notes may not fill the elf notes buffer
326 * completely and we will end up with zero-filled data
327 * between the elf notes and the device dumps. Tools will
328 * then try to decode this zero-filled data as valid notes
329 * and we don't want that. Hence, adding device dumps before
330 * the other elf notes ensure that zero-filled data can be
331 * avoided.
332 */
333#ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
334 /* Read device dumps */
335 if (*fpos < elfcorebuf_sz + vmcoredd_orig_sz) {
336 tsz = min(elfcorebuf_sz + vmcoredd_orig_sz -
337 (size_t)*fpos, buflen);
338 start = *fpos - elfcorebuf_sz;
339 if (vmcoredd_copy_dumps(buffer, start, tsz, userbuf))
340 return -EFAULT;
341
342 buflen -= tsz;
343 *fpos += tsz;
344 buffer += tsz;
345 acc += tsz;
346
347 /* leave now if filled buffer already */
348 if (!buflen)
349 return acc;
350 }
351#endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
352
353 /* Read remaining elf notes */
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700354 tsz = min(elfcorebuf_sz + elfnotes_sz - (size_t)*fpos, buflen);
Rahul Lakkireddy7efe48d2018-05-02 15:17:18 +0530355 kaddr = elfnotes_buf + *fpos - elfcorebuf_sz - vmcoredd_orig_sz;
Michael Holzheu9cb21812013-09-11 14:24:51 -0700356 if (copy_to(buffer, kaddr, tsz, userbuf))
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700357 return -EFAULT;
Rahul Lakkireddy7efe48d2018-05-02 15:17:18 +0530358
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700359 buflen -= tsz;
360 *fpos += tsz;
361 buffer += tsz;
362 acc += tsz;
363
364 /* leave now if filled buffer already */
365 if (buflen == 0)
366 return acc;
367 }
368
HATAYAMA Daisukeb27eb182013-07-03 15:02:13 -0700369 list_for_each_entry(m, &vmcore_list, list) {
370 if (*fpos < m->offset + m->size) {
Dave Young0b50a2d2016-03-17 14:21:03 -0700371 tsz = (size_t)min_t(unsigned long long,
372 m->offset + m->size - *fpos,
373 buflen);
HATAYAMA Daisukeb27eb182013-07-03 15:02:13 -0700374 start = m->paddr + *fpos - m->offset;
Lianbo Jiang992b6492018-09-30 16:37:41 +0800375 tmp = read_from_oldmem(buffer, tsz, &start,
376 userbuf, sme_active());
HATAYAMA Daisukeb27eb182013-07-03 15:02:13 -0700377 if (tmp < 0)
378 return tmp;
379 buflen -= tsz;
380 *fpos += tsz;
381 buffer += tsz;
382 acc += tsz;
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700383
HATAYAMA Daisukeb27eb182013-07-03 15:02:13 -0700384 /* leave now if filled buffer already */
385 if (buflen == 0)
386 return acc;
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700387 }
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700388 }
HATAYAMA Daisukeb27eb182013-07-03 15:02:13 -0700389
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700390 return acc;
391}
392
Michael Holzheu9cb21812013-09-11 14:24:51 -0700393static ssize_t read_vmcore(struct file *file, char __user *buffer,
394 size_t buflen, loff_t *fpos)
395{
396 return __read_vmcore((__force char *) buffer, buflen, fpos, 1);
397}
398
399/*
400 * The vmcore fault handler uses the page cache and fills data using the
401 * standard __vmcore_read() function.
402 *
403 * On s390 the fault handler is used for memory regions that can't be mapped
404 * directly with remap_pfn_range().
405 */
Souptick Joarder36f06202018-08-21 21:54:44 -0700406static vm_fault_t mmap_vmcore_fault(struct vm_fault *vmf)
Michael Holzheu9cb21812013-09-11 14:24:51 -0700407{
408#ifdef CONFIG_S390
Dave Jiang11bac802017-02-24 14:56:41 -0800409 struct address_space *mapping = vmf->vma->vm_file->f_mapping;
Michael Holzheu9cb21812013-09-11 14:24:51 -0700410 pgoff_t index = vmf->pgoff;
411 struct page *page;
412 loff_t offset;
413 char *buf;
414 int rc;
415
416 page = find_or_create_page(mapping, index, GFP_KERNEL);
417 if (!page)
418 return VM_FAULT_OOM;
419 if (!PageUptodate(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300420 offset = (loff_t) index << PAGE_SHIFT;
Michael Holzheu9cb21812013-09-11 14:24:51 -0700421 buf = __va((page_to_pfn(page) << PAGE_SHIFT));
422 rc = __read_vmcore(buf, PAGE_SIZE, &offset, 0);
423 if (rc < 0) {
424 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300425 put_page(page);
Souptick Joarderb5c21232018-10-30 15:04:35 -0700426 return vmf_error(rc);
Michael Holzheu9cb21812013-09-11 14:24:51 -0700427 }
428 SetPageUptodate(page);
429 }
430 unlock_page(page);
431 vmf->page = page;
432 return 0;
433#else
434 return VM_FAULT_SIGBUS;
435#endif
436}
437
438static const struct vm_operations_struct vmcore_mmap_ops = {
439 .fault = mmap_vmcore_fault,
440};
441
HATAYAMA Daisuke83086972013-07-03 15:02:23 -0700442/**
Rahul Lakkireddy27242732018-05-02 15:17:17 +0530443 * vmcore_alloc_buf - allocate buffer in vmalloc memory
444 * @sizez: size of buffer
HATAYAMA Daisuke83086972013-07-03 15:02:23 -0700445 *
446 * If CONFIG_MMU is defined, use vmalloc_user() to allow users to mmap
447 * the buffer to user-space by means of remap_vmalloc_range().
448 *
449 * If CONFIG_MMU is not defined, use vzalloc() since mmap_vmcore() is
450 * disabled and there's no need to allow users to mmap the buffer.
451 */
Rahul Lakkireddy27242732018-05-02 15:17:17 +0530452static inline char *vmcore_alloc_buf(size_t size)
HATAYAMA Daisuke83086972013-07-03 15:02:23 -0700453{
454#ifdef CONFIG_MMU
Rahul Lakkireddy27242732018-05-02 15:17:17 +0530455 return vmalloc_user(size);
HATAYAMA Daisuke83086972013-07-03 15:02:23 -0700456#else
Rahul Lakkireddy27242732018-05-02 15:17:17 +0530457 return vzalloc(size);
HATAYAMA Daisuke83086972013-07-03 15:02:23 -0700458#endif
459}
460
461/*
462 * Disable mmap_vmcore() if CONFIG_MMU is not defined. MMU is
463 * essential for mmap_vmcore() in order to map physically
464 * non-contiguous objects (ELF header, ELF note segment and memory
465 * regions in the 1st kernel pointed to by PT_LOAD entries) into
466 * virtually contiguous user-space in ELF layout.
467 */
Michael Holzheu11e376a2013-09-11 14:24:53 -0700468#ifdef CONFIG_MMU
Vitaly Kuznetsov0692ded2014-08-08 14:22:05 -0700469/*
470 * remap_oldmem_pfn_checked - do remap_oldmem_pfn_range replacing all pages
471 * reported as not being ram with the zero page.
472 *
473 * @vma: vm_area_struct describing requested mapping
474 * @from: start remapping from
475 * @pfn: page frame number to start remapping to
476 * @size: remapping size
477 * @prot: protection bits
478 *
479 * Returns zero on success, -EAGAIN on failure.
480 */
481static int remap_oldmem_pfn_checked(struct vm_area_struct *vma,
482 unsigned long from, unsigned long pfn,
483 unsigned long size, pgprot_t prot)
484{
485 unsigned long map_size;
486 unsigned long pos_start, pos_end, pos;
487 unsigned long zeropage_pfn = my_zero_pfn(0);
488 size_t len = 0;
489
490 pos_start = pfn;
491 pos_end = pfn + (size >> PAGE_SHIFT);
492
493 for (pos = pos_start; pos < pos_end; ++pos) {
494 if (!pfn_is_ram(pos)) {
495 /*
496 * We hit a page which is not ram. Remap the continuous
497 * region between pos_start and pos-1 and replace
498 * the non-ram page at pos with the zero page.
499 */
500 if (pos > pos_start) {
501 /* Remap continuous region */
502 map_size = (pos - pos_start) << PAGE_SHIFT;
503 if (remap_oldmem_pfn_range(vma, from + len,
504 pos_start, map_size,
505 prot))
506 goto fail;
507 len += map_size;
508 }
509 /* Remap the zero page */
510 if (remap_oldmem_pfn_range(vma, from + len,
511 zeropage_pfn,
512 PAGE_SIZE, prot))
513 goto fail;
514 len += PAGE_SIZE;
515 pos_start = pos + 1;
516 }
517 }
518 if (pos > pos_start) {
519 /* Remap the rest */
520 map_size = (pos - pos_start) << PAGE_SHIFT;
521 if (remap_oldmem_pfn_range(vma, from + len, pos_start,
522 map_size, prot))
523 goto fail;
524 }
525 return 0;
526fail:
Mike Rapoport897ab3e2017-02-24 14:58:22 -0800527 do_munmap(vma->vm_mm, from, len, NULL);
Vitaly Kuznetsov0692ded2014-08-08 14:22:05 -0700528 return -EAGAIN;
529}
530
531static int vmcore_remap_oldmem_pfn(struct vm_area_struct *vma,
532 unsigned long from, unsigned long pfn,
533 unsigned long size, pgprot_t prot)
534{
535 /*
536 * Check if oldmem_pfn_is_ram was registered to avoid
537 * looping over all pages without a reason.
538 */
539 if (oldmem_pfn_is_ram)
540 return remap_oldmem_pfn_checked(vma, from, pfn, size, prot);
541 else
542 return remap_oldmem_pfn_range(vma, from, pfn, size, prot);
543}
544
HATAYAMA Daisuke83086972013-07-03 15:02:23 -0700545static int mmap_vmcore(struct file *file, struct vm_area_struct *vma)
546{
547 size_t size = vma->vm_end - vma->vm_start;
548 u64 start, end, len, tsz;
549 struct vmcore *m;
550
551 start = (u64)vma->vm_pgoff << PAGE_SHIFT;
552 end = start + size;
553
554 if (size > vmcore_size || end > vmcore_size)
555 return -EINVAL;
556
557 if (vma->vm_flags & (VM_WRITE | VM_EXEC))
558 return -EPERM;
559
560 vma->vm_flags &= ~(VM_MAYWRITE | VM_MAYEXEC);
561 vma->vm_flags |= VM_MIXEDMAP;
Michael Holzheu9cb21812013-09-11 14:24:51 -0700562 vma->vm_ops = &vmcore_mmap_ops;
HATAYAMA Daisuke83086972013-07-03 15:02:23 -0700563
564 len = 0;
565
566 if (start < elfcorebuf_sz) {
567 u64 pfn;
568
569 tsz = min(elfcorebuf_sz - (size_t)start, size);
570 pfn = __pa(elfcorebuf + start) >> PAGE_SHIFT;
571 if (remap_pfn_range(vma, vma->vm_start, pfn, tsz,
572 vma->vm_page_prot))
573 return -EAGAIN;
574 size -= tsz;
575 start += tsz;
576 len += tsz;
577
578 if (size == 0)
579 return 0;
580 }
581
582 if (start < elfcorebuf_sz + elfnotes_sz) {
583 void *kaddr;
584
Rahul Lakkireddy7efe48d2018-05-02 15:17:18 +0530585 /* We add device dumps before other elf notes because the
586 * other elf notes may not fill the elf notes buffer
587 * completely and we will end up with zero-filled data
588 * between the elf notes and the device dumps. Tools will
589 * then try to decode this zero-filled data as valid notes
590 * and we don't want that. Hence, adding device dumps before
591 * the other elf notes ensure that zero-filled data can be
592 * avoided. This also ensures that the device dumps and
593 * other elf notes can be properly mmaped at page aligned
594 * address.
595 */
596#ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
597 /* Read device dumps */
598 if (start < elfcorebuf_sz + vmcoredd_orig_sz) {
599 u64 start_off;
600
601 tsz = min(elfcorebuf_sz + vmcoredd_orig_sz -
602 (size_t)start, size);
603 start_off = start - elfcorebuf_sz;
604 if (vmcoredd_mmap_dumps(vma, vma->vm_start + len,
605 start_off, tsz))
606 goto fail;
607
608 size -= tsz;
609 start += tsz;
610 len += tsz;
611
612 /* leave now if filled buffer already */
613 if (!size)
614 return 0;
615 }
616#endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
617
618 /* Read remaining elf notes */
HATAYAMA Daisuke83086972013-07-03 15:02:23 -0700619 tsz = min(elfcorebuf_sz + elfnotes_sz - (size_t)start, size);
Rahul Lakkireddy7efe48d2018-05-02 15:17:18 +0530620 kaddr = elfnotes_buf + start - elfcorebuf_sz - vmcoredd_orig_sz;
HATAYAMA Daisuke83086972013-07-03 15:02:23 -0700621 if (remap_vmalloc_range_partial(vma, vma->vm_start + len,
622 kaddr, tsz))
623 goto fail;
Rahul Lakkireddy7efe48d2018-05-02 15:17:18 +0530624
HATAYAMA Daisuke83086972013-07-03 15:02:23 -0700625 size -= tsz;
626 start += tsz;
627 len += tsz;
628
629 if (size == 0)
630 return 0;
631 }
632
633 list_for_each_entry(m, &vmcore_list, list) {
634 if (start < m->offset + m->size) {
635 u64 paddr = 0;
636
Dave Young0b50a2d2016-03-17 14:21:03 -0700637 tsz = (size_t)min_t(unsigned long long,
638 m->offset + m->size - start, size);
HATAYAMA Daisuke83086972013-07-03 15:02:23 -0700639 paddr = m->paddr + start - m->offset;
Vitaly Kuznetsov0692ded2014-08-08 14:22:05 -0700640 if (vmcore_remap_oldmem_pfn(vma, vma->vm_start + len,
641 paddr >> PAGE_SHIFT, tsz,
642 vma->vm_page_prot))
HATAYAMA Daisuke83086972013-07-03 15:02:23 -0700643 goto fail;
644 size -= tsz;
645 start += tsz;
646 len += tsz;
647
648 if (size == 0)
649 return 0;
650 }
651 }
652
653 return 0;
654fail:
Mike Rapoport897ab3e2017-02-24 14:58:22 -0800655 do_munmap(vma->vm_mm, vma->vm_start, len, NULL);
HATAYAMA Daisuke83086972013-07-03 15:02:23 -0700656 return -EAGAIN;
657}
658#else
659static int mmap_vmcore(struct file *file, struct vm_area_struct *vma)
660{
661 return -ENOSYS;
662}
663#endif
664
Alexey Dobriyan5aa140c2008-10-06 14:36:31 +0400665static const struct file_operations proc_vmcore_operations = {
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700666 .read = read_vmcore,
Arnd Bergmannc227e692010-09-22 13:04:54 -0700667 .llseek = default_llseek,
HATAYAMA Daisuke83086972013-07-03 15:02:23 -0700668 .mmap = mmap_vmcore,
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700669};
670
671static struct vmcore* __init get_new_element(void)
672{
Cyrill Gorcunov2f6d3112009-06-17 16:26:00 -0700673 return kzalloc(sizeof(struct vmcore), GFP_KERNEL);
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700674}
675
Rahul Lakkireddy44c752f2018-05-21 19:07:50 +0530676static u64 get_vmcore_size(size_t elfsz, size_t elfnotesegsz,
677 struct list_head *vc_list)
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700678{
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700679 u64 size;
HATAYAMA Daisuke591ff712013-07-03 15:02:22 -0700680 struct vmcore *m;
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700681
HATAYAMA Daisuke591ff712013-07-03 15:02:22 -0700682 size = elfsz + elfnotesegsz;
683 list_for_each_entry(m, vc_list, list) {
684 size += m->size;
Vivek Goyal72658e92005-06-25 14:58:22 -0700685 }
686 return size;
687}
688
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700689/**
690 * update_note_header_size_elf64 - update p_memsz member of each PT_NOTE entry
691 *
692 * @ehdr_ptr: ELF header
693 *
694 * This function updates p_memsz member of each PT_NOTE entry in the
695 * program header table pointed to by @ehdr_ptr to real size of ELF
696 * note segment.
697 */
698static int __init update_note_header_size_elf64(const Elf64_Ehdr *ehdr_ptr)
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700699{
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700700 int i, rc=0;
701 Elf64_Phdr *phdr_ptr;
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700702 Elf64_Nhdr *nhdr_ptr;
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700703
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700704 phdr_ptr = (Elf64_Phdr *)(ehdr_ptr + 1);
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700705 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700706 void *notes_section;
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700707 u64 offset, max_sz, sz, real_sz = 0;
708 if (phdr_ptr->p_type != PT_NOTE)
709 continue;
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700710 max_sz = phdr_ptr->p_memsz;
711 offset = phdr_ptr->p_offset;
712 notes_section = kmalloc(max_sz, GFP_KERNEL);
713 if (!notes_section)
714 return -ENOMEM;
Michael Holzheube8a8d02013-09-11 14:24:49 -0700715 rc = elfcorehdr_read_notes(notes_section, max_sz, &offset);
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700716 if (rc < 0) {
717 kfree(notes_section);
718 return rc;
719 }
720 nhdr_ptr = notes_section;
Greg Pearson38dfac82014-02-10 14:25:36 -0800721 while (nhdr_ptr->n_namesz != 0) {
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700722 sz = sizeof(Elf64_Nhdr) +
WANG Chao34b47762015-02-17 13:46:01 -0800723 (((u64)nhdr_ptr->n_namesz + 3) & ~3) +
724 (((u64)nhdr_ptr->n_descsz + 3) & ~3);
Greg Pearson38dfac82014-02-10 14:25:36 -0800725 if ((real_sz + sz) > max_sz) {
726 pr_warn("Warning: Exceeded p_memsz, dropping PT_NOTE entry n_namesz=0x%x, n_descsz=0x%x\n",
727 nhdr_ptr->n_namesz, nhdr_ptr->n_descsz);
728 break;
729 }
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700730 real_sz += sz;
731 nhdr_ptr = (Elf64_Nhdr*)((char*)nhdr_ptr + sz);
732 }
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700733 kfree(notes_section);
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700734 phdr_ptr->p_memsz = real_sz;
Greg Pearson38dfac82014-02-10 14:25:36 -0800735 if (real_sz == 0) {
736 pr_warn("Warning: Zero PT_NOTE entries found\n");
Greg Pearson38dfac82014-02-10 14:25:36 -0800737 }
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700738 }
739
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700740 return 0;
741}
742
743/**
744 * get_note_number_and_size_elf64 - get the number of PT_NOTE program
745 * headers and sum of real size of their ELF note segment headers and
746 * data.
747 *
748 * @ehdr_ptr: ELF header
749 * @nr_ptnote: buffer for the number of PT_NOTE program headers
750 * @sz_ptnote: buffer for size of unique PT_NOTE program header
751 *
752 * This function is used to merge multiple PT_NOTE program headers
753 * into a unique single one. The resulting unique entry will have
754 * @sz_ptnote in its phdr->p_mem.
755 *
756 * It is assumed that program headers with PT_NOTE type pointed to by
757 * @ehdr_ptr has already been updated by update_note_header_size_elf64
758 * and each of PT_NOTE program headers has actual ELF note segment
759 * size in its p_memsz member.
760 */
761static int __init get_note_number_and_size_elf64(const Elf64_Ehdr *ehdr_ptr,
762 int *nr_ptnote, u64 *sz_ptnote)
763{
764 int i;
765 Elf64_Phdr *phdr_ptr;
766
767 *nr_ptnote = *sz_ptnote = 0;
768
769 phdr_ptr = (Elf64_Phdr *)(ehdr_ptr + 1);
770 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
771 if (phdr_ptr->p_type != PT_NOTE)
772 continue;
773 *nr_ptnote += 1;
774 *sz_ptnote += phdr_ptr->p_memsz;
775 }
776
777 return 0;
778}
779
780/**
781 * copy_notes_elf64 - copy ELF note segments in a given buffer
782 *
783 * @ehdr_ptr: ELF header
784 * @notes_buf: buffer into which ELF note segments are copied
785 *
786 * This function is used to copy ELF note segment in the 1st kernel
787 * into the buffer @notes_buf in the 2nd kernel. It is assumed that
788 * size of the buffer @notes_buf is equal to or larger than sum of the
789 * real ELF note segment headers and data.
790 *
791 * It is assumed that program headers with PT_NOTE type pointed to by
792 * @ehdr_ptr has already been updated by update_note_header_size_elf64
793 * and each of PT_NOTE program headers has actual ELF note segment
794 * size in its p_memsz member.
795 */
796static int __init copy_notes_elf64(const Elf64_Ehdr *ehdr_ptr, char *notes_buf)
797{
798 int i, rc=0;
799 Elf64_Phdr *phdr_ptr;
800
801 phdr_ptr = (Elf64_Phdr*)(ehdr_ptr + 1);
802
803 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
804 u64 offset;
805 if (phdr_ptr->p_type != PT_NOTE)
806 continue;
807 offset = phdr_ptr->p_offset;
Michael Holzheube8a8d02013-09-11 14:24:49 -0700808 rc = elfcorehdr_read_notes(notes_buf, phdr_ptr->p_memsz,
809 &offset);
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700810 if (rc < 0)
811 return rc;
812 notes_buf += phdr_ptr->p_memsz;
813 }
814
815 return 0;
816}
817
818/* Merges all the PT_NOTE headers into one. */
819static int __init merge_note_headers_elf64(char *elfptr, size_t *elfsz,
820 char **notes_buf, size_t *notes_sz)
821{
822 int i, nr_ptnote=0, rc=0;
823 char *tmp;
824 Elf64_Ehdr *ehdr_ptr;
825 Elf64_Phdr phdr;
826 u64 phdr_sz = 0, note_off;
827
828 ehdr_ptr = (Elf64_Ehdr *)elfptr;
829
830 rc = update_note_header_size_elf64(ehdr_ptr);
831 if (rc < 0)
832 return rc;
833
834 rc = get_note_number_and_size_elf64(ehdr_ptr, &nr_ptnote, &phdr_sz);
835 if (rc < 0)
836 return rc;
837
838 *notes_sz = roundup(phdr_sz, PAGE_SIZE);
Rahul Lakkireddy27242732018-05-02 15:17:17 +0530839 *notes_buf = vmcore_alloc_buf(*notes_sz);
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700840 if (!*notes_buf)
841 return -ENOMEM;
842
843 rc = copy_notes_elf64(ehdr_ptr, *notes_buf);
844 if (rc < 0)
845 return rc;
846
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700847 /* Prepare merged PT_NOTE program header. */
848 phdr.p_type = PT_NOTE;
849 phdr.p_flags = 0;
850 note_off = sizeof(Elf64_Ehdr) +
851 (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf64_Phdr);
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700852 phdr.p_offset = roundup(note_off, PAGE_SIZE);
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700853 phdr.p_vaddr = phdr.p_paddr = 0;
854 phdr.p_filesz = phdr.p_memsz = phdr_sz;
855 phdr.p_align = 0;
856
857 /* Add merged PT_NOTE program header*/
858 tmp = elfptr + sizeof(Elf64_Ehdr);
859 memcpy(tmp, &phdr, sizeof(phdr));
860 tmp += sizeof(phdr);
861
862 /* Remove unwanted PT_NOTE program headers. */
863 i = (nr_ptnote - 1) * sizeof(Elf64_Phdr);
864 *elfsz = *elfsz - i;
865 memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf64_Ehdr)-sizeof(Elf64_Phdr)));
HATAYAMA Daisukef2bdacd2013-07-03 15:02:14 -0700866 memset(elfptr + *elfsz, 0, i);
867 *elfsz = roundup(*elfsz, PAGE_SIZE);
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700868
869 /* Modify e_phnum to reflect merged headers. */
870 ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
871
Rahul Lakkireddy7efe48d2018-05-02 15:17:18 +0530872 /* Store the size of all notes. We need this to update the note
873 * header when the device dumps will be added.
874 */
875 elfnotes_orig_sz = phdr.p_memsz;
876
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700877 return 0;
878}
879
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700880/**
881 * update_note_header_size_elf32 - update p_memsz member of each PT_NOTE entry
882 *
883 * @ehdr_ptr: ELF header
884 *
885 * This function updates p_memsz member of each PT_NOTE entry in the
886 * program header table pointed to by @ehdr_ptr to real size of ELF
887 * note segment.
888 */
889static int __init update_note_header_size_elf32(const Elf32_Ehdr *ehdr_ptr)
Vivek Goyal72658e92005-06-25 14:58:22 -0700890{
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700891 int i, rc=0;
892 Elf32_Phdr *phdr_ptr;
Vivek Goyal72658e92005-06-25 14:58:22 -0700893 Elf32_Nhdr *nhdr_ptr;
Vivek Goyal72658e92005-06-25 14:58:22 -0700894
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700895 phdr_ptr = (Elf32_Phdr *)(ehdr_ptr + 1);
Vivek Goyal72658e92005-06-25 14:58:22 -0700896 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
Vivek Goyal72658e92005-06-25 14:58:22 -0700897 void *notes_section;
Vivek Goyal72658e92005-06-25 14:58:22 -0700898 u64 offset, max_sz, sz, real_sz = 0;
899 if (phdr_ptr->p_type != PT_NOTE)
900 continue;
Vivek Goyal72658e92005-06-25 14:58:22 -0700901 max_sz = phdr_ptr->p_memsz;
902 offset = phdr_ptr->p_offset;
903 notes_section = kmalloc(max_sz, GFP_KERNEL);
904 if (!notes_section)
905 return -ENOMEM;
Michael Holzheube8a8d02013-09-11 14:24:49 -0700906 rc = elfcorehdr_read_notes(notes_section, max_sz, &offset);
Vivek Goyal72658e92005-06-25 14:58:22 -0700907 if (rc < 0) {
908 kfree(notes_section);
909 return rc;
910 }
911 nhdr_ptr = notes_section;
Greg Pearson38dfac82014-02-10 14:25:36 -0800912 while (nhdr_ptr->n_namesz != 0) {
Vivek Goyal72658e92005-06-25 14:58:22 -0700913 sz = sizeof(Elf32_Nhdr) +
WANG Chao34b47762015-02-17 13:46:01 -0800914 (((u64)nhdr_ptr->n_namesz + 3) & ~3) +
915 (((u64)nhdr_ptr->n_descsz + 3) & ~3);
Greg Pearson38dfac82014-02-10 14:25:36 -0800916 if ((real_sz + sz) > max_sz) {
917 pr_warn("Warning: Exceeded p_memsz, dropping PT_NOTE entry n_namesz=0x%x, n_descsz=0x%x\n",
918 nhdr_ptr->n_namesz, nhdr_ptr->n_descsz);
919 break;
920 }
Vivek Goyal72658e92005-06-25 14:58:22 -0700921 real_sz += sz;
922 nhdr_ptr = (Elf32_Nhdr*)((char*)nhdr_ptr + sz);
923 }
Vivek Goyal72658e92005-06-25 14:58:22 -0700924 kfree(notes_section);
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700925 phdr_ptr->p_memsz = real_sz;
Greg Pearson38dfac82014-02-10 14:25:36 -0800926 if (real_sz == 0) {
927 pr_warn("Warning: Zero PT_NOTE entries found\n");
Greg Pearson38dfac82014-02-10 14:25:36 -0800928 }
Vivek Goyal72658e92005-06-25 14:58:22 -0700929 }
930
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700931 return 0;
932}
933
934/**
935 * get_note_number_and_size_elf32 - get the number of PT_NOTE program
936 * headers and sum of real size of their ELF note segment headers and
937 * data.
938 *
939 * @ehdr_ptr: ELF header
940 * @nr_ptnote: buffer for the number of PT_NOTE program headers
941 * @sz_ptnote: buffer for size of unique PT_NOTE program header
942 *
943 * This function is used to merge multiple PT_NOTE program headers
944 * into a unique single one. The resulting unique entry will have
945 * @sz_ptnote in its phdr->p_mem.
946 *
947 * It is assumed that program headers with PT_NOTE type pointed to by
948 * @ehdr_ptr has already been updated by update_note_header_size_elf32
949 * and each of PT_NOTE program headers has actual ELF note segment
950 * size in its p_memsz member.
951 */
952static int __init get_note_number_and_size_elf32(const Elf32_Ehdr *ehdr_ptr,
953 int *nr_ptnote, u64 *sz_ptnote)
954{
955 int i;
956 Elf32_Phdr *phdr_ptr;
957
958 *nr_ptnote = *sz_ptnote = 0;
959
960 phdr_ptr = (Elf32_Phdr *)(ehdr_ptr + 1);
961 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
962 if (phdr_ptr->p_type != PT_NOTE)
963 continue;
964 *nr_ptnote += 1;
965 *sz_ptnote += phdr_ptr->p_memsz;
966 }
967
968 return 0;
969}
970
971/**
972 * copy_notes_elf32 - copy ELF note segments in a given buffer
973 *
974 * @ehdr_ptr: ELF header
975 * @notes_buf: buffer into which ELF note segments are copied
976 *
977 * This function is used to copy ELF note segment in the 1st kernel
978 * into the buffer @notes_buf in the 2nd kernel. It is assumed that
979 * size of the buffer @notes_buf is equal to or larger than sum of the
980 * real ELF note segment headers and data.
981 *
982 * It is assumed that program headers with PT_NOTE type pointed to by
983 * @ehdr_ptr has already been updated by update_note_header_size_elf32
984 * and each of PT_NOTE program headers has actual ELF note segment
985 * size in its p_memsz member.
986 */
987static int __init copy_notes_elf32(const Elf32_Ehdr *ehdr_ptr, char *notes_buf)
988{
989 int i, rc=0;
990 Elf32_Phdr *phdr_ptr;
991
992 phdr_ptr = (Elf32_Phdr*)(ehdr_ptr + 1);
993
994 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
995 u64 offset;
996 if (phdr_ptr->p_type != PT_NOTE)
997 continue;
998 offset = phdr_ptr->p_offset;
Michael Holzheube8a8d02013-09-11 14:24:49 -0700999 rc = elfcorehdr_read_notes(notes_buf, phdr_ptr->p_memsz,
1000 &offset);
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -07001001 if (rc < 0)
1002 return rc;
1003 notes_buf += phdr_ptr->p_memsz;
1004 }
1005
1006 return 0;
1007}
1008
1009/* Merges all the PT_NOTE headers into one. */
1010static int __init merge_note_headers_elf32(char *elfptr, size_t *elfsz,
1011 char **notes_buf, size_t *notes_sz)
1012{
1013 int i, nr_ptnote=0, rc=0;
1014 char *tmp;
1015 Elf32_Ehdr *ehdr_ptr;
1016 Elf32_Phdr phdr;
1017 u64 phdr_sz = 0, note_off;
1018
1019 ehdr_ptr = (Elf32_Ehdr *)elfptr;
1020
1021 rc = update_note_header_size_elf32(ehdr_ptr);
1022 if (rc < 0)
1023 return rc;
1024
1025 rc = get_note_number_and_size_elf32(ehdr_ptr, &nr_ptnote, &phdr_sz);
1026 if (rc < 0)
1027 return rc;
1028
1029 *notes_sz = roundup(phdr_sz, PAGE_SIZE);
Rahul Lakkireddy27242732018-05-02 15:17:17 +05301030 *notes_buf = vmcore_alloc_buf(*notes_sz);
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -07001031 if (!*notes_buf)
1032 return -ENOMEM;
1033
1034 rc = copy_notes_elf32(ehdr_ptr, *notes_buf);
1035 if (rc < 0)
1036 return rc;
1037
Vivek Goyal72658e92005-06-25 14:58:22 -07001038 /* Prepare merged PT_NOTE program header. */
1039 phdr.p_type = PT_NOTE;
1040 phdr.p_flags = 0;
1041 note_off = sizeof(Elf32_Ehdr) +
1042 (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf32_Phdr);
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -07001043 phdr.p_offset = roundup(note_off, PAGE_SIZE);
Vivek Goyal72658e92005-06-25 14:58:22 -07001044 phdr.p_vaddr = phdr.p_paddr = 0;
1045 phdr.p_filesz = phdr.p_memsz = phdr_sz;
1046 phdr.p_align = 0;
1047
1048 /* Add merged PT_NOTE program header*/
1049 tmp = elfptr + sizeof(Elf32_Ehdr);
1050 memcpy(tmp, &phdr, sizeof(phdr));
1051 tmp += sizeof(phdr);
1052
1053 /* Remove unwanted PT_NOTE program headers. */
1054 i = (nr_ptnote - 1) * sizeof(Elf32_Phdr);
1055 *elfsz = *elfsz - i;
1056 memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf32_Ehdr)-sizeof(Elf32_Phdr)));
HATAYAMA Daisukef2bdacd2013-07-03 15:02:14 -07001057 memset(elfptr + *elfsz, 0, i);
1058 *elfsz = roundup(*elfsz, PAGE_SIZE);
Vivek Goyal72658e92005-06-25 14:58:22 -07001059
1060 /* Modify e_phnum to reflect merged headers. */
1061 ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
1062
Rahul Lakkireddy7efe48d2018-05-02 15:17:18 +05301063 /* Store the size of all notes. We need this to update the note
1064 * header when the device dumps will be added.
1065 */
1066 elfnotes_orig_sz = phdr.p_memsz;
1067
Vivek Goyal72658e92005-06-25 14:58:22 -07001068 return 0;
1069}
1070
Vivek Goyal666bfdd2005-06-25 14:58:21 -07001071/* Add memory chunks represented by program headers to vmcore list. Also update
1072 * the new offset fields of exported program headers. */
1073static int __init process_ptload_program_headers_elf64(char *elfptr,
1074 size_t elfsz,
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -07001075 size_t elfnotes_sz,
Vivek Goyal666bfdd2005-06-25 14:58:21 -07001076 struct list_head *vc_list)
1077{
1078 int i;
1079 Elf64_Ehdr *ehdr_ptr;
1080 Elf64_Phdr *phdr_ptr;
1081 loff_t vmcore_off;
1082 struct vmcore *new;
1083
1084 ehdr_ptr = (Elf64_Ehdr *)elfptr;
1085 phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr)); /* PT_NOTE hdr */
1086
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -07001087 /* Skip Elf header, program headers and Elf note segment. */
1088 vmcore_off = elfsz + elfnotes_sz;
Vivek Goyal666bfdd2005-06-25 14:58:21 -07001089
1090 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
HATAYAMA Daisuke7f614cd2013-07-03 15:02:15 -07001091 u64 paddr, start, end, size;
1092
Vivek Goyal666bfdd2005-06-25 14:58:21 -07001093 if (phdr_ptr->p_type != PT_LOAD)
1094 continue;
1095
HATAYAMA Daisuke7f614cd2013-07-03 15:02:15 -07001096 paddr = phdr_ptr->p_offset;
1097 start = rounddown(paddr, PAGE_SIZE);
1098 end = roundup(paddr + phdr_ptr->p_memsz, PAGE_SIZE);
1099 size = end - start;
1100
Vivek Goyal666bfdd2005-06-25 14:58:21 -07001101 /* Add this contiguous chunk of memory to vmcore list.*/
1102 new = get_new_element();
1103 if (!new)
1104 return -ENOMEM;
HATAYAMA Daisuke7f614cd2013-07-03 15:02:15 -07001105 new->paddr = start;
1106 new->size = size;
Vivek Goyal666bfdd2005-06-25 14:58:21 -07001107 list_add_tail(&new->list, vc_list);
1108
1109 /* Update the program header offset. */
HATAYAMA Daisuke7f614cd2013-07-03 15:02:15 -07001110 phdr_ptr->p_offset = vmcore_off + (paddr - start);
1111 vmcore_off = vmcore_off + size;
Vivek Goyal666bfdd2005-06-25 14:58:21 -07001112 }
1113 return 0;
1114}
1115
Vivek Goyal72658e92005-06-25 14:58:22 -07001116static int __init process_ptload_program_headers_elf32(char *elfptr,
1117 size_t elfsz,
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -07001118 size_t elfnotes_sz,
Vivek Goyal72658e92005-06-25 14:58:22 -07001119 struct list_head *vc_list)
1120{
1121 int i;
1122 Elf32_Ehdr *ehdr_ptr;
1123 Elf32_Phdr *phdr_ptr;
1124 loff_t vmcore_off;
1125 struct vmcore *new;
1126
1127 ehdr_ptr = (Elf32_Ehdr *)elfptr;
1128 phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr)); /* PT_NOTE hdr */
1129
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -07001130 /* Skip Elf header, program headers and Elf note segment. */
1131 vmcore_off = elfsz + elfnotes_sz;
Vivek Goyal72658e92005-06-25 14:58:22 -07001132
1133 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
HATAYAMA Daisuke7f614cd2013-07-03 15:02:15 -07001134 u64 paddr, start, end, size;
1135
Vivek Goyal72658e92005-06-25 14:58:22 -07001136 if (phdr_ptr->p_type != PT_LOAD)
1137 continue;
1138
HATAYAMA Daisuke7f614cd2013-07-03 15:02:15 -07001139 paddr = phdr_ptr->p_offset;
1140 start = rounddown(paddr, PAGE_SIZE);
1141 end = roundup(paddr + phdr_ptr->p_memsz, PAGE_SIZE);
1142 size = end - start;
1143
Vivek Goyal72658e92005-06-25 14:58:22 -07001144 /* Add this contiguous chunk of memory to vmcore list.*/
1145 new = get_new_element();
1146 if (!new)
1147 return -ENOMEM;
HATAYAMA Daisuke7f614cd2013-07-03 15:02:15 -07001148 new->paddr = start;
1149 new->size = size;
Vivek Goyal72658e92005-06-25 14:58:22 -07001150 list_add_tail(&new->list, vc_list);
1151
1152 /* Update the program header offset */
HATAYAMA Daisuke7f614cd2013-07-03 15:02:15 -07001153 phdr_ptr->p_offset = vmcore_off + (paddr - start);
1154 vmcore_off = vmcore_off + size;
Vivek Goyal72658e92005-06-25 14:58:22 -07001155 }
1156 return 0;
1157}
1158
Vivek Goyal666bfdd2005-06-25 14:58:21 -07001159/* Sets offset fields of vmcore elements. */
Rahul Lakkireddy7efe48d2018-05-02 15:17:18 +05301160static void set_vmcore_list_offsets(size_t elfsz, size_t elfnotes_sz,
1161 struct list_head *vc_list)
Vivek Goyal666bfdd2005-06-25 14:58:21 -07001162{
1163 loff_t vmcore_off;
Vivek Goyal666bfdd2005-06-25 14:58:21 -07001164 struct vmcore *m;
1165
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -07001166 /* Skip Elf header, program headers and Elf note segment. */
1167 vmcore_off = elfsz + elfnotes_sz;
Vivek Goyal666bfdd2005-06-25 14:58:21 -07001168
1169 list_for_each_entry(m, vc_list, list) {
1170 m->offset = vmcore_off;
1171 vmcore_off += m->size;
1172 }
1173}
1174
HATAYAMA Daisukef2bdacd2013-07-03 15:02:14 -07001175static void free_elfcorebuf(void)
Vivek Goyal72658e92005-06-25 14:58:22 -07001176{
HATAYAMA Daisukef2bdacd2013-07-03 15:02:14 -07001177 free_pages((unsigned long)elfcorebuf, get_order(elfcorebuf_sz_orig));
1178 elfcorebuf = NULL;
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -07001179 vfree(elfnotes_buf);
1180 elfnotes_buf = NULL;
Vivek Goyal72658e92005-06-25 14:58:22 -07001181}
1182
Vivek Goyal666bfdd2005-06-25 14:58:21 -07001183static int __init parse_crash_elf64_headers(void)
1184{
1185 int rc=0;
1186 Elf64_Ehdr ehdr;
1187 u64 addr;
1188
1189 addr = elfcorehdr_addr;
1190
1191 /* Read Elf header */
Michael Holzheube8a8d02013-09-11 14:24:49 -07001192 rc = elfcorehdr_read((char *)&ehdr, sizeof(Elf64_Ehdr), &addr);
Vivek Goyal666bfdd2005-06-25 14:58:21 -07001193 if (rc < 0)
1194 return rc;
1195
1196 /* Do some basic Verification. */
1197 if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
1198 (ehdr.e_type != ET_CORE) ||
Mika Westerberg9833c392010-11-19 09:29:24 +01001199 !vmcore_elf64_check_arch(&ehdr) ||
Vivek Goyal666bfdd2005-06-25 14:58:21 -07001200 ehdr.e_ident[EI_CLASS] != ELFCLASS64 ||
1201 ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
1202 ehdr.e_version != EV_CURRENT ||
1203 ehdr.e_ehsize != sizeof(Elf64_Ehdr) ||
1204 ehdr.e_phentsize != sizeof(Elf64_Phdr) ||
1205 ehdr.e_phnum == 0) {
Andrew Morton87ebdc02013-02-27 17:03:16 -08001206 pr_warn("Warning: Core image elf header is not sane\n");
Vivek Goyal666bfdd2005-06-25 14:58:21 -07001207 return -EINVAL;
1208 }
1209
1210 /* Read in all elf headers. */
HATAYAMA Daisukef2bdacd2013-07-03 15:02:14 -07001211 elfcorebuf_sz_orig = sizeof(Elf64_Ehdr) +
1212 ehdr.e_phnum * sizeof(Elf64_Phdr);
1213 elfcorebuf_sz = elfcorebuf_sz_orig;
1214 elfcorebuf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
1215 get_order(elfcorebuf_sz_orig));
Vivek Goyal666bfdd2005-06-25 14:58:21 -07001216 if (!elfcorebuf)
1217 return -ENOMEM;
1218 addr = elfcorehdr_addr;
Michael Holzheube8a8d02013-09-11 14:24:49 -07001219 rc = elfcorehdr_read(elfcorebuf, elfcorebuf_sz_orig, &addr);
HATAYAMA Daisukef2bdacd2013-07-03 15:02:14 -07001220 if (rc < 0)
1221 goto fail;
Vivek Goyal666bfdd2005-06-25 14:58:21 -07001222
1223 /* Merge all PT_NOTE headers into one. */
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -07001224 rc = merge_note_headers_elf64(elfcorebuf, &elfcorebuf_sz,
1225 &elfnotes_buf, &elfnotes_sz);
HATAYAMA Daisukef2bdacd2013-07-03 15:02:14 -07001226 if (rc)
1227 goto fail;
Vivek Goyal666bfdd2005-06-25 14:58:21 -07001228 rc = process_ptload_program_headers_elf64(elfcorebuf, elfcorebuf_sz,
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -07001229 elfnotes_sz, &vmcore_list);
HATAYAMA Daisukef2bdacd2013-07-03 15:02:14 -07001230 if (rc)
1231 goto fail;
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -07001232 set_vmcore_list_offsets(elfcorebuf_sz, elfnotes_sz, &vmcore_list);
Vivek Goyal666bfdd2005-06-25 14:58:21 -07001233 return 0;
HATAYAMA Daisukef2bdacd2013-07-03 15:02:14 -07001234fail:
1235 free_elfcorebuf();
1236 return rc;
Vivek Goyal666bfdd2005-06-25 14:58:21 -07001237}
1238
Vivek Goyal72658e92005-06-25 14:58:22 -07001239static int __init parse_crash_elf32_headers(void)
1240{
1241 int rc=0;
1242 Elf32_Ehdr ehdr;
1243 u64 addr;
1244
1245 addr = elfcorehdr_addr;
1246
1247 /* Read Elf header */
Michael Holzheube8a8d02013-09-11 14:24:49 -07001248 rc = elfcorehdr_read((char *)&ehdr, sizeof(Elf32_Ehdr), &addr);
Vivek Goyal72658e92005-06-25 14:58:22 -07001249 if (rc < 0)
1250 return rc;
1251
1252 /* Do some basic Verification. */
1253 if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
1254 (ehdr.e_type != ET_CORE) ||
Daniel Wagnere55d5312016-02-11 13:36:54 +01001255 !vmcore_elf32_check_arch(&ehdr) ||
Vivek Goyal72658e92005-06-25 14:58:22 -07001256 ehdr.e_ident[EI_CLASS] != ELFCLASS32||
1257 ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
1258 ehdr.e_version != EV_CURRENT ||
1259 ehdr.e_ehsize != sizeof(Elf32_Ehdr) ||
1260 ehdr.e_phentsize != sizeof(Elf32_Phdr) ||
1261 ehdr.e_phnum == 0) {
Andrew Morton87ebdc02013-02-27 17:03:16 -08001262 pr_warn("Warning: Core image elf header is not sane\n");
Vivek Goyal72658e92005-06-25 14:58:22 -07001263 return -EINVAL;
1264 }
1265
1266 /* Read in all elf headers. */
HATAYAMA Daisukef2bdacd2013-07-03 15:02:14 -07001267 elfcorebuf_sz_orig = sizeof(Elf32_Ehdr) + ehdr.e_phnum * sizeof(Elf32_Phdr);
1268 elfcorebuf_sz = elfcorebuf_sz_orig;
1269 elfcorebuf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
1270 get_order(elfcorebuf_sz_orig));
Vivek Goyal72658e92005-06-25 14:58:22 -07001271 if (!elfcorebuf)
1272 return -ENOMEM;
1273 addr = elfcorehdr_addr;
Michael Holzheube8a8d02013-09-11 14:24:49 -07001274 rc = elfcorehdr_read(elfcorebuf, elfcorebuf_sz_orig, &addr);
HATAYAMA Daisukef2bdacd2013-07-03 15:02:14 -07001275 if (rc < 0)
1276 goto fail;
Vivek Goyal72658e92005-06-25 14:58:22 -07001277
1278 /* Merge all PT_NOTE headers into one. */
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -07001279 rc = merge_note_headers_elf32(elfcorebuf, &elfcorebuf_sz,
1280 &elfnotes_buf, &elfnotes_sz);
HATAYAMA Daisukef2bdacd2013-07-03 15:02:14 -07001281 if (rc)
1282 goto fail;
Vivek Goyal72658e92005-06-25 14:58:22 -07001283 rc = process_ptload_program_headers_elf32(elfcorebuf, elfcorebuf_sz,
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -07001284 elfnotes_sz, &vmcore_list);
HATAYAMA Daisukef2bdacd2013-07-03 15:02:14 -07001285 if (rc)
1286 goto fail;
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -07001287 set_vmcore_list_offsets(elfcorebuf_sz, elfnotes_sz, &vmcore_list);
Vivek Goyal72658e92005-06-25 14:58:22 -07001288 return 0;
HATAYAMA Daisukef2bdacd2013-07-03 15:02:14 -07001289fail:
1290 free_elfcorebuf();
1291 return rc;
Vivek Goyal72658e92005-06-25 14:58:22 -07001292}
1293
Vivek Goyal666bfdd2005-06-25 14:58:21 -07001294static int __init parse_crash_elf_headers(void)
1295{
1296 unsigned char e_ident[EI_NIDENT];
1297 u64 addr;
1298 int rc=0;
1299
1300 addr = elfcorehdr_addr;
Michael Holzheube8a8d02013-09-11 14:24:49 -07001301 rc = elfcorehdr_read(e_ident, EI_NIDENT, &addr);
Vivek Goyal666bfdd2005-06-25 14:58:21 -07001302 if (rc < 0)
1303 return rc;
1304 if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) {
Andrew Morton87ebdc02013-02-27 17:03:16 -08001305 pr_warn("Warning: Core image elf header not found\n");
Vivek Goyal666bfdd2005-06-25 14:58:21 -07001306 return -EINVAL;
1307 }
1308
1309 if (e_ident[EI_CLASS] == ELFCLASS64) {
1310 rc = parse_crash_elf64_headers();
1311 if (rc)
1312 return rc;
Vivek Goyal72658e92005-06-25 14:58:22 -07001313 } else if (e_ident[EI_CLASS] == ELFCLASS32) {
1314 rc = parse_crash_elf32_headers();
1315 if (rc)
1316 return rc;
Vivek Goyal666bfdd2005-06-25 14:58:21 -07001317 } else {
Andrew Morton87ebdc02013-02-27 17:03:16 -08001318 pr_warn("Warning: Core image elf header is not sane\n");
Vivek Goyal666bfdd2005-06-25 14:58:21 -07001319 return -EINVAL;
1320 }
HATAYAMA Daisuke591ff712013-07-03 15:02:22 -07001321
1322 /* Determine vmcore size. */
1323 vmcore_size = get_vmcore_size(elfcorebuf_sz, elfnotes_sz,
1324 &vmcore_list);
1325
Vivek Goyal666bfdd2005-06-25 14:58:21 -07001326 return 0;
1327}
1328
Rahul Lakkireddy27242732018-05-02 15:17:17 +05301329#ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
1330/**
1331 * vmcoredd_write_header - Write vmcore device dump header at the
1332 * beginning of the dump's buffer.
1333 * @buf: Output buffer where the note is written
1334 * @data: Dump info
1335 * @size: Size of the dump
1336 *
1337 * Fills beginning of the dump's buffer with vmcore device dump header.
1338 */
1339static void vmcoredd_write_header(void *buf, struct vmcoredd_data *data,
1340 u32 size)
1341{
1342 struct vmcoredd_header *vdd_hdr = (struct vmcoredd_header *)buf;
1343
1344 vdd_hdr->n_namesz = sizeof(vdd_hdr->name);
1345 vdd_hdr->n_descsz = size + sizeof(vdd_hdr->dump_name);
1346 vdd_hdr->n_type = NT_VMCOREDD;
1347
1348 strncpy((char *)vdd_hdr->name, VMCOREDD_NOTE_NAME,
1349 sizeof(vdd_hdr->name));
1350 memcpy(vdd_hdr->dump_name, data->dump_name, sizeof(vdd_hdr->dump_name));
1351}
1352
1353/**
Rahul Lakkireddy7efe48d2018-05-02 15:17:18 +05301354 * vmcoredd_update_program_headers - Update all Elf program headers
1355 * @elfptr: Pointer to elf header
1356 * @elfnotesz: Size of elf notes aligned to page size
1357 * @vmcoreddsz: Size of device dumps to be added to elf note header
1358 *
1359 * Determine type of Elf header (Elf64 or Elf32) and update the elf note size.
1360 * Also update the offsets of all the program headers after the elf note header.
1361 */
1362static void vmcoredd_update_program_headers(char *elfptr, size_t elfnotesz,
1363 size_t vmcoreddsz)
1364{
1365 unsigned char *e_ident = (unsigned char *)elfptr;
1366 u64 start, end, size;
1367 loff_t vmcore_off;
1368 u32 i;
1369
1370 vmcore_off = elfcorebuf_sz + elfnotesz;
1371
1372 if (e_ident[EI_CLASS] == ELFCLASS64) {
1373 Elf64_Ehdr *ehdr = (Elf64_Ehdr *)elfptr;
1374 Elf64_Phdr *phdr = (Elf64_Phdr *)(elfptr + sizeof(Elf64_Ehdr));
1375
1376 /* Update all program headers */
1377 for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
1378 if (phdr->p_type == PT_NOTE) {
1379 /* Update note size */
1380 phdr->p_memsz = elfnotes_orig_sz + vmcoreddsz;
1381 phdr->p_filesz = phdr->p_memsz;
1382 continue;
1383 }
1384
1385 start = rounddown(phdr->p_offset, PAGE_SIZE);
1386 end = roundup(phdr->p_offset + phdr->p_memsz,
1387 PAGE_SIZE);
1388 size = end - start;
1389 phdr->p_offset = vmcore_off + (phdr->p_offset - start);
1390 vmcore_off += size;
1391 }
1392 } else {
1393 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)elfptr;
1394 Elf32_Phdr *phdr = (Elf32_Phdr *)(elfptr + sizeof(Elf32_Ehdr));
1395
1396 /* Update all program headers */
1397 for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
1398 if (phdr->p_type == PT_NOTE) {
1399 /* Update note size */
1400 phdr->p_memsz = elfnotes_orig_sz + vmcoreddsz;
1401 phdr->p_filesz = phdr->p_memsz;
1402 continue;
1403 }
1404
1405 start = rounddown(phdr->p_offset, PAGE_SIZE);
1406 end = roundup(phdr->p_offset + phdr->p_memsz,
1407 PAGE_SIZE);
1408 size = end - start;
1409 phdr->p_offset = vmcore_off + (phdr->p_offset - start);
1410 vmcore_off += size;
1411 }
1412 }
1413}
1414
1415/**
1416 * vmcoredd_update_size - Update the total size of the device dumps and update
1417 * Elf header
1418 * @dump_size: Size of the current device dump to be added to total size
1419 *
1420 * Update the total size of all the device dumps and update the Elf program
1421 * headers. Calculate the new offsets for the vmcore list and update the
1422 * total vmcore size.
1423 */
1424static void vmcoredd_update_size(size_t dump_size)
1425{
1426 vmcoredd_orig_sz += dump_size;
1427 elfnotes_sz = roundup(elfnotes_orig_sz, PAGE_SIZE) + vmcoredd_orig_sz;
1428 vmcoredd_update_program_headers(elfcorebuf, elfnotes_sz,
1429 vmcoredd_orig_sz);
1430
1431 /* Update vmcore list offsets */
1432 set_vmcore_list_offsets(elfcorebuf_sz, elfnotes_sz, &vmcore_list);
1433
1434 vmcore_size = get_vmcore_size(elfcorebuf_sz, elfnotes_sz,
1435 &vmcore_list);
1436 proc_vmcore->size = vmcore_size;
1437}
1438
1439/**
Rahul Lakkireddy27242732018-05-02 15:17:17 +05301440 * vmcore_add_device_dump - Add a buffer containing device dump to vmcore
1441 * @data: dump info.
1442 *
1443 * Allocate a buffer and invoke the calling driver's dump collect routine.
1444 * Write Elf note at the beginning of the buffer to indicate vmcore device
1445 * dump and add the dump to global list.
1446 */
1447int vmcore_add_device_dump(struct vmcoredd_data *data)
1448{
1449 struct vmcoredd_node *dump;
1450 void *buf = NULL;
1451 size_t data_size;
1452 int ret;
1453
1454 if (!data || !strlen(data->dump_name) ||
1455 !data->vmcoredd_callback || !data->size)
1456 return -EINVAL;
1457
1458 dump = vzalloc(sizeof(*dump));
1459 if (!dump) {
1460 ret = -ENOMEM;
1461 goto out_err;
1462 }
1463
1464 /* Keep size of the buffer page aligned so that it can be mmaped */
1465 data_size = roundup(sizeof(struct vmcoredd_header) + data->size,
1466 PAGE_SIZE);
1467
1468 /* Allocate buffer for driver's to write their dumps */
1469 buf = vmcore_alloc_buf(data_size);
1470 if (!buf) {
1471 ret = -ENOMEM;
1472 goto out_err;
1473 }
1474
1475 vmcoredd_write_header(buf, data, data_size -
1476 sizeof(struct vmcoredd_header));
1477
1478 /* Invoke the driver's dump collection routing */
1479 ret = data->vmcoredd_callback(data, buf +
1480 sizeof(struct vmcoredd_header));
1481 if (ret)
1482 goto out_err;
1483
1484 dump->buf = buf;
1485 dump->size = data_size;
1486
1487 /* Add the dump to driver sysfs list */
1488 mutex_lock(&vmcoredd_mutex);
1489 list_add_tail(&dump->list, &vmcoredd_list);
1490 mutex_unlock(&vmcoredd_mutex);
1491
Rahul Lakkireddy7efe48d2018-05-02 15:17:18 +05301492 vmcoredd_update_size(data_size);
Rahul Lakkireddy27242732018-05-02 15:17:17 +05301493 return 0;
1494
1495out_err:
1496 if (buf)
1497 vfree(buf);
1498
1499 if (dump)
1500 vfree(dump);
1501
1502 return ret;
1503}
1504EXPORT_SYMBOL(vmcore_add_device_dump);
1505#endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
1506
1507/* Free all dumps in vmcore device dump list */
1508static void vmcore_free_device_dumps(void)
1509{
1510#ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP
1511 mutex_lock(&vmcoredd_mutex);
1512 while (!list_empty(&vmcoredd_list)) {
1513 struct vmcoredd_node *dump;
1514
1515 dump = list_first_entry(&vmcoredd_list, struct vmcoredd_node,
1516 list);
1517 list_del(&dump->list);
1518 vfree(dump->buf);
1519 vfree(dump);
1520 }
1521 mutex_unlock(&vmcoredd_mutex);
1522#endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */
1523}
1524
Vivek Goyal666bfdd2005-06-25 14:58:21 -07001525/* Init function for vmcore module. */
1526static int __init vmcore_init(void)
1527{
1528 int rc = 0;
1529
Michael Holzheube8a8d02013-09-11 14:24:49 -07001530 /* Allow architectures to allocate ELF header in 2nd kernel */
1531 rc = elfcorehdr_alloc(&elfcorehdr_addr, &elfcorehdr_size);
1532 if (rc)
1533 return rc;
1534 /*
1535 * If elfcorehdr= has been passed in cmdline or created in 2nd kernel,
1536 * then capture the dump.
1537 */
Simon Horman85a0ee32008-10-18 20:28:29 -07001538 if (!(is_vmcore_usable()))
Vivek Goyal666bfdd2005-06-25 14:58:21 -07001539 return rc;
1540 rc = parse_crash_elf_headers();
1541 if (rc) {
Andrew Morton87ebdc02013-02-27 17:03:16 -08001542 pr_warn("Kdump: vmcore not initialized\n");
Vivek Goyal666bfdd2005-06-25 14:58:21 -07001543 return rc;
1544 }
Michael Holzheube8a8d02013-09-11 14:24:49 -07001545 elfcorehdr_free(elfcorehdr_addr);
1546 elfcorehdr_addr = ELFCORE_ADDR_ERR;
Vivek Goyal666bfdd2005-06-25 14:58:21 -07001547
Alexey Dobriyan5aa140c2008-10-06 14:36:31 +04001548 proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations);
Vivek Goyal666bfdd2005-06-25 14:58:21 -07001549 if (proc_vmcore)
1550 proc_vmcore->size = vmcore_size;
1551 return 0;
1552}
Paul Gortmakerabaf3782014-01-23 15:55:45 -08001553fs_initcall(vmcore_init);
Mahesh Salgaonkar162573932012-02-16 01:15:00 +00001554
1555/* Cleanup function for vmcore module. */
1556void vmcore_cleanup(void)
1557{
Mahesh Salgaonkar162573932012-02-16 01:15:00 +00001558 if (proc_vmcore) {
David Howellsa8ca16e2013-04-12 17:27:28 +01001559 proc_remove(proc_vmcore);
Mahesh Salgaonkar162573932012-02-16 01:15:00 +00001560 proc_vmcore = NULL;
1561 }
1562
1563 /* clear the vmcore list. */
Alexey Dobriyan593bc692018-02-06 15:37:02 -08001564 while (!list_empty(&vmcore_list)) {
Mahesh Salgaonkar162573932012-02-16 01:15:00 +00001565 struct vmcore *m;
1566
Alexey Dobriyan593bc692018-02-06 15:37:02 -08001567 m = list_first_entry(&vmcore_list, struct vmcore, list);
Mahesh Salgaonkar162573932012-02-16 01:15:00 +00001568 list_del(&m->list);
1569 kfree(m);
1570 }
HATAYAMA Daisukef2bdacd2013-07-03 15:02:14 -07001571 free_elfcorebuf();
Rahul Lakkireddy27242732018-05-02 15:17:17 +05301572
1573 /* clear vmcore device dump list */
1574 vmcore_free_device_dumps();
Mahesh Salgaonkar162573932012-02-16 01:15:00 +00001575}