xref: /openbmc/linux/arch/sparc/kernel/traps_32.c (revision 5d0e4d78)
1 /*
2  * arch/sparc/kernel/traps.c
3  *
4  * Copyright 1995, 2008 David S. Miller (davem@davemloft.net)
5  * Copyright 2000 Jakub Jelinek (jakub@redhat.com)
6  */
7 
8 /*
9  * I hate traps on the sparc, grrr...
10  */
11 
12 #include <linux/sched/mm.h>
13 #include <linux/sched/debug.h>
14 #include <linux/mm_types.h>
15 #include <linux/kernel.h>
16 #include <linux/signal.h>
17 #include <linux/smp.h>
18 #include <linux/kdebug.h>
19 #include <linux/export.h>
20 
21 #include <asm/delay.h>
22 #include <asm/ptrace.h>
23 #include <asm/oplib.h>
24 #include <asm/page.h>
25 #include <asm/pgtable.h>
26 #include <asm/unistd.h>
27 #include <asm/traps.h>
28 
29 #include "entry.h"
30 #include "kernel.h"
31 
32 /* #define TRAP_DEBUG */
33 
34 static void instruction_dump(unsigned long *pc)
35 {
36 	int i;
37 
38 	if((((unsigned long) pc) & 3))
39                 return;
40 
41 	for(i = -3; i < 6; i++)
42 		printk("%c%08lx%c",i?' ':'<',pc[i],i?' ':'>');
43 	printk("\n");
44 }
45 
46 #define __SAVE __asm__ __volatile__("save %sp, -0x40, %sp\n\t")
47 #define __RESTORE __asm__ __volatile__("restore %g0, %g0, %g0\n\t")
48 
49 void __noreturn die_if_kernel(char *str, struct pt_regs *regs)
50 {
51 	static int die_counter;
52 	int count = 0;
53 
54 	/* Amuse the user. */
55 	printk(
56 "              \\|/ ____ \\|/\n"
57 "              \"@'/ ,. \\`@\"\n"
58 "              /_| \\__/ |_\\\n"
59 "                 \\__U_/\n");
60 
61 	printk("%s(%d): %s [#%d]\n", current->comm, task_pid_nr(current), str, ++die_counter);
62 	show_regs(regs);
63 	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
64 
65 	__SAVE; __SAVE; __SAVE; __SAVE;
66 	__SAVE; __SAVE; __SAVE; __SAVE;
67 	__RESTORE; __RESTORE; __RESTORE; __RESTORE;
68 	__RESTORE; __RESTORE; __RESTORE; __RESTORE;
69 
70 	{
71 		struct reg_window32 *rw = (struct reg_window32 *)regs->u_regs[UREG_FP];
72 
73 		/* Stop the back trace when we hit userland or we
74 		 * find some badly aligned kernel stack. Set an upper
75 		 * bound in case our stack is trashed and we loop.
76 		 */
77 		while(rw					&&
78 		      count++ < 30				&&
79                       (((unsigned long) rw) >= PAGE_OFFSET)	&&
80 		      !(((unsigned long) rw) & 0x7)) {
81 			printk("Caller[%08lx]: %pS\n", rw->ins[7],
82 			       (void *) rw->ins[7]);
83 			rw = (struct reg_window32 *)rw->ins[6];
84 		}
85 	}
86 	printk("Instruction DUMP:");
87 	instruction_dump ((unsigned long *) regs->pc);
88 	if(regs->psr & PSR_PS)
89 		do_exit(SIGKILL);
90 	do_exit(SIGSEGV);
91 }
92 
93 void do_hw_interrupt(struct pt_regs *regs, unsigned long type)
94 {
95 	siginfo_t info;
96 
97 	if(type < 0x80) {
98 		/* Sun OS's puke from bad traps, Linux survives! */
99 		printk("Unimplemented Sparc TRAP, type = %02lx\n", type);
100 		die_if_kernel("Whee... Hello Mr. Penguin", regs);
101 	}
102 
103 	if(regs->psr & PSR_PS)
104 		die_if_kernel("Kernel bad trap", regs);
105 
106 	info.si_signo = SIGILL;
107 	info.si_errno = 0;
108 	info.si_code = ILL_ILLTRP;
109 	info.si_addr = (void __user *)regs->pc;
110 	info.si_trapno = type - 0x80;
111 	force_sig_info(SIGILL, &info, current);
112 }
113 
114 void do_illegal_instruction(struct pt_regs *regs, unsigned long pc, unsigned long npc,
115 			    unsigned long psr)
116 {
117 	siginfo_t info;
118 
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
125 
126 	info.si_signo = SIGILL;
127 	info.si_errno = 0;
128 	info.si_code = ILL_ILLOPC;
129 	info.si_addr = (void __user *)pc;
130 	info.si_trapno = 0;
131 	send_sig_info(SIGILL, &info, current);
132 }
133 
134 void do_priv_instruction(struct pt_regs *regs, unsigned long pc, unsigned long npc,
135 			 unsigned long psr)
136 {
137 	siginfo_t info;
138 
139 	if(psr & PSR_PS)
140 		die_if_kernel("Penguin instruction from Penguin mode??!?!", regs);
141 	info.si_signo = SIGILL;
142 	info.si_errno = 0;
143 	info.si_code = ILL_PRVOPC;
144 	info.si_addr = (void __user *)pc;
145 	info.si_trapno = 0;
146 	send_sig_info(SIGILL, &info, current);
147 }
148 
149 /* XXX User may want to be allowed to do this. XXX */
150 
151 void do_memaccess_unaligned(struct pt_regs *regs, unsigned long pc, unsigned long npc,
152 			    unsigned long psr)
153 {
154 	siginfo_t info;
155 
156 	if(regs->psr & PSR_PS) {
157 		printk("KERNEL MNA at pc %08lx npc %08lx called by %08lx\n", pc, npc,
158 		       regs->u_regs[UREG_RETPC]);
159 		die_if_kernel("BOGUS", regs);
160 		/* die_if_kernel("Kernel MNA access", regs); */
161 	}
162 #if 0
163 	show_regs (regs);
164 	instruction_dump ((unsigned long *) regs->pc);
165 	printk ("do_MNA!\n");
166 #endif
167 	info.si_signo = SIGBUS;
168 	info.si_errno = 0;
169 	info.si_code = BUS_ADRALN;
170 	info.si_addr = /* FIXME: Should dig out mna address */ (void *)0;
171 	info.si_trapno = 0;
172 	send_sig_info(SIGBUS, &info, current);
173 }
174 
175 static unsigned long init_fsr = 0x0UL;
176 static unsigned long init_fregs[32] __attribute__ ((aligned (8))) =
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 		  ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL };
181 
182 void do_fpd_trap(struct pt_regs *regs, unsigned long pc, unsigned long npc,
183 		 unsigned long psr)
184 {
185 	/* Sanity check... */
186 	if(psr & PSR_PS)
187 		die_if_kernel("Kernel gets FloatingPenguinUnit disabled trap", regs);
188 
189 	put_psr(get_psr() | PSR_EF);    /* Allow FPU ops. */
190 	regs->psr |= PSR_EF;
191 #ifndef CONFIG_SMP
192 	if(last_task_used_math == current)
193 		return;
194 	if(last_task_used_math) {
195 		/* Other processes fpu state, save away */
196 		struct task_struct *fptask = last_task_used_math;
197 		fpsave(&fptask->thread.float_regs[0], &fptask->thread.fsr,
198 		       &fptask->thread.fpqueue[0], &fptask->thread.fpqdepth);
199 	}
200 	last_task_used_math = current;
201 	if(used_math()) {
202 		fpload(&current->thread.float_regs[0], &current->thread.fsr);
203 	} else {
204 		/* Set initial sane state. */
205 		fpload(&init_fregs[0], &init_fsr);
206 		set_used_math();
207 	}
208 #else
209 	if(!used_math()) {
210 		fpload(&init_fregs[0], &init_fsr);
211 		set_used_math();
212 	} else {
213 		fpload(&current->thread.float_regs[0], &current->thread.fsr);
214 	}
215 	set_thread_flag(TIF_USEDFPU);
216 #endif
217 }
218 
219 static unsigned long fake_regs[32] __attribute__ ((aligned (8)));
220 static unsigned long fake_fsr;
221 static unsigned long fake_queue[32] __attribute__ ((aligned (8)));
222 static unsigned long fake_depth;
223 
224 void do_fpe_trap(struct pt_regs *regs, unsigned long pc, unsigned long npc,
225 		 unsigned long psr)
226 {
227 	static int calls;
228 	siginfo_t info;
229 	unsigned long fsr;
230 	int ret = 0;
231 #ifndef CONFIG_SMP
232 	struct task_struct *fpt = last_task_used_math;
233 #else
234 	struct task_struct *fpt = current;
235 #endif
236 	put_psr(get_psr() | PSR_EF);
237 	/* If nobody owns the fpu right now, just clear the
238 	 * error into our fake static buffer and hope it don't
239 	 * happen again.  Thank you crashme...
240 	 */
241 #ifndef CONFIG_SMP
242 	if(!fpt) {
243 #else
244 	if (!test_tsk_thread_flag(fpt, TIF_USEDFPU)) {
245 #endif
246 		fpsave(&fake_regs[0], &fake_fsr, &fake_queue[0], &fake_depth);
247 		regs->psr &= ~PSR_EF;
248 		return;
249 	}
250 	fpsave(&fpt->thread.float_regs[0], &fpt->thread.fsr,
251 	       &fpt->thread.fpqueue[0], &fpt->thread.fpqdepth);
252 #ifdef DEBUG_FPU
253 	printk("Hmm, FP exception, fsr was %016lx\n", fpt->thread.fsr);
254 #endif
255 
256 	switch ((fpt->thread.fsr & 0x1c000)) {
257 	/* switch on the contents of the ftt [floating point trap type] field */
258 #ifdef DEBUG_FPU
259 	case (1 << 14):
260 		printk("IEEE_754_exception\n");
261 		break;
262 #endif
263 	case (2 << 14):  /* unfinished_FPop (underflow & co) */
264 	case (3 << 14):  /* unimplemented_FPop (quad stuff, maybe sqrt) */
265 		ret = do_mathemu(regs, fpt);
266 		break;
267 #ifdef DEBUG_FPU
268 	case (4 << 14):
269 		printk("sequence_error (OS bug...)\n");
270 		break;
271 	case (5 << 14):
272 		printk("hardware_error (uhoh!)\n");
273 		break;
274 	case (6 << 14):
275 		printk("invalid_fp_register (user error)\n");
276 		break;
277 #endif /* DEBUG_FPU */
278 	}
279 	/* If we successfully emulated the FPop, we pretend the trap never happened :-> */
280 	if (ret) {
281 		fpload(&current->thread.float_regs[0], &current->thread.fsr);
282 		return;
283 	}
284 	/* nope, better SIGFPE the offending process... */
285 
286 #ifdef CONFIG_SMP
287 	clear_tsk_thread_flag(fpt, TIF_USEDFPU);
288 #endif
289 	if(psr & PSR_PS) {
290 		/* The first fsr store/load we tried trapped,
291 		 * the second one will not (we hope).
292 		 */
293 		printk("WARNING: FPU exception from kernel mode. at pc=%08lx\n",
294 		       regs->pc);
295 		regs->pc = regs->npc;
296 		regs->npc += 4;
297 		calls++;
298 		if(calls > 2)
299 			die_if_kernel("Too many Penguin-FPU traps from kernel mode",
300 				      regs);
301 		return;
302 	}
303 
304 	fsr = fpt->thread.fsr;
305 	info.si_signo = SIGFPE;
306 	info.si_errno = 0;
307 	info.si_addr = (void __user *)pc;
308 	info.si_trapno = 0;
309 	info.si_code = __SI_FAULT;
310 	if ((fsr & 0x1c000) == (1 << 14)) {
311 		if (fsr & 0x10)
312 			info.si_code = FPE_FLTINV;
313 		else if (fsr & 0x08)
314 			info.si_code = FPE_FLTOVF;
315 		else if (fsr & 0x04)
316 			info.si_code = FPE_FLTUND;
317 		else if (fsr & 0x02)
318 			info.si_code = FPE_FLTDIV;
319 		else if (fsr & 0x01)
320 			info.si_code = FPE_FLTRES;
321 	}
322 	send_sig_info(SIGFPE, &info, fpt);
323 #ifndef CONFIG_SMP
324 	last_task_used_math = NULL;
325 #endif
326 	regs->psr &= ~PSR_EF;
327 	if(calls > 0)
328 		calls=0;
329 }
330 
331 void handle_tag_overflow(struct pt_regs *regs, unsigned long pc, unsigned long npc,
332 			 unsigned long psr)
333 {
334 	siginfo_t info;
335 
336 	if(psr & PSR_PS)
337 		die_if_kernel("Penguin overflow trap from kernel mode", regs);
338 	info.si_signo = SIGEMT;
339 	info.si_errno = 0;
340 	info.si_code = EMT_TAGOVF;
341 	info.si_addr = (void __user *)pc;
342 	info.si_trapno = 0;
343 	send_sig_info(SIGEMT, &info, current);
344 }
345 
346 void handle_watchpoint(struct pt_regs *regs, unsigned long pc, unsigned long npc,
347 		       unsigned long psr)
348 {
349 #ifdef TRAP_DEBUG
350 	printk("Watchpoint detected at PC %08lx NPC %08lx PSR %08lx\n",
351 	       pc, npc, psr);
352 #endif
353 	if(psr & PSR_PS)
354 		panic("Tell me what a watchpoint trap is, and I'll then deal "
355 		      "with such a beast...");
356 }
357 
358 void handle_reg_access(struct pt_regs *regs, unsigned long pc, unsigned long npc,
359 		       unsigned long psr)
360 {
361 	siginfo_t info;
362 
363 #ifdef TRAP_DEBUG
364 	printk("Register Access Exception at PC %08lx NPC %08lx PSR %08lx\n",
365 	       pc, npc, psr);
366 #endif
367 	info.si_signo = SIGBUS;
368 	info.si_errno = 0;
369 	info.si_code = BUS_OBJERR;
370 	info.si_addr = (void __user *)pc;
371 	info.si_trapno = 0;
372 	force_sig_info(SIGBUS, &info, current);
373 }
374 
375 void handle_cp_disabled(struct pt_regs *regs, unsigned long pc, unsigned long npc,
376 			unsigned long psr)
377 {
378 	siginfo_t info;
379 
380 	info.si_signo = SIGILL;
381 	info.si_errno = 0;
382 	info.si_code = ILL_COPROC;
383 	info.si_addr = (void __user *)pc;
384 	info.si_trapno = 0;
385 	send_sig_info(SIGILL, &info, current);
386 }
387 
388 void handle_cp_exception(struct pt_regs *regs, unsigned long pc, unsigned long npc,
389 			 unsigned long psr)
390 {
391 	siginfo_t info;
392 
393 #ifdef TRAP_DEBUG
394 	printk("Co-Processor Exception at PC %08lx NPC %08lx PSR %08lx\n",
395 	       pc, npc, psr);
396 #endif
397 	info.si_signo = SIGILL;
398 	info.si_errno = 0;
399 	info.si_code = ILL_COPROC;
400 	info.si_addr = (void __user *)pc;
401 	info.si_trapno = 0;
402 	send_sig_info(SIGILL, &info, current);
403 }
404 
405 void handle_hw_divzero(struct pt_regs *regs, unsigned long pc, unsigned long npc,
406 		       unsigned long psr)
407 {
408 	siginfo_t info;
409 
410 	info.si_signo = SIGFPE;
411 	info.si_errno = 0;
412 	info.si_code = FPE_INTDIV;
413 	info.si_addr = (void __user *)pc;
414 	info.si_trapno = 0;
415 	send_sig_info(SIGFPE, &info, current);
416 }
417 
418 #ifdef CONFIG_DEBUG_BUGVERBOSE
419 void do_BUG(const char *file, int line)
420 {
421         // bust_spinlocks(1);   XXX Not in our original BUG()
422         printk("kernel BUG at %s:%d!\n", file, line);
423 }
424 EXPORT_SYMBOL(do_BUG);
425 #endif
426 
427 /* Since we have our mappings set up, on multiprocessors we can spin them
428  * up here so that timer interrupts work during initialization.
429  */
430 
431 void trap_init(void)
432 {
433 	extern void thread_info_offsets_are_bolixed_pete(void);
434 
435 	/* Force linker to barf if mismatched */
436 	if (TI_UWINMASK    != offsetof(struct thread_info, uwinmask) ||
437 	    TI_TASK        != offsetof(struct thread_info, task) ||
438 	    TI_FLAGS       != offsetof(struct thread_info, flags) ||
439 	    TI_CPU         != offsetof(struct thread_info, cpu) ||
440 	    TI_PREEMPT     != offsetof(struct thread_info, preempt_count) ||
441 	    TI_SOFTIRQ     != offsetof(struct thread_info, softirq_count) ||
442 	    TI_HARDIRQ     != offsetof(struct thread_info, hardirq_count) ||
443 	    TI_KSP         != offsetof(struct thread_info, ksp) ||
444 	    TI_KPC         != offsetof(struct thread_info, kpc) ||
445 	    TI_KPSR        != offsetof(struct thread_info, kpsr) ||
446 	    TI_KWIM        != offsetof(struct thread_info, kwim) ||
447 	    TI_REG_WINDOW  != offsetof(struct thread_info, reg_window) ||
448 	    TI_RWIN_SPTRS  != offsetof(struct thread_info, rwbuf_stkptrs) ||
449 	    TI_W_SAVED     != offsetof(struct thread_info, w_saved))
450 		thread_info_offsets_are_bolixed_pete();
451 
452 	/* Attach to the address space of init_task. */
453 	mmgrab(&init_mm);
454 	current->active_mm = &init_mm;
455 
456 	/* NOTE: Other cpus have this done as they are started
457 	 *       up on SMP.
458 	 */
459 }
460