@@ -18,7 +18,6 @@ use crate::virtdev;
1818use crate :: vm;
1919
2020use alloc:: collections:: BTreeMap ;
21- use alloc:: sync:: Arc ;
2221use alloc:: vec:: Vec ;
2322use log:: { debug, info} ;
2423use managed:: ManagedMap ;
@@ -40,11 +39,11 @@ fn build_vm(
4039 cfg : & config:: UserVmConfig ,
4140 info : & BootInfo ,
4241 add_uart : bool ,
43- ) -> Arc < vm:: VirtualMachine > {
42+ ) -> vm:: VirtualMachine {
4443 let physical_config = if add_uart == false {
45- vm:: PhysicalDeviceConfig :: default ( )
44+ vm:: HostPhysicalDevices :: default ( )
4645 } else {
47- vm:: PhysicalDeviceConfig {
46+ vm:: HostPhysicalDevices {
4847 serial : RwLock :: new ( Some (
4948 physdev:: com:: Uart8250 :: new ( 0x3f8 )
5049 . expect ( "Failed to create UART" ) ,
@@ -83,47 +82,18 @@ fn build_vm(
8382
8483 acpi. add_sdt ( madt) . unwrap ( ) ;
8584
86- let device_map = config. virtual_devices_mut ( ) ;
85+ let virtual_devices = & mut config. virtual_devices ;
8786
88- device_map
89- . register_device ( virtdev:: acpi:: AcpiRuntime :: new ( 0x600 ) . unwrap ( ) )
90- . unwrap ( ) ;
91- device_map
92- . register_device ( virtdev:: debug:: DebugPort :: new ( 0x402 ) )
93- . unwrap ( ) ;
94- device_map
95- . register_device ( virtdev:: com:: Uart8250 :: new ( 0x3F8 ) )
96- . unwrap ( ) ;
97- device_map
98- . register_device ( virtdev:: vga:: VgaController :: new ( ) )
99- . unwrap ( ) ;
100- device_map
101- . register_device ( virtdev:: dma:: Dma8237 :: new ( ) )
102- . unwrap ( ) ;
103- device_map
104- . register_device ( virtdev:: ignore:: IgnoredDevice :: new ( ) )
105- . unwrap ( ) ;
106- device_map
107- . register_device ( virtdev:: pci:: PciRootComplex :: new ( ) )
108- . unwrap ( ) ;
109- device_map
110- . register_device ( virtdev:: pic:: Pic8259 :: new ( ) )
111- . unwrap ( ) ;
112- device_map
113- . register_device ( virtdev:: keyboard:: Keyboard8042 :: new ( ) )
114- . unwrap ( ) ;
115- device_map
116- . register_device ( virtdev:: pit:: Pit8254 :: new ( ) )
117- . unwrap ( ) ;
118- device_map
119- . register_device ( virtdev:: pos:: ProgrammableOptionSelect :: new ( ) )
120- . unwrap ( ) ;
121- device_map
122- . register_device ( virtdev:: rtc:: CmosRtc :: new ( cfg. memory ) )
123- . unwrap ( ) ;
124- device_map
125- . register_device ( virtdev:: ioapic:: IoApic :: new ( ) )
126- . unwrap ( ) ;
87+ virtual_devices. push ( RwLock :: new (
88+ virtdev:: DynamicVirtualDevice :: DebugPort (
89+ virtdev:: debug:: DebugPort :: new ( 0x402 )
90+ . expect ( "Failed to make DebugPort" ) ,
91+ ) ,
92+ ) ) ;
93+
94+ virtual_devices. push ( RwLock :: new ( virtdev:: DynamicVirtualDevice :: Uart (
95+ virtdev:: com:: Uart8250 :: new ( 0x3F8 ) . expect ( "Failed to make Uart" ) ,
96+ ) ) ) ;
12797
12898 let mut fw_cfg_builder = virtdev:: qemu_fw_cfg:: QemuFwCfgBuilder :: new ( ) ;
12999
@@ -155,7 +125,9 @@ fn build_vm(
155125 )
156126 . unwrap ( ) ;
157127
158- device_map. register_device ( fw_cfg_builder. build ( ) ) . unwrap ( ) ;
128+ virtual_devices. push ( RwLock :: new ( virtdev:: DynamicVirtualDevice :: Qemu (
129+ fw_cfg_builder. build ( ) . expect ( "Failed to build FW Cfg" ) ,
130+ ) ) ) ;
159131
160132 vm:: VirtualMachine :: new ( vm_id, config, info) . expect ( "Failed to create vm" )
161133}
@@ -251,8 +223,6 @@ unsafe fn kmain(mut boot_info: BootInfo) -> ! {
251223 ioapic:: map_gsi_vector ( interrupt:: gsi:: UART , interrupt:: vector:: UART , 0 )
252224 . expect ( "Failed to map com0 gsi" ) ;
253225
254- let mut builder = vm:: VirtualMachineSetBuilder :: new ( ) ;
255-
256226 let raw_cfg = boot_info
257227 . find_module ( "mythril.cfg" )
258228 . expect ( "Failed to find 'mythril.cfg' in boot information" )
@@ -263,13 +233,15 @@ unsafe fn kmain(mut boot_info: BootInfo) -> ! {
263233
264234 debug ! ( "mythril.cfg: {:?}" , mythril_cfg) ;
265235
266- for ( num, vm) in mythril_cfg. vms . into_iter ( ) . enumerate ( ) {
267- builder
268- . insert_machine ( build_vm ( num as u32 , & vm, & boot_info, num == 0 ) )
269- . expect ( "Failed to insert new vm" ) ;
270- }
271-
272- vm:: init_virtual_machines ( builder. finalize ( ) ) ;
236+ let vms = mythril_cfg
237+ . vms
238+ . into_iter ( )
239+ . enumerate ( )
240+ . map ( |( num, vm_cfg) | {
241+ build_vm ( num as u32 , & vm_cfg, & boot_info, num == 0 )
242+ } ) ;
243+ vm:: init_virtual_machines ( vms)
244+ . expect ( "Failed to initialize early virtual machine state" ) ;
273245
274246 debug ! ( "AP_STARTUP address: 0x{:x}" , AP_STARTUP_ADDR ) ;
275247
@@ -332,5 +304,17 @@ unsafe fn kmain(mut boot_info: BootInfo) -> ! {
332304 core:: ptr:: write_volatile ( & mut AP_READY as * mut u8 , 0 ) ;
333305 }
334306
307+ // Also don't start the bsp core if there is no guest assigned to use it
308+ let bsp_core_id = percore:: read_core_id ( ) ;
309+ if !vm:: virtual_machines ( ) . is_assigned_core_id ( bsp_core_id) {
310+ debug ! (
311+ "Not starting core ID '{}' because it is not assigned to a guest" ,
312+ bsp_core_id
313+ ) ;
314+ loop {
315+ crate :: lock:: relax_cpu ( ) ;
316+ }
317+ }
318+
335319 vcpu:: mp_entry_point ( )
336320}
0 commit comments