diff --git a/crates/libxernel/src/sync/spin.rs b/crates/libxernel/src/sync/spin.rs index afcd7c7a..eef54265 100644 --- a/crates/libxernel/src/sync/spin.rs +++ b/crates/libxernel/src/sync/spin.rs @@ -6,7 +6,7 @@ use core::{ }; use crate::{ - ipl::{raise_ipl, IPL}, + ipl::{raise_ipl, splx, IPL}, on_drop::OnDrop, }; @@ -82,7 +82,7 @@ impl Spinlock { pub fn aquire(&self) -> OnDrop, impl FnOnce()> { let ipl = raise_ipl(IPL::DPC); - let callback = move || write_cr8(ipl); + let callback = move || splx(ipl); OnDrop::new(self.lock(), callback) } diff --git a/xernel/kernel/src/arch/amd64/interrupts/mod.rs b/xernel/kernel/src/arch/amd64/interrupts/mod.rs index a5a1ce48..eccbd063 100644 --- a/xernel/kernel/src/arch/amd64/interrupts/mod.rs +++ b/xernel/kernel/src/arch/amd64/interrupts/mod.rs @@ -24,6 +24,7 @@ pub fn init() { handlers[0xE] = IRQHandler::Handler(page_fault_handler); handlers[0x8] = IRQHandler::Handler(double_fault_handler); handlers[0xF0] = IRQHandler::Handler(apic_spurious_interrupt); + // TODO: allocate vectors accordingly or manually set all known interrupt handlers here handlers[0x2f] = IRQHandler::Handler(dispatch_dpcs); handlers[0xd0] = IRQHandler::Handler(keyboard); } diff --git a/xernel/kernel/src/arch/amd64/tsc.rs b/xernel/kernel/src/arch/amd64/tsc.rs index d63ed417..73ececc2 100644 --- a/xernel/kernel/src/arch/amd64/tsc.rs +++ b/xernel/kernel/src/arch/amd64/tsc.rs @@ -7,6 +7,7 @@ use crate::acpi::hpet; pub static TSC_TICKS_PER_MS: AtomicU64 = AtomicU64::new(0); +// TODO: Use TSC Deadshot mode for apic pub fn calibrate_tsc() { let start: u64 = rdtsc(); hpet::sleep(10_000_000); diff --git a/xernel/kernel/src/cpu.rs b/xernel/kernel/src/cpu.rs index 42c8115f..649a0044 100644 --- a/xernel/kernel/src/cpu.rs +++ b/xernel/kernel/src/cpu.rs @@ -114,6 +114,8 @@ impl Deref for PerCpu { } } +// TODO: Replace RwLock with spinlock to use aquire functionality +// TODO: Create helper methods for cpu struct (add_dpc, add_timer, ...) #[repr(C, align(8))] pub struct Cpu { // NOTE: don't move these variables as we need to access them from assembly diff --git a/xernel/kernel/src/drivers/ps2/keyboard.rs b/xernel/kernel/src/drivers/ps2/keyboard.rs index 46252a74..9f146cd6 100644 --- a/xernel/kernel/src/drivers/ps2/keyboard.rs +++ b/xernel/kernel/src/drivers/ps2/keyboard.rs @@ -1,4 +1,7 @@ -use crate::{arch::amd64::{apic::APIC, ports::inb}, sched::context::TrapFrame}; +use crate::{ + arch::amd64::{apic::APIC, ports::inb}, + sched::context::TrapFrame, +}; pub fn keyboard(_: &mut TrapFrame) { dbg!("keyboard hit"); diff --git a/xernel/kernel/src/main.rs b/xernel/kernel/src/main.rs index 7ff5d166..f4dcd8ba 100644 --- a/xernel/kernel/src/main.rs +++ b/xernel/kernel/src/main.rs @@ -156,15 +156,16 @@ extern "C" fn kernel_main() -> ! { // FIXME: If used in code, code panics with error "virtual address must be sign extended in bits 48 to 64" let _user_task = Thread::new_user_thread(process.clone(), VirtAddr::new(0x200000)); - let page = FRAME_ALLOCATOR.lock().allocate_frame::().unwrap(); + let page = FRAME_ALLOCATOR.aquire().allocate_frame::().unwrap(); - KERNEL_PAGE_MAPPER.lock().map( + KERNEL_PAGE_MAPPER.aquire().map( page, Page::from_start_address(VirtAddr::new(0x200000)).unwrap(), PageTableFlags::WRITABLE | PageTableFlags::USER_ACCESSIBLE | PageTableFlags::PRESENT, true, ); + // FIXME: If aquire is used probably deadlock let mut process = process.lock(); let pm = process.get_page_table().as_mut().unwrap(); pm.map( @@ -192,6 +193,7 @@ extern "C" fn kernel_main() -> ! { let kernel_task2 = Thread::kernel_thread_from_fn(task2); + // TODO: use convenience functions current_cpu().run_queue.write().push_back(Arc::new(main_task)); current_cpu().run_queue.write().push_back(Arc::new(kernel_task)); current_cpu().run_queue.write().push_back(Arc::new(kernel_task2)); diff --git a/xernel/kernel/src/sched/context.rs b/xernel/kernel/src/sched/context.rs index 40b3ccbc..9524b473 100644 --- a/xernel/kernel/src/sched/context.rs +++ b/xernel/kernel/src/sched/context.rs @@ -1,7 +1,5 @@ use core::arch::asm; -use crate::arch::amd64::write_cr8; - #[derive(Debug, Clone, Copy, Default)] #[repr(C)] pub struct Context { @@ -85,6 +83,8 @@ impl TrapFrame { } } +// TODO: Maybe rework switching to new thread +// TODO: Move to switch.S since platform dependant #[naked] /// Restores the gives TrapFrame and jumps to new RIP via iretq /// Is used to startup a new thread when it's first executed diff --git a/xernel/kernel/src/timer/mod.rs b/xernel/kernel/src/timer/mod.rs index 823e5886..1a22d77e 100644 --- a/xernel/kernel/src/timer/mod.rs +++ b/xernel/kernel/src/timer/mod.rs @@ -14,10 +14,7 @@ use crate::amd64::interrupts::register_handler; use crate::amd64::tsc; use crate::apic::APIC; use crate::sched::context::TrapFrame; -use libxernel::{ - ipl::{get_ipl, IPL}, - sync::Once, -}; +use libxernel::{ipl::IPL, sync::Once}; static UPTIME: AtomicUsize = AtomicUsize::new(0); static TIMER_VECTOR: Once = Once::new(); @@ -51,6 +48,7 @@ pub fn timer_interrupt_handler(_frame: &mut TrapFrame) { if let Some(event) = next_event { APIC.oneshot(*TIMER_VECTOR, &event.deadline); + // TODO: Find a way to clone event if event.periodic { //timer_queue.queue_event(event.clone()); } @@ -70,5 +68,6 @@ pub fn hardclock(_: ()) { UPTIME.fetch_add(1, Ordering::SeqCst); let event = TimerEvent::new(hardclock, (), Duration::from_secs(1), false); + // TODO: use convenience functions current_cpu().timer_queue.write().enqueue(event); }