machine_kexec_64.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 <asm/page.h>
  17. #include <asm/current.h>
  18. #include <asm/machdep.h>
  19. #include <asm/cacheflush.h>
  20. #include <asm/paca.h>
  21. #include <asm/mmu.h>
  22. #include <asm/sections.h> /* _end */
  23. #include <asm/prom.h>
  24. #include <asm/smp.h>
  25. #include <asm/hw_breakpoint.h>
  26. int default_machine_kexec_prepare(struct kimage *image)
  27. {
  28. int i;
  29. unsigned long begin, end; /* limits of segment */
  30. unsigned long low, high; /* limits of blocked memory range */
  31. struct device_node *node;
  32. const unsigned long *basep;
  33. const unsigned int *sizep;
  34. if (!ppc_md.hpte_clear_all)
  35. return -ENOENT;
  36. /*
  37. * Since we use the kernel fault handlers and paging code to
  38. * handle the virtual mode, we must make sure no destination
  39. * overlaps kernel static data or bss.
  40. */
  41. for (i = 0; i < image->nr_segments; i++)
  42. if (image->segment[i].mem < __pa(_end))
  43. return -ETXTBSY;
  44. /*
  45. * For non-LPAR, we absolutely can not overwrite the mmu hash
  46. * table, since we are still using the bolted entries in it to
  47. * do the copy. Check that here.
  48. *
  49. * It is safe if the end is below the start of the blocked
  50. * region (end <= low), or if the beginning is after the
  51. * end of the blocked region (begin >= high). Use the
  52. * boolean identity !(a || b) === (!a && !b).
  53. */
  54. if (htab_address) {
  55. low = __pa(htab_address);
  56. high = low + htab_size_bytes;
  57. for (i = 0; i < image->nr_segments; i++) {
  58. begin = image->segment[i].mem;
  59. end = begin + image->segment[i].memsz;
  60. if ((begin < high) && (end > low))
  61. return -ETXTBSY;
  62. }
  63. }
  64. /* We also should not overwrite the tce tables */
  65. for (node = of_find_node_by_type(NULL, "pci"); node != NULL;
  66. node = of_find_node_by_type(node, "pci")) {
  67. basep = of_get_property(node, "linux,tce-base", NULL);
  68. sizep = of_get_property(node, "linux,tce-size", NULL);
  69. if (basep == NULL || sizep == NULL)
  70. continue;
  71. low = *basep;
  72. high = low + (*sizep);
  73. for (i = 0; i < image->nr_segments; i++) {
  74. begin = image->segment[i].mem;
  75. end = begin + image->segment[i].memsz;
  76. if ((begin < high) && (end > low))
  77. return -ETXTBSY;
  78. }
  79. }
  80. return 0;
  81. }
  82. #define IND_FLAGS (IND_DESTINATION | IND_INDIRECTION | IND_DONE | IND_SOURCE)
  83. static void copy_segments(unsigned long ind)
  84. {
  85. unsigned long entry;
  86. unsigned long *ptr;
  87. void *dest;
  88. void *addr;
  89. /*
  90. * We rely on kexec_load to create a lists that properly
  91. * initializes these pointers before they are used.
  92. * We will still crash if the list is wrong, but at least
  93. * the compiler will be quiet.
  94. */
  95. ptr = NULL;
  96. dest = NULL;
  97. for (entry = ind; !(entry & IND_DONE); entry = *ptr++) {
  98. addr = __va(entry & PAGE_MASK);
  99. switch (entry & IND_FLAGS) {
  100. case IND_DESTINATION:
  101. dest = addr;
  102. break;
  103. case IND_INDIRECTION:
  104. ptr = addr;
  105. break;
  106. case IND_SOURCE:
  107. copy_page(dest, addr);
  108. dest += PAGE_SIZE;
  109. }
  110. }
  111. }
  112. void kexec_copy_flush(struct kimage *image)
  113. {
  114. long i, nr_segments = image->nr_segments;
  115. struct kexec_segment ranges[KEXEC_SEGMENT_MAX];
  116. /* save the ranges on the stack to efficiently flush the icache */
  117. memcpy(ranges, image->segment, sizeof(ranges));
  118. /*
  119. * After this call we may not use anything allocated in dynamic
  120. * memory, including *image.
  121. *
  122. * Only globals and the stack are allowed.
  123. */
  124. copy_segments(image->head);
  125. /*
  126. * we need to clear the icache for all dest pages sometime,
  127. * including ones that were in place on the original copy
  128. */
  129. for (i = 0; i < nr_segments; i++)
  130. flush_icache_range((unsigned long)__va(ranges[i].mem),
  131. (unsigned long)__va(ranges[i].mem + ranges[i].memsz));
  132. }
  133. #ifdef CONFIG_SMP
  134. static int kexec_all_irq_disabled = 0;
  135. static void kexec_smp_down(void *arg)
  136. {
  137. local_irq_disable();
  138. mb(); /* make sure our irqs are disabled before we say they are */
  139. get_paca()->kexec_state = KEXEC_STATE_IRQS_OFF;
  140. while(kexec_all_irq_disabled == 0)
  141. cpu_relax();
  142. mb(); /* make sure all irqs are disabled before this */
  143. hw_breakpoint_disable();
  144. /*
  145. * Now every CPU has IRQs off, we can clear out any pending
  146. * IPIs and be sure that no more will come in after this.
  147. */
  148. if (ppc_md.kexec_cpu_down)
  149. ppc_md.kexec_cpu_down(0, 1);
  150. kexec_smp_wait();
  151. /* NOTREACHED */
  152. }
  153. static void kexec_prepare_cpus_wait(int wait_state)
  154. {
  155. int my_cpu, i, notified=-1;
  156. hw_breakpoint_disable();
  157. my_cpu = get_cpu();
  158. /* Make sure each CPU has atleast made it to the state we need */
  159. for (i=0; i < NR_CPUS; i++) {
  160. if (i == my_cpu)
  161. continue;
  162. while (paca[i].kexec_state < wait_state) {
  163. barrier();
  164. if (!cpu_possible(i)) {
  165. printk("kexec: cpu %d hw_cpu_id %d is not"
  166. " possible, ignoring\n",
  167. i, paca[i].hw_cpu_id);
  168. break;
  169. }
  170. if (!cpu_online(i)) {
  171. /* Fixme: this can be spinning in
  172. * pSeries_secondary_wait with a paca
  173. * waiting for it to go online.
  174. */
  175. printk("kexec: cpu %d hw_cpu_id %d is not"
  176. " online, ignoring\n",
  177. i, paca[i].hw_cpu_id);
  178. break;
  179. }
  180. if (i != notified) {
  181. printk( "kexec: waiting for cpu %d (physical"
  182. " %d) to enter %i state\n",
  183. i, paca[i].hw_cpu_id, wait_state);
  184. notified = i;
  185. }
  186. }
  187. }
  188. mb();
  189. }
  190. static void kexec_prepare_cpus(void)
  191. {
  192. smp_call_function(kexec_smp_down, NULL, /* wait */0);
  193. local_irq_disable();
  194. mb(); /* make sure IRQs are disabled before we say they are */
  195. get_paca()->kexec_state = KEXEC_STATE_IRQS_OFF;
  196. kexec_prepare_cpus_wait(KEXEC_STATE_IRQS_OFF);
  197. /* we are sure every CPU has IRQs off at this point */
  198. kexec_all_irq_disabled = 1;
  199. /* after we tell the others to go down */
  200. if (ppc_md.kexec_cpu_down)
  201. ppc_md.kexec_cpu_down(0, 0);
  202. /* Before removing MMU mapings make sure all CPUs have entered real mode */
  203. kexec_prepare_cpus_wait(KEXEC_STATE_REAL_MODE);
  204. put_cpu();
  205. }
  206. #else /* ! SMP */
  207. static void kexec_prepare_cpus(void)
  208. {
  209. /*
  210. * move the secondarys to us so that we can copy
  211. * the new kernel 0-0x100 safely
  212. *
  213. * do this if kexec in setup.c ?
  214. *
  215. * We need to release the cpus if we are ever going from an
  216. * UP to an SMP kernel.
  217. */
  218. smp_release_cpus();
  219. if (ppc_md.kexec_cpu_down)
  220. ppc_md.kexec_cpu_down(0, 0);
  221. local_irq_disable();
  222. }
  223. #endif /* SMP */
  224. /*
  225. * kexec thread structure and stack.
  226. *
  227. * We need to make sure that this is 16384-byte aligned due to the
  228. * way process stacks are handled. It also must be statically allocated
  229. * or allocated as part of the kimage, because everything else may be
  230. * overwritten when we copy the kexec image. We piggyback on the
  231. * "init_task" linker section here to statically allocate a stack.
  232. *
  233. * We could use a smaller stack if we don't care about anything using
  234. * current, but that audit has not been performed.
  235. */
  236. static union thread_union kexec_stack __init_task_data =
  237. { };
  238. /* Our assembly helper, in kexec_stub.S */
  239. extern NORET_TYPE void kexec_sequence(void *newstack, unsigned long start,
  240. void *image, void *control,
  241. void (*clear_all)(void)) ATTRIB_NORET;
  242. /* too late to fail here */
  243. void default_machine_kexec(struct kimage *image)
  244. {
  245. /* prepare control code if any */
  246. /*
  247. * If the kexec boot is the normal one, need to shutdown other cpus
  248. * into our wait loop and quiesce interrupts.
  249. * Otherwise, in the case of crashed mode (crashing_cpu >= 0),
  250. * stopping other CPUs and collecting their pt_regs is done before
  251. * using debugger IPI.
  252. */
  253. if (crashing_cpu == -1)
  254. kexec_prepare_cpus();
  255. /* switch to a staticly allocated stack. Based on irq stack code.
  256. * XXX: the task struct will likely be invalid once we do the copy!
  257. */
  258. kexec_stack.thread_info.task = current_thread_info()->task;
  259. kexec_stack.thread_info.flags = 0;
  260. /* Some things are best done in assembly. Finding globals with
  261. * a toc is easier in C, so pass in what we can.
  262. */
  263. kexec_sequence(&kexec_stack, image->start, image,
  264. page_address(image->control_code_page),
  265. ppc_md.hpte_clear_all);
  266. /* NOTREACHED */
  267. }
  268. /* Values we need to export to the second kernel via the device tree. */
  269. static unsigned long htab_base;
  270. static struct property htab_base_prop = {
  271. .name = "linux,htab-base",
  272. .length = sizeof(unsigned long),
  273. .value = &htab_base,
  274. };
  275. static struct property htab_size_prop = {
  276. .name = "linux,htab-size",
  277. .length = sizeof(unsigned long),
  278. .value = &htab_size_bytes,
  279. };
  280. static int __init export_htab_values(void)
  281. {
  282. struct device_node *node;
  283. struct property *prop;
  284. /* On machines with no htab htab_address is NULL */
  285. if (!htab_address)
  286. return -ENODEV;
  287. node = of_find_node_by_path("/chosen");
  288. if (!node)
  289. return -ENODEV;
  290. /* remove any stale propertys so ours can be found */
  291. prop = of_find_property(node, htab_base_prop.name, NULL);
  292. if (prop)
  293. prom_remove_property(node, prop);
  294. prop = of_find_property(node, htab_size_prop.name, NULL);
  295. if (prop)
  296. prom_remove_property(node, prop);
  297. htab_base = __pa(htab_address);
  298. prom_add_property(node, &htab_base_prop);
  299. prom_add_property(node, &htab_size_prop);
  300. of_node_put(node);
  301. return 0;
  302. }
  303. late_initcall(export_htab_values);