17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7257d1b4Sraf * Common Development and Distribution License (the "License"). 6*7257d1b4Sraf * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 20*7257d1b4Sraf */ 21*7257d1b4Sraf 22*7257d1b4Sraf /* 23*7257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7257d1b4Sraf */ 26*7257d1b4Sraf 27*7257d1b4Sraf /* 287c478bd9Sstevel@tonic-gate * Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T 297c478bd9Sstevel@tonic-gate * All Rights Reserved 307c478bd9Sstevel@tonic-gate * 317c478bd9Sstevel@tonic-gate * Portions of this source code were derived from Berkeley 327c478bd9Sstevel@tonic-gate * 4.3 BSD under license from the regents of the University of 337c478bd9Sstevel@tonic-gate * California. 347c478bd9Sstevel@tonic-gate */ 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 377c478bd9Sstevel@tonic-gate 387c478bd9Sstevel@tonic-gate /* Swap handler for SIGFPE codes. */ 397c478bd9Sstevel@tonic-gate 40*7257d1b4Sraf #include "lint.h" 417c478bd9Sstevel@tonic-gate #include <mtlib.h> 427c478bd9Sstevel@tonic-gate #include <errno.h> 437c478bd9Sstevel@tonic-gate #include <signal.h> 447c478bd9Sstevel@tonic-gate #include <floatingpoint.h> 457c478bd9Sstevel@tonic-gate #include <sys/types.h> 467c478bd9Sstevel@tonic-gate #include <sys/ucontext.h> 477c478bd9Sstevel@tonic-gate #include <sys/siginfo.h> 487c478bd9Sstevel@tonic-gate #include <thread.h> 497c478bd9Sstevel@tonic-gate #include <synch.h> 507c478bd9Sstevel@tonic-gate #include <stdlib.h> 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate #ifndef FPE_INTDIV 537c478bd9Sstevel@tonic-gate #define FPE_INTDIV 1 /* integer divide by zero */ 547c478bd9Sstevel@tonic-gate #endif 557c478bd9Sstevel@tonic-gate #ifndef FPE_INTOVF 567c478bd9Sstevel@tonic-gate #define FPE_INTOVF 2 /* integer overflow */ 577c478bd9Sstevel@tonic-gate #endif 587c478bd9Sstevel@tonic-gate #ifndef FPE_FLTDIV 597c478bd9Sstevel@tonic-gate #define FPE_FLTDIV 3 /* [floating divide by zero] */ 607c478bd9Sstevel@tonic-gate #endif 617c478bd9Sstevel@tonic-gate #ifndef FPE_FLTOVF 627c478bd9Sstevel@tonic-gate #define FPE_FLTOVF 4 /* [floating overflow] */ 637c478bd9Sstevel@tonic-gate #endif 647c478bd9Sstevel@tonic-gate #ifndef FPE_FLTUND 657c478bd9Sstevel@tonic-gate #define FPE_FLTUND 5 /* [floating underflow] */ 667c478bd9Sstevel@tonic-gate #endif 677c478bd9Sstevel@tonic-gate #ifndef FPE_FLTRES 687c478bd9Sstevel@tonic-gate #define FPE_FLTRES 6 /* [floating inexact result] */ 697c478bd9Sstevel@tonic-gate #endif 707c478bd9Sstevel@tonic-gate #ifndef FPE_FLTINV 717c478bd9Sstevel@tonic-gate #define FPE_FLTINV 7 /* [floating invalid operation] */ 727c478bd9Sstevel@tonic-gate #endif 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate #if defined(__i386) || defined(__amd64) 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate #ifndef FPE_FLTSUB 777c478bd9Sstevel@tonic-gate #define FPE_FLTSUB 8 /* subscript out of range */ 787c478bd9Sstevel@tonic-gate #endif 797c478bd9Sstevel@tonic-gate #ifndef FPE_FLTDEN 807c478bd9Sstevel@tonic-gate #define FPE_FLTDEN 9 /* x86-specific: denormal operand */ 817c478bd9Sstevel@tonic-gate #endif 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate #define N_SIGFPE_CODE 10 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate #else 867c478bd9Sstevel@tonic-gate 877c478bd9Sstevel@tonic-gate #define N_SIGFPE_CODE 8 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate #endif /* __i386 */ 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate /* Array of SIGFPE codes. */ 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate static const sigfpe_code_type sigfpe_codes[N_SIGFPE_CODE] = { 947c478bd9Sstevel@tonic-gate FPE_INTDIV, 957c478bd9Sstevel@tonic-gate FPE_INTOVF, 967c478bd9Sstevel@tonic-gate FPE_FLTDIV, 977c478bd9Sstevel@tonic-gate FPE_FLTOVF, 987c478bd9Sstevel@tonic-gate FPE_FLTUND, 997c478bd9Sstevel@tonic-gate FPE_FLTRES, 1007c478bd9Sstevel@tonic-gate FPE_FLTINV, 1017c478bd9Sstevel@tonic-gate #if defined(__i386) || defined(__amd64) 1027c478bd9Sstevel@tonic-gate FPE_FLTSUB, 1037c478bd9Sstevel@tonic-gate FPE_FLTDEN, 1047c478bd9Sstevel@tonic-gate #endif 1057c478bd9Sstevel@tonic-gate 0 1067c478bd9Sstevel@tonic-gate }; 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate /* Array of handlers. */ 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate static mutex_t sigfpe_lock = DEFAULTMUTEX; 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate sigfpe_handler_type ieee_handlers[N_IEEE_EXCEPTION]; 1137c478bd9Sstevel@tonic-gate static sigfpe_handler_type sigfpe_handlers[N_SIGFPE_CODE]; 1147c478bd9Sstevel@tonic-gate 1157c478bd9Sstevel@tonic-gate static int _sigfpe_master_enabled; 1167c478bd9Sstevel@tonic-gate /* Originally zero, set to 1 by _enable_sigfpe_master. */ 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate #ifndef BADSIG 1197c478bd9Sstevel@tonic-gate #define BADSIG (void (*)(void))-1 1207c478bd9Sstevel@tonic-gate #endif 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate static void 1237c478bd9Sstevel@tonic-gate _sigfpe_master(int sig, siginfo_t *siginfo, void *arg) 1247c478bd9Sstevel@tonic-gate { 1257c478bd9Sstevel@tonic-gate ucontext_t *ucontext = arg; 1267c478bd9Sstevel@tonic-gate int i; 1277c478bd9Sstevel@tonic-gate int code; 1287c478bd9Sstevel@tonic-gate enum fp_exception_type exception; 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate lmutex_lock(&sigfpe_lock); 1317c478bd9Sstevel@tonic-gate code = siginfo->si_code; 132*7257d1b4Sraf for (i = 0; (i < N_SIGFPE_CODE) && (code != sigfpe_codes[i]); i++) 133*7257d1b4Sraf continue; 1347c478bd9Sstevel@tonic-gate /* Find index of handler. */ 1357c478bd9Sstevel@tonic-gate if (i >= N_SIGFPE_CODE) 1367c478bd9Sstevel@tonic-gate i = N_SIGFPE_CODE - 1; 1377c478bd9Sstevel@tonic-gate switch ((intptr_t)sigfpe_handlers[i]) { 1387c478bd9Sstevel@tonic-gate case ((intptr_t)(SIGFPE_DEFAULT)): 1397c478bd9Sstevel@tonic-gate switch (code) { 1407c478bd9Sstevel@tonic-gate case FPE_FLTINV: 1417c478bd9Sstevel@tonic-gate exception = fp_invalid; 1427c478bd9Sstevel@tonic-gate goto ieee; 1437c478bd9Sstevel@tonic-gate case FPE_FLTRES: 1447c478bd9Sstevel@tonic-gate exception = fp_inexact; 1457c478bd9Sstevel@tonic-gate goto ieee; 1467c478bd9Sstevel@tonic-gate case FPE_FLTDIV: 1477c478bd9Sstevel@tonic-gate exception = fp_division; 1487c478bd9Sstevel@tonic-gate goto ieee; 1497c478bd9Sstevel@tonic-gate case FPE_FLTUND: 1507c478bd9Sstevel@tonic-gate exception = fp_underflow; 1517c478bd9Sstevel@tonic-gate goto ieee; 1527c478bd9Sstevel@tonic-gate case FPE_FLTOVF: 1537c478bd9Sstevel@tonic-gate exception = fp_overflow; 1547c478bd9Sstevel@tonic-gate goto ieee; 1557c478bd9Sstevel@tonic-gate default: /* The common default treatment is to abort. */ 1567c478bd9Sstevel@tonic-gate break; 1577c478bd9Sstevel@tonic-gate } 1587c478bd9Sstevel@tonic-gate case ((intptr_t)(SIGFPE_ABORT)): 1597c478bd9Sstevel@tonic-gate abort(); 1607c478bd9Sstevel@tonic-gate break; 1617c478bd9Sstevel@tonic-gate case ((intptr_t)(SIGFPE_IGNORE)): 1627c478bd9Sstevel@tonic-gate lmutex_unlock(&sigfpe_lock); 1637c478bd9Sstevel@tonic-gate return; 1647c478bd9Sstevel@tonic-gate default: /* User-defined not SIGFPE_DEFAULT or SIGFPE_ABORT. */ 1657c478bd9Sstevel@tonic-gate (sigfpe_handlers[i])(sig, siginfo, ucontext); 1667c478bd9Sstevel@tonic-gate lmutex_unlock(&sigfpe_lock); 1677c478bd9Sstevel@tonic-gate return; 1687c478bd9Sstevel@tonic-gate } 1697c478bd9Sstevel@tonic-gate ieee: 1707c478bd9Sstevel@tonic-gate switch ((intptr_t)ieee_handlers[(int)exception]) { 1717c478bd9Sstevel@tonic-gate case ((intptr_t)(SIGFPE_DEFAULT)): /* Error condition but ignore it. */ 1727c478bd9Sstevel@tonic-gate case ((intptr_t)(SIGFPE_IGNORE)): /* Error condition but ignore it. */ 1737c478bd9Sstevel@tonic-gate lmutex_unlock(&sigfpe_lock); 1747c478bd9Sstevel@tonic-gate return; 1757c478bd9Sstevel@tonic-gate case ((intptr_t)(SIGFPE_ABORT)): 1767c478bd9Sstevel@tonic-gate abort(); 1777c478bd9Sstevel@tonic-gate default: 1787c478bd9Sstevel@tonic-gate (ieee_handlers[(int)exception])(sig, siginfo, ucontext); 1797c478bd9Sstevel@tonic-gate lmutex_unlock(&sigfpe_lock); 1807c478bd9Sstevel@tonic-gate return; 1817c478bd9Sstevel@tonic-gate } 1827c478bd9Sstevel@tonic-gate } 1837c478bd9Sstevel@tonic-gate 1847c478bd9Sstevel@tonic-gate static int 1857c478bd9Sstevel@tonic-gate _enable_sigfpe_master(void) 1867c478bd9Sstevel@tonic-gate { 1877c478bd9Sstevel@tonic-gate /* Enable the sigfpe master handler always. */ 1887c478bd9Sstevel@tonic-gate struct sigaction newsigact, oldsigact; 1897c478bd9Sstevel@tonic-gate 1907c478bd9Sstevel@tonic-gate newsigact.sa_sigaction = _sigfpe_master; 1917c478bd9Sstevel@tonic-gate (void) sigemptyset(&newsigact.sa_mask); 1927c478bd9Sstevel@tonic-gate newsigact.sa_flags = SA_SIGINFO; /* enhanced handler */ 1937c478bd9Sstevel@tonic-gate _sigfpe_master_enabled = 1; 1947c478bd9Sstevel@tonic-gate return (sigaction(SIGFPE, &newsigact, &oldsigact)); 1957c478bd9Sstevel@tonic-gate } 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate static int 1987c478bd9Sstevel@tonic-gate _test_sigfpe_master(void) 1997c478bd9Sstevel@tonic-gate { 2007c478bd9Sstevel@tonic-gate /* 2017c478bd9Sstevel@tonic-gate * Enable the sigfpe master handler if it's never been enabled 2027c478bd9Sstevel@tonic-gate * before. 2037c478bd9Sstevel@tonic-gate */ 2047c478bd9Sstevel@tonic-gate 2057c478bd9Sstevel@tonic-gate if (_sigfpe_master_enabled == 0) 2067c478bd9Sstevel@tonic-gate return (_enable_sigfpe_master()); 2077c478bd9Sstevel@tonic-gate else 2087c478bd9Sstevel@tonic-gate return (_sigfpe_master_enabled); 2097c478bd9Sstevel@tonic-gate } 2107c478bd9Sstevel@tonic-gate 2117c478bd9Sstevel@tonic-gate sigfpe_handler_type 2127c478bd9Sstevel@tonic-gate sigfpe(sigfpe_code_type code, sigfpe_handler_type hdl) 2137c478bd9Sstevel@tonic-gate { 2147c478bd9Sstevel@tonic-gate sigfpe_handler_type oldhdl; 2157c478bd9Sstevel@tonic-gate int i; 2167c478bd9Sstevel@tonic-gate 2177c478bd9Sstevel@tonic-gate lmutex_lock(&sigfpe_lock); 2187c478bd9Sstevel@tonic-gate (void) _test_sigfpe_master(); 219*7257d1b4Sraf for (i = 0; (i < N_SIGFPE_CODE) && (code != sigfpe_codes[i]); i++) 220*7257d1b4Sraf continue; 2217c478bd9Sstevel@tonic-gate /* Find index of handler. */ 2227c478bd9Sstevel@tonic-gate if (i >= N_SIGFPE_CODE) { 2237c478bd9Sstevel@tonic-gate errno = EINVAL; 2247c478bd9Sstevel@tonic-gate lmutex_unlock(&sigfpe_lock); 2257c478bd9Sstevel@tonic-gate /* Not 0 or SIGFPE code */ 2267c478bd9Sstevel@tonic-gate return ((sigfpe_handler_type)BADSIG); 2277c478bd9Sstevel@tonic-gate } 2287c478bd9Sstevel@tonic-gate oldhdl = sigfpe_handlers[i]; 2297c478bd9Sstevel@tonic-gate sigfpe_handlers[i] = hdl; 2307c478bd9Sstevel@tonic-gate lmutex_unlock(&sigfpe_lock); 2317c478bd9Sstevel@tonic-gate return (oldhdl); 2327c478bd9Sstevel@tonic-gate } 233