4
0

lib.rs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. //! Minimal startup / runtime for RISCV CPU's
  2. //!
  3. //! # Features
  4. //!
  5. //! This crate provides
  6. //!
  7. //! - Before main initialization of the `.bss` and `.data` sections.
  8. //!
  9. //! - Before main initialization of the FPU (for targets that have a FPU).
  10. //!
  11. //! - A `panic_fmt` implementation that just calls abort that you can opt into
  12. //! through the "abort-on-panic" Cargo feature. If you don't use this feature
  13. //! you'll have to provide the `panic_fmt` lang item yourself. Documentation
  14. //! [here][1]
  15. //!
  16. //! [1]: https://doc.rust-lang.org/unstable-book/language-features/lang-items.html
  17. //!
  18. //! - A minimal `start` lang item to support the standard `fn main()`
  19. //! interface. (The processor goes to sleep after returning from `main`)
  20. //!
  21. //! - A linker script that encodes the memory layout of a generic RISC-V
  22. //! microcontroller. This linker script is missing some information that must
  23. //! be supplied through a `memory.x` file (see example below).
  24. //!
  25. //! - A `_sheap` symbol at whose address you can locate a heap.
  26. //!
  27. //! ``` text
  28. //! $ cargo new --bin app && cd $_
  29. //!
  30. //! $ # add this crate as a dependency
  31. //! $ edit Cargo.toml && cat $_
  32. //! [dependencies.riscv-rt]
  33. //! version = "0.1.0"
  34. //!
  35. //! $ # tell Xargo which standard crates to build
  36. //! $ edit Xargo.toml && cat $_
  37. //! [dependencies.core]
  38. //! stage = 0
  39. //!
  40. //! [dependencies.compiler_builtins]
  41. //! features = ["mem"]
  42. //! stage = 1
  43. //!
  44. //! $ # memory layout of the device
  45. //! $ edit memory.x && cat $_
  46. //! MEMORY
  47. //! {
  48. //! /* NOTE K = KiBi = 1024 bytes */
  49. //! FLASH : ORIGIN = 0x08000000, LENGTH = 128K
  50. //! RAM : ORIGIN = 0x20000000, LENGTH = 8K
  51. //! }
  52. //!
  53. //! $ edit src/main.rs && cat $_
  54. //! ```
  55. //!
  56. //! ``` ignore,no_run
  57. //! #![no_std]
  58. //!
  59. //! extern crate riscv_rt;
  60. //!
  61. //! fn main() {
  62. //! // do something here
  63. //! }
  64. //! ```
  65. //!
  66. //! ``` text
  67. //! $ cargo install xargo
  68. //!
  69. //! $ xargo rustc --target riscv32-unknown-none -- \
  70. //! -C link-arg=-Tlink.x -C linker=riscv32-unknown-elf-ld -Z linker-flavor=ld
  71. //!
  72. //! $ riscv32-unknown-elf-objdump -Cd $(find target -name app) | head
  73. //!
  74. //! Disassembly of section .text:
  75. //!
  76. //! 20400000 <_start>:
  77. //! 20400000: 800011b7 lui gp,0x80001
  78. //! 20400004: 80018193 addi gp,gp,-2048 # 80000800 <_stack_start+0xffffc800>
  79. //! 20400008: 80004137 lui sp,0x80004
  80. //! ```
  81. //!
  82. //! # Symbol interfaces
  83. //!
  84. //! This crate makes heavy use of symbols, linker sections and linker scripts to
  85. //! provide most of its functionality. Below are described the main symbol
  86. //! interfaces.
  87. //!
  88. //! ## `memory.x`
  89. //!
  90. //! This file supplies the information about the device to the linker.
  91. //!
  92. //! ### `MEMORY`
  93. //!
  94. //! The main information that this file must provide is the memory layout of
  95. //! the device in the form of the `MEMORY` command. The command is documented
  96. //! [here][2], but at a minimum you'll want to create two memory regions: one
  97. //! for Flash memory and another for RAM.
  98. //!
  99. //! [2]: https://sourceware.org/binutils/docs/ld/MEMORY.html
  100. //!
  101. //! The program instructions (the `.text` section) will be stored in the memory
  102. //! region named FLASH, and the program `static` variables (the sections `.bss`
  103. //! and `.data`) will be allocated in the memory region named RAM.
  104. //!
  105. //! ### `_stack_start`
  106. //!
  107. //! This symbol provides the address at which the call stack will be allocated.
  108. //! The call stack grows downwards so this address is usually set to the highest
  109. //! valid RAM address plus one (this *is* an invalid address but the processor
  110. //! will decrement the stack pointer *before* using its value as an address).
  111. //!
  112. //! If omitted this symbol value will default to `ORIGIN(RAM) + LENGTH(RAM)`.
  113. //!
  114. //! #### Example
  115. //!
  116. //! Allocating the call stack on a different RAM region.
  117. //!
  118. //! ```
  119. //! MEMORY
  120. //! {
  121. //! /* call stack will go here */
  122. //! CCRAM : ORIGIN = 0x10000000, LENGTH = 8K
  123. //! FLASH : ORIGIN = 0x08000000, LENGTH = 256K
  124. //! /* static variables will go here */
  125. //! RAM : ORIGIN = 0x20000000, LENGTH = 40K
  126. //! }
  127. //!
  128. //! _stack_start = ORIGIN(CCRAM) + LENGTH(CCRAM);
  129. //! ```
  130. //!
  131. //! ### `_sheap`
  132. //!
  133. //! This symbol is located in RAM right after the `.bss` and `.data` sections.
  134. //! You can use the address of this symbol as the start address of a heap
  135. //! region. This symbol is 4 byte aligned so that address will be a multiple of
  136. //! 4.
  137. //!
  138. //! #### Example
  139. //!
  140. //! ```
  141. //! extern crate some_allocator;
  142. //!
  143. //! // Size of the heap in bytes
  144. //! const SIZE: usize = 1024;
  145. //!
  146. //! extern "C" {
  147. //! static mut _sheap: u8;
  148. //! }
  149. //!
  150. //! fn main() {
  151. //! unsafe {
  152. //! let start_address = &mut _sheap as *mut u8;
  153. //! some_allocator::initialize(start_address, SIZE);
  154. //! }
  155. //! }
  156. //! ```
  157. // NOTE: Adapted from cortex-m/src/lib.rs
  158. #![no_std]
  159. #![deny(missing_docs)]
  160. #![deny(warnings)]
  161. #![feature(asm)]
  162. #![feature(compiler_builtins_lib)]
  163. #![feature(const_fn)]
  164. #![feature(global_asm)]
  165. #![feature(lang_items)]
  166. #![feature(linkage)]
  167. #![feature(naked_functions)]
  168. #![feature(used)]
  169. extern crate compiler_builtins;
  170. extern crate riscv;
  171. extern crate r0;
  172. mod lang_items;
  173. use riscv::{asm, csr};
  174. pub use riscv::csr::{Trap, Interrupt, Exception};
  175. extern "C" {
  176. // NOTE `rustc` forces this signature on us. See `src/lang_items.rs`
  177. fn main(argc: isize, argv: *const *const u8) -> isize;
  178. // Boundaries of the .bss section
  179. static mut _ebss: u32;
  180. static mut _sbss: u32;
  181. // Boundaries of the .data section
  182. static mut _edata: u32;
  183. static mut _sdata: u32;
  184. // Initial values of the .data section (stored in Flash)
  185. static _sidata: u32;
  186. // Address of _start_trap
  187. static _start_trap: u32;
  188. }
  189. /// Entry point of all programs (_start).
  190. ///
  191. /// It initializes DWARF call frame information, the stack pointer, the
  192. /// frame pointer (needed for closures to work in start_rust) and the global
  193. /// pointer. Then it calls _start_rust.
  194. #[cfg(target_arch = "riscv")]
  195. global_asm!(r#"
  196. .section .init
  197. .globl _start
  198. _start:
  199. .cfi_startproc
  200. .cfi_undefined ra
  201. // .option push
  202. // .option norelax
  203. lui gp, %hi(__global_pointer$)
  204. addi gp, gp, %lo(__global_pointer$)
  205. // .option pop
  206. lui sp, %hi(_stack_start)
  207. addi sp, sp, %lo(_stack_start)
  208. add s0, sp, zero
  209. jal zero, _start_rust
  210. .cfi_endproc
  211. "#);
  212. /// Rust entry point (_start_rust)
  213. ///
  214. /// Zeros bss section, initializes data section and calls main. This function
  215. /// never returns.
  216. #[naked]
  217. #[link_section = ".init.rust"]
  218. #[export_name = "_start_rust"]
  219. pub extern "C" fn start_rust() -> ! {
  220. unsafe {
  221. r0::zero_bss(&mut _sbss, &mut _ebss);
  222. r0::init_data(&mut _sdata, &mut _edata, &_sidata);
  223. }
  224. // TODO: Enable FPU when available
  225. // Set mtvec to _start_trap
  226. #[cfg(target_arch = "riscv")]
  227. unsafe {
  228. // csr::mtvec.write(|w| w.bits(_start_trap));
  229. asm!("csrrw zero, 0x305, $0"
  230. :
  231. : "r"(&_start_trap)
  232. :
  233. : "volatile");
  234. }
  235. // Neither `argc` or `argv` make sense in bare metal context so we
  236. // just stub them
  237. unsafe {
  238. main(0, ::core::ptr::null());
  239. }
  240. // If `main` returns, then we go into "reactive" mode and simply attend
  241. // interrupts as they occur.
  242. loop {
  243. asm::wfi();
  244. }
  245. }
  246. /// Trap entry point (_start_trap)
  247. ///
  248. /// Saves caller saved registers ra, t0..6, a0..7, calls _start_trap_rust,
  249. /// restores caller saved registers and then returns.
  250. #[cfg(target_arch = "riscv")]
  251. global_asm!(r#"
  252. .section .trap
  253. .align 4
  254. .global _start_trap
  255. _start_trap:
  256. addi sp, sp, -16*4
  257. sw ra, 1*4(sp)
  258. sw t0, 2*4(sp)
  259. sw t1, 3*4(sp)
  260. sw t2, 4*4(sp)
  261. sw t3, 5*4(sp)
  262. sw t4, 6*4(sp)
  263. sw t5, 7*4(sp)
  264. sw t6, 8*4(sp)
  265. sw a0, 9*4(sp)
  266. sw a1, 10*4(sp)
  267. sw a2, 11*4(sp)
  268. sw a3, 12*4(sp)
  269. sw a4, 13*4(sp)
  270. sw a5, 14*4(sp)
  271. sw a6, 15*4(sp)
  272. sw a7, 16*4(sp)
  273. jal ra, _start_trap_rust
  274. lw ra, 1*4(sp)
  275. lw t0, 2*4(sp)
  276. lw t1, 3*4(sp)
  277. lw t2, 4*4(sp)
  278. lw t3, 5*4(sp)
  279. lw t4, 6*4(sp)
  280. lw t5, 7*4(sp)
  281. lw t6, 8*4(sp)
  282. lw a0, 9*4(sp)
  283. lw a1, 10*4(sp)
  284. lw a2, 11*4(sp)
  285. lw a3, 12*4(sp)
  286. lw a4, 13*4(sp)
  287. lw a5, 14*4(sp)
  288. lw a6, 15*4(sp)
  289. lw a7, 16*4(sp)
  290. addi sp, sp, 16*4
  291. mret
  292. "#);
  293. /// Trap entry point rust (_start_trap_rust)
  294. ///
  295. /// mcause is read to determine the cause of the trap. XLEN-1 bit indicates
  296. /// if it's an interrupt or an exception. The result is converted to an element
  297. /// of the Interrupt or Exception enum and passed to handle_interrupt or
  298. /// handle_exception.
  299. #[link_section = ".trap.rust"]
  300. #[export_name = "_start_trap_rust"]
  301. pub extern "C" fn start_trap_rust() {
  302. // dispatch trap to handler
  303. trap_handler(csr::mcause.read().cause());
  304. // mstatus, remain in M-mode after mret
  305. csr::mstatus.set(|w| w.mpp(csr::MPP::Machine));
  306. }
  307. /// Default Trap Handler
  308. #[used]
  309. #[no_mangle]
  310. #[linkage = "weak"]
  311. fn trap_handler(_: Trap) {}