]>
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 | ||
e74abb32 | 16 | use crate::intrinsics; |
60c5eb7d | 17 | use crate::os::raw::c_char; |
e74abb32 XL |
18 | |
19 | pub mod alloc; | |
20 | pub mod args; | |
e74abb32 | 21 | pub mod cmath; |
60c5eb7d | 22 | pub mod condvar; |
e74abb32 | 23 | pub mod env; |
ba9703b0 | 24 | pub mod ext; |
60c5eb7d | 25 | pub mod fast_thread_local; |
e74abb32 | 26 | pub mod fd; |
60c5eb7d XL |
27 | pub mod fs; |
28 | pub mod io; | |
29 | pub mod memchr; | |
30 | pub mod mutex; | |
e74abb32 | 31 | pub mod net; |
60c5eb7d | 32 | pub mod os; |
e74abb32 XL |
33 | pub mod path; |
34 | pub mod pipe; | |
35 | pub mod process; | |
60c5eb7d | 36 | pub mod rwlock; |
e74abb32 | 37 | pub mod stack_overflow; |
60c5eb7d XL |
38 | pub mod stdio; |
39 | pub mod thread; | |
e74abb32 | 40 | pub mod thread_local; |
60c5eb7d | 41 | pub mod time; |
e74abb32 | 42 | |
e74abb32 | 43 | use crate::io::ErrorKind; |
60c5eb7d | 44 | pub use crate::sys_common::os_str_bytes as os_str; |
e74abb32 XL |
45 | |
46 | #[allow(unused_extern_crates)] | |
47 | pub extern crate hermit_abi as abi; | |
48 | ||
49 | pub fn unsupported<T>() -> crate::io::Result<T> { | |
50 | Err(unsupported_err()) | |
51 | } | |
52 | ||
53 | pub fn unsupported_err() -> crate::io::Error { | |
60c5eb7d | 54 | crate::io::Error::new(crate::io::ErrorKind::Other, "operation not supported on HermitCore yet") |
e74abb32 XL |
55 | } |
56 | ||
57 | // This enum is used as the storage for a bunch of types which can't actually | |
58 | // exist. | |
59 | #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)] | |
60 | pub enum Void {} | |
61 | ||
62 | pub unsafe fn strlen(start: *const c_char) -> usize { | |
63 | let mut str = start; | |
64 | ||
65 | while *str != 0 { | |
66 | str = str.offset(1); | |
67 | } | |
68 | ||
69 | (str as usize) - (start as usize) | |
70 | } | |
71 | ||
72 | #[no_mangle] | |
73 | pub extern "C" fn floor(x: f64) -> f64 { | |
60c5eb7d | 74 | unsafe { intrinsics::floorf64(x) } |
e74abb32 XL |
75 | } |
76 | ||
f9f354fc XL |
77 | pub fn abort_internal() -> ! { |
78 | unsafe { | |
79 | abi::abort(); | |
80 | } | |
e74abb32 XL |
81 | } |
82 | ||
83 | // FIXME: just a workaround to test the system | |
84 | pub fn hashmap_random_keys() -> (u64, u64) { | |
85 | (1, 2) | |
86 | } | |
87 | ||
88 | // This function is needed by the panic runtime. The symbol is named in | |
89 | // pre-link args for the target specification, so keep that in sync. | |
90 | #[cfg(not(test))] | |
91 | #[no_mangle] | |
92 | // NB. used by both libunwind and libpanic_abort | |
f9f354fc | 93 | pub extern "C" fn __rust_abort() { |
e74abb32 XL |
94 | abort_internal(); |
95 | } | |
96 | ||
97 | #[cfg(not(test))] | |
98 | pub fn init() { | |
ba9703b0 | 99 | let _ = net::init(); |
e74abb32 XL |
100 | } |
101 | ||
102 | #[cfg(not(test))] | |
103 | #[no_mangle] | |
60c5eb7d XL |
104 | pub unsafe extern "C" fn runtime_entry( |
105 | argc: i32, | |
106 | argv: *const *const c_char, | |
107 | env: *const *const c_char, | |
108 | ) -> ! { | |
ba9703b0 | 109 | use crate::sys::hermit::fast_thread_local::run_dtors; |
e74abb32 XL |
110 | extern "C" { |
111 | fn main(argc: isize, argv: *const *const c_char) -> i32; | |
112 | } | |
113 | ||
114 | // initialize environment | |
115 | os::init_environment(env as *const *const i8); | |
116 | ||
117 | let result = main(argc as isize, argv); | |
118 | ||
ba9703b0 | 119 | run_dtors(); |
e74abb32 XL |
120 | abi::exit(result); |
121 | } | |
122 | ||
123 | pub fn decode_error_kind(errno: i32) -> ErrorKind { | |
124 | match errno { | |
125 | x if x == 13 as i32 => ErrorKind::PermissionDenied, | |
126 | x if x == 98 as i32 => ErrorKind::AddrInUse, | |
127 | x if x == 99 as i32 => ErrorKind::AddrNotAvailable, | |
128 | x if x == 11 as i32 => ErrorKind::WouldBlock, | |
129 | x if x == 103 as i32 => ErrorKind::ConnectionAborted, | |
130 | x if x == 111 as i32 => ErrorKind::ConnectionRefused, | |
131 | x if x == 104 as i32 => ErrorKind::ConnectionReset, | |
132 | x if x == 17 as i32 => ErrorKind::AlreadyExists, | |
133 | x if x == 4 as i32 => ErrorKind::Interrupted, | |
134 | x if x == 22 as i32 => ErrorKind::InvalidInput, | |
135 | x if x == 2 as i32 => ErrorKind::NotFound, | |
136 | x if x == 107 as i32 => ErrorKind::NotConnected, | |
137 | x if x == 1 as i32 => ErrorKind::PermissionDenied, | |
138 | x if x == 32 as i32 => ErrorKind::BrokenPipe, | |
139 | x if x == 110 as i32 => ErrorKind::TimedOut, | |
140 | _ => ErrorKind::Other, | |
141 | } | |
142 | } | |
143 | ||
144 | pub fn cvt(result: i32) -> crate::io::Result<usize> { | |
60c5eb7d | 145 | if result < 0 { Err(crate::io::Error::from_raw_os_error(-result)) } else { Ok(result as usize) } |
e74abb32 | 146 | } |