detached 23e4f41

internal/fakecgo: simplify ppc64le trampolines (#424)

Drop NOFRAME and manual LR/CR save/restore from the x_cgo_* trampolines,
letting the Go assembler manage their frames. These are called from Go
and don't need ELFv2 linkage area management.

Simplify setg_trampoline by dropping NOFRAME, using a Go-managed $16
frame with FP-relative argument access, and saving R31 directly to the
stack instead of shuttling through a non-volatile register.

In threadentry_trampoline, save and restore all C callee-saved registers
(R14-R31, F14-F31) using the SAVE_GPR/SAVE_FPR macros from abi_ppc64x.h.
This function is the entry point for threads created by pthread_create,
so it must preserve the C caller's non-volatile registers across the
call into Go code. Without this, Go can clobber these registers and
corrupt the C runtime's state when the thread exits.
commit
23e4f41d5c257ba46bcbeda5980dd43c02d4b311
author
George Adams · · 2026-02-26 18:02 UTC
committer
GitHub · · 2026-02-26 18:02 UTC
parent
7618415
signature
GPG UnverifiedB5690EEEBB952194 The key is unknown
1 file · +30 -129
M internal/fakecgo/trampolines_ppc64le.s +30 -129
@@ -5,188 +5,89 @@
55
66 #include "textflag.h"
77 #include "go_asm.h"
8+#include "abi_ppc64x.h"
89
910 // These trampolines map the C ABI to Go ABI and call into the Go equivalent functions.
10-//
11-// PPC64LE ELFv2 ABI stack frame layout:
12-// 0(R1) = backchain (pointer to caller's frame)
13-// 8(R1) = CR save area
14-// 16(R1) = LR save area
15-// 24(R1) = reserved
16-// 32(R1) = parameter save area (minimum 64 bytes for 8 args)
17-//
18-// Two patterns are used depending on call direction:
19-//
20-// C→Go trampolines: The C caller already provides a 32-byte linkage area.
21-// Save LR/CR into caller's frame at 16(R1)/8(R1) BEFORE allocating,
22-// then use MOVDU to allocate and set backchain atomically.
23-//
24-// Go→C trampolines: Go callers don't provide ELFv2 linkage area.
25-// Allocate frame first with MOVDU, then save LR/CR into OUR frame.
26-
27-TEXT x_cgo_init_trampoline(SB), NOSPLIT|NOFRAME, $0-0
28- MOVD LR, 16(R1)
29- MOVW CR, R0
30- MOVD R0, 8(R1)
3111
32- MOVDU R1, -32(R1)
33-
34- // R3, R4 already have the arguments
12+TEXT x_cgo_init_trampoline(SB), NOSPLIT, $0-0
3513 MOVD ·x_cgo_init_call(SB), R12
3614 MOVD (R12), R12
3715 MOVD R12, CTR
3816 CALL CTR
39-
40- ADD $32, R1
41-
42- MOVD 16(R1), LR
43- MOVD 8(R1), R0
44- MOVW R0, CR
4517 RET
4618
47-TEXT x_cgo_thread_start_trampoline(SB), NOSPLIT|NOFRAME, $0-0
48- MOVD LR, 16(R1)
49- MOVW CR, R0
50- MOVD R0, 8(R1)
51-
52- MOVDU R1, -32(R1)
53-
19+TEXT x_cgo_thread_start_trampoline(SB), NOSPLIT, $0-0
5420 MOVD ·x_cgo_thread_start_call(SB), R12
5521 MOVD (R12), R12
5622 MOVD R12, CTR
5723 CALL CTR
58-
59- ADD $32, R1
60-
61- MOVD 16(R1), LR
62- MOVD 8(R1), R0
63- MOVW R0, CR
6424 RET
6525
66-// void (*_cgo_setenv)(char**)
67-// C arg: R3 = pointer to env
68-// This is C→Go: caller is C ABI.
69-TEXT x_cgo_setenv_trampoline(SB), NOSPLIT|NOFRAME, $0-0
70- MOVD LR, 16(R1)
71- MOVW CR, R0
72- MOVD R0, 8(R1)
73-
74- MOVDU R1, -32(R1)
75-
26+TEXT x_cgo_setenv_trampoline(SB), NOSPLIT, $0-0
7627 MOVD ·x_cgo_setenv_call(SB), R12
7728 MOVD (R12), R12
7829 MOVD R12, CTR
7930 CALL CTR
80-
81- ADD $32, R1
82-
83- MOVD 16(R1), LR
84- MOVD 8(R1), R0
85- MOVW R0, CR
8631 RET
8732
88-TEXT x_cgo_unsetenv_trampoline(SB), NOSPLIT|NOFRAME, $0-0
89- MOVD LR, 16(R1)
90- MOVW CR, R0
91- MOVD R0, 8(R1)
92-
93- MOVDU R1, -32(R1)
94-
33+TEXT x_cgo_unsetenv_trampoline(SB), NOSPLIT, $0-0
9534 MOVD ·x_cgo_unsetenv_call(SB), R12
9635 MOVD (R12), R12
9736 MOVD R12, CTR
9837 CALL CTR
99-
100- ADD $32, R1
101-
102- MOVD 16(R1), LR
103- MOVD 8(R1), R0
104- MOVW R0, CR
10538 RET
10639
107-TEXT x_cgo_notify_runtime_init_done_trampoline(SB), NOSPLIT|NOFRAME, $0-0
108- MOVD LR, 16(R1)
109- MOVW CR, R0
110- MOVD R0, 8(R1)
111-
112- MOVDU R1, -32(R1)
113-
40+TEXT x_cgo_notify_runtime_init_done_trampoline(SB), NOSPLIT, $0-0
11441 CALL ·x_cgo_notify_runtime_init_done(SB)
115-
116- ADD $32, R1
117-
118- MOVD 16(R1), LR
119- MOVD 8(R1), R0
120- MOVW R0, CR
12142 RET
12243
123-TEXT x_cgo_bindm_trampoline(SB), NOSPLIT|NOFRAME, $0-0
124- MOVD LR, 16(R1)
125- MOVW CR, R0
126- MOVD R0, 8(R1)
127-
128- MOVDU R1, -32(R1)
129-
44+TEXT x_cgo_bindm_trampoline(SB), NOSPLIT, $0-0
13045 CALL ·x_cgo_bindm(SB)
131-
132- ADD $32, R1
133-
134- MOVD 16(R1), LR
135- MOVD 8(R1), R0
136- MOVW R0, CR
13746 RET
13847
139-TEXT ·setg_trampoline(SB), NOSPLIT|NOFRAME, $0-16
140- // Save LR, CR, and R31 to non-volatile registers (C ABI preserves R14-R31)
141- MOVD LR, R20
142- MOVW CR, R21
143- MOVD R31, R22 // save R31 because load_g clobbers it
144-
145- // Load arguments from Go stack
146- MOVD 32(R1), R12 // setg function pointer
147- MOVD 40(R1), R3 // g pointer → first C arg
48+// func setg_trampoline(setg uintptr, g uintptr)
49+TEXT ·setg_trampoline(SB), NOSPLIT, $16-16
50+ MOVD R31, 8(R1) // save R31 (load_g clobbers it)
14851
149- // Allocate ELFv2 frame for the C callee (32 bytes minimum)
150- MOVDU R1, -32(R1)
52+ MOVD setg+0(FP), R12
53+ MOVD newg+8(FP), R3
15154
152- // Call setg_gcc which stores g to TLS
15355 MOVD R12, CTR
15456 CALL CTR
15557
156- // setg_gcc stored g to TLS but restored old g in R30.
157- // Call load_g to reload g from TLS into R30.
158- // Note: load_g clobbers R31
15958 CALL runtime·load_g(SB)
16059
161- // Deallocate frame
162- ADD $32, R1
163-
164- // Clear R0 before returning to Go code.
165- // Go uses R0 as a constant 0 for things like "std r0,X(r1)" to zero stack locations.
166- // C/assembly functions may leave garbage in R0.
167- XOR R0, R0, R0
168-
169- // Restore LR, CR, and R31 from non-volatile registers
170- MOVD R22, R31 // restore R31
171- MOVD R20, LR
172- MOVW R21, CR
60+ MOVD 8(R1), R31
61+ XOR R0, R0, R0
17362 RET
17463
17564 TEXT threadentry_trampoline(SB), NOSPLIT|NOFRAME, $0-0
176- MOVD LR, 16(R1)
65+ // Called from C (pthread_create). Must save all C callee-saved registers.
66+ // Uses NOFRAME for proper ELFv2 backchain via MOVDU.
67+ MOVD LR, R0
68+ MOVD R0, 16(R1)
17769 MOVW CR, R0
17870 MOVD R0, 8(R1)
17971
180- MOVDU R1, -32(R1)
72+ MOVDU R1, -320(R1)
73+
74+ SAVE_GPR(32)
75+ SAVE_FPR(32+SAVE_GPR_SIZE)
76+
77+ MOVD $0, R0
18178
18279 MOVD ·threadentry_call(SB), R12
18380 MOVD (R12), R12
18481 MOVD R12, CTR
18582 CALL CTR
18683
187- ADD $32, R1
84+ RESTORE_FPR(32+SAVE_GPR_SIZE)
85+ RESTORE_GPR(32)
86+
87+ ADD $320, R1
18888
189- MOVD 16(R1), LR
89+ MOVD 16(R1), R0
90+ MOVD R0, LR
19091 MOVD 8(R1), R0
19192 MOVW R0, CR
19293 RET
@@ -5,188 +5,89 @@
5 5
6 #include "textflag.h"6 #include "textflag.h"
7 #include "go_asm.h"7 #include "go_asm.h"
8+#include "abi_ppc64x.h"
8 9
9 // These trampolines map the C ABI to Go ABI and call into the Go equivalent functions.10 // These trampolines map the C ABI to Go ABI and call into the Go equivalent functions.
10-//
11-// PPC64LE ELFv2 ABI stack frame layout:
12-// 0(R1) = backchain (pointer to caller's frame)
13-// 8(R1) = CR save area
14-// 16(R1) = LR save area
15-// 24(R1) = reserved
16-// 32(R1) = parameter save area (minimum 64 bytes for 8 args)
17-//
18-// Two patterns are used depending on call direction:
19-//
20-// C→Go trampolines: The C caller already provides a 32-byte linkage area.
21-// Save LR/CR into caller's frame at 16(R1)/8(R1) BEFORE allocating,
22-// then use MOVDU to allocate and set backchain atomically.
23-//
24-// Go→C trampolines: Go callers don't provide ELFv2 linkage area.
25-// Allocate frame first with MOVDU, then save LR/CR into OUR frame.
26-
27-TEXT x_cgo_init_trampoline(SB), NOSPLIT|NOFRAME, $0-0
28- MOVD LR, 16(R1)
29- MOVW CR, R0
30- MOVD R0, 8(R1)
31 11
32- MOVDU R1, -32(R1)12+TEXT x_cgo_init_trampoline(SB), NOSPLIT, $0-0
33-
34- // R3, R4 already have the arguments
35 MOVD ·x_cgo_init_call(SB), R1213 MOVD ·x_cgo_init_call(SB), R12
36 MOVD (R12), R1214 MOVD (R12), R12
37 MOVD R12, CTR15 MOVD R12, CTR
38 CALL CTR16 CALL CTR
39-
40- ADD $32, R1
41-
42- MOVD 16(R1), LR
43- MOVD 8(R1), R0
44- MOVW R0, CR
45 RET17 RET
46 18
47-TEXT x_cgo_thread_start_trampoline(SB), NOSPLIT|NOFRAME, $0-019+TEXT x_cgo_thread_start_trampoline(SB), NOSPLIT, $0-0
48- MOVD LR, 16(R1)
49- MOVW CR, R0
50- MOVD R0, 8(R1)
51-
52- MOVDU R1, -32(R1)
53-
54 MOVD ·x_cgo_thread_start_call(SB), R1220 MOVD ·x_cgo_thread_start_call(SB), R12
55 MOVD (R12), R1221 MOVD (R12), R12
56 MOVD R12, CTR22 MOVD R12, CTR
57 CALL CTR23 CALL CTR
58-
59- ADD $32, R1
60-
61- MOVD 16(R1), LR
62- MOVD 8(R1), R0
63- MOVW R0, CR
64 RET24 RET
65 25
66-// void (*_cgo_setenv)(char**)26+TEXT x_cgo_setenv_trampoline(SB), NOSPLIT, $0-0
67-// C arg: R3 = pointer to env
68-// This is C→Go: caller is C ABI.
69-TEXT x_cgo_setenv_trampoline(SB), NOSPLIT|NOFRAME, $0-0
70- MOVD LR, 16(R1)
71- MOVW CR, R0
72- MOVD R0, 8(R1)
73-
74- MOVDU R1, -32(R1)
75-
76 MOVD ·x_cgo_setenv_call(SB), R1227 MOVD ·x_cgo_setenv_call(SB), R12
77 MOVD (R12), R1228 MOVD (R12), R12
78 MOVD R12, CTR29 MOVD R12, CTR
79 CALL CTR30 CALL CTR
80-
81- ADD $32, R1
82-
83- MOVD 16(R1), LR
84- MOVD 8(R1), R0
85- MOVW R0, CR
86 RET31 RET
87 32
88-TEXT x_cgo_unsetenv_trampoline(SB), NOSPLIT|NOFRAME, $0-033+TEXT x_cgo_unsetenv_trampoline(SB), NOSPLIT, $0-0
89- MOVD LR, 16(R1)
90- MOVW CR, R0
91- MOVD R0, 8(R1)
92-
93- MOVDU R1, -32(R1)
94-
95 MOVD ·x_cgo_unsetenv_call(SB), R1234 MOVD ·x_cgo_unsetenv_call(SB), R12
96 MOVD (R12), R1235 MOVD (R12), R12
97 MOVD R12, CTR36 MOVD R12, CTR
98 CALL CTR37 CALL CTR
99-
100- ADD $32, R1
101-
102- MOVD 16(R1), LR
103- MOVD 8(R1), R0
104- MOVW R0, CR
105 RET38 RET
106 39
107-TEXT x_cgo_notify_runtime_init_done_trampoline(SB), NOSPLIT|NOFRAME, $0-040+TEXT x_cgo_notify_runtime_init_done_trampoline(SB), NOSPLIT, $0-0
108- MOVD LR, 16(R1)
109- MOVW CR, R0
110- MOVD R0, 8(R1)
111-
112- MOVDU R1, -32(R1)
113-
114 CALL ·x_cgo_notify_runtime_init_done(SB)41 CALL ·x_cgo_notify_runtime_init_done(SB)
115-
116- ADD $32, R1
117-
118- MOVD 16(R1), LR
119- MOVD 8(R1), R0
120- MOVW R0, CR
121 RET42 RET
122 43
123-TEXT x_cgo_bindm_trampoline(SB), NOSPLIT|NOFRAME, $0-044+TEXT x_cgo_bindm_trampoline(SB), NOSPLIT, $0-0
124- MOVD LR, 16(R1)
125- MOVW CR, R0
126- MOVD R0, 8(R1)
127-
128- MOVDU R1, -32(R1)
129-
130 CALL ·x_cgo_bindm(SB)45 CALL ·x_cgo_bindm(SB)
131-
132- ADD $32, R1
133-
134- MOVD 16(R1), LR
135- MOVD 8(R1), R0
136- MOVW R0, CR
137 RET46 RET
138 47
139-TEXT ·setg_trampoline(SB), NOSPLIT|NOFRAME, $0-1648+// func setg_trampoline(setg uintptr, g uintptr)
140- // Save LR, CR, and R31 to non-volatile registers (C ABI preserves R14-R31)49+TEXT ·setg_trampoline(SB), NOSPLIT, $16-16
141- MOVD LR, R2050+ MOVD R31, 8(R1) // save R31 (load_g clobbers it)
142- MOVW CR, R21
143- MOVD R31, R22 // save R31 because load_g clobbers it
144-
145- // Load arguments from Go stack
146- MOVD 32(R1), R12 // setg function pointer
147- MOVD 40(R1), R3 // g pointer → first C arg
148 51
149- // Allocate ELFv2 frame for the C callee (32 bytes minimum)52+ MOVD setg+0(FP), R12
150- MOVDU R1, -32(R1)53+ MOVD newg+8(FP), R3
151 54
152- // Call setg_gcc which stores g to TLS
153 MOVD R12, CTR55 MOVD R12, CTR
154 CALL CTR56 CALL CTR
155 57
156- // setg_gcc stored g to TLS but restored old g in R30.
157- // Call load_g to reload g from TLS into R30.
158- // Note: load_g clobbers R31
159 CALL runtime·load_g(SB)58 CALL runtime·load_g(SB)
160 59
161- // Deallocate frame60+ MOVD 8(R1), R31
162- ADD $32, R161+ XOR R0, R0, R0
163-
164- // Clear R0 before returning to Go code.
165- // Go uses R0 as a constant 0 for things like "std r0,X(r1)" to zero stack locations.
166- // C/assembly functions may leave garbage in R0.
167- XOR R0, R0, R0
168-
169- // Restore LR, CR, and R31 from non-volatile registers
170- MOVD R22, R31 // restore R31
171- MOVD R20, LR
172- MOVW R21, CR
173 RET62 RET
174 63
175 TEXT threadentry_trampoline(SB), NOSPLIT|NOFRAME, $0-064 TEXT threadentry_trampoline(SB), NOSPLIT|NOFRAME, $0-0
176- MOVD LR, 16(R1)65+ // Called from C (pthread_create). Must save all C callee-saved registers.
66+ // Uses NOFRAME for proper ELFv2 backchain via MOVDU.
67+ MOVD LR, R0
68+ MOVD R0, 16(R1)
177 MOVW CR, R069 MOVW CR, R0
178 MOVD R0, 8(R1)70 MOVD R0, 8(R1)
179 71
180- MOVDU R1, -32(R1)72+ MOVDU R1, -320(R1)
73+
74+ SAVE_GPR(32)
75+ SAVE_FPR(32+SAVE_GPR_SIZE)
76+
77+ MOVD $0, R0
181 78
182 MOVD ·threadentry_call(SB), R1279 MOVD ·threadentry_call(SB), R12
183 MOVD (R12), R1280 MOVD (R12), R12
184 MOVD R12, CTR81 MOVD R12, CTR
185 CALL CTR82 CALL CTR
186 83
187- ADD $32, R184+ RESTORE_FPR(32+SAVE_GPR_SIZE)
85+ RESTORE_GPR(32)
86+
87+ ADD $320, R1
188 88
189- MOVD 16(R1), LR89+ MOVD 16(R1), R0
90+ MOVD R0, LR
190 MOVD 8(R1), R091 MOVD 8(R1), R0
191 MOVW R0, CR92 MOVW R0, CR
192 RET93 RET

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