1 /*
2  *  linux/arch/ppc64/kernel/traps.c
3  *
4  *  Copyright (C) 1995-1996  Gary Thomas (gdt@linuxppc.org)
5  *
6  *  This program is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU General Public License
8  *  as published by the Free Software Foundation; either version
9  *  2 of the License, or (at your option) any later version.
10  *
11  *  Modified by Cort Dougan (cort@cs.nmt.edu)
12  *  and Paul Mackerras (paulus@cs.anu.edu.au)
13  */
14 
15 /*
16  * This file handles the architecture-dependent parts of hardware exceptions
17  */
18 
19 #include <linux/errno.h>
20 #include <linux/sched.h>
21 #include <linux/kernel.h>
22 #include <linux/mm.h>
23 #include <linux/stddef.h>
24 #include <linux/unistd.h>
25 #include <linux/ptrace.h>
26 #include <linux/slab.h>
27 #include <linux/user.h>
28 #include <linux/a.out.h>
29 #include <linux/interrupt.h>
30 #include <linux/config.h>
31 #include <linux/init.h>
32 #include <asm/iSeries/HvCall.h>
33 #include <asm/iSeries/HvCallCfg.h>
34 
35 #ifdef CONFIG_KDB
36 #include <linux/kdb.h>
37 #endif
38 
39 #include <asm/pgtable.h>
40 #include <asm/uaccess.h>
41 #include <asm/system.h>
42 #include <asm/io.h>
43 #include <asm/processor.h>
44 #include <asm/ppcdebug.h>
45 #include <asm/machdep.h> /* for ppc_attention_msg */
46 
47 extern int fix_alignment(struct pt_regs *);
48 extern void bad_page_fault(struct pt_regs *, unsigned long);
49 
50 /* This is true if we are using the firmware NMI handler (typically LPAR) */
51 extern int fwnmi_active;
52 /* This is true if we are using a check-exception based handler */
53 extern int check_exception_flag;
54 
55 #ifdef CONFIG_XMON
56 extern void xmon(struct pt_regs *regs);
57 extern int xmon_bpt(struct pt_regs *regs);
58 extern int xmon_sstep(struct pt_regs *regs);
59 extern int xmon_iabr_match(struct pt_regs *regs);
60 extern int xmon_dabr_match(struct pt_regs *regs);
61 extern void (*xmon_fault_handler)(struct pt_regs *regs);
62 #endif
63 
64 #ifdef CONFIG_XMON
65 void (*debugger)(struct pt_regs *regs) = xmon;
66 int (*debugger_bpt)(struct pt_regs *regs) = xmon_bpt;
67 int (*debugger_sstep)(struct pt_regs *regs) = xmon_sstep;
68 int (*debugger_iabr_match)(struct pt_regs *regs) = xmon_iabr_match;
69 int (*debugger_dabr_match)(struct pt_regs *regs) = xmon_dabr_match;
70 void (*debugger_fault_handler)(struct pt_regs *regs);
71 #else
72 #ifdef CONFIG_KGDB
73 void (*debugger)(struct pt_regs *regs);
74 int (*debugger_bpt)(struct pt_regs *regs);
75 int (*debugger_sstep)(struct pt_regs *regs);
76 int (*debugger_iabr_match)(struct pt_regs *regs);
77 int (*debugger_dabr_match)(struct pt_regs *regs);
78 void (*debugger_fault_handler)(struct pt_regs *regs);
79 #else
80 #ifdef CONFIG_KDB
81 void (*debugger)(struct pt_regs *regs);
82 int (*debugger_bpt)(struct pt_regs *regs);
83 int (*debugger_sstep)(struct pt_regs *regs);
84 int (*debugger_iabr_match)(struct pt_regs *regs);
85 int (*debugger_dabr_match)(struct pt_regs *regs);
86 void (*debugger_fault_handler)(struct pt_regs *regs);
87 #endif /* kdb */
88 #endif /* kgdb */
89 #endif /* xmon */
90 
91 void set_local_DABR(void *valp);
92 
93 /* do not want to kmalloc or wait on lock during machine check */
94 char mce_data_buf[RTAS_ERROR_LOG_MAX]__page_aligned;
95 
96 /*
97  * Trap & Exception support
98  */
99 
100 static void
_exception(int signr,siginfo_t * info,struct pt_regs * regs)101 _exception(int signr, siginfo_t *info, struct pt_regs *regs)
102 {
103 	if (!user_mode(regs))
104 	{
105 		show_regs(regs);
106 #if defined(CONFIG_XMON) || defined(CONFIG_KGDB) || defined(CONFIG_KDB)
107 		debugger(regs);
108 #endif
109 		print_backtrace((unsigned long *)regs->gpr[1]);
110 		panic("Exception in kernel pc %lx signal %d",regs->nip,signr);
111 #if defined(CONFIG_PPCDBG) && (defined(CONFIG_XMON) || defined(CONFIG_KGDB))
112 	/* Allow us to catch SIGILLs for 64-bit app/glibc debugging. -Peter */
113 	} else if (signr == SIGILL) {
114 		ifppcdebug(PPCDBG_SIGNALXMON)
115 			debugger(regs);
116 #endif
117 	}
118 	force_sig_info(signr, info, current);
119 }
120 
121 /* Get the error information for errors coming through the
122  * FWNMI vectors.  The pt_regs' r3 will be updated to reflect
123  * the actual r3 if possible, and a ptr to the error log entry
124  * will be returned if found.
125  */
FWNMI_get_errinfo(struct pt_regs * regs)126 static struct rtas_error_log *FWNMI_get_errinfo(struct pt_regs *regs)
127 {
128 	unsigned long errdata = regs->gpr[3];
129 	struct rtas_error_log *errhdr = NULL;
130 	unsigned long *savep;
131 
132 	if ((errdata >= 0x7000 && errdata < 0x7fff0) ||
133 	    (errdata >= rtas.base && errdata < rtas.base + rtas.size - 16)) {
134 		savep = __va(errdata);
135 		regs->gpr[3] = savep[0];	/* restore original r3 */
136 		memset(mce_data_buf, 0, RTAS_ERROR_LOG_MAX);
137 		memcpy(mce_data_buf, (char *)(savep + 1), RTAS_ERROR_LOG_MAX);
138 		errhdr = (struct rtas_error_log *)mce_data_buf;
139 	} else {
140 		printk("FWNMI: corrupt r3\n");
141 	}
142 	return errhdr;
143 }
144 
145 /* Call this when done with the data returned by FWNMI_get_errinfo.
146  * It will release the saved data area for other CPUs in the
147  * partition to receive FWNMI errors.
148  */
FWNMI_release_errinfo(void)149 static void FWNMI_release_errinfo(void)
150 {
151 	unsigned long ret = rtas_call(rtas_token("ibm,nmi-interlock"), 0, 1, NULL);
152 	if (ret != 0)
153 		printk("FWNMI: nmi-interlock failed: %ld\n", ret);
154 }
155 
156 void
SystemResetException(struct pt_regs * regs)157 SystemResetException(struct pt_regs *regs)
158 {
159 	char *msg = "System Reset in kernel mode.\n";
160 	printk(msg);
161 	if (fwnmi_active) {
162 		unsigned long *r3 = __va(regs->gpr[3]); /* for FWNMI debug */
163 		printk("FWNMI is active with save area at %p\n", r3);
164 		FWNMI_release_errinfo();
165 	}
166 #if defined(CONFIG_XMON)
167 	xmon(regs);
168 	if (smp_processor_id() == 0)
169 		udbg_printf("leaving xmon...\n");
170 #endif
171 #if defined(CONFIG_KDB)
172 	kdb_reset_debugger(regs);
173 #endif
174 }
175 
176 /*
177  * See if we can recover from a machine check exception.
178  * This is only called on power4 (or above) and only via
179  * the Firmware Non-Maskable Interrupts (fwnmi) handler
180  * which provides the error analysis for us.
181  *
182  * Return 1 if corrected (or delivered a signal).
183  * Return 0 if there is nothing we can do.
184  */
recover_mce(struct pt_regs * regs,struct rtas_error_log * errp)185 static int recover_mce(struct pt_regs *regs, struct rtas_error_log *errp)
186 {
187 	siginfo_t info;
188 	int nonfatal = 0;
189 
190 
191 	if (errp->disposition == DISP_FULLY_RECOVERED) {
192 		/* Platform corrected itself */
193 		nonfatal = 1;
194 	} else if ((regs->msr & MSR_RI) &&
195 		   user_mode(regs) &&
196 		   errp->severity == SEVERITY_ERROR_SYNC &&
197 		   errp->disposition == DISP_NOT_RECOVERED &&
198 		   errp->target == TARGET_MEMORY &&
199 		   errp->type == TYPE_ECC_UNCORR &&
200 		   !(current->pid == 0 || current->pid == 1)) {
201 
202 		/* Kill off a user process with an ECC error */
203 		printk(KERN_ERR "MCE: uncorrectable ecc error killed process %d (%s).\n", current->pid, current->comm);
204 
205 		info.si_signo = SIGBUS;
206 		info.si_errno = 0;
207 		/* XXX better si_code for ECC error? */
208 		info.si_code = BUS_ADRERR;
209 		info.si_addr = (void *)regs->nip;
210 		_exception(SIGBUS, &info, regs);
211 		nonfatal = 1;
212 	}
213 
214 	log_error((char *)errp, ERR_TYPE_RTAS_LOG, !nonfatal);
215 
216 	return nonfatal;
217 }
218 
219 /*
220  * Handle a machine check.
221  *
222  * Note that on Power 4 and beyond Firmware Non-Maskable Interrupts (fwnmi)
223  * should be present.  If so the handler which called us tells us if the
224  * error was recovered (never true if RI=0).
225  *
226  * On hardware prior to Power 4 these exceptions were asynchronous which
227  * means we can't tell exactly where it occurred and so we can't recover.
228  *
229  * Note that the debugger should test RI=0 and warn the user that system
230  * state has been corrupted.
231  */
232 void
MachineCheckException(struct pt_regs * regs)233 MachineCheckException(struct pt_regs *regs)
234 {
235 	struct rtas_error_log *errp;
236 
237 	if (fwnmi_active) {
238 		errp = FWNMI_get_errinfo(regs);
239 		FWNMI_release_errinfo();
240 		if (errp && recover_mce(regs, errp))
241 			return;
242 	} else if (check_exception_flag) {
243 		int status;
244 		unsigned long long srr1 = regs->msr;
245 
246 		memset(mce_data_buf, 0, RTAS_ERROR_LOG_MAX);
247 		/* XXX
248 		 * We only pass the low 32 bits of SRR1, this could
249 		 * be changed to 7 input params and the high 32 bits
250 		 * of SRR1 could be passed as the extended info argument.
251 		 */
252 		status = rtas_call(rtas_token("check-exception"), 6, 1, NULL,
253 				   0x200, (uint)srr1, RTAS_INTERNAL_ERROR, 0,
254 				   __pa(mce_data_buf), RTAS_ERROR_LOG_MAX);
255 		if (status == 0)
256 			log_error((char *)mce_data_buf, ERR_TYPE_RTAS_LOG, 1);
257 	}
258 
259 #if defined(CONFIG_XMON) || defined(CONFIG_KGDB)
260 	if (debugger_fault_handler) {
261 		debugger_fault_handler(regs);
262 		return;
263 	}
264 #endif
265 	printk(KERN_EMERG "Unrecoverable Machine check.\n");
266 	printk(KERN_EMERG "Caused by (from SRR1=%lx): ", regs->msr);
267 	show_regs(regs);
268 #if defined(CONFIG_XMON) || defined(CONFIG_KGDB) || defined(CONFIG_KDB)
269 	debugger(regs);
270 #endif
271 	print_backtrace((unsigned long *)regs->gpr[1]);
272 	panic("machine check");
273 }
274 
275 void
SMIException(struct pt_regs * regs)276 SMIException(struct pt_regs *regs)
277 {
278 #if defined(CONFIG_XMON) || defined(CONFIG_KGDB) || defined(CONFIG_KDB)
279 	{
280 		debugger(regs);
281 		return;
282 	}
283 #endif
284 	show_regs(regs);
285 	print_backtrace((unsigned long *)regs->gpr[1]);
286 	panic("System Management Interrupt");
287 }
288 
289 void
UnknownException(struct pt_regs * regs)290 UnknownException(struct pt_regs *regs)
291 {
292 	siginfo_t info;
293 
294 	printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
295 	       regs->nip, regs->msr, regs->trap);
296 
297 	info.si_signo = SIGTRAP;
298 	info.si_errno = 0;
299 	info.si_code = 0;
300 	info.si_addr = 0;
301 	_exception(SIGTRAP, &info, regs);
302 }
303 
304 void
InstructionBreakpointException(struct pt_regs * regs)305 InstructionBreakpointException(struct pt_regs *regs)
306 {
307 	siginfo_t info;
308 
309 #if defined(CONFIG_XMON) || defined(CONFIG_KGDB) || defined (CONFIG_KDB)
310 	if (debugger_iabr_match(regs))
311 		return;
312 #endif
313 	info.si_signo = SIGTRAP;
314 	info.si_errno = 0;
315 	info.si_code = TRAP_BRKPT;
316 	info.si_addr = (void *)regs->nip;
317 	_exception(SIGTRAP, &info, regs);
318 }
319 
320 static void
parse_fpe(siginfo_t * info,struct pt_regs * regs)321 parse_fpe(siginfo_t *info, struct pt_regs *regs)
322 {
323 	unsigned long fpscr;
324 
325 	if (regs->msr & MSR_FP)
326 		giveup_fpu(current);
327 
328 	fpscr = current->thread.fpscr;
329 
330 	/* Invalid operation */
331 	if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
332 		info->si_code = FPE_FLTINV;
333 
334 	/* Overflow */
335 	else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
336 		info->si_code = FPE_FLTOVF;
337 
338 	/* Underflow */
339 	else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
340 		info->si_code = FPE_FLTUND;
341 
342 	/* Divide by zero */
343 	else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
344 		info->si_code = FPE_FLTDIV;
345 
346 	/* Inexact result */
347 	else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
348 		info->si_code = FPE_FLTRES;
349 
350 	else
351 		info->si_code = 0;
352 
353 	info->si_signo = SIGFPE;
354 	info->si_errno = 0;
355 	info->si_addr = (void *)regs->nip;
356 	_exception(SIGFPE, info, regs);
357 }
358 
359 #ifndef CONFIG_ALTIVEC
IllegalAltiVecInstruction(struct pt_regs * regs)360 void IllegalAltiVecInstruction(struct pt_regs *regs)
361 {
362 	siginfo_t info;
363 
364 	info.si_signo = SIGILL;
365 	info.si_errno = 0;
366 	info.si_code = ILL_ILLTRP;
367 	info.si_addr = (void *)regs->nip;
368 	_exception(SIGILL, &info, regs);
369 }
370 #endif
371 
372 void
ProgramCheckException(struct pt_regs * regs)373 ProgramCheckException(struct pt_regs *regs)
374 {
375 	siginfo_t info;
376 
377 	if (regs->msr & 0x100000) {
378 		/* IEEE FP exception */
379 
380 		parse_fpe(&info, regs);
381 	} else if (regs->msr & 0x40000) {
382 		/* Privileged instruction */
383 
384 		info.si_signo = SIGILL;
385 		info.si_errno = 0;
386 		info.si_code = ILL_PRVOPC;
387 		info.si_addr = (void *)regs->nip;
388 		_exception(SIGILL, &info, regs);
389 	} else if (regs->msr & 0x20000) {
390 		/* trap exception */
391 
392 #if defined(CONFIG_XMON) || defined(CONFIG_KGDB) || defined(CONFIG_KDB)
393 		if (debugger_bpt(regs))
394 			return;
395 #endif
396 		info.si_signo = SIGTRAP;
397 		info.si_errno = 0;
398 		info.si_code = TRAP_BRKPT;
399 		info.si_addr = (void *)regs->nip;
400 		_exception(SIGTRAP, &info, regs);
401 	} else {
402 		/* Illegal instruction */
403 
404 		info.si_signo = SIGILL;
405 		info.si_errno = 0;
406 		info.si_code = ILL_ILLTRP;
407 		info.si_addr = (void *)regs->nip;
408 		_exception(SIGILL, &info, regs);
409 	}
410 }
411 
412  void
KernelFPUnavailableException(struct pt_regs * regs)413 KernelFPUnavailableException(struct pt_regs *regs)
414 {
415 	printk("Illegal floating point used in kernel (task=0x%016lx, pc=0x%016lx, trap=0x%08x)\n",
416 		current, regs->nip, regs->trap);
417 	panic("Unrecoverable FP Unavailable Exception in Kernel");
418 }
419 
420 
421 void
KernelAltiVecUnavailableException(struct pt_regs * regs)422 KernelAltiVecUnavailableException(struct pt_regs *regs)
423 {
424 	printk("Illegal Altivec used in kernel (task=0x%016lx, pc=0x%016lx, trap=0x%08x)\n",
425 		(unsigned long)current, regs->nip, (unsigned int)regs->trap);
426 	panic("Unrecoverable Altivec Unavailable Exception in Kernel");
427 }
428 
429 void
AltiVecAssistException(struct pt_regs * regs)430 AltiVecAssistException(struct pt_regs *regs)
431 {
432 #ifdef CONFIG_ALTIVEC
433 	printk("Altivec assist called by %s, switching java mode off\n",
434 		current->comm);
435 	/* We do this the "hard" way, but that's ok for now, maybe one
436 	 * day, we'll have a proper implementation...
437 	 */
438 	if (regs->msr & MSR_VEC)
439 		giveup_altivec(current);
440 	current->thread.vscr.u[3] |= 0x00010000;
441 #else
442 	siginfo_t info;
443 
444 	printk("Altivec assist called by %s;, no altivec support !\n",
445 		current->comm);
446 
447 	info.si_signo = SIGTRAP;
448 	info.si_errno = 0;
449 	info.si_code = 0;
450 	info.si_addr = 0;
451 	_exception(SIGTRAP, &info, regs);
452 #endif /* CONFIG_ALTIVEC */
453 }
454 
455 void
ThermalInterrupt(struct pt_regs * regs)456 ThermalInterrupt(struct pt_regs *regs)
457 {
458 	panic("Thermal interrupt exception not handled !");
459 }
460 
461 void
SingleStepException(struct pt_regs * regs)462 SingleStepException(struct pt_regs *regs)
463 {
464 	siginfo_t info;
465 
466 	regs->msr &= ~MSR_SE;  /* Turn off 'trace' bit */
467 
468 #if defined(CONFIG_XMON) || defined(CONFIG_KGDB) || defined(CONFIG_KDB)
469 	if (debugger_sstep(regs))
470 		return;
471 #endif
472 	info.si_signo = SIGTRAP;
473 	info.si_errno = 0;
474 	info.si_code = TRAP_TRACE;
475 	info.si_addr = (void *)regs->nip;
476 	_exception(SIGTRAP, &info, regs);
477 }
478 
479 void
AlignmentException(struct pt_regs * regs)480 AlignmentException(struct pt_regs *regs)
481 {
482 	int fixed;
483 	siginfo_t info;
484 
485 	fixed = fix_alignment(regs);
486 	if (fixed == 1) {
487 		ifppcdebug(PPCDBG_ALIGNFIXUP)
488 			if (!user_mode(regs))
489 				PPCDBG(PPCDBG_ALIGNFIXUP, "fix alignment at %lx\n", regs->nip);
490 		regs->nip += 4;	/* skip over emulated instruction */
491 		return;
492 	}
493 
494 	/* Operand address was bad */
495 	if (fixed == -EFAULT) {
496 		if (user_mode(regs)) {
497 			info.si_signo = SIGSEGV;
498 			info.si_errno = 0;
499 			info.si_code = SEGV_MAPERR;
500 			info.si_addr = (void *)regs->dar;
501 			force_sig_info(SIGSEGV, &info, current);
502 		} else {
503 			/* Search exception table */
504 			bad_page_fault(regs, regs->dar);
505 		}
506 
507 		return;
508 	}
509 
510 	info.si_signo = SIGBUS;
511 	info.si_errno = 0;
512 	info.si_code = BUS_ADRALN;
513 	info.si_addr = (void *)regs->nip;
514 	_exception(SIGBUS, &info, regs);
515 }
516 
trap_init(void)517 void __init trap_init(void)
518 {
519 }
520 
521 /*
522  * Set the DABR on all processors in the system.  The value is defined as:
523  * DAB(0:60), Break Translate(61), Write(62), Read(63)
524  */
525 void
set_all_DABR(unsigned long val)526 set_all_DABR(unsigned long val) {
527 	set_local_DABR(&val);
528 	smp_call_function(set_local_DABR, &val, 0, 0);
529 }
530 
531 void
set_local_DABR(void * valp)532 set_local_DABR(void *valp) {
533 	unsigned long val = *((unsigned long *)valp);
534 
535 	HvCall_setDABR(val);
536 }
537