fwsGonzo / libriscv

The fastest RISC-V sandbox
BSD 3-Clause "New" or "Revised" License
522 stars 46 forks source link

Dynamic linking for host functions #180

Closed fwsGonzo closed 3 days ago

fwsGonzo commented 3 days ago

I've been thinking about using dynamic linking for supporting host functions more dynamically. Or at least, a way to support them in libriscv proper. Right now there are all kinds of schemes to define host functions in guests ranging from fully dynamic with hashes to fast pre-built assembly functions using build system code generation.

The thing is, though, that dynamically linked host functions will never be the fastest option. They must fundamentally be loaded somewhere, which is not inside the main execute segment, and then they will invoke system calls (presumably), which leaves out one register for arguments (A7). It's probably OK, as I won't be using this in my games, but it would be nice to have a supported/blessed way to define host functions inside libriscv.

So, how does it (probably) work?

  1. Create a dynamic executable
  2. Define host functions up-front by (globally) registering them with libriscv
  3. Load dynamic executable
  4. During loading, the globally registered host functions will be automatically turned into a shared library
  5. The shared library will be made accessible, so the dynamic linker can open it
  6. When opened, the guest can now access the host functions through the API
fwsGonzo commented 3 days ago

I think this idea is too weak right now. Too many drawbacks, too few benefits.