GNU Linux-libre 4.14.319-gnu1
[releases.git] / arch / sparc / kernel / traps_32.c
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
35 static 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
50 void __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         make_task_dead((regs->psr & PSR_PS) ? SIGKILL : 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 {
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
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
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(&current->thread.float_regs[0], &current->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(&current->thread.float_regs[0], &current->thread.fsr);
213         }
214         set_thread_flag(TIF_USEDFPU);
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
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
243         if (!test_tsk_thread_flag(fpt, TIF_USEDFPU)) {
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(&current->thread.float_regs[0], &current->thread.fsr);
281                 return;
282         }
283         /* nope, better SIGFPE the offending process... */
284                
285 #ifdef CONFIG_SMP
286         clear_tsk_thread_flag(fpt, TIF_USEDFPU);
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 = FPE_FIXME;
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 }
423 EXPORT_SYMBOL(do_BUG);
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
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) ||
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. */
452         mmgrab(&init_mm);
453         current->active_mm = &init_mm;
454
455         /* NOTE: Other cpus have this done as they are started
456          *       up on SMP.
457          */
458 }