Skip to contentSkip to conversation

riscv: lib: sbi_shutdown add exit code.

PATCH Superseded Jesse Taube 2 participants 4 messages Jun 20, 2025 – Jun 24, 2025

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 riscv: lib: sbi_shutdown add exit code.

When exiting it may be useful for the sbi implementation to know the exit code.
Add exit code to sbi_shutdown, and use it in exit().

---
 lib/riscv/asm/sbi.h | 2 +-
 lib/riscv/io.c      | 2 +-
 lib/riscv/sbi.c     | 4 ++--
 3 files changed, 4 insertions(+), 4 deletions(-)
lib/riscv/asm/sbi.h +1 -1
index a5738a5c..de11c109 100644
@@ -250,7 +250,7 @@ struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0,
 			unsigned long arg3, unsigned long arg4,
 			unsigned long arg5);
 
-void sbi_shutdown(void);
+void sbi_shutdown(unsigned int code);
 struct sbiret sbi_hart_start(unsigned long hartid, unsigned long entry, unsigned long sp);
 struct sbiret sbi_hart_stop(void);
 struct sbiret sbi_hart_get_status(unsigned long hartid);
lib/riscv/io.c +1 -1
index fb40adb7..02231268 100644
@@ -150,7 +150,7 @@ void halt(int code);
 void exit(int code)
 {
 	printf("\nEXIT: STATUS=%d\n", ((code) << 1) | 1);
-	sbi_shutdown();
+	sbi_shutdown(code & 1);
 	halt(code);
 	__builtin_unreachable();
 }
lib/riscv/sbi.c +2 -2
index 2959378f..9dd11e9d 100644
@@ -107,9 +107,9 @@ struct sbiret sbi_sse_inject(unsigned long event_id, unsigned long hart_id)
 	return sbi_ecall(SBI_EXT_SSE, SBI_EXT_SSE_INJECT, event_id, hart_id, 0, 0, 0, 0);
 }
 
-void sbi_shutdown(void)
+void sbi_shutdown(unsigned int code)
 {
-	sbi_ecall(SBI_EXT_SRST, 0, 0, 0, 0, 0, 0, 0);
+	sbi_ecall(SBI_EXT_SRST, 0, 0, code, 0, 0, 0, 0);
 	puts("SBI shutdown failed!\n");
 }
 
Signature · 1 line
2.43.0
#1 lore raw
Quoted 42 lines · Jesse Taube

When exiting it may be useful for the sbi implementation to know the exit code.
Add exit code to sbi_shutdown, and use it in exit().

---
 lib/riscv/asm/sbi.h | 2 +-
 lib/riscv/io.c      | 2 +-
 lib/riscv/sbi.c     | 4 ++--
 3 files changed, 4 insertions(+), 4 deletions(-)
lib/riscv/asm/sbi.h +1 -1
index a5738a5c..de11c109 100644
@@ -250,7 +250,7 @@ struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0,
 			unsigned long arg3, unsigned long arg4,
 			unsigned long arg5);
 
-void sbi_shutdown(void);
+void sbi_shutdown(unsigned int code);
 struct sbiret sbi_hart_start(unsigned long hartid, unsigned long entry, unsigned long sp);
 struct sbiret sbi_hart_stop(void);
 struct sbiret sbi_hart_get_status(unsigned long hartid);
lib/riscv/io.c +1 -1
index fb40adb7..02231268 100644
@@ -150,7 +150,7 @@ void halt(int code);
 void exit(int code)
 {
 	printf("\nEXIT: STATUS=%d\n", ((code) << 1) | 1);
-	sbi_shutdown();
+	sbi_shutdown(code & 1);
 	halt(code);
 	__builtin_unreachable();
 }
lib/riscv/sbi.c +0 -0
index 2959378f..9dd11e9d 100644

lib/riscv/sbi.c struct sbiret sbi_sse_inject(unsigned long event_id, unsigned long hart_id) · line 112

