Skip to content

Commit

Permalink
cannon: add RegSP constant (ethereum-optimism#13316)
Browse files Browse the repository at this point in the history
* add RegSP constant

* address comments

* fix natspec

* update semver-lock
  • Loading branch information
zhiqiangxu authored Dec 9, 2024
1 parent b613161 commit a47441c
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 25 deletions.
15 changes: 8 additions & 7 deletions cannon/mipsevm/exec/mips_syscalls.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/ethereum-optimism/optimism/cannon/mipsevm/arch"
"github.com/ethereum-optimism/optimism/cannon/mipsevm/memory"
"github.com/ethereum-optimism/optimism/cannon/mipsevm/program"
"github.com/ethereum-optimism/optimism/cannon/mipsevm/register"
)

type Word = arch.Word
Expand Down Expand Up @@ -99,12 +100,12 @@ const (
)

func GetSyscallArgs(registers *[32]Word) (syscallNum, a0, a1, a2, a3 Word) {
syscallNum = registers[RegSyscallNum] // v0
syscallNum = registers[register.RegSyscallNum] // v0

a0 = registers[RegSyscallParam1]
a1 = registers[RegSyscallParam2]
a2 = registers[RegSyscallParam3]
a3 = registers[RegSyscallParam4]
a0 = registers[register.RegSyscallParam1]
a1 = registers[register.RegSyscallParam2]
a2 = registers[register.RegSyscallParam3]
a3 = registers[register.RegSyscallParam4]

return syscallNum, a0, a1, a2, a3
}
Expand Down Expand Up @@ -281,8 +282,8 @@ func HandleSysFcntl(a0, a1 Word) (v0, v1 Word) {
}

func HandleSyscallUpdates(cpu *mipsevm.CpuScalars, registers *[32]Word, v0, v1 Word) {
registers[RegSyscallRet1] = v0
registers[RegSyscallErrno] = v1
registers[register.RegSyscallRet1] = v0
registers[register.RegSyscallErrno] = v1

cpu.PC = cpu.NextPC
cpu.NextPC = cpu.NextPC + 4
Expand Down
7 changes: 4 additions & 3 deletions cannon/mipsevm/multithreaded/mips.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/ethereum-optimism/optimism/cannon/mipsevm/arch"
"github.com/ethereum-optimism/optimism/cannon/mipsevm/exec"
"github.com/ethereum-optimism/optimism/cannon/mipsevm/program"
"github.com/ethereum-optimism/optimism/cannon/mipsevm/register"
)

type Word = arch.Word
Expand Down Expand Up @@ -57,10 +58,10 @@ func (m *InstrumentedState) handleSyscall() error {
Registers: thread.Registers,
}

newThread.Registers[29] = a1
newThread.Registers[register.RegSP] = a1
// the child will perceive a 0 value as returned value instead, and no error
newThread.Registers[exec.RegSyscallRet1] = 0
newThread.Registers[exec.RegSyscallErrno] = 0
newThread.Registers[register.RegSyscallRet1] = 0
newThread.Registers[register.RegSyscallErrno] = 0
m.state.NextThreadId++

// Preempt this thread for the new one. But not before updating PCs
Expand Down
3 changes: 2 additions & 1 deletion cannon/mipsevm/program/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/ethereum-optimism/optimism/cannon/mipsevm"
"github.com/ethereum-optimism/optimism/cannon/mipsevm/arch"
"github.com/ethereum-optimism/optimism/cannon/mipsevm/memory"
"github.com/ethereum-optimism/optimism/cannon/mipsevm/register"
)

