Updated to fedora-glibc-20051116T0829
[glibc.git] / sysdeps / unix / sysv / linux / i386 / makecontext.S
blob12ba4e2d67e91c340516cd2954dfe0341118d5b1
1 /* Create new context.
2    Copyright (C) 2001, 2002, 2005 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Ulrich Drepper <drepper@redhat.com>, 2001.
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, write to the Free
18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
21 #include <sysdep.h>
23 #include "ucontext_i.h"
26 ENTRY(__makecontext)
27         movl    4(%esp), %eax
29         /* Load the address of the function we are supposed to run.  */
30         movl    8(%esp), %ecx
32         /* Compute the address of the stack.  The information comes from
33            to us_stack element.  */
34         movl    oSS_SP(%eax), %edx
35         movl    %ecx, oEIP(%eax)
36         addl    oSS_SIZE(%eax), %edx
38         /* Put the next context on the new stack (from the uc_link
39            element).  */
40         movl    oLINK(%eax), %ecx
41         movl    %ecx, -4(%edx)
43         /* Remember the number of parameters for the exit handler since
44            it has to remove them.  We store the number in the EBX register
45            which the function we will call must preserve.  */
46         movl    12(%esp), %ecx
47         movl    %ecx, oEBX(%eax)
49         /* Make room on the new stack for the parameters.  */
50         negl    %ecx
51         leal    -8(%edx,%ecx,4), %edx
52         negl    %ecx
53         /* Store the future stack pointer.  */
54         movl    %edx, oESP(%eax)
56         /* Copy all the parameters.  */
57         jecxz   2f
58 1:      movl    12(%esp,%ecx,4), %eax
59         movl    %eax, (%edx,%ecx,4)
60         decl    %ecx
61         jnz     1b
64         /* If the function we call returns we must continue with the
65            context which is given in the uc_link element.  To do this
66            set the return address for the function the user provides
67            to a little bit of helper code which does the magic (see
68            below).  */
69 #ifdef PIC
70         call    1f
71         cfi_adjust_cfa_offset (4)
72 1:      popl    %ecx
73         cfi_adjust_cfa_offset (-4)
74         addl    $L(exitcode)-1b, %ecx
75         movl    %ecx, (%edx)
76 #else
77         movl    $L(exitcode), (%edx)
78 #endif
79         /* 'makecontext' returns no value.  */
80         ret
82         /* This is the helper code which gets called if a function which
83            is registered with 'makecontext' returns.  In this case we
84            have to install the context listed in the uc_link element of
85            the context 'makecontext' manipulated at the time of the
86            'makecontext' call.  If the pointer is NULL the process must
87            terminate.  */
88         cfi_endproc
89 L(exitcode):
90         /* This removes the parameters passed to the function given to
91            'makecontext' from the stack.  EBX contains the number of
92            parameters (see above).  */
93         leal    (%esp,%ebx,4), %esp
95 #ifdef  PIC
96         call    1f
97 1:      popl    %ebx
98         addl    $_GLOBAL_OFFSET_TABLE_+[.-1b], %ebx
99 #endif
100         popl    %eax                    /* This is the next context.  */
101         testl   %eax, %eax
102         je      2f                      /* If it is zero exit.  */
104         pushl   %eax
105         call    JUMPTARGET(__setcontext)
106         /* If this returns (which can happen if the syscall fails) we'll
107            exit the program with the return error value (-1).  */
109 2:      pushl   %eax
110         call    HIDDEN_JUMPTARGET(exit)
111         /* The 'exit' call should never return.  In case it does cause
112            the process to terminate.  */
113         hlt
114         cfi_startproc
115 END(__makecontext)
117 weak_alias (__makecontext, makecontext)