Linux Audio

Check our new training course

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