crash.c 5.4 KB

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