lib.rs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. //! A minimal RISC-V's SBI implementation library in Rust.
  2. //!
  3. //! *Note: If you are a user looking for binary distribution download for RustSBI, you may consider
  4. //! using the [RustSBI Prototyping System](https://github.com/rustsbi/standalone)
  5. //! which will provide binaries for each platforms.
  6. //! If you are a vendor or contributor who wants to adapt RustSBI to your new product or board,
  7. //! you may consider adapting the Prototyping System first to get your board adapted in an afternoon;
  8. //! you are only advised to build a discrete crate if your team have a lot of time working on this board.*
  9. //!
  10. //! *For more details on binary downloads the the RustSBI Prototyping System,
  11. //! see section [Prototyping System vs discrete packages](#download-binary-file-the-prototyping-system-vs-discrete-packages).*
  12. //!
  13. //! The crate `rustsbi` acts as core trait and instance abstraction of the RustSBI ecosystem.
  14. //!
  15. //! # What is RISC-V SBI?
  16. //!
  17. //! RISC-V SBI is short for RISC-V Supervisor Binary Interface. SBI acts as an interface to environment
  18. //! for your operating system kernel.
  19. //! An SBI implementation will allow furtherly bootstrap your kernel, and provide an environment while the kernel is running.
  20. //!
  21. //! More generally, The SBI allows supervisor-mode (S-mode or VS-mode) software to be portable across
  22. //! all RISC-V implementations by defining an abstraction for platform (or hypervisor) specific functionality.
  23. //!
  24. //! # Use RustSBI services in your supervisor software
  25. //!
  26. //! SBI environment features include boot sequence and a kernel environment. To bootstrap your kernel,
  27. //! place kernel into RustSBI implementation defined address, then RustSBI will prepare an
  28. //! environment and call the entry function on this address.
  29. //!
  30. //! ## Make SBI environment calls
  31. //!
  32. //! To use the kernel environment, you either use SBI calls or emulated instructions.
  33. //! SBI calls are similar to operating systems' `syscall`s. RISC-V SBI defined many SBI extensions,
  34. //! and in each extension there are different functions, you should pick a function before calling.
  35. //! Then, you should prepare some parameters, whose definition are not the same among functions.
  36. //!
  37. //! Now you have an extension number, a function number, and a few SBI call parameters.
  38. //! You invoke a special `ecall` instruction on supervisor level, and it will trap into machine level
  39. //! SBI implementation. It will handle your `ecall`, similar to your kernel handling system calls
  40. //! from user level.
  41. //!
  42. //! SBI functions return two values other than one. First value will be an error number,
  43. //! it will tell if SBI call have succeeded, or which error have occurred.
  44. //! Second value is the real return value, its meaning is different according to which function you calls.
  45. //!
  46. //! ## Call SBI in different programming languages
  47. //!
  48. //! Making SBI calls are similar to making system calls.
  49. //!
  50. //! Extension number is required to put on register `a7`, function number on `a6` if applicable.
  51. //! Parameters should be placed from `a0` to `a5`, first into `a0`, second into `a1`, etc.
  52. //! Unused parameters can be set to any value or leave untouched.
  53. //!
  54. //! After registers are ready, invoke an instruction called `ecall`.
  55. //! Then, the return value is placed into `a0` and `a1` registers.
  56. //! The error value could be read from `a0`, and return value is placed into `a1`.
  57. //!
  58. //! In Rust, here is an example to call SBI functions using inline assembly:
  59. //!
  60. //! ```no_run
  61. //! # #[repr(C)] struct SbiRet { error: usize, value: usize }
  62. //! # const EXTENSION_BASE: usize = 0x10;
  63. //! # const FUNCTION_BASE_GET_SPEC_VERSION: usize = 0x0;
  64. //! #[inline(always)]
  65. //! fn sbi_call(extension: usize, function: usize, arg0: usize, arg1: usize) -> SbiRet {
  66. //! let (error, value);
  67. //! match () {
  68. //! #[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))]
  69. //! () => unsafe { asm!(
  70. //! "ecall",
  71. //! in("a0") arg0, in("a1") arg1,
  72. //! in("a6") function, in("a7") extension,
  73. //! lateout("a0") error, lateout("a1") value,
  74. //! ) },
  75. //! #[cfg(not(any(target_arch = "riscv32", target_arch = "riscv64")))]
  76. //! () => {
  77. //! drop((extension, function, arg0, arg1));
  78. //! unimplemented!("not RISC-V instruction set architecture")
  79. //! }
  80. //! };
  81. //! SbiRet { error, value }
  82. //! }
  83. //!
  84. //! #[inline]
  85. //! pub fn get_spec_version() -> SbiRet {
  86. //! sbi_call(EXTENSION_BASE, FUNCTION_BASE_GET_SPEC_VERSION, 0, 0)
  87. //! }
  88. //! ```
  89. //!
  90. //! SBI functions would return a result thus some of these may fail.
  91. //! In this example we only take the value, but in complete designs we should handle the `error`
  92. //! returned by SbiRet.
  93. //!
  94. //! You may use other languages to call SBI environment. In C programming language, we can call like this:
  95. //!
  96. //! ```text
  97. //! #define SBI_CALL(ext, funct, arg0, arg1, arg2, arg3) ({ \
  98. //! register uintptr_t a0 asm ("a0") = (uintptr_t)(arg0); \
  99. //! register uintptr_t a1 asm ("a1") = (uintptr_t)(arg1); \
  100. //! register uintptr_t a2 asm ("a2") = (uintptr_t)(arg2); \
  101. //! register uintptr_t a3 asm ("a3") = (uintptr_t)(arg3); \
  102. //! register uintptr_t a6 asm ("a6") = (uintptr_t)(funct); \
  103. //! register uintptr_t a7 asm ("a7") = (uintptr_t)(ext); \
  104. //! asm volatile ("ecall" \
  105. //! : "+r" (a0), "+r" (a1) \
  106. //! : "r" (a1), "r" (a2), "r" (a3), "r" (a6), "r" (a7) \
  107. //! : "memory") \
  108. //! {a0, a1}; \
  109. //! })
  110. //!
  111. //! #define SBI_CALL_0(ext, funct) SBI_CALL(ext, funct, 0, 0, 0, 0)
  112. //!
  113. //! static inline sbiret get_spec_version() {
  114. //! SBI_CALL_0(EXTENSION_BASE, FUNCTION_BASE_GET_SPEC_VERSION)
  115. //! }
  116. //! ```
  117. //!
  118. //! # Implement RustSBI on machine environment
  119. //!
  120. //! Boards, SoC vendors, machine environment emulators and research projects may adapt RustSBI
  121. //! to specific environments.
  122. //! RustSBI project supports these demands either by discrete package or the Prototyping System.
  123. //! Developers may choose the Prototyping System to shorten development time,
  124. //! or discrete packages to include fine-grained features.
  125. //!
  126. //! Hypervisor and supervisor environment emulator developers may refer to
  127. //! [Hypervisor and emulator development with RustSBI](#hypervisor-and-emulator-development-with-rustsbi)
  128. //! for such purposes as RustSBI provide different set of features dedicated for emulated or virtual
  129. //! environments.
  130. //!
  131. //! ## Use the Prototyping System
  132. //!
  133. //! The RustSBI Prototyping System aims to get your platform working with SBI in an afternoon.
  134. //! It supports most RISC-V platforms available by providing scalable set of drivers and features.
  135. //! It provides custom features such as Penglai TEE, DramForever's emulated hypervisor extension, and Raven
  136. //! the firmware debugger framework.
  137. //!
  138. //! You may find further documents on [RustSBI Prototyping System repository](https://github.com/rustsbi/standalone).
  139. //!
  140. //! ## Discrete RustSBI package on bare metal RISC-V hardware
  141. //!
  142. //! Discrete packages provide developers with most scalability and complete control of underlying
  143. //! hardware. It is ideal if advanced low power features, management cores and other features should
  144. //! be used in this implementation.
  145. //!
  146. //! RustSBI supports discrete package by default. Create a new `#![no_std]` bare-metal package
  147. //! to get started. Add following lines to `Cargo.toml`:
  148. //!
  149. //! ```toml
  150. //! [dependencies]
  151. //! rustsbi = "0.4.0"
  152. //! ```
  153. //!
  154. //! After hardware initialization process, the part of firmware with RustSBI linked should run on memory
  155. //! blocks with fast accesses, as it would be called frequently by operating system.
  156. //! If the supervisor is called by trap generator semantics, insert `rustsbi::RustSBI` structure
  157. //! in your hart executor structure.
  158. //!
  159. //! ```rust
  160. //! # struct Clint;
  161. //! # struct MyPlatRfnc;
  162. //! # struct MyPlatHsm;
  163. //! # struct MyBoardPower;
  164. //! # struct MyPlatPmu;
  165. //! # struct MyPlatDbcn;
  166. //! use rustsbi::RustSBI;
  167. //!
  168. //! # struct SupervisorContext;
  169. //! /// Executes the supervisor within.
  170. //! struct Executor {
  171. //! ctx: SupervisorContext,
  172. //! /* other environment variables ... */
  173. //! sbi: RustSBI<Clint, Clint, MyPlatRfnc, MyPlatHsm, MyBoardPower, MyPlatPmu, MyPlatDbcn>,
  174. //! /* custom_1: CustomSBI<...> */
  175. //! }
  176. //!
  177. //! # struct Trap;
  178. //! impl Executor {
  179. //! /// A function that runs the provided supervisor, uses `&mut self` for it
  180. //! /// modifies `SupervisorContext`.
  181. //! ///
  182. //! /// It returns for every Trap the supervisor produces. Its handler should read
  183. //! /// and modify `self.ctx` if necessary. After handled, `run()` this structure
  184. //! /// again or exit execution process.
  185. //! pub fn run(&mut self) -> Trap {
  186. //! todo!("fill in generic or platform specific trampoline procedure")
  187. //! }
  188. //! }
  189. //! ```
  190. //!
  191. //! After each `run()`, process the trap returned with the RustSBI instance in executor.
  192. //! Call `RustSBI::handle_ecall` and fill in developer provided `SupervisorContext` if necessary.
  193. //!
  194. //! ```no_run
  195. //! # use sbi_spec::binary::SbiRet;
  196. //! # struct RustSBI {} // Mock, prevent doc test error when feature singleton is enabled
  197. //! # impl RustSBI { fn handle_ecall(&self, e: (), f: (), p: ()) -> SbiRet { SbiRet::success(0) } }
  198. //! # struct Executor { sbi: RustSBI }
  199. //! # #[derive(Copy, Clone)] enum Trap { Exception(Exception) }
  200. //! # impl Trap { fn cause(&self) -> Self { *self } }
  201. //! # #[derive(Copy, Clone)] enum Exception { SupervisorEcall }
  202. //! # impl Executor {
  203. //! # fn new(board_params: BoardParams) -> Executor { let _ = board_params; Executor { sbi: RustSBI {} } }
  204. //! # fn run(&mut self) -> Trap { Trap::Exception(Exception::SupervisorEcall) }
  205. //! # fn sbi_extension(&self) -> () { }
  206. //! # fn sbi_function(&self) -> () { }
  207. //! # fn sbi_params(&self) -> () { }
  208. //! # fn fill_sbi_return(&mut self, ans: SbiRet) { let _ = ans; }
  209. //! # }
  210. //! # struct BoardParams;
  211. //! # const MY_SPECIAL_EXIT: usize = 0x233;
  212. //! /// Board specific power operations.
  213. //! enum Operation {
  214. //! Reboot,
  215. //! Shutdown,
  216. //! }
  217. //!
  218. //! # impl From<SbiRet> for Operation { fn from(_: SbiRet) -> Self { todo!() } }
  219. //! /// Execute supervisor in given board parameters.
  220. //! pub fn execute_supervisor(board_params: BoardParams) -> Operation {
  221. //! let mut exec = Executor::new(board_params);
  222. //! loop {
  223. //! let trap = exec.run();
  224. //! if let Trap::Exception(Exception::SupervisorEcall) = trap.cause() {
  225. //! let ans = exec.sbi.handle_ecall(
  226. //! exec.sbi_extension(),
  227. //! exec.sbi_function(),
  228. //! exec.sbi_params(),
  229. //! );
  230. //! if ans.error == MY_SPECIAL_EXIT {
  231. //! break Operation::from(ans)
  232. //! }
  233. //! // This line would also advance `sepc` with `4` to indicate the `ecall` is handled.
  234. //! exec.fill_sbi_return(ans);
  235. //! } else {
  236. //! // other trap types ...
  237. //! }
  238. //! }
  239. //! }
  240. //! ```
  241. //!
  242. //! Now, call supervisor execution function in your bare metal package to finish the discrete
  243. //! package project.
  244. //!
  245. //! ```no_run
  246. //! # #[cfg(nightly)] // disable checks
  247. //! #[naked]
  248. //! #[link_section = ".text.entry"]
  249. //! #[export_name = "_start"]
  250. //! unsafe extern "C" fn entry() -> ! {
  251. //! #[link_section = ".bss.uninit"]
  252. //! static mut SBI_STACK: [u8; LEN_STACK_SBI] = [0; LEN_STACK_SBI];
  253. //!
  254. //! // Note: actual assembly code varies between platforms.
  255. //! // Double check documents before continue on.
  256. //! core::arch::asm!(
  257. //! // 1. Turn off interrupt
  258. //! "csrw mie, zero",
  259. //! // 2. Initialize programming langauge runtime
  260. //! // only clear bss if hartid is zero
  261. //! "csrr t0, mhartid",
  262. //! "bnez t0, 2f",
  263. //! // clear bss segment
  264. //! "la t0, sbss",
  265. //! "la t1, ebss",
  266. //! "1:",
  267. //! "bgeu t0, t1, 2f",
  268. //! "sd zero, 0(t0)",
  269. //! "addi t0, t0, 8",
  270. //! "j 1b",
  271. //! "2:",
  272. //! // 3. Prepare stack for each hart
  273. //! "la sp, {stack}",
  274. //! "li t0, {per_hart_stack_size}",
  275. //! "csrr t1, mhartid",
  276. //! "addi t1, t1, 1",
  277. //! "1: ",
  278. //! "add sp, sp, t0",
  279. //! "addi t1, t1, -1",
  280. //! "bnez t1, 1b",
  281. //! "j {rust_main}",
  282. //! // 4. Clean up
  283. //! "j {finalize}",
  284. //! per_hart_stack_size = const LEN_STACK_PER_HART,
  285. //! stack = sym SBI_STACK,
  286. //! rust_main = sym rust_main,
  287. //! finalize = sym finalize,
  288. //! options(noreturn)
  289. //! )
  290. //! }
  291. //!
  292. //! # fn board_init_once() {}
  293. //! # fn print_information_once() {}
  294. //! # fn execute_supervisor(_bp: &()) -> Operation { Operation::Shutdown }
  295. //! /// Power operation after main function
  296. //! enum Operation {
  297. //! Reboot,
  298. //! Shutdown,
  299. //! // Add board specific low power modes if necessary. This will allow the
  300. //! // function `finalize` to operate on board specific power management chips.
  301. //! }
  302. //!
  303. //! /// Rust entry, call in `entry` assembly function
  304. //! extern "C" fn rust_main(_hartid: usize, opaque: usize) -> Operation {
  305. //! // .. board initialization process ...
  306. //! let board_params = board_init_once();
  307. //! // .. print necessary information and rustsbi::LOGO ..
  308. //! print_information_once();
  309. //! // execute supervisor, return as Operation
  310. //! execute_supervisor(&board_params)
  311. //! }
  312. //!
  313. //! # fn wfi() {}
  314. //! /// Perform board specific power operations
  315. //! ///
  316. //! /// The function here provides a stub to example power operations.
  317. //! /// Actual board developers should provide with more practical communications
  318. //! /// to external chips on power operation.
  319. //! unsafe extern "C" fn finalize(op: Operation) -> ! {
  320. //! match op {
  321. //! Operation::Shutdown => {
  322. //! // easiest way to make a hart look like powered off
  323. //! loop { wfi(); }
  324. //! }
  325. //! Operation::Reboot => {
  326. //! # fn entry() -> ! { loop {} } // mock
  327. //! // easiest software reset is to jump to entry directly
  328. //! entry()
  329. //! }
  330. //! // .. more power operations goes here ..
  331. //! }
  332. //! }
  333. //! ```
  334. //!
  335. //! Now RustSBI would run on machine environment, you may start a kernel or use an SBI test suite
  336. //! to check if it is properly implemented.
  337. //!
  338. //! Some platforms would provide system memory under different grades in speed and size to reduce product cost.
  339. //! Those platforms would typically provide two parts of code memory, first one being relatively small, not fast
  340. //! but instantly available after chip start, while the second one is larger in size but typically requires
  341. //! memory training. The former one would include built-in SRAM memory, and the later would include
  342. //! external SRAM or DDR memory. On those platforms, a first stage bootloader is typically needed to
  343. //! train memory for later stages. In such situation, RustSBI implementation should be linked or concated
  344. //! to the second stage bootloader, and the first stage could be a standalone binary package bundled with it.
  345. //!
  346. //! # Hypervisor and emulator development with RustSBI
  347. //!
  348. //! RustSBI crate supports to develop RISC-V emulators, and both Type-1 and Type-2 hypervisors.
  349. //! Hypervisor developers may find it easy to handle standard SBI functions with an instance
  350. //! based RustSBI interface.
  351. //!
  352. //! ## Hypervisors using RustSBI
  353. //!
  354. //! Both Type-1 and Type-2 hypervisors on RISC-V run on HS-mode hardware. Depending on demands
  355. //! of virtualized systems, hypervisors may either provide transparent information from host machine
  356. //! or provide another set of information to override the current environment. Notably,
  357. //! RISC-V hypervisors do not have direct access to machine mode (M-mode) registers.
  358. //!
  359. //! RustSBI supports both by providing a `MachineInfo` structure in instance based interface.
  360. //! If RISC-V hypervisors choose to use existing information on current machine, it may require
  361. //! to call underlying M-mode environment using SBI calls and fill in information into `MachineInfo`.
  362. //! If hypervisors use customized information other than taking the same one from the
  363. //! environment they reside in, they may fill in custom one into `MachineInfo` structures.
  364. //! When creating RustSBI instance, `MachineInfo` structure is required as an input of constructor.
  365. //!
  366. //! To begin with, disable default features in file `Cargo.toml`:
  367. //!
  368. //! ```toml
  369. //! [dependencies]
  370. //! rustsbi = { version = "0.4.0", default-features = false }
  371. //! ```
  372. //!
  373. //! This will disable default feature `machine` which will assume that RustSBI runs on M-mode directly,
  374. //! which is not appropriate in our purpose. After that, a `RustSBI` instance may be placed
  375. //! in the virtual machine structure to prepare for SBI environment:
  376. //!
  377. //! ```rust
  378. //! # struct RustSBI<>();
  379. //! struct VmHart {
  380. //! // other fields ...
  381. //! env: RustSBI</* Types, .. */>,
  382. //! }
  383. //! ```
  384. //!
  385. //! When the virtual machine hart traps into hypervisor, its code should decide whether
  386. //! this trap is an SBI environment call. If that is true, pass in parameters by `env.handle_ecall`
  387. //! function. RustSBI will handle with SBI standard constants, call corresponding extension module
  388. //! and provide parameters according to the extension and function IDs.
  389. //!
  390. //! Crate `rustsbi` adapts to standard RISC-V SBI calls.
  391. //! If the hypervisor have custom SBI extensions that RustSBI does not recognize, those extension
  392. //! and function IDs can be checked before calling RustSBI `env.handle_ecall`.
  393. //!
  394. //! ```no_run
  395. //! # use sbi_spec::binary::SbiRet;
  396. //! # struct MyExtensionEnv {}
  397. //! # impl MyExtensionEnv { fn handle_ecall(&self, params: ()) -> SbiRet { SbiRet::success(0) } }
  398. //! # struct RustSBI {} // Mock, prevent doc test error when feature singleton is enabled
  399. //! # impl RustSBI { fn handle_ecall(&self, params: ()) -> SbiRet { SbiRet::success(0) } }
  400. //! # struct VmHart { my_extension_env: MyExtensionEnv, env: RustSBI }
  401. //! # #[derive(Copy, Clone)] enum Trap { Exception(Exception) }
  402. //! # impl Trap { fn cause(&self) -> Self { *self } }
  403. //! # #[derive(Copy, Clone)] enum Exception { SupervisorEcall }
  404. //! # impl VmHart {
  405. //! # fn new() -> VmHart { VmHart { my_extension_env: MyExtensionEnv {}, env: RustSBI {} } }
  406. //! # fn run(&mut self) -> Trap { Trap::Exception(Exception::SupervisorEcall) }
  407. //! # fn trap_params(&self) -> () { }
  408. //! # fn fill_in(&mut self, ans: SbiRet) { let _ = ans; }
  409. //! # }
  410. //! let mut hart = VmHart::new();
  411. //! loop {
  412. //! let trap = hart.run();
  413. //! if let Trap::Exception(Exception::SupervisorEcall) = trap.cause() {
  414. //! // Firstly, handle custom extensions
  415. //! let my_extension_sbiret = hart.my_extension_env.handle_ecall(hart.trap_params());
  416. //! // If custom extension handles correctly, fill in its result and continue to hart.
  417. //! // The custom handler may handle `probe_extension` in `base` extension as well
  418. //! // to allow detections to whether custom extension exists.
  419. //! if my_extension_sbiret != SbiRet::not_supported() {
  420. //! hart.fill_in(my_extension_sbiret);
  421. //! continue;
  422. //! }
  423. //! // Then, if it's not a custom extension, handle it using standard SBI handler.
  424. //! let standard_sbiret = hart.env.handle_ecall(hart.trap_params());
  425. //! hart.fill_in(standard_sbiret);
  426. //! }
  427. //! }
  428. //! ```
  429. //!
  430. //! RustSBI would interact well with custom extension environments in this way.
  431. //!
  432. //! ## Emulators using RustSBI
  433. //!
  434. //! RustSBI library may be used to write RISC-V emulators. Other than hardware accelereted binary
  435. //! translation methods, emulators typically do not use host hardware specific features,
  436. //! thus may build and run on any architecture.
  437. //! Like hardware RISC-V implementations, software emulated RISC-V environment would still need SBI
  438. //! implementation to support supervisor environment.
  439. //!
  440. //! Writing emulators would follow the similiar process with writing hypervisors, see
  441. //! [Hypervisors using RustSBI](#hypervisors-using-rustsbi) for details.
  442. //!
  443. //! # Download binary file: the Prototyping System vs discrete packages
  444. //!
  445. //! RustSBI ecosystem would typically provide support for most platforms. Those support packages
  446. //! would be provided either from the RustSBI Prototyping System or vendor provided discrete SBI
  447. //! implementation packages.
  448. //!
  449. //! The RustSBI Prototyping System is a universal support package provided by RustSBI ecosystem.
  450. //! It is designed to save development time while providing most SBI feature possible.
  451. //! It also includes a universal test kernel to allow testing SBI implementations on current environment.
  452. //! Users may choose to download from [Prototyping System repository](https://github.com/rustsbi/standalone)
  453. //! to get various types of RustSBI packages for their boards.
  454. //! Vendors and contributors may find it easy to adapt new SoCs and boards into Prototyping System.
  455. //!
  456. //! Discrete SBI packages are SBI environment support packages specially designed for one board
  457. //! or SoC, it will be provided by board vendor or RustSBI ecosystem.
  458. //! Vendors may find it easy to include fine grained features in each support package, but the
  459. //! maintainence situation would vary between vendors and it would likely to cost a lot of time
  460. //! to develop from a bare-metal executable. Users may find a boost in performance, energy saving
  461. //! indexes and feature granularity in discrete packages, but it would depends on whether the
  462. //! vendor provide it.
  463. //!
  464. //! To download binary package for the Prototyping System, visit its project website for a download link.
  465. //! To download them for discrete packages, RustSBI users may visit distribution source of SoC or board
  466. //! manufacturers. Additionally, users may visit [the awesome page](https://github.com/rustsbi/awesome-rustsbi)
  467. //! for a curated list ofboth Prototyping System and discrete packages provided by RustSBI ecosystem.
  468. //!
  469. //! # Notes for RustSBI developers
  470. //!
  471. //! Following useful hints are for firmware and kernel developers when working with SBI and RustSBI.
  472. //!
  473. //! ## RustSBI is a library for interfaces
  474. //!
  475. //! This library adapts to individual Rust traits to provide basic SBI features.
  476. //! When building for own platform, implement traits in this library and pass them to the functions
  477. //! begin with `init`. After that, you may call `rustsbi::ecall`, `RustSBI::handle_ecall` or
  478. //! similiar functions in your own exception handler.
  479. //! It would dispatch parameters from supervisor to the traits to execute SBI functions.
  480. //!
  481. //! The library also implements useful functions which may help with platform specific binaries.
  482. //! The `LOGO` can be printed if necessary when the binary is initializing.
  483. //!
  484. //! Note that this crate is a library which contains common building blocks in SBI implementation.
  485. //! The RustSBI ecosystem would provide different level of support for each board, those support
  486. //! packages would use `rustsbi` crate as library to provide different type of SBI binary releases.
  487. //!
  488. //! ## Hardware discovery and feature detection
  489. //!
  490. //! According to the RISC-V SBI specification, SBI itself does not specify any method for hardware discovery.
  491. //! The supervisor software must rely on the other industry standard hardware
  492. //! discovery methods (i.e. Device Tree or ACPI) for that purpose.
  493. //!
  494. //! To detect any feature under bare metal or under supervisor level, developers may depend on
  495. //! any hardware discovery methods, or use try-execute-trap method to detect any instructions or
  496. //! CSRs. If SBI is implemented in user level emulators, it may requires to depend on operating
  497. //! system calls or use the signal trap method to detect any RISC-V core features.
  498. #![no_std]
  499. mod console;
  500. mod hart_mask;
  501. mod hsm;
  502. mod instance;
  503. mod ipi;
  504. mod memory_range;
  505. mod pmu;
  506. mod reset;
  507. mod rfence;
  508. mod timer;
  509. /// The RustSBI logo without blank lines on the beginning
  510. pub const LOGO: &str = r".______ __ __ _______.___________. _______..______ __
  511. | _ \ | | | | / | | / || _ \ | |
  512. | |_) | | | | | | (----`---| |----`| (----`| |_) || |
  513. | / | | | | \ \ | | \ \ | _ < | |
  514. | |\ \----.| `--' |.----) | | | .----) | | |_) || |
  515. | _| `._____| \______/ |_______/ |__| |_______/ |______/ |__|";
  516. // RustSBI supports RISC-V SBI specification 2.0-rc1
  517. const SBI_SPEC_MAJOR: usize = 2;
  518. const SBI_SPEC_MINOR: usize = 0;
  519. /// RustSBI implementation ID: 4
  520. ///
  521. /// Ref: https://github.com/riscv-non-isa/riscv-sbi-doc/pull/61
  522. const IMPL_ID_RUSTSBI: usize = 4;
  523. const RUSTSBI_VERSION_MAJOR: usize = (env!("CARGO_PKG_VERSION_MAJOR").as_bytes()[0] - b'0') as _;
  524. const RUSTSBI_VERSION_MINOR: usize = (env!("CARGO_PKG_VERSION_MINOR").as_bytes()[0] - b'0') as _;
  525. const RUSTSBI_VERSION_PATCH: usize = (env!("CARGO_PKG_VERSION_PATCH").as_bytes()[0] - b'0') as _;
  526. const RUSTSBI_VERSION: usize =
  527. (RUSTSBI_VERSION_MAJOR << 16) + (RUSTSBI_VERSION_MINOR << 8) + RUSTSBI_VERSION_PATCH;
  528. /// RustSBI version as a string
  529. pub const VERSION: &str = env!("CARGO_PKG_VERSION");
  530. pub extern crate sbi_spec as spec;
  531. pub use console::Console;
  532. pub use hart_mask::HartMask;
  533. pub use hsm::Hsm;
  534. pub use instance::{Builder, RustSBI};
  535. pub use ipi::Ipi;
  536. pub use memory_range::Physical;
  537. pub use pmu::Pmu;
  538. pub use reset::Reset;
  539. pub use rfence::Rfence as Fence;
  540. pub use timer::Timer;
  541. #[cfg(not(feature = "machine"))]
  542. pub use instance::MachineInfo;