Browse Source

gate SBI legacy extension under `legacy` feature

luojia65 2 years ago
parent
commit
a577447b02
7 changed files with 40 additions and 5 deletions
  1. 2 0
      CHANGELOG.md
  2. 3 1
      Cargo.toml
  3. 15 0
      src/ecall.rs
  4. 6 3
      src/hart_mask.rs
  5. 2 0
      src/lib.rs
  6. 1 0
      src/reset.rs
  7. 11 1
      src/util.rs

+ 2 - 0
CHANGELOG.md

@@ -8,8 +8,10 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
 ## Unreleased
 
 ### Added
+- Feature `legacy` to gate SBI legacy extension
 
 ### Modified
+- Update depenency embedded-hal to 1.0.0-alpha.8
 
 ### Fixed
 

+ 3 - 1
Cargo.toml

@@ -16,12 +16,14 @@ categories = ["os", "embedded", "hardware-support", "no-std"]
 edition = "2021"
 
 [dependencies]
-embedded-hal = "1.0.0-alpha.8"
+embedded-hal = { version = "1.0.0-alpha.8", optional = true }
 nb = "1.0"
 riscv = "0.7"
 
 [features]
 default = []
+# Support legacy extension; this feature is not included by default.
+legacy = ["embedded-hal"]
 # Dynamic pointer widths on SBI implementations; useful for developing hypervisors
 guest = []
 

+ 15 - 0
src/ecall.rs

@@ -4,6 +4,7 @@
 mod base;
 mod hsm;
 mod ipi;
+#[cfg(feature = "legacy")]
 mod legacy;
 mod pmu;
 mod rfence;
@@ -18,14 +19,20 @@ pub const EXTENSION_HSM: u32 = 0x48534D;
 pub const EXTENSION_SRST: u32 = 0x53525354;
 pub const EXTENSION_PMU: u32 = 0x504D55;
 
+#[cfg(feature = "legacy")]
 const LEGACY_SET_TIMER: u32 = 0x0;
+#[cfg(feature = "legacy")]
 const LEGACY_CONSOLE_PUTCHAR: u32 = 0x01;
+#[cfg(feature = "legacy")]
 const LEGACY_CONSOLE_GETCHAR: u32 = 0x02;
 // const LEGACY_CLEAR_IPI: u32 = 0x03;
+#[cfg(feature = "legacy")]
 const LEGACY_SEND_IPI: u32 = 0x04;
+#[cfg(feature = "legacy")]
 // const LEGACY_REMOTE_FENCE_I: u32 = 0x05;
 // const LEGACY_REMOTE_SFENCE_VMA: u32 = 0x06;
 // const LEGACY_REMOTE_SFENCE_VMA_ASID: u32 = 0x07;
+#[cfg(feature = "legacy")]
 const LEGACY_SHUTDOWN: u32 = 0x08;
 
 /// Supervisor environment call handler function
@@ -99,6 +106,7 @@ pub fn handle_ecall(extension: usize, function: usize, param: [usize; 6]) -> Sbi
                 function, param[0], param[1], param[2], param[3], param[4], param[5],
             ),
         },
+        #[cfg(feature = "legacy")]
         LEGACY_SET_TIMER => match () {
             #[cfg(target_pointer_width = "64")]
             () => legacy::set_timer_64(param[0]),
@@ -106,9 +114,13 @@ pub fn handle_ecall(extension: usize, function: usize, param: [usize; 6]) -> Sbi
             () => legacy::set_timer_32(param[0], param[1]),
         }
         .legacy_void(param[0], param[1]),
+        #[cfg(feature = "legacy")]
         LEGACY_CONSOLE_PUTCHAR => legacy::console_putchar(param[0]).legacy_void(param[0], param[1]),
+        #[cfg(feature = "legacy")]
         LEGACY_CONSOLE_GETCHAR => legacy::console_getchar().legacy_return(param[1]),
+        #[cfg(feature = "legacy")]
         LEGACY_SEND_IPI => legacy::send_ipi(param[0]).legacy_void(param[0], param[1]),
+        #[cfg(feature = "legacy")]
         LEGACY_SHUTDOWN => legacy::shutdown().legacy_void(param[0], param[1]),
         _ => SbiRet::not_supported(),
     }
@@ -204,6 +216,7 @@ impl SbiRet {
             value: 0,
         }
     }
+    #[cfg(feature = "legacy")]
     #[inline]
     pub(crate) fn legacy_ok(legacy_value: usize) -> SbiRet {
         SbiRet {
@@ -212,6 +225,7 @@ impl SbiRet {
         }
     }
     // only used for legacy where a0, a1 return value is not modified
+    #[cfg(feature = "legacy")]
     #[inline]
     pub(crate) fn legacy_void(self, a0: usize, a1: usize) -> SbiRet {
         SbiRet {
@@ -219,6 +233,7 @@ impl SbiRet {
             value: a1,
         }
     }
+    #[cfg(feature = "legacy")]
     #[inline]
     pub(crate) fn legacy_return(self, a1: usize) -> SbiRet {
         SbiRet {

+ 6 - 3
src/hart_mask.rs

@@ -40,6 +40,7 @@ impl HartMask {
                 }
                 hart_mask & (1 << idx) != 0
             }
+            #[cfg(feature = "legacy")]
             MaskInner::Legacy { legacy_bit_vector } => {
                 slow_legacy_has_bit(legacy_bit_vector, hart_id)
             }
@@ -50,6 +51,7 @@ impl HartMask {
     /// from S level, it would result in machine level load access or load misaligned exception.*
     ///
     /// Construct a hart mask from legacy bit vector and number of harts in current platform.
+    #[cfg(feature = "legacy")]
     #[inline]
     pub(crate) unsafe fn legacy_from_addr(vaddr: usize) -> HartMask {
         HartMask {
@@ -66,12 +68,12 @@ enum MaskInner {
         hart_mask: usize,
         hart_mask_base: usize,
     },
-    Legacy {
-        legacy_bit_vector: *const usize,
-    },
+    #[cfg(feature = "legacy")]
+    Legacy { legacy_bit_vector: *const usize },
 }
 
 // not #[inline] to speed up new version bit vector
+#[cfg(feature = "legacy")]
 fn slow_legacy_has_bit(legacy_bit_vector: *const usize, hart_id: usize) -> bool {
     fn split_index_usize(index: usize) -> (usize, usize) {
         let bits_in_usize = usize::BITS as usize;
@@ -82,6 +84,7 @@ fn slow_legacy_has_bit(legacy_bit_vector: *const usize, hart_id: usize) -> bool
     cur_vector & (1 << j) != 0
 }
 
+#[cfg(feature = "legacy")]
 #[inline]
 unsafe fn get_vaddr_usize(vaddr_ptr: *const usize) -> usize {
     match () {

+ 2 - 0
src/lib.rs

@@ -161,6 +161,7 @@
 
 extern crate alloc;
 
+#[cfg(feature = "legacy")]
 #[doc(hidden)]
 #[macro_use]
 pub mod legacy_stdio;
@@ -208,6 +209,7 @@ pub use ecall::SbiRet;
 pub use hart_mask::HartMask;
 pub use hsm::{init_hsm, Hsm};
 pub use ipi::{init_ipi, Ipi};
+#[cfg(feature = "legacy")]
 #[doc(hidden)]
 pub use legacy_stdio::{legacy_stdio_getchar, legacy_stdio_putchar};
 pub use logo::LOGO;

+ 1 - 0
src/reset.rs

@@ -91,6 +91,7 @@ pub(crate) fn system_reset(reset_type: usize, reset_reason: usize) -> SbiRet {
     SbiRet::not_supported()
 }
 
+#[cfg(feature = "legacy")]
 #[inline]
 pub(crate) fn legacy_reset() -> ! {
     if let Some(obj) = RESET.get() {

+ 11 - 1
src/util.rs

@@ -3,13 +3,14 @@
 // Ref: once_cell
 
 use alloc::boxed::Box;
+#[cfg(feature = "legacy")]
+use core::ops::{Deref, DerefMut};
 use core::{
     arch::asm,
     cell::UnsafeCell,
     fmt::{self, Debug},
     marker::PhantomData,
     mem::MaybeUninit,
-    ops::{Deref, DerefMut},
     ptr::{self, Pointee},
 };
 
@@ -124,16 +125,19 @@ impl<T: ?Sized> OnceFatBox<T> {
 unsafe impl<T: Sync + Send + ?Sized> Sync for OnceFatBox<T> {}
 
 /// Use only amo instructions on mutex; no lr/sc instruction is used
+#[cfg(feature = "legacy")]
 pub struct AmoMutex<T: ?Sized> {
     lock: UnsafeCell<u8>,
     data: UnsafeCell<T>,
 }
 
+#[cfg(feature = "legacy")]
 pub struct AmoMutexGuard<'a, T: ?Sized> {
     lock: *mut u8,
     data: &'a mut T,
 }
 
+#[cfg(feature = "legacy")]
 impl<T> AmoMutex<T> {
     /// Create a new AmoMutex
     #[inline]
@@ -168,9 +172,12 @@ impl<T> AmoMutex<T> {
     }
 }
 
+#[cfg(feature = "legacy")]
 unsafe impl<T: ?Sized + Send> Sync for AmoMutex<T> {}
+#[cfg(feature = "legacy")]
 unsafe impl<T: ?Sized + Send> Send for AmoMutex<T> {}
 
+#[cfg(feature = "legacy")]
 impl<'a, T: ?Sized> Deref for AmoMutexGuard<'a, T> {
     type Target = T;
     #[inline]
@@ -179,12 +186,15 @@ impl<'a, T: ?Sized> Deref for AmoMutexGuard<'a, T> {
     }
 }
 
+#[cfg(feature = "legacy")]
 impl<'a, T: ?Sized> DerefMut for AmoMutexGuard<'a, T> {
     #[inline]
     fn deref_mut(&mut self) -> &mut T {
         self.data
     }
 }
+
+#[cfg(feature = "legacy")]
 impl<'a, T: ?Sized> Drop for AmoMutexGuard<'a, T> {
     /// The dropping of the mutex guard will release the lock it was created from.
     #[inline]