nmi.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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/stp.h>
  19. #include <asm/cputime.h>
  20. #include <asm/nmi.h>
  21. #include <asm/crw.h>
  22. #include <asm/switch_to.h>
  23. #include <asm/ctl_reg.h>
  24. struct mcck_struct {
  25. unsigned int kill_task : 1;
  26. unsigned int channel_report : 1;
  27. unsigned int warning : 1;
  28. unsigned int stp_queue : 1;
  29. unsigned long mcck_code;
  30. };
  31. static DEFINE_PER_CPU(struct mcck_struct, cpu_mcck);
  32. static void s390_handle_damage(void)
  33. {
  34. smp_send_stop();
  35. disabled_wait((unsigned long) __builtin_return_address(0));
  36. while (1);
  37. }
  38. /*
  39. * Main machine check handler function. Will be called with interrupts enabled
  40. * or disabled and machine checks enabled or disabled.
  41. */
  42. void s390_handle_mcck(void)
  43. {
  44. unsigned long flags;
  45. struct mcck_struct mcck;
  46. /*
  47. * Disable machine checks and get the current state of accumulated
  48. * machine checks. Afterwards delete the old state and enable machine
  49. * checks again.
  50. */
  51. local_irq_save(flags);
  52. local_mcck_disable();
  53. mcck = *this_cpu_ptr(&cpu_mcck);
  54. memset(this_cpu_ptr(&cpu_mcck), 0, sizeof(mcck));
  55. clear_cpu_flag(CIF_MCCK_PENDING);
  56. local_mcck_enable();
  57. local_irq_restore(flags);
  58. if (mcck.channel_report)
  59. crw_handle_channel_report();
  60. /*
  61. * A warning may remain for a prolonged period on the bare iron.
  62. * (actually until the machine is powered off, or the problem is gone)
  63. * So we just stop listening for the WARNING MCH and avoid continuously
  64. * being interrupted. One caveat is however, that we must do this per
  65. * processor and cannot use the smp version of ctl_clear_bit().
  66. * On VM we only get one interrupt per virtally presented machinecheck.
  67. * Though one suffices, we may get one interrupt per (virtual) cpu.
  68. */
  69. if (mcck.warning) { /* WARNING pending ? */
  70. static int mchchk_wng_posted = 0;
  71. /* Use single cpu clear, as we cannot handle smp here. */
  72. __ctl_clear_bit(14, 24); /* Disable WARNING MCH */
  73. if (xchg(&mchchk_wng_posted, 1) == 0)
  74. kill_cad_pid(SIGPWR, 1);
  75. }
  76. if (mcck.stp_queue)
  77. stp_queue_work();
  78. if (mcck.kill_task) {
  79. local_irq_enable();
  80. printk(KERN_EMERG "mcck: Terminating task because of machine "
  81. "malfunction (code 0x%016lx).\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_validate_registers(union mci mci, int umode)
  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. Stop system or terminate process.
  103. */
  104. if (!umode)
  105. s390_handle_damage();
  106. kill_task = 1;
  107. }
  108. if (!mci.fp) {
  109. /*
  110. * Floating point registers can't be restored. If the
  111. * kernel currently uses floating point registers the
  112. * system is stopped. If the process has its floating
  113. * pointer registers loaded it is terminated.
  114. * Otherwise just revalidate the registers.
  115. */
  116. if (S390_lowcore.fpu_flags & KERNEL_VXR_V0V7)
  117. s390_handle_damage();
  118. if (!test_cpu_flag(CIF_FPU))
  119. kill_task = 1;
  120. }
  121. fpt_save_area = &S390_lowcore.floating_pt_save_area;
  122. fpt_creg_save_area = &S390_lowcore.fpt_creg_save_area;
  123. if (!mci.fc) {
  124. /*
  125. * Floating point control register can't be restored.
  126. * If the kernel currently uses the floating pointer
  127. * registers and needs the FPC register the system is
  128. * stopped. If the process has its floating pointer
  129. * registers loaded it is terminated. Otherwiese the
  130. * FPC is just revalidated.
  131. */
  132. if (S390_lowcore.fpu_flags & KERNEL_FPC)
  133. s390_handle_damage();
  134. asm volatile("lfpc 0(%0)" : : "a" (&zero), "m" (zero));
  135. if (!test_cpu_flag(CIF_FPU))
  136. kill_task = 1;
  137. } else
  138. asm volatile("lfpc 0(%0)" : : "a" (fpt_creg_save_area));
  139. if (!MACHINE_HAS_VX) {
  140. /* Validate floating point registers */
  141. asm volatile(
  142. " ld 0,0(%0)\n"
  143. " ld 1,8(%0)\n"
  144. " ld 2,16(%0)\n"
  145. " ld 3,24(%0)\n"
  146. " ld 4,32(%0)\n"
  147. " ld 5,40(%0)\n"
  148. " ld 6,48(%0)\n"
  149. " ld 7,56(%0)\n"
  150. " ld 8,64(%0)\n"
  151. " ld 9,72(%0)\n"
  152. " ld 10,80(%0)\n"
  153. " ld 11,88(%0)\n"
  154. " ld 12,96(%0)\n"
  155. " ld 13,104(%0)\n"
  156. " ld 14,112(%0)\n"
  157. " ld 15,120(%0)\n"
  158. : : "a" (fpt_save_area));
  159. } else {
  160. /* Validate vector registers */
  161. union ctlreg0 cr0;
  162. if (!mci.vr) {
  163. /*
  164. * Vector registers can't be restored. If the kernel
  165. * currently uses vector registers the system is
  166. * stopped. If the process has its vector registers
  167. * loaded it is terminated. Otherwise just revalidate
  168. * the registers.
  169. */
  170. if (S390_lowcore.fpu_flags & KERNEL_VXR)
  171. s390_handle_damage();
  172. if (!test_cpu_flag(CIF_FPU))
  173. kill_task = 1;
  174. }
  175. cr0.val = S390_lowcore.cregs_save_area[0];
  176. cr0.afp = cr0.vx = 1;
  177. __ctl_load(cr0.val, 0, 0);
  178. asm volatile(
  179. " la 1,%0\n"
  180. " .word 0xe70f,0x1000,0x0036\n" /* vlm 0,15,0(1) */
  181. " .word 0xe70f,0x1100,0x0c36\n" /* vlm 16,31,256(1) */
  182. : : "Q" (*(struct vx_array *)
  183. &S390_lowcore.vector_save_area) : "1");
  184. __ctl_load(S390_lowcore.cregs_save_area[0], 0, 0);
  185. }
  186. /* Validate access registers */
  187. asm volatile(
  188. " lam 0,15,0(%0)"
  189. : : "a" (&S390_lowcore.access_regs_save_area));
  190. if (!mci.ar) {
  191. /*
  192. * Access registers have unknown contents.
  193. * Terminating task.
  194. */
  195. kill_task = 1;
  196. }
  197. /* Validate control registers */
  198. if (!mci.cr) {
  199. /*
  200. * Control registers have unknown contents.
  201. * Can't recover and therefore stopping machine.
  202. */
  203. s390_handle_damage();
  204. } else {
  205. asm volatile(
  206. " lctlg 0,15,0(%0)"
  207. : : "a" (&S390_lowcore.cregs_save_area));
  208. }
  209. /*
  210. * We don't even try to validate the TOD register, since we simply
  211. * can't write something sensible into that register.
  212. */
  213. /*
  214. * See if we can validate the TOD programmable register with its
  215. * old contents (should be zero) otherwise set it to zero.
  216. */
  217. if (!mci.pr)
  218. asm volatile(
  219. " sr 0,0\n"
  220. " sckpf"
  221. : : : "0", "cc");
  222. else
  223. asm volatile(
  224. " l 0,0(%0)\n"
  225. " sckpf"
  226. : : "a" (&S390_lowcore.tod_progreg_save_area)
  227. : "0", "cc");
  228. /* Validate clock comparator register */
  229. set_clock_comparator(S390_lowcore.clock_comparator);
  230. /* Check if old PSW is valid */
  231. if (!mci.wp)
  232. /*
  233. * Can't tell if we come from user or kernel mode
  234. * -> stopping machine.
  235. */
  236. s390_handle_damage();
  237. if (!mci.ms || !mci.pm || !mci.ia)
  238. kill_task = 1;
  239. return kill_task;
  240. }
  241. #define MAX_IPD_COUNT 29
  242. #define MAX_IPD_TIME (5 * 60 * USEC_PER_SEC) /* 5 minutes */
  243. #define ED_STP_ISLAND 6 /* External damage STP island check */
  244. #define ED_STP_SYNC 7 /* External damage STP sync check */
  245. /*
  246. * machine check handler.
  247. */
  248. void notrace s390_do_machine_check(struct pt_regs *regs)
  249. {
  250. static int ipd_count;
  251. static DEFINE_SPINLOCK(ipd_lock);
  252. static unsigned long long last_ipd;
  253. struct mcck_struct *mcck;
  254. unsigned long long tmp;
  255. union mci mci;
  256. nmi_enter();
  257. inc_irq_stat(NMI_NMI);
  258. mci.val = S390_lowcore.mcck_interruption_code;
  259. mcck = this_cpu_ptr(&cpu_mcck);
  260. if (mci.sd) {
  261. /* System damage -> stopping machine */
  262. s390_handle_damage();
  263. }
  264. if (mci.pd) {
  265. if (mci.b) {
  266. /* Processing backup -> verify if we can survive this */
  267. u64 z_mcic, o_mcic, t_mcic;
  268. z_mcic = (1ULL<<63 | 1ULL<<59 | 1ULL<<29);
  269. o_mcic = (1ULL<<43 | 1ULL<<42 | 1ULL<<41 | 1ULL<<40 |
  270. 1ULL<<36 | 1ULL<<35 | 1ULL<<34 | 1ULL<<32 |
  271. 1ULL<<30 | 1ULL<<21 | 1ULL<<20 | 1ULL<<17 |
  272. 1ULL<<16);
  273. t_mcic = mci.val;
  274. if (((t_mcic & z_mcic) != 0) ||
  275. ((t_mcic & o_mcic) != o_mcic)) {
  276. s390_handle_damage();
  277. }
  278. /*
  279. * Nullifying exigent condition, therefore we might
  280. * retry this instruction.
  281. */
  282. spin_lock(&ipd_lock);
  283. tmp = get_tod_clock();
  284. if (((tmp - last_ipd) >> 12) < MAX_IPD_TIME)
  285. ipd_count++;
  286. else
  287. ipd_count = 1;
  288. last_ipd = tmp;
  289. if (ipd_count == MAX_IPD_COUNT)
  290. s390_handle_damage();
  291. spin_unlock(&ipd_lock);
  292. } else {
  293. /* Processing damage -> stopping machine */
  294. s390_handle_damage();
  295. }
  296. }
  297. if (s390_validate_registers(mci, user_mode(regs))) {
  298. /*
  299. * Couldn't restore all register contents for the
  300. * user space process -> mark task for termination.
  301. */
  302. mcck->kill_task = 1;
  303. mcck->mcck_code = mci.val;
  304. set_cpu_flag(CIF_MCCK_PENDING);
  305. }
  306. if (mci.cd) {
  307. /* Timing facility damage */
  308. s390_handle_damage();
  309. }
  310. if (mci.ed && mci.ec) {
  311. /* External damage */
  312. if (S390_lowcore.external_damage_code & (1U << ED_STP_SYNC))
  313. mcck->stp_queue |= stp_sync_check();
  314. if (S390_lowcore.external_damage_code & (1U << ED_STP_ISLAND))
  315. mcck->stp_queue |= stp_island_check();
  316. if (mcck->stp_queue)
  317. set_cpu_flag(CIF_MCCK_PENDING);
  318. }
  319. if (mci.se)
  320. /* Storage error uncorrected */
  321. s390_handle_damage();
  322. if (mci.ke)
  323. /* Storage key-error uncorrected */
  324. s390_handle_damage();
  325. if (mci.ds && mci.fa)
  326. /* Storage degradation */
  327. s390_handle_damage();
  328. if (mci.cp) {
  329. /* Channel report word pending */
  330. mcck->channel_report = 1;
  331. set_cpu_flag(CIF_MCCK_PENDING);
  332. }
  333. if (mci.w) {
  334. /* Warning pending */
  335. mcck->warning = 1;
  336. set_cpu_flag(CIF_MCCK_PENDING);
  337. }
  338. nmi_exit();
  339. }
  340. static int __init machine_check_init(void)
  341. {
  342. ctl_set_bit(14, 25); /* enable external damage MCH */
  343. ctl_set_bit(14, 27); /* enable system recovery MCH */
  344. ctl_set_bit(14, 24); /* enable warning MCH */
  345. return 0;
  346. }
  347. early_initcall(machine_check_init);