const WordSizeBytes = arch.WordSizeBytes
Expand Down Expand Up @@ -63,7 +64,7 @@ func PatchStack(st mipsevm.FPVMState) error {
if err := st.GetMemory().SetMemoryRange(sp-4*memory.PageSize, bytes.NewReader(make([]byte, 5*memory.PageSize))); err != nil {
return errors.New("failed to allocate page for stack content")
}
st.GetRegistersRef()[29] = sp
st.GetRegistersRef()[register.RegSP] = sp

storeMem := func(addr Word, v Word) {
var dat [WordSizeBytes]byte
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package exec
package register

// FYI: https://en.wikibooks.org/wiki/MIPS_Assembly/Register_File
//
Expand All @@ -12,6 +12,8 @@ const (
RegA2 = 6
// 4th syscall argument; set to 0/1 for success/error
RegA3 = 7
// Stack pointer
RegSP = 29
)

// FYI: https://web.archive.org/web/20231223163047/https://www.linux-mips.org/wiki/Syscall
Expand Down
7 changes: 4 additions & 3 deletions cannon/mipsevm/tests/evm_multithreaded_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/ethereum-optimism/optimism/cannon/mipsevm/exec"
"github.com/ethereum-optimism/optimism/cannon/mipsevm/multithreaded"
mttestutil "github.com/ethereum-optimism/optimism/cannon/mipsevm/multithreaded/testutil"
"github.com/ethereum-optimism/optimism/cannon/mipsevm/register"
"github.com/ethereum-optimism/optimism/cannon/mipsevm/testutil"
)

Expand Down Expand Up @@ -327,9 +328,9 @@ func TestEVM_SysClone_Successful(t *testing.T) {
expectedNewThread.PC = state.GetCpu().NextPC
expectedNewThread.NextPC = state.GetCpu().NextPC + 4
expectedNewThread.ThreadId = 1
expectedNewThread.Registers[2] = 0
expectedNewThread.Registers[7] = 0
expectedNewThread.Registers[29] = stackPtr
expectedNewThread.Registers[register.RegSyscallRet1] = 0
expectedNewThread.Registers[register.RegSyscallErrno] = 0
expectedNewThread.Registers[register.RegSP] = stackPtr

var err error
var stepWitness *mipsevm.StepWitness
Expand Down
7 changes: 4 additions & 3 deletions cannon/mipsevm/tests/fuzz_evm_multithreaded_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/ethereum-optimism/optimism/cannon/mipsevm/exec"
"github.com/ethereum-optimism/optimism/cannon/mipsevm/multithreaded"
mttestutil "github.com/ethereum-optimism/optimism/cannon/mipsevm/multithreaded/testutil"
"github.com/ethereum-optimism/optimism/cannon/mipsevm/register"
"github.com/ethereum-optimism/optimism/cannon/mipsevm/testutil"
)

Expand Down Expand Up @@ -46,9 +47,9 @@ func FuzzStateSyscallCloneMT(f *testing.F) {
epxectedNewThread := expected.ExpectNewThread()
epxectedNewThread.PC = state.GetCpu().NextPC
epxectedNewThread.NextPC = state.GetCpu().NextPC + 4
epxectedNewThread.Registers[2] = 0
epxectedNewThread.Registers[7] = 0
epxectedNewThread.Registers[29] = stackPtr
epxectedNewThread.Registers[register.RegSyscallNum] = 0
epxectedNewThread.Registers[register.RegSyscallErrno] = 0
epxectedNewThread.Registers[register.RegSP] = stackPtr
expected.NextThreadId = nextThreadId + 1
expected.StepsSinceLastContextSwitch = 0
if state.TraverseRight {
Expand Down
4 changes: 2 additions & 2 deletions packages/contracts-bedrock/snapshots/semver-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@
"sourceCodeHash": "0x6c45dd23cb0d6f9bf4f84855ad0caf70e53dee3fe6c41454f7bf8df52ec3a9af"
},
"src/cannon/MIPS2.sol": {
"initCodeHash": "0x7476695bb101cb45213793291124e3ec41e13a02d291837b76d8a35bfc8ec2c1",
"sourceCodeHash": "0xeaceb5d28bd58fca6a234d9291ca01424bf83576d191ee3046272bc4987d0b29"
"initCodeHash": "0x4971f62a6aecf91bd795fa44b5ce3cb77a987719af4f351d4aec5b6c3bf81387",
"sourceCodeHash": "0x8da8be0b7d60af0eb11bd58653f1854d56a8f0616f3aeaeba7ab9ec340d02ac7"
},
"src/cannon/MIPS64.sol": {
"initCodeHash": "0x6516160f35a85abb65d8102fa71f03cb57518787f9af85bc951f27ee60e6bb8f",
Expand Down
10 changes: 5 additions & 5 deletions packages/contracts-bedrock/src/cannon/MIPS2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ contract MIPS2 is ISemver {
}

/// @notice The semantic version of the MIPS2 contract.
/// @custom:semver 1.0.0-beta.25
string public constant version = "1.0.0-beta.25";
/// @custom:semver 1.0.0-beta.26
string public constant version = "1.0.0-beta.26";

/// @notice The preimage oracle contract.
IPreimageOracle internal immutable ORACLE;
Expand Down Expand Up @@ -428,10 +428,10 @@ contract MIPS2 is ISemver {
for (uint256 i; i < 32; i++) {
newThread.registers[i] = thread.registers[i];
}
newThread.registers[29] = a1; // set stack pointer
newThread.registers[sys.REG_SP] = a1; // set stack pointer
// the child will perceive a 0 value as returned value instead, and no error
newThread.registers[2] = 0;
newThread.registers[7] = 0;
newThread.registers[sys.REG_SYSCALL_RET1] = 0;
newThread.registers[sys.REG_SYSCALL_ERRNO] = 0;
state.nextThreadID++;

// Preempt this thread for the new one. But not before updating PCs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ library MIPSSyscalls {
uint32 internal constant REG_A1 = 5;
uint32 internal constant REG_A2 = 6;
uint32 internal constant REG_A3 = 7;
uint32 internal constant REG_SP = 29;

// FYI: https://web.archive.org/web/20231223163047/https://www.linux-mips.org/wiki/Syscall
uint32 internal constant REG_SYSCALL_NUM = REG_V0;
Expand Down

0 comments on commit a47441c

Please sign in to comment.