crash.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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_on_init = 1;
  25. static int kdump_on_fatal_mca = 1;
  26. static inline Elf64_Word
  27. *append_elf_note(Elf64_Word *buf, char *name, unsigned type, void *data,
  28. size_t data_len)
  29. {
  30. struct elf_note *note = (struct elf_note *)buf;
  31. note->n_namesz = strlen(name) + 1;
  32. note->n_descsz = data_len;
  33. note->n_type = type;
  34. buf += (sizeof(*note) + 3)/4;
  35. memcpy(buf, name, note->n_namesz);
  36. buf += (note->n_namesz + 3)/4;
  37. memcpy(buf, data, data_len);
  38. buf += (data_len + 3)/4;
  39. return buf;
  40. }
  41. static void
  42. final_note(void *buf)
  43. {
  44. memset(buf, 0, sizeof(struct elf_note));
  45. }
  46. extern void ia64_dump_cpu_regs(void *);
  47. static DEFINE_PER_CPU(struct elf_prstatus, elf_prstatus);
  48. void
  49. crash_save_this_cpu(void)
  50. {
  51. void *buf;
  52. unsigned long cfm, sof, sol;
  53. int cpu = smp_processor_id();
  54. struct elf_prstatus *prstatus = &per_cpu(elf_prstatus, cpu);
  55. elf_greg_t *dst = (elf_greg_t *)&(prstatus->pr_reg);
  56. memset(prstatus, 0, sizeof(*prstatus));
  57. prstatus->pr_pid = current->pid;
  58. ia64_dump_cpu_regs(dst);
  59. cfm = dst[43];
  60. sol = (cfm >> 7) & 0x7f;
  61. sof = cfm & 0x7f;
  62. dst[46] = (unsigned long)ia64_rse_skip_regs((unsigned long *)dst[46],
  63. sof - sol);
  64. buf = (u64 *) per_cpu_ptr(crash_notes, cpu);
  65. if (!buf)
  66. return;
  67. buf = append_elf_note(buf, KEXEC_CORE_NOTE_NAME, NT_PRSTATUS, prstatus,
  68. sizeof(*prstatus));
  69. final_note(buf);
  70. }
  71. #ifdef CONFIG_SMP
  72. static int
  73. kdump_wait_cpu_freeze(void)
  74. {
  75. int cpu_num = num_online_cpus() - 1;
  76. int timeout = 1000;
  77. while(timeout-- > 0) {
  78. if (atomic_read(&kdump_cpu_frozen) == cpu_num)
  79. return 0;
  80. udelay(1000);
  81. }
  82. return 1;
  83. }
  84. #endif
  85. void
  86. machine_crash_shutdown(struct pt_regs *pt)
  87. {
  88. /* This function is only called after the system
  89. * has paniced or is otherwise in a critical state.
  90. * The minimum amount of code to allow a kexec'd kernel
  91. * to run successfully needs to happen here.
  92. *
  93. * In practice this means shooting down the other cpus in
  94. * an SMP system.
  95. */
  96. kexec_disable_iosapic();
  97. #ifdef CONFIG_SMP
  98. kdump_smp_send_stop();
  99. /* not all cpu response to IPI, send INIT to freeze them */
  100. if (kdump_wait_cpu_freeze() && kdump_on_init) {
  101. kdump_smp_send_init();
  102. }
  103. #endif
  104. }
  105. static void
  106. machine_kdump_on_init(void)
  107. {
  108. crash_save_vmcoreinfo();
  109. local_irq_disable();
  110. kexec_disable_iosapic();
  111. machine_kexec(ia64_kimage);
  112. }
  113. void
  114. kdump_cpu_freeze(struct unw_frame_info *info, void *arg)
  115. {
  116. int cpuid;
  117. local_irq_disable();
  118. cpuid = smp_processor_id();
  119. crash_save_this_cpu();
  120. current->thread.ksp = (__u64)info->sw - 16;
  121. atomic_inc(&kdump_cpu_frozen);
  122. kdump_status[cpuid] = 1;
  123. mb();
  124. #ifdef CONFIG_HOTPLUG_CPU
  125. if (cpuid != 0)
  126. ia64_jump_to_sal(&sal_boot_rendez_state[cpuid]);
  127. #endif
  128. for (;;)
  129. cpu_relax();
  130. }
  131. static int
  132. kdump_init_notifier(struct notifier_block *self, unsigned long val, void *data)
  133. {
  134. struct ia64_mca_notify_die *nd;
  135. struct die_args *args = data;
  136. if (!kdump_on_init && !kdump_on_fatal_mca)
  137. return NOTIFY_DONE;
  138. if (!ia64_kimage) {
  139. if (val == DIE_INIT_MONARCH_LEAVE)
  140. ia64_mca_printk(KERN_NOTICE
  141. "%s: kdump not configured\n",
  142. __func__);
  143. return NOTIFY_DONE;
  144. }
  145. if (val != DIE_INIT_MONARCH_LEAVE &&
  146. val != DIE_INIT_SLAVE_LEAVE &&
  147. val != DIE_INIT_MONARCH_PROCESS &&
  148. val != DIE_MCA_RENDZVOUS_LEAVE &&
  149. val != DIE_MCA_MONARCH_LEAVE)
  150. return NOTIFY_DONE;
  151. nd = (struct ia64_mca_notify_die *)args->err;
  152. /* Reason code 1 means machine check rendezvous*/
  153. if ((val == DIE_INIT_MONARCH_LEAVE || val == DIE_INIT_SLAVE_LEAVE
  154. || val == DIE_INIT_MONARCH_PROCESS) && nd->sos->rv_rc == 1)
  155. return NOTIFY_DONE;
  156. switch (val) {
  157. case DIE_INIT_MONARCH_PROCESS:
  158. if (kdump_on_init) {
  159. atomic_set(&kdump_in_progress, 1);
  160. *(nd->monarch_cpu) = -1;
  161. }
  162. break;
  163. case DIE_INIT_MONARCH_LEAVE:
  164. if (kdump_on_init)
  165. machine_kdump_on_init();
  166. break;
  167. case DIE_INIT_SLAVE_LEAVE:
  168. if (atomic_read(&kdump_in_progress))
  169. unw_init_running(kdump_cpu_freeze, NULL);
  170. break;
  171. case DIE_MCA_RENDZVOUS_LEAVE:
  172. if (atomic_read(&kdump_in_progress))
  173. unw_init_running(kdump_cpu_freeze, NULL);
  174. break;
  175. case DIE_MCA_MONARCH_LEAVE:
  176. /* *(nd->data) indicate if MCA is recoverable */
  177. if (kdump_on_fatal_mca && !(*(nd->data))) {
  178. atomic_set(&kdump_in_progress, 1);
  179. *(nd->monarch_cpu) = -1;
  180. machine_kdump_on_init();
  181. }
  182. break;
  183. }
  184. return NOTIFY_DONE;
  185. }
  186. #ifdef CONFIG_SYSCTL
  187. static ctl_table kdump_ctl_table[] = {
  188. {
  189. .ctl_name = CTL_UNNUMBERED,
  190. .procname = "kdump_on_init",
  191. .data = &kdump_on_init,
  192. .maxlen = sizeof(int),
  193. .mode = 0644,
  194. .proc_handler = &proc_dointvec,
  195. },
  196. {
  197. .ctl_name = CTL_UNNUMBERED,
  198. .procname = "kdump_on_fatal_mca",
  199. .data = &kdump_on_fatal_mca,
  200. .maxlen = sizeof(int),
  201. .mode = 0644,
  202. .proc_handler = &proc_dointvec,
  203. },
  204. { .ctl_name = 0 }
  205. };
  206. static ctl_table sys_table[] = {
  207. {
  208. .ctl_name = CTL_KERN,
  209. .procname = "kernel",
  210. .mode = 0555,
  211. .child = kdump_ctl_table,
  212. },
  213. { .ctl_name = 0 }
  214. };
  215. #endif
  216. static int
  217. machine_crash_setup(void)
  218. {
  219. /* be notified before default_monarch_init_process */
  220. static struct notifier_block kdump_init_notifier_nb = {
  221. .notifier_call = kdump_init_notifier,
  222. .priority = 1,
  223. };
  224. int ret;
  225. if((ret = register_die_notifier(&kdump_init_notifier_nb)) != 0)
  226. return ret;
  227. #ifdef CONFIG_SYSCTL
  228. register_sysctl_table(sys_table);
  229. #endif
  230. return 0;
  231. }
  232. __initcall(machine_crash_setup);