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_device.h> 21 #include <linux/memblock.h> 22 #include <linux/slab.h> 23 #include <linux/vmalloc.h> 24 #include <asm/setup.h> 25 #include <asm/drmem.h> 26 #include <asm/firmware.h> 27 #include <asm/kexec_ranges.h> 28 #include <asm/crashdump-ppc64.h> 29 #include <asm/mmzone.h> 30 #include <asm/prom.h> 31 #include <asm/plpks.h> 32 33 struct umem_info { 34 u64 *buf; /* data buffer for usable-memory property */ 35 u32 size; /* size allocated for the data buffer */ 36 u32 max_entries; /* maximum no. of entries */ 37 u32 idx; /* index of current entry */ 38 39 /* usable memory ranges to look up */ 40 unsigned int nr_ranges; 41 const struct range *ranges; 42 }; 43 44 const struct kexec_file_ops * const kexec_file_loaders[] = { 45 &kexec_elf64_ops, 46 NULL 47 }; 48 49 /** 50 * get_exclude_memory_ranges - Get exclude memory ranges. This list includes 51 * regions like opal/rtas, tce-table, initrd, 52 * kernel, htab which should be avoided while 53 * setting up kexec load segments. 54 * @mem_ranges: Range list to add the memory ranges to. 55 * 56 * Returns 0 on success, negative errno on error. 57 */ 58 static int get_exclude_memory_ranges(struct crash_mem **mem_ranges) 59 { 60 int ret; 61 62 ret = add_tce_mem_ranges(mem_ranges); 63 if (ret) 64 goto out; 65 66 ret = add_initrd_mem_range(mem_ranges); 67 if (ret) 68 goto out; 69 70 ret = add_htab_mem_range(mem_ranges); 71 if (ret) 72 goto out; 73 74 ret = add_kernel_mem_range(mem_ranges); 75 if (ret) 76 goto out; 77 78 ret = add_rtas_mem_range(mem_ranges); 79 if (ret) 80 goto out; 81 82 ret = add_opal_mem_range(mem_ranges); 83 if (ret) 84 goto out; 85 86 ret = add_reserved_mem_ranges(mem_ranges); 87 if (ret) 88 goto out; 89 90 /* exclude memory ranges should be sorted for easy lookup */ 91 sort_memory_ranges(*mem_ranges, true); 92 out: 93 if (ret) 94 pr_err("Failed to setup exclude memory ranges\n"); 95 return ret; 96 } 97 98 /** 99 * get_usable_memory_ranges - Get usable memory ranges. This list includes 100 * regions like crashkernel, opal/rtas & tce-table, 101 * that kdump kernel could use. 102 * @mem_ranges: Range list to add the memory ranges to. 103 * 104 * Returns 0 on success, negative errno on error. 105 */ 106 static int get_usable_memory_ranges(struct crash_mem **mem_ranges) 107 { 108 int ret; 109 110 /* 111 * Early boot failure observed on guests when low memory (first memory 112 * block?) is not added to usable memory. So, add [0, crashk_res.end] 113 * instead of [crashk_res.start, crashk_res.end] to workaround it. 114 * Also, crashed kernel's memory must be added to reserve map to 115 * avoid kdump kernel from using it. 116 */ 117 ret = add_mem_range(mem_ranges, 0, crashk_res.end + 1); 118 if (ret) 119 goto out; 120 121 ret = add_rtas_mem_range(mem_ranges); 122 if (ret) 123 goto out; 124 125 ret = add_opal_mem_range(mem_ranges); 126 if (ret) 127 goto out; 128 129 ret = add_tce_mem_ranges(mem_ranges); 130 out: 131 if (ret) 132 pr_err("Failed to setup usable memory ranges\n"); 133 return ret; 134 } 135 136 /** 137 * get_crash_memory_ranges - Get crash memory ranges. This list includes 138 * first/crashing kernel's memory regions that 139 * would be exported via an elfcore. 140 * @mem_ranges: Range list to add the memory ranges to. 141 * 142 * Returns 0 on success, negative errno on error. 143 */ 144 static int get_crash_memory_ranges(struct crash_mem **mem_ranges) 145 { 146 phys_addr_t base, end; 147 struct crash_mem *tmem; 148 u64 i; 149 int ret; 150 151 for_each_mem_range(i, &base, &end) { 152 u64 size = end - base; 153 154 /* Skip backup memory region, which needs a separate entry */ 155 if (base == BACKUP_SRC_START) { 156 if (size > BACKUP_SRC_SIZE) { 157 base = BACKUP_SRC_END + 1; 158 size -= BACKUP_SRC_SIZE; 159 } else 160 continue; 161 } 162 163 ret = add_mem_range(mem_ranges, base, size); 164 if (ret) 165 goto out; 166 167 /* Try merging adjacent ranges before reallocation attempt */ 168 if ((*mem_ranges)->nr_ranges == (*mem_ranges)->max_nr_ranges) 169 sort_memory_ranges(*mem_ranges, true); 170 } 171 172 /* Reallocate memory ranges if there is no space to split ranges */ 173 tmem = *mem_ranges; 174 if (tmem && (tmem->nr_ranges == tmem->max_nr_ranges)) { 175 tmem = realloc_mem_ranges(mem_ranges); 176 if (!tmem) 177 goto out; 178 } 179 180 /* Exclude crashkernel region */ 181 ret = crash_exclude_mem_range(tmem, crashk_res.start, crashk_res.end); 182 if (ret) 183 goto out; 184 185 /* 186 * FIXME: For now, stay in parity with kexec-tools but if RTAS/OPAL 187 * regions are exported to save their context at the time of 188 * crash, they should actually be backed up just like the 189 * first 64K bytes of memory. 190 */ 191 ret = add_rtas_mem_range(mem_ranges); 192 if (ret) 193 goto out; 194 195 ret = add_opal_mem_range(mem_ranges); 196 if (ret) 197 goto out; 198 199 /* create a separate program header for the backup region */ 200 ret = add_mem_range(mem_ranges, BACKUP_SRC_START, BACKUP_SRC_SIZE); 201 if (ret) 202 goto out; 203 204 sort_memory_ranges(*mem_ranges, false); 205 out: 206 if (ret) 207 pr_err("Failed to setup crash memory ranges\n"); 208 return ret; 209 } 210 211 /** 212 * get_reserved_memory_ranges - Get reserve memory ranges. This list includes 213 * memory regions that should be added to the 214 * memory reserve map to ensure the region is 215 * protected from any mischief. 216 * @mem_ranges: Range list to add the memory ranges to. 217 * 218 * Returns 0 on success, negative errno on error. 219 */ 220 static int get_reserved_memory_ranges(struct crash_mem **mem_ranges) 221 { 222 int ret; 223 224 ret = add_rtas_mem_range(mem_ranges); 225 if (ret) 226 goto out; 227 228 ret = add_tce_mem_ranges(mem_ranges); 229 if (ret) 230 goto out; 231 232 ret = add_reserved_mem_ranges(mem_ranges); 233 out: 234 if (ret) 235 pr_err("Failed to setup reserved memory ranges\n"); 236 return ret; 237 } 238 239 /** 240 * __locate_mem_hole_top_down - Looks top down for a large enough memory hole 241 * in the memory regions between buf_min & buf_max 242 * for the buffer. If found, sets kbuf->mem. 243 * @kbuf: Buffer contents and memory parameters. 244 * @buf_min: Minimum address for the buffer. 245 * @buf_max: Maximum address for the buffer. 246 * 247 * Returns 0 on success, negative errno on error. 248 */ 249 static int __locate_mem_hole_top_down(struct kexec_buf *kbuf, 250 u64 buf_min, u64 buf_max) 251 { 252 int ret = -EADDRNOTAVAIL; 253 phys_addr_t start, end; 254 u64 i; 255 256 for_each_mem_range_rev(i, &start, &end) { 257 /* 258 * memblock uses [start, end) convention while it is 259 * [start, end] here. Fix the off-by-one to have the 260 * same convention. 261 */ 262 end -= 1; 263 264 if (start > buf_max) 265 continue; 266 267 /* Memory hole not found */ 268 if (end < buf_min) 269 break; 270 271 /* Adjust memory region based on the given range */ 272 if (start < buf_min) 273 start = buf_min; 274 if (end > buf_max) 275 end = buf_max; 276 277 start = ALIGN(start, kbuf->buf_align); 278 if (start < end && (end - start + 1) >= kbuf->memsz) { 279 /* Suitable memory range found. Set kbuf->mem */ 280 kbuf->mem = ALIGN_DOWN(end - kbuf->memsz + 1, 281 kbuf->buf_align); 282 ret = 0; 283 break; 284 } 285 } 286 287 return ret; 288 } 289 290 /** 291 * locate_mem_hole_top_down_ppc64 - Skip special memory regions to find a 292 * suitable buffer with top down approach. 293 * @kbuf: Buffer contents and memory parameters. 294 * @buf_min: Minimum address for the buffer. 295 * @buf_max: Maximum address for the buffer. 296 * @emem: Exclude memory ranges. 297 * 298 * Returns 0 on success, negative errno on error. 299 */ 300 static int locate_mem_hole_top_down_ppc64(struct kexec_buf *kbuf, 301 u64 buf_min, u64 buf_max, 302 const struct crash_mem *emem) 303 { 304 int i, ret = 0, err = -EADDRNOTAVAIL; 305 u64 start, end, tmin, tmax; 306 307 tmax = buf_max; 308 for (i = (emem->nr_ranges - 1); i >= 0; i--) { 309 start = emem->ranges[i].start; 310 end = emem->ranges[i].end; 311 312 if (start > tmax) 313 continue; 314 315 if (end < tmax) { 316 tmin = (end < buf_min ? buf_min : end + 1); 317 ret = __locate_mem_hole_top_down(kbuf, tmin, tmax); 318 if (!ret) 319 return 0; 320 } 321 322 tmax = start - 1; 323 324 if (tmax < buf_min) { 325 ret = err; 326 break; 327 } 328 ret = 0; 329 } 330 331 if (!ret) { 332 tmin = buf_min; 333 ret = __locate_mem_hole_top_down(kbuf, tmin, tmax); 334 } 335 return ret; 336 } 337 338 /** 339 * __locate_mem_hole_bottom_up - Looks bottom up for a large enough memory hole 340 * in the memory regions between buf_min & buf_max 341 * for the buffer. If found, sets kbuf->mem. 342 * @kbuf: Buffer contents and memory parameters. 343 * @buf_min: Minimum address for the buffer. 344 * @buf_max: Maximum address for the buffer. 345 * 346 * Returns 0 on success, negative errno on error. 347 */ 348 static int __locate_mem_hole_bottom_up(struct kexec_buf *kbuf, 349 u64 buf_min, u64 buf_max) 350 { 351 int ret = -EADDRNOTAVAIL; 352 phys_addr_t start, end; 353 u64 i; 354 355 for_each_mem_range(i, &start, &end) { 356 /* 357 * memblock uses [start, end) convention while it is 358 * [start, end] here. Fix the off-by-one to have the 359 * same convention. 360 */ 361 end -= 1; 362 363 if (end < buf_min) 364 continue; 365 366 /* Memory hole not found */ 367 if (start > buf_max) 368 break; 369 370 /* Adjust memory region based on the given range */ 371 if (start < buf_min) 372 start = buf_min; 373 if (end > buf_max) 374 end = buf_max; 375 376 start = ALIGN(start, kbuf->buf_align); 377 if (start < end && (end - start + 1) >= kbuf->memsz) { 378 /* Suitable memory range found. Set kbuf->mem */ 379 kbuf->mem = start; 380 ret = 0; 381 break; 382 } 383 } 384 385 return ret; 386 } 387 388 /** 389 * locate_mem_hole_bottom_up_ppc64 - Skip special memory regions to find a 390 * suitable buffer with bottom up approach. 391 * @kbuf: Buffer contents and memory parameters. 392 * @buf_min: Minimum address for the buffer. 393 * @buf_max: Maximum address for the buffer. 394 * @emem: Exclude memory ranges. 395 * 396 * Returns 0 on success, negative errno on error. 397 */ 398 static int locate_mem_hole_bottom_up_ppc64(struct kexec_buf *kbuf, 399 u64 buf_min, u64 buf_max, 400 const struct crash_mem *emem) 401 { 402 int i, ret = 0, err = -EADDRNOTAVAIL; 403 u64 start, end, tmin, tmax; 404 405 tmin = buf_min; 406 for (i = 0; i < emem->nr_ranges; i++) { 407 start = emem->ranges[i].start; 408 end = emem->ranges[i].end; 409 410 if (end < tmin) 411 continue; 412 413 if (start > tmin) { 414 tmax = (start > buf_max ? buf_max : start - 1); 415 ret = __locate_mem_hole_bottom_up(kbuf, tmin, tmax); 416 if (!ret) 417 return 0; 418 } 419 420 tmin = end + 1; 421 422 if (tmin > buf_max) { 423 ret = err; 424 break; 425 } 426 ret = 0; 427 } 428 429 if (!ret) { 430 tmax = buf_max; 431 ret = __locate_mem_hole_bottom_up(kbuf, tmin, tmax); 432 } 433 return ret; 434 } 435 436 /** 437 * check_realloc_usable_mem - Reallocate buffer if it can't accommodate entries 438 * @um_info: Usable memory buffer and ranges info. 439 * @cnt: No. of entries to accommodate. 440 * 441 * Frees up the old buffer if memory reallocation fails. 442 * 443 * Returns buffer on success, NULL on error. 444 */ 445 static u64 *check_realloc_usable_mem(struct umem_info *um_info, int cnt) 446 { 447 u32 new_size; 448 u64 *tbuf; 449 450 if ((um_info->idx + cnt) <= um_info->max_entries) 451 return um_info->buf; 452 453 new_size = um_info->size + MEM_RANGE_CHUNK_SZ; 454 tbuf = krealloc(um_info->buf, new_size, GFP_KERNEL); 455 if (tbuf) { 456 um_info->buf = tbuf; 457 um_info->size = new_size; 458 um_info->max_entries = (um_info->size / sizeof(u64)); 459 } 460 461 return tbuf; 462 } 463 464 /** 465 * add_usable_mem - Add the usable memory ranges within the given memory range 466 * to the buffer 467 * @um_info: Usable memory buffer and ranges info. 468 * @base: Base address of memory range to look for. 469 * @end: End address of memory range to look for. 470 * 471 * Returns 0 on success, negative errno on error. 472 */ 473 static int add_usable_mem(struct umem_info *um_info, u64 base, u64 end) 474 { 475 u64 loc_base, loc_end; 476 bool add; 477 int i; 478 479 for (i = 0; i < um_info->nr_ranges; i++) { 480 add = false; 481 loc_base = um_info->ranges[i].start; 482 loc_end = um_info->ranges[i].end; 483 if (loc_base >= base && loc_end <= end) 484 add = true; 485 else if (base < loc_end && end > loc_base) { 486 if (loc_base < base) 487 loc_base = base; 488 if (loc_end > end) 489 loc_end = end; 490 add = true; 491 } 492 493 if (add) { 494 if (!check_realloc_usable_mem(um_info, 2)) 495 return -ENOMEM; 496 497 um_info->buf[um_info->idx++] = cpu_to_be64(loc_base); 498 um_info->buf[um_info->idx++] = 499 cpu_to_be64(loc_end - loc_base + 1); 500 } 501 } 502 503 return 0; 504 } 505 506 /** 507 * kdump_setup_usable_lmb - This is a callback function that gets called by 508 * walk_drmem_lmbs for every LMB to set its 509 * usable memory ranges. 510 * @lmb: LMB info. 511 * @usm: linux,drconf-usable-memory property value. 512 * @data: Pointer to usable memory buffer and ranges info. 513 * 514 * Returns 0 on success, negative errno on error. 515 */ 516 static int kdump_setup_usable_lmb(struct drmem_lmb *lmb, const __be32 **usm, 517 void *data) 518 { 519 struct umem_info *um_info; 520 int tmp_idx, ret; 521 u64 base, end; 522 523 /* 524 * kdump load isn't supported on kernels already booted with 525 * linux,drconf-usable-memory property. 526 */ 527 if (*usm) { 528 pr_err("linux,drconf-usable-memory property already exists!"); 529 return -EINVAL; 530 } 531 532 um_info = data; 533 tmp_idx = um_info->idx; 534 if (!check_realloc_usable_mem(um_info, 1)) 535 return -ENOMEM; 536 537 um_info->idx++; 538 base = lmb->base_addr; 539 end = base + drmem_lmb_size() - 1; 540 ret = add_usable_mem(um_info, base, end); 541 if (!ret) { 542 /* 543 * Update the no. of ranges added. Two entries (base & size) 544 * for every range added. 545 */ 546 um_info->buf[tmp_idx] = 547 cpu_to_be64((um_info->idx - tmp_idx - 1) / 2); 548 } 549 550 return ret; 551 } 552 553 #define NODE_PATH_LEN 256 554 /** 555 * add_usable_mem_property - Add usable memory property for the given 556 * memory node. 557 * @fdt: Flattened device tree for the kdump kernel. 558 * @dn: Memory node. 559 * @um_info: Usable memory buffer and ranges info. 560 * 561 * Returns 0 on success, negative errno on error. 562 */ 563 static int add_usable_mem_property(void *fdt, struct device_node *dn, 564 struct umem_info *um_info) 565 { 566 int n_mem_addr_cells, n_mem_size_cells, node; 567 char path[NODE_PATH_LEN]; 568 int i, len, ranges, ret; 569 const __be32 *prop; 570 u64 base, end; 571 572 of_node_get(dn); 573 574 if (snprintf(path, NODE_PATH_LEN, "%pOF", dn) > (NODE_PATH_LEN - 1)) { 575 pr_err("Buffer (%d) too small for memory node: %pOF\n", 576 NODE_PATH_LEN, dn); 577 return -EOVERFLOW; 578 } 579 pr_debug("Memory node path: %s\n", path); 580 581 /* Now that we know the path, find its offset in kdump kernel's fdt */ 582 node = fdt_path_offset(fdt, path); 583 if (node < 0) { 584 pr_err("Malformed device tree: error reading %s\n", path); 585 ret = -EINVAL; 586 goto out; 587 } 588 589 /* Get the address & size cells */ 590 n_mem_addr_cells = of_n_addr_cells(dn); 591 n_mem_size_cells = of_n_size_cells(dn); 592 pr_debug("address cells: %d, size cells: %d\n", n_mem_addr_cells, 593 n_mem_size_cells); 594 595 um_info->idx = 0; 596 if (!check_realloc_usable_mem(um_info, 2)) { 597 ret = -ENOMEM; 598 goto out; 599 } 600 601 prop = of_get_property(dn, "reg", &len); 602 if (!prop || len <= 0) { 603 ret = 0; 604 goto out; 605 } 606 607 /* 608 * "reg" property represents sequence of (addr,size) tuples 609 * each representing a memory range. 610 */ 611 ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells); 612 613 for (i = 0; i < ranges; i++) { 614 base = of_read_number(prop, n_mem_addr_cells); 615 prop += n_mem_addr_cells; 616 end = base + of_read_number(prop, n_mem_size_cells) - 1; 617 prop += n_mem_size_cells; 618 619 ret = add_usable_mem(um_info, base, end); 620 if (ret) 621 goto out; 622 } 623 624 /* 625 * No kdump kernel usable memory found in this memory node. 626 * Write (0,0) tuple in linux,usable-memory property for 627 * this region to be ignored. 628 */ 629 if (um_info->idx == 0) { 630 um_info->buf[0] = 0; 631 um_info->buf[1] = 0; 632 um_info->idx = 2; 633 } 634 635 ret = fdt_setprop(fdt, node, "linux,usable-memory", um_info->buf, 636 (um_info->idx * sizeof(u64))); 637 638 out: 639 of_node_put(dn); 640 return ret; 641 } 642 643 644 /** 645 * update_usable_mem_fdt - Updates kdump kernel's fdt with linux,usable-memory 646 * and linux,drconf-usable-memory DT properties as 647 * appropriate to restrict its memory usage. 648 * @fdt: Flattened device tree for the kdump kernel. 649 * @usable_mem: Usable memory ranges for kdump kernel. 650 * 651 * Returns 0 on success, negative errno on error. 652 */ 653 static int update_usable_mem_fdt(void *fdt, struct crash_mem *usable_mem) 654 { 655 struct umem_info um_info; 656 struct device_node *dn; 657 int node, ret = 0; 658 659 if (!usable_mem) { 660 pr_err("Usable memory ranges for kdump kernel not found\n"); 661 return -ENOENT; 662 } 663 664 node = fdt_path_offset(fdt, "/ibm,dynamic-reconfiguration-memory"); 665 if (node == -FDT_ERR_NOTFOUND) 666 pr_debug("No dynamic reconfiguration memory found\n"); 667 else if (node < 0) { 668 pr_err("Malformed device tree: error reading /ibm,dynamic-reconfiguration-memory.\n"); 669 return -EINVAL; 670 } 671 672 um_info.buf = NULL; 673 um_info.size = 0; 674 um_info.max_entries = 0; 675 um_info.idx = 0; 676 /* Memory ranges to look up */ 677 um_info.ranges = &(usable_mem->ranges[0]); 678 um_info.nr_ranges = usable_mem->nr_ranges; 679 680 dn = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory"); 681 if (dn) { 682 ret = walk_drmem_lmbs(dn, &um_info, kdump_setup_usable_lmb); 683 of_node_put(dn); 684 685 if (ret) { 686 pr_err("Could not setup linux,drconf-usable-memory property for kdump\n"); 687 goto out; 688 } 689 690 ret = fdt_setprop(fdt, node, "linux,drconf-usable-memory", 691 um_info.buf, (um_info.idx * sizeof(u64))); 692 if (ret) { 693 pr_err("Failed to update fdt with linux,drconf-usable-memory property: %s", 694 fdt_strerror(ret)); 695 goto out; 696 } 697 } 698 699 /* 700 * Walk through each memory node and set linux,usable-memory property 701 * for the corresponding node in kdump kernel's fdt. 702 */ 703 for_each_node_by_type(dn, "memory") { 704 ret = add_usable_mem_property(fdt, dn, &um_info); 705 if (ret) { 706 pr_err("Failed to set linux,usable-memory property for %s node", 707 dn->full_name); 708 of_node_put(dn); 709 goto out; 710 } 711 } 712 713 out: 714 kfree(um_info.buf); 715 return ret; 716 } 717 718 /** 719 * load_backup_segment - Locate a memory hole to place the backup region. 720 * @image: Kexec image. 721 * @kbuf: Buffer contents and memory parameters. 722 * 723 * Returns 0 on success, negative errno on error. 724 */ 725 static int load_backup_segment(struct kimage *image, struct kexec_buf *kbuf) 726 { 727 void *buf; 728 int ret; 729 730 /* 731 * Setup a source buffer for backup segment. 732 * 733 * A source buffer has no meaning for backup region as data will 734 * be copied from backup source, after crash, in the purgatory. 735 * But as load segment code doesn't recognize such segments, 736 * setup a dummy source buffer to keep it happy for now. 737 */ 738 buf = vzalloc(BACKUP_SRC_SIZE); 739 if (!buf) 740 return -ENOMEM; 741 742 kbuf->buffer = buf; 743 kbuf->mem = KEXEC_BUF_MEM_UNKNOWN; 744 kbuf->bufsz = kbuf->memsz = BACKUP_SRC_SIZE; 745 kbuf->top_down = false; 746 747 ret = kexec_add_buffer(kbuf); 748 if (ret) { 749 vfree(buf); 750 return ret; 751 } 752 753 image->arch.backup_buf = buf; 754 image->arch.backup_start = kbuf->mem; 755 return 0; 756 } 757 758 /** 759 * update_backup_region_phdr - Update backup region's offset for the core to 760 * export the region appropriately. 761 * @image: Kexec image. 762 * @ehdr: ELF core header. 763 * 764 * Assumes an exclusive program header is setup for the backup region 765 * in the ELF headers 766 * 767 * Returns nothing. 768 */ 769 static void update_backup_region_phdr(struct kimage *image, Elf64_Ehdr *ehdr) 770 { 771 Elf64_Phdr *phdr; 772 unsigned int i; 773 774 phdr = (Elf64_Phdr *)(ehdr + 1); 775 for (i = 0; i < ehdr->e_phnum; i++) { 776 if (phdr->p_paddr == BACKUP_SRC_START) { 777 phdr->p_offset = image->arch.backup_start; 778 pr_debug("Backup region offset updated to 0x%lx\n", 779 image->arch.backup_start); 780 return; 781 } 782 } 783 } 784 785 /** 786 * load_elfcorehdr_segment - Setup crash memory ranges and initialize elfcorehdr 787 * segment needed to load kdump kernel. 788 * @image: Kexec image. 789 * @kbuf: Buffer contents and memory parameters. 790 * 791 * Returns 0 on success, negative errno on error. 792 */ 793 static int load_elfcorehdr_segment(struct kimage *image, struct kexec_buf *kbuf) 794 { 795 struct crash_mem *cmem = NULL; 796 unsigned long headers_sz; 797 void *headers = NULL; 798 int ret; 799 800 ret = get_crash_memory_ranges(&cmem); 801 if (ret) 802 goto out; 803 804 /* Setup elfcorehdr segment */ 805 ret = crash_prepare_elf64_headers(cmem, false, &headers, &headers_sz); 806 if (ret) { 807 pr_err("Failed to prepare elf headers for the core\n"); 808 goto out; 809 } 810 811 /* Fix the offset for backup region in the ELF header */ 812 update_backup_region_phdr(image, headers); 813 814 kbuf->buffer = headers; 815 kbuf->mem = KEXEC_BUF_MEM_UNKNOWN; 816 kbuf->bufsz = kbuf->memsz = headers_sz; 817 kbuf->top_down = false; 818 819 ret = kexec_add_buffer(kbuf); 820 if (ret) { 821 vfree(headers); 822 goto out; 823 } 824 825 image->elf_load_addr = kbuf->mem; 826 image->elf_headers_sz = headers_sz; 827 image->elf_headers = headers; 828 out: 829 kfree(cmem); 830 return ret; 831 } 832 833 /** 834 * load_crashdump_segments_ppc64 - Initialize the additional segements needed 835 * to load kdump kernel. 836 * @image: Kexec image. 837 * @kbuf: Buffer contents and memory parameters. 838 * 839 * Returns 0 on success, negative errno on error. 840 */ 841 int load_crashdump_segments_ppc64(struct kimage *image, 842 struct kexec_buf *kbuf) 843 { 844 int ret; 845 846 /* Load backup segment - first 64K bytes of the crashing kernel */ 847 ret = load_backup_segment(image, kbuf); 848 if (ret) { 849 pr_err("Failed to load backup segment\n"); 850 return ret; 851 } 852 pr_debug("Loaded the backup region at 0x%lx\n", kbuf->mem); 853 854 /* Load elfcorehdr segment - to export crashing kernel's vmcore */ 855 ret = load_elfcorehdr_segment(image, kbuf); 856 if (ret) { 857 pr_err("Failed to load elfcorehdr segment\n"); 858 return ret; 859 } 860 pr_debug("Loaded elf core header at 0x%lx, bufsz=0x%lx memsz=0x%lx\n", 861 image->elf_load_addr, kbuf->bufsz, kbuf->memsz); 862 863 return 0; 864 } 865 866 /** 867 * setup_purgatory_ppc64 - initialize PPC64 specific purgatory's global 868 * variables and call setup_purgatory() to initialize 869 * common global variable. 870 * @image: kexec image. 871 * @slave_code: Slave code for the purgatory. 872 * @fdt: Flattened device tree for the next kernel. 873 * @kernel_load_addr: Address where the kernel is loaded. 874 * @fdt_load_addr: Address where the flattened device tree is loaded. 875 * 876 * Returns 0 on success, negative errno on error. 877 */ 878 int setup_purgatory_ppc64(struct kimage *image, const void *slave_code, 879 const void *fdt, unsigned long kernel_load_addr, 880 unsigned long fdt_load_addr) 881 { 882 struct device_node *dn = NULL; 883 int ret; 884 885 ret = setup_purgatory(image, slave_code, fdt, kernel_load_addr, 886 fdt_load_addr); 887 if (ret) 888 goto out; 889 890 if (image->type == KEXEC_TYPE_CRASH) { 891 u32 my_run_at_load = 1; 892 893 /* 894 * Tell relocatable kernel to run at load address 895 * via the word meant for that at 0x5c. 896 */ 897 ret = kexec_purgatory_get_set_symbol(image, "run_at_load", 898 &my_run_at_load, 899 sizeof(my_run_at_load), 900 false); 901 if (ret) 902 goto out; 903 } 904 905 /* Tell purgatory where to look for backup region */ 906 ret = kexec_purgatory_get_set_symbol(image, "backup_start", 907 &image->arch.backup_start, 908 sizeof(image->arch.backup_start), 909 false); 910 if (ret) 911 goto out; 912 913 /* Setup OPAL base & entry values */ 914 dn = of_find_node_by_path("/ibm,opal"); 915 if (dn) { 916 u64 val; 917 918 of_property_read_u64(dn, "opal-base-address", &val); 919 ret = kexec_purgatory_get_set_symbol(image, "opal_base", &val, 920 sizeof(val), false); 921 if (ret) 922 goto out; 923 924 of_property_read_u64(dn, "opal-entry-address", &val); 925 ret = kexec_purgatory_get_set_symbol(image, "opal_entry", &val, 926 sizeof(val), false); 927 } 928 out: 929 if (ret) 930 pr_err("Failed to setup purgatory symbols"); 931 of_node_put(dn); 932 return ret; 933 } 934 935 /** 936 * get_cpu_node_size - Compute the size of a CPU node in the FDT. 937 * This should be done only once and the value is stored in 938 * a static variable. 939 * Returns the max size of a CPU node in the FDT. 940 */ 941 static unsigned int cpu_node_size(void) 942 { 943 static unsigned int size; 944 struct device_node *dn; 945 struct property *pp; 946 947 /* 948 * Don't compute it twice, we are assuming that the per CPU node size 949 * doesn't change during the system's life. 950 */ 951 if (size) 952 return size; 953 954 dn = of_find_node_by_type(NULL, "cpu"); 955 if (WARN_ON_ONCE(!dn)) { 956 // Unlikely to happen 957 return 0; 958 } 959 960 /* 961 * We compute the sub node size for a CPU node, assuming it 962 * will be the same for all. 963 */ 964 size += strlen(dn->name) + 5; 965 for_each_property_of_node(dn, pp) { 966 size += strlen(pp->name); 967 size += pp->length; 968 } 969 970 of_node_put(dn); 971 return size; 972 } 973 974 /** 975 * kexec_extra_fdt_size_ppc64 - Return the estimated additional size needed to 976 * setup FDT for kexec/kdump kernel. 977 * @image: kexec image being loaded. 978 * 979 * Returns the estimated extra size needed for kexec/kdump kernel FDT. 980 */ 981 unsigned int kexec_extra_fdt_size_ppc64(struct kimage *image) 982 { 983 unsigned int cpu_nodes, extra_size = 0; 984 struct device_node *dn; 985 u64 usm_entries; 986 987 // Budget some space for the password blob. There's already extra space 988 // for the key name 989 if (plpks_is_available()) 990 extra_size += (unsigned int)plpks_get_passwordlen(); 991 992 if (image->type != KEXEC_TYPE_CRASH) 993 return extra_size; 994 995 /* 996 * For kdump kernel, account for linux,usable-memory and 997 * linux,drconf-usable-memory properties. Get an approximate on the 998 * number of usable memory entries and use for FDT size estimation. 999 */ 1000 if (drmem_lmb_size()) { 1001 usm_entries = ((memory_hotplug_max() / drmem_lmb_size()) + 1002 (2 * (resource_size(&crashk_res) / drmem_lmb_size()))); 1003 extra_size += (unsigned int)(usm_entries * sizeof(u64)); 1004 } 1005 1006 /* 1007 * Get the number of CPU nodes in the current DT. This allows to 1008 * reserve places for CPU nodes added since the boot time. 1009 */ 1010 cpu_nodes = 0; 1011 for_each_node_by_type(dn, "cpu") { 1012 cpu_nodes++; 1013 } 1014 1015 if (cpu_nodes > boot_cpu_node_count) 1016 extra_size += (cpu_nodes - boot_cpu_node_count) * cpu_node_size(); 1017 1018 return extra_size; 1019 } 1020 1021 /** 1022 * add_node_props - Reads node properties from device node structure and add 1023 * them to fdt. 1024 * @fdt: Flattened device tree of the kernel 1025 * @node_offset: offset of the node to add a property at 1026 * @dn: device node pointer 1027 * 1028 * Returns 0 on success, negative errno on error. 1029 */ 1030 static int add_node_props(void *fdt, int node_offset, const struct device_node *dn) 1031 { 1032 int ret = 0; 1033 struct property *pp; 1034 1035 if (!dn) 1036 return -EINVAL; 1037 1038 for_each_property_of_node(dn, pp) { 1039 ret = fdt_setprop(fdt, node_offset, pp->name, pp->value, pp->length); 1040 if (ret < 0) { 1041 pr_err("Unable to add %s property: %s\n", pp->name, fdt_strerror(ret)); 1042 return ret; 1043 } 1044 } 1045 return ret; 1046 } 1047 1048 /** 1049 * update_cpus_node - Update cpus node of flattened device tree using of_root 1050 * device node. 1051 * @fdt: Flattened device tree of the kernel. 1052 * 1053 * Returns 0 on success, negative errno on error. 1054 */ 1055 static int update_cpus_node(void *fdt) 1056 { 1057 struct device_node *cpus_node, *dn; 1058 int cpus_offset, cpus_subnode_offset, ret = 0; 1059 1060 cpus_offset = fdt_path_offset(fdt, "/cpus"); 1061 if (cpus_offset < 0 && cpus_offset != -FDT_ERR_NOTFOUND) { 1062 pr_err("Malformed device tree: error reading /cpus node: %s\n", 1063 fdt_strerror(cpus_offset)); 1064 return cpus_offset; 1065 } 1066 1067 if (cpus_offset > 0) { 1068 ret = fdt_del_node(fdt, cpus_offset); 1069 if (ret < 0) { 1070 pr_err("Error deleting /cpus node: %s\n", fdt_strerror(ret)); 1071 return -EINVAL; 1072 } 1073 } 1074 1075 /* Add cpus node to fdt */ 1076 cpus_offset = fdt_add_subnode(fdt, fdt_path_offset(fdt, "/"), "cpus"); 1077 if (cpus_offset < 0) { 1078 pr_err("Error creating /cpus node: %s\n", fdt_strerror(cpus_offset)); 1079 return -EINVAL; 1080 } 1081 1082 /* Add cpus node properties */ 1083 cpus_node = of_find_node_by_path("/cpus"); 1084 ret = add_node_props(fdt, cpus_offset, cpus_node); 1085 of_node_put(cpus_node); 1086 if (ret < 0) 1087 return ret; 1088 1089 /* Loop through all subnodes of cpus and add them to fdt */ 1090 for_each_node_by_type(dn, "cpu") { 1091 cpus_subnode_offset = fdt_add_subnode(fdt, cpus_offset, dn->full_name); 1092 if (cpus_subnode_offset < 0) { 1093 pr_err("Unable to add %s subnode: %s\n", dn->full_name, 1094 fdt_strerror(cpus_subnode_offset)); 1095 ret = cpus_subnode_offset; 1096 goto out; 1097 } 1098 1099 ret = add_node_props(fdt, cpus_subnode_offset, dn); 1100 if (ret < 0) 1101 goto out; 1102 } 1103 out: 1104 of_node_put(dn); 1105 return ret; 1106 } 1107 1108 static int copy_property(void *fdt, int node_offset, const struct device_node *dn, 1109 const char *propname) 1110 { 1111 const void *prop, *fdtprop; 1112 int len = 0, fdtlen = 0; 1113 1114 prop = of_get_property(dn, propname, &len); 1115 fdtprop = fdt_getprop(fdt, node_offset, propname, &fdtlen); 1116 1117 if (fdtprop && !prop) 1118 return fdt_delprop(fdt, node_offset, propname); 1119 else if (prop) 1120 return fdt_setprop(fdt, node_offset, propname, prop, len); 1121 else 1122 return -FDT_ERR_NOTFOUND; 1123 } 1124 1125 static int update_pci_dma_nodes(void *fdt, const char *dmapropname) 1126 { 1127 struct device_node *dn; 1128 int pci_offset, root_offset, ret = 0; 1129 1130 if (!firmware_has_feature(FW_FEATURE_LPAR)) 1131 return 0; 1132 1133 root_offset = fdt_path_offset(fdt, "/"); 1134 for_each_node_with_property(dn, dmapropname) { 1135 pci_offset = fdt_subnode_offset(fdt, root_offset, of_node_full_name(dn)); 1136 if (pci_offset < 0) 1137 continue; 1138 1139 ret = copy_property(fdt, pci_offset, dn, "ibm,dma-window"); 1140 if (ret < 0) 1141 break; 1142 ret = copy_property(fdt, pci_offset, dn, dmapropname); 1143 if (ret < 0) 1144 break; 1145 } 1146 1147 return ret; 1148 } 1149 1150 /** 1151 * setup_new_fdt_ppc64 - Update the flattend device-tree of the kernel 1152 * being loaded. 1153 * @image: kexec image being loaded. 1154 * @fdt: Flattened device tree for the next kernel. 1155 * @initrd_load_addr: Address where the next initrd will be loaded. 1156 * @initrd_len: Size of the next initrd, or 0 if there will be none. 1157 * @cmdline: Command line for the next kernel, or NULL if there will 1158 * be none. 1159 * 1160 * Returns 0 on success, negative errno on error. 1161 */ 1162 int setup_new_fdt_ppc64(const struct kimage *image, void *fdt, 1163 unsigned long initrd_load_addr, 1164 unsigned long initrd_len, const char *cmdline) 1165 { 1166 struct crash_mem *umem = NULL, *rmem = NULL; 1167 int i, nr_ranges, ret; 1168 1169 /* 1170 * Restrict memory usage for kdump kernel by setting up 1171 * usable memory ranges and memory reserve map. 1172 */ 1173 if (image->type == KEXEC_TYPE_CRASH) { 1174 ret = get_usable_memory_ranges(&umem); 1175 if (ret) 1176 goto out; 1177 1178 ret = update_usable_mem_fdt(fdt, umem); 1179 if (ret) { 1180 pr_err("Error setting up usable-memory property for kdump kernel\n"); 1181 goto out; 1182 } 1183 1184 /* 1185 * Ensure we don't touch crashed kernel's memory except the 1186 * first 64K of RAM, which will be backed up. 1187 */ 1188 ret = fdt_add_mem_rsv(fdt, BACKUP_SRC_END + 1, 1189 crashk_res.start - BACKUP_SRC_SIZE); 1190 if (ret) { 1191 pr_err("Error reserving crash memory: %s\n", 1192 fdt_strerror(ret)); 1193 goto out; 1194 } 1195 1196 /* Ensure backup region is not used by kdump/capture kernel */ 1197 ret = fdt_add_mem_rsv(fdt, image->arch.backup_start, 1198 BACKUP_SRC_SIZE); 1199 if (ret) { 1200 pr_err("Error reserving memory for backup: %s\n", 1201 fdt_strerror(ret)); 1202 goto out; 1203 } 1204 } 1205 1206 /* Update cpus nodes information to account hotplug CPUs. */ 1207 ret = update_cpus_node(fdt); 1208 if (ret < 0) 1209 goto out; 1210 1211 #define DIRECT64_PROPNAME "linux,direct64-ddr-window-info" 1212 #define DMA64_PROPNAME "linux,dma64-ddr-window-info" 1213 ret = update_pci_dma_nodes(fdt, DIRECT64_PROPNAME); 1214 if (ret < 0) 1215 goto out; 1216 1217 ret = update_pci_dma_nodes(fdt, DMA64_PROPNAME); 1218 if (ret < 0) 1219 goto out; 1220 #undef DMA64_PROPNAME 1221 #undef DIRECT64_PROPNAME 1222 1223 /* Update memory reserve map */ 1224 ret = get_reserved_memory_ranges(&rmem); 1225 if (ret) 1226 goto out; 1227 1228 nr_ranges = rmem ? rmem->nr_ranges : 0; 1229 for (i = 0; i < nr_ranges; i++) { 1230 u64 base, size; 1231 1232 base = rmem->ranges[i].start; 1233 size = rmem->ranges[i].end - base + 1; 1234 ret = fdt_add_mem_rsv(fdt, base, size); 1235 if (ret) { 1236 pr_err("Error updating memory reserve map: %s\n", 1237 fdt_strerror(ret)); 1238 goto out; 1239 } 1240 } 1241 1242 // If we have PLPKS active, we need to provide the password to the new kernel 1243 if (plpks_is_available()) 1244 ret = plpks_populate_fdt(fdt); 1245 1246 out: 1247 kfree(rmem); 1248 kfree(umem); 1249 return ret; 1250 } 1251 1252 /** 1253 * arch_kexec_locate_mem_hole - Skip special memory regions like rtas, opal, 1254 * tce-table, reserved-ranges & such (exclude 1255 * memory ranges) as they can't be used for kexec 1256 * segment buffer. Sets kbuf->mem when a suitable 1257 * memory hole is found. 1258 * @kbuf: Buffer contents and memory parameters. 1259 * 1260 * Assumes minimum of PAGE_SIZE alignment for kbuf->memsz & kbuf->buf_align. 1261 * 1262 * Returns 0 on success, negative errno on error. 1263 */ 1264 int arch_kexec_locate_mem_hole(struct kexec_buf *kbuf) 1265 { 1266 struct crash_mem **emem; 1267 u64 buf_min, buf_max; 1268 int ret; 1269 1270 /* Look up the exclude ranges list while locating the memory hole */ 1271 emem = &(kbuf->image->arch.exclude_ranges); 1272 if (!(*emem) || ((*emem)->nr_ranges == 0)) { 1273 pr_warn("No exclude range list. Using the default locate mem hole method\n"); 1274 return kexec_locate_mem_hole(kbuf); 1275 } 1276 1277 buf_min = kbuf->buf_min; 1278 buf_max = kbuf->buf_max; 1279 /* Segments for kdump kernel should be within crashkernel region */ 1280 if (kbuf->image->type == KEXEC_TYPE_CRASH) { 1281 buf_min = (buf_min < crashk_res.start ? 1282 crashk_res.start : buf_min); 1283 buf_max = (buf_max > crashk_res.end ? 1284 crashk_res.end : buf_max); 1285 } 1286 1287 if (buf_min > buf_max) { 1288 pr_err("Invalid buffer min and/or max values\n"); 1289 return -EINVAL; 1290 } 1291 1292 if (kbuf->top_down) 1293 ret = locate_mem_hole_top_down_ppc64(kbuf, buf_min, buf_max, 1294 *emem); 1295 else 1296 ret = locate_mem_hole_bottom_up_ppc64(kbuf, buf_min, buf_max, 1297 *emem); 1298 1299 /* Add the buffer allocated to the exclude list for the next lookup */ 1300 if (!ret) { 1301 add_mem_range(emem, kbuf->mem, kbuf->memsz); 1302 sort_memory_ranges(*emem, true); 1303 } else { 1304 pr_err("Failed to locate memory buffer of size %lu\n", 1305 kbuf->memsz); 1306 } 1307 return ret; 1308 } 1309 1310 /** 1311 * arch_kexec_kernel_image_probe - Does additional handling needed to setup 1312 * kexec segments. 1313 * @image: kexec image being loaded. 1314 * @buf: Buffer pointing to elf data. 1315 * @buf_len: Length of the buffer. 1316 * 1317 * Returns 0 on success, negative errno on error. 1318 */ 1319 int arch_kexec_kernel_image_probe(struct kimage *image, void *buf, 1320 unsigned long buf_len) 1321 { 1322 int ret; 1323 1324 /* Get exclude memory ranges needed for setting up kexec segments */ 1325 ret = get_exclude_memory_ranges(&(image->arch.exclude_ranges)); 1326 if (ret) { 1327 pr_err("Failed to setup exclude memory ranges for buffer lookup\n"); 1328 return ret; 1329 } 1330 1331 return kexec_image_probe_default(image, buf, buf_len); 1332 } 1333 1334 /** 1335 * arch_kimage_file_post_load_cleanup - Frees up all the allocations done 1336 * while loading the image. 1337 * @image: kexec image being loaded. 1338 * 1339 * Returns 0 on success, negative errno on error. 1340 */ 1341 int arch_kimage_file_post_load_cleanup(struct kimage *image) 1342 { 1343 kfree(image->arch.exclude_ranges); 1344 image->arch.exclude_ranges = NULL; 1345 1346 vfree(image->arch.backup_buf); 1347 image->arch.backup_buf = NULL; 1348 1349 vfree(image->elf_headers); 1350 image->elf_headers = NULL; 1351 image->elf_headers_sz = 0; 1352 1353 kvfree(image->arch.fdt); 1354 image->arch.fdt = NULL; 1355 1356 return kexec_image_post_load_cleanup_default(image); 1357 } 1358