]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/i386/clone.S
Add INLINE_SYSCALL_ERROR_RETURN_VALUE
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / i386 / clone.S
1 /* Copyright (C) 1996-2015 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Richard Henderson (rth@tamu.edu)
4
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.
9
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.
14
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/>. */
18
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. */
21
22 #include <sysdep.h>
23 #define _ERRNO_H 1
24 #include <bits/errno.h>
25 #include <asm-syntax.h>
26
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); */
29
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
38
39 #define __NR_clone 120
40 #define SYS_clone 120
41
42 #define CLONE_VM 0x00000100
43 #define CLONE_THREAD 0x00010000
44
45 .text
46 ENTRY (__clone)
47 /* Sanity check arguments. */
48 movl $-EINVAL,%eax
49 movl FUNC(%esp),%ecx /* no NULL function pointers */
50 testl %ecx,%ecx
51 jz SYSCALL_ERROR_LABEL
52 movl STACK(%esp),%ecx /* no NULL stack pointers */
53 testl %ecx,%ecx
54 jz SYSCALL_ERROR_LABEL
55
56 /* Insert the argument onto the new stack. Make sure the new
57 thread is started with an alignment of (mod 16). */
58 andl $0xfffffff0, %ecx
59 subl $28,%ecx
60 movl ARG(%esp),%eax /* no negative argument counts */
61 movl %eax,12(%ecx)
62
63 /* Save the function pointer as the zeroth argument.
64 It will be popped off in the child in the ebx frobbing below. */
65 movl FUNC(%esp),%eax
66 movl %eax,8(%ecx)
67 /* Don't leak any information. */
68 movl $0,4(%ecx)
69
70 /* Do the system call */
71 pushl %ebx
72 cfi_adjust_cfa_offset (4)
73 pushl %esi
74 cfi_adjust_cfa_offset (4)
75 pushl %edi
76 cfi_adjust_cfa_offset (4)
77
78 movl TLS+12(%esp),%esi
79 cfi_rel_offset (esi, 4)
80 movl PTID+12(%esp),%edx
81 movl FLAGS+12(%esp),%ebx
82 cfi_rel_offset (ebx, 8)
83 movl CTID+12(%esp),%edi
84 cfi_rel_offset (edi, 0)
85 movl $SYS_ify(clone),%eax
86
87 /* Remember the flag value. */
88 movl %ebx, (%ecx)
89
90 /* End FDE now, because in the child the unwind info will be
91 wrong. */
92 cfi_endproc
93
94 int $0x80
95 popl %edi
96 popl %esi
97 popl %ebx
98
99 test %eax,%eax
100 jl SYSCALL_ERROR_LABEL
101 jz L(thread_start)
102
103 ret
104
105 L(thread_start):
106 cfi_startproc;
107 /* Clearing frame pointer is insufficient, use CFI. */
108 cfi_undefined (eip);
109 /* Note: %esi is zero. */
110 movl %esi,%ebp /* terminate the stack frame */
111 testl $CLONE_THREAD, %edi
112 je L(newpid)
113 L(haspid):
114 call *%ebx
115 #ifdef PIC
116 call L(here)
117 L(here):
118 popl %ebx
119 addl $_GLOBAL_OFFSET_TABLE_+[.-L(here)], %ebx
120 #endif
121 movl %eax, %ebx
122 movl $SYS_ify(exit), %eax
123 ENTER_KERNEL
124
125 .subsection 2
126 L(newpid):
127 testl $CLONE_VM, %edi
128 movl $-1, %eax
129 jne L(nomoregetpid)
130 movl $SYS_ify(getpid), %eax
131 ENTER_KERNEL
132 L(nomoregetpid):
133 movl %eax, %gs:PID
134 movl %eax, %gs:TID
135 jmp L(haspid)
136 .previous
137 cfi_endproc;
138
139 cfi_startproc
140 PSEUDO_END (__clone)
141
142 weak_alias (__clone, clone)