...

Source file src/runtime/signal_amd64.go

Documentation: runtime

		 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  //go:build amd64 && (darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris)
		 6  // +build amd64
		 7  // +build darwin dragonfly freebsd linux netbsd openbsd solaris
		 8  
		 9  package runtime
		10  
		11  import (
		12  	"runtime/internal/sys"
		13  	"unsafe"
		14  )
		15  
		16  func dumpregs(c *sigctxt) {
		17  	print("rax		", hex(c.rax()), "\n")
		18  	print("rbx		", hex(c.rbx()), "\n")
		19  	print("rcx		", hex(c.rcx()), "\n")
		20  	print("rdx		", hex(c.rdx()), "\n")
		21  	print("rdi		", hex(c.rdi()), "\n")
		22  	print("rsi		", hex(c.rsi()), "\n")
		23  	print("rbp		", hex(c.rbp()), "\n")
		24  	print("rsp		", hex(c.rsp()), "\n")
		25  	print("r8		 ", hex(c.r8()), "\n")
		26  	print("r9		 ", hex(c.r9()), "\n")
		27  	print("r10		", hex(c.r10()), "\n")
		28  	print("r11		", hex(c.r11()), "\n")
		29  	print("r12		", hex(c.r12()), "\n")
		30  	print("r13		", hex(c.r13()), "\n")
		31  	print("r14		", hex(c.r14()), "\n")
		32  	print("r15		", hex(c.r15()), "\n")
		33  	print("rip		", hex(c.rip()), "\n")
		34  	print("rflags ", hex(c.rflags()), "\n")
		35  	print("cs		 ", hex(c.cs()), "\n")
		36  	print("fs		 ", hex(c.fs()), "\n")
		37  	print("gs		 ", hex(c.gs()), "\n")
		38  }
		39  
		40  //go:nosplit
		41  //go:nowritebarrierrec
		42  func (c *sigctxt) sigpc() uintptr { return uintptr(c.rip()) }
		43  
		44  func (c *sigctxt) sigsp() uintptr { return uintptr(c.rsp()) }
		45  func (c *sigctxt) siglr() uintptr { return 0 }
		46  func (c *sigctxt) fault() uintptr { return uintptr(c.sigaddr()) }
		47  
		48  // preparePanic sets up the stack to look like a call to sigpanic.
		49  func (c *sigctxt) preparePanic(sig uint32, gp *g) {
		50  	// Work around Leopard bug that doesn't set FPE_INTDIV.
		51  	// Look at instruction to see if it is a divide.
		52  	// Not necessary in Snow Leopard (si_code will be != 0).
		53  	if GOOS == "darwin" && sig == _SIGFPE && gp.sigcode0 == 0 {
		54  		pc := (*[4]byte)(unsafe.Pointer(gp.sigpc))
		55  		i := 0
		56  		if pc[i]&0xF0 == 0x40 { // 64-bit REX prefix
		57  			i++
		58  		} else if pc[i] == 0x66 { // 16-bit instruction prefix
		59  			i++
		60  		}
		61  		if pc[i] == 0xF6 || pc[i] == 0xF7 {
		62  			gp.sigcode0 = _FPE_INTDIV
		63  		}
		64  	}
		65  
		66  	pc := uintptr(c.rip())
		67  	sp := uintptr(c.rsp())
		68  
		69  	// In case we are panicking from external code, we need to initialize
		70  	// Go special registers. We inject sigpanic0 (instead of sigpanic),
		71  	// which takes care of that.
		72  	if shouldPushSigpanic(gp, pc, *(*uintptr)(unsafe.Pointer(sp))) {
		73  		c.pushCall(funcPC(sigpanic0), pc)
		74  	} else {
		75  		// Not safe to push the call. Just clobber the frame.
		76  		c.set_rip(uint64(funcPC(sigpanic0)))
		77  	}
		78  }
		79  
		80  func (c *sigctxt) pushCall(targetPC, resumePC uintptr) {
		81  	// Make it look like we called target at resumePC.
		82  	sp := uintptr(c.rsp())
		83  	sp -= sys.PtrSize
		84  	*(*uintptr)(unsafe.Pointer(sp)) = resumePC
		85  	c.set_rsp(uint64(sp))
		86  	c.set_rip(uint64(targetPC))
		87  }
		88  

View as plain text