kernel: get building

This is where the story really starts...

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2020-08-09 21:33:21 +08:00
parent 8ccecb6d99
commit 0006d4d99c
3 changed files with 62 additions and 31 deletions

View file

@ -3,9 +3,9 @@ use crate::arch::mem::MemoryMapping;
use crate::arch::process::Process as ArchProcess; use crate::arch::process::Process as ArchProcess;
use crate::arch::process::{Thread, RETURN_FROM_ISR}; use crate::arch::process::{Thread, RETURN_FROM_ISR};
use crate::mem::{MemoryManager, PAGE_SIZE}; use crate::mem::{MemoryManager, PAGE_SIZE};
use crate::services::SystemServices;
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};
use crate::services::SystemServices;
extern "Rust" { extern "Rust" {
fn _xous_syscall_return_result(result: &xous::Result, context: &Thread) -> !; fn _xous_syscall_return_result(result: &xous::Result, context: &Thread) -> !;
@ -82,18 +82,25 @@ pub extern "C" fn trap_handler(
// will want to adjust the return value of the current process prior to // will want to adjust the return value of the current process prior to
// performing the switch in order to avoid constantly executing the same // performing the switch in order to avoid constantly executing the same
// instruction. // instruction.
let (tid, thread) = ArchProcess::with_current(|p| (p.current_tid(), p.current_thread())); let tid = ArchProcess::with_current_mut(|p| {
thread.sepc += 4; p.current_thread().sepc += 4;
let call = SysCall::from_args(a0, a1, a2, a3, a4, a5, a6, a7).unwrap_or_else(|_| unsafe { p.current_tid()
_xous_syscall_return_result(&xous::Result::Error(xous::Error::UnhandledSyscall), thread) });
let call = SysCall::from_args(a0, a1, a2, a3, a4, a5, a6, a7).unwrap_or_else(|_| {
ArchProcess::with_current_mut(|p| unsafe {
_xous_syscall_return_result(
&xous::Result::Error(xous::Error::UnhandledSyscall),
p.current_thread(),
)
})
}); });
let response = let response =
crate::syscall::handle(pid, tid, call).unwrap_or_else(|e| xous::Result::Error(e)); 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()); ArchProcess::with_current_mut(|p| {
let thread = p.current_thread();
// If we're resuming a process that was previously sleeping, restore the // If we're resuming a process that was previously sleeping, restore the
// context. Otherwise, keep the context the same but pass the return // context. Otherwise, keep the context the same but pass the return
// values in 8 return registers. // values in 8 return registers.
@ -103,6 +110,7 @@ pub extern "C" fn trap_handler(
// println!("Returning to address {:08x}", tid.sepc); // println!("Returning to address {:08x}", tid.sepc);
unsafe { _xous_syscall_return_result(&response, thread) }; unsafe { _xous_syscall_return_result(&response, thread) };
} }
});
} }
use crate::arch::exception::RiscvException; use crate::arch::exception::RiscvException;
@ -127,7 +135,9 @@ pub extern "C" fn trap_handler(
// the page isn't shared, then this is a reserved page. Allocate // the page isn't shared, then this is a reserved page. Allocate
// a real page to back it and resume execution. // a real page to back it and resume execution.
if flags & 1 == 0 && flags != 0 && flags & (1 << 8) == 0 { if flags & 1 == 0 && flags != 0 && flags & (1 << 8) == 0 {
let new_page = MemoryManager::with_mut(|mm| mm.alloc_page(pid).expect("Couldn't allocate new page")); let new_page = MemoryManager::with_mut(|mm| {
mm.alloc_page(pid).expect("Couldn't allocate new page")
});
let ppn1 = (new_page >> 22) & ((1 << 12) - 1); let ppn1 = (new_page >> 22) & ((1 << 12) - 1);
let ppn0 = (new_page >> 12) & ((1 << 10) - 1); let ppn0 = (new_page >> 12) & ((1 << 10) - 1);
unsafe { unsafe {
@ -149,7 +159,7 @@ pub extern "C" fn trap_handler(
flush_mmu(); flush_mmu();
}; };
ArchProcess::with_current(|process| { ArchProcess::with_current_mut(|process| {
crate::arch::syscall::resume( crate::arch::syscall::resume(
current_pid().get() == 1, current_pid().get() == 1,
process.current_thread(), process.current_thread(),
@ -179,7 +189,7 @@ 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| { ArchProcess::with_current_mut(|process| {
crate::arch::syscall::resume(current_pid().get() == 1, process.current_thread()) crate::arch::syscall::resume(current_pid().get() == 1, process.current_thread())
}); });
} }
@ -202,7 +212,7 @@ 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");
ArchProcess::with_current(|process| { ArchProcess::with_current_mut(|process| {
crate::arch::syscall::resume(current_pid().get() == 1, process.current_thread()) crate::arch::syscall::resume(current_pid().get() == 1, process.current_thread())
}) })
} }

View file

@ -126,6 +126,15 @@ impl Process {
f(&process) f(&process)
} }
/// Calls the provided function with the current inner process state.
pub fn with_current_mut<F, R>(f: F) -> R
where
F: FnOnce(&mut Process) -> R,
{
let mut process = Self::current();
f(&mut process)
}
pub fn with_inner_mut<F, R>(f: F) -> R pub fn with_inner_mut<F, R>(f: F) -> R
where where
F: FnOnce(&mut ProcessInner) -> R, F: FnOnce(&mut ProcessInner) -> R,

View file

@ -43,9 +43,6 @@ pub use crate::arch::process::{INITIAL_TID, MAX_PROCESS_COUNT};
/// A big unifying struct containing all of the system state. /// A big unifying struct containing all of the system state.
/// This is inherited from the stage 1 bootloader. /// This is inherited from the stage 1 bootloader.
pub struct SystemServices { pub struct SystemServices {
/// Current PID.
// pid: PID,
/// A table of all processes in the system /// A table of all processes in the system
pub processes: [Process; MAX_PROCESS_COUNT], pub processes: [Process; MAX_PROCESS_COUNT],
@ -218,6 +215,7 @@ impl Process {
} }
} }
#[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,
@ -234,6 +232,23 @@ thread_local!(static SYSTEM_SERVICES: RefCell<SystemServices> = RefCell::new(Sys
_syscall_depth: 0, _syscall_depth: 0,
})); }));
#[cfg(baremetal)]
static mut SYSTEM_SERVICES: SystemServices = 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,
};
impl core::fmt::Debug for Process { impl core::fmt::Debug for Process {
fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::result::Result<(), core::fmt::Error> { fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::result::Result<(), core::fmt::Error> {
write!( write!(
@ -253,9 +268,8 @@ impl SystemServices {
F: FnOnce(&SystemServices) -> R, F: FnOnce(&SystemServices) -> R,
{ {
#[cfg(baremetal)] #[cfg(baremetal)]
{ unsafe {
let ss = unsafe { SYSTEM_SERVICES }.borrow(); f(&SYSTEM_SERVICES)
f(&*ss)
} }
#[cfg(not(baremetal))] #[cfg(not(baremetal))]
SYSTEM_SERVICES.with(|ss| f(&ss.borrow())) SYSTEM_SERVICES.with(|ss| f(&ss.borrow()))
@ -266,9 +280,8 @@ impl SystemServices {
F: FnOnce(&mut SystemServices) -> R, F: FnOnce(&mut SystemServices) -> R,
{ {
#[cfg(baremetal)] #[cfg(baremetal)]
{ unsafe {
let mut ss = unsafe { SYSTEM_SERVICES }.borrow_mut(); f(&mut SYSTEM_SERVICES)
f(&mut *ss)
} }
#[cfg(not(baremetal))] #[cfg(not(baremetal))]
@ -781,8 +794,7 @@ 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::Setup(_) | ProcessState::Allocated => new_tid = INITIAL_TID,
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