Browse Source

lib: implement rustsbi traits for their references

luojia65 2 years ago
parent
commit
4f9fc9f8a3
7 changed files with 154 additions and 0 deletions
  1. 2 0
      CHANGELOG.md
  2. 19 0
      src/hsm.rs
  3. 7 0
      src/ipi.rs
  4. 58 0
      src/pmu.rs
  5. 12 0
      src/reset.rs
  6. 49 0
      src/rfence.rs
  7. 7 0
      src/timer.rs

+ 2 - 0
CHANGELOG.md

@@ -11,6 +11,8 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
 
 - Instance based and/or machine independent RustSBI to support hypervisor development
 - Structure `MachineInfo` for non-machine environment, e.g. cross-architecture emulator
+- Builder structure for instance based RustSBI framework
+- Implement RustSBI traits for their references
 - Feature `legacy` to gate SBI legacy extension
 - Expose `init_*` functions on instance based RustSBI implementation
 - LEGACY_CLEAR_IPI implemented

+ 19 - 0
src/hsm.rs

@@ -190,6 +190,25 @@ pub trait Hsm: Send + Sync {
     }
 }
 
+impl<T: Hsm> Hsm for &T {
+    #[inline]
+    fn hart_start(&self, hartid: usize, start_addr: usize, opaque: usize) -> SbiRet {
+        T::hart_start(self, hartid, start_addr, opaque)
+    }
+    #[inline]
+    fn hart_stop(&self) -> SbiRet {
+        T::hart_stop(self)
+    }
+    #[inline]
+    fn hart_get_status(&self, hartid: usize) -> SbiRet {
+        T::hart_get_status(self, hartid)
+    }
+    #[inline]
+    fn hart_suspend(&self, suspend_type: u32, resume_addr: usize, opaque: usize) -> SbiRet {
+        T::hart_suspend(self, suspend_type, resume_addr, opaque)
+    }
+}
+
 #[cfg(feature = "singleton")]
 use crate::util::AmoOnceRef;
 

+ 7 - 0
src/ipi.rs

@@ -16,6 +16,13 @@ pub trait Ipi: Send + Sync {
     fn send_ipi(&self, hart_mask: HartMask) -> SbiRet;
 }
 
+impl<T: Ipi> Ipi for &T {
+    #[inline]
+    fn send_ipi(&self, hart_mask: HartMask) -> SbiRet {
+        T::send_ipi(self, hart_mask)
+    }
+}
+
 #[cfg(feature = "singleton")]
 static IPI: AmoOnceRef<dyn Ipi> = AmoOnceRef::new();
 

+ 58 - 0
src/pmu.rs

@@ -196,6 +196,64 @@ pub trait Pmu: Send + Sync {
     fn counter_fw_read(&self, counter_idx: usize) -> SbiRet;
 }
 
+impl<T: Pmu> Pmu for &T {
+    #[inline]
+    fn num_counters(&self) -> usize {
+        T::num_counters(self)
+    }
+    #[inline]
+    fn counter_get_info(&self, counter_idx: usize) -> SbiRet {
+        T::counter_get_info(self, counter_idx)
+    }
+    #[inline]
+    fn counter_config_matching(
+        &self,
+        counter_idx_base: usize,
+        counter_idx_mask: usize,
+        config_flags: usize,
+        event_idx: usize,
+        event_data: u64,
+    ) -> SbiRet {
+        T::counter_config_matching(
+            self,
+            counter_idx_base,
+            counter_idx_mask,
+            config_flags,
+            event_idx,
+            event_data,
+        )
+    }
+    #[inline]
+    fn counter_start(
+        &self,
+        counter_idx_base: usize,
+        counter_idx_mask: usize,
+        start_flags: usize,
+        initial_value: u64,
+    ) -> SbiRet {
+        T::counter_start(
+            self,
+            counter_idx_base,
+            counter_idx_mask,
+            start_flags,
+            initial_value,
+        )
+    }
+    #[inline]
+    fn counter_stop(
+        &self,
+        counter_idx_base: usize,
+        counter_idx_mask: usize,
+        stop_flags: usize,
+    ) -> SbiRet {
+        T::counter_stop(self, counter_idx_base, counter_idx_mask, stop_flags)
+    }
+    #[inline]
+    fn counter_fw_read(&self, counter_idx: usize) -> SbiRet {
+        T::counter_fw_read(self, counter_idx)
+    }
+}
+
 #[cfg(feature = "singleton")]
 use crate::util::AmoOnceRef;
 

