nmi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. * Heiko Carstens <heiko.carstens@de.ibm.com>,
  10. */
  11. #include <linux/kernel_stat.h>
  12. #include <linux/init.h>
  13. #include <linux/errno.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/export.h>
  22. #include <asm/lowcore.h>
  23. #include <asm/smp.h>
  24. #include <asm/stp.h>
  25. #include <asm/cputime.h>
  26. #include <asm/nmi.h>
  27. #include <asm/crw.h>
  28. #include <asm/switch_to.h>
  29. #include <asm/ctl_reg.h>
  30. #include <asm/asm-offsets.h>
  31. #include <linux/kvm_host.h>
  32. struct mcck_struct {
  33. unsigned int kill_task : 1;
  34. unsigned int channel_report : 1;
  35. unsigned int warning : 1;
  36. unsigned int stp_queue : 1;
  37. unsigned long mcck_code;
  38. };
  39. static DEFINE_PER_CPU(struct mcck_struct, cpu_mcck);
  40. static struct kmem_cache *mcesa_cache;
  41. static unsigned long mcesa_origin_lc;
  42. static inline int nmi_needs_mcesa(void)
  43. {
  44. return MACHINE_HAS_VX || MACHINE_HAS_GS;
  45. }
  46. static inline unsigned long nmi_get_mcesa_size(void)
  47. {
  48. if (MACHINE_HAS_GS)
  49. return MCESA_MAX_SIZE;
  50. return MCESA_MIN_SIZE;
  51. }
  52. /*
  53. * The initial machine check extended save area for the boot CPU.
  54. * It will be replaced by nmi_init() with an allocated structure.
  55. * The structure is required for machine check happening early in
  56. * the boot process.
  57. */
  58. static struct mcesa boot_mcesa __initdata __aligned(MCESA_MAX_SIZE);
  59. void __init nmi_alloc_boot_cpu(struct lowcore *lc)
  60. {
  61. if (!nmi_needs_mcesa())
  62. return;
  63. lc->mcesad = (unsigned long) &boot_mcesa;
  64. if (MACHINE_HAS_GS)
  65. lc->mcesad |= ilog2(MCESA_MAX_SIZE);
  66. }
  67. static int __init nmi_init(void)
  68. {
  69. unsigned long origin, cr0, size;
  70. if (!nmi_needs_mcesa())
  71. return 0;
  72. size = nmi_get_mcesa_size();
  73. if (size > MCESA_MIN_SIZE)
  74. mcesa_origin_lc = ilog2(size);
  75. /* create slab cache for the machine-check-extended-save-areas */
  76. mcesa_cache = kmem_cache_create("nmi_save_areas", size, size, 0, NULL);
  77. if (!mcesa_cache)
  78. panic("Couldn't create nmi save area cache");
  79. origin = (unsigned long) kmem_cache_alloc(mcesa_cache, GFP_KERNEL);
  80. if (!origin)
  81. panic("Couldn't allocate nmi save area");
  82. /* The pointer is stored with mcesa_bits ORed in */
  83. kmemleak_not_leak((void *) origin);
  84. __ctl_store(cr0, 0, 0);
  85. __ctl_clear_bit(0, 28); /* disable lowcore protection */
  86. /* Replace boot_mcesa on the boot CPU */
  87. S390_lowcore.mcesad = origin | mcesa_origin_lc;
  88. __ctl_load(cr0, 0, 0);
  89. return 0;
  90. }
  91. early_initcall(nmi_init);
  92. int nmi_alloc_per_cpu(struct lowcore *lc)
  93. {
  94. unsigned long origin;
  95. if (!nmi_needs_mcesa())
  96. return 0;
  97. origin = (unsigned long) kmem_cache_alloc(mcesa_cache, GFP_KERNEL);
  98. if (!origin)
  99. return -ENOMEM;
  100. /* The pointer is stored with mcesa_bits ORed in */
  101. kmemleak_not_leak((void *) origin);
  102. lc->mcesad = origin | mcesa_origin_lc;
  103. return 0;
  104. }
  105. void nmi_free_per_cpu(struct lowcore *lc)
  106. {
  107. if (!nmi_needs_mcesa())
  108. return;
  109. kmem_cache_free(mcesa_cache, (void *)(lc->mcesad & MCESA_ORIGIN_MASK));
  110. }
  111. static notrace void s390_handle_damage(void)
  112. {
  113. smp_emergency_stop();
  114. disabled_wait((unsigned long) __builtin_return_address(0));
  115. while (1);
  116. }
  117. NOKPROBE_SYMBOL(s390_handle_damage);
  118. /*
  119. * Main machine check handler function. Will be called with interrupts enabled
  120. * or disabled and machine checks enabled or disabled.
  121. */
  122. void s390_handle_mcck(void)
  123. {
  124. unsigned long flags;
  125. struct mcck_struct mcck;
  126. /*
  127. * Disable machine checks and get the current state of accumulated
  128. * machine checks. Afterwards delete the old state and enable machine
  129. * checks again.
  130. */
  131. local_irq_save(flags);
  132. local_mcck_disable();
  133. mcck = *this_cpu_ptr(&cpu_mcck);
  134. memset(this_cpu_ptr(&cpu_mcck), 0, sizeof(mcck));
  135. clear_cpu_flag(CIF_MCCK_PENDING);
  136. local_mcck_enable();
  137. local_irq_restore(flags);
  138. if (mcck.channel_report)
  139. crw_handle_channel_report();
  140. /*
  141. * A warning may remain for a prolonged period on the bare iron.
  142. * (actually until the machine is powered off, or the problem is gone)
  143. * So we just stop listening for the WARNING MCH and avoid continuously
  144. * being interrupted. One caveat is however, that we must do this per
  145. * processor and cannot use the smp version of ctl_clear_bit().
  146. * On VM we only get one interrupt per virtally presented machinecheck.
  147. * Though one suffices, we may get one interrupt per (virtual) cpu.
  148. */
  149. if (mcck.warning) { /* WARNING pending ? */
  150. static int mchchk_wng_posted = 0;
  151. /* Use single cpu clear, as we cannot handle smp here. */
  152. __ctl_clear_bit(14, 24); /* Disable WARNING MCH */
  153. if (xchg(&mchchk_wng_posted, 1) == 0)
  154. kill_cad_pid(SIGPWR, 1);
  155. }
  156. if (mcck.stp_queue)
  157. stp_queue_work();
  158. if (mcck.kill_task) {
  159. local_irq_enable();
  160. printk(KERN_EMERG "mcck: Terminating task because of machine "
  161. "malfunction (code 0x%016lx).\n", mcck.mcck_code);
  162. printk(KERN_EMERG "mcck: task: %s, pid: %d.\n",
  163. current->comm, current->pid);
  164. do_exit(SIGSEGV);
  165. }
  166. }
  167. EXPORT_SYMBOL_GPL(s390_handle_mcck);
  168. /*
  169. * returns 0 if all required registers are available
  170. * returns 1 otherwise
  171. */
  172. static int notrace s390_check_registers(union mci mci, int umode)
  173. {
  174. union ctlreg2 cr2;
  175. int kill_task;
  176. kill_task = 0;
  177. if (!mci.gr) {
  178. /*
  179. * General purpose registers couldn't be restored and have
  180. * unknown contents. Stop system or terminate process.
  181. */
  182. if (!umode)
  183. s390_handle_damage();
  184. kill_task = 1;
  185. }
  186. /* Check control registers */
  187. if (!mci.cr) {
  188. /*
  189. * Control registers have unknown contents.
  190. * Can't recover and therefore stopping machine.
  191. */
  192. s390_handle_damage();
  193. }
  194. if (!mci.fp) {
  195. /*
  196. * Floating point registers can't be restored. If the
  197. * kernel currently uses floating point registers the
  198. * system is stopped. If the process has its floating
  199. * pointer registers loaded it is terminated.
  200. */
  201. if (S390_lowcore.fpu_flags & KERNEL_VXR_V0V7)
  202. s390_handle_damage();
  203. if (!test_cpu_flag(CIF_FPU))
  204. kill_task = 1;
  205. }
  206. if (!mci.fc) {
  207. /*
  208. * Floating point control register can't be restored.
  209. * If the kernel currently uses the floating pointer
  210. * registers and needs the FPC register the system is
  211. * stopped. If the process has its floating pointer
  212. * registers loaded it is terminated.
  213. */
  214. if (S390_lowcore.fpu_flags & KERNEL_FPC)
  215. s390_handle_damage();
  216. if (!test_cpu_flag(CIF_FPU))
  217. kill_task = 1;
  218. }
  219. if (MACHINE_HAS_VX) {
  220. if (!mci.vr) {
  221. /*
  222. * Vector registers can't be restored. If the kernel
  223. * currently uses vector registers the system is
  224. * stopped. If the process has its vector registers
  225. * loaded it is terminated.
  226. */
  227. if (S390_lowcore.fpu_flags & KERNEL_VXR)
  228. s390_handle_damage();
  229. if (!test_cpu_flag(CIF_FPU))
  230. kill_task = 1;
  231. }
  232. }
  233. /* Check if access registers are valid */
  234. if (!mci.ar) {
  235. /*
  236. * Access registers have unknown contents.
  237. * Terminating task.
  238. */
  239. kill_task = 1;
  240. }
  241. /* Check guarded storage registers */
  242. cr2.val = S390_lowcore.cregs_save_area[2];
  243. if (cr2.gse) {
  244. if (!mci.gs) {
  245. /*
  246. * Guarded storage register can't be restored and
  247. * the current processes uses guarded storage.
  248. * It has to be terminated.
  249. */
  250. kill_task = 1;
  251. }
  252. }
  253. /* Check if old PSW is valid */
  254. if (!mci.wp) {
  255. /*
  256. * Can't tell if we come from user or kernel mode
  257. * -> stopping machine.
  258. */
  259. s390_handle_damage();
  260. }
  261. /* Check for invalid kernel instruction address */
  262. if (!mci.ia && !umode) {
  263. /*
  264. * The instruction address got lost while running
  265. * in the kernel -> stopping machine.
  266. */
  267. s390_handle_damage();
  268. }
  269. if (!mci.ms || !mci.pm || !mci.ia)
  270. kill_task = 1;
  271. return kill_task;
  272. }
  273. NOKPROBE_SYMBOL(s390_check_registers);
  274. /*
  275. * Backup the guest's machine check info to its description block
  276. */
  277. static void notrace s390_backup_mcck_info(struct pt_regs *regs)
  278. {
  279. struct mcck_volatile_info *mcck_backup;
  280. struct sie_page *sie_page;
  281. /* r14 contains the sie block, which was set in sie64a */
  282. struct kvm_s390_sie_block *sie_block =
  283. (struct kvm_s390_sie_block *) regs->gprs[14];
  284. if (sie_block == NULL)
  285. /* Something's seriously wrong, stop system. */
  286. s390_handle_damage();
  287. sie_page = container_of(sie_block, struct sie_page, sie_block);
  288. mcck_backup = &sie_page->mcck_info;
  289. mcck_backup->mcic = S390_lowcore.mcck_interruption_code &
  290. ~(MCCK_CODE_CP | MCCK_CODE_EXT_DAMAGE);
  291. mcck_backup->ext_damage_code = S390_lowcore.external_damage_code;
  292. mcck_backup->failing_storage_address
  293. = S390_lowcore.failing_storage_address;
  294. }
  295. NOKPROBE_SYMBOL(s390_backup_mcck_info);
  296. #define MAX_IPD_COUNT 29
  297. #define MAX_IPD_TIME (5 * 60 * USEC_PER_SEC) /* 5 minutes */
  298. #define ED_STP_ISLAND 6 /* External damage STP island check */
  299. #define ED_STP_SYNC 7 /* External damage STP sync check */
  300. #define MCCK_CODE_NO_GUEST (MCCK_CODE_CP | MCCK_CODE_EXT_DAMAGE)
  301. /*
  302. * machine check handler.
  303. */
  304. void notrace s390_do_machine_check(struct pt_regs *regs)
  305. {
  306. static int ipd_count;
  307. static DEFINE_SPINLOCK(ipd_lock);
  308. static unsigned long long last_ipd;
  309. struct mcck_struct *mcck;
  310. unsigned long long tmp;
  311. union mci mci;
  312. unsigned long mcck_dam_code;
  313. nmi_enter();
  314. inc_irq_stat(NMI_NMI);
  315. mci.val = S390_lowcore.mcck_interruption_code;
  316. mcck = this_cpu_ptr(&cpu_mcck);
  317. if (mci.sd) {
  318. /* System damage -> stopping machine */
  319. s390_handle_damage();
  320. }
  321. /*
  322. * Reinject the instruction processing damages' machine checks
  323. * including Delayed Access Exception into the guest
  324. * instead of damaging the host if they happen in the guest.
  325. */
  326. if (mci.pd && !test_cpu_flag(CIF_MCCK_GUEST)) {
  327. if (mci.b) {
  328. /* Processing backup -> verify if we can survive this */
  329. u64 z_mcic, o_mcic, t_mcic;
  330. z_mcic = (1ULL<<63 | 1ULL<<59 | 1ULL<<29);
  331. o_mcic = (1ULL<<43 | 1ULL<<42 | 1ULL<<41 | 1ULL<<40 |
  332. 1ULL<<36 | 1ULL<<35 | 1ULL<<34 | 1ULL<<32 |
  333. 1ULL<<30 | 1ULL<<21 | 1ULL<<20 | 1ULL<<17 |
  334. 1ULL<<16);
  335. t_mcic = mci.val;
  336. if (((t_mcic & z_mcic) != 0) ||
  337. ((t_mcic & o_mcic) != o_mcic)) {
  338. s390_handle_damage();
  339. }
  340. /*
  341. * Nullifying exigent condition, therefore we might
  342. * retry this instruction.
  343. */
  344. spin_lock(&ipd_lock);
  345. tmp = get_tod_clock();
  346. if (((tmp - last_ipd) >> 12) < MAX_IPD_TIME)
  347. ipd_count++;
  348. else
  349. ipd_count = 1;
  350. last_ipd = tmp;
  351. if (ipd_count == MAX_IPD_COUNT)
  352. s390_handle_damage();
  353. spin_unlock(&ipd_lock);
  354. } else {
  355. /* Processing damage -> stopping machine */
  356. s390_handle_damage();
  357. }
  358. }
  359. if (s390_check_registers(mci, user_mode(regs))) {
  360. /*
  361. * Couldn't restore all register contents for the
  362. * user space process -> mark task for termination.
  363. */
  364. mcck->kill_task = 1;
  365. mcck->mcck_code = mci.val;
  366. set_cpu_flag(CIF_MCCK_PENDING);
  367. }
  368. /*
  369. * Backup the machine check's info if it happens when the guest
  370. * is running.
  371. */
  372. if (test_cpu_flag(CIF_MCCK_GUEST))
  373. s390_backup_mcck_info(regs);
  374. if (mci.cd) {
  375. /* Timing facility damage */
  376. s390_handle_damage();
  377. }
  378. if (mci.ed && mci.ec) {
  379. /* External damage */
  380. if (S390_lowcore.external_damage_code & (1U << ED_STP_SYNC))
  381. mcck->stp_queue |= stp_sync_check();
  382. if (S390_lowcore.external_damage_code & (1U << ED_STP_ISLAND))
  383. mcck->stp_queue |= stp_island_check();
  384. if (mcck->stp_queue)
  385. set_cpu_flag(CIF_MCCK_PENDING);
  386. }
  387. /*
  388. * Reinject storage related machine checks into the guest if they
  389. * happen when the guest is running.
  390. */
  391. if (!test_cpu_flag(CIF_MCCK_GUEST)) {
  392. if (mci.se)
  393. /* Storage error uncorrected */
  394. s390_handle_damage();
  395. if (mci.ke)
  396. /* Storage key-error uncorrected */
  397. s390_handle_damage();
  398. if (mci.ds && mci.fa)
  399. /* Storage degradation */
  400. s390_handle_damage();
  401. }
  402. if (mci.cp) {
  403. /* Channel report word pending */
  404. mcck->channel_report = 1;
  405. set_cpu_flag(CIF_MCCK_PENDING);
  406. }
  407. if (mci.w) {
  408. /* Warning pending */
  409. mcck->warning = 1;
  410. set_cpu_flag(CIF_MCCK_PENDING);
  411. }
  412. /*
  413. * If there are only Channel Report Pending and External Damage
  414. * machine checks, they will not be reinjected into the guest
  415. * because they refer to host conditions only.
  416. */
  417. mcck_dam_code = (mci.val & MCIC_SUBCLASS_MASK);
  418. if (test_cpu_flag(CIF_MCCK_GUEST) &&
  419. (mcck_dam_code & MCCK_CODE_NO_GUEST) != mcck_dam_code) {
  420. /* Set exit reason code for host's later handling */
  421. *((long *)(regs->gprs[15] + __SF_SIE_REASON)) = -EINTR;
  422. }
  423. clear_cpu_flag(CIF_MCCK_GUEST);
  424. nmi_exit();
  425. }
  426. NOKPROBE_SYMBOL(s390_do_machine_check);
  427. static int __init machine_check_init(void)
  428. {
  429. ctl_set_bit(14, 25); /* enable external damage MCH */
  430. ctl_set_bit(14, 27); /* enable system recovery MCH */
  431. ctl_set_bit(14, 24); /* enable warning MCH */
  432. return 0;
  433. }
  434. early_initcall(machine_check_init);