@@ -38,6 +38,7 @@ use crate::cpu_config::templates::{
38
38
use crate :: device_manager:: legacy:: PortIODeviceManager ;
39
39
use crate :: device_manager:: mmio:: MMIODeviceManager ;
40
40
use crate :: device_manager:: persist:: MMIODevManagerConstructorArgs ;
41
+ use crate :: device_manager:: resources:: ResourceAllocator ;
41
42
use crate :: devices:: legacy:: serial:: SerialOut ;
42
43
#[ cfg( target_arch = "aarch64" ) ]
43
44
use crate :: devices:: legacy:: RTCDevice ;
@@ -105,13 +106,15 @@ pub enum StartMicrovmError {
105
106
/// Cannot open the block device backing file: {0}
106
107
OpenBlockDevice ( io:: Error ) ,
107
108
/// Cannot initialize a MMIO Device or add a device to the MMIO Bus or cmdline: {0}
108
- RegisterMmioDevice ( device_manager:: mmio:: MmioError ) ,
109
+ RegisterMmioDevice ( # [ from ] device_manager:: mmio:: MmioError ) ,
109
110
/// Cannot restore microvm state: {0}
110
111
RestoreMicrovmState ( MicrovmStateError ) ,
111
112
/// Cannot set vm resources: {0}
112
113
SetVmResources ( VmConfigError ) ,
113
114
/// Cannot create the entropy device: {0}
114
115
CreateEntropyDevice ( crate :: devices:: virtio:: rng:: EntropyError ) ,
116
+ /// Failed to allocate guest resource: {0}
117
+ AllocateResources ( #[ from] vm_allocator:: Error ) ,
115
118
}
116
119
117
120
/// It's convenient to automatically convert `linux_loader::cmdline::Error`s
@@ -147,15 +150,10 @@ fn create_vmm_and_vcpus(
147
150
. map_err ( VmmError :: EventFd )
148
151
. map_err ( Internal ) ?;
149
152
153
+ let resource_allocator = ResourceAllocator :: new ( ) ?;
154
+
150
155
// Instantiate the MMIO device manager.
151
- // 'mmio_base' address has to be an address which is protected by the kernel
152
- // and is architectural specific.
153
- let mmio_device_manager = MMIODeviceManager :: new (
154
- crate :: arch:: MMIO_MEM_START ,
155
- crate :: arch:: MMIO_MEM_SIZE ,
156
- ( crate :: arch:: IRQ_BASE , crate :: arch:: IRQ_MAX ) ,
157
- )
158
- . map_err ( StartMicrovmError :: RegisterMmioDevice ) ?;
156
+ let mmio_device_manager = MMIODeviceManager :: new ( ) ;
159
157
160
158
// For x86_64 we need to create the interrupt controller before calling `KVM_CREATE_VCPUS`
161
159
// while on aarch64 we need to do it the other way around.
@@ -208,6 +206,7 @@ fn create_vmm_and_vcpus(
208
206
uffd,
209
207
vcpus_handles : Vec :: new ( ) ,
210
208
vcpus_exit_evt,
209
+ resource_allocator,
211
210
mmio_device_manager,
212
211
#[ cfg( target_arch = "x86_64" ) ]
213
212
pio_device_manager,
@@ -497,6 +496,7 @@ pub fn build_microvm_from_snapshot(
497
496
mem : guest_memory,
498
497
vm : vmm. vm . fd ( ) ,
499
498
event_manager,
499
+ resource_allocator : & mut vmm. resource_allocator ,
500
500
vm_resources,
501
501
instance_id : & instance_info. id ,
502
502
} ;
@@ -681,7 +681,7 @@ fn attach_legacy_devices_aarch64(
681
681
set_stdout_nonblocking ( ) ;
682
682
let serial = setup_serial_device ( event_manager, std:: io:: stdin ( ) , std:: io:: stdout ( ) ) ?;
683
683
vmm. mmio_device_manager
684
- . register_mmio_serial ( vmm. vm . fd ( ) , serial, None )
684
+ . register_mmio_serial ( vmm. vm . fd ( ) , & mut vmm . resource_allocator , serial, None )
685
685
. map_err ( VmmError :: RegisterMMIODevice ) ?;
686
686
vmm. mmio_device_manager
687
687
. add_mmio_serial_to_cmdline ( cmdline)
@@ -692,7 +692,7 @@ fn attach_legacy_devices_aarch64(
692
692
& crate :: devices:: legacy:: rtc_pl031:: METRICS ,
693
693
) ) ;
694
694
vmm. mmio_device_manager
695
- . register_mmio_rtc ( rtc, None )
695
+ . register_mmio_rtc ( & mut vmm . resource_allocator , rtc, None )
696
696
. map_err ( VmmError :: RegisterMMIODevice )
697
697
}
698
698
@@ -827,7 +827,13 @@ fn attach_virtio_device<T: 'static + VirtioDevice + MutEventSubscriber + Debug>(
827
827
// The device mutex mustn't be locked here otherwise it will deadlock.
828
828
let device = MmioTransport :: new ( vmm. guest_memory ( ) . clone ( ) , device, is_vhost_user) ;
829
829
vmm. mmio_device_manager
830
- . register_mmio_virtio_for_boot ( vmm. vm . fd ( ) , id, device, cmdline)
830
+ . register_mmio_virtio_for_boot (
831
+ vmm. vm . fd ( ) ,
832
+ & mut vmm. resource_allocator ,
833
+ id,
834
+ device,
835
+ cmdline,
836
+ )
831
837
. map_err ( RegisterMmioDevice )
832
838
. map ( |_| ( ) )
833
839
}
@@ -841,7 +847,7 @@ pub(crate) fn attach_boot_timer_device(
841
847
let boot_timer = crate :: devices:: pseudo:: BootTimer :: new ( request_ts) ;
842
848
843
849
vmm. mmio_device_manager
844
- . register_mmio_boot_timer ( boot_timer)
850
+ . register_mmio_boot_timer ( & mut vmm . resource_allocator , boot_timer)
845
851
. map_err ( RegisterMmioDevice ) ?;
846
852
847
853
Ok ( ( ) )
@@ -964,6 +970,7 @@ pub mod tests {
964
970
965
971
use super :: * ;
966
972
use crate :: arch:: DeviceType ;
973
+ use crate :: device_manager:: resources:: ResourceAllocator ;
967
974
use crate :: devices:: virtio:: block:: CacheType ;
968
975
use crate :: devices:: virtio:: rng:: device:: ENTROPY_DEV_ID ;
969
976
use crate :: devices:: virtio:: vsock:: { TYPE_VSOCK , VSOCK_DEV_ID } ;
@@ -1006,15 +1013,6 @@ pub mod tests {
1006
1013
}
1007
1014
}
1008
1015
1009
- fn default_mmio_device_manager ( ) -> MMIODeviceManager {
1010
- MMIODeviceManager :: new (
1011
- crate :: arch:: MMIO_MEM_START ,
1012
- crate :: arch:: MMIO_MEM_SIZE ,
1013
- ( crate :: arch:: IRQ_BASE , crate :: arch:: IRQ_MAX ) ,
1014
- )
1015
- . unwrap ( )
1016
- }
1017
-
1018
1016
fn cmdline_contains ( cmdline : & Cmdline , slug : & str ) -> bool {
1019
1017
// The following unwraps can never fail; the only way any of these methods
1020
1018
// would return an `Err` is if one of the following conditions is met:
@@ -1051,7 +1049,7 @@ pub mod tests {
1051
1049
1052
1050
let mut vm = Vm :: new ( vec ! [ ] ) . unwrap ( ) ;
1053
1051
vm. memory_init ( & guest_memory, false ) . unwrap ( ) ;
1054
- let mmio_device_manager = default_mmio_device_manager ( ) ;
1052
+ let mmio_device_manager = MMIODeviceManager :: new ( ) ;
1055
1053
#[ cfg( target_arch = "x86_64" ) ]
1056
1054
let pio_device_manager = PortIODeviceManager :: new (
1057
1055
Arc :: new ( Mutex :: new ( BusDevice :: Serial ( SerialWrapper {
@@ -1087,6 +1085,7 @@ pub mod tests {
1087
1085
uffd : None ,
1088
1086
vcpus_handles : Vec :: new ( ) ,
1089
1087
vcpus_exit_evt,
1088
+ resource_allocator : ResourceAllocator :: new ( ) . unwrap ( ) ,
1090
1089
mmio_device_manager,
1091
1090
#[ cfg( target_arch = "x86_64" ) ]
1092
1091
pio_device_manager,
0 commit comments