1// Copyright 2013 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// +build darwin dragonfly freebsd linux netbsd openbsd
6
7package runtime
8
9import (
10	"runtime/internal/sys"
11	"unsafe"
12)
13
14func dumpregs(c *sigctxt) {
15	print("eax    ", hex(c.eax()), "\n")
16	print("ebx    ", hex(c.ebx()), "\n")
17	print("ecx    ", hex(c.ecx()), "\n")
18	print("edx    ", hex(c.edx()), "\n")
19	print("edi    ", hex(c.edi()), "\n")
20	print("esi    ", hex(c.esi()), "\n")
21	print("ebp    ", hex(c.ebp()), "\n")
22	print("esp    ", hex(c.esp()), "\n")
23	print("eip    ", hex(c.eip()), "\n")
24	print("eflags ", hex(c.eflags()), "\n")
25	print("cs     ", hex(c.cs()), "\n")
26	print("fs     ", hex(c.fs()), "\n")
27	print("gs     ", hex(c.gs()), "\n")
28}
29
30//go:nosplit
31//go:nowritebarrierrec
32func (c *sigctxt) sigpc() uintptr { return uintptr(c.eip()) }
33
34func (c *sigctxt) sigsp() uintptr { return uintptr(c.esp()) }
35func (c *sigctxt) siglr() uintptr { return 0 }
36func (c *sigctxt) fault() uintptr { return uintptr(c.sigaddr()) }
37
38// preparePanic sets up the stack to look like a call to sigpanic.
39func (c *sigctxt) preparePanic(sig uint32, gp *g) {
40	if GOOS == "darwin" {
41		// Work around Leopard bug that doesn't set FPE_INTDIV.
42		// Look at instruction to see if it is a divide.
43		// Not necessary in Snow Leopard (si_code will be != 0).
44		if sig == _SIGFPE && gp.sigcode0 == 0 {
45			pc := (*[4]byte)(unsafe.Pointer(gp.sigpc))
46			i := 0
47			if pc[i] == 0x66 { // 16-bit instruction prefix
48				i++
49			}
50			if pc[i] == 0xF6 || pc[i] == 0xF7 {
51				gp.sigcode0 = _FPE_INTDIV
52			}
53		}
54	}
55
56	pc := uintptr(c.eip())
57	sp := uintptr(c.esp())
58
59	if shouldPushSigpanic(gp, pc, *(*uintptr)(unsafe.Pointer(sp))) {
60		c.pushCall(funcPC(sigpanic))
61	} else {
62		// Not safe to push the call. Just clobber the frame.
63		c.set_eip(uint32(funcPC(sigpanic)))
64	}
65}
66
67const pushCallSupported = true
68
69func (c *sigctxt) pushCall(targetPC uintptr) {
70	// Make it look like the signaled instruction called target.
71	pc := uintptr(c.eip())
72	sp := uintptr(c.esp())
73	sp -= sys.PtrSize
74	*(*uintptr)(unsafe.Pointer(sp)) = pc
75	c.set_esp(uint32(sp))
76	c.set_eip(uint32(targetPC))
77}
78