blob: 5630e5a395e0d463ba072fb4dd47330e76d565e1 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Adrian Bunk88278ca2008-05-19 16:53:02 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * arch/sparc/kernel/traps.c
4 *
David S. Miller4fe3ebe2008-07-17 22:11:32 -07005 * Copyright 1995, 2008 David S. Miller (davem@davemloft.net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Copyright 2000 Jakub Jelinek (jakub@redhat.com)
7 */
8
9/*
10 * I hate traps on the sparc, grrr...
11 */
12
Ingo Molnar68e21be2017-02-01 19:08:20 +010013#include <linux/sched/mm.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010014#include <linux/sched/debug.h>
Ingo Molnar589ee622017-02-04 00:16:44 +010015#include <linux/mm_types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/signal.h>
18#include <linux/smp.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070019#include <linux/kdebug.h>
Paul Gortmaker7b64db62011-07-18 15:57:46 -040020#include <linux/export.h>
Mike Rapoport65fddcf2020-06-08 21:32:42 -070021#include <linux/pgtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#include <asm/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/ptrace.h>
25#include <asm/oplib.h>
26#include <asm/page.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/unistd.h>
28#include <asm/traps.h>
29
Sam Ravnborg8d74e322008-12-08 01:04:59 -080030#include "entry.h"
31#include "kernel.h"
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/* #define TRAP_DEBUG */
34
Adrian Bunkc61c65c2008-06-05 11:40:58 -070035static void instruction_dump(unsigned long *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
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
Sam Ravnborgfcd01962014-04-21 21:39:21 +020050void __noreturn die_if_kernel(char *str, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
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
Alexey Dobriyan19c58702007-10-18 23:40:41 -070062 printk("%s(%d): %s [#%d]\n", current->comm, task_pid_nr(current), str, ++die_counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 show_regs(regs);
Rusty Russell373d4d02013-01-21 17:17:39 +103064 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66 __SAVE; __SAVE; __SAVE; __SAVE;
67 __SAVE; __SAVE; __SAVE; __SAVE;
68 __RESTORE; __RESTORE; __RESTORE; __RESTORE;
69 __RESTORE; __RESTORE; __RESTORE; __RESTORE;
70
71 {
Sam Ravnborg4d7b92a2009-01-02 19:32:59 -080072 struct reg_window32 *rw = (struct reg_window32 *)regs->u_regs[UREG_FP];
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
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)) {
David S. Miller4fe3ebe2008-07-17 22:11:32 -070082 printk("Caller[%08lx]: %pS\n", rw->ins[7],
83 (void *) rw->ins[7]);
Sam Ravnborg4d7b92a2009-01-02 19:32:59 -080084 rw = (struct reg_window32 *)rw->ins[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
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{
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 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
Eric W. Biederman2c9f7ea2021-05-28 13:38:19 -0500105 force_sig_fault_trapno(SIGILL, ILL_ILLTRP,
106 (void __user *)regs->pc, type - 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
109void do_illegal_instruction(struct pt_regs *regs, unsigned long pc, unsigned long npc,
110 unsigned long psr)
111{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 if(psr & PSR_PS)
113 die_if_kernel("Kernel illegal instruction", regs);
114#ifdef TRAP_DEBUG
115 printk("Ill instr. at pc=%08lx instruction is %08lx\n",
116 regs->pc, *(unsigned long *)regs->pc);
117#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Eric W. Biederman2c9f7ea2021-05-28 13:38:19 -0500119 send_sig_fault(SIGILL, ILL_ILLOPC, (void __user *)pc, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
122void do_priv_instruction(struct pt_regs *regs, unsigned long pc, unsigned long npc,
123 unsigned long psr)
124{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 if(psr & PSR_PS)
126 die_if_kernel("Penguin instruction from Penguin mode??!?!", regs);
Eric W. Biederman2c9f7ea2021-05-28 13:38:19 -0500127 send_sig_fault(SIGILL, ILL_PRVOPC, (void __user *)pc, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
130/* XXX User may want to be allowed to do this. XXX */
131
132void do_memaccess_unaligned(struct pt_regs *regs, unsigned long pc, unsigned long npc,
133 unsigned long psr)
134{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 if(regs->psr & PSR_PS) {
136 printk("KERNEL MNA at pc %08lx npc %08lx called by %08lx\n", pc, npc,
137 regs->u_regs[UREG_RETPC]);
138 die_if_kernel("BOGUS", regs);
139 /* die_if_kernel("Kernel MNA access", regs); */
140 }
141#if 0
142 show_regs (regs);
143 instruction_dump ((unsigned long *) regs->pc);
144 printk ("do_MNA!\n");
145#endif
Eric W. Biederman0e3d9f12018-04-19 16:57:48 -0500146 send_sig_fault(SIGBUS, BUS_ADRALN,
147 /* FIXME: Should dig out mna address */ (void *)0,
Eric W. Biederman2c9f7ea2021-05-28 13:38:19 -0500148 current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151static unsigned long init_fsr = 0x0UL;
152static unsigned long init_fregs[32] __attribute__ ((aligned (8))) =
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,
156 ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL };
157
158void do_fpd_trap(struct pt_regs *regs, unsigned long pc, unsigned long npc,
159 unsigned long psr)
160{
161 /* Sanity check... */
162 if(psr & PSR_PS)
163 die_if_kernel("Kernel gets FloatingPenguinUnit disabled trap", regs);
164
165 put_psr(get_psr() | PSR_EF); /* Allow FPU ops. */
166 regs->psr |= PSR_EF;
167#ifndef CONFIG_SMP
168 if(last_task_used_math == current)
169 return;
170 if(last_task_used_math) {
171 /* Other processes fpu state, save away */
172 struct task_struct *fptask = last_task_used_math;
173 fpsave(&fptask->thread.float_regs[0], &fptask->thread.fsr,
174 &fptask->thread.fpqueue[0], &fptask->thread.fpqdepth);
175 }
176 last_task_used_math = current;
177 if(used_math()) {
178 fpload(&current->thread.float_regs[0], &current->thread.fsr);
179 } else {
180 /* Set initial sane state. */
181 fpload(&init_fregs[0], &init_fsr);
182 set_used_math();
183 }
184#else
185 if(!used_math()) {
186 fpload(&init_fregs[0], &init_fsr);
187 set_used_math();
188 } else {
189 fpload(&current->thread.float_regs[0], &current->thread.fsr);
190 }
William Lee Irwin III54f565e2007-03-12 17:08:25 -0700191 set_thread_flag(TIF_USEDFPU);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192#endif
193}
194
195static unsigned long fake_regs[32] __attribute__ ((aligned (8)));
196static unsigned long fake_fsr;
197static unsigned long fake_queue[32] __attribute__ ((aligned (8)));
198static unsigned long fake_depth;
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200void do_fpe_trap(struct pt_regs *regs, unsigned long pc, unsigned long npc,
201 unsigned long psr)
202{
203 static int calls;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 unsigned long fsr;
205 int ret = 0;
Eric W. Biederman0e3d9f12018-04-19 16:57:48 -0500206 int code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207#ifndef CONFIG_SMP
208 struct task_struct *fpt = last_task_used_math;
209#else
210 struct task_struct *fpt = current;
211#endif
212 put_psr(get_psr() | PSR_EF);
213 /* If nobody owns the fpu right now, just clear the
214 * error into our fake static buffer and hope it don't
215 * happen again. Thank you crashme...
216 */
217#ifndef CONFIG_SMP
218 if(!fpt) {
219#else
William Lee Irwin III54f565e2007-03-12 17:08:25 -0700220 if (!test_tsk_thread_flag(fpt, TIF_USEDFPU)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221#endif
222 fpsave(&fake_regs[0], &fake_fsr, &fake_queue[0], &fake_depth);
223 regs->psr &= ~PSR_EF;
224 return;
225 }
226 fpsave(&fpt->thread.float_regs[0], &fpt->thread.fsr,
227 &fpt->thread.fpqueue[0], &fpt->thread.fpqdepth);
228#ifdef DEBUG_FPU
229 printk("Hmm, FP exception, fsr was %016lx\n", fpt->thread.fsr);
230#endif
231
232 switch ((fpt->thread.fsr & 0x1c000)) {
233 /* switch on the contents of the ftt [floating point trap type] field */
234#ifdef DEBUG_FPU
235 case (1 << 14):
236 printk("IEEE_754_exception\n");
237 break;
238#endif
239 case (2 << 14): /* unfinished_FPop (underflow & co) */
240 case (3 << 14): /* unimplemented_FPop (quad stuff, maybe sqrt) */
241 ret = do_mathemu(regs, fpt);
242 break;
243#ifdef DEBUG_FPU
244 case (4 << 14):
245 printk("sequence_error (OS bug...)\n");
246 break;
247 case (5 << 14):
248 printk("hardware_error (uhoh!)\n");
249 break;
250 case (6 << 14):
251 printk("invalid_fp_register (user error)\n");
252 break;
253#endif /* DEBUG_FPU */
254 }
255 /* If we successfully emulated the FPop, we pretend the trap never happened :-> */
256 if (ret) {
257 fpload(&current->thread.float_regs[0], &current->thread.fsr);
258 return;
259 }
260 /* nope, better SIGFPE the offending process... */
261
262#ifdef CONFIG_SMP
William Lee Irwin III54f565e2007-03-12 17:08:25 -0700263 clear_tsk_thread_flag(fpt, TIF_USEDFPU);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264#endif
265 if(psr & PSR_PS) {
266 /* The first fsr store/load we tried trapped,
267 * the second one will not (we hope).
268 */
269 printk("WARNING: FPU exception from kernel mode. at pc=%08lx\n",
270 regs->pc);
271 regs->pc = regs->npc;
272 regs->npc += 4;
273 calls++;
274 if(calls > 2)
275 die_if_kernel("Too many Penguin-FPU traps from kernel mode",
276 regs);
277 return;
278 }
279
280 fsr = fpt->thread.fsr;
Eric W. Biederman0e3d9f12018-04-19 16:57:48 -0500281 code = FPE_FLTUNK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if ((fsr & 0x1c000) == (1 << 14)) {
283 if (fsr & 0x10)
Eric W. Biederman0e3d9f12018-04-19 16:57:48 -0500284 code = FPE_FLTINV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 else if (fsr & 0x08)
Eric W. Biederman0e3d9f12018-04-19 16:57:48 -0500286 code = FPE_FLTOVF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 else if (fsr & 0x04)
Eric W. Biederman0e3d9f12018-04-19 16:57:48 -0500288 code = FPE_FLTUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 else if (fsr & 0x02)
Eric W. Biederman0e3d9f12018-04-19 16:57:48 -0500290 code = FPE_FLTDIV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 else if (fsr & 0x01)
Eric W. Biederman0e3d9f12018-04-19 16:57:48 -0500292 code = FPE_FLTRES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
Eric W. Biederman2c9f7ea2021-05-28 13:38:19 -0500294 send_sig_fault(SIGFPE, code, (void __user *)pc, fpt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295#ifndef CONFIG_SMP
296 last_task_used_math = NULL;
297#endif
298 regs->psr &= ~PSR_EF;
299 if(calls > 0)
300 calls=0;
301}
302
303void handle_tag_overflow(struct pt_regs *regs, unsigned long pc, unsigned long npc,
304 unsigned long psr)
305{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if(psr & PSR_PS)
307 die_if_kernel("Penguin overflow trap from kernel mode", regs);
Eric W. Biederman2c9f7ea2021-05-28 13:38:19 -0500308 send_sig_fault(SIGEMT, EMT_TAGOVF, (void __user *)pc, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
311void handle_watchpoint(struct pt_regs *regs, unsigned long pc, unsigned long npc,
312 unsigned long psr)
313{
314#ifdef TRAP_DEBUG
315 printk("Watchpoint detected at PC %08lx NPC %08lx PSR %08lx\n",
316 pc, npc, psr);
317#endif
318 if(psr & PSR_PS)
319 panic("Tell me what a watchpoint trap is, and I'll then deal "
320 "with such a beast...");
321}
322
323void handle_reg_access(struct pt_regs *regs, unsigned long pc, unsigned long npc,
324 unsigned long psr)
325{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326#ifdef TRAP_DEBUG
327 printk("Register Access Exception at PC %08lx NPC %08lx PSR %08lx\n",
328 pc, npc, psr);
329#endif
Eric W. Biederman2c9f7ea2021-05-28 13:38:19 -0500330 force_sig_fault(SIGBUS, BUS_OBJERR, (void __user *)pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
333void handle_cp_disabled(struct pt_regs *regs, unsigned long pc, unsigned long npc,
334 unsigned long psr)
335{
Eric W. Biederman2c9f7ea2021-05-28 13:38:19 -0500336 send_sig_fault(SIGILL, ILL_COPROC, (void __user *)pc, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
339void handle_cp_exception(struct pt_regs *regs, unsigned long pc, unsigned long npc,
340 unsigned long psr)
341{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342#ifdef TRAP_DEBUG
343 printk("Co-Processor Exception at PC %08lx NPC %08lx PSR %08lx\n",
344 pc, npc, psr);
345#endif
Eric W. Biederman2c9f7ea2021-05-28 13:38:19 -0500346 send_sig_fault(SIGILL, ILL_COPROC, (void __user *)pc, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
349void handle_hw_divzero(struct pt_regs *regs, unsigned long pc, unsigned long npc,
350 unsigned long psr)
351{
Eric W. Biederman2c9f7ea2021-05-28 13:38:19 -0500352 send_sig_fault(SIGFPE, FPE_INTDIV, (void __user *)pc, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
355#ifdef CONFIG_DEBUG_BUGVERBOSE
356void do_BUG(const char *file, int line)
357{
358 // bust_spinlocks(1); XXX Not in our original BUG()
359 printk("kernel BUG at %s:%d!\n", file, line);
360}
Sam Ravnborg6943f3d2009-01-08 16:58:05 -0800361EXPORT_SYMBOL(do_BUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362#endif
363
364/* Since we have our mappings set up, on multiprocessors we can spin them
365 * up here so that timer interrupts work during initialization.
366 */
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368void trap_init(void)
369{
370 extern void thread_info_offsets_are_bolixed_pete(void);
371
372 /* Force linker to barf if mismatched */
373 if (TI_UWINMASK != offsetof(struct thread_info, uwinmask) ||
374 TI_TASK != offsetof(struct thread_info, task) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 TI_FLAGS != offsetof(struct thread_info, flags) ||
376 TI_CPU != offsetof(struct thread_info, cpu) ||
377 TI_PREEMPT != offsetof(struct thread_info, preempt_count) ||
378 TI_SOFTIRQ != offsetof(struct thread_info, softirq_count) ||
379 TI_HARDIRQ != offsetof(struct thread_info, hardirq_count) ||
380 TI_KSP != offsetof(struct thread_info, ksp) ||
381 TI_KPC != offsetof(struct thread_info, kpc) ||
382 TI_KPSR != offsetof(struct thread_info, kpsr) ||
383 TI_KWIM != offsetof(struct thread_info, kwim) ||
384 TI_REG_WINDOW != offsetof(struct thread_info, reg_window) ||
385 TI_RWIN_SPTRS != offsetof(struct thread_info, rwbuf_stkptrs) ||
386 TI_W_SAVED != offsetof(struct thread_info, w_saved))
387 thread_info_offsets_are_bolixed_pete();
388
389 /* Attach to the address space of init_task. */
Vegard Nossumf1f10072017-02-27 14:30:07 -0800390 mmgrab(&init_mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 current->active_mm = &init_mm;
392
393 /* NOTE: Other cpus have this done as they are started
394 * up on SMP.
395 */
396}