+ 12 - 0
src/reset.rs

@@ -50,6 +50,18 @@ pub trait Reset: Send + Sync {
     }
 }
 
+impl<T: Reset> Reset for &T {
+    #[inline]
+    fn system_reset(&self, reset_type: u32, reset_reason: u32) -> SbiRet {
+        T::system_reset(self, reset_type, reset_reason)
+    }
+    #[cfg(feature = "legacy")]
+    #[inline]
+    fn legacy_reset(&self) -> ! {
+        T::legacy_reset(self)
+    }
+}
+
 #[cfg(feature = "singleton")]
 use crate::util::AmoOnceRef;
 

+ 49 - 0
src/rfence.rs

@@ -135,6 +135,55 @@ pub trait Rfence: Send + Sync {
     }
 }
 
+impl<T: Rfence> Rfence for &T {
+    #[inline]
+    fn remote_fence_i(&self, hart_mask: HartMask) -> SbiRet {
+        T::remote_fence_i(self, hart_mask)
+    }
+    #[inline]
+    fn remote_sfence_vma(&self, hart_mask: HartMask, start_addr: usize, size: usize) -> SbiRet {
+        T::remote_sfence_vma(self, hart_mask, start_addr, size)
+    }
+    #[inline]
+    fn remote_sfence_vma_asid(
+        &self,
+        hart_mask: HartMask,
+        start_addr: usize,
+        size: usize,
+        asid: usize,
+    ) -> SbiRet {
+        T::remote_sfence_vma_asid(self, hart_mask, start_addr, size, asid)
+    }
+    #[inline]
+    fn remote_hfence_gvma_vmid(
+        &self,
+        hart_mask: HartMask,
+        start_addr: usize,
+        size: usize,
+        vmid: usize,
+    ) -> SbiRet {
+        T::remote_hfence_gvma_vmid(self, hart_mask, start_addr, size, vmid)
+    }
+    #[inline]
+    fn remote_hfence_gvma(&self, hart_mask: HartMask, start_addr: usize, size: usize) -> SbiRet {
+        T::remote_hfence_gvma(self, hart_mask, start_addr, size)
+    }
+    #[inline]
+    fn remote_hfence_vvma_asid(
+        &self,
+        hart_mask: HartMask,
+        start_addr: usize,
+        size: usize,
+        asid: usize,
+    ) -> SbiRet {
+        T::remote_hfence_vvma_asid(self, hart_mask, start_addr, size, asid)
+    }
+    #[inline]
+    fn remote_hfence_vvma(&self, hart_mask: HartMask, start_addr: usize, size: usize) -> SbiRet {
+        T::remote_hfence_vvma(self, hart_mask, start_addr, size)
+    }
+}
+
 #[cfg(feature = "singleton")]
 use crate::util::AmoOnceRef;
 

+ 7 - 0
src/timer.rs

@@ -13,6 +13,13 @@ pub trait Timer: Send + Sync {
     fn set_timer(&self, stime_value: u64);
 }
 
+impl<T: Timer> Timer for &T {
+    #[inline]
+    fn set_timer(&self, stime_value: u64) {
+        T::set_timer(self, stime_value)
+    }
+}
+
 #[cfg(feature = "singleton")]
 static TIMER: AmoOnceRef<dyn Timer> = AmoOnceRef::new();