Blame sysdeps/unix/sysv/linux/riscv/clone.S

Packit 6c4009
/* Wrapper around clone system call.  RISC-V version.
Packit 6c4009
   Copyright (C) 1996-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library.  If not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
/* clone() is even more special than fork() as it mucks with stacks
Packit 6c4009
   and invokes a function in the right context after its all over.  */
Packit 6c4009
Packit 6c4009
#include <sys/asm.h>
Packit 6c4009
#include <sysdep.h>
Packit 6c4009
#define _ERRNO_H	1
Packit 6c4009
#include <bits/errno.h>
Packit 6c4009
#include <tls.h>
Packit 6c4009
#include "tcb-offsets.h"
Packit 6c4009
Packit 6c4009
/* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg,
Packit 6c4009
	     void *parent_tidptr, void *tls, void *child_tidptr) */
Packit 6c4009
Packit 6c4009
	.text
Packit 6c4009
LEAF (__clone)
Packit 6c4009
Packit 6c4009
	/* Sanity check arguments.  */
Packit 6c4009
	beqz		a0,L (invalid)	/* No NULL function pointers.  */
Packit 6c4009
	beqz		a1,L (invalid)	/* No NULL stack pointers.  */
Packit 6c4009
Packit 6c4009
	addi		a1,a1,-16	/* Reserve argument save space.  */
Packit 6c4009
	REG_S		a0,0(a1)	/* Save function pointer.  */
Packit 6c4009
	REG_S		a3,SZREG(a1)	/* Save argument pointer.  */
Packit 6c4009
Packit 6c4009
	/* The syscall expects the args to be in different slots.  */
Packit 6c4009
	mv		a0,a2
Packit 6c4009
	mv		a2,a4
Packit 6c4009
	mv		a3,a5
Packit 6c4009
	mv		a4,a6
Packit 6c4009
Packit 6c4009
	/* Do the system call.  */
Packit 6c4009
	li		a7,__NR_clone
Packit 6c4009
	scall
Packit 6c4009
Packit 6c4009
	bltz		a0,L (error)
Packit 6c4009
	beqz		a0,L (thread_start)
Packit 6c4009
Packit 6c4009
	/* Successful return from the parent.  */
Packit 6c4009
	ret
Packit 6c4009
Packit 6c4009
L (invalid):
Packit 6c4009
	li		a0, -EINVAL
Packit 6c4009
	/* Something bad happened -- no child created.  */
Packit 6c4009
L (error):
Packit 6c4009
	j		__syscall_error
Packit 6c4009
	END (__clone)
Packit 6c4009
Packit 6c4009
/* Load up the arguments to the function.  Put this block of code in
Packit 6c4009
   its own function so that we can terminate the stack trace with our
Packit 6c4009
   debug info.  */
Packit 6c4009
Packit 6c4009
ENTRY (__thread_start)
Packit 6c4009
L (thread_start):
Packit 6c4009
	/* Restore the arg for user's function.  */
Packit 6c4009
	REG_L		a1,0(sp)	/* Function pointer.  */
Packit 6c4009
	REG_L		a0,SZREG(sp)	/* Argument pointer.  */
Packit 6c4009
Packit 6c4009
	/* Call the user's function.  */
Packit 6c4009
	jalr		a1
Packit 6c4009
Packit 6c4009
	/* Call exit with the function's return value.  */
Packit 6c4009
	li		a7, __NR_exit
Packit 6c4009
	scall
Packit 6c4009
Packit 6c4009
	END (__thread_start)
Packit 6c4009
Packit 6c4009
libc_hidden_def (__clone)
Packit 6c4009
weak_alias (__clone, clone)