1 // SPDX-License-Identifier: GPL-2.0-only
3 * PPC64 code to handle Linux booting another kernel.
5 * Copyright (C) 2004-2005, IBM Corp.
7 * Created by: Milton D Miller II
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>
20 #include <linux/libfdt.h>
23 #include <asm/current.h>
24 #include <asm/machdep.h>
25 #include <asm/cacheflush.h>
26 #include <asm/firmware.h>
29 #include <asm/sections.h> /* _end */
30 #include <asm/setup.h>
32 #include <asm/hw_breakpoint.h>
34 #include <asm/ultravisor.h>
35 #include <asm/crashdump-ppc64.h>
37 int machine_kexec_prepare(struct kimage *image)
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;
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.
51 for (i = 0; i < image->nr_segments; i++)
52 if (image->segment[i].mem < __pa(_end))
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)
63 high = low + (*sizep);
65 for (i = 0; i < image->nr_segments; i++) {
66 begin = image->segment[i].mem;
67 end = begin + image->segment[i].memsz;
69 if ((begin < high) && (end > low)) {
79 /* Called during kexec sequence with MMU off */
80 static notrace void copy_segments(unsigned long ind)
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.
96 for (entry = ind; !(entry & IND_DONE); entry = *ptr++) {
97 addr = __va(entry & PAGE_MASK);
99 switch (entry & IND_FLAGS) {
100 case IND_DESTINATION:
103 case IND_INDIRECTION:
107 copy_page(dest, addr);
113 /* Called during kexec sequence with MMU off */
114 notrace void kexec_copy_flush(struct kimage *image)
116 long i, nr_segments = image->nr_segments;
117 struct kexec_segment ranges[KEXEC_SEGMENT_MAX];
119 /* save the ranges on the stack to efficiently flush the icache */
120 memcpy(ranges, image->segment, sizeof(ranges));
123 * After this call we may not use anything allocated in dynamic
124 * memory, including *image.
126 * Only globals and the stack are allowed.
128 copy_segments(image->head);
131 * we need to clear the icache for all dest pages sometime,
132 * including ones that were in place on the original copy
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));
141 static int kexec_all_irq_disabled = 0;
143 static void kexec_smp_down(void *arg)
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)
152 mb(); /* make sure all irqs are disabled before this */
153 hw_breakpoint_disable();
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.
158 if (ppc_md.kexec_cpu_down)
159 ppc_md.kexec_cpu_down(0, 1);
167 static void kexec_prepare_cpus_wait(int wait_state)
169 int my_cpu, i, notified=-1;
171 hw_breakpoint_disable();
173 /* Make sure each CPU has at least made it to the state we need.
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
188 for_each_online_cpu(i) {
192 while (paca_ptrs[i]->kexec_state < wait_state) {
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);
206 * We need to make sure each present CPU is online. The next kernel will scan
207 * the device tree and assume primary threads are online and query secondary
208 * threads via RTAS to online them if required. If we don't online primary
209 * threads, they will be stuck. However, we also online secondary threads as we
210 * may be using 'cede offline'. In this case RTAS doesn't see the secondary
211 * threads as offline -- and again, these CPUs will be stuck.
213 * So, we online all CPUs that should be running, including secondary threads.
215 static void wake_offline_cpus(void)
219 for_each_present_cpu(cpu) {
220 if (!cpu_online(cpu)) {
221 printk(KERN_INFO "kexec: Waking offline cpu %d.\n",
223 WARN_ON(add_cpu(cpu));
228 static void kexec_prepare_cpus(void)
231 smp_call_function(kexec_smp_down, NULL, /* wait */0);
235 mb(); /* make sure IRQs are disabled before we say they are */
236 get_paca()->kexec_state = KEXEC_STATE_IRQS_OFF;
238 kexec_prepare_cpus_wait(KEXEC_STATE_IRQS_OFF);
239 /* we are sure every CPU has IRQs off at this point */
240 kexec_all_irq_disabled = 1;
243 * Before removing MMU mappings make sure all CPUs have entered real
246 kexec_prepare_cpus_wait(KEXEC_STATE_REAL_MODE);
248 /* after we tell the others to go down */
249 if (ppc_md.kexec_cpu_down)
250 ppc_md.kexec_cpu_down(0, 0);
257 static void kexec_prepare_cpus(void)
260 * move the secondarys to us so that we can copy
261 * the new kernel 0-0x100 safely
263 * do this if kexec in setup.c ?
265 * We need to release the cpus if we are ever going from an
266 * UP to an SMP kernel.
269 if (ppc_md.kexec_cpu_down)
270 ppc_md.kexec_cpu_down(0, 0);
278 * kexec thread structure and stack.
280 * We need to make sure that this is 16384-byte aligned due to the
281 * way process stacks are handled. It also must be statically allocated
282 * or allocated as part of the kimage, because everything else may be
283 * overwritten when we copy the kexec image. We piggyback on the
284 * "init_task" linker section here to statically allocate a stack.
286 * We could use a smaller stack if we don't care about anything using
287 * current, but that audit has not been performed.
289 static union thread_union kexec_stack = { };
292 * For similar reasons to the stack above, the kexecing CPU needs to be on a
293 * static PACA; we switch to kexec_paca.
295 static struct paca_struct kexec_paca;
297 /* Our assembly helper, in misc_64.S */
298 extern void kexec_sequence(void *newstack, unsigned long start,
299 void *image, void *control,
300 void (*clear_all)(void),
301 bool copy_with_mmu_off) __noreturn;
303 /* too late to fail here */
304 void default_machine_kexec(struct kimage *image)
306 bool copy_with_mmu_off;
308 /* prepare control code if any */
311 * If the kexec boot is the normal one, need to shutdown other cpus
312 * into our wait loop and quiesce interrupts.
313 * Otherwise, in the case of crashed mode (crashing_cpu >= 0),
314 * stopping other CPUs and collecting their pt_regs is done before
315 * using debugger IPI.
318 if (!kdump_in_progress())
319 kexec_prepare_cpus();
321 #ifdef CONFIG_PPC_PSERIES
323 * This must be done after other CPUs have shut down, otherwise they
324 * could execute the 'scv' instruction, which is not supported with
325 * reloc disabled (see configure_exceptions()).
327 if (firmware_has_feature(FW_FEATURE_SET_MODE))
328 pseries_disable_reloc_on_exc();
331 printk("kexec: Starting switchover sequence.\n");
333 /* switch to a staticly allocated stack. Based on irq stack code.
334 * We setup preempt_count to avoid using VMX in memcpy.
335 * XXX: the task struct will likely be invalid once we do the copy!
337 current_thread_info()->flags = 0;
338 current_thread_info()->preempt_count = HARDIRQ_OFFSET;
340 /* We need a static PACA, too; copy this CPU's PACA over and switch to
341 * it. Also poison per_cpu_offset and NULL lppaca to catch anyone using
344 memcpy(&kexec_paca, get_paca(), sizeof(struct paca_struct));
345 kexec_paca.data_offset = 0xedeaddeadeeeeeeeUL;
346 #ifdef CONFIG_PPC_PSERIES
347 kexec_paca.lppaca_ptr = NULL;
350 if (is_secure_guest() && !(image->preserve_context ||
351 image->type == KEXEC_TYPE_CRASH)) {
352 uv_unshare_all_pages();
353 printk("kexec: Unshared all shared pages.\n");
356 paca_ptrs[kexec_paca.paca_index] = &kexec_paca;
358 setup_paca(&kexec_paca);
361 * The lppaca should be unregistered at this point so the HV won't
362 * touch it. In the case of a crash, none of the lppacas are
363 * unregistered so there is not much we can do about it here.
367 * On Book3S, the copy must happen with the MMU off if we are either
368 * using Radix page tables or we are not in an LPAR since we can
369 * overwrite the page tables while copying.
371 * In an LPAR, we keep the MMU on otherwise we can't access beyond
372 * the RMA. On BookE there is no real MMU off mode, so we have to
373 * keep it enabled as well (but then we have bolted TLB entries).
375 #ifdef CONFIG_PPC_BOOK3E_64
376 copy_with_mmu_off = false;
378 copy_with_mmu_off = radix_enabled() ||
379 !(firmware_has_feature(FW_FEATURE_LPAR) ||
380 firmware_has_feature(FW_FEATURE_PS3_LV1));
383 /* Some things are best done in assembly. Finding globals with
384 * a toc is easier in C, so pass in what we can.
386 kexec_sequence(&kexec_stack, image->start, image,
387 page_address(image->control_code_page),
388 mmu_cleanup_all, copy_with_mmu_off);
392 #ifdef CONFIG_PPC_64S_HASH_MMU
393 /* Values we need to export to the second kernel via the device tree. */
394 static __be64 htab_base;
395 static __be64 htab_size;
397 static struct property htab_base_prop = {
398 .name = "linux,htab-base",
399 .length = sizeof(unsigned long),
403 static struct property htab_size_prop = {
404 .name = "linux,htab-size",
405 .length = sizeof(unsigned long),
409 static int __init export_htab_values(void)
411 struct device_node *node;
413 /* On machines with no htab htab_address is NULL */
417 node = of_find_node_by_path("/chosen");
421 /* remove any stale properties so ours can be found */
422 of_remove_property(node, of_find_property(node, htab_base_prop.name, NULL));
423 of_remove_property(node, of_find_property(node, htab_size_prop.name, NULL));
425 htab_base = cpu_to_be64(__pa(htab_address));
426 of_add_property(node, &htab_base_prop);
427 htab_size = cpu_to_be64(htab_size_bytes);
428 of_add_property(node, &htab_size_prop);
433 late_initcall(export_htab_values);
434 #endif /* CONFIG_PPC_64S_HASH_MMU */
436 #if defined(CONFIG_KEXEC_FILE) || defined(CONFIG_CRASH_DUMP)
438 * add_node_props - Reads node properties from device node structure and add
440 * @fdt: Flattened device tree of the kernel
441 * @node_offset: offset of the node to add a property at
442 * @dn: device node pointer
444 * Returns 0 on success, negative errno on error.
446 static int add_node_props(void *fdt, int node_offset, const struct device_node *dn)
454 for_each_property_of_node(dn, pp) {
455 ret = fdt_setprop(fdt, node_offset, pp->name, pp->value, pp->length);
457 pr_err("Unable to add %s property: %s\n", pp->name, fdt_strerror(ret));
465 * update_cpus_node - Update cpus node of flattened device tree using of_root
467 * @fdt: Flattened device tree of the kernel.
469 * Returns 0 on success, negative errno on error.
471 * Note: expecting no subnodes under /cpus/<node> with device_type == "cpu".
472 * If this changes, update this function to include them.
474 int update_cpus_node(void *fdt)
476 int prev_node_offset;
477 const char *device_type;
478 const struct fdt_property *prop;
479 struct device_node *cpus_node, *dn;
480 int cpus_offset, cpus_subnode_offset, ret = 0;
482 cpus_offset = fdt_path_offset(fdt, "/cpus");
483 if (cpus_offset < 0 && cpus_offset != -FDT_ERR_NOTFOUND) {
484 pr_err("Malformed device tree: error reading /cpus node: %s\n",
485 fdt_strerror(cpus_offset));
489 prev_node_offset = cpus_offset;
490 /* Delete sub-nodes of /cpus node with device_type == "cpu" */
491 for (cpus_subnode_offset = fdt_first_subnode(fdt, cpus_offset); cpus_subnode_offset >= 0;) {
492 /* Ignore nodes that do not have a device_type property or device_type != "cpu" */
493 prop = fdt_get_property(fdt, cpus_subnode_offset, "device_type", NULL);
494 if (!prop || strcmp(prop->data, "cpu")) {
495 prev_node_offset = cpus_subnode_offset;
499 ret = fdt_del_node(fdt, cpus_subnode_offset);
501 pr_err("Failed to delete a cpus sub-node: %s\n", fdt_strerror(ret));
505 if (prev_node_offset == cpus_offset)
506 cpus_subnode_offset = fdt_first_subnode(fdt, cpus_offset);
508 cpus_subnode_offset = fdt_next_subnode(fdt, prev_node_offset);
511 cpus_node = of_find_node_by_path("/cpus");
512 /* Fail here to avoid kexec/kdump kernel boot hung */
514 pr_err("No /cpus node found\n");
518 /* Add all /cpus sub-nodes of device_type == "cpu" to FDT */
519 for_each_child_of_node(cpus_node, dn) {
520 /* Ignore device nodes that do not have a device_type property
521 * or device_type != "cpu".
523 device_type = of_get_property(dn, "device_type", NULL);
524 if (!device_type || strcmp(device_type, "cpu"))
527 cpus_subnode_offset = fdt_add_subnode(fdt, cpus_offset, dn->full_name);
528 if (cpus_subnode_offset < 0) {
529 pr_err("Unable to add %s subnode: %s\n", dn->full_name,
530 fdt_strerror(cpus_subnode_offset));
531 ret = cpus_subnode_offset;
535 ret = add_node_props(fdt, cpus_subnode_offset, dn);
540 of_node_put(cpus_node);
544 #endif /* CONFIG_KEXEC_FILE || CONFIG_CRASH_DUMP */