traps.c 50.1 KB
Newer Older
1 2
/*
 *  Copyright (C) 1995-1996  Gary Thomas (gdt@linuxppc.org)
3
 *  Copyright 2007-2010 Freescale Semiconductor, Inc.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 *  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@samba.org)
 */

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

#include <linux/errno.h>
#include <linux/sched.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/stddef.h>
#include <linux/unistd.h>
P
Paul Mackerras 已提交
24
#include <linux/ptrace.h>
25 26 27 28
#include <linux/user.h>
#include <linux/interrupt.h>
#include <linux/init.h>
#include <linux/module.h>
P
Paul Mackerras 已提交
29
#include <linux/prctl.h>
30 31
#include <linux/delay.h>
#include <linux/kprobes.h>
32
#include <linux/kexec.h>
33
#include <linux/backlight.h>
34
#include <linux/bug.h>
35
#include <linux/kdebug.h>
36
#include <linux/debugfs.h>
37
#include <linux/ratelimit.h>
38
#include <linux/context_tracking.h>
39

40
#include <asm/emulated_ops.h>
41 42 43
#include <asm/pgtable.h>
#include <asm/uaccess.h>
#include <asm/io.h>
44 45
#include <asm/machdep.h>
#include <asm/rtas.h>
46
#include <asm/pmc.h>
47 48 49 50
#include <asm/reg.h>
#ifdef CONFIG_PMAC_BACKLIGHT
#include <asm/backlight.h>
#endif
51
#ifdef CONFIG_PPC64
52
#include <asm/firmware.h>
53
#include <asm/processor.h>
54
#include <asm/tm.h>
55
#endif
56
#include <asm/kexec.h>
57
#include <asm/ppc-opcode.h>
58
#include <asm/rio.h>
59
#include <asm/fadump.h>
60
#include <asm/switch_to.h>
61
#include <asm/tm.h>
62
#include <asm/debug.h>
63
#include <sysdev/fsl_pci.h>
64

65
#if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
66 67 68 69 70
int (*__debugger)(struct pt_regs *regs) __read_mostly;
int (*__debugger_ipi)(struct pt_regs *regs) __read_mostly;
int (*__debugger_bpt)(struct pt_regs *regs) __read_mostly;
int (*__debugger_sstep)(struct pt_regs *regs) __read_mostly;
int (*__debugger_iabr_match)(struct pt_regs *regs) __read_mostly;
71
int (*__debugger_break_match)(struct pt_regs *regs) __read_mostly;
72
int (*__debugger_fault_handler)(struct pt_regs *regs) __read_mostly;
73 74 75 76 77 78

EXPORT_SYMBOL(__debugger);
EXPORT_SYMBOL(__debugger_ipi);
EXPORT_SYMBOL(__debugger_bpt);
EXPORT_SYMBOL(__debugger_sstep);
EXPORT_SYMBOL(__debugger_iabr_match);
79
EXPORT_SYMBOL(__debugger_break_match);
80 81 82
EXPORT_SYMBOL(__debugger_fault_handler);
#endif

83 84 85 86 87 88 89
/* Transactional Memory trap debug */
#ifdef TM_DEBUG_SW
#define TM_DEBUG(x...) printk(KERN_INFO x)
#else
#define TM_DEBUG(x...) do { } while(0)
#endif

90 91 92 93
/*
 * Trap & Exception support
 */

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
#ifdef CONFIG_PMAC_BACKLIGHT
static void pmac_backlight_unblank(void)
{
	mutex_lock(&pmac_backlight_mutex);
	if (pmac_backlight) {
		struct backlight_properties *props;

		props = &pmac_backlight->props;
		props->brightness = props->max_brightness;
		props->power = FB_BLANK_UNBLANK;
		backlight_update_status(pmac_backlight);
	}
	mutex_unlock(&pmac_backlight_mutex);
}
#else
static inline void pmac_backlight_unblank(void) { }
#endif

A
Anton Blanchard 已提交
112 113 114 115 116 117
static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
static int die_owner = -1;
static unsigned int die_nest_count;
static int die_counter;

static unsigned __kprobes long oops_begin(struct pt_regs *regs)
118
{
A
Anton Blanchard 已提交
119
	int cpu;
120
	unsigned long flags;
121 122 123 124

	if (debugger(regs))
		return 1;

125 126
	oops_enter();

A
Anton Blanchard 已提交
127 128 129 130 131 132 133 134
	/* racy, but better than risking deadlock. */
	raw_local_irq_save(flags);
	cpu = smp_processor_id();
	if (!arch_spin_trylock(&die_lock)) {
		if (cpu == die_owner)
			/* nested oops. should stop eventually */;
		else
			arch_spin_lock(&die_lock);
135
	}
A
Anton Blanchard 已提交
136 137 138 139 140 141 142 143
	die_nest_count++;
	die_owner = cpu;
	console_verbose();
	bust_spinlocks(1);
	if (machine_is(powermac))
		pmac_backlight_unblank();
	return flags;
}
144

A
Anton Blanchard 已提交
145 146 147
static void __kprobes oops_end(unsigned long flags, struct pt_regs *regs,
			       int signr)
{
148
	bust_spinlocks(0);
A
Anton Blanchard 已提交
149
	die_owner = -1;
150
	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
A
Anton Blanchard 已提交
151
	die_nest_count--;
152 153
	oops_exit();
	printk("\n");
A
Anton Blanchard 已提交
154 155 156 157
	if (!die_nest_count)
		/* Nest count reaches zero, release the lock. */
		arch_spin_unlock(&die_lock);
	raw_local_irq_restore(flags);
158

159 160
	crash_fadump(regs, "die oops");

161 162 163 164 165
	/*
	 * A system reset (0x100) is a request to dump, so we always send
	 * it through the crashdump code.
	 */
	if (kexec_should_crash(current) || (TRAP(regs) == 0x100)) {
166
		crash_kexec(regs);
167 168 169 170 171 172 173 174

		/*
		 * We aren't the primary crash CPU. We need to send it
		 * to a holding pattern to avoid it ending up in the panic
		 * code.
		 */
		crash_kexec_secondary(regs);
	}
175

A
Anton Blanchard 已提交
176 177 178
	if (!signr)
		return;

179 180 181 182 183 184 185 186 187 188 189
	/*
	 * While our oops output is serialised by a spinlock, output
	 * from panic() called below can race and corrupt it. If we
	 * know we are going to panic, delay for 1 second so we have a
	 * chance to get clean backtraces from all CPUs that are oopsing.
	 */
	if (in_interrupt() || panic_on_oops || !current->pid ||
	    is_global_init(current)) {
		mdelay(MSEC_PER_SEC);
	}

190 191
	if (in_interrupt())
		panic("Fatal exception in interrupt");
H
Horms 已提交
192
	if (panic_on_oops)
193
		panic("Fatal exception");
A
Anton Blanchard 已提交
194 195
	do_exit(signr);
}
H
Horms 已提交
196

A
Anton Blanchard 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
static int __kprobes __die(const char *str, struct pt_regs *regs, long err)
{
	printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
#ifdef CONFIG_PREEMPT
	printk("PREEMPT ");
#endif
#ifdef CONFIG_SMP
	printk("SMP NR_CPUS=%d ", NR_CPUS);
#endif
#ifdef CONFIG_DEBUG_PAGEALLOC
	printk("DEBUG_PAGEALLOC ");
#endif
#ifdef CONFIG_NUMA
	printk("NUMA ");
#endif
	printk("%s\n", ppc_md.name ? ppc_md.name : "");

	if (notify_die(DIE_OOPS, str, regs, err, 255, SIGSEGV) == NOTIFY_STOP)
		return 1;

	print_modules();
	show_regs(regs);
219 220 221 222

	return 0;
}

A
Anton Blanchard 已提交
223 224 225 226 227 228 229 230 231
void die(const char *str, struct pt_regs *regs, long err)
{
	unsigned long flags = oops_begin(regs);

	if (__die(str, regs, err))
		err = 0;
	oops_end(flags, regs, err);
}

232 233 234 235 236 237 238 239 240
void user_single_step_siginfo(struct task_struct *tsk,
				struct pt_regs *regs, siginfo_t *info)
{
	memset(info, 0, sizeof(*info));
	info->si_signo = SIGTRAP;
	info->si_code = TRAP_TRACE;
	info->si_addr = (void __user *)regs->nip;
}

