Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add QEMU pvpanic ISA device #596

Merged
merged 24 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from 19 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
28 changes: 28 additions & 0 deletions bin/propolis-server/src/lib/initializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use propolis::hw::chipset::Chipset;
use propolis::hw::ibmpc;
use propolis::hw::pci;
use propolis::hw::ps2::ctrl::PS2Ctrl;
use propolis::hw::qemu::pvpanic::QemuPvpanic;
use propolis::hw::qemu::{debug::QemuDebugPort, fwcfg, ramfb};
use propolis::hw::uart::LpcUart;
use propolis::hw::{nvme, virtio};
Expand Down Expand Up @@ -276,6 +277,33 @@ impl<'a> MachineInitializer<'a> {
Ok(())
}

pub fn initialize_qemu_pvpanic(
&self,
uuid: uuid::Uuid,
) -> Result<(), anyhow::Error> {
if let Some(ref spec) = self.spec.devices.qemu_pvpanic {
hawkw marked this conversation as resolved.
Show resolved Hide resolved
if spec.enable_mmio {
hawkw marked this conversation as resolved.
Show resolved Hide resolved
let pvpanic = QemuPvpanic::create(
self.log.new(slog::o!("dev" => "qemu-pvpanic")),
);
pvpanic.attach_pio(&self.machine.bus_pio);
self.inv.register(&pvpanic)?;

if let Some(ref registry) = self.producer_registry {
let producer =
crate::stats::PvpanicProducer::new(uuid, pvpanic);
registry.register_producer(producer).map_err(|error| {
anyhow::anyhow!(
"failed to register PVPANIC Oximeter producer: {error}"
)
})?;
hawkw marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

Ok(())
}

fn create_storage_backend_from_spec(
&self,
backend_spec: &instance_spec::v0::StorageBackendV0,
Expand Down
3 changes: 3 additions & 0 deletions bin/propolis-server/src/lib/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ use uuid::Uuid;

use crate::server::MetricsEndpointConfig;

mod pvpanic;
pub use self::pvpanic::PvpanicProducer;

const OXIMETER_STAT_INTERVAL: tokio::time::Duration =
tokio::time::Duration::from_secs(30);

Expand Down
73 changes: 73 additions & 0 deletions bin/propolis-server/src/lib/stats/pvpanic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use super::InstanceUuid;
hawkw marked this conversation as resolved.
Show resolved Hide resolved
use oximeter::{
types::{Cumulative, Sample},
Metric, MetricsError, Producer,
};
use propolis::hw::qemu::pvpanic;
use std::sync::Arc;
use uuid::Uuid;

#[derive(Clone, Debug)]
pub struct PvpanicProducer {
/// The name to use as the Oximeter target, i.e. the identifier of the
/// source of these metrics.
stat_name: InstanceUuid,

/// Kernel panic counts for the relevant instance.
host_handled_panics: PvPanicHostHandled,
guest_handled_panics: PvPanicGuestHandled,

pvpanic: Arc<pvpanic::QemuPvpanic>,
}

/// An Oximeter `Metric` that specifies the number of times an instance's guest
/// reported a guest-handled kernel panic using the QEMU `pvpanic` device.
#[derive(Debug, Default, Copy, Clone, Metric)]
struct PvPanicGuestHandled {
/// The number of times this instance's guest handled a kernel panic.
#[datum]
pub count: Cumulative<i64>,
}

/// An Oximeter `Metric` that specifies the number of times an instance's guest
/// reported a host-handled kernel panic using the QEMU `pvpanic` device.
#[derive(Debug, Default, Copy, Clone, Metric)]
struct PvPanicHostHandled {
/// The number of times this instance's reported a host-handled kernel panic.
#[datum]
pub count: Cumulative<i64>,
}

impl PvpanicProducer {
pub fn new(id: Uuid, pvpanic: Arc<pvpanic::QemuPvpanic>) -> Self {
PvpanicProducer {
stat_name: InstanceUuid { uuid: id },
host_handled_panics: Default::default(),
guest_handled_panics: Default::default(),
pvpanic,
}
}
}

impl Producer for PvpanicProducer {
fn produce(
&mut self,
) -> Result<Box<dyn Iterator<Item = Sample> + 'static>, MetricsError> {
let pvpanic::PanicCounts { guest_handled, host_handled } =
self.pvpanic.panic_counts();

self.host_handled_panics.datum_mut().set(host_handled as i64);
self.guest_handled_panics.datum_mut().set(guest_handled as i64);

let data = vec![
Sample::new(&self.stat_name, &self.guest_handled_panics)?,
Sample::new(&self.stat_name, &self.host_handled_panics)?,
];

Ok(Box::new(data.into_iter()))
}
}
1 change: 1 addition & 0 deletions bin/propolis-server/src/lib/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ impl VmController {
let ps2ctrl_id = init.initialize_ps2(&chipset)?;
let ps2ctrl: Option<Arc<PS2Ctrl>> = inv.get_concrete(ps2ctrl_id);
init.initialize_qemu_debug_port()?;
init.initialize_qemu_pvpanic(properties.id)?;
init.initialize_network_devices(&chipset)?;
#[cfg(feature = "falcon")]
init.initialize_softnpu_ports(&chipset)?;
Expand Down
17 changes: 16 additions & 1 deletion bin/propolis-standalone/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ use std::time::{SystemTime, UNIX_EPOCH};
use anyhow::Context;
use clap::Parser;
use futures::future::BoxFuture;
use propolis::hw::qemu::pvpanic::QemuPvpanic;
use slog::{o, Drain};
use strum::IntoEnumIterator;
use tokio::runtime;

use propolis::chardev::{BlockingSource, Sink, Source, UDSock};
use propolis::hw::chipset::{i440fx, Chipset};
use propolis::hw::ibmpc;
use propolis::hw::ps2::ctrl::PS2Ctrl;
use propolis::hw::uart::LpcUart;
use propolis::hw::{ibmpc, qemu};
use propolis::intr_pins::FuncPin;
use propolis::usdt::register_probes;
use propolis::vcpu::Vcpu;
Expand Down Expand Up @@ -929,6 +930,20 @@ fn setup_instance(

chipset.pci_attach(bdf, nvme);
}
qemu::pvpanic::DEVICE_NAME => {
let enable_mmio = dev
.options
.get("enable_mmio")
.and_then(|opt| opt.as_bool())
.unwrap_or(false);
if enable_mmio {
let pvpanic = QemuPvpanic::create(
log.new(slog::o!("dev" => "pvpanic")),
);
pvpanic.attach_pio(pio);
inv.register(&pvpanic)?;
}
}
_ => {
slog::error!(log, "unrecognized driver"; "name" => name);
return Err(Error::new(
Expand Down
59 changes: 59 additions & 0 deletions crates/propolis-api-types/src/instance_spec/components/devices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,46 @@ pub enum MigrationCompatibilityError {
ComponentConfiguration(String),
}

#[derive(
Clone,
Copy,
Deserialize,
Serialize,
Debug,
PartialEq,
Eq,
JsonSchema,
Default,
)]
#[serde(deny_unknown_fields)]
pub struct QemuPvpanic {
/// Enable the QEMU PVPANIC ISA bus device (I/O port 0x505).
pub enable_mmio: bool,
// TODO(eliza): add support for the PCI PVPANIC device...
}

impl MigrationElement for Option<QemuPvpanic> {
fn kind(&self) -> &'static str {
"QemuPvpanic"
}

fn can_migrate_from_element(
&self,
other: &Self,
) -> Result<(), crate::instance_spec::migration::ElementCompatibilityError>
{
if self != other {
Err(MigrationCompatibilityError::ComponentConfiguration(format!(
"pvpanic configuration mismatch (self: {0:?}, other: {1:?})",
self, other
))
.into())
} else {
Ok(())
}
}
}

//
// Structs for Falcon devices. These devices don't support live migration.
//
Expand Down Expand Up @@ -385,4 +425,23 @@ mod test {
b2.pci_path = PciPath::new(4, 5, 6).unwrap();
assert!(b1.can_migrate_from_element(&b2).is_err());
}

#[test]
fn incompatible_qemu_pvpanic() {
let d1 = Some(QemuPvpanic { enable_mmio: true });
let d2 = Some(QemuPvpanic { enable_mmio: false });
assert!(d1.can_migrate_from_element(&d2).is_err());
assert!(d1.can_migrate_from_element(&None).is_err());
}

#[test]
fn compatible_qemu_pvpanic() {
let d1 = Some(QemuPvpanic { enable_mmio: true });
let d2 = Some(QemuPvpanic { enable_mmio: true });
assert!(d1.can_migrate_from_element(&d2).is_ok());

let d1 = Some(QemuPvpanic { enable_mmio: false });
let d2 = Some(QemuPvpanic { enable_mmio: false });
assert!(d1.can_migrate_from_element(&d2).is_ok());
}
}
10 changes: 10 additions & 0 deletions crates/propolis-api-types/src/instance_spec/v0/mod.rs
hawkw marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ pub struct DeviceSpecV0 {
pub network_devices: HashMap<SpecKey, NetworkDeviceV0>,
pub serial_ports: HashMap<SpecKey, components::devices::SerialPort>,
pub pci_pci_bridges: HashMap<SpecKey, components::devices::PciPciBridge>,
pub qemu_pvpanic: Option<components::devices::QemuPvpanic>,
hawkw marked this conversation as resolved.
Show resolved Hide resolved

#[cfg(feature = "falcon")]
pub softnpu_pci_port: Option<components::devices::SoftNpuPciPort>,
Expand Down Expand Up @@ -169,6 +170,15 @@ impl DeviceSpecV0 {
)
})?;

self.qemu_pvpanic
.can_migrate_from_element(&other.qemu_pvpanic)
.map_err(|e| {
MigrationCompatibilityError::ElementMismatch(
"QEMU PVPANIC device".to_string(),
e,
)
})?;

Ok(())
}
}
Expand Down
1 change: 1 addition & 0 deletions lib/propolis/src/hw/qemu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

