From nobody Mon Jun 22 14:24:02 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 416DCC433F5 for ; Tue, 22 Mar 2022 10:21:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232950AbiCVKXS (ORCPT ); Tue, 22 Mar 2022 06:23:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43394 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232981AbiCVKXD (ORCPT ); Tue, 22 Mar 2022 06:23:03 -0400 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8F6017EB3B for ; Tue, 22 Mar 2022 03:21:36 -0700 (PDT) Received: from integral2.. (unknown [182.2.69.158]) by gnuweeb.org (Postfix) with ESMTPSA id 22F1B7E33D; Tue, 22 Mar 2022 10:21:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1647944495; bh=223IIXvAVF8dvE1omCKX0nJn3bhN+15baNDRjWgZABw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FBwOO27KcIc0I6tBNoeyls7EqfAwxtDkaKmPrOtjOqszEaYtEFw3w6XP132DKTIMQ 7Id6ESH3s5FppDJ1S/iE/K5nswQ8VfjV7wTMedWZpqvf5lV03tcWbAfmt14389a5C5 syOBLaMpm8EUmOe4oxoCTg2YXZtFISwAVkiH1Vwsu3NCjndMdl64ku49sfWH9XiRkg HYqlpKGX8WKoDfZYwc4RuQSYi2kDjmbkeSKi6y0FYp7xztxffJR6baBAfKRBPtXwRE GrW6jVKXtK19DZD5i6tvOYBQNADCpPr52Z3+fAniSNBsodc2jV55mLgfgigD0gfRXk aQ0gc14Haaf3w== From: Ammar Faizi To: Willy Tarreau Cc: "Paul E. McKenney" , Alviro Iskandar Setiawan , Nugraha , Linux Kernel Mailing List , GNU/Weeb Mailing List , Ammar Faizi Subject: [RFC PATCH v2 1/8] tools/nolibc: x86-64: Update System V ABI document link Date: Tue, 22 Mar 2022 17:21:08 +0700 Message-Id: <20220322102115.186179-2-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220322102115.186179-1-ammarfaizi2@gnuweeb.org> References: <20220322102115.186179-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The old link no longer works, update it. Signed-off-by: Ammar Faizi --- @@ Changelog: Link RFC v1: https://lore.kernel.org/lkml/20220320093750.159991-2-ammarf= aizi2@gnuweeb.org RFC v1 -> RFC v2: * No changes * --- tools/include/nolibc/arch-x86_64.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/include/nolibc/arch-x86_64.h b/tools/include/nolibc/arch= -x86_64.h index fe517c16cd4d..a7b70ea51b68 100644 --- a/tools/include/nolibc/arch-x86_64.h +++ b/tools/include/nolibc/arch-x86_64.h @@ -61,7 +61,7 @@ struct sys_stat_struct { * - see also x86-64 ABI section A.2 AMD64 Linux Kernel Conventions, A.2= .1 * Calling Conventions. * - * Link x86-64 ABI: https://gitlab.com/x86-psABIs/x86-64-ABI/-/wikis/x86-6= 4-psABI + * Link x86-64 ABI: https://gitlab.com/x86-psABIs/x86-64-ABI/-/wikis/home * */ =20 --=20 Ammar Faizi From nobody Mon Jun 22 14:24:02 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A894EC433EF for ; Tue, 22 Mar 2022 10:21:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232924AbiCVKXK (ORCPT ); Tue, 22 Mar 2022 06:23:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43414 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233036AbiCVKXH (ORCPT ); Tue, 22 Mar 2022 06:23:07 -0400 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D1F617EB3B for ; Tue, 22 Mar 2022 03:21:40 -0700 (PDT) Received: from integral2.. (unknown [182.2.69.158]) by gnuweeb.org (Postfix) with ESMTPSA id 64C1B7E308; Tue, 22 Mar 2022 10:21:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1647944500; bh=Y2yOZug2HGJL8VFMD6EqH4ZMxTYA+8NhLWRdkoEiaiI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iU1ZJPKJ6OWuXzbkmXhjXlHLVN4GyQDrbU48G2W/1KGHQV0QAHqrc1/z0dJ43rDql DfHracclBX+ZX2sAsx2bVZ+nI5wZO5CiVd/lcgfmiPB4hVFRPVg9ZxRoiXrOf5cOPF tfxJ0P4OFxBqLjee9lxqOl7Z9NMgFg3i3pw5w6ndmidjveU46MBw6M90u1KeF7Oo82 KoNNkfr9Cor7zJ1DNim0nSE4qkxmHJ7wSN9QWAdcUAjOOk2T3LYyt4I/6MmwiUK3F8 sYMykrXoqDE2mGkWynu80NPF+vZSOOHGfIuygTmQFmfMFlA1yqsTD7UQHR0hfnNGPE j6okpatS7+61A== From: Ammar Faizi To: Willy Tarreau Cc: "Paul E. McKenney" , Alviro Iskandar Setiawan , Nugraha , Linux Kernel Mailing List , GNU/Weeb Mailing List , Ammar Faizi , llvm@lists.linux.dev, Nick Desaulniers Subject: [RFC PATCH v2 2/8] tools/nolibc: Remove .global _start from the entry point code Date: Tue, 22 Mar 2022 17:21:09 +0700 Message-Id: <20220322102115.186179-3-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220322102115.186179-1-ammarfaizi2@gnuweeb.org> References: <20220322102115.186179-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Building with clang yields the following error: ``` :3:1: error: _start changed binding to STB_GLOBAL .global _start ^ 1 error generated. ``` Make sure only specify one between `.global _start` and `.weak _start`. Removing `.global _start`. Cc: llvm@lists.linux.dev Cc: Nick Desaulniers Signed-off-by: Ammar Faizi Reviewed-by: Nick Desaulniers --- @@ Changelog: Link RFC v1: https://lore.kernel.org/llvm/20220320093750.159991-3-ammarf= aizi2@gnuweeb.org RFC v1 -> RFC v2: - Remove all `.global _start` for all build (GCC and Clang) instead of removing all `.weak _start` for clang build (Comment from Willy). --- tools/include/nolibc/arch-aarch64.h | 1 - tools/include/nolibc/arch-arm.h | 1 - tools/include/nolibc/arch-i386.h | 1 - tools/include/nolibc/arch-mips.h | 1 - tools/include/nolibc/arch-riscv.h | 1 - tools/include/nolibc/arch-x86_64.h | 1 - 6 files changed, 6 deletions(-) diff --git a/tools/include/nolibc/arch-aarch64.h b/tools/include/nolibc/arc= h-aarch64.h index 87d9e434820c..2dbd80d633cb 100644 --- a/tools/include/nolibc/arch-aarch64.h +++ b/tools/include/nolibc/arch-aarch64.h @@ -184,7 +184,6 @@ struct sys_stat_struct { /* startup code */ asm(".section .text\n" ".weak _start\n" - ".global _start\n" "_start:\n" "ldr x0, [sp]\n" // argc (x0) was in the stack "add x1, sp, 8\n" // argv (x1) =3D sp diff --git a/tools/include/nolibc/arch-arm.h b/tools/include/nolibc/arch-ar= m.h index 001a3c8c9ad5..1191395b5acd 100644 --- a/tools/include/nolibc/arch-arm.h +++ b/tools/include/nolibc/arch-arm.h @@ -177,7 +177,6 @@ struct sys_stat_struct { /* startup code */ asm(".section .text\n" ".weak _start\n" - ".global _start\n" "_start:\n" #if defined(__THUMBEB__) || defined(__THUMBEL__) /* We enter here in 32-bit mode but if some previous functions were in diff --git a/tools/include/nolibc/arch-i386.h b/tools/include/nolibc/arch-i= 386.h index d7e4d53325a3..125a691fc631 100644 --- a/tools/include/nolibc/arch-i386.h +++ b/tools/include/nolibc/arch-i386.h @@ -176,7 +176,6 @@ struct sys_stat_struct { */ asm(".section .text\n" ".weak _start\n" - ".global _start\n" "_start:\n" "pop %eax\n" // argc (first arg, %eax) "mov %esp, %ebx\n" // argv[] (second arg, %ebx) diff --git a/tools/include/nolibc/arch-mips.h b/tools/include/nolibc/arch-m= ips.h index c9a6aac87c6d..1a124790c99f 100644 --- a/tools/include/nolibc/arch-mips.h +++ b/tools/include/nolibc/arch-mips.h @@ -192,7 +192,6 @@ struct sys_stat_struct { asm(".section .text\n" ".weak __start\n" ".set nomips16\n" - ".global __start\n" ".set noreorder\n" ".option pic0\n" ".ent __start\n" diff --git a/tools/include/nolibc/arch-riscv.h b/tools/include/nolibc/arch-= riscv.h index bc10b7b5706d..511d67fc534e 100644 --- a/tools/include/nolibc/arch-riscv.h +++ b/tools/include/nolibc/arch-riscv.h @@ -185,7 +185,6 @@ struct sys_stat_struct { /* startup code */ asm(".section .text\n" ".weak _start\n" - ".global _start\n" "_start:\n" ".option push\n" ".option norelax\n" diff --git a/tools/include/nolibc/arch-x86_64.h b/tools/include/nolibc/arch= -x86_64.h index a7b70ea51b68..84c174181425 100644 --- a/tools/include/nolibc/arch-x86_64.h +++ b/tools/include/nolibc/arch-x86_64.h @@ -199,7 +199,6 @@ struct sys_stat_struct { */ asm(".section .text\n" ".weak _start\n" - ".global _start\n" "_start:\n" "pop %rdi\n" // argc (first arg, %rdi) "mov %rsp, %rsi\n" // argv[] (second arg, %rsi) --=20 Ammar Faizi From nobody Mon Jun 22 14:24:02 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A153FC433F5 for ; Tue, 22 Mar 2022 10:21:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232975AbiCVKXX (ORCPT ); Tue, 22 Mar 2022 06:23:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43474 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232891AbiCVKXM (ORCPT ); Tue, 22 Mar 2022 06:23:12 -0400 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 021CE80206 for ; Tue, 22 Mar 2022 03:21:46 -0700 (PDT) Received: from integral2.. (unknown [182.2.69.158]) by gnuweeb.org (Postfix) with ESMTPSA id 5684D7E333; Tue, 22 Mar 2022 10:21:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1647944505; bh=dIEcw13d58BgwsAsKr9aSvq4e+T/YoaDZ/K+GATzEMo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QZexaRnht7B/2rtHCZZKPWmZStsWAr7sC+7MfIHszEaGUn64SWrsVVwW7HPL8w9/q KZYqcFu8U8uiubFOfCuZq5WG1q3RMqDOc0vO1HjMtP0wX05X+LuCoSCkq2cimr5MI+ kh1WqEvM/8aLv5FgILZ4kmkfBZBgWYqeKzYMloAuuRxeoe+qRcwx5VJ3V+GmVt0qQB r2dNYnisQwaR5IBq35gzpCQMbQAONpz+Oz3B9tL06rAO9RAPCt8bJx8YW+pqK6q4oY pT9gcnC85RsDmUxn3vQ4GHcZzE4Y8OYyQifp/IcZXABdMcN6YpWqGtYpIE3obQtxH6 aJ/E9994dz62w== From: Ammar Faizi To: Willy Tarreau Cc: "Paul E. McKenney" , Alviro Iskandar Setiawan , Nugraha , Linux Kernel Mailing List , GNU/Weeb Mailing List , Ammar Faizi , x86@kernel.org, llvm@lists.linux.dev, David Laight Subject: [RFC PATCH v2 3/8] tools/nolibc: i386: Implement syscall with 6 arguments Date: Tue, 22 Mar 2022 17:21:10 +0700 Message-Id: <20220322102115.186179-4-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220322102115.186179-1-ammarfaizi2@gnuweeb.org> References: <20220322102115.186179-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" On i386, the 6th argument of syscall goes in %ebp. However, both Clang and GCC cannot use %ebp in the clobber list and in the "r" constraint without using -fomit-frame-pointer. To make it always available for any kind of compilation, the below workaround is implemented. For clang (the Assembly statement can't clobber %ebp): 1) Push the 6-th argument. 2) Push %ebp. 3) Load the 6-th argument from 4(%esp) to %ebp. 4) Do the syscall (int $0x80). 5) Pop %ebp (restore the old value of %ebp). 6) Add %esp by 4 (undo the stack pointer). For GCC, fortunately it has a #pragma that can force a specific function to be compiled with -fomit-frame-pointer, so it can use "r"(var) where var is a variable bound to %ebp. Cc: x86@kernel.org Cc: llvm@lists.linux.dev Link: https://lore.kernel.org/lkml/2e335ac54db44f1d8496583d97f9dab0@AcuMS.a= culab.com Suggested-by: David Laight Signed-off-by: Ammar Faizi --- @@ Changelog: Link RFC v1: https://lore.kernel.org/llvm/20220320093750.159991-4-ammarf= aizi2@gnuweeb.org RFC v1 -> RFC v2: - Fix %ebp saving method. Don't use redzone, i386 doesn't have a redzone (comment from David and Alviro). --- tools/include/nolibc/arch-i386.h | 66 ++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/tools/include/nolibc/arch-i386.h b/tools/include/nolibc/arch-i= 386.h index 125a691fc631..9f4dc36e6ac2 100644 --- a/tools/include/nolibc/arch-i386.h +++ b/tools/include/nolibc/arch-i386.h @@ -167,6 +167,72 @@ struct sys_stat_struct { _ret; \ }) =20 + +/* + * Both Clang and GCC cannot use %ebp in the clobber list and in the "r" + * constraint without using -fomit-frame-pointer. To make it always + * available for any kind of compilation, the below workaround is + * implemented. + * + * For clang (the Assembly statement can't clobber %ebp): + * 1) Push the 6-th argument. + * 2) Push %ebp. + * 3) Load the 6-th argument from 4(%esp) to %ebp. + * 4) Do the syscall (int $0x80). + * 5) Pop %ebp (restore the old value of %ebp). + * 6) Add %esp by 4 (undo the stack pointer). + * + * For GCC, fortunately it has a #pragma that can force a specific function + * to be compiled with -fomit-frame-pointer, so it can use "r"(var) where + * var is a variable bound to %ebp. + * + */ +#if defined(__clang__) +static inline long ____do_syscall6(long eax, long ebx, long ecx, long edx, + long esi, long edi, long ebp) +{ + __asm__ volatile ( + "pushl %[arg6]\n\t" + "pushl %%ebp\n\t" + "movl 4(%%esp), %%ebp\n\t" + "int $0x80\n\t" + "popl %%ebp\n\t" + "addl $4,%%esp\n\t" + : "=3Da"(eax) + : "a"(eax), "b"(ebx), "c"(ecx), "d"(edx), "S"(esi), "D"(edi), + [arg6]"m"(ebp) + : "memory", "cc" + ); + return eax; +} + +#else /* #if defined(__clang__) */ +#pragma GCC push_options +#pragma GCC optimize "-fomit-frame-pointer" +static long ____do_syscall6(long eax, long ebx, long ecx, long edx, long e= si, + long edi, long ebp) +{ + register long __ebp __asm__("ebp") =3D ebp; + __asm__ volatile ( + "int $0x80" + : "=3Da"(eax) + : "a"(eax), "b"(ebx), "c"(ecx), "d"(edx), "S"(esi), "D"(edi), + "r"(__ebp) + : "memory", "cc" + ); + return eax; +} +#pragma GCC pop_options +#endif /* #if defined(__clang__) */ + +#define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) ( \ + ____do_syscall6((long)(num), (long)(arg1), \ + (long)(arg2), (long)(arg3), \ + (long)(arg4), (long)(arg5), \ + (long)(arg6)) \ +) + + /* startup code */ /* * i386 System V ABI mandates: --=20 Ammar Faizi From nobody Mon Jun 22 14:24:02 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A077DC433F5 for ; Tue, 22 Mar 2022 10:22:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232985AbiCVKXa (ORCPT ); Tue, 22 Mar 2022 06:23:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43872 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232984AbiCVKXZ (ORCPT ); Tue, 22 Mar 2022 06:23:25 -0400 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ADECC80221 for ; Tue, 22 Mar 2022 03:21:49 -0700 (PDT) Received: from integral2.. (unknown [182.2.69.158]) by gnuweeb.org (Postfix) with ESMTPSA id 09D957E2FC; Tue, 22 Mar 2022 10:21:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1647944509; bh=p66YvFuGrGyt3VddGxZU7iLnQ3idpcgTeeL7pb2/ZMc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=na7uKYyAv5UF8SI1p/DQHmVtDdz56ZlALpZLQPCGfgB6jCOJt+7PZW8WuJI6k5UL4 Fo9OHOcXEhTUp2Qii1BuH5l96VspuqJxZ3To6waUQqTA+3N5h4hsRiPfUnQfBoug++ hFxJfMmvDfFeu1Ns9Z7BxVArFbVjHYDF801h1w4cb7Qyrg32x25fkepyaSGsGcAyrL r2W4azmW9ED0MELvqF/lAvxUrQjXyh0J2BQqh5sMycdvX4oMMChsDxyoX1uC8l47wT NGc7hlQCab+NfhkGctCsb1EmUo0H8dDuKUOxPriHudRhthxA+tc1aTD+9dZxLjK7Wd rfeFXxWApMw2g== From: Ammar Faizi To: Willy Tarreau Cc: "Paul E. McKenney" , Alviro Iskandar Setiawan , Nugraha , Linux Kernel Mailing List , GNU/Weeb Mailing List , Ammar Faizi Subject: [RFC PATCH v2 4/8] tools/nolibc/sys: Implement `mmap()` and `munmap()` Date: Tue, 22 Mar 2022 17:21:11 +0700 Message-Id: <20220322102115.186179-5-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220322102115.186179-1-ammarfaizi2@gnuweeb.org> References: <20220322102115.186179-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Implement mmap() and munmap(). Currently, they are only available for architecures that have my_syscall6 macro. For architectures that don't have, this function will return -1 with errno set to ENOSYS (Function not implemented). This has been tested on x86 and i386. Notes for i386: 1) The common mmap() syscall implementation uses __NR_mmap2 instead of __NR_mmap. 2) The offset must be shifted-right by 12-bit. Signed-off-by: Ammar Faizi --- @@ Changelog: Link RFC v1: https://lore.kernel.org/lkml/20220320093750.159991-5-ammarf= aizi2@gnuweeb.org/ RFC v1 -> RFC v2: * No changes * --- tools/include/nolibc/sys.h | 62 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h index 4d4308d5d111..08491070387b 100644 --- a/tools/include/nolibc/sys.h +++ b/tools/include/nolibc/sys.h @@ -14,6 +14,7 @@ #include #include // for SIGCHLD #include +#include #include #include #include @@ -675,6 +676,67 @@ int mknod(const char *path, mode_t mode, dev_t dev) return ret; } =20 +#ifndef MAP_SHARED +#define MAP_SHARED 0x01 /* Share changes */ +#define MAP_PRIVATE 0x02 /* Changes are private */ +#define MAP_SHARED_VALIDATE 0x03 /* share + validate extension flags */ +#endif + +#ifndef MAP_FAILED +#define MAP_FAILED ((void *)-1) +#endif + +static __attribute__((unused)) +void *sys_mmap(void *addr, size_t length, int prot, int flags, int fd, + off_t offset) +{ +#ifndef my_syscall6 + /* Function not implemented. */ + return -ENOSYS; +#else + + int n; + +#if defined(__i386__) + n =3D __NR_mmap2; + offset >>=3D 12; +#else + n =3D __NR_mmap; +#endif + + return (void *)my_syscall6(n, addr, length, prot, flags, fd, offset); +#endif +} + +static __attribute__((unused)) +void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t o= ffset) +{ + void *ret =3D sys_mmap(addr, length, prot, flags, fd, offset); + + if ((unsigned long)ret >=3D -4095UL) { + SET_ERRNO(-(long)ret); + ret =3D MAP_FAILED; + } + return ret; +} + +static __attribute__((unused)) +int sys_munmap(void *addr, size_t length) +{ + return my_syscall2(__NR_munmap, addr, length); +} + +static __attribute__((unused)) +int munmap(void *addr, size_t length) +{ + int ret =3D sys_munmap(addr, length); + + if (ret < 0) { + SET_ERRNO(-ret); + ret =3D -1; + } + return ret; +} =20 /* * int mount(const char *source, const char *target, --=20 Ammar Faizi From nobody Mon Jun 22 14:24:02 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5B79DC433EF for ; Tue, 22 Mar 2022 10:22:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232974AbiCVKXe (ORCPT ); Tue, 22 Mar 2022 06:23:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43874 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233006AbiCVKXZ (ORCPT ); Tue, 22 Mar 2022 06:23:25 -0400 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4B85F80239 for ; Tue, 22 Mar 2022 03:21:54 -0700 (PDT) Received: from integral2.. (unknown [182.2.69.158]) by gnuweeb.org (Postfix) with ESMTPSA id 1429F7E337; Tue, 22 Mar 2022 10:21:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1647944513; bh=V1krABsPZ17e1z+Qe7nb1l0Lb2juQEQNEq83Qg0iKqs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LJuHOmOqi1WzLHHHhvu29FB/FfiPGXUMvTUoErRz0j0xu96Cvn5Yvo2jbaSRZNC2+ lfBlsRCX9XFZ5I2OKIOyLI4JQuA5npVpMDer1KMVSo0JEJbv+4/pnjLIjLO9FqjCKd Rn0SPSB1l74gWhNkklRbuWxPxhRutHwuqGSH9MhnoLiZTSsDy/FLQXkWA0Furalszx QcAi/ppH+J4uV5oM4L6wFfDA6BXJdv6omCDDP1IEMcp7EF9kmSi7LrOrtgQiyzs7cn VuHexLNTbrpXN8g6PdWVHq/mb9AXindGVAhfias0G8JxHHD0YdqQYHsv8ERcdCcR57 uAncV4TQvTN4w== From: Ammar Faizi To: Willy Tarreau Cc: "Paul E. McKenney" , Alviro Iskandar Setiawan , Nugraha , Linux Kernel Mailing List , GNU/Weeb Mailing List , Ammar Faizi Subject: [RFC PATCH v2 5/8] tools/nolibc/types: Implement `offsetof()` and `container_of()` macro Date: Tue, 22 Mar 2022 17:21:12 +0700 Message-Id: <20220322102115.186179-6-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220322102115.186179-1-ammarfaizi2@gnuweeb.org> References: <20220322102115.186179-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Implement `offsetof()` and `container_of()` macro. The first use case of these macros is for `malloc()`, `realloc()` and `free()`. Signed-off-by: Ammar Faizi --- tools/include/nolibc/types.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tools/include/nolibc/types.h b/tools/include/nolibc/types.h index 357e60ad38a8..959997034e55 100644 --- a/tools/include/nolibc/types.h +++ b/tools/include/nolibc/types.h @@ -191,4 +191,15 @@ struct stat { #define major(dev) ((unsigned int)(((dev) >> 8) & 0xfff)) #define minor(dev) ((unsigned int)(((dev) & 0xff)) =20 +#ifndef offsetof +#define offsetof(TYPE, FIELD) ((size_t) &((TYPE *)0)->FIELD) +#endif + +#ifndef container_of +#define container_of(PTR, TYPE, FIELD) ({ \ + __typeof__(((TYPE *)0)->FIELD) *__FIELD_PTR =3D (PTR); \ + (TYPE *)((char *) __FIELD_PTR - offsetof(TYPE, FIELD)); \ +}) +#endif + #endif /* _NOLIBC_TYPES_H */ --=20 Ammar Faizi From nobody Mon Jun 22 14:24:02 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CF69AC433EF for ; Tue, 22 Mar 2022 10:22:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232588AbiCVKXi (ORCPT ); Tue, 22 Mar 2022 06:23:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44120 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233058AbiCVKXb (ORCPT ); Tue, 22 Mar 2022 06:23:31 -0400 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 703C481198 for ; Tue, 22 Mar 2022 03:21:57 -0700 (PDT) Received: from integral2.. (unknown [182.2.69.158]) by gnuweeb.org (Postfix) with ESMTPSA id 09F127E33E; Tue, 22 Mar 2022 10:21:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1647944516; bh=x+mKN/1p95Zf0+fLiZBiEJVGOeVZbrDqmetqJdLgFD4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cT5xjjU4jkQdMIwuGOxBIramShd5dkEo+XyOG2iYIYWG9zFSZ60OS42FDLHCfdf4D oQeb5+PUAmNrCiFpeA6M/Unj/FM6NvgggmZO9FIKxgvlektJV6Ro7ZEQyGugl20YOi e/QO4nySl3GgRXzZ7cAphWPkHyS8MxgUZbKP/L7XrsYsgH6HJYOUc0yzFGBs2sBhpd YAy2K0L7ScVcuoDD6ISwWIoBLMh8VEiZdx5rncoCMjrNXbSSLTBni5Ib9tRBWmw2Yo wSCSPAG2MpnGgaEkwT/vAHSVhpvYugdNBPhxE/Mu0u9Cos4cEhN8HtnkOtVWVAPd48 GpX+C/AjNdH4g== From: Ammar Faizi To: Willy Tarreau Cc: "Paul E. McKenney" , Alviro Iskandar Setiawan , Nugraha , Linux Kernel Mailing List , GNU/Weeb Mailing List , Ammar Faizi Subject: [RFC PATCH v2 6/8] tools/nolibc/stdlib: Implement `malloc()`, `calloc()`, `realloc()` and `free()` Date: Tue, 22 Mar 2022 17:21:13 +0700 Message-Id: <20220322102115.186179-7-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220322102115.186179-1-ammarfaizi2@gnuweeb.org> References: <20220322102115.186179-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Implement basic dynamic allocator functions. These functions are currently only available on architectures that have nolibc mmap() syscall implemented. These are not a super-fast memory allocator, but at least they can satisfy basic needs for having heap without libc. Signed-off-by: Ammar Faizi --- @@ Changelog: Link: https://lore.kernel.org/lkml/20220320093750.159991-6-ammarfaizi2@g= nuweeb.org RFC v1 -> RFC v2: - Move container_of() and offsetof() macro to types.h with a separate patch (comment from Willy). --- tools/include/nolibc/stdlib.h | 68 +++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/tools/include/nolibc/stdlib.h b/tools/include/nolibc/stdlib.h index aca8616335e3..a0ed75431e0a 100644 --- a/tools/include/nolibc/stdlib.h +++ b/tools/include/nolibc/stdlib.h @@ -11,7 +11,12 @@ #include "arch.h" #include "types.h" #include "sys.h" +#include "string.h" =20 +struct nolibc_heap { + size_t len; + char user_p[] __attribute__((__aligned__)); +}; =20 /* Buffer used to store int-to-ASCII conversions. Will only be implemented= if * any of the related functions is implemented. The area is large enough to @@ -60,6 +65,18 @@ int atoi(const char *s) return atol(s); } =20 +static __attribute__((unused)) +void free(void *ptr) +{ + struct nolibc_heap *heap; + + if (!ptr) + return; + + heap =3D container_of(ptr, struct nolibc_heap, user_p); + munmap(heap, heap->len); +} + /* Tries to find the environment variable named in the environment = array * pointed to by global variable "environ" which must be declared as a cha= r **, * and must be terminated by a NULL (it is recommended to set this variabl= e to @@ -205,6 +222,57 @@ char *ltoa(long in) return itoa_buffer; } =20 +static __attribute__((unused)) +void *malloc(size_t len) +{ + struct nolibc_heap *heap; + + heap =3D mmap(NULL, sizeof(*heap) + len, PROT_READ|PROT_WRITE, + MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); + if (__builtin_expect(heap =3D=3D MAP_FAILED, 0)) + return NULL; + + heap->len =3D sizeof(*heap) + len; + return heap->user_p; +} + +static __attribute__((unused)) +void *calloc(size_t size, size_t nmemb) +{ + void *orig; + size_t res =3D 0; + + if (__builtin_expect(__builtin_mul_overflow(nmemb, size, &res), 0)) { + SET_ERRNO(ENOMEM); + return NULL; + } + + /* + * No need to zero the heap, the MAP_ANONYMOUS in malloc() + * already does it. + */ + return malloc(res); +} + +static __attribute__((unused)) +void *realloc(void *old_ptr, size_t new_size) +{ + struct nolibc_heap *heap; + void *ret; + + ret =3D malloc(new_size); + if (__builtin_expect(!ret, 0)) + return NULL; + + if (!old_ptr) + return ret; + + heap =3D container_of(old_ptr, struct nolibc_heap, user_p); + memcpy(ret, heap->user_p, heap->len); + munmap(heap, heap->len); + return ret; +} + /* converts unsigned long integer to a string using the static itoa_b= uffer * and returns the pointer to that string. */ --=20 Ammar Faizi From nobody Mon Jun 22 14:24:02 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 32407C433EF for ; Tue, 22 Mar 2022 10:22:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233032AbiCVKXr (ORCPT ); Tue, 22 Mar 2022 06:23:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43908 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233094AbiCVKXb (ORCPT ); Tue, 22 Mar 2022 06:23:31 -0400 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 04A70811A7 for ; Tue, 22 Mar 2022 03:22:00 -0700 (PDT) Received: from integral2.. (unknown [182.2.69.158]) by gnuweeb.org (Postfix) with ESMTPSA id 518A57E33D; Tue, 22 Mar 2022 10:21:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1647944520; bh=hiJ9XJQoRgMa4/fOsRSCSX7gvGvKUHkqA0mcpuI4lqQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EiId7k7nK05JcFz25EpP9QwMckMfGq8uRCIBPB502ocHCAJcJs/TMlGzyrNVjAP6X 9x5LBwkE6kgRu8yXphis65A8JsYykGjUw/VgNSCwmyPZKsQ5J1lJ7dtVuCvysG9wJ/ VYO4vNaNqa8L1cyohARBq/OmIpm40LniLsNk2wZDWlrBeshuNVZZ1Xd2xasKfg2tww nfQcZicD+NmVWJfHs5oM8Girks6EsAV/M0g7EGrJg93/mogpafTBU857OnB5fjls0j KO/BYpyhByauLwl6zYOAaVtxFPJ8sUQOmQQuzaqVStMrOc84H2oZ4dWhB2Yx47ALh1 d6O4rSwBoXTQQ== From: Ammar Faizi To: Willy Tarreau Cc: "Paul E. McKenney" , Alviro Iskandar Setiawan , Nugraha , Linux Kernel Mailing List , GNU/Weeb Mailing List , Ammar Faizi Subject: [RFC PATCH v2 7/8] tools/nolibc/string: Implement `strnlen()` Date: Tue, 22 Mar 2022 17:21:14 +0700 Message-Id: <20220322102115.186179-8-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220322102115.186179-1-ammarfaizi2@gnuweeb.org> References: <20220322102115.186179-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" size_t strnlen(const char *str, size_t maxlen); The strnlen() function returns the number of bytes in the string pointed to by sstr, excluding the terminating null byte ('\0'), but at most maxlen. In doing this, strnlen() looks only at the first maxlen characters in the string pointed to by str and never beyond str[maxlen-1]. The first use case of this function is for determining the memory allocation size in the strndup() function. Link: https://lore.kernel.org/lkml/CAOG64qMpEMh+EkOfjNdAoueC+uQyT2Uv3689_sO= r37-JxdJf4g@mail.gmail.com Suggested-by: Alviro Iskandar Setiawan Signed-off-by: Ammar Faizi --- tools/include/nolibc/string.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tools/include/nolibc/string.h b/tools/include/nolibc/string.h index 0d5e870c7c0b..1426eefc1ef2 100644 --- a/tools/include/nolibc/string.h +++ b/tools/include/nolibc/string.h @@ -138,6 +138,15 @@ size_t nolibc_strlen(const char *str) nolibc_strlen((str)); \ }) =20 +static __attribute__((unused)) +size_t strnlen(const char *str, size_t maxlen) +{ + size_t len; + + for (len =3D 0; (len < maxlen) && str[len]; len++); + return len; +} + static __attribute__((unused)) size_t strlcat(char *dst, const char *src, size_t size) { --=20 Ammar Faizi From nobody Mon Jun 22 14:24:02 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A1CDFC433F5 for ; Tue, 22 Mar 2022 10:22:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232972AbiCVKXm (ORCPT ); Tue, 22 Mar 2022 06:23:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43860 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233043AbiCVKXe (ORCPT ); Tue, 22 Mar 2022 06:23:34 -0400 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A82A4811B7 for ; Tue, 22 Mar 2022 03:22:04 -0700 (PDT) Received: from integral2.. (unknown [182.2.69.158]) by gnuweeb.org (Postfix) with ESMTPSA id EF33E7E343; Tue, 22 Mar 2022 10:22:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1647944523; bh=vWuQhpoAXiBJj3wfUX35L7A8raKBOQqVL60x1T1gmI8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cpwcf9F07dn3br1s8e47fpUodg+PtsS/O7sdpWg5xam/tebk5YqqseF4WV9nLvrLx R0f/cjcT6EqBf5GPINaxgDQ9GE+xhkinW9uEsebfZBKILuMtZB0nHnQrLrmm0l9GQS EZtTy9oxqFrpZrJT9NF9LEfShN1WJ8ljl8Iuwa62dQDP3TszQm5Q9DkcC4CiTXY2dN FjdvNr+FsnUlWPJDnOnxWUisClxdqu+PB89BpDiAO8UfcfN+ZzLUgC6eDr3W1mJfUV Xg+2LqxbmcNrxI1+Rk9bWUZk9om8jBNWbzTz6enf9EGZgaVIp89NxTMcV08rclNRoI TrWTRN2LMdHzA== From: Ammar Faizi To: Willy Tarreau Cc: "Paul E. McKenney" , Alviro Iskandar Setiawan , Nugraha , Linux Kernel Mailing List , GNU/Weeb Mailing List , Ammar Faizi Subject: [RFC PATCH v2 8/8] tools/include/string: Implement `strdup()` and `strndup()` Date: Tue, 22 Mar 2022 17:21:15 +0700 Message-Id: <20220322102115.186179-9-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220322102115.186179-1-ammarfaizi2@gnuweeb.org> References: <20220322102115.186179-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" These functions are currently only available on architectures that have my_syscall6() macro implemented. Since these functions use malloc(), malloc() uses mmap(), mmap() depends on my_syscall6() macro. On architectures that don't support my_syscall6(), these function will always return NULL with errno set to ENOSYS. Signed-off-by: Ammar Faizi --- @@ Changelog: Link RFC v1: https://lore.kernel.org/lkml/20220320093750.159991-7-ammarf= aizi2@gnuweeb.org/ RFC v1 -> RFC v2: - Update strdup and strndup implementation, use strlen and strnlen to g= et the string length first (comment from Willy and Alviro). - Fix the subject line prefix, it was "tools/include/string: ", it shou= ld be "tools/nolibc/string: ". - Update the commit message. --- tools/include/nolibc/string.h | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tools/include/nolibc/string.h b/tools/include/nolibc/string.h index 1426eefc1ef2..bcc76f89199e 100644 --- a/tools/include/nolibc/string.h +++ b/tools/include/nolibc/string.h @@ -9,6 +9,8 @@ =20 #include "std.h" =20 +static void *malloc(size_t len); + /* * As much as possible, please keep functions alphabetically sorted. */ @@ -147,6 +149,36 @@ size_t strnlen(const char *str, size_t maxlen) return len; } =20 +static __attribute__((unused)) +char *strdup(const char *str) +{ + size_t len; + char *ret; + + len =3D strlen(str); + ret =3D malloc(len + 1); + if (__builtin_expect(ret !=3D NULL, 1)) + memcpy(ret, str, len + 1); + + return ret; +} + +static __attribute__((unused)) +char *strndup(const char *str, size_t maxlen) +{ + size_t len; + char *ret; + + len =3D strnlen(str, maxlen); + ret =3D malloc(len + 1); + if (__builtin_expect(ret !=3D NULL, 1)) { + memcpy(ret, str, len); + ret[len] =3D '\0'; + } + + return ret; +} + static __attribute__((unused)) size_t strlcat(char *dst, const char *src, size_t size) { --=20 Ammar Faizi