Skip to contentSkip to conversation

target/nios2: Pass semihosting arg to exit

PATCH In review Keith Packard via 2 participants 6 messages Jul 31, 2023 – Aug 1, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/nios2: Pass semihosting arg to exit

Instead of using the function number (which is always zero), fetch the application-provided exit code argument and pass that to the two exit functions.

---
 target/nios2/nios2-semi.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
target/nios2/nios2-semi.c +3 -2
index 3738774976..ffd1f095f6 100644
@@ -133,8 +133,9 @@ void do_nios2_semihosting(CPUNios2State *env)
     args = env->regs[R_ARG1];
     switch (nr) {
     case HOSTED_EXIT:
-        gdb_exit(env->regs[R_ARG0]);
-        exit(env->regs[R_ARG0]);
+        GET_ARG(0);
+        gdb_exit(arg0);
+        exit(arg0);
 
     case HOSTED_OPEN:
         GET_ARG(0);
Signature · 1 line
2.40.1
#1 lore raw
Quoted 15 lines · Keith Packard via

Instead of using the function number (which is always zero), fetch the application-provided exit code argument and pass that to the two exit functions.

---
 target/nios2/nios2-semi.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
target/nios2/nios2-semi.c +0 -0
index 3738774976..ffd1f095f6 100644

target/nios2/nios2-semi.c void do_nios2_semihosting(CPUNios2State *env) · line 138

Quoted 8 lines
target/nios2/nios2-semi.c +3 -2
@@ -133,8 +133,9 @@ void do_nios2_semihosting(CPUNios2State *env)
     args = env->regs[R_ARG1];
     switch (nr) {
     case HOSTED_EXIT:
-        gdb_exit(env->regs[R_ARG0]);
-        exit(env->regs[R_ARG0]);
+        GET_ARG(0);
+        gdb_exit(arg0);
+        exit(arg0);

The spec
https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;a=blob;f=libgloss/nios2/nios2-semi.txt;hb=HEAD

says that for HOSTED_EXIT the exit code is in r5, not in a parameter block pointed to by r5. That would imply that the correct change is to use R_ARG1 rather than R_ARG0 here.

thanks
-- PMM

#2 lore raw
Quoted 5 lines

says that for HOSTED_EXIT the exit code is in r5, not in a parameter block pointed to by r5. That would imply that the correct change is to use R_ARG1 rather than R_ARG0 here.

Ah, thanks -- I hadn't managed to find the actual standard yet. I'll resubmit with that fixed.

Signature · 1 line
-keith
#3 lore raw
Quoted 8 lines · Keith Packard
Quoted 6 lines

says that for HOSTED_EXIT the exit code is in r5, not in a parameter block pointed to by r5. That would imply that the correct change is to use R_ARG1 rather than R_ARG0 here.

Ah, thanks -- I hadn't managed to find the actual standard yet.

Yeah, the closest to a "standard" we have for nios2 is that I asked the Codesourcery folks to document it in the libgloss sources and put the URL to it in a comment at the top of nios2-semi.c, given that there's no official spec and the original and main guest-side user is libgloss.
m68k is in a similar position only without the URL in our source file :-)

-- PMM

#4 lore raw
Quoted 8 lines

Yeah, the closest to a "standard" we have for nios2 is that I asked the Codesourcery folks to document it in the libgloss sources and put the URL to it in a comment at the top of nios2-semi.c, given that there's no official spec and the original and main guest-side user is libgloss.
m68k is in a similar position only without the URL in our source file :-)

Yeah, we had the same ask when getting risc-v semihosting merged. For that, I actually pushed through an "official" risc-v standard. I kinda wish I'd used the m68k model instead of the arm model as that provides simple POSIX semantics...

https://github.com/riscv-software-src/riscv-semihosting/blob/main/riscv-semihosting-spec.adoc

Signature · 1 line
-keith
#5 lore raw
Quoted 10 lines · Keith Packard
Quoted 9 lines

Yeah, the closest to a "standard" we have for nios2 is that I asked the Codesourcery folks to document it in the libgloss sources and put the URL to it in a comment at the top of nios2-semi.c, given that there's no official spec and the original and main guest-side user is libgloss.
m68k is in a similar position only without the URL in our source file :-)

Quoted 4 lines

Yeah, we had the same ask when getting risc-v semihosting merged. For that, I actually pushed through an "official" risc-v standard. I kinda wish I'd used the m68k model instead of the arm model as that provides simple POSIX semantics...

Yeah, there's a lot of stuff in the Arm semihosting API that I wouldn't put in there for a from-new version that didn't need to handle legacy guests. Notably the "errno" concept is badly underspecified and/or broken. On an architecture with a decent number of registers it would probably also make sense to use them rather than having every single call put its args in memory.

thanks
-- PMM