]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/uclibc/lib/contrib/uclibc/libc/sysdeps/linux/xtensa/clone.S
Inital import
[l4.git] / l4 / pkg / uclibc / lib / contrib / uclibc / libc / sysdeps / linux / xtensa / clone.S
1 /* Copyright (C) 2001, 2005, 2007 Free Software Foundation, Inc.
2
3    The GNU C Library is free software; you can redistribute it and/or
4    modify it under the terms of the GNU Lesser General Public
5    License as published by the Free Software Foundation; either
6    version 2.1 of the License, or (at your option) any later version.
7
8    The GNU C Library is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11    Lesser General Public License for more details.
12
13    You should have received a copy of the GNU Lesser General Public
14    License along with the GNU C Library; if not, write to the Free
15    Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
16    Boston, MA 02110-1301, USA.  */
17
18 /* clone is even more special than fork as it mucks with stacks
19    and invokes a function in the right context after it's all over.  */
20
21 #include "sysdep.h"
22 #include <sys/syscall.h>
23 #define _ERRNO_H        1
24 #include <bits/errno.h>
25
26 /* int clone (a2 = int (*fn)(void *arg),
27               a3 = void *child_stack,
28               a4 = int flags,
29               a5 = void *arg,
30               a6 = pid_t *ptid,
31               a7 = struct user_desc *tls,
32               16(sp) = pid_t *ctid) */
33
34         .text
35 ENTRY (__clone)
36
37         /* Sanity check arguments.  */
38         beqz    a2, .Leinval    /* no NULL function pointers */
39         beqz    a3, .Leinval    /* no NULL stack pointers */
40
41         /* a2 and a3 are candidates for destruction by system-call return
42            parameters.  We don't need the stack pointer after the system
43            call.  We trust that the kernel will preserve a7, a9, and a6.  */
44
45         mov     a9, a5                  /* save function argument */
46         mov     a5, a7
47         mov     a7, a2                  /* save function pointer */
48         mov     a8, a6                  /* use a8 as a temp */
49         mov     a6, a4
50         mov     a4, a8
51         l32i    a8, a1, 16              /* child_tid */
52         movi    a2, SYS_ify (clone)
53
54         /* syscall (a2 = NR_clone,
55                     a6 = clone_flags,
56                     a3 = usp,
57                     a4 = parent_tid,
58                     a5 = child_tls,
59                     a8 = child_tid) */
60         syscall
61         bltz    a2, SYSCALL_ERROR_LABEL
62         beqz    a2, .Lthread_start
63
64         /* Fall through for parent.  */
65 .Lpseudo_end:
66         retw
67
68 .Leinval:
69         movi    a2, -EINVAL
70         j       SYSCALL_ERROR_LABEL
71
72 .Lthread_start:
73         /* Start child thread.  */
74         movi    a0, 0                   /* terminate the stack frame */
75
76 #ifdef RESET_PID
77         /* Check and see if we need to reset the PID.  */
78         bbsi.l  a6, 16, 1f              /* CLONE_THREAD = 0x00010000 */
79         movi    a2, -1
80         bbsi.l  a6, 8, 2f               /* CLONE_VM = 0x00000100 */
81         movi    a2, SYS_ify (getpid)
82         syscall
83 2:      rur     a3, THREADPTR
84         movi    a4, PID_OFFSET
85         add     a4, a4, a3
86         s32i    a2, a4, 0
87         movi    a4, TID_OFFSET
88         add     a4, a4, a3
89         s32i    a2, a3, 0
90 1:
91 #endif /* RESET_PID */
92
93         mov     a6, a9                  /* load up the 'arg' parameter */
94         callx4  a7                      /* call the user's function */
95
96         /* Call _exit.  Note that any return parameter from the user's
97            function in a6 is seen as inputs to _exit.  */
98         movi    a2, JUMPTARGET(_exit)
99         callx4  a2
100
101 PSEUDO_END (__clone)
102
103 weak_alias (__clone, clone)