From patchwork Sun Jun 4 05:34:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhangjin Wu X-Patchwork-Id: 13266474 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id F2D82C7EE2D for ; Sun, 4 Jun 2023 05:35:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=W/I78CdP/UKpr6XztgdZpaxh9LF5V180w4YSOd2e8ic=; b=RAY8cIBS8Gpaeg 1PeEPja5I29ay6C3se1Z8lA3kFr1Jbu09863IqjzfwRj8lQsy4OJdjMaruw0aIqhhfRTcA/K6hC0+ iA49Jt56pvPpqibfifmFnZg9cV6Ck3+29UqaMkGQPBe02S/Ffg7G281WaSpIGGnAEUU2/VP2BUbt4 kf3xUzV708TXI+TrF09hOm56WbG2MsqbqVV1ofjpBfCHKzWmdmi67wNq+qFcTTtIA80D4Q0+LQMpQ 5zYmthuyr6wNzp+nAGxKaVXeJIOqvBwiRApHoBGZtwN88gtjgESyFKjTWQTRk46xk9Rs2nqeGi1WX Id07Rjbb9tvWWXnXRsAg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1q5gOn-00BMwj-2i; Sun, 04 Jun 2023 05:35:29 +0000 Received: from bg4.exmail.qq.com ([43.154.221.58]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1q5gOk-00BMvd-2I for linux-riscv@lists.infradead.org; Sun, 04 Jun 2023 05:35:28 +0000 X-QQ-mid: bizesmtp83t1685856912tz2mpqb9 Received: from linux-lab-host.localdomain ( [61.141.77.49]) by bizesmtp.qq.com (ESMTP) with id ; Sun, 04 Jun 2023 13:35:11 +0800 (CST) X-QQ-SSF: 01200000000000D0V000000A0000000 X-QQ-FEAT: rZJGTgY0+YN6Hgm0UEoWeI0raRxwz+OClFuVPySOxGuRMnsmCqEXyxefSu3x0 2WU1GagREmkvIIMdLAcKZPPfzXOjtPC6QHE3ohpmFo0EwDu8CwIBqVcnio0loP9RgMSMEle vfF2rNJrqkaUHui8aQBWgK5qof0qpL61KoggBuPyvGXjWlRfC+JUz68VKJvriGEPu22kSbc RqLABcHytpqcY77XZL3DpY4doFeZYFXTqw9icVDCrprILlhn+oHUh/o89uCtTCHgHw1Z4/g qH5mWGlhL8RN+5kTi7TjkC8GBo1nfTwYXvNmegQ1933fDtiBVCTz03KNUfGCKHNsjcRg/aK RsT2h465n546UCWpcxgDIIFqF9rZUXUyBTxm19I3slRZRZGF2/hpk36ULoPt4VGRjQLLtme X-QQ-GoodBg: 0 X-BIZMAIL-ID: 350183426812210734 From: Zhangjin Wu To: w@1wt.eu Cc: falcon@tinylab.org, arnd@arndb.de, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-riscv@lists.infradead.org, thomas@t-8ch.de Subject: [PATCH 1/4] tools/nolibc: unistd.h: add __syscall() and __syscall_ret() helpers Date: Sun, 4 Jun 2023 13:34:29 +0800 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:tinylab.org:qybglogicsvrsz:qybglogicsvrsz3a-3 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230603_223527_052632_7D5D4647 X-CRM114-Status: GOOD ( 10.41 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org most of the library routines share the same code model, let's add some macros to simplify the coding and shrink the code lines too. One added for syscall return, one added for syscall call, both of them can get the typeof 'return value' automatically. To get the return type of syscalls, __auto_type is better than typeof(), but it is not supported by the old compilers (before 2013, see [1]), so, use typeof() here. [1]: https://gcc.gnu.org/legacy-ml/gcc-patches/2013-11/msg01378.html Signed-off-by: Zhangjin Wu --- tools/include/nolibc/sys.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h index 1d6f33f58629..937a8578e3d4 100644 --- a/tools/include/nolibc/sys.h +++ b/tools/include/nolibc/sys.h @@ -28,6 +28,21 @@ #include "errno.h" #include "types.h" +/* Syscall call and return helpers */ +#define __syscall_ret(ret) \ +({ \ + if (ret < 0) { \ + SET_ERRNO(-ret); \ + ret = (typeof(ret))-1; \ + } \ + ret; \ +}) + +#define __syscall(name, ...) \ +({ \ + typeof(sys_##name(__VA_ARGS__)) ret = sys_##name(__VA_ARGS__); \ + __syscall_ret(ret); \ +}) /* Functions in this file only describe syscalls. They're declared static so * that the compiler usually decides to inline them while still being allowed From patchwork Sun Jun 4 05:36:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhangjin Wu X-Patchwork-Id: 13266475 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 150D7C77B73 for ; Sun, 4 Jun 2023 05:38:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=KgMfECI4LExzfFO9G03CYZ12rcavMGm7tzfdsGj/nMg=; b=CvVUw2obV3XVqH qadgr4nMMdaaiRuvJwcaDlo0uvj5RNAB1n9MX7CAb3QUlqxRP/Nzi3m9NEgpk7az+XX/gj9T/qh4m uJOWMIGHyYXOfV7ASWaujiReYEAxRRf72OM+eXwpjVdiHroKk4LPyX2S5KP/0AvPdAO5VfQJGVwGP zM+MJsk4d3dckjhIdV7xhusK5Ul1p67tgkHV7C3u0tFxUHcTkfqhIHcXfPMIlAKtCR7SHXkA2bGxg Roaqys/cpZENe7dHaPM64Fdm4GddROyq9Lum115FK367GaorupG7EMqjSv6GJSANHANPc0zlIJw/b bzv3auj7qfvQ2IGQkObQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1q5gRQ-00BNM1-08; Sun, 04 Jun 2023 05:38:12 +0000 Received: from bg4.exmail.qq.com ([43.155.67.158]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1q5gRN-00BNKo-2D for linux-riscv@lists.infradead.org; Sun, 04 Jun 2023 05:38:11 +0000 X-QQ-mid: bizesmtp91t1685857075t2knx7k6 Received: from linux-lab-host.localdomain ( [61.141.77.49]) by bizesmtp.qq.com (ESMTP) with id ; Sun, 04 Jun 2023 13:37:54 +0800 (CST) X-QQ-SSF: 01200000000000D0V000000A0000000 X-QQ-FEAT: qTKqCPEPPMCMusprEIJpvMeXnrP/Pg47ltBveugHl56AMUwpiPQN+dX7Bx1al hyQMy5WOb2iwGqbVG2Vee4E1CJKGTYaPVCKPKX8kD8/yo3tZ2rIeymiY72vSvRoMb27pNEG jbtc7RzZOGJhVxHVAMve+71tINCsEp/ZTion6ifg/TNLEPpYBd9LH/w5ADuQNFBURZosfvJ q84P945SwQwPVyJeqTGthznnJkaFBxh1lvZinTuthAASHZzYB5dxSii1VjZ0QNeeP5aptOE l4sRjOXi7m0tDtf6US/kKjuJkH4SfpZllHn8WRggB3P2ElsmPmnvGNtklXZjXmiFa11fK2N 83pqciJQiKSs3As+YWzh+KbdwYUfVOkVV+ojkfRpGAYNrxHTeuIvSLLAg2zfw== X-QQ-GoodBg: 0 X-BIZMAIL-ID: 1136455146398936182 From: Zhangjin Wu To: w@1wt.eu Cc: falcon@tinylab.org, arnd@arndb.de, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-riscv@lists.infradead.org, thomas@t-8ch.de Subject: [PATCH 2/4] tools/nolibc: unistd.h: apply __syscall_ret() helper Date: Sun, 4 Jun 2023 13:36:20 +0800 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:tinylab.org:qybglogicsvrsz:qybglogicsvrsz3a-3 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230603_223810_027704_7232C4CF X-CRM114-Status: UNSURE ( 7.34 ) X-CRM114-Notice: Please train this message. X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org get the return type automatically and use __syscall_ret() to shrink 4 code lines. Signed-off-by: Zhangjin Wu --- tools/include/nolibc/unistd.h | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/tools/include/nolibc/unistd.h b/tools/include/nolibc/unistd.h index 6773e83c16a0..e4e2b4c09771 100644 --- a/tools/include/nolibc/unistd.h +++ b/tools/include/nolibc/unistd.h @@ -56,14 +56,10 @@ int tcsetpgrp(int fd, pid_t pid) return ioctl(fd, TIOCSPGRP, &pid); } -#define _syscall(N, ...) \ -({ \ - int _ret = my_syscall##N(__VA_ARGS__); \ - if (_ret < 0) { \ - SET_ERRNO(-_ret); \ - _ret = -1; \ - } \ - _ret; \ +#define _syscall(N, ...) \ +({ \ + typeof(my_syscall##N(__VA_ARGS__)) _ret = my_syscall##N(__VA_ARGS__); \ + __syscall_ret(_ret); \ }) #define _sycall_narg(...) __syscall_narg(__VA_ARGS__, 6, 5, 4, 3, 2, 1, 0) From patchwork Sun Jun 4 05:39:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhangjin Wu X-Patchwork-Id: 13266477 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 94C87C7EE23 for ; Sun, 4 Jun 2023 05:42:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=Zt+yuc//oxEEhXj2Y+rILhO1nDL9XIp5EwfbxDdqIVg=; b=NpuxhWHrqFWGxP hw4DaGexb7jZJJizrFToZbyGEs1T9xNfLClbEIqNdRqRhVdJtrtGOj+KJnoxgg50rCRPUNv1+pLvW BB+fmVHRIQ6o4Eh6ZxSwVenNGGJoEHa62ujU3yoU1+exhJz1wZvj6yMFOV1XKOTxm+VdzHhIjdKo8 +06+D+VY2cv+7geWtKljnQrz1688TzzfYk3EBFoQzVovD04kxlQtjLmvKYzkrUEMVKnvaD32B0Xga mmSF+A/LmAqC8CuWeSyN0ptDmi3w/K8BRBDr/itv+yDHXzzXkvRm85+1SAfF+ZVU9uRh5ogswNfbg VMdR0u2w2/o+aWJlNMtA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1q5gVo-00BNwf-0F; Sun, 04 Jun 2023 05:42:44 +0000 Received: from bg4.exmail.qq.com ([43.154.221.58]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1q5gVk-00BNv5-19 for linux-riscv@lists.infradead.org; Sun, 04 Jun 2023 05:42:41 +0000 X-QQ-mid: bizesmtp72t1685857346t5xr8sni Received: from linux-lab-host.localdomain ( [61.141.77.49]) by bizesmtp.qq.com (ESMTP) with id ; Sun, 04 Jun 2023 13:42:25 +0800 (CST) X-QQ-SSF: 01200000000000D0V000000A0000000 X-QQ-FEAT: Z2qCHCq1/922HOu2rI3BNb7YZnwq6I3XqG+zETTdRX5VsVqTCcaEqWdJ/xe8Z nyxTVuLQd3K6s8lYS4Qgz4f0pv/WAO12sNEiy+9qWHlqo5jTMfgKcP3i63dAjba4Dysgloc Hm16xIq/C730s527UMkx8tR+oOeoz8YIinChVkV78frR33mszF+YhAzfvGGCSRQCtbSb+oX nTj+SzN5TCXcn7OkkJ7nFrXv8+dJu0PtIN+2aAgwVhP6Xbn9tAX2inmqu5dwvjx8K3jpeu7 0p4Ag9EspgteAHAvgrqlXocbra5y+qJmTRonXAqsZ9yN9VUhbtHOxVgUYE+6tbxM7kaeCse 8i67iyDZsITzvq1dype13lr1sYOAUfVG0VWOX3FbPOzygs8+PuFSEA//QvLKw== X-QQ-GoodBg: 0 X-BIZMAIL-ID: 5104637929055565147 From: Zhangjin Wu To: w@1wt.eu Cc: falcon@tinylab.org, arnd@arndb.de, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-riscv@lists.infradead.org, thomas@t-8ch.de Subject: [PATCH 3/4] tools/nolibc: sys.h: apply __syscall_ret() helper Date: Sun, 4 Jun 2023 13:39:03 +0800 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:tinylab.org:qybglogicsvrsz:qybglogicsvrsz3a-3 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230603_224240_694374_08A1E4AE X-CRM114-Status: UNSURE ( 7.76 ) X-CRM114-Notice: Please train this message. X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Use __syscall_ret() helper to shrink the code lines of brk() and getpagesize(), 10 lines removed. Signed-off-by: Zhangjin Wu --- tools/include/nolibc/sys.h | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h index 937a8578e3d4..976f23d1fdad 100644 --- a/tools/include/nolibc/sys.h +++ b/tools/include/nolibc/sys.h @@ -81,13 +81,9 @@ void *sys_brk(void *addr) static __attribute__((unused)) int brk(void *addr) { - void *ret = sys_brk(addr); + int ret = sys_brk(addr) ? 0 : -ENOMEM; - if (!ret) { - SET_ERRNO(ENOMEM); - return -1; - } - return 0; + return __syscall_ret(ret); } static __attribute__((unused)) @@ -550,15 +546,9 @@ static unsigned long getauxval(unsigned long key); static __attribute__((unused)) long getpagesize(void) { - long ret; + long ret = getauxval(AT_PAGESZ) ?: -ENOENT; - ret = getauxval(AT_PAGESZ); - if (!ret) { - SET_ERRNO(ENOENT); - return -1; - } - - return ret; + return __syscall_ret(ret); } From patchwork Sun Jun 4 05:43:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhangjin Wu X-Patchwork-Id: 13266479 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 480BAC7EE23 for ; Sun, 4 Jun 2023 05:46:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=fgKal/KijXzlplf6CUjazwz1DbqTGWdWZSclcMCGt/c=; b=qwmPp72nx34/gL vX6951J629oLX5x5X55gHvY/F59eyNzCQ9OZwdVOD6oxWUmepmibK+FdhxtbeXodTEq0yirjEJk+F kXOwTFMx+zIUEJurI/ARrWfWAeqcfUEOITcLaOkkoMwQYC9+eqKsK+JRyAMXzLNYipTeMHkocwGIs bHGn8eb+yjLkUPtbimMkiK2q9VJQexjYbc6YTW8iHNd4VnnYlf6+I7CzWswbiTfdCRK4xSclt73o2 cDPtJnE3XFMtW/Qd87x+rVhI3zbbgIgE90MHPQOZw5dPH/2kU8FPDTU+cnhEcTOruLo9RwkdXI62p rDzsoAvECxBLe1diNk5w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1q5gYv-00BOIz-2G; Sun, 04 Jun 2023 05:45:57 +0000 Received: from bg4.exmail.qq.com ([43.155.67.158]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1q5gYp-00BOI2-2m for linux-riscv@lists.infradead.org; Sun, 04 Jun 2023 05:45:55 +0000 X-QQ-mid: bizesmtp74t1685857519t0x8zryb Received: from linux-lab-host.localdomain ( [61.141.77.49]) by bizesmtp.qq.com (ESMTP) with id ; Sun, 04 Jun 2023 13:45:18 +0800 (CST) X-QQ-SSF: 01200000000000D0V000000A0000000 X-QQ-FEAT: k0mQ4ihyJQNhZgjLnbfL7HCaFDgVKYuM1kJqwk8p56+jzW2yx/W+okDk8osaV Lg8uL+Xt+N5tzChN4aOjAi7U0xipva9vNgmF05ZRtV6ZFu7JgsMRO1oQ2B0KNgQAaiqlxGW 6kEicqhzo2KaeYMm+7AvGGkM6xlPhNguVPJc0zj6Nhj1AKiNazsAN0RMnayd3av0lnQZNeI 63NG1LkOkrGtz1uU9Oor9J4lKW/ToBiKJnlc4xpmSg+7m55yXvlNc3zGX1Q6j4/kaMt3JEA 9nt/UfODaLrX4DmRtWieMyIzbSqoBlxG0+0XNWNY6b2jHFpPnY0yANZcp2YH5/TMOd3tgDN YDVtbWVQtXTbGKO06+Y2iimGeIWUy8NqCoL77vFmKucyN9a9gA= X-QQ-GoodBg: 0 X-BIZMAIL-ID: 3837813299015765052 From: Zhangjin Wu To: w@1wt.eu Cc: falcon@tinylab.org, arnd@arndb.de, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-riscv@lists.infradead.org, thomas@t-8ch.de Subject: [PATCH 4/4] tools/nolibc: sys.h: apply __syscall() helper Date: Sun, 4 Jun 2023 13:43:34 +0800 Message-Id: <472e4986e3b217ba42b4ca798dcb32b11ea89a72.1685856497.git.falcon@tinylab.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:tinylab.org:qybglogicsvrsz:qybglogicsvrsz3a-3 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230603_224552_230018_FEEBD319 X-CRM114-Status: UNSURE ( 8.92 ) X-CRM114-Notice: Please train this message. X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Use __syscall() helper to shrink 252 lines of code. $ git show HEAD^:tools/include/nolibc/sys.h | wc -l 1432 $ git show HEAD:tools/include/nolibc/sys.h | wc -l 1180 $ echo "1432-1180" | bc -l 252 Signed-off-by: Zhangjin Wu --- tools/include/nolibc/sys.h | 336 +++++-------------------------------- 1 file changed, 42 insertions(+), 294 deletions(-) diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h index 976f23d1fdad..1f64f760d7fd 100644 --- a/tools/include/nolibc/sys.h +++ b/tools/include/nolibc/sys.h @@ -113,13 +113,7 @@ int sys_chdir(const char *path) static __attribute__((unused)) int chdir(const char *path) { - int ret = sys_chdir(path); - - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; - } - return ret; + return __syscall(chdir, path); } @@ -142,13 +136,7 @@ int sys_chmod(const char *path, mode_t mode) static __attribute__((unused)) int chmod(const char *path, mode_t mode) { - int ret = sys_chmod(path, mode); - - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; - } - return ret; + return __syscall(chmod, path, mode); } @@ -171,13 +159,7 @@ int sys_chown(const char *path, uid_t owner, gid_t group) static __attribute__((unused)) int chown(const char *path, uid_t owner, gid_t group) { - int ret = sys_chown(path, owner, group); - - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; - } - return ret; + return __syscall(chown, path, owner, group); } @@ -194,13 +176,7 @@ int sys_chroot(const char *path) static __attribute__((unused)) int chroot(const char *path) { - int ret = sys_chroot(path); - - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; - } - return ret; + return __syscall(chroot, path); } @@ -217,13 +193,7 @@ int sys_close(int fd) static __attribute__((unused)) int close(int fd) { - int ret = sys_close(fd); - - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; - } - return ret; + return __syscall(close, fd); } @@ -240,13 +210,7 @@ int sys_dup(int fd) static __attribute__((unused)) int dup(int fd) { - int ret = sys_dup(fd); - - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; - } - return ret; + return __syscall(dup, fd); } @@ -269,13 +233,7 @@ int sys_dup2(int old, int new) static __attribute__((unused)) int dup2(int old, int new) { - int ret = sys_dup2(old, new); - - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; - } - return ret; + return __syscall(dup2, old, new); } @@ -293,13 +251,7 @@ int sys_dup3(int old, int new, int flags) static __attribute__((unused)) int dup3(int old, int new, int flags) { - int ret = sys_dup3(old, new, flags); - - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; - } - return ret; + return __syscall(dup3, old, new, flags); } #endif @@ -317,13 +269,7 @@ int sys_execve(const char *filename, char *const argv[], char *const envp[]) static __attribute__((unused)) int execve(const char *filename, char *const argv[], char *const envp[]) { - int ret = sys_execve(filename, argv, envp); - - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; - } - return ret; + return __syscall(execve, filename, argv, envp); } @@ -370,13 +316,7 @@ pid_t sys_fork(void) static __attribute__((unused)) pid_t fork(void) { - pid_t ret = sys_fork(); - - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; - } - return ret; + return __syscall(fork); } @@ -393,13 +333,7 @@ int sys_fsync(int fd) static __attribute__((unused)) int fsync(int fd) { - int ret = sys_fsync(fd); - - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; - } - return ret; + return __syscall(fsync, fd); } @@ -416,13 +350,7 @@ int sys_getdents64(int fd, struct linux_dirent64 *dirp, int count) static __attribute__((unused)) int getdents64(int fd, struct linux_dirent64 *dirp, int count) { - int ret = sys_getdents64(fd, dirp, count); - - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; - } - return ret; + return __syscall(getdents64, fd, dirp, count); } @@ -460,13 +388,7 @@ pid_t sys_getpgid(pid_t pid) static __attribute__((unused)) pid_t getpgid(pid_t pid) { - pid_t ret = sys_getpgid(pid); - - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; - } - return ret; + return __syscall(getpgid, pid); } @@ -565,13 +487,7 @@ int sys_gettimeofday(struct timeval *tv, struct timezone *tz) static __attribute__((unused)) int gettimeofday(struct timeval *tv, struct timezone *tz) { - int ret = sys_gettimeofday(tv, tz); - - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; - } - return ret; + return __syscall(gettimeofday, tv, tz); } @@ -609,13 +525,7 @@ int sys_ioctl(int fd, unsigned long req, void *value) static __attribute__((unused)) int ioctl(int fd, unsigned long req, void *value) { - int ret = sys_ioctl(fd, req, value); - - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; - } - return ret; + return __syscall(ioctl, fd, req, value); } /* @@ -631,13 +541,7 @@ int sys_kill(pid_t pid, int signal) static __attribute__((unused)) int kill(pid_t pid, int signal) { - int ret = sys_kill(pid, signal); - - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; - } - return ret; + return __syscall(kill, pid, signal); } @@ -660,13 +564,7 @@ int sys_link(const char *old, const char *new) static __attribute__((unused)) int link(const char *old, const char *new) { - int ret = sys_link(old, new); - - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; - } - return ret; + return __syscall(link, old, new); } @@ -683,13 +581,7 @@ off_t sys_lseek(int fd, off_t offset, int whence) static __attribute__((unused)) off_t lseek(int fd, off_t offset, int whence) { - off_t ret = sys_lseek(fd, offset, whence); - - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; - } - return ret; + return __syscall(lseek, fd, offset, whence); } @@ -712,13 +604,7 @@ int sys_mkdir(const char *path, mode_t mode) static __attribute__((unused)) int mkdir(const char *path, mode_t mode) { - int ret = sys_mkdir(path, mode); - - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; - } - return ret; + return __syscall(mkdir, path, mode); } @@ -741,13 +627,7 @@ long sys_mknod(const char *path, mode_t mode, dev_t dev) static __attribute__((unused)) int mknod(const char *path, mode_t mode, dev_t dev) { - int ret = sys_mknod(path, mode, dev); - - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; - } - return ret; + return __syscall(mknod, path, mode, dev); } #ifndef MAP_SHARED @@ -805,13 +685,7 @@ int sys_munmap(void *addr, size_t length) static __attribute__((unused)) int munmap(void *addr, size_t length) { - int ret = sys_munmap(addr, length); - - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; - } - return ret; + return __syscall(munmap, addr, length); } /* @@ -831,13 +705,7 @@ int mount(const char *src, const char *tgt, const char *fst, unsigned long flags, const void *data) { - int ret = sys_mount(src, tgt, fst, flags, data); - - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; - } - return ret; + return __syscall(mount, src, tgt, fst, flags, data); } @@ -871,13 +739,7 @@ int open(const char *path, int flags, ...) va_end(args); } - ret = sys_open(path, flags, mode); - - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; - } - return ret; + return __syscall(open, path, flags, mode); } @@ -897,13 +759,7 @@ static __attribute__((unused)) int prctl(int option, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5) { - int ret = sys_prctl(option, arg2, arg3, arg4, arg5); - - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; - } - return ret; + return __syscall(prctl, option, arg2, arg3, arg4, arg5); } @@ -920,13 +776,7 @@ int sys_pivot_root(const char *new, const char *old) static __attribute__((unused)) int pivot_root(const char *new, const char *old) { - int ret = sys_pivot_root(new, old); - - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; - } - return ret; + return __syscall(pivot_root, new, old); } @@ -955,13 +805,7 @@ int sys_poll(struct pollfd *fds, int nfds, int timeout) static __attribute__((unused)) int poll(struct pollfd *fds, int nfds, int timeout) { - int ret = sys_poll(fds, nfds, timeout); - - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; - } - return ret; + return __syscall(poll, fds, nfds, timeout); } @@ -978,13 +822,7 @@ ssize_t sys_read(int fd, void *buf, size_t count) static __attribute__((unused)) ssize_t read(int fd, void *buf, size_t count) { - ssize_t ret = sys_read(fd, buf, count); - - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; - } - return ret; + return __syscall(read, fd, buf, count); } @@ -1002,13 +840,7 @@ ssize_t sys_reboot(int magic1, int magic2, int cmd, void *arg) static __attribute__((unused)) int reboot(int cmd) { - int ret = sys_reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, 0); - - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; - } - return ret; + return __syscall(reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, 0); } @@ -1025,13 +857,7 @@ int sys_sched_yield(void) static __attribute__((unused)) int sched_yield(void) { - int ret = sys_sched_yield(); - - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; - } - return ret; + return __syscall(sched_yield); } @@ -1071,13 +897,7 @@ int sys_select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeva static __attribute__((unused)) int select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *timeout) { - int ret = sys_select(nfds, rfds, wfds, efds, timeout); - - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; - } - return ret; + return __syscall(select, nfds, rfds, wfds, efds, timeout); } @@ -1094,13 +914,7 @@ int sys_setpgid(pid_t pid, pid_t pgid) static __attribute__((unused)) int setpgid(pid_t pid, pid_t pgid) { - int ret = sys_setpgid(pid, pgid); - - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; - } - return ret; + return __syscall(setpgid, pid, pgid); } @@ -1117,13 +931,7 @@ pid_t sys_setsid(void) static __attribute__((unused)) pid_t setsid(void) { - pid_t ret = sys_setsid(); - - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; - } - return ret; + return __syscall(setsid); } #if defined(__NR_statx) @@ -1140,13 +948,7 @@ int sys_statx(int fd, const char *path, int flags, unsigned int mask, struct sta static __attribute__((unused)) int statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf) { - int ret = sys_statx(fd, path, flags, mask, buf); - - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; - } - return ret; + return __syscall(statx, fd, path, flags, mask, buf); } #endif @@ -1223,13 +1025,7 @@ int sys_stat(const char *path, struct stat *buf) static __attribute__((unused)) int stat(const char *path, struct stat *buf) { - int ret = sys_stat(path, buf); - - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; - } - return ret; + return __syscall(stat, path, buf); } @@ -1252,13 +1048,7 @@ int sys_symlink(const char *old, const char *new) static __attribute__((unused)) int symlink(const char *old, const char *new) { - int ret = sys_symlink(old, new); - - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; - } - return ret; + return __syscall(symlink, old, new); } @@ -1292,13 +1082,7 @@ int sys_umount2(const char *path, int flags) static __attribute__((unused)) int umount2(const char *path, int flags) { - int ret = sys_umount2(path, flags); - - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; - } - return ret; + return __syscall(umount2, path, flags); } @@ -1321,13 +1105,7 @@ int sys_unlink(const char *path) static __attribute__((unused)) int unlink(const char *path) { - int ret = sys_unlink(path); - - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; - } - return ret; + return __syscall(unlink, path); } @@ -1346,38 +1124,20 @@ pid_t sys_wait4(pid_t pid, int *status, int options, struct rusage *rusage) static __attribute__((unused)) pid_t wait(int *status) { - pid_t ret = sys_wait4(-1, status, 0, NULL); - - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; - } - return ret; + return __syscall(wait4, -1, status, 0, NULL); } static __attribute__((unused)) pid_t wait4(pid_t pid, int *status, int options, struct rusage *rusage) { - pid_t ret = sys_wait4(pid, status, options, rusage); - - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; - } - return ret; + return __syscall(wait4, pid, status, options, rusage); } static __attribute__((unused)) pid_t waitpid(pid_t pid, int *status, int options) { - pid_t ret = sys_wait4(pid, status, options, NULL); - - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; - } - return ret; + return __syscall(wait4, pid, status, options, NULL); } @@ -1394,13 +1154,7 @@ ssize_t sys_write(int fd, const void *buf, size_t count) static __attribute__((unused)) ssize_t write(int fd, const void *buf, size_t count) { - ssize_t ret = sys_write(fd, buf, count); - - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; - } - return ret; + return __syscall(write, fd, buf, count); } @@ -1417,13 +1171,7 @@ int sys_memfd_create(const char *name, unsigned int flags) static __attribute__((unused)) int memfd_create(const char *name, unsigned int flags) { - ssize_t ret = sys_memfd_create(name, flags); - - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; - } - return ret; + return __syscall(memfd_create, name, flags); } /* make sure to include all global symbols */