2024-01-03 03:22:36 +00:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
|
2023-11-29 02:30:05 +00:00
|
|
|
use super::*;
|
2023-12-06 09:04:07 +00:00
|
|
|
use crate::error::Error;
|
2023-11-29 02:30:05 +00:00
|
|
|
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};
|
2023-11-30 06:30:32 +00:00
|
|
|
use tdx_guest::tdcall::{get_report, TdCallError};
|
2023-11-29 02:30:05 +00:00
|
|
|
|
|
|
|
|
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 {
|
2023-11-30 06:30:32 +00:00
|
|
|
DeviceId::new(0xa, 0x7b)
|
2023-11-29 02:30:05 +00:00
|
|
|
}
|
|
|
|
|
}
|
2023-11-30 06:30:32 +00:00
|
|
|
|
2023-12-06 09:04:07 +00:00
|
|
|
impl From<TdCallError> for Error {
|
|
|
|
|
fn from(err: TdCallError) -> Self {
|
|
|
|
|
match err {
|
|
|
|
|
TdCallError::TdxNoValidVeInfo => {
|
|
|
|
|
Error::with_message(Errno::EINVAL, "TdCallError::TdxNoValidVeInfo")
|
|
|
|
|
}
|
|
|
|
|
TdCallError::TdxOperandInvalid => {
|
|
|
|
|
Error::with_message(Errno::EINVAL, "TdCallError::TdxOperandInvalid")
|
|
|
|
|
}
|
|
|
|
|
TdCallError::TdxPageAlreadyAccepted => {
|
|
|
|
|
Error::with_message(Errno::EINVAL, "TdCallError::TdxPageAlreadyAccepted")
|
|
|
|
|
}
|
|
|
|
|
TdCallError::TdxPageSizeMismatch => {
|
|
|
|
|
Error::with_message(Errno::EINVAL, "TdCallError::TdxPageSizeMismatch")
|
|
|
|
|
}
|
|
|
|
|
TdCallError::TdxOperandBusy => {
|
|
|
|
|
Error::with_message(Errno::EBUSY, "TdCallError::TdxOperandBusy")
|
|
|
|
|
}
|
|
|
|
|
TdCallError::Other => Error::with_message(Errno::EAGAIN, "TdCallError::Other"),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 02:30:05 +00:00
|
|
|
impl FileIo for TdxGuest {
|
|
|
|
|
fn read(&self, buf: &mut [u8]) -> Result<usize> {
|
|
|
|
|
return_errno_with_message!(Errno::EPERM, "Read operation not supported")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn write(&self, buf: &[u8]) -> Result<usize> {
|
|
|
|
|
return_errno_with_message!(Errno::EPERM, "Write operation not supported")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn ioctl(&self, cmd: IoctlCmd, arg: usize) -> Result<i32> {
|
|
|
|
|
match cmd {
|
|
|
|
|
IoctlCmd::TDXGETREPORT => {
|
2023-11-30 06:30:32 +00:00
|
|
|
let mut tdx_report: TdxReportRequest = read_val_from_user(arg)?;
|
|
|
|
|
match get_report(&mut tdx_report.tdreport, &tdx_report.reportdata) {
|
|
|
|
|
Ok(_) => {}
|
2023-12-06 09:04:07 +00:00
|
|
|
Err(err) => return Err(err.into()),
|
2023-11-30 06:30:32 +00:00
|
|
|
};
|
2023-11-29 02:30:05 +00:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|