Move all files into ports/ subdirectory in preparation for merge with glibc
[glibc.git] / ports / sysdeps / unix / sysv / linux / alpha / clone.S
blob1c6c8d6b7e72fb03a909864f8f36966b95ceadb6
1 /* Copyright (C) 1996, 1997, 2002 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Richard Henderson <rth@tamu.edu>, 1996.
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library.  If not, see
17    <http://www.gnu.org/licenses/>.  */
19 /* clone() is even more special than fork() as it mucks with stacks
20    and invokes a function in the right context after its all over.  */
22 #include <sysdep.h>
23 #define _ERRNO_H        1
24 #include <bits/errno.h>
26 #define CLONE_VM        0x00000100
27 #define CLONE_THREAD    0x00010000
29 /* int clone(int (*fn)(void *arg), void *child_stack, int flags,
30              void *arg, pid_t *ptid, void *tls, pid_t *ctid);
32    Note that everything past ARG is technically optional, based
33    on FLAGS, and that CTID is arg 7, and thus is on the stack.
34    However, since a load from top-of-stack better be legal always,
35    we don't bother checking FLAGS.  */
37         .text
38 ENTRY(__clone)
39 #ifdef PROF
40         ldgp    gp,0(pv)
41         .set noat
42         lda     AT, _mcount
43         jsr     AT, (AT), _mcount
44         .set at
45         .prologue 1
46 #else
47         .prologue 0
48 #endif
50         /* Sanity check arguments.  */
51         ldiq    v0,EINVAL
52         beq     a0,$error               /* no NULL function pointers */
53         beq     a1,$error               /* no NULL stack pointers */
55         /* Save the fn ptr and arg on the new stack.  */
56         subq    a1,32,a1
57         stq     a0,0(a1)
58         stq     a3,8(a1)
59 #ifdef RESET_PID
60         stq     a2,16(a1)
61 #endif
63         /* The syscall is of the form clone(flags, usp, ptid, ctid, tls).
64            Shift the flags, ptid, ctid, tls arguments into place; the
65            child_stack argument is already correct.  */
66         mov     a2,a0
67         mov     a4,a2
68         ldq     a3,0(sp)
69         mov     a5,a4
71         /* Do the system call.  */
72         ldiq    v0,__NR_clone
73         call_pal PAL_callsys
75         bne     a3,$error
76         beq     v0,thread_start
78         /* Successful return from the parent.  */
79         ret
81         /* Something bad happened -- no child created.  */
82 $error:
83 #ifndef PROF
84         br      gp,1f
85 1:      ldgp    gp,0(gp)
86 #endif
87         SYSCALL_ERROR_HANDLER
89         END(__clone)
91 /* Load up the arguments to the function.  Put this block of code in
92    its own function so that we can terminate the stack trace with our
93    debug info.  */
95         .ent thread_start
96 thread_start:
97         .frame  fp,0,fp,0
98         mov     0, fp
99         .prologue 0
101 #ifdef RESET_PID
102         /* Check and see if we need to reset the PID.  */
103         ldq     t0,16(sp)
104         lda     t1,CLONE_THREAD
105         and     t0,t1,t2
106         beq     t2,2f
108 #endif
110         /* Load up the arguments.  */
111         ldq     pv,0(sp)
112         ldq     a0,8(sp)
113         addq    sp,32,sp
115         /* Call the user's function.  */
116         jsr     ra,(pv)
117         ldgp    gp,0(ra)
119         /* Call _exit rather than doing it inline for breakpoint purposes.  */
120         mov     v0,a0
121 #ifdef PIC
122         bsr     ra, HIDDEN_JUMPTARGET(_exit)    !samegp
123 #else
124         jsr     ra, HIDDEN_JUMPTARGET(_exit)
125 #endif
127         /* Die horribly.  */
128         halt
130 #ifdef RESET_PID
132         rduniq
133         lda     t1, CLONE_VM
134         mov     v0, s0
135         lda     v0, -1
136         and     t0, t1, t2
137         bne     t2, 3f
138         lda     v0, __NR_getxpid
139         callsys
141         stl     v0, PID_OFFSET(s0)
142         stl     v0, TID_OFFSET(s0)
143         br      1b
144 #endif
146         .end thread_start
148 weak_alias (__clone, clone)