From 1da0ff395d2b25d9452b4edc79f79c171f14654b Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Mon, 11 Nov 2019 14:56:10 +0000 Subject: [PATCH] vmm: cpu: Add the CpuManager onto the IO bus This allows the kernel (via ACPI based controls) to query and control the CPU state. Signed-off-by: Rob Bradford --- vmm/src/cpu.rs | 30 ++++++++++++++++++++++++------ vmm/src/vm.rs | 3 ++- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/vmm/src/cpu.rs b/vmm/src/cpu.rs index 48322767d..30a69eec2 100644 --- a/vmm/src/cpu.rs +++ b/vmm/src/cpu.rs @@ -11,7 +11,7 @@ use std::os::unix::thread::JoinHandleExt; use std::sync::atomic::{AtomicBool, Ordering}; -use std::sync::{Arc, Barrier, Mutex, RwLock}; +use std::sync::{Arc, Barrier, Mutex, RwLock, Weak}; use std::thread; use std::{fmt, io, result}; @@ -19,7 +19,7 @@ use libc::{c_void, siginfo_t}; use crate::device_manager::DeviceManager; -use devices::ioapic; +use devices::{ioapic, BusDevice}; use kvm_ioctls::*; use vm_memory::{Address, GuestAddress, GuestMemoryMmap}; @@ -110,6 +110,9 @@ pub enum Error { /// Failed to join on vCPU threads ThreadCleanup, + + /// Cannot add legacy device to Bus. + BusError(devices::BusError), } pub type Result = result::Result; @@ -329,7 +332,7 @@ impl Vcpu { pub struct CpuManager { boot_vcpus: u8, - io_bus: Arc, + io_bus: Weak, mmio_bus: Arc, ioapic: Option>>, vm_memory: Arc>, @@ -341,6 +344,12 @@ pub struct CpuManager { threads: Vec>, } +impl BusDevice for CpuManager { + fn read(&mut self, _base: u64, _offset: u64, data: &mut [u8]) {} + + fn write(&mut self, _base: u64, _offset: u64, data: &[u8]) {} +} + impl CpuManager { pub fn new( boot_vcpus: u8, @@ -349,10 +358,10 @@ impl CpuManager { fd: Arc, cpuid: CpuId, reset_evt: EventFd, - ) -> Arc> { + ) -> Result>> { let cpu_manager = Arc::new(Mutex::new(CpuManager { boot_vcpus, - io_bus: device_manager.io_bus().clone(), + io_bus: Arc::downgrade(&device_manager.io_bus().clone()), mmio_bus: device_manager.mmio_bus().clone(), ioapic: device_manager.ioapic().clone(), vm_memory: guest_memory, @@ -365,6 +374,15 @@ impl CpuManager { })); cpu_manager + .lock() + .unwrap() + .io_bus + .upgrade() + .unwrap() + .insert(cpu_manager.clone(), 0x0cd8, 0xc) + .map_err(Error::BusError)?; + + Ok(cpu_manager) } // Starts all the vCPUs that the VM is booting with. Blocks until all vCPUs are running. @@ -383,7 +401,7 @@ impl CpuManager { let mut vcpu = Vcpu::new( cpu_id, &self.fd, - self.io_bus.clone(), + self.io_bus.clone().upgrade().unwrap(), self.mmio_bus.clone(), ioapic, creation_ts, diff --git a/vmm/src/vm.rs b/vmm/src/vm.rs index 943465eba..46b84204a 100755 --- a/vmm/src/vm.rs +++ b/vmm/src/vm.rs @@ -428,7 +428,8 @@ impl Vm { fd, cpuid, reset_evt, - ); + ) + .map_err(Error::CpuManager)?; Ok(Vm { kernel,