diff --git a/cannon/mipsevm/exec/calling_convention.go b/cannon/mipsevm/exec/calling_convention.go new file mode 100644 index 00000000000..8cbb963a964 --- /dev/null +++ b/cannon/mipsevm/exec/calling_convention.go @@ -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 +) diff --git a/cannon/mipsevm/exec/mips_syscalls.go b/cannon/mipsevm/exec/mips_syscalls.go index 8679a39b773..6387a2a2b91 100644 --- a/cannon/mipsevm/exec/mips_syscalls.go +++ b/cannon/mipsevm/exec/mips_syscalls.go @@ -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 } @@ -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 diff --git a/cannon/mipsevm/multithreaded/mips.go b/cannon/mipsevm/multithreaded/mips.go index f738ecf38d7..72ab6720bf3 100644 --- a/cannon/mipsevm/multithreaded/mips.go +++ b/cannon/mipsevm/multithreaded/mips.go @@ -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