xref: /linux/arch/s390/kernel/nmi.c (revision ebe1cd530fb2f6450656758bd904cfb5767f8d33)
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>
25*ebe1cd53SHeiko Carstens #include <asm/ctlreg.h>
26f5daba1dSHeiko Carstens #include <asm/smp.h>
27fd5ada04SMartin Schwidefsky #include <asm/stp.h>
2876d4e00aSMartin Schwidefsky #include <asm/cputime.h>
29f5daba1dSHeiko Carstens #include <asm/nmi.h>
30f5daba1dSHeiko Carstens #include <asm/crw.h>
3180703617SMartin Schwidefsky #include <asm/switch_to.h>
32c929500dSQingFeng Hao #include <asm/asm-offsets.h>
3339d62336SThomas Richter #include <asm/pai.h>
345720aab2SHeiko Carstens #include <asm/vx-insn.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 {
486c81511cSMartin Schwidefsky 	return MACHINE_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 	 */
134506faa5bSHeiko Carstens 	__ctl_store(cr0.val, 0, 0);
135506faa5bSHeiko Carstens 	cr0_new = cr0;
136506faa5bSHeiko Carstens 	cr0_new.lap = 0;
137506faa5bSHeiko Carstens 	__ctl_load(cr0_new.val, 0, 0);
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;
149506faa5bSHeiko Carstens 	__ctl_load(cr0.val, 0, 0);
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;
162f5daba1dSHeiko Carstens 
163f5daba1dSHeiko Carstens 	/*
164f5daba1dSHeiko Carstens 	 * Disable machine checks and get the current state of accumulated
165f5daba1dSHeiko Carstens 	 * machine checks. Afterwards delete the old state and enable machine
166f5daba1dSHeiko Carstens 	 * checks again.
167f5daba1dSHeiko Carstens 	 */
168f5daba1dSHeiko Carstens 	local_mcck_disable();
1692cb4a182SSebastian Ott 	mcck = *this_cpu_ptr(&cpu_mcck);
1702cb4a182SSebastian Ott 	memset(this_cpu_ptr(&cpu_mcck), 0, sizeof(mcck));
171f5daba1dSHeiko Carstens 	local_mcck_enable();
172f5daba1dSHeiko Carstens 
173f5daba1dSHeiko Carstens 	if (mcck.channel_report)
174f5daba1dSHeiko Carstens 		crw_handle_channel_report();
175f5daba1dSHeiko Carstens 	/*
1767b886416SHeiko Carstens 	 * A warning may remain for a prolonged period on the bare iron.
1777b886416SHeiko Carstens 	 * (actually until the machine is powered off, or the problem is gone)
1787b886416SHeiko Carstens 	 * So we just stop listening for the WARNING MCH and avoid continuously
179f5daba1dSHeiko Carstens 	 * being interrupted.  One caveat is however, that we must do this per
180f5daba1dSHeiko Carstens 	 * processor and cannot use the smp version of ctl_clear_bit().
181f5daba1dSHeiko Carstens 	 * On VM we only get one interrupt per virtally presented machinecheck.
1827b886416SHeiko Carstens 	 * Though one suffices, we may get one interrupt per (virtual) cpu.
183f5daba1dSHeiko Carstens 	 */
184f5daba1dSHeiko Carstens 	if (mcck.warning) {	/* WARNING pending ? */
185f5daba1dSHeiko Carstens 		static int mchchk_wng_posted = 0;
1867b886416SHeiko Carstens 
1877b886416SHeiko Carstens 		/* Use single cpu clear, as we cannot handle smp here. */
188f5daba1dSHeiko Carstens 		__ctl_clear_bit(14, 24);	/* Disable WARNING MCH */
189f5daba1dSHeiko Carstens 		if (xchg(&mchchk_wng_posted, 1) == 0)
190f5daba1dSHeiko Carstens 			kill_cad_pid(SIGPWR, 1);
191f5daba1dSHeiko Carstens 	}
19229b0a825SHeiko Carstens 	if (mcck.stp_queue)
19329b0a825SHeiko Carstens 		stp_queue_work();
194f5daba1dSHeiko Carstens 	if (mcck.kill_task) {
195f5daba1dSHeiko Carstens 		printk(KERN_EMERG "mcck: Terminating task because of machine "
196dc6e1555SHeiko Carstens 		       "malfunction (code 0x%016lx).\n", mcck.mcck_code);
197f5daba1dSHeiko Carstens 		printk(KERN_EMERG "mcck: task: %s, pid: %d.\n",
198f5daba1dSHeiko Carstens 		       current->comm, current->pid);
199e7ec1d2eSAlexander Gordeev 		if (is_global_init(current))
200e7ec1d2eSAlexander Gordeev 			panic("mcck: Attempting to kill init!\n");
201e7ec1d2eSAlexander Gordeev 		do_send_sig_info(SIGKILL, SEND_SIG_PRIV, current, PIDTYPE_PID);
202f5daba1dSHeiko Carstens 	}
203f5daba1dSHeiko Carstens }
204f5daba1dSHeiko Carstens 
205f5daba1dSHeiko Carstens /*
206f9e5938aSHeiko Carstens  * returns 0 if register contents could be validated
207f5daba1dSHeiko Carstens  * returns 1 otherwise
208f5daba1dSHeiko Carstens  */
209f9e5938aSHeiko Carstens static int notrace s390_validate_registers(union mci mci)
210f5daba1dSHeiko Carstens {
2115fa2ea07SAlexander Gordeev 	struct mcesa *mcesa;
2125fa2ea07SAlexander Gordeev 	void *fpt_save_area;
213ad3bc0acSMartin Schwidefsky 	union ctlreg2 cr2;
214f5daba1dSHeiko Carstens 	int kill_task;
2155fa2ea07SAlexander Gordeev 	u64 zero;
216f5daba1dSHeiko Carstens 
217f5daba1dSHeiko Carstens 	kill_task = 0;
2185fa2ea07SAlexander Gordeev 	zero = 0;
219f5daba1dSHeiko Carstens 
220f9e5938aSHeiko Carstens 	if (!mci.gr || !mci.fp)
221f5daba1dSHeiko Carstens 		kill_task = 1;
2225fa2ea07SAlexander Gordeev 	fpt_save_area = &S390_lowcore.floating_pt_save_area;
223dc6e1555SHeiko Carstens 	if (!mci.fc) {
224f9e5938aSHeiko Carstens 		kill_task = 1;
2255fa2ea07SAlexander Gordeev 		asm volatile(
2265fa2ea07SAlexander Gordeev 			"	lfpc	%0\n"
2275fa2ea07SAlexander Gordeev 			:
2285fa2ea07SAlexander Gordeev 			: "Q" (zero));
2295fa2ea07SAlexander Gordeev 	} else {
2305fa2ea07SAlexander Gordeev 		asm volatile(
2315fa2ea07SAlexander Gordeev 			"	lfpc	%0\n"
2325fa2ea07SAlexander Gordeev 			:
2335fa2ea07SAlexander Gordeev 			: "Q" (S390_lowcore.fpt_creg_save_area));
23486fa7087SHeiko Carstens 	}
235f5daba1dSHeiko Carstens 
236402ff5a3SHeiko Carstens 	mcesa = __va(S390_lowcore.mcesad & MCESA_ORIGIN_MASK);
2375fa2ea07SAlexander Gordeev 	if (!MACHINE_HAS_VX) {
2385fa2ea07SAlexander Gordeev 		/* Validate floating point registers */
2395fa2ea07SAlexander Gordeev 		asm volatile(
2405fa2ea07SAlexander Gordeev 			"	ld	0,0(%0)\n"
2415fa2ea07SAlexander Gordeev 			"	ld	1,8(%0)\n"
2425fa2ea07SAlexander Gordeev 			"	ld	2,16(%0)\n"
2435fa2ea07SAlexander Gordeev 			"	ld	3,24(%0)\n"
2445fa2ea07SAlexander Gordeev 			"	ld	4,32(%0)\n"
2455fa2ea07SAlexander Gordeev 			"	ld	5,40(%0)\n"
2465fa2ea07SAlexander Gordeev 			"	ld	6,48(%0)\n"
2475fa2ea07SAlexander Gordeev 			"	ld	7,56(%0)\n"
2485fa2ea07SAlexander Gordeev 			"	ld	8,64(%0)\n"
2495fa2ea07SAlexander Gordeev 			"	ld	9,72(%0)\n"
2505fa2ea07SAlexander Gordeev 			"	ld	10,80(%0)\n"
2515fa2ea07SAlexander Gordeev 			"	ld	11,88(%0)\n"
2525fa2ea07SAlexander Gordeev 			"	ld	12,96(%0)\n"
2535fa2ea07SAlexander Gordeev 			"	ld	13,104(%0)\n"
2545fa2ea07SAlexander Gordeev 			"	ld	14,112(%0)\n"
2555fa2ea07SAlexander Gordeev 			"	ld	15,120(%0)\n"
2565fa2ea07SAlexander Gordeev 			:
2575fa2ea07SAlexander Gordeev 			: "a" (fpt_save_area)
2585fa2ea07SAlexander Gordeev 			: "memory");
2595fa2ea07SAlexander Gordeev 	} else {
2605fa2ea07SAlexander Gordeev 		/* Validate vector registers */
2615fa2ea07SAlexander Gordeev 		union ctlreg0 cr0;
2625fa2ea07SAlexander Gordeev 
263f094a39cSChristian Borntraeger 		/*
264f094a39cSChristian Borntraeger 		 * The vector validity must only be checked if not running a
265f094a39cSChristian Borntraeger 		 * KVM guest. For KVM guests the machine check is forwarded by
266f094a39cSChristian Borntraeger 		 * KVM and it is the responsibility of the guest to take
267f094a39cSChristian Borntraeger 		 * appropriate actions. The host vector or FPU values have been
268f094a39cSChristian Borntraeger 		 * saved by KVM and will be restored by KVM.
269f094a39cSChristian Borntraeger 		 */
270f9e5938aSHeiko Carstens 		if (!mci.vr && !test_cpu_flag(CIF_MCCK_GUEST))
27180703617SMartin Schwidefsky 			kill_task = 1;
2725fa2ea07SAlexander Gordeev 		cr0.val = S390_lowcore.cregs_save_area[0];
2735fa2ea07SAlexander Gordeev 		cr0.afp = cr0.vx = 1;
2745fa2ea07SAlexander Gordeev 		__ctl_load(cr0.val, 0, 0);
2755fa2ea07SAlexander Gordeev 		asm volatile(
2765fa2ea07SAlexander Gordeev 			"	la	1,%0\n"
2775720aab2SHeiko Carstens 			"	VLM	0,15,0,1\n"
2785720aab2SHeiko Carstens 			"	VLM	16,31,256,1\n"
2795fa2ea07SAlexander Gordeev 			:
2805fa2ea07SAlexander Gordeev 			: "Q" (*(struct vx_array *)mcesa->vector_save_area)
2815fa2ea07SAlexander Gordeev 			: "1");
2825fa2ea07SAlexander Gordeev 		__ctl_load(S390_lowcore.cregs_save_area[0], 0, 0);
28380703617SMartin Schwidefsky 	}
2845fa2ea07SAlexander Gordeev 	/* Validate access registers */
2855fa2ea07SAlexander Gordeev 	asm volatile(
2865fa2ea07SAlexander Gordeev 		"	lam	0,15,0(%0)\n"
2875fa2ea07SAlexander Gordeev 		:
2885fa2ea07SAlexander Gordeev 		: "a" (&S390_lowcore.access_regs_save_area)
2895fa2ea07SAlexander Gordeev 		: "memory");
290f9e5938aSHeiko Carstens 	if (!mci.ar)
291f5daba1dSHeiko Carstens 		kill_task = 1;
2925fa2ea07SAlexander Gordeev 	/* Validate guarded storage registers */
293ad3bc0acSMartin Schwidefsky 	cr2.val = S390_lowcore.cregs_save_area[2];
294ad3bc0acSMartin Schwidefsky 	if (cr2.gse) {
2953037a52fSMartin Schwidefsky 		if (!mci.gs) {
296916cda1aSMartin Schwidefsky 			/*
2971ea1d6a8SChristian Borntraeger 			 * 2 cases:
2981ea1d6a8SChristian Borntraeger 			 * - machine check in kernel or userspace
2991ea1d6a8SChristian Borntraeger 			 * - machine check while running SIE (KVM guest)
3001ea1d6a8SChristian Borntraeger 			 * For kernel or userspace the userspace values of
3011ea1d6a8SChristian Borntraeger 			 * guarded storage control can not be recreated, the
3021ea1d6a8SChristian Borntraeger 			 * process must be terminated.
3031ea1d6a8SChristian Borntraeger 			 * For SIE the guest values of guarded storage can not
3041ea1d6a8SChristian Borntraeger 			 * be recreated. This is either due to a bug or due to
3051ea1d6a8SChristian Borntraeger 			 * GS being disabled in the guest. The guest will be
3061ea1d6a8SChristian Borntraeger 			 * notified by KVM code and the guests machine check
3071ea1d6a8SChristian Borntraeger 			 * handling must take care of this.  The host values
3081ea1d6a8SChristian Borntraeger 			 * are saved by KVM and are not affected.
309916cda1aSMartin Schwidefsky 			 */
3101ea1d6a8SChristian Borntraeger 			if (!test_cpu_flag(CIF_MCCK_GUEST))
311916cda1aSMartin Schwidefsky 				kill_task = 1;
3125fa2ea07SAlexander Gordeev 		} else {
3135fa2ea07SAlexander Gordeev 			load_gs_cb((struct gs_cb *)mcesa->guarded_storage_save_area);
314916cda1aSMartin Schwidefsky 		}
3153037a52fSMartin Schwidefsky 	}
3165fa2ea07SAlexander Gordeev 	/*
3175fa2ea07SAlexander Gordeev 	 * The getcpu vdso syscall reads CPU number from the programmable
3185fa2ea07SAlexander Gordeev 	 * field of the TOD clock. Disregard the TOD programmable register
3195fa2ea07SAlexander Gordeev 	 * validity bit and load the CPU number into the TOD programmable
3205fa2ea07SAlexander Gordeev 	 * field unconditionally.
3215fa2ea07SAlexander Gordeev 	 */
3225fa2ea07SAlexander Gordeev 	set_tod_programmable_field(raw_smp_processor_id());
3235fa2ea07SAlexander Gordeev 	/* Validate clock comparator register */
3245fa2ea07SAlexander Gordeev 	set_clock_comparator(S390_lowcore.clock_comparator);
325f5daba1dSHeiko Carstens 
326dc6e1555SHeiko Carstens 	if (!mci.ms || !mci.pm || !mci.ia)
327f5daba1dSHeiko Carstens 		kill_task = 1;
328f5daba1dSHeiko Carstens 
329f5daba1dSHeiko Carstens 	return kill_task;
330f5daba1dSHeiko Carstens }
3315fa2ea07SAlexander Gordeev NOKPROBE_SYMBOL(s390_validate_registers);
332f5daba1dSHeiko Carstens 
333da72ca4dSQingFeng Hao /*
334da72ca4dSQingFeng Hao  * Backup the guest's machine check info to its description block
335da72ca4dSQingFeng Hao  */
336da72ca4dSQingFeng Hao static void notrace s390_backup_mcck_info(struct pt_regs *regs)
337da72ca4dSQingFeng Hao {
338da72ca4dSQingFeng Hao 	struct mcck_volatile_info *mcck_backup;
339da72ca4dSQingFeng Hao 	struct sie_page *sie_page;
340da72ca4dSQingFeng Hao 
341da72ca4dSQingFeng Hao 	/* r14 contains the sie block, which was set in sie64a */
342ae4b60f6SNico Boehr 	struct kvm_s390_sie_block *sie_block = phys_to_virt(regs->gprs[14]);
343da72ca4dSQingFeng Hao 
344da72ca4dSQingFeng Hao 	if (sie_block == NULL)
345da72ca4dSQingFeng Hao 		/* Something's seriously wrong, stop system. */
346da72ca4dSQingFeng Hao 		s390_handle_damage();
347da72ca4dSQingFeng Hao 
348da72ca4dSQingFeng Hao 	sie_page = container_of(sie_block, struct sie_page, sie_block);
349da72ca4dSQingFeng Hao 	mcck_backup = &sie_page->mcck_info;
350da72ca4dSQingFeng Hao 	mcck_backup->mcic = S390_lowcore.mcck_interruption_code &
351da72ca4dSQingFeng Hao 				~(MCCK_CODE_CP | MCCK_CODE_EXT_DAMAGE);
352da72ca4dSQingFeng Hao 	mcck_backup->ext_damage_code = S390_lowcore.external_damage_code;
353da72ca4dSQingFeng Hao 	mcck_backup->failing_storage_address
354da72ca4dSQingFeng Hao 			= S390_lowcore.failing_storage_address;
355da72ca4dSQingFeng Hao }
35600a8f886SMartin Schwidefsky NOKPROBE_SYMBOL(s390_backup_mcck_info);
357da72ca4dSQingFeng Hao 
358f5daba1dSHeiko Carstens #define MAX_IPD_COUNT	29
359f5daba1dSHeiko Carstens #define MAX_IPD_TIME	(5 * 60 * USEC_PER_SEC) /* 5 minutes */
360f5daba1dSHeiko Carstens 
361f5daba1dSHeiko Carstens #define ED_STP_ISLAND	6	/* External damage STP island check */
362f5daba1dSHeiko Carstens #define ED_STP_SYNC	7	/* External damage STP sync check */
363f5daba1dSHeiko Carstens 
364c929500dSQingFeng Hao #define MCCK_CODE_NO_GUEST	(MCCK_CODE_CP | MCCK_CODE_EXT_DAMAGE)
365c929500dSQingFeng Hao 
366f5daba1dSHeiko Carstens /*
367f5daba1dSHeiko Carstens  * machine check handler.
368f5daba1dSHeiko Carstens  */
369e7ec1d2eSAlexander Gordeev void notrace s390_do_machine_check(struct pt_regs *regs)
370f5daba1dSHeiko Carstens {
371f5daba1dSHeiko Carstens 	static int ipd_count;
372f5daba1dSHeiko Carstens 	static DEFINE_SPINLOCK(ipd_lock);
373f5daba1dSHeiko Carstens 	static unsigned long long last_ipd;
374f5daba1dSHeiko Carstens 	struct mcck_struct *mcck;
375f5daba1dSHeiko Carstens 	unsigned long long tmp;
376520763a3SSven Schnelle 	irqentry_state_t irq_state;
377dc6e1555SHeiko Carstens 	union mci mci;
378c929500dSQingFeng Hao 	unsigned long mcck_dam_code;
3790b0ed657SSven Schnelle 	int mcck_pending = 0;
380f5daba1dSHeiko Carstens 
381520763a3SSven Schnelle 	irq_state = irqentry_nmi_enter(regs);
38256e62a73SSven Schnelle 
38356e62a73SSven Schnelle 	if (user_mode(regs))
38456e62a73SSven Schnelle 		update_timer_mcck();
385420f42ecSHeiko Carstens 	inc_irq_stat(NMI_NMI);
386dc6e1555SHeiko Carstens 	mci.val = S390_lowcore.mcck_interruption_code;
387eb7e7d76SChristoph Lameter 	mcck = this_cpu_ptr(&cpu_mcck);
388f5daba1dSHeiko Carstens 
389c929500dSQingFeng Hao 	/*
390c929500dSQingFeng Hao 	 * Reinject the instruction processing damages' machine checks
391c929500dSQingFeng Hao 	 * including Delayed Access Exception into the guest
392c929500dSQingFeng Hao 	 * instead of damaging the host if they happen in the guest.
393c929500dSQingFeng Hao 	 */
394c929500dSQingFeng Hao 	if (mci.pd && !test_cpu_flag(CIF_MCCK_GUEST)) {
395dc6e1555SHeiko Carstens 		if (mci.b) {
396f5daba1dSHeiko Carstens 			/* Processing backup -> verify if we can survive this */
397f5daba1dSHeiko Carstens 			u64 z_mcic, o_mcic, t_mcic;
398f5daba1dSHeiko Carstens 			z_mcic = (1ULL<<63 | 1ULL<<59 | 1ULL<<29);
399f5daba1dSHeiko Carstens 			o_mcic = (1ULL<<43 | 1ULL<<42 | 1ULL<<41 | 1ULL<<40 |
400f5daba1dSHeiko Carstens 				  1ULL<<36 | 1ULL<<35 | 1ULL<<34 | 1ULL<<32 |
401f5daba1dSHeiko Carstens 				  1ULL<<30 | 1ULL<<21 | 1ULL<<20 | 1ULL<<17 |
402f5daba1dSHeiko Carstens 				  1ULL<<16);
403dc6e1555SHeiko Carstens 			t_mcic = mci.val;
404f5daba1dSHeiko Carstens 
405f5daba1dSHeiko Carstens 			if (((t_mcic & z_mcic) != 0) ||
406f5daba1dSHeiko Carstens 			    ((t_mcic & o_mcic) != o_mcic)) {
4073d68286aSHeiko Carstens 				s390_handle_damage();
408f5daba1dSHeiko Carstens 			}
409f5daba1dSHeiko Carstens 
410f5daba1dSHeiko Carstens 			/*
411f5daba1dSHeiko Carstens 			 * Nullifying exigent condition, therefore we might
412f5daba1dSHeiko Carstens 			 * retry this instruction.
413f5daba1dSHeiko Carstens 			 */
414f5daba1dSHeiko Carstens 			spin_lock(&ipd_lock);
4151aae0560SHeiko Carstens 			tmp = get_tod_clock();
416f5daba1dSHeiko Carstens 			if (((tmp - last_ipd) >> 12) < MAX_IPD_TIME)
417f5daba1dSHeiko Carstens 				ipd_count++;
418f5daba1dSHeiko Carstens 			else
419f5daba1dSHeiko Carstens 				ipd_count = 1;
420f5daba1dSHeiko Carstens 			last_ipd = tmp;
421f5daba1dSHeiko Carstens 			if (ipd_count == MAX_IPD_COUNT)
4223d68286aSHeiko Carstens 				s390_handle_damage();
423f5daba1dSHeiko Carstens 			spin_unlock(&ipd_lock);
424f5daba1dSHeiko Carstens 		} else {
425f5daba1dSHeiko Carstens 			/* Processing damage -> stopping machine */
4263d68286aSHeiko Carstens 			s390_handle_damage();
427f5daba1dSHeiko Carstens 		}
428f5daba1dSHeiko Carstens 	}
429f9e5938aSHeiko Carstens 	if (s390_validate_registers(mci)) {
430f9e5938aSHeiko Carstens 		if (!user_mode(regs))
431f9e5938aSHeiko Carstens 			s390_handle_damage();
432f5daba1dSHeiko Carstens 		/*
4338f149ea6SMartin Schwidefsky 		 * Couldn't restore all register contents for the
4348f149ea6SMartin Schwidefsky 		 * user space process -> mark task for termination.
435f5daba1dSHeiko Carstens 		 */
436f5daba1dSHeiko Carstens 		mcck->kill_task = 1;
437dc6e1555SHeiko Carstens 		mcck->mcck_code = mci.val;
4380b0ed657SSven Schnelle 		mcck_pending = 1;
439f5daba1dSHeiko Carstens 	}
440da72ca4dSQingFeng Hao 
441da72ca4dSQingFeng Hao 	/*
442da72ca4dSQingFeng Hao 	 * Backup the machine check's info if it happens when the guest
443da72ca4dSQingFeng Hao 	 * is running.
444da72ca4dSQingFeng Hao 	 */
445da72ca4dSQingFeng Hao 	if (test_cpu_flag(CIF_MCCK_GUEST))
446da72ca4dSQingFeng Hao 		s390_backup_mcck_info(regs);
447da72ca4dSQingFeng Hao 
448dc6e1555SHeiko Carstens 	if (mci.cd) {
449f5daba1dSHeiko Carstens 		/* Timing facility damage */
4503d68286aSHeiko Carstens 		s390_handle_damage();
451f5daba1dSHeiko Carstens 	}
452dc6e1555SHeiko Carstens 	if (mci.ed && mci.ec) {
453f5daba1dSHeiko Carstens 		/* External damage */
454f5daba1dSHeiko Carstens 		if (S390_lowcore.external_damage_code & (1U << ED_STP_SYNC))
45529b0a825SHeiko Carstens 			mcck->stp_queue |= stp_sync_check();
456f5daba1dSHeiko Carstens 		if (S390_lowcore.external_damage_code & (1U << ED_STP_ISLAND))
45729b0a825SHeiko Carstens 			mcck->stp_queue |= stp_island_check();
4580b0ed657SSven Schnelle 		mcck_pending = 1;
459f5daba1dSHeiko Carstens 	}
460742aed05SHeiko Carstens 	/*
461742aed05SHeiko Carstens 	 * Reinject storage related machine checks into the guest if they
462742aed05SHeiko Carstens 	 * happen when the guest is running.
463742aed05SHeiko Carstens 	 */
464742aed05SHeiko Carstens 	if (!test_cpu_flag(CIF_MCCK_GUEST)) {
465742aed05SHeiko Carstens 		/* Storage error uncorrected */
466742aed05SHeiko Carstens 		if (mci.se)
467742aed05SHeiko Carstens 			s390_handle_damage();
468742aed05SHeiko Carstens 		/* Storage key-error uncorrected */
469742aed05SHeiko Carstens 		if (mci.ke)
470742aed05SHeiko Carstens 			s390_handle_damage();
471742aed05SHeiko Carstens 		/* Storage degradation */
472742aed05SHeiko Carstens 		if (mci.ds && mci.fa)
473742aed05SHeiko Carstens 			s390_handle_damage();
474742aed05SHeiko Carstens 	}
475dc6e1555SHeiko Carstens 	if (mci.cp) {
476f5daba1dSHeiko Carstens 		/* Channel report word pending */
477f5daba1dSHeiko Carstens 		mcck->channel_report = 1;
4780b0ed657SSven Schnelle 		mcck_pending = 1;
479f5daba1dSHeiko Carstens 	}
480dc6e1555SHeiko Carstens 	if (mci.w) {
481f5daba1dSHeiko Carstens 		/* Warning pending */
482f5daba1dSHeiko Carstens 		mcck->warning = 1;
4830b0ed657SSven Schnelle 		mcck_pending = 1;
484f5daba1dSHeiko Carstens 	}
485c929500dSQingFeng Hao 
486c929500dSQingFeng Hao 	/*
487c929500dSQingFeng Hao 	 * If there are only Channel Report Pending and External Damage
488c929500dSQingFeng Hao 	 * machine checks, they will not be reinjected into the guest
489c929500dSQingFeng Hao 	 * because they refer to host conditions only.
490c929500dSQingFeng Hao 	 */
491c929500dSQingFeng Hao 	mcck_dam_code = (mci.val & MCIC_SUBCLASS_MASK);
492c929500dSQingFeng Hao 	if (test_cpu_flag(CIF_MCCK_GUEST) &&
493c929500dSQingFeng Hao 	(mcck_dam_code & MCCK_CODE_NO_GUEST) != mcck_dam_code) {
494c929500dSQingFeng Hao 		/* Set exit reason code for host's later handling */
495c929500dSQingFeng Hao 		*((long *)(regs->gprs[15] + __SF_SIE_REASON)) = -EINTR;
496c929500dSQingFeng Hao 	}
497c929500dSQingFeng Hao 	clear_cpu_flag(CIF_MCCK_GUEST);
4980b0ed657SSven Schnelle 
4990b0ed657SSven Schnelle 	if (mcck_pending)
5000b0ed657SSven Schnelle 		schedule_mcck_handler();
5010b0ed657SSven Schnelle 
502520763a3SSven Schnelle 	irqentry_nmi_exit(regs, irq_state);
503f5daba1dSHeiko Carstens }
50400a8f886SMartin Schwidefsky NOKPROBE_SYMBOL(s390_do_machine_check);
505f5daba1dSHeiko Carstens 
506f5daba1dSHeiko Carstens static int __init machine_check_init(void)
507f5daba1dSHeiko Carstens {
508f5daba1dSHeiko Carstens 	ctl_set_bit(14, 25);	/* enable external damage MCH */
509f5daba1dSHeiko Carstens 	ctl_set_bit(14, 27);	/* enable system recovery MCH */
510f5daba1dSHeiko Carstens 	ctl_set_bit(14, 24);	/* enable warning MCH */
511f5daba1dSHeiko Carstens 	return 0;
512f5daba1dSHeiko Carstens }
51324d05ff8SHeiko Carstens early_initcall(machine_check_init);
514