Commit a47441c8 authored by zhiqiangxu's avatar zhiqiangxu Committed by GitHub

cannon: add `RegSP` constant (#13316)

* add RegSP constant

* address comments

* fix natspec

* update semver-lock
parent b6131611
...@@ -11,6 +11,7 @@ import ( ...@@ -11,6 +11,7 @@ import (
"github.com/ethereum-optimism/optimism/cannon/mipsevm/arch" "github.com/ethereum-optimism/optimism/cannon/mipsevm/arch"
"github.com/ethereum-optimism/optimism/cannon/mipsevm/memory" "github.com/ethereum-optimism/optimism/cannon/mipsevm/memory"
"github.com/ethereum-optimism/optimism/cannon/mipsevm/program" "github.com/ethereum-optimism/optimism/cannon/mipsevm/program"
"github.com/ethereum-optimism/optimism/cannon/mipsevm/register"
) )
type Word = arch.Word type Word = arch.Word
...@@ -99,12 +100,12 @@ const ( ...@@ -99,12 +100,12 @@ const (
) )
func GetSyscallArgs(registers *[32]Word) (syscallNum, a0, a1, a2, a3 Word) { func GetSyscallArgs(registers *[32]Word) (syscallNum, a0, a1, a2, a3 Word) {
syscallNum = registers[RegSyscallNum] // v0 syscallNum = registers[register.RegSyscallNum] // v0
a0 = registers[RegSyscallParam1] a0 = registers[register.RegSyscallParam1]
a1 = registers[RegSyscallParam2] a1 = registers[register.RegSyscallParam2]
a2 = registers[RegSyscallParam3] a2 = registers[register.RegSyscallParam3]
a3 = registers[RegSyscallParam4] a3 = registers[register.RegSyscallParam4]
return syscallNum, a0, a1, a2, a3 return syscallNum, a0, a1, a2, a3
} }
...@@ -281,8 +282,8 @@ func HandleSysFcntl(a0, a1 Word) (v0, v1 Word) { ...@@ -281,8 +282,8 @@ func HandleSysFcntl(a0, a1 Word) (v0, v1 Word) {
} }
func HandleSyscallUpdates(cpu *mipsevm.CpuScalars, registers *[32]Word, v0, v1 Word) { func HandleSyscallUpdates(cpu *mipsevm.CpuScalars, registers *[32]Word, v0, v1 Word) {
registers[RegSyscallRet1] = v0 registers[register.RegSyscallRet1] = v0
registers[RegSyscallErrno] = v1 registers[register.RegSyscallErrno] = v1
cpu.PC = cpu.NextPC cpu.PC = cpu.NextPC
cpu.NextPC = cpu.NextPC + 4 cpu.NextPC = cpu.NextPC + 4
......
...@@ -12,6 +12,7 @@ import ( ...@@ -12,6 +12,7 @@ import (
"github.com/ethereum-optimism/optimism/cannon/mipsevm/arch" "github.com/ethereum-optimism/optimism/cannon/mipsevm/arch"
"github.com/ethereum-optimism/optimism/cannon/mipsevm/exec" "github.com/ethereum-optimism/optimism/cannon/mipsevm/exec"
"github.com/ethereum-optimism/optimism/cannon/mipsevm/program" "github.com/ethereum-optimism/optimism/cannon/mipsevm/program"
"github.com/ethereum-optimism/optimism/cannon/mipsevm/register"
) )
type Word = arch.Word type Word = arch.Word
...@@ -57,10 +58,10 @@ func (m *InstrumentedState) handleSyscall() error { ...@@ -57,10 +58,10 @@ func (m *InstrumentedState) handleSyscall() error {
Registers: thread.Registers, 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 // the child will perceive a 0 value as returned value instead, and no error
newThread.Registers[exec.RegSyscallRet1] = 0 newThread.Registers[register.RegSyscallRet1] = 0
newThread.Registers[exec.RegSyscallErrno] = 0 newThread.Registers[register.RegSyscallErrno] = 0
m.state.NextThreadId++ m.state.NextThreadId++
// Preempt this thread for the new one. But not before updating PCs // Preempt this thread for the new one. But not before updating PCs
......
...@@ -9,6 +9,7 @@ import ( ...@@ -9,6 +9,7 @@ import (
"github.com/ethereum-optimism/optimism/cannon/mipsevm" "github.com/ethereum-optimism/optimism/cannon/mipsevm"
"github.com/ethereum-optimism/optimism/cannon/mipsevm/arch" "github.com/ethereum-optimism/optimism/cannon/mipsevm/arch"
"github.com/ethereum-optimism/optimism/cannon/mipsevm/memory" "github.com/ethereum-optimism/optimism/cannon/mipsevm/memory"
"github.com/ethereum-optimism/optimism/cannon/mipsevm/register"
) )
const WordSizeBytes = arch.WordSizeBytes const WordSizeBytes = arch.WordSizeBytes
...@@ -63,7 +64,7 @@ func PatchStack(st mipsevm.FPVMState) error { ...@@ -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 { 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") 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) { storeMem := func(addr Word, v Word) {
var dat [WordSizeBytes]byte var dat [WordSizeBytes]byte
......
package exec package register
// FYI: https://en.wikibooks.org/wiki/MIPS_Assembly/Register_File // FYI: https://en.wikibooks.org/wiki/MIPS_Assembly/Register_File
// //
...@@ -12,6 +12,8 @@ const ( ...@@ -12,6 +12,8 @@ const (
RegA2 = 6 RegA2 = 6
// 4th syscall argument; set to 0/1 for success/error // 4th syscall argument; set to 0/1 for success/error
RegA3 = 7 RegA3 = 7
// Stack pointer
RegSP = 29
) )
// FYI: https://web.archive.org/web/20231223163047/https://www.linux-mips.org/wiki/Syscall // FYI: https://web.archive.org/web/20231223163047/https://www.linux-mips.org/wiki/Syscall
......
...@@ -17,6 +17,7 @@ import ( ...@@ -17,6 +17,7 @@ import (
"github.com/ethereum-optimism/optimism/cannon/mipsevm/exec" "github.com/ethereum-optimism/optimism/cannon/mipsevm/exec"
"github.com/ethereum-optimism/optimism/cannon/mipsevm/multithreaded" "github.com/ethereum-optimism/optimism/cannon/mipsevm/multithreaded"
mttestutil "github.com/ethereum-optimism/optimism/cannon/mipsevm/multithreaded/testutil" 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" "github.com/ethereum-optimism/optimism/cannon/mipsevm/testutil"
) )
...@@ -327,9 +328,9 @@ func TestEVM_SysClone_Successful(t *testing.T) { ...@@ -327,9 +328,9 @@ func TestEVM_SysClone_Successful(t *testing.T) {
expectedNewThread.PC = state.GetCpu().NextPC expectedNewThread.PC = state.GetCpu().NextPC
expectedNewThread.NextPC = state.GetCpu().NextPC + 4 expectedNewThread.NextPC = state.GetCpu().NextPC + 4
expectedNewThread.ThreadId = 1 expectedNewThread.ThreadId = 1
expectedNewThread.Registers[2] = 0 expectedNewThread.Registers[register.RegSyscallRet1] = 0
expectedNewThread.Registers[7] = 0 expectedNewThread.Registers[register.RegSyscallErrno] = 0
expectedNewThread.Registers[29] = stackPtr expectedNewThread.Registers[register.RegSP] = stackPtr
var err error var err error
var stepWitness *mipsevm.StepWitness var stepWitness *mipsevm.StepWitness
......
...@@ -10,6 +10,7 @@ import ( ...@@ -10,6 +10,7 @@ import (
"github.com/ethereum-optimism/optimism/cannon/mipsevm/exec" "github.com/ethereum-optimism/optimism/cannon/mipsevm/exec"
"github.com/ethereum-optimism/optimism/cannon/mipsevm/multithreaded" "github.com/ethereum-optimism/optimism/cannon/mipsevm/multithreaded"
mttestutil "github.com/ethereum-optimism/optimism/cannon/mipsevm/multithreaded/testutil" 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" "github.com/ethereum-optimism/optimism/cannon/mipsevm/testutil"
) )
...@@ -46,9 +47,9 @@ func FuzzStateSyscallCloneMT(f *testing.F) { ...@@ -46,9 +47,9 @@ func FuzzStateSyscallCloneMT(f *testing.F) {
epxectedNewThread := expected.ExpectNewThread() epxectedNewThread := expected.ExpectNewThread()
epxectedNewThread.PC = state.GetCpu().NextPC epxectedNewThread.PC = state.GetCpu().NextPC
epxectedNewThread.NextPC = state.GetCpu().NextPC + 4 epxectedNewThread.NextPC = state.GetCpu().NextPC + 4
epxectedNewThread.Registers[2] = 0 epxectedNewThread.Registers[register.RegSyscallNum] = 0
epxectedNewThread.Registers[7] = 0 epxectedNewThread.Registers[register.RegSyscallErrno] = 0
epxectedNewThread.Registers[29] = stackPtr epxectedNewThread.Registers[register.RegSP] = stackPtr
expected.NextThreadId = nextThreadId + 1 expected.NextThreadId = nextThreadId + 1
expected.StepsSinceLastContextSwitch = 0 expected.StepsSinceLastContextSwitch = 0
if state.TraverseRight { if state.TraverseRight {
......
...@@ -140,8 +140,8 @@ ...@@ -140,8 +140,8 @@
"sourceCodeHash": "0x6c45dd23cb0d6f9bf4f84855ad0caf70e53dee3fe6c41454f7bf8df52ec3a9af" "sourceCodeHash": "0x6c45dd23cb0d6f9bf4f84855ad0caf70e53dee3fe6c41454f7bf8df52ec3a9af"
}, },
"src/cannon/MIPS2.sol": { "src/cannon/MIPS2.sol": {
"initCodeHash": "0x7476695bb101cb45213793291124e3ec41e13a02d291837b76d8a35bfc8ec2c1", "initCodeHash": "0x4971f62a6aecf91bd795fa44b5ce3cb77a987719af4f351d4aec5b6c3bf81387",
"sourceCodeHash": "0xeaceb5d28bd58fca6a234d9291ca01424bf83576d191ee3046272bc4987d0b29" "sourceCodeHash": "0x8da8be0b7d60af0eb11bd58653f1854d56a8f0616f3aeaeba7ab9ec340d02ac7"
}, },
"src/cannon/MIPS64.sol": { "src/cannon/MIPS64.sol": {
"initCodeHash": "0x6516160f35a85abb65d8102fa71f03cb57518787f9af85bc951f27ee60e6bb8f", "initCodeHash": "0x6516160f35a85abb65d8102fa71f03cb57518787f9af85bc951f27ee60e6bb8f",
......
...@@ -63,8 +63,8 @@ contract MIPS2 is ISemver { ...@@ -63,8 +63,8 @@ contract MIPS2 is ISemver {
} }
/// @notice The semantic version of the MIPS2 contract. /// @notice The semantic version of the MIPS2 contract.
/// @custom:semver 1.0.0-beta.25 /// @custom:semver 1.0.0-beta.26
string public constant version = "1.0.0-beta.25"; string public constant version = "1.0.0-beta.26";
/// @notice The preimage oracle contract. /// @notice The preimage oracle contract.
IPreimageOracle internal immutable ORACLE; IPreimageOracle internal immutable ORACLE;
...@@ -428,10 +428,10 @@ contract MIPS2 is ISemver { ...@@ -428,10 +428,10 @@ contract MIPS2 is ISemver {
for (uint256 i; i < 32; i++) { for (uint256 i; i < 32; i++) {
newThread.registers[i] = thread.registers[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 // the child will perceive a 0 value as returned value instead, and no error
newThread.registers[2] = 0; newThread.registers[sys.REG_SYSCALL_RET1] = 0;
newThread.registers[7] = 0; newThread.registers[sys.REG_SYSCALL_ERRNO] = 0;
state.nextThreadID++; state.nextThreadID++;
// Preempt this thread for the new one. But not before updating PCs // Preempt this thread for the new one. But not before updating PCs
......
...@@ -156,6 +156,7 @@ library MIPSSyscalls { ...@@ -156,6 +156,7 @@ library MIPSSyscalls {
uint32 internal constant REG_A1 = 5; uint32 internal constant REG_A1 = 5;
uint32 internal constant REG_A2 = 6; uint32 internal constant REG_A2 = 6;
uint32 internal constant REG_A3 = 7; 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 // FYI: https://web.archive.org/web/20231223163047/https://www.linux-mips.org/wiki/Syscall
uint32 internal constant REG_SYSCALL_NUM = REG_V0; uint32 internal constant REG_SYSCALL_NUM = REG_V0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment