Browse Source

rustsbi: reduce code size by inlining internal functions

Signed-off-by: luojia65 <[email protected]>
luojia65 3 years ago
parent
commit
8c9312d9f1
4 changed files with 24 additions and 2 deletions
  1. 2 1
      CHANGELOG.md
  2. 11 0
      src/ecall.rs
  3. 2 1
      src/hart_mask.rs
  4. 9 0
      src/legacy_stdio.rs

+ 2 - 1
CHANGELOG.md

@@ -13,12 +13,13 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
 
 ## [0.2.1] - 2022-02-14
 
-This update fixes a moderate bug on IPI module. The previous version of RustSBI did not follow the SBI definition of IPI
+This update fixes a severe bug on IPI module. The previous version of RustSBI did not follow the SBI definition of IPI
 module on SBI v0.3 format. Users are encouraged to use 0.2.1 and newer version instead of yanked 0.2.0 version.
 
 ### Modified
 
 - Internal speed up to new SBI v0.3+ IPI procedure
+- Reduce code size by inlining internal functions
 
 ### Fixed
 

+ 11 - 0
src/ecall.rs

@@ -127,6 +127,7 @@ const SBI_ERR_ALREADY_STOPPED: usize = usize::from_ne_bytes(isize::to_ne_bytes(-
 
 impl SbiRet {
     /// Return success SBI state with given value.
+    #[inline]
     pub fn ok(value: usize) -> SbiRet {
         SbiRet {
             error: SBI_SUCCESS,
@@ -134,6 +135,7 @@ impl SbiRet {
         }
     }
     /// The SBI call request failed for unknown reasons.
+    #[inline]
     pub fn failed() -> SbiRet {
         SbiRet {
             error: SBI_ERR_FAILED,
@@ -142,6 +144,7 @@ impl SbiRet {
     }
     /// SBI call failed due to not supported by target ISA, operation type not supported,
     /// or target operation type not implemented on purpose.
+    #[inline]
     pub fn not_supported() -> SbiRet {
         SbiRet {
             error: SBI_ERR_NOT_SUPPORTED,
@@ -150,6 +153,7 @@ impl SbiRet {
     }
     /// SBI call failed due to invalid hart mask parameter, invalid target hart id, invalid operation type
     /// or invalid resource index.
+    #[inline]
     pub fn invalid_param() -> SbiRet {
         SbiRet {
             error: SBI_ERR_INVALID_PARAM,
@@ -158,6 +162,7 @@ impl SbiRet {
     }
     /// SBI call failed for invalid mask start address, not a valid physical address parameter,
     /// or the target address is prohibited by PMP to run in supervisor mode.
+    #[inline]
     pub fn invalid_address() -> SbiRet {
         SbiRet {
             error: SBI_ERR_INVALID_ADDRESS,
@@ -166,6 +171,7 @@ impl SbiRet {
     }
     /// SBI call failed for the target resource is already available, e.g. the target hart is already
     /// started when caller still request it to start.
+    #[inline]
     pub fn already_available() -> SbiRet {
         SbiRet {
             error: SBI_ERR_ALREADY_AVAILABLE,
@@ -173,6 +179,7 @@ impl SbiRet {
         }
     }
     /// SBI call failed for the target resource is already started, e.g. target performance counter is started.
+    #[inline]
     pub fn already_started() -> SbiRet {
         SbiRet {
             error: SBI_ERR_ALREADY_STARTED,
@@ -180,12 +187,14 @@ impl SbiRet {
         }
     }
     /// SBI call failed for the target resource is already stopped, e.g. target performance counter is stopped.
+    #[inline]
     pub fn already_stopped() -> SbiRet {
         SbiRet {
             error: SBI_ERR_ALREADY_STOPPED,
             value: 0,
         }
     }
+    #[inline]
     pub(crate) fn legacy_ok(legacy_value: usize) -> SbiRet {
         SbiRet {
             error: legacy_value,
@@ -193,12 +202,14 @@ impl SbiRet {
         }
     }
     // only used for legacy where a0, a1 return value is not modified
+    #[inline]
     pub(crate) fn legacy_void(self, a0: usize, a1: usize) -> SbiRet {
         SbiRet {
             error: a0,
             value: a1,
         }
     }
+    #[inline]
     pub(crate) fn legacy_return(self, a1: usize) -> SbiRet {
         SbiRet {
             error: self.error,

+ 2 - 1
src/hart_mask.rs

@@ -18,6 +18,7 @@ impl HartMask {
         }
     }
     /// Check if the `hart_id` is included in this hart mask structure.
+    #[inline]
     pub fn has_bit(&self, hart_id: usize) -> bool {
         match self.inner {
             MaskInner::BitVector { hart_mask, hart_mask_base } => {
@@ -49,7 +50,7 @@ impl HartMask {
     ///
     /// Construct a hart mask from legacy bit vector and number of harts in current platform.
     #[inline]
-    pub unsafe fn legacy_from_addr(vaddr: usize) -> HartMask {
+    pub(crate) unsafe fn legacy_from_addr(vaddr: usize) -> HartMask {
         HartMask {
             inner: MaskInner::Legacy {
                 legacy_bit_vector: vaddr as *const _,

+ 9 - 0
src/legacy_stdio.rs

@@ -20,6 +20,7 @@ struct EmbeddedHalSerial<T> {
 
 impl<T> EmbeddedHalSerial<T> {
     /// Create a wrapper with a value
+    #[inline]
     fn new(inner: T) -> Self {
         Self { inner }
     }
@@ -29,12 +30,14 @@ impl<T: Send> LegacyStdio for EmbeddedHalSerial<T>
 where
     T: Read<u8> + Write<u8>,
 {
+    #[inline]
     fn getchar(&mut self) -> u8 {
         // 直接调用embedded-hal里面的函数
         // 关于unwrap:因为这个是legacy函数,这里没有详细的处理流程,就panic掉
         block!(self.inner.read()).ok().unwrap()
     }
 
+    #[inline]
     fn putchar(&mut self, ch: u8) {
         // 直接调用函数写一个字节
         block!(self.inner.write(ch)).ok();
@@ -51,10 +54,12 @@ where
     T: Write<u8> + Send + 'static,
     R: Read<u8> + Send + 'static,
 {
+    #[inline]
     fn getchar(&mut self) -> u8 {
         block!(self.1.read()).ok().unwrap()
     }
 
+    #[inline]
     fn putchar(&mut self, ch: u8) {
         block!(self.0.write(ch)).ok();
         block!(self.0.flush()).ok();
@@ -79,12 +84,14 @@ where
     *LEGACY_STDIO.lock() = Some(Box::new(serial));
 }
 
+#[inline]
 pub fn legacy_stdio_putchar(ch: u8) {
     if let Some(stdio) = LEGACY_STDIO.lock().as_mut() {
         stdio.putchar(ch)
     }
 }
 
+#[inline]
 pub fn legacy_stdio_getchar() -> usize {
     if let Some(stdio) = LEGACY_STDIO.lock().as_mut() {
         stdio.getchar() as usize
@@ -100,6 +107,7 @@ use core::fmt;
 struct Stdout;
 
 impl fmt::Write for Stdout {
+    #[inline]
     fn write_str(&mut self, s: &str) -> fmt::Result {
         if let Some(stdio) = LEGACY_STDIO.lock().as_mut() {
             for byte in s.as_bytes() {
@@ -110,6 +118,7 @@ impl fmt::Write for Stdout {
     }
 }
 
+#[inline]
 #[doc(hidden)]
 pub fn _print(args: fmt::Arguments) {
     use fmt::Write;