1 | /* Copyright (C) 1996-2022 Free Software Foundation, Inc. |
2 | This file is part of the GNU C Library. |
3 | |
4 | The GNU C Library is free software; you can redistribute it and/or |
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. |
8 | |
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 |
12 | Lesser General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU Lesser General Public |
15 | License along with the GNU C Library; if not, see |
16 | <https://www.gnu.org/licenses/>. */ |
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> |
22 | #define _ERRNO_H 1 |
23 | #include <bits/errno.h> |
24 | #include <asm-syntax.h> |
25 | |
26 | /* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg, |
27 | pid_t *ptid, struct user_desc *tls, pid_t *ctid); */ |
28 | |
29 | #define PARMS 4 /* no space for saved regs */ |
30 | #define FUNC PARMS |
31 | #define STACK FUNC+4 |
32 | #define FLAGS STACK+4 |
33 | #define ARG FLAGS+4 |
34 | #define PTID ARG+4 |
35 | #define TLS PTID+4 |
36 | #define CTID TLS+4 |
37 | |
38 | #define __NR_clone 120 |
39 | #define SYS_clone 120 |
40 | |
41 | .text |
42 | ENTRY (__clone) |
43 | /* Sanity check arguments. */ |
44 | movl $-EINVAL,%eax |
45 | movl FUNC(%esp),%ecx /* no NULL function pointers */ |
46 | testl %ecx,%ecx |
47 | jz SYSCALL_ERROR_LABEL |
48 | movl STACK(%esp),%ecx /* no NULL stack pointers */ |
49 | testl %ecx,%ecx |
50 | jz SYSCALL_ERROR_LABEL |
51 | |
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 |
55 | subl $28,%ecx |
56 | movl ARG(%esp),%eax /* no negative argument counts */ |
57 | movl %eax,12(%ecx) |
58 | |
59 | /* Save the function pointer as the zeroth argument. |
60 | It will be popped off in the child in the ebx frobbing below. */ |
61 | movl FUNC(%esp),%eax |
62 | movl %eax,8(%ecx) |
63 | /* Don't leak any information. */ |
64 | movl $0,4(%ecx) |
65 | |
66 | /* Do the system call */ |
67 | pushl %ebx |
68 | cfi_adjust_cfa_offset (4) |
69 | pushl %esi |
70 | cfi_adjust_cfa_offset (4) |
71 | pushl %edi |
72 | cfi_adjust_cfa_offset (4) |
73 | |
74 | movl TLS+12(%esp),%esi |
75 | cfi_rel_offset (esi, 4) |
76 | movl PTID+12(%esp),%edx |
77 | movl FLAGS+12(%esp),%ebx |
78 | cfi_rel_offset (ebx, 8) |
79 | movl CTID+12(%esp),%edi |
80 | cfi_rel_offset (edi, 0) |
81 | movl $SYS_ify(clone),%eax |
82 | |
83 | /* Remember the flag value. */ |
84 | movl %ebx, (%ecx) |
85 | |
86 | /* End FDE now, because in the child the unwind info will be |
87 | wrong. */ |
88 | cfi_endproc |
89 | |
90 | int $0x80 |
91 | popl %edi |
92 | popl %esi |
93 | popl %ebx |
94 | |
95 | test %eax,%eax |
96 | jl SYSCALL_ERROR_LABEL |
97 | jz L(thread_start) |
98 | |
99 | ret |
100 | |
101 | L(thread_start): |
102 | cfi_startproc; |
103 | /* Clearing frame pointer is insufficient, use CFI. */ |
104 | cfi_undefined (eip); |
105 | /* Note: %esi is zero. */ |
106 | movl %esi,%ebp /* terminate the stack frame */ |
107 | call *%ebx |
108 | movl %eax, %ebx |
109 | movl $SYS_ify(exit), %eax |
110 | ENTER_KERNEL |
111 | |
112 | PSEUDO_END (__clone) |
113 | |
114 | libc_hidden_def (__clone) |
115 | weak_alias (__clone, clone) |
116 | |