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