Skip to content

Commit 19ea039

Browse files
committed
feat(hv): add tdx_init_mem_region for KvmVcpu
Signed-off-by: Changyuan Lyu <changyuanl@google.com>
1 parent 5070eff commit 19ea039

File tree

7 files changed

+90
-1
lines changed

7 files changed

+90
-1
lines changed

alioth/src/hv/hv.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ pub enum Error {
111111
#[cfg(target_arch = "x86_64")]
112112
#[snafu(display("SEV command error code {code:#x?}"))]
113113
SevErr { code: SevStatus },
114+
#[cfg(target_arch = "x86_64")]
115+
#[snafu(display("TDX command error code {code:#x?}"))]
116+
TdxErr { code: u64 },
114117
}
115118

116119
impl From<std::sync::mpsc::RecvError> for Error {
@@ -214,6 +217,9 @@ pub trait Vcpu {
214217
let pc = self.get_reg(Reg::Pc)?;
215218
self.set_regs(&[(Reg::Pc, pc + 4)])
216219
}
220+
221+
#[cfg(target_arch = "x86_64")]
222+
fn tdx_init_mem_region(&self, data: &[u8], gpa: u64, measure: bool) -> Result<()>;
217223
}
218224

219225
#[cfg(not(target_arch = "x86_64"))]

alioth/src/hv/kvm/kvm.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ mod x86_64;
2020
mod device;
2121
#[cfg(target_arch = "x86_64")]
2222
mod sev;
23+
#[cfg(target_arch = "x86_64")]
24+
mod tdx;
2325
#[path = "vcpu/vcpu.rs"]
2426
mod vcpu;
2527
#[path = "vm/vm.rs"]

alioth/src/hv/kvm/tdx.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2026 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use std::os::fd::OwnedFd;
16+
17+
use snafu::ResultExt;
18+
19+
use crate::hv::{Result, error};
20+
use crate::sys::kvm::kvm_memory_encrypt_op;
21+
use crate::sys::tdx::{KvmTdxCmd, KvmTdxCmdId};
22+
23+
pub fn tdx_op<T>(fd: &OwnedFd, cmd: KvmTdxCmdId, flags: u32, data: Option<&mut T>) -> Result<()> {
24+
let mut req = KvmTdxCmd {
25+
id: cmd,
26+
flags,
27+
data: data.map(|d| d as *mut _ as _).unwrap_or(0),
28+
hw_error: 0,
29+
};
30+
unsafe { kvm_memory_encrypt_op(fd, &mut req) }.context(error::MemEncrypt)?;
31+
if req.hw_error != 0 {
32+
return error::TdxErr { code: req.hw_error }.fail();
33+
}
34+
Ok(())
35+
}

alioth/src/hv/kvm/vcpu/vcpu.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#[path = "vcpu_aarch64.rs"]
1717
mod aarch64;
1818
#[cfg(target_arch = "x86_64")]
19-
#[path = "vcpu_x86_64.rs"]
19+
#[path = "vcpu_x86_64/vcpu_x86_64.rs"]
2020
mod x86_64;
2121

2222
mod vmentry;
@@ -232,4 +232,9 @@ impl Vcpu for KvmVcpu {
232232
fn dump(&self) -> Result<(), Error> {
233233
Ok(())
234234
}
235+
236+
#[cfg(target_arch = "x86_64")]
237+
fn tdx_init_mem_region(&self, data: &[u8], gpa: u64, measure: bool) -> Result<()> {
238+
KvmVcpu::tdx_init_mem_region(self, data, gpa, measure)
239+
}
235240
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2026 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use crate::hv::Result;
16+
use crate::hv::kvm::tdx::tdx_op;
17+
use crate::hv::kvm::vcpu::KvmVcpu;
18+
use crate::sys::tdx::{KvmTdxCmdId, KvmTdxInitMemRegion, KvmTdxInitMemRegionFlag};
19+
20+
impl KvmVcpu {
21+
pub fn tdx_init_mem_region(&self, data: &[u8], gpa: u64, measure: bool) -> Result<()> {
22+
let mut region = KvmTdxInitMemRegion {
23+
source_addr: data.as_ptr() as u64,
24+
nr_pages: data.len() as u64 >> 12,
25+
gpa,
26+
};
27+
let flag = if measure {
28+
KvmTdxInitMemRegionFlag::MEASURE_MEMORY_REGION
29+
} else {
30+
KvmTdxInitMemRegionFlag::empty()
31+
};
32+
tdx_op(
33+
&self.fd,
34+
KvmTdxCmdId::INIT_MEM_REGION,
35+
flag.bits(),
36+
Some(&mut region),
37+
)
38+
}
39+
}

alioth/src/hv/kvm/vcpu/vcpu_x86_64.rs renamed to alioth/src/hv/kvm/vcpu/vcpu_x86_64/vcpu_x86_64.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
mod tdx;
16+
1517
use std::arch::x86_64::CpuidResult;
1618
use std::collections::HashMap;
1719
use std::iter::zip;
File renamed without changes.

0 commit comments

Comments
 (0)