machine_kexec_64.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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[].cpu_start was written) and
  147. * get into a peculiar state. If the platform supports
  148. * smp_ops->take_timebase(), the secondary CPU will probably be spinning
  149. * in there. If not (i.e. pseries), the secondary will continue on and
  150. * try to online itself/idle/etc. If it survives that, we need to find
  151. * these possible-but-not-online-but-should-be CPUs and chaperone them
  152. * into kexec_smp_wait().
  153. */
  154. for_each_online_cpu(i) {
  155. if (i == my_cpu)
  156. continue;
  157. while (paca[i].kexec_state < wait_state) {
  158. barrier();
  159. if (i != notified) {
  160. printk(KERN_INFO "kexec: waiting for cpu %d "
  161. "(physical %d) to enter %i state\n",
  162. i, paca[i].hw_cpu_id, wait_state);
  163. notified = i;
  164. }
  165. }
  166. }
  167. mb();
  168. }
  169. /*
  170. * We need to make sure each present CPU is online. The next kernel will scan
  171. * the device tree and assume primary threads are online and query secondary
  172. * threads via RTAS to online them if required. If we don't online primary
  173. * threads, they will be stuck. However, we also online secondary threads as we
  174. * may be using 'cede offline'. In this case RTAS doesn't see the secondary
  175. * threads as offline -- and again, these CPUs will be stuck.
  176. *
  177. * So, we online all CPUs that should be running, including secondary threads.
  178. */
  179. static void wake_offline_cpus(void)
  180. {
  181. int cpu = 0;
  182. for_each_present_cpu(cpu) {
  183. if (!cpu_online(cpu)) {
  184. printk(KERN_INFO "kexec: Waking offline cpu %d.\n",
  185. cpu);
  186. WARN_ON(cpu_up(cpu));
  187. }
  188. }
  189. }
  190. static void kexec_prepare_cpus(void)
  191. {
  192. wake_offline_cpus();
  193. smp_call_function(kexec_smp_down, NULL, /* wait */0);
  194. local_irq_disable();
  195. hard_irq_disable();
  196. mb(); /* make sure IRQs are disabled before we say they are */
  197. get_paca()->kexec_state = KEXEC_STATE_IRQS_OFF;
  198. kexec_prepare_cpus_wait(KEXEC_STATE_IRQS_OFF);
  199. /* we are sure every CPU has IRQs off at this point */
  200. kexec_all_irq_disabled = 1;
  201. /* after we tell the others to go down */
  202. if (ppc_md.kexec_cpu_down)
  203. ppc_md.kexec_cpu_down(0, 0);
  204. /*
  205. * Before removing MMU mappings make sure all CPUs have entered real
  206. * mode:
  207. */
  208. kexec_prepare_cpus_wait(KEXEC_STATE_REAL_MODE);
  209. put_cpu();
  210. }
  211. #else /* ! SMP */
  212. static void kexec_prepare_cpus(void)
  213. {
  214. /*
  215. * move the secondarys to us so that we can copy
  216. * the new kernel 0-0x100 safely
  217. *
  218. * do this if kexec in setup.c ?
  219. *
  220. * We need to release the cpus if we are ever going from an
  221. * UP to an SMP kernel.
  222. */
  223. smp_release_cpus();
  224. if (ppc_md.kexec_cpu_down)
  225. ppc_md.kexec_cpu_down(0, 0);
  226. local_irq_disable();
  227. hard_irq_disable();
  228. }
  229. #endif /* SMP */
  230. /*
  231. * kexec thread structure and stack.
  232. *
  233. * We need to make sure that this is 16384-byte aligned due to the
  234. * way process stacks are handled. It also must be statically allocated
  235. * or allocated as part of the kimage, because everything else may be
  236. * overwritten when we copy the kexec image. We piggyback on the
  237. * "init_task" linker section here to statically allocate a stack.
  238. *
  239. * We could use a smaller stack if we don't care about anything using
  240. * current, but that audit has not been performed.
  241. */
  242. static union thread_union kexec_stack __init_task_data =
  243. { };
  244. /*
  245. * For similar reasons to the stack above, the kexecing CPU needs to be on a
  246. * static PACA; we switch to kexec_paca.
  247. */
  248. struct paca_struct kexec_paca;
  249. /* Our assembly helper, in misc_64.S */
  250. extern void kexec_sequence(void *newstack, unsigned long start,
  251. void *image, void *control,
  252. void (*clear_all)(void),
  253. bool copy_with_mmu_off) __noreturn;
  254. /* too late to fail here */
  255. void default_machine_kexec(struct kimage *image)
  256. {
  257. bool copy_with_mmu_off;
  258. /* prepare control code if any */
  259. /*
  260. * If the kexec boot is the normal one, need to shutdown other cpus
  261. * into our wait loop and quiesce interrupts.
  262. * Otherwise, in the case of crashed mode (crashing_cpu >= 0),
  263. * stopping other CPUs and collecting their pt_regs is done before
  264. * using debugger IPI.
  265. */
  266. if (!kdump_in_progress())
  267. kexec_prepare_cpus();
  268. pr_debug("kexec: Starting switchover sequence.\n");
  269. /* switch to a staticly allocated stack. Based on irq stack code.
  270. * We setup preempt_count to avoid using VMX in memcpy.
  271. * XXX: the task struct will likely be invalid once we do the copy!
  272. */
  273. kexec_stack.thread_info.task = current_thread_info()->task;
  274. kexec_stack.thread_info.flags = 0;
  275. kexec_stack.thread_info.preempt_count = HARDIRQ_OFFSET;
  276. kexec_stack.thread_info.cpu = current_thread_info()->cpu;
  277. /* We need a static PACA, too; copy this CPU's PACA over and switch to
  278. * it. Also poison per_cpu_offset to catch anyone using non-static
  279. * data.
  280. */
  281. memcpy(&kexec_paca, get_paca(), sizeof(struct paca_struct));
  282. kexec_paca.data_offset = 0xedeaddeadeeeeeeeUL;
  283. paca = (struct paca_struct *)RELOC_HIDE(&kexec_paca, 0) -
  284. kexec_paca.paca_index;
  285. setup_paca(&kexec_paca);
  286. /* XXX: If anyone does 'dynamic lppacas' this will also need to be
  287. * switched to a static version!
  288. */
  289. /*
  290. * On Book3S, the copy must happen with the MMU off if we are either
  291. * using Radix page tables or we are not in an LPAR since we can
  292. * overwrite the page tables while copying.
  293. *
  294. * In an LPAR, we keep the MMU on otherwise we can't access beyond
  295. * the RMA. On BookE there is no real MMU off mode, so we have to
  296. * keep it enabled as well (but then we have bolted TLB entries).
  297. */
  298. #ifdef CONFIG_PPC_BOOK3E
  299. copy_with_mmu_off = false;
  300. #else
  301. copy_with_mmu_off = radix_enabled() ||
  302. !(firmware_has_feature(FW_FEATURE_LPAR) ||
  303. firmware_has_feature(FW_FEATURE_PS3_LV1));
  304. #endif
  305. /* Some things are best done in assembly. Finding globals with
  306. * a toc is easier in C, so pass in what we can.
  307. */
  308. kexec_sequence(&kexec_stack, image->start, image,
  309. page_address(image->control_code_page),
  310. mmu_cleanup_all, copy_with_mmu_off);
  311. /* NOTREACHED */
  312. }
  313. #ifdef CONFIG_PPC_STD_MMU_64
  314. /* Values we need to export to the second kernel via the device tree. */
  315. static unsigned long htab_base;
  316. static unsigned long htab_size;
  317. static struct property htab_base_prop = {
  318. .name = "linux,htab-base",
  319. .length = sizeof(unsigned long),
  320. .value = &htab_base,
  321. };
  322. static struct property htab_size_prop = {
  323. .name = "linux,htab-size",
  324. .length = sizeof(unsigned long),
  325. .value = &htab_size,
  326. };
  327. static int __init export_htab_values(void)
  328. {
  329. struct device_node *node;
  330. /* On machines with no htab htab_address is NULL */
  331. if (!htab_address)
  332. return -ENODEV;
  333. node = of_find_node_by_path("/chosen");
  334. if (!node)
  335. return -ENODEV;
  336. /* remove any stale propertys so ours can be found */
  337. of_remove_property(node, of_find_property(node, htab_base_prop.name, NULL));
  338. of_remove_property(node, of_find_property(node, htab_size_prop.name, NULL));
  339. htab_base = cpu_to_be64(__pa(htab_address));
  340. of_add_property(node, &htab_base_prop);
  341. htab_size = cpu_to_be64(htab_size_bytes);
  342. of_add_property(node, &htab_size_prop);
  343. of_node_put(node);
  344. return 0;
  345. }
  346. late_initcall(export_htab_values);
  347. #endif /* CONFIG_PPC_STD_MMU_64 */