| 1 |
/* GSysExceptions.c low level module interfacing exceptions to the OS. |
| 2 |
|
| 3 |
Copyright (C) 2016-2025 Free Software Foundation, Inc. |
| 4 |
Contributed by Gaius Mulley <[email protected]>. |
| 5 |
|
| 6 |
This file is part of GNU Modula-2. |
| 7 |
|
| 8 |
GNU Modula-2 is free software; you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation; either version 3, or (at your option) |
| 11 |
any later version. |
| 12 |
|
| 13 |
GNU Modula-2 is distributed in the hope that it will be useful, but |
| 14 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
General Public License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU General Public License |
| 19 |
along with GNU Modula-2; see the file COPYING3. If not see |
| 20 |
<http://www.gnu.org/licenses/>. */ |
| 21 |
|
| 22 |
#include "config.h" |
| 23 |
#include "system.h" |
| 24 |
|
| 25 |
#include "gm2-libs-host.h" |
| 26 |
|
| 27 |
#ifdef MC_M2 |
| 28 |
#include "GSysExceptions.h" |
| 29 |
#define DECL_PROC_T(X) SysExceptions_PROCEXCEPTION X |
| 30 |
#define PROC_FUNC(X) X.proc |
| 31 |
#else |
| 32 |
#define DECL_PROC_T(X) void (*X) (void *) |
| 33 |
#define PROC_FUNC(X) X |
| 34 |
#endif |
| 35 |
|
| 36 |
#undef EXTERN |
| 37 |
#if defined(__cplusplus) |
| 38 |
#define EXTERN extern "C" |
| 39 |
#else |
| 40 |
#define EXTERN |
| 41 |
#endif |
| 42 |
|
| 43 |
#if 0 |
| 44 |
/* Signals. */ |
| 45 |
#define SIGHUP 1 /* Hangup (POSIX). */ |
| 46 |
#define SIGINT 2 /* Interrupt (ANSI). */ |
| 47 |
#define SIGQUIT 3 /* Quit (POSIX). */ |
| 48 |
#define SIGILL 4 /* Illegal instruction (ANSI). */ |
| 49 |
#define SIGTRAP 5 /* Trace trap (POSIX). */ |
| 50 |
#define SIGABRT 6 /* Abort (ANSI). */ |
| 51 |
#define SIGIOT 6 /* IOT trap (4.2 BSD). */ |
| 52 |
#define SIGBUS 7 /* BUS error (4.2 BSD). */ |
| 53 |
#define SIGFPE 8 /* Floating-point exception (ANSI). */ |
| 54 |
#define SIGKILL 9 /* Kill, unblockable (POSIX). */ |
| 55 |
#define SIGUSR1 10 /* User-defined signal 1 (POSIX). */ |
| 56 |
#define SIGSEGV 11 /* Segmentation violation (ANSI). */ |
| 57 |
#define SIGUSR2 12 /* User-defined signal 2 (POSIX). */ |
| 58 |
#define SIGPIPE 13 /* Broken pipe (POSIX). */ |
| 59 |
#define SIGALRM 14 /* Alarm clock (POSIX). */ |
| 60 |
#define SIGTERM 15 /* Termination (ANSI). */ |
| 61 |
#define SIGSTKFLT 16 /* Stack fault. */ |
| 62 |
#define SIGCLD SIGCHLD /* Same as SIGCHLD (System V). */ |
| 63 |
#define SIGCHLD 17 /* Child status has changed (POSIX). */ |
| 64 |
#define SIGCONT 18 /* Continue (POSIX). */ |
| 65 |
#define SIGSTOP 19 /* Stop, unblockable (POSIX). */ |
| 66 |
#define SIGTSTP 20 /* Keyboard stop (POSIX). */ |
| 67 |
#define SIGTTIN 21 /* Background read from tty (POSIX). */ |
| 68 |
#define SIGTTOU 22 /* Background write to tty (POSIX). */ |
| 69 |
#define SIGURG 23 /* Urgent condition on socket (4.2 BSD). */ |
| 70 |
#define SIGXCPU 24 /* CPU limit exceeded (4.2 BSD). */ |
| 71 |
#define SIGXFSZ 25 /* File size limit exceeded (4.2 BSD). */ |
| 72 |
#define SIGVTALRM 26 /* Virtual alarm clock (4.2 BSD). */ |
| 73 |
#define SIGPROF 27 /* Profiling alarm clock (4.2 BSD). */ |
| 74 |
#define SIGWINCH 28 /* Window size change (4.3 BSD, Sun). */ |
| 75 |
#define SIGPOLL SIGIO /* Pollable event occurred (System V). */ |
| 76 |
#define SIGIO 29 /* I/O now possible (4.2 BSD). */ |
| 77 |
#define SIGPWR 30 /* Power failure restart (System V). */ |
| 78 |
#define SIGSYS 31 /* Bad system call. */ |
| 79 |
#define SIGUNUSED 31 |
| 80 |
|
| 81 |
(indexException, rangeException, caseSelectException, invalidLocation, |
| 82 |
functionException, wholeValueException, wholeDivException, realValueException, |
| 83 |
realDivException, complexValueException, complexDivException, protException, |
| 84 |
sysException, coException, exException |
| 85 |
); |
| 86 |
#endif |
| 87 |
|
| 88 |
/* wholeDivException and realDivException are caught by SIGFPE |
| 89 |
and depatched to the appropriate Modula-2 runtime routine upon |
| 90 |
testing FPE_INTDIV or FPE_FLTDIV. realValueException is also |
| 91 |
caught by SIGFPE and dispatched by testing FFE_FLTOVF or |
| 92 |
FPE_FLTUND or FPE_FLTRES or FPE_FLTINV. indexException is |
| 93 |
caught by SIGFPE and dispatched by FPE_FLTSUB. */ |
| 94 |
|
| 95 |
#if defined(HAVE_SIGNAL_H) |
| 96 |
static struct sigaction sigbus; |
| 97 |
static struct sigaction sigfpe_; |
| 98 |
static struct sigaction sigsegv; |
| 99 |
|
| 100 |
static void (*indexProc) (void *); |
| 101 |
static void (*rangeProc) (void *); |
| 102 |
static void (*assignmentrangeProc) (void *); |
| 103 |
static void (*caseProc) (void *); |
| 104 |
static void (*invalidlocProc) (void *); |
| 105 |
static void (*functionProc) (void *); |
| 106 |
static void (*wholevalueProc) (void *); |
| 107 |
static void (*wholedivProc) (void *); |
| 108 |
static void (*realvalueProc) (void *); |
| 109 |
static void (*realdivProc) (void *); |
| 110 |
static void (*complexvalueProc) (void *); |
| 111 |
static void (*complexdivProc) (void *); |
| 112 |
static void (*protectionProc) (void *); |
| 113 |
static void (*systemProc) (void *); |
| 114 |
static void (*coroutineProc) (void *); |
| 115 |
static void (*exceptionProc) (void *); |
| 116 |
|
| 117 |
|
| 118 |
static void |
| 119 |
sigbusDespatcher (int signum, siginfo_t *info, void *ucontext) |
| 120 |
{ |
| 121 |
switch (signum) |
| 122 |
{ |
| 123 |
|
| 124 |
case SIGSEGV: |
| 125 |
case SIGBUS: |
| 126 |
if (info) |
| 127 |
(*invalidlocProc) (info->si_addr); |
| 128 |
break; |
| 129 |
default: |
| 130 |
perror ("not expecting to arrive here with this signal"); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
static void |
| 135 |
sigfpeDespatcher (int signum, siginfo_t *info, void *ucontext) |
| 136 |
{ |
| 137 |
switch (signum) |
| 138 |
{ |
| 139 |
|
| 140 |
case SIGFPE: |
| 141 |
if (info) |
| 142 |
{ |
| 143 |
if (info->si_code | FPE_INTDIV) |
| 144 |
(*wholedivProc) (info->si_addr); /* integer divide by zero. */ |
| 145 |
if (info->si_code | FPE_INTOVF) |
| 146 |
(*wholevalueProc) (info->si_addr); /* integer overflow. */ |
| 147 |
if (info->si_code | FPE_FLTDIV) |
| 148 |
(*realdivProc) ( |
| 149 |
info->si_addr); /* floating-point divide by zero. */ |
| 150 |
if (info->si_code | FPE_FLTOVF) |
| 151 |
(*realvalueProc) (info->si_addr); /* floating-point overflow. */ |
| 152 |
if (info->si_code | FPE_FLTUND) |
| 153 |
(*realvalueProc) (info->si_addr); /* floating-point underflow. */ |
| 154 |
if (info->si_code | FPE_FLTRES) |
| 155 |
(*realvalueProc) ( |
| 156 |
info->si_addr); /* floating-point inexact result. */ |
| 157 |
if (info->si_code | FPE_FLTINV) |
| 158 |
(*realvalueProc) ( |
| 159 |
info->si_addr); /* floating-point invalid result. */ |
| 160 |
if (info->si_code | FPE_FLTSUB) |
| 161 |
(*indexProc) (info->si_addr); /* subscript out of range. */ |
| 162 |
} |
| 163 |
break; |
| 164 |
default: |
| 165 |
perror ("not expecting to arrive here with this signal"); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
EXTERN |
| 170 |
void |
| 171 |
SysExceptions_InitExceptionHandlers (DECL_PROC_T(indexf), |
| 172 |
DECL_PROC_T(range), |
| 173 |
DECL_PROC_T(casef), |
| 174 |
DECL_PROC_T(invalidloc), |
| 175 |
DECL_PROC_T(function), |
| 176 |
DECL_PROC_T(wholevalue), |
| 177 |
DECL_PROC_T(wholediv), |
| 178 |
DECL_PROC_T(realvalue), |
| 179 |
DECL_PROC_T(realdiv), |
| 180 |
DECL_PROC_T(complexvalue), |
| 181 |
DECL_PROC_T(complexdiv), |
| 182 |
DECL_PROC_T(protection), |
| 183 |
DECL_PROC_T(systemf), |
| 184 |
DECL_PROC_T(coroutine), |
| 185 |
DECL_PROC_T(exception)) |
| 186 |
{ |
| 187 |
struct sigaction old; |
| 188 |
|
| 189 |
indexProc = PROC_FUNC (indexf); |
| 190 |
rangeProc = PROC_FUNC (range); |
| 191 |
caseProc = PROC_FUNC (casef); |
| 192 |
invalidlocProc = PROC_FUNC (invalidloc); |
| 193 |
functionProc = PROC_FUNC (function); |
| 194 |
wholevalueProc = PROC_FUNC (wholevalue); |
| 195 |
wholedivProc = PROC_FUNC (wholediv); |
| 196 |
realvalueProc = PROC_FUNC (realvalue); |
| 197 |
realdivProc = PROC_FUNC (realdiv); |
| 198 |
complexvalueProc = PROC_FUNC (complexvalue); |
| 199 |
complexdivProc = PROC_FUNC (complexdiv); |
| 200 |
protectionProc = PROC_FUNC (protection); |
| 201 |
systemProc = PROC_FUNC (systemf); |
| 202 |
coroutineProc = PROC_FUNC (coroutine); |
| 203 |
exceptionProc = PROC_FUNC (exception); |
| 204 |
|
| 205 |
sigbus.sa_sigaction = sigbusDespatcher; |
| 206 |
sigbus.sa_flags = (SA_SIGINFO); |
| 207 |
sigemptyset (&sigbus.sa_mask); |
| 208 |
|
| 209 |
if (sigaction (SIGBUS, &sigbus, &old) != 0) |
| 210 |
perror ("unable to install the sigbus signal handler"); |
| 211 |
|
| 212 |
sigsegv.sa_sigaction = sigbusDespatcher; |
| 213 |
sigsegv.sa_flags = (SA_SIGINFO); |
| 214 |
sigemptyset (&sigsegv.sa_mask); |
| 215 |
|
| 216 |
if (sigaction (SIGSEGV, &sigsegv, &old) != 0) |
| 217 |
perror ("unable to install the sigsegv signal handler"); |
| 218 |
|
| 219 |
sigfpe_.sa_sigaction = sigfpeDespatcher; |
| 220 |
sigfpe_.sa_flags = (SA_SIGINFO); |
| 221 |
sigemptyset (&sigfpe_.sa_mask); |
| 222 |
|
| 223 |
if (sigaction (SIGFPE, &sigfpe_, &old) != 0) |
| 224 |
perror ("unable to install the sigfpe signal handler"); |
| 225 |
} |
| 226 |
|
| 227 |
#else |
| 228 |
EXTERN |
| 229 |
void |
| 230 |
SysExceptions_InitExceptionHandlers (DECL_PROC_T(indexf), |
| 231 |
DECL_PROC_T(range), |
| 232 |
DECL_PROC_T(casef), |
| 233 |
DECL_PROC_T(invalidloc), |
| 234 |
DECL_PROC_T(function), |
| 235 |
DECL_PROC_T(wholevalue), |
| 236 |
DECL_PROC_T(wholediv), |
| 237 |
DECL_PROC_T(realvalue), |
| 238 |
DECL_PROC_T(realdiv), |
| 239 |
DECL_PROC_T(complexvalue), |
| 240 |
DECL_PROC_T(complexdiv), |
| 241 |
DECL_PROC_T(protection), |
| 242 |
DECL_PROC_T(systemf), |
| 243 |
DECL_PROC_T(coroutine), |
| 244 |
DECL_PROC_T(exception)) |
| 245 |
{ |
| 246 |
} |
| 247 |
#endif |
| 248 |
|
| 249 |
/* GNU Modula-2 linking fodder. */ |
| 250 |
|
| 251 |
EXTERN |
| 252 |
void |
| 253 |
_M2_SysExceptions_init (int argc, char *argv[], char *envp[]) |
| 254 |
{ |
| 255 |
} |
| 256 |
|
| 257 |
EXTERN |
| 258 |
void |
| 259 |
_M2_SysExceptions_fini (int argc, char *argv[], char *envp[]) |
| 260 |
{ |
| 261 |
} |