Blame sysdeps/unix/sysv/linux/x86_64/vfork.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
#include <sysdep.h>
Packit Service 82fcde
#define _ERRNO_H	1
Packit Service 82fcde
#include <bits/errno.h>
Packit Service 82fcde
#include <tcb-offsets.h>
Packit Service 82fcde
Packit Service 82fcde
#if SHSTK_ENABLED
Packit Service 82fcde
/* The shadow stack prevents us from pushing the saved return PC onto
Packit Service 82fcde
   the stack and returning normally.  Instead we pop the shadow stack
Packit Service 82fcde
   and return directly.  This is the safest way to return and ensures
Packit Service 82fcde
   any stack manipulations done by the vfork'd child doesn't cause the
Packit Service 82fcde
   parent to terminate when CET is enabled.  */
Packit Service 82fcde
# undef SYSCALL_ERROR_HANDLER
Packit Service 82fcde
# define SYSCALL_ERROR_HANDLER			\
Packit Service 82fcde
0:						\
Packit Service 82fcde
  SYSCALL_SET_ERRNO;				\
Packit Service 82fcde
  or $-1, %RAX_LP;				\
Packit Service 82fcde
  jmp 1b;
Packit Service 82fcde
# undef SYSCALL_ERROR_LABEL
Packit Service 82fcde
# define SYSCALL_ERROR_LABEL 0f
Packit Service 82fcde
#endif
Packit Service 82fcde
Packit Service 82fcde
/* Clone the calling process, but without copying the whole address space.
Packit Service 82fcde
   The calling process is suspended until the new process exits or is
Packit Service 82fcde
   replaced by a call to `execve'.  Return -1 for errors, 0 to the new process,
Packit Service 82fcde
   and the process ID of the new process to the old process.  */
Packit Service 82fcde
Packit Service 82fcde
ENTRY (__vfork)
Packit Service 82fcde
Packit Service 82fcde
	/* Pop the return PC value into RDI.  We need a register that
Packit Service 82fcde
	   is preserved by the syscall and that we're allowed to destroy. */
Packit Service 82fcde
	popq	%rdi
Packit Service 82fcde
	cfi_adjust_cfa_offset(-8)
Packit Service 82fcde
	cfi_register(%rip, %rdi)
Packit Service 82fcde
Packit Service 82fcde
	/* Stuff the syscall number in RAX and enter into the kernel.  */
Packit Service 82fcde
	movl	$SYS_ify (vfork), %eax
Packit Service 82fcde
	syscall
Packit Service 82fcde
Packit Service 82fcde
#if !SHSTK_ENABLED
Packit Service 82fcde
	/* Push back the return PC.  */
Packit Service 82fcde
	pushq	%rdi
Packit Service 82fcde
	cfi_adjust_cfa_offset(8)
Packit Service 82fcde
#endif
Packit Service 82fcde
Packit Service 82fcde
	cmpl	$-4095, %eax
Packit Service 82fcde
	jae SYSCALL_ERROR_LABEL		/* Branch forward if it failed.  */
Packit Service 82fcde
Packit Service 82fcde
#if SHSTK_ENABLED
Packit Service 82fcde
1:
Packit Service 82fcde
	/* Check if shadow stack is in use.  */
Packit Service 82fcde
	xorl	%esi, %esi
Packit Service 82fcde
	rdsspq	%rsi
Packit Service 82fcde
	testq	%rsi, %rsi
Packit Service 82fcde
	/* Normal return if shadow stack isn't in use.  */
Packit Service 82fcde
	je	L(no_shstk)
Packit Service 82fcde
Packit Service 82fcde
	/* Pop return address from shadow stack and jump back to caller
Packit Service 82fcde
	   directly.  */
Packit Service 82fcde
	movl	$1, %esi
Packit Service 82fcde
	incsspq	%rsi
Packit Service 82fcde
	jmp	*%rdi
Packit Service 82fcde
Packit Service 82fcde
L(no_shstk):
Packit Service 82fcde
	/* Push back the return PC.  */
Packit Service 82fcde
	pushq	%rdi
Packit Service 82fcde
	cfi_adjust_cfa_offset(8)
Packit Service 82fcde
#endif
Packit Service 82fcde
Packit Service 82fcde
	/* Normal return.  */
Packit Service 82fcde
	ret
Packit Service 82fcde
Packit Service 82fcde
PSEUDO_END (__vfork)
Packit Service 82fcde
libc_hidden_def (__vfork)
Packit Service 82fcde
Packit Service 82fcde
weak_alias (__vfork, vfork)
Packit Service 82fcde
strong_alias (__vfork, __libc_vfork)