Skip to contentSkip to conversation

nolibc: add generic part1 of prepare for rv32

PATCH Superseded Zhangjin Wu 2 participants 10 messages May 29, 2023

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover 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(-)
Signature · 1 line
2.25.1
#1 lore raw

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()")
---
 tools/testing/selftests/nolibc/nolibc-test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/nolibc/nolibc-test.c +1 -1
index 7de46305f419..d417ca5d976f 100644
@@ -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 */
Signature · 1 line
2.25.1
#2 lore raw

PATCH v2 2/7 selftests/nolibc: allow specify extra arguments for qemu

In reply to Zhangjin Wu

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" ...
---
 tools/testing/selftests/nolibc/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/nolibc/Makefile +1 -1
index 47c3c89092e4..44088535682e 100644
@@ -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.
Signature · 1 line
2.25.1
#3 lore raw

PATCH v2 3/7 selftests/nolibc: fix up compile warning with glibc on x86_64

In reply to Zhangjin Wu

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.

---
 tools/testing/selftests/nolibc/nolibc-test.c | 4 ++++
 1 file changed, 4 insertions(+)
tools/testing/selftests/nolibc/nolibc-test.c +4 -0
index d417ca5d976f..7f9b716fd9b1 100644
@@ -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;
 }
Signature · 1 line
2.25.1
maintainer
#4 lore raw

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

Quoted 34 lines · Zhangjin Wu

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.

---
 tools/testing/selftests/nolibc/nolibc-test.c | 4 ++++
 1 file changed, 4 insertions(+)
tools/testing/selftests/nolibc/nolibc-test.c +0 -0
index d417ca5d976f..7f9b716fd9b1 100644
@@ -174,7 +174,11 @@ static int expect_eq(uint64_t expr, int llen, uint64_t val)
 {

tools/testing/selftests/nolibc/nolibc-test.c static int expect_eq(uint64_t expr, int llen, uint64_t val) · line 184

Quoted 10 lines
tools/testing/selftests/nolibc/nolibc-test.c +4 -0
 	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

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#5 lore raw

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

Quoted 49 lines · Zhangjin Wu
Quoted 34 lines

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.

---
 tools/testing/selftests/nolibc/nolibc-test.c | 4 ++++
 1 file changed, 4 insertions(+)
tools/testing/selftests/nolibc/nolibc-test.c +0 -0
index d417ca5d976f..7f9b716fd9b1 100644
@@ -174,7 +174,11 @@ static int expect_eq(uint64_t expr, int llen, uint64_t val)
 {

tools/testing/selftests/nolibc/nolibc-test.c static int expect_eq(uint64_t expr, int llen, uint64_t val) · line 184

Quoted 10 lines
tools/testing/selftests/nolibc/nolibc-test.c +4 -0
 	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:

Review

Quoted 2 lines
(quoted diff) +1 -1
-  	llen += printf(" = %lld ", expr);
+  	llen += printf(" = %lld ", (long long)expr);

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

Thanks,
Zhangjin

Quoted 2 lines

Willy

#6 lore raw

PATCH v2 4/7 selftests/nolibc: not include limits.h for nolibc

In reply to Zhangjin Wu

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.

---
 tools/include/nolibc/stdint.h                | 14 ++++++++++++++
 tools/testing/selftests/nolibc/nolibc-test.c |  4 +---
 2 files changed, 15 insertions(+), 3 deletions(-)
tools/include/nolibc/stdint.h +14 -0
index c1ce4f5e0603..31a5264539ae 100644
@@ -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 */
tools/testing/selftests/nolibc/nolibc-test.c +1 -3
index 7f9b716fd9b1..e75ce6b68565 100644
@@ -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
 
Signature · 7 lines
2.25.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#7 lore raw

PATCH v2 5/7 selftests/nolibc: use INT_MAX instead of __INT_MAX__

In reply to Zhangjin Wu

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

---
 tools/testing/selftests/nolibc/nolibc-test.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/nolibc/nolibc-test.c +2 -2
index e75ce6b68565..9ff9d87cc78e 100644
@@ -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, ':');
Signature · 7 lines
2.25.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#8 lore raw

PATCH v2 6/7 tools/nolibc: arm: add missing my_syscall6

In reply to Zhangjin Wu

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

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

---
 tools/include/nolibc/arch-arm.h | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
tools/include/nolibc/arch-arm.h +23 -0
index 45b89ffe8247..ca4c66987497 100644
@@ -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));
 
Signature · 1 line
2.25.1
#9 lore raw

PATCH v2 7/7 tools/nolibc: open: fix up compile warning for arm

In reply to Zhangjin Wu

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

---
 tools/include/nolibc/sys.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/include/nolibc/sys.h +1 -1
index 1d6f33f58629..154194056962 100644
@@ -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);
 	}
 
Signature · 1 line
2.25.1