xref: /illumos-gate/usr/src/lib/libc/port/fp/sigfpe.c (revision 03270635d68df6a0392fb8f4b7c04acad764648b)
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
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 		default:	/* The common default treatment is to abort. */
154 			break;
155 		}
156 		/* FALLTHROUGH */
157 	case ((intptr_t)(SIGFPE_ABORT)):
158 		abort();
159 		break;
160 	case ((intptr_t)(SIGFPE_IGNORE)):
161 		lmutex_unlock(&sigfpe_lock);
162 		return;
163 	default:	/* User-defined not SIGFPE_DEFAULT or SIGFPE_ABORT. */
164 		(sigfpe_handlers[i])(sig, siginfo, ucontext);
165 		lmutex_unlock(&sigfpe_lock);
166 		return;
167 	}
168 ieee:
169 	switch ((intptr_t)ieee_handlers[(int)exception]) {
170 	case ((intptr_t)(SIGFPE_DEFAULT)): /* Error condition but ignore it. */
171 	case ((intptr_t)(SIGFPE_IGNORE)): /* Error condition but ignore it. */
172 		lmutex_unlock(&sigfpe_lock);
173 		return;
174 	case ((intptr_t)(SIGFPE_ABORT)):
175 		abort();
176 	default:
177 		(ieee_handlers[(int)exception])(sig, siginfo, ucontext);
178 		lmutex_unlock(&sigfpe_lock);
179 		return;
180 	}
181 }
182 
183 static int
184 _enable_sigfpe_master(void)
185 {
186 	/* Enable the sigfpe master handler always. */
187 	struct sigaction newsigact, oldsigact;
188 
189 	newsigact.sa_sigaction = _sigfpe_master;
190 	(void) sigemptyset(&newsigact.sa_mask);
191 	newsigact.sa_flags = SA_SIGINFO;	/* enhanced handler */
192 	_sigfpe_master_enabled = 1;
193 	return (sigaction(SIGFPE, &newsigact, &oldsigact));
194 }
195 
196 static int
197 _test_sigfpe_master(void)
198 {
199 	/*
200 	 * Enable the sigfpe master handler if it's never been enabled
201 	 * before.
202 	 */
203 
204 	if (_sigfpe_master_enabled == 0)
205 		return (_enable_sigfpe_master());
206 	else
207 		return (_sigfpe_master_enabled);
208 }
209 
210 sigfpe_handler_type
211 sigfpe(sigfpe_code_type code, sigfpe_handler_type hdl)
212 {
213 	sigfpe_handler_type oldhdl;
214 	int		i;
215 
216 	lmutex_lock(&sigfpe_lock);
217 	(void) _test_sigfpe_master();
218 	for (i = 0; (i < N_SIGFPE_CODE) && (code != sigfpe_codes[i]); i++)
219 		continue;
220 	/* Find index of handler. */
221 	if (i >= N_SIGFPE_CODE) {
222 		errno = EINVAL;
223 		lmutex_unlock(&sigfpe_lock);
224 		/* Not 0 or SIGFPE code */
225 		return ((sigfpe_handler_type)BADSIG);
226 	}
227 	oldhdl = sigfpe_handlers[i];
228 	sigfpe_handlers[i] = hdl;
229 	lmutex_unlock(&sigfpe_lock);
230 	return (oldhdl);
231 }
232