xref: /openbmc/linux/arch/s390/kernel/nmi.c (revision 916cda1a)
1 /*
2  *   Machine check handler
3  *
4  *    Copyright IBM Corp. 2000, 2009
5  *    Author(s): Ingo Adlung <adlung@de.ibm.com>,
6  *		 Martin Schwidefsky <schwidefsky@de.ibm.com>,
7  *		 Cornelia Huck <cornelia.huck@de.ibm.com>,
8  *		 Heiko Carstens <heiko.carstens@de.ibm.com>,
9  */
10 
11 #include <linux/kernel_stat.h>
12 #include <linux/init.h>
13 #include <linux/errno.h>
14 #include <linux/hardirq.h>
15 #include <linux/time.h>
16 #include <linux/module.h>
17 #include <linux/sched/signal.h>
18 
19 #include <linux/export.h>
20 #include <asm/lowcore.h>
21 #include <asm/smp.h>
22 #include <asm/stp.h>
23 #include <asm/cputime.h>
24 #include <asm/nmi.h>
25 #include <asm/crw.h>
26 #include <asm/switch_to.h>
27 #include <asm/ctl_reg.h>
28 
29 struct mcck_struct {
30 	unsigned int kill_task : 1;
31 	unsigned int channel_report : 1;
32 	unsigned int warning : 1;
33 	unsigned int stp_queue : 1;
34 	unsigned long mcck_code;
35 };
36 
37 static DEFINE_PER_CPU(struct mcck_struct, cpu_mcck);
38 
39 static void s390_handle_damage(void)
40 {
41 	smp_send_stop();
42 	disabled_wait((unsigned long) __builtin_return_address(0));
43 	while (1);
44 }
45 
46 /*
47  * Main machine check handler function. Will be called with interrupts enabled
48  * or disabled and machine checks enabled or disabled.
49  */
50 void s390_handle_mcck(void)
51 {
52 	unsigned long flags;
53 	struct mcck_struct mcck;
54 
55 	/*
56 	 * Disable machine checks and get the current state of accumulated
57 	 * machine checks. Afterwards delete the old state and enable machine
58 	 * checks again.
59 	 */
60 	local_irq_save(flags);
61 	local_mcck_disable();
62 	mcck = *this_cpu_ptr(&cpu_mcck);
63 	memset(this_cpu_ptr(&cpu_mcck), 0, sizeof(mcck));
64 	clear_cpu_flag(CIF_MCCK_PENDING);
65 	local_mcck_enable();
66 	local_irq_restore(flags);
67 
68 	if (mcck.channel_report)
69 		crw_handle_channel_report();
70 	/*
71 	 * A warning may remain for a prolonged period on the bare iron.
72 	 * (actually until the machine is powered off, or the problem is gone)
73 	 * So we just stop listening for the WARNING MCH and avoid continuously
74 	 * being interrupted.  One caveat is however, that we must do this per
75 	 * processor and cannot use the smp version of ctl_clear_bit().
76 	 * On VM we only get one interrupt per virtally presented machinecheck.
77 	 * Though one suffices, we may get one interrupt per (virtual) cpu.
78 	 */
79 	if (mcck.warning) {	/* WARNING pending ? */
80 		static int mchchk_wng_posted = 0;
81 
82 		/* Use single cpu clear, as we cannot handle smp here. */
83 		__ctl_clear_bit(14, 24);	/* Disable WARNING MCH */
84 		if (xchg(&mchchk_wng_posted, 1) == 0)
85 			kill_cad_pid(SIGPWR, 1);
86 	}
87 	if (mcck.stp_queue)
88 		stp_queue_work();
89 	if (mcck.kill_task) {
90 		local_irq_enable();
91 		printk(KERN_EMERG "mcck: Terminating task because of machine "
92 		       "malfunction (code 0x%016lx).\n", mcck.mcck_code);
93 		printk(KERN_EMERG "mcck: task: %s, pid: %d.\n",
94 		       current->comm, current->pid);
95 		do_exit(SIGSEGV);
96 	}
97 }
98 EXPORT_SYMBOL_GPL(s390_handle_mcck);
99 
100 /*
101  * returns 0 if all registers could be validated
102  * returns 1 otherwise
103  */
104 static int notrace s390_validate_registers(union mci mci, int umode)
105 {
106 	int kill_task;
107 	u64 zero;
108 	void *fpt_save_area;
109 	struct mcesa *mcesa;
110 
111 	kill_task = 0;
112 	zero = 0;
113 
114 	if (!mci.gr) {
115 		/*
116 		 * General purpose registers couldn't be restored and have
117 		 * unknown contents. Stop system or terminate process.
118 		 */
119 		if (!umode)
120 			s390_handle_damage();
121 		kill_task = 1;
122 	}
123 	/* Validate control registers */
124 	if (!mci.cr) {
125 		/*
126 		 * Control registers have unknown contents.
127 		 * Can't recover and therefore stopping machine.
128 		 */
129 		s390_handle_damage();
130 	} else {
131 		asm volatile(
132 			"	lctlg	0,15,0(%0)\n"
133 			"	ptlb\n"
134 			: : "a" (&S390_lowcore.cregs_save_area) : "memory");
135 	}
136 	if (!mci.fp) {
137 		/*
138 		 * Floating point registers can't be restored. If the
139 		 * kernel currently uses floating point registers the
140 		 * system is stopped. If the process has its floating
141 		 * pointer registers loaded it is terminated.
142 		 * Otherwise just revalidate the registers.
143 		 */
144 		if (S390_lowcore.fpu_flags & KERNEL_VXR_V0V7)
145 			s390_handle_damage();
146 		if (!test_cpu_flag(CIF_FPU))
147 			kill_task = 1;
148 	}
149 	fpt_save_area = &S390_lowcore.floating_pt_save_area;
150 	if (!mci.fc) {
151 		/*
152 		 * Floating point control register can't be restored.
153 		 * If the kernel currently uses the floating pointer
154 		 * registers and needs the FPC register the system is
155 		 * stopped. If the process has its floating pointer
156 		 * registers loaded it is terminated. Otherwiese the
157 		 * FPC is just revalidated.
158 		 */
159 		if (S390_lowcore.fpu_flags & KERNEL_FPC)
160 			s390_handle_damage();
161 		asm volatile("lfpc %0" : : "Q" (zero));
162 		if (!test_cpu_flag(CIF_FPU))
163 			kill_task = 1;
164 	} else {
165 		asm volatile("lfpc %0"
166 			     : : "Q" (S390_lowcore.fpt_creg_save_area));
167 	}
168 
169 	mcesa = (struct mcesa *)(S390_lowcore.mcesad & MCESA_ORIGIN_MASK);
170 	if (!MACHINE_HAS_VX) {
171 		/* Validate floating point registers */
172 		asm volatile(
173 			"	ld	0,0(%0)\n"
174 			"	ld	1,8(%0)\n"
175 			"	ld	2,16(%0)\n"
176 			"	ld	3,24(%0)\n"
177 			"	ld	4,32(%0)\n"
178 			"	ld	5,40(%0)\n"
179 			"	ld	6,48(%0)\n"
180 			"	ld	7,56(%0)\n"
181 			"	ld	8,64(%0)\n"
182 			"	ld	9,72(%0)\n"
183 			"	ld	10,80(%0)\n"
184 			"	ld	11,88(%0)\n"
185 			"	ld	12,96(%0)\n"
186 			"	ld	13,104(%0)\n"
187 			"	ld	14,112(%0)\n"
188 			"	ld	15,120(%0)\n"
189 			: : "a" (fpt_save_area) : "memory");
190 	} else {
191 		/* Validate vector registers */
192 		union ctlreg0 cr0;
193 
194 		if (!mci.vr) {
195 			/*
196 			 * Vector registers can't be restored. If the kernel
197 			 * currently uses vector registers the system is
198 			 * stopped. If the process has its vector registers
199 			 * loaded it is terminated. Otherwise just revalidate
200 			 * the registers.
201 			 */
202 			if (S390_lowcore.fpu_flags & KERNEL_VXR)
203 				s390_handle_damage();
204 			if (!test_cpu_flag(CIF_FPU))
205 				kill_task = 1;
206 		}
207 		cr0.val = S390_lowcore.cregs_save_area[0];
208 		cr0.afp = cr0.vx = 1;
209 		__ctl_load(cr0.val, 0, 0);
210 		asm volatile(
211 			"	la	1,%0\n"
212 			"	.word	0xe70f,0x1000,0x0036\n"	/* vlm 0,15,0(1) */
213 			"	.word	0xe70f,0x1100,0x0c36\n"	/* vlm 16,31,256(1) */
214 			: : "Q" (*(struct vx_array *) mcesa->vector_save_area)
215 			: "1");
216 		__ctl_load(S390_lowcore.cregs_save_area[0], 0, 0);
217 	}
218 	/* Validate access registers */
219 	asm volatile(
220 		"	lam	0,15,0(%0)"
221 		: : "a" (&S390_lowcore.access_regs_save_area));
222 	if (!mci.ar) {
223 		/*
224 		 * Access registers have unknown contents.
225 		 * Terminating task.
226 		 */
227 		kill_task = 1;
228 	}
229 	/* Validate guarded storage registers */
230 	if (MACHINE_HAS_GS && (S390_lowcore.cregs_save_area[2] & (1UL << 4))) {
231 		if (!mci.gs)
232 			/*
233 			 * Guarded storage register can't be restored and
234 			 * the current processes uses guarded storage.
235 			 * It has to be terminated.
236 			 */
237 			kill_task = 1;
238 		else
239 			load_gs_cb((struct gs_cb *)
240 				   mcesa->guarded_storage_save_area);
241 	}
242 	/*
243 	 * We don't even try to validate the TOD register, since we simply
244 	 * can't write something sensible into that register.
245 	 */
246 	/*
247 	 * See if we can validate the TOD programmable register with its
248 	 * old contents (should be zero) otherwise set it to zero.
249 	 */
250 	if (!mci.pr)
251 		asm volatile(
252 			"	sr	0,0\n"
253 			"	sckpf"
254 			: : : "0", "cc");
255 	else
256 		asm volatile(
257 			"	l	0,%0\n"
258 			"	sckpf"
259 			: : "Q" (S390_lowcore.tod_progreg_save_area)
260 			: "0", "cc");
261 	/* Validate clock comparator register */
262 	set_clock_comparator(S390_lowcore.clock_comparator);
263 	/* Check if old PSW is valid */
264 	if (!mci.wp)
265 		/*
266 		 * Can't tell if we come from user or kernel mode
267 		 * -> stopping machine.
268 		 */
269 		s390_handle_damage();
270 
271 	if (!mci.ms || !mci.pm || !mci.ia)
272 		kill_task = 1;
273 
274 	return kill_task;
275 }
276 
277 #define MAX_IPD_COUNT	29
278 #define MAX_IPD_TIME	(5 * 60 * USEC_PER_SEC) /* 5 minutes */
279 
280 #define ED_STP_ISLAND	6	/* External damage STP island check */
281 #define ED_STP_SYNC	7	/* External damage STP sync check */
282 
283 /*
284  * machine check handler.
285  */
286 void notrace s390_do_machine_check(struct pt_regs *regs)
287 {
288 	static int ipd_count;
289 	static DEFINE_SPINLOCK(ipd_lock);
290 	static unsigned long long last_ipd;
291 	struct mcck_struct *mcck;
292 	unsigned long long tmp;
293 	union mci mci;
294 
295 	nmi_enter();
296 	inc_irq_stat(NMI_NMI);
297 	mci.val = S390_lowcore.mcck_interruption_code;
298 	mcck = this_cpu_ptr(&cpu_mcck);
299 
300 	if (mci.sd) {
301 		/* System damage -> stopping machine */
302 		s390_handle_damage();
303 	}
304 	if (mci.pd) {
305 		if (mci.b) {
306 			/* Processing backup -> verify if we can survive this */
307 			u64 z_mcic, o_mcic, t_mcic;
308 			z_mcic = (1ULL<<63 | 1ULL<<59 | 1ULL<<29);
309 			o_mcic = (1ULL<<43 | 1ULL<<42 | 1ULL<<41 | 1ULL<<40 |
310 				  1ULL<<36 | 1ULL<<35 | 1ULL<<34 | 1ULL<<32 |
311 				  1ULL<<30 | 1ULL<<21 | 1ULL<<20 | 1ULL<<17 |
312 				  1ULL<<16);
313 			t_mcic = mci.val;
314 
315 			if (((t_mcic & z_mcic) != 0) ||
316 			    ((t_mcic & o_mcic) != o_mcic)) {
317 				s390_handle_damage();
318 			}
319 
320 			/*
321 			 * Nullifying exigent condition, therefore we might
322 			 * retry this instruction.
323 			 */
324 			spin_lock(&ipd_lock);
325 			tmp = get_tod_clock();
326 			if (((tmp - last_ipd) >> 12) < MAX_IPD_TIME)
327 				ipd_count++;
328 			else
329 				ipd_count = 1;
330 			last_ipd = tmp;
331 			if (ipd_count == MAX_IPD_COUNT)
332 				s390_handle_damage();
333 			spin_unlock(&ipd_lock);
334 		} else {
335 			/* Processing damage -> stopping machine */
336 			s390_handle_damage();
337 		}
338 	}
339 	if (s390_validate_registers(mci, user_mode(regs))) {
340 		/*
341 		 * Couldn't restore all register contents for the
342 		 * user space process -> mark task for termination.
343 		 */
344 		mcck->kill_task = 1;
345 		mcck->mcck_code = mci.val;
346 		set_cpu_flag(CIF_MCCK_PENDING);
347 	}
348 	if (mci.cd) {
349 		/* Timing facility damage */
350 		s390_handle_damage();
351 	}
352 	if (mci.ed && mci.ec) {
353 		/* External damage */
354 		if (S390_lowcore.external_damage_code & (1U << ED_STP_SYNC))
355 			mcck->stp_queue |= stp_sync_check();
356 		if (S390_lowcore.external_damage_code & (1U << ED_STP_ISLAND))
357 			mcck->stp_queue |= stp_island_check();
358 		if (mcck->stp_queue)
359 			set_cpu_flag(CIF_MCCK_PENDING);
360 	}
361 	if (mci.se)
362 		/* Storage error uncorrected */
363 		s390_handle_damage();
364 	if (mci.ke)
365 		/* Storage key-error uncorrected */
366 		s390_handle_damage();
367 	if (mci.ds && mci.fa)
368 		/* Storage degradation */
369 		s390_handle_damage();
370 	if (mci.cp) {
371 		/* Channel report word pending */
372 		mcck->channel_report = 1;
373 		set_cpu_flag(CIF_MCCK_PENDING);
374 	}
375 	if (mci.w) {
376 		/* Warning pending */
377 		mcck->warning = 1;
378 		set_cpu_flag(CIF_MCCK_PENDING);
379 	}
380 	nmi_exit();
381 }
382 
383 static int __init machine_check_init(void)
384 {
385 	ctl_set_bit(14, 25);	/* enable external damage MCH */
386 	ctl_set_bit(14, 27);	/* enable system recovery MCH */
387 	ctl_set_bit(14, 24);	/* enable warning MCH */
388 	return 0;
389 }
390 early_initcall(machine_check_init);
391