Skip to content

Commit ada8147

Browse files
stlankesmkroening
authored andcommitted
add support of console
Add structs and constants, which are derived from the specification Virtual I/O Device (VIRTIO), Version 1.2
1 parent 07bcc57 commit ada8147

File tree

3 files changed

+186
-21
lines changed

3 files changed

+186
-21
lines changed

src/console.rs

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
//! Console Device
2+
3+
use num_enum::{FromPrimitive, IntoPrimitive};
4+
use volatile::access::ReadOnly;
5+
use volatile_macro::VolatileFieldAccess;
6+
7+
pub use super::features::console::F;
8+
use crate::{le16, le32};
9+
10+
/// Console Device Configuration Layout
11+
///
12+
/// Use [`ConfigVolatileFieldAccess`] to work with this struct.
13+
#[doc(alias = "virtio_console_config")]
14+
#[cfg_attr(
15+
feature = "zerocopy",
16+
derive(
17+
zerocopy_derive::KnownLayout,
18+
zerocopy_derive::Immutable,
19+
zerocopy_derive::FromBytes,
20+
)
21+
)]
22+
#[derive(VolatileFieldAccess)]
23+
#[repr(C)]
24+
pub struct Config {
25+
#[access(ReadOnly)]
26+
cols: le16,
27+
#[access(ReadOnly)]
28+
rows: le16,
29+
#[access(ReadOnly)]
30+
max_nr_ports: le32,
31+
#[access(ReadOnly)]
32+
emerg_wr: le32,
33+
}
34+
35+
/// Control Message
36+
#[doc(alias = "virtio_console_control")]
37+
#[cfg_attr(
38+
feature = "zerocopy",
39+
derive(
40+
zerocopy_derive::KnownLayout,
41+
zerocopy_derive::Immutable,
42+
zerocopy_derive::FromBytes,
43+
zerocopy_derive::IntoBytes,
44+
)
45+
)]
46+
#[derive(Clone, Copy, Debug)]
47+
#[repr(C)]
48+
pub struct Control {
49+
/// Port number
50+
pub id: le32,
51+
/// The kind of control event
52+
pub event: le16,
53+
/// Extra information for the event
54+
pub value: le16,
55+
}
56+
57+
/// Event
58+
#[doc(alias = "VIRTIO_CONSOLE")]
59+
#[derive(IntoPrimitive, FromPrimitive, PartialEq, Eq, Clone, Copy, Debug)]
60+
#[non_exhaustive]
61+
#[repr(u16)]
62+
pub enum Device {
63+
/// Sent by the driver at initialization to indicate that it is ready to receive control messages.
64+
///
65+
/// A value of 1 indicates success, and 0 indicates failure.
66+
/// The port number `id` is unused.
67+
#[doc(alias = "VIRTIO_CONSOLE_DEVICE_READY")]
68+
DeviceReady = 0,
69+
70+
/// Sent by the device, to create a new port.
71+
///
72+
/// `value` is unused.
73+
#[doc(alias = "VIRTIO_CONSOLE_DEVICE_ADD")]
74+
DeviceAdd = 1,
75+
76+
/// Sent by the device, to remove an existing port.
77+
///
78+
/// `value` is unused.
79+
#[doc(alias = "VIRTIO_CONSOLE_DEVICE_REMOVE")]
80+
DeviceRemove = 2,
81+
82+
/// Sent by the driver in response to the device's VIRTIO_CONSOLE_PORT_ADD message, to indicate that the port is ready to be used.
83+
///
84+
/// A `value` of 1 indicates success, and 0 indicates failure.
85+
#[doc(alias = "VIRTIO_CONSOLE_PORT_READY")]
86+
PortReady = 3,
87+
88+
/// Sent by the device to nominate a port as a console port.
89+
///
90+
/// There MAY be more than one console port.
91+
#[doc(alias = "VIRTIO_CONSOLE_CONSOLE_PORT")]
92+
ConsolePort = 4,
93+
94+
/// Sent by the device to indicate a console size change.
95+
///
96+
/// `value` is unused.
97+
/// The buffer is followed by the number of columns and rows ([`virtio_console_resize`]).
98+
///
99+
/// [`virtio_console_resize`]: Resize
100+
#[doc(alias = "VIRTIO_CONSOLE_RESIZE")]
101+
Resize = 5,
102+
103+
/// This message is sent by both the device and the driver.
104+
///
105+
/// `value` indicates the state: 0 (port closed) or 1 (port open).
106+
/// This allows for ports to be used directly by guest and host processes to communicate in an application-defined manner.
107+
#[doc(alias = "VIRTIO_CONSOLE_PORT_OPEN")]
108+
PortOpen = 6,
109+
110+
/// Sent by the device to give a tag to the port.
111+
///
112+
/// This control command is immediately followed by the UTF-8 name of the port for identification within the guest (without a NUL terminator).
113+
#[doc(alias = "VIRTIO_CONSOLE_PORT_NAME")]
114+
PortName = 7,
115+
116+
#[num_enum(catch_all)]
117+
Unknown(u16),
118+
}
119+
120+
/// Resize Message Layout
121+
#[doc(alias = "virtio_console_resize")]
122+
#[cfg_attr(
123+
feature = "zerocopy",
124+
derive(
125+
zerocopy_derive::KnownLayout,
126+
zerocopy_derive::Immutable,
127+
zerocopy_derive::FromBytes,
128+
zerocopy_derive::IntoBytes,
129+
)
130+
)]
131+
#[derive(Clone, Copy, Debug)]
132+
#[repr(C)]
133+
pub struct Resize {
134+
pub cols: le16,
135+
pub rows: le16,
136+
}

