detached 4ad96f6

internal/fakecgo: preserve callee-saved registers in threadentry_trampoline (#347)

`threadentry_trampoline` is called from glibc (that is, C code) when
creating a new thread. When the thread exists, `threadentry_trampoline`
return back to glibc, which can perform some additional cleanups.

We currently aren't honoring the Linux C ABI. It specify some registers
as calle-saved which are clobbered by the Go code. This can cause
unexpected behaviors when returning to glibc when the thread exists,
as the C compiler assumes some registers will have the same value as
before the `threadentry_trampoline` function was called, but they don't.

This is a precondition to merge #344.
Also, might fix #304.
commit
4ad96f670df5c42b56d877115e398c3bc1d2c75d
author
Quim Muntal · · 2025-10-16 19:58 UTC
committer
GitHub · · 2025-10-16 19:58 UTC
parent
91e14e1
signature
GPG UnverifiedB5690EEEBB952194 The key is unknown
3 files · +45 -4
M internal/fakecgo/trampolines_amd64.s +11 -1
@@ -25,6 +25,7 @@ return value will be in AX
2525 */
2626 #include "textflag.h"
2727 #include "go_asm.h"
28+#include "abi_amd64.h"
2829
2930 // these trampolines map the gcc ABI to Go ABI and then calls into the Go equivalent functions.
3031
@@ -73,11 +74,20 @@ TEXT ·setg_trampoline(SB), NOSPLIT, $0-16
7374 CALL BX
7475 RET
7576
76-TEXT threadentry_trampoline(SB), NOSPLIT, $16
77+TEXT threadentry_trampoline(SB), NOSPLIT, $0
78+ // See crosscall2.
79+ PUSH_REGS_HOST_TO_ABI0()
80+
81+ // X15 is designated by Go as a fixed zero register.
82+ // Calling directly into ABIInternal, ensure it is zero.
83+ PXOR X15, X15
84+
7785 MOVQ DI, AX
7886 MOVQ ·threadentry_call(SB), DX
7987 MOVQ (DX), CX
8088 CALL CX
89+
90+ POP_REGS_HOST_TO_ABI0()
8191 RET
8292
8393 TEXT ·call5(SB), NOSPLIT, $0-56
@@ -25,6 +25,7 @@ return value will be in AX
25 */25 */
26 #include "textflag.h"26 #include "textflag.h"
27 #include "go_asm.h"27 #include "go_asm.h"
28+#include "abi_amd64.h"
28 29
29 // these trampolines map the gcc ABI to Go ABI and then calls into the Go equivalent functions.30 // these trampolines map the gcc ABI to Go ABI and then calls into the Go equivalent functions.
30 31
@@ -73,11 +74,20 @@ TEXT ·setg_trampoline(SB), NOSPLIT, $0-16
73 CALL BX74 CALL BX
74 RET75 RET
75 76
76-TEXT threadentry_trampoline(SB), NOSPLIT, $1677+TEXT threadentry_trampoline(SB), NOSPLIT, $0
78+ // See crosscall2.
79+ PUSH_REGS_HOST_TO_ABI0()
80+
81+ // X15 is designated by Go as a fixed zero register.
82+ // Calling directly into ABIInternal, ensure it is zero.
83+ PXOR X15, X15
84+
77 MOVQ DI, AX85 MOVQ DI, AX
78 MOVQ ·threadentry_call(SB), DX86 MOVQ ·threadentry_call(SB), DX
79 MOVQ (DX), CX87 MOVQ (DX), CX
80 CALL CX88 CALL CX
89+
90+ POP_REGS_HOST_TO_ABI0()
81 RET91 RET
82 92
83 TEXT ·call5(SB), NOSPLIT, $0-5693 TEXT ·call5(SB), NOSPLIT, $0-56
M internal/fakecgo/trampolines_arm64.s +16 -1
@@ -5,6 +5,7 @@
55
66 #include "textflag.h"
77 #include "go_asm.h"
8+#include "abi_arm64.h"
89
910 // these trampolines map the gcc ABI to Go ABI and then calls into the Go equivalent functions.
1011
@@ -53,11 +54,25 @@ TEXT ·setg_trampoline(SB), NOSPLIT, $0-16
5354 RET
5455
5556 TEXT threadentry_trampoline(SB), NOSPLIT, $0-0
56- MOVD R0, 8(RSP)
57+ // See crosscall2.
58+ SUB $(8*24), RSP
59+ STP (R0, R1), (8*1)(RSP)
60+ MOVD R3, (8*3)(RSP)
61+
62+ SAVE_R19_TO_R28(8*4)
63+ SAVE_F8_TO_F15(8*14)
64+ STP (R29, R30), (8*22)(RSP)
65+
5766 MOVD ·threadentry_call(SB), R26
5867 MOVD (R26), R2
5968 CALL (R2)
6069 MOVD $0, R0 // TODO: get the return value from threadentry
70+
71+ RESTORE_R19_TO_R28(8*4)
72+ RESTORE_F8_TO_F15(8*14)
73+ LDP (8*22)(RSP), (R29, R30)
74+
75+ ADD $(8*24), RSP
6176 RET
6277
6378 TEXT ·call5(SB), NOSPLIT, $0-0
@@ -5,6 +5,7 @@
5 5
6 #include "textflag.h"6 #include "textflag.h"
7 #include "go_asm.h"7 #include "go_asm.h"
8+#include "abi_arm64.h"
8 9
9 // these trampolines map the gcc ABI to Go ABI and then calls into the Go equivalent functions.10 // these trampolines map the gcc ABI to Go ABI and then calls into the Go equivalent functions.
10 11
@@ -53,11 +54,25 @@ TEXT ·setg_trampoline(SB), NOSPLIT, $0-16
53 RET54 RET
54 55
55 TEXT threadentry_trampoline(SB), NOSPLIT, $0-056 TEXT threadentry_trampoline(SB), NOSPLIT, $0-0
56- MOVD R0, 8(RSP)57+ // See crosscall2.
58+ SUB $(8*24), RSP
59+ STP (R0, R1), (8*1)(RSP)
60+ MOVD R3, (8*3)(RSP)
61+
62+ SAVE_R19_TO_R28(8*4)
63+ SAVE_F8_TO_F15(8*14)
64+ STP (R29, R30), (8*22)(RSP)
65+
57 MOVD ·threadentry_call(SB), R2666 MOVD ·threadentry_call(SB), R26
58 MOVD (R26), R267 MOVD (R26), R2
59 CALL (R2)68 CALL (R2)
60 MOVD $0, R0 // TODO: get the return value from threadentry69 MOVD $0, R0 // TODO: get the return value from threadentry
70+
71+ RESTORE_R19_TO_R28(8*4)
72+ RESTORE_F8_TO_F15(8*14)
73+ LDP (8*22)(RSP), (R29, R30)
74+
75+ ADD $(8*24), RSP
61 RET76 RET
62 77
63 TEXT ·call5(SB), NOSPLIT, $0-078 TEXT ·call5(SB), NOSPLIT, $0-0
M internal/fakecgo/trampolines_loong64.s +18 -2
@@ -5,6 +5,7 @@
55
66 #include "textflag.h"
77 #include "go_asm.h"
8+#include "abi_loong64.h"
89
910 // these trampolines map the gcc ABI to Go ABI and then calls into the Go equivalent functions.
1011
@@ -52,11 +53,26 @@ TEXT ·setg_trampoline(SB), NOSPLIT, $0
5253 CALL (R5)
5354 RET
5455
55-TEXT threadentry_trampoline(SB), NOSPLIT, $16
56- MOVV R4, 8(R3)
56+TEXT threadentry_trampoline(SB), NOSPLIT, $0
57+ // See crosscall2.
58+ ADDV $(-23*8), R3
59+ MOVV R4, (1*8)(R3) // fn unsafe.Pointer
60+ MOVV R5, (2*8)(R3) // a unsafe.Pointer
61+ MOVV R7, (3*8)(R3) // ctxt uintptr
62+
63+ SAVE_R22_TO_R31((4*8))
64+ SAVE_F24_TO_F31((14*8))
65+ MOVV R1, (22*8)(R3)
66+
5767 MOVV ·threadentry_call(SB), R5
5868 MOVV (R5), R6
5969 CALL (R6)
70+
71+ RESTORE_R22_TO_R31((4*8))
72+ RESTORE_F24_TO_F31((14*8))
73+ MOVV (22*8)(R3), R1
74+
75+ ADDV $(23*8), R3
6076 RET
6177
6278 TEXT ·call5(SB), NOSPLIT, $0-0
@@ -5,6 +5,7 @@
5 5
6 #include "textflag.h"6 #include "textflag.h"
7 #include "go_asm.h"7 #include "go_asm.h"
8+#include "abi_loong64.h"
8 9
9 // these trampolines map the gcc ABI to Go ABI and then calls into the Go equivalent functions.10 // these trampolines map the gcc ABI to Go ABI and then calls into the Go equivalent functions.
10 11
@@ -52,11 +53,26 @@ TEXT ·setg_trampoline(SB), NOSPLIT, $0
52 CALL (R5)53 CALL (R5)
53 RET54 RET
54 55
55-TEXT threadentry_trampoline(SB), NOSPLIT, $1656+TEXT threadentry_trampoline(SB), NOSPLIT, $0
56- MOVV R4, 8(R3)57+ // See crosscall2.
58+ ADDV $(-23*8), R3
59+ MOVV R4, (1*8)(R3) // fn unsafe.Pointer
60+ MOVV R5, (2*8)(R3) // a unsafe.Pointer
61+ MOVV R7, (3*8)(R3) // ctxt uintptr
62+
63+ SAVE_R22_TO_R31((4*8))
64+ SAVE_F24_TO_F31((14*8))
65+ MOVV R1, (22*8)(R3)
66+
57 MOVV ·threadentry_call(SB), R567 MOVV ·threadentry_call(SB), R5
58 MOVV (R5), R668 MOVV (R5), R6
59 CALL (R6)69 CALL (R6)
70+
71+ RESTORE_R22_TO_R31((4*8))
72+ RESTORE_F24_TO_F31((14*8))
73+ MOVV (22*8)(R3), R1
74+
75+ ADDV $(23*8), R3
60 RET76 RET
61 77
62 TEXT ·call5(SB), NOSPLIT, $0-078 TEXT ·call5(SB), NOSPLIT, $0-0

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 102

↑↓ moveenter openesc close