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