about summary refs log tree commit diff
path: root/library/std/src/os
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-01-22 16:13:26 +0100
committerGitHub <noreply@github.com>2024-01-22 16:13:26 +0100
commite9c2e1bfbed1b7c1f48bcbd545620a54af2c40cf (patch)
tree63bf2a44e3432a3bd9b00751fa50a1def50dfebb /library/std/src/os
parentc5984caa443cbffdfe9223df75f6e9ab88409f1f (diff)
parent50e4fede2427f54cbdb9e4d556aa9ef4c68332fb (diff)
downloadrust-e9c2e1bfbed1b7c1f48bcbd545620a54af2c40cf.tar.gz
rust-e9c2e1bfbed1b7c1f48bcbd545620a54af2c40cf.zip
Rollup merge of #119408 - betrusted-io:xous-fixes-add-network, r=Mark-Simulacrum
xous: misc fixes + add network support

This patchset makes several fixes to Xous support. Additionally, this patch adds networking support.

Many of these fixes are the result of the recent patch to get `unwinding` support merged. As a result of this patch, we can now run rust tests. As a result of these tests, we now have 729 tests passing:

```
failures:
    env::tests::test
    env::tests::test_self_exe_path
    env::tests::vars_debug
    env::tests::vars_os_debug
    os::raw::tests::same
    path::tests::test_push
    path::tests::test_set_file_name
    time::tests::since_epoch
test result: FAILED. 729 passed; 8 failed; 1 ignored; 0 measured; 0 filtered out; finished in 214.54s
```

In the course of fixing several tests and getting the test sequence to reliably run, several issues were found. This patchset fixes those issues.
Diffstat (limited to 'library/std/src/os')
-rw-r--r--library/std/src/os/xous/ffi.rs22
-rw-r--r--library/std/src/os/xous/services.rs6
-rw-r--r--library/std/src/os/xous/services/dns.rs28
-rw-r--r--library/std/src/os/xous/services/log.rs11
-rw-r--r--library/std/src/os/xous/services/net.rs95
5 files changed, 152 insertions, 10 deletions
diff --git a/library/std/src/os/xous/ffi.rs b/library/std/src/os/xous/ffi.rs
index 8be7fbb102f..7fe84db515c 100644
--- a/library/std/src/os/xous/ffi.rs
+++ b/library/std/src/os/xous/ffi.rs
@@ -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)
     }
@@ -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;
@@ -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;
diff --git a/library/std/src/os/xous/services.rs b/library/std/src/os/xous/services.rs
index 5c219f1fbb9..a75be1b8570 100644
--- a/library/std/src/os/xous/services.rs
+++ b/library/std/src/os/xous/services.rs
@@ -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::*;
 
diff --git a/library/std/src/os/xous/services/dns.rs b/library/std/src/os/xous/services/dns.rs
new file mode 100644
index 00000000000..a7d88f4892c
--- /dev/null
+++ b/library/std/src/os/xous/services/dns.rs
@@ -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
+}
diff --git a/library/std/src/os/xous/services/log.rs b/library/std/src/os/xous/services/log.rs
index e6bae929eac..55a501dc7d0 100644
--- a/library/std/src/os/xous/services/log.rs
+++ b/library/std/src/os/xous/services/log.rs
@@ -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,
diff --git a/library/std/src/os/xous/services/net.rs b/library/std/src/os/xous/services/net.rs
new file mode 100644
index 00000000000..26d337dcef1
--- /dev/null
+++ b/library/std/src/os/xous/services/net.rs
@@ -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
+}