xref: /netbsd-src/lib/libc/arch/sparc/sys/__clone.S (revision ce099b40997c43048fb78bd578195f81d2456523)
1*ce099b40Smartin/*	$NetBSD: __clone.S,v 1.6 2008/04/28 20:22:57 martin Exp $	*/
2fdda2239Schristos
3fdda2239Schristos/*-
4fdda2239Schristos * Copyright (c) 2001 The NetBSD Foundation, Inc.
5fdda2239Schristos * All rights reserved.
6fdda2239Schristos *
7fdda2239Schristos * This code is derived from software contributed to The NetBSD Foundation
8fdda2239Schristos * by Jason R. Thorpe.
9fdda2239Schristos *
10fdda2239Schristos * Redistribution and use in source and binary forms, with or without
11fdda2239Schristos * modification, are permitted provided that the following conditions
12fdda2239Schristos * are met:
13fdda2239Schristos * 1. Redistributions of source code must retain the above copyright
14fdda2239Schristos *    notice, this list of conditions and the following disclaimer.
15fdda2239Schristos * 2. Redistributions in binary form must reproduce the above copyright
16fdda2239Schristos *    notice, this list of conditions and the following disclaimer in the
17fdda2239Schristos *    documentation and/or other materials provided with the distribution.
18fdda2239Schristos *
19fdda2239Schristos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20fdda2239Schristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21fdda2239Schristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22fdda2239Schristos * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23fdda2239Schristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24fdda2239Schristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25fdda2239Schristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26fdda2239Schristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27fdda2239Schristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28fdda2239Schristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29fdda2239Schristos * POSSIBILITY OF SUCH DAMAGE.
30fdda2239Schristos */
31fdda2239Schristos
32fdda2239Schristos#include <sys/errno.h>
33fdda2239Schristos
34fdda2239Schristos#include "SYS.h"
35fdda2239Schristos
36fdda2239Schristos#ifdef WEAK_ALIAS
37fdda2239SchristosWEAK_ALIAS(clone, __clone)
38fdda2239Schristos#endif
39fdda2239Schristos
40fdda2239Schristos/*
41fdda2239Schristos * int __clone(int (*fn)(void *), void *stack, int flags, void *arg);
42fdda2239Schristos */
43fdda2239SchristosENTRY(__clone)
4455a9a999Smartin	save	%sp, -CCFSZ, %sp
45fdda2239Schristos
46fdda2239Schristos	/*
47fdda2239Schristos	 * Sanity checks: func and stack may not be NULL.
48fdda2239Schristos	 */
49fdda2239Schristos	tst	%i0
50fdda2239Schristos	be	8f		! func == NULL, bail
51fdda2239Schristos	 orcc	%i1, %g0, %o1	! setup stack arg for syscall, test
52fdda2239Schristos	be	8f		! stack == NULL, bail
53fdda2239Schristos	 mov	%i2, %o0	! setup flags arg for syscall
54fdda2239Schristos
55fdda2239Schristos	/*
561c4ebce5Smartin	 * Allocate "caller's" frame in the child stack as ABI
571c4ebce5Smartin	 * requires.
581c4ebce5Smartin	 *
591c4ebce5Smartin	 * We pass the function and the argument to the child by
601c4ebce5Smartin	 * stashing them at the bottom of the frame.  There they are
611c4ebce5Smartin	 * safe from window spill would we need to take one as it's
621c4ebce5Smartin	 * below the window save area.
63fdda2239Schristos	 */
641c4ebce5Smartin	sub	%o1, CCFSZ, %o1	! make space on the child's stack
651c4ebce5Smartin	st	%i0, [%o1+CCFSZ-8]	! save func
661c4ebce5Smartin	st	%i3, [%o1+CCFSZ-4]	! save arg
67fdda2239Schristos
68fdda2239Schristos	/*
69fdda2239Schristos	 * Args are now set up for system call as (flags, stack).
70fdda2239Schristos	 */
71fdda2239Schristos	mov	SYS___clone, %g1
72fdda2239Schristos	t	ST_SYSCALL
739ee271adSmartin	bcs	9f
74fdda2239Schristos	 tst	%o1		! %o1 (rv[1]) == 0 in parent
75fdda2239Schristos
76fdda2239Schristos1:	bz	2f		! yes, parent
771c4ebce5Smartin	 ld	[%sp+CCFSZ-8], %l0	! grab the function...
78fdda2239Schristos	call	%l0 		! Call the clone's entry point.
791c4ebce5Smartin	 ld	[%sp+CCFSZ-4], %o0	! ...and the argument from the stack
80fdda2239Schristos
81fdda2239Schristos	CALL(_C_LABEL(_exit))
82fdda2239Schristos	/* NOTREACHED */
83fdda2239Schristos
84fdda2239Schristos2:	ret
85fdda2239Schristos	 restore %g0, %o0, %o0
86fdda2239Schristos
87ce5186b5Smartin8:	restore	%g0, EINVAL, %o0
88ce5186b5Smartin	ERROR()
89ce5186b5Smartin	/* NOTREACHED */
90ce5186b5Smartin
91ce5186b5Smartin9:	restore %g0, %o0, %o0
92ce5186b5Smartin	ERROR()
93ce5186b5Smartin	/* NOTREACHED */
94