vmm: rearrange order of operations in boot_kernel()
Rearranged operations to group setting up similar resources together.
Some of the reordering is also actually necessary to satisfy some Rust
ownership rules in the following commits.
@@ -432,11 +432,15 @@ impl Vmm {
/// only call this from run_vmm() or other functions
/// that can guarantee single instances
pub fn boot_kernel(&mut self) -> Result<()> {
+ if self.kernel_config.is_none() {
+ return Err(Error::MissingKernelConfig);
+ }
+
let mem_size = self.vm_config.mem_size_mib << 20;
let arch_mem_regions = x86_64::arch_memory_regions(mem_size);
+ let guest_mem = GuestMemory::new(&arch_mem_regions).map_err(Error::GuestMemory)?;
let vcpu_count = self.vm_config.vcpu_count;
- let guest_mem = GuestMemory::new(&arch_mem_regions).map_err(Error::GuestMemory)?;
/* Instantiating MMIO device manager
'mmio_base' address has to be an address which is protected by the kernel, in this case
@@ -449,16 +453,8 @@ impl Vmm {
self.attach_net_devices(&mut device_manager)?;
self.attach_vsock_devices(&guest_mem, &mut device_manager)?;
- let epoll_context = &mut self.epoll_context;
-
- let exit_evt = EventFd::new().map_err(Error::EventFd)?;
- epoll_context.add_event(&exit_evt, EpollDispatch::Exit)?;
- epoll_context.add_event_from_rawfd(libc::STDIN_FILENO, EpollDispatch::Stdin)?;
-
- let kernel_config = match self.kernel_config.as_mut() {
- Some(x) => x,
- None => return Err(Error::MissingKernelConfig),
- };
+ // safe to unwrap since we've already validated it's Some()
+ let kernel_config = self.kernel_config.as_mut().unwrap();
let kvm = Kvm::new().map_err(Error::Kvm)?;
let vm = Vm::new(&kvm, guest_mem).map_err(Error::Vm)?;
@@ -494,6 +490,10 @@ impl Vmm {
vcpu_count,
)?;
+ let exit_evt = EventFd::new().map_err(Error::EventFd)?;
+ self.epoll_context
+ .add_event(&exit_evt, EpollDispatch::Exit)?;
+
let mut io_bus = devices::Bus::new();
let com_evt_1_3 = EventFd::new().map_err(Error::EventFd)?;
let com_evt_2_4 = EventFd::new().map_err(Error::EventFd)?;
@@ -501,6 +501,8 @@ impl Vmm {
com_evt_1_3.try_clone().map_err(Error::EventFd)?,
Box::new(stdout()),
)));
+ self.epoll_context
+ .add_event_from_rawfd(libc::STDIN_FILENO, EpollDispatch::Stdin)?;
//TODO: put all thse things related to setting up io bus in a struct or something
vm.set_io_bus(
@@ -512,9 +514,10 @@ impl Vmm {
).map_err(Error::VmIOBus)?;
let mut vcpu_handles = Vec::with_capacity(vcpu_count as usize);
- let vcpu_thread_barrier = Arc::new(Barrier::new((vcpu_count + 1) as usize));
let kill_signaled = Arc::new(AtomicBool::new(false));
+ let vcpu_thread_barrier = Arc::new(Barrier::new((vcpu_count + 1) as usize));
+
for cpu_id in 0..vcpu_count {
let io_bus = io_bus.clone();
let mmio_bus = device_manager.bus.clone();
