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