xref: /linux/arch/s390/kernel/nmi.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
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>
34036cbbafSAlexander 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 
nmi_needs_mcesa(void)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 
nmi_alloc_mcesa_early(u64 * mcesad)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 
nmi_alloc_mcesa(u64 * mcesad)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 
nmi_free_mcesa(u64 * mcesad)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 
nmi_puts(char * dest,const char * src)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 
u64_to_hex(char * dest,u64 val)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 
s390_handle_damage(void)11800a8f886SMartin Schwidefsky static notrace void s390_handle_damage(void)
119f5daba1dSHeiko Carstens {
120*d7c3ebc4SSven Schnelle 	struct lowcore *lc = get_lowcore();
121506faa5bSHeiko Carstens 	union ctlreg0 cr0, cr0_new;
122506faa5bSHeiko Carstens 	char message[100];
123506faa5bSHeiko Carstens 	psw_t psw_save;
124506faa5bSHeiko Carstens 	char *ptr;
125506faa5bSHeiko Carstens 
12600a8f886SMartin Schwidefsky 	smp_emergency_stop();
127506faa5bSHeiko Carstens 	diag_amode31_ops.diag308_reset();
128506faa5bSHeiko Carstens 	ptr = nmi_puts(message, "System stopped due to unrecoverable machine check, code: 0x");
129*d7c3ebc4SSven Schnelle 	u64_to_hex(ptr, lc->mcck_interruption_code);
130506faa5bSHeiko Carstens 
131506faa5bSHeiko Carstens 	/*
132506faa5bSHeiko Carstens 	 * Disable low address protection and make machine check new PSW a
133506faa5bSHeiko Carstens 	 * disabled wait PSW. Any additional machine check cannot be handled.
134506faa5bSHeiko Carstens 	 */
135527618abSHeiko Carstens 	local_ctl_store(0, &cr0.reg);
136506faa5bSHeiko Carstens 	cr0_new = cr0;
137506faa5bSHeiko Carstens 	cr0_new.lap = 0;
138527618abSHeiko Carstens 	local_ctl_load(0, &cr0_new.reg);
139*d7c3ebc4SSven Schnelle 	psw_save = lc->mcck_new_psw;
140*d7c3ebc4SSven Schnelle 	psw_bits(lc->mcck_new_psw).io = 0;
141*d7c3ebc4SSven Schnelle 	psw_bits(lc->mcck_new_psw).ext = 0;
142*d7c3ebc4SSven Schnelle 	psw_bits(lc->mcck_new_psw).wait = 1;
143506faa5bSHeiko Carstens 	sclp_emergency_printk(message);
144506faa5bSHeiko Carstens 
145506faa5bSHeiko Carstens 	/*
146506faa5bSHeiko Carstens 	 * Restore machine check new PSW and control register 0 to original
147506faa5bSHeiko Carstens 	 * values. This makes possible system dump analysis easier.
148506faa5bSHeiko Carstens 	 */
149*d7c3ebc4SSven Schnelle 	lc->mcck_new_psw = psw_save;
150527618abSHeiko Carstens 	local_ctl_load(0, &cr0.reg);
15198587c2dSMartin Schwidefsky 	disabled_wait();
152f5daba1dSHeiko Carstens 	while (1);
153f5daba1dSHeiko Carstens }
15400a8f886SMartin Schwidefsky NOKPROBE_SYMBOL(s390_handle_damage);
155f5daba1dSHeiko Carstens 
156f5daba1dSHeiko Carstens /*
15756e62a73SSven Schnelle  * Main machine check handler function. Will be called with interrupts disabled
15856e62a73SSven Schnelle  * and machine checks enabled.
159f5daba1dSHeiko Carstens  */
s390_handle_mcck(void)160e7ec1d2eSAlexander Gordeev void s390_handle_mcck(void)
161f5daba1dSHeiko Carstens {
162f5daba1dSHeiko Carstens 	struct mcck_struct mcck;
1631c8b8cf2SHeiko Carstens 	unsigned long mflags;
164f5daba1dSHeiko Carstens 
165f5daba1dSHeiko Carstens 	/*
166f5daba1dSHeiko Carstens 	 * Disable machine checks and get the current state of accumulated
167f5daba1dSHeiko Carstens 	 * machine checks. Afterwards delete the old state and enable machine
168f5daba1dSHeiko Carstens 	 * checks again.
169f5daba1dSHeiko Carstens 	 */
1701c8b8cf2SHeiko Carstens 	local_mcck_save(mflags);
1712cb4a182SSebastian Ott 	mcck = *this_cpu_ptr(&cpu_mcck);
1722cb4a182SSebastian Ott 	memset(this_cpu_ptr(&cpu_mcck), 0, sizeof(mcck));
1731c8b8cf2SHeiko Carstens 	local_mcck_restore(mflags);
174f5daba1dSHeiko Carstens 
175f5daba1dSHeiko Carstens 	if (mcck.channel_report)
176f5daba1dSHeiko Carstens 		crw_handle_channel_report();
177f5daba1dSHeiko Carstens 	/*
1787b886416SHeiko Carstens 	 * A warning may remain for a prolonged period on the bare iron.
1797b886416SHeiko Carstens 	 * (actually until the machine is powered off, or the problem is gone)
1807b886416SHeiko Carstens 	 * So we just stop listening for the WARNING MCH and avoid continuously
181f5daba1dSHeiko Carstens 	 * being interrupted.  One caveat is however, that we must do this per
182f5daba1dSHeiko Carstens 	 * processor and cannot use the smp version of ctl_clear_bit().
183f5daba1dSHeiko Carstens 	 * On VM we only get one interrupt per virtally presented machinecheck.
1847b886416SHeiko Carstens 	 * Though one suffices, we may get one interrupt per (virtual) cpu.
185f5daba1dSHeiko Carstens 	 */
186f5daba1dSHeiko Carstens 	if (mcck.warning) {	/* WARNING pending ? */
187f5daba1dSHeiko Carstens 		static int mchchk_wng_posted = 0;
1887b886416SHeiko Carstens 
1897b886416SHeiko Carstens 		/* Use single cpu clear, as we cannot handle smp here. */
19099441a38SHeiko Carstens 		local_ctl_clear_bit(14, CR14_WARNING_SUBMASK_BIT);
191f5daba1dSHeiko Carstens 		if (xchg(&mchchk_wng_posted, 1) == 0)
192f5daba1dSHeiko Carstens 			kill_cad_pid(SIGPWR, 1);
193f5daba1dSHeiko Carstens 	}
19429b0a825SHeiko Carstens 	if (mcck.stp_queue)
19529b0a825SHeiko Carstens 		stp_queue_work();
196f5daba1dSHeiko Carstens 	if (mcck.kill_task) {
197f5daba1dSHeiko Carstens 		printk(KERN_EMERG "mcck: Terminating task because of machine "
198dc6e1555SHeiko Carstens 		       "malfunction (code 0x%016lx).\n", mcck.mcck_code);
199f5daba1dSHeiko Carstens 		printk(KERN_EMERG "mcck: task: %s, pid: %d.\n",
200f5daba1dSHeiko Carstens 		       current->comm, current->pid);
201e7ec1d2eSAlexander Gordeev 		if (is_global_init(current))
202e7ec1d2eSAlexander Gordeev 			panic("mcck: Attempting to kill init!\n");
203e7ec1d2eSAlexander Gordeev 		do_send_sig_info(SIGKILL, SEND_SIG_PRIV, current, PIDTYPE_PID);
204f5daba1dSHeiko Carstens 	}
205f5daba1dSHeiko Carstens }
206f5daba1dSHeiko Carstens 
2079e96afabSHeiko Carstens /**
2089e96afabSHeiko Carstens  * nmi_registers_valid - verify if registers are valid
2099e96afabSHeiko Carstens  * @mci: machine check interruption code
2109e96afabSHeiko Carstens  *
2119e96afabSHeiko Carstens  * Inspect a machine check interruption code and verify if all required
2129e96afabSHeiko Carstens  * registers are valid. For some registers the corresponding validity bit is
2139e96afabSHeiko Carstens  * ignored and the registers are set to the expected value.
2149e96afabSHeiko Carstens  * Returns true if all registers are valid, otherwise false.
215f5daba1dSHeiko Carstens  */
nmi_registers_valid(union mci mci)2169e96afabSHeiko Carstens static bool notrace nmi_registers_valid(union mci mci)
217f5daba1dSHeiko Carstens {
218ad3bc0acSMartin Schwidefsky 	union ctlreg2 cr2;
219f5daba1dSHeiko Carstens 
2209e96afabSHeiko Carstens 	/*
2219e96afabSHeiko Carstens 	 * The getcpu vdso syscall reads the CPU number from the programmable
2229e96afabSHeiko Carstens 	 * field of the TOD clock. Disregard the TOD programmable register
2239e96afabSHeiko Carstens 	 * validity bit and load the CPU number into the TOD programmable field
2249e96afabSHeiko Carstens 	 * unconditionally.
2259e96afabSHeiko Carstens 	 */
2269e96afabSHeiko Carstens 	set_tod_programmable_field(raw_smp_processor_id());
2279e96afabSHeiko Carstens 	/*
2289e96afabSHeiko Carstens 	 * Set the clock comparator register to the next expected value.
2299e96afabSHeiko Carstens 	 */
230208da1d5SSven Schnelle 	set_clock_comparator(get_lowcore()->clock_comparator);
2319e96afabSHeiko Carstens 	if (!mci.gr || !mci.fp || !mci.fc)
2329e96afabSHeiko Carstens 		return false;
233f094a39cSChristian Borntraeger 	/*
234f094a39cSChristian Borntraeger 	 * The vector validity must only be checked if not running a
235f094a39cSChristian Borntraeger 	 * KVM guest. For KVM guests the machine check is forwarded by
236f094a39cSChristian Borntraeger 	 * KVM and it is the responsibility of the guest to take
237f094a39cSChristian Borntraeger 	 * appropriate actions. The host vector or FPU values have been
238f094a39cSChristian Borntraeger 	 * saved by KVM and will be restored by KVM.
239f094a39cSChristian Borntraeger 	 */
240f9e5938aSHeiko Carstens 	if (!mci.vr && !test_cpu_flag(CIF_MCCK_GUEST))
2419e96afabSHeiko Carstens 		return false;
242f9e5938aSHeiko Carstens 	if (!mci.ar)
2439e96afabSHeiko Carstens 		return false;
244916cda1aSMartin Schwidefsky 	/*
2459e96afabSHeiko Carstens 	 * Two cases for guarded storage registers:
2461ea1d6a8SChristian Borntraeger 	 * - machine check in kernel or userspace
2471ea1d6a8SChristian Borntraeger 	 * - machine check while running SIE (KVM guest)
2489e96afabSHeiko Carstens 	 * For kernel or userspace the userspace values of guarded storage
2499e96afabSHeiko Carstens 	 * control can not be recreated, the process must be terminated.
2509e96afabSHeiko Carstens 	 * For SIE the guest values of guarded storage can not be recreated.
2519e96afabSHeiko Carstens 	 * This is either due to a bug or due to GS being disabled in the
2529e96afabSHeiko Carstens 	 * guest. The guest will be notified by KVM code and the guests machine
2539e96afabSHeiko Carstens 	 * check handling must take care of this. The host values are saved by
2549e96afabSHeiko Carstens 	 * KVM and are not affected.
255916cda1aSMartin Schwidefsky 	 */
256208da1d5SSven Schnelle 	cr2.reg = get_lowcore()->cregs_save_area[2];
2579e96afabSHeiko Carstens 	if (cr2.gse && !mci.gs && !test_cpu_flag(CIF_MCCK_GUEST))
2589e96afabSHeiko Carstens 		return false;
259dc6e1555SHeiko Carstens 	if (!mci.ms || !mci.pm || !mci.ia)
2609e96afabSHeiko Carstens 		return false;
2619e96afabSHeiko Carstens 	return true;
262f5daba1dSHeiko Carstens }
2639e96afabSHeiko Carstens NOKPROBE_SYMBOL(nmi_registers_valid);
264f5daba1dSHeiko Carstens 
265da72ca4dSQingFeng Hao /*
266da72ca4dSQingFeng Hao  * Backup the guest's machine check info to its description block
267da72ca4dSQingFeng Hao  */
s390_backup_mcck_info(struct pt_regs * regs)268da72ca4dSQingFeng Hao static void notrace s390_backup_mcck_info(struct pt_regs *regs)
269da72ca4dSQingFeng Hao {
270da72ca4dSQingFeng Hao 	struct mcck_volatile_info *mcck_backup;
271da72ca4dSQingFeng Hao 	struct sie_page *sie_page;
272da72ca4dSQingFeng Hao 
273da72ca4dSQingFeng Hao 	/* r14 contains the sie block, which was set in sie64a */
274ae4b60f6SNico Boehr 	struct kvm_s390_sie_block *sie_block = phys_to_virt(regs->gprs[14]);
275da72ca4dSQingFeng Hao 
276da72ca4dSQingFeng Hao 	if (sie_block == NULL)
277da72ca4dSQingFeng Hao 		/* Something's seriously wrong, stop system. */
278da72ca4dSQingFeng Hao 		s390_handle_damage();
279da72ca4dSQingFeng Hao 
280da72ca4dSQingFeng Hao 	sie_page = container_of(sie_block, struct sie_page, sie_block);
281da72ca4dSQingFeng Hao 	mcck_backup = &sie_page->mcck_info;
282208da1d5SSven Schnelle 	mcck_backup->mcic = get_lowcore()->mcck_interruption_code &
283da72ca4dSQingFeng Hao 				~(MCCK_CODE_CP | MCCK_CODE_EXT_DAMAGE);
284208da1d5SSven Schnelle 	mcck_backup->ext_damage_code = get_lowcore()->external_damage_code;
285208da1d5SSven Schnelle 	mcck_backup->failing_storage_address = get_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  */
s390_do_machine_check(struct pt_regs * regs)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;
305*d7c3ebc4SSven Schnelle 	struct lowcore *lc = get_lowcore();
306f5daba1dSHeiko Carstens 	struct mcck_struct *mcck;
307f5daba1dSHeiko Carstens 	unsigned long long tmp;
308520763a3SSven Schnelle 	irqentry_state_t irq_state;
309dc6e1555SHeiko Carstens 	union mci mci;
310c929500dSQingFeng Hao 	unsigned long mcck_dam_code;
3110b0ed657SSven Schnelle 	int mcck_pending = 0;
312f5daba1dSHeiko Carstens 
313520763a3SSven Schnelle 	irq_state = irqentry_nmi_enter(regs);
31456e62a73SSven Schnelle 
31556e62a73SSven Schnelle 	if (user_mode(regs))
31656e62a73SSven Schnelle 		update_timer_mcck();
317420f42ecSHeiko Carstens 	inc_irq_stat(NMI_NMI);
318*d7c3ebc4SSven Schnelle 	mci.val = lc->mcck_interruption_code;
319eb7e7d76SChristoph Lameter 	mcck = this_cpu_ptr(&cpu_mcck);
320f5daba1dSHeiko Carstens 
321c929500dSQingFeng Hao 	/*
322c929500dSQingFeng Hao 	 * Reinject the instruction processing damages' machine checks
323c929500dSQingFeng Hao 	 * including Delayed Access Exception into the guest
324c929500dSQingFeng Hao 	 * instead of damaging the host if they happen in the guest.
325c929500dSQingFeng Hao 	 */
326c929500dSQingFeng Hao 	if (mci.pd && !test_cpu_flag(CIF_MCCK_GUEST)) {
327dc6e1555SHeiko Carstens 		if (mci.b) {
328f5daba1dSHeiko Carstens 			/* Processing backup -> verify if we can survive this */
329f5daba1dSHeiko Carstens 			u64 z_mcic, o_mcic, t_mcic;
330f5daba1dSHeiko Carstens 			z_mcic = (1ULL<<63 | 1ULL<<59 | 1ULL<<29);
331f5daba1dSHeiko Carstens 			o_mcic = (1ULL<<43 | 1ULL<<42 | 1ULL<<41 | 1ULL<<40 |
332f5daba1dSHeiko Carstens 				  1ULL<<36 | 1ULL<<35 | 1ULL<<34 | 1ULL<<32 |
333f5daba1dSHeiko Carstens 				  1ULL<<30 | 1ULL<<21 | 1ULL<<20 | 1ULL<<17 |
334f5daba1dSHeiko Carstens 				  1ULL<<16);
335dc6e1555SHeiko Carstens 			t_mcic = mci.val;
336f5daba1dSHeiko Carstens 
337f5daba1dSHeiko Carstens 			if (((t_mcic & z_mcic) != 0) ||
338f5daba1dSHeiko Carstens 			    ((t_mcic & o_mcic) != o_mcic)) {
3393d68286aSHeiko Carstens 				s390_handle_damage();
340f5daba1dSHeiko Carstens 			}
341f5daba1dSHeiko Carstens 
342f5daba1dSHeiko Carstens 			/*
343f5daba1dSHeiko Carstens 			 * Nullifying exigent condition, therefore we might
344f5daba1dSHeiko Carstens 			 * retry this instruction.
345f5daba1dSHeiko Carstens 			 */
346f5daba1dSHeiko Carstens 			spin_lock(&ipd_lock);
3471aae0560SHeiko Carstens 			tmp = get_tod_clock();
348f5daba1dSHeiko Carstens 			if (((tmp - last_ipd) >> 12) < MAX_IPD_TIME)
349f5daba1dSHeiko Carstens 				ipd_count++;
350f5daba1dSHeiko Carstens 			else
351f5daba1dSHeiko Carstens 				ipd_count = 1;
352f5daba1dSHeiko Carstens 			last_ipd = tmp;
353f5daba1dSHeiko Carstens 			if (ipd_count == MAX_IPD_COUNT)
3543d68286aSHeiko Carstens 				s390_handle_damage();
355f5daba1dSHeiko Carstens 			spin_unlock(&ipd_lock);
356f5daba1dSHeiko Carstens 		} else {
357f5daba1dSHeiko Carstens 			/* Processing damage -> stopping machine */
3583d68286aSHeiko Carstens 			s390_handle_damage();
359f5daba1dSHeiko Carstens 		}
360f5daba1dSHeiko Carstens 	}
3619e96afabSHeiko Carstens 	if (!nmi_registers_valid(mci)) {
362f9e5938aSHeiko Carstens 		if (!user_mode(regs))
363f9e5938aSHeiko Carstens 			s390_handle_damage();
364f5daba1dSHeiko Carstens 		/*
3658f149ea6SMartin Schwidefsky 		 * Couldn't restore all register contents for the
3668f149ea6SMartin Schwidefsky 		 * user space process -> mark task for termination.
367f5daba1dSHeiko Carstens 		 */
368f5daba1dSHeiko Carstens 		mcck->kill_task = 1;
369dc6e1555SHeiko Carstens 		mcck->mcck_code = mci.val;
3700b0ed657SSven Schnelle 		mcck_pending = 1;
371f5daba1dSHeiko Carstens 	}
372da72ca4dSQingFeng Hao 
373da72ca4dSQingFeng Hao 	/*
374da72ca4dSQingFeng Hao 	 * Backup the machine check's info if it happens when the guest
375da72ca4dSQingFeng Hao 	 * is running.
376da72ca4dSQingFeng Hao 	 */
377da72ca4dSQingFeng Hao 	if (test_cpu_flag(CIF_MCCK_GUEST))
378da72ca4dSQingFeng Hao 		s390_backup_mcck_info(regs);
379da72ca4dSQingFeng Hao 
380dc6e1555SHeiko Carstens 	if (mci.cd) {
381f5daba1dSHeiko Carstens 		/* Timing facility damage */
3823d68286aSHeiko Carstens 		s390_handle_damage();
383f5daba1dSHeiko Carstens 	}
384dc6e1555SHeiko Carstens 	if (mci.ed && mci.ec) {
385f5daba1dSHeiko Carstens 		/* External damage */
386*d7c3ebc4SSven Schnelle 		if (lc->external_damage_code & (1U << ED_STP_SYNC))
38729b0a825SHeiko Carstens 			mcck->stp_queue |= stp_sync_check();
388*d7c3ebc4SSven Schnelle 		if (lc->external_damage_code & (1U << ED_STP_ISLAND))
38929b0a825SHeiko Carstens 			mcck->stp_queue |= stp_island_check();
3900b0ed657SSven Schnelle 		mcck_pending = 1;
391f5daba1dSHeiko Carstens 	}
392742aed05SHeiko Carstens 	/*
393742aed05SHeiko Carstens 	 * Reinject storage related machine checks into the guest if they
394742aed05SHeiko Carstens 	 * happen when the guest is running.
395742aed05SHeiko Carstens 	 */
396742aed05SHeiko Carstens 	if (!test_cpu_flag(CIF_MCCK_GUEST)) {
397742aed05SHeiko Carstens 		/* Storage error uncorrected */
398742aed05SHeiko Carstens 		if (mci.se)
399742aed05SHeiko Carstens 			s390_handle_damage();
400742aed05SHeiko Carstens 		/* Storage key-error uncorrected */
401742aed05SHeiko Carstens 		if (mci.ke)
402742aed05SHeiko Carstens 			s390_handle_damage();
403742aed05SHeiko Carstens 		/* Storage degradation */
404742aed05SHeiko Carstens 		if (mci.ds && mci.fa)
405742aed05SHeiko Carstens 			s390_handle_damage();
406742aed05SHeiko Carstens 	}
407dc6e1555SHeiko Carstens 	if (mci.cp) {
408f5daba1dSHeiko Carstens 		/* Channel report word pending */
409f5daba1dSHeiko Carstens 		mcck->channel_report = 1;
4100b0ed657SSven Schnelle 		mcck_pending = 1;
411f5daba1dSHeiko Carstens 	}
412dc6e1555SHeiko Carstens 	if (mci.w) {
413f5daba1dSHeiko Carstens 		/* Warning pending */
414f5daba1dSHeiko Carstens 		mcck->warning = 1;
4150b0ed657SSven Schnelle 		mcck_pending = 1;
416f5daba1dSHeiko Carstens 	}
417c929500dSQingFeng Hao 
418c929500dSQingFeng Hao 	/*
419c929500dSQingFeng Hao 	 * If there are only Channel Report Pending and External Damage
420c929500dSQingFeng Hao 	 * machine checks, they will not be reinjected into the guest
421c929500dSQingFeng Hao 	 * because they refer to host conditions only.
422c929500dSQingFeng Hao 	 */
423c929500dSQingFeng Hao 	mcck_dam_code = (mci.val & MCIC_SUBCLASS_MASK);
424c929500dSQingFeng Hao 	if (test_cpu_flag(CIF_MCCK_GUEST) &&
425c929500dSQingFeng Hao 	(mcck_dam_code & MCCK_CODE_NO_GUEST) != mcck_dam_code) {
426c929500dSQingFeng Hao 		/* Set exit reason code for host's later handling */
427c929500dSQingFeng Hao 		*((long *)(regs->gprs[15] + __SF_SIE_REASON)) = -EINTR;
428c929500dSQingFeng Hao 	}
429c929500dSQingFeng Hao 	clear_cpu_flag(CIF_MCCK_GUEST);
4300b0ed657SSven Schnelle 
4310b0ed657SSven Schnelle 	if (mcck_pending)
4320b0ed657SSven Schnelle 		schedule_mcck_handler();
4330b0ed657SSven Schnelle 
434520763a3SSven Schnelle 	irqentry_nmi_exit(regs, irq_state);
435f5daba1dSHeiko Carstens }
43600a8f886SMartin Schwidefsky NOKPROBE_SYMBOL(s390_do_machine_check);
437f5daba1dSHeiko Carstens 
machine_check_init(void)438f5daba1dSHeiko Carstens static int __init machine_check_init(void)
439f5daba1dSHeiko Carstens {
44099441a38SHeiko Carstens 	system_ctl_set_bit(14, CR14_EXTERNAL_DAMAGE_SUBMASK_BIT);
44199441a38SHeiko Carstens 	system_ctl_set_bit(14, CR14_RECOVERY_SUBMASK_BIT);
44299441a38SHeiko Carstens 	system_ctl_set_bit(14, CR14_WARNING_SUBMASK_BIT);
443f5daba1dSHeiko Carstens 	return 0;
444f5daba1dSHeiko Carstens }
44524d05ff8SHeiko Carstens early_initcall(machine_check_init);
446