Loading...
1/*
2 * arch/sparc/kernel/traps.c
3 *
4 * Copyright 1995, 2008 David S. Miller (davem@davemloft.net)
5 * Copyright 2000 Jakub Jelinek (jakub@redhat.com)
6 */
7
8/*
9 * I hate traps on the sparc, grrr...
10 */
11
12#include <linux/sched.h> /* for jiffies */
13#include <linux/kernel.h>
14#include <linux/signal.h>
15#include <linux/smp.h>
16#include <linux/kdebug.h>
17
18#include <asm/delay.h>
19#include <asm/system.h>
20#include <asm/ptrace.h>
21#include <asm/oplib.h>
22#include <asm/page.h>
23#include <asm/pgtable.h>
24#include <asm/unistd.h>
25#include <asm/traps.h>
26
27#include "entry.h"
28#include "kernel.h"
29
30/* #define TRAP_DEBUG */
31
32static void instruction_dump(unsigned long *pc)
33{
34 int i;
35
36 if((((unsigned long) pc) & 3))
37 return;
38
39 for(i = -3; i < 6; i++)
40 printk("%c%08lx%c",i?' ':'<',pc[i],i?' ':'>');
41 printk("\n");
42}
43
44#define __SAVE __asm__ __volatile__("save %sp, -0x40, %sp\n\t")
45#define __RESTORE __asm__ __volatile__("restore %g0, %g0, %g0\n\t")
46
47void die_if_kernel(char *str, struct pt_regs *regs)
48{
49 static int die_counter;
50 int count = 0;
51
52 /* Amuse the user. */
53 printk(
54" \\|/ ____ \\|/\n"
55" \"@'/ ,. \\`@\"\n"
56" /_| \\__/ |_\\\n"
57" \\__U_/\n");
58
59 printk("%s(%d): %s [#%d]\n", current->comm, task_pid_nr(current), str, ++die_counter);
60 show_regs(regs);
61 add_taint(TAINT_DIE);
62
63 __SAVE; __SAVE; __SAVE; __SAVE;
64 __SAVE; __SAVE; __SAVE; __SAVE;
65 __RESTORE; __RESTORE; __RESTORE; __RESTORE;
66 __RESTORE; __RESTORE; __RESTORE; __RESTORE;
67
68 {
69 struct reg_window32 *rw = (struct reg_window32 *)regs->u_regs[UREG_FP];
70
71 /* Stop the back trace when we hit userland or we
72 * find some badly aligned kernel stack. Set an upper
73 * bound in case our stack is trashed and we loop.
74 */
75 while(rw &&
76 count++ < 30 &&
77 (((unsigned long) rw) >= PAGE_OFFSET) &&
78 !(((unsigned long) rw) & 0x7)) {
79 printk("Caller[%08lx]: %pS\n", rw->ins[7],
80 (void *) rw->ins[7]);
81 rw = (struct reg_window32 *)rw->ins[6];
82 }
83 }
84 printk("Instruction DUMP:");
85 instruction_dump ((unsigned long *) regs->pc);
86 if(regs->psr & PSR_PS)
87 do_exit(SIGKILL);
88 do_exit(SIGSEGV);
89}
90
91void do_hw_interrupt(struct pt_regs *regs, unsigned long type)
92{
93 siginfo_t info;
94
95 if(type < 0x80) {
96 /* Sun OS's puke from bad traps, Linux survives! */
97 printk("Unimplemented Sparc TRAP, type = %02lx\n", type);
98 die_if_kernel("Whee... Hello Mr. Penguin", regs);
99 }
100
101 if(regs->psr & PSR_PS)
102 die_if_kernel("Kernel bad trap", regs);
103
104 info.si_signo = SIGILL;
105 info.si_errno = 0;
106 info.si_code = ILL_ILLTRP;
107 info.si_addr = (void __user *)regs->pc;
108 info.si_trapno = type - 0x80;
109 force_sig_info(SIGILL, &info, current);
110}
111
112void do_illegal_instruction(struct pt_regs *regs, unsigned long pc, unsigned long npc,
113 unsigned long psr)
114{
115 siginfo_t info;
116
117 if(psr & PSR_PS)
118 die_if_kernel("Kernel illegal instruction", regs);
119#ifdef TRAP_DEBUG
120 printk("Ill instr. at pc=%08lx instruction is %08lx\n",
121 regs->pc, *(unsigned long *)regs->pc);
122#endif
123 if (!do_user_muldiv (regs, pc))
124 return;
125
126 info.si_signo = SIGILL;
127 info.si_errno = 0;
128 info.si_code = ILL_ILLOPC;
129 info.si_addr = (void __user *)pc;
130 info.si_trapno = 0;
131 send_sig_info(SIGILL, &info, current);
132}
133
134void do_priv_instruction(struct pt_regs *regs, unsigned long pc, unsigned long npc,
135 unsigned long psr)
136{
137 siginfo_t info;
138
139 if(psr & PSR_PS)
140 die_if_kernel("Penguin instruction from Penguin mode??!?!", regs);
141 info.si_signo = SIGILL;
142 info.si_errno = 0;
143 info.si_code = ILL_PRVOPC;
144 info.si_addr = (void __user *)pc;
145 info.si_trapno = 0;
146 send_sig_info(SIGILL, &info, current);
147}
148
149/* XXX User may want to be allowed to do this. XXX */
150
151void do_memaccess_unaligned(struct pt_regs *regs, unsigned long pc, unsigned long npc,
152 unsigned long psr)
153{
154 siginfo_t info;
155
156 if(regs->psr & PSR_PS) {
157 printk("KERNEL MNA at pc %08lx npc %08lx called by %08lx\n", pc, npc,
158 regs->u_regs[UREG_RETPC]);
159 die_if_kernel("BOGUS", regs);
160 /* die_if_kernel("Kernel MNA access", regs); */
161 }
162#if 0
163 show_regs (regs);
164 instruction_dump ((unsigned long *) regs->pc);
165 printk ("do_MNA!\n");
166#endif
167 info.si_signo = SIGBUS;
168 info.si_errno = 0;
169 info.si_code = BUS_ADRALN;
170 info.si_addr = /* FIXME: Should dig out mna address */ (void *)0;
171 info.si_trapno = 0;
172 send_sig_info(SIGBUS, &info, current);
173}
174
175static unsigned long init_fsr = 0x0UL;
176static unsigned long init_fregs[32] __attribute__ ((aligned (8))) =
177 { ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL,
178 ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL,
179 ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL,
180 ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL };
181
182void do_fpd_trap(struct pt_regs *regs, unsigned long pc, unsigned long npc,
183 unsigned long psr)
184{
185 /* Sanity check... */
186 if(psr & PSR_PS)
187 die_if_kernel("Kernel gets FloatingPenguinUnit disabled trap", regs);
188
189 put_psr(get_psr() | PSR_EF); /* Allow FPU ops. */
190 regs->psr |= PSR_EF;
191#ifndef CONFIG_SMP
192 if(last_task_used_math == current)
193 return;
194 if(last_task_used_math) {
195 /* Other processes fpu state, save away */
196 struct task_struct *fptask = last_task_used_math;
197 fpsave(&fptask->thread.float_regs[0], &fptask->thread.fsr,
198 &fptask->thread.fpqueue[0], &fptask->thread.fpqdepth);
199 }
200 last_task_used_math = current;
201 if(used_math()) {
202 fpload(¤t->thread.float_regs[0], ¤t->thread.fsr);
203 } else {
204 /* Set initial sane state. */
205 fpload(&init_fregs[0], &init_fsr);
206 set_used_math();
207 }
208#else
209 if(!used_math()) {
210 fpload(&init_fregs[0], &init_fsr);
211 set_used_math();
212 } else {
213 fpload(¤t->thread.float_regs[0], ¤t->thread.fsr);
214 }
215 set_thread_flag(TIF_USEDFPU);
216#endif
217}
218
219static unsigned long fake_regs[32] __attribute__ ((aligned (8)));
220static unsigned long fake_fsr;
221static unsigned long fake_queue[32] __attribute__ ((aligned (8)));
222static unsigned long fake_depth;
223
224extern int do_mathemu(struct pt_regs *, struct task_struct *);
225
226void do_fpe_trap(struct pt_regs *regs, unsigned long pc, unsigned long npc,
227 unsigned long psr)
228{
229 static int calls;
230 siginfo_t info;
231 unsigned long fsr;
232 int ret = 0;
233#ifndef CONFIG_SMP
234 struct task_struct *fpt = last_task_used_math;
235#else
236 struct task_struct *fpt = current;
237#endif
238 put_psr(get_psr() | PSR_EF);
239 /* If nobody owns the fpu right now, just clear the
240 * error into our fake static buffer and hope it don't
241 * happen again. Thank you crashme...
242 */
243#ifndef CONFIG_SMP
244 if(!fpt) {
245#else
246 if (!test_tsk_thread_flag(fpt, TIF_USEDFPU)) {
247#endif
248 fpsave(&fake_regs[0], &fake_fsr, &fake_queue[0], &fake_depth);
249 regs->psr &= ~PSR_EF;
250 return;
251 }
252 fpsave(&fpt->thread.float_regs[0], &fpt->thread.fsr,
253 &fpt->thread.fpqueue[0], &fpt->thread.fpqdepth);
254#ifdef DEBUG_FPU
255 printk("Hmm, FP exception, fsr was %016lx\n", fpt->thread.fsr);
256#endif
257
258 switch ((fpt->thread.fsr & 0x1c000)) {
259 /* switch on the contents of the ftt [floating point trap type] field */
260#ifdef DEBUG_FPU
261 case (1 << 14):
262 printk("IEEE_754_exception\n");
263 break;
264#endif
265 case (2 << 14): /* unfinished_FPop (underflow & co) */
266 case (3 << 14): /* unimplemented_FPop (quad stuff, maybe sqrt) */
267 ret = do_mathemu(regs, fpt);
268 break;
269#ifdef DEBUG_FPU
270 case (4 << 14):
271 printk("sequence_error (OS bug...)\n");
272 break;
273 case (5 << 14):
274 printk("hardware_error (uhoh!)\n");
275 break;
276 case (6 << 14):
277 printk("invalid_fp_register (user error)\n");
278 break;
279#endif /* DEBUG_FPU */
280 }
281 /* If we successfully emulated the FPop, we pretend the trap never happened :-> */
282 if (ret) {
283 fpload(¤t->thread.float_regs[0], ¤t->thread.fsr);
284 return;
285 }
286 /* nope, better SIGFPE the offending process... */
287
288#ifdef CONFIG_SMP
289 clear_tsk_thread_flag(fpt, TIF_USEDFPU);
290#endif
291 if(psr & PSR_PS) {
292 /* The first fsr store/load we tried trapped,
293 * the second one will not (we hope).
294 */
295 printk("WARNING: FPU exception from kernel mode. at pc=%08lx\n",
296 regs->pc);
297 regs->pc = regs->npc;
298 regs->npc += 4;
299 calls++;
300 if(calls > 2)
301 die_if_kernel("Too many Penguin-FPU traps from kernel mode",
302 regs);
303 return;
304 }
305
306 fsr = fpt->thread.fsr;
307 info.si_signo = SIGFPE;
308 info.si_errno = 0;
309 info.si_addr = (void __user *)pc;
310 info.si_trapno = 0;
311 info.si_code = __SI_FAULT;
312 if ((fsr & 0x1c000) == (1 << 14)) {
313 if (fsr & 0x10)
314 info.si_code = FPE_FLTINV;
315 else if (fsr & 0x08)
316 info.si_code = FPE_FLTOVF;
317 else if (fsr & 0x04)
318 info.si_code = FPE_FLTUND;
319 else if (fsr & 0x02)
320 info.si_code = FPE_FLTDIV;
321 else if (fsr & 0x01)
322 info.si_code = FPE_FLTRES;
323 }
324 send_sig_info(SIGFPE, &info, fpt);
325#ifndef CONFIG_SMP
326 last_task_used_math = NULL;
327#endif
328 regs->psr &= ~PSR_EF;
329 if(calls > 0)
330 calls=0;
331}
332
333void handle_tag_overflow(struct pt_regs *regs, unsigned long pc, unsigned long npc,
334 unsigned long psr)
335{
336 siginfo_t info;
337
338 if(psr & PSR_PS)
339 die_if_kernel("Penguin overflow trap from kernel mode", regs);
340 info.si_signo = SIGEMT;
341 info.si_errno = 0;
342 info.si_code = EMT_TAGOVF;
343 info.si_addr = (void __user *)pc;
344 info.si_trapno = 0;
345 send_sig_info(SIGEMT, &info, current);
346}
347
348void handle_watchpoint(struct pt_regs *regs, unsigned long pc, unsigned long npc,
349 unsigned long psr)
350{
351#ifdef TRAP_DEBUG
352 printk("Watchpoint detected at PC %08lx NPC %08lx PSR %08lx\n",
353 pc, npc, psr);
354#endif
355 if(psr & PSR_PS)
356 panic("Tell me what a watchpoint trap is, and I'll then deal "
357 "with such a beast...");
358}
359
360void handle_reg_access(struct pt_regs *regs, unsigned long pc, unsigned long npc,
361 unsigned long psr)
362{
363 siginfo_t info;
364
365#ifdef TRAP_DEBUG
366 printk("Register Access Exception at PC %08lx NPC %08lx PSR %08lx\n",
367 pc, npc, psr);
368#endif
369 info.si_signo = SIGBUS;
370 info.si_errno = 0;
371 info.si_code = BUS_OBJERR;
372 info.si_addr = (void __user *)pc;
373 info.si_trapno = 0;
374 force_sig_info(SIGBUS, &info, current);
375}
376
377void handle_cp_disabled(struct pt_regs *regs, unsigned long pc, unsigned long npc,
378 unsigned long psr)
379{
380 siginfo_t info;
381
382 info.si_signo = SIGILL;
383 info.si_errno = 0;
384 info.si_code = ILL_COPROC;
385 info.si_addr = (void __user *)pc;
386 info.si_trapno = 0;
387 send_sig_info(SIGILL, &info, current);
388}
389
390void handle_cp_exception(struct pt_regs *regs, unsigned long pc, unsigned long npc,
391 unsigned long psr)
392{
393 siginfo_t info;
394
395#ifdef TRAP_DEBUG
396 printk("Co-Processor Exception at PC %08lx NPC %08lx PSR %08lx\n",
397 pc, npc, psr);
398#endif
399 info.si_signo = SIGILL;
400 info.si_errno = 0;
401 info.si_code = ILL_COPROC;
402 info.si_addr = (void __user *)pc;
403 info.si_trapno = 0;
404 send_sig_info(SIGILL, &info, current);
405}
406
407void handle_hw_divzero(struct pt_regs *regs, unsigned long pc, unsigned long npc,
408 unsigned long psr)
409{
410 siginfo_t info;
411
412 info.si_signo = SIGFPE;
413 info.si_errno = 0;
414 info.si_code = FPE_INTDIV;
415 info.si_addr = (void __user *)pc;
416 info.si_trapno = 0;
417 send_sig_info(SIGFPE, &info, current);
418}
419
420#ifdef CONFIG_DEBUG_BUGVERBOSE
421void do_BUG(const char *file, int line)
422{
423 // bust_spinlocks(1); XXX Not in our original BUG()
424 printk("kernel BUG at %s:%d!\n", file, line);
425}
426EXPORT_SYMBOL(do_BUG);
427#endif
428
429/* Since we have our mappings set up, on multiprocessors we can spin them
430 * up here so that timer interrupts work during initialization.
431 */
432
433void trap_init(void)
434{
435 extern void thread_info_offsets_are_bolixed_pete(void);
436
437 /* Force linker to barf if mismatched */
438 if (TI_UWINMASK != offsetof(struct thread_info, uwinmask) ||
439 TI_TASK != offsetof(struct thread_info, task) ||
440 TI_EXECDOMAIN != offsetof(struct thread_info, exec_domain) ||
441 TI_FLAGS != offsetof(struct thread_info, flags) ||
442 TI_CPU != offsetof(struct thread_info, cpu) ||
443 TI_PREEMPT != offsetof(struct thread_info, preempt_count) ||
444 TI_SOFTIRQ != offsetof(struct thread_info, softirq_count) ||
445 TI_HARDIRQ != offsetof(struct thread_info, hardirq_count) ||
446 TI_KSP != offsetof(struct thread_info, ksp) ||
447 TI_KPC != offsetof(struct thread_info, kpc) ||
448 TI_KPSR != offsetof(struct thread_info, kpsr) ||
449 TI_KWIM != offsetof(struct thread_info, kwim) ||
450 TI_REG_WINDOW != offsetof(struct thread_info, reg_window) ||
451 TI_RWIN_SPTRS != offsetof(struct thread_info, rwbuf_stkptrs) ||
452 TI_W_SAVED != offsetof(struct thread_info, w_saved))
453 thread_info_offsets_are_bolixed_pete();
454
455 /* Attach to the address space of init_task. */
456 atomic_inc(&init_mm.mm_count);
457 current->active_mm = &init_mm;
458
459 /* NOTE: Other cpus have this done as they are started
460 * up on SMP.
461 */
462}
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * arch/sparc/kernel/traps.c
4 *
5 * Copyright 1995, 2008 David S. Miller (davem@davemloft.net)
6 * Copyright 2000 Jakub Jelinek (jakub@redhat.com)
7 */
8
9/*
10 * I hate traps on the sparc, grrr...
11 */
12
13#include <linux/sched/mm.h>
14#include <linux/sched/debug.h>
15#include <linux/mm_types.h>
16#include <linux/kernel.h>
17#include <linux/signal.h>
18#include <linux/smp.h>
19#include <linux/kdebug.h>
20#include <linux/export.h>
21
22#include <asm/delay.h>
23#include <asm/ptrace.h>
24#include <asm/oplib.h>
25#include <asm/page.h>
26#include <asm/pgtable.h>
27#include <asm/unistd.h>
28#include <asm/traps.h>
29
30#include "entry.h"
31#include "kernel.h"
32
33/* #define TRAP_DEBUG */
34
35static void instruction_dump(unsigned long *pc)
36{
37 int i;
38
39 if((((unsigned long) pc) & 3))
40 return;
41
42 for(i = -3; i < 6; i++)
43 printk("%c%08lx%c",i?' ':'<',pc[i],i?' ':'>');
44 printk("\n");
45}
46
47#define __SAVE __asm__ __volatile__("save %sp, -0x40, %sp\n\t")
48#define __RESTORE __asm__ __volatile__("restore %g0, %g0, %g0\n\t")
49
50void __noreturn die_if_kernel(char *str, struct pt_regs *regs)
51{
52 static int die_counter;
53 int count = 0;
54
55 /* Amuse the user. */
56 printk(
57" \\|/ ____ \\|/\n"
58" \"@'/ ,. \\`@\"\n"
59" /_| \\__/ |_\\\n"
60" \\__U_/\n");
61
62 printk("%s(%d): %s [#%d]\n", current->comm, task_pid_nr(current), str, ++die_counter);
63 show_regs(regs);
64 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
65
66 __SAVE; __SAVE; __SAVE; __SAVE;
67 __SAVE; __SAVE; __SAVE; __SAVE;
68 __RESTORE; __RESTORE; __RESTORE; __RESTORE;
69 __RESTORE; __RESTORE; __RESTORE; __RESTORE;
70
71 {
72 struct reg_window32 *rw = (struct reg_window32 *)regs->u_regs[UREG_FP];
73
74 /* Stop the back trace when we hit userland or we
75 * find some badly aligned kernel stack. Set an upper
76 * bound in case our stack is trashed and we loop.
77 */
78 while(rw &&
79 count++ < 30 &&
80 (((unsigned long) rw) >= PAGE_OFFSET) &&
81 !(((unsigned long) rw) & 0x7)) {
82 printk("Caller[%08lx]: %pS\n", rw->ins[7],
83 (void *) rw->ins[7]);
84 rw = (struct reg_window32 *)rw->ins[6];
85 }
86 }
87 printk("Instruction DUMP:");
88 instruction_dump ((unsigned long *) regs->pc);
89 if(regs->psr & PSR_PS)
90 do_exit(SIGKILL);
91 do_exit(SIGSEGV);
92}
93
94void do_hw_interrupt(struct pt_regs *regs, unsigned long type)
95{
96 siginfo_t info;
97
98 if(type < 0x80) {
99 /* Sun OS's puke from bad traps, Linux survives! */
100 printk("Unimplemented Sparc TRAP, type = %02lx\n", type);
101 die_if_kernel("Whee... Hello Mr. Penguin", regs);
102 }
103
104 if(regs->psr & PSR_PS)
105 die_if_kernel("Kernel bad trap", regs);
106
107 info.si_signo = SIGILL;
108 info.si_errno = 0;
109 info.si_code = ILL_ILLTRP;
110 info.si_addr = (void __user *)regs->pc;
111 info.si_trapno = type - 0x80;
112 force_sig_info(SIGILL, &info, current);
113}
114
115void do_illegal_instruction(struct pt_regs *regs, unsigned long pc, unsigned long npc,
116 unsigned long psr)
117{
118 siginfo_t info;
119
120 if(psr & PSR_PS)
121 die_if_kernel("Kernel illegal instruction", regs);
122#ifdef TRAP_DEBUG
123 printk("Ill instr. at pc=%08lx instruction is %08lx\n",
124 regs->pc, *(unsigned long *)regs->pc);
125#endif
126
127 info.si_signo = SIGILL;
128 info.si_errno = 0;
129 info.si_code = ILL_ILLOPC;
130 info.si_addr = (void __user *)pc;
131 info.si_trapno = 0;
132 send_sig_info(SIGILL, &info, current);
133}
134
135void do_priv_instruction(struct pt_regs *regs, unsigned long pc, unsigned long npc,
136 unsigned long psr)
137{
138 siginfo_t info;
139
140 if(psr & PSR_PS)
141 die_if_kernel("Penguin instruction from Penguin mode??!?!", regs);
142 info.si_signo = SIGILL;
143 info.si_errno = 0;
144 info.si_code = ILL_PRVOPC;
145 info.si_addr = (void __user *)pc;
146 info.si_trapno = 0;
147 send_sig_info(SIGILL, &info, current);
148}
149
150/* XXX User may want to be allowed to do this. XXX */
151
152void do_memaccess_unaligned(struct pt_regs *regs, unsigned long pc, unsigned long npc,
153 unsigned long psr)
154{
155 siginfo_t info;
156
157 if(regs->psr & PSR_PS) {
158 printk("KERNEL MNA at pc %08lx npc %08lx called by %08lx\n", pc, npc,
159 regs->u_regs[UREG_RETPC]);
160 die_if_kernel("BOGUS", regs);
161 /* die_if_kernel("Kernel MNA access", regs); */
162 }
163#if 0
164 show_regs (regs);
165 instruction_dump ((unsigned long *) regs->pc);
166 printk ("do_MNA!\n");
167#endif
168 info.si_signo = SIGBUS;
169 info.si_errno = 0;
170 info.si_code = BUS_ADRALN;
171 info.si_addr = /* FIXME: Should dig out mna address */ (void *)0;
172 info.si_trapno = 0;
173 send_sig_info(SIGBUS, &info, current);
174}
175
176static unsigned long init_fsr = 0x0UL;
177static unsigned long init_fregs[32] __attribute__ ((aligned (8))) =
178 { ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL,
179 ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL,
180 ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL,
181 ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL };
182
183void do_fpd_trap(struct pt_regs *regs, unsigned long pc, unsigned long npc,
184 unsigned long psr)
185{
186 /* Sanity check... */
187 if(psr & PSR_PS)
188 die_if_kernel("Kernel gets FloatingPenguinUnit disabled trap", regs);
189
190 put_psr(get_psr() | PSR_EF); /* Allow FPU ops. */
191 regs->psr |= PSR_EF;
192#ifndef CONFIG_SMP
193 if(last_task_used_math == current)
194 return;
195 if(last_task_used_math) {
196 /* Other processes fpu state, save away */
197 struct task_struct *fptask = last_task_used_math;
198 fpsave(&fptask->thread.float_regs[0], &fptask->thread.fsr,
199 &fptask->thread.fpqueue[0], &fptask->thread.fpqdepth);
200 }
201 last_task_used_math = current;
202 if(used_math()) {
203 fpload(¤t->thread.float_regs[0], ¤t->thread.fsr);
204 } else {
205 /* Set initial sane state. */
206 fpload(&init_fregs[0], &init_fsr);
207 set_used_math();
208 }
209#else
210 if(!used_math()) {
211 fpload(&init_fregs[0], &init_fsr);
212 set_used_math();
213 } else {
214 fpload(¤t->thread.float_regs[0], ¤t->thread.fsr);
215 }
216 set_thread_flag(TIF_USEDFPU);
217#endif
218}
219
220static unsigned long fake_regs[32] __attribute__ ((aligned (8)));
221static unsigned long fake_fsr;
222static unsigned long fake_queue[32] __attribute__ ((aligned (8)));
223static unsigned long fake_depth;
224
225void do_fpe_trap(struct pt_regs *regs, unsigned long pc, unsigned long npc,
226 unsigned long psr)
227{
228 static int calls;
229 siginfo_t info;
230 unsigned long fsr;
231 int ret = 0;
232#ifndef CONFIG_SMP
233 struct task_struct *fpt = last_task_used_math;
234#else
235 struct task_struct *fpt = current;
236#endif
237 put_psr(get_psr() | PSR_EF);
238 /* If nobody owns the fpu right now, just clear the
239 * error into our fake static buffer and hope it don't
240 * happen again. Thank you crashme...
241 */
242#ifndef CONFIG_SMP
243 if(!fpt) {
244#else
245 if (!test_tsk_thread_flag(fpt, TIF_USEDFPU)) {
246#endif
247 fpsave(&fake_regs[0], &fake_fsr, &fake_queue[0], &fake_depth);
248 regs->psr &= ~PSR_EF;
249 return;
250 }
251 fpsave(&fpt->thread.float_regs[0], &fpt->thread.fsr,
252 &fpt->thread.fpqueue[0], &fpt->thread.fpqdepth);
253#ifdef DEBUG_FPU
254 printk("Hmm, FP exception, fsr was %016lx\n", fpt->thread.fsr);
255#endif
256
257 switch ((fpt->thread.fsr & 0x1c000)) {
258 /* switch on the contents of the ftt [floating point trap type] field */
259#ifdef DEBUG_FPU
260 case (1 << 14):
261 printk("IEEE_754_exception\n");
262 break;
263#endif
264 case (2 << 14): /* unfinished_FPop (underflow & co) */
265 case (3 << 14): /* unimplemented_FPop (quad stuff, maybe sqrt) */
266 ret = do_mathemu(regs, fpt);
267 break;
268#ifdef DEBUG_FPU
269 case (4 << 14):
270 printk("sequence_error (OS bug...)\n");
271 break;
272 case (5 << 14):
273 printk("hardware_error (uhoh!)\n");
274 break;
275 case (6 << 14):
276 printk("invalid_fp_register (user error)\n");
277 break;
278#endif /* DEBUG_FPU */
279 }
280 /* If we successfully emulated the FPop, we pretend the trap never happened :-> */
281 if (ret) {
282 fpload(¤t->thread.float_regs[0], ¤t->thread.fsr);
283 return;
284 }
285 /* nope, better SIGFPE the offending process... */
286
287#ifdef CONFIG_SMP
288 clear_tsk_thread_flag(fpt, TIF_USEDFPU);
289#endif
290 if(psr & PSR_PS) {
291 /* The first fsr store/load we tried trapped,
292 * the second one will not (we hope).
293 */
294 printk("WARNING: FPU exception from kernel mode. at pc=%08lx\n",
295 regs->pc);
296 regs->pc = regs->npc;
297 regs->npc += 4;
298 calls++;
299 if(calls > 2)
300 die_if_kernel("Too many Penguin-FPU traps from kernel mode",
301 regs);
302 return;
303 }
304
305 fsr = fpt->thread.fsr;
306 info.si_signo = SIGFPE;
307 info.si_errno = 0;
308 info.si_addr = (void __user *)pc;
309 info.si_trapno = 0;
310 info.si_code = FPE_FIXME;
311 if ((fsr & 0x1c000) == (1 << 14)) {
312 if (fsr & 0x10)
313 info.si_code = FPE_FLTINV;
314 else if (fsr & 0x08)
315 info.si_code = FPE_FLTOVF;
316 else if (fsr & 0x04)
317 info.si_code = FPE_FLTUND;
318 else if (fsr & 0x02)
319 info.si_code = FPE_FLTDIV;
320 else if (fsr & 0x01)
321 info.si_code = FPE_FLTRES;
322 }
323 send_sig_info(SIGFPE, &info, fpt);
324#ifndef CONFIG_SMP
325 last_task_used_math = NULL;
326#endif
327 regs->psr &= ~PSR_EF;
328 if(calls > 0)
329 calls=0;
330}
331
332void handle_tag_overflow(struct pt_regs *regs, unsigned long pc, unsigned long npc,
333 unsigned long psr)
334{
335 siginfo_t info;
336
337 if(psr & PSR_PS)
338 die_if_kernel("Penguin overflow trap from kernel mode", regs);
339 info.si_signo = SIGEMT;
340 info.si_errno = 0;
341 info.si_code = EMT_TAGOVF;
342 info.si_addr = (void __user *)pc;
343 info.si_trapno = 0;
344 send_sig_info(SIGEMT, &info, current);
345}
346
347void handle_watchpoint(struct pt_regs *regs, unsigned long pc, unsigned long npc,
348 unsigned long psr)
349{
350#ifdef TRAP_DEBUG
351 printk("Watchpoint detected at PC %08lx NPC %08lx PSR %08lx\n",
352 pc, npc, psr);
353#endif
354 if(psr & PSR_PS)
355 panic("Tell me what a watchpoint trap is, and I'll then deal "
356 "with such a beast...");
357}
358
359void handle_reg_access(struct pt_regs *regs, unsigned long pc, unsigned long npc,
360 unsigned long psr)
361{
362 siginfo_t info;
363
364#ifdef TRAP_DEBUG
365 printk("Register Access Exception at PC %08lx NPC %08lx PSR %08lx\n",
366 pc, npc, psr);
367#endif
368 info.si_signo = SIGBUS;
369 info.si_errno = 0;
370 info.si_code = BUS_OBJERR;
371 info.si_addr = (void __user *)pc;
372 info.si_trapno = 0;
373 force_sig_info(SIGBUS, &info, current);
374}
375
376void handle_cp_disabled(struct pt_regs *regs, unsigned long pc, unsigned long npc,
377 unsigned long psr)
378{
379 siginfo_t info;
380
381 info.si_signo = SIGILL;
382 info.si_errno = 0;
383 info.si_code = ILL_COPROC;
384 info.si_addr = (void __user *)pc;
385 info.si_trapno = 0;
386 send_sig_info(SIGILL, &info, current);
387}
388
389void handle_cp_exception(struct pt_regs *regs, unsigned long pc, unsigned long npc,
390 unsigned long psr)
391{
392 siginfo_t info;
393
394#ifdef TRAP_DEBUG
395 printk("Co-Processor Exception at PC %08lx NPC %08lx PSR %08lx\n",
396 pc, npc, psr);
397#endif
398 info.si_signo = SIGILL;
399 info.si_errno = 0;
400 info.si_code = ILL_COPROC;
401 info.si_addr = (void __user *)pc;
402 info.si_trapno = 0;
403 send_sig_info(SIGILL, &info, current);
404}
405
406void handle_hw_divzero(struct pt_regs *regs, unsigned long pc, unsigned long npc,
407 unsigned long psr)
408{
409 siginfo_t info;
410
411 info.si_signo = SIGFPE;
412 info.si_errno = 0;
413 info.si_code = FPE_INTDIV;
414 info.si_addr = (void __user *)pc;
415 info.si_trapno = 0;
416 send_sig_info(SIGFPE, &info, current);
417}
418
419#ifdef CONFIG_DEBUG_BUGVERBOSE
420void do_BUG(const char *file, int line)
421{
422 // bust_spinlocks(1); XXX Not in our original BUG()
423 printk("kernel BUG at %s:%d!\n", file, line);
424}
425EXPORT_SYMBOL(do_BUG);
426#endif
427
428/* Since we have our mappings set up, on multiprocessors we can spin them
429 * up here so that timer interrupts work during initialization.
430 */
431
432void trap_init(void)
433{
434 extern void thread_info_offsets_are_bolixed_pete(void);
435
436 /* Force linker to barf if mismatched */
437 if (TI_UWINMASK != offsetof(struct thread_info, uwinmask) ||
438 TI_TASK != offsetof(struct thread_info, task) ||
439 TI_FLAGS != offsetof(struct thread_info, flags) ||
440 TI_CPU != offsetof(struct thread_info, cpu) ||
441 TI_PREEMPT != offsetof(struct thread_info, preempt_count) ||
442 TI_SOFTIRQ != offsetof(struct thread_info, softirq_count) ||
443 TI_HARDIRQ != offsetof(struct thread_info, hardirq_count) ||
444 TI_KSP != offsetof(struct thread_info, ksp) ||
445 TI_KPC != offsetof(struct thread_info, kpc) ||
446 TI_KPSR != offsetof(struct thread_info, kpsr) ||
447 TI_KWIM != offsetof(struct thread_info, kwim) ||
448 TI_REG_WINDOW != offsetof(struct thread_info, reg_window) ||
449 TI_RWIN_SPTRS != offsetof(struct thread_info, rwbuf_stkptrs) ||
450 TI_W_SAVED != offsetof(struct thread_info, w_saved))
451 thread_info_offsets_are_bolixed_pete();
452
453 /* Attach to the address space of init_task. */
454 mmgrab(&init_mm);
455 current->active_mm = &init_mm;
456
457 /* NOTE: Other cpus have this done as they are started
458 * up on SMP.
459 */
460}