machine_kexec_64.c 10.0 KB

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