1995-08-09 08:06:35 -07:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
1996-04-30 07:53:58 -07:00
|
|
|
/* Objective Caml */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
|
|
|
|
/* */
|
1996-04-30 07:53:58 -07:00
|
|
|
/* Copyright 1996 Institut National de Recherche en Informatique et */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* Automatique. Distributed only by permission. */
|
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
1995-12-21 03:01:45 -08:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
1996-10-08 02:31:43 -07:00
|
|
|
#if defined(__linux) && defined(TARGET_power)
|
1996-07-12 10:53:01 -07:00
|
|
|
#include <asm/sigcontext.h>
|
|
|
|
#endif
|
1995-12-21 03:01:45 -08:00
|
|
|
#include "alloc.h"
|
1996-11-15 07:24:14 -08:00
|
|
|
#include "callback.h"
|
1995-12-21 04:29:49 -08:00
|
|
|
#include "memory.h"
|
1995-12-21 03:01:45 -08:00
|
|
|
#include "minor_gc.h"
|
1995-07-10 02:48:27 -07:00
|
|
|
#include "misc.h"
|
|
|
|
#include "mlvalues.h"
|
1995-07-24 05:44:52 -07:00
|
|
|
#include "fail.h"
|
1995-07-10 02:48:27 -07:00
|
|
|
#include "signals.h"
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
volatile int async_signal_mode = 0;
|
|
|
|
volatile int pending_signal = 0;
|
|
|
|
volatile int force_major_slice = 0;
|
1995-12-21 04:29:49 -08:00
|
|
|
value signal_handlers = 0;
|
1997-03-17 02:17:32 -08:00
|
|
|
extern unsigned long caml_last_return_address;
|
1997-08-29 08:37:22 -07:00
|
|
|
void (*enter_blocking_section_hook)() = NULL;
|
|
|
|
void (*leave_blocking_section_hook)() = NULL;
|
1995-12-21 03:01:45 -08:00
|
|
|
|
1997-08-29 08:37:22 -07:00
|
|
|
/* Execute a signal handler immediately. */
|
1995-12-21 03:01:45 -08:00
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
static void execute_signal(int signal_number)
|
1995-12-21 03:01:45 -08:00
|
|
|
{
|
|
|
|
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 */
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void garbage_collection(void)
|
1995-12-21 03:01:45 -08:00
|
|
|
{
|
|
|
|
int sig;
|
|
|
|
|
1995-12-22 08:48:37 -08:00
|
|
|
if (young_ptr < young_start || force_major_slice) minor_collection();
|
1995-12-21 03:01:45 -08:00
|
|
|
/* If a signal arrives between the following two instructions,
|
|
|
|
it will be lost. */
|
|
|
|
sig = pending_signal;
|
|
|
|
pending_signal = 0;
|
|
|
|
young_limit = young_start;
|
1997-11-20 06:14:21 -08:00
|
|
|
if (sig) execute_signal(sig);
|
1995-12-21 03:01:45 -08:00
|
|
|
}
|
|
|
|
|
1995-12-22 08:48:37 -08:00
|
|
|
/* Trigger a garbage collection as soon as possible */
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void urge_major_slice (void)
|
1995-12-22 08:48:37 -08:00
|
|
|
{
|
|
|
|
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. */
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void enter_blocking_section(void)
|
1995-07-10 02:48:27 -07:00
|
|
|
{
|
1995-12-21 03:01:45 -08:00
|
|
|
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;
|
1997-11-25 04:38:09 -08:00
|
|
|
young_limit = young_start;
|
1995-12-21 03:01:45 -08:00
|
|
|
if (sig) execute_signal(sig);
|
|
|
|
async_signal_mode = 1;
|
|
|
|
if (!pending_signal) break;
|
|
|
|
async_signal_mode = 0;
|
|
|
|
}
|
1997-08-29 08:37:22 -07:00
|
|
|
if (enter_blocking_section_hook != NULL) enter_blocking_section_hook();
|
1995-07-10 02:48:27 -07:00
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void leave_blocking_section(void)
|
1995-07-10 02:48:27 -07:00
|
|
|
{
|
1997-08-29 08:37:22 -07:00
|
|
|
if (leave_blocking_section_hook != NULL) leave_blocking_section_hook();
|
1997-11-20 06:14:21 -08:00
|
|
|
Assert(async_signal_mode);
|
1995-12-21 03:01:45 -08:00
|
|
|
async_signal_mode = 0;
|
|
|
|
}
|
|
|
|
|
1996-10-08 02:31:43 -07:00
|
|
|
#if defined(TARGET_alpha) || defined(TARGET_mips) || \
|
|
|
|
(defined(TARGET_power) && defined(_AIX))
|
1997-09-02 05:55:01 -07:00
|
|
|
void handle_signal(int sig, int code, struct sigcontext * context)
|
1996-10-08 02:31:43 -07:00
|
|
|
#elif defined(TARGET_power) && defined(__linux)
|
1997-09-02 05:55:01 -07:00
|
|
|
void handle_signal(int sig, sutrct pt_regs * context)
|
1995-12-21 05:19:42 -08:00
|
|
|
#else
|
1997-09-02 05:55:01 -07:00
|
|
|
void handle_signal(int sig)
|
1995-12-21 03:01:45 -08:00
|
|
|
#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
|
1997-03-17 02:17:32 -08:00
|
|
|
we are inside C code (i.e. caml_last_return_address != 0). */
|
1995-12-21 03:01:45 -08:00
|
|
|
#ifdef TARGET_alpha
|
|
|
|
/* Cached in register $14 */
|
1997-03-17 02:17:32 -08:00
|
|
|
if (caml_last_return_address == 0)
|
1995-12-21 03:01:45 -08:00
|
|
|
context->sc_regs[14] = (long) young_limit;
|
1995-12-21 06:21:11 -08:00
|
|
|
#endif
|
|
|
|
#ifdef TARGET_mips
|
1995-12-22 08:48:37 -08:00
|
|
|
/* Cached in register $23 */
|
1997-03-17 02:17:32 -08:00
|
|
|
if (caml_last_return_address == 0)
|
1995-12-22 08:48:37 -08:00
|
|
|
context->sc_regs[23] = (int) young_limit;
|
1996-01-06 10:56:39 -08:00
|
|
|
#endif
|
|
|
|
#ifdef TARGET_power
|
|
|
|
/* Cached in register 31 */
|
|
|
|
#ifdef _AIX
|
1997-03-17 02:17:32 -08:00
|
|
|
if (caml_last_return_address == 0)
|
1996-01-06 10:56:39 -08:00
|
|
|
context->sc_jmpbuf.jmp_context.gpr[31] = (ulong_t) young_limit;
|
|
|
|
#endif
|
1996-07-12 10:53:01 -07:00
|
|
|
#ifdef __linux
|
1997-03-17 02:17:32 -08:00
|
|
|
if (caml_last_return_address == 0)
|
1996-07-12 10:53:01 -07:00
|
|
|
context->gpr[31] = (unsigned long) young_limit;
|
|
|
|
#endif
|
1995-12-21 03:01:45 -08:00
|
|
|
#endif
|
|
|
|
}
|
1995-07-10 02:48:27 -07:00
|
|
|
}
|
|
|
|
|
1995-12-21 03:01:45 -08:00
|
|
|
#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
|
1996-04-18 09:27:16 -07:00
|
|
|
#ifndef SIGPROF
|
|
|
|
#define SIGPROF -1
|
|
|
|
#endif
|
1995-12-21 03:01:45 -08:00
|
|
|
|
|
|
|
int posix_signals[] = {
|
|
|
|
SIGABRT, SIGALRM, SIGFPE, SIGHUP, SIGILL, SIGINT, SIGKILL, SIGPIPE,
|
|
|
|
SIGQUIT, SIGSEGV, SIGTERM, SIGUSR1, SIGUSR2, SIGCHLD, SIGCONT,
|
1996-04-18 09:27:16 -07:00
|
|
|
SIGSTOP, SIGTSTP, SIGTTIN, SIGTTOU, SIGVTALRM, SIGPROF
|
1995-12-21 03:01:45 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
#ifndef NSIG
|
|
|
|
#define NSIG 32
|
|
|
|
#endif
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
value install_signal_handler(value signal_number, value action) /* ML */
|
|
|
|
|
1995-07-10 02:48:27 -07:00
|
|
|
{
|
1995-12-21 03:01:45 -08:00
|
|
|
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 */
|
1995-12-21 04:29:49 -08:00
|
|
|
if (signal_handlers == 0) {
|
|
|
|
int i;
|
1997-05-26 10:16:31 -07:00
|
|
|
Begin_root (action);
|
|
|
|
signal_handlers = alloc_tuple(NSIG);
|
|
|
|
End_roots();
|
1995-12-21 04:29:49 -08:00
|
|
|
for (i = 0; i < NSIG; i++) Field(signal_handlers, i) = Val_int(0);
|
|
|
|
register_global_root(&signal_handlers);
|
|
|
|
}
|
1995-12-21 03:01:45 -08:00
|
|
|
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
|
1995-07-10 02:48:27 -07:00
|
|
|
return Val_unit;
|
|
|
|
}
|
1995-12-05 05:07:49 -08:00
|
|
|
|
1995-12-21 03:01:45 -08:00
|
|
|
/* Machine- and OS-dependent handling of bound check trap */
|
1995-12-05 05:07:49 -08:00
|
|
|
|
1995-12-21 03:01:45 -08:00
|
|
|
#if defined(TARGET_sparc) && defined(SYS_sunos)
|
1997-09-02 05:55:01 -07:00
|
|
|
static void trap_handler(int sig, int code,
|
|
|
|
struct sigcontext * context, char * address)
|
1995-12-05 05:07:49 -08:00
|
|
|
{
|
1996-10-09 06:36:48 -07:00
|
|
|
if (code == ILL_TRAP_FAULT(5)) {
|
1995-12-05 05:07:49 -08:00
|
|
|
array_bound_error();
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Fatal error: illegal instruction, code 0x%x\n", code);
|
|
|
|
exit(100);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1995-12-21 03:01:45 -08:00
|
|
|
#if defined(TARGET_sparc) && defined(SYS_solaris)
|
1997-11-17 05:24:57 -08:00
|
|
|
static void trap_handler(int sig, siginfo_t * info, void * context)
|
1995-12-05 05:07:49 -08:00
|
|
|
{
|
1996-10-09 06:36:48 -07:00
|
|
|
if (info->si_code == ILL_ILLTRP) {
|
1995-12-05 05:07:49 -08:00
|
|
|
array_bound_error();
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Fatal error: illegal instruction, code 0x%x\n",
|
|
|
|
info->si_code);
|
|
|
|
exit(100);
|
|
|
|
}
|
|
|
|
}
|
1995-12-21 03:01:45 -08:00
|
|
|
#endif
|
|
|
|
|
1996-04-18 09:27:16 -07:00
|
|
|
#if defined(TARGET_sparc) && defined(SYS_bsd)
|
1997-09-02 05:55:01 -07:00
|
|
|
static void trap_handler(int sig)
|
1996-04-18 09:27:16 -07:00
|
|
|
{
|
|
|
|
array_bound_error();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1996-01-07 08:58:44 -08:00
|
|
|
#if defined(TARGET_power)
|
1997-09-02 05:55:01 -07:00
|
|
|
static void trap_handler(int sig)
|
1996-01-07 08:58:44 -08:00
|
|
|
{
|
|
|
|
array_bound_error();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1995-12-21 03:01:45 -08:00
|
|
|
/* Initialization of signal stuff */
|
1995-12-05 05:07:49 -08:00
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void init_signals(void)
|
1995-12-05 05:07:49 -08:00
|
|
|
{
|
1996-04-18 09:27:16 -07:00
|
|
|
#if defined(TARGET_sparc) && (defined(SYS_sunos) || defined(SYS_bsd))
|
1995-12-21 03:01:45 -08:00
|
|
|
signal(SIGILL, trap_handler);
|
|
|
|
#endif
|
|
|
|
#if defined(TARGET_sparc) && defined(SYS_solaris)
|
1995-12-05 05:07:49 -08:00
|
|
|
struct sigaction act;
|
|
|
|
act.sa_sigaction = trap_handler;
|
|
|
|
sigemptyset(&act.sa_mask);
|
|
|
|
act.sa_flags = SA_SIGINFO;
|
|
|
|
sigaction(SIGILL, &act, NULL);
|
|
|
|
#endif
|
1996-01-07 08:58:44 -08:00
|
|
|
#if defined(TARGET_power)
|
|
|
|
signal(SIGTRAP, trap_handler);
|
|
|
|
#endif
|
1995-12-05 05:07:49 -08:00
|
|
|
}
|
|
|
|
|