From patchwork Tue Jun 6 08:09:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Zhangjin Wu X-Patchwork-Id: 13268689 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 E9BD1C7EE24 for ; Tue, 6 Jun 2023 08:10:15 +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=3yAyg0jymbNLy168GEr5OudaTaSpfTKH9EJEzjS05ik=; b=4zsVArB6u6dUrk 5TIZwOq6iWI08rZrnoku/STXrplUIruc6SqZPx6gGgwyR8Twa0Ru2ZwBa9eRxckWf8teONO33ZLQb xx2ZbrQvLZJ/+SrwyfID48JjOB8Xj1D4u69Al3ctQjL5Sln79qkcA7hsGjl41Vx5DBHkRMDRlLLlM Qr+hEdU3qmMeq3a8QR7O3ZeCKUdUVurx4w91qdSklEvmNYaOzb6C7Cv4dY1lCqtmlV62bf5To1gVH LfeZPjJUX7pp1IYfstje9ujTCMpqFHGC3wEgk/i+snGMH3EQ2cm0qEu+BNz+ZfiNYEG29v9vSaqDF LjtrelsNRynmUr2OtAGg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1q6Rlc-000k1z-0l; Tue, 06 Jun 2023 08:10:12 +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 1q6RlY-000k0n-2k for linux-riscv@lists.infradead.org; Tue, 06 Jun 2023 08:10:10 +0000 X-QQ-mid: bizesmtp74t1686038995tv89ingj Received: from linux-lab-host.localdomain ( [61.141.77.49]) by bizesmtp.qq.com (ESMTP) with id ; Tue, 06 Jun 2023 16:09:54 +0800 (CST) X-QQ-SSF: 01200000000000D0V000000A0000000 X-QQ-FEAT: HH6/KuQOBEZ8A8pYx8XsBdrlyHL8GtyVq+ZUYV+ZZkz5xn0a8lAF48lB5ZtBN SEWV6uZWjpbmERUWWwjt4KHGN6OUhX3XImQ0L7p7kXrbRfdWAKcpM/mpRkjsNRth7sRK30F oLwh+CsaqTOX+bNQGyM0K20+LqxfeXTvq1RHm5O/G6QDI3z42kjmXotyPTq/8v9Ii9CqVa5 WoUNXfWEfyW1TrJwG4a8+z0fI2ABZfq1QiLkCmY+J3QAlzybxry9PnCd7+qW5+2UC7Sp4ma FIf/x3mgWeYBACj50Qgz2pRM3ScdIBRZDKtwL910cw7SxyhfKwdvvN+K/3izjyLKz2AkkJM REp0CjT5nX4zPp5U+vBZkFTEMNAyE9w7qEI508/U7kiFmnEDTMTHEmP64/qJ1FRhs/y9Mus X-QQ-GoodBg: 0 X-BIZMAIL-ID: 11179949754190029416 From: Zhangjin Wu To: thomas@t-8ch.de, 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, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Subject: [PATCH v2 1/4] tools/nolibc: sys.h: add __syscall() and __sysret() helpers Date: Tue, 6 Jun 2023 16:09:51 +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-20230606_011009_215483_2F9A0DB5 X-CRM114-Status: GOOD ( 11.19 ) 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 two helpers to simplify the coding and shrink the code lines too. One added for syscall return, one added for syscall call. Thomas suggested to use inline function instead of macro for __sysret(), and he also helped to simplify the __syscall() a lot. Willy suggested to make __sysret() be always inline. Suggested-by: Willy Tarreau Link: https://lore.kernel.org/linux-riscv/ZH1+hkhiA2+ItSvX@1wt.eu/ Suggested-by: Thomas Weißschuh Link: https://lore.kernel.org/linux-riscv/ea4e7442-7223-4211-ba29-70821e907888@t-8ch.de/ Signed-off-by: Zhangjin Wu --- tools/include/nolibc/sys.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h index 5464f93e863e..c12c14db056e 100644 --- a/tools/include/nolibc/sys.h +++ b/tools/include/nolibc/sys.h @@ -28,6 +28,18 @@ #include "errno.h" #include "types.h" +/* Syscall return helper, set errno as -ret when ret < 0 */ +static inline __attribute__((always_inline)) long __sysret(long ret) +{ + if (ret < 0) { + SET_ERRNO(-ret); + ret = -1; + } + return ret; +} + +/* Syscall call helper, use syscall name instead of syscall number */ +#define __syscall(name, ...) __sysret(sys_##name(__VA_ARGS__)) /* 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 Tue Jun 6 08:11: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: 13268691 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 B900AC77B73 for ; Tue, 6 Jun 2023 08:15:28 +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=hDEB7ZxfNkENoO9QeoIIIXbmeslfzU4I8Bj5ekMLs78=; b=rIz296t2LFaPD3 HrnBjRltjyY/LBni/FtQP1Knu3qYh8bwABZl2oGlSOSNycWqLluy0we2gG2UYMcQibZeh4Thp44S5 QLLR6AaYXQASvoiVnwppBBRZRUl9NvqdCcahGqC1QyEfi14ATphEI/a4uWl+fN4UPDfcuIZ9HaOpl G6wosh7ZLOU/8qRznlQOLbC3/oKQX1NvVjH5cmeNdEcoUf9hLTN1/784BJ5QuHUg8dcEDNv+KevQZ 1tsbRCYuLmjn6Tz9wulQ2fQn5+TIFUt/FWoyrrEbCaFXTuOSGMaV9OO0Uj02r7LsrgYl+GaGb4uQS jWqgut3K9uiMd61tUolQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1q6Rqe-000koy-2a; Tue, 06 Jun 2023 08:15:24 +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 1q6Rqb-000knE-2h for linux-riscv@lists.infradead.org; Tue, 06 Jun 2023 08:15:23 +0000 X-QQ-mid: bizesmtp90t1686039304trwmue6s Received: from linux-lab-host.localdomain ( [61.141.77.49]) by bizesmtp.qq.com (ESMTP) with id ; Tue, 06 Jun 2023 16:15:03 +0800 (CST) X-QQ-SSF: 01200000000000D0V000000A0000000 X-QQ-FEAT: Sx1DrRIRqMdZ/MlCKruD26v8RqVr1CqYgFN5TjLwES8X+31r0YtgE6IBd5cmf DKk1dhvd4kdbyYCcvDHYlgPvIIZUN8eIxVkKKiLqrQ4UUMsnftqUrk03X/oF/6ZFzFAwjhj 77U0T4jL4pFsMjQBOVLSF3utZBeIScSOMCTU82gLNitvMSA7M6+yqVKIrOMBZcyYpTEkNUb VI5+pKYaFWHr2fNaC+pzOW98RgrorJ472+Q5MGW5Eor2qTEcQ3Y1Ib5/9I5EvC1EB41M1l1 Norl38y/vfKux6oTJBLP1e3GtPQ194vOUr95Or/8HE/F4qLGhBmgAUrUYtf/7JA/nDZfWgs CLnnL18aYxDAf+ZnzXWb6h5xBNkYESID5pJspa3wChK48GopzMjXEGvre/m3A== X-QQ-GoodBg: 0 X-BIZMAIL-ID: 13882787233173972735 From: Zhangjin Wu To: thomas@t-8ch.de, 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 Subject: [PATCH v2 2/4] tools/nolibc: unistd.h: apply __sysret() helper Date: Tue, 6 Jun 2023 16:11:03 +0800 Message-Id: <52084c2083472421b13d092357819b14f405e0ea.1686036862.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-20230606_011522_190453_D8249626 X-CRM114-Status: UNSURE ( 6.87 ) 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 __sysret() to shrink the whole _syscall() to oneline code. Signed-off-by: Zhangjin Wu --- tools/include/nolibc/unistd.h | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/tools/include/nolibc/unistd.h b/tools/include/nolibc/unistd.h index c20b2fbf065e..7e3c005d28ba 100644 --- a/tools/include/nolibc/unistd.h +++ b/tools/include/nolibc/unistd.h @@ -56,16 +56,7 @@ int tcsetpgrp(int fd, pid_t pid) return ioctl(fd, TIOCSPGRP, &pid); } -#define _syscall(N, ...) \ -({ \ - long _ret = my_syscall##N(__VA_ARGS__); \ - if (_ret < 0) { \ - SET_ERRNO(-_ret); \ - _ret = -1; \ - } \ - _ret; \ -}) - +#define _syscall(N, ...) __sysret(my_syscall##N(__VA_ARGS__)) #define _sycall_narg(...) __syscall_narg(__VA_ARGS__, 6, 5, 4, 3, 2, 1, 0) #define __syscall_narg(_0, _1, _2, _3, _4, _5, _6, N, ...) N #define _syscall_n(N, ...) _syscall(N, __VA_ARGS__) From patchwork Tue Jun 6 08:16:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhangjin Wu X-Patchwork-Id: 13268696 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 5D009C7EE24 for ; Tue, 6 Jun 2023 08:16:50 +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=YdjchpEnkcUNawnOygspLd2aUBYvboaJGIT5fviY/tM=; b=AEbeKt2cjJ69bF YghRmLJdybHet735CK5nASNzSIMlCT7CPEA0bimAwASm/0Rj111E/TjVggwBPoyAlG/eiY7sYBAvc VlT+SZee3yZWsgKSnVsjxHTI1000jb3thxMp0oRgLlMACQrPIc4cFB877QCDJe5KYbuq5n+FNvMRE gNptz2V6KUPa0PxyqEfvSIQ7ql3OJgVJZezHTOJOyKCbglBOLtHXonS1RC3hWt/Dc/mq+nMXrQyoe g63hGgTSpJMOZ8QTDzYJOJ0g+3jbosyWPiWpZDRPeIEYcp74Y1SfRGYsy6XXCeTvi0sM2ipy7P6Z5 D0K+4XsSoJ+SduLEiAWg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1q6Rry-000l26-1i; Tue, 06 Jun 2023 08:16:46 +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 1q6Rrv-000l19-23 for linux-riscv@lists.infradead.org; Tue, 06 Jun 2023 08:16:45 +0000 X-QQ-mid: bizesmtp79t1686039390t3zfjj1g Received: from linux-lab-host.localdomain ( [61.141.77.49]) by bizesmtp.qq.com (ESMTP) with id ; Tue, 06 Jun 2023 16:16:29 +0800 (CST) X-QQ-SSF: 01200000000000D0V000000A0000000 X-QQ-FEAT: fs34Pe/+C2RtSKmKHJC+wIdHChvPdDuUHsW0Y/Oc0Nqkrgol9/YGblT2gCBGq cvnFZ34Cj87k/IclwMobyHylf5h+9fQkW9ILGGrAdoTKg0xnOYEGdZTtIiQcKOkVrx0jmAW JY4DnauTC17PygPFGcUxAME9czb9I8yE/6Wi/RpF+fIioznqAMNklXe4n/XNMPvwtBZKLsK HzdqGH1uJ98z7dPX4MSq8KVirLCEXk/O6EymZycq6APbFhLTe8UpChE8PlruhQIr6z4joi7 6L5DhdOHvTng5Ie0eOs6uxPs3durFfFDDHs9nZ7BbNDrXrjSylG6VdHuvnnaAv8JLU6EW8j Jdv+XKibESyIOVUM2iaxHOmMGHW7CDYGQu68zuWQxmkQJlFiWVlD0YaJJQT2w== X-QQ-GoodBg: 0 X-BIZMAIL-ID: 14987419174521382926 From: Zhangjin Wu To: thomas@t-8ch.de, 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 Subject: [PATCH v2 3/4] tools/nolibc: sys.h: apply __sysret() helper Date: Tue, 6 Jun 2023 16:16:12 +0800 Message-Id: <20039f63dbbb54889c310196e909469ace5e503e.1686036862.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-20230606_011643_990520_30397F72 X-CRM114-Status: UNSURE ( 7.67 ) 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 __sysret() to shrink both brk() and getpagesize() to oneline code. Signed-off-by: Zhangjin Wu --- tools/include/nolibc/sys.h | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h index c12c14db056e..f6e3168b3e50 100644 --- a/tools/include/nolibc/sys.h +++ b/tools/include/nolibc/sys.h @@ -78,13 +78,7 @@ void *sys_brk(void *addr) static __attribute__((unused)) int brk(void *addr) { - void *ret = sys_brk(addr); - - if (!ret) { - SET_ERRNO(ENOMEM); - return -1; - } - return 0; + return __sysret(sys_brk(addr) ? 0 : -ENOMEM); } static __attribute__((unused)) @@ -547,15 +541,7 @@ static unsigned long getauxval(unsigned long key); static __attribute__((unused)) long getpagesize(void) { - long ret; - - ret = getauxval(AT_PAGESZ); - if (!ret) { - SET_ERRNO(ENOENT); - return -1; - } - - return ret; + return __sysret(getauxval(AT_PAGESZ) ?: -ENOENT); } From patchwork Tue Jun 6 08:17:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhangjin Wu X-Patchwork-Id: 13268697 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 233DBC77B73 for ; Tue, 6 Jun 2023 08:18:07 +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=6VtqRNGkYBFZTdPD4XQo8vy8HWzDSulfi7yB9RqIRgs=; b=MOP8Iy9J9xn5GA aFTDCS6UiVVvS6Zv3xA18bBEQNd0yd0D3sX3ERoiaww8hrDlptt6hQbev3lyFW9e1TZZAmvAgkTTy 1gme2A9khCXd7wxZCGPJoTHtQxvGrVrAuLDZvh3f1yFgvO8vYz137dTbqQ9EAjR0jPcXN8bwVV+BP CUegmAXNwQS53hR1qeYSCt9pwYuhnU1CXMnONpxLyLMijudGi5tAQmTYsBSMH5KaNFTBEzjCYF/zF G7MypJren3TVnVcRuHbaKsqpgZ1crjTK2bRB4LDjArrIs2SMRsBpZLkTmtApe6McmiEc2bgycIcD3 HGrRo8/X9JSqIpfoQhYw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1q6RtC-000lJR-37; Tue, 06 Jun 2023 08:18:02 +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 1q6Rt8-000lHg-1l for linux-riscv@lists.infradead.org; Tue, 06 Jun 2023 08:18:02 +0000 X-QQ-mid: bizesmtp87t1686039463tyigwi8k Received: from linux-lab-host.localdomain ( [61.141.77.49]) by bizesmtp.qq.com (ESMTP) with id ; Tue, 06 Jun 2023 16:17:42 +0800 (CST) X-QQ-SSF: 01200000000000D0V000000A0000000 X-QQ-FEAT: F7SaWszrsrL6jDLoUmUajkY+RVr0d5UzNLhqfwzMPFLZ5nbKS5axwqN/67E3R fNwEwJtdcWRDVbCBrVAWNy+evzIKDFp/4o5+dV9u9+MbYgb3669ikWhskRedGhY+tzS136Z xZfoEXhZySVtLm91gIuU6Y3DsOds/XQbTtSkBGC4xtJg4Do9cztk1JsmxvEedN40vbjUYpM r/9++y9w0naHm5UXjTXiyZGPpcuUP78JsRJQQ0zKth6II6GJ8fFmkuacPIEpQboapg9ND32 niw3IkNTiW2UMNFES4bq/SZDnPqli5kurdryNi02s4AtW13tsSsi2KtGoQDiHyoBKdpgdfO BPuxg1mIvbvMyfMR/jwAmsvZV0zdYQWLmudHDd9OgTcWMHmLag= X-QQ-GoodBg: 0 X-BIZMAIL-ID: 989690427853110087 From: Zhangjin Wu To: thomas@t-8ch.de, 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 Subject: [PATCH v2 4/4] tools/nolibc: sys.h: apply __syscall() helper Date: Tue, 6 Jun 2023 16:17:38 +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-20230606_011758_901625_2A1E30E1 X-CRM114-Status: UNSURE ( 9.20 ) 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 1425 $ git show HEAD:tools/include/nolibc/sys.h | wc -l 1173 $ echo "1425-1173" | 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 f6e3168b3e50..0cfc5157845a 100644 --- a/tools/include/nolibc/sys.h +++ b/tools/include/nolibc/sys.h @@ -108,13 +108,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); } @@ -137,13 +131,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); } @@ -166,13 +154,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); } @@ -189,13 +171,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); } @@ -212,13 +188,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); } @@ -235,13 +205,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); } @@ -264,13 +228,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); } @@ -288,13 +246,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 @@ -312,13 +264,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); } @@ -365,13 +311,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); } @@ -388,13 +328,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); } @@ -411,13 +345,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); } @@ -455,13 +383,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); } @@ -562,13 +484,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); } @@ -606,13 +522,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); } /* @@ -628,13 +538,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); } @@ -657,13 +561,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); } @@ -684,13 +582,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); } @@ -713,13 +605,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); } @@ -742,13 +628,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 @@ -806,13 +686,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); } /* @@ -832,13 +706,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); } @@ -872,13 +740,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); } @@ -898,13 +760,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); } @@ -921,13 +777,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); } @@ -956,13 +806,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); } @@ -979,13 +823,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); } @@ -1003,13 +841,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); } @@ -1026,13 +858,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); } @@ -1072,13 +898,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); } @@ -1095,13 +915,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); } @@ -1118,13 +932,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) @@ -1141,13 +949,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 @@ -1227,13 +1029,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); } @@ -1256,13 +1052,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); } @@ -1296,13 +1086,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); } @@ -1325,13 +1109,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); } @@ -1354,38 +1132,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); } @@ -1402,13 +1162,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); } @@ -1425,13 +1179,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 */