diff mbox series

[v4,07/10] tools/nolibc: clean up mmap() support

Message ID b1162bc16ce5c397e99925e49317756c110e6f1a.1687187451.git.falcon@tinylab.org (mailing list archive)
State New
Headers show
Series tools/nolibc: add a new syscall helper | expand

Commit Message

Zhangjin Wu June 19, 2023, 3:51 p.m. UTC
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 <falcon@tinylab.org>
---
 tools/include/nolibc/sys.h   | 24 +-----------------------
 tools/include/nolibc/types.h | 11 +++++++++++
 2 files changed, 12 insertions(+), 23 deletions(-)

Comments

Thomas Weißschuh June 21, 2023, 6:48 p.m. UTC | #1
On 2023-06-19 23:51:20+0800, Zhangjin Wu wrote:
> 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 <falcon@tinylab.org>
> ---
>  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)

This could return a plain integer type instead to save some casts.
Not sure if API compatibility is guaranteed for the raw sys_ functions.

>  {
> -#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
> -- 
> 2.25.1
>
Zhangjin Wu June 22, 2023, 7:08 p.m. UTC | #2
Hi, Thomas

> On 2023-06-19 23:51:20+0800, Zhangjin Wu wrote:
> > 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 <falcon@tinylab.org>
> > ---
> >  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)
> 
> This could return a plain integer type instead to save some casts.
> Not sure if API compatibility is guaranteed for the raw sys_ functions.
>

Seems musl simply not provide the sys_xxx() functions, but let the library
routines call __syscall() directly. If we can treat these sys_xxx() as internal
functions, perhaps we can simply let the return type of sys_xxx() as 'long' to.

    $ grep "^void \*sys_" -ur tools/include/nolibc/sys.h 
    void *sys_brk(void *addr)
    void *sys_mmap(void *addr, size_t length, int prot, int flags, int fd,

Thanks,
Zhangjin


> >  {
> > -#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
> > -- 
> > 2.25.1
> >
diff mbox series

Patch

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