xref: /linux/arch/s390/kernel/nmi.c (revision 036cbbafbd1eb2a7437912c2fe5fdefc9d52c309)
1a17ae4c3SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2f5daba1dSHeiko Carstens /*
3f5daba1dSHeiko Carstens  *   Machine check handler
4f5daba1dSHeiko Carstens  *
5f5daba1dSHeiko Carstens  *    Copyright IBM Corp. 2000, 2009
6f5daba1dSHeiko Carstens  *    Author(s): Ingo Adlung <adlung@de.ibm.com>,
7f5daba1dSHeiko Carstens  *		 Martin Schwidefsky <schwidefsky@de.ibm.com>,
8f5daba1dSHeiko Carstens  *		 Cornelia Huck <cornelia.huck@de.ibm.com>,
9f5daba1dSHeiko Carstens  */
10f5daba1dSHeiko Carstens 
11052ff461SHeiko Carstens #include <linux/kernel_stat.h>
12f5daba1dSHeiko Carstens #include <linux/init.h>
13f5daba1dSHeiko Carstens #include <linux/errno.h>
14520763a3SSven Schnelle #include <linux/entry-common.h>
1581f64b87SHeiko Carstens #include <linux/hardirq.h>
166c81511cSMartin Schwidefsky #include <linux/log2.h>
1700a8f886SMartin Schwidefsky #include <linux/kprobes.h>
18514c6032SRandy Dunlap #include <linux/kmemleak.h>
19f5daba1dSHeiko Carstens #include <linux/time.h>
203f07c014SIngo Molnar #include <linux/module.h>
213f07c014SIngo Molnar #include <linux/sched/signal.h>
225720aab2SHeiko Carstens #include <linux/kvm_host.h>
233994a52bSPaul Gortmaker #include <linux/export.h>
24f5daba1dSHeiko Carstens #include <asm/lowcore.h>
25ebe1cd53SHeiko Carstens #include <asm/ctlreg.h>
26fd2527f2SHeiko Carstens #include <asm/fpu.h>
27f5daba1dSHeiko Carstens #include <asm/smp.h>
28fd5ada04SMartin Schwidefsky #include <asm/stp.h>
2976d4e00aSMartin Schwidefsky #include <asm/cputime.h>
30f5daba1dSHeiko Carstens #include <asm/nmi.h>
31f5daba1dSHeiko Carstens #include <asm/crw.h>
32c929500dSQingFeng Hao #include <asm/asm-offsets.h>
3339d62336SThomas Richter #include <asm/pai.h>
34*036cbbafSAlexander Gordeev #include <asm/vtime.h>
35f5daba1dSHeiko Carstens 
36f5daba1dSHeiko Carstens struct mcck_struct {
3736324963SHeiko Carstens 	unsigned int kill_task : 1;
3836324963SHeiko Carstens 	unsigned int channel_report : 1;
3936324963SHeiko Carstens 	unsigned int warning : 1;
4029b0a825SHeiko Carstens 	unsigned int stp_queue : 1;
41dc6e1555SHeiko Carstens 	unsigned long mcck_code;
42f5daba1dSHeiko Carstens };
43f5daba1dSHeiko Carstens 
44f5daba1dSHeiko Carstens static DEFINE_PER_CPU(struct mcck_struct, cpu_mcck);
456c81511cSMartin Schwidefsky 
466c81511cSMartin Schwidefsky static inline int nmi_needs_mcesa(void)
476c81511cSMartin Schwidefsky {
4818564756SHeiko Carstens 	return cpu_has_vx() || MACHINE_HAS_GS;
496c81511cSMartin Schwidefsky }
506c81511cSMartin Schwidefsky 
516c81511cSMartin Schwidefsky /*
526c81511cSMartin Schwidefsky  * The initial machine check extended save area for the boot CPU.
53c7ed509bSAlexander Gordeev  * It will be replaced on the boot CPU reinit with an allocated
54c7ed509bSAlexander Gordeev  * structure. The structure is required for machine check happening
55c7ed509bSAlexander Gordeev  * early in the boot process.
566c81511cSMartin Schwidefsky  */
5712dd19c1SAlexander Gordeev static struct mcesa boot_mcesa __aligned(MCESA_MAX_SIZE);
586c81511cSMartin Schwidefsky 
59c7ed509bSAlexander Gordeev void __init nmi_alloc_mcesa_early(u64 *mcesad)
606c81511cSMartin Schwidefsky {
616c81511cSMartin Schwidefsky 	if (!nmi_needs_mcesa())
626c81511cSMartin Schwidefsky 		return;
63c7ed509bSAlexander Gordeev 	*mcesad = __pa(&boot_mcesa);
646c81511cSMartin Schwidefsky 	if (MACHINE_HAS_GS)
65c7ed509bSAlexander Gordeev 		*mcesad |= ilog2(MCESA_MAX_SIZE);
666c81511cSMartin Schwidefsky }
676c81511cSMartin Schwidefsky 
68bb3860ccSHeiko Carstens int nmi_alloc_mcesa(u64 *mcesad)
696c81511cSMartin Schwidefsky {
70c7ed509bSAlexander Gordeev 	unsigned long size;
71bb3860ccSHeiko Carstens 	void *origin;
726c81511cSMartin Schwidefsky 
73c7ed509bSAlexander Gordeev 	*mcesad = 0;
746c81511cSMartin Schwidefsky 	if (!nmi_needs_mcesa())
756c81511cSMartin Schwidefsky 		return 0;
76bb3860ccSHeiko Carstens 	size = MACHINE_HAS_GS ? MCESA_MAX_SIZE : MCESA_MIN_SIZE;
77bb3860ccSHeiko Carstens 	origin = kmalloc(size, GFP_KERNEL);
786c81511cSMartin Schwidefsky 	if (!origin)
796c81511cSMartin Schwidefsky 		return -ENOMEM;
806c81511cSMartin Schwidefsky 	/* The pointer is stored with mcesa_bits ORed in */
81bb3860ccSHeiko Carstens 	kmemleak_not_leak(origin);
82bb3860ccSHeiko Carstens 	*mcesad = __pa(origin);
83bb3860ccSHeiko Carstens 	if (MACHINE_HAS_GS)
84bb3860ccSHeiko Carstens 		*mcesad |= ilog2(MCESA_MAX_SIZE);
856c81511cSMartin Schwidefsky 	return 0;
866c81511cSMartin Schwidefsky }
876c81511cSMartin Schwidefsky 
88c7ed509bSAlexander Gordeev void nmi_free_mcesa(u64 *mcesad)
896c81511cSMartin Schwidefsky {
906c81511cSMartin Schwidefsky 	if (!nmi_needs_mcesa())
916c81511cSMartin Schwidefsky 		return;
92bb3860ccSHeiko Carstens 	kfree(__va(*mcesad & MCESA_ORIGIN_MASK));
936c81511cSMartin Schwidefsky }
94f5daba1dSHeiko Carstens 
95506faa5bSHeiko Carstens static __always_inline char *nmi_puts(char *dest, const char *src)
96506faa5bSHeiko Carstens {
97506faa5bSHeiko Carstens 	while (*src)
98506faa5bSHeiko Carstens 		*dest++ = *src++;
99506faa5bSHeiko Carstens 	*dest = 0;
100506faa5bSHeiko Carstens 	return dest;
101506faa5bSHeiko Carstens }
102506faa5bSHeiko Carstens 
103506faa5bSHeiko Carstens static __always_inline char *u64_to_hex(char *dest, u64 val)
104506faa5bSHeiko Carstens {
105506faa5bSHeiko Carstens 	int i, num;
106506faa5bSHeiko Carstens 
107506faa5bSHeiko Carstens 	for (i = 1; i <= 16; i++) {
108506faa5bSHeiko Carstens 		num = (val >> (64 - 4 * i)) & 0xf;
109506faa5bSHeiko Carstens 		if (num >= 10)
110506faa5bSHeiko Carstens 			*dest++ = 'A' + num - 10;
111506faa5bSHeiko Carstens 		else
112506faa5bSHeiko Carstens 			*dest++ = '0' + num;
113506faa5bSHeiko Carstens 	}
114506faa5bSHeiko Carstens 	*dest = 0;
115506faa5bSHeiko Carstens 	return dest;
116506faa5bSHeiko Carstens }
117506faa5bSHeiko Carstens 
11800a8f886SMartin Schwidefsky static notrace void s390_handle_damage(void)
119f5daba1dSHeiko Carstens {
120506faa5bSHeiko Carstens 	union ctlreg0 cr0, cr0_new;
121506faa5bSHeiko Carstens 	char message[100];
122506faa5bSHeiko Carstens 	psw_t psw_save;
123506faa5bSHeiko Carstens 	char *ptr;
124506faa5bSHeiko Carstens 
12500a8f886SMartin Schwidefsky 	smp_emergency_stop();
126506faa5bSHeiko Carstens 	diag_amode31_ops.diag308_reset();
127506faa5bSHeiko Carstens 	ptr = nmi_puts(message, "System stopped due to unrecoverable machine check, code: 0x");
128506faa5bSHeiko Carstens 	u64_to_hex(ptr, S390_lowcore.mcck_interruption_code);
129506faa5bSHeiko Carstens 
130506faa5bSHeiko Carstens 	/*
131506faa5bSHeiko Carstens 	 * Disable low address protection and make machine check new PSW a
132506faa5bSHeiko Carstens 	 * disabled wait PSW. Any additional machine check cannot be handled.
133506faa5bSHeiko Carstens 	 */
134527618abSHeiko Carstens 	local_ctl_store(0, &cr0.reg);
135506faa5bSHeiko Carstens 	cr0_new = cr0;
136506faa5bSHeiko Carstens 	cr0_new.lap = 0;
137527618abSHeiko Carstens 	local_ctl_load(0, &cr0_new.reg);
138506faa5bSHeiko Carstens 	psw_save = S390_lowcore.mcck_new_psw;
139506faa5bSHeiko Carstens 	psw_bits(S390_lowcore.mcck_new_psw).io = 0;
140506faa5bSHeiko Carstens 	psw_bits(S390_lowcore.mcck_new_psw).ext = 0;
141506faa5bSHeiko Carstens 	psw_bits(S390_lowcore.mcck_new_psw).wait = 1;
142506faa5bSHeiko Carstens 	sclp_emergency_printk(message);
143506faa5bSHeiko Carstens 
144506faa5bSHeiko Carstens 	/*
145506faa5bSHeiko Carstens 	 * Restore machine check new PSW and control register 0 to original
146506faa5bSHeiko Carstens 	 * values. This makes possible system dump analysis easier.
147506faa5bSHeiko Carstens 	 */
148506faa5bSHeiko Carstens 	S390_lowcore.mcck_new_psw = psw_save;
149527618abSHeiko Carstens 	local_ctl_load(0, &cr0.reg);
15098587c2dSMartin Schwidefsky 	disabled_wait();
151f5daba1dSHeiko Carstens 	while (1);
152f5daba1dSHeiko Carstens }
15300a8f886SMartin Schwidefsky NOKPROBE_SYMBOL(s390_handle_damage);
154f5daba1dSHeiko Carstens 
155f5daba1dSHeiko Carstens /*
15656e62a73SSven Schnelle  * Main machine check handler function. Will be called with interrupts disabled
15756e62a73SSven Schnelle  * and machine checks enabled.
158f5daba1dSHeiko Carstens  */
159e7ec1d2eSAlexander Gordeev void s390_handle_mcck(void)
160f5daba1dSHeiko Carstens {
161f5daba1dSHeiko Carstens 	struct mcck_struct mcck;
1621c8b8cf2SHeiko Carstens 	unsigned long mflags;
163f5daba1dSHeiko Carstens 
164f5daba1dSHeiko Carstens 	/*
165f5daba1dSHeiko Carstens 	 * Disable machine checks and get the current state of accumulated
166f5daba1dSHeiko Carstens 	 * machine checks. Afterwards delete the old state and enable machine
167f5daba1dSHeiko Carstens 	 * checks again.
168f5daba1dSHeiko Carstens 	 */
1691c8b8cf2SHeiko Carstens 	local_mcck_save(mflags);
1702cb4a182SSebastian Ott 	mcck = *this_cpu_ptr(&cpu_mcck);
1712cb4a182SSebastian Ott 	memset(this_cpu_ptr(&cpu_mcck), 0, sizeof(mcck));
1721c8b8cf2SHeiko Carstens 	local_mcck_restore(mflags);
173f5daba1dSHeiko Carstens 
174f5daba1dSHeiko Carstens 	if (mcck.channel_report)
175f5daba1dSHeiko Carstens 		crw_handle_channel_report();
176f5daba1dSHeiko Carstens 	/*
1777b886416SHeiko Carstens 	 * A warning may remain for a prolonged period on the bare iron.
1787b886416SHeiko Carstens 	 * (actually until the machine is powered off, or the problem is gone)
1797b886416SHeiko Carstens 	 * So we just stop listening for the WARNING MCH and avoid continuously
180f5daba1dSHeiko Carstens 	 * being interrupted.  One caveat is however, that we must do this per
181f5daba1dSHeiko Carstens 	 * processor and cannot use the smp version of ctl_clear_bit().
182f5daba1dSHeiko Carstens 	 * On VM we only get one interrupt per virtally presented machinecheck.
1837b886416SHeiko Carstens 	 * Though one suffices, we may get one interrupt per (virtual) cpu.
184f5daba1dSHeiko Carstens 	 */
185f5daba1dSHeiko Carstens 	if (mcck.warning) {	/* WARNING pending ? */
186f5daba1dSHeiko Carstens 		static int mchchk_wng_posted = 0;
1877b886416SHeiko Carstens 
1887b886416SHeiko Carstens 		/* Use single cpu clear, as we cannot handle smp here. */
18999441a38SHeiko Carstens 		local_ctl_clear_bit(14, CR14_WARNING_SUBMASK_BIT);
190f5daba1dSHeiko Carstens 		if (xchg(&mchchk_wng_posted, 1) == 0)
191f5daba1dSHeiko Carstens 			kill_cad_pid(SIGPWR, 1);
192f5daba1dSHeiko Carstens 	}
19329b0a825SHeiko Carstens 	if (mcck.stp_queue)
19429b0a825SHeiko Carstens 		stp_queue_work();
195f5daba1dSHeiko Carstens 	if (mcck.kill_task) {
196f5daba1dSHeiko Carstens 		printk(KERN_EMERG "mcck: Terminating task because of machine "
197dc6e1555SHeiko Carstens 		       "malfunction (code 0x%016lx).\n", mcck.mcck_code);
198f5daba1dSHeiko Carstens 		printk(KERN_EMERG "mcck: task: %s, pid: %d.\n",
199f5daba1dSHeiko Carstens 		       current->comm, current->pid);
200e7ec1d2eSAlexander Gordeev 		if (is_global_init(current))
201e7ec1d2eSAlexander Gordeev 			panic("mcck: Attempting to kill init!\n");
202e7ec1d2eSAlexander Gordeev 		do_send_sig_info(SIGKILL, SEND_SIG_PRIV, current, PIDTYPE_PID);
203f5daba1dSHeiko Carstens 	}
204f5daba1dSHeiko Carstens }
205f5daba1dSHeiko Carstens 
2069e96afabSHeiko Carstens /**
2079e96afabSHeiko Carstens  * nmi_registers_valid - verify if registers are valid
2089e96afabSHeiko Carstens  * @mci: machine check interruption code
2099e96afabSHeiko Carstens  *
2109e96afabSHeiko Carstens  * Inspect a machine check interruption code and verify if all required
2119e96afabSHeiko Carstens  * registers are valid. For some registers the corresponding validity bit is
2129e96afabSHeiko Carstens  * ignored and the registers are set to the expected value.
2139e96afabSHeiko Carstens  * Returns true if all registers are valid, otherwise false.
214f5daba1dSHeiko Carstens  */
2159e96afabSHeiko Carstens static bool notrace nmi_registers_valid(union mci mci)
216f5daba1dSHeiko Carstens {
217ad3bc0acSMartin Schwidefsky 	union ctlreg2 cr2;
218f5daba1dSHeiko Carstens 
2199e96afabSHeiko Carstens 	/*
2209e96afabSHeiko Carstens 	 * The getcpu vdso syscall reads the CPU number from the programmable
2219e96afabSHeiko Carstens 	 * field of the TOD clock. Disregard the TOD programmable register
2229e96afabSHeiko Carstens 	 * validity bit and load the CPU number into the TOD programmable field
2239e96afabSHeiko Carstens 	 * unconditionally.
2249e96afabSHeiko Carstens 	 */
2259e96afabSHeiko Carstens 	set_tod_programmable_field(raw_smp_processor_id());
2269e96afabSHeiko Carstens 	/*
2279e96afabSHeiko Carstens 	 * Set the clock comparator register to the next expected value.
2289e96afabSHeiko Carstens 	 */
2299e96afabSHeiko Carstens 	set_clock_comparator(S390_lowcore.clock_comparator);
2309e96afabSHeiko Carstens 	if (!mci.gr || !mci.fp || !mci.fc)
2319e96afabSHeiko Carstens 		return false;
232f094a39cSChristian Borntraeger 	/*
233f094a39cSChristian Borntraeger 	 * The vector validity must only be checked if not running a
234f094a39cSChristian Borntraeger 	 * KVM guest. For KVM guests the machine check is forwarded by
235f094a39cSChristian Borntraeger 	 * KVM and it is the responsibility of the guest to take
236f094a39cSChristian Borntraeger 	 * appropriate actions. The host vector or FPU values have been
237f094a39cSChristian Borntraeger 	 * saved by KVM and will be restored by KVM.
238f094a39cSChristian Borntraeger 	 */
239f9e5938aSHeiko Carstens 	if (!mci.vr && !test_cpu_flag(CIF_MCCK_GUEST))
2409e96afabSHeiko Carstens 		return false;
241f9e5938aSHeiko Carstens 	if (!mci.ar)
2429e96afabSHeiko Carstens 		return false;
243916cda1aSMartin Schwidefsky 	/*
2449e96afabSHeiko Carstens 	 * Two cases for guarded storage registers:
2451ea1d6a8SChristian Borntraeger 	 * - machine check in kernel or userspace
2461ea1d6a8SChristian Borntraeger 	 * - machine check while running SIE (KVM guest)
2479e96afabSHeiko Carstens 	 * For kernel or userspace the userspace values of guarded storage
2489e96afabSHeiko Carstens 	 * control can not be recreated, the process must be terminated.
2499e96afabSHeiko Carstens 	 * For SIE the guest values of guarded storage can not be recreated.
2509e96afabSHeiko Carstens 	 * This is either due to a bug or due to GS being disabled in the
2519e96afabSHeiko Carstens 	 * guest. The guest will be notified by KVM code and the guests machine
2529e96afabSHeiko Carstens 	 * check handling must take care of this. The host values are saved by
2539e96afabSHeiko Carstens 	 * KVM and are not affected.
254916cda1aSMartin Schwidefsky 	 */
2559e96afabSHeiko Carstens 	cr2.reg = S390_lowcore.cregs_save_area[2];
2569e96afabSHeiko Carstens 	if (cr2.gse && !mci.gs && !test_cpu_flag(CIF_MCCK_GUEST))
2579e96afabSHeiko Carstens 		return false;
258dc6e1555SHeiko Carstens 	if (!mci.ms || !mci.pm || !mci.ia)
2599e96afabSHeiko Carstens 		return false;
2609e96afabSHeiko Carstens 	return true;
261f5daba1dSHeiko Carstens }
2629e96afabSHeiko Carstens NOKPROBE_SYMBOL(nmi_registers_valid);
263f5daba1dSHeiko Carstens 
264da72ca4dSQingFeng Hao /*
265da72ca4dSQingFeng Hao  * Backup the guest's machine check info to its description block
266da72ca4dSQingFeng Hao  */
267da72ca4dSQingFeng Hao static void notrace s390_backup_mcck_info(struct pt_regs *regs)
268da72ca4dSQingFeng Hao {
269da72ca4dSQingFeng Hao 	struct mcck_volatile_info *mcck_backup;
270da72ca4dSQingFeng Hao 	struct sie_page *sie_page;
271da72ca4dSQingFeng Hao 
272da72ca4dSQingFeng Hao 	/* r14 contains the sie block, which was set in sie64a */
273ae4b60f6SNico Boehr 	struct kvm_s390_sie_block *sie_block = phys_to_virt(regs->gprs[14]);
274da72ca4dSQingFeng Hao 
275da72ca4dSQingFeng Hao 	if (sie_block == NULL)
276da72ca4dSQingFeng Hao 		/* Something's seriously wrong, stop system. */
277da72ca4dSQingFeng Hao 		s390_handle_damage();
278da72ca4dSQingFeng Hao 
279da72ca4dSQingFeng Hao 	sie_page = container_of(sie_block, struct sie_page, sie_block);
280da72ca4dSQingFeng Hao 	mcck_backup = &sie_page->mcck_info;
281da72ca4dSQingFeng Hao 	mcck_backup->mcic = S390_lowcore.mcck_interruption_code &
282da72ca4dSQingFeng Hao 				~(MCCK_CODE_CP | MCCK_CODE_EXT_DAMAGE);
283da72ca4dSQingFeng Hao 	mcck_backup->ext_damage_code = S390_lowcore.external_damage_code;
284da72ca4dSQingFeng Hao 	mcck_backup->failing_storage_address
285da72ca4dSQingFeng Hao 			= S390_lowcore.failing_storage_address;
286da72ca4dSQingFeng Hao }
28700a8f886SMartin Schwidefsky NOKPROBE_SYMBOL(s390_backup_mcck_info);
288da72ca4dSQingFeng Hao 
289f5daba1dSHeiko Carstens #define MAX_IPD_COUNT	29
290f5daba1dSHeiko Carstens #define MAX_IPD_TIME	(5 * 60 * USEC_PER_SEC) /* 5 minutes */
291f5daba1dSHeiko Carstens 
292f5daba1dSHeiko Carstens #define ED_STP_ISLAND	6	/* External damage STP island check */
293f5daba1dSHeiko Carstens #define ED_STP_SYNC	7	/* External damage STP sync check */
294f5daba1dSHeiko Carstens 
295c929500dSQingFeng Hao #define MCCK_CODE_NO_GUEST	(MCCK_CODE_CP | MCCK_CODE_EXT_DAMAGE)
296c929500dSQingFeng Hao 
297f5daba1dSHeiko Carstens /*
298f5daba1dSHeiko Carstens  * machine check handler.
299f5daba1dSHeiko Carstens  */
300e7ec1d2eSAlexander Gordeev void notrace s390_do_machine_check(struct pt_regs *regs)
301f5daba1dSHeiko Carstens {
302f5daba1dSHeiko Carstens 	static int ipd_count;
303f5daba1dSHeiko Carstens 	static DEFINE_SPINLOCK(ipd_lock);
304f5daba1dSHeiko Carstens 	static unsigned long long last_ipd;
305f5daba1dSHeiko Carstens 	struct mcck_struct *mcck;
306f5daba1dSHeiko Carstens 	unsigned long long tmp;
307520763a3SSven Schnelle 	irqentry_state_t irq_state;
308dc6e1555SHeiko Carstens 	union mci mci;
309c929500dSQingFeng Hao 	unsigned long mcck_dam_code;
3100b0ed657SSven Schnelle 	int mcck_pending = 0;
311f5daba1dSHeiko Carstens 
312520763a3SSven Schnelle 	irq_state = irqentry_nmi_enter(regs);
31356e62a73SSven Schnelle 
31456e62a73SSven Schnelle 	if (user_mode(regs))
31556e62a73SSven Schnelle 		update_timer_mcck();
316420f42ecSHeiko Carstens 	inc_irq_stat(NMI_NMI);
317dc6e1555SHeiko Carstens 	mci.val = S390_lowcore.mcck_interruption_code;
318eb7e7d76SChristoph Lameter 	mcck = this_cpu_ptr(&cpu_mcck);
319f5daba1dSHeiko Carstens 
320c929500dSQingFeng Hao 	/*
321c929500dSQingFeng Hao 	 * Reinject the instruction processing damages' machine checks
322c929500dSQingFeng Hao 	 * including Delayed Access Exception into the guest
323c929500dSQingFeng Hao 	 * instead of damaging the host if they happen in the guest.
324c929500dSQingFeng Hao 	 */
325c929500dSQingFeng Hao 	if (mci.pd && !test_cpu_flag(CIF_MCCK_GUEST)) {
326dc6e1555SHeiko Carstens 		if (mci.b) {
327f5daba1dSHeiko Carstens 			/* Processing backup -> verify if we can survive this */
328f5daba1dSHeiko Carstens 			u64 z_mcic, o_mcic, t_mcic;
329f5daba1dSHeiko Carstens 			z_mcic = (1ULL<<63 | 1ULL<<59 | 1ULL<<29);
330f5daba1dSHeiko Carstens 			o_mcic = (1ULL<<43 | 1ULL<<42 | 1ULL<<41 | 1ULL<<40 |
331f5daba1dSHeiko Carstens 				  1ULL<<36 | 1ULL<<35 | 1ULL<<34 | 1ULL<<32 |
332f5daba1dSHeiko Carstens 				  1ULL<<30 | 1ULL<<21 | 1ULL<<20 | 1ULL<<17 |
333f5daba1dSHeiko Carstens 				  1ULL<<16);
334dc6e1555SHeiko Carstens 			t_mcic = mci.val;
335f5daba1dSHeiko Carstens 
336f5daba1dSHeiko Carstens 			if (((t_mcic & z_mcic) != 0) ||
337f5daba1dSHeiko Carstens 			    ((t_mcic & o_mcic) != o_mcic)) {
3383d68286aSHeiko Carstens 				s390_handle_damage();
339f5daba1dSHeiko Carstens 			}
340f5daba1dSHeiko Carstens 
341f5daba1dSHeiko Carstens 			/*
342f5daba1dSHeiko Carstens 			 * Nullifying exigent condition, therefore we might
343f5daba1dSHeiko Carstens 			 * retry this instruction.
344f5daba1dSHeiko Carstens 			 */
345f5daba1dSHeiko Carstens 			spin_lock(&ipd_lock);
3461aae0560SHeiko Carstens 			tmp = get_tod_clock();
347f5daba1dSHeiko Carstens 			if (((tmp - last_ipd) >> 12) < MAX_IPD_TIME)
348f5daba1dSHeiko Carstens 				ipd_count++;
349f5daba1dSHeiko Carstens 			else
350f5daba1dSHeiko Carstens 				ipd_count = 1;
351f5daba1dSHeiko Carstens 			last_ipd = tmp;
352f5daba1dSHeiko Carstens 			if (ipd_count == MAX_IPD_COUNT)
3533d68286aSHeiko Carstens 				s390_handle_damage();
354f5daba1dSHeiko Carstens 			spin_unlock(&ipd_lock);
355f5daba1dSHeiko Carstens 		} else {
356f5daba1dSHeiko Carstens 			/* Processing damage -> stopping machine */
3573d68286aSHeiko Carstens 			s390_handle_damage();
358f5daba1dSHeiko Carstens 		}
359f5daba1dSHeiko Carstens 	}
3609e96afabSHeiko Carstens 	if (!nmi_registers_valid(mci)) {
361f9e5938aSHeiko Carstens 		if (!user_mode(regs))
362f9e5938aSHeiko Carstens 			s390_handle_damage();
363f5daba1dSHeiko Carstens 		/*
3648f149ea6SMartin Schwidefsky 		 * Couldn't restore all register contents for the
3658f149ea6SMartin Schwidefsky 		 * user space process -> mark task for termination.
366f5daba1dSHeiko Carstens 		 */
367f5daba1dSHeiko Carstens 		mcck->kill_task = 1;
368dc6e1555SHeiko Carstens 		mcck->mcck_code = mci.val;
3690b0ed657SSven Schnelle 		mcck_pending = 1;
370f5daba1dSHeiko Carstens 	}
371da72ca4dSQingFeng Hao 
372da72ca4dSQingFeng Hao 	/*
373da72ca4dSQingFeng Hao 	 * Backup the machine check's info if it happens when the guest
374da72ca4dSQingFeng Hao 	 * is running.
375da72ca4dSQingFeng Hao 	 */
376da72ca4dSQingFeng Hao 	if (test_cpu_flag(CIF_MCCK_GUEST))
377da72ca4dSQingFeng Hao 		s390_backup_mcck_info(regs);
378da72ca4dSQingFeng Hao 
379dc6e1555SHeiko Carstens 	if (mci.cd) {
380f5daba1dSHeiko Carstens 		/* Timing facility damage */
3813d68286aSHeiko Carstens 		s390_handle_damage();
382f5daba1dSHeiko Carstens 	}
383dc6e1555SHeiko Carstens 	if (mci.ed && mci.ec) {
384f5daba1dSHeiko Carstens 		/* External damage */
385f5daba1dSHeiko Carstens 		if (S390_lowcore.external_damage_code & (1U << ED_STP_SYNC))
38629b0a825SHeiko Carstens 			mcck->stp_queue |= stp_sync_check();
387f5daba1dSHeiko Carstens 		if (S390_lowcore.external_damage_code & (1U << ED_STP_ISLAND))
38829b0a825SHeiko Carstens 			mcck->stp_queue |= stp_island_check();
3890b0ed657SSven Schnelle 		mcck_pending = 1;
390f5daba1dSHeiko Carstens 	}
391742aed05SHeiko Carstens 	/*
392742aed05SHeiko Carstens 	 * Reinject storage related machine checks into the guest if they
393742aed05SHeiko Carstens 	 * happen when the guest is running.
394742aed05SHeiko Carstens 	 */
395742aed05SHeiko Carstens 	if (!test_cpu_flag(CIF_MCCK_GUEST)) {
396742aed05SHeiko Carstens 		/* Storage error uncorrected */
397742aed05SHeiko Carstens 		if (mci.se)
398742aed05SHeiko Carstens 			s390_handle_damage();
399742aed05SHeiko Carstens 		/* Storage key-error uncorrected */
400742aed05SHeiko Carstens 		if (mci.ke)
401742aed05SHeiko Carstens 			s390_handle_damage();
402742aed05SHeiko Carstens 		/* Storage degradation */
403742aed05SHeiko Carstens 		if (mci.ds && mci.fa)
404742aed05SHeiko Carstens 			s390_handle_damage();
405742aed05SHeiko Carstens 	}
406dc6e1555SHeiko Carstens 	if (mci.cp) {
407f5daba1dSHeiko Carstens 		/* Channel report word pending */
408f5daba1dSHeiko Carstens 		mcck->channel_report = 1;
4090b0ed657SSven Schnelle 		mcck_pending = 1;
410f5daba1dSHeiko Carstens 	}
411dc6e1555SHeiko Carstens 	if (mci.w) {
412f5daba1dSHeiko Carstens 		/* Warning pending */
413f5daba1dSHeiko Carstens 		mcck->warning = 1;
4140b0ed657SSven Schnelle 		mcck_pending = 1;
415f5daba1dSHeiko Carstens 	}
416c929500dSQingFeng Hao 
417c929500dSQingFeng Hao 	/*
418c929500dSQingFeng Hao 	 * If there are only Channel Report Pending and External Damage
419c929500dSQingFeng Hao 	 * machine checks, they will not be reinjected into the guest
420c929500dSQingFeng Hao 	 * because they refer to host conditions only.
421c929500dSQingFeng Hao 	 */
422c929500dSQingFeng Hao 	mcck_dam_code = (mci.val & MCIC_SUBCLASS_MASK);
423c929500dSQingFeng Hao 	if (test_cpu_flag(CIF_MCCK_GUEST) &&
424c929500dSQingFeng Hao 	(mcck_dam_code & MCCK_CODE_NO_GUEST) != mcck_dam_code) {
425c929500dSQingFeng Hao 		/* Set exit reason code for host's later handling */
426c929500dSQingFeng Hao 		*((long *)(regs->gprs[15] + __SF_SIE_REASON)) = -EINTR;
427c929500dSQingFeng Hao 	}
428c929500dSQingFeng Hao 	clear_cpu_flag(CIF_MCCK_GUEST);
4290b0ed657SSven Schnelle 
4300b0ed657SSven Schnelle 	if (mcck_pending)
4310b0ed657SSven Schnelle 		schedule_mcck_handler();
4320b0ed657SSven Schnelle 
433520763a3SSven Schnelle 	irqentry_nmi_exit(regs, irq_state);
434f5daba1dSHeiko Carstens }
43500a8f886SMartin Schwidefsky NOKPROBE_SYMBOL(s390_do_machine_check);
436f5daba1dSHeiko Carstens 
437f5daba1dSHeiko Carstens static int __init machine_check_init(void)
438f5daba1dSHeiko Carstens {
43999441a38SHeiko Carstens 	system_ctl_set_bit(14, CR14_EXTERNAL_DAMAGE_SUBMASK_BIT);
44099441a38SHeiko Carstens 	system_ctl_set_bit(14, CR14_RECOVERY_SUBMASK_BIT);
44199441a38SHeiko Carstens 	system_ctl_set_bit(14, CR14_WARNING_SUBMASK_BIT);
442f5daba1dSHeiko Carstens 	return 0;
443f5daba1dSHeiko Carstens }
44424d05ff8SHeiko Carstens early_initcall(machine_check_init);
445