Skip to content

Commit 10d1896

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 e8309ab commit 10d1896

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};
@@ -109,41 +104,22 @@ pub fn configure_system_for_boot(
109104
let cmdline = boot_cmdline
110105
.as_cstring()
111106
.expect("Cannot create cstring from cmdline string");
112-
configure_system(
107+
108+
let fdt = fdt::create_fdt(
113109
&vmm.guest_memory,
114-
cmdline,
115110
vcpu_mpidr,
111+
cmdline,
116112
vmm.mmio_device_manager.get_device_info(),
117113
vmm.vm.get_irqchip(),
118114
&vmm.acpi_device_manager.vmgenid,
119115
initrd,
120116
)?;
121-
Ok(())
122-
}
123117

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

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;
@@ -184,15 +184,28 @@ pub fn configure_system_for_boot(
184184
&boot_cmdline,
185185
)
186186
.map_err(ConfigurationError::LoadCommandline)?;
187-
configure_system(
187+
188+
// Note that this puts the mptable at the last 1k of Linux's 640k base RAM
189+
mptable::setup_mptable(
188190
&vmm.guest_memory,
189191
&mut vmm.resource_allocator,
190-
GuestAddress(crate::arch::x86_64::layout::CMDLINE_START),
191-
cmdline_size,
192-
initrd,
193192
vcpu_config.vcpu_count,
194-
entry_point.protocol,
195-
)?;
193+
)
194+
.map_err(ConfigurationError::MpTableSetup)?;
195+
196+
match entry_point.protocol {
197+
BootProtocol::PvhBoot => {
198+
configure_pvh(&vmm.guest_memory, GuestAddress(CMDLINE_START), initrd)?;
199+
}
200+
BootProtocol::LinuxBoot => {
201+
configure_64bit_boot(
202+
&vmm.guest_memory,
203+
GuestAddress(CMDLINE_START),
204+
cmdline_size,
205+
initrd,
206+
)?;
207+
}
208+
}
196209

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

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

476463
use super::*;
464+
use crate::device_manager::resources::ResourceAllocator;
477465
use crate::test_utils::{arch_mem, single_region_mem};
478466

479467
#[test]
@@ -497,94 +485,35 @@ mod tests {
497485
let no_vcpus = 4;
498486
let gm = single_region_mem(0x10000);
499487
let mut resource_allocator = ResourceAllocator::new().unwrap();
500-
let config_err = configure_system(
501-
&gm,
502-
&mut resource_allocator,
503-
GuestAddress(0),
504-
0,
505-
&None,
506-
1,
507-
BootProtocol::LinuxBoot,
508-
);
488+
let err = mptable::setup_mptable(&gm, &mut resource_allocator, 1);
509489
assert!(matches!(
510-
config_err.unwrap_err(),
511-
super::ConfigurationError::MpTableSetup(mptable::MptableError::NotEnoughMemory)
490+
err.unwrap_err(),
491+
mptable::MptableError::NotEnoughMemory
512492
));
513493

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

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

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

590519
#[test]

0 commit comments

Comments
 (0)