nmi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Machine check handler
  4. *
  5. * Copyright IBM Corp. 2000, 2009
  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/kernel_stat.h>
  11. #include <linux/init.h>
  12. #include <linux/errno.h>
  13. #include <linux/entry-common.h>
  14. #include <linux/hardirq.h>
  15. #include <linux/log2.h>
  16. #include <linux/kprobes.h>
  17. #include <linux/kmemleak.h>
  18. #include <linux/time.h>
  19. #include <linux/module.h>
  20. #include <linux/sched/signal.h>
  21. #include <linux/kvm_host.h>
  22. #include <linux/export.h>
  23. #include <asm/lowcore.h>
  24. #include <asm/ctlreg.h>
  25. #include <asm/fpu.h>
  26. #include <asm/smp.h>
  27. #include <asm/stp.h>
  28. #include <asm/cputime.h>
  29. #include <asm/nmi.h>
  30. #include <asm/crw.h>
  31. #include <asm/asm-offsets.h>
  32. #include <asm/pai.h>
  33. #include <asm/vtime.h>
  34. struct mcck_struct {
  35. unsigned int kill_task : 1;
  36. unsigned int channel_report : 1;
  37. unsigned int warning : 1;
  38. unsigned int stp_queue : 1;
  39. unsigned long mcck_code;
  40. };
  41. static DEFINE_PER_CPU(struct mcck_struct, cpu_mcck);
  42. static inline int nmi_needs_mcesa(void)
  43. {
  44. return cpu_has_vx() || MACHINE_HAS_GS;
  45. }
  46. /*
  47. * The initial machine check extended save area for the boot CPU.
  48. * It will be replaced on the boot CPU reinit with an allocated
  49. * structure. The structure is required for machine check happening
  50. * early in the boot process.
  51. */
  52. static struct mcesa boot_mcesa __aligned(MCESA_MAX_SIZE);
  53. void __init nmi_alloc_mcesa_early(u64 *mcesad)
  54. {
  55. if (!nmi_needs_mcesa())
  56. return;
  57. *mcesad = __pa(&boot_mcesa);
  58. if (MACHINE_HAS_GS)
  59. *mcesad |= ilog2(MCESA_MAX_SIZE);
  60. }
  61. int nmi_alloc_mcesa(u64 *mcesad)
  62. {
  63. unsigned long size;
  64. void *origin;
  65. *mcesad = 0;
  66. if (!nmi_needs_mcesa())
  67. return 0;
  68. size = MACHINE_HAS_GS ? MCESA_MAX_SIZE : MCESA_MIN_SIZE;
  69. origin = kmalloc(size, GFP_KERNEL);
  70. if (!origin)
  71. return -ENOMEM;
  72. /* The pointer is stored with mcesa_bits ORed in */
  73. kmemleak_not_leak(origin);
  74. *mcesad = __pa(origin);
  75. if (MACHINE_HAS_GS)
  76. *mcesad |= ilog2(MCESA_MAX_SIZE);
  77. return 0;
  78. }
  79. void nmi_free_mcesa(u64 *mcesad)
  80. {
  81. if (!nmi_needs_mcesa())
  82. return;
  83. kfree(__va(*mcesad & MCESA_ORIGIN_MASK));
  84. }
  85. static __always_inline char *nmi_puts(char *dest, const char *src)
  86. {
  87. while (*src)
  88. *dest++ = *src++;
  89. *dest = 0;
  90. return dest;
  91. }
  92. static __always_inline char *u64_to_hex(char *dest, u64 val)
  93. {
  94. int i, num;
  95. for (i = 1; i <= 16; i++) {
  96. num = (val >> (64 - 4 * i)) & 0xf;
  97. if (num >= 10)
  98. *dest++ = 'A' + num - 10;
  99. else
  100. *dest++ = '0' + num;
  101. }
  102. *dest = 0;
  103. return dest;
  104. }
  105. static notrace void s390_handle_damage(void)
  106. {
  107. struct lowcore *lc = get_lowcore();
  108. union ctlreg0 cr0, cr0_new;
  109. char message[100];
  110. psw_t psw_save;
  111. char *ptr;
  112. smp_emergency_stop();
  113. diag_amode31_ops.diag308_reset();
  114. ptr = nmi_puts(message, "System stopped due to unrecoverable machine check, code: 0x");
  115. u64_to_hex(ptr, lc->mcck_interruption_code);
  116. /*
  117. * Disable low address protection and make machine check new PSW a
  118. * disabled wait PSW. Any additional machine check cannot be handled.
  119. */
  120. local_ctl_store(0, &cr0.reg);
  121. cr0_new = cr0;
  122. cr0_new.lap = 0;
  123. local_ctl_load(0, &cr0_new.reg);
  124. psw_save = lc->mcck_new_psw;
  125. psw_bits(lc->mcck_new_psw).io = 0;
  126. psw_bits(lc->mcck_new_psw).ext = 0;
  127. psw_bits(lc->mcck_new_psw).wait = 1;
  128. sclp_emergency_printk(message);
  129. /*
  130. * Restore machine check new PSW and control register 0 to original
  131. * values. This makes possible system dump analysis easier.
  132. */
  133. lc->mcck_new_psw = psw_save;
  134. local_ctl_load(0, &cr0.reg);
  135. disabled_wait();
  136. while (1);
  137. }
  138. NOKPROBE_SYMBOL(s390_handle_damage);
  139. /*
  140. * Main machine check handler function. Will be called with interrupts disabled
  141. * and machine checks enabled.
  142. */
  143. void s390_handle_mcck(void)
  144. {
  145. struct mcck_struct mcck;
  146. unsigned long mflags;
  147. /*
  148. * Disable machine checks and get the current state of accumulated
  149. * machine checks. Afterwards delete the old state and enable machine
  150. * checks again.
  151. */
  152. local_mcck_save(mflags);
  153. mcck = *this_cpu_ptr(&cpu_mcck);
  154. memset(this_cpu_ptr(&cpu_mcck), 0, sizeof(mcck));
  155. local_mcck_restore(mflags);
  156. if (mcck.channel_report)
  157. crw_handle_channel_report();
  158. /*
  159. * A warning may remain for a prolonged period on the bare iron.
  160. * (actually until the machine is powered off, or the problem is gone)
  161. * So we just stop listening for the WARNING MCH and avoid continuously
  162. * being interrupted. One caveat is however, that we must do this per
  163. * processor and cannot use the smp version of ctl_clear_bit().
  164. * On VM we only get one interrupt per virtally presented machinecheck.
  165. * Though one suffices, we may get one interrupt per (virtual) cpu.
  166. */
  167. if (mcck.warning) { /* WARNING pending ? */
  168. static int mchchk_wng_posted = 0;
  169. /* Use single cpu clear, as we cannot handle smp here. */
  170. local_ctl_clear_bit(14, CR14_WARNING_SUBMASK_BIT);
  171. if (xchg(&mchchk_wng_posted, 1) == 0)
  172. kill_cad_pid(SIGPWR, 1);
  173. }
  174. if (mcck.stp_queue)
  175. stp_queue_work();
  176. if (mcck.kill_task) {
  177. printk(KERN_EMERG "mcck: Terminating task because of machine "
  178. "malfunction (code 0x%016lx).\n", mcck.mcck_code);
  179. printk(KERN_EMERG "mcck: task: %s, pid: %d.\n",
  180. current->comm, current->pid);
  181. if (is_global_init(current))
  182. panic("mcck: Attempting to kill init!\n");
  183. do_send_sig_info(SIGKILL, SEND_SIG_PRIV, current, PIDTYPE_PID);
  184. }
  185. }
  186. /**
  187. * nmi_registers_valid - verify if registers are valid
  188. * @mci: machine check interruption code
  189. *
  190. * Inspect a machine check interruption code and verify if all required
  191. * registers are valid. For some registers the corresponding validity bit is
  192. * ignored and the registers are set to the expected value.
  193. * Returns true if all registers are valid, otherwise false.
  194. */
  195. static bool notrace nmi_registers_valid(union mci mci)
  196. {
  197. union ctlreg2 cr2;
  198. /*
  199. * The getcpu vdso syscall reads the CPU number from the programmable
  200. * field of the TOD clock. Disregard the TOD programmable register
  201. * validity bit and load the CPU number into the TOD programmable field
  202. * unconditionally.
  203. */
  204. set_tod_programmable_field(raw_smp_processor_id());
  205. /*
  206. * Set the clock comparator register to the next expected value.
  207. */
  208. set_clock_comparator(get_lowcore()->clock_comparator);
  209. if (!mci.gr || !mci.fp || !mci.fc)
  210. return false;
  211. /*
  212. * The vector validity must only be checked if not running a
  213. * KVM guest. For KVM guests the machine check is forwarded by
  214. * KVM and it is the responsibility of the guest to take
  215. * appropriate actions. The host vector or FPU values have been
  216. * saved by KVM and will be restored by KVM.
  217. */
  218. if (!mci.vr && !test_cpu_flag(CIF_MCCK_GUEST))
  219. return false;
  220. if (!mci.ar)
  221. return false;
  222. /*
  223. * Two cases for guarded storage registers:
  224. * - machine check in kernel or userspace
  225. * - machine check while running SIE (KVM guest)
  226. * For kernel or userspace the userspace values of guarded storage
  227. * control can not be recreated, the process must be terminated.
  228. * For SIE the guest values of guarded storage can not be recreated.
  229. * This is either due to a bug or due to GS being disabled in the
  230. * guest. The guest will be notified by KVM code and the guests machine
  231. * check handling must take care of this. The host values are saved by
  232. * KVM and are not affected.
  233. */
  234. cr2.reg = get_lowcore()->cregs_save_area[2];
  235. if (cr2.gse && !mci.gs && !test_cpu_flag(CIF_MCCK_GUEST))
  236. return false;
  237. if (!mci.ms || !mci.pm || !mci.ia)
  238. return false;
  239. return true;
  240. }
  241. NOKPROBE_SYMBOL(nmi_registers_valid);
  242. /*
  243. * Backup the guest's machine check info to its description block
  244. */
  245. static void notrace s390_backup_mcck_info(struct pt_regs *regs)
  246. {
  247. struct mcck_volatile_info *mcck_backup;
  248. struct sie_page *sie_page;
  249. /* r14 contains the sie block, which was set in sie64a */
  250. struct kvm_s390_sie_block *sie_block = phys_to_virt(regs->gprs[14]);
  251. if (sie_block == NULL)
  252. /* Something's seriously wrong, stop system. */
  253. s390_handle_damage();
  254. sie_page = container_of(sie_block, struct sie_page, sie_block);
  255. mcck_backup = &sie_page->mcck_info;
  256. mcck_backup->mcic = get_lowcore()->mcck_interruption_code &
  257. ~(MCCK_CODE_CP | MCCK_CODE_EXT_DAMAGE);
  258. mcck_backup->ext_damage_code = get_lowcore()->external_damage_code;
  259. mcck_backup->failing_storage_address = get_lowcore()->failing_storage_address;
  260. }
  261. NOKPROBE_SYMBOL(s390_backup_mcck_info);
  262. #define MAX_IPD_COUNT 29
  263. #define MAX_IPD_TIME (5 * 60 * USEC_PER_SEC) /* 5 minutes */
  264. #define ED_STP_ISLAND 6 /* External damage STP island check */
  265. #define ED_STP_SYNC 7 /* External damage STP sync check */
  266. #define MCCK_CODE_NO_GUEST (MCCK_CODE_CP | MCCK_CODE_EXT_DAMAGE)
  267. /*
  268. * machine check handler.
  269. */
  270. void notrace s390_do_machine_check(struct pt_regs *regs)
  271. {
  272. static int ipd_count;
  273. static DEFINE_SPINLOCK(ipd_lock);
  274. static unsigned long long last_ipd;
  275. struct lowcore *lc = get_lowcore();
  276. struct mcck_struct *mcck;
  277. unsigned long long tmp;
  278. irqentry_state_t irq_state;
  279. union mci mci;
  280. unsigned long mcck_dam_code;
  281. int mcck_pending = 0;
  282. irq_state = irqentry_nmi_enter(regs);
  283. if (user_mode(regs))
  284. update_timer_mcck();
  285. inc_irq_stat(NMI_NMI);
  286. mci.val = lc->mcck_interruption_code;
  287. mcck = this_cpu_ptr(&cpu_mcck);
  288. /*
  289. * Reinject the instruction processing damages' machine checks
  290. * including Delayed Access Exception into the guest
  291. * instead of damaging the host if they happen in the guest.
  292. */
  293. if (mci.pd && !test_cpu_flag(CIF_MCCK_GUEST)) {
  294. if (mci.b) {
  295. /* Processing backup -> verify if we can survive this */
  296. u64 z_mcic, o_mcic, t_mcic;
  297. z_mcic = (1ULL<<63 | 1ULL<<59 | 1ULL<<29);
  298. o_mcic = (1ULL<<43 | 1ULL<<42 | 1ULL<<41 | 1ULL<<40 |
  299. 1ULL<<36 | 1ULL<<35 | 1ULL<<34 | 1ULL<<32 |
  300. 1ULL<<30 | 1ULL<<21 | 1ULL<<20 | 1ULL<<17 |
  301. 1ULL<<16);
  302. t_mcic = mci.val;
  303. if (((t_mcic & z_mcic) != 0) ||
  304. ((t_mcic & o_mcic) != o_mcic)) {
  305. s390_handle_damage();
  306. }
  307. /*
  308. * Nullifying exigent condition, therefore we might
  309. * retry this instruction.
  310. */
  311. spin_lock(&ipd_lock);
  312. tmp = get_tod_clock();
  313. if (((tmp - last_ipd) >> 12) < MAX_IPD_TIME)
  314. ipd_count++;
  315. else
  316. ipd_count = 1;
  317. last_ipd = tmp;
  318. if (ipd_count == MAX_IPD_COUNT)
  319. s390_handle_damage();
  320. spin_unlock(&ipd_lock);
  321. } else {
  322. /* Processing damage -> stopping machine */
  323. s390_handle_damage();
  324. }
  325. }
  326. if (!nmi_registers_valid(mci)) {
  327. if (!user_mode(regs))
  328. s390_handle_damage();
  329. /*
  330. * Couldn't restore all register contents for the
  331. * user space process -> mark task for termination.
  332. */
  333. mcck->kill_task = 1;
  334. mcck->mcck_code = mci.val;
  335. mcck_pending = 1;
  336. }
  337. /*
  338. * Backup the machine check's info if it happens when the guest
  339. * is running.
  340. */
  341. if (test_cpu_flag(CIF_MCCK_GUEST))
  342. s390_backup_mcck_info(regs);
  343. if (mci.cd) {
  344. /* Timing facility damage */
  345. s390_handle_damage();
  346. }
  347. if (mci.ed && mci.ec) {
  348. /* External damage */
  349. if (lc->external_damage_code & (1U << ED_STP_SYNC))
  350. mcck->stp_queue |= stp_sync_check();
  351. if (lc->external_damage_code & (1U << ED_STP_ISLAND))
  352. mcck->stp_queue |= stp_island_check();
  353. mcck_pending = 1;
  354. }
  355. /*
  356. * Reinject storage related machine checks into the guest if they
  357. * happen when the guest is running.
  358. */
  359. if (!test_cpu_flag(CIF_MCCK_GUEST)) {
  360. /* Storage error uncorrected */
  361. if (mci.se)
  362. s390_handle_damage();
  363. /* Storage key-error uncorrected */
  364. if (mci.ke)
  365. s390_handle_damage();
  366. /* Storage degradation */
  367. if (mci.ds && mci.fa)
  368. s390_handle_damage();
  369. }
  370. if (mci.cp) {
  371. /* Channel report word pending */
  372. mcck->channel_report = 1;
  373. mcck_pending = 1;
  374. }
  375. if (mci.w) {
  376. /* Warning pending */
  377. mcck->warning = 1;
  378. mcck_pending = 1;
  379. }
  380. /*
  381. * If there are only Channel Report Pending and External Damage
  382. * machine checks, they will not be reinjected into the guest
  383. * because they refer to host conditions only.
  384. */
  385. mcck_dam_code = (mci.val & MCIC_SUBCLASS_MASK);
  386. if (test_cpu_flag(CIF_MCCK_GUEST) &&
  387. (mcck_dam_code & MCCK_CODE_NO_GUEST) != mcck_dam_code) {
  388. /* Set exit reason code for host's later handling */
  389. *((long *)(regs->gprs[15] + __SF_SIE_REASON)) = -EINTR;
  390. }
  391. clear_cpu_flag(CIF_MCCK_GUEST);
  392. if (mcck_pending)
  393. schedule_mcck_handler();
  394. irqentry_nmi_exit(regs, irq_state);
  395. }
  396. NOKPROBE_SYMBOL(s390_do_machine_check);
  397. static int __init machine_check_init(void)
  398. {
  399. system_ctl_set_bit(14, CR14_EXTERNAL_DAMAGE_SUBMASK_BIT);
  400. system_ctl_set_bit(14, CR14_RECOVERY_SUBMASK_BIT);
  401. system_ctl_set_bit(14, CR14_WARNING_SUBMASK_BIT);
  402. return 0;
  403. }
  404. early_initcall(machine_check_init);