From patchwork Tue Jun 1 14:55:15 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: 43648 X-Patchwork-Delegate: azanella@linux.vnet.ibm.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 34F5939D5C38 for ; Tue, 1 Jun 2021 15:00:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 34F5939D5C38 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1622559650; 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=QihUVjIcfsuAQ98fO5e7oYD47I22X2lhH2AmNeIU3wxWiOVyigxAtaNCj8jr+ZKWi WuVrFmYR8NuxNACaqytEMdqn5tdT2iM5PuqC6+zc+YqMUwBsNT3kB8sWExYBW9+dQi HpZRhctYvPCtAMHXj9NlX5tXw1qZXI/c0qw1s+1I= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pj1-x1032.google.com (mail-pj1-x1032.google.com [IPv6:2607:f8b0:4864:20::1032]) by sourceware.org (Postfix) with ESMTPS id 0F0AF39BF439 for ; Tue, 1 Jun 2021 14:55:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 0F0AF39BF439 Received: by mail-pj1-x1032.google.com with SMTP id m13-20020a17090b068db02901656cc93a75so1564400pjz.3 for ; Tue, 01 Jun 2021 07:55:20 -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=b5SqDEOguMp/JbPaADVRps26ISfao24PZa88OSvHW3pNyn7hz4woYAz7Ysg7h3lkAi xbdiG+bsMyGZbBP2EIYvTYaHxU2PBFNUoLH4HBwyskHXupShZEw6bgqwu1OXDqNpZAoM q8BhjjJ+ZlZdDw7AeHVTurligOzcTBlGVWQPXCzxtnNImdwBTzJNFLREifrI00mE321r dyXW8g8JmpPLj3YAaI0VqOhifdXVp+rDazvui5EvuFGudbcvTMD4oboNb2gh0PG3WgRJ tXDRfVmx9Qf7QwFD75dA6gnaM/l899IQcL01uzZ5hAkMlltAAAofQQYfVdCFAIOJfbF1 PMzQ== X-Gm-Message-State: AOAM530yKEjAsOPrfFNg7/JfVgMoR9rVLzK4BpoRz5td08DsqgpmWCJZ QvPlKIZT9BEGPNor8GGnF30= X-Google-Smtp-Source: ABdhPJxj9gMqH0XI7XC137NiEjVp257/ptSecMoDF5d5S7L2OOoglvjM8hGtCw0YT3ErtFoQzwthXg== X-Received: by 2002:a17:902:b609:b029:ec:e80d:7ebd with SMTP id b9-20020a170902b609b02900ece80d7ebdmr26520144pls.75.1622559319164; Tue, 01 Jun 2021 07:55:19 -0700 (PDT) Received: from gnu-cfl-2.localdomain ([172.56.38.102]) by smtp.gmail.com with ESMTPSA id o17sm14744896pgj.25.2021.06.01.07.55.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Jun 2021 07:55:17 -0700 (PDT) Received: from gnu-cfl-2.. (localhost [IPv6:::1]) by gnu-cfl-2.localdomain (Postfix) with ESMTP id 3A74EC039C; Tue, 1 Jun 2021 07:55:16 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v8 2/3] x86-64: Add the clone3 wrapper Date: Tue, 1 Jun 2021 07:55:15 -0700 Message-Id: <20210601145516.3553627-3-hjl.tools@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210601145516.3553627-1-hjl.tools@gmail.com> References: <20210601145516.3553627-1-hjl.tools@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-3034.8 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+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" extern int clone3 (struct clone_args *__cl_args, size_t __size, int (*__func) (void *__arg), void *__arg); Reviewed-by: Adhemerval Zanella --- 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__ */