pub mod debug;
pub mod fwcfg;
pub mod pvpanic;
pub mod ramfb;
111 changes: 111 additions & 0 deletions lib/propolis/src/hw/qemu/pvpanic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use std::sync::{Arc, Mutex};

use crate::common::*;
use crate::pio::{PioBus, PioFn};

/// Implements the QEMU [pvpanic device], which
/// may be used by guests to notify the host when a kernel panic has occurred.
///
/// QEMU exposes the pvpanic virtual device as a device on the ISA bus (I/O port
/// 0x505), a PCI device, and through ACPI. Currently, Propolis only implements
/// the ISA bus pvpanic device, but the PCI device may be implemented in the
/// future.
///
/// [pvpanic device]: https://www.qemu.org/docs/master/specs/pvpanic.html
#[derive(Debug)]
pub struct QemuPvpanic {
counts: Mutex<PanicCounts>,
log: slog::Logger,
}

/// Counts the number of guest kernel panics reported using the [`QemuPvpanic`]
/// virtual device.
#[derive(Copy, Clone, Debug)]
pub struct PanicCounts {
/// Counts the number of guest kernel panics handled by the host.
pub host_handled: usize,
/// Counts the number of guest kernel panics handled by the guest.
pub guest_handled: usize,
}

pub const DEVICE_NAME: &str = "qemu-pvpanic";

