s390mach.c 13 KB

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