Coder Social home page Coder Social logo

riscvm's Introduction

Tiny RISC-V virtual machine written in Nelua (and C as consequence).

This is a minimal and simple RISC-V ISA emulator implementing a subset of RV64I instructions.

Only the instructions required to run a minimal userspace virtual machine are implemented.

The emulator allows to run C files compiled with GCC to RISC-V inside a minimal sandboxed virtual machine. The virtual machine is able to call host functions through system calls.

This project has inspired by libriscv.

What is interesting about this?

This is a minimal example on how to interpret C programs inside a sandboxed virtual machine. One may use this concept to run compiled programs with GCC on any system that have the emulator, or to sandbox a C application inside isolated environment, or to do any kind of hot reloading in application on the fly (usually done with scripting languages).

Run examples

Run examples with Nelua:

nelua -r riscvm.nelua examples/fib.bin
nelua -r riscvm.nelua examples/ack.bin
nelua -r riscvm.nelua examples/sieve.bin

The examples were compiled from the respective C files into RV64I binary.

Alternatively if you don't have Nelua:

gcc -O2 -o riscvm riscvm.c
./riscvm examples/fib.bin

Compiling an example

All the examples are already compiled to RV64I, but in case you want to edit or run a new example, then use for example make EXAMPLE=fib.c to compile fib.c into fib.bin, this requires RISC-V elf toolchain.

How it works?

  1. An example is coded in freestanding C code, functionality outside the sandboxed environment such as printing to terminal is implemented through system calls in lib/syscalls.h and minimal libc is implemented in lib/tinyc.c.
  2. The C example is compiled to RISC-V elf binary, using lib/start.s to initialize the properly the virtual machine state, and using special link rules through lib/link.ld to adjust the instruction addresses.
  3. RISC-V bytecode is stripped from the compiled elf binary into a bytecode binary.
  4. The bytecode binary is loaded and run through riscvm, interpreting RV64I instructions.
  5. While interpreting the virtual machine may call the host through system calls.
  6. The application stops once the exit system call is called or if any error occur.

How this was implemented?

This was implemented by reading the RISC-V specification

Benchmarks

Equivalent code was run with the Lua 5.4, riscvm and natively in the same system for some examples, from the experiments the interpreted code can be 10~20x slower than native code:

example lua 5.4 riscvm x86_64
ack 1070ms 1022ms 47ms
sieve 1077ms 716ms 64ms

NOTE: This VM does not do any kind of JIT and has a very simple implementation, there are space for optimizations.

Future improvements

These extensions are not implemented yet and would be useful:

  • M extension - Multiply and division
  • F extension - Floating point with single precision
  • D extension - Floating point with double precision

Also more C functions such as malloc/free/memcpy could be implemented yet as system calls.

riscvm's People

Contributors

edubart avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.