• Stars
    star
    625
  • Rank 71,862 (Top 2 %)
  • Language
    C++
  • License
    MIT License
  • Created almost 9 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

RISC-V simulator for x86-64

rv8

RISC-V simulator for x86-64

Build Status Coverity Status License: MIT Donate

rv8 is a RISC-V simulation suite comprising a high performance x86-64 binary translator, a user mode simulator, a full system emulator, an ELF binary analysis tool and ISA metadata:

  • rv-jit - user mode x86-64 binary translator
  • rv-sim - user mode system call proxy simulator
  • rv-sys - full system emulator with soft MMU
  • rv-bin - ELF disassembler and histogram tool
  • rv-meta - code and documentation generator

The rv8 simulator suite contains libraries and command line tools for creating instruction opcode maps, C headers and source containing instruction set metadata, instruction decoders, a JIT assembler, LaTeX documentation, a metadata based RISC-V disassembler, a histogram tool for generating statistics on RISC-V ELF executables, a RISC-V proxy syscall simulator, a RISC-V full system emulator that implements the RISC-V 1.9.1 privileged specification and an x86-64 binary translator.

rv8 binary translation

RISC-V to x86-64 binary translator

The rv8 binary translation engine works by interpreting code while profiling it for hot paths. Hot paths are translated on the fly to native code. The translation engine maintains a call stack to allow runtime inlining of hot functions. A jump target cache is used to accelerate returns and indirect calls through function pointers. The translator supports hybrid binary translation and interpretation to handle instructions that do not have native translations. Currently ‘IM’ code is translated and ‘AFD’ is interpreted. The translator supports RVC compressed code.

The rv8 binary translator supports a number of simple optimisations:

  • Hybrid interpretation and compilation of hot code paths
  • Incremental translation with dynamic trace linking
  • Inline caching of function calls in hot code paths
  • L1 jump target cache for indirect calls and returns
  • Macro-op fusion for common RISC-V instruction sequences

RISC-V full system emulator

The rv8 suite includes a full system emulator that implements the RISC-V privileged ISA with support for interrupts, MMIO (memory mapped input output) devices, a soft MMU (memory management unit) with separate instruction and data TLBs (translation lookaside buffers). The full system emulator has a simple integrated debugger that allows setting breakpoints, single stepping and disassembling instructions as they are executed.

The rv8 full system emulator has the following features:

  • RISC-V IMAFD Base plus Privileged ISA (riscv32 and riscv64)
  • Simple integrated debug command line interface
  • Histograms (registers, instruction and program counter frequency)
  • Soft MMU supporting sv32, sv39, sv48 page translation modes
  • Abstract MMIO device interface for device emulation
  • Extensible interpreter generated from ISA metadata
  • Protected address space

RISC-V user mode simulator

The rv8 user mode simulator is a single address space implementation of the RISC-V ISA that implements a subset of the RISC-V Linux syscall ABI (application binary interface) and delegates system calls to the underlying native host operating system. The user mode simulator can run RISC-V Linux binaries on non-Linux operating systems via system call emulation. The current user mode simulator implements a small number of system calls to allow running RISC-V Linux ELF static binaries.

The rv8 user mode simulator has the following features:

  • RISC-V IMAFD Base ISA (riscv32 and riscv64)
  • Simple integrated debug command line interface
  • A small set of emulated Linux system calls for simple file IO
  • Extensible interpreter generated from ISA metadata
  • Instruction logging mode for tracing program execution
  • Shared address space
    • 0x000000000000 - 0x000000000fff (zero)
    • 0x000000001000 - 0x7ffdffffffff (guest)
    • 0x7ffe00000000 - 0x7fffffffffff (host)

Project Goals

The future goals of the rv8 project are:

  • Concise metadata representing the RISC-V ISA
  • Tools for metadata-based generation of source and documentation
  • High performance emulation, sandboxing and binary translation
  • RISC-V-(n) → RISC-V-(n+1)
  • RISC-V-(n) → Intel i7 / AMD64 + AVX-512
  • RISC-V Linux ABI emulation on MacOS, Windows, Linux and *BSD
  • RISC-V Linux ABI randomisation and entropy coding
  • RISC-V Specification undefined behaviour investigation
  • RISC-V Virtualization and memory protection investigation

