@@ -215,6 +215,82 @@ TEXT runtime·sigtramp(SB),NOSPLIT,$0
215215 POPQ BP
216216 RET
217217
218+ // Used instead of sigtramp in programs that use cgo.
219+ // Arguments from kernel are in DI, SI, DX.
220+ TEXT runtime·cgoSigtramp(SB),NOSPLIT,$0
221+ // If no traceback function, do usual sigtramp.
222+ MOVQ runtime·cgoTraceback(SB), AX
223+ TESTQ AX, AX
224+ JZ sigtramp
225+
226+ // If no traceback support function, which means that
227+ // runtime/cgo was not linked in, do usual sigtramp.
228+ MOVQ _cgo_callers(SB), AX
229+ TESTQ AX, AX
230+ JZ sigtramp
231+
232+ // Figure out if we are currently in a cgo call.
233+ // If not, just do usual sigtramp.
234+ get_tls(CX)
235+ MOVQ g(CX),AX
236+ TESTQ AX, AX
237+ JZ sigtrampnog // g == nil
238+ MOVQ g_m(AX), AX
239+ TESTQ AX, AX
240+ JZ sigtramp // g.m == nil
241+ MOVL m_ncgo(AX), CX
242+ TESTL CX, CX
243+ JZ sigtramp // g.m.ncgo == 0
244+ MOVQ m_curg(AX), CX
245+ TESTQ CX, CX
246+ JZ sigtramp // g.m.curg == nil
247+ MOVQ g_syscallsp(CX), CX
248+ TESTQ CX, CX
249+ JZ sigtramp // g.m.curg.syscallsp == 0
250+ MOVQ m_cgoCallers(AX), R8
251+ TESTQ R8, R8
252+ JZ sigtramp // g.m.cgoCallers == nil
253+ MOVL m_cgoCallersUse(AX), CX
254+ TESTL CX, CX
255+ JNZ sigtramp // g.m.cgoCallersUse != 0
256+
257+ // Jump to a function in runtime/cgo.
258+ // That function, written in C, will call the user's traceback
259+ // function with proper unwind info, and will then call back here.
260+ // The first three arguments, and the fifth, are already in registers.
261+ // Set the two remaining arguments now.
262+ MOVQ runtime·cgoTraceback(SB), CX
263+ MOVQ $runtime·sigtramp(SB), R9
264+ MOVQ _cgo_callers(SB), AX
265+ JMP AX
266+
267+ sigtramp:
268+ JMP runtime·sigtramp(SB)
269+
270+ sigtrampnog:
271+ // Signal arrived on a non-Go thread. If this is SIGPROF, get a
272+ // stack trace.
273+ CMPL DI, $27 // 27 == SIGPROF
274+ JNZ sigtramp
275+
276+ // Lock sigprofCallersUse.
277+ MOVL $0 , AX
278+ MOVL $1 , CX
279+ MOVQ $runtime·sigprofCallersUse(SB), R11
280+ LOCK
281+ CMPXCHGL CX, 0 (R11)
282+ JNZ sigtramp // Skip stack trace if already locked.
283+
284+ // Jump to the traceback function in runtime/cgo.
285+ // It will call back to sigprofNonGo, which will ignore the
286+ // arguments passed in registers.
287+ // First three arguments to traceback function are in registers already.
288+ MOVQ runtime·cgoTraceback(SB), CX
289+ MOVQ $runtime·sigprofCallers(SB), R8
290+ MOVQ $runtime·sigprofNonGo(SB), R9
291+ MOVQ _cgo_callers(SB), AX
292+ JMP AX
293+
218294TEXT runtime·mmap_trampoline(SB),NOSPLIT,$0
219295 PUSHQ BP // make a frame; keep stack aligned
220296 MOVQ SP, BP
0 commit comments