]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/unix/sysv/linux/i386/clone.S
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / i386 / clone.S
CommitLineData
dff8da6b 1/* Copyright (C) 1996-2024 Free Software Foundation, Inc.
0c5ecdc4 2 This file is part of the GNU C Library.
267ca16a 3
0c5ecdc4 4 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
267ca16a 8
0c5ecdc4
UD
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 12 Lesser General Public License for more details.
267ca16a 13
41bdb6e2 14 You should have received a copy of the GNU Lesser General Public
59ba27a6 15 License along with the GNU C Library; if not, see
5a82c748 16 <https://www.gnu.org/licenses/>. */
267ca16a
UD
17
18/* clone() is even more special than fork() as it mucks with stacks
19 and invokes a function in the right context after its all over. */
20
21#include <sysdep.h>
11336c16 22#define _ERRNO_H 1
5107cf1d 23#include <bits/errno.h>
cbdee279 24#include <asm-syntax.h>
267ca16a 25
b7cb624b 26/* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg,
b33e6163 27 pid_t *ptid, struct user_desc *tls, pid_t *ctid); */
267ca16a 28
2366713d 29#define PARMS 4 /* no space for saved regs */
9e25f6e2
GM
30#define FUNC PARMS
31#define STACK FUNC+4
2366713d 32#define FLAGS STACK+4
9e25f6e2 33#define ARG FLAGS+4
2366713d
JM
34#define PTID ARG+4
35#define TLS PTID+4
36#define CTID TLS+4
b7cb624b
UD
37
38#define __NR_clone 120
39#define SYS_clone 120
9e25f6e2 40
267ca16a 41 .text
2366713d 42ENTRY (__clone)
267ca16a
UD
43 /* Sanity check arguments. */
44 movl $-EINVAL,%eax
9e25f6e2 45 movl FUNC(%esp),%ecx /* no NULL function pointers */
69963deb
UD
46 testl %ecx,%ecx
47 jz SYSCALL_ERROR_LABEL
9e25f6e2 48 movl STACK(%esp),%ecx /* no NULL stack pointers */
69963deb
UD
49 testl %ecx,%ecx
50 jz SYSCALL_ERROR_LABEL
267ca16a 51
6c052003
UD
52 /* Insert the argument onto the new stack. Make sure the new
53 thread is started with an alignment of (mod 16). */
54 andl $0xfffffff0, %ecx
7d585303 55 subl $28,%ecx
9e25f6e2 56 movl ARG(%esp),%eax /* no negative argument counts */
56c91066 57 movl %eax,12(%ecx)
0c5ecdc4 58
267ca16a
UD
59 /* Save the function pointer as the zeroth argument.
60 It will be popped off in the child in the ebx frobbing below. */
9e25f6e2 61 movl FUNC(%esp),%eax
56c91066 62 movl %eax,8(%ecx)
dde36a31 63 /* Don't leak any information. */
56c91066 64 movl $0,4(%ecx)
267ca16a
UD
65
66 /* Do the system call */
67 pushl %ebx
1ad9da69 68 cfi_adjust_cfa_offset (4)
b7cb624b 69 pushl %esi
1ad9da69 70 cfi_adjust_cfa_offset (4)
56c91066 71 pushl %edi
1ad9da69 72 cfi_adjust_cfa_offset (4)
fee732e5 73
56c91066 74 movl TLS+12(%esp),%esi
1ad9da69 75 cfi_rel_offset (esi, 4)
56c91066
UD
76 movl PTID+12(%esp),%edx
77 movl FLAGS+12(%esp),%ebx
1ad9da69 78 cfi_rel_offset (ebx, 8)
56c91066 79 movl CTID+12(%esp),%edi
1ad9da69 80 cfi_rel_offset (edi, 0)
267ca16a 81 movl $SYS_ify(clone),%eax
1ff241b8 82
1ff241b8
UD
83 /* Remember the flag value. */
84 movl %ebx, (%ecx)
1ff241b8 85
fee732e5
UD
86 /* End FDE now, because in the child the unwind info will be
87 wrong. */
88 cfi_endproc
89
267ca16a 90 int $0x80
56c91066 91 popl %edi
b7cb624b 92 popl %esi
267ca16a
UD
93 popl %ebx
94
95 test %eax,%eax
c0fb8a56 96 jl SYSCALL_ERROR_LABEL
b7cb624b 97 jz L(thread_start)
267ca16a
UD
98
99 ret
267ca16a 100
b7cb624b 101L(thread_start):
11bf311e
UD
102 cfi_startproc;
103 /* Clearing frame pointer is insufficient, use CFI. */
104 cfi_undefined (eip);
1ff241b8
UD
105 /* Note: %esi is zero. */
106 movl %esi,%ebp /* terminate the stack frame */
267ca16a 107 call *%ebx
b7cb624b
UD
108 movl %eax, %ebx
109 movl $SYS_ify(exit), %eax
1ad9da69 110 ENTER_KERNEL
267ca16a 111
2366713d 112PSEUDO_END (__clone)
6ed0492f 113
9ff72da4 114libc_hidden_def (__clone)
2366713d 115weak_alias (__clone, clone)