From patchwork Mon Jun 19 15:41:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Zhangjin Wu X-Patchwork-Id: 13284731 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 7F8F9EB64DA for ; Mon, 19 Jun 2023 15:41:46 +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=c/pNTxRHTMNLGPEUTGdhFRAXXaH6qA/rZXMM19kKlNI=; b=4hJd38wiS+v3hM FWPae7O6ZUjJ81VqqSmboCUf7BLUmVz9vw5wKi9/5pOEzl2cHYiSXJOCMJbbU2d0z82E2rHXXLjTG 0/apDgVcPNgfe7uFKMUTgGJixLJrr/nj+MPnTL5igyh+t6GHwBt23xkcaZ6iTXH+ALmR8ZvpRU7Zz Jqmd1fDU4CCieejwpHY9R4QWQoTxXvYVU18UVUbduNlSgILZvop8DrNdCiqpwaptrsNo+m+mVXwZe 3lz2rpO6VgYyj7y/2VLX/CyrjfsX/MoPPPJqC8VePNqDAuKcP83TW/adHGC3ghx6x0aNtZjizfIFb dzRYIMXLdK7AU0QF7c2A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qBH0g-008nrN-2v; Mon, 19 Jun 2023 15:41:42 +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 1qBH0e-008nqQ-0i for linux-riscv@lists.infradead.org; Mon, 19 Jun 2023 15:41:41 +0000 X-QQ-mid: bizesmtp75t1687189283tf03uy18 Received: from linux-lab-host.localdomain ( [116.30.126.60]) by bizesmtp.qq.com (ESMTP) with id ; Mon, 19 Jun 2023 23:41:21 +0800 (CST) X-QQ-SSF: 01200000000000D0V000000A0000000 X-QQ-FEAT: 3M0okmaRx3jbjpkri1hCtt3wapIdtdZ3mKAKL3x4ywEH+9AAkaH2L1w9F6TnS Tx+YKnNgtl1OQjDw+EgPeDa3FaWoF9IQmEUTi2AyVScB6EvSGVTGHXGnIS67XikEy6+naJo fSmO3ZxxH1j3v3ekw8Pf+fz/3Vw5y0iNtBdxY5vVPRSdvVba2yz0+IlpPxN96v8lDQ9vEY2 zEO3bvznX+ArQ0IfNCgqqXbXZ6lLhLV+/FvfQ6IuoCN3z9HLoccn/YpEDOzgZ15ObHr8ldM 6JtYrJ077RhHpSdzMd4doDetReqFvlVxWZ5HY+fXN+9H53o+TN+P/bL9iFJv+xQQb9gYLW6 Nt2EHqyX1g14gbJiAKteWKJ+DTc5ksX0Nx4cmNTluBs3O+E5h5KOoLZ47NeDSnyQVQuzXij X-QQ-GoodBg: 0 X-BIZMAIL-ID: 11055691782021961800 From: Zhangjin Wu To: w@1wt.eu Cc: david.laight@aculab.com, arnd@arndb.de, falcon@tinylab.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-riscv@lists.infradead.org, thomas@t-8ch.de, =?utf-8?q?Thomas_Wei?= =?utf-8?q?=C3=9Fschuh?= Subject: [PATCH v4 01/10] tools/nolibc: sys.h: add a syscall return helper Date: Mon, 19 Jun 2023 23:41:18 +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-20230619_084140_561903_8300F5AA X-CRM114-Status: GOOD ( 11.21 ) 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 syscall return logic: In general, a 0 return value indicates success. A -1 return value indicates an error, and an error number is stored in errno. [1] Let's add a __sysret() helper for the above logic to simplify the coding and shrink the code lines too. Thomas suggested to use inline function instead of macro for __sysret(). Willy suggested to make __sysret() be always inline. [1]: https://man7.org/linux/man-pages/man2/syscall.2.html 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/ Reviewed-by: Thomas Weißschuh Signed-off-by: Zhangjin Wu --- tools/include/nolibc/sys.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h index 856249a11890..150777207468 100644 --- a/tools/include/nolibc/sys.h +++ b/tools/include/nolibc/sys.h @@ -28,6 +28,16 @@ #include "errno.h" #include "types.h" +/* Syscall return helper, set errno as -ret when ret < 0 */ +static __inline__ __attribute__((unused, always_inline)) +long __sysret(long ret) +{ + if (ret < 0) { + SET_ERRNO(-ret); + ret = -1; + } + return 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 Mon Jun 19 15:42:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Zhangjin Wu X-Patchwork-Id: 13284733 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 B8838EB64D9 for ; Mon, 19 Jun 2023 15:43:22 +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=zxo6KMmRBd/Nt+z2e+tSAK76B9ahRvSv8OuE9xZ3nDE=; b=tTSX5422ruCSDj aE5b2CaQKtFF/6JaMmLp5MGbQ064N2H9r/naDLDcRNJ+dGXAjy5skzeovp2XvEi7FM6+aeulSkW2O vIw6WAMtLiuOny2e3SqgsxFp/6mMQ75UYGMKQ8dZilF2+00zpVlrJvUu4ipPXgHvglg1a+FAssGM5 plpkj7CmRlwPzJdBzRyFhEh03hZeu4XEyvZE6UaNK6IcxocgstTyEGu7dMiUGYoPgReAjHgdJ/yr3 aKRIb4NrbFHnbg6uORHc+kupJ9ibL547WpEJTtQZz9b/jvwVFmvnBs0EAPgyuW8IoYWzp16joEaBa nsfvgSpMIUrzP2gXWz4A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qBH2F-008o3T-05; Mon, 19 Jun 2023 15:43:19 +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 1qBH2C-008o1q-0z for linux-riscv@lists.infradead.org; Mon, 19 Jun 2023 15:43:17 +0000 X-QQ-mid: bizesmtp69t1687189376ti8rnzfh Received: from linux-lab-host.localdomain ( [116.30.126.60]) by bizesmtp.qq.com (ESMTP) with id ; Mon, 19 Jun 2023 23:42:55 +0800 (CST) X-QQ-SSF: 01200000000000D0V000000A0000000 X-QQ-FEAT: +ynUkgUhZJmZnOoZiuIrMXfznkHM+fe2DLac6X2hCaoz93yo9fZDMpk5Spd0r mSF8dENOviLg1tJwZf5i8XjFY7wh/EMzZ6we+sOjSPbex/IT+t42oXh6kLDmrKNsWRn0ZF/ DFeHcdSDq8QFtvEYZv9uWfXNKxTja9BzUqs50Q06SuXTkrBvBT+CAC6q/1Z73+U/J2Xlwdt xxO6gjkBoZiC1MW+pwmr2iY1RcucnfvBZOhdE1tSrZ3u6S0u2JqXjNoP46emcM30tBF3/T4 uKUNiyw5muanaQef8DBnvLIGF7LXRmwiDWnqcnPjlLS8FU4mQOlT9jBBWlRUZZO8eobjKTC rjDgHEPm5h3js4fPiteWFYHPVhq5vAKE5HZes7Mp5ce4PX+uP2yLvdCR/VRrw== X-QQ-GoodBg: 0 X-BIZMAIL-ID: 6609999703960132723 From: Zhangjin Wu To: w@1wt.eu Cc: david.laight@aculab.com, arnd@arndb.de, falcon@tinylab.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-riscv@lists.infradead.org, thomas@t-8ch.de, =?utf-8?q?Thomas_Wei?= =?utf-8?q?=C3=9Fschuh?= Subject: [PATCH v4 02/10] tools/nolibc: unistd.h: apply __sysret() helper Date: Mon, 19 Jun 2023 23:42:26 +0800 Message-Id: <2d88e5573cfd5761757d4f9ebb528d62e9e29377.1687187451.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-20230619_084316_644662_20E53F97 X-CRM114-Status: UNSURE ( 7.18 ) 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. Reviewed-by: Thomas Weißschuh 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 0e832e10a0b2..fabc846f797b 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 _syscall_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 Mon Jun 19 15:44:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Zhangjin Wu X-Patchwork-Id: 13284738 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 31770EB64DA for ; Mon, 19 Jun 2023 15:45: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=nXLS05BWqVCBfYq59wjOoC2dObZn67tLUNqksW5faD0=; b=1i+lijYvLZXkK6 xpz6o2hifmCQ44MxUSqH5g46CBSOk0dYKdRPQDNLHZTaxcxCW3PYydriKd1bivY6SQGfsQX71j/v+ 9jqQ76wduAabp9b3ibMg1f3z2oOonWSuHW7v5aSgNYN8CRFiycHollmBeoUhqCEem3uCH1sG3Nv3T q57viRt0UgIlOw2gx+ryt/iG3TTm4YRQj2IpCe3qTzkTHdCWtr0JV3beZKPLrg8C7L4p15sk3wvVJ NcJzD57FrK8iAos60GxybP/L+Dk7oQ+7b6f5NzZMLRyGeBfvtt9ZrRmUX7P0xEefmNITUO7AZyIY+ lFcWaqocLmY3Q4IvTaqw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qBH4F-008oqL-2q; Mon, 19 Jun 2023 15:45:23 +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 1qBH4C-008ooA-04 for linux-riscv@lists.infradead.org; Mon, 19 Jun 2023 15:45:22 +0000 X-QQ-mid: bizesmtp66t1687189493tyi5nzgp Received: from linux-lab-host.localdomain ( [116.30.126.60]) by bizesmtp.qq.com (ESMTP) with id ; Mon, 19 Jun 2023 23:44:52 +0800 (CST) X-QQ-SSF: 01200000000000D0V000000A0000000 X-QQ-FEAT: 5q30pvLz2idsdmc9f9pGnrjy20UAEYph0v6umJ4a/uXyjNtPiHSgH+UYiKC0d CP/677LjPaJ3QbXCilHSkpklfY0Wy/gAYGuGYjC1qHGzQN24elRKaU7shaAKpQ5tKLNIafU zRsMH2nY+2IuRyg/P9DypoW91LN2fhPuz/Eax6yHRCO0vwcOyH5rHp60E/QpGQRmrQ1ztti kapizPoW9zGahmrRqe83WA2vMZcnwW408MUdOb1mQ9wliOm78ijZhUM6DUIjqWXJ48Xh1F6 NMwnLEJA5u2zeCquafQvKOxG2RZRNla/gELXwfERmi+aVW4AEcqW2NZtESqY/G14xL6RHxk 2PkdqueuIIjKJlFbQuc6QF4+90c7CHGgeJDcXIZfhq5nvzCe25/1vRstXAt+g== X-QQ-GoodBg: 0 X-BIZMAIL-ID: 5705770642168147759 From: Zhangjin Wu To: w@1wt.eu Cc: david.laight@aculab.com, arnd@arndb.de, falcon@tinylab.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-riscv@lists.infradead.org, thomas@t-8ch.de, =?utf-8?q?Thomas_Wei?= =?utf-8?q?=C3=9Fschuh?= Subject: [PATCH v4 03/10] tools/nolibc: sys.h: apply __sysret() helper Date: Mon, 19 Jun 2023 23:44:00 +0800 Message-Id: <251570bef4f2b707e53996702321ac7425e3ee64.1687187451.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-20230619_084520_364333_18898BBF X-CRM114-Status: GOOD ( 11.03 ) 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 most of the library routines to oneline code. Removed 266 lines of duplicated code. Reviewed-by: Thomas Weißschuh Signed-off-by: Zhangjin Wu --- tools/include/nolibc/sys.h | 354 +++++-------------------------------- 1 file changed, 44 insertions(+), 310 deletions(-) diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h index 150777207468..4fbefe5adf93 100644 --- a/tools/include/nolibc/sys.h +++ b/tools/include/nolibc/sys.h @@ -76,13 +76,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)) @@ -112,13 +106,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 __sysret(sys_chdir(path)); } @@ -141,13 +129,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 __sysret(sys_chmod(path, mode)); } @@ -170,13 +152,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 __sysret(sys_chown(path, owner, group)); } @@ -193,13 +169,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 __sysret(sys_chroot(path)); } @@ -216,13 +186,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 __sysret(sys_close(fd)); } @@ -239,13 +203,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 __sysret(sys_dup(fd)); } @@ -268,13 +226,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 __sysret(sys_dup2(old, new)); } @@ -292,13 +244,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 __sysret(sys_dup3(old, new, flags)); } #endif @@ -316,13 +262,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 __sysret(sys_execve(filename, argv, envp)); } @@ -369,13 +309,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 __sysret(sys_fork()); } @@ -392,13 +326,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 __sysret(sys_fsync(fd)); } @@ -415,13 +343,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 __sysret(sys_getdents64(fd, dirp, count)); } @@ -459,13 +381,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 __sysret(sys_getpgid(pid)); } @@ -545,15 +461,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); } @@ -570,13 +478,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 __sysret(sys_gettimeofday(tv, tz)); } @@ -614,13 +516,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 __sysret(sys_ioctl(fd, req, value)); } /* @@ -636,13 +532,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 __sysret(sys_kill(pid, signal)); } @@ -665,13 +555,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 __sysret(sys_link(old, new)); } @@ -688,13 +572,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 __sysret(sys_lseek(fd, offset, whence)); } @@ -717,13 +595,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 __sysret(sys_mkdir(path, mode)); } @@ -746,13 +618,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 __sysret(sys_mknod(path, mode, dev)); } #ifndef MAP_SHARED @@ -810,13 +676,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 __sysret(sys_munmap(addr, length)); } /* @@ -836,13 +696,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 __sysret(sys_mount(src, tgt, fst, flags, data)); } @@ -876,13 +730,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 __sysret(sys_open(path, flags, mode)); } @@ -902,13 +750,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 __sysret(sys_prctl(option, arg2, arg3, arg4, arg5)); } @@ -925,13 +767,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 __sysret(sys_pivot_root(new, old)); } @@ -960,13 +796,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 __sysret(sys_poll(fds, nfds, timeout)); } @@ -983,13 +813,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 __sysret(sys_read(fd, buf, count)); } @@ -1007,13 +831,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 __sysret(sys_reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, 0)); } @@ -1030,13 +848,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 __sysret(sys_sched_yield()); } @@ -1076,13 +888,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 __sysret(sys_select(nfds, rfds, wfds, efds, timeout)); } @@ -1099,13 +905,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 __sysret(sys_setpgid(pid, pgid)); } @@ -1122,13 +922,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 __sysret(sys_setsid()); } #if defined(__NR_statx) @@ -1145,13 +939,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 __sysret(sys_statx(fd, path, flags, mask, buf)); } #endif @@ -1231,13 +1019,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 __sysret(sys_stat(path, buf)); } @@ -1260,13 +1042,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 __sysret(sys_symlink(old, new)); } @@ -1300,13 +1076,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 __sysret(sys_umount2(path, flags)); } @@ -1329,13 +1099,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 __sysret(sys_unlink(path)); } @@ -1354,38 +1118,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 __sysret(sys_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 __sysret(sys_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 __sysret(sys_wait4(pid, status, options, NULL)); } @@ -1402,13 +1148,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 __sysret(sys_write(fd, buf, count)); } @@ -1425,13 +1165,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 __sysret(sys_memfd_create(name, flags)); } /* make sure to include all global symbols */ From patchwork Mon Jun 19 15:45:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhangjin Wu X-Patchwork-Id: 13284739 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 959E4EB64DA for ; Mon, 19 Jun 2023 15:46: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=oqQdDchEWmwo/vyKEgOtAkdaKCe50MK+I8nnuwaTYM4=; b=xvYmv7V7NrQmFT Sez91npomXNFvFYLycnv6hTbYYS+WAiweM84P+rMcnp172Mg6lB1lvcdYB9eJm9lzOGJ8MsUXstcG 48Yr4IW/09BYj6nKsgFPorwzGAyXJieFCFxT1i/cTC5CtusiGB24oaOygoPQALMsADZxTeoMpjiFf s6GRdKzXvbUjXFLuN8xeLycNaMN2q4AWFXKSCnoGVjYMsSboJINeEBgNhXOGWtedXOkDF2ku1KA22 3aTSAWLSLJv4fYNgKQ4/8hU27+50t/GEDgPMJNm9TOOT+Qritteqx7jzs+tgVy00TkGrMl3SCXR6Z v+pJYnR7xQspVzE/2rKg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qBH5K-008pBE-1E; Mon, 19 Jun 2023 15:46:30 +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 1qBH5G-008p94-2Y for linux-riscv@lists.infradead.org; Mon, 19 Jun 2023 15:46:28 +0000 X-QQ-mid: bizesmtp74t1687189569t7m7jpj5 Received: from linux-lab-host.localdomain ( [116.30.126.60]) by bizesmtp.qq.com (ESMTP) with id ; Mon, 19 Jun 2023 23:46:08 +0800 (CST) X-QQ-SSF: 01200000000000D0V000000A0000000 X-QQ-FEAT: 6/K5pWSRdGohZNUvm+E+aBmN22HnnFyRXuRHvnGIbTDVz3MMHAcfAkZHut/22 oBZEiw0bdSq3fdy8TZ1wLcWNbBv6BS5Qkib2Uf/E07s/XKnsvEdfPyIeyfdcmGQ6iaEekGP wdGVwIuoFAS/GmCNoaZGs0BE23z2/9ssANxofiDOaUMK8Q7LUyJZt3nZLNQ0BTE+xWG91mO 1CqBoQCuXORCxllYiYYSKCPE9I3aJbD3ryi9WUUHv34iJKJthT6220tPZBmICcgArJqXJZb JaMq/mVg+AInXb433YehYBPwUtAehkvYfqnYiWAMbukF2h8Ph9i9NRvCK3jVSr7od41o5YK kjrCWk+7LIYzRIw/YRYGUaX+gcdvt1WqvCfNx7SdcYexpWQ7yYQ3liuAHlGfg== X-QQ-GoodBg: 0 X-BIZMAIL-ID: 6480484968202464133 From: Zhangjin Wu To: w@1wt.eu Cc: david.laight@aculab.com, arnd@arndb.de, falcon@tinylab.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-riscv@lists.infradead.org, thomas@t-8ch.de Subject: [PATCH v4 04/10] tools/nolibc: unistd.h: reorder the syscall macros Date: Mon, 19 Jun 2023 23:45:56 +0800 Message-Id: <1bd84b8adfa209f89a04e6beadb412385af7859b.1687187451.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-20230619_084627_138270_B3CB3B9A X-CRM114-Status: UNSURE ( 8.62 ) 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 Tune the macros in the using order and align most of them. Signed-off-by: Zhangjin Wu --- tools/include/nolibc/unistd.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/include/nolibc/unistd.h b/tools/include/nolibc/unistd.h index fabc846f797b..e38f3660c051 100644 --- a/tools/include/nolibc/unistd.h +++ b/tools/include/nolibc/unistd.h @@ -56,9 +56,9 @@ int tcsetpgrp(int fd, pid_t pid) return ioctl(fd, TIOCSPGRP, &pid); } -#define _syscall(N, ...) __sysret(my_syscall##N(__VA_ARGS__)) -#define _syscall_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_narg(...) __syscall_narg(__VA_ARGS__, 6, 5, 4, 3, 2, 1, 0) +#define _syscall(N, ...) __sysret(my_syscall##N(__VA_ARGS__)) #define _syscall_n(N, ...) _syscall(N, __VA_ARGS__) #define syscall(...) _syscall_n(_syscall_narg(__VA_ARGS__), ##__VA_ARGS__) From patchwork Mon Jun 19 15:47: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: 13284745 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 9AF23EB64DA for ; Mon, 19 Jun 2023 15:47:41 +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=s9vLKT6/7VPlkmEqpCH3YWJfPzd9sjo4Z/4Cyvy+a6k=; b=UYThf9RWvUrV4s sbm6uL6pV2aKWApu8KBJZfxm2gRUi6WOnYqqIQUSfzvlP+1DmlcIHu2/BVL8b17PFhDbAA3HjhjgG LxOcFRauwSTPYs57aIJhXbD5aOj6eb6UdWMYVoGr2FhJIM/cMv7ismf+dcLJtGVUiPzTC6vLmz3ja glin5GkiKtTi7FPDuIpCjEKfza30wMtc7NoPWL0LjNW/4TD08sni9sSnakyPU+opBaA2kLtfzlmgY ZTvwYpVLJ4bw9u7YkCS2bPdx6E1eHpv6MhsKhOhmf5QTlOcgKWchWZkfhC8lqxNFLVry/Ha4/519z 7S1l7+JC6BmAfK3njxUg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qBH6P-008pOU-2D; Mon, 19 Jun 2023 15:47:37 +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 1qBH6M-008pNI-23 for linux-riscv@lists.infradead.org; Mon, 19 Jun 2023 15:47:36 +0000 X-QQ-mid: bizesmtp71t1687189637tfw3qus3 Received: from linux-lab-host.localdomain ( [116.30.126.60]) by bizesmtp.qq.com (ESMTP) with id ; Mon, 19 Jun 2023 23:47:15 +0800 (CST) X-QQ-SSF: 01200000000000D0V000000A0000000 X-QQ-FEAT: FVl8EHhfVR5hTM2MdejT0GIbTUMQnvvUQOWCRxCGR49gRYux9d12G2vVb1lRF 8nq44JbZnU0bAxj0jSCsfcBSnRc0JIxf/jv3bG/13gWzc3g2D0LkkHTvfJF2apjjfviXc7Z CzsdtFb6SMoG0Rg4aRXlRooX7fXnpjG/6m4wWa+KcgD8bp8m+4QaoymFemrnQ4SBC5aTwHe C5zEEEN2nDn46AjT8WhSfRULcj588bZGxRL3Nq6UQirUYAVy/3YPM7NnaEH63bX6VUzD6lt V0gXqP6ju6pyEStUUX71fZJeb/rH0LOdpd3lRFsptbPeM4NeKX51kaJi+AKMiikSpDE90/C P4rfTz822WFFf8KcwQRa8FJoB+Azztezxzl3qZrnw/JLJlyL3pXca5bRlaU81/rWwt1HFvf X-QQ-GoodBg: 0 X-BIZMAIL-ID: 3012571999382365335 From: Zhangjin Wu To: w@1wt.eu Cc: david.laight@aculab.com, arnd@arndb.de, falcon@tinylab.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-riscv@lists.infradead.org, thomas@t-8ch.de Subject: [PATCH v4 05/10] tools/nolibc: add missing my_syscall6() for mips Date: Mon, 19 Jun 2023 23:47:12 +0800 Message-Id: <44abf14c4fa920eadb98833356e517d414b1d66d.1687187451.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-20230619_084734_996098_01B98B8A X-CRM114-Status: GOOD ( 12.00 ) 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 It is able to pass the 6th argument like the 5th argument via the stack for mips, let's add a new my_syscall6() now, see [1] for details: The mips/o32 system call convention passes arguments 5 through 8 on the user stack. Both mmap() and pselect6() require my_syscall6(). [1]: https://man7.org/linux/man-pages/man2/syscall.2.html Signed-off-by: Zhangjin Wu --- tools/include/nolibc/arch-mips.h | 26 ++++++++++++++++++++++++++ tools/include/nolibc/nolibc.h | 9 ++++----- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/tools/include/nolibc/arch-mips.h b/tools/include/nolibc/arch-mips.h index db24e0837a39..48dd42b4abc3 100644 --- a/tools/include/nolibc/arch-mips.h +++ b/tools/include/nolibc/arch-mips.h @@ -178,6 +178,32 @@ struct sys_stat_struct { _arg4 ? -_num : _num; \ }) +#define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \ +({ \ + register long _num __asm__ ("v0") = (num); \ + register long _arg1 __asm__ ("a0") = (long)(arg1); \ + register long _arg2 __asm__ ("a1") = (long)(arg2); \ + register long _arg3 __asm__ ("a2") = (long)(arg3); \ + register long _arg4 __asm__ ("a3") = (long)(arg4); \ + register long _arg5 = (long)(arg5); \ + register long _arg6 = (long)(arg6); \ + \ + __asm__ volatile ( \ + "addiu $sp, $sp, -32\n" \ + "sw %7, 16($sp)\n" \ + "sw %8, 20($sp)\n" \ + "syscall\n " \ + "addiu $sp, $sp, 32\n" \ + : "=r" (_num), "=r"(_arg4) \ + : "0"(_num), \ + "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \ + "r"(_arg6) \ + : "memory", "cc", "at", "v1", "hi", "lo", \ + "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9" \ + ); \ + _arg4 ? -_num : _num; \ +}) + char **environ __attribute__((weak)); const unsigned long *_auxv __attribute__((weak)); diff --git a/tools/include/nolibc/nolibc.h b/tools/include/nolibc/nolibc.h index 05a228a6ee78..1f8d821000ac 100644 --- a/tools/include/nolibc/nolibc.h +++ b/tools/include/nolibc/nolibc.h @@ -13,11 +13,10 @@ * Syscalls are split into 3 levels: * - The lower level is the arch-specific syscall() definition, consisting in * assembly code in compound expressions. These are called my_syscall0() to - * my_syscall6() depending on the number of arguments. The MIPS - * implementation is limited to 5 arguments. All input arguments are cast - * to a long stored in a register. These expressions always return the - * syscall's return value as a signed long value which is often either a - * pointer or the negated errno value. + * my_syscall6() depending on the number of arguments. All input arguments + * are castto a long stored in a register. These expressions always return + * the syscall's return value as a signed long value which is often either + * a pointer or the negated errno value. * * - The second level is mostly architecture-independent. It is made of * static functions called sys_() which rely on my_syscallN() From patchwork Mon Jun 19 15:48:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhangjin Wu X-Patchwork-Id: 13284746 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 876B1EB64D9 for ; Mon, 19 Jun 2023 15:50:42 +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=g7OI99EJtdkc2d8l9l2QvOzPDwT8wKobslDNbjAt9Uo=; b=VYMzeGQCvsjg9m I7VkV9LdfK35tfQdCDJNFWXYdilyYIwT2SnWYVrJ2mCVnds3PnHJMfbmEk/0cQA95eBw99Xx05UnV mpPE41Q5OJGF3XroMoq4No+xxVC/jn7V1eci1oooVLWgpJslmjGKz1pOBxpJ6p+egicMCVCRenJJK 0pO3ID/HBtgcdaD3nUbVDNaT5t+ybu+QpuDNeJqu5FRVcccMDep23/P1W816As33mD1QAHlrg977k Wx+fhMz2u+bWHvmPL9iSPYgeVlFSDvxOPlrFKkso1Ql0XwWIxuguyDHTS9QOUDuGelcJtTyJS1xTM NOVqM+ZcUf/PVYndPjPw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qBH9K-008ptW-0W; Mon, 19 Jun 2023 15:50:38 +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 1qBH9G-008ps4-2j for linux-riscv@lists.infradead.org; Mon, 19 Jun 2023 15:50:36 +0000 X-QQ-mid: bizesmtp65t1687189816tdj31vkq Received: from linux-lab-host.localdomain ( [116.30.126.60]) by bizesmtp.qq.com (ESMTP) with id ; Mon, 19 Jun 2023 23:50:14 +0800 (CST) X-QQ-SSF: 01200000000000D0V000000A0000000 X-QQ-FEAT: lJ9dImo9Gpeyz8IOKq+GcNXt5bf5lKsHx4krldfzto/2klwvpwlW5zu4HsFMO T3D5l8mDvV8FkGonqQzuWoU43QdE6GRR6PIVIB0HxaHgFBH0BglfRFDghFmMPBZM8DI5II7 1VmJ6Lw8HBn/7eZ94UtUuKdSkNOVFWWoatTOu/yoQdknRrDgAMarKa+RO+x/sJPTWthw60d FOPdQ9jEm8q4jTAlrRtzy/dOgsqWUQkAXSGHiUxYPOh0i7mnXppFlh6qDQrVDFpSYG70mbs HCEsMn0XqQDYpLsRzogrLqNBsOjF9Vx78SpiIXCEViHeVbcUwPuoB20V4Mq9sMAQiVDcul0 3A3z92ODahZBnR/e1BhsIBg5cub/ZAWqKvEcrF71WY0VAO9OHRUliKtrQKgw8FfmlatbISe X-QQ-GoodBg: 0 X-BIZMAIL-ID: 1071758241014777112 From: Zhangjin Wu To: w@1wt.eu Cc: david.laight@aculab.com, arnd@arndb.de, falcon@tinylab.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-riscv@lists.infradead.org, thomas@t-8ch.de, David Laight Subject: [PATCH v4 06/10] tools/nolibc: __sysret: support syscalls who return a pointer Date: Mon, 19 Jun 2023 23:48:21 +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-20230619_085035_188728_37F0923A X-CRM114-Status: GOOD ( 12.10 ) 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 To support syscalls (e.g. mmap()) who return a pointer and to allow the pointer as big as possible, we should convert the negated errno value to unsigned long (uintptr_t), otherwise, in signed long, a potential big pointer (whose highest bit is 1) will be treated as a failure. tools/include/nolibc/errno.h defines the MAX_ERRNO, let's use it directly. after converting to unsigned long, the negative errno value from -1 to -MAX_ERRNO becomes something like '~1 + 1' (every bit is 1) to '~MAX_ERRNO + 1', '~1 + 1' is the biggest, '~MAX_ERRNO + 1' is the smallest, so, the check becomes: if (ret <= (unsigned long)-1 && ret >= (unsigned long)-MAX_ERRNO) { ... } Since (unsigned long)-1 is the biggest unsigned long value, it is always true if bigger than (unsigned long)-MAX_ERRNO, so, just reserve the following check is enough: if (ret >= (unsigned long)-MAX_ERRNO) { ... } Suggested-by: David Laight Link: https://lore.kernel.org/linux-riscv/94dd5170929f454fbc0a10a2eb3b108d@AcuMS.aculab.com/ Signed-off-by: Zhangjin Wu --- tools/include/nolibc/sys.h | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h index 4fbefe5adf93..8a6e16472d54 100644 --- a/tools/include/nolibc/sys.h +++ b/tools/include/nolibc/sys.h @@ -28,13 +28,16 @@ #include "errno.h" #include "types.h" -/* Syscall return helper, set errno as -ret when ret < 0 */ + +/* Syscall return helper for library routines + * set errno as -ret when ret in [-MAX_ERRNO, -1] + */ static __inline__ __attribute__((unused, always_inline)) -long __sysret(long ret) +long __sysret(unsigned long ret) { - if (ret < 0) { - SET_ERRNO(-ret); - ret = -1; + if (ret >= (unsigned long)-MAX_ERRNO) { + SET_ERRNO(-(long)ret); + return -1; } return ret; } From patchwork Mon Jun 19 15:51: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: 13284755 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 0F77EEB64DA for ; Mon, 19 Jun 2023 15:51:53 +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=SQnaX+gJTY7CTB2CvVQBvc5YadilhOb7wnsHqLe5j4s=; b=B/4HsHwX8mHsPz CTNN4PzX5scOrIDLTy9C2PHYj7qO1ha99SPpDgx/0itS2L1vYtugISnjyUhLjkjHj111iayniuBWu ZoiI0PTv8U5aizn6ELZ81rHppHt3SHA7y3lD4JS/mEblcf+c1+x9GIuga9qXuMlBGsJM95IIG2n7l Et0Gh3AnAzCScg4Pj294pQX6UXh48CEomLv+MlN5Lonr151DYy0B8KF00Xp1qk3cdm4tpKVVc+64T uFMtNE9ttNfs4rZhDkMgYwoau0bgk4NoSymCQNKvuo/kWazL52UXnIb1YjrZWsRJBD8M2P0yvqqwR 8VJTInR44ixfJTKeiiEw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qBHAS-008q9f-2H; Mon, 19 Jun 2023 15:51:48 +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 1qBHAP-008q7A-00 for linux-riscv@lists.infradead.org; Mon, 19 Jun 2023 15:51:47 +0000 X-QQ-mid: bizesmtp64t1687189887tg4fzjsv Received: from linux-lab-host.localdomain ( [116.30.126.60]) by bizesmtp.qq.com (ESMTP) with id ; Mon, 19 Jun 2023 23:51:26 +0800 (CST) X-QQ-SSF: 01200000000000D0V000000A0000000 X-QQ-FEAT: 5q30pvLz2icBH/caXxT27x4f9FBSJcYyNdLV7m3J8v2Ry7takMP0mXa7DT1i3 jQwPJO38Q3Zvqx2UcT3WQjrB96lfe6DY5IP8gMxXL0flRy+x+YiGwXSCxfPaniPbV/rHyxs yoCZLQy3AZVEbwKpQFcTc9cslDqmauQUwqfn8v36BVI4qnUnlhFcGWHj9fDtwxI8XCdfYIv i9fBc5Z6q3dWlxGEJI1/wI4qq4HDv2X/movyUNAkL1VkfOgx0kA6MUkYC6oLN4eefLH/mRc tao0Zr/YcNcFkXKtnmghIGRbaKk31qEouoCTsHb4mhVL4uhexQC/kBtEQnAz7gZoTN4Yoz5 6O2NyuOrm0FK53k38XqBR3Z/A38eOqjxexOj5nmTN6RQsEeT+lQZW4+A4mKEA== X-QQ-GoodBg: 0 X-BIZMAIL-ID: 6198696245822752568 From: Zhangjin Wu To: w@1wt.eu Cc: david.laight@aculab.com, arnd@arndb.de, falcon@tinylab.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-riscv@lists.infradead.org, thomas@t-8ch.de Subject: [PATCH v4 07/10] tools/nolibc: clean up mmap() support Date: Mon, 19 Jun 2023 23:51: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-20230619_085145_343516_DC67C0C4 X-CRM114-Status: GOOD ( 10.10 ) 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 Do several cleanups together: - Since all supported architectures have my_syscall6() now, remove the #ifdef check. - Move the mmap() related macros to tools/include/nolibc/types.h - Apply the new __sysret() to convert the calling of sys_map() to oneline code Signed-off-by: Zhangjin Wu --- tools/include/nolibc/sys.h | 24 +----------------------- tools/include/nolibc/types.h | 11 +++++++++++ 2 files changed, 12 insertions(+), 23 deletions(-) diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h index 8a6e16472d54..1c02cec3bcd9 100644 --- a/tools/include/nolibc/sys.h +++ b/tools/include/nolibc/sys.h @@ -624,26 +624,11 @@ int mknod(const char *path, mode_t mode, dev_t dev) return __sysret(sys_mknod(path, mode, dev)); } -#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 - #ifndef sys_mmap 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 (void *)-ENOSYS; -#else - int n; #if defined(__NR_mmap2) @@ -654,20 +639,13 @@ void *sys_mmap(void *addr, size_t length, int prot, int flags, int fd, #endif return (void *)my_syscall6(n, addr, length, prot, flags, fd, offset); -#endif } #endif static __attribute__((unused)) void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) { - void *ret = sys_mmap(addr, length, prot, flags, fd, offset); - - if ((unsigned long)ret >= -4095UL) { - SET_ERRNO(-(long)ret); - ret = MAP_FAILED; - } - return ret; + return (void *)__sysret((unsigned long)sys_mmap(addr, length, prot, flags, fd, offset)); } static __attribute__((unused)) diff --git a/tools/include/nolibc/types.h b/tools/include/nolibc/types.h index f96e28bff4ba..f889d4e0ac7e 100644 --- a/tools/include/nolibc/types.h +++ b/tools/include/nolibc/types.h @@ -81,6 +81,17 @@ #define MAXPATHLEN (PATH_MAX) #endif +/* flags for mmap */ +#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 + /* whence values for lseek() */ #define SEEK_SET 0 #define SEEK_CUR 1 From patchwork Mon Jun 19 15:52:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhangjin Wu X-Patchwork-Id: 13284756 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 E2C89EB64D9 for ; Mon, 19 Jun 2023 15:53:36 +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=FpkstmRqfvGPyMHxTW9apmCkVeS8YrjjuiOmguts4tY=; b=RdhUlxS6OdvDky HsSRE3Y8+0JbWXPob/5I9ZMz6nikfiXWXpylGoV8VOZrIpcB3IS2Wk37/DArFwFGHtt4vUWPNSEqC s3y6HwKYSlCFK0IwuE0Hl2UtZLwx8gKy4iYdavZVZNhIys4ky0OXQwjDq63m6t/yOqcrM3Z/nyl3C Rfc6gjHlzHE50ojeQdkKRydjQKg0Cu3MygFSUIcm6cazDhO+mURHR5UuZk/AHviCPnxMoH8hp36BC g5qEzHCIsIt6EZ1hBnbjEHJkVijfqLe3V00Kn4bfCFQX49kEc/WheeWpEP/jpUxotWjtF6PEdONcy pw8jkhONb7V79nwMaO9Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qBHC9-008qfI-05; Mon, 19 Jun 2023 15:53:33 +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 1qBHC4-008qdk-2s for linux-riscv@lists.infradead.org; Mon, 19 Jun 2023 15:53:30 +0000 X-QQ-mid: bizesmtp85t1687189991tj28c6n8 Received: from linux-lab-host.localdomain ( [116.30.126.60]) by bizesmtp.qq.com (ESMTP) with id ; Mon, 19 Jun 2023 23:53:09 +0800 (CST) X-QQ-SSF: 01200000000000D0V000000A0000000 X-QQ-FEAT: IcCSTr/hHjPXBzWN/U8TqAy26Icbc9MCR5PRDeanWzS97OqZQp6D8x32yCRXd z7v3lJkDjh8Bnduvjgxp6BUtmiTBWR4NZ+ZdQiVjrii5Yv4zug2TpFN1ywarWMuQiS68CRm oI9rETIcMUDABclQFL803tjbxYaTUC8P5gnCkG/xKhjTdifzmw9KjwKDZj9R0x5vjatl5xW n44oyKgmuqburMW/jSxFr/Lm/YCcDTSSCHz/KPdiklbNFlPmEQje0supEPDaWXkOp098Hrq PWsFjKpwymtqmhg2ZEPFcOKRiDvAB1mdcheA4/IXre18GQrwZVLwISWCRUdKb7ACqM87hMx dGz9eICsxNFzymXN/Z7LPoSpK5ykIi/QDivgqdAd+0g6VfRJ7M62nmIGdf8MacIUA3vKknU X-QQ-GoodBg: 0 X-BIZMAIL-ID: 16052288436696807788 From: Zhangjin Wu To: w@1wt.eu Cc: david.laight@aculab.com, arnd@arndb.de, falcon@tinylab.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-riscv@lists.infradead.org, thomas@t-8ch.de Subject: [PATCH v4 08/10] selftests/nolibc: add EXPECT_PTREQ, EXPECT_PTRNE and EXPECT_PTRER Date: Mon, 19 Jun 2023 23:52:31 +0800 Message-Id: <12bd89048c33436a72d2a1b34ea79c60ac3643d5.1687187451.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-20230619_085329_241292_AF49CAEA X-CRM114-Status: GOOD ( 10.29 ) 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 The syscalls like sbrk() and mmap() return pointers, to test them, more pointer compare test macros are required, add them: - EXPECT_PTREQ() expects two equal pointers. - EXPECT_PTRNE() expects two non-equal pointers. - EXPECT_PTRER() expects failure with a specified errno. - EXPECT_PTRER2() expects failure with two specified errnos. Signed-off-by: Zhangjin Wu --- tools/testing/selftests/nolibc/nolibc-test.c | 58 ++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c index 486334981e60..34af802dadfd 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -361,6 +361,64 @@ static int expect_ptrnz(const void *expr, int llen) return ret; } +#define EXPECT_PTREQ(cond, expr, cmp) \ + do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_ptreq(expr, llen, cmp); } while (0) + +static int expect_ptreq(const void *expr, int llen, const void *cmp) +{ + int ret = 0; + + llen += printf(" = <%p> ", expr); + if (expr != cmp) { + ret = 1; + llen += pad_spc(llen, 64, "[FAIL]\n"); + } else { + llen += pad_spc(llen, 64, " [OK]\n"); + } + return ret; +} + +#define EXPECT_PTRNE(cond, expr, cmp) \ + do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_ptrne(expr, llen, cmp); } while (0) + +static int expect_ptrne(const void *expr, int llen, const void *cmp) +{ + int ret = 0; + + llen += printf(" = <%p> ", expr); + if (expr == cmp) { + ret = 1; + llen += pad_spc(llen, 64, "[FAIL]\n"); + } else { + llen += pad_spc(llen, 64, " [OK]\n"); + } + return ret; +} + +#define EXPECT_PTRER2(cond, expr, expret, experr1, experr2) \ + do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_ptrerr2(expr, expret, experr1, experr2, llen); } while (0) + +#define EXPECT_PTRER(cond, expr, expret, experr) \ + EXPECT_PTRER2(cond, expr, expret, experr, 0) + +static int expect_ptrerr2(const void *expr, const void *expret, int experr1, int experr2, int llen) +{ + int ret = 0; + int _errno = errno; + + llen += printf(" = <%p> %s ", expr, errorname(_errno)); + if (expr != expret || (_errno != experr1 && _errno != experr2)) { + ret = 1; + if (experr2 == 0) + llen += printf(" != (<%p> %s) ", expret, errorname(experr1)); + else + llen += printf(" != (<%p> %s %s) ", expret, errorname(experr1), errorname(experr2)); + llen += pad_spc(llen, 64, "[FAIL]\n"); + } else { + llen += pad_spc(llen, 64, " [OK]\n"); + } + return ret; +} #define EXPECT_STRZR(cond, expr) \ do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_strzr(expr, llen); } while (0) From patchwork Mon Jun 19 15:54:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhangjin Wu X-Patchwork-Id: 13284757 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 5B6CBEB64DB for ; Mon, 19 Jun 2023 15:55:03 +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=yp92ESjfsjb3VO5Tve05o0hPCX6puOgy6PncFCUYYiA=; b=EfJYODku3vCXfo StSA0UObGk7XPojK3M2jS4BaVuZbe0DaIdJ9B7itouKepRFHpCp31BtJrM/ocRwcfRXbgekel/5vr c5p6ysNlneofKyzYCoWYjSp6p2ooTrQK9nbYKUHeB0i7MFS41X13iJ5NZ6Pk9ElODDEpYecODnaWC vfPiFx147OZpUNWPXoewHe/jOyae44IPlfGY5ck7PJ0TzxGlJSedGNumD9GXoZAulD+o4fP5fWrZw Vusw0qHuPszZUt0gT21G/1kpvackEaMQ/2HAiwFfXqzCVtgKXJe1SlusEqg4IwHV4a0QlKJ72PANp YLqGFhhXulveqGZqREuQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qBHDX-008r4o-1V; Mon, 19 Jun 2023 15:54:59 +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 1qBHDT-008r3X-39 for linux-riscv@lists.infradead.org; Mon, 19 Jun 2023 15:54:57 +0000 X-QQ-mid: bizesmtp88t1687190077tbbjb744 Received: from linux-lab-host.localdomain ( [116.30.126.60]) by bizesmtp.qq.com (ESMTP) with id ; Mon, 19 Jun 2023 23:54:36 +0800 (CST) X-QQ-SSF: 01200000000000D0V000000A0000000 X-QQ-FEAT: 7jw2iSiCazrClih7lQSw1lHt5a5sEA2WAD1UMQ9RMYxH4lPlMP9RlLSFyxJtv j+1aODH0kfVvVUG+Hy8dMmNAmWCFvsk7cZphwRrkAIZmNzeRcqbsnR/b9YWp4mLgpZYF8ld 1Z2z/0qDH1rmprRPceUvjzkuQXRO5Dnej7ew6HFEXPKN76cwi5AkJzV4Piq3D1EF4ZqlAgb J0QjvClC5shv0UDB2arUdlC8rfhsKGMNY9guCj6qdEvcw1uJFuz9xx6PEb4iLKZBwtyNtiA 9QJE/Iu05lxs6gtg6Q0YYXvOdh+oKy6Jy6Ckk5ArTaEIK5YorKlbnR/0/zKNqR5/pxE+LAy 83PDzWkqpozLdwmYpDUci7RfDcCowkFAAhvNqPTgaagQ0fEoys/YTJD6UYrAA== X-QQ-GoodBg: 0 X-BIZMAIL-ID: 2291941404729433177 From: Zhangjin Wu To: w@1wt.eu Cc: david.laight@aculab.com, arnd@arndb.de, falcon@tinylab.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-riscv@lists.infradead.org, thomas@t-8ch.de Subject: [PATCH v4 09/10] selftests/nolibc: add sbrk_0 to test current brk getting Date: Mon, 19 Jun 2023 23:54:15 +0800 Message-Id: <105b488a94953ec8e7b4555bbc89c8e5a17e2c8f.1687187451.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-20230619_085456_312568_B94AFB80 X-CRM114-Status: UNSURE ( 7.15 ) 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 From musl 0.9.14 (to the latest version 1.2.3), both sbrk() and brk() have almost been disabled for they conflict with malloc, only sbrk(0) is still permitted as a way to get the current brk, let's support such case. EXPECT_PTRNE() is used to expect sbrk() always successfully getting the current brk. Signed-off-by: Zhangjin Wu --- tools/testing/selftests/nolibc/nolibc-test.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c index 34af802dadfd..80ab29e2887c 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -630,6 +630,7 @@ int run_syscall(int min, int max) CASE_TEST(kill_0); EXPECT_SYSZR(1, kill(getpid(), 0)); break; CASE_TEST(kill_CONT); EXPECT_SYSZR(1, kill(getpid(), 0)); break; CASE_TEST(kill_BADPID); EXPECT_SYSER(1, kill(INT_MAX, 0), -1, ESRCH); break; + CASE_TEST(sbrk_0); EXPECT_PTRNE(1, sbrk(0), (void *)-1); break; CASE_TEST(sbrk); if ((p1 = p2 = sbrk(4096)) != (void *)-1) p2 = sbrk(-4096); EXPECT_SYSZR(1, (p2 == (void *)-1) || p2 == p1); break; CASE_TEST(brk); EXPECT_SYSZR(1, brk(sbrk(0))); break; CASE_TEST(chdir_root); EXPECT_SYSZR(1, chdir("/")); break; From patchwork Mon Jun 19 15:55:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhangjin Wu X-Patchwork-Id: 13284792 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 67B0FEB64DA for ; Mon, 19 Jun 2023 15:56:20 +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=iU3/7wlSzW7dhrOh7DNxwG0fuDAD1umcjB2Ce+3C7G0=; b=g37c+13yWaR0qZ EGL3wCUW5LoRnt21+4jmaYL/83PWeGSSuwbFI++tn1OnPUW2i2WJQH64vVjLminEkJMCA2+b1IXnq M3ci11IVooiugfr8zUsaKtzY0J48H1wvCF21WToq8E2fWDY8jSCSlnGVNS5iT6eSfWxG+DooubZ8K 3KFNB+t49d+JwA23VjZhjy085+9z2g3+Yqcq0d3inDJ4Kf9vpX3sxJ6RV3mbJNa6tR/A4VIEJoQqy x8jPBQNnakD1CDjvMOk4TkLoRMFQbb/Ute8FCp1IyTSTmACrtWweWrg3ePqKeRtUaBN5YYnkPaqF5 tnsvlE6Zs67lt615y2ig==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qBHEm-008rLb-2B; Mon, 19 Jun 2023 15:56:16 +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 1qBHEk-008rKg-2V for linux-riscv@lists.infradead.org; Mon, 19 Jun 2023 15:56:16 +0000 X-QQ-mid: bizesmtp67t1687190155tjgcboy4 Received: from linux-lab-host.localdomain ( [116.30.126.60]) by bizesmtp.qq.com (ESMTP) with id ; Mon, 19 Jun 2023 23:55:54 +0800 (CST) X-QQ-SSF: 01200000000000D0V000000A0000000 X-QQ-FEAT: 3M0okmaRx3j+DH7O25w4I49usNQKwukZko0M+fI3sghNet1qUt9QPoFPGaQxB qUMyPH1KyGxQc++m+763kfmCEtcyL49JXo2yHMQadZgrhtZ7A1HIxET4qQIWuaQOQuc0B5z vXxgUMSbD9P70U/yfA+nxAN7nnTlIGoBh+4w3niCvA+Z/2EOraI8DQpA6m6s2k1ajHZvFoE FAdBwRDieRX70M3hWobE0uCkcUfsM27XxUYZSJ731A6sywzYCHmnPH1saAHF580yKVSi5mj zZ4fd6N1vzXqxDKGes/atceNbMHFINvKZj0tmM57KNReZMT+tcf5OTPZU7XYbS6Z2xzD3im 9Tcu3DQ5VpCXjBBMGVSGOFXsL98Q4YN1yqC9dgHnKrP/0d+Ma8YNWMUUD2oOwcdwJyI9WXD X-QQ-GoodBg: 0 X-BIZMAIL-ID: 1231696777476800510 From: Zhangjin Wu To: w@1wt.eu Cc: david.laight@aculab.com, arnd@arndb.de, falcon@tinylab.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-riscv@lists.infradead.org, thomas@t-8ch.de Subject: [PATCH v4 10/10] selftests/nolibc: add mmap and munmap test cases Date: Mon, 19 Jun 2023 23:55:41 +0800 Message-Id: <9c46f648cd8c784405afed565bed120f0a2f239e.1687187451.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-20230619_085615_122229_8E04AC6C X-CRM114-Status: GOOD ( 11.36 ) 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 Three mmap/munmap related test cases are added: - mmap(NULL, 0, PROT_READ, MAP_PRIVATE, 0, 0), MAP_FAILED, EINVAL) The length argument must be greater than 0, otherwise, fail with -EINVAL. - munmap((void *)-1, 4*1024), -1, EINVAL) Invalid (void *)-1 address fail with -EINVAL. - test_mmap_munmap(4*1024) It finds a init file, mmap() it and then munmap(). Signed-off-by: Zhangjin Wu --- tools/testing/selftests/nolibc/nolibc-test.c | 31 ++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c index 80ab29e2887c..f7c0ca72cb28 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -592,6 +592,34 @@ static int test_stat_timestamps(void) return 0; } +int test_mmap_munmap(int size) +{ + char init_files[5][20] = {"/init", "/sbin/init", "/etc/init", "/bin/init", "/bin/sh"}; + int ret, fd, i; + void *mem; + + for (i = 0; i < 5; i++) { + ret = fd = open(init_files[i], O_RDONLY); + if (ret < 0) + continue; + else + break; + } + if (ret < 0) + return ret; + + mem = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); + if (mem == MAP_FAILED) + return -1; + + ret = munmap(mem, size); + if (ret < 0) + return ret; + + return close(fd); +} + + /* Run syscall tests between IDs and . * Return 0 on success, non-zero on failure. */ @@ -666,6 +694,9 @@ int run_syscall(int min, int max) CASE_TEST(lseek_m1); EXPECT_SYSER(1, lseek(-1, 0, SEEK_SET), -1, EBADF); break; CASE_TEST(lseek_0); EXPECT_SYSER(1, lseek(0, 0, SEEK_SET), -1, ESPIPE); break; CASE_TEST(mkdir_root); EXPECT_SYSER(1, mkdir("/", 0755), -1, EEXIST); break; + CASE_TEST(mmap_bad); EXPECT_PTRER(1, mmap(NULL, 0, PROT_READ, MAP_PRIVATE, 0, 0), MAP_FAILED, EINVAL); break; + CASE_TEST(munmap_bad); EXPECT_SYSER(1, munmap((void *)-1, 0), -1, EINVAL); break; + CASE_TEST(mmap_good); EXPECT_SYSZR(1, test_mmap_munmap(4*1024)); break; CASE_TEST(open_tty); EXPECT_SYSNE(1, tmp = open("/dev/null", 0), -1); if (tmp != -1) close(tmp); break; CASE_TEST(open_blah); EXPECT_SYSER(1, tmp = open("/proc/self/blah", 0), -1, ENOENT); if (tmp != -1) close(tmp); break; CASE_TEST(poll_null); EXPECT_SYSZR(1, poll(NULL, 0, 0)); break;