Back to index

glibc  2.9
trampoline.c
Go to the documentation of this file.
00001 /* Set thread_state for sighandler, and sigcontext to recover.  MIPS version.
00002    Copyright (C) 1996, 1997, 1998, 2005 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <hurd/signal.h>
00021 #include <hurd/userlink.h>
00022 #include <thread_state.h>
00023 #include <assert.h>
00024 #include <errno.h>
00025 #include "hurdfault.h"
00026 #include <intr-msg.h>
00027 
00028 
00029 struct sigcontext *
00030 _hurd_setup_sighandler (struct hurd_sigstate *ss, __sighandler_t handler,
00031                      int signo, struct hurd_signal_detail *detail,
00032                      volatile int rpc_wait,
00033                      struct machine_thread_all_state *state)
00034 {
00035   __label__ trampoline, rpc_wait_trampoline, firewall;
00036   void *volatile sigsp;
00037   struct sigcontext *scp;
00038   struct
00039     {
00040       int signo;
00041       long int sigcode;
00042       struct sigcontext *scp;      /* Points to ctx, below.  */
00043       void *sigreturn_addr;
00044       void *sigreturn_returns_here;
00045       struct sigcontext *return_scp; /* Same; arg to sigreturn.  */
00046       struct sigcontext ctx;
00047       struct hurd_userlink link;
00048     } *stackframe;
00049 
00050   if (ss->context)
00051     {
00052       /* We have a previous sigcontext that sigreturn was about
00053         to restore when another signal arrived.  We will just base
00054         our setup on that.  */
00055       if (! _hurdsig_catch_memory_fault (ss->context))
00056        {
00057          memcpy (&state->basic, &ss->context->sc_mips_thread_state,
00058                 sizeof (state->basic));
00059          memcpy (&state->exc, &ss->context->sc_mips_exc_state,
00060                 sizeof (state->exc));
00061          state->set = (1 << MIPS_THREAD_STATE) | (1 << MIPS_EXC_STATE);
00062          if (state->exc.coproc_state & SC_COPROC_USE_FPU)
00063            {
00064              memcpy (&state->fpu, &ss->context->sc_mips_float_state,
00065                     sizeof (state->fpu));
00066              state->set |= (1 << MIPS_FLOAT_STATE);
00067            }
00068        }
00069     }
00070 
00071   if (! machine_get_basic_state (ss->thread, state))
00072     return NULL;
00073 
00074   /* Save the original SP in the gratuitous s0 ($16) slot.
00075      We may need to reset the SP (the `r29' slot) to avoid clobbering an
00076      interrupted RPC frame.  */
00077   state->basic.r16 = state->basic.r29;
00078 
00079   if ((ss->actions[signo].sa_flags & SA_ONSTACK) &&
00080       !(ss->sigaltstack.ss_flags & (SS_DISABLE|SS_ONSTACK)))
00081     {
00082       sigsp = ss->sigaltstack.ss_sp + ss->sigaltstack.ss_size;
00083       ss->sigaltstack.ss_flags |= SS_ONSTACK;
00084       /* XXX need to set up base of new stack for
00085         per-thread variables, cthreads.  */
00086     }
00087   else
00088     sigsp = (char *) state->basic.r29;
00089 
00090   /* Push the arguments to call `trampoline' on the stack.  */
00091   sigsp -= sizeof (*stackframe);
00092   stackframe = sigsp;
00093 
00094   if (_hurdsig_catch_memory_fault (stackframe))
00095     {
00096       /* We got a fault trying to write the stack frame.
00097         We cannot set up the signal handler.
00098         Returning NULL tells our caller, who will nuke us with a SIGILL.  */
00099       return NULL;
00100     }
00101   else
00102     {
00103       int ok;
00104 
00105       extern void _hurdsig_longjmp_from_handler (void *, jmp_buf, int);
00106 
00107       /* Add a link to the thread's active-resources list.  We mark this as
00108         the only user of the "resource", so the cleanup function will be
00109         called by any longjmp which is unwinding past the signal frame.
00110         The cleanup function (in sigunwind.c) will make sure that all the
00111         appropriate cleanups done by sigreturn are taken care of.  */
00112       stackframe->link.cleanup = &_hurdsig_longjmp_from_handler;
00113       stackframe->link.cleanup_data = &stackframe->ctx;
00114       stackframe->link.resource.next = NULL;
00115       stackframe->link.resource.prevp = NULL;
00116       stackframe->link.thread.next = ss->active_resources;
00117       stackframe->link.thread.prevp = &ss->active_resources;
00118       if (stackframe->link.thread.next)
00119        stackframe->link.thread.next->thread.prevp
00120          = &stackframe->link.thread.next;
00121       ss->active_resources = &stackframe->link;
00122 
00123       /* Set up the arguments for the signal handler.  */
00124       stackframe->signo = signo;
00125       stackframe->sigcode = detail->code;
00126       stackframe->scp = stackframe->return_scp = scp = &stackframe->ctx;
00127       stackframe->sigreturn_addr = &__sigreturn;
00128       stackframe->sigreturn_returns_here = &&firewall; /* Crash on return.  */
00129 
00130       /* Set up the sigcontext from the current state of the thread.  */
00131 
00132       scp->sc_onstack = ss->sigaltstack.ss_flags & SS_ONSTACK ? 1 : 0;
00133 
00134       /* struct sigcontext is laid out so that starting at sc_gpr
00135         mimics a struct mips_thread_state.  */
00136       memcpy (&scp->sc_mips_thread_state,
00137              &state->basic, sizeof (state->basic));
00138 
00139       /* struct sigcontext is laid out so that starting at sc_cause
00140         mimics a struct mips_exc_state.  */
00141       ok = machine_get_state (ss->thread, state, MIPS_EXC_STATE,
00142                            &state->exc, &scp->sc_cause,
00143                            sizeof (state->exc));
00144 
00145       if (ok && (scp->sc_coproc_used & SC_COPROC_USE_FPU))
00146        /* struct sigcontext is laid out so that starting at sc_fpr
00147           mimics a struct mips_float_state.  This state
00148           is only meaningful if the coprocessor was used.  */
00149          ok = machine_get_state (ss->thread, state, MIPS_FLOAT_STATE,
00150                               &state->fpu, &scp->sc_mips_float_state,
00151                               sizeof (state->fpu));
00152 
00153       _hurdsig_end_catch_fault ();
00154 
00155       if (! ok)
00156        return NULL;
00157     }
00158 
00159   /* Modify the thread state to call the trampoline code on the new stack.  */
00160   if (rpc_wait)
00161     {
00162       /* The signalee thread was blocked in a mach_msg_trap system call,
00163         still waiting for a reply.  We will have it run the special
00164         trampoline code which retries the message receive before running
00165         the signal handler.
00166 
00167         To do this we change the OPTION argument in its registers to
00168         enable only message reception, since the request message has
00169         already been sent.  */
00170 
00171       /* The system call arguments are stored in consecutive registers
00172         starting with a0 ($4).  */
00173       struct mach_msg_trap_args *args = (void *) &state->basic.r4;
00174 
00175       if (_hurdsig_catch_memory_fault (args))
00176        {
00177          /* Faulted accessing ARGS.  Bomb.  */
00178          return NULL;
00179        }
00180 
00181       assert (args->option & MACH_RCV_MSG);
00182       /* Disable the message-send, since it has already completed.  The
00183         calls we retry need only wait to receive the reply message.  */
00184       args->option &= ~MACH_SEND_MSG;
00185 
00186       /* Limit the time to receive the reply message, in case the server
00187         claimed that `interrupt_operation' succeeded but in fact the RPC
00188         is hung.  */
00189       args->option |= MACH_RCV_TIMEOUT;
00190       args->timeout = _hurd_interrupted_rpc_timeout;
00191 
00192       _hurdsig_end_catch_fault ();
00193 
00194       state->basic.pc = (int) &&rpc_wait_trampoline;
00195       /* The reply-receiving trampoline code runs initially on the original
00196         user stack.  We pass it the signal stack pointer in s4 ($20).  */
00197       state->basic.r29 = state->basic.r16; /* Restore mach_msg syscall SP.  */
00198       state->basic.r20 = (int) sigsp;
00199       /* After doing the message receive, the trampoline code will need to
00200         update the v0 ($2) value to be restored by sigreturn.  To simplify
00201         the assembly code, we pass the address of its slot in SCP to the
00202         trampoline code in s5 ($21).  */
00203       state->basic.r21 = (int) &scp->sc_gpr[1];
00204       /* We must preserve the mach_msg_trap args in a0..t2 ($4..$10).
00205         Pass the handler args to the trampoline code in s1..s3 ($17..$19).  */
00206       state->basic.r17 = signo;
00207       state->basic.r18 = detail->code;
00208       state->basic.r19 = (int) scp;
00209     }
00210   else
00211     {
00212       state->basic.pc = (int) &&trampoline;
00213       state->basic.r29 = (int) sigsp;
00214       state->basic.r4 = signo;
00215       state->basic.r5 = detail->code;
00216       state->basic.r6 = (int) scp;
00217     }
00218 
00219   /* We pass the handler function to the trampoline code in s6 ($22).  */
00220   state->basic.r22 = (int) handler;
00221   /* In the callee-saved register s0 ($16), we save the SCP value to pass
00222      to __sigreturn after the handler returns.  */
00223   state->basic.r16 = (int) scp;
00224 
00225   return scp;
00226 
00227   /* The trampoline code follows.  This is not actually executed as part of
00228      this function, it is just convenient to write it that way.  */
00229 
00230  rpc_wait_trampoline:
00231   /* This is the entry point when we have an RPC reply message to receive
00232      before running the handler.  The MACH_MSG_SEND bit has already been
00233      cleared in the OPTION argument in our registers.  For our convenience,
00234      $3 points to the sc_gpr[1] member of the sigcontext (saved v0 ($2)).  */
00235   asm volatile
00236     (".set noat; .set noreorder; .set nomacro\n"
00237      /* Retry the interrupted mach_msg system call.  */
00238 #ifdef __mips64
00239      "dli $2, -25\n"        /* mach_msg_trap */
00240 #else
00241      "li $2, -25\n"         /* mach_msg_trap */
00242 #endif
00243      "syscall\n"
00244      /* When the sigcontext was saved, v0 was MACH_RCV_INTERRUPTED.  But
00245        now the message receive has completed and the original caller of
00246        the RPC (i.e. the code running when the signal arrived) needs to
00247        see the final return value of the message receive in v0.  So
00248        store the new v0 value into the sc_gpr[1] member of the sigcontext
00249        (whose address is in s5 to make this code simpler).  */
00250 #ifdef __mips64
00251      "sd $2, ($21)\n"
00252 #else
00253      "sw $2, ($21)\n"
00254 #endif
00255      /* Since the argument registers needed to have the mach_msg_trap
00256        arguments, we've stored the arguments to the handler function
00257        in registers s1..s3 ($17..$19).  */
00258      "move $4, $17\n"
00259      "move $5, $18\n"
00260      "move $6, $19\n"
00261      /* Switch to the signal stack.  */
00262      "move $29, $20\n");
00263 
00264  trampoline:
00265   /* Entry point for running the handler normally.  The arguments to the
00266      handler function are already in the standard registers:
00267 
00268        a0     SIGNO
00269        a1     SIGCODE
00270        a2     SCP
00271      */
00272   asm volatile
00273     ("move $25, $22\n"             /* Copy s6 to t9 for MIPS ABI.  */
00274      "jal $25; nop\n"              /* Call the handler function.  */
00275      /* Call __sigreturn (SCP); this cannot return.  */
00276 #ifdef __mips64
00277      "dla $1,%0\n"
00278 #else
00279      "la $1,%0\n"
00280 #endif
00281      "j $1\n"
00282      "move $4, $16"         /* Set up arg from saved SCP in delay slot.  */
00283      : : "i" (&__sigreturn));
00284 
00285   /* NOTREACHED */
00286   asm volatile (".set reorder; .set at; .set macro");
00287 
00288  firewall:
00289   asm volatile ("hlt: j hlt");
00290 
00291   return NULL;
00292 }