xref: /netbsd-src/lib/libc/arch/sparc64/sys/__clone.S (revision ce099b40997c43048fb78bd578195f81d2456523)
1*ce099b40Smartin/*	$NetBSD: __clone.S,v 1.7 2008/04/28 20:22:57 martin Exp $	*/
2f98eeabaSthorpej
3f98eeabaSthorpej/*-
4f98eeabaSthorpej * Copyright (c) 2001 The NetBSD Foundation, Inc.
5f98eeabaSthorpej * All rights reserved.
6f98eeabaSthorpej *
7f98eeabaSthorpej * This code is derived from software contributed to The NetBSD Foundation
8f98eeabaSthorpej * by Jason R. Thorpe.
9f98eeabaSthorpej *
10f98eeabaSthorpej * Redistribution and use in source and binary forms, with or without
11f98eeabaSthorpej * modification, are permitted provided that the following conditions
12f98eeabaSthorpej * are met:
13f98eeabaSthorpej * 1. Redistributions of source code must retain the above copyright
14f98eeabaSthorpej *    notice, this list of conditions and the following disclaimer.
15f98eeabaSthorpej * 2. Redistributions in binary form must reproduce the above copyright
16f98eeabaSthorpej *    notice, this list of conditions and the following disclaimer in the
17f98eeabaSthorpej *    documentation and/or other materials provided with the distribution.
18f98eeabaSthorpej *
19f98eeabaSthorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20f98eeabaSthorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21f98eeabaSthorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22f98eeabaSthorpej * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23f98eeabaSthorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24f98eeabaSthorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25f98eeabaSthorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26f98eeabaSthorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27f98eeabaSthorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28f98eeabaSthorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29f98eeabaSthorpej * POSSIBILITY OF SUCH DAMAGE.
30f98eeabaSthorpej */
31f98eeabaSthorpej
32f98eeabaSthorpej#include <sys/errno.h>
33f98eeabaSthorpej
34f98eeabaSthorpej#include "SYS.h"
35f98eeabaSthorpej
36f98eeabaSthorpej#ifdef WEAK_ALIAS
37f98eeabaSthorpejWEAK_ALIAS(clone, __clone)
38f98eeabaSthorpej#endif
39f98eeabaSthorpej
40f98eeabaSthorpej/*
41f98eeabaSthorpej * int __clone(int (*fn)(void *), void *stack, int flags, void *arg);
42f98eeabaSthorpej */
43f98eeabaSthorpejENTRY(__clone)
44586bc581Smartin	save	%sp, -CC64FSZ, %sp
45f98eeabaSthorpej
46f98eeabaSthorpej	/*
47f98eeabaSthorpej	 * Sanity checks: func and stack may not be NULL.
48f98eeabaSthorpej	 */
4997ab4e62Smartin	brz,pn	%i0,8f		! func == NULL, bail
50f98eeabaSthorpej	 orcc	%i1, %g0, %o1	! setup stack arg for syscall, test
5197ab4e62Smartin	bz,pn	%xcc, 8f	! stack == NULL, bail
52f98eeabaSthorpej	 mov	%i2, %o0	! setup flags arg for syscall
53f98eeabaSthorpej
54f98eeabaSthorpej	/*
55f666b179Smartin	 * Allocate "caller's" frame in the child stack as ABI
56f666b179Smartin	 * requires.  Subtract BIAS - it will be 64-bit code.
57f666b179Smartin	 *
58f666b179Smartin	 * We pass the function and the argument to the child by
59f666b179Smartin	 * stashing them at the bottom of the frame.  There they are
60f666b179Smartin	 * safe from window spill would we need to take one as it's
61f666b179Smartin	 * below the window save area.
62f98eeabaSthorpej	 */
63f666b179Smartin	sub	%o1, CC64FSZ+BIAS, %o1		! make space
64f666b179Smartin	stx	%i0, [%o1+CC64FSZ-16+BIAS]	! save func
65f666b179Smartin	stx	%i3, [%o1+CC64FSZ-8+BIAS]	! save arg
66f98eeabaSthorpej
67f98eeabaSthorpej	/*
68f98eeabaSthorpej	 * Args are now set up for system call as (flags, stack).
69f98eeabaSthorpej	 */
70f98eeabaSthorpej	mov	SYS___clone, %g1
71f98eeabaSthorpej	t	ST_SYSCALL
7297ab4e62Smartin	bcs,pn	%xcc, 9f
73f98eeabaSthorpej	 tst	%o1		! %o1 (rv[1]) == 0 in parent
7497ab4e62Smartin	bz	%xcc, 2f	! yes, parent
75f666b179Smartin	 ldx	[%sp+CC64FSZ-16+BIAS], %l0	! grab the function...
76f98eeabaSthorpej	call	%l0 		! Call the clone's entry point.
77f666b179Smartin	 ldx	[%sp+CC64FSZ-8+BIAS], %o0	! ...and the argument
78f98eeabaSthorpej
7922abdf82Seeh	JUMP(_exit)
80f98eeabaSthorpej	/* NOTREACHED */
81f98eeabaSthorpej
82f98eeabaSthorpej2:	ret
83f98eeabaSthorpej	 restore %g0, %o0, %o0
84f98eeabaSthorpej
8597ab4e62Smartin8:	restore %g0, EINVAL, %o0
8697ab4e62Smartin	ERROR()
8797ab4e62Smartin	/* NOTREACHED */
8897ab4e62Smartin
8997ab4e62Smartin9:	restore %o0, %g0, %o0
9097ab4e62Smartin	ERROR()
9197ab4e62Smartin	/* NOTREACHED */
92