Home | History | Annotate | Line # | Download | only in sys
__clone.S revision 1.7.8.2
      1  1.7.8.2  martin /*	$NetBSD: __clone.S,v 1.7.8.2 2008/04/28 20:22:57 martin Exp $	*/
      2  1.7.8.2  martin 
      3  1.7.8.2  martin /*-
      4  1.7.8.2  martin  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5  1.7.8.2  martin  * All rights reserved.
      6  1.7.8.2  martin  *
      7  1.7.8.2  martin  * This code is derived from software contributed to The NetBSD Foundation
      8  1.7.8.2  martin  * by Jason R. Thorpe.
      9  1.7.8.2  martin  *
     10  1.7.8.2  martin  * Redistribution and use in source and binary forms, with or without
     11  1.7.8.2  martin  * modification, are permitted provided that the following conditions
     12  1.7.8.2  martin  * are met:
     13  1.7.8.2  martin  * 1. Redistributions of source code must retain the above copyright
     14  1.7.8.2  martin  *    notice, this list of conditions and the following disclaimer.
     15  1.7.8.2  martin  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.7.8.2  martin  *    notice, this list of conditions and the following disclaimer in the
     17  1.7.8.2  martin  *    documentation and/or other materials provided with the distribution.
     18  1.7.8.2  martin  *
     19  1.7.8.2  martin  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.7.8.2  martin  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.7.8.2  martin  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.7.8.2  martin  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.7.8.2  martin  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.7.8.2  martin  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.7.8.2  martin  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.7.8.2  martin  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.7.8.2  martin  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.7.8.2  martin  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.7.8.2  martin  * POSSIBILITY OF SUCH DAMAGE.
     30  1.7.8.2  martin  */
     31  1.7.8.2  martin 
     32  1.7.8.2  martin #include <sys/errno.h>
     33  1.7.8.2  martin 
     34  1.7.8.2  martin #include "SYS.h"
     35  1.7.8.2  martin 
     36  1.7.8.2  martin #ifdef WEAK_ALIAS
     37  1.7.8.2  martin WEAK_ALIAS(clone, __clone)
     38  1.7.8.2  martin #endif
     39  1.7.8.2  martin 
     40  1.7.8.2  martin /*
     41  1.7.8.2  martin  * int __clone(int (*fn)(void *), void *stack, int flags, void *arg);
     42  1.7.8.2  martin  */
     43  1.7.8.2  martin LEAF(__clone)
     44  1.7.8.2  martin 	PIC_PROLOGUE(__clone, t9)
     45  1.7.8.2  martin 
     46  1.7.8.2  martin 	/*
     47  1.7.8.2  martin 	 * Sanity checks: func and stack may not be NULL.
     48  1.7.8.2  martin 	 */
     49  1.7.8.2  martin 	li	v0, EINVAL
     50  1.7.8.2  martin 	beq	a0, zero, 8f
     51  1.7.8.2  martin 	beq	a1, zero, 8f
     52  1.7.8.2  martin 
     53  1.7.8.2  martin 	/*
     54  1.7.8.2  martin 	 * We need to be able to get at the func and arg arguments
     55  1.7.8.2  martin 	 * in the child.  Luckily, we have a convenient place to
     56  1.7.8.2  martin 	 * do this: the child's stack.
     57  1.7.8.2  martin 	 */
     58  1.7.8.2  martin 	subu	a1, a1, (2 * SZREG)
     59  1.7.8.2  martin 	REG_S	a0, 0(a1)
     60  1.7.8.2  martin 	REG_S	a3, SZREG(a1)
     61  1.7.8.2  martin 
     62  1.7.8.2  martin 	/*
     63  1.7.8.2  martin 	 * The system call expects (flags, stack).
     64  1.7.8.2  martin 	 */
     65  1.7.8.2  martin 	move	a0, a2
     66  1.7.8.2  martin 	SYSTRAP(__clone)
     67  1.7.8.2  martin 	bne	a3, zero, 8f
     68  1.7.8.2  martin 
     69  1.7.8.2  martin 	bne	v1, zero, 9f	# v1 (rv[1]) == 0, parent, child pid in v0
     70  1.7.8.2  martin 
     71  1.7.8.2  martin 	j	ra		# parent return
     72  1.7.8.2  martin 
     73  1.7.8.2  martin 8:	PIC_CALL(__cerror, t9)
     74  1.7.8.2  martin 	/* NOTREACHED */
     75  1.7.8.2  martin 
     76  1.7.8.2  martin 9:	/*
     77  1.7.8.2  martin 	 * Child: Fetch the function and argument from the new stack,
     78  1.7.8.2  martin 	 * and create a frame so that the child can be safely called.
     79  1.7.8.2  martin 	 *
     80  1.7.8.2  martin 	 * There are already register slots on the stack from above.
     81  1.7.8.2  martin 	 * We take 4 register slots more.  The lower 4 accommodate o32
     82  1.7.8.2  martin 	 * argument save area.  The next higher is used to save GP (if
     83  1.7.8.2  martin 	 * ABICALLS).  The highest is unused.
     84  1.7.8.2  martin 	 */
     85  1.7.8.2  martin 	REG_L	t9, 0(sp)
     86  1.7.8.2  martin 	REG_L	a0, SZREG(sp)
     87  1.7.8.2  martin 	subu	sp, sp, (4 * SZREG)
     88  1.7.8.2  martin 
     89  1.7.8.2  martin #ifdef __ABICALLS__
     90  1.7.8.2  martin 	.cprestore (4 * SZREG)
     91  1.7.8.2  martin #endif
     92  1.7.8.2  martin 
     93  1.7.8.2  martin 	/* Call the clone's entry point. */
     94  1.7.8.2  martin 	jal	t9
     95  1.7.8.2  martin 
     96  1.7.8.2  martin 	/* Pass the return value to _exit(). */
     97  1.7.8.2  martin 	move	a0, v0
     98  1.7.8.2  martin 	PIC_CALL(_exit, t9)
     99  1.7.8.2  martin 
    100  1.7.8.2  martin 	/* NOTREACHED */
    101  1.7.8.2  martin END(__clone)
    102