Skip to content

Commit c0384d1

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 9eaa3a6 commit c0384d1

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};
@@ -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

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

199212
// Create ACPI tables and write them in guest memory
200213
// For the time being we only support ACPI in x86_64
@@ -209,32 +222,6 @@ pub fn configure_system_for_boot(
209222
Ok(())
210223
}
211224

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

478465
use super::*;
466+
use crate::device_manager::resources::ResourceAllocator;
479467
use crate::test_utils::{arch_mem, single_region_mem};
480468

481469
#[test]
@@ -499,94 +487,35 @@ mod tests {
499487
let no_vcpus = 4;
500488
let gm = single_region_mem(0x10000);
501489
let mut resource_allocator = ResourceAllocator::new().unwrap();
502-
let config_err = configure_system(
503-
&gm,
504-
&mut resource_allocator,
505-
GuestAddress(0),
506-
0,
507-
&None,
508-
1,
509-
BootProtocol::LinuxBoot,
510-
);
490+
let err = mptable::setup_mptable(&gm, &mut resource_allocator, 1);
511491
assert!(matches!(
512-
config_err.unwrap_err(),
513-
super::ConfigurationError::MpTableSetup(mptable::MptableError::NotEnoughMemory)
492+
err.unwrap_err(),
493+
mptable::MptableError::NotEnoughMemory
514494
));
515495

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

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

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

592521
#[test]

0 commit comments

Comments
 (0)