From patchwork Sat May 15 12:34:41 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: 43431 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 A918F3951C65; Sat, 15 May 2021 12:34:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A918F3951C65 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1621082089; bh=Lc4t2B1yg6vZf3Ya2xYSNRSVvE0bf4Il53PZSVmn/0E=; 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=sv1c0Za7aX753M0dIyNHmfnz9YZwCr9L+fV1epWLbiCM9RwHNRftc3t+T9actxip0 BmGZEDsZaUyVOl+mJoQ8iZQsOv7dnq4u776iFCPGwwWIsAiSQzMG7Je1bxMpLCx2Gg Q30g9KRnoMaaRSkDqqDN59OpUvPq8GQD6rgUuE6E= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pj1-x102b.google.com (mail-pj1-x102b.google.com [IPv6:2607:f8b0:4864:20::102b]) by sourceware.org (Postfix) with ESMTPS id AA6BD38515E4 for ; Sat, 15 May 2021 12:34:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org AA6BD38515E4 Received: by mail-pj1-x102b.google.com with SMTP id g24so1173034pji.4 for ; Sat, 15 May 2021 05:34:45 -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=Lc4t2B1yg6vZf3Ya2xYSNRSVvE0bf4Il53PZSVmn/0E=; b=HVw+KRFNTAyUu+0hmzOJGc7fmFQ6216DBWOnsWc1Ly06tJLzl/GkAqwwNsNCDjyTM2 mhsjJloLbUK6VDUE8g2OxMZvDXUe1oqQFNcQQ91LGS5Zw5AqLua69v82ibjIKcoQo6hW BzYLiH0DeYxmOzxr4YTYFHIkJRuyNmoLMNwGvlxePLrYSUtLbD2KzBIxAp60tmqRwNFI wfhrP5T/jtuPU4ST3z1+VZoclpBkiwPSJy+eSUFg7VhCM4rmWYdELIYSttsiRCsp2aTF gTuMz2UujWU9/P+pUnTSPjqlmjrkrgPVbxGBp7etPNY7a6ZVXcNXHU1m9MGa1H+sU8dg YHUA== X-Gm-Message-State: AOAM532vypAqRPHtLvdozBWtGuGs4NlgAHctVPRawh7N/8QVKARdpG+Y SXebYEFXoQOpkJhfo4EKR3ayIEVGWtDozw== X-Google-Smtp-Source: ABdhPJwwjVi4FdjK0LjEc/iOqtwQTREcpwl1qEJvAYOQbQUo7wOYswNOjOIC+Nx/AsiaCBy94u5huA== X-Received: by 2002:a17:90a:f690:: with SMTP id cl16mr58981684pjb.207.1621082084821; Sat, 15 May 2021 05:34:44 -0700 (PDT) Received: from gnu-cfl-2.localdomain ([172.56.39.231]) by smtp.gmail.com with ESMTPSA id m8sm5903037pfk.49.2021.05.15.05.34.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 15 May 2021 05:34:44 -0700 (PDT) Received: from gnu-cfl-2.. (localhost [IPv6:::1]) by gnu-cfl-2.localdomain (Postfix) with ESMTP id 587BEC0412; Sat, 15 May 2021 05:34:42 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v5 4/5] x86-64: Add the clone3 wrapper Date: Sat, 15 May 2021 05:34:41 -0700 Message-Id: <20210515123442.1432385-5-hjl.tools@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210515123442.1432385-1-hjl.tools@gmail.com> References: <20210515123442.1432385-1-hjl.tools@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-3035.3 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, 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..f7d4036a6a --- /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 +#include + +/* The userland implementation is: + int clone3 (struct clone_args *cl_args, 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: func + rdx: arg + + The kernel expects: + rax: system call number + rdi: cl_args + rsi: size */ + + .text +ENTRY (__clone3) + /* Sanity check arguments. */ + movq $-EINVAL, %rax + testq %rdi, %rdi /* No NULL cl_args pointer. */ + jz SYSCALL_ERROR_LABEL + testq %rsi, %rsi /* No NULL function pointer. */ + jz SYSCALL_ERROR_LABEL + + /* Save the function pointer in R8 which is preserved by the + syscall. */ + movq %rsi, %r8 + + /* Put sizeof (struct clone_args) in ESI. */ + movl $CLONE_ARGS_SIZE , %esi + + /* 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 + + /* Set up arguments for the function call. */ + movq %rdx, %rdi /* Argument. */ + call *%r8 /* 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__ */