Skip to content

Commit

Permalink
works!
Browse files Browse the repository at this point in the history
  • Loading branch information
clabby committed Aug 10, 2024
1 parent cdcfa77 commit eba86f2
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 14 deletions.
25 changes: 17 additions & 8 deletions cannon/mipsevm/exec/mips_instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func ExecMipsCoreStepLogic(cpu *mipsevm.CpuScalars, registers *[32]uint64, memor
// SignExtImm
rt = SignExtend(insn&0xFFFF, 16)
}
} else if opcode >= 0x28 || opcode == 0x22 || opcode == 0x26 || opcode == 0x1A || opcode == 0x1B {
} else if opcode >= 0x27 || opcode == 0x22 || opcode == 0x26 || opcode == 0x1A || opcode == 0x1B {
// store rt value with store
rt = registers[rtReg]

Expand All @@ -63,7 +63,7 @@ func ExecMipsCoreStepLogic(cpu *mipsevm.CpuScalars, registers *[32]uint64, memor
return HandleBranch(cpu, registers, opcode, insn, rtReg, rs)
}

storeAddr := uint64(0xFF_FF_FF_FF_FF_FF_FF_FF)
storeAddr := ^uint64(0)
// memory fetch (all I-type)
// we do the load for stores also
mem := uint64(0)
Expand Down Expand Up @@ -114,7 +114,7 @@ func ExecMipsCoreStepLogic(cpu *mipsevm.CpuScalars, registers *[32]uint64, memor
}

// write memory
if storeAddr != 0xFF_FF_FF_FF_FF_FF_FF_FF {
if storeAddr != ^uint64(0) {
memTracker.TrackMemAccess(storeAddr)
memory.SetDoubleWord(storeAddr, val)
}
Expand Down Expand Up @@ -281,7 +281,7 @@ func ExecuteMipsInstruction(insn, opcode, fun, rs, rt, mem uint64) uint64 {
case 0x22: // lwl
val := mem << ((rs & 3) * 8)
mask := uint64(uint32(0xFFFFFFFF) << ((rs & 3) * 8))
return SignExtend((rt & ^mask)|val, 32)
return SignExtend(((rt & ^mask)|val)&0xFFFFFFFF, 32)
case 0x23: // lw
return SignExtend((mem>>(32-((rs&0x4)<<3)))&0xFFFFFFFF, 32)
case 0x24: // lbu
Expand All @@ -291,7 +291,7 @@ func ExecuteMipsInstruction(insn, opcode, fun, rs, rt, mem uint64) uint64 {
case 0x26: // lwr
val := mem >> (24 - (rs&3)*8)
mask := uint64(uint32(0xFFFFFFFF) >> (24 - (rs&3)*8))
return SignExtend((rt & ^mask)|val, 32)
return SignExtend(((rt & ^mask)|val)&0xFFFFFFFF, 32)
case 0x28: // sb
val := (rt & 0xFF) << (56 - (rs&7)*8)
mask := 0xFFFFFFFFFFFFFFFF ^ uint64(0xFF<<(56-(rs&7)*8))
Expand Down Expand Up @@ -319,7 +319,10 @@ func ExecuteMipsInstruction(insn, opcode, fun, rs, rt, mem uint64) uint64 {
case 0x30: // ll
return SignExtend((mem>>(32-((rs&0x4)<<3)))&0xFFFFFFFF, 32)
case 0x38: // sc
return rt
sl := 32 - ((rs & 0x4) << 3)
val := (rt & 0xFFFFFFFF) << sl
mask := 0xFFFFFFFFFFFFFFFF ^ uint64(0xFFFFFFFF<<sl)
return (mem & mask) | val
// MIPS64
case 0x1A: // ldl
sl := (rs & 0x7) << 3
Expand Down Expand Up @@ -348,9 +351,15 @@ func ExecuteMipsInstruction(insn, opcode, fun, rs, rt, mem uint64) uint64 {
case 0x37: // ld
return mem
case 0x3C: // scd
return rt
sl := (rs & 0x7) << 3
val := rt << sl
mask := uint64(0xFFFFFFFFFFFFFFFF) << sl
return (mem & ^mask) | val
case 0x3F: // sd
return rt
sl := (rs & 0x7) << 3
val := rt << sl
mask := uint64(0xFFFFFFFFFFFFFFFF) << sl
return (mem & ^mask) | val
default:
panic(fmt.Sprintf("invalid instruction: %x", insn))
}
Expand Down
1 change: 1 addition & 0 deletions cannon/mipsevm/exec/mips_syscalls.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const (
SysSigaltstack = 5129
SysRtSigaction = 5013
SysPrlimit64 = 5297
SysGetRLimit = 5095
SysClose = 5003
SysPread64 = 5016
SysFstat64 = 5005
Expand Down
2 changes: 1 addition & 1 deletion cannon/mipsevm/multithreaded/instrumented_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestInstrumentedState_MultithreadedProgram(t *testing.T) {

var stdOutBuf, stdErrBuf bytes.Buffer
us := NewInstrumentedState(state, oracle, io.MultiWriter(&stdOutBuf, os.Stdout), io.MultiWriter(&stdErrBuf, os.Stderr), testutil.CreateLogger())
for i := 0; i < 1_000_000; i++ {
for i := 0; i < 2_000_000; i++ {
if us.GetState().GetExited() {
break
}
Expand Down
1 change: 1 addition & 0 deletions cannon/mipsevm/multithreaded/mips.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ func (m *InstrumentedState) handleSyscall() error {
case exec.SysSigaltstack:
case exec.SysRtSigaction:
case exec.SysPrlimit64:
case exec.SysGetRLimit:
case exec.SysClose:
case exec.SysPread64:
case exec.SysFstat64:
Expand Down
5 changes: 1 addition & 4 deletions cannon/mipsevm/program/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package program
import (
"bytes"
"debug/elf"
"encoding/binary"
"fmt"

"github.com/ethereum-optimism/optimism/cannon/mipsevm"
Expand Down Expand Up @@ -67,9 +66,7 @@ func PatchStack(st mipsevm.FPVMState) error {
st.GetRegisters()[29] = sp

storeMem := func(addr uint64, v uint64) {
var dat [8]byte
binary.BigEndian.PutUint64(dat[:], v)
_ = st.GetMemory().SetMemoryRange(addr, bytes.NewReader(dat[:]))
st.GetMemory().SetDoubleWord(addr, v)
}

// init argc, argv, aux on stack
Expand Down
2 changes: 1 addition & 1 deletion cannon/testdata/example/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ bin:
# verify output with: readelf -h bin/<name>.elf
# result is mips64, big endian, R3000
bin/%.elf: bin
cd $(@:bin/%.elf=%) && GOOS=linux GODEBUG=memprofilerate=0 GOARCH=mips64 GOMIPS64=softfloat go build -o ../$@ .
cd $(@:bin/%.elf=%) && GOOS=linux GOARCH=mips64 GOMIPS64=softfloat go build -o ../$@ .

# take any ELF and dump it
# TODO: currently have the little-endian toolchain, but should use the big-endian one. The -EB compat flag works though.
Expand Down

0 comments on commit eba86f2

Please sign in to comment.