xous-rs: begin adding support for native message passing

This will send data via a TCP connection to simulate making syscalls.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2020-06-03 12:06:46 +08:00
parent 0fae191651
commit e059494a7f
7 changed files with 250 additions and 41 deletions

View file

@ -17,3 +17,9 @@ default-features = false
# 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 = []
[target.'cfg(windows)'.dependencies]
lazy_static = "1.4"
[target.'cfg(unix)'.dependencies]
lazy_static = "1.4"

View file

@ -8,6 +8,18 @@ fn main() {
let target = env::var("TARGET").unwrap();
let name = env::var("CARGO_PKG_NAME").unwrap();
let target_os = target.split('-').nth(2).unwrap_or("none");
// If we're not running on a desktop-class operating system, emit the "baremetal"
// config setting. This will enable software to do tasks such as
// managing memory.
if target_os == "none" {
println!("Target {} is bare metal", target);
println!("cargo:rustc-cfg=baremetal");
} else {
println!("Target {} is NOT bare metal", target);
}
if target.starts_with("riscv") {
fs::copy(
format!("bin/{}.a", target),

View file

@ -74,6 +74,36 @@ pub enum Error {
UnhandledSyscall = 17,
InvalidSyscall = 18,
ShareViolation = 19,
UnknownError = 20,
}
impl Error {
pub fn from_usize(arg: usize) -> Self {
use crate::Error::*;
match arg {
0 => NoError,
1 => BadAlignment,
2 => BadAddress,
3 => OutOfMemory,
4 => MemoryInUse,
5 => InterruptNotFound,
6 => InterruptInUse,
7 => InvalidString,
8 => ServerExists,
9 => ServerNotFound,
10 => ProcessNotFound,
11 => ProcessNotChild,
12 => ProcessTerminated,
13 => Timeout,
14 => InternalError,
15 => ServerQueueFull,
16 => ContextNotAvailable,
17 => UnhandledSyscall,
18 => InvalidSyscall,
19 => ShareViolation,
_ => UnknownError,
}
}
}
#[repr(C)]
@ -95,12 +125,41 @@ pub struct MemoryMessage {
pub buf: MemoryRange,
/// The offset within the buffer where the interesting stuff starts.
pub offset: Option<MemorySize>,
pub offset: Option<MemoryAddress>,
/// How many bytes in the buffer are valid
pub valid: Option<MemorySize>,
}
impl MemoryMessage {
pub fn from_usize(
id: usize,
addr: usize,
size: usize,
offset: usize,
valid: usize,
) -> Option<MemoryMessage> {
let addr = match MemoryAddress::new(addr) {
None => return None,
Some(s) => s,
};
let size = match MemorySize::new(size) {
None => return None,
Some(s) => s,
};
let buf = MemoryRange { addr, size };
let offset = MemoryAddress::new(offset);
let valid = MemorySize::new(valid);
Some(MemoryMessage {
id,
buf,
offset,
valid,
})
}
}
#[repr(C)]
#[derive(Debug, PartialEq)]
/// A simple scalar message. This is similar to a `move` message.
@ -112,6 +171,24 @@ pub struct ScalarMessage {
pub arg4: usize,
}
impl ScalarMessage {
pub fn from_usize(
id: usize,
arg1: usize,
arg2: usize,
arg3: usize,
arg4: usize,
) -> ScalarMessage {
ScalarMessage {
id,
arg1,
arg2,
arg3,
arg4,
}
}
}
#[repr(usize)]
#[derive(Debug, PartialEq)]
pub enum Message {
@ -178,6 +255,32 @@ impl MemoryRange {
}
}
/// Which memory region the operation should affect.
#[derive(Debug, Copy, Clone)]
pub enum MemoryType {
/// The address where addresses go when no `virt` is specified.
Default = 1,
/// Addresses will begin here when `IncreaseHeap` is called.
Heap = 2,
/// When messages are passed to a process, they will go here.
Messages = 3,
/// Unlike other memory types, this defines the "end" of the region.
Stack = 4,
}
impl From<usize> for MemoryType {
fn from(arg: usize) -> Self {
match arg {
2 => MemoryType::Heap,
3 => MemoryType::Messages,
4 => MemoryType::Stack,
_ => MemoryType::Default,
}
}
}
#[repr(C)]
#[derive(Debug, PartialEq)]
@ -204,6 +307,57 @@ pub enum Result {
UnknownResult(usize, usize, usize, usize, usize, usize, usize),
}
impl Result {
pub fn from_args(src: [usize; 8]) -> Self {
match src[0] {
0 => Result::Ok,
1 => Result::Error(Error::from_usize(src[1])),
2 => Result::MemoryAddress(src[1] as *mut u8),
3 => {
let addr = match MemoryAddress::new(src[1]) {
None => return Result::Error(Error::InternalError),
Some(s) => s,
};
let size = match MemorySize::new(src[2]) {
None => return Result::Error(Error::InternalError),
Some(s) => s,
};
Result::MemoryRange(MemoryRange { addr, size })
}
4 => Result::ReadyContexts(src[1], src[2], src[3], src[4], src[5], src[6], src[7]),
5 => Result::ResumeProcess,
6 => Result::ServerID((src[1], src[2], src[3], src[4])),
7 => Result::ConnectionID(src[1] as CID),
8 => {
let sender = src[1];
let message = match src[2] {
0 => match MemoryMessage::from_usize(src[3], src[4], src[5], src[6], src[7]) {
None => return Result::Error(Error::InternalError),
Some(s) => Message::MutableBorrow(s),
},
1 => match MemoryMessage::from_usize(src[3], src[4], src[5], src[6], src[7]) {
None => return Result::Error(Error::InternalError),
Some(s) => Message::ImmutableBorrow(s),
},
2 => match MemoryMessage::from_usize(src[3], src[4], src[5], src[6], src[7]) {
None => return Result::Error(Error::InternalError),
Some(s) => Message::Move(s),
},
3 => Message::Scalar(ScalarMessage::from_usize(
src[3], src[4], src[5], src[6], src[7],
)),
_ => return Result::Error(Error::InternalError),
};
Result::Message(MessageEnvelope { sender, message })
}
9 => Result::ThreadID(src[1] as CtxID),
10 => Result::MessageResult(src[1], src[2]),
_ => Result::UnknownResult(src[1], src[2], src[3], src[4], src[5], src[6], src[7]),
}
}
}
impl From<Error> for Result {
fn from(e: Error) -> Self {
Result::Error(e)

52
xous-rs/src/hosted/mod.rs Normal file
View file

@ -0,0 +1,52 @@
use lazy_static::lazy_static;
use std::io::{Read, Write};
use std::mem::size_of;
use std::net::TcpStream;
use std::sync::Mutex;
use crate::Result;
lazy_static! {
pub static ref XOUS_SERVER_CONNECTION: Mutex<Option<TcpStream>> = Mutex::new(None);
}
#[allow(clippy::too_many_arguments)]
#[no_mangle]
pub fn _xous_syscall(
nr: usize,
a1: usize,
a2: usize,
a3: usize,
a4: usize,
a5: usize,
a6: usize,
a7: usize,
ret: &mut Result,
) {
let xous_server_connection = &mut *XOUS_SERVER_CONNECTION.lock().unwrap();
if xous_server_connection.is_none() {
println!("Opening connection to Xous server...");
let conn = TcpStream::connect("localhost:9687").unwrap();
*xous_server_connection = Some(conn);
}
println!(
"Syscall: {} {:08x} {} {} {} {} {} {}",
nr, a1, a2, a3, a4, a5, a6, a7
);
let mut xsc = xous_server_connection.as_ref().unwrap();
for word in &[nr, a1, a2, a3, a4, a5, a6, a7] {
xsc.write_all(&word.to_le_bytes()).expect("Disconnection");
}
let mut pkt = [0usize; 8];
let mut word = [0u8; size_of::<usize>()];
for pkt_word in pkt.iter_mut() {
xsc.read_exact(&mut word).expect("Disconnection");
*pkt_word = usize::from_le_bytes(word);
}
*ret = Result::from_args(pkt);
}

View file

@ -1,10 +1,20 @@
#![no_std]
#![cfg_attr(not(any(windows,unix)), no_std)]
#[macro_use]
extern crate bitflags;
extern crate num_derive;
extern crate num_traits;
#[cfg(not(any(windows,unix)))]
pub mod native;
#[cfg(not(any(windows,unix)))]
pub use native::*;
#[cfg(any(windows,unix))]
pub mod hosted;
#[cfg(any(windows,unix))]
pub use hosted::*;
pub mod definitions;
pub mod syscall;

View file

@ -0,0 +1,2 @@
// pub mod syscall;
// pub use syscall::*;

View file

@ -1,37 +1,10 @@
use crate::{
CpuID, Error, MemoryAddress, MemoryFlags, MemoryMessage, MemoryRange, MemorySize,
CpuID, Error, MemoryAddress, MemoryFlags, MemoryMessage, MemoryRange, MemorySize, MemoryType,
Message, MessageEnvelope, MessageSender, Result, ScalarMessage, SyscallResult, CID, PID, SID,
};
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
/// Which memory region the operation should affect.
#[derive(Debug, Copy, Clone)]
pub enum MemoryType {
/// The address where addresses go when no `virt` is specified.
Default = 1,
/// Addresses will begin here when `IncreaseHeap` is called.
Heap = 2,
/// When messages are passed to a process, they will go here.
Messages = 3,
/// Unlike other memory types, this defines the "end" of the region.
Stack = 4,
}
impl From<usize> for MemoryType {
fn from(arg: usize) -> Self {
match arg {
2 => MemoryType::Heap,
3 => MemoryType::Messages,
4 => MemoryType::Stack,
_ => MemoryType::Default,
}
}
}
#[derive(Debug)]
pub enum SysCall {
/// Allocates pages of memory, equal to a total of `size` bytes. A physical
@ -603,17 +576,17 @@ impl SysCall {
}
extern "Rust" {
fn _xous_syscall_rust(
nr: usize,
a1: usize,
a2: usize,
a3: usize,
a4: usize,
a5: usize,
a6: usize,
a7: usize,
ret: &mut Result,
);
// fn _xous_syscall_rust(
// nr: usize,
// a1: usize,
// a2: usize,
// a3: usize,
// a4: usize,
// a5: usize,
// a6: usize,
// a7: usize,
// ret: &mut Result,
// );
fn _xous_syscall(
nr: usize,
a1: usize,