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