xref: /netbsd-src/lib/libc/arch/riscv/sys/fork.S (revision 75b842b84762e0251223b719bf77fbef2e651bf6)
1*75b842b8Sskrll/*	$NetBSD: fork.S,v 1.3 2023/05/07 12:41:47 skrll Exp $	*/
26cf6fe02Smatt
36cf6fe02Smatt
46cf6fe02Smatt/*-
56cf6fe02Smatt * Copyright (c) 2014 The NetBSD Foundation, Inc.
66cf6fe02Smatt * All rights reserved.
76cf6fe02Smatt *
86cf6fe02Smatt * This code is derived from software contributed to The NetBSD Foundation
96cf6fe02Smatt * by Matt Thomas of 3am Software Foundry.
106cf6fe02Smatt *
116cf6fe02Smatt * Redistribution and use in source and binary forms, with or without
126cf6fe02Smatt * modification, are permitted provided that the following conditions
136cf6fe02Smatt * are met:
146cf6fe02Smatt * 1. Redistributions of source code must retain the above copyright
156cf6fe02Smatt *    notice, this list of conditions and the following disclaimer.
166cf6fe02Smatt * 2. Redistributions in binary form must reproduce the above copyright
176cf6fe02Smatt *    notice, this list of conditions and the following disclaimer in the
186cf6fe02Smatt *    documentation and/or other materials provided with the distribution.
196cf6fe02Smatt *
206cf6fe02Smatt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
216cf6fe02Smatt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
226cf6fe02Smatt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
236cf6fe02Smatt * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
246cf6fe02Smatt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
256cf6fe02Smatt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
266cf6fe02Smatt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
276cf6fe02Smatt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
286cf6fe02Smatt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
296cf6fe02Smatt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
306cf6fe02Smatt * POSSIBILITY OF SUCH DAMAGE.
316cf6fe02Smatt */
326cf6fe02Smatt#include "SYS.h"
336cf6fe02Smatt
346cf6fe02Smatt#if defined(LIBC_SCCS) && !defined(lint)
35*75b842b8Sskrll	RCSID("$NetBSD: fork.S,v 1.3 2023/05/07 12:41:47 skrll Exp $")
366cf6fe02Smatt#endif /* LIBC_SCCS and not lint */
376cf6fe02Smatt
38*75b842b8Sskrll/*
39*75b842b8Sskrll * pid = fork();
40*75b842b8Sskrll *
41*75b842b8Sskrll * On return from the SYSTRAP:
42*75b842b8Sskrll * a1 == 0 in parent process, a1 == 1 in child process.
43*75b842b8Sskrll * a0 == pid of child in parent, a0 == pid of parent in child.
44*75b842b8Sskrll */
45*75b842b8Sskrll
466cf6fe02SmattENTRY(__fork)
476cf6fe02Smatt	SYSTRAP(fork)		// pid = fork()
486cf6fe02Smatt	JUMP_TO_CERROR()	/* error */
496cf6fe02Smatt	/* success */
508a3081d2Smatt	addi	a1, a1, -1	// a1 0->0xffffffff in parent, 1->0 in child
518a3081d2Smatt	and	a0, a0, a1	// a0 = child pid in parent, 0 in child
526cf6fe02Smatt	ret
536cf6fe02SmattEND(__fork)
54