-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: 袁浩 <yuanhao34@huawei.com>
- Loading branch information
袁浩
committed
Oct 9, 2023
1 parent
37fda98
commit 6a7448c
Showing
19 changed files
with
1,265 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use crate::alloc::{GlobalAlloc, Layout, System}; | ||
use crate::ptr; | ||
use crate::sys::common::alloc::{realloc_fallback, MIN_ALIGN}; | ||
|
||
#[stable(feature = "alloc_system_type", since = "1.28.0")] | ||
unsafe impl GlobalAlloc for System { | ||
#[inline] | ||
unsafe fn alloc(&self, layout: Layout) -> *mut u8 { | ||
// jemalloc provides alignment less than MIN_ALIGN for small allocations. | ||
// So only rely on MIN_ALIGN if size >= align. | ||
// Also see <https://github.com/rust-lang/rust/issues/45955> and | ||
// <https://github.com/rust-lang/rust/issues/62251#issuecomment-507580914>. | ||
if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() { | ||
libc::malloc(layout.size()) as *mut u8 | ||
} else { | ||
aligned_malloc(&layout) | ||
} | ||
} | ||
|
||
#[inline] | ||
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { | ||
// See the comment above in `alloc` for why this check looks the way it does. | ||
if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() { | ||
libc::calloc(layout.size(), 1) as *mut u8 | ||
} else { | ||
let ptr = self.alloc(layout); | ||
if !ptr.is_null() { | ||
ptr::write_bytes(ptr, 0, layout.size()); | ||
} | ||
ptr | ||
} | ||
} | ||
|
||
#[inline] | ||
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) { | ||
libc::free(ptr as *mut libc::c_void) | ||
} | ||
|
||
#[inline] | ||
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 { | ||
if layout.align() <= MIN_ALIGN && layout.align() <= new_size { | ||
libc::realloc(ptr as *mut libc::c_void, new_size) as *mut u8 | ||
} else { | ||
realloc_fallback(self, ptr, layout, new_size) | ||
} | ||
} | ||
} | ||
|
||
#[inline] | ||
unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 { | ||
let mut out = ptr::null_mut(); | ||
// posix_memalign requires that the alignment be a multiple of `sizeof(void*)`. | ||
// Since these are all powers of 2, we can just use max. | ||
let align = layout.align().max(crate::mem::size_of::<usize>()); | ||
let ret = libc::posix_memalign(&mut out, align, layout.size()); | ||
if ret != 0 { ptr::null_mut() } else { out as *mut u8 } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use crate::sys::mutex::Mutex; | ||
use crate::time::Duration; | ||
|
||
pub struct Condvar {} | ||
|
||
pub type MovableCondvar = Condvar; | ||
|
||
impl Condvar { | ||
#[inline] | ||
pub const fn new() -> Condvar { | ||
Condvar {} | ||
} | ||
|
||
#[inline] | ||
pub unsafe fn notify_one(&self) {} | ||
|
||
#[inline] | ||
pub unsafe fn notify_all(&self) {} | ||
|
||
pub unsafe fn wait(&self, _mutex: &Mutex) { | ||
panic!("condvar wait not supported") | ||
} | ||
|
||
pub unsafe fn wait_timeout(&self, _mutex: &Mutex, _dur: Duration) -> bool { | ||
panic!("condvar wait not supported"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
pub fn memchr(needle: u8, haystack: &[u8]) -> Option<usize> { | ||
let p = unsafe { | ||
libc::memchr( | ||
haystack.as_ptr() as *const libc::c_void, | ||
needle as libc::c_int, | ||
haystack.len(), | ||
) | ||
}; | ||
if p.is_null() { None } else { Some(p.addr() - haystack.as_ptr().addr()) } | ||
} | ||
|
||
pub fn memrchr(needle: u8, haystack: &[u8]) -> Option<usize> { | ||
fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option<usize> { | ||
core::slice::memchr::memrchr(needle, haystack) | ||
} | ||
|
||
memrchr_specific(needle, haystack) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
//! System bindings for the Teeos platform | ||
//! | ||
//! This module contains the facade (aka platform-specific) implementations of | ||
//! OS level functionality for Teeos. | ||
#![allow(unsafe_op_in_unsafe_fn)] | ||
#![allow(unused_variables)] | ||
#![allow(dead_code)] | ||
|
||
pub use self::rand::hashmap_random_keys; | ||
|
||
pub mod alloc; | ||
#[path = "../unsupported/args.rs"] | ||
pub mod args; | ||
#[path = "../unix/cmath.rs"] | ||
pub mod cmath; | ||
pub mod condvar; | ||
#[path = "../unsupported/env.rs"] | ||
pub mod env; | ||
#[path = "../unsupported/locks/mod.rs"] | ||
pub mod locks; | ||
//pub mod fd; | ||
#[path = "../unsupported/fs.rs"] | ||
pub mod fs; | ||
#[path = "../unsupported/io.rs"] | ||
pub mod io; | ||
pub mod memchr; | ||
pub mod mutex; | ||
#[path = "../unsupported/net.rs"] | ||
pub mod net; | ||
#[path = "../unsupported/once.rs"] | ||
pub mod once; | ||
pub mod os; | ||
#[path = "../unix/os_str.rs"] | ||
pub mod os_str; | ||
#[path = "../unix/path.rs"] | ||
pub mod path; | ||
#[path = "../unsupported/pipe.rs"] | ||
pub mod pipe; | ||
#[path = "../unsupported/process.rs"] | ||
pub mod process; | ||
mod rand; | ||
pub mod rwlock; | ||
pub mod stdio; | ||
pub mod thread; | ||
pub mod thread_local_dtor; | ||
pub mod thread_local_key; | ||
#[path = "../unsupported/thread_parking.rs"] | ||
pub mod thread_parking; | ||
#[allow(non_camel_case_types)] | ||
#[path = "time.rs"] | ||
pub mod time; | ||
|
||
use crate::io::ErrorKind; | ||
|
||
pub fn abort_internal() -> ! { | ||
unsafe { libc::abort() } | ||
} | ||
|
||
// Trusted Applications are loaded as dynamic libraries on Teeos, | ||
// so this should never be called. | ||
pub fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {} | ||
|
||
// SAFETY: must be called only once during runtime cleanup. | ||
// this is not guaranteed to run, for example when the program aborts. | ||
pub unsafe fn cleanup() { | ||
unimplemented!() | ||
// We do NOT have stack overflow handler, because TEE OS will kill TA when it happens. | ||
// So cleanup is commented | ||
// stack_overflow::cleanup(); | ||
} | ||
|
||
#[inline] | ||
pub(crate) fn is_interrupted(errno: i32) -> bool { | ||
errno == libc::EINTR | ||
} | ||
|
||
// Note: code below is 1:1 copied from unix/mod.rs | ||
pub fn decode_error_kind(errno: i32) -> ErrorKind { | ||
use ErrorKind::*; | ||
match errno as libc::c_int { | ||
libc::E2BIG => ArgumentListTooLong, | ||
libc::EADDRINUSE => AddrInUse, | ||
libc::EADDRNOTAVAIL => AddrNotAvailable, | ||
libc::EBUSY => ResourceBusy, | ||
libc::ECONNABORTED => ConnectionAborted, | ||
libc::ECONNREFUSED => ConnectionRefused, | ||
libc::ECONNRESET => ConnectionReset, | ||
libc::EDEADLK => Deadlock, | ||
libc::EDQUOT => FilesystemQuotaExceeded, | ||
libc::EEXIST => AlreadyExists, | ||
libc::EFBIG => FileTooLarge, | ||
libc::EHOSTUNREACH => HostUnreachable, | ||
libc::EINTR => Interrupted, | ||
libc::EINVAL => InvalidInput, | ||
libc::EISDIR => IsADirectory, | ||
libc::ELOOP => FilesystemLoop, | ||
libc::ENOENT => NotFound, | ||
libc::ENOMEM => OutOfMemory, | ||
libc::ENOSPC => StorageFull, | ||
libc::ENOSYS => Unsupported, | ||
libc::EMLINK => TooManyLinks, | ||
libc::ENAMETOOLONG => InvalidFilename, | ||
libc::ENETDOWN => NetworkDown, | ||
libc::ENETUNREACH => NetworkUnreachable, | ||
libc::ENOTCONN => NotConnected, | ||
libc::ENOTDIR => NotADirectory, | ||
libc::ENOTEMPTY => DirectoryNotEmpty, | ||
libc::EPIPE => BrokenPipe, | ||
libc::EROFS => ReadOnlyFilesystem, | ||
libc::ESPIPE => NotSeekable, | ||
libc::ESTALE => StaleNetworkFileHandle, | ||
libc::ETIMEDOUT => TimedOut, | ||
libc::ETXTBSY => ExecutableFileBusy, | ||
libc::EXDEV => CrossesDevices, | ||
|
||
libc::EACCES | libc::EPERM => PermissionDenied, | ||
|
||
// These two constants can have the same value on some systems, | ||
// but different values on others, so we can't use a match | ||
// clause | ||
x if x == libc::EAGAIN || x == libc::EWOULDBLOCK => WouldBlock, | ||
|
||
_ => Uncategorized, | ||
} | ||
} | ||
|
||
#[doc(hidden)] | ||
pub trait IsMinusOne { | ||
fn is_minus_one(&self) -> bool; | ||
} | ||
|
||
macro_rules! impl_is_minus_one { | ||
($($t:ident)*) => ($(impl IsMinusOne for $t { | ||
fn is_minus_one(&self) -> bool { | ||
*self == -1 | ||
} | ||
})*) | ||
} | ||
|
||
impl_is_minus_one! { i8 i16 i32 i64 isize } | ||
|
||
pub fn cvt<T: IsMinusOne>(t: T) -> crate::io::Result<T> { | ||
if t.is_minus_one() { Err(crate::io::Error::last_os_error()) } else { Ok(t) } | ||
} | ||
|
||
pub fn cvt_r<T, F>(mut f: F) -> crate::io::Result<T> | ||
where | ||
T: IsMinusOne, | ||
F: FnMut() -> T, | ||
{ | ||
loop { | ||
match cvt(f()) { | ||
Err(ref e) if e.kind() == ErrorKind::Interrupted => {} | ||
other => return other, | ||
} | ||
} | ||
} | ||
|
||
pub fn cvt_nz(error: libc::c_int) -> crate::io::Result<()> { | ||
if error == 0 { Ok(()) } else { Err(crate::io::Error::from_raw_os_error(error)) } | ||
} | ||
|
||
use crate::io as std_io; | ||
pub fn unsupported<T>() -> std_io::Result<T> { | ||
Err(unsupported_err()) | ||
} | ||
|
||
pub fn unsupported_err() -> std_io::Error { | ||
std_io::Error::new(std_io::ErrorKind::Unsupported, "operation not supported on this platform") | ||
} |
Oops, something went wrong.