Supported Platforms

  • Target
    • RV32IMAFDC
    • RV64IMAFDC
    • Privilged ISA 1.9.1
  • Host
    • Linux (Debian 9.0 x86-64, Ubuntu 16.04 x86-64, Fedora 25 x86-64) (stable)
    • macOS (Sierra 10.11 x86-64) (stable)
    • FreeBSD (11 x86-64) (alpha)

Getting Started

Building riscv-gnu-toolchain

$ sudo apt-get install autoconf automake autotools-dev curl libmpc-dev libmpfr-dev libgmp-dev gawk build-essential bison flex texinfo gperf libtool patchutils bc zlib1g-dev
$ git clone https://github.com/riscv/riscv-gnu-toolchain.git
$ cd riscv-gnu-toolchain
$ git submodule update --init --recursive
$ ./configure --prefix=/opt/riscv/toolchain
$ make

Building rv8

$ export RISCV=/opt/riscv/toolchain
$ git clone https://github.com/rv8-io/rv8.git
$ cd rv8
$ git submodule update --init --recursive
$ make
$ sudo make install

Running rv8

$ make test-build
$ rv-jit build/riscv64-unknown-elf/bin/test-dhrystone

Build Dependencies

  • gmake
  • gcc-5.4 or clang-3.4 (Linux minimum)
  • gcc-6.3 or clang-3.8 (Linux recommended)
  • RISC-V GNU Toolchain
  • RISCV environment variable

Ubuntu 14.04LTS Dependencies

The compiler in Ubuntu 14.04LTS doesn't support C++14. These instructions will install g++6 from the ubuntu toolchain repository and build the project using g++6.

sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y
sudo apt-get update
sudo apt-get install g++-6
make CXX=g++-6 CC=gcc-6

Screenshots

ASCII map screenshot

Example ASCII map output from make map

Histogram screenshot

Example Instructions Histogram from rv-bin histogram

Histogram screenshot

Example Registers Histogram from rv-bin histogram

RISC-V disassembly screenshot

Example Disassembly output from rv-bin dump

See RISC-V Instruction Set Listing and RISC-V Instruction Types for sample LaTeX output.

Useful Commands

Command Description
make map print a colour opcode map
make latex output a LaTeX opcode tex
make pdf output a LaTeX opcode pdf
make test-spike run the ABI Proxy Simulator tests with spike
make test-sim run the ABI Proxy Simulator tests with rv-sim
make qemu-tests run the QEMU tests with rv-sim
make test-sys run the Privileged System Emulator tests with rv-sys
make linux bootstrap bbl, linux kernel and busybox image
sudo make install install to /usr/local/bin

Notes

  • The linux target requires the RISC-V GNU Linux Toolchain
  • The test-build target requires the RISC-V ELF Toolchain
  • The qemu-tests target requires the third_party/qemu-tests to be built

Project Structure

Directories

Directory Description
meta Instruction set metadata
src/abi Application binary interface
src/app Test programs and utilities
src/asm Assembler metadata library
src/elf ELF parser library
src/emu ISA simulator and emulator headers
src/mem Memory allocator library
src/model ISA metamodel library
src/rom Boot ROM generator
src/test ISA simulator and emulator tests
src/util Miscellaneous utilities library
doc/pdf Generated documentation

Libraries

The following table shows the RISC-V Meta libraries:

Name Description Scale
libriscv_asm.a ISA metadata and disassembly formatting micro
libriscv_elf.a ELF parser micro
libriscv_gen.a Source and documentation generators macro
libriscv_model.a Instruction set metamodel macro
libriscv_util.a Utility functions for tools mini

Program Options

RISC-V x86-64 Simulator

RISC-V x86-64 JIT Simulator command line options:

$ rv-jit -h
usage: rv-jit [<emulator_options>] [--] <elf_file> [<options>]
            --log-instructions, -l            Log Instructions
                --log-operands, -o            Log Instructions and Operands
                 --symbolicate, -S            Symbolicate addresses in instruction log
              --log-memory-map, -m            Log Memory Map Information
                --log-syscalls, -c            Log System Calls
               --log-registers, -r            Log Registers (defaults to integer registers)
               --log-jit-trace, -T            Log JIT trace
            --log-jit-regalloc, -T            Log JIT register allocation
              --log-exit-stats, -E            Log Registers and Statistics at Exit
             --save-exit-stats, -D <string>   Save Registers and Statistics at Exit
          --pc-usage-histogram, -P            Record program counter usage
    --register-usage-histogram, -R            Record register usage
 --instruction-usage-histogram, -I            Record instruction usage
                       --debug, -d            Start up in debugger CLI
                   --no-pseudo, -x            Disable Pseudoinstruction decoding
                   --no-fusion, -N            Disable JIT macro-op fusion
     --memory-mapped-registers, -M            Disable JIT host register mapping
              --update-instret, -i            Update instret in JIT code
                    --no-trace, -t            Disable JIT tracer
                       --audit, -a            Enable JIT audit
                 --trace-iters, -I <string>   Trace iterations
                        --seed, -s <string>   Random seed
                        --help, -h            Show help

Notes

  • Currently only the Linux syscall ABI proxy is implemented for the JIT simulator

RISC-V Proxy Simulator

The ABI Proxy Simulator command line options:

$ rv-sim -h
usage: rv-sim [<emulator_options>] [--] <elf_file> [<options>]
            --log-instructions, -l            Log Instructions
                --log-operands, -o            Log Instructions and Operands
                 --symbolicate, -S            Symbolicate addresses in instruction log
              --log-memory-map, -m            Log Memory Map Information
                --log-syscalls, -c            Log System Calls
               --log-registers, -r            Log Registers (defaults to integer registers)
              --log-exit-stats, -E            Log Registers and Statistics at Exit
             --save-exit-stats, -D <string>   Save Registers and Statistics at Exit
          --pc-usage-histogram, -P            Record program counter usage
    --register-usage-histogram, -R            Record register usage
 --instruction-usage-histogram, -I            Record instruction usage
                       --debug, -d            Start up in debugger CLI
                   --no-pseudo, -x            Disable Pseudoinstruction decoding
                        --seed, -s <string>   Random seed
                        --help, -h            Show help

To run the simple Hello World program (Proxy Mode):

rv-sim build/riscv64-unknown-elf/bin/hello-world-libc

RISC-V Full System Emulator

The Privilged ISA Full System Emulator command line options:

$ rv-sys -h
usage: rv-sys [<options>] <elf_file>
            --log-instructions, -l            Log Instructions
                --log-operands, -o            Log Instructions and Operands
                    --log-mmio, -O            Log Memory Mapped IO
              --log-memory-map, -m            Log Memory Map Information
               --log-mmode-csr, -M            Log Machine Control and Status Registers
               --log-smode-csr, -S            Log Supervisor Control and Status Registers
               --log-registers, -r            Log Registers (defaults to integer registers)
               --log-pagewalks, -v            Log Pagewalks
                  --log-config, -c            Log Config
                   --log-traps, -t            Log Traps
              --log-exit-stats, -E            Log Registers and Statistics at Exit
             --save-exit-stats, -D <string>   Save Registers and Statistics at Exit
          --pc-usage-histogram, -P            Record program counter usage
    --register-usage-histogram, -R            Record register usage
 --instruction-usage-histogram, -I            Record instruction usage
                       --debug, -d            Start up in debugger
                  --debug-trap, -T            Start up in debugger and enter debugger on trap
                   --no-pseudo, -x            Disable Pseudoinstruction decoding
                --map-physical, -p <string>   Map execuatable at physical address
                      --binary, -b <string>   Boot Binary ( 32, 64 )
                        --seed, -s <string>   Random seed
                        --help, -h            Show help

To run the privilged UART echo program (Privileged Mode):

rv-sys build/riscv64-unknown-elf/bin/test-m-mmio-uart

RISC-V ELF Dump Utility

ELF Dump usage command line options:

