Back to index

glibc  2.9
__sigstack_longjmp.c
Go to the documentation of this file.
00001 /* Copyright (C) 2004 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by David Mosberger-Tang <davidm@hpl.hp.com>.
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 /* The public __longjmp() implementation is limited to jumping within
00021    the same stack.  That is, in general it is not possible to use this
00022    __longjmp() implementation to cross from one stack to another.
00023    In constrast, the __sigstack_longjmp() implemented here allows
00024    crossing from the alternate signal stack to the normal stack
00025    as a special case.  */
00026 
00027 #include <assert.h>
00028 #include <setjmp.h>
00029 #include <signal.h>
00030 #include <stdint.h>
00031 #include <stdlib.h>
00032 
00033 #include <sysdep.h>
00034 #include <sys/rse.h>
00035 
00036 #define JB_SP 0
00037 #define JB_BSP       17
00038 
00039 struct rbs_flush_values
00040   {
00041     unsigned long bsp;
00042     unsigned long rsc;
00043     unsigned long rnat;
00044   };
00045 
00046 extern struct rbs_flush_values __ia64_flush_rbs (void);
00047 extern void __ia64_longjmp (__jmp_buf buf, int val, long rnat, long rsc)
00048      __attribute__ ((__noreturn__));
00049 
00050 static void
00051 copy_rbs (unsigned long *dst, unsigned long *dst_end, unsigned long dst_rnat,
00052          unsigned long *src, unsigned long *src_end,
00053          unsigned long current_rnat)
00054 {
00055   unsigned long dst_slot, src_rnat = 0, src_slot, *src_rnat_addr, nat_bit;
00056   int first_time = 1;
00057 
00058   while (dst < dst_end)
00059     {
00060       dst_slot = ia64_rse_slot_num (dst);
00061       if (dst_slot == 63)
00062        {
00063          *dst++ = dst_rnat;
00064          dst_rnat = 0;
00065        }
00066       else
00067        {
00068          /* read source value, including NaT bit: */
00069          src_slot = ia64_rse_slot_num (src);
00070          if (src_slot == 63)
00071            {
00072              /* skip src RNaT slot */
00073              ++src;
00074              src_slot = 0;
00075            }
00076          if (first_time || src_slot == 0)
00077            {
00078              first_time = 0;
00079              src_rnat_addr = ia64_rse_rnat_addr (src);
00080              if (src_rnat_addr < src_end)
00081               src_rnat = *src_rnat_addr;
00082              else
00083               src_rnat = current_rnat;
00084            }
00085          nat_bit = (src_rnat >> src_slot) & 1;
00086 
00087          assert (src < src_end);
00088 
00089          *dst++ = *src++;
00090          if (nat_bit)
00091            dst_rnat |=  (1UL << dst_slot);
00092          else
00093            dst_rnat &= ~(1UL << dst_slot);
00094        }
00095     }
00096   dst_slot = ia64_rse_slot_num (dst);
00097   if (dst_slot > 0)
00098     *ia64_rse_rnat_addr (dst) = dst_rnat;
00099 }
00100 
00101 void
00102 __sigstack_longjmp (__jmp_buf buf, int val)
00103 {
00104   unsigned long *rbs_base, *bsp, *bspstore, *jb_bsp, jb_sp, ss_sp;
00105   unsigned long ndirty, rnat, load_rnat, *jb_rnat_addr;
00106   struct sigcontext *sc;
00107   stack_t stk;
00108   struct rbs_flush_values c;
00109 
00110   /* put RSE into enforced-lazy mode and return current bsp/rsc/rnat: */
00111   c = __ia64_flush_rbs ();
00112 
00113   jb_sp  = ((unsigned long *)  buf)[JB_SP];
00114   jb_bsp = ((unsigned long **) buf)[JB_BSP];
00115 
00116   INTERNAL_SYSCALL_DECL (err);
00117   (void) INTERNAL_SYSCALL (sigaltstack, err, 2, NULL, &stk);
00118 
00119   ss_sp = (unsigned long) stk.ss_sp;
00120   jb_rnat_addr = ia64_rse_rnat_addr (jb_bsp);
00121 
00122   if ((stk.ss_flags & SS_ONSTACK) == 0 || jb_sp - ss_sp < stk.ss_size)
00123     /* Normal non-stack-crossing longjmp; if the RNaT slot for the bsp
00124        saved in the jump-buffer is the same as the one for the current
00125        BSP, use the current AR.RNAT value, otherwise, load it from the
00126        jump-buffer's RNaT-slot.  */
00127     load_rnat = (ia64_rse_rnat_addr ((unsigned long *) c.bsp) != jb_rnat_addr);
00128   else
00129     {
00130       /* If we are on the alternate signal-stack and the jump-buffer
00131         lies outside the signal-stack, we may need to copy back the
00132         dirty partition which was torn off and saved on the
00133         signal-stack when the signal was delivered.
00134 
00135         Caveat: we assume that the top of the alternate signal-stack
00136                stores the sigcontext structure of the signal that
00137                caused the switch to the signal-stack.    This should
00138                be a fairly safe assumption but the kernel _could_
00139                do things differently.. */
00140       sc = ((struct sigcontext *) ((ss_sp + stk.ss_size) & -16) - 1);
00141 
00142       /* As a sanity-check, verify that the register-backing-store base
00143         of the alternate signal-stack is where we expect it.  */
00144       rbs_base = (unsigned long *)
00145        ((ss_sp + sizeof (long) - 1) & -sizeof (long));
00146 
00147       assert ((unsigned long) rbs_base == sc->sc_rbs_base);
00148 
00149       ndirty = ia64_rse_num_regs (rbs_base, rbs_base + (sc->sc_loadrs >> 19));
00150       bsp = (unsigned long *) sc->sc_ar_bsp;
00151       bspstore = ia64_rse_skip_regs (bsp, -ndirty);
00152 
00153       if (bspstore < jb_bsp)
00154        /* AR.BSPSTORE at the time of the signal was below the value
00155           of AR.BSP saved in the jump-buffer => copy the missing
00156           portion from the torn off dirty partition which got saved
00157           on the alternate signal-stack.  */
00158        copy_rbs (bspstore, jb_bsp, sc->sc_ar_rnat,
00159                 rbs_base, (unsigned long *) c.bsp, c.rnat);
00160 
00161       load_rnat = 1;
00162     }
00163   if (load_rnat)
00164     rnat = *jb_rnat_addr;
00165   else
00166     rnat = c.rnat;
00167   __ia64_longjmp (buf, val, rnat, c.rsc);
00168 }