10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*6812Sraf * Common Development and Distribution License (the "License").
6*6812Sraf * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
20*6812Sraf */
21*6812Sraf
22*6812Sraf /*
23*6812Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
25*6812Sraf */
26*6812Sraf
27*6812Sraf /*
280Sstevel@tonic-gate * Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T
290Sstevel@tonic-gate * All Rights Reserved
300Sstevel@tonic-gate *
310Sstevel@tonic-gate * Portions of this source code were derived from Berkeley
320Sstevel@tonic-gate * 4.3 BSD under license from the regents of the University of
330Sstevel@tonic-gate * California.
340Sstevel@tonic-gate */
350Sstevel@tonic-gate
360Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
370Sstevel@tonic-gate
380Sstevel@tonic-gate /* Swap handler for SIGFPE codes. */
390Sstevel@tonic-gate
40*6812Sraf #include "lint.h"
410Sstevel@tonic-gate #include <mtlib.h>
420Sstevel@tonic-gate #include <errno.h>
430Sstevel@tonic-gate #include <signal.h>
440Sstevel@tonic-gate #include <floatingpoint.h>
450Sstevel@tonic-gate #include <sys/types.h>
460Sstevel@tonic-gate #include <sys/ucontext.h>
470Sstevel@tonic-gate #include <sys/siginfo.h>
480Sstevel@tonic-gate #include <thread.h>
490Sstevel@tonic-gate #include <synch.h>
500Sstevel@tonic-gate #include <stdlib.h>
510Sstevel@tonic-gate
520Sstevel@tonic-gate #ifndef FPE_INTDIV
530Sstevel@tonic-gate #define FPE_INTDIV 1 /* integer divide by zero */
540Sstevel@tonic-gate #endif
550Sstevel@tonic-gate #ifndef FPE_INTOVF
560Sstevel@tonic-gate #define FPE_INTOVF 2 /* integer overflow */
570Sstevel@tonic-gate #endif
580Sstevel@tonic-gate #ifndef FPE_FLTDIV
590Sstevel@tonic-gate #define FPE_FLTDIV 3 /* [floating divide by zero] */
600Sstevel@tonic-gate #endif
610Sstevel@tonic-gate #ifndef FPE_FLTOVF
620Sstevel@tonic-gate #define FPE_FLTOVF 4 /* [floating overflow] */
630Sstevel@tonic-gate #endif
640Sstevel@tonic-gate #ifndef FPE_FLTUND
650Sstevel@tonic-gate #define FPE_FLTUND 5 /* [floating underflow] */
660Sstevel@tonic-gate #endif
670Sstevel@tonic-gate #ifndef FPE_FLTRES
680Sstevel@tonic-gate #define FPE_FLTRES 6 /* [floating inexact result] */
690Sstevel@tonic-gate #endif
700Sstevel@tonic-gate #ifndef FPE_FLTINV
710Sstevel@tonic-gate #define FPE_FLTINV 7 /* [floating invalid operation] */
720Sstevel@tonic-gate #endif
730Sstevel@tonic-gate
740Sstevel@tonic-gate #if defined(__i386) || defined(__amd64)
750Sstevel@tonic-gate
760Sstevel@tonic-gate #ifndef FPE_FLTSUB
770Sstevel@tonic-gate #define FPE_FLTSUB 8 /* subscript out of range */
780Sstevel@tonic-gate #endif
790Sstevel@tonic-gate #ifndef FPE_FLTDEN
800Sstevel@tonic-gate #define FPE_FLTDEN 9 /* x86-specific: denormal operand */
810Sstevel@tonic-gate #endif
820Sstevel@tonic-gate
830Sstevel@tonic-gate #define N_SIGFPE_CODE 10
840Sstevel@tonic-gate
850Sstevel@tonic-gate #else
860Sstevel@tonic-gate
870Sstevel@tonic-gate #define N_SIGFPE_CODE 8
880Sstevel@tonic-gate
890Sstevel@tonic-gate #endif /* __i386 */
900Sstevel@tonic-gate
910Sstevel@tonic-gate /* Array of SIGFPE codes. */
920Sstevel@tonic-gate
930Sstevel@tonic-gate static const sigfpe_code_type sigfpe_codes[N_SIGFPE_CODE] = {
940Sstevel@tonic-gate FPE_INTDIV,
950Sstevel@tonic-gate FPE_INTOVF,
960Sstevel@tonic-gate FPE_FLTDIV,
970Sstevel@tonic-gate FPE_FLTOVF,
980Sstevel@tonic-gate FPE_FLTUND,
990Sstevel@tonic-gate FPE_FLTRES,
1000Sstevel@tonic-gate FPE_FLTINV,
1010Sstevel@tonic-gate #if defined(__i386) || defined(__amd64)
1020Sstevel@tonic-gate FPE_FLTSUB,
1030Sstevel@tonic-gate FPE_FLTDEN,
1040Sstevel@tonic-gate #endif
1050Sstevel@tonic-gate 0
1060Sstevel@tonic-gate };
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate /* Array of handlers. */
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate static mutex_t sigfpe_lock = DEFAULTMUTEX;
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate sigfpe_handler_type ieee_handlers[N_IEEE_EXCEPTION];
1130Sstevel@tonic-gate static sigfpe_handler_type sigfpe_handlers[N_SIGFPE_CODE];
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate static int _sigfpe_master_enabled;
1160Sstevel@tonic-gate /* Originally zero, set to 1 by _enable_sigfpe_master. */
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate #ifndef BADSIG
1190Sstevel@tonic-gate #define BADSIG (void (*)(void))-1
1200Sstevel@tonic-gate #endif
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate static void
_sigfpe_master(int sig,siginfo_t * siginfo,void * arg)1230Sstevel@tonic-gate _sigfpe_master(int sig, siginfo_t *siginfo, void *arg)
1240Sstevel@tonic-gate {
1250Sstevel@tonic-gate ucontext_t *ucontext = arg;
1260Sstevel@tonic-gate int i;
1270Sstevel@tonic-gate int code;
1280Sstevel@tonic-gate enum fp_exception_type exception;
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate lmutex_lock(&sigfpe_lock);
1310Sstevel@tonic-gate code = siginfo->si_code;
132*6812Sraf for (i = 0; (i < N_SIGFPE_CODE) && (code != sigfpe_codes[i]); i++)
133*6812Sraf continue;
1340Sstevel@tonic-gate /* Find index of handler. */
1350Sstevel@tonic-gate if (i >= N_SIGFPE_CODE)
1360Sstevel@tonic-gate i = N_SIGFPE_CODE - 1;
1370Sstevel@tonic-gate switch ((intptr_t)sigfpe_handlers[i]) {
1380Sstevel@tonic-gate case ((intptr_t)(SIGFPE_DEFAULT)):
1390Sstevel@tonic-gate switch (code) {
1400Sstevel@tonic-gate case FPE_FLTINV:
1410Sstevel@tonic-gate exception = fp_invalid;
1420Sstevel@tonic-gate goto ieee;
1430Sstevel@tonic-gate case FPE_FLTRES:
1440Sstevel@tonic-gate exception = fp_inexact;
1450Sstevel@tonic-gate goto ieee;
1460Sstevel@tonic-gate case FPE_FLTDIV:
1470Sstevel@tonic-gate exception = fp_division;
1480Sstevel@tonic-gate goto ieee;
1490Sstevel@tonic-gate case FPE_FLTUND:
1500Sstevel@tonic-gate exception = fp_underflow;
1510Sstevel@tonic-gate goto ieee;
1520Sstevel@tonic-gate case FPE_FLTOVF:
1530Sstevel@tonic-gate exception = fp_overflow;
1540Sstevel@tonic-gate goto ieee;
1550Sstevel@tonic-gate default: /* The common default treatment is to abort. */
1560Sstevel@tonic-gate break;
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate case ((intptr_t)(SIGFPE_ABORT)):
1590Sstevel@tonic-gate abort();
1600Sstevel@tonic-gate break;
1610Sstevel@tonic-gate case ((intptr_t)(SIGFPE_IGNORE)):
1620Sstevel@tonic-gate lmutex_unlock(&sigfpe_lock);
1630Sstevel@tonic-gate return;
1640Sstevel@tonic-gate default: /* User-defined not SIGFPE_DEFAULT or SIGFPE_ABORT. */
1650Sstevel@tonic-gate (sigfpe_handlers[i])(sig, siginfo, ucontext);
1660Sstevel@tonic-gate lmutex_unlock(&sigfpe_lock);
1670Sstevel@tonic-gate return;
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate ieee:
1700Sstevel@tonic-gate switch ((intptr_t)ieee_handlers[(int)exception]) {
1710Sstevel@tonic-gate case ((intptr_t)(SIGFPE_DEFAULT)): /* Error condition but ignore it. */
1720Sstevel@tonic-gate case ((intptr_t)(SIGFPE_IGNORE)): /* Error condition but ignore it. */
1730Sstevel@tonic-gate lmutex_unlock(&sigfpe_lock);
1740Sstevel@tonic-gate return;
1750Sstevel@tonic-gate case ((intptr_t)(SIGFPE_ABORT)):
1760Sstevel@tonic-gate abort();
1770Sstevel@tonic-gate default:
1780Sstevel@tonic-gate (ieee_handlers[(int)exception])(sig, siginfo, ucontext);
1790Sstevel@tonic-gate lmutex_unlock(&sigfpe_lock);
1800Sstevel@tonic-gate return;
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate
1840Sstevel@tonic-gate static int
_enable_sigfpe_master(void)1850Sstevel@tonic-gate _enable_sigfpe_master(void)
1860Sstevel@tonic-gate {
1870Sstevel@tonic-gate /* Enable the sigfpe master handler always. */
1880Sstevel@tonic-gate struct sigaction newsigact, oldsigact;
1890Sstevel@tonic-gate
1900Sstevel@tonic-gate newsigact.sa_sigaction = _sigfpe_master;
1910Sstevel@tonic-gate (void) sigemptyset(&newsigact.sa_mask);
1920Sstevel@tonic-gate newsigact.sa_flags = SA_SIGINFO; /* enhanced handler */
1930Sstevel@tonic-gate _sigfpe_master_enabled = 1;
1940Sstevel@tonic-gate return (sigaction(SIGFPE, &newsigact, &oldsigact));
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate
1970Sstevel@tonic-gate static int
_test_sigfpe_master(void)1980Sstevel@tonic-gate _test_sigfpe_master(void)
1990Sstevel@tonic-gate {
2000Sstevel@tonic-gate /*
2010Sstevel@tonic-gate * Enable the sigfpe master handler if it's never been enabled
2020Sstevel@tonic-gate * before.
2030Sstevel@tonic-gate */
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate if (_sigfpe_master_enabled == 0)
2060Sstevel@tonic-gate return (_enable_sigfpe_master());
2070Sstevel@tonic-gate else
2080Sstevel@tonic-gate return (_sigfpe_master_enabled);
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate
2110Sstevel@tonic-gate sigfpe_handler_type
sigfpe(sigfpe_code_type code,sigfpe_handler_type hdl)2120Sstevel@tonic-gate sigfpe(sigfpe_code_type code, sigfpe_handler_type hdl)
2130Sstevel@tonic-gate {
2140Sstevel@tonic-gate sigfpe_handler_type oldhdl;
2150Sstevel@tonic-gate int i;
2160Sstevel@tonic-gate
2170Sstevel@tonic-gate lmutex_lock(&sigfpe_lock);
2180Sstevel@tonic-gate (void) _test_sigfpe_master();
219*6812Sraf for (i = 0; (i < N_SIGFPE_CODE) && (code != sigfpe_codes[i]); i++)
220*6812Sraf continue;
2210Sstevel@tonic-gate /* Find index of handler. */
2220Sstevel@tonic-gate if (i >= N_SIGFPE_CODE) {
2230Sstevel@tonic-gate errno = EINVAL;
2240Sstevel@tonic-gate lmutex_unlock(&sigfpe_lock);
2250Sstevel@tonic-gate /* Not 0 or SIGFPE code */
2260Sstevel@tonic-gate return ((sigfpe_handler_type)BADSIG);
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate oldhdl = sigfpe_handlers[i];
2290Sstevel@tonic-gate sigfpe_handlers[i] = hdl;
2300Sstevel@tonic-gate lmutex_unlock(&sigfpe_lock);
2310Sstevel@tonic-gate return (oldhdl);
2320Sstevel@tonic-gate }
233