Amanieu/a-tale-of-binary-translation

Simple RISC-V emulator presented at Rust Nation 2023

Rust

67

2 commits

updated Feb 17, 2023

See the code

README

A tale of binary translation

This repo contains the code for the RISC-V emulator that I presented at Rust Nation 2023.

It is intended as a teaching example to introduce people to the principles behind binary translation, and not a proper production-ready emulator. However, pull requests are still welcome!

Building the examples

A build.sh script is provided in the examples directory to build the assembly code examples used in the presentation. This requires clang and llvm-objcopy to be installed. Pass the name of the assembly file to the script to build it.

The resulting .bin file can be passed as an argument to the emulator:

examples/build.sh examples/hello_word.s
cargo run -- -b interpreter examples/hello_world.bin

System calls

The behavior of the ecall and ebreak instructions is defined in syscall.rs. Currently, ebreak will stop the emulator and exit the process. ecall invokes a system call depending on the value of x17:

System call number (x17)NameDescriptionArgumentsReturn value
0printPrints a stringx10: string address x11: string length
1print_intPrints an integerx10: integer to print
2get_argReturns the Nth argument as an integer, or 0 if absentx10: argument indexx10: argument value

You can easily define additional system calls by extending syscall.rs.

License

Licensed under either of:

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Contributors

Amanieu

2 commits

Amanieu/a-tale-of-binary-translation

Simple RISC-V emulator presented at Rust Nation 2023

Rust

67

2 commits

updated Feb 17, 2023

See the code

README

A tale of binary translation

This repo contains the code for the RISC-V emulator that I presented at Rust Nation 2023.

It is intended as a teaching example to introduce people to the principles behind binary translation, and not a proper production-ready emulator. However, pull requests are still welcome!

Building the examples

A build.sh script is provided in the examples directory to build the assembly code examples used in the presentation. This requires clang and llvm-objcopy to be installed. Pass the name of the assembly file to the script to build it.

The resulting .bin file can be passed as an argument to the emulator:

examples/build.sh examples/hello_word.s
cargo run -- -b interpreter examples/hello_world.bin

System calls

The behavior of the ecall and ebreak instructions is defined in syscall.rs. Currently, ebreak will stop the emulator and exit the process. ecall invokes a system call depending on the value of x17:

System call number (x17)NameDescriptionArgumentsReturn value
0printPrints a stringx10: string address x11: string length
1print_intPrints an integerx10: integer to print
2get_argReturns the Nth argument as an integer, or 0 if absentx10: argument indexx10: argument value

You can easily define additional system calls by extending syscall.rs.

License

Licensed under either of:

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Contributors

Amanieu

2 commits

Languages

Rust

100.0%