Back to index

glibc  2.9
makecontext.c
Go to the documentation of this file.
00001 /* Copyright (C) 2001 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 #include <libintl.h>
00021 #include <stdarg.h>
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <ucontext.h>
00025 #include <sys/rse.h>
00026 
00027 
00028 struct fdesc
00029   {
00030     unsigned long ip;
00031     unsigned long gp;
00032   };
00033 
00034 #define PUSH(val)                         \
00035 do {                                      \
00036   if (ia64_rse_is_rnat_slot (rbs))        \
00037     *rbs++ = 0;                                  \
00038   *rbs++ = (val);                         \
00039 } while (0)
00040 
00041 
00042 /* This implementation can handle an ARGC value of at most 8 and
00043    values can be passed only in integer registers (r32-r39).  */
00044 
00045 void
00046 __makecontext (ucontext_t *ucp, void (*func) (void), int argc, ...)
00047 {
00048   struct sigcontext *sc = &ucp->uc_mcontext;
00049   extern void __start_context (ucontext_t *link, long gp, ...);
00050   unsigned long stack_start, stack_end;
00051   va_list ap;
00052   long *rbs;
00053   int i;
00054 
00055   stack_start = (long) sc->sc_stack.ss_sp;
00056   stack_end = (long) sc->sc_stack.ss_sp + sc->sc_stack.ss_size;
00057 
00058   stack_start = (stack_start + 7) & -8;
00059   stack_end = stack_end & -16;
00060 
00061   if (argc > 8)
00062     {
00063       fprintf (stderr, _("\
00064 makecontext: does not know how to handle more than 8 arguments\n"));
00065       exit (-1);
00066     }
00067 
00068   /* set the entry point and global pointer: */
00069   sc->sc_br[0] = ((struct fdesc *) &__start_context)->ip;
00070   sc->sc_br[1] = ((struct fdesc *) func)->ip;
00071   sc->sc_gr[1] = ((struct fdesc *) func)->gp;
00072 
00073   /* set up the call frame: */
00074   sc->sc_ar_pfs = ((sc->sc_ar_pfs & ~0x3fffffffffUL)
00075                  | (argc + 2) | ((argc + 2) << 7));
00076   rbs = (long *) stack_start;
00077   PUSH((long) ucp->uc_link);
00078   PUSH(((struct fdesc *) &__start_context)->gp);
00079   va_start (ap, argc);
00080   for (i = 0; i < argc; ++i)
00081     PUSH(va_arg (ap, long));
00082   va_end (ap);
00083 
00084   /* set the memory and register stack pointers: */
00085   sc->sc_ar_bsp = (long) rbs;
00086   sc->sc_gr[12] = stack_end - 16;
00087 
00088   /* clear the NaT bits for r1 and r12: */
00089   sc->sc_nat &= ~((1 << 1) | (1 << 12));
00090   sc->sc_ar_rnat = 0;
00091 }
00092 
00093 weak_alias (__makecontext, makecontext)