crash.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * arch/ia64/kernel/crash.c
  4. *
  5. * Architecture specific (ia64) functions for kexec based crash dumps.
  6. *
  7. * Created by: Khalid Aziz <khalid.aziz@hp.com>
  8. * Copyright (C) 2005 Hewlett-Packard Development Company, L.P.
  9. * Copyright (C) 2005 Intel Corp Zou Nan hai <nanhai.zou@intel.com>
  10. *
  11. */
  12. #include <linux/smp.h>
  13. #include <linux/delay.h>
  14. #include <linux/crash_dump.h>
  15. #include <linux/bootmem.h>
  16. #include <linux/kexec.h>
  17. #include <linux/elfcore.h>
  18. #include <linux/sysctl.h>
  19. #include <linux/init.h>
  20. #include <linux/kdebug.h>
  21. #include <asm/mca.h>
  22. int kdump_status[NR_CPUS];
  23. static atomic_t kdump_cpu_frozen;
  24. atomic_t kdump_in_progress;
  25. static int kdump_freeze_monarch;
  26. static int kdump_on_init = 1;
  27. static int kdump_on_fatal_mca = 1;
  28. extern void ia64_dump_cpu_regs(void *);
  29. static DEFINE_PER_CPU(struct elf_prstatus, elf_prstatus);
  30. void
  31. crash_save_this_cpu(void)
  32. {
  33. void *buf;
  34. unsigned long cfm, sof, sol;
  35. int cpu = smp_processor_id();
  36. struct elf_prstatus *prstatus = &per_cpu(elf_prstatus, cpu);
  37. elf_greg_t *dst = (elf_greg_t *)&(prstatus->pr_reg);
  38. memset(prstatus, 0, sizeof(*prstatus));
  39. prstatus->pr_pid = current->pid;
  40. ia64_dump_cpu_regs(dst);
  41. cfm = dst[43];
  42. sol = (cfm >> 7) & 0x7f;
  43. sof = cfm & 0x7f;
  44. dst[46] = (unsigned long)ia64_rse_skip_regs((unsigned long *)dst[46],
  45. sof - sol);
  46. buf = (u64 *) per_cpu_ptr(crash_notes, cpu);
  47. if (!buf)
  48. return;
  49. buf = append_elf_note(buf, KEXEC_CORE_NOTE_NAME, NT_PRSTATUS, prstatus,
  50. sizeof(*prstatus));
  51. final_note(buf);
  52. }
  53. #ifdef CONFIG_SMP
  54. static int
  55. kdump_wait_cpu_freeze(void)
  56. {
  57. int cpu_num = num_online_cpus() - 1;
  58. int timeout = 1000;
  59. while(timeout-- > 0) {
  60. if (atomic_read(&kdump_cpu_frozen) == cpu_num)
  61. return 0;
  62. udelay(1000);
  63. }
  64. return 1;
  65. }
  66. #endif
  67. void
  68. machine_crash_shutdown(struct pt_regs *pt)
  69. {
  70. /* This function is only called after the system
  71. * has paniced or is otherwise in a critical state.
  72. * The minimum amount of code to allow a kexec'd kernel
  73. * to run successfully needs to happen here.
  74. *
  75. * In practice this means shooting down the other cpus in
  76. * an SMP system.
  77. */
  78. kexec_disable_iosapic();
  79. #ifdef CONFIG_SMP
  80. /*
  81. * If kdump_on_init is set and an INIT is asserted here, kdump will
  82. * be started again via INIT monarch.
  83. */
  84. local_irq_disable();
  85. ia64_set_psr_mc(); /* mask MCA/INIT */
  86. if (atomic_inc_return(&kdump_in_progress) != 1)
  87. unw_init_running(kdump_cpu_freeze, NULL);
  88. /*
  89. * Now this cpu is ready for kdump.
  90. * Stop all others by IPI or INIT. They could receive INIT from
  91. * outside and might be INIT monarch, but only thing they have to
  92. * do is falling into kdump_cpu_freeze().
  93. *
  94. * If an INIT is asserted here:
  95. * - All receivers might be slaves, since some of cpus could already
  96. * be frozen and INIT might be masked on monarch. In this case,
  97. * all slaves will be frozen soon since kdump_in_progress will let
  98. * them into DIE_INIT_SLAVE_LEAVE.
  99. * - One might be a monarch, but INIT rendezvous will fail since
  100. * at least this cpu already have INIT masked so it never join
  101. * to the rendezvous. In this case, all slaves and monarch will
  102. * be frozen soon with no wait since the INIT rendezvous is skipped
  103. * by kdump_in_progress.
  104. */
  105. kdump_smp_send_stop();
  106. /* not all cpu response to IPI, send INIT to freeze them */
  107. if (kdump_wait_cpu_freeze()) {
  108. kdump_smp_send_init();
  109. /* wait again, don't go ahead if possible */
  110. kdump_wait_cpu_freeze();
  111. }
  112. #endif
  113. }
  114. static void
  115. machine_kdump_on_init(void)
  116. {
  117. crash_save_vmcoreinfo();
  118. local_irq_disable();
  119. kexec_disable_iosapic();
  120. machine_kexec(ia64_kimage);
  121. }
  122. void
  123. kdump_cpu_freeze(struct unw_frame_info *info, void *arg)
  124. {
  125. int cpuid;
  126. local_irq_disable();
  127. cpuid = smp_processor_id();
  128. crash_save_this_cpu();
  129. current->thread.ksp = (__u64)info->sw - 16;
  130. ia64_set_psr_mc(); /* mask MCA/INIT and stop reentrance */
  131. atomic_inc(&kdump_cpu_frozen);
  132. kdump_status[cpuid] = 1;
  133. mb();
  134. for (;;)
  135. cpu_relax();
  136. }
  137. static int
  138. kdump_init_notifier(struct notifier_block *self, unsigned long val, void *data)
  139. {
  140. struct ia64_mca_notify_die *nd;
  141. struct die_args *args = data;
  142. if (atomic_read(&kdump_in_progress)) {
  143. switch (val) {
  144. case DIE_INIT_MONARCH_LEAVE:
  145. if (!kdump_freeze_monarch)
  146. break;
  147. /* fall through */
  148. case DIE_INIT_SLAVE_LEAVE:
  149. case DIE_INIT_MONARCH_ENTER:
  150. case DIE_MCA_RENDZVOUS_LEAVE:
  151. unw_init_running(kdump_cpu_freeze, NULL);
  152. break;
  153. }
  154. }
  155. if (!kdump_on_init && !kdump_on_fatal_mca)
  156. return NOTIFY_DONE;
  157. if (!ia64_kimage) {
  158. if (val == DIE_INIT_MONARCH_LEAVE)
  159. ia64_mca_printk(KERN_NOTICE
  160. "%s: kdump not configured\n",
  161. __func__);
  162. return NOTIFY_DONE;
  163. }
  164. if (val != DIE_INIT_MONARCH_LEAVE &&
  165. val != DIE_INIT_MONARCH_PROCESS &&
  166. val != DIE_MCA_MONARCH_LEAVE)
  167. return NOTIFY_DONE;
  168. nd = (struct ia64_mca_notify_die *)args->err;
  169. switch (val) {
  170. case DIE_INIT_MONARCH_PROCESS:
  171. /* Reason code 1 means machine check rendezvous*/
  172. if (kdump_on_init && (nd->sos->rv_rc != 1)) {
  173. if (atomic_inc_return(&kdump_in_progress) != 1)
  174. kdump_freeze_monarch = 1;
  175. }
  176. break;
  177. case DIE_INIT_MONARCH_LEAVE:
  178. /* Reason code 1 means machine check rendezvous*/
  179. if (kdump_on_init && (nd->sos->rv_rc != 1))
  180. machine_kdump_on_init();
  181. break;
  182. case DIE_MCA_MONARCH_LEAVE:
  183. /* *(nd->data) indicate if MCA is recoverable */
  184. if (kdump_on_fatal_mca && !(*(nd->data))) {
  185. if (atomic_inc_return(&kdump_in_progress) == 1)
  186. machine_kdump_on_init();
  187. /* We got fatal MCA while kdump!? No way!! */
  188. }
  189. break;
  190. }
  191. return NOTIFY_DONE;
  192. }
  193. #ifdef CONFIG_SYSCTL
  194. static struct ctl_table kdump_ctl_table[] = {
  195. {
  196. .procname = "kdump_on_init",
  197. .data = &kdump_on_init,
  198. .maxlen = sizeof(int),
  199. .mode = 0644,
  200. .proc_handler = proc_dointvec,
  201. },
  202. {
  203. .procname = "kdump_on_fatal_mca",
  204. .data = &kdump_on_fatal_mca,
  205. .maxlen = sizeof(int),
  206. .mode = 0644,
  207. .proc_handler = proc_dointvec,
  208. },
  209. { }
  210. };
  211. static struct ctl_table sys_table[] = {
  212. {
  213. .procname = "kernel",
  214. .mode = 0555,
  215. .child = kdump_ctl_table,
  216. },
  217. { }
  218. };
  219. #endif
  220. static int
  221. machine_crash_setup(void)
  222. {
  223. /* be notified before default_monarch_init_process */
  224. static struct notifier_block kdump_init_notifier_nb = {
  225. .notifier_call = kdump_init_notifier,
  226. .priority = 1,
  227. };
  228. int ret;
  229. if((ret = register_die_notifier(&kdump_init_notifier_nb)) != 0)
  230. return ret;
  231. #ifdef CONFIG_SYSCTL
  232. register_sysctl_table(sys_table);
  233. #endif
  234. return 0;
  235. }
  236. __initcall(machine_crash_setup);