[PATCH] common-user/host/riscv: use tail pseudoinstruction for calling tail

Icenowy Zheng posted 1 patch 7 months ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20250417072206.364008-1-uwu@icenowy.me
Maintainers: Riku Voipio <riku.voipio@iki.fi>
common-user/host/riscv/safe-syscall.inc.S | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
[PATCH] common-user/host/riscv: use tail pseudoinstruction for calling tail
Posted by Icenowy Zheng 7 months ago
The j pseudoinstruction maps to a JAL instruction, which can only handle
a jump to somewhere with a signed 20-bit destination. In case of static
linking and LTO'ing this easily leads to "relocation truncated to fit"
error.

Switch to use tail pseudoinstruction, which is the standard way to
tail-call a function in medium code model (emits AUIPC+JALR).

Signed-off-by: Icenowy Zheng <uwu@icenowy.me>
---
P.S.
It seems that moving it to common-user/ makes the file out of the
MAINTAINERS section of "RISC-V TCG CPUS". I Manually added the
maintainers there, but the MAINTAINERS file seems to need a change on
this.

 common-user/host/riscv/safe-syscall.inc.S | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/common-user/host/riscv/safe-syscall.inc.S b/common-user/host/riscv/safe-syscall.inc.S
index dfe83c300e..c8b81e33d0 100644
--- a/common-user/host/riscv/safe-syscall.inc.S
+++ b/common-user/host/riscv/safe-syscall.inc.S
@@ -69,11 +69,11 @@ safe_syscall_end:
 
         /* code path setting errno */
 0:      neg     a0, a0
-        j       safe_syscall_set_errno_tail
+        tail    safe_syscall_set_errno_tail
 
         /* code path when we didn't execute the syscall */
 2:      li      a0, QEMU_ERESTARTSYS
-        j       safe_syscall_set_errno_tail
+        tail    safe_syscall_set_errno_tail
 
         .cfi_endproc
         .size   safe_syscall_base, .-safe_syscall_base
-- 
2.49.0
Re: [PATCH] common-user/host/riscv: use tail pseudoinstruction for calling tail
Posted by Alistair Francis 6 months, 3 weeks ago
On Thu, Apr 17, 2025 at 5:24 PM Icenowy Zheng <uwu@icenowy.me> wrote:
>
> The j pseudoinstruction maps to a JAL instruction, which can only handle
> a jump to somewhere with a signed 20-bit destination. In case of static
> linking and LTO'ing this easily leads to "relocation truncated to fit"
> error.
>
> Switch to use tail pseudoinstruction, which is the standard way to
> tail-call a function in medium code model (emits AUIPC+JALR).
>
> Signed-off-by: Icenowy Zheng <uwu@icenowy.me>

Thanks!

Applied to riscv-to-apply.next

Alistair

> ---
> P.S.
> It seems that moving it to common-user/ makes the file out of the
> MAINTAINERS section of "RISC-V TCG CPUS". I Manually added the
> maintainers there, but the MAINTAINERS file seems to need a change on
> this.
>
>  common-user/host/riscv/safe-syscall.inc.S | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/common-user/host/riscv/safe-syscall.inc.S b/common-user/host/riscv/safe-syscall.inc.S
> index dfe83c300e..c8b81e33d0 100644
> --- a/common-user/host/riscv/safe-syscall.inc.S
> +++ b/common-user/host/riscv/safe-syscall.inc.S
> @@ -69,11 +69,11 @@ safe_syscall_end:
>
>          /* code path setting errno */
>  0:      neg     a0, a0
> -        j       safe_syscall_set_errno_tail
> +        tail    safe_syscall_set_errno_tail
>
>          /* code path when we didn't execute the syscall */
>  2:      li      a0, QEMU_ERESTARTSYS
> -        j       safe_syscall_set_errno_tail
> +        tail    safe_syscall_set_errno_tail
>
>          .cfi_endproc
>          .size   safe_syscall_base, .-safe_syscall_base
> --
> 2.49.0
>
>
Re: [PATCH] common-user/host/riscv: use tail pseudoinstruction for calling tail
Posted by Richard Henderson 7 months ago
On 4/17/25 00:22, Icenowy Zheng wrote:
> The j pseudoinstruction maps to a JAL instruction, which can only handle
> a jump to somewhere with a signed 20-bit destination. In case of static
> linking and LTO'ing this easily leads to "relocation truncated to fit"
> error.
> 
> Switch to use tail pseudoinstruction, which is the standard way to
> tail-call a function in medium code model (emits AUIPC+JALR).
> 
> Signed-off-by: Icenowy Zheng <uwu@icenowy.me>
> ---
> P.S.
> It seems that moving it to common-user/ makes the file out of the
> MAINTAINERS section of "RISC-V TCG CPUS". I Manually added the
> maintainers there, but the MAINTAINERS file seems to need a change on
> this.
> 
>   common-user/host/riscv/safe-syscall.inc.S | 4 ++--
>   1 file changed, 2 insertions(+), 2 deletions(-)

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>


r~

> 
> diff --git a/common-user/host/riscv/safe-syscall.inc.S b/common-user/host/riscv/safe-syscall.inc.S
> index dfe83c300e..c8b81e33d0 100644
> --- a/common-user/host/riscv/safe-syscall.inc.S
> +++ b/common-user/host/riscv/safe-syscall.inc.S
> @@ -69,11 +69,11 @@ safe_syscall_end:
>   
>           /* code path setting errno */
>   0:      neg     a0, a0
> -        j       safe_syscall_set_errno_tail
> +        tail    safe_syscall_set_errno_tail
>   
>           /* code path when we didn't execute the syscall */
>   2:      li      a0, QEMU_ERESTARTSYS
> -        j       safe_syscall_set_errno_tail
> +        tail    safe_syscall_set_errno_tail
>   
>           .cfi_endproc
>           .size   safe_syscall_base, .-safe_syscall_base