mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-19 09:00:54 +00:00
kernel: down to 29 errors on bare metal
Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
ebf568dadd
commit
814be7cd84
4 changed files with 98 additions and 64 deletions
|
|
@ -1,8 +1,8 @@
|
||||||
use crate::arch::current_pid;
|
use crate::arch::current_pid;
|
||||||
use crate::arch::mem::MemoryMapping;
|
use crate::arch::mem::MemoryMapping;
|
||||||
use crate::arch::process::Process as ArchProcess;
|
use crate::arch::process::Process as ArchProcess;
|
||||||
use crate::mem::{MemoryManagerHandle, PAGE_SIZE};
|
|
||||||
use crate::arch::process::{Thread, RETURN_FROM_ISR};
|
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 riscv::register::{scause, sepc, sie, sstatus, stval, vexriscv::sim, vexriscv::sip};
|
||||||
use xous::{SysCall, PID, TID};
|
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)
|
_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);
|
println!("Syscall Result: {:?}", response);
|
||||||
let thread = ArchProcess::with_current(|p| p.current_thread());
|
let thread = ArchProcess::with_current(|p| p.current_thread());
|
||||||
|
|
@ -150,8 +151,12 @@ pub extern "C" fn trap_handler(
|
||||||
flush_mmu();
|
flush_mmu();
|
||||||
};
|
};
|
||||||
|
|
||||||
ArchProcess::with_current(|process|
|
ArchProcess::with_current(|process| {
|
||||||
crate::arch::syscall::resume(current_pid().get() == 1, process.current_thread()));
|
crate::arch::syscall::resume(
|
||||||
|
current_pid().get() == 1,
|
||||||
|
process.current_thread(),
|
||||||
|
)
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
RiscvException::InstructionPageFault(RETURN_FROM_ISR, _offset) => {
|
RiscvException::InstructionPageFault(RETURN_FROM_ISR, _offset) => {
|
||||||
|
|
@ -177,7 +182,9 @@ pub extern "C" fn trap_handler(
|
||||||
// Re-enable interrupts now that they're handled
|
// Re-enable interrupts now that they're handled
|
||||||
enable_all_irqs();
|
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");
|
crate::irq::handle(irqs_pending).expect("Couldn't handle IRQ");
|
||||||
let mut process = ProcessHandle::get();
|
ArchProcess::with_current(|process| {
|
||||||
crate::arch::syscall::resume(current_pid().get() == 1, process.current_thread());
|
crate::arch::syscall::resume(current_pid().get() == 1, process.current_thread())
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ use xous::{ProcessInit, ProcessKey, ThreadInit, PID, TID};
|
||||||
|
|
||||||
use crate::args::KernelArguments;
|
use crate::args::KernelArguments;
|
||||||
const DEFAULT_STACK_SIZE: usize = 131072;
|
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;
|
// pub use crate::arch::mem::DEFAULT_STACK_TOP;
|
||||||
|
|
||||||
/// This is the address a program will jump to in order to return from an ISR.
|
/// 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 {
|
pub struct InitialProcess {
|
||||||
/// The RISC-V SATP value, which includes the offset of the root page
|
/// The RISC-V SATP value, which includes the offset of the root page
|
||||||
/// table plus the process ID.
|
/// table plus the process ID.
|
||||||
satp: usize,
|
pub satp: usize,
|
||||||
|
|
||||||
/// Where execution begins
|
/// Where execution begins
|
||||||
entrypoint: usize,
|
entrypoint: usize,
|
||||||
|
|
@ -244,7 +244,7 @@ pub fn set_current_pid(pid: PID) {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn current_pid() -> PID {
|
pub fn current_pid() -> PID {
|
||||||
unsafe { PROCESS_TABLE.get().current }
|
unsafe { PROCESS_TABLE.borrow().current }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -141,7 +141,9 @@ pub struct Server {
|
||||||
queue_tail: usize,
|
queue_tail: usize,
|
||||||
|
|
||||||
/// Where data will appear
|
/// Where data will appear
|
||||||
// queue: &'static mut [QueuedMessage],
|
#[cfg(baremetal)]
|
||||||
|
queue: &'static mut [QueuedMessage],
|
||||||
|
#[cfg(not(baremetal))]
|
||||||
queue: Vec<QueuedMessage>,
|
queue: Vec<QueuedMessage>,
|
||||||
|
|
||||||
/// The `context mask` is a bitfield of contexts that are able to handle
|
/// The `context mask` is a bitfield of contexts that are able to handle
|
||||||
|
|
@ -172,18 +174,24 @@ impl Server {
|
||||||
return Err(xous::Error::MemoryInUse);
|
return Err(xous::Error::MemoryInUse);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut queue = vec![]; /* = unsafe {
|
#[cfg(baremetal)]
|
||||||
slice::from_raw_parts_mut(
|
let mut queue = unsafe {
|
||||||
queue_addr as *mut QueuedMessage,
|
core::slice::from_raw_parts_mut(
|
||||||
queue_size / mem::size_of::<QueuedMessage>(),
|
queue_addr as *mut QueuedMessage,
|
||||||
)
|
queue_size / mem::size_of::<QueuedMessage>(),
|
||||||
};*/
|
)
|
||||||
|
};
|
||||||
|
|
||||||
// TODO: Replace this with a direct operation on a passed-in page
|
#[cfg(not(baremetal))]
|
||||||
queue.resize_with(
|
let queue = {
|
||||||
crate::arch::mem::PAGE_SIZE / mem::size_of::<QueuedMessage>(),
|
let mut queue = vec![];
|
||||||
|| QueuedMessage::Empty,
|
// 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 {
|
*new = Some(Server {
|
||||||
sid,
|
sid,
|
||||||
|
|
|
||||||
|
|
@ -4,10 +4,31 @@ pub use crate::arch::process::Process as ArchProcess;
|
||||||
pub use crate::arch::process::Thread;
|
pub use crate::arch::process::Thread;
|
||||||
|
|
||||||
use core::cell::RefCell;
|
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 std::thread_local;
|
||||||
|
|
||||||
use crate::filled_array;
|
use crate::filled_array;
|
||||||
// use crate::mem::{MemoryManagerHandle, PAGE_SIZE};
|
use crate::mem::PAGE_SIZE;
|
||||||
use crate::server::Server;
|
use crate::server::Server;
|
||||||
// use core::mem;
|
// use core::mem;
|
||||||
use xous::{
|
use xous::{
|
||||||
|
|
@ -160,7 +181,7 @@ impl Process {
|
||||||
/// This process has at least one context that may be run
|
/// This process has at least one context that may be run
|
||||||
pub fn runnable(&self) -> bool {
|
pub fn runnable(&self) -> bool {
|
||||||
match self.state {
|
match self.state {
|
||||||
/*ProcessState::Setup(_) | */ProcessState::Ready(_) => true,
|
/*ProcessState::Setup(_) | */ ProcessState::Ready(_) => true,
|
||||||
_ => false,
|
_ => 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 {
|
thread_local!(static SYSTEM_SERVICES: RefCell<SystemServices> = RefCell::new(SystemServices {
|
||||||
processes: [Process {
|
processes: [Process {
|
||||||
state: ProcessState::Free,
|
state: ProcessState::Free,
|
||||||
|
|
@ -248,6 +251,12 @@ impl SystemServices {
|
||||||
where
|
where
|
||||||
F: FnOnce(&SystemServices) -> R,
|
F: FnOnce(&SystemServices) -> R,
|
||||||
{
|
{
|
||||||
|
#[cfg(baremetal)]
|
||||||
|
{
|
||||||
|
let ss = unsafe { SYSTEM_SERVICES }.borrow();
|
||||||
|
f(&*ss)
|
||||||
|
}
|
||||||
|
#[cfg(not(baremetal))]
|
||||||
SYSTEM_SERVICES.with(|ss| f(&ss.borrow()))
|
SYSTEM_SERVICES.with(|ss| f(&ss.borrow()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -255,6 +264,13 @@ impl SystemServices {
|
||||||
where
|
where
|
||||||
F: FnOnce(&mut SystemServices) -> R,
|
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()))
|
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
|
/// kernel. These arguments decide where the memory spaces are located, as
|
||||||
/// well as where the stack and program counter should initially go.
|
/// well as where the stack and program counter should initially go.
|
||||||
#[cfg(baremetal)]
|
#[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.
|
// Look through the kernel arguments and create a new process for each.
|
||||||
let init_offsets = {
|
let init_offsets = {
|
||||||
let mut init_count = 1;
|
let mut init_count = 1;
|
||||||
|
|
@ -271,7 +287,7 @@ impl SystemServices {
|
||||||
init_count += 1;
|
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
|
// Copy over the initial process list. The pid is encoded in the SATP
|
||||||
|
|
@ -289,12 +305,13 @@ impl SystemServices {
|
||||||
// init.sp,
|
// init.sp,
|
||||||
// pid - 1
|
// 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 {
|
if pid == 1 {
|
||||||
process.ppid = 0;
|
|
||||||
process.state = ProcessState::Running(0);
|
process.state = ProcessState::Running(0);
|
||||||
} else {
|
} else {
|
||||||
process.ppid = 1;
|
|
||||||
process.state = ProcessState::Setup(init.entrypoint, init.sp, DEFAULT_STACK_SIZE);
|
process.state = ProcessState::Setup(init.entrypoint, init.sp, DEFAULT_STACK_SIZE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -533,26 +550,26 @@ impl SystemServices {
|
||||||
process.state = match process.state {
|
process.state = match process.state {
|
||||||
ProcessState::Free => return Err(xous::Error::ProcessNotFound),
|
ProcessState::Free => return Err(xous::Error::ProcessNotFound),
|
||||||
ProcessState::Sleeping => 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) => {
|
ProcessState::Setup(setup) => {
|
||||||
// Activate the process, which enables its memory mapping
|
// Activate the process, which enables its memory mapping
|
||||||
process.activate()?;
|
process.activate()?;
|
||||||
|
|
||||||
// If a context is specified for a Setup task to switch to,
|
// If a context is specified for a Setup task to switch to,
|
||||||
// ensure it's the INITIAL_TID. Otherwise it's not valid.
|
// ensure it's the INITIAL_TID. Otherwise it's not valid.
|
||||||
if let Some(ctx) = tid {
|
if let Some(ctx) = tid {
|
||||||
if ctx != INITIAL_TID {
|
if ctx != INITIAL_TID {
|
||||||
return Err(xous::Error::InvalidThread);
|
return Err(xous::Error::InvalidThread);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut p = crate::arch::process::Process::current();
|
let mut p = crate::arch::process::Process::current();
|
||||||
p.setup_thread(INITIAL_TID, setup)?;
|
p.setup_thread(INITIAL_TID, setup)?;
|
||||||
p.set_thread(INITIAL_TID)?;
|
p.set_thread(INITIAL_TID)?;
|
||||||
// process.current_thread = INITIAL_TID as u8;
|
// process.current_thread = INITIAL_TID as u8;
|
||||||
|
|
||||||
// Mark the current proces state as "running, and no waiting contexts"
|
// Mark the current proces state as "running, and no waiting contexts"
|
||||||
ProcessState::Running(0)
|
ProcessState::Running(0)
|
||||||
}*/
|
}*/
|
||||||
ProcessState::Ready(0) => {
|
ProcessState::Ready(0) => {
|
||||||
panic!("ProcessState was `Ready(0)`, which is invalid!");
|
panic!("ProcessState was `Ready(0)`, which is invalid!");
|
||||||
|
|
@ -753,7 +770,8 @@ impl SystemServices {
|
||||||
// Ensure the new process can be run.
|
// Ensure the new process can be run.
|
||||||
match new.state {
|
match new.state {
|
||||||
ProcessState::Free => return Err(xous::Error::ProcessNotFound),
|
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) => {
|
ProcessState::Running(x) | ProcessState::Ready(x) => {
|
||||||
// If no new context is specified, take the previous
|
// If no new context is specified, take the previous
|
||||||
// context. If that is not runnable, do a round-robin
|
// context. If that is not runnable, do a round-robin
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue