+#if !(defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
+ void * fun=native_pointer(function);
+ void *code = &(((struct simple_fun *) fun)->code);
+#endif
+
+ /* Build a stack frame showing `interrupted' so that the
+ * user's backtrace makes (as much) sense (as usual) */
+
+ /* FIXME: what about restoring fp state? */
+ /* FIXME: what about restoring errno? */
+#ifdef LISP_FEATURE_X86
+ /* Suppose the existence of some function that saved all
+ * registers, called call_into_lisp, then restored GP registers and
+ * returned. It would look something like this:
+
+ push ebp
+ mov ebp esp
+ pushfl
+ pushal
+ push $0
+ push $0
+ pushl {address of function to call}
+ call 0x8058db0 <call_into_lisp>
+ addl $12,%esp
+ popal
+ popfl
+ leave
+ ret
+
+ * What we do here is set up the stack that call_into_lisp would
+ * expect to see if it had been called by this code, and frob the
+ * signal context so that signal return goes directly to call_into_lisp,
+ * and when that function (and the lisp function it invoked) returns,
+ * it returns to the second half of this imaginary function which
+ * restores all registers and returns to C
+
+ * For this to work, the latter part of the imaginary function
+ * must obviously exist in reality. That would be post_signal_tramp
+ */
+
+ u32 *sp=(u32 *)*os_context_register_addr(context,reg_ESP);
+
+#if defined(LISP_FEATURE_DARWIN)
+ u32 *register_save_area = (u32 *)os_validate(0, 0x40);
+
+ FSHOW_SIGNAL((stderr, "/arrange_return_to_lisp_function: preparing to go to function %x, sp: %x\n", function, sp));
+ FSHOW_SIGNAL((stderr, "/arrange_return_to_lisp_function: context: %x, &context %x\n", context, &context));
+
+ /* 1. os_validate (malloc/mmap) register_save_block
+ * 2. copy register state into register_save_block
+ * 3. put a pointer to register_save_block in a register in the context
+ * 4. set the context's EIP to point to a trampoline which:
+ * a. builds the fake stack frame from the block
+ * b. frees the block
+ * c. calls the function
+ */
+
+ *register_save_area = *os_context_pc_addr(context);
+ *(register_save_area + 1) = function;
+ *(register_save_area + 2) = *os_context_register_addr(context,reg_EDI);
+ *(register_save_area + 3) = *os_context_register_addr(context,reg_ESI);
+ *(register_save_area + 4) = *os_context_register_addr(context,reg_EDX);
+ *(register_save_area + 5) = *os_context_register_addr(context,reg_ECX);
+ *(register_save_area + 6) = *os_context_register_addr(context,reg_EBX);
+ *(register_save_area + 7) = *os_context_register_addr(context,reg_EAX);
+ *(register_save_area + 8) = *context_eflags_addr(context);
+
+ *os_context_pc_addr(context) = call_into_lisp_tramp;
+ *os_context_register_addr(context,reg_ECX) = register_save_area;
+#else
+
+ /* return address for call_into_lisp: */
+ *(sp-15) = (u32)post_signal_tramp;
+ *(sp-14) = function; /* args for call_into_lisp : function*/
+ *(sp-13) = 0; /* arg array */
+ *(sp-12) = 0; /* no. args */
+ /* this order matches that used in POPAD */
+ *(sp-11)=*os_context_register_addr(context,reg_EDI);
+ *(sp-10)=*os_context_register_addr(context,reg_ESI);
+
+ *(sp-9)=*os_context_register_addr(context,reg_ESP)-8;
+ /* POPAD ignores the value of ESP: */
+ *(sp-8)=0;
+ *(sp-7)=*os_context_register_addr(context,reg_EBX);
+
+ *(sp-6)=*os_context_register_addr(context,reg_EDX);
+ *(sp-5)=*os_context_register_addr(context,reg_ECX);
+ *(sp-4)=*os_context_register_addr(context,reg_EAX);
+ *(sp-3)=*context_eflags_addr(context);
+ *(sp-2)=*os_context_register_addr(context,reg_EBP);
+ *(sp-1)=*os_context_pc_addr(context);
+
+#endif
+
+#elif defined(LISP_FEATURE_X86_64)
+ u64 *sp=(u64 *)*os_context_register_addr(context,reg_RSP);
+
+ /* return address for call_into_lisp: */
+ *(sp-18) = (u64)post_signal_tramp;
+
+ *(sp-17)=*os_context_register_addr(context,reg_R15);
+ *(sp-16)=*os_context_register_addr(context,reg_R14);
+ *(sp-15)=*os_context_register_addr(context,reg_R13);
+ *(sp-14)=*os_context_register_addr(context,reg_R12);
+ *(sp-13)=*os_context_register_addr(context,reg_R11);
+ *(sp-12)=*os_context_register_addr(context,reg_R10);
+ *(sp-11)=*os_context_register_addr(context,reg_R9);
+ *(sp-10)=*os_context_register_addr(context,reg_R8);
+ *(sp-9)=*os_context_register_addr(context,reg_RDI);
+ *(sp-8)=*os_context_register_addr(context,reg_RSI);
+ /* skip RBP and RSP */
+ *(sp-7)=*os_context_register_addr(context,reg_RBX);
+ *(sp-6)=*os_context_register_addr(context,reg_RDX);
+ *(sp-5)=*os_context_register_addr(context,reg_RCX);
+ *(sp-4)=*os_context_register_addr(context,reg_RAX);
+ *(sp-3)=*context_eflags_addr(context);
+ *(sp-2)=*os_context_register_addr(context,reg_RBP);
+ *(sp-1)=*os_context_pc_addr(context);
+
+ *os_context_register_addr(context,reg_RDI) =
+ (os_context_register_t)function; /* function */
+ *os_context_register_addr(context,reg_RSI) = 0; /* arg. array */
+ *os_context_register_addr(context,reg_RDX) = 0; /* no. args */
+#else
+ struct thread *th=arch_os_get_current_thread();
+ build_fake_control_stack_frames(th,context);
+#endif
+
+#ifdef LISP_FEATURE_X86
+
+#if !defined(LISP_FEATURE_DARWIN)
+ *os_context_pc_addr(context) = (os_context_register_t)call_into_lisp;
+ *os_context_register_addr(context,reg_ECX) = 0;
+ *os_context_register_addr(context,reg_EBP) = (os_context_register_t)(sp-2);
+#ifdef __NetBSD__
+ *os_context_register_addr(context,reg_UESP) =
+ (os_context_register_t)(sp-15);
+#else
+ *os_context_register_addr(context,reg_ESP) = (os_context_register_t)(sp-15);
+#endif /* __NETBSD__ */
+#endif /* LISP_FEATURE_DARWIN */
+
+#elif defined(LISP_FEATURE_X86_64)
+ *os_context_pc_addr(context) = (os_context_register_t)call_into_lisp;
+ *os_context_register_addr(context,reg_RCX) = 0;
+ *os_context_register_addr(context,reg_RBP) = (os_context_register_t)(sp-2);
+ *os_context_register_addr(context,reg_RSP) = (os_context_register_t)(sp-18);
+#else
+ /* this much of the calling convention is common to all
+ non-x86 ports */
+ *os_context_pc_addr(context) = (os_context_register_t)(unsigned long)code;
+ *os_context_register_addr(context,reg_NARGS) = 0;
+ *os_context_register_addr(context,reg_LIP) =
+ (os_context_register_t)(unsigned long)code;
+ *os_context_register_addr(context,reg_CFP) =
+ (os_context_register_t)(unsigned long)current_control_frame_pointer;
+#endif