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 extern volatile lispobj all_threads_lock;
80 void sigaddset_blockable(sigset_t *s)
84 sigaddset(s, SIGQUIT);
85 sigaddset(s, SIGPIPE);
86 sigaddset(s, SIGALRM);
89 sigaddset(s, SIGTSTP);
90 sigaddset(s, SIGCHLD);
92 sigaddset(s, SIGXCPU);
93 sigaddset(s, SIGXFSZ);
94 sigaddset(s, SIGVTALRM);
95 sigaddset(s, SIGPROF);
96 sigaddset(s, SIGWINCH);
97 sigaddset(s, SIGUSR1);
98 sigaddset(s, SIGUSR2);
99 #ifdef LISP_FEATURE_SB_THREAD
100 sigaddset(s, SIG_STOP_FOR_GC);
101 sigaddset(s, SIG_INTERRUPT_THREAD);
105 static sigset_t blockable_sigset;
107 inline static void check_blockables_blocked_or_lose()
109 /* Get the current sigmask, by blocking the empty set. */
110 sigset_t empty,current;
113 thread_sigmask(SIG_BLOCK, &empty, ¤t);
114 for(i=0;i<NSIG;i++) {
115 if (sigismember(&blockable_sigset, i) && !sigismember(¤t, i))
116 lose("blockable signal %d not blocked",i);
120 inline static void check_interrupts_enabled_or_lose(os_context_t *context)
122 struct thread *thread=arch_os_get_current_thread();
123 if (SymbolValue(INTERRUPTS_ENABLED,thread) == NIL)
124 lose("interrupts not enabled");
126 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
127 (!foreign_function_call_active) &&
129 arch_pseudo_atomic_atomic(context))
130 lose ("in pseudo atomic section");
133 /* When we catch an internal error, should we pass it back to Lisp to
134 * be handled in a high-level way? (Early in cold init, the answer is
135 * 'no', because Lisp is still too brain-dead to handle anything.
136 * After sufficient initialization has been completed, the answer
138 boolean internal_errors_enabled = 0;
140 struct interrupt_data * global_interrupt_data;
142 /* At the toplevel repl we routinely call this function. The signal
143 * mask ought to be clear anyway most of the time, but may be non-zero
144 * if we were interrupted e.g. while waiting for a queue. */
146 void reset_signal_mask ()
150 thread_sigmask(SIG_SETMASK,&new,0);
157 * utility routines used by various signal handlers
161 build_fake_control_stack_frames(struct thread *th,os_context_t *context)
163 #ifndef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
167 /* Build a fake stack frame or frames */
169 current_control_frame_pointer =
170 (lispobj *)(*os_context_register_addr(context, reg_CSP));
171 if ((lispobj *)(*os_context_register_addr(context, reg_CFP))
172 == current_control_frame_pointer) {
173 /* There is a small window during call where the callee's
174 * frame isn't built yet. */
175 if (lowtag_of(*os_context_register_addr(context, reg_CODE))
176 == FUN_POINTER_LOWTAG) {
177 /* We have called, but not built the new frame, so
178 * build it for them. */
179 current_control_frame_pointer[0] =
180 *os_context_register_addr(context, reg_OCFP);
181 current_control_frame_pointer[1] =
182 *os_context_register_addr(context, reg_LRA);
183 current_control_frame_pointer += 8;
184 /* Build our frame on top of it. */
185 oldcont = (lispobj)(*os_context_register_addr(context, reg_CFP));
188 /* We haven't yet called, build our frame as if the
189 * partial frame wasn't there. */
190 oldcont = (lispobj)(*os_context_register_addr(context, reg_OCFP));
193 /* We can't tell whether we are still in the caller if it had to
194 * allocate a stack frame due to stack arguments. */
195 /* This observation provoked some past CMUCL maintainer to ask
196 * "Can anything strange happen during return?" */
199 oldcont = (lispobj)(*os_context_register_addr(context, reg_CFP));
202 current_control_stack_pointer = current_control_frame_pointer + 8;
204 current_control_frame_pointer[0] = oldcont;
205 current_control_frame_pointer[1] = NIL;
206 current_control_frame_pointer[2] =
207 (lispobj)(*os_context_register_addr(context, reg_CODE));
212 fake_foreign_function_call(os_context_t *context)
215 struct thread *thread=arch_os_get_current_thread();
217 /* context_index incrementing must not be interrupted */
218 check_blockables_blocked_or_lose();
220 /* Get current Lisp state from context. */
222 dynamic_space_free_pointer =
223 (lispobj *)(*os_context_register_addr(context, reg_ALLOC));
224 #if defined(LISP_FEATURE_ALPHA)
225 if ((long)dynamic_space_free_pointer & 1) {
226 lose("dead in fake_foreign_function_call, context = %x", context);
231 current_binding_stack_pointer =
232 (lispobj *)(*os_context_register_addr(context, reg_BSP));
235 build_fake_control_stack_frames(thread,context);
237 /* Do dynamic binding of the active interrupt context index
238 * and save the context in the context array. */
240 fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,thread));
242 if (context_index >= MAX_INTERRUPTS) {
243 lose("maximum interrupt nesting depth (%d) exceeded", MAX_INTERRUPTS);
246 bind_variable(FREE_INTERRUPT_CONTEXT_INDEX,
247 make_fixnum(context_index + 1),thread);
249 thread->interrupt_contexts[context_index] = context;
251 /* no longer in Lisp now */
252 foreign_function_call_active = 1;
255 /* blocks all blockable signals. If you are calling from a signal handler,
256 * the usual signal mask will be restored from the context when the handler
257 * finishes. Otherwise, be careful */
260 undo_fake_foreign_function_call(os_context_t *context)
262 struct thread *thread=arch_os_get_current_thread();
263 /* Block all blockable signals. */
266 sigaddset_blockable(&block);
267 thread_sigmask(SIG_BLOCK, &block, 0);
269 /* going back into Lisp */
270 foreign_function_call_active = 0;
272 /* Undo dynamic binding of FREE_INTERRUPT_CONTEXT_INDEX */
276 /* Put the dynamic space free pointer back into the context. */
277 *os_context_register_addr(context, reg_ALLOC) =
278 (unsigned long) dynamic_space_free_pointer;
282 /* a handler for the signal caused by execution of a trap opcode
283 * signalling an internal error */
285 interrupt_internal_error(int signal, siginfo_t *info, os_context_t *context,
288 lispobj context_sap = 0;
290 check_blockables_blocked_or_lose();
291 fake_foreign_function_call(context);
293 /* Allocate the SAP object while the interrupts are still
295 if (internal_errors_enabled) {
296 context_sap = alloc_sap(context);
299 thread_sigmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
301 if (internal_errors_enabled) {
302 SHOW("in interrupt_internal_error");
304 /* Display some rudimentary debugging information about the
305 * error, so that even if the Lisp error handler gets badly
306 * confused, we have a chance to determine what's going on. */
307 describe_internal_error(context);
309 funcall2(SymbolFunction(INTERNAL_ERROR), context_sap,
310 continuable ? T : NIL);
312 describe_internal_error(context);
313 /* There's no good way to recover from an internal error
314 * before the Lisp error handling mechanism is set up. */
315 lose("internal error too early in init, can't recover");
317 undo_fake_foreign_function_call(context); /* blocks signals again */
319 arch_skip_instruction(context);
324 interrupt_handle_pending(os_context_t *context)
326 struct thread *thread;
327 struct interrupt_data *data;
329 check_blockables_blocked_or_lose();
330 check_interrupts_enabled_or_lose(context);
332 thread=arch_os_get_current_thread();
333 data=thread->interrupt_data;
335 /* Pseudo atomic may trigger several times for a single interrupt,
336 * and while without-interrupts should not, a false trigger by
337 * pseudo-atomic may eat a pending handler even from
338 * without-interrupts. */
339 if (data->pending_handler) {
341 /* If we're here as the result of a pseudo-atomic as opposed
342 * to WITHOUT-INTERRUPTS, then INTERRUPT_PENDING is already
343 * NIL, because maybe_defer_handler sets
344 * PSEUDO_ATOMIC_INTERRUPTED only if interrupts are enabled.*/
345 SetSymbolValue(INTERRUPT_PENDING, NIL,thread);
347 /* restore the saved signal mask from the original signal (the
348 * one that interrupted us during the critical section) into the
349 * os_context for the signal we're currently in the handler for.
350 * This should ensure that when we return from the handler the
351 * blocked signals are unblocked */
352 sigcopyset(os_context_sigmask_addr(context), &data->pending_mask);
354 sigemptyset(&data->pending_mask);
355 /* This will break on sparc linux: the deferred handler really wants
356 * to be called with a void_context */
357 run_deferred_handler(data,(void *)context);
362 * the two main signal handlers:
363 * interrupt_handle_now(..)
364 * maybe_now_maybe_later(..)
366 * to which we have added interrupt_handle_now_handler(..). Why?
367 * Well, mostly because the SPARC/Linux platform doesn't quite do
368 * signals the way we want them done. The third argument in the
369 * handler isn't filled in by the kernel properly, so we fix it up
370 * ourselves in the arch_os_get_context(..) function; however, we only
371 * want to do this when we first hit the handler, and not when
372 * interrupt_handle_now(..) is being called from some other handler
373 * (when the fixup will already have been done). -- CSR, 2002-07-23
377 interrupt_handle_now(int signal, siginfo_t *info, void *void_context)
379 os_context_t *context = (os_context_t*)void_context;
380 struct thread *thread=arch_os_get_current_thread();
381 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
382 boolean were_in_lisp;
384 union interrupt_handler handler;
385 check_blockables_blocked_or_lose();
386 check_interrupts_enabled_or_lose(context);
388 #ifdef LISP_FEATURE_LINUX
389 /* Under Linux on some architectures, we appear to have to restore
390 the FPU control word from the context, as after the signal is
391 delivered we appear to have a null FPU control word. */
392 os_restore_fp_control(context);
394 handler = thread->interrupt_data->interrupt_handlers[signal];
396 if (ARE_SAME_HANDLER(handler.c, SIG_IGN)) {
400 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
401 were_in_lisp = !foreign_function_call_active;
405 fake_foreign_function_call(context);
410 "/entering interrupt_handle_now(%d, info, context)\n",
414 if (ARE_SAME_HANDLER(handler.c, SIG_DFL)) {
416 /* This can happen if someone tries to ignore or default one
417 * of the signals we need for runtime support, and the runtime
418 * support decides to pass on it. */
419 lose("no handler for signal %d in interrupt_handle_now(..)", signal);
421 } else if (lowtag_of(handler.lisp) == FUN_POINTER_LOWTAG) {
422 /* Once we've decided what to do about contexts in a
423 * return-elsewhere world (the original context will no longer
424 * be available; should we copy it or was nobody using it anyway?)
425 * then we should convert this to return-elsewhere */
427 /* CMUCL comment said "Allocate the SAPs while the interrupts
428 * are still disabled.". I (dan, 2003.08.21) assume this is
429 * because we're not in pseudoatomic and allocation shouldn't
430 * be interrupted. In which case it's no longer an issue as
431 * all our allocation from C now goes through a PA wrapper,
432 * but still, doesn't hurt */
434 lispobj info_sap,context_sap = alloc_sap(context);
435 info_sap = alloc_sap(info);
436 /* Allow signals again. */
437 thread_sigmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
440 SHOW("calling Lisp-level handler");
443 funcall3(handler.lisp,
450 SHOW("calling C-level handler");
453 /* Allow signals again. */
454 thread_sigmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
456 (*handler.c)(signal, info, void_context);
459 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
463 undo_fake_foreign_function_call(context); /* block signals again */
468 "/returning from interrupt_handle_now(%d, info, context)\n",
473 /* This is called at the end of a critical section if the indications
474 * are that some signal was deferred during the section. Note that as
475 * far as C or the kernel is concerned we dealt with the signal
476 * already; we're just doing the Lisp-level processing now that we
480 run_deferred_handler(struct interrupt_data *data, void *v_context) {
481 /* The pending_handler may enable interrupts (see
482 * interrupt_maybe_gc_int) and then another interrupt may hit,
483 * overwrite interrupt_data, so reset the pending handler before
484 * calling it. Trust the handler to finish with the siginfo before
485 * enabling interrupts. */
486 void (*pending_handler) (int, siginfo_t*, void*)=data->pending_handler;
487 data->pending_handler=0;
488 (*pending_handler)(data->pending_signal,&(data->pending_info), v_context);
492 maybe_defer_handler(void *handler, struct interrupt_data *data,
493 int signal, siginfo_t *info, os_context_t *context)
495 struct thread *thread=arch_os_get_current_thread();
497 check_blockables_blocked_or_lose();
499 if (SymbolValue(INTERRUPT_PENDING,thread) != NIL)
500 lose("interrupt already pending");
501 /* If interrupts are disabled then INTERRUPT_PENDING is set and
502 * not PSEDUO_ATOMIC_INTERRUPTED. This is important for a pseudo
503 * atomic section inside a without-interrupts.
505 if (SymbolValue(INTERRUPTS_ENABLED,thread) == NIL) {
506 store_signal_data_for_later(data,handler,signal,info,context);
507 SetSymbolValue(INTERRUPT_PENDING, T,thread);
510 "/maybe_defer_handler(%x,%d),thread=%ld: deferred\n",
511 (unsigned int)handler,signal,thread->os_thread));
515 /* a slightly confusing test. arch_pseudo_atomic_atomic() doesn't
516 * actually use its argument for anything on x86, so this branch
517 * may succeed even when context is null (gencgc alloc()) */
519 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
520 (!foreign_function_call_active) &&
522 arch_pseudo_atomic_atomic(context)) {
523 store_signal_data_for_later(data,handler,signal,info,context);
524 arch_set_pseudo_atomic_interrupted(context);
527 "/maybe_defer_handler(%x,%d),thread=%ld: deferred(PA)\n",
528 (unsigned int)handler,signal,thread->os_thread));
534 "/maybe_defer_handler(%x,%d),thread=%ld: not deferred\n",
535 (unsigned int)handler,signal,thread->os_thread));
541 store_signal_data_for_later (struct interrupt_data *data, void *handler,
543 siginfo_t *info, os_context_t *context)
545 if (data->pending_handler)
546 lose("tried to overwrite pending interrupt handler %x with %x\n",
547 data->pending_handler, handler);
549 lose("tried to defer null interrupt handler\n");
550 data->pending_handler = handler;
551 data->pending_signal = signal;
553 memcpy(&(data->pending_info), info, sizeof(siginfo_t));
555 /* the signal mask in the context (from before we were
556 * interrupted) is copied to be restored when
557 * run_deferred_handler happens. Then the usually-blocked
558 * signals are added to the mask in the context so that we are
559 * running with blocked signals when the handler returns */
560 sigcopyset(&(data->pending_mask),os_context_sigmask_addr(context));
561 sigaddset_blockable(os_context_sigmask_addr(context));
566 maybe_now_maybe_later(int signal, siginfo_t *info, void *void_context)
568 os_context_t *context = arch_os_get_context(&void_context);
569 struct thread *thread=arch_os_get_current_thread();
570 struct interrupt_data *data=thread->interrupt_data;
571 #ifdef LISP_FEATURE_LINUX
572 os_restore_fp_control(context);
574 if(maybe_defer_handler(interrupt_handle_now,data,
575 signal,info,context))
577 interrupt_handle_now(signal, info, context);
578 #ifdef LISP_FEATURE_DARWIN
579 /* Work around G5 bug */
580 DARWIN_FIX_CONTEXT(context);
585 low_level_interrupt_handle_now(int signal, siginfo_t *info, void *void_context)
587 os_context_t *context = (os_context_t*)void_context;
588 struct thread *thread=arch_os_get_current_thread();
590 #ifdef LISP_FEATURE_LINUX
591 os_restore_fp_control(context);
593 check_blockables_blocked_or_lose();
594 check_interrupts_enabled_or_lose(context);
595 (*thread->interrupt_data->interrupt_low_level_handlers[signal])
596 (signal, info, void_context);
597 #ifdef LISP_FEATURE_DARWIN
598 /* Work around G5 bug */
599 DARWIN_FIX_CONTEXT(context);
604 low_level_maybe_now_maybe_later(int signal, siginfo_t *info, void *void_context)
606 os_context_t *context = arch_os_get_context(&void_context);
607 struct thread *thread=arch_os_get_current_thread();
608 struct interrupt_data *data=thread->interrupt_data;
609 #ifdef LISP_FEATURE_LINUX
610 os_restore_fp_control(context);
612 if(maybe_defer_handler(low_level_interrupt_handle_now,data,
613 signal,info,context))
615 low_level_interrupt_handle_now(signal, info, context);
616 #ifdef LISP_FEATURE_DARWIN
617 /* Work around G5 bug */
618 DARWIN_FIX_CONTEXT(context);
622 #ifdef LISP_FEATURE_SB_THREAD
624 sig_stop_for_gc_handler(int signal, siginfo_t *info, void *void_context)
626 os_context_t *context = arch_os_get_context(&void_context);
627 struct thread *thread=arch_os_get_current_thread();
631 /* need the context stored so it can have registers scavenged */
632 fake_foreign_function_call(context);
635 for(i=1;i<NSIG;i++) sigaddset(&ss,i); /* Block everything. */
636 thread_sigmask(SIG_BLOCK,&ss,0);
638 /* The GC can't tell if a thread is a zombie, so this would be a
639 * good time to let the kernel reap any of our children in that
640 * awful state, to stop them from being waited for indefinitely.
641 * Userland reaping is done later when GC is finished */
642 if(thread->state!=STATE_STOPPING) {
643 lose("sig_stop_for_gc_handler: wrong thread state: %ld\n",
644 fixnum_value(thread->state));
646 thread->state=STATE_STOPPED;
648 sigemptyset(&ss); sigaddset(&ss,SIG_STOP_FOR_GC);
650 if(thread->state!=STATE_STOPPED) {
651 lose("sig_stop_for_gc_handler: wrong thread state on wakeup: %ld\n",
652 fixnum_value(thread->state));
654 thread->state=STATE_RUNNING;
656 undo_fake_foreign_function_call(context);
661 interrupt_handle_now_handler(int signal, siginfo_t *info, void *void_context)
663 os_context_t *context = arch_os_get_context(&void_context);
664 interrupt_handle_now(signal, info, context);
665 #ifdef LISP_FEATURE_DARWIN
666 DARWIN_FIX_CONTEXT(context);
671 * stuff to detect and handle hitting the GC trigger
674 #ifndef LISP_FEATURE_GENCGC
675 /* since GENCGC has its own way to record trigger */
677 gc_trigger_hit(int signal, siginfo_t *info, os_context_t *context)
679 if (current_auto_gc_trigger == NULL)
682 void *badaddr=arch_get_bad_addr(signal,info,context);
683 return (badaddr >= (void *)current_auto_gc_trigger &&
684 badaddr <((void *)current_dynamic_space + DYNAMIC_SPACE_SIZE));
689 /* manipulate the signal context and stack such that when the handler
690 * returns, it will call function instead of whatever it was doing
694 #if (defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
695 int *context_eflags_addr(os_context_t *context);
698 extern lispobj call_into_lisp(lispobj fun, lispobj *args, int nargs);
699 extern void post_signal_tramp(void);
700 void arrange_return_to_lisp_function(os_context_t *context, lispobj function)
702 #if !(defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
703 void * fun=native_pointer(function);
704 void *code = &(((struct simple_fun *) fun)->code);
707 /* Build a stack frame showing `interrupted' so that the
708 * user's backtrace makes (as much) sense (as usual) */
710 /* FIXME: what about restoring fp state? */
711 /* FIXME: what about restoring errno? */
712 #ifdef LISP_FEATURE_X86
713 /* Suppose the existence of some function that saved all
714 * registers, called call_into_lisp, then restored GP registers and
715 * returned. It would look something like this:
723 pushl {address of function to call}
724 call 0x8058db0 <call_into_lisp>
731 * What we do here is set up the stack that call_into_lisp would
732 * expect to see if it had been called by this code, and frob the
733 * signal context so that signal return goes directly to call_into_lisp,
734 * and when that function (and the lisp function it invoked) returns,
735 * it returns to the second half of this imaginary function which
736 * restores all registers and returns to C
738 * For this to work, the latter part of the imaginary function
739 * must obviously exist in reality. That would be post_signal_tramp
742 u32 *sp=(u32 *)*os_context_register_addr(context,reg_ESP);
744 *(sp-15) = post_signal_tramp; /* return address for call_into_lisp */
745 *(sp-14) = function; /* args for call_into_lisp : function*/
746 *(sp-13) = 0; /* arg array */
747 *(sp-12) = 0; /* no. args */
748 /* this order matches that used in POPAD */
749 *(sp-11)=*os_context_register_addr(context,reg_EDI);
750 *(sp-10)=*os_context_register_addr(context,reg_ESI);
752 *(sp-9)=*os_context_register_addr(context,reg_ESP)-8;
753 /* POPAD ignores the value of ESP: */
755 *(sp-7)=*os_context_register_addr(context,reg_EBX);
757 *(sp-6)=*os_context_register_addr(context,reg_EDX);
758 *(sp-5)=*os_context_register_addr(context,reg_ECX);
759 *(sp-4)=*os_context_register_addr(context,reg_EAX);
760 *(sp-3)=*context_eflags_addr(context);
761 *(sp-2)=*os_context_register_addr(context,reg_EBP);
762 *(sp-1)=*os_context_pc_addr(context);
764 #elif defined(LISP_FEATURE_X86_64)
765 u64 *sp=(u64 *)*os_context_register_addr(context,reg_RSP);
766 *(sp-20) = post_signal_tramp; /* return address for call_into_lisp */
768 *(sp-19)=*os_context_register_addr(context,reg_R15);
769 *(sp-18)=*os_context_register_addr(context,reg_R14);
770 *(sp-17)=*os_context_register_addr(context,reg_R13);
771 *(sp-16)=*os_context_register_addr(context,reg_R12);
772 *(sp-15)=*os_context_register_addr(context,reg_R11);
773 *(sp-14)=*os_context_register_addr(context,reg_R10);
774 *(sp-13)=*os_context_register_addr(context,reg_R9);
775 *(sp-12)=*os_context_register_addr(context,reg_R8);
776 *(sp-11)=*os_context_register_addr(context,reg_RDI);
777 *(sp-10)=*os_context_register_addr(context,reg_RSI);
778 *(sp-9)=*os_context_register_addr(context,reg_RSP)-16;
780 *(sp-7)=*os_context_register_addr(context,reg_RBX);
781 *(sp-6)=*os_context_register_addr(context,reg_RDX);
782 *(sp-5)=*os_context_register_addr(context,reg_RCX);
783 *(sp-4)=*os_context_register_addr(context,reg_RAX);
784 *(sp-3)=*context_eflags_addr(context);
785 *(sp-2)=*os_context_register_addr(context,reg_RBP);
786 *(sp-1)=*os_context_pc_addr(context);
788 *os_context_register_addr(context,reg_RDI) = function; /* function */
789 *os_context_register_addr(context,reg_RSI) = 0; /* arg. array */
790 *os_context_register_addr(context,reg_RDX) = 0; /* no. args */
792 struct thread *th=arch_os_get_current_thread();
793 build_fake_control_stack_frames(th,context);
796 #ifdef LISP_FEATURE_X86
797 *os_context_pc_addr(context) = call_into_lisp;
798 *os_context_register_addr(context,reg_ECX) = 0;
799 *os_context_register_addr(context,reg_EBP) = sp-2;
801 *os_context_register_addr(context,reg_UESP) = sp-15;
803 *os_context_register_addr(context,reg_ESP) = sp-15;
805 #elif defined(LISP_FEATURE_X86_64)
806 *os_context_pc_addr(context) = call_into_lisp;
807 *os_context_register_addr(context,reg_RCX) = 0;
808 *os_context_register_addr(context,reg_RBP) = sp-2;
809 *os_context_register_addr(context,reg_RSP) = sp-20;
811 /* this much of the calling convention is common to all
813 *os_context_pc_addr(context) = code;
814 *os_context_register_addr(context,reg_NARGS) = 0;
815 *os_context_register_addr(context,reg_LIP) = code;
816 *os_context_register_addr(context,reg_CFP) =
817 current_control_frame_pointer;
819 #ifdef ARCH_HAS_NPC_REGISTER
820 *os_context_npc_addr(context) =
821 4 + *os_context_pc_addr(context);
823 #ifdef LISP_FEATURE_SPARC
824 *os_context_register_addr(context,reg_CODE) =
825 fun + FUN_POINTER_LOWTAG;
829 #ifdef LISP_FEATURE_SB_THREAD
830 void interrupt_thread_handler(int num, siginfo_t *info, void *v_context)
832 os_context_t *context = (os_context_t*)arch_os_get_context(&v_context);
833 /* The order of interrupt execution is peculiar. If thread A
834 * interrupts thread B with I1, I2 and B for some reason recieves
835 * I1 when FUN2 is already on the list, then it is FUN2 that gets
836 * to run first. But when FUN2 is run SIG_INTERRUPT_THREAD is
837 * enabled again and I2 hits pretty soon in FUN2 and run
838 * FUN1. This is of course just one scenario, and the order of
839 * thread interrupt execution is undefined. */
840 struct thread *th=arch_os_get_current_thread();
842 get_spinlock(&th->interrupt_fun_lock,(long)th);
843 c=((struct cons *)native_pointer(th->interrupt_fun));
844 arrange_return_to_lisp_function(context,c->car);
845 th->interrupt_fun=(lispobj *)(c->cdr);
846 release_spinlock(&th->interrupt_fun_lock);
851 /* KLUDGE: Theoretically the approach we use for undefined alien
852 * variables should work for functions as well, but on PPC/Darwin
853 * we get bus error at bogus addresses instead, hence this workaround,
854 * that has the added benefit of automatically discriminating between
855 * functions and variables.
857 void undefined_alien_function() {
858 funcall0(SymbolFunction(UNDEFINED_ALIEN_FUNCTION_ERROR));
861 boolean handle_guard_page_triggered(os_context_t *context,void *addr){
862 struct thread *th=arch_os_get_current_thread();
864 /* note the os_context hackery here. When the signal handler returns,
865 * it won't go back to what it was doing ... */
866 if(addr >= CONTROL_STACK_GUARD_PAGE(th) &&
867 addr < CONTROL_STACK_GUARD_PAGE(th) + os_vm_page_size) {
868 /* We hit the end of the control stack: disable guard page
869 * protection so the error handler has some headroom, protect the
870 * previous page so that we can catch returns from the guard page
872 protect_control_stack_guard_page(th->os_thread,0);
873 protect_control_stack_return_guard_page(th->os_thread,1);
875 arrange_return_to_lisp_function
876 (context, SymbolFunction(CONTROL_STACK_EXHAUSTED_ERROR));
879 else if(addr >= CONTROL_STACK_RETURN_GUARD_PAGE(th) &&
880 addr < CONTROL_STACK_RETURN_GUARD_PAGE(th) + os_vm_page_size) {
881 /* We're returning from the guard page: reprotect it, and
882 * unprotect this one. This works even if we somehow missed
883 * the return-guard-page, and hit it on our way to new
884 * exhaustion instead. */
885 protect_control_stack_guard_page(th->os_thread,1);
886 protect_control_stack_return_guard_page(th->os_thread,0);
889 else if (addr >= undefined_alien_address &&
890 addr < undefined_alien_address + os_vm_page_size) {
891 arrange_return_to_lisp_function
892 (context, SymbolFunction(UNDEFINED_ALIEN_VARIABLE_ERROR));
898 #ifndef LISP_FEATURE_GENCGC
899 /* This function gets called from the SIGSEGV (for e.g. Linux, NetBSD, &
900 * OpenBSD) or SIGBUS (for e.g. FreeBSD) handler. Here we check
901 * whether the signal was due to treading on the mprotect()ed zone -
902 * and if so, arrange for a GC to happen. */
903 extern unsigned long bytes_consed_between_gcs; /* gc-common.c */
906 interrupt_maybe_gc(int signal, siginfo_t *info, void *void_context)
908 os_context_t *context=(os_context_t *) void_context;
909 struct thread *th=arch_os_get_current_thread();
910 struct interrupt_data *data=
911 th ? th->interrupt_data : global_interrupt_data;
913 if(!data->pending_handler && !foreign_function_call_active &&
914 gc_trigger_hit(signal, info, context)){
915 clear_auto_gc_trigger();
916 if(!maybe_defer_handler(interrupt_maybe_gc_int,
917 data,signal,info,void_context))
918 interrupt_maybe_gc_int(signal,info,void_context);
926 /* this is also used by gencgc, in alloc() */
928 interrupt_maybe_gc_int(int signal, siginfo_t *info, void *void_context)
930 os_context_t *context=(os_context_t *) void_context;
932 check_blockables_blocked_or_lose();
933 fake_foreign_function_call(context);
935 /* SUB-GC may return without GCing if *GC-INHIBIT* is set, in
936 * which case we will be running with no gc trigger barrier
937 * thing for a while. But it shouldn't be long until the end
940 * FIXME: It would be good to protect the end of dynamic space
941 * and signal a storage condition from there.
944 /* restore the signal mask from the interrupted context before
945 * calling into Lisp */
947 thread_sigmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
949 funcall0(SymbolFunction(SUB_GC));
951 undo_fake_foreign_function_call(context);
957 * noise to install handlers
961 undoably_install_low_level_interrupt_handler (int signal,
967 struct thread *th=arch_os_get_current_thread();
968 struct interrupt_data *data=
969 th ? th->interrupt_data : global_interrupt_data;
971 if (0 > signal || signal >= NSIG) {
972 lose("bad signal number %d", signal);
975 if (sigismember(&blockable_sigset,signal))
976 sa.sa_sigaction = low_level_maybe_now_maybe_later;
978 sa.sa_sigaction = handler;
980 sigemptyset(&sa.sa_mask);
981 sigaddset_blockable(&sa.sa_mask);
982 sa.sa_flags = SA_SIGINFO | SA_RESTART;
983 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
984 if((signal==SIG_MEMORY_FAULT)
985 #ifdef SIG_INTERRUPT_THREAD
986 || (signal==SIG_INTERRUPT_THREAD)
989 sa.sa_flags|= SA_ONSTACK;
992 sigaction(signal, &sa, NULL);
993 data->interrupt_low_level_handlers[signal] =
994 (ARE_SAME_HANDLER(handler, SIG_DFL) ? 0 : handler);
997 /* This is called from Lisp. */
999 install_handler(int signal, void handler(int, siginfo_t*, void*))
1001 struct sigaction sa;
1003 union interrupt_handler oldhandler;
1004 struct thread *th=arch_os_get_current_thread();
1005 struct interrupt_data *data=
1006 th ? th->interrupt_data : global_interrupt_data;
1008 FSHOW((stderr, "/entering POSIX install_handler(%d, ..)\n", signal));
1011 sigaddset(&new, signal);
1012 thread_sigmask(SIG_BLOCK, &new, &old);
1015 sigaddset_blockable(&new);
1017 FSHOW((stderr, "/data->interrupt_low_level_handlers[signal]=%x\n",
1018 (unsigned int)data->interrupt_low_level_handlers[signal]));
1019 if (data->interrupt_low_level_handlers[signal]==0) {
1020 if (ARE_SAME_HANDLER(handler, SIG_DFL) ||
1021 ARE_SAME_HANDLER(handler, SIG_IGN)) {
1022 sa.sa_sigaction = handler;
1023 } else if (sigismember(&new, signal)) {
1024 sa.sa_sigaction = maybe_now_maybe_later;
1026 sa.sa_sigaction = interrupt_handle_now_handler;
1029 sigemptyset(&sa.sa_mask);
1030 sigaddset_blockable(&sa.sa_mask);
1031 sa.sa_flags = SA_SIGINFO | SA_RESTART;
1032 sigaction(signal, &sa, NULL);
1035 oldhandler = data->interrupt_handlers[signal];
1036 data->interrupt_handlers[signal].c = handler;
1038 thread_sigmask(SIG_SETMASK, &old, 0);
1040 FSHOW((stderr, "/leaving POSIX install_handler(%d, ..)\n", signal));
1042 return (unsigned long)oldhandler.lisp;
1049 SHOW("entering interrupt_init()");
1050 sigemptyset(&blockable_sigset);
1051 sigaddset_blockable(&blockable_sigset);
1053 global_interrupt_data=calloc(sizeof(struct interrupt_data), 1);
1055 /* Set up high level handler information. */
1056 for (i = 0; i < NSIG; i++) {
1057 global_interrupt_data->interrupt_handlers[i].c =
1058 /* (The cast here blasts away the distinction between
1059 * SA_SIGACTION-style three-argument handlers and
1060 * signal(..)-style one-argument handlers, which is OK
1061 * because it works to call the 1-argument form where the
1062 * 3-argument form is expected.) */
1063 (void (*)(int, siginfo_t*, void*))SIG_DFL;
1066 SHOW("returning from interrupt_init()");