Expand description
Minimal startup / runtime for RISC-V CPU’s
§Minimum Supported Rust Version (MSRV)
This crate is guaranteed to compile on stable Rust 1.60 and up. It might compile with older versions but that may change in any new patch release.
§Features
This crate provides
-
Before main initialization of the
.bss
and.data
sections. -
#[entry]
to declare the entry point of the program -
#[pre_init]
to run code beforestatic
variables are initialized -
A linker script that encodes the memory layout of a generic RISC-V microcontroller. This linker script is missing some information that must be supplied through a
memory.x
file (see example below). This file must be supplied using rustflags and listed beforelink.x
. Arbitrary filename can be use instead ofmemory.x
. -
A
_sheap
symbol at whose address you can locate a heap. -
Support for a runtime in supervisor mode, that can be bootstrapped via Supervisor Binary Interface (SBI)
$ cargo new --bin app && cd $_
$ # add this crate as a dependency
$ edit Cargo.toml && cat $_
[dependencies]
riscv-rt = "0.6.1"
panic-halt = "0.2.0"
$ # memory layout of the device
$ edit memory.x && cat $_
MEMORY
{
RAM : ORIGIN = 0x80000000, LENGTH = 16K
FLASH : ORIGIN = 0x20000000, LENGTH = 16M
}
REGION_ALIAS("REGION_TEXT", FLASH);
REGION_ALIAS("REGION_RODATA", FLASH);
REGION_ALIAS("REGION_DATA", RAM);
REGION_ALIAS("REGION_BSS", RAM);
REGION_ALIAS("REGION_HEAP", RAM);
REGION_ALIAS("REGION_STACK", RAM);
$ edit src/main.rs && cat $_
#![no_std]
#![no_main]
extern crate panic_halt;
use riscv_rt::entry;
// use `main` as the entry point of this application
// `main` is not allowed to return
#[entry]
fn main() -> ! {
// do something here
loop { }
}
$ mkdir .cargo && edit .cargo/config && cat $_
[target.riscv32imac-unknown-none-elf]
rustflags = [
"-C", "link-arg=-Tmemory.x",
"-C", "link-arg=-Tlink.x",
]
[build]
target = "riscv32imac-unknown-none-elf"
$ edit build.rs && cat $_
use std::env;
use std::fs;
use std::path::PathBuf;
fn main() {
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
// Put the linker script somewhere the linker can find it.
fs::write(out_dir.join("memory.x"), include_bytes!("memory.x")).unwrap();
println!("cargo:rustc-link-search={}", out_dir.display());
println!("cargo:rerun-if-changed=memory.x");
println!("cargo:rerun-if-changed=build.rs");
}
$ cargo build
$ riscv32-unknown-elf-objdump -Cd $(find target -name app) | head
Disassembly of section .text:
20000000 <_start>:
20000000: 800011b7 lui gp,0x80001
20000004: 80018193 addi gp,gp,-2048 # 80000800 <_stack_start+0xffffc800>
20000008: 80004137 lui sp,0x80004
§Symbol interfaces
This crate makes heavy use of symbols, linker sections and linker scripts to provide most of its functionality. Below are described the main symbol interfaces.
§memory.x
This file supplies the information about the device to the linker.
§MEMORY
The main information that this file must provide is the memory layout of
the device in the form of the MEMORY
command. The command is documented
here, but at a minimum you’ll want to create at least one memory region.
To support different relocation models (RAM-only, FLASH+RAM) multiple regions are used:
REGION_TEXT
- for.init
,.trap
and.text
sectionsREGION_RODATA
- for.rodata
section and storing initial values for.data
sectionREGION_DATA
- for.data
sectionREGION_BSS
- for.bss
sectionREGION_HEAP
- for the heap areaREGION_STACK
- for hart stacks
Specific aliases for these regions must be defined in memory.x
file (see example below).
§_stext
This symbol provides the loading address of .text
section. This value can be changed
to override the loading address of the firmware (for example, in case of bootloader present).
If omitted this symbol value will default to ORIGIN(REGION_TEXT)
.
§_stack_start
This symbol provides the address at which the call stack will be allocated. The call stack grows downwards so this address is usually set to the highest valid RAM address plus one (this is an invalid address but the processor will decrement the stack pointer before using its value as an address).
In case of multiple harts present, this address defines the initial stack pointer for hart 0.
Stack pointer for hart N
is calculated as _stack_start - N * _hart_stack_size
.
If omitted this symbol value will default to ORIGIN(REGION_STACK) + LENGTH(REGION_STACK)
.
§Example
Allocating the call stack on a different RAM region.
MEMORY
{
L2_LIM : ORIGIN = 0x08000000, LENGTH = 1M
RAM : ORIGIN = 0x80000000, LENGTH = 16K
FLASH : ORIGIN = 0x20000000, LENGTH = 16M
}
REGION_ALIAS("REGION_TEXT", FLASH);
REGION_ALIAS("REGION_RODATA", FLASH);
REGION_ALIAS("REGION_DATA", RAM);
REGION_ALIAS("REGION_BSS", RAM);
REGION_ALIAS("REGION_HEAP", RAM);
REGION_ALIAS("REGION_STACK", L2_LIM);
_stack_start = ORIGIN(L2_LIM) + LENGTH(L2_LIM);
§_max_hart_id
This symbol defines the maximum hart id supported. All harts with id
greater than _max_hart_id
will be redirected to abort()
.
This symbol is supposed to be redefined in platform support crates for multi-core targets.
If omitted this symbol value will default to 0 (single core).
§_hart_stack_size
This symbol defines stack area size for one hart.
If omitted this symbol value will default to 2K.
§_heap_size
This symbol provides the size of a heap region. The default value is 0. You can set _heap_size
to a non-zero value if you are planning to use heap allocations.
§_sheap
This symbol is located in RAM right after the .bss
and .data
sections.
You can use the address of this symbol as the start address of a heap
region. This symbol is 4 byte aligned so that address will be a multiple of 4.
§Example
extern crate some_allocator;
extern "C" {
static _sheap: u8;
static _heap_size: u8;
}
fn main() {
unsafe {
let heap_bottom = &_sheap as *const u8 as usize;
let heap_size = &_heap_size as *const u8 as usize;
some_allocator::initialize(heap_bottom, heap_size);
}
}
§_mp_hook
This function is called from all the harts and must return true only for one hart,
which will perform memory initialization. For other harts it must return false
and implement wake-up in platform-dependent way (e.g. after waiting for a user interrupt).
The parameter hartid
specifies the hartid of the caller.
This function can be redefined in the following way:
#[export_name = "_mp_hook"]
pub extern "Rust" fn mp_hook(hartid: usize) -> bool {
// ...
}
Default implementation of this function wakes hart 0 and busy-loops all the other harts.
§Core exception handlers
This functions are called when corresponding exception occurs. You can define an exception handler with one of the following names:
InstructionMisaligned
InstructionFault
IllegalInstruction
Breakpoint
LoadMisaligned
LoadFault
StoreMisaligned
StoreFault
UserEnvCall
SupervisorEnvCall
MachineEnvCall
InstructionPageFault
LoadPageFault
StorePageFault
For example:
#[export_name = "MachineEnvCall"]
fn custom_menv_call_handler(trap_frame: &riscv_rt::TrapFrame) {
// ...
}
or
#[no_mangle]
fn MachineEnvCall(trap_frame: &riscv_rt::TrapFrame) -> ! {
// ...
}
If exception handler is not explicitly defined, ExceptionHandler
is called.
§ExceptionHandler
This function is called when exception without defined exception handler is occured.
The exception reason can be decoded from the
mcause
/scause
register.
This function can be redefined in the following way:
#[export_name = "ExceptionHandler"]
fn custom_exception_handler(trap_frame: &riscv_rt::TrapFrame) -> ! {
// ...
}
or
#[no_mangle]
fn ExceptionHandler(trap_frame: &riscv_rt::TrapFrame) -> ! {
// ...
}
Default implementation of this function stucks in a busy-loop.
§Core interrupt handlers
This functions are called when corresponding interrupt is occured. You can define an interrupt handler with one of the following names:
UserSoft
SupervisorSoft
MachineSoft
UserTimer
SupervisorTimer
MachineTimer
UserExternal
SupervisorExternal
MachineExternal
For example:
#[export_name = "MachineTimer"]
fn custom_timer_handler() {
// ...
}
or
#[no_mangle]
fn MachineTimer() {
// ...
}
If interrupt handler is not explicitly defined, DefaultHandler
is called.
§DefaultHandler
This function is called when interrupt without defined interrupt handler is occured.
The interrupt reason can be decoded from the mcause
/scause
register.
This function can be redefined in the following way:
#[export_name = "DefaultHandler"]
fn custom_interrupt_handler() {
// ...
}
or
#[no_mangle]
fn DefaultHandler() {
// ...
}
Default implementation of this function stucks in a busy-loop.
§Features
§single-hart
This feature saves a little code size if there is only one hart on the target.
§s-mode
The supervisor mode feature (s-mode
) can be activated via Cargo features.
For example:
[dependencies]
riscv-rt = {features=["s-mode"]}
Internally, riscv-rt uses different versions of precompiled static libraries
for (i) machine mode and (ii) supervisor mode. If the s-mode
feature was activated,
the build script selects the s-mode library. While most registers/instructions have variants for
both mcause
and scause
, the mhartid
hardware thread register is not available in supervisor
mode. Instead, the hartid is passed as parameter by a bootstrapping firmware (i.e., SBI).
Use case: QEMU supports OpenSBI as default firmware. Using the SBI requires riscv-rt to be run in supervisor mode instead of machine mode.
APP_BINARY=$(find target -name app)
sudo qemu-system-riscv64 -m 2G -nographic -machine virt -kernel $APP_BINARY
It requires the memory layout to be non-overlapping, like
MEMORY
{
RAM : ORIGIN = 0x80200000, LENGTH = 0x8000000
FLASH : ORIGIN = 0x20000000, LENGTH = 16M
}
Structs§
- Registers saved in trap handler
Functions§
- Rust entry point (_start_rust)
- Trap entry point rust (_start_trap_rust)
Attribute Macros§
- Attribute to declare the entry point of the program
- Attribute to mark which function will be called at the beginning of the reset handler.