traps.c 11.6 KB
Newer Older
1
/*
2
 *  linux/arch/ppc64/kernel/traps.c
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 *  Copyright (C) 1995-1996  Gary Thomas (gdt@linuxppc.org)
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version
 *  2 of the License, or (at your option) any later version.
 *
 *  Modified by Cort Dougan (cort@cs.nmt.edu)
 *  and Paul Mackerras (paulus@cs.anu.edu.au)
 */

/*
 * This file handles the architecture-dependent parts of hardware exceptions
 */

19
#include <linux/config.h>
20 21 22 23 24 25 26 27 28 29 30
#include <linux/errno.h>
#include <linux/sched.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/stddef.h>
#include <linux/unistd.h>
#include <linux/slab.h>
#include <linux/user.h>
#include <linux/a.out.h>
#include <linux/interrupt.h>
#include <linux/init.h>
31
#include <linux/module.h>
32 33 34 35 36 37 38 39 40

#include <asm/pgtable.h>
#include <asm/uaccess.h>
#include <asm/system.h>
#include <asm/io.h>
#include <asm/processor.h>
#include <asm/ppcdebug.h>

extern int fix_alignment(struct pt_regs *);
41
extern void bad_page_fault(struct pt_regs *, unsigned long, int);
42

43
#ifdef CONFIG_PPC_PSERIES
44 45
/* This is true if we are using the firmware NMI handler (typically LPAR) */
extern int fwnmi_active;
46
#endif
47

48 49 50 51 52 53 54 55 56 57 58 59 60 61
#ifdef CONFIG_DEBUGGER
int (*__debugger)(struct pt_regs *regs);
int (*__debugger_bpt)(struct pt_regs *regs);
int (*__debugger_sstep)(struct pt_regs *regs);
int (*__debugger_iabr_match)(struct pt_regs *regs);
int (*__debugger_dabr_match)(struct pt_regs *regs);
int (*__debugger_fault_handler)(struct pt_regs *regs);

EXPORT_SYMBOL(__debugger);
EXPORT_SYMBOL(__debugger_bpt);
EXPORT_SYMBOL(__debugger_sstep);
EXPORT_SYMBOL(__debugger_iabr_match);
EXPORT_SYMBOL(__debugger_dabr_match);
EXPORT_SYMBOL(__debugger_fault_handler);
62
#endif
63

64 65 66 67
/*
 * Trap & Exception support
 */

68 69 70
static spinlock_t die_lock = SPIN_LOCK_UNLOCKED;

void die(const char *str, struct pt_regs *regs, long err)
71
{
72
	static int die_counter;
73

74 75 76
	console_verbose();
	spin_lock_irq(&die_lock);
	bust_spinlocks(1);
77
	printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
78 79 80 81
	show_regs(regs);
	bust_spinlocks(0);
	spin_unlock_irq(&die_lock);

82 83 84 85 86 87 88 89 90
	if (in_interrupt())
		panic("Fatal exception in interrupt");

	if (panic_on_oops) {
		printk(KERN_EMERG "Fatal exception: panic in 5 seconds\n");
		set_current_state(TASK_UNINTERRUPTIBLE);
		schedule_timeout(5 * HZ);
		panic("Fatal exception");
	}
91 92 93 94 95 96 97
	do_exit(SIGSEGV);
}

static void
_exception(int signr, siginfo_t *info, struct pt_regs *regs)
{
	if (!user_mode(regs)) {
98 99 100
		if (debugger(regs))
			return;
		die("Exception in kernel mode\n", regs, signr);
101
	}
102 103

	force_sig_info(signr, info, current);
104 105
}

106
#ifdef CONFIG_PPC_PSERIES
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
/* Get the error information for errors coming through the
 * FWNMI vectors.  The pt_regs' r3 will be updated to reflect
 * the actual r3 if possible, and a ptr to the error log entry
 * will be returned if found.
 */
static struct rtas_error_log *FWNMI_get_errinfo(struct pt_regs *regs)
{
	unsigned long errdata = regs->gpr[3];
	struct rtas_error_log *errhdr = NULL;
	unsigned long *savep;

