Crate riscv_rt [] [src]

Minimal startup / runtime for RISCV CPU's


This crate provides

  • Before main initialization of the .bss and .data sections.

  • Before main initialization of the FPU (for targets that have a FPU).

  • A panic_fmt implementation that just calls abort that you can opt into through the "abort-on-panic" Cargo feature. If you don't use this feature you'll have to provide the panic_fmt lang item yourself. Documentation here

  • A minimal start lang item to support the standard fn main() interface. (The processor goes to sleep after returning from main)

  • 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).

  • A _sheap symbol at whose address you can locate a heap.

$ cargo new --bin app && cd $_

$ # add this crate as a dependency
$ edit Cargo.toml && cat $_
version = "0.1.0"

$ # tell Xargo which standard crates to build
$ edit Xargo.toml && cat $_
stage = 0

features = ["mem"]
stage = 1

$ # memory layout of the device
$ edit memory.x && cat $_
  /* NOTE K = KiBi = 1024 bytes */
  FLASH : ORIGIN = 0x08000000, LENGTH = 128K
  RAM : ORIGIN = 0x20000000, LENGTH = 8K

$ edit src/ && cat $_
Be careful when using this code, it's not being tested!

extern crate riscv_rt;

fn main() {
    // do something here
$ cargo install xargo

$ xargo rustc --target riscv32-unknown-none -- \
   -C link-arg=-Tlink.x -C linker=riscv32-unknown-elf-ld -Z linker-flavor=ld

$ riscv32-unknown-elf-objdump -Cd $(find target -name app) | head

Disassembly of section .text:

20400000 <_start>:
20400000:   800011b7    lui gp,0x80001
20400004:   80018193    addi    gp,gp,-2048 # 80000800 <_stack_start+0xffffc800>
20400008:   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.


This file supplies the information about the device to the linker.


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 two memory regions: one for Flash memory and another for RAM.

The program instructions (the .text section) will be stored in the memory region named FLASH, and the program static variables (the sections .bss and .data) will be allocated in the memory region named RAM.


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).

If omitted this symbol value will default to ORIGIN(RAM) + LENGTH(RAM).


Allocating the call stack on a different RAM region.

  /* call stack will go here */
  CCRAM : ORIGIN = 0x10000000, LENGTH = 8K
  FLASH : ORIGIN = 0x08000000, LENGTH = 256K
  /* static variables will go here */
  RAM : ORIGIN = 0x20000000, LENGTH = 40K

_stack_start = ORIGIN(CCRAM) + LENGTH(CCRAM);


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.


extern crate some_allocator;

// Size of the heap in bytes
const SIZE: usize = 1024;

extern "C" {
    static mut _sheap: u8;

fn main() {
    unsafe {
        let start_address = &mut _sheap as *mut u8;
        some_allocator::initialize(start_address, SIZE);







Machine Cause CSR (mcause) is ReadOnly. Trap Cause



Rust entry point (_start_rust)


Trap entry point rust (_start_trap_rust)