Message ID | f054cd45de26bccb330ad842bc2b3b708b2a429d.1687957589.git.falcon@tinylab.org (mailing list archive) |
---|---|
State | New |
Headers | show |
Series | tools/nolibc: add a new syscall helper | expand |
On Wed, Jun 28, 2023 at 09:41:13PM +0800, Zhangjin Wu wrote: > 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)); > } One point regarding this one. By doing so, we're hard-coding the fact that we consider that MAP_FAILED is always -1. I'm not necessarily against it, but this implication can be confusing for those searching where it's being set. I would suggest putting a comment before the mmap() function saying: /* Note that on Linux MAP_FAILED is -1 so we can use the generic __sysret() * which returns -1 upon error and still satisfy user land that checks for * MAP_FAILED. */ Since it's an assumed choice that theoretically could affect portability, it should be reflected in the commit message as well (and we all know it does not have any impact). Thanks! Willy
> On Wed, Jun 28, 2023 at 09:41:13PM +0800, Zhangjin Wu wrote: > > 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)); > > } > > One point regarding this one. By doing so, we're hard-coding the fact > that we consider that MAP_FAILED is always -1. I'm not necessarily > against it, but this implication can be confusing for those searching > where it's being set. I would suggest putting a comment before the > mmap() function saying: > > /* Note that on Linux MAP_FAILED is -1 so we can use the generic __sysret() > * which returns -1 upon error and still satisfy user land that checks for > * MAP_FAILED. > */ > > Since it's an assumed choice that theoretically could affect portability, > it should be reflected in the commit message as well (and we all know it > does not have any impact). > Yeah, we do need such a comment and commit message note, thanks. Best regards, Zhangjin > Thanks! > Willy
diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h index b6125e600dc2..e0ac95a4bfa1 100644 --- a/tools/include/nolibc/sys.h +++ b/tools/include/nolibc/sys.h @@ -632,26 +632,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) @@ -662,20 +647,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..bed62da7877c 100644 --- a/tools/include/nolibc/types.h +++ b/tools/include/nolibc/types.h @@ -10,6 +10,7 @@ #include "std.h" #include <linux/time.h> #include <linux/stat.h> +#include <linux/mman.h> /* Only the generic macros and types may be defined here. The arch-specific @@ -81,6 +82,11 @@ #define MAXPATHLEN (PATH_MAX) #endif +/* flags for mmap */ +#ifndef MAP_FAILED +#define MAP_FAILED ((void *)-1) +#endif + /* whence values for lseek() */ #define SEEK_SET 0 #define SEEK_CUR 1
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 and reuse most of them from <linux/mman.h> - Apply the new __sysret() to convert the calling of sys_map() to oneline code Signed-off-by: Zhangjin Wu <falcon@tinylab.org> --- tools/include/nolibc/sys.h | 24 +----------------------- tools/include/nolibc/types.h | 6 ++++++ 2 files changed, 7 insertions(+), 23 deletions(-)