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