Skip to contentSkip to conversation

x86/asm: Introduce a tailcall pseduo-op

PATCH In review Andrew Cooper 2 participants 4 messages Jun 30, 2023 – Jul 5, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/asm: Introduce a tailcall pseduo-op

It was previously noted that CALL/BUG is a weird combination, but there is good reason to use this pattern.

Introduce an explicit tailcall macro make it clearer in context.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>

It would be nicer if tailcall was shorter, but that loses clarity. RISC-V has 'tail' as an alias for 'b', but that looses the call aspect, and tcall isn't sufficiently recognisable as tailcall IMO.

---
 xen/arch/x86/boot/x86_64.S           |  6 ++----
 xen/arch/x86/include/asm/asm-defns.h |  9 +++++++++
 xen/arch/x86/x86_64/entry.S          | 12 ++++--------
 3 files changed, 15 insertions(+), 12 deletions(-)
xen/arch/x86/boot/x86_64.S +2 -4
index 5d12937a0e40..04bb62ae8680 100644
@@ -74,14 +74,12 @@ ENTRY(__high_start)
 .L_ap_cet_done:
 #endif /* CONFIG_XEN_SHSTK || CONFIG_XEN_IBT */
 
-        call    start_secondary
-        BUG     /* start_secondary() shouldn't return. */
+        tailcall start_secondary
 
 .L_bsp:
         /* Pass off the Multiboot info structure to C land (if applicable). */
         mov     multiboot_ptr(%rip),%edi
-        call    __start_xen
-        BUG     /* __start_xen() shouldn't return. */
+        tailcall __start_xen
 
         .section .data.page_aligned, "aw", @progbits
         .align PAGE_SIZE, 0
xen/arch/x86/include/asm/asm-defns.h +9 -0
index 8bd9007731d5..9a7073ced5be 100644
@@ -20,6 +20,15 @@
     .byte 0x0f, 0x01, 0xdd
 .endm
 
+/*
+ * Call a noreturn function.  This could be JMP, but CALL results in a more
+ * helpful backtrace.  BUG is to catch functions which do decide to return...
+ */
+.macro tailcall fn:req
+    call  \fn
+    BUG   /* Shouldn't return */
+.endm
+
 .macro INDIRECT_BRANCH insn:req arg:req
 /*
  * Create an indirect branch.  insn is one of call/jmp, arg is a single
xen/arch/x86/x86_64/entry.S +4 -8
index 8b77d7113bbf..bca1500e2b45 100644
@@ -824,8 +824,7 @@ handle_exception_saved:
         DISPATCH(X86_EXC_CP, do_entry_CP)
 #undef DISPATCH
 
-        call  do_unhandled_trap
-        BUG   /* do_unhandled_trap() shouldn't return. */
+        tailcall do_unhandled_trap
 
 .L_exn_dispatch_done:
         mov   %r15, STACK_CPUINFO_FIELD(xen_cr3)(%r14)
@@ -880,8 +879,7 @@ exception_with_ints_disabled:
 FATAL_exception_with_ints_disabled:
         xorl  %esi,%esi
         movq  %rsp,%rdi
-        call  fatal_trap
-        BUG   /* fatal_trap() shouldn't return. */
+        tailcall fatal_trap
 
 ENTRY(divide_error)
         ENDBR64
@@ -989,8 +987,7 @@ ENTRY(double_fault)
 .Ldblf_cr3_okay:
 
         movq  %rsp,%rdi
-        call  do_double_fault
-        BUG   /* do_double_fault() shouldn't return. */
+        tailcall do_double_fault
 
 ENTRY(nmi)
         ENDBR64
@@ -1085,8 +1082,7 @@ handle_ist_exception:
         DISPATCH(X86_EXC_MC,  do_machine_check)
 #undef DISPATCH
 
-        call  do_unhandled_trap
-        BUG   /* do_unhandled_trap() shouldn't return. */
+        tailcall do_unhandled_trap
 
 .L_ist_dispatch_done:
         mov   %r15, STACK_CPUINFO_FIELD(xen_cr3)(%r14)

base-commit: f51e5d8eae8ece77a949571f39ee49904f3129aa

Signature · 1 line
2.30.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Andrew Cooper

It was previously noted that CALL/BUG is a weird combination, but there is good reason to use this pattern.

Introduce an explicit tailcall macro make it clearer in context.

No functional change.

Did I? Must have been a long time back, as I don't think I remember us talking about this.

Quoted 1 line
Quoted 8 lines

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>

It would be nicer if tailcall was shorter, but that loses clarity. RISC-V has 'tail' as an alias for 'b', but that looses the call aspect, and tcall isn't sufficiently recognisable as tailcall IMO.

I agree with all aspects you mention here.

Jan

#2 lore raw
Quoted 11 lines · Jan Beulich
Quoted 8 lines · Andrew Cooper

It was previously noted that CALL/BUG is a weird combination, but there is good reason to use this pattern.

Introduce an explicit tailcall macro make it clearer in context.

No functional change.

Did I? Must have been a long time back, as I don't think I remember us talking about this.

This was discussed on multiple patch reviews, where I was introducing the pattern and you were complaining about the BUG and comment.  I can drop the tag if you'd prefer.

Quoted 2 lines
Quoted 1 line

Thanks

~Andrew

#3 lore raw
Quoted 9 lines · Andrew Cooper
Quoted 8 lines · Jan Beulich
Quoted 7 lines · Andrew Cooper

It was previously noted that CALL/BUG is a weird combination, but there is good reason to use this pattern.

Introduce an explicit tailcall macro make it clearer in context.

No functional change.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Did I? Must have been a long time back, as I don't think I remember us talking about this.

This was discussed on multiple patch reviews, where I was introducing the pattern and you were complaining about the BUG and comment.  I can drop the tag if you'd prefer.

I'll leave that up to you. As said, it must have been quite some time back.

Jan