tools/nolibc: clean up mmap() routine
authorZhangjin Wu <falcon@tinylab.org>
Fri, 7 Jul 2023 14:58:20 +0000 (22:58 +0800)
committerWilly Tarreau <w@1wt.eu>
Wed, 23 Aug 2023 02:38:02 +0000 (04:38 +0200)
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 generic __sysret() to convert the calling of sys_map()
  to oneline code

Note, since MAP_FAILED is -1 on Linux, so we can use the generic
__sysret() which returns -1 upon error and still satisfy user land that
checks for MAP_FAILED.

Suggested-by: Willy Tarreau <w@1wt.eu>
Link: https://lore.kernel.org/lkml/20230702192347.GJ16233@1wt.eu/
Signed-off-by: Zhangjin Wu <falcon@tinylab.org>
Signed-off-by: Willy Tarreau <w@1wt.eu>
tools/include/nolibc/sys.h
tools/include/nolibc/types.h

index 3479f54..3d01a24 100644 (file)
@@ -636,26 +636,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)
@@ -666,20 +651,18 @@ 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
 
+/* 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.
+ */
+
 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))
index f96e28b..5e1bac8 100644 (file)
@@ -8,6 +8,7 @@
 #define _NOLIBC_TYPES_H
 
 #include "std.h"
+#include <linux/mman.h>
 #include <linux/time.h>
 #include <linux/stat.h>
 
 #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