241 242 243
void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
{
	siginfo_t info;
244 245 246 247
	const char fmt32[] = KERN_INFO "%s[%d]: unhandled signal %d " \
			"at %08lx nip %08lx lr %08lx code %x\n";
	const char fmt64[] = KERN_INFO "%s[%d]: unhandled signal %d " \
			"at %016lx nip %016lx lr %016lx code %x\n";
248 249

	if (!user_mode(regs)) {
A
Anton Blanchard 已提交
250 251 252 253 254
		die("Exception in kernel mode", regs, signr);
		return;
	}

	if (show_unhandled_signals && unhandled_signal(current, signr)) {
255 256 257 258
		printk_ratelimited(regs->msr & MSR_64BIT ? fmt64 : fmt32,
				   current->comm, current->pid, signr,
				   addr, regs->nip, regs->link, code);
	}
259

260
	if (arch_irqs_disabled() && !arch_irq_disabled_regs(regs))
261 262
		local_irq_enable();

263
	current->thread.trap_nr = code;
264 265 266 267 268 269 270 271 272 273 274
	memset(&info, 0, sizeof(info));
	info.si_signo = signr;
	info.si_code = code;
	info.si_addr = (void __user *) addr;
	force_sig_info(signr, &info, current);
}

#ifdef CONFIG_PPC64
void system_reset_exception(struct pt_regs *regs)
{
	/* See if any machine dependent calls */
275 276 277 278
	if (ppc_md.system_reset_exception) {
		if (ppc_md.system_reset_exception(regs))
			return;
	}
279

P
Paul Mackerras 已提交
280
	die("System Reset", regs, SIGABRT);
281 282 283 284 285 286 287

	/* 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. */
}
288 289 290 291 292 293 294 295

/*
 * This function is called in real mode. Strictly no printk's please.
 *
 * regs->nip and regs->msr contains srr0 and ssr1.
 */
long machine_check_early(struct pt_regs *regs)
{
296 297
	long handled = 0;

298
	__this_cpu_inc(irq_stat.mce_exceptions);
299

300 301
	add_taint(TAINT_MACHINE_CHECK, LOCKDEP_NOW_UNRELIABLE);

302 303 304
	if (cur_cpu_spec && cur_cpu_spec->machine_check_early)
		handled = cur_cpu_spec->machine_check_early(regs);
	return handled;
305 306
}

307 308
long hmi_exception_realmode(struct pt_regs *regs)
{
309
	__this_cpu_inc(irq_stat.hmi_exceptions);
310 311 312 313 314 315 316

	if (ppc_md.hmi_exception_early)
		ppc_md.hmi_exception_early(regs);

	return 0;
}

317 318 319 320 321 322 323 324 325 326 327 328 329 330
#endif

/*
 * I/O accesses can cause machine checks on powermacs.
 * Check if the NIP corresponds to the address of a sync
 * instruction for which there is an entry in the exception
 * table.
 * Note that the 601 only takes a machine check on TEA
 * (transfer error ack) signal assertion, and does not
 * set any of the top 16 bits of SRR1.
 *  -- paulus.
 */
static inline int check_io_access(struct pt_regs *regs)
{
331
#ifdef CONFIG_PPC32
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
	unsigned long msr = regs->msr;
	const struct exception_table_entry *entry;
	unsigned int *nip = (unsigned int *)regs->nip;

	if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
	    && (entry = search_exception_tables(regs->nip)) != NULL) {
		/*
		 * Check that it's a sync instruction, or somewhere
		 * in the twi; isync; nop sequence that inb/inw/inl uses.
		 * As the address is in the exception table
		 * we should be able to read the instr there.
		 * For the debug message, we look at the preceding
		 * load or store.
		 */
		if (*nip == 0x60000000)		/* nop */
			nip -= 2;
		else if (*nip == 0x4c00012c)	/* isync */
			--nip;
		if (*nip == 0x7c0004ac || (*nip >> 26) == 3) {
			/* sync or twi */
			unsigned int rb;

			--nip;
			rb = (*nip >> 11) & 0x1f;
			printk(KERN_DEBUG "%s bad port %lx at %p\n",
			       (*nip & 0x100)? "OUT to": "IN from",
			       regs->gpr[rb] - _IO_BASE, nip);
			regs->msr |= MSR_RI;
			regs->nip = entry->fixup;
			return 1;
		}
	}
364
#endif /* CONFIG_PPC32 */
365 366 367
	return 0;
}

368
#ifdef CONFIG_PPC_ADV_DEBUG_REGS
369 370 371 372 373 374
/* On 4xx, the reason for the machine check or program exception
   is in the ESR. */
#define get_reason(regs)	((regs)->dsisr)
#ifndef CONFIG_FSL_BOOKE
#define get_mc_reason(regs)	((regs)->dsisr)
#else
375
#define get_mc_reason(regs)	(mfspr(SPRN_MCSR))
376 377 378 379 380 381 382
#endif
#define REASON_FP		ESR_FP
#define REASON_ILLEGAL		(ESR_PIL | ESR_PUO)
#define REASON_PRIVILEGED	ESR_PPR
#define REASON_TRAP		ESR_PTR

/* single-step stuff */
383 384
#define single_stepping(regs)	(current->thread.debug.dbcr0 & DBCR0_IC)
#define clear_single_step(regs)	(current->thread.debug.dbcr0 &= ~DBCR0_IC)
385 386 387 388 389 390

#else
/* On non-4xx, the reason for the machine check or program
   exception is in the MSR. */
#define get_reason(regs)	((regs)->msr)
#define get_mc_reason(regs)	((regs)->msr)
391
#define REASON_TM		0x200000
392 393 394 395 396 397 398 399 400
#define REASON_FP		0x100000
#define REASON_ILLEGAL		0x80000
#define REASON_PRIVILEGED	0x40000
#define REASON_TRAP		0x20000

#define single_stepping(regs)	((regs)->msr & MSR_SE)
#define clear_single_step(regs)	((regs)->msr &= ~MSR_SE)
#endif

401 402
#if defined(CONFIG_4xx)
int machine_check_4xx(struct pt_regs *regs)
403
{
404
	unsigned long reason = get_mc_reason(regs);
405 406 407 408 409 410 411

	if (reason & ESR_IMCP) {
		printk("Instruction");
		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
	} else
		printk("Data");
	printk(" machine check in kernel mode.\n");
412 413 414 415 416 417 418 419

	return 0;
}

int machine_check_440A(struct pt_regs *regs)
{
	unsigned long reason = get_mc_reason(regs);

420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
	printk("Machine check in kernel mode.\n");
	if (reason & ESR_IMCP){
		printk("Instruction Synchronous Machine Check exception\n");
		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
	}
	else {
		u32 mcsr = mfspr(SPRN_MCSR);
		if (mcsr & MCSR_IB)
			printk("Instruction Read PLB Error\n");
		if (mcsr & MCSR_DRB)
			printk("Data Read PLB Error\n");
		if (mcsr & MCSR_DWB)
			printk("Data Write PLB Error\n");
		if (mcsr & MCSR_TLBP)
			printk("TLB Parity Error\n");
		if (mcsr & MCSR_ICP){
			flush_instruction_cache();
			printk("I-Cache Parity Error\n");
		}
		if (mcsr & MCSR_DCSP)
			printk("D-Cache Search Parity Error\n");
		if (mcsr & MCSR_DCFP)
			printk("D-Cache Flush Parity Error\n");
		if (mcsr & MCSR_IMPE)
			printk("Machine Check exception is imprecise\n");

		/* Clear MCSR */
		mtspr(SPRN_MCSR, mcsr);
	}
449 450
	return 0;
}
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490

