AntonLydike / riscemu

RISC-V emulator in python
MIT License
48 stars 13 forks source link

Method to add syscalls? #15

Open RobertBaruch opened 2 years ago

RobertBaruch commented 2 years ago

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

AntonLydike commented 2 years ago

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!

RobertBaruch commented 2 years ago

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

AntonLydike commented 2 years ago

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 commented 2 years ago

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

RobertBaruch commented 2 years ago

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 :>