]>
Commit | Line | Data |
---|---|---|
e74abb32 XL |
1 | //! System bindings for HermitCore |
2 | //! | |
3 | //! This module contains the facade (aka platform-specific) implementations of | |
4 | //! OS level functionality for HermitCore. | |
5 | //! | |
6 | //! This is all super highly experimental and not actually intended for | |
7 | //! wide/production use yet, it's still all in the experimental category. This | |
8 | //! will likely change over time. | |
9 | //! | |
10 | //! Currently all functions here are basically stubs that immediately return | |
11 | //! errors. The hope is that with a portability lint we can turn actually just | |
12 | //! remove all this and just omit parts of the standard library if we're | |
13 | //! compiling for wasm. That way it's a compile time error for something that's | |
14 | //! guaranteed to be a runtime error! | |
15 | ||
29967ef6 XL |
16 | #![allow(unsafe_op_in_unsafe_fn)] |
17 | ||
e74abb32 | 18 | use crate::intrinsics; |
60c5eb7d | 19 | use crate::os::raw::c_char; |
e74abb32 XL |
20 | |
21 | pub mod alloc; | |
22 | pub mod args; | |
cdc7bbd5 | 23 | #[path = "../unix/cmath.rs"] |
e74abb32 | 24 | pub mod cmath; |
e74abb32 | 25 | pub mod env; |
e74abb32 | 26 | pub mod fd; |
60c5eb7d | 27 | pub mod fs; |
f2b60f7d | 28 | pub mod futex; |
cdc7bbd5 | 29 | #[path = "../unsupported/io.rs"] |
60c5eb7d XL |
30 | pub mod io; |
31 | pub mod memchr; | |
e74abb32 | 32 | pub mod net; |
60c5eb7d | 33 | pub mod os; |
94222f64 XL |
34 | #[path = "../unix/os_str.rs"] |
35 | pub mod os_str; | |
cdc7bbd5 | 36 | #[path = "../unix/path.rs"] |
e74abb32 | 37 | pub mod path; |
6a06907d | 38 | #[path = "../unsupported/pipe.rs"] |
e74abb32 | 39 | pub mod pipe; |
29967ef6 | 40 | #[path = "../unsupported/process.rs"] |
e74abb32 | 41 | pub mod process; |
60c5eb7d XL |
42 | pub mod stdio; |
43 | pub mod thread; | |
3dfed10e | 44 | pub mod thread_local_dtor; |
cdc7bbd5 | 45 | #[path = "../unsupported/thread_local_key.rs"] |
3dfed10e | 46 | pub mod thread_local_key; |
60c5eb7d | 47 | pub mod time; |
e74abb32 | 48 | |
f2b60f7d | 49 | #[path = "../unix/locks"] |
5e7ed085 | 50 | pub mod locks { |
f2b60f7d FG |
51 | mod futex_condvar; |
52 | mod futex_mutex; | |
53 | mod futex_rwlock; | |
54 | pub(crate) use futex_condvar::MovableCondvar; | |
55 | pub(crate) use futex_mutex::{MovableMutex, Mutex}; | |
56 | pub(crate) use futex_rwlock::{MovableRwLock, RwLock}; | |
5e7ed085 FG |
57 | } |
58 | ||
e74abb32 XL |
59 | use crate::io::ErrorKind; |
60 | ||
61 | #[allow(unused_extern_crates)] | |
62 | pub extern crate hermit_abi as abi; | |
63 | ||
64 | pub fn unsupported<T>() -> crate::io::Result<T> { | |
65 | Err(unsupported_err()) | |
66 | } | |
67 | ||
68 | pub fn unsupported_err() -> crate::io::Error { | |
5099ac24 | 69 | crate::io::const_io_error!( |
cdc7bbd5 | 70 | crate::io::ErrorKind::Unsupported, |
5099ac24 | 71 | "operation not supported on HermitCore yet", |
cdc7bbd5 | 72 | ) |
e74abb32 XL |
73 | } |
74 | ||
e74abb32 XL |
75 | #[no_mangle] |
76 | pub extern "C" fn floor(x: f64) -> f64 { | |
60c5eb7d | 77 | unsafe { intrinsics::floorf64(x) } |
e74abb32 XL |
78 | } |
79 | ||
f9f354fc XL |
80 | pub fn abort_internal() -> ! { |
81 | unsafe { | |
82 | abi::abort(); | |
83 | } | |
e74abb32 XL |
84 | } |
85 | ||
86 | // FIXME: just a workaround to test the system | |
87 | pub fn hashmap_random_keys() -> (u64, u64) { | |
88 | (1, 2) | |
89 | } | |
90 | ||
91 | // This function is needed by the panic runtime. The symbol is named in | |
92 | // pre-link args for the target specification, so keep that in sync. | |
93 | #[cfg(not(test))] | |
94 | #[no_mangle] | |
95 | // NB. used by both libunwind and libpanic_abort | |
f9f354fc | 96 | pub extern "C" fn __rust_abort() { |
e74abb32 XL |
97 | abort_internal(); |
98 | } | |
99 | ||
cdc7bbd5 XL |
100 | // SAFETY: must be called only once during runtime initialization. |
101 | // NOTE: this is not guaranteed to run, for example when Rust code is called externally. | |
f2b60f7d | 102 | pub unsafe fn init(argc: isize, argv: *const *const u8, _sigpipe: u8) { |
ba9703b0 | 103 | let _ = net::init(); |
cdc7bbd5 XL |
104 | args::init(argc, argv); |
105 | } | |
106 | ||
107 | // SAFETY: must be called only once during runtime cleanup. | |
108 | // NOTE: this is not guaranteed to run, for example when the program aborts. | |
2b03887a | 109 | pub unsafe fn cleanup() {} |
e74abb32 XL |
110 | |
111 | #[cfg(not(test))] | |
112 | #[no_mangle] | |
60c5eb7d XL |
113 | pub unsafe extern "C" fn runtime_entry( |
114 | argc: i32, | |
115 | argv: *const *const c_char, | |
116 | env: *const *const c_char, | |
117 | ) -> ! { | |
3dfed10e | 118 | use crate::sys::hermit::thread_local_dtor::run_dtors; |
e74abb32 XL |
119 | extern "C" { |
120 | fn main(argc: isize, argv: *const *const c_char) -> i32; | |
121 | } | |
122 | ||
123 | // initialize environment | |
124 | os::init_environment(env as *const *const i8); | |
125 | ||
126 | let result = main(argc as isize, argv); | |
127 | ||
ba9703b0 | 128 | run_dtors(); |
e74abb32 XL |
129 | abi::exit(result); |
130 | } | |
131 | ||
132 | pub fn decode_error_kind(errno: i32) -> ErrorKind { | |
133 | match errno { | |
134 | x if x == 13 as i32 => ErrorKind::PermissionDenied, | |
135 | x if x == 98 as i32 => ErrorKind::AddrInUse, | |
136 | x if x == 99 as i32 => ErrorKind::AddrNotAvailable, | |
137 | x if x == 11 as i32 => ErrorKind::WouldBlock, | |
138 | x if x == 103 as i32 => ErrorKind::ConnectionAborted, | |
139 | x if x == 111 as i32 => ErrorKind::ConnectionRefused, | |
140 | x if x == 104 as i32 => ErrorKind::ConnectionReset, | |
141 | x if x == 17 as i32 => ErrorKind::AlreadyExists, | |
142 | x if x == 4 as i32 => ErrorKind::Interrupted, | |
143 | x if x == 22 as i32 => ErrorKind::InvalidInput, | |
144 | x if x == 2 as i32 => ErrorKind::NotFound, | |
145 | x if x == 107 as i32 => ErrorKind::NotConnected, | |
146 | x if x == 1 as i32 => ErrorKind::PermissionDenied, | |
147 | x if x == 32 as i32 => ErrorKind::BrokenPipe, | |
148 | x if x == 110 as i32 => ErrorKind::TimedOut, | |
136023e0 | 149 | _ => ErrorKind::Uncategorized, |
e74abb32 XL |
150 | } |
151 | } | |
152 | ||
153 | pub fn cvt(result: i32) -> crate::io::Result<usize> { | |
60c5eb7d | 154 | if result < 0 { Err(crate::io::Error::from_raw_os_error(-result)) } else { Ok(result as usize) } |
e74abb32 | 155 | } |