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: 694329 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EDA03EB64D9 for ; Mon, 19 Jun 2023 15:41:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231295AbjFSPlj (ORCPT ); Mon, 19 Jun 2023 11:41:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45308 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231130AbjFSPli (ORCPT ); Mon, 19 Jun 2023 11:41:38 -0400 Received: from bg4.exmail.qq.com (bg4.exmail.qq.com [43.154.54.12]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0BCC8113; Mon, 19 Jun 2023 08:41:36 -0700 (PDT) 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 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.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: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: 694328 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2C9DBEB64D9 for ; Mon, 19 Jun 2023 15:45:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232016AbjFSPpb (ORCPT ); Mon, 19 Jun 2023 11:45:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47986 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231845AbjFSPpS (ORCPT ); Mon, 19 Jun 2023 11:45:18 -0400 Received: from bg4.exmail.qq.com (bg4.exmail.qq.com [43.154.54.12]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0C173E60; Mon, 19 Jun 2023 08:45:07 -0700 (PDT) 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 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.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: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: 694327 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 174C9EB64D9 for ; Mon, 19 Jun 2023 15:47:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229905AbjFSPre (ORCPT ); Mon, 19 Jun 2023 11:47:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49686 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229825AbjFSPrd (ORCPT ); Mon, 19 Jun 2023 11:47:33 -0400 Received: from bg4.exmail.qq.com (bg4.exmail.qq.com [43.154.54.12]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5FB56A4; Mon, 19 Jun 2023 08:47:31 -0700 (PDT) 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 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.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: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: 694326 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 71D83EB64D9 for ; Mon, 19 Jun 2023 15:51:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229963AbjFSPvo (ORCPT ); Mon, 19 Jun 2023 11:51:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51830 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229848AbjFSPvn (ORCPT ); Mon, 19 Jun 2023 11:51:43 -0400 Received: from bg4.exmail.qq.com (bg4.exmail.qq.com [43.155.65.254]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A0AD0A4; Mon, 19 Jun 2023 08:51:41 -0700 (PDT) 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 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.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: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: 694325 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1223EEB64D9 for ; Mon, 19 Jun 2023 15:56:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230003AbjFSP4O (ORCPT ); Mon, 19 Jun 2023 11:56:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55196 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229888AbjFSP4N (ORCPT ); Mon, 19 Jun 2023 11:56:13 -0400 Received: from bg4.exmail.qq.com (bg4.exmail.qq.com [43.154.54.12]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 90FFE188; Mon, 19 Jun 2023 08:56:11 -0700 (PDT) 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 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.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;