From a0c82ee25c370d9f9cebe5a671d0504133436d08 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Thu, 11 Mar 2021 12:27:55 +0800 Subject: [PATCH] xous: initial draft of `xous` target support This is an initial cut of the library as it will be included in the libstd. Signed-off-by: Sean Cross --- xous-rs/Cargo.toml | 18 +++++++++++++--- xous-rs/src/arch/mod.rs | 8 ++++---- xous-rs/src/arch/native.rs | 12 +++++------ xous-rs/src/buffer.rs | 42 +++++++++++++++++++------------------- xous-rs/src/carton.rs | 13 ++++++------ xous-rs/src/definitions.rs | 20 ++++++++++++++++++ xous-rs/src/lib.rs | 19 ++++++++++------- xous-rs/src/process.rs | 20 ++++++------------ xous-rs/src/syscall.rs | 18 ++++++++-------- 9 files changed, 99 insertions(+), 71 deletions(-) diff --git a/xous-rs/Cargo.toml b/xous-rs/Cargo.toml index e92c63b4b..6aab704d7 100644 --- a/xous-rs/Cargo.toml +++ b/xous-rs/Cargo.toml @@ -7,21 +7,33 @@ license = "MIT OR Apache-2.0" description = "System call interface for Xous" [dependencies] -bitflags = "1" +bitflags = {version = "1", optional = true } xous-macros = { path = "../macros", version = "0.1.0" } log = { version = "0.4", optional = true } -rkyv = { version = "0.3.1", default_features = false } +rkyv = { version = "0.3.1", default_features = false, optional = true } +core = { version = '1.0.0', optional = true, package = 'rustc-std-workspace-core' } +compiler_builtins = { version = '0.1.0', optional = true } [features] + # If this is set, then the "Drop" feature of MemoryMessage structs # will not be implemented. This should only be set by the kernel. forget-memory-messages = [] +# Enable using `bitflags` +bit-flags = ["bitflags"] + +# Enable sending commands using `rkyv` +archive = ["rkyv"] + # `logging` causes this program to connect to a log server on startup # so you can run log commands such as `info!()`. logging = ["log"] -default = [] +# This is being built as part of libstd +rustc-dep-of-std = ['core', 'compiler_builtins/rustc-dep-of-std'] + +default = ["bit-flags", "archive"] [target.'cfg(any(windows,unix))'.dependencies] lazy_static = "1.4" diff --git a/xous-rs/src/arch/mod.rs b/xous-rs/src/arch/mod.rs index 481135b2e..e25ed479f 100644 --- a/xous-rs/src/arch/mod.rs +++ b/xous-rs/src/arch/mod.rs @@ -1,9 +1,9 @@ -#[cfg(target_os = "none")] +#[cfg(any(target_os = "none", target_os = "xous"))] pub mod native; -#[cfg(target_os = "none")] +#[cfg(any(target_os = "none", target_os = "xous"))] pub use native::*; -#[cfg(not(target_os = "none"))] +#[cfg(not(any(target_os = "none", target_os = "xous")))] pub mod hosted; -#[cfg(not(target_os = "none"))] +#[cfg(not(any(target_os = "none", target_os = "xous")))] pub use hosted::*; diff --git a/xous-rs/src/arch/native.rs b/xous-rs/src/arch/native.rs index b9cebc292..7f0411329 100644 --- a/xous-rs/src/arch/native.rs +++ b/xous-rs/src/arch/native.rs @@ -174,12 +174,12 @@ where T: Send + 'static, U: Send + 'static, { - let stack = crate::map_memory( - None, - None, - 131_072, - crate::MemoryFlags::R | crate::MemoryFlags::W | crate::MemoryFlags::RESERVE, - )?; + #[cfg(feature = "bit-flags")] + let flags = crate::MemoryFlags::R | crate::MemoryFlags::W | crate::MemoryFlags::RESERVE; + #[cfg(not(feature = "bit-flags"))] + let flags = 0b0000_0010 | 0b0000_0100 | 0b0000_0001; + + let stack = crate::map_memory(None, None, 131_072, flags)?; let start = unsafe { core::mem::transmute(*f) }; let arg = unsafe { core::mem::transmute(arg) }; Ok(ThreadInit::new(start, stack, Some(arg), [0; 12])) diff --git a/xous-rs/src/buffer.rs b/xous-rs/src/buffer.rs index 6b9a440cd..47926a150 100644 --- a/xous-rs/src/buffer.rs +++ b/xous-rs/src/buffer.rs @@ -1,4 +1,7 @@ -use crate::{Result, Error, MemoryMessage, MemoryRange, MemoryFlags, MemorySize, Message, CID, map_memory, send_message}; +use crate::{ + map_memory, send_message, Error, MemoryMessage, MemoryRange, MemorySize, Message, + Result, CID, +}; #[derive(Debug)] pub struct XousBuffer<'a> { @@ -11,16 +14,23 @@ pub struct XousBuffer<'a> { impl<'a> XousBuffer<'a> { #[allow(dead_code)] pub fn new(len: usize) -> Self { - let remainder = - if ((len & 0xFFF) == 0) && (len > 0) { 0 } - else { 0x1000 - (len & 0xFFF) }; + let remainder = if ((len & 0xFFF) == 0) && (len > 0) { + 0 + } else { + 0x1000 - (len & 0xFFF) + }; + + #[cfg(feature = "bit-flags")] + let flags = crate::MemoryFlags::R | crate::MemoryFlags::W; + #[cfg(not(feature = "bit-flags"))] + let flags = 0b0000_0010 | 0b0000_0100; let new_mem = map_memory( None, None, // Ensure our byte size is a multiple of 4096 len + remainder, - MemoryFlags::R | MemoryFlags::W, + flags, ) .expect("XousBuffer: error in new()/map_memory"); @@ -28,7 +38,9 @@ impl<'a> XousBuffer<'a> { valid.size = MemorySize::new(len + remainder).unwrap(); XousBuffer { range: new_mem, - slice: unsafe { core::slice::from_raw_parts_mut(new_mem.as_mut_ptr(), len + remainder) }, + slice: unsafe { + core::slice::from_raw_parts_mut(new_mem.as_mut_ptr(), len + remainder) + }, valid, should_drop: true, } @@ -46,11 +58,7 @@ impl<'a> XousBuffer<'a> { /// Perform a mutable lend of this Carton to the server. #[allow(dead_code)] - pub fn lend_mut( - &mut self, - connection: CID, - id: u32, - ) -> core::result::Result { + pub fn lend_mut(&mut self, connection: CID, id: u32) -> core::result::Result { let msg = MemoryMessage { id: id as usize, buf: self.valid, @@ -61,11 +69,7 @@ impl<'a> XousBuffer<'a> { } #[allow(dead_code)] - pub fn lend( - &self, - connection: CID, - id: u32 - ) -> core::result::Result { + pub fn lend(&self, connection: CID, id: u32) -> core::result::Result { let msg = MemoryMessage { id: id as usize, buf: self.valid, @@ -76,11 +80,7 @@ impl<'a> XousBuffer<'a> { } #[allow(dead_code)] - pub fn send( - mut self, - connection: CID, - id: u32, - ) -> core::result::Result { + pub fn send(mut self, connection: CID, id: u32) -> core::result::Result { let msg = crate::MemoryMessage { id: id as usize, buf: self.valid, diff --git a/xous-rs/src/carton.rs b/xous-rs/src/carton.rs index c81a0d805..b087b5698 100644 --- a/xous-rs/src/carton.rs +++ b/xous-rs/src/carton.rs @@ -19,13 +19,12 @@ impl<'a> Carton<'a> { let remainder = bytes.len() & 4095; let size = bytes.len() + (4096 - remainder); - let new_mem = crate::map_memory( - None, - None, - size, - crate::MemoryFlags::R | crate::MemoryFlags::W, - ) - .unwrap(); + #[cfg(feature = "bit-flags")] + let flags = crate::MemoryFlags::R | crate::MemoryFlags::W; + #[cfg(not(feature = "bit-flags"))] + let flags = 0b0000_0010 | 0b0000_0100; + + let new_mem = crate::map_memory(None, None, size, flags).unwrap(); // NOTE: Remaining bytes are not zeroed. We assume the kernel has done this for us. unsafe { diff --git a/xous-rs/src/definitions.rs b/xous-rs/src/definitions.rs index b2b7b9b42..1c14e45dc 100644 --- a/xous-rs/src/definitions.rs +++ b/xous-rs/src/definitions.rs @@ -115,6 +115,7 @@ pub struct MemoryRange { pub size: MemorySize, } +#[cfg(feature = "bitflags")] bitflags! { /// Flags to be passed to the MapMemory struct. /// Note that it is an error to have memory be @@ -138,6 +139,25 @@ bitflags! { const X = 0b0000_1000; } } +#[cfg(feature = "bitflags")] +pub(crate) fn get_bits(bf: &MemoryFlags) -> usize { + bf.bits() +} +#[cfg(feature = "bitflags")] +pub(crate) fn from_bits(raw: usize) -> Option { + MemoryFlags::from_bits(raw) +} + +#[cfg(not(feature = "bitflags"))] +pub type MemoryFlags = usize; +#[cfg(not(feature = "bitflags"))] +pub(crate) fn get_bits(bf: &MemoryFlags) -> usize { + *bf +} +#[cfg(not(feature = "bitflags"))] +pub(crate) fn from_bits(raw: usize) -> Option { + Some(raw) +} pub fn pid_from_usize(src: usize) -> core::result::Result { if src > u8::MAX as _ { diff --git a/xous-rs/src/lib.rs b/xous-rs/src/lib.rs index 1229a7c14..805591e22 100644 --- a/xous-rs/src/lib.rs +++ b/xous-rs/src/lib.rs @@ -1,5 +1,6 @@ -#![cfg_attr(target_os = "none", no_std)] +#![cfg_attr(any(target_os = "none", target_os = "xous"), no_std)] +#[cfg(feature = "bitflags")] #[macro_use] extern crate bitflags; @@ -12,27 +13,30 @@ pub mod arch; pub mod carton; pub mod definitions; mod messages; -pub mod string; + pub mod buffer; -pub mod syscall; pub mod process; +#[cfg(feature = "archive")] +pub mod string; +pub mod syscall; pub mod names; pub use arch::{ProcessArgs, ProcessInit, ProcessKey, ThreadInit}; +pub use buffer::XousBuffer; pub use definitions::*; pub use messages::*; +#[cfg(feature = "archive")] pub use string::*; pub use syscall::*; -pub use buffer::XousBuffer; -#[cfg(not(target_os = "none"))] +#[cfg(not(any(target_os = "none", target_os = "xous")))] pub use arch::ProcessArgsAsThread; -#[cfg(target_os = "none")] +#[cfg(any(target_os = "none", target_os = "xous"))] pub fn init() {} -#[cfg(not(target_os = "none"))] +#[cfg(not(any(target_os = "none", target_os = "xous")))] pub fn init() { use std::panic; panic::set_hook(Box::new(|arg| { @@ -61,6 +65,7 @@ macro_rules! maybe_main { } fn main() { + #[cfg(not(target_os = "xous"))] xous::arch::ensure_connection().unwrap(); unsafe { xous_entry() }; } diff --git a/xous-rs/src/process.rs b/xous-rs/src/process.rs index 171a78f47..c0357d85f 100644 --- a/xous-rs/src/process.rs +++ b/xous-rs/src/process.rs @@ -1,17 +1,9 @@ -#[cfg(not(target_os = "none"))] pub fn id() -> u32 { - std::process::id() + if let Ok(crate::Result::ProcessID(pid)) = crate::syscall::current_pid() { + pid.get() as u32 + } else { + 0 + } } -#[cfg(target_os = "none")] -static PID_KEEPER: core::sync::atomic::AtomicU32 = core::sync::atomic::AtomicU32::new(0); - -#[cfg(target_os = "none")] -pub fn id() -> u32 { - PID_KEEPER.load(core::sync::atomic::Ordering::Relaxed) -} - -#[cfg(target_os = "none")] -pub fn set_id(id: u32) { - PID_KEEPER.store(id, core::sync::atomic::Ordering::Relaxed) -} +pub fn set_id(_id: u32) {} diff --git a/xous-rs/src/syscall.rs b/xous-rs/src/syscall.rs index c663b24d1..1c242b5b8 100644 --- a/xous-rs/src/syscall.rs +++ b/xous-rs/src/syscall.rs @@ -8,7 +8,7 @@ use core::convert::TryInto; // use num_derive::FromPrimitive; // use num_traits::FromPrimitive; -#[cfg(not(target_os = "none"))] +#[cfg(not(any(target_os = "none", target_os = "xous")))] use crate::ProcessArgsAsThread; #[derive(Debug, PartialEq)] @@ -405,7 +405,7 @@ impl SysCall { a1.map(|x| x.get()).unwrap_or_default(), a2.map(|x| x.get()).unwrap_or_default(), a3.get(), - a4.bits(), + crate::get_bits(a4), 0, 0, 0, @@ -509,7 +509,7 @@ impl SysCall { SysCall::IncreaseHeap(a1, a2) => [ SysCallNumber::IncreaseHeap as usize, *a1 as usize, - a2.bits(), + crate::get_bits(a2), 0, 0, 0, @@ -530,7 +530,7 @@ impl SysCall { SysCallNumber::UpdateMemoryFlags as usize, a1.get(), *a2 as usize, - a3.bits(), + crate::get_bits(a3), 0, 0, 0, @@ -725,7 +725,7 @@ impl SysCall { MemoryAddress::new(a1), MemoryAddress::new(a2), MemoryAddress::new(a3).ok_or(Error::InvalidSyscall)?, - MemoryFlags::from_bits(a4).ok_or(Error::InvalidSyscall)?, + crate::from_bits(a4).ok_or(Error::InvalidSyscall)?, ), SysCallNumber::UnmapMemory => { SysCall::UnmapMemory(MemoryRange::new(a1, a2).or(Err(Error::InvalidSyscall))?) @@ -749,13 +749,13 @@ impl SysCall { SysCallNumber::ReadyThreads => SysCall::ReadyThreads(pid_from_usize(a1)?), SysCallNumber::IncreaseHeap => SysCall::IncreaseHeap( a1 as usize, - MemoryFlags::from_bits(a2).ok_or(Error::InvalidSyscall)?, + crate::from_bits(a2).ok_or(Error::InvalidSyscall)?, ), SysCallNumber::DecreaseHeap => SysCall::DecreaseHeap(a1 as usize), SysCallNumber::UpdateMemoryFlags => SysCall::UpdateMemoryFlags( MemoryAddress::new(a1).ok_or(Error::InvalidSyscall)?, a2 as usize, - MemoryFlags::from_bits(a3).ok_or(Error::InvalidSyscall)?, + crate::from_bits(a3).ok_or(Error::InvalidSyscall)?, ), SysCallNumber::SetMemRegion => SysCall::SetMemRegion( pid_from_usize(a1)?, @@ -1364,7 +1364,7 @@ pub fn wait_thread(joiner: crate::arch::WaitHandle) -> SysCallResult { } /// Create a new process by running it in its own thread -#[cfg(not(target_os = "none"))] +#[cfg(not(any(target_os = "none", target_os = "xous")))] pub fn create_process_as_thread( args: ProcessArgsAsThread, ) -> core::result::Result @@ -1382,7 +1382,7 @@ where } /// Wait for a thread to finish -#[cfg(not(target_os = "none"))] +#[cfg(not(any(target_os = "none", target_os = "xous")))] pub fn wait_process_as_thread(joiner: crate::arch::ProcessHandleAsThread) -> SysCallResult { crate::arch::wait_process_as_thread(joiner) }