int machine_check_47x(struct pt_regs *regs)
{
	unsigned long reason = get_mc_reason(regs);
	u32 mcsr;

	printk(KERN_ERR "Machine check in kernel mode.\n");
	if (reason & ESR_IMCP) {
		printk(KERN_ERR
		       "Instruction Synchronous Machine Check exception\n");
		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
		return 0;
	}
	mcsr = mfspr(SPRN_MCSR);
	if (mcsr & MCSR_IB)
		printk(KERN_ERR "Instruction Read PLB Error\n");
	if (mcsr & MCSR_DRB)
		printk(KERN_ERR "Data Read PLB Error\n");
	if (mcsr & MCSR_DWB)
		printk(KERN_ERR "Data Write PLB Error\n");
	if (mcsr & MCSR_TLBP)
		printk(KERN_ERR "TLB Parity Error\n");
	if (mcsr & MCSR_ICP) {
		flush_instruction_cache();
		printk(KERN_ERR "I-Cache Parity Error\n");
	}
	if (mcsr & MCSR_DCSP)
		printk(KERN_ERR "D-Cache Search Parity Error\n");
	if (mcsr & PPC47x_MCSR_GPR)
		printk(KERN_ERR "GPR Parity Error\n");
	if (mcsr & PPC47x_MCSR_FPR)
		printk(KERN_ERR "FPR Parity Error\n");
	if (mcsr & PPC47x_MCSR_IPR)
		printk(KERN_ERR "Machine Check exception is imprecise\n");

	/* Clear MCSR */
	mtspr(SPRN_MCSR, mcsr);

	return 0;
}
491
#elif defined(CONFIG_E500)
492 493 494 495 496 497
int machine_check_e500mc(struct pt_regs *regs)
{
	unsigned long mcsr = mfspr(SPRN_MCSR);
	unsigned long reason = mcsr;
	int recoverable = 1;

498
	if (reason & MCSR_LD) {
499 500 501 502 503
		recoverable = fsl_rio_mcheck_exception(regs);
		if (recoverable == 1)
			goto silent_out;
	}

504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
	printk("Machine check in kernel mode.\n");
	printk("Caused by (from MCSR=%lx): ", reason);

	if (reason & MCSR_MCP)
		printk("Machine Check Signal\n");

	if (reason & MCSR_ICPERR) {
		printk("Instruction Cache Parity Error\n");

		/*
		 * This is recoverable by invalidating the i-cache.
		 */
		mtspr(SPRN_L1CSR1, mfspr(SPRN_L1CSR1) | L1CSR1_ICFI);
		while (mfspr(SPRN_L1CSR1) & L1CSR1_ICFI)
			;

		/*
		 * This will generally be accompanied by an instruction
		 * fetch error report -- only treat MCSR_IF as fatal
		 * if it wasn't due to an L1 parity error.
		 */
		reason &= ~MCSR_IF;
	}

	if (reason & MCSR_DCPERR_MC) {
		printk("Data Cache Parity Error\n");
530 531 532 533 534 535 536 537

		/*
		 * In write shadow mode we auto-recover from the error, but it
		 * may still get logged and cause a machine check.  We should
		 * only treat the non-write shadow case as non-recoverable.
		 */
		if (!(mfspr(SPRN_L1CSR2) & L1CSR2_DCWS))
			recoverable = 0;
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
	}

	if (reason & MCSR_L2MMU_MHIT) {
		printk("Hit on multiple TLB entries\n");
		recoverable = 0;
	}

	if (reason & MCSR_NMI)
		printk("Non-maskable interrupt\n");

	if (reason & MCSR_IF) {
		printk("Instruction Fetch Error Report\n");
		recoverable = 0;
	}

	if (reason & MCSR_LD) {
		printk("Load Error Report\n");
		recoverable = 0;
	}

	if (reason & MCSR_ST) {
		printk("Store Error Report\n");
		recoverable = 0;
	}

	if (reason & MCSR_LDG) {
		printk("Guarded Load Error Report\n");
		recoverable = 0;
	}

	if (reason & MCSR_TLBSYNC)
		printk("Simultaneous tlbsync operations\n");

	if (reason & MCSR_BSL2_ERR) {
		printk("Level 2 Cache Error\n");
		recoverable = 0;
	}

	if (reason & MCSR_MAV) {
		u64 addr;

		addr = mfspr(SPRN_MCAR);
		addr |= (u64)mfspr(SPRN_MCARU) << 32;

		printk("Machine Check %s Address: %#llx\n",
		       reason & MCSR_MEA ? "Effective" : "Physical", addr);
	}

586
silent_out:
587 588 589 590
	mtspr(SPRN_MCSR, mcsr);
	return mfspr(SPRN_MCSR) == 0 && recoverable;
}

591 592 593 594
int machine_check_e500(struct pt_regs *regs)
{
	unsigned long reason = get_mc_reason(regs);

595 596 597
	if (reason & MCSR_BUS_RBERR) {
		if (fsl_rio_mcheck_exception(regs))
			return 1;
598 599
		if (fsl_pci_mcheck_exception(regs))
			return 1;
600 601
	}

602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
	printk("Machine check in kernel mode.\n");
	printk("Caused by (from MCSR=%lx): ", reason);

	if (reason & MCSR_MCP)
		printk("Machine Check Signal\n");
	if (reason & MCSR_ICPERR)
		printk("Instruction Cache Parity Error\n");
	if (reason & MCSR_DCP_PERR)
		printk("Data Cache Push Parity Error\n");
	if (reason & MCSR_DCPERR)
		printk("Data Cache Parity Error\n");
	if (reason & MCSR_BUS_IAERR)
		printk("Bus - Instruction Address Error\n");
	if (reason & MCSR_BUS_RAERR)
		printk("Bus - Read Address Error\n");
	if (reason & MCSR_BUS_WAERR)
		printk("Bus - Write Address Error\n");
	if (reason & MCSR_BUS_IBERR)
		printk("Bus - Instruction Data Error\n");
	if (reason & MCSR_BUS_RBERR)
		printk("Bus - Read Data Bus Error\n");
	if (reason & MCSR_BUS_WBERR)
624
		printk("Bus - Write Data Bus Error\n");
625 626 627 628
	if (reason & MCSR_BUS_IPERR)
		printk("Bus - Instruction Parity Error\n");
	if (reason & MCSR_BUS_RPERR)
		printk("Bus - Read Parity Error\n");
629 630 631

	return 0;
}
632 633 634 635 636

int machine_check_generic(struct pt_regs *regs)
{
	return 0;
}
637 638 639 640 641
#elif defined(CONFIG_E200)
int machine_check_e200(struct pt_regs *regs)
{
	unsigned long reason = get_mc_reason(regs);

642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
	printk("Machine check in kernel mode.\n");
	printk("Caused by (from MCSR=%lx): ", reason);

	if (reason & MCSR_MCP)
		printk("Machine Check Signal\n");
	if (reason & MCSR_CP_PERR)
		printk("Cache Push Parity Error\n");
	if (reason & MCSR_CPERR)
		printk("Cache Parity Error\n");
	if (reason & MCSR_EXCP_ERR)
		printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
	if (reason & MCSR_BUS_IRERR)
		printk("Bus - Read Bus Error on instruction fetch\n");
	if (reason & MCSR_BUS_DRERR)
		printk("Bus - Read Bus Error on data load\n");
	if (reason & MCSR_BUS_WRERR)
		printk("Bus - Write Bus Error on buffered store or cache line push\n");
659 660 661 662 663 664 665 666

	return 0;
}
#else
int machine_check_generic(struct pt_regs *regs)
{
	unsigned long reason = get_mc_reason(regs);

667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
	printk("Machine check in kernel mode.\n");
	printk("Caused by (from SRR1=%lx): ", reason);
	switch (reason & 0x601F0000) {
	case 0x80000:
		printk("Machine check signal\n");
		break;
	case 0:		/* for 601 */
	case 0x40000:
	case 0x140000:	/* 7450 MSS error and TEA */
		printk("Transfer error ack signal\n");
		break;
	case 0x20000:
		printk("Data parity error signal\n");
		break;
	case 0x10000:
		printk("Address parity error signal\n");
		break;
	case 0x20000000:
		printk("L1 Data Cache error\n");
		break;
	case 0x40000000:
		printk("L1 Instruction Cache error\n");
		break;
	case 0x00100000:
		printk("L2 data cache parity error\n");
		break;
	default:
		printk("Unknown values in msr\n");
	}
696 697
	return 0;
}
698
#endif /* everything else */
699 700 701

void machine_check_exception(struct pt_regs *regs)
{
702
	enum ctx_state prev_state = exception_enter();
703 704
	int recover = 0;

705
	__this_cpu_inc(irq_stat.mce_exceptions);
706

707 708 709 710 711 712
	/* See if any machine dependent calls. In theory, we would want
	 * to call the CPU first, and call the ppc_md. one if the CPU
	 * one returns a positive number. However there is existing code
	 * that assumes the board gets a first chance, so let's keep it
	 * that way for now and fix things later. --BenH.
	 */
713 714
	if (ppc_md.machine_check_exception)
		recover = ppc_md.machine_check_exception(regs);
715 716
	else if (cur_cpu_spec->machine_check)
		recover = cur_cpu_spec->machine_check(regs);
717

718
	if (recover > 0)
719
		goto bail;
720 721

#if defined(CONFIG_8xx) && defined(CONFIG_PCI)
722 723 724 725 726 727
	/* the qspan pci read routines can cause machine checks -- Cort
	 *
	 * yuck !!! that totally needs to go away ! There are better ways
	 * to deal with that than having a wart in the mcheck handler.
	 * -- BenH
	 */
728
	bad_page_fault(regs, regs->dar, SIGBUS);
729
	goto bail;
730 731
#endif

732
	if (debugger_fault_handler(regs))
733
		goto bail;
734 735

	if (check_io_access(regs))
736
		goto bail;
737

P
Paul Mackerras 已提交
738
	die("Machine check", regs, SIGBUS);
739 740 741 742

	/* Must die if the interrupt is not recoverable */
	if (!(regs->msr & MSR_RI))
		panic("Unrecoverable Machine check");
743 744 745

bail:
	exception_exit(prev_state);
746 747 748 749 750 751 752
}

