]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/i386/clone.S
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / i386 / clone.S
1 /* Copyright (C) 1996-2021 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 <https://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 .text
43 ENTRY (__clone)
44 /* Sanity check arguments. */
45 movl $-EINVAL,%eax
46 movl FUNC(%esp),%ecx /* no NULL function pointers */
47 testl %ecx,%ecx
48 jz SYSCALL_ERROR_LABEL
49 movl STACK(%esp),%ecx /* no NULL stack pointers */
50 testl %ecx,%ecx
51 jz SYSCALL_ERROR_LABEL
52
53 /* Insert the argument onto the new stack. Make sure the new
54 thread is started with an alignment of (mod 16). */
55 andl $0xfffffff0, %ecx
56 subl $28,%ecx
57 movl ARG(%esp),%eax /* no negative argument counts */
58 movl %eax,12(%ecx)
59
60 /* Save the function pointer as the zeroth argument.
61 It will be popped off in the child in the ebx frobbing below. */
62 movl FUNC(%esp),%eax
63 movl %eax,8(%ecx)
64 /* Don't leak any information. */
65 movl $0,4(%ecx)
66
67 /* Do the system call */
68 pushl %ebx
69 cfi_adjust_cfa_offset (4)
70 pushl %esi
71 cfi_adjust_cfa_offset (4)
72 pushl %edi
73 cfi_adjust_cfa_offset (4)
74
75 movl TLS+12(%esp),%esi
76 cfi_rel_offset (esi, 4)
77 movl PTID+12(%esp),%edx
78 movl FLAGS+12(%esp),%ebx
79 cfi_rel_offset (ebx, 8)
80 movl CTID+12(%esp),%edi
81 cfi_rel_offset (edi, 0)
82 movl $SYS_ify(clone),%eax
83
84 /* Remember the flag value. */
85 movl %ebx, (%ecx)
86
87 /* End FDE now, because in the child the unwind info will be
88 wrong. */
89 cfi_endproc
90
91 int $0x80
92 popl %edi
93 popl %esi
94 popl %ebx
95
96 test %eax,%eax
97 jl SYSCALL_ERROR_LABEL
98 jz L(thread_start)
99
100 ret
101
102 L(thread_start):
103 cfi_startproc;
104 /* Clearing frame pointer is insufficient, use CFI. */
105 cfi_undefined (eip);
106 /* Note: %esi is zero. */
107 movl %esi,%ebp /* terminate the stack frame */
108 call *%ebx
109 #ifdef PIC
110 call L(here)
111 L(here):
112 popl %ebx
113 addl $_GLOBAL_OFFSET_TABLE_+[.-L(here)], %ebx
114 #endif
115 movl %eax, %ebx
116 movl $SYS_ify(exit), %eax
117 ENTER_KERNEL
118
119 PSEUDO_END (__clone)
120
121 libc_hidden_def (__clone)
122 weak_alias (__clone, clone)