Skip to content

Commit 5d5f6ea

Browse files
committed
refactor: move KVM_SET_TSS_ADDR call into Vm::arch_create
Since we issue this ioctl with a constant address, it doesn't really matter _when_ we do it. Internally, it causes a hidden memslot of size 3 pages to be created. If we create it _after_ all other memslots, then this ioctl can fail with EEXIST if it would overlap some pre-existing memslot. Now, instead the creation of the overlapping memslot would fail with EEXIST. But this is a moot point, because if Firecracker ever tried to create a memslot that overlaps this one, something is fundamentally broken. Signed-off-by: Patrick Roy <[email protected]>
1 parent 0861696 commit 5d5f6ea

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

src/vmm/src/vstate/vm/aarch64.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4+
use kvm_ioctls::VmFd;
45
use serde::{Deserialize, Serialize};
56

67
use super::Vm;
@@ -26,7 +27,7 @@ pub enum ArchVmError {
2627
}
2728

2829
impl Vm {
29-
pub(super) fn arch_create(_: &Kvm) -> Result<ArchVm, ArchVmError> {
30+
pub(super) fn arch_create(_: &Kvm, _: &VmFd) -> Result<ArchVm, ArchVmError> {
3031
Ok(ArchVm {
3132
irqchip_handle: None,
3233
})

src/vmm/src/vstate/vm/mod.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ use kvm_bindings::{kvm_userspace_memory_region, KVM_MEM_LOG_DIRTY_PAGES};
99
use kvm_ioctls::VmFd;
1010
use vmm_sys_util::eventfd::EventFd;
1111

12-
#[cfg(target_arch = "x86_64")]
13-
use crate::utils::u64_to_usize;
1412
use crate::vstate::kvm::Kvm;
1513
use crate::vstate::memory::{Address, GuestMemory, GuestMemoryMmap, GuestMemoryRegion};
1614

@@ -58,7 +56,7 @@ impl Vm {
5856
/// Create a new `Vm` struct.
5957
pub fn new(kvm: &Kvm) -> Result<Self, VmError> {
6058
let fd = kvm.fd.create_vm().map_err(VmError::VmFd)?;
61-
let arch = Vm::arch_create(kvm)?;
59+
let arch = Vm::arch_create(kvm, &fd)?;
6260

6361
Ok(Vm { fd, arch })
6462
}
@@ -85,13 +83,7 @@ impl Vm {
8583

8684
/// Initializes the guest memory.
8785
pub fn memory_init(&self, guest_mem: &GuestMemoryMmap) -> Result<(), VmError> {
88-
self.set_kvm_memory_regions(guest_mem)?;
89-
#[cfg(target_arch = "x86_64")]
90-
self.fd
91-
.set_tss_address(u64_to_usize(crate::arch::x86_64::layout::KVM_TSS_ADDRESS))
92-
.map_err(VmError::VmSetup)?;
93-
94-
Ok(())
86+
self.set_kvm_memory_regions(guest_mem)
9587
}
9688

9789
pub(crate) fn set_kvm_memory_regions(

src/vmm/src/vstate/vm/x86_64.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ use kvm_bindings::{
77
kvm_clock_data, kvm_irqchip, kvm_pit_config, kvm_pit_state2, MsrList, KVM_CLOCK_TSC_STABLE,
88
KVM_IRQCHIP_IOAPIC, KVM_IRQCHIP_PIC_MASTER, KVM_IRQCHIP_PIC_SLAVE, KVM_PIT_SPEAKER_DUMMY,
99
};
10+
use kvm_ioctls::VmFd;
1011
use serde::{Deserialize, Serialize};
1112

1213
use crate::arch::x86_64::msr::MsrError;
14+
use crate::utils::u64_to_usize;
1315
use crate::vstate::kvm::Kvm;
1416
use crate::Vm;
1517

@@ -42,6 +44,8 @@ pub enum ArchVmError {
4244
VmSetIrqChip(kvm_ioctls::Error),
4345
/// Failed to get MSR index list to save into snapshots: {0}
4446
GetMsrsToSave(MsrError),
47+
/// Failed during KVM_SET_TSS_ADDRESS: {0}
48+
SetTssAddress(kvm_ioctls::Error),
4549
}
4650

4751
/// Structure representing the current architecture's understand of what a "virtual machine" is.
@@ -51,9 +55,12 @@ pub struct ArchVm {
5155
}
5256

5357
impl Vm {
54-
pub(super) fn arch_create(kvm: &Kvm) -> Result<ArchVm, ArchVmError> {
58+
pub(super) fn arch_create(kvm: &Kvm, fd: &VmFd) -> Result<ArchVm, ArchVmError> {
5559
let msrs_to_save = kvm.msrs_to_save().map_err(ArchVmError::GetMsrsToSave)?;
5660

61+
fd.set_tss_address(u64_to_usize(crate::arch::x86_64::layout::KVM_TSS_ADDRESS))
62+
.map_err(ArchVmError::SetTssAddress)?;
63+
5764
Ok(ArchVm { msrs_to_save })
5865
}
5966

0 commit comments

Comments
 (0)