void SMIException(struct pt_regs *regs)
{
	die("System Management Interrupt", regs, SIGABRT);
}

753 754 755 756 757 758 759 760 761 762 763 764 765 766
void handle_hmi_exception(struct pt_regs *regs)
{
	struct pt_regs *old_regs;

	old_regs = set_irq_regs(regs);
	irq_enter();

	if (ppc_md.handle_hmi_exception)
		ppc_md.handle_hmi_exception(regs);

	irq_exit();
	set_irq_regs(old_regs);
}

767
void unknown_exception(struct pt_regs *regs)
768
{
769 770
	enum ctx_state prev_state = exception_enter();

771 772 773 774
	printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
	       regs->nip, regs->msr, regs->trap);

	_exception(SIGTRAP, regs, 0, 0);
775 776

	exception_exit(prev_state);
777 778
}

779
void instruction_breakpoint_exception(struct pt_regs *regs)
780
{
781 782
	enum ctx_state prev_state = exception_enter();

783 784
	if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
					5, SIGTRAP) == NOTIFY_STOP)
785
		goto bail;
786
	if (debugger_iabr_match(regs))
787
		goto bail;
788
	_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
789 790 791

bail:
	exception_exit(prev_state);
792 793 794 795 796 797 798
}

void RunModeException(struct pt_regs *regs)
{
	_exception(SIGTRAP, regs, 0, 0);
}

P
Paul Mackerras 已提交
799
void __kprobes single_step_exception(struct pt_regs *regs)
800
{
801 802
	enum ctx_state prev_state = exception_enter();

803
	clear_single_step(regs);
804 805 806

	if (notify_die(DIE_SSTEP, "single_step", regs, 5,
					5, SIGTRAP) == NOTIFY_STOP)
807
		goto bail;
808
	if (debugger_sstep(regs))
809
		goto bail;
810 811

	_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
812 813 814

bail:
	exception_exit(prev_state);
815 816 817 818 819 820 821 822
}

/*
 * After we have successfully emulated an instruction, we have to
 * check if the instruction was being single-stepped, and if so,
 * pretend we got a single-step exception.  This was pointed out
 * by Kumar Gala.  -- paulus
 */
P
Paul Mackerras 已提交
823
static void emulate_single_step(struct pt_regs *regs)
824
{
825 826
	if (single_stepping(regs))
		single_step_exception(regs);
827 828
}

829
static inline int __parse_fpscr(unsigned long fpscr)
830
{
831
	int ret = 0;
832 833 834

	/* Invalid operation */
	if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
835
		ret = FPE_FLTINV;
836 837 838

	/* Overflow */
	else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
839
		ret = FPE_FLTOVF;
840 841 842

	/* Underflow */
	else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
843
		ret = FPE_FLTUND;
844 845 846

	/* Divide by zero */
	else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
847
		ret = FPE_FLTDIV;
848 849 850

	/* Inexact result */
	else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
851 852 853 854 855 856 857 858 859 860 861
		ret = FPE_FLTRES;

	return ret;
}

static void parse_fpe(struct pt_regs *regs)
{
	int code = 0;

	flush_fp_to_thread(current);

862
	code = __parse_fpscr(current->thread.fp_state.fpscr);
863 864 865 866 867 868

	_exception(SIGFPE, regs, code, regs->nip);
}

/*
 * Illegal instruction emulation support.  Originally written to
869 870 871 872 873 874 875
 * provide the PVR to user applications using the mfspr rd, PVR.
 * Return non-zero if we can't emulate, or -EFAULT if the associated
 * memory access caused an access fault.  Return zero on success.
 *
 * There are a couple of ways to do this, either "decode" the instruction
 * or directly match lots of bits.  In this case, matching lots of
 * bits is faster and easier.
876
 *
877 878 879 880 881 882 883 884 885 886 887
 */
static int emulate_string_inst(struct pt_regs *regs, u32 instword)
{
	u8 rT = (instword >> 21) & 0x1f;
	u8 rA = (instword >> 16) & 0x1f;
	u8 NB_RB = (instword >> 11) & 0x1f;
	u32 num_bytes;
	unsigned long EA;
	int pos = 0;

	/* Early out if we are an invalid form of lswx */
888
	if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
889 890 891 892 893
		if ((rT == rA) || (rT == NB_RB))
			return -EINVAL;

	EA = (rA == 0) ? 0 : regs->gpr[rA];

894 895 896
	switch (instword & PPC_INST_STRING_MASK) {
		case PPC_INST_LSWX:
		case PPC_INST_STSWX:
897 898 899
			EA += NB_RB;
			num_bytes = regs->xer & 0x7f;
			break;
900 901
		case PPC_INST_LSWI:
		case PPC_INST_STSWI:
902 903 904 905 906 907 908 909 910 911 912
			num_bytes = (NB_RB == 0) ? 32 : NB_RB;
			break;
		default:
			return -EINVAL;
	}

	while (num_bytes != 0)
	{
		u8 val;
		u32 shift = 8 * (3 - (pos & 0x3));

913 914 915 916
		/* if process is 32-bit, clear upper 32 bits of EA */
		if ((regs->msr & MSR_64BIT) == 0)
			EA &= 0xFFFFFFFF;

917 918 919
		switch ((instword & PPC_INST_STRING_MASK)) {
			case PPC_INST_LSWX:
			case PPC_INST_LSWI:
920 921 922 923 924 925 926 927
				if (get_user(val, (u8 __user *)EA))
					return -EFAULT;
				/* first time updating this reg,
				 * zero it out */
				if (pos == 0)
					regs->gpr[rT] = 0;
				regs->gpr[rT] |= val << shift;
				break;
928 929
			case PPC_INST_STSWI:
			case PPC_INST_STSWX:
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
				val = regs->gpr[rT] >> shift;
				if (put_user(val, (u8 __user *)EA))
					return -EFAULT;
				break;
		}
		/* move EA to next address */
		EA += 1;
		num_bytes--;

		/* manage our position within the register */
		if (++pos == 4) {
			pos = 0;
			if (++rT == 32)
				rT = 0;
		}
	}

	return 0;
}

950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
{
	u32 ra,rs;
	unsigned long tmp;

	ra = (instword >> 16) & 0x1f;
	rs = (instword >> 21) & 0x1f;

	tmp = regs->gpr[rs];
	tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
	tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
	tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
	regs->gpr[ra] = tmp;

	return 0;
}

967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
static int emulate_isel(struct pt_regs *regs, u32 instword)
{
	u8 rT = (instword >> 21) & 0x1f;
	u8 rA = (instword >> 16) & 0x1f;
	u8 rB = (instword >> 11) & 0x1f;
	u8 BC = (instword >> 6) & 0x1f;
	u8 bit;
	unsigned long tmp;

	tmp = (rA == 0) ? 0 : regs->gpr[rA];
	bit = (regs->ccr >> (31 - BC)) & 0x1;

	regs->gpr[rT] = bit ? tmp : regs->gpr[rB];

	return 0;
}

984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
static inline bool tm_abort_check(struct pt_regs *regs, int cause)
{
        /* If we're emulating a load/store in an active transaction, we cannot
         * emulate it as the kernel operates in transaction suspended context.
         * We need to abort the transaction.  This creates a persistent TM
         * abort so tell the user what caused it with a new code.
	 */
	if (MSR_TM_TRANSACTIONAL(regs->msr)) {
		tm_enable();
		tm_abort(cause);
		return true;
	}
	return false;
}
#else
static inline bool tm_abort_check(struct pt_regs *regs, int reason)
{
	return false;
}
#endif

1006 1007 1008 1009 1010
static int emulate_instruction(struct pt_regs *regs)
{
	u32 instword;
	u32 rd;

1011
	if (!user_mode(regs))
1012 1013 1014 1015 1016 1017 1018
		return -EINVAL;
	CHECK_FULL_REGS(regs);

	if (get_user(instword, (u32 __user *)(regs->nip)))
		return -EFAULT;

	/* Emulate the mfspr rD, PVR. */
1019
	if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
1020
		PPC_WARN_EMULATED(mfpvr, regs);
1021 1022 1023 1024 1025 1026
		rd = (instword >> 21) & 0x1f;
		regs->gpr[rd] = mfspr(SPRN_PVR);
		return 0;
	}

	/* Emulating the dcba insn is just a no-op.  */
