1 | /* Copyright (C) 2001-2020 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 | /* The userland implementation is: |
27 | int clone (int (*fn)(void *arg), void *child_stack, int flags, void *arg), |
28 | the kernel entry is: |
29 | int clone (long flags, void *child_stack). |
30 | |
31 | The parameters are passed in register and on the stack from userland: |
32 | rdi: fn |
33 | rsi: child_stack |
34 | rdx: flags |
35 | rcx: arg |
36 | r8d: TID field in parent |
37 | r9d: thread pointer |
38 | %esp+8: TID field in child |
39 | |
40 | The kernel expects: |
41 | rax: system call number |
42 | rdi: flags |
43 | rsi: child_stack |
44 | rdx: TID field in parent |
45 | r10: TID field in child |
46 | r8: thread pointer */ |
47 | |
48 | |
49 | .text |
50 | ENTRY (__clone) |
51 | /* Sanity check arguments. */ |
52 | movq $-EINVAL,%rax |
53 | testq %rdi,%rdi /* no NULL function pointers */ |
54 | jz SYSCALL_ERROR_LABEL |
55 | testq %rsi,%rsi /* no NULL stack pointers */ |
56 | jz SYSCALL_ERROR_LABEL |
57 | |
58 | /* Insert the argument onto the new stack. */ |
59 | subq $16,%rsi |
60 | movq %rcx,8(%rsi) |
61 | |
62 | /* Save the function pointer. It will be popped off in the |
63 | child in the ebx frobbing below. */ |
64 | movq %rdi,0(%rsi) |
65 | |
66 | /* Do the system call. */ |
67 | movq %rdx, %rdi |
68 | movq %r8, %rdx |
69 | movq %r9, %r8 |
70 | mov 8(%rsp), %R10_LP |
71 | movl $SYS_ify(clone),%eax |
72 | |
73 | /* End FDE now, because in the child the unwind info will be |
74 | wrong. */ |
75 | cfi_endproc; |
76 | syscall |
77 | |
78 | testq %rax,%rax |
79 | jl SYSCALL_ERROR_LABEL |
80 | jz L(thread_start) |
81 | |
82 | ret |
83 | |
84 | L(thread_start): |
85 | cfi_startproc; |
86 | /* Clearing frame pointer is insufficient, use CFI. */ |
87 | cfi_undefined (rip); |
88 | /* Clear the frame pointer. The ABI suggests this be done, to mark |
89 | the outermost frame obviously. */ |
90 | xorl %ebp, %ebp |
91 | |
92 | /* Set up arguments for the function call. */ |
93 | popq %rax /* Function to call. */ |
94 | popq %rdi /* Argument. */ |
95 | call *%rax |
96 | /* Call exit with return value from function call. */ |
97 | movq %rax, %rdi |
98 | movl $SYS_ify(exit), %eax |
99 | syscall |
100 | cfi_endproc; |
101 | |
102 | cfi_startproc; |
103 | PSEUDO_END (__clone) |
104 | |
105 | libc_hidden_def (__clone) |
106 | weak_alias (__clone, clone) |
107 | |