diff --git a/rustsbi-qemu/src/execute.rs b/rustsbi-qemu/src/execute.rs index 99b6f26..96c0df0 100644 --- a/rustsbi-qemu/src/execute.rs +++ b/rustsbi-qemu/src/execute.rs @@ -1,4 +1,4 @@ -use crate::{clint, hart_id, qemu_hsm::QemuHsm, Supervisor}; +use crate::{clint, hart_id, qemu_hsm::QemuHsm, Supervisor, penglai}; use core::arch::asm; use riscv::register::*; @@ -111,18 +111,18 @@ impl Context { use rustsbi::spec::{binary::*, hsm::*, srst::*}; let extension = self.a(7); let function = self.a(6); - let ans = rustsbi::ecall( - extension, - function, - [ - self.a(0), - self.a(1), - self.a(2), - self.a(3), - self.a(4), - self.a(5), - ], - ); + let param = [ + self.a(0), + self.a(1), + self.a(2), + self.a(3), + self.a(4), + self.a(5), + ]; + let ans = match extension { + penglai::EID_PENGLAI_HOST => penglai::ecall(extension, function, param), + _ => rustsbi::ecall(extension, function, param), + }; // 判断导致退出执行流程的调用 if ans.error == RET_SUCCESS { match extension { diff --git a/rustsbi-qemu/src/main.rs b/rustsbi-qemu/src/main.rs index 5cbb46a..db0231d 100644 --- a/rustsbi-qemu/src/main.rs +++ b/rustsbi-qemu/src/main.rs @@ -11,6 +11,9 @@ mod ns16550a; mod qemu_hsm; mod qemu_test; +// penglai enclave. todo: move to separate crate? +mod penglai; + #[macro_use] // for print extern crate rustsbi; diff --git a/rustsbi-qemu/src/penglai.rs b/rustsbi-qemu/src/penglai.rs new file mode 100644 index 0000000..3c41f8e --- /dev/null +++ b/rustsbi-qemu/src/penglai.rs @@ -0,0 +1,82 @@ +/* todo: move to separate crate like sbi_spec? */ +use rustsbi::spec::binary::SbiRet; +pub use host::EID_PENGLAI_HOST; + +mod host { + pub const EID_PENGLAI_HOST: usize /*u32*/ = 0x100100; + + pub const CREATE_ENCLAVE: usize = 99; + pub const ATTEST_ENCLAVE: usize = 98; + pub const RUN_ENCLAVE: usize = 97; + pub const STOP_ENCLAVE: usize = 96; + pub const RESUME_ENCLAVE: usize = 95; + pub const DESTROY_ENCALVE: usize = 94; + + #[repr(C)] + pub struct EnclaveCreate { + pub enclave_id_ptr: usize, /* todo: what's this? */ + pub enclave_name: [u8; 16], + pub enclave_type: EnclaveType, + pub enclave_physical_address: usize, + pub enclave_length: usize, + pub entry_point: usize, + pub free_memory: usize, // what's this? + pub kbuffer_physical_address: usize, + pub kbuffer_length: usize, + pub shared_mem_physical_address: usize, + pub shared_mem_length: usize, + pub ecall_argument: [usize; 4], + pub return_value: usize, + } + + #[repr(C)] + pub struct EnclaveRun { + pub memory_argument_address: usize, + pub memory_argument_size: usize, + // todo: extended arguments + } + + #[repr(usize)] + pub enum EnclaveType { + Normal = 0, + Server = 1, + } + + #[repr(C)] + pub struct ShangMiReport { + pub hash: [u8; 32], + pub signature: [u8; 64], + pub public_key: [u8; 64], + } + + #[repr(C)] + pub struct EnclaveReport { + pub hash: [u8; 32], + pub signature: [u8; 64], + pub nonce: usize, + } + + #[repr(C)] + pub struct Report { + pub shangmi_report: ShangMiReport, + pub enclave_report: EnclaveReport, + pub device_public_key: [u8; 64], + } +} + +use host::*; + +// todo: instance based? strctu Contect { penglai: Penglai, ... } + +#[inline] +pub fn ecall(extension: usize, function: usize, param: [usize; 6]) -> SbiRet { + match (extension, function) { + (EID_PENGLAI_HOST, CREATE_ENCLAVE) => create_enclave(param[0]), + _ => SbiRet::not_supported(), + } +} + +#[inline] +fn create_enclave(param: usize) -> SbiRet { + todo!() +}