devices: add debug-console device

This commit adds the debug-console (or debugcon) device to CHV. It is a
very simple device on I/O port 0xe9 supported by QEMU and BOCHS. It is
meant for printing information as easy as possible, without any
necessary configuration from the guest at all.

It is primarily interesting to OS/kernel and firmware developers as they
can produce output as soon as the guest starts without any configuration
of a serial device or similar. Furthermore, a kernel hacker might use
this device for information of type B whereas information of type A are
printed to the serial device.

This device is not used by default by Linux, Windows, or any other
"real" OS, but only by toy kernels and during firmware development.

In the CLI, it can be configured similar to --console or --serial with
the --debug-console parameter.

Signed-off-by: Philipp Schuster <philipp.schuster@cyberus-technology.de>
This commit is contained in:
Philipp Schuster 2024-01-17 22:12:43 +01:00 committed by Bo Chen
parent 8f90fba250
commit e50a641126
11 changed files with 429 additions and 14 deletions

View File

@ -0,0 +1,64 @@
// Copyright © 2023 Cyberus Technology
//
// SPDX-License-Identifier: Apache-2.0
//
//! Module for [`DebugconState`].
use std::io;
use std::io::Write;
use std::sync::{Arc, Barrier};
use vm_device::BusDevice;
use vm_migration::{Migratable, MigratableError, Pausable, Snapshot, Snapshottable, Transportable};
/// I/O-port.
pub const DEFAULT_PORT: u64 = 0xe9;
#[derive(Default)]
pub struct DebugconState {}
/// Emulates a debug console similar to the QEMU debugcon device. This device
/// is stateless and only prints the bytes (usually text) that are written to
/// it.
///
/// This device is only available on x86.
///
/// Reference:
/// - https://github.com/qemu/qemu/blob/master/hw/char/debugcon.c
/// - https://phip1611.de/blog/how-to-use-qemus-debugcon-feature-and-write-to-a-file/
pub struct DebugConsole {
id: String,
out: Box<dyn io::Write + Send>,
}
impl DebugConsole {
pub fn new(id: String, out: Box<dyn io::Write + Send>) -> Self {
Self { id, out }
}
}
impl BusDevice for DebugConsole {
fn read(&mut self, _base: u64, _offset: u64, _data: &mut [u8]) {}
fn write(&mut self, _base: u64, _offset: u64, data: &[u8]) -> Option<Arc<Barrier>> {
if let Err(e) = self.out.write_all(data) {
// unlikely
error!("debug-console: failed writing data: {e:?}");
}
None
}
}
impl Snapshottable for DebugConsole {
fn id(&self) -> String {
self.id.clone()
}
fn snapshot(&mut self) -> Result<Snapshot, MigratableError> {
Snapshot::new_from_state(&())
}
}
impl Pausable for DebugConsole {}
impl Transportable for DebugConsole {}
impl Migratable for DebugConsole {}

View File

@ -15,6 +15,8 @@ extern crate event_monitor;
extern crate log; extern crate log;
pub mod acpi; pub mod acpi;
#[cfg(target_arch = "x86_64")]
pub mod debug_console;
#[cfg(target_arch = "aarch64")] #[cfg(target_arch = "aarch64")]
pub mod gic; pub mod gic;
pub mod interrupt_controller; pub mod interrupt_controller;

View File

