Home | History | Annotate | Line # | Download | only in sys
__clone.S revision 1.6.10.1
      1 /* $NetBSD: __clone.S,v 1.6.10.1 2007/08/28 17:36:30 matt Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2001 Christopher Gilbert
      5  * All rights reserved.
      6  *
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  * 3. The name of the company nor the name of the author may be used to
     13  *    endorse or promote products derived from this software without specific
     14  *    prior written permission.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     22  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <machine/asm.h>
     30 #include <sys/errno.h>
     31 #include "SYS.h"
     32 
     33 #ifdef WEAK_ALIAS
     34 WEAK_ALIAS(clone, __clone)
     35 #endif
     36 
     37 /*
     38  * int __clone(int (*fn)(void *), void *stack, int flags, void *arg);
     39  */
     40 ENTRY(__clone)
     41 	/* test stack and function are not null */
     42 #ifdef __thumb__
     43 	cmp	r0, #0
     44 	beq	.Leinval
     45 	cmp	r1, #0
     46 	beq	.Leinval
     47 #else
     48 	teq	r0, #0x00
     49 	teqne	r1, #0x00
     50 	beq	.Leinval
     51 #endif
     52 
     53 	/* place the func and its arg onto the child's stack */
     54 #ifdef __thumb__
     55 	sub	r1, #8
     56 	str	r0, [r1, #0]
     57 	str	r3, [r1, #4]
     58 #else
     59 	stmfd	r1!, {r0, r3}
     60 #endif
     61 
     62 	/* syscall expects (flags, stack) */
     63 	mov	r0, r2
     64 
     65 	SYSTRAP(__clone)
     66 	bcs	.Lcerror
     67 
     68 	/*
     69 	 * r1 and r0 are the same as from fork:
     70 	 * r1 == 0 in parent process, r1 == 1 in child process.
     71 	 * r0 == pid of child in parent, r0 == pid of parent in child.
     72 	 */
     73 #ifdef __thumb__
     74 	cmp	r1, #0
     75 	bne	1f
     76 	RET
     77 1:
     78 #else
     79 	teq	r1, #0x00
     80 
     81 	/* if this is the parent then just return the pid */
     82 	RETc(eq)
     83 #endif
     84 	/*
     85 	 * This is the child
     86 	 * load the function and arg off the stack
     87 	 */
     88 	XPOP	{r1, r2}
     89 
     90 	/* setup return address */
     91 #ifdef __thumb__
     92 	add	r0, pc, #.Lreturnhere - . - 8
     93 	mov	lr, r0
     94 #else
     95 	add	lr, pc, #.Lreturnhere - . - 8
     96 #endif
     97 
     98 	/* place arg in r0 */
     99 	mov	r0, r2
    100 
    101 	/* call the function */
    102 #ifdef _ARM_ARCH_4T
    103 	bx	r1
    104 #else
    105 	mov	pc, r1
    106 #endif
    107 
    108 .Lreturnhere:
    109 	/* call _exit with the returned value */
    110 	bl	PLT_SYM(_C_LABEL(_exit))
    111 
    112 	/* NOTREACHED */
    113 
    114 	/* error handler if func or stack is NULL */
    115 .Leinval:
    116 	mov	r0, #EINVAL
    117 .Lcerror:
    118 	_SYSCALL_CERROR
    119 END(__clone)
    120