1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#![allow(unused)]
use alloc::boxed::Box;
use alloc::vec::Vec;
use core::convert::{TryFrom, TryInto};
use core::fmt::Write;
use core::{isize, ptr, slice, str};
use crate::arch;
use crate::console;
use crate::environment;
use crate::errno::*;
use crate::syscalls::fs::{self, FilePerms, PosixFile, SeekWhence};
use crate::util;
pub use self::generic::*;
pub use self::uhyve::*;
mod generic;
mod uhyve;
const SEEK_SET: i32 = 0;
const SEEK_CUR: i32 = 1;
const SEEK_END: i32 = 2;
impl TryFrom<i32> for SeekWhence {
type Error = &'static str;
fn try_from(value: i32) -> Result<Self, Self::Error> {
match value {
SEEK_CUR => Ok(SeekWhence::Cur),
SEEK_SET => Ok(SeekWhence::Set),
SEEK_END => Ok(SeekWhence::End),
_ => Err("Got invalid seek whence parameter!"),
}
}
}
const O_WRONLY: i32 = 0o0001;
const O_RDWR: i32 = 0o0002;
const O_CREAT: i32 = 0o0100;
const O_EXCL: i32 = 0o0200;
const O_TRUNC: i32 = 0o1000;
const O_APPEND: i32 = 0o2000;
const O_DIRECT: i32 = 0o40000;
fn open_flags_to_perm(flags: i32, mode: u32) -> FilePerms {
let mode =
match mode {
0x777 => 0o777,
0 => 0,
_ => {
info!("Mode neither 777 nor 0, should never happen with current hermit stdlib! Using 777");
0o777
}
};
let mut perms = FilePerms {
raw: flags as u32,
mode,
..Default::default()
};
perms.write = flags & (O_WRONLY | O_RDWR) != 0;
perms.creat = flags & (O_CREAT) != 0;
perms.excl = flags & (O_EXCL) != 0;
perms.trunc = flags & (O_TRUNC) != 0;
perms.append = flags & (O_APPEND) != 0;
perms.directio = flags & (O_DIRECT) != 0;
if flags & !(O_WRONLY | O_RDWR | O_CREAT | O_EXCL | O_TRUNC | O_APPEND | O_DIRECT) != 0 {
warn!("Unknown file flags used! {}", flags);
}
perms
}
pub trait SyscallInterface: Send + Sync {
fn init(&self) {
}
fn get_application_parameters(&self) -> (i32, *const *const u8, *const *const u8) {
let mut argv = Vec::new();
let name = Box::leak(Box::new("{name}\0")).as_ptr();
argv.push(name);
if let Some(args) = environment::get_command_line_argv() {
debug!("Setting argv as: {:?}", args);
for a in args {
let ptr = Box::leak(format!("{}\0", a).into_boxed_str()).as_ptr();
argv.push(ptr);
}
}
let environ = ptr::null() as *const *const u8;
let argc = argv.len() as i32;
let argv = Box::leak(argv.into_boxed_slice()).as_ptr();
(argc, argv, environ)
}
fn shutdown(&self, _arg: i32) -> ! {
arch::processor::shutdown()
}
fn get_mac_address(&self) -> Result<[u8; 6], ()> {
#[cfg(feature = "pci")]
match arch::kernel::pci::get_network_driver() {
Some(driver) => Ok(driver.lock().get_mac_address()),
_ => Err(()),
}
#[cfg(not(feature = "pci"))]
Err(())
}
fn get_mtu(&self) -> Result<u16, ()> {
#[cfg(feature = "pci")]
match arch::kernel::pci::get_network_driver() {
Some(driver) => Ok(driver.lock().get_mtu()),
_ => Err(()),
}
#[cfg(not(feature = "pci"))]
Err(())
}
fn has_packet(&self) -> bool {
#[cfg(feature = "pci")]
match arch::kernel::pci::get_network_driver() {
Some(driver) => driver.lock().has_packet(),
_ => false,
}
#[cfg(not(feature = "pci"))]
false
}
fn get_tx_buffer(&self, len: usize) -> Result<(*mut u8, usize), ()> {
#[cfg(feature = "pci")]
match arch::kernel::pci::get_network_driver() {
Some(driver) => driver.lock().get_tx_buffer(len),
_ => Err(()),
}
#[cfg(not(feature = "pci"))]
Err(())
}
fn send_tx_buffer(&self, handle: usize, len: usize) -> Result<(), ()> {
#[cfg(feature = "pci")]
match arch::kernel::pci::get_network_driver() {
Some(driver) => driver.lock().send_tx_buffer(handle, len),
_ => Err(()),
}
#[cfg(not(feature = "pci"))]
Err(())
}
fn receive_rx_buffer(&self) -> Result<(&'static [u8], usize), ()> {
#[cfg(feature = "pci")]
match arch::kernel::pci::get_network_driver() {
Some(driver) => driver.lock().receive_rx_buffer(),
_ => Err(()),
}
#[cfg(not(feature = "pci"))]
Err(())
}
fn rx_buffer_consumed(&self, handle: usize) -> Result<(), ()> {
#[cfg(feature = "pci")]
match arch::kernel::pci::get_network_driver() {
Some(driver) => {
driver.lock().rx_buffer_consumed(handle);
Ok(())
}
_ => Err(()),
}
#[cfg(not(feature = "pci"))]
Err(())
}
#[cfg(not(target_arch = "x86_64"))]
fn unlink(&self, _name: *const u8) -> i32 {
debug!("unlink is unimplemented, returning -ENOSYS");
-ENOSYS
}
#[cfg(target_arch = "x86_64")]
fn unlink(&self, name: *const u8) -> i32 {
let name = unsafe { util::c_str_to_str(name) };
debug!("unlink {}", name);
fs::FILESYSTEM
.lock()
.unlink(&name)
.expect("Unlinking failed!");
0
}
#[cfg(not(target_arch = "x86_64"))]
fn open(&self, _name: *const u8, _flags: i32, _mode: i32) -> i32 {
debug!("open is unimplemented, returning -ENOSYS");
-ENOSYS
}
#[cfg(target_arch = "x86_64")]
fn open(&self, name: *const u8, flags: i32, mode: i32) -> i32 {
let name = unsafe { util::c_str_to_str(name) };
debug!("Open {}, {}, {}", name, flags, mode);
let mut fs = fs::FILESYSTEM.lock();
let fd = fs.open(&name, open_flags_to_perm(flags, mode as u32));
if let Ok(fd) = fd {
fd as i32
} else {
-1
}
}
fn close(&self, fd: i32) -> i32 {
if fd < 3 {
return 0;
}
let mut fs = fs::FILESYSTEM.lock();
fs.close(fd as u64);
0
}
#[cfg(not(target_arch = "x86_64"))]
fn read(&self, _fd: i32, _buf: *mut u8, _len: usize) -> isize {
debug!("read is unimplemented, returning -ENOSYS");
-ENOSYS as isize
}
#[cfg(target_arch = "x86_64")]
fn read(&self, fd: i32, buf: *mut u8, len: usize) -> isize {
debug!("Read! {}, {}", fd, len);
let mut fs = fs::FILESYSTEM.lock();
let mut read_bytes = 0;
fs.fd_op(fd as u64, |file: &mut Box<dyn PosixFile + Send>| {
let dat = file.read(len as u32).unwrap();
read_bytes = dat.len();
unsafe {
core::slice::from_raw_parts_mut(buf, read_bytes).copy_from_slice(&dat);
}
});
read_bytes as isize
}
fn write(&self, fd: i32, buf: *const u8, len: usize) -> isize {
assert!(len <= isize::MAX as usize);
if fd > 2 {
let buf = unsafe { slice::from_raw_parts(buf, len) };
let mut written_bytes = 0;
let mut fs = fs::FILESYSTEM.lock();
fs.fd_op(fd as u64, |file: &mut Box<dyn PosixFile + Send>| {
written_bytes = file.write(buf).unwrap();
});
debug!("Write done! {}", written_bytes);
written_bytes as isize
} else {
unsafe {
let slice = slice::from_raw_parts(buf, len);
console::CONSOLE
.lock()
.write_str(str::from_utf8_unchecked(slice))
.unwrap();
}
len as isize
}
}
fn lseek(&self, fd: i32, offset: isize, whence: i32) -> isize {
debug!("lseek! {}, {}, {}", fd, offset, whence);
let mut fs = fs::FILESYSTEM.lock();
let mut ret = 0;
fs.fd_op(fd as u64, |file: &mut Box<dyn PosixFile + Send>| {
ret = file.lseek(offset, whence.try_into().unwrap()).unwrap();
});
ret as isize
}
fn stat(&self, _file: *const u8, _st: usize) -> i32 {
info!("stat is unimplemented");
-ENOSYS
}
}