From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1039) id C205A3984069; Wed, 14 Jul 2021 13:37:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C205A3984069 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: H.J. Lu To: glibc-cvs@sourceware.org Subject: [glibc] x86-64: Add the clone3 wrapper X-Act-Checkin: glibc X-Git-Author: H.J. Lu X-Git-Refname: refs/heads/master X-Git-Oldrev: d8ea0d0168b190bdf138a20358293c939509367f X-Git-Newrev: 24c78e2c759f278ced4256dfe9d88af06af81743 Message-Id: <20210714133724.C205A3984069@sourceware.org> Date: Wed, 14 Jul 2021 13:37:24 +0000 (GMT) X-BeenThere: glibc-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Glibc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 14 Jul 2021 13:37:24 -0000 https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=24c78e2c759f278ced4256dfe9d88af06af81743 commit 24c78e2c759f278ced4256dfe9d88af06af81743 Author: H.J. Lu Date: Thu May 13 08:24:36 2021 -0700 x86-64: Add the clone3 wrapper extern int clone3 (struct clone_args *__cl_args, size_t __size, int (*__func) (void *__arg), void *__arg); Reviewed-by: Adhemerval Zanella Diff: --- sysdeps/unix/sysv/linux/x86_64/clone3.S | 92 +++++++++++++++++++++++++++++++++ sysdeps/unix/sysv/linux/x86_64/sysdep.h | 2 + 2 files changed, 94 insertions(+) diff --git a/sysdeps/unix/sysv/linux/x86_64/clone3.S b/sysdeps/unix/sysv/linux/x86_64/clone3.S new file mode 100644 index 0000000000..71caaecc29 --- /dev/null +++ b/sysdeps/unix/sysv/linux/x86_64/clone3.S @@ -0,0 +1,92 @@ +/* The clone3 syscall wrapper. Linux/x86-64 version. + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +/* clone3() is even more special than fork() as it mucks with stacks + and invokes a function in the right context after its all over. */ + +#include + +/* The userland implementation is: + int clone3 (struct clone_args *cl_args, size_t size, + int (*func)(void *arg), void *arg); + the kernel entry is: + int clone3 (struct clone_args *cl_args, size_t size); + + The parameters are passed in registers from userland: + rdi: cl_args + rsi: size + rdx: func + rcx: arg + + The kernel expects: + rax: system call number + rdi: cl_args + rsi: size */ + + .text +ENTRY (__clone3) + /* Sanity check arguments. */ + movl $-EINVAL, %eax + test %RDI_LP, %RDI_LP /* No NULL cl_args pointer. */ + jz SYSCALL_ERROR_LABEL + test %RDX_LP, %RDX_LP /* No NULL function pointer. */ + jz SYSCALL_ERROR_LABEL + + /* Save the cl_args pointer in R8 which is preserved by the + syscall. */ + mov %RCX_LP, %R8_LP + + /* Do the system call. */ + movl $SYS_ify(clone3), %eax + + /* End FDE now, because in the child the unwind info will be + wrong. */ + cfi_endproc + syscall + + test %RAX_LP, %RAX_LP + jl SYSCALL_ERROR_LABEL + jz L(thread_start) + + ret + +L(thread_start): + cfi_startproc + /* Clearing frame pointer is insufficient, use CFI. */ + cfi_undefined (rip) + /* Clear the frame pointer. The ABI suggests this be done, to mark + the outermost frame obviously. */ + xorl %ebp, %ebp + + /* Align stack to 16 bytes per the x86-64 psABI. */ + and $-16, %RSP_LP + + /* Set up arguments for the function call. */ + mov %R8_LP, %RDI_LP /* Argument. */ + call *%rdx /* Call function. */ + /* Call exit with return value from function call. */ + movq %rax, %rdi + movl $SYS_ify(exit), %eax + syscall + cfi_endproc + + cfi_startproc +PSEUDO_END (__clone3) + +libc_hidden_def (__clone3) +weak_alias (__clone3, clone3) diff --git a/sysdeps/unix/sysv/linux/x86_64/sysdep.h b/sysdeps/unix/sysv/linux/x86_64/sysdep.h index dbad2c788a..f26ffc68ae 100644 --- a/sysdeps/unix/sysv/linux/x86_64/sysdep.h +++ b/sysdeps/unix/sysv/linux/x86_64/sysdep.h @@ -377,6 +377,8 @@ # define HAVE_GETCPU_VSYSCALL "__vdso_getcpu" # define HAVE_CLOCK_GETRES64_VSYSCALL "__vdso_clock_getres" +# define HAVE_CLONE3_WAPPER 1 + # define SINGLE_THREAD_BY_GLOBAL 1 #endif /* __ASSEMBLER__ */