11e8f19b3f300f0cd702b30f31759eb4dbe01b7e
[sbcl.git] / src / runtime / interrupt.c
1 /*
2  * interrupt-handling magic
3  */
4
5 /*
6  * This software is part of the SBCL system. See the README file for
7  * more information.
8  *
9  * This software is derived from the CMU CL system, which was
10  * written at Carnegie Mellon University and released into the
11  * public domain. The software is in the public domain and is
12  * provided with absolutely no warranty. See the COPYING and CREDITS
13  * files for more information.
14  */
15
16
17 /* As far as I can tell, what's going on here is:
18  *
19  * In the case of most signals, when Lisp asks us to handle the
20  * signal, the outermost handler (the one actually passed to UNIX) is
21  * either interrupt_handle_now(..) or maybe_now_maybe_later(..).
22  * In that case, the Lisp-level handler is stored in interrupt_handlers[..]
23  * and interrupt_low_level_handlers[..] is cleared.
24  *
25  * However, some signals need special handling, e.g.
26  *
27  * o the SIGSEGV (for e.g. Linux) or SIGBUS (for e.g. FreeBSD) used by the
28  *   garbage collector to detect violations of write protection,
29  *   because some cases of such signals (e.g. GC-related violations of
30  *   write protection) are handled at C level and never passed on to
31  *   Lisp. For such signals, we still store any Lisp-level handler
32  *   in interrupt_handlers[..], but for the outermost handle we use
33  *   the value from interrupt_low_level_handlers[..], instead of the
34  *   ordinary interrupt_handle_now(..) or interrupt_handle_later(..).
35  *
36  * o the SIGTRAP (Linux/Alpha) which Lisp code uses to handle breakpoints,
37  *   pseudo-atomic sections, and some classes of error (e.g. "function
38  *   not defined").  This never goes anywhere near the Lisp handlers at all.
39  *   See runtime/alpha-arch.c and code/signal.lisp
40  *
41  * - WHN 20000728, dan 20010128 */
42
43 #include "sbcl.h"
44
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <signal.h>
49 #include <sys/types.h>
50 #ifndef LISP_FEATURE_WIN32
51 #include <sys/wait.h>
52 #endif
53 #include <errno.h>
54
55 #include "runtime.h"
56 #include "arch.h"
57 #include "os.h"
58 #include "interrupt.h"
59 #include "globals.h"
60 #include "lispregs.h"
61 #include "validate.h"
62 #include "gc.h"
63 #include "alloc.h"
64 #include "dynbind.h"
65 #include "interr.h"
66 #include "pseudo-atomic.h"
67 #include "genesis/fdefn.h"
68 #include "genesis/simple-fun.h"
69 #include "genesis/cons.h"
70
71 /* Under Linux on some architectures, we appear to have to restore the
72  * FPU control word from the context, as after the signal is delivered
73  * we appear to have a null FPU control word. */
74 #if defined(RESTORE_FP_CONTROL_FROM_CONTEXT)
75 #define RESTORE_FP_CONTROL_WORD(context,void_context)           \
76     os_context_t *context = arch_os_get_context(&void_context); \
77     os_restore_fp_control(context);
78 #else
79 #define RESTORE_FP_CONTROL_WORD(context,void_context)           \
80     os_context_t *context = arch_os_get_context(&void_context);
81 #endif
82
83 /* These are to be used in signal handlers. Currently all handlers are
84  * called from one of:
85  *
86  * interrupt_handle_now_handler
87  * maybe_now_maybe_later
88  * unblock_me_trampoline
89  * low_level_handle_now_handler
90  * low_level_maybe_now_maybe_later
91  * low_level_unblock_me_trampoline
92  */
93 #define SAVE_ERRNO(context,void_context)                        \
94     {                                                           \
95         int _saved_errno = errno;                               \
96         RESTORE_FP_CONTROL_WORD(context,void_context);          \
97         {
98
99 #define RESTORE_ERRNO                                           \
100         }                                                       \
101         errno = _saved_errno;                                   \
102     }
103
104 static void run_deferred_handler(struct interrupt_data *data, void *v_context);
105 #ifndef LISP_FEATURE_WIN32
106 static void store_signal_data_for_later (struct interrupt_data *data,
107                                          void *handler, int signal,
108                                          siginfo_t *info,
109                                          os_context_t *context);
110
111 static void
112 fill_current_sigmask(sigset_t *sigset)
113 {
114     /* Get the current sigmask, by blocking the empty set. */
115     sigset_t empty;
116     sigemptyset(&empty);
117     thread_sigmask(SIG_BLOCK, &empty, sigset);
118 }
119
120 void
121 sigaddset_deferrable(sigset_t *s)
122 {
123     sigaddset(s, SIGHUP);
124     sigaddset(s, SIGINT);
125     sigaddset(s, SIGTERM);
126     sigaddset(s, SIGQUIT);
127     sigaddset(s, SIGPIPE);
128     sigaddset(s, SIGALRM);
129     sigaddset(s, SIGURG);
130     sigaddset(s, SIGTSTP);
131     sigaddset(s, SIGCHLD);
132     sigaddset(s, SIGIO);
133 #ifndef LISP_FEATURE_HPUX
134     sigaddset(s, SIGXCPU);
135     sigaddset(s, SIGXFSZ);
136 #endif
137     sigaddset(s, SIGVTALRM);
138     sigaddset(s, SIGPROF);
139     sigaddset(s, SIGWINCH);
140 }
141
142 void
143 sigdelset_deferrable(sigset_t *s)
144 {
145     sigdelset(s, SIGHUP);
146     sigdelset(s, SIGINT);
147     sigdelset(s, SIGQUIT);
148     sigdelset(s, SIGPIPE);
149     sigdelset(s, SIGALRM);
150     sigdelset(s, SIGURG);
151     sigdelset(s, SIGTSTP);
152     sigdelset(s, SIGCHLD);
153     sigdelset(s, SIGIO);
154 #ifndef LISP_FEATURE_HPUX
155     sigdelset(s, SIGXCPU);
156     sigdelset(s, SIGXFSZ);
157 #endif
158     sigdelset(s, SIGVTALRM);
159     sigdelset(s, SIGPROF);
160     sigdelset(s, SIGWINCH);
161 }
162
163 void
164 sigaddset_blockable(sigset_t *sigset)
165 {
166     sigaddset_deferrable(sigset);
167     sigaddset_gc(sigset);
168 }
169
170 void
171 sigaddset_gc(sigset_t *sigset)
172 {
173 #ifdef LISP_FEATURE_SB_THREAD
174     sigaddset(sigset,SIG_STOP_FOR_GC);
175 #endif
176 }
177
178 void
179 sigdelset_gc(sigset_t *sigset)
180 {
181 #ifdef LISP_FEATURE_SB_THREAD
182     sigdelset(sigset,SIG_STOP_FOR_GC);
183 #endif
184 }
185
186 /* initialized in interrupt_init */
187 sigset_t deferrable_sigset;
188 sigset_t blockable_sigset;
189 sigset_t gc_sigset;
190 #endif
191
192 boolean
193 deferrables_blocked_in_sigset_p(sigset_t *sigset)
194 {
195 #if !defined(LISP_FEATURE_WIN32)
196     int i;
197     for(i = 1; i < NSIG; i++) {
198         if (sigismember(&deferrable_sigset, i) && sigismember(sigset, i))
199             return 1;
200     }
201 #endif
202     return 0;
203 }
204
205 void
206 check_deferrables_unblocked_in_sigset_or_lose(sigset_t *sigset)
207 {
208 #if !defined(LISP_FEATURE_WIN32)
209     int i;
210     for(i = 1; i < NSIG; i++) {
211         if (sigismember(&deferrable_sigset, i) && sigismember(sigset, i))
212             lose("deferrable signal %d blocked\n",i);
213     }
214 #endif
215 }
216
217 void
218 check_deferrables_blocked_in_sigset_or_lose(sigset_t *sigset)
219 {
220 #if !defined(LISP_FEATURE_WIN32)
221     int i;
222     for(i = 1; i < NSIG; i++) {
223         if (sigismember(&deferrable_sigset, i) && !sigismember(sigset, i))
224             lose("deferrable signal %d not blocked\n",i);
225     }
226 #endif
227 }
228
229 void
230 check_deferrables_unblocked_or_lose(void)
231 {
232 #if !defined(LISP_FEATURE_WIN32)
233     sigset_t current;
234     fill_current_sigmask(&current);
235     check_deferrables_unblocked_in_sigset_or_lose(&current);
236 #endif
237 }
238
239 void
240 check_deferrables_blocked_or_lose(void)
241 {
242 #if !defined(LISP_FEATURE_WIN32)
243     sigset_t current;
244     fill_current_sigmask(&current);
245     check_deferrables_blocked_in_sigset_or_lose(&current);
246 #endif
247 }
248
249 void
250 check_blockables_blocked_or_lose(void)
251 {
252 #if !defined(LISP_FEATURE_WIN32)
253     sigset_t current;
254     int i;
255     fill_current_sigmask(&current);
256     for(i = 1; i < NSIG; i++) {
257         if (sigismember(&blockable_sigset, i) && !sigismember(&current, i))
258             lose("blockable signal %d not blocked\n",i);
259     }
260 #endif
261 }
262
263 void
264 check_gc_signals_unblocked_in_sigset_or_lose(sigset_t *sigset)
265 {
266 #if !defined(LISP_FEATURE_WIN32)
267     int i;
268     for(i = 1; i < NSIG; i++) {
269         if (sigismember(&gc_sigset, i) && sigismember(sigset, i))
270             lose("gc signal %d blocked\n",i);
271     }
272 #endif
273 }
274
275 void
276 check_gc_signals_unblocked_or_lose(void)
277 {
278 #if !defined(LISP_FEATURE_WIN32)
279     sigset_t current;
280     fill_current_sigmask(&current);
281     check_gc_signals_unblocked_in_sigset_or_lose(&current);
282 #endif
283 }
284
285 inline static void
286 check_interrupts_enabled_or_lose(os_context_t *context)
287 {
288     struct thread *thread=arch_os_get_current_thread();
289     if (SymbolValue(INTERRUPTS_ENABLED,thread) == NIL)
290         lose("interrupts not enabled\n");
291     if (arch_pseudo_atomic_atomic(context))
292         lose ("in pseudo atomic section\n");
293 }
294
295 /* Save sigset (or the current sigmask if 0) if there is no pending
296  * handler, because that means that deferabbles are already blocked.
297  * The purpose is to avoid losing the pending gc signal if a
298  * deferrable interrupt async unwinds between clearing the pseudo
299  * atomic and trapping to GC.*/
300 void
301 maybe_save_gc_mask_and_block_deferrables(sigset_t *sigset)
302 {
303     struct thread *thread = arch_os_get_current_thread();
304     struct interrupt_data *data = thread->interrupt_data;
305     sigset_t oldset;
306     /* Obviously, this function is called when signals may not be
307      * blocked. Let's make sure we are not interrupted. */
308     thread_sigmask(SIG_BLOCK, &blockable_sigset, &oldset);
309 #ifndef LISP_FEATURE_SB_THREAD
310     /* With threads a SIG_STOP_FOR_GC and a normal GC may also want to
311      * block. */
312     if (data->gc_blocked_deferrables)
313         lose("gc_blocked_deferrables already true\n");
314 #endif
315     if ((!data->pending_handler) &&
316         (!data->gc_blocked_deferrables)) {
317         FSHOW_SIGNAL((stderr,"/setting gc_blocked_deferrables\n"));
318         data->gc_blocked_deferrables = 1;
319         if (sigset) {
320             /* This is the sigmask of some context. */
321             sigcopyset(&data->pending_mask, sigset);
322             sigaddset_deferrable(sigset);
323             thread_sigmask(SIG_SETMASK,&oldset,0);
324             return;
325         } else {
326             /* Operating on the current sigmask. Save oldset and
327              * unblock gc signals. In the end, this is equivalent to
328              * blocking the deferrables. */
329             sigcopyset(&data->pending_mask, &oldset);
330             unblock_gc_signals();
331             return;
332         }
333     }
334     thread_sigmask(SIG_SETMASK,&oldset,0);
335 }
336
337 /* Are we leaving WITH-GCING and already running with interrupts
338  * enabled, without the protection of *GC-INHIBIT* T and there is gc
339  * (or stop for gc) pending, but we haven't trapped yet? */
340 int
341 in_leaving_without_gcing_race_p(struct thread *thread)
342 {
343     return ((SymbolValue(IN_WITHOUT_GCING,thread) != NIL) &&
344             (SymbolValue(INTERRUPTS_ENABLED,thread) != NIL) &&
345             (SymbolValue(GC_INHIBIT,thread) == NIL) &&
346             ((SymbolValue(GC_PENDING,thread) != NIL)
347 #if defined(LISP_FEATURE_SB_THREAD)
348              || (SymbolValue(STOP_FOR_GC_PENDING,thread) != NIL)
349 #endif
350              ));
351 }
352
353 /* Check our baroque invariants. */
354 void
355 check_interrupt_context_or_lose(os_context_t *context)
356 {
357     struct thread *thread = arch_os_get_current_thread();
358     struct interrupt_data *data = thread->interrupt_data;
359     int interrupt_deferred_p = (data->pending_handler != 0);
360     int interrupt_pending = (SymbolValue(INTERRUPT_PENDING,thread) != NIL);
361     sigset_t *sigset = os_context_sigmask_addr(context);
362     /* On PPC pseudo_atomic_interrupted is cleared when coming out of
363      * handle_allocation_trap. */
364 #if defined(LISP_FEATURE_GENCGC) && !defined(LISP_FEATURE_PPC)
365     int interrupts_enabled = (SymbolValue(INTERRUPTS_ENABLED,thread) != NIL);
366     int gc_inhibit = (SymbolValue(GC_INHIBIT,thread) != NIL);
367     int gc_pending = (SymbolValue(GC_PENDING,thread) == T);
368     int pseudo_atomic_interrupted = get_pseudo_atomic_interrupted(thread);
369     int in_race_p = in_leaving_without_gcing_race_p(thread);
370     /* In the time window between leaving the *INTERRUPTS-ENABLED* NIL
371      * section and trapping, a SIG_STOP_FOR_GC would see the next
372      * check fail, for this reason sig_stop_for_gc handler does not
373      * call this function. */
374     if (interrupt_deferred_p) {
375         if (!(!interrupts_enabled || pseudo_atomic_interrupted || in_race_p))
376             lose("Stray deferred interrupt.\n");
377     }
378     if (gc_pending)
379         if (!(pseudo_atomic_interrupted || gc_inhibit || in_race_p))
380             lose("GC_PENDING, but why?\n");
381 #if defined(LISP_FEATURE_SB_THREAD)
382     {
383         int stop_for_gc_pending =
384             (SymbolValue(STOP_FOR_GC_PENDING,thread) != NIL);
385         if (stop_for_gc_pending)
386             if (!(pseudo_atomic_interrupted || gc_inhibit || in_race_p))
387                 lose("STOP_FOR_GC_PENDING, but why?\n");
388     }
389 #endif
390 #endif
391     if (interrupt_pending && !interrupt_deferred_p)
392         lose("INTERRUPT_PENDING but not pending handler.\n");
393     if ((data->gc_blocked_deferrables) && interrupt_pending)
394         lose("gc_blocked_deferrables and interrupt pending\n.");
395     if (data->gc_blocked_deferrables)
396         check_deferrables_blocked_in_sigset_or_lose(sigset);
397     if (interrupt_pending || interrupt_deferred_p)
398         check_deferrables_blocked_in_sigset_or_lose(sigset);
399     else {
400         check_deferrables_unblocked_in_sigset_or_lose(sigset);
401         /* If deferrables are unblocked then we are open to signals
402          * that run lisp code. */
403         check_gc_signals_unblocked_in_sigset_or_lose(sigset);
404     }
405 }
406
407 /* When we catch an internal error, should we pass it back to Lisp to
408  * be handled in a high-level way? (Early in cold init, the answer is
409  * 'no', because Lisp is still too brain-dead to handle anything.
410  * After sufficient initialization has been completed, the answer
411  * becomes 'yes'.) */
412 boolean internal_errors_enabled = 0;
413
414 #ifndef LISP_FEATURE_WIN32
415 static void (*interrupt_low_level_handlers[NSIG]) (int, siginfo_t*, void*);
416 #endif
417 union interrupt_handler interrupt_handlers[NSIG];
418
419 void
420 block_blockable_signals(void)
421 {
422 #ifndef LISP_FEATURE_WIN32
423     thread_sigmask(SIG_BLOCK, &blockable_sigset, 0);
424 #endif
425 }
426
427 void
428 block_deferrable_signals(void)
429 {
430 #ifndef LISP_FEATURE_WIN32
431     thread_sigmask(SIG_BLOCK, &deferrable_sigset, 0);
432 #endif
433 }
434
435 void
436 unblock_deferrable_signals_in_sigset(sigset_t *sigset)
437 {
438 #ifndef LISP_FEATURE_WIN32
439     if (interrupt_handler_pending_p())
440         lose("unblock_deferrable_signals_in_sigset: losing proposition\n");
441     check_gc_signals_unblocked_in_sigset_or_lose(sigset);
442     sigdelset_deferrable(sigset);
443 #endif
444 }
445
446 void
447 unblock_deferrable_signals(void)
448 {
449 #ifndef LISP_FEATURE_WIN32
450     if (interrupt_handler_pending_p())
451         lose("unblock_deferrable_signals: losing proposition\n");
452     check_gc_signals_unblocked_or_lose();
453     thread_sigmask(SIG_UNBLOCK, &deferrable_sigset, 0);
454 #endif
455 }
456
457 void
458 unblock_gc_signals(void)
459 {
460 #if defined(LISP_FEATURE_SB_THREAD) && !defined(LISP_FEATURE_WIN32)
461     thread_sigmask(SIG_UNBLOCK,&gc_sigset,0);
462 #endif
463 }
464
465 void
466 unblock_signals_in_context_and_maybe_warn(os_context_t *context)
467 {
468 #ifndef LISP_FEATURE_WIN32
469     int i, oops=0;
470     sigset_t *sigset=os_context_sigmask_addr(context);
471     for(i = 1; i < NSIG; i++) {
472         if (sigismember(&gc_sigset, i) && sigismember(sigset, i)) {
473             if (!oops) {
474                 fprintf(stderr,
475 "Enabling blocked gc signals to allow returning to Lisp without risking\n\
476 gc deadlocks. Since GC signals are only blocked in signal handlers when \n\
477 they are not safe to interrupt at all, this is a pretty severe occurrence.\n");
478             }
479             oops=1;
480         }
481     }
482     sigdelset_gc(sigset);
483     if (!interrupt_handler_pending_p()) {
484         unblock_deferrable_signals_in_sigset(sigset);
485     }
486 #endif
487 }
488
489 \f
490 /*
491  * utility routines used by various signal handlers
492  */
493
494 static void
495 build_fake_control_stack_frames(struct thread *th,os_context_t *context)
496 {
497 #ifndef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
498
499     lispobj oldcont;
500
501     /* Build a fake stack frame or frames */
502
503     current_control_frame_pointer =
504         (lispobj *)(unsigned long)
505             (*os_context_register_addr(context, reg_CSP));
506     if ((lispobj *)(unsigned long)
507             (*os_context_register_addr(context, reg_CFP))
508         == current_control_frame_pointer) {
509         /* There is a small window during call where the callee's
510          * frame isn't built yet. */
511         if (lowtag_of(*os_context_register_addr(context, reg_CODE))
512             == FUN_POINTER_LOWTAG) {
513             /* We have called, but not built the new frame, so
514              * build it for them. */
515             current_control_frame_pointer[0] =
516                 *os_context_register_addr(context, reg_OCFP);
517             current_control_frame_pointer[1] =
518                 *os_context_register_addr(context, reg_LRA);
519             current_control_frame_pointer += 8;
520             /* Build our frame on top of it. */
521             oldcont = (lispobj)(*os_context_register_addr(context, reg_CFP));
522         }
523         else {
524             /* We haven't yet called, build our frame as if the
525              * partial frame wasn't there. */
526             oldcont = (lispobj)(*os_context_register_addr(context, reg_OCFP));
527         }
528     }
529     /* We can't tell whether we are still in the caller if it had to
530      * allocate a stack frame due to stack arguments. */
531     /* This observation provoked some past CMUCL maintainer to ask
532      * "Can anything strange happen during return?" */
533     else {
534         /* normal case */
535         oldcont = (lispobj)(*os_context_register_addr(context, reg_CFP));
536     }
537
538     current_control_stack_pointer = current_control_frame_pointer + 8;
539
540     current_control_frame_pointer[0] = oldcont;
541     current_control_frame_pointer[1] = NIL;
542     current_control_frame_pointer[2] =
543         (lispobj)(*os_context_register_addr(context, reg_CODE));
544 #endif
545 }
546
547 /* Stores the context for gc to scavange and builds fake stack
548  * frames. */
549 void
550 fake_foreign_function_call(os_context_t *context)
551 {
552     int context_index;
553     struct thread *thread=arch_os_get_current_thread();
554
555     /* context_index incrementing must not be interrupted */
556     check_blockables_blocked_or_lose();
557
558     /* Get current Lisp state from context. */
559 #ifdef reg_ALLOC
560     dynamic_space_free_pointer =
561         (lispobj *)(unsigned long)
562             (*os_context_register_addr(context, reg_ALLOC));
563 /*     fprintf(stderr,"dynamic_space_free_pointer: %p\n", */
564 /*             dynamic_space_free_pointer); */
565 #if defined(LISP_FEATURE_ALPHA) || defined(LISP_FEATURE_MIPS)
566     if ((long)dynamic_space_free_pointer & 1) {
567         lose("dead in fake_foreign_function_call, context = %x\n", context);
568     }
569 #endif
570 /* why doesnt PPC and SPARC do something like this: */
571 #if defined(LISP_FEATURE_HPPA)
572     if ((long)dynamic_space_free_pointer & 4) {
573         lose("dead in fake_foreign_function_call, context = %x, d_s_f_p = %x\n", context, dynamic_space_free_pointer);
574     }
575 #endif
576 #endif
577 #ifdef reg_BSP
578     current_binding_stack_pointer =
579         (lispobj *)(unsigned long)
580             (*os_context_register_addr(context, reg_BSP));
581 #endif
582
583     build_fake_control_stack_frames(thread,context);
584
585     /* Do dynamic binding of the active interrupt context index
586      * and save the context in the context array. */
587     context_index =
588         fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,thread));
589
590     if (context_index >= MAX_INTERRUPTS) {
591         lose("maximum interrupt nesting depth (%d) exceeded\n", MAX_INTERRUPTS);
592     }
593
594     bind_variable(FREE_INTERRUPT_CONTEXT_INDEX,
595                   make_fixnum(context_index + 1),thread);
596
597     thread->interrupt_contexts[context_index] = context;
598
599 #ifdef FOREIGN_FUNCTION_CALL_FLAG
600     foreign_function_call_active = 1;
601 #endif
602 }
603
604 /* blocks all blockable signals.  If you are calling from a signal handler,
605  * the usual signal mask will be restored from the context when the handler
606  * finishes.  Otherwise, be careful */
607 void
608 undo_fake_foreign_function_call(os_context_t *context)
609 {
610     struct thread *thread=arch_os_get_current_thread();
611     /* Block all blockable signals. */
612     block_blockable_signals();
613
614 #ifdef FOREIGN_FUNCTION_CALL_FLAG
615     foreign_function_call_active = 0;
616 #endif
617
618     /* Undo dynamic binding of FREE_INTERRUPT_CONTEXT_INDEX */
619     unbind(thread);
620
621 #ifdef reg_ALLOC
622     /* Put the dynamic space free pointer back into the context. */
623     *os_context_register_addr(context, reg_ALLOC) =
624         (unsigned long) dynamic_space_free_pointer
625         | (*os_context_register_addr(context, reg_ALLOC)
626            & LOWTAG_MASK);
627     /*
628       ((unsigned long)(*os_context_register_addr(context, reg_ALLOC))
629       & ~LOWTAG_MASK)
630       | ((unsigned long) dynamic_space_free_pointer & LOWTAG_MASK);
631     */
632 #endif
633 }
634
635 /* a handler for the signal caused by execution of a trap opcode
636  * signalling an internal error */
637 void
638 interrupt_internal_error(os_context_t *context, boolean continuable)
639 {
640     lispobj context_sap;
641
642     fake_foreign_function_call(context);
643
644     if (!internal_errors_enabled) {
645         describe_internal_error(context);
646         /* There's no good way to recover from an internal error
647          * before the Lisp error handling mechanism is set up. */
648         lose("internal error too early in init, can't recover\n");
649     }
650
651     /* Allocate the SAP object while the interrupts are still
652      * disabled. */
653     unblock_gc_signals();
654     context_sap = alloc_sap(context);
655
656 #ifndef LISP_FEATURE_WIN32
657     thread_sigmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
658 #endif
659
660 #if defined(LISP_FEATURE_LINUX) && defined(LISP_FEATURE_MIPS)
661     /* Workaround for blocked SIGTRAP. */
662     {
663         sigset_t newset;
664         sigemptyset(&newset);
665         sigaddset(&newset, SIGTRAP);
666         thread_sigmask(SIG_UNBLOCK, &newset, 0);
667     }
668 #endif
669
670     SHOW("in interrupt_internal_error");
671 #ifdef QSHOW
672     /* Display some rudimentary debugging information about the
673      * error, so that even if the Lisp error handler gets badly
674      * confused, we have a chance to determine what's going on. */
675     describe_internal_error(context);
676 #endif
677     funcall2(StaticSymbolFunction(INTERNAL_ERROR), context_sap,
678              continuable ? T : NIL);
679
680     undo_fake_foreign_function_call(context); /* blocks signals again */
681     if (continuable)
682         arch_skip_instruction(context);
683 }
684
685 boolean
686 interrupt_handler_pending_p(void)
687 {
688     struct thread *thread = arch_os_get_current_thread();
689     struct interrupt_data *data = thread->interrupt_data;
690     return (data->pending_handler != 0);
691 }
692
693 void
694 interrupt_handle_pending(os_context_t *context)
695 {
696     /* There are three ways we can get here. First, if an interrupt
697      * occurs within pseudo-atomic, it will be deferred, and we'll
698      * trap to here at the end of the pseudo-atomic block. Second, if
699      * the GC (in alloc()) decides that a GC is required, it will set
700      * *GC-PENDING* and pseudo-atomic-interrupted if not *GC-INHIBIT*,
701      * and alloc() is always called from within pseudo-atomic, and
702      * thus we end up here again. Third, when calling GC-ON or at the
703      * end of a WITHOUT-GCING, MAYBE-HANDLE-PENDING-GC will trap to
704      * here if there is a pending GC. Fourth, ahem, at the end of
705      * WITHOUT-INTERRUPTS (bar complications with nesting). */
706
707     /* Win32 only needs to handle the GC cases (for now?) */
708
709     struct thread *thread = arch_os_get_current_thread();
710     struct interrupt_data *data = thread->interrupt_data;
711
712     if (arch_pseudo_atomic_atomic(context)) {
713         lose("Handling pending interrupt in pseduo atomic.");
714     }
715
716     FSHOW_SIGNAL((stderr, "/entering interrupt_handle_pending\n"));
717
718     check_blockables_blocked_or_lose();
719
720     /* If GC/SIG_STOP_FOR_GC struck during PA and there was no pending
721      * handler, then the pending mask was saved and
722      * gc_blocked_deferrables set. Hence, there can be no pending
723      * handler and it's safe to restore the pending mask.
724      *
725      * Note, that if gc_blocked_deferrables is false we may still have
726      * to GC. In this case, we are coming out of a WITHOUT-GCING or a
727      * pseudo atomic was interrupt be a deferrable first. */
728     if (data->gc_blocked_deferrables) {
729         if (data->pending_handler)
730             lose("GC blocked deferrables but still got a pending handler.");
731         if (SymbolValue(GC_INHIBIT,thread)!=NIL)
732             lose("GC blocked deferrables while GC is inhibited.");
733         /* Restore the saved signal mask from the original signal (the
734          * one that interrupted us during the critical section) into
735          * the os_context for the signal we're currently in the
736          * handler for. This should ensure that when we return from
737          * the handler the blocked signals are unblocked. */
738         sigcopyset(os_context_sigmask_addr(context), &data->pending_mask);
739         data->gc_blocked_deferrables = 0;
740     }
741
742     if (SymbolValue(GC_INHIBIT,thread)==NIL) {
743         void *original_pending_handler = data->pending_handler;
744
745 #ifdef LISP_FEATURE_SB_THREAD
746         if (SymbolValue(STOP_FOR_GC_PENDING,thread) != NIL) {
747             /* STOP_FOR_GC_PENDING and GC_PENDING are cleared by
748              * the signal handler if it actually stops us. */
749             arch_clear_pseudo_atomic_interrupted(context);
750             sig_stop_for_gc_handler(SIG_STOP_FOR_GC,NULL,context);
751         } else
752 #endif
753          /* Test for T and not for != NIL since the value :IN-PROGRESS
754           * is used in SUB-GC as part of the mechanism to supress
755           * recursive gcs.*/
756         if (SymbolValue(GC_PENDING,thread) == T) {
757
758             /* Two reasons for doing this. First, if there is a
759              * pending handler we don't want to run. Second, we are
760              * going to clear pseudo atomic interrupted to avoid
761              * spurious trapping on every allocation in SUB_GC and
762              * having a pending handler with interrupts enabled and
763              * without pseudo atomic interrupted breaks an
764              * invariant. */
765             if (data->pending_handler) {
766                 bind_variable(ALLOW_WITH_INTERRUPTS, NIL, thread);
767                 bind_variable(INTERRUPTS_ENABLED, NIL, thread);
768             }
769
770             arch_clear_pseudo_atomic_interrupted(context);
771
772             /* GC_PENDING is cleared in SUB-GC, or if another thread
773              * is doing a gc already we will get a SIG_STOP_FOR_GC and
774              * that will clear it.
775              *
776              * If there is a pending handler or gc was triggerred in a
777              * signal handler then maybe_gc won't run POST_GC and will
778              * return normally. */
779             if (!maybe_gc(context))
780                 lose("GC not inhibited but maybe_gc did not GC.");
781
782             if (data->pending_handler) {
783                 unbind(thread);
784                 unbind(thread);
785             }
786         } else if (SymbolValue(GC_PENDING,thread) != NIL) {
787             /* It's not NIL or T so GC_PENDING is :IN-PROGRESS. If
788              * GC-PENDING is not NIL then we cannot trap on pseudo
789              * atomic due to GC (see if(GC_PENDING) logic in
790              * cheneygc.c an gengcgc.c), plus there is a outer
791              * WITHOUT-INTERRUPTS SUB_GC, so how did we end up
792              * here? */
793             lose("Trapping to run pending handler while GC in progress.");
794         }
795
796         check_blockables_blocked_or_lose();
797
798         /* No GC shall be lost. If SUB_GC triggers another GC then
799          * that should be handled on the spot. */
800         if (SymbolValue(GC_PENDING,thread) != NIL)
801             lose("GC_PENDING after doing gc.");
802 #ifdef LISP_FEATURE_SB_THREAD
803         if (SymbolValue(STOP_FOR_GC_PENDING,thread) != NIL)
804             lose("STOP_FOR_GC_PENDING after doing gc.");
805 #endif
806         /* Check two things. First, that gc does not clobber a handler
807          * that's already pending. Second, that there is no interrupt
808          * lossage: if original_pending_handler was NULL then even if
809          * an interrupt arrived during GC (POST-GC, really) it was
810          * handled. */
811         if (original_pending_handler != data->pending_handler)
812             lose("pending handler changed in gc: %x -> %d.",
813                  original_pending_handler, data->pending_handler);
814     }
815
816 #ifndef LISP_FEATURE_WIN32
817     /* There may be no pending handler, because it was only a gc that
818      * had to be executed or because Lisp is a bit too eager to call
819      * DO-PENDING-INTERRUPT. */
820     if ((SymbolValue(INTERRUPTS_ENABLED,thread) != NIL) &&
821         (data->pending_handler))  {
822         /* No matter how we ended up here, clear both
823          * INTERRUPT_PENDING and pseudo atomic interrupted. It's safe
824          * because we checked above that there is no GC pending. */
825         SetSymbolValue(INTERRUPT_PENDING, NIL, thread);
826         arch_clear_pseudo_atomic_interrupted(context);
827         /* Restore the sigmask in the context. */
828         sigcopyset(os_context_sigmask_addr(context), &data->pending_mask);
829         /* This will break on sparc linux: the deferred handler really
830          * wants to be called with a void_context */
831         run_deferred_handler(data,(void *)context);
832     }
833     /* It is possible that the end of this function was reached
834      * without never actually doing anything, the tests in Lisp for
835      * when to call receive-pending-interrupt are not exact. */
836     FSHOW_SIGNAL((stderr, "/exiting interrupt_handle_pending\n"));
837 #endif
838 }
839 \f
840 /*
841  * the two main signal handlers:
842  *   interrupt_handle_now(..)
843  *   maybe_now_maybe_later(..)
844  *
845  * to which we have added interrupt_handle_now_handler(..).  Why?
846  * Well, mostly because the SPARC/Linux platform doesn't quite do
847  * signals the way we want them done.  The third argument in the
848  * handler isn't filled in by the kernel properly, so we fix it up
849  * ourselves in the arch_os_get_context(..) function; however, we only
850  * want to do this when we first hit the handler, and not when
851  * interrupt_handle_now(..) is being called from some other handler
852  * (when the fixup will already have been done). -- CSR, 2002-07-23
853  */
854
855 void
856 interrupt_handle_now(int signal, siginfo_t *info, os_context_t *context)
857 {
858 #ifdef FOREIGN_FUNCTION_CALL_FLAG
859     boolean were_in_lisp;
860 #endif
861     union interrupt_handler handler;
862
863     check_blockables_blocked_or_lose();
864
865 #ifndef LISP_FEATURE_WIN32
866     if (sigismember(&deferrable_sigset,signal))
867         check_interrupts_enabled_or_lose(context);
868 #endif
869
870     handler = interrupt_handlers[signal];
871
872     if (ARE_SAME_HANDLER(handler.c, SIG_IGN)) {
873         return;
874     }
875
876 #ifdef FOREIGN_FUNCTION_CALL_FLAG
877     were_in_lisp = !foreign_function_call_active;
878     if (were_in_lisp)
879 #endif
880     {
881         fake_foreign_function_call(context);
882     }
883
884     FSHOW_SIGNAL((stderr,
885                   "/entering interrupt_handle_now(%d, info, context)\n",
886                   signal));
887
888     if (ARE_SAME_HANDLER(handler.c, SIG_DFL)) {
889
890         /* This can happen if someone tries to ignore or default one
891          * of the signals we need for runtime support, and the runtime
892          * support decides to pass on it. */
893         lose("no handler for signal %d in interrupt_handle_now(..)\n", signal);
894
895     } else if (lowtag_of(handler.lisp) == FUN_POINTER_LOWTAG) {
896         /* Once we've decided what to do about contexts in a
897          * return-elsewhere world (the original context will no longer
898          * be available; should we copy it or was nobody using it anyway?)
899          * then we should convert this to return-elsewhere */
900
901         /* CMUCL comment said "Allocate the SAPs while the interrupts
902          * are still disabled.".  I (dan, 2003.08.21) assume this is
903          * because we're not in pseudoatomic and allocation shouldn't
904          * be interrupted.  In which case it's no longer an issue as
905          * all our allocation from C now goes through a PA wrapper,
906          * but still, doesn't hurt.
907          *
908          * Yeah, but non-gencgc platforms don't really wrap allocation
909          * in PA. MG - 2005-08-29  */
910
911         lispobj info_sap, context_sap;
912         /* Leave deferrable signals blocked, the handler itself will
913          * allow signals again when it sees fit. */
914         unblock_gc_signals();
915         context_sap = alloc_sap(context);
916         info_sap = alloc_sap(info);
917
918         FSHOW_SIGNAL((stderr,"/calling Lisp-level handler\n"));
919
920         funcall3(handler.lisp,
921                  make_fixnum(signal),
922                  info_sap,
923                  context_sap);
924     } else {
925         /* This cannot happen in sane circumstances. */
926
927         FSHOW_SIGNAL((stderr,"/calling C-level handler\n"));
928
929 #ifndef LISP_FEATURE_WIN32
930         /* Allow signals again. */
931         thread_sigmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
932 #endif
933         (*handler.c)(signal, info, context);
934     }
935
936 #ifdef FOREIGN_FUNCTION_CALL_FLAG
937     if (were_in_lisp)
938 #endif
939     {
940         undo_fake_foreign_function_call(context); /* block signals again */
941     }
942
943     FSHOW_SIGNAL((stderr,
944                   "/returning from interrupt_handle_now(%d, info, context)\n",
945                   signal));
946 }
947
948 /* This is called at the end of a critical section if the indications
949  * are that some signal was deferred during the section.  Note that as
950  * far as C or the kernel is concerned we dealt with the signal
951  * already; we're just doing the Lisp-level processing now that we
952  * put off then */
953 static void
954 run_deferred_handler(struct interrupt_data *data, void *v_context)
955 {
956     /* The pending_handler may enable interrupts and then another
957      * interrupt may hit, overwrite interrupt_data, so reset the
958      * pending handler before calling it. Trust the handler to finish
959      * with the siginfo before enabling interrupts. */
960     void (*pending_handler) (int, siginfo_t*, void*)=data->pending_handler;
961
962     data->pending_handler=0;
963     FSHOW_SIGNAL((stderr, "/running deferred handler %p\n", pending_handler));
964     (*pending_handler)(data->pending_signal,&(data->pending_info), v_context);
965 }
966
967 #ifndef LISP_FEATURE_WIN32
968 boolean
969 maybe_defer_handler(void *handler, struct interrupt_data *data,
970                     int signal, siginfo_t *info, os_context_t *context)
971 {
972     struct thread *thread=arch_os_get_current_thread();
973
974     check_blockables_blocked_or_lose();
975
976     if (SymbolValue(INTERRUPT_PENDING,thread) != NIL)
977         lose("interrupt already pending\n");
978     if (thread->interrupt_data->pending_handler)
979         lose("there is a pending handler already (PA)\n");
980     if (data->gc_blocked_deferrables)
981         lose("maybe_defer_handler: gc_blocked_deferrables true\n");
982     check_interrupt_context_or_lose(context);
983     /* If interrupts are disabled then INTERRUPT_PENDING is set and
984      * not PSEDUO_ATOMIC_INTERRUPTED. This is important for a pseudo
985      * atomic section inside a WITHOUT-INTERRUPTS.
986      *
987      * Also, if in_leaving_without_gcing_race_p then
988      * interrupt_handle_pending is going to be called soon, so
989      * stashing the signal away is safe.
990      */
991     if ((SymbolValue(INTERRUPTS_ENABLED,thread) == NIL) ||
992         in_leaving_without_gcing_race_p(thread)) {
993         store_signal_data_for_later(data,handler,signal,info,context);
994         SetSymbolValue(INTERRUPT_PENDING, T,thread);
995         FSHOW_SIGNAL((stderr,
996                       "/maybe_defer_handler(%x,%d): deferred (RACE=%d)\n",
997                       (unsigned int)handler,signal,
998                       in_leaving_without_gcing_race_p(thread)));
999         check_interrupt_context_or_lose(context);
1000         return 1;
1001     }
1002     /* a slightly confusing test. arch_pseudo_atomic_atomic() doesn't
1003      * actually use its argument for anything on x86, so this branch
1004      * may succeed even when context is null (gencgc alloc()) */
1005     if (arch_pseudo_atomic_atomic(context)) {
1006         store_signal_data_for_later(data,handler,signal,info,context);
1007         arch_set_pseudo_atomic_interrupted(context);
1008         FSHOW_SIGNAL((stderr,
1009                       "/maybe_defer_handler(%x,%d): deferred(PA)\n",
1010                       (unsigned int)handler,signal));
1011         check_interrupt_context_or_lose(context);
1012         return 1;
1013     }
1014     FSHOW_SIGNAL((stderr,
1015                   "/maybe_defer_handler(%x,%d): not deferred\n",
1016                   (unsigned int)handler,signal));
1017     return 0;
1018 }
1019
1020 static void
1021 store_signal_data_for_later (struct interrupt_data *data, void *handler,
1022                              int signal,
1023                              siginfo_t *info, os_context_t *context)
1024 {
1025     if (data->pending_handler)
1026         lose("tried to overwrite pending interrupt handler %x with %x\n",
1027              data->pending_handler, handler);
1028     if (!handler)
1029         lose("tried to defer null interrupt handler\n");
1030     data->pending_handler = handler;
1031     data->pending_signal = signal;
1032     if(info)
1033         memcpy(&(data->pending_info), info, sizeof(siginfo_t));
1034
1035     FSHOW_SIGNAL((stderr, "/store_signal_data_for_later: signal: %d\n",
1036                   signal));
1037
1038     if(!context)
1039         lose("Null context");
1040
1041     /* the signal mask in the context (from before we were
1042      * interrupted) is copied to be restored when run_deferred_handler
1043      * happens. Then the usually-blocked signals are added to the mask
1044      * in the context so that we are running with blocked signals when
1045      * the handler returns */
1046     sigcopyset(&(data->pending_mask),os_context_sigmask_addr(context));
1047     sigaddset_deferrable(os_context_sigmask_addr(context));
1048 }
1049
1050 static void
1051 maybe_now_maybe_later(int signal, siginfo_t *info, void *void_context)
1052 {
1053     SAVE_ERRNO(context,void_context);
1054     struct thread *thread = arch_os_get_current_thread();
1055     struct interrupt_data *data = thread->interrupt_data;
1056
1057     if(!maybe_defer_handler(interrupt_handle_now,data,signal,info,context))
1058         interrupt_handle_now(signal, info, context);
1059     RESTORE_ERRNO;
1060 }
1061
1062 static void
1063 low_level_interrupt_handle_now(int signal, siginfo_t *info,
1064                                os_context_t *context)
1065 {
1066     /* No FP control fixage needed, caller has done that. */
1067     check_blockables_blocked_or_lose();
1068     check_interrupts_enabled_or_lose(context);
1069     (*interrupt_low_level_handlers[signal])(signal, info, context);
1070     /* No Darwin context fixage needed, caller does that. */
1071 }
1072
1073 static void
1074 low_level_maybe_now_maybe_later(int signal, siginfo_t *info, void *void_context)
1075 {
1076     SAVE_ERRNO(context,void_context);
1077     struct thread *thread = arch_os_get_current_thread();
1078     struct interrupt_data *data = thread->interrupt_data;
1079
1080     if(!maybe_defer_handler(low_level_interrupt_handle_now,data,
1081                             signal,info,context))
1082         low_level_interrupt_handle_now(signal, info, context);
1083     RESTORE_ERRNO;
1084 }
1085 #endif
1086
1087 #ifdef LISP_FEATURE_SB_THREAD
1088
1089 /* This function must not cons, because that may trigger a GC. */
1090 void
1091 sig_stop_for_gc_handler(int signal, siginfo_t *info, void *void_context)
1092 {
1093     os_context_t *context = arch_os_get_context(&void_context);
1094
1095     struct thread *thread=arch_os_get_current_thread();
1096     sigset_t ss;
1097
1098     /* Test for GC_INHIBIT _first_, else we'd trap on every single
1099      * pseudo atomic until gc is finally allowed. */
1100     if (SymbolValue(GC_INHIBIT,thread) != NIL) {
1101         SetSymbolValue(STOP_FOR_GC_PENDING,T,thread);
1102         FSHOW_SIGNAL((stderr, "sig_stop_for_gc deferred (*GC-INHIBIT*)\n"));
1103         return;
1104     } else if (arch_pseudo_atomic_atomic(context)) {
1105         SetSymbolValue(STOP_FOR_GC_PENDING,T,thread);
1106         arch_set_pseudo_atomic_interrupted(context);
1107         maybe_save_gc_mask_and_block_deferrables
1108             (os_context_sigmask_addr(context));
1109         FSHOW_SIGNAL((stderr,"sig_stop_for_gc deferred (PA)\n"));
1110         return;
1111     }
1112
1113     FSHOW_SIGNAL((stderr, "/sig_stop_for_gc_handler\n"));
1114
1115     /* Not PA and GC not inhibited -- we can stop now. */
1116
1117     /* need the context stored so it can have registers scavenged */
1118     fake_foreign_function_call(context);
1119
1120     /* Block everything. */
1121     sigfillset(&ss);
1122     thread_sigmask(SIG_BLOCK,&ss,0);
1123
1124     /* Not pending anymore. */
1125     SetSymbolValue(GC_PENDING,NIL,thread);
1126     SetSymbolValue(STOP_FOR_GC_PENDING,NIL,thread);
1127
1128     if(thread_state(thread)!=STATE_RUNNING) {
1129         lose("sig_stop_for_gc_handler: wrong thread state: %ld\n",
1130              fixnum_value(thread->state));
1131     }
1132
1133     set_thread_state(thread,STATE_SUSPENDED);
1134     FSHOW_SIGNAL((stderr,"suspended\n"));
1135
1136     wait_for_thread_state_change(thread, STATE_SUSPENDED);
1137     FSHOW_SIGNAL((stderr,"resumed\n"));
1138
1139     if(thread_state(thread)!=STATE_RUNNING) {
1140         lose("sig_stop_for_gc_handler: wrong thread state on wakeup: %ld\n",
1141              fixnum_value(thread_state(thread)));
1142     }
1143
1144     undo_fake_foreign_function_call(context);
1145 }
1146
1147 #endif
1148
1149 void
1150 interrupt_handle_now_handler(int signal, siginfo_t *info, void *void_context)
1151 {
1152     SAVE_ERRNO(context,void_context);
1153 #ifndef LISP_FEATURE_WIN32
1154     if ((signal == SIGILL) || (signal == SIGBUS)
1155 #ifndef LISP_FEATURE_LINUX
1156         || (signal == SIGEMT)
1157 #endif
1158         )
1159         corruption_warning_and_maybe_lose("Signal %d recieved", signal);
1160 #endif
1161     interrupt_handle_now(signal, info, context);
1162     RESTORE_ERRNO;
1163 }
1164
1165 /* manipulate the signal context and stack such that when the handler
1166  * returns, it will call function instead of whatever it was doing
1167  * previously
1168  */
1169
1170 #if (defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
1171 extern int *context_eflags_addr(os_context_t *context);
1172 #endif
1173
1174 extern lispobj call_into_lisp(lispobj fun, lispobj *args, int nargs);
1175 extern void post_signal_tramp(void);
1176 extern void call_into_lisp_tramp(void);
1177 void
1178 arrange_return_to_lisp_function(os_context_t *context, lispobj function)
1179 {
1180     check_gc_signals_unblocked_in_sigset_or_lose
1181         (os_context_sigmask_addr(context));
1182 #if !(defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
1183     void * fun=native_pointer(function);
1184     void *code = &(((struct simple_fun *) fun)->code);
1185 #endif
1186
1187     /* Build a stack frame showing `interrupted' so that the
1188      * user's backtrace makes (as much) sense (as usual) */
1189
1190     /* fp state is saved and restored by call_into_lisp */
1191     /* FIXME: errno is not restored, but since current uses of this
1192      * function only call Lisp code that signals an error, it's not
1193      * much of a problem. In other words, running out of the control
1194      * stack between a syscall and (GET-ERRNO) may clobber errno if
1195      * something fails during signalling or in the handler. But I
1196      * can't see what can go wrong as long as there is no CONTINUE
1197      * like restart on them. */
1198 #ifdef LISP_FEATURE_X86
1199     /* Suppose the existence of some function that saved all
1200      * registers, called call_into_lisp, then restored GP registers and
1201      * returned.  It would look something like this:
1202
1203      push   ebp
1204      mov    ebp esp
1205      pushfl
1206      pushal
1207      push   $0
1208      push   $0
1209      pushl  {address of function to call}
1210      call   0x8058db0 <call_into_lisp>
1211      addl   $12,%esp
1212      popal
1213      popfl
1214      leave
1215      ret
1216
1217      * What we do here is set up the stack that call_into_lisp would
1218      * expect to see if it had been called by this code, and frob the
1219      * signal context so that signal return goes directly to call_into_lisp,
1220      * and when that function (and the lisp function it invoked) returns,
1221      * it returns to the second half of this imaginary function which
1222      * restores all registers and returns to C
1223
1224      * For this to work, the latter part of the imaginary function
1225      * must obviously exist in reality.  That would be post_signal_tramp
1226      */
1227
1228     u32 *sp=(u32 *)*os_context_register_addr(context,reg_ESP);
1229
1230 #if defined(LISP_FEATURE_DARWIN)
1231     u32 *register_save_area = (u32 *)os_validate(0, 0x40);
1232
1233     FSHOW_SIGNAL((stderr, "/arrange_return_to_lisp_function: preparing to go to function %x, sp: %x\n", function, sp));
1234     FSHOW_SIGNAL((stderr, "/arrange_return_to_lisp_function: context: %x, &context %x\n", context, &context));
1235
1236     /* 1. os_validate (malloc/mmap) register_save_block
1237      * 2. copy register state into register_save_block
1238      * 3. put a pointer to register_save_block in a register in the context
1239      * 4. set the context's EIP to point to a trampoline which:
1240      *    a. builds the fake stack frame from the block
1241      *    b. frees the block
1242      *    c. calls the function
1243      */
1244
1245     *register_save_area = *os_context_pc_addr(context);
1246     *(register_save_area + 1) = function;
1247     *(register_save_area + 2) = *os_context_register_addr(context,reg_EDI);
1248     *(register_save_area + 3) = *os_context_register_addr(context,reg_ESI);
1249     *(register_save_area + 4) = *os_context_register_addr(context,reg_EDX);
1250     *(register_save_area + 5) = *os_context_register_addr(context,reg_ECX);
1251     *(register_save_area + 6) = *os_context_register_addr(context,reg_EBX);
1252     *(register_save_area + 7) = *os_context_register_addr(context,reg_EAX);
1253     *(register_save_area + 8) = *context_eflags_addr(context);
1254
1255     *os_context_pc_addr(context) =
1256       (os_context_register_t) call_into_lisp_tramp;
1257     *os_context_register_addr(context,reg_ECX) =
1258       (os_context_register_t) register_save_area;
1259 #else
1260
1261     /* return address for call_into_lisp: */
1262     *(sp-15) = (u32)post_signal_tramp;
1263     *(sp-14) = function;        /* args for call_into_lisp : function*/
1264     *(sp-13) = 0;               /*                           arg array */
1265     *(sp-12) = 0;               /*                           no. args */
1266     /* this order matches that used in POPAD */
1267     *(sp-11)=*os_context_register_addr(context,reg_EDI);
1268     *(sp-10)=*os_context_register_addr(context,reg_ESI);
1269
1270     *(sp-9)=*os_context_register_addr(context,reg_ESP)-8;
1271     /* POPAD ignores the value of ESP:  */
1272     *(sp-8)=0;
1273     *(sp-7)=*os_context_register_addr(context,reg_EBX);
1274
1275     *(sp-6)=*os_context_register_addr(context,reg_EDX);
1276     *(sp-5)=*os_context_register_addr(context,reg_ECX);
1277     *(sp-4)=*os_context_register_addr(context,reg_EAX);
1278     *(sp-3)=*context_eflags_addr(context);
1279     *(sp-2)=*os_context_register_addr(context,reg_EBP);
1280     *(sp-1)=*os_context_pc_addr(context);
1281
1282 #endif
1283
1284 #elif defined(LISP_FEATURE_X86_64)
1285     u64 *sp=(u64 *)*os_context_register_addr(context,reg_RSP);
1286
1287     /* return address for call_into_lisp: */
1288     *(sp-18) = (u64)post_signal_tramp;
1289
1290     *(sp-17)=*os_context_register_addr(context,reg_R15);
1291     *(sp-16)=*os_context_register_addr(context,reg_R14);
1292     *(sp-15)=*os_context_register_addr(context,reg_R13);
1293     *(sp-14)=*os_context_register_addr(context,reg_R12);
1294     *(sp-13)=*os_context_register_addr(context,reg_R11);
1295     *(sp-12)=*os_context_register_addr(context,reg_R10);
1296     *(sp-11)=*os_context_register_addr(context,reg_R9);
1297     *(sp-10)=*os_context_register_addr(context,reg_R8);
1298     *(sp-9)=*os_context_register_addr(context,reg_RDI);
1299     *(sp-8)=*os_context_register_addr(context,reg_RSI);
1300     /* skip RBP and RSP */
1301     *(sp-7)=*os_context_register_addr(context,reg_RBX);
1302     *(sp-6)=*os_context_register_addr(context,reg_RDX);
1303     *(sp-5)=*os_context_register_addr(context,reg_RCX);
1304     *(sp-4)=*os_context_register_addr(context,reg_RAX);
1305     *(sp-3)=*context_eflags_addr(context);
1306     *(sp-2)=*os_context_register_addr(context,reg_RBP);
1307     *(sp-1)=*os_context_pc_addr(context);
1308
1309     *os_context_register_addr(context,reg_RDI) =
1310         (os_context_register_t)function; /* function */
1311     *os_context_register_addr(context,reg_RSI) = 0;        /* arg. array */
1312     *os_context_register_addr(context,reg_RDX) = 0;        /* no. args */
1313 #else
1314     struct thread *th=arch_os_get_current_thread();
1315     build_fake_control_stack_frames(th,context);
1316 #endif
1317
1318 #ifdef LISP_FEATURE_X86
1319
1320 #if !defined(LISP_FEATURE_DARWIN)
1321     *os_context_pc_addr(context) = (os_context_register_t)call_into_lisp;
1322     *os_context_register_addr(context,reg_ECX) = 0;
1323     *os_context_register_addr(context,reg_EBP) = (os_context_register_t)(sp-2);
1324 #ifdef __NetBSD__
1325     *os_context_register_addr(context,reg_UESP) =
1326         (os_context_register_t)(sp-15);
1327 #else
1328     *os_context_register_addr(context,reg_ESP) = (os_context_register_t)(sp-15);
1329 #endif /* __NETBSD__ */
1330 #endif /* LISP_FEATURE_DARWIN */
1331
1332 #elif defined(LISP_FEATURE_X86_64)
1333     *os_context_pc_addr(context) = (os_context_register_t)call_into_lisp;
1334     *os_context_register_addr(context,reg_RCX) = 0;
1335     *os_context_register_addr(context,reg_RBP) = (os_context_register_t)(sp-2);
1336     *os_context_register_addr(context,reg_RSP) = (os_context_register_t)(sp-18);
1337 #else
1338     /* this much of the calling convention is common to all
1339        non-x86 ports */
1340     *os_context_pc_addr(context) = (os_context_register_t)(unsigned long)code;
1341     *os_context_register_addr(context,reg_NARGS) = 0;
1342     *os_context_register_addr(context,reg_LIP) =
1343         (os_context_register_t)(unsigned long)code;
1344     *os_context_register_addr(context,reg_CFP) =
1345         (os_context_register_t)(unsigned long)current_control_frame_pointer;
1346 #endif
1347 #ifdef ARCH_HAS_NPC_REGISTER
1348     *os_context_npc_addr(context) =
1349         4 + *os_context_pc_addr(context);
1350 #endif
1351 #ifdef LISP_FEATURE_SPARC
1352     *os_context_register_addr(context,reg_CODE) =
1353         (os_context_register_t)(fun + FUN_POINTER_LOWTAG);
1354 #endif
1355     FSHOW((stderr, "/arranged return to Lisp function (0x%lx)\n",
1356            (long)function));
1357 }
1358
1359 /* KLUDGE: Theoretically the approach we use for undefined alien
1360  * variables should work for functions as well, but on PPC/Darwin
1361  * we get bus error at bogus addresses instead, hence this workaround,
1362  * that has the added benefit of automatically discriminating between
1363  * functions and variables.
1364  */
1365 void
1366 undefined_alien_function(void)
1367 {
1368     funcall0(StaticSymbolFunction(UNDEFINED_ALIEN_FUNCTION_ERROR));
1369 }
1370
1371 boolean
1372 handle_guard_page_triggered(os_context_t *context,os_vm_address_t addr)
1373 {
1374     struct thread *th=arch_os_get_current_thread();
1375
1376     /* note the os_context hackery here.  When the signal handler returns,
1377      * it won't go back to what it was doing ... */
1378     if(addr >= CONTROL_STACK_GUARD_PAGE(th) &&
1379        addr < CONTROL_STACK_GUARD_PAGE(th) + os_vm_page_size) {
1380         /* We hit the end of the control stack: disable guard page
1381          * protection so the error handler has some headroom, protect the
1382          * previous page so that we can catch returns from the guard page
1383          * and restore it. */
1384         corruption_warning_and_maybe_lose("Control stack exhausted");
1385         protect_control_stack_guard_page(0, NULL);
1386         protect_control_stack_return_guard_page(1, NULL);
1387
1388 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
1389         /* For the unfortunate case, when the control stack is
1390          * exhausted in a signal handler. */
1391         unblock_signals_in_context_and_maybe_warn(context);
1392 #endif
1393         arrange_return_to_lisp_function
1394             (context, StaticSymbolFunction(CONTROL_STACK_EXHAUSTED_ERROR));
1395         return 1;
1396     }
1397     else if(addr >= CONTROL_STACK_RETURN_GUARD_PAGE(th) &&
1398             addr < CONTROL_STACK_RETURN_GUARD_PAGE(th) + os_vm_page_size) {
1399         /* We're returning from the guard page: reprotect it, and
1400          * unprotect this one. This works even if we somehow missed
1401          * the return-guard-page, and hit it on our way to new
1402          * exhaustion instead. */
1403         fprintf(stderr, "INFO: Control stack guard page reprotected\n");
1404         protect_control_stack_guard_page(1, NULL);
1405         protect_control_stack_return_guard_page(0, NULL);
1406         return 1;
1407     }
1408     else if(addr >= BINDING_STACK_GUARD_PAGE(th) &&
1409             addr < BINDING_STACK_GUARD_PAGE(th) + os_vm_page_size) {
1410         corruption_warning_and_maybe_lose("Binding stack exhausted");
1411         protect_binding_stack_guard_page(0, NULL);
1412         protect_binding_stack_return_guard_page(1, NULL);
1413
1414         /* For the unfortunate case, when the binding stack is
1415          * exhausted in a signal handler. */
1416         unblock_signals_in_context_and_maybe_warn(context);
1417         arrange_return_to_lisp_function
1418             (context, StaticSymbolFunction(BINDING_STACK_EXHAUSTED_ERROR));
1419         return 1;
1420     }
1421     else if(addr >= BINDING_STACK_RETURN_GUARD_PAGE(th) &&
1422             addr < BINDING_STACK_RETURN_GUARD_PAGE(th) + os_vm_page_size) {
1423         fprintf(stderr, "INFO: Binding stack guard page reprotected\n");
1424         protect_binding_stack_guard_page(1, NULL);
1425         protect_binding_stack_return_guard_page(0, NULL);
1426         return 1;
1427     }
1428     else if(addr >= ALIEN_STACK_GUARD_PAGE(th) &&
1429             addr < ALIEN_STACK_GUARD_PAGE(th) + os_vm_page_size) {
1430         corruption_warning_and_maybe_lose("Alien stack exhausted");
1431         protect_alien_stack_guard_page(0, NULL);
1432         protect_alien_stack_return_guard_page(1, NULL);
1433
1434         /* For the unfortunate case, when the alien stack is
1435          * exhausted in a signal handler. */
1436         unblock_signals_in_context_and_maybe_warn(context);
1437         arrange_return_to_lisp_function
1438             (context, StaticSymbolFunction(ALIEN_STACK_EXHAUSTED_ERROR));
1439         return 1;
1440     }
1441     else if(addr >= ALIEN_STACK_RETURN_GUARD_PAGE(th) &&
1442             addr < ALIEN_STACK_RETURN_GUARD_PAGE(th) + os_vm_page_size) {
1443         fprintf(stderr, "INFO: Alien stack guard page reprotected\n");
1444         protect_alien_stack_guard_page(1, NULL);
1445         protect_alien_stack_return_guard_page(0, NULL);
1446         return 1;
1447     }
1448     else if (addr >= undefined_alien_address &&
1449              addr < undefined_alien_address + os_vm_page_size) {
1450         arrange_return_to_lisp_function
1451           (context, StaticSymbolFunction(UNDEFINED_ALIEN_VARIABLE_ERROR));
1452         return 1;
1453     }
1454     else return 0;
1455 }
1456 \f
1457 /*
1458  * noise to install handlers
1459  */
1460
1461 #ifndef LISP_FEATURE_WIN32
1462 /* In Linux 2.4 synchronous signals (sigtrap & co) can be delivered if
1463  * they are blocked, in Linux 2.6 the default handler is invoked
1464  * instead that usually coredumps. One might hastily think that adding
1465  * SA_NODEFER helps, but until ~2.6.13 if SA_NODEFER is specified then
1466  * the whole sa_mask is ignored and instead of not adding the signal
1467  * in question to the mask. That means if it's not blockable the
1468  * signal must be unblocked at the beginning of signal handlers.
1469  *
1470  * It turns out that NetBSD's SA_NODEFER doesn't DTRT in a different
1471  * way: if SA_NODEFER is set and the signal is in sa_mask, the signal
1472  * will be unblocked in the sigmask during the signal handler.  -- RMK
1473  * X-mas day, 2005
1474  */
1475 static volatile int sigaction_nodefer_works = -1;
1476
1477 #define SA_NODEFER_TEST_BLOCK_SIGNAL SIGABRT
1478 #define SA_NODEFER_TEST_KILL_SIGNAL SIGUSR1
1479
1480 static void
1481 sigaction_nodefer_test_handler(int signal, siginfo_t *info, void *void_context)
1482 {
1483     sigset_t empty, current;
1484     int i;
1485     sigemptyset(&empty);
1486     thread_sigmask(SIG_BLOCK, &empty, &current);
1487     /* There should be exactly two blocked signals: the two we added
1488      * to sa_mask when setting up the handler.  NetBSD doesn't block
1489      * the signal we're handling when SA_NODEFER is set; Linux before
1490      * 2.6.13 or so also doesn't block the other signal when
1491      * SA_NODEFER is set. */
1492     for(i = 1; i < NSIG; i++)
1493         if (sigismember(&current, i) !=
1494             (((i == SA_NODEFER_TEST_BLOCK_SIGNAL) || (i == signal)) ? 1 : 0)) {
1495             FSHOW_SIGNAL((stderr, "SA_NODEFER doesn't work, signal %d\n", i));
1496             sigaction_nodefer_works = 0;
1497         }
1498     if (sigaction_nodefer_works == -1)
1499         sigaction_nodefer_works = 1;
1500 }
1501
1502 static void
1503 see_if_sigaction_nodefer_works(void)
1504 {
1505     struct sigaction sa, old_sa;
1506
1507     sa.sa_flags = SA_SIGINFO | SA_NODEFER;
1508     sa.sa_sigaction = sigaction_nodefer_test_handler;
1509     sigemptyset(&sa.sa_mask);
1510     sigaddset(&sa.sa_mask, SA_NODEFER_TEST_BLOCK_SIGNAL);
1511     sigaddset(&sa.sa_mask, SA_NODEFER_TEST_KILL_SIGNAL);
1512     sigaction(SA_NODEFER_TEST_KILL_SIGNAL, &sa, &old_sa);
1513     /* Make sure no signals are blocked. */
1514     {
1515         sigset_t empty;
1516         sigemptyset(&empty);
1517         thread_sigmask(SIG_SETMASK, &empty, 0);
1518     }
1519     kill(getpid(), SA_NODEFER_TEST_KILL_SIGNAL);
1520     while (sigaction_nodefer_works == -1);
1521     sigaction(SA_NODEFER_TEST_KILL_SIGNAL, &old_sa, NULL);
1522 }
1523
1524 #undef SA_NODEFER_TEST_BLOCK_SIGNAL
1525 #undef SA_NODEFER_TEST_KILL_SIGNAL
1526
1527 static void
1528 unblock_me_trampoline(int signal, siginfo_t *info, void *void_context)
1529 {
1530     SAVE_ERRNO(context,void_context);
1531     sigset_t unblock;
1532
1533     sigemptyset(&unblock);
1534     sigaddset(&unblock, signal);
1535     thread_sigmask(SIG_UNBLOCK, &unblock, 0);
1536     interrupt_handle_now(signal, info, context);
1537     RESTORE_ERRNO;
1538 }
1539
1540 static void
1541 low_level_unblock_me_trampoline(int signal, siginfo_t *info, void *void_context)
1542 {
1543     SAVE_ERRNO(context,void_context);
1544     sigset_t unblock;
1545
1546     sigemptyset(&unblock);
1547     sigaddset(&unblock, signal);
1548     thread_sigmask(SIG_UNBLOCK, &unblock, 0);
1549     (*interrupt_low_level_handlers[signal])(signal, info, void_context);
1550     RESTORE_ERRNO;
1551 }
1552
1553 static void
1554 low_level_handle_now_handler(int signal, siginfo_t *info, void *void_context)
1555 {
1556     SAVE_ERRNO(context,void_context);
1557     (*interrupt_low_level_handlers[signal])(signal, info, void_context);
1558     RESTORE_ERRNO;
1559 }
1560
1561 void
1562 undoably_install_low_level_interrupt_handler (int signal,
1563                                               interrupt_handler_t handler)
1564 {
1565     struct sigaction sa;
1566
1567     if (0 > signal || signal >= NSIG) {
1568         lose("bad signal number %d\n", signal);
1569     }
1570
1571     if (ARE_SAME_HANDLER(handler, SIG_DFL))
1572         sa.sa_sigaction = handler;
1573     else if (sigismember(&deferrable_sigset,signal))
1574         sa.sa_sigaction = low_level_maybe_now_maybe_later;
1575     /* The use of a trampoline appears to break the
1576        arch_os_get_context() workaround for SPARC/Linux.  For now,
1577        don't use the trampoline (and so be vulnerable to the problems
1578        that SA_NODEFER is meant to solve. */
1579 #if !(defined(LISP_FEATURE_SPARC) && defined(LISP_FEATURE_LINUX))
1580     else if (!sigaction_nodefer_works &&
1581              !sigismember(&blockable_sigset, signal))
1582         sa.sa_sigaction = low_level_unblock_me_trampoline;
1583 #endif
1584     else
1585         sa.sa_sigaction = low_level_handle_now_handler;
1586
1587     sigcopyset(&sa.sa_mask, &blockable_sigset);
1588     sa.sa_flags = SA_SIGINFO | SA_RESTART
1589         | (sigaction_nodefer_works ? SA_NODEFER : 0);
1590 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
1591     if((signal==SIG_MEMORY_FAULT))
1592         sa.sa_flags |= SA_ONSTACK;
1593 #endif
1594
1595     sigaction(signal, &sa, NULL);
1596     interrupt_low_level_handlers[signal] =
1597         (ARE_SAME_HANDLER(handler, SIG_DFL) ? 0 : handler);
1598 }
1599 #endif
1600
1601 /* This is called from Lisp. */
1602 unsigned long
1603 install_handler(int signal, void handler(int, siginfo_t*, void*))
1604 {
1605 #ifndef LISP_FEATURE_WIN32
1606     struct sigaction sa;
1607     sigset_t old, new;
1608     union interrupt_handler oldhandler;
1609
1610     FSHOW((stderr, "/entering POSIX install_handler(%d, ..)\n", signal));
1611
1612     sigemptyset(&new);
1613     sigaddset(&new, signal);
1614     thread_sigmask(SIG_BLOCK, &new, &old);
1615
1616     FSHOW((stderr, "/interrupt_low_level_handlers[signal]=%x\n",
1617            (unsigned int)interrupt_low_level_handlers[signal]));
1618     if (interrupt_low_level_handlers[signal]==0) {
1619         if (ARE_SAME_HANDLER(handler, SIG_DFL) ||
1620             ARE_SAME_HANDLER(handler, SIG_IGN))
1621             sa.sa_sigaction = handler;
1622         else if (sigismember(&deferrable_sigset, signal))
1623             sa.sa_sigaction = maybe_now_maybe_later;
1624         else if (!sigaction_nodefer_works &&
1625                  !sigismember(&blockable_sigset, signal))
1626             sa.sa_sigaction = unblock_me_trampoline;
1627         else
1628             sa.sa_sigaction = interrupt_handle_now_handler;
1629
1630         sigcopyset(&sa.sa_mask, &blockable_sigset);
1631         sa.sa_flags = SA_SIGINFO | SA_RESTART |
1632             (sigaction_nodefer_works ? SA_NODEFER : 0);
1633         sigaction(signal, &sa, NULL);
1634     }
1635
1636     oldhandler = interrupt_handlers[signal];
1637     interrupt_handlers[signal].c = handler;
1638
1639     thread_sigmask(SIG_SETMASK, &old, 0);
1640
1641     FSHOW((stderr, "/leaving POSIX install_handler(%d, ..)\n", signal));
1642
1643     return (unsigned long)oldhandler.lisp;
1644 #else
1645     /* Probably-wrong Win32 hack */
1646     return 0;
1647 #endif
1648 }
1649
1650 /* This must not go through lisp as it's allowed anytime, even when on
1651  * the altstack. */
1652 void
1653 sigabrt_handler(int signal, siginfo_t *info, void *void_context)
1654 {
1655     lose("SIGABRT received.\n");
1656 }
1657
1658 void
1659 interrupt_init(void)
1660 {
1661 #ifndef LISP_FEATURE_WIN32
1662     int i;
1663     SHOW("entering interrupt_init()");
1664     see_if_sigaction_nodefer_works();
1665     sigemptyset(&deferrable_sigset);
1666     sigemptyset(&blockable_sigset);
1667     sigemptyset(&gc_sigset);
1668     sigaddset_deferrable(&deferrable_sigset);
1669     sigaddset_blockable(&blockable_sigset);
1670     sigaddset_gc(&gc_sigset);
1671
1672     /* Set up high level handler information. */
1673     for (i = 0; i < NSIG; i++) {
1674         interrupt_handlers[i].c =
1675             /* (The cast here blasts away the distinction between
1676              * SA_SIGACTION-style three-argument handlers and
1677              * signal(..)-style one-argument handlers, which is OK
1678              * because it works to call the 1-argument form where the
1679              * 3-argument form is expected.) */
1680             (void (*)(int, siginfo_t*, void*))SIG_DFL;
1681     }
1682     undoably_install_low_level_interrupt_handler(SIGABRT, sigabrt_handler);
1683     SHOW("returning from interrupt_init()");
1684 #endif
1685 }
1686
1687 #ifndef LISP_FEATURE_WIN32
1688 int
1689 siginfo_code(siginfo_t *info)
1690 {
1691     return info->si_code;
1692 }
1693 os_vm_address_t current_memory_fault_address;
1694
1695 void
1696 lisp_memory_fault_error(os_context_t *context, os_vm_address_t addr)
1697 {
1698    /* FIXME: This is lossy: if we get another memory fault (eg. from
1699     * another thread) before lisp has read this, we lose the information.
1700     * However, since this is mostly informative, we'll live with that for
1701     * now -- some address is better then no address in this case.
1702     */
1703     current_memory_fault_address = addr;
1704     /* To allow debugging memory faults in signal handlers and such. */
1705     corruption_warning_and_maybe_lose("Memory fault");
1706     unblock_signals_in_context_and_maybe_warn(context);
1707     arrange_return_to_lisp_function(context,
1708                                     StaticSymbolFunction(MEMORY_FAULT_ERROR));
1709 }
1710 #endif
1711
1712 static void
1713 unhandled_trap_error(os_context_t *context)
1714 {
1715     lispobj context_sap;
1716     fake_foreign_function_call(context);
1717     unblock_gc_signals();
1718     context_sap = alloc_sap(context);
1719 #ifndef LISP_FEATURE_WIN32
1720     thread_sigmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
1721 #endif
1722     funcall1(StaticSymbolFunction(UNHANDLED_TRAP_ERROR), context_sap);
1723     lose("UNHANDLED-TRAP-ERROR fell through");
1724 }
1725
1726 /* Common logic for trapping instructions. How we actually handle each
1727  * case is highly architecture dependent, but the overall shape is
1728  * this. */
1729 void
1730 handle_trap(os_context_t *context, int trap)
1731 {
1732     switch(trap) {
1733     case trap_PendingInterrupt:
1734         FSHOW((stderr, "/<trap pending interrupt>\n"));
1735         arch_skip_instruction(context);
1736         interrupt_handle_pending(context);
1737         break;
1738     case trap_Error:
1739     case trap_Cerror:
1740         FSHOW((stderr, "/<trap error/cerror %d>\n", trap));
1741         interrupt_internal_error(context, trap==trap_Cerror);
1742         break;
1743     case trap_Breakpoint:
1744         arch_handle_breakpoint(context);
1745         break;
1746     case trap_FunEndBreakpoint:
1747         arch_handle_fun_end_breakpoint(context);
1748         break;
1749 #ifdef trap_AfterBreakpoint
1750     case trap_AfterBreakpoint:
1751         arch_handle_after_breakpoint(context);
1752         break;
1753 #endif
1754 #ifdef trap_SingleStepAround
1755     case trap_SingleStepAround:
1756     case trap_SingleStepBefore:
1757         arch_handle_single_step_trap(context, trap);
1758         break;
1759 #endif
1760     case trap_Halt:
1761         fake_foreign_function_call(context);
1762         lose("%%PRIMITIVE HALT called; the party is over.\n");
1763     default:
1764         unhandled_trap_error(context);
1765     }
1766 }