machine_kexec_64.c 8.4 KB

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