/src/cloud-hypervisor/devices/src/debug_console.rs
Line | Count | Source |
1 | | // Copyright © 2023 Cyberus Technology |
2 | | // |
3 | | // SPDX-License-Identifier: Apache-2.0 |
4 | | // |
5 | | |
6 | | //! Module for [`DebugconState`]. |
7 | | |
8 | | use std::io; |
9 | | use std::io::Write; |
10 | | use std::sync::{Arc, Barrier}; |
11 | | |
12 | | use log::error; |
13 | | use vm_device::BusDevice; |
14 | | use vm_migration::{Migratable, MigratableError, Pausable, Snapshot, Snapshottable, Transportable}; |
15 | | |
16 | | /// I/O-port. |
17 | | pub const DEFAULT_PORT: u64 = 0xe9; |
18 | | |
19 | | #[derive(Default)] |
20 | | pub struct DebugconState {} |
21 | | |
22 | | /// Emulates a debug console similar to the QEMU debugcon device. This device |
23 | | /// is stateless and only prints the bytes (usually text) that are written to |
24 | | /// it. |
25 | | /// |
26 | | /// This device is only available on x86. |
27 | | /// |
28 | | /// Reference: |
29 | | /// - https://github.com/qemu/qemu/blob/master/hw/char/debugcon.c |
30 | | /// - https://phip1611.de/blog/how-to-use-qemus-debugcon-feature-and-write-to-a-file/ |
31 | | pub struct DebugConsole { |
32 | | id: String, |
33 | | out: Box<dyn io::Write + Send>, |
34 | | } |
35 | | |
36 | | impl DebugConsole { |
37 | 0 | pub fn new(id: String, out: Box<dyn io::Write + Send>) -> Self { |
38 | 0 | Self { id, out } |
39 | 0 | } |
40 | | } |
41 | | |
42 | | impl BusDevice for DebugConsole { |
43 | 0 | fn read(&mut self, _base: u64, _offset: u64, _data: &mut [u8]) {} |
44 | | |
45 | 0 | fn write(&mut self, _base: u64, _offset: u64, data: &[u8]) -> Option<Arc<Barrier>> { |
46 | 0 | if let Err(e) = self.out.write_all(data) { |
47 | | // unlikely |
48 | 0 | error!("debug-console: failed writing data: {e:?}"); |
49 | 0 | } |
50 | 0 | None |
51 | 0 | } |
52 | | } |
53 | | |
54 | | impl Snapshottable for DebugConsole { |
55 | 0 | fn id(&self) -> String { |
56 | 0 | self.id.clone() |
57 | 0 | } |
58 | | |
59 | 0 | fn snapshot(&mut self) -> Result<Snapshot, MigratableError> { |
60 | 0 | Snapshot::new_from_state(&()) |
61 | 0 | } |
62 | | } |
63 | | |
64 | | impl Pausable for DebugConsole {} |
65 | | impl Transportable for DebugConsole {} |
66 | | impl Migratable for DebugConsole {} |