1// Copyright 2015 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#include "textflag.h"
6
7// xx_cgo_panicmem is the entrypoint for SIGSEGV as intercepted via a
8// mach thread port as EXC_BAD_ACCESS. As the segfault may have happened
9// in C code, we first need to load_g then call xx_cgo_panicmem.
10//
11//	R1 - LR at moment of fault
12//	R2 - PC at moment of fault
13TEXT xx_cgo_panicmem(SB),NOSPLIT|NOFRAME,$0
14	// If in external C code, we need to load the g register.
15	BL  runtime·load_g(SB)
16	CMP $0, g
17	BNE ongothread
18
19	// On a foreign thread.
20	// TODO(crawshaw): call badsignal
21	MOVD.W $0, -16(RSP)
22	MOVW $139, R1
23	MOVW R1, 8(RSP)
24	B    runtime·exit(SB)
25
26ongothread:
27	// Trigger a SIGSEGV panic.
28	//
29	// The goal is to arrange the stack so it looks like the runtime
30	// function sigpanic was called from the PC that faulted. It has
31	// to be sigpanic, as the stack unwinding code in traceback.go
32	// looks explicitly for it.
33	//
34	// To do this we call into runtime·setsigsegv, which sets the
35	// appropriate state inside the g object. We give it the faulting
36	// PC on the stack, then put it in the LR before calling sigpanic.
37
38	// Build a 32-byte stack frame for us for this call.
39	// Saved LR (none available) is at the bottom,
40	// then the PC argument for setsigsegv,
41	// then a copy of the LR for us to restore.
42	MOVD.W $0, -32(RSP)
43	MOVD R1, 8(RSP)
44	MOVD R2, 16(RSP)
45	BL runtime·setsigsegv(SB)
46	MOVD 8(RSP), R1
47	MOVD 16(RSP), R2
48
49	// Build a 16-byte stack frame for the simulated
50	// call to sigpanic, by taking 16 bytes away from the
51	// 32-byte stack frame above.
52	// The saved LR in this frame is the LR at time of fault,
53	// and the LR on entry to sigpanic is the PC at time of fault.
54	MOVD.W R1, 16(RSP)
55	MOVD R2, R30
56	B runtime·sigpanic(SB)
57