nmi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Machine check handler
  4. *
  5. * Copyright IBM Corp. 2000, 2009
  6. * Author(s): Ingo Adlung <[email protected]>,
  7. * Martin Schwidefsky <[email protected]>,
  8. * Cornelia Huck <[email protected]>,
  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/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 <asm/pai.h>
  32. #include <linux/kvm_host.h>
  33. struct mcck_struct {
  34. unsigned int kill_task : 1;
  35. unsigned int channel_report : 1;
  36. unsigned int warning : 1;
  37. unsigned int stp_queue : 1;
  38. unsigned long mcck_code;
  39. };
  40. static DEFINE_PER_CPU(struct mcck_struct, cpu_mcck);
  41. static struct kmem_cache *mcesa_cache;
  42. static unsigned long mcesa_origin_lc;
  43. static inline int nmi_needs_mcesa(void)
  44. {
  45. return MACHINE_HAS_VX || MACHINE_HAS_GS;
  46. }
  47. static inline unsigned long nmi_get_mcesa_size(void)
  48. {
  49. if (MACHINE_HAS_GS)
  50. return MCESA_MAX_SIZE;
  51. return MCESA_MIN_SIZE;
  52. }
  53. /*
  54. * The initial machine check extended save area for the boot CPU.
  55. * It will be replaced on the boot CPU reinit with an allocated
  56. * structure. The structure is required for machine check happening
  57. * early in the boot process.
  58. */
  59. static struct mcesa boot_mcesa __aligned(MCESA_MAX_SIZE);
  60. void __init nmi_alloc_mcesa_early(u64 *mcesad)
  61. {
  62. if (!nmi_needs_mcesa())
  63. return;
  64. *mcesad = __pa(&boot_mcesa);
  65. if (MACHINE_HAS_GS)
  66. *mcesad |= ilog2(MCESA_MAX_SIZE);
  67. }
  68. static void __init nmi_alloc_cache(void)
  69. {
  70. unsigned long size;
  71. if (!nmi_needs_mcesa())
  72. return;
  73. size = nmi_get_mcesa_size();
  74. if (size > MCESA_MIN_SIZE)
  75. mcesa_origin_lc = ilog2(size);
  76. /* create slab cache for the machine-check-extended-save-areas */
  77. mcesa_cache = kmem_cache_create("nmi_save_areas", size, size, 0, NULL);
  78. if (!mcesa_cache)
  79. panic("Couldn't create nmi save area cache");
  80. }
  81. int __ref nmi_alloc_mcesa(u64 *mcesad)
  82. {
  83. unsigned long origin;
  84. *mcesad = 0;
  85. if (!nmi_needs_mcesa())
  86. return 0;
  87. if (!mcesa_cache)
  88. nmi_alloc_cache();
  89. origin = (unsigned long) kmem_cache_alloc(mcesa_cache, GFP_KERNEL);
  90. if (!origin)
  91. return -ENOMEM;
  92. /* The pointer is stored with mcesa_bits ORed in */
  93. kmemleak_not_leak((void *) origin);
  94. *mcesad = __pa(origin) | mcesa_origin_lc;
  95. return 0;
  96. }
  97. void nmi_free_mcesa(u64 *mcesad)
  98. {
  99. if (!nmi_needs_mcesa())
  100. return;
  101. kmem_cache_free(mcesa_cache, __va(*mcesad & MCESA_ORIGIN_MASK));
  102. }
  103. static notrace void s390_handle_damage(void)
  104. {
  105. smp_emergency_stop();
  106. disabled_wait();
  107. while (1);
  108. }
  109. NOKPROBE_SYMBOL(s390_handle_damage);
  110. /*
  111. * Main machine check handler function. Will be called with interrupts disabled
  112. * and machine checks enabled.
  113. */
  114. void __s390_handle_mcck(void)
  115. {
  116. struct mcck_struct mcck;
  117. /*
  118. * Disable machine checks and get the current state of accumulated
  119. * machine checks. Afterwards delete the old state and enable machine
  120. * checks again.
  121. */
  122. local_mcck_disable();
  123. mcck = *this_cpu_ptr(&cpu_mcck);
  124. memset(this_cpu_ptr(&cpu_mcck), 0, sizeof(mcck));
  125. local_mcck_enable();
  126. if (mcck.channel_report)
  127. crw_handle_channel_report();
  128. /*
  129. * A warning may remain for a prolonged period on the bare iron.
  130. * (actually until the machine is powered off, or the problem is gone)
  131. * So we just stop listening for the WARNING MCH and avoid continuously
  132. * being interrupted. One caveat is however, that we must do this per
  133. * processor and cannot use the smp version of ctl_clear_bit().
  134. * On VM we only get one interrupt per virtally presented machinecheck.
  135. * Though one suffices, we may get one interrupt per (virtual) cpu.
  136. */
  137. if (mcck.warning) { /* WARNING pending ? */
  138. static int mchchk_wng_posted = 0;
  139. /* Use single cpu clear, as we cannot handle smp here. */
  140. __ctl_clear_bit(14, 24); /* Disable WARNING MCH */
  141. if (xchg(&mchchk_wng_posted, 1) == 0)
  142. kill_cad_pid(SIGPWR, 1);
  143. }
  144. if (mcck.stp_queue)
  145. stp_queue_work();
  146. if (mcck.kill_task) {
  147. local_irq_enable();
  148. printk(KERN_EMERG "mcck: Terminating task because of machine "
  149. "malfunction (code 0x%016lx).\n", mcck.mcck_code);
  150. printk(KERN_EMERG "mcck: task: %s, pid: %d.\n",
  151. current->comm, current->pid);
  152. make_task_dead(SIGSEGV);
  153. }
  154. }
  155. void noinstr s390_handle_mcck(struct pt_regs *regs)
  156. {
  157. trace_hardirqs_off();
  158. pai_kernel_enter(regs);
  159. __s390_handle_mcck();
  160. pai_kernel_exit(regs);
  161. trace_hardirqs_on();
  162. }
  163. /*
  164. * returns 0 if all required registers are available
  165. * returns 1 otherwise
  166. */
  167. static int notrace s390_validate_registers(union mci mci, int umode)
  168. {
  169. struct mcesa *mcesa;
  170. void *fpt_save_area;
  171. union ctlreg2 cr2;
  172. int kill_task;
  173. u64 zero;
  174. kill_task = 0;
  175. zero = 0;
  176. if (!mci.gr) {
  177. /*
  178. * General purpose registers couldn't be restored and have
  179. * unknown contents. Stop system or terminate process.
  180. */
  181. if (!umode)
  182. s390_handle_damage();
  183. kill_task = 1;
  184. }
  185. if (!mci.fp) {
  186. /*
  187. * Floating point registers can't be restored. If the
  188. * kernel currently uses floating point registers the
  189. * system is stopped. If the process has its floating
  190. * pointer registers loaded it is terminated.
  191. */
  192. if (S390_lowcore.fpu_flags & KERNEL_VXR_V0V7)
  193. s390_handle_damage();
  194. if (!test_cpu_flag(CIF_FPU))
  195. kill_task = 1;
  196. }
  197. fpt_save_area = &S390_lowcore.floating_pt_save_area;
  198. if (!mci.fc) {
  199. /*
  200. * Floating point control register can't be restored.
  201. * If the kernel currently uses the floating pointer
  202. * registers and needs the FPC register the system is
  203. * stopped. If the process has its floating pointer
  204. * registers loaded it is terminated. Otherwise the
  205. * FPC is just validated.
  206. */
  207. if (S390_lowcore.fpu_flags & KERNEL_FPC)
  208. s390_handle_damage();
  209. asm volatile(
  210. " lfpc %0\n"
  211. :
  212. : "Q" (zero));
  213. if (!test_cpu_flag(CIF_FPU))
  214. kill_task = 1;
  215. } else {
  216. asm volatile(
  217. " lfpc %0\n"
  218. :
  219. : "Q" (S390_lowcore.fpt_creg_save_area));
  220. }
  221. mcesa = __va(S390_lowcore.mcesad & MCESA_ORIGIN_MASK);
  222. if (!MACHINE_HAS_VX) {
  223. /* Validate floating point registers */
  224. asm volatile(
  225. " ld 0,0(%0)\n"
  226. " ld 1,8(%0)\n"
  227. " ld 2,16(%0)\n"
  228. " ld 3,24(%0)\n"
  229. " ld 4,32(%0)\n"
  230. " ld 5,40(%0)\n"
  231. " ld 6,48(%0)\n"
  232. " ld 7,56(%0)\n"
  233. " ld 8,64(%0)\n"
  234. " ld 9,72(%0)\n"
  235. " ld 10,80(%0)\n"
  236. " ld 11,88(%0)\n"
  237. " ld 12,96(%0)\n"
  238. " ld 13,104(%0)\n"
  239. " ld 14,112(%0)\n"
  240. " ld 15,120(%0)\n"
  241. :
  242. : "a" (fpt_save_area)
  243. : "memory");
  244. } else {
  245. /* Validate vector registers */
  246. union ctlreg0 cr0;
  247. /*
  248. * The vector validity must only be checked if not running a
  249. * KVM guest. For KVM guests the machine check is forwarded by
  250. * KVM and it is the responsibility of the guest to take
  251. * appropriate actions. The host vector or FPU values have been
  252. * saved by KVM and will be restored by KVM.
  253. */
  254. if (!mci.vr && !test_cpu_flag(CIF_MCCK_GUEST)) {
  255. /*
  256. * Vector registers can't be restored. If the kernel
  257. * currently uses vector registers the system is
  258. * stopped. If the process has its vector registers
  259. * loaded it is terminated. Otherwise just validate
  260. * the registers.
  261. */
  262. if (S390_lowcore.fpu_flags & KERNEL_VXR)
  263. s390_handle_damage();
  264. if (!test_cpu_flag(CIF_FPU))
  265. kill_task = 1;
  266. }
  267. cr0.val = S390_lowcore.cregs_save_area[0];
  268. cr0.afp = cr0.vx = 1;
  269. __ctl_load(cr0.val, 0, 0);
  270. asm volatile(
  271. " la 1,%0\n"
  272. " .word 0xe70f,0x1000,0x0036\n" /* vlm 0,15,0(1) */
  273. " .word 0xe70f,0x1100,0x0c36\n" /* vlm 16,31,256(1) */
  274. :
  275. : "Q" (*(struct vx_array *)mcesa->vector_save_area)
  276. : "1");
  277. __ctl_load(S390_lowcore.cregs_save_area[0], 0, 0);
  278. }
  279. /* Validate access registers */
  280. asm volatile(
  281. " lam 0,15,0(%0)\n"
  282. :
  283. : "a" (&S390_lowcore.access_regs_save_area)
  284. : "memory");
  285. if (!mci.ar) {
  286. /*
  287. * Access registers have unknown contents.
  288. * Terminating task.
  289. */
  290. kill_task = 1;
  291. }
  292. /* Validate guarded storage registers */
  293. cr2.val = S390_lowcore.cregs_save_area[2];
  294. if (cr2.gse) {
  295. if (!mci.gs) {
  296. /*
  297. * 2 cases:
  298. * - machine check in kernel or userspace
  299. * - machine check while running SIE (KVM guest)
  300. * For kernel or userspace the userspace values of
  301. * guarded storage control can not be recreated, the
  302. * process must be terminated.
  303. * For SIE the guest values of guarded storage can not
  304. * be recreated. This is either due to a bug or due to
  305. * GS being disabled in the guest. The guest will be
  306. * notified by KVM code and the guests machine check
  307. * handling must take care of this. The host values
  308. * are saved by KVM and are not affected.
  309. */
  310. if (!test_cpu_flag(CIF_MCCK_GUEST))
  311. kill_task = 1;
  312. } else {
  313. load_gs_cb((struct gs_cb *)mcesa->guarded_storage_save_area);
  314. }
  315. }
  316. /*
  317. * The getcpu vdso syscall reads CPU number from the programmable
  318. * field of the TOD clock. Disregard the TOD programmable register
  319. * validity bit and load the CPU number into the TOD programmable
  320. * field unconditionally.
  321. */
  322. set_tod_programmable_field(raw_smp_processor_id());
  323. /* Validate clock comparator register */
  324. set_clock_comparator(S390_lowcore.clock_comparator);
  325. if (!mci.ms || !mci.pm || !mci.ia)
  326. kill_task = 1;
  327. return kill_task;
  328. }
  329. NOKPROBE_SYMBOL(s390_validate_registers);
  330. /*
  331. * Backup the guest's machine check info to its description block
  332. */
  333. static void notrace s390_backup_mcck_info(struct pt_regs *regs)
  334. {
  335. struct mcck_volatile_info *mcck_backup;
  336. struct sie_page *sie_page;
  337. /* r14 contains the sie block, which was set in sie64a */
  338. struct kvm_s390_sie_block *sie_block =
  339. (struct kvm_s390_sie_block *) regs->gprs[14];
  340. if (sie_block == NULL)
  341. /* Something's seriously wrong, stop system. */
  342. s390_handle_damage();
  343. sie_page = container_of(sie_block, struct sie_page, sie_block);
  344. mcck_backup = &sie_page->mcck_info;
  345. mcck_backup->mcic = S390_lowcore.mcck_interruption_code &
  346. ~(MCCK_CODE_CP | MCCK_CODE_EXT_DAMAGE);
  347. mcck_backup->ext_damage_code = S390_lowcore.external_damage_code;
  348. mcck_backup->failing_storage_address
  349. = S390_lowcore.failing_storage_address;
  350. }
  351. NOKPROBE_SYMBOL(s390_backup_mcck_info);
  352. #define MAX_IPD_COUNT 29
  353. #define MAX_IPD_TIME (5 * 60 * USEC_PER_SEC) /* 5 minutes */
  354. #define ED_STP_ISLAND 6 /* External damage STP island check */
  355. #define ED_STP_SYNC 7 /* External damage STP sync check */
  356. #define MCCK_CODE_NO_GUEST (MCCK_CODE_CP | MCCK_CODE_EXT_DAMAGE)
  357. /*
  358. * machine check handler.
  359. */
  360. int notrace s390_do_machine_check(struct pt_regs *regs)
  361. {
  362. static int ipd_count;
  363. static DEFINE_SPINLOCK(ipd_lock);
  364. static unsigned long long last_ipd;
  365. struct mcck_struct *mcck;
  366. unsigned long long tmp;
  367. irqentry_state_t irq_state;
  368. union mci mci;
  369. unsigned long mcck_dam_code;
  370. int mcck_pending = 0;
  371. irq_state = irqentry_nmi_enter(regs);
  372. if (user_mode(regs))
  373. update_timer_mcck();
  374. inc_irq_stat(NMI_NMI);
  375. mci.val = S390_lowcore.mcck_interruption_code;
  376. mcck = this_cpu_ptr(&cpu_mcck);
  377. /*
  378. * Reinject the instruction processing damages' machine checks
  379. * including Delayed Access Exception into the guest
  380. * instead of damaging the host if they happen in the guest.
  381. */
  382. if (mci.pd && !test_cpu_flag(CIF_MCCK_GUEST)) {
  383. if (mci.b) {
  384. /* Processing backup -> verify if we can survive this */
  385. u64 z_mcic, o_mcic, t_mcic;
  386. z_mcic = (1ULL<<63 | 1ULL<<59 | 1ULL<<29);
  387. o_mcic = (1ULL<<43 | 1ULL<<42 | 1ULL<<41 | 1ULL<<40 |
  388. 1ULL<<36 | 1ULL<<35 | 1ULL<<34 | 1ULL<<32 |
  389. 1ULL<<30 | 1ULL<<21 | 1ULL<<20 | 1ULL<<17 |
  390. 1ULL<<16);
  391. t_mcic = mci.val;
  392. if (((t_mcic & z_mcic) != 0) ||
  393. ((t_mcic & o_mcic) != o_mcic)) {
  394. s390_handle_damage();
  395. }
  396. /*
  397. * Nullifying exigent condition, therefore we might
  398. * retry this instruction.
  399. */
  400. spin_lock(&ipd_lock);
  401. tmp = get_tod_clock();
  402. if (((tmp - last_ipd) >> 12) < MAX_IPD_TIME)
  403. ipd_count++;
  404. else
  405. ipd_count = 1;
  406. last_ipd = tmp;
  407. if (ipd_count == MAX_IPD_COUNT)
  408. s390_handle_damage();
  409. spin_unlock(&ipd_lock);
  410. } else {
  411. /* Processing damage -> stopping machine */
  412. s390_handle_damage();
  413. }
  414. }
  415. if (s390_validate_registers(mci, user_mode(regs))) {
  416. /*
  417. * Couldn't restore all register contents for the
  418. * user space process -> mark task for termination.
  419. */
  420. mcck->kill_task = 1;
  421. mcck->mcck_code = mci.val;
  422. mcck_pending = 1;
  423. }
  424. /*
  425. * Backup the machine check's info if it happens when the guest
  426. * is running.
  427. */
  428. if (test_cpu_flag(CIF_MCCK_GUEST))
  429. s390_backup_mcck_info(regs);
  430. if (mci.cd) {
  431. /* Timing facility damage */
  432. s390_handle_damage();
  433. }
  434. if (mci.ed && mci.ec) {
  435. /* External damage */
  436. if (S390_lowcore.external_damage_code & (1U << ED_STP_SYNC))
  437. mcck->stp_queue |= stp_sync_check();
  438. if (S390_lowcore.external_damage_code & (1U << ED_STP_ISLAND))
  439. mcck->stp_queue |= stp_island_check();
  440. mcck_pending = 1;
  441. }
  442. if (mci.cp) {
  443. /* Channel report word pending */
  444. mcck->channel_report = 1;
  445. mcck_pending = 1;
  446. }
  447. if (mci.w) {
  448. /* Warning pending */
  449. mcck->warning = 1;
  450. mcck_pending = 1;
  451. }
  452. /*
  453. * If there are only Channel Report Pending and External Damage
  454. * machine checks, they will not be reinjected into the guest
  455. * because they refer to host conditions only.
  456. */
  457. mcck_dam_code = (mci.val & MCIC_SUBCLASS_MASK);
  458. if (test_cpu_flag(CIF_MCCK_GUEST) &&
  459. (mcck_dam_code & MCCK_CODE_NO_GUEST) != mcck_dam_code) {
  460. /* Set exit reason code for host's later handling */
  461. *((long *)(regs->gprs[15] + __SF_SIE_REASON)) = -EINTR;
  462. }
  463. clear_cpu_flag(CIF_MCCK_GUEST);
  464. if (user_mode(regs) && mcck_pending) {
  465. irqentry_nmi_exit(regs, irq_state);
  466. return 1;
  467. }
  468. if (mcck_pending)
  469. schedule_mcck_handler();
  470. irqentry_nmi_exit(regs, irq_state);
  471. return 0;
  472. }
  473. NOKPROBE_SYMBOL(s390_do_machine_check);
  474. static int __init machine_check_init(void)
  475. {
  476. ctl_set_bit(14, 25); /* enable external damage MCH */
  477. ctl_set_bit(14, 27); /* enable system recovery MCH */
  478. ctl_set_bit(14, 24); /* enable warning MCH */
  479. return 0;
  480. }
  481. early_initcall(machine_check_init);