Skip to contentSkip to conversation

selftest/powerpc/benchmark: remove requirement libc-dev

PATCH Superseded Madhavan Srinivasan 3 participants 6 messages Aug 5, 2024 – Aug 12, 2024

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Madhavan Srinivasan

author maintainer
#0 lore raw

PATCH 1/1 selftest/powerpc/benchmark: remove requirement libc-dev

Currently exec-target.c file is linked as static and this post a requirement to install libc dev package to build. Without it, build-breaks when compiling selftest/powerpc/benchmark.

CC exec_target
/usr/bin/ld: cannot find -lc: No such file or directory
collect2: error: ld returned 1 exit status

exec_target.c is using "syscall" library function which could be replaced with a inline assembly and the same is proposed as a fix here.

---
 tools/testing/selftests/powerpc/benchmarks/Makefile    |  2 +-
 .../testing/selftests/powerpc/benchmarks/exec_target.c | 10 ++++++++--
 2 files changed, 9 insertions(+), 3 deletions(-)
tools/testing/selftests/powerpc/benchmarks/Makefile +1 -1
index 1321922038d0..ca4483c238b9 100644
@@ -18,4 +18,4 @@ $(OUTPUT)/context_switch: LDLIBS += -lpthread
 
 $(OUTPUT)/fork: LDLIBS += -lpthread
 
-$(OUTPUT)/exec_target: CFLAGS += -static -nostartfiles
+$(OUTPUT)/exec_target: CFLAGS += -nostartfiles
tools/testing/selftests/powerpc/benchmarks/exec_target.c +8 -2
index c14b0fc1edde..20027a23b594 100644
@@ -7,10 +7,16 @@
  */
 
 #define _GNU_SOURCE
-#include <unistd.h>
 #include <sys/syscall.h>
 
 void _start(void)
 {
-	syscall(SYS_exit, 0);
+	asm volatile (
+		"li %%r0, %[sys_exit];"
+		"li %%r3, 0;"
+		"sc;"
+		:
+		: [sys_exit] "i" (SYS_exit)
+		: "r0", "r3"
+	);
 }
Signature · 1 line
2.45.2
#1 lore raw

Le 05/08/2024 à 10:30, Madhavan Srinivasan a écrit :

Quoted 41 lines

Currently exec-target.c file is linked as static and this post a requirement to install libc dev package to build. Without it, build-breaks when compiling selftest/powerpc/benchmark.

CC exec_target
/usr/bin/ld: cannot find -lc: No such file or directory
collect2: error: ld returned 1 exit status

exec_target.c is using "syscall" library function which could be replaced with a inline assembly and the same is proposed as a fix here.

---
  tools/testing/selftests/powerpc/benchmarks/Makefile    |  2 +-
  .../testing/selftests/powerpc/benchmarks/exec_target.c | 10 ++++++++--
  2 files changed, 9 insertions(+), 3 deletions(-)
tools/testing/selftests/powerpc/benchmarks/Makefile +1 -1
index 1321922038d0..ca4483c238b9 100644
@@ -18,4 +18,4 @@ $(OUTPUT)/context_switch: LDLIBS += -lpthread
 
  $(OUTPUT)/fork: LDLIBS += -lpthread
 
-$(OUTPUT)/exec_target: CFLAGS += -static -nostartfiles
+$(OUTPUT)/exec_target: CFLAGS += -nostartfiles
tools/testing/selftests/powerpc/benchmarks/exec_target.c +0 -1
index c14b0fc1edde..20027a23b594 100644
@@ -7,10 +7,16 @@
   */
 
  #define _GNU_SOURCE
-#include <unistd.h>
  #include <sys/syscall.h>
 
  void _start(void)

tools/testing/selftests/powerpc/benchmarks/exec_target.c line 21

Quoted 10 lines
tools/testing/selftests/powerpc/benchmarks/exec_target.c +8 -1
  {
-	syscall(SYS_exit, 0);
+	asm volatile (
+		"li %%r0, %[sys_exit];"
+		"li %%r3, 0;"
+		"sc;"
+		:
+		: [sys_exit] "i" (SYS_exit)
+		: "r0", "r3"
+	);

That looks ok because SYS_exit() is not supposed to return, but in the general case you should take a lot more precautions regarding which registers get clobbered when using sc.

Maybe it is worth a comment.

Christophe

Quoted 1 line
tools/testing/selftests/powerpc/benchmarks/exec_target.c +0 -0
  }
author maintainer
#2 lore raw
Quoted 53 lines · Christophe Leroy

Le 05/08/2024 à 10:30, Madhavan Srinivasan a écrit :

Quoted 37 lines

Currently exec-target.c file is linked as static and this post a requirement to install libc dev package to build. Without it, build-breaks when compiling selftest/powerpc/benchmark.

   CC       exec_target
/usr/bin/ld: cannot find -lc: No such file or directory
collect2: error: ld returned 1 exit status

