]>
git.proxmox.com Git - rustc.git/blob - vendor/hermit-abi/src/lib.rs
1 //! `hermit-abi` is small interface to call functions from the unikernel
2 //! [RustyHermit](https://github.com/hermitcore/libhermit-rs).
5 #![allow(clippy::missing_safety_doc)]
6 #![allow(clippy::result_unit_err)]
15 // sysmbols, which are part of the library operating system
18 fn sys_secure_rand64() -> Option
<u64>;
19 fn sys_secure_rand32() -> Option
<u32>;
24 fn sys_srand(seed
: u32);
25 fn sys_get_processor_count() -> usize;
26 fn sys_malloc(size
: usize, align
: usize) -> *mut u8;
27 fn sys_realloc(ptr
: *mut u8, size
: usize, align
: usize, new_size
: usize) -> *mut u8;
28 fn sys_free(ptr
: *mut u8, size
: usize, align
: usize);
29 fn sys_init_queue(ptr
: usize) -> i32;
30 fn sys_notify(id
: usize, count
: i32) -> i32;
31 fn sys_add_queue(id
: usize, timeout_ns
: i64) -> i32;
32 fn sys_wait(id
: usize) -> i32;
33 fn sys_destroy_queue(id
: usize) -> i32;
34 fn sys_read(fd
: i32, buf
: *mut u8, len
: usize) -> isize;
35 fn sys_write(fd
: i32, buf
: *const u8, len
: usize) -> isize;
36 fn sys_close(fd
: i32) -> i32;
37 fn sys_sem_init(sem
: *mut *const c_void
, value
: u32) -> i32;
38 fn sys_sem_destroy(sem
: *const c_void
) -> i32;
39 fn sys_sem_post(sem
: *const c_void
) -> i32;
40 fn sys_sem_trywait(sem
: *const c_void
) -> i32;
41 fn sys_sem_timedwait(sem
: *const c_void
, ms
: u32) -> i32;
42 fn sys_recmutex_init(recmutex
: *mut *const c_void
) -> i32;
43 fn sys_recmutex_destroy(recmutex
: *const c_void
) -> i32;
44 fn sys_recmutex_lock(recmutex
: *const c_void
) -> i32;
45 fn sys_recmutex_unlock(recmutex
: *const c_void
) -> i32;
46 fn sys_getpid() -> u32;
47 fn sys_exit(arg
: i32) -> !;
49 fn sys_usleep(usecs
: u64);
52 func
: extern "C" fn(usize),
58 func
: extern "C" fn(usize),
64 fn sys_join(id
: Tid
) -> i32;
66 fn sys_clock_gettime(clock_id
: u64, tp
: *mut timespec
) -> i32;
67 fn sys_open(name
: *const i8, flags
: i32, mode
: i32) -> i32;
68 fn sys_unlink(name
: *const i8) -> i32;
69 fn sys_network_init() -> i32;
70 fn sys_block_current_task();
71 fn sys_wakeup_task(tid
: Tid
);
72 fn sys_get_priority() -> u8;
75 /// A thread handle type
78 /// Maximum number of priorities
79 pub const NO_PRIORITIES
: usize = 31;
81 /// Priority of a thread
82 #[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Copy)]
83 pub struct Priority(u8);
86 pub const fn into(self) -> u8 {
90 pub const fn from(x
: u8) -> Self {
95 pub const HIGH_PRIO
: Priority
= Priority
::from(3);
96 pub const NORMAL_PRIO
: Priority
= Priority
::from(2);
97 pub const LOW_PRIO
: Priority
= Priority
::from(1);
99 /// A handle, identifying a socket
100 #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default, Hash)]
101 pub struct Handle(usize);
103 pub const NSEC_PER_SEC
: u64 = 1_000_000_000;
104 pub const CLOCK_REALTIME
: u64 = 1;
105 pub const CLOCK_MONOTONIC
: u64 = 4;
106 pub const STDIN_FILENO
: libc
::c_int
= 0;
107 pub const STDOUT_FILENO
: libc
::c_int
= 1;
108 pub const STDERR_FILENO
: libc
::c_int
= 2;
109 pub const O_RDONLY
: i32 = 0o0;
110 pub const O_WRONLY
: i32 = 0o1;
111 pub const O_RDWR
: i32 = 0o2;
112 pub const O_CREAT
: i32 = 0o100;
113 pub const O_EXCL
: i32 = 0o200;
114 pub const O_TRUNC
: i32 = 0o1000;
115 pub const O_APPEND
: i32 = 0o2000;
117 /// returns true if file descriptor `fd` is a tty
118 pub fn isatty(_fd
: libc
::c_int
) -> bool
{
122 /// initialize the network stack
123 pub fn network_init() -> i32 {
124 unsafe { sys_network_init() }
127 /// `timespec` is used by `clock_gettime` to retrieve the
129 #[derive(Copy, Clone, Debug)]
131 pub struct timespec
{
138 /// Internet protocol version.
139 #[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
146 /// A four-octet IPv4 address.
147 #[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Default)]
148 pub struct Ipv4Address(pub [u8; 4]);
150 /// A sixteen-octet IPv6 address.
151 #[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Default)]
152 pub struct Ipv6Address(pub [u8; 16]);
154 /// An internetworking address.
155 #[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
157 /// An unspecified address.
158 /// May be used as a placeholder for storage where the address is not assigned yet.
166 /// determines the number of activated processors
168 pub unsafe fn get_processor_count() -> usize {
169 sys_get_processor_count()
174 pub unsafe fn malloc(size
: usize, align
: usize) -> *mut u8 {
175 sys_malloc(size
, align
)
180 pub unsafe fn realloc(ptr
: *mut u8, size
: usize, align
: usize, new_size
: usize) -> *mut u8 {
181 sys_realloc(ptr
, size
, align
, new_size
)
186 pub unsafe fn free(ptr
: *mut u8, size
: usize, align
: usize) {
187 sys_free(ptr
, size
, align
)
191 pub unsafe fn notify(id
: usize, count
: i32) -> i32 {
192 sys_notify(id
, count
)
197 pub unsafe fn add_queue(id
: usize, timeout_ns
: i64) -> i32 {
198 sys_add_queue(id
, timeout_ns
)
203 pub unsafe fn wait(id
: usize) -> i32 {
209 pub unsafe fn init_queue(id
: usize) -> i32 {
215 pub unsafe fn destroy_queue(id
: usize) -> i32 {
216 sys_destroy_queue(id
)
219 /// read from a file descriptor
221 /// read() attempts to read `len` bytes of data from the object
222 /// referenced by the descriptor `fd` into the buffer pointed
225 pub unsafe fn read(fd
: i32, buf
: *mut u8, len
: usize) -> isize {
226 sys_read(fd
, buf
, len
)
229 /// write to a file descriptor
231 /// write() attempts to write `len` of data to the object
232 /// referenced by the descriptor `fd` from the
233 /// buffer pointed to by `buf`.
235 pub unsafe fn write(fd
: i32, buf
: *const u8, len
: usize) -> isize {
236 sys_write(fd
, buf
, len
)
239 /// close a file descriptor
241 /// The close() call deletes a file descriptor `fd` from the object
244 pub unsafe fn close(fd
: i32) -> i32 {
248 /// sem_init() initializes the unnamed semaphore at the address
249 /// pointed to by `sem`. The `value` argument specifies the
250 /// initial value for the semaphore.
252 pub unsafe fn sem_init(sem
: *mut *const c_void
, value
: u32) -> i32 {
253 sys_sem_init(sem
, value
)
256 /// sem_destroy() frees the unnamed semaphore at the address
257 /// pointed to by `sem`.
259 pub unsafe fn sem_destroy(sem
: *const c_void
) -> i32 {
263 /// sem_post() increments the semaphore pointed to by `sem`.
264 /// If the semaphore's value consequently becomes greater
265 /// than zero, then another thread blocked in a sem_wait call
266 /// will be woken up and proceed to lock the semaphore.
268 pub unsafe fn sem_post(sem
: *const c_void
) -> i32 {
272 /// try to decrement a semaphore
274 /// sem_trywait() is the same as sem_timedwait(), except that
275 /// if the decrement cannot be immediately performed, then call
276 /// returns a negative value instead of blocking.
278 pub unsafe fn sem_trywait(sem
: *const c_void
) -> i32 {
282 /// decrement a semaphore
284 /// sem_timedwait() decrements the semaphore pointed to by `sem`.
285 /// If the semaphore's value is greater than zero, then the
286 /// the function returns immediately. If the semaphore currently
287 /// has the value zero, then the call blocks until either
288 /// it becomes possible to perform the decrement of the time limit
289 /// to wait for the semaphore is expired. A time limit `ms` of
290 /// means infinity waiting time.
292 pub unsafe fn sem_timedwait(sem
: *const c_void
, ms
: u32) -> i32 {
293 sys_sem_timedwait(sem
, ms
)
298 pub unsafe fn recmutex_init(recmutex
: *mut *const c_void
) -> i32 {
299 sys_recmutex_init(recmutex
)
304 pub unsafe fn recmutex_destroy(recmutex
: *const c_void
) -> i32 {
305 sys_recmutex_destroy(recmutex
)
310 pub unsafe fn recmutex_lock(recmutex
: *const c_void
) -> i32 {
311 sys_recmutex_lock(recmutex
)
316 pub unsafe fn recmutex_unlock(recmutex
: *const c_void
) -> i32 {
317 sys_recmutex_unlock(recmutex
)
320 /// Determines the id of the current thread
322 pub unsafe fn getpid() -> u32 {
326 /// cause normal termination and return `arg`
327 /// to the host system
329 pub unsafe fn exit(arg
: i32) -> ! {
333 /// cause abnormal termination
335 pub unsafe fn abort() -> ! {
339 /// suspend execution for microsecond intervals
341 /// The usleep() function suspends execution of the calling
342 /// thread for (at least) `usecs` microseconds.
344 pub unsafe fn usleep(usecs
: u64) {
348 /// spawn a new thread
350 /// spawn() starts a new thread. The new thread starts execution
351 /// by invoking `func(usize)`; `arg` is passed as the argument
352 /// to `func`. `prio` defines the priority of the new thread,
353 /// which can be between `LOW_PRIO` and `HIGH_PRIO`.
354 /// `core_id` defines the core, where the thread is located.
355 /// A negative value give the operating system the possibility
356 /// to select the core by its own.
360 func
: extern "C" fn(usize),
365 sys_spawn(id
, func
, arg
, prio
, core_id
)
368 /// spawn a new thread with user-specified stack size
370 /// spawn2() starts a new thread. The new thread starts execution
371 /// by invoking `func(usize)`; `arg` is passed as the argument
372 /// to `func`. `prio` defines the priority of the new thread,
373 /// which can be between `LOW_PRIO` and `HIGH_PRIO`.
374 /// `core_id` defines the core, where the thread is located.
375 /// A negative value give the operating system the possibility
376 /// to select the core by its own.
377 /// In contrast to spawn(), spawn2() is able to define the
380 pub unsafe fn spawn2(
381 func
: extern "C" fn(usize),
387 sys_spawn2(func
, arg
, prio
, stack_size
, core_id
)
390 /// join with a terminated thread
392 /// The join() function waits for the thread specified by `id`
395 pub unsafe fn join(id
: Tid
) -> i32 {
399 /// yield the processor
401 /// causes the calling thread to relinquish the CPU. The thread
402 /// is moved to the end of the queue for its static priority.
404 pub unsafe fn yield_now() {
410 /// The clock_gettime() functions allow the calling thread
411 /// to retrieve the value used by a clock which is specified
414 /// `CLOCK_REALTIME`: the system's real time clock,
415 /// expressed as the amount of time since the Epoch.
417 /// `CLOCK_MONOTONIC`: clock that increments monotonically,
418 /// tracking the time since an arbitrary point
420 pub unsafe fn clock_gettime(clock_id
: u64, tp
: *mut timespec
) -> i32 {
421 sys_clock_gettime(clock_id
, tp
)
424 /// open and possibly create a file
426 /// The open() system call opens the file specified by `name`.
427 /// If the specified file does not exist, it may optionally
428 /// be created by open().
430 pub unsafe fn open(name
: *const i8, flags
: i32, mode
: i32) -> i32 {
431 sys_open(name
, flags
, mode
)
434 /// delete the file it refers to `name`
436 pub unsafe fn unlink(name
: *const i8) -> i32 {
440 /// The largest number `rand` will return
441 pub const RAND_MAX
: u64 = 2_147_483_647;
443 /// The function computes a sequence of pseudo-random integers
444 /// in the range of 0 to RAND_MAX
446 pub unsafe fn rand() -> u32 {
450 /// The function sets its argument as the seed for a new sequence
451 /// of pseudo-random numbers to be returned by `rand`
453 pub unsafe fn srand(seed
: u32) {
457 /// Create a cryptographicly secure 32bit random number with the support of
458 /// the underlying hardware. If the required hardware isn't available,
459 /// the function returns `None`.
461 pub unsafe fn secure_rand32() -> Option
<u32> {
465 /// Create a cryptographicly secure 64bit random number with the support of
466 /// the underlying hardware. If the required hardware isn't available,
467 /// the function returns `None`.
469 pub unsafe fn secure_rand64() -> Option
<u64> {
473 /// Add current task to the queue of blocked tasl. After calling `block_current_task`,
474 /// call `yield_now` to switch to another task.
476 pub unsafe fn block_current_task() {
477 sys_block_current_task();
480 /// Wakeup task with the thread id `tid`
482 pub unsafe fn wakeup_task(tid
: Tid
) {
483 sys_wakeup_task(tid
);
486 /// Determine the priority of the current thread
488 pub unsafe fn get_priority() -> Priority
{
489 Priority
::from(sys_get_priority())