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