blob: 4588d1eb18d26a09ec66611a8a02f881254c6620 [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@samba.org34c2a142007-03-20 20:38:13 -0500149 print_modules();
150 show_regs(regs);
151 } else {
152 printk("Recursive die() failure, output suppressed\n");
153 }
154
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000155 bust_spinlocks(0);
anton@samba.org34c2a142007-03-20 20:38:13 -0500156 die.lock_owner = -1;
Pavel Emelianovbcdcd8e2007-07-17 04:03:42 -0700157 add_taint(TAINT_DIE);
anton@samba.org34c2a142007-03-20 20:38:13 -0500158 spin_unlock_irqrestore(&die.lock, flags);
David Wilderc0ce7d02006-06-23 15:29:34 -0700159
160 if (kexec_should_crash(current) ||
161 kexec_sr_activated(smp_processor_id()))
162 crash_kexec(regs);
163 crash_kexec_secondary(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000164
165 if (in_interrupt())
166 panic("Fatal exception in interrupt");
167
Hormscea6a4b2006-07-30 03:03:34 -0700168 if (panic_on_oops)
Horms012c4372006-08-13 23:24:22 -0700169 panic("Fatal exception");
Hormscea6a4b2006-07-30 03:03:34 -0700170
anton@samba.org293e4682007-03-20 20:38:11 -0500171 oops_exit();
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000172 do_exit(err);
173
174 return 0;
175}
176
Oleg Nesterov25baa352009-12-15 16:47:18 -0800177void user_single_step_siginfo(struct task_struct *tsk,
178 struct pt_regs *regs, siginfo_t *info)
179{
180 memset(info, 0, sizeof(*info));
181 info->si_signo = SIGTRAP;
182 info->si_code = TRAP_TRACE;
183 info->si_addr = (void __user *)regs->nip;
184}
185
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000186void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
187{
188 siginfo_t info;
Olof Johanssond0c3d532007-10-12 10:20:07 +1000189 const char fmt32[] = KERN_INFO "%s[%d]: unhandled signal %d " \
190 "at %08lx nip %08lx lr %08lx code %x\n";
191 const char fmt64[] = KERN_INFO "%s[%d]: unhandled signal %d " \
192 "at %016lx nip %016lx lr %016lx code %x\n";
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000193
194 if (!user_mode(regs)) {
195 if (die("Exception in kernel mode", regs, signr))
196 return;
Olof Johanssond0c3d532007-10-12 10:20:07 +1000197 } else if (show_unhandled_signals &&
198 unhandled_signal(current, signr) &&
199 printk_ratelimit()) {
200 printk(regs->msr & MSR_SF ? fmt64 : fmt32,
201 current->comm, current->pid, signr,
202 addr, regs->nip, regs->link, code);
203 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000204
205 memset(&info, 0, sizeof(info));
206 info.si_signo = signr;
207 info.si_code = code;
208 info.si_addr = (void __user *) addr;
209 force_sig_info(signr, &info, current);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000210}
211
212#ifdef CONFIG_PPC64
213void system_reset_exception(struct pt_regs *regs)
214{
215 /* See if any machine dependent calls */
Arnd Bergmannc902be72006-01-04 19:55:53 +0000216 if (ppc_md.system_reset_exception) {
217 if (ppc_md.system_reset_exception(regs))
218 return;
219 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000220
David Wilderc0ce7d02006-06-23 15:29:34 -0700221#ifdef CONFIG_KEXEC
222 cpu_set(smp_processor_id(), cpus_in_sr);
223#endif
224
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000225 die("System Reset", regs, SIGABRT);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000226
David Wildereac83922006-06-29 15:17:30 -0700227 /*
228 * Some CPUs when released from the debugger will execute this path.
229 * These CPUs entered the debugger via a soft-reset. If the CPU was
230 * hung before entering the debugger it will return to the hung
231 * state when exiting this function. This causes a problem in
232 * kdump since the hung CPU(s) will not respond to the IPI sent
233 * from kdump. To prevent the problem we call crash_kexec_secondary()
234 * here. If a kdump had not been initiated or we exit the debugger
235 * with the "exit and recover" command (x) crash_kexec_secondary()
236 * will return after 5ms and the CPU returns to its previous state.
237 */
238 crash_kexec_secondary(regs);
239
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000240 /* Must die if the interrupt is not recoverable */
241 if (!(regs->msr & MSR_RI))
242 panic("Unrecoverable System Reset");
243
244 /* What should we do here? We could issue a shutdown or hard reset. */
245}
246#endif
247
248/*
249 * I/O accesses can cause machine checks on powermacs.
250 * Check if the NIP corresponds to the address of a sync
251 * instruction for which there is an entry in the exception
252 * table.
253 * Note that the 601 only takes a machine check on TEA
254 * (transfer error ack) signal assertion, and does not
255 * set any of the top 16 bits of SRR1.
256 * -- paulus.
257 */
258static inline int check_io_access(struct pt_regs *regs)
259{
Benjamin Herrenschmidt68a64352006-11-13 09:27:39 +1100260#ifdef CONFIG_PPC32
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000261 unsigned long msr = regs->msr;
262 const struct exception_table_entry *entry;
263 unsigned int *nip = (unsigned int *)regs->nip;
264
265 if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
266 && (entry = search_exception_tables(regs->nip)) != NULL) {
267 /*
268 * Check that it's a sync instruction, or somewhere
269 * in the twi; isync; nop sequence that inb/inw/inl uses.
270 * As the address is in the exception table
271 * we should be able to read the instr there.
272 * For the debug message, we look at the preceding
273 * load or store.
274 */
275 if (*nip == 0x60000000) /* nop */
276 nip -= 2;
277 else if (*nip == 0x4c00012c) /* isync */
278 --nip;
279 if (*nip == 0x7c0004ac || (*nip >> 26) == 3) {
280 /* sync or twi */
281 unsigned int rb;
282
283 --nip;
284 rb = (*nip >> 11) & 0x1f;
285 printk(KERN_DEBUG "%s bad port %lx at %p\n",
286 (*nip & 0x100)? "OUT to": "IN from",
287 regs->gpr[rb] - _IO_BASE, nip);
288 regs->msr |= MSR_RI;
289 regs->nip = entry->fixup;
290 return 1;
291 }
292 }
Benjamin Herrenschmidt68a64352006-11-13 09:27:39 +1100293#endif /* CONFIG_PPC32 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000294 return 0;
295}
296
297#if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
298/* On 4xx, the reason for the machine check or program exception
299 is in the ESR. */
300#define get_reason(regs) ((regs)->dsisr)
301#ifndef CONFIG_FSL_BOOKE
302#define get_mc_reason(regs) ((regs)->dsisr)
303#else
Becky Bruce86d7a9a2007-08-02 15:37:15 -0500304#define get_mc_reason(regs) (mfspr(SPRN_MCSR) & MCSR_MASK)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000305#endif
306#define REASON_FP ESR_FP
307#define REASON_ILLEGAL (ESR_PIL | ESR_PUO)
308#define REASON_PRIVILEGED ESR_PPR
309#define REASON_TRAP ESR_PTR
310
311/* single-step stuff */
312#define single_stepping(regs) (current->thread.dbcr0 & DBCR0_IC)
313#define clear_single_step(regs) (current->thread.dbcr0 &= ~DBCR0_IC)
314
315#else
316/* On non-4xx, the reason for the machine check or program
317 exception is in the MSR. */
318#define get_reason(regs) ((regs)->msr)
319#define get_mc_reason(regs) ((regs)->msr)
320#define REASON_FP 0x100000
321#define REASON_ILLEGAL 0x80000
322#define REASON_PRIVILEGED 0x40000
323#define REASON_TRAP 0x20000
324
325#define single_stepping(regs) ((regs)->msr & MSR_SE)
326#define clear_single_step(regs) ((regs)->msr &= ~MSR_SE)
327#endif
328
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100329#if defined(CONFIG_4xx)
330int machine_check_4xx(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000331{
Kumar Gala1a6a4ff2006-03-30 21:11:15 -0600332 unsigned long reason = get_mc_reason(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000333
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000334 if (reason & ESR_IMCP) {
335 printk("Instruction");
336 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
337 } else
338 printk("Data");
339 printk(" machine check in kernel mode.\n");
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100340
341 return 0;
342}
343
344int machine_check_440A(struct pt_regs *regs)
345{
346 unsigned long reason = get_mc_reason(regs);
347
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000348 printk("Machine check in kernel mode.\n");
349 if (reason & ESR_IMCP){
350 printk("Instruction Synchronous Machine Check exception\n");
351 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
352 }
353 else {
354 u32 mcsr = mfspr(SPRN_MCSR);
355 if (mcsr & MCSR_IB)
356 printk("Instruction Read PLB Error\n");
357 if (mcsr & MCSR_DRB)
358 printk("Data Read PLB Error\n");
359 if (mcsr & MCSR_DWB)
360 printk("Data Write PLB Error\n");
361 if (mcsr & MCSR_TLBP)
362 printk("TLB Parity Error\n");
363 if (mcsr & MCSR_ICP){
364 flush_instruction_cache();
365 printk("I-Cache Parity Error\n");
366 }
367 if (mcsr & MCSR_DCSP)
368 printk("D-Cache Search Parity Error\n");
369 if (mcsr & MCSR_DCFP)
370 printk("D-Cache Flush Parity Error\n");
371 if (mcsr & MCSR_IMPE)
372 printk("Machine Check exception is imprecise\n");
373
374 /* Clear MCSR */
375 mtspr(SPRN_MCSR, mcsr);
376 }
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100377 return 0;
378}
379#elif defined(CONFIG_E500)
380int machine_check_e500(struct pt_regs *regs)
381{
382 unsigned long reason = get_mc_reason(regs);
383
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000384 printk("Machine check in kernel mode.\n");
385 printk("Caused by (from MCSR=%lx): ", reason);
386
387 if (reason & MCSR_MCP)
388 printk("Machine Check Signal\n");
389 if (reason & MCSR_ICPERR)
390 printk("Instruction Cache Parity Error\n");
391 if (reason & MCSR_DCP_PERR)
392 printk("Data Cache Push Parity Error\n");
393 if (reason & MCSR_DCPERR)
394 printk("Data Cache Parity Error\n");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000395 if (reason & MCSR_BUS_IAERR)
396 printk("Bus - Instruction Address Error\n");
397 if (reason & MCSR_BUS_RAERR)
398 printk("Bus - Read Address Error\n");
399 if (reason & MCSR_BUS_WAERR)
400 printk("Bus - Write Address Error\n");
401 if (reason & MCSR_BUS_IBERR)
402 printk("Bus - Instruction Data Error\n");
403 if (reason & MCSR_BUS_RBERR)
404 printk("Bus - Read Data Bus Error\n");
405 if (reason & MCSR_BUS_WBERR)
406 printk("Bus - Read Data Bus Error\n");
407 if (reason & MCSR_BUS_IPERR)
408 printk("Bus - Instruction Parity Error\n");
409 if (reason & MCSR_BUS_RPERR)
410 printk("Bus - Read Parity Error\n");
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100411
412 return 0;
413}
414#elif defined(CONFIG_E200)
415int machine_check_e200(struct pt_regs *regs)
416{
417 unsigned long reason = get_mc_reason(regs);
418
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000419 printk("Machine check in kernel mode.\n");
420 printk("Caused by (from MCSR=%lx): ", reason);
421
422 if (reason & MCSR_MCP)
423 printk("Machine Check Signal\n");
424 if (reason & MCSR_CP_PERR)
425 printk("Cache Push Parity Error\n");
426 if (reason & MCSR_CPERR)
427 printk("Cache Parity Error\n");
428 if (reason & MCSR_EXCP_ERR)
429 printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
430 if (reason & MCSR_BUS_IRERR)
431 printk("Bus - Read Bus Error on instruction fetch\n");
432 if (reason & MCSR_BUS_DRERR)
433 printk("Bus - Read Bus Error on data load\n");
434 if (reason & MCSR_BUS_WRERR)
435 printk("Bus - Write Bus Error on buffered store or cache line push\n");
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100436
437 return 0;
438}
439#else
440int machine_check_generic(struct pt_regs *regs)
441{
442 unsigned long reason = get_mc_reason(regs);
443
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000444 printk("Machine check in kernel mode.\n");
445 printk("Caused by (from SRR1=%lx): ", reason);
446 switch (reason & 0x601F0000) {
447 case 0x80000:
448 printk("Machine check signal\n");
449 break;
450 case 0: /* for 601 */
451 case 0x40000:
452 case 0x140000: /* 7450 MSS error and TEA */
453 printk("Transfer error ack signal\n");
454 break;
455 case 0x20000:
456 printk("Data parity error signal\n");
457 break;
458 case 0x10000:
459 printk("Address parity error signal\n");
460 break;
461 case 0x20000000:
462 printk("L1 Data Cache error\n");
463 break;
464 case 0x40000000:
465 printk("L1 Instruction Cache error\n");
466 break;
467 case 0x00100000:
468 printk("L2 data cache parity error\n");
469 break;
470 default:
471 printk("Unknown values in msr\n");
472 }
Olof Johansson75918a42007-09-21 05:11:20 +1000473 return 0;
474}
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100475#endif /* everything else */
Olof Johansson75918a42007-09-21 05:11:20 +1000476
477void machine_check_exception(struct pt_regs *regs)
478{
479 int recover = 0;
480
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100481 /* See if any machine dependent calls. In theory, we would want
482 * to call the CPU first, and call the ppc_md. one if the CPU
483 * one returns a positive number. However there is existing code
484 * that assumes the board gets a first chance, so let's keep it
485 * that way for now and fix things later. --BenH.
486 */
Olof Johansson75918a42007-09-21 05:11:20 +1000487 if (ppc_md.machine_check_exception)
488 recover = ppc_md.machine_check_exception(regs);
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100489 else if (cur_cpu_spec->machine_check)
490 recover = cur_cpu_spec->machine_check(regs);
Olof Johansson75918a42007-09-21 05:11:20 +1000491
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100492 if (recover > 0)
Olof Johansson75918a42007-09-21 05:11:20 +1000493 return;
494
495 if (user_mode(regs)) {
496 regs->msr |= MSR_RI;
497 _exception(SIGBUS, regs, BUS_ADRERR, regs->nip);
498 return;
499 }
500
501#if defined(CONFIG_8xx) && defined(CONFIG_PCI)
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100502 /* the qspan pci read routines can cause machine checks -- Cort
503 *
504 * yuck !!! that totally needs to go away ! There are better ways
505 * to deal with that than having a wart in the mcheck handler.
506 * -- BenH
507 */
Olof Johansson75918a42007-09-21 05:11:20 +1000508 bad_page_fault(regs, regs->dar, SIGBUS);
509 return;
510#endif
511
512 if (debugger_fault_handler(regs)) {
513 regs->msr |= MSR_RI;
514 return;
515 }
516
517 if (check_io_access(regs))
518 return;
519
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000520 if (debugger_fault_handler(regs))
521 return;
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000522 die("Machine check", regs, SIGBUS);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000523
524 /* Must die if the interrupt is not recoverable */
525 if (!(regs->msr & MSR_RI))
526 panic("Unrecoverable Machine check");
527}
528
529void SMIException(struct pt_regs *regs)
530{
531 die("System Management Interrupt", regs, SIGABRT);
532}
533
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000534void unknown_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000535{
536 printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
537 regs->nip, regs->msr, regs->trap);
538
539 _exception(SIGTRAP, regs, 0, 0);
540}
541
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000542void instruction_breakpoint_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000543{
544 if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
545 5, SIGTRAP) == NOTIFY_STOP)
546 return;
547 if (debugger_iabr_match(regs))
548 return;
549 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
550}
551
552void RunModeException(struct pt_regs *regs)
553{
554 _exception(SIGTRAP, regs, 0, 0);
555}
556
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000557void __kprobes single_step_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000558{
559 regs->msr &= ~(MSR_SE | MSR_BE); /* Turn off 'trace' bits */
560
561 if (notify_die(DIE_SSTEP, "single_step", regs, 5,
562 5, SIGTRAP) == NOTIFY_STOP)
563 return;
564 if (debugger_sstep(regs))
565 return;
566
567 _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
568}
569
570/*
571 * After we have successfully emulated an instruction, we have to
572 * check if the instruction was being single-stepped, and if so,
573 * pretend we got a single-step exception. This was pointed out
574 * by Kumar Gala. -- paulus
575 */
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000576static void emulate_single_step(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000577{
578 if (single_stepping(regs)) {
579 clear_single_step(regs);
580 _exception(SIGTRAP, regs, TRAP_TRACE, 0);
581 }
582}
583
Kumar Gala5fad2932007-02-07 01:47:59 -0600584static inline int __parse_fpscr(unsigned long fpscr)
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000585{
Kumar Gala5fad2932007-02-07 01:47:59 -0600586 int ret = 0;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000587
588 /* Invalid operation */
589 if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600590 ret = FPE_FLTINV;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000591
592 /* Overflow */
593 else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600594 ret = FPE_FLTOVF;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000595
596 /* Underflow */
597 else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600598 ret = FPE_FLTUND;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000599
600 /* Divide by zero */
601 else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600602 ret = FPE_FLTDIV;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000603
604 /* Inexact result */
605 else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600606 ret = FPE_FLTRES;
607
608 return ret;
609}
610
611static void parse_fpe(struct pt_regs *regs)
612{
613 int code = 0;
614
615 flush_fp_to_thread(current);
616
617 code = __parse_fpscr(current->thread.fpscr.val);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000618
619 _exception(SIGFPE, regs, code, regs->nip);
620}
621
622/*
623 * Illegal instruction emulation support. Originally written to
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000624 * provide the PVR to user applications using the mfspr rd, PVR.
625 * Return non-zero if we can't emulate, or -EFAULT if the associated
626 * memory access caused an access fault. Return zero on success.
627 *
628 * There are a couple of ways to do this, either "decode" the instruction
629 * or directly match lots of bits. In this case, matching lots of
630 * bits is faster and easier.
Paul Mackerras86417782005-10-10 22:37:57 +1000631 *
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000632 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000633static int emulate_string_inst(struct pt_regs *regs, u32 instword)
634{
635 u8 rT = (instword >> 21) & 0x1f;
636 u8 rA = (instword >> 16) & 0x1f;
637 u8 NB_RB = (instword >> 11) & 0x1f;
638 u32 num_bytes;
639 unsigned long EA;
640 int pos = 0;
641
642 /* Early out if we are an invalid form of lswx */
Kumar Gala16c57b32009-02-10 20:10:44 +0000643 if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000644 if ((rT == rA) || (rT == NB_RB))
645 return -EINVAL;
646
647 EA = (rA == 0) ? 0 : regs->gpr[rA];
648
Kumar Gala16c57b32009-02-10 20:10:44 +0000649 switch (instword & PPC_INST_STRING_MASK) {
650 case PPC_INST_LSWX:
651 case PPC_INST_STSWX:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000652 EA += NB_RB;
653 num_bytes = regs->xer & 0x7f;
654 break;
Kumar Gala16c57b32009-02-10 20:10:44 +0000655 case PPC_INST_LSWI:
656 case PPC_INST_STSWI:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000657 num_bytes = (NB_RB == 0) ? 32 : NB_RB;
658 break;
659 default:
660 return -EINVAL;
661 }
662
663 while (num_bytes != 0)
664 {
665 u8 val;
666 u32 shift = 8 * (3 - (pos & 0x3));
667
Kumar Gala16c57b32009-02-10 20:10:44 +0000668 switch ((instword & PPC_INST_STRING_MASK)) {
669 case PPC_INST_LSWX:
670 case PPC_INST_LSWI:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000671 if (get_user(val, (u8 __user *)EA))
672 return -EFAULT;
673 /* first time updating this reg,
674 * zero it out */
675 if (pos == 0)
676 regs->gpr[rT] = 0;
677 regs->gpr[rT] |= val << shift;
678 break;
Kumar Gala16c57b32009-02-10 20:10:44 +0000679 case PPC_INST_STSWI:
680 case PPC_INST_STSWX:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000681 val = regs->gpr[rT] >> shift;
682 if (put_user(val, (u8 __user *)EA))
683 return -EFAULT;
684 break;
685 }
686 /* move EA to next address */
687 EA += 1;
688 num_bytes--;
689
690 /* manage our position within the register */
691 if (++pos == 4) {
692 pos = 0;
693 if (++rT == 32)
694 rT = 0;
695 }
696 }
697
698 return 0;
699}
700
Will Schmidtc3412dc2006-08-30 13:11:38 -0500701static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
702{
703 u32 ra,rs;
704 unsigned long tmp;
705
706 ra = (instword >> 16) & 0x1f;
707 rs = (instword >> 21) & 0x1f;
708
709 tmp = regs->gpr[rs];
710 tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
711 tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
712 tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
713 regs->gpr[ra] = tmp;
714
715 return 0;
716}
717
Kumar Galac1469f12007-11-19 21:35:29 -0600718static int emulate_isel(struct pt_regs *regs, u32 instword)
719{
720 u8 rT = (instword >> 21) & 0x1f;
721 u8 rA = (instword >> 16) & 0x1f;
722 u8 rB = (instword >> 11) & 0x1f;
723 u8 BC = (instword >> 6) & 0x1f;
724 u8 bit;
725 unsigned long tmp;
726
727 tmp = (rA == 0) ? 0 : regs->gpr[rA];
728 bit = (regs->ccr >> (31 - BC)) & 0x1;
729
730 regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
731
732 return 0;
733}
734
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000735static int emulate_instruction(struct pt_regs *regs)
736{
737 u32 instword;
738 u32 rd;
739
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000740 if (!user_mode(regs) || (regs->msr & MSR_LE))
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000741 return -EINVAL;
742 CHECK_FULL_REGS(regs);
743
744 if (get_user(instword, (u32 __user *)(regs->nip)))
745 return -EFAULT;
746
747 /* Emulate the mfspr rD, PVR. */
Kumar Gala16c57b32009-02-10 20:10:44 +0000748 if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000749 PPC_WARN_EMULATED(mfpvr, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000750 rd = (instword >> 21) & 0x1f;
751 regs->gpr[rd] = mfspr(SPRN_PVR);
752 return 0;
753 }
754
755 /* Emulating the dcba insn is just a no-op. */
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000756 if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000757 PPC_WARN_EMULATED(dcba, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000758 return 0;
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000759 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000760
761 /* Emulate the mcrxr insn. */
Kumar Gala16c57b32009-02-10 20:10:44 +0000762 if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
Paul Mackerras86417782005-10-10 22:37:57 +1000763 int shift = (instword >> 21) & 0x1c;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000764 unsigned long msk = 0xf0000000UL >> shift;
765
Anton Blanchardeecff812009-10-27 18:46:55 +0000766 PPC_WARN_EMULATED(mcrxr, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000767 regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
768 regs->xer &= ~0xf0000000UL;
769 return 0;
770 }
771
772 /* Emulate load/store string insn. */
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000773 if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000774 PPC_WARN_EMULATED(string, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000775 return emulate_string_inst(regs, instword);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000776 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000777
Will Schmidtc3412dc2006-08-30 13:11:38 -0500778 /* Emulate the popcntb (Population Count Bytes) instruction. */
Kumar Gala16c57b32009-02-10 20:10:44 +0000779 if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000780 PPC_WARN_EMULATED(popcntb, regs);
Will Schmidtc3412dc2006-08-30 13:11:38 -0500781 return emulate_popcntb_inst(regs, instword);
782 }
783
Kumar Galac1469f12007-11-19 21:35:29 -0600784 /* Emulate isel (Integer Select) instruction */
Kumar Gala16c57b32009-02-10 20:10:44 +0000785 if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000786 PPC_WARN_EMULATED(isel, regs);
Kumar Galac1469f12007-11-19 21:35:29 -0600787 return emulate_isel(regs, instword);
788 }
789
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000790 return -EINVAL;
791}
792
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -0800793int is_valid_bugaddr(unsigned long addr)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000794{
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -0800795 return is_kernel_addr(addr);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000796}
797
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000798void __kprobes program_check_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000799{
800 unsigned int reason = get_reason(regs);
801 extern int do_mathemu(struct pt_regs *regs);
802
Kim Phillipsaa42c692006-12-08 02:43:30 -0600803 /* We can now get here via a FP Unavailable exception if the core
Kumar Gala04903a32007-02-07 01:13:32 -0600804 * has no FPU, in that case the reason flags will be 0 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000805
806 if (reason & REASON_FP) {
807 /* IEEE FP exception */
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000808 parse_fpe(regs);
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000809 return;
810 }
811 if (reason & REASON_TRAP) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000812 /* trap exception */
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000813 if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
814 == NOTIFY_STOP)
815 return;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000816 if (debugger_bpt(regs))
817 return;
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -0800818
819 if (!(regs->msr & MSR_PR) && /* not user-mode */
Heiko Carstens608e2612007-07-15 23:41:39 -0700820 report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000821 regs->nip += 4;
822 return;
823 }
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000824 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
825 return;
826 }
827
Paul Mackerrascd8a5672006-03-03 17:11:40 +1100828 local_irq_enable();
829
Kumar Gala04903a32007-02-07 01:13:32 -0600830#ifdef CONFIG_MATH_EMULATION
831 /* (reason & REASON_ILLEGAL) would be the obvious thing here,
832 * but there seems to be a hardware bug on the 405GP (RevD)
833 * that means ESR is sometimes set incorrectly - either to
834 * ESR_DST (!?) or 0. In the process of chasing this with the
835 * hardware people - not sure if it can happen on any illegal
836 * instruction or only on FP instructions, whether there is a
837 * pattern to occurences etc. -dgibson 31/Mar/2003 */
Kumar Gala5fad2932007-02-07 01:47:59 -0600838 switch (do_mathemu(regs)) {
839 case 0:
Kumar Gala04903a32007-02-07 01:13:32 -0600840 emulate_single_step(regs);
841 return;
Kumar Gala5fad2932007-02-07 01:47:59 -0600842 case 1: {
843 int code = 0;
844 code = __parse_fpscr(current->thread.fpscr.val);
845 _exception(SIGFPE, regs, code, regs->nip);
846 return;
847 }
848 case -EFAULT:
849 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
850 return;
Kumar Gala04903a32007-02-07 01:13:32 -0600851 }
Kumar Gala5fad2932007-02-07 01:47:59 -0600852 /* fall through on any other errors */
Kumar Gala04903a32007-02-07 01:13:32 -0600853#endif /* CONFIG_MATH_EMULATION */
854
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000855 /* Try to emulate it if we should. */
856 if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000857 switch (emulate_instruction(regs)) {
858 case 0:
859 regs->nip += 4;
860 emulate_single_step(regs);
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000861 return;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000862 case -EFAULT:
863 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000864 return;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000865 }
866 }
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000867
868 if (reason & REASON_PRIVILEGED)
869 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
870 else
871 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000872}
873
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000874void alignment_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000875{
Benjamin Herrenschmidt4393c4f2006-11-01 15:11:39 +1100876 int sig, code, fixed = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000877
Paul Mackerrase9370ae2006-06-07 16:15:39 +1000878 /* we don't implement logging of alignment exceptions */
879 if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
880 fixed = fix_alignment(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000881
882 if (fixed == 1) {
883 regs->nip += 4; /* skip over emulated instruction */
884 emulate_single_step(regs);
885 return;
886 }
887
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000888 /* Operand address was bad */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000889 if (fixed == -EFAULT) {
Benjamin Herrenschmidt4393c4f2006-11-01 15:11:39 +1100890 sig = SIGSEGV;
891 code = SEGV_ACCERR;
892 } else {
893 sig = SIGBUS;
894 code = BUS_ADRALN;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000895 }
Benjamin Herrenschmidt4393c4f2006-11-01 15:11:39 +1100896 if (user_mode(regs))
897 _exception(sig, regs, code, regs->dar);
898 else
899 bad_page_fault(regs, regs->dar, sig);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000900}
901
902void StackOverflow(struct pt_regs *regs)
903{
904 printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
905 current, regs->gpr[1]);
906 debugger(regs);
907 show_regs(regs);
908 panic("kernel stack overflow");
909}
910
911void nonrecoverable_exception(struct pt_regs *regs)
912{
913 printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
914 regs->nip, regs->msr);
915 debugger(regs);
916 die("nonrecoverable exception", regs, SIGKILL);
917}
918
919void trace_syscall(struct pt_regs *regs)
920{
921 printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld %s\n",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700922 current, task_pid_nr(current), regs->nip, regs->link, regs->gpr[0],
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000923 regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
924}
925
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000926void kernel_fp_unavailable_exception(struct pt_regs *regs)
927{
928 printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
929 "%lx at %lx\n", regs->trap, regs->nip);
930 die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
931}
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000932
933void altivec_unavailable_exception(struct pt_regs *regs)
934{
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000935 if (user_mode(regs)) {
936 /* A user program has executed an altivec instruction,
937 but this kernel doesn't support altivec. */
938 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
939 return;
940 }
Anton Blanchard6c4841c2006-10-13 11:41:00 +1000941
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000942 printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
943 "%lx at %lx\n", regs->trap, regs->nip);
944 die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000945}
946
Michael Neulingce48b212008-06-25 14:07:18 +1000947void vsx_unavailable_exception(struct pt_regs *regs)
948{
949 if (user_mode(regs)) {
950 /* A user program has executed an vsx instruction,
951 but this kernel doesn't support vsx. */
952 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
953 return;
954 }
955
956 printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
957 "%lx at %lx\n", regs->trap, regs->nip);
958 die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
959}
960
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000961void performance_monitor_exception(struct pt_regs *regs)
962{
963 perf_irq(regs);
964}
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000965
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000966#ifdef CONFIG_8xx
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000967void SoftwareEmulation(struct pt_regs *regs)
968{
969 extern int do_mathemu(struct pt_regs *);
970 extern int Soft_emulate_8xx(struct pt_regs *);
Scott Wood5dd57a12007-09-18 15:29:35 -0500971#if defined(CONFIG_MATH_EMULATION) || defined(CONFIG_8XX_MINIMAL_FPEMU)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000972 int errcode;
Scott Wood5dd57a12007-09-18 15:29:35 -0500973#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000974
975 CHECK_FULL_REGS(regs);
976
977 if (!user_mode(regs)) {
978 debugger(regs);
979 die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
980 }
981
982#ifdef CONFIG_MATH_EMULATION
983 errcode = do_mathemu(regs);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000984 if (errcode >= 0)
Anton Blanchardeecff812009-10-27 18:46:55 +0000985 PPC_WARN_EMULATED(math, regs);
Kumar Gala5fad2932007-02-07 01:47:59 -0600986
987 switch (errcode) {
988 case 0:
989 emulate_single_step(regs);
990 return;
991 case 1: {
992 int code = 0;
993 code = __parse_fpscr(current->thread.fpscr.val);
994 _exception(SIGFPE, regs, code, regs->nip);
995 return;
996 }
997 case -EFAULT:
998 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
999 return;
1000 default:
1001 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1002 return;
1003 }
1004
Scott Wood5dd57a12007-09-18 15:29:35 -05001005#elif defined(CONFIG_8XX_MINIMAL_FPEMU)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001006 errcode = Soft_emulate_8xx(regs);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001007 if (errcode >= 0)
Anton Blanchardeecff812009-10-27 18:46:55 +00001008 PPC_WARN_EMULATED(8xx, regs);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001009
Kumar Gala5fad2932007-02-07 01:47:59 -06001010 switch (errcode) {
1011 case 0:
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001012 emulate_single_step(regs);
Kumar Gala5fad2932007-02-07 01:47:59 -06001013 return;
1014 case 1:
1015 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1016 return;
1017 case -EFAULT:
1018 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1019 return;
1020 }
Scott Wood5dd57a12007-09-18 15:29:35 -05001021#else
1022 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
Kumar Gala5fad2932007-02-07 01:47:59 -06001023#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001024}
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001025#endif /* CONFIG_8xx */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001026
1027#if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
1028
Kumar Galaf8279622008-06-26 02:01:37 -05001029void __kprobes DebugException(struct pt_regs *regs, unsigned long debug_status)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001030{
Roland McGrathec097c82009-05-28 21:26:38 +00001031 /* Hack alert: On BookE, Branch Taken stops on the branch itself, while
1032 * on server, it stops on the target of the branch. In order to simulate
1033 * the server behaviour, we thus restart right away with a single step
1034 * instead of stopping here when hitting a BT
1035 */
1036 if (debug_status & DBSR_BT) {
1037 regs->msr &= ~MSR_DE;
1038
1039 /* Disable BT */
1040 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
1041 /* Clear the BT event */
1042 mtspr(SPRN_DBSR, DBSR_BT);
1043
1044 /* Do the single step trick only when coming from userspace */
1045 if (user_mode(regs)) {
1046 current->thread.dbcr0 &= ~DBCR0_BT;
1047 current->thread.dbcr0 |= DBCR0_IDM | DBCR0_IC;
1048 regs->msr |= MSR_DE;
1049 return;
1050 }
1051
1052 if (notify_die(DIE_SSTEP, "block_step", regs, 5,
1053 5, SIGTRAP) == NOTIFY_STOP) {
1054 return;
1055 }
1056 if (debugger_sstep(regs))
1057 return;
1058 } else if (debug_status & DBSR_IC) { /* Instruction complete */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001059 regs->msr &= ~MSR_DE;
Kumar Galaf8279622008-06-26 02:01:37 -05001060
1061 /* Disable instruction completion */
1062 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
1063 /* Clear the instruction completion event */
1064 mtspr(SPRN_DBSR, DBSR_IC);
1065
1066 if (notify_die(DIE_SSTEP, "single_step", regs, 5,
1067 5, SIGTRAP) == NOTIFY_STOP) {
1068 return;
1069 }
1070
1071 if (debugger_sstep(regs))
1072 return;
1073
Roland McGrathec097c82009-05-28 21:26:38 +00001074 if (user_mode(regs))
1075 current->thread.dbcr0 &= ~(DBCR0_IC);
Kumar Galaf8279622008-06-26 02:01:37 -05001076
1077 _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
Luis Machadod6a61bf2008-07-24 02:10:41 +10001078 } else if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
1079 regs->msr &= ~MSR_DE;
1080
1081 if (user_mode(regs)) {
1082 current->thread.dbcr0 &= ~(DBSR_DAC1R | DBSR_DAC1W |
1083 DBCR0_IDM);
1084 } else {
1085 /* Disable DAC interupts */
1086 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~(DBSR_DAC1R |
1087 DBSR_DAC1W | DBCR0_IDM));
1088
1089 /* Clear the DAC event */
1090 mtspr(SPRN_DBSR, (DBSR_DAC1R | DBSR_DAC1W));
1091 }
1092 /* Setup and send the trap to the handler */
1093 do_dabr(regs, mfspr(SPRN_DAC1), debug_status);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001094 }
1095}
1096#endif /* CONFIG_4xx || CONFIG_BOOKE */
1097
1098#if !defined(CONFIG_TAU_INT)
1099void TAUException(struct pt_regs *regs)
1100{
1101 printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx %s\n",
1102 regs->nip, regs->msr, regs->trap, print_tainted());
1103}
1104#endif /* CONFIG_INT_TAU */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001105
1106#ifdef CONFIG_ALTIVEC
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001107void altivec_assist_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001108{
1109 int err;
1110
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001111 if (!user_mode(regs)) {
1112 printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
1113 " at %lx\n", regs->nip);
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001114 die("Kernel VMX/Altivec assist exception", regs, SIGILL);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001115 }
1116
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001117 flush_altivec_to_thread(current);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001118
Anton Blanchardeecff812009-10-27 18:46:55 +00001119 PPC_WARN_EMULATED(altivec, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001120 err = emulate_altivec(regs);
1121 if (err == 0) {
1122 regs->nip += 4; /* skip emulated instruction */
1123 emulate_single_step(regs);
1124 return;
1125 }
1126
1127 if (err == -EFAULT) {
1128 /* got an error reading the instruction */
1129 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1130 } else {
1131 /* didn't recognize the instruction */
1132 /* XXX quick hack for now: set the non-Java bit in the VSCR */
1133 if (printk_ratelimit())
1134 printk(KERN_ERR "Unrecognized altivec instruction "
1135 "in %s at %lx\n", current->comm, regs->nip);
1136 current->thread.vscr.u[3] |= 0x10000;
1137 }
1138}
1139#endif /* CONFIG_ALTIVEC */
1140
Michael Neulingce48b212008-06-25 14:07:18 +10001141#ifdef CONFIG_VSX
1142void vsx_assist_exception(struct pt_regs *regs)
1143{
1144 if (!user_mode(regs)) {
1145 printk(KERN_EMERG "VSX assist exception in kernel mode"
1146 " at %lx\n", regs->nip);
1147 die("Kernel VSX assist exception", regs, SIGILL);
1148 }
1149
1150 flush_vsx_to_thread(current);
1151 printk(KERN_INFO "VSX assist not supported at %lx\n", regs->nip);
1152 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1153}
1154#endif /* CONFIG_VSX */
1155
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001156#ifdef CONFIG_FSL_BOOKE
Kumar Gala620165f2009-02-12 13:54:53 +00001157
1158void doorbell_exception(struct pt_regs *regs)
1159{
1160#ifdef CONFIG_SMP
1161 int cpu = smp_processor_id();
1162 int msg;
1163
1164 if (num_online_cpus() < 2)
1165 return;
1166
1167 for (msg = 0; msg < 4; msg++)
1168 if (test_and_clear_bit(msg, &dbell_smp_message[cpu]))
1169 smp_message_recv(msg);
1170#else
1171 printk(KERN_WARNING "Received doorbell on non-smp system\n");
1172#endif
1173}
1174
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001175void CacheLockingException(struct pt_regs *regs, unsigned long address,
1176 unsigned long error_code)
1177{
1178 /* We treat cache locking instructions from the user
1179 * as priv ops, in the future we could try to do
1180 * something smarter
1181 */
1182 if (error_code & (ESR_DLK|ESR_ILK))
1183 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
1184 return;
1185}
1186#endif /* CONFIG_FSL_BOOKE */
1187
1188#ifdef CONFIG_SPE
1189void SPEFloatingPointException(struct pt_regs *regs)
1190{
Liu Yu6a800f32008-10-28 11:50:21 +08001191 extern int do_spe_mathemu(struct pt_regs *regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001192 unsigned long spefscr;
1193 int fpexc_mode;
1194 int code = 0;
Liu Yu6a800f32008-10-28 11:50:21 +08001195 int err;
1196
1197 preempt_disable();
1198 if (regs->msr & MSR_SPE)
1199 giveup_spe(current);
1200 preempt_enable();
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001201
1202 spefscr = current->thread.spefscr;
1203 fpexc_mode = current->thread.fpexc_mode;
1204
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001205 if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
1206 code = FPE_FLTOVF;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001207 }
1208 else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
1209 code = FPE_FLTUND;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001210 }
1211 else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
1212 code = FPE_FLTDIV;
1213 else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
1214 code = FPE_FLTINV;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001215 }
1216 else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
1217 code = FPE_FLTRES;
1218
Liu Yu6a800f32008-10-28 11:50:21 +08001219 err = do_spe_mathemu(regs);
1220 if (err == 0) {
1221 regs->nip += 4; /* skip emulated instruction */
1222 emulate_single_step(regs);
1223 return;
1224 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001225
Liu Yu6a800f32008-10-28 11:50:21 +08001226 if (err == -EFAULT) {
1227 /* got an error reading the instruction */
1228 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1229 } else if (err == -EINVAL) {
1230 /* didn't recognize the instruction */
1231 printk(KERN_ERR "unrecognized spe instruction "
1232 "in %s at %lx\n", current->comm, regs->nip);
1233 } else {
1234 _exception(SIGFPE, regs, code, regs->nip);
1235 }
1236
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001237 return;
1238}
Liu Yu6a800f32008-10-28 11:50:21 +08001239
1240void SPEFloatingPointRoundException(struct pt_regs *regs)
1241{
1242 extern int speround_handler(struct pt_regs *regs);
1243 int err;
1244
1245 preempt_disable();
1246 if (regs->msr & MSR_SPE)
1247 giveup_spe(current);
1248 preempt_enable();
1249
1250 regs->nip -= 4;
1251 err = speround_handler(regs);
1252 if (err == 0) {
1253 regs->nip += 4; /* skip emulated instruction */
1254 emulate_single_step(regs);
1255 return;
1256 }
1257
1258 if (err == -EFAULT) {
1259 /* got an error reading the instruction */
1260 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1261 } else if (err == -EINVAL) {
1262 /* didn't recognize the instruction */
1263 printk(KERN_ERR "unrecognized spe instruction "
1264 "in %s at %lx\n", current->comm, regs->nip);
1265 } else {
1266 _exception(SIGFPE, regs, 0, regs->nip);
1267 return;
1268 }
1269}
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001270#endif
1271
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001272/*
1273 * We enter here if we get an unrecoverable exception, that is, one
1274 * that happened at a point where the RI (recoverable interrupt) bit
1275 * in the MSR is 0. This indicates that SRR0/1 are live, and that
1276 * we therefore lost state by taking this exception.
1277 */
1278void unrecoverable_exception(struct pt_regs *regs)
1279{
1280 printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
1281 regs->trap, regs->nip);
1282 die("Unrecoverable exception", regs, SIGABRT);
1283}
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001284
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001285#ifdef CONFIG_BOOKE_WDT
1286/*
1287 * Default handler for a Watchdog exception,
1288 * spins until a reboot occurs
1289 */
1290void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
1291{
1292 /* Generic WatchdogHandler, implement your own */
1293 mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
1294 return;
1295}
1296
1297void WatchdogException(struct pt_regs *regs)
1298{
1299 printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
1300 WatchdogHandler(regs);
1301}
1302#endif
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001303
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001304/*
1305 * We enter here if we discover during exception entry that we are
1306 * running in supervisor mode with a userspace value in the stack pointer.
1307 */
1308void kernel_bad_stack(struct pt_regs *regs)
1309{
1310 printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
1311 regs->gpr[1], regs->nip);
1312 die("Bad kernel stack pointer", regs, SIGABRT);
1313}
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001314
1315void __init trap_init(void)
1316{
1317}
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001318
1319
1320#ifdef CONFIG_PPC_EMULATED_STATS
1321
1322#define WARN_EMULATED_SETUP(type) .type = { .name = #type }
1323
1324struct ppc_emulated ppc_emulated = {
1325#ifdef CONFIG_ALTIVEC
1326 WARN_EMULATED_SETUP(altivec),
1327#endif
1328 WARN_EMULATED_SETUP(dcba),
1329 WARN_EMULATED_SETUP(dcbz),
1330 WARN_EMULATED_SETUP(fp_pair),
1331 WARN_EMULATED_SETUP(isel),
1332 WARN_EMULATED_SETUP(mcrxr),
1333 WARN_EMULATED_SETUP(mfpvr),
1334 WARN_EMULATED_SETUP(multiple),
1335 WARN_EMULATED_SETUP(popcntb),
1336 WARN_EMULATED_SETUP(spe),
1337 WARN_EMULATED_SETUP(string),
1338 WARN_EMULATED_SETUP(unaligned),
1339#ifdef CONFIG_MATH_EMULATION
1340 WARN_EMULATED_SETUP(math),
1341#elif defined(CONFIG_8XX_MINIMAL_FPEMU)
1342 WARN_EMULATED_SETUP(8xx),
1343#endif
1344#ifdef CONFIG_VSX
1345 WARN_EMULATED_SETUP(vsx),
1346#endif
1347};
1348
1349u32 ppc_warn_emulated;
1350
1351void ppc_warn_emulated_print(const char *type)
1352{
1353 if (printk_ratelimit())
1354 pr_warning("%s used emulated %s instruction\n", current->comm,
1355 type);
1356}
1357
1358static int __init ppc_warn_emulated_init(void)
1359{
1360 struct dentry *dir, *d;
1361 unsigned int i;
1362 struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
1363
1364 if (!powerpc_debugfs_root)
1365 return -ENODEV;
1366
1367 dir = debugfs_create_dir("emulated_instructions",
1368 powerpc_debugfs_root);
1369 if (!dir)
1370 return -ENOMEM;
1371
1372 d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
1373 &ppc_warn_emulated);
1374 if (!d)
1375 goto fail;
1376
1377 for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
1378 d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
1379 (u32 *)&entries[i].val.counter);
1380 if (!d)
1381 goto fail;
1382 }
1383
1384 return 0;
1385
1386fail:
1387 debugfs_remove_recursive(dir);
1388 return -ENOMEM;
1389}
1390
1391device_initcall(ppc_warn_emulated_init);
1392
1393#endif /* CONFIG_PPC_EMULATED_STATS */