crash.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * Architecture specific (PPC64) functions for kexec based crash dumps.
  3. *
  4. * Copyright (C) 2005, IBM Corp.
  5. *
  6. * Created by: Haren Myneni
  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. */
  12. #undef DEBUG
  13. #include <linux/kernel.h>
  14. #include <linux/smp.h>
  15. #include <linux/reboot.h>
  16. #include <linux/kexec.h>
  17. #include <linux/bootmem.h>
  18. #include <linux/crash_dump.h>
  19. #include <linux/delay.h>
  20. #include <linux/elf.h>
  21. #include <linux/elfcore.h>
  22. #include <linux/init.h>
  23. #include <linux/irq.h>
  24. #include <linux/types.h>
  25. #include <asm/processor.h>
  26. #include <asm/machdep.h>
  27. #include <asm/kexec.h>
  28. #include <asm/kdump.h>
  29. #include <asm/lmb.h>
  30. #include <asm/firmware.h>
  31. #include <asm/smp.h>
  32. #include <asm/system.h>
  33. #include <asm/setjmp.h>
  34. #ifdef DEBUG
  35. #include <asm/udbg.h>
  36. #define DBG(fmt...) udbg_printf(fmt)
  37. #else
  38. #define DBG(fmt...)
  39. #endif
  40. /* This keeps a track of which one is crashing cpu. */
  41. int crashing_cpu = -1;
  42. static cpumask_t cpus_in_crash = CPU_MASK_NONE;
  43. cpumask_t cpus_in_sr = CPU_MASK_NONE;
  44. #define CRASH_HANDLER_MAX 1
  45. /* NULL terminated list of shutdown handles */
  46. static crash_shutdown_t crash_shutdown_handles[CRASH_HANDLER_MAX+1];
  47. static DEFINE_SPINLOCK(crash_handlers_lock);
  48. #ifdef CONFIG_SMP
  49. static atomic_t enter_on_soft_reset = ATOMIC_INIT(0);
  50. void crash_ipi_callback(struct pt_regs *regs)
  51. {
  52. int cpu = smp_processor_id();
  53. if (!cpu_online(cpu))
  54. return;
  55. hard_irq_disable();
  56. if (!cpu_isset(cpu, cpus_in_crash))
  57. crash_save_cpu(regs, cpu);
  58. cpu_set(cpu, cpus_in_crash);
  59. /*
  60. * Entered via soft-reset - could be the kdump
  61. * process is invoked using soft-reset or user activated
  62. * it if some CPU did not respond to an IPI.
  63. * For soft-reset, the secondary CPU can enter this func
  64. * twice. 1 - using IPI, and 2. soft-reset.
  65. * Tell the kexec CPU that entered via soft-reset and ready
  66. * to go down.
  67. */
  68. if (cpu_isset(cpu, cpus_in_sr)) {
  69. cpu_clear(cpu, cpus_in_sr);
  70. atomic_inc(&enter_on_soft_reset);
  71. }
  72. /*
  73. * Starting the kdump boot.
  74. * This barrier is needed to make sure that all CPUs are stopped.
  75. * If not, soft-reset will be invoked to bring other CPUs.
  76. */
  77. while (!cpu_isset(crashing_cpu, cpus_in_crash))
  78. cpu_relax();
  79. if (ppc_md.kexec_cpu_down)
  80. ppc_md.kexec_cpu_down(1, 1);
  81. #ifdef CONFIG_PPC64
  82. kexec_smp_wait();
  83. #else
  84. for (;;); /* FIXME */
  85. #endif
  86. /* NOTREACHED */
  87. }
  88. /*
  89. * Wait until all CPUs are entered via soft-reset.
  90. */
  91. static void crash_soft_reset_check(int cpu)
  92. {
  93. unsigned int ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */
  94. cpu_clear(cpu, cpus_in_sr);
  95. while (atomic_read(&enter_on_soft_reset) != ncpus)
  96. cpu_relax();
  97. }
  98. static void crash_kexec_prepare_cpus(int cpu)
  99. {
  100. unsigned int msecs;
  101. unsigned int ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */
  102. crash_send_ipi(crash_ipi_callback);
  103. smp_wmb();
  104. /*
  105. * FIXME: Until we will have the way to stop other CPUSs reliabally,
  106. * the crash CPU will send an IPI and wait for other CPUs to
  107. * respond.
  108. * Delay of at least 10 seconds.
  109. */
  110. printk(KERN_EMERG "Sending IPI to other cpus...\n");
  111. msecs = 10000;
  112. while ((cpus_weight(cpus_in_crash) < ncpus) && (--msecs > 0)) {
  113. cpu_relax();
  114. mdelay(1);
  115. }
  116. /* Would it be better to replace the trap vector here? */
  117. /*
  118. * FIXME: In case if we do not get all CPUs, one possibility: ask the
  119. * user to do soft reset such that we get all.
  120. * Soft-reset will be used until better mechanism is implemented.
  121. */
  122. if (cpus_weight(cpus_in_crash) < ncpus) {
  123. printk(KERN_EMERG "done waiting: %d cpu(s) not responding\n",
  124. ncpus - cpus_weight(cpus_in_crash));
  125. printk(KERN_EMERG "Activate soft-reset to stop other cpu(s)\n");
  126. cpus_in_sr = CPU_MASK_NONE;
  127. atomic_set(&enter_on_soft_reset, 0);
  128. while (cpus_weight(cpus_in_crash) < ncpus)
  129. cpu_relax();
  130. }
  131. /*
  132. * Make sure all CPUs are entered via soft-reset if the kdump is
  133. * invoked using soft-reset.
  134. */
  135. if (cpu_isset(cpu, cpus_in_sr))
  136. crash_soft_reset_check(cpu);
  137. /* Leave the IPI callback set */
  138. }
  139. /*
  140. * This function will be called by secondary cpus or by kexec cpu
  141. * if soft-reset is activated to stop some CPUs.
  142. */
  143. void crash_kexec_secondary(struct pt_regs *regs)
  144. {
  145. int cpu = smp_processor_id();
  146. unsigned long flags;
  147. int msecs = 5;
  148. local_irq_save(flags);
  149. /* Wait 5ms if the kexec CPU is not entered yet. */
  150. while (crashing_cpu < 0) {
  151. if (--msecs < 0) {
  152. /*
  153. * Either kdump image is not loaded or
  154. * kdump process is not started - Probably xmon
  155. * exited using 'x'(exit and recover) or
  156. * kexec_should_crash() failed for all running tasks.
  157. */
  158. cpu_clear(cpu, cpus_in_sr);
  159. local_irq_restore(flags);
  160. return;
  161. }
  162. mdelay(1);
  163. cpu_relax();
  164. }
  165. if (cpu == crashing_cpu) {
  166. /*
  167. * Panic CPU will enter this func only via soft-reset.
  168. * Wait until all secondary CPUs entered and
  169. * then start kexec boot.
  170. */
  171. crash_soft_reset_check(cpu);
  172. cpu_set(crashing_cpu, cpus_in_crash);
  173. if (ppc_md.kexec_cpu_down)
  174. ppc_md.kexec_cpu_down(1, 0);
  175. machine_kexec(kexec_crash_image);
  176. /* NOTREACHED */
  177. }
  178. crash_ipi_callback(regs);
  179. }
  180. #else
  181. static void crash_kexec_prepare_cpus(int cpu)
  182. {
  183. /*
  184. * move the secondarys to us so that we can copy
  185. * the new kernel 0-0x100 safely
  186. *
  187. * do this if kexec in setup.c ?
  188. */
  189. #ifdef CONFIG_PPC64
  190. smp_release_cpus();
  191. #else
  192. /* FIXME */
  193. #endif
  194. }
  195. void crash_kexec_secondary(struct pt_regs *regs)
  196. {
  197. cpus_in_sr = CPU_MASK_NONE;
  198. }
  199. #endif
  200. #ifdef CONFIG_SPU_BASE
  201. #include <asm/spu.h>
  202. #include <asm/spu_priv1.h>
  203. struct crash_spu_info {
  204. struct spu *spu;
  205. u32 saved_spu_runcntl_RW;
  206. u32 saved_spu_status_R;
  207. u32 saved_spu_npc_RW;
  208. u64 saved_mfc_sr1_RW;
  209. u64 saved_mfc_dar;
  210. u64 saved_mfc_dsisr;
  211. };
  212. #define CRASH_NUM_SPUS 16 /* Enough for current hardware */
  213. static struct crash_spu_info crash_spu_info[CRASH_NUM_SPUS];
  214. static void crash_kexec_stop_spus(void)
  215. {
  216. struct spu *spu;
  217. int i;
  218. u64 tmp;
  219. for (i = 0; i < CRASH_NUM_SPUS; i++) {
  220. if (!crash_spu_info[i].spu)
  221. continue;
  222. spu = crash_spu_info[i].spu;
  223. crash_spu_info[i].saved_spu_runcntl_RW =
  224. in_be32(&spu->problem->spu_runcntl_RW);
  225. crash_spu_info[i].saved_spu_status_R =
  226. in_be32(&spu->problem->spu_status_R);
  227. crash_spu_info[i].saved_spu_npc_RW =
  228. in_be32(&spu->problem->spu_npc_RW);
  229. crash_spu_info[i].saved_mfc_dar = spu_mfc_dar_get(spu);
  230. crash_spu_info[i].saved_mfc_dsisr = spu_mfc_dsisr_get(spu);
  231. tmp = spu_mfc_sr1_get(spu);
  232. crash_spu_info[i].saved_mfc_sr1_RW = tmp;
  233. tmp &= ~MFC_STATE1_MASTER_RUN_CONTROL_MASK;
  234. spu_mfc_sr1_set(spu, tmp);
  235. __delay(200);
  236. }
  237. }
  238. void crash_register_spus(struct list_head *list)
  239. {
  240. struct spu *spu;
  241. list_for_each_entry(spu, list, full_list) {
  242. if (WARN_ON(spu->number >= CRASH_NUM_SPUS))
  243. continue;
  244. crash_spu_info[spu->number].spu = spu;
  245. }
  246. }
  247. #else
  248. static inline void crash_kexec_stop_spus(void)
  249. {
  250. }
  251. #endif /* CONFIG_SPU_BASE */
  252. /*
  253. * Register a function to be called on shutdown. Only use this if you
  254. * can't reset your device in the second kernel.
  255. */
  256. int crash_shutdown_register(crash_shutdown_t handler)
  257. {
  258. unsigned int i, rc;
  259. spin_lock(&crash_handlers_lock);
  260. for (i = 0 ; i < CRASH_HANDLER_MAX; i++)
  261. if (!crash_shutdown_handles[i]) {
  262. /* Insert handle at first empty entry */
  263. crash_shutdown_handles[i] = handler;
  264. rc = 0;
  265. break;
  266. }
  267. if (i == CRASH_HANDLER_MAX) {
  268. printk(KERN_ERR "Crash shutdown handles full, "
  269. "not registered.\n");
  270. rc = 1;
  271. }
  272. spin_unlock(&crash_handlers_lock);
  273. return rc;
  274. }
  275. EXPORT_SYMBOL(crash_shutdown_register);
  276. int crash_shutdown_unregister(crash_shutdown_t handler)
  277. {
  278. unsigned int i, rc;
  279. spin_lock(&crash_handlers_lock);
  280. for (i = 0 ; i < CRASH_HANDLER_MAX; i++)
  281. if (crash_shutdown_handles[i] == handler)
  282. break;
  283. if (i == CRASH_HANDLER_MAX) {
  284. printk(KERN_ERR "Crash shutdown handle not found\n");
  285. rc = 1;
  286. } else {
  287. /* Shift handles down */
  288. for (; crash_shutdown_handles[i]; i++)
  289. crash_shutdown_handles[i] =
  290. crash_shutdown_handles[i+1];
  291. rc = 0;
  292. }
  293. spin_unlock(&crash_handlers_lock);
  294. return rc;
  295. }
  296. EXPORT_SYMBOL(crash_shutdown_unregister);
  297. static unsigned long crash_shutdown_buf[JMP_BUF_LEN];
  298. static int handle_fault(struct pt_regs *regs)
  299. {
  300. longjmp(crash_shutdown_buf, 1);
  301. return 0;
  302. }
  303. void default_machine_crash_shutdown(struct pt_regs *regs)
  304. {
  305. unsigned int i;
  306. int (*old_handler)(struct pt_regs *regs);
  307. /*
  308. * This function is only called after the system
  309. * has panicked or is otherwise in a critical state.
  310. * The minimum amount of code to allow a kexec'd kernel
  311. * to run successfully needs to happen here.
  312. *
  313. * In practice this means stopping other cpus in
  314. * an SMP system.
  315. * The kernel is broken so disable interrupts.
  316. */
  317. hard_irq_disable();
  318. for_each_irq(i) {
  319. struct irq_desc *desc = irq_desc + i;
  320. if (desc->status & IRQ_INPROGRESS)
  321. desc->chip->eoi(i);
  322. if (!(desc->status & IRQ_DISABLED))
  323. desc->chip->disable(i);
  324. }
  325. /*
  326. * Call registered shutdown routines savely. Swap out
  327. * __debugger_fault_handler, and replace on exit.
  328. */
  329. old_handler = __debugger_fault_handler;
  330. __debugger_fault_handler = handle_fault;
  331. for (i = 0; crash_shutdown_handles[i]; i++) {
  332. if (setjmp(crash_shutdown_buf) == 0) {
  333. /*
  334. * Insert syncs and delay to ensure
  335. * instructions in the dangerous region don't
  336. * leak away from this protected region.
  337. */
  338. asm volatile("sync; isync");
  339. /* dangerous region */
  340. crash_shutdown_handles[i]();
  341. asm volatile("sync; isync");
  342. }
  343. }
  344. __debugger_fault_handler = old_handler;
  345. /*
  346. * Make a note of crashing cpu. Will be used in machine_kexec
  347. * such that another IPI will not be sent.
  348. */
  349. crashing_cpu = smp_processor_id();
  350. crash_save_cpu(regs, crashing_cpu);
  351. crash_kexec_prepare_cpus(crashing_cpu);
  352. cpu_set(crashing_cpu, cpus_in_crash);
  353. crash_kexec_stop_spus();
  354. if (ppc_md.kexec_cpu_down)
  355. ppc_md.kexec_cpu_down(1, 0);
  356. }