Skip to content

Commit

Permalink
Kernel: Interrupts working
Browse files Browse the repository at this point in the history
  • Loading branch information
corigan01 committed Jan 8, 2025
1 parent f1dcbcc commit 814d630
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 22 deletions.
12 changes: 8 additions & 4 deletions crates/arch/src/idt64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub enum GateKind {
TrapGate,
}

#[repr(C)]
#[repr(C, align(4096))]
#[derive(Clone, Copy)]
pub struct InterruptDescTable([GateDescriptor; 256]);

Expand All @@ -53,16 +53,16 @@ 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,
}
}
}

#[repr(C)]
#[derive(Clone, Copy)]
#[repr(C, packed)]
#[derive(Clone, Copy, Debug)]
pub struct IdtPointer {
limit: u16,
offset: u64,
Expand Down Expand Up @@ -409,3 +409,7 @@ macro_rules! attach_irq {
}
}};
}

pub fn fire_debug_int() {
unsafe { core::arch::asm!("int 0x01") };
}
31 changes: 30 additions & 1 deletion kernel/src/idt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<InterruptDescTable> = 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!");
}
19 changes: 2 additions & 17 deletions kernel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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!");
Expand All @@ -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();
}

0 comments on commit 814d630

Please sign in to comment.