crash.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 <asm/kdebug.h>
  20. #include <asm/mca.h>
  21. int kdump_status[NR_CPUS];
  22. atomic_t kdump_cpu_freezed;
  23. atomic_t kdump_in_progress;
  24. 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()
  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, "CORE", NT_PRSTATUS, prstatus,
  67. sizeof(*prstatus));
  68. final_note(buf);
  69. }
  70. static int
  71. kdump_wait_cpu_freeze(void)
  72. {
  73. int cpu_num = num_online_cpus() - 1;
  74. int timeout = 1000;
  75. while(timeout-- > 0) {
  76. if (atomic_read(&kdump_cpu_freezed) == cpu_num)
  77. return 0;
  78. udelay(1000);
  79. }
  80. return 1;
  81. }
  82. void
  83. machine_crash_shutdown(struct pt_regs *pt)
  84. {
  85. /* This function is only called after the system
  86. * has paniced or is otherwise in a critical state.
  87. * The minimum amount of code to allow a kexec'd kernel
  88. * to run successfully needs to happen here.
  89. *
  90. * In practice this means shooting down the other cpus in
  91. * an SMP system.
  92. */
  93. kexec_disable_iosapic();
  94. #ifdef CONFIG_SMP
  95. kdump_smp_send_stop();
  96. if (kdump_wait_cpu_freeze() && kdump_on_init) {
  97. //not all cpu response to IPI, send INIT to freeze them
  98. kdump_smp_send_init();
  99. }
  100. #endif
  101. }
  102. static void
  103. machine_kdump_on_init(void)
  104. {
  105. local_irq_disable();
  106. kexec_disable_iosapic();
  107. machine_kexec(ia64_kimage);
  108. }
  109. void
  110. kdump_cpu_freeze(struct unw_frame_info *info, void *arg)
  111. {
  112. int cpuid;
  113. local_irq_disable();
  114. cpuid = smp_processor_id();
  115. crash_save_this_cpu();
  116. current->thread.ksp = (__u64)info->sw - 16;
  117. atomic_inc(&kdump_cpu_freezed);
  118. kdump_status[cpuid] = 1;
  119. mb();
  120. if (cpuid == 0) {
  121. for (;;)
  122. cpu_relax();
  123. } else
  124. ia64_jump_to_sal(&sal_boot_rendez_state[cpuid]);
  125. }
  126. static int
  127. kdump_init_notifier(struct notifier_block *self, unsigned long val, void *data)
  128. {
  129. struct ia64_mca_notify_die *nd;
  130. struct die_args *args = data;
  131. if (!kdump_on_init)
  132. return NOTIFY_DONE;
  133. if (val != DIE_INIT_MONARCH_ENTER &&
  134. val != DIE_INIT_SLAVE_ENTER &&
  135. val != DIE_MCA_RENDZVOUS_LEAVE &&
  136. val != DIE_MCA_MONARCH_LEAVE)
  137. return NOTIFY_DONE;
  138. nd = (struct ia64_mca_notify_die *)args->err;
  139. /* Reason code 1 means machine check rendezous*/
  140. if ((val == DIE_INIT_MONARCH_ENTER || DIE_INIT_SLAVE_ENTER) &&
  141. nd->sos->rv_rc == 1)
  142. return NOTIFY_DONE;
  143. switch (val) {
  144. case DIE_INIT_MONARCH_ENTER:
  145. machine_kdump_on_init();
  146. break;
  147. case DIE_INIT_SLAVE_ENTER:
  148. unw_init_running(kdump_cpu_freeze, NULL);
  149. break;
  150. case DIE_MCA_RENDZVOUS_LEAVE:
  151. if (atomic_read(&kdump_in_progress))
  152. unw_init_running(kdump_cpu_freeze, NULL);
  153. break;
  154. case DIE_MCA_MONARCH_LEAVE:
  155. /* die_register->signr indicate if MCA is recoverable */
  156. if (!args->signr)
  157. machine_kdump_on_init();
  158. break;
  159. }
  160. return NOTIFY_DONE;
  161. }
  162. #ifdef CONFIG_SYSCTL
  163. static ctl_table kdump_on_init_table[] = {
  164. {
  165. .ctl_name = CTL_UNNUMBERED,
  166. .procname = "kdump_on_init",
  167. .data = &kdump_on_init,
  168. .maxlen = sizeof(int),
  169. .mode = 0644,
  170. .proc_handler = &proc_dointvec,
  171. },
  172. { .ctl_name = 0 }
  173. };
  174. static ctl_table sys_table[] = {
  175. {
  176. .ctl_name = CTL_KERN,
  177. .procname = "kernel",
  178. .mode = 0555,
  179. .child = kdump_on_init_table,
  180. },
  181. { .ctl_name = 0 }
  182. };
  183. #endif
  184. static int
  185. machine_crash_setup(void)
  186. {
  187. static struct notifier_block kdump_init_notifier_nb = {
  188. .notifier_call = kdump_init_notifier,
  189. };
  190. int ret;
  191. if((ret = register_die_notifier(&kdump_init_notifier_nb)) != 0)
  192. return ret;
  193. #ifdef CONFIG_SYSCTL
  194. register_sysctl_table(sys_table, 0);
  195. #endif
  196. return 0;
  197. }
  198. __initcall(machine_crash_setup);