Quoted 8 lines
lib/riscv/sbi.c +2 -2
@@ -107,9 +107,9 @@ struct sbiret sbi_sse_inject(unsigned long event_id, unsigned long hart_id)
 	return sbi_ecall(SBI_EXT_SSE, SBI_EXT_SSE_INJECT, event_id, hart_id, 0, 0, 0, 0);
 }
 
-void sbi_shutdown(void)
+void sbi_shutdown(unsigned int code)
 {
-	sbi_ecall(SBI_EXT_SRST, 0, 0, 0, 0, 0, 0, 0);
+	sbi_ecall(SBI_EXT_SRST, 0, 0, code, 0, 0, 0, 0);

We can't do this because a kvm-unit-tests exit code is not an SRST::reset_reason[1]. This could result in the SBI implementation returning an error, or doing something else, rather than shutting down.

If this is a custom kvm-unit-tests-specific SBI implementation, then we could pass in a reset_reason in the 0xE0000000 - 0xEFFFFFFF range.

[1] https://github.com/riscv-non-isa/riscv-sbi-doc/blob/master/src/ext-sys-reset.adoc#table_srst_system_reset_reasons

Thanks,
drew

Quoted 11 lines
lib/riscv/sbi.c +0 -0
 	puts("SBI shutdown failed!\n");
 }
 
Signature · 7 lines
2.43.0


-- 
kvm-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kvm-riscv
#2 lore raw
Quoted 47 lines · Andrew Jones
Quoted 45 lines · Jesse Taube

When exiting it may be useful for the sbi implementation to know the exit code.
Add exit code to sbi_shutdown, and use it in exit().

---
 lib/riscv/asm/sbi.h | 2 +-
 lib/riscv/io.c      | 2 +-
 lib/riscv/sbi.c     | 4 ++--
 3 files changed, 4 insertions(+), 4 deletions(-)
lib/riscv/asm/sbi.h +1 -1
index a5738a5c..de11c109 100644
@@ -250,7 +250,7 @@ struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0,
                      unsigned long arg3, unsigned long arg4,
                      unsigned long arg5);
 
-void sbi_shutdown(void);
+void sbi_shutdown(unsigned int code);
 struct sbiret sbi_hart_start(unsigned long hartid, unsigned long entry, unsigned long sp);
 struct sbiret sbi_hart_stop(void);
 struct sbiret sbi_hart_get_status(unsigned long hartid);
lib/riscv/io.c +1 -1
index fb40adb7..02231268 100644
@@ -150,7 +150,7 @@ void halt(int code);
 void exit(int code)
 {
      printf("\nEXIT: STATUS=%d\n", ((code) << 1) | 1);
-     sbi_shutdown();
+     sbi_shutdown(code & 1);
      halt(code);
      __builtin_unreachable();
 }
lib/riscv/sbi.c +0 -0
index 2959378f..9dd11e9d 100644
@@ -107,9 +107,9 @@ struct sbiret sbi_sse_inject(unsigned long event_id, unsigned long hart_id)
      return sbi_ecall(SBI_EXT_SSE, SBI_EXT_SSE_INJECT, event_id, hart_id, 0, 0, 0, 0);
 }
 
Quoted 10 lines

lib/riscv/sbi.c struct sbiret sbi_sse_inject(unsigned long event_id, unsigned long hart_id) · line 112

Quoted 5 lines
lib/riscv/sbi.c +2 -2
-void sbi_shutdown(void)
+void sbi_shutdown(unsigned int code)
 {
-     sbi_ecall(SBI_EXT_SRST, 0, 0, 0, 0, 0, 0, 0);
+     sbi_ecall(SBI_EXT_SRST, 0, 0, code, 0, 0, 0, 0);

We can't do this because a kvm-unit-tests exit code is not an SRST::reset_reason[1]. This could result in the SBI implementation returning an error, or doing something else, rather than shutting down.

Yes that's why there is:
+sbi_shutdown(code & 1);
Admittedly it should probably be:
+sbi_shutdown(!!code);

Quoted 3 lines

If this is a custom kvm-unit-tests-specific SBI implementation, then we could pass in a reset_reason in the 0xE0000000 - 0xEFFFFFFF range.

That still doesn't guarantee it to succeed.
In the exit function we can add a fallback like sbi_shutdown(0);, but reason code 1 (System failure) should always work.
If anyone wants to use it for SBI specific codes, that's fine, but I only added it for the No reason and System failure exit codes.

Thanks,
Jesse Taube.

Quoted 18 lines

[1] https://github.com/riscv-non-isa/riscv-sbi-doc/blob/master/src/ext-sys-reset.adoc#table_srst_system_reset_reasons

Thanks,
drew

Quoted 11 lines
lib/riscv/sbi.c +0 -1
      puts("SBI shutdown failed!\n");
 }
 