	if ((errdata >= 0x7000 && errdata < 0x7fff0) ||
	    (errdata >= rtas.base && errdata < rtas.base + rtas.size - 16)) {
		savep = __va(errdata);
		regs->gpr[3] = savep[0];	/* restore original r3 */
		errhdr = (struct rtas_error_log *)(savep + 1);
	} else {
		printk("FWNMI: corrupt r3\n");
	}
	return errhdr;
}

/* Call this when done with the data returned by FWNMI_get_errinfo.
 * It will release the saved data area for other CPUs in the
 * partition to receive FWNMI errors.
 */
static void FWNMI_release_errinfo(void)
{
	unsigned long ret = rtas_call(rtas_token("ibm,nmi-interlock"), 0, 1, NULL);
	if (ret != 0)
		printk("FWNMI: nmi-interlock failed: %ld\n", ret);
}
139
#endif
140

141 142 143
void
SystemResetException(struct pt_regs *regs)
{
144
#ifdef CONFIG_PPC_PSERIES
145
	if (fwnmi_active) {
146 147 148 149
		struct rtas_error_log *errhdr = FWNMI_get_errinfo(regs);
		if (errhdr) {
			/* XXX Should look at FWNMI information */
		}
150
		FWNMI_release_errinfo();
151
	}
152
#endif
153

154 155
	if (!debugger(regs))
		die("System Reset", regs, 0);
156 157 158 159 160 161 162 163

	/* Must die if the interrupt is not recoverable */
	if (!(regs->msr & MSR_RI))
		panic("Unrecoverable System Reset");

	/* What should we do here? We could issue a shutdown or hard reset. */
}

164
#ifdef CONFIG_PPC_PSERIES
165 166 167 168 169 170 171 172 173 174
/* 
 * See if we can recover from a machine check exception.
 * This is only called on power4 (or above) and only via
 * the Firmware Non-Maskable Interrupts (fwnmi) handler
 * which provides the error analysis for us.
 *
 * Return 1 if corrected (or delivered a signal).
 * Return 0 if there is nothing we can do.
 */
static int recover_mce(struct pt_regs *regs, struct rtas_error_log err)
175
{
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
	siginfo_t info;

	if (err.disposition == DISP_FULLY_RECOVERED) {
		/* Platform corrected itself */
		return 1;
	} else if ((regs->msr & MSR_RI) &&
		   user_mode(regs) &&
		   err.severity == SEVERITY_ERROR_SYNC &&
		   err.disposition == DISP_NOT_RECOVERED &&
		   err.target == TARGET_MEMORY &&
		   err.type == TYPE_ECC_UNCORR &&
		   !(current->pid == 0 || current->pid == 1)) {
		/* Kill off a user process with an ECC error */
		info.si_signo = SIGBUS;
		info.si_errno = 0;
		/* XXX something better for ECC error? */
		info.si_code = BUS_ADRERR;
		info.si_addr = (void *)regs->nip;
		printk(KERN_ERR "MCE: uncorrectable ecc error for pid %d\n",
		       current->pid);
		_exception(SIGBUS, &info, regs);
		return 1;
	}
199
	return 0;
200
}
201
#endif
202

203 204 205 206 207 208 209 210 211 212 213 214 215
/*
 * Handle a machine check.
 *
 * Note that on Power 4 and beyond Firmware Non-Maskable Interrupts (fwnmi)
 * should be present.  If so the handler which called us tells us if the
 * error was recovered (never true if RI=0).
 *
 * On hardware prior to Power 4 these exceptions were asynchronous which
 * means we can't tell exactly where it occurred and so we can't recover.
 *
 * Note that the debugger should test RI=0 and warn the user that system
 * state has been corrupted.
 */
216 217 218
void
MachineCheckException(struct pt_regs *regs)
{
219
#ifdef CONFIG_PPC_PSERIES
220
	struct rtas_error_log err, *errp;
221

222
	if (fwnmi_active) {
223 224 225 226 227 228
		errp = FWNMI_get_errinfo(regs);
		if (errp)
			err = *errp;
		FWNMI_release_errinfo();	/* frees errp */
		if (errp && recover_mce(regs, err))
			return;
229
	}
230
#endif
231

232
	if (debugger_fault_handler(regs))
233
		return;
234 235 236 237
	if (debugger(regs))
		return;

	die("Machine check in kernel mode", regs, 0);
238 239 240 241 242
}

