From b1f04a69c2864d3ac143c341b3f1a1db273ed293 Mon Sep 17 00:00:00 2001 From: Meziu Date: Tue, 14 Sep 2021 19:24:38 +0200 Subject: [PATCH 1/6] ARMv6K Horizon OS support --- src/unix/newlib/horizon/mod.rs | 193 +++++++++++++++++++++++++++++++++ src/unix/newlib/mod.rs | 3 + 2 files changed, 196 insertions(+) create mode 100644 src/unix/newlib/horizon/mod.rs diff --git a/src/unix/newlib/horizon/mod.rs b/src/unix/newlib/horizon/mod.rs new file mode 100644 index 0000000000000..e25bd82345f24 --- /dev/null +++ b/src/unix/newlib/horizon/mod.rs @@ -0,0 +1,193 @@ +//! ARMv6K Nintendo 3DS C Newlib definitions + +pub type c_char = u8; +pub type c_long = i32; +pub type c_ulong = u32; + +pub type wchar_t = ::c_uint; + +pub type in_port_t = ::c_ushort; +pub type u_register_t = ::c_uint; +pub type u_char = ::c_uchar; +pub type u_short = ::c_ushort; +pub type u_int = ::c_uint; +pub type u_long = c_ulong; +pub type ushort = ::c_ushort; +pub type uint = ::c_uint; +pub type ulong = c_ulong; +pub type clock_t = c_ulong; +pub type daddr_t = c_long; +pub type caddr_t = *mut c_char; +pub type sbintime_t = ::c_longlong; + +pub const SIGEV_NONE: ::c_int = 1; +pub const SIGEV_SIGNAL: ::c_int = 2; +pub const SIGEV_THREAD: ::c_int = 3; +pub const SA_NOCLDSTOP: ::c_int = 1; +pub const MINSIGSTKSZ: ::c_int = 2048; +pub const SIGSTKSZ: ::c_int = 8192; +pub const SS_ONSTACK: ::c_int = 1; +pub const SS_DISABLE: ::c_int = 2; +pub const SIG_SETMASK: ::c_int = 0; +pub const SIG_BLOCK: ::c_int = 1; +pub const SIG_UNBLOCK: ::c_int = 2; +pub const SIGHUP: ::c_int = 1; +pub const SIGINT: ::c_int = 2; +pub const SIGQUIT: ::c_int = 3; +pub const SIGILL: ::c_int = 4; +pub const SIGTRAP: ::c_int = 5; +pub const SIGABRT: ::c_int = 6; +pub const SIGEMT: ::c_int = 7; +pub const SIGFPE: ::c_int = 8; +pub const SIGKILL: ::c_int = 9; +pub const SIGBUS: ::c_int = 10; +pub const SIGSEGV: ::c_int = 11; +pub const SIGSYS: ::c_int = 12; +pub const SIGPIPE: ::c_int = 13; +pub const SIGALRM: ::c_int = 14; +pub const SIGTERM: ::c_int = 15; +pub const SIGURG: ::c_int = 16; +pub const SIGSTOP: ::c_int = 17; +pub const SIGTSTP: ::c_int = 18; +pub const SIGCONT: ::c_int = 19; +pub const SIGCHLD: ::c_int = 20; +pub const SIGCLD: ::c_int = 20; +pub const SIGTTIN: ::c_int = 21; +pub const SIGTTOU: ::c_int = 22; +pub const SIGIO: ::c_int = 23; +pub const SIGPOLL: ::c_int = 23; +pub const SIGXCPU: ::c_int = 24; +pub const SIGXFSZ: ::c_int = 25; +pub const SIGVTALRM: ::c_int = 26; +pub const SIGPROF: ::c_int = 27; +pub const SIGWINCH: ::c_int = 28; +pub const SIGLOST: ::c_int = 29; +pub const SIGUSR1: ::c_int = 30; +pub const SIGUSR2: ::c_int = 31; +pub const NSIG: ::c_int = 32; +pub const CLOCK_ENABLED: ::c_uint = 1; +pub const CLOCK_DISABLED: ::c_uint = 0; +pub const CLOCK_ALLOWED: ::c_uint = 1; +pub const CLOCK_DISALLOWED: ::c_uint = 0; +pub const TIMER_ABSTIME: ::c_uint = 4; +pub const SOL_SOCKET: ::c_int = 65535; +pub const MSG_OOB: ::c_int = 1; +pub const MSG_PEEK: ::c_int = 2; +pub const MSG_DONTWAIT: ::c_int = 4; +pub const MSG_DONTROUTE: ::c_int = 0; +pub const MSG_WAITALL: ::c_int = 0; +pub const MSG_MORE: ::c_int = 0; +pub const MSG_NOSIGNAL: ::c_int = 0; +pub const SOL_CONFIG: ::c_uint = 65534; + +pub const _SC_PAGESIZE: ::c_int = 8; +pub const _SC_GETPW_R_SIZE_MAX: ::c_int = 51; + +pub const PTHREAD_STACK_MIN: ::size_t = 4096; +pub const WNOHANG: ::c_int = 1; + +pub const POLLIN: ::c_short = 0x0001; +pub const POLLPRI: ::c_short = 0x0002; +pub const POLLOUT: ::c_short = 0x0004; +pub const POLLRDNORM: ::c_short = 0x0040; +pub const POLLWRNORM: ::c_short = POLLOUT; +pub const POLLRDBAND: ::c_short = 0x0080; +pub const POLLWRBAND: ::c_short = 0x0100; +pub const POLLERR: ::c_short = 0x0008; +pub const POLLHUP: ::c_short = 0x0010; +pub const POLLNVAL: ::c_short = 0x0020; + +pub const EAI_AGAIN: ::c_int = 2; +pub const EAI_BADFLAGS: ::c_int = 3; +pub const EAI_FAIL: ::c_int = 4; +pub const EAI_SERVICE: ::c_int = 9; +pub const EAI_SYSTEM: ::c_int = 11; +pub const EAI_BADHINTS: ::c_int = 12; +pub const EAI_PROTOCOL: ::c_int = 13; +pub const EAI_OVERFLOW: ::c_int = 14; +pub const EAI_MAX: ::c_int = 15; + +pub const AF_UNIX: ::c_int = 1; +pub const AF_INET6: ::c_int = 23; + +pub const FIONBIO: ::c_ulong = 1; + +pub const RTLD_DEFAULT: *mut ::c_void = 0 as *mut ::c_void; + +// External implementations are needed to use networking and threading. +s! { + pub struct sockaddr { + pub sa_family: ::sa_family_t, + pub sa_data: [::c_char; 14], + } + + pub struct sockaddr_storage { + pub ss_family: ::sa_family_t, + pub __ss_padding: [c_char; 26usize], + } + + pub struct sockaddr_in { + pub sin_family: ::sa_family_t, + pub sin_port: in_port_t, + pub sin_addr: ::in_addr, + pub sin_zero: [::c_uchar; 8usize], + } + + pub struct sockaddr_in6 { + pub sin6_family: ::sa_family_t, + pub sin6_port: ::in_port_t, + pub sin6_flowinfo: u32, + pub sin6_addr: ::in6_addr, + pub sin6_scope_id: u32, + } + + pub struct sockaddr_un { + pub sun_len: ::c_uchar, + pub sun_family: ::sa_family_t, + pub sun_path: [::c_char; 104usize], + } +} + +// Horizon OS works doesn't or can't hold any of this information +safe_f! { + pub {const} fn WIFSTOPPED(_status: ::c_int) -> bool { + false + } + + pub {const} fn WSTOPSIG(_status: ::c_int) -> ::c_int { + 0 + } + + pub {const} fn WIFCONTINUED(_status: ::c_int) -> bool { + true + } + + pub {const} fn WIFSIGNALED(_status: ::c_int) -> bool { + false + } + + pub {const} fn WTERMSIG(_status: ::c_int) -> ::c_int { + 0 + } + + pub {const} fn WIFEXITED(_status: ::c_int) -> bool { + true + } + + pub {const} fn WEXITSTATUS(_status: ::c_int) -> ::c_int { + 0 + } + + pub {const} fn WCOREDUMP(_status: ::c_int) -> bool { + false + } +} + +extern "C" { + pub fn pthread_create( + native: *mut ::pthread_t, + attr: *const ::pthread_attr_t, + f: extern "C" fn(_: *mut ::c_void) -> *mut ::c_void, + value: *mut ::c_void, + ) -> ::c_int; +} diff --git a/src/unix/newlib/mod.rs b/src/unix/newlib/mod.rs index 3a7cfa4a25c30..642c13f61c676 100644 --- a/src/unix/newlib/mod.rs +++ b/src/unix/newlib/mod.rs @@ -706,6 +706,9 @@ cfg_if! { if #[cfg(target_os = "espidf")] { mod espidf; pub use self::espidf::*; + } else if #[cfg(target_os = "horizon")] { + mod horizon; + pub use self::horizon::*; } else if #[cfg(target_arch = "arm")] { mod arm; pub use self::arm::*; From 616d1ea09e79d7285104348b91b492eb4fd7badc Mon Sep 17 00:00:00 2001 From: Meziu Date: Sun, 24 Oct 2021 20:34:57 +0200 Subject: [PATCH 2/6] Fixed struct/constants order --- src/unix/newlib/horizon/mod.rs | 68 +++++++++++++++++----------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/src/unix/newlib/horizon/mod.rs b/src/unix/newlib/horizon/mod.rs index e25bd82345f24..90c0f5aff17b1 100644 --- a/src/unix/newlib/horizon/mod.rs +++ b/src/unix/newlib/horizon/mod.rs @@ -20,6 +20,40 @@ pub type daddr_t = c_long; pub type caddr_t = *mut c_char; pub type sbintime_t = ::c_longlong; +// External implementations are needed to use networking and threading. +s! { + pub struct sockaddr { + pub sa_family: ::sa_family_t, + pub sa_data: [::c_char; 14], + } + + pub struct sockaddr_storage { + pub ss_family: ::sa_family_t, + pub __ss_padding: [c_char; 26usize], + } + + pub struct sockaddr_in { + pub sin_family: ::sa_family_t, + pub sin_port: in_port_t, + pub sin_addr: ::in_addr, + pub sin_zero: [::c_uchar; 8usize], + } + + pub struct sockaddr_in6 { + pub sin6_family: ::sa_family_t, + pub sin6_port: ::in_port_t, + pub sin6_flowinfo: u32, + pub sin6_addr: ::in6_addr, + pub sin6_scope_id: u32, + } + + pub struct sockaddr_un { + pub sun_len: ::c_uchar, + pub sun_family: ::sa_family_t, + pub sun_path: [::c_char; 104usize], + } +} + pub const SIGEV_NONE: ::c_int = 1; pub const SIGEV_SIGNAL: ::c_int = 2; pub const SIGEV_THREAD: ::c_int = 3; @@ -114,40 +148,6 @@ pub const FIONBIO: ::c_ulong = 1; pub const RTLD_DEFAULT: *mut ::c_void = 0 as *mut ::c_void; -// External implementations are needed to use networking and threading. -s! { - pub struct sockaddr { - pub sa_family: ::sa_family_t, - pub sa_data: [::c_char; 14], - } - - pub struct sockaddr_storage { - pub ss_family: ::sa_family_t, - pub __ss_padding: [c_char; 26usize], - } - - pub struct sockaddr_in { - pub sin_family: ::sa_family_t, - pub sin_port: in_port_t, - pub sin_addr: ::in_addr, - pub sin_zero: [::c_uchar; 8usize], - } - - pub struct sockaddr_in6 { - pub sin6_family: ::sa_family_t, - pub sin6_port: ::in_port_t, - pub sin6_flowinfo: u32, - pub sin6_addr: ::in6_addr, - pub sin6_scope_id: u32, - } - - pub struct sockaddr_un { - pub sun_len: ::c_uchar, - pub sun_family: ::sa_family_t, - pub sun_path: [::c_char; 104usize], - } -} - // Horizon OS works doesn't or can't hold any of this information safe_f! { pub {const} fn WIFSTOPPED(_status: ::c_int) -> bool { From 0674d9038da6c16b017127f621c3870150ffb736 Mon Sep 17 00:00:00 2001 From: Meziu Date: Sun, 26 Dec 2021 17:55:40 +0100 Subject: [PATCH 3/6] Fixed network implementations --- src/unix/newlib/horizon/mod.rs | 2 +- src/unix/newlib/mod.rs | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/unix/newlib/horizon/mod.rs b/src/unix/newlib/horizon/mod.rs index 90c0f5aff17b1..a1867bf0a51da 100644 --- a/src/unix/newlib/horizon/mod.rs +++ b/src/unix/newlib/horizon/mod.rs @@ -24,7 +24,7 @@ pub type sbintime_t = ::c_longlong; s! { pub struct sockaddr { pub sa_family: ::sa_family_t, - pub sa_data: [::c_char; 14], + pub sa_data: *const c_char, } pub struct sockaddr_storage { diff --git a/src/unix/newlib/mod.rs b/src/unix/newlib/mod.rs index 642c13f61c676..197fe395b3c2e 100644 --- a/src/unix/newlib/mod.rs +++ b/src/unix/newlib/mod.rs @@ -15,7 +15,10 @@ pub type off_t = i64; pub type pthread_t = ::c_ulong; pub type pthread_key_t = ::c_uint; pub type rlim_t = u32; +#[cfg(not(target_os = "horizon"))] pub type sa_family_t = u8; +#[cfg(target_os = "horizon")] +pub type sa_family_t = u16; pub type socklen_t = u32; pub type speed_t = u32; pub type suseconds_t = i32; @@ -477,7 +480,10 @@ pub const SO_SNDLOWAT: ::c_int = 0x1003; pub const SO_RCVLOWAT: ::c_int = 0x1004; pub const SO_SNDTIMEO: ::c_int = 0x1005; pub const SO_RCVTIMEO: ::c_int = 0x1006; +#[cfg(not(target_os = "horizon"))] pub const SO_ERROR: ::c_int = 0x1007; +#[cfg(target_os = "horizon")] +pub const SO_ERROR: ::c_int = 0x1009; pub const SO_TYPE: ::c_int = 0x1008; pub const SOCK_CLOEXEC: ::c_int = O_CLOEXEC; @@ -512,7 +518,10 @@ pub const TCP_KEEPIDLE: ::c_int = 256; pub const TCP_KEEPINTVL: ::c_int = 512; pub const TCP_KEEPCNT: ::c_int = 1024; +#[cfg(not(target_os = "horizon"))] pub const IP_TOS: ::c_int = 3; +#[cfg(target_os = "horizon")] +pub const IP_TOS: ::c_int = 7; pub const IP_TTL: ::c_int = 8; pub const IP_MULTICAST_IF: ::c_int = 9; pub const IP_MULTICAST_TTL: ::c_int = 10; From 63a2ff70284848ab5304160480c52f762f3365cf Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Mon, 27 Dec 2021 13:48:39 +0100 Subject: [PATCH 4/6] Fixed conditional programming lint --- src/unix/newlib/horizon/mod.rs | 5 +++-- src/unix/newlib/mod.rs | 35 ++++++++++++++++++++++------------ 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/unix/newlib/horizon/mod.rs b/src/unix/newlib/horizon/mod.rs index a1867bf0a51da..de9a1186ecdd6 100644 --- a/src/unix/newlib/horizon/mod.rs +++ b/src/unix/newlib/horizon/mod.rs @@ -29,14 +29,13 @@ s! { pub struct sockaddr_storage { pub ss_family: ::sa_family_t, - pub __ss_padding: [c_char; 26usize], + pub __ss_padding: [::c_char; 26usize], } pub struct sockaddr_in { pub sin_family: ::sa_family_t, pub sin_port: in_port_t, pub sin_addr: ::in_addr, - pub sin_zero: [::c_uchar; 8usize], } pub struct sockaddr_in6 { @@ -190,4 +189,6 @@ extern "C" { f: extern "C" fn(_: *mut ::c_void) -> *mut ::c_void, value: *mut ::c_void, ) -> ::c_int; + + pub fn gethostid() -> ::c_long; } diff --git a/src/unix/newlib/mod.rs b/src/unix/newlib/mod.rs index 197fe395b3c2e..b1300f1692760 100644 --- a/src/unix/newlib/mod.rs +++ b/src/unix/newlib/mod.rs @@ -15,10 +15,15 @@ pub type off_t = i64; pub type pthread_t = ::c_ulong; pub type pthread_key_t = ::c_uint; pub type rlim_t = u32; -#[cfg(not(target_os = "horizon"))] -pub type sa_family_t = u8; -#[cfg(target_os = "horizon")] -pub type sa_family_t = u16; + +cfg_if! { + if #[cfg(target_os = "horizon")] { + pub type sa_family_t = u16; + } else { + pub type sa_family_t = u8; + } +} + pub type socklen_t = u32; pub type speed_t = u32; pub type suseconds_t = i32; @@ -480,10 +485,13 @@ pub const SO_SNDLOWAT: ::c_int = 0x1003; pub const SO_RCVLOWAT: ::c_int = 0x1004; pub const SO_SNDTIMEO: ::c_int = 0x1005; pub const SO_RCVTIMEO: ::c_int = 0x1006; -#[cfg(not(target_os = "horizon"))] -pub const SO_ERROR: ::c_int = 0x1007; -#[cfg(target_os = "horizon")] -pub const SO_ERROR: ::c_int = 0x1009; +cfg_if! { + if #[cfg(target_os = "horizon")] { + pub const SO_ERROR: ::c_int = 0x1009; + } else { + pub const SO_ERROR: ::c_int = 0x1007; + } +} pub const SO_TYPE: ::c_int = 0x1008; pub const SOCK_CLOEXEC: ::c_int = O_CLOEXEC; @@ -518,10 +526,13 @@ pub const TCP_KEEPIDLE: ::c_int = 256; pub const TCP_KEEPINTVL: ::c_int = 512; pub const TCP_KEEPCNT: ::c_int = 1024; -#[cfg(not(target_os = "horizon"))] -pub const IP_TOS: ::c_int = 3; -#[cfg(target_os = "horizon")] -pub const IP_TOS: ::c_int = 7; +cfg_if! { + if #[cfg(target_os = "horizon")] { + pub const IP_TOS: ::c_int = 7; + } else { + pub const IP_TOS: ::c_int = 3; + } +} pub const IP_TTL: ::c_int = 8; pub const IP_MULTICAST_IF: ::c_int = 9; pub const IP_MULTICAST_TTL: ::c_int = 10; From cef3b81128c262b68f983e91621002c294108e85 Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Thu, 30 Dec 2021 10:41:34 +0100 Subject: [PATCH 5/6] Fixed sockaddr definition --- src/unix/newlib/horizon/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/unix/newlib/horizon/mod.rs b/src/unix/newlib/horizon/mod.rs index de9a1186ecdd6..a154d72707b9c 100644 --- a/src/unix/newlib/horizon/mod.rs +++ b/src/unix/newlib/horizon/mod.rs @@ -24,7 +24,7 @@ pub type sbintime_t = ::c_longlong; s! { pub struct sockaddr { pub sa_family: ::sa_family_t, - pub sa_data: *const c_char, + pub sa_data: [::c_char; 26usize], } pub struct sockaddr_storage { From 98ef7e73e32139fb5cf47df40421b5dc88c9434f Mon Sep 17 00:00:00 2001 From: Ian Chamberlain Date: Wed, 19 Jan 2022 22:16:42 -0500 Subject: [PATCH 6/6] Use c_longlong for time_t on horizon --- src/unix/newlib/mod.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/unix/newlib/mod.rs b/src/unix/newlib/mod.rs index b1300f1692760..f1d738cb05b8e 100644 --- a/src/unix/newlib/mod.rs +++ b/src/unix/newlib/mod.rs @@ -28,9 +28,16 @@ pub type socklen_t = u32; pub type speed_t = u32; pub type suseconds_t = i32; pub type tcflag_t = ::c_uint; -pub type time_t = i32; pub type useconds_t = u32; +cfg_if! { + if #[cfg(target_os = "horizon")] { + pub type time_t = ::c_longlong; + } else { + pub type time_t = i32; + } +} + s! { // The order of the `ai_addr` field in this struct is crucial // for converting between the Rust and C types.