From 55ea3dc86f921325888b2220208135af446e6ebf Mon Sep 17 00:00:00 2001 From: Hsy-Intel Date: Wed, 29 Nov 2023 10:30:05 +0800 Subject: [PATCH] Add tdx-guest device --- services/libs/jinux-std/src/device/mod.rs | 8 +++ .../libs/jinux-std/src/device/tdxguest/mod.rs | 55 +++++++++++++++++++ services/libs/jinux-std/src/fs/device.rs | 1 + services/libs/jinux-std/src/fs/utils/inode.rs | 1 + services/libs/jinux-std/src/fs/utils/ioctl.rs | 2 + 5 files changed, 67 insertions(+) create mode 100644 services/libs/jinux-std/src/device/tdxguest/mod.rs diff --git a/services/libs/jinux-std/src/device/mod.rs b/services/libs/jinux-std/src/device/mod.rs index 2fc391eef..79d1248d2 100644 --- a/services/libs/jinux-std/src/device/mod.rs +++ b/services/libs/jinux-std/src/device/mod.rs @@ -1,6 +1,8 @@ mod null; mod pty; mod random; +#[cfg(feature = "intel_tdx")] +mod tdxguest; pub mod tty; mod urandom; mod zero; @@ -10,6 +12,8 @@ use crate::prelude::*; pub use pty::new_pty_pair; pub use pty::{PtyMaster, PtySlave}; pub use random::Random; +#[cfg(feature = "intel_tdx")] +pub use tdxguest::TdxGuest; pub use urandom::Urandom; use self::tty::get_n_tty; @@ -25,6 +29,10 @@ pub fn init() -> Result<()> { add_node(console, "console")?; let tty = Arc::new(tty::TtyDevice); add_node(tty, "tty")?; + #[cfg(feature = "intel_tdx")] + let tdx_guest = Arc::new(tdxguest::TdxGuest); + #[cfg(feature = "intel_tdx")] + add_node(tdx_guest, "tdx-guest")?; let random = Arc::new(random::Random); add_node(random, "random")?; let urandom = Arc::new(urandom::Urandom); diff --git a/services/libs/jinux-std/src/device/tdxguest/mod.rs b/services/libs/jinux-std/src/device/tdxguest/mod.rs new file mode 100644 index 000000000..b23604a29 --- /dev/null +++ b/services/libs/jinux-std/src/device/tdxguest/mod.rs @@ -0,0 +1,55 @@ +use super::*; +use crate::events::IoEvents; +use crate::fs::inode_handle::FileIo; +use crate::fs::utils::IoctlCmd; +use crate::process::signal::Poller; +use crate::util::{read_val_from_user, write_val_to_user}; +use tdx_guest::tdcall::get_report; + +const TDX_REPORTDATA_LEN: usize = 64; +const TDX_REPORT_LEN: usize = 1024; + +#[derive(Debug, Clone, Copy, Pod)] +#[repr(C)] +pub struct TdxReportRequest { + reportdata: [u8; TDX_REPORTDATA_LEN], + tdreport: [u8; TDX_REPORT_LEN], +} + +pub struct TdxGuest; + +impl Device for TdxGuest { + fn type_(&self) -> DeviceType { + DeviceType::MiscDevice + } + + fn id(&self) -> DeviceId { + DeviceId::new(10, 0) + } +} +impl FileIo for TdxGuest { + fn read(&self, buf: &mut [u8]) -> Result { + return_errno_with_message!(Errno::EPERM, "Read operation not supported") + } + + fn write(&self, buf: &[u8]) -> Result { + return_errno_with_message!(Errno::EPERM, "Write operation not supported") + } + + fn ioctl(&self, cmd: IoctlCmd, arg: usize) -> Result { + match cmd { + IoctlCmd::TDXGETREPORT => { + let tdx_report: TdxReportRequest = read_val_from_user(arg)?; + get_report(&tdx_report.tdreport, &tdx_report.reportdata).unwrap(); + write_val_to_user(arg, &tdx_report)?; + Ok(0) + } + _ => return_errno_with_message!(Errno::EPERM, "Unsupported ioctl"), + } + } + + fn poll(&self, mask: IoEvents, poller: Option<&Poller>) -> IoEvents { + let events = IoEvents::IN | IoEvents::OUT; + events & mask + } +} diff --git a/services/libs/jinux-std/src/fs/device.rs b/services/libs/jinux-std/src/fs/device.rs index b85c577d6..d93928f5d 100644 --- a/services/libs/jinux-std/src/fs/device.rs +++ b/services/libs/jinux-std/src/fs/device.rs @@ -33,6 +33,7 @@ impl Debug for dyn Device { pub enum DeviceType { CharDevice, BlockDevice, + MiscDevice, } /// Device Id diff --git a/services/libs/jinux-std/src/fs/utils/inode.rs b/services/libs/jinux-std/src/fs/utils/inode.rs index 2573ff159..5726442fa 100644 --- a/services/libs/jinux-std/src/fs/utils/inode.rs +++ b/services/libs/jinux-std/src/fs/utils/inode.rs @@ -51,6 +51,7 @@ impl From for InodeType { match type_ { DeviceType::CharDevice => InodeType::CharDevice, DeviceType::BlockDevice => InodeType::BlockDevice, + DeviceType::MiscDevice => InodeType::CharDevice, } } } diff --git a/services/libs/jinux-std/src/fs/utils/ioctl.rs b/services/libs/jinux-std/src/fs/utils/ioctl.rs index 9a57cb020..0901c544b 100644 --- a/services/libs/jinux-std/src/fs/utils/ioctl.rs +++ b/services/libs/jinux-std/src/fs/utils/ioctl.rs @@ -29,4 +29,6 @@ pub enum IoctlCmd { TIOCSPTLCK = 0x40045431, /// Safely open the slave TIOCGPTPEER = 0x40045441, + /// Get tdx report using TDCALL + TDXGETREPORT = 0xc4405401, }