2023-07-12 09:35:33

by Zhangjin Wu

[permalink] [raw]
Subject: [PATCH v3 00/11] tools/nolibc: shrink arch support

Hi, Willy, Thomas

Here is the revision of the v2 arch support shrink patchset [1], it
mainly applies suggestions from you.

It is based on the 20230710-nolibc-ser2-tom-syscall-configv4-report
branch of nolibc repo.

Tested for all of the supported archs:

arch/board | result
------------|------------
arm/versatilepb | 151 test(s): 144 passed, 7 skipped, 0 failed => status: warning.
arm/vexpress-a9 | 151 test(s): 144 passed, 7 skipped, 0 failed => status: warning.
arm/virt | 151 test(s): 144 passed, 7 skipped, 0 failed => status: warning.
aarch64/virt | 151 test(s): 144 passed, 7 skipped, 0 failed => status: warning.
i386/pc | 151 test(s): 144 passed, 7 skipped, 0 failed => status: warning.
x86_64/pc | 151 test(s): 144 passed, 7 skipped, 0 failed => status: warning.
mipsel/malta | 151 test(s): 144 passed, 7 skipped, 0 failed => status: warning.
loongarch64/virt | 151 test(s): 144 passed, 7 skipped, 0 failed => status: warning.
riscv64/virt | 151 test(s): 144 passed, 7 skipped, 0 failed => status: warning.
riscv32/virt | 151 test(s): 122 passed, 7 skipped, 22 failed => status: failure.
s390x/s390-ccw-virtio | 151 test(s): 144 passed, 7 skipped, 0 failed => status: warning.

Changes from v2 --> v3:

* tools/nolibc: remove the old sys_stat support

Revert the reorg operation, basically the same as v1

* tools/nolibc: add new crt.h with _start_c

Revert the reorg operation
Add crt.h in Makefile
Add _nolibc_main alias for main to silence the compile warning about

* tools/nolibc: arm: shrink _start with _start_c
tools/nolibc: aarch64: shrink _start with _start_c
tools/nolibc: i386: shrink _start with _start_c
tools/nolibc: x86_64: shrink _start with _start_c
tools/nolibc: mips: shrink _start with _start_c
tools/nolibc: loongarch: shrink _start with _start_c
tools/nolibc: riscv: shrink _start with _start_c
tools/nolibc: s390: shrink _start with _start_c

Revert the reorg operation, but still use post-whitespaces instead of post-tab.
Include "crt.h" in arch-<ARCH>.h

* tools/nolibc: arch-*.h: add missing space after ','

Fix up the errors reported by scripts/checkpatch.pl

Best regards,
Zhangjin
---
[1]: https://lore.kernel.org/lkml/[email protected]/

Zhangjin Wu (11):
tools/nolibc: remove the old sys_stat support
tools/nolibc: add new crt.h with _start_c
tools/nolibc: arm: shrink _start with _start_c
tools/nolibc: aarch64: shrink _start with _start_c
tools/nolibc: i386: shrink _start with _start_c
tools/nolibc: x86_64: shrink _start with _start_c
tools/nolibc: mips: shrink _start with _start_c
tools/nolibc: loongarch: shrink _start with _start_c
tools/nolibc: riscv: shrink _start with _start_c
tools/nolibc: s390: shrink _start with _start_c
tools/nolibc: arch-*.h: add missing space after ','

tools/include/nolibc/Makefile | 1 +
tools/include/nolibc/arch-aarch64.h | 56 ++----------------
tools/include/nolibc/arch-arm.h | 82 +++------------------------
tools/include/nolibc/arch-i386.h | 61 +++-----------------
tools/include/nolibc/arch-loongarch.h | 45 ++-------------
tools/include/nolibc/arch-mips.h | 76 ++++---------------------
tools/include/nolibc/arch-riscv.h | 68 +++-------------------
tools/include/nolibc/arch-s390.h | 63 ++------------------
tools/include/nolibc/arch-x86_64.h | 57 +++----------------
tools/include/nolibc/crt.h | 59 +++++++++++++++++++
tools/include/nolibc/sys.h | 63 ++++----------------
tools/include/nolibc/types.h | 4 +-
12 files changed, 133 insertions(+), 502 deletions(-)
create mode 100644 tools/include/nolibc/crt.h

