+/* manipulate the signal context and stack such that when the handler
+ * returns, it will call function instead of whatever it was doing
+ * previously
+ */
+
+extern lispobj call_into_lisp(lispobj fun, lispobj *args, int nargs);
+extern void post_signal_tramp(void);
+void arrange_return_to_lisp_function(os_context_t *context, lispobj function)
+{
+ void * fun=native_pointer(function);
+ char *code = &(((struct simple_fun *) fun)->code);
+
+ /* Build a stack frame showing `interrupted' so that the
+ * user's backtrace makes (as much) sense (as usual) */
+#ifdef LISP_FEATURE_X86
+ /* Suppose the existence of some function that saved all
+ * registers, called call_into_lisp, then restored GP registers and
+ * returned. We shortcut this: fake the stack that call_into_lisp
+ * would see, then arrange to have it called directly. post_signal_tramp
+ * is the second half of this function
+ */
+ u32 *sp=(u32 *)*os_context_register_addr(context,reg_ESP);
+
+ *(sp-14) = post_signal_tramp; /* return address for call_into_lisp */
+ *(sp-13) = function; /* args for call_into_lisp : function*/
+ *(sp-12) = 0; /* arg array */
+ *(sp-11) = 0; /* no. args */
+ /* this order matches that used in POPAD */
+ *(sp-10)=*os_context_register_addr(context,reg_EDI);
+ *(sp-9)=*os_context_register_addr(context,reg_ESI);
+ /* this gets overwritten again before it's used, anyway */
+ *(sp-8)=*os_context_register_addr(context,reg_EBP);
+ *(sp-7)=0 ; /* POPAD doesn't set ESP, but expects a gap for it anyway */
+ *(sp-6)=*os_context_register_addr(context,reg_EBX);
+
+ *(sp-5)=*os_context_register_addr(context,reg_EDX);
+ *(sp-4)=*os_context_register_addr(context,reg_ECX);
+ *(sp-3)=*os_context_register_addr(context,reg_EAX);
+ *(sp-2)=*os_context_register_addr(context,reg_EBP);
+ *(sp-1)=*os_context_pc_addr(context);
+
+#else
+ struct thread *th=arch_os_get_current_thread();
+ build_fake_control_stack_frames(th,context);
+#endif
+
+#ifdef LISP_FEATURE_X86
+ *os_context_pc_addr(context) = call_into_lisp;
+ *os_context_register_addr(context,reg_ECX) = 0;
+ *os_context_register_addr(context,reg_EBP) = sp-2;
+ *os_context_register_addr(context,reg_ESP) = sp-14;
+#else
+ /* this much of the calling convention is common to all
+ non-x86 ports */
+ *os_context_pc_addr(context) = code;
+ *os_context_register_addr(context,reg_NARGS) = 0;
+ *os_context_register_addr(context,reg_LIP) = code;
+ *os_context_register_addr(context,reg_CFP) =
+ current_control_frame_pointer;
+#endif
+#ifdef ARCH_HAS_NPC_REGISTER
+ *os_context_npc_addr(context) =
+ 4 + *os_context_pc_addr(context);
+#endif
+#ifdef LISP_FEATURE_SPARC
+ *os_context_register_addr(context,reg_CODE) =
+ fun + FUN_POINTER_LOWTAG;
+#endif
+}
+
+#ifdef LISP_FEATURE_SB_THREAD
+void interrupt_thread_handler(int num, siginfo_t *info, void *v_context)
+{
+ os_context_t *context = (os_context_t*)arch_os_get_context(&v_context);
+ struct thread *th=arch_os_get_current_thread();
+ struct interrupt_data *data=
+ th ? th->interrupt_data : global_interrupt_data;
+ if(maybe_defer_handler(interrupt_thread_handler,data,num,info,context)){
+ return ;