1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * PPC64 code to handle Linux booting another kernel. 4 * 5 * Copyright (C) 2004-2005, IBM Corp. 6 * 7 * Created by: Milton D Miller II 8 */ 9 10 11 #include <linux/kexec.h> 12 #include <linux/smp.h> 13 #include <linux/thread_info.h> 14 #include <linux/init_task.h> 15 #include <linux/errno.h> 16 #include <linux/kernel.h> 17 #include <linux/cpu.h> 18 #include <linux/hardirq.h> 19 #include <linux/of.h> 20 #include <linux/libfdt.h> 21 22 #include <asm/page.h> 23 #include <asm/current.h> 24 #include <asm/machdep.h> 25 #include <asm/cacheflush.h> 26 #include <asm/firmware.h> 27 #include <asm/paca.h> 28 #include <asm/mmu.h> 29 #include <asm/sections.h> /* _end */ 30 #include <asm/setup.h> 31 #include <asm/smp.h> 32 #include <asm/hw_breakpoint.h> 33 #include <asm/svm.h> 34 #include <asm/ultravisor.h> 35 #include <asm/crashdump-ppc64.h> 36 37 int machine_kexec_prepare(struct kimage *image) 38 { 39 int i; 40 unsigned long begin, end; /* limits of segment */ 41 unsigned long low, high; /* limits of blocked memory range */ 42 struct device_node *node; 43 const unsigned long *basep; 44 const unsigned int *sizep; 45 46 /* 47 * Since we use the kernel fault handlers and paging code to 48 * handle the virtual mode, we must make sure no destination 49 * overlaps kernel static data or bss. 50 */ 51 for (i = 0; i < image->nr_segments; i++) 52 if (image->segment[i].mem < __pa(_end)) 53 return -ETXTBSY; 54 55 /* We also should not overwrite the tce tables */ 56 for_each_node_by_type(node, "pci") { 57 basep = of_get_property(node, "linux,tce-base", NULL); 58 sizep = of_get_property(node, "linux,tce-size", NULL); 59 if (basep == NULL || sizep == NULL) 60 continue; 61 62 low = *basep; 63 high = low + (*sizep); 64 65 for (i = 0; i < image->nr_segments; i++) { 66 begin = image->segment[i].mem; 67 end = begin + image->segment[i].memsz; 68 69 if ((begin < high) && (end > low)) { 70 of_node_put(node); 71 return -ETXTBSY; 72 } 73 } 74 } 75 76 return 0; 77 } 78 79 /* Called during kexec sequence with MMU off */ 80 static notrace void copy_segments(unsigned long ind) 81 { 82 unsigned long entry; 83 unsigned long *ptr; 84 void *dest; 85 void *addr; 86 87 /* 88 * We rely on kexec_load to create a lists that properly 89 * initializes these pointers before they are used. 90 * We will still crash if the list is wrong, but at least 91 * the compiler will be quiet. 92 */ 93 ptr = NULL; 94 dest = NULL; 95 96 for (entry = ind; !(entry & IND_DONE); entry = *ptr++) { 97 addr = __va(entry & PAGE_MASK); 98 99 switch (entry & IND_FLAGS) { 100 case IND_DESTINATION: 101 dest = addr; 102 break; 103 case IND_INDIRECTION: 104 ptr = addr; 105 break; 106 case IND_SOURCE: 107 copy_page(dest, addr); 108 dest += PAGE_SIZE; 109 } 110 } 111 } 112 113 /* Called during kexec sequence with MMU off */ 114 notrace void kexec_copy_flush(struct kimage *image) 115 { 116 long i, nr_segments = image->nr_segments; 117 struct kexec_segment ranges[KEXEC_SEGMENT_MAX]; 118 119 /* save the ranges on the stack to efficiently flush the icache */ 120 memcpy(ranges, image->segment, sizeof(ranges)); 121 122 /* 123 * After this call we may not use anything allocated in dynamic 124 * memory, including *image. 125 * 126 * Only globals and the stack are allowed. 127 */ 128 copy_segments(image->head); 129 130 /* 131 * we need to clear the icache for all dest pages sometime, 132 * including ones that were in place on the original copy 133 */ 134 for (i = 0; i < nr_segments; i++) 135 flush_icache_range((unsigned long)__va(ranges[i].mem), 136 (unsigned long)__va(ranges[i].mem + ranges[i].memsz)); 137 } 138 139 #ifdef CONFIG_SMP 140 141 static int kexec_all_irq_disabled = 0; 142 143 static void kexec_smp_down(void *arg) 144 { 145 local_irq_disable(); 146 hard_irq_disable(); 147 148 mb(); /* make sure our irqs are disabled before we say they are */ 149 get_paca()->kexec_state = KEXEC_STATE_IRQS_OFF; 150 while(kexec_all_irq_disabled == 0) 151 cpu_relax(); 152 mb(); /* make sure all irqs are disabled before this */ 153 hw_breakpoint_disable(); 154 /* 155 * Now every CPU has IRQs off, we can clear out any pending 156 * IPIs and be sure that no more will come in after this. 157 */ 158 if (ppc_md.kexec_cpu_down) 159 ppc_md.kexec_cpu_down(0, 1); 160 161 reset_sprs(); 162 163 kexec_smp_wait(); 164 /* NOTREACHED */ 165 } 166 167 static void kexec_prepare_cpus_wait(int wait_state) 168 { 169 int my_cpu, i, notified=-1; 170 171 hw_breakpoint_disable(); 172 my_cpu = raw_smp_processor_id(); 173 /* Make sure each CPU has at least made it to the state we need. 174 * 175 * FIXME: There is a (slim) chance of a problem if not all of the CPUs 176 * are correctly onlined. If somehow we start a CPU on boot with RTAS 177 * start-cpu, but somehow that CPU doesn't write callin_cpu_map[] in 178 * time, the boot CPU will timeout. If it does eventually execute 179 * stuff, the secondary will start up (paca_ptrs[]->cpu_start was 180 * written) and get into a peculiar state. 181 * If the platform supports smp_ops->take_timebase(), the secondary CPU 182 * will probably be spinning in there. If not (i.e. pseries), the 183 * secondary will continue on and try to online itself/idle/etc. If it 184 * survives that, we need to find these 185 * possible-but-not-online-but-should-be CPUs and chaperone them into 186 * kexec_smp_wait(). 187 */ 188 for_each_online_cpu(i) { 189 if (i == my_cpu) 190 continue; 191 192 while (paca_ptrs[i]->kexec_state < wait_state) { 193 barrier(); 194 if (i != notified) { 195 printk(KERN_INFO "kexec: waiting for cpu %d " 196 "(physical %d) to enter %i state\n", 197 i, paca_ptrs[i]->hw_cpu_id, wait_state); 198 notified = i; 199 } 200 } 201 } 202 mb(); 203 } 204 205 206 /* 207 * The add_cpu() call in wake_offline_cpus() can fail as cpu_bootable() 208 * returns false for CPUs that fail the cpu_smt_thread_allowed() check 209 * or non primary threads if SMT is disabled. Re-enable SMT and set the 210 * number of SMT threads to threads per core. 211 */ 212 static void kexec_smt_reenable(void) 213 { 214 #if defined(CONFIG_SMP) && defined(CONFIG_HOTPLUG_SMT) 215 lock_device_hotplug(); 216 cpu_smt_num_threads = threads_per_core; 217 cpu_smt_control = CPU_SMT_ENABLED; 218 unlock_device_hotplug(); 219 #endif 220 } 221 222 /* 223 * We need to make sure each present CPU is online. The next kernel will scan 224 * the device tree and assume primary threads are online and query secondary 225 * threads via RTAS to online them if required. If we don't online primary 226 * threads, they will be stuck. However, we also online secondary threads as we 227 * may be using 'cede offline'. In this case RTAS doesn't see the secondary 228 * threads as offline -- and again, these CPUs will be stuck. 229 * 230 * So, we online all CPUs that should be running, including secondary threads. 231 */ 232 static void wake_offline_cpus(void) 233 { 234 int cpu = 0; 235 236 kexec_smt_reenable(); 237 238 for_each_present_cpu(cpu) { 239 if (!cpu_online(cpu)) { 240 printk(KERN_INFO "kexec: Waking offline cpu %d.\n", 241 cpu); 242 WARN_ON(add_cpu(cpu)); 243 } 244 } 245 } 246 247 static void kexec_prepare_cpus(void) 248 { 249 wake_offline_cpus(); 250 smp_call_function(kexec_smp_down, NULL, /* wait */0); 251 local_irq_disable(); 252 hard_irq_disable(); 253 254 mb(); /* make sure IRQs are disabled before we say they are */ 255 get_paca()->kexec_state = KEXEC_STATE_IRQS_OFF; 256 257 kexec_prepare_cpus_wait(KEXEC_STATE_IRQS_OFF); 258 /* we are sure every CPU has IRQs off at this point */ 259 kexec_all_irq_disabled = 1; 260 261 /* 262 * Before removing MMU mappings make sure all CPUs have entered real 263 * mode: 264 */ 265 kexec_prepare_cpus_wait(KEXEC_STATE_REAL_MODE); 266 267 /* after we tell the others to go down */ 268 if (ppc_md.kexec_cpu_down) 269 ppc_md.kexec_cpu_down(0, 0); 270 } 271 272 #else /* ! SMP */ 273 274 static void kexec_prepare_cpus(void) 275 { 276 /* 277 * move the secondarys to us so that we can copy 278 * the new kernel 0-0x100 safely 279 * 280 * do this if kexec in setup.c ? 281 * 282 * We need to release the cpus if we are ever going from an 283 * UP to an SMP kernel. 284 */ 285 smp_release_cpus(); 286 if (ppc_md.kexec_cpu_down) 287 ppc_md.kexec_cpu_down(0, 0); 288 local_irq_disable(); 289 hard_irq_disable(); 290 } 291 292 #endif /* SMP */ 293 294 /* 295 * kexec thread structure and stack. 296 * 297 * We need to make sure that this is 16384-byte aligned due to the 298 * way process stacks are handled. It also must be statically allocated 299 * or allocated as part of the kimage, because everything else may be 300 * overwritten when we copy the kexec image. We piggyback on the 301 * "init_task" linker section here to statically allocate a stack. 302 * 303 * We could use a smaller stack if we don't care about anything using 304 * current, but that audit has not been performed. 305 */ 306 static union thread_union kexec_stack = { }; 307 308 /* 309 * For similar reasons to the stack above, the kexecing CPU needs to be on a 310 * static PACA; we switch to kexec_paca. 311 */ 312 static struct paca_struct kexec_paca; 313 314 /* Our assembly helper, in misc_64.S */ 315 extern void kexec_sequence(void *newstack, unsigned long start, 316 void *image, void *control, 317 void (*clear_all)(void), 318 bool copy_with_mmu_off) __noreturn; 319 320 /* too late to fail here */ 321 void default_machine_kexec(struct kimage *image) 322 { 323 bool copy_with_mmu_off; 324 325 /* prepare control code if any */ 326 327 /* 328 * If the kexec boot is the normal one, need to shutdown other cpus 329 * into our wait loop and quiesce interrupts. 330 * Otherwise, in the case of crashed mode (crashing_cpu >= 0), 331 * stopping other CPUs and collecting their pt_regs is done before 332 * using debugger IPI. 333 */ 334 335 if (!kdump_in_progress()) 336 kexec_prepare_cpus(); 337 338 #ifdef CONFIG_PPC_PSERIES 339 /* 340 * This must be done after other CPUs have shut down, otherwise they 341 * could execute the 'scv' instruction, which is not supported with 342 * reloc disabled (see configure_exceptions()). 343 */ 344 if (firmware_has_feature(FW_FEATURE_SET_MODE)) 345 pseries_disable_reloc_on_exc(); 346 #endif 347 348 printk("kexec: Starting switchover sequence.\n"); 349 350 /* switch to a staticly allocated stack. Based on irq stack code. 351 * We setup preempt_count to avoid using VMX in memcpy. 352 * XXX: the task struct will likely be invalid once we do the copy! 353 */ 354 current_thread_info()->flags = 0; 355 current_thread_info()->preempt_count = HARDIRQ_OFFSET; 356 357 /* We need a static PACA, too; copy this CPU's PACA over and switch to 358 * it. Also poison per_cpu_offset and NULL lppaca to catch anyone using 359 * non-static data. 360 */ 361 memcpy(&kexec_paca, get_paca(), sizeof(struct paca_struct)); 362 kexec_paca.data_offset = 0xedeaddeadeeeeeeeUL; 363 #ifdef CONFIG_PPC_PSERIES 364 kexec_paca.lppaca_ptr = NULL; 365 #endif 366 367 if (is_secure_guest() && !(image->preserve_context || 368 image->type == KEXEC_TYPE_CRASH)) { 369 uv_unshare_all_pages(); 370 printk("kexec: Unshared all shared pages.\n"); 371 } 372 373 paca_ptrs[kexec_paca.paca_index] = &kexec_paca; 374 375 setup_paca(&kexec_paca); 376 377 /* 378 * The lppaca should be unregistered at this point so the HV won't 379 * touch it. In the case of a crash, none of the lppacas are 380 * unregistered so there is not much we can do about it here. 381 */ 382 383 /* 384 * On Book3S, the copy must happen with the MMU off if we are either 385 * using Radix page tables or we are not in an LPAR since we can 386 * overwrite the page tables while copying. 387 * 388 * In an LPAR, we keep the MMU on otherwise we can't access beyond 389 * the RMA. On BookE there is no real MMU off mode, so we have to 390 * keep it enabled as well (but then we have bolted TLB entries). 391 */ 392 #ifdef CONFIG_PPC_BOOK3E_64 393 copy_with_mmu_off = false; 394 #else 395 copy_with_mmu_off = radix_enabled() || 396 !(firmware_has_feature(FW_FEATURE_LPAR) || 397 firmware_has_feature(FW_FEATURE_PS3_LV1)); 398 #endif 399 400 /* Some things are best done in assembly. Finding globals with 401 * a toc is easier in C, so pass in what we can. 402 */ 403 kexec_sequence(&kexec_stack, image->start, image, 404 page_address(image->control_code_page), 405 mmu_cleanup_all, copy_with_mmu_off); 406 /* NOTREACHED */ 407 } 408 409 #ifdef CONFIG_PPC_64S_HASH_MMU 410 /* Values we need to export to the second kernel via the device tree. */ 411 static __be64 htab_base; 412 static __be64 htab_size; 413 414 static struct property htab_base_prop = { 415 .name = "linux,htab-base", 416 .length = sizeof(unsigned long), 417 .value = &htab_base, 418 }; 419 420 static struct property htab_size_prop = { 421 .name = "linux,htab-size", 422 .length = sizeof(unsigned long), 423 .value = &htab_size, 424 }; 425 426 static int __init export_htab_values(void) 427 { 428 struct device_node *node; 429 430 /* On machines with no htab htab_address is NULL */ 431 if (!htab_address) 432 return -ENODEV; 433 434 node = of_find_node_by_path("/chosen"); 435 if (!node) 436 return -ENODEV; 437 438 /* remove any stale properties so ours can be found */ 439 of_remove_property(node, of_find_property(node, htab_base_prop.name, NULL)); 440 of_remove_property(node, of_find_property(node, htab_size_prop.name, NULL)); 441 442 htab_base = cpu_to_be64(__pa(htab_address)); 443 of_add_property(node, &htab_base_prop); 444 htab_size = cpu_to_be64(htab_size_bytes); 445 of_add_property(node, &htab_size_prop); 446 447 of_node_put(node); 448 return 0; 449 } 450 late_initcall(export_htab_values); 451 #endif /* CONFIG_PPC_64S_HASH_MMU */ 452 453 #if defined(CONFIG_KEXEC_FILE) || defined(CONFIG_CRASH_DUMP) 454 /** 455 * add_node_props - Reads node properties from device node structure and add 456 * them to fdt. 457 * @fdt: Flattened device tree of the kernel 458 * @node_offset: offset of the node to add a property at 459 * @dn: device node pointer 460 * 461 * Returns 0 on success, negative errno on error. 462 */ 463 static int add_node_props(void *fdt, int node_offset, const struct device_node *dn) 464 { 465 int ret = 0; 466 struct property *pp; 467 468 if (!dn) 469 return -EINVAL; 470 471 for_each_property_of_node(dn, pp) { 472 ret = fdt_setprop(fdt, node_offset, pp->name, pp->value, pp->length); 473 if (ret < 0) { 474 pr_err("Unable to add %s property: %s\n", pp->name, fdt_strerror(ret)); 475 return ret; 476 } 477 } 478 return ret; 479 } 480 481 /** 482 * update_cpus_node - Update cpus node of flattened device tree using of_root 483 * device node. 484 * @fdt: Flattened device tree of the kernel. 485 * 486 * Returns 0 on success, negative errno on error. 487 * 488 * Note: expecting no subnodes under /cpus/<node> with device_type == "cpu". 489 * If this changes, update this function to include them. 490 */ 491 int update_cpus_node(void *fdt) 492 { 493 int prev_node_offset; 494 const char *device_type; 495 const struct fdt_property *prop; 496 struct device_node *cpus_node, *dn; 497 int cpus_offset, cpus_subnode_offset, ret = 0; 498 499 cpus_offset = fdt_path_offset(fdt, "/cpus"); 500 if (cpus_offset < 0 && cpus_offset != -FDT_ERR_NOTFOUND) { 501 pr_err("Malformed device tree: error reading /cpus node: %s\n", 502 fdt_strerror(cpus_offset)); 503 return cpus_offset; 504 } 505 506 prev_node_offset = cpus_offset; 507 /* Delete sub-nodes of /cpus node with device_type == "cpu" */ 508 for (cpus_subnode_offset = fdt_first_subnode(fdt, cpus_offset); cpus_subnode_offset >= 0;) { 509 /* Ignore nodes that do not have a device_type property or device_type != "cpu" */ 510 prop = fdt_get_property(fdt, cpus_subnode_offset, "device_type", NULL); 511 if (!prop || strcmp(prop->data, "cpu")) { 512 prev_node_offset = cpus_subnode_offset; 513 goto next_node; 514 } 515 516 ret = fdt_del_node(fdt, cpus_subnode_offset); 517 if (ret < 0) { 518 pr_err("Failed to delete a cpus sub-node: %s\n", fdt_strerror(ret)); 519 return ret; 520 } 521 next_node: 522 if (prev_node_offset == cpus_offset) 523 cpus_subnode_offset = fdt_first_subnode(fdt, cpus_offset); 524 else 525 cpus_subnode_offset = fdt_next_subnode(fdt, prev_node_offset); 526 } 527 528 cpus_node = of_find_node_by_path("/cpus"); 529 /* Fail here to avoid kexec/kdump kernel boot hung */ 530 if (!cpus_node) { 531 pr_err("No /cpus node found\n"); 532 return -EINVAL; 533 } 534 535 /* Add all /cpus sub-nodes of device_type == "cpu" to FDT */ 536 for_each_child_of_node(cpus_node, dn) { 537 /* Ignore device nodes that do not have a device_type property 538 * or device_type != "cpu". 539 */ 540 device_type = of_get_property(dn, "device_type", NULL); 541 if (!device_type || strcmp(device_type, "cpu")) 542 continue; 543 544 cpus_subnode_offset = fdt_add_subnode(fdt, cpus_offset, dn->full_name); 545 if (cpus_subnode_offset < 0) { 546 pr_err("Unable to add %s subnode: %s\n", dn->full_name, 547 fdt_strerror(cpus_subnode_offset)); 548 ret = cpus_subnode_offset; 549 goto out; 550 } 551 552 ret = add_node_props(fdt, cpus_subnode_offset, dn); 553 if (ret < 0) 554 goto out; 555 } 556 out: 557 of_node_put(cpus_node); 558 of_node_put(dn); 559 return ret; 560 } 561 #endif /* CONFIG_KEXEC_FILE || CONFIG_CRASH_DUMP */ 562