Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xous: misc fixes + add network support #119408

Merged
merged 14 commits into from
Jan 22, 2024
Merged
22 changes: 12 additions & 10 deletions library/std/src/os/xous/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,29 +88,31 @@ fn lend_impl(
let a3 = opcode;
let a4 = data.as_ptr() as usize;
let a5 = data.len();
let mut a6 = arg1;
let mut a7 = arg2;
let a6 = arg1;
let a7 = arg2;
let mut ret1;
let mut ret2;

unsafe {
core::arch::asm!(
"ecall",
inlateout("a0") a0,
inlateout("a1") a1 => _,
inlateout("a2") a2 => _,
inlateout("a1") a1 => ret1,
inlateout("a2") a2 => ret2,
inlateout("a3") a3 => _,
inlateout("a4") a4 => _,
inlateout("a5") a5 => _,
inlateout("a6") a6,
inlateout("a7") a7,
inlateout("a6") a6 => _,
inlateout("a7") a7 => _,
)
};

let result = a0;

if result == SyscallResult::MemoryReturned as usize {
Ok((a6, a7))
Ok((ret1, ret2))
} else if result == SyscallResult::Error as usize {
Err(a1.into())
Err(ret1.into())
} else {
Err(Error::InternalError)
}
Expand Down Expand Up @@ -405,7 +407,7 @@ pub(crate) unsafe fn map_memory<T>(
pub(crate) unsafe fn unmap_memory<T>(range: *mut [T]) -> Result<(), Error> {
let mut a0 = Syscall::UnmapMemory as usize;
let mut a1 = range.as_mut_ptr() as usize;
let a2 = range.len();
let a2 = range.len() * core::mem::size_of::<T>();
let a3 = 0;
let a4 = 0;
let a5 = 0;
Expand Down Expand Up @@ -450,7 +452,7 @@ pub(crate) unsafe fn update_memory_flags<T>(
) -> Result<(), Error> {
let mut a0 = Syscall::UpdateMemoryFlags as usize;
let mut a1 = range.as_mut_ptr() as usize;
let a2 = range.len();
let a2 = range.len() * core::mem::size_of::<T>();
let a3 = new_flags.bits();
let a4 = 0; // Process ID is currently None
let a5 = 0;
Expand Down
6 changes: 6 additions & 0 deletions library/std/src/os/xous/services.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
use crate::os::xous::ffi::Connection;
use core::sync::atomic::{AtomicU32, Ordering};

mod dns;
pub(crate) use dns::*;

mod log;
pub(crate) use log::*;

mod net;
pub(crate) use net::*;

mod systime;
pub(crate) use systime::*;

Expand Down
28 changes: 28 additions & 0 deletions library/std/src/os/xous/services/dns.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use crate::os::xous::ffi::Connection;
use crate::os::xous::services::connect;
use core::sync::atomic::{AtomicU32, Ordering};

#[repr(usize)]
pub(crate) enum DnsLendMut {
RawLookup = 6,
}

impl Into<usize> for DnsLendMut {
fn into(self) -> usize {
self as usize
}
}

/// Return a `Connection` to the DNS lookup server. This server is used for
/// querying domain name values.
pub(crate) fn dns_server() -> Connection {
static DNS_CONNECTION: AtomicU32 = AtomicU32::new(0);
let cid = DNS_CONNECTION.load(Ordering::Relaxed);
if cid != 0 {
return cid.into();
}

let cid = connect("_DNS Resolver Middleware_").unwrap();
DNS_CONNECTION.store(cid.into(), Ordering::Relaxed);
cid
}
11 changes: 11 additions & 0 deletions library/std/src/os/xous/services/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ impl<'a> Into<[usize; 5]> for LogScalar<'a> {
}
}

pub(crate) enum LogLend {
StandardOutput = 1,
StandardError = 2,
}

impl Into<usize> for LogLend {
fn into(self) -> usize {
self as usize
}
}

/// Return a `Connection` to the log server, which is used for printing messages to
/// the console and reporting panics. If the log server has not yet started, this
/// will block until the server is running. It is safe to call this multiple times,
Expand Down
95 changes: 95 additions & 0 deletions library/std/src/os/xous/services/net.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
use crate::os::xous::ffi::Connection;
use crate::os::xous::services::connect;
use core::sync::atomic::{AtomicU32, Ordering};

pub(crate) enum NetBlockingScalar {
StdGetTtlUdp(u16 /* fd */), /* 36 */
StdSetTtlUdp(u16 /* fd */, u32 /* ttl */), /* 37 */
StdGetTtlTcp(u16 /* fd */), /* 36 */
StdSetTtlTcp(u16 /* fd */, u32 /* ttl */), /* 37 */
StdGetNodelay(u16 /* fd */), /* 38 */
StdSetNodelay(u16 /* fd */, bool), /* 39 */
StdTcpClose(u16 /* fd */), /* 34 */
StdUdpClose(u16 /* fd */), /* 41 */
StdTcpStreamShutdown(u16 /* fd */, crate::net::Shutdown /* how */), /* 46 */
}

pub(crate) enum NetLendMut {
StdTcpConnect, /* 30 */
StdTcpTx(u16 /* fd */), /* 31 */
StdTcpPeek(u16 /* fd */, bool /* nonblocking */), /* 32 */
StdTcpRx(u16 /* fd */, bool /* nonblocking */), /* 33 */
StdGetAddress(u16 /* fd */), /* 35 */
StdUdpBind, /* 40 */
StdUdpRx(u16 /* fd */), /* 42 */
StdUdpTx(u16 /* fd */), /* 43 */
StdTcpListen, /* 44 */
StdTcpAccept(u16 /* fd */), /* 45 */
}

impl Into<usize> for NetLendMut {
fn into(self) -> usize {
match self {
NetLendMut::StdTcpConnect => 30,
NetLendMut::StdTcpTx(fd) => 31 | ((fd as usize) << 16),
NetLendMut::StdTcpPeek(fd, blocking) => {
32 | ((fd as usize) << 16) | if blocking { 0x8000 } else { 0 }
}
NetLendMut::StdTcpRx(fd, blocking) => {
33 | ((fd as usize) << 16) | if blocking { 0x8000 } else { 0 }
}
NetLendMut::StdGetAddress(fd) => 35 | ((fd as usize) << 16),
NetLendMut::StdUdpBind => 40,
NetLendMut::StdUdpRx(fd) => 42 | ((fd as usize) << 16),
NetLendMut::StdUdpTx(fd) => 43 | ((fd as usize) << 16),
NetLendMut::StdTcpListen => 44,
NetLendMut::StdTcpAccept(fd) => 45 | ((fd as usize) << 16),
}
}
}

impl<'a> Into<[usize; 5]> for NetBlockingScalar {
fn into(self) -> [usize; 5] {
match self {
NetBlockingScalar::StdGetTtlTcp(fd) => [36 | ((fd as usize) << 16), 0, 0, 0, 0],
NetBlockingScalar::StdGetTtlUdp(fd) => [36 | ((fd as usize) << 16), 0, 0, 0, 1],
NetBlockingScalar::StdSetTtlTcp(fd, ttl) => {
[37 | ((fd as usize) << 16), ttl as _, 0, 0, 0]
}
NetBlockingScalar::StdSetTtlUdp(fd, ttl) => {
[37 | ((fd as usize) << 16), ttl as _, 0, 0, 1]
}
NetBlockingScalar::StdGetNodelay(fd) => [38 | ((fd as usize) << 16), 0, 0, 0, 0],
NetBlockingScalar::StdSetNodelay(fd, enabled) => {
[39 | ((fd as usize) << 16), if enabled { 1 } else { 0 }, 0, 0, 1]
}
NetBlockingScalar::StdTcpClose(fd) => [34 | ((fd as usize) << 16), 0, 0, 0, 0],
NetBlockingScalar::StdUdpClose(fd) => [41 | ((fd as usize) << 16), 0, 0, 0, 0],
NetBlockingScalar::StdTcpStreamShutdown(fd, how) => [
46 | ((fd as usize) << 16),
match how {
crate::net::Shutdown::Read => 1,
crate::net::Shutdown::Write => 2,
crate::net::Shutdown::Both => 3,
},
0,
0,
0,
],
}
}
}

/// Return a `Connection` to the Network server. This server provides all
/// OS-level networking functions.
pub(crate) fn net_server() -> Connection {
static NET_CONNECTION: AtomicU32 = AtomicU32::new(0);
let cid = NET_CONNECTION.load(Ordering::Relaxed);
if cid != 0 {
return cid.into();
}

let cid = connect("_Middleware Network Server_").unwrap();
NET_CONNECTION.store(cid.into(), Ordering::Relaxed);
cid
}
8 changes: 8 additions & 0 deletions library/std/src/sys/pal/xous/alloc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
use crate::alloc::{GlobalAlloc, Layout, System};

#[cfg(not(test))]
#[export_name = "_ZN16__rust_internals3std3sys4xous5alloc8DLMALLOCE"]
static mut DLMALLOC: dlmalloc::Dlmalloc = dlmalloc::Dlmalloc::new();

#[cfg(test)]
extern "Rust" {
#[link_name = "_ZN16__rust_internals3std3sys4xous5alloc8DLMALLOCE"]
static mut DLMALLOC: dlmalloc::Dlmalloc;
}

#[stable(feature = "alloc_system_type", since = "1.28.0")]
unsafe impl GlobalAlloc for System {
#[inline]
Expand Down
Loading
Loading