The model is written in the Sail language. Although specifications in Sail are quite readable as pseudocode, it would help to have the Sail manual handy.
The Sail modules in the model directory have the structure shown
below. Arrows indicate a dependency relationship, and italics
indicate fragments that are not strictly part of the specification,
such as the platform memory map.
-
riscv_xlen32.sailandriscv_xlen64.saildefinexlenfor RV32 and RV64. One of them is chosen during the build using the ARCH variable. -
prelude_*.sailcontain useful Sail library functions. These files should be referred to as needed. The lowest level memory access primitives are defined inprelude_mem.sail, and are implemented by the various Sail backends.prelude_mem.saildepends on the value ofxlen. -
riscv_types.sailcontains some basic RISC-V definitions. This file should be read first, since these definitions are used throughout the specification, such as privilege levels, register indices, interrupt and exception definitions and enumerations, and types used to define memory accesses. The register type is separately defined inriscv_reg_type.sailso that extensions of the model can redefine it if required. -
riscv_regs.sailcontains the base register file, where each register is defined as having theregtypetype defined inriscv_reg_type.sailand indexed by the indices defined inriscv_types.sail. -
riscv_pc_access.saildefines functions to access and modify the program counter. -
riscv_sys_regs.saildescribes the privileged architectural state, viz. M-mode and S-mode CSRs, and contains helpers to interpret their content, such as WLRL and WARL fields.riscv_sys_control.saildescribes interrupt and exception delegation and dispatch, and the handling of privilege transitions.riscv_sys_exceptions.saildefines the handling of the addresses involved in exception handling.riscv_sync_exception.saildescribes the structure used to capture the architectural information for an exception.Since WLRL and WARL fields are intended to capture platform-specific functionality, future versions of the model might separate their handling functions out into a separate platform-defined file. The current implementation of these functions usually implement the same behavior as the Spike emulator.
-
riscv_pmp_regs.sailandriscv_pmp_control.sailimplement support for physical memory protection (PMP).riscv_pmp_regshandle read and write access to the PMP registers, whileriscv_pmp_controlimplements the permission checking and matching priority. -
riscv_platform.sailcontains platform-specific functionality for the model. It contains the physical memory map, the local interrupt controller, and the MMIO interfaces to the clock, timer and terminal devices. Sailexterndefinitions are used to connect externally provided (i.e. external to the Sail model) platform functionality, such as those provided by the platform support in the C and OCaml emulators. This file also contains the externally selectable options for platform behavior, such as the handling of misaligned memory accesses, the handling of PTE dirty-bit updates during address translation, etc. These platform options can be specified via command line switches in the C and OCaml emulators. -
riscv_mem.sailcontains the functions that convert accesses to physical addresses into accesses to physical memory, or MMIO accesses to the devices provided by the platform, or into the appropriate access fault. This file also contains definitions that are used in the weak memory concurrency model. -
The
riscv_vmem_*.sailfiles describe the S-mode address translation.riscv_vmem_typesandriscv_vmem_common.sailcontain the definitions and processing of the page-table entries and their various permission and status bits.riscv_types_ext,riscv_vmem_sv32.sail,riscv_vmem_sv39.sail, andriscv_vmem_sv48.sailcontain the specifications for the corresponding page-table walks, andriscv_vmem_rv32.sailandriscv_vmem_rv64.saildescribe the top-level address translation for the corresponding architectures. -
The
riscv_addr_checks_common.sailandriscv_addr_checks.sailcontain extension hooks to support the checking and transformation of memory addresses during the execution of an instruction. The transformed addresses are used for any address translation; however, any memory access exceptions are reported in terms of the original memory address (i.e. the one generated by the instruction, not the hook). -
Files matching
riscv_insts_*.sailcapture the instruction definitions and their assembly language formats. Each file contains the instructions for an extension, withriscv_insts_base.sailcontaining the base integer instruction set. Each instruction is represented as a variant clause of theasttype, and its execution semantics are represented as a clause of theexecutefunction.mappingclauses specify the encoding and decoding of each instruction to and from their binary representations and assembly language formats. Hint instructions that are not implicitly handled elsewhere are explicitly handled inriscv_insts_hints.sail. -
riscv_fetch.sailcontains the instruction fetch function. It supports checking and transformation of the fetch address as described above. -
riscv_step.sailimplements the top-level fetch and execute loop. Thefetchis done in 16-bit granules to handle RVC instructions. Thestepfunction performs the instruction fetch, handles any fetch errors, decodes the fetched value, dispatches the execution of the decoded instruction, and checks for any pending interrupts that may need to be handled. Aloopfunction implements the execute loop, and uses the same HTIF (host-target interface) mechanism as the Spike emulator to detect termination of execution. -
riscv_analysis.sailis used in the formal operational RVWMO memory model.
Note that the files above are listed in dependency order, i.e. files earlier in the order do not depend on later files.
The diagram below illustrates how the C emulator is built from the Sail model. The OCaml emulator follows the same approach.
The nodes that are not colored are the handwritten C files for the C emulator. The black arrows indicate dependency relationships, while the red arrow indicates a file generated by the Sail compiler from Sail source files.
riscv_sim is the top level file for the C emulator: it processes
command line options, initializes the platform model with any ISA
implementation choices if specified, and loads the ELF program or OS
image into raw memory, including any ROM firmware such as the Berkeley
boot loader and DeviceTree binary blobs, and initializes the memory
map.
The generated C model riscv_model_$ARCH is built from the Sail
sources by the Sail compiler for the specified architecture $ARCH,
either RV32 or RV64. It contains calls to the platform interface
riscv_platform for platform-specific information; the latter is
typically defined as externally specified in the Sail file
riscv_platform.sail.
The Sail system provides a C library for use with its C backend, which provides the low-level details of the implementation of raw memory and bitvectors (typically optimized to use the native machine word representation).