xref: /openbmc/linux/arch/powerpc/kernel/traps.c (revision 25baa35befeebe6a4a8d8d12a4fc5b95918bda54)
114cf11afSPaul Mackerras /*
214cf11afSPaul Mackerras  *  Copyright (C) 1995-1996  Gary Thomas (gdt@linuxppc.org)
314cf11afSPaul Mackerras  *
414cf11afSPaul Mackerras  *  This program is free software; you can redistribute it and/or
514cf11afSPaul Mackerras  *  modify it under the terms of the GNU General Public License
614cf11afSPaul Mackerras  *  as published by the Free Software Foundation; either version
714cf11afSPaul Mackerras  *  2 of the License, or (at your option) any later version.
814cf11afSPaul Mackerras  *
914cf11afSPaul Mackerras  *  Modified by Cort Dougan (cort@cs.nmt.edu)
1014cf11afSPaul Mackerras  *  and Paul Mackerras (paulus@samba.org)
1114cf11afSPaul Mackerras  */
1214cf11afSPaul Mackerras 
1314cf11afSPaul Mackerras /*
1414cf11afSPaul Mackerras  * This file handles the architecture-dependent parts of hardware exceptions
1514cf11afSPaul Mackerras  */
1614cf11afSPaul Mackerras 
1714cf11afSPaul Mackerras #include <linux/errno.h>
1814cf11afSPaul Mackerras #include <linux/sched.h>
1914cf11afSPaul Mackerras #include <linux/kernel.h>
2014cf11afSPaul Mackerras #include <linux/mm.h>
2114cf11afSPaul Mackerras #include <linux/stddef.h>
2214cf11afSPaul Mackerras #include <linux/unistd.h>
238dad3f92SPaul Mackerras #include <linux/ptrace.h>
2414cf11afSPaul Mackerras #include <linux/slab.h>
2514cf11afSPaul Mackerras #include <linux/user.h>
2614cf11afSPaul Mackerras #include <linux/interrupt.h>
2714cf11afSPaul Mackerras #include <linux/init.h>
2814cf11afSPaul Mackerras #include <linux/module.h>
298dad3f92SPaul Mackerras #include <linux/prctl.h>
3014cf11afSPaul Mackerras #include <linux/delay.h>
3114cf11afSPaul Mackerras #include <linux/kprobes.h>
32cc532915SMichael Ellerman #include <linux/kexec.h>
335474c120SMichael Hanselmann #include <linux/backlight.h>
3473c9ceabSJeremy Fitzhardinge #include <linux/bug.h>
351eeb66a1SChristoph Hellwig #include <linux/kdebug.h>
3680947e7cSGeert Uytterhoeven #include <linux/debugfs.h>
3714cf11afSPaul Mackerras 
3880947e7cSGeert Uytterhoeven #include <asm/emulated_ops.h>
3914cf11afSPaul Mackerras #include <asm/pgtable.h>
4014cf11afSPaul Mackerras #include <asm/uaccess.h>
4114cf11afSPaul Mackerras #include <asm/system.h>
4214cf11afSPaul Mackerras #include <asm/io.h>
4386417780SPaul Mackerras #include <asm/machdep.h>
4486417780SPaul Mackerras #include <asm/rtas.h>
45f7f6f4feSDavid Gibson #include <asm/pmc.h>
46dc1c1ca3SStephen Rothwell #ifdef CONFIG_PPC32
4714cf11afSPaul Mackerras #include <asm/reg.h>
4886417780SPaul Mackerras #endif
4914cf11afSPaul Mackerras #ifdef CONFIG_PMAC_BACKLIGHT
5014cf11afSPaul Mackerras #include <asm/backlight.h>
5114cf11afSPaul Mackerras #endif
52dc1c1ca3SStephen Rothwell #ifdef CONFIG_PPC64
5386417780SPaul Mackerras #include <asm/firmware.h>
54dc1c1ca3SStephen Rothwell #include <asm/processor.h>
55dc1c1ca3SStephen Rothwell #endif
56c0ce7d08SDavid Wilder #include <asm/kexec.h>
5716c57b36SKumar Gala #include <asm/ppc-opcode.h>
58620165f9SKumar Gala #ifdef CONFIG_FSL_BOOKE
59620165f9SKumar Gala #include <asm/dbell.h>
60620165f9SKumar Gala #endif
61dc1c1ca3SStephen Rothwell 
627dbb922cSOlof Johansson #if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
6314cf11afSPaul Mackerras int (*__debugger)(struct pt_regs *regs);
6414cf11afSPaul Mackerras int (*__debugger_ipi)(struct pt_regs *regs);
6514cf11afSPaul Mackerras int (*__debugger_bpt)(struct pt_regs *regs);
6614cf11afSPaul Mackerras int (*__debugger_sstep)(struct pt_regs *regs);
6714cf11afSPaul Mackerras int (*__debugger_iabr_match)(struct pt_regs *regs);
6814cf11afSPaul Mackerras int (*__debugger_dabr_match)(struct pt_regs *regs);
6914cf11afSPaul Mackerras int (*__debugger_fault_handler)(struct pt_regs *regs);
7014cf11afSPaul Mackerras 
7114cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger);
7214cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_ipi);
7314cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_bpt);
7414cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_sstep);
7514cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_iabr_match);
7614cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_dabr_match);
7714cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_fault_handler);
7814cf11afSPaul Mackerras #endif
7914cf11afSPaul Mackerras 
8014cf11afSPaul Mackerras /*
8114cf11afSPaul Mackerras  * Trap & Exception support
8214cf11afSPaul Mackerras  */
8314cf11afSPaul Mackerras 
846031d9d9Santon@samba.org #ifdef CONFIG_PMAC_BACKLIGHT
856031d9d9Santon@samba.org static void pmac_backlight_unblank(void)
866031d9d9Santon@samba.org {
876031d9d9Santon@samba.org 	mutex_lock(&pmac_backlight_mutex);
886031d9d9Santon@samba.org 	if (pmac_backlight) {
896031d9d9Santon@samba.org 		struct backlight_properties *props;
906031d9d9Santon@samba.org 
916031d9d9Santon@samba.org 		props = &pmac_backlight->props;
926031d9d9Santon@samba.org 		props->brightness = props->max_brightness;
936031d9d9Santon@samba.org 		props->power = FB_BLANK_UNBLANK;
946031d9d9Santon@samba.org 		backlight_update_status(pmac_backlight);
956031d9d9Santon@samba.org 	}
966031d9d9Santon@samba.org 	mutex_unlock(&pmac_backlight_mutex);
976031d9d9Santon@samba.org }
986031d9d9Santon@samba.org #else
996031d9d9Santon@samba.org static inline void pmac_backlight_unblank(void) { }
1006031d9d9Santon@samba.org #endif
1016031d9d9Santon@samba.org 
10214cf11afSPaul Mackerras int die(const char *str, struct pt_regs *regs, long err)
10314cf11afSPaul Mackerras {
10434c2a14fSanton@samba.org 	static struct {
10534c2a14fSanton@samba.org 		spinlock_t lock;
10634c2a14fSanton@samba.org 		u32 lock_owner;
10734c2a14fSanton@samba.org 		int lock_owner_depth;
10834c2a14fSanton@samba.org 	} die = {
10934c2a14fSanton@samba.org 		.lock =			__SPIN_LOCK_UNLOCKED(die.lock),
11034c2a14fSanton@samba.org 		.lock_owner =		-1,
11134c2a14fSanton@samba.org 		.lock_owner_depth =	0
11234c2a14fSanton@samba.org 	};
113c0ce7d08SDavid Wilder 	static int die_counter;
11434c2a14fSanton@samba.org 	unsigned long flags;
11514cf11afSPaul Mackerras 
11614cf11afSPaul Mackerras 	if (debugger(regs))
11714cf11afSPaul Mackerras 		return 1;
11814cf11afSPaul Mackerras 
119293e4688Santon@samba.org 	oops_enter();
120293e4688Santon@samba.org 
12134c2a14fSanton@samba.org 	if (die.lock_owner != raw_smp_processor_id()) {
12214cf11afSPaul Mackerras 		console_verbose();
12334c2a14fSanton@samba.org 		spin_lock_irqsave(&die.lock, flags);
12434c2a14fSanton@samba.org 		die.lock_owner = smp_processor_id();
12534c2a14fSanton@samba.org 		die.lock_owner_depth = 0;
12614cf11afSPaul Mackerras 		bust_spinlocks(1);
1276031d9d9Santon@samba.org 		if (machine_is(powermac))
1286031d9d9Santon@samba.org 			pmac_backlight_unblank();
12934c2a14fSanton@samba.org 	} else {
13034c2a14fSanton@samba.org 		local_save_flags(flags);
13134c2a14fSanton@samba.org 	}
1325474c120SMichael Hanselmann 
13334c2a14fSanton@samba.org 	if (++die.lock_owner_depth < 3) {
13414cf11afSPaul Mackerras 		printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
13514cf11afSPaul Mackerras #ifdef CONFIG_PREEMPT
13614cf11afSPaul Mackerras 		printk("PREEMPT ");
13714cf11afSPaul Mackerras #endif
13814cf11afSPaul Mackerras #ifdef CONFIG_SMP
13914cf11afSPaul Mackerras 		printk("SMP NR_CPUS=%d ", NR_CPUS);
14014cf11afSPaul Mackerras #endif
14114cf11afSPaul Mackerras #ifdef CONFIG_DEBUG_PAGEALLOC
14214cf11afSPaul Mackerras 		printk("DEBUG_PAGEALLOC ");
14314cf11afSPaul Mackerras #endif
14414cf11afSPaul Mackerras #ifdef CONFIG_NUMA
14514cf11afSPaul Mackerras 		printk("NUMA ");
14614cf11afSPaul Mackerras #endif
147ae7f4463Santon@samba.org 		printk("%s\n", ppc_md.name ? ppc_md.name : "");
148e8222502SBenjamin Herrenschmidt 
14914cf11afSPaul Mackerras 		print_modules();
15014cf11afSPaul Mackerras 		show_regs(regs);
15134c2a14fSanton@samba.org 	} else {
15234c2a14fSanton@samba.org 		printk("Recursive die() failure, output suppressed\n");
15334c2a14fSanton@samba.org 	}
15434c2a14fSanton@samba.org 
15514cf11afSPaul Mackerras 	bust_spinlocks(0);
15634c2a14fSanton@samba.org 	die.lock_owner = -1;
157bcdcd8e7SPavel Emelianov 	add_taint(TAINT_DIE);
15834c2a14fSanton@samba.org 	spin_unlock_irqrestore(&die.lock, flags);
159cc532915SMichael Ellerman 
160c0ce7d08SDavid Wilder 	if (kexec_should_crash(current) ||
161c0ce7d08SDavid Wilder 		kexec_sr_activated(smp_processor_id()))
162cc532915SMichael Ellerman 		crash_kexec(regs);
163c0ce7d08SDavid Wilder 	crash_kexec_secondary(regs);
16414cf11afSPaul Mackerras 
16514cf11afSPaul Mackerras 	if (in_interrupt())
16614cf11afSPaul Mackerras 		panic("Fatal exception in interrupt");
16714cf11afSPaul Mackerras 
168cea6a4baSHorms 	if (panic_on_oops)
169012c437dSHorms 		panic("Fatal exception");
170cea6a4baSHorms 
171293e4688Santon@samba.org 	oops_exit();
17214cf11afSPaul Mackerras 	do_exit(err);
17314cf11afSPaul Mackerras 
17414cf11afSPaul Mackerras 	return 0;
17514cf11afSPaul Mackerras }
17614cf11afSPaul Mackerras 
177*25baa35bSOleg Nesterov void user_single_step_siginfo(struct task_struct *tsk,
178*25baa35bSOleg Nesterov 				struct pt_regs *regs, siginfo_t *info)
179*25baa35bSOleg Nesterov {
180*25baa35bSOleg Nesterov 	memset(info, 0, sizeof(*info));
181*25baa35bSOleg Nesterov 	info->si_signo = SIGTRAP;
182*25baa35bSOleg Nesterov 	info->si_code = TRAP_TRACE;
183*25baa35bSOleg Nesterov 	info->si_addr = (void __user *)regs->nip;
184*25baa35bSOleg Nesterov }
185*25baa35bSOleg Nesterov 
18614cf11afSPaul Mackerras void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
18714cf11afSPaul Mackerras {
18814cf11afSPaul Mackerras 	siginfo_t info;
189d0c3d534SOlof Johansson 	const char fmt32[] = KERN_INFO "%s[%d]: unhandled signal %d " \
190d0c3d534SOlof Johansson 			"at %08lx nip %08lx lr %08lx code %x\n";
191d0c3d534SOlof Johansson 	const char fmt64[] = KERN_INFO "%s[%d]: unhandled signal %d " \
192d0c3d534SOlof Johansson 			"at %016lx nip %016lx lr %016lx code %x\n";
19314cf11afSPaul Mackerras 
19414cf11afSPaul Mackerras 	if (!user_mode(regs)) {
19514cf11afSPaul Mackerras 		if (die("Exception in kernel mode", regs, signr))
19614cf11afSPaul Mackerras 			return;
197d0c3d534SOlof Johansson 	} else if (show_unhandled_signals &&
198d0c3d534SOlof Johansson 		    unhandled_signal(current, signr) &&
199d0c3d534SOlof Johansson 		    printk_ratelimit()) {
200d0c3d534SOlof Johansson 			printk(regs->msr & MSR_SF ? fmt64 : fmt32,
201d0c3d534SOlof Johansson 				current->comm, current->pid, signr,
202d0c3d534SOlof Johansson 				addr, regs->nip, regs->link, code);
20314cf11afSPaul Mackerras 		}
20414cf11afSPaul Mackerras 
20514cf11afSPaul Mackerras 	memset(&info, 0, sizeof(info));
20614cf11afSPaul Mackerras 	info.si_signo = signr;
20714cf11afSPaul Mackerras 	info.si_code = code;
20814cf11afSPaul Mackerras 	info.si_addr = (void __user *) addr;
20914cf11afSPaul Mackerras 	force_sig_info(signr, &info, current);
21014cf11afSPaul Mackerras }
21114cf11afSPaul Mackerras 
21214cf11afSPaul Mackerras #ifdef CONFIG_PPC64
21314cf11afSPaul Mackerras void system_reset_exception(struct pt_regs *regs)
21414cf11afSPaul Mackerras {
21514cf11afSPaul Mackerras 	/* See if any machine dependent calls */
216c902be71SArnd Bergmann 	if (ppc_md.system_reset_exception) {
217c902be71SArnd Bergmann 		if (ppc_md.system_reset_exception(regs))
218c902be71SArnd Bergmann 			return;
219c902be71SArnd Bergmann 	}
22014cf11afSPaul Mackerras 
221c0ce7d08SDavid Wilder #ifdef CONFIG_KEXEC
222c0ce7d08SDavid Wilder 	cpu_set(smp_processor_id(), cpus_in_sr);
223c0ce7d08SDavid Wilder #endif
224c0ce7d08SDavid Wilder 
2258dad3f92SPaul Mackerras 	die("System Reset", regs, SIGABRT);
22614cf11afSPaul Mackerras 
227eac8392fSDavid Wilder 	/*
228eac8392fSDavid Wilder 	 * Some CPUs when released from the debugger will execute this path.
229eac8392fSDavid Wilder 	 * These CPUs entered the debugger via a soft-reset. If the CPU was
230eac8392fSDavid Wilder 	 * hung before entering the debugger it will return to the hung
231eac8392fSDavid Wilder 	 * state when exiting this function.  This causes a problem in
232eac8392fSDavid Wilder 	 * kdump since the hung CPU(s) will not respond to the IPI sent
233eac8392fSDavid Wilder 	 * from kdump. To prevent the problem we call crash_kexec_secondary()
234eac8392fSDavid Wilder 	 * here. If a kdump had not been initiated or we exit the debugger
235eac8392fSDavid Wilder 	 * with the "exit and recover" command (x) crash_kexec_secondary()
236eac8392fSDavid Wilder 	 * will return after 5ms and the CPU returns to its previous state.
237eac8392fSDavid Wilder 	 */
238eac8392fSDavid Wilder 	crash_kexec_secondary(regs);
239eac8392fSDavid Wilder 
24014cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
24114cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
24214cf11afSPaul Mackerras 		panic("Unrecoverable System Reset");
24314cf11afSPaul Mackerras 
24414cf11afSPaul Mackerras 	/* What should we do here? We could issue a shutdown or hard reset. */
24514cf11afSPaul Mackerras }
24614cf11afSPaul Mackerras #endif
24714cf11afSPaul Mackerras 
24814cf11afSPaul Mackerras /*
24914cf11afSPaul Mackerras  * I/O accesses can cause machine checks on powermacs.
25014cf11afSPaul Mackerras  * Check if the NIP corresponds to the address of a sync
25114cf11afSPaul Mackerras  * instruction for which there is an entry in the exception
25214cf11afSPaul Mackerras  * table.
25314cf11afSPaul Mackerras  * Note that the 601 only takes a machine check on TEA
25414cf11afSPaul Mackerras  * (transfer error ack) signal assertion, and does not
25514cf11afSPaul Mackerras  * set any of the top 16 bits of SRR1.
25614cf11afSPaul Mackerras  *  -- paulus.
25714cf11afSPaul Mackerras  */
25814cf11afSPaul Mackerras static inline int check_io_access(struct pt_regs *regs)
25914cf11afSPaul Mackerras {
26068a64357SBenjamin Herrenschmidt #ifdef CONFIG_PPC32
26114cf11afSPaul Mackerras 	unsigned long msr = regs->msr;
26214cf11afSPaul Mackerras 	const struct exception_table_entry *entry;
26314cf11afSPaul Mackerras 	unsigned int *nip = (unsigned int *)regs->nip;
26414cf11afSPaul Mackerras 
26514cf11afSPaul Mackerras 	if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
26614cf11afSPaul Mackerras 	    && (entry = search_exception_tables(regs->nip)) != NULL) {
26714cf11afSPaul Mackerras 		/*
26814cf11afSPaul Mackerras 		 * Check that it's a sync instruction, or somewhere
26914cf11afSPaul Mackerras 		 * in the twi; isync; nop sequence that inb/inw/inl uses.
27014cf11afSPaul Mackerras 		 * As the address is in the exception table
27114cf11afSPaul Mackerras 		 * we should be able to read the instr there.
27214cf11afSPaul Mackerras 		 * For the debug message, we look at the preceding
27314cf11afSPaul Mackerras 		 * load or store.
27414cf11afSPaul Mackerras 		 */
27514cf11afSPaul Mackerras 		if (*nip == 0x60000000)		/* nop */
27614cf11afSPaul Mackerras 			nip -= 2;
27714cf11afSPaul Mackerras 		else if (*nip == 0x4c00012c)	/* isync */
27814cf11afSPaul Mackerras 			--nip;
27914cf11afSPaul Mackerras 		if (*nip == 0x7c0004ac || (*nip >> 26) == 3) {
28014cf11afSPaul Mackerras 			/* sync or twi */
28114cf11afSPaul Mackerras 			unsigned int rb;
28214cf11afSPaul Mackerras 
28314cf11afSPaul Mackerras 			--nip;
28414cf11afSPaul Mackerras 			rb = (*nip >> 11) & 0x1f;
28514cf11afSPaul Mackerras 			printk(KERN_DEBUG "%s bad port %lx at %p\n",
28614cf11afSPaul Mackerras 			       (*nip & 0x100)? "OUT to": "IN from",
28714cf11afSPaul Mackerras 			       regs->gpr[rb] - _IO_BASE, nip);
28814cf11afSPaul Mackerras 			regs->msr |= MSR_RI;
28914cf11afSPaul Mackerras 			regs->nip = entry->fixup;
29014cf11afSPaul Mackerras 			return 1;
29114cf11afSPaul Mackerras 		}
29214cf11afSPaul Mackerras 	}
29368a64357SBenjamin Herrenschmidt #endif /* CONFIG_PPC32 */
29414cf11afSPaul Mackerras 	return 0;
29514cf11afSPaul Mackerras }
29614cf11afSPaul Mackerras 
29714cf11afSPaul Mackerras #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
29814cf11afSPaul Mackerras /* On 4xx, the reason for the machine check or program exception
29914cf11afSPaul Mackerras    is in the ESR. */
30014cf11afSPaul Mackerras #define get_reason(regs)	((regs)->dsisr)
30114cf11afSPaul Mackerras #ifndef CONFIG_FSL_BOOKE
30214cf11afSPaul Mackerras #define get_mc_reason(regs)	((regs)->dsisr)
30314cf11afSPaul Mackerras #else
30486d7a9a9SBecky Bruce #define get_mc_reason(regs)	(mfspr(SPRN_MCSR) & MCSR_MASK)
30514cf11afSPaul Mackerras #endif
30614cf11afSPaul Mackerras #define REASON_FP		ESR_FP
30714cf11afSPaul Mackerras #define REASON_ILLEGAL		(ESR_PIL | ESR_PUO)
30814cf11afSPaul Mackerras #define REASON_PRIVILEGED	ESR_PPR
30914cf11afSPaul Mackerras #define REASON_TRAP		ESR_PTR
31014cf11afSPaul Mackerras 
31114cf11afSPaul Mackerras /* single-step stuff */
31214cf11afSPaul Mackerras #define single_stepping(regs)	(current->thread.dbcr0 & DBCR0_IC)
31314cf11afSPaul Mackerras #define clear_single_step(regs)	(current->thread.dbcr0 &= ~DBCR0_IC)
31414cf11afSPaul Mackerras 
31514cf11afSPaul Mackerras #else
31614cf11afSPaul Mackerras /* On non-4xx, the reason for the machine check or program
31714cf11afSPaul Mackerras    exception is in the MSR. */
31814cf11afSPaul Mackerras #define get_reason(regs)	((regs)->msr)
31914cf11afSPaul Mackerras #define get_mc_reason(regs)	((regs)->msr)
32014cf11afSPaul Mackerras #define REASON_FP		0x100000
32114cf11afSPaul Mackerras #define REASON_ILLEGAL		0x80000
32214cf11afSPaul Mackerras #define REASON_PRIVILEGED	0x40000
32314cf11afSPaul Mackerras #define REASON_TRAP		0x20000
32414cf11afSPaul Mackerras 
32514cf11afSPaul Mackerras #define single_stepping(regs)	((regs)->msr & MSR_SE)
32614cf11afSPaul Mackerras #define clear_single_step(regs)	((regs)->msr &= ~MSR_SE)
32714cf11afSPaul Mackerras #endif
32814cf11afSPaul Mackerras 
32947c0bd1aSBenjamin Herrenschmidt #if defined(CONFIG_4xx)
33047c0bd1aSBenjamin Herrenschmidt int machine_check_4xx(struct pt_regs *regs)
33114cf11afSPaul Mackerras {
3321a6a4ffeSKumar Gala 	unsigned long reason = get_mc_reason(regs);
33314cf11afSPaul Mackerras 
33414cf11afSPaul Mackerras 	if (reason & ESR_IMCP) {
33514cf11afSPaul Mackerras 		printk("Instruction");
33614cf11afSPaul Mackerras 		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
33714cf11afSPaul Mackerras 	} else
33814cf11afSPaul Mackerras 		printk("Data");
33914cf11afSPaul Mackerras 	printk(" machine check in kernel mode.\n");
34047c0bd1aSBenjamin Herrenschmidt 
34147c0bd1aSBenjamin Herrenschmidt 	return 0;
34247c0bd1aSBenjamin Herrenschmidt }
34347c0bd1aSBenjamin Herrenschmidt 
34447c0bd1aSBenjamin Herrenschmidt int machine_check_440A(struct pt_regs *regs)
34547c0bd1aSBenjamin Herrenschmidt {
34647c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
34747c0bd1aSBenjamin Herrenschmidt 
34814cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
34914cf11afSPaul Mackerras 	if (reason & ESR_IMCP){
35014cf11afSPaul Mackerras 		printk("Instruction Synchronous Machine Check exception\n");
35114cf11afSPaul Mackerras 		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
35214cf11afSPaul Mackerras 	}
35314cf11afSPaul Mackerras 	else {
35414cf11afSPaul Mackerras 		u32 mcsr = mfspr(SPRN_MCSR);
35514cf11afSPaul Mackerras 		if (mcsr & MCSR_IB)
35614cf11afSPaul Mackerras 			printk("Instruction Read PLB Error\n");
35714cf11afSPaul Mackerras 		if (mcsr & MCSR_DRB)
35814cf11afSPaul Mackerras 			printk("Data Read PLB Error\n");
35914cf11afSPaul Mackerras 		if (mcsr & MCSR_DWB)
36014cf11afSPaul Mackerras 			printk("Data Write PLB Error\n");
36114cf11afSPaul Mackerras 		if (mcsr & MCSR_TLBP)
36214cf11afSPaul Mackerras 			printk("TLB Parity Error\n");
36314cf11afSPaul Mackerras 		if (mcsr & MCSR_ICP){
36414cf11afSPaul Mackerras 			flush_instruction_cache();
36514cf11afSPaul Mackerras 			printk("I-Cache Parity Error\n");
36614cf11afSPaul Mackerras 		}
36714cf11afSPaul Mackerras 		if (mcsr & MCSR_DCSP)
36814cf11afSPaul Mackerras 			printk("D-Cache Search Parity Error\n");
36914cf11afSPaul Mackerras 		if (mcsr & MCSR_DCFP)
37014cf11afSPaul Mackerras 			printk("D-Cache Flush Parity Error\n");
37114cf11afSPaul Mackerras 		if (mcsr & MCSR_IMPE)
37214cf11afSPaul Mackerras 			printk("Machine Check exception is imprecise\n");
37314cf11afSPaul Mackerras 
37414cf11afSPaul Mackerras 		/* Clear MCSR */
37514cf11afSPaul Mackerras 		mtspr(SPRN_MCSR, mcsr);
37614cf11afSPaul Mackerras 	}
37747c0bd1aSBenjamin Herrenschmidt 	return 0;
37847c0bd1aSBenjamin Herrenschmidt }
37914cf11afSPaul Mackerras #elif defined(CONFIG_E500)
38047c0bd1aSBenjamin Herrenschmidt int machine_check_e500(struct pt_regs *regs)
38147c0bd1aSBenjamin Herrenschmidt {
38247c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
38347c0bd1aSBenjamin Herrenschmidt 
38414cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
38514cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
38614cf11afSPaul Mackerras 
38714cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
38814cf11afSPaul Mackerras 		printk("Machine Check Signal\n");
38914cf11afSPaul Mackerras 	if (reason & MCSR_ICPERR)
39014cf11afSPaul Mackerras 		printk("Instruction Cache Parity Error\n");
39114cf11afSPaul Mackerras 	if (reason & MCSR_DCP_PERR)
39214cf11afSPaul Mackerras 		printk("Data Cache Push Parity Error\n");
39314cf11afSPaul Mackerras 	if (reason & MCSR_DCPERR)
39414cf11afSPaul Mackerras 		printk("Data Cache Parity Error\n");
39514cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IAERR)
39614cf11afSPaul Mackerras 		printk("Bus - Instruction Address Error\n");
39714cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RAERR)
39814cf11afSPaul Mackerras 		printk("Bus - Read Address Error\n");
39914cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WAERR)
40014cf11afSPaul Mackerras 		printk("Bus - Write Address Error\n");
40114cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IBERR)
40214cf11afSPaul Mackerras 		printk("Bus - Instruction Data Error\n");
40314cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RBERR)
40414cf11afSPaul Mackerras 		printk("Bus - Read Data Bus Error\n");
40514cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WBERR)
40614cf11afSPaul Mackerras 		printk("Bus - Read Data Bus Error\n");
40714cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IPERR)
40814cf11afSPaul Mackerras 		printk("Bus - Instruction Parity Error\n");
40914cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RPERR)
41014cf11afSPaul Mackerras 		printk("Bus - Read Parity Error\n");
41147c0bd1aSBenjamin Herrenschmidt 
41247c0bd1aSBenjamin Herrenschmidt 	return 0;
41347c0bd1aSBenjamin Herrenschmidt }
41414cf11afSPaul Mackerras #elif defined(CONFIG_E200)
41547c0bd1aSBenjamin Herrenschmidt int machine_check_e200(struct pt_regs *regs)
41647c0bd1aSBenjamin Herrenschmidt {
41747c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
41847c0bd1aSBenjamin Herrenschmidt 
41914cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
42014cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
42114cf11afSPaul Mackerras 
42214cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
42314cf11afSPaul Mackerras 		printk("Machine Check Signal\n");
42414cf11afSPaul Mackerras 	if (reason & MCSR_CP_PERR)
42514cf11afSPaul Mackerras 		printk("Cache Push Parity Error\n");
42614cf11afSPaul Mackerras 	if (reason & MCSR_CPERR)
42714cf11afSPaul Mackerras 		printk("Cache Parity Error\n");
42814cf11afSPaul Mackerras 	if (reason & MCSR_EXCP_ERR)
42914cf11afSPaul Mackerras 		printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
43014cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IRERR)
43114cf11afSPaul Mackerras 		printk("Bus - Read Bus Error on instruction fetch\n");
43214cf11afSPaul Mackerras 	if (reason & MCSR_BUS_DRERR)
43314cf11afSPaul Mackerras 		printk("Bus - Read Bus Error on data load\n");
43414cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WRERR)
43514cf11afSPaul Mackerras 		printk("Bus - Write Bus Error on buffered store or cache line push\n");
43647c0bd1aSBenjamin Herrenschmidt 
43747c0bd1aSBenjamin Herrenschmidt 	return 0;
43847c0bd1aSBenjamin Herrenschmidt }
43947c0bd1aSBenjamin Herrenschmidt #else
44047c0bd1aSBenjamin Herrenschmidt int machine_check_generic(struct pt_regs *regs)
44147c0bd1aSBenjamin Herrenschmidt {
44247c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
44347c0bd1aSBenjamin Herrenschmidt 
44414cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
44514cf11afSPaul Mackerras 	printk("Caused by (from SRR1=%lx): ", reason);
44614cf11afSPaul Mackerras 	switch (reason & 0x601F0000) {
44714cf11afSPaul Mackerras 	case 0x80000:
44814cf11afSPaul Mackerras 		printk("Machine check signal\n");
44914cf11afSPaul Mackerras 		break;
45014cf11afSPaul Mackerras 	case 0:		/* for 601 */
45114cf11afSPaul Mackerras 	case 0x40000:
45214cf11afSPaul Mackerras 	case 0x140000:	/* 7450 MSS error and TEA */
45314cf11afSPaul Mackerras 		printk("Transfer error ack signal\n");
45414cf11afSPaul Mackerras 		break;
45514cf11afSPaul Mackerras 	case 0x20000:
45614cf11afSPaul Mackerras 		printk("Data parity error signal\n");
45714cf11afSPaul Mackerras 		break;
45814cf11afSPaul Mackerras 	case 0x10000:
45914cf11afSPaul Mackerras 		printk("Address parity error signal\n");
46014cf11afSPaul Mackerras 		break;
46114cf11afSPaul Mackerras 	case 0x20000000:
46214cf11afSPaul Mackerras 		printk("L1 Data Cache error\n");
46314cf11afSPaul Mackerras 		break;
46414cf11afSPaul Mackerras 	case 0x40000000:
46514cf11afSPaul Mackerras 		printk("L1 Instruction Cache error\n");
46614cf11afSPaul Mackerras 		break;
46714cf11afSPaul Mackerras 	case 0x00100000:
46814cf11afSPaul Mackerras 		printk("L2 data cache parity error\n");
46914cf11afSPaul Mackerras 		break;
47014cf11afSPaul Mackerras 	default:
47114cf11afSPaul Mackerras 		printk("Unknown values in msr\n");
47214cf11afSPaul Mackerras 	}
47375918a4bSOlof Johansson 	return 0;
47475918a4bSOlof Johansson }
47547c0bd1aSBenjamin Herrenschmidt #endif /* everything else */
47675918a4bSOlof Johansson 
47775918a4bSOlof Johansson void machine_check_exception(struct pt_regs *regs)
47875918a4bSOlof Johansson {
47975918a4bSOlof Johansson 	int recover = 0;
48075918a4bSOlof Johansson 
48147c0bd1aSBenjamin Herrenschmidt 	/* See if any machine dependent calls. In theory, we would want
48247c0bd1aSBenjamin Herrenschmidt 	 * to call the CPU first, and call the ppc_md. one if the CPU
48347c0bd1aSBenjamin Herrenschmidt 	 * one returns a positive number. However there is existing code
48447c0bd1aSBenjamin Herrenschmidt 	 * that assumes the board gets a first chance, so let's keep it
48547c0bd1aSBenjamin Herrenschmidt 	 * that way for now and fix things later. --BenH.
48647c0bd1aSBenjamin Herrenschmidt 	 */
48775918a4bSOlof Johansson 	if (ppc_md.machine_check_exception)
48875918a4bSOlof Johansson 		recover = ppc_md.machine_check_exception(regs);
48947c0bd1aSBenjamin Herrenschmidt 	else if (cur_cpu_spec->machine_check)
49047c0bd1aSBenjamin Herrenschmidt 		recover = cur_cpu_spec->machine_check(regs);
49175918a4bSOlof Johansson 
49247c0bd1aSBenjamin Herrenschmidt 	if (recover > 0)
49375918a4bSOlof Johansson 		return;
49475918a4bSOlof Johansson 
49575918a4bSOlof Johansson 	if (user_mode(regs)) {
49675918a4bSOlof Johansson 		regs->msr |= MSR_RI;
49775918a4bSOlof Johansson 		_exception(SIGBUS, regs, BUS_ADRERR, regs->nip);
49875918a4bSOlof Johansson 		return;
49975918a4bSOlof Johansson 	}
50075918a4bSOlof Johansson 
50175918a4bSOlof Johansson #if defined(CONFIG_8xx) && defined(CONFIG_PCI)
50247c0bd1aSBenjamin Herrenschmidt 	/* the qspan pci read routines can cause machine checks -- Cort
50347c0bd1aSBenjamin Herrenschmidt 	 *
50447c0bd1aSBenjamin Herrenschmidt 	 * yuck !!! that totally needs to go away ! There are better ways
50547c0bd1aSBenjamin Herrenschmidt 	 * to deal with that than having a wart in the mcheck handler.
50647c0bd1aSBenjamin Herrenschmidt 	 * -- BenH
50747c0bd1aSBenjamin Herrenschmidt 	 */
50875918a4bSOlof Johansson 	bad_page_fault(regs, regs->dar, SIGBUS);
50975918a4bSOlof Johansson 	return;
51075918a4bSOlof Johansson #endif
51175918a4bSOlof Johansson 
51275918a4bSOlof Johansson 	if (debugger_fault_handler(regs)) {
51375918a4bSOlof Johansson 		regs->msr |= MSR_RI;
51475918a4bSOlof Johansson 		return;
51575918a4bSOlof Johansson 	}
51675918a4bSOlof Johansson 
51775918a4bSOlof Johansson 	if (check_io_access(regs))
51875918a4bSOlof Johansson 		return;
51975918a4bSOlof Johansson 
52014cf11afSPaul Mackerras 	if (debugger_fault_handler(regs))
52114cf11afSPaul Mackerras 		return;
5228dad3f92SPaul Mackerras 	die("Machine check", regs, SIGBUS);
52314cf11afSPaul Mackerras 
52414cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
52514cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
52614cf11afSPaul Mackerras 		panic("Unrecoverable Machine check");
52714cf11afSPaul Mackerras }
52814cf11afSPaul Mackerras 
52914cf11afSPaul Mackerras void SMIException(struct pt_regs *regs)
53014cf11afSPaul Mackerras {
53114cf11afSPaul Mackerras 	die("System Management Interrupt", regs, SIGABRT);
53214cf11afSPaul Mackerras }
53314cf11afSPaul Mackerras 
534dc1c1ca3SStephen Rothwell void unknown_exception(struct pt_regs *regs)
53514cf11afSPaul Mackerras {
53614cf11afSPaul Mackerras 	printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
53714cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap);
53814cf11afSPaul Mackerras 
53914cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, 0, 0);
54014cf11afSPaul Mackerras }
54114cf11afSPaul Mackerras 
542dc1c1ca3SStephen Rothwell void instruction_breakpoint_exception(struct pt_regs *regs)
54314cf11afSPaul Mackerras {
54414cf11afSPaul Mackerras 	if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
54514cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
54614cf11afSPaul Mackerras 		return;
54714cf11afSPaul Mackerras 	if (debugger_iabr_match(regs))
54814cf11afSPaul Mackerras 		return;
54914cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
55014cf11afSPaul Mackerras }
55114cf11afSPaul Mackerras 
55214cf11afSPaul Mackerras void RunModeException(struct pt_regs *regs)
55314cf11afSPaul Mackerras {
55414cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, 0, 0);
55514cf11afSPaul Mackerras }
55614cf11afSPaul Mackerras 
5578dad3f92SPaul Mackerras void __kprobes single_step_exception(struct pt_regs *regs)
55814cf11afSPaul Mackerras {
55914cf11afSPaul Mackerras 	regs->msr &= ~(MSR_SE | MSR_BE);  /* Turn off 'trace' bits */
56014cf11afSPaul Mackerras 
56114cf11afSPaul Mackerras 	if (notify_die(DIE_SSTEP, "single_step", regs, 5,
56214cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
56314cf11afSPaul Mackerras 		return;
56414cf11afSPaul Mackerras 	if (debugger_sstep(regs))
56514cf11afSPaul Mackerras 		return;
56614cf11afSPaul Mackerras 
56714cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
56814cf11afSPaul Mackerras }
56914cf11afSPaul Mackerras 
57014cf11afSPaul Mackerras /*
57114cf11afSPaul Mackerras  * After we have successfully emulated an instruction, we have to
57214cf11afSPaul Mackerras  * check if the instruction was being single-stepped, and if so,
57314cf11afSPaul Mackerras  * pretend we got a single-step exception.  This was pointed out
57414cf11afSPaul Mackerras  * by Kumar Gala.  -- paulus
57514cf11afSPaul Mackerras  */
5768dad3f92SPaul Mackerras static void emulate_single_step(struct pt_regs *regs)
57714cf11afSPaul Mackerras {
57814cf11afSPaul Mackerras 	if (single_stepping(regs)) {
57914cf11afSPaul Mackerras 		clear_single_step(regs);
58014cf11afSPaul Mackerras 		_exception(SIGTRAP, regs, TRAP_TRACE, 0);
58114cf11afSPaul Mackerras 	}
58214cf11afSPaul Mackerras }
58314cf11afSPaul Mackerras 
5845fad293bSKumar Gala static inline int __parse_fpscr(unsigned long fpscr)
585dc1c1ca3SStephen Rothwell {
5865fad293bSKumar Gala 	int ret = 0;
587dc1c1ca3SStephen Rothwell 
588dc1c1ca3SStephen Rothwell 	/* Invalid operation */
589dc1c1ca3SStephen Rothwell 	if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
5905fad293bSKumar Gala 		ret = FPE_FLTINV;
591dc1c1ca3SStephen Rothwell 
592dc1c1ca3SStephen Rothwell 	/* Overflow */
593dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
5945fad293bSKumar Gala 		ret = FPE_FLTOVF;
595dc1c1ca3SStephen Rothwell 
596dc1c1ca3SStephen Rothwell 	/* Underflow */
597dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
5985fad293bSKumar Gala 		ret = FPE_FLTUND;
599dc1c1ca3SStephen Rothwell 
600dc1c1ca3SStephen Rothwell 	/* Divide by zero */
601dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
6025fad293bSKumar Gala 		ret = FPE_FLTDIV;
603dc1c1ca3SStephen Rothwell 
604dc1c1ca3SStephen Rothwell 	/* Inexact result */
605dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
6065fad293bSKumar Gala 		ret = FPE_FLTRES;
6075fad293bSKumar Gala 
6085fad293bSKumar Gala 	return ret;
6095fad293bSKumar Gala }
6105fad293bSKumar Gala 
6115fad293bSKumar Gala static void parse_fpe(struct pt_regs *regs)
6125fad293bSKumar Gala {
6135fad293bSKumar Gala 	int code = 0;
6145fad293bSKumar Gala 
6155fad293bSKumar Gala 	flush_fp_to_thread(current);
6165fad293bSKumar Gala 
6175fad293bSKumar Gala 	code = __parse_fpscr(current->thread.fpscr.val);
618dc1c1ca3SStephen Rothwell 
619dc1c1ca3SStephen Rothwell 	_exception(SIGFPE, regs, code, regs->nip);
620dc1c1ca3SStephen Rothwell }
621dc1c1ca3SStephen Rothwell 
622dc1c1ca3SStephen Rothwell /*
623dc1c1ca3SStephen Rothwell  * Illegal instruction emulation support.  Originally written to
62414cf11afSPaul Mackerras  * provide the PVR to user applications using the mfspr rd, PVR.
62514cf11afSPaul Mackerras  * Return non-zero if we can't emulate, or -EFAULT if the associated
62614cf11afSPaul Mackerras  * memory access caused an access fault.  Return zero on success.
62714cf11afSPaul Mackerras  *
62814cf11afSPaul Mackerras  * There are a couple of ways to do this, either "decode" the instruction
62914cf11afSPaul Mackerras  * or directly match lots of bits.  In this case, matching lots of
63014cf11afSPaul Mackerras  * bits is faster and easier.
63186417780SPaul Mackerras  *
63214cf11afSPaul Mackerras  */
63314cf11afSPaul Mackerras static int emulate_string_inst(struct pt_regs *regs, u32 instword)
63414cf11afSPaul Mackerras {
63514cf11afSPaul Mackerras 	u8 rT = (instword >> 21) & 0x1f;
63614cf11afSPaul Mackerras 	u8 rA = (instword >> 16) & 0x1f;
63714cf11afSPaul Mackerras 	u8 NB_RB = (instword >> 11) & 0x1f;
63814cf11afSPaul Mackerras 	u32 num_bytes;
63914cf11afSPaul Mackerras 	unsigned long EA;
64014cf11afSPaul Mackerras 	int pos = 0;
64114cf11afSPaul Mackerras 
64214cf11afSPaul Mackerras 	/* Early out if we are an invalid form of lswx */
64316c57b36SKumar Gala 	if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
64414cf11afSPaul Mackerras 		if ((rT == rA) || (rT == NB_RB))
64514cf11afSPaul Mackerras 			return -EINVAL;
64614cf11afSPaul Mackerras 
64714cf11afSPaul Mackerras 	EA = (rA == 0) ? 0 : regs->gpr[rA];
64814cf11afSPaul Mackerras 
64916c57b36SKumar Gala 	switch (instword & PPC_INST_STRING_MASK) {
65016c57b36SKumar Gala 		case PPC_INST_LSWX:
65116c57b36SKumar Gala 		case PPC_INST_STSWX:
65214cf11afSPaul Mackerras 			EA += NB_RB;
65314cf11afSPaul Mackerras 			num_bytes = regs->xer & 0x7f;
65414cf11afSPaul Mackerras 			break;
65516c57b36SKumar Gala 		case PPC_INST_LSWI:
65616c57b36SKumar Gala 		case PPC_INST_STSWI:
65714cf11afSPaul Mackerras 			num_bytes = (NB_RB == 0) ? 32 : NB_RB;
65814cf11afSPaul Mackerras 			break;
65914cf11afSPaul Mackerras 		default:
66014cf11afSPaul Mackerras 			return -EINVAL;
66114cf11afSPaul Mackerras 	}
66214cf11afSPaul Mackerras 
66314cf11afSPaul Mackerras 	while (num_bytes != 0)
66414cf11afSPaul Mackerras 	{
66514cf11afSPaul Mackerras 		u8 val;
66614cf11afSPaul Mackerras 		u32 shift = 8 * (3 - (pos & 0x3));
66714cf11afSPaul Mackerras 
66816c57b36SKumar Gala 		switch ((instword & PPC_INST_STRING_MASK)) {
66916c57b36SKumar Gala 			case PPC_INST_LSWX:
67016c57b36SKumar Gala 			case PPC_INST_LSWI:
67114cf11afSPaul Mackerras 				if (get_user(val, (u8 __user *)EA))
67214cf11afSPaul Mackerras 					return -EFAULT;
67314cf11afSPaul Mackerras 				/* first time updating this reg,
67414cf11afSPaul Mackerras 				 * zero it out */
67514cf11afSPaul Mackerras 				if (pos == 0)
67614cf11afSPaul Mackerras 					regs->gpr[rT] = 0;
67714cf11afSPaul Mackerras 				regs->gpr[rT] |= val << shift;
67814cf11afSPaul Mackerras 				break;
67916c57b36SKumar Gala 			case PPC_INST_STSWI:
68016c57b36SKumar Gala 			case PPC_INST_STSWX:
68114cf11afSPaul Mackerras 				val = regs->gpr[rT] >> shift;
68214cf11afSPaul Mackerras 				if (put_user(val, (u8 __user *)EA))
68314cf11afSPaul Mackerras 					return -EFAULT;
68414cf11afSPaul Mackerras 				break;
68514cf11afSPaul Mackerras 		}
68614cf11afSPaul Mackerras 		/* move EA to next address */
68714cf11afSPaul Mackerras 		EA += 1;
68814cf11afSPaul Mackerras 		num_bytes--;
68914cf11afSPaul Mackerras 
69014cf11afSPaul Mackerras 		/* manage our position within the register */
69114cf11afSPaul Mackerras 		if (++pos == 4) {
69214cf11afSPaul Mackerras 			pos = 0;
69314cf11afSPaul Mackerras 			if (++rT == 32)
69414cf11afSPaul Mackerras 				rT = 0;
69514cf11afSPaul Mackerras 		}
69614cf11afSPaul Mackerras 	}
69714cf11afSPaul Mackerras 
69814cf11afSPaul Mackerras 	return 0;
69914cf11afSPaul Mackerras }
70014cf11afSPaul Mackerras 
701c3412dcbSWill Schmidt static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
702c3412dcbSWill Schmidt {
703c3412dcbSWill Schmidt 	u32 ra,rs;
704c3412dcbSWill Schmidt 	unsigned long tmp;
705c3412dcbSWill Schmidt 
706c3412dcbSWill Schmidt 	ra = (instword >> 16) & 0x1f;
707c3412dcbSWill Schmidt 	rs = (instword >> 21) & 0x1f;
708c3412dcbSWill Schmidt 
709c3412dcbSWill Schmidt 	tmp = regs->gpr[rs];
710c3412dcbSWill Schmidt 	tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
711c3412dcbSWill Schmidt 	tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
712c3412dcbSWill Schmidt 	tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
713c3412dcbSWill Schmidt 	regs->gpr[ra] = tmp;
714c3412dcbSWill Schmidt 
715c3412dcbSWill Schmidt 	return 0;
716c3412dcbSWill Schmidt }
717c3412dcbSWill Schmidt 
718c1469f13SKumar Gala static int emulate_isel(struct pt_regs *regs, u32 instword)
719c1469f13SKumar Gala {
720c1469f13SKumar Gala 	u8 rT = (instword >> 21) & 0x1f;
721c1469f13SKumar Gala 	u8 rA = (instword >> 16) & 0x1f;
722c1469f13SKumar Gala 	u8 rB = (instword >> 11) & 0x1f;
723c1469f13SKumar Gala 	u8 BC = (instword >> 6) & 0x1f;
724c1469f13SKumar Gala 	u8 bit;
725c1469f13SKumar Gala 	unsigned long tmp;
726c1469f13SKumar Gala 
727c1469f13SKumar Gala 	tmp = (rA == 0) ? 0 : regs->gpr[rA];
728c1469f13SKumar Gala 	bit = (regs->ccr >> (31 - BC)) & 0x1;
729c1469f13SKumar Gala 
730c1469f13SKumar Gala 	regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
731c1469f13SKumar Gala 
732c1469f13SKumar Gala 	return 0;
733c1469f13SKumar Gala }
734c1469f13SKumar Gala 
73514cf11afSPaul Mackerras static int emulate_instruction(struct pt_regs *regs)
73614cf11afSPaul Mackerras {
73714cf11afSPaul Mackerras 	u32 instword;
73814cf11afSPaul Mackerras 	u32 rd;
73914cf11afSPaul Mackerras 
740fab5db97SPaul Mackerras 	if (!user_mode(regs) || (regs->msr & MSR_LE))
74114cf11afSPaul Mackerras 		return -EINVAL;
74214cf11afSPaul Mackerras 	CHECK_FULL_REGS(regs);
74314cf11afSPaul Mackerras 
74414cf11afSPaul Mackerras 	if (get_user(instword, (u32 __user *)(regs->nip)))
74514cf11afSPaul Mackerras 		return -EFAULT;
74614cf11afSPaul Mackerras 
74714cf11afSPaul Mackerras 	/* Emulate the mfspr rD, PVR. */
74816c57b36SKumar Gala 	if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
749eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(mfpvr, regs);
75014cf11afSPaul Mackerras 		rd = (instword >> 21) & 0x1f;
75114cf11afSPaul Mackerras 		regs->gpr[rd] = mfspr(SPRN_PVR);
75214cf11afSPaul Mackerras 		return 0;
75314cf11afSPaul Mackerras 	}
75414cf11afSPaul Mackerras 
75514cf11afSPaul Mackerras 	/* Emulating the dcba insn is just a no-op.  */
75680947e7cSGeert Uytterhoeven 	if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
757eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(dcba, regs);
75814cf11afSPaul Mackerras 		return 0;
75980947e7cSGeert Uytterhoeven 	}
76014cf11afSPaul Mackerras 
76114cf11afSPaul Mackerras 	/* Emulate the mcrxr insn.  */
76216c57b36SKumar Gala 	if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
76386417780SPaul Mackerras 		int shift = (instword >> 21) & 0x1c;
76414cf11afSPaul Mackerras 		unsigned long msk = 0xf0000000UL >> shift;
76514cf11afSPaul Mackerras 
766eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(mcrxr, regs);
76714cf11afSPaul Mackerras 		regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
76814cf11afSPaul Mackerras 		regs->xer &= ~0xf0000000UL;
76914cf11afSPaul Mackerras 		return 0;
77014cf11afSPaul Mackerras 	}
77114cf11afSPaul Mackerras 
77214cf11afSPaul Mackerras 	/* Emulate load/store string insn. */
77380947e7cSGeert Uytterhoeven 	if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
774eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(string, regs);
77514cf11afSPaul Mackerras 		return emulate_string_inst(regs, instword);
77680947e7cSGeert Uytterhoeven 	}
77714cf11afSPaul Mackerras 
778c3412dcbSWill Schmidt 	/* Emulate the popcntb (Population Count Bytes) instruction. */
77916c57b36SKumar Gala 	if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
780eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(popcntb, regs);
781c3412dcbSWill Schmidt 		return emulate_popcntb_inst(regs, instword);
782c3412dcbSWill Schmidt 	}
783c3412dcbSWill Schmidt 
784c1469f13SKumar Gala 	/* Emulate isel (Integer Select) instruction */
78516c57b36SKumar Gala 	if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
786eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(isel, regs);
787c1469f13SKumar Gala 		return emulate_isel(regs, instword);
788c1469f13SKumar Gala 	}
789c1469f13SKumar Gala 
79014cf11afSPaul Mackerras 	return -EINVAL;
79114cf11afSPaul Mackerras }
79214cf11afSPaul Mackerras 
79373c9ceabSJeremy Fitzhardinge int is_valid_bugaddr(unsigned long addr)
79414cf11afSPaul Mackerras {
79573c9ceabSJeremy Fitzhardinge 	return is_kernel_addr(addr);
79614cf11afSPaul Mackerras }
79714cf11afSPaul Mackerras 
7988dad3f92SPaul Mackerras void __kprobes program_check_exception(struct pt_regs *regs)
79914cf11afSPaul Mackerras {
80014cf11afSPaul Mackerras 	unsigned int reason = get_reason(regs);
80114cf11afSPaul Mackerras 	extern int do_mathemu(struct pt_regs *regs);
80214cf11afSPaul Mackerras 
803aa42c69cSKim Phillips 	/* We can now get here via a FP Unavailable exception if the core
80404903a30SKumar Gala 	 * has no FPU, in that case the reason flags will be 0 */
80514cf11afSPaul Mackerras 
80614cf11afSPaul Mackerras 	if (reason & REASON_FP) {
80714cf11afSPaul Mackerras 		/* IEEE FP exception */
808dc1c1ca3SStephen Rothwell 		parse_fpe(regs);
8098dad3f92SPaul Mackerras 		return;
8108dad3f92SPaul Mackerras 	}
8118dad3f92SPaul Mackerras 	if (reason & REASON_TRAP) {
81214cf11afSPaul Mackerras 		/* trap exception */
813dc1c1ca3SStephen Rothwell 		if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
814dc1c1ca3SStephen Rothwell 				== NOTIFY_STOP)
815dc1c1ca3SStephen Rothwell 			return;
81614cf11afSPaul Mackerras 		if (debugger_bpt(regs))
81714cf11afSPaul Mackerras 			return;
81873c9ceabSJeremy Fitzhardinge 
81973c9ceabSJeremy Fitzhardinge 		if (!(regs->msr & MSR_PR) &&  /* not user-mode */
820608e2619SHeiko Carstens 		    report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
82114cf11afSPaul Mackerras 			regs->nip += 4;
82214cf11afSPaul Mackerras 			return;
82314cf11afSPaul Mackerras 		}
8248dad3f92SPaul Mackerras 		_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
8258dad3f92SPaul Mackerras 		return;
8268dad3f92SPaul Mackerras 	}
8278dad3f92SPaul Mackerras 
828cd8a5673SPaul Mackerras 	local_irq_enable();
829cd8a5673SPaul Mackerras 
83004903a30SKumar Gala #ifdef CONFIG_MATH_EMULATION
83104903a30SKumar Gala 	/* (reason & REASON_ILLEGAL) would be the obvious thing here,
83204903a30SKumar Gala 	 * but there seems to be a hardware bug on the 405GP (RevD)
83304903a30SKumar Gala 	 * that means ESR is sometimes set incorrectly - either to
83404903a30SKumar Gala 	 * ESR_DST (!?) or 0.  In the process of chasing this with the
83504903a30SKumar Gala 	 * hardware people - not sure if it can happen on any illegal
83604903a30SKumar Gala 	 * instruction or only on FP instructions, whether there is a
83704903a30SKumar Gala 	 * pattern to occurences etc. -dgibson 31/Mar/2003 */
8385fad293bSKumar Gala 	switch (do_mathemu(regs)) {
8395fad293bSKumar Gala 	case 0:
84004903a30SKumar Gala 		emulate_single_step(regs);
84104903a30SKumar Gala 		return;
8425fad293bSKumar Gala 	case 1: {
8435fad293bSKumar Gala 			int code = 0;
8445fad293bSKumar Gala 			code = __parse_fpscr(current->thread.fpscr.val);
8455fad293bSKumar Gala 			_exception(SIGFPE, regs, code, regs->nip);
8465fad293bSKumar Gala 			return;
84704903a30SKumar Gala 		}
8485fad293bSKumar Gala 	case -EFAULT:
8495fad293bSKumar Gala 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
8505fad293bSKumar Gala 		return;
8515fad293bSKumar Gala 	}
8525fad293bSKumar Gala 	/* fall through on any other errors */
85304903a30SKumar Gala #endif /* CONFIG_MATH_EMULATION */
85404903a30SKumar Gala 
8558dad3f92SPaul Mackerras 	/* Try to emulate it if we should. */
8568dad3f92SPaul Mackerras 	if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
85714cf11afSPaul Mackerras 		switch (emulate_instruction(regs)) {
85814cf11afSPaul Mackerras 		case 0:
85914cf11afSPaul Mackerras 			regs->nip += 4;
86014cf11afSPaul Mackerras 			emulate_single_step(regs);
8618dad3f92SPaul Mackerras 			return;
86214cf11afSPaul Mackerras 		case -EFAULT:
86314cf11afSPaul Mackerras 			_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
8648dad3f92SPaul Mackerras 			return;
8658dad3f92SPaul Mackerras 		}
8668dad3f92SPaul Mackerras 	}
8678dad3f92SPaul Mackerras 
86814cf11afSPaul Mackerras 	if (reason & REASON_PRIVILEGED)
86914cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
87014cf11afSPaul Mackerras 	else
87114cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
87214cf11afSPaul Mackerras }
87314cf11afSPaul Mackerras 
874dc1c1ca3SStephen Rothwell void alignment_exception(struct pt_regs *regs)
87514cf11afSPaul Mackerras {
8764393c4f6SBenjamin Herrenschmidt 	int sig, code, fixed = 0;
87714cf11afSPaul Mackerras 
878e9370ae1SPaul Mackerras 	/* we don't implement logging of alignment exceptions */
879e9370ae1SPaul Mackerras 	if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
88014cf11afSPaul Mackerras 		fixed = fix_alignment(regs);
88114cf11afSPaul Mackerras 
88214cf11afSPaul Mackerras 	if (fixed == 1) {
88314cf11afSPaul Mackerras 		regs->nip += 4;	/* skip over emulated instruction */
88414cf11afSPaul Mackerras 		emulate_single_step(regs);
88514cf11afSPaul Mackerras 		return;
88614cf11afSPaul Mackerras 	}
88714cf11afSPaul Mackerras 
88814cf11afSPaul Mackerras 	/* Operand address was bad */
88914cf11afSPaul Mackerras 	if (fixed == -EFAULT) {
8904393c4f6SBenjamin Herrenschmidt 		sig = SIGSEGV;
8914393c4f6SBenjamin Herrenschmidt 		code = SEGV_ACCERR;
8924393c4f6SBenjamin Herrenschmidt 	} else {
8934393c4f6SBenjamin Herrenschmidt 		sig = SIGBUS;
8944393c4f6SBenjamin Herrenschmidt 		code = BUS_ADRALN;
89514cf11afSPaul Mackerras 	}
8964393c4f6SBenjamin Herrenschmidt 	if (user_mode(regs))
8974393c4f6SBenjamin Herrenschmidt 		_exception(sig, regs, code, regs->dar);
8984393c4f6SBenjamin Herrenschmidt 	else
8994393c4f6SBenjamin Herrenschmidt 		bad_page_fault(regs, regs->dar, sig);
90014cf11afSPaul Mackerras }
90114cf11afSPaul Mackerras 
90214cf11afSPaul Mackerras void StackOverflow(struct pt_regs *regs)
90314cf11afSPaul Mackerras {
90414cf11afSPaul Mackerras 	printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
90514cf11afSPaul Mackerras 	       current, regs->gpr[1]);
90614cf11afSPaul Mackerras 	debugger(regs);
90714cf11afSPaul Mackerras 	show_regs(regs);
90814cf11afSPaul Mackerras 	panic("kernel stack overflow");
90914cf11afSPaul Mackerras }
91014cf11afSPaul Mackerras 
91114cf11afSPaul Mackerras void nonrecoverable_exception(struct pt_regs *regs)
91214cf11afSPaul Mackerras {
91314cf11afSPaul Mackerras 	printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
91414cf11afSPaul Mackerras 	       regs->nip, regs->msr);
91514cf11afSPaul Mackerras 	debugger(regs);
91614cf11afSPaul Mackerras 	die("nonrecoverable exception", regs, SIGKILL);
91714cf11afSPaul Mackerras }
91814cf11afSPaul Mackerras 
91914cf11afSPaul Mackerras void trace_syscall(struct pt_regs *regs)
92014cf11afSPaul Mackerras {
92114cf11afSPaul Mackerras 	printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld    %s\n",
92219c5870cSAlexey Dobriyan 	       current, task_pid_nr(current), regs->nip, regs->link, regs->gpr[0],
92314cf11afSPaul Mackerras 	       regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
92414cf11afSPaul Mackerras }
92514cf11afSPaul Mackerras 
926dc1c1ca3SStephen Rothwell void kernel_fp_unavailable_exception(struct pt_regs *regs)
927dc1c1ca3SStephen Rothwell {
928dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
929dc1c1ca3SStephen Rothwell 			  "%lx at %lx\n", regs->trap, regs->nip);
930dc1c1ca3SStephen Rothwell 	die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
931dc1c1ca3SStephen Rothwell }
932dc1c1ca3SStephen Rothwell 
933dc1c1ca3SStephen Rothwell void altivec_unavailable_exception(struct pt_regs *regs)
934dc1c1ca3SStephen Rothwell {
935dc1c1ca3SStephen Rothwell 	if (user_mode(regs)) {
936dc1c1ca3SStephen Rothwell 		/* A user program has executed an altivec instruction,
937dc1c1ca3SStephen Rothwell 		   but this kernel doesn't support altivec. */
938dc1c1ca3SStephen Rothwell 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
939dc1c1ca3SStephen Rothwell 		return;
940dc1c1ca3SStephen Rothwell 	}
9416c4841c2SAnton Blanchard 
942dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
943dc1c1ca3SStephen Rothwell 			"%lx at %lx\n", regs->trap, regs->nip);
944dc1c1ca3SStephen Rothwell 	die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
945dc1c1ca3SStephen Rothwell }
946dc1c1ca3SStephen Rothwell 
947ce48b210SMichael Neuling void vsx_unavailable_exception(struct pt_regs *regs)
948ce48b210SMichael Neuling {
949ce48b210SMichael Neuling 	if (user_mode(regs)) {
950ce48b210SMichael Neuling 		/* A user program has executed an vsx instruction,
951ce48b210SMichael Neuling 		   but this kernel doesn't support vsx. */
952ce48b210SMichael Neuling 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
953ce48b210SMichael Neuling 		return;
954ce48b210SMichael Neuling 	}
955ce48b210SMichael Neuling 
956ce48b210SMichael Neuling 	printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
957ce48b210SMichael Neuling 			"%lx at %lx\n", regs->trap, regs->nip);
958ce48b210SMichael Neuling 	die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
959ce48b210SMichael Neuling }
960ce48b210SMichael Neuling 
961dc1c1ca3SStephen Rothwell void performance_monitor_exception(struct pt_regs *regs)
962dc1c1ca3SStephen Rothwell {
963dc1c1ca3SStephen Rothwell 	perf_irq(regs);
964dc1c1ca3SStephen Rothwell }
965dc1c1ca3SStephen Rothwell 
9668dad3f92SPaul Mackerras #ifdef CONFIG_8xx
96714cf11afSPaul Mackerras void SoftwareEmulation(struct pt_regs *regs)
96814cf11afSPaul Mackerras {
96914cf11afSPaul Mackerras 	extern int do_mathemu(struct pt_regs *);
97014cf11afSPaul Mackerras 	extern int Soft_emulate_8xx(struct pt_regs *);
9715dd57a13SScott Wood #if defined(CONFIG_MATH_EMULATION) || defined(CONFIG_8XX_MINIMAL_FPEMU)
97214cf11afSPaul Mackerras 	int errcode;
9735dd57a13SScott Wood #endif
97414cf11afSPaul Mackerras 
97514cf11afSPaul Mackerras 	CHECK_FULL_REGS(regs);
97614cf11afSPaul Mackerras 
97714cf11afSPaul Mackerras 	if (!user_mode(regs)) {
97814cf11afSPaul Mackerras 		debugger(regs);
97914cf11afSPaul Mackerras 		die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
98014cf11afSPaul Mackerras 	}
98114cf11afSPaul Mackerras 
98214cf11afSPaul Mackerras #ifdef CONFIG_MATH_EMULATION
98314cf11afSPaul Mackerras 	errcode = do_mathemu(regs);
98480947e7cSGeert Uytterhoeven 	if (errcode >= 0)
985eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(math, regs);
9865fad293bSKumar Gala 
9875fad293bSKumar Gala 	switch (errcode) {
9885fad293bSKumar Gala 	case 0:
9895fad293bSKumar Gala 		emulate_single_step(regs);
9905fad293bSKumar Gala 		return;
9915fad293bSKumar Gala 	case 1: {
9925fad293bSKumar Gala 			int code = 0;
9935fad293bSKumar Gala 			code = __parse_fpscr(current->thread.fpscr.val);
9945fad293bSKumar Gala 			_exception(SIGFPE, regs, code, regs->nip);
9955fad293bSKumar Gala 			return;
9965fad293bSKumar Gala 		}
9975fad293bSKumar Gala 	case -EFAULT:
9985fad293bSKumar Gala 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
9995fad293bSKumar Gala 		return;
10005fad293bSKumar Gala 	default:
10015fad293bSKumar Gala 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
10025fad293bSKumar Gala 		return;
10035fad293bSKumar Gala 	}
10045fad293bSKumar Gala 
10055dd57a13SScott Wood #elif defined(CONFIG_8XX_MINIMAL_FPEMU)
100614cf11afSPaul Mackerras 	errcode = Soft_emulate_8xx(regs);
100780947e7cSGeert Uytterhoeven 	if (errcode >= 0)
1008eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(8xx, regs);
100980947e7cSGeert Uytterhoeven 
10105fad293bSKumar Gala 	switch (errcode) {
10115fad293bSKumar Gala 	case 0:
101214cf11afSPaul Mackerras 		emulate_single_step(regs);
10135fad293bSKumar Gala 		return;
10145fad293bSKumar Gala 	case 1:
10155fad293bSKumar Gala 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
10165fad293bSKumar Gala 		return;
10175fad293bSKumar Gala 	case -EFAULT:
10185fad293bSKumar Gala 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
10195fad293bSKumar Gala 		return;
10205fad293bSKumar Gala 	}
10215dd57a13SScott Wood #else
10225dd57a13SScott Wood 	_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
10235fad293bSKumar Gala #endif
102414cf11afSPaul Mackerras }
10258dad3f92SPaul Mackerras #endif /* CONFIG_8xx */
102614cf11afSPaul Mackerras 
102714cf11afSPaul Mackerras #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
102814cf11afSPaul Mackerras 
1029f8279621SKumar Gala void __kprobes DebugException(struct pt_regs *regs, unsigned long debug_status)
103014cf11afSPaul Mackerras {
1031ec097c84SRoland McGrath 	/* Hack alert: On BookE, Branch Taken stops on the branch itself, while
1032ec097c84SRoland McGrath 	 * on server, it stops on the target of the branch. In order to simulate
1033ec097c84SRoland McGrath 	 * the server behaviour, we thus restart right away with a single step
1034ec097c84SRoland McGrath 	 * instead of stopping here when hitting a BT
1035ec097c84SRoland McGrath 	 */
1036ec097c84SRoland McGrath 	if (debug_status & DBSR_BT) {
1037ec097c84SRoland McGrath 		regs->msr &= ~MSR_DE;
1038ec097c84SRoland McGrath 
1039ec097c84SRoland McGrath 		/* Disable BT */
1040ec097c84SRoland McGrath 		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
1041ec097c84SRoland McGrath 		/* Clear the BT event */
1042ec097c84SRoland McGrath 		mtspr(SPRN_DBSR, DBSR_BT);
1043ec097c84SRoland McGrath 
1044ec097c84SRoland McGrath 		/* Do the single step trick only when coming from userspace */
1045ec097c84SRoland McGrath 		if (user_mode(regs)) {
1046ec097c84SRoland McGrath 			current->thread.dbcr0 &= ~DBCR0_BT;
1047ec097c84SRoland McGrath 			current->thread.dbcr0 |= DBCR0_IDM | DBCR0_IC;
1048ec097c84SRoland McGrath 			regs->msr |= MSR_DE;
1049ec097c84SRoland McGrath 			return;
1050ec097c84SRoland McGrath 		}
1051ec097c84SRoland McGrath 
1052ec097c84SRoland McGrath 		if (notify_die(DIE_SSTEP, "block_step", regs, 5,
1053ec097c84SRoland McGrath 			       5, SIGTRAP) == NOTIFY_STOP) {
1054ec097c84SRoland McGrath 			return;
1055ec097c84SRoland McGrath 		}
1056ec097c84SRoland McGrath 		if (debugger_sstep(regs))
1057ec097c84SRoland McGrath 			return;
1058ec097c84SRoland McGrath 	} else if (debug_status & DBSR_IC) { 	/* Instruction complete */
105914cf11afSPaul Mackerras 		regs->msr &= ~MSR_DE;
1060f8279621SKumar Gala 
106114cf11afSPaul Mackerras 		/* Disable instruction completion */
106214cf11afSPaul Mackerras 		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
106314cf11afSPaul Mackerras 		/* Clear the instruction completion event */
106414cf11afSPaul Mackerras 		mtspr(SPRN_DBSR, DBSR_IC);
1065f8279621SKumar Gala 
1066f8279621SKumar Gala 		if (notify_die(DIE_SSTEP, "single_step", regs, 5,
1067f8279621SKumar Gala 			       5, SIGTRAP) == NOTIFY_STOP) {
106814cf11afSPaul Mackerras 			return;
106914cf11afSPaul Mackerras 		}
1070f8279621SKumar Gala 
1071f8279621SKumar Gala 		if (debugger_sstep(regs))
1072f8279621SKumar Gala 			return;
1073f8279621SKumar Gala 
1074ec097c84SRoland McGrath 		if (user_mode(regs))
1075ec097c84SRoland McGrath 			current->thread.dbcr0 &= ~(DBCR0_IC);
1076f8279621SKumar Gala 
1077f8279621SKumar Gala 		_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
1078d6a61bfcSLuis Machado 	} else if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
1079d6a61bfcSLuis Machado 		regs->msr &= ~MSR_DE;
1080d6a61bfcSLuis Machado 
1081d6a61bfcSLuis Machado 		if (user_mode(regs)) {
1082d6a61bfcSLuis Machado 			current->thread.dbcr0 &= ~(DBSR_DAC1R | DBSR_DAC1W |
1083d6a61bfcSLuis Machado 								DBCR0_IDM);
1084d6a61bfcSLuis Machado 		} else {
1085d6a61bfcSLuis Machado 			/* Disable DAC interupts */
1086d6a61bfcSLuis Machado 			mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~(DBSR_DAC1R |
1087d6a61bfcSLuis Machado 						DBSR_DAC1W | DBCR0_IDM));
1088d6a61bfcSLuis Machado 
1089d6a61bfcSLuis Machado 			/* Clear the DAC event */
1090d6a61bfcSLuis Machado 			mtspr(SPRN_DBSR, (DBSR_DAC1R | DBSR_DAC1W));
1091d6a61bfcSLuis Machado 		}
1092d6a61bfcSLuis Machado 		/* Setup and send the trap to the handler */
1093d6a61bfcSLuis Machado 		do_dabr(regs, mfspr(SPRN_DAC1), debug_status);
109414cf11afSPaul Mackerras 	}
109514cf11afSPaul Mackerras }
109614cf11afSPaul Mackerras #endif /* CONFIG_4xx || CONFIG_BOOKE */
109714cf11afSPaul Mackerras 
109814cf11afSPaul Mackerras #if !defined(CONFIG_TAU_INT)
109914cf11afSPaul Mackerras void TAUException(struct pt_regs *regs)
110014cf11afSPaul Mackerras {
110114cf11afSPaul Mackerras 	printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx    %s\n",
110214cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap, print_tainted());
110314cf11afSPaul Mackerras }
110414cf11afSPaul Mackerras #endif /* CONFIG_INT_TAU */
110514cf11afSPaul Mackerras 
110614cf11afSPaul Mackerras #ifdef CONFIG_ALTIVEC
1107dc1c1ca3SStephen Rothwell void altivec_assist_exception(struct pt_regs *regs)
110814cf11afSPaul Mackerras {
110914cf11afSPaul Mackerras 	int err;
111014cf11afSPaul Mackerras 
111114cf11afSPaul Mackerras 	if (!user_mode(regs)) {
111214cf11afSPaul Mackerras 		printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
111314cf11afSPaul Mackerras 		       " at %lx\n", regs->nip);
11148dad3f92SPaul Mackerras 		die("Kernel VMX/Altivec assist exception", regs, SIGILL);
111514cf11afSPaul Mackerras 	}
111614cf11afSPaul Mackerras 
1117dc1c1ca3SStephen Rothwell 	flush_altivec_to_thread(current);
1118dc1c1ca3SStephen Rothwell 
1119eecff81dSAnton Blanchard 	PPC_WARN_EMULATED(altivec, regs);
112014cf11afSPaul Mackerras 	err = emulate_altivec(regs);
112114cf11afSPaul Mackerras 	if (err == 0) {
112214cf11afSPaul Mackerras 		regs->nip += 4;		/* skip emulated instruction */
112314cf11afSPaul Mackerras 		emulate_single_step(regs);
112414cf11afSPaul Mackerras 		return;
112514cf11afSPaul Mackerras 	}
112614cf11afSPaul Mackerras 
112714cf11afSPaul Mackerras 	if (err == -EFAULT) {
112814cf11afSPaul Mackerras 		/* got an error reading the instruction */
112914cf11afSPaul Mackerras 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
113014cf11afSPaul Mackerras 	} else {
113114cf11afSPaul Mackerras 		/* didn't recognize the instruction */
113214cf11afSPaul Mackerras 		/* XXX quick hack for now: set the non-Java bit in the VSCR */
113314cf11afSPaul Mackerras 		if (printk_ratelimit())
113414cf11afSPaul Mackerras 			printk(KERN_ERR "Unrecognized altivec instruction "
113514cf11afSPaul Mackerras 			       "in %s at %lx\n", current->comm, regs->nip);
113614cf11afSPaul Mackerras 		current->thread.vscr.u[3] |= 0x10000;
113714cf11afSPaul Mackerras 	}
113814cf11afSPaul Mackerras }
113914cf11afSPaul Mackerras #endif /* CONFIG_ALTIVEC */
114014cf11afSPaul Mackerras 
1141ce48b210SMichael Neuling #ifdef CONFIG_VSX
1142ce48b210SMichael Neuling void vsx_assist_exception(struct pt_regs *regs)
1143ce48b210SMichael Neuling {
1144ce48b210SMichael Neuling 	if (!user_mode(regs)) {
1145ce48b210SMichael Neuling 		printk(KERN_EMERG "VSX assist exception in kernel mode"
1146ce48b210SMichael Neuling 		       " at %lx\n", regs->nip);
1147ce48b210SMichael Neuling 		die("Kernel VSX assist exception", regs, SIGILL);
1148ce48b210SMichael Neuling 	}
1149ce48b210SMichael Neuling 
1150ce48b210SMichael Neuling 	flush_vsx_to_thread(current);
1151ce48b210SMichael Neuling 	printk(KERN_INFO "VSX assist not supported at %lx\n", regs->nip);
1152ce48b210SMichael Neuling 	_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1153ce48b210SMichael Neuling }
1154ce48b210SMichael Neuling #endif /* CONFIG_VSX */
1155ce48b210SMichael Neuling 
115614cf11afSPaul Mackerras #ifdef CONFIG_FSL_BOOKE
1157620165f9SKumar Gala 
1158620165f9SKumar Gala void doorbell_exception(struct pt_regs *regs)
1159620165f9SKumar Gala {
1160620165f9SKumar Gala #ifdef CONFIG_SMP
1161620165f9SKumar Gala 	int cpu = smp_processor_id();
1162620165f9SKumar Gala 	int msg;
1163620165f9SKumar Gala 
1164620165f9SKumar Gala 	if (num_online_cpus() < 2)
1165620165f9SKumar Gala 		return;
1166620165f9SKumar Gala 
1167620165f9SKumar Gala 	for (msg = 0; msg < 4; msg++)
1168620165f9SKumar Gala 		if (test_and_clear_bit(msg, &dbell_smp_message[cpu]))
1169620165f9SKumar Gala 			smp_message_recv(msg);
1170620165f9SKumar Gala #else
1171620165f9SKumar Gala 	printk(KERN_WARNING "Received doorbell on non-smp system\n");
1172620165f9SKumar Gala #endif
1173620165f9SKumar Gala }
1174620165f9SKumar Gala 
117514cf11afSPaul Mackerras void CacheLockingException(struct pt_regs *regs, unsigned long address,
117614cf11afSPaul Mackerras 			   unsigned long error_code)
117714cf11afSPaul Mackerras {
117814cf11afSPaul Mackerras 	/* We treat cache locking instructions from the user
117914cf11afSPaul Mackerras 	 * as priv ops, in the future we could try to do
118014cf11afSPaul Mackerras 	 * something smarter
118114cf11afSPaul Mackerras 	 */
118214cf11afSPaul Mackerras 	if (error_code & (ESR_DLK|ESR_ILK))
118314cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
118414cf11afSPaul Mackerras 	return;
118514cf11afSPaul Mackerras }
118614cf11afSPaul Mackerras #endif /* CONFIG_FSL_BOOKE */
118714cf11afSPaul Mackerras 
118814cf11afSPaul Mackerras #ifdef CONFIG_SPE
118914cf11afSPaul Mackerras void SPEFloatingPointException(struct pt_regs *regs)
119014cf11afSPaul Mackerras {
11916a800f36SLiu Yu 	extern int do_spe_mathemu(struct pt_regs *regs);
119214cf11afSPaul Mackerras 	unsigned long spefscr;
119314cf11afSPaul Mackerras 	int fpexc_mode;
119414cf11afSPaul Mackerras 	int code = 0;
11956a800f36SLiu Yu 	int err;
11966a800f36SLiu Yu 
11976a800f36SLiu Yu 	preempt_disable();
11986a800f36SLiu Yu 	if (regs->msr & MSR_SPE)
11996a800f36SLiu Yu 		giveup_spe(current);
12006a800f36SLiu Yu 	preempt_enable();
120114cf11afSPaul Mackerras 
120214cf11afSPaul Mackerras 	spefscr = current->thread.spefscr;
120314cf11afSPaul Mackerras 	fpexc_mode = current->thread.fpexc_mode;
120414cf11afSPaul Mackerras 
120514cf11afSPaul Mackerras 	if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
120614cf11afSPaul Mackerras 		code = FPE_FLTOVF;
120714cf11afSPaul Mackerras 	}
120814cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
120914cf11afSPaul Mackerras 		code = FPE_FLTUND;
121014cf11afSPaul Mackerras 	}
121114cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
121214cf11afSPaul Mackerras 		code = FPE_FLTDIV;
121314cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
121414cf11afSPaul Mackerras 		code = FPE_FLTINV;
121514cf11afSPaul Mackerras 	}
121614cf11afSPaul Mackerras 	else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
121714cf11afSPaul Mackerras 		code = FPE_FLTRES;
121814cf11afSPaul Mackerras 
12196a800f36SLiu Yu 	err = do_spe_mathemu(regs);
12206a800f36SLiu Yu 	if (err == 0) {
12216a800f36SLiu Yu 		regs->nip += 4;		/* skip emulated instruction */
12226a800f36SLiu Yu 		emulate_single_step(regs);
122314cf11afSPaul Mackerras 		return;
122414cf11afSPaul Mackerras 	}
12256a800f36SLiu Yu 
12266a800f36SLiu Yu 	if (err == -EFAULT) {
12276a800f36SLiu Yu 		/* got an error reading the instruction */
12286a800f36SLiu Yu 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
12296a800f36SLiu Yu 	} else if (err == -EINVAL) {
12306a800f36SLiu Yu 		/* didn't recognize the instruction */
12316a800f36SLiu Yu 		printk(KERN_ERR "unrecognized spe instruction "
12326a800f36SLiu Yu 		       "in %s at %lx\n", current->comm, regs->nip);
12336a800f36SLiu Yu 	} else {
12346a800f36SLiu Yu 		_exception(SIGFPE, regs, code, regs->nip);
12356a800f36SLiu Yu 	}
12366a800f36SLiu Yu 
12376a800f36SLiu Yu 	return;
12386a800f36SLiu Yu }
12396a800f36SLiu Yu 
12406a800f36SLiu Yu void SPEFloatingPointRoundException(struct pt_regs *regs)
12416a800f36SLiu Yu {
12426a800f36SLiu Yu 	extern int speround_handler(struct pt_regs *regs);
12436a800f36SLiu Yu 	int err;
12446a800f36SLiu Yu 
12456a800f36SLiu Yu 	preempt_disable();
12466a800f36SLiu Yu 	if (regs->msr & MSR_SPE)
12476a800f36SLiu Yu 		giveup_spe(current);
12486a800f36SLiu Yu 	preempt_enable();
12496a800f36SLiu Yu 
12506a800f36SLiu Yu 	regs->nip -= 4;
12516a800f36SLiu Yu 	err = speround_handler(regs);
12526a800f36SLiu Yu 	if (err == 0) {
12536a800f36SLiu Yu 		regs->nip += 4;		/* skip emulated instruction */
12546a800f36SLiu Yu 		emulate_single_step(regs);
12556a800f36SLiu Yu 		return;
12566a800f36SLiu Yu 	}
12576a800f36SLiu Yu 
12586a800f36SLiu Yu 	if (err == -EFAULT) {
12596a800f36SLiu Yu 		/* got an error reading the instruction */
12606a800f36SLiu Yu 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
12616a800f36SLiu Yu 	} else if (err == -EINVAL) {
12626a800f36SLiu Yu 		/* didn't recognize the instruction */
12636a800f36SLiu Yu 		printk(KERN_ERR "unrecognized spe instruction "
12646a800f36SLiu Yu 		       "in %s at %lx\n", current->comm, regs->nip);
12656a800f36SLiu Yu 	} else {
12666a800f36SLiu Yu 		_exception(SIGFPE, regs, 0, regs->nip);
12676a800f36SLiu Yu 		return;
12686a800f36SLiu Yu 	}
12696a800f36SLiu Yu }
127014cf11afSPaul Mackerras #endif
127114cf11afSPaul Mackerras 
1272dc1c1ca3SStephen Rothwell /*
1273dc1c1ca3SStephen Rothwell  * We enter here if we get an unrecoverable exception, that is, one
1274dc1c1ca3SStephen Rothwell  * that happened at a point where the RI (recoverable interrupt) bit
1275dc1c1ca3SStephen Rothwell  * in the MSR is 0.  This indicates that SRR0/1 are live, and that
1276dc1c1ca3SStephen Rothwell  * we therefore lost state by taking this exception.
1277dc1c1ca3SStephen Rothwell  */
1278dc1c1ca3SStephen Rothwell void unrecoverable_exception(struct pt_regs *regs)
1279dc1c1ca3SStephen Rothwell {
1280dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
1281dc1c1ca3SStephen Rothwell 	       regs->trap, regs->nip);
1282dc1c1ca3SStephen Rothwell 	die("Unrecoverable exception", regs, SIGABRT);
1283dc1c1ca3SStephen Rothwell }
1284dc1c1ca3SStephen Rothwell 
128514cf11afSPaul Mackerras #ifdef CONFIG_BOOKE_WDT
128614cf11afSPaul Mackerras /*
128714cf11afSPaul Mackerras  * Default handler for a Watchdog exception,
128814cf11afSPaul Mackerras  * spins until a reboot occurs
128914cf11afSPaul Mackerras  */
129014cf11afSPaul Mackerras void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
129114cf11afSPaul Mackerras {
129214cf11afSPaul Mackerras 	/* Generic WatchdogHandler, implement your own */
129314cf11afSPaul Mackerras 	mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
129414cf11afSPaul Mackerras 	return;
129514cf11afSPaul Mackerras }
129614cf11afSPaul Mackerras 
129714cf11afSPaul Mackerras void WatchdogException(struct pt_regs *regs)
129814cf11afSPaul Mackerras {
129914cf11afSPaul Mackerras 	printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
130014cf11afSPaul Mackerras 	WatchdogHandler(regs);
130114cf11afSPaul Mackerras }
130214cf11afSPaul Mackerras #endif
1303dc1c1ca3SStephen Rothwell 
1304dc1c1ca3SStephen Rothwell /*
1305dc1c1ca3SStephen Rothwell  * We enter here if we discover during exception entry that we are
1306dc1c1ca3SStephen Rothwell  * running in supervisor mode with a userspace value in the stack pointer.
1307dc1c1ca3SStephen Rothwell  */
1308dc1c1ca3SStephen Rothwell void kernel_bad_stack(struct pt_regs *regs)
1309dc1c1ca3SStephen Rothwell {
1310dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
1311dc1c1ca3SStephen Rothwell 	       regs->gpr[1], regs->nip);
1312dc1c1ca3SStephen Rothwell 	die("Bad kernel stack pointer", regs, SIGABRT);
1313dc1c1ca3SStephen Rothwell }
131414cf11afSPaul Mackerras 
131514cf11afSPaul Mackerras void __init trap_init(void)
131614cf11afSPaul Mackerras {
131714cf11afSPaul Mackerras }
131880947e7cSGeert Uytterhoeven 
131980947e7cSGeert Uytterhoeven 
132080947e7cSGeert Uytterhoeven #ifdef CONFIG_PPC_EMULATED_STATS
132180947e7cSGeert Uytterhoeven 
132280947e7cSGeert Uytterhoeven #define WARN_EMULATED_SETUP(type)	.type = { .name = #type }
132380947e7cSGeert Uytterhoeven 
132480947e7cSGeert Uytterhoeven struct ppc_emulated ppc_emulated = {
132580947e7cSGeert Uytterhoeven #ifdef CONFIG_ALTIVEC
132680947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(altivec),
132780947e7cSGeert Uytterhoeven #endif
132880947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(dcba),
132980947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(dcbz),
133080947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(fp_pair),
133180947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(isel),
133280947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(mcrxr),
133380947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(mfpvr),
133480947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(multiple),
133580947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(popcntb),
133680947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(spe),
133780947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(string),
133880947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(unaligned),
133980947e7cSGeert Uytterhoeven #ifdef CONFIG_MATH_EMULATION
134080947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(math),
134180947e7cSGeert Uytterhoeven #elif defined(CONFIG_8XX_MINIMAL_FPEMU)
134280947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(8xx),
134380947e7cSGeert Uytterhoeven #endif
134480947e7cSGeert Uytterhoeven #ifdef CONFIG_VSX
134580947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(vsx),
134680947e7cSGeert Uytterhoeven #endif
134780947e7cSGeert Uytterhoeven };
134880947e7cSGeert Uytterhoeven 
134980947e7cSGeert Uytterhoeven u32 ppc_warn_emulated;
135080947e7cSGeert Uytterhoeven 
135180947e7cSGeert Uytterhoeven void ppc_warn_emulated_print(const char *type)
135280947e7cSGeert Uytterhoeven {
135380947e7cSGeert Uytterhoeven 	if (printk_ratelimit())
135480947e7cSGeert Uytterhoeven 		pr_warning("%s used emulated %s instruction\n", current->comm,
135580947e7cSGeert Uytterhoeven 			   type);
135680947e7cSGeert Uytterhoeven }
135780947e7cSGeert Uytterhoeven 
135880947e7cSGeert Uytterhoeven static int __init ppc_warn_emulated_init(void)
135980947e7cSGeert Uytterhoeven {
136080947e7cSGeert Uytterhoeven 	struct dentry *dir, *d;
136180947e7cSGeert Uytterhoeven 	unsigned int i;
136280947e7cSGeert Uytterhoeven 	struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
136380947e7cSGeert Uytterhoeven 
136480947e7cSGeert Uytterhoeven 	if (!powerpc_debugfs_root)
136580947e7cSGeert Uytterhoeven 		return -ENODEV;
136680947e7cSGeert Uytterhoeven 
136780947e7cSGeert Uytterhoeven 	dir = debugfs_create_dir("emulated_instructions",
136880947e7cSGeert Uytterhoeven 				 powerpc_debugfs_root);
136980947e7cSGeert Uytterhoeven 	if (!dir)
137080947e7cSGeert Uytterhoeven 		return -ENOMEM;
137180947e7cSGeert Uytterhoeven 
137280947e7cSGeert Uytterhoeven 	d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
137380947e7cSGeert Uytterhoeven 			       &ppc_warn_emulated);
137480947e7cSGeert Uytterhoeven 	if (!d)
137580947e7cSGeert Uytterhoeven 		goto fail;
137680947e7cSGeert Uytterhoeven 
137780947e7cSGeert Uytterhoeven 	for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
137880947e7cSGeert Uytterhoeven 		d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
137980947e7cSGeert Uytterhoeven 				       (u32 *)&entries[i].val.counter);
138080947e7cSGeert Uytterhoeven 		if (!d)
138180947e7cSGeert Uytterhoeven 			goto fail;
138280947e7cSGeert Uytterhoeven 	}
138380947e7cSGeert Uytterhoeven 
138480947e7cSGeert Uytterhoeven 	return 0;
138580947e7cSGeert Uytterhoeven 
138680947e7cSGeert Uytterhoeven fail:
138780947e7cSGeert Uytterhoeven 	debugfs_remove_recursive(dir);
138880947e7cSGeert Uytterhoeven 	return -ENOMEM;
138980947e7cSGeert Uytterhoeven }
139080947e7cSGeert Uytterhoeven 
139180947e7cSGeert Uytterhoeven device_initcall(ppc_warn_emulated_init);
139280947e7cSGeert Uytterhoeven 
139380947e7cSGeert Uytterhoeven #endif /* CONFIG_PPC_EMULATED_STATS */
1394