1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * ppc64 code to implement the kexec_file_load syscall 4 * 5 * Copyright (C) 2004 Adam Litke (agl@us.ibm.com) 6 * Copyright (C) 2004 IBM Corp. 7 * Copyright (C) 2004,2005 Milton D Miller II, IBM Corporation 8 * Copyright (C) 2005 R Sharada (sharada@in.ibm.com) 9 * Copyright (C) 2006 Mohan Kumar M (mohan@in.ibm.com) 10 * Copyright (C) 2020 IBM Corporation 11 * 12 * Based on kexec-tools' kexec-ppc64.c, kexec-elf-rel-ppc64.c, fs2dt.c. 13 * Heavily modified for the kernel by 14 * Hari Bathini, IBM Corporation. 15 */ 16 17 #include <linux/kexec.h> 18 #include <linux/of_fdt.h> 19 #include <linux/libfdt.h> 20 #include <linux/of.h> 21 #include <linux/of_address.h> 22 #include <linux/memblock.h> 23 #include <linux/slab.h> 24 #include <linux/vmalloc.h> 25 #include <asm/setup.h> 26 #include <asm/drmem.h> 27 #include <asm/firmware.h> 28 #include <asm/kexec_ranges.h> 29 #include <asm/crashdump-ppc64.h> 30 #include <asm/mmzone.h> 31 #include <asm/iommu.h> 32 #include <asm/prom.h> 33 #include <asm/plpks.h> 34 #include <asm/cputhreads.h> 35 36 struct umem_info { 37 __be64 *buf; /* data buffer for usable-memory property */ 38 u32 size; /* size allocated for the data buffer */ 39 u32 max_entries; /* maximum no. of entries */ 40 u32 idx; /* index of current entry */ 41 42 /* usable memory ranges to look up */ 43 unsigned int nr_ranges; 44 const struct range *ranges; 45 }; 46 47 const struct kexec_file_ops * const kexec_file_loaders[] = { 48 &kexec_elf64_ops, 49 NULL 50 }; 51 52 int arch_check_excluded_range(struct kimage *image, unsigned long start, 53 unsigned long end) 54 { 55 struct crash_mem *emem; 56 int i; 57 58 emem = image->arch.exclude_ranges; 59 for (i = 0; i < emem->nr_ranges; i++) 60 if (start <= emem->ranges[i].end && end >= emem->ranges[i].start) 61 return 1; 62 63 return 0; 64 } 65 66 #ifdef CONFIG_CRASH_DUMP 67 /** 68 * check_realloc_usable_mem - Reallocate buffer if it can't accommodate entries 69 * @um_info: Usable memory buffer and ranges info. 70 * @cnt: No. of entries to accommodate. 71 * 72 * Frees up the old buffer if memory reallocation fails. 73 * 74 * Returns buffer on success, NULL on error. 75 */ 76 static __be64 *check_realloc_usable_mem(struct umem_info *um_info, int cnt) 77 { 78 u32 new_size; 79 __be64 *tbuf; 80 81 if ((um_info->idx + cnt) <= um_info->max_entries) 82 return um_info->buf; 83 84 new_size = um_info->size + MEM_RANGE_CHUNK_SZ; 85 tbuf = krealloc(um_info->buf, new_size, GFP_KERNEL); 86 if (tbuf) { 87 um_info->buf = tbuf; 88 um_info->size = new_size; 89 um_info->max_entries = (um_info->size / sizeof(u64)); 90 } 91 92 return tbuf; 93 } 94 95 /** 96 * add_usable_mem - Add the usable memory ranges within the given memory range 97 * to the buffer 98 * @um_info: Usable memory buffer and ranges info. 99 * @base: Base address of memory range to look for. 100 * @end: End address of memory range to look for. 101 * 102 * Returns 0 on success, negative errno on error. 103 */ 104 static int add_usable_mem(struct umem_info *um_info, u64 base, u64 end) 105 { 106 u64 loc_base, loc_end; 107 bool add; 108 int i; 109 110 for (i = 0; i < um_info->nr_ranges; i++) { 111 add = false; 112 loc_base = um_info->ranges[i].start; 113 loc_end = um_info->ranges[i].end; 114 if (loc_base >= base && loc_end <= end) 115 add = true; 116 else if (base <= loc_end && end >= loc_base) { 117 if (loc_base < base) 118 loc_base = base; 119 if (loc_end > end) 120 loc_end = end; 121 add = true; 122 } 123 124 if (add) { 125 if (!check_realloc_usable_mem(um_info, 2)) 126 return -ENOMEM; 127 128 um_info->buf[um_info->idx++] = cpu_to_be64(loc_base); 129 um_info->buf[um_info->idx++] = 130 cpu_to_be64(loc_end - loc_base + 1); 131 } 132 } 133 134 return 0; 135 } 136 137 /** 138 * kdump_setup_usable_lmb - This is a callback function that gets called by 139 * walk_drmem_lmbs for every LMB to set its 140 * usable memory ranges. 141 * @lmb: LMB info. 142 * @usm: linux,drconf-usable-memory property value. 143 * @data: Pointer to usable memory buffer and ranges info. 144 * 145 * Returns 0 on success, negative errno on error. 146 */ 147 static int kdump_setup_usable_lmb(struct drmem_lmb *lmb, const __be32 **usm, 148 void *data) 149 { 150 struct umem_info *um_info; 151 int tmp_idx, ret; 152 u64 base, end; 153 154 /* 155 * kdump load isn't supported on kernels already booted with 156 * linux,drconf-usable-memory property. 157 */ 158 if (*usm) { 159 pr_err("linux,drconf-usable-memory property already exists!"); 160 return -EINVAL; 161 } 162 163 um_info = data; 164 tmp_idx = um_info->idx; 165 if (!check_realloc_usable_mem(um_info, 1)) 166 return -ENOMEM; 167 168 um_info->idx++; 169 base = lmb->base_addr; 170 end = base + drmem_lmb_size() - 1; 171 ret = add_usable_mem(um_info, base, end); 172 if (!ret) { 173 /* 174 * Update the no. of ranges added. Two entries (base & size) 175 * for every range added. 176 */ 177 um_info->buf[tmp_idx] = 178 cpu_to_be64((um_info->idx - tmp_idx - 1) / 2); 179 } 180 181 return ret; 182 } 183 184 #define NODE_PATH_LEN 256 185 /** 186 * add_usable_mem_property - Add usable memory property for the given 187 * memory node. 188 * @fdt: Flattened device tree for the kdump kernel. 189 * @dn: Memory node. 190 * @um_info: Usable memory buffer and ranges info. 191 * 192 * Returns 0 on success, negative errno on error. 193 */ 194 static int add_usable_mem_property(void *fdt, struct device_node *dn, 195 struct umem_info *um_info) 196 { 197 int node; 198 char path[NODE_PATH_LEN]; 199 int i, ret; 200 u64 base, size; 201 202 of_node_get(dn); 203 204 if (snprintf(path, NODE_PATH_LEN, "%pOF", dn) > (NODE_PATH_LEN - 1)) { 205 pr_err("Buffer (%d) too small for memory node: %pOF\n", 206 NODE_PATH_LEN, dn); 207 return -EOVERFLOW; 208 } 209 kexec_dprintk("Memory node path: %s\n", path); 210 211 /* Now that we know the path, find its offset in kdump kernel's fdt */ 212 node = fdt_path_offset(fdt, path); 213 if (node < 0) { 214 pr_err("Malformed device tree: error reading %s\n", path); 215 ret = -EINVAL; 216 goto out; 217 } 218 219 um_info->idx = 0; 220 if (!check_realloc_usable_mem(um_info, 2)) { 221 ret = -ENOMEM; 222 goto out; 223 } 224 225 /* 226 * "reg" property represents sequence of (addr,size) tuples 227 * each representing a memory range. 228 */ 229 for (i = 0; ; i++) { 230 ret = of_property_read_reg(dn, i, &base, &size); 231 if (ret) 232 break; 233 234 ret = add_usable_mem(um_info, base, base + size - 1); 235 if (ret) 236 goto out; 237 } 238 239 // No reg or empty reg? Skip this node. 240 if (i == 0) 241 goto out; 242 243 /* 244 * No kdump kernel usable memory found in this memory node. 245 * Write (0,0) tuple in linux,usable-memory property for 246 * this region to be ignored. 247 */ 248 if (um_info->idx == 0) { 249 um_info->buf[0] = 0; 250 um_info->buf[1] = 0; 251 um_info->idx = 2; 252 } 253 254 ret = fdt_setprop(fdt, node, "linux,usable-memory", um_info->buf, 255 (um_info->idx * sizeof(u64))); 256 257 out: 258 of_node_put(dn); 259 return ret; 260 } 261 262 263 /** 264 * update_usable_mem_fdt - Updates kdump kernel's fdt with linux,usable-memory 265 * and linux,drconf-usable-memory DT properties as 266 * appropriate to restrict its memory usage. 267 * @fdt: Flattened device tree for the kdump kernel. 268 * @usable_mem: Usable memory ranges for kdump kernel. 269 * 270 * Returns 0 on success, negative errno on error. 271 */ 272 static int update_usable_mem_fdt(void *fdt, struct crash_mem *usable_mem) 273 { 274 struct umem_info um_info; 275 struct device_node *dn; 276 int node, ret = 0; 277 278 if (!usable_mem) { 279 pr_err("Usable memory ranges for kdump kernel not found\n"); 280 return -ENOENT; 281 } 282 283 node = fdt_path_offset(fdt, "/ibm,dynamic-reconfiguration-memory"); 284 if (node == -FDT_ERR_NOTFOUND) 285 kexec_dprintk("No dynamic reconfiguration memory found\n"); 286 else if (node < 0) { 287 pr_err("Malformed device tree: error reading /ibm,dynamic-reconfiguration-memory.\n"); 288 return -EINVAL; 289 } 290 291 um_info.buf = NULL; 292 um_info.size = 0; 293 um_info.max_entries = 0; 294 um_info.idx = 0; 295 /* Memory ranges to look up */ 296 um_info.ranges = &(usable_mem->ranges[0]); 297 um_info.nr_ranges = usable_mem->nr_ranges; 298 299 dn = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory"); 300 if (dn) { 301 ret = walk_drmem_lmbs(dn, &um_info, kdump_setup_usable_lmb); 302 of_node_put(dn); 303 304 if (ret) { 305 pr_err("Could not setup linux,drconf-usable-memory property for kdump\n"); 306 goto out; 307 } 308 309 ret = fdt_setprop(fdt, node, "linux,drconf-usable-memory", 310 um_info.buf, (um_info.idx * sizeof(u64))); 311 if (ret) { 312 pr_err("Failed to update fdt with linux,drconf-usable-memory property: %s", 313 fdt_strerror(ret)); 314 goto out; 315 } 316 } 317 318 /* 319 * Walk through each memory node and set linux,usable-memory property 320 * for the corresponding node in kdump kernel's fdt. 321 */ 322 for_each_node_by_type(dn, "memory") { 323 ret = add_usable_mem_property(fdt, dn, &um_info); 324 if (ret) { 325 pr_err("Failed to set linux,usable-memory property for %s node", 326 dn->full_name); 327 of_node_put(dn); 328 goto out; 329 } 330 } 331 332 out: 333 kfree(um_info.buf); 334 return ret; 335 } 336 337 /** 338 * load_backup_segment - Locate a memory hole to place the backup region. 339 * @image: Kexec image. 340 * @kbuf: Buffer contents and memory parameters. 341 * 342 * Returns 0 on success, negative errno on error. 343 */ 344 static int load_backup_segment(struct kimage *image, struct kexec_buf *kbuf) 345 { 346 void *buf; 347 int ret; 348 349 /* 350 * Setup a source buffer for backup segment. 351 * 352 * A source buffer has no meaning for backup region as data will 353 * be copied from backup source, after crash, in the purgatory. 354 * But as load segment code doesn't recognize such segments, 355 * setup a dummy source buffer to keep it happy for now. 356 */ 357 buf = vzalloc(BACKUP_SRC_SIZE); 358 if (!buf) 359 return -ENOMEM; 360 361 kbuf->buffer = buf; 362 kbuf->mem = KEXEC_BUF_MEM_UNKNOWN; 363 kbuf->bufsz = kbuf->memsz = BACKUP_SRC_SIZE; 364 kbuf->top_down = false; 365 366 ret = kexec_add_buffer(kbuf); 367 if (ret) { 368 vfree(buf); 369 return ret; 370 } 371 372 image->arch.backup_buf = buf; 373 image->arch.backup_start = kbuf->mem; 374 return 0; 375 } 376 377 static unsigned int kdump_extra_elfcorehdr_size(struct crash_mem *cmem) 378 { 379 #if defined(CONFIG_CRASH_HOTPLUG) && defined(CONFIG_MEMORY_HOTPLUG) 380 if (CONFIG_CRASH_MAX_MEMORY_RANGES > (unsigned int)PN_XNUM) 381 pr_warn("Number of Phdrs %u exceeds max\n", CONFIG_CRASH_MAX_MEMORY_RANGES); 382 else if (cmem->nr_ranges >= CONFIG_CRASH_MAX_MEMORY_RANGES) 383 pr_warn("Configured crash mem ranges may not be enough\n"); 384 else 385 return (CONFIG_CRASH_MAX_MEMORY_RANGES - cmem->nr_ranges) * sizeof(Elf64_Phdr); 386 #endif 387 return 0; 388 } 389 390 /** 391 * load_elfcorehdr_segment - Setup crash memory ranges and initialize elfcorehdr 392 * segment needed to load kdump kernel. 393 * @image: Kexec image. 394 * @kbuf: Buffer contents and memory parameters. 395 * 396 * Returns 0 on success, negative errno on error. 397 */ 398 static int load_elfcorehdr_segment(struct kimage *image, struct kexec_buf *kbuf) 399 { 400 struct crash_mem *cmem = NULL; 401 unsigned long headers_sz; 402 void *headers = NULL; 403 int ret; 404 405 ret = get_crash_memory_ranges(&cmem); 406 if (ret) 407 goto out; 408 409 /* Setup elfcorehdr segment */ 410 ret = crash_prepare_elf64_headers(cmem, false, &headers, &headers_sz); 411 if (ret) { 412 pr_err("Failed to prepare elf headers for the core\n"); 413 goto out; 414 } 415 416 /* Fix the offset for backup region in the ELF header */ 417 sync_backup_region_phdr(image, headers, false); 418 419 kbuf->buffer = headers; 420 kbuf->mem = KEXEC_BUF_MEM_UNKNOWN; 421 kbuf->bufsz = headers_sz; 422 423 /* 424 * Account for extra space required to accommodate additional memory 425 * ranges in elfcorehdr due to memory hotplug events. 426 */ 427 kbuf->memsz = headers_sz + kdump_extra_elfcorehdr_size(cmem); 428 kbuf->top_down = false; 429 430 ret = kexec_add_buffer(kbuf); 431 if (ret) { 432 vfree(headers); 433 goto out; 434 } 435 436 image->elf_load_addr = kbuf->mem; 437 438 /* 439 * If CONFIG_CRASH_HOTPLUG is enabled, the elfcorehdr kexec segment 440 * memsz can be larger than bufsz. Always initialize elf_headers_sz 441 * with memsz. This ensures the correct size is reserved for elfcorehdr 442 * memory in the FDT prepared for kdump. 443 */ 444 image->elf_headers_sz = kbuf->memsz; 445 image->elf_headers = headers; 446 out: 447 kfree(cmem); 448 return ret; 449 } 450 451 /** 452 * load_crashdump_segments_ppc64 - Initialize the additional segements needed 453 * to load kdump kernel. 454 * @image: Kexec image. 455 * @kbuf: Buffer contents and memory parameters. 456 * 457 * Returns 0 on success, negative errno on error. 458 */ 459 int load_crashdump_segments_ppc64(struct kimage *image, 460 struct kexec_buf *kbuf) 461 { 462 int ret; 463 464 /* Load backup segment - first 64K bytes of the crashing kernel */ 465 ret = load_backup_segment(image, kbuf); 466 if (ret) { 467 pr_err("Failed to load backup segment\n"); 468 return ret; 469 } 470 kexec_dprintk("Loaded the backup region at 0x%lx\n", kbuf->mem); 471 472 /* Load elfcorehdr segment - to export crashing kernel's vmcore */ 473 ret = load_elfcorehdr_segment(image, kbuf); 474 if (ret) { 475 pr_err("Failed to load elfcorehdr segment\n"); 476 return ret; 477 } 478 kexec_dprintk("Loaded elf core header at 0x%lx, bufsz=0x%lx memsz=0x%lx\n", 479 image->elf_load_addr, kbuf->bufsz, kbuf->memsz); 480 481 return 0; 482 } 483 #endif 484 485 /** 486 * setup_purgatory_ppc64 - initialize PPC64 specific purgatory's global 487 * variables and call setup_purgatory() to initialize 488 * common global variable. 489 * @image: kexec image. 490 * @slave_code: Slave code for the purgatory. 491 * @fdt: Flattened device tree for the next kernel. 492 * @kernel_load_addr: Address where the kernel is loaded. 493 * @fdt_load_addr: Address where the flattened device tree is loaded. 494 * 495 * Returns 0 on success, negative errno on error. 496 */ 497 int setup_purgatory_ppc64(struct kimage *image, const void *slave_code, 498 const void *fdt, unsigned long kernel_load_addr, 499 unsigned long fdt_load_addr) 500 { 501 struct device_node *dn = NULL; 502 int ret; 503 504 ret = setup_purgatory(image, slave_code, fdt, kernel_load_addr, 505 fdt_load_addr); 506 if (ret) 507 goto out; 508 509 if (image->type == KEXEC_TYPE_CRASH) { 510 u32 my_run_at_load = 1; 511 512 /* 513 * Tell relocatable kernel to run at load address 514 * via the word meant for that at 0x5c. 515 */ 516 ret = kexec_purgatory_get_set_symbol(image, "run_at_load", 517 &my_run_at_load, 518 sizeof(my_run_at_load), 519 false); 520 if (ret) 521 goto out; 522 } 523 524 /* Tell purgatory where to look for backup region */ 525 ret = kexec_purgatory_get_set_symbol(image, "backup_start", 526 &image->arch.backup_start, 527 sizeof(image->arch.backup_start), 528 false); 529 if (ret) 530 goto out; 531 532 /* Setup OPAL base & entry values */ 533 dn = of_find_node_by_path("/ibm,opal"); 534 if (dn) { 535 u64 val; 536 537 ret = of_property_read_u64(dn, "opal-base-address", &val); 538 if (ret) 539 goto out; 540 541 ret = kexec_purgatory_get_set_symbol(image, "opal_base", &val, 542 sizeof(val), false); 543 if (ret) 544 goto out; 545 546 ret = of_property_read_u64(dn, "opal-entry-address", &val); 547 if (ret) 548 goto out; 549 ret = kexec_purgatory_get_set_symbol(image, "opal_entry", &val, 550 sizeof(val), false); 551 } 552 out: 553 if (ret) 554 pr_err("Failed to setup purgatory symbols"); 555 of_node_put(dn); 556 return ret; 557 } 558 559 /** 560 * cpu_node_size - Compute the size of a CPU node in the FDT. 561 * This should be done only once and the value is stored in 562 * a static variable. 563 * Returns the max size of a CPU node in the FDT. 564 */ 565 static unsigned int cpu_node_size(void) 566 { 567 static unsigned int size; 568 struct device_node *dn; 569 struct property *pp; 570 571 /* 572 * Don't compute it twice, we are assuming that the per CPU node size 573 * doesn't change during the system's life. 574 */ 575 if (size) 576 return size; 577 578 dn = of_find_node_by_type(NULL, "cpu"); 579 if (WARN_ON_ONCE(!dn)) { 580 // Unlikely to happen 581 return 0; 582 } 583 584 /* 585 * We compute the sub node size for a CPU node, assuming it 586 * will be the same for all. 587 */ 588 size += strlen(dn->name) + 5; 589 for_each_property_of_node(dn, pp) { 590 size += strlen(pp->name); 591 size += pp->length; 592 } 593 594 of_node_put(dn); 595 return size; 596 } 597 598 static unsigned int kdump_extra_fdt_size_ppc64(struct kimage *image, unsigned int cpu_nodes) 599 { 600 unsigned int extra_size = 0; 601 u64 usm_entries; 602 #ifdef CONFIG_CRASH_HOTPLUG 603 unsigned int possible_cpu_nodes; 604 #endif 605 606 if (!IS_ENABLED(CONFIG_CRASH_DUMP) || image->type != KEXEC_TYPE_CRASH) 607 return 0; 608 609 /* 610 * For kdump kernel, account for linux,usable-memory and 611 * linux,drconf-usable-memory properties. Get an approximate on the 612 * number of usable memory entries and use for FDT size estimation. 613 */ 614 if (drmem_lmb_size()) { 615 usm_entries = ((memory_hotplug_max() / drmem_lmb_size()) + 616 (2 * (resource_size(&crashk_res) / drmem_lmb_size()))); 617 extra_size += (unsigned int)(usm_entries * sizeof(u64)); 618 } 619 620 #ifdef CONFIG_CRASH_HOTPLUG 621 /* 622 * Make sure enough space is reserved to accommodate possible CPU nodes 623 * in the crash FDT. This allows packing possible CPU nodes which are 624 * not yet present in the system without regenerating the entire FDT. 625 */ 626 if (image->type == KEXEC_TYPE_CRASH) { 627 possible_cpu_nodes = num_possible_cpus() / threads_per_core; 628 if (possible_cpu_nodes > cpu_nodes) 629 extra_size += (possible_cpu_nodes - cpu_nodes) * cpu_node_size(); 630 } 631 #endif 632 633 return extra_size; 634 } 635 636 /** 637 * kexec_extra_fdt_size_ppc64 - Return the estimated additional size needed to 638 * setup FDT for kexec/kdump kernel. 639 * @image: kexec image being loaded. 640 * 641 * Returns the estimated extra size needed for kexec/kdump kernel FDT. 642 */ 643 unsigned int kexec_extra_fdt_size_ppc64(struct kimage *image, struct crash_mem *rmem) 644 { 645 struct device_node *dn; 646 unsigned int cpu_nodes = 0, extra_size = 0; 647 648 // Budget some space for the password blob. There's already extra space 649 // for the key name 650 if (plpks_is_available()) 651 extra_size += (unsigned int)plpks_get_passwordlen(); 652 653 /* Get the number of CPU nodes in the current device tree */ 654 for_each_node_by_type(dn, "cpu") { 655 cpu_nodes++; 656 } 657 658 /* Consider extra space for CPU nodes added since the boot time */ 659 if (cpu_nodes > boot_cpu_node_count) 660 extra_size += (cpu_nodes - boot_cpu_node_count) * cpu_node_size(); 661 662 /* Consider extra space for reserved memory ranges if any */ 663 if (rmem && rmem->nr_ranges > 0) 664 extra_size += sizeof(struct fdt_reserve_entry) * rmem->nr_ranges; 665 666 return extra_size + kdump_extra_fdt_size_ppc64(image, cpu_nodes); 667 } 668 669 static int copy_property(void *fdt, int node_offset, const struct device_node *dn, 670 const char *propname) 671 { 672 const void *prop, *fdtprop; 673 int len = 0, fdtlen = 0; 674 675 prop = of_get_property(dn, propname, &len); 676 fdtprop = fdt_getprop(fdt, node_offset, propname, &fdtlen); 677 678 if (fdtprop && !prop) 679 return fdt_delprop(fdt, node_offset, propname); 680 else if (prop) 681 return fdt_setprop(fdt, node_offset, propname, prop, len); 682 else 683 return -FDT_ERR_NOTFOUND; 684 } 685 686 static int update_pci_dma_nodes(void *fdt, const char *dmapropname) 687 { 688 struct device_node *dn; 689 int pci_offset, root_offset, ret = 0; 690 691 if (!firmware_has_feature(FW_FEATURE_LPAR)) 692 return 0; 693 694 root_offset = fdt_path_offset(fdt, "/"); 695 for_each_node_with_property(dn, dmapropname) { 696 pci_offset = fdt_subnode_offset(fdt, root_offset, of_node_full_name(dn)); 697 if (pci_offset < 0) 698 continue; 699 700 ret = copy_property(fdt, pci_offset, dn, "ibm,dma-window"); 701 if (ret < 0) { 702 of_node_put(dn); 703 break; 704 } 705 ret = copy_property(fdt, pci_offset, dn, dmapropname); 706 if (ret < 0) { 707 of_node_put(dn); 708 break; 709 } 710 } 711 712 return ret; 713 } 714 715 /** 716 * setup_new_fdt_ppc64 - Update the flattend device-tree of the kernel 717 * being loaded. 718 * @image: kexec image being loaded. 719 * @fdt: Flattened device tree for the next kernel. 720 * @rmem: Reserved memory ranges. 721 * 722 * Returns 0 on success, negative errno on error. 723 */ 724 int setup_new_fdt_ppc64(const struct kimage *image, void *fdt, struct crash_mem *rmem) 725 { 726 struct crash_mem *umem = NULL; 727 int i, nr_ranges, ret; 728 729 #ifdef CONFIG_CRASH_DUMP 730 /* 731 * Restrict memory usage for kdump kernel by setting up 732 * usable memory ranges and memory reserve map. 733 */ 734 if (image->type == KEXEC_TYPE_CRASH) { 735 ret = get_usable_memory_ranges(&umem); 736 if (ret) 737 goto out; 738 739 ret = update_usable_mem_fdt(fdt, umem); 740 if (ret) { 741 pr_err("Error setting up usable-memory property for kdump kernel\n"); 742 goto out; 743 } 744 745 /* 746 * Ensure we don't touch crashed kernel's memory except the 747 * first 64K of RAM, which will be backed up. 748 */ 749 ret = fdt_add_mem_rsv(fdt, BACKUP_SRC_END + 1, 750 crashk_res.start - BACKUP_SRC_SIZE); 751 if (ret) { 752 pr_err("Error reserving crash memory: %s\n", 753 fdt_strerror(ret)); 754 goto out; 755 } 756 757 /* Ensure backup region is not used by kdump/capture kernel */ 758 ret = fdt_add_mem_rsv(fdt, image->arch.backup_start, 759 BACKUP_SRC_SIZE); 760 if (ret) { 761 pr_err("Error reserving memory for backup: %s\n", 762 fdt_strerror(ret)); 763 goto out; 764 } 765 } 766 #endif 767 768 /* Update cpus nodes information to account hotplug CPUs. */ 769 ret = update_cpus_node(fdt); 770 if (ret < 0) 771 goto out; 772 773 ret = update_pci_dma_nodes(fdt, DIRECT64_PROPNAME); 774 if (ret < 0) 775 goto out; 776 777 ret = update_pci_dma_nodes(fdt, DMA64_PROPNAME); 778 if (ret < 0) 779 goto out; 780 781 /* Update memory reserve map */ 782 nr_ranges = rmem ? rmem->nr_ranges : 0; 783 for (i = 0; i < nr_ranges; i++) { 784 u64 base, size; 785 786 base = rmem->ranges[i].start; 787 size = rmem->ranges[i].end - base + 1; 788 ret = fdt_add_mem_rsv(fdt, base, size); 789 if (ret) { 790 pr_err("Error updating memory reserve map: %s\n", 791 fdt_strerror(ret)); 792 goto out; 793 } 794 } 795 796 // If we have PLPKS active, we need to provide the password to the new kernel 797 if (plpks_is_available()) 798 ret = plpks_populate_fdt(fdt); 799 800 out: 801 kfree(umem); 802 return ret; 803 } 804 805 /** 806 * arch_kexec_kernel_image_probe - Does additional handling needed to setup 807 * kexec segments. 808 * @image: kexec image being loaded. 809 * @buf: Buffer pointing to elf data. 810 * @buf_len: Length of the buffer. 811 * 812 * Returns 0 on success, negative errno on error. 813 */ 814 int arch_kexec_kernel_image_probe(struct kimage *image, void *buf, 815 unsigned long buf_len) 816 { 817 int ret; 818 819 /* Get exclude memory ranges needed for setting up kexec segments */ 820 ret = get_exclude_memory_ranges(&(image->arch.exclude_ranges)); 821 if (ret) { 822 pr_err("Failed to setup exclude memory ranges for buffer lookup\n"); 823 return ret; 824 } 825 826 return kexec_image_probe_default(image, buf, buf_len); 827 } 828 829 /** 830 * arch_kimage_file_post_load_cleanup - Frees up all the allocations done 831 * while loading the image. 832 * @image: kexec image being loaded. 833 * 834 * Returns 0 on success, negative errno on error. 835 */ 836 int arch_kimage_file_post_load_cleanup(struct kimage *image) 837 { 838 kfree(image->arch.exclude_ranges); 839 image->arch.exclude_ranges = NULL; 840 841 vfree(image->arch.backup_buf); 842 image->arch.backup_buf = NULL; 843 844 vfree(image->elf_headers); 845 image->elf_headers = NULL; 846 image->elf_headers_sz = 0; 847 848 kvfree(image->arch.fdt); 849 image->arch.fdt = NULL; 850 851 return kexec_image_post_load_cleanup_default(image); 852 } 853