signal/sparc: Use send_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 if(psr & PSR_PS)
120 die_if_kernel("Kernel illegal instruction", regs);
121#ifdef TRAP_DEBUG
122 printk("Ill instr. at pc=%08lx instruction is %08lx\n",
123 regs->pc, *(unsigned long *)regs->pc);
124#endif
1da177e4 125
0e3d9f1e 126 send_sig_fault(SIGILL, ILL_ILLOPC, (void __user *)pc, 0, current);
1da177e4
LT
127}
128
129void do_priv_instruction(struct pt_regs *regs, unsigned long pc, unsigned long npc,
130 unsigned long psr)
131{
1da177e4
LT
132 if(psr & PSR_PS)
133 die_if_kernel("Penguin instruction from Penguin mode??!?!", regs);
0e3d9f1e 134 send_sig_fault(SIGILL, ILL_PRVOPC, (void __user *)pc, 0, current);
1da177e4
LT
135}
136
137/* XXX User may want to be allowed to do this. XXX */
138
139void do_memaccess_unaligned(struct pt_regs *regs, unsigned long pc, unsigned long npc,
140 unsigned long psr)
141{
1da177e4
LT
142 if(regs->psr & PSR_PS) {
143 printk("KERNEL MNA at pc %08lx npc %08lx called by %08lx\n", pc, npc,
144 regs->u_regs[UREG_RETPC]);
145 die_if_kernel("BOGUS", regs);
146 /* die_if_kernel("Kernel MNA access", regs); */
147 }
148#if 0
149 show_regs (regs);
150 instruction_dump ((unsigned long *) regs->pc);
151 printk ("do_MNA!\n");
152#endif
0e3d9f1e
EB
153 send_sig_fault(SIGBUS, BUS_ADRALN,
154 /* FIXME: Should dig out mna address */ (void *)0,
155 0, current);
1da177e4
LT
156}
157
1da177e4
LT
158static unsigned long init_fsr = 0x0UL;
159static unsigned long init_fregs[32] __attribute__ ((aligned (8))) =
160 { ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL,
161 ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL,
162 ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL,
163 ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL };
164
165void do_fpd_trap(struct pt_regs *regs, unsigned long pc, unsigned long npc,
166 unsigned long psr)
167{
168 /* Sanity check... */
169 if(psr & PSR_PS)
170 die_if_kernel("Kernel gets FloatingPenguinUnit disabled trap", regs);
171
172 put_psr(get_psr() | PSR_EF); /* Allow FPU ops. */
173 regs->psr |= PSR_EF;
174#ifndef CONFIG_SMP
175 if(last_task_used_math == current)
176 return;
177 if(last_task_used_math) {
178 /* Other processes fpu state, save away */
179 struct task_struct *fptask = last_task_used_math;
180 fpsave(&fptask->thread.float_regs[0], &fptask->thread.fsr,
181 &fptask->thread.fpqueue[0], &fptask->thread.fpqdepth);
182 }
183 last_task_used_math = current;
184 if(used_math()) {
185 fpload(&current->thread.float_regs[0], &current->thread.fsr);
186 } else {
187 /* Set initial sane state. */
188 fpload(&init_fregs[0], &init_fsr);
189 set_used_math();
190 }
191#else
192 if(!used_math()) {
193 fpload(&init_fregs[0], &init_fsr);
194 set_used_math();
195 } else {
196 fpload(&current->thread.float_regs[0], &current->thread.fsr);
197 }
54f565ea 198 set_thread_flag(TIF_USEDFPU);
1da177e4
LT
199#endif
200}
201
202static unsigned long fake_regs[32] __attribute__ ((aligned (8)));
203static unsigned long fake_fsr;
204static unsigned long fake_queue[32] __attribute__ ((aligned (8)));
205static unsigned long fake_depth;
206
1da177e4
LT
207void do_fpe_trap(struct pt_regs *regs, unsigned long pc, unsigned long npc,
208 unsigned long psr)
209{
210 static int calls;
1da177e4
LT
211 unsigned long fsr;
212 int ret = 0;
0e3d9f1e 213 int code;
1da177e4
LT
214#ifndef CONFIG_SMP
215 struct task_struct *fpt = last_task_used_math;
216#else
217 struct task_struct *fpt = current;
218#endif
219 put_psr(get_psr() | PSR_EF);
220 /* If nobody owns the fpu right now, just clear the
221 * error into our fake static buffer and hope it don't
222 * happen again. Thank you crashme...
223 */
224#ifndef CONFIG_SMP
225 if(!fpt) {
226#else
54f565ea 227 if (!test_tsk_thread_flag(fpt, TIF_USEDFPU)) {
1da177e4
LT
228#endif
229 fpsave(&fake_regs[0], &fake_fsr, &fake_queue[0], &fake_depth);
230 regs->psr &= ~PSR_EF;
231 return;
232 }
233 fpsave(&fpt->thread.float_regs[0], &fpt->thread.fsr,
234 &fpt->thread.fpqueue[0], &fpt->thread.fpqdepth);
235#ifdef DEBUG_FPU
236 printk("Hmm, FP exception, fsr was %016lx\n", fpt->thread.fsr);
237#endif
238
239 switch ((fpt->thread.fsr & 0x1c000)) {
240 /* switch on the contents of the ftt [floating point trap type] field */
241#ifdef DEBUG_FPU
242 case (1 << 14):
243 printk("IEEE_754_exception\n");
244 break;
245#endif
246 case (2 << 14): /* unfinished_FPop (underflow & co) */
247 case (3 << 14): /* unimplemented_FPop (quad stuff, maybe sqrt) */
248 ret = do_mathemu(regs, fpt);
249 break;
250#ifdef DEBUG_FPU
251 case (4 << 14):
252 printk("sequence_error (OS bug...)\n");
253 break;
254 case (5 << 14):
255 printk("hardware_error (uhoh!)\n");
256 break;
257 case (6 << 14):
258 printk("invalid_fp_register (user error)\n");
259 break;
260#endif /* DEBUG_FPU */
261 }
262 /* If we successfully emulated the FPop, we pretend the trap never happened :-> */
263 if (ret) {
264 fpload(&current->thread.float_regs[0], &current->thread.fsr);
265 return;
266 }
267 /* nope, better SIGFPE the offending process... */
268
269#ifdef CONFIG_SMP
54f565ea 270 clear_tsk_thread_flag(fpt, TIF_USEDFPU);
1da177e4
LT
271#endif
272 if(psr & PSR_PS) {
273 /* The first fsr store/load we tried trapped,
274 * the second one will not (we hope).
275 */
276 printk("WARNING: FPU exception from kernel mode. at pc=%08lx\n",
277 regs->pc);
278 regs->pc = regs->npc;
279 regs->npc += 4;
280 calls++;
281 if(calls > 2)
282 die_if_kernel("Too many Penguin-FPU traps from kernel mode",
283 regs);
284 return;
285 }
286
287 fsr = fpt->thread.fsr;
0e3d9f1e 288 code = FPE_FLTUNK;
1da177e4
LT
289 if ((fsr & 0x1c000) == (1 << 14)) {
290 if (fsr & 0x10)
0e3d9f1e 291 code = FPE_FLTINV;
1da177e4 292 else if (fsr & 0x08)
0e3d9f1e 293 code = FPE_FLTOVF;
1da177e4 294 else if (fsr & 0x04)
0e3d9f1e 295 code = FPE_FLTUND;
1da177e4 296 else if (fsr & 0x02)
0e3d9f1e 297 code = FPE_FLTDIV;
1da177e4 298 else if (fsr & 0x01)
0e3d9f1e 299 code = FPE_FLTRES;
1da177e4 300 }
0e3d9f1e 301 send_sig_fault(SIGFPE, code, (void __user *)pc, 0, fpt);
1da177e4
LT
302#ifndef CONFIG_SMP
303 last_task_used_math = NULL;
304#endif
305 regs->psr &= ~PSR_EF;
306 if(calls > 0)
307 calls=0;
308}
309
310void handle_tag_overflow(struct pt_regs *regs, unsigned long pc, unsigned long npc,
311 unsigned long psr)
312{
1da177e4
LT
313 if(psr & PSR_PS)
314 die_if_kernel("Penguin overflow trap from kernel mode", regs);
0e3d9f1e 315 send_sig_fault(SIGEMT, EMT_TAGOVF, (void __user *)pc, 0, current);
1da177e4
LT
316}
317
318void handle_watchpoint(struct pt_regs *regs, unsigned long pc, unsigned long npc,
319 unsigned long psr)
320{
321#ifdef TRAP_DEBUG
322 printk("Watchpoint detected at PC %08lx NPC %08lx PSR %08lx\n",
323 pc, npc, psr);
324#endif
325 if(psr & PSR_PS)
326 panic("Tell me what a watchpoint trap is, and I'll then deal "
327 "with such a beast...");
328}
329
330void handle_reg_access(struct pt_regs *regs, unsigned long pc, unsigned long npc,
331 unsigned long psr)
332{
333 siginfo_t info;
334
335#ifdef TRAP_DEBUG
336 printk("Register Access Exception at PC %08lx NPC %08lx PSR %08lx\n",
337 pc, npc, psr);
338#endif
3eb0f519 339 clear_siginfo(&info);
1da177e4
LT
340 info.si_signo = SIGBUS;
341 info.si_errno = 0;
342 info.si_code = BUS_OBJERR;
343 info.si_addr = (void __user *)pc;
344 info.si_trapno = 0;
345 force_sig_info(SIGBUS, &info, current);
346}
347
348void handle_cp_disabled(struct pt_regs *regs, unsigned long pc, unsigned long npc,
349 unsigned long psr)
350{
0e3d9f1e 351 send_sig_fault(SIGILL, ILL_COPROC, (void __user *)pc, 0, current);
1da177e4
LT
352}
353
354void handle_cp_exception(struct pt_regs *regs, unsigned long pc, unsigned long npc,
355 unsigned long psr)
356{
1da177e4
LT
357#ifdef TRAP_DEBUG
358 printk("Co-Processor Exception at PC %08lx NPC %08lx PSR %08lx\n",
359 pc, npc, psr);
360#endif
0e3d9f1e 361 send_sig_fault(SIGILL, ILL_COPROC, (void __user *)pc, 0, current);
1da177e4
LT
362}
363
364void handle_hw_divzero(struct pt_regs *regs, unsigned long pc, unsigned long npc,
365 unsigned long psr)
366{
0e3d9f1e 367 send_sig_fault(SIGFPE, FPE_INTDIV, (void __user *)pc, 0, current);
1da177e4
LT
368}
369
370#ifdef CONFIG_DEBUG_BUGVERBOSE
371void do_BUG(const char *file, int line)
372{
373 // bust_spinlocks(1); XXX Not in our original BUG()
374 printk("kernel BUG at %s:%d!\n", file, line);
375}
6943f3da 376EXPORT_SYMBOL(do_BUG);
1da177e4
LT
377#endif
378
379/* Since we have our mappings set up, on multiprocessors we can spin them
380 * up here so that timer interrupts work during initialization.
381 */
382
1da177e4
LT
383void trap_init(void)
384{
385 extern void thread_info_offsets_are_bolixed_pete(void);
386
387 /* Force linker to barf if mismatched */
388 if (TI_UWINMASK != offsetof(struct thread_info, uwinmask) ||
389 TI_TASK != offsetof(struct thread_info, task) ||
1da177e4
LT
390 TI_FLAGS != offsetof(struct thread_info, flags) ||
391 TI_CPU != offsetof(struct thread_info, cpu) ||
392 TI_PREEMPT != offsetof(struct thread_info, preempt_count) ||
393 TI_SOFTIRQ != offsetof(struct thread_info, softirq_count) ||
394 TI_HARDIRQ != offsetof(struct thread_info, hardirq_count) ||
395 TI_KSP != offsetof(struct thread_info, ksp) ||
396 TI_KPC != offsetof(struct thread_info, kpc) ||
397 TI_KPSR != offsetof(struct thread_info, kpsr) ||
398 TI_KWIM != offsetof(struct thread_info, kwim) ||
399 TI_REG_WINDOW != offsetof(struct thread_info, reg_window) ||
400 TI_RWIN_SPTRS != offsetof(struct thread_info, rwbuf_stkptrs) ||
401 TI_W_SAVED != offsetof(struct thread_info, w_saved))
402 thread_info_offsets_are_bolixed_pete();
403
404 /* Attach to the address space of init_task. */
f1f10076 405 mmgrab(&init_mm);
1da177e4
LT
406 current->active_mm = &init_mm;
407
408 /* NOTE: Other cpus have this done as they are started
409 * up on SMP.
410 */
411}