Skip to content

Commit c98f801

Browse files
ShadowCurseroypat
authored andcommitted
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 4c83038 commit c98f801

File tree

2 files changed

+40
-135
lines changed

2 files changed

+40
-135
lines changed

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

Lines changed: 7 additions & 31 deletions
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

Lines changed: 33 additions & 104 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 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
@@ -205,32 +218,6 @@ pub fn configure_system_for_boot(
205218
Ok(())
206219
}
207220

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

474461
use super::*;
462+
use crate::device_manager::resources::ResourceAllocator;
475463
use crate::test_utils::{arch_mem, single_region_mem};
476464

477465
#[test]
@@ -495,94 +483,35 @@ mod tests {
495483
let no_vcpus = 4;
496484
let gm = single_region_mem(0x10000);
497485
let mut resource_allocator = ResourceAllocator::new().unwrap();
498-
let config_err = configure_system(
499-
&gm,
500-
&mut resource_allocator,
501-
GuestAddress(0),
502-
0,
503-
&None,
504-
1,
505-
BootProtocol::LinuxBoot,
506-
);
486+
let err = mptable::setup_mptable(&gm, &mut resource_allocator, 1);
507487
assert!(matches!(
508-
config_err.unwrap_err(),
509-
super::ConfigurationError::MpTableSetup(mptable::MptableError::NotEnoughMemory)
488+
err.unwrap_err(),
489+
mptable::MptableError::NotEnoughMemory
510490
));
511491

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

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

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

588517
#[test]

0 commit comments

Comments
 (0)