Back to index

glibc  2.9
makecontext.c
Go to the documentation of this file.
00001 /* Create new context.
00002    Copyright (C) 2008 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Helge Deller <deller@gmx.de>, 2008.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <libintl.h>
00022 #include <stdarg.h>
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <sysdep.h>
00026 #include <ucontext.h>
00027 
00028 /* XXX: This implementation only handles integer arguments.  */
00029 
00030 void
00031 __makecontext (ucontext_t *ucp, void (*func) (void), int argc, ...)
00032 {
00033   unsigned int *sp;
00034   va_list ap;
00035   int i;
00036 
00037   if (argc > 8)
00038     {
00039       fprintf (stderr, _("\
00040 makecontext: does not know how to handle more than 8 arguments\n"));
00041       exit (-1);
00042     }
00043 
00044   /* Get stack pointer.  */
00045   sp = (unsigned int *) ucp->uc_stack.ss_sp;
00046 
00047   /* Store address to jump to.  */
00048   ucp->uc_mcontext.sc_gr[2] = (unsigned long) func;
00049 
00050   va_start (ap, argc);
00051   /* Handle arguments.  */
00052   for (i = 0; i < argc; ++i)
00053     switch (i)
00054       {
00055       case 0:
00056       case 1:
00057       case 2:
00058       case 3:
00059        ucp->uc_mcontext.sc_gr[26-i] = va_arg (ap, int);
00060        break;
00061       case 4:
00062       case 5:
00063       case 6:
00064       case 7:
00065        if (sizeof(unsigned long) == 4) {
00066               /* 32bit: put arg7-arg4 on stack.  */
00067               sp[7-i] = va_arg (ap, int);
00068        } else {
00069               /* 64bit: r19-r22 are arg7-arg4.  */
00070               ucp->uc_mcontext.sc_gr[22+4-i] = va_arg (ap, int);
00071        }
00072        break;
00073       }
00074   va_end (ap);
00075 
00076 }
00077 
00078 
00079 weak_alias(__makecontext, makecontext)