2023-05-29 13:02:46

by Zhangjin Wu

[permalink] [raw]
Subject: [PATCH v2 0/7] nolibc: add generic part1 of prepare for rv32

Hi, All

Thanks very much for your review suggestions of the v1 series [1], this
is the generic part1 of the v2 revison.

* selftests/nolibc: syscall_args: use generic __NR_statx

A more generic statx is used instead of fstat

(Review suggestions from Willy, Arnd)

* selftests/nolibc: allow specify extra arguments for qemu

Besides BIOS, QEMU_ARGS_EXTRA is better for more requirements

(Review suggestions from Thomas, Willy)

* selftests/nolibc: fix up compile warning with glibc on x86_64

Definition of uint64_t differs from glibc and nolibc, use the right
print format here

* selftests/nolibc: not include limits.h for nolibc

Remove the requirement of limits.h for nolibc can let us use older
glibc for rv32

(Review suggestions from thomas)

* selftests/nolibc: use INT_MAX instead of __INT_MAX__

A trivial cleanup, based on the previous patch

* tools/nolibc: arm: add missing my_syscall6

Required by future forced pselect6/pselect6_time64, tested on arm/vexpress-a9

(Review suggestions from Arnd)

* tools/nolibc: open: fix up compile warning for arm

A trivial fixup based on compiler's suggestion and glibc code

Best regards,
Zhangjin

----
[1]: https://lore.kernel.org/linux-riscv/[email protected]/T/#t

Zhangjin Wu (7):
selftests/nolibc: syscall_args: use __NR_statx for rv32
selftests/nolibc: allow specify extra arguments for qemu
selftests/nolibc: fix up compile warning with glibc on x86_64
selftests/nolibc: not include limits.h for nolibc
selftests/nolibc: use INT_MAX instead of __INT_MAX__
tools/nolibc: arm: add missing my_syscall6
tools/nolibc: open: fix up compile warning for arm

tools/include/nolibc/arch-arm.h | 23 ++++++++++++++++++++
tools/include/nolibc/stdint.h | 14 ++++++++++++
tools/include/nolibc/sys.h | 2 +-
tools/testing/selftests/nolibc/Makefile | 2 +-
tools/testing/selftests/nolibc/nolibc-test.c | 14 +++++++-----
5 files changed, 47 insertions(+), 8 deletions(-)

--
2.25.1



2023-05-29 13:08:16

by Zhangjin Wu

[permalink] [raw]
Subject: [PATCH v2 2/7] selftests/nolibc: allow specify extra arguments for qemu

The opensbi package from Ubuntu 20.04 only provides rv64 firmwares:

$ dpkg -S opensbi | grep -E "fw_.*bin|fw_.*elf" | uniq
opensbi: /usr/lib/riscv64-linux-gnu/opensbi/generic/fw_dynamic.bin
opensbi: /usr/lib/riscv64-linux-gnu/opensbi/generic/fw_jump.bin
opensbi: /usr/lib/riscv64-linux-gnu/opensbi/generic/fw_dynamic.elf
opensbi: /usr/lib/riscv64-linux-gnu/opensbi/generic/fw_jump.elf

To run this nolibc test for rv32, users must build opensbi or download a
prebuilt one from qemu repository:

https://gitlab.com/qemu-project/qemu/-/blob/master/pc-bios/opensbi-riscv32-generic-fw_dynamic.bin

And then use -bios to tell qemu use it to avoid such failure:

$ qemu-system-riscv32 -display none -no-reboot -kernel /path/to/arch/riscv/boot/Image -serial stdio -M virt -append "console=ttyS0 panic=-1"
qemu-system-riscv32: Unable to load the RISC-V firmware "opensbi-riscv32-generic-fw_dynamic.bin"

To run from makefile, QEMU_ARGS_EXTRA is added to allow pass extra
arguments like -bios:

$ make run QEMU_ARGS_EXTRA="-bios /path/to/opensbi-riscv32-generic-fw_dynamic.bin" ...