1027
	if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
1028
		PPC_WARN_EMULATED(dcba, regs);
1029
		return 0;
1030
	}
1031 1032

	/* Emulate the mcrxr insn.  */
1033
	if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
1034
		int shift = (instword >> 21) & 0x1c;
1035 1036
		unsigned long msk = 0xf0000000UL >> shift;

1037
		PPC_WARN_EMULATED(mcrxr, regs);
1038 1039 1040 1041 1042 1043
		regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
		regs->xer &= ~0xf0000000UL;
		return 0;
	}

	/* Emulate load/store string insn. */
1044
	if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
1045 1046 1047
		if (tm_abort_check(regs,
				   TM_CAUSE_EMULATE | TM_CAUSE_PERSISTENT))
			return -EINVAL;
1048
		PPC_WARN_EMULATED(string, regs);
1049
		return emulate_string_inst(regs, instword);
1050
	}
1051

1052
	/* Emulate the popcntb (Population Count Bytes) instruction. */
1053
	if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
1054
		PPC_WARN_EMULATED(popcntb, regs);
1055 1056 1057
		return emulate_popcntb_inst(regs, instword);
	}

1058
	/* Emulate isel (Integer Select) instruction */
1059
	if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
1060
		PPC_WARN_EMULATED(isel, regs);
1061 1062 1063
		return emulate_isel(regs, instword);
	}

1064 1065 1066 1067 1068 1069 1070
	/* Emulate sync instruction variants */
	if ((instword & PPC_INST_SYNC_MASK) == PPC_INST_SYNC) {
		PPC_WARN_EMULATED(sync, regs);
		asm volatile("sync");
		return 0;
	}

1071 1072
#ifdef CONFIG_PPC64
	/* Emulate the mfspr rD, DSCR. */
1073 1074 1075 1076
	if ((((instword & PPC_INST_MFSPR_DSCR_USER_MASK) ==
		PPC_INST_MFSPR_DSCR_USER) ||
	     ((instword & PPC_INST_MFSPR_DSCR_MASK) ==
		PPC_INST_MFSPR_DSCR)) &&
1077 1078 1079 1080 1081 1082 1083
			cpu_has_feature(CPU_FTR_DSCR)) {
		PPC_WARN_EMULATED(mfdscr, regs);
		rd = (instword >> 21) & 0x1f;
		regs->gpr[rd] = mfspr(SPRN_DSCR);
		return 0;
	}
	/* Emulate the mtspr DSCR, rD. */
1084 1085 1086 1087
	if ((((instword & PPC_INST_MTSPR_DSCR_USER_MASK) ==
		PPC_INST_MTSPR_DSCR_USER) ||
	     ((instword & PPC_INST_MTSPR_DSCR_MASK) ==
		PPC_INST_MTSPR_DSCR)) &&
1088 1089 1090
			cpu_has_feature(CPU_FTR_DSCR)) {
		PPC_WARN_EMULATED(mtdscr, regs);
		rd = (instword >> 21) & 0x1f;
1091
		current->thread.dscr = regs->gpr[rd];
1092
		current->thread.dscr_inherit = 1;
1093
		mtspr(SPRN_DSCR, current->thread.dscr);
1094 1095 1096 1097
		return 0;
	}
#endif

1098 1099 1100
	return -EINVAL;
}

1101
int is_valid_bugaddr(unsigned long addr)
1102
{
1103
	return is_kernel_addr(addr);
1104 1105
}

1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
#ifdef CONFIG_MATH_EMULATION
static int emulate_math(struct pt_regs *regs)
{
	int ret;
	extern int do_mathemu(struct pt_regs *regs);

	ret = do_mathemu(regs);
	if (ret >= 0)
		PPC_WARN_EMULATED(math, regs);

	switch (ret) {
	case 0:
		emulate_single_step(regs);
		return 0;
	case 1: {
			int code = 0;
1122
			code = __parse_fpscr(current->thread.fp_state.fpscr);
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
			_exception(SIGFPE, regs, code, regs->nip);
			return 0;
		}
	case -EFAULT:
		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
		return 0;
	}

	return -1;
}
#else
static inline int emulate_math(struct pt_regs *regs) { return -1; }
#endif

P
Paul Mackerras 已提交
1137
void __kprobes program_check_exception(struct pt_regs *regs)
1138
{
1139
	enum ctx_state prev_state = exception_enter();
1140 1141
	unsigned int reason = get_reason(regs);

1142
	/* We can now get here via a FP Unavailable exception if the core
1143
	 * has no FPU, in that case the reason flags will be 0 */
1144

1145 1146 1147
	if (reason & REASON_FP) {
		/* IEEE FP exception */
		parse_fpe(regs);
1148
		goto bail;
P
Paul Mackerras 已提交
1149 1150
	}
	if (reason & REASON_TRAP) {
1151
		unsigned long bugaddr;
1152 1153 1154
		/* Debugger is first in line to stop recursive faults in
		 * rcu_lock, notify_die, or atomic_notifier_call_chain */
		if (debugger_bpt(regs))
1155
			goto bail;
1156

1157
		/* trap exception */
1158 1159
		if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
				== NOTIFY_STOP)
1160
			goto bail;
1161

1162 1163 1164 1165 1166 1167 1168
		bugaddr = regs->nip;
		/*
		 * Fixup bugaddr for BUG_ON() in real mode
		 */
		if (!is_kernel_addr(bugaddr) && !(regs->msr & MSR_IR))
			bugaddr += PAGE_OFFSET;

1169
		if (!(regs->msr & MSR_PR) &&  /* not user-mode */
1170
		    report_bug(bugaddr, regs) == BUG_TRAP_TYPE_WARN) {
1171
			regs->nip += 4;
1172
			goto bail;
1173
		}
P
Paul Mackerras 已提交
1174
		_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
1175
		goto bail;
P
Paul Mackerras 已提交
1176
	}
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
	if (reason & REASON_TM) {
		/* This is a TM "Bad Thing Exception" program check.
		 * This occurs when:
		 * -  An rfid/hrfid/mtmsrd attempts to cause an illegal
		 *    transition in TM states.
		 * -  A trechkpt is attempted when transactional.
		 * -  A treclaim is attempted when non transactional.
		 * -  A tend is illegally attempted.
		 * -  writing a TM SPR when transactional.
		 */
		if (!user_mode(regs) &&
		    report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
			regs->nip += 4;
1191
			goto bail;
1192 1193 1194 1195 1196 1197 1198 1199 1200
		}
		/* If usermode caused this, it's done something illegal and
		 * gets a SIGILL slap on the wrist.  We call it an illegal
		 * operand to distinguish from the instruction just being bad
		 * (e.g. executing a 'tend' on a CPU without TM!); it's an
		 * illegal /placement/ of a valid instruction.
		 */
		if (user_mode(regs)) {
			_exception(SIGILL, regs, ILL_ILLOPN, regs->nip);
1201
			goto bail;
1202 1203 1204 1205 1206 1207 1208
		} else {
			printk(KERN_EMERG "Unexpected TM Bad Thing exception "
			       "at %lx (msr 0x%x)\n", regs->nip, reason);
			die("Unrecoverable exception", regs, SIGABRT);
		}
	}
#endif
P
Paul Mackerras 已提交
1209

1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
	/*
	 * If we took the program check in the kernel skip down to sending a
	 * SIGILL. The subsequent cases all relate to emulating instructions
	 * which we should only do for userspace. We also do not want to enable
	 * interrupts for kernel faults because that might lead to further
	 * faults, and loose the context of the original exception.
	 */
	if (!user_mode(regs))
		goto sigill;

1220 1221 1222
	/* We restore the interrupt state now */
	if (!arch_irq_disabled_regs(regs))
		local_irq_enable();
1223

1224 1225 1226 1227 1228 1229
	/* (reason & REASON_ILLEGAL) would be the obvious thing here,
	 * but there seems to be a hardware bug on the 405GP (RevD)
	 * that means ESR is sometimes set incorrectly - either to
	 * ESR_DST (!?) or 0.  In the process of chasing this with the
	 * hardware people - not sure if it can happen on any illegal
	 * instruction or only on FP instructions, whether there is a
1230 1231
	 * pattern to occurrences etc. -dgibson 31/Mar/2003
	 */
1232
	if (!emulate_math(regs))
1233
		goto bail;
1234

P
Paul Mackerras 已提交
1235 1236
	/* Try to emulate it if we should. */
	if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
