From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pg1-x536.google.com (mail-pg1-x536.google.com [IPv6:2607:f8b0:4864:20::536]) by sourceware.org (Postfix) with ESMTPS id 62AD938515F6 for ; Tue, 13 Jul 2021 19:13:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 62AD938515F6 Received: by mail-pg1-x536.google.com with SMTP id 62so22468702pgf.1 for ; Tue, 13 Jul 2021 12:13:02 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=kpCe3XNTmhOoB/eV/w7vGSy2ebP3TsBxzdjVc+Uklzc=; b=Ee8piS6K3QCUgig9v/QIDn3hygKCTCDzUuq8hB95jQZD8ziBIt6/HsfCZ1cFbe4ETy S6QvRvT2UDVYUsSIjwY6j3O4mBT+eYkcys4IwDJrRk6V0ZT9q8MAvvmtq5g1swH1/aCp sNIsIrV21q7XDaKJZiZDhpHMCcwKULcchV9ac6Qoj2WwJMc8ViCV4lBL9oy6zuHKrKUn zg5w0Gy3F/SctEWqXngpMrWpFKWw83xK33JiAI2d0ebKjFCREAxBx5CFyFI/MGtBkuUK HXD3CjxQcd9l+o8IQX6gp3STgsQdGL1wRr2RIcnqkb09pvS3Tt2BA8XXdCYiCF3G+khv HWDQ== X-Gm-Message-State: AOAM5336UJGtEIKdKNWE0JdFjO8cfZWmcAAOWAEeLbpVRG/Y+bKFysUJ cruZnoFAhuZP1rnQwLGB7vIshw== X-Google-Smtp-Source: ABdhPJxKuCM+sObNvZzgJJw56Rct9uNF2hq2OFaGB5jw25qy61AG1pd1by0GhaPiFlFo1Z/BpDAcqg== X-Received: by 2002:aa7:8b07:0:b029:2f7:d38e:ff1 with SMTP id f7-20020aa78b070000b02902f7d38e0ff1mr6213695pfd.72.1626203581258; Tue, 13 Jul 2021 12:13:01 -0700 (PDT) Received: from [192.168.1.108] ([177.194.59.218]) by smtp.gmail.com with ESMTPSA id o1sm17204135pjf.56.2021.07.13.12.12.59 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 13 Jul 2021 12:13:00 -0700 (PDT) Subject: Re: [PATCH v8 2/3] x86-64: Add the clone3 wrapper To: "H.J. Lu" , libc-alpha@sourceware.org Cc: Florian Weimer , Noah Goldstein References: <20210601145516.3553627-1-hjl.tools@gmail.com> <20210601145516.3553627-3-hjl.tools@gmail.com> From: Adhemerval Zanella Message-ID: Date: Tue, 13 Jul 2021 16:12:57 -0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: <20210601145516.3553627-3-hjl.tools@gmail.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, NICE_REPLY_A, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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-List-Received-Date: Tue, 13 Jul 2021 19:13:04 -0000 LGTM, thanks. Reviewed-by: Adhemerval Zanella On 01/06/2021 11:55, H.J. Lu wrote: > 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__ */ > Ok.