void
UnknownException(struct pt_regs *regs)
{
243 244
	siginfo_t info;

245 246
	printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
	       regs->nip, regs->msr, regs->trap);
247 248 249 250 251 252

	info.si_signo = SIGTRAP;
	info.si_errno = 0;
	info.si_code = 0;
	info.si_addr = 0;
	_exception(SIGTRAP, &info, regs);	
253 254 255 256 257
}

void
InstructionBreakpointException(struct pt_regs *regs)
{
258 259
	siginfo_t info;

260
	if (debugger_iabr_match(regs))
261
		return;
262 263 264 265 266 267 268 269 270 271
	info.si_signo = SIGTRAP;
	info.si_errno = 0;
	info.si_code = TRAP_BRKPT;
	info.si_addr = (void *)regs->nip;
	_exception(SIGTRAP, &info, regs);
}

static void parse_fpe(struct pt_regs *regs)
{
	siginfo_t info;
272
	unsigned long fpscr;
273 274 275 276

	if (regs->msr & MSR_FP)
		giveup_fpu(current);

277
	fpscr = current->thread.fpscr;
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305

	/* Invalid operation */
	if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
		info.si_code = FPE_FLTINV;

	/* Overflow */
	else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
		info.si_code = FPE_FLTOVF;

	/* Underflow */
	else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
		info.si_code = FPE_FLTUND;

	/* Divide by zero */
	else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
		info.si_code = FPE_FLTDIV;

	/* Inexact result */
	else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
		info.si_code = FPE_FLTRES;

	else
		info.si_code = 0;

	info.si_signo = SIGFPE;
	info.si_errno = 0;
	info.si_addr = (void *)regs->nip;
	_exception(SIGFPE, &info, regs);
306 307
}

308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
/*
 * Look through the list of trap instructions that are used for BUG(),
 * BUG_ON() and WARN_ON() and see if we hit one.  At this point we know
 * that the exception was caused by a trap instruction of some kind.
 * Returns 1 if we should continue (i.e. it was a WARN_ON) or 0
 * otherwise.
 */
extern struct bug_entry __start___bug_table[], __stop___bug_table[];

#ifndef CONFIG_MODULES
#define module_find_bug(x)	NULL
#endif

static struct bug_entry *find_bug(unsigned long bugaddr)
{
	struct bug_entry *bug;

	for (bug = __start___bug_table; bug < __stop___bug_table; ++bug)
		if (bugaddr == bug->bug_addr)
			return bug;
	return module_find_bug(bugaddr);
}

int
check_bug_trap(struct pt_regs *regs)
{
	struct bug_entry *bug;
	unsigned long addr;

	if (regs->msr & MSR_PR)
		return 0;	/* not in kernel */
	addr = regs->nip;	/* address of trap instruction */
	if (addr < PAGE_OFFSET)
		return 0;
	bug = find_bug(regs->nip);
	if (bug == NULL)
		return 0;
	if (bug->line & BUG_WARNING_TRAP) {
		/* this is a WARN_ON rather than BUG/BUG_ON */
		printk(KERN_ERR "Badness in %s at %s:%d\n",
		       bug->function, bug->file,
Anton Blanchard's avatar
Anton Blanchard committed
349
		      (unsigned int)bug->line & ~BUG_WARNING_TRAP);
350
		show_stack(current, (void *)regs->gpr[1]);
351 352 353
		return 1;
	}
	printk(KERN_CRIT "kernel BUG in %s at %s:%d!\n",
Anton Blanchard's avatar
Anton Blanchard committed
354
	       bug->function, bug->file, (unsigned int)bug->line);
355 356 357
	return 0;
}

