Skip to content

Commit 68b54c1

Browse files
袁浩Sword-Destiny
袁浩
authored andcommitted
add teeos std impl
Signed-off-by: 袁浩 <[email protected]>
1 parent 0a1e559 commit 68b54c1

File tree

20 files changed

+1615
-4
lines changed

20 files changed

+1615
-4
lines changed

compiler/rustc_target/src/spec/targets/aarch64_unknown_teeos.rs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ pub fn target() -> Target {
44
let mut base = base::teeos::opts();
55
base.features = "+strict-align,+neon,+fp-armv8".into();
66
base.max_atomic_width = Some(128);
7-
base.linker = Some("aarch64-linux-gnu-ld".into());
87

98
Target {
109
llvm_target: "aarch64-unknown-none".into(),

library/panic_abort/src/lib.rs

+10
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ pub unsafe fn __rust_start_panic(_payload: &mut dyn PanicPayload) -> u32 {
8181
}
8282
core::intrinsics::unreachable();
8383
}
84+
} else if #[cfg(target_os = "teeos")] {
85+
mod teeos {
86+
extern "C" {
87+
pub fn TEE_Panic(code: u32) -> !;
88+
}
89+
}
90+
91+
unsafe fn abort() -> ! {
92+
teeos::TEE_Panic(1);
93+
}
8494
} else {
8595
unsafe fn abort() -> ! {
8696
core::intrinsics::abort();

library/std/build.rs

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ fn main() {
3535
|| target.contains("xous")
3636
|| target.contains("hurd")
3737
|| target.contains("uefi")
38+
|| target.contains("teeos")
3839
// See src/bootstrap/synthetic_targets.rs
3940
|| env::var("RUSTC_BOOTSTRAP_SYNTHETIC_TARGET").is_ok()
4041
{

library/std/src/sys/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ cfg_if::cfg_if! {
5353
} else if #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] {
5454
mod sgx;
5555
pub use self::sgx::*;
56+
} else if #[cfg(target_os = "teeos")] {
57+
mod teeos;
58+
pub use self::teeos::*;
5659
} else {
5760
mod unsupported;
5861
pub use self::unsupported::*;

library/std/src/sys/teeos/alloc.rs

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use crate::alloc::{GlobalAlloc, Layout, System};
2+
use crate::ptr;
3+
use crate::sys::common::alloc::{realloc_fallback, MIN_ALIGN};
4+
5+
#[stable(feature = "alloc_system_type", since = "1.28.0")]
6+
unsafe impl GlobalAlloc for System {
7+
#[inline]
8+
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
9+
// jemalloc provides alignment less than MIN_ALIGN for small allocations.
10+
// So only rely on MIN_ALIGN if size >= align.
11+
// Also see <https://github.com/rust-lang/rust/issues/45955> and
12+
// <https://github.com/rust-lang/rust/issues/62251#issuecomment-507580914>.
13+
if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
14+
libc::malloc(layout.size()) as *mut u8
15+
} else {
16+
aligned_malloc(&layout)
17+
}
18+
}
19+
20+
#[inline]
21+
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
22+
// See the comment above in `alloc` for why this check looks the way it does.
23+
if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
24+
libc::calloc(layout.size(), 1) as *mut u8
25+
} else {
26+
let ptr = self.alloc(layout);
27+
if !ptr.is_null() {
28+
ptr::write_bytes(ptr, 0, layout.size());
29+
}
30+
ptr
31+
}
32+
}
33+
34+
#[inline]
35+
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
36+
libc::free(ptr as *mut libc::c_void)
37+
}
38+
39+
#[inline]
40+
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
41+
if layout.align() <= MIN_ALIGN && layout.align() <= new_size {
42+
libc::realloc(ptr as *mut libc::c_void, new_size) as *mut u8
43+
} else {
44+
realloc_fallback(self, ptr, layout, new_size)
45+
}
46+
}
47+
}
48+
49+
#[inline]
50+
unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 {
51+
let mut out = ptr::null_mut();
52+
// posix_memalign requires that the alignment be a multiple of `sizeof(void*)`.
53+
// Since these are all powers of 2, we can just use max.
54+
let align = layout.align().max(crate::mem::size_of::<usize>());
55+
let ret = libc::posix_memalign(&mut out, align, layout.size());
56+
if ret != 0 { ptr::null_mut() } else { out as *mut u8 }
57+
}

