1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Firmware Assisted dump: A robust mechanism to get reliable kernel crash 4 * dump with assistance from firmware. This approach does not use kexec, 5 * instead firmware assists in booting the kdump kernel while preserving 6 * memory contents. The most of the code implementation has been adapted 7 * from phyp assisted dump implementation written by Linas Vepstas and 8 * Manish Ahuja 9 * 10 * Copyright 2011 IBM Corporation 11 * Author: Mahesh Salgaonkar <mahesh@linux.vnet.ibm.com> 12 */ 13 14 #undef DEBUG 15 #define pr_fmt(fmt) "fadump: " fmt 16 17 #include <linux/string.h> 18 #include <linux/memblock.h> 19 #include <linux/delay.h> 20 #include <linux/seq_file.h> 21 #include <linux/crash_dump.h> 22 #include <linux/kobject.h> 23 #include <linux/sysfs.h> 24 #include <linux/slab.h> 25 #include <linux/cma.h> 26 #include <linux/hugetlb.h> 27 28 #include <asm/debugfs.h> 29 #include <asm/page.h> 30 #include <asm/prom.h> 31 #include <asm/fadump.h> 32 #include <asm/fadump-internal.h> 33 #include <asm/setup.h> 34 35 static struct fw_dump fw_dump; 36 37 static DEFINE_MUTEX(fadump_mutex); 38 struct fad_crash_memory_ranges *crash_memory_ranges; 39 int crash_memory_ranges_size; 40 int crash_mem_ranges; 41 int max_crash_mem_ranges; 42 43 #ifdef CONFIG_CMA 44 static struct cma *fadump_cma; 45 46 /* 47 * fadump_cma_init() - Initialize CMA area from a fadump reserved memory 48 * 49 * This function initializes CMA area from fadump reserved memory. 50 * The total size of fadump reserved memory covers for boot memory size 51 * + cpu data size + hpte size and metadata. 52 * Initialize only the area equivalent to boot memory size for CMA use. 53 * The reamining portion of fadump reserved memory will be not given 54 * to CMA and pages for thoes will stay reserved. boot memory size is 55 * aligned per CMA requirement to satisy cma_init_reserved_mem() call. 56 * But for some reason even if it fails we still have the memory reservation 57 * with us and we can still continue doing fadump. 58 */ 59 int __init fadump_cma_init(void) 60 { 61 unsigned long long base, size; 62 int rc; 63 64 if (!fw_dump.fadump_enabled) 65 return 0; 66 67 /* 68 * Do not use CMA if user has provided fadump=nocma kernel parameter. 69 * Return 1 to continue with fadump old behaviour. 70 */ 71 if (fw_dump.nocma) 72 return 1; 73 74 base = fw_dump.reserve_dump_area_start; 75 size = fw_dump.boot_memory_size; 76 77 if (!size) 78 return 0; 79 80 rc = cma_init_reserved_mem(base, size, 0, "fadump_cma", &fadump_cma); 81 if (rc) { 82 pr_err("Failed to init cma area for firmware-assisted dump,%d\n", rc); 83 /* 84 * Though the CMA init has failed we still have memory 85 * reservation with us. The reserved memory will be 86 * blocked from production system usage. Hence return 1, 87 * so that we can continue with fadump. 88 */ 89 return 1; 90 } 91 92 /* 93 * So we now have successfully initialized cma area for fadump. 94 */ 95 pr_info("Initialized 0x%lx bytes cma area at %ldMB from 0x%lx " 96 "bytes of memory reserved for firmware-assisted dump\n", 97 cma_get_size(fadump_cma), 98 (unsigned long)cma_get_base(fadump_cma) >> 20, 99 fw_dump.reserve_dump_area_size); 100 return 1; 101 } 102 #else 103 static int __init fadump_cma_init(void) { return 1; } 104 #endif /* CONFIG_CMA */ 105 106 /* Scan the Firmware Assisted dump configuration details. */ 107 int __init early_init_dt_scan_fw_dump(unsigned long node, const char *uname, 108 int depth, void *data) 109 { 110 if (depth != 1) 111 return 0; 112 113 if (strcmp(uname, "rtas") == 0) { 114 rtas_fadump_dt_scan(&fw_dump, node); 115 return 1; 116 } 117 118 if (strcmp(uname, "ibm,opal") == 0) { 119 opal_fadump_dt_scan(&fw_dump, node); 120 return 1; 121 } 122 123 return 0; 124 } 125 126 /* 127 * If fadump is registered, check if the memory provided 128 * falls within boot memory area and reserved memory area. 129 */ 130 int is_fadump_memory_area(u64 addr, ulong size) 131 { 132 u64 d_start = fw_dump.reserve_dump_area_start; 133 u64 d_end = d_start + fw_dump.reserve_dump_area_size; 134 135 if (!fw_dump.dump_registered) 136 return 0; 137 138 if (((addr + size) > d_start) && (addr <= d_end)) 139 return 1; 140 141 return (addr + size) > RMA_START && addr <= fw_dump.boot_memory_size; 142 } 143 144 int should_fadump_crash(void) 145 { 146 if (!fw_dump.dump_registered || !fw_dump.fadumphdr_addr) 147 return 0; 148 return 1; 149 } 150 151 int is_fadump_active(void) 152 { 153 return fw_dump.dump_active; 154 } 155 156 /* 157 * Returns true, if there are no holes in memory area between d_start to d_end, 158 * false otherwise. 159 */ 160 static bool is_fadump_mem_area_contiguous(u64 d_start, u64 d_end) 161 { 162 struct memblock_region *reg; 163 bool ret = false; 164 u64 start, end; 165 166 for_each_memblock(memory, reg) { 167 start = max_t(u64, d_start, reg->base); 168 end = min_t(u64, d_end, (reg->base + reg->size)); 169 if (d_start < end) { 170 /* Memory hole from d_start to start */ 171 if (start > d_start) 172 break; 173 174 if (end == d_end) { 175 ret = true; 176 break; 177 } 178 179 d_start = end + 1; 180 } 181 } 182 183 return ret; 184 } 185 186 /* 187 * Returns true, if there are no holes in boot memory area, 188 * false otherwise. 189 */ 190 bool is_fadump_boot_mem_contiguous(void) 191 { 192 return is_fadump_mem_area_contiguous(0, fw_dump.boot_memory_size); 193 } 194 195 /* 196 * Returns true, if there are no holes in reserved memory area, 197 * false otherwise. 198 */ 199 bool is_fadump_reserved_mem_contiguous(void) 200 { 201 u64 d_start, d_end; 202 203 d_start = fw_dump.reserve_dump_area_start; 204 d_end = d_start + fw_dump.reserve_dump_area_size; 205 return is_fadump_mem_area_contiguous(d_start, d_end); 206 } 207 208 /* Print firmware assisted dump configurations for debugging purpose. */ 209 static void fadump_show_config(void) 210 { 211 pr_debug("Support for firmware-assisted dump (fadump): %s\n", 212 (fw_dump.fadump_supported ? "present" : "no support")); 213 214 if (!fw_dump.fadump_supported) 215 return; 216 217 pr_debug("Fadump enabled : %s\n", 218 (fw_dump.fadump_enabled ? "yes" : "no")); 219 pr_debug("Dump Active : %s\n", 220 (fw_dump.dump_active ? "yes" : "no")); 221 pr_debug("Dump section sizes:\n"); 222 pr_debug(" CPU state data size: %lx\n", fw_dump.cpu_state_data_size); 223 pr_debug(" HPTE region size : %lx\n", fw_dump.hpte_region_size); 224 pr_debug("Boot memory size : %lx\n", fw_dump.boot_memory_size); 225 } 226 227 /** 228 * fadump_calculate_reserve_size(): reserve variable boot area 5% of System RAM 229 * 230 * Function to find the largest memory size we need to reserve during early 231 * boot process. This will be the size of the memory that is required for a 232 * kernel to boot successfully. 233 * 234 * This function has been taken from phyp-assisted dump feature implementation. 235 * 236 * returns larger of 256MB or 5% rounded down to multiples of 256MB. 237 * 238 * TODO: Come up with better approach to find out more accurate memory size 239 * that is required for a kernel to boot successfully. 240 * 241 */ 242 static inline unsigned long fadump_calculate_reserve_size(void) 243 { 244 int ret; 245 unsigned long long base, size; 246 247 if (fw_dump.reserve_bootvar) 248 pr_warn("'fadump_reserve_mem=' parameter is deprecated in favor of 'crashkernel=' parameter.\n"); 249 250 /* 251 * Check if the size is specified through crashkernel= cmdline 252 * option. If yes, then use that but ignore base as fadump reserves 253 * memory at a predefined offset. 254 */ 255 ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(), 256 &size, &base); 257 if (ret == 0 && size > 0) { 258 unsigned long max_size; 259 260 if (fw_dump.reserve_bootvar) 261 pr_info("Using 'crashkernel=' parameter for memory reservation.\n"); 262 263 fw_dump.reserve_bootvar = (unsigned long)size; 264 265 /* 266 * Adjust if the boot memory size specified is above 267 * the upper limit. 268 */ 269 max_size = memblock_phys_mem_size() / MAX_BOOT_MEM_RATIO; 270 if (fw_dump.reserve_bootvar > max_size) { 271 fw_dump.reserve_bootvar = max_size; 272 pr_info("Adjusted boot memory size to %luMB\n", 273 (fw_dump.reserve_bootvar >> 20)); 274 } 275 276 return fw_dump.reserve_bootvar; 277 } else if (fw_dump.reserve_bootvar) { 278 /* 279 * 'fadump_reserve_mem=' is being used to reserve memory 280 * for firmware-assisted dump. 281 */ 282 return fw_dump.reserve_bootvar; 283 } 284 285 /* divide by 20 to get 5% of value */ 286 size = memblock_phys_mem_size() / 20; 287 288 /* round it down in multiples of 256 */ 289 size = size & ~0x0FFFFFFFUL; 290 291 /* Truncate to memory_limit. We don't want to over reserve the memory.*/ 292 if (memory_limit && size > memory_limit) 293 size = memory_limit; 294 295 return (size > MIN_BOOT_MEM ? size : MIN_BOOT_MEM); 296 } 297 298 /* 299 * Calculate the total memory size required to be reserved for 300 * firmware-assisted dump registration. 301 */ 302 static unsigned long get_fadump_area_size(void) 303 { 304 unsigned long size = 0; 305 306 size += fw_dump.cpu_state_data_size; 307 size += fw_dump.hpte_region_size; 308 size += fw_dump.boot_memory_size; 309 size += sizeof(struct fadump_crash_info_header); 310 size += sizeof(struct elfhdr); /* ELF core header.*/ 311 size += sizeof(struct elf_phdr); /* place holder for cpu notes */ 312 /* Program headers for crash memory regions. */ 313 size += sizeof(struct elf_phdr) * (memblock_num_regions(memory) + 2); 314 315 size = PAGE_ALIGN(size); 316 return size; 317 } 318 319 static void __init fadump_reserve_crash_area(unsigned long base, 320 unsigned long size) 321 { 322 struct memblock_region *reg; 323 unsigned long mstart, mend, msize; 324 325 for_each_memblock(memory, reg) { 326 mstart = max_t(unsigned long, base, reg->base); 327 mend = reg->base + reg->size; 328 mend = min(base + size, mend); 329 330 if (mstart < mend) { 331 msize = mend - mstart; 332 memblock_reserve(mstart, msize); 333 pr_info("Reserved %ldMB of memory at %#016lx for saving crash dump\n", 334 (msize >> 20), mstart); 335 } 336 } 337 } 338 339 int __init fadump_reserve_mem(void) 340 { 341 u64 base, size, mem_boundary; 342 int ret = 1; 343 344 if (!fw_dump.fadump_enabled) 345 return 0; 346 347 if (!fw_dump.fadump_supported) { 348 pr_info("Firmware-Assisted Dump is not supported on this hardware\n"); 349 goto error_out; 350 } 351 /* 352 * Initialize boot memory size 353 * If dump is active then we have already calculated the size during 354 * first kernel. 355 */ 356 if (!fw_dump.dump_active) { 357 fw_dump.boot_memory_size = 358 PAGE_ALIGN(fadump_calculate_reserve_size()); 359 #ifdef CONFIG_CMA 360 if (!fw_dump.nocma) 361 fw_dump.boot_memory_size = 362 ALIGN(fw_dump.boot_memory_size, 363 FADUMP_CMA_ALIGNMENT); 364 #endif 365 } 366 367 /* 368 * Calculate the memory boundary. 369 * If memory_limit is less than actual memory boundary then reserve 370 * the memory for fadump beyond the memory_limit and adjust the 371 * memory_limit accordingly, so that the running kernel can run with 372 * specified memory_limit. 373 */ 374 if (memory_limit && memory_limit < memblock_end_of_DRAM()) { 375 size = get_fadump_area_size(); 376 if ((memory_limit + size) < memblock_end_of_DRAM()) 377 memory_limit += size; 378 else 379 memory_limit = memblock_end_of_DRAM(); 380 printk(KERN_INFO "Adjusted memory_limit for firmware-assisted" 381 " dump, now %#016llx\n", memory_limit); 382 } 383 if (memory_limit) 384 mem_boundary = memory_limit; 385 else 386 mem_boundary = memblock_end_of_DRAM(); 387 388 base = fw_dump.boot_memory_size; 389 size = get_fadump_area_size(); 390 fw_dump.reserve_dump_area_size = size; 391 if (fw_dump.dump_active) { 392 pr_info("Firmware-assisted dump is active.\n"); 393 394 #ifdef CONFIG_HUGETLB_PAGE 395 /* 396 * FADump capture kernel doesn't care much about hugepages. 397 * In fact, handling hugepages in capture kernel is asking for 398 * trouble. So, disable HugeTLB support when fadump is active. 399 */ 400 hugetlb_disabled = true; 401 #endif 402 /* 403 * If last boot has crashed then reserve all the memory 404 * above boot_memory_size so that we don't touch it until 405 * dump is written to disk by userspace tool. This memory 406 * will be released for general use once the dump is saved. 407 */ 408 size = mem_boundary - base; 409 fadump_reserve_crash_area(base, size); 410 411 pr_debug("fadumphdr_addr = %#016lx\n", fw_dump.fadumphdr_addr); 412 pr_debug("Reserve dump area start address: 0x%lx\n", 413 fw_dump.reserve_dump_area_start); 414 } else { 415 /* 416 * Reserve memory at an offset closer to bottom of the RAM to 417 * minimize the impact of memory hot-remove operation. We can't 418 * use memblock_find_in_range() here since it doesn't allocate 419 * from bottom to top. 420 */ 421 while (base <= (mem_boundary - size)) { 422 if (memblock_is_region_memory(base, size) && 423 !memblock_is_region_reserved(base, size)) 424 break; 425 426 base += size; 427 } 428 429 if ((base > (mem_boundary - size)) || 430 memblock_reserve(base, size)) { 431 pr_err("Failed to reserve memory!\n"); 432 goto error_out; 433 } 434 435 pr_info("Reserved %lldMB of memory at %#016llx (System RAM: %lldMB)\n", 436 (size >> 20), base, (memblock_phys_mem_size() >> 20)); 437 438 fw_dump.reserve_dump_area_start = base; 439 ret = fadump_cma_init(); 440 } 441 442 return ret; 443 error_out: 444 fw_dump.fadump_enabled = 0; 445 return 0; 446 } 447 448 unsigned long __init arch_reserved_kernel_pages(void) 449 { 450 return memblock_reserved_size() / PAGE_SIZE; 451 } 452 453 /* Look for fadump= cmdline option. */ 454 static int __init early_fadump_param(char *p) 455 { 456 if (!p) 457 return 1; 458 459 if (strncmp(p, "on", 2) == 0) 460 fw_dump.fadump_enabled = 1; 461 else if (strncmp(p, "off", 3) == 0) 462 fw_dump.fadump_enabled = 0; 463 else if (strncmp(p, "nocma", 5) == 0) { 464 fw_dump.fadump_enabled = 1; 465 fw_dump.nocma = 1; 466 } 467 468 return 0; 469 } 470 early_param("fadump", early_fadump_param); 471 472 /* 473 * Look for fadump_reserve_mem= cmdline option 474 * TODO: Remove references to 'fadump_reserve_mem=' parameter, 475 * the sooner 'crashkernel=' parameter is accustomed to. 476 */ 477 static int __init early_fadump_reserve_mem(char *p) 478 { 479 if (p) 480 fw_dump.reserve_bootvar = memparse(p, &p); 481 return 0; 482 } 483 early_param("fadump_reserve_mem", early_fadump_reserve_mem); 484 485 void crash_fadump(struct pt_regs *regs, const char *str) 486 { 487 struct fadump_crash_info_header *fdh = NULL; 488 int old_cpu, this_cpu; 489 490 if (!should_fadump_crash()) 491 return; 492 493 /* 494 * old_cpu == -1 means this is the first CPU which has come here, 495 * go ahead and trigger fadump. 496 * 497 * old_cpu != -1 means some other CPU has already on it's way 498 * to trigger fadump, just keep looping here. 499 */ 500 this_cpu = smp_processor_id(); 501 old_cpu = cmpxchg(&crashing_cpu, -1, this_cpu); 502 503 if (old_cpu != -1) { 504 /* 505 * We can't loop here indefinitely. Wait as long as fadump 506 * is in force. If we race with fadump un-registration this 507 * loop will break and then we go down to normal panic path 508 * and reboot. If fadump is in force the first crashing 509 * cpu will definitely trigger fadump. 510 */ 511 while (fw_dump.dump_registered) 512 cpu_relax(); 513 return; 514 } 515 516 fdh = __va(fw_dump.fadumphdr_addr); 517 fdh->crashing_cpu = crashing_cpu; 518 crash_save_vmcoreinfo(); 519 520 if (regs) 521 fdh->regs = *regs; 522 else 523 ppc_save_regs(&fdh->regs); 524 525 fdh->online_mask = *cpu_online_mask; 526 527 fw_dump.ops->fadump_trigger(fdh, str); 528 } 529 530 u32 *fadump_regs_to_elf_notes(u32 *buf, struct pt_regs *regs) 531 { 532 struct elf_prstatus prstatus; 533 534 memset(&prstatus, 0, sizeof(prstatus)); 535 /* 536 * FIXME: How do i get PID? Do I really need it? 537 * prstatus.pr_pid = ???? 538 */ 539 elf_core_copy_kernel_regs(&prstatus.pr_reg, regs); 540 buf = append_elf_note(buf, CRASH_CORE_NOTE_NAME, NT_PRSTATUS, 541 &prstatus, sizeof(prstatus)); 542 return buf; 543 } 544 545 void fadump_update_elfcore_header(char *bufp) 546 { 547 struct elfhdr *elf; 548 struct elf_phdr *phdr; 549 550 elf = (struct elfhdr *)bufp; 551 bufp += sizeof(struct elfhdr); 552 553 /* First note is a place holder for cpu notes info. */ 554 phdr = (struct elf_phdr *)bufp; 555 556 if (phdr->p_type == PT_NOTE) { 557 phdr->p_paddr = __pa(fw_dump.cpu_notes_buf_vaddr); 558 phdr->p_offset = phdr->p_paddr; 559 phdr->p_filesz = fw_dump.cpu_notes_buf_size; 560 phdr->p_memsz = fw_dump.cpu_notes_buf_size; 561 } 562 return; 563 } 564 565 static void *fadump_alloc_buffer(unsigned long size) 566 { 567 unsigned long count, i; 568 struct page *page; 569 void *vaddr; 570 571 vaddr = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO); 572 if (!vaddr) 573 return NULL; 574 575 count = PAGE_ALIGN(size) / PAGE_SIZE; 576 page = virt_to_page(vaddr); 577 for (i = 0; i < count; i++) 578 mark_page_reserved(page + i); 579 return vaddr; 580 } 581 582 static void fadump_free_buffer(unsigned long vaddr, unsigned long size) 583 { 584 free_reserved_area((void *)vaddr, (void *)(vaddr + size), -1, NULL); 585 } 586 587 s32 fadump_setup_cpu_notes_buf(u32 num_cpus) 588 { 589 /* Allocate buffer to hold cpu crash notes. */ 590 fw_dump.cpu_notes_buf_size = num_cpus * sizeof(note_buf_t); 591 fw_dump.cpu_notes_buf_size = PAGE_ALIGN(fw_dump.cpu_notes_buf_size); 592 fw_dump.cpu_notes_buf_vaddr = 593 (unsigned long)fadump_alloc_buffer(fw_dump.cpu_notes_buf_size); 594 if (!fw_dump.cpu_notes_buf_vaddr) { 595 pr_err("Failed to allocate %ld bytes for CPU notes buffer\n", 596 fw_dump.cpu_notes_buf_size); 597 return -ENOMEM; 598 } 599 600 pr_debug("Allocated buffer for cpu notes of size %ld at 0x%lx\n", 601 fw_dump.cpu_notes_buf_size, 602 fw_dump.cpu_notes_buf_vaddr); 603 return 0; 604 } 605 606 void fadump_free_cpu_notes_buf(void) 607 { 608 if (!fw_dump.cpu_notes_buf_vaddr) 609 return; 610 611 fadump_free_buffer(fw_dump.cpu_notes_buf_vaddr, 612 fw_dump.cpu_notes_buf_size); 613 fw_dump.cpu_notes_buf_vaddr = 0; 614 fw_dump.cpu_notes_buf_size = 0; 615 } 616 617 static void free_crash_memory_ranges(void) 618 { 619 kfree(crash_memory_ranges); 620 crash_memory_ranges = NULL; 621 crash_memory_ranges_size = 0; 622 max_crash_mem_ranges = 0; 623 } 624 625 /* 626 * Allocate or reallocate crash memory ranges array in incremental units 627 * of PAGE_SIZE. 628 */ 629 static int allocate_crash_memory_ranges(void) 630 { 631 struct fad_crash_memory_ranges *new_array; 632 u64 new_size; 633 634 new_size = crash_memory_ranges_size + PAGE_SIZE; 635 pr_debug("Allocating %llu bytes of memory for crash memory ranges\n", 636 new_size); 637 638 new_array = krealloc(crash_memory_ranges, new_size, GFP_KERNEL); 639 if (new_array == NULL) { 640 pr_err("Insufficient memory for setting up crash memory ranges\n"); 641 free_crash_memory_ranges(); 642 return -ENOMEM; 643 } 644 645 crash_memory_ranges = new_array; 646 crash_memory_ranges_size = new_size; 647 max_crash_mem_ranges = (new_size / 648 sizeof(struct fad_crash_memory_ranges)); 649 return 0; 650 } 651 652 static inline int fadump_add_crash_memory(unsigned long long base, 653 unsigned long long end) 654 { 655 u64 start, size; 656 bool is_adjacent = false; 657 658 if (base == end) 659 return 0; 660 661 /* 662 * Fold adjacent memory ranges to bring down the memory ranges/ 663 * PT_LOAD segments count. 664 */ 665 if (crash_mem_ranges) { 666 start = crash_memory_ranges[crash_mem_ranges - 1].base; 667 size = crash_memory_ranges[crash_mem_ranges - 1].size; 668 669 if ((start + size) == base) 670 is_adjacent = true; 671 } 672 if (!is_adjacent) { 673 /* resize the array on reaching the limit */ 674 if (crash_mem_ranges == max_crash_mem_ranges) { 675 int ret; 676 677 ret = allocate_crash_memory_ranges(); 678 if (ret) 679 return ret; 680 } 681 682 start = base; 683 crash_memory_ranges[crash_mem_ranges].base = start; 684 crash_mem_ranges++; 685 } 686 687 crash_memory_ranges[crash_mem_ranges - 1].size = (end - start); 688 pr_debug("crash_memory_range[%d] [%#016llx-%#016llx], %#llx bytes\n", 689 (crash_mem_ranges - 1), start, end - 1, (end - start)); 690 return 0; 691 } 692 693 static int fadump_exclude_reserved_area(unsigned long long start, 694 unsigned long long end) 695 { 696 unsigned long long ra_start, ra_end; 697 int ret = 0; 698 699 ra_start = fw_dump.reserve_dump_area_start; 700 ra_end = ra_start + fw_dump.reserve_dump_area_size; 701 702 if ((ra_start < end) && (ra_end > start)) { 703 if ((start < ra_start) && (end > ra_end)) { 704 ret = fadump_add_crash_memory(start, ra_start); 705 if (ret) 706 return ret; 707 708 ret = fadump_add_crash_memory(ra_end, end); 709 } else if (start < ra_start) { 710 ret = fadump_add_crash_memory(start, ra_start); 711 } else if (ra_end < end) { 712 ret = fadump_add_crash_memory(ra_end, end); 713 } 714 } else 715 ret = fadump_add_crash_memory(start, end); 716 717 return ret; 718 } 719 720 static int fadump_init_elfcore_header(char *bufp) 721 { 722 struct elfhdr *elf; 723 724 elf = (struct elfhdr *) bufp; 725 bufp += sizeof(struct elfhdr); 726 memcpy(elf->e_ident, ELFMAG, SELFMAG); 727 elf->e_ident[EI_CLASS] = ELF_CLASS; 728 elf->e_ident[EI_DATA] = ELF_DATA; 729 elf->e_ident[EI_VERSION] = EV_CURRENT; 730 elf->e_ident[EI_OSABI] = ELF_OSABI; 731 memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD); 732 elf->e_type = ET_CORE; 733 elf->e_machine = ELF_ARCH; 734 elf->e_version = EV_CURRENT; 735 elf->e_entry = 0; 736 elf->e_phoff = sizeof(struct elfhdr); 737 elf->e_shoff = 0; 738 #if defined(_CALL_ELF) 739 elf->e_flags = _CALL_ELF; 740 #else 741 elf->e_flags = 0; 742 #endif 743 elf->e_ehsize = sizeof(struct elfhdr); 744 elf->e_phentsize = sizeof(struct elf_phdr); 745 elf->e_phnum = 0; 746 elf->e_shentsize = 0; 747 elf->e_shnum = 0; 748 elf->e_shstrndx = 0; 749 750 return 0; 751 } 752 753 /* 754 * Traverse through memblock structure and setup crash memory ranges. These 755 * ranges will be used create PT_LOAD program headers in elfcore header. 756 */ 757 static int fadump_setup_crash_memory_ranges(void) 758 { 759 struct memblock_region *reg; 760 unsigned long long start, end; 761 int ret; 762 763 pr_debug("Setup crash memory ranges.\n"); 764 crash_mem_ranges = 0; 765 766 /* 767 * add the first memory chunk (RMA_START through boot_memory_size) as 768 * a separate memory chunk. The reason is, at the time crash firmware 769 * will move the content of this memory chunk to different location 770 * specified during fadump registration. We need to create a separate 771 * program header for this chunk with the correct offset. 772 */ 773 ret = fadump_add_crash_memory(RMA_START, fw_dump.boot_memory_size); 774 if (ret) 775 return ret; 776 777 for_each_memblock(memory, reg) { 778 start = (unsigned long long)reg->base; 779 end = start + (unsigned long long)reg->size; 780 781 /* 782 * skip the first memory chunk that is already added (RMA_START 783 * through boot_memory_size). This logic needs a relook if and 784 * when RMA_START changes to a non-zero value. 785 */ 786 BUILD_BUG_ON(RMA_START != 0); 787 if (start < fw_dump.boot_memory_size) { 788 if (end > fw_dump.boot_memory_size) 789 start = fw_dump.boot_memory_size; 790 else 791 continue; 792 } 793 794 /* add this range excluding the reserved dump area. */ 795 ret = fadump_exclude_reserved_area(start, end); 796 if (ret) 797 return ret; 798 } 799 800 return 0; 801 } 802 803 /* 804 * If the given physical address falls within the boot memory region then 805 * return the relocated address that points to the dump region reserved 806 * for saving initial boot memory contents. 807 */ 808 static inline unsigned long fadump_relocate(unsigned long paddr) 809 { 810 if (paddr > RMA_START && paddr < fw_dump.boot_memory_size) 811 return fw_dump.boot_mem_dest_addr + paddr; 812 else 813 return paddr; 814 } 815 816 static int fadump_create_elfcore_headers(char *bufp) 817 { 818 struct elfhdr *elf; 819 struct elf_phdr *phdr; 820 int i; 821 822 fadump_init_elfcore_header(bufp); 823 elf = (struct elfhdr *)bufp; 824 bufp += sizeof(struct elfhdr); 825 826 /* 827 * setup ELF PT_NOTE, place holder for cpu notes info. The notes info 828 * will be populated during second kernel boot after crash. Hence 829 * this PT_NOTE will always be the first elf note. 830 * 831 * NOTE: Any new ELF note addition should be placed after this note. 832 */ 833 phdr = (struct elf_phdr *)bufp; 834 bufp += sizeof(struct elf_phdr); 835 phdr->p_type = PT_NOTE; 836 phdr->p_flags = 0; 837 phdr->p_vaddr = 0; 838 phdr->p_align = 0; 839 840 phdr->p_offset = 0; 841 phdr->p_paddr = 0; 842 phdr->p_filesz = 0; 843 phdr->p_memsz = 0; 844 845 (elf->e_phnum)++; 846 847 /* setup ELF PT_NOTE for vmcoreinfo */ 848 phdr = (struct elf_phdr *)bufp; 849 bufp += sizeof(struct elf_phdr); 850 phdr->p_type = PT_NOTE; 851 phdr->p_flags = 0; 852 phdr->p_vaddr = 0; 853 phdr->p_align = 0; 854 855 phdr->p_paddr = fadump_relocate(paddr_vmcoreinfo_note()); 856 phdr->p_offset = phdr->p_paddr; 857 phdr->p_memsz = phdr->p_filesz = VMCOREINFO_NOTE_SIZE; 858 859 /* Increment number of program headers. */ 860 (elf->e_phnum)++; 861 862 /* setup PT_LOAD sections. */ 863 864 for (i = 0; i < crash_mem_ranges; i++) { 865 unsigned long long mbase, msize; 866 mbase = crash_memory_ranges[i].base; 867 msize = crash_memory_ranges[i].size; 868 869 if (!msize) 870 continue; 871 872 phdr = (struct elf_phdr *)bufp; 873 bufp += sizeof(struct elf_phdr); 874 phdr->p_type = PT_LOAD; 875 phdr->p_flags = PF_R|PF_W|PF_X; 876 phdr->p_offset = mbase; 877 878 if (mbase == RMA_START) { 879 /* 880 * The entire RMA region will be moved by firmware 881 * to the specified destination_address. Hence set 882 * the correct offset. 883 */ 884 phdr->p_offset = fw_dump.boot_mem_dest_addr; 885 } 886 887 phdr->p_paddr = mbase; 888 phdr->p_vaddr = (unsigned long)__va(mbase); 889 phdr->p_filesz = msize; 890 phdr->p_memsz = msize; 891 phdr->p_align = 0; 892 893 /* Increment number of program headers. */ 894 (elf->e_phnum)++; 895 } 896 return 0; 897 } 898 899 static unsigned long init_fadump_header(unsigned long addr) 900 { 901 struct fadump_crash_info_header *fdh; 902 903 if (!addr) 904 return 0; 905 906 fdh = __va(addr); 907 addr += sizeof(struct fadump_crash_info_header); 908 909 memset(fdh, 0, sizeof(struct fadump_crash_info_header)); 910 fdh->magic_number = FADUMP_CRASH_INFO_MAGIC; 911 fdh->elfcorehdr_addr = addr; 912 /* We will set the crashing cpu id in crash_fadump() during crash. */ 913 fdh->crashing_cpu = FADUMP_CPU_UNKNOWN; 914 915 return addr; 916 } 917 918 static int register_fadump(void) 919 { 920 unsigned long addr; 921 void *vaddr; 922 int ret; 923 924 /* 925 * If no memory is reserved then we can not register for firmware- 926 * assisted dump. 927 */ 928 if (!fw_dump.reserve_dump_area_size) 929 return -ENODEV; 930 931 ret = fadump_setup_crash_memory_ranges(); 932 if (ret) 933 return ret; 934 935 addr = fw_dump.fadumphdr_addr; 936 937 /* Initialize fadump crash info header. */ 938 addr = init_fadump_header(addr); 939 vaddr = __va(addr); 940 941 pr_debug("Creating ELF core headers at %#016lx\n", addr); 942 fadump_create_elfcore_headers(vaddr); 943 944 /* register the future kernel dump with firmware. */ 945 pr_debug("Registering for firmware-assisted kernel dump...\n"); 946 return fw_dump.ops->fadump_register(&fw_dump); 947 } 948 949 void fadump_cleanup(void) 950 { 951 /* Invalidate the registration only if dump is active. */ 952 if (fw_dump.dump_active) { 953 pr_debug("Invalidating firmware-assisted dump registration\n"); 954 fw_dump.ops->fadump_invalidate(&fw_dump); 955 } else if (fw_dump.dump_registered) { 956 /* Un-register Firmware-assisted dump if it was registered. */ 957 fw_dump.ops->fadump_unregister(&fw_dump); 958 free_crash_memory_ranges(); 959 } 960 } 961 962 static void fadump_free_reserved_memory(unsigned long start_pfn, 963 unsigned long end_pfn) 964 { 965 unsigned long pfn; 966 unsigned long time_limit = jiffies + HZ; 967 968 pr_info("freeing reserved memory (0x%llx - 0x%llx)\n", 969 PFN_PHYS(start_pfn), PFN_PHYS(end_pfn)); 970 971 for (pfn = start_pfn; pfn < end_pfn; pfn++) { 972 free_reserved_page(pfn_to_page(pfn)); 973 974 if (time_after(jiffies, time_limit)) { 975 cond_resched(); 976 time_limit = jiffies + HZ; 977 } 978 } 979 } 980 981 /* 982 * Skip memory holes and free memory that was actually reserved. 983 */ 984 static void fadump_release_reserved_area(unsigned long start, unsigned long end) 985 { 986 struct memblock_region *reg; 987 unsigned long tstart, tend; 988 unsigned long start_pfn = PHYS_PFN(start); 989 unsigned long end_pfn = PHYS_PFN(end); 990 991 for_each_memblock(memory, reg) { 992 tstart = max(start_pfn, memblock_region_memory_base_pfn(reg)); 993 tend = min(end_pfn, memblock_region_memory_end_pfn(reg)); 994 if (tstart < tend) { 995 fadump_free_reserved_memory(tstart, tend); 996 997 if (tend == end_pfn) 998 break; 999 1000 start_pfn = tend + 1; 1001 } 1002 } 1003 } 1004 1005 /* 1006 * Release the memory that was reserved in early boot to preserve the memory 1007 * contents. The released memory will be available for general use. 1008 */ 1009 static void fadump_release_memory(unsigned long begin, unsigned long end) 1010 { 1011 unsigned long ra_start, ra_end; 1012 1013 ra_start = fw_dump.reserve_dump_area_start; 1014 ra_end = ra_start + fw_dump.reserve_dump_area_size; 1015 1016 /* 1017 * exclude the dump reserve area. Will reuse it for next 1018 * fadump registration. 1019 */ 1020 if (begin < ra_end && end > ra_start) { 1021 if (begin < ra_start) 1022 fadump_release_reserved_area(begin, ra_start); 1023 if (end > ra_end) 1024 fadump_release_reserved_area(ra_end, end); 1025 } else 1026 fadump_release_reserved_area(begin, end); 1027 } 1028 1029 static void fadump_invalidate_release_mem(void) 1030 { 1031 mutex_lock(&fadump_mutex); 1032 if (!fw_dump.dump_active) { 1033 mutex_unlock(&fadump_mutex); 1034 return; 1035 } 1036 1037 fadump_cleanup(); 1038 mutex_unlock(&fadump_mutex); 1039 1040 fadump_release_memory(fw_dump.boot_memory_size, memblock_end_of_DRAM()); 1041 fadump_free_cpu_notes_buf(); 1042 1043 /* Initialize the kernel dump memory structure for FAD registration. */ 1044 fw_dump.ops->fadump_init_mem_struct(&fw_dump); 1045 } 1046 1047 static ssize_t fadump_release_memory_store(struct kobject *kobj, 1048 struct kobj_attribute *attr, 1049 const char *buf, size_t count) 1050 { 1051 int input = -1; 1052 1053 if (!fw_dump.dump_active) 1054 return -EPERM; 1055 1056 if (kstrtoint(buf, 0, &input)) 1057 return -EINVAL; 1058 1059 if (input == 1) { 1060 /* 1061 * Take away the '/proc/vmcore'. We are releasing the dump 1062 * memory, hence it will not be valid anymore. 1063 */ 1064 #ifdef CONFIG_PROC_VMCORE 1065 vmcore_cleanup(); 1066 #endif 1067 fadump_invalidate_release_mem(); 1068 1069 } else 1070 return -EINVAL; 1071 return count; 1072 } 1073 1074 static ssize_t fadump_enabled_show(struct kobject *kobj, 1075 struct kobj_attribute *attr, 1076 char *buf) 1077 { 1078 return sprintf(buf, "%d\n", fw_dump.fadump_enabled); 1079 } 1080 1081 static ssize_t fadump_register_show(struct kobject *kobj, 1082 struct kobj_attribute *attr, 1083 char *buf) 1084 { 1085 return sprintf(buf, "%d\n", fw_dump.dump_registered); 1086 } 1087 1088 static ssize_t fadump_register_store(struct kobject *kobj, 1089 struct kobj_attribute *attr, 1090 const char *buf, size_t count) 1091 { 1092 int ret = 0; 1093 int input = -1; 1094 1095 if (!fw_dump.fadump_enabled || fw_dump.dump_active) 1096 return -EPERM; 1097 1098 if (kstrtoint(buf, 0, &input)) 1099 return -EINVAL; 1100 1101 mutex_lock(&fadump_mutex); 1102 1103 switch (input) { 1104 case 0: 1105 if (fw_dump.dump_registered == 0) { 1106 goto unlock_out; 1107 } 1108 1109 /* Un-register Firmware-assisted dump */ 1110 pr_debug("Un-register firmware-assisted dump\n"); 1111 fw_dump.ops->fadump_unregister(&fw_dump); 1112 break; 1113 case 1: 1114 if (fw_dump.dump_registered == 1) { 1115 /* Un-register Firmware-assisted dump */ 1116 fw_dump.ops->fadump_unregister(&fw_dump); 1117 } 1118 /* Register Firmware-assisted dump */ 1119 ret = register_fadump(); 1120 break; 1121 default: 1122 ret = -EINVAL; 1123 break; 1124 } 1125 1126 unlock_out: 1127 mutex_unlock(&fadump_mutex); 1128 return ret < 0 ? ret : count; 1129 } 1130 1131 static int fadump_region_show(struct seq_file *m, void *private) 1132 { 1133 if (!fw_dump.fadump_enabled) 1134 return 0; 1135 1136 mutex_lock(&fadump_mutex); 1137 fw_dump.ops->fadump_region_show(&fw_dump, m); 1138 mutex_unlock(&fadump_mutex); 1139 return 0; 1140 } 1141 1142 static struct kobj_attribute fadump_release_attr = __ATTR(fadump_release_mem, 1143 0200, NULL, 1144 fadump_release_memory_store); 1145 static struct kobj_attribute fadump_attr = __ATTR(fadump_enabled, 1146 0444, fadump_enabled_show, 1147 NULL); 1148 static struct kobj_attribute fadump_register_attr = __ATTR(fadump_registered, 1149 0644, fadump_register_show, 1150 fadump_register_store); 1151 1152 DEFINE_SHOW_ATTRIBUTE(fadump_region); 1153 1154 static void fadump_init_files(void) 1155 { 1156 struct dentry *debugfs_file; 1157 int rc = 0; 1158 1159 rc = sysfs_create_file(kernel_kobj, &fadump_attr.attr); 1160 if (rc) 1161 printk(KERN_ERR "fadump: unable to create sysfs file" 1162 " fadump_enabled (%d)\n", rc); 1163 1164 rc = sysfs_create_file(kernel_kobj, &fadump_register_attr.attr); 1165 if (rc) 1166 printk(KERN_ERR "fadump: unable to create sysfs file" 1167 " fadump_registered (%d)\n", rc); 1168 1169 debugfs_file = debugfs_create_file("fadump_region", 0444, 1170 powerpc_debugfs_root, NULL, 1171 &fadump_region_fops); 1172 if (!debugfs_file) 1173 printk(KERN_ERR "fadump: unable to create debugfs file" 1174 " fadump_region\n"); 1175 1176 if (fw_dump.dump_active) { 1177 rc = sysfs_create_file(kernel_kobj, &fadump_release_attr.attr); 1178 if (rc) 1179 printk(KERN_ERR "fadump: unable to create sysfs file" 1180 " fadump_release_mem (%d)\n", rc); 1181 } 1182 return; 1183 } 1184 1185 /* 1186 * Prepare for firmware-assisted dump. 1187 */ 1188 int __init setup_fadump(void) 1189 { 1190 if (!fw_dump.fadump_enabled) 1191 return 0; 1192 1193 if (!fw_dump.fadump_supported) { 1194 printk(KERN_ERR "Firmware-assisted dump is not supported on" 1195 " this hardware\n"); 1196 return 0; 1197 } 1198 1199 fadump_show_config(); 1200 /* 1201 * If dump data is available then see if it is valid and prepare for 1202 * saving it to the disk. 1203 */ 1204 if (fw_dump.dump_active) { 1205 /* 1206 * if dump process fails then invalidate the registration 1207 * and release memory before proceeding for re-registration. 1208 */ 1209 if (fw_dump.ops->fadump_process(&fw_dump) < 0) 1210 fadump_invalidate_release_mem(); 1211 } 1212 /* Initialize the kernel dump memory structure for FAD registration. */ 1213 else if (fw_dump.reserve_dump_area_size) 1214 fw_dump.ops->fadump_init_mem_struct(&fw_dump); 1215 1216 fadump_init_files(); 1217 1218 return 1; 1219 } 1220 subsys_initcall(setup_fadump); 1221