From 0b8cd65458a3f816c670c4a3eecdd595b33f0d25 Mon Sep 17 00:00:00 2001 From: greg Date: Thu, 18 Jul 2019 10:28:20 -0700 Subject: [PATCH] Handle timer interrupt --- src/interrupts.rs | 28 ++++++++++++++++++++++++++++ src/main.rs | 6 +++++- src/vga_buffer.rs | 5 ++++- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/interrupts.rs b/src/interrupts.rs index 991207c..5825b09 100644 --- a/src/interrupts.rs +++ b/src/interrupts.rs @@ -5,6 +5,22 @@ use pic8259_simple::ChainedPics; use spin; use crate::gdt; +#[derive(Copy, Clone, Debug)] +#[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) + } +} + + const PIC_1_OFFSET: u8 = 32; const PIC_2_OFFSET: u8 = PIC_1_OFFSET + 8; static PICS: spin::Mutex = @@ -18,10 +34,14 @@ lazy_static! { 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 }; } + + pub fn init_idt() { IDT.load(); } @@ -30,6 +50,7 @@ pub fn initalize_pics() { unsafe { PICS.lock().initialize(); } + x86_64::instructions::interrupts::enable(); } extern "x86-interrupt" fn breakpoint_handler(stack_frame: &mut InterruptStackFrame) { @@ -40,3 +61,10 @@ extern "x86-interrupt" fn breakpoint_handler(stack_frame: &mut InterruptStackFra extern "x86-interrupt" fn double_fault_handler(stack_frame: &mut InterruptStackFrame, code: u64) { panic!("EXCEPTION - DOUBLE FAULT (code {})\n{:#?}", code, stack_frame); } + +extern "x86-interrupt" fn timer_interrupt_handler(_stack_frame: &mut InterruptStackFrame) { + print!("."); + unsafe { + PICS.lock().notify_end_of_interrupt(InterruptIndex::Timer.as_u8()) + } +} diff --git a/src/main.rs b/src/main.rs index 459af83..11f953d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,6 +30,10 @@ pub extern "C" fn _start() -> ! { for i in 1..10 { println!("Gamarjoba, munde: {}", i); } - loop {} + loop { + for _ in 0..100_000 { + } + print!("-"); + } } diff --git a/src/vga_buffer.rs b/src/vga_buffer.rs index a9224e1..b4f21d6 100644 --- a/src/vga_buffer.rs +++ b/src/vga_buffer.rs @@ -18,7 +18,10 @@ macro_rules! println { #[doc(hidden)] pub fn _print(args: fmt::Arguments) { use core::fmt::Write; - WRITER.lock().write_fmt(args).unwrap(); + use x86_64::instructions::interrupts; + interrupts::without_interrupts(|| { + WRITER.lock().write_fmt(args).unwrap(); + }); } #[allow(dead_code)]