Signed-off-by: Zhangjin Wu <[email protected]>
---
tools/testing/selftests/nolibc/Makefile | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/testing/selftests/nolibc/Makefile b/tools/testing/selftests/nolibc/Makefile
index 47c3c89092e4..44088535682e 100644
--- a/tools/testing/selftests/nolibc/Makefile
+++ b/tools/testing/selftests/nolibc/Makefile
@@ -64,7 +64,7 @@ QEMU_ARGS_mips = -M malta -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)"
QEMU_ARGS_riscv = -M virt -append "console=ttyS0 panic=-1 $(TEST:%=NOLIBC_TEST=%)"
QEMU_ARGS_s390 = -M s390-ccw-virtio -m 1G -append "console=ttyS0 panic=-1 $(TEST:%=NOLIBC_TEST=%)"
QEMU_ARGS_loongarch = -M virt -append "console=ttyS0,115200 panic=-1 $(TEST:%=NOLIBC_TEST=%)"
-QEMU_ARGS = $(QEMU_ARGS_$(ARCH))
+QEMU_ARGS = $(QEMU_ARGS_$(ARCH)) $(QEMU_ARGS_EXTRA)

# OUTPUT is only set when run from the main makefile, otherwise
# it defaults to this nolibc directory.
--
2.25.1



2023-05-29 13:08:39

by Zhangjin Wu

[permalink] [raw]
Subject: [PATCH v2 1/7] selftests/nolibc: syscall_args: use generic __NR_statx

Compiling nolibc-test.c for rv32 got such error:

tools/testing/selftests/nolibc/nolibc-test.c:599:57: error: ‘__NR_fstat’ undeclared (first use in this function)
599 | CASE_TEST(syscall_args); EXPECT_SYSER(1, syscall(__NR_fstat, 0, NULL), -1, EFAULT); break;

The generic include/uapi/asm-generic/unistd.h used by rv32 doesn't
support __NR_fstat, use the more generic __NR_statx instead:

Running test 'syscall'
69 syscall_noargs = 1 [OK]
70 syscall_args = -1 EFAULT [OK]

__NR_statx has been added from v4.10:

a528d35e8bfc ("statx: Add a system call to make enhanced file info available")

It has been supported by all of the platforms since at least from v4.20.

Fixes: 8e3ab529bef9 ("tools/nolibc/unistd: add syscall()")
Signed-off-by: Zhangjin Wu <[email protected]>
---
tools/testing/selftests/nolibc/nolibc-test.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index 7de46305f419..d417ca5d976f 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -621,7 +621,7 @@ int run_syscall(int min, int max)
CASE_TEST(write_badf); EXPECT_SYSER(1, write(-1, &tmp, 1), -1, EBADF); break;
CASE_TEST(write_zero); EXPECT_SYSZR(1, write(1, &tmp, 0)); break;
CASE_TEST(syscall_noargs); EXPECT_SYSEQ(1, syscall(__NR_getpid), getpid()); break;
- CASE_TEST(syscall_args); EXPECT_SYSER(1, syscall(__NR_fstat, 0, NULL), -1, EFAULT); break;
+ CASE_TEST(syscall_args); EXPECT_SYSER(1, syscall(__NR_statx, 0, NULL, 0, 0, NULL), -1, EFAULT); break;
case __LINE__:
return ret; /* must be last */
/* note: do not set any defaults so as to permit holes above */
--
2.25.1


2023-05-29 13:08:48

by Zhangjin Wu

[permalink] [raw]
Subject: [PATCH v2 3/7] selftests/nolibc: fix up compile warning with glibc on x86_64

Compiling nolibc-test.c with gcc on x86_64 got such warning:

tools/testing/selftests/nolibc/nolibc-test.c: In function ‘expect_eq’:
tools/testing/selftests/nolibc/nolibc-test.c:177:24: warning: format ‘%lld’ expects argument of type ‘long long int’, but argument 2 has type ‘uint64_t’ {aka ‘long unsigned int’} [-Wformat=]
177 | llen += printf(" = %lld ", expr);
| ~~~^ ~~~~
| | |
| | uint64_t {aka long unsigned int}
| long long int
| %ld

It because that glibc defines uint64_t as "unsigned long int" when word
size (means sizeof(long)) is 64bit (see include/bits/types.h), but
nolibc directly use the 64bit "unsigned long long" (see
tools/include/nolibc/stdint.h), which is simpler, seems kernel uses it
too (include/uapi/asm-generic/int-ll64.h).

It is able to do like glibc, defining __WORDSIZE for all of platforms
and using "unsigned long int" to define uint64_t when __WORDSIZE is
64bits, but here uses a simpler solution: nolibc always requires %lld to
match "unsigned long long", for others, only require %lld when word size
is 32bit.

