Back to index

glibc  2.9
sigtramp.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991,1992,1994,1996,1997,2004 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #ifndef       __GNUC__
00020   #error This file uses GNU C extensions; you must compile with GCC.
00021 #endif
00022 
00023 /* Get the definition of `struct sigcontext'.  */
00024 #define       KERNEL
00025 #define       sigvec        sun_sigvec
00026 #define       sigstack      sun_sigstack
00027 #define       sigcontext    sun_sigcontext
00028 #include "/usr/include/sys/signal.h"
00029 #undef sigvec
00030 #undef sigstack
00031 #undef sigcontext
00032 #undef NSIG
00033 #undef SIGABRT
00034 #undef SIGCLD
00035 #undef SV_ONSTACK
00036 #undef SV_RESETHAND
00037 #undef SV_INTERRUPT
00038 #undef SA_ONSTACK
00039 #undef SA_NOCLDSTOP
00040 #undef SIG_ERR
00041 #undef SIG_DFL
00042 #undef SIG_IGN
00043 #undef sigmask
00044 #undef SIG_BLOCK
00045 #undef SIG_UNBLOCK
00046 #undef SIG_SETMASK
00047 
00048 #include <signal.h>
00049 #include <stddef.h>
00050 #include <errno.h>
00051 
00052 /* Defined in __sigvec.S.  */
00053 extern int __raw_sigvec (int sig, CONST struct sigvec *vec,
00054                       struct sigvec *ovec);
00055 
00056 /* User-specified signal handlers.  */
00057 #define mytramp 1
00058 #ifdef mytramp
00059 static __sighandler_t handlers[NSIG];
00060 #else
00061 #define handlers _sigfunc
00062 extern __sighandler_t _sigfunc[];
00063 #endif
00064 
00065 #if mytramp
00066 
00067 /* Handler for all signals that are handled by a user-specified function.
00068    Saves and restores the general regs %g2-%g7, the %y register, and
00069    all the FPU regs (including %fsr), around calling the user's handler.  */
00070 static void
00071 trampoline (sig)
00072      int sig;
00073 {
00074   /* We use `double' and `long long int' so `std' (store doubleword) insns,
00075      which might be faster than single-word stores, will be generated.  */
00076   register double f0 asm("%f0");
00077   register double f2 asm("%f2");
00078   register double f4 asm("%f4");
00079   register double f6 asm("%f6");
00080   register double f8 asm("%f8");
00081   register double f10 asm("%f10");
00082   register double f12 asm("%f12");
00083   register double f14 asm("%f14");
00084   register double f16 asm("%f16");
00085   register double f18 asm("%f18");
00086   register double f20 asm("%f20");
00087   register double f22 asm("%f22");
00088   register double f24 asm("%f24");
00089   register double f26 asm("%f26");
00090   register double f28 asm("%f28");
00091   register double f30 asm("%f30");
00092   register long long int g2 asm("%g2");
00093   register long long int g4 asm("%g4");
00094   register long long int g6 asm("%g6");
00095   register int *fp asm("%fp");
00096 
00097   int code;
00098   register struct sigcontext *context asm("%i0"); /* See end of fn.  */
00099   void *addr;
00100   int y;
00101   double fpsave[16];
00102   int fsr;
00103   int savefpu;
00104   long long int glsave[3];
00105 
00106   /* SIG isn't really passed as an arg.
00107      The args to the signal handler are at fp[16..19].  */
00108   sig = fp[16];
00109   code = fp[17];
00110   context = (struct sigcontext *) fp[18];
00111   addr = (PTR) fp[19];
00112 
00113   /* Save the Y register.  */
00114   asm("rd %%y, %0" : "=r" (y));
00115 
00116   /* Save the FPU regs if the FPU enable bit is set in the PSR,
00117      and the signal isn't an FP exception.  */
00118   savefpu = (context->sc_psr & 0x1000) && sig != SIGFPE;
00119   if (savefpu)
00120     {
00121       fpsave[0] = f0;
00122       fpsave[1] = f2;
00123       fpsave[2] = f4;
00124       fpsave[3] = f6;
00125       fpsave[4] = f8;
00126       fpsave[5] = f10;
00127       fpsave[6] = f12;
00128       fpsave[7] = f14;
00129       fpsave[8] = f16;
00130       fpsave[9] = f18;
00131       fpsave[10] = f20;
00132       fpsave[11] = f22;
00133       fpsave[12] = f24;
00134       fpsave[13] = f26;
00135       fpsave[14] = f28;
00136       fpsave[15] = f30;
00137 
00138       /* Force it into a stack slot so the asm won't barf.  Sigh.  */
00139       (void) &fsr;
00140       asm("st %%fsr, %0" : "=m" (fsr));
00141     }
00142 
00143   /* Save the global registers (except for %g1, which is a scratch reg).  */
00144   glsave[0] = g2;
00145   glsave[1] = g4;
00146   glsave[2] = g6;
00147 
00148   /* Call the user's handler.  */
00149   (*((void (*) (int sig, int code, struct sigcontext *context,
00150               void *addr)) handlers[sig]))
00151     (sig, code, context, addr);
00152 
00153   /* Restore the Y register.  */
00154   asm("mov %0, %%y" : : "r" (y));
00155 
00156   if (savefpu)
00157     {
00158       /* Restore the FPU regs.  */
00159       f0 = fpsave[0];
00160       f2 = fpsave[1];
00161       f4 = fpsave[2];
00162       f6 = fpsave[3];
00163       f8 = fpsave[4];
00164       f10 = fpsave[5];
00165       f12 = fpsave[6];
00166       f14 = fpsave[7];
00167       f16 = fpsave[8];
00168       f18 = fpsave[9];
00169       f20 = fpsave[10];
00170       f22 = fpsave[11];
00171       f24 = fpsave[12];
00172       f26 = fpsave[13];
00173       f28 = fpsave[14];
00174       f30 = fpsave[15];
00175 
00176       asm("ld %0, %%fsr" : : "m" (fsr));
00177     }
00178 
00179   /* Restore the globals.  */
00180   g2 = glsave[0];
00181   g4 = glsave[1];
00182   g6 = glsave[2];
00183 
00184   /* Unwind a frame, and do a "sigcleanup" system call.
00185      The system call apparently does a return.
00186      I don't know what it's for.  Ask Sun.  */
00187   asm("restore %%g0, 139, %%g1\n"
00188       "ta 0\n"
00189       "! this should be i0: %0"    /* Useless insn that will never be executed, */
00190                             /* here to make the compiler happy.  */
00191       : /* No outputs.  */ :
00192       /* CONTEXT is bound to %i0.  We reference it as an input here to make
00193         sure the compiler considers it live at this point, and preserves
00194         the value in that register.  The restore makes %i0 become %o0, the
00195         argument to the system call.  */
00196       "r" (context));
00197 }
00198 #endif
00199 
00200 int
00201 __sigvec (sig, vec, ovec)
00202      int sig;
00203      const struct sigvec *vec;
00204      struct sigvec *ovec;
00205 {
00206 #ifndef       mytramp
00207   extern void _sigtramp (int);
00208 #define       trampoline    _sigtramp
00209 #endif
00210   struct sigvec myvec;
00211   int mask;
00212   __sighandler_t ohandler;
00213 
00214   if (sig <= 0 || sig >= NSIG)
00215     {
00216       __set_errno (EINVAL);
00217       return -1;
00218     }
00219 
00220   mask = __sigblock (sigmask(sig));
00221 
00222   ohandler = handlers[sig];
00223 
00224   if (vec != NULL &&
00225       vec->sv_handler != SIG_IGN && vec->sv_handler != SIG_DFL)
00226     {
00227       handlers[sig] = vec->sv_handler;
00228       myvec = *vec;
00229       myvec.sv_handler = trampoline;
00230       vec = &myvec;
00231     }
00232 
00233   if (__raw_sigvec(sig, vec, ovec) < 0)
00234     {
00235       int save = errno;
00236       (void) __sigsetmask(mask);
00237       errno = save;
00238       return -1;
00239     }
00240 
00241   if (ovec != NULL && ovec->sv_handler == trampoline)
00242     ovec->sv_handler = ohandler;
00243 
00244   (void) __sigsetmask (mask);
00245 
00246   return 0;
00247 }