2 * interrupt-handling magic
6 * This software is part of the SBCL system. See the README file for
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.
17 /* As far as I can tell, what's going on here is:
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.
25 * However, some signals need special handling, e.g.
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(..).
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
41 * - WHN 20000728, dan 20010128 */
48 #include <sys/types.h>
56 #include "interrupt.h"
65 #include "genesis/fdefn.h"
66 #include "genesis/simple-fun.h"
67 #include "genesis/cons.h"
71 void run_deferred_handler(struct interrupt_data *data, void *v_context) ;
72 static void store_signal_data_for_later (struct interrupt_data *data,
73 void *handler, int signal,
75 os_context_t *context);
76 boolean interrupt_maybe_gc_int(int signal, siginfo_t *info, void *v_context);
78 void sigaddset_deferrable(sigset_t *s)
82 sigaddset(s, SIGQUIT);
83 sigaddset(s, SIGPIPE);
84 sigaddset(s, SIGALRM);
87 sigaddset(s, SIGTSTP);
88 sigaddset(s, SIGCHLD);
90 sigaddset(s, SIGXCPU);
91 sigaddset(s, SIGXFSZ);
92 sigaddset(s, SIGVTALRM);
93 sigaddset(s, SIGPROF);
94 sigaddset(s, SIGWINCH);
95 sigaddset(s, SIGUSR1);
96 sigaddset(s, SIGUSR2);
97 #ifdef LISP_FEATURE_SB_THREAD
98 sigaddset(s, SIG_INTERRUPT_THREAD);
102 void sigaddset_blockable(sigset_t *s)
104 sigaddset_deferrable(s);
105 #ifdef LISP_FEATURE_SB_THREAD
106 sigaddset(s, SIG_STOP_FOR_GC);
110 /* initialized in interrupt_init */
111 static sigset_t deferrable_sigset;
112 static sigset_t blockable_sigset;
114 inline static void check_blockables_blocked_or_lose()
116 /* Get the current sigmask, by blocking the empty set. */
117 sigset_t empty,current;
120 thread_sigmask(SIG_BLOCK, &empty, ¤t);
121 for(i=0;i<NSIG;i++) {
122 if (sigismember(&blockable_sigset, i) && !sigismember(¤t, i))
123 lose("blockable signal %d not blocked",i);
127 inline static void check_interrupts_enabled_or_lose(os_context_t *context)
129 struct thread *thread=arch_os_get_current_thread();
130 if (SymbolValue(INTERRUPTS_ENABLED,thread) == NIL)
131 lose("interrupts not enabled");
133 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
134 (!foreign_function_call_active) &&
136 arch_pseudo_atomic_atomic(context))
137 lose ("in pseudo atomic section");
140 /* When we catch an internal error, should we pass it back to Lisp to
141 * be handled in a high-level way? (Early in cold init, the answer is
142 * 'no', because Lisp is still too brain-dead to handle anything.
143 * After sufficient initialization has been completed, the answer
145 boolean internal_errors_enabled = 0;
147 struct interrupt_data * global_interrupt_data;
149 /* At the toplevel repl we routinely call this function. The signal
150 * mask ought to be clear anyway most of the time, but may be non-zero
151 * if we were interrupted e.g. while waiting for a queue. */
153 void reset_signal_mask ()
157 thread_sigmask(SIG_SETMASK,&new,0);
160 void block_blockable_signals ()
164 sigaddset_blockable(&block);
165 thread_sigmask(SIG_BLOCK, &block, 0);
170 * utility routines used by various signal handlers
174 build_fake_control_stack_frames(struct thread *th,os_context_t *context)
176 #ifndef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
180 /* Build a fake stack frame or frames */
182 current_control_frame_pointer =
183 (lispobj *)(*os_context_register_addr(context, reg_CSP));
184 if ((lispobj *)(*os_context_register_addr(context, reg_CFP))
185 == current_control_frame_pointer) {
186 /* There is a small window during call where the callee's
187 * frame isn't built yet. */
188 if (lowtag_of(*os_context_register_addr(context, reg_CODE))
189 == FUN_POINTER_LOWTAG) {
190 /* We have called, but not built the new frame, so
191 * build it for them. */
192 current_control_frame_pointer[0] =
193 *os_context_register_addr(context, reg_OCFP);
194 current_control_frame_pointer[1] =
195 *os_context_register_addr(context, reg_LRA);
196 current_control_frame_pointer += 8;
197 /* Build our frame on top of it. */
198 oldcont = (lispobj)(*os_context_register_addr(context, reg_CFP));
201 /* We haven't yet called, build our frame as if the
202 * partial frame wasn't there. */
203 oldcont = (lispobj)(*os_context_register_addr(context, reg_OCFP));
206 /* We can't tell whether we are still in the caller if it had to
207 * allocate a stack frame due to stack arguments. */
208 /* This observation provoked some past CMUCL maintainer to ask
209 * "Can anything strange happen during return?" */
212 oldcont = (lispobj)(*os_context_register_addr(context, reg_CFP));
215 current_control_stack_pointer = current_control_frame_pointer + 8;
217 current_control_frame_pointer[0] = oldcont;
218 current_control_frame_pointer[1] = NIL;
219 current_control_frame_pointer[2] =
220 (lispobj)(*os_context_register_addr(context, reg_CODE));
225 fake_foreign_function_call(os_context_t *context)
228 struct thread *thread=arch_os_get_current_thread();
230 /* context_index incrementing must not be interrupted */
231 check_blockables_blocked_or_lose();
233 /* Get current Lisp state from context. */
235 dynamic_space_free_pointer =
236 (lispobj *)(*os_context_register_addr(context, reg_ALLOC));
237 #if defined(LISP_FEATURE_ALPHA)
238 if ((long)dynamic_space_free_pointer & 1) {
239 lose("dead in fake_foreign_function_call, context = %x", context);
244 current_binding_stack_pointer =
245 (lispobj *)(*os_context_register_addr(context, reg_BSP));
248 build_fake_control_stack_frames(thread,context);
250 /* Do dynamic binding of the active interrupt context index
251 * and save the context in the context array. */
253 fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,thread));
255 if (context_index >= MAX_INTERRUPTS) {
256 lose("maximum interrupt nesting depth (%d) exceeded", MAX_INTERRUPTS);
259 bind_variable(FREE_INTERRUPT_CONTEXT_INDEX,
260 make_fixnum(context_index + 1),thread);
262 thread->interrupt_contexts[context_index] = context;
264 /* no longer in Lisp now */
265 foreign_function_call_active = 1;
268 /* blocks all blockable signals. If you are calling from a signal handler,
269 * the usual signal mask will be restored from the context when the handler
270 * finishes. Otherwise, be careful */
273 undo_fake_foreign_function_call(os_context_t *context)
275 struct thread *thread=arch_os_get_current_thread();
276 /* Block all blockable signals. */
277 block_blockable_signals();
279 /* going back into Lisp */
280 foreign_function_call_active = 0;
282 /* Undo dynamic binding of FREE_INTERRUPT_CONTEXT_INDEX */
286 /* Put the dynamic space free pointer back into the context. */
287 *os_context_register_addr(context, reg_ALLOC) =
288 (unsigned long) dynamic_space_free_pointer;
292 /* a handler for the signal caused by execution of a trap opcode
293 * signalling an internal error */
295 interrupt_internal_error(int signal, siginfo_t *info, os_context_t *context,
298 lispobj context_sap = 0;
300 check_blockables_blocked_or_lose();
301 fake_foreign_function_call(context);
303 /* Allocate the SAP object while the interrupts are still
305 if (internal_errors_enabled) {
306 context_sap = alloc_sap(context);
309 thread_sigmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
311 if (internal_errors_enabled) {
312 SHOW("in interrupt_internal_error");
314 /* Display some rudimentary debugging information about the
315 * error, so that even if the Lisp error handler gets badly
316 * confused, we have a chance to determine what's going on. */
317 describe_internal_error(context);
319 funcall2(SymbolFunction(INTERNAL_ERROR), context_sap,
320 continuable ? T : NIL);
322 describe_internal_error(context);
323 /* There's no good way to recover from an internal error
324 * before the Lisp error handling mechanism is set up. */
325 lose("internal error too early in init, can't recover");
327 undo_fake_foreign_function_call(context); /* blocks signals again */
329 arch_skip_instruction(context);
334 interrupt_handle_pending(os_context_t *context)
336 struct thread *thread;
337 struct interrupt_data *data;
339 check_blockables_blocked_or_lose();
341 thread=arch_os_get_current_thread();
342 data=thread->interrupt_data;
344 if (SymbolValue(GC_INHIBIT,thread)==NIL) {
345 #ifdef LISP_FEATURE_SB_THREAD
346 if (SymbolValue(STOP_FOR_GC_PENDING,thread) != NIL) {
347 /* another thread has already initiated a gc, this attempt
348 * might as well be cancelled */
349 SetSymbolValue(GC_PENDING,NIL,thread);
350 SetSymbolValue(STOP_FOR_GC_PENDING,NIL,thread);
351 sig_stop_for_gc_handler(SIG_STOP_FOR_GC,NULL,context);
354 if (SymbolValue(GC_PENDING,thread) != NIL) {
355 /* GC_PENDING is cleared in SUB-GC, or if another thread
356 * is doing a gc already we will get a SIG_STOP_FOR_GC and
357 * that will clear it. */
358 interrupt_maybe_gc_int(0,NULL,context);
360 check_blockables_blocked_or_lose();
363 /* we may be here only to do the gc stuff, if interrupts are
364 * enabled run the pending handler */
365 if (!((SymbolValue(INTERRUPTS_ENABLED,thread) == NIL) ||
367 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
368 (!foreign_function_call_active) &&
370 arch_pseudo_atomic_atomic(context)))) {
372 /* There may be no pending handler, because it was only a gc
373 * that had to be executed or because pseudo atomic triggered
374 * twice for a single interrupt. For the interested reader,
375 * that may happen if an interrupt hits after the interrupted
376 * flag is cleared but before pseduo-atomic is set and a
377 * pseudo atomic is interrupted in that interrupt. */
378 if (data->pending_handler) {
380 /* If we're here as the result of a pseudo-atomic as opposed
381 * to WITHOUT-INTERRUPTS, then INTERRUPT_PENDING is already
382 * NIL, because maybe_defer_handler sets
383 * PSEUDO_ATOMIC_INTERRUPTED only if interrupts are enabled.*/
384 SetSymbolValue(INTERRUPT_PENDING, NIL,thread);
386 /* restore the saved signal mask from the original signal (the
387 * one that interrupted us during the critical section) into the
388 * os_context for the signal we're currently in the handler for.
389 * This should ensure that when we return from the handler the
390 * blocked signals are unblocked */
391 sigcopyset(os_context_sigmask_addr(context), &data->pending_mask);
393 sigemptyset(&data->pending_mask);
394 /* This will break on sparc linux: the deferred handler really wants
395 * to be called with a void_context */
396 run_deferred_handler(data,(void *)context);
402 * the two main signal handlers:
403 * interrupt_handle_now(..)
404 * maybe_now_maybe_later(..)
406 * to which we have added interrupt_handle_now_handler(..). Why?
407 * Well, mostly because the SPARC/Linux platform doesn't quite do
408 * signals the way we want them done. The third argument in the
409 * handler isn't filled in by the kernel properly, so we fix it up
410 * ourselves in the arch_os_get_context(..) function; however, we only
411 * want to do this when we first hit the handler, and not when
412 * interrupt_handle_now(..) is being called from some other handler
413 * (when the fixup will already have been done). -- CSR, 2002-07-23
417 interrupt_handle_now(int signal, siginfo_t *info, void *void_context)
419 os_context_t *context = (os_context_t*)void_context;
420 struct thread *thread=arch_os_get_current_thread();
421 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
422 boolean were_in_lisp;
424 union interrupt_handler handler;
425 check_blockables_blocked_or_lose();
426 check_interrupts_enabled_or_lose(context);
428 #ifdef LISP_FEATURE_LINUX
429 /* Under Linux on some architectures, we appear to have to restore
430 the FPU control word from the context, as after the signal is
431 delivered we appear to have a null FPU control word. */
432 os_restore_fp_control(context);
434 handler = thread->interrupt_data->interrupt_handlers[signal];
436 if (ARE_SAME_HANDLER(handler.c, SIG_IGN)) {
440 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
441 were_in_lisp = !foreign_function_call_active;
445 fake_foreign_function_call(context);
448 FSHOW_SIGNAL((stderr,
449 "/entering interrupt_handle_now(%d, info, context)\n",
452 if (ARE_SAME_HANDLER(handler.c, SIG_DFL)) {
454 /* This can happen if someone tries to ignore or default one
455 * of the signals we need for runtime support, and the runtime
456 * support decides to pass on it. */
457 lose("no handler for signal %d in interrupt_handle_now(..)", signal);
459 } else if (lowtag_of(handler.lisp) == FUN_POINTER_LOWTAG) {
460 /* Once we've decided what to do about contexts in a
461 * return-elsewhere world (the original context will no longer
462 * be available; should we copy it or was nobody using it anyway?)
463 * then we should convert this to return-elsewhere */
465 /* CMUCL comment said "Allocate the SAPs while the interrupts
466 * are still disabled.". I (dan, 2003.08.21) assume this is
467 * because we're not in pseudoatomic and allocation shouldn't
468 * be interrupted. In which case it's no longer an issue as
469 * all our allocation from C now goes through a PA wrapper,
470 * but still, doesn't hurt */
472 lispobj info_sap,context_sap = alloc_sap(context);
473 info_sap = alloc_sap(info);
474 /* Allow signals again. */
475 thread_sigmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
477 FSHOW_SIGNAL((stderr,"/calling Lisp-level handler\n"));
479 funcall3(handler.lisp,
485 FSHOW_SIGNAL((stderr,"/calling C-level handler\n"));
487 /* Allow signals again. */
488 thread_sigmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
490 (*handler.c)(signal, info, void_context);
493 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
497 undo_fake_foreign_function_call(context); /* block signals again */
500 FSHOW_SIGNAL((stderr,
501 "/returning from interrupt_handle_now(%d, info, context)\n",
505 /* This is called at the end of a critical section if the indications
506 * are that some signal was deferred during the section. Note that as
507 * far as C or the kernel is concerned we dealt with the signal
508 * already; we're just doing the Lisp-level processing now that we
512 run_deferred_handler(struct interrupt_data *data, void *v_context) {
513 /* The pending_handler may enable interrupts and then another
514 * interrupt may hit, overwrite interrupt_data, so reset the
515 * pending handler before calling it. Trust the handler to finish
516 * with the siginfo before enabling interrupts. */
517 void (*pending_handler) (int, siginfo_t*, void*)=data->pending_handler;
518 data->pending_handler=0;
519 (*pending_handler)(data->pending_signal,&(data->pending_info), v_context);
523 maybe_defer_handler(void *handler, struct interrupt_data *data,
524 int signal, siginfo_t *info, os_context_t *context)
526 struct thread *thread=arch_os_get_current_thread();
528 check_blockables_blocked_or_lose();
530 if (SymbolValue(INTERRUPT_PENDING,thread) != NIL)
531 lose("interrupt already pending");
532 /* If interrupts are disabled then INTERRUPT_PENDING is set and
533 * not PSEDUO_ATOMIC_INTERRUPTED. This is important for a pseudo
534 * atomic section inside a WITHOUT-INTERRUPTS.
536 if (SymbolValue(INTERRUPTS_ENABLED,thread) == NIL) {
537 store_signal_data_for_later(data,handler,signal,info,context);
538 SetSymbolValue(INTERRUPT_PENDING, T,thread);
539 FSHOW_SIGNAL((stderr,
540 "/maybe_defer_handler(%x,%d),thread=%lu: deferred\n",
541 (unsigned int)handler,signal,
542 (unsigned long)thread->os_thread));
545 /* a slightly confusing test. arch_pseudo_atomic_atomic() doesn't
546 * actually use its argument for anything on x86, so this branch
547 * may succeed even when context is null (gencgc alloc()) */
549 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
550 /* FIXME: this foreign_function_call_active test is dubious at
551 * best. If a foreign call is made in a pseudo atomic section
552 * (?) or more likely a pseudo atomic section is in a foreign
553 * call then an interrupt is executed immediately. Maybe it
554 * has to do with C code not maintaining pseudo atomic
555 * properly. MG - 2005-08-10 */
556 (!foreign_function_call_active) &&
558 arch_pseudo_atomic_atomic(context)) {
559 store_signal_data_for_later(data,handler,signal,info,context);
560 arch_set_pseudo_atomic_interrupted(context);
561 FSHOW_SIGNAL((stderr,
562 "/maybe_defer_handler(%x,%d),thread=%lu: deferred(PA)\n",
563 (unsigned int)handler,signal,
564 (unsigned long)thread->os_thread));
567 FSHOW_SIGNAL((stderr,
568 "/maybe_defer_handler(%x,%d),thread=%lu: not deferred\n",
569 (unsigned int)handler,signal,
570 (unsigned long)thread->os_thread));
575 store_signal_data_for_later (struct interrupt_data *data, void *handler,
577 siginfo_t *info, os_context_t *context)
579 if (data->pending_handler)
580 lose("tried to overwrite pending interrupt handler %x with %x\n",
581 data->pending_handler, handler);
583 lose("tried to defer null interrupt handler\n");
584 data->pending_handler = handler;
585 data->pending_signal = signal;
587 memcpy(&(data->pending_info), info, sizeof(siginfo_t));
589 /* the signal mask in the context (from before we were
590 * interrupted) is copied to be restored when
591 * run_deferred_handler happens. Then the usually-blocked
592 * signals are added to the mask in the context so that we are
593 * running with blocked signals when the handler returns */
594 sigcopyset(&(data->pending_mask),os_context_sigmask_addr(context));
595 sigaddset_deferrable(os_context_sigmask_addr(context));
600 maybe_now_maybe_later(int signal, siginfo_t *info, void *void_context)
602 os_context_t *context = arch_os_get_context(&void_context);
603 struct thread *thread=arch_os_get_current_thread();
604 struct interrupt_data *data=thread->interrupt_data;
605 #ifdef LISP_FEATURE_LINUX
606 os_restore_fp_control(context);
608 if(maybe_defer_handler(interrupt_handle_now,data,
609 signal,info,context))
611 interrupt_handle_now(signal, info, context);
612 #ifdef LISP_FEATURE_DARWIN
613 /* Work around G5 bug */
614 DARWIN_FIX_CONTEXT(context);
619 low_level_interrupt_handle_now(int signal, siginfo_t *info, void *void_context)
621 os_context_t *context = (os_context_t*)void_context;
622 struct thread *thread=arch_os_get_current_thread();
623 struct interrupt_data *data=
624 thread ? thread->interrupt_data : global_interrupt_data;
626 #ifdef LISP_FEATURE_LINUX
627 os_restore_fp_control(context);
629 check_blockables_blocked_or_lose();
630 check_interrupts_enabled_or_lose(context);
631 (*data->interrupt_low_level_handlers[signal])
632 (signal, info, void_context);
633 #ifdef LISP_FEATURE_DARWIN
634 /* Work around G5 bug */
635 DARWIN_FIX_CONTEXT(context);
640 low_level_maybe_now_maybe_later(int signal, siginfo_t *info, void *void_context)
642 os_context_t *context = arch_os_get_context(&void_context);
643 struct thread *thread=arch_os_get_current_thread();
644 struct interrupt_data *data=
645 thread ? thread->interrupt_data : global_interrupt_data;
646 #ifdef LISP_FEATURE_LINUX
647 os_restore_fp_control(context);
649 if(maybe_defer_handler(low_level_interrupt_handle_now,data,
650 signal,info,context))
652 low_level_interrupt_handle_now(signal, info, context);
653 #ifdef LISP_FEATURE_DARWIN
654 /* Work around G5 bug */
655 DARWIN_FIX_CONTEXT(context);
659 #ifdef LISP_FEATURE_SB_THREAD
662 sig_stop_for_gc_handler(int signal, siginfo_t *info, void *void_context)
664 os_context_t *context = arch_os_get_context(&void_context);
665 struct thread *thread=arch_os_get_current_thread();
669 if ((arch_pseudo_atomic_atomic(context) ||
670 SymbolValue(GC_INHIBIT,thread) != NIL)) {
671 SetSymbolValue(STOP_FOR_GC_PENDING,T,thread);
672 if (SymbolValue(GC_INHIBIT,thread) == NIL)
673 arch_set_pseudo_atomic_interrupted(context);
674 FSHOW_SIGNAL((stderr,"thread=%lu sig_stop_for_gc deferred\n",
677 /* need the context stored so it can have registers scavenged */
678 fake_foreign_function_call(context);
681 for(i=1;i<NSIG;i++) sigaddset(&ss,i); /* Block everything. */
682 thread_sigmask(SIG_BLOCK,&ss,0);
684 /* The GC can't tell if a thread is a zombie, so this would be a
685 * good time to let the kernel reap any of our children in that
686 * awful state, to stop them from being waited for indefinitely.
687 * Userland reaping is done later when GC is finished */
688 if(thread->state!=STATE_RUNNING) {
689 lose("sig_stop_for_gc_handler: wrong thread state: %ld\n",
690 fixnum_value(thread->state));
692 thread->state=STATE_SUSPENDED;
693 FSHOW_SIGNAL((stderr,"thread=%lu suspended\n",thread->os_thread));
695 sigemptyset(&ss); sigaddset(&ss,SIG_STOP_FOR_GC);
697 FSHOW_SIGNAL((stderr,"thread=%lu resumed\n",thread->os_thread));
698 if(thread->state!=STATE_RUNNING) {
699 lose("sig_stop_for_gc_handler: wrong thread state on wakeup: %ld\n",
700 fixnum_value(thread->state));
703 undo_fake_foreign_function_call(context);
709 interrupt_handle_now_handler(int signal, siginfo_t *info, void *void_context)
711 os_context_t *context = arch_os_get_context(&void_context);
712 interrupt_handle_now(signal, info, context);
713 #ifdef LISP_FEATURE_DARWIN
714 DARWIN_FIX_CONTEXT(context);
719 * stuff to detect and handle hitting the GC trigger
722 #ifndef LISP_FEATURE_GENCGC
723 /* since GENCGC has its own way to record trigger */
725 gc_trigger_hit(int signal, siginfo_t *info, os_context_t *context)
727 if (current_auto_gc_trigger == NULL)
730 void *badaddr=arch_get_bad_addr(signal,info,context);
731 return (badaddr >= (void *)current_auto_gc_trigger &&
732 badaddr <((void *)current_dynamic_space + DYNAMIC_SPACE_SIZE));
737 /* manipulate the signal context and stack such that when the handler
738 * returns, it will call function instead of whatever it was doing
742 #if (defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
743 int *context_eflags_addr(os_context_t *context);
746 extern lispobj call_into_lisp(lispobj fun, lispobj *args, int nargs);
747 extern void post_signal_tramp(void);
748 void arrange_return_to_lisp_function(os_context_t *context, lispobj function)
750 #if !(defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
751 void * fun=native_pointer(function);
752 void *code = &(((struct simple_fun *) fun)->code);
755 /* Build a stack frame showing `interrupted' so that the
756 * user's backtrace makes (as much) sense (as usual) */
758 /* FIXME: what about restoring fp state? */
759 /* FIXME: what about restoring errno? */
760 #ifdef LISP_FEATURE_X86
761 /* Suppose the existence of some function that saved all
762 * registers, called call_into_lisp, then restored GP registers and
763 * returned. It would look something like this:
771 pushl {address of function to call}
772 call 0x8058db0 <call_into_lisp>
779 * What we do here is set up the stack that call_into_lisp would
780 * expect to see if it had been called by this code, and frob the
781 * signal context so that signal return goes directly to call_into_lisp,
782 * and when that function (and the lisp function it invoked) returns,
783 * it returns to the second half of this imaginary function which
784 * restores all registers and returns to C
786 * For this to work, the latter part of the imaginary function
787 * must obviously exist in reality. That would be post_signal_tramp
790 u32 *sp=(u32 *)*os_context_register_addr(context,reg_ESP);
792 /* return address for call_into_lisp: */
793 *(sp-15) = (u32)post_signal_tramp;
794 *(sp-14) = function; /* args for call_into_lisp : function*/
795 *(sp-13) = 0; /* arg array */
796 *(sp-12) = 0; /* no. args */
797 /* this order matches that used in POPAD */
798 *(sp-11)=*os_context_register_addr(context,reg_EDI);
799 *(sp-10)=*os_context_register_addr(context,reg_ESI);
801 *(sp-9)=*os_context_register_addr(context,reg_ESP)-8;
802 /* POPAD ignores the value of ESP: */
804 *(sp-7)=*os_context_register_addr(context,reg_EBX);
806 *(sp-6)=*os_context_register_addr(context,reg_EDX);
807 *(sp-5)=*os_context_register_addr(context,reg_ECX);
808 *(sp-4)=*os_context_register_addr(context,reg_EAX);
809 *(sp-3)=*context_eflags_addr(context);
810 *(sp-2)=*os_context_register_addr(context,reg_EBP);
811 *(sp-1)=*os_context_pc_addr(context);
813 #elif defined(LISP_FEATURE_X86_64)
814 u64 *sp=(u64 *)*os_context_register_addr(context,reg_RSP);
815 /* return address for call_into_lisp: */
816 *(sp-18) = (u64)post_signal_tramp;
818 *(sp-17)=*os_context_register_addr(context,reg_R15);
819 *(sp-16)=*os_context_register_addr(context,reg_R14);
820 *(sp-15)=*os_context_register_addr(context,reg_R13);
821 *(sp-14)=*os_context_register_addr(context,reg_R12);
822 *(sp-13)=*os_context_register_addr(context,reg_R11);
823 *(sp-12)=*os_context_register_addr(context,reg_R10);
824 *(sp-11)=*os_context_register_addr(context,reg_R9);
825 *(sp-10)=*os_context_register_addr(context,reg_R8);
826 *(sp-9)=*os_context_register_addr(context,reg_RDI);
827 *(sp-8)=*os_context_register_addr(context,reg_RSI);
828 /* skip RBP and RSP */
829 *(sp-7)=*os_context_register_addr(context,reg_RBX);
830 *(sp-6)=*os_context_register_addr(context,reg_RDX);
831 *(sp-5)=*os_context_register_addr(context,reg_RCX);
832 *(sp-4)=*os_context_register_addr(context,reg_RAX);
833 *(sp-3)=*context_eflags_addr(context);
834 *(sp-2)=*os_context_register_addr(context,reg_RBP);
835 *(sp-1)=*os_context_pc_addr(context);
837 *os_context_register_addr(context,reg_RDI) =
838 (os_context_register_t)function; /* function */
839 *os_context_register_addr(context,reg_RSI) = 0; /* arg. array */
840 *os_context_register_addr(context,reg_RDX) = 0; /* no. args */
842 struct thread *th=arch_os_get_current_thread();
843 build_fake_control_stack_frames(th,context);
846 #ifdef LISP_FEATURE_X86
847 *os_context_pc_addr(context) = (os_context_register_t)call_into_lisp;
848 *os_context_register_addr(context,reg_ECX) = 0;
849 *os_context_register_addr(context,reg_EBP) = (os_context_register_t)(sp-2);
851 *os_context_register_addr(context,reg_UESP) =
852 (os_context_register_t)(sp-15);
854 *os_context_register_addr(context,reg_ESP) = (os_context_register_t)(sp-15);
856 #elif defined(LISP_FEATURE_X86_64)
857 *os_context_pc_addr(context) = (os_context_register_t)call_into_lisp;
858 *os_context_register_addr(context,reg_RCX) = 0;
859 *os_context_register_addr(context,reg_RBP) = (os_context_register_t)(sp-2);
860 *os_context_register_addr(context,reg_RSP) = (os_context_register_t)(sp-18);
862 /* this much of the calling convention is common to all
864 *os_context_pc_addr(context) = (os_context_register_t)code;
865 *os_context_register_addr(context,reg_NARGS) = 0;
866 *os_context_register_addr(context,reg_LIP) = (os_context_register_t)code;
867 *os_context_register_addr(context,reg_CFP) =
868 (os_context_register_t)current_control_frame_pointer;
870 #ifdef ARCH_HAS_NPC_REGISTER
871 *os_context_npc_addr(context) =
872 4 + *os_context_pc_addr(context);
874 #ifdef LISP_FEATURE_SPARC
875 *os_context_register_addr(context,reg_CODE) =
876 (os_context_register_t)(fun + FUN_POINTER_LOWTAG);
880 #ifdef LISP_FEATURE_SB_THREAD
881 void interrupt_thread_handler(int num, siginfo_t *info, void *v_context)
883 os_context_t *context = (os_context_t*)arch_os_get_context(&v_context);
884 /* The order of interrupt execution is peculiar. If thread A
885 * interrupts thread B with I1, I2 and B for some reason receives
886 * I1 when FUN2 is already on the list, then it is FUN2 that gets
887 * to run first. But when FUN2 is run SIG_INTERRUPT_THREAD is
888 * enabled again and I2 hits pretty soon in FUN2 and run
889 * FUN1. This is of course just one scenario, and the order of
890 * thread interrupt execution is undefined. */
891 struct thread *th=arch_os_get_current_thread();
894 if (th->state != STATE_RUNNING)
895 lose("interrupt_thread_handler: thread %lu in wrong state: %d\n",
896 th->os_thread,fixnum_value(th->state));
897 get_spinlock(&th->interrupt_fun_lock,(long)th);
898 c=((struct cons *)native_pointer(th->interrupt_fun));
900 th->interrupt_fun=c->cdr;
901 release_spinlock(&th->interrupt_fun_lock);
903 lose("interrupt_thread_handler: NIL function\n");
904 arrange_return_to_lisp_function(context,function);
909 /* KLUDGE: Theoretically the approach we use for undefined alien
910 * variables should work for functions as well, but on PPC/Darwin
911 * we get bus error at bogus addresses instead, hence this workaround,
912 * that has the added benefit of automatically discriminating between
913 * functions and variables.
915 void undefined_alien_function() {
916 funcall0(SymbolFunction(UNDEFINED_ALIEN_FUNCTION_ERROR));
919 boolean handle_guard_page_triggered(os_context_t *context,os_vm_address_t addr)
921 struct thread *th=arch_os_get_current_thread();
923 /* note the os_context hackery here. When the signal handler returns,
924 * it won't go back to what it was doing ... */
925 if(addr >= CONTROL_STACK_GUARD_PAGE(th) &&
926 addr < CONTROL_STACK_GUARD_PAGE(th) + os_vm_page_size) {
927 /* We hit the end of the control stack: disable guard page
928 * protection so the error handler has some headroom, protect the
929 * previous page so that we can catch returns from the guard page
931 protect_control_stack_guard_page(th,0);
932 protect_control_stack_return_guard_page(th,1);
934 arrange_return_to_lisp_function
935 (context, SymbolFunction(CONTROL_STACK_EXHAUSTED_ERROR));
938 else if(addr >= CONTROL_STACK_RETURN_GUARD_PAGE(th) &&
939 addr < CONTROL_STACK_RETURN_GUARD_PAGE(th) + os_vm_page_size) {
940 /* We're returning from the guard page: reprotect it, and
941 * unprotect this one. This works even if we somehow missed
942 * the return-guard-page, and hit it on our way to new
943 * exhaustion instead. */
944 protect_control_stack_guard_page(th,1);
945 protect_control_stack_return_guard_page(th,0);
948 else if (addr >= undefined_alien_address &&
949 addr < undefined_alien_address + os_vm_page_size) {
950 arrange_return_to_lisp_function
951 (context, SymbolFunction(UNDEFINED_ALIEN_VARIABLE_ERROR));
957 #ifndef LISP_FEATURE_GENCGC
958 /* This function gets called from the SIGSEGV (for e.g. Linux, NetBSD, &
959 * OpenBSD) or SIGBUS (for e.g. FreeBSD) handler. Here we check
960 * whether the signal was due to treading on the mprotect()ed zone -
961 * and if so, arrange for a GC to happen. */
962 extern unsigned long bytes_consed_between_gcs; /* gc-common.c */
965 interrupt_maybe_gc(int signal, siginfo_t *info, void *void_context)
967 os_context_t *context=(os_context_t *) void_context;
968 struct thread *th=arch_os_get_current_thread();
969 struct interrupt_data *data=
970 th ? th->interrupt_data : global_interrupt_data;
972 if(!foreign_function_call_active && gc_trigger_hit(signal, info, context)){
973 struct thread *thread=arch_os_get_current_thread();
974 clear_auto_gc_trigger();
975 /* Don't flood the system with interrupts if the need to gc is
976 * already noted. This can happen for example when SUB-GC
977 * allocates or after a gc triggered in a WITHOUT-GCING. */
978 if (SymbolValue(GC_PENDING,thread) == NIL) {
979 if (SymbolValue(GC_INHIBIT,thread) == NIL) {
980 if (arch_pseudo_atomic_atomic(context)) {
981 /* set things up so that GC happens when we finish
983 SetSymbolValue(GC_PENDING,T,thread);
984 arch_set_pseudo_atomic_interrupted(context);
986 interrupt_maybe_gc_int(signal,info,void_context);
989 SetSymbolValue(GC_PENDING,T,thread);
999 /* this is also used by gencgc, in alloc() */
1001 interrupt_maybe_gc_int(int signal, siginfo_t *info, void *void_context)
1003 os_context_t *context=(os_context_t *) void_context;
1004 struct thread *thread=arch_os_get_current_thread();
1006 check_blockables_blocked_or_lose();
1007 fake_foreign_function_call(context);
1009 /* SUB-GC may return without GCing if *GC-INHIBIT* is set, in
1010 * which case we will be running with no gc trigger barrier
1011 * thing for a while. But it shouldn't be long until the end
1014 * FIXME: It would be good to protect the end of dynamic space
1015 * and signal a storage condition from there.
1018 /* Restore the signal mask from the interrupted context before
1019 * calling into Lisp if interrupts are enabled. Why not always?
1021 * Suppose there is a WITHOUT-INTERRUPTS block far, far out. If an
1022 * interrupt hits while in SUB-GC, it is deferred and the
1023 * os_context_sigmask of that interrupt is set to block further
1024 * deferrable interrupts (until the first one is
1025 * handled). Unfortunately, that context refers to this place and
1026 * when we return from here the signals will not be blocked.
1028 * A kludgy alternative is to propagate the sigmask change to the
1031 if(SymbolValue(INTERRUPTS_ENABLED,thread)!=NIL)
1032 thread_sigmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
1033 #ifdef LISP_FEATURE_SB_THREAD
1036 sigaddset(&new,SIG_STOP_FOR_GC);
1037 thread_sigmask(SIG_UNBLOCK,&new,0);
1040 funcall0(SymbolFunction(SUB_GC));
1042 undo_fake_foreign_function_call(context);
1048 * noise to install handlers
1052 undoably_install_low_level_interrupt_handler (int signal,
1057 struct sigaction sa;
1058 struct thread *th=arch_os_get_current_thread();
1059 struct interrupt_data *data=
1060 th ? th->interrupt_data : global_interrupt_data;
1062 if (0 > signal || signal >= NSIG) {
1063 lose("bad signal number %d", signal);
1066 if (sigismember(&deferrable_sigset,signal))
1067 sa.sa_sigaction = low_level_maybe_now_maybe_later;
1069 sa.sa_sigaction = handler;
1071 sigemptyset(&sa.sa_mask);
1072 sigaddset_blockable(&sa.sa_mask);
1073 sa.sa_flags = SA_SIGINFO | SA_RESTART;
1074 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
1075 if((signal==SIG_MEMORY_FAULT)
1076 #ifdef SIG_INTERRUPT_THREAD
1077 || (signal==SIG_INTERRUPT_THREAD)
1080 sa.sa_flags|= SA_ONSTACK;
1083 sigaction(signal, &sa, NULL);
1084 data->interrupt_low_level_handlers[signal] =
1085 (ARE_SAME_HANDLER(handler, SIG_DFL) ? 0 : handler);
1088 /* This is called from Lisp. */
1090 install_handler(int signal, void handler(int, siginfo_t*, void*))
1092 struct sigaction sa;
1094 union interrupt_handler oldhandler;
1095 struct thread *th=arch_os_get_current_thread();
1096 struct interrupt_data *data=
1097 th ? th->interrupt_data : global_interrupt_data;
1099 FSHOW((stderr, "/entering POSIX install_handler(%d, ..)\n", signal));
1102 sigaddset(&new, signal);
1103 thread_sigmask(SIG_BLOCK, &new, &old);
1105 FSHOW((stderr, "/data->interrupt_low_level_handlers[signal]=%x\n",
1106 (unsigned int)data->interrupt_low_level_handlers[signal]));
1107 if (data->interrupt_low_level_handlers[signal]==0) {
1108 if (ARE_SAME_HANDLER(handler, SIG_DFL) ||
1109 ARE_SAME_HANDLER(handler, SIG_IGN)) {
1110 sa.sa_sigaction = handler;
1111 } else if (sigismember(&deferrable_sigset, signal)) {
1112 sa.sa_sigaction = maybe_now_maybe_later;
1114 sa.sa_sigaction = interrupt_handle_now_handler;
1117 sigemptyset(&sa.sa_mask);
1118 sigaddset_blockable(&sa.sa_mask);
1119 sa.sa_flags = SA_SIGINFO | SA_RESTART;
1120 sigaction(signal, &sa, NULL);
1123 oldhandler = data->interrupt_handlers[signal];
1124 data->interrupt_handlers[signal].c = handler;
1126 thread_sigmask(SIG_SETMASK, &old, 0);
1128 FSHOW((stderr, "/leaving POSIX install_handler(%d, ..)\n", signal));
1130 return (unsigned long)oldhandler.lisp;
1137 SHOW("entering interrupt_init()");
1138 sigemptyset(&deferrable_sigset);
1139 sigemptyset(&blockable_sigset);
1140 sigaddset_deferrable(&deferrable_sigset);
1141 sigaddset_blockable(&blockable_sigset);
1143 global_interrupt_data=calloc(sizeof(struct interrupt_data), 1);
1145 /* Set up high level handler information. */
1146 for (i = 0; i < NSIG; i++) {
1147 global_interrupt_data->interrupt_handlers[i].c =
1148 /* (The cast here blasts away the distinction between
1149 * SA_SIGACTION-style three-argument handlers and
1150 * signal(..)-style one-argument handlers, which is OK
1151 * because it works to call the 1-argument form where the
1152 * 3-argument form is expected.) */
1153 (void (*)(int, siginfo_t*, void*))SIG_DFL;
1156 SHOW("returning from interrupt_init()");