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

Packit 6c4009
/* Copyright (C) 2001-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 <sysdep.h>
Packit 6c4009
#define _ERRNO_H	1
Packit 6c4009
#include <bits/errno.h>
Packit 6c4009
#include <asm-syntax.h>
Packit 6c4009
Packit 6c4009
/* The userland implementation is:
Packit 6c4009
   int clone (int (*fn)(void *arg), void *child_stack, int flags, void *arg),
Packit 6c4009
   the kernel entry is:
Packit 6c4009
   int clone (long flags, void *child_stack).
Packit 6c4009
Packit 6c4009
   The parameters are passed in register and on the stack from userland:
Packit 6c4009
   rdi: fn
Packit 6c4009
   rsi: child_stack
Packit 6c4009
   rdx:	flags
Packit 6c4009
   rcx: arg
Packit 6c4009
   r8d:	TID field in parent
Packit 6c4009
   r9d: thread pointer
Packit 6c4009
%esp+8:	TID field in child
Packit 6c4009
Packit 6c4009
   The kernel expects:
Packit 6c4009
   rax: system call number
Packit 6c4009
   rdi: flags
Packit 6c4009
   rsi: child_stack
Packit 6c4009
   rdx: TID field in parent
Packit 6c4009
   r10: TID field in child
Packit 6c4009
   r8:	thread pointer  */
Packit 6c4009
Packit 6c4009
Packit 6c4009
        .text
Packit 6c4009
ENTRY (__clone)
Packit 6c4009
	/* Sanity check arguments.  */
Packit 6c4009
	movq	$-EINVAL,%rax
Packit 6c4009
	testq	%rdi,%rdi		/* no NULL function pointers */
Packit 6c4009
	jz	SYSCALL_ERROR_LABEL
Packit 6c4009
	testq	%rsi,%rsi		/* no NULL stack pointers */
Packit 6c4009
	jz	SYSCALL_ERROR_LABEL
Packit 6c4009
Packit 6c4009
	/* Insert the argument onto the new stack.  */
Packit 6c4009
	subq	$16,%rsi
Packit 6c4009
	movq	%rcx,8(%rsi)
Packit 6c4009
Packit 6c4009
	/* Save the function pointer.  It will be popped off in the
Packit 6c4009
	   child in the ebx frobbing below.  */
Packit 6c4009
	movq	%rdi,0(%rsi)
Packit 6c4009
Packit 6c4009
	/* Do the system call.  */
Packit 6c4009
	movq	%rdx, %rdi
Packit 6c4009
	movq	%r8, %rdx
Packit 6c4009
	movq	%r9, %r8
Packit 6c4009
	mov	8(%rsp), %R10_LP
Packit 6c4009
	movl	$SYS_ify(clone),%eax
Packit 6c4009
Packit 6c4009
	/* End FDE now, because in the child the unwind info will be
Packit 6c4009
	   wrong.  */
Packit 6c4009
	cfi_endproc;
Packit 6c4009
	syscall
Packit 6c4009
Packit 6c4009
	testq	%rax,%rax
Packit 6c4009
	jl	SYSCALL_ERROR_LABEL
Packit 6c4009
	jz	L(thread_start)
Packit 6c4009
Packit 6c4009
	ret
Packit 6c4009
Packit 6c4009
L(thread_start):
Packit 6c4009
	cfi_startproc;
Packit 6c4009
	/* Clearing frame pointer is insufficient, use CFI.  */
Packit 6c4009
	cfi_undefined (rip);
Packit 6c4009
	/* Clear the frame pointer.  The ABI suggests this be done, to mark
Packit 6c4009
	   the outermost frame obviously.  */
Packit 6c4009
	xorl	%ebp, %ebp
Packit 6c4009
Packit 6c4009
	/* Set up arguments for the function call.  */
Packit 6c4009
	popq	%rax		/* Function to call.  */
Packit 6c4009
	popq	%rdi		/* Argument.  */
Packit 6c4009
	call	*%rax
Packit 6c4009
	/* Call exit with return value from function call. */
Packit 6c4009
	movq	%rax, %rdi
Packit 6c4009
	movl	$SYS_ify(exit), %eax
Packit 6c4009
	syscall
Packit 6c4009
	cfi_endproc;
Packit 6c4009
Packit 6c4009
	cfi_startproc;
Packit 6c4009
PSEUDO_END (__clone)
Packit 6c4009
Packit 6c4009
libc_hidden_def (__clone)
Packit 6c4009
weak_alias (__clone, clone)