exec_target.c is using "syscall" library function which could be replaced with a inline assembly and the same is proposed as a fix here.

---
  tools/testing/selftests/powerpc/benchmarks/Makefile    |  2 +- .../testing/selftests/powerpc/benchmarks/exec_target.c | 10 ++++++++--

  2 files changed, 9 insertions(+), 3 deletions(-)
tools/testing/selftests/powerpc/benchmarks/Makefile +0 -0
index 1321922038d0..ca4483c238b9 100644
@@ -18,4 +18,4 @@ $(OUTPUT)/context_switch: LDLIBS += -lpthread

    $(OUTPUT)/fork: LDLIBS += -lpthread -$(OUTPUT)/exec_target: CFLAGS += -static -nostartfiles +$(OUTPUT)/exec_target: CFLAGS += -nostartfiles

tools/testing/selftests/powerpc/benchmarks/exec_target.c +0 -1
index c14b0fc1edde..20027a23b594 100644
@@ -7,10 +7,16 @@
    */
     #define _GNU_SOURCE
-#include <unistd.h>
   #include <sys/syscall.h>
     void _start(void)

tools/testing/selftests/powerpc/benchmarks/exec_target.c line 19

Quoted 10 lines
tools/testing/selftests/powerpc/benchmarks/exec_target.c +8 -1
   {
-    syscall(SYS_exit, 0);
+    asm volatile (
+        "li %%r0, %[sys_exit];"
+        "li %%r3, 0;"
+        "sc;"
+        :
+        : [sys_exit] "i" (SYS_exit)
+        : "r0", "r3"
+    );

That looks ok because SYS_exit() is not supposed to return, but in the general case you should take a lot more precautions regarding which registers get clobbered when using sc.

Quoted 1 line

Maybe it is worth a comment.

ok sure and something like this will help?

+        : "r0", "r3" //clobber registers, r0 - syscall number, r3 - exit value

Maddy

Quoted 4 lines

Christophe

Quoted 1 line
tools/testing/selftests/powerpc/benchmarks/exec_target.c +0 -0
   }
#3 lore raw

Le 09/08/2024 à 06:25, Madhavan Srinivasan a écrit :

Quoted 55 lines · Christophe Leroy
Quoted 53 lines

Le 05/08/2024 à 10:30, Madhavan Srinivasan a écrit :

Quoted 37 lines

Currently exec-target.c file is linked as static and this post a requirement to install libc dev package to build. Without it, build-breaks when compiling selftest/powerpc/benchmark.

   CC       exec_target
/usr/bin/ld: cannot find -lc: No such file or directory
collect2: error: ld returned 1 exit status

exec_target.c is using "syscall" library function which could be replaced with a inline assembly and the same is proposed as a fix here.

---
  tools/testing/selftests/powerpc/benchmarks/Makefile    |  2 +- .../testing/selftests/powerpc/benchmarks/exec_target.c | 10 ++++++++--

  2 files changed, 9 insertions(+), 3 deletions(-)
tools/testing/selftests/powerpc/benchmarks/Makefile +0 -0
index 1321922038d0..ca4483c238b9 100644
@@ -18,4 +18,4 @@ $(OUTPUT)/context_switch: LDLIBS += -lpthread

    $(OUTPUT)/fork: LDLIBS += -lpthread -$(OUTPUT)/exec_target: CFLAGS += -static -nostartfiles +$(OUTPUT)/exec_target: CFLAGS += -nostartfiles

tools/testing/selftests/powerpc/benchmarks/exec_target.c +0 -1
index c14b0fc1edde..20027a23b594 100644
@@ -7,10 +7,16 @@
    */
     #define _GNU_SOURCE
-#include <unistd.h>
   #include <sys/syscall.h>
     void _start(void)

tools/testing/selftests/powerpc/benchmarks/exec_target.c line 19

