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	Getdents = 0x1160,
31	FileStat = 0x1170,
32	FileFstat = 0x1180,
33	Mkdir = 0x1190,
34	SharedMemOpen = 0x1200,
35	SharedMemClose = 0x1210,
36}
37
38into_hypercall_addresses! {
39	impl From<Hypercall> for HypercallAddress {
40		match {
41			Exit,
42			FileClose,
43			FileLseek,
44			FileOpen,
45			FileRead,
46			FileUnlink,
47			FileWrite,
48			Getdents,
49			Mkdir,
50			FileStat,
51			FileFstat,
52			SerialReadBuffer,
53			SerialReadByte,
54			SerialWriteBuffer,
55			SerialWriteByte,
56		}
57	}
58}
59
60/// Hypervisor calls available in Uhyve with their respective parameters. See the [module level documentation](crate) on how to invoke them.
61#[non_exhaustive]
62#[derive(Debug)]
63pub enum Hypercall<'a> {
64	/// Exit the VM and return a status code.
65	Exit(i32),
66	FileClose(&'a mut CloseParams),
67	FileLseek(&'a mut LseekParams),
68	FileOpen(&'a mut OpenParams),
69	FileRead(&'a mut ReadParams),
70	FileWrite(&'a mut WriteParams),
71	FileUnlink(&'a mut UnlinkParams),
72	/// Get directory entries from a directory. Similar to linux getdents64.
73	Getdents(&'a mut GetdentParams),
74	/// Read file metadata. Similar to `stat(2)` / `lstat(2)`.
75	FileStat(&'a mut StatParams),
76	/// Read file metadata for an open descriptor. Similar to `fstat(2)`.
77	FileFstat(&'a mut FstatParams),
78	/// Create a new directory.
79	Mkdir(&'a mut MkdirParams),
80	/// Write a char to the terminal.
81	SerialWriteByte(u8),
82	/// Write a buffer to the terminal
83	SerialWriteBuffer(&'a SerialWriteBufferParams),
84	/// Read a single byte from the terminal
85	SerialReadByte,
86	/// Read a buffer from the terminal
87	SerialReadBuffer(&'a SerialReadBufferParams),
88}
89impl<'a> Hypercall<'a> {
90	/// Get a hypercall's port address.
91	pub fn port(self) -> u16 {
92		HypercallAddress::from(self) as u16
93	}
94}