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