xref: /netbsd-src/lib/libc/arch/riscv/sys/__clone.S (revision 35c3446182b5d2ae37a831f3e9c1597901314cac)
1*35c34461Smatt/*	$NetBSD: __clone.S,v 1.3 2015/03/27 23:23:14 matt Exp $	*/
26cf6fe02Smatt
36cf6fe02Smatt/*-
46cf6fe02Smatt * Copyright (c) 2001 The NetBSD Foundation, Inc.
56cf6fe02Smatt * All rights reserved.
66cf6fe02Smatt *
76cf6fe02Smatt * This code is derived from software contributed to The NetBSD Foundation
86cf6fe02Smatt * by Jason R. Thorpe.
96cf6fe02Smatt *
106cf6fe02Smatt * Redistribution and use in source and binary forms, with or without
116cf6fe02Smatt * modification, are permitted provided that the following conditions
126cf6fe02Smatt * are met:
136cf6fe02Smatt * 1. Redistributions of source code must retain the above copyright
146cf6fe02Smatt *    notice, this list of conditions and the following disclaimer.
156cf6fe02Smatt * 2. Redistributions in binary form must reproduce the above copyright
166cf6fe02Smatt *    notice, this list of conditions and the following disclaimer in the
176cf6fe02Smatt *    documentation and/or other materials provided with the distribution.
186cf6fe02Smatt *
196cf6fe02Smatt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
206cf6fe02Smatt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
216cf6fe02Smatt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
226cf6fe02Smatt * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
236cf6fe02Smatt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
246cf6fe02Smatt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
256cf6fe02Smatt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
266cf6fe02Smatt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
276cf6fe02Smatt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
286cf6fe02Smatt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
296cf6fe02Smatt * POSSIBILITY OF SUCH DAMAGE.
306cf6fe02Smatt */
316cf6fe02Smatt
326cf6fe02Smatt#include <sys/errno.h>
336cf6fe02Smatt
346cf6fe02Smatt#include "SYS.h"
356cf6fe02Smatt
366cf6fe02Smatt#if defined(SYSLIBC_SCCS) && !defined(lint)
37*35c34461Smatt	RCSID("$NetBSD: __clone.S,v 1.3 2015/03/27 23:23:14 matt Exp $");
386cf6fe02Smatt#endif /* SYSLIBC_SCCS and not lint */
396cf6fe02Smatt
406cf6fe02Smatt#ifdef WEAK_ALIAS
416cf6fe02SmattWEAK_ALIAS(clone, __clone)
426cf6fe02Smatt#endif
436cf6fe02Smatt
446cf6fe02Smatt/*
456cf6fe02Smatt * int __clone(int (*fn)(void *), void *stack, int flags, void *arg);
466cf6fe02Smatt */
476cf6fe02SmattENTRY(__clone)
486cf6fe02Smatt	/*
496cf6fe02Smatt	 * Sanity checks: func and stack may not be NULL.
506cf6fe02Smatt	 */
518a3081d2Smatt	mv	t0, a0			/* we need a0 for return value */
528a3081d2Smatt	li	a0, EINVAL
538a3081d2Smatt	beqz	t0, 8f
546cf6fe02Smatt	beqz	a1, 8f
556cf6fe02Smatt
566cf6fe02Smatt	/*
576cf6fe02Smatt	 * We need to be able to get at the func and arg arguments
586cf6fe02Smatt	 * in the child.  Luckily, we have a convenient place to
596cf6fe02Smatt	 * do this: the child's stack.
606cf6fe02Smatt	 */
616cf6fe02Smatt	addi	a1, a1, -CALLFRAME_SIZ
628a3081d2Smatt	REG_S	t0, 0(a1)
636cf6fe02Smatt	REG_S	a3, SZREG(a1)
646cf6fe02Smatt
656cf6fe02Smatt	/*
666cf6fe02Smatt	 * The system call expects (flags, stack).
676cf6fe02Smatt	 */
686cf6fe02Smatt	mv	a0, a2
696cf6fe02Smatt	SYSTRAP(__clone)
708a3081d2Smatt	# a1 (rv[1]) == 0, parent, child pid in a0
716cf6fe02Smatt8:	JUMP_TO_CERROR()	/* error */
728a3081d2Smatt	bnez	a1, 9f		/* success */
736cf6fe02Smatt	ret			# parent return
746cf6fe02Smatt
756cf6fe02Smatt	/* NOTREACHED */
766cf6fe02Smatt
776cf6fe02Smatt9:	/*
786cf6fe02Smatt	 * Child: Fetch the function and argument from the new stack and create
796cf6fe02Smatt	 a frame so that the child can be safely called.
806cf6fe02Smatt	 *
816cf6fe02Smatt	 * There are already register slots on the stack from above.
826cf6fe02Smatt	 * They already include the o32 argument save area.  The
836cf6fe02Smatt	 * highest is unused.  a1 should equal sp now.
846cf6fe02Smatt	 */
856cf6fe02Smatt
868a3081d2Smatt	REG_L		t0, 0(sp)
876cf6fe02Smatt	REG_L		a0, SZREG(sp)
886cf6fe02Smatt
896cf6fe02Smatt	REG_S		zero, CALLFRAME_RA(sp)	/* make sure stack frame ends */
906cf6fe02Smatt
916cf6fe02Smatt	/* Call the clone's entry point. */
928a3081d2Smatt	jalr		t0
936cf6fe02Smatt
946cf6fe02Smatt	/* Pass the return value to _exit. */
95*35c34461Smatt	tail		_C_LABEL(_exit)
966cf6fe02Smatt
976cf6fe02Smatt	/* NOTREACHED */
986cf6fe02SmattEND(__clone)
99