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