2.9
[glibc/nacl-glibc.git] / sysdeps / unix / sysv / linux / i386 / makecontext.S
blobad2340555ac1d6e9c47245b5d3f455d1fd63f466
1 /* Create new context.
2    Copyright (C) 2001, 2002, 2005, 2007, 2008 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         /* Remember the number of parameters for the exit handler since
39            it has to remove them.  We store the number in the EBX register
40            which the function we will call must preserve.  */
41         movl    12(%esp), %ecx
42         movl    %ecx, oEBX(%eax)
44         /* Make room on the new stack for the parameters.
45            Room for the arguments, return address (== L(exitcode)) and
46            oLINK pointer is needed.  One of the pointer sizes is subtracted
47            after aligning the stack.  */
48         negl    %ecx
49         leal    -4(%edx,%ecx,4), %edx
50         negl    %ecx
52         /* Align the stack.  */
53         andl    $0xfffffff0, %edx
54         subl    $4, %edx
56         /* Store the future stack pointer.  */
57         movl    %edx, oESP(%eax)
59         /* Put the next context on the new stack (from the uc_link
60            element).  */
61         movl    oLINK(%eax), %eax
62         movl    %eax, 4(%edx,%ecx,4)
64         /* Copy all the parameters.  */
65         jecxz   2f
66 1:      movl    12(%esp,%ecx,4), %eax
67         movl    %eax, (%edx,%ecx,4)
68         decl    %ecx
69         jnz     1b
72         /* If the function we call returns we must continue with the
73            context which is given in the uc_link element.  To do this
74            set the return address for the function the user provides
75            to a little bit of helper code which does the magic (see
76            below).  */
77 #ifdef PIC
78         call    1f
79         cfi_adjust_cfa_offset (4)
80 1:      popl    %ecx
81         cfi_adjust_cfa_offset (-4)
82         addl    $L(exitcode)-1b, %ecx
83         movl    %ecx, (%edx)
84 #else
85         movl    $L(exitcode), (%edx)
86 #endif
87         /* 'makecontext' returns no value.  */
88         ret
90         /* This is the helper code which gets called if a function which
91            is registered with 'makecontext' returns.  In this case we
92            have to install the context listed in the uc_link element of
93            the context 'makecontext' manipulated at the time of the
94            'makecontext' call.  If the pointer is NULL the process must
95            terminate.  */
96         cfi_endproc
97 L(exitcode):
98         /* This removes the parameters passed to the function given to
99            'makecontext' from the stack.  EBX contains the number of
100            parameters (see above).  */
101         leal    (%esp,%ebx,4), %esp
103 #ifdef  PIC
104         call    1f
105 1:      popl    %ebx
106         addl    $_GLOBAL_OFFSET_TABLE_+[.-1b], %ebx
107 #endif
108         popl    %eax                    /* This is the next context.  */
109         testl   %eax, %eax
110         je      2f                      /* If it is zero exit.  */
112         pushl   %eax
113         call    JUMPTARGET(__setcontext)
114         /* If this returns (which can happen if the syscall fails) we'll
115            exit the program with the return error value (-1).  */
117 2:      pushl   %eax
118         call    HIDDEN_JUMPTARGET(exit)
119         /* The 'exit' call should never return.  In case it does cause
120            the process to terminate.  */
121         hlt
122         cfi_startproc
123 END(__makecontext)
125 weak_alias (__makecontext, makecontext)