From 85210e6b2e226216431f80f5a0b3ebb329f98adb Mon Sep 17 00:00:00 2001 From: imarkov Date: Thu, 29 Jul 2021 22:04:44 +0300 Subject: [PATCH] Support for the ESP-IDF framework (Xtensa and RiscV arch) --- src/unix/mod.rs | 18 +++++++- src/unix/newlib/{xtensa => espidf}/mod.rs | 16 +++++-- src/unix/newlib/mod.rs | 52 ++++++++++++++--------- 3 files changed, 61 insertions(+), 25 deletions(-) rename src/unix/newlib/{xtensa => espidf}/mod.rs (85%) diff --git a/src/unix/mod.rs b/src/unix/mod.rs index be7b6e73e8c59..f91ae616702ec 100644 --- a/src/unix/mod.rs +++ b/src/unix/mod.rs @@ -297,8 +297,8 @@ pub const ATF_PUBL: ::c_int = 0x08; pub const ATF_USETRAILERS: ::c_int = 0x10; cfg_if! { - if #[cfg(target_os = "l4re")] { - // required libraries for L4Re are linked externally, ATM + if #[cfg(any(target_os = "l4re", target_os = "espidf"))] { + // required libraries for L4Re and the ESP-IDF framework are linked externally, ATM } else if #[cfg(feature = "std")] { // cargo build, don't pull in anything extra as the libstd dep // already pulls in all libs. @@ -576,6 +576,7 @@ extern "C" { )))] #[cfg_attr(target_os = "netbsd", link_name = "__socket30")] #[cfg_attr(target_os = "illumos", link_name = "__xnet_socket")] + #[cfg_attr(target_os = "espidf", link_name = "lwip_socket")] pub fn socket(domain: ::c_int, ty: ::c_int, protocol: ::c_int) -> ::c_int; #[cfg(not(all( libc_cfg_target_vendor, @@ -587,11 +588,13 @@ extern "C" { link_name = "connect$UNIX2003" )] #[cfg_attr(target_os = "illumos", link_name = "__xnet_connect")] + #[cfg_attr(target_os = "espidf", link_name = "lwip_connect")] pub fn connect(socket: ::c_int, address: *const sockaddr, len: socklen_t) -> ::c_int; #[cfg_attr( all(target_os = "macos", target_arch = "x86"), link_name = "listen$UNIX2003" )] + #[cfg_attr(target_os = "espidf", link_name = "lwip_listen")] pub fn listen(socket: ::c_int, backlog: ::c_int) -> ::c_int; #[cfg(not(all( libc_cfg_target_vendor, @@ -602,6 +605,7 @@ extern "C" { all(target_os = "macos", target_arch = "x86"), link_name = "accept$UNIX2003" )] + #[cfg_attr(target_os = "espidf", link_name = "lwip_accept")] pub fn accept(socket: ::c_int, address: *mut sockaddr, address_len: *mut socklen_t) -> ::c_int; #[cfg(not(all( libc_cfg_target_vendor, @@ -612,6 +616,7 @@ extern "C" { all(target_os = "macos", target_arch = "x86"), link_name = "getpeername$UNIX2003" )] + #[cfg_attr(target_os = "espidf", link_name = "lwip_getpeername")] pub fn getpeername( socket: ::c_int, address: *mut sockaddr, @@ -626,11 +631,13 @@ extern "C" { all(target_os = "macos", target_arch = "x86"), link_name = "getsockname$UNIX2003" )] + #[cfg_attr(target_os = "espidf", link_name = "lwip_getsockname")] pub fn getsockname( socket: ::c_int, address: *mut sockaddr, address_len: *mut socklen_t, ) -> ::c_int; + #[cfg_attr(target_os = "espidf", link_name = "lwip_setsockopt")] pub fn setsockopt( socket: ::c_int, level: ::c_int, @@ -659,6 +666,7 @@ extern "C" { link_name = "sendto$UNIX2003" )] #[cfg_attr(target_os = "illumos", link_name = "__xnet_sendto")] + #[cfg_attr(target_os = "espidf", link_name = "lwip_sendto")] pub fn sendto( socket: ::c_int, buf: *const ::c_void, @@ -667,6 +675,7 @@ extern "C" { addr: *const sockaddr, addrlen: socklen_t, ) -> ::ssize_t; + #[cfg_attr(target_os = "espidf", link_name = "lwip_shutdown")] pub fn shutdown(socket: ::c_int, how: ::c_int) -> ::c_int; #[cfg_attr( @@ -1122,6 +1131,7 @@ extern "C" { pub fn pthread_rwlockattr_destroy(attr: *mut pthread_rwlockattr_t) -> ::c_int; #[cfg_attr(target_os = "illumos", link_name = "__xnet_getsockopt")] + #[cfg_attr(target_os = "espidf", link_name = "lwip_getsockopt")] pub fn getsockopt( sockfd: ::c_int, level: ::c_int, @@ -1147,6 +1157,7 @@ extern "C" { target_vendor = "nintendo" )))] #[cfg_attr(target_os = "illumos", link_name = "__xnet_getaddrinfo")] + #[cfg_attr(target_os = "espidf", link_name = "lwip_getaddrinfo")] pub fn getaddrinfo( node: *const c_char, service: *const c_char, @@ -1158,6 +1169,7 @@ extern "C" { target_arch = "powerpc", target_vendor = "nintendo" )))] + #[cfg_attr(target_os = "espidf", link_name = "lwip_freeaddrinfo")] pub fn freeaddrinfo(res: *mut addrinfo); pub fn gai_strerror(errcode: ::c_int) -> *const ::c_char; #[cfg_attr( @@ -1233,11 +1245,13 @@ extern "C" { all(target_os = "macos", target_arch = "x86"), link_name = "send$UNIX2003" )] + #[cfg_attr(target_os = "espidf", link_name = "lwip_send")] pub fn send(socket: ::c_int, buf: *const ::c_void, len: ::size_t, flags: ::c_int) -> ::ssize_t; #[cfg_attr( all(target_os = "macos", target_arch = "x86"), link_name = "recv$UNIX2003" )] + #[cfg_attr(target_os = "espidf", link_name = "lwip_recv")] pub fn recv(socket: ::c_int, buf: *mut ::c_void, len: ::size_t, flags: ::c_int) -> ::ssize_t; #[cfg_attr( all(target_os = "macos", target_arch = "x86"), diff --git a/src/unix/newlib/xtensa/mod.rs b/src/unix/newlib/espidf/mod.rs similarity index 85% rename from src/unix/newlib/xtensa/mod.rs rename to src/unix/newlib/espidf/mod.rs index f520f7a1ee6f6..d3e1059465f7f 100644 --- a/src/unix/newlib/xtensa/mod.rs +++ b/src/unix/newlib/espidf/mod.rs @@ -84,10 +84,20 @@ pub const MSG_WAITALL: ::c_int = 0x02; pub const MSG_MORE: ::c_int = 0x10; pub const MSG_NOSIGNAL: ::c_int = 0x20; +pub const PTHREAD_STACK_MIN: ::size_t = 768; + 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; + + pub fn getrandom(buf: *mut ::c_void, buflen: ::size_t, flags: ::c_uint) -> ::ssize_t; + + #[link_name = "lwip_sendmsg"] pub fn sendmsg(s: ::c_int, msg: *const ::msghdr, flags: ::c_int) -> ::ssize_t; + #[link_name = "lwip_recvmsg"] pub fn recvmsg(s: ::c_int, msg: *mut ::msghdr, flags: ::c_int) -> ::ssize_t; - - pub fn writev(s: ::c_int, iov: *const ::iovec, iovcnt: ::c_int) -> ::c_int; - pub fn readv(fd: ::c_int, iov: *const ::iovec, iovcnt: ::c_int) -> ::ssize_t; } diff --git a/src/unix/newlib/mod.rs b/src/unix/newlib/mod.rs index 59256b0c206ce..4a35356d0c66c 100644 --- a/src/unix/newlib/mod.rs +++ b/src/unix/newlib/mod.rs @@ -33,16 +33,14 @@ s! { pub ai_protocol: ::c_int, pub ai_addrlen: socklen_t, - #[cfg(not(all(libc_cfg_target_vendor, target_arch = "powerpc", - target_vendor = "nintendo")))] - #[cfg(target_arch = "xtensa")] + #[cfg(target_os = "espidf")] pub ai_addr: *mut sockaddr, pub ai_canonname: *mut ::c_char, - #[cfg(not(all(libc_cfg_target_vendor, target_arch = "powerpc", - target_vendor = "nintendo")))] - #[cfg(not(target_arch = "xtensa"))] + #[cfg(not(any( + target_os = "espidf", + all(libc_cfg_target_vendor, target_arch = "powerpc", target_vendor = "nintendo"))))] pub ai_addr: *mut sockaddr, pub ai_next: *mut addrinfo, @@ -232,23 +230,37 @@ s! { // unverified constants align_const! { pub const PTHREAD_MUTEX_INITIALIZER: pthread_mutex_t = pthread_mutex_t { - size: [0; __SIZEOF_PTHREAD_MUTEX_T], + size: [__PTHREAD_INITIALIZER_BYTE; __SIZEOF_PTHREAD_MUTEX_T], }; pub const PTHREAD_COND_INITIALIZER: pthread_cond_t = pthread_cond_t { - size: [0; __SIZEOF_PTHREAD_COND_T], + size: [__PTHREAD_INITIALIZER_BYTE; __SIZEOF_PTHREAD_COND_T], }; pub const PTHREAD_RWLOCK_INITIALIZER: pthread_rwlock_t = pthread_rwlock_t { - size: [0; __SIZEOF_PTHREAD_RWLOCK_T], + size: [__PTHREAD_INITIALIZER_BYTE; __SIZEOF_PTHREAD_RWLOCK_T], }; } pub const NCCS: usize = 32; -pub const __SIZEOF_PTHREAD_ATTR_T: usize = 56; -pub const __SIZEOF_PTHREAD_MUTEX_T: usize = 40; -pub const __SIZEOF_PTHREAD_MUTEXATTR_T: usize = 4; -pub const __SIZEOF_PTHREAD_COND_T: usize = 48; -pub const __SIZEOF_PTHREAD_CONDATTR_T: usize = 4; -pub const __SIZEOF_PTHREAD_RWLOCK_T: usize = 56; -pub const __SIZEOF_PTHREAD_RWLOCKATTR_T: usize = 8; +cfg_if! { + if #[cfg(target_os = "espidf")] { + const __PTHREAD_INITIALIZER_BYTE: u8 = 0xff; + pub const __SIZEOF_PTHREAD_ATTR_T: usize = 32; + pub const __SIZEOF_PTHREAD_MUTEX_T: usize = 4; + pub const __SIZEOF_PTHREAD_MUTEXATTR_T: usize = 12; + pub const __SIZEOF_PTHREAD_COND_T: usize = 4; + pub const __SIZEOF_PTHREAD_CONDATTR_T: usize = 8; + pub const __SIZEOF_PTHREAD_RWLOCK_T: usize = 4; + pub const __SIZEOF_PTHREAD_RWLOCKATTR_T: usize = 12; + } else { + const __PTHREAD_INITIALIZER_BYTE: u8 = 0; + pub const __SIZEOF_PTHREAD_ATTR_T: usize = 56; + pub const __SIZEOF_PTHREAD_MUTEX_T: usize = 40; + pub const __SIZEOF_PTHREAD_MUTEXATTR_T: usize = 4; + pub const __SIZEOF_PTHREAD_COND_T: usize = 48; + pub const __SIZEOF_PTHREAD_CONDATTR_T: usize = 4; + pub const __SIZEOF_PTHREAD_RWLOCK_T: usize = 56; + pub const __SIZEOF_PTHREAD_RWLOCKATTR_T: usize = 8; + } +} pub const __SIZEOF_PTHREAD_BARRIER_T: usize = 32; pub const __SIZEOF_PTHREAD_BARRIERATTR_T: usize = 4; pub const __PTHREAD_MUTEX_HAVE_PREV: usize = 1; @@ -688,15 +700,15 @@ extern "C" { } cfg_if! { - if #[cfg(target_arch = "arm")] { + if #[cfg(target_os = "espidf")] { + mod espidf; + pub use self::espidf::*; + } else if #[cfg(target_arch = "arm")] { mod arm; pub use self::arm::*; } else if #[cfg(target_arch = "aarch64")] { mod aarch64; pub use self::aarch64::*; - } else if #[cfg(target_arch = "xtensa")] { - mod xtensa; - pub use self::xtensa::*; } else if #[cfg(target_arch = "powerpc")] { mod powerpc; pub use self::powerpc::*;