$ rv-bin dump -h
usage: dump [<options>] <elf_file>
                       --color, -c            Enable Color
            --print-elf-header, -e            Print ELF header
        --print-elf-header-ext, -x            Print ELF header (extended)
       --print-section-headers, -s            Print Section headers
       --print-program-headers, -p            Print Program headers
          --print-symbol-table, -t            Print Symbol Table
           --print-relocations, -r            Print Relocations
           --print-disassembly, -d            Print Disassembly
                      --pseudo, -P            Decode Pseudoinstructions
               --print-headers, -h            Print All Headers
                   --print-all, -a            Print All Headers and Disassembly
                        --help, -h            Show help

To run the ELF parser and disassembler:

rv-bin dump -c -a build/riscv64-unknown-elf/bin/hello-world-pcrel

Notes

  • The ELF dissassembler output requires 125 column terminal window

RISC-V ELF Histogram Utility

The ELF Histogram Utility usage command line options:

$ rv-bin histogram -h
usage: histogram [<options>] <elf_file>
                        --help, -h            Show help
                        --char, -c <string>   Character to use in bars
                        --bars, -b            Print bars next to counts
                --instructions, -I            Instruction Usage Histogram
                   --registers, -R            Register Usage Histogram
          --registers-operands, -P            Register Usage Histogram (with operand positions)
                   --max-chars, -m <string>   Maximum number of characters for bars
                --reverse-sort, -r            Sort in Reverse

To print the top 20 instructions in a RISC-V ELF binary:

rv-bin histogram -I -b -c â–ˆ linux/vmlinux | head -20

To print the top 20 registers in a RISC-V ELF binary:

rv-bin histogram -R -b -c â–ˆ linux/vmlinux | head -20

RISC-V Metadata Utility

The RV source and documentation generator usage command line options:

$ rv-meta -h
usage: rv-meta [<options>]
                        --help, -h            Show help
                  --isa-subset, -I <string>   ISA subset (e.g. RV32IMA, RV32G, RV32GSC, RV64IMA, RV64G, RV64GSC)
                    --read-isa, -r <string>   Read instruction set metadata from directory
                  --no-comment, -N            Don't emit comments in generated source
           --numeric-constants, -0            Use numeric constants in generated source
         --print-constraints-h, -XC           Print constraints header
            --print-fpu-test-h, -FH           Print FPU test header
            --print-fpu-test-c, -FC           Print FPU test source
              --print-interp-h, -V            Print interpreter header
                 --print-jit-h, -J            Print jit header
                --print-jit-cc, -K            Print jit source
   --substitute-question-marks, -?            Substitute question marks for zeros in LaTeX output
                 --print-latex, -l            Print LaTeX instruction listing
                       --color, -c            Enable ANSI color map
                   --print-map, -m            Print instruction map
      --print-map-pseudocode-c, -mc           Print instruction map with C pseudocode
    --print-map-pseudocode-alt, -ma           Print instruction map with alternate pseudocode
              --print-markdown, -md           Print instruction reference in markdown
                --print-meta-h, -H            Print metadata header
               --print-meta-cc, -C            Print metadata source
            --print-operands-h, -A            Print operands header
             --print-strings-h, -SH           Print strings header
            --print-strings-cc, -SC           Print strings source
              --print-switch-h, -S            Print switch header

To print a colour opcode map for the RV32IMA ISA subset:

rv-meta -I RV32IMA -m -c -r meta

To print a colour opcode map for the RV64IMAFDS ISA subset:

rv-meta -I RV64IMAFDS -m -c -r meta

To output LaTeX for the RV32C ISA subset:

rv-meta -I RV32C -l -r meta

To output LaTeX for the RV64G ISA subset:

rv-meta -I RV64G -l -r meta

References

More Repositories

1

riscv-probe

Simple machine mode program to probe RISC-V control and status registers
C
116
star
2

riscv-disassembler

RISC-V Disassembler with support for RV32/RV64/RV128 IMAFDC
C
97
star
3

busybear-linux

busybear-linux is a tiny RISC-V Linux root filesystem image that targets the VirtIO board in riscv-qemu.
Shell
95
star
4

