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