Skip to content

Commit

Permalink
runtime: add runtime defs in signal_linux_riscv.go
Browse files Browse the repository at this point in the history
Fixes golang#14

Change-Id: I84907cb5eb504bde4d740dc1b30e59ee9ef4dd6e
  • Loading branch information
ammubhave committed Nov 16, 2016
1 parent 79bf346 commit 7fb5bb5
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/runtime/defs_linux_riscv.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ type sigcontext struct {
sc_fpregs user_fpregs_struct
}

type sigaltstackt struct {
type stackt struct {
ss_sp *byte
ss_flags int32
pad_cgo_0 [4]byte
Expand All @@ -203,7 +203,7 @@ type sigaltstackt struct {
type ucontext struct {
uc_flags uint64
uc_link *ucontext
uc_stack sigaltstackt
uc_stack stackt
uc_mcontext sigcontext
uc_sigmask uint64
}
68 changes: 68 additions & 0 deletions src/runtime/signal_linux_riscv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package runtime

import (
"runtime/internal/sys"
"unsafe"
)

type sigctxt struct {
info *siginfo
ctxt unsafe.Pointer
}

//go:nosplit
//go:nowritebarrierrec
func (c *sigctxt) regs() *sigcontext { return &(*ucontext)(c.ctxt).uc_mcontext }

func (c *sigctxt) ra() uint64 { return c.regs().sc_regs.ra }
func (c *sigctxt) sp() uint64 { return c.regs().sc_regs.sp }
func (c *sigctxt) gp() uint64 { return c.regs().sc_regs.gp }
func (c *sigctxt) tp() uint64 { return c.regs().sc_regs.tp }
func (c *sigctxt) t0() uint64 { return c.regs().sc_regs.t0 }
func (c *sigctxt) t1() uint64 { return c.regs().sc_regs.t1 }
func (c *sigctxt) t2() uint64 { return c.regs().sc_regs.t2 }
func (c *sigctxt) s0() uint64 { return c.regs().sc_regs.s0 }
func (c *sigctxt) s1() uint64 { return c.regs().sc_regs.s1 }
func (c *sigctxt) a0() uint64 { return c.regs().sc_regs.a0 }
func (c *sigctxt) a1() uint64 { return c.regs().sc_regs.a1 }
func (c *sigctxt) a2() uint64 { return c.regs().sc_regs.a2 }
func (c *sigctxt) a3() uint64 { return c.regs().sc_regs.a3 }
func (c *sigctxt) a4() uint64 { return c.regs().sc_regs.a4 }
func (c *sigctxt) a5() uint64 { return c.regs().sc_regs.a5 }
func (c *sigctxt) a6() uint64 { return c.regs().sc_regs.a6 }
func (c *sigctxt) a7() uint64 { return c.regs().sc_regs.a7 }
func (c *sigctxt) s2() uint64 { return c.regs().sc_regs.s2 }
func (c *sigctxt) s3() uint64 { return c.regs().sc_regs.s3 }
func (c *sigctxt) s4() uint64 { return c.regs().sc_regs.s4 }
func (c *sigctxt) s5() uint64 { return c.regs().sc_regs.s5 }
func (c *sigctxt) s6() uint64 { return c.regs().sc_regs.s6 }
func (c *sigctxt) s7() uint64 { return c.regs().sc_regs.s7 }
func (c *sigctxt) s8() uint64 { return c.regs().sc_regs.s8 }
func (c *sigctxt) s9() uint64 { return c.regs().sc_regs.s9 }
func (c *sigctxt) s10() uint64 { return c.regs().sc_regs.s10 }
func (c *sigctxt) s11() uint64 { return c.regs().sc_regs.s11 }
func (c *sigctxt) t3() uint64 { return c.regs().sc_regs.t3 }
func (c *sigctxt) t4() uint64 { return c.regs().sc_regs.t4 }
func (c *sigctxt) t5() uint64 { return c.regs().sc_regs.t5 }
func (c *sigctxt) t6() uint64 { return c.regs().sc_regs.t6 }

//go:nosplit
//go:nowritebarrierrec
func (c *sigctxt) pc() uint64 { return c.regs().sc_regs.pc }

func (c *sigctxt) sigcode() uint32 { return uint32(c.info.si_code) }
func (c *sigctxt) sigaddr() uint64 { return c.info.si_addr }

func (c *sigctxt) set_pc(x uint64) { c.regs().sc_regs.pc = x }
func (c *sigctxt) set_ra(x uint64) { c.regs().sc_regs.ra = x }
func (c *sigctxt) set_sp(x uint64) { c.regs().sc_regs.sp = x }
func (c *sigctxt) set_gp(x uint64) { c.regs().sc_regs.gp = x }

func (c *sigctxt) set_sigcode(x uint32) { c.info.si_code = int32(x) }
func (c *sigctxt) set_sigaddr(x uint64) {
*(*uintptr)(add(unsafe.Pointer(c.info), 2*sys.PtrSize)) = uintptr(x)
}
90 changes: 90 additions & 0 deletions src/runtime/signal_riscv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build linux
// +build riscv

package runtime

import (
"runtime/internal/sys"
"unsafe"
)

func dumpregs(c *sigctxt) {
print("ra ", hex(c.ra()), "\t")
print("sp ", hex(c.sp()), "\n")
print("gp ", hex(c.gp()), "\t")
print("tp ", hex(c.tp()), "\n")
print("t0 ", hex(c.t0()), "\t")
print("t1 ", hex(c.t1()), "\n")
print("t2 ", hex(c.t2()), "\t")
print("s0 ", hex(c.s0()), "\n")
print("s1 ", hex(c.s1()), "\t")
print("a0 ", hex(c.a0()), "\n")
print("a1 ", hex(c.a1()), "\t")
print("a2 ", hex(c.a2()), "\n")
print("a3 ", hex(c.a3()), "\t")
print("a4 ", hex(c.a4()), "\n")
print("a5 ", hex(c.a5()), "\t")
print("a6 ", hex(c.a6()), "\n")
print("a7 ", hex(c.a7()), "\t")
print("s2 ", hex(c.s2()), "\n")
print("s3 ", hex(c.s3()), "\t")
print("s4 ", hex(c.s4()), "\n")
print("s5 ", hex(c.s5()), "\t")
print("s6 ", hex(c.s6()), "\n")
print("s7 ", hex(c.s7()), "\t")
print("s8 ", hex(c.s8()), "\n")
print("s9 ", hex(c.s9()), "\t")
print("s10 ", hex(c.s10()), "\n")
print("s11 ", hex(c.s11()), "\t")
print("t3 ", hex(c.t3()), "\n")
print("t4 ", hex(c.t4()), "\t")
print("t5 ", hex(c.t5()), "\n")
print("t6 ", hex(c.t6()), "\t")
print("pc ", hex(c.pc()), "\n")
}

//go:nosplit
//go:nowritebarrierrec
func (c *sigctxt) sigpc() uintptr { return uintptr(c.pc()) }

func (c *sigctxt) sigsp() uintptr { return uintptr(c.sp()) }
func (c *sigctxt) siglr() uintptr { return uintptr(c.ra()) }
func (c *sigctxt) fault() uintptr { return uintptr(c.sigaddr()) }

// preparePanic sets up the stack to look like a call to sigpanic.
func (c *sigctxt) preparePanic(sig uint32, gp *g) {
// We arrange RA, and pc to pretend the panicking
// function calls sigpanic directly.
// Always save RA to stack so that panics in leaf
// functions are correctly handled. This smashes
// the stack frame but we're not going back there
// anyway.
sp := c.sp() - sys.PtrSize
c.set_sp(sp)
*(*uint64)(unsafe.Pointer(uintptr(sp))) = c.ra()

pc := gp.sigpc

// If we don't recognize the PC as code
// but we do recognize the RA register as code,
// then assume this was a call to non-code and treat like
// pc == 0, to make unwinding show the context.
if pc != 0 && findfunc(pc) == nil && findfunc(uintptr(c.ra())) != nil {
pc = 0
}

// Don't bother saving PC if it's zero, which is
// probably a call to a nil func: the RA
// is more useful in the stack trace.
if pc != 0 {
c.set_ra(uint64(pc))
}

// In case we are panicking from external C code
c.set_gp(uint64(uintptr(unsafe.Pointer(gp))))
c.set_pc(uint64(funcPC(sigpanic)))
}

0 comments on commit 7fb5bb5

Please sign in to comment.