--
2.25.1



2023-07-12 09:36:14

by Zhangjin Wu

[permalink] [raw]
Subject: [PATCH v3 03/11] tools/nolibc: arm: shrink _start with _start_c

move most of the _start operations to _start_c().

Signed-off-by: Zhangjin Wu <[email protected]>
---
tools/include/nolibc/arch-arm.h | 43 +++++----------------------------
1 file changed, 6 insertions(+), 37 deletions(-)

diff --git a/tools/include/nolibc/arch-arm.h b/tools/include/nolibc/arch-arm.h
index ea723596ed23..74773ddcf4ca 100644
--- a/tools/include/nolibc/arch-arm.h
+++ b/tools/include/nolibc/arch-arm.h
@@ -8,6 +8,7 @@
#define _NOLIBC_ARCH_ARM_H

#include "compiler.h"
+#include "crt.h"

/* Syscalls for ARM in ARM or Thumb modes :
* - registers are 32-bit
@@ -183,49 +184,17 @@
_arg1; \
})

-
-char **environ __attribute__((weak));
-const unsigned long *_auxv __attribute__((weak));
-
/* startup code */
void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __no_stack_protector _start(void)
{
__asm__ volatile (
#ifdef _NOLIBC_STACKPROTECTOR
- "bl __stack_chk_init\n" /* initialize stack protector */
+ "bl __stack_chk_init\n" /* initialize stack protector */
#endif
- "pop {%r0}\n" /* argc was in the stack */
- "mov %r1, %sp\n" /* argv = sp */
-
- "add %r2, %r0, $1\n" /* envp = (argc + 1) ... */
- "lsl %r2, %r2, $2\n" /* * 4 ... */
- "add %r2, %r2, %r1\n" /* + argv */
- "ldr %r3, 1f\n" /* r3 = &environ (see below) */
- "str %r2, [r3]\n" /* store envp into environ */
-
- "mov r4, r2\n" /* search for auxv (follows NULL after last env) */
- "0:\n"
- "mov r5, r4\n" /* r5 = r4 */
- "add r4, r4, #4\n" /* r4 += 4 */
- "ldr r5,[r5]\n" /* r5 = *r5 = *(r4-4) */
- "cmp r5, #0\n" /* and stop at NULL after last env */
- "bne 0b\n"
- "ldr %r3, 2f\n" /* r3 = &_auxv (low bits) */
- "str r4, [r3]\n" /* store r4 into _auxv */
-
- "mov %r3, $8\n" /* AAPCS : sp must be 8-byte aligned in the */
- "neg %r3, %r3\n" /* callee, and bl doesn't push (lr=pc) */
- "and %r3, %r3, %r1\n" /* so we do sp = r1(=sp) & r3(=-8); */
- "mov %sp, %r3\n"
-
- "bl main\n" /* main() returns the status code, we'll exit with it. */
- "movs r7, $1\n" /* NR_exit == 1 */
- "svc $0x00\n"
- ".align 2\n" /* below are the pointers to a few variables */
- "1:\n"
- ".word environ\n"
- "2:\n"
- ".word _auxv\n"
+ "mov %r0, sp\n" /* save stack pointer to %r0, as arg1 of _start_c */
+ "and ip, %r0, #-8\n" /* sp must be 8-byte aligned in the callee */
+ "mov sp, ip\n"
+ "bl _start_c\n" /* transfer to c runtime */
);
__builtin_unreachable();
}
--
2.25.1


2023-07-12 09:36:15

by Zhangjin Wu

[permalink] [raw]
Subject: [PATCH v3 08/11] tools/nolibc: loongarch: shrink _start with _start_c

move most of the _start operations to _start_c().

