1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 /*
28 * Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T
29 * All Rights Reserved
30 *
31 * Portions of this source code were derived from Berkeley
32 * 4.3 BSD under license from the regents of the University of
33 * California.
34 */
35
36 /* Swap handler for SIGFPE codes. */
37
38 #include "lint.h"
39 #include <mtlib.h>
40 #include <errno.h>
41 #include <signal.h>
42 #include <floatingpoint.h>
43 #include <sys/types.h>
44 #include <sys/ucontext.h>
45 #include <sys/siginfo.h>
46 #include <thread.h>
47 #include <synch.h>
48 #include <stdlib.h>
49
50 #ifndef FPE_INTDIV
51 #define FPE_INTDIV 1 /* integer divide by zero */
52 #endif
53 #ifndef FPE_INTOVF
54 #define FPE_INTOVF 2 /* integer overflow */
55 #endif
56 #ifndef FPE_FLTDIV
57 #define FPE_FLTDIV 3 /* [floating divide by zero] */
58 #endif
59 #ifndef FPE_FLTOVF
60 #define FPE_FLTOVF 4 /* [floating overflow] */
61 #endif
62 #ifndef FPE_FLTUND
63 #define FPE_FLTUND 5 /* [floating underflow] */
64 #endif
65 #ifndef FPE_FLTRES
66 #define FPE_FLTRES 6 /* [floating inexact result] */
67 #endif
68 #ifndef FPE_FLTINV
69 #define FPE_FLTINV 7 /* [floating invalid operation] */
70 #endif
71
72 #if defined(__i386) || defined(__amd64)
73
74 #ifndef FPE_FLTSUB
75 #define FPE_FLTSUB 8 /* subscript out of range */
76 #endif
77 #ifndef FPE_FLTDEN
78 #define FPE_FLTDEN 9 /* x86-specific: denormal operand */
79 #endif
80
81 #define N_SIGFPE_CODE 10
82
83 #else
84
85 #define N_SIGFPE_CODE 8
86
87 #endif /* __i386 */
88
89 /* Array of SIGFPE codes. */
90
91 static const sigfpe_code_type sigfpe_codes[N_SIGFPE_CODE] = {
92 FPE_INTDIV,
93 FPE_INTOVF,
94 FPE_FLTDIV,
95 FPE_FLTOVF,
96 FPE_FLTUND,
97 FPE_FLTRES,
98 FPE_FLTINV,
99 #if defined(__i386) || defined(__amd64)
100 FPE_FLTSUB,
101 FPE_FLTDEN,
102 #endif
103 0
104 };
105
106 /* Array of handlers. */
107
108 static mutex_t sigfpe_lock = DEFAULTMUTEX;
109
110 sigfpe_handler_type ieee_handlers[N_IEEE_EXCEPTION];
111 static sigfpe_handler_type sigfpe_handlers[N_SIGFPE_CODE];
112
113 static int _sigfpe_master_enabled;
114 /* Originally zero, set to 1 by _enable_sigfpe_master. */
115
116 #ifndef BADSIG
117 #define BADSIG (void (*)(void))-1
118 #endif
119
120 static void
_sigfpe_master(int sig,siginfo_t * siginfo,void * arg)121 _sigfpe_master(int sig, siginfo_t *siginfo, void *arg)
122 {
123 ucontext_t *ucontext = arg;
124 int i;
125 int code;
126 enum fp_exception_type exception;
127
128 lmutex_lock(&sigfpe_lock);
129 code = siginfo->si_code;
130 for (i = 0; (i < N_SIGFPE_CODE) && (code != sigfpe_codes[i]); i++)
131 continue;
132 /* Find index of handler. */
133 if (i >= N_SIGFPE_CODE)
134 i = N_SIGFPE_CODE - 1;
135 switch ((intptr_t)sigfpe_handlers[i]) {
136 case ((intptr_t)(SIGFPE_DEFAULT)):
137 switch (code) {
138 case FPE_FLTINV:
139 exception = fp_invalid;
140 goto ieee;
141 case FPE_FLTRES:
142 exception = fp_inexact;
143 goto ieee;
144 case FPE_FLTDIV:
145 exception = fp_division;
146 goto ieee;
147 case FPE_FLTUND:
148 exception = fp_underflow;
149 goto ieee;
150 case FPE_FLTOVF:
151 exception = fp_overflow;
152 goto ieee;
153 #if defined(__i386) || defined(__amd64)
154 case FPE_FLTDEN:
155 exception = fp_denormalized;
156 goto ieee;
157 #endif
158 default: /* The common default treatment is to abort. */
159 break;
160 }
161 /* FALLTHROUGH */
162 case ((intptr_t)(SIGFPE_ABORT)):
163 abort();
164 break;
165 case ((intptr_t)(SIGFPE_IGNORE)):
166 lmutex_unlock(&sigfpe_lock);
167 return;
168 default: /* User-defined not SIGFPE_DEFAULT or SIGFPE_ABORT. */
169 (sigfpe_handlers[i])(sig, siginfo, ucontext);
170 lmutex_unlock(&sigfpe_lock);
171 return;
172 }
173 ieee:
174 switch ((intptr_t)ieee_handlers[(int)exception]) {
175 case ((intptr_t)(SIGFPE_DEFAULT)): /* Error condition but ignore it. */
176 case ((intptr_t)(SIGFPE_IGNORE)): /* Error condition but ignore it. */
177 lmutex_unlock(&sigfpe_lock);
178 return;
179 case ((intptr_t)(SIGFPE_ABORT)):
180 abort();
181 default:
182 (ieee_handlers[(int)exception])(sig, siginfo, ucontext);
183 lmutex_unlock(&sigfpe_lock);
184 return;
185 }
186 }
187
188 static int
_enable_sigfpe_master(void)189 _enable_sigfpe_master(void)
190 {
191 /* Enable the sigfpe master handler always. */
192 struct sigaction newsigact, oldsigact;
193
194 newsigact.sa_sigaction = _sigfpe_master;
195 (void) sigemptyset(&newsigact.sa_mask);
196 newsigact.sa_flags = SA_SIGINFO; /* enhanced handler */
197 _sigfpe_master_enabled = 1;
198 return (sigaction(SIGFPE, &newsigact, &oldsigact));
199 }
200
201 static int
_test_sigfpe_master(void)202 _test_sigfpe_master(void)
203 {
204 /*
205 * Enable the sigfpe master handler if it's never been enabled
206 * before.
207 */
208
209 if (_sigfpe_master_enabled == 0)
210 return (_enable_sigfpe_master());
211 else
212 return (_sigfpe_master_enabled);
213 }
214
215 sigfpe_handler_type
sigfpe(sigfpe_code_type code,sigfpe_handler_type hdl)216 sigfpe(sigfpe_code_type code, sigfpe_handler_type hdl)
217 {
218 sigfpe_handler_type oldhdl;
219 int i;
220
221 lmutex_lock(&sigfpe_lock);
222 (void) _test_sigfpe_master();
223 for (i = 0; (i < N_SIGFPE_CODE) && (code != sigfpe_codes[i]); i++)
224 continue;
225 /* Find index of handler. */
226 if (i >= N_SIGFPE_CODE) {
227 errno = EINVAL;
228 lmutex_unlock(&sigfpe_lock);
229 /* Not 0 or SIGFPE code */
230 return ((sigfpe_handler_type)BADSIG);
231 }
232 oldhdl = sigfpe_handlers[i];
233 sigfpe_handlers[i] = hdl;
234 lmutex_unlock(&sigfpe_lock);
235 return (oldhdl);
236 }
237