]>
Commit | Line | Data |
---|---|---|
04277e02 | 1 | /* Copyright (C) 2001-2019 Free Software Foundation, Inc. |
c9cf6dde AJ |
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 | |
59ba27a6 PE |
15 | License along with the GNU C Library; if not, see |
16 | <http://www.gnu.org/licenses/>. */ | |
c9cf6dde AJ |
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> | |
c9cf6dde AJ |
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 | ||
e2b80a58 | 31 | The parameters are passed in register and on the stack from userland: |
c9cf6dde AJ |
32 | rdi: fn |
33 | rsi: child_stack | |
34 | rdx: flags | |
35 | rcx: arg | |
e2b80a58 UD |
36 | r8d: TID field in parent |
37 | r9d: thread pointer | |
38 | %esp+8: TID field in child | |
c9cf6dde AJ |
39 | |
40 | The kernel expects: | |
41 | rax: system call number | |
42 | rdi: flags | |
e2b80a58 UD |
43 | rsi: child_stack |
44 | rdx: TID field in parent | |
45 | r10: TID field in child | |
46 | r8: thread pointer */ | |
c9cf6dde AJ |
47 | |
48 | ||
49 | .text | |
29691210 | 50 | ENTRY (__clone) |
c9cf6dde AJ |
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 | |
e2b80a58 | 63 | child in the ebx frobbing below. */ |
c9cf6dde AJ |
64 | movq %rdi,0(%rsi) |
65 | ||
66 | /* Do the system call. */ | |
67 | movq %rdx, %rdi | |
e2b80a58 UD |
68 | movq %r8, %rdx |
69 | movq %r9, %r8 | |
f6ee6623 | 70 | mov 8(%rsp), %R10_LP |
ee618985 | 71 | movl $SYS_ify(clone),%eax |
249a3d0c UD |
72 | |
73 | /* End FDE now, because in the child the unwind info will be | |
74 | wrong. */ | |
75 | cfi_endproc; | |
c9cf6dde AJ |
76 | syscall |
77 | ||
78 | testq %rax,%rax | |
79 | jl SYSCALL_ERROR_LABEL | |
1ff241b8 | 80 | jz L(thread_start) |
c9cf6dde | 81 | |
c9cf6dde AJ |
82 | ret |
83 | ||
1ff241b8 | 84 | L(thread_start): |
11bf311e UD |
85 | cfi_startproc; |
86 | /* Clearing frame pointer is insufficient, use CFI. */ | |
87 | cfi_undefined (rip); | |
249a3d0c UD |
88 | /* Clear the frame pointer. The ABI suggests this be done, to mark |
89 | the outermost frame obviously. */ | |
ee618985 | 90 | xorl %ebp, %ebp |
249a3d0c | 91 | |
c9cf6dde AJ |
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 | |
3f823e87 AZ |
98 | movl $SYS_ify(exit), %eax |
99 | syscall | |
11bf311e | 100 | cfi_endproc; |
c9cf6dde | 101 | |
249a3d0c | 102 | cfi_startproc; |
29691210 | 103 | PSEUDO_END (__clone) |
c9cf6dde | 104 | |
9ff72da4 | 105 | libc_hidden_def (__clone) |
29691210 | 106 | weak_alias (__clone, clone) |