mirror of
https://github.com/panpaul/tiny_os
synced 2024-09-20 09:45:19 +08:00
feat: template for scheduler
This commit is contained in:
parent
c67a771500
commit
ea80bd3e8d
1
.vscode/settings.json
vendored
1
.vscode/settings.json
vendored
@ -11,4 +11,5 @@
|
|||||||
"rust-analyzer.linkedProjects": [
|
"rust-analyzer.linkedProjects": [
|
||||||
"./kernel/Cargo.toml"
|
"./kernel/Cargo.toml"
|
||||||
],
|
],
|
||||||
|
"rust-analyzer.showUnlinkedFileNotification": false,
|
||||||
}
|
}
|
@ -1,11 +1,11 @@
|
|||||||
use crate::entry::HART_ID;
|
use crate::entry::HART_ID;
|
||||||
use crate::plat::console::CONSOLE;
|
use crate::plat::console::CONSOLE;
|
||||||
use log::trace;
|
|
||||||
use riscv::register::scause::{Interrupt as I, Trap as T};
|
|
||||||
use riscv::register::stvec::TrapMode;
|
|
||||||
|
|
||||||
use crate::plat::timer::{Timer, TimerOps};
|
use crate::plat::timer::{Timer, TimerOps};
|
||||||
use crate::plat::trap::{Trap, TrapContextOps, TrapOps};
|
use crate::plat::trap::{Trap, TrapContextOps, TrapOps};
|
||||||
|
use crate::scheduler::SCHEDULER;
|
||||||
|
use log::trace;
|
||||||
|
use riscv::register::scause::{Exception as E, Interrupt as I, Trap as T};
|
||||||
|
use riscv::register::stvec::TrapMode;
|
||||||
|
|
||||||
core::arch::global_asm!(include_str!("./asm/trap64.S"));
|
core::arch::global_asm!(include_str!("./asm/trap64.S"));
|
||||||
core::arch::global_asm!(include_str!("./asm/trap_common.S"));
|
core::arch::global_asm!(include_str!("./asm/trap_common.S"));
|
||||||
@ -17,29 +17,52 @@ extern "C" {
|
|||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
extern "C" fn kernel_trap_handler(tf: &mut TrapContext) {
|
extern "C" fn kernel_trap_handler(tf: &mut TrapContext) {
|
||||||
// TODO: consider redesigning this, should we allow trap in kernel?
|
// TODO: Replace CONSOLE Mutex to ReentrantLock
|
||||||
unsafe { CONSOLE.force_unlock() }
|
unsafe { CONSOLE.force_unlock() }
|
||||||
trap_handler(tf, true)
|
trap_handler(tf, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn trap_handler(_tf: &mut TrapContext, _from_kernel: bool) {
|
fn trap_handler(tf: &mut TrapContext, from_kernel: bool) {
|
||||||
let scause = riscv::register::scause::read();
|
let scause = riscv::register::scause::read();
|
||||||
trace!("[Interrupt] cpu@{} scause: {:?}", HART_ID.get(), scause.cause());
|
trace!("[Trap] cpu@{} scause: {:?}", HART_ID.get(), scause.cause());
|
||||||
|
|
||||||
|
macro_rules! panic_fatal {
|
||||||
|
($msg:expr) => {
|
||||||
|
panic!(
|
||||||
|
"[Trap] {}, scause: {:?}, bits: {:#x}, sepc: {:#x}, stval: {:#x}",
|
||||||
|
$msg,
|
||||||
|
scause.cause(),
|
||||||
|
scause.bits(),
|
||||||
|
tf.sepc,
|
||||||
|
riscv::register::stval::read()
|
||||||
|
)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
match scause.cause() {
|
match scause.cause() {
|
||||||
T::Interrupt(I::SupervisorTimer) => {
|
T::Interrupt(I::SupervisorTimer) => {
|
||||||
// TODO: refactor this
|
if !from_kernel {
|
||||||
trace!("[Interrupt] Tick: {}", riscv::register::time::read64());
|
SCHEDULER.schedule()
|
||||||
Timer::tick();
|
}
|
||||||
},
|
},
|
||||||
_ => {
|
T::Exception(E::UserEnvCall) => {
|
||||||
panic!(
|
// TODO: handle syscall
|
||||||
"[Interrupt] Unhandled Trap, scause: {:?}, bits: {:#x}",
|
tf.sepc += 4;
|
||||||
scause.cause(),
|
|
||||||
scause.bits()
|
|
||||||
);
|
|
||||||
},
|
},
|
||||||
|
T::Interrupt(I::SupervisorExternal) => {
|
||||||
|
// TODO: handle external interrupt, e.g. plic
|
||||||
|
},
|
||||||
|
T::Exception(E::InstructionPageFault) | T::Exception(E::LoadPageFault) | T::Exception(E::StorePageFault) => {
|
||||||
|
if from_kernel {
|
||||||
|
panic_fatal!("Page Fault in Kernel");
|
||||||
|
}
|
||||||
|
// TODO: handle page fault
|
||||||
|
},
|
||||||
|
_ => panic_fatal!("Unhandled Trap"),
|
||||||
}
|
}
|
||||||
trace!("[Interrupt] exiting...");
|
|
||||||
|
Timer::tick();
|
||||||
|
trace!("[Trap] exiting...");
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TrapOps for Trap {
|
impl TrapOps for Trap {
|
||||||
|
@ -29,6 +29,7 @@ mod lang;
|
|||||||
mod logging;
|
mod logging;
|
||||||
mod objects;
|
mod objects;
|
||||||
mod plat;
|
mod plat;
|
||||||
|
mod scheduler;
|
||||||
|
|
||||||
// test infrastructure
|
// test infrastructure
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
15
kernel/src/scheduler/mod.rs
Normal file
15
kernel/src/scheduler/mod.rs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
pub static SCHEDULER: Scheduler = Scheduler::new();
|
||||||
|
|
||||||
|
pub struct Scheduler {}
|
||||||
|
|
||||||
|
impl Scheduler {
|
||||||
|
pub const fn new() -> Self {
|
||||||
|
// TODO: implement Scheduler::new
|
||||||
|
Self {}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline(never)]
|
||||||
|
pub fn schedule(&self) {
|
||||||
|
todo!("Scheduler::schedule");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user