Handle timer interrupt

This commit is contained in:
greg 2019-07-18 10:28:20 -07:00
parent 2627b216a1
commit 0b8cd65458
3 changed files with 37 additions and 2 deletions

View File

@ -5,6 +5,22 @@ use pic8259_simple::ChainedPics;
use spin; use spin;
use crate::gdt; 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_1_OFFSET: u8 = 32;
const PIC_2_OFFSET: u8 = PIC_1_OFFSET + 8; const PIC_2_OFFSET: u8 = PIC_1_OFFSET + 8;
static PICS: spin::Mutex<ChainedPics> = static PICS: spin::Mutex<ChainedPics> =
@ -18,10 +34,14 @@ lazy_static! {
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); .set_stack_index(gdt::DOUBLE_FAULT_IST_INDEX);
} }
idt[InterruptIndex::Timer.as_usize()]
.set_handler_fn(timer_interrupt_handler);
idt idt
}; };
} }
pub fn init_idt() { pub fn init_idt() {
IDT.load(); IDT.load();
} }
@ -30,6 +50,7 @@ pub fn initalize_pics() {
unsafe { unsafe {
PICS.lock().initialize(); PICS.lock().initialize();
} }
x86_64::instructions::interrupts::enable();
} }
extern "x86-interrupt" fn breakpoint_handler(stack_frame: &mut InterruptStackFrame) { 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) { extern "x86-interrupt" fn double_fault_handler(stack_frame: &mut InterruptStackFrame, code: u64) {
panic!("EXCEPTION - DOUBLE FAULT (code {})\n{:#?}", code, stack_frame); 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())
}
}

View File

@ -30,6 +30,10 @@ pub extern "C" fn _start() -> ! {
for i in 1..10 { for i in 1..10 {
println!("Gamarjoba, munde: {}", i); println!("Gamarjoba, munde: {}", i);
} }
loop {} loop {
for _ in 0..100_000 {
}
print!("-");
}
} }

View File

@ -18,7 +18,10 @@ macro_rules! println {
#[doc(hidden)] #[doc(hidden)]
pub fn _print(args: fmt::Arguments) { pub fn _print(args: fmt::Arguments) {
use core::fmt::Write; use core::fmt::Write;
use x86_64::instructions::interrupts;
interrupts::without_interrupts(|| {
WRITER.lock().write_fmt(args).unwrap(); WRITER.lock().write_fmt(args).unwrap();
});
} }
#[allow(dead_code)] #[allow(dead_code)]