Signed-off-by: Zhangjin Wu <[email protected]>
---
tools/testing/selftests/nolibc/nolibc-test.c | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index d417ca5d976f..7f9b716fd9b1 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -174,7 +174,11 @@ static int expect_eq(uint64_t expr, int llen, uint64_t val)
{
int ret = !(expr == val);

+#if __SIZEOF_LONG__ == 4 || defined(NOLIBC)
llen += printf(" = %lld ", expr);
+#else
+ llen += printf(" = %ld ", expr);
+#endif
pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
return ret;
}
--
2.25.1


2023-05-29 13:14:56

by Zhangjin Wu

[permalink] [raw]
Subject: [PATCH v2 4/7] selftests/nolibc: not include limits.h for nolibc

When compile nolibc-test.c with 2.31 glibc, we got such error:

In file included from /usr/riscv64-linux-gnu/include/sys/cdefs.h:452,
from /usr/riscv64-linux-gnu/include/features.h:461,
from /usr/riscv64-linux-gnu/include/bits/libc-header-start.h:33,
from /usr/riscv64-linux-gnu/include/limits.h:26,
from /usr/lib/gcc-cross/riscv64-linux-gnu/9/include/limits.h:194,
from /usr/lib/gcc-cross/riscv64-linux-gnu/9/include/syslimits.h:7,
from /usr/lib/gcc-cross/riscv64-linux-gnu/9/include/limits.h:34,
from /labs/linux-lab/src/linux-stable/tools/testing/selftests/nolibc/nolibc-test.c:6:
/usr/riscv64-linux-gnu/include/bits/wordsize.h:28:3: error: #error "rv32i-based targets are not supported"
28 | # error "rv32i-based targets are not supported"