library/std/src/sys/teeos/condvar.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use crate::sys::mutex::Mutex;
2+
use crate::time::Duration;
3+
4+
pub struct Condvar {}
5+
6+
pub type MovableCondvar = Condvar;
7+
8+
impl Condvar {
9+
#[inline]
10+
pub const fn new() -> Condvar {
11+
Condvar {}
12+
}
13+
14+
#[inline]
15+
pub unsafe fn notify_one(&self) {}
16+
17+
#[inline]
18+
pub unsafe fn notify_all(&self) {}
19+
20+
pub unsafe fn wait(&self, _mutex: &Mutex) {
21+
panic!("condvar wait not supported")
22+
}
23+
24+
pub unsafe fn wait_timeout(&self, _mutex: &Mutex, _dur: Duration) -> bool {
25+
panic!("condvar wait not supported");
26+
}
27+
}

library/std/src/sys/teeos/memchr.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pub fn memchr(needle: u8, haystack: &[u8]) -> Option<usize> {
2+
let p = unsafe {
3+
libc::memchr(
4+
haystack.as_ptr() as *const libc::c_void,
5+
needle as libc::c_int,
6+
haystack.len(),
7+
)
8+
};
9+
if p.is_null() { None } else { Some(p.addr() - haystack.as_ptr().addr()) }
10+
}
11+
12+
pub fn memrchr(needle: u8, haystack: &[u8]) -> Option<usize> {
13+
fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option<usize> {
14+
core::slice::memchr::memrchr(needle, haystack)
15+
}
16+
17+
memrchr_specific(needle, haystack)
18+
}

library/std/src/sys/teeos/mod.rs