@ -1,7 +1,16 @@
# `cloud-hypervisor` debug IO port # `cloud-hypervisor` debug IO ports
`cloud-hypervisor` uses the [`0x80`](https://www.intel.com/content/www/us/en/support/articles/000005500/boards-and-kits.html) When running x86 guests, `cloud-hypervisor` provides different kinds of debug ports:
I/O port to trace user defined guest events. - [`0x80` debug port](https://www.intel.com/content/www/us/en/support/articles/000005500/boards-and-kits.html)
- Debug console (by default at `0xe9`).
- Firmware debug port at `0x402`.
All of them can be used to trace user-defined guest events and all of them can
be used simultaneously.
## Debug Ports Overview
### `0x80` I/O port
Whenever the guest write one byte between `0x0` and `0xF` on this particular Whenever the guest write one byte between `0x0` and `0xF` on this particular
I/O port, `cloud-hypervisor` will log and timestamp that event at the `debug` I/O port, `cloud-hypervisor` will log and timestamp that event at the `debug`
@ -30,7 +39,7 @@ guest will have `cloud-hypervisor` generate timestamped logs of all those steps.
That provides a basic but convenient way of measuring not only the overall guest That provides a basic but convenient way of measuring not only the overall guest
boot time but all intermediate steps as well. boot time but all intermediate steps as well.
## Logging #### Logging
Assuming parts of the guest software stack have been instrumented to use the Assuming parts of the guest software stack have been instrumented to use the
`cloud-hypervisor` debug I/O port, we may want to gather the related logs. `cloud-hypervisor` debug I/O port, we may want to gather the related logs.
@ -59,3 +68,29 @@ $ grep "Debug I/O port" /tmp/ch-fw.log
cloud-hypervisor: 19.762449ms: DEBUG:vmm/src/vm.rs:510 -- [Debug I/O port: Firmware code 0x0] 0.019004 seconds cloud-hypervisor: 19.762449ms: DEBUG:vmm/src/vm.rs:510 -- [Debug I/O port: Firmware code 0x0] 0.019004 seconds
cloud-hypervisor: 403.499628ms: DEBUG:vmm/src/vm.rs:510 -- [Debug I/O port: Firmware code 0x1] 0.402744 seconds cloud-hypervisor: 403.499628ms: DEBUG:vmm/src/vm.rs:510 -- [Debug I/O port: Firmware code 0x1] 0.402744 seconds
``` ```
### Debug console port
The debug console is inspired by QEMU and Bochs, which have a similar feature.
By default, the I/O port `0xe9` is used. This port can be configured like a
console. Thus, it can print to a tty, a file, or a pty, for example.
### Firmware debug port
The firmware debug port is also a simple port that prints all bytes written to
it. The firmware debug port only prints to stdout.
## When do I need these ports?
The ports are on the one hand interesting for firmware or kernel developers, as
they provide an easy way to print debug information from within a guest.
Furthermore, you can patch "normal" software to measure certain events, such as
the boot time of a guest.
## Which port should I choose?
The `0x80` debug port and the port of the firmware debug device are always
available. The debug console must be activated via the command line, but
provides more configuration options.
You can use different ports for different aspect of your logging messages.

View File

@ -173,6 +173,8 @@ impl RequestHandler for StubApiRequestHandler {
iommu: false, iommu: false,
socket: None, socket: None,
}, },
#[cfg(target_arch = "x86_64")]
debug_console: DebugConsoleConfig::default(),
devices: None, devices: None,
user_devices: None, user_devices: None,
vdpa: None, vdpa: None,

View File

@ -418,6 +418,15 @@ fn create_app(default_vcpus: String, default_memory: String, default_rng: String
.group("vm-config"), .group("vm-config"),
); );
#[cfg(target_arch = "x86_64")]
let app = app.arg(
Arg::new("debug-console")
.long("debug-console")
.help("Debug console: off|pty|tty|file=</path/to/a/file>,iobase=<port in hex>")
.default_value("off,iobase=0xe9")
.group("vm-config"),
);
#[cfg(feature = "guest_debug")] #[cfg(feature = "guest_debug")]
let app = app.arg( let app = app.arg(
Arg::new("gdb") Arg::new("gdb")
@ -792,6 +801,8 @@ mod unit_tests {
ConsoleConfig, ConsoleOutputMode, CpuFeatures, CpusConfig, MemoryConfig, PayloadConfig, ConsoleConfig, ConsoleOutputMode, CpuFeatures, CpusConfig, MemoryConfig, PayloadConfig,
RngConfig, VmConfig, VmParams, RngConfig, VmConfig, VmParams,
}; };
#[cfg(target_arch = "x86_64")]
use vmm::vm_config::DebugConsoleConfig;
fn get_vm_config_from_vec(args: &[&str]) -> VmConfig { fn get_vm_config_from_vec(args: &[&str]) -> VmConfig {
let (default_vcpus, default_memory, default_rng) = prepare_default_values(); let (default_vcpus, default_memory, default_rng) = prepare_default_values();
@ -881,6 +892,8 @@ mod unit_tests {
iommu: false, iommu: false,
socket: None, socket: None,
}, },
#[cfg(target_arch = "x86_64")]
debug_console: DebugConsoleConfig::default(),
devices: None, devices: None,
user_devices: None, user_devices: None,
vdpa: None, vdpa: None,
@ -1512,6 +1525,30 @@ mod unit_tests {
}); });
} }
#[cfg(target_arch = "x86_64")]
#[test]
fn test_valid_vm_config_debug_console() {
[(
vec![
"cloud-hypervisor",
"--kernel",
"/path/to/kernel",
"--debug-console",
"tty,iobase=0xe9",
],
// 233 == 0xe9
r#"{
"payload": {"kernel": "/path/to/kernel" },
"debug_console": {"mode": "Tty", "iobase": 233 }
}"#,
true,
)]
.iter()
.for_each(|(cli, openapi, equal)| {
compare_vm_config_cli_vs_json(cli, openapi, *equal);
});
}
#[test] #[test]
fn test_valid_vm_config_serial_console() { fn test_valid_vm_config_serial_console() {
[ [

View File

@ -582,6 +582,8 @@ components:
$ref: "#/components/schemas/ConsoleConfig" $ref: "#/components/schemas/ConsoleConfig"
console: console:
$ref: "#/components/schemas/ConsoleConfig" $ref: "#/components/schemas/ConsoleConfig"
debug_console:
$ref: "#/components/schemas/DebugConsoleConfig"
devices: devices:
type: array type: array
items: items:
@ -999,6 +1001,19 @@ components:
type: boolean type: boolean
default: false default: false
DebugConsoleConfig:
required:
- mode
type: object
properties:
file:
type: string
mode:
type: string
enum: ["Off", "Pty", "Tty", "File", "Null"]
iobase:
type: integer
DeviceConfig: DeviceConfig:
required: required:
- path - path

View File

@ -61,6 +61,9 @@ pub enum Error {
ParsePersistentMemory(OptionParserError), ParsePersistentMemory(OptionParserError),
/// Failed parsing console /// Failed parsing console
ParseConsole(OptionParserError), ParseConsole(OptionParserError),
#[cfg(target_arch = "x86_64")]
/// Failed parsing debug-console
ParseDebugConsole(OptionParserError),
/// No mode given for console /// No mode given for console
ParseConsoleInvalidModeGiven, ParseConsoleInvalidModeGiven,
/// Failed parsing device parameters /// Failed parsing device parameters
@ -116,6 +119,9 @@ pub enum ValidationError {
ConsoleSocketPathMissing, ConsoleSocketPathMissing,
/// Max is less than boot /// Max is less than boot
CpusMaxLowerThanBoot, CpusMaxLowerThanBoot,
/// Missing file value for debug-console
#[cfg(target_arch = "x86_64")]
DebugconFileMissing,
/// Both socket and path specified /// Both socket and path specified
DiskSocketAndPath, DiskSocketAndPath,
/// Using vhost user requires shared memory /// Using vhost user requires shared memory
@ -185,6 +191,9 @@ pub enum ValidationError {
DefaultPciSegmentInvalidNode(u32), DefaultPciSegmentInvalidNode(u32),
/// Invalid rate-limiter group /// Invalid rate-limiter group
InvalidRateLimiterGroup, InvalidRateLimiterGroup,
/// The specified I/O port was invalid. It should be provided in hex, such as `0xe9`.
#[cfg(target_arch = "x86_64")]
InvalidIoPortHex(String),
} }
type ValidationResult<T> = std::result::Result<T, ValidationError>; type ValidationResult<T> = std::result::Result<T, ValidationError>;
@ -197,6 +206,8 @@ impl fmt::Display for ValidationError {
ConsoleFileMissing => write!(f, "Path missing when using file console mode"), ConsoleFileMissing => write!(f, "Path missing when using file console mode"),
ConsoleSocketPathMissing => write!(f, "Path missing when using socket console mode"), ConsoleSocketPathMissing => write!(f, "Path missing when using socket console mode"),
CpusMaxLowerThanBoot => write!(f, "Max CPUs lower than boot CPUs"), CpusMaxLowerThanBoot => write!(f, "Max CPUs lower than boot CPUs"),
#[cfg(target_arch = "x86_64")]
DebugconFileMissing => write!(f, "Path missing when using file mode for debug console"),
DiskSocketAndPath => write!(f, "Disk path and vhost socket both provided"), DiskSocketAndPath => write!(f, "Disk path and vhost socket both provided"),
VhostUserRequiresSharedMemory => { VhostUserRequiresSharedMemory => {
write!( write!(
@ -311,6 +322,13 @@ impl fmt::Display for ValidationError {
InvalidRateLimiterGroup => { InvalidRateLimiterGroup => {
write!(f, "Invalid rate-limiter group") write!(f, "Invalid rate-limiter group")
} }
#[cfg(target_arch = "x86_64")]
InvalidIoPortHex(s) => {
write!(
f,
"The IO port was not properly provided in hex or a `0x` prefix is missing: {s}"
)
}
} }
} }
} }
@ -320,6 +338,8 @@ impl fmt::Display for Error {
use self::Error::*; use self::Error::*;
match self { match self {
ParseConsole(o) => write!(f, "Error parsing --console: {o}"), ParseConsole(o) => write!(f, "Error parsing --console: {o}"),
#[cfg(target_arch = "x86_64")]
ParseDebugConsole(o) => write!(f, "Error parsing --debug-console: {o}"),
ParseConsoleInvalidModeGiven => { ParseConsoleInvalidModeGiven => {
write!(f, "Error parsing --console: invalid console mode given") write!(f, "Error parsing --console: invalid console mode given")
} }
@ -399,6 +419,8 @@ pub struct VmParams<'a> {
pub pmem: Option<Vec<&'a str>>, pub pmem: Option<Vec<&'a str>>,
pub serial: &'a str, pub serial: &'a str,
pub console: &'a str, pub console: &'a str,
#[cfg(target_arch = "x86_64")]
pub debug_console: &'a str,
pub devices: Option<Vec<&'a str>>, pub devices: Option<Vec<&'a str>>,
pub user_devices: Option<Vec<&'a str>>, pub user_devices: Option<Vec<&'a str>>,
pub vdpa: Option<Vec<&'a str>>, pub vdpa: Option<Vec<&'a str>>,
@ -440,6 +462,8 @@ impl<'a> VmParams<'a> {
.get_many::<String>("net") .get_many::<String>("net")
.map(|x| x.map(|y| y as &str).collect()); .map(|x| x.map(|y| y as &str).collect());
let console = args.get_one::<String>("console").unwrap(); let console = args.get_one::<String>("console").unwrap();
#[cfg(target_arch = "x86_64")]
let debug_console = args.get_one::<String>("debug-console").unwrap().as_str();
let balloon = args.get_one::<String>("balloon").map(|x| x as &str); let balloon = args.get_one::<String>("balloon").map(|x| x as &str);
let fs: Option<Vec<&str>> = args let fs: Option<Vec<&str>> = args
.get_many::<String>("fs") .get_many::<String>("fs")
@ -489,6 +513,8 @@ impl<'a> VmParams<'a> {
pmem, pmem,
serial, serial,
console, console,
#[cfg(target_arch = "x86_64")]
debug_console,
devices, devices,
user_devices, user_devices,
vdpa, vdpa,
@ -1636,6 +1662,59 @@ impl ConsoleConfig {
} }
} }
#[cfg(target_arch = "x86_64")]
impl DebugConsoleConfig {
pub fn parse(debug_console_ops: &str) -> Result<Self> {
let mut parser = OptionParser::new();
parser
.add_valueless("off")
.add_valueless("pty")
.add_valueless("tty")
.add_valueless("null")
.add("file")
.add("iobase");
parser
.parse(debug_console_ops)
.map_err(Error::ParseConsole)?;
let mut file: Option<PathBuf> = default_consoleconfig_file();
let mut iobase: Option<u16> = None;
let mut mode: ConsoleOutputMode = ConsoleOutputMode::Off;
if parser.is_set("off") {
} else if parser.is_set("pty") {
mode = ConsoleOutputMode::Pty
} else if parser.is_set("tty") {
mode = ConsoleOutputMode::Tty
} else if parser.is_set("null") {
mode = ConsoleOutputMode::Null
} else if parser.is_set("file") {
mode = ConsoleOutputMode::File;
file =
Some(PathBuf::from(parser.get("file").ok_or(
Error::Validation(ValidationError::ConsoleFileMissing),
)?));
} else {
return Err(Error::ParseConsoleInvalidModeGiven);
}
if parser.is_set("iobase") {
if let Some(iobase_opt) = parser.get("iobase") {
if !iobase_opt.starts_with("0x") {
return Err(Error::Validation(ValidationError::InvalidIoPortHex(
iobase_opt,
)));
}
iobase = Some(u16::from_str_radix(&iobase_opt[2..], 16).map_err(|_| {
Error::Validation(ValidationError::InvalidIoPortHex(iobase_opt))
})?);
}
}
Ok(Self { file, mode, iobase })
}
}
impl DeviceConfig { impl DeviceConfig {
pub const SYNTAX: &'static str = pub const SYNTAX: &'static str =
"Direct device assignment parameters \"path=<device_path>,iommu=on|off,id=<device_id>,pci_segment=<segment_id>\""; "Direct device assignment parameters \"path=<device_path>,iommu=on|off,id=<device_id>,pci_segment=<segment_id>\"";
@ -2054,9 +2133,20 @@ impl VmConfig {
// Using such double tty mode, you need to configure the kernel // Using such double tty mode, you need to configure the kernel
// properly, such as: // properly, such as:
// "console=hvc0 earlyprintk=ttyS0" // "console=hvc0 earlyprintk=ttyS0"
if self.console.mode == ConsoleOutputMode::Tty && self.serial.mode == ConsoleOutputMode::Tty
{ let mut tty_consoles = Vec::new();
warn!("Using TTY output for both virtio-console and serial port"); if self.console.mode == ConsoleOutputMode::Tty {
tty_consoles.push("virtio-console");
};
if self.serial.mode == ConsoleOutputMode::Tty {
tty_consoles.push("serial-console");
};
#[cfg(target_arch = "x86_64")]
if self.debug_console.mode == ConsoleOutputMode::Tty {
tty_consoles.push("debug-console");
};
if tty_consoles.len() > 1 {
warn!("Using TTY output for multiple consoles: {:?}", tty_consoles);
} }
if self.console.mode == ConsoleOutputMode::File && self.console.file.is_none() { if self.console.mode == ConsoleOutputMode::File && self.console.file.is_none() {
@ -2375,6 +2465,8 @@ impl VmConfig {
let console = ConsoleConfig::parse(vm_params.console)?; let console = ConsoleConfig::parse(vm_params.console)?;
let serial = ConsoleConfig::parse(vm_params.serial)?; let serial = ConsoleConfig::parse(vm_params.serial)?;
#[cfg(target_arch = "x86_64")]
let debug_console = DebugConsoleConfig::parse(vm_params.debug_console)?;
let mut devices: Option<Vec<DeviceConfig>> = None; let mut devices: Option<Vec<DeviceConfig>> = None;
if let Some(device_list) = &vm_params.devices { if let Some(device_list) = &vm_params.devices {
@ -2482,6 +2574,8 @@ impl VmConfig {
pmem, pmem,
serial, serial,
console, console,
#[cfg(target_arch = "x86_64")]
debug_console,
devices, devices,
user_devices, user_devices,
vdpa, vdpa,
@ -2606,6 +2700,8 @@ impl Clone for VmConfig {
pmem: self.pmem.clone(), pmem: self.pmem.clone(),
serial: self.serial.clone(), serial: self.serial.clone(),
console: self.console.clone(), console: self.console.clone(),
#[cfg(target_arch = "x86_64")]
debug_console: self.debug_console.clone(),
devices: self.devices.clone(), devices: self.devices.clone(),
user_devices: self.user_devices.clone(), user_devices: self.user_devices.clone(),
vdpa: self.vdpa.clone(), vdpa: self.vdpa.clone(),
@ -3341,6 +3437,8 @@ mod tests {
iommu: false, iommu: false,
socket: None, socket: None,
}, },
#[cfg(target_arch = "x86_64")]
debug_console: DebugConsoleConfig::default(),
devices: None, devices: None,
user_devices: None, user_devices: None,
vdpa: None, vdpa: None,

View File

@ -41,14 +41,14 @@ use block::{
}; };
#[cfg(feature = "io_uring")] #[cfg(feature = "io_uring")]
use block::{fixed_vhd_async::FixedVhdDiskAsync, raw_async::RawFileDisk}; use block::{fixed_vhd_async::FixedVhdDiskAsync, raw_async::RawFileDisk};
#[cfg(target_arch = "x86_64")]
use devices::debug_console::DebugConsole;
#[cfg(target_arch = "aarch64")] #[cfg(target_arch = "aarch64")]
use devices::gic; use devices::gic;
#[cfg(target_arch = "x86_64")] #[cfg(target_arch = "x86_64")]
use devices::ioapic; use devices::ioapic;
#[cfg(target_arch = "aarch64")] #[cfg(target_arch = "aarch64")]
use devices::legacy::Pl011; use devices::legacy::Pl011;
#[cfg(target_arch = "x86_64")]
use devices::legacy::Serial;
use devices::{ use devices::{
interrupt_controller, interrupt_controller::InterruptController, AcpiNotificationFlags, interrupt_controller, interrupt_controller::InterruptController, AcpiNotificationFlags,
}; };
@ -103,6 +103,8 @@ use vm_migration::{
use vm_virtio::AccessPlatform; use vm_virtio::AccessPlatform;
use vm_virtio::VirtioDeviceType; use vm_virtio::VirtioDeviceType;
use vmm_sys_util::eventfd::EventFd; use vmm_sys_util::eventfd::EventFd;
#[cfg(target_arch = "x86_64")]
use {devices::debug_console, devices::legacy::Serial};
#[cfg(target_arch = "aarch64")] #[cfg(target_arch = "aarch64")]
const MMIO_LEN: u64 = 0x1000; const MMIO_LEN: u64 = 0x1000;
@ -111,6 +113,8 @@ const MMIO_LEN: u64 = 0x1000;
#[cfg(target_arch = "x86_64")] #[cfg(target_arch = "x86_64")]
const IOAPIC_DEVICE_NAME: &str = "__ioapic"; const IOAPIC_DEVICE_NAME: &str = "__ioapic";
const SERIAL_DEVICE_NAME: &str = "__serial"; const SERIAL_DEVICE_NAME: &str = "__serial";
#[cfg(target_arch = "x86_64")]
const DEBUGCON_DEVICE_NAME: &str = "__debug_console";
#[cfg(target_arch = "aarch64")] #[cfg(target_arch = "aarch64")]
const GPIO_DEVICE_NAME: &str = "__gpio"; const GPIO_DEVICE_NAME: &str = "__gpio";
const RNG_DEVICE_NAME: &str = "__rng"; const RNG_DEVICE_NAME: &str = "__rng";
@ -249,6 +253,10 @@ pub enum DeviceManagerError {
/// Error creating serial output file /// Error creating serial output file
SerialOutputFileOpen(io::Error), SerialOutputFileOpen(io::Error),
#[cfg(target_arch = "x86_64")]
/// Error creating debug-console output file
DebugconOutputFileOpen(io::Error),
/// Error creating console output file /// Error creating console output file
ConsoleOutputFileOpen(io::Error), ConsoleOutputFileOpen(io::Error),
@ -258,6 +266,9 @@ pub enum DeviceManagerError {
/// Error creating console pty /// Error creating console pty
ConsolePtyOpen(io::Error), ConsolePtyOpen(io::Error),
/// Error creating console pty
DebugconPtyOpen(io::Error),
/// Error setting pty raw mode /// Error setting pty raw mode
SetPtyRaw(vmm_sys_util::errno::Error), SetPtyRaw(vmm_sys_util::errno::Error),
@ -819,6 +830,9 @@ pub struct DeviceManager {
// serial PTY // serial PTY
serial_pty: Option<Arc<Mutex<PtyPair>>>, serial_pty: Option<Arc<Mutex<PtyPair>>>,
// debug-console PTY
debug_console_pty: Option<Arc<Mutex<PtyPair>>>,
// Serial Manager // Serial Manager
serial_manager: Option<Arc<SerialManager>>, serial_manager: Option<Arc<SerialManager>>,
@ -1166,6 +1180,7 @@ impl DeviceManager {
serial_pty: None, serial_pty: None,
serial_manager: None, serial_manager: None,
console_pty: None, console_pty: None,
debug_console_pty: None,
console_resize_pipe: None, console_resize_pipe: None,
original_termios_opt: Arc::new(Mutex::new(None)), original_termios_opt: Arc::new(Mutex::new(None)),
virtio_mem_devices: Vec::new(), virtio_mem_devices: Vec::new(),
@ -1209,6 +1224,12 @@ impl DeviceManager {
.map(|pty| pty.lock().unwrap().clone()) .map(|pty| pty.lock().unwrap().clone())
} }
pub fn debug_console_pty(&self) -> Option<PtyPair> {
self.debug_console_pty
.as_ref()
.map(|pty| pty.lock().unwrap().clone())
}
pub fn console_resize_pipe(&self) -> Option<Arc<File>> { pub fn console_resize_pipe(&self) -> Option<Arc<File>> {
self.console_resize_pipe.as_ref().map(Arc::clone) self.console_resize_pipe.as_ref().map(Arc::clone)
} }
@ -1217,6 +1238,7 @@ impl DeviceManager {
&mut self, &mut self,
serial_pty: Option<PtyPair>, serial_pty: Option<PtyPair>,
console_pty: Option<PtyPair>, console_pty: Option<PtyPair>,
debug_console_pty: Option<PtyPair>,
console_resize_pipe: Option<File>, console_resize_pipe: Option<File>,
original_termios_opt: Arc<Mutex<Option<termios>>>, original_termios_opt: Arc<Mutex<Option<termios>>>,
) -> DeviceManagerResult<()> { ) -> DeviceManagerResult<()> {
@ -1276,11 +1298,12 @@ impl DeviceManager {
self.original_termios_opt = original_termios_opt; self.original_termios_opt = original_termios_opt;
self.console = self.add_console_device( self.console = self.add_console_devices(
&legacy_interrupt_manager, &legacy_interrupt_manager,
&mut virtio_devices, &mut virtio_devices,
serial_pty, serial_pty,
console_pty, console_pty,
debug_console_pty,
console_resize_pipe, console_resize_pipe,
)?; )?;
@ -1799,6 +1822,53 @@ impl DeviceManager {
Ok(()) Ok(())
} }
#[cfg(target_arch = "x86_64")]
fn add_debug_console_device(
&mut self,
debug_console_writer: Box<dyn io::Write + Send>,
) -> DeviceManagerResult<Arc<Mutex<DebugConsole>>> {
let id = String::from(DEBUGCON_DEVICE_NAME);
let debug_console = Arc::new(Mutex::new(DebugConsole::new(
id.clone(),
debug_console_writer,
)));
let port = self
.config
.lock()
.unwrap()
.debug_console
.clone()
.iobase
.map(|port| port as u64)
.unwrap_or(debug_console::DEFAULT_PORT);
self.bus_devices
.push(Arc::clone(&debug_console) as Arc<Mutex<dyn BusDevice>>);
self.address_manager
.allocator
.lock()
.unwrap()
.allocate_io_addresses(Some(GuestAddress(port)), 0x1, None)
.ok_or(DeviceManagerError::AllocateIoPort)?;
self.address_manager
.io_bus
.insert(debug_console.clone(), port, 0x1)
.map_err(DeviceManagerError::BusError)?;
// Fill the device tree with a new node. In case of restore, we
// know there is nothing to do, so we can simply override the
// existing entry.
self.device_tree
.lock()
.unwrap()
.insert(id.clone(), device_node!(id, debug_console));
Ok(debug_console)
}
#[cfg(target_arch = "x86_64")] #[cfg(target_arch = "x86_64")]
fn add_serial_device( fn add_serial_device(
&mut self, &mut self,
@ -2086,12 +2156,19 @@ impl DeviceManager {
}) })
} }
fn add_console_device( /// Adds all devices that behave like a console with respect to the VM
/// configuration. This includes:
/// - debug-console
/// - serial-console
/// - virtio-console
fn add_console_devices(
&mut self, &mut self,
interrupt_manager: &Arc<dyn InterruptManager<GroupConfig = LegacyIrqGroupConfig>>, interrupt_manager: &Arc<dyn InterruptManager<GroupConfig = LegacyIrqGroupConfig>>,
virtio_devices: &mut Vec<MetaVirtioDevice>, virtio_devices: &mut Vec<MetaVirtioDevice>,
serial_pty: Option<PtyPair>, serial_pty: Option<PtyPair>,
console_pty: Option<PtyPair>, console_pty: Option<PtyPair>,
#[cfg(target_arch = "x86_64")] debug_console_pty: Option<PtyPair>,
#[cfg(not(target_arch = "x86_64"))] _: Option<PtyPair>,
console_resize_pipe: Option<File>, console_resize_pipe: Option<File>,
) -> DeviceManagerResult<Arc<Console>> { ) -> DeviceManagerResult<Arc<Console>> {
let serial_config = self.config.lock().unwrap().serial.clone(); let serial_config = self.config.lock().unwrap().serial.clone();
@ -2101,7 +2178,7 @@ impl DeviceManager {
.map_err(DeviceManagerError::SerialOutputFileOpen)?, .map_err(DeviceManagerError::SerialOutputFileOpen)?,
)), )),
ConsoleOutputMode::Pty => { ConsoleOutputMode::Pty => {
if let Some(pty) = serial_pty { if let Some(pty) = serial_pty.clone() {
self.config.lock().unwrap().serial.file = Some(pty.path.clone()); self.config.lock().unwrap().serial.file = Some(pty.path.clone());
self.serial_pty = Some(Arc::new(Mutex::new(pty))); self.serial_pty = Some(Arc::new(Mutex::new(pty)));
} else { } else {
@ -2149,6 +2226,44 @@ impl DeviceManager {
}; };
} }
#[cfg(target_arch = "x86_64")]
{
let debug_console_config = self.config.lock().unwrap().debug_console.clone();
let debug_console_writer: Option<Box<dyn io::Write + Send>> = match debug_console_config
.mode
{
ConsoleOutputMode::File => Some(Box::new(
File::create(debug_console_config.file.as_ref().unwrap())
.map_err(DeviceManagerError::DebugconOutputFileOpen)?,
)),
ConsoleOutputMode::Pty => {
if let Some(pty) = debug_console_pty {
self.config.lock().unwrap().debug_console.file = Some(pty.path.clone());
self.debug_console_pty = Some(Arc::new(Mutex::new(pty)));
} else {
let (main, sub, path) =
create_pty().map_err(DeviceManagerError::DebugconPtyOpen)?;
self.set_raw_mode(&sub)
.map_err(DeviceManagerError::SetPtyRaw)?;
self.config.lock().unwrap().debug_console.file = Some(path.clone());
self.debug_console_pty = Some(Arc::new(Mutex::new(PtyPair { main, path })));
}
None
}
ConsoleOutputMode::Tty => {
let out = stdout();
let _ = self.set_raw_mode(&out);
Some(Box::new(out))
}
ConsoleOutputMode::Off | ConsoleOutputMode::Null | ConsoleOutputMode::Socket => {
None
}
};
if let Some(writer) = debug_console_writer {
let _ = self.add_debug_console_device(writer)?;
}
}
let console_resizer = let console_resizer =
self.add_virtio_console_device(virtio_devices, console_pty, console_resize_pipe)?; self.add_virtio_console_device(virtio_devices, console_pty, console_resize_pipe)?;

View File

@ -801,6 +801,7 @@ impl Vmm {
None, None,
None, None,
None, None,
None,
Arc::clone(&self.original_termios_opt), Arc::clone(&self.original_termios_opt),
Some(snapshot), Some(snapshot),
) )
@ -1244,6 +1245,7 @@ impl RequestHandler for Vmm {
None, None,
None, None,
None, None,
None,
Arc::clone(&self.original_termios_opt), Arc::clone(&self.original_termios_opt),
None, None,
None, None,
@ -1343,6 +1345,7 @@ impl RequestHandler for Vmm {
None, None,
None, None,
None, None,
None,
Arc::clone(&self.original_termios_opt), Arc::clone(&self.original_termios_opt),
Some(snapshot), Some(snapshot),
Some(source_url), Some(source_url),
@ -1377,17 +1380,24 @@ impl RequestHandler for Vmm {
fn vm_reboot(&mut self) -> result::Result<(), VmError> { fn vm_reboot(&mut self) -> result::Result<(), VmError> {
// First we stop the current VM // First we stop the current VM
let (config, serial_pty, console_pty, console_resize_pipe) = let (config, serial_pty, console_pty, debug_console_pty, console_resize_pipe) =
if let Some(mut vm) = self.vm.take() { if let Some(mut vm) = self.vm.take() {
let config = vm.get_config(); let config = vm.get_config();
let serial_pty = vm.serial_pty(); let serial_pty = vm.serial_pty();
let console_pty = vm.console_pty(); let console_pty = vm.console_pty();
let debug_console_pty = vm.debug_console_pty();
let console_resize_pipe = vm let console_resize_pipe = vm
.console_resize_pipe() .console_resize_pipe()
.as_ref() .as_ref()
.map(|pipe| pipe.try_clone().unwrap()); .map(|pipe| pipe.try_clone().unwrap());
vm.shutdown()?; vm.shutdown()?;
(config, serial_pty, console_pty, console_resize_pipe) (
config,
serial_pty,
console_pty,
debug_console_pty,
console_resize_pipe,
)
} else { } else {
return Err(VmError::VmNotCreated); return Err(VmError::VmNotCreated);
}; };
@ -1423,6 +1433,7 @@ impl RequestHandler for Vmm {
activate_evt, activate_evt,
serial_pty, serial_pty,
console_pty, console_pty,
debug_console_pty,
console_resize_pipe, console_resize_pipe,
Arc::clone(&self.original_termios_opt), Arc::clone(&self.original_termios_opt),
None, None,
@ -2020,6 +2031,8 @@ const DEVICE_MANAGER_SNAPSHOT_ID: &str = "device-manager";
#[cfg(test)] #[cfg(test)]
mod unit_tests { mod unit_tests {
use super::*; use super::*;
#[cfg(target_arch = "x86_64")]
use crate::config::DebugConsoleConfig;
use config::{ use config::{
ConsoleConfig, ConsoleOutputMode, CpusConfig, HotplugMethod, MemoryConfig, PayloadConfig, ConsoleConfig, ConsoleOutputMode, CpusConfig, HotplugMethod, MemoryConfig, PayloadConfig,
RngConfig, VmConfig, RngConfig, VmConfig,
@ -2094,6 +2107,8 @@ mod unit_tests {
iommu: false, iommu: false,
socket: None, socket: None,
}, },
#[cfg(target_arch = "x86_64")]
debug_console: DebugConsoleConfig::default(),
devices: None, devices: None,
user_devices: None, user_devices: None,
vdpa: None, vdpa: None,

View File

@ -479,6 +479,7 @@ impl Vm {
timestamp: Instant, timestamp: Instant,
serial_pty: Option<PtyPair>, serial_pty: Option<PtyPair>,
console_pty: Option<PtyPair>, console_pty: Option<PtyPair>,
debug_console_pty: Option<PtyPair>,
console_resize_pipe: Option<File>, console_resize_pipe: Option<File>,
original_termios: Arc<Mutex<Option<termios>>>, original_termios: Arc<Mutex<Option<termios>>>,
snapshot: Option<Snapshot>, snapshot: Option<Snapshot>,
@ -631,6 +632,7 @@ impl Vm {
.create_devices( .create_devices(
serial_pty, serial_pty,
console_pty, console_pty,
debug_console_pty,
console_resize_pipe, console_resize_pipe,
original_termios, original_termios,
) )
@ -786,6 +788,7 @@ impl Vm {
activate_evt: EventFd, activate_evt: EventFd,
serial_pty: Option<PtyPair>, serial_pty: Option<PtyPair>,
console_pty: Option<PtyPair>, console_pty: Option<PtyPair>,
debug_console_pty: Option<PtyPair>,
console_resize_pipe: Option<File>, console_resize_pipe: Option<File>,
original_termios: Arc<Mutex<Option<termios>>>, original_termios: Arc<Mutex<Option<termios>>>,
snapshot: Option<Snapshot>, snapshot: Option<Snapshot>,
@ -865,6 +868,7 @@ impl Vm {
timestamp, timestamp,
serial_pty, serial_pty,
console_pty, console_pty,
debug_console_pty,
console_resize_pipe, console_resize_pipe,
original_termios, original_termios,
snapshot, snapshot,
@ -1299,6 +1303,10 @@ impl Vm {
self.device_manager.lock().unwrap().console_pty() self.device_manager.lock().unwrap().console_pty()
} }
pub fn debug_console_pty(&self) -> Option<PtyPair> {
self.device_manager.lock().unwrap().debug_console_pty()
}
pub fn console_resize_pipe(&self) -> Option<Arc<File>> { pub fn console_resize_pipe(&self) -> Option<Arc<File>> {
self.device_manager.lock().unwrap().console_resize_pipe() self.device_manager.lock().unwrap().console_resize_pipe()
} }

View File

@ -396,6 +396,27 @@ pub fn default_consoleconfig_file() -> Option<PathBuf> {
None None
} }
#[cfg(target_arch = "x86_64")]
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct DebugConsoleConfig {
#[serde(default)]
pub file: Option<PathBuf>,
pub mode: ConsoleOutputMode,
/// Optionally dedicated I/O-port, if the default port should not be used.
pub iobase: Option<u16>,
}
#[cfg(target_arch = "x86_64")]
impl Default for DebugConsoleConfig {
fn default() -> Self {
Self {
file: None,
mode: ConsoleOutputMode::Off,
iobase: Some(devices::debug_console::DEFAULT_PORT as u16),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)] #[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct DeviceConfig { pub struct DeviceConfig {
pub path: PathBuf, pub path: PathBuf,
@ -537,6 +558,9 @@ pub struct VmConfig {
pub serial: ConsoleConfig, pub serial: ConsoleConfig,
#[serde(default = "default_console")] #[serde(default = "default_console")]
pub console: ConsoleConfig, pub console: ConsoleConfig,
#[cfg(target_arch = "x86_64")]
#[serde(default)]
pub debug_console: DebugConsoleConfig,
pub devices: Option<Vec<DeviceConfig>>, pub devices: Option<Vec<DeviceConfig>>,
pub user_devices: Option<Vec<UserDeviceConfig>>, pub user_devices: Option<Vec<UserDeviceConfig>>,
pub vdpa: Option<Vec<VdpaConfig>>, pub vdpa: Option<Vec<VdpaConfig>>,