machine_kexec_64.c 11 KB

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