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

Packit Service 82fcde
/* Copyright (C) 2001-2018 Free Software Foundation, Inc.
Packit Service 82fcde
   This file is part of the GNU C Library.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 82fcde
   modify it under the terms of the GNU Lesser General Public
Packit Service 82fcde
   License as published by the Free Software Foundation; either
Packit Service 82fcde
   version 2.1 of the License, or (at your option) any later version.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 82fcde
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 82fcde
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 82fcde
   Lesser General Public License for more details.
Packit Service 82fcde
Packit Service 82fcde
   You should have received a copy of the GNU Lesser General Public
Packit Service 82fcde
   License along with the GNU C Library; if not, see
Packit Service 82fcde
   <http://www.gnu.org/licenses/>.  */
Packit Service 82fcde
Packit Service 82fcde
/* clone() is even more special than fork() as it mucks with stacks
Packit Service 82fcde
   and invokes a function in the right context after its all over.  */
Packit Service 82fcde
Packit Service 82fcde
#include <sysdep.h>
Packit Service 82fcde
#define _ERRNO_H	1
Packit Service 82fcde
#include <bits/errno.h>
Packit Service 82fcde
#include <asm-syntax.h>
Packit Service 82fcde
Packit Service 82fcde
/* The userland implementation is:
Packit Service 82fcde
   int clone (int (*fn)(void *arg), void *child_stack, int flags, void *arg),
Packit Service 82fcde
   the kernel entry is:
Packit Service 82fcde
   int clone (long flags, void *child_stack).
Packit Service 82fcde
Packit Service 82fcde
   The parameters are passed in register and on the stack from userland:
Packit Service 82fcde
   rdi: fn
Packit Service 82fcde
   rsi: child_stack
Packit Service 82fcde
   rdx:	flags
Packit Service 82fcde
   rcx: arg
Packit Service 82fcde
   r8d:	TID field in parent
Packit Service 82fcde
   r9d: thread pointer
Packit Service 82fcde
%esp+8:	TID field in child
Packit Service 82fcde
Packit Service 82fcde
   The kernel expects:
Packit Service 82fcde
   rax: system call number
Packit Service 82fcde
   rdi: flags
Packit Service 82fcde
   rsi: child_stack
Packit Service 82fcde
   rdx: TID field in parent
Packit Service 82fcde
   r10: TID field in child
Packit Service 82fcde
   r8:	thread pointer  */
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
        .text
Packit Service 82fcde
ENTRY (__clone)
Packit Service 82fcde
	/* Sanity check arguments.  */
Packit Service 82fcde
	movq	$-EINVAL,%rax
Packit Service 82fcde
	testq	%rdi,%rdi		/* no NULL function pointers */
Packit Service 82fcde
	jz	SYSCALL_ERROR_LABEL
Packit Service 82fcde
	testq	%rsi,%rsi		/* no NULL stack pointers */
Packit Service 82fcde
	jz	SYSCALL_ERROR_LABEL
Packit Service 82fcde
Packit Service 82fcde
	/* Insert the argument onto the new stack.  */
Packit Service 82fcde
	subq	$16,%rsi
Packit Service 82fcde
	movq	%rcx,8(%rsi)
Packit Service 82fcde
Packit Service 82fcde
	/* Save the function pointer.  It will be popped off in the
Packit Service 82fcde
	   child in the ebx frobbing below.  */
Packit Service 82fcde
	movq	%rdi,0(%rsi)
Packit Service 82fcde
Packit Service 82fcde
	/* Do the system call.  */
Packit Service 82fcde
	movq	%rdx, %rdi
Packit Service 82fcde
	movq	%r8, %rdx
Packit Service 82fcde
	movq	%r9, %r8
Packit Service 82fcde
	mov	8(%rsp), %R10_LP
Packit Service 82fcde
	movl	$SYS_ify(clone),%eax
Packit Service 82fcde
Packit Service 82fcde
	/* End FDE now, because in the child the unwind info will be
Packit Service 82fcde
	   wrong.  */
Packit Service 82fcde
	cfi_endproc;
Packit Service 82fcde
	syscall
Packit Service 82fcde
Packit Service 82fcde
	testq	%rax,%rax
Packit Service 82fcde
	jl	SYSCALL_ERROR_LABEL
Packit Service 82fcde
	jz	L(thread_start)
Packit Service 82fcde
Packit Service 82fcde
	ret
Packit Service 82fcde
Packit Service 82fcde
L(thread_start):
Packit Service 82fcde
	cfi_startproc;
Packit Service 82fcde
	/* Clearing frame pointer is insufficient, use CFI.  */
Packit Service 82fcde
	cfi_undefined (rip);
Packit Service 82fcde
	/* Clear the frame pointer.  The ABI suggests this be done, to mark
Packit Service 82fcde
	   the outermost frame obviously.  */
Packit Service 82fcde
	xorl	%ebp, %ebp
Packit Service 82fcde
Packit Service 82fcde
	/* Set up arguments for the function call.  */
Packit Service 82fcde
	popq	%rax		/* Function to call.  */
Packit Service 82fcde
	popq	%rdi		/* Argument.  */
Packit Service 82fcde
	call	*%rax
Packit Service 82fcde
	/* Call exit with return value from function call. */
Packit Service 82fcde
	movq	%rax, %rdi
Packit Service 82fcde
	movl	$SYS_ify(exit), %eax
Packit Service 82fcde
	syscall
Packit Service 82fcde
	cfi_endproc;
Packit Service 82fcde
Packit Service 82fcde
	cfi_startproc;
Packit Service 82fcde
PSEUDO_END (__clone)
Packit Service 82fcde
Packit Service 82fcde
libc_hidden_def (__clone)
Packit Service 82fcde
weak_alias (__clone, clone)