Commit | Line | Data |
---|---|---|
40b0b3f8 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
fce0d574 | 2 | /* |
3d1229d6 | 3 | * PPC64 code to handle Linux booting another kernel. |
fce0d574 S |
4 | * |
5 | * Copyright (C) 2004-2005, IBM Corp. | |
6 | * | |
7 | * Created by: Milton D Miller II | |
fce0d574 S |
8 | */ |
9 | ||
10 | ||
fce0d574 S |
11 | #include <linux/kexec.h> |
12 | #include <linux/smp.h> | |
13 | #include <linux/thread_info.h> | |
d200c922 | 14 | #include <linux/init_task.h> |
fce0d574 | 15 | #include <linux/errno.h> |
e2f7f737 | 16 | #include <linux/kernel.h> |
e8e5c215 | 17 | #include <linux/cpu.h> |
79c66ce8 | 18 | #include <linux/hardirq.h> |
e6f6390a | 19 | #include <linux/of.h> |
fce0d574 S |
20 | |
21 | #include <asm/page.h> | |
22 | #include <asm/current.h> | |
23 | #include <asm/machdep.h> | |
24 | #include <asm/cacheflush.h> | |
b970b41e | 25 | #include <asm/firmware.h> |
fce0d574 S |
26 | #include <asm/paca.h> |
27 | #include <asm/mmu.h> | |
28 | #include <asm/sections.h> /* _end */ | |
2249ca9d | 29 | #include <asm/smp.h> |
5aae8a53 | 30 | #include <asm/hw_breakpoint.h> |
256ba2c1 RP |
31 | #include <asm/svm.h> |
32 | #include <asm/ultravisor.h> | |
fce0d574 | 33 | |
8f7fadb4 | 34 | int machine_kexec_prepare(struct kimage *image) |
fce0d574 S |
35 | { |
36 | int i; | |
37 | unsigned long begin, end; /* limits of segment */ | |
38 | unsigned long low, high; /* limits of blocked memory range */ | |
39 | struct device_node *node; | |
a7f67bdf JK |
40 | const unsigned long *basep; |
41 | const unsigned int *sizep; | |
fce0d574 | 42 | |
fce0d574 S |
43 | /* |
44 | * Since we use the kernel fault handlers and paging code to | |
45 | * handle the virtual mode, we must make sure no destination | |
46 | * overlaps kernel static data or bss. | |
47 | */ | |
72414d3f | 48 | for (i = 0; i < image->nr_segments; i++) |
fce0d574 S |
49 | if (image->segment[i].mem < __pa(_end)) |
50 | return -ETXTBSY; | |
51 | ||
fce0d574 | 52 | /* We also should not overwrite the tce tables */ |
94db7c5e | 53 | for_each_node_by_type(node, "pci") { |
e2eb6392 SR |
54 | basep = of_get_property(node, "linux,tce-base", NULL); |
55 | sizep = of_get_property(node, "linux,tce-size", NULL); | |
fce0d574 S |
56 | if (basep == NULL || sizep == NULL) |
57 | continue; | |
58 | ||
59 | low = *basep; | |
60 | high = low + (*sizep); | |
61 | ||
72414d3f | 62 | for (i = 0; i < image->nr_segments; i++) { |
fce0d574 S |
63 | begin = image->segment[i].mem; |
64 | end = begin + image->segment[i].memsz; | |
65 | ||
c00103ab | 66 | if ((begin < high) && (end > low)) { |
67 | of_node_put(node); | |
fce0d574 | 68 | return -ETXTBSY; |
c00103ab | 69 | } |
fce0d574 S |
70 | } |
71 | } | |
72 | ||
73 | return 0; | |
74 | } | |
75 | ||
8119cefd HB |
76 | /* Called during kexec sequence with MMU off */ |
77 | static notrace void copy_segments(unsigned long ind) | |
fce0d574 S |
78 | { |
79 | unsigned long entry; | |
80 | unsigned long *ptr; | |
81 | void *dest; | |
82 | void *addr; | |
83 | ||
84 | /* | |
85 | * We rely on kexec_load to create a lists that properly | |
86 | * initializes these pointers before they are used. | |
87 | * We will still crash if the list is wrong, but at least | |
88 | * the compiler will be quiet. | |
89 | */ | |
90 | ptr = NULL; | |
91 | dest = NULL; | |
92 | ||
93 | for (entry = ind; !(entry & IND_DONE); entry = *ptr++) { | |
94 | addr = __va(entry & PAGE_MASK); | |
95 | ||
96 | switch (entry & IND_FLAGS) { | |
97 | case IND_DESTINATION: | |
98 | dest = addr; | |
99 | break; | |
100 | case IND_INDIRECTION: | |
101 | ptr = addr; | |
102 | break; | |
103 | case IND_SOURCE: | |
104 | copy_page(dest, addr); | |
105 | dest += PAGE_SIZE; | |
106 | } | |
107 | } | |
108 | } | |
109 | ||
8119cefd HB |
110 | /* Called during kexec sequence with MMU off */ |
111 | notrace void kexec_copy_flush(struct kimage *image) | |
fce0d574 S |
112 | { |
113 | long i, nr_segments = image->nr_segments; | |
114 | struct kexec_segment ranges[KEXEC_SEGMENT_MAX]; | |
115 | ||
116 | /* save the ranges on the stack to efficiently flush the icache */ | |
117 | memcpy(ranges, image->segment, sizeof(ranges)); | |
118 | ||
119 | /* | |
120 | * After this call we may not use anything allocated in dynamic | |
121 | * memory, including *image. | |
122 | * | |
123 | * Only globals and the stack are allowed. | |
124 | */ | |
125 | copy_segments(image->head); | |
126 | ||
127 | /* | |
128 | * we need to clear the icache for all dest pages sometime, | |
129 | * including ones that were in place on the original copy | |
130 | */ | |
131 | for (i = 0; i < nr_segments; i++) | |
b5666f70 ME |
132 | flush_icache_range((unsigned long)__va(ranges[i].mem), |
133 | (unsigned long)__va(ranges[i].mem + ranges[i].memsz)); | |
fce0d574 S |
134 | } |
135 | ||
136 | #ifdef CONFIG_SMP | |
137 | ||
1fc711f7 MN |
138 | static int kexec_all_irq_disabled = 0; |
139 | ||
1c21a293 | 140 | static void kexec_smp_down(void *arg) |
fce0d574 | 141 | { |
1fc711f7 | 142 | local_irq_disable(); |
8520e443 PF |
143 | hard_irq_disable(); |
144 | ||
1fc711f7 MN |
145 | mb(); /* make sure our irqs are disabled before we say they are */ |
146 | get_paca()->kexec_state = KEXEC_STATE_IRQS_OFF; | |
147 | while(kexec_all_irq_disabled == 0) | |
148 | cpu_relax(); | |
149 | mb(); /* make sure all irqs are disabled before this */ | |
5aae8a53 | 150 | hw_breakpoint_disable(); |
1fc711f7 MN |
151 | /* |
152 | * Now every CPU has IRQs off, we can clear out any pending | |
153 | * IPIs and be sure that no more will come in after this. | |
154 | */ | |
c5e24354 ME |
155 | if (ppc_md.kexec_cpu_down) |
156 | ppc_md.kexec_cpu_down(0, 1); | |
fce0d574 | 157 | |
000a42b3 AK |
158 | reset_sprs(); |
159 | ||
fce0d574 S |
160 | kexec_smp_wait(); |
161 | /* NOTREACHED */ | |
162 | } | |
163 | ||
1fc711f7 | 164 | static void kexec_prepare_cpus_wait(int wait_state) |
fce0d574 S |
165 | { |
166 | int my_cpu, i, notified=-1; | |
167 | ||
5aae8a53 | 168 | hw_breakpoint_disable(); |
fce0d574 | 169 | my_cpu = get_cpu(); |
e2f7f737 ME |
170 | /* Make sure each CPU has at least made it to the state we need. |
171 | * | |
172 | * FIXME: There is a (slim) chance of a problem if not all of the CPUs | |
173 | * are correctly onlined. If somehow we start a CPU on boot with RTAS | |
174 | * start-cpu, but somehow that CPU doesn't write callin_cpu_map[] in | |
175 | * time, the boot CPU will timeout. If it does eventually execute | |
d2e60075 NP |
176 | * stuff, the secondary will start up (paca_ptrs[]->cpu_start was |
177 | * written) and get into a peculiar state. | |
178 | * If the platform supports smp_ops->take_timebase(), the secondary CPU | |
179 | * will probably be spinning in there. If not (i.e. pseries), the | |
180 | * secondary will continue on and try to online itself/idle/etc. If it | |
181 | * survives that, we need to find these | |
182 | * possible-but-not-online-but-should-be CPUs and chaperone them into | |
183 | * kexec_smp_wait(). | |
e2f7f737 | 184 | */ |
b636f137 | 185 | for_each_online_cpu(i) { |
fce0d574 S |
186 | if (i == my_cpu) |
187 | continue; | |
188 | ||
d2e60075 | 189 | while (paca_ptrs[i]->kexec_state < wait_state) { |
b3ca8093 | 190 | barrier(); |
fce0d574 | 191 | if (i != notified) { |
e2f7f737 ME |
192 | printk(KERN_INFO "kexec: waiting for cpu %d " |
193 | "(physical %d) to enter %i state\n", | |
d2e60075 | 194 | i, paca_ptrs[i]->hw_cpu_id, wait_state); |
fce0d574 S |
195 | notified = i; |
196 | } | |
197 | } | |
198 | } | |
1fc711f7 MN |
199 | mb(); |
200 | } | |
201 | ||
e8e5c215 ME |
202 | /* |
203 | * We need to make sure each present CPU is online. The next kernel will scan | |
204 | * the device tree and assume primary threads are online and query secondary | |
205 | * threads via RTAS to online them if required. If we don't online primary | |
206 | * threads, they will be stuck. However, we also online secondary threads as we | |
207 | * may be using 'cede offline'. In this case RTAS doesn't see the secondary | |
208 | * threads as offline -- and again, these CPUs will be stuck. | |
209 | * | |
210 | * So, we online all CPUs that should be running, including secondary threads. | |
211 | */ | |
212 | static void wake_offline_cpus(void) | |
1fc711f7 | 213 | { |
e8e5c215 ME |
214 | int cpu = 0; |
215 | ||
216 | for_each_present_cpu(cpu) { | |
217 | if (!cpu_online(cpu)) { | |
218 | printk(KERN_INFO "kexec: Waking offline cpu %d.\n", | |
219 | cpu); | |
4d37cc2d | 220 | WARN_ON(add_cpu(cpu)); |
e8e5c215 ME |
221 | } |
222 | } | |
223 | } | |
1fc711f7 | 224 | |
e8e5c215 ME |
225 | static void kexec_prepare_cpus(void) |
226 | { | |
227 | wake_offline_cpus(); | |
1fc711f7 MN |
228 | smp_call_function(kexec_smp_down, NULL, /* wait */0); |
229 | local_irq_disable(); | |
8520e443 PF |
230 | hard_irq_disable(); |
231 | ||
1fc711f7 MN |
232 | mb(); /* make sure IRQs are disabled before we say they are */ |
233 | get_paca()->kexec_state = KEXEC_STATE_IRQS_OFF; | |
234 | ||
235 | kexec_prepare_cpus_wait(KEXEC_STATE_IRQS_OFF); | |
236 | /* we are sure every CPU has IRQs off at this point */ | |
237 | kexec_all_irq_disabled = 1; | |
fce0d574 | 238 | |
e2f7f737 ME |
239 | /* |
240 | * Before removing MMU mappings make sure all CPUs have entered real | |
241 | * mode: | |
242 | */ | |
1fc711f7 | 243 | kexec_prepare_cpus_wait(KEXEC_STATE_REAL_MODE); |
fce0d574 | 244 | |
d2b04b0c CLG |
245 | /* after we tell the others to go down */ |
246 | if (ppc_md.kexec_cpu_down) | |
247 | ppc_md.kexec_cpu_down(0, 0); | |
248 | ||
1fc711f7 | 249 | put_cpu(); |
fce0d574 S |
250 | } |
251 | ||
252 | #else /* ! SMP */ | |
253 | ||
254 | static void kexec_prepare_cpus(void) | |
255 | { | |
256 | /* | |
257 | * move the secondarys to us so that we can copy | |
258 | * the new kernel 0-0x100 safely | |
259 | * | |
260 | * do this if kexec in setup.c ? | |
75eedfed OJ |
261 | * |
262 | * We need to release the cpus if we are ever going from an | |
263 | * UP to an SMP kernel. | |
fce0d574 | 264 | */ |
75eedfed | 265 | smp_release_cpus(); |
c5e24354 ME |
266 | if (ppc_md.kexec_cpu_down) |
267 | ppc_md.kexec_cpu_down(0, 0); | |
fce0d574 | 268 | local_irq_disable(); |
8520e443 | 269 | hard_irq_disable(); |
fce0d574 S |
270 | } |
271 | ||
272 | #endif /* SMP */ | |
273 | ||
274 | /* | |
275 | * kexec thread structure and stack. | |
276 | * | |
277 | * We need to make sure that this is 16384-byte aligned due to the | |
278 | * way process stacks are handled. It also must be statically allocated | |
279 | * or allocated as part of the kimage, because everything else may be | |
280 | * overwritten when we copy the kexec image. We piggyback on the | |
281 | * "init_task" linker section here to statically allocate a stack. | |
282 | * | |
283 | * We could use a smaller stack if we don't care about anything using | |
284 | * current, but that audit has not been performed. | |
285 | */ | |
d200c922 JP |
286 | static union thread_union kexec_stack __init_task_data = |
287 | { }; | |
fce0d574 | 288 | |
fc53b420 ME |
289 | /* |
290 | * For similar reasons to the stack above, the kexecing CPU needs to be on a | |
291 | * static PACA; we switch to kexec_paca. | |
292 | */ | |
a4abd55a | 293 | static struct paca_struct kexec_paca; |
fc53b420 | 294 | |
07fb41a7 | 295 | /* Our assembly helper, in misc_64.S */ |
9402c95f JP |
296 | extern void kexec_sequence(void *newstack, unsigned long start, |
297 | void *image, void *control, | |
b970b41e BH |
298 | void (*clear_all)(void), |
299 | bool copy_with_mmu_off) __noreturn; | |
fce0d574 S |
300 | |
301 | /* too late to fail here */ | |
3d1229d6 | 302 | void default_machine_kexec(struct kimage *image) |
fce0d574 | 303 | { |
b970b41e BH |
304 | bool copy_with_mmu_off; |
305 | ||
fce0d574 S |
306 | /* prepare control code if any */ |
307 | ||
cc532915 ME |
308 | /* |
309 | * If the kexec boot is the normal one, need to shutdown other cpus | |
310 | * into our wait loop and quiesce interrupts. | |
311 | * Otherwise, in the case of crashed mode (crashing_cpu >= 0), | |
312 | * stopping other CPUs and collecting their pt_regs is done before | |
313 | * using debugger IPI. | |
314 | */ | |
315 | ||
c1caae3d | 316 | if (!kdump_in_progress()) |
54622f10 | 317 | kexec_prepare_cpus(); |
fce0d574 | 318 | |
0d976313 | 319 | printk("kexec: Starting switchover sequence.\n"); |
e2f7f737 | 320 | |
fce0d574 | 321 | /* switch to a staticly allocated stack. Based on irq stack code. |
79c66ce8 | 322 | * We setup preempt_count to avoid using VMX in memcpy. |
fce0d574 S |
323 | * XXX: the task struct will likely be invalid once we do the copy! |
324 | */ | |
ed1cd6de CL |
325 | current_thread_info()->flags = 0; |
326 | current_thread_info()->preempt_count = HARDIRQ_OFFSET; | |
fce0d574 | 327 | |
fc53b420 | 328 | /* We need a static PACA, too; copy this CPU's PACA over and switch to |
499dcd41 NP |
329 | * it. Also poison per_cpu_offset and NULL lppaca to catch anyone using |
330 | * non-static data. | |
fc53b420 ME |
331 | */ |
332 | memcpy(&kexec_paca, get_paca(), sizeof(struct paca_struct)); | |
333 | kexec_paca.data_offset = 0xedeaddeadeeeeeeeUL; | |
499dcd41 NP |
334 | #ifdef CONFIG_PPC_PSERIES |
335 | kexec_paca.lppaca_ptr = NULL; | |
336 | #endif | |
256ba2c1 RP |
337 | |
338 | if (is_secure_guest() && !(image->preserve_context || | |
339 | image->type == KEXEC_TYPE_CRASH)) { | |
340 | uv_unshare_all_pages(); | |
341 | printk("kexec: Unshared all shared pages.\n"); | |
342 | } | |
343 | ||
d2e60075 | 344 | paca_ptrs[kexec_paca.paca_index] = &kexec_paca; |
499dcd41 | 345 | |
fc53b420 ME |
346 | setup_paca(&kexec_paca); |
347 | ||
499dcd41 NP |
348 | /* |
349 | * The lppaca should be unregistered at this point so the HV won't | |
350 | * touch it. In the case of a crash, none of the lppacas are | |
351 | * unregistered so there is not much we can do about it here. | |
fc53b420 | 352 | */ |
499dcd41 | 353 | |
b970b41e BH |
354 | /* |
355 | * On Book3S, the copy must happen with the MMU off if we are either | |
356 | * using Radix page tables or we are not in an LPAR since we can | |
357 | * overwrite the page tables while copying. | |
358 | * | |
359 | * In an LPAR, we keep the MMU on otherwise we can't access beyond | |
360 | * the RMA. On BookE there is no real MMU off mode, so we have to | |
361 | * keep it enabled as well (but then we have bolted TLB entries). | |
362 | */ | |
e0d68273 | 363 | #ifdef CONFIG_PPC_BOOK3E_64 |
b970b41e BH |
364 | copy_with_mmu_off = false; |
365 | #else | |
366 | copy_with_mmu_off = radix_enabled() || | |
367 | !(firmware_has_feature(FW_FEATURE_LPAR) || | |
368 | firmware_has_feature(FW_FEATURE_PS3_LV1)); | |
369 | #endif | |
fc53b420 | 370 | |
fce0d574 S |
371 | /* Some things are best done in assembly. Finding globals with |
372 | * a toc is easier in C, so pass in what we can. | |
373 | */ | |
374 | kexec_sequence(&kexec_stack, image->start, image, | |
fe036a06 | 375 | page_address(image->control_code_page), |
b970b41e | 376 | mmu_cleanup_all, copy_with_mmu_off); |
fce0d574 S |
377 | /* NOTREACHED */ |
378 | } | |
593e537b | 379 | |
387e220a | 380 | #ifdef CONFIG_PPC_64S_HASH_MMU |
593e537b | 381 | /* Values we need to export to the second kernel via the device tree. */ |
2e8e4f5b | 382 | static unsigned long htab_base; |
ea961a82 | 383 | static unsigned long htab_size; |
593e537b ME |
384 | |
385 | static struct property htab_base_prop = { | |
386 | .name = "linux,htab-base", | |
387 | .length = sizeof(unsigned long), | |
1a38147e | 388 | .value = &htab_base, |
593e537b ME |
389 | }; |
390 | ||
391 | static struct property htab_size_prop = { | |
392 | .name = "linux,htab-size", | |
393 | .length = sizeof(unsigned long), | |
ea961a82 | 394 | .value = &htab_size, |
593e537b ME |
395 | }; |
396 | ||
6f29c329 | 397 | static int __init export_htab_values(void) |
593e537b ME |
398 | { |
399 | struct device_node *node; | |
400 | ||
2e8e4f5b DF |
401 | /* On machines with no htab htab_address is NULL */ |
402 | if (!htab_address) | |
6f29c329 | 403 | return -ENODEV; |
2e8e4f5b | 404 | |
593e537b ME |
405 | node = of_find_node_by_path("/chosen"); |
406 | if (!node) | |
6f29c329 | 407 | return -ENODEV; |
593e537b | 408 | |
1fd02f66 | 409 | /* remove any stale properties so ours can be found */ |
925e2d1d SJS |
410 | of_remove_property(node, of_find_property(node, htab_base_prop.name, NULL)); |
411 | of_remove_property(node, of_find_property(node, htab_size_prop.name, NULL)); | |
ed7b2144 | 412 | |
ea961a82 | 413 | htab_base = cpu_to_be64(__pa(htab_address)); |
79d1c712 | 414 | of_add_property(node, &htab_base_prop); |
ea961a82 | 415 | htab_size = cpu_to_be64(htab_size_bytes); |
79d1c712 | 416 | of_add_property(node, &htab_size_prop); |
593e537b | 417 | |
593e537b | 418 | of_node_put(node); |
aa98c50d | 419 | return 0; |
35dd5432 | 420 | } |
6f29c329 | 421 | late_initcall(export_htab_values); |
387e220a | 422 | #endif /* CONFIG_PPC_64S_HASH_MMU */ |