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 Jakub Jelinek <jakub@redhat.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 <stdarg.h>
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <ucontext.h>
00024 
00025 void
00026 __makecontext (ucontext_t *ucp, void (*func) (void), int argc, ...)
00027 {
00028   extern void __makecontext_ret (void);
00029   unsigned long *sp, *topsp;
00030   va_list ap;
00031   int i;
00032 
00033   sp = (long *) ((long) ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size);
00034   sp -= (argc > 6 ? argc : 6) + 32;
00035   sp = (long *) (((long) sp) & -16L);
00036   topsp = sp + (argc > 6 ? argc : 6) + 16;
00037 
00038   ucp->uc_mcontext.mc_gregs[MC_PC] = (long) func;
00039   ucp->uc_mcontext.mc_gregs[MC_NPC] = ((long) func) + 4;
00040   ucp->uc_mcontext.mc_gregs[MC_O6] = ((long) sp) - 0x7ff;
00041   ucp->uc_mcontext.mc_gregs[MC_O7] = ((long) __makecontext_ret) - 8;
00042   ucp->uc_mcontext.mc_fp = ((long) topsp) - 0x7ff;
00043   ucp->uc_mcontext.mc_i7 = 0;
00044   topsp[14] = 0;
00045   topsp[15] = 0;
00046   sp[8] = (long) ucp->uc_link;
00047   va_start (ap, argc);
00048   for (i = 0; i < argc; ++i)
00049     if (i < 6)
00050       ucp->uc_mcontext.mc_gregs[MC_O0 + i] = va_arg (ap, long);
00051     else
00052       sp[16 + i] = va_arg (ap, long);
00053   va_end (ap);
00054 }
00055 
00056 asm ("                                           \n\
00057        .text                                     \n\
00058        .type  __makecontext_ret, #function              \n\
00059 __makecontext_ret:                               \n\
00060        mov    1, %o1                             \n\
00061        call   __setcontext                       \n\
00062         mov   %i0, %o0                           \n\
00063        unimp  0                                  \n\
00064        .size  __makecontext_ret, .-__makecontext_ret    \n\
00065      ");
00066 
00067 weak_alias (__makecontext, makecontext)