remove PID caching
[uclibc-ng.git] / libc / sysdeps / linux / nios2 / clone.S
blob04f06348cef807474cdc5780f8dc014e11cd0ac7
1 /* clone() implementation for Nios II.
2    Copyright (C) 2008-2016 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Andrew Jenner <andrew@codesourcery.com>, 2008.
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, see
18    <http://www.gnu.org/licenses/>.  */
20 /* clone() is even more special than fork() as it mucks with stacks
21    and invokes a function in the right context after its all over.  */
23 #include <sysdep.h>
24 #define _ERRNO_H        1
25 #include <bits/errno.h>
27 #define CLONE_VM      0x00000100
29 /* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg,
30              void *parent_tidptr, void *tls, void *child_tidptr) */
32         .text
33 ENTRY(__clone)
34         /* Sanity check arguments.  */
35         movi    r2, EINVAL
37         subi    r5, r5, 8       /* Reserve argument save space.  */
38         stw     r4, 4(r5)       /* Save function pointer.  */
39         stw     r7, 0(r5)       /* Save argument pointer.  */
41         /* Load arguments.  */
42         mov     r4, r6
43         ldw     r6, 0(sp)
44         ldw     r7, 8(sp)
45         ldw     r8, 4(sp)
47         /* Do the system call.  */
48         movi    r2, SYS_ify (clone)
50         trap
52         /* See if we're on the newly created thread.  */
53         beq     r2, zero, thread_start
54         /* Successful return from the parent */
55         ret
57 thread_start:
58         /* We expect the argument registers to be preserved across system
59            calls and across task cloning, so flags should be in r4 here.  */
60         andi    r2, r4, CLONE_VM
61         bne     r2, zero, 2f
62         DO_CALL (getpid, 0)
64         ldw     r5, 4(sp)       /* Function pointer.  */
65         ldw     r4, 0(sp)       /* Argument pointer.  */
66         addi    sp, sp, 8
68         /* Call the user's function.  */
69         callr   r5
71         /* _exit with the result.  */
72         mov     r4, r2
73 #ifdef __PIC__
74         nextpc  r22
75 1:      movhi   r8, %hiadj(_gp_got - 1b)
76         addi    r8, r8, %lo(_gp_got - 1b)
77         add     r22, r22, r8
78         ldw     r8, %call(HIDDEN_JUMPTARGET(_exit))(r22)
79         jmp     r8
80 #else
81         jmpi    _exit
82 #endif
84 END(__clone)
85 libc_hidden_def (__clone)
86 weak_alias (__clone, clone)