+170
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
//! System bindings for the Teeos platform
2+
//!
3+
//! This module contains the facade (aka platform-specific) implementations of
4+
//! OS level functionality for Teeos.
5+
#![allow(unsafe_op_in_unsafe_fn)]
6+
#![allow(unused_variables)]
7+
#![allow(dead_code)]
8+
9+
pub use self::rand::hashmap_random_keys;
10+
11+
pub mod alloc;
12+
#[path = "../unsupported/args.rs"]
13+
pub mod args;
14+
#[path = "../unix/cmath.rs"]
15+
pub mod cmath;
16+
pub mod condvar;
17+
#[path = "../unsupported/env.rs"]
18+
pub mod env;
19+
#[path = "../unsupported/locks/mod.rs"]
20+
pub mod locks;
21+
//pub mod fd;
22+
#[path = "../unsupported/fs.rs"]
23+
pub mod fs;
24+
#[path = "../unsupported/io.rs"]
25+
pub mod io;
26+
pub mod memchr;
27+
pub mod mutex;
28+
pub mod net;
29+
#[path = "../unsupported/once.rs"]
30+
pub mod once;
31+
pub mod os;
32+
#[path = "../unix/os_str.rs"]
33+
pub mod os_str;
34+
#[path = "../unix/path.rs"]
35+
pub mod path;
36+
#[path = "../unsupported/pipe.rs"]
37+
pub mod pipe;
38+
#[path = "../unsupported/process.rs"]
39+
pub mod process;
40+
mod rand;
41+
pub mod rwlock;
42+
pub mod stdio;
43+
pub mod thread;
44+
pub mod thread_local_dtor;
45+
#[path = "../unix/thread_local_key.rs"]
46+
pub mod thread_local_key;
47+
#[path = "../unsupported/thread_parking.rs"]
48+
pub mod thread_parking;
49+
#[allow(non_camel_case_types)]
50+
#[path = "time.rs"]
51+
pub mod time;
52+
53+
use crate::io::ErrorKind;
54+
55+
pub fn abort_internal() -> ! {
56+
unsafe { libc::abort() }
57+
}
58+
59+
// Trusted Applications are loaded as dynamic libraries on Teeos,
60+
// so this should never be called.
61+
pub fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {}
62+
63+
// SAFETY: must be called only once during runtime cleanup.
64+
// this is not guaranteed to run, for example when the program aborts.
65+
pub unsafe fn cleanup() {
66+
unimplemented!()
67+
// We do NOT have stack overflow handler, because TEE OS will kill TA when it happens.
68+
// So cleanup is commented
69+
// stack_overflow::cleanup();
70+
}
71+
72+
#[inline]
73+
pub(crate) fn is_interrupted(errno: i32) -> bool {
74+
errno == libc::EINTR
75+
}
76+
77+
// Note: code below is 1:1 copied from unix/mod.rs
78+
pub fn decode_error_kind(errno: i32) -> ErrorKind {
79+
use ErrorKind::*;
80+
match errno as libc::c_int {
81+
libc::E2BIG => ArgumentListTooLong,
82+
libc::EADDRINUSE => AddrInUse,
83+
libc::EADDRNOTAVAIL => AddrNotAvailable,
84+
libc::EBUSY => ResourceBusy,
85+
libc::ECONNABORTED => ConnectionAborted,
86+
libc::ECONNREFUSED => ConnectionRefused,
87+
libc::ECONNRESET => ConnectionReset,
88+
libc::EDEADLK => Deadlock,
89+
libc::EDQUOT => FilesystemQuotaExceeded,
90+
libc::EEXIST => AlreadyExists,
91+
libc::EFBIG => FileTooLarge,
92+
libc::EHOSTUNREACH => HostUnreachable,
93+
libc::EINTR => Interrupted,
94+
libc::EINVAL => InvalidInput,
95+
libc::EISDIR => IsADirectory,
96+
libc::ELOOP => FilesystemLoop,
97+
libc::ENOENT => NotFound,
98+
libc::ENOMEM => OutOfMemory,
99+
libc::ENOSPC => StorageFull,
100+
libc::ENOSYS => Unsupported,
101+
libc::EMLINK => TooManyLinks,
102+
libc::ENAMETOOLONG => InvalidFilename,
103+
libc::ENETDOWN => NetworkDown,
104+
libc::ENETUNREACH => NetworkUnreachable,
105+
libc::ENOTCONN => NotConnected,
106+
libc::ENOTDIR => NotADirectory,
107+
libc::ENOTEMPTY => DirectoryNotEmpty,
108+
libc::EPIPE => BrokenPipe,
109+
libc::EROFS => ReadOnlyFilesystem,
110+
libc::ESPIPE => NotSeekable,
111+
libc::ESTALE => StaleNetworkFileHandle,
112+
libc::ETIMEDOUT => TimedOut,
113+
libc::ETXTBSY => ExecutableFileBusy,
114+
libc::EXDEV => CrossesDevices,
115+
116+
libc::EACCES | libc::EPERM => PermissionDenied,
117+
118+
// These two constants can have the same value on some systems,
119+
// but different values on others, so we can't use a match
120+
// clause
121+
x if x == libc::EAGAIN || x == libc::EWOULDBLOCK => WouldBlock,
122+
123+
_ => Uncategorized,
124+
}
125+
}
126+
127+
#[doc(hidden)]
128+
pub trait IsMinusOne {
129+
fn is_minus_one(&self) -> bool;
130+
}
131+
132+
macro_rules! impl_is_minus_one {
133+
($($t:ident)*) => ($(impl IsMinusOne for $t {
134+
fn is_minus_one(&self) -> bool {
135+
*self == -1
136+
}
137+
})*)
138+
}
139+
140+
impl_is_minus_one! { i8 i16 i32 i64 isize }
141+
142+
pub fn cvt<T: IsMinusOne>(t: T) -> crate::io::Result<T> {
143+
if t.is_minus_one() { Err(crate::io::Error::last_os_error()) } else { Ok(t) }
144+
}
145+
146+
pub fn cvt_r<T, F>(mut f: F) -> crate::io::Result<T>
147+
where
148+
T: IsMinusOne,
149+
F: FnMut() -> T,
150+
{
151+
loop {
152+
match cvt(f()) {
153+
Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
154+
other => return other,
155+
}
156+
}
157+
}
158+
159+
pub fn cvt_nz(error: libc::c_int) -> crate::io::Result<()> {
160+
if error == 0 { Ok(()) } else { Err(crate::io::Error::from_raw_os_error(error)) }
161+
}
162+
163+
use crate::io as std_io;
164+
pub fn unsupported<T>() -> std_io::Result<T> {
165+
Err(unsupported_err())
166+
}
167+
168+
pub fn unsupported_err() -> std_io::Error {
169+
std_io::Error::new(std_io::ErrorKind::Unsupported, "operation not supported on this platform")
170+
}

0 commit comments

Comments
 (0)