xref: /titanic_44/usr/src/lib/libc/port/fp/sigfpe.c (revision ee0eb9f2d12b8a03b5a193cd667735c1962e0c7b)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
37 
38 /* Swap handler for SIGFPE codes.	 */
39 
40 #include "lint.h"
41 #include <mtlib.h>
42 #include <errno.h>
43 #include <signal.h>
44 #include <floatingpoint.h>
45 #include <sys/types.h>
46 #include <sys/ucontext.h>
47 #include <sys/siginfo.h>
48 #include <thread.h>
49 #include <synch.h>
50 #include <stdlib.h>
51 
52 #ifndef FPE_INTDIV
53 #define	FPE_INTDIV	1 /* integer divide by zero */
54 #endif
55 #ifndef FPE_INTOVF
56 #define	FPE_INTOVF	2 /* integer overflow */
57 #endif
58 #ifndef FPE_FLTDIV
59 #define	FPE_FLTDIV	3 /* [floating divide by zero] */
60 #endif
61 #ifndef FPE_FLTOVF
62 #define	FPE_FLTOVF	4 /* [floating overflow] */
63 #endif
64 #ifndef FPE_FLTUND
65 #define	FPE_FLTUND	5 /* [floating underflow] */
66 #endif
67 #ifndef FPE_FLTRES
68 #define	FPE_FLTRES	6 /* [floating inexact result] */
69 #endif
70 #ifndef FPE_FLTINV
71 #define	FPE_FLTINV	7 /* [floating invalid operation] */
72 #endif
73 
74 #if defined(__i386) || defined(__amd64)
75 
76 #ifndef	FPE_FLTSUB
77 #define	FPE_FLTSUB	8 /* subscript out of range */
78 #endif
79 #ifndef	FPE_FLTDEN
80 #define	FPE_FLTDEN	9 /* x86-specific: denormal operand */
81 #endif
82 
83 #define	N_SIGFPE_CODE	10
84 
85 #else
86 
87 #define	N_SIGFPE_CODE	8
88 
89 #endif /* __i386 */
90 
91 /* Array of SIGFPE codes. */
92 
93 static const sigfpe_code_type sigfpe_codes[N_SIGFPE_CODE] = {
94 	FPE_INTDIV,
95 	FPE_INTOVF,
96 	FPE_FLTDIV,
97 	FPE_FLTOVF,
98 	FPE_FLTUND,
99 	FPE_FLTRES,
100 	FPE_FLTINV,
101 #if defined(__i386) || defined(__amd64)
102 	FPE_FLTSUB,
103 	FPE_FLTDEN,
104 #endif
105 	0
106 };
107 
108 /* Array of handlers. */
109 
110 static mutex_t sigfpe_lock = DEFAULTMUTEX;
111 
112 sigfpe_handler_type ieee_handlers[N_IEEE_EXCEPTION];
113 static sigfpe_handler_type sigfpe_handlers[N_SIGFPE_CODE];
114 
115 static	int	_sigfpe_master_enabled;
116 /* Originally zero, set to 1 by _enable_sigfpe_master. */
117 
118 #ifndef BADSIG
119 #define	BADSIG		(void (*)(void))-1
120 #endif
121 
122 static void
123 _sigfpe_master(int sig, siginfo_t *siginfo, void *arg)
124 {
125 	ucontext_t	*ucontext = arg;
126 	int		i;
127 	int		code;
128 	enum fp_exception_type exception;
129 
130 	lmutex_lock(&sigfpe_lock);
131 	code = siginfo->si_code;
132 	for (i = 0; (i < N_SIGFPE_CODE) && (code != sigfpe_codes[i]); i++)
133 		continue;
134 	/* Find index of handler. */
135 	if (i >= N_SIGFPE_CODE)
136 		i = N_SIGFPE_CODE - 1;
137 	switch ((intptr_t)sigfpe_handlers[i]) {
138 	case ((intptr_t)(SIGFPE_DEFAULT)):
139 		switch (code) {
140 		case FPE_FLTINV:
141 			exception = fp_invalid;
142 			goto ieee;
143 		case FPE_FLTRES:
144 			exception = fp_inexact;
145 			goto ieee;
146 		case FPE_FLTDIV:
147 			exception = fp_division;
148 			goto ieee;
149 		case FPE_FLTUND:
150 			exception = fp_underflow;
151 			goto ieee;
152 		case FPE_FLTOVF:
153 			exception = fp_overflow;
154 			goto ieee;
155 		default:	/* The common default treatment is to abort. */
156 			break;
157 		}
158 	case ((intptr_t)(SIGFPE_ABORT)):
159 		abort();
160 		break;
161 	case ((intptr_t)(SIGFPE_IGNORE)):
162 		lmutex_unlock(&sigfpe_lock);
163 		return;
164 	default:	/* User-defined not SIGFPE_DEFAULT or SIGFPE_ABORT. */
165 		(sigfpe_handlers[i])(sig, siginfo, ucontext);
166 		lmutex_unlock(&sigfpe_lock);
167 		return;
168 	}
169 ieee:
170 	switch ((intptr_t)ieee_handlers[(int)exception]) {
171 	case ((intptr_t)(SIGFPE_DEFAULT)): /* Error condition but ignore it. */
172 	case ((intptr_t)(SIGFPE_IGNORE)): /* Error condition but ignore it. */
173 		lmutex_unlock(&sigfpe_lock);
174 		return;
175 	case ((intptr_t)(SIGFPE_ABORT)):
176 		abort();
177 	default:
178 		(ieee_handlers[(int)exception])(sig, siginfo, ucontext);
179 		lmutex_unlock(&sigfpe_lock);
180 		return;
181 	}
182 }
183 
184 static int
185 _enable_sigfpe_master(void)
186 {
187 	/* Enable the sigfpe master handler always. */
188 	struct sigaction newsigact, oldsigact;
189 
190 	newsigact.sa_sigaction = _sigfpe_master;
191 	(void) sigemptyset(&newsigact.sa_mask);
192 	newsigact.sa_flags = SA_SIGINFO;	/* enhanced handler */
193 	_sigfpe_master_enabled = 1;
194 	return (sigaction(SIGFPE, &newsigact, &oldsigact));
195 }
196 
197 static int
198 _test_sigfpe_master(void)
199 {
200 	/*
201 	 * Enable the sigfpe master handler if it's never been enabled
202 	 * before.
203 	 */
204 
205 	if (_sigfpe_master_enabled == 0)
206 		return (_enable_sigfpe_master());
207 	else
208 		return (_sigfpe_master_enabled);
209 }
210 
211 sigfpe_handler_type
212 sigfpe(sigfpe_code_type code, sigfpe_handler_type hdl)
213 {
214 	sigfpe_handler_type oldhdl;
215 	int		i;
216 
217 	lmutex_lock(&sigfpe_lock);
218 	(void) _test_sigfpe_master();
219 	for (i = 0; (i < N_SIGFPE_CODE) && (code != sigfpe_codes[i]); i++)
220 		continue;
221 	/* Find index of handler. */
222 	if (i >= N_SIGFPE_CODE) {
223 		errno = EINVAL;
224 		lmutex_unlock(&sigfpe_lock);
225 		/* Not 0 or SIGFPE code */
226 		return ((sigfpe_handler_type)BADSIG);
227 	}
228 	oldhdl = sigfpe_handlers[i];
229 	sigfpe_handlers[i] = hdl;
230 	lmutex_unlock(&sigfpe_lock);
231 	return (oldhdl);
232 }
233