mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
kernel: down to 12 errors
Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
814be7cd84
commit
e48e1820f6
3 changed files with 52 additions and 50 deletions
|
|
@ -5,6 +5,7 @@ use crate::arch::process::{Thread, RETURN_FROM_ISR};
|
||||||
use crate::mem::{MemoryManagerHandle, PAGE_SIZE};
|
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};
|
||||||
|
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) -> !;
|
||||||
|
|
@ -173,11 +174,10 @@ pub extern "C" fn trap_handler(
|
||||||
previous_pid, previous_context
|
previous_pid, previous_context
|
||||||
);
|
);
|
||||||
// Switch to the previous process' address space.
|
// Switch to the previous process' address space.
|
||||||
{
|
SystemServices::with_mut(|ss| {
|
||||||
let mut ss = SystemServicesHandle::get();
|
|
||||||
ss.finish_callback_and_resume(previous_pid, previous_context)
|
ss.finish_callback_and_resume(previous_pid, previous_context)
|
||||||
.expect("unable to resume previous PID");
|
.expect("unable to resume previous PID")
|
||||||
}
|
});
|
||||||
|
|
||||||
// Re-enable interrupts now that they're handled
|
// Re-enable interrupts now that they're handled
|
||||||
enable_all_irqs();
|
enable_all_irqs();
|
||||||
|
|
@ -197,9 +197,9 @@ pub extern "C" fn trap_handler(
|
||||||
// when this function runs.
|
// when this function runs.
|
||||||
unsafe {
|
unsafe {
|
||||||
if PREVIOUS_PAIR.is_none() {
|
if PREVIOUS_PAIR.is_none() {
|
||||||
let context_nr = SystemServicesHandle::get().current_context_nr();
|
let tid = crate::arch::process::current_tid();
|
||||||
PREVIOUS_PAIR = Some((pid, context_nr));
|
PREVIOUS_PAIR = Some((pid, tid));
|
||||||
println!("ISR: Setting previous pair to ({}, {})", pid, context_nr);
|
println!("ISR: Setting previous pair to ({}, {})", pid, tid);
|
||||||
} else {
|
} else {
|
||||||
println!("ISR: Previous pair is not None");
|
println!("ISR: Previous pair is not None");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use core::mem;
|
use core::mem;
|
||||||
use core::cell::RefCell;
|
use core::cell::RefCell;
|
||||||
static mut PROCESS: *mut Process = 0xff80_1000 as *mut Process;
|
static mut PROCESS: *mut ProcessImpl = 0xff80_1000 as *mut ProcessImpl;
|
||||||
pub const MAX_THREAD: TID = 31;
|
pub const MAX_THREAD: TID = 31;
|
||||||
pub const INITIAL_TID: TID = 1;
|
pub const INITIAL_TID: TID = 1;
|
||||||
use crate::arch::mem::PAGE_SIZE;
|
use crate::arch::mem::PAGE_SIZE;
|
||||||
|
|
@ -21,6 +21,9 @@ pub const IRQ_CONTEXT: usize = 1;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct ProcessImpl {
|
struct ProcessImpl {
|
||||||
|
/// Used by the interrupt handler to calculate offsets
|
||||||
|
scratch: usize,
|
||||||
|
|
||||||
/// Global parameters used by the operating system
|
/// Global parameters used by the operating system
|
||||||
pub inner: ProcessInner,
|
pub inner: ProcessInner,
|
||||||
|
|
||||||
|
|
@ -62,30 +65,16 @@ pub struct InitialProcess {
|
||||||
pub satp: usize,
|
pub satp: usize,
|
||||||
|
|
||||||
/// Where execution begins
|
/// Where execution begins
|
||||||
entrypoint: usize,
|
pub entrypoint: usize,
|
||||||
|
|
||||||
/// Address of the top of the stack
|
/// Address of the top of the stack
|
||||||
sp: usize,
|
pub sp: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Process {
|
pub struct Process {
|
||||||
/// Used by the interrupt handler to calculate offsets
|
pid: PID,
|
||||||
scratch: usize,
|
|
||||||
|
|
||||||
/// The index into the `contexts` list. This must never be 0. The interrupt
|
|
||||||
/// handler writes to this field, so it must not be moved.
|
|
||||||
thread_nr: TID,
|
|
||||||
|
|
||||||
_hash: [usize; 8],
|
|
||||||
|
|
||||||
/// Global parameters used by the operating system
|
|
||||||
pub inner: ProcessInner,
|
|
||||||
|
|
||||||
/// The interrupt handler will save the current process to this Thread when
|
|
||||||
/// the trap handler is entered.
|
|
||||||
contexts: [Thread; MAX_THREAD],
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
|
|
@ -103,6 +92,10 @@ pub struct Thread {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Process {
|
impl Process {
|
||||||
|
pub fn current() -> Process {
|
||||||
|
Process { pid: unsafe { PROCESS_TABLE.borrow().current } }
|
||||||
|
}
|
||||||
|
|
||||||
/// Calls the provided function with the current inner process state.
|
/// Calls the provided function with the current inner process state.
|
||||||
pub fn with_inner<F, R>(f: F) -> R
|
pub fn with_inner<F, R>(f: F) -> R
|
||||||
where
|
where
|
||||||
|
|
@ -123,8 +116,8 @@ impl Process {
|
||||||
where
|
where
|
||||||
F: FnOnce(&Process) -> R,
|
F: FnOnce(&Process) -> R,
|
||||||
{
|
{
|
||||||
let mut process = unsafe { &mut *PROCESS };
|
let process = Self::current();
|
||||||
f(process)
|
f(&process)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_inner_mut<F, R>(f: F) -> R
|
pub fn with_inner_mut<F, R>(f: F) -> R
|
||||||
|
|
@ -141,35 +134,41 @@ impl Process {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn current_thread(&mut self) -> &mut Thread {
|
pub fn current_thread(&mut self) -> &mut Thread {
|
||||||
assert!(self.thread_nr != 0, "thread number was 0");
|
let mut process = unsafe { &mut *PROCESS };
|
||||||
&mut self.contexts[self.thread_nr - 1]
|
assert!(process.current_thread != 0, "thread number was 0");
|
||||||
|
&mut process.threads[process.current_thread - 1]
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn current_tid(&self) -> TID {
|
pub fn current_tid(&self) -> TID {
|
||||||
self.thread_nr
|
let mut process = unsafe { &mut *PROCESS };
|
||||||
|
process.current_thread
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set the current thread number.
|
/// Set the current thread number.
|
||||||
pub fn set_thread(&mut self, thread: TID) {
|
pub fn set_thread(&mut self, thread: TID) -> Result<(), xous::Error> {
|
||||||
|
let mut process = unsafe { &mut *PROCESS };
|
||||||
assert!(
|
assert!(
|
||||||
thread > 0 && thread <= self.contexts.len(),
|
thread > 0 && thread <= process.threads.len(),
|
||||||
"attempt to switch to an invalid thread {}",
|
"attempt to switch to an invalid thread {}",
|
||||||
thread
|
thread
|
||||||
);
|
);
|
||||||
self.thread_nr = thread;
|
process.current_thread = thread;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn thread(&mut self, thread_nr: TID) -> &mut Thread {
|
pub fn thread(&mut self, thread: TID) -> &mut Thread {
|
||||||
|
let mut process = unsafe { &mut *PROCESS };
|
||||||
assert!(
|
assert!(
|
||||||
thread_nr > 0 && thread_nr <= self.contexts.len(),
|
thread > 0 && thread <= process.threads.len(),
|
||||||
"attempt to retrieve an invalid thread {}",
|
"attempt to retrieve an invalid thread {}",
|
||||||
thread_nr
|
thread
|
||||||
);
|
);
|
||||||
&mut self.contexts[thread_nr - 1]
|
&mut process.threads[thread - 1]
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn find_free_context_nr(&self) -> Option<TID> {
|
pub fn find_free_thread_nr(&self) -> Option<TID> {
|
||||||
for (index, thread) in self.contexts.iter().enumerate() {
|
let mut process = unsafe { &mut *PROCESS };
|
||||||
|
for (index, thread) in process.threads.iter().enumerate() {
|
||||||
if index != 0 && thread.sepc == 0 {
|
if index != 0 && thread.sepc == 0 {
|
||||||
return Some(index as TID + 1);
|
return Some(index as TID + 1);
|
||||||
}
|
}
|
||||||
|
|
@ -188,6 +187,7 @@ impl Process {
|
||||||
/// Initialize this process thread with the given entrypoint and stack
|
/// Initialize this process thread with the given entrypoint and stack
|
||||||
/// addresses.
|
/// addresses.
|
||||||
pub fn init(&mut self, entrypoint: usize, stack: usize, thread: usize) {
|
pub fn init(&mut self, entrypoint: usize, stack: usize, thread: usize) {
|
||||||
|
let mut process = unsafe { &mut *PROCESS };
|
||||||
assert!(
|
assert!(
|
||||||
mem::size_of::<Process>() == PAGE_SIZE,
|
mem::size_of::<Process>() == PAGE_SIZE,
|
||||||
"Process size is {}, not PAGE_SIZE ({})",
|
"Process size is {}, not PAGE_SIZE ({})",
|
||||||
|
|
@ -195,7 +195,7 @@ impl Process {
|
||||||
PAGE_SIZE
|
PAGE_SIZE
|
||||||
);
|
);
|
||||||
assert!(
|
assert!(
|
||||||
thread + 1 < self.contexts.len(),
|
thread + 1 < process.threads.len(),
|
||||||
"tried to init a thread that's out of range"
|
"tried to init a thread that's out of range"
|
||||||
);
|
);
|
||||||
assert!(thread != 1, "tried to init using the irq thread");
|
assert!(thread != 1, "tried to init using the irq thread");
|
||||||
|
|
@ -206,8 +206,8 @@ impl Process {
|
||||||
);
|
);
|
||||||
// By convention, thread 0 is the trap thread. Therefore, thread 1 is
|
// By convention, thread 0 is the trap thread. Therefore, thread 1 is
|
||||||
// the first default thread.
|
// the first default thread.
|
||||||
self.thread_nr = thread;
|
process.current_thread = thread;
|
||||||
for thread in self.contexts.iter_mut() {
|
for thread in process.threads.iter_mut() {
|
||||||
*thread = Default::default();
|
*thread = Default::default();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -217,7 +217,7 @@ impl Process {
|
||||||
thread.sepc = entrypoint;
|
thread.sepc = entrypoint;
|
||||||
thread.registers[1] = stack;
|
thread.registers[1] = stack;
|
||||||
|
|
||||||
self.inner = Default::default();
|
process.inner = Default::default();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -247,6 +247,9 @@ pub fn current_pid() -> PID {
|
||||||
unsafe { PROCESS_TABLE.borrow().current }
|
unsafe { PROCESS_TABLE.borrow().current }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn current_tid() -> TID {
|
||||||
|
unsafe { (*PROCESS).current_thread }
|
||||||
|
}
|
||||||
|
|
||||||
// pub struct ProcessHandle<'a> {
|
// pub struct ProcessHandle<'a> {
|
||||||
// process: &'a mut Process,
|
// process: &'a mut Process,
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,6 @@ macro_rules! thread_local {
|
||||||
use std::thread_local;
|
use std::thread_local;
|
||||||
|
|
||||||
use crate::filled_array;
|
use crate::filled_array;
|
||||||
use crate::mem::PAGE_SIZE;
|
|
||||||
use crate::server::Server;
|
use crate::server::Server;
|
||||||
// use core::mem;
|
// use core::mem;
|
||||||
use xous::{
|
use xous::{
|
||||||
|
|
@ -320,7 +319,7 @@ impl SystemServices {
|
||||||
// once a context switch _away_ from the kernel occurs, however we need
|
// once a context switch _away_ from the kernel occurs, however we need
|
||||||
// to make sure other fields such as "thread number" are all valid.
|
// to make sure other fields such as "thread number" are all valid.
|
||||||
// ProcessHandle::get().init(0, 0, INITIAL_TID);
|
// ProcessHandle::get().init(0, 0, INITIAL_TID);
|
||||||
self.processes[0].current_context = INITIAL_TID as u8;
|
// self.processes[0].current_thread = INITIAL_TID as u8;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Add a new entry to the process table. This results in a new address space
|
/// Add a new entry to the process table. This results in a new address space
|
||||||
|
|
@ -991,7 +990,7 @@ impl SystemServices {
|
||||||
// Locate an address to fit the new memory.
|
// Locate an address to fit the new memory.
|
||||||
dest_mapping.activate()?;
|
dest_mapping.activate()?;
|
||||||
let dest_virt = mm
|
let dest_virt = mm
|
||||||
.find_virtual_address(dest_virt, len, MemoryType::Messages)
|
.find_virtual_address(dest_virt, len, xous::MemoryType::Messages)
|
||||||
.or_else(|e| {
|
.or_else(|e| {
|
||||||
src_mapping.activate().expect("couldn't undo mapping");
|
src_mapping.activate().expect("couldn't undo mapping");
|
||||||
Err(e)
|
Err(e)
|
||||||
|
|
@ -1004,7 +1003,7 @@ impl SystemServices {
|
||||||
|
|
||||||
// Lend each subsequent page.
|
// Lend each subsequent page.
|
||||||
for offset in
|
for offset in
|
||||||
(0..(len / mem::size_of::<usize>())).step_by(PAGE_SIZE / mem::size_of::<usize>())
|
(0..(len / core::mem::size_of::<usize>())).step_by(crate::mem::PAGE_SIZE / core::mem::size_of::<usize>())
|
||||||
{
|
{
|
||||||
mm.move_page(
|
mm.move_page(
|
||||||
&src_mapping,
|
&src_mapping,
|
||||||
|
|
@ -1084,7 +1083,7 @@ impl SystemServices {
|
||||||
// Locate an address to fit the new memory.
|
// Locate an address to fit the new memory.
|
||||||
dest_mapping.activate();
|
dest_mapping.activate();
|
||||||
let dest_virt = mm
|
let dest_virt = mm
|
||||||
.find_virtual_address(dest_virt, len, MemoryType::Messages)
|
.find_virtual_address(dest_virt, len, xous::MemoryType::Messages)
|
||||||
.or_else(|e| {
|
.or_else(|e| {
|
||||||
src_mapping.activate();
|
src_mapping.activate();
|
||||||
Err(e)
|
Err(e)
|
||||||
|
|
@ -1095,7 +1094,7 @@ impl SystemServices {
|
||||||
|
|
||||||
// Lend each subsequent page.
|
// Lend each subsequent page.
|
||||||
for offset in
|
for offset in
|
||||||
(0..(len / mem::size_of::<usize>())).step_by(PAGE_SIZE / mem::size_of::<usize>())
|
(0..(len / core::mem::size_of::<usize>())).step_by(crate::mem::PAGE_SIZE / core::mem::size_of::<usize>())
|
||||||
{
|
{
|
||||||
mm.lend_page(
|
mm.lend_page(
|
||||||
&src_mapping,
|
&src_mapping,
|
||||||
|
|
@ -1165,7 +1164,7 @@ impl SystemServices {
|
||||||
|
|
||||||
// Lend each subsequent page.
|
// Lend each subsequent page.
|
||||||
for offset in
|
for offset in
|
||||||
(0..(len / mem::size_of::<usize>())).step_by(PAGE_SIZE / mem::size_of::<usize>())
|
(0..(len / core::mem::size_of::<usize>())).step_by(crate::mem::PAGE_SIZE / core::mem::size_of::<usize>())
|
||||||
{
|
{
|
||||||
mm.unlend_page(
|
mm.unlend_page(
|
||||||
&src_mapping,
|
&src_mapping,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue