From 618e528f35f8e16464d196e5273776bfaf2ba390 Mon Sep 17 00:00:00 2001 From: Julien CLEMENT Date: Sat, 4 Dec 2021 20:03:25 +0100 Subject: [PATCH] feat(pic): add pic init Signed-off-by: Julien CLEMENT --- Cargo.lock | 10 ++++++++++ Cargo.toml | 1 + src/gdt.rs | 17 +++++++++++++---- src/interrupts.rs | 37 +++++++++++++++++++++++++++++++++++-- src/lib.rs | 2 ++ 5 files changed, 61 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6605e31..6bba807 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -19,6 +19,7 @@ name = "julios" version = "0.1.0" dependencies = [ "lazy_static", + "pic8259", "spin", "volatile 0.2.7", "x86_64", @@ -33,6 +34,15 @@ dependencies = [ "spin", ] +[[package]] +name = "pic8259" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24ec21f514e2e16e94649f1d041ca4a7069b512c037ac156360652a775e6229d" +dependencies = [ + "x86_64", +] + [[package]] name = "spin" version = "0.5.2" diff --git a/Cargo.toml b/Cargo.toml index 1e66036..4dd8fb2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ crate-type = ["staticlib"] volatile = "0.2.6" spin = "0.5.2" x86_64 = "0.14.2" +pic8259 = "0.10.1" [dependencies.lazy_static] version = "1.0" diff --git a/src/gdt.rs b/src/gdt.rs index 7ab41cb..275f5d6 100644 --- a/src/gdt.rs +++ b/src/gdt.rs @@ -1,8 +1,11 @@ use crate::println; use lazy_static::lazy_static; -use x86_64::{VirtAddr, structures::tss::TaskStateSegment}; -use x86_64::structures::gdt::{GlobalDescriptorTable, Descriptor, SegmentSelector}; -use x86_64::instructions::{segmentation::{CS, Segment}, tables::load_tss}; +use x86_64::instructions::{ + segmentation::{Segment, CS}, + tables::load_tss, +}; +use x86_64::structures::gdt::{Descriptor, GlobalDescriptorTable, SegmentSelector}; +use x86_64::{structures::tss::TaskStateSegment, VirtAddr}; pub const DOUBLE_FAULT_IST_INDEX: u16 = 0; @@ -26,7 +29,13 @@ lazy_static! { let mut gdt = GlobalDescriptorTable::new(); let code_selector = gdt.add_entry(Descriptor::kernel_code_segment()); let tss_selector = gdt.add_entry(Descriptor::tss_segment(&TSS)); - (gdt, Selectors { code_selector, tss_selector }) + ( + gdt, + Selectors { + code_selector, + tss_selector, + }, + ) }; } diff --git a/src/interrupts.rs b/src/interrupts.rs index ee67b4b..ac58e4b 100644 --- a/src/interrupts.rs +++ b/src/interrupts.rs @@ -1,17 +1,44 @@ use crate::gdt; -use crate::println; +use crate::{println, print}; use crate::vga::{self, Color, ColorCode}; use lazy_static::lazy_static; +use pic8259::ChainedPics; +use spin; use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame}; +pub const PIC_1_OFFSET: u8 = 32; +pub const PIC_2_OFFSET: u8 = PIC_1_OFFSET + 8; + +#[derive(Debug, Clone, Copy)] +#[repr(u8)] +pub enum InterruptIndex { + Timer = PIC_1_OFFSET, +} + +impl InterruptIndex { + fn as_u8(self) -> u8 { + self as u8 + } + + fn as_usize(self) -> usize { + usize::from(self.as_u8()) + } +} + +pub static PICS: spin::Mutex = + spin::Mutex::new(unsafe { ChainedPics::new(PIC_1_OFFSET, PIC_2_OFFSET) }); + lazy_static! { static ref IDT: InterruptDescriptorTable = { let mut idt = InterruptDescriptorTable::new(); idt.breakpoint.set_handler_fn(breakpoint_handler); unsafe { - idt.double_fault.set_handler_fn(double_fault_handler) + idt.double_fault + .set_handler_fn(double_fault_handler) .set_stack_index(gdt::DOUBLE_FAULT_IST_INDEX); } + idt[InterruptIndex::Timer.as_usize()] + .set_handler_fn(timer_interrupt_handler); idt }; } @@ -34,3 +61,9 @@ extern "x86-interrupt" fn double_fault_handler( ) -> ! { panic!("EXCEPTION: DOUBLE FAULT\n{:#?}", stack_frame); } + +extern "x86-interrupt" fn timer_interrupt_handler( + _stack_frame: InterruptStackFrame) +{ + print!("."); +} diff --git a/src/lib.rs b/src/lib.rs index 3bff3d0..e7166c1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,6 +22,8 @@ pub fn init() { println!("Starting init"); gdt::init_gdt(); interrupts::init_idt(); + unsafe { interrupts::PICS.lock().initialize() }; + x86_64::instructions::interrupts::enable(); vga::change_color(ColorCode::new(Color::LightGreen, Color::Black)); }