kernel: down to 29 errors on bare metal

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2020-08-09 16:48:58 +08:00
parent ebf568dadd
commit 814be7cd84
4 changed files with 98 additions and 64 deletions

View file

@ -1,8 +1,8 @@
use crate::arch::current_pid;
use crate::arch::mem::MemoryMapping;
use crate::arch::process::Process as ArchProcess;
use crate::mem::{MemoryManagerHandle, PAGE_SIZE};
use crate::arch::process::{Thread, RETURN_FROM_ISR};
use crate::mem::{MemoryManagerHandle, PAGE_SIZE};
use riscv::register::{scause, sepc, sie, sstatus, stval, vexriscv::sim, vexriscv::sip};
use xous::{SysCall, PID, TID};
@ -87,7 +87,8 @@ pub extern "C" fn trap_handler(
_xous_syscall_return_result(&xous::Result::Error(xous::Error::UnhandledSyscall), thread)
});
let response = crate::syscall::handle(pid, tid, call).unwrap_or_else(|e| xous::Result::Error(e));
let response =
crate::syscall::handle(pid, tid, call).unwrap_or_else(|e| xous::Result::Error(e));
println!("Syscall Result: {:?}", response);
let thread = ArchProcess::with_current(|p| p.current_thread());
@ -150,8 +151,12 @@ pub extern "C" fn trap_handler(
flush_mmu();
};
ArchProcess::with_current(|process|
crate::arch::syscall::resume(current_pid().get() == 1, process.current_thread()));
ArchProcess::with_current(|process| {
crate::arch::syscall::resume(
current_pid().get() == 1,
process.current_thread(),
)
});
}
}
RiscvException::InstructionPageFault(RETURN_FROM_ISR, _offset) => {
@ -177,7 +182,9 @@ pub extern "C" fn trap_handler(
// Re-enable interrupts now that they're handled
enable_all_irqs();
ArchProcess::with_current(|process| crate::arch::syscall::resume(current_pid().get() == 1, process.current_thread()));
ArchProcess::with_current(|process| {
crate::arch::syscall::resume(current_pid().get() == 1, process.current_thread())
});
}
_ => (),
}
@ -198,7 +205,8 @@ pub extern "C" fn trap_handler(
}
}
crate::irq::handle(irqs_pending).expect("Couldn't handle IRQ");
let mut process = ProcessHandle::get();
crate::arch::syscall::resume(current_pid().get() == 1, process.current_thread());
ArchProcess::with_current(|process| {
crate::arch::syscall::resume(current_pid().get() == 1, process.current_thread())
})
}
}

View file

