From patchwork Wed Jul 14 13:33:27 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: 44342 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 9AD813984051 for ; Wed, 14 Jul 2021 13:34:12 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9AD813984051 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1626269652; bh=Z4bAMKAg95gm2jUWRlqJjOgP+nI/JyYPqlfDkVZ7SjE=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=acToFxaPtHsGpnvDXq5wC00lVINR2bfzPdbHNR/XptwKDagKDo2W96XKsUqAuJhjA 5REi9nc/Fe2Vki5vdefo8ztGFMV+8w7VDICHAtHaK322/H/6nJQELVeXDUzfbhmprB VAgfkjhHM15PM0eqYkMl18dhn0XYcSzkge9zezA0= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x432.google.com (mail-pf1-x432.google.com [IPv6:2607:f8b0:4864:20::432]) by sourceware.org (Postfix) with ESMTPS id 4D3033984046 for ; Wed, 14 Jul 2021 13:33:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 4D3033984046 Received: by mail-pf1-x432.google.com with SMTP id 17so2055914pfz.4 for ; Wed, 14 Jul 2021 06:33:40 -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:mime-version :content-transfer-encoding; bh=Z4bAMKAg95gm2jUWRlqJjOgP+nI/JyYPqlfDkVZ7SjE=; b=dXhKhLWWMMl34+8N9X4mT3gTBARykne7TySwnaYeipQrdzRkVEp4DaFMIettEV1rPH ZLSR8SOIgQBFBOVOuCS5dCSGxjhcNJYO9TRRJKEGKa+QctXArzTZjK0n5COJ6PDArcWb P5sEr4wMsDNP1ZwRVQnmhd/AHm3A8g75MpcfR6R+Y5WqoJu69Cb6M5/Cjm8egky9nn12 6Oi0MYMhb8MfjFk591zg9/nSi6J/lSZE1MS19/AETjQNY4uoJDA3OkhSbn64eBV+LyEd 3QsgrzgM8WxyLfoLb9mkENwLCtadu95GIU9925NxUheHBuSqrIp5SQGjAvd49grnhN+w y8Sg== X-Gm-Message-State: AOAM531dzsVjCubViPABhXVz9EoiMNYijjrtvWJhupavB1sEb/qjhT0I bzwLAD/fjcA+AUKjp5gI+rY= X-Google-Smtp-Source: ABdhPJy34j3DaR/4ZvIxVfFllpx/CGqpzhoMAuFHAdA/Ec6flO9JlelWUD32t5S8uyjXlcloGNEwHg== X-Received: by 2002:a65:56ca:: with SMTP id w10mr9477326pgs.107.1626269619501; Wed, 14 Jul 2021 06:33:39 -0700 (PDT) Received: from gnu-cfl-2.localdomain ([172.56.39.115]) by smtp.gmail.com with ESMTPSA id d23sm2404821pjd.25.2021.07.14.06.33.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Jul 2021 06:33:39 -0700 (PDT) Received: from gnu-tgl-2.localdomain (gnu-tgl-2 [192.168.1.34]) by gnu-cfl-2.localdomain (Postfix) with ESMTPS id DC5D6C0048; Wed, 14 Jul 2021 06:33:37 -0700 (PDT) Received: from gnu-tgl-2.lan (localhost [IPv6:::1]) by gnu-tgl-2.localdomain (Postfix) with ESMTP id B7E8F3004C3; Wed, 14 Jul 2021 06:33:27 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH] i386: Add the clone3 wrapper Date: Wed, 14 Jul 2021 06:33:27 -0700 Message-Id: <20210714133327.3102313-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Spam-Status: No, score=-3032.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_BARRACUDACENTRAL, 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-Patchwork-Original-From: "H.J. Lu via Libc-alpha" From: "H.J. Lu" Reply-To: "H.J. Lu" 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: Carlos O'Donell --- sysdeps/unix/sysv/linux/i386/clone3.S | 123 ++++++++++++++++++++++++++ sysdeps/unix/sysv/linux/i386/sysdep.h | 2 + 2 files changed, 125 insertions(+) create mode 100644 sysdeps/unix/sysv/linux/i386/clone3.S diff --git a/sysdeps/unix/sysv/linux/i386/clone3.S b/sysdeps/unix/sysv/linux/i386/clone3.S new file mode 100644 index 0000000000..bef3ce0455 --- /dev/null +++ b/sysdeps/unix/sysv/linux/i386/clone3.S @@ -0,0 +1,123 @@ +/* The clone3 syscall wrapper. Linux/i386 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 on stack from userland: + 16(%esp) arg + 12(%esp) func + 8(%esp) size + 4(%esp) cl_args + (%esp) Return address + + The kernel expects: + eax: system call number + ebx: cl_args + ecx: size + */ + +#define CL_ARGS 4 +#define SIZE 8 +#define FUNC 12 +#define ARG 16 + + .text +ENTRY (__clone3) + /* Sanity check arguments. */ + movl $-EINVAL, %eax + movl CL_ARGS(%esp), %ecx /* No NULL cl_args pointer. */ + testl %ecx, %ecx + jz SYSCALL_ERROR_LABEL + /* Save the function pointer in EDX which is preserved by the + system call. */ + movl FUNC(%esp), %edx /* No NULL function pointer. */ + testl %edx, %edx + jz SYSCALL_ERROR_LABEL + + /* Save EBX and ESI. */ + pushl %ebx + cfi_adjust_cfa_offset (4) + pushl %esi + cfi_adjust_cfa_offset (4) + + /* Save the function argument in ESI which is preserved by the + system call. */ + movl (ARG + 8)(%esp), %esi + + /* Put cl_args in EBX. */ + movl %ecx, %ebx + + /* Put size in ECX. */ + movl (SIZE + 8)(%esp), %ecx + + /* Do the system call. */ + movl $SYS_ify(clone3), %eax + + /* End FDE now, because in the child the unwind info will be + wrong. */ + cfi_endproc + + int $0x80 + test %eax, %eax + /* No need to restore EBX and ESI in child. */ + jz L(thread_start) + + /* Restore EBX and ESI in parent. */ + pop %esi + pop %ebx + jl SYSCALL_ERROR_LABEL + + ret + +L(thread_start): + cfi_startproc + /* Clearing frame pointer is insufficient, use CFI. */ + cfi_undefined (eip) + xorl %ebp, %ebp /* Terminate the stack frame. */ + + /* Align stack to 16 bytes per the i386 psABI. */ + andl $-16, %esp + + /* The PUSH below will decrement stack pointer by 4 bytes. */ + subl $12, %esp + + /* Set up the argument for the function call. */ + pushl %esi /* Argument. */ + cfi_adjust_cfa_offset (4) + call *%edx /* Call function. */ + + /* Call exit with return value from function call. */ + movl %eax, %ebx + movl $SYS_ify(exit), %eax + ENTER_KERNEL + cfi_endproc + + cfi_startproc +PSEUDO_END (__clone3) + +libc_hidden_def (__clone3) +weak_alias (__clone3, clone3) diff --git a/sysdeps/unix/sysv/linux/i386/sysdep.h b/sysdeps/unix/sysv/linux/i386/sysdep.h index 8680b49bf7..3927a1a6e0 100644 --- a/sysdeps/unix/sysv/linux/i386/sysdep.h +++ b/sysdeps/unix/sysv/linux/i386/sysdep.h @@ -291,6 +291,8 @@ struct libc_do_syscall_args # define HAVE_TIME_VSYSCALL "__vdso_time" # define HAVE_CLOCK_GETRES_VSYSCALL "__vdso_clock_getres" +# define HAVE_CLONE3_WAPPER 1 + # undef HAVE_INTERNAL_BRK_ADDR_SYMBOL # define HAVE_INTERNAL_BRK_ADDR_SYMBOL 1