crash.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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_ENTER &&
  142. val != DIE_INIT_SLAVE_ENTER &&
  143. val != DIE_MCA_RENDZVOUS_LEAVE &&
  144. val != DIE_MCA_MONARCH_LEAVE)
  145. return NOTIFY_DONE;
  146. nd = (struct ia64_mca_notify_die *)args->err;
  147. /* Reason code 1 means machine check rendezous*/
  148. if ((val == DIE_INIT_MONARCH_ENTER || val == DIE_INIT_SLAVE_ENTER) &&
  149. nd->sos->rv_rc == 1)
  150. return NOTIFY_DONE;
  151. switch (val) {
  152. case DIE_INIT_MONARCH_ENTER:
  153. machine_kdump_on_init();
  154. break;
  155. case DIE_INIT_SLAVE_ENTER:
  156. unw_init_running(kdump_cpu_freeze, NULL);
  157. break;
  158. case DIE_MCA_RENDZVOUS_LEAVE:
  159. if (atomic_read(&kdump_in_progress))
  160. unw_init_running(kdump_cpu_freeze, NULL);
  161. break;
  162. case DIE_MCA_MONARCH_LEAVE:
  163. /* die_register->signr indicate if MCA is recoverable */
  164. if (!args->signr)
  165. machine_kdump_on_init();
  166. break;
  167. }
  168. return NOTIFY_DONE;
  169. }
  170. #ifdef CONFIG_SYSCTL
  171. static ctl_table kdump_on_init_table[] = {
  172. {
  173. .ctl_name = CTL_UNNUMBERED,
  174. .procname = "kdump_on_init",
  175. .data = &kdump_on_init,
  176. .maxlen = sizeof(int),
  177. .mode = 0644,
  178. .proc_handler = &proc_dointvec,
  179. },
  180. { .ctl_name = 0 }
  181. };
  182. static ctl_table sys_table[] = {
  183. {
  184. .ctl_name = CTL_KERN,
  185. .procname = "kernel",
  186. .mode = 0555,
  187. .child = kdump_on_init_table,
  188. },
  189. { .ctl_name = 0 }
  190. };
  191. #endif
  192. static int
  193. machine_crash_setup(void)
  194. {
  195. static struct notifier_block kdump_init_notifier_nb = {
  196. .notifier_call = kdump_init_notifier,
  197. };
  198. int ret;
  199. if((ret = register_die_notifier(&kdump_init_notifier_nb)) != 0)
  200. return ret;
  201. #ifdef CONFIG_SYSCTL
  202. register_sysctl_table(sys_table);
  203. #endif
  204. return 0;
  205. }
  206. __initcall(machine_crash_setup);