blob: 651a51914e3442ff1de32ac6216611830c57d2f0 [file] [log] [blame]
Greg Kroah-Hartmana17ae4c2017-11-24 15:00:32 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Heiko Carstensf5daba12009-03-26 15:24:01 +01003 * Machine check handler
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02005 * Copyright IBM Corp. 2000, 2009
Heiko Carstensf5daba12009-03-26 15:24:01 +01006 * 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>,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
Heiko Carstens052ff462011-01-05 12:47:28 +010012#include <linux/kernel_stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/errno.h>
Heiko Carstens81f64b82009-04-14 15:36:18 +020015#include <linux/hardirq.h>
Martin Schwidefsky6c815112017-10-12 13:24:47 +020016#include <linux/log2.h>
Martin Schwidefsky00a8f882017-09-15 16:24:31 +020017#include <linux/kprobes.h>
Randy Dunlap514c6032018-04-05 16:25:34 -070018#include <linux/kmemleak.h>
Heiko Carstens022e4fc2006-05-01 12:16:14 -070019#include <linux/time.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010020#include <linux/module.h>
21#include <linux/sched/signal.h>
22
Paul Gortmaker3994a522017-02-09 15:20:23 -050023#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/lowcore.h>
Heiko Carstensf5daba12009-03-26 15:24:01 +010025#include <asm/smp.h>
Martin Schwidefskyfd5ada02016-05-31 15:06:51 +020026#include <asm/stp.h>
Martin Schwidefsky76d4e002009-06-12 10:26:21 +020027#include <asm/cputime.h>
Heiko Carstensf5daba12009-03-26 15:24:01 +010028#include <asm/nmi.h>
29#include <asm/crw.h>
Martin Schwidefsky80703612014-10-06 17:53:53 +020030#include <asm/switch_to.h>
Heiko Carstenscad49cf2015-07-07 08:40:49 +020031#include <asm/ctl_reg.h>
QingFeng Haoc9295002017-06-07 11:30:42 +020032#include <asm/asm-offsets.h>
QingFeng Haoda72ca42017-06-07 11:41:19 +020033#include <linux/kvm_host.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Heiko Carstens77fa2242005-06-25 14:55:30 -070035struct mcck_struct {
Heiko Carstens36324962015-10-12 13:04:12 +020036 unsigned int kill_task : 1;
37 unsigned int channel_report : 1;
38 unsigned int warning : 1;
Heiko Carstens29b0a822015-10-09 13:48:03 +020039 unsigned int stp_queue : 1;
Heiko Carstensdc6e1552015-10-12 13:00:39 +020040 unsigned long mcck_code;
Heiko Carstens77fa2242005-06-25 14:55:30 -070041};
42
43static DEFINE_PER_CPU(struct mcck_struct, cpu_mcck);
Martin Schwidefsky6c815112017-10-12 13:24:47 +020044static struct kmem_cache *mcesa_cache;
45static unsigned long mcesa_origin_lc;
46
47static inline int nmi_needs_mcesa(void)
48{
49 return MACHINE_HAS_VX || MACHINE_HAS_GS;
50}
51
52static inline unsigned long nmi_get_mcesa_size(void)
53{
54 if (MACHINE_HAS_GS)
55 return MCESA_MAX_SIZE;
56 return MCESA_MIN_SIZE;
57}
58
59/*
60 * The initial machine check extended save area for the boot CPU.
Alexander Gordeevc7ed5092021-12-10 16:32:11 +010061 * It will be replaced on the boot CPU reinit with an allocated
62 * structure. The structure is required for machine check happening
63 * early in the boot process.
Martin Schwidefsky6c815112017-10-12 13:24:47 +020064 */
65static struct mcesa boot_mcesa __initdata __aligned(MCESA_MAX_SIZE);
66
Alexander Gordeevc7ed5092021-12-10 16:32:11 +010067void __init nmi_alloc_mcesa_early(u64 *mcesad)
Martin Schwidefsky6c815112017-10-12 13:24:47 +020068{
69 if (!nmi_needs_mcesa())
70 return;
Alexander Gordeevc7ed5092021-12-10 16:32:11 +010071 *mcesad = __pa(&boot_mcesa);
Martin Schwidefsky6c815112017-10-12 13:24:47 +020072 if (MACHINE_HAS_GS)
Alexander Gordeevc7ed5092021-12-10 16:32:11 +010073 *mcesad |= ilog2(MCESA_MAX_SIZE);
Martin Schwidefsky6c815112017-10-12 13:24:47 +020074}
75
Alexander Gordeevc7ed5092021-12-10 16:32:11 +010076static void __init nmi_alloc_cache(void)
Martin Schwidefsky6c815112017-10-12 13:24:47 +020077{
Alexander Gordeevc7ed5092021-12-10 16:32:11 +010078 unsigned long size;
Martin Schwidefsky6c815112017-10-12 13:24:47 +020079
80 if (!nmi_needs_mcesa())
Alexander Gordeevc7ed5092021-12-10 16:32:11 +010081 return;
Martin Schwidefsky6c815112017-10-12 13:24:47 +020082 size = nmi_get_mcesa_size();
83 if (size > MCESA_MIN_SIZE)
84 mcesa_origin_lc = ilog2(size);
85 /* create slab cache for the machine-check-extended-save-areas */
86 mcesa_cache = kmem_cache_create("nmi_save_areas", size, size, 0, NULL);
87 if (!mcesa_cache)
88 panic("Couldn't create nmi save area cache");
Martin Schwidefsky6c815112017-10-12 13:24:47 +020089}
Martin Schwidefsky6c815112017-10-12 13:24:47 +020090
Alexander Gordeevc7ed5092021-12-10 16:32:11 +010091int __ref nmi_alloc_mcesa(u64 *mcesad)
Martin Schwidefsky6c815112017-10-12 13:24:47 +020092{
93 unsigned long origin;
94
Alexander Gordeevc7ed5092021-12-10 16:32:11 +010095 *mcesad = 0;
Martin Schwidefsky6c815112017-10-12 13:24:47 +020096 if (!nmi_needs_mcesa())
97 return 0;
Alexander Gordeevc7ed5092021-12-10 16:32:11 +010098 if (!mcesa_cache)
99 nmi_alloc_cache();
Martin Schwidefsky6c815112017-10-12 13:24:47 +0200100 origin = (unsigned long) kmem_cache_alloc(mcesa_cache, GFP_KERNEL);
101 if (!origin)
102 return -ENOMEM;
103 /* The pointer is stored with mcesa_bits ORed in */
104 kmemleak_not_leak((void *) origin);
Alexander Gordeevc7ed5092021-12-10 16:32:11 +0100105 *mcesad = __pa(origin) | mcesa_origin_lc;
Martin Schwidefsky6c815112017-10-12 13:24:47 +0200106 return 0;
107}
108
Alexander Gordeevc7ed5092021-12-10 16:32:11 +0100109void nmi_free_mcesa(u64 *mcesad)
Martin Schwidefsky6c815112017-10-12 13:24:47 +0200110{
111 if (!nmi_needs_mcesa())
112 return;
Alexander Gordeevc7ed5092021-12-10 16:32:11 +0100113 kmem_cache_free(mcesa_cache, __va(*mcesad & MCESA_ORIGIN_MASK));
Martin Schwidefsky6c815112017-10-12 13:24:47 +0200114}
Heiko Carstens77fa2242005-06-25 14:55:30 -0700115
Martin Schwidefsky00a8f882017-09-15 16:24:31 +0200116static notrace void s390_handle_damage(void)
Heiko Carstensf5daba12009-03-26 15:24:01 +0100117{
Martin Schwidefsky00a8f882017-09-15 16:24:31 +0200118 smp_emergency_stop();
Martin Schwidefsky98587c22019-04-30 12:33:45 +0200119 disabled_wait();
Heiko Carstensf5daba12009-03-26 15:24:01 +0100120 while (1);
121}
Martin Schwidefsky00a8f882017-09-15 16:24:31 +0200122NOKPROBE_SYMBOL(s390_handle_damage);
Heiko Carstensf5daba12009-03-26 15:24:01 +0100123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124/*
Sven Schnelle56e62a72020-11-21 11:14:56 +0100125 * Main machine check handler function. Will be called with interrupts disabled
126 * and machine checks enabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 */
Sven Schnelle56e62a72020-11-21 11:14:56 +0100128void __s390_handle_mcck(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
Heiko Carstens77fa2242005-06-25 14:55:30 -0700130 struct mcck_struct mcck;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Heiko Carstens77fa2242005-06-25 14:55:30 -0700132 /*
133 * Disable machine checks and get the current state of accumulated
134 * machine checks. Afterwards delete the old state and enable machine
135 * checks again.
136 */
Heiko Carstens77fa2242005-06-25 14:55:30 -0700137 local_mcck_disable();
Sebastian Ott2cb4a182014-11-28 15:40:57 +0100138 mcck = *this_cpu_ptr(&cpu_mcck);
139 memset(this_cpu_ptr(&cpu_mcck), 0, sizeof(mcck));
Heiko Carstens77fa2242005-06-25 14:55:30 -0700140 local_mcck_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Heiko Carstens77fa2242005-06-25 14:55:30 -0700142 if (mcck.channel_report)
Heiko Carstensf5daba12009-03-26 15:24:01 +0100143 crw_handle_channel_report();
Heiko Carstens7b886412009-03-26 15:24:02 +0100144 /*
145 * A warning may remain for a prolonged period on the bare iron.
146 * (actually until the machine is powered off, or the problem is gone)
147 * So we just stop listening for the WARNING MCH and avoid continuously
148 * being interrupted. One caveat is however, that we must do this per
149 * processor and cannot use the smp version of ctl_clear_bit().
150 * On VM we only get one interrupt per virtally presented machinecheck.
151 * Though one suffices, we may get one interrupt per (virtual) cpu.
152 */
Heiko Carstens77fa2242005-06-25 14:55:30 -0700153 if (mcck.warning) { /* WARNING pending ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 static int mchchk_wng_posted = 0;
Heiko Carstens7b886412009-03-26 15:24:02 +0100155
156 /* Use single cpu clear, as we cannot handle smp here. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 __ctl_clear_bit(14, 24); /* Disable WARNING MCH */
158 if (xchg(&mchchk_wng_posted, 1) == 0)
Cedric Le Goater9ec52092006-10-02 02:19:00 -0700159 kill_cad_pid(SIGPWR, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
Heiko Carstens29b0a822015-10-09 13:48:03 +0200161 if (mcck.stp_queue)
162 stp_queue_work();
Heiko Carstens77fa2242005-06-25 14:55:30 -0700163 if (mcck.kill_task) {
164 local_irq_enable();
165 printk(KERN_EMERG "mcck: Terminating task because of machine "
Heiko Carstensdc6e1552015-10-12 13:00:39 +0200166 "malfunction (code 0x%016lx).\n", mcck.mcck_code);
Heiko Carstens77fa2242005-06-25 14:55:30 -0700167 printk(KERN_EMERG "mcck: task: %s, pid: %d.\n",
168 current->comm, current->pid);
Eric W. Biederman0e254982021-06-28 14:52:01 -0500169 make_task_dead(SIGSEGV);
Heiko Carstens77fa2242005-06-25 14:55:30 -0700170 }
171}
172
Sven Schnelle56e62a72020-11-21 11:14:56 +0100173void noinstr s390_handle_mcck(void)
174{
175 trace_hardirqs_off();
176 __s390_handle_mcck();
177 trace_hardirqs_on();
178}
Heiko Carstens77fa2242005-06-25 14:55:30 -0700179/*
Martin Schwidefsky3037a522017-10-12 13:24:48 +0200180 * returns 0 if all required registers are available
Heiko Carstens77fa2242005-06-25 14:55:30 -0700181 * returns 1 otherwise
182 */
Alexander Gordeev5fa2ea02021-06-18 08:17:18 +0200183static int notrace s390_validate_registers(union mci mci, int umode)
Heiko Carstens77fa2242005-06-25 14:55:30 -0700184{
Alexander Gordeev5fa2ea02021-06-18 08:17:18 +0200185 struct mcesa *mcesa;
186 void *fpt_save_area;
Martin Schwidefskyad3bc0a2017-10-12 13:24:45 +0200187 union ctlreg2 cr2;
Heiko Carstens77fa2242005-06-25 14:55:30 -0700188 int kill_task;
Alexander Gordeev5fa2ea02021-06-18 08:17:18 +0200189 u64 zero;
Heiko Carstens77fa2242005-06-25 14:55:30 -0700190
191 kill_task = 0;
Alexander Gordeev5fa2ea02021-06-18 08:17:18 +0200192 zero = 0;
Heiko Carstensf5daba12009-03-26 15:24:01 +0100193
Heiko Carstensdc6e1552015-10-12 13:00:39 +0200194 if (!mci.gr) {
Heiko Carstens77fa2242005-06-25 14:55:30 -0700195 /*
196 * General purpose registers couldn't be restored and have
Martin Schwidefsky8f149ea2016-08-22 14:40:06 +0200197 * unknown contents. Stop system or terminate process.
Heiko Carstens77fa2242005-06-25 14:55:30 -0700198 */
Martin Schwidefsky8f149ea2016-08-22 14:40:06 +0200199 if (!umode)
200 s390_handle_damage();
Heiko Carstens77fa2242005-06-25 14:55:30 -0700201 kill_task = 1;
Heiko Carstensf5daba12009-03-26 15:24:01 +0100202 }
Heiko Carstensdc6e1552015-10-12 13:00:39 +0200203 if (!mci.fp) {
Heiko Carstens77fa2242005-06-25 14:55:30 -0700204 /*
Martin Schwidefsky8f149ea2016-08-22 14:40:06 +0200205 * Floating point registers can't be restored. If the
206 * kernel currently uses floating point registers the
207 * system is stopped. If the process has its floating
208 * pointer registers loaded it is terminated.
Heiko Carstens77fa2242005-06-25 14:55:30 -0700209 */
Martin Schwidefsky8f149ea2016-08-22 14:40:06 +0200210 if (S390_lowcore.fpu_flags & KERNEL_VXR_V0V7)
211 s390_handle_damage();
212 if (!test_cpu_flag(CIF_FPU))
213 kill_task = 1;
Heiko Carstensf5daba12009-03-26 15:24:01 +0100214 }
Alexander Gordeev5fa2ea02021-06-18 08:17:18 +0200215 fpt_save_area = &S390_lowcore.floating_pt_save_area;
Heiko Carstensdc6e1552015-10-12 13:00:39 +0200216 if (!mci.fc) {
Heiko Carstens5a79859a2015-02-12 13:08:27 +0100217 /*
218 * Floating point control register can't be restored.
Martin Schwidefsky8f149ea2016-08-22 14:40:06 +0200219 * If the kernel currently uses the floating pointer
220 * registers and needs the FPC register the system is
221 * stopped. If the process has its floating pointer
Alexander Gordeev5fa2ea02021-06-18 08:17:18 +0200222 * registers loaded it is terminated. Otherwise the
223 * FPC is just validated.
Heiko Carstens5a79859a2015-02-12 13:08:27 +0100224 */
Martin Schwidefsky8f149ea2016-08-22 14:40:06 +0200225 if (S390_lowcore.fpu_flags & KERNEL_FPC)
226 s390_handle_damage();
Alexander Gordeev5fa2ea02021-06-18 08:17:18 +0200227 asm volatile(
228 " lfpc %0\n"
229 :
230 : "Q" (zero));
Martin Schwidefsky8f149ea2016-08-22 14:40:06 +0200231 if (!test_cpu_flag(CIF_FPU))
232 kill_task = 1;
Alexander Gordeev5fa2ea02021-06-18 08:17:18 +0200233 } else {
234 asm volatile(
235 " lfpc %0\n"
236 :
237 : "Q" (S390_lowcore.fpt_creg_save_area));
Heiko Carstens86fa7082016-12-13 12:42:07 +0100238 }
Heiko Carstens5a79859a2015-02-12 13:08:27 +0100239
Heiko Carstens402ff5a2021-11-25 19:59:16 +0100240 mcesa = __va(S390_lowcore.mcesad & MCESA_ORIGIN_MASK);
Alexander Gordeev5fa2ea02021-06-18 08:17:18 +0200241 if (!MACHINE_HAS_VX) {
242 /* Validate floating point registers */
243 asm volatile(
244 " ld 0,0(%0)\n"
245 " ld 1,8(%0)\n"
246 " ld 2,16(%0)\n"
247 " ld 3,24(%0)\n"
248 " ld 4,32(%0)\n"
249 " ld 5,40(%0)\n"
250 " ld 6,48(%0)\n"
251 " ld 7,56(%0)\n"
252 " ld 8,64(%0)\n"
253 " ld 9,72(%0)\n"
254 " ld 10,80(%0)\n"
255 " ld 11,88(%0)\n"
256 " ld 12,96(%0)\n"
257 " ld 13,104(%0)\n"
258 " ld 14,112(%0)\n"
259 " ld 15,120(%0)\n"
260 :
261 : "a" (fpt_save_area)
262 : "memory");
263 } else {
264 /* Validate vector registers */
265 union ctlreg0 cr0;
266
Christian Borntraegerf094a392022-01-17 18:40:32 +0100267 /*
268 * The vector validity must only be checked if not running a
269 * KVM guest. For KVM guests the machine check is forwarded by
270 * KVM and it is the responsibility of the guest to take
271 * appropriate actions. The host vector or FPU values have been
272 * saved by KVM and will be restored by KVM.
273 */
274 if (!mci.vr && !test_cpu_flag(CIF_MCCK_GUEST)) {
Martin Schwidefsky80703612014-10-06 17:53:53 +0200275 /*
Martin Schwidefsky8f149ea2016-08-22 14:40:06 +0200276 * Vector registers can't be restored. If the kernel
277 * currently uses vector registers the system is
278 * stopped. If the process has its vector registers
Alexander Gordeev5fa2ea02021-06-18 08:17:18 +0200279 * loaded it is terminated. Otherwise just validate
280 * the registers.
Martin Schwidefsky80703612014-10-06 17:53:53 +0200281 */
Martin Schwidefsky8f149ea2016-08-22 14:40:06 +0200282 if (S390_lowcore.fpu_flags & KERNEL_VXR)
283 s390_handle_damage();
284 if (!test_cpu_flag(CIF_FPU))
285 kill_task = 1;
Martin Schwidefsky80703612014-10-06 17:53:53 +0200286 }
Alexander Gordeev5fa2ea02021-06-18 08:17:18 +0200287 cr0.val = S390_lowcore.cregs_save_area[0];
288 cr0.afp = cr0.vx = 1;
289 __ctl_load(cr0.val, 0, 0);
290 asm volatile(
291 " la 1,%0\n"
292 " .word 0xe70f,0x1000,0x0036\n" /* vlm 0,15,0(1) */
293 " .word 0xe70f,0x1100,0x0c36\n" /* vlm 16,31,256(1) */
294 :
295 : "Q" (*(struct vx_array *)mcesa->vector_save_area)
296 : "1");
297 __ctl_load(S390_lowcore.cregs_save_area[0], 0, 0);
Martin Schwidefsky80703612014-10-06 17:53:53 +0200298 }
Alexander Gordeev5fa2ea02021-06-18 08:17:18 +0200299 /* Validate access registers */
300 asm volatile(
301 " lam 0,15,0(%0)\n"
302 :
303 : "a" (&S390_lowcore.access_regs_save_area)
304 : "memory");
Heiko Carstensdc6e1552015-10-12 13:00:39 +0200305 if (!mci.ar) {
Heiko Carstens77fa2242005-06-25 14:55:30 -0700306 /*
307 * Access registers have unknown contents.
308 * Terminating task.
309 */
310 kill_task = 1;
Heiko Carstensf5daba12009-03-26 15:24:01 +0100311 }
Alexander Gordeev5fa2ea02021-06-18 08:17:18 +0200312 /* Validate guarded storage registers */
Martin Schwidefskyad3bc0a2017-10-12 13:24:45 +0200313 cr2.val = S390_lowcore.cregs_save_area[2];
314 if (cr2.gse) {
Martin Schwidefsky3037a522017-10-12 13:24:48 +0200315 if (!mci.gs) {
Martin Schwidefsky916cda12016-01-26 14:10:34 +0100316 /*
Christian Borntraeger1ea1d6a2022-01-13 11:44:19 +0100317 * 2 cases:
318 * - machine check in kernel or userspace
319 * - machine check while running SIE (KVM guest)
320 * For kernel or userspace the userspace values of
321 * guarded storage control can not be recreated, the
322 * process must be terminated.
323 * For SIE the guest values of guarded storage can not
324 * be recreated. This is either due to a bug or due to
325 * GS being disabled in the guest. The guest will be
326 * notified by KVM code and the guests machine check
327 * handling must take care of this. The host values
328 * are saved by KVM and are not affected.
Martin Schwidefsky916cda12016-01-26 14:10:34 +0100329 */
Christian Borntraeger1ea1d6a2022-01-13 11:44:19 +0100330 if (!test_cpu_flag(CIF_MCCK_GUEST))
331 kill_task = 1;
Alexander Gordeev5fa2ea02021-06-18 08:17:18 +0200332 } else {
333 load_gs_cb((struct gs_cb *)mcesa->guarded_storage_save_area);
Martin Schwidefsky3037a522017-10-12 13:24:48 +0200334 }
Martin Schwidefsky916cda12016-01-26 14:10:34 +0100335 }
Alexander Gordeev5fa2ea02021-06-18 08:17:18 +0200336 /*
337 * The getcpu vdso syscall reads CPU number from the programmable
338 * field of the TOD clock. Disregard the TOD programmable register
339 * validity bit and load the CPU number into the TOD programmable
340 * field unconditionally.
341 */
342 set_tod_programmable_field(raw_smp_processor_id());
343 /* Validate clock comparator register */
344 set_clock_comparator(S390_lowcore.clock_comparator);
Heiko Carstens77fa2242005-06-25 14:55:30 -0700345
Heiko Carstensdc6e1552015-10-12 13:00:39 +0200346 if (!mci.ms || !mci.pm || !mci.ia)
Heiko Carstens77fa2242005-06-25 14:55:30 -0700347 kill_task = 1;
348
349 return kill_task;
350}
Alexander Gordeev5fa2ea02021-06-18 08:17:18 +0200351NOKPROBE_SYMBOL(s390_validate_registers);
Heiko Carstens77fa2242005-06-25 14:55:30 -0700352
QingFeng Haoda72ca42017-06-07 11:41:19 +0200353/*
354 * Backup the guest's machine check info to its description block
355 */
356static void notrace s390_backup_mcck_info(struct pt_regs *regs)
357{
358 struct mcck_volatile_info *mcck_backup;
359 struct sie_page *sie_page;
360
361 /* r14 contains the sie block, which was set in sie64a */
362 struct kvm_s390_sie_block *sie_block =
363 (struct kvm_s390_sie_block *) regs->gprs[14];
364
365 if (sie_block == NULL)
366 /* Something's seriously wrong, stop system. */
367 s390_handle_damage();
368
369 sie_page = container_of(sie_block, struct sie_page, sie_block);
370 mcck_backup = &sie_page->mcck_info;
371 mcck_backup->mcic = S390_lowcore.mcck_interruption_code &
372 ~(MCCK_CODE_CP | MCCK_CODE_EXT_DAMAGE);
373 mcck_backup->ext_damage_code = S390_lowcore.external_damage_code;
374 mcck_backup->failing_storage_address
375 = S390_lowcore.failing_storage_address;
376}
Martin Schwidefsky00a8f882017-09-15 16:24:31 +0200377NOKPROBE_SYMBOL(s390_backup_mcck_info);
QingFeng Haoda72ca42017-06-07 11:41:19 +0200378
Heiko Carstensb73d40c2006-04-27 18:40:23 -0700379#define MAX_IPD_COUNT 29
Heiko Carstens022e4fc2006-05-01 12:16:14 -0700380#define MAX_IPD_TIME (5 * 60 * USEC_PER_SEC) /* 5 minutes */
Heiko Carstensb73d40c2006-04-27 18:40:23 -0700381
Heiko Carstensf5daba12009-03-26 15:24:01 +0100382#define ED_STP_ISLAND 6 /* External damage STP island check */
383#define ED_STP_SYNC 7 /* External damage STP sync check */
Heiko Carstensf5daba12009-03-26 15:24:01 +0100384
QingFeng Haoc9295002017-06-07 11:30:42 +0200385#define MCCK_CODE_NO_GUEST (MCCK_CODE_CP | MCCK_CODE_EXT_DAMAGE)
386
Heiko Carstens77fa2242005-06-25 14:55:30 -0700387/*
388 * machine check handler.
389 */
Sven Schnelle0b0ed652020-02-20 12:09:36 +0100390int notrace s390_do_machine_check(struct pt_regs *regs)
Heiko Carstens77fa2242005-06-25 14:55:30 -0700391{
Heiko Carstensf5daba12009-03-26 15:24:01 +0100392 static int ipd_count;
Heiko Carstensb73d40c2006-04-27 18:40:23 -0700393 static DEFINE_SPINLOCK(ipd_lock);
394 static unsigned long long last_ipd;
Heiko Carstensf5daba12009-03-26 15:24:01 +0100395 struct mcck_struct *mcck;
Heiko Carstensb73d40c2006-04-27 18:40:23 -0700396 unsigned long long tmp;
Heiko Carstensdc6e1552015-10-12 13:00:39 +0200397 union mci mci;
QingFeng Haoc9295002017-06-07 11:30:42 +0200398 unsigned long mcck_dam_code;
Sven Schnelle0b0ed652020-02-20 12:09:36 +0100399 int mcck_pending = 0;
Heiko Carstens77fa2242005-06-25 14:55:30 -0700400
Heiko Carstens81f64b82009-04-14 15:36:18 +0200401 nmi_enter();
Sven Schnelle56e62a72020-11-21 11:14:56 +0100402
403 if (user_mode(regs))
404 update_timer_mcck();
Heiko Carstens420f42e2013-01-02 15:18:18 +0100405 inc_irq_stat(NMI_NMI);
Heiko Carstensdc6e1552015-10-12 13:00:39 +0200406 mci.val = S390_lowcore.mcck_interruption_code;
Christoph Lametereb7e7d72014-08-17 12:30:45 -0500407 mcck = this_cpu_ptr(&cpu_mcck);
Heiko Carstens77fa2242005-06-25 14:55:30 -0700408
QingFeng Haoc9295002017-06-07 11:30:42 +0200409 /*
410 * Reinject the instruction processing damages' machine checks
411 * including Delayed Access Exception into the guest
412 * instead of damaging the host if they happen in the guest.
413 */
414 if (mci.pd && !test_cpu_flag(CIF_MCCK_GUEST)) {
Heiko Carstensdc6e1552015-10-12 13:00:39 +0200415 if (mci.b) {
Heiko Carstens77fa2242005-06-25 14:55:30 -0700416 /* Processing backup -> verify if we can survive this */
417 u64 z_mcic, o_mcic, t_mcic;
Heiko Carstens77fa2242005-06-25 14:55:30 -0700418 z_mcic = (1ULL<<63 | 1ULL<<59 | 1ULL<<29);
419 o_mcic = (1ULL<<43 | 1ULL<<42 | 1ULL<<41 | 1ULL<<40 |
420 1ULL<<36 | 1ULL<<35 | 1ULL<<34 | 1ULL<<32 |
421 1ULL<<30 | 1ULL<<21 | 1ULL<<20 | 1ULL<<17 |
422 1ULL<<16);
Heiko Carstensdc6e1552015-10-12 13:00:39 +0200423 t_mcic = mci.val;
Heiko Carstens77fa2242005-06-25 14:55:30 -0700424
425 if (((t_mcic & z_mcic) != 0) ||
426 ((t_mcic & o_mcic) != o_mcic)) {
Heiko Carstens3d682862015-10-12 12:39:09 +0200427 s390_handle_damage();
Heiko Carstens77fa2242005-06-25 14:55:30 -0700428 }
Heiko Carstensb73d40c2006-04-27 18:40:23 -0700429
430 /*
431 * Nullifying exigent condition, therefore we might
432 * retry this instruction.
433 */
Heiko Carstensb73d40c2006-04-27 18:40:23 -0700434 spin_lock(&ipd_lock);
Heiko Carstens1aae0562013-01-30 09:49:40 +0100435 tmp = get_tod_clock();
Heiko Carstensb73d40c2006-04-27 18:40:23 -0700436 if (((tmp - last_ipd) >> 12) < MAX_IPD_TIME)
437 ipd_count++;
438 else
439 ipd_count = 1;
Heiko Carstensb73d40c2006-04-27 18:40:23 -0700440 last_ipd = tmp;
Heiko Carstensb73d40c2006-04-27 18:40:23 -0700441 if (ipd_count == MAX_IPD_COUNT)
Heiko Carstens3d682862015-10-12 12:39:09 +0200442 s390_handle_damage();
Heiko Carstensb73d40c2006-04-27 18:40:23 -0700443 spin_unlock(&ipd_lock);
Heiko Carstensf5daba12009-03-26 15:24:01 +0100444 } else {
Heiko Carstens77fa2242005-06-25 14:55:30 -0700445 /* Processing damage -> stopping machine */
Heiko Carstens3d682862015-10-12 12:39:09 +0200446 s390_handle_damage();
Heiko Carstens77fa2242005-06-25 14:55:30 -0700447 }
448 }
Alexander Gordeev5fa2ea02021-06-18 08:17:18 +0200449 if (s390_validate_registers(mci, user_mode(regs))) {
Martin Schwidefsky8f149ea2016-08-22 14:40:06 +0200450 /*
451 * Couldn't restore all register contents for the
452 * user space process -> mark task for termination.
453 */
454 mcck->kill_task = 1;
455 mcck->mcck_code = mci.val;
Sven Schnelle0b0ed652020-02-20 12:09:36 +0100456 mcck_pending = 1;
Heiko Carstens77fa2242005-06-25 14:55:30 -0700457 }
QingFeng Haoda72ca42017-06-07 11:41:19 +0200458
459 /*
460 * Backup the machine check's info if it happens when the guest
461 * is running.
462 */
463 if (test_cpu_flag(CIF_MCCK_GUEST))
464 s390_backup_mcck_info(regs);
465
Heiko Carstensdc6e1552015-10-12 13:00:39 +0200466 if (mci.cd) {
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100467 /* Timing facility damage */
Heiko Carstens3d682862015-10-12 12:39:09 +0200468 s390_handle_damage();
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100469 }
Heiko Carstensdc6e1552015-10-12 13:00:39 +0200470 if (mci.ed && mci.ec) {
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100471 /* External damage */
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200472 if (S390_lowcore.external_damage_code & (1U << ED_STP_SYNC))
Heiko Carstens29b0a822015-10-09 13:48:03 +0200473 mcck->stp_queue |= stp_sync_check();
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200474 if (S390_lowcore.external_damage_code & (1U << ED_STP_ISLAND))
Heiko Carstens29b0a822015-10-09 13:48:03 +0200475 mcck->stp_queue |= stp_island_check();
Sven Schnelle0b0ed652020-02-20 12:09:36 +0100476 mcck_pending = 1;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100477 }
QingFeng Haoc9295002017-06-07 11:30:42 +0200478
Heiko Carstensdc6e1552015-10-12 13:00:39 +0200479 if (mci.cp) {
Heiko Carstens77fa2242005-06-25 14:55:30 -0700480 /* Channel report word pending */
481 mcck->channel_report = 1;
Sven Schnelle0b0ed652020-02-20 12:09:36 +0100482 mcck_pending = 1;
Heiko Carstens77fa2242005-06-25 14:55:30 -0700483 }
Heiko Carstensdc6e1552015-10-12 13:00:39 +0200484 if (mci.w) {
Heiko Carstens77fa2242005-06-25 14:55:30 -0700485 /* Warning pending */
486 mcck->warning = 1;
Sven Schnelle0b0ed652020-02-20 12:09:36 +0100487 mcck_pending = 1;
Heiko Carstens77fa2242005-06-25 14:55:30 -0700488 }
QingFeng Haoc9295002017-06-07 11:30:42 +0200489
490 /*
491 * If there are only Channel Report Pending and External Damage
492 * machine checks, they will not be reinjected into the guest
493 * because they refer to host conditions only.
494 */
495 mcck_dam_code = (mci.val & MCIC_SUBCLASS_MASK);
496 if (test_cpu_flag(CIF_MCCK_GUEST) &&
497 (mcck_dam_code & MCCK_CODE_NO_GUEST) != mcck_dam_code) {
498 /* Set exit reason code for host's later handling */
499 *((long *)(regs->gprs[15] + __SF_SIE_REASON)) = -EINTR;
500 }
501 clear_cpu_flag(CIF_MCCK_GUEST);
Sven Schnelle0b0ed652020-02-20 12:09:36 +0100502
503 if (user_mode(regs) && mcck_pending) {
504 nmi_exit();
505 return 1;
506 }
507
508 if (mcck_pending)
509 schedule_mcck_handler();
510
Heiko Carstens81f64b82009-04-14 15:36:18 +0200511 nmi_exit();
Sven Schnelle0b0ed652020-02-20 12:09:36 +0100512 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
Martin Schwidefsky00a8f882017-09-15 16:24:31 +0200514NOKPROBE_SYMBOL(s390_do_machine_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Heiko Carstensf5daba12009-03-26 15:24:01 +0100516static int __init machine_check_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100518 ctl_set_bit(14, 25); /* enable external damage MCH */
Heiko Carstensf5daba12009-03-26 15:24:01 +0100519 ctl_set_bit(14, 27); /* enable system recovery MCH */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 ctl_set_bit(14, 24); /* enable warning MCH */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return 0;
522}
Heiko Carstens24d05ff2015-08-17 08:09:17 +0200523early_initcall(machine_check_init);