xref: /linux/arch/s390/kernel/nmi.c (revision f9e5938ace2cc8ecf15815649481266017135757)
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>
25f5daba1dSHeiko Carstens #include <asm/smp.h>
26fd5ada04SMartin Schwidefsky #include <asm/stp.h>
2776d4e00aSMartin Schwidefsky #include <asm/cputime.h>
28f5daba1dSHeiko Carstens #include <asm/nmi.h>
29f5daba1dSHeiko Carstens #include <asm/crw.h>
3080703617SMartin Schwidefsky #include <asm/switch_to.h>
31cad49cfcSHeiko Carstens #include <asm/ctl_reg.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 static struct kmem_cache *mcesa_cache;
466c81511cSMartin Schwidefsky static unsigned long mcesa_origin_lc;
476c81511cSMartin Schwidefsky 
486c81511cSMartin Schwidefsky static inline int nmi_needs_mcesa(void)
496c81511cSMartin Schwidefsky {
506c81511cSMartin Schwidefsky 	return MACHINE_HAS_VX || MACHINE_HAS_GS;
516c81511cSMartin Schwidefsky }
526c81511cSMartin Schwidefsky 
536c81511cSMartin Schwidefsky static inline unsigned long nmi_get_mcesa_size(void)
546c81511cSMartin Schwidefsky {
556c81511cSMartin Schwidefsky 	if (MACHINE_HAS_GS)
566c81511cSMartin Schwidefsky 		return MCESA_MAX_SIZE;
576c81511cSMartin Schwidefsky 	return MCESA_MIN_SIZE;
586c81511cSMartin Schwidefsky }
596c81511cSMartin Schwidefsky 
606c81511cSMartin Schwidefsky /*
616c81511cSMartin Schwidefsky  * The initial machine check extended save area for the boot CPU.
62c7ed509bSAlexander Gordeev  * It will be replaced on the boot CPU reinit with an allocated
63c7ed509bSAlexander Gordeev  * structure. The structure is required for machine check happening
64c7ed509bSAlexander Gordeev  * early in the boot process.
656c81511cSMartin Schwidefsky  */
6612dd19c1SAlexander Gordeev static struct mcesa boot_mcesa __aligned(MCESA_MAX_SIZE);
676c81511cSMartin Schwidefsky 
68c7ed509bSAlexander Gordeev void __init nmi_alloc_mcesa_early(u64 *mcesad)
696c81511cSMartin Schwidefsky {
706c81511cSMartin Schwidefsky 	if (!nmi_needs_mcesa())
716c81511cSMartin Schwidefsky 		return;
72c7ed509bSAlexander Gordeev 	*mcesad = __pa(&boot_mcesa);
736c81511cSMartin Schwidefsky 	if (MACHINE_HAS_GS)
74c7ed509bSAlexander Gordeev 		*mcesad |= ilog2(MCESA_MAX_SIZE);
756c81511cSMartin Schwidefsky }
766c81511cSMartin Schwidefsky 
77c7ed509bSAlexander Gordeev static void __init nmi_alloc_cache(void)
786c81511cSMartin Schwidefsky {
79c7ed509bSAlexander Gordeev 	unsigned long size;
806c81511cSMartin Schwidefsky 
816c81511cSMartin Schwidefsky 	if (!nmi_needs_mcesa())
82c7ed509bSAlexander Gordeev 		return;
836c81511cSMartin Schwidefsky 	size = nmi_get_mcesa_size();
846c81511cSMartin Schwidefsky 	if (size > MCESA_MIN_SIZE)
856c81511cSMartin Schwidefsky 		mcesa_origin_lc = ilog2(size);
866c81511cSMartin Schwidefsky 	/* create slab cache for the machine-check-extended-save-areas */
876c81511cSMartin Schwidefsky 	mcesa_cache = kmem_cache_create("nmi_save_areas", size, size, 0, NULL);
886c81511cSMartin Schwidefsky 	if (!mcesa_cache)
896c81511cSMartin Schwidefsky 		panic("Couldn't create nmi save area cache");
906c81511cSMartin Schwidefsky }
916c81511cSMartin Schwidefsky 
92c7ed509bSAlexander Gordeev int __ref nmi_alloc_mcesa(u64 *mcesad)
936c81511cSMartin Schwidefsky {
946c81511cSMartin Schwidefsky 	unsigned long origin;
956c81511cSMartin Schwidefsky 
96c7ed509bSAlexander Gordeev 	*mcesad = 0;
976c81511cSMartin Schwidefsky 	if (!nmi_needs_mcesa())
986c81511cSMartin Schwidefsky 		return 0;
99c7ed509bSAlexander Gordeev 	if (!mcesa_cache)
100c7ed509bSAlexander Gordeev 		nmi_alloc_cache();
1016c81511cSMartin Schwidefsky 	origin = (unsigned long) kmem_cache_alloc(mcesa_cache, GFP_KERNEL);
1026c81511cSMartin Schwidefsky 	if (!origin)
1036c81511cSMartin Schwidefsky 		return -ENOMEM;
1046c81511cSMartin Schwidefsky 	/* The pointer is stored with mcesa_bits ORed in */
1056c81511cSMartin Schwidefsky 	kmemleak_not_leak((void *) origin);
106c7ed509bSAlexander Gordeev 	*mcesad = __pa(origin) | mcesa_origin_lc;
1076c81511cSMartin Schwidefsky 	return 0;
1086c81511cSMartin Schwidefsky }
1096c81511cSMartin Schwidefsky 
110c7ed509bSAlexander Gordeev void nmi_free_mcesa(u64 *mcesad)
1116c81511cSMartin Schwidefsky {
1126c81511cSMartin Schwidefsky 	if (!nmi_needs_mcesa())
1136c81511cSMartin Schwidefsky 		return;
114c7ed509bSAlexander Gordeev 	kmem_cache_free(mcesa_cache, __va(*mcesad & MCESA_ORIGIN_MASK));
1156c81511cSMartin Schwidefsky }
116f5daba1dSHeiko Carstens 
11700a8f886SMartin Schwidefsky static notrace void s390_handle_damage(void)
118f5daba1dSHeiko Carstens {
11900a8f886SMartin Schwidefsky 	smp_emergency_stop();
12098587c2dSMartin Schwidefsky 	disabled_wait();
121f5daba1dSHeiko Carstens 	while (1);
122f5daba1dSHeiko Carstens }
12300a8f886SMartin Schwidefsky NOKPROBE_SYMBOL(s390_handle_damage);
124f5daba1dSHeiko Carstens 
125f5daba1dSHeiko Carstens /*
12656e62a73SSven Schnelle  * Main machine check handler function. Will be called with interrupts disabled
12756e62a73SSven Schnelle  * and machine checks enabled.
128f5daba1dSHeiko Carstens  */
12956e62a73SSven Schnelle void __s390_handle_mcck(void)
130f5daba1dSHeiko Carstens {
131f5daba1dSHeiko Carstens 	struct mcck_struct mcck;
132f5daba1dSHeiko Carstens 
133f5daba1dSHeiko Carstens 	/*
134f5daba1dSHeiko Carstens 	 * Disable machine checks and get the current state of accumulated
135f5daba1dSHeiko Carstens 	 * machine checks. Afterwards delete the old state and enable machine
136f5daba1dSHeiko Carstens 	 * checks again.
137f5daba1dSHeiko Carstens 	 */
138f5daba1dSHeiko Carstens 	local_mcck_disable();
1392cb4a182SSebastian Ott 	mcck = *this_cpu_ptr(&cpu_mcck);
1402cb4a182SSebastian Ott 	memset(this_cpu_ptr(&cpu_mcck), 0, sizeof(mcck));
141f5daba1dSHeiko Carstens 	local_mcck_enable();
142f5daba1dSHeiko Carstens 
143f5daba1dSHeiko Carstens 	if (mcck.channel_report)
144f5daba1dSHeiko Carstens 		crw_handle_channel_report();
145f5daba1dSHeiko Carstens 	/*
1467b886416SHeiko Carstens 	 * A warning may remain for a prolonged period on the bare iron.
1477b886416SHeiko Carstens 	 * (actually until the machine is powered off, or the problem is gone)
1487b886416SHeiko Carstens 	 * So we just stop listening for the WARNING MCH and avoid continuously
149f5daba1dSHeiko Carstens 	 * being interrupted.  One caveat is however, that we must do this per
150f5daba1dSHeiko Carstens 	 * processor and cannot use the smp version of ctl_clear_bit().
151f5daba1dSHeiko Carstens 	 * On VM we only get one interrupt per virtally presented machinecheck.
1527b886416SHeiko Carstens 	 * Though one suffices, we may get one interrupt per (virtual) cpu.
153f5daba1dSHeiko Carstens 	 */
154f5daba1dSHeiko Carstens 	if (mcck.warning) {	/* WARNING pending ? */
155f5daba1dSHeiko Carstens 		static int mchchk_wng_posted = 0;
1567b886416SHeiko Carstens 
1577b886416SHeiko Carstens 		/* Use single cpu clear, as we cannot handle smp here. */
158f5daba1dSHeiko Carstens 		__ctl_clear_bit(14, 24);	/* Disable WARNING MCH */
159f5daba1dSHeiko Carstens 		if (xchg(&mchchk_wng_posted, 1) == 0)
160f5daba1dSHeiko Carstens 			kill_cad_pid(SIGPWR, 1);
161f5daba1dSHeiko Carstens 	}
16229b0a825SHeiko Carstens 	if (mcck.stp_queue)
16329b0a825SHeiko Carstens 		stp_queue_work();
164f5daba1dSHeiko Carstens 	if (mcck.kill_task) {
165f5daba1dSHeiko Carstens 		local_irq_enable();
166f5daba1dSHeiko Carstens 		printk(KERN_EMERG "mcck: Terminating task because of machine "
167dc6e1555SHeiko Carstens 		       "malfunction (code 0x%016lx).\n", mcck.mcck_code);
168f5daba1dSHeiko Carstens 		printk(KERN_EMERG "mcck: task: %s, pid: %d.\n",
169f5daba1dSHeiko Carstens 		       current->comm, current->pid);
1700e25498fSEric W. Biederman 		make_task_dead(SIGSEGV);
171f5daba1dSHeiko Carstens 	}
172f5daba1dSHeiko Carstens }
173f5daba1dSHeiko Carstens 
17439d62336SThomas Richter void noinstr s390_handle_mcck(struct pt_regs *regs)
17556e62a73SSven Schnelle {
17656e62a73SSven Schnelle 	trace_hardirqs_off();
17739d62336SThomas Richter 	pai_kernel_enter(regs);
17856e62a73SSven Schnelle 	__s390_handle_mcck();
17939d62336SThomas Richter 	pai_kernel_exit(regs);
18056e62a73SSven Schnelle 	trace_hardirqs_on();
18156e62a73SSven Schnelle }
182f5daba1dSHeiko Carstens /*
183*f9e5938aSHeiko Carstens  * returns 0 if register contents could be validated
184f5daba1dSHeiko Carstens  * returns 1 otherwise
185f5daba1dSHeiko Carstens  */
186*f9e5938aSHeiko Carstens static int notrace s390_validate_registers(union mci mci)
187f5daba1dSHeiko Carstens {
1885fa2ea07SAlexander Gordeev 	struct mcesa *mcesa;
1895fa2ea07SAlexander Gordeev 	void *fpt_save_area;
190ad3bc0acSMartin Schwidefsky 	union ctlreg2 cr2;
191f5daba1dSHeiko Carstens 	int kill_task;
1925fa2ea07SAlexander Gordeev 	u64 zero;
193f5daba1dSHeiko Carstens 
194f5daba1dSHeiko Carstens 	kill_task = 0;
1955fa2ea07SAlexander Gordeev 	zero = 0;
196f5daba1dSHeiko Carstens 
197*f9e5938aSHeiko Carstens 	if (!mci.gr || !mci.fp)
198f5daba1dSHeiko Carstens 		kill_task = 1;
1995fa2ea07SAlexander Gordeev 	fpt_save_area = &S390_lowcore.floating_pt_save_area;
200dc6e1555SHeiko Carstens 	if (!mci.fc) {
201*f9e5938aSHeiko Carstens 		kill_task = 1;
2025fa2ea07SAlexander Gordeev 		asm volatile(
2035fa2ea07SAlexander Gordeev 			"	lfpc	%0\n"
2045fa2ea07SAlexander Gordeev 			:
2055fa2ea07SAlexander Gordeev 			: "Q" (zero));
2065fa2ea07SAlexander Gordeev 	} else {
2075fa2ea07SAlexander Gordeev 		asm volatile(
2085fa2ea07SAlexander Gordeev 			"	lfpc	%0\n"
2095fa2ea07SAlexander Gordeev 			:
2105fa2ea07SAlexander Gordeev 			: "Q" (S390_lowcore.fpt_creg_save_area));
21186fa7087SHeiko Carstens 	}
212f5daba1dSHeiko Carstens 
213402ff5a3SHeiko Carstens 	mcesa = __va(S390_lowcore.mcesad & MCESA_ORIGIN_MASK);
2145fa2ea07SAlexander Gordeev 	if (!MACHINE_HAS_VX) {
2155fa2ea07SAlexander Gordeev 		/* Validate floating point registers */
2165fa2ea07SAlexander Gordeev 		asm volatile(
2175fa2ea07SAlexander Gordeev 			"	ld	0,0(%0)\n"
2185fa2ea07SAlexander Gordeev 			"	ld	1,8(%0)\n"
2195fa2ea07SAlexander Gordeev 			"	ld	2,16(%0)\n"
2205fa2ea07SAlexander Gordeev 			"	ld	3,24(%0)\n"
2215fa2ea07SAlexander Gordeev 			"	ld	4,32(%0)\n"
2225fa2ea07SAlexander Gordeev 			"	ld	5,40(%0)\n"
2235fa2ea07SAlexander Gordeev 			"	ld	6,48(%0)\n"
2245fa2ea07SAlexander Gordeev 			"	ld	7,56(%0)\n"
2255fa2ea07SAlexander Gordeev 			"	ld	8,64(%0)\n"
2265fa2ea07SAlexander Gordeev 			"	ld	9,72(%0)\n"
2275fa2ea07SAlexander Gordeev 			"	ld	10,80(%0)\n"
2285fa2ea07SAlexander Gordeev 			"	ld	11,88(%0)\n"
2295fa2ea07SAlexander Gordeev 			"	ld	12,96(%0)\n"
2305fa2ea07SAlexander Gordeev 			"	ld	13,104(%0)\n"
2315fa2ea07SAlexander Gordeev 			"	ld	14,112(%0)\n"
2325fa2ea07SAlexander Gordeev 			"	ld	15,120(%0)\n"
2335fa2ea07SAlexander Gordeev 			:
2345fa2ea07SAlexander Gordeev 			: "a" (fpt_save_area)
2355fa2ea07SAlexander Gordeev 			: "memory");
2365fa2ea07SAlexander Gordeev 	} else {
2375fa2ea07SAlexander Gordeev 		/* Validate vector registers */
2385fa2ea07SAlexander Gordeev 		union ctlreg0 cr0;
2395fa2ea07SAlexander Gordeev 
240f094a39cSChristian Borntraeger 		/*
241f094a39cSChristian Borntraeger 		 * The vector validity must only be checked if not running a
242f094a39cSChristian Borntraeger 		 * KVM guest. For KVM guests the machine check is forwarded by
243f094a39cSChristian Borntraeger 		 * KVM and it is the responsibility of the guest to take
244f094a39cSChristian Borntraeger 		 * appropriate actions. The host vector or FPU values have been
245f094a39cSChristian Borntraeger 		 * saved by KVM and will be restored by KVM.
246f094a39cSChristian Borntraeger 		 */
247*f9e5938aSHeiko Carstens 		if (!mci.vr && !test_cpu_flag(CIF_MCCK_GUEST))
24880703617SMartin Schwidefsky 			kill_task = 1;
2495fa2ea07SAlexander Gordeev 		cr0.val = S390_lowcore.cregs_save_area[0];
2505fa2ea07SAlexander Gordeev 		cr0.afp = cr0.vx = 1;
2515fa2ea07SAlexander Gordeev 		__ctl_load(cr0.val, 0, 0);
2525fa2ea07SAlexander Gordeev 		asm volatile(
2535fa2ea07SAlexander Gordeev 			"	la	1,%0\n"
2545720aab2SHeiko Carstens 			"	VLM	0,15,0,1\n"
2555720aab2SHeiko Carstens 			"	VLM	16,31,256,1\n"
2565fa2ea07SAlexander Gordeev 			:
2575fa2ea07SAlexander Gordeev 			: "Q" (*(struct vx_array *)mcesa->vector_save_area)
2585fa2ea07SAlexander Gordeev 			: "1");
2595fa2ea07SAlexander Gordeev 		__ctl_load(S390_lowcore.cregs_save_area[0], 0, 0);
26080703617SMartin Schwidefsky 	}
2615fa2ea07SAlexander Gordeev 	/* Validate access registers */
2625fa2ea07SAlexander Gordeev 	asm volatile(
2635fa2ea07SAlexander Gordeev 		"	lam	0,15,0(%0)\n"
2645fa2ea07SAlexander Gordeev 		:
2655fa2ea07SAlexander Gordeev 		: "a" (&S390_lowcore.access_regs_save_area)
2665fa2ea07SAlexander Gordeev 		: "memory");
267*f9e5938aSHeiko Carstens 	if (!mci.ar)
268f5daba1dSHeiko Carstens 		kill_task = 1;
2695fa2ea07SAlexander Gordeev 	/* Validate guarded storage registers */
270ad3bc0acSMartin Schwidefsky 	cr2.val = S390_lowcore.cregs_save_area[2];
271ad3bc0acSMartin Schwidefsky 	if (cr2.gse) {
2723037a52fSMartin Schwidefsky 		if (!mci.gs) {
273916cda1aSMartin Schwidefsky 			/*
2741ea1d6a8SChristian Borntraeger 			 * 2 cases:
2751ea1d6a8SChristian Borntraeger 			 * - machine check in kernel or userspace
2761ea1d6a8SChristian Borntraeger 			 * - machine check while running SIE (KVM guest)
2771ea1d6a8SChristian Borntraeger 			 * For kernel or userspace the userspace values of
2781ea1d6a8SChristian Borntraeger 			 * guarded storage control can not be recreated, the
2791ea1d6a8SChristian Borntraeger 			 * process must be terminated.
2801ea1d6a8SChristian Borntraeger 			 * For SIE the guest values of guarded storage can not
2811ea1d6a8SChristian Borntraeger 			 * be recreated. This is either due to a bug or due to
2821ea1d6a8SChristian Borntraeger 			 * GS being disabled in the guest. The guest will be
2831ea1d6a8SChristian Borntraeger 			 * notified by KVM code and the guests machine check
2841ea1d6a8SChristian Borntraeger 			 * handling must take care of this.  The host values
2851ea1d6a8SChristian Borntraeger 			 * are saved by KVM and are not affected.
286916cda1aSMartin Schwidefsky 			 */
2871ea1d6a8SChristian Borntraeger 			if (!test_cpu_flag(CIF_MCCK_GUEST))
288916cda1aSMartin Schwidefsky 				kill_task = 1;
2895fa2ea07SAlexander Gordeev 		} else {
2905fa2ea07SAlexander Gordeev 			load_gs_cb((struct gs_cb *)mcesa->guarded_storage_save_area);
291916cda1aSMartin Schwidefsky 		}
2923037a52fSMartin Schwidefsky 	}
2935fa2ea07SAlexander Gordeev 	/*
2945fa2ea07SAlexander Gordeev 	 * The getcpu vdso syscall reads CPU number from the programmable
2955fa2ea07SAlexander Gordeev 	 * field of the TOD clock. Disregard the TOD programmable register
2965fa2ea07SAlexander Gordeev 	 * validity bit and load the CPU number into the TOD programmable
2975fa2ea07SAlexander Gordeev 	 * field unconditionally.
2985fa2ea07SAlexander Gordeev 	 */
2995fa2ea07SAlexander Gordeev 	set_tod_programmable_field(raw_smp_processor_id());
3005fa2ea07SAlexander Gordeev 	/* Validate clock comparator register */
3015fa2ea07SAlexander Gordeev 	set_clock_comparator(S390_lowcore.clock_comparator);
302f5daba1dSHeiko Carstens 
303dc6e1555SHeiko Carstens 	if (!mci.ms || !mci.pm || !mci.ia)
304f5daba1dSHeiko Carstens 		kill_task = 1;
305f5daba1dSHeiko Carstens 
306f5daba1dSHeiko Carstens 	return kill_task;
307f5daba1dSHeiko Carstens }
3085fa2ea07SAlexander Gordeev NOKPROBE_SYMBOL(s390_validate_registers);
309f5daba1dSHeiko Carstens 
310da72ca4dSQingFeng Hao /*
311da72ca4dSQingFeng Hao  * Backup the guest's machine check info to its description block
312da72ca4dSQingFeng Hao  */
313da72ca4dSQingFeng Hao static void notrace s390_backup_mcck_info(struct pt_regs *regs)
314da72ca4dSQingFeng Hao {
315da72ca4dSQingFeng Hao 	struct mcck_volatile_info *mcck_backup;
316da72ca4dSQingFeng Hao 	struct sie_page *sie_page;
317da72ca4dSQingFeng Hao 
318da72ca4dSQingFeng Hao 	/* r14 contains the sie block, which was set in sie64a */
319da72ca4dSQingFeng Hao 	struct kvm_s390_sie_block *sie_block =
320da72ca4dSQingFeng Hao 			(struct kvm_s390_sie_block *) regs->gprs[14];
321da72ca4dSQingFeng Hao 
322da72ca4dSQingFeng Hao 	if (sie_block == NULL)
323da72ca4dSQingFeng Hao 		/* Something's seriously wrong, stop system. */
324da72ca4dSQingFeng Hao 		s390_handle_damage();
325da72ca4dSQingFeng Hao 
326da72ca4dSQingFeng Hao 	sie_page = container_of(sie_block, struct sie_page, sie_block);
327da72ca4dSQingFeng Hao 	mcck_backup = &sie_page->mcck_info;
328da72ca4dSQingFeng Hao 	mcck_backup->mcic = S390_lowcore.mcck_interruption_code &
329da72ca4dSQingFeng Hao 				~(MCCK_CODE_CP | MCCK_CODE_EXT_DAMAGE);
330da72ca4dSQingFeng Hao 	mcck_backup->ext_damage_code = S390_lowcore.external_damage_code;
331da72ca4dSQingFeng Hao 	mcck_backup->failing_storage_address
332da72ca4dSQingFeng Hao 			= S390_lowcore.failing_storage_address;
333da72ca4dSQingFeng Hao }
33400a8f886SMartin Schwidefsky NOKPROBE_SYMBOL(s390_backup_mcck_info);
335da72ca4dSQingFeng Hao 
336f5daba1dSHeiko Carstens #define MAX_IPD_COUNT	29
337f5daba1dSHeiko Carstens #define MAX_IPD_TIME	(5 * 60 * USEC_PER_SEC) /* 5 minutes */
338f5daba1dSHeiko Carstens 
339f5daba1dSHeiko Carstens #define ED_STP_ISLAND	6	/* External damage STP island check */
340f5daba1dSHeiko Carstens #define ED_STP_SYNC	7	/* External damage STP sync check */
341f5daba1dSHeiko Carstens 
342c929500dSQingFeng Hao #define MCCK_CODE_NO_GUEST	(MCCK_CODE_CP | MCCK_CODE_EXT_DAMAGE)
343c929500dSQingFeng Hao 
344f5daba1dSHeiko Carstens /*
345f5daba1dSHeiko Carstens  * machine check handler.
346f5daba1dSHeiko Carstens  */
3470b0ed657SSven Schnelle int notrace s390_do_machine_check(struct pt_regs *regs)
348f5daba1dSHeiko Carstens {
349f5daba1dSHeiko Carstens 	static int ipd_count;
350f5daba1dSHeiko Carstens 	static DEFINE_SPINLOCK(ipd_lock);
351f5daba1dSHeiko Carstens 	static unsigned long long last_ipd;
352f5daba1dSHeiko Carstens 	struct mcck_struct *mcck;
353f5daba1dSHeiko Carstens 	unsigned long long tmp;
354520763a3SSven Schnelle 	irqentry_state_t irq_state;
355dc6e1555SHeiko Carstens 	union mci mci;
356c929500dSQingFeng Hao 	unsigned long mcck_dam_code;
3570b0ed657SSven Schnelle 	int mcck_pending = 0;
358f5daba1dSHeiko Carstens 
359520763a3SSven Schnelle 	irq_state = irqentry_nmi_enter(regs);
36056e62a73SSven Schnelle 
36156e62a73SSven Schnelle 	if (user_mode(regs))
36256e62a73SSven Schnelle 		update_timer_mcck();
363420f42ecSHeiko Carstens 	inc_irq_stat(NMI_NMI);
364dc6e1555SHeiko Carstens 	mci.val = S390_lowcore.mcck_interruption_code;
365eb7e7d76SChristoph Lameter 	mcck = this_cpu_ptr(&cpu_mcck);
366f5daba1dSHeiko Carstens 
367c929500dSQingFeng Hao 	/*
368c929500dSQingFeng Hao 	 * Reinject the instruction processing damages' machine checks
369c929500dSQingFeng Hao 	 * including Delayed Access Exception into the guest
370c929500dSQingFeng Hao 	 * instead of damaging the host if they happen in the guest.
371c929500dSQingFeng Hao 	 */
372c929500dSQingFeng Hao 	if (mci.pd && !test_cpu_flag(CIF_MCCK_GUEST)) {
373dc6e1555SHeiko Carstens 		if (mci.b) {
374f5daba1dSHeiko Carstens 			/* Processing backup -> verify if we can survive this */
375f5daba1dSHeiko Carstens 			u64 z_mcic, o_mcic, t_mcic;
376f5daba1dSHeiko Carstens 			z_mcic = (1ULL<<63 | 1ULL<<59 | 1ULL<<29);
377f5daba1dSHeiko Carstens 			o_mcic = (1ULL<<43 | 1ULL<<42 | 1ULL<<41 | 1ULL<<40 |
378f5daba1dSHeiko Carstens 				  1ULL<<36 | 1ULL<<35 | 1ULL<<34 | 1ULL<<32 |
379f5daba1dSHeiko Carstens 				  1ULL<<30 | 1ULL<<21 | 1ULL<<20 | 1ULL<<17 |
380f5daba1dSHeiko Carstens 				  1ULL<<16);
381dc6e1555SHeiko Carstens 			t_mcic = mci.val;
382f5daba1dSHeiko Carstens 
383f5daba1dSHeiko Carstens 			if (((t_mcic & z_mcic) != 0) ||
384f5daba1dSHeiko Carstens 			    ((t_mcic & o_mcic) != o_mcic)) {
3853d68286aSHeiko Carstens 				s390_handle_damage();
386f5daba1dSHeiko Carstens 			}
387f5daba1dSHeiko Carstens 
388f5daba1dSHeiko Carstens 			/*
389f5daba1dSHeiko Carstens 			 * Nullifying exigent condition, therefore we might
390f5daba1dSHeiko Carstens 			 * retry this instruction.
391f5daba1dSHeiko Carstens 			 */
392f5daba1dSHeiko Carstens 			spin_lock(&ipd_lock);
3931aae0560SHeiko Carstens 			tmp = get_tod_clock();
394f5daba1dSHeiko Carstens 			if (((tmp - last_ipd) >> 12) < MAX_IPD_TIME)
395f5daba1dSHeiko Carstens 				ipd_count++;
396f5daba1dSHeiko Carstens 			else
397f5daba1dSHeiko Carstens 				ipd_count = 1;
398f5daba1dSHeiko Carstens 			last_ipd = tmp;
399f5daba1dSHeiko Carstens 			if (ipd_count == MAX_IPD_COUNT)
4003d68286aSHeiko Carstens 				s390_handle_damage();
401f5daba1dSHeiko Carstens 			spin_unlock(&ipd_lock);
402f5daba1dSHeiko Carstens 		} else {
403f5daba1dSHeiko Carstens 			/* Processing damage -> stopping machine */
4043d68286aSHeiko Carstens 			s390_handle_damage();
405f5daba1dSHeiko Carstens 		}
406f5daba1dSHeiko Carstens 	}
407*f9e5938aSHeiko Carstens 	if (s390_validate_registers(mci)) {
408*f9e5938aSHeiko Carstens 		if (!user_mode(regs))
409*f9e5938aSHeiko Carstens 			s390_handle_damage();
410f5daba1dSHeiko Carstens 		/*
4118f149ea6SMartin Schwidefsky 		 * Couldn't restore all register contents for the
4128f149ea6SMartin Schwidefsky 		 * user space process -> mark task for termination.
413f5daba1dSHeiko Carstens 		 */
414f5daba1dSHeiko Carstens 		mcck->kill_task = 1;
415dc6e1555SHeiko Carstens 		mcck->mcck_code = mci.val;
4160b0ed657SSven Schnelle 		mcck_pending = 1;
417f5daba1dSHeiko Carstens 	}
418da72ca4dSQingFeng Hao 
419da72ca4dSQingFeng Hao 	/*
420da72ca4dSQingFeng Hao 	 * Backup the machine check's info if it happens when the guest
421da72ca4dSQingFeng Hao 	 * is running.
422da72ca4dSQingFeng Hao 	 */
423da72ca4dSQingFeng Hao 	if (test_cpu_flag(CIF_MCCK_GUEST))
424da72ca4dSQingFeng Hao 		s390_backup_mcck_info(regs);
425da72ca4dSQingFeng Hao 
426dc6e1555SHeiko Carstens 	if (mci.cd) {
427f5daba1dSHeiko Carstens 		/* Timing facility damage */
4283d68286aSHeiko Carstens 		s390_handle_damage();
429f5daba1dSHeiko Carstens 	}
430dc6e1555SHeiko Carstens 	if (mci.ed && mci.ec) {
431f5daba1dSHeiko Carstens 		/* External damage */
432f5daba1dSHeiko Carstens 		if (S390_lowcore.external_damage_code & (1U << ED_STP_SYNC))
43329b0a825SHeiko Carstens 			mcck->stp_queue |= stp_sync_check();
434f5daba1dSHeiko Carstens 		if (S390_lowcore.external_damage_code & (1U << ED_STP_ISLAND))
43529b0a825SHeiko Carstens 			mcck->stp_queue |= stp_island_check();
4360b0ed657SSven Schnelle 		mcck_pending = 1;
437f5daba1dSHeiko Carstens 	}
438c929500dSQingFeng Hao 
439dc6e1555SHeiko Carstens 	if (mci.cp) {
440f5daba1dSHeiko Carstens 		/* Channel report word pending */
441f5daba1dSHeiko Carstens 		mcck->channel_report = 1;
4420b0ed657SSven Schnelle 		mcck_pending = 1;
443f5daba1dSHeiko Carstens 	}
444dc6e1555SHeiko Carstens 	if (mci.w) {
445f5daba1dSHeiko Carstens 		/* Warning pending */
446f5daba1dSHeiko Carstens 		mcck->warning = 1;
4470b0ed657SSven Schnelle 		mcck_pending = 1;
448f5daba1dSHeiko Carstens 	}
449c929500dSQingFeng Hao 
450c929500dSQingFeng Hao 	/*
451c929500dSQingFeng Hao 	 * If there are only Channel Report Pending and External Damage
452c929500dSQingFeng Hao 	 * machine checks, they will not be reinjected into the guest
453c929500dSQingFeng Hao 	 * because they refer to host conditions only.
454c929500dSQingFeng Hao 	 */
455c929500dSQingFeng Hao 	mcck_dam_code = (mci.val & MCIC_SUBCLASS_MASK);
456c929500dSQingFeng Hao 	if (test_cpu_flag(CIF_MCCK_GUEST) &&
457c929500dSQingFeng Hao 	(mcck_dam_code & MCCK_CODE_NO_GUEST) != mcck_dam_code) {
458c929500dSQingFeng Hao 		/* Set exit reason code for host's later handling */
459c929500dSQingFeng Hao 		*((long *)(regs->gprs[15] + __SF_SIE_REASON)) = -EINTR;
460c929500dSQingFeng Hao 	}
461c929500dSQingFeng Hao 	clear_cpu_flag(CIF_MCCK_GUEST);
4620b0ed657SSven Schnelle 
4630b0ed657SSven Schnelle 	if (user_mode(regs) && mcck_pending) {
464520763a3SSven Schnelle 		irqentry_nmi_exit(regs, irq_state);
4650b0ed657SSven Schnelle 		return 1;
4660b0ed657SSven Schnelle 	}
4670b0ed657SSven Schnelle 
4680b0ed657SSven Schnelle 	if (mcck_pending)
4690b0ed657SSven Schnelle 		schedule_mcck_handler();
4700b0ed657SSven Schnelle 
471520763a3SSven Schnelle 	irqentry_nmi_exit(regs, irq_state);
4720b0ed657SSven Schnelle 	return 0;
473f5daba1dSHeiko Carstens }
47400a8f886SMartin Schwidefsky NOKPROBE_SYMBOL(s390_do_machine_check);
475f5daba1dSHeiko Carstens 
476f5daba1dSHeiko Carstens static int __init machine_check_init(void)
477f5daba1dSHeiko Carstens {
478f5daba1dSHeiko Carstens 	ctl_set_bit(14, 25);	/* enable external damage MCH */
479f5daba1dSHeiko Carstens 	ctl_set_bit(14, 27);	/* enable system recovery MCH */
480f5daba1dSHeiko Carstens 	ctl_set_bit(14, 24);	/* enable warning MCH */
481f5daba1dSHeiko Carstens 	return 0;
482f5daba1dSHeiko Carstens }
48324d05ff8SHeiko Carstens early_initcall(machine_check_init);
484