crash.c 5.4 KB

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