Signed-off-by: Zhangjin Wu <[email protected]>
---
tools/include/nolibc/arch-loongarch.h | 43 ++++-----------------------
1 file changed, 5 insertions(+), 38 deletions(-)

diff --git a/tools/include/nolibc/arch-loongarch.h b/tools/include/nolibc/arch-loongarch.h
index 8aa7724fe38e..9db70d6f2c31 100644
--- a/tools/include/nolibc/arch-loongarch.h
+++ b/tools/include/nolibc/arch-loongarch.h
@@ -8,6 +8,7 @@
#define _NOLIBC_ARCH_LOONGARCH_H

#include "compiler.h"
+#include "crt.h"

/* Syscalls for LoongArch :
* - stack is 16-byte aligned
@@ -143,26 +144,9 @@
_arg1; \
})

-char **environ __attribute__((weak));
-const unsigned long *_auxv __attribute__((weak));
-
#if __loongarch_grlen == 32
-#define LONGLOG "2"
-#define SZREG "4"
-#define REG_L "ld.w"
-#define LONG_S "st.w"
-#define LONG_ADD "add.w"
-#define LONG_ADDI "addi.w"
-#define LONG_SLL "slli.w"
#define LONG_BSTRINS "bstrins.w"
#else /* __loongarch_grlen == 64 */
-#define LONGLOG "3"
-#define SZREG "8"
-#define REG_L "ld.d"
-#define LONG_S "st.d"
-#define LONG_ADD "add.d"
-#define LONG_ADDI "addi.d"
-#define LONG_SLL "slli.d"
#define LONG_BSTRINS "bstrins.d"
#endif

@@ -171,28 +155,11 @@ void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __no_stack_pr
{
__asm__ volatile (
#ifdef _NOLIBC_STACKPROTECTOR
- "bl __stack_chk_init\n" /* initialize stack protector */
+ "bl __stack_chk_init\n" /* initialize stack protector */
#endif
- REG_L " $a0, $sp, 0\n" /* argc (a0) was in the stack */
- LONG_ADDI " $a1, $sp, "SZREG"\n" /* argv (a1) = sp + SZREG */
- LONG_SLL " $a2, $a0, "LONGLOG"\n" /* envp (a2) = SZREG*argc ... */
- LONG_ADDI " $a2, $a2, "SZREG"\n" /* + SZREG (skip null) */
- LONG_ADD " $a2, $a2, $a1\n" /* + argv */
-
- "move $a3, $a2\n" /* iterate a3 over envp to find auxv (after NULL) */
- "0:\n" /* do { */
- REG_L " $a4, $a3, 0\n" /* a4 = *a3; */
- LONG_ADDI " $a3, $a3, "SZREG"\n" /* a3 += sizeof(void*); */
- "bne $a4, $zero, 0b\n" /* } while (a4); */
- "la.pcrel $a4, _auxv\n" /* a4 = &_auxv */
- LONG_S " $a3, $a4, 0\n" /* store a3 into _auxv */
-
- "la.pcrel $a3, environ\n" /* a3 = &environ */
- LONG_S " $a2, $a3, 0\n" /* store envp(a2) into environ */
- LONG_BSTRINS " $sp, $zero, 3, 0\n" /* sp must be 16-byte aligned */
- "bl main\n" /* main() returns the status code, we'll exit with it. */
- "li.w $a7, 93\n" /* NR_exit == 93 */
- "syscall 0\n"
+ "move $a0, $sp\n" /* save stack pointer to $a0, as arg1 of _start_c */
+ LONG_BSTRINS " $sp, $zero, 3, 0\n" /* $sp must be 16-byte aligned */
+ "bl _start_c\n" /* transfer to c runtime */
);
__builtin_unreachable();
}
--
2.25.1



2023-07-12 09:59:44

by Zhangjin Wu

[permalink] [raw]
Subject: [PATCH v3 11/11] tools/nolibc: arch-*.h: add missing space after ','

Fix up such errors reported by scripts/checkpatch.pl:

