xous: remove nearly all dependencies

This removes nearly all of the dependencies from `xous-rs`, making it
mostly self-contained.

There is one remaining dependency tree which is `xous-macros`. This is a
legacy feature from before a time we had main(). Attempts will be made
to remove it as well.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2022-05-31 12:29:23 +08:00
parent 58dd02c282
commit dd02fce340
10 changed files with 319 additions and 75 deletions

View file

@ -22,7 +22,6 @@ compiler_builtins = { version = '0.1.0', optional = true }
forget-memory-messages = []
# Enable using `bitflags`
bit-flags = ["bitflags"]
# This is being built as part of libstd
rustc-dep-of-std = ['core', 'compiler_builtins/rustc-dep-of-std']
@ -31,7 +30,7 @@ rustc-dep-of-std = ['core', 'compiler_builtins/rustc-dep-of-std']
# testing.
processes-as-threads = []
default = ["bit-flags", "xous-macros"]
default = ["xous-macros"]
[target.'cfg(any(windows,unix))'.dependencies]
lazy_static = "1.4.0"

View file

@ -293,10 +293,7 @@ pub fn create_thread_n_pre(
arg3: &usize,
arg4: &usize,
) -> core::result::Result<ThreadInit, crate::Error> {
#[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)?;
Ok(ThreadInit::new(start, stack, *arg1, *arg2, *arg3, *arg4))

View file

@ -19,10 +19,7 @@ impl<'a> Carton<'a> {
let remainder = bytes.len() & 4095;
let size = bytes.len() + (4096 - remainder);
#[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();

View file

@ -69,6 +69,9 @@ pub static TESTING_RNG_SEED: AtomicU64 = AtomicU64::new(0);
pub mod exceptions;
pub use exceptions::*;
pub mod memoryflags;
pub use memoryflags::*;
pub mod messages;
pub use messages::*;
@ -154,50 +157,6 @@ pub struct MemoryRange {
pub(crate) size: MemorySize,
}
#[cfg(feature = "bitflags")]
bitflags! {
/// Flags to be passed to the MapMemory struct.
/// Note that it is an error to have memory be
/// writable and not readable.
pub struct MemoryFlags: usize {
/// Free this memory
const FREE = 0b0000_0000;
/// Immediately allocate this memory. Otherwise it will
/// be demand-paged. This is implicitly set when `phys`
/// is not 0.
const RESERVE = 0b0000_0001;
/// Allow the CPU to read from this page.
const R = 0b0000_0010;
/// Allow the CPU to write to this page.
const W = 0b0000_0100;
/// Allow the CPU to execute from this page.
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> {
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<MemoryFlags> {
Some(raw)
}
pub fn pid_from_usize(src: usize) -> core::result::Result<PID, Error> {
if src > u8::MAX as _ {
return Err(Error::InvalidPID);
@ -483,14 +442,7 @@ pub enum Result {
impl Result {
fn add_opcode(opcode: usize, args: [usize; 7]) -> [usize; 8] {
[
opcode,
args[0],
args[1],
args[2],
args[3],
args[4],
args[5],
args[6],
opcode, args[0], args[1], args[2], args[3], args[4], args[5], args[6],
]
}

View file

@ -0,0 +1,196 @@
/// Flags to be passed to the MapMemory struct.
/// Note that it is an error to have memory be
/// writable and not readable.
#[derive(Copy, PartialEq, Eq, Clone, PartialOrd, Ord, Hash, Debug)]
pub struct MemoryFlags {
bits: usize,
}
impl MemoryFlags {
/// Free this memory
pub const FREE: Self = Self { bits: 0b0000_0000 };
/// Immediately allocate this memory. Otherwise it will
/// be demand-paged. This is implicitly set when `phys`
/// is not 0.
pub const RESERVE: Self = Self { bits: 0b0000_0001 };
/// Allow the CPU to read from this page.
pub const R: Self = Self { bits: 0b0000_0010 };
/// Allow the CPU to write to this page.
pub const W: Self = Self { bits: 0b0000_0100 };
/// Allow the CPU to execute from this page.
pub const X: Self = Self { bits: 0b0000_1000 };
pub fn bits(&self) -> usize {
self.bits
}
pub fn from_bits(raw: usize) -> Option<MemoryFlags> {
if raw > 16 {
None
} else {
Some(MemoryFlags { bits: raw })
}
}
pub fn is_empty(&self) -> bool {
self.bits == 0
}
pub fn empty() -> MemoryFlags {
MemoryFlags { bits: 0 }
}
pub fn all() -> MemoryFlags {
MemoryFlags { bits: 15 }
}
}
// impl core::fmt::Debug for MemoryFlags {
// fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
// // Iterate over the valid flags
// let mut first = true;
// for (name, _) in self.iter() {
// if !first {
// f.write_str(" | ")?;
// }
// first = false;
// f.write_str(name)?;
// }
// // Append any extra bits that correspond to flags to the end of the format
// let extra_bits = self.bits & !Self::all().bits();
// // if extra_bits != <$T as Bits>::EMPTY {
// // if !first {
// // f.write_str(" | ")?;
// // }
// // first = false;
// // core::write!(f, "{:#x}", extra_bits)?;
// // }
// if first {
// f.write_str("(empty)")?;
// }
// core::fmt::Result::Ok(())
// }
// }
impl core::fmt::Binary for MemoryFlags {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
core::fmt::Binary::fmt(&self.bits, f)
}
}
impl core::fmt::Octal for MemoryFlags {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
core::fmt::Octal::fmt(&self.bits, f)
}
}
impl core::fmt::LowerHex for MemoryFlags {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
core::fmt::LowerHex::fmt(&self.bits, f)
}
}
impl core::fmt::UpperHex for MemoryFlags {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
core::fmt::UpperHex::fmt(&self.bits, f)
}
}
impl core::ops::BitOr for MemoryFlags {
type Output = Self;
/// Returns the union of the two sets of flags.
#[inline]
fn bitor(self, other: MemoryFlags) -> Self {
Self {
bits: self.bits | other.bits,
}
}
}
impl core::ops::BitOrAssign for MemoryFlags {
/// Adds the set of flags.
#[inline]
fn bitor_assign(&mut self, other: Self) {
self.bits |= other.bits;
}
}
impl core::ops::BitXor for MemoryFlags {
type Output = Self;
/// Returns the left flags, but with all the right flags toggled.
#[inline]
fn bitxor(self, other: Self) -> Self {
Self {
bits: self.bits ^ other.bits,
}
}
}
impl core::ops::BitXorAssign for MemoryFlags {
/// Toggles the set of flags.
#[inline]
fn bitxor_assign(&mut self, other: Self) {
self.bits ^= other.bits;
}
}
impl core::ops::BitAnd for MemoryFlags {
type Output = Self;
/// Returns the intersection between the two sets of flags.
#[inline]
fn bitand(self, other: Self) -> Self {
Self {
bits: self.bits & other.bits,
}
}
}
impl core::ops::BitAndAssign for MemoryFlags {
/// Disables all flags disabled in the set.
#[inline]
fn bitand_assign(&mut self, other: Self) {
self.bits &= other.bits;
}
}
impl core::ops::Sub for MemoryFlags {
type Output = Self;
/// Returns the set difference of the two sets of flags.
#[inline]
fn sub(self, other: Self) -> Self {
Self {
bits: self.bits & !other.bits,
}
}
}
impl core::ops::SubAssign for MemoryFlags {
/// Disables all flags enabled in the set.
#[inline]
fn sub_assign(&mut self, other: Self) {
self.bits &= !other.bits;
}
}
impl core::ops::Not for MemoryFlags {
type Output = Self;
/// Returns the complement of this set of flags.
#[inline]
fn not(self) -> Self {
Self { bits: !self.bits } & MemoryFlags { bits: 15 }
}
}

View file

@ -15,6 +15,7 @@ pub mod carton;
pub mod definitions;
pub mod process;
pub mod services;
pub mod string;
pub mod stringbuffer;
pub mod syscall;

1
xous-rs/src/services.rs Normal file
View file

@ -0,0 +1 @@
pub mod nameserver;

View file

@ -0,0 +1,110 @@
use core::sync::atomic::{AtomicU32, Ordering};
// By making this repr(C), the layout of this struct becomes well-defined
// and no longer shifts around.
// By marking it as `align(4096) we define that it will be page-aligned,
// meaning it can be sent between processes.
#[repr(C, align(4096))]
struct ConnectRequest {
name: [u8; 64],
len: u32,
_padding: [u8; 4096 - 4 - 64],
}
impl Default for ConnectRequest {
fn default() -> Self {
ConnectRequest {
name: [0u8; 64],
len: 0,
_padding: [0u8; 4096 - 4 - 64],
}
}
}
impl ConnectRequest {
pub fn new(name: &str) -> Option<Self> {
let mut cr: ConnectRequest = Default::default();
let name_bytes = name.as_bytes();
// Ensure the bytes won't blow out the buffer
if name_bytes.len() > 64 {
return None;
}
// Copy the string into our backing store.
for (&src_byte, dest_byte) in name_bytes.iter().zip(&mut cr.name) {
*dest_byte = src_byte;
}
// Set the string length to the length of the passed-in String,
// or the maximum possible length. Which ever is smaller.
cr.len = 64usize.min(name.as_bytes().len()) as u32;
// If the string is not valid, set its length to 0.
if core::str::from_utf8(&cr.name[0..cr.len as usize]).is_err() {
return None;
}
if cr.len == 0 {
None
} else {
Some(cr)
}
}
}
pub fn connect(name: &str) -> Option<crate::CID> {
let mut request = ConnectRequest::new(name)?;
let ns_cid = nameserver();
let memory_range = unsafe {
crate::MemoryRange::new(
&mut request as *mut ConnectRequest as usize,
core::mem::size_of::<ConnectRequest>(),
)
.unwrap()
};
let response = crate::send_message(
ns_cid,
crate::Message::new_lend_mut(
6, /* BlockingConnect */
memory_range,
None,
crate::MemoryAddress::new(request.len as usize),
),
)
.expect("unable to perform lookup");
if let crate::Result::MemoryReturned(_, _) = response {
let response_ptr = &request as *const ConnectRequest as *const u32;
let result = unsafe { response_ptr.read() };
if result == 0 {
let cid = unsafe { response_ptr.add(1).read() };
let mut token = [0u32; 4];
token[0] = unsafe { response_ptr.add(2).read() };
token[1] = unsafe { response_ptr.add(3).read() };
token[2] = unsafe { response_ptr.add(4).read() };
token[3] = unsafe { response_ptr.add(5).read() };
// println!("Successfully connected to {}. CID: {}, token: {:?}", name, cid, token);
Some(cid)
} else {
let _error = unsafe { response_ptr.add(1).read() };
// println!("Error connecting to {}. Type: {} Code: {}", name, result, _error);
None
}
} else {
None
}
}
pub(crate) fn nameserver() -> crate::CID {
static NAMESERVER_CID: AtomicU32 = AtomicU32::new(0);
let cid = NAMESERVER_CID.load(Ordering::Relaxed);
if cid != 0 {
return cid;
}
let cid = crate::connect(crate::SID::from_bytes(b"xous-name-server").unwrap()).unwrap();
NAMESERVER_CID.store(cid, Ordering::Relaxed);
cid
}

View file

@ -44,10 +44,7 @@ impl<'a> StringBuffer<'a> {
0x1000 - (capacity & 0xFFF)
};
#[cfg(feature = "bit-flags")]
let flags = crate::MemoryFlags::R | crate::MemoryFlags::W;
#[cfg(not(feature = "bit-flags"))]
let flags = 0b0000_0010 | 0b0000_0100;
// Allocate enough memory to hold the requested data
let new_mem = map_memory(
@ -87,10 +84,7 @@ impl<'a> StringBuffer<'a> {
return;
}
#[cfg(feature = "bit-flags")]
let flags = crate::MemoryFlags::R | crate::MemoryFlags::W;
#[cfg(not(feature = "bit-flags"))]
let flags = 0b0000_0010 | 0b0000_0100;
// Allocate enough memory to hold the new requested data
let new_slice = if rounded_new_capacity > 0 {

View file

@ -546,7 +546,7 @@ impl SysCall {
a1.map(|x| x.get()).unwrap_or_default(),
a2.map(|x| x.get()).unwrap_or_default(),
a3.get(),
crate::get_bits(a4),
a4.bits(),
0,
0,
0,
@ -651,7 +651,7 @@ impl SysCall {
SysCall::IncreaseHeap(a1, a2) => [
SysCallNumber::IncreaseHeap as usize,
*a1 as usize,
crate::get_bits(a2),
a2.bits(),
0,
0,
0,
@ -672,7 +672,7 @@ impl SysCall {
SysCallNumber::UpdateMemoryFlags as usize,
a1.as_mut_ptr() as usize,
a1.len(),
crate::get_bits(a2),
a2.bits(),
a3.map(|m| m.get() as usize).unwrap_or(0),
0,
0,
@ -894,7 +894,7 @@ impl SysCall {
MemoryAddress::new(a1),
MemoryAddress::new(a2),
MemoryAddress::new(a3).ok_or(Error::InvalidSyscall)?,
crate::from_bits(a4).ok_or(Error::InvalidSyscall)?,
crate::MemoryFlags::from_bits(a4).ok_or(Error::InvalidSyscall)?,
),
SysCallNumber::UnmapMemory => SysCall::UnmapMemory(unsafe {
MemoryRange::new(a1, a2).or(Err(Error::InvalidSyscall))
@ -918,12 +918,12 @@ impl SysCall {
SysCallNumber::ReadyThreads => SysCall::ReadyThreads(pid_from_usize(a1)?),
SysCallNumber::IncreaseHeap => SysCall::IncreaseHeap(
a1 as usize,
crate::from_bits(a2).ok_or(Error::InvalidSyscall)?,
crate::MemoryFlags::from_bits(a2).ok_or(Error::InvalidSyscall)?,
),
SysCallNumber::DecreaseHeap => SysCall::DecreaseHeap(a1 as usize),
SysCallNumber::UpdateMemoryFlags => SysCall::UpdateMemoryFlags(
unsafe { MemoryRange::new(a1, a2) }?,
crate::from_bits(a3).ok_or(Error::InvalidSyscall)?,
crate::MemoryFlags::from_bits(a3).ok_or(Error::InvalidSyscall)?,
PID::new(a4 as _),
),
SysCallNumber::SetMemRegion => SysCall::SetMemRegion(
@ -1774,10 +1774,7 @@ fn handle_exception(exception_type: usize, arg1: usize, arg2: usize) -> isize {
pub fn set_exception_handler(
handler: fn(crate::Exception) -> isize,
) -> core::result::Result<(), Error> {
#[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)?;
EXCEPTION_HANDLER.store(handler as usize, core::sync::atomic::Ordering::SeqCst);