Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions alioth/src/board/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ use crate::arch::layout::{
PCIE_MMIO_32_NON_PREFETCHABLE_START, PCIE_MMIO_32_PREFETCHABLE_END,
PCIE_MMIO_32_PREFETCHABLE_START, RAM_32_SIZE,
};
use crate::device::MmioDev;
#[cfg(target_arch = "x86_64")]
use crate::device::fw_cfg::FwCfg;
use crate::errors::{DebugTrace, trace_error};
use crate::hv::{Coco, Vcpu, Vm, VmEntry, VmExit};
#[cfg(target_arch = "x86_64")]
use crate::loader::xen;
use crate::loader::{Executable, InitState, Payload, linux};
use crate::mem::emulated::Mmio;
use crate::mem::mapped::ArcMemPages;
use crate::mem::{MemBackend, MemConfig, MemRegion, MemRegionType, Memory};
use crate::pci::bus::PciBus;
Expand Down Expand Up @@ -210,9 +210,9 @@ where
pub arch: ArchBoard<V>,
pub config: BoardConfig,
pub payload: RwLock<Option<Payload>>,
pub io_devs: RwLock<Vec<(u16, Arc<dyn Mmio>)>>,
pub io_devs: RwLock<Vec<(u16, Arc<dyn MmioDev>)>>,
#[cfg(target_arch = "aarch64")]
pub mmio_devs: RwLock<Vec<(u64, Arc<MemRegion>)>>,
pub mmio_devs: RwLock<Vec<(u64, Arc<dyn MmioDev>)>>,
pub pci_bus: PciBus,
#[cfg(target_arch = "x86_64")]
pub fw_cfg: Mutex<Option<Arc<Mutex<FwCfg>>>>,
Expand Down Expand Up @@ -419,7 +419,7 @@ where
}
#[cfg(target_arch = "aarch64")]
for (addr, dev) in self.mmio_devs.read().iter() {
self.memory.add_region(*addr, dev.clone())?;
self.memory.add_mmio_dev(*addr, dev.clone())?;
}
self.add_pci_devs()?;
let init_state = self.load_payload()?;
Expand Down
28 changes: 27 additions & 1 deletion alioth/src/device/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use alioth_macros::trace_error;
use snafu::Snafu;

use crate::errors::DebugTrace;
use crate::mem::emulated::Mmio;

pub mod console;
#[cfg(target_arch = "x86_64")]
#[path = "fw_cfg/fw_cfg.rs"]
Expand All @@ -21,6 +27,26 @@ pub mod net;
pub mod pl011;
#[cfg(target_arch = "aarch64")]
pub mod pl031;
pub mod pvpanic;
#[cfg(target_arch = "x86_64")]
pub mod serial;

#[trace_error]
#[derive(Snafu, DebugTrace)]
#[snafu(module, visibility(pub(crate)), context(suffix(false)))]
pub enum Error {
#[snafu(display("Device is not pausable"))]
NotPausable,
}

pub type Result<T, E = Error> = std::result::Result<T, E>;

pub trait Pause {
fn pause(&self) -> Result<()> {
error::NotPausable.fail()
}
fn resume(&self) -> Result<()> {
error::NotPausable.fail()
}
}

pub trait MmioDev: Mmio + Pause {}
13 changes: 13 additions & 0 deletions alioth/src/device/fw_cfg/fw_cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use zerocopy::{FromBytes, Immutable, IntoBytes};
use crate::arch::layout::{
PORT_FW_CFG_DATA, PORT_FW_CFG_DMA_HI, PORT_FW_CFG_DMA_LO, PORT_FW_CFG_SELECTOR,
};
use crate::device::{self, MmioDev, Pause};
#[cfg(target_arch = "x86_64")]
use crate::firmware::acpi::AcpiTable;
#[cfg(target_arch = "x86_64")]
Expand Down Expand Up @@ -512,6 +513,18 @@ impl Mmio for Mutex<FwCfg> {
}
}

impl Pause for Mutex<FwCfg> {
fn pause(&self) -> device::Result<()> {
Ok(())
}

fn resume(&self) -> device::Result<()> {
Ok(())
}
}

impl MmioDev for Mutex<FwCfg> {}

#[derive(Debug, PartialEq, Eq, Deserialize, Help)]
pub enum FwCfgContentParam {
/// Path to a file with binary contents.
Expand Down
16 changes: 16 additions & 0 deletions alioth/src/device/pl011.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use bitflags::bitflags;
use parking_lot::Mutex;

use crate::device::console::{Console, UartRecv};
use crate::device::{self, MmioDev, Pause};
use crate::hv::IrqSender;
use crate::mem::emulated::{Action, Mmio};
use crate::{hv, mem};
Expand Down Expand Up @@ -258,6 +259,21 @@ where
}
}