src/features.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,34 @@ macro_rules! feature_bits {
289289
() => {};
290290
}
291291

292+
pub mod console {
293+
use crate::le128;
294+
295+
feature_bits! {
296+
/// Console Device Feature Bits
297+
#[doc(alias = "VIRTIO_CONSOLE_F")]
298+
pub struct F: le128 {
299+
/// Configuration `cols` and `rows` are valid.
300+
#[doc(alias = "VIRTIO_CONSOLE_F_SIZE")]
301+
const SIZE = 1 << 0;
302+
303+
/// Device has support for multiple ports;
304+
///
305+
/// `max_nr_ports` is valid and control virtqueues will be used.
306+
#[doc(alias = "VIRTIO_CONSOLE_F_MULTIPORT")]
307+
const MULTIPORT = 1 << 1;
308+
309+
/// Device has support for emergency write.
310+
///
311+
/// Configuration field emerg_wr is valid.
312+
#[doc(alias = "VIRTIO_CONSOLE_F_EMERG_WRITE")]
313+
const EMERG_WRITE = 1 << 2;
314+
}
315+
}
316+
317+
impl crate::FeatureBits for F {}
318+
}
319+
292320
pub mod net {
293321
use crate::le128;
294322

src/lib.rs

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -61,27 +61,27 @@
6161
//!
6262
//! ## Device Types
6363
//!
64-
//! | Device Type | Available | Module |
65-
//! | --------------------------------- | --------- | --------- |
66-
//! | Network Device | ✅ | [`net`] |
67-
//! | Block Device | ❌ | |
68-
//! | Console Device | | |
69-
//! | Entropy Device | ❌ | |
70-
//! | Traditional Memory Balloon Device | ❌ | |
71-
//! | SCSI Host Device | ❌ | |
72-
//! | GPU Device | ❌ | |
73-
//! | Input Device | ❌ | |
74-
//! | Crypto Device | ❌ | |
75-
//! | Socket Device | ✅ | [`vsock`] |
76-
//! | File System Device | ✅ | [`fs`] |
77-
//! | RPMB Device | ❌ | |
78-
//! | IOMMU Device | ❌ | |
79-
//! | Sound Device | ❌ | |
80-
//! | Memory Device | ❌ | |
81-
//! | I2C Adapter Device | ❌ | |
82-
//! | SCMI Device | ❌ | |
83-
//! | GPIO Device | ❌ | |
84-
//! | PMEM Device | ❌ | |
64+
//! | Device Type | Available | Module |
65+
//! | --------------------------------- | --------- | ----------- |
66+
//! | Network Device | ✅ | [`net`] |
67+
//! | Block Device | ❌ | |
68+
//! | Console Device | | [`console`] |
69+
//! | Entropy Device | ❌ | |
70+
//! | Traditional Memory Balloon Device | ❌ | |
71+
//! | SCSI Host Device | ❌ | |
72+
//! | GPU Device | ❌ | |
73+
//! | Input Device | ❌ | |
74+
//! | Crypto Device | ❌ | |
75+
//! | Socket Device | ✅ | [`vsock`] |
76+
//! | File System Device | ✅ | [`fs`] |
77+
//! | RPMB Device | ❌ | |
78+
//! | IOMMU Device | ❌ | |
79+
//! | Sound Device | ❌ | |
80+
//! | Memory Device | ❌ | |
81+
//! | I2C Adapter Device | ❌ | |
82+
//! | SCMI Device | ❌ | |
83+
//! | GPIO Device | ❌ | |
84+
//! | PMEM Device | ❌ | |
8585
8686
#![cfg_attr(not(test), no_std)]
8787
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
@@ -94,6 +94,7 @@ extern crate alloc;
9494
mod bitflags;
9595
#[macro_use]
9696
pub mod volatile;
97+
pub mod console;
9798
#[cfg(any(feature = "mmio", feature = "pci"))]
9899
mod driver_notifications;
99100
mod features;

0 commit comments

Comments
 (0)