From patchwork Wed May 26 12:39:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 43584 X-Patchwork-Delegate: fweimer@redhat.com Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6773F384F02D; Wed, 26 May 2021 12:40:03 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6773F384F02D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1622032803; bh=NFnw/idFnvnE3iSB1f8BcjWWhAcB1nhsuSndNwU6PoM=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=Ib+nhAPa6ocGFktLQ8OGcM2//Ie1DXavDnu8tZvHadNVGzZTPeJ69mTTxjAThUcIz GKXDowx52EkJYC7riKHXK+aasPpM6w7AD2i9QUOa2qOTL9h9ypzArdAYslDJTFigaB Wy+DVqhQNmOSjdYlxemYZNVJRpljnXjyCKWJnsG0= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pg1-x532.google.com (mail-pg1-x532.google.com [IPv6:2607:f8b0:4864:20::532]) by sourceware.org (Postfix) with ESMTPS id 4B6DF3857403 for ; Wed, 26 May 2021 12:40:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 4B6DF3857403 Received: by mail-pg1-x532.google.com with SMTP id t193so839842pgb.4 for ; Wed, 26 May 2021 05:40:00 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=NFnw/idFnvnE3iSB1f8BcjWWhAcB1nhsuSndNwU6PoM=; b=cUQZORBI5x0HK5nFOUvLpRYVPQzQrhkmMICr5gdyy2I3ztblLlrPc2bOSU5ELqBdC/ x9BcQcglbqZfvCr5nzsX8K8b57Dptgm9mmSXHUC2C1hhF+tNRE8FNaTOlT5mTnAVzbvO cw9zl/Loa93h6V7Fut4rvoxu+0emDrRXXWRMkxHnR5rCY1hV5HKFJup+JF1mFZBaq6mo wdDOqrc9d2R0ShDq5XSNLidNGtCCFvrNWyHE2K6Q4K+mNbOdO6Cez6kgebso2YDQoAXO 4ZlanB5U1HieogHsL8W/JvEDk7xw4a2OXYAZxrIySw8sQbVZlPOs0pHA+1ClUIiR7Rt+ cr1g== X-Gm-Message-State: AOAM533Xe6sDErZ4CD+WhTB48AJl1cBlzUu9k9n0jpQLCCYr8ppt7usV TbaxXi/3K/s5P4pOWnBTwyk= X-Google-Smtp-Source: ABdhPJwVksvCQt9p3UNin81YyDGy9HOEEo4WlEIYBsY4p7+jZsHEP5t83HNXdLGoj225y/AneIQwaw== X-Received: by 2002:a63:561d:: with SMTP id k29mr24789832pgb.335.1622032799366; Wed, 26 May 2021 05:39:59 -0700 (PDT) Received: from gnu-cfl-2.localdomain ([172.56.38.102]) by smtp.gmail.com with ESMTPSA id f16sm7210837pju.12.2021.05.26.05.39.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 May 2021 05:39:58 -0700 (PDT) Received: from gnu-cfl-2.. (localhost [IPv6:::1]) by gnu-cfl-2.localdomain (Postfix) with ESMTP id 0EE11C04CE; Wed, 26 May 2021 05:39:57 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v7 2/3] x86-64: Add the clone3 wrapper Date: Wed, 26 May 2021 05:39:55 -0700 Message-Id: <20210526123956.2712353-3-hjl.tools@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210526123956.2712353-1-hjl.tools@gmail.com> References: <20210526123956.2712353-1-hjl.tools@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-3035.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: "H.J. Lu via Libc-alpha" From: "H.J. Lu" Reply-To: "H.J. Lu" Cc: Florian Weimer Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" extern int clone3 (struct clone_args *__cl_args, size_t __size, int (*__func) (void *__arg), void *__arg); --- sysdeps/unix/sysv/linux/x86_64/clone3.S | 92 +++++++++++++++++++++++++ sysdeps/unix/sysv/linux/x86_64/sysdep.h | 2 + 2 files changed, 94 insertions(+) create mode 100644 sysdeps/unix/sysv/linux/x86_64/clone3.S 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__ */