358 359 360
void
ProgramCheckException(struct pt_regs *regs)
{
361 362
	siginfo_t info;

363
	if (debugger_fault_handler(regs))
364 365
		return;

366 367
	if (regs->msr & 0x100000) {
		/* IEEE FP exception */
368 369 370 371 372 373 374 375 376 377

		parse_fpe(regs);
	} else if (regs->msr & 0x40000) {
		/* Privileged instruction */

		info.si_signo = SIGILL;
		info.si_errno = 0;
		info.si_code = ILL_PRVOPC;
		info.si_addr = (void *)regs->nip;
		_exception(SIGILL, &info, regs);
378 379
	} else if (regs->msr & 0x20000) {
		/* trap exception */
380

381
		if (debugger_bpt(regs))
382
			return;
383

384 385 386 387
		if (check_bug_trap(regs)) {
			regs->nip += 4;
			return;
		}
388 389 390 391 392
		info.si_signo = SIGTRAP;
		info.si_errno = 0;
		info.si_code = TRAP_BRKPT;
		info.si_addr = (void *)regs->nip;
		_exception(SIGTRAP, &info, regs);
393
	} else {
394 395 396 397 398 399 400
		/* Illegal instruction */

		info.si_signo = SIGILL;
		info.si_errno = 0;
		info.si_code = ILL_ILLTRP;
		info.si_addr = (void *)regs->nip;
		_exception(SIGILL, &info, regs);
401 402 403
	}
}

404
void
405 406
KernelFPUnavailableException(struct pt_regs *regs)
{
407
	die("Unrecoverable FP Unavailable Exception in Kernel", regs, 0);
408 409
}

410 411 412
void
KernelAltivecUnavailableException(struct pt_regs *regs)
{
413
	die("Unrecoverable VMX/Altivec Unavailable Exception in Kernel", regs, 0);
414 415
}

416 417 418
void
SingleStepException(struct pt_regs *regs)
{
419 420
	siginfo_t info;

421
	regs->msr &= ~MSR_SE;  /* Turn off 'trace' bit */
422

423
	if (debugger_sstep(regs))
424
		return;
425

426 427 428 429 430 431
	info.si_signo = SIGTRAP;
	info.si_errno = 0;
	info.si_code = TRAP_TRACE;
	info.si_addr = (void *)regs->nip;
	_exception(SIGTRAP, &info, regs);	
}
432 433 434 435

void
PerformanceMonitorException(struct pt_regs *regs)
{
436 437 438 439 440 441 442
	siginfo_t info;

	info.si_signo = SIGTRAP;
	info.si_errno = 0;
	info.si_code = TRAP_BRKPT;
	info.si_addr = 0;
	_exception(SIGTRAP, &info, regs);
443 444 445 446 447 448
}

void
AlignmentException(struct pt_regs *regs)
{
	int fixed;
449
	siginfo_t info;
450 451

	fixed = fix_alignment(regs);
452

453
	if (fixed == 1) {
454 455 456
		if (!user_mode(regs))
			PPCDBG(PPCDBG_ALIGNFIXUP, "fix alignment at %lx\n",
			       regs->nip);
457 458 459
		regs->nip += 4;	/* skip over emulated instruction */
		return;
	}
460 461

	/* Operand address was bad */	
462
	if (fixed == -EFAULT) {
463 464 465 466 467 468 469 470
		if (user_mode(regs)) {
			info.si_signo = SIGSEGV;
			info.si_errno = 0;
			info.si_code = SEGV_MAPERR;
			info.si_addr = (void *)regs->dar;
			force_sig_info(SIGSEGV, &info, current);
		} else {
			/* Search exception table */
471
			bad_page_fault(regs, regs->dar, SIGSEGV);
472 473
		}

474 475
		return;
	}
476 477 478 479 480 481

	info.si_signo = SIGBUS;
	info.si_errno = 0;
	info.si_code = BUS_ADRALN;
	info.si_addr = (void *)regs->nip;
	_exception(SIGBUS, &info, regs);	
482 483
}

484 485 486 487 488 489 490 491 492 493 494
#ifdef CONFIG_ALTIVEC
void
AltivecAssistException(struct pt_regs *regs)
{
	if (regs->msr & MSR_VEC)
		giveup_altivec(current);
	/* XXX quick hack for now: set the non-Java bit in the VSCR */
	current->thread.vscr.u[3] |= 0x10000;
}
#endif /* CONFIG_ALTIVEC */

495 496 497
void __init trap_init(void)
{
}