riscemu icon indicating copy to clipboard operation
riscemu copied to clipboard

Method to add syscalls?

Open RobertBaruch opened this issue 2 years ago • 5 comments

Could you add a method to add syscalls to the CPU? Thanks!

RobertBaruch avatar Apr 18 '22 00:04 RobertBaruch

You can add syscalls by editing the syscalls.py file. I added some instructions at the bottom of docs/syscalls.md.

I'll leave this issue open if you have any further questions!

AntonLydike avatar Apr 18 '22 18:04 AntonLydike

Oh, I was kind of hoping not to have to modify the files in the distro...

RobertBaruch avatar Apr 18 '22 23:04 RobertBaruch

I haven't tested this yet, but you could probably do it like this:

import riscemu.syscall as syscall
from riscemu import UserModeCPU, RV32M, RV32I, RV32A, RunConfig

syscall.SYSCALLS[42] = 'somesyscall'

class MySyscallInterface(syscall.SyscallInterface):
    def somesyscall(self, scall: syscall.Syscall):
        print(scall)
        scall.ret(-1)

cpu = UserModeCPU([RV32M, RV32I, RV32A], RunConfig())

cpu.syscall_int = MySyscallInterface()

# ...

You should keep in mind, that the registers don't work with integers, but with a wrapper around them called Int32 (and UInt32). They behave just like integers, but you must unwrap them if you wish to use "normal" integers to e.g. read memory at an address, or set the CPU pc.

AntonLydike avatar Apr 19 '22 06:04 AntonLydike

This looks pretty boilerplate heavy though, maybe I should add an easier method to add syscalls at runtime...

AntonLydike avatar Apr 19 '22 06:04 AntonLydike

Yah -- also just assigning syscall_int wouldn't add the symbols. It's ok to restrict adding syscalls to config time. Once the processor runs, it's probably a bad idea to add syscalls :>

RobertBaruch avatar Apr 19 '22 15:04 RobertBaruch