From 814d630e1c51ef72bdc25dc513d9e68ab34c18f3 Mon Sep 17 00:00:00 2001 From: corigan01 Date: Tue, 7 Jan 2025 20:44:53 -0600 Subject: [PATCH] Kernel: Interrupts working --- crates/arch/src/idt64.rs | 12 ++++++++---- kernel/src/idt.rs | 31 ++++++++++++++++++++++++++++++- kernel/src/main.rs | 19 ++----------------- 3 files changed, 40 insertions(+), 22 deletions(-) diff --git a/crates/arch/src/idt64.rs b/crates/arch/src/idt64.rs index 853377ef..dd79abab 100644 --- a/crates/arch/src/idt64.rs +++ b/crates/arch/src/idt64.rs @@ -40,7 +40,7 @@ pub enum GateKind { TrapGate, } -#[repr(C)] +#[repr(C, align(4096))] #[derive(Clone, Copy)] pub struct InterruptDescTable([GateDescriptor; 256]); @@ -53,7 +53,7 @@ impl InterruptDescTable { self.0[irq as usize] = gate; } - pub fn submit_table(&'static self) -> IdtPointer { + pub fn submit_table(&self) -> IdtPointer { IdtPointer { limit: 255, offset: self.0.as_ptr() as u64, @@ -61,8 +61,8 @@ impl InterruptDescTable { } } -#[repr(C)] -#[derive(Clone, Copy)] +#[repr(C, packed)] +#[derive(Clone, Copy, Debug)] pub struct IdtPointer { limit: u16, offset: u64, @@ -409,3 +409,7 @@ macro_rules! attach_irq { } }}; } + +pub fn fire_debug_int() { + unsafe { core::arch::asm!("int 0x01") }; +} diff --git a/kernel/src/idt.rs b/kernel/src/idt.rs index c3769027..2675d264 100644 --- a/kernel/src/idt.rs +++ b/kernel/src/idt.rs @@ -5,7 +5,7 @@ \___\_\_,_/\_,_/_//_/\__/\_,_/_/_/_/ /_/|_|\__/_/ /_//_/\__/_/ Part of the Quantum OS Kernel -Copyright 2024 Gavin Kellam +Copyright 2025 Gavin Kellam Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, @@ -22,3 +22,32 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FO DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ + +use arch::{ + attach_irq, + idt64::{ExceptionKind, InterruptDescTable, InterruptInfo, fire_debug_int, interrupt}, +}; +use lldebug::{logln, sync::Mutex}; + +static INTERRUPT_TABLE: Mutex = Mutex::new(InterruptDescTable::new()); + +#[interrupt(0..256)] +fn main_handler(args: InterruptInfo) { + logln!("Handler == {:#016x?}", args); + + if args.flags.exception_kind() == ExceptionKind::Abort { + panic!("Interrupt -- {:?}", args.flags); + } +} + +pub fn attach_interrupts() { + let mut idt = INTERRUPT_TABLE.lock(); + attach_irq!(idt, main_handler); + unsafe { idt.submit_table().load() }; + + logln!("Attached Interrupts!"); + + logln!("Checking Interrupts..."); + fire_debug_int(); + logln!("Interrupts Working!"); +} diff --git a/kernel/src/main.rs b/kernel/src/main.rs index 6f532dec..ec87ede4 100644 --- a/kernel/src/main.rs +++ b/kernel/src/main.rs @@ -28,12 +28,9 @@ OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWA #![feature(sync_unsafe_cell)] #![feature(abi_x86_interrupt)] +mod idt; mod panic; -use arch::{ - attach_irq, - idt64::{ExceptionKind, InterruptDescTable, InterruptInfo, interrupt}, -}; use bootloader::KernelBootHeader; use lldebug::{debug_ready, logln, make_debug}; use serial::{Serial, baud::SerialBaud}; @@ -50,15 +47,6 @@ extern "C" fn _start(kbh: u64) -> ! { panic!("Main should not return"); } -#[interrupt(0..256)] -fn main_handler(args: InterruptInfo) { - logln!("Handler == {:#?}", args); - - if args.flags.exception_kind() == ExceptionKind::Abort { - panic!("Interrupt -- {:?}", args.flags); - } -} - #[debug_ready] fn main(kbh: &KernelBootHeader) { logln!("Welcome to the Quantum Kernel!"); @@ -67,8 +55,5 @@ fn main(kbh: &KernelBootHeader) { HumanBytes::from(kbh.phys_mem_map.bytes_of(mem::phys::PhysMemoryKind::Free)) ); - let mut idt = InterruptDescTable::new(); - attach_irq!(idt, main_handler); - - logln!("Attached Interrupts!"); + idt::attach_interrupts(); }