1 /* go-signal.c -- signal handling for Go.
3 Copyright 2009 The Go Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style
5 license that can be found in the LICENSE file. */
19 #ifdef USING_SPLIT_STACK
21 extern void __splitstack_getcontext(void *context
[10]);
23 extern void __splitstack_setcontext(void *context
[10]);
25 extern void *__splitstack_find_context(void *context
[10], size_t *,
26 void **, void **, void **);
30 // The rest of the signal handler, written in Go.
32 extern void sigtrampgo(uint32
, siginfo_t
*, void *)
33 __asm__(GOSYM_PREFIX
"runtime.sigtrampgo");
35 // The Go signal handler, written in C. This should be running on the
36 // alternate signal stack. This is responsible for setting up the
37 // split stack context so that stack guard checks will work as
40 void sigtramp(int, siginfo_t
*, void *)
41 __attribute__ ((no_split_stack
));
43 void sigtramp(int, siginfo_t
*, void *)
44 __asm__ (GOSYM_PREFIX
"runtime.sigtramp");
46 #ifndef USING_SPLIT_STACK
48 // When not using split stacks, there are no stack checks, and there
49 // is nothing special for this function to do.
52 sigtramp(int sig
, siginfo_t
*info
, void *context
)
54 sigtrampgo(sig
, info
, context
);
57 #else // USING_SPLIT_STACK
60 sigtramp(int sig
, siginfo_t
*info
, void *context
)
63 void *stack_context
[10];
76 // Let the Go code handle this case.
77 // It should only call nosplit functions in this case.
78 sigtrampgo(sig
, info
, context
);
82 // If this signal is one for which we will panic, we are not
83 // on the alternate signal stack. It's OK to call split-stack
85 if (sig
== SIGBUS
|| sig
== SIGFPE
|| sig
== SIGSEGV
) {
86 sigtrampgo(sig
, info
, context
);
90 // We are running on the alternate signal stack.
92 __splitstack_getcontext(&stack_context
[0]);
94 stack
= __splitstack_find_context((void*)(&gp
->m
->gsignal
->stackcontext
[0]),
95 &stack_size
, &next_segment
,
96 &next_sp
, &initial_sp
);
98 // If some non-Go code called sigaltstack, adjust.
99 sp
= (uintptr
)(&stack_size
);
100 if (sp
< (uintptr
)(stack
) || sp
>= (uintptr
)(stack
) + stack_size
) {
101 sigaltstack(nil
, &st
);
102 if ((st
.ss_flags
& SS_DISABLE
) != 0) {
103 runtime_printf("signal %d received on thread with no signal stack\n", (int32
)(sig
));
104 runtime_throw("non-Go code disabled sigaltstack");
107 stsp
= (uintptr
)(st
.ss_sp
);
108 if (sp
< stsp
|| sp
>= stsp
+ st
.ss_size
) {
109 runtime_printf("signal %d received but handler not on signal stack\n", (int32
)(sig
));
110 runtime_throw("non-Go code set up signal handler without SA_ONSTACK flag");
113 // Unfortunately __splitstack_find_context will return NULL
114 // when it is called on a context that has never been used.
115 // There isn't much we can do but assume all is well.
117 // Here the gc runtime adjusts the gsignal
118 // stack guard to match the values returned by
119 // sigaltstack. Unfortunately we have no way
121 runtime_printf("signal %d received on unknown signal stack\n", (int32
)(sig
));
122 runtime_throw("non-Go code changed signal stack");
126 // Set the split stack context so that the stack guards are
127 // checked correctly.
129 __splitstack_setcontext((void*)(&gp
->m
->gsignal
->stackcontext
[0]));
131 sigtrampgo(sig
, info
, context
);
133 // We are going to return back to the signal trampoline and
134 // then to whatever we were doing before we got the signal.
135 // Restore the split stack context so that stack guards are
136 // checked correctly.
138 __splitstack_setcontext(&stack_context
[0]);
141 #endif // USING_SPLIT_STACK
143 // C function to return the address of the sigtramp function.
144 uintptr
getSigtramp(void) __asm__ (GOSYM_PREFIX
"runtime.getSigtramp");
149 return (uintptr
)(void*)sigtramp
;
152 // C code to manage the sigaction sa_sigaction field, which is
153 // typically a union and so hard for mksysinfo.sh to handle.
155 uintptr
getSigactionHandler(struct sigaction
*)
156 __attribute__ ((no_split_stack
));
158 uintptr
getSigactionHandler(struct sigaction
*)
159 __asm__ (GOSYM_PREFIX
"runtime.getSigactionHandler");
162 getSigactionHandler(struct sigaction
* sa
)
164 return (uintptr
)(sa
->sa_sigaction
);
167 void setSigactionHandler(struct sigaction
*, uintptr
)
168 __attribute__ ((no_split_stack
));
170 void setSigactionHandler(struct sigaction
*, uintptr
)
171 __asm__ (GOSYM_PREFIX
"runtime.setSigactionHandler");
174 setSigactionHandler(struct sigaction
* sa
, uintptr handler
)
176 sa
->sa_sigaction
= (void*)(handler
);
179 // C code to fetch values from the siginfo_t and ucontext_t pointers
180 // passed to a signal handler.
182 uintptr
getSiginfoCode(siginfo_t
*)
183 __attribute__ ((no_split_stack
));
185 uintptr
getSiginfoCode(siginfo_t
*)
186 __asm__ (GOSYM_PREFIX
"runtime.getSiginfoCode");
189 getSiginfoCode(siginfo_t
*info
)
191 return (uintptr
)(info
->si_code
);
194 struct getSiginfoRet
{
199 struct getSiginfoRet
getSiginfo(siginfo_t
*, void *)
200 __asm__(GOSYM_PREFIX
"runtime.getSiginfo");
203 getSiginfo(siginfo_t
*info
, void *context
__attribute__((unused
)))
205 struct getSiginfoRet ret
;
212 ret
.sigaddr
= (uintptr
)(info
->si_addr
);
216 // There doesn't seem to be a portable way to get the PC.
217 // Use unportable code to pull it from context, and if that fails
218 // try a stack backtrace across the signal handler.
220 #if defined(__x86_64__) && defined(__linux__)
221 ret
.sigpc
= ((ucontext_t
*)(context
))->uc_mcontext
.gregs
[REG_RIP
];
222 #elif defined(__i386__) && defined(__linux__)
223 ret
.sigpc
= ((ucontext_t
*)(context
))->uc_mcontext
.gregs
[REG_EIP
];
224 #elif defined(__alpha__) && defined(__linux__)
225 ret
.sigpc
= ((ucontext_t
*)(context
))->uc_mcontext
.sc_pc
;
226 #elif defined(__PPC__) && defined(__linux__)
227 ret
.sigpc
= ((ucontext_t
*)(context
))->uc_mcontext
.regs
->nip
;
228 #elif defined(__PPC__) && defined(_AIX)
229 ret
.sigpc
= ((ucontext_t
*)(context
))->uc_mcontext
.jmp_context
.iar
;
230 #elif defined(__aarch64__) && defined(__linux__)
231 ret
.sigpc
= ((ucontext_t
*)(context
))->uc_mcontext
.pc
;
232 #elif defined(__NetBSD__)
233 ret
.sigpc
= _UC_MACHINE_PC(((ucontext_t
*)(context
)));
236 if (ret
.sigpc
== 0) {
237 // Skip getSiginfo/sighandler/sigtrampgo/sigtramp/handler.
238 n
= runtime_callers(5, &loc
[0], 1, false);
240 ret
.sigpc
= loc
[0].pc
;
247 // Dump registers when crashing in a signal.
248 // There is no portable way to write this,
249 // so we just have some CPU/OS specific implementations.
251 void dumpregs(siginfo_t
*, void *)
252 __asm__(GOSYM_PREFIX
"runtime.dumpregs");
255 dumpregs(siginfo_t
*info
__attribute__((unused
)), void *context
__attribute__((unused
)))
257 #if defined(__x86_64__) && defined(__linux__)
259 mcontext_t
*m
= &((ucontext_t
*)(context
))->uc_mcontext
;
261 runtime_printf("rax %X\n", m
->gregs
[REG_RAX
]);
262 runtime_printf("rbx %X\n", m
->gregs
[REG_RBX
]);
263 runtime_printf("rcx %X\n", m
->gregs
[REG_RCX
]);
264 runtime_printf("rdx %X\n", m
->gregs
[REG_RDX
]);
265 runtime_printf("rdi %X\n", m
->gregs
[REG_RDI
]);
266 runtime_printf("rsi %X\n", m
->gregs
[REG_RSI
]);
267 runtime_printf("rbp %X\n", m
->gregs
[REG_RBP
]);
268 runtime_printf("rsp %X\n", m
->gregs
[REG_RSP
]);
269 runtime_printf("r8 %X\n", m
->gregs
[REG_R8
]);
270 runtime_printf("r9 %X\n", m
->gregs
[REG_R9
]);
271 runtime_printf("r10 %X\n", m
->gregs
[REG_R10
]);
272 runtime_printf("r11 %X\n", m
->gregs
[REG_R11
]);
273 runtime_printf("r12 %X\n", m
->gregs
[REG_R12
]);
274 runtime_printf("r13 %X\n", m
->gregs
[REG_R13
]);
275 runtime_printf("r14 %X\n", m
->gregs
[REG_R14
]);
276 runtime_printf("r15 %X\n", m
->gregs
[REG_R15
]);
277 runtime_printf("rip %X\n", m
->gregs
[REG_RIP
]);
278 runtime_printf("rflags %X\n", m
->gregs
[REG_EFL
]);
279 runtime_printf("cs %X\n", m
->gregs
[REG_CSGSFS
] & 0xffff);
280 runtime_printf("fs %X\n", (m
->gregs
[REG_CSGSFS
] >> 16) & 0xffff);
281 runtime_printf("gs %X\n", (m
->gregs
[REG_CSGSFS
] >> 32) & 0xffff);
283 #elif defined(__i386__) && defined(__linux__)
285 mcontext_t
*m
= &((ucontext_t
*)(context
))->uc_mcontext
;
287 runtime_printf("eax %x\n", m
->gregs
[REG_EAX
]);
288 runtime_printf("ebx %x\n", m
->gregs
[REG_EBX
]);
289 runtime_printf("ecx %x\n", m
->gregs
[REG_ECX
]);
290 runtime_printf("edx %x\n", m
->gregs
[REG_EDX
]);
291 runtime_printf("edi %x\n", m
->gregs
[REG_EDI
]);
292 runtime_printf("esi %x\n", m
->gregs
[REG_ESI
]);
293 runtime_printf("ebp %x\n", m
->gregs
[REG_EBP
]);
294 runtime_printf("esp %x\n", m
->gregs
[REG_ESP
]);
295 runtime_printf("eip %x\n", m
->gregs
[REG_EIP
]);
296 runtime_printf("eflags %x\n", m
->gregs
[REG_EFL
]);
297 runtime_printf("cs %x\n", m
->gregs
[REG_CS
]);
298 runtime_printf("fs %x\n", m
->gregs
[REG_FS
]);
299 runtime_printf("gs %x\n", m
->gregs
[REG_GS
]);
301 #elif defined(__alpha__) && defined(__linux__)
303 mcontext_t
*m
= &((ucontext_t
*)(context
))->uc_mcontext
;
305 runtime_printf("v0 %X\n", m
->sc_regs
[0]);
306 runtime_printf("t0 %X\n", m
->sc_regs
[1]);
307 runtime_printf("t1 %X\n", m
->sc_regs
[2]);
308 runtime_printf("t2 %X\n", m
->sc_regs
[3]);
309 runtime_printf("t3 %X\n", m
->sc_regs
[4]);
310 runtime_printf("t4 %X\n", m
->sc_regs
[5]);
311 runtime_printf("t5 %X\n", m
->sc_regs
[6]);
312 runtime_printf("t6 %X\n", m
->sc_regs
[7]);
313 runtime_printf("t7 %X\n", m
->sc_regs
[8]);
314 runtime_printf("s0 %X\n", m
->sc_regs
[9]);
315 runtime_printf("s1 %X\n", m
->sc_regs
[10]);
316 runtime_printf("s2 %X\n", m
->sc_regs
[11]);
317 runtime_printf("s3 %X\n", m
->sc_regs
[12]);
318 runtime_printf("s4 %X\n", m
->sc_regs
[13]);
319 runtime_printf("s5 %X\n", m
->sc_regs
[14]);
320 runtime_printf("fp %X\n", m
->sc_regs
[15]);
321 runtime_printf("a0 %X\n", m
->sc_regs
[16]);
322 runtime_printf("a1 %X\n", m
->sc_regs
[17]);
323 runtime_printf("a2 %X\n", m
->sc_regs
[18]);
324 runtime_printf("a3 %X\n", m
->sc_regs
[19]);
325 runtime_printf("a4 %X\n", m
->sc_regs
[20]);
326 runtime_printf("a5 %X\n", m
->sc_regs
[21]);
327 runtime_printf("t8 %X\n", m
->sc_regs
[22]);
328 runtime_printf("t9 %X\n", m
->sc_regs
[23]);
329 runtime_printf("t10 %X\n", m
->sc_regs
[24]);
330 runtime_printf("t11 %X\n", m
->sc_regs
[25]);
331 runtime_printf("ra %X\n", m
->sc_regs
[26]);
332 runtime_printf("t12 %X\n", m
->sc_regs
[27]);
333 runtime_printf("at %X\n", m
->sc_regs
[28]);
334 runtime_printf("gp %X\n", m
->sc_regs
[29]);
335 runtime_printf("sp %X\n", m
->sc_regs
[30]);
336 runtime_printf("pc %X\n", m
->sc_pc
);
338 #elif defined(__PPC__) && defined(__LITTLE_ENDIAN__) && defined(__linux__)
340 mcontext_t
*m
= &((ucontext_t
*)(context
))->uc_mcontext
;
343 for (i
= 0; i
< 32; i
++)
344 runtime_printf("r%d %X\n", i
, m
->regs
->gpr
[i
]);
345 runtime_printf("pc %X\n", m
->regs
->nip
);
346 runtime_printf("msr %X\n", m
->regs
->msr
);
347 runtime_printf("cr %X\n", m
->regs
->ccr
);
348 runtime_printf("lr %X\n", m
->regs
->link
);
349 runtime_printf("ctr %X\n", m
->regs
->ctr
);
350 runtime_printf("xer %X\n", m
->regs
->xer
);
352 #elif defined(__PPC__) && defined(_AIX)
354 mcontext_t
*m
= &((ucontext_t
*)(context
))->uc_mcontext
;
357 for (i
= 0; i
< 32; i
++)
358 runtime_printf("r%d %p\n", i
, m
->jmp_context
.gpr
[i
]);
359 runtime_printf("pc %p\n", m
->jmp_context
.iar
);
360 runtime_printf("msr %p\n", m
->jmp_context
.msr
);
361 runtime_printf("cr %x\n", m
->jmp_context
.cr
);
362 runtime_printf("lr %p\n", m
->jmp_context
.lr
);
363 runtime_printf("ctr %p\n", m
->jmp_context
.ctr
);
364 runtime_printf("xer %x\n", m
->jmp_context
.xer
);
366 #elif defined(__aarch64__) && defined(__linux__)
368 mcontext_t
*m
= &((ucontext_t
*)(context
))->uc_mcontext
;
371 for (i
= 0; i
< 31; i
++)
372 runtime_printf("x%d %X\n", i
, m
->regs
[i
]);
373 runtime_printf("sp %X\n", m
->sp
);
374 runtime_printf("pc %X\n", m
->pc
);
375 runtime_printf("pstate %X\n", m
->pstate
);