1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
package native
import (
"bytes"
"debug/elf"
"fmt"
"syscall"
"unsafe"
sys "golang.org/x/sys/unix"
"github.com/go-delve/delve/pkg/proc"
"github.com/go-delve/delve/pkg/proc/linutil"
"golang.org/x/arch/riscv64/riscv64asm"
)
func (thread *nativeThread) fpRegisters() ([]proc.Register, []byte, error) {
var err error
var riscv64_fpregs linutil.RISCV64PtraceFpRegs
thread.dbp.execPtraceFunc(func() { err = ptraceGetFpRegset(thread.ID, &riscv64_fpregs) })
fpregs := riscv64_fpregs.Decode()
if err != nil {
err = fmt.Errorf("could not get floating point registers: %v", err.Error())
}
return fpregs, riscv64_fpregs.Fregs, err
}
func (t *nativeThread) restoreRegisters(savedRegs proc.Registers) error {
var restoreRegistersErr error
sr := savedRegs.(*linutil.RISCV64Registers)
t.dbp.execPtraceFunc(func() {
restoreRegistersErr = ptraceSetGRegs(t.ID, sr.Regs)
if restoreRegistersErr != syscall.Errno(0) {
return
}
if sr.Fpregset != nil {
iov := sys.Iovec{Base: &sr.Fpregset[0], Len: uint64(len(sr.Fpregset))}
_, _, restoreRegistersErr = syscall.Syscall6(syscall.SYS_PTRACE, sys.PTRACE_SETREGSET, uintptr(t.ID), uintptr(elf.NT_FPREGSET), uintptr(unsafe.Pointer(&iov)), 0, 0)
}
})
if restoreRegistersErr == syscall.Errno(0) {
restoreRegistersErr = nil
}
return restoreRegistersErr
}
// resolvePC is used to resolve next PC for current instruction.
func (t *nativeThread) resolvePC(savedRegs proc.Registers) ([]uint64, error) {
regs := savedRegs.(*linutil.RISCV64Registers)
nextInstLen := t.BinInfo().Arch.MaxInstructionLength()
nextInstBytes := make([]byte, nextInstLen)
var err error
t.dbp.execPtraceFunc(func() {
_, err = sys.PtracePeekData(t.ID, uintptr(regs.PC()), nextInstBytes)
})
if err != nil {
return nil, err
}
nextPCs := []uint64{regs.PC() + uint64(nextInstLen)}
if bytes.Equal(nextInstBytes, t.BinInfo().Arch.AltBreakpointInstruction()) {
return nextPCs, nil
} else if bytes.Equal(nextInstBytes, t.BinInfo().Arch.BreakpointInstruction()) {
nextInstLen = 2
nextPCs = []uint64{regs.PC() + uint64(nextInstLen)}
return nextPCs, nil
}
nextInst, err := riscv64asm.Decode(nextInstBytes)
if err != nil {
return nil, err
}
if nextInst.Len == 2 {
nextInstBytes = nextInstBytes[:2]
nextInstLen = 2
nextPCs = []uint64{regs.PC() + uint64(nextInstLen)}
}
switch nextInst.Op {
case riscv64asm.BEQ, riscv64asm.BNE, riscv64asm.BLT, riscv64asm.BGE, riscv64asm.BLTU, riscv64asm.BGEU:
rs1, _ := nextInst.Args[0].(riscv64asm.Reg)
rs2, _ := nextInst.Args[1].(riscv64asm.Reg)
bimm12, _ := nextInst.Args[2].(riscv64asm.Simm)
src1u, _ := regs.GetReg(uint64(rs1))
src2u, _ := regs.GetReg(uint64(rs2))
src1, src2 := int64(src1u), int64(src2u)
switch nextInst.Op {
case riscv64asm.BEQ:
if src1 == src2 && int(bimm12.Imm) != nextInstLen {
nextPCs = append(nextPCs, regs.PC()+uint64(bimm12.Imm))
}
case riscv64asm.BNE:
if src1 != src2 && int(bimm12.Imm) != nextInstLen {
nextPCs = append(nextPCs, regs.PC()+uint64(bimm12.Imm))
}
case riscv64asm.BLT:
if src1 < src2 && int(bimm12.Imm) != nextInstLen {
nextPCs = append(nextPCs, regs.PC()+uint64(bimm12.Imm))
}
case riscv64asm.BGE:
if src1 >= src2 && int(bimm12.Imm) != nextInstLen {
nextPCs = append(nextPCs, regs.PC()+uint64(bimm12.Imm))
}
case riscv64asm.BLTU:
if src1u < src2u && int(bimm12.Imm) != nextInstLen {
nextPCs = append(nextPCs, regs.PC()+uint64(bimm12.Imm))
}
case riscv64asm.BGEU:
if src1u >= src2u && int(bimm12.Imm) != nextInstLen {
nextPCs = append(nextPCs, regs.PC()+uint64(bimm12.Imm))
}
}
case riscv64asm.JAL:
jimm, _ := nextInst.Args[1].(riscv64asm.Simm)
if int(jimm.Imm) != nextInstLen {
nextPCs = append(nextPCs, regs.PC()+uint64(jimm.Imm))
}
case riscv64asm.JALR:
rd, _ := nextInst.Args[0].(riscv64asm.Reg)
rs1_mem := nextInst.Args[1].(riscv64asm.RegOffset)
rs1, ofs := rs1_mem.OfsReg, rs1_mem.Ofs
src1, _ := regs.GetReg(uint64(rs1))
if rd == riscv64asm.X0 && rs1 == riscv64asm.X1 {
nextPCs = []uint64{(src1 + uint64(ofs.Imm)) & (^uint64(0x1))}
}
if (src1+uint64(ofs.Imm))&(^uint64(0x1)) != nextPCs[0] {
nextPCs = append(nextPCs, (src1+uint64(ofs.Imm))&(^uint64(0x1)))
}
// We can't put a breakpoint in the middle of a lr/sc atomic sequence, so look for the end of the sequence and put the breakpoint there.
// RISC-V Go only use lr.w/d.aq, see comments at the beginning of $GOROOT/src/runtime/internal/atomic/atomic_riscv64.s
case riscv64asm.LR_D_AQ, riscv64asm.LR_W_AQ:
// Currently, RISC-V Go only use this kind of lr/sc sequence, so only check this pattern.
// defined in $GOROOT/src/cmd/compile/internal/riscv64/ssa.go:
// LR (Rarg0), Rtmp
// BNE Rtmp, Rarg1, 3(PC)
// SC Rarg2, (Rarg0), Rtmp
// BNE Rtmp, ZERO, -3(PC)
curPC := regs.PC() + uint64(nextInstLen)
t.dbp.execPtraceFunc(func() {
_, err = sys.PtracePeekData(t.ID, uintptr(curPC), nextInstBytes)
})
if err != nil {
return nil, err
}
nextInst, err = riscv64asm.Decode(nextInstBytes)
if err != nil {
return nil, err
}
if nextInst.Len == 2 {
nextInstBytes = nextInstBytes[:2]
}
if nextInst.Op != riscv64asm.BNE {
break
}
curPC += uint64(nextInstLen)
t.dbp.execPtraceFunc(func() {
_, err = sys.PtracePeekData(t.ID, uintptr(curPC), nextInstBytes)
})
if err != nil {
return nil, err
}
nextInst, err = riscv64asm.Decode(nextInstBytes)
if err != nil {
return nil, err
}
if nextInst.Len == 2 {
nextInstBytes = nextInstBytes[:2]
}
if nextInst.Op != riscv64asm.SC_D_RL && nextInst.Op != riscv64asm.SC_W_RL {
break
}
curPC += uint64(nextInstLen)
t.dbp.execPtraceFunc(func() {
_, err = sys.PtracePeekData(t.ID, uintptr(regs.PC()+uint64(curPC)), nextInstBytes)
})
if err != nil {
return nil, err
}
nextInst, err = riscv64asm.Decode(nextInstBytes)
if err != nil {
return nil, err
}
if nextInst.Len == 2 {
nextInstBytes = nextInstBytes[:2]
}
if nextInst.Op != riscv64asm.BNE {
break
}
nextPCs = []uint64{curPC}
}
return nextPCs, nil
}
// RISC-V doesn't have ptrace singlestep support, so use breakpoint to emulate it.
func (procgrp *processGroup) singleStep(t *nativeThread) (err error) {
regs, err := t.Registers()
if err != nil {
return err
}
nextPCs, err := t.resolvePC(regs)
if err != nil {
return err
}
originalDataSet := make(map[uintptr][]byte)
// Do in batch, first set breakpoint, then continue.
t.dbp.execPtraceFunc(func() {
breakpointInstr := t.BinInfo().Arch.BreakpointInstruction()
readWriteMem := func(i int, addr uintptr, instr []byte) error {
originalData := make([]byte, len(breakpointInstr))
_, err = sys.PtracePeekData(t.ID, addr, originalData)
if err != nil {
return err
}
_, err = sys.PtracePokeData(t.ID, addr, instr)
if err != nil {
return err
}
// Everything is ok, store originalData
originalDataSet[addr] = originalData
return nil
}
for i, nextPC := range nextPCs {
err = readWriteMem(i, uintptr(nextPC), breakpointInstr)
if err != nil {
return
}
}
})
// Make sure we restore before return.
defer func() {
t.dbp.execPtraceFunc(func() {
for addr, originalData := range originalDataSet {
if originalData != nil {
_, err = sys.PtracePokeData(t.ID, addr, originalData)
}
}
})
}()
if err != nil {
return err
}
for {
sig := 0
t.dbp.execPtraceFunc(func() { err = ptraceCont(t.ID, sig) })
if err != nil {
return err
}
// To be able to catch process exit, we can only use wait instead of waitFast.
wpid, status, err := t.dbp.wait(t.ID, 0)
if err != nil {
return err
}
if (status == nil || status.Exited()) && wpid == t.dbp.pid {
t.dbp.postExit()
rs := 0
if status != nil {
rs = status.ExitStatus()
}
return proc.ErrProcessExited{Pid: t.dbp.pid, Status: rs}
}
if wpid == t.ID {
sig = 0
switch s := status.StopSignal(); s {
case sys.SIGTRAP:
return nil
case sys.SIGSTOP:
// delayed SIGSTOP, ignore it
case sys.SIGILL, sys.SIGBUS, sys.SIGFPE, sys.SIGSEGV, sys.SIGSTKFLT:
// propagate signals that can have been caused by the current instruction
sig = int(s)
default:
// delay propagation of all other signals
t.os.delayedSignal = int(s)
}
}
}
}
|