machine_kexec.c 7.7 KB

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