Browse Source

build(deps): upgrade rustsbi 0.4.0 denpendencies and format code (#21)

Plucky923 5 months ago
parent
commit
4a66b36632

+ 2 - 2
Cargo.lock

@@ -133,9 +133,9 @@ checksum = "cf8b4cfb0da0528321d22daee4299a23a8c5ac8848623d716e898d2a9eec0694"
 
 [[package]]
 name = "rustsbi"
-version = "0.4.0-alpha.3"
+version = "0.4.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "ce47c5c62f6a4d9586b78cc2ba963330bc57a046c4b2949ebc128265707a40c3"
+checksum = "44c13763120794ed11d64bac885fb31d384ae385c3287b0697711b97affbf8ab"
 dependencies = [
  "riscv",
  "rustsbi-macros",

+ 1 - 1
prototyper/Cargo.toml

@@ -13,7 +13,7 @@ aclint = "0.0.0"
 log = "0.4.21"
 panic-halt = "0.2.0"
 riscv = "0.11.1"
-rustsbi = { version = "0.4.0-alpha.3", features = ["machine"] }
+rustsbi = { version = "0.4.0", features = ["machine"] }
 sbi-spec = { version = "0.0.7", features = ["legacy"] }
 serde = { version = "1.0.202", default-features = false, features = ["derive"]}
 serde-device-tree = { git = "https://github.com/rustsbi/serde-device-tree", default-features = false }

+ 3 - 3
prototyper/src/board.rs

@@ -14,8 +14,9 @@ use crate::sbi::ipi::IpiDevice;
 use crate::sbi::reset::ResetDevice;
 use crate::sbi::SBI;
 
-pub(crate) static mut SBI_IMPL: MaybeUninit<SBI<'static, MachineConsole, SifiveClint, SifiveTestDevice>> =
-    MaybeUninit::uninit();
+pub(crate) static mut SBI_IMPL: MaybeUninit<
+    SBI<'static, MachineConsole, SifiveClint, SifiveTestDevice>,
+> = MaybeUninit::uninit();
 
 /// Console Device: Uart16550
 #[doc(hidden)]
@@ -90,7 +91,6 @@ pub(crate) fn ipi_dev_init(base: usize) {
     SIFIVECLINT.store(base as _, Release);
 }
 
-
 /// Reset Device: SifiveTestDevice
 impl ResetDevice for SifiveTestDevice {
     #[inline]

+ 2 - 3
prototyper/src/dynamic.rs

@@ -44,13 +44,12 @@ pub fn read_paddr(paddr: usize) -> Result<DynamicInfo, DynamicReadError> {
         bad_version: None,
     };
     // check pointer before dereference
-    if DYNAMIC_INFO_INVALID_ADDRESSES == paddr
-    {
+    if DYNAMIC_INFO_INVALID_ADDRESSES == paddr {
         error.bad_paddr = Some(paddr);
         return Err(error);
     }
     let ans = unsafe { *(paddr as *const DynamicInfo) };
-    
+
     if ans.magic != MAGIC {
         error.bad_magic = Some(ans.magic);
     }

+ 9 - 4
prototyper/src/riscv_spec.rs

@@ -22,9 +22,13 @@ pub mod menvcfg {
 
     pub fn set_bits(option: usize) {
         let mut bits: usize;
-        unsafe { asm!("csrr {}, menvcfg", out(reg) bits, options(nomem)); }
+        unsafe {
+            asm!("csrr {}, menvcfg", out(reg) bits, options(nomem));
+        }
         bits |= option;
-        unsafe { asm!("csrw menvcfg, {}", in(reg) bits, options(nomem)); }
+        unsafe {
+            asm!("csrw menvcfg, {}", in(reg) bits, options(nomem));
+        }
     }
 }
 
@@ -32,11 +36,12 @@ pub mod stimecmp {
     use core::arch::asm;
 
     pub fn set(value: u64) {
-        unsafe { asm!("csrrw zero, stimecmp, {}", in(reg) value, options(nomem)); }
+        unsafe {
+            asm!("csrrw zero, stimecmp, {}", in(reg) value, options(nomem));
+        }
     }
 }
 
-
 #[inline]
 pub fn current_hartid() -> usize {
     riscv::register::mhartid::read()

+ 14 - 6
prototyper/src/sbi/console.rs

@@ -1,7 +1,7 @@
+use crate::board::SBI_IMPL;
 use core::fmt::{self, Write};
 use rustsbi::{Console, Physical, SbiRet};
 use spin::Mutex;
-use crate::board::SBI_IMPL;
 
 pub trait ConsoleDevice {
     fn read(&self, buf: &mut [u8]) -> usize;
@@ -28,7 +28,9 @@ impl<'a, T: ConsoleDevice> SbiConsole<'a, T> {
         let mut c = 0u8;
         let console = self.inner.lock();
         loop {
-            if console.read(core::slice::from_mut(&mut c)) == 1 {break;}
+            if console.read(core::slice::from_mut(&mut c)) == 1 {
+                break;
+            }
         }
         c as _
     }
@@ -60,7 +62,6 @@ impl<'a, T: ConsoleDevice> Console for SbiConsole<'a, T> {
     }
 }
 
-
 impl<'a, T: ConsoleDevice> fmt::Write for SbiConsole<'a, T> {
     #[inline]
     fn write_str(&mut self, s: &str) -> fmt::Result {
@@ -74,13 +75,20 @@ impl<'a, T: ConsoleDevice> fmt::Write for SbiConsole<'a, T> {
     }
 }
 
-
 #[inline]
 pub fn putchar(c: usize) -> usize {
-    unsafe { SBI_IMPL.assume_init_mut() }.console.as_mut().unwrap().putchar(c)
+    unsafe { SBI_IMPL.assume_init_mut() }
+        .console
+        .as_mut()
+        .unwrap()
+        .putchar(c)
 }
 
 #[inline]
 pub fn getchar() -> usize {
-    unsafe { SBI_IMPL.assume_init_mut() }.console.as_mut().unwrap().getchar()
+    unsafe { SBI_IMPL.assume_init_mut() }
+        .console
+        .as_mut()
+        .unwrap()
+        .getchar()
 }

+ 29 - 14
prototyper/src/sbi/hsm.rs

@@ -3,8 +3,8 @@ use core::{
     hint::spin_loop,
     sync::atomic::{AtomicUsize, Ordering},
 };
-use rustsbi::{spec::hsm::hart_state, SbiRet};
 use riscv::register::mstatus::MPP;
+use rustsbi::{spec::hsm::hart_state, SbiRet};
 
 use crate::board::SBI_IMPL;
 use crate::riscv_spec::current_hartid;
@@ -13,7 +13,6 @@ use crate::sbi::trap_stack::ROOT_STACK;
 
 const HART_STATE_START_PENDING_EXT: usize = usize::MAX;
 
-
 type HsmState = AtomicUsize;
 
 pub(crate) struct HsmCell<T> {
@@ -47,7 +46,6 @@ impl<T> HsmCell<T> {
     }
 }
 
-
 /// 当前硬件线程的共享对象。
 pub struct LocalHsmCell<'a, T>(&'a HsmCell<T>);
 
@@ -99,7 +97,7 @@ impl<T> LocalHsmCell<'_, T> {
     }
 }
 
-impl<T : core::fmt::Debug> RemoteHsmCell<'_, T> {
+impl<T: core::fmt::Debug> RemoteHsmCell<'_, T> {
     /// 向关闭状态的硬件线程传入共享数据,并将其状态设置为启动挂起,返回是否放入成功。
     #[inline]
     pub fn start(&self, t: T) -> bool {
@@ -145,7 +143,6 @@ impl<T : core::fmt::Debug> RemoteHsmCell<'_, T> {
     }
 }
 
-
 /// 获取此 hart 的 local hsm 对象。
 pub(crate) fn local_hsm() -> LocalHsmCell<'static, NextStage> {
     unsafe {
@@ -178,17 +175,23 @@ pub(crate) fn remote_hsm(hart_id: usize) -> Option<RemoteHsmCell<'static, NextSt
     }
 }
 
-
-
-/// HSM 
+/// HSM
 pub(crate) struct SbiHsm;
 
 impl rustsbi::Hsm for SbiHsm {
     fn hart_start(&self, hartid: usize, start_addr: usize, opaque: usize) -> SbiRet {
         match remote_hsm(hartid) {
             Some(remote) => {
-                if remote.start(NextStage { start_addr, opaque, next_mode: MPP::Supervisor }) {
-                    unsafe { SBI_IMPL.assume_init_ref() }.ipi.as_ref().unwrap().set_msip(hartid);
+                if remote.start(NextStage {
+                    start_addr,
+                    opaque,
+                    next_mode: MPP::Supervisor,
+                }) {
+                    unsafe { SBI_IMPL.assume_init_ref() }
+                        .ipi
+                        .as_ref()
+                        .unwrap()
+                        .set_msip(hartid);
                     SbiRet::success(0)
                 } else {
                     SbiRet::already_started()
@@ -201,8 +204,14 @@ impl rustsbi::Hsm for SbiHsm {
     #[inline]
     fn hart_stop(&self) -> SbiRet {
         local_hsm().stop();
-        unsafe { SBI_IMPL.assume_init_ref() }.ipi.as_ref().unwrap().clear_msip(current_hartid());
-        unsafe { riscv::register::mie::clear_msoft(); }
+        unsafe { SBI_IMPL.assume_init_ref() }
+            .ipi
+            .as_ref()
+            .unwrap()
+            .clear_msip(current_hartid());
+        unsafe {
+            riscv::register::mie::clear_msoft();
+        }
         riscv::asm::wfi();
         SbiRet::success(0)
     }
@@ -219,8 +228,14 @@ impl rustsbi::Hsm for SbiHsm {
         use rustsbi::spec::hsm::suspend_type::{NON_RETENTIVE, RETENTIVE};
         if matches!(suspend_type, NON_RETENTIVE | RETENTIVE) {
             local_hsm().suspend();
-            unsafe { SBI_IMPL.assume_init_ref() }.ipi.as_ref().unwrap().clear_msip(current_hartid());
-            unsafe { riscv::register::mie::set_msoft(); }
+            unsafe { SBI_IMPL.assume_init_ref() }
+                .ipi
+                .as_ref()
+                .unwrap()
+                .clear_msip(current_hartid());
+            unsafe {
+                riscv::register::mie::set_msoft();
+            }
             riscv::asm::wfi();
             local_hsm().resume();
             SbiRet::success(0)

+ 7 - 8
prototyper/src/sbi/mod.rs

@@ -1,24 +1,23 @@
 use rustsbi::RustSBI;
 
 pub mod console;
-pub mod rfence;
-pub mod ipi;
 pub mod hsm;
+pub mod ipi;
 pub mod reset;
+pub mod rfence;
 
 pub mod fifo;
-pub mod logger;
 pub mod hart_context;
+pub mod logger;
 pub mod trap;
 pub mod trap_stack;
 
-use console::{SbiConsole, ConsoleDevice};
-use ipi::{SbiIpi, IpiDevice};
-use reset::{SbiReset, ResetDevice};
+use console::{ConsoleDevice, SbiConsole};
 use hsm::SbiHsm;
+use ipi::{IpiDevice, SbiIpi};
+use reset::{ResetDevice, SbiReset};
 use rfence::SbiRFence;
 
-
 #[derive(RustSBI, Default)]
 #[rustsbi(dynamic)]
 pub struct SBI<'a, C: ConsoleDevice, I: IpiDevice, R: ResetDevice> {
@@ -31,5 +30,5 @@ pub struct SBI<'a, C: ConsoleDevice, I: IpiDevice, R: ResetDevice> {
     #[rustsbi(reset)]
     pub reset: Option<SbiReset<'a, R>>,
     #[rustsbi(fence)]
-    pub rfence: Option<SbiRFence>
+    pub rfence: Option<SbiRFence>,
 }

+ 5 - 1
prototyper/src/sbi/reset.rs

@@ -61,5 +61,9 @@ impl<'a, T: ResetDevice> rustsbi::Reset for SbiReset<'a, T> {
 }
 
 pub fn fail() -> ! {
-    unsafe { SBI_IMPL.assume_init_ref() }.reset.as_ref().unwrap().fail()
+    unsafe { SBI_IMPL.assume_init_ref() }
+        .reset
+        .as_ref()
+        .unwrap()
+        .fail()
 }

+ 2 - 2
prototyper/src/sbi/rfence.rs

@@ -2,10 +2,10 @@ use rustsbi::{HartMask, SbiRet};
 use spin::Mutex;
 
 use crate::board::SBI_IMPL;
-use crate::sbi::fifo::{Fifo, FifoError};
 use crate::riscv_spec::current_hartid;
-use crate::sbi::trap_stack::ROOT_STACK;
+use crate::sbi::fifo::{Fifo, FifoError};
 use crate::sbi::trap;
+use crate::sbi::trap_stack::ROOT_STACK;
 
 use core::sync::atomic::{AtomicU32, Ordering};
 

+ 6 - 2
prototyper/src/sbi/trap_stack.rs

@@ -1,9 +1,9 @@
 use core::mem::forget;
 use fast_trap::FreeTrapStack;
 
+use crate::riscv_spec::current_hartid;
 use crate::sbi::hart_context::HartContext;
 use crate::sbi::trap::fast_handler;
-use crate::riscv_spec::current_hartid;
 
 const LEN_STACK_PER_HART: usize = 16 * 1024;
 pub const NUM_HART_MAX: usize = 8;
@@ -34,7 +34,11 @@ pub(crate) unsafe extern "C" fn locate() {
 
 /// 预备陷入栈。
 pub(crate) fn prepare_for_trap() {
-    unsafe { ROOT_STACK.get_unchecked_mut(current_hartid()).load_as_stack() };
+    unsafe {
+        ROOT_STACK
+            .get_unchecked_mut(current_hartid())
+            .load_as_stack()
+    };
 }
 
 /// 类型化栈。