mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-27 03:13:23 +00:00
make tdcall and tdvmcall public and polish code
This commit is contained in:
committed by
Tate, Hongliang Tian
parent
12fc074f56
commit
d7710207bb
@ -8,18 +8,18 @@ pub mod asm;
|
||||
pub mod tdcall;
|
||||
pub mod tdvmcall;
|
||||
|
||||
pub use self::tdcall::{deadloop, tdg_vp_veinfo_get, TdxVirtualExceptionType};
|
||||
pub use self::tdcall::{get_veinfo, TdxVirtualExceptionType};
|
||||
pub use self::tdvmcall::print;
|
||||
|
||||
use alloc::string::{String, ToString};
|
||||
use raw_cpuid::{native_cpuid::cpuid_count, CpuIdResult};
|
||||
use tdcall::{tdg_vp_vmcall, InitError, TdgVeInfo, TdgVpInfo};
|
||||
use tdcall::{InitError, TdgVeInfo, TdgVpInfo};
|
||||
use tdvmcall::*;
|
||||
|
||||
const TDX_CPUID_LEAF_ID: u64 = 0x21;
|
||||
|
||||
pub fn tdx_early_init() -> Result<TdgVpInfo, InitError> {
|
||||
match is_tdx_guest() {
|
||||
Ok(_) => Ok(tdcall::tdg_vp_info()?),
|
||||
Ok(_) => Ok(tdcall::get_tdinfo()?),
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
@ -30,9 +30,9 @@ fn is_tdx_guest() -> Result<(), InitError> {
|
||||
return Err(InitError::TdxCpuLeafIdError);
|
||||
}
|
||||
let cpuid_result: CpuIdResult = cpuid_count(TDX_CPUID_LEAF_ID as u32, 0);
|
||||
if convert_ascii(cpuid_result.ebx) == "Inte"
|
||||
&& convert_ascii(cpuid_result.edx) == "lTDX"
|
||||
&& convert_ascii(cpuid_result.ecx) == " "
|
||||
if &cpuid_result.ebx.to_ne_bytes() == b"Inte"
|
||||
&& &cpuid_result.ebx.to_ne_bytes() == b"lTDX"
|
||||
&& &cpuid_result.ecx.to_ne_bytes() == b" "
|
||||
{
|
||||
Ok(())
|
||||
} else {
|
||||
@ -40,16 +40,6 @@ fn is_tdx_guest() -> Result<(), InitError> {
|
||||
}
|
||||
}
|
||||
|
||||
fn convert_ascii(reg: u32) -> String {
|
||||
let bytes = [
|
||||
(reg & 0xFF) as u8,
|
||||
((reg >> 8) & 0xFF) as u8,
|
||||
((reg >> 16) & 0xFF) as u8,
|
||||
((reg >> 24) & 0xFF) as u8,
|
||||
];
|
||||
String::from_utf8_lossy(&bytes).to_string()
|
||||
}
|
||||
|
||||
pub trait TdxTrapFrame {
|
||||
fn rax(&self) -> usize;
|
||||
fn set_rax(&mut self, rax: usize);
|
||||
@ -69,13 +59,35 @@ pub trait TdxTrapFrame {
|
||||
|
||||
pub fn virtual_exception_handler(trapframe: &mut impl TdxTrapFrame, ve_info: &TdgVeInfo) {
|
||||
match ve_info.exit_reason.into() {
|
||||
TdxVirtualExceptionType::Hlt
|
||||
| TdxVirtualExceptionType::Io
|
||||
| TdxVirtualExceptionType::MsrRead
|
||||
| TdxVirtualExceptionType::MsrWrite
|
||||
| TdxVirtualExceptionType::CpuId => tdg_vp_vmcall(trapframe, ve_info),
|
||||
TdxVirtualExceptionType::Hlt => {
|
||||
serial_println!("Ready to halt");
|
||||
hlt();
|
||||
}
|
||||
TdxVirtualExceptionType::Io => {
|
||||
if !handle_io(trapframe, ve_info) {
|
||||
serial_println!("Handle tdx ioexit errors, ready to halt");
|
||||
hlt();
|
||||
}
|
||||
}
|
||||
TdxVirtualExceptionType::MsrRead => {
|
||||
let msr = rdmsr(trapframe.rcx() as u32).unwrap();
|
||||
trapframe.set_rax((msr as u32 & u32::MAX) as usize);
|
||||
trapframe.set_rdx(((msr >> 32) as u32 & u32::MAX) as usize);
|
||||
}
|
||||
TdxVirtualExceptionType::MsrWrite => {
|
||||
let data = trapframe.rax() as u64 | ((trapframe.rdx() as u64) << 32);
|
||||
wrmsr(trapframe.rcx() as u32, data).unwrap();
|
||||
}
|
||||
TdxVirtualExceptionType::CpuId => {
|
||||
let cpuid_info = cpuid(trapframe.rax() as u32, trapframe.rcx() as u32).unwrap();
|
||||
let mask = 0xFFFF_FFFF_0000_0000_usize;
|
||||
trapframe.set_rax((trapframe.rax() & mask) | cpuid_info.eax);
|
||||
trapframe.set_rbx((trapframe.rbx() & mask) | cpuid_info.ebx);
|
||||
trapframe.set_rcx((trapframe.rcx() & mask) | cpuid_info.ecx);
|
||||
trapframe.set_rdx((trapframe.rdx() & mask) | cpuid_info.edx);
|
||||
}
|
||||
TdxVirtualExceptionType::Other => panic!("Unknown TDX vitrual exception type"),
|
||||
_ => return,
|
||||
};
|
||||
}
|
||||
trapframe.set_rip(trapframe.rip() + ve_info.exit_instruction_length as usize);
|
||||
}
|
||||
|
Reference in New Issue
Block a user