mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-26 09:52:39 +00:00
Due to an initial misunderstanding of how the RISC-V is architected, contexts were going to be an emergent property of the kernel. However, after further consideration, contexts must now be baked into the kernel. Switching between contexts can be handled by the scheduler, but the kernel must keep track of processor registers. Keep track of context IDs and ensure they are sane. With this patch, interrupts work, as does basic context switching. Signed-off-by: Sean Cross <sean@xobs.io>
45 lines
1.1 KiB
Rust
45 lines
1.1 KiB
Rust
use crate::services::ProcessContext;
|
|
use riscv::register::{sepc, sstatus};
|
|
|
|
extern "C" {
|
|
fn _xous_resume_context(regs: *const usize) -> !;
|
|
}
|
|
|
|
pub fn invoke(
|
|
context: &mut ProcessContext,
|
|
supervisor: bool,
|
|
pc: usize,
|
|
sp: usize,
|
|
ret_addr: usize,
|
|
args: &[usize],
|
|
) {
|
|
set_supervisor(supervisor);
|
|
context.registers[0] = ret_addr;
|
|
context.registers[1] = sp;
|
|
context.registers[9] = args[0];
|
|
context.registers[10] = args[1];
|
|
context.sepc = pc;
|
|
}
|
|
|
|
fn set_supervisor(supervisor: bool) {
|
|
if supervisor {
|
|
unsafe { sstatus::set_spp(sstatus::SPP::Supervisor) };
|
|
} else {
|
|
unsafe { sstatus::set_spp(sstatus::SPP::User) };
|
|
}
|
|
}
|
|
|
|
pub fn resume(supervisor: bool, context: &ProcessContext) -> ! {
|
|
sepc::write(context.sepc);
|
|
|
|
// Return to the appropriate CPU mode
|
|
set_supervisor(supervisor);
|
|
|
|
println!(
|
|
"Switching to PID {}, SP: {:08x}, PC: {:08x}",
|
|
crate::arch::current_pid(),
|
|
context.registers[1],
|
|
context.sepc,
|
|
);
|
|
unsafe { _xous_resume_context(context.registers.as_ptr()) };
|
|
}
|