Update copyright notices with scripts/update-copyrights
[glibc.git] / sysdeps / unix / sysv / linux / i386 / clone.S
blobc8dd2e4a372cd87626b6b300471277a1003eeafa
1 /* Copyright (C) 1996-2014 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Richard Henderson (rth@tamu.edu)
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.  */
19 /* clone() is even more special than fork() as it mucks with stacks
20    and invokes a function in the right context after its all over.  */
22 #include <sysdep.h>
23 #define _ERRNO_H        1
24 #include <bits/errno.h>
25 #include <asm-syntax.h>
27 /* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg,
28              pid_t *ptid, struct user_desc *tls, pid_t *ctid); */
30 #define PARMS   4               /* no space for saved regs */
31 #define FUNC    PARMS
32 #define STACK   FUNC+4
33 #define FLAGS   STACK+4
34 #define ARG     FLAGS+4
35 #define PTID    ARG+4
36 #define TLS     PTID+4
37 #define CTID    TLS+4
39 #define __NR_clone 120
40 #define SYS_clone 120
42 #define CLONE_VM        0x00000100
43 #define CLONE_THREAD    0x00010000
45         .text
46 ENTRY (__clone)
47         /* Sanity check arguments.  */
48         movl    $-EINVAL,%eax
49         movl    FUNC(%esp),%ecx         /* no NULL function pointers */
50 #ifdef PIC
51         jecxz   SYSCALL_ERROR_LABEL
52 #else
53         testl   %ecx,%ecx
54         jz      SYSCALL_ERROR_LABEL
55 #endif
56         movl    STACK(%esp),%ecx        /* no NULL stack pointers */
57 #ifdef PIC
58         jecxz   SYSCALL_ERROR_LABEL
59 #else
60         testl   %ecx,%ecx
61         jz      SYSCALL_ERROR_LABEL
62 #endif
64         /* Insert the argument onto the new stack.  Make sure the new
65            thread is started with an alignment of (mod 16).  */
66         andl    $0xfffffff0, %ecx
67         subl    $28,%ecx
68         movl    ARG(%esp),%eax          /* no negative argument counts */
69         movl    %eax,12(%ecx)
71         /* Save the function pointer as the zeroth argument.
72            It will be popped off in the child in the ebx frobbing below.  */
73         movl    FUNC(%esp),%eax
74         movl    %eax,8(%ecx)
75         /* Don't leak any information.  */
76         movl    $0,4(%ecx)
77 #ifndef RESET_PID
78         movl    $0,(%ecx)
79 #endif
81         /* Do the system call */
82         pushl   %ebx
83         cfi_adjust_cfa_offset (4)
84         pushl   %esi
85         cfi_adjust_cfa_offset (4)
86         pushl   %edi
87         cfi_adjust_cfa_offset (4)
89         movl    TLS+12(%esp),%esi
90         cfi_rel_offset (esi, 4)
91         movl    PTID+12(%esp),%edx
92         movl    FLAGS+12(%esp),%ebx
93         cfi_rel_offset (ebx, 8)
94         movl    CTID+12(%esp),%edi
95         cfi_rel_offset (edi, 0)
96         movl    $SYS_ify(clone),%eax
98 #ifdef RESET_PID
99         /* Remember the flag value.  */
100         movl    %ebx, (%ecx)
101 #endif
103         /* End FDE now, because in the child the unwind info will be
104            wrong.  */
105         cfi_endproc
107         int     $0x80
108         popl    %edi
109         popl    %esi
110         popl    %ebx
112         test    %eax,%eax
113         jl      SYSCALL_ERROR_LABEL
114         jz      L(thread_start)
116         ret
118 L(thread_start):
119         cfi_startproc;
120         /* Clearing frame pointer is insufficient, use CFI.  */
121         cfi_undefined (eip);
122         /* Note: %esi is zero.  */
123         movl    %esi,%ebp       /* terminate the stack frame */
124 #ifdef RESET_PID
125         testl   $CLONE_THREAD, %edi
126         je      L(newpid)
127 L(haspid):
128 #endif
129         call    *%ebx
130 #ifdef PIC
131         call    L(here)
132 L(here):
133         popl    %ebx
134         addl    $_GLOBAL_OFFSET_TABLE_+[.-L(here)], %ebx
135 #endif
136         movl    %eax, %ebx
137         movl    $SYS_ify(exit), %eax
138         ENTER_KERNEL
140 #ifdef RESET_PID
141         .subsection 2
142 L(newpid):
143         testl   $CLONE_VM, %edi
144         movl    $-1, %eax
145         jne     L(nomoregetpid)
146         movl    $SYS_ify(getpid), %eax
147         ENTER_KERNEL
148 L(nomoregetpid):
149         movl    %eax, %gs:PID
150         movl    %eax, %gs:TID
151         jmp     L(haspid)
152         .previous
153 #endif
154         cfi_endproc;
156         cfi_startproc
157 PSEUDO_END (__clone)
159 weak_alias (__clone, clone)