Skip to content

Commit

Permalink
cannon: use constant instead of magic value (ethereum-optimism#12386)
Browse files Browse the repository at this point in the history
* use constant instead of magic value

* address comments

* address comments

* add RegSyscallErrno
  • Loading branch information
zhiqiangxu authored Oct 16, 2024
1 parent 86e5f63 commit ea60a8a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
27 changes: 27 additions & 0 deletions cannon/mipsevm/exec/calling_convention.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package exec

// FYI: https://en.wikibooks.org/wiki/MIPS_Assembly/Register_File
//
// https://refspecs.linuxfoundation.org/elf/mipsabi.pdf
const (
// syscall number; 1st return value
RegV0 = 2
// syscall arguments; returned unmodified
RegA0 = 4
RegA1 = 5
RegA2 = 6
// 4th syscall argument; set to 0/1 for success/error
RegA3 = 7
)

// FYI: https://web.archive.org/web/20231223163047/https://www.linux-mips.org/wiki/Syscall

const (
RegSyscallNum = RegV0
RegSyscallErrno = RegA3
RegSyscallRet1 = RegV0
RegSyscallParam1 = RegA0
RegSyscallParam2 = RegA1
RegSyscallParam3 = RegA2
RegSyscallParam4 = RegA3
)
14 changes: 7 additions & 7 deletions cannon/mipsevm/exec/mips_syscalls.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ const (
)

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

a0 = registers[4]
a1 = registers[5]
a2 = registers[6]
a3 = registers[7]
a0 = registers[RegSyscallParam1]
a1 = registers[RegSyscallParam2]
a2 = registers[RegSyscallParam3]
a3 = registers[RegSyscallParam4]

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

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

cpu.PC = cpu.NextPC
cpu.NextPC = cpu.NextPC + 4
Expand Down
4 changes: 2 additions & 2 deletions cannon/mipsevm/multithreaded/mips.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ func (m *InstrumentedState) handleSyscall() error {

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

// Preempt this thread for the new one. But not before updating PCs
Expand Down

0 comments on commit ea60a8a

Please sign in to comment.