@ -9,7 +9,7 @@ use xous::{ProcessInit, ProcessKey, ThreadInit, PID, TID};
use crate::args::KernelArguments;
const DEFAULT_STACK_SIZE: usize = 131072;
const MAX_PROCESS_COUNT: usize = 32;
pub const MAX_PROCESS_COUNT: usize = 32;
// pub use crate::arch::mem::DEFAULT_STACK_TOP;
/// This is the address a program will jump to in order to return from an ISR.
@ -59,7 +59,7 @@ static PROCESS_TABLE: RefCell<ProcessTable> = RefCell::new(ProcessTable {
pub struct InitialProcess {
/// The RISC-V SATP value, which includes the offset of the root page
/// table plus the process ID.
satp: usize,
pub satp: usize,
/// Where execution begins
entrypoint: usize,
@ -244,7 +244,7 @@ pub fn set_current_pid(pid: PID) {
}
pub fn current_pid() -> PID {
unsafe { PROCESS_TABLE.get().current }
unsafe { PROCESS_TABLE.borrow().current }
}

View file

@ -141,7 +141,9 @@ pub struct Server {
queue_tail: usize,
/// Where data will appear
// queue: &'static mut [QueuedMessage],
#[cfg(baremetal)]
queue: &'static mut [QueuedMessage],
#[cfg(not(baremetal))]
queue: Vec<QueuedMessage>,
/// The `context mask` is a bitfield of contexts that are able to handle
@ -172,18 +174,24 @@ impl Server {
return Err(xous::Error::MemoryInUse);
}
let mut queue = vec![]; /* = unsafe {
slice::from_raw_parts_mut(
queue_addr as *mut QueuedMessage,
queue_size / mem::size_of::<QueuedMessage>(),
)
};*/
#[cfg(baremetal)]
let mut queue = unsafe {
core::slice::from_raw_parts_mut(
queue_addr as *mut QueuedMessage,
queue_size / mem::size_of::<QueuedMessage>(),
)
};
// TODO: Replace this with a direct operation on a passed-in page
queue.resize_with(
crate::arch::mem::PAGE_SIZE / mem::size_of::<QueuedMessage>(),
|| QueuedMessage::Empty,
);
#[cfg(not(baremetal))]
let queue = {
let mut queue = vec![];
// TODO: Replace this with a direct operation on a passed-in page
queue.resize_with(
crate::arch::mem::PAGE_SIZE / mem::size_of::<QueuedMessage>(),
|| QueuedMessage::Empty,
);
queue
};
*new = Some(Server {
sid,

View file

@ -4,10 +4,31 @@ pub use crate::arch::process::Process as ArchProcess;
pub use crate::arch::process::Thread;
use core::cell::RefCell;
// Fake macro to implement "thread local" on baremetal as a simple static variable.
// The kernel is single-threaded so it's really just one thread.
#[cfg(baremetal)]
macro_rules! thread_local {
// empty (base case for the recursion)
() => {};
// process multiple declarations
($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = $init:expr; $($rest:tt)*) => (
$(#[$attr])* $vis static $name: $t = $init;
$crate::services::thread_local!($($rest)*);
);
// handle a single declaration
($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = $init:expr) => (
$(#[$attr])* $vis static $name: $t = $init;
);
}
#[cfg(not(baremetal))]
use std::thread_local;
use crate::filled_array;
// use crate::mem::{MemoryManagerHandle, PAGE_SIZE};
use crate::mem::PAGE_SIZE;
use crate::server::Server;
// use core::mem;
use xous::{
@ -160,7 +181,7 @@ impl Process {
/// This process has at least one context that may be run
pub fn runnable(&self) -> bool {
match self.state {
/*ProcessState::Setup(_) | */ProcessState::Ready(_) => true,
/*ProcessState::Setup(_) | */ ProcessState::Ready(_) => true,
_ => false,
}
}
@ -196,24 +217,6 @@ impl Process {
}
}
#[cfg(baremetal)]
static SYSTEM_SERVICES: RefCell<SystemServices> = RefCell::new(SystemServices {
processes: [Process {
state: ProcessState::Free,
ppid: unsafe { PID::new_unchecked(1) },
pid: unsafe { PID::new_unchecked(1) },
mapping: arch::mem::DEFAULT_MEMORY_MAPPING,
// current_thread: 0,
previous_context: INITIAL_TID as u8,
}; MAX_PROCESS_COUNT],
// Note we can't use MAX_SERVER_COUNT here because of how Rust's
// macro tokenization works
servers: filled_array![None; 32],
_syscall_stack: [(0, 0), (0, 0), (0, 0)],
_syscall_depth: 0,
});
#[cfg(not(baremetal))]
thread_local!(static SYSTEM_SERVICES: RefCell<SystemServices> = RefCell::new(SystemServices {
processes: [Process {
state: ProcessState::Free,
@ -248,6 +251,12 @@ impl SystemServices {
where
F: FnOnce(&SystemServices) -> R,
{
#[cfg(baremetal)]
{
let ss = unsafe { SYSTEM_SERVICES }.borrow();
f(&*ss)
}
#[cfg(not(baremetal))]
SYSTEM_SERVICES.with(|ss| f(&ss.borrow()))
}
@ -255,6 +264,13 @@ impl SystemServices {
where
F: FnOnce(&mut SystemServices) -> R,
{
#[cfg(baremetal)]
{
let mut ss = unsafe { SYSTEM_SERVICES }.borrow_mut();
f(&mut *ss)
}
#[cfg(not(baremetal))]
SYSTEM_SERVICES.with(|ss| f(&mut ss.borrow_mut()))
}
@ -262,7 +278,7 @@ impl SystemServices {
/// kernel. These arguments decide where the memory spaces are located, as
/// well as where the stack and program counter should initially go.
#[cfg(baremetal)]
pub fn init_from_memory(&mut self, base: *const u32, args: &KernelArguments) {
pub fn init_from_memory(&mut self, base: *const u32, args: &crate::args::KernelArguments) {
// Look through the kernel arguments and create a new process for each.
let init_offsets = {
let mut init_count = 1;
@ -271,7 +287,7 @@ impl SystemServices {
init_count += 1;
}
}
unsafe { slice::from_raw_parts(base as *const InitialProcess, init_count) }
unsafe { core::slice::from_raw_parts(base as *const crate::arch::process::InitialProcess, init_count) }
};
// Copy over the initial process list. The pid is encoded in the SATP
@ -289,12 +305,13 @@ impl SystemServices {
// init.sp,
// pid - 1
// );
unsafe { process.mapping.from_raw(init.satp) };
unsafe {
process.mapping.from_raw(init.satp);
process.ppid = PID::new_unchecked(1);
};
if pid == 1 {
process.ppid = 0;
process.state = ProcessState::Running(0);
} else {
process.ppid = 1;
process.state = ProcessState::Setup(init.entrypoint, init.sp, DEFAULT_STACK_SIZE);
}
}
@ -533,26 +550,26 @@ impl SystemServices {
process.state = match process.state {
ProcessState::Free => return Err(xous::Error::ProcessNotFound),
ProcessState::Sleeping => return Err(xous::Error::ProcessNotFound),
ProcessState::Allocated => return Err(xous::Error::ProcessNotFound),/*
ProcessState::Allocated => return Err(xous::Error::ProcessNotFound), /*
ProcessState::Setup(setup) => {
// Activate the process, which enables its memory mapping
process.activate()?;
// Activate the process, which enables its memory mapping
process.activate()?;
// If a context is specified for a Setup task to switch to,
// ensure it's the INITIAL_TID. Otherwise it's not valid.
if let Some(ctx) = tid {
if ctx != INITIAL_TID {
return Err(xous::Error::InvalidThread);
}
}
// If a context is specified for a Setup task to switch to,
// ensure it's the INITIAL_TID. Otherwise it's not valid.
if let Some(ctx) = tid {
if ctx != INITIAL_TID {
return Err(xous::Error::InvalidThread);
}
}
let mut p = crate::arch::process::Process::current();
p.setup_thread(INITIAL_TID, setup)?;
p.set_thread(INITIAL_TID)?;
// process.current_thread = INITIAL_TID as u8;
let mut p = crate::arch::process::Process::current();
p.setup_thread(INITIAL_TID, setup)?;
p.set_thread(INITIAL_TID)?;
// process.current_thread = INITIAL_TID as u8;
// Mark the current proces state as "running, and no waiting contexts"
ProcessState::Running(0)
// Mark the current proces state as "running, and no waiting contexts"
ProcessState::Running(0)
}*/
ProcessState::Ready(0) => {
panic!("ProcessState was `Ready(0)`, which is invalid!");
@ -753,7 +770,8 @@ impl SystemServices {
// Ensure the new process can be run.
match new.state {
ProcessState::Free => return Err(xous::Error::ProcessNotFound),
/*ProcessState::Setup(_) | */ProcessState::Allocated => new_tid = INITIAL_TID,
/*ProcessState::Setup(_) | */
ProcessState::Allocated => new_tid = INITIAL_TID,
ProcessState::Running(x) | ProcessState::Ready(x) => {
// If no new context is specified, take the previous
// context. If that is not runnable, do a round-robin