1 // SPDX-License-Identifier: GPL-2.0
3 * arch/sparc/kernel/traps.c
5 * Copyright 1995, 2008 David S. Miller (davem@davemloft.net)
6 * Copyright 2000 Jakub Jelinek (jakub@redhat.com)
10 * I hate traps on the sparc, grrr...
13 #include <linux/cpu.h>
14 #include <linux/sched/mm.h>
15 #include <linux/sched/debug.h>
16 #include <linux/mm_types.h>
17 #include <linux/kernel.h>
18 #include <linux/signal.h>
19 #include <linux/smp.h>
20 #include <linux/kdebug.h>
21 #include <linux/export.h>
22 #include <linux/pgtable.h>
24 #include <asm/delay.h>
25 #include <asm/ptrace.h>
26 #include <asm/oplib.h>
28 #include <asm/unistd.h>
29 #include <asm/traps.h>
34 /* #define TRAP_DEBUG */
36 static void instruction_dump(unsigned long *pc
)
40 if((((unsigned long) pc
) & 3))
43 for(i
= -3; i
< 6; i
++)
44 printk("%c%08lx%c",i
?' ':'<',pc
[i
],i
?' ':'>');
48 #define __SAVE __asm__ __volatile__("save %sp, -0x40, %sp\n\t")
49 #define __RESTORE __asm__ __volatile__("restore %g0, %g0, %g0\n\t")
51 void __noreturn
die_if_kernel(char *str
, struct pt_regs
*regs
)
53 static int die_counter
;
63 printk("%s(%d): %s [#%d]\n", current
->comm
, task_pid_nr(current
), str
, ++die_counter
);
65 add_taint(TAINT_DIE
, LOCKDEP_NOW_UNRELIABLE
);
67 __SAVE
; __SAVE
; __SAVE
; __SAVE
;
68 __SAVE
; __SAVE
; __SAVE
; __SAVE
;
69 __RESTORE
; __RESTORE
; __RESTORE
; __RESTORE
;
70 __RESTORE
; __RESTORE
; __RESTORE
; __RESTORE
;
73 struct reg_window32
*rw
= (struct reg_window32
*)regs
->u_regs
[UREG_FP
];
75 /* Stop the back trace when we hit userland or we
76 * find some badly aligned kernel stack. Set an upper
77 * bound in case our stack is trashed and we loop.
81 (((unsigned long) rw
) >= PAGE_OFFSET
) &&
82 !(((unsigned long) rw
) & 0x7)) {
83 printk("Caller[%08lx]: %pS\n", rw
->ins
[7],
85 rw
= (struct reg_window32
*)rw
->ins
[6];
88 printk("Instruction DUMP:");
89 instruction_dump ((unsigned long *) regs
->pc
);
90 make_task_dead((regs
->psr
& PSR_PS
) ? SIGKILL
: SIGSEGV
);
93 void do_hw_interrupt(struct pt_regs
*regs
, unsigned long type
)
96 /* Sun OS's puke from bad traps, Linux survives! */
97 printk("Unimplemented Sparc TRAP, type = %02lx\n", type
);
98 die_if_kernel("Whee... Hello Mr. Penguin", regs
);
101 if(regs
->psr
& PSR_PS
)
102 die_if_kernel("Kernel bad trap", regs
);
104 force_sig_fault_trapno(SIGILL
, ILL_ILLTRP
,
105 (void __user
*)regs
->pc
, type
- 0x80);
108 void do_illegal_instruction(struct pt_regs
*regs
, unsigned long pc
, unsigned long npc
,
112 die_if_kernel("Kernel illegal instruction", regs
);
114 printk("Ill instr. at pc=%08lx instruction is %08lx\n",
115 regs
->pc
, *(unsigned long *)regs
->pc
);
118 send_sig_fault(SIGILL
, ILL_ILLOPC
, (void __user
*)pc
, current
);
121 void do_priv_instruction(struct pt_regs
*regs
, unsigned long pc
, unsigned long npc
,
125 die_if_kernel("Penguin instruction from Penguin mode??!?!", regs
);
126 send_sig_fault(SIGILL
, ILL_PRVOPC
, (void __user
*)pc
, current
);
129 /* XXX User may want to be allowed to do this. XXX */
131 void do_memaccess_unaligned(struct pt_regs
*regs
, unsigned long pc
, unsigned long npc
,
134 if(regs
->psr
& PSR_PS
) {
135 printk("KERNEL MNA at pc %08lx npc %08lx called by %08lx\n", pc
, npc
,
136 regs
->u_regs
[UREG_RETPC
]);
137 die_if_kernel("BOGUS", regs
);
138 /* die_if_kernel("Kernel MNA access", regs); */
142 instruction_dump ((unsigned long *) regs
->pc
);
143 printk ("do_MNA!\n");
145 send_sig_fault(SIGBUS
, BUS_ADRALN
,
146 /* FIXME: Should dig out mna address */ (void *)0,
150 static unsigned long init_fsr
= 0x0UL
;
151 static unsigned long init_fregs
[32] __attribute__ ((aligned (8))) =
152 { ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL,
153 ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL,
154 ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL,
155 ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL };
157 void do_fpd_trap(struct pt_regs
*regs
, unsigned long pc
, unsigned long npc
,
160 /* Sanity check... */
162 die_if_kernel("Kernel gets FloatingPenguinUnit disabled trap", regs
);
164 put_psr(get_psr() | PSR_EF
); /* Allow FPU ops. */
167 if(last_task_used_math
== current
)
169 if(last_task_used_math
) {
170 /* Other processes fpu state, save away */
171 struct task_struct
*fptask
= last_task_used_math
;
172 fpsave(&fptask
->thread
.float_regs
[0], &fptask
->thread
.fsr
,
173 &fptask
->thread
.fpqueue
[0], &fptask
->thread
.fpqdepth
);
175 last_task_used_math
= current
;
177 fpload(¤t
->thread
.float_regs
[0], ¤t
->thread
.fsr
);
179 /* Set initial sane state. */
180 fpload(&init_fregs
[0], &init_fsr
);
185 fpload(&init_fregs
[0], &init_fsr
);
188 fpload(¤t
->thread
.float_regs
[0], ¤t
->thread
.fsr
);
190 set_thread_flag(TIF_USEDFPU
);
194 static unsigned long fake_regs
[32] __attribute__ ((aligned (8)));
195 static unsigned long fake_fsr
;
196 static unsigned long fake_queue
[32] __attribute__ ((aligned (8)));
197 static unsigned long fake_depth
;
199 void do_fpe_trap(struct pt_regs
*regs
, unsigned long pc
, unsigned long npc
,
207 struct task_struct
*fpt
= last_task_used_math
;
209 struct task_struct
*fpt
= current
;
211 put_psr(get_psr() | PSR_EF
);
212 /* If nobody owns the fpu right now, just clear the
213 * error into our fake static buffer and hope it don't
214 * happen again. Thank you crashme...
219 if (!test_tsk_thread_flag(fpt
, TIF_USEDFPU
)) {
221 fpsave(&fake_regs
[0], &fake_fsr
, &fake_queue
[0], &fake_depth
);
222 regs
->psr
&= ~PSR_EF
;
225 fpsave(&fpt
->thread
.float_regs
[0], &fpt
->thread
.fsr
,
226 &fpt
->thread
.fpqueue
[0], &fpt
->thread
.fpqdepth
);
228 printk("Hmm, FP exception, fsr was %016lx\n", fpt
->thread
.fsr
);
231 switch ((fpt
->thread
.fsr
& 0x1c000)) {
232 /* switch on the contents of the ftt [floating point trap type] field */
235 printk("IEEE_754_exception\n");
238 case (2 << 14): /* unfinished_FPop (underflow & co) */
239 case (3 << 14): /* unimplemented_FPop (quad stuff, maybe sqrt) */
240 ret
= do_mathemu(regs
, fpt
);
244 printk("sequence_error (OS bug...)\n");
247 printk("hardware_error (uhoh!)\n");
250 printk("invalid_fp_register (user error)\n");
252 #endif /* DEBUG_FPU */
254 /* If we successfully emulated the FPop, we pretend the trap never happened :-> */
256 fpload(¤t
->thread
.float_regs
[0], ¤t
->thread
.fsr
);
259 /* nope, better SIGFPE the offending process... */
262 clear_tsk_thread_flag(fpt
, TIF_USEDFPU
);
265 /* The first fsr store/load we tried trapped,
266 * the second one will not (we hope).
268 printk("WARNING: FPU exception from kernel mode. at pc=%08lx\n",
270 regs
->pc
= regs
->npc
;
274 die_if_kernel("Too many Penguin-FPU traps from kernel mode",
279 fsr
= fpt
->thread
.fsr
;
281 if ((fsr
& 0x1c000) == (1 << 14)) {
293 send_sig_fault(SIGFPE
, code
, (void __user
*)pc
, fpt
);
295 last_task_used_math
= NULL
;
297 regs
->psr
&= ~PSR_EF
;
302 void handle_tag_overflow(struct pt_regs
*regs
, unsigned long pc
, unsigned long npc
,
306 die_if_kernel("Penguin overflow trap from kernel mode", regs
);
307 send_sig_fault(SIGEMT
, EMT_TAGOVF
, (void __user
*)pc
, current
);
310 void handle_watchpoint(struct pt_regs
*regs
, unsigned long pc
, unsigned long npc
,
314 printk("Watchpoint detected at PC %08lx NPC %08lx PSR %08lx\n",
318 panic("Tell me what a watchpoint trap is, and I'll then deal "
319 "with such a beast...");
322 void handle_reg_access(struct pt_regs
*regs
, unsigned long pc
, unsigned long npc
,
326 printk("Register Access Exception at PC %08lx NPC %08lx PSR %08lx\n",
329 force_sig_fault(SIGBUS
, BUS_OBJERR
, (void __user
*)pc
);
332 void handle_cp_disabled(struct pt_regs
*regs
, unsigned long pc
, unsigned long npc
,
335 send_sig_fault(SIGILL
, ILL_COPROC
, (void __user
*)pc
, current
);
338 void handle_cp_exception(struct pt_regs
*regs
, unsigned long pc
, unsigned long npc
,
342 printk("Co-Processor Exception at PC %08lx NPC %08lx PSR %08lx\n",
345 send_sig_fault(SIGILL
, ILL_COPROC
, (void __user
*)pc
, current
);
348 void handle_hw_divzero(struct pt_regs
*regs
, unsigned long pc
, unsigned long npc
,
351 send_sig_fault(SIGFPE
, FPE_INTDIV
, (void __user
*)pc
, current
);
354 #ifdef CONFIG_DEBUG_BUGVERBOSE
355 void do_BUG(const char *file
, int line
)
357 // bust_spinlocks(1); XXX Not in our original BUG()
358 printk("kernel BUG at %s:%d!\n", file
, line
);
360 EXPORT_SYMBOL(do_BUG
);
363 /* Since we have our mappings set up, on multiprocessors we can spin them
364 * up here so that timer interrupts work during initialization.
369 extern void thread_info_offsets_are_bolixed_pete(void);
371 /* Force linker to barf if mismatched */
372 if (TI_UWINMASK
!= offsetof(struct thread_info
, uwinmask
) ||
373 TI_TASK
!= offsetof(struct thread_info
, task
) ||
374 TI_FLAGS
!= offsetof(struct thread_info
, flags
) ||
375 TI_CPU
!= offsetof(struct thread_info
, cpu
) ||
376 TI_PREEMPT
!= offsetof(struct thread_info
, preempt_count
) ||
377 TI_SOFTIRQ
!= offsetof(struct thread_info
, softirq_count
) ||
378 TI_HARDIRQ
!= offsetof(struct thread_info
, hardirq_count
) ||
379 TI_KSP
!= offsetof(struct thread_info
, ksp
) ||
380 TI_KPC
!= offsetof(struct thread_info
, kpc
) ||
381 TI_KPSR
!= offsetof(struct thread_info
, kpsr
) ||
382 TI_KWIM
!= offsetof(struct thread_info
, kwim
) ||
383 TI_REG_WINDOW
!= offsetof(struct thread_info
, reg_window
) ||
384 TI_RWIN_SPTRS
!= offsetof(struct thread_info
, rwbuf_stkptrs
) ||
385 TI_W_SAVED
!= offsetof(struct thread_info
, w_saved
))
386 thread_info_offsets_are_bolixed_pete();
388 /* Attach to the address space of init_task. */
390 current
->active_mm
= &init_mm
;
392 /* NOTE: Other cpus have this done as they are started