- Linus: drop support for old-style Makefiles entirely. Big.
[davej-history.git] / arch / ppc / kernel / traps.c
blob3b7473dda3f5da4f2e61a67ea6aea3386bce8f2e
1 /*
2 * linux/arch/ppc/kernel/traps.c
4 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
11 * Modified by Cort Dougan (cort@cs.nmt.edu)
12 * and Paul Mackerras (paulus@cs.anu.edu.au)
16 * This file handles the architecture-dependent parts of hardware exceptions
19 #include <linux/errno.h>
20 #include <linux/sched.h>
21 #include <linux/kernel.h>
22 #include <linux/mm.h>
23 #include <linux/stddef.h>
24 #include <linux/unistd.h>
25 #include <linux/ptrace.h>
26 #include <linux/malloc.h>
27 #include <linux/user.h>
28 #include <linux/a.out.h>
29 #include <linux/interrupt.h>
30 #include <linux/config.h>
31 #include <linux/init.h>
33 #include <asm/pgtable.h>
34 #include <asm/uaccess.h>
35 #include <asm/system.h>
36 #include <asm/io.h>
37 #include <asm/processor.h>
39 extern int fix_alignment(struct pt_regs *);
40 extern void bad_page_fault(struct pt_regs *, unsigned long);
42 #ifdef CONFIG_XMON
43 extern void xmon(struct pt_regs *regs);
44 extern int xmon_bpt(struct pt_regs *regs);
45 extern int xmon_sstep(struct pt_regs *regs);
46 extern int xmon_iabr_match(struct pt_regs *regs);
47 extern int xmon_dabr_match(struct pt_regs *regs);
48 extern void (*xmon_fault_handler)(struct pt_regs *regs);
49 #endif
51 #ifdef CONFIG_XMON
52 void (*debugger)(struct pt_regs *regs) = xmon;
53 int (*debugger_bpt)(struct pt_regs *regs) = xmon_bpt;
54 int (*debugger_sstep)(struct pt_regs *regs) = xmon_sstep;
55 int (*debugger_iabr_match)(struct pt_regs *regs) = xmon_iabr_match;
56 int (*debugger_dabr_match)(struct pt_regs *regs) = xmon_dabr_match;
57 void (*debugger_fault_handler)(struct pt_regs *regs);
58 #else
59 #ifdef CONFIG_KGDB
60 void (*debugger)(struct pt_regs *regs);
61 int (*debugger_bpt)(struct pt_regs *regs);
62 int (*debugger_sstep)(struct pt_regs *regs);
63 int (*debugger_iabr_match)(struct pt_regs *regs);
64 int (*debugger_dabr_match)(struct pt_regs *regs);
65 void (*debugger_fault_handler)(struct pt_regs *regs);
66 #endif
67 #endif
69 * Trap & Exception support
72 void
73 _exception(int signr, struct pt_regs *regs)
75 if (!user_mode(regs))
77 show_regs(regs);
78 #if defined(CONFIG_XMON) || defined(CONFIG_KGDB)
79 debugger(regs);
80 #endif
81 print_backtrace((unsigned long *)regs->gpr[1]);
82 panic("Exception in kernel pc %lx signal %d",regs->nip,signr);
84 force_sig(signr, current);
87 void
88 MachineCheckException(struct pt_regs *regs)
90 #ifdef CONFIG_ALL_PPC
91 unsigned long fixup;
92 #endif /* CONFIG_ALL_PPC */
94 if (user_mode(regs)) {
95 _exception(SIGSEGV, regs);
96 return;
99 #if defined(CONFIG_8xx) && defined(CONFIG_PCI)
100 /* the qspan pci read routines can cause machine checks -- Cort */
101 bad_page_fault(regs, regs->dar);
102 return;
103 #endif
104 #if defined(CONFIG_XMON) || defined(CONFIG_KGDB)
105 if (debugger_fault_handler) {
106 debugger_fault_handler(regs);
107 return;
109 #endif
111 #ifdef CONFIG_ALL_PPC
113 * I/O accesses can cause machine checks on powermacs.
114 * Check if the NIP corresponds to the address of a sync
115 * instruction for which there is an entry in the exception
116 * table.
118 if (regs->msr & (0x80000 | 0x40000)
119 && (fixup = search_exception_table(regs->nip)) != 0) {
121 * Check that it's a sync instruction.
122 * As the address is in the exception table
123 * we should be able to read the instr there.
125 if (*(unsigned int *)regs->nip == 0x7c0004ac) {
126 unsigned int lsi = ((unsigned int *)regs->nip)[-1];
127 int rb = (lsi >> 11) & 0x1f;
128 printk(KERN_DEBUG "%s bad port %lx at %lx\n",
129 (lsi & 0x100)? "OUT to": "IN from",
130 regs->gpr[rb] - _IO_BASE, regs->nip);
131 regs->nip = fixup;
132 return;
135 #endif /* CONFIG_ALL_PPC */
136 printk("Machine check in kernel mode.\n");
137 printk("Caused by (from SRR1=%lx): ", regs->msr);
138 switch (regs->msr & 0xF0000) {
139 case 0x80000:
140 printk("Machine check signal\n");
141 break;
142 case 0x40000:
143 printk("Transfer error ack signal\n");
144 break;
145 case 0x20000:
146 printk("Data parity error signal\n");
147 break;
148 case 0x10000:
149 printk("Address parity error signal\n");
150 break;
151 default:
152 printk("Unknown values in msr\n");
154 show_regs(regs);
155 #if defined(CONFIG_XMON) || defined(CONFIG_KGDB)
156 debugger(regs);
157 #endif
158 print_backtrace((unsigned long *)regs->gpr[1]);
159 panic("machine check");
162 void
163 SMIException(struct pt_regs *regs)
165 #if defined(CONFIG_XMON) || defined(CONFIG_KGDB)
167 debugger(regs);
168 return;
170 #endif
171 show_regs(regs);
172 print_backtrace((unsigned long *)regs->gpr[1]);
173 panic("System Management Interrupt");
176 void
177 UnknownException(struct pt_regs *regs)
179 printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
180 regs->nip, regs->msr, regs->trap);
181 _exception(SIGTRAP, regs);
184 void
185 InstructionBreakpoint(struct pt_regs *regs)
187 #if defined(CONFIG_XMON) || defined(CONFIG_KGDB)
188 if (debugger_iabr_match(regs))
189 return;
190 #endif
191 _exception(SIGTRAP, regs);
194 void
195 RunModeException(struct pt_regs *regs)
197 _exception(SIGTRAP, regs);
200 /* Illegal instruction emulation support. Originally written to
201 * provide the PVR to user applications using the mfspr rd, PVR.
202 * Return non-zero if we can't emulate, or EFAULT if the associated
203 * memory access caused an access fault. Return zero on success.
205 * There are a couple of ways to do this, either "decode" the instruction
206 * or directly match lots of bits. In this case, matching lots of
207 * bits is faster and easier.
210 #define INST_MFSPR_PVR 0x7c1f42a6
211 #define INST_MFSPR_PVR_MASK 0xfc1fffff
213 static int
214 emulate_instruction(struct pt_regs *regs)
216 uint instword;
217 uint rd;
218 uint retval;
220 retval = EFAULT;
222 if (!user_mode(regs))
223 return retval;
225 if (get_user(instword, (uint *)(regs->nip)))
226 return retval;
228 /* Emulate the mfspr rD, PVR.
230 if ((instword & INST_MFSPR_PVR_MASK) == INST_MFSPR_PVR) {
231 rd = (instword >> 21) & 0x1f;
232 regs->gpr[rd] = _get_PVR();
233 retval = 0;
235 if (retval == 0)
236 regs->nip += 4;
237 return(retval);
240 void
241 ProgramCheckException(struct pt_regs *regs)
243 #if defined(CONFIG_4xx)
244 unsigned int esr = mfspr(SPRN_ESR);
246 if (esr & ESR_PTR) {
247 #if defined(CONFIG_XMON) || defined(CONFIG_KGDB)
248 if (debugger_bpt(regs))
249 return;
250 #endif
251 _exception(SIGTRAP, regs);
252 } else {
253 _exception(SIGILL, regs);
255 #else
256 if (regs->msr & 0x100000) {
257 /* IEEE FP exception */
258 _exception(SIGFPE, regs);
259 } else if (regs->msr & 0x20000) {
260 /* trap exception */
261 #if defined(CONFIG_XMON) || defined(CONFIG_KGDB)
262 if (debugger_bpt(regs))
263 return;
264 #endif
265 _exception(SIGTRAP, regs);
266 } else {
267 /* Try to emulate it if we should. */
268 int errcode;
269 if ((errcode = emulate_instruction(regs))) {
270 if (errcode == EFAULT)
271 _exception(SIGBUS, regs);
272 else
273 _exception(SIGILL, regs);
276 #endif
279 void
280 SingleStepException(struct pt_regs *regs)
282 regs->msr &= ~MSR_SE; /* Turn off 'trace' bit */
283 #if defined(CONFIG_XMON) || defined(CONFIG_KGDB)
284 if (debugger_sstep(regs))
285 return;
286 #endif
287 _exception(SIGTRAP, regs);
290 void
291 AlignmentException(struct pt_regs *regs)
293 int fixed;
295 fixed = fix_alignment(regs);
296 if (fixed == 1) {
297 regs->nip += 4; /* skip over emulated instruction */
298 return;
300 if (fixed == -EFAULT) {
301 /* fixed == -EFAULT means the operand address was bad */
302 if (user_mode(regs))
303 force_sig(SIGSEGV, current);
304 else
305 bad_page_fault(regs, regs->dar);
306 return;
308 _exception(SIGBUS, regs);
311 void
312 StackOverflow(struct pt_regs *regs)
314 printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
315 current, regs->gpr[1]);
316 #if defined(CONFIG_XMON) || defined(CONFIG_KGDB)
317 debugger(regs);
318 #endif
319 show_regs(regs);
320 print_backtrace((unsigned long *)regs->gpr[1]);
321 panic("kernel stack overflow");
324 void
325 trace_syscall(struct pt_regs *regs)
327 printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld\n",
328 current, current->pid, regs->nip, regs->link, regs->gpr[0],
329 regs->ccr&0x10000000?"Error=":"", regs->gpr[3]);
332 #ifdef CONFIG_8xx
333 void
334 SoftwareEmulation(struct pt_regs *regs)
336 extern int do_mathemu(struct pt_regs *);
337 int errcode;
339 if (!user_mode(regs)) {
340 show_regs(regs);
341 #if defined(CONFIG_XMON) || defined(CONFIG_KGDB)
342 debugger(regs);
343 #endif
344 print_backtrace((unsigned long *)regs->gpr[1]);
345 panic("Kernel Mode Software FPU Emulation");
348 #ifdef CONFIG_MATH_EMULATION
349 if ((errcode = do_mathemu(regs))) {
350 #else
351 if ((errcode = Soft_emulate_8xx(regs))) {
352 #endif
353 if (errcode > 0)
354 _exception(SIGFPE, regs);
355 else if (errcode == -EFAULT)
356 _exception(SIGSEGV, regs);
357 else
358 _exception(SIGILL, regs);
361 #endif
363 void
364 TAUException(struct pt_regs *regs)
366 printk("TAU trap at PC: %lx, SR: %lx, vector=%lx\n",
367 regs->nip, regs->msr, regs->trap);
370 void __init trap_init(void)