Bläddra i källkod

feat(spec): add spec for penglai pmp extension

Signed-off-by: Mechanicu <jinzhe.oerv@isrc.iscas.ac.cn>
Mechanicu 1 vecka sedan
förälder
incheckning
8a5bd8de35
5 ändrade filer med 149 tillägg och 0 borttagningar
  1. 1 0
      Cargo.toml
  2. 14 0
      library/penglai/Cargo.toml
  3. 26 0
      library/penglai/src/enclave.rs
  4. 56 0
      library/penglai/src/host.rs
  5. 52 0
      library/penglai/src/lib.rs

+ 1 - 0
Cargo.toml

@@ -6,6 +6,7 @@ members = [
     "library/sbi-spec",
     "library/sbi-testing",
     "library/rustsbi",
+    "library/penglai",
     "prototyper/prototyper",
     "prototyper/bench-kernel",
     "prototyper/test-kernel",

+ 14 - 0
library/penglai/Cargo.toml

@@ -0,0 +1,14 @@
+[package]
+name = "penglai"
+description = "Definitions and constants in the Penglai PMP extension"
+version = "0.0.0"
+authors = ["Char <jinzhe.oerv@isrc.iscas.ac.cn>", "Luo Jia <me@luojia.cc>"]
+documentation = "https://docs.rs/penglai"
+edition.workspace = true
+license.workspace = true
+repository.workspace = true
+keywords = ["riscv", "sbi", "rustsbi", "penglai", "tee"]
+categories = ["os", "embedded", "hardware-support", "no-std"]
+
+[dev-dependencies]
+static_assertions = "1.1.0"

+ 26 - 0
library/penglai/src/enclave.rs

@@ -0,0 +1,26 @@
+//! Penglai PMP enclave-side extension (Penglai Enclave extension) spec.
+
+/// Extension ID for Penglai Host extension.
+///
+/// Penglai Enclave extension isn't a standard extension. The currently used extension ID is temporary.
+pub const EID_PENGLAI_ENCLAVE: usize = 0x100101;
+pub use fid::*;
+
+mod fid {
+    /// Feature ID for enclave exit.
+    #[doc(alias = "SBI_EXIT_ENCLAVE")]
+    pub const ENCLAVE_EXIT: usize = 99;
+    /// Feature ID for request service from host.
+    #[doc(alias = "SBI_ENCLAVE_OCALL")]
+    pub const ENCLAVE_OCALL: usize = 98;
+    /// Feature ID for get key from secure monitor.
+    #[doc(alias = "SBI_GET_KEY")]
+    pub const GET_KEY: usize = 88;
+}
+
+pub mod ocall_type {
+    /// ocall for request host for print.
+    pub const OCALL_SYS_WRITE: usize = 3;
+    /// ocall reserved for user defined.
+    pub const OCALL_USER_DEFINED: usize = 9;
+}

+ 56 - 0
library/penglai/src/host.rs

@@ -0,0 +1,56 @@
+//! Penglai PMP host-side extension (Penglai Host extension) spec.
+
+/// Extension ID for Penglai Host extension.
+///
+/// Penglai Host extension isn't a standard extension. The currently used extension ID is temporary.
+pub const EID_PENGLAI_HOST: usize = 0x100100;
+pub use fid::*;
+
+mod fid {
+    /// Feature ID for init secure memory management.
+    #[doc(alias = "SBI_MM_INIT")]
+    pub const MM_INIT: usize = 100;
+    /// Feature ID for create an enclave.
+    #[doc(alias = "SBI_CREATE_ENCLAVE")]
+    pub const CREATE_ENCLAVE: usize = 99;
+    /// Feature ID for attest enclave and generate attest report.
+    #[doc(alias = "SBI_ATTEST_ENCLAVE")]
+    pub const ATTEST_ENCLAVE: usize = 98;
+    /// Feature ID for running enclave on current hart.
+    #[doc(alias = "SBI_RUN_ENCLAVE")]
+    pub const RUN_ENCLAVE: usize = 97;
+    /// Feature ID for stoping enclave.
+    #[doc(alias = "SBI_STOP_ENCLAVE")]
+    pub const STOP_ENCLAVE: usize = 96;
+    /// Feature ID for resume enclave.
+    #[doc(alias = "SBI_RESUME_ENCLAVE")]
+    pub const RESUME_ENCLAVE: usize = 95;
+    /// Feature ID for destory enclave.
+    #[doc(alias = "SBI_DESTROY_ENCLAVE")]
+    pub const DESTROY_ENCLAVE: usize = 94;
+    /// Feature ID for allocate secure memory from secure monitor.
+    #[doc(alias = "SBI_ALLOC_ENCLAVE_MM")]
+    pub const ALLOC_ENCLAVE_MM: usize = 93;
+    /// Feature ID for extend secure memory.
+    #[doc(alias = "SBI_MEMORY_EXTEND")]
+    pub const MEMORY_EXTEND: usize = 92;
+    /// Feature ID for reclaim secure memory from secure monitor.
+    #[doc(alias = "SBI_MEMORY_RECLAIM")]
+    pub const MEMORY_RECLAIM: usize = 91;
+    /// Feature ID for free secure memory used by enclave.
+    #[doc(alias = "SBI_FREE_ENCLAVE_MEM")]
+    pub const FREE_ENCLAVE_MEM: usize = 90;
+    /// Feature ID for print debug information.
+    #[doc(alias = "SBI_DEBUG_PRINT")]
+    pub const DEBUG_PRINT: usize = 88;
+}
+
+/// Enclave resume status.
+pub mod resume_status {
+    /// Resume enclave from the timer interrupt.
+    pub const RESUME_FROM_TIMER_IRQ: usize = 2000;
+    /// Resume enclave from enclave stopped.
+    pub const RESUME_FROM_STOP: usize = 2003;
+    /// Resume enclave from an ocall.
+    pub const RESUME_FROM_OCALL: usize = 2;
+}

+ 52 - 0
library/penglai/src/lib.rs

@@ -0,0 +1,52 @@
+//! Penglai PMP Extension structure and constant definitions.
+//!
+//! Penglai PMP Extension is a lightweight TEE solution built on RISC-V’s PMP feature.
+//! This crate provides the SBI structures and constant definitions required by
+//! the Penglai PMP extension.
+//!
+//! This crate can be integrated as part of RustSBI and used in Prototyper,
+//! or included as a component of Rust-based bare-metal applications or operating systems
+//! to facilitate invoking services provided by the Penglai PMP extension.
+#![no_std]
+
+pub mod enclave;
+pub mod host;
+
+#[cfg(test)]
+mod tests {
+    use static_assertions::const_assert_eq;
+
+    #[test]
+    fn test_penglai_host() {
+        use crate::host::*;
+        const_assert_eq!(0x100100, EID_PENGLAI_HOST);
+        const_assert_eq!(100, MM_INIT);
+        const_assert_eq!(99, CREATE_ENCLAVE);
+        const_assert_eq!(98, ATTEST_ENCLAVE);
+        const_assert_eq!(97, RUN_ENCLAVE);
+        const_assert_eq!(96, STOP_ENCLAVE);
+        const_assert_eq!(95, RESUME_ENCLAVE);
+        const_assert_eq!(94, DESTROY_ENCLAVE);
+        const_assert_eq!(93, ALLOC_ENCLAVE_MM);
+        const_assert_eq!(92, MEMORY_EXTEND);
+        const_assert_eq!(91, MEMORY_RECLAIM);
+        const_assert_eq!(90, FREE_ENCLAVE_MEM);
+        const_assert_eq!(88, DEBUG_PRINT);
+
+        const_assert_eq!(2, resume_status::RESUME_FROM_OCALL);
+        const_assert_eq!(2003, resume_status::RESUME_FROM_STOP);
+        const_assert_eq!(2000, resume_status::RESUME_FROM_TIMER_IRQ);
+    }
+
+    #[test]
+    fn test_penglai_enclave() {
+        use crate::enclave::*;
+        const_assert_eq!(0x100101, EID_PENGLAI_ENCLAVE);
+        const_assert_eq!(99, ENCLAVE_EXIT);
+        const_assert_eq!(98, ENCLAVE_OCALL);
+        const_assert_eq!(88, GET_KEY);
+
+        const_assert_eq!(3, ocall_type::OCALL_SYS_WRITE);
+        const_assert_eq!(9, ocall_type::OCALL_USER_DEFINED);
+    }
+}