Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Comment thread
Inphi marked this conversation as resolved.

// FYI: https://en.wikibooks.org/wiki/MIPS_Assembly/Register_File
Comment thread
Inphi marked this conversation as resolved.
//
// 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
Comment thread
Inphi marked this conversation as resolved.

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