1 /* $NetBSD: mips_fputrap.c,v 1.12 2021/05/29 12:35:27 simonb 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
35 #include <mips/cpuregs.h>
36 #include <mips/regnum.h>
37 #include <mips/locore.h>
38
39 #if defined(FPEMUL) || !defined(NOFPU)
40 void mips_fpuexcept(struct lwp *, uint32_t);
41 void mips_fpuillinst(struct lwp *, uint32_t);
42 static int fpustat2sicode(uint32_t);
43
44 void
mips_fpuexcept(struct lwp * l,uint32_t fpustat)45 mips_fpuexcept(struct lwp *l, uint32_t fpustat)
46 {
47 ksiginfo_t ksi;
48
49 #ifdef FPEMUL_DEBUG
50 printf("%s(%#x,%#"PRIxREGISTER")\n",
51 __func__, fpustat, l->l_md.md_utf->tf_regs[_R_PC]);
52 #endif
53
54 KSI_INIT_TRAP(&ksi);
55 ksi.ksi_signo = SIGFPE;
56 ksi.ksi_code = fpustat2sicode(fpustat);
57 ksi.ksi_trap = fpustat;
58 (*l->l_proc->p_emul->e_trapsignal)(l, &ksi);
59 }
60
61 void
mips_fpuillinst(struct lwp * l,uint32_t opcode)62 mips_fpuillinst(struct lwp *l, uint32_t opcode)
63 {
64 ksiginfo_t ksi;
65
66 #ifdef FPEMUL_DEBUG
67 printf("%s(%#x,%#"PRIxREGISTER")\n",
68 __func__, opcode, l->l_md.md_utf->tf_regs[_R_PC]);
69 #endif
70
71 KSI_INIT_TRAP(&ksi);
72 ksi.ksi_signo = SIGILL;
73 ksi.ksi_code = ILL_ILLOPC;
74 ksi.ksi_trap = opcode;
75 ksi.ksi_addr = (void *)(uintptr_t)l->l_md.md_utf->tf_regs[_R_PC];
76 (*l->l_proc->p_emul->e_trapsignal)(l, &ksi);
77 }
78
79 static const struct {
80 unsigned int bit;
81 int code;
82 } fpecodes[] = {
83 { MIPS_FCSR_CAUSE_I, FPE_FLTRES },
84 { MIPS_FCSR_CAUSE_U, FPE_FLTUND },
85 { MIPS_FCSR_CAUSE_O, FPE_FLTOVF },
86 { MIPS_FCSR_CAUSE_Z, FPE_FLTDIV },
87 { MIPS_FCSR_CAUSE_V, FPE_FLTINV },
88 { MIPS_FCSR_CAUSE_E, FPE_FLTINV }
89 };
90
91 static int
fpustat2sicode(uint32_t fpustat)92 fpustat2sicode(uint32_t fpustat)
93 {
94 for (size_t i = 0; i < __arraycount(fpecodes); i++) {
95 if (fpustat & fpecodes[i].bit)
96 return fpecodes[i].code;
97 }
98
99 return FPE_FLTINV;
100 }
101 #endif /* FPEMUL || !NOFPU */
102