]> git.proxmox.com Git - rustc.git/blame - src/libstd/sys/sgx/mod.rs
New upstream version 1.42.0+dfsg1
[rustc.git] / src / libstd / sys / sgx / mod.rs
CommitLineData
0731742a
XL
1//! System bindings for the Fortanix SGX platform
2//!
3//! This module contains the facade (aka platform-specific) implementations of
4//! OS level functionality for Fortanix SGX.
5
532ac7d7
XL
6use crate::io::ErrorKind;
7use crate::os::raw::c_char;
8use crate::sync::atomic::{AtomicBool, Ordering};
0731742a
XL
9
10pub mod abi;
11mod waitqueue;
12
13pub mod alloc;
14pub mod args;
0731742a
XL
15pub mod cmath;
16pub mod condvar;
17pub mod env;
18pub mod ext;
19pub mod fd;
20pub mod fs;
9fa01778 21pub mod io;
0731742a
XL
22pub mod memchr;
23pub mod mutex;
24pub mod net;
25pub mod os;
0731742a
XL
26pub mod path;
27pub mod pipe;
28pub mod process;
29pub mod rwlock;
30pub mod stack_overflow;
dfeec247 31pub mod stdio;
0731742a
XL
32pub mod thread;
33pub mod thread_local;
34pub mod time;
0731742a 35
532ac7d7
XL
36pub use crate::sys_common::os_str_bytes as os_str;
37
0731742a 38#[cfg(not(test))]
dfeec247 39pub fn init() {}
0731742a
XL
40
41/// This function is used to implement functionality that simply doesn't exist.
42/// Programs relying on this functionality will need to deal with the error.
532ac7d7 43pub fn unsupported<T>() -> crate::io::Result<T> {
0731742a
XL
44 Err(unsupported_err())
45}
46
532ac7d7 47pub fn unsupported_err() -> crate::io::Error {
dfeec247 48 crate::io::Error::new(ErrorKind::Other, "operation not supported on SGX yet")
0731742a
XL
49}
50
51/// This function is used to implement various functions that doesn't exist,
52/// but the lack of which might not be reason for error. If no error is
53/// returned, the program might very well be able to function normally. This is
54/// what happens when `SGX_INEFFECTIVE_ERROR` is set to `true`. If it is
55/// `false`, the behavior is the same as `unsupported`.
532ac7d7 56pub fn sgx_ineffective<T>(v: T) -> crate::io::Result<T> {
0731742a
XL
57 static SGX_INEFFECTIVE_ERROR: AtomicBool = AtomicBool::new(false);
58 if SGX_INEFFECTIVE_ERROR.load(Ordering::Relaxed) {
dfeec247
XL
59 Err(crate::io::Error::new(
60 ErrorKind::Other,
61 "operation can't be trusted to have any effect on SGX",
62 ))
0731742a
XL
63 } else {
64 Ok(v)
65 }
66}
67
532ac7d7 68pub fn decode_error_kind(code: i32) -> ErrorKind {
0731742a
XL
69 use fortanix_sgx_abi::Error;
70
71 // FIXME: not sure how to make sure all variants of Error are covered
72 if code == Error::NotFound as _ {
532ac7d7 73 ErrorKind::NotFound
0731742a 74 } else if code == Error::PermissionDenied as _ {
532ac7d7 75 ErrorKind::PermissionDenied
0731742a 76 } else if code == Error::ConnectionRefused as _ {
532ac7d7 77 ErrorKind::ConnectionRefused
0731742a 78 } else if code == Error::ConnectionReset as _ {
532ac7d7 79 ErrorKind::ConnectionReset
0731742a 80 } else if code == Error::ConnectionAborted as _ {
532ac7d7 81 ErrorKind::ConnectionAborted
0731742a 82 } else if code == Error::NotConnected as _ {
532ac7d7 83 ErrorKind::NotConnected
0731742a 84 } else if code == Error::AddrInUse as _ {
532ac7d7 85 ErrorKind::AddrInUse
0731742a 86 } else if code == Error::AddrNotAvailable as _ {
532ac7d7 87 ErrorKind::AddrNotAvailable
0731742a 88 } else if code == Error::BrokenPipe as _ {
532ac7d7 89 ErrorKind::BrokenPipe
0731742a 90 } else if code == Error::AlreadyExists as _ {
532ac7d7 91 ErrorKind::AlreadyExists
0731742a 92 } else if code == Error::WouldBlock as _ {
532ac7d7 93 ErrorKind::WouldBlock
0731742a 94 } else if code == Error::InvalidInput as _ {
532ac7d7 95 ErrorKind::InvalidInput
0731742a 96 } else if code == Error::InvalidData as _ {
532ac7d7 97 ErrorKind::InvalidData
0731742a 98 } else if code == Error::TimedOut as _ {
532ac7d7 99 ErrorKind::TimedOut
0731742a 100 } else if code == Error::WriteZero as _ {
532ac7d7 101 ErrorKind::WriteZero
0731742a 102 } else if code == Error::Interrupted as _ {
532ac7d7 103 ErrorKind::Interrupted
0731742a 104 } else if code == Error::Other as _ {
532ac7d7 105 ErrorKind::Other
0731742a 106 } else if code == Error::UnexpectedEof as _ {
532ac7d7 107 ErrorKind::UnexpectedEof
0731742a 108 } else {
532ac7d7 109 ErrorKind::Other
0731742a
XL
110 }
111}
112
113// This enum is used as the storage for a bunch of types which can't actually
114// exist.
115#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
116pub enum Void {}
117
118pub unsafe fn strlen(mut s: *const c_char) -> usize {
119 let mut n = 0;
120 while *s != 0 {
121 n += 1;
122 s = s.offset(1);
123 }
dfeec247 124 return n;
0731742a
XL
125}
126
127pub unsafe fn abort_internal() -> ! {
9fa01778 128 abi::usercalls::exit(true)
0731742a
XL
129}
130
532ac7d7
XL
131// This function is needed by the panic runtime. The symbol is named in
132// pre-link args for the target specification, so keep that in sync.
133#[cfg(not(test))]
134#[no_mangle]
135// NB. used by both libunwind and libpanic_abort
136pub unsafe extern "C" fn __rust_abort() {
137 abort_internal();
138}
139
0731742a
XL
140pub fn hashmap_random_keys() -> (u64, u64) {
141 fn rdrand64() -> u64 {
142 unsafe {
416331ca 143 let mut ret: u64 = 0;
0731742a 144 for _ in 0..10 {
532ac7d7 145 if crate::arch::x86_64::_rdrand64_step(&mut ret) == 1 {
0731742a
XL
146 return ret;
147 }
148 }
532ac7d7 149 rtabort!("Failed to obtain random data");
0731742a
XL
150 }
151 }
152 (rdrand64(), rdrand64())
153}
154
532ac7d7 155pub use crate::sys_common::{AsInner, FromInner, IntoInner};
0731742a
XL
156
157pub trait TryIntoInner<Inner>: Sized {
158 fn try_into_inner(self) -> Result<Inner, Self>;
159}