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