--

2.43.0

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

#3 lore raw
Quoted 59 lines · Jesse Taube
Quoted 47 lines · Andrew Jones
Quoted 45 lines · Jesse Taube

When exiting it may be useful for the sbi implementation to know the exit code.
Add exit code to sbi_shutdown, and use it in exit().

---
 lib/riscv/asm/sbi.h | 2 +-
 lib/riscv/io.c      | 2 +-
 lib/riscv/sbi.c     | 4 ++--
 3 files changed, 4 insertions(+), 4 deletions(-)
lib/riscv/asm/sbi.h +1 -1
index a5738a5c..de11c109 100644
@@ -250,7 +250,7 @@ struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0,
                      unsigned long arg3, unsigned long arg4,
                      unsigned long arg5);
 
-void sbi_shutdown(void);
+void sbi_shutdown(unsigned int code);
 struct sbiret sbi_hart_start(unsigned long hartid, unsigned long entry, unsigned long sp);
 struct sbiret sbi_hart_stop(void);
 struct sbiret sbi_hart_get_status(unsigned long hartid);
lib/riscv/io.c +1 -1
index fb40adb7..02231268 100644
@@ -150,7 +150,7 @@ void halt(int code);
 void exit(int code)
 {
      printf("\nEXIT: STATUS=%d\n", ((code) << 1) | 1);
-     sbi_shutdown();
+     sbi_shutdown(code & 1);
      halt(code);
      __builtin_unreachable();
 }
lib/riscv/sbi.c +0 -0
index 2959378f..9dd11e9d 100644
@@ -107,9 +107,9 @@ struct sbiret sbi_sse_inject(unsigned long event_id, unsigned long hart_id)
      return sbi_ecall(SBI_EXT_SSE, SBI_EXT_SSE_INJECT, event_id, hart_id, 0, 0, 0, 0);
 }
 
Quoted 10 lines

lib/riscv/sbi.c struct sbiret sbi_sse_inject(unsigned long event_id, unsigned long hart_id) · line 112

Quoted 5 lines
lib/riscv/sbi.c +2 -2
-void sbi_shutdown(void)
+void sbi_shutdown(unsigned int code)
 {
-     sbi_ecall(SBI_EXT_SRST, 0, 0, 0, 0, 0, 0, 0);
+     sbi_ecall(SBI_EXT_SRST, 0, 0, code, 0, 0, 0, 0);

We can't do this because a kvm-unit-tests exit code is not an SRST::reset_reason[1]. This could result in the SBI implementation returning an error, or doing something else, rather than shutting down.

Quoted 2 lines

Yes that's why there is:
+sbi_shutdown(code & 1);

Ah, I overlooked that.

Quoted 2 lines

Admittedly it should probably be:
+sbi_shutdown(!!code);

Indeed, it would only work now because bit 0 is set in the abort exit code.

Quoted 10 lines
Quoted 4 lines

If this is a custom kvm-unit-tests-specific SBI implementation, then we could pass in a reset_reason in the 0xE0000000 - 0xEFFFFFFF range.

That still doesn't guarantee it to succeed.
In the exit function we can add a fallback like sbi_shutdown(0);, but reason code 1 (System failure) should always work.
If anyone wants to use it for SBI specific codes, that's fine, but I only added it for the No reason and System failure exit codes.

Yup, I see now that the intention is just to say success/failure, not pass down the actual exit code. The commit message says exit code, though, which is why I didn't read the patch closely.

Please send a v2 with the !! change and a change to the commit message.

Thanks,
drew