Skip to main content

uhyve_interface/v2/
mod.rs

1//! # Uhyve Hypervisor Interface V2
2//!
3//! The Uhyve hypercall interface works as follows:
4//!
5//! - The guest writes (or reads) to the respective [`HypercallAddress`](v2::HypercallAddress). The 64-bit value written to that location is the guest's physical memory address of the hypercall's parameter.
6//! - The hypervisor handles the hypercall. Depending on the Hypercall, the hypervisor might change the parameters struct in the guest's memory.
7
8pub mod parameters;
9use parameters::*;
10
11/// Enum containing all valid MMIO addresses for hypercalls.
12///
13/// The discriminants of this enum are the respective addresses, so one can get the code by calling
14/// e.g., `HypercallAddress::Exit as u64`.
15#[non_exhaustive]
16#[repr(u64)]
17#[derive(Clone, Copy, Debug, Eq, PartialEq, num_enum::TryFromPrimitive, Hash)]
18pub enum HypercallAddress {
19	Exit = 0x1010,
20	SerialWriteByte = 0x1020,
21	SerialWriteBuffer = 0x1030,
22	SerialReadByte = 0x1040,
23	SerialReadBuffer = 0x1050,
24	FileWrite = 0x1100,
25	FileOpen = 0x1110,
26	FileClose = 0x1120,
27	FileRead = 0x1130,
28	FileLseek = 0x1140,
29	FileUnlink = 0x1150,
30	SharedMemOpen = 0x1200,
31	SharedMemClose = 0x1210,
32}
33
34into_hypercall_addresses! {
35	impl From<Hypercall> for HypercallAddress {
36		match {
37			Exit,
38			FileClose,
39			FileLseek,
40			FileOpen,
41			FileRead,
42			FileUnlink,
43			FileWrite,
44			SerialReadBuffer,
45			SerialReadByte,
46			SerialWriteBuffer,
47			SerialWriteByte,
48		}
49	}
50}
51
52/// Hypervisor calls available in Uhyve with their respective parameters. See the [module level documentation](crate) on how to invoke them.
53#[non_exhaustive]
54#[derive(Debug)]
55pub enum Hypercall<'a> {
56	/// Exit the VM and return a status code.
57	Exit(i32),
58	FileClose(&'a mut CloseParams),
59	FileLseek(&'a mut LseekParams),
60	FileOpen(&'a mut OpenParams),
61	FileRead(&'a mut ReadParams),
62	FileWrite(&'a mut WriteParams),
63	FileUnlink(&'a mut UnlinkParams),
64	/// Write a char to the terminal.
65	SerialWriteByte(u8),
66	/// Write a buffer to the terminal
67	SerialWriteBuffer(&'a SerialWriteBufferParams),
68	/// Read a single byte from the terminal
69	SerialReadByte,
70	/// Read a buffer from the terminal
71	SerialReadBuffer(&'a SerialReadBufferParams),
72}
73impl<'a> Hypercall<'a> {
74	/// Get a hypercall's port address.
75	pub fn port(self) -> u16 {
76		HypercallAddress::from(self) as u16
77	}
78}