1237 1238 1239 1240
		switch (emulate_instruction(regs)) {
		case 0:
			regs->nip += 4;
			emulate_single_step(regs);
1241
			goto bail;
1242 1243
		case -EFAULT:
			_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1244
			goto bail;
1245 1246
		}
	}
P
Paul Mackerras 已提交
1247

1248
sigill:
P
Paul Mackerras 已提交
1249 1250 1251 1252
	if (reason & REASON_PRIVILEGED)
		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
	else
		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1253 1254 1255

bail:
	exception_exit(prev_state);
1256 1257
}

1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
/*
 * This occurs when running in hypervisor mode on POWER6 or later
 * and an illegal instruction is encountered.
 */
void __kprobes emulation_assist_interrupt(struct pt_regs *regs)
{
	regs->msr |= REASON_ILLEGAL;
	program_check_exception(regs);
}

1268
void alignment_exception(struct pt_regs *regs)
1269
{
1270
	enum ctx_state prev_state = exception_enter();
1271
	int sig, code, fixed = 0;
1272

1273 1274 1275 1276
	/* We restore the interrupt state now */
	if (!arch_irq_disabled_regs(regs))
		local_irq_enable();

1277 1278 1279
	if (tm_abort_check(regs, TM_CAUSE_ALIGNMENT | TM_CAUSE_PERSISTENT))
		goto bail;

1280 1281 1282
	/* we don't implement logging of alignment exceptions */
	if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
		fixed = fix_alignment(regs);
1283 1284 1285 1286

	if (fixed == 1) {
		regs->nip += 4;	/* skip over emulated instruction */
		emulate_single_step(regs);
1287
		goto bail;
1288 1289
	}

1290
	/* Operand address was bad */
1291
	if (fixed == -EFAULT) {
1292 1293 1294 1295 1296
		sig = SIGSEGV;
		code = SEGV_ACCERR;
	} else {
		sig = SIGBUS;
		code = BUS_ADRALN;
1297
	}
1298 1299 1300 1301
	if (user_mode(regs))
		_exception(sig, regs, code, regs->dar);
	else
		bad_page_fault(regs, regs->dar, sig);
1302 1303 1304

bail:
	exception_exit(prev_state);
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
}

void StackOverflow(struct pt_regs *regs)
{
	printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
	       current, regs->gpr[1]);
	debugger(regs);
	show_regs(regs);
	panic("kernel stack overflow");
}

void nonrecoverable_exception(struct pt_regs *regs)
{
	printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
	       regs->nip, regs->msr);
	debugger(regs);
	die("nonrecoverable exception", regs, SIGKILL);
}

1324 1325
void kernel_fp_unavailable_exception(struct pt_regs *regs)
{
1326 1327
	enum ctx_state prev_state = exception_enter();

1328 1329 1330
	printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
			  "%lx at %lx\n", regs->trap, regs->nip);
	die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
1331 1332

	exception_exit(prev_state);
1333 1334 1335 1336
}

void altivec_unavailable_exception(struct pt_regs *regs)
{
1337 1338
	enum ctx_state prev_state = exception_enter();

1339 1340 1341 1342
	if (user_mode(regs)) {
		/* A user program has executed an altivec instruction,
		   but this kernel doesn't support altivec. */
		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1343
		goto bail;
1344
	}
1345

1346 1347 1348
	printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
			"%lx at %lx\n", regs->trap, regs->nip);
	die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
1349 1350 1351

bail:
	exception_exit(prev_state);
1352 1353
}

1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
void vsx_unavailable_exception(struct pt_regs *regs)
{
	if (user_mode(regs)) {
		/* A user program has executed an vsx instruction,
		   but this kernel doesn't support vsx. */
		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
		return;
	}

	printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
			"%lx at %lx\n", regs->trap, regs->nip);
	die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
}

1368
#ifdef CONFIG_PPC64
1369
void facility_unavailable_exception(struct pt_regs *regs)
1370
{
1371
	static char *facility_strings[] = {
1372 1373 1374 1375 1376 1377 1378 1379
		[FSCR_FP_LG] = "FPU",
		[FSCR_VECVSX_LG] = "VMX/VSX",
		[FSCR_DSCR_LG] = "DSCR",
		[FSCR_PM_LG] = "PMU SPRs",
		[FSCR_BHRB_LG] = "BHRB",
		[FSCR_TM_LG] = "TM",
		[FSCR_EBB_LG] = "EBB",
		[FSCR_TAR_LG] = "TAR",
1380
	};
1381
	char *facility = "unknown";
1382
	u64 value;
1383
	u32 instword, rd;
1384 1385
	u8 status;
	bool hv;
1386

1387 1388
	hv = (regs->trap == 0xf80);
	if (hv)
1389
		value = mfspr(SPRN_HFSCR);
1390 1391 1392 1393 1394
	else
		value = mfspr(SPRN_FSCR);

	status = value >> 56;
	if (status == FSCR_DSCR_LG) {
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404
		/*
		 * User is accessing the DSCR register using the problem
		 * state only SPR number (0x03) either through a mfspr or
		 * a mtspr instruction. If it is a write attempt through
		 * a mtspr, then we set the inherit bit. This also allows
		 * the user to write or read the register directly in the
		 * future by setting via the FSCR DSCR bit. But in case it
		 * is a read DSCR attempt through a mfspr instruction, we
		 * just emulate the instruction instead. This code path will
		 * always emulate all the mfspr instructions till the user
1405
		 * has attempted at least one mtspr instruction. This way it
1406 1407 1408 1409
		 * preserves the same behaviour when the user is accessing
		 * the DSCR through privilege level only SPR number (0x11)
		 * which is emulated through illegal instruction exception.
		 * We always leave HFSCR DSCR set.
1410
		 */
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
		if (get_user(instword, (u32 __user *)(regs->nip))) {
			pr_err("Failed to fetch the user instruction\n");
			return;
		}

		/* Write into DSCR (mtspr 0x03, RS) */
		if ((instword & PPC_INST_MTSPR_DSCR_USER_MASK)
				== PPC_INST_MTSPR_DSCR_USER) {
			rd = (instword >> 21) & 0x1f;
			current->thread.dscr = regs->gpr[rd];
			current->thread.dscr_inherit = 1;
			mtspr(SPRN_FSCR, value | FSCR_DSCR);
		}

		/* Read from DSCR (mfspr RT, 0x03) */
		if ((instword & PPC_INST_MFSPR_DSCR_USER_MASK)
				== PPC_INST_MFSPR_DSCR_USER) {
			if (emulate_instruction(regs)) {
				pr_err("DSCR based mfspr emulation failed\n");
				return;
			}
			regs->nip += 4;
			emulate_single_step(regs);
		}
1435
		return;
1436 1437
	}

1438 1439 1440
	if ((status < ARRAY_SIZE(facility_strings)) &&
	    facility_strings[status])
		facility = facility_strings[status];
1441

1442 1443 1444 1445
	/* We restore the interrupt state now */
	if (!arch_irq_disabled_regs(regs))
		local_irq_enable();

1446 1447 1448
	pr_err_ratelimited(
		"%sFacility '%s' unavailable, exception at 0x%lx, MSR=%lx\n",
		hv ? "Hypervisor " : "", facility, regs->nip, regs->msr);
1449 1450 1451 1452 1453 1454

	if (user_mode(regs)) {
		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
		return;
	}

1455
	die("Unexpected facility unavailable exception", regs, SIGABRT);
1456
}
1457
#endif
1458

1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474
#ifdef CONFIG_PPC_TRANSACTIONAL_MEM

void fp_unavailable_tm(struct pt_regs *regs)
{
	/* Note:  This does not handle any kind of FP laziness. */

	TM_DEBUG("FP Unavailable trap whilst transactional at 0x%lx, MSR=%lx\n",
		 regs->nip, regs->msr);

        /* We can only have got here if the task started using FP after
         * beginning the transaction.  So, the transactional regs are just a
         * copy of the checkpointed ones.  But, we still need to recheckpoint
         * as we're enabling FP for the process; it will return, abort the
         * transaction, and probably retry but now with FP enabled.  So the
         * checkpointed FP registers need to be loaded.
	 */
1475
	tm_reclaim_current(TM_CAUSE_FAC_UNAV);
1476 1477 1478 1479 1480 1481 1482 1483
	/* Reclaim didn't save out any FPRs to transact_fprs. */

	/* Enable FP for the task: */
	regs->msr |= (MSR_FP | current->thread.fpexc_mode);

	/* This loads and recheckpoints the FP registers from
	 * thread.fpr[].  They will remain in registers after the
	 * checkpoint so we don't need to reload them after.
1484 1485
	 * If VMX is in use, the VRs now hold checkpointed values,
	 * so we don't want to load the VRs from the thread_struct.
1486
	 */
1487 1488 1489 1490 1491 1492 1493 1494
	tm_recheckpoint(&current->thread, MSR_FP);

	/* If VMX is in use, get the transactional values back */
	if (regs->msr & MSR_VEC) {
		do_load_up_transact_altivec(&current->thread);
		/* At this point all the VSX state is loaded, so enable it */
		regs->msr |= MSR_VSX;
	}
1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505
}

void altivec_unavailable_tm(struct pt_regs *regs)
{
	/* See the comments in fp_unavailable_tm().  This function operates
	 * the same way.
	 */

	TM_DEBUG("Vector Unavailable trap whilst transactional at 0x%lx,"
		 "MSR=%lx\n",
		 regs->nip, regs->msr);
1506
	tm_reclaim_current(TM_CAUSE_FAC_UNAV);
1507
	regs->msr |= MSR_VEC;
1508
	tm_recheckpoint(&current->thread, MSR_VEC);
1509
	current->thread.used_vr = 1;
1510 1511 1512 1513 1514

	if (regs->msr & MSR_FP) {
		do_load_up_transact_fpu(&current->thread);
		regs->msr |= MSR_VSX;
	}
1515 1516 1517 1518
}

void vsx_unavailable_tm(struct pt_regs *regs)
{
1519 1520
	unsigned long orig_msr = regs->msr;

1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531
	/* See the comments in fp_unavailable_tm().  This works similarly,
	 * though we're loading both FP and VEC registers in here.
	 *
	 * If FP isn't in use, load FP regs.  If VEC isn't in use, load VEC
	 * regs.  Either way, set MSR_VSX.
	 */

	TM_DEBUG("VSX Unavailable trap whilst transactional at 0x%lx,"
		 "MSR=%lx\n",
		 regs->nip, regs->msr);

1532 1533 1534 1535 1536 1537 1538 1539
	current->thread.used_vsr = 1;

	/* If FP and VMX are already loaded, we have all the state we need */
	if ((orig_msr & (MSR_FP | MSR_VEC)) == (MSR_FP | MSR_VEC)) {
		regs->msr |= MSR_VSX;
		return;
	}

1540
	/* This reclaims FP and/or VR regs if they're already enabled */
1541
	tm_reclaim_current(TM_CAUSE_FAC_UNAV);
1542 1543 1544

	regs->msr |= MSR_VEC | MSR_FP | current->thread.fpexc_mode |
		MSR_VSX;
1545 1546 1547 1548 1549 1550 1551 1552 1553 1554

	/* This loads & recheckpoints FP and VRs; but we have
	 * to be sure not to overwrite previously-valid state.
	 */
	tm_recheckpoint(&current->thread, regs->msr & ~orig_msr);

	if (orig_msr & MSR_FP)
		do_load_up_transact_fpu(&current->thread);
	if (orig_msr & MSR_VEC)
		do_load_up_transact_altivec(&current->thread);
1555 1556 1557
}
#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */

1558 1559
void performance_monitor_exception(struct pt_regs *regs)
{
1560
	__this_cpu_inc(irq_stat.pmu_irqs);
1561

1562 1563 1564
	perf_irq(regs);
}

P
Paul Mackerras 已提交
1565
#ifdef CONFIG_8xx
1566 1567 1568 1569 1570 1571
void SoftwareEmulation(struct pt_regs *regs)
{
	CHECK_FULL_REGS(regs);

	if (!user_mode(regs)) {
		debugger(regs);
1572 1573
		die("Kernel Mode Unimplemented Instruction or SW FPU Emulation",
			regs, SIGFPE);
1574 1575
	}

1576
	if (!emulate_math(regs))
1577
		return;
1578

1579
	_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1580
}
P
Paul Mackerras 已提交
1581
#endif /* CONFIG_8xx */
1582

1583
#ifdef CONFIG_PPC_ADV_DEBUG_REGS
1584 1585 1586 1587 1588 1589 1590 1591 1592 1593
static void handle_debug(struct pt_regs *regs, unsigned long debug_status)
{
	int changed = 0;
	/*
	 * Determine the cause of the debug event, clear the
	 * event flags and send a trap to the handler. Torez
	 */
	if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
		dbcr_dac(current) &= ~(DBCR_DAC1R | DBCR_DAC1W);
#ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
1594
		current->thread.debug.dbcr2 &= ~DBCR2_DAC12MODE;
1595 1596 1597 1598 1599 1600 1601 1602 1603 1604
#endif
		do_send_trap(regs, mfspr(SPRN_DAC1), debug_status, TRAP_HWBKPT,
			     5);
		changed |= 0x01;
	}  else if (debug_status & (DBSR_DAC2R | DBSR_DAC2W)) {
		dbcr_dac(current) &= ~(DBCR_DAC2R | DBCR_DAC2W);
		do_send_trap(regs, mfspr(SPRN_DAC2), debug_status, TRAP_HWBKPT,
			     6);
		changed |= 0x01;
	}  else if (debug_status & DBSR_IAC1) {
1605
		current->thread.debug.dbcr0 &= ~DBCR0_IAC1;
1606 1607 1608 1609 1610
		dbcr_iac_range(current) &= ~DBCR_IAC12MODE;
		do_send_trap(regs, mfspr(SPRN_IAC1), debug_status, TRAP_HWBKPT,
			     1);
		changed |= 0x01;
	}  else if (debug_status & DBSR_IAC2) {
1611
		current->thread.debug.dbcr0 &= ~DBCR0_IAC2;
1612 1613 1614 1615
		do_send_trap(regs, mfspr(SPRN_IAC2), debug_status, TRAP_HWBKPT,
			     2);
		changed |= 0x01;
	}  else if (debug_status & DBSR_IAC3) {
1616
		current->thread.debug.dbcr0 &= ~DBCR0_IAC3;
1617 1618 1619 1620 1621
		dbcr_iac_range(current) &= ~DBCR_IAC34MODE;
		do_send_trap(regs, mfspr(SPRN_IAC3), debug_status, TRAP_HWBKPT,
			     3);
		changed |= 0x01;
	}  else if (debug_status & DBSR_IAC4) {
1622
		current->thread.debug.dbcr0 &= ~DBCR0_IAC4;
1623 1624 1625 1626 1627 1628 1629 1630 1631
		do_send_trap(regs, mfspr(SPRN_IAC4), debug_status, TRAP_HWBKPT,
			     4);
		changed |= 0x01;
	}
	/*
	 * At the point this routine was called, the MSR(DE) was turned off.
	 * Check all other debug flags and see if that bit needs to be turned
	 * back on or not.
	 */
1632
	if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
1633
			       current->thread.debug.dbcr1))
1634 1635 1636
		regs->msr |= MSR_DE;
	else
		/* Make sure the IDM flag is off */
1637
		current->thread.debug.dbcr0 &= ~DBCR0_IDM;
1638 1639

	if (changed & 0x01)
1640
		mtspr(SPRN_DBCR0, current->thread.debug.dbcr0);
1641
}
1642

1643
void __kprobes DebugException(struct pt_regs *regs, unsigned long debug_status)
1644
{
1645
	current->thread.debug.dbsr = debug_status;
1646

1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661
	/* Hack alert: On BookE, Branch Taken stops on the branch itself, while
	 * on server, it stops on the target of the branch. In order to simulate
	 * the server behaviour, we thus restart right away with a single step
	 * instead of stopping here when hitting a BT
	 */
	if (debug_status & DBSR_BT) {
		regs->msr &= ~MSR_DE;

		/* Disable BT */
		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
		/* Clear the BT event */
		mtspr(SPRN_DBSR, DBSR_BT);

		/* Do the single step trick only when coming from userspace */
		if (user_mode(regs)) {
1662 1663
			current->thread.debug.dbcr0 &= ~DBCR0_BT;
			current->thread.debug.dbcr0 |= DBCR0_IDM | DBCR0_IC;
1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674
			regs->msr |= MSR_DE;
			return;
		}

		if (notify_die(DIE_SSTEP, "block_step", regs, 5,
			       5, SIGTRAP) == NOTIFY_STOP) {
			return;
		}
		if (debugger_sstep(regs))
			return;
	} else if (debug_status & DBSR_IC) { 	/* Instruction complete */
1675
		regs->msr &= ~MSR_DE;
1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689

		/* Disable instruction completion */
		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
		/* Clear the instruction completion event */
		mtspr(SPRN_DBSR, DBSR_IC);

		if (notify_die(DIE_SSTEP, "single_step", regs, 5,
			       5, SIGTRAP) == NOTIFY_STOP) {
			return;
		}

		if (debugger_sstep(regs))
			return;

1690
		if (user_mode(regs)) {
1691 1692 1693
			current->thread.debug.dbcr0 &= ~DBCR0_IC;
			if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
					       current->thread.debug.dbcr1))
1694 1695 1696
				regs->msr |= MSR_DE;
			else
				/* Make sure the IDM bit is off */
1697
				current->thread.debug.dbcr0 &= ~DBCR0_IDM;
1698
		}
1699 1700 1701 1702

		_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
	} else
		handle_debug(regs, debug_status);
1703
}
1704
#endif /* CONFIG_PPC_ADV_DEBUG_REGS */
1705 1706 1707 1708 1709 1710 1711 1712 1713 1714

#if !defined(CONFIG_TAU_INT)
void TAUException(struct pt_regs *regs)
{
	printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx    %s\n",
	       regs->nip, regs->msr, regs->trap, print_tainted());
}
#endif /* CONFIG_INT_TAU */

#ifdef CONFIG_ALTIVEC
1715
void altivec_assist_exception(struct pt_regs *regs)
1716 1717 1718 1719 1720 1721
{
	int err;

	if (!user_mode(regs)) {
		printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
		       " at %lx\n", regs->nip);
P
Paul Mackerras 已提交
1722
		die("Kernel VMX/Altivec assist exception", regs, SIGILL);
1723 1724
	}

1725 1726
	flush_altivec_to_thread(current);

1727
	PPC_WARN_EMULATED(altivec, regs);
1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740
	err = emulate_altivec(regs);
	if (err == 0) {
		regs->nip += 4;		/* skip emulated instruction */
		emulate_single_step(regs);
		return;
	}

	if (err == -EFAULT) {
		/* got an error reading the instruction */
		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
	} else {
		/* didn't recognize the instruction */
		/* XXX quick hack for now: set the non-Java bit in the VSCR */
1741 1742
		printk_ratelimited(KERN_ERR "Unrecognized altivec instruction "
				   "in %s at %lx\n", current->comm, regs->nip);
1743
		current->thread.vr_state.vscr.u[3] |= 0x10000;
1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764
	}
}
#endif /* CONFIG_ALTIVEC */

#ifdef CONFIG_FSL_BOOKE
void CacheLockingException(struct pt_regs *regs, unsigned long address,
			   unsigned long error_code)
{
	/* We treat cache locking instructions from the user
	 * as priv ops, in the future we could try to do
	 * something smarter
	 */
	if (error_code & (ESR_DLK|ESR_ILK))
		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
	return;
}
#endif /* CONFIG_FSL_BOOKE */

#ifdef CONFIG_SPE
void SPEFloatingPointException(struct pt_regs *regs)
{
1765
	extern int do_spe_mathemu(struct pt_regs *regs);
1766 1767 1768
	unsigned long spefscr;
	int fpexc_mode;
	int code = 0;
1769 1770
	int err;

1771
	flush_spe_to_thread(current);
1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789

	spefscr = current->thread.spefscr;
	fpexc_mode = current->thread.fpexc_mode;

	if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
		code = FPE_FLTOVF;
	}
	else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
		code = FPE_FLTUND;
	}
	else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
		code = FPE_FLTDIV;
	else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
		code = FPE_FLTINV;
	}
	else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
		code = FPE_FLTRES;

1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806
	err = do_spe_mathemu(regs);
	if (err == 0) {
		regs->nip += 4;		/* skip emulated instruction */
		emulate_single_step(regs);
		return;
	}

	if (err == -EFAULT) {
		/* got an error reading the instruction */
		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
	} else if (err == -EINVAL) {
		/* didn't recognize the instruction */
		printk(KERN_ERR "unrecognized spe instruction "
		       "in %s at %lx\n", current->comm, regs->nip);
	} else {
		_exception(SIGFPE, regs, code, regs->nip);
	}
1807 1808 1809

	return;
}
1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840

void SPEFloatingPointRoundException(struct pt_regs *regs)
{
	extern int speround_handler(struct pt_regs *regs);
	int err;

	preempt_disable();
	if (regs->msr & MSR_SPE)
		giveup_spe(current);
	preempt_enable();

	regs->nip -= 4;
	err = speround_handler(regs);
	if (err == 0) {
		regs->nip += 4;		/* skip emulated instruction */
		emulate_single_step(regs);
		return;
	}

	if (err == -EFAULT) {
		/* got an error reading the instruction */
		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
	} else if (err == -EINVAL) {
		/* didn't recognize the instruction */
		printk(KERN_ERR "unrecognized spe instruction "
		       "in %s at %lx\n", current->comm, regs->nip);
	} else {
		_exception(SIGFPE, regs, 0, regs->nip);
		return;
	}
}
1841 1842
#endif

1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855
/*
 * We enter here if we get an unrecoverable exception, that is, one
 * that happened at a point where the RI (recoverable interrupt) bit
 * in the MSR is 0.  This indicates that SRR0/1 are live, and that
 * we therefore lost state by taking this exception.
 */
void unrecoverable_exception(struct pt_regs *regs)
{
	printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
	       regs->trap, regs->nip);
	die("Unrecoverable exception", regs, SIGABRT);
}

1856
#if defined(CONFIG_BOOKE_WDT) || defined(CONFIG_40x)
1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873
/*
 * Default handler for a Watchdog exception,
 * spins until a reboot occurs
 */
void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
{
	/* Generic WatchdogHandler, implement your own */
	mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
	return;
}

void WatchdogException(struct pt_regs *regs)
{
	printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
	WatchdogHandler(regs);
}
#endif
1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884

/*
 * We enter here if we discover during exception entry that we are
 * running in supervisor mode with a userspace value in the stack pointer.
 */
void kernel_bad_stack(struct pt_regs *regs)
{
	printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
	       regs->gpr[1], regs->nip);
	die("Bad kernel stack pointer", regs, SIGABRT);
}
1885 1886 1887 1888

void __init trap_init(void)
{
}
1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908


#ifdef CONFIG_PPC_EMULATED_STATS

#define WARN_EMULATED_SETUP(type)	.type = { .name = #type }

struct ppc_emulated ppc_emulated = {
#ifdef CONFIG_ALTIVEC
	WARN_EMULATED_SETUP(altivec),
#endif
	WARN_EMULATED_SETUP(dcba),
	WARN_EMULATED_SETUP(dcbz),
	WARN_EMULATED_SETUP(fp_pair),
	WARN_EMULATED_SETUP(isel),
	WARN_EMULATED_SETUP(mcrxr),
	WARN_EMULATED_SETUP(mfpvr),
	WARN_EMULATED_SETUP(multiple),
	WARN_EMULATED_SETUP(popcntb),
	WARN_EMULATED_SETUP(spe),
	WARN_EMULATED_SETUP(string),
1909
	WARN_EMULATED_SETUP(sync),
1910 1911 1912 1913 1914 1915 1916
	WARN_EMULATED_SETUP(unaligned),
#ifdef CONFIG_MATH_EMULATION
	WARN_EMULATED_SETUP(math),
#endif
#ifdef CONFIG_VSX
	WARN_EMULATED_SETUP(vsx),
#endif
1917 1918 1919
#ifdef CONFIG_PPC64
	WARN_EMULATED_SETUP(mfdscr),
	WARN_EMULATED_SETUP(mtdscr),
A
Anton Blanchard 已提交
1920
	WARN_EMULATED_SETUP(lq_stq),
1921
#endif
1922 1923 1924 1925 1926 1927
};

u32 ppc_warn_emulated;

void ppc_warn_emulated_print(const char *type)
{
1928 1929
	pr_warn_ratelimited("%s used emulated %s instruction\n", current->comm,
			    type);
1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967
}

static int __init ppc_warn_emulated_init(void)
{
	struct dentry *dir, *d;
	unsigned int i;
	struct ppc_emulated_entry *entries = (void *)&ppc_emulated;

	if (!powerpc_debugfs_root)
		return -ENODEV;

	dir = debugfs_create_dir("emulated_instructions",
				 powerpc_debugfs_root);
	if (!dir)
		return -ENOMEM;

	d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
			       &ppc_warn_emulated);
	if (!d)
		goto fail;

	for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
		d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
				       (u32 *)&entries[i].val.counter);
		if (!d)
			goto fail;
	}

	return 0;

fail:
	debugfs_remove_recursive(dir);
	return -ENOMEM;
}

device_initcall(ppc_warn_emulated_init);

#endif /* CONFIG_PPC_EMULATED_STATS */