s390mach.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /*
  2. * drivers/s390/s390mach.c
  3. * S/390 machine check handler
  4. *
  5. * Copyright IBM Corp. 2000,2008
  6. * Author(s): Ingo Adlung (adlung@de.ibm.com)
  7. * Martin Schwidefsky (schwidefsky@de.ibm.com)
  8. * Cornelia Huck <cornelia.huck@de.ibm.com>
  9. */
  10. #include <linux/init.h>
  11. #include <linux/sched.h>
  12. #include <linux/errno.h>
  13. #include <linux/workqueue.h>
  14. #include <linux/time.h>
  15. #include <linux/device.h>
  16. #include <linux/kthread.h>
  17. #include <asm/etr.h>
  18. #include <asm/lowcore.h>
  19. #include <asm/cio.h>
  20. #include "s390mach.h"
  21. static struct semaphore m_sem;
  22. static NORET_TYPE void
  23. s390_handle_damage(char *msg)
  24. {
  25. #ifdef CONFIG_SMP
  26. smp_send_stop();
  27. #endif
  28. disabled_wait((unsigned long) __builtin_return_address(0));
  29. for(;;);
  30. }
  31. static crw_handler_t crw_handlers[NR_RSCS];
  32. /**
  33. * s390_register_crw_handler() - register a channel report word handler
  34. * @rsc: reporting source code to handle
  35. * @handler: handler to be registered
  36. *
  37. * Returns %0 on success and a negative error value otherwise.
  38. */
  39. int s390_register_crw_handler(int rsc, crw_handler_t handler)
  40. {
  41. if ((rsc < 0) || (rsc >= NR_RSCS))
  42. return -EINVAL;
  43. if (!cmpxchg(&crw_handlers[rsc], NULL, handler))
  44. return 0;
  45. return -EBUSY;
  46. }
  47. /**
  48. * s390_unregister_crw_handler() - unregister a channel report word handler
  49. * @rsc: reporting source code to handle
  50. */
  51. void s390_unregister_crw_handler(int rsc)
  52. {
  53. if ((rsc < 0) || (rsc >= NR_RSCS))
  54. return;
  55. xchg(&crw_handlers[rsc], NULL);
  56. synchronize_sched();
  57. }
  58. /*
  59. * Retrieve CRWs and call function to handle event.
  60. */
  61. static int s390_collect_crw_info(void *param)
  62. {
  63. struct crw crw[2];
  64. int ccode;
  65. struct semaphore *sem;
  66. unsigned int chain;
  67. int ignore;
  68. sem = (struct semaphore *)param;
  69. repeat:
  70. ignore = down_interruptible(sem);
  71. chain = 0;
  72. while (1) {
  73. if (unlikely(chain > 1)) {
  74. struct crw tmp_crw;
  75. printk(KERN_WARNING"%s: Code does not support more "
  76. "than two chained crws; please report to "
  77. "linux390@de.ibm.com!\n", __func__);
  78. ccode = stcrw(&tmp_crw);
  79. printk(KERN_WARNING"%s: crw reports slct=%d, oflw=%d, "
  80. "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
  81. __func__, tmp_crw.slct, tmp_crw.oflw,
  82. tmp_crw.chn, tmp_crw.rsc, tmp_crw.anc,
  83. tmp_crw.erc, tmp_crw.rsid);
  84. printk(KERN_WARNING"%s: This was crw number %x in the "
  85. "chain\n", __func__, chain);
  86. if (ccode != 0)
  87. break;
  88. chain = tmp_crw.chn ? chain + 1 : 0;
  89. continue;
  90. }
  91. ccode = stcrw(&crw[chain]);
  92. if (ccode != 0)
  93. break;
  94. printk(KERN_DEBUG "crw_info : CRW reports slct=%d, oflw=%d, "
  95. "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
  96. crw[chain].slct, crw[chain].oflw, crw[chain].chn,
  97. crw[chain].rsc, crw[chain].anc, crw[chain].erc,
  98. crw[chain].rsid);
  99. /* Check for overflows. */
  100. if (crw[chain].oflw) {
  101. int i;
  102. pr_debug("%s: crw overflow detected!\n", __func__);
  103. for (i = 0; i < NR_RSCS; i++) {
  104. if (crw_handlers[i])
  105. crw_handlers[i](NULL, NULL, 1);
  106. }
  107. chain = 0;
  108. continue;
  109. }
  110. if (crw[0].chn && !chain) {
  111. chain++;
  112. continue;
  113. }
  114. if (crw_handlers[crw[chain].rsc])
  115. crw_handlers[crw[chain].rsc](&crw[0],
  116. chain ? &crw[1] : NULL,
  117. 0);
  118. /* chain is always 0 or 1 here. */
  119. chain = crw[chain].chn ? chain + 1 : 0;
  120. }
  121. goto repeat;
  122. return 0;
  123. }
  124. struct mcck_struct {
  125. int kill_task;
  126. int channel_report;
  127. int warning;
  128. unsigned long long mcck_code;
  129. };
  130. static DEFINE_PER_CPU(struct mcck_struct, cpu_mcck);
  131. /*
  132. * Main machine check handler function. Will be called with interrupts enabled
  133. * or disabled and machine checks enabled or disabled.
  134. */
  135. void
  136. s390_handle_mcck(void)
  137. {
  138. unsigned long flags;
  139. struct mcck_struct mcck;
  140. /*
  141. * Disable machine checks and get the current state of accumulated
  142. * machine checks. Afterwards delete the old state and enable machine
  143. * checks again.
  144. */
  145. local_irq_save(flags);
  146. local_mcck_disable();
  147. mcck = __get_cpu_var(cpu_mcck);
  148. memset(&__get_cpu_var(cpu_mcck), 0, sizeof(struct mcck_struct));
  149. clear_thread_flag(TIF_MCCK_PENDING);
  150. local_mcck_enable();
  151. local_irq_restore(flags);
  152. if (mcck.channel_report)
  153. up(&m_sem);
  154. #ifdef CONFIG_MACHCHK_WARNING
  155. /*
  156. * The warning may remain for a prolonged period on the bare iron.
  157. * (actually till the machine is powered off, or until the problem is gone)
  158. * So we just stop listening for the WARNING MCH and prevent continuously
  159. * being interrupted. One caveat is however, that we must do this per
  160. * processor and cannot use the smp version of ctl_clear_bit().
  161. * On VM we only get one interrupt per virtally presented machinecheck.
  162. * Though one suffices, we may get one interrupt per (virtual) processor.
  163. */
  164. if (mcck.warning) { /* WARNING pending ? */
  165. static int mchchk_wng_posted = 0;
  166. /*
  167. * Use single machine clear, as we cannot handle smp right now
  168. */
  169. __ctl_clear_bit(14, 24); /* Disable WARNING MCH */
  170. if (xchg(&mchchk_wng_posted, 1) == 0)
  171. kill_cad_pid(SIGPWR, 1);
  172. }
  173. #endif
  174. if (mcck.kill_task) {
  175. local_irq_enable();
  176. printk(KERN_EMERG "mcck: Terminating task because of machine "
  177. "malfunction (code 0x%016llx).\n", mcck.mcck_code);
  178. printk(KERN_EMERG "mcck: task: %s, pid: %d.\n",
  179. current->comm, current->pid);
  180. do_exit(SIGSEGV);
  181. }
  182. }
  183. EXPORT_SYMBOL_GPL(s390_handle_mcck);
  184. /*
  185. * returns 0 if all registers could be validated
  186. * returns 1 otherwise
  187. */
  188. static int
  189. s390_revalidate_registers(struct mci *mci)
  190. {
  191. int kill_task;
  192. u64 tmpclock;
  193. u64 zero;
  194. void *fpt_save_area, *fpt_creg_save_area;
  195. kill_task = 0;
  196. zero = 0;
  197. /* General purpose registers */
  198. if (!mci->gr)
  199. /*
  200. * General purpose registers couldn't be restored and have
  201. * unknown contents. Process needs to be terminated.
  202. */
  203. kill_task = 1;
  204. /* Revalidate floating point registers */
  205. if (!mci->fp)
  206. /*
  207. * Floating point registers can't be restored and
  208. * therefore the process needs to be terminated.
  209. */
  210. kill_task = 1;
  211. #ifndef CONFIG_64BIT
  212. asm volatile(
  213. " ld 0,0(%0)\n"
  214. " ld 2,8(%0)\n"
  215. " ld 4,16(%0)\n"
  216. " ld 6,24(%0)"
  217. : : "a" (&S390_lowcore.floating_pt_save_area));
  218. #endif
  219. if (MACHINE_HAS_IEEE) {
  220. #ifdef CONFIG_64BIT
  221. fpt_save_area = &S390_lowcore.floating_pt_save_area;
  222. fpt_creg_save_area = &S390_lowcore.fpt_creg_save_area;
  223. #else
  224. fpt_save_area = (void *) S390_lowcore.extended_save_area_addr;
  225. fpt_creg_save_area = fpt_save_area+128;
  226. #endif
  227. /* Floating point control register */
  228. if (!mci->fc) {
  229. /*
  230. * Floating point control register can't be restored.
  231. * Task will be terminated.
  232. */
  233. asm volatile("lfpc 0(%0)" : : "a" (&zero), "m" (zero));
  234. kill_task = 1;
  235. } else
  236. asm volatile("lfpc 0(%0)" : : "a" (fpt_creg_save_area));
  237. asm volatile(
  238. " ld 0,0(%0)\n"
  239. " ld 1,8(%0)\n"
  240. " ld 2,16(%0)\n"
  241. " ld 3,24(%0)\n"
  242. " ld 4,32(%0)\n"
  243. " ld 5,40(%0)\n"
  244. " ld 6,48(%0)\n"
  245. " ld 7,56(%0)\n"
  246. " ld 8,64(%0)\n"
  247. " ld 9,72(%0)\n"
  248. " ld 10,80(%0)\n"
  249. " ld 11,88(%0)\n"
  250. " ld 12,96(%0)\n"
  251. " ld 13,104(%0)\n"
  252. " ld 14,112(%0)\n"
  253. " ld 15,120(%0)\n"
  254. : : "a" (fpt_save_area));
  255. }
  256. /* Revalidate access registers */
  257. asm volatile(
  258. " lam 0,15,0(%0)"
  259. : : "a" (&S390_lowcore.access_regs_save_area));
  260. if (!mci->ar)
  261. /*
  262. * Access registers have unknown contents.
  263. * Terminating task.
  264. */
  265. kill_task = 1;
  266. /* Revalidate control registers */
  267. if (!mci->cr)
  268. /*
  269. * Control registers have unknown contents.
  270. * Can't recover and therefore stopping machine.
  271. */
  272. s390_handle_damage("invalid control registers.");
  273. else
  274. #ifdef CONFIG_64BIT
  275. asm volatile(
  276. " lctlg 0,15,0(%0)"
  277. : : "a" (&S390_lowcore.cregs_save_area));
  278. #else
  279. asm volatile(
  280. " lctl 0,15,0(%0)"
  281. : : "a" (&S390_lowcore.cregs_save_area));
  282. #endif
  283. /*
  284. * We don't even try to revalidate the TOD register, since we simply
  285. * can't write something sensible into that register.
  286. */
  287. #ifdef CONFIG_64BIT
  288. /*
  289. * See if we can revalidate the TOD programmable register with its
  290. * old contents (should be zero) otherwise set it to zero.
  291. */
  292. if (!mci->pr)
  293. asm volatile(
  294. " sr 0,0\n"
  295. " sckpf"
  296. : : : "0", "cc");
  297. else
  298. asm volatile(
  299. " l 0,0(%0)\n"
  300. " sckpf"
  301. : : "a" (&S390_lowcore.tod_progreg_save_area)
  302. : "0", "cc");
  303. #endif
  304. /* Revalidate clock comparator register */
  305. asm volatile(
  306. " stck 0(%1)\n"
  307. " sckc 0(%1)"
  308. : "=m" (tmpclock) : "a" (&(tmpclock)) : "cc", "memory");
  309. /* Check if old PSW is valid */
  310. if (!mci->wp)
  311. /*
  312. * Can't tell if we come from user or kernel mode
  313. * -> stopping machine.
  314. */
  315. s390_handle_damage("old psw invalid.");
  316. if (!mci->ms || !mci->pm || !mci->ia)
  317. kill_task = 1;
  318. return kill_task;
  319. }
  320. #define MAX_IPD_COUNT 29
  321. #define MAX_IPD_TIME (5 * 60 * USEC_PER_SEC) /* 5 minutes */
  322. /*
  323. * machine check handler.
  324. */
  325. void
  326. s390_do_machine_check(struct pt_regs *regs)
  327. {
  328. static DEFINE_SPINLOCK(ipd_lock);
  329. static unsigned long long last_ipd;
  330. static int ipd_count;
  331. unsigned long long tmp;
  332. struct mci *mci;
  333. struct mcck_struct *mcck;
  334. int umode;
  335. lockdep_off();
  336. mci = (struct mci *) &S390_lowcore.mcck_interruption_code;
  337. mcck = &__get_cpu_var(cpu_mcck);
  338. umode = user_mode(regs);
  339. if (mci->sd)
  340. /* System damage -> stopping machine */
  341. s390_handle_damage("received system damage machine check.");
  342. if (mci->pd) {
  343. if (mci->b) {
  344. /* Processing backup -> verify if we can survive this */
  345. u64 z_mcic, o_mcic, t_mcic;
  346. #ifdef CONFIG_64BIT
  347. z_mcic = (1ULL<<63 | 1ULL<<59 | 1ULL<<29);
  348. o_mcic = (1ULL<<43 | 1ULL<<42 | 1ULL<<41 | 1ULL<<40 |
  349. 1ULL<<36 | 1ULL<<35 | 1ULL<<34 | 1ULL<<32 |
  350. 1ULL<<30 | 1ULL<<21 | 1ULL<<20 | 1ULL<<17 |
  351. 1ULL<<16);
  352. #else
  353. z_mcic = (1ULL<<63 | 1ULL<<59 | 1ULL<<57 | 1ULL<<50 |
  354. 1ULL<<29);
  355. o_mcic = (1ULL<<43 | 1ULL<<42 | 1ULL<<41 | 1ULL<<40 |
  356. 1ULL<<36 | 1ULL<<35 | 1ULL<<34 | 1ULL<<32 |
  357. 1ULL<<30 | 1ULL<<20 | 1ULL<<17 | 1ULL<<16);
  358. #endif
  359. t_mcic = *(u64 *)mci;
  360. if (((t_mcic & z_mcic) != 0) ||
  361. ((t_mcic & o_mcic) != o_mcic)) {
  362. s390_handle_damage("processing backup machine "
  363. "check with damage.");
  364. }
  365. /*
  366. * Nullifying exigent condition, therefore we might
  367. * retry this instruction.
  368. */
  369. spin_lock(&ipd_lock);
  370. tmp = get_clock();
  371. if (((tmp - last_ipd) >> 12) < MAX_IPD_TIME)
  372. ipd_count++;
  373. else
  374. ipd_count = 1;
  375. last_ipd = tmp;
  376. if (ipd_count == MAX_IPD_COUNT)
  377. s390_handle_damage("too many ipd retries.");
  378. spin_unlock(&ipd_lock);
  379. }
  380. else {
  381. /* Processing damage -> stopping machine */
  382. s390_handle_damage("received instruction processing "
  383. "damage machine check.");
  384. }
  385. }
  386. if (s390_revalidate_registers(mci)) {
  387. if (umode) {
  388. /*
  389. * Couldn't restore all register contents while in
  390. * user mode -> mark task for termination.
  391. */
  392. mcck->kill_task = 1;
  393. mcck->mcck_code = *(unsigned long long *) mci;
  394. set_thread_flag(TIF_MCCK_PENDING);
  395. }
  396. else
  397. /*
  398. * Couldn't restore all register contents while in
  399. * kernel mode -> stopping machine.
  400. */
  401. s390_handle_damage("unable to revalidate registers.");
  402. }
  403. if (mci->cd) {
  404. /* Timing facility damage */
  405. s390_handle_damage("TOD clock damaged");
  406. }
  407. if (mci->ed && mci->ec) {
  408. /* External damage */
  409. if (S390_lowcore.external_damage_code & (1U << ED_ETR_SYNC))
  410. etr_sync_check();
  411. if (S390_lowcore.external_damage_code & (1U << ED_ETR_SWITCH))
  412. etr_switch_to_local();
  413. if (S390_lowcore.external_damage_code & (1U << ED_STP_SYNC))
  414. stp_sync_check();
  415. if (S390_lowcore.external_damage_code & (1U << ED_STP_ISLAND))
  416. stp_island_check();
  417. }
  418. if (mci->se)
  419. /* Storage error uncorrected */
  420. s390_handle_damage("received storage error uncorrected "
  421. "machine check.");
  422. if (mci->ke)
  423. /* Storage key-error uncorrected */
  424. s390_handle_damage("received storage key-error uncorrected "
  425. "machine check.");
  426. if (mci->ds && mci->fa)
  427. /* Storage degradation */
  428. s390_handle_damage("received storage degradation machine "
  429. "check.");
  430. if (mci->cp) {
  431. /* Channel report word pending */
  432. mcck->channel_report = 1;
  433. set_thread_flag(TIF_MCCK_PENDING);
  434. }
  435. if (mci->w) {
  436. /* Warning pending */
  437. mcck->warning = 1;
  438. set_thread_flag(TIF_MCCK_PENDING);
  439. }
  440. lockdep_on();
  441. }
  442. /*
  443. * s390_init_machine_check
  444. *
  445. * initialize machine check handling
  446. */
  447. static int
  448. machine_check_init(void)
  449. {
  450. init_MUTEX_LOCKED(&m_sem);
  451. ctl_set_bit(14, 25); /* enable external damage MCH */
  452. ctl_set_bit(14, 27); /* enable system recovery MCH */
  453. #ifdef CONFIG_MACHCHK_WARNING
  454. ctl_set_bit(14, 24); /* enable warning MCH */
  455. #endif
  456. return 0;
  457. }
  458. /*
  459. * Initialize the machine check handler really early to be able to
  460. * catch all machine checks that happen during boot
  461. */
  462. arch_initcall(machine_check_init);
  463. /*
  464. * Machine checks for the channel subsystem must be enabled
  465. * after the channel subsystem is initialized
  466. */
  467. static int __init
  468. machine_check_crw_init (void)
  469. {
  470. struct task_struct *task;
  471. task = kthread_run(s390_collect_crw_info, &m_sem, "kmcheck");
  472. if (IS_ERR(task))
  473. return PTR_ERR(task);
  474. ctl_set_bit(14, 28); /* enable channel report MCH */
  475. return 0;
  476. }
  477. device_initcall (machine_check_crw_init);