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 #include <linux/debugfs.h> 28 #include <linux/of.h> 29 #include <linux/of_fdt.h> 30 31 #include <asm/page.h> 32 #include <asm/fadump.h> 33 #include <asm/fadump-internal.h> 34 #include <asm/setup.h> 35 #include <asm/interrupt.h> 36 #include <asm/prom.h> 37 38 /* 39 * The CPU who acquired the lock to trigger the fadump crash should 40 * wait for other CPUs to enter. 41 * 42 * The timeout is in milliseconds. 43 */ 44 #define CRASH_TIMEOUT 500 45 46 static struct fw_dump fw_dump; 47 48 static void __init fadump_reserve_crash_area(u64 base); 49 50 #ifndef CONFIG_PRESERVE_FA_DUMP 51 52 static struct kobject *fadump_kobj; 53 54 static atomic_t cpus_in_fadump; 55 static DEFINE_MUTEX(fadump_mutex); 56 57 #define RESERVED_RNGS_SZ 16384 /* 16K - 128 entries */ 58 #define RESERVED_RNGS_CNT (RESERVED_RNGS_SZ / \ 59 sizeof(struct fadump_memory_range)) 60 static struct fadump_memory_range rngs[RESERVED_RNGS_CNT]; 61 static struct fadump_mrange_info 62 reserved_mrange_info = { "reserved", rngs, RESERVED_RNGS_SZ, 0, RESERVED_RNGS_CNT, true }; 63 64 static void __init early_init_dt_scan_reserved_ranges(unsigned long node); 65 66 #ifdef CONFIG_CMA 67 static struct cma *fadump_cma; 68 69 /* 70 * fadump_cma_init() - Initialize CMA area from a fadump reserved memory 71 * 72 * This function initializes CMA area from fadump reserved memory. 73 * The total size of fadump reserved memory covers for boot memory size 74 * + cpu data size + hpte size and metadata. 75 * Initialize only the area equivalent to boot memory size for CMA use. 76 * The remaining portion of fadump reserved memory will be not given 77 * to CMA and pages for those will stay reserved. boot memory size is 78 * aligned per CMA requirement to satisy cma_init_reserved_mem() call. 79 * But for some reason even if it fails we still have the memory reservation 80 * with us and we can still continue doing fadump. 81 */ 82 void __init fadump_cma_init(void) 83 { 84 unsigned long long base, size, end; 85 int rc; 86 87 if (!fw_dump.fadump_supported || !fw_dump.fadump_enabled || 88 fw_dump.dump_active) 89 return; 90 /* 91 * Do not use CMA if user has provided fadump=nocma kernel parameter. 92 */ 93 if (fw_dump.nocma || !fw_dump.boot_memory_size) 94 return; 95 96 /* 97 * [base, end) should be reserved during early init in 98 * fadump_reserve_mem(). No need to check this here as 99 * cma_init_reserved_mem() already checks for overlap. 100 * Here we give the aligned chunk of this reserved memory to CMA. 101 */ 102 base = fw_dump.reserve_dump_area_start; 103 size = fw_dump.boot_memory_size; 104 end = base + size; 105 106 base = ALIGN(base, CMA_MIN_ALIGNMENT_BYTES); 107 end = ALIGN_DOWN(end, CMA_MIN_ALIGNMENT_BYTES); 108 size = end - base; 109 110 if (end <= base) { 111 pr_warn("%s: Too less memory to give to CMA\n", __func__); 112 return; 113 } 114 115 rc = cma_init_reserved_mem(base, size, 0, "fadump_cma", &fadump_cma); 116 if (rc) { 117 pr_err("Failed to init cma area for firmware-assisted dump,%d\n", rc); 118 /* 119 * Though the CMA init has failed we still have memory 120 * reservation with us. The reserved memory will be 121 * blocked from production system usage. Hence return 1, 122 * so that we can continue with fadump. 123 */ 124 return; 125 } 126 127 /* 128 * If CMA activation fails, keep the pages reserved, instead of 129 * exposing them to buddy allocator. Same as 'fadump=nocma' case. 130 */ 131 cma_reserve_pages_on_error(fadump_cma); 132 133 /* 134 * So we now have successfully initialized cma area for fadump. 135 */ 136 pr_info("Initialized [0x%llx, %luMB] cma area from [0x%lx, %luMB] " 137 "bytes of memory reserved for firmware-assisted dump\n", 138 cma_get_base(fadump_cma), cma_get_size(fadump_cma) >> 20, 139 fw_dump.reserve_dump_area_start, 140 fw_dump.boot_memory_size >> 20); 141 return; 142 } 143 #endif /* CONFIG_CMA */ 144 145 /* 146 * Additional parameters meant for capture kernel are placed in a dedicated area. 147 * If this is capture kernel boot, append these parameters to bootargs. 148 */ 149 void __init fadump_append_bootargs(void) 150 { 151 char *append_args; 152 size_t len; 153 154 if (!fw_dump.dump_active || !fw_dump.param_area_supported || !fw_dump.param_area) 155 return; 156 157 if (fw_dump.param_area < fw_dump.boot_mem_top) { 158 if (memblock_reserve(fw_dump.param_area, COMMAND_LINE_SIZE)) { 159 pr_warn("WARNING: Can't use additional parameters area!\n"); 160 fw_dump.param_area = 0; 161 return; 162 } 163 } 164 165 append_args = (char *)fw_dump.param_area; 166 len = strlen(boot_command_line); 167 168 /* 169 * Too late to fail even if cmdline size exceeds. Truncate additional parameters 170 * to cmdline size and proceed anyway. 171 */ 172 if (len + strlen(append_args) >= COMMAND_LINE_SIZE - 1) 173 pr_warn("WARNING: Appending parameters exceeds cmdline size. Truncating!\n"); 174 175 pr_debug("Cmdline: %s\n", boot_command_line); 176 snprintf(boot_command_line + len, COMMAND_LINE_SIZE - len, " %s", append_args); 177 pr_info("Updated cmdline: %s\n", boot_command_line); 178 } 179 180 /* Scan the Firmware Assisted dump configuration details. */ 181 int __init early_init_dt_scan_fw_dump(unsigned long node, const char *uname, 182 int depth, void *data) 183 { 184 if (depth == 0) { 185 early_init_dt_scan_reserved_ranges(node); 186 return 0; 187 } 188 189 if (depth != 1) 190 return 0; 191 192 if (strcmp(uname, "rtas") == 0) { 193 rtas_fadump_dt_scan(&fw_dump, node); 194 return 1; 195 } 196 197 if (strcmp(uname, "ibm,opal") == 0) { 198 opal_fadump_dt_scan(&fw_dump, node); 199 return 1; 200 } 201 202 return 0; 203 } 204 205 /* 206 * If fadump is registered, check if the memory provided 207 * falls within boot memory area and reserved memory area. 208 */ 209 int is_fadump_memory_area(u64 addr, unsigned long size) 210 { 211 u64 d_start, d_end; 212 213 if (!fw_dump.dump_registered) 214 return 0; 215 216 if (!size) 217 return 0; 218 219 d_start = fw_dump.reserve_dump_area_start; 220 d_end = d_start + fw_dump.reserve_dump_area_size; 221 if (((addr + size) > d_start) && (addr <= d_end)) 222 return 1; 223 224 return (addr <= fw_dump.boot_mem_top); 225 } 226 227 int should_fadump_crash(void) 228 { 229 if (!fw_dump.dump_registered || !fw_dump.fadumphdr_addr) 230 return 0; 231 return 1; 232 } 233 234 int is_fadump_active(void) 235 { 236 return fw_dump.dump_active; 237 } 238 239 /* 240 * Returns true, if there are no holes in memory area between d_start to d_end, 241 * false otherwise. 242 */ 243 static bool is_fadump_mem_area_contiguous(u64 d_start, u64 d_end) 244 { 245 phys_addr_t reg_start, reg_end; 246 bool ret = false; 247 u64 i, start, end; 248 249 for_each_mem_range(i, ®_start, ®_end) { 250 start = max_t(u64, d_start, reg_start); 251 end = min_t(u64, d_end, reg_end); 252 if (d_start < end) { 253 /* Memory hole from d_start to start */ 254 if (start > d_start) 255 break; 256 257 if (end == d_end) { 258 ret = true; 259 break; 260 } 261 262 d_start = end + 1; 263 } 264 } 265 266 return ret; 267 } 268 269 /* 270 * Returns true, if there are no holes in reserved memory area, 271 * false otherwise. 272 */ 273 bool is_fadump_reserved_mem_contiguous(void) 274 { 275 u64 d_start, d_end; 276 277 d_start = fw_dump.reserve_dump_area_start; 278 d_end = d_start + fw_dump.reserve_dump_area_size; 279 return is_fadump_mem_area_contiguous(d_start, d_end); 280 } 281 282 /* Print firmware assisted dump configurations for debugging purpose. */ 283 static void __init fadump_show_config(void) 284 { 285 int i; 286 287 pr_debug("Support for firmware-assisted dump (fadump): %s\n", 288 (fw_dump.fadump_supported ? "present" : "no support")); 289 290 if (!fw_dump.fadump_supported) 291 return; 292 293 pr_debug("Fadump enabled : %s\n", 294 (fw_dump.fadump_enabled ? "yes" : "no")); 295 pr_debug("Dump Active : %s\n", 296 (fw_dump.dump_active ? "yes" : "no")); 297 pr_debug("Dump section sizes:\n"); 298 pr_debug(" CPU state data size: %lx\n", fw_dump.cpu_state_data_size); 299 pr_debug(" HPTE region size : %lx\n", fw_dump.hpte_region_size); 300 pr_debug(" Boot memory size : %lx\n", fw_dump.boot_memory_size); 301 pr_debug(" Boot memory top : %llx\n", fw_dump.boot_mem_top); 302 pr_debug("Boot memory regions cnt: %llx\n", fw_dump.boot_mem_regs_cnt); 303 for (i = 0; i < fw_dump.boot_mem_regs_cnt; i++) { 304 pr_debug("[%03d] base = %llx, size = %llx\n", i, 305 fw_dump.boot_mem_addr[i], fw_dump.boot_mem_sz[i]); 306 } 307 } 308 309 /** 310 * fadump_calculate_reserve_size(): reserve variable boot area 5% of System RAM 311 * 312 * Function to find the largest memory size we need to reserve during early 313 * boot process. This will be the size of the memory that is required for a 314 * kernel to boot successfully. 315 * 316 * This function has been taken from phyp-assisted dump feature implementation. 317 * 318 * returns larger of 256MB or 5% rounded down to multiples of 256MB. 319 * 320 * TODO: Come up with better approach to find out more accurate memory size 321 * that is required for a kernel to boot successfully. 322 * 323 */ 324 static __init u64 fadump_calculate_reserve_size(void) 325 { 326 u64 base, size, bootmem_min; 327 int ret; 328 329 if (fw_dump.reserve_bootvar) 330 pr_warn("'fadump_reserve_mem=' parameter is deprecated in favor of 'crashkernel=' parameter.\n"); 331 332 /* 333 * Check if the size is specified through crashkernel= cmdline 334 * option. If yes, then use that but ignore base as fadump reserves 335 * memory at a predefined offset. 336 */ 337 ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(), 338 &size, &base, NULL, NULL); 339 if (ret == 0 && size > 0) { 340 unsigned long max_size; 341 342 if (fw_dump.reserve_bootvar) 343 pr_info("Using 'crashkernel=' parameter for memory reservation.\n"); 344 345 fw_dump.reserve_bootvar = (unsigned long)size; 346 347 /* 348 * Adjust if the boot memory size specified is above 349 * the upper limit. 350 */ 351 max_size = memblock_phys_mem_size() / MAX_BOOT_MEM_RATIO; 352 if (fw_dump.reserve_bootvar > max_size) { 353 fw_dump.reserve_bootvar = max_size; 354 pr_info("Adjusted boot memory size to %luMB\n", 355 (fw_dump.reserve_bootvar >> 20)); 356 } 357 358 return fw_dump.reserve_bootvar; 359 } else if (fw_dump.reserve_bootvar) { 360 /* 361 * 'fadump_reserve_mem=' is being used to reserve memory 362 * for firmware-assisted dump. 363 */ 364 return fw_dump.reserve_bootvar; 365 } 366 367 /* divide by 20 to get 5% of value */ 368 size = memblock_phys_mem_size() / 20; 369 370 /* round it down in multiples of 256 */ 371 size = size & ~0x0FFFFFFFUL; 372 373 /* Truncate to memory_limit. We don't want to over reserve the memory.*/ 374 if (memory_limit && size > memory_limit) 375 size = memory_limit; 376 377 bootmem_min = fw_dump.ops->fadump_get_bootmem_min(); 378 return (size > bootmem_min ? size : bootmem_min); 379 } 380 381 /* 382 * Calculate the total memory size required to be reserved for 383 * firmware-assisted dump registration. 384 */ 385 static unsigned long __init get_fadump_area_size(void) 386 { 387 unsigned long size = 0; 388 389 size += fw_dump.cpu_state_data_size; 390 size += fw_dump.hpte_region_size; 391 /* 392 * Account for pagesize alignment of boot memory area destination address. 393 * This faciliates in mmap reading of first kernel's memory. 394 */ 395 size = PAGE_ALIGN(size); 396 size += fw_dump.boot_memory_size; 397 size += sizeof(struct fadump_crash_info_header); 398 399 /* This is to hold kernel metadata on platforms that support it */ 400 size += (fw_dump.ops->fadump_get_metadata_size ? 401 fw_dump.ops->fadump_get_metadata_size() : 0); 402 return size; 403 } 404 405 static int __init add_boot_mem_region(unsigned long rstart, 406 unsigned long rsize) 407 { 408 int max_boot_mem_rgns = fw_dump.ops->fadump_max_boot_mem_rgns(); 409 int i = fw_dump.boot_mem_regs_cnt++; 410 411 if (fw_dump.boot_mem_regs_cnt > max_boot_mem_rgns) { 412 fw_dump.boot_mem_regs_cnt = max_boot_mem_rgns; 413 return 0; 414 } 415 416 pr_debug("Added boot memory range[%d] [%#016lx-%#016lx)\n", 417 i, rstart, (rstart + rsize)); 418 fw_dump.boot_mem_addr[i] = rstart; 419 fw_dump.boot_mem_sz[i] = rsize; 420 return 1; 421 } 422 423 /* 424 * Firmware usually has a hard limit on the data it can copy per region. 425 * Honour that by splitting a memory range into multiple regions. 426 */ 427 static int __init add_boot_mem_regions(unsigned long mstart, 428 unsigned long msize) 429 { 430 unsigned long rstart, rsize, max_size; 431 int ret = 1; 432 433 rstart = mstart; 434 max_size = fw_dump.max_copy_size ? fw_dump.max_copy_size : msize; 435 while (msize) { 436 if (msize > max_size) 437 rsize = max_size; 438 else 439 rsize = msize; 440 441 ret = add_boot_mem_region(rstart, rsize); 442 if (!ret) 443 break; 444 445 msize -= rsize; 446 rstart += rsize; 447 } 448 449 return ret; 450 } 451 452 static int __init fadump_get_boot_mem_regions(void) 453 { 454 unsigned long size, cur_size, hole_size, last_end; 455 unsigned long mem_size = fw_dump.boot_memory_size; 456 phys_addr_t reg_start, reg_end; 457 int ret = 1; 458 u64 i; 459 460 fw_dump.boot_mem_regs_cnt = 0; 461 462 last_end = 0; 463 hole_size = 0; 464 cur_size = 0; 465 for_each_mem_range(i, ®_start, ®_end) { 466 size = reg_end - reg_start; 467 hole_size += (reg_start - last_end); 468 469 if ((cur_size + size) >= mem_size) { 470 size = (mem_size - cur_size); 471 ret = add_boot_mem_regions(reg_start, size); 472 break; 473 } 474 475 mem_size -= size; 476 cur_size += size; 477 ret = add_boot_mem_regions(reg_start, size); 478 if (!ret) 479 break; 480 481 last_end = reg_end; 482 } 483 fw_dump.boot_mem_top = PAGE_ALIGN(fw_dump.boot_memory_size + hole_size); 484 485 return ret; 486 } 487 488 /* 489 * Returns true, if the given range overlaps with reserved memory ranges 490 * starting at idx. Also, updates idx to index of overlapping memory range 491 * with the given memory range. 492 * False, otherwise. 493 */ 494 static bool __init overlaps_reserved_ranges(u64 base, u64 end, int *idx) 495 { 496 bool ret = false; 497 int i; 498 499 for (i = *idx; i < reserved_mrange_info.mem_range_cnt; i++) { 500 u64 rbase = reserved_mrange_info.mem_ranges[i].base; 501 u64 rend = rbase + reserved_mrange_info.mem_ranges[i].size; 502 503 if (end <= rbase) 504 break; 505 506 if ((end > rbase) && (base < rend)) { 507 *idx = i; 508 ret = true; 509 break; 510 } 511 } 512 513 return ret; 514 } 515 516 /* 517 * Locate a suitable memory area to reserve memory for FADump. While at it, 518 * lookup reserved-ranges & avoid overlap with them, as they are used by F/W. 519 */ 520 static u64 __init fadump_locate_reserve_mem(u64 base, u64 size) 521 { 522 struct fadump_memory_range *mrngs; 523 phys_addr_t mstart, mend; 524 int idx = 0; 525 u64 i, ret = 0; 526 527 mrngs = reserved_mrange_info.mem_ranges; 528 for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, 529 &mstart, &mend, NULL) { 530 pr_debug("%llu) mstart: %llx, mend: %llx, base: %llx\n", 531 i, mstart, mend, base); 532 533 if (mstart > base) 534 base = PAGE_ALIGN(mstart); 535 536 while ((mend > base) && ((mend - base) >= size)) { 537 if (!overlaps_reserved_ranges(base, base+size, &idx)) { 538 ret = base; 539 goto out; 540 } 541 542 base = mrngs[idx].base + mrngs[idx].size; 543 base = PAGE_ALIGN(base); 544 } 545 } 546 547 out: 548 return ret; 549 } 550 551 int __init fadump_reserve_mem(void) 552 { 553 u64 base, size, mem_boundary, bootmem_min; 554 int ret = 1; 555 556 if (!fw_dump.fadump_enabled) 557 return 0; 558 559 if (!fw_dump.fadump_supported) { 560 pr_info("Firmware-Assisted Dump is not supported on this hardware\n"); 561 goto error_out; 562 } 563 564 /* 565 * Initialize boot memory size 566 * If dump is active then we have already calculated the size during 567 * first kernel. 568 */ 569 if (!fw_dump.dump_active) { 570 fw_dump.boot_memory_size = 571 PAGE_ALIGN(fadump_calculate_reserve_size()); 572 573 bootmem_min = fw_dump.ops->fadump_get_bootmem_min(); 574 if (fw_dump.boot_memory_size < bootmem_min) { 575 pr_err("Can't enable fadump with boot memory size (0x%lx) less than 0x%llx\n", 576 fw_dump.boot_memory_size, bootmem_min); 577 goto error_out; 578 } 579 580 if (!fadump_get_boot_mem_regions()) { 581 pr_err("Too many holes in boot memory area to enable fadump\n"); 582 goto error_out; 583 } 584 } 585 586 if (memory_limit) 587 mem_boundary = memory_limit; 588 else 589 mem_boundary = memblock_end_of_DRAM(); 590 591 base = fw_dump.boot_mem_top; 592 size = get_fadump_area_size(); 593 fw_dump.reserve_dump_area_size = size; 594 if (fw_dump.dump_active) { 595 pr_info("Firmware-assisted dump is active.\n"); 596 597 #ifdef CONFIG_HUGETLB_PAGE 598 /* 599 * FADump capture kernel doesn't care much about hugepages. 600 * In fact, handling hugepages in capture kernel is asking for 601 * trouble. So, disable HugeTLB support when fadump is active. 602 */ 603 hugetlb_disabled = true; 604 #endif 605 /* 606 * If last boot has crashed then reserve all the memory 607 * above boot memory size so that we don't touch it until 608 * dump is written to disk by userspace tool. This memory 609 * can be released for general use by invalidating fadump. 610 */ 611 fadump_reserve_crash_area(base); 612 613 pr_debug("fadumphdr_addr = %#016lx\n", fw_dump.fadumphdr_addr); 614 pr_debug("Reserve dump area start address: 0x%lx\n", 615 fw_dump.reserve_dump_area_start); 616 } else { 617 /* 618 * Reserve memory at an offset closer to bottom of the RAM to 619 * minimize the impact of memory hot-remove operation. 620 */ 621 base = fadump_locate_reserve_mem(base, size); 622 623 if (!base || (base + size > mem_boundary)) { 624 pr_err("Failed to find memory chunk for reservation!\n"); 625 goto error_out; 626 } 627 fw_dump.reserve_dump_area_start = base; 628 629 /* 630 * Calculate the kernel metadata address and register it with 631 * f/w if the platform supports. 632 */ 633 if (fw_dump.ops->fadump_setup_metadata && 634 (fw_dump.ops->fadump_setup_metadata(&fw_dump) < 0)) 635 goto error_out; 636 637 if (memblock_reserve(base, size)) { 638 pr_err("Failed to reserve memory!\n"); 639 goto error_out; 640 } 641 642 pr_info("Reserved %lldMB of memory at %#016llx (System RAM: %lldMB)\n", 643 (size >> 20), base, (memblock_phys_mem_size() >> 20)); 644 } 645 646 return ret; 647 error_out: 648 fw_dump.fadump_enabled = 0; 649 fw_dump.reserve_dump_area_size = 0; 650 return 0; 651 } 652 653 /* Look for fadump= cmdline option. */ 654 static int __init early_fadump_param(char *p) 655 { 656 if (!p) 657 return 1; 658 659 if (strncmp(p, "on", 2) == 0) 660 fw_dump.fadump_enabled = 1; 661 else if (strncmp(p, "off", 3) == 0) 662 fw_dump.fadump_enabled = 0; 663 else if (strncmp(p, "nocma", 5) == 0) { 664 fw_dump.fadump_enabled = 1; 665 fw_dump.nocma = 1; 666 } 667 668 return 0; 669 } 670 early_param("fadump", early_fadump_param); 671 672 /* 673 * Look for fadump_reserve_mem= cmdline option 674 * TODO: Remove references to 'fadump_reserve_mem=' parameter, 675 * the sooner 'crashkernel=' parameter is accustomed to. 676 */ 677 static int __init early_fadump_reserve_mem(char *p) 678 { 679 if (p) 680 fw_dump.reserve_bootvar = memparse(p, &p); 681 return 0; 682 } 683 early_param("fadump_reserve_mem", early_fadump_reserve_mem); 684 685 void crash_fadump(struct pt_regs *regs, const char *str) 686 { 687 unsigned int msecs; 688 struct fadump_crash_info_header *fdh = NULL; 689 int old_cpu, this_cpu; 690 /* Do not include first CPU */ 691 unsigned int ncpus = num_online_cpus() - 1; 692 693 if (!should_fadump_crash()) 694 return; 695 696 /* 697 * old_cpu == -1 means this is the first CPU which has come here, 698 * go ahead and trigger fadump. 699 * 700 * old_cpu != -1 means some other CPU has already on its way 701 * to trigger fadump, just keep looping here. 702 */ 703 this_cpu = smp_processor_id(); 704 old_cpu = cmpxchg(&crashing_cpu, -1, this_cpu); 705 706 if (old_cpu != -1) { 707 atomic_inc(&cpus_in_fadump); 708 709 /* 710 * We can't loop here indefinitely. Wait as long as fadump 711 * is in force. If we race with fadump un-registration this 712 * loop will break and then we go down to normal panic path 713 * and reboot. If fadump is in force the first crashing 714 * cpu will definitely trigger fadump. 715 */ 716 while (fw_dump.dump_registered) 717 cpu_relax(); 718 return; 719 } 720 721 fdh = __va(fw_dump.fadumphdr_addr); 722 fdh->crashing_cpu = crashing_cpu; 723 crash_save_vmcoreinfo(); 724 725 if (regs) 726 fdh->regs = *regs; 727 else 728 ppc_save_regs(&fdh->regs); 729 730 fdh->cpu_mask = *cpu_online_mask; 731 732 /* 733 * If we came in via system reset, wait a while for the secondary 734 * CPUs to enter. 735 */ 736 if (TRAP(&(fdh->regs)) == INTERRUPT_SYSTEM_RESET) { 737 msecs = CRASH_TIMEOUT; 738 while ((atomic_read(&cpus_in_fadump) < ncpus) && (--msecs > 0)) 739 mdelay(1); 740 } 741 742 fw_dump.ops->fadump_trigger(fdh, str); 743 } 744 745 u32 *__init fadump_regs_to_elf_notes(u32 *buf, struct pt_regs *regs) 746 { 747 struct elf_prstatus prstatus; 748 749 memset(&prstatus, 0, sizeof(prstatus)); 750 /* 751 * FIXME: How do i get PID? Do I really need it? 752 * prstatus.pr_pid = ???? 753 */ 754 elf_core_copy_regs(&prstatus.pr_reg, regs); 755 buf = append_elf_note(buf, NN_PRSTATUS, NT_PRSTATUS, 756 &prstatus, sizeof(prstatus)); 757 return buf; 758 } 759 760 void __init fadump_update_elfcore_header(char *bufp) 761 { 762 struct elf_phdr *phdr; 763 764 bufp += sizeof(struct elfhdr); 765 766 /* First note is a place holder for cpu notes info. */ 767 phdr = (struct elf_phdr *)bufp; 768 769 if (phdr->p_type == PT_NOTE) { 770 phdr->p_paddr = __pa(fw_dump.cpu_notes_buf_vaddr); 771 phdr->p_offset = phdr->p_paddr; 772 phdr->p_filesz = fw_dump.cpu_notes_buf_size; 773 phdr->p_memsz = fw_dump.cpu_notes_buf_size; 774 } 775 return; 776 } 777 778 static void *__init fadump_alloc_buffer(unsigned long size) 779 { 780 unsigned long count, i; 781 struct page *page; 782 void *vaddr; 783 784 vaddr = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO); 785 if (!vaddr) 786 return NULL; 787 788 count = PAGE_ALIGN(size) / PAGE_SIZE; 789 page = virt_to_page(vaddr); 790 for (i = 0; i < count; i++) 791 mark_page_reserved(page + i); 792 return vaddr; 793 } 794 795 static void fadump_free_buffer(unsigned long vaddr, unsigned long size) 796 { 797 free_reserved_area((void *)vaddr, (void *)(vaddr + size), -1, NULL); 798 } 799 800 s32 __init fadump_setup_cpu_notes_buf(u32 num_cpus) 801 { 802 /* Allocate buffer to hold cpu crash notes. */ 803 fw_dump.cpu_notes_buf_size = num_cpus * sizeof(note_buf_t); 804 fw_dump.cpu_notes_buf_size = PAGE_ALIGN(fw_dump.cpu_notes_buf_size); 805 fw_dump.cpu_notes_buf_vaddr = 806 (unsigned long)fadump_alloc_buffer(fw_dump.cpu_notes_buf_size); 807 if (!fw_dump.cpu_notes_buf_vaddr) { 808 pr_err("Failed to allocate %ld bytes for CPU notes buffer\n", 809 fw_dump.cpu_notes_buf_size); 810 return -ENOMEM; 811 } 812 813 pr_debug("Allocated buffer for cpu notes of size %ld at 0x%lx\n", 814 fw_dump.cpu_notes_buf_size, 815 fw_dump.cpu_notes_buf_vaddr); 816 return 0; 817 } 818 819 void fadump_free_cpu_notes_buf(void) 820 { 821 if (!fw_dump.cpu_notes_buf_vaddr) 822 return; 823 824 fadump_free_buffer(fw_dump.cpu_notes_buf_vaddr, 825 fw_dump.cpu_notes_buf_size); 826 fw_dump.cpu_notes_buf_vaddr = 0; 827 fw_dump.cpu_notes_buf_size = 0; 828 } 829 830 static void fadump_free_mem_ranges(struct fadump_mrange_info *mrange_info) 831 { 832 if (mrange_info->is_static) { 833 mrange_info->mem_range_cnt = 0; 834 return; 835 } 836 837 kfree(mrange_info->mem_ranges); 838 memset((void *)((u64)mrange_info + RNG_NAME_SZ), 0, 839 (sizeof(struct fadump_mrange_info) - RNG_NAME_SZ)); 840 } 841 842 /* 843 * Allocate or reallocate mem_ranges array in incremental units 844 * of PAGE_SIZE. 845 */ 846 static int fadump_alloc_mem_ranges(struct fadump_mrange_info *mrange_info) 847 { 848 struct fadump_memory_range *new_array; 849 u64 new_size; 850 851 new_size = mrange_info->mem_ranges_sz + PAGE_SIZE; 852 pr_debug("Allocating %llu bytes of memory for %s memory ranges\n", 853 new_size, mrange_info->name); 854 855 new_array = krealloc(mrange_info->mem_ranges, new_size, GFP_KERNEL); 856 if (new_array == NULL) { 857 pr_err("Insufficient memory for setting up %s memory ranges\n", 858 mrange_info->name); 859 fadump_free_mem_ranges(mrange_info); 860 return -ENOMEM; 861 } 862 863 mrange_info->mem_ranges = new_array; 864 mrange_info->mem_ranges_sz = new_size; 865 mrange_info->max_mem_ranges = (new_size / 866 sizeof(struct fadump_memory_range)); 867 return 0; 868 } 869 static inline int fadump_add_mem_range(struct fadump_mrange_info *mrange_info, 870 u64 base, u64 end) 871 { 872 struct fadump_memory_range *mem_ranges = mrange_info->mem_ranges; 873 bool is_adjacent = false; 874 u64 start, size; 875 876 if (base == end) 877 return 0; 878 879 /* 880 * Fold adjacent memory ranges to bring down the memory ranges/ 881 * PT_LOAD segments count. 882 */ 883 if (mrange_info->mem_range_cnt) { 884 start = mem_ranges[mrange_info->mem_range_cnt - 1].base; 885 size = mem_ranges[mrange_info->mem_range_cnt - 1].size; 886 887 /* 888 * Boot memory area needs separate PT_LOAD segment(s) as it 889 * is moved to a different location at the time of crash. 890 * So, fold only if the region is not boot memory area. 891 */ 892 if ((start + size) == base && start >= fw_dump.boot_mem_top) 893 is_adjacent = true; 894 } 895 if (!is_adjacent) { 896 /* resize the array on reaching the limit */ 897 if (mrange_info->mem_range_cnt == mrange_info->max_mem_ranges) { 898 int ret; 899 900 if (mrange_info->is_static) { 901 pr_err("Reached array size limit for %s memory ranges\n", 902 mrange_info->name); 903 return -ENOSPC; 904 } 905 906 ret = fadump_alloc_mem_ranges(mrange_info); 907 if (ret) 908 return ret; 909 910 /* Update to the new resized array */ 911 mem_ranges = mrange_info->mem_ranges; 912 } 913 914 start = base; 915 mem_ranges[mrange_info->mem_range_cnt].base = start; 916 mrange_info->mem_range_cnt++; 917 } 918 919 mem_ranges[mrange_info->mem_range_cnt - 1].size = (end - start); 920 pr_debug("%s_memory_range[%d] [%#016llx-%#016llx], %#llx bytes\n", 921 mrange_info->name, (mrange_info->mem_range_cnt - 1), 922 start, end - 1, (end - start)); 923 return 0; 924 } 925 926 static int fadump_init_elfcore_header(char *bufp) 927 { 928 struct elfhdr *elf; 929 930 elf = (struct elfhdr *) bufp; 931 bufp += sizeof(struct elfhdr); 932 memcpy(elf->e_ident, ELFMAG, SELFMAG); 933 elf->e_ident[EI_CLASS] = ELF_CLASS; 934 elf->e_ident[EI_DATA] = ELF_DATA; 935 elf->e_ident[EI_VERSION] = EV_CURRENT; 936 elf->e_ident[EI_OSABI] = ELF_OSABI; 937 memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD); 938 elf->e_type = ET_CORE; 939 elf->e_machine = ELF_ARCH; 940 elf->e_version = EV_CURRENT; 941 elf->e_entry = 0; 942 elf->e_phoff = sizeof(struct elfhdr); 943 elf->e_shoff = 0; 944 945 if (IS_ENABLED(CONFIG_PPC64_ELF_ABI_V2)) 946 elf->e_flags = 2; 947 else if (IS_ENABLED(CONFIG_PPC64_ELF_ABI_V1)) 948 elf->e_flags = 1; 949 else 950 elf->e_flags = 0; 951 952 elf->e_ehsize = sizeof(struct elfhdr); 953 elf->e_phentsize = sizeof(struct elf_phdr); 954 elf->e_phnum = 0; 955 elf->e_shentsize = 0; 956 elf->e_shnum = 0; 957 elf->e_shstrndx = 0; 958 959 return 0; 960 } 961 962 /* 963 * If the given physical address falls within the boot memory region then 964 * return the relocated address that points to the dump region reserved 965 * for saving initial boot memory contents. 966 */ 967 static inline unsigned long fadump_relocate(unsigned long paddr) 968 { 969 unsigned long raddr, rstart, rend, rlast, hole_size; 970 int i; 971 972 hole_size = 0; 973 rlast = 0; 974 raddr = paddr; 975 for (i = 0; i < fw_dump.boot_mem_regs_cnt; i++) { 976 rstart = fw_dump.boot_mem_addr[i]; 977 rend = rstart + fw_dump.boot_mem_sz[i]; 978 hole_size += (rstart - rlast); 979 980 if (paddr >= rstart && paddr < rend) { 981 raddr += fw_dump.boot_mem_dest_addr - hole_size; 982 break; 983 } 984 985 rlast = rend; 986 } 987 988 pr_debug("vmcoreinfo: paddr = 0x%lx, raddr = 0x%lx\n", paddr, raddr); 989 return raddr; 990 } 991 992 static void __init populate_elf_pt_load(struct elf_phdr *phdr, u64 start, 993 u64 size, unsigned long long offset) 994 { 995 phdr->p_align = 0; 996 phdr->p_memsz = size; 997 phdr->p_filesz = size; 998 phdr->p_paddr = start; 999 phdr->p_offset = offset; 1000 phdr->p_type = PT_LOAD; 1001 phdr->p_flags = PF_R|PF_W|PF_X; 1002 phdr->p_vaddr = (unsigned long)__va(start); 1003 } 1004 1005 static void __init fadump_populate_elfcorehdr(struct fadump_crash_info_header *fdh) 1006 { 1007 char *bufp; 1008 struct elfhdr *elf; 1009 struct elf_phdr *phdr; 1010 u64 boot_mem_dest_offset; 1011 unsigned long long i, ra_start, ra_end, ra_size, mstart, mend; 1012 1013 bufp = (char *) fw_dump.elfcorehdr_addr; 1014 fadump_init_elfcore_header(bufp); 1015 elf = (struct elfhdr *)bufp; 1016 bufp += sizeof(struct elfhdr); 1017 1018 /* 1019 * Set up ELF PT_NOTE, a placeholder for CPU notes information. 1020 * The notes info will be populated later by platform-specific code. 1021 * Hence, this PT_NOTE will always be the first ELF note. 1022 * 1023 * NOTE: Any new ELF note addition should be placed after this note. 1024 */ 1025 phdr = (struct elf_phdr *)bufp; 1026 bufp += sizeof(struct elf_phdr); 1027 phdr->p_type = PT_NOTE; 1028 phdr->p_flags = 0; 1029 phdr->p_vaddr = 0; 1030 phdr->p_align = 0; 1031 phdr->p_offset = 0; 1032 phdr->p_paddr = 0; 1033 phdr->p_filesz = 0; 1034 phdr->p_memsz = 0; 1035 /* Increment number of program headers. */ 1036 (elf->e_phnum)++; 1037 1038 /* setup ELF PT_NOTE for vmcoreinfo */ 1039 phdr = (struct elf_phdr *)bufp; 1040 bufp += sizeof(struct elf_phdr); 1041 phdr->p_type = PT_NOTE; 1042 phdr->p_flags = 0; 1043 phdr->p_vaddr = 0; 1044 phdr->p_align = 0; 1045 phdr->p_paddr = phdr->p_offset = fdh->vmcoreinfo_raddr; 1046 phdr->p_memsz = phdr->p_filesz = fdh->vmcoreinfo_size; 1047 /* Increment number of program headers. */ 1048 (elf->e_phnum)++; 1049 1050 /* 1051 * Setup PT_LOAD sections. first include boot memory regions 1052 * and then add rest of the memory regions. 1053 */ 1054 boot_mem_dest_offset = fw_dump.boot_mem_dest_addr; 1055 for (i = 0; i < fw_dump.boot_mem_regs_cnt; i++) { 1056 phdr = (struct elf_phdr *)bufp; 1057 bufp += sizeof(struct elf_phdr); 1058 populate_elf_pt_load(phdr, fw_dump.boot_mem_addr[i], 1059 fw_dump.boot_mem_sz[i], 1060 boot_mem_dest_offset); 1061 /* Increment number of program headers. */ 1062 (elf->e_phnum)++; 1063 boot_mem_dest_offset += fw_dump.boot_mem_sz[i]; 1064 } 1065 1066 /* Memory reserved for fadump in first kernel */ 1067 ra_start = fw_dump.reserve_dump_area_start; 1068 ra_size = get_fadump_area_size(); 1069 ra_end = ra_start + ra_size; 1070 1071 phdr = (struct elf_phdr *)bufp; 1072 for_each_mem_range(i, &mstart, &mend) { 1073 /* Boot memory regions already added, skip them now */ 1074 if (mstart < fw_dump.boot_mem_top) { 1075 if (mend > fw_dump.boot_mem_top) 1076 mstart = fw_dump.boot_mem_top; 1077 else 1078 continue; 1079 } 1080 1081 /* Handle memblock regions overlaps with fadump reserved area */ 1082 if ((ra_start < mend) && (ra_end > mstart)) { 1083 if ((mstart < ra_start) && (mend > ra_end)) { 1084 populate_elf_pt_load(phdr, mstart, ra_start - mstart, mstart); 1085 /* Increment number of program headers. */ 1086 (elf->e_phnum)++; 1087 bufp += sizeof(struct elf_phdr); 1088 phdr = (struct elf_phdr *)bufp; 1089 populate_elf_pt_load(phdr, ra_end, mend - ra_end, ra_end); 1090 } else if (mstart < ra_start) { 1091 populate_elf_pt_load(phdr, mstart, ra_start - mstart, mstart); 1092 } else if (ra_end < mend) { 1093 populate_elf_pt_load(phdr, ra_end, mend - ra_end, ra_end); 1094 } 1095 } else { 1096 /* No overlap with fadump reserved memory region */ 1097 populate_elf_pt_load(phdr, mstart, mend - mstart, mstart); 1098 } 1099 1100 /* Increment number of program headers. */ 1101 (elf->e_phnum)++; 1102 bufp += sizeof(struct elf_phdr); 1103 phdr = (struct elf_phdr *) bufp; 1104 } 1105 } 1106 1107 static unsigned long init_fadump_header(unsigned long addr) 1108 { 1109 struct fadump_crash_info_header *fdh; 1110 1111 if (!addr) 1112 return 0; 1113 1114 fdh = __va(addr); 1115 addr += sizeof(struct fadump_crash_info_header); 1116 1117 memset(fdh, 0, sizeof(struct fadump_crash_info_header)); 1118 fdh->magic_number = FADUMP_CRASH_INFO_MAGIC; 1119 fdh->version = FADUMP_HEADER_VERSION; 1120 /* We will set the crashing cpu id in crash_fadump() during crash. */ 1121 fdh->crashing_cpu = FADUMP_CPU_UNKNOWN; 1122 1123 /* 1124 * The physical address and size of vmcoreinfo are required in the 1125 * second kernel to prepare elfcorehdr. 1126 */ 1127 fdh->vmcoreinfo_raddr = fadump_relocate(paddr_vmcoreinfo_note()); 1128 fdh->vmcoreinfo_size = VMCOREINFO_NOTE_SIZE; 1129 1130 1131 fdh->pt_regs_sz = sizeof(struct pt_regs); 1132 /* 1133 * When LPAR is terminated by PYHP, ensure all possible CPUs' 1134 * register data is processed while exporting the vmcore. 1135 */ 1136 fdh->cpu_mask = *cpu_possible_mask; 1137 fdh->cpu_mask_sz = sizeof(struct cpumask); 1138 1139 return addr; 1140 } 1141 1142 static int register_fadump(void) 1143 { 1144 unsigned long addr; 1145 1146 /* 1147 * If no memory is reserved then we can not register for firmware- 1148 * assisted dump. 1149 */ 1150 if (!fw_dump.reserve_dump_area_size) 1151 return -ENODEV; 1152 1153 addr = fw_dump.fadumphdr_addr; 1154 1155 /* Initialize fadump crash info header. */ 1156 addr = init_fadump_header(addr); 1157 1158 /* register the future kernel dump with firmware. */ 1159 pr_debug("Registering for firmware-assisted kernel dump...\n"); 1160 return fw_dump.ops->fadump_register(&fw_dump); 1161 } 1162 1163 void fadump_cleanup(void) 1164 { 1165 if (!fw_dump.fadump_supported) 1166 return; 1167 1168 /* Invalidate the registration only if dump is active. */ 1169 if (fw_dump.dump_active) { 1170 pr_debug("Invalidating firmware-assisted dump registration\n"); 1171 fw_dump.ops->fadump_invalidate(&fw_dump); 1172 } else if (fw_dump.dump_registered) { 1173 /* Un-register Firmware-assisted dump if it was registered. */ 1174 fw_dump.ops->fadump_unregister(&fw_dump); 1175 } 1176 1177 if (fw_dump.ops->fadump_cleanup) 1178 fw_dump.ops->fadump_cleanup(&fw_dump); 1179 } 1180 1181 static void fadump_free_reserved_memory(unsigned long start_pfn, 1182 unsigned long end_pfn) 1183 { 1184 unsigned long pfn; 1185 unsigned long time_limit = jiffies + HZ; 1186 1187 pr_info("freeing reserved memory (0x%llx - 0x%llx)\n", 1188 PFN_PHYS(start_pfn), PFN_PHYS(end_pfn)); 1189 1190 for (pfn = start_pfn; pfn < end_pfn; pfn++) { 1191 free_reserved_page(pfn_to_page(pfn)); 1192 1193 if (time_after(jiffies, time_limit)) { 1194 cond_resched(); 1195 time_limit = jiffies + HZ; 1196 } 1197 } 1198 } 1199 1200 /* 1201 * Skip memory holes and free memory that was actually reserved. 1202 */ 1203 static void fadump_release_reserved_area(u64 start, u64 end) 1204 { 1205 unsigned long reg_spfn, reg_epfn; 1206 u64 tstart, tend, spfn, epfn; 1207 int i; 1208 1209 spfn = PHYS_PFN(start); 1210 epfn = PHYS_PFN(end); 1211 1212 for_each_mem_pfn_range(i, MAX_NUMNODES, ®_spfn, ®_epfn, NULL) { 1213 tstart = max_t(u64, spfn, reg_spfn); 1214 tend = min_t(u64, epfn, reg_epfn); 1215 1216 if (tstart < tend) { 1217 fadump_free_reserved_memory(tstart, tend); 1218 1219 if (tend == epfn) 1220 break; 1221 1222 spfn = tend; 1223 } 1224 } 1225 } 1226 1227 /* 1228 * Sort the mem ranges in-place and merge adjacent ranges 1229 * to minimize the memory ranges count. 1230 */ 1231 static void sort_and_merge_mem_ranges(struct fadump_mrange_info *mrange_info) 1232 { 1233 struct fadump_memory_range *mem_ranges; 1234 u64 base, size; 1235 int i, j, idx; 1236 1237 if (!reserved_mrange_info.mem_range_cnt) 1238 return; 1239 1240 /* Sort the memory ranges */ 1241 mem_ranges = mrange_info->mem_ranges; 1242 for (i = 0; i < mrange_info->mem_range_cnt; i++) { 1243 idx = i; 1244 for (j = (i + 1); j < mrange_info->mem_range_cnt; j++) { 1245 if (mem_ranges[idx].base > mem_ranges[j].base) 1246 idx = j; 1247 } 1248 if (idx != i) 1249 swap(mem_ranges[idx], mem_ranges[i]); 1250 } 1251 1252 /* Merge adjacent reserved ranges */ 1253 idx = 0; 1254 for (i = 1; i < mrange_info->mem_range_cnt; i++) { 1255 base = mem_ranges[i-1].base; 1256 size = mem_ranges[i-1].size; 1257 if (mem_ranges[i].base == (base + size)) 1258 mem_ranges[idx].size += mem_ranges[i].size; 1259 else { 1260 idx++; 1261 if (i == idx) 1262 continue; 1263 1264 mem_ranges[idx] = mem_ranges[i]; 1265 } 1266 } 1267 mrange_info->mem_range_cnt = idx + 1; 1268 } 1269 1270 /* 1271 * Scan reserved-ranges to consider them while reserving/releasing 1272 * memory for FADump. 1273 */ 1274 static void __init early_init_dt_scan_reserved_ranges(unsigned long node) 1275 { 1276 const __be32 *prop; 1277 int len, ret = -1; 1278 unsigned long i; 1279 1280 /* reserved-ranges already scanned */ 1281 if (reserved_mrange_info.mem_range_cnt != 0) 1282 return; 1283 1284 prop = of_get_flat_dt_prop(node, "reserved-ranges", &len); 1285 if (!prop) 1286 return; 1287 1288 /* 1289 * Each reserved range is an (address,size) pair, 2 cells each, 1290 * totalling 4 cells per range. 1291 */ 1292 for (i = 0; i < len / (sizeof(*prop) * 4); i++) { 1293 u64 base, size; 1294 1295 base = of_read_number(prop + (i * 4) + 0, 2); 1296 size = of_read_number(prop + (i * 4) + 2, 2); 1297 1298 if (size) { 1299 ret = fadump_add_mem_range(&reserved_mrange_info, 1300 base, base + size); 1301 if (ret < 0) { 1302 pr_warn("some reserved ranges are ignored!\n"); 1303 break; 1304 } 1305 } 1306 } 1307 1308 /* Compact reserved ranges */ 1309 sort_and_merge_mem_ranges(&reserved_mrange_info); 1310 } 1311 1312 /* 1313 * Release the memory that was reserved during early boot to preserve the 1314 * crash'ed kernel's memory contents except reserved dump area (permanent 1315 * reservation) and reserved ranges used by F/W. The released memory will 1316 * be available for general use. 1317 */ 1318 static void fadump_release_memory(u64 begin, u64 end) 1319 { 1320 u64 ra_start, ra_end, tstart; 1321 int i, ret; 1322 1323 ra_start = fw_dump.reserve_dump_area_start; 1324 ra_end = ra_start + fw_dump.reserve_dump_area_size; 1325 1326 /* 1327 * If reserved ranges array limit is hit, overwrite the last reserved 1328 * memory range with reserved dump area to ensure it is excluded from 1329 * the memory being released (reused for next FADump registration). 1330 */ 1331 if (reserved_mrange_info.mem_range_cnt == 1332 reserved_mrange_info.max_mem_ranges) 1333 reserved_mrange_info.mem_range_cnt--; 1334 1335 ret = fadump_add_mem_range(&reserved_mrange_info, ra_start, ra_end); 1336 if (ret != 0) 1337 return; 1338 1339 /* Get the reserved ranges list in order first. */ 1340 sort_and_merge_mem_ranges(&reserved_mrange_info); 1341 1342 /* Exclude reserved ranges and release remaining memory */ 1343 tstart = begin; 1344 for (i = 0; i < reserved_mrange_info.mem_range_cnt; i++) { 1345 ra_start = reserved_mrange_info.mem_ranges[i].base; 1346 ra_end = ra_start + reserved_mrange_info.mem_ranges[i].size; 1347 1348 if (tstart >= ra_end) 1349 continue; 1350 1351 if (tstart < ra_start) 1352 fadump_release_reserved_area(tstart, ra_start); 1353 tstart = ra_end; 1354 } 1355 1356 if (tstart < end) 1357 fadump_release_reserved_area(tstart, end); 1358 } 1359 1360 static void fadump_free_elfcorehdr_buf(void) 1361 { 1362 if (fw_dump.elfcorehdr_addr == 0 || fw_dump.elfcorehdr_size == 0) 1363 return; 1364 1365 /* 1366 * Before freeing the memory of `elfcorehdr`, reset the global 1367 * `elfcorehdr_addr` to prevent modules like `vmcore` from accessing 1368 * invalid memory. 1369 */ 1370 elfcorehdr_addr = ELFCORE_ADDR_ERR; 1371 fadump_free_buffer(fw_dump.elfcorehdr_addr, fw_dump.elfcorehdr_size); 1372 fw_dump.elfcorehdr_addr = 0; 1373 fw_dump.elfcorehdr_size = 0; 1374 } 1375 1376 static void fadump_invalidate_release_mem(void) 1377 { 1378 mutex_lock(&fadump_mutex); 1379 if (!fw_dump.dump_active) { 1380 mutex_unlock(&fadump_mutex); 1381 return; 1382 } 1383 1384 fadump_cleanup(); 1385 mutex_unlock(&fadump_mutex); 1386 1387 fadump_free_elfcorehdr_buf(); 1388 fadump_release_memory(fw_dump.boot_mem_top, memblock_end_of_DRAM()); 1389 fadump_free_cpu_notes_buf(); 1390 1391 /* 1392 * Setup kernel metadata and initialize the kernel dump 1393 * memory structure for FADump re-registration. 1394 */ 1395 if (fw_dump.ops->fadump_setup_metadata && 1396 (fw_dump.ops->fadump_setup_metadata(&fw_dump) < 0)) 1397 pr_warn("Failed to setup kernel metadata!\n"); 1398 fw_dump.ops->fadump_init_mem_struct(&fw_dump); 1399 } 1400 1401 static ssize_t release_mem_store(struct kobject *kobj, 1402 struct kobj_attribute *attr, 1403 const char *buf, size_t count) 1404 { 1405 int input = -1; 1406 1407 if (!fw_dump.dump_active) 1408 return -EPERM; 1409 1410 if (kstrtoint(buf, 0, &input)) 1411 return -EINVAL; 1412 1413 if (input == 1) { 1414 /* 1415 * Take away the '/proc/vmcore'. We are releasing the dump 1416 * memory, hence it will not be valid anymore. 1417 */ 1418 #ifdef CONFIG_PROC_VMCORE 1419 vmcore_cleanup(); 1420 #endif 1421 fadump_invalidate_release_mem(); 1422 1423 } else 1424 return -EINVAL; 1425 return count; 1426 } 1427 1428 /* Release the reserved memory and disable the FADump */ 1429 static void __init unregister_fadump(void) 1430 { 1431 fadump_cleanup(); 1432 fadump_release_memory(fw_dump.reserve_dump_area_start, 1433 fw_dump.reserve_dump_area_size); 1434 fw_dump.fadump_enabled = 0; 1435 kobject_put(fadump_kobj); 1436 } 1437 1438 static ssize_t enabled_show(struct kobject *kobj, 1439 struct kobj_attribute *attr, 1440 char *buf) 1441 { 1442 return sprintf(buf, "%d\n", fw_dump.fadump_enabled); 1443 } 1444 1445 /* 1446 * /sys/kernel/fadump/hotplug_ready sysfs node returns 1, which inidcates 1447 * to usersapce that fadump re-registration is not required on memory 1448 * hotplug events. 1449 */ 1450 static ssize_t hotplug_ready_show(struct kobject *kobj, 1451 struct kobj_attribute *attr, 1452 char *buf) 1453 { 1454 return sprintf(buf, "%d\n", 1); 1455 } 1456 1457 static ssize_t mem_reserved_show(struct kobject *kobj, 1458 struct kobj_attribute *attr, 1459 char *buf) 1460 { 1461 return sprintf(buf, "%ld\n", fw_dump.reserve_dump_area_size); 1462 } 1463 1464 static ssize_t registered_show(struct kobject *kobj, 1465 struct kobj_attribute *attr, 1466 char *buf) 1467 { 1468 return sprintf(buf, "%d\n", fw_dump.dump_registered); 1469 } 1470 1471 static ssize_t bootargs_append_show(struct kobject *kobj, 1472 struct kobj_attribute *attr, 1473 char *buf) 1474 { 1475 return sprintf(buf, "%s\n", (char *)__va(fw_dump.param_area)); 1476 } 1477 1478 static ssize_t bootargs_append_store(struct kobject *kobj, 1479 struct kobj_attribute *attr, 1480 const char *buf, size_t count) 1481 { 1482 char *params; 1483 1484 if (!fw_dump.fadump_enabled || fw_dump.dump_active) 1485 return -EPERM; 1486 1487 if (count >= COMMAND_LINE_SIZE) 1488 return -EINVAL; 1489 1490 /* 1491 * Fail here instead of handling this scenario with 1492 * some silly workaround in capture kernel. 1493 */ 1494 if (saved_command_line_len + count >= COMMAND_LINE_SIZE) { 1495 pr_err("Appending parameters exceeds cmdline size!\n"); 1496 return -ENOSPC; 1497 } 1498 1499 params = __va(fw_dump.param_area); 1500 strscpy_pad(params, buf, COMMAND_LINE_SIZE); 1501 /* Remove newline character at the end. */ 1502 if (params[count-1] == '\n') 1503 params[count-1] = '\0'; 1504 1505 return count; 1506 } 1507 1508 static ssize_t registered_store(struct kobject *kobj, 1509 struct kobj_attribute *attr, 1510 const char *buf, size_t count) 1511 { 1512 int ret = 0; 1513 int input = -1; 1514 1515 if (!fw_dump.fadump_enabled || fw_dump.dump_active) 1516 return -EPERM; 1517 1518 if (kstrtoint(buf, 0, &input)) 1519 return -EINVAL; 1520 1521 mutex_lock(&fadump_mutex); 1522 1523 switch (input) { 1524 case 0: 1525 if (fw_dump.dump_registered == 0) { 1526 goto unlock_out; 1527 } 1528 1529 /* Un-register Firmware-assisted dump */ 1530 pr_debug("Un-register firmware-assisted dump\n"); 1531 fw_dump.ops->fadump_unregister(&fw_dump); 1532 break; 1533 case 1: 1534 if (fw_dump.dump_registered == 1) { 1535 /* Un-register Firmware-assisted dump */ 1536 fw_dump.ops->fadump_unregister(&fw_dump); 1537 } 1538 /* Register Firmware-assisted dump */ 1539 ret = register_fadump(); 1540 break; 1541 default: 1542 ret = -EINVAL; 1543 break; 1544 } 1545 1546 unlock_out: 1547 mutex_unlock(&fadump_mutex); 1548 return ret < 0 ? ret : count; 1549 } 1550 1551 static int fadump_region_show(struct seq_file *m, void *private) 1552 { 1553 if (!fw_dump.fadump_enabled) 1554 return 0; 1555 1556 mutex_lock(&fadump_mutex); 1557 fw_dump.ops->fadump_region_show(&fw_dump, m); 1558 mutex_unlock(&fadump_mutex); 1559 return 0; 1560 } 1561 1562 static struct kobj_attribute release_attr = __ATTR_WO(release_mem); 1563 static struct kobj_attribute enable_attr = __ATTR_RO(enabled); 1564 static struct kobj_attribute register_attr = __ATTR_RW(registered); 1565 static struct kobj_attribute mem_reserved_attr = __ATTR_RO(mem_reserved); 1566 static struct kobj_attribute hotplug_ready_attr = __ATTR_RO(hotplug_ready); 1567 static struct kobj_attribute bootargs_append_attr = __ATTR_RW(bootargs_append); 1568 1569 static struct attribute *fadump_attrs[] = { 1570 &enable_attr.attr, 1571 ®ister_attr.attr, 1572 &mem_reserved_attr.attr, 1573 &hotplug_ready_attr.attr, 1574 NULL, 1575 }; 1576 1577 ATTRIBUTE_GROUPS(fadump); 1578 1579 DEFINE_SHOW_ATTRIBUTE(fadump_region); 1580 1581 static void __init fadump_init_files(void) 1582 { 1583 int rc = 0; 1584 1585 fadump_kobj = kobject_create_and_add("fadump", kernel_kobj); 1586 if (!fadump_kobj) { 1587 pr_err("failed to create fadump kobject\n"); 1588 return; 1589 } 1590 1591 if (fw_dump.param_area) { 1592 rc = sysfs_create_file(fadump_kobj, &bootargs_append_attr.attr); 1593 if (rc) 1594 pr_err("unable to create bootargs_append sysfs file (%d)\n", rc); 1595 } 1596 1597 debugfs_create_file("fadump_region", 0444, arch_debugfs_dir, NULL, 1598 &fadump_region_fops); 1599 1600 if (fw_dump.dump_active) { 1601 rc = sysfs_create_file(fadump_kobj, &release_attr.attr); 1602 if (rc) 1603 pr_err("unable to create release_mem sysfs file (%d)\n", 1604 rc); 1605 } 1606 1607 rc = sysfs_create_groups(fadump_kobj, fadump_groups); 1608 if (rc) { 1609 pr_err("sysfs group creation failed (%d), unregistering FADump", 1610 rc); 1611 unregister_fadump(); 1612 return; 1613 } 1614 1615 /* 1616 * The FADump sysfs are moved from kernel_kobj to fadump_kobj need to 1617 * create symlink at old location to maintain backward compatibility. 1618 * 1619 * - fadump_enabled -> fadump/enabled 1620 * - fadump_registered -> fadump/registered 1621 * - fadump_release_mem -> fadump/release_mem 1622 */ 1623 rc = compat_only_sysfs_link_entry_to_kobj(kernel_kobj, fadump_kobj, 1624 "enabled", "fadump_enabled"); 1625 if (rc) { 1626 pr_err("unable to create fadump_enabled symlink (%d)", rc); 1627 return; 1628 } 1629 1630 rc = compat_only_sysfs_link_entry_to_kobj(kernel_kobj, fadump_kobj, 1631 "registered", 1632 "fadump_registered"); 1633 if (rc) { 1634 pr_err("unable to create fadump_registered symlink (%d)", rc); 1635 sysfs_remove_link(kernel_kobj, "fadump_enabled"); 1636 return; 1637 } 1638 1639 if (fw_dump.dump_active) { 1640 rc = compat_only_sysfs_link_entry_to_kobj(kernel_kobj, 1641 fadump_kobj, 1642 "release_mem", 1643 "fadump_release_mem"); 1644 if (rc) 1645 pr_err("unable to create fadump_release_mem symlink (%d)", 1646 rc); 1647 } 1648 return; 1649 } 1650 1651 static int __init fadump_setup_elfcorehdr_buf(void) 1652 { 1653 int elf_phdr_cnt; 1654 unsigned long elfcorehdr_size; 1655 1656 /* 1657 * Program header for CPU notes comes first, followed by one for 1658 * vmcoreinfo, and the remaining program headers correspond to 1659 * memory regions. 1660 */ 1661 elf_phdr_cnt = 2 + fw_dump.boot_mem_regs_cnt + memblock_num_regions(memory); 1662 elfcorehdr_size = sizeof(struct elfhdr) + (elf_phdr_cnt * sizeof(struct elf_phdr)); 1663 elfcorehdr_size = PAGE_ALIGN(elfcorehdr_size); 1664 1665 fw_dump.elfcorehdr_addr = (u64)fadump_alloc_buffer(elfcorehdr_size); 1666 if (!fw_dump.elfcorehdr_addr) { 1667 pr_err("Failed to allocate %lu bytes for elfcorehdr\n", 1668 elfcorehdr_size); 1669 return -ENOMEM; 1670 } 1671 fw_dump.elfcorehdr_size = elfcorehdr_size; 1672 return 0; 1673 } 1674 1675 /* 1676 * Check if the fadump header of crashed kernel is compatible with fadump kernel. 1677 * 1678 * It checks the magic number, endianness, and size of non-primitive type 1679 * members of fadump header to ensure safe dump collection. 1680 */ 1681 static bool __init is_fadump_header_compatible(struct fadump_crash_info_header *fdh) 1682 { 1683 if (fdh->magic_number == FADUMP_CRASH_INFO_MAGIC_OLD) { 1684 pr_err("Old magic number, can't process the dump.\n"); 1685 return false; 1686 } 1687 1688 if (fdh->magic_number != FADUMP_CRASH_INFO_MAGIC) { 1689 if (fdh->magic_number == swab64(FADUMP_CRASH_INFO_MAGIC)) 1690 pr_err("Endianness mismatch between the crashed and fadump kernels.\n"); 1691 else 1692 pr_err("Fadump header is corrupted.\n"); 1693 1694 return false; 1695 } 1696 1697 /* 1698 * Dump collection is not safe if the size of non-primitive type members 1699 * of the fadump header do not match between crashed and fadump kernel. 1700 */ 1701 if (fdh->pt_regs_sz != sizeof(struct pt_regs) || 1702 fdh->cpu_mask_sz != sizeof(struct cpumask)) { 1703 pr_err("Fadump header size mismatch.\n"); 1704 return false; 1705 } 1706 1707 return true; 1708 } 1709 1710 static void __init fadump_process(void) 1711 { 1712 struct fadump_crash_info_header *fdh; 1713 1714 fdh = (struct fadump_crash_info_header *) __va(fw_dump.fadumphdr_addr); 1715 if (!fdh) { 1716 pr_err("Crash info header is empty.\n"); 1717 goto err_out; 1718 } 1719 1720 /* Avoid processing the dump if fadump header isn't compatible */ 1721 if (!is_fadump_header_compatible(fdh)) 1722 goto err_out; 1723 1724 /* Allocate buffer for elfcorehdr */ 1725 if (fadump_setup_elfcorehdr_buf()) 1726 goto err_out; 1727 1728 fadump_populate_elfcorehdr(fdh); 1729 1730 /* Let platform update the CPU notes in elfcorehdr */ 1731 if (fw_dump.ops->fadump_process(&fw_dump) < 0) 1732 goto err_out; 1733 1734 /* 1735 * elfcorehdr is now ready to be exported. 1736 * 1737 * set elfcorehdr_addr so that vmcore module will export the 1738 * elfcorehdr through '/proc/vmcore'. 1739 */ 1740 elfcorehdr_addr = virt_to_phys((void *)fw_dump.elfcorehdr_addr); 1741 return; 1742 1743 err_out: 1744 fadump_invalidate_release_mem(); 1745 } 1746 1747 /* 1748 * Reserve memory to store additional parameters to be passed 1749 * for fadump/capture kernel. 1750 */ 1751 void __init fadump_setup_param_area(void) 1752 { 1753 phys_addr_t range_start, range_end; 1754 1755 if (!fw_dump.param_area_supported || fw_dump.dump_active) 1756 return; 1757 1758 /* This memory can't be used by PFW or bootloader as it is shared across kernels */ 1759 if (early_radix_enabled()) { 1760 /* 1761 * Anywhere in the upper half should be good enough as all memory 1762 * is accessible in real mode. 1763 */ 1764 range_start = memblock_end_of_DRAM() / 2; 1765 range_end = memblock_end_of_DRAM(); 1766 } else { 1767 /* 1768 * Memory range for passing additional parameters for HASH MMU 1769 * must meet the following conditions: 1770 * 1. The first memory block size must be higher than the 1771 * minimum RMA (MIN_RMA) size. Bootloader can use memory 1772 * upto RMA size. So it should be avoided. 1773 * 2. The range should be between MIN_RMA and RMA size (ppc64_rma_size) 1774 * 3. It must not overlap with the fadump reserved area. 1775 */ 1776 if (ppc64_rma_size < MIN_RMA*1024*1024) 1777 return; 1778 1779 range_start = MIN_RMA * 1024 * 1024; 1780 range_end = min(ppc64_rma_size, fw_dump.boot_mem_top); 1781 } 1782 1783 fw_dump.param_area = memblock_phys_alloc_range(COMMAND_LINE_SIZE, 1784 COMMAND_LINE_SIZE, 1785 range_start, 1786 range_end); 1787 if (!fw_dump.param_area) { 1788 pr_warn("WARNING: Could not setup area to pass additional parameters!\n"); 1789 return; 1790 } 1791 1792 memset((void *)fw_dump.param_area, 0, COMMAND_LINE_SIZE); 1793 } 1794 1795 /* 1796 * Prepare for firmware-assisted dump. 1797 */ 1798 int __init setup_fadump(void) 1799 { 1800 if (!fw_dump.fadump_supported) 1801 return 0; 1802 1803 fadump_init_files(); 1804 fadump_show_config(); 1805 1806 if (!fw_dump.fadump_enabled) 1807 return 1; 1808 1809 /* 1810 * If dump data is available then see if it is valid and prepare for 1811 * saving it to the disk. 1812 */ 1813 if (fw_dump.dump_active) { 1814 fadump_process(); 1815 } 1816 /* Initialize the kernel dump memory structure and register with f/w */ 1817 else if (fw_dump.reserve_dump_area_size) { 1818 fw_dump.ops->fadump_init_mem_struct(&fw_dump); 1819 register_fadump(); 1820 } 1821 1822 /* 1823 * In case of panic, fadump is triggered via ppc_panic_event() 1824 * panic notifier. Setting crash_kexec_post_notifiers to 'true' 1825 * lets panic() function take crash friendly path before panic 1826 * notifiers are invoked. 1827 */ 1828 crash_kexec_post_notifiers = true; 1829 1830 return 1; 1831 } 1832 /* 1833 * Use subsys_initcall_sync() here because there is dependency with 1834 * crash_save_vmcoreinfo_init(), which must run first to ensure vmcoreinfo initialization 1835 * is done before registering with f/w. 1836 */ 1837 subsys_initcall_sync(setup_fadump); 1838 #else /* !CONFIG_PRESERVE_FA_DUMP */ 1839 1840 /* Scan the Firmware Assisted dump configuration details. */ 1841 int __init early_init_dt_scan_fw_dump(unsigned long node, const char *uname, 1842 int depth, void *data) 1843 { 1844 if ((depth != 1) || (strcmp(uname, "ibm,opal") != 0)) 1845 return 0; 1846 1847 opal_fadump_dt_scan(&fw_dump, node); 1848 return 1; 1849 } 1850 1851 /* 1852 * When dump is active but PRESERVE_FA_DUMP is enabled on the kernel, 1853 * preserve crash data. The subsequent memory preserving kernel boot 1854 * is likely to process this crash data. 1855 */ 1856 int __init fadump_reserve_mem(void) 1857 { 1858 if (fw_dump.dump_active) { 1859 /* 1860 * If last boot has crashed then reserve all the memory 1861 * above boot memory to preserve crash data. 1862 */ 1863 pr_info("Preserving crash data for processing in next boot.\n"); 1864 fadump_reserve_crash_area(fw_dump.boot_mem_top); 1865 } else 1866 pr_debug("FADump-aware kernel..\n"); 1867 1868 return 1; 1869 } 1870 #endif /* CONFIG_PRESERVE_FA_DUMP */ 1871 1872 /* Preserve everything above the base address */ 1873 static void __init fadump_reserve_crash_area(u64 base) 1874 { 1875 u64 i, mstart, mend, msize; 1876 1877 for_each_mem_range(i, &mstart, &mend) { 1878 msize = mend - mstart; 1879 1880 if ((mstart + msize) < base) 1881 continue; 1882 1883 if (mstart < base) { 1884 msize -= (base - mstart); 1885 mstart = base; 1886 } 1887 1888 pr_info("Reserving %lluMB of memory at %#016llx for preserving crash data", 1889 (msize >> 20), mstart); 1890 memblock_reserve(mstart, msize); 1891 } 1892 } 1893