1#![no_std]
32#![deny(warnings, missing_docs)]
33
34#[cfg(all(feature = "nobios", target_arch = "riscv64"))]
36pub mod msbi;
37#[cfg(all(feature = "nobios", target_arch = "riscv64"))]
39core::arch::global_asm!(include_str!("m_entry.asm"));
40
41const SBI_CONSOLE_PUTCHAR: usize = 1;
43const SBI_CONSOLE_GETCHAR: usize = 2;
44
45const SBI_EXT_TIMER: usize = 0x54494D45;
47const SBI_EXT_SRST: usize = 0x53525354;
48
49#[cfg(all(target_arch = "riscv64", not(feature = "nobios")))]
51#[inline(always)]
52fn sbi_call(eid: usize, fid: usize, arg0: usize, arg1: usize, arg2: usize) -> usize {
53 let ret;
54 unsafe {
57 core::arch::asm!(
58 "ecall",
59 inlateout("x10") arg0 => ret,
60 in("x11") arg1,
61 in("x12") arg2,
62 in("x16") fid,
63 in("x17") eid,
64 );
65 }
66 ret
67}
68
69#[cfg(all(target_arch = "riscv64", feature = "nobios"))]
71#[inline(always)]
72fn sbi_call(eid: usize, fid: usize, arg0: usize, arg1: usize, arg2: usize) -> usize {
73 let ret1: isize;
74 let ret2: usize;
75 unsafe {
79 core::arch::asm!(
80 "ecall",
81 inlateout("x10") arg0 => ret1,
82 inlateout("x11") arg1 => ret2,
83 in("x12") arg2,
84 in("x16") fid,
85 in("x17") eid
86 );
87 }
88 if ret1 < 0 {
89 panic!("SBI call failed: {}", ret1);
90 }
91 ret2
92}
93
94#[cfg(not(target_arch = "riscv64"))]
96#[inline(always)]
97fn sbi_call(_eid: usize, _fid: usize, _arg0: usize, _arg1: usize, _arg2: usize) -> usize {
98 unimplemented!("SBI calls are only supported on riscv64")
99}
100
101pub fn set_timer(timer: u64) {
103 sbi_call(SBI_EXT_TIMER, 0, timer as usize, 0, 0);
104}
105
106pub fn console_putchar(c: u8) {
108 sbi_call(SBI_CONSOLE_PUTCHAR, 0, c as usize, 0, 0);
109}
110
111pub fn console_getchar() -> usize {
113 sbi_call(SBI_CONSOLE_GETCHAR, 0, 0, 0, 0)
114}
115
116pub fn shutdown(failure: bool) -> ! {
118 if failure {
119 sbi_call(SBI_EXT_SRST, 0, 1, 0, 0);
120 } else {
121 sbi_call(SBI_EXT_SRST, 0, 0, 0, 0);
122 }
123 panic!("It should shutdown!");
124}