glkitty

port of the OpenGL gears demo to kitty terminal graphics protocol
C
90
star
5

ethical_hashmap

Fast open addressing hash table
C++
41
star
6

riscv-meta

RISC-V Instruction Set Metadata
39
star
7

crefl

crefl is a runtime library and compiler plug-in to support reflection in C.
C++
37
star
8

bignum

C++ bignum with support for arbitrary precision integer arithmetic.
C++
27
star
9

aes-gcm

Benchmarking AES-GCM on GPUs with OpenCL
C
25
star
10

glyb

glyb is an experimental graphics API agnostic text rendering, text layout, and vector canvas library.
C++
23
star
11

riscv-atomics

RISC-V implementation of the C/C++ Atomic operations library
C
19
star
12

rv8-bench

rv8 benchmark suite
C
18
star
13

musl-riscv-toolchain

riscv-linux musl gcc toolchain bootstrap scripts
Shell
17
star
14

zvec

zip_vector in-memory compressed variable length integer array
C++
17
star
15

queue_atomic

Multiple producer multiple consumer queue template using C++11 atomics.
C++
13
star
16

userblk

Linux Userspace Block Device Driver Interface
C
10
star
17

glxsync

An example X Windows OpenGL application using GLX and XSync extended frame synchronization
C
10
star
18

vf128

vf128 variable length floating-point
C++
10
star
19

vlu

Variable Length Unary (VLU) - little-endian variable length integer coding
C++
9
star
20

libSystem-mmap

memory map interposition for libsystem_malloc.dylib
C
9
star
21

glcube

OpenGL CMake project template using C99 linmath.h
C
8
star
22

michaeljclark.github.io

RISC-V simulator for x86-64
SCSS
8
star
23

latypus

a high performance C++1y m:n threaded HTTP client and server
C++
8
star
24

graphstorm

OpenCL accelerated force-directed graph layout engine
C
7
star
25

stdendian

(unofficial) proposal for stdendian.h header providing cross-platform endian macros and endian conversion functions.
C++
7
star
26

cpipe

low latency concurrent pipe buffer using C11 threads and atomics.
C
7
star
27

mumule

simple thread pool implementation using the C11 thread support library
C
6
star
28

c-fmt

C++14 sprintf like variadic template string formatter
C++
5
star
29

nat

Arbitrary precision arithmetic library with simple expression compiler targetting a subset of the RISC-V ISA
C++
5
star
30

vecmap

cache optimized map using lower_bound and vector<pair<Key,T>>
C++
4
star
31

llvlir

Low Level Variable Length Intermediate Representation
Python
4
star
32

musl-xnu

work in progress port of musl libc to macOS x86-64 XNU kernel
C
4
star
33

glhash

glhash is an OpenGL compute shader implementing SHA-2-256 in GLSL.
C
4
star
34

cutty

OpenGL terminal emulator
C++
4
star
35

c11atomic

C11 stdatomic.h compat for MSVC++
C
4
star
36

riscv-attacks

RISC-V software vulnerability exploitation examples and fixes
C
3
star
37

async_tls

async TLS example using openssl
C++
3
star
38

xi

Xi (aka ξ), a search tool for the Unicode Character Database.
C++
3
star
39

vkgears

Port of the iconic gears demo to GLSL and the Vulkan API using GLFW.
C
3
star
40

qemu-riscv-tests

builds and runs riscv-tests and qemu-tests in qemu-system-riscv32 and qemu-system-riscv64
Assembly
3
star
41

sushi

Xcode, Visual Studio and Ninja project generator
C++
3
star
42

cpuident

x86-64 cpuid processor info tool
C
2
star
43

maj2random

maj2random is a floating point hash function derived from SHA-2
C
2
star
44

chashmap

simple open addressing hashmap, and linked hashmap in C11.
C
2
star
45

libedit

cross-platform port of NetBSD libedit to Linux and Windows.
C
1
star
46

glhull

experiment to render béziergon convex interior hulls.
C
1
star
47

emu

Tiny x86 emulator example using the Windows Hypervisor Platform API.
SourcePawn
1
star