impl<I> Pause for Pl011<I>
where
I: IrqSender,
{
fn pause(&self) -> device::Result<()> {
Ok(())
}

fn resume(&self) -> device::Result<()> {
Ok(())
}
}

impl<I> MmioDev for Pl011<I> where I: IrqSender {}

struct Pl011Recv<I: IrqSender> {
irq_line: Arc<I>,
reg: Arc<Mutex<Pl011Reg>>,
Expand Down
13 changes: 13 additions & 0 deletions alioth/src/device/pl031.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use std::time::{SystemTime, UNIX_EPOCH};
use bitflags::bitflags;
use parking_lot::Mutex;

use crate::device::{self, MmioDev, Pause};
use crate::mem;
use crate::mem::emulated::{Action, Mmio};

Expand Down Expand Up @@ -141,3 +142,15 @@ impl Mmio for Pl031 {
Ok(Action::None)
}
}

impl Pause for Pl031 {
fn pause(&self) -> device::Result<()> {
Ok(())
}

fn resume(&self) -> device::Result<()> {
Ok(())
}
}

impl MmioDev for Pl031 {}
18 changes: 17 additions & 1 deletion alioth/src/device/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use bitflags::bitflags;
use parking_lot::Mutex;

use crate::device::console::{Console, UartRecv};
use crate::device::{self, MmioDev, Pause};
use crate::hv::IrqSender;
use crate::mem;
use crate::mem::emulated::{Action, Mmio};
Expand Down Expand Up @@ -191,7 +192,7 @@ pub struct Serial<I> {

impl<I> Mmio for Serial<I>
where
I: IrqSender + Sync + Send + 'static,
I: IrqSender,
{
fn size(&self) -> u64 {
8
Expand Down Expand Up @@ -282,6 +283,21 @@ where
}
}

impl<I> Pause for Serial<I>
where
I: IrqSender,
{
fn pause(&self) -> device::Result<()> {
Ok(())
}

fn resume(&self) -> device::Result<()> {
Ok(())
}
}

impl<I> MmioDev for Serial<I> where I: IrqSender {}

struct SerialRecv<I: IrqSender> {
pub name: Arc<str>,
pub irq_sender: Arc<I>,
Expand Down
8 changes: 3 additions & 5 deletions alioth/src/mem/emulated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ pub trait Mmio: Debug + Send + Sync + 'static {
fn size(&self) -> u64;
}

pub type MmioRange = Arc<dyn Mmio>;

impl Mmio for MmioRange {
impl Mmio for Arc<dyn Mmio> {
fn read(&self, offset: u64, size: u8) -> Result<u64> {
Mmio::read(self.as_ref(), offset, size)
}
Expand All @@ -59,7 +57,7 @@ impl Mmio for MmioRange {
}
}

impl SlotBackend for MmioRange {
impl SlotBackend for Arc<dyn Mmio> {
fn size(&self) -> u64 {
Mmio::size(self.as_ref())
}
Expand Down Expand Up @@ -120,7 +118,7 @@ macro_rules! impl_mmio_for_zerocopy {
}

#[derive(Debug)]
pub struct MmioBus<R = MmioRange>
pub struct MmioBus<R = Arc<dyn Mmio>>
where
R: Debug + SlotBackend,
{
Expand Down
23 changes: 14 additions & 9 deletions alioth/src/mem/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use crate::errors::{DebugTrace, trace_error};
use crate::hv::{MemMapOption, VmEntry, VmMemory};

use self::addressable::{Addressable, SlotBackend};
use self::emulated::{Action, MmioBus, MmioRange};
use self::emulated::{Action, Mmio, MmioBus};
use self::mapped::{ArcMemPages, Ram, RamBus};

#[trace_error]
Expand Down Expand Up @@ -125,15 +125,15 @@ impl MemConfig {
pub enum MemRange {
Ram(ArcMemPages),
DevMem(ArcMemPages),
Emulated(MmioRange),
Emulated(Arc<dyn Mmio>),
Span(u64),
}

impl MemRange {
pub fn size(&self) -> u64 {
match self {
MemRange::Ram(pages) | MemRange::DevMem(pages) => pages.size(),
MemRange::Emulated(range) => range.size(),
MemRange::Emulated(range) => Mmio::size(range),
MemRange::Span(size) => *size,
}
}
Expand Down Expand Up @@ -192,8 +192,8 @@ impl MemRegion {
}
}

pub fn with_emulated(range: MmioRange, type_: MemRegionType) -> MemRegion {
let size = range.size();
pub fn with_emulated(range: Arc<dyn Mmio>, type_: MemRegionType) -> MemRegion {
let size = Mmio::size(&range);
MemRegion {
ranges: vec![MemRange::Emulated(range)],
entries: vec![MemRegionEntry { type_, size }],
Expand Down Expand Up @@ -223,12 +223,12 @@ impl SlotBackend for Arc<MemRegion> {

#[derive(Debug)]
pub struct IoRegion {
pub range: MmioRange,
pub range: Arc<dyn Mmio>,
pub callbacks: Mutex<Vec<Box<dyn MemRegionCallback>>>,
}

impl IoRegion {
pub fn new(range: MmioRange) -> IoRegion {
pub fn new(range: Arc<dyn Mmio>) -> IoRegion {
IoRegion {
range,
callbacks: Mutex::new(vec![]),
Expand All @@ -238,7 +238,7 @@ impl IoRegion {

impl SlotBackend for Arc<IoRegion> {
fn size(&self) -> u64 {
self.range.size()
Mmio::size(self.range.as_ref())
}
}

Expand Down Expand Up @@ -339,6 +339,11 @@ impl Memory {
Ok(())
}

pub fn add_mmio_dev(&self, addr: u64, dev: Arc<dyn Mmio>) -> Result<()> {
let region = MemRegion::with_emulated(dev, MemRegionType::Hidden);
self.add_region(addr, Arc::new(region))
}

pub fn add_region(&self, addr: u64, region: Arc<MemRegion>) -> Result<()> {
region.validate()?;
let mut regions = self.regions.lock();
Expand Down Expand Up @@ -460,7 +465,7 @@ impl Memory {
entries
}

pub fn add_io_dev(&self, port: u16, dev: MmioRange) -> Result<()> {
pub fn add_io_dev(&self, port: u16, dev: Arc<dyn Mmio>) -> Result<()> {
self.add_io_region(port, Arc::new(IoRegion::new(dev)))
}

Expand Down
2 changes: 1 addition & 1 deletion alioth/src/pci/bus_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ use std::sync::atomic::AtomicU32;

use assert_matches::assert_matches;

use crate::device::pvpanic::{PVPANIC_DEVICE_ID, PVPANIC_VENDOR_ID, PvPanic};
use crate::mem::emulated::{Action, Mmio};
use crate::pci::Bdf;
use crate::pci::bus::{Address, PciBus, PciIoBus};
use crate::pci::config::{BAR_MEM64, BAR_PREFETCHABLE, CommonHeader, offset_bar};
use crate::pci::pvpanic::{PVPANIC_DEVICE_ID, PVPANIC_VENDOR_ID, PvPanic};
use crate::pci::segment::PciSegment;

#[test]
Expand Down
11 changes: 11 additions & 0 deletions alioth/src/pci/host_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::device::{self, Pause};
use crate::pci;
use crate::pci::cap::PciCapList;
use crate::pci::config::{CommonHeader, DeviceHeader, EmulatedConfig, HeaderType, PciConfig};
Expand Down Expand Up @@ -47,6 +48,16 @@ impl HostBridge {
}
}

impl Pause for HostBridge {
fn pause(&self) -> device::Result<()> {
Ok(())
}

fn resume(&self) -> device::Result<()> {
Ok(())
}
}

impl Pci for HostBridge {
fn name(&self) -> &str {
"host_bridge"
Expand Down
4 changes: 3 additions & 1 deletion alioth/src/pci/pci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub mod bus;
pub mod cap;
pub mod config;
pub mod host_bridge;
pub mod pvpanic;
pub mod segment;

use std::fmt::{Debug, Display, Formatter};
Expand All @@ -24,6 +25,7 @@ use std::sync::Arc;
use bitfield::bitfield;
use snafu::Snafu;

use crate::device::Pause;
use crate::errors::{DebugTrace, trace_error};
use crate::mem::{IoRegion, MemRegion};

Expand Down Expand Up @@ -59,7 +61,7 @@ pub enum Error {

pub type Result<T, E = Error> = std::result::Result<T, E>;

pub trait Pci: Debug + Send + Sync + 'static {
pub trait Pci: Debug + Send + Sync + Pause + 'static {
fn name(&self) -> &str;
fn config(&self) -> &dyn PciConfig;
fn reset(&self) -> Result<()>;
Expand Down
11 changes: 11 additions & 0 deletions alioth/src/device/pvpanic.rs → alioth/src/pci/pvpanic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::sync::Arc;

use bitflags::bitflags;

use crate::device::{self, Pause};
use crate::mem::emulated::{Action, Mmio};
use crate::mem::{self, MemRegion};
use crate::pci::cap::PciCapList;
Expand Down Expand Up @@ -92,6 +93,16 @@ impl Default for PvPanic {
}
}

impl Pause for PvPanic {
fn pause(&self) -> device::Result<()> {
Ok(())
}

fn resume(&self) -> device::Result<()> {
Ok(())
}
}

impl Pci for PvPanic {
fn name(&self) -> &str {
"pvpanic"
Expand Down
Loading