remove PID caching
[uclibc-ng.git] / libc / sysdeps / linux / x86_64 / clone.S
blob3dda8da0279272d302ed814fcb89e27c2ea49d21
1 /* Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
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.
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.
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/>.  */
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.  */
21 #include <features.h>
22 #define _ERRNO_H 1
23 #include <bits/errno.h>
24 #include <sys/syscall.h>
26 #define CLONE_VM        0x00000100
27 #define CLONE_THREAD    0x00010000
29 /* The userland implementation is:
30    int clone (int (*fn)(void *arg), void *child_stack, int flags, void *arg),
31    the kernel entry is:
32    int clone (long flags, void *child_stack).
34    The parameters are passed in register and on the stack from userland:
35    rdi: fn
36    rsi: child_stack
37    rdx: flags
38    rcx: arg
39    r8d: TID field in parent
40    r9d: thread pointer
41 %esp+8: TID field in child
43    The kernel expects:
44    rax: system call number
45    rdi: flags
46    rsi: child_stack
47    rdx: TID field in parent
48    r10: TID field in child
49    r8:  thread pointer  */
52 .text
53 .global clone
54 .type   clone,%function
55 clone:
56         /* Sanity check arguments.  */
57         movq    $-EINVAL,%rax
58         testq   %rdi,%rdi               /* no NULL function pointers */
59         jz      __syscall_error
60         testq   %rsi,%rsi               /* no NULL stack pointers */
61         jz      __syscall_error
63         /* Insert the argument onto the new stack.  */
64         subq    $16,%rsi
65         movq    %rcx,8(%rsi)
67         /* Save the function pointer.  It will be popped off in the
68            child in the ebx frobbing below.  */
69         movq    %rdi,0(%rsi)
71         /* Do the system call.  */
72         movq    %rdx, %rdi
73         movq    %r8, %rdx
74         movq    %r9, %r8
75         movq    8(%rsp), %r10
76         movl    $__NR_clone,%eax
78         syscall
80         testq   %rax,%rax
81         jl      __syscall_error
82         jz      .Lthread_start
84 .Lpseudo_end:
85         ret
87 .Lthread_start:
88         /* Clear the frame pointer.  The ABI suggests this be done, to mark
89            the outermost frame obviously.  */
90         xorl    %ebp, %ebp
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    $__NR_exit, %eax
99         syscall
101 .size clone,.-clone
102 weak_alias(clone, __clone)