[PATCH v2 1/4] tools/nolibc: sys.h: add __syscall() and __sysret() helpers

From: Zhangjin Wu
Date: Tue Jun 06 2023 - 06:33:53 EST


> most of the library routines share the same code model, let's add two
> helpers to simplify the coding and shrink the code lines too.
>
> One added for syscall return, one added for syscall call.
>
> Thomas suggested to use inline function instead of macro for __sysret(),
> and he also helped to simplify the __syscall() a lot.
>
> Willy suggested to make __sysret() be always inline.
>
> Suggested-by: Willy Tarreau <w@xxxxxx>
> Link: https://lore.kernel.org/linux-riscv/ZH1+hkhiA2+ItSvX@xxxxxx/
> Suggested-by: Thomas Weißschuh <linux@xxxxxxxxxxxxxx>
> Link: https://lore.kernel.org/linux-riscv/ea4e7442-7223-4211-ba29-70821e907888@xxxxxxxx/
> Signed-off-by: Zhangjin Wu <falcon@xxxxxxxxxxx>
> ---
> tools/include/nolibc/sys.h | 12 ++++++++++++
> 1 file changed, 12 insertions(+)
>
> diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h
> index 5464f93e863e..c12c14db056e 100644
> --- a/tools/include/nolibc/sys.h
> +++ b/tools/include/nolibc/sys.h
> @@ -28,6 +28,18 @@
> #include "errno.h"
> #include "types.h"
>
> +/* Syscall return helper, set errno as -ret when ret < 0 */
> +static inline __attribute__((always_inline)) long __sysret(long ret)

Sorry, the run-user/run targets in tools/testing/selftests/nolibc/Makefile
complains about the above line, seems it doesn't support the 'inline' keyword
and requires '__inline__'.

Just checked my own test script and the run-user / run targets, the only
difference is it forcely uses -std=c89, do we need to align with the kernel
Makefile and use -std=gnu11 instead?

Whatever, I need to change this line to align with the other codes, use
__inline__ as we have used in tools/include/nolibc/stdlib.h:

diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h
index 0cfc5157845a..48365288a903 100644
--- a/tools/include/nolibc/sys.h
+++ b/tools/include/nolibc/sys.h
@@ -29,7 +29,8 @@
#include "types.h"

/* Syscall return helper, set errno as -ret when ret < 0 */
-static inline __attribute__((always_inline)) long __sysret(long ret)
+static __inline__ __attribute__((unused, always_inline))
+long __sysret(long ret)
{
if (ret < 0) {
SET_ERRNO(-ret);

Best regards,
Zhangjin

> +{
> + if (ret < 0) {
> + SET_ERRNO(-ret);
> + ret = -1;
> + }
> + return ret;
> +}
> +
> +/* Syscall call helper, use syscall name instead of syscall number */
> +#define __syscall(name, ...) __sysret(sys_##name(__VA_ARGS__))
>
> /* Functions in this file only describe syscalls. They're declared static so
> * that the compiler usually decides to inline them while still being allowed
> --
> 2.25.1