99 lines
2.6 KiB
Rust
99 lines
2.6 KiB
Rust
use lazy_static::lazy_static;
|
|
use x86_64::structures::idt::{InterruptStackFrame, InterruptDescriptorTable};
|
|
use crate::println;
|
|
use pic8259_simple::ChainedPics;
|
|
use spin;
|
|
use crate::gdt;
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
#[repr(u8)]
|
|
pub enum InterruptIndex {
|
|
Timer = PIC_1_OFFSET,
|
|
Keyboard,
|
|
}
|
|
|
|
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<ChainedPics> =
|
|
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)
|
|
.set_stack_index(gdt::DOUBLE_FAULT_IST_INDEX);
|
|
}
|
|
idt[InterruptIndex::Timer.as_usize()]
|
|
.set_handler_fn(timer_interrupt_handler);
|
|
idt[InterruptIndex::Keyboard.as_usize()]
|
|
.set_handler_fn(keyboard_interrupt_handler);
|
|
idt
|
|
};
|
|
}
|
|
|
|
pub fn init_idt() {
|
|
IDT.load();
|
|
}
|
|
|
|
pub fn initalize_pics() {
|
|
unsafe {
|
|
PICS.lock().initialize();
|
|
}
|
|
x86_64::instructions::interrupts::enable();
|
|
}
|
|
|
|
extern "x86-interrupt" fn breakpoint_handler(stack_frame: &mut InterruptStackFrame) {
|
|
|
|
println!("EXCEPTION - BREAKPOINT\n{:#?}", stack_frame);
|
|
}
|
|
|
|
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())
|
|
}
|
|
}
|
|
|
|
extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: &mut InterruptStackFrame) {
|
|
use x86_64::instructions::port::Port;
|
|
use pc_keyboard::{Keyboard, ScancodeSet1, DecodedKey, HandleControl, layouts};
|
|
use spin::Mutex;
|
|
|
|
lazy_static! {
|
|
static ref KEYBOARD: Mutex<Keyboard<layouts::Us104Key, ScancodeSet1>> =
|
|
Mutex::new(Keyboard::new(layouts::Us104Key, ScancodeSet1, HandleControl::Ignore));
|
|
}
|
|
|
|
let mut keyboard = KEYBOARD.lock();
|
|
let mut port = Port::new(0x60);
|
|
let scancode: u8 = unsafe { port.read() };
|
|
|
|
if let Ok(Some(key_event)) = keyboard.add_byte(scancode) {
|
|
if let Some(key) = keyboard.process_keyevent(key_event) {
|
|
match key {
|
|
DecodedKey::Unicode(character) => print!("{}", character),
|
|
DecodedKey::RawKey(key) => print!("{:?}", key),
|
|
}
|
|
}
|
|
}
|
|
unsafe {
|
|
PICS.lock().notify_end_of_interrupt(InterruptIndex::Keyboard.as_u8())
|
|
}
|
|
}
|