[PATCH] md: allow SET_BITMAP_FILE to work on 64bit kernel with 32bit userspace
[linux-2.6.22.y-op.git] / arch / sparc / kernel / traps.c
blob6a70d215fd0432c4e6783505a44370c75bc48e2d
1 /* $Id: traps.c,v 1.64 2000/09/03 15:00:49 anton Exp $
2 * arch/sparc/kernel/traps.c
4 * Copyright 1995 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright 2000 Jakub Jelinek (jakub@redhat.com)
6 */
8 /*
9 * I hate traps on the sparc, grrr...
12 #include <linux/sched.h> /* for jiffies */
13 #include <linux/kernel.h>
14 #include <linux/kallsyms.h>
15 #include <linux/signal.h>
16 #include <linux/smp.h>
17 #include <linux/smp_lock.h>
19 #include <asm/delay.h>
20 #include <asm/system.h>
21 #include <asm/ptrace.h>
22 #include <asm/oplib.h>
23 #include <asm/page.h>
24 #include <asm/pgtable.h>
25 #include <asm/kdebug.h>
26 #include <asm/unistd.h>
27 #include <asm/traps.h>
29 /* #define TRAP_DEBUG */
31 struct trap_trace_entry {
32 unsigned long pc;
33 unsigned long type;
36 int trap_curbuf = 0;
37 struct trap_trace_entry trapbuf[1024];
39 void syscall_trace_entry(struct pt_regs *regs)
41 printk("%s[%d]: ", current->comm, current->pid);
42 printk("scall<%d> (could be %d)\n", (int) regs->u_regs[UREG_G1],
43 (int) regs->u_regs[UREG_I0]);
46 void syscall_trace_exit(struct pt_regs *regs)
50 void sun4m_nmi(struct pt_regs *regs)
52 unsigned long afsr, afar;
54 printk("Aieee: sun4m NMI received!\n");
55 /* XXX HyperSparc hack XXX */
56 __asm__ __volatile__("mov 0x500, %%g1\n\t"
57 "lda [%%g1] 0x4, %0\n\t"
58 "mov 0x600, %%g1\n\t"
59 "lda [%%g1] 0x4, %1\n\t" :
60 "=r" (afsr), "=r" (afar));
61 printk("afsr=%08lx afar=%08lx\n", afsr, afar);
62 printk("you lose buddy boy...\n");
63 show_regs(regs);
64 prom_halt();
67 void sun4d_nmi(struct pt_regs *regs)
69 printk("Aieee: sun4d NMI received!\n");
70 printk("you lose buddy boy...\n");
71 show_regs(regs);
72 prom_halt();
75 void instruction_dump (unsigned long *pc)
77 int i;
79 if((((unsigned long) pc) & 3))
80 return;
82 for(i = -3; i < 6; i++)
83 printk("%c%08lx%c",i?' ':'<',pc[i],i?' ':'>');
84 printk("\n");
87 #define __SAVE __asm__ __volatile__("save %sp, -0x40, %sp\n\t")
88 #define __RESTORE __asm__ __volatile__("restore %g0, %g0, %g0\n\t")
90 void die_if_kernel(char *str, struct pt_regs *regs)
92 static int die_counter;
93 int count = 0;
95 /* Amuse the user. */
96 printk(
97 " \\|/ ____ \\|/\n"
98 " \"@'/ ,. \\`@\"\n"
99 " /_| \\__/ |_\\\n"
100 " \\__U_/\n");
102 printk("%s(%d): %s [#%d]\n", current->comm, current->pid, str, ++die_counter);
103 show_regs(regs);
105 __SAVE; __SAVE; __SAVE; __SAVE;
106 __SAVE; __SAVE; __SAVE; __SAVE;
107 __RESTORE; __RESTORE; __RESTORE; __RESTORE;
108 __RESTORE; __RESTORE; __RESTORE; __RESTORE;
111 struct reg_window *rw = (struct reg_window *)regs->u_regs[UREG_FP];
113 /* Stop the back trace when we hit userland or we
114 * find some badly aligned kernel stack. Set an upper
115 * bound in case our stack is trashed and we loop.
117 while(rw &&
118 count++ < 30 &&
119 (((unsigned long) rw) >= PAGE_OFFSET) &&
120 !(((unsigned long) rw) & 0x7)) {
121 printk("Caller[%08lx]", rw->ins[7]);
122 print_symbol(": %s\n", rw->ins[7]);
123 rw = (struct reg_window *)rw->ins[6];
126 printk("Instruction DUMP:");
127 instruction_dump ((unsigned long *) regs->pc);
128 if(regs->psr & PSR_PS)
129 do_exit(SIGKILL);
130 do_exit(SIGSEGV);
133 void do_hw_interrupt(struct pt_regs *regs, unsigned long type)
135 siginfo_t info;
137 if(type < 0x80) {
138 /* Sun OS's puke from bad traps, Linux survives! */
139 printk("Unimplemented Sparc TRAP, type = %02lx\n", type);
140 die_if_kernel("Whee... Hello Mr. Penguin", regs);
143 if(regs->psr & PSR_PS)
144 die_if_kernel("Kernel bad trap", regs);
146 info.si_signo = SIGILL;
147 info.si_errno = 0;
148 info.si_code = ILL_ILLTRP;
149 info.si_addr = (void __user *)regs->pc;
150 info.si_trapno = type - 0x80;
151 force_sig_info(SIGILL, &info, current);
154 void do_illegal_instruction(struct pt_regs *regs, unsigned long pc, unsigned long npc,
155 unsigned long psr)
157 extern int do_user_muldiv (struct pt_regs *, unsigned long);
158 siginfo_t info;
160 if(psr & PSR_PS)
161 die_if_kernel("Kernel illegal instruction", regs);
162 #ifdef TRAP_DEBUG
163 printk("Ill instr. at pc=%08lx instruction is %08lx\n",
164 regs->pc, *(unsigned long *)regs->pc);
165 #endif
166 if (!do_user_muldiv (regs, pc))
167 return;
169 info.si_signo = SIGILL;
170 info.si_errno = 0;
171 info.si_code = ILL_ILLOPC;
172 info.si_addr = (void __user *)pc;
173 info.si_trapno = 0;
174 send_sig_info(SIGILL, &info, current);
177 void do_priv_instruction(struct pt_regs *regs, unsigned long pc, unsigned long npc,
178 unsigned long psr)
180 siginfo_t info;
182 if(psr & PSR_PS)
183 die_if_kernel("Penguin instruction from Penguin mode??!?!", regs);
184 info.si_signo = SIGILL;
185 info.si_errno = 0;
186 info.si_code = ILL_PRVOPC;
187 info.si_addr = (void __user *)pc;
188 info.si_trapno = 0;
189 send_sig_info(SIGILL, &info, current);
192 /* XXX User may want to be allowed to do this. XXX */
194 void do_memaccess_unaligned(struct pt_regs *regs, unsigned long pc, unsigned long npc,
195 unsigned long psr)
197 siginfo_t info;
199 if(regs->psr & PSR_PS) {
200 printk("KERNEL MNA at pc %08lx npc %08lx called by %08lx\n", pc, npc,
201 regs->u_regs[UREG_RETPC]);
202 die_if_kernel("BOGUS", regs);
203 /* die_if_kernel("Kernel MNA access", regs); */
205 #if 0
206 show_regs (regs);
207 instruction_dump ((unsigned long *) regs->pc);
208 printk ("do_MNA!\n");
209 #endif
210 info.si_signo = SIGBUS;
211 info.si_errno = 0;
212 info.si_code = BUS_ADRALN;
213 info.si_addr = /* FIXME: Should dig out mna address */ (void *)0;
214 info.si_trapno = 0;
215 send_sig_info(SIGBUS, &info, current);
218 extern void fpsave(unsigned long *fpregs, unsigned long *fsr,
219 void *fpqueue, unsigned long *fpqdepth);
220 extern void fpload(unsigned long *fpregs, unsigned long *fsr);
222 static unsigned long init_fsr = 0x0UL;
223 static unsigned long init_fregs[32] __attribute__ ((aligned (8))) =
224 { ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL,
225 ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL,
226 ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL,
227 ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL };
229 void do_fpd_trap(struct pt_regs *regs, unsigned long pc, unsigned long npc,
230 unsigned long psr)
232 /* Sanity check... */
233 if(psr & PSR_PS)
234 die_if_kernel("Kernel gets FloatingPenguinUnit disabled trap", regs);
236 put_psr(get_psr() | PSR_EF); /* Allow FPU ops. */
237 regs->psr |= PSR_EF;
238 #ifndef CONFIG_SMP
239 if(last_task_used_math == current)
240 return;
241 if(last_task_used_math) {
242 /* Other processes fpu state, save away */
243 struct task_struct *fptask = last_task_used_math;
244 fpsave(&fptask->thread.float_regs[0], &fptask->thread.fsr,
245 &fptask->thread.fpqueue[0], &fptask->thread.fpqdepth);
247 last_task_used_math = current;
248 if(used_math()) {
249 fpload(&current->thread.float_regs[0], &current->thread.fsr);
250 } else {
251 /* Set initial sane state. */
252 fpload(&init_fregs[0], &init_fsr);
253 set_used_math();
255 #else
256 if(!used_math()) {
257 fpload(&init_fregs[0], &init_fsr);
258 set_used_math();
259 } else {
260 fpload(&current->thread.float_regs[0], &current->thread.fsr);
262 current_thread_info()->flags |= _TIF_USEDFPU;
263 #endif
266 static unsigned long fake_regs[32] __attribute__ ((aligned (8)));
267 static unsigned long fake_fsr;
268 static unsigned long fake_queue[32] __attribute__ ((aligned (8)));
269 static unsigned long fake_depth;
271 extern int do_mathemu(struct pt_regs *, struct task_struct *);
273 void do_fpe_trap(struct pt_regs *regs, unsigned long pc, unsigned long npc,
274 unsigned long psr)
276 static int calls;
277 siginfo_t info;
278 unsigned long fsr;
279 int ret = 0;
280 #ifndef CONFIG_SMP
281 struct task_struct *fpt = last_task_used_math;
282 #else
283 struct task_struct *fpt = current;
284 #endif
285 put_psr(get_psr() | PSR_EF);
286 /* If nobody owns the fpu right now, just clear the
287 * error into our fake static buffer and hope it don't
288 * happen again. Thank you crashme...
290 #ifndef CONFIG_SMP
291 if(!fpt) {
292 #else
293 if(!(task_thread_info(fpt)->flags & _TIF_USEDFPU)) {
294 #endif
295 fpsave(&fake_regs[0], &fake_fsr, &fake_queue[0], &fake_depth);
296 regs->psr &= ~PSR_EF;
297 return;
299 fpsave(&fpt->thread.float_regs[0], &fpt->thread.fsr,
300 &fpt->thread.fpqueue[0], &fpt->thread.fpqdepth);
301 #ifdef DEBUG_FPU
302 printk("Hmm, FP exception, fsr was %016lx\n", fpt->thread.fsr);
303 #endif
305 switch ((fpt->thread.fsr & 0x1c000)) {
306 /* switch on the contents of the ftt [floating point trap type] field */
307 #ifdef DEBUG_FPU
308 case (1 << 14):
309 printk("IEEE_754_exception\n");
310 break;
311 #endif
312 case (2 << 14): /* unfinished_FPop (underflow & co) */
313 case (3 << 14): /* unimplemented_FPop (quad stuff, maybe sqrt) */
314 ret = do_mathemu(regs, fpt);
315 break;
316 #ifdef DEBUG_FPU
317 case (4 << 14):
318 printk("sequence_error (OS bug...)\n");
319 break;
320 case (5 << 14):
321 printk("hardware_error (uhoh!)\n");
322 break;
323 case (6 << 14):
324 printk("invalid_fp_register (user error)\n");
325 break;
326 #endif /* DEBUG_FPU */
328 /* If we successfully emulated the FPop, we pretend the trap never happened :-> */
329 if (ret) {
330 fpload(&current->thread.float_regs[0], &current->thread.fsr);
331 return;
333 /* nope, better SIGFPE the offending process... */
335 #ifdef CONFIG_SMP
336 task_thread_info(fpt)->flags &= ~_TIF_USEDFPU;
337 #endif
338 if(psr & PSR_PS) {
339 /* The first fsr store/load we tried trapped,
340 * the second one will not (we hope).
342 printk("WARNING: FPU exception from kernel mode. at pc=%08lx\n",
343 regs->pc);
344 regs->pc = regs->npc;
345 regs->npc += 4;
346 calls++;
347 if(calls > 2)
348 die_if_kernel("Too many Penguin-FPU traps from kernel mode",
349 regs);
350 return;
353 fsr = fpt->thread.fsr;
354 info.si_signo = SIGFPE;
355 info.si_errno = 0;
356 info.si_addr = (void __user *)pc;
357 info.si_trapno = 0;
358 info.si_code = __SI_FAULT;
359 if ((fsr & 0x1c000) == (1 << 14)) {
360 if (fsr & 0x10)
361 info.si_code = FPE_FLTINV;
362 else if (fsr & 0x08)
363 info.si_code = FPE_FLTOVF;
364 else if (fsr & 0x04)
365 info.si_code = FPE_FLTUND;
366 else if (fsr & 0x02)
367 info.si_code = FPE_FLTDIV;
368 else if (fsr & 0x01)
369 info.si_code = FPE_FLTRES;
371 send_sig_info(SIGFPE, &info, fpt);
372 #ifndef CONFIG_SMP
373 last_task_used_math = NULL;
374 #endif
375 regs->psr &= ~PSR_EF;
376 if(calls > 0)
377 calls=0;
380 void handle_tag_overflow(struct pt_regs *regs, unsigned long pc, unsigned long npc,
381 unsigned long psr)
383 siginfo_t info;
385 if(psr & PSR_PS)
386 die_if_kernel("Penguin overflow trap from kernel mode", regs);
387 info.si_signo = SIGEMT;
388 info.si_errno = 0;
389 info.si_code = EMT_TAGOVF;
390 info.si_addr = (void __user *)pc;
391 info.si_trapno = 0;
392 send_sig_info(SIGEMT, &info, current);
395 void handle_watchpoint(struct pt_regs *regs, unsigned long pc, unsigned long npc,
396 unsigned long psr)
398 #ifdef TRAP_DEBUG
399 printk("Watchpoint detected at PC %08lx NPC %08lx PSR %08lx\n",
400 pc, npc, psr);
401 #endif
402 if(psr & PSR_PS)
403 panic("Tell me what a watchpoint trap is, and I'll then deal "
404 "with such a beast...");
407 void handle_reg_access(struct pt_regs *regs, unsigned long pc, unsigned long npc,
408 unsigned long psr)
410 siginfo_t info;
412 #ifdef TRAP_DEBUG
413 printk("Register Access Exception at PC %08lx NPC %08lx PSR %08lx\n",
414 pc, npc, psr);
415 #endif
416 info.si_signo = SIGBUS;
417 info.si_errno = 0;
418 info.si_code = BUS_OBJERR;
419 info.si_addr = (void __user *)pc;
420 info.si_trapno = 0;
421 force_sig_info(SIGBUS, &info, current);
424 void handle_cp_disabled(struct pt_regs *regs, unsigned long pc, unsigned long npc,
425 unsigned long psr)
427 siginfo_t info;
429 info.si_signo = SIGILL;
430 info.si_errno = 0;
431 info.si_code = ILL_COPROC;
432 info.si_addr = (void __user *)pc;
433 info.si_trapno = 0;
434 send_sig_info(SIGILL, &info, current);
437 void handle_cp_exception(struct pt_regs *regs, unsigned long pc, unsigned long npc,
438 unsigned long psr)
440 siginfo_t info;
442 #ifdef TRAP_DEBUG
443 printk("Co-Processor Exception at PC %08lx NPC %08lx PSR %08lx\n",
444 pc, npc, psr);
445 #endif
446 info.si_signo = SIGILL;
447 info.si_errno = 0;
448 info.si_code = ILL_COPROC;
449 info.si_addr = (void __user *)pc;
450 info.si_trapno = 0;
451 send_sig_info(SIGILL, &info, current);
454 void handle_hw_divzero(struct pt_regs *regs, unsigned long pc, unsigned long npc,
455 unsigned long psr)
457 siginfo_t info;
459 info.si_signo = SIGFPE;
460 info.si_errno = 0;
461 info.si_code = FPE_INTDIV;
462 info.si_addr = (void __user *)pc;
463 info.si_trapno = 0;
464 send_sig_info(SIGFPE, &info, current);
467 #ifdef CONFIG_DEBUG_BUGVERBOSE
468 void do_BUG(const char *file, int line)
470 // bust_spinlocks(1); XXX Not in our original BUG()
471 printk("kernel BUG at %s:%d!\n", file, line);
473 #endif
475 /* Since we have our mappings set up, on multiprocessors we can spin them
476 * up here so that timer interrupts work during initialization.
479 extern void sparc_cpu_startup(void);
481 int linux_smp_still_initting;
482 unsigned int thiscpus_tbr;
483 int thiscpus_mid;
485 void trap_init(void)
487 extern void thread_info_offsets_are_bolixed_pete(void);
489 /* Force linker to barf if mismatched */
490 if (TI_UWINMASK != offsetof(struct thread_info, uwinmask) ||
491 TI_TASK != offsetof(struct thread_info, task) ||
492 TI_EXECDOMAIN != offsetof(struct thread_info, exec_domain) ||
493 TI_FLAGS != offsetof(struct thread_info, flags) ||
494 TI_CPU != offsetof(struct thread_info, cpu) ||
495 TI_PREEMPT != offsetof(struct thread_info, preempt_count) ||
496 TI_SOFTIRQ != offsetof(struct thread_info, softirq_count) ||
497 TI_HARDIRQ != offsetof(struct thread_info, hardirq_count) ||
498 TI_KSP != offsetof(struct thread_info, ksp) ||
499 TI_KPC != offsetof(struct thread_info, kpc) ||
500 TI_KPSR != offsetof(struct thread_info, kpsr) ||
501 TI_KWIM != offsetof(struct thread_info, kwim) ||
502 TI_REG_WINDOW != offsetof(struct thread_info, reg_window) ||
503 TI_RWIN_SPTRS != offsetof(struct thread_info, rwbuf_stkptrs) ||
504 TI_W_SAVED != offsetof(struct thread_info, w_saved))
505 thread_info_offsets_are_bolixed_pete();
507 /* Attach to the address space of init_task. */
508 atomic_inc(&init_mm.mm_count);
509 current->active_mm = &init_mm;
511 /* NOTE: Other cpus have this done as they are started
512 * up on SMP.