uhyve_interface/v2/parameters.rs
1//! Parameters for [Hypercalls](crate::v2::Hypercall).
2
3use core::marker::PhantomData;
4
5use num_enum::{IntoPrimitive, TryFromPrimitive};
6
7use crate::GuestPhysAddr;
8/// Re-export of all unchanged parameters and flags from v1.
9pub use crate::parameters::*;
10pub use crate::v1::parameters::{CloseParams, OpenParams, UnlinkParams};
11
12/// Parameters for a [`FileWrite`](crate::v2::Hypercall::FileWrite) hypercall.
13#[repr(C)]
14#[derive(Debug, Copy, Clone)]
15pub struct WriteParams {
16 /// Number of bytes in the buffer to be written.
17 pub len: u64,
18 /// Number of bytes written on success or errno.
19 pub ret: i64,
20 /// Buffer to be written into the file.
21 pub buf: GuestPhysAddr,
22 /// File descriptor of the file.
23 pub fd: i32,
24}
25
26/// Parameters for a [`FileRead`](crate::v2::Hypercall::FileRead) hypercall.
27#[repr(C)]
28#[derive(Debug, Copy, Clone)]
29pub struct ReadParams {
30 /// Number of bytes to read into the buffer.
31 pub len: u64,
32 /// Number of bytes read on success or errno.
33 pub ret: i64,
34 /// Buffer to read the file into.
35 pub buf: GuestPhysAddr,
36 /// File descriptor of the file.
37 pub fd: i32,
38}
39
40/// Parameters for a [`FileLseek`](crate::v2::Hypercall::FileLseek) hypercall
41#[repr(C)]
42#[derive(Debug, Copy, Clone)]
43pub struct LseekParams {
44 /// Offset in the file.
45 pub offset: i64,
46 /// `whence` value of the lseek call.
47 pub whence: u32,
48 /// File descriptor of the file.
49 pub fd: i32,
50}
51
52/// Parameters for a [`SerialWriteBuffer`](crate::v1::Hypercall::SerialWriteBuffer) hypercall.
53#[repr(C)]
54#[derive(Debug, Copy, Clone)]
55pub struct SerialWriteBufferParams {
56 /// Length of the buffer.
57 pub len: u64,
58 /// Address of the buffer to be printed.
59 pub buf: GuestPhysAddr,
60}
61
62/// Parameters for a [`SerialReadBuffer`](crate::v2::Hypercall::SerialReadBuffer) hypercall.
63#[repr(C)]
64#[derive(Debug, Copy, Clone)]
65pub struct SerialReadBufferParams {
66 /// length of `buf`.
67 pub maxlen: u64,
68 /// Amount of bytes acutally written.
69 pub len: u64,
70 /// Address to write to.
71 pub buf: GuestPhysAddr,
72}
73
74/// File type enum from Linux kernel
75#[derive(TryFromPrimitive, IntoPrimitive, PartialEq, Eq, Clone, Copy, Debug)]
76#[repr(u8)]
77pub enum FileType {
78 Unknown = 0, // DT_UNKNOWN
79 Fifo = 1, // DT_FIFO
80 CharacterDevice = 2, // DT_CHR
81 Directory = 4, // DT_DIR
82 BlockDevice = 6, // DT_BLK
83 RegularFile = 8, // DT_REG
84 SymbolicLink = 10, // DT_LNK
85 Socket = 12, // DT_SOCK
86 Whiteout = 14, // DT_WHT
87}
88/// Dirent64 struct from Linux kernel
89#[repr(C)]
90pub struct Dirent64 {
91 /// 64-bit inode number
92 pub d_ino: u64,
93 /// Field without meaning. Kept for BW compatibility. Will not be used by Uhyve
94 pub d_off: i64,
95 /// Size of this dirent
96 pub d_reclen: u16,
97 /// File type
98 pub d_type: FileType,
99 /// Filename (null-terminated)
100 pub d_name: PhantomData<u8>,
101}
102/// Result of a [`Getdent`](crate::v2::Hypercall::Getdent) hypercall.
103#[derive(Debug, Copy, Clone)]
104#[repr(C)]
105pub enum GetdentResult {
106 /// No result. Guests should set this value before calling the hypercall.
107 None,
108 /// Number of bytes written on success.
109 Success(u64),
110 /// End of directory.
111 EndOfDirectory,
112 /// Error with libc errno.
113 Error(i32),
114}
115/// Parameters for a [`Getdent`](crate::v2::Hypercall::Getdent) hypercall.
116#[repr(C)]
117#[derive(Debug, Copy, Clone)]
118pub struct GetdentParams {
119 /// Guest file descriptor of the directory (from [`FileOpen`](crate::v2::Hypercall::FileOpen) with `O_DIRECTORY`).
120 pub fd: i32,
121 /// Buffer to write to.
122 pub buf: GuestPhysAddr,
123 /// Length of the buffer.
124 pub len: u64,
125 /// Return value of the hypercall.
126 pub ret: GetdentResult,
127}
128
129/// Result of a [`Mkdir`](crate::v2::Hypercall::Mkdir) hypercall.
130#[derive(Debug, Copy, Clone)]
131#[repr(C)]
132pub enum MkdirResult {
133 /// No result. Guests should set this value before calling the hypercall.
134 None,
135 /// Directory was created.
136 Success,
137 /// Error with libc errno.
138 Error(i32),
139}
140
141/// Parameters for a [`Mkdir`](crate::v2::Hypercall::Mkdir) hypercall.
142#[repr(C)]
143#[derive(Debug, Copy, Clone)]
144pub struct MkdirParams {
145 /// Path to create. Zero terminated C-String.
146 pub path: GuestPhysAddr,
147 /// Length of the path buffer.
148 pub len: u64,
149 /// Return value of the hypercall.
150 pub ret: MkdirResult,
151}
152
153/// Which stat-like operation to perform.
154#[derive(TryFromPrimitive, IntoPrimitive, PartialEq, Eq, Clone, Copy, Debug)]
155#[repr(u32)]
156pub enum StatKind {
157 /// Follow symlinks (like `stat(2)`).
158 Stat = 0,
159 /// Do not follow symlinks (like `lstat(2)`).
160 LStat = 1,
161}
162
163/// Time value used in [`FileAttr`].
164#[repr(C)]
165#[derive(Debug, Copy, Clone, Default, PartialEq, Eq)]
166pub struct Timespec {
167 /// Seconds since the Unix epoch.
168 pub tv_sec: i64,
169 /// Nanoseconds.
170 pub tv_nsec: i32,
171}
172impl Timespec {
173 pub fn from_nsecs(secs: i64, nsecs: i64) -> Option<Self> {
174 nsecs.try_into().ok().map(|nsec| Self {
175 tv_sec: secs,
176 tv_nsec: nsec,
177 })
178 }
179}
180
181/// File metadata returned by [`FileStat`](crate::v2::Hypercall::FileStat).
182///
183/// Layout-compatible with Hermit's `FileAttr`.
184#[repr(C)]
185#[derive(Debug, Copy, Clone, Default, PartialEq, Eq)]
186pub struct FileAttr {
187 pub st_dev: u64,
188 pub st_ino: u64,
189 pub st_nlink: u64,
190 /// `st_mode` from POSIX (`S_IFMT` and permission bits).
191 pub st_mode: u32,
192 pub st_uid: u32,
193 pub st_gid: u32,
194 pub st_rdev: u64,
195 pub st_size: i64,
196 pub st_blksize: i64,
197 pub st_blocks: i64,
198 pub st_atim: Timespec,
199 pub st_mtim: Timespec,
200 pub st_ctim: Timespec,
201}
202
203/// Result of a [`FileStat`](crate::v2::Hypercall::FileStat) hypercall.
204#[derive(Debug, Copy, Clone, PartialEq, Eq)]
205#[repr(C)]
206pub enum StatResult {
207 /// No result. Guests should set this value before calling the hypercall.
208 None,
209 /// [`FileAttr`] was written to `attr` on success.
210 Success,
211 /// Error with libc errno.
212 Error(i32),
213}
214
215/// Parameters for a [`FileStat`](crate::v2::Hypercall::FileStat) hypercall.
216#[repr(C)]
217#[derive(Debug, Copy, Clone)]
218pub struct StatParams {
219 /// Path to stat. Must be a null-terminated UTF-8 string.
220 pub name: GuestPhysAddr,
221 /// Whether to follow symlinks on the host.
222 pub kind: StatKind,
223 /// Guest buffer to write the resulting [`FileAttr`] into.
224 pub attr: GuestPhysAddr,
225 /// Return value of the hypercall.
226 pub ret: StatResult,
227}
228
229/// Parameters for a [`FileFstat`](crate::v2::Hypercall::FileFstat) hypercall.
230#[repr(C)]
231#[derive(Debug, Copy, Clone)]
232pub struct FstatParams {
233 /// Guest file descriptor (from [`FileOpen`](crate::v2::Hypercall::FileOpen)).
234 pub fd: i32,
235 /// Guest buffer to write the resulting [`FileAttr`] into.
236 pub attr: GuestPhysAddr,
237 /// Return value of the hypercall.
238 pub ret: StatResult,
239}