Text file
src/runtime/cgo/asm_unix_arm64.s
1 // Copyright 2026 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build goexperiment.runtimecgopurego && !msan && !tsan && unix && arm64
6
7 #include "go_asm.h"
8 #include "textflag.h"
9 #include "abi_arm64.h"
10
11 // cgoCallTrampoline is called by runtime.asmcgocall (or runtime.cgocall)
12 // using the AAPCS64 C ABI with R0 pointing to a cgoCallFrame.
13 TEXT ·cgoCallTrampoline(SB),NOSPLIT|NOFRAME,$0-0
14 // Go unwinders expect the saved LR at SP as soon as SP changes.
15 // The C frame pointer needs a separate (FP, LR) record.
16 STP.W (R30, R19), -32(RSP)
17 STP (R29, R30), 16(RSP)
18 ADD $16, RSP, R29
19 MOVD R0, R19
20 MOVD cgoCallFrame_fn(R0), R9
21 MOVD cgoCallFrame_a1(R0), R1
22 MOVD cgoCallFrame_a2(R0), R2
23 MOVD cgoCallFrame_a3(R0), R3
24 MOVD cgoCallFrame_a4(R0), R4
25 MOVD cgoCallFrame_a5(R0), R5
26 MOVD cgoCallFrame_a0(R0), R0
27 BL (R9)
28 MOVD R0, cgoCallFrame_r1(R19)
29 MOVD $0, R0
30 MOVD 16(RSP), R29
31 LDP.P 32(RSP), (R30, R19)
32 RET
33
34 #define CGO_PROLOGUE \
35 SUB $208, RSP; \
36 SAVE_R19_TO_R28(8*6); \
37 SAVE_F8_TO_F15(8*16); \
38 STP (R29, R30), (8*24)(RSP); \
39 ADD $(8*24), RSP, R29; \
40 MOVD ZR, g
41
42 #define CGO_EPILOGUE \
43 RESTORE_R19_TO_R28(8*6); \
44 RESTORE_F8_TO_F15(8*16); \
45 LDP (8*24)(RSP), (R29, R30); \
46 ADD $208, RSP
47
48 // Indirectly call Go targets via R9 to avoid the linker's static NOSPLIT
49 // stack overflow check: these entrypoints are only invoked from C on a
50 // full-size C/pthread stack after saving host registers in CGO_PROLOGUE.
51 #define CALL_GO(fn) \
52 MOVD $fn(SB), R9; \
53 BL (R9)
54
55 TEXT _cgo_wait_runtime_init_done(SB),NOSPLIT|NOFRAME,$0-0
56 // Save R27 in caller-saved R9: global accesses below clobber R27
57 // (callee-saved in the C ABI) under -dynlink.
58 MOVD R27, R9
59 MOVD $·runtime_init_done(SB), R1
60 LDARW (R1), R1
61 CMPW $2, R1
62 BNE slow
63 MOVD $·cgo_context_function(SB), R0
64 LDAR (R0), R0
65 MOVD R9, R27
66 CBNZ R0, callctx
67 RET
68 slow:
69 MOVD R9, R27
70 CGO_PROLOGUE
71 CALL_GO(·cgoWaitRuntimeInit)
72 MOVD 8(RSP), R0
73 CGO_EPILOGUE
74 CBNZ R0, callctx
75 RET
76 callctx:
77 B ·x_cgo_call_context(SB)
78
79 // x_cgo_call_context calls the registered cgo context function (passed in R0).
80 // cmd/link emits static unwind info matching the standard frame-pointer prologue
81 // so callbacks using _Unwind_Backtrace unwind cleanly into the C caller.
82 TEXT ·x_cgo_call_context(SB),NOSPLIT|NOFRAME,$0
83 STP.W (R29, R30), -16(RSP)
84 MOVD RSP, R29
85 SUB $16, RSP
86
87 MOVD R0, R9 // pfn
88 MOVD ZR, 0(RSP) // arg.context = 0
89 MOVD RSP, R0 // &arg
90 BL (R9)
91
92 MOVD 0(RSP), R0 // return arg.context
93 MOVD R29, RSP
94 LDP.P 16(RSP), (R29, R30)
95 RET
96
97 TEXT _cgo_release_context(SB),NOSPLIT|NOFRAME,$0-0
98 CBZ R0, done
99 CGO_PROLOGUE
100 MOVD R0, 8(RSP)
101 CALL_GO(·cgoReleaseContext)
102 CGO_EPILOGUE
103 done:
104 RET
105
106 TEXT ·pthreadKeyDestructor(SB),NOSPLIT|NOFRAME,$0-0
107 MOVD R0, R1
108 MOVD $0, R0
109 MOVD $0, R3
110 JMP crosscall2(SB)
111
112 TEXT ·x_cgo_threadentry(SB),NOSPLIT|NOFRAME,$0-0
113 CGO_PROLOGUE
114 CALL_GO(·threadentry<ABIInternal>)
115 CGO_EPILOGUE
116 MOVD $0, R0
117 RET
118
119 TEXT x_cgo_init<>(SB),NOSPLIT|NOFRAME,$0-0
120 CGO_PROLOGUE
121 STP (R0, R1), 8(RSP)
122 STP (R2, R3), 24(RSP)
123 CALL_GO(·cgoInit)
124 CGO_EPILOGUE
125 RET
126
127 GLOBL _cgo_init(SB), NOPTR, $8
128 DATA _cgo_init+0(SB)/8, $x_cgo_init<>(SB)
129
130 // x_cgo_callers is jumped to from runtime.cgoSigtramp on the signal stack.
131 // cmd/link emits static unwind info matching the standard frame-pointer prologue
132 // so traceback callbacks unwind cleanly into the OS signal trampoline.
133 TEXT ·x_cgo_callers(SB),NOSPLIT|NOFRAME,$0
134 STP.W (R29, R30), -16(RSP)
135 MOVD RSP, R29
136 SUB $64, RSP
137
138 STP (ZR, R2), 0(RSP) // arg.context = 0, arg.sigContext = context
139 MOVD $32, R6
140 STP (R4, R6), 16(RSP) // arg.buf = cgoCallers, arg.max = 32
141
142 STP (R0, R1), 32(RSP) // save sig, info
143 STP (R2, R5), 48(RSP) // save context, sigtramp
144
145 MOVD RSP, R0 // &arg
146 BL (R3) // (*cgoTraceback)(&arg)
147
148 LDP 32(RSP), (R0, R1) // restore sig, info
149 LDP 48(RSP), (R2, R5) // restore context, sigtramp
150
151 MOVD R29, RSP
152 LDP.P 16(RSP), (R29, R30)
153 B (R5) // sigtramp(sig, info, context)
154
155 GLOBL _cgo_callers(SB), NOPTR, $8
156 DATA _cgo_callers+0(SB)/8, $·x_cgo_callers(SB)
157
View as plain text