Skip to content

Instantly share code, notes, and snippets.

@luojia65
Last active July 10, 2025 07:44
Show Gist options
  • Select an option

  • Save luojia65/c7a0e7a727562681c2c944d2ff86b9e8 to your computer and use it in GitHub Desktop.

Select an option

Save luojia65/c7a0e7a727562681c2c944d2ff86b9e8 to your computer and use it in GitHub Desktop.
// lib.rs
#![no_std]
pub mod host;
pub mod guest;
#[cfg(test)]
mod tests {
// 参考:sbi-spec
}
// host.rs
pub const EID_HOST: usize = 0x100100;
mod fid {
/// 文档参考sbi-spec
pub const RUN_ENCLAVE: usize = 0x0;
pub const DESTROY_ENCLAVE: usize = 0x1;
// ..
}
pub use fid::*;
// guest.rs
// TODO
// Cargo.toml
penglai = { path = "../../library/penglai" }
// workspace root Cargo.toml
[workspace]
resolver = "3"
members = [
"library/macros",
"library/sbi-rt",
"library/sbi-spec",
"library/sbi-testing",
"library/rustsbi",
"library/penglai", # <------
"prototyper/prototyper",
"prototyper/bench-kernel",
"prototyper/test-kernel",
"xtask",
]
#[inline]
#[allow(clippy::too_many_arguments)]
pub fn sbi_call_handler(
mut ctx: FastContext,
a1: usize,
a2: usize,
a3: usize,
a4: usize,
a5: usize,
a6: usize,
a7: usize,
) -> FastResult {
use sbi_spec::{base, hsm, legacy};
/*
let mut ret = unsafe {
PLATFORM
.sbi
.handle_ecall(a7, a6, [ctx.a0(), a1, a2, a3, a4, a5])
};
*/
let mut ret = match a7 {
penglai::EID_HOST | penglai::EID_GUEST => PENGLAI_PLATFORM.handle_ecall(a7, a6, [ctx.a0(), a1, a2, a3, a4, a5]),
_ => unsafe { PLATFORM.sbi.handle_ecall(a7, a6, [ctx.a0(), a1, a2, a3, a4, a5]) }
};
if ret.is_ok() {
match (a7, a6) {
// Handle non-retentive suspend
(hsm::EID_HSM, hsm::HART_SUSPEND)
if matches!(ctx.a0() as u32, hsm::suspend_type::NON_RETENTIVE) =>
{
return switch(ctx, a1, a2);
}
// Handle legacy console probe
(base::EID_BASE, base::PROBE_EXTENSION)
if matches!(
ctx.a0(),
legacy::LEGACY_CONSOLE_PUTCHAR | legacy::LEGACY_CONSOLE_GETCHAR |
penglai::EID_HOST | penglai::EID_GUEST
) =>
{
ret.value = 1;
}
_ => {}
}
} else {
match a7 {
legacy::LEGACY_CONSOLE_PUTCHAR => {
ret.error = console::putchar(ctx.a0());
ret.value = a1;
}
legacy::LEGACY_CONSOLE_GETCHAR => {
ret.error = console::getchar();
ret.value = a1;
}
_ => {}
}
}
ctx.regs().a = [ret.error, ret.value, a2, a3, a4, a5, a6, a7];
let epc = mepc::read();
mepc::write(epc + get_inst(epc).1);
ctx.restore()
}
// prototyper/prototyper/src/tee/penglai.rs
struct PenglaiPlatform {
// ...
}
impl RustSBI for PenglaiPlatform {
fn handle_ecall(
&self,
extension: usize,
function: usize,
param: [usize; 6],
) -> SbiRet {
...
}
}
pub(crate) static mut PENGLAI_PLATFORM: PenglaiPlatform = PenglaiPlatform::new();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment