kernel: fix server indexing

Server IDs were being improperly indexed, resulting in a
probably-invalid server index being returned.

Adjust these so that correct server indices are returned.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2020-08-29 22:10:24 +08:00
parent 7d8f589830
commit 8f70c0640c
6 changed files with 102 additions and 52 deletions

View file

@ -38,9 +38,9 @@
"vmip",
"xous"
],
"rust-analyzer.checkOnSave.allTargets": false,
"rust-analyzer.checkOnSave.extraArgs": [
"--target",
"riscv32imac-unknown-none-elf"
]
"rust-analyzer.checkOnSave.allTargets": false
// "rust-analyzer.checkOnSave.extraArgs": [
// "--target",
// "riscv32imac-unknown-none-elf"
// ]
}

View file

@ -14,7 +14,7 @@ use std::thread_local;
use crate::arch::process::Process;
use crate::services::SystemServices;
use xous_kernel::{MemoryAddress, ProcessInit, ProcessKey, Result, SysCall, PID, TID};
use xous_kernel::{MemoryAddress, ProcessInit, ProcessKey, Result, SysCall, ThreadInit, PID, TID};
enum ThreadMessage {
SysCall(PID, TID, SysCall),
@ -263,13 +263,15 @@ fn listen_thread(
let mut access_key = [0u8; 16];
conn.read_exact(&mut access_key).unwrap();
// Spawn a new process. This process will start out in the "Setup()" state.
// Spawn a new process. This process will start out in the "Allocated" state.
chn.send(ThreadMessage::NewConnection(
conn.try_clone()
.expect("couldn't make a copy of the network connection for the kernel"),
ProcessKey::new(access_key),
))
.expect("couldn't request a new PID");
// The kernel will immediately respond with a new PID.
let NewPidMessage::NewPid(new_pid) = new_pid_channel
.recv()
.expect("couldn't receive message from main thread");
@ -441,7 +443,8 @@ pub fn idle() -> bool {
let new_pid = SystemServices::with_mut(|ss| ss.create_process(init)).unwrap();
println!(" {:^5} | {}", new_pid, arg);
let process_args = xous_kernel::ProcessArgs::new("program", arg);
xous_kernel::arch::create_process_post(process_args, init, new_pid).expect("couldn't spawn");
xous_kernel::arch::create_process_post(process_args, init, new_pid)
.expect("couldn't spawn");
}
}
@ -452,10 +455,10 @@ pub fn idle() -> bool {
// into a PID, and register the connection with the server.
let new_pid =
crate::arch::process::register_connection_for_key(conn, access_key).unwrap();
// println!(
// "KERNEL: Access key {:?} mapped to PID {}",
// access_key, new_pid
// );
println!(
"KERNEL: Access key {:?} mapped to PID {}",
access_key, new_pid
);
// Inform the backchannel of the new process ID.
new_pid_sender
@ -468,7 +471,11 @@ pub fn idle() -> bool {
// Switch to this process immediately, which moves it from `Setup(_)` to `Running(0)`.
// Note that in this system, multiple processes can be active at once. This is
// similar to having one core for each process
// SystemServices::with_mut(|ss| ss.switch_to_thread(new_pid, Some(1))).unwrap();
SystemServices::with_mut(|ss| {
ss.create_thread(new_pid, ThreadInit {})?;
ss.switch_to_thread(new_pid, None)
})
.unwrap();
}
ThreadMessage::SysCall(pid, thread_id, call) => {
// println!("KERNEL({}): Received syscall {:?}", pid, call);

View file

@ -1,5 +1,5 @@
use crate::arch;
use crate::services::SystemServices;
// use crate::services::SystemServices;
use xous_kernel::{MemoryAddress, PID};
static mut IRQ_HANDLERS: [Option<(PID, MemoryAddress, Option<MemoryAddress>)>; 32] = [None; 32];

View file

@ -359,7 +359,7 @@ impl Server {
) => (
xous_kernel::MessageEnvelope {
sender: self.queue_tail | (server_idx << 16),
message: xous_kernel::Message::Borrow(xous_kernel::MemoryMessage {
body: xous_kernel::Message::Borrow(xous_kernel::MemoryMessage {
id,
buf: MemoryRange::new(buf, buf_size).ok()?,
offset: MemorySize::new(offset),
@ -385,7 +385,7 @@ impl Server {
) => (
xous_kernel::MessageEnvelope {
sender: self.queue_tail | (server_idx << 16),
message: xous_kernel::Message::MutableBorrow(xous_kernel::MemoryMessage {
body: xous_kernel::Message::MutableBorrow(xous_kernel::MemoryMessage {
id,
buf: MemoryRange::new(buf, buf_size).ok()?,
offset: MemorySize::new(offset),
@ -411,7 +411,7 @@ impl Server {
) => (
xous_kernel::MessageEnvelope {
sender: self.queue_tail | (server_idx << 16),
message: xous_kernel::Message::Borrow(xous_kernel::MemoryMessage {
body: xous_kernel::Message::Borrow(xous_kernel::MemoryMessage {
id,
buf: MemoryRange::new(buf, buf_size).ok()?,
offset: MemorySize::new(offset),
@ -437,7 +437,7 @@ impl Server {
) => (
xous_kernel::MessageEnvelope {
sender: self.queue_tail | (server_idx << 16),
message: xous_kernel::Message::MutableBorrow(xous_kernel::MemoryMessage {
body: xous_kernel::Message::MutableBorrow(xous_kernel::MemoryMessage {
id,
buf: MemoryRange::new(buf, buf_size).ok()?,
offset: MemorySize::new(offset),
@ -463,7 +463,7 @@ impl Server {
) => {
let msg = xous_kernel::MessageEnvelope {
sender: make_sender(pid, ctx),
message: xous_kernel::Message::Move(xous_kernel::MemoryMessage {
body: xous_kernel::Message::Move(xous_kernel::MemoryMessage {
id,
buf: MemoryRange::new(buf, buf_size).ok()?,
offset: MemorySize::new(offset),
@ -482,7 +482,7 @@ impl Server {
QueuedMessage::ScalarMessage(pid, ctx, _reserved, id, arg1, arg2, arg3, arg4) => {
let msg = xous_kernel::MessageEnvelope {
sender: make_sender(pid, ctx),
message: xous_kernel::Message::Scalar(xous_kernel::ScalarMessage {
body: xous_kernel::Message::Scalar(xous_kernel::ScalarMessage {
id,
arg1,
arg2,

View file

@ -2,9 +2,10 @@ use crate::arch;
use crate::arch::mem::MemoryMapping;
pub use crate::arch::process::Process as ArchProcess;
pub use crate::arch::process::Thread;
use crate::mem::MemoryManager;
use xous_kernel::MemoryRange;
use core::num::NonZeroU8;
use crate::filled_array;
use crate::server::Server;
// use core::mem;
@ -126,7 +127,7 @@ pub struct ProcessInner {
pub mem_heap_max: usize,
/// A mapping of connection IDs to server indexes
pub connection_map: [u8; 32],
pub connection_map: [Option<NonZeroU8>; 32],
/// A copy of this process' ID
pub pid: PID,
@ -145,7 +146,7 @@ impl Default for ProcessInner {
mem_heap_base: arch::mem::DEFAULT_HEAP_BASE,
mem_heap_size: 0,
mem_heap_max: 524_288,
connection_map: [0; 32],
connection_map: [None; 32],
pid: unsafe { PID::new_unchecked(1) },
_reserved: [0; 1],
}
@ -398,7 +399,11 @@ impl SystemServices {
/// 2. Save the process state, if it hasn't already been saved
/// 3. Run the new process, returning to an illegal instruction
#[cfg(baremetal)]
pub fn finish_callback_and_resume(&mut self, pid: PID, tid: TID) -> Result<(), xous_kernel::Error> {
pub fn finish_callback_and_resume(
&mut self,
pid: PID,
tid: TID,
) -> Result<(), xous_kernel::Error> {
// Get the current process (which was the interrupt handler) and mark it
// as Ready. Note that the new PID may very well be the same PID.
{
@ -474,7 +479,9 @@ impl SystemServices {
.get_process_mut(current_pid)
.expect("couldn't get current PID");
current.state = match current.state {
ProcessState::Running(x) => ProcessState::Ready(x | (1 << arch::process::current_tid())),
ProcessState::Running(x) => {
ProcessState::Ready(x | (1 << arch::process::current_tid()))
}
y => panic!("current process was {:?}, not 'Running(_)'", y),
};
println!("Making PID {} state {:?}", current_pid, current.state);
@ -557,7 +564,11 @@ impl SystemServices {
/// # Panics
///
/// If the current process is not running, or if it's "Running" but has no free contexts
pub fn switch_to_thread(&mut self, pid: PID, tid: Option<TID>) -> Result<(), xous_kernel::Error> {
pub fn switch_to_thread(
&mut self,
pid: PID,
tid: Option<TID>,
) -> Result<(), xous_kernel::Error> {
let process = self.get_process_mut(pid)?;
// println!(
// "switch_to_thread({}:{:?}): Old state was {:?}",
@ -1226,7 +1237,11 @@ impl SystemServices {
///
/// * **ThreadNotAvailable**: The process has used all of its context
/// slots.
pub fn create_thread(&mut self, pid: PID, thread_init: ThreadInit) -> Result<TID, xous_kernel::Error> {
pub fn create_thread(
&mut self,
pid: PID,
thread_init: ThreadInit,
) -> Result<TID, xous_kernel::Error> {
let mut process = self.get_process_mut(pid)?;
process.activate()?;
@ -1274,10 +1289,13 @@ impl SystemServices {
// TODO: Come up with a way to randomize the server ID
let ppid = self.get_process(pid)?.ppid.get();
if ppid != 1 {
panic!("Clients cannot start servers yet");
panic!(
"KERNEL({}): Non-PID1 processes cannot start servers yet",
pid.get()
);
}
for entry in self.servers.iter_mut() {
for (_idx, entry) in self.servers.iter_mut().enumerate() {
if entry == &None {
#[cfg(baremetal)]
// Allocate a single page for the server queue
@ -1290,10 +1308,12 @@ impl SystemServices {
#[cfg(not(baremetal))]
let backing = MemoryRange::new(4096, 4096).unwrap();
// println!(
// "KERNEL({}): Found a free slot for a server -- allocating an entry",
// self.pid.get()
// );
println!(
"KERNEL({}): Found a free slot for server {:?} @ {} -- allocating an entry",
pid.get(),
sid,
_idx,
);
// Initialize the server with the given memory page.
Server::init(entry, pid, sid, backing).map_err(|x| x)?;
@ -1309,34 +1329,50 @@ impl SystemServices {
// Check to see if we've already connected to this server.
// While doing this, find a free slot in case we haven't
// yet connected.
let mut slot_idx = None;
let _pid = crate::arch::process::current_pid();
// println!("KERNEL({}): Server table: {:?}", _pid.get(), self.servers);
ArchProcess::with_inner_mut(|process_inner| {
let mut slot_idx = None;
// 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() {
for (connection_idx, server_idx) in process_inner.connection_map.iter().enumerate() {
// If we find an empty slot, use it
if *server_idx == 0 {
slot_idx = Some(idx);
if server_idx.is_none() {
if slot_idx.is_none() {
slot_idx = Some(connection_idx);
}
continue;
}
// If a connection to this server ID exists already, return it.
if let Some(allocated_server) = &self.servers[*server_idx as usize] {
let server_idx = (server_idx.unwrap().get() as usize) - 2;
if let Some(allocated_server) = &self.servers[server_idx] {
if allocated_server.sid == sid {
return Ok(idx as CID + 2);
// println!("KERNEL({}): Existing connection to SID {:?} found in this process @ {}, process connection map is: {:?}",
// _pid.get(),
// sid,
// (connection_idx as CID) + 2,
// process_inner.connection_map,
// );
return Ok((connection_idx as CID) + 2);
}
}
}
let slot_idx = slot_idx.ok_or_else(|| Error::OutOfMemory)?;
// Look through all servers for one whose SID matches.
for (idx, server) in self.servers.iter().enumerate() {
for (server_idx, server) in self.servers.iter().enumerate() {
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);
process_inner.connection_map[slot_idx] = Some(NonZeroU8::new((server_idx as u8) + 2).unwrap());
// println!(
// "KERNEL({}): New connection to {:?}. After connection, process connection map is: {:?}",
// _pid.get(),
// sid,
// process_inner.connection_map
// );
return Ok((slot_idx as CID) + 2);
}
}
}
@ -1366,14 +1402,19 @@ impl SystemServices {
ArchProcess::with_inner(|process_inner| {
if cid >= process_inner.connection_map.len() {
println!("CID {} > connection map len", cid);
println!("KERNEL: CID {} > connection map len", cid);
return None;
}
let server_idx = process_inner.connection_map[cid] as usize;
let mut server_idx = process_inner.connection_map[cid]?.get() as usize;
if server_idx < 2 {
panic!("KERNEL: CID {} is invalid", cid + 2);
}
server_idx -= 2;
if server_idx >= self.servers.len() {
println!("CID {} and server_idx >= {}", cid, server_idx);
println!("KERNEL: CID {} and server_idx >= {}", cid + 2, server_idx);
None
} else {
// println!("KERNEL: SIDX for CID {} found at index {}", cid + 2, server_idx);
Some(server_idx)
}
})
@ -1475,8 +1516,10 @@ impl SystemServices {
// that matches this index. Note that connection map entries
// are offset by two, because 0 == free and 1 == "tombstone".
for mapping in process_inner.connection_map.iter_mut() {
if *mapping == (idx as u8) + 2 {
*mapping = 1;
if let Some(mapping) = mapping {
if mapping.get() == (idx as u8) + 2 {
*mapping = NonZeroU8::new(1).unwrap();
}
}
}
})

View file

@ -119,7 +119,7 @@ fn send_message(pid: PID, thread: TID, cid: CID, message: Message) -> SysCallRes
e
})?,
};
let envelope = MessageEnvelope { sender, message };
let envelope = MessageEnvelope { sender, body: message };
// Mark the server's context as "Ready". If this fails, return the context
// to the blocking list.