ERROR: space required after that ',' (ctx:VxV)
#148: FILE: tools/include/nolibc/arch-aarch64.h:148:
+void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __no_stack_protector _start(void)
^

ERROR: space required after that ',' (ctx:VxV)
#148: FILE: tools/include/nolibc/arch-aarch64.h:148:
+void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __no_stack_protector _start(void)
^

Signed-off-by: Zhangjin Wu <[email protected]>
---
tools/include/nolibc/arch-aarch64.h | 2 +-
tools/include/nolibc/arch-arm.h | 2 +-
tools/include/nolibc/arch-i386.h | 2 +-
tools/include/nolibc/arch-loongarch.h | 2 +-
tools/include/nolibc/arch-mips.h | 2 +-
tools/include/nolibc/arch-riscv.h | 2 +-
tools/include/nolibc/arch-s390.h | 2 +-
tools/include/nolibc/arch-x86_64.h | 2 +-
8 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/tools/include/nolibc/arch-aarch64.h b/tools/include/nolibc/arch-aarch64.h
index e52fa5a20d71..c94fdca9ace6 100644
--- a/tools/include/nolibc/arch-aarch64.h
+++ b/tools/include/nolibc/arch-aarch64.h
@@ -145,7 +145,7 @@
})

/* startup code */
-void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __no_stack_protector _start(void)
+void __attribute__((weak, noreturn, optimize("omit-frame-pointer"))) __no_stack_protector _start(void)
{
__asm__ volatile (
#ifdef _NOLIBC_STACKPROTECTOR
diff --git a/tools/include/nolibc/arch-arm.h b/tools/include/nolibc/arch-arm.h
index 74773ddcf4ca..5f8bfc24e9c7 100644
--- a/tools/include/nolibc/arch-arm.h
+++ b/tools/include/nolibc/arch-arm.h
@@ -185,7 +185,7 @@
})