/// Indicates that a guest panic has happened and should be processed by the
/// host
const HOST_HANDLED: u8 = 0b01;
/// Indicates a guest panic has happened and will be handled by the guest; the
/// host should record it or report it, but should not affect the execution of
/// the guest.
const GUEST_HANDLED: u8 = 0b10;

#[usdt::provider(provider = "propolis")]
mod probes {
fn pvpanic_pio_write(value: u8) {}
}

impl QemuPvpanic {
const IOPORT: u16 = 0x505;

pub fn create(log: slog::Logger) -> Arc<Self> {
Arc::new(Self {
counts: Mutex::new(PanicCounts {
host_handled: 0,
guest_handled: 0,
}),
log,
})
}

/// Attaches this pvpanic device to the provided [`PioBus`].
pub fn attach_pio(self: &Arc<Self>, pio: &PioBus) {
let piodev = self.clone();
let piofn = Arc::new(move |_port: u16, rwo: RWOp| piodev.pio_rw(rwo))
as Arc<PioFn>;
pio.register(Self::IOPORT, 1, piofn).unwrap();
}

/// Returns the current panic counts reported by the guest.
pub fn panic_counts(&self) -> PanicCounts {
*self.counts.lock().unwrap()
}

fn pio_rw(&self, rwo: RWOp) {
match rwo {
RWOp::Read(ro) => {
ro.write_u8(HOST_HANDLED | GUEST_HANDLED);
}
RWOp::Write(wo) => {
let value = wo.read_u8();
probes::pvpanic_pio_write!(|| value);
let host_handled = value & HOST_HANDLED != 0;
let guest_handled = value & GUEST_HANDLED != 0;
slog::debug!(
self.log,
"guest kernel panic";
"host_handled" => host_handled,
"guest_handled" => guest_handled,
);

let mut counts = self.counts.lock().unwrap();

if host_handled {
counts.host_handled += 1;
}

if guest_handled {
counts.guest_handled += 1;
}
}
}
}
}

impl Entity for QemuPvpanic {
fn type_name(&self) -> &'static str {
DEVICE_NAME
}
}
Loading
Loading