Skip to content

Commit

Permalink
refactor(kernel): change idt module structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Hqnnqh committed Jan 11, 2025
1 parent 74f8948 commit 7e33e23
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 73 deletions.
6 changes: 6 additions & 0 deletions kernel/src/idt/dispatch/handler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use crate::{assign_isr, idt::descriptor::GateType};

// Interrupt Service Routines
assign_isr!(
0, GateType::TrapGate
3, GateType::TrapGate);
75 changes: 3 additions & 72 deletions kernel/src/idt/handler.rs → kernel/src/idt/dispatch/macros.rs
Original file line number Diff line number Diff line change
@@ -1,72 +1,3 @@
use core::arch::naked_asm;

use framebuffer::color;

use crate::{assign_isr, println};

use super::{descriptor::GateType, InterruptDescriptorTable};
use hal::cpu_state::CpuState;

assign_isr!(
0, GateType::TrapGate
3, GateType::TrapGate);

fn dispatch(state: &CpuState) -> &CpuState {
println!(color::INFO, "Hello DEBUG Interrupt!");
state
}

#[naked]
extern "C" fn interrupt_stub() {
unsafe {
naked_asm!(
"push rax",
"push rbx",
"push rcx",
"push rdx",
"push rsi",
"push rdi",
"push rbp",
"push r8",
"push r9",
"push r10",
"push r11",
"push r12",
"push r13",
"push r14",
"push r15",
// pass rsp to the dispatch handler (stack pointer)
"mov rdi, rsp",
"call {interrupt_dispatch}",

// restore the stack pointer returned by the dispatch handler
"mov rsp, rax",

"pop r15",
"pop r14",
"pop r13",
"pop r12",
"pop r11",
"pop r10",
"pop r9",
"pop r8",
"pop rbp",
"pop rdi",
"pop rsi",
"pop rdx",
"pop rcx",
"pop rbx",
"pop rax",
// remove vector number + error code (16 bytes)
"add rsp, 16",
"iretq",
interrupt_dispatch = sym dispatch
);
}
}

// Interrupt Service Routines

/// Declare an Inetrrupt Service Routine
#[macro_export]
macro_rules! declare_isr {
Expand All @@ -83,7 +14,7 @@ macro_rules! declare_isr {
"push {isr_number}",
"jmp {interrupt_stub}",
isr_number = const $isr_number,
interrupt_stub = sym interrupt_stub,
interrupt_stub = sym $crate::idt::dispatch::interrupt_stub,
);
}
}
Expand Down Expand Up @@ -118,8 +49,8 @@ macro_rules! assign_isr {
$crate::declare_isr!($($error)? $isr_number);
)*

impl InterruptDescriptorTable {
pub(super) fn assign_handlers(&mut self) {
impl $crate::idt::InterruptDescriptorTable {
pub(in $crate::idt) fn assign_handlers(&mut self) {
$(
self.set_handler(
$isr_number,
Expand Down
63 changes: 63 additions & 0 deletions kernel/src/idt/dispatch/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use core::arch::naked_asm;

use framebuffer::color;
use hal::cpu_state::CpuState;

use crate::println;

pub(super) mod handler;
pub(super) mod macros;

fn dispatch(state: &CpuState) -> &CpuState {
println!(color::INFO, "Hello DEBUG Interrupt!");
state
}

#[naked]
extern "C" fn interrupt_stub() {
unsafe {
naked_asm!(
"push rax",
"push rbx",
"push rcx",
"push rdx",
"push rsi",
"push rdi",
"push rbp",
"push r8",
"push r9",
"push r10",
"push r11",
"push r12",
"push r13",
"push r14",
"push r15",
// pass rsp to the dispatch handler (stack pointer)
"mov rdi, rsp",
"call {interrupt_dispatch}",

// restore the stack pointer returned by the dispatch handler
"mov rsp, rax",

"pop r15",
"pop r14",
"pop r13",
"pop r12",
"pop r11",
"pop r10",
"pop r9",
"pop r8",
"pop rbp",
"pop rdi",
"pop rsi",
"pop rdx",
"pop rcx",
"pop rbx",
"pop rax",
// remove vector number + error code (16 bytes)
"add rsp, 16",
"iretq",
interrupt_dispatch = sym dispatch
);
}
}
2 changes: 1 addition & 1 deletion kernel/src/idt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use sync::spin::SpinLock;
use crate::gdt::KERNEL_CS;

mod descriptor;
mod handler;
mod dispatch;

const IDT_MAX_DESCRIPTORS: usize = 256;

Expand Down

0 comments on commit 7e33e23

Please sign in to comment.