/* startup code */
-void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __no_stack_protector _start(void)
+void __attribute__((weak, noreturn, optimize("omit-frame-pointer"))) __no_stack_protector _start(void)
{
__asm__ volatile (
#ifdef _NOLIBC_STACKPROTECTOR
diff --git a/tools/include/nolibc/arch-i386.h b/tools/include/nolibc/arch-i386.h
index f0d0f5c364b8..915f0e77629e 100644
--- a/tools/include/nolibc/arch-i386.h
+++ b/tools/include/nolibc/arch-i386.h
@@ -162,7 +162,7 @@
* 2) The deepest stack frame should be set to zero
*
*/
-void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __no_stack_protector _start(void)
+void __attribute__((weak, noreturn, optimize("omit-frame-pointer"))) __no_stack_protector _start(void)
{
__asm__ volatile (
#ifdef _NOLIBC_STACKPROTECTOR
diff --git a/tools/include/nolibc/arch-loongarch.h b/tools/include/nolibc/arch-loongarch.h
index 9db70d6f2c31..6edec94538e0 100644
--- a/tools/include/nolibc/arch-loongarch.h
+++ b/tools/include/nolibc/arch-loongarch.h
@@ -151,7 +151,7 @@
#endif

/* startup code */
-void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __no_stack_protector _start(void)
+void __attribute__((weak, noreturn, optimize("omit-frame-pointer"))) __no_stack_protector _start(void)
{
__asm__ volatile (
#ifdef _NOLIBC_STACKPROTECTOR
diff --git a/tools/include/nolibc/arch-mips.h b/tools/include/nolibc/arch-mips.h
index b1c070c5b24a..36d4bf0c6aaa 100644
--- a/tools/include/nolibc/arch-mips.h
+++ b/tools/include/nolibc/arch-mips.h
@@ -175,7 +175,7 @@
})

/* startup code, note that it's called __start on MIPS */
-void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __no_stack_protector __start(void)
+void __attribute__((weak, noreturn, optimize("omit-frame-pointer"))) __no_stack_protector __start(void)
{
__asm__ volatile (
".set push\n"
diff --git a/tools/include/nolibc/arch-riscv.h b/tools/include/nolibc/arch-riscv.h
index 2e3fcf925ae9..043e2fd85ab0 100644
--- a/tools/include/nolibc/arch-riscv.h
+++ b/tools/include/nolibc/arch-riscv.h
@@ -143,7 +143,7 @@
})

/* startup code */
-void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __no_stack_protector _start(void)
+void __attribute__((weak, noreturn, optimize("omit-frame-pointer"))) __no_stack_protector _start(void)
{
__asm__ volatile (
".option push\n"
diff --git a/tools/include/nolibc/arch-s390.h b/tools/include/nolibc/arch-s390.h
index 051f3f4ed19b..705576d8fd15 100644
--- a/tools/include/nolibc/arch-s390.h
+++ b/tools/include/nolibc/arch-s390.h
@@ -139,7 +139,7 @@
})

/* startup code */
-void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __no_stack_protector _start(void)
+void __attribute__((weak, noreturn, optimize("omit-frame-pointer"))) __no_stack_protector _start(void)
{
__asm__ volatile (
"lgr %r2, %r15\n" /* save stack pointer to %r2, as arg1 of _start_c */
diff --git a/tools/include/nolibc/arch-x86_64.h b/tools/include/nolibc/arch-x86_64.h
index 0048adf6b11f..f5614a67f05a 100644
--- a/tools/include/nolibc/arch-x86_64.h
+++ b/tools/include/nolibc/arch-x86_64.h
@@ -161,7 +161,7 @@
* 2) The deepest stack frame should be zero (the %rbp).
*
*/
-void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __no_stack_protector _start(void)
+void __attribute__((weak, noreturn, optimize("omit-frame-pointer"))) __no_stack_protector _start(void)
{
__asm__ volatile (
#ifdef _NOLIBC_STACKPROTECTOR
--
2.25.1


2023-07-15 10:31:26

by Willy Tarreau

[permalink] [raw]
Subject: Re: [PATCH v3 00/11] tools/nolibc: shrink arch support

On Wed, Jul 12, 2023 at 05:15:28PM +0800, Zhangjin Wu wrote:
> Hi, Willy, Thomas
>
> Here is the revision of the v2 arch support shrink patchset [1], it
> mainly applies suggestions from you.
>
> It is based on the 20230710-nolibc-ser2-tom-syscall-configv4-report
> branch of nolibc repo.
>
> Tested for all of the supported archs:
>
> arch/board | result
> ------------|------------
> arm/versatilepb | 151 test(s): 144 passed, 7 skipped, 0 failed => status: warning.
> arm/vexpress-a9 | 151 test(s): 144 passed, 7 skipped, 0 failed => status: warning.
> arm/virt | 151 test(s): 144 passed, 7 skipped, 0 failed => status: warning.
> aarch64/virt | 151 test(s): 144 passed, 7 skipped, 0 failed => status: warning.
> i386/pc | 151 test(s): 144 passed, 7 skipped, 0 failed => status: warning.
> x86_64/pc | 151 test(s): 144 passed, 7 skipped, 0 failed => status: warning.
> mipsel/malta | 151 test(s): 144 passed, 7 skipped, 0 failed => status: warning.
> loongarch64/virt | 151 test(s): 144 passed, 7 skipped, 0 failed => status: warning.
> riscv64/virt | 151 test(s): 144 passed, 7 skipped, 0 failed => status: warning.
> riscv32/virt | 151 test(s): 122 passed, 7 skipped, 22 failed => status: failure.
> s390x/s390-ccw-virtio | 151 test(s): 144 passed, 7 skipped, 0 failed => status: warning.

Thanks for all this work. So I've checked a few random archs and trust
your tests above to confirm they're correct ;-) I'd just like to get
your confirmation regarding statx() support in 4.14 and 4.19, and likely
adjust _start_c() according to the last few tests. Also please do prepend
to the list the patch that adds the optimize("-Os") to fix _start, and we
should be good.

Thanks!
Willy