Glibc (>= 2.33) commit 5b6113d62efa ("RISC-V: Support the 32-bit ABI
implementation") fixed up above error.

As suggested by Thomas, defining INT_MIN/INT_MAX for nolibc can remove
the including of limits.h, and therefore no above error. of course, the
other libcs still require limits.h, move it to the right place.

The LONG_MIN/LONG_MAX are also defined too.

Suggested-by: Thomas Weißschuh <[email protected]>
Link: https://lore.kernel.org/linux-riscv/[email protected]/T/#
Signed-off-by: Zhangjin Wu <[email protected]>
---
tools/include/nolibc/stdint.h | 14 ++++++++++++++
tools/testing/selftests/nolibc/nolibc-test.c | 4 +---
2 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/tools/include/nolibc/stdint.h b/tools/include/nolibc/stdint.h
index c1ce4f5e0603..31a5264539ae 100644
--- a/tools/include/nolibc/stdint.h
+++ b/tools/include/nolibc/stdint.h
@@ -96,4 +96,18 @@ typedef uint64_t uintmax_t;
#define UINT_FAST32_MAX SIZE_MAX
#define UINT_FAST64_MAX SIZE_MAX

+#ifndef INT_MIN
+#define INT_MIN (-__INT_MAX__ - 1)
+#endif
+#ifndef INT_MAX
+#define INT_MAX __INT_MAX__
+#endif
+
+#ifndef LONG_MIN
+#define LONG_MIN (-__LONG_MAX__ - 1)
+#endif
+#ifndef LONG_MAX
+#define LONG_MAX __LONG_MAX__
+#endif
+
#endif /* _NOLIBC_STDINT_H */
diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index 7f9b716fd9b1..e75ce6b68565 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -2,9 +2,6 @@

#define _GNU_SOURCE

-/* platform-specific include files coming from the compiler */
-#include <limits.h>
-
/* libc-specific include files
* The program may be built in 3 ways:
* $(CC) -nostdlib -include /path/to/nolibc.h => NOLIBC already defined
@@ -39,6 +36,7 @@
#include <stddef.h>
#include <stdint.h>
#include <unistd.h>
+#include <limits.h>
#endif
#endif

--
2.25.1


2023-05-29 13:14:59

by Zhangjin Wu

[permalink] [raw]
Subject: [PATCH v2 7/7] tools/nolibc: open: fix up compile warning for arm

In function ‘open’:
nolibc/sysroot/arm/include/sys.h:919:23: warning: ‘mode_t’ {aka ‘short unsigned int’} is promoted to ‘int’ when passed through ‘...’
919 | mode = va_arg(args, mode_t);
| ^
nolibc/sysroot/arm/include/sys.h:919:23: note: (so you should pass ‘int’ not ‘mode_t’ {aka ‘short unsigned int’} to ‘va_arg’)
nolibc/sysroot/arm/include/sys.h:919:23: note: if this code is reached, the program will abort

Signed-off-by: Zhangjin Wu <[email protected]>
---
tools/include/nolibc/sys.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h
index 1d6f33f58629..154194056962 100644
--- a/tools/include/nolibc/sys.h
+++ b/tools/include/nolibc/sys.h
@@ -862,7 +862,7 @@ int open(const char *path, int flags, ...)
va_list args;

va_start(args, flags);
- mode = va_arg(args, mode_t);
+ mode = va_arg(args, int);
va_end(args);
}

--
2.25.1


2023-05-29 13:15:04

by Zhangjin Wu

[permalink] [raw]
Subject: [PATCH v2 6/7] tools/nolibc: arm: add missing my_syscall6

This is required by the coming removal of the oldselect and newselect
support.

pselect6/pselect6_time64 will be used unconditionally, they have 6
arguments.

Suggested-by: Arnd Bergmann <[email protected]>
Link: https://lore.kernel.org/linux-riscv/[email protected]/T/#t
Signed-off-by: Zhangjin Wu <[email protected]>
---
tools/include/nolibc/arch-arm.h | 23 +++++++++++++++++++++++
1 file changed, 23 insertions(+)

diff --git a/tools/include/nolibc/arch-arm.h b/tools/include/nolibc/arch-arm.h
index 45b89ffe8247..ca4c66987497 100644
--- a/tools/include/nolibc/arch-arm.h
+++ b/tools/include/nolibc/arch-arm.h
@@ -198,6 +198,29 @@ struct sys_stat_struct {
_arg1; \
})

+#define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \
+({ \
+ register long _num __asm__(_NOLIBC_SYSCALL_REG) = (num); \
+ register long _arg1 __asm__ ("r0") = (long)(arg1); \
+ register long _arg2 __asm__ ("r1") = (long)(arg2); \
+ register long _arg3 __asm__ ("r2") = (long)(arg3); \
+ register long _arg4 __asm__ ("r3") = (long)(arg4); \
+ register long _arg5 __asm__ ("r4") = (long)(arg5); \
+ register long _arg6 __asm__ ("r5") = (long)(arg6); \
+ \
+ __asm__ volatile ( \
+ _NOLIBC_THUMB_SET_R7 \
+ "svc #0\n" \
+ _NOLIBC_THUMB_RESTORE_R7 \
+ : "=r"(_arg1), "=r" (_num) \
+ : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \
+ "r"(_arg6), "r"(_num) \
+ : "memory", "cc", "lr" \
+ ); \
+ _arg1; \
+})
+
+
char **environ __attribute__((weak));
const unsigned long *_auxv __attribute__((weak));

--
2.25.1


2023-05-29 13:15:51

by Zhangjin Wu

[permalink] [raw]
Subject: [PATCH v2 5/7] selftests/nolibc: use INT_MAX instead of __INT_MAX__

nolibc now has INT_MAX in stdint.h, so, don't mix INT_MAX and
__INT_MAX__, unify them to INT_MAX.

Signed-off-by: Zhangjin Wu <[email protected]>
---
tools/testing/selftests/nolibc/nolibc-test.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index e75ce6b68565..9ff9d87cc78e 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -935,7 +935,7 @@ static const struct test test_names[] = {
int main(int argc, char **argv, char **envp)
{
int min = 0;
- int max = __INT_MAX__;
+ int max = INT_MAX;
int ret = 0;
int err;
int idx;
@@ -983,7 +983,7 @@ int main(int argc, char **argv, char **envp)
* here, which defaults to the full range.
*/
do {
- min = 0; max = __INT_MAX__;
+ min = 0; max = INT_MAX;
value = colon;
if (value && *value) {
colon = strchr(value, ':');
--
2.25.1


2023-05-29 13:15:56

by Willy Tarreau

[permalink] [raw]
Subject: Re: [PATCH v2 3/7] selftests/nolibc: fix up compile warning with glibc on x86_64

On Mon, May 29, 2023 at 09:00:01PM +0800, Zhangjin Wu wrote:
> Compiling nolibc-test.c with gcc on x86_64 got such warning:
>
> tools/testing/selftests/nolibc/nolibc-test.c: In function 'expect_eq':
> tools/testing/selftests/nolibc/nolibc-test.c:177:24: warning: format '%lld' expects argument of type 'long long int', but argument 2 has type 'uint64_t' {aka 'long unsigned int'} [-Wformat=]
> 177 | llen += printf(" = %lld ", expr);
> | ~~~^ ~~~~
> | | |
> | | uint64_t {aka long unsigned int}
> | long long int
> | %ld
>
> It because that glibc defines uint64_t as "unsigned long int" when word
> size (means sizeof(long)) is 64bit (see include/bits/types.h), but
> nolibc directly use the 64bit "unsigned long long" (see
> tools/include/nolibc/stdint.h), which is simpler, seems kernel uses it
> too (include/uapi/asm-generic/int-ll64.h).
>
> It is able to do like glibc, defining __WORDSIZE for all of platforms
> and using "unsigned long int" to define uint64_t when __WORDSIZE is
> 64bits, but here uses a simpler solution: nolibc always requires %lld to
> match "unsigned long long", for others, only require %lld when word size
> is 32bit.
>
> Signed-off-by: Zhangjin Wu <[email protected]>
> ---
> tools/testing/selftests/nolibc/nolibc-test.c | 4 ++++
> 1 file changed, 4 insertions(+)
>
> diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
> index d417ca5d976f..7f9b716fd9b1 100644
> --- a/tools/testing/selftests/nolibc/nolibc-test.c
> +++ b/tools/testing/selftests/nolibc/nolibc-test.c
> @@ -174,7 +174,11 @@ static int expect_eq(uint64_t expr, int llen, uint64_t val)
> {
> int ret = !(expr == val);
>
> +#if __SIZEOF_LONG__ == 4 || defined(NOLIBC)
> llen += printf(" = %lld ", expr);
> +#else
> + llen += printf(" = %ld ", expr);
> +#endif
> pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
> return ret;
> }

Please don't proceed like this. There's much easier to do here for a printf,
just cast the expression to the type printf expects:

- llen += printf(" = %lld ", expr);
+ llen += printf(" = %lld ", (long long)expr);

Willy

2023-05-29 13:18:47

by Zhangjin Wu

[permalink] [raw]
Subject: Re: [PATCH v2 3/7] selftests/nolibc: fix up compile warning with glibc on x86_64

> On Mon, May 29, 2023 at 09:00:01PM +0800, Zhangjin Wu wrote:
> > Compiling nolibc-test.c with gcc on x86_64 got such warning:
> >
> > tools/testing/selftests/nolibc/nolibc-test.c: In function 'expect_eq':
> > tools/testing/selftests/nolibc/nolibc-test.c:177:24: warning: format '%lld' expects argument of type 'long long int', but argument 2 has type 'uint64_t' {aka 'long unsigned int'} [-Wformat=]
> > 177 | llen += printf(" = %lld ", expr);
> > | ~~~^ ~~~~
> > | | |
> > | | uint64_t {aka long unsigned int}
> > | long long int
> > | %ld
> >
> > It because that glibc defines uint64_t as "unsigned long int" when word
> > size (means sizeof(long)) is 64bit (see include/bits/types.h), but
> > nolibc directly use the 64bit "unsigned long long" (see
> > tools/include/nolibc/stdint.h), which is simpler, seems kernel uses it
> > too (include/uapi/asm-generic/int-ll64.h).
> >
> > It is able to do like glibc, defining __WORDSIZE for all of platforms
> > and using "unsigned long int" to define uint64_t when __WORDSIZE is
> > 64bits, but here uses a simpler solution: nolibc always requires %lld to
> > match "unsigned long long", for others, only require %lld when word size
> > is 32bit.
> >
> > Signed-off-by: Zhangjin Wu <[email protected]>
> > ---
> > tools/testing/selftests/nolibc/nolibc-test.c | 4 ++++
> > 1 file changed, 4 insertions(+)
> >
> > diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
> > index d417ca5d976f..7f9b716fd9b1 100644
> > --- a/tools/testing/selftests/nolibc/nolibc-test.c
> > +++ b/tools/testing/selftests/nolibc/nolibc-test.c
> > @@ -174,7 +174,11 @@ static int expect_eq(uint64_t expr, int llen, uint64_t val)
> > {
> > int ret = !(expr == val);
> >
> > +#if __SIZEOF_LONG__ == 4 || defined(NOLIBC)
> > llen += printf(" = %lld ", expr);
> > +#else
> > + llen += printf(" = %ld ", expr);
> > +#endif
> > pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
> > return ret;
> > }
>
> Please don't proceed like this. There's much easier to do here for a printf,
> just cast the expression to the type printf expects:
>
> - llen += printf(" = %lld ", expr);
> + llen += printf(" = %lld ", (long long)expr);

Yes, this conversion is better, my method make things more complex ;-)

Thanks,
Zhangjin

>
> Willy