Quoted 10 lines
tools/testing/selftests/powerpc/benchmarks/exec_target.c +8 -1
   {
-    syscall(SYS_exit, 0);
+    asm volatile (
+        "li %%r0, %[sys_exit];"
+        "li %%r3, 0;"
+        "sc;"
+        :
+        : [sys_exit] "i" (SYS_exit)
+        : "r0", "r3"
+    );

That looks ok because SYS_exit() is not supposed to return, but in the general case you should take a lot more precautions regarding which registers get clobbered when using sc.

Quoted 9 lines
Quoted 1 line

Maybe it is worth a comment.

ok sure and something like this will help?

+        : "r0", "r3" //clobber registers, r0 - syscall number, r3 - exit value

Not really.

sc will clobber r0 and r3-r12, also SO bit in CR.

Here the reason why you have no problem with that is that SYS_exit never returns. At the end, even your "r0" and "r3" clobber are unnecessary because of that.

Christophe

author maintainer
#4 lore raw
Quoted 69 lines · Christophe Leroy

Le 09/08/2024 à 06:25, Madhavan Srinivasan a écrit :

Quoted 56 lines · Christophe Leroy
Quoted 54 lines

Le 05/08/2024 à 10:30, Madhavan Srinivasan a écrit :

Quoted 38 lines

Currently exec-target.c file is linked as static and this post a requirement to install libc dev package to build. Without it, build-breaks when compiling selftest/powerpc/benchmark.

   CC       exec_target
/usr/bin/ld: cannot find -lc: No such file or directory
collect2: error: ld returned 1 exit status

exec_target.c is using "syscall" library function which could be replaced with a inline assembly and the same is proposed as a fix here.

---
  tools/testing/selftests/powerpc/benchmarks/Makefile    | 2 +- .../testing/selftests/powerpc/benchmarks/exec_target.c | 10
++++++++--

  2 files changed, 9 insertions(+), 3 deletions(-)
tools/testing/selftests/powerpc/benchmarks/Makefile +0 -0
index 1321922038d0..ca4483c238b9 100644
@@ -18,4 +18,4 @@ $(OUTPUT)/context_switch: LDLIBS += -lpthread

    $(OUTPUT)/fork: LDLIBS += -lpthread -$(OUTPUT)/exec_target: CFLAGS += -static -nostartfiles +$(OUTPUT)/exec_target: CFLAGS += -nostartfiles

tools/testing/selftests/powerpc/benchmarks/exec_target.c +0 -1
index c14b0fc1edde..20027a23b594 100644
@@ -7,10 +7,16 @@
    */
     #define _GNU_SOURCE
-#include <unistd.h>
   #include <sys/syscall.h>
     void _start(void)

tools/testing/selftests/powerpc/benchmarks/exec_target.c line 19

Quoted 10 lines
tools/testing/selftests/powerpc/benchmarks/exec_target.c +8 -1
   {
-    syscall(SYS_exit, 0);
+    asm volatile (
+        "li %%r0, %[sys_exit];"
+        "li %%r3, 0;"
+        "sc;"
+        :
+        : [sys_exit] "i" (SYS_exit)
+        : "r0", "r3"
+    );

That looks ok because SYS_exit() is not supposed to return, but in the general case you should take a lot more precautions regarding which registers get clobbered when using sc.

Quoted 9 lines
Quoted 1 line

Maybe it is worth a comment.

ok sure and something like this will help?

+        : "r0", "r3" //clobber registers, r0 - syscall number, r3 - exit value

Quoted 5 lines

Not really.

sc will clobber r0 and r3-r12, also SO bit in CR.

Here the reason why you have no problem with that is that SYS_exit never returns. At the end, even your "r0" and "r3" clobber are unnecessary because of that.

ah nice. thanks for the details.
I will add comment and post a v2 soon.

Maddy

Quoted 2 lines

Christophe

#5 lore raw
In reply to Christophe Leroy
Quoted 31 lines · Christophe Leroy

Le 05/08/2024 à 10:30, Madhavan Srinivasan a écrit :

Quoted 30 lines

Currently exec-target.c file is linked as static and this post a requirement to install libc dev package to build. Without it, build-breaks when compiling selftest/powerpc/benchmark.

CC exec_target
/usr/bin/ld: cannot find -lc: No such file or directory
collect2: error: ld returned 1 exit status

exec_target.c is using "syscall" library function which could be replaced with a inline assembly and the same is proposed as a fix here.

---
  tools/testing/selftests/powerpc/benchmarks/Makefile    |  2 +-
  .../testing/selftests/powerpc/benchmarks/exec_target.c | 10 ++++++++--
  2 files changed, 9 insertions(+), 3 deletions(-)
tools/testing/selftests/powerpc/benchmarks/exec_target.c +0 -1
index c14b0fc1edde..20027a23b594 100644
@@ -7,10 +7,16 @@
   */
 
  #define _GNU_SOURCE
-#include <unistd.h>
  #include <sys/syscall.h>
 
Quoted 15 lines

tools/testing/selftests/powerpc/benchmarks/exec_target.c line 21

Quoted 11 lines
tools/testing/selftests/powerpc/benchmarks/exec_target.c +8 -1
  void _start(void)
  {
-	syscall(SYS_exit, 0);
+	asm volatile (
+		"li %%r0, %[sys_exit];"
+		"li %%r3, 0;"
+		"sc;"
+		:
+		: [sys_exit] "i" (SYS_exit)
+		: "r0", "r3"
+	);

That looks ok because SYS_exit() is not supposed to return, but in the general case you should take a lot more precautions regarding which registers get clobbered when using sc.

That's my fault for just blurting out that diff on slack without thinking about it too hard.

We should probably just add the proper clobbers, in case anyone copies it in future. Which should be:

	asm volatile (
		"li %%r0, %[sys_exit];"
		"li %%r3, 0;"
		"sc;"
		:
		: [sys_exit] "i" (SYS_exit)
		: "r0", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10",
		  "r11", "r12", "r13", "cr0", "ctr", "xer", "memory"
	);

cheers