|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +use std::sync::{Arc, Mutex}; |
| 6 | + |
| 7 | +use crate::common::*; |
| 8 | +use crate::pio::{PioBus, PioFn}; |
| 9 | + |
| 10 | +/// Implements the QEMU [pvpanic device], which |
| 11 | +/// may be used by guests to notify the host when a kernel panic has occurred. |
| 12 | +/// |
| 13 | +/// QEMU exposes the pvpanic virtual device as a device on the ISA bus (I/O port |
| 14 | +/// 0x505), a PCI device, and through ACPI. Currently, Propolis only implements |
| 15 | +/// the ISA bus pvpanic device, but the PCI device may be implemented in the |
| 16 | +/// future. |
| 17 | +/// |
| 18 | +/// [pvpanic device]: https://www.qemu.org/docs/master/specs/pvpanic.html |
| 19 | +#[derive(Debug)] |
| 20 | +pub struct QemuPvpanic { |
| 21 | + counts: Mutex<PanicCounts>, |
| 22 | + log: slog::Logger, |
| 23 | +} |
| 24 | + |
| 25 | +/// Counts the number of guest kernel panics reported using the [`QemuPvpanic`] |
| 26 | +/// virtual device. |
| 27 | +#[derive(Copy, Clone, Debug)] |
| 28 | +pub struct PanicCounts { |
| 29 | + /// Counts the number of guest kernel panics handled by the host. |
| 30 | + pub host_handled: usize, |
| 31 | + /// Counts the number of guest kernel panics handled by the guest. |
| 32 | + pub guest_handled: usize, |
| 33 | +} |
| 34 | + |
| 35 | +pub const DEVICE_NAME: &str = "qemu-pvpanic"; |
| 36 | + |
| 37 | +/// Indicates that a guest panic has happened and should be processed by the |
| 38 | +/// host |
| 39 | +const HOST_HANDLED: u8 = 0b01; |
| 40 | +/// Indicates a guest panic has happened and will be handled by the guest; the |
| 41 | +/// host should record it or report it, but should not affect the execution of |
| 42 | +/// the guest. |
| 43 | +const GUEST_HANDLED: u8 = 0b10; |
| 44 | + |
| 45 | +#[usdt::provider(provider = "propolis")] |
| 46 | +mod probes { |
| 47 | + fn pvpanic_pio_write(value: u8) {} |
| 48 | +} |
| 49 | + |
| 50 | +impl QemuPvpanic { |
| 51 | + const IOPORT: u16 = 0x505; |
| 52 | + |
| 53 | + pub fn create(log: slog::Logger) -> Arc<Self> { |
| 54 | + Arc::new(Self { |
| 55 | + counts: Mutex::new(PanicCounts { |
| 56 | + host_handled: 0, |
| 57 | + guest_handled: 0, |
| 58 | + }), |
| 59 | + log, |
| 60 | + }) |
| 61 | + } |
| 62 | + |
| 63 | + /// Attaches this pvpanic device to the provided [`PioBus`]. |
| 64 | + pub fn attach_pio(self: &Arc<Self>, pio: &PioBus) { |
| 65 | + let piodev = self.clone(); |
| 66 | + let piofn = Arc::new(move |_port: u16, rwo: RWOp| piodev.pio_rw(rwo)) |
| 67 | + as Arc<PioFn>; |
| 68 | + pio.register(Self::IOPORT, 1, piofn).unwrap(); |
| 69 | + } |
| 70 | + |
| 71 | + /// Returns the current panic counts reported by the guest. |
| 72 | + pub fn panic_counts(&self) -> PanicCounts { |
| 73 | + *self.counts.lock().unwrap() |
| 74 | + } |
| 75 | + |
| 76 | + fn pio_rw(&self, rwo: RWOp) { |
| 77 | + match rwo { |
| 78 | + RWOp::Read(ro) => { |
| 79 | + ro.write_u8(HOST_HANDLED | GUEST_HANDLED); |
| 80 | + } |
| 81 | + RWOp::Write(wo) => { |
| 82 | + let value = wo.read_u8(); |
| 83 | + probes::pvpanic_pio_write!(|| value); |
| 84 | + let host_handled = value & HOST_HANDLED != 0; |
| 85 | + let guest_handled = value & GUEST_HANDLED != 0; |
| 86 | + slog::debug!( |
| 87 | + self.log, |
| 88 | + "guest kernel panic"; |
| 89 | + "host_handled" => host_handled, |
| 90 | + "guest_handled" => guest_handled, |
| 91 | + ); |
| 92 | + |
| 93 | + let mut counts = self.counts.lock().unwrap(); |
| 94 | + |
| 95 | + if host_handled { |
| 96 | + counts.host_handled += 1; |
| 97 | + } |
| 98 | + |
| 99 | + if guest_handled { |
| 100 | + counts.guest_handled += 1; |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +impl Entity for QemuPvpanic { |
| 108 | + fn type_name(&self) -> &'static str { |
| 109 | + DEVICE_NAME |
| 110 | + } |
| 111 | +} |
0 commit comments