Newer
Older
/* $Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/lisp/interrupt.c,v 1.4 1994/07/05 16:08:03 hallgren Exp $ */
/* Interrupt handing magic. */
#include <stdio.h>
#include <signal.h>
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
#include "lisp.h"
#include "internals.h"
#include "os.h"
#include "arch.h"
#include "globals.h"
#include "lispregs.h"
#include "interrupt.h"
#include "validate.h"
#include "monitor.h"
#include "gc.h"
#include "alloc.h"
#include "dynbind.h"
#include "interr.h"
boolean internal_errors_enabled = 0;
struct sigcontext *lisp_interrupt_contexts[MAX_INTERRUPTS];
union interrupt_handler interrupt_handlers[NSIG];
void (*interrupt_low_level_handlers[NSIG])(int signal, int code,
struct sigcontext *scp) = {0};
static int pending_signal = 0, pending_code = 0, pending_mask = 0;
static boolean maybe_gc_pending = FALSE;
/****************************************************************\
* Utility routines used by various signal handlers. *
\****************************************************************/
void fake_foreign_function_call(struct sigcontext *context)
{
int context_index;
lispobj oldcont;
/* Get current LISP state from context */
#ifdef reg_ALLOC
current_dynamic_space_free_pointer = (lispobj *)SC_REG(context, reg_ALLOC);
#ifdef alpha
if((long) current_dynamic_space_free_pointer & 1) {
printf("Dead in fake_foriegn_function-call, context = %x\n",context);
lose("");
}
#endif
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
#endif
#ifdef reg_BSP
current_binding_stack_pointer = (lispobj *)SC_REG(context, reg_BSP);
#endif
#ifndef i386
/* Build a fake stack frame */
current_control_frame_pointer = (lispobj *)SC_REG(context, reg_CSP);
if ((lispobj *)SC_REG(context, reg_CFP)==current_control_frame_pointer) {
/* There is a small window during call where the callee's frame */
/* isn't built yet. */
if (LowtagOf(SC_REG(context, reg_CODE)) == type_FunctionPointer) {
/* We have called, but not built the new frame, so
build it for them. */
current_control_frame_pointer[0] = SC_REG(context, reg_OCFP);
current_control_frame_pointer[1] = SC_REG(context, reg_LRA);
current_control_frame_pointer += 8;
/* Build our frame on top of it. */
oldcont = (lispobj)SC_REG(context, reg_CFP);
}
else {
/* We haven't yet called, build our frame as if the
partial frame wasn't there. */
oldcont = (lispobj)SC_REG(context, reg_OCFP);
}
}
/* ### We can't tell if we are still in the caller if it had to
reg_ALLOCate the stack frame due to stack arguments. */
/* ### Can anything strange happen during return? */
else
/* Normal case. */
oldcont = (lispobj)SC_REG(context, reg_CFP);
current_control_stack_pointer = current_control_frame_pointer + 8;
current_control_frame_pointer[0] = oldcont;
current_control_frame_pointer[1] = NIL;
current_control_frame_pointer[2] = (lispobj)SC_REG(context, reg_CODE);
#endif
/* Do dynamic binding of the active interrupt context index
and save the context in the context array. */
context_index = SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX)>>2;
if (context_index >= MAX_INTERRUPTS) {
fprintf(stderr,
"Maximum number (%d) of interrupts exceeded. Exiting.\n",
MAX_INTERRUPTS);
exit(1);
}
bind_variable(FREE_INTERRUPT_CONTEXT_INDEX,
make_fixnum(context_index + 1));
lisp_interrupt_contexts[context_index] = context;
/* No longer in Lisp now. */
foreign_function_call_active = 1;
}
void undo_fake_foreign_function_call(struct sigcontext *context)
{
/* Block all blockable signals */
sigblock(BLOCKABLE);
/* Going back into lisp. */
foreign_function_call_active = 0;
/* Undo dynamic binding. */
/* ### Do I really need to unbind_to_here()? */
unbind();
#ifdef reg_ALLOC
/* Put the dynamic space free pointer back into the context. */
SC_REG(context, reg_ALLOC) =
(unsigned long) current_dynamic_space_free_pointer;
#endif
}
void interrupt_internal_error(int signal, int code, struct sigcontext *context,
boolean continuable)
{
sigsetmask(context->sc_mask);
fake_foreign_function_call(context);
if (internal_errors_enabled)
funcall2(SymbolFunction(INTERNAL_ERROR), alloc_sap(context),
continuable ? T : NIL);
else
internal_error(context);
undo_fake_foreign_function_call(context);
if (continuable)
arch_skip_instruction(context);
}
void interrupt_handle_pending(struct sigcontext *context)
{
boolean were_in_lisp = !foreign_function_call_active;
SetSymbolValue(INTERRUPT_PENDING, NIL);
if (maybe_gc_pending) {
maybe_gc_pending = FALSE;
if (were_in_lisp)
fake_foreign_function_call(context);
funcall0(SymbolFunction(MAYBE_GC));
if (were_in_lisp)
undo_fake_foreign_function_call(context);
}
if (pending_signal) {
int signal, code;
signal = pending_signal;
code = pending_code;
pending_signal = 0;
pending_code = 0;
interrupt_handle_now(signal, code, context);
}
context->sc_mask = pending_mask;
pending_mask = 0;
}
/****************************************************************\
* interrupt_handle_now, maybe_now_maybe_later *
* the two main signal handlers. *
\****************************************************************/
void interrupt_handle_now(int signal, int code, struct sigcontext *context)
{
int were_in_lisp;
union interrupt_handler handler;
handler = interrupt_handlers[signal];
if(handler.c==SIG_IGN)
return;
were_in_lisp = !foreign_function_call_active;
if (were_in_lisp)
fake_foreign_function_call(context);
/* Allow signals again. */
sigsetmask(context->sc_mask);
if (handler.c==SIG_DFL)
/* This can happen if someone tries to ignore or default on of the */
/* signals we need for runtime support, and the runtime support */
/* decides to pass on it. */
lose("interrupt_handle_now: No handler for signal %d?\n", signal);
else if (LowtagOf(handler.lisp) == type_FunctionPointer)
funcall3(handler.lisp, make_fixnum(signal), make_fixnum(code),
alloc_sap(context));
else
(*handler.c)(signal, code, context);
if (were_in_lisp)
undo_fake_foreign_function_call(context);
}
static void maybe_now_maybe_later(int signal, int code,
struct sigcontext *context)
{
if (SymbolValue(INTERRUPTS_ENABLED) == NIL) {
pending_signal = signal;
pending_code = code;
pending_mask = context->sc_mask;
context->sc_mask |= BLOCKABLE;
SetSymbolValue(INTERRUPT_PENDING, T);
} else if ((!foreign_function_call_active)
&& arch_pseudo_atomic_atomic(context)) {
pending_signal = signal;
pending_code = code;
pending_mask = context->sc_mask;
context->sc_mask |= BLOCKABLE;
arch_set_pseudo_atomic_interrupted(context);
} else
interrupt_handle_now(signal, code, context);
}
/****************************************************************\
* Stuff to detect and handle hitting the gc trigger. *
\****************************************************************/
#ifndef INTERNAL_GC_TRIGGER
static boolean gc_trigger_hit(int signal, int code, struct sigcontext *context)
{
if (current_auto_gc_trigger == NULL)
return FALSE;
else{
lispobj *badaddr=(lispobj *)arch_get_bad_addr(signal, code, context);
return (badaddr >= current_auto_gc_trigger &&
badaddr < current_dynamic_space + DYNAMIC_SPACE_SIZE);
}
}
#endif
boolean interrupt_maybe_gc(int signal, int code, struct sigcontext *context)
{
if (!foreign_function_call_active
#ifndef INTERNAL_GC_TRIGGER
&& gc_trigger_hit(signal, code, context)
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#endif
) {
#ifndef INTERNAL_GC_TRIGGER
clear_auto_gc_trigger();
#endif
if (arch_pseudo_atomic_atomic(context)) {
maybe_gc_pending = TRUE;
if (pending_signal == 0) {
pending_mask = context->sc_mask;
context->sc_mask |= BLOCKABLE;
}
arch_set_pseudo_atomic_interrupted(context);
}
else {
fake_foreign_function_call(context);
funcall0(SymbolFunction(MAYBE_GC));
undo_fake_foreign_function_call(context);
}
return TRUE;
}else
return FALSE;
}
/****************************************************************\
* Noise to install handlers. *
\****************************************************************/
void interrupt_install_low_level_handler
(int signal,
void handler(int signal, int code, struct sigcontext *handler))
{
struct sigvec sv;
sv.sv_handler=handler;
sv.sv_mask=BLOCKABLE;
sv.sv_flags=0;
sigvec(signal,&sv,NULL);
interrupt_low_level_handlers[signal]=(handler==SIG_DFL ? 0 : handler);
}
unsigned long install_handler(int signal,
void handler(int signal, int code,
struct sigcontext *handler))
{
struct sigvec sv;
int oldmask;
union interrupt_handler oldhandler;
oldmask = sigblock(sigmask(signal));
if(interrupt_low_level_handlers[signal]==0){
if(handler==SIG_DFL || handler==SIG_IGN)
sv.sv_handler = handler;
else if (sigmask(signal)&BLOCKABLE)
sv.sv_handler = maybe_now_maybe_later;
else
sv.sv_handler = interrupt_handle_now;
sv.sv_mask = BLOCKABLE;
sv.sv_flags = 0;
sigvec(signal, &sv, NULL);
}
oldhandler = interrupt_handlers[signal];
interrupt_handlers[signal].c = handler;
sigsetmask(oldmask);
return (unsigned long)oldhandler.lisp;
}
void interrupt_init(void)
{
int i;
for (i = 0; i < NSIG; i++)
interrupt_handlers[i].c = SIG_DFL;
}