1 // SPDX-License-Identifier: GPL-2.0
3 * arch/sparc/kernel/traps.c
10 * I hate traps on the sparc, grrr...
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>
22 #include <asm/delay.h>
23 #include <asm/ptrace.h>
24 #include <asm/oplib.h>
26 #include <asm/pgtable.h>
27 #include <asm/unistd.h>
28 #include <asm/traps.h>
33 /* #define TRAP_DEBUG */
35 static void instruction_dump(unsigned long *pc)
39 if((((unsigned long) pc) & 3))
42 for(i = -3; i < 6; i++)
43 printk("%c%08lx%c",i?' ':'<',pc[i],i?' ':'>');
47 #define __SAVE __asm__ __volatile__("save %sp, -0x40, %sp\n\t")
48 #define __RESTORE __asm__ __volatile__("restore %g0, %g0, %g0\n\t")
50 void __noreturn die_if_kernel(char *str, struct pt_regs *regs)
52 static int die_counter;
62 printk("%s(%d): %s [#%d]\n", current->comm, task_pid_nr(current), str, ++die_counter);
64 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
66 __SAVE; __SAVE; __SAVE; __SAVE;
67 __SAVE; __SAVE; __SAVE; __SAVE;
68 __RESTORE; __RESTORE; __RESTORE; __RESTORE;
69 __RESTORE; __RESTORE; __RESTORE; __RESTORE;
72 struct reg_window32 *rw = (struct reg_window32 *)regs->u_regs[UREG_FP];
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.
80 (((unsigned long) rw) >= PAGE_OFFSET) &&
81 !(((unsigned long) rw) & 0x7)) {
82 printk("Caller[%08lx]: %pS\n", rw->ins[7],
84 rw = (struct reg_window32 *)rw->ins[6];
87 printk("Instruction DUMP:");
88 instruction_dump ((unsigned long *) regs->pc);
89 if(regs->psr & PSR_PS)
94 void do_hw_interrupt(struct pt_regs *regs, unsigned long type)
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);
104 if(regs->psr & PSR_PS)
105 die_if_kernel("Kernel bad trap", regs);
107 info.si_signo = SIGILL;
109 info.si_code = ILL_ILLTRP;
110 info.si_addr = (void __user *)regs->pc;
111 info.si_trapno = type - 0x80;
112 force_sig_info(SIGILL, &info, current);
115 void do_illegal_instruction(struct pt_regs *regs, unsigned long pc, unsigned long npc,
121 die_if_kernel("Kernel illegal instruction", regs);
123 printk("Ill instr. at pc=%08lx instruction is %08lx\n",
124 regs->pc, *(unsigned long *)regs->pc);
127 info.si_signo = SIGILL;
129 info.si_code = ILL_ILLOPC;
130 info.si_addr = (void __user *)pc;
132 send_sig_info(SIGILL, &info, current);
135 void do_priv_instruction(struct pt_regs *regs, unsigned long pc, unsigned long npc,
141 die_if_kernel("Penguin instruction from Penguin mode??!?!", regs);
142 info.si_signo = SIGILL;
144 info.si_code = ILL_PRVOPC;
145 info.si_addr = (void __user *)pc;
147 send_sig_info(SIGILL, &info, current);
150 /* XXX User may want to be allowed to do this. XXX */
152 void do_memaccess_unaligned(struct pt_regs *regs, unsigned long pc, unsigned long npc,
157 if(regs->psr & PSR_PS) {
158 printk("KERNEL MNA at pc %08lx npc %08lx called by %08lx\n", pc, npc,
159 regs->u_regs[UREG_RETPC]);
160 die_if_kernel("BOGUS", regs);
161 /* die_if_kernel("Kernel MNA access", regs); */
165 instruction_dump ((unsigned long *) regs->pc);
166 printk ("do_MNA!\n");
168 info.si_signo = SIGBUS;
170 info.si_code = BUS_ADRALN;
171 info.si_addr = /* FIXME: Should dig out mna address */ (void *)0;
173 send_sig_info(SIGBUS, &info, current);
176 static unsigned long init_fsr = 0x0UL;
177 static unsigned long init_fregs[32] __attribute__ ((aligned (8))) =
178 { ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL,
179 ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL,
180 ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL,
181 ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL };
183 void do_fpd_trap(struct pt_regs *regs, unsigned long pc, unsigned long npc,
186 /* Sanity check... */
188 die_if_kernel("Kernel gets FloatingPenguinUnit disabled trap", regs);
190 put_psr(get_psr() | PSR_EF); /* Allow FPU ops. */
193 if(last_task_used_math == current)
195 if(last_task_used_math) {
196 /* Other processes fpu state, save away */
197 struct task_struct *fptask = last_task_used_math;
198 fpsave(&fptask->thread.float_regs[0], &fptask->thread.fsr,
199 &fptask->thread.fpqueue[0], &fptask->thread.fpqdepth);
201 last_task_used_math = current;
203 fpload(¤t->thread.float_regs[0], ¤t->thread.fsr);
205 /* Set initial sane state. */
206 fpload(&init_fregs[0], &init_fsr);
211 fpload(&init_fregs[0], &init_fsr);
214 fpload(¤t->thread.float_regs[0], ¤t->thread.fsr);
216 set_thread_flag(TIF_USEDFPU);
220 static unsigned long fake_regs[32] __attribute__ ((aligned (8)));
221 static unsigned long fake_fsr;
222 static unsigned long fake_queue[32] __attribute__ ((aligned (8)));
223 static unsigned long fake_depth;
225 void do_fpe_trap(struct pt_regs *regs, unsigned long pc, unsigned long npc,
233 struct task_struct *fpt = last_task_used_math;
235 struct task_struct *fpt = current;
237 put_psr(get_psr() | PSR_EF);
238 /* If nobody owns the fpu right now, just clear the
239 * error into our fake static buffer and hope it don't
240 * happen again. Thank you crashme...
245 if (!test_tsk_thread_flag(fpt, TIF_USEDFPU)) {
247 fpsave(&fake_regs[0], &fake_fsr, &fake_queue[0], &fake_depth);
248 regs->psr &= ~PSR_EF;
251 fpsave(&fpt->thread.float_regs[0], &fpt->thread.fsr,
252 &fpt->thread.fpqueue[0], &fpt->thread.fpqdepth);
254 printk("Hmm, FP exception, fsr was %016lx\n", fpt->thread.fsr);
257 switch ((fpt->thread.fsr & 0x1c000)) {
258 /* switch on the contents of the ftt [floating point trap type] field */
261 printk("IEEE_754_exception\n");
264 case (2 << 14): /* unfinished_FPop (underflow & co) */
265 case (3 << 14): /* unimplemented_FPop (quad stuff, maybe sqrt) */
266 ret = do_mathemu(regs, fpt);
270 printk("sequence_error (OS bug...)\n");
273 printk("hardware_error (uhoh!)\n");
276 printk("invalid_fp_register (user error)\n");
278 #endif /* DEBUG_FPU */
280 /* If we successfully emulated the FPop, we pretend the trap never happened :-> */
282 fpload(¤t->thread.float_regs[0], ¤t->thread.fsr);
285 /* nope, better SIGFPE the offending process... */
288 clear_tsk_thread_flag(fpt, TIF_USEDFPU);
291 /* The first fsr store/load we tried trapped,
292 * the second one will not (we hope).
294 printk("WARNING: FPU exception from kernel mode. at pc=%08lx\n",
296 regs->pc = regs->npc;
300 die_if_kernel("Too many Penguin-FPU traps from kernel mode",
305 fsr = fpt->thread.fsr;
306 info.si_signo = SIGFPE;
308 info.si_addr = (void __user *)pc;
310 info.si_code = FPE_FIXME;
311 if ((fsr & 0x1c000) == (1 << 14)) {
313 info.si_code = FPE_FLTINV;
315 info.si_code = FPE_FLTOVF;
317 info.si_code = FPE_FLTUND;
319 info.si_code = FPE_FLTDIV;
321 info.si_code = FPE_FLTRES;
323 send_sig_info(SIGFPE, &info, fpt);
325 last_task_used_math = NULL;
327 regs->psr &= ~PSR_EF;
332 void handle_tag_overflow(struct pt_regs *regs, unsigned long pc, unsigned long npc,
338 die_if_kernel("Penguin overflow trap from kernel mode", regs);
339 info.si_signo = SIGEMT;
341 info.si_code = EMT_TAGOVF;
342 info.si_addr = (void __user *)pc;
344 send_sig_info(SIGEMT, &info, current);
347 void handle_watchpoint(struct pt_regs *regs, unsigned long pc, unsigned long npc,
351 printk("Watchpoint detected at PC %08lx NPC %08lx PSR %08lx\n",
355 panic("Tell me what a watchpoint trap is, and I'll then deal "
356 "with such a beast...");
359 void handle_reg_access(struct pt_regs *regs, unsigned long pc, unsigned long npc,
365 printk("Register Access Exception at PC %08lx NPC %08lx PSR %08lx\n",
368 info.si_signo = SIGBUS;
370 info.si_code = BUS_OBJERR;
371 info.si_addr = (void __user *)pc;
373 force_sig_info(SIGBUS, &info, current);
376 void handle_cp_disabled(struct pt_regs *regs, unsigned long pc, unsigned long npc,
381 info.si_signo = SIGILL;
383 info.si_code = ILL_COPROC;
384 info.si_addr = (void __user *)pc;
386 send_sig_info(SIGILL, &info, current);
389 void handle_cp_exception(struct pt_regs *regs, unsigned long pc, unsigned long npc,
395 printk("Co-Processor Exception at PC %08lx NPC %08lx PSR %08lx\n",
398 info.si_signo = SIGILL;
400 info.si_code = ILL_COPROC;
401 info.si_addr = (void __user *)pc;
403 send_sig_info(SIGILL, &info, current);
406 void handle_hw_divzero(struct pt_regs *regs, unsigned long pc, unsigned long npc,
411 info.si_signo = SIGFPE;
413 info.si_code = FPE_INTDIV;
414 info.si_addr = (void __user *)pc;
416 send_sig_info(SIGFPE, &info, current);
419 #ifdef CONFIG_DEBUG_BUGVERBOSE
420 void do_BUG(const char *file, int line)
422 // bust_spinlocks(1); XXX Not in our original BUG()
423 printk("kernel BUG at %s:%d!\n", file, line);
425 EXPORT_SYMBOL(do_BUG);
428 /* Since we have our mappings set up, on multiprocessors we can spin them
429 * up here so that timer interrupts work during initialization.
434 extern void thread_info_offsets_are_bolixed_pete(void);
436 /* Force linker to barf if mismatched */
437 if (TI_UWINMASK != offsetof(struct thread_info, uwinmask) ||
438 TI_TASK != offsetof(struct thread_info, task) ||
439 TI_FLAGS != offsetof(struct thread_info, flags) ||
440 TI_CPU != offsetof(struct thread_info, cpu) ||
441 TI_PREEMPT != offsetof(struct thread_info, preempt_count) ||
442 TI_SOFTIRQ != offsetof(struct thread_info, softirq_count) ||
443 TI_HARDIRQ != offsetof(struct thread_info, hardirq_count) ||
444 TI_KSP != offsetof(struct thread_info, ksp) ||
445 TI_KPC != offsetof(struct thread_info, kpc) ||
446 TI_KPSR != offsetof(struct thread_info, kpsr) ||
447 TI_KWIM != offsetof(struct thread_info, kwim) ||
448 TI_REG_WINDOW != offsetof(struct thread_info, reg_window) ||
449 TI_RWIN_SPTRS != offsetof(struct thread_info, rwbuf_stkptrs) ||
450 TI_W_SAVED != offsetof(struct thread_info, w_saved))
451 thread_info_offsets_are_bolixed_pete();
453 /* Attach to the address space of init_task. */
455 current->active_mm = &init_mm;
457 /* NOTE: Other cpus have this done as they are started