selftests: sud_test: return correct emulated syscall value on RISC-V
PATCH 1/1 selftests: sud_test: return correct emulated syscall value on RISC-V
Currently, the sud_test expects the emulated syscall to return the emulated syscall number. This assumption only works on architectures were the syscall calling convention use the same register for syscall number/syscall return value. This is not the case for RISC-V and thus the return value must be also emulated using the provided ucontext.
--- tools/testing/selftests/syscall_user_dispatch/sud_test.c | 8 ++++++++ 1 file changed, 8 insertions(+)
tools/testing/selftests/syscall_user_dispatch/sud_test.c +8 -0
@@ -158,6 +158,14 @@ static void handle_sigsys(int sig, siginfo_t *info, void *ucontext)/* In preparation for sigreturn. */ SYSCALL_DISPATCH_OFF (glob_sel); + +/* +* Modify interrupted context returned value according to syscall +* calling convention +*/ +#if defined(__riscv) + ((ucontext_t *)ucontext)->uc_mcontext.__gregs[REG_A0 ] =MAGIC_SYSCALL_1 ; +#endif }TEST (dispatch_and_return)
Signature · 1 line
2.40.1
Quoted 20 lines · On Wed, 13 Sep 2023 07:07:11 PDT (-0700), [email protected] wrote:
Currently, the
sud_testexpects the emulated syscall to return the emulated syscall number. This assumption only works on architectures were the syscall calling convention use the same register for syscall number/syscall return value. This is not the case for RISC-V and thus the return value must be also emulated using the provided ucontext.Signed-off-by Clément Léger <[email protected]>--- tools/testing/selftests/syscall_user_dispatch/sud_test.c | 8 ++++++++ 1 file changed, 8 insertions(+)tools/testing/selftests/syscall_user_dispatch/sud_test.c +1
-0@@ -158,6 +158,14 @@ static void handle_sigsys(int sig, siginfo_t *info, void *ucontext)/* In preparation for sigreturn. */ SYSCALL_DISPATCH_OFF (glob_sel); +
tools/testing/selftests/syscall_user_dispatch/sud_test.c static void handle_sigsys(int sig, siginfo_t *info, void *ucontext) · line 171
Quoted 10 lines
tools/testing/selftests/syscall_user_dispatch/sud_test.c +7
-0+/* +* Modify interrupted context returned value according to syscall +* calling convention +*/ +#if defined(__riscv) + ((ucontext_t *)ucontext)->uc_mcontext.__gregs[REG_A0 ] =MAGIC_SYSCALL_1 ; +#endif }TEST (dispatch_and_return)
I'm not sure if I'm just tired, but it took me a while to figure out why this was necessary. I think this is a better explanation:
diff --git a/tools/testing/selftests/syscall_user_dispatch/sud_test.c b/tools/testing/selftests/syscall_user_dispatch/sud_test.c index b5d592d4099e..a913fd90cfa3 100644 --- a/tools/testing/selftests/syscall_user_dispatch/sud_test.c +++ b/tools/testing/selftests/syscall_user_dispatch/sud_test.c @@ -158,6 +158,16 @@ static void handle_sigsys(int sig, siginfo_t *info, void *ucontext)
/* In preparation for sigreturn. */
SYSCALL_DISPATCH_OFF(glob_sel);
+ /*
+ * The tests for argument handling assume that `syscall(x) == x`. This
+ * is a NOP on x86 because the syscall number is passed in %rax, which
+ * happens to also be the function ABI return register. Other
+ * architectures may need to swizzle the arguments around.
+ */
+#if defined(__riscv)
+ (ucontext_t*)ucontext)->uc_mcontext.__gregs[REG_A0] =
+ (ucontext_t*)ucontext)->uc_mcontext.__gregs[REG_A7];
+#endif
}
TEST(dispatch_and_return)but also
as I agree this is correct.
also: wouldn't arm64 also need to move x8 into x0 here, for essentially the same reason as we do?
Quoted 33 lines · Palmer Dabbelt
Quoted 32 lines · On Wed, 13 Sep 2023 07:07:11 PDT (-0700), [email protected] wrote:
Currently, the
sud_testexpects the emulated syscall to return the emulated syscall number. This assumption only works on architectures were the syscall calling convention use the same register for syscall number/syscall return value. This is not the case for RISC-V and thus the return value must be also emulated using the provided ucontext.Signed-off-by Clément Léger <[email protected]>---
tools/testing/selftests/syscall_user_dispatch/sud_test.c | 8 ++++++++1 file changed, 8 insertions(+)tools/testing/selftests/syscall_user_dispatch/sud_test.c +8
-0@@ -158,6 +158,14 @@ static void handle_sigsys(int sig, siginfo_t *info, void *ucontext) /* In preparation for sigreturn. */ SYSCALL_DISPATCH_OFF(glob_sel); + + /* +* Modify interrupted context returned value according to syscall +* calling convention +*/ +#if defined(__riscv) + ((ucontext_t*)ucontext)->uc_mcontext.__gregs[REG_A0] =MAGIC_SYSCALL_1 ; +#endif }
Quoted 4 lines
Quoted 1 line
TEST(dispatch_and_return)
I'm not sure if I'm just tired, but it took me a while to figure out why this was necessary. I think this is a better explanation:
I think it's because this mechanism does not behave like other syscalls at all and the classic calling convention does not really apply...
Quoted 10 lines
diff --git
a/tools/testing/selftests/syscall_user_dispatch/sud_test.cb/tools/testing/selftests/syscall_user_dispatch/sud_test.cindexb5d592d4099e..a913fd90cfa3100644
---a/tools/testing/selftests/syscall_user_dispatch/sud_test.c+++b/tools/testing/selftests/syscall_user_dispatch/sud_test.c@@ -158,6 +158,16 @@ static voidhandle_sigsys(int sig, siginfo_t *info, void *ucontext)
/* In preparation for sigreturn. */SYSCALL_DISPATCH_OFF(glob_sel);
Quoted 8 lines
+ /*
+ * The tests for argument handling assume thatsyscall(x) == x. This
+ * is a NOP on x86 because the syscall number is passed in %rax, which
+ * happens to also be the function ABI return register. Other + * architectures may need to swizzle the arguments around. + */
Indeed, that is more clear. Should I send a v2 ?
Quoted 9 lines
+#if
defined(__riscv)
+ (ucontext_t*)ucontext)->uc_mcontext.__gregs[REG_A0] = + (ucontext_t*)ucontext)->uc_mcontext.__gregs[REG_A7]; +#endif
}
TEST(dispatch_and_return)but also
Quoted 7 lines
Reviewed-by Palmer Dabbelt <[email protected]>Acked-by Palmer Dabbelt <[email protected]>as I agree this is correct.
also: wouldn't arm64 also need to move x8 into x0 here, for essentially the same reason as we do?
Yes, as well as for a bunch of other architectures. I suspect this has only been tested on x86. AFAIK, this feature is mainly for wine usage which then makes sense for x86 and games.
Thanks,
Clément
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Quoted 36 lines · On Thu, 09 Nov 2023 00:22:46 PST (-0800), [email protected] wrote:
Quoted 33 lines · Palmer Dabbelt
Quoted 32 lines · On Wed, 13 Sep 2023 07:07:11 PDT (-0700), [email protected] wrote:
Currently, the
sud_testexpects the emulated syscall to return the emulated syscall number. This assumption only works on architectures were the syscall calling convention use the same register for syscall number/syscall return value. This is not the case for RISC-V and thus the return value must be also emulated using the provided ucontext.Signed-off-by Clément Léger <[email protected]>---
tools/testing/selftests/syscall_user_dispatch/sud_test.c | 8 ++++++++1 file changed, 8 insertions(+)tools/testing/selftests/syscall_user_dispatch/sud_test.c +8
-0@@ -158,6 +158,14 @@ static void handle_sigsys(int sig, siginfo_t *info, void *ucontext) /* In preparation for sigreturn. */ SYSCALL_DISPATCH_OFF(glob_sel); + + /* +* Modify interrupted context returned value according to syscall +* calling convention +*/ +#if defined(__riscv) + ((ucontext_t*)ucontext)->uc_mcontext.__gregs[REG_A0] =MAGIC_SYSCALL_1 ; +#endif }
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
TEST(dispatch_and_return)
I'm not sure if I'm just tired, but it took me a while to figure out why this was necessary. I think this is a better explanation:
I think it's because this mechanism does not behave like other syscalls at all and the classic calling convention does not really apply...
Yep. I also got tripped up because I mis-read the docs and though SIGSYS was only for some error case (where it's actually for all the intercepted syscalls).
Quoted 18 lines
Quoted 9 lines
diff --git
a/tools/testing/selftests/syscall_user_dispatch/sud_test.cb/tools/testing/selftests/syscall_user_dispatch/sud_test.cindexb5d592d4099e..a913fd90cfa3100644
---a/tools/testing/selftests/syscall_user_dispatch/sud_test.c+++b/tools/testing/selftests/syscall_user_dispatch/sud_test.c@@ -158,6 +158,16 @@ static voidhandle_sigsys(int sig, siginfo_t *info, void *ucontext)
/* In preparation for sigreturn. */SYSCALL_DISPATCH_OFF(glob_sel);Quoted 8 lines
+ /*
+ * The tests for argument handling assume thatsyscall(x) == x. This
+ * is a NOP on x86 because the syscall number is passed in %rax, which
+ * happens to also be the function ABI return register. Other + * architectures may need to swizzle the arguments around. + */
Quoted 1 line
Indeed, that is more clear. Should I send a v2 ?
I would, but +Thomas as it looks like he's the one taking patches for this.
Quoted 13 lines
Quoted 13 lines
+#if
defined(__riscv)
+ (ucontext_t*)ucontext)->uc_mcontext.__gregs[REG_A0] = + (ucontext_t*)ucontext)->uc_mcontext.__gregs[REG_A7]; +#endif
}
TEST(dispatch_and_return)but also
Reviewed-by Palmer Dabbelt <[email protected]>Acked-by Palmer Dabbelt <[email protected]>
Quoted 8 lines
Quoted 4 lines
as I agree this is correct.
also: wouldn't arm64 also need to move x8 into x0 here, for essentially the same reason as we do?
Yes, as well as for a bunch of other architectures. I suspect this has only been tested on x86. AFAIK, this feature is mainly for wine usage which then makes sense for x86 and games.
Ya, makes sense -- I'd just looked at Arm to double-check my understanding here, as we usually don't find bugs in generic code before Arm does.
Quoted 4 lines
Thanks,
Clément