xref: /netbsd/sys/arch/mips/mips/mips_fputrap.c (revision 6550d01e)
1 /* $NetBSD: mips_fputrap.c,v 1.6 2009/12/14 00:46:06 matt Exp $ */
2 
3 /*
4  * Copyright (c) 2004
5  *	Matthias Drochner. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions, and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/proc.h>
32 #include <sys/signal.h>
33 #include <sys/siginfo.h>
34 #include <mips/cpuregs.h>
35 #include <mips/regnum.h>
36 
37 #ifndef SOFTFLOAT
38 void mips_fpuexcept(struct lwp *, unsigned int);
39 void mips_fpuillinst(struct lwp *, unsigned int, unsigned long);
40 static int fpustat2sicode(unsigned int);
41 
42 void
43 mips_fpuexcept(struct lwp *l, unsigned int fpustat)
44 {
45 	ksiginfo_t ksi;
46 
47 	KSI_INIT_TRAP(&ksi);
48 	ksi.ksi_signo = SIGFPE;
49 	ksi.ksi_code = fpustat2sicode(fpustat);
50 	ksi.ksi_trap = fpustat;
51 	(*l->l_proc->p_emul->e_trapsignal)(l, &ksi);
52 }
53 
54 void
55 mips_fpuillinst(struct lwp *l, unsigned int opcode, unsigned long vaddr)
56 {
57 	ksiginfo_t ksi;
58 
59 	KSI_INIT_TRAP(&ksi);
60 	ksi.ksi_signo = SIGILL;
61 	ksi.ksi_code = ILL_ILLOPC;
62 	ksi.ksi_trap = opcode;
63 	ksi.ksi_addr = (void *)vaddr;
64 	(*l->l_proc->p_emul->e_trapsignal)(l, &ksi);
65 }
66 
67 static struct {
68 	unsigned int bit;
69 	int code;
70 } fpecodes[] = {
71 	{ MIPS_FPU_EXCEPTION_UNDERFLOW, FPE_FLTUND },
72 	{ MIPS_FPU_EXCEPTION_OVERFLOW, FPE_FLTOVF },
73 	{ MIPS_FPU_EXCEPTION_INEXACT, FPE_FLTRES },
74 	{ MIPS_FPU_EXCEPTION_DIV0, FPE_FLTDIV },
75 	{ MIPS_FPU_EXCEPTION_INVALID, FPE_FLTINV },
76 	{ MIPS_FPU_EXCEPTION_UNIMPL, FPE_FLTINV }
77 };
78 
79 static int
80 fpustat2sicode(unsigned int fpustat)
81 {
82 	int i;
83 
84 	for (i = 0; i < 6; i++)
85 		if (fpustat & fpecodes[i].bit)
86 			return (fpecodes[i].code);
87 	return (FPE_FLTINV);
88 }
89 #endif /* !SOFTFLOAT */
90 
91 void fpemul_trapsignal(struct lwp *, unsigned int, unsigned int);
92 
93 void
94 fpemul_trapsignal(struct lwp *l, unsigned int sig, unsigned int code)
95 {
96 	ksiginfo_t ksi;
97 
98 #if DEBUG
99 	printf("fpemul_trapsignal(%x,%x,%#"PRIxREGISTER")\n",
100 	   sig, code, l->l_md.md_regs->f_regs[_R_PC]);
101 #endif
102 
103 	KSI_INIT_TRAP(&ksi);
104 	ksi.ksi_signo = sig;
105 	ksi.ksi_code = 1; /* XXX */
106 	ksi.ksi_trap = code;
107 	(*l->l_proc->p_emul->e_trapsignal)(l, &ksi);
108 }
109