Skip to content

Commit 9e30ad1

Browse files
committed
refactor: inline configure_system into configure_system_for_boot
There is no reason to have these functions separately, so merge them. Signed-off-by: Egor Lazarchuk <[email protected]>
1 parent 8623eaa commit 9e30ad1

File tree

2 files changed

+40
-135
lines changed

2 files changed

+40
-135
lines changed

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

+7-31
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,16 @@ pub mod vcpu;
1717
pub mod vm;
1818

1919
use std::cmp::min;
20-
use std::collections::HashMap;
21-
use std::ffi::CString;
2220
use std::fmt::Debug;
2321
use std::fs::File;
2422

2523
use linux_loader::loader::pe::PE as Loader;
2624
use linux_loader::loader::{Cmdline, KernelLoader};
2725
use vm_memory::GuestMemoryError;
2826

29-
use self::gic::GICDevice;
30-
use crate::arch::{BootProtocol, DeviceType, EntryPoint};
27+
use crate::arch::{BootProtocol, EntryPoint};
3128
use crate::cpu_config::aarch64::{CpuConfiguration, CpuConfigurationError};
3229
use crate::cpu_config::templates::CustomCpuTemplate;
33-
use crate::device_manager::mmio::MMIODeviceInfo;
34-
use crate::devices::acpi::vmgenid::VmGenId;
3530
use crate::initrd::InitrdConfig;
3631
use crate::vmm_config::machine_config::MachineConfig;
3732
use crate::vstate::memory::{Address, Bytes, GuestAddress, GuestMemory, GuestMemoryMmap};
@@ -108,41 +103,22 @@ pub fn configure_system_for_boot(
108103
let cmdline = boot_cmdline
109104
.as_cstring()
110105
.expect("Cannot create cstring from cmdline string");
111-
configure_system(
106+
107+
let fdt = fdt::create_fdt(
112108
&vmm.guest_memory,
113-
cmdline,
114109
vcpu_mpidr,
110+
cmdline,
115111
vmm.mmio_device_manager.get_device_info(),
116112
vmm.vm.get_irqchip(),
117113
&vmm.acpi_device_manager.vmgenid,
118114
initrd,
119115
)?;
120-
Ok(())
121-
}
122116

123-
/// Configures the system and should be called once per vm before starting vcpu threads.
124-
fn configure_system(
125-
guest_mem: &GuestMemoryMmap,
126-
cmdline_cstring: CString,
127-
vcpu_mpidr: Vec<u64>,
128-
device_info: &HashMap<(DeviceType, String), MMIODeviceInfo>,
129-
gic_device: &GICDevice,
130-
vmgenid: &Option<VmGenId>,
131-
initrd: &Option<InitrdConfig>,
132-
) -> Result<(), ConfigurationError> {
133-
let fdt = fdt::create_fdt(
134-
guest_mem,
135-
vcpu_mpidr,
136-
cmdline_cstring,
137-
device_info,
138-
gic_device,
139-
vmgenid,
140-
initrd,
141-
)?;
142-
let fdt_address = GuestAddress(get_fdt_addr(guest_mem));
143-
guest_mem
117+
let fdt_address = GuestAddress(get_fdt_addr(&vmm.guest_memory));
118+
vmm.guest_memory
144119
.write_slice(fdt.as_slice(), fdt_address)
145120
.map_err(ConfigurationError::MemoryError)?;
121+
146122
Ok(())
147123
}
148124

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

+33-104
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub mod generated;
3333

3434
use std::fs::File;
3535

36+
use layout::CMDLINE_START;
3637
use linux_loader::configurator::linux::LinuxBootConfigurator;
3738
use linux_loader::configurator::pvh::PvhBootConfigurator;
3839
use linux_loader::configurator::{BootConfigurator, BootParams};
@@ -49,7 +50,6 @@ use crate::acpi::create_acpi_tables;
4950
use crate::arch::{BootProtocol, SYSTEM_MEM_SIZE, SYSTEM_MEM_START};
5051
use crate::cpu_config::templates::{CustomCpuTemplate, GuestConfigError};
5152
use crate::cpu_config::x86_64::CpuConfiguration;
52-
use crate::device_manager::resources::ResourceAllocator;
5353
use crate::initrd::InitrdConfig;
5454
use crate::utils::{mib_to_bytes, u64_to_usize};
5555
use crate::vmm_config::machine_config::MachineConfig;
@@ -183,15 +183,28 @@ pub fn configure_system_for_boot(
183183
&boot_cmdline,
184184
)
185185
.map_err(ConfigurationError::LoadCommandline)?;
186-
configure_system(
186+
187+
// Note that this puts the mptable at the last 1k of Linux's 640k base RAM
188+
mptable::setup_mptable(
187189
&vmm.guest_memory,
188190
&mut vmm.resource_allocator,
189-
GuestAddress(crate::arch::x86_64::layout::CMDLINE_START),
190-
cmdline_size,
191-
initrd,
192191
vcpu_config.vcpu_count,
193-
entry_point.protocol,
194-
)?;
192+
)
193+
.map_err(ConfigurationError::MpTableSetup)?;
194+
195+
match entry_point.protocol {
196+
BootProtocol::PvhBoot => {
197+
configure_pvh(&vmm.guest_memory, GuestAddress(CMDLINE_START), initrd)?;
198+
}
199+
BootProtocol::LinuxBoot => {
200+
configure_64bit_boot(
201+
&vmm.guest_memory,
202+
GuestAddress(CMDLINE_START),
203+
cmdline_size,
204+
initrd,
205+
)?;
206+
}
207+
}
195208

196209
// Create ACPI tables and write them in guest memory
197210
// For the time being we only support ACPI in x86_64
@@ -206,32 +219,6 @@ pub fn configure_system_for_boot(
206219
Ok(())
207220
}
208221

209-
/// Configures the system and should be called once per vm before starting vcpu threads.
210-
fn configure_system(
211-
guest_mem: &GuestMemoryMmap,
212-
resource_allocator: &mut ResourceAllocator,
213-
cmdline_addr: GuestAddress,
214-
cmdline_size: usize,
215-
initrd: &Option<InitrdConfig>,
216-
num_cpus: u8,
217-
boot_prot: BootProtocol,
218-
) -> Result<(), ConfigurationError> {
219-
// Note that this puts the mptable at the last 1k of Linux's 640k base RAM
220-
mptable::setup_mptable(guest_mem, resource_allocator, num_cpus)
221-
.map_err(ConfigurationError::MpTableSetup)?;
222-
223-
match boot_prot {
224-
BootProtocol::PvhBoot => {
225-
configure_pvh(guest_mem, cmdline_addr, initrd)?;
226-
}
227-
BootProtocol::LinuxBoot => {
228-
configure_64bit_boot(guest_mem, cmdline_addr, cmdline_size, initrd)?;
229-
}
230-
}
231-
232-
Ok(())
233-
}
234-
235222
fn configure_pvh(
236223
guest_mem: &GuestMemoryMmap,
237224
cmdline_addr: GuestAddress,
@@ -473,6 +460,7 @@ mod tests {
473460
use linux_loader::loader::bootparam::boot_e820_entry;
474461

475462
use super::*;
463+
use crate::device_manager::resources::ResourceAllocator;
476464
use crate::test_utils::{arch_mem, single_region_mem};
477465

478466
#[test]
@@ -496,94 +484,35 @@ mod tests {
496484
let no_vcpus = 4;
497485
let gm = single_region_mem(0x10000);
498486
let mut resource_allocator = ResourceAllocator::new().unwrap();
499-
let config_err = configure_system(
500-
&gm,
501-
&mut resource_allocator,
502-
GuestAddress(0),
503-
0,
504-
&None,
505-
1,
506-
BootProtocol::LinuxBoot,
507-
);
487+
let err = mptable::setup_mptable(&gm, &mut resource_allocator, 1);
508488
assert!(matches!(
509-
config_err.unwrap_err(),
510-
super::ConfigurationError::MpTableSetup(mptable::MptableError::NotEnoughMemory)
489+
err.unwrap_err(),
490+
mptable::MptableError::NotEnoughMemory
511491
));
512492

513493
// Now assigning some memory that falls before the 32bit memory hole.
514494
let mem_size = mib_to_bytes(128);
515495
let gm = arch_mem(mem_size);
516496
let mut resource_allocator = ResourceAllocator::new().unwrap();
517-
configure_system(
518-
&gm,
519-
&mut resource_allocator,
520-
GuestAddress(0),
521-
0,
522-
&None,
523-
no_vcpus,
524-
BootProtocol::LinuxBoot,
525-
)
526-
.unwrap();
527-
configure_system(
528-
&gm,
529-
&mut resource_allocator,
530-
GuestAddress(0),
531-
0,
532-
&None,
533-
no_vcpus,
534-
BootProtocol::PvhBoot,
535-
)
536-
.unwrap();
497+
mptable::setup_mptable(&gm, &mut resource_allocator, no_vcpus).unwrap();
498+
configure_64bit_boot(&gm, GuestAddress(0), 0, &None).unwrap();
499+
configure_pvh(&gm, GuestAddress(0), &None).unwrap();
537500

538501
// Now assigning some memory that is equal to the start of the 32bit memory hole.
539502
let mem_size = mib_to_bytes(3328);
540503
let gm = arch_mem(mem_size);
541504
let mut resource_allocator = ResourceAllocator::new().unwrap();
542-
configure_system(
543-
&gm,
544-
&mut resource_allocator,
545-
GuestAddress(0),
546-
0,
547-
&None,
548-
no_vcpus,
549-
BootProtocol::LinuxBoot,
550-
)
551-
.unwrap();
552-
configure_system(
553-
&gm,
554-
&mut resource_allocator,
555-
GuestAddress(0),
556-
0,
557-
&None,
558-
no_vcpus,
559-
BootProtocol::PvhBoot,
560-
)
561-
.unwrap();
505+
mptable::setup_mptable(&gm, &mut resource_allocator, no_vcpus).unwrap();
506+
configure_64bit_boot(&gm, GuestAddress(0), 0, &None).unwrap();
507+
configure_pvh(&gm, GuestAddress(0), &None).unwrap();
562508

563509
// Now assigning some memory that falls after the 32bit memory hole.
564510
let mem_size = mib_to_bytes(3330);
565511
let gm = arch_mem(mem_size);
566512
let mut resource_allocator = ResourceAllocator::new().unwrap();
567-
configure_system(
568-
&gm,
569-
&mut resource_allocator,
570-
GuestAddress(0),
571-
0,
572-
&None,
573-
no_vcpus,
574-
BootProtocol::LinuxBoot,
575-
)
576-
.unwrap();
577-
configure_system(
578-
&gm,
579-
&mut resource_allocator,
580-
GuestAddress(0),
581-
0,
582-
&None,
583-
no_vcpus,
584-
BootProtocol::PvhBoot,
585-
)
586-
.unwrap();
513+
mptable::setup_mptable(&gm, &mut resource_allocator, no_vcpus).unwrap();
514+
configure_64bit_boot(&gm, GuestAddress(0), 0, &None).unwrap();
515+
configure_pvh(&gm, GuestAddress(0), &None).unwrap();
587516
}
588517

589518
#[test]

0 commit comments

Comments
 (0)