1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
|
/***********************************************************************/
/* */
/* Caml Special Light */
/* */
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
/* */
/* Copyright 1995 Institut National de Recherche en Informatique et */
/* Automatique. Distributed only by permission. */
/* */
/***********************************************************************/
/* $Id$ */
#include <signal.h>
#include <stdio.h>
#include "alloc.h"
#include "memory.h"
#include "minor_gc.h"
#include "misc.h"
#include "mlvalues.h"
#include "fail.h"
#include "signals.h"
Volatile int async_signal_mode = 0;
Volatile int pending_signal = 0;
Volatile int force_major_slice = 0;
value signal_handlers = 0;
extern char * caml_last_return_address;
/* Call the handler for the given signal */
static void execute_signal(signal_number)
int signal_number;
{
Assert (!async_signal_mode);
callback(Field(signal_handlers, signal_number), Val_int(signal_number));
}
/* This routine is the common entry point for garbage collection
and signal handling */
void garbage_collection()
{
int sig;
if (young_ptr < young_start || force_major_slice) minor_collection();
/* If a signal arrives between the following two instructions,
it will be lost. */
sig = pending_signal;
pending_signal = 0;
if (sig) execute_signal(sig);
young_limit = young_start;
}
/* Trigger a garbage collection as soon as possible */
void urge_major_slice ()
{
force_major_slice = 1;
young_limit = young_end;
/* This is only moderately effective on ports that cache young_limit
in a register, since modify() is called directly, not through
caml_c_call, so it may take a while before the register is reloaded
from young_limit. */
}
void enter_blocking_section()
{
int sig;
while (1){
Assert (!async_signal_mode);
/* If a signal arrives between the next two instructions,
it will be lost. */
sig = pending_signal;
pending_signal = 0;
if (sig) execute_signal(sig);
async_signal_mode = 1;
if (!pending_signal) break;
async_signal_mode = 0;
}
}
/* This function may be called from outside a blocking section. */
void leave_blocking_section()
{
async_signal_mode = 0;
}
#if defined(TARGET_alpha) || defined(TARGET_mips) || defined(TARGET_power)
void handle_signal(sig, code, context)
int sig, code;
struct sigcontext * context;
#else
void handle_signal(sig)
int sig;
#endif
{
#ifndef POSIX_SIGNALS
#ifndef BSD_SIGNALS
signal(sig, handle_signal);
#endif
#endif
if (async_signal_mode) {
/* We are interrupting a C function blocked on I/O.
Callback the Caml code immediately. */
leave_blocking_section();
callback(Field(signal_handlers, sig), Val_int(sig));
enter_blocking_section();
} else {
/* We can't execute the signal code immediately.
Instead, we remember the signal and play with the allocation limit
so that the next allocation will trigger a garbage collection. */
pending_signal = sig;
young_limit = young_end;
/* Some ports cache young_limit in a register.
Use the signal context to modify that register too, but not if
we are inside C code (i.e. caml_last_return_address != NULL). */
#ifdef TARGET_alpha
/* Cached in register $14 */
if (caml_last_return_address == NULL)
context->sc_regs[14] = (long) young_limit;
#endif
#ifdef TARGET_mips
/* Cached in register $23 */
if (caml_last_return_address == NULL)
context->sc_regs[23] = (int) young_limit;
#endif
#ifdef TARGET_power
/* Cached in register 31 */
#ifdef _AIX
if (caml_last_return_address == NULL)
context->sc_jmpbuf.jmp_context.gpr[31] = (ulong_t) young_limit;
#endif
#endif
}
}
#ifndef SIGABRT
#define SIGABRT -1
#endif
#ifndef SIGALRM
#define SIGALRM -1
#endif
#ifndef SIGFPE
#define SIGFPE -1
#endif
#ifndef SIGHUP
#define SIGHUP -1
#endif
#ifndef SIGILL
#define SIGILL -1
#endif
#ifndef SIGINT
#define SIGINT -1
#endif
#ifndef SIGKILL
#define SIGKILL -1
#endif
#ifndef SIGPIPE
#define SIGPIPE -1
#endif
#ifndef SIGQUIT
#define SIGQUIT -1
#endif
#ifndef SIGSEGV
#define SIGSEGV -1
#endif
#ifndef SIGTERM
#define SIGTERM -1
#endif
#ifndef SIGUSR1
#define SIGUSR1 -1
#endif
#ifndef SIGUSR2
#define SIGUSR2 -1
#endif
#ifndef SIGCHLD
#define SIGCHLD -1
#endif
#ifndef SIGCONT
#define SIGCONT -1
#endif
#ifndef SIGSTOP
#define SIGSTOP -1
#endif
#ifndef SIGTSTP
#define SIGTSTP -1
#endif
#ifndef SIGTTIN
#define SIGTTIN -1
#endif
#ifndef SIGTTOU
#define SIGTTOU -1
#endif
#ifndef SIGVTALRM
#define SIGVTALRM -1
#endif
#ifndef SIGPROF
#define SIGPROF -1
#endif
int posix_signals[] = {
SIGABRT, SIGALRM, SIGFPE, SIGHUP, SIGILL, SIGINT, SIGKILL, SIGPIPE,
SIGQUIT, SIGSEGV, SIGTERM, SIGUSR1, SIGUSR2, SIGCHLD, SIGCONT,
SIGSTOP, SIGTSTP, SIGTTIN, SIGTTOU, SIGVTALRM, SIGPROF
};
#ifndef NSIG
#define NSIG 32
#endif
value install_signal_handler(signal_number, action) /* ML */
value signal_number, action;
{
int sig;
void (*act)();
#ifdef POSIX_SIGNALS
struct sigaction sigact;
#endif
sig = Int_val(signal_number);
if (sig < 0) sig = posix_signals[-sig-1];
if (sig < 0 || sig >= NSIG)
invalid_argument("Sys.signal: unavailable signal");
switch(action) {
case Val_int(0): /* Signal_default */
act = SIG_DFL;
break;
case Val_int(1): /* Signal_ignore */
act = SIG_IGN;
break;
default: /* Signal_handle */
if (signal_handlers == 0) {
int i;
Push_roots(r, 1);
r[0] = action;
signal_handlers = alloc_tuple(NSIG);
action = r[0];
Pop_roots();
for (i = 0; i < NSIG; i++) Field(signal_handlers, i) = Val_int(0);
register_global_root(&signal_handlers);
}
modify(&Field(signal_handlers, sig), Field(action, 0));
act = handle_signal;
break;
}
#ifndef POSIX_SIGNALS
signal(sig, act);
#else
sigact.sa_handler = act;
sigact.sa_flags = 0;
sigemptyset(&sigact.sa_mask);
sigaction(sig, &sigact, NULL);
#endif
return Val_unit;
}
/* Machine- and OS-dependent handling of bound check trap */
#if defined(TARGET_sparc) && defined(SYS_sunos)
static void trap_handler(sig, code, context, address)
int sig, code;
struct sigcontext * context;
char * address;
{
if (sig == SIGILL && code == ILL_TRAP_FAULT(5)) {
array_bound_error();
} else {
fprintf(stderr, "Fatal error: illegal instruction, code 0x%x\n", code);
exit(100);
}
}
#endif
#if defined(TARGET_sparc) && defined(SYS_solaris)
static void trap_handler(sig, info, context)
int sig;
siginfo_t * info;
struct ucontext_t * context;
{
if (sig == SIGILL && info->si_code == ILL_ILLTRP) {
array_bound_error();
} else {
fprintf(stderr, "Fatal error: illegal instruction, code 0x%x\n",
info->si_code);
exit(100);
}
}
#endif
#if defined(TARGET_sparc) && defined(SYS_bsd)
static void trap_handler(sig)
int sig;
{
array_bound_error();
}
#endif
#if defined(TARGET_power)
static void trap_handler(sig)
int sig;
{
array_bound_error();
}
#endif
/* Initialization of signal stuff */
void init_signals()
{
#if defined(TARGET_sparc) && (defined(SYS_sunos) || defined(SYS_bsd))
signal(SIGILL, trap_handler);
#endif
#if defined(TARGET_sparc) && defined(SYS_solaris)
struct sigaction act;
act.sa_sigaction = trap_handler;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_SIGINFO;
sigaction(SIGILL, &act, NULL);
#endif
#if defined(TARGET_power)
signal(SIGTRAP, trap_handler);
#endif
}
|