uhyve_interface/parameters.rs
1//! Parameters for hypercalls.
2
3use crate::{GuestPhysAddr, GuestVirtAddr, MAX_ARGC_ENVC};
4
5/// Parameters for a [`Cmdsize`](crate::Hypercall::Cmdsize) hypercall which provides the lengths of the items in the argument end environment vector.
6#[repr(C, packed)]
7#[derive(Debug, Copy, Clone)]
8pub struct CmdsizeParams {
9 /// Nr of items in the kernel command line.
10 pub argc: i32,
11 /// Lengths of the items in the kernel command line.
12 pub argsz: [i32; MAX_ARGC_ENVC],
13 /// Nr of items in the environment.
14 pub envc: i32,
15 /// Length of the items in the environment.
16 pub envsz: [i32; MAX_ARGC_ENVC],
17}
18impl CmdsizeParams {
19 #[cfg(feature = "std")]
20 /// Update the struct with the lengths of the given command.
21 /// - `path` is usually the path and name of the application. E.g., "/home/hermit/app"
22 /// - `args` is a list of strings that form the parameters. (E.g., `["-v", "myarg"]`)
23 ///
24 /// Note that this hypercall only transfers the sizes. It usually has to be followed up with the [`Cmdval` Hypercall](crate::Hypercall::Cmdval).
25 pub fn update(&mut self, path: &std::path::Path, args: &[String]) {
26 self.argc = 0;
27
28 self.argsz[0] = path.as_os_str().len() as i32 + 1;
29
30 self.argc += 1;
31 for argument in args {
32 self.argsz[(self.argc) as usize] = argument.len() as i32 + 1;
33
34 self.argc += 1;
35 }
36
37 self.envc = 0;
38 // let mut counter = 0;
39 for (key, value) in std::env::vars_os() {
40 if self.envc < MAX_ARGC_ENVC.try_into().unwrap() {
41 self.envsz[self.envc as usize] = (key.len() + value.len()) as i32 + 2;
42 self.envc += 1;
43 } else {
44 log::warn!("Environment is too large! {key:?}={value:?} will not be passed!");
45 }
46 }
47 }
48}
49
50/// Parameters for a [`Cmdval`](crate::Hypercall::Cmdval) hypercall, which copies the arguments end environment of the application into the VM's memory.
51#[repr(C, packed)]
52#[derive(Debug, Copy, Clone)]
53pub struct CmdvalParams {
54 /// Pointer to a memory section in the VM memory which holds addresses for the destinations of the individual arguments
55 pub argv: GuestPhysAddr,
56 /// Pointer to a memory section in the VM memory which holds addresses for the destinations of the individual environment variables
57 pub envp: GuestPhysAddr,
58}
59
60/// Parameters for a [`Exit`](crate::Hypercall::Exit) hypercall.
61#[repr(C, packed)]
62#[derive(Debug, Copy, Clone)]
63pub struct ExitParams {
64 /// The return code of the guest.
65 pub arg: i32,
66}
67
68/// Parameters for a [`FileUnlink`](crate::Hypercall::FileUnlink) hypercall.
69#[repr(C, packed)]
70#[derive(Debug, Copy, Clone)]
71pub struct UnlinkParams {
72 /// Address of the file that should be unlinked.
73 pub name: GuestPhysAddr,
74 /// On success, `0` is returned. On error, `-1` is returned.
75 pub ret: i32,
76}
77
78/// Parameters for a [`FileWrite`](crate::Hypercall::FileWrite) hypercall.
79#[repr(C, packed)]
80#[derive(Debug, Copy, Clone)]
81pub struct WriteParams {
82 /// File descriptor of the file.
83 pub fd: i32,
84 /// Buffer to be written into the file.
85 pub buf: GuestVirtAddr,
86 /// Number of bytes in the buffer to be written.
87 pub len: usize,
88}
89
90/// Parameters for a [`FileRead`](crate::Hypercall::FileRead) hypercall.
91#[repr(C, packed)]
92#[derive(Debug, Copy, Clone)]
93pub struct ReadParams {
94 /// File descriptor of the file.
95 pub fd: i32,
96 /// Buffer to read the file into.
97 pub buf: GuestVirtAddr,
98 /// Number of bytes to read into the buffer.
99 pub len: usize,
100 /// Number of bytes read on success. `-1` on failure.
101 pub ret: isize,
102}
103
104/// Parameters for a [`FileClose`](crate::Hypercall::FileClose) hypercall.
105#[repr(C, packed)]
106#[derive(Debug, Copy, Clone)]
107pub struct CloseParams {
108 /// File descriptor of the file.
109 pub fd: i32,
110 /// Zero on success, `-1` on failure.
111 pub ret: i32,
112}
113
114/// Parameters for a [`FileOpen`](crate::Hypercall::FileOpen) hypercall.
115#[repr(C, packed)]
116#[derive(Debug, Copy, Clone)]
117pub struct OpenParams {
118 /// Pathname of the file to be opened.
119 pub name: GuestPhysAddr,
120 /// Posix file access mode flags.
121 pub flags: i32,
122 /// Access permissions upon opening/creating a file.
123 pub mode: i32,
124 /// File descriptor upon successful opening or `-1` upon failure.
125 pub ret: i32,
126}
127
128/// Parameters for a [`FileLseek`](crate::Hypercall::FileLseek) hypercall
129#[repr(C, packed)]
130#[derive(Debug, Copy, Clone)]
131pub struct LseekParams {
132 /// File descriptor of the file.
133 pub fd: i32,
134 /// Offset in the file.
135 pub offset: isize,
136 /// `whence` value of the lseek call.
137 pub whence: i32,
138}
139
140/// Parameters for a [`SerialWriteBuffer`](crate::Hypercall::SerialWriteBuffer) hypercall.
141#[repr(C, packed)]
142#[derive(Debug, Copy, Clone)]
143pub struct SerialWriteBufferParams {
144 pub buf: GuestPhysAddr,
145 pub len: usize,
146}
147
148// File operations supported by Hermit and Uhyve
149pub const O_RDONLY: i32 = 0o0000;
150pub const O_WRONLY: i32 = 0o0001;
151pub const O_RDWR: i32 = 0o0002;
152pub const O_CREAT: i32 = 0o0100;
153pub const O_EXCL: i32 = 0o0200;
154pub const O_TRUNC: i32 = 0o1000;
155pub const O_APPEND: i32 = 0o2000;
156pub const O_DIRECT: i32 = 0o40000;
157pub const O_DIRECTORY: i32 = 0o200000;
158
159pub const ALLOWED_OPEN_FLAGS: i32 =
160 O_RDONLY | O_WRONLY | O_RDWR | O_CREAT | O_EXCL | O_TRUNC | O_APPEND | O_DIRECT | O_DIRECTORY;
161
162pub const ENOENT: i32 = 2;
163pub const EINVAL: i32 = 22;