hermit_abi/
lib.rs

1//! `hermit-abi` is small interface to call functions from the
2//! [Hermit unikernel](https://github.com/hermit-os/kernel).
3
4#![no_std]
5#![allow(nonstandard_style)]
6#![allow(dead_code)]
7#![allow(clippy::missing_safety_doc)]
8#![allow(clippy::result_unit_err)]
9
10pub mod errno;
11
12use core::ffi::c_char;
13pub use core::ffi::{c_int, c_short, c_void};
14
15pub use self::errno::*;
16
17/// A thread handle type
18pub type Tid = u32;
19
20/// Maximum number of priorities
21pub const NO_PRIORITIES: usize = 31;
22
23/// Priority of a thread
24#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Copy)]
25pub struct Priority(u8);
26
27impl Priority {
28	pub const fn into(self) -> u8 {
29		self.0
30	}
31
32	pub const fn from(x: u8) -> Self {
33		Priority(x)
34	}
35}
36
37pub const HIGH_PRIO: Priority = Priority::from(3);
38pub const NORMAL_PRIO: Priority = Priority::from(2);
39pub const LOW_PRIO: Priority = Priority::from(1);
40
41pub const FUTEX_RELATIVE_TIMEOUT: u32 = 1;
42pub const CLOCK_REALTIME: clockid_t = 1;
43pub const CLOCK_MONOTONIC: clockid_t = 4;
44pub const STDIN_FILENO: c_int = 0;
45pub const STDOUT_FILENO: c_int = 1;
46pub const STDERR_FILENO: c_int = 2;
47pub const O_RDONLY: i32 = 0o0;
48pub const O_WRONLY: i32 = 0o1;
49pub const O_RDWR: i32 = 0o2;
50pub const O_CREAT: i32 = 0o100;
51pub const O_EXCL: i32 = 0o200;
52pub const O_TRUNC: i32 = 0o1000;
53pub const O_APPEND: i32 = 0o2000;
54pub const O_NONBLOCK: i32 = 0o4000;
55pub const O_DIRECTORY: i32 = 0o200000;
56pub const F_DUPFD: i32 = 0;
57pub const F_GETFD: i32 = 1;
58pub const F_SETFD: i32 = 2;
59pub const F_GETFL: i32 = 3;
60pub const F_SETFL: i32 = 4;
61pub const FD_CLOEXEC: i32 = 1;
62
63/// returns true if file descriptor `fd` is a tty
64pub fn isatty(_fd: c_int) -> bool {
65	false
66}
67
68/// `timespec` is used by `clock_gettime` to retrieve the
69/// current time
70#[derive(Default, Copy, Clone, Debug)]
71#[repr(C)]
72pub struct timespec {
73	/// seconds
74	pub tv_sec: time_t,
75	/// nanoseconds
76	pub tv_nsec: i32,
77}
78
79#[repr(C)]
80#[derive(Debug, Copy, Clone)]
81pub struct timeval {
82	pub tv_sec: time_t,
83	pub tv_usec: suseconds_t,
84}
85
86/// The largest number `rand` will return
87pub const RAND_MAX: i32 = 2_147_483_647;
88
89/// Socket address family: IPv4
90pub const AF_INET: i32 = 0;
91/// Socket address family: IPv6
92pub const AF_INET6: i32 = 1;
93/// Socket address family: VSOCK protocol for hypervisor-guest communication
94pub const AF_VSOCK: i32 = 2;
95pub const IPPROTO_IP: i32 = 0;
96pub const IPPROTO_IPV6: i32 = 41;
97pub const IPPROTO_UDP: i32 = 17;
98pub const IPPROTO_TCP: i32 = 6;
99pub const IPV6_ADD_MEMBERSHIP: i32 = 12;
100pub const IPV6_DROP_MEMBERSHIP: i32 = 13;
101pub const IPV6_MULTICAST_LOOP: i32 = 19;
102pub const IPV6_V6ONLY: i32 = 27;
103pub const IP_TOS: i32 = 1;
104pub const IP_TTL: i32 = 2;
105pub const IP_MULTICAST_TTL: i32 = 5;
106pub const IP_MULTICAST_LOOP: i32 = 7;
107pub const IP_ADD_MEMBERSHIP: i32 = 3;
108pub const IP_DROP_MEMBERSHIP: i32 = 4;
109pub const SHUT_RD: i32 = 0;
110pub const SHUT_WR: i32 = 1;
111pub const SHUT_RDWR: i32 = 2;
112/// Socket supports datagrams (connectionless,  unreliable  messages of a fixed maximum length)
113pub const SOCK_DGRAM: i32 = 2;
114/// Socket provides sequenced, reliable,  two-way,  connection-based byte streams.
115pub const SOCK_STREAM: i32 = 1;
116/// Set the O_NONBLOCK file status flag on the open socket
117pub const SOCK_NONBLOCK: i32 = 0o4000;
118/// Set  the  close-on-exec flag on the new socket
119pub const SOCK_CLOEXEC: i32 = 0o40000;
120pub const SOL_SOCKET: i32 = 4095;
121pub const SO_REUSEADDR: i32 = 0x0004;
122pub const SO_KEEPALIVE: i32 = 0x0008;
123pub const SO_BROADCAST: i32 = 0x0020;
124pub const SO_LINGER: i32 = 0x0080;
125pub const SO_SNDBUF: i32 = 0x1001;
126pub const SO_RCVBUF: i32 = 0x1002;
127pub const SO_SNDTIMEO: i32 = 0x1005;
128pub const SO_RCVTIMEO: i32 = 0x1006;
129pub const SO_ERROR: i32 = 0x1007;
130pub const TCP_NODELAY: i32 = 1;
131pub const MSG_PEEK: i32 = 1;
132pub const FIONBIO: i32 = 0x8008667eu32 as i32;
133pub const EAI_AGAIN: i32 = 2;
134pub const EAI_BADFLAGS: i32 = 3;
135pub const EAI_FAIL: i32 = 4;
136pub const EAI_FAMILY: i32 = 5;
137pub const EAI_MEMORY: i32 = 6;
138pub const EAI_NODATA: i32 = 7;
139pub const EAI_NONAME: i32 = 8;
140pub const EAI_SERVICE: i32 = 9;
141pub const EAI_SOCKTYPE: i32 = 10;
142pub const EAI_SYSTEM: i32 = 11;
143pub const EAI_OVERFLOW: i32 = 14;
144pub const POLLIN: i16 = 0x1;
145pub const POLLPRI: i16 = 0x2;
146pub const POLLOUT: i16 = 0x4;
147pub const POLLERR: i16 = 0x8;
148pub const POLLHUP: i16 = 0x10;
149pub const POLLNVAL: i16 = 0x20;
150pub const POLLRDNORM: i16 = 0x040;
151pub const POLLRDBAND: i16 = 0x080;
152pub const POLLWRNORM: i16 = 0x0100;
153pub const POLLWRBAND: i16 = 0x0200;
154pub const POLLRDHUP: i16 = 0x2000;
155pub const EFD_SEMAPHORE: i16 = 0o1;
156pub const EFD_NONBLOCK: i16 = 0o4000;
157pub const EFD_CLOEXEC: i16 = 0o40000;
158pub const IOV_MAX: usize = 1024;
159/// VMADDR_CID_ANY means that any address is possible for binding
160pub const VMADDR_CID_ANY: u32 = u32::MAX;
161pub const VMADDR_CID_HYPERVISOR: u32 = 0;
162pub const VMADDR_CID_LOCAL: u32 = 1;
163pub const VMADDR_CID_HOST: u32 = 2;
164pub type sa_family_t = u8;
165pub type socklen_t = u32;
166pub type in_addr_t = u32;
167pub type in_port_t = u16;
168pub type time_t = i64;
169pub type useconds_t = u32;
170pub type suseconds_t = i32;
171pub type nfds_t = usize;
172pub type sem_t = *const c_void;
173pub type pid_t = i32;
174pub type clockid_t = i32;
175
176#[repr(C)]
177#[derive(Debug, Copy, Clone, Default)]
178pub struct in_addr {
179	pub s_addr: in_addr_t,
180}
181
182#[repr(C, align(4))]
183#[derive(Debug, Copy, Clone, Default)]
184pub struct in6_addr {
185	pub s6_addr: [u8; 16],
186}
187
188#[repr(C)]
189#[derive(Debug, Copy, Clone, Default)]
190pub struct sockaddr {
191	pub sa_len: u8,
192	pub sa_family: sa_family_t,
193	pub sa_data: [c_char; 14],
194}
195
196#[repr(C)]
197#[derive(Debug, Copy, Clone, Default)]
198pub struct sockaddr_vm {
199	pub svm_len: u8,
200	pub svm_family: sa_family_t,
201	pub svm_reserved1: u16,
202	pub svm_port: u32,
203	pub svm_cid: u32,
204	pub svm_zero: [u8; 4],
205}
206
207#[repr(C)]
208#[derive(Debug, Copy, Clone, Default)]
209pub struct sockaddr_in {
210	pub sin_len: u8,
211	pub sin_family: sa_family_t,
212	pub sin_port: in_port_t,
213	pub sin_addr: in_addr,
214	pub sin_zero: [c_char; 8],
215}
216
217#[repr(C)]
218#[derive(Debug, Copy, Clone, Default)]
219pub struct sockaddr_in6 {
220	pub sin6_len: u8,
221	pub sin6_family: sa_family_t,
222	pub sin6_port: in_port_t,
223	pub sin6_flowinfo: u32,
224	pub sin6_addr: in6_addr,
225	pub sin6_scope_id: u32,
226}
227
228#[repr(C)]
229#[derive(Debug, Copy, Clone)]
230pub struct addrinfo {
231	pub ai_flags: i32,
232	pub ai_family: i32,
233	pub ai_socktype: i32,
234	pub ai_protocol: i32,
235	pub ai_addrlen: socklen_t,
236	pub ai_canonname: *mut c_char,
237	pub ai_addr: *mut sockaddr,
238	pub ai_next: *mut addrinfo,
239}
240
241#[repr(C)]
242#[derive(Debug, Copy, Clone)]
243pub struct sockaddr_storage {
244	pub s2_len: u8,
245	pub ss_family: sa_family_t,
246	__ss_pad1: [u8; 6],
247	__ss_align: i64,
248	__ss_pad2: [u8; 112],
249}
250
251#[repr(C)]
252#[derive(Debug, Copy, Clone, Default)]
253pub struct ip_mreq {
254	pub imr_multiaddr: in_addr,
255	pub imr_interface: in_addr,
256}
257
258#[repr(C)]
259#[derive(Debug, Copy, Clone, Default)]
260pub struct ipv6_mreq {
261	pub ipv6mr_multiaddr: in6_addr,
262	pub ipv6mr_interface: u32,
263}
264
265#[repr(C)]
266#[derive(Debug, Copy, Clone, Default)]
267pub struct linger {
268	pub l_onoff: i32,
269	pub l_linger: i32,
270}
271
272#[repr(C)]
273#[derive(Debug, Copy, Clone, Default)]
274pub struct pollfd {
275	/// file descriptor
276	pub fd: i32,
277	/// events to look for
278	pub events: i16,
279	/// events returned
280	pub revents: i16,
281}
282
283#[repr(C)]
284#[derive(Debug, Default, Copy, Clone)]
285pub struct stat {
286	pub st_dev: u64,
287	pub st_ino: u64,
288	pub st_nlink: u64,
289	/// access permissions
290	pub st_mode: u32,
291	/// user id
292	pub st_uid: u32,
293	/// group id
294	pub st_gid: u32,
295	/// device id
296	pub st_rdev: u64,
297	/// size in bytes
298	pub st_size: i64,
299	/// block size
300	pub st_blksize: i64,
301	/// size in blocks
302	pub st_blocks: i64,
303	/// time of last access
304	pub st_atim: timespec,
305	/// time of last modification
306	pub st_mtim: timespec,
307	/// time of last status change
308	pub st_ctim: timespec,
309}
310
311#[repr(C)]
312#[derive(Debug, Clone, Copy)]
313pub struct dirent64 {
314	/// 64-bit inode number
315	pub d_ino: u64,
316	/// 64-bit offset to next structure
317	pub d_off: i64,
318	/// Size of this dirent
319	pub d_reclen: u16,
320	/// File type
321	pub d_type: u8,
322	/// Filename (null-terminated)
323	pub d_name: [c_char; 256],
324}
325
326#[repr(C)]
327#[derive(Debug, Clone, Copy)]
328/// Describes  a  region  of  memory, beginning at `iov_base` address and with the size of `iov_len` bytes.
329pub struct iovec {
330	/// Starting address
331	pub iov_base: *mut c_void,
332	/// Size of the memory pointed to by iov_base.
333	pub iov_len: usize,
334}
335
336pub const DT_UNKNOWN: u8 = 0;
337pub const DT_FIFO: u8 = 1;
338pub const DT_CHR: u8 = 2;
339pub const DT_DIR: u8 = 4;
340pub const DT_BLK: u8 = 6;
341pub const DT_REG: u8 = 8;
342pub const DT_LNK: u8 = 10;
343pub const DT_SOCK: u8 = 12;
344pub const DT_WHT: u8 = 14;
345
346pub const S_IFIFO: u32 = 0o1_0000;
347pub const S_IFCHR: u32 = 0o2_0000;
348pub const S_IFBLK: u32 = 0o6_0000;
349pub const S_IFDIR: u32 = 0o4_0000;
350pub const S_IFREG: u32 = 0o10_0000;
351pub const S_IFLNK: u32 = 0o12_0000;
352pub const S_IFSOCK: u32 = 0o14_0000;
353pub const S_IFMT: u32 = 0o17_0000;
354
355/// Pages may not be accessed.
356pub const PROT_NONE: u32 = 0;
357/// Indicates that the memory region should be readable.
358pub const PROT_READ: u32 = 1 << 0;
359/// Indicates that the memory region should be writable.
360pub const PROT_WRITE: u32 = 1 << 1;
361/// Indicates that the memory region should be executable.
362pub const PROT_EXEC: u32 = 1 << 2;
363
364/// The file offset is set to offset bytes.
365pub const SEEK_SET: i32 = 0;
366/// The file offset is set to its current location plus offset bytes.
367pub const SEEK_CUR: i32 = 1;
368/// The file offset is set to the size of the file plus offset bytes.
369pub const SEEK_END: i32 = 2;
370
371// symbols, which are part of the library operating system
372extern "C" {
373	/// Get the last error number from the thread local storage
374	#[link_name = "sys_get_errno"]
375	pub fn get_errno() -> i32;
376
377	/// Get the last error number from the thread local storage
378	#[link_name = "sys_errno"]
379	pub fn errno() -> i32;
380
381	/// Get memory page size
382	#[link_name = "sys_getpagesize"]
383	pub fn getpagesize() -> i32;
384
385	/// Creates a new virtual memory mapping of the `size` specified with
386	/// protection bits specified in `prot_flags`.
387	#[link_name = "sys_mmap"]
388	pub fn mmap(size: usize, prot_flags: u32, ret: &mut *mut u8) -> i32;
389
390	/// Unmaps memory at the specified `ptr` for `size` bytes.
391	#[link_name = "sys_munmap"]
392	pub fn munmap(ptr: *mut u8, size: usize) -> i32;
393
394	/// Configures the protections associated with a region of virtual memory
395	/// starting at `ptr` and going to `size`.
396	///
397	/// Returns 0 on success and an error code on failure.
398	#[link_name = "sys_mprotect"]
399	pub fn mprotect(ptr: *mut u8, size: usize, prot_flags: u32) -> i32;
400
401	/// If the value at address matches the expected value, park the current thread until it is either
402	/// woken up with [`futex_wake`] (returns 0) or an optional timeout elapses (returns -ETIMEDOUT).
403	///
404	/// Setting `timeout` to null means the function will only return if [`futex_wake`] is called.
405	/// Otherwise, `timeout` is interpreted as an absolute time measured with [`CLOCK_MONOTONIC`].
406	/// If [`FUTEX_RELATIVE_TIMEOUT`] is set in `flags` the timeout is understood to be relative
407	/// to the current time.
408	///
409	/// Returns -EINVAL if `address` is null, the timeout is negative or `flags` contains unknown values.
410	#[link_name = "sys_futex_wait"]
411	pub fn futex_wait(
412		address: *mut u32,
413		expected: u32,
414		timeout: *const timespec,
415		flags: u32,
416	) -> i32;
417
418	/// Wake `count` threads waiting on the futex at `address`. Returns the number of threads
419	/// woken up (saturates to `i32::MAX`). If `count` is `i32::MAX`, wake up all matching
420	/// waiting threads. If `count` is negative or `address` is null, returns -EINVAL.
421	#[link_name = "sys_futex_wake"]
422	pub fn futex_wake(address: *mut u32, count: i32) -> i32;
423
424	/// sem_init() initializes the unnamed semaphore at the address
425	/// pointed to by `sem`.  The `value` argument specifies the
426	/// initial value for the semaphore. If `pshared` is nonzero,
427	/// then the semaphore is shared between processes (currently
428	/// not supported).
429	#[link_name = "sys_sem_init"]
430	pub fn sem_init(sem: *mut sem_t, pshared: i32, value: u32) -> i32;
431
432	/// sem_destroy() frees the unnamed semaphore at the address
433	/// pointed to by `sem`.
434	#[link_name = "sys_sem_destroy"]
435	pub fn sem_destroy(sem: *mut sem_t) -> i32;
436
437	/// sem_post() increments the semaphore pointed to by `sem`.
438	/// If the semaphore's value consequently becomes greater
439	/// than zero, then another thread blocked in a sem_wait call
440	/// will be woken up and proceed to lock the semaphore.
441	#[link_name = "sys_sem_post"]
442	pub fn sem_post(sem: *mut sem_t) -> i32;
443
444	/// try to decrement a semaphore
445	///
446	/// sem_trywait() is the same as sem_timedwait(), except that
447	/// if the  decrement cannot be immediately performed, then  call
448	/// returns a negative value instead of blocking.
449	#[link_name = "sys_sem_trywait"]
450	pub fn sem_trywait(sem: *mut sem_t) -> i32;
451
452	/// decrement a semaphore
453	///
454	/// sem_timedwait() decrements the semaphore pointed to by `sem`.
455	/// If the semaphore's value is greater than zero, then the
456	/// the function returns immediately. If the semaphore currently
457	/// has the value zero, then the call blocks until either
458	/// it becomes possible to perform the decrement of the time limit
459	/// to wait for the semaphore is expired. A time limit `ms` of
460	/// means infinity waiting time.
461	#[link_name = "sys_sem_timedwait"]
462	pub fn sem_timedwait(sem: *mut sem_t, abs_timeout: *const timespec) -> i32;
463
464	/// Determines the id of the current thread
465	#[link_name = "sys_getpid"]
466	pub fn getpid() -> pid_t;
467
468	/// cause normal termination and return `status`
469	/// to the host system
470	#[link_name = "sys_exit"]
471	pub fn exit(status: i32) -> !;
472
473	/// cause abnormal termination
474	#[link_name = "sys_abort"]
475	pub fn abort() -> !;
476
477	/// suspend execution for microsecond intervals
478	///
479	/// The usleep() function suspends execution of the calling
480	/// thread for (at least) `usecs` microseconds.
481	#[link_name = "sys_usleep"]
482	pub fn usleep(usecs: u64);
483
484	/// suspend thread execution for an interval measured in nanoseconds
485	#[link_name = "sys_nanosleep"]
486	pub fn nanosleep(req: *const timespec) -> i32;
487
488	/// spawn a new thread
489	///
490	/// spawn() starts a new thread. The new thread starts execution
491	/// by invoking `func(usize)`; `arg` is passed as the argument
492	/// to `func`. `prio` defines the priority of the new thread,
493	/// which can be between `LOW_PRIO` and `HIGH_PRIO`.
494	/// `core_id` defines the core, where the thread is located.
495	/// A negative value give the operating system the possibility
496	/// to select the core by its own.
497	#[link_name = "sys_spawn"]
498	pub fn spawn(
499		id: *mut Tid,
500		func: extern "C" fn(usize),
501		arg: usize,
502		prio: u8,
503		core_id: isize,
504	) -> i32;
505
506	/// spawn a new thread with user-specified stack size
507	///
508	/// spawn2() starts a new thread. The new thread starts execution
509	/// by invoking `func(usize)`; `arg` is passed as the argument
510	/// to `func`. `prio` defines the priority of the new thread,
511	/// which can be between `LOW_PRIO` and `HIGH_PRIO`.
512	/// `core_id` defines the core, where the thread is located.
513	/// A negative value give the operating system the possibility
514	/// to select the core by its own.
515	/// In contrast to spawn(), spawn2() is able to define the
516	/// stack size.
517	#[link_name = "sys_spawn2"]
518	pub fn spawn2(
519		func: extern "C" fn(usize),
520		arg: usize,
521		prio: u8,
522		stack_size: usize,
523		core_id: isize,
524	) -> Tid;
525
526	/// join with a terminated thread
527	///
528	/// The join() function waits for the thread specified by `id`
529	/// to terminate.
530	#[link_name = "sys_join"]
531	pub fn join(id: Tid) -> i32;
532
533	/// yield the processor
534	///
535	/// causes the calling thread to relinquish the CPU. The thread
536	/// is moved to the end of the queue for its static priority.
537	#[link_name = "sys_yield"]
538	pub fn yield_now();
539
540	/// get current time
541	///
542	/// The clock_gettime() functions allow the calling thread
543	/// to retrieve the value used by a clock which is specified
544	/// by `clockid`.
545	///
546	/// `CLOCK_REALTIME`: the system's real time clock,
547	/// expressed as the amount of time since the Epoch.
548	///
549	/// `CLOCK_MONOTONIC`: clock that increments monotonically,
550	/// tracking the time since an arbitrary point
551	#[link_name = "sys_clock_gettime"]
552	pub fn clock_gettime(clockid: clockid_t, tp: *mut timespec) -> i32;
553
554	/// open and possibly create a file
555	///
556	/// The open() system call opens the file specified by `name`.
557	/// If the specified file does not exist, it may optionally
558	/// be created by open().
559	#[link_name = "sys_open"]
560	pub fn open(name: *const c_char, flags: i32, mode: i32) -> i32;
561
562	/// open a directory
563	///
564	/// The opendir() system call opens the directory specified by `name`.
565	#[deprecated(since = "0.4.0", note = "please use `open`")]
566	#[link_name = "sys_opendir"]
567	pub fn opendir(name: *const c_char) -> i32;
568
569	/// delete the file it refers to `name`
570	#[link_name = "sys_unlink"]
571	pub fn unlink(name: *const c_char) -> i32;
572
573	/// remove directory it refers to `name`
574	#[link_name = "sys_rmdir"]
575	pub fn rmdir(name: *const c_char) -> i32;
576
577	/// stat
578	#[link_name = "sys_stat"]
579	pub fn stat(name: *const c_char, stat: *mut stat) -> i32;
580
581	/// lstat
582	#[link_name = "sys_lstat"]
583	pub fn lstat(name: *const c_char, stat: *mut stat) -> i32;
584
585	/// fstat
586	#[link_name = "sys_fstat"]
587	pub fn fstat(fd: i32, stat: *mut stat) -> i32;
588
589	/// Returns an estimate of the default amount of parallelism
590	/// a program should use. This number often corresponds to the
591	/// amount of CPUs a computer has, but it may diverge in
592	/// various cases.
593	#[link_name = "sys_available_parallelism"]
594	pub fn available_parallelism() -> usize;
595
596	/// determines the number of activated processors
597	#[deprecated(since = "0.4.0", note = "please use `available_parallelism`")]
598	#[link_name = "sys_get_processor_count"]
599	pub fn get_processor_count() -> usize;
600
601	#[link_name = "sys_malloc"]
602	pub fn malloc(size: usize, align: usize) -> *mut u8;
603
604	#[link_name = "sys_alloc"]
605	pub fn alloc(size: usize, align: usize) -> *mut u8;
606
607	#[link_name = "sys_alloc_zeroed"]
608	pub fn alloc_zeroed(size: usize, align: usize) -> *mut u8;
609
610	#[link_name = "sys_realloc"]
611	pub fn realloc(ptr: *mut u8, size: usize, align: usize, new_size: usize) -> *mut u8;
612
613	#[link_name = "sys_free"]
614	pub fn free(ptr: *mut u8, size: usize, align: usize);
615
616	#[link_name = "sys_dealloc"]
617	pub fn dealloc(ptr: *mut u8, size: usize, align: usize);
618
619	#[link_name = "sys_notify"]
620	pub fn notify(id: usize, count: i32) -> i32;
621
622	#[doc(hidden)]
623	#[link_name = "sys_add_queue"]
624	pub fn add_queue(id: usize, timeout_ns: i64) -> i32;
625
626	#[doc(hidden)]
627	#[link_name = "sys_wait"]
628	pub fn wait(id: usize) -> i32;
629
630	#[doc(hidden)]
631	#[link_name = "sys_init_queue"]
632	pub fn init_queue(id: usize) -> i32;
633
634	#[doc(hidden)]
635	#[link_name = "sys_destroy_queue"]
636	pub fn destroy_queue(id: usize) -> i32;
637
638	/// initialize the network stack
639	#[link_name = "sys_network_init"]
640	pub fn network_init() -> i32;
641
642	/// Add current task to the queue of blocked tasks. After calling `block_current_task`,
643	/// call `yield_now` to switch to another task.
644	#[link_name = "sys_block_current_task"]
645	pub fn block_current_task();
646
647	/// Add current task to the queue of blocked tasks, but wake it when `timeout` milliseconds
648	/// have elapsed.
649	///
650	/// After calling `block_current_task`, call `yield_now` to switch to another task.
651	#[link_name = "sys_block_current_task_with_timeout"]
652	pub fn block_current_task_with_timeout(timeout: u64);
653
654	/// Wakeup task with the thread id `tid`
655	#[link_name = "sys_wakeup_taskt"]
656	pub fn wakeup_task(tid: Tid);
657
658	/// The system call `getaddrbyname` determine the network host entry.
659	/// It expects an array of u8 with a size of in_addr or of in6_addr.
660	/// The result of the DNS request will be stored in this array.
661	///
662	/// # Example
663	///
664	/// ```
665	/// use hermit_abi::in_addr;
666	/// let c_string = std::ffi::CString::new("rust-lang.org").expect("CString::new failed");
667	/// let name = c_string.into_raw();
668	/// let mut inaddr: in_addr = Default::default();
669	/// let _ = unsafe {
670	///         hermit_abi::getaddrbyname(
671	///                 name,
672	///                 &mut inaddr as *mut _ as *mut u8,
673	///                 std::mem::size_of::<in_addr>(),
674	///         )
675	/// };
676	///
677	/// // retake pointer to free memory
678	/// let _ = CString::from_raw(name);
679	/// ```
680	#[link_name = "sys_getaddrbyname"]
681	pub fn getaddrbyname(name: *const c_char, inaddr: *mut u8, len: usize) -> i32;
682
683	#[link_name = "sys_accept"]
684	pub fn accept(s: i32, addr: *mut sockaddr, addrlen: *mut socklen_t) -> i32;
685
686	/// bind a name to a socket
687	#[link_name = "sys_bind"]
688	pub fn bind(s: i32, name: *const sockaddr, namelen: socklen_t) -> i32;
689
690	#[link_name = "sys_connect"]
691	pub fn connect(s: i32, name: *const sockaddr, namelen: socklen_t) -> i32;
692
693	/// read from a file descriptor
694	///
695	/// read() attempts to read `len` bytes of data from the object
696	/// referenced by the descriptor `fd` into the buffer pointed
697	/// to by `buf`.
698	#[link_name = "sys_read"]
699	pub fn read(fd: i32, buf: *mut u8, len: usize) -> isize;
700
701	/// `read()` attempts to read `nbyte` of data to the object referenced by the
702	/// descriptor `fd` from a buffer. `read()` performs the same
703	/// action, but scatters the input data from the `iovcnt` buffers specified by the
704	/// members of the iov array: `iov[0], iov[1], ..., iov[iovcnt-1]`.
705	///
706	/// ```
707	/// struct iovec {
708	///     char   *iov_base;  /* Base address. */
709	///     size_t iov_len;    /* Length. */
710	/// };
711	/// ```
712	///
713	/// Each `iovec` entry specifies the base address and length of an area in memory from
714	/// which data should be written.  `readv()` will always fill an completely
715	/// before proceeding to the next.
716	#[link_name = "sys_readv"]
717	pub fn readv(fd: i32, iov: *const iovec, iovcnt: usize) -> isize;
718
719	/// `getdents64` reads directory entries from the directory referenced
720	/// by the file descriptor `fd` into the buffer pointed to by `buf`.
721	#[link_name = "sys_getdents64"]
722	pub fn getdents64(fd: i32, dirp: *mut dirent64, count: usize) -> i64;
723
724	/// 'mkdir' attempts to create a directory,
725	/// it returns 0 on success and -1 on error
726	#[link_name = "sys_mkdir"]
727	pub fn mkdir(name: *const i8, mode: u32) -> i32;
728
729	/// Fill `len` bytes in `buf` with cryptographically secure random data.
730	///
731	/// Returns either the number of bytes written to buf (a positive value) or
732	/// * `-EINVAL` if `flags` contains unknown flags.
733	/// * `-ENOSYS` if the system does not support random data generation.
734	#[link_name = "sys_read_entropy"]
735	pub fn read_entropy(buf: *mut u8, len: usize, flags: u32) -> isize;
736
737	/// receive() a message from a socket
738	#[link_name = "sys_recv"]
739	pub fn recv(socket: i32, buf: *mut u8, len: usize, flags: i32) -> isize;
740
741	/// receive() a message from a socket
742	#[link_name = "sys_recvfrom"]
743	pub fn recvfrom(
744		socket: i32,
745		buf: *mut u8,
746		len: usize,
747		flags: i32,
748		addr: *mut sockaddr,
749		addrlen: *mut socklen_t,
750	) -> isize;
751
752	/// The fseek() function sets the file position indicator for the stream pointed to by stream.
753	/// The new position, measured in bytes, is obtained by adding offset bytes to the position
754	/// specified by whence.  If whence is set to SEEK_SET, SEEK_CUR, or SEEK_END, the offset is
755	/// relative to the start of the file, the current position indicator, or end-of-file,
756	/// respectively.
757	#[link_name = "sys_lseek"]
758	pub fn lseek(fd: i32, offset: isize, whence: i32) -> isize;
759
760	/// write to a file descriptor
761	///
762	/// write() attempts to write `len` of data to the object
763	/// referenced by the descriptor `fd` from the
764	/// buffer pointed to by `buf`.
765	#[link_name = "sys_write"]
766	pub fn write(fd: i32, buf: *const u8, len: usize) -> isize;
767
768	/// `write()` attempts to write `nbyte` of data to the object referenced by the
769	/// descriptor `fd` from a buffer. `writev()` performs the same
770	/// action, but gathers the output data from the `iovcnt` buffers specified by the
771	/// members of the iov array: `iov[0], iov[1], ..., iov[iovcnt-1]`.
772	///
773	/// ```
774	/// struct iovec {
775	///     char   *iov_base;  /* Base address. */
776	///     size_t iov_len;    /* Length. */
777	/// };
778	/// ```
779	///
780	/// Each `iovec` entry specifies the base address and length of an area in memory from
781	/// which data should be written.  `writev()` will always write a
782	/// complete area before proceeding to the next.
783	#[link_name = "sys_writev"]
784	pub fn writev(fd: i32, iov: *const iovec, iovcnt: usize) -> isize;
785
786	/// close a file descriptor
787	///
788	/// The close() call deletes a file descriptor `fd` from the object
789	/// reference table.
790	#[link_name = "sys_close"]
791	pub fn close(fd: i32) -> i32;
792
793	/// duplicate an existing file descriptor
794	#[link_name = "sys_dup"]
795	pub fn dup(fd: i32) -> i32;
796
797	#[link_name = "sys_getpeername"]
798	pub fn getpeername(s: i32, name: *mut sockaddr, namelen: *mut socklen_t) -> i32;
799
800	#[link_name = "sys_getsockname"]
801	pub fn getsockname(s: i32, name: *mut sockaddr, namelen: *mut socklen_t) -> i32;
802
803	#[link_name = "sys_getsockopt"]
804	pub fn getsockopt(
805		s: i32,
806		level: i32,
807		optname: i32,
808		optval: *mut c_void,
809		optlen: *mut socklen_t,
810	) -> i32;
811
812	#[link_name = "sys_setsockopt"]
813	pub fn setsockopt(
814		s: i32,
815		level: i32,
816		optname: i32,
817		optval: *const c_void,
818		optlen: socklen_t,
819	) -> i32;
820
821	#[link_name = "sys_ioctl"]
822	pub fn ioctl(s: i32, cmd: i32, argp: *mut c_void) -> i32;
823
824	#[link_name = "sys_fcntl"]
825	pub fn fcntl(fd: i32, cmd: i32, arg: i32) -> i32;
826
827	/// `eventfd` creates an linux-like "eventfd object" that can be used
828	/// as an event wait/notify mechanism by user-space applications, and by
829	/// the kernel to notify user-space applications of events. The
830	/// object contains an unsigned 64-bit integer counter
831	/// that is maintained by the kernel. This counter is initialized
832	/// with the value specified in the argument `initval`.
833	///
834	/// As its return value, `eventfd` returns a new file descriptor that
835	/// can be used to refer to the eventfd object.
836	///
837	/// The following values may be bitwise set in flags to change the
838	/// behavior of `eventfd`:
839	///
840	/// `EFD_NONBLOCK`: Set the file descriptor in non-blocking mode
841	/// `EFD_SEMAPHORE`: Provide semaphore-like semantics for reads
842	/// from the new file descriptor.
843	#[link_name = "sys_eventfd"]
844	pub fn eventfd(initval: u64, flags: i16) -> i32;
845
846	/// The unix-like `poll` waits for one of a set of file descriptors
847	/// to become ready to perform I/O. The set of file descriptors to be
848	/// monitored is specified in the `fds` argument, which is an array
849	/// of structures of `pollfd`.
850	#[link_name = "sys_poll"]
851	pub fn poll(fds: *mut pollfd, nfds: nfds_t, timeout: i32) -> i32;
852
853	/// listen for connections on a socket
854	///
855	/// The `backlog` parameter defines the maximum length for the queue of pending
856	/// connections. Currently, the `backlog` must be one.
857	#[link_name = "sys_listen"]
858	pub fn listen(s: i32, backlog: i32) -> i32;
859
860	#[link_name = "sys_send"]
861	pub fn send(s: i32, mem: *const c_void, len: usize, flags: i32) -> isize;
862
863	#[link_name = "sys_sendto"]
864	pub fn sendto(
865		s: i32,
866		mem: *const c_void,
867		len: usize,
868		flags: i32,
869		to: *const sockaddr,
870		tolen: socklen_t,
871	) -> isize;
872
873	/// shut down part of a full-duplex connection
874	#[link_name = "sys_shutdown"]
875	pub fn shutdown(sockfd: i32, how: i32) -> i32;
876
877	#[deprecated(since = "0.4.0", note = "use `shutdown` instead")]
878	#[link_name = "sys_shutdown_socket"]
879	pub fn shutdown_socket(s: i32, how: i32) -> i32;
880
881	#[link_name = "sys_socket"]
882	pub fn socket(domain: i32, type_: i32, protocol: i32) -> i32;
883
884	#[link_name = "sys_freeaddrinfo"]
885	pub fn freeaddrinfo(ai: *mut addrinfo);
886
887	#[link_name = "sys_getaddrinfo"]
888	pub fn getaddrinfo(
889		nodename: *const c_char,
890		servname: *const c_char,
891		hints: *const addrinfo,
892		res: *mut *mut addrinfo,
893	) -> i32;
894
895	fn sys_get_priority() -> u8;
896	fn sys_set_priority(tid: Tid, prio: u8);
897}
898
899/// Determine the priority of the current thread
900#[inline(always)]
901pub unsafe fn get_priority() -> Priority {
902	Priority::from(sys_get_priority())
903}
904
905/// Determine the priority of the current thread
906#[inline(always)]
907pub unsafe fn set_priority(tid: Tid, prio: Priority) {
908	sys_set_priority(tid, prio.into());
909}