blob: a1a16eb97c7bd141c7a68419b44769ebea35c854 [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>
Vivek Goyal666bfdd2005-06-25 14:58:21 -070019#include <linux/bootmem.h>
20#include <linux/init.h>
21#include <linux/crash_dump.h>
22#include <linux/list.h>
HATAYAMA Daisuke83086972013-07-03 15:02:23 -070023#include <linux/vmalloc.h>
Vivek Goyal666bfdd2005-06-25 14:58:21 -070024#include <asm/uaccess.h>
25#include <asm/io.h>
David Howells2f96b8c2013-04-12 00:10:25 +010026#include "internal.h"
Vivek Goyal666bfdd2005-06-25 14:58:21 -070027
28/* List representing chunks of contiguous memory areas and their offsets in
29 * vmcore file.
30 */
31static LIST_HEAD(vmcore_list);
32
33/* Stores the pointer to the buffer containing kernel elf core headers. */
34static char *elfcorebuf;
35static size_t elfcorebuf_sz;
HATAYAMA Daisukef2bdacd2013-07-03 15:02:14 -070036static size_t elfcorebuf_sz_orig;
Vivek Goyal666bfdd2005-06-25 14:58:21 -070037
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -070038static char *elfnotes_buf;
39static size_t elfnotes_sz;
40
Vivek Goyal666bfdd2005-06-25 14:58:21 -070041/* Total size of vmcore file. */
42static u64 vmcore_size;
43
Alexey Dobriyan5aa140c2008-10-06 14:36:31 +040044static struct proc_dir_entry *proc_vmcore = NULL;
Vivek Goyal666bfdd2005-06-25 14:58:21 -070045
Olaf Hering997c1362011-05-26 16:25:54 -070046/*
47 * Returns > 0 for RAM pages, 0 for non-RAM pages, < 0 on error
48 * The called function has to take care of module refcounting.
49 */
50static int (*oldmem_pfn_is_ram)(unsigned long pfn);
51
52int register_oldmem_pfn_is_ram(int (*fn)(unsigned long pfn))
53{
54 if (oldmem_pfn_is_ram)
55 return -EBUSY;
56 oldmem_pfn_is_ram = fn;
57 return 0;
58}
59EXPORT_SYMBOL_GPL(register_oldmem_pfn_is_ram);
60
61void unregister_oldmem_pfn_is_ram(void)
62{
63 oldmem_pfn_is_ram = NULL;
64 wmb();
65}
66EXPORT_SYMBOL_GPL(unregister_oldmem_pfn_is_ram);
67
68static int pfn_is_ram(unsigned long pfn)
69{
70 int (*fn)(unsigned long pfn);
71 /* pfn is ram unless fn() checks pagetype */
72 int ret = 1;
73
74 /*
75 * Ask hypervisor if the pfn is really ram.
76 * A ballooned page contains no data and reading from such a page
77 * will cause high load in the hypervisor.
78 */
79 fn = oldmem_pfn_is_ram;
80 if (fn)
81 ret = fn(pfn);
82
83 return ret;
84}
85
Vivek Goyal666bfdd2005-06-25 14:58:21 -070086/* Reads a page from the oldmem device from given offset. */
87static ssize_t read_from_oldmem(char *buf, size_t count,
Vivek Goyal9e9e3942006-01-11 12:17:37 -080088 u64 *ppos, int userbuf)
Vivek Goyal666bfdd2005-06-25 14:58:21 -070089{
90 unsigned long pfn, offset;
91 size_t nr_bytes;
92 ssize_t read = 0, tmp;
93
94 if (!count)
95 return 0;
96
97 offset = (unsigned long)(*ppos % PAGE_SIZE);
98 pfn = (unsigned long)(*ppos / PAGE_SIZE);
Vivek Goyal666bfdd2005-06-25 14:58:21 -070099
100 do {
101 if (count > (PAGE_SIZE - offset))
102 nr_bytes = PAGE_SIZE - offset;
103 else
104 nr_bytes = count;
105
Olaf Hering997c1362011-05-26 16:25:54 -0700106 /* If pfn is not ram, return zeros for sparse dump files */
107 if (pfn_is_ram(pfn) == 0)
108 memset(buf, 0, nr_bytes);
109 else {
110 tmp = copy_oldmem_page(pfn, buf, nr_bytes,
111 offset, userbuf);
112 if (tmp < 0)
113 return tmp;
114 }
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700115 *ppos += nr_bytes;
116 count -= nr_bytes;
117 buf += nr_bytes;
118 read += nr_bytes;
119 ++pfn;
120 offset = 0;
121 } while (count);
122
123 return read;
124}
125
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700126/* Read from the ELF header and then the crash dump. On error, negative value is
127 * returned otherwise number of bytes read are returned.
128 */
129static ssize_t read_vmcore(struct file *file, char __user *buffer,
130 size_t buflen, loff_t *fpos)
131{
132 ssize_t acc = 0, tmp;
Vivek Goyal80e8ff62006-04-10 22:54:10 -0700133 size_t tsz;
HATAYAMA Daisukeb27eb182013-07-03 15:02:13 -0700134 u64 start;
135 struct vmcore *m = NULL;
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700136
137 if (buflen == 0 || *fpos >= vmcore_size)
138 return 0;
139
140 /* trim buflen to not go beyond EOF */
141 if (buflen > vmcore_size - *fpos)
142 buflen = vmcore_size - *fpos;
143
144 /* Read ELF core header */
145 if (*fpos < elfcorebuf_sz) {
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700146 tsz = min(elfcorebuf_sz - (size_t)*fpos, buflen);
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700147 if (copy_to_user(buffer, elfcorebuf + *fpos, tsz))
148 return -EFAULT;
149 buflen -= tsz;
150 *fpos += tsz;
151 buffer += tsz;
152 acc += tsz;
153
154 /* leave now if filled buffer already */
155 if (buflen == 0)
156 return acc;
157 }
158
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700159 /* Read Elf note segment */
160 if (*fpos < elfcorebuf_sz + elfnotes_sz) {
161 void *kaddr;
162
163 tsz = min(elfcorebuf_sz + elfnotes_sz - (size_t)*fpos, buflen);
164 kaddr = elfnotes_buf + *fpos - elfcorebuf_sz;
165 if (copy_to_user(buffer, kaddr, tsz))
166 return -EFAULT;
167 buflen -= tsz;
168 *fpos += tsz;
169 buffer += tsz;
170 acc += tsz;
171
172 /* leave now if filled buffer already */
173 if (buflen == 0)
174 return acc;
175 }
176
HATAYAMA Daisukeb27eb182013-07-03 15:02:13 -0700177 list_for_each_entry(m, &vmcore_list, list) {
178 if (*fpos < m->offset + m->size) {
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700179 tsz = min_t(size_t, m->offset + m->size - *fpos, buflen);
HATAYAMA Daisukeb27eb182013-07-03 15:02:13 -0700180 start = m->paddr + *fpos - m->offset;
181 tmp = read_from_oldmem(buffer, tsz, &start, 1);
182 if (tmp < 0)
183 return tmp;
184 buflen -= tsz;
185 *fpos += tsz;
186 buffer += tsz;
187 acc += tsz;
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700188
HATAYAMA Daisukeb27eb182013-07-03 15:02:13 -0700189 /* leave now if filled buffer already */
190 if (buflen == 0)
191 return acc;
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700192 }
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700193 }
HATAYAMA Daisukeb27eb182013-07-03 15:02:13 -0700194
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700195 return acc;
196}
197
HATAYAMA Daisuke83086972013-07-03 15:02:23 -0700198/**
199 * alloc_elfnotes_buf - allocate buffer for ELF note segment in
200 * vmalloc memory
201 *
202 * @notes_sz: size of buffer
203 *
204 * If CONFIG_MMU is defined, use vmalloc_user() to allow users to mmap
205 * the buffer to user-space by means of remap_vmalloc_range().
206 *
207 * If CONFIG_MMU is not defined, use vzalloc() since mmap_vmcore() is
208 * disabled and there's no need to allow users to mmap the buffer.
209 */
210static inline char *alloc_elfnotes_buf(size_t notes_sz)
211{
212#ifdef CONFIG_MMU
213 return vmalloc_user(notes_sz);
214#else
215 return vzalloc(notes_sz);
216#endif
217}
218
219/*
220 * Disable mmap_vmcore() if CONFIG_MMU is not defined. MMU is
221 * essential for mmap_vmcore() in order to map physically
222 * non-contiguous objects (ELF header, ELF note segment and memory
223 * regions in the 1st kernel pointed to by PT_LOAD entries) into
224 * virtually contiguous user-space in ELF layout.
225 */
Michael Holzheu5a749532013-07-18 12:17:57 +0200226#if defined(CONFIG_MMU) && !defined(CONFIG_S390)
HATAYAMA Daisuke83086972013-07-03 15:02:23 -0700227static int mmap_vmcore(struct file *file, struct vm_area_struct *vma)
228{
229 size_t size = vma->vm_end - vma->vm_start;
230 u64 start, end, len, tsz;
231 struct vmcore *m;
232
233 start = (u64)vma->vm_pgoff << PAGE_SHIFT;
234 end = start + size;
235
236 if (size > vmcore_size || end > vmcore_size)
237 return -EINVAL;
238
239 if (vma->vm_flags & (VM_WRITE | VM_EXEC))
240 return -EPERM;
241
242 vma->vm_flags &= ~(VM_MAYWRITE | VM_MAYEXEC);
243 vma->vm_flags |= VM_MIXEDMAP;
244
245 len = 0;
246
247 if (start < elfcorebuf_sz) {
248 u64 pfn;
249
250 tsz = min(elfcorebuf_sz - (size_t)start, size);
251 pfn = __pa(elfcorebuf + start) >> PAGE_SHIFT;
252 if (remap_pfn_range(vma, vma->vm_start, pfn, tsz,
253 vma->vm_page_prot))
254 return -EAGAIN;
255 size -= tsz;
256 start += tsz;
257 len += tsz;
258
259 if (size == 0)
260 return 0;
261 }
262
263 if (start < elfcorebuf_sz + elfnotes_sz) {
264 void *kaddr;
265
266 tsz = min(elfcorebuf_sz + elfnotes_sz - (size_t)start, size);
267 kaddr = elfnotes_buf + start - elfcorebuf_sz;
268 if (remap_vmalloc_range_partial(vma, vma->vm_start + len,
269 kaddr, tsz))
270 goto fail;
271 size -= tsz;
272 start += tsz;
273 len += tsz;
274
275 if (size == 0)
276 return 0;
277 }
278
279 list_for_each_entry(m, &vmcore_list, list) {
280 if (start < m->offset + m->size) {
281 u64 paddr = 0;
282
283 tsz = min_t(size_t, m->offset + m->size - start, size);
284 paddr = m->paddr + start - m->offset;
285 if (remap_pfn_range(vma, vma->vm_start + len,
286 paddr >> PAGE_SHIFT, tsz,
287 vma->vm_page_prot))
288 goto fail;
289 size -= tsz;
290 start += tsz;
291 len += tsz;
292
293 if (size == 0)
294 return 0;
295 }
296 }
297
298 return 0;
299fail:
300 do_munmap(vma->vm_mm, vma->vm_start, len);
301 return -EAGAIN;
302}
303#else
304static int mmap_vmcore(struct file *file, struct vm_area_struct *vma)
305{
306 return -ENOSYS;
307}
308#endif
309
Alexey Dobriyan5aa140c2008-10-06 14:36:31 +0400310static const struct file_operations proc_vmcore_operations = {
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700311 .read = read_vmcore,
Arnd Bergmannc227e692010-09-22 13:04:54 -0700312 .llseek = default_llseek,
HATAYAMA Daisuke83086972013-07-03 15:02:23 -0700313 .mmap = mmap_vmcore,
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700314};
315
316static struct vmcore* __init get_new_element(void)
317{
Cyrill Gorcunov2f6d3112009-06-17 16:26:00 -0700318 return kzalloc(sizeof(struct vmcore), GFP_KERNEL);
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700319}
320
HATAYAMA Daisuke591ff712013-07-03 15:02:22 -0700321static u64 __init get_vmcore_size(size_t elfsz, size_t elfnotesegsz,
322 struct list_head *vc_list)
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700323{
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700324 u64 size;
HATAYAMA Daisuke591ff712013-07-03 15:02:22 -0700325 struct vmcore *m;
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700326
HATAYAMA Daisuke591ff712013-07-03 15:02:22 -0700327 size = elfsz + elfnotesegsz;
328 list_for_each_entry(m, vc_list, list) {
329 size += m->size;
Vivek Goyal72658e92005-06-25 14:58:22 -0700330 }
331 return size;
332}
333
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700334/**
335 * update_note_header_size_elf64 - update p_memsz member of each PT_NOTE entry
336 *
337 * @ehdr_ptr: ELF header
338 *
339 * This function updates p_memsz member of each PT_NOTE entry in the
340 * program header table pointed to by @ehdr_ptr to real size of ELF
341 * note segment.
342 */
343static int __init update_note_header_size_elf64(const Elf64_Ehdr *ehdr_ptr)
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700344{
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700345 int i, rc=0;
346 Elf64_Phdr *phdr_ptr;
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700347 Elf64_Nhdr *nhdr_ptr;
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700348
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700349 phdr_ptr = (Elf64_Phdr *)(ehdr_ptr + 1);
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700350 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700351 void *notes_section;
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700352 u64 offset, max_sz, sz, real_sz = 0;
353 if (phdr_ptr->p_type != PT_NOTE)
354 continue;
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700355 max_sz = phdr_ptr->p_memsz;
356 offset = phdr_ptr->p_offset;
357 notes_section = kmalloc(max_sz, GFP_KERNEL);
358 if (!notes_section)
359 return -ENOMEM;
360 rc = read_from_oldmem(notes_section, max_sz, &offset, 0);
361 if (rc < 0) {
362 kfree(notes_section);
363 return rc;
364 }
365 nhdr_ptr = notes_section;
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700366 while (real_sz < max_sz) {
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700367 if (nhdr_ptr->n_namesz == 0)
368 break;
369 sz = sizeof(Elf64_Nhdr) +
370 ((nhdr_ptr->n_namesz + 3) & ~3) +
371 ((nhdr_ptr->n_descsz + 3) & ~3);
372 real_sz += sz;
373 nhdr_ptr = (Elf64_Nhdr*)((char*)nhdr_ptr + sz);
374 }
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700375 kfree(notes_section);
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700376 phdr_ptr->p_memsz = real_sz;
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700377 }
378
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700379 return 0;
380}
381
382/**
383 * get_note_number_and_size_elf64 - get the number of PT_NOTE program
384 * headers and sum of real size of their ELF note segment headers and
385 * data.
386 *
387 * @ehdr_ptr: ELF header
388 * @nr_ptnote: buffer for the number of PT_NOTE program headers
389 * @sz_ptnote: buffer for size of unique PT_NOTE program header
390 *
391 * This function is used to merge multiple PT_NOTE program headers
392 * into a unique single one. The resulting unique entry will have
393 * @sz_ptnote in its phdr->p_mem.
394 *
395 * It is assumed that program headers with PT_NOTE type pointed to by
396 * @ehdr_ptr has already been updated by update_note_header_size_elf64
397 * and each of PT_NOTE program headers has actual ELF note segment
398 * size in its p_memsz member.
399 */
400static int __init get_note_number_and_size_elf64(const Elf64_Ehdr *ehdr_ptr,
401 int *nr_ptnote, u64 *sz_ptnote)
402{
403 int i;
404 Elf64_Phdr *phdr_ptr;
405
406 *nr_ptnote = *sz_ptnote = 0;
407
408 phdr_ptr = (Elf64_Phdr *)(ehdr_ptr + 1);
409 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
410 if (phdr_ptr->p_type != PT_NOTE)
411 continue;
412 *nr_ptnote += 1;
413 *sz_ptnote += phdr_ptr->p_memsz;
414 }
415
416 return 0;
417}
418
419/**
420 * copy_notes_elf64 - copy ELF note segments in a given buffer
421 *
422 * @ehdr_ptr: ELF header
423 * @notes_buf: buffer into which ELF note segments are copied
424 *
425 * This function is used to copy ELF note segment in the 1st kernel
426 * into the buffer @notes_buf in the 2nd kernel. It is assumed that
427 * size of the buffer @notes_buf is equal to or larger than sum of the
428 * real ELF note segment headers and data.
429 *
430 * It is assumed that program headers with PT_NOTE type pointed to by
431 * @ehdr_ptr has already been updated by update_note_header_size_elf64
432 * and each of PT_NOTE program headers has actual ELF note segment
433 * size in its p_memsz member.
434 */
435static int __init copy_notes_elf64(const Elf64_Ehdr *ehdr_ptr, char *notes_buf)
436{
437 int i, rc=0;
438 Elf64_Phdr *phdr_ptr;
439
440 phdr_ptr = (Elf64_Phdr*)(ehdr_ptr + 1);
441
442 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
443 u64 offset;
444 if (phdr_ptr->p_type != PT_NOTE)
445 continue;
446 offset = phdr_ptr->p_offset;
447 rc = read_from_oldmem(notes_buf, phdr_ptr->p_memsz, &offset, 0);
448 if (rc < 0)
449 return rc;
450 notes_buf += phdr_ptr->p_memsz;
451 }
452
453 return 0;
454}
455
456/* Merges all the PT_NOTE headers into one. */
457static int __init merge_note_headers_elf64(char *elfptr, size_t *elfsz,
458 char **notes_buf, size_t *notes_sz)
459{
460 int i, nr_ptnote=0, rc=0;
461 char *tmp;
462 Elf64_Ehdr *ehdr_ptr;
463 Elf64_Phdr phdr;
464 u64 phdr_sz = 0, note_off;
465
466 ehdr_ptr = (Elf64_Ehdr *)elfptr;
467
468 rc = update_note_header_size_elf64(ehdr_ptr);
469 if (rc < 0)
470 return rc;
471
472 rc = get_note_number_and_size_elf64(ehdr_ptr, &nr_ptnote, &phdr_sz);
473 if (rc < 0)
474 return rc;
475
476 *notes_sz = roundup(phdr_sz, PAGE_SIZE);
HATAYAMA Daisuke83086972013-07-03 15:02:23 -0700477 *notes_buf = alloc_elfnotes_buf(*notes_sz);
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700478 if (!*notes_buf)
479 return -ENOMEM;
480
481 rc = copy_notes_elf64(ehdr_ptr, *notes_buf);
482 if (rc < 0)
483 return rc;
484
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700485 /* Prepare merged PT_NOTE program header. */
486 phdr.p_type = PT_NOTE;
487 phdr.p_flags = 0;
488 note_off = sizeof(Elf64_Ehdr) +
489 (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf64_Phdr);
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700490 phdr.p_offset = roundup(note_off, PAGE_SIZE);
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700491 phdr.p_vaddr = phdr.p_paddr = 0;
492 phdr.p_filesz = phdr.p_memsz = phdr_sz;
493 phdr.p_align = 0;
494
495 /* Add merged PT_NOTE program header*/
496 tmp = elfptr + sizeof(Elf64_Ehdr);
497 memcpy(tmp, &phdr, sizeof(phdr));
498 tmp += sizeof(phdr);
499
500 /* Remove unwanted PT_NOTE program headers. */
501 i = (nr_ptnote - 1) * sizeof(Elf64_Phdr);
502 *elfsz = *elfsz - i;
503 memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf64_Ehdr)-sizeof(Elf64_Phdr)));
HATAYAMA Daisukef2bdacd2013-07-03 15:02:14 -0700504 memset(elfptr + *elfsz, 0, i);
505 *elfsz = roundup(*elfsz, PAGE_SIZE);
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700506
507 /* Modify e_phnum to reflect merged headers. */
508 ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
509
510 return 0;
511}
512
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700513/**
514 * update_note_header_size_elf32 - update p_memsz member of each PT_NOTE entry
515 *
516 * @ehdr_ptr: ELF header
517 *
518 * This function updates p_memsz member of each PT_NOTE entry in the
519 * program header table pointed to by @ehdr_ptr to real size of ELF
520 * note segment.
521 */
522static int __init update_note_header_size_elf32(const Elf32_Ehdr *ehdr_ptr)
Vivek Goyal72658e92005-06-25 14:58:22 -0700523{
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700524 int i, rc=0;
525 Elf32_Phdr *phdr_ptr;
Vivek Goyal72658e92005-06-25 14:58:22 -0700526 Elf32_Nhdr *nhdr_ptr;
Vivek Goyal72658e92005-06-25 14:58:22 -0700527
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700528 phdr_ptr = (Elf32_Phdr *)(ehdr_ptr + 1);
Vivek Goyal72658e92005-06-25 14:58:22 -0700529 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
Vivek Goyal72658e92005-06-25 14:58:22 -0700530 void *notes_section;
Vivek Goyal72658e92005-06-25 14:58:22 -0700531 u64 offset, max_sz, sz, real_sz = 0;
532 if (phdr_ptr->p_type != PT_NOTE)
533 continue;
Vivek Goyal72658e92005-06-25 14:58:22 -0700534 max_sz = phdr_ptr->p_memsz;
535 offset = phdr_ptr->p_offset;
536 notes_section = kmalloc(max_sz, GFP_KERNEL);
537 if (!notes_section)
538 return -ENOMEM;
539 rc = read_from_oldmem(notes_section, max_sz, &offset, 0);
540 if (rc < 0) {
541 kfree(notes_section);
542 return rc;
543 }
544 nhdr_ptr = notes_section;
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700545 while (real_sz < max_sz) {
Vivek Goyal72658e92005-06-25 14:58:22 -0700546 if (nhdr_ptr->n_namesz == 0)
547 break;
548 sz = sizeof(Elf32_Nhdr) +
549 ((nhdr_ptr->n_namesz + 3) & ~3) +
550 ((nhdr_ptr->n_descsz + 3) & ~3);
551 real_sz += sz;
552 nhdr_ptr = (Elf32_Nhdr*)((char*)nhdr_ptr + sz);
553 }
Vivek Goyal72658e92005-06-25 14:58:22 -0700554 kfree(notes_section);
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700555 phdr_ptr->p_memsz = real_sz;
Vivek Goyal72658e92005-06-25 14:58:22 -0700556 }
557
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700558 return 0;
559}
560
561/**
562 * get_note_number_and_size_elf32 - get the number of PT_NOTE program
563 * headers and sum of real size of their ELF note segment headers and
564 * data.
565 *
566 * @ehdr_ptr: ELF header
567 * @nr_ptnote: buffer for the number of PT_NOTE program headers
568 * @sz_ptnote: buffer for size of unique PT_NOTE program header
569 *
570 * This function is used to merge multiple PT_NOTE program headers
571 * into a unique single one. The resulting unique entry will have
572 * @sz_ptnote in its phdr->p_mem.
573 *
574 * It is assumed that program headers with PT_NOTE type pointed to by
575 * @ehdr_ptr has already been updated by update_note_header_size_elf32
576 * and each of PT_NOTE program headers has actual ELF note segment
577 * size in its p_memsz member.
578 */
579static int __init get_note_number_and_size_elf32(const Elf32_Ehdr *ehdr_ptr,
580 int *nr_ptnote, u64 *sz_ptnote)
581{
582 int i;
583 Elf32_Phdr *phdr_ptr;
584
585 *nr_ptnote = *sz_ptnote = 0;
586
587 phdr_ptr = (Elf32_Phdr *)(ehdr_ptr + 1);
588 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
589 if (phdr_ptr->p_type != PT_NOTE)
590 continue;
591 *nr_ptnote += 1;
592 *sz_ptnote += phdr_ptr->p_memsz;
593 }
594
595 return 0;
596}
597
598/**
599 * copy_notes_elf32 - copy ELF note segments in a given buffer
600 *
601 * @ehdr_ptr: ELF header
602 * @notes_buf: buffer into which ELF note segments are copied
603 *
604 * This function is used to copy ELF note segment in the 1st kernel
605 * into the buffer @notes_buf in the 2nd kernel. It is assumed that
606 * size of the buffer @notes_buf is equal to or larger than sum of the
607 * real ELF note segment headers and data.
608 *
609 * It is assumed that program headers with PT_NOTE type pointed to by
610 * @ehdr_ptr has already been updated by update_note_header_size_elf32
611 * and each of PT_NOTE program headers has actual ELF note segment
612 * size in its p_memsz member.
613 */
614static int __init copy_notes_elf32(const Elf32_Ehdr *ehdr_ptr, char *notes_buf)
615{
616 int i, rc=0;
617 Elf32_Phdr *phdr_ptr;
618
619 phdr_ptr = (Elf32_Phdr*)(ehdr_ptr + 1);
620
621 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
622 u64 offset;
623 if (phdr_ptr->p_type != PT_NOTE)
624 continue;
625 offset = phdr_ptr->p_offset;
626 rc = read_from_oldmem(notes_buf, phdr_ptr->p_memsz, &offset, 0);
627 if (rc < 0)
628 return rc;
629 notes_buf += phdr_ptr->p_memsz;
630 }
631
632 return 0;
633}
634
635/* Merges all the PT_NOTE headers into one. */
636static int __init merge_note_headers_elf32(char *elfptr, size_t *elfsz,
637 char **notes_buf, size_t *notes_sz)
638{
639 int i, nr_ptnote=0, rc=0;
640 char *tmp;
641 Elf32_Ehdr *ehdr_ptr;
642 Elf32_Phdr phdr;
643 u64 phdr_sz = 0, note_off;
644
645 ehdr_ptr = (Elf32_Ehdr *)elfptr;
646
647 rc = update_note_header_size_elf32(ehdr_ptr);
648 if (rc < 0)
649 return rc;
650
651 rc = get_note_number_and_size_elf32(ehdr_ptr, &nr_ptnote, &phdr_sz);
652 if (rc < 0)
653 return rc;
654
655 *notes_sz = roundup(phdr_sz, PAGE_SIZE);
HATAYAMA Daisuke83086972013-07-03 15:02:23 -0700656 *notes_buf = alloc_elfnotes_buf(*notes_sz);
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700657 if (!*notes_buf)
658 return -ENOMEM;
659
660 rc = copy_notes_elf32(ehdr_ptr, *notes_buf);
661 if (rc < 0)
662 return rc;
663
Vivek Goyal72658e92005-06-25 14:58:22 -0700664 /* Prepare merged PT_NOTE program header. */
665 phdr.p_type = PT_NOTE;
666 phdr.p_flags = 0;
667 note_off = sizeof(Elf32_Ehdr) +
668 (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf32_Phdr);
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700669 phdr.p_offset = roundup(note_off, PAGE_SIZE);
Vivek Goyal72658e92005-06-25 14:58:22 -0700670 phdr.p_vaddr = phdr.p_paddr = 0;
671 phdr.p_filesz = phdr.p_memsz = phdr_sz;
672 phdr.p_align = 0;
673
674 /* Add merged PT_NOTE program header*/
675 tmp = elfptr + sizeof(Elf32_Ehdr);
676 memcpy(tmp, &phdr, sizeof(phdr));
677 tmp += sizeof(phdr);
678
679 /* Remove unwanted PT_NOTE program headers. */
680 i = (nr_ptnote - 1) * sizeof(Elf32_Phdr);
681 *elfsz = *elfsz - i;
682 memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf32_Ehdr)-sizeof(Elf32_Phdr)));
HATAYAMA Daisukef2bdacd2013-07-03 15:02:14 -0700683 memset(elfptr + *elfsz, 0, i);
684 *elfsz = roundup(*elfsz, PAGE_SIZE);
Vivek Goyal72658e92005-06-25 14:58:22 -0700685
686 /* Modify e_phnum to reflect merged headers. */
687 ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1;
688
689 return 0;
690}
691
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700692/* Add memory chunks represented by program headers to vmcore list. Also update
693 * the new offset fields of exported program headers. */
694static int __init process_ptload_program_headers_elf64(char *elfptr,
695 size_t elfsz,
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700696 size_t elfnotes_sz,
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700697 struct list_head *vc_list)
698{
699 int i;
700 Elf64_Ehdr *ehdr_ptr;
701 Elf64_Phdr *phdr_ptr;
702 loff_t vmcore_off;
703 struct vmcore *new;
704
705 ehdr_ptr = (Elf64_Ehdr *)elfptr;
706 phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr)); /* PT_NOTE hdr */
707
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700708 /* Skip Elf header, program headers and Elf note segment. */
709 vmcore_off = elfsz + elfnotes_sz;
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700710
711 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
HATAYAMA Daisuke7f614cd2013-07-03 15:02:15 -0700712 u64 paddr, start, end, size;
713
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700714 if (phdr_ptr->p_type != PT_LOAD)
715 continue;
716
HATAYAMA Daisuke7f614cd2013-07-03 15:02:15 -0700717 paddr = phdr_ptr->p_offset;
718 start = rounddown(paddr, PAGE_SIZE);
719 end = roundup(paddr + phdr_ptr->p_memsz, PAGE_SIZE);
720 size = end - start;
721
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700722 /* Add this contiguous chunk of memory to vmcore list.*/
723 new = get_new_element();
724 if (!new)
725 return -ENOMEM;
HATAYAMA Daisuke7f614cd2013-07-03 15:02:15 -0700726 new->paddr = start;
727 new->size = size;
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700728 list_add_tail(&new->list, vc_list);
729
730 /* Update the program header offset. */
HATAYAMA Daisuke7f614cd2013-07-03 15:02:15 -0700731 phdr_ptr->p_offset = vmcore_off + (paddr - start);
732 vmcore_off = vmcore_off + size;
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700733 }
734 return 0;
735}
736
Vivek Goyal72658e92005-06-25 14:58:22 -0700737static int __init process_ptload_program_headers_elf32(char *elfptr,
738 size_t elfsz,
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700739 size_t elfnotes_sz,
Vivek Goyal72658e92005-06-25 14:58:22 -0700740 struct list_head *vc_list)
741{
742 int i;
743 Elf32_Ehdr *ehdr_ptr;
744 Elf32_Phdr *phdr_ptr;
745 loff_t vmcore_off;
746 struct vmcore *new;
747
748 ehdr_ptr = (Elf32_Ehdr *)elfptr;
749 phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr)); /* PT_NOTE hdr */
750
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700751 /* Skip Elf header, program headers and Elf note segment. */
752 vmcore_off = elfsz + elfnotes_sz;
Vivek Goyal72658e92005-06-25 14:58:22 -0700753
754 for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) {
HATAYAMA Daisuke7f614cd2013-07-03 15:02:15 -0700755 u64 paddr, start, end, size;
756
Vivek Goyal72658e92005-06-25 14:58:22 -0700757 if (phdr_ptr->p_type != PT_LOAD)
758 continue;
759
HATAYAMA Daisuke7f614cd2013-07-03 15:02:15 -0700760 paddr = phdr_ptr->p_offset;
761 start = rounddown(paddr, PAGE_SIZE);
762 end = roundup(paddr + phdr_ptr->p_memsz, PAGE_SIZE);
763 size = end - start;
764
Vivek Goyal72658e92005-06-25 14:58:22 -0700765 /* Add this contiguous chunk of memory to vmcore list.*/
766 new = get_new_element();
767 if (!new)
768 return -ENOMEM;
HATAYAMA Daisuke7f614cd2013-07-03 15:02:15 -0700769 new->paddr = start;
770 new->size = size;
Vivek Goyal72658e92005-06-25 14:58:22 -0700771 list_add_tail(&new->list, vc_list);
772
773 /* Update the program header offset */
HATAYAMA Daisuke7f614cd2013-07-03 15:02:15 -0700774 phdr_ptr->p_offset = vmcore_off + (paddr - start);
775 vmcore_off = vmcore_off + size;
Vivek Goyal72658e92005-06-25 14:58:22 -0700776 }
777 return 0;
778}
779
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700780/* Sets offset fields of vmcore elements. */
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700781static void __init set_vmcore_list_offsets(size_t elfsz, size_t elfnotes_sz,
HATAYAMA Daisukef2bdacd2013-07-03 15:02:14 -0700782 struct list_head *vc_list)
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700783{
784 loff_t vmcore_off;
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700785 struct vmcore *m;
786
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700787 /* Skip Elf header, program headers and Elf note segment. */
788 vmcore_off = elfsz + elfnotes_sz;
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700789
790 list_for_each_entry(m, vc_list, list) {
791 m->offset = vmcore_off;
792 vmcore_off += m->size;
793 }
794}
795
HATAYAMA Daisukef2bdacd2013-07-03 15:02:14 -0700796static void free_elfcorebuf(void)
Vivek Goyal72658e92005-06-25 14:58:22 -0700797{
HATAYAMA Daisukef2bdacd2013-07-03 15:02:14 -0700798 free_pages((unsigned long)elfcorebuf, get_order(elfcorebuf_sz_orig));
799 elfcorebuf = NULL;
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700800 vfree(elfnotes_buf);
801 elfnotes_buf = NULL;
Vivek Goyal72658e92005-06-25 14:58:22 -0700802}
803
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700804static int __init parse_crash_elf64_headers(void)
805{
806 int rc=0;
807 Elf64_Ehdr ehdr;
808 u64 addr;
809
810 addr = elfcorehdr_addr;
811
812 /* Read Elf header */
813 rc = read_from_oldmem((char*)&ehdr, sizeof(Elf64_Ehdr), &addr, 0);
814 if (rc < 0)
815 return rc;
816
817 /* Do some basic Verification. */
818 if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
819 (ehdr.e_type != ET_CORE) ||
Mika Westerberg9833c392010-11-19 09:29:24 +0100820 !vmcore_elf64_check_arch(&ehdr) ||
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700821 ehdr.e_ident[EI_CLASS] != ELFCLASS64 ||
822 ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
823 ehdr.e_version != EV_CURRENT ||
824 ehdr.e_ehsize != sizeof(Elf64_Ehdr) ||
825 ehdr.e_phentsize != sizeof(Elf64_Phdr) ||
826 ehdr.e_phnum == 0) {
Andrew Morton87ebdc02013-02-27 17:03:16 -0800827 pr_warn("Warning: Core image elf header is not sane\n");
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700828 return -EINVAL;
829 }
830
831 /* Read in all elf headers. */
HATAYAMA Daisukef2bdacd2013-07-03 15:02:14 -0700832 elfcorebuf_sz_orig = sizeof(Elf64_Ehdr) +
833 ehdr.e_phnum * sizeof(Elf64_Phdr);
834 elfcorebuf_sz = elfcorebuf_sz_orig;
835 elfcorebuf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
836 get_order(elfcorebuf_sz_orig));
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700837 if (!elfcorebuf)
838 return -ENOMEM;
839 addr = elfcorehdr_addr;
HATAYAMA Daisukef2bdacd2013-07-03 15:02:14 -0700840 rc = read_from_oldmem(elfcorebuf, elfcorebuf_sz_orig, &addr, 0);
841 if (rc < 0)
842 goto fail;
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700843
844 /* Merge all PT_NOTE headers into one. */
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700845 rc = merge_note_headers_elf64(elfcorebuf, &elfcorebuf_sz,
846 &elfnotes_buf, &elfnotes_sz);
HATAYAMA Daisukef2bdacd2013-07-03 15:02:14 -0700847 if (rc)
848 goto fail;
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700849 rc = process_ptload_program_headers_elf64(elfcorebuf, elfcorebuf_sz,
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700850 elfnotes_sz, &vmcore_list);
HATAYAMA Daisukef2bdacd2013-07-03 15:02:14 -0700851 if (rc)
852 goto fail;
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700853 set_vmcore_list_offsets(elfcorebuf_sz, elfnotes_sz, &vmcore_list);
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700854 return 0;
HATAYAMA Daisukef2bdacd2013-07-03 15:02:14 -0700855fail:
856 free_elfcorebuf();
857 return rc;
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700858}
859
Vivek Goyal72658e92005-06-25 14:58:22 -0700860static int __init parse_crash_elf32_headers(void)
861{
862 int rc=0;
863 Elf32_Ehdr ehdr;
864 u64 addr;
865
866 addr = elfcorehdr_addr;
867
868 /* Read Elf header */
869 rc = read_from_oldmem((char*)&ehdr, sizeof(Elf32_Ehdr), &addr, 0);
870 if (rc < 0)
871 return rc;
872
873 /* Do some basic Verification. */
874 if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
875 (ehdr.e_type != ET_CORE) ||
876 !elf_check_arch(&ehdr) ||
877 ehdr.e_ident[EI_CLASS] != ELFCLASS32||
878 ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
879 ehdr.e_version != EV_CURRENT ||
880 ehdr.e_ehsize != sizeof(Elf32_Ehdr) ||
881 ehdr.e_phentsize != sizeof(Elf32_Phdr) ||
882 ehdr.e_phnum == 0) {
Andrew Morton87ebdc02013-02-27 17:03:16 -0800883 pr_warn("Warning: Core image elf header is not sane\n");
Vivek Goyal72658e92005-06-25 14:58:22 -0700884 return -EINVAL;
885 }
886
887 /* Read in all elf headers. */
HATAYAMA Daisukef2bdacd2013-07-03 15:02:14 -0700888 elfcorebuf_sz_orig = sizeof(Elf32_Ehdr) + ehdr.e_phnum * sizeof(Elf32_Phdr);
889 elfcorebuf_sz = elfcorebuf_sz_orig;
890 elfcorebuf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
891 get_order(elfcorebuf_sz_orig));
Vivek Goyal72658e92005-06-25 14:58:22 -0700892 if (!elfcorebuf)
893 return -ENOMEM;
894 addr = elfcorehdr_addr;
HATAYAMA Daisukef2bdacd2013-07-03 15:02:14 -0700895 rc = read_from_oldmem(elfcorebuf, elfcorebuf_sz_orig, &addr, 0);
896 if (rc < 0)
897 goto fail;
Vivek Goyal72658e92005-06-25 14:58:22 -0700898
899 /* Merge all PT_NOTE headers into one. */
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700900 rc = merge_note_headers_elf32(elfcorebuf, &elfcorebuf_sz,
901 &elfnotes_buf, &elfnotes_sz);
HATAYAMA Daisukef2bdacd2013-07-03 15:02:14 -0700902 if (rc)
903 goto fail;
Vivek Goyal72658e92005-06-25 14:58:22 -0700904 rc = process_ptload_program_headers_elf32(elfcorebuf, elfcorebuf_sz,
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700905 elfnotes_sz, &vmcore_list);
HATAYAMA Daisukef2bdacd2013-07-03 15:02:14 -0700906 if (rc)
907 goto fail;
HATAYAMA Daisuke087350c2013-07-03 15:02:19 -0700908 set_vmcore_list_offsets(elfcorebuf_sz, elfnotes_sz, &vmcore_list);
Vivek Goyal72658e92005-06-25 14:58:22 -0700909 return 0;
HATAYAMA Daisukef2bdacd2013-07-03 15:02:14 -0700910fail:
911 free_elfcorebuf();
912 return rc;
Vivek Goyal72658e92005-06-25 14:58:22 -0700913}
914
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700915static int __init parse_crash_elf_headers(void)
916{
917 unsigned char e_ident[EI_NIDENT];
918 u64 addr;
919 int rc=0;
920
921 addr = elfcorehdr_addr;
922 rc = read_from_oldmem(e_ident, EI_NIDENT, &addr, 0);
923 if (rc < 0)
924 return rc;
925 if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) {
Andrew Morton87ebdc02013-02-27 17:03:16 -0800926 pr_warn("Warning: Core image elf header not found\n");
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700927 return -EINVAL;
928 }
929
930 if (e_ident[EI_CLASS] == ELFCLASS64) {
931 rc = parse_crash_elf64_headers();
932 if (rc)
933 return rc;
Vivek Goyal72658e92005-06-25 14:58:22 -0700934 } else if (e_ident[EI_CLASS] == ELFCLASS32) {
935 rc = parse_crash_elf32_headers();
936 if (rc)
937 return rc;
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700938 } else {
Andrew Morton87ebdc02013-02-27 17:03:16 -0800939 pr_warn("Warning: Core image elf header is not sane\n");
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700940 return -EINVAL;
941 }
HATAYAMA Daisuke591ff712013-07-03 15:02:22 -0700942
943 /* Determine vmcore size. */
944 vmcore_size = get_vmcore_size(elfcorebuf_sz, elfnotes_sz,
945 &vmcore_list);
946
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700947 return 0;
948}
949
950/* Init function for vmcore module. */
951static int __init vmcore_init(void)
952{
953 int rc = 0;
954
955 /* If elfcorehdr= has been passed in cmdline, then capture the dump.*/
Simon Horman85a0ee32008-10-18 20:28:29 -0700956 if (!(is_vmcore_usable()))
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700957 return rc;
958 rc = parse_crash_elf_headers();
959 if (rc) {
Andrew Morton87ebdc02013-02-27 17:03:16 -0800960 pr_warn("Kdump: vmcore not initialized\n");
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700961 return rc;
962 }
963
Alexey Dobriyan5aa140c2008-10-06 14:36:31 +0400964 proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations);
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700965 if (proc_vmcore)
966 proc_vmcore->size = vmcore_size;
967 return 0;
968}
969module_init(vmcore_init)
Mahesh Salgaonkar162573932012-02-16 01:15:00 +0000970
971/* Cleanup function for vmcore module. */
972void vmcore_cleanup(void)
973{
974 struct list_head *pos, *next;
975
976 if (proc_vmcore) {
David Howellsa8ca16e2013-04-12 17:27:28 +0100977 proc_remove(proc_vmcore);
Mahesh Salgaonkar162573932012-02-16 01:15:00 +0000978 proc_vmcore = NULL;
979 }
980
981 /* clear the vmcore list. */
982 list_for_each_safe(pos, next, &vmcore_list) {
983 struct vmcore *m;
984
985 m = list_entry(pos, struct vmcore, list);
986 list_del(&m->list);
987 kfree(m);
988 }
HATAYAMA Daisukef2bdacd2013-07-03 15:02:14 -0700989 free_elfcorebuf();
Mahesh Salgaonkar162573932012-02-16 01:15:00 +0000990}
991EXPORT_SYMBOL_GPL(vmcore_cleanup);