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