Skip to content

Commit d141b9b

Browse files
committed
refactor: allow storing Arc<Vm> within Vmm
We'd like to be able to store Vm within an atomic reference so we can pass it around and share it with other components. The main issue with doing this change is that we need Vm to be `mut` during initialization and the builder.rs code was creating Vmm with Vm embedded in it. To solve this, we break down the initialization of the Vmm object. We first create its individual parts (Vm, Kvm and DeviceManager), perform any necessary initialization logic on Vm and once this done add it within an Arc. Signed-off-by: Babis Chalios <[email protected]>
1 parent f3301b4 commit d141b9b

File tree

8 files changed

+239
-161
lines changed

8 files changed

+239
-161
lines changed

src/vmm/src/arch/aarch64/mod.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use crate::utils::{align_up, u64_to_usize, usize_to_u64};
3232
use crate::vmm_config::machine_config::MachineConfig;
3333
use crate::vstate::memory::{Address, Bytes, GuestAddress, GuestMemory, GuestMemoryMmap};
3434
use crate::vstate::vcpu::KvmVcpuError;
35-
use crate::{Vcpu, VcpuConfig, Vmm, logger};
35+
use crate::{DeviceManager, Kvm, Vcpu, VcpuConfig, Vm, logger};
3636

3737
/// Errors thrown while configuring aarch64 system.
3838
#[derive(Debug, thiserror::Error, displaydoc::Display)]
@@ -82,8 +82,11 @@ pub fn arch_memory_regions(size: usize) -> Vec<(GuestAddress, usize)> {
8282
}
8383

8484
/// Configures the system for booting Linux.
85+
#[allow(clippy::too_many_arguments)]
8586
pub fn configure_system_for_boot(
86-
vmm: &mut Vmm,
87+
kvm: &Kvm,
88+
vm: &Vm,
89+
device_manager: &mut DeviceManager,
8790
vcpus: &mut [Vcpu],
8891
machine_config: &MachineConfig,
8992
cpu_template: &CustomCpuTemplate,
@@ -103,11 +106,11 @@ pub fn configure_system_for_boot(
103106
cpu_config,
104107
};
105108

106-
let optional_capabilities = vmm.kvm.optional_capabilities();
109+
let optional_capabilities = kvm.optional_capabilities();
107110
// Configure vCPUs with normalizing and setting the generated CPU configuration.
108111
for vcpu in vcpus.iter_mut() {
109112
vcpu.kvm_vcpu.configure(
110-
vmm.vm.guest_memory(),
113+
vm.guest_memory(),
111114
entry_point,
112115
&vcpu_config,
113116
&optional_capabilities,
@@ -123,18 +126,16 @@ pub fn configure_system_for_boot(
123126
.expect("Cannot create cstring from cmdline string");
124127

125128
let fdt = fdt::create_fdt(
126-
vmm.vm.guest_memory(),
129+
vm.guest_memory(),
127130
vcpu_mpidr,
128131
cmdline,
129-
&vmm.device_manager,
130-
vmm.vm.get_irqchip(),
132+
device_manager,
133+
vm.get_irqchip(),
131134
initrd,
132135
)?;
133136

134-
let fdt_address = GuestAddress(get_fdt_addr(vmm.vm.guest_memory()));
135-
vmm.vm
136-
.guest_memory()
137-
.write_slice(fdt.as_slice(), fdt_address)?;
137+
let fdt_address = GuestAddress(get_fdt_addr(vm.guest_memory()));
138+
vm.guest_memory().write_slice(fdt.as_slice(), fdt_address)?;
138139

139140
Ok(())
140141
}

src/vmm/src/arch/x86_64/mod.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub mod generated;
3333

3434
use std::fs::File;
3535

36+
use kvm::Kvm;
3637
use layout::{
3738
CMDLINE_START, FIRST_ADDR_PAST_32BITS, FIRST_ADDR_PAST_64BITS_MMIO, MMIO32_MEM_SIZE,
3839
MMIO32_MEM_START, MMIO64_MEM_SIZE, MMIO64_MEM_START, PCI_MMCONFIG_SIZE, PCI_MMCONFIG_START,
@@ -53,14 +54,15 @@ use crate::acpi::create_acpi_tables;
5354
use crate::arch::{BootProtocol, SYSTEM_MEM_SIZE, SYSTEM_MEM_START, arch_memory_regions_with_gap};
5455
use crate::cpu_config::templates::{CustomCpuTemplate, GuestConfigError};
5556
use crate::cpu_config::x86_64::CpuConfiguration;
57+
use crate::device_manager::DeviceManager;
5658
use crate::initrd::InitrdConfig;
5759
use crate::utils::{align_down, u64_to_usize, usize_to_u64};
5860
use crate::vmm_config::machine_config::MachineConfig;
5961
use crate::vstate::memory::{
6062
Address, GuestAddress, GuestMemory, GuestMemoryMmap, GuestMemoryRegion,
6163
};
6264
use crate::vstate::vcpu::KvmVcpuConfigureError;
63-
use crate::{Vcpu, VcpuConfig, Vmm, logger};
65+
use crate::{Vcpu, VcpuConfig, Vm, logger};
6466

6567
// Value taken from https://elixir.bootlin.com/linux/v5.10.68/source/arch/x86/include/uapi/asm/e820.h#L31
6668
// Usable normal RAM
@@ -169,8 +171,11 @@ pub fn initrd_load_addr(guest_mem: &GuestMemoryMmap, initrd_size: usize) -> Opti
169171
}
170172

171173
/// Configures the system for booting Linux.
174+
#[allow(clippy::too_many_arguments)]
172175
pub fn configure_system_for_boot(
173-
vmm: &mut Vmm,
176+
kvm: &Kvm,
177+
vm: &Vm,
178+
device_manager: &mut DeviceManager,
174179
vcpus: &mut [Vcpu],
175180
machine_config: &MachineConfig,
176181
cpu_template: &CustomCpuTemplate,
@@ -179,8 +184,7 @@ pub fn configure_system_for_boot(
179184
boot_cmdline: Cmdline,
180185
) -> Result<(), ConfigurationError> {
181186
// Construct the base CpuConfiguration to apply CPU template onto.
182-
let cpu_config =
183-
CpuConfiguration::new(vmm.kvm.supported_cpuid.clone(), cpu_template, &vcpus[0])?;
187+
let cpu_config = CpuConfiguration::new(kvm.supported_cpuid.clone(), cpu_template, &vcpus[0])?;
184188
// Apply CPU template to the base CpuConfiguration.
185189
let cpu_config = CpuConfiguration::apply_template(cpu_config, cpu_template)?;
186190

@@ -193,7 +197,7 @@ pub fn configure_system_for_boot(
193197
// Configure vCPUs with normalizing and setting the generated CPU configuration.
194198
for vcpu in vcpus.iter_mut() {
195199
vcpu.kvm_vcpu
196-
.configure(vmm.vm.guest_memory(), entry_point, &vcpu_config)?;
200+
.configure(vm.guest_memory(), entry_point, &vcpu_config)?;
197201
}
198202

199203
// Write the kernel command line to guest memory. This is x86_64 specific, since on
@@ -204,27 +208,27 @@ pub fn configure_system_for_boot(
204208
.expect("Cannot create cstring from cmdline string");
205209

206210
load_cmdline(
207-
vmm.vm.guest_memory(),
211+
vm.guest_memory(),
208212
GuestAddress(crate::arch::x86_64::layout::CMDLINE_START),
209213
&boot_cmdline,
210214
)
211215
.map_err(ConfigurationError::LoadCommandline)?;
212216

213217
// Note that this puts the mptable at the last 1k of Linux's 640k base RAM
214218
mptable::setup_mptable(
215-
vmm.vm.guest_memory(),
216-
&vmm.device_manager.resource_allocator,
219+
vm.guest_memory(),
220+
&device_manager.resource_allocator,
217221
vcpu_config.vcpu_count,
218222
)
219223
.map_err(ConfigurationError::MpTableSetup)?;
220224

221225
match entry_point.protocol {
222226
BootProtocol::PvhBoot => {
223-
configure_pvh(vmm.vm.guest_memory(), GuestAddress(CMDLINE_START), initrd)?;
227+
configure_pvh(vm.guest_memory(), GuestAddress(CMDLINE_START), initrd)?;
224228
}
225229
BootProtocol::LinuxBoot => {
226230
configure_64bit_boot(
227-
vmm.vm.guest_memory(),
231+
vm.guest_memory(),
228232
GuestAddress(CMDLINE_START),
229233
cmdline_size,
230234
initrd,
@@ -234,7 +238,7 @@ pub fn configure_system_for_boot(
234238

235239
// Create ACPI tables and write them in guest memory
236240
// For the time being we only support ACPI in x86_64
237-
create_acpi_tables(vmm.vm.guest_memory(), &mut vmm.device_manager, vcpus)?;
241+
create_acpi_tables(vm.guest_memory(), device_manager, vcpus)?;
238242
Ok(())
239243
}
240244

0 commit comments

Comments
 (0)