Skip to content

Commit c523bca

Browse files
committed
feat(tdx): configure required KVM capabilities for TDX VMs
Signed-off-by: Changyuan Lyu <changyuanl@google.com>
1 parent 69202a5 commit c523bca

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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::vm::KvmVm;
17+
use crate::sys::kvm::{KvmCap, KvmHypercall};
18+
19+
impl KvmVm {
20+
pub fn tdx_init(&self) -> Result<()> {
21+
let map_gpa_range = 1 << KvmHypercall::MAP_GPA_RANGE.raw();
22+
self.vm.enable_cap(KvmCap::EXIT_HYPERCALL, map_gpa_range)?;
23+
self.vm.enable_cap(KvmCap::X86_APIC_BUS_CYCLES_NS, 40)?;
24+
Ok(())
25+
}
26+
}

alioth/src/hv/kvm/vm/vm_x86_64/vm_x86_64.rs

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

15-
pub mod sev;
15+
mod sev;
16+
mod tdx;
1617

1718
use std::os::fd::{FromRawFd, OwnedFd};
1819
use std::path::Path;
@@ -58,7 +59,7 @@ impl VmArch {
5859
let fd = SevFd::new(dev_sev)?;
5960
Ok(VmArch { sev_fd: Some(fd) })
6061
}
61-
Coco::IntelTdx { attr } => todo!("Intel TDX {attr:?}"),
62+
Coco::IntelTdx { .. } => Ok(VmArch::default()),
6263
}
6364
}
6465
}
@@ -91,14 +92,6 @@ impl KvmVm {
9192
}
9293

9394
pub fn init(&self, config: &VmConfig) -> Result<()> {
94-
if let Some(coco) = &config.coco {
95-
match coco {
96-
Coco::AmdSev { policy } => self.sev_init(*policy),
97-
Coco::AmdSnp { .. } => self.snp_init(),
98-
Coco::IntelTdx { attr } => todo!("Intel TDX {attr:?}"),
99-
}?;
100-
}
101-
10295
let x2apic_caps =
10396
KvmX2apicApiFlag::USE_32BIT_IDS | KvmX2apicApiFlag::DISABLE_BROADCAST_QUIRK;
10497
if let Err(e) = self.vm.enable_cap(KvmCap::X2APIC_API, x2apic_caps.bits()) {
@@ -109,6 +102,15 @@ impl KvmVm {
109102
unsafe { kvm_set_tss_addr(&self.vm.fd, 0xf000_0000) }.context(error::SetVmParam)?;
110103
unsafe { kvm_set_identity_map_addr(&self.vm.fd, &0xf000_3000) }
111104
.context(error::SetVmParam)?;
105+
106+
if let Some(coco) = &config.coco {
107+
match coco {
108+
Coco::AmdSev { policy } => self.sev_init(*policy),
109+
Coco::AmdSnp { .. } => self.snp_init(),
110+
Coco::IntelTdx { .. } => self.tdx_init(),
111+
}?;
112+
}
113+
112114
Ok(())
113115
}
114116
}

alioth/src/sys/linux/kvm.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,7 @@ consts! {
530530
EXIT_HYPERCALL = 201;
531531
// GUEST_MEMFD = 234;
532532
// VM_TYPES = 235;
533+
X86_APIC_BUS_CYCLES_NS = 237;
533534
}
534535
}
535536

0 commit comments

Comments
 (0)