#include "lint.h"
#include <mtlib.h>
#include <errno.h>
#include <signal.h>
#include <floatingpoint.h>
#include <sys/types.h>
#include <sys/ucontext.h>
#include <sys/siginfo.h>
#include <thread.h>
#include <synch.h>
#include <stdlib.h>
#ifndef FPE_INTDIV
#define FPE_INTDIV 1
#endif
#ifndef FPE_INTOVF
#define FPE_INTOVF 2
#endif
#ifndef FPE_FLTDIV
#define FPE_FLTDIV 3
#endif
#ifndef FPE_FLTOVF
#define FPE_FLTOVF 4
#endif
#ifndef FPE_FLTUND
#define FPE_FLTUND 5
#endif
#ifndef FPE_FLTRES
#define FPE_FLTRES 6
#endif
#ifndef FPE_FLTINV
#define FPE_FLTINV 7
#endif
#if defined(__i386) || defined(__amd64)
#ifndef FPE_FLTSUB
#define FPE_FLTSUB 8
#endif
#ifndef FPE_FLTDEN
#define FPE_FLTDEN 9
#endif
#define N_SIGFPE_CODE 10
#else
#define N_SIGFPE_CODE 8
#endif
static const sigfpe_code_type sigfpe_codes[N_SIGFPE_CODE] = {
FPE_INTDIV,
FPE_INTOVF,
FPE_FLTDIV,
FPE_FLTOVF,
FPE_FLTUND,
FPE_FLTRES,
FPE_FLTINV,
#if defined(__i386) || defined(__amd64)
FPE_FLTSUB,
FPE_FLTDEN,
#endif
0
};
static mutex_t sigfpe_lock = DEFAULTMUTEX;
sigfpe_handler_type ieee_handlers[N_IEEE_EXCEPTION];
static sigfpe_handler_type sigfpe_handlers[N_SIGFPE_CODE];
static int _sigfpe_master_enabled;
#ifndef BADSIG
#define BADSIG (void (*)(void))-1
#endif
static void
_sigfpe_master(int sig, siginfo_t *siginfo, void *arg)
{
ucontext_t *ucontext = arg;
int i;
int code;
enum fp_exception_type exception;
lmutex_lock(&sigfpe_lock);
code = siginfo->si_code;
for (i = 0; (i < N_SIGFPE_CODE) && (code != sigfpe_codes[i]); i++)
continue;
if (i >= N_SIGFPE_CODE)
i = N_SIGFPE_CODE - 1;
switch ((intptr_t)sigfpe_handlers[i]) {
case ((intptr_t)(SIGFPE_DEFAULT)):
switch (code) {
case FPE_FLTINV:
exception = fp_invalid;
goto ieee;
case FPE_FLTRES:
exception = fp_inexact;
goto ieee;
case FPE_FLTDIV:
exception = fp_division;
goto ieee;
case FPE_FLTUND:
exception = fp_underflow;
goto ieee;
case FPE_FLTOVF:
exception = fp_overflow;
goto ieee;
#if defined(__i386) || defined(__amd64)
case FPE_FLTDEN:
exception = fp_denormalized;
goto ieee;
#endif
default:
break;
}
case ((intptr_t)(SIGFPE_ABORT)):
abort();
break;
case ((intptr_t)(SIGFPE_IGNORE)):
lmutex_unlock(&sigfpe_lock);
return;
default:
(sigfpe_handlers[i])(sig, siginfo, ucontext);
lmutex_unlock(&sigfpe_lock);
return;
}
ieee:
switch ((intptr_t)ieee_handlers[(int)exception]) {
case ((intptr_t)(SIGFPE_DEFAULT)):
case ((intptr_t)(SIGFPE_IGNORE)):
lmutex_unlock(&sigfpe_lock);
return;
case ((intptr_t)(SIGFPE_ABORT)):
abort();
default:
(ieee_handlers[(int)exception])(sig, siginfo, ucontext);
lmutex_unlock(&sigfpe_lock);
return;
}
}
static int
_enable_sigfpe_master(void)
{
struct sigaction newsigact, oldsigact;
newsigact.sa_sigaction = _sigfpe_master;
(void) sigemptyset(&newsigact.sa_mask);
newsigact.sa_flags = SA_SIGINFO;
_sigfpe_master_enabled = 1;
return (sigaction(SIGFPE, &newsigact, &oldsigact));
}
static int
_test_sigfpe_master(void)
{
if (_sigfpe_master_enabled == 0)
return (_enable_sigfpe_master());
else
return (_sigfpe_master_enabled);
}
sigfpe_handler_type
sigfpe(sigfpe_code_type code, sigfpe_handler_type hdl)
{
sigfpe_handler_type oldhdl;
int i;
lmutex_lock(&sigfpe_lock);
(void) _test_sigfpe_master();
for (i = 0; (i < N_SIGFPE_CODE) && (code != sigfpe_codes[i]); i++)
continue;
if (i >= N_SIGFPE_CODE) {
errno = EINVAL;
lmutex_unlock(&sigfpe_lock);
return ((sigfpe_handler_type)BADSIG);
}
oldhdl = sigfpe_handlers[i];
sigfpe_handlers[i] = hdl;
lmutex_unlock(&sigfpe_lock);
return (oldhdl);
}