kernel: start using tls

This allows us to run multiple kernels in parallel.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2020-06-24 17:51:31 +08:00
parent abcba48a2a
commit 54a0db50b3
7 changed files with 116 additions and 113 deletions

View file

@ -3,12 +3,14 @@ pub mod mem;
pub mod process;
pub mod syscall;
use std::cell::RefCell;
use std::env;
use std::io::Read;
use std::mem::size_of;
use std::net::{TcpListener, TcpStream};
use std::sync::mpsc::{channel, Receiver, Sender};
use std::thread::spawn;
use std::thread_local;
use std::time::Duration;
use xous::{Result, SysCall, PID};
@ -16,16 +18,24 @@ use xous::{Result, SysCall, PID};
use crate::arch::process::ProcessHandle;
use crate::services::SystemServicesHandle;
pub type KernelArguments = Option<String>;
const DEFAULT_LISTEN_ADDRESS: &str = "localhost:9687";
thread_local!(static NETWORK_LISTEN_ADDRESS: RefCell<String> = RefCell::new(DEFAULT_LISTEN_ADDRESS.to_owned()));
/// Set the network address for this particular thread.
pub fn set_listen_address(new_address: &str) {
NETWORK_LISTEN_ADDRESS.with(|nla| {
let mut address = nla.borrow_mut();
*address = new_address.to_owned();
});
}
/// Each client gets its own connection and its own thread, which is handled here.
fn handle_connection(mut conn: TcpStream, pid: PID, chn: Sender<(PID, SysCall)>) {
loop {
let mut pkt = [0usize; 8];
let mut incoming_word = [0u8; size_of::<usize>()];
conn.set_nonblocking(true).expect("couldn't enable nonblocking mode");
conn.set_nonblocking(true)
.expect("couldn't enable nonblocking mode");
for word in pkt.iter_mut() {
loop {
if let Err(e) = conn.read_exact(&mut incoming_word) {
@ -60,10 +70,7 @@ fn handle_connection(mut conn: TcpStream, pid: PID, chn: Sender<(PID, SysCall)>)
}
}
fn listen_thread(address: Option<String>, chn: Sender<(PID, SysCall)>, quit: Receiver<()>) {
let listen_addr = address.unwrap_or_else(|| {
env::var("XOUS_LISTEN_ADDR").unwrap_or_else(|_| DEFAULT_LISTEN_ADDRESS.to_owned())
});
fn listen_thread(listen_addr: String, chn: Sender<(PID, SysCall)>, quit: Receiver<()>) {
println!("Starting Xous server on {}...", listen_addr);
let listener = TcpListener::bind(listen_addr).unwrap_or_else(|e| {
panic!("Unable to create server: {}", e);
@ -85,7 +92,10 @@ fn listen_thread(address: Option<String>, chn: Sender<(PID, SysCall)>, quit: Rec
ss.spawn_process(process::ProcessInit::new(conn.try_clone().unwrap()), ())
.unwrap()
};
println!("New client connected from {} and assigned PID {}", addr, new_pid);
println!(
"New client connected from {} and assigned PID {}",
addr, new_pid
);
let conn_copy = conn.try_clone().expect("couldn't duplicate connection");
let jh = spawn(move || handle_connection(conn, new_pid, thr_chn));
clients.push((jh, conn_copy));
@ -95,10 +105,14 @@ fn listen_thread(address: Option<String>, chn: Sender<(PID, SysCall)>, quit: Rec
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {
continue;
}
x => {
_x => {
if let Err(e) = _x {
println!("Got error receiving quit timeout: {:?}", e);
}
for (jh, conn) in clients {
use std::net::Shutdown;
conn.shutdown(Shutdown::Both).expect("couldn't shutdown client");
conn.shutdown(Shutdown::Both)
.expect("couldn't shutdown client");
jh.join().expect("couldn't join client thread");
}
return;
@ -116,15 +130,18 @@ fn listen_thread(address: Option<String>, chn: Sender<(PID, SysCall)>, quit: Rec
/// The idle function is run when there are no directly-runnable processes
/// that kmain can activate. In a hosted environment,this is the primary
/// thread that handles network communications, and this function never returns.
pub fn idle(args: &KernelArguments) -> bool {
pub fn idle() -> bool {
// Start listening.
let (sender, receiver) = channel();
let (term_sender, term_receiver) = channel();
let server_addr = args.clone();
let listen_thread_handle = spawn(move || listen_thread(server_addr, sender, term_receiver));
let listen_addr = env::var("XOUS_LISTEN_ADDR")
.unwrap_or_else(|_| NETWORK_LISTEN_ADDRESS.with(|nla| nla.borrow().clone()));
let listen_thread_handle = spawn(move || listen_thread(listen_addr, sender, term_receiver));
while let Ok((pid, call)) = receiver.recv() {
println!("Received message from PID {}: {:?}", pid, call);
{
let mut ss = SystemServicesHandle::get();
ss.switch_to(pid, Some(1)).unwrap();
@ -138,9 +155,10 @@ pub fn idle(args: &KernelArguments) -> bool {
// Handle the syscall within the Xous kernel
let response = crate::syscall::handle(pid, call).unwrap_or_else(Result::Error);
println!("KERNEL: Response to PID {}: {:?}", pid, response);
// There's a response if it wasn't a blocked process and we're not terminating.
// Send the response back to the target.
if response != Result::BlockedProcess && !is_terminate && !is_shutdown{
if response != Result::BlockedProcess && !is_terminate && !is_shutdown {
{
let mut processes = ProcessHandle::get();
let mut response_vec = Vec::new();

View file

@ -126,7 +126,7 @@ fn next_pid_to_run(last_pid: Option<PID>) -> Option<PID> {
/// Common main function for baremetal and hosted environments.
#[no_mangle]
pub extern "C" fn kmain(args: crate::arch::KernelArguments) {
pub extern "C" fn kmain() {
// Start performing round-robin on all child processes.
// Note that at this point, no new direct children of INIT may be created.
@ -144,7 +144,7 @@ pub extern "C" fn kmain(args: crate::arch::KernelArguments) {
None => {
println!("No runnable tasks found. Entering idle state...");
// Special case for testing: idle can return `false` to indicate exit
if ! arch::idle(&args) {
if ! arch::idle() {
return;
}
}
@ -156,5 +156,5 @@ pub extern "C" fn kmain(args: crate::arch::KernelArguments) {
/// this function does not exist.
#[cfg(not(baremetal))]
fn main() {
kmain(None);
kmain();
}

View file

@ -120,7 +120,7 @@ enum QueuedMessage {
}
/// A pointer to resolve a server ID to a particular process
#[derive(PartialEq)]
#[derive(PartialEq, Debug)]
pub struct Server {
/// A randomly-generated ID
pub sid: SID,

View file

@ -1150,6 +1150,9 @@ impl SystemServices {
let mut slot_idx = None;
let mut process = ProcessHandle::get();
println!("KERNEL: Connecting to server -- map: {:?}", process.inner.connection_map);
println!("KERNEL: All servers: {:?}", self.servers);
// Look through the connection map for (1) a free slot, and (2) an
// existing connection
for (idx, server_idx) in process.inner.connection_map.iter().enumerate() {
@ -1171,6 +1174,7 @@ impl SystemServices {
if let Some(allocated_server) = server {
if allocated_server.sid == sid {
process.inner.connection_map[slot_idx] = idx as u8 + 2;
println!("KERNEL: After connection, server map is: {:?}", process.inner.connection_map);
return Ok(idx + 1);
}
}

View file

@ -3,7 +3,8 @@ use std::thread::spawn;
use std::net::{TcpStream, ToSocketAddrs};
use std::time::Duration;
use xous::{rsyscall_to, SysCall, Result};
use xous::{SysCall, rsyscall};
use std::sync::mpsc::channel;
mod shutdown;
@ -15,17 +16,23 @@ fn shutdown() {
.expect("invalid server address")
.next()
.expect("unable to resolve server address");
let server_spec = server_spec.to_owned();
// Launch the main thread
let main_thread = spawn(|| kmain(Some(server_spec)));
let server_spec_server = server_spec.clone();
let main_thread = spawn(move || {
crate::arch::set_listen_address(&server_spec_server);
kmain()
});
// This is now the client.
xous::hosted::set_xous_address(&server_spec);
// Connect to server
for i in 1..11 {
println!("Retrying connection {}/10", i);
let res = TcpStream::connect_timeout(&server_addr, Duration::from_millis(200));
if let Ok(mut stream) = res {
let call_result = rsyscall_to(SysCall::Shutdown, &mut stream);
if res.is_ok() {
let call_result = rsyscall(SysCall::Shutdown);
println!("Call result: {:?}", call_result);
break;
}
@ -42,63 +49,59 @@ fn send_scalar_message() {
.expect("invalid server address")
.next()
.expect("unable to resolve server address");
let server_spec = server_spec.to_owned();
// Launch the main thread
let main_thread = spawn(|| kmain(Some(server_spec)));
let server_spec_server = server_spec.clone();
let main_thread = spawn(move || {
crate::arch::set_listen_address(&server_spec_server);
kmain()
});
// Connect to server. This first instance needs to make sure the kernel is listening.
let mut server_conn = None;
// let mut server_conn = None;
let mut connected = false;
for i in 1..11 {
println!("Retrying connection {}/10", i);
let res = TcpStream::connect_timeout(&server_addr, Duration::from_millis(200));
if let Ok(stream) = res {
server_conn = Some(stream);
if res.is_ok() {
connected = true;
break;
}
}
// Convert the Option<conn> into conn
let mut server_conn = server_conn.expect("unable to connect to server");
assert!(connected, "unable to connect to server");
// The client instance should connect instantly.
let mut client_conn = TcpStream::connect_timeout(&server_addr, Duration::from_millis(200)).expect("client couldn't connect");
let xous_client_spec = server_spec.to_owned();
let xous_server_spec = server_spec.to_owned();
xous::hosted::set_xous_address(server_spec);
let server_xous_addr = match rsyscall_to(SysCall::CreateServer(5), &mut server_conn).expect("couldn't create xous server") {
Result::ServerID(sid) => sid,
x => panic!("unexpected syscall result: {:?}", x),
};
let (server_addr_send, server_addr_recv) = channel();
eprintln!("CLIENT: Connecting to server {:?}", server_xous_addr);
let client_xous_conn = match rsyscall_to(SysCall::Connect(server_xous_addr), &mut client_conn) {
Err(e) => panic!("unable to connect to xous server: {:?}", e),
Ok(Result::ConnectionID(i)) => i,
Ok(o) => panic!("unexpected return value: {:?}", o),
};
let xous_server = spawn(move || {
xous::hosted::set_xous_address(&xous_client_spec);
println!("SERVER: Creating server");
let sid = xous::create_server(0x7884_3123).expect("couldn't create test server");
println!("SERVER: Now listening on {:?}", sid);
server_addr_send.send(sid).unwrap();
println!("SERVER: Listening for message...");
let msg = xous::receive_message(sid).expect("couldn't receive messages");
println!("SERVER: Received message: {:?}", msg);
});
eprintln!("CLIENT: Calling SendMessage...");
match rsyscall_to(
SysCall::SendMessage(client_xous_conn, xous::Message::Scalar(xous::ScalarMessage { id: 1, arg1: 2, arg2: 3, arg3: 4, arg4: 5} )),
&mut client_conn).expect("couldn't send message") {
Result::Ok => (),
x => panic!("Unexpected message result: {:?}", x),
}
let xous_client = spawn(move || {
xous::hosted::set_xous_address(&xous_server_spec);
println!("CLIENT: Waiting for server address...");
let sid = server_addr_recv.recv().unwrap();
println!("CLIENT: Connecting to server {:?}", sid);
let conn = xous::connect(sid).expect("couldn't connect to server");
xous::send_message(conn, xous::Message::Scalar(xous::ScalarMessage { id: 1, arg1: 2, arg2: 3, arg3: 4, arg4: 5} )).expect("couldn't send message");
});
std::thread::sleep(Duration::from_secs(1));
eprintln!("SERVER: Calling ReceiveMessage...");
let incoming_message = match rsyscall_to(
SysCall::ReceiveMessage(server_xous_addr),
&mut server_conn
) {
Err(e) => panic!("couldn't receive message: {:?}", e),
Ok(Result::Message(m)) => m,
Ok(o) => panic!("received invalid message: {:?}", o),
};
eprintln!("SERVER: Received message: {:?}", incoming_message);
xous_server.join().expect("couldn't join server process");
xous_client.join().expect("couldn't join client process");
// Any process ought to be able to shut down the system currently.
rsyscall_to(SysCall::Shutdown, &mut client_conn).expect("unable to shutdown server");
rsyscall(SysCall::Shutdown).expect("unable to shutdown server");
eprintln!("Joining main thread, waiting for it to quit");
main_thread.join().expect("couldn't join main thread");
main_thread.join().expect("couldn't join kernel process");
}

View file

@ -1,14 +1,20 @@
use lazy_static::lazy_static;
use std::io::{Read, Write};
use std::mem::size_of;
use std::net::TcpStream;
use std::sync::Mutex;
use std::thread_local;
use std::cell::RefCell;
use crate::Result;
lazy_static! {
pub static ref XOUS_SERVER_CONNECTION: Mutex<Option<TcpStream>> = Mutex::new(None);
thread_local!(static NETWORK_CONNECT_ADDRESS: RefCell<String> = RefCell::new("localhost:9687".to_owned()));
thread_local!(static XOUS_SERVER_CONNECTION: RefCell<Option<TcpStream>> = RefCell::new(None));
/// Set the network address for this particular thread.
pub fn set_xous_address(new_address: &str) {
NETWORK_CONNECT_ADDRESS.with(|nca| {
let mut address = nca.borrow_mut();
*address = new_address.to_owned();
});
}
#[allow(clippy::too_many_arguments)]
@ -24,19 +30,22 @@ pub fn _xous_syscall(
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);
}
let xsc: &mut TcpStream = (*xous_server_connection).as_mut().unwrap();
_xous_syscall_to(nr, a1, a2, a3, a4, a5, a6, a7, ret, xsc)
XOUS_SERVER_CONNECTION.with(|xsc| {
if xsc.borrow().is_none() {
NETWORK_CONNECT_ADDRESS.with(|nca| {
println!("Opening connection to Xous server @ {}...", nca.borrow());
let conn = TcpStream::connect(nca.borrow().as_str()).unwrap();
*xsc.borrow_mut() = Some(conn);
});
}
// let xsc: &mut TcpStream = (*xous_server_connection).as_mut().unwrap();
_xous_syscall_to(nr, a1, a2, a3, a4, a5, a6, a7, ret, xsc.borrow_mut().as_mut().unwrap())
})
}
#[allow(clippy::too_many_arguments)]
#[no_mangle]
pub fn _xous_syscall_to(
fn _xous_syscall_to(
nr: usize,
a1: usize,
a2: usize,
@ -48,10 +57,10 @@ pub fn _xous_syscall_to(
ret: &mut Result,
xsc: &mut TcpStream,
) {
// print!(
// "Making Syscall: {:?}",
// SysCall::from_args(nr, a1, a2, a3, a4, a5, a6, a7).unwrap()
// );
println!(
"Making Syscall: {:?}",
crate::SysCall::from_args(nr, a1, a2, a3, a4, a5, a6, a7).unwrap()
);
// Send the packet to the server
for word in &[nr, a1, a2, a3, a4, a5, a6, a7] {
@ -68,7 +77,7 @@ pub fn _xous_syscall_to(
}
*ret = Result::from_args(pkt);
// println!(" Response: {:?}", *ret);
println!(" Response: {:?}", *ret);
if Result::BlockedProcess == *ret {
// println!(" Waiting again");
} else {

View file

@ -5,9 +5,6 @@ use crate::{
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
#[cfg(not(baremetal))]
use std::net::TcpStream;
#[derive(Debug, PartialEq)]
pub enum SysCall {
/// Allocates pages of memory, equal to a total of `size` bytes. A physical
@ -636,19 +633,6 @@ extern "Rust" {
a7: usize,
ret: &mut Result,
);
#[cfg(not(baremetal))]
pub fn _xous_syscall_to(
nr: usize,
a1: usize,
a2: usize,
a3: usize,
a4: usize,
a5: usize,
a6: usize,
a7: usize,
ret: &mut Result,
xsc: &mut TcpStream,
);
}
/// Map the given physical address to the given virtual address.
@ -820,21 +804,6 @@ pub fn rsyscall(call: SysCall) -> SysCallResult {
}
}
/// Perform a remote syscall to a given address
#[cfg(not(baremetal))]
pub fn rsyscall_to(call: SysCall, stream: &mut TcpStream) -> SysCallResult {
let mut ret = Result::Ok;
let args = call.as_args();
unsafe {
_xous_syscall_to(
args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], &mut ret, stream,
)
};
match ret {
Result::Error(e) => Err(e),
other => Ok(other),
}
}
// /// This is dangerous, but fast.
// pub unsafe fn dangerous_syscall(call: SysCall) -> SyscallResult {
// use core::mem::{transmute, MaybeUninit};