Skip to main content

kernel/arch/x86_64/cpu/tables/
idt.rs

1// SPDX-License-Identifier: GPL-3.0-only
2//! Helper for building the IDT
3//!
4//! Authors: MarioS271
5
6use crate::kinfo;
7use x86_64::structures::idt::InterruptDescriptorTable;
8
9/// Build the IDT with all exception and IRQ handlers
10pub fn idt_init() -> InterruptDescriptorTable {
11    let mut idt = InterruptDescriptorTable::new();
12
13    use crate::arch::x86_64::interrupts::{exceptions, irqs};
14    use super::tss::{DEBUG_IST_STACK_INDEX, DOUBLE_FAULT_IST_STACK_INDEX, MACHINE_CHECK_IST_STACK_INDEX, NMI_IST_STACK_INDEX};
15
16    // Faults
17    idt.divide_error.set_handler_fn(exceptions::_0_divide_error::handler);
18    unsafe { idt.debug.set_handler_fn(exceptions::_1_debug::handler).set_stack_index(DEBUG_IST_STACK_INDEX as u16); }
19    unsafe { idt.non_maskable_interrupt.set_handler_fn(exceptions::_2_non_maskable_interrupt::handler).set_stack_index(NMI_IST_STACK_INDEX as u16); }
20    idt.breakpoint.set_handler_fn(exceptions::_3_breakpoint::handler);
21    idt.overflow.set_handler_fn(exceptions::_4_overflow::handler);
22    idt.bound_range_exceeded.set_handler_fn(exceptions::invalid_fault_handler::handler::<5>);
23    idt.invalid_opcode.set_handler_fn(exceptions::_6_invalid_opcode::handler);
24    idt.device_not_available.set_handler_fn(exceptions::_7_device_not_avail::handler);
25    unsafe { idt.double_fault.set_handler_fn(exceptions::_8_double_fault::handler).set_stack_index(DOUBLE_FAULT_IST_STACK_INDEX as u16); }
26    idt.invalid_tss.set_handler_fn(exceptions::_10_invalid_tss::handler);
27    idt.segment_not_present.set_handler_fn(exceptions::_11_segment_not_present::handler);
28    idt.stack_segment_fault.set_handler_fn(exceptions::_12_stack_segment_fault::handler);
29    idt.general_protection_fault.set_handler_fn(exceptions::_13_general_protection_fault::handler);
30    idt.page_fault.set_handler_fn(exceptions::_14_page_fault::handler);
31    idt.x87_floating_point.set_handler_fn(exceptions::_16_x87_floating_point::handler);
32    idt.alignment_check.set_handler_fn(exceptions::_17_alignment_check::handler);
33    unsafe { idt.machine_check.set_handler_fn(exceptions::_18_machine_check::handler).set_stack_index(MACHINE_CHECK_IST_STACK_INDEX as u16); }
34    idt.simd_floating_point.set_handler_fn(exceptions::_19_simd_floating_point::handler);
35    idt.virtualization.set_handler_fn(exceptions::_20_virtualization::handler);
36    idt.vmm_communication_exception.set_handler_fn(exceptions::_29_vmm_communication_exception::handler);
37    idt.security_exception.set_handler_fn(exceptions::_30_security_exception::handler);
38
39    // PIC IRQs
40    idt[39].set_handler_fn(irqs::pic::irq7_spurious::handler);
41
42    kinfo!("Initialized IDT");
43
44    idt
45}