move interface to the unikernel in the crate hermit-abi

=> simplifies the maintenance of the interface
This commit is contained in:
Stefan Lankes 2019-10-20 07:55:10 +00:00
parent dc094bf184
commit 5ebd4d9c27
12 changed files with 70 additions and 114 deletions

View File

@ -1291,6 +1291,17 @@ dependencies = [
"unicode-segmentation",
]
[[package]]
name = "hermit-abi"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f22b8f315b98f415780ddbe9163c7dbbc5a07225b6d102ace1d8aeef85775140"
dependencies = [
"compiler_builtins",
"libc",
"rustc-std-workspace-core",
]
[[package]]
name = "hex"
version = "0.3.2"
@ -4073,7 +4084,8 @@ dependencies = [
"core",
"dlmalloc",
"fortanix-sgx-abi",
"hashbrown",
"hashbrown 0.6.1",
"hermit-abi",
"libc",
"panic_abort",
"panic_unwind",

View File

@ -50,6 +50,9 @@ dlmalloc = { version = "0.1", features = ['rustc-dep-of-std'] }
[target.x86_64-fortanix-unknown-sgx.dependencies]
fortanix-sgx-abi = { version = "0.3.2", features = ['rustc-dep-of-std'] }
[target.x86_64-unknown-hermit.dependencies]
hermit-abi = { version = "0.1", features = ['rustc-dep-of-std'] }
[target.wasm32-wasi.dependencies]
wasi = { version = "0.7.0", features = ['rustc-dep-of-std', 'alloc'] }

View File

@ -1,21 +1,16 @@
use crate::alloc::{GlobalAlloc, Layout, System};
use crate::ptr;
extern "C" {
fn sys_malloc(size: usize, align: usize) -> *mut u8;
fn sys_realloc(ptr: *mut u8, size: usize, align: usize, new_size: usize) -> *mut u8;
fn sys_free(ptr: *mut u8, size: usize, align: usize);
}
use crate::sys::hermit::abi;
#[stable(feature = "alloc_system_type", since = "1.28.0")]
unsafe impl GlobalAlloc for System {
#[inline]
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
sys_malloc(layout.size(), layout.align())
abi::malloc(layout.size(), layout.align())
}
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
let addr = sys_malloc(layout.size(), layout.align());
let addr = abi::malloc(layout.size(), layout.align());
if !addr.is_null() {
ptr::write_bytes(
@ -30,11 +25,11 @@ unsafe impl GlobalAlloc for System {
#[inline]
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
sys_free(ptr, layout.size(), layout.align())
abi::free(ptr, layout.size(), layout.align())
}
#[inline]
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
sys_realloc(ptr, layout.size(), layout.align(), new_size)
abi::realloc(ptr, layout.size(), layout.align(), new_size)
}
}

View File

@ -1,4 +1,5 @@
use crate::cmp;
use crate::sys::hermit::abi;
use crate::sys::mutex::Mutex;
use crate::time::Duration;
@ -6,13 +7,6 @@ pub struct Condvar {
identifier: usize,
}
extern "C" {
fn sys_notify(id: usize, count: i32) -> i32;
fn sys_add_queue(id: usize, timeout_ns: i64) -> i32;
fn sys_wait(id: usize) -> i32;
fn sys_destroy_queue(id: usize) -> i32;
}
impl Condvar {
pub const fn new() -> Condvar {
Condvar { identifier: 0 }
@ -24,19 +18,19 @@ impl Condvar {
}
pub unsafe fn notify_one(&self) {
let _ = sys_notify(self.id(), 1);
let _ = abi::notify(self.id(), 1);
}
#[inline]
pub unsafe fn notify_all(&self) {
let _ = sys_notify(self.id(), -1 /* =all */);
let _ = abi::notify(self.id(), -1 /* =all */);
}
pub unsafe fn wait(&self, mutex: &Mutex) {
// add current task to the wait queue
let _ = sys_add_queue(self.id(), -1 /* no timeout */);
let _ = abi::add_queue(self.id(), -1 /* no timeout */);
mutex.unlock();
let _ = sys_wait(self.id());
let _ = abi::wait(self.id());
mutex.lock();
}
@ -45,12 +39,12 @@ impl Condvar {
let nanos = cmp::min(i64::max_value() as u128, nanos);
// add current task to the wait queue
let _ = sys_add_queue(self.id(), nanos as i64);
let _ = abi::add_queue(self.id(), nanos as i64);
mutex.unlock();
// If the return value is !0 then a timeout happened, so we return
// `false` as we weren't actually notified.
let ret = sys_wait(self.id()) == 0;
let ret = abi::wait(self.id()) == 0;
mutex.lock();
ret
@ -58,7 +52,7 @@ impl Condvar {
#[inline]
pub unsafe fn destroy(&self) {
let _ = sys_destroy_queue(self.id());
let _ = abi::destroy_queue(self.id());
}
#[inline]

View File

@ -3,14 +3,9 @@
use crate::io::{self, Read, ErrorKind};
use crate::mem;
use crate::sys::cvt;
use crate::sys::hermit::abi;
use crate::sys_common::AsInner;
extern {
fn sys_read(fd: i32, buf: *mut u8, len: usize) -> isize;
fn sys_write(fd: i32, buf: *const u8, len: usize) -> isize;
fn sys_close(fd: i32) -> i32;
}
#[derive(Debug)]
pub struct FileDesc {
fd: i32,
@ -31,7 +26,7 @@ impl FileDesc {
}
pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
let result = unsafe { sys_read(self.fd, buf.as_mut_ptr(), buf.len()) };
let result = unsafe { abi::read(self.fd, buf.as_mut_ptr(), buf.len()) };
cvt(result as i32)
}
@ -41,7 +36,7 @@ impl FileDesc {
}
pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
let result = unsafe { sys_write(self.fd, buf.as_ptr(), buf.len()) };
let result = unsafe { abi::write(self.fd, buf.as_ptr(), buf.len()) };
cvt(result as i32)
}
@ -82,6 +77,6 @@ impl Drop for FileDesc {
// the file descriptor was closed or not, and if we retried (for
// something like EINTR), we might close another valid file descriptor
// (opened after we closed ours.
let _ = unsafe { sys_close(self.fd) };
let _ = unsafe { abi::close(self.fd) };
}
}

View File

@ -6,6 +6,7 @@ use crate::io::{SeekFrom, IoSlice, IoSliceMut};
use crate::path::{Path, PathBuf};
use crate::sys::time::SystemTime;
use crate::sys::{unsupported, Void};
use crate::sys::hermit::abi;
use crate::sys::hermit::fd::FileDesc;
use crate::sys::cvt;
use crate::sys_common::os_str_bytes::OsStrExt;
@ -13,11 +14,6 @@ use crate::sys_common::os_str_bytes::OsStrExt;
pub use crate::sys_common::fs::copy;
//pub use crate::sys_common::fs::remove_dir_all;
extern {
fn sys_open(name: *const i8, flags: i32, mode: i32) -> i32;
fn sys_unlink(name: *const i8) -> i32;
}
fn cstr(path: &Path) -> io::Result<CString> {
Ok(CString::new(path.as_os_str().as_bytes())?)
}
@ -272,7 +268,7 @@ impl File {
mode = 0;
}
let fd = unsafe { cvt(sys_open(path.as_ptr(), flags, mode))? };
let fd = unsafe { cvt(abi::open(path.as_ptr(), flags, mode))? };
Ok(File(FileDesc::new(fd as i32)))
}
@ -345,7 +341,7 @@ pub fn readdir(_p: &Path) -> io::Result<ReadDir> {
pub fn unlink(path: &Path) -> io::Result<()> {
let name = cstr(path)?;
let _ = unsafe { cvt(sys_unlink(name.as_ptr()))? };
let _ = unsafe { cvt(abi::unlink(name.as_ptr()))? };
Ok(())
}

View File

@ -42,6 +42,9 @@ pub mod fast_thread_local;
pub use crate::sys_common::os_str_bytes as os_str;
use crate::io::ErrorKind;
#[allow(unused_extern_crates)]
pub extern crate hermit_abi as abi;
pub fn unsupported<T>() -> crate::io::Result<T> {
Err(unsupported_err())
}
@ -74,11 +77,7 @@ pub extern "C" fn floor(x: f64) -> f64 {
}
pub unsafe fn abort_internal() -> ! {
extern "C" {
fn sys_abort() ->!;
}
sys_abort();
abi::abort();
}
// FIXME: just a workaround to test the system
@ -108,7 +107,6 @@ pub unsafe extern "C" fn runtime_entry(argc: i32, argv: *const *const c_char,
env: *const *const c_char) -> ! {
extern "C" {
fn main(argc: isize, argv: *const *const c_char) -> i32;
fn sys_exit(arg: i32) ->!;
}
// initialize environment
@ -116,7 +114,7 @@ pub unsafe extern "C" fn runtime_entry(argc: i32, argv: *const *const c_char,
let result = main(argc as isize, argv);
sys_exit(result);
abi::exit(result);
}
pub fn decode_error_kind(errno: i32) -> ErrorKind {

View File

@ -1,17 +1,6 @@
use crate::ptr;
use crate::ffi::c_void;
extern "C" {
fn sys_sem_init(sem: *mut *const c_void, value: u32) -> i32;
fn sys_sem_destroy(sem: *const c_void) -> i32;
fn sys_sem_post(sem: *const c_void) -> i32;
fn sys_sem_trywait(sem: *const c_void) -> i32;
fn sys_sem_timedwait(sem: *const c_void, ms: u32) -> i32;
fn sys_recmutex_init(recmutex: *mut *const c_void) -> i32;
fn sys_recmutex_destroy(recmutex: *const c_void) -> i32;
fn sys_recmutex_lock(recmutex: *const c_void) -> i32;
fn sys_recmutex_unlock(recmutex: *const c_void) -> i32;
}
use crate::sys::hermit::abi;
pub struct Mutex {
inner: *const c_void
@ -27,28 +16,28 @@ impl Mutex {
#[inline]
pub unsafe fn init(&mut self) {
let _ = sys_sem_init(&mut self.inner as *mut *const c_void, 1);
let _ = abi::sem_init(&mut self.inner as *mut *const c_void, 1);
}
#[inline]
pub unsafe fn lock(&self) {
let _ = sys_sem_timedwait(self.inner, 0);
let _ = abi::sem_timedwait(self.inner, 0);
}
#[inline]
pub unsafe fn unlock(&self) {
let _ = sys_sem_post(self.inner);
let _ = abi::sem_post(self.inner);
}
#[inline]
pub unsafe fn try_lock(&self) -> bool {
let result = sys_sem_trywait(self.inner);
let result = abi::sem_trywait(self.inner);
result == 0
}
#[inline]
pub unsafe fn destroy(&self) {
let _ = sys_sem_destroy(self.inner);
let _ = abi::sem_destroy(self.inner);
}
}
@ -63,12 +52,12 @@ impl ReentrantMutex {
#[inline]
pub unsafe fn init(&mut self) {
let _ = sys_recmutex_init(&mut self.inner as *mut *const c_void);
let _ = abi::recmutex_init(&mut self.inner as *mut *const c_void);
}
#[inline]
pub unsafe fn lock(&self) {
let _ = sys_recmutex_lock(self.inner);
let _ = abi::recmutex_lock(self.inner);
}
#[inline]
@ -78,11 +67,11 @@ impl ReentrantMutex {
#[inline]
pub unsafe fn unlock(&self) {
let _ = sys_recmutex_unlock(self.inner);
let _ = abi::recmutex_unlock(self.inner);
}
#[inline]
pub unsafe fn destroy(&self) {
let _ = sys_recmutex_destroy(self.inner);
let _ = abi::recmutex_destroy(self.inner);
}
}

View File

@ -12,11 +12,7 @@ use crate::collections::HashMap;
use crate::vec;
use crate::sync::Mutex;
use crate::sys_common::os_str_bytes::*;
extern "C" {
fn sys_getpid() -> u32;
fn sys_exit(arg: i32) ->!;
}
use crate::sys::hermit::abi;
pub fn errno() -> i32 {
0
@ -167,12 +163,12 @@ pub fn home_dir() -> Option<PathBuf> {
pub fn exit(code: i32) -> ! {
unsafe {
sys_exit(code);
abi::exit(code);
}
}
pub fn getpid() -> u32 {
unsafe {
sys_getpid()
abi::getpid()
}
}

View File

@ -1,9 +1,6 @@
use crate::io;
use crate::io::{IoSlice, IoSliceMut};
extern "C" {
fn sys_write(fd: i32, buf: *const u8, len: usize) -> isize;
}
use crate::sys::hermit::abi;
pub struct Stdin;
pub struct Stdout;
@ -35,7 +32,7 @@ impl Stdout {
let len;
unsafe {
len = sys_write(1, data.as_ptr() as *const u8, data.len())
len = abi::write(1, data.as_ptr() as *const u8, data.len())
}
if len < 0 {
@ -49,7 +46,7 @@ impl Stdout {
let len;
unsafe {
len = sys_write(1, data.as_ptr() as *const u8, data.len())
len = abi::write(1, data.as_ptr() as *const u8, data.len())
}
if len < 0 {
@ -73,7 +70,7 @@ impl Stderr {
let len;
unsafe {
len = sys_write(2, data.as_ptr() as *const u8, data.len())
len = abi::write(2, data.as_ptr() as *const u8, data.len())
}
if len < 0 {
@ -87,7 +84,7 @@ impl Stderr {
let len;
unsafe {
len = sys_write(2, data.as_ptr() as *const u8, data.len())
len = abi::write(2, data.as_ptr() as *const u8, data.len())
}
if len < 0 {

View File

@ -2,6 +2,7 @@
use crate::ffi::CStr;
use crate::io;
use crate::sys::hermit::abi;
use crate::time::Duration;
use crate::mem;
use crate::fmt;
@ -9,7 +10,7 @@ use core::u32;
use crate::sys_common::thread::*;
pub type Tid = u32;
pub type Tid = abi::Tid;
/// Priority of a task
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Copy)]
@ -33,14 +34,6 @@ impl fmt::Display for Priority {
pub const NORMAL_PRIO: Priority = Priority::from(2);
extern "C" {
fn sys_usleep(usecs: u64);
fn sys_spawn(id: *mut Tid, func: extern "C" fn(usize),
arg: usize, prio: u8, core_id: isize) -> i32;
fn sys_join(id: Tid) -> i32;
fn sys_yield();
}
pub struct Thread {
tid: Tid
}
@ -56,7 +49,7 @@ impl Thread {
{
let p = box p;
let mut tid: Tid = u32::MAX;
let ret = sys_spawn(&mut tid as *mut Tid, thread_start,
let ret = abi::spawn(&mut tid as *mut Tid, thread_start,
&*p as *const _ as *const u8 as usize,
Priority::into(NORMAL_PRIO), core_id);
@ -83,7 +76,7 @@ impl Thread {
#[inline]
pub fn yield_now() {
unsafe {
sys_yield();
abi::yield_now();
}
}
@ -95,13 +88,13 @@ impl Thread {
#[inline]
pub fn sleep(dur: Duration) {
unsafe {
sys_usleep(dur.as_micros() as u64);
abi::usleep(dur.as_micros() as u64);
}
}
pub fn join(self) {
unsafe {
let _ = sys_join(self.tid);
let _ = abi::join(self.tid);
}
}

View File

@ -4,21 +4,9 @@ use crate::time::Duration;
use crate::cmp::Ordering;
use crate::convert::TryInto;
use core::hash::{Hash, Hasher};
const NSEC_PER_SEC: u64 = 1_000_000_000;
const CLOCK_REALTIME: u64 = 1;
const CLOCK_MONOTONIC: u64 = 4;
extern "C" {
fn sys_clock_gettime(clock_id: u64, tp: *mut timespec) -> i32;
}
#[derive(Copy, Clone, Debug)]
#[repr(C)]
pub struct timespec {
pub tv_sec: i64,
pub tv_nsec: i64,
}
use crate::sys::hermit::abi;
use crate::sys::hermit::abi::{CLOCK_REALTIME, CLOCK_MONOTONIC, NSEC_PER_SEC};
use crate::sys::hermit::abi::timespec;
#[derive(Copy, Clone, Debug)]
struct Timespec {
@ -131,7 +119,7 @@ pub struct Instant {
impl Instant {
pub fn now() -> Instant {
let mut time: Timespec = Timespec::zero();
let _ = unsafe { sys_clock_gettime(CLOCK_MONOTONIC, &mut time.t as *mut timespec) };
let _ = unsafe { abi::clock_gettime(CLOCK_MONOTONIC, &mut time.t as *mut timespec) };
Instant { t: time }
}
@ -169,7 +157,7 @@ pub const UNIX_EPOCH: SystemTime = SystemTime {
impl SystemTime {
pub fn now() -> SystemTime {
let mut time: Timespec = Timespec::zero();
let _ = unsafe { sys_clock_gettime(CLOCK_REALTIME, &mut time.t as *mut timespec) };
let _ = unsafe { abi::clock_gettime(CLOCK_REALTIME, &mut time.t as *mut timespec) };
SystemTime { t: time }
}