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