blob: 0a320dbd950a75ce6f3d91dc313a0ad402f7a41f [file] [log] [blame]
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001/*
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Modified by Cort Dougan (cort@cs.nmt.edu)
10 * and Paul Mackerras (paulus@samba.org)
11 */
12
13/*
14 * This file handles the architecture-dependent parts of hardware exceptions
15 */
16
Paul Mackerras14cf11a2005-09-26 16:04:21 +100017#include <linux/errno.h>
18#include <linux/sched.h>
19#include <linux/kernel.h>
20#include <linux/mm.h>
21#include <linux/stddef.h>
22#include <linux/unistd.h>
Paul Mackerras8dad3f92005-10-06 13:27:05 +100023#include <linux/ptrace.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100024#include <linux/slab.h>
25#include <linux/user.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100026#include <linux/interrupt.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100027#include <linux/init.h>
28#include <linux/module.h>
Paul Mackerras8dad3f92005-10-06 13:27:05 +100029#include <linux/prctl.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100030#include <linux/delay.h>
31#include <linux/kprobes.h>
Michael Ellermancc532912005-12-04 18:39:43 +110032#include <linux/kexec.h>
Michael Hanselmann5474c122006-06-25 05:47:08 -070033#include <linux/backlight.h>
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -080034#include <linux/bug.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070035#include <linux/kdebug.h>
Geert Uytterhoeven80947e72009-05-18 02:10:05 +000036#include <linux/debugfs.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100037
Geert Uytterhoeven80947e72009-05-18 02:10:05 +000038#include <asm/emulated_ops.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100039#include <asm/pgtable.h>
40#include <asm/uaccess.h>
41#include <asm/system.h>
42#include <asm/io.h>
Paul Mackerras86417782005-10-10 22:37:57 +100043#include <asm/machdep.h>
44#include <asm/rtas.h>
David Gibsonf7f6f4f2005-10-19 14:53:32 +100045#include <asm/pmc.h>
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +100046#ifdef CONFIG_PPC32
Paul Mackerras14cf11a2005-09-26 16:04:21 +100047#include <asm/reg.h>
Paul Mackerras86417782005-10-10 22:37:57 +100048#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +100049#ifdef CONFIG_PMAC_BACKLIGHT
50#include <asm/backlight.h>
51#endif
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +100052#ifdef CONFIG_PPC64
Paul Mackerras86417782005-10-10 22:37:57 +100053#include <asm/firmware.h>
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +100054#include <asm/processor.h>
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +100055#endif
David Wilderc0ce7d02006-06-23 15:29:34 -070056#include <asm/kexec.h>
Kumar Gala16c57b32009-02-10 20:10:44 +000057#include <asm/ppc-opcode.h>
Kumar Gala620165f2009-02-12 13:54:53 +000058#ifdef CONFIG_FSL_BOOKE
59#include <asm/dbell.h>
60#endif
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +100061
Olof Johansson7dbb9222008-01-31 14:34:47 +110062#if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
Anton Blanchard5be34922010-01-12 00:50:14 +000063int (*__debugger)(struct pt_regs *regs) __read_mostly;
64int (*__debugger_ipi)(struct pt_regs *regs) __read_mostly;
65int (*__debugger_bpt)(struct pt_regs *regs) __read_mostly;
66int (*__debugger_sstep)(struct pt_regs *regs) __read_mostly;
67int (*__debugger_iabr_match)(struct pt_regs *regs) __read_mostly;
68int (*__debugger_dabr_match)(struct pt_regs *regs) __read_mostly;
69int (*__debugger_fault_handler)(struct pt_regs *regs) __read_mostly;
Paul Mackerras14cf11a2005-09-26 16:04:21 +100070
71EXPORT_SYMBOL(__debugger);
72EXPORT_SYMBOL(__debugger_ipi);
73EXPORT_SYMBOL(__debugger_bpt);
74EXPORT_SYMBOL(__debugger_sstep);
75EXPORT_SYMBOL(__debugger_iabr_match);
76EXPORT_SYMBOL(__debugger_dabr_match);
77EXPORT_SYMBOL(__debugger_fault_handler);
78#endif
79
Paul Mackerras14cf11a2005-09-26 16:04:21 +100080/*
81 * Trap & Exception support
82 */
83
anton@samba.org6031d9d2007-03-20 20:38:12 -050084#ifdef CONFIG_PMAC_BACKLIGHT
85static void pmac_backlight_unblank(void)
86{
87 mutex_lock(&pmac_backlight_mutex);
88 if (pmac_backlight) {
89 struct backlight_properties *props;
90
91 props = &pmac_backlight->props;
92 props->brightness = props->max_brightness;
93 props->power = FB_BLANK_UNBLANK;
94 backlight_update_status(pmac_backlight);
95 }
96 mutex_unlock(&pmac_backlight_mutex);
97}
98#else
99static inline void pmac_backlight_unblank(void) { }
100#endif
101
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000102int die(const char *str, struct pt_regs *regs, long err)
103{
anton@samba.org34c2a142007-03-20 20:38:13 -0500104 static struct {
105 spinlock_t lock;
106 u32 lock_owner;
107 int lock_owner_depth;
108 } die = {
109 .lock = __SPIN_LOCK_UNLOCKED(die.lock),
110 .lock_owner = -1,
111 .lock_owner_depth = 0
112 };
David Wilderc0ce7d02006-06-23 15:29:34 -0700113 static int die_counter;
anton@samba.org34c2a142007-03-20 20:38:13 -0500114 unsigned long flags;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000115
116 if (debugger(regs))
117 return 1;
118
anton@samba.org293e4682007-03-20 20:38:11 -0500119 oops_enter();
120
anton@samba.org34c2a142007-03-20 20:38:13 -0500121 if (die.lock_owner != raw_smp_processor_id()) {
122 console_verbose();
123 spin_lock_irqsave(&die.lock, flags);
124 die.lock_owner = smp_processor_id();
125 die.lock_owner_depth = 0;
126 bust_spinlocks(1);
127 if (machine_is(powermac))
128 pmac_backlight_unblank();
129 } else {
130 local_save_flags(flags);
131 }
Michael Hanselmann5474c122006-06-25 05:47:08 -0700132
anton@samba.org34c2a142007-03-20 20:38:13 -0500133 if (++die.lock_owner_depth < 3) {
134 printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000135#ifdef CONFIG_PREEMPT
anton@samba.org34c2a142007-03-20 20:38:13 -0500136 printk("PREEMPT ");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000137#endif
138#ifdef CONFIG_SMP
anton@samba.org34c2a142007-03-20 20:38:13 -0500139 printk("SMP NR_CPUS=%d ", NR_CPUS);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000140#endif
141#ifdef CONFIG_DEBUG_PAGEALLOC
anton@samba.org34c2a142007-03-20 20:38:13 -0500142 printk("DEBUG_PAGEALLOC ");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000143#endif
144#ifdef CONFIG_NUMA
anton@samba.org34c2a142007-03-20 20:38:13 -0500145 printk("NUMA ");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000146#endif
anton@samba.orgae7f4462007-03-20 20:38:14 -0500147 printk("%s\n", ppc_md.name ? ppc_md.name : "");
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100148
Anton Blanchard66fcb102010-02-07 14:44:16 +0000149 sysfs_printk_last_file();
150 if (notify_die(DIE_OOPS, str, regs, err, 255,
151 SIGSEGV) == NOTIFY_STOP)
152 return 1;
153
anton@samba.org34c2a142007-03-20 20:38:13 -0500154 print_modules();
155 show_regs(regs);
156 } else {
157 printk("Recursive die() failure, output suppressed\n");
158 }
159
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000160 bust_spinlocks(0);
anton@samba.org34c2a142007-03-20 20:38:13 -0500161 die.lock_owner = -1;
Pavel Emelianovbcdcd8e2007-07-17 04:03:42 -0700162 add_taint(TAINT_DIE);
anton@samba.org34c2a142007-03-20 20:38:13 -0500163 spin_unlock_irqrestore(&die.lock, flags);
David Wilderc0ce7d02006-06-23 15:29:34 -0700164
165 if (kexec_should_crash(current) ||
166 kexec_sr_activated(smp_processor_id()))
167 crash_kexec(regs);
168 crash_kexec_secondary(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000169
170 if (in_interrupt())
171 panic("Fatal exception in interrupt");
172
Hormscea6a4b2006-07-30 03:03:34 -0700173 if (panic_on_oops)
Horms012c4372006-08-13 23:24:22 -0700174 panic("Fatal exception");
Hormscea6a4b2006-07-30 03:03:34 -0700175
anton@samba.org293e4682007-03-20 20:38:11 -0500176 oops_exit();
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000177 do_exit(err);
178
179 return 0;
180}
181
Oleg Nesterov25baa352009-12-15 16:47:18 -0800182void user_single_step_siginfo(struct task_struct *tsk,
183 struct pt_regs *regs, siginfo_t *info)
184{
185 memset(info, 0, sizeof(*info));
186 info->si_signo = SIGTRAP;
187 info->si_code = TRAP_TRACE;
188 info->si_addr = (void __user *)regs->nip;
189}
190
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000191void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
192{
193 siginfo_t info;
Olof Johanssond0c3d532007-10-12 10:20:07 +1000194 const char fmt32[] = KERN_INFO "%s[%d]: unhandled signal %d " \
195 "at %08lx nip %08lx lr %08lx code %x\n";
196 const char fmt64[] = KERN_INFO "%s[%d]: unhandled signal %d " \
197 "at %016lx nip %016lx lr %016lx code %x\n";
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000198
199 if (!user_mode(regs)) {
200 if (die("Exception in kernel mode", regs, signr))
201 return;
Olof Johanssond0c3d532007-10-12 10:20:07 +1000202 } else if (show_unhandled_signals &&
203 unhandled_signal(current, signr) &&
204 printk_ratelimit()) {
205 printk(regs->msr & MSR_SF ? fmt64 : fmt32,
206 current->comm, current->pid, signr,
207 addr, regs->nip, regs->link, code);
208 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000209
210 memset(&info, 0, sizeof(info));
211 info.si_signo = signr;
212 info.si_code = code;
213 info.si_addr = (void __user *) addr;
214 force_sig_info(signr, &info, current);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000215}
216
217#ifdef CONFIG_PPC64
218void system_reset_exception(struct pt_regs *regs)
219{
220 /* See if any machine dependent calls */
Arnd Bergmannc902be72006-01-04 19:55:53 +0000221 if (ppc_md.system_reset_exception) {
222 if (ppc_md.system_reset_exception(regs))
223 return;
224 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000225
David Wilderc0ce7d02006-06-23 15:29:34 -0700226#ifdef CONFIG_KEXEC
227 cpu_set(smp_processor_id(), cpus_in_sr);
228#endif
229
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000230 die("System Reset", regs, SIGABRT);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000231
David Wildereac83922006-06-29 15:17:30 -0700232 /*
233 * Some CPUs when released from the debugger will execute this path.
234 * These CPUs entered the debugger via a soft-reset. If the CPU was
235 * hung before entering the debugger it will return to the hung
236 * state when exiting this function. This causes a problem in
237 * kdump since the hung CPU(s) will not respond to the IPI sent
238 * from kdump. To prevent the problem we call crash_kexec_secondary()
239 * here. If a kdump had not been initiated or we exit the debugger
240 * with the "exit and recover" command (x) crash_kexec_secondary()
241 * will return after 5ms and the CPU returns to its previous state.
242 */
243 crash_kexec_secondary(regs);
244
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000245 /* Must die if the interrupt is not recoverable */
246 if (!(regs->msr & MSR_RI))
247 panic("Unrecoverable System Reset");
248
249 /* What should we do here? We could issue a shutdown or hard reset. */
250}
251#endif
252
253/*
254 * I/O accesses can cause machine checks on powermacs.
255 * Check if the NIP corresponds to the address of a sync
256 * instruction for which there is an entry in the exception
257 * table.
258 * Note that the 601 only takes a machine check on TEA
259 * (transfer error ack) signal assertion, and does not
260 * set any of the top 16 bits of SRR1.
261 * -- paulus.
262 */
263static inline int check_io_access(struct pt_regs *regs)
264{
Benjamin Herrenschmidt68a64352006-11-13 09:27:39 +1100265#ifdef CONFIG_PPC32
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000266 unsigned long msr = regs->msr;
267 const struct exception_table_entry *entry;
268 unsigned int *nip = (unsigned int *)regs->nip;
269
270 if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
271 && (entry = search_exception_tables(regs->nip)) != NULL) {
272 /*
273 * Check that it's a sync instruction, or somewhere
274 * in the twi; isync; nop sequence that inb/inw/inl uses.
275 * As the address is in the exception table
276 * we should be able to read the instr there.
277 * For the debug message, we look at the preceding
278 * load or store.
279 */
280 if (*nip == 0x60000000) /* nop */
281 nip -= 2;
282 else if (*nip == 0x4c00012c) /* isync */
283 --nip;
284 if (*nip == 0x7c0004ac || (*nip >> 26) == 3) {
285 /* sync or twi */
286 unsigned int rb;
287
288 --nip;
289 rb = (*nip >> 11) & 0x1f;
290 printk(KERN_DEBUG "%s bad port %lx at %p\n",
291 (*nip & 0x100)? "OUT to": "IN from",
292 regs->gpr[rb] - _IO_BASE, nip);
293 regs->msr |= MSR_RI;
294 regs->nip = entry->fixup;
295 return 1;
296 }
297 }
Benjamin Herrenschmidt68a64352006-11-13 09:27:39 +1100298#endif /* CONFIG_PPC32 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000299 return 0;
300}
301
302#if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
303/* On 4xx, the reason for the machine check or program exception
304 is in the ESR. */
305#define get_reason(regs) ((regs)->dsisr)
306#ifndef CONFIG_FSL_BOOKE
307#define get_mc_reason(regs) ((regs)->dsisr)
308#else
Becky Bruce86d7a9a2007-08-02 15:37:15 -0500309#define get_mc_reason(regs) (mfspr(SPRN_MCSR) & MCSR_MASK)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000310#endif
311#define REASON_FP ESR_FP
312#define REASON_ILLEGAL (ESR_PIL | ESR_PUO)
313#define REASON_PRIVILEGED ESR_PPR
314#define REASON_TRAP ESR_PTR
315
316/* single-step stuff */
317#define single_stepping(regs) (current->thread.dbcr0 & DBCR0_IC)
318#define clear_single_step(regs) (current->thread.dbcr0 &= ~DBCR0_IC)
319
320#else
321/* On non-4xx, the reason for the machine check or program
322 exception is in the MSR. */
323#define get_reason(regs) ((regs)->msr)
324#define get_mc_reason(regs) ((regs)->msr)
325#define REASON_FP 0x100000
326#define REASON_ILLEGAL 0x80000
327#define REASON_PRIVILEGED 0x40000
328#define REASON_TRAP 0x20000
329
330#define single_stepping(regs) ((regs)->msr & MSR_SE)
331#define clear_single_step(regs) ((regs)->msr &= ~MSR_SE)
332#endif
333
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100334#if defined(CONFIG_4xx)
335int machine_check_4xx(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000336{
Kumar Gala1a6a4ff2006-03-30 21:11:15 -0600337 unsigned long reason = get_mc_reason(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000338
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000339 if (reason & ESR_IMCP) {
340 printk("Instruction");
341 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
342 } else
343 printk("Data");
344 printk(" machine check in kernel mode.\n");
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100345
346 return 0;
347}
348
349int machine_check_440A(struct pt_regs *regs)
350{
351 unsigned long reason = get_mc_reason(regs);
352
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000353 printk("Machine check in kernel mode.\n");
354 if (reason & ESR_IMCP){
355 printk("Instruction Synchronous Machine Check exception\n");
356 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
357 }
358 else {
359 u32 mcsr = mfspr(SPRN_MCSR);
360 if (mcsr & MCSR_IB)
361 printk("Instruction Read PLB Error\n");
362 if (mcsr & MCSR_DRB)
363 printk("Data Read PLB Error\n");
364 if (mcsr & MCSR_DWB)
365 printk("Data Write PLB Error\n");
366 if (mcsr & MCSR_TLBP)
367 printk("TLB Parity Error\n");
368 if (mcsr & MCSR_ICP){
369 flush_instruction_cache();
370 printk("I-Cache Parity Error\n");
371 }
372 if (mcsr & MCSR_DCSP)
373 printk("D-Cache Search Parity Error\n");
374 if (mcsr & MCSR_DCFP)
375 printk("D-Cache Flush Parity Error\n");
376 if (mcsr & MCSR_IMPE)
377 printk("Machine Check exception is imprecise\n");
378
379 /* Clear MCSR */
380 mtspr(SPRN_MCSR, mcsr);
381 }
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100382 return 0;
383}
384#elif defined(CONFIG_E500)
385int machine_check_e500(struct pt_regs *regs)
386{
387 unsigned long reason = get_mc_reason(regs);
388
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000389 printk("Machine check in kernel mode.\n");
390 printk("Caused by (from MCSR=%lx): ", reason);
391
392 if (reason & MCSR_MCP)
393 printk("Machine Check Signal\n");
394 if (reason & MCSR_ICPERR)
395 printk("Instruction Cache Parity Error\n");
396 if (reason & MCSR_DCP_PERR)
397 printk("Data Cache Push Parity Error\n");
398 if (reason & MCSR_DCPERR)
399 printk("Data Cache Parity Error\n");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000400 if (reason & MCSR_BUS_IAERR)
401 printk("Bus - Instruction Address Error\n");
402 if (reason & MCSR_BUS_RAERR)
403 printk("Bus - Read Address Error\n");
404 if (reason & MCSR_BUS_WAERR)
405 printk("Bus - Write Address Error\n");
406 if (reason & MCSR_BUS_IBERR)
407 printk("Bus - Instruction Data Error\n");
408 if (reason & MCSR_BUS_RBERR)
409 printk("Bus - Read Data Bus Error\n");
410 if (reason & MCSR_BUS_WBERR)
411 printk("Bus - Read Data Bus Error\n");
412 if (reason & MCSR_BUS_IPERR)
413 printk("Bus - Instruction Parity Error\n");
414 if (reason & MCSR_BUS_RPERR)
415 printk("Bus - Read Parity Error\n");
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100416
417 return 0;
418}
419#elif defined(CONFIG_E200)
420int machine_check_e200(struct pt_regs *regs)
421{
422 unsigned long reason = get_mc_reason(regs);
423
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000424 printk("Machine check in kernel mode.\n");
425 printk("Caused by (from MCSR=%lx): ", reason);
426
427 if (reason & MCSR_MCP)
428 printk("Machine Check Signal\n");
429 if (reason & MCSR_CP_PERR)
430 printk("Cache Push Parity Error\n");
431 if (reason & MCSR_CPERR)
432 printk("Cache Parity Error\n");
433 if (reason & MCSR_EXCP_ERR)
434 printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
435 if (reason & MCSR_BUS_IRERR)
436 printk("Bus - Read Bus Error on instruction fetch\n");
437 if (reason & MCSR_BUS_DRERR)
438 printk("Bus - Read Bus Error on data load\n");
439 if (reason & MCSR_BUS_WRERR)
440 printk("Bus - Write Bus Error on buffered store or cache line push\n");
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100441
442 return 0;
443}
444#else
445int machine_check_generic(struct pt_regs *regs)
446{
447 unsigned long reason = get_mc_reason(regs);
448
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000449 printk("Machine check in kernel mode.\n");
450 printk("Caused by (from SRR1=%lx): ", reason);
451 switch (reason & 0x601F0000) {
452 case 0x80000:
453 printk("Machine check signal\n");
454 break;
455 case 0: /* for 601 */
456 case 0x40000:
457 case 0x140000: /* 7450 MSS error and TEA */
458 printk("Transfer error ack signal\n");
459 break;
460 case 0x20000:
461 printk("Data parity error signal\n");
462 break;
463 case 0x10000:
464 printk("Address parity error signal\n");
465 break;
466 case 0x20000000:
467 printk("L1 Data Cache error\n");
468 break;
469 case 0x40000000:
470 printk("L1 Instruction Cache error\n");
471 break;
472 case 0x00100000:
473 printk("L2 data cache parity error\n");
474 break;
475 default:
476 printk("Unknown values in msr\n");
477 }
Olof Johansson75918a42007-09-21 05:11:20 +1000478 return 0;
479}
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100480#endif /* everything else */
Olof Johansson75918a42007-09-21 05:11:20 +1000481
482void machine_check_exception(struct pt_regs *regs)
483{
484 int recover = 0;
485
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100486 /* See if any machine dependent calls. In theory, we would want
487 * to call the CPU first, and call the ppc_md. one if the CPU
488 * one returns a positive number. However there is existing code
489 * that assumes the board gets a first chance, so let's keep it
490 * that way for now and fix things later. --BenH.
491 */
Olof Johansson75918a42007-09-21 05:11:20 +1000492 if (ppc_md.machine_check_exception)
493 recover = ppc_md.machine_check_exception(regs);
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100494 else if (cur_cpu_spec->machine_check)
495 recover = cur_cpu_spec->machine_check(regs);
Olof Johansson75918a42007-09-21 05:11:20 +1000496
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100497 if (recover > 0)
Olof Johansson75918a42007-09-21 05:11:20 +1000498 return;
499
500 if (user_mode(regs)) {
501 regs->msr |= MSR_RI;
502 _exception(SIGBUS, regs, BUS_ADRERR, regs->nip);
503 return;
504 }
505
506#if defined(CONFIG_8xx) && defined(CONFIG_PCI)
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100507 /* the qspan pci read routines can cause machine checks -- Cort
508 *
509 * yuck !!! that totally needs to go away ! There are better ways
510 * to deal with that than having a wart in the mcheck handler.
511 * -- BenH
512 */
Olof Johansson75918a42007-09-21 05:11:20 +1000513 bad_page_fault(regs, regs->dar, SIGBUS);
514 return;
515#endif
516
517 if (debugger_fault_handler(regs)) {
518 regs->msr |= MSR_RI;
519 return;
520 }
521
522 if (check_io_access(regs))
523 return;
524
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000525 if (debugger_fault_handler(regs))
526 return;
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000527 die("Machine check", regs, SIGBUS);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000528
529 /* Must die if the interrupt is not recoverable */
530 if (!(regs->msr & MSR_RI))
531 panic("Unrecoverable Machine check");
532}
533
534void SMIException(struct pt_regs *regs)
535{
536 die("System Management Interrupt", regs, SIGABRT);
537}
538
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000539void unknown_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000540{
541 printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
542 regs->nip, regs->msr, regs->trap);
543
544 _exception(SIGTRAP, regs, 0, 0);
545}
546
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000547void instruction_breakpoint_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000548{
549 if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
550 5, SIGTRAP) == NOTIFY_STOP)
551 return;
552 if (debugger_iabr_match(regs))
553 return;
554 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
555}
556
557void RunModeException(struct pt_regs *regs)
558{
559 _exception(SIGTRAP, regs, 0, 0);
560}
561
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000562void __kprobes single_step_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000563{
564 regs->msr &= ~(MSR_SE | MSR_BE); /* Turn off 'trace' bits */
565
566 if (notify_die(DIE_SSTEP, "single_step", regs, 5,
567 5, SIGTRAP) == NOTIFY_STOP)
568 return;
569 if (debugger_sstep(regs))
570 return;
571
572 _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
573}
574
575/*
576 * After we have successfully emulated an instruction, we have to
577 * check if the instruction was being single-stepped, and if so,
578 * pretend we got a single-step exception. This was pointed out
579 * by Kumar Gala. -- paulus
580 */
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000581static void emulate_single_step(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000582{
583 if (single_stepping(regs)) {
584 clear_single_step(regs);
585 _exception(SIGTRAP, regs, TRAP_TRACE, 0);
586 }
587}
588
Kumar Gala5fad2932007-02-07 01:47:59 -0600589static inline int __parse_fpscr(unsigned long fpscr)
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000590{
Kumar Gala5fad2932007-02-07 01:47:59 -0600591 int ret = 0;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000592
593 /* Invalid operation */
594 if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600595 ret = FPE_FLTINV;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000596
597 /* Overflow */
598 else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600599 ret = FPE_FLTOVF;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000600
601 /* Underflow */
602 else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600603 ret = FPE_FLTUND;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000604
605 /* Divide by zero */
606 else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600607 ret = FPE_FLTDIV;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000608
609 /* Inexact result */
610 else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600611 ret = FPE_FLTRES;
612
613 return ret;
614}
615
616static void parse_fpe(struct pt_regs *regs)
617{
618 int code = 0;
619
620 flush_fp_to_thread(current);
621
622 code = __parse_fpscr(current->thread.fpscr.val);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000623
624 _exception(SIGFPE, regs, code, regs->nip);
625}
626
627/*
628 * Illegal instruction emulation support. Originally written to
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000629 * provide the PVR to user applications using the mfspr rd, PVR.
630 * Return non-zero if we can't emulate, or -EFAULT if the associated
631 * memory access caused an access fault. Return zero on success.
632 *
633 * There are a couple of ways to do this, either "decode" the instruction
634 * or directly match lots of bits. In this case, matching lots of
635 * bits is faster and easier.
Paul Mackerras86417782005-10-10 22:37:57 +1000636 *
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000637 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000638static int emulate_string_inst(struct pt_regs *regs, u32 instword)
639{
640 u8 rT = (instword >> 21) & 0x1f;
641 u8 rA = (instword >> 16) & 0x1f;
642 u8 NB_RB = (instword >> 11) & 0x1f;
643 u32 num_bytes;
644 unsigned long EA;
645 int pos = 0;
646
647 /* Early out if we are an invalid form of lswx */
Kumar Gala16c57b32009-02-10 20:10:44 +0000648 if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000649 if ((rT == rA) || (rT == NB_RB))
650 return -EINVAL;
651
652 EA = (rA == 0) ? 0 : regs->gpr[rA];
653
Kumar Gala16c57b32009-02-10 20:10:44 +0000654 switch (instword & PPC_INST_STRING_MASK) {
655 case PPC_INST_LSWX:
656 case PPC_INST_STSWX:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000657 EA += NB_RB;
658 num_bytes = regs->xer & 0x7f;
659 break;
Kumar Gala16c57b32009-02-10 20:10:44 +0000660 case PPC_INST_LSWI:
661 case PPC_INST_STSWI:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000662 num_bytes = (NB_RB == 0) ? 32 : NB_RB;
663 break;
664 default:
665 return -EINVAL;
666 }
667
668 while (num_bytes != 0)
669 {
670 u8 val;
671 u32 shift = 8 * (3 - (pos & 0x3));
672
Kumar Gala16c57b32009-02-10 20:10:44 +0000673 switch ((instword & PPC_INST_STRING_MASK)) {
674 case PPC_INST_LSWX:
675 case PPC_INST_LSWI:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000676 if (get_user(val, (u8 __user *)EA))
677 return -EFAULT;
678 /* first time updating this reg,
679 * zero it out */
680 if (pos == 0)
681 regs->gpr[rT] = 0;
682 regs->gpr[rT] |= val << shift;
683 break;
Kumar Gala16c57b32009-02-10 20:10:44 +0000684 case PPC_INST_STSWI:
685 case PPC_INST_STSWX:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000686 val = regs->gpr[rT] >> shift;
687 if (put_user(val, (u8 __user *)EA))
688 return -EFAULT;
689 break;
690 }
691 /* move EA to next address */
692 EA += 1;
693 num_bytes--;
694
695 /* manage our position within the register */
696 if (++pos == 4) {
697 pos = 0;
698 if (++rT == 32)
699 rT = 0;
700 }
701 }
702
703 return 0;
704}
705
Will Schmidtc3412dc2006-08-30 13:11:38 -0500706static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
707{
708 u32 ra,rs;
709 unsigned long tmp;
710
711 ra = (instword >> 16) & 0x1f;
712 rs = (instword >> 21) & 0x1f;
713
714 tmp = regs->gpr[rs];
715 tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
716 tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
717 tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
718 regs->gpr[ra] = tmp;
719
720 return 0;
721}
722
Kumar Galac1469f12007-11-19 21:35:29 -0600723static int emulate_isel(struct pt_regs *regs, u32 instword)
724{
725 u8 rT = (instword >> 21) & 0x1f;
726 u8 rA = (instword >> 16) & 0x1f;
727 u8 rB = (instword >> 11) & 0x1f;
728 u8 BC = (instword >> 6) & 0x1f;
729 u8 bit;
730 unsigned long tmp;
731
732 tmp = (rA == 0) ? 0 : regs->gpr[rA];
733 bit = (regs->ccr >> (31 - BC)) & 0x1;
734
735 regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
736
737 return 0;
738}
739
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000740static int emulate_instruction(struct pt_regs *regs)
741{
742 u32 instword;
743 u32 rd;
744
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000745 if (!user_mode(regs) || (regs->msr & MSR_LE))
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000746 return -EINVAL;
747 CHECK_FULL_REGS(regs);
748
749 if (get_user(instword, (u32 __user *)(regs->nip)))
750 return -EFAULT;
751
752 /* Emulate the mfspr rD, PVR. */
Kumar Gala16c57b32009-02-10 20:10:44 +0000753 if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000754 PPC_WARN_EMULATED(mfpvr, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000755 rd = (instword >> 21) & 0x1f;
756 regs->gpr[rd] = mfspr(SPRN_PVR);
757 return 0;
758 }
759
760 /* Emulating the dcba insn is just a no-op. */
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000761 if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000762 PPC_WARN_EMULATED(dcba, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000763 return 0;
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000764 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000765
766 /* Emulate the mcrxr insn. */
Kumar Gala16c57b32009-02-10 20:10:44 +0000767 if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
Paul Mackerras86417782005-10-10 22:37:57 +1000768 int shift = (instword >> 21) & 0x1c;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000769 unsigned long msk = 0xf0000000UL >> shift;
770
Anton Blanchardeecff812009-10-27 18:46:55 +0000771 PPC_WARN_EMULATED(mcrxr, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000772 regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
773 regs->xer &= ~0xf0000000UL;
774 return 0;
775 }
776
777 /* Emulate load/store string insn. */
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000778 if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000779 PPC_WARN_EMULATED(string, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000780 return emulate_string_inst(regs, instword);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000781 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000782
Will Schmidtc3412dc2006-08-30 13:11:38 -0500783 /* Emulate the popcntb (Population Count Bytes) instruction. */
Kumar Gala16c57b32009-02-10 20:10:44 +0000784 if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000785 PPC_WARN_EMULATED(popcntb, regs);
Will Schmidtc3412dc2006-08-30 13:11:38 -0500786 return emulate_popcntb_inst(regs, instword);
787 }
788
Kumar Galac1469f12007-11-19 21:35:29 -0600789 /* Emulate isel (Integer Select) instruction */
Kumar Gala16c57b32009-02-10 20:10:44 +0000790 if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000791 PPC_WARN_EMULATED(isel, regs);
Kumar Galac1469f12007-11-19 21:35:29 -0600792 return emulate_isel(regs, instword);
793 }
794
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000795 return -EINVAL;
796}
797
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -0800798int is_valid_bugaddr(unsigned long addr)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000799{
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -0800800 return is_kernel_addr(addr);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000801}
802
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000803void __kprobes program_check_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000804{
805 unsigned int reason = get_reason(regs);
806 extern int do_mathemu(struct pt_regs *regs);
807
Kim Phillipsaa42c692006-12-08 02:43:30 -0600808 /* We can now get here via a FP Unavailable exception if the core
Kumar Gala04903a32007-02-07 01:13:32 -0600809 * has no FPU, in that case the reason flags will be 0 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000810
811 if (reason & REASON_FP) {
812 /* IEEE FP exception */
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000813 parse_fpe(regs);
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000814 return;
815 }
816 if (reason & REASON_TRAP) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000817 /* trap exception */
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000818 if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
819 == NOTIFY_STOP)
820 return;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000821 if (debugger_bpt(regs))
822 return;
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -0800823
824 if (!(regs->msr & MSR_PR) && /* not user-mode */
Heiko Carstens608e2612007-07-15 23:41:39 -0700825 report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000826 regs->nip += 4;
827 return;
828 }
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000829 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
830 return;
831 }
832
Paul Mackerrascd8a5672006-03-03 17:11:40 +1100833 local_irq_enable();
834
Kumar Gala04903a32007-02-07 01:13:32 -0600835#ifdef CONFIG_MATH_EMULATION
836 /* (reason & REASON_ILLEGAL) would be the obvious thing here,
837 * but there seems to be a hardware bug on the 405GP (RevD)
838 * that means ESR is sometimes set incorrectly - either to
839 * ESR_DST (!?) or 0. In the process of chasing this with the
840 * hardware people - not sure if it can happen on any illegal
841 * instruction or only on FP instructions, whether there is a
842 * pattern to occurences etc. -dgibson 31/Mar/2003 */
Kumar Gala5fad2932007-02-07 01:47:59 -0600843 switch (do_mathemu(regs)) {
844 case 0:
Kumar Gala04903a32007-02-07 01:13:32 -0600845 emulate_single_step(regs);
846 return;
Kumar Gala5fad2932007-02-07 01:47:59 -0600847 case 1: {
848 int code = 0;
849 code = __parse_fpscr(current->thread.fpscr.val);
850 _exception(SIGFPE, regs, code, regs->nip);
851 return;
852 }
853 case -EFAULT:
854 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
855 return;
Kumar Gala04903a32007-02-07 01:13:32 -0600856 }
Kumar Gala5fad2932007-02-07 01:47:59 -0600857 /* fall through on any other errors */
Kumar Gala04903a32007-02-07 01:13:32 -0600858#endif /* CONFIG_MATH_EMULATION */
859
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000860 /* Try to emulate it if we should. */
861 if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000862 switch (emulate_instruction(regs)) {
863 case 0:
864 regs->nip += 4;
865 emulate_single_step(regs);
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000866 return;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000867 case -EFAULT:
868 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000869 return;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000870 }
871 }
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000872
873 if (reason & REASON_PRIVILEGED)
874 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
875 else
876 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000877}
878
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000879void alignment_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000880{
Benjamin Herrenschmidt4393c4f2006-11-01 15:11:39 +1100881 int sig, code, fixed = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000882
Paul Mackerrase9370ae2006-06-07 16:15:39 +1000883 /* we don't implement logging of alignment exceptions */
884 if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
885 fixed = fix_alignment(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000886
887 if (fixed == 1) {
888 regs->nip += 4; /* skip over emulated instruction */
889 emulate_single_step(regs);
890 return;
891 }
892
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000893 /* Operand address was bad */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000894 if (fixed == -EFAULT) {
Benjamin Herrenschmidt4393c4f2006-11-01 15:11:39 +1100895 sig = SIGSEGV;
896 code = SEGV_ACCERR;
897 } else {
898 sig = SIGBUS;
899 code = BUS_ADRALN;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000900 }
Benjamin Herrenschmidt4393c4f2006-11-01 15:11:39 +1100901 if (user_mode(regs))
902 _exception(sig, regs, code, regs->dar);
903 else
904 bad_page_fault(regs, regs->dar, sig);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000905}
906
907void StackOverflow(struct pt_regs *regs)
908{
909 printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
910 current, regs->gpr[1]);
911 debugger(regs);
912 show_regs(regs);
913 panic("kernel stack overflow");
914}
915
916void nonrecoverable_exception(struct pt_regs *regs)
917{
918 printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
919 regs->nip, regs->msr);
920 debugger(regs);
921 die("nonrecoverable exception", regs, SIGKILL);
922}
923
924void trace_syscall(struct pt_regs *regs)
925{
926 printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld %s\n",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700927 current, task_pid_nr(current), regs->nip, regs->link, regs->gpr[0],
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000928 regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
929}
930
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000931void kernel_fp_unavailable_exception(struct pt_regs *regs)
932{
933 printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
934 "%lx at %lx\n", regs->trap, regs->nip);
935 die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
936}
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000937
938void altivec_unavailable_exception(struct pt_regs *regs)
939{
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000940 if (user_mode(regs)) {
941 /* A user program has executed an altivec instruction,
942 but this kernel doesn't support altivec. */
943 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
944 return;
945 }
Anton Blanchard6c4841c2006-10-13 11:41:00 +1000946
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000947 printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
948 "%lx at %lx\n", regs->trap, regs->nip);
949 die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000950}
951
Michael Neulingce48b212008-06-25 14:07:18 +1000952void vsx_unavailable_exception(struct pt_regs *regs)
953{
954 if (user_mode(regs)) {
955 /* A user program has executed an vsx instruction,
956 but this kernel doesn't support vsx. */
957 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
958 return;
959 }
960
961 printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
962 "%lx at %lx\n", regs->trap, regs->nip);
963 die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
964}
965
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000966void performance_monitor_exception(struct pt_regs *regs)
967{
968 perf_irq(regs);
969}
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000970
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000971#ifdef CONFIG_8xx
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000972void SoftwareEmulation(struct pt_regs *regs)
973{
974 extern int do_mathemu(struct pt_regs *);
975 extern int Soft_emulate_8xx(struct pt_regs *);
Scott Wood5dd57a12007-09-18 15:29:35 -0500976#if defined(CONFIG_MATH_EMULATION) || defined(CONFIG_8XX_MINIMAL_FPEMU)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000977 int errcode;
Scott Wood5dd57a12007-09-18 15:29:35 -0500978#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000979
980 CHECK_FULL_REGS(regs);
981
982 if (!user_mode(regs)) {
983 debugger(regs);
984 die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
985 }
986
987#ifdef CONFIG_MATH_EMULATION
988 errcode = do_mathemu(regs);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000989 if (errcode >= 0)
Anton Blanchardeecff812009-10-27 18:46:55 +0000990 PPC_WARN_EMULATED(math, regs);
Kumar Gala5fad2932007-02-07 01:47:59 -0600991
992 switch (errcode) {
993 case 0:
994 emulate_single_step(regs);
995 return;
996 case 1: {
997 int code = 0;
998 code = __parse_fpscr(current->thread.fpscr.val);
999 _exception(SIGFPE, regs, code, regs->nip);
1000 return;
1001 }
1002 case -EFAULT:
1003 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1004 return;
1005 default:
1006 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1007 return;
1008 }
1009
Scott Wood5dd57a12007-09-18 15:29:35 -05001010#elif defined(CONFIG_8XX_MINIMAL_FPEMU)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001011 errcode = Soft_emulate_8xx(regs);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001012 if (errcode >= 0)
Anton Blanchardeecff812009-10-27 18:46:55 +00001013 PPC_WARN_EMULATED(8xx, regs);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001014
Kumar Gala5fad2932007-02-07 01:47:59 -06001015 switch (errcode) {
1016 case 0:
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001017 emulate_single_step(regs);
Kumar Gala5fad2932007-02-07 01:47:59 -06001018 return;
1019 case 1:
1020 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1021 return;
1022 case -EFAULT:
1023 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1024 return;
1025 }
Scott Wood5dd57a12007-09-18 15:29:35 -05001026#else
1027 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
Kumar Gala5fad2932007-02-07 01:47:59 -06001028#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001029}
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001030#endif /* CONFIG_8xx */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001031
1032#if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
1033
Kumar Galaf8279622008-06-26 02:01:37 -05001034void __kprobes DebugException(struct pt_regs *regs, unsigned long debug_status)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001035{
Roland McGrathec097c82009-05-28 21:26:38 +00001036 /* Hack alert: On BookE, Branch Taken stops on the branch itself, while
1037 * on server, it stops on the target of the branch. In order to simulate
1038 * the server behaviour, we thus restart right away with a single step
1039 * instead of stopping here when hitting a BT
1040 */
1041 if (debug_status & DBSR_BT) {
1042 regs->msr &= ~MSR_DE;
1043
1044 /* Disable BT */
1045 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
1046 /* Clear the BT event */
1047 mtspr(SPRN_DBSR, DBSR_BT);
1048
1049 /* Do the single step trick only when coming from userspace */
1050 if (user_mode(regs)) {
1051 current->thread.dbcr0 &= ~DBCR0_BT;
1052 current->thread.dbcr0 |= DBCR0_IDM | DBCR0_IC;
1053 regs->msr |= MSR_DE;
1054 return;
1055 }
1056
1057 if (notify_die(DIE_SSTEP, "block_step", regs, 5,
1058 5, SIGTRAP) == NOTIFY_STOP) {
1059 return;
1060 }
1061 if (debugger_sstep(regs))
1062 return;
1063 } else if (debug_status & DBSR_IC) { /* Instruction complete */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001064 regs->msr &= ~MSR_DE;
Kumar Galaf8279622008-06-26 02:01:37 -05001065
1066 /* Disable instruction completion */
1067 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
1068 /* Clear the instruction completion event */
1069 mtspr(SPRN_DBSR, DBSR_IC);
1070
1071 if (notify_die(DIE_SSTEP, "single_step", regs, 5,
1072 5, SIGTRAP) == NOTIFY_STOP) {
1073 return;
1074 }
1075
1076 if (debugger_sstep(regs))
1077 return;
1078
Roland McGrathec097c82009-05-28 21:26:38 +00001079 if (user_mode(regs))
1080 current->thread.dbcr0 &= ~(DBCR0_IC);
Kumar Galaf8279622008-06-26 02:01:37 -05001081
1082 _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
Luis Machadod6a61bf2008-07-24 02:10:41 +10001083 } else if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
1084 regs->msr &= ~MSR_DE;
1085
1086 if (user_mode(regs)) {
1087 current->thread.dbcr0 &= ~(DBSR_DAC1R | DBSR_DAC1W |
1088 DBCR0_IDM);
1089 } else {
1090 /* Disable DAC interupts */
1091 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~(DBSR_DAC1R |
1092 DBSR_DAC1W | DBCR0_IDM));
1093
1094 /* Clear the DAC event */
1095 mtspr(SPRN_DBSR, (DBSR_DAC1R | DBSR_DAC1W));
1096 }
1097 /* Setup and send the trap to the handler */
1098 do_dabr(regs, mfspr(SPRN_DAC1), debug_status);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001099 }
1100}
1101#endif /* CONFIG_4xx || CONFIG_BOOKE */
1102
1103#if !defined(CONFIG_TAU_INT)
1104void TAUException(struct pt_regs *regs)
1105{
1106 printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx %s\n",
1107 regs->nip, regs->msr, regs->trap, print_tainted());
1108}
1109#endif /* CONFIG_INT_TAU */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001110
1111#ifdef CONFIG_ALTIVEC
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001112void altivec_assist_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001113{
1114 int err;
1115
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001116 if (!user_mode(regs)) {
1117 printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
1118 " at %lx\n", regs->nip);
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001119 die("Kernel VMX/Altivec assist exception", regs, SIGILL);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001120 }
1121
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001122 flush_altivec_to_thread(current);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001123
Anton Blanchardeecff812009-10-27 18:46:55 +00001124 PPC_WARN_EMULATED(altivec, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001125 err = emulate_altivec(regs);
1126 if (err == 0) {
1127 regs->nip += 4; /* skip emulated instruction */
1128 emulate_single_step(regs);
1129 return;
1130 }
1131
1132 if (err == -EFAULT) {
1133 /* got an error reading the instruction */
1134 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1135 } else {
1136 /* didn't recognize the instruction */
1137 /* XXX quick hack for now: set the non-Java bit in the VSCR */
1138 if (printk_ratelimit())
1139 printk(KERN_ERR "Unrecognized altivec instruction "
1140 "in %s at %lx\n", current->comm, regs->nip);
1141 current->thread.vscr.u[3] |= 0x10000;
1142 }
1143}
1144#endif /* CONFIG_ALTIVEC */
1145
Michael Neulingce48b212008-06-25 14:07:18 +10001146#ifdef CONFIG_VSX
1147void vsx_assist_exception(struct pt_regs *regs)
1148{
1149 if (!user_mode(regs)) {
1150 printk(KERN_EMERG "VSX assist exception in kernel mode"
1151 " at %lx\n", regs->nip);
1152 die("Kernel VSX assist exception", regs, SIGILL);
1153 }
1154
1155 flush_vsx_to_thread(current);
1156 printk(KERN_INFO "VSX assist not supported at %lx\n", regs->nip);
1157 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1158}
1159#endif /* CONFIG_VSX */
1160
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001161#ifdef CONFIG_FSL_BOOKE
Kumar Gala620165f2009-02-12 13:54:53 +00001162
1163void doorbell_exception(struct pt_regs *regs)
1164{
1165#ifdef CONFIG_SMP
1166 int cpu = smp_processor_id();
1167 int msg;
1168
1169 if (num_online_cpus() < 2)
1170 return;
1171
1172 for (msg = 0; msg < 4; msg++)
1173 if (test_and_clear_bit(msg, &dbell_smp_message[cpu]))
1174 smp_message_recv(msg);
1175#else
1176 printk(KERN_WARNING "Received doorbell on non-smp system\n");
1177#endif
1178}
1179
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001180void CacheLockingException(struct pt_regs *regs, unsigned long address,
1181 unsigned long error_code)
1182{
1183 /* We treat cache locking instructions from the user
1184 * as priv ops, in the future we could try to do
1185 * something smarter
1186 */
1187 if (error_code & (ESR_DLK|ESR_ILK))
1188 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
1189 return;
1190}
1191#endif /* CONFIG_FSL_BOOKE */
1192
1193#ifdef CONFIG_SPE
1194void SPEFloatingPointException(struct pt_regs *regs)
1195{
Liu Yu6a800f32008-10-28 11:50:21 +08001196 extern int do_spe_mathemu(struct pt_regs *regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001197 unsigned long spefscr;
1198 int fpexc_mode;
1199 int code = 0;
Liu Yu6a800f32008-10-28 11:50:21 +08001200 int err;
1201
1202 preempt_disable();
1203 if (regs->msr & MSR_SPE)
1204 giveup_spe(current);
1205 preempt_enable();
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001206
1207 spefscr = current->thread.spefscr;
1208 fpexc_mode = current->thread.fpexc_mode;
1209
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001210 if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
1211 code = FPE_FLTOVF;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001212 }
1213 else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
1214 code = FPE_FLTUND;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001215 }
1216 else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
1217 code = FPE_FLTDIV;
1218 else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
1219 code = FPE_FLTINV;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001220 }
1221 else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
1222 code = FPE_FLTRES;
1223
Liu Yu6a800f32008-10-28 11:50:21 +08001224 err = do_spe_mathemu(regs);
1225 if (err == 0) {
1226 regs->nip += 4; /* skip emulated instruction */
1227 emulate_single_step(regs);
1228 return;
1229 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001230
Liu Yu6a800f32008-10-28 11:50:21 +08001231 if (err == -EFAULT) {
1232 /* got an error reading the instruction */
1233 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1234 } else if (err == -EINVAL) {
1235 /* didn't recognize the instruction */
1236 printk(KERN_ERR "unrecognized spe instruction "
1237 "in %s at %lx\n", current->comm, regs->nip);
1238 } else {
1239 _exception(SIGFPE, regs, code, regs->nip);
1240 }
1241
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001242 return;
1243}
Liu Yu6a800f32008-10-28 11:50:21 +08001244
1245void SPEFloatingPointRoundException(struct pt_regs *regs)
1246{
1247 extern int speround_handler(struct pt_regs *regs);
1248 int err;
1249
1250 preempt_disable();
1251 if (regs->msr & MSR_SPE)
1252 giveup_spe(current);
1253 preempt_enable();
1254
1255 regs->nip -= 4;
1256 err = speround_handler(regs);
1257 if (err == 0) {
1258 regs->nip += 4; /* skip emulated instruction */
1259 emulate_single_step(regs);
1260 return;
1261 }
1262
1263 if (err == -EFAULT) {
1264 /* got an error reading the instruction */
1265 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1266 } else if (err == -EINVAL) {
1267 /* didn't recognize the instruction */
1268 printk(KERN_ERR "unrecognized spe instruction "
1269 "in %s at %lx\n", current->comm, regs->nip);
1270 } else {
1271 _exception(SIGFPE, regs, 0, regs->nip);
1272 return;
1273 }
1274}
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001275#endif
1276
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001277/*
1278 * We enter here if we get an unrecoverable exception, that is, one
1279 * that happened at a point where the RI (recoverable interrupt) bit
1280 * in the MSR is 0. This indicates that SRR0/1 are live, and that
1281 * we therefore lost state by taking this exception.
1282 */
1283void unrecoverable_exception(struct pt_regs *regs)
1284{
1285 printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
1286 regs->trap, regs->nip);
1287 die("Unrecoverable exception", regs, SIGABRT);
1288}
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001289
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001290#ifdef CONFIG_BOOKE_WDT
1291/*
1292 * Default handler for a Watchdog exception,
1293 * spins until a reboot occurs
1294 */
1295void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
1296{
1297 /* Generic WatchdogHandler, implement your own */
1298 mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
1299 return;
1300}
1301
1302void WatchdogException(struct pt_regs *regs)
1303{
1304 printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
1305 WatchdogHandler(regs);
1306}
1307#endif
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001308
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001309/*
1310 * We enter here if we discover during exception entry that we are
1311 * running in supervisor mode with a userspace value in the stack pointer.
1312 */
1313void kernel_bad_stack(struct pt_regs *regs)
1314{
1315 printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
1316 regs->gpr[1], regs->nip);
1317 die("Bad kernel stack pointer", regs, SIGABRT);
1318}
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001319
1320void __init trap_init(void)
1321{
1322}
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001323
1324
1325#ifdef CONFIG_PPC_EMULATED_STATS
1326
1327#define WARN_EMULATED_SETUP(type) .type = { .name = #type }
1328
1329struct ppc_emulated ppc_emulated = {
1330#ifdef CONFIG_ALTIVEC
1331 WARN_EMULATED_SETUP(altivec),
1332#endif
1333 WARN_EMULATED_SETUP(dcba),
1334 WARN_EMULATED_SETUP(dcbz),
1335 WARN_EMULATED_SETUP(fp_pair),
1336 WARN_EMULATED_SETUP(isel),
1337 WARN_EMULATED_SETUP(mcrxr),
1338 WARN_EMULATED_SETUP(mfpvr),
1339 WARN_EMULATED_SETUP(multiple),
1340 WARN_EMULATED_SETUP(popcntb),
1341 WARN_EMULATED_SETUP(spe),
1342 WARN_EMULATED_SETUP(string),
1343 WARN_EMULATED_SETUP(unaligned),
1344#ifdef CONFIG_MATH_EMULATION
1345 WARN_EMULATED_SETUP(math),
1346#elif defined(CONFIG_8XX_MINIMAL_FPEMU)
1347 WARN_EMULATED_SETUP(8xx),
1348#endif
1349#ifdef CONFIG_VSX
1350 WARN_EMULATED_SETUP(vsx),
1351#endif
1352};
1353
1354u32 ppc_warn_emulated;
1355
1356void ppc_warn_emulated_print(const char *type)
1357{
1358 if (printk_ratelimit())
1359 pr_warning("%s used emulated %s instruction\n", current->comm,
1360 type);
1361}
1362
1363static int __init ppc_warn_emulated_init(void)
1364{
1365 struct dentry *dir, *d;
1366 unsigned int i;
1367 struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
1368
1369 if (!powerpc_debugfs_root)
1370 return -ENODEV;
1371
1372 dir = debugfs_create_dir("emulated_instructions",
1373 powerpc_debugfs_root);
1374 if (!dir)
1375 return -ENOMEM;
1376
1377 d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
1378 &ppc_warn_emulated);
1379 if (!d)
1380 goto fail;
1381
1382 for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
1383 d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
1384 (u32 *)&entries[i].val.counter);
1385 if (!d)
1386 goto fail;
1387 }
1388
1389 return 0;
1390
1391fail:
1392 debugfs_remove_recursive(dir);
1393 return -ENOMEM;
1394}
1395
1396device_initcall(ppc_warn_emulated_init);
1397
1398#endif /* CONFIG_PPC_EMULATED_STATS */