nmi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * Machine check handler
  3. *
  4. * Copyright IBM Corp. 2000, 2009
  5. * Author(s): Ingo Adlung <adlung@de.ibm.com>,
  6. * Martin Schwidefsky <schwidefsky@de.ibm.com>,
  7. * Cornelia Huck <cornelia.huck@de.ibm.com>,
  8. * Heiko Carstens <heiko.carstens@de.ibm.com>,
  9. */
  10. #include <linux/kernel_stat.h>
  11. #include <linux/init.h>
  12. #include <linux/errno.h>
  13. #include <linux/hardirq.h>
  14. #include <linux/time.h>
  15. #include <linux/module.h>
  16. #include <asm/lowcore.h>
  17. #include <asm/smp.h>
  18. #include <asm/etr.h>
  19. #include <asm/cputime.h>
  20. #include <asm/nmi.h>
  21. #include <asm/crw.h>
  22. #include <asm/switch_to.h>
  23. struct mcck_struct {
  24. int kill_task;
  25. int channel_report;
  26. int warning;
  27. unsigned long long mcck_code;
  28. };
  29. static DEFINE_PER_CPU(struct mcck_struct, cpu_mcck);
  30. static void s390_handle_damage(char *msg)
  31. {
  32. smp_send_stop();
  33. disabled_wait((unsigned long) __builtin_return_address(0));
  34. while (1);
  35. }
  36. /*
  37. * Main machine check handler function. Will be called with interrupts enabled
  38. * or disabled and machine checks enabled or disabled.
  39. */
  40. void s390_handle_mcck(void)
  41. {
  42. unsigned long flags;
  43. struct mcck_struct mcck;
  44. /*
  45. * Disable machine checks and get the current state of accumulated
  46. * machine checks. Afterwards delete the old state and enable machine
  47. * checks again.
  48. */
  49. local_irq_save(flags);
  50. local_mcck_disable();
  51. /*
  52. * Ummm... Does this make sense at all? Copying the percpu struct
  53. * and then zapping it one statement later?
  54. */
  55. memcpy(&mcck, this_cpu_ptr(&cpu_mcck), sizeof(mcck));
  56. memset(&mcck, 0, sizeof(struct mcck_struct));
  57. clear_cpu_flag(CIF_MCCK_PENDING);
  58. local_mcck_enable();
  59. local_irq_restore(flags);
  60. if (mcck.channel_report)
  61. crw_handle_channel_report();
  62. /*
  63. * A warning may remain for a prolonged period on the bare iron.
  64. * (actually until the machine is powered off, or the problem is gone)
  65. * So we just stop listening for the WARNING MCH and avoid continuously
  66. * being interrupted. One caveat is however, that we must do this per
  67. * processor and cannot use the smp version of ctl_clear_bit().
  68. * On VM we only get one interrupt per virtally presented machinecheck.
  69. * Though one suffices, we may get one interrupt per (virtual) cpu.
  70. */
  71. if (mcck.warning) { /* WARNING pending ? */
  72. static int mchchk_wng_posted = 0;
  73. /* Use single cpu clear, as we cannot handle smp here. */
  74. __ctl_clear_bit(14, 24); /* Disable WARNING MCH */
  75. if (xchg(&mchchk_wng_posted, 1) == 0)
  76. kill_cad_pid(SIGPWR, 1);
  77. }
  78. if (mcck.kill_task) {
  79. local_irq_enable();
  80. printk(KERN_EMERG "mcck: Terminating task because of machine "
  81. "malfunction (code 0x%016llx).\n", mcck.mcck_code);
  82. printk(KERN_EMERG "mcck: task: %s, pid: %d.\n",
  83. current->comm, current->pid);
  84. do_exit(SIGSEGV);
  85. }
  86. }
  87. EXPORT_SYMBOL_GPL(s390_handle_mcck);
  88. /*
  89. * returns 0 if all registers could be validated
  90. * returns 1 otherwise
  91. */
  92. static int notrace s390_revalidate_registers(struct mci *mci)
  93. {
  94. int kill_task;
  95. u64 zero;
  96. void *fpt_save_area, *fpt_creg_save_area;
  97. kill_task = 0;
  98. zero = 0;
  99. if (!mci->gr) {
  100. /*
  101. * General purpose registers couldn't be restored and have
  102. * unknown contents. Process needs to be terminated.
  103. */
  104. kill_task = 1;
  105. }
  106. if (!mci->fp) {
  107. /*
  108. * Floating point registers can't be restored and
  109. * therefore the process needs to be terminated.
  110. */
  111. kill_task = 1;
  112. }
  113. #ifndef CONFIG_64BIT
  114. asm volatile(
  115. " ld 0,0(%0)\n"
  116. " ld 2,8(%0)\n"
  117. " ld 4,16(%0)\n"
  118. " ld 6,24(%0)"
  119. : : "a" (&S390_lowcore.floating_pt_save_area));
  120. #endif
  121. if (MACHINE_HAS_IEEE) {
  122. #ifdef CONFIG_64BIT
  123. fpt_save_area = &S390_lowcore.floating_pt_save_area;
  124. fpt_creg_save_area = &S390_lowcore.fpt_creg_save_area;
  125. #else
  126. fpt_save_area = (void *) S390_lowcore.extended_save_area_addr;
  127. fpt_creg_save_area = fpt_save_area + 128;
  128. #endif
  129. if (!mci->fc) {
  130. /*
  131. * Floating point control register can't be restored.
  132. * Task will be terminated.
  133. */
  134. asm volatile("lfpc 0(%0)" : : "a" (&zero), "m" (zero));
  135. kill_task = 1;
  136. } else
  137. asm volatile("lfpc 0(%0)" : : "a" (fpt_creg_save_area));
  138. asm volatile(
  139. " ld 0,0(%0)\n"
  140. " ld 1,8(%0)\n"
  141. " ld 2,16(%0)\n"
  142. " ld 3,24(%0)\n"
  143. " ld 4,32(%0)\n"
  144. " ld 5,40(%0)\n"
  145. " ld 6,48(%0)\n"
  146. " ld 7,56(%0)\n"
  147. " ld 8,64(%0)\n"
  148. " ld 9,72(%0)\n"
  149. " ld 10,80(%0)\n"
  150. " ld 11,88(%0)\n"
  151. " ld 12,96(%0)\n"
  152. " ld 13,104(%0)\n"
  153. " ld 14,112(%0)\n"
  154. " ld 15,120(%0)\n"
  155. : : "a" (fpt_save_area));
  156. }
  157. #ifdef CONFIG_64BIT
  158. /* Revalidate vector registers */
  159. if (MACHINE_HAS_VX && current->thread.vxrs) {
  160. if (!mci->vr) {
  161. /*
  162. * Vector registers can't be restored and therefore
  163. * the process needs to be terminated.
  164. */
  165. kill_task = 1;
  166. }
  167. restore_vx_regs((__vector128 *)
  168. S390_lowcore.vector_save_area_addr);
  169. }
  170. #endif
  171. /* Revalidate access registers */
  172. asm volatile(
  173. " lam 0,15,0(%0)"
  174. : : "a" (&S390_lowcore.access_regs_save_area));
  175. if (!mci->ar) {
  176. /*
  177. * Access registers have unknown contents.
  178. * Terminating task.
  179. */
  180. kill_task = 1;
  181. }
  182. /* Revalidate control registers */
  183. if (!mci->cr) {
  184. /*
  185. * Control registers have unknown contents.
  186. * Can't recover and therefore stopping machine.
  187. */
  188. s390_handle_damage("invalid control registers.");
  189. } else {
  190. #ifdef CONFIG_64BIT
  191. asm volatile(
  192. " lctlg 0,15,0(%0)"
  193. : : "a" (&S390_lowcore.cregs_save_area));
  194. #else
  195. asm volatile(
  196. " lctl 0,15,0(%0)"
  197. : : "a" (&S390_lowcore.cregs_save_area));
  198. #endif
  199. }
  200. /*
  201. * We don't even try to revalidate the TOD register, since we simply
  202. * can't write something sensible into that register.
  203. */
  204. #ifdef CONFIG_64BIT
  205. /*
  206. * See if we can revalidate the TOD programmable register with its
  207. * old contents (should be zero) otherwise set it to zero.
  208. */
  209. if (!mci->pr)
  210. asm volatile(
  211. " sr 0,0\n"
  212. " sckpf"
  213. : : : "0", "cc");
  214. else
  215. asm volatile(
  216. " l 0,0(%0)\n"
  217. " sckpf"
  218. : : "a" (&S390_lowcore.tod_progreg_save_area)
  219. : "0", "cc");
  220. #endif
  221. /* Revalidate clock comparator register */
  222. set_clock_comparator(S390_lowcore.clock_comparator);
  223. /* Check if old PSW is valid */
  224. if (!mci->wp)
  225. /*
  226. * Can't tell if we come from user or kernel mode
  227. * -> stopping machine.
  228. */
  229. s390_handle_damage("old psw invalid.");
  230. if (!mci->ms || !mci->pm || !mci->ia)
  231. kill_task = 1;
  232. return kill_task;
  233. }
  234. #define MAX_IPD_COUNT 29
  235. #define MAX_IPD_TIME (5 * 60 * USEC_PER_SEC) /* 5 minutes */
  236. #define ED_STP_ISLAND 6 /* External damage STP island check */
  237. #define ED_STP_SYNC 7 /* External damage STP sync check */
  238. #define ED_ETR_SYNC 12 /* External damage ETR sync check */
  239. #define ED_ETR_SWITCH 13 /* External damage ETR switch to local */
  240. /*
  241. * machine check handler.
  242. */
  243. void notrace s390_do_machine_check(struct pt_regs *regs)
  244. {
  245. static int ipd_count;
  246. static DEFINE_SPINLOCK(ipd_lock);
  247. static unsigned long long last_ipd;
  248. struct mcck_struct *mcck;
  249. unsigned long long tmp;
  250. struct mci *mci;
  251. int umode;
  252. nmi_enter();
  253. inc_irq_stat(NMI_NMI);
  254. mci = (struct mci *) &S390_lowcore.mcck_interruption_code;
  255. mcck = this_cpu_ptr(&cpu_mcck);
  256. umode = user_mode(regs);
  257. if (mci->sd) {
  258. /* System damage -> stopping machine */
  259. s390_handle_damage("received system damage machine check.");
  260. }
  261. if (mci->pd) {
  262. if (mci->b) {
  263. /* Processing backup -> verify if we can survive this */
  264. u64 z_mcic, o_mcic, t_mcic;
  265. #ifdef CONFIG_64BIT
  266. z_mcic = (1ULL<<63 | 1ULL<<59 | 1ULL<<29);
  267. o_mcic = (1ULL<<43 | 1ULL<<42 | 1ULL<<41 | 1ULL<<40 |
  268. 1ULL<<36 | 1ULL<<35 | 1ULL<<34 | 1ULL<<32 |
  269. 1ULL<<30 | 1ULL<<21 | 1ULL<<20 | 1ULL<<17 |
  270. 1ULL<<16);
  271. #else
  272. z_mcic = (1ULL<<63 | 1ULL<<59 | 1ULL<<57 | 1ULL<<50 |
  273. 1ULL<<29);
  274. o_mcic = (1ULL<<43 | 1ULL<<42 | 1ULL<<41 | 1ULL<<40 |
  275. 1ULL<<36 | 1ULL<<35 | 1ULL<<34 | 1ULL<<32 |
  276. 1ULL<<30 | 1ULL<<20 | 1ULL<<17 | 1ULL<<16);
  277. #endif
  278. t_mcic = *(u64 *)mci;
  279. if (((t_mcic & z_mcic) != 0) ||
  280. ((t_mcic & o_mcic) != o_mcic)) {
  281. s390_handle_damage("processing backup machine "
  282. "check with damage.");
  283. }
  284. /*
  285. * Nullifying exigent condition, therefore we might
  286. * retry this instruction.
  287. */
  288. spin_lock(&ipd_lock);
  289. tmp = get_tod_clock();
  290. if (((tmp - last_ipd) >> 12) < MAX_IPD_TIME)
  291. ipd_count++;
  292. else
  293. ipd_count = 1;
  294. last_ipd = tmp;
  295. if (ipd_count == MAX_IPD_COUNT)
  296. s390_handle_damage("too many ipd retries.");
  297. spin_unlock(&ipd_lock);
  298. } else {
  299. /* Processing damage -> stopping machine */
  300. s390_handle_damage("received instruction processing "
  301. "damage machine check.");
  302. }
  303. }
  304. if (s390_revalidate_registers(mci)) {
  305. if (umode) {
  306. /*
  307. * Couldn't restore all register contents while in
  308. * user mode -> mark task for termination.
  309. */
  310. mcck->kill_task = 1;
  311. mcck->mcck_code = *(unsigned long long *) mci;
  312. set_cpu_flag(CIF_MCCK_PENDING);
  313. } else {
  314. /*
  315. * Couldn't restore all register contents while in
  316. * kernel mode -> stopping machine.
  317. */
  318. s390_handle_damage("unable to revalidate registers.");
  319. }
  320. }
  321. if (mci->cd) {
  322. /* Timing facility damage */
  323. s390_handle_damage("TOD clock damaged");
  324. }
  325. if (mci->ed && mci->ec) {
  326. /* External damage */
  327. if (S390_lowcore.external_damage_code & (1U << ED_ETR_SYNC))
  328. etr_sync_check();
  329. if (S390_lowcore.external_damage_code & (1U << ED_ETR_SWITCH))
  330. etr_switch_to_local();
  331. if (S390_lowcore.external_damage_code & (1U << ED_STP_SYNC))
  332. stp_sync_check();
  333. if (S390_lowcore.external_damage_code & (1U << ED_STP_ISLAND))
  334. stp_island_check();
  335. }
  336. if (mci->se)
  337. /* Storage error uncorrected */
  338. s390_handle_damage("received storage error uncorrected "
  339. "machine check.");
  340. if (mci->ke)
  341. /* Storage key-error uncorrected */
  342. s390_handle_damage("received storage key-error uncorrected "
  343. "machine check.");
  344. if (mci->ds && mci->fa)
  345. /* Storage degradation */
  346. s390_handle_damage("received storage degradation machine "
  347. "check.");
  348. if (mci->cp) {
  349. /* Channel report word pending */
  350. mcck->channel_report = 1;
  351. set_cpu_flag(CIF_MCCK_PENDING);
  352. }
  353. if (mci->w) {
  354. /* Warning pending */
  355. mcck->warning = 1;
  356. set_cpu_flag(CIF_MCCK_PENDING);
  357. }
  358. nmi_exit();
  359. }
  360. static int __init machine_check_init(void)
  361. {
  362. ctl_set_bit(14, 25); /* enable external damage MCH */
  363. ctl_set_bit(14, 27); /* enable system recovery MCH */
  364. ctl_set_bit(14, 24); /* enable warning MCH */
  365. return 0;
  366. }
  367. arch_initcall(machine_check_init);