Skip to content

Commit 0fc9e92

Browse files
committed
stablize interface to kernel
In case, that POSIX provides similiar system calls, HermitOS uses variants of these system calls. These similiarties should increase the readability of the interface.
1 parent 20e3f32 commit 0fc9e92

File tree

3 files changed

+88
-62
lines changed

3 files changed

+88
-62
lines changed

hermit-abi/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "hermit-abi"
3-
version = "0.3.9"
3+
version = "0.4.0"
44
authors = ["Stefan Lankes"]
55
license = "MIT OR Apache-2.0"
66
edition = "2021"

hermit-abi/src/lib.rs

Lines changed: 86 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub mod errno;
1010
pub mod tcplistener;
1111
pub mod tcpstream;
1212

13+
use core::ffi::c_char;
1314
pub use core::ffi::{c_int, c_short, c_void};
1415

1516
pub use self::errno::*;
@@ -42,7 +43,6 @@ pub const LOW_PRIO: Priority = Priority::from(1);
4243
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default, Hash)]
4344
pub struct Handle(usize);
4445

45-
pub const NSEC_PER_SEC: u64 = 1_000_000_000;
4646
pub const FUTEX_RELATIVE_TIMEOUT: u32 = 1;
4747
pub const CLOCK_REALTIME: u64 = 1;
4848
pub const CLOCK_MONOTONIC: u64 = 4;
@@ -75,9 +75,16 @@ pub fn isatty(_fd: c_int) -> bool {
7575
#[repr(C)]
7676
pub struct timespec {
7777
/// seconds
78-
pub tv_sec: i64,
78+
pub tv_sec: time_t,
7979
/// nanoseconds
80-
pub tv_nsec: i64,
80+
pub tv_nsec: u32,
81+
}
82+
83+
#[repr(C)]
84+
#[derive(Debug, Copy, Clone)]
85+
pub struct timeval {
86+
pub tv_sec: time_t,
87+
pub tv_usec: suseconds_t,
8188
}
8289

8390
/// Internet protocol version.
@@ -109,7 +116,7 @@ pub enum IpAddress {
109116
}
110117

111118
/// The largest number `rand` will return
112-
pub const RAND_MAX: u64 = 2_147_483_647;
119+
pub const RAND_MAX: i32 = 2_147_483_647;
113120

114121
pub const AF_INET: i32 = 0;
115122
pub const AF_INET6: i32 = 1;
@@ -173,14 +180,17 @@ pub type in_port_t = u16;
173180
pub type time_t = i64;
174181
pub type suseconds_t = i64;
175182
pub type nfds_t = usize;
183+
pub type sem_t = *const c_void;
184+
pub type pid_t = i32;
185+
pub type clockid_t = i32;
176186

177187
#[repr(C)]
178188
#[derive(Debug, Copy, Clone)]
179189
pub struct in_addr {
180-
pub s_addr: u32,
190+
pub s_addr: in_addr_t,
181191
}
182192

183-
#[repr(C)]
193+
#[repr(C, align(4))]
184194
#[derive(Debug, Copy, Clone)]
185195
pub struct in6_addr {
186196
pub s6_addr: [u8; 16],
@@ -207,10 +217,11 @@ pub struct sockaddr_in {
207217
#[repr(C)]
208218
#[derive(Debug, Copy, Clone)]
209219
pub struct sockaddr_in6 {
220+
pub sin6_len: u8,
210221
pub sin6_family: sa_family_t,
211-
pub sin6_port: u16,
212-
pub sin6_addr: in6_addr,
222+
pub sin6_port: in_port_t,
213223
pub sin6_flowinfo: u32,
224+
pub sin6_addr: in6_addr,
214225
pub sin6_scope_id: u32,
215226
}
216227

@@ -222,8 +233,8 @@ pub struct addrinfo {
222233
pub ai_socktype: i32,
223234
pub ai_protocol: i32,
224235
pub ai_addrlen: socklen_t,
225-
pub ai_addr: *mut sockaddr,
226236
pub ai_canonname: *mut u8,
237+
pub ai_addr: *mut sockaddr,
227238
pub ai_next: *mut addrinfo,
228239
}
229240

@@ -232,8 +243,9 @@ pub struct addrinfo {
232243
pub struct sockaddr_storage {
233244
pub s2_len: u8,
234245
pub ss_family: sa_family_t,
235-
pub s2_data1: [i8; 2usize],
236-
pub s2_data2: [u32; 3usize],
246+
__ss_pad1: [u8; 6],
247+
__ss_align: i64,
248+
__ss_pad2: [u8; 112],
237249
}
238250

239251
#[repr(C)]
@@ -257,13 +269,6 @@ pub struct linger {
257269
pub l_linger: i32,
258270
}
259271

260-
#[repr(C)]
261-
#[derive(Debug, Copy, Clone)]
262-
pub struct timeval {
263-
pub tv_sec: time_t,
264-
pub tv_usec: suseconds_t,
265-
}
266-
267272
#[repr(C)]
268273
#[derive(Debug, Copy, Clone)]
269274
pub struct pollfd {
@@ -296,14 +301,14 @@ pub struct stat {
296301
/// size in blocks
297302
pub st_blocks: i64,
298303
/// time of last access
299-
pub st_atime: u64,
300-
pub st_atime_nsec: u64,
304+
pub st_atime: time_t,
305+
pub st_atime_nsec: u32,
301306
/// time of last modification
302-
pub st_mtime: u64,
303-
pub st_mtime_nsec: u64,
307+
pub st_mtime: time_t,
308+
pub st_mtime_nsec: u32,
304309
/// time of last status change
305-
pub st_ctime: u64,
306-
pub st_ctime_nsec: u64,
310+
pub st_ctime: time_t,
311+
pub st_ctime_nsec: u32,
307312
}
308313

309314
#[repr(C)]
@@ -318,30 +323,38 @@ pub struct dirent64 {
318323
/// File type
319324
pub d_type: u8,
320325
/// Filename (null-terminated)
321-
pub d_name: core::marker::PhantomData<u8>,
326+
pub d_name: [u8; 256],
322327
}
323328

324-
pub const DT_UNKNOWN: u32 = 0;
325-
pub const DT_FIFO: u32 = 1;
326-
pub const DT_CHR: u32 = 2;
327-
pub const DT_DIR: u32 = 4;
328-
pub const DT_BLK: u32 = 6;
329-
pub const DT_REG: u32 = 8;
330-
pub const DT_LNK: u32 = 10;
331-
pub const DT_SOCK: u32 = 12;
332-
pub const DT_WHT: u32 = 14;
333-
334-
pub const S_IFDIR: u32 = 0x4000;
335-
pub const S_IFREG: u32 = 0x8000;
336-
pub const S_IFLNK: u32 = 0xA000;
337-
pub const S_IFMT: u32 = 0xF000;
338-
339-
// sysmbols, which are part of the library operating system
329+
pub const DT_UNKNOWN: u8 = 0;
330+
pub const DT_FIFO: u8 = 1;
331+
pub const DT_CHR: u8 = 2;
332+
pub const DT_DIR: u8 = 4;
333+
pub const DT_BLK: u8 = 6;
334+
pub const DT_REG: u8 = 8;
335+
pub const DT_LNK: u8 = 10;
336+
pub const DT_SOCK: u8 = 12;
337+
pub const DT_WHT: u8 = 14;
338+
339+
pub const S_IFIFO: u32 = 0o1_0000;
340+
pub const S_IFCHR: u32 = 0o2_0000;
341+
pub const S_IFBLK: u32 = 0o6_0000;
342+
pub const S_IFDIR: u32 = 0o4_0000;
343+
pub const S_IFREG: u32 = 0o10_0000;
344+
pub const S_IFLNK: u32 = 0o12_0000;
345+
pub const S_IFSOCK: u32 = 0o14_0000;
346+
pub const S_IFMT: u32 = 0o17_0000;
347+
348+
// symbols, which are part of the library operating system
340349
extern "C" {
341350
/// Get the last error number from the thread local storage
342351
#[link_name = "sys_get_errno"]
343352
pub fn get_errno() -> i32;
344353

354+
/// Get the last error number from the thread local storage
355+
#[link_name = "sys_errno"]
356+
pub fn errno() -> i32;
357+
345358
/// If the value at address matches the expected value, park the current thread until it is either
346359
/// woken up with [`futex_wake`] (returns 0) or an optional timeout elapses (returns -ETIMEDOUT).
347360
///
@@ -367,29 +380,31 @@ extern "C" {
367380

368381
/// sem_init() initializes the unnamed semaphore at the address
369382
/// pointed to by `sem`. The `value` argument specifies the
370-
/// initial value for the semaphore.
383+
/// initial value for the semaphore. If `pshared` is nonzero,
384+
/// then the semaphore is shared between processes (currently
385+
/// not supported).
371386
#[link_name = "sys_sem_init"]
372-
pub fn sem_init(sem: *mut *const c_void, value: u32) -> i32;
387+
pub fn sem_init(sem: *mut sem_t, pshared: i32, value: u32) -> i32;
373388

374389
/// sem_destroy() frees the unnamed semaphore at the address
375390
/// pointed to by `sem`.
376391
#[link_name = "sys_sem_destroy"]
377-
pub fn sem_destroy(sem: *const c_void) -> i32;
392+
pub fn sem_destroy(sem: *mut sem_t) -> i32;
378393

379394
/// sem_post() increments the semaphore pointed to by `sem`.
380395
/// If the semaphore's value consequently becomes greater
381396
/// than zero, then another thread blocked in a sem_wait call
382397
/// will be woken up and proceed to lock the semaphore.
383398
#[link_name = "sys_sem_post"]
384-
pub fn sem_post(sem: *const c_void) -> i32;
399+
pub fn sem_post(sem: *mut sem_t) -> i32;
385400

386401
/// try to decrement a semaphore
387402
///
388403
/// sem_trywait() is the same as sem_timedwait(), except that
389404
/// if the decrement cannot be immediately performed, then call
390405
/// returns a negative value instead of blocking.
391406
#[link_name = "sys_sem_trywait"]
392-
pub fn sem_trywait(sem: *const c_void) -> i32;
407+
pub fn sem_trywait(sem: *mut sem_t) -> i32;
393408

394409
/// decrement a semaphore
395410
///
@@ -400,17 +415,17 @@ extern "C" {
400415
/// it becomes possible to perform the decrement of the time limit
401416
/// to wait for the semaphore is expired. A time limit `ms` of
402417
/// means infinity waiting time.
403-
#[link_name = "sys_timedwait"]
404-
pub fn sem_timedwait(sem: *const c_void, ms: u32) -> i32;
418+
#[link_name = "sys_sem_timedwait"]
419+
pub fn sem_timedwait(sem: *mut sem_t, abs_timeout: *const timespec) -> i32;
405420

406421
/// Determines the id of the current thread
407422
#[link_name = "sys_getpid"]
408-
pub fn getpid() -> u32;
423+
pub fn getpid() -> pid_t;
409424

410-
/// cause normal termination and return `arg`
425+
/// cause normal termination and return `status`
411426
/// to the host system
412427
#[link_name = "sys_exit"]
413-
pub fn exit(arg: i32) -> !;
428+
pub fn exit(status: i32) -> !;
414429

415430
/// cause abnormal termination
416431
#[link_name = "sys_abort"]
@@ -423,6 +438,10 @@ extern "C" {
423438
#[link_name = "sys_usleep"]
424439
pub fn usleep(usecs: u64);
425440

441+
/// suspend thread execution for an interval measured in nanoseconds
442+
#[link_name = "sys_nanosleep"]
443+
pub fn nanosleep(tp: *const timespec) -> i32;
444+
426445
/// spawn a new thread
427446
///
428447
/// spawn() starts a new thread. The new thread starts execution
@@ -487,37 +506,37 @@ extern "C" {
487506
/// `CLOCK_MONOTONIC`: clock that increments monotonically,
488507
/// tracking the time since an arbitrary point
489508
#[link_name = "sys_clock_gettime"]
490-
pub fn clock_gettime(clock_id: u64, tp: *mut timespec) -> i32;
509+
pub fn clock_gettime(clock_id: clockid_t, tp: *mut timespec) -> i32;
491510

492511
/// open and possibly create a file
493512
///
494513
/// The open() system call opens the file specified by `name`.
495514
/// If the specified file does not exist, it may optionally
496515
/// be created by open().
497516
#[link_name = "sys_open"]
498-
pub fn open(name: *const i8, flags: i32, mode: i32) -> i32;
517+
pub fn open(name: *const c_char, flags: i32, mode: i32) -> i32;
499518

500519
/// open a directory
501520
///
502521
/// The opendir() system call opens the directory specified by `name`.
503522
#[link_name = "sys_opendir"]
504-
pub fn opendir(name: *const i8) -> i32;
523+
pub fn opendir(name: *const c_char) -> i32;
505524

506525
/// delete the file it refers to `name`
507526
#[link_name = "sys_unlink"]
508-
pub fn unlink(name: *const i8) -> i32;
527+
pub fn unlink(name: *const c_char) -> i32;
509528

510529
/// remove directory it refers to `name`
511530
#[link_name = "sys_rmdir"]
512-
pub fn rmdir(name: *const i8) -> i32;
531+
pub fn rmdir(name: *const c_char) -> i32;
513532

514533
/// stat
515534
#[link_name = "sys_stat"]
516-
pub fn stat(name: *const i8, stat: *mut stat) -> i32;
535+
pub fn stat(name: *const c_char, stat: *mut stat) -> i32;
517536

518537
/// lstat
519538
#[link_name = "sys_lstat"]
520-
pub fn lstat(name: *const i8, stat: *mut stat) -> i32;
539+
pub fn lstat(name: *const c_char, stat: *mut stat) -> i32;
521540

522541
/// fstat
523542
#[link_name = "sys_fstat"]
@@ -530,14 +549,21 @@ extern "C" {
530549
#[link_name = "sys_malloc"]
531550
pub fn malloc(size: usize, align: usize) -> *mut u8;
532551

533-
#[doc(hidden)]
552+
#[link_name = "sys_alloc"]
553+
pub fn alloc(size: usize, align: usize) -> *mut u8;
554+
555+
#[link_name = "sys_alloc_zeroed"]
556+
pub fn alloc_zeroed(size: usize, align: usize) -> *mut u8;
557+
534558
#[link_name = "sys_realloc"]
535559
pub fn realloc(ptr: *mut u8, size: usize, align: usize, new_size: usize) -> *mut u8;
536560

537-
#[doc(hidden)]
538561
#[link_name = "sys_free"]
539562
pub fn free(ptr: *mut u8, size: usize, align: usize);
540563

564+
#[link_name = "sys_dealloc"]
565+
pub fn dealloc(ptr: *mut u8, size: usize, align: usize);
566+
541567
#[link_name = "sys_notify"]
542568
pub fn notify(id: usize, count: i32) -> i32;
543569

0 commit comments

Comments
 (0)