crash.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * arch/ia64/kernel/crash.c
  3. *
  4. * Architecture specific (ia64) functions for kexec based crash dumps.
  5. *
  6. * Created by: Khalid Aziz <khalid.aziz@hp.com>
  7. * Copyright (C) 2005 Hewlett-Packard Development Company, L.P.
  8. * Copyright (C) 2005 Intel Corp Zou Nan hai <nanhai.zou@intel.com>
  9. *
  10. */
  11. #include <linux/smp.h>
  12. #include <linux/delay.h>
  13. #include <linux/crash_dump.h>
  14. #include <linux/bootmem.h>
  15. #include <linux/kexec.h>
  16. #include <linux/elfcore.h>
  17. #include <linux/sysctl.h>
  18. #include <linux/init.h>
  19. #include <linux/kdebug.h>
  20. #include <asm/mca.h>
  21. int kdump_status[NR_CPUS];
  22. static atomic_t kdump_cpu_frozen;
  23. atomic_t kdump_in_progress;
  24. static int kdump_freeze_monarch;
  25. static int kdump_on_init = 1;
  26. static int kdump_on_fatal_mca = 1;
  27. static inline Elf64_Word
  28. *append_elf_note(Elf64_Word *buf, char *name, unsigned type, void *data,
  29. size_t data_len)
  30. {
  31. struct elf_note *note = (struct elf_note *)buf;
  32. note->n_namesz = strlen(name) + 1;
  33. note->n_descsz = data_len;
  34. note->n_type = type;
  35. buf += (sizeof(*note) + 3)/4;
  36. memcpy(buf, name, note->n_namesz);
  37. buf += (note->n_namesz + 3)/4;
  38. memcpy(buf, data, data_len);
  39. buf += (data_len + 3)/4;
  40. return buf;
  41. }
  42. static void
  43. final_note(void *buf)
  44. {
  45. memset(buf, 0, sizeof(struct elf_note));
  46. }
  47. extern void ia64_dump_cpu_regs(void *);
  48. static DEFINE_PER_CPU(struct elf_prstatus, elf_prstatus);
  49. void
  50. crash_save_this_cpu(void)
  51. {
  52. void *buf;
  53. unsigned long cfm, sof, sol;
  54. int cpu = smp_processor_id();
  55. struct elf_prstatus *prstatus = &per_cpu(elf_prstatus, cpu);
  56. elf_greg_t *dst = (elf_greg_t *)&(prstatus->pr_reg);
  57. memset(prstatus, 0, sizeof(*prstatus));
  58. prstatus->pr_pid = current->pid;
  59. ia64_dump_cpu_regs(dst);
  60. cfm = dst[43];
  61. sol = (cfm >> 7) & 0x7f;
  62. sof = cfm & 0x7f;
  63. dst[46] = (unsigned long)ia64_rse_skip_regs((unsigned long *)dst[46],
  64. sof - sol);
  65. buf = (u64 *) per_cpu_ptr(crash_notes, cpu);
  66. if (!buf)
  67. return;
  68. buf = append_elf_note(buf, KEXEC_CORE_NOTE_NAME, NT_PRSTATUS, prstatus,
  69. sizeof(*prstatus));
  70. final_note(buf);
  71. }
  72. #ifdef CONFIG_SMP
  73. static int
  74. kdump_wait_cpu_freeze(void)
  75. {
  76. int cpu_num = num_online_cpus() - 1;
  77. int timeout = 1000;
  78. while(timeout-- > 0) {
  79. if (atomic_read(&kdump_cpu_frozen) == cpu_num)
  80. return 0;
  81. udelay(1000);
  82. }
  83. return 1;
  84. }
  85. #endif
  86. void
  87. machine_crash_shutdown(struct pt_regs *pt)
  88. {
  89. /* This function is only called after the system
  90. * has paniced or is otherwise in a critical state.
  91. * The minimum amount of code to allow a kexec'd kernel
  92. * to run successfully needs to happen here.
  93. *
  94. * In practice this means shooting down the other cpus in
  95. * an SMP system.
  96. */
  97. kexec_disable_iosapic();
  98. #ifdef CONFIG_SMP
  99. /*
  100. * If kdump_on_init is set and an INIT is asserted here, kdump will
  101. * be started again via INIT monarch.
  102. */
  103. local_irq_disable();
  104. ia64_set_psr_mc(); /* mask MCA/INIT */
  105. if (atomic_inc_return(&kdump_in_progress) != 1)
  106. unw_init_running(kdump_cpu_freeze, NULL);
  107. /*
  108. * Now this cpu is ready for kdump.
  109. * Stop all others by IPI or INIT. They could receive INIT from
  110. * outside and might be INIT monarch, but only thing they have to
  111. * do is falling into kdump_cpu_freeze().
  112. *
  113. * If an INIT is asserted here:
  114. * - All receivers might be slaves, since some of cpus could already
  115. * be frozen and INIT might be masked on monarch. In this case,
  116. * all slaves will be frozen soon since kdump_in_progress will let
  117. * them into DIE_INIT_SLAVE_LEAVE.
  118. * - One might be a monarch, but INIT rendezvous will fail since
  119. * at least this cpu already have INIT masked so it never join
  120. * to the rendezvous. In this case, all slaves and monarch will
  121. * be frozen soon with no wait since the INIT rendezvous is skipped
  122. * by kdump_in_progress.
  123. */
  124. kdump_smp_send_stop();
  125. /* not all cpu response to IPI, send INIT to freeze them */
  126. if (kdump_wait_cpu_freeze()) {
  127. kdump_smp_send_init();
  128. /* wait again, don't go ahead if possible */
  129. kdump_wait_cpu_freeze();
  130. }
  131. #endif
  132. }
  133. static void
  134. machine_kdump_on_init(void)
  135. {
  136. crash_save_vmcoreinfo();
  137. local_irq_disable();
  138. kexec_disable_iosapic();
  139. machine_kexec(ia64_kimage);
  140. }
  141. void
  142. kdump_cpu_freeze(struct unw_frame_info *info, void *arg)
  143. {
  144. int cpuid;
  145. local_irq_disable();
  146. cpuid = smp_processor_id();
  147. crash_save_this_cpu();
  148. current->thread.ksp = (__u64)info->sw - 16;
  149. ia64_set_psr_mc(); /* mask MCA/INIT and stop reentrance */
  150. atomic_inc(&kdump_cpu_frozen);
  151. kdump_status[cpuid] = 1;
  152. mb();
  153. for (;;)
  154. cpu_relax();
  155. }
  156. static int
  157. kdump_init_notifier(struct notifier_block *self, unsigned long val, void *data)
  158. {
  159. struct ia64_mca_notify_die *nd;
  160. struct die_args *args = data;
  161. if (atomic_read(&kdump_in_progress)) {
  162. switch (val) {
  163. case DIE_INIT_MONARCH_LEAVE:
  164. if (!kdump_freeze_monarch)
  165. break;
  166. /* fall through */
  167. case DIE_INIT_SLAVE_LEAVE:
  168. case DIE_INIT_MONARCH_ENTER:
  169. case DIE_MCA_RENDZVOUS_LEAVE:
  170. unw_init_running(kdump_cpu_freeze, NULL);
  171. break;
  172. }
  173. }
  174. if (!kdump_on_init && !kdump_on_fatal_mca)
  175. return NOTIFY_DONE;
  176. if (!ia64_kimage) {
  177. if (val == DIE_INIT_MONARCH_LEAVE)
  178. ia64_mca_printk(KERN_NOTICE
  179. "%s: kdump not configured\n",
  180. __func__);
  181. return NOTIFY_DONE;
  182. }
  183. if (val != DIE_INIT_MONARCH_LEAVE &&
  184. val != DIE_INIT_MONARCH_PROCESS &&
  185. val != DIE_MCA_MONARCH_LEAVE)
  186. return NOTIFY_DONE;
  187. nd = (struct ia64_mca_notify_die *)args->err;
  188. switch (val) {
  189. case DIE_INIT_MONARCH_PROCESS:
  190. /* Reason code 1 means machine check rendezvous*/
  191. if (kdump_on_init && (nd->sos->rv_rc != 1)) {
  192. if (atomic_inc_return(&kdump_in_progress) != 1)
  193. kdump_freeze_monarch = 1;
  194. }
  195. break;
  196. case DIE_INIT_MONARCH_LEAVE:
  197. /* Reason code 1 means machine check rendezvous*/
  198. if (kdump_on_init && (nd->sos->rv_rc != 1))
  199. machine_kdump_on_init();
  200. break;
  201. case DIE_MCA_MONARCH_LEAVE:
  202. /* *(nd->data) indicate if MCA is recoverable */
  203. if (kdump_on_fatal_mca && !(*(nd->data))) {
  204. if (atomic_inc_return(&kdump_in_progress) == 1)
  205. machine_kdump_on_init();
  206. /* We got fatal MCA while kdump!? No way!! */
  207. }
  208. break;
  209. }
  210. return NOTIFY_DONE;
  211. }
  212. #ifdef CONFIG_SYSCTL
  213. static struct ctl_table kdump_ctl_table[] = {
  214. {
  215. .procname = "kdump_on_init",
  216. .data = &kdump_on_init,
  217. .maxlen = sizeof(int),
  218. .mode = 0644,
  219. .proc_handler = proc_dointvec,
  220. },
  221. {
  222. .procname = "kdump_on_fatal_mca",
  223. .data = &kdump_on_fatal_mca,
  224. .maxlen = sizeof(int),
  225. .mode = 0644,
  226. .proc_handler = proc_dointvec,
  227. },
  228. { }
  229. };
  230. static struct ctl_table sys_table[] = {
  231. {
  232. .procname = "kernel",
  233. .mode = 0555,
  234. .child = kdump_ctl_table,
  235. },
  236. { }
  237. };
  238. #endif
  239. static int
  240. machine_crash_setup(void)
  241. {
  242. /* be notified before default_monarch_init_process */
  243. static struct notifier_block kdump_init_notifier_nb = {
  244. .notifier_call = kdump_init_notifier,
  245. .priority = 1,
  246. };
  247. int ret;
  248. if((ret = register_die_notifier(&kdump_init_notifier_nb)) != 0)
  249. return ret;
  250. #ifdef CONFIG_SYSCTL
  251. register_sysctl_table(sys_table);
  252. #endif
  253. return 0;
  254. }
  255. __initcall(machine_crash_setup);