hypervisor: tdx: do not use u64 to represent pointers

Also drop support for building the TDX code for 32-bit targets.  All
CPUs with TDX support are 64-bit so supporting 32-bit targets is not
needed.

Signed-off-by: Demi Marie Obenour <demiobenour@gmail.com>
This commit is contained in:
Demi Marie Obenour
2025-06-26 20:03:30 -04:00
committed by Rob Bradford
parent 42522a88c0
commit 199d2d05d8
3 changed files with 25 additions and 15 deletions

View File

@@ -1001,7 +1001,7 @@ impl vm::Vm for KvmVm {
&self.fd.as_raw_fd(), &self.fd.as_raw_fd(),
TdxCommand::InitVm, TdxCommand::InitVm,
0, 0,
&data as *const _ as u64, &data as *const _ as *const _,
) )
.map_err(vm::HypervisorVmError::InitializeTdx) .map_err(vm::HypervisorVmError::InitializeTdx)
} }
@@ -1011,8 +1011,13 @@ impl vm::Vm for KvmVm {
/// ///
#[cfg(feature = "tdx")] #[cfg(feature = "tdx")]
fn tdx_finalize(&self) -> vm::Result<()> { fn tdx_finalize(&self) -> vm::Result<()> {
tdx_command(&self.fd.as_raw_fd(), TdxCommand::Finalize, 0, 0) tdx_command(
.map_err(vm::HypervisorVmError::FinalizeTdx) &self.fd.as_raw_fd(),
TdxCommand::Finalize,
0,
std::ptr::null(),
)
.map_err(vm::HypervisorVmError::FinalizeTdx)
} }
/// ///
@@ -1021,9 +1026,9 @@ impl vm::Vm for KvmVm {
#[cfg(feature = "tdx")] #[cfg(feature = "tdx")]
fn tdx_init_memory_region( fn tdx_init_memory_region(
&self, &self,
host_address: u64, host_address: *mut u8,
guest_address: u64, guest_address: u64,
size: u64, size: usize,
measure: bool, measure: bool,
) -> vm::Result<()> { ) -> vm::Result<()> {
#[repr(C)] #[repr(C)]
@@ -1033,16 +1038,16 @@ impl vm::Vm for KvmVm {
pages: u64, pages: u64,
} }
let data = TdxInitMemRegion { let data = TdxInitMemRegion {
host_address, host_address: host_address as _,
guest_address, guest_address,
pages: size / 4096, pages: (size / 4096).try_into().unwrap(),
}; };
tdx_command( tdx_command(
&self.fd.as_raw_fd(), &self.fd.as_raw_fd(),
TdxCommand::InitMemRegion, TdxCommand::InitMemRegion,
u32::from(measure), u32::from(measure),
&data as *const _ as u64, &data as *const _ as *const _,
) )
.map_err(vm::HypervisorVmError::InitMemRegionTdx) .map_err(vm::HypervisorVmError::InitMemRegionTdx)
} }
@@ -1058,7 +1063,7 @@ fn tdx_command(
fd: &RawFd, fd: &RawFd,
command: TdxCommand, command: TdxCommand,
flags: u32, flags: u32,
data: u64, data: *const libc::c_void,
) -> std::result::Result<(), std::io::Error> { ) -> std::result::Result<(), std::io::Error> {
#[repr(C)] #[repr(C)]
struct TdxIoctlCmd { struct TdxIoctlCmd {
@@ -1071,7 +1076,7 @@ fn tdx_command(
let cmd = TdxIoctlCmd { let cmd = TdxIoctlCmd {
command, command,
flags, flags,
data, data: data as _,
error: 0, error: 0,
unused: 0, unused: 0,
}; };
@@ -1287,7 +1292,7 @@ impl hypervisor::Hypervisor for KvmHypervisor {
&self.kvm.as_raw_fd(), &self.kvm.as_raw_fd(),
TdxCommand::Capabilities, TdxCommand::Capabilities,
0, 0,
&data as *const _ as u64, &data as *const _ as *const _,
) )
.map_err(|e| hypervisor::HypervisorError::TdxCapabilities(e.into()))?; .map_err(|e| hypervisor::HypervisorError::TdxCapabilities(e.into()))?;
@@ -2648,6 +2653,11 @@ impl cpu::Vcpu for KvmVcpu {
/// ///
#[cfg(feature = "tdx")] #[cfg(feature = "tdx")]
fn tdx_init(&self, hob_address: u64) -> cpu::Result<()> { fn tdx_init(&self, hob_address: u64) -> cpu::Result<()> {
// On 32-bit, the next cast would clobber the high 32 bits.
#[cfg(not(target_pointer_width = "64"))]
compile_error!("32-bit TDX not supported");
let hob_address = hob_address as *const _;
tdx_command(&self.fd.as_raw_fd(), TdxCommand::InitVcpu, 0, hob_address) tdx_command(&self.fd.as_raw_fd(), TdxCommand::InitVcpu, 0, hob_address)
.map_err(cpu::HypervisorCpuError::InitializeTdx) .map_err(cpu::HypervisorCpuError::InitializeTdx)
} }

View File

@@ -403,9 +403,9 @@ pub trait Vm: Send + Sync + Any {
/// Initialize a TDX memory region for this VM /// Initialize a TDX memory region for this VM
fn tdx_init_memory_region( fn tdx_init_memory_region(
&self, &self,
_host_address: u64, _host_address: *mut u8,
_guest_address: u64, _guest_address: u64,
_size: u64, _size: usize,
_measure: bool, _measure: bool,
) -> Result<()> { ) -> Result<()> {
unimplemented!() unimplemented!()

View File

@@ -2251,9 +2251,9 @@ impl Vm {
for section in sections { for section in sections {
self.vm self.vm
.tdx_init_memory_region( .tdx_init_memory_region(
mem.get_host_address(GuestAddress(section.address)).unwrap() as u64, mem.get_host_address(GuestAddress(section.address)).unwrap(),
section.address, section.address,
section.size, section.size.try_into().unwrap(),
/* TDVF_SECTION_ATTRIBUTES_EXTENDMR */ /* TDVF_SECTION_ATTRIBUTES_EXTENDMR */
section.attributes == 1, section.attributes == 1,
) )