2021-12-28 08:56:29 +00:00
|
|
|
use crate::{
|
|
|
|
arch::{drivers::vga::WRITER, gdt},
|
2022-01-16 15:23:19 +00:00
|
|
|
kernel_state::KERNEL_STATE,
|
2022-02-19 15:07:33 +00:00
|
|
|
print, println,
|
|
|
|
rhai_shell::KEYBUFF,
|
2021-12-28 08:56:29 +00:00
|
|
|
};
|
2022-01-22 07:26:25 +00:00
|
|
|
|
2022-02-23 00:15:16 +00:00
|
|
|
use cpuio::outb;
|
2021-11-16 06:09:27 +00:00
|
|
|
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;
|
|
|
|
pub static PICS: spin::Mutex<ChainedPics> =
|
|
|
|
spin::Mutex::new(unsafe { ChainedPics::new(PIC_1_OFFSET, PIC_2_OFFSET) });
|
|
|
|
|
|
|
|
/// Interrupt offsets.
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
#[repr(u8)]
|
|
|
|
pub enum InterruptIndex {
|
|
|
|
Timer = PIC_1_OFFSET,
|
|
|
|
Keyboard,
|
2022-02-23 00:15:16 +00:00
|
|
|
// SecondInterrupt = PIC_2_OFFSET,
|
|
|
|
Cmos = 0x70,
|
2021-11-16 06:09:27 +00:00
|
|
|
}
|
|
|
|
impl InterruptIndex {
|
|
|
|
fn as_u8(self) -> u8 {
|
|
|
|
self as u8
|
|
|
|
}
|
|
|
|
fn as_usize(self) -> usize {
|
|
|
|
usize::from(self.as_u8())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn init_idt() {
|
|
|
|
IDT.load();
|
|
|
|
}
|
|
|
|
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)
|
2022-02-23 00:15:16 +00:00
|
|
|
.set_stack_index(gdt::DOUBLE_FAULT_IST_INDEX);
|
2021-11-16 06:09:27 +00:00
|
|
|
}
|
2022-02-23 00:15:16 +00:00
|
|
|
|
|
|
|
// This gives fast interrupts
|
|
|
|
set_pit_frequency(1000);
|
|
|
|
|
2021-11-16 06:09:27 +00:00
|
|
|
idt[InterruptIndex::Timer.as_usize()].set_handler_fn(timer_interrupt_handler);
|
|
|
|
idt[InterruptIndex::Keyboard.as_usize()] .set_handler_fn(keyboard_interrupt_handler);
|
2022-02-23 00:15:16 +00:00
|
|
|
|
|
|
|
idt[6].set_handler_fn(floppy_disk_interrupt_handler);
|
|
|
|
|
|
|
|
|
2021-11-16 06:09:27 +00:00
|
|
|
idt
|
|
|
|
};
|
|
|
|
}
|
|
|
|
extern "x86-interrupt" fn breakpoint_handler(stack_frame: InterruptStackFrame) {
|
|
|
|
println!("EXCEPTION: BREAKPOINT\n{:#?}", stack_frame);
|
|
|
|
}
|
|
|
|
// new
|
|
|
|
extern "x86-interrupt" fn double_fault_handler(
|
|
|
|
stack_frame: InterruptStackFrame,
|
|
|
|
_error_code: u64,
|
|
|
|
) -> ! {
|
|
|
|
panic!("EXCEPTION: DOUBLE FAULT\n{:#?}", stack_frame);
|
|
|
|
}
|
|
|
|
extern "x86-interrupt" fn timer_interrupt_handler(_stack_frame: InterruptStackFrame) {
|
2022-02-28 14:54:41 +00:00
|
|
|
kernel::tick();
|
2021-11-16 06:09:27 +00:00
|
|
|
unsafe {
|
|
|
|
PICS.lock()
|
|
|
|
.notify_end_of_interrupt(InterruptIndex::Timer.as_u8());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: InterruptStackFrame) {
|
|
|
|
use crate::keyboard::{
|
2022-02-02 07:42:09 +00:00
|
|
|
CustomLayout, CustomScancodeSet, DecodedKey, DecodedKeyKind, HandleControl, KeyCode,
|
2021-11-16 06:09:27 +00:00
|
|
|
Keyboard,
|
|
|
|
};
|
|
|
|
use spin::Mutex;
|
|
|
|
use x86_64::instructions::port::Port;
|
|
|
|
lazy_static! {
|
2022-02-02 07:42:09 +00:00
|
|
|
static ref KEYBOARD: Mutex<Keyboard<CustomLayout, CustomScancodeSet>> =
|
2021-11-16 06:09:27 +00:00
|
|
|
Mutex::new(Keyboard::new(
|
|
|
|
CustomLayout::new_us104key(),
|
2022-02-02 07:42:09 +00:00
|
|
|
CustomScancodeSet::default(),
|
2021-11-16 06:09:27 +00:00
|
|
|
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 {
|
|
|
|
kind: DecodedKeyKind::Unicode,
|
|
|
|
value: character,
|
|
|
|
} => {
|
2021-12-28 08:56:29 +00:00
|
|
|
match character {
|
|
|
|
// Backspace
|
|
|
|
8 => {
|
|
|
|
WRITER.lock().backspace();
|
2022-02-18 18:12:07 +00:00
|
|
|
KEYBUFF.lock().push(8_u8 as _);
|
2022-01-16 15:23:19 +00:00
|
|
|
// print!(" ");
|
|
|
|
// WRITER.lock().backspace();
|
|
|
|
}
|
|
|
|
|
2022-01-28 13:40:47 +00:00
|
|
|
0x5E => KERNEL_STATE.lock().shutdown(),
|
|
|
|
|
2021-12-28 08:56:29 +00:00
|
|
|
_ => {
|
2022-02-17 10:05:56 +00:00
|
|
|
let mut buff = KEYBUFF.lock();
|
|
|
|
|
|
|
|
buff.push(character as u8 as char);
|
|
|
|
|
2021-12-28 08:56:29 +00:00
|
|
|
print!("{}", char::try_from(character).unwrap());
|
|
|
|
}
|
|
|
|
}
|
2021-11-16 06:09:27 +00:00
|
|
|
}
|
|
|
|
DecodedKey {
|
|
|
|
kind: DecodedKeyKind::RawKey,
|
|
|
|
value: key,
|
2022-01-13 14:54:33 +00:00
|
|
|
} => match KeyCode::from(key) {
|
|
|
|
KeyCode::AltLeft => {}
|
2022-01-28 13:40:47 +00:00
|
|
|
|
2022-01-26 01:40:37 +00:00
|
|
|
KeyCode::AltRight => {}
|
2022-01-13 14:54:33 +00:00
|
|
|
_ => {
|
2021-12-30 09:32:32 +00:00
|
|
|
print!("{:?}", KeyCode::from(key))
|
|
|
|
}
|
2022-01-13 14:54:33 +00:00
|
|
|
},
|
2022-02-01 21:27:40 +00:00
|
|
|
// Register a keyboard interrupt handler for the given scancode.
|
2021-11-16 06:09:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
PICS.lock()
|
|
|
|
.notify_end_of_interrupt(InterruptIndex::Keyboard.as_u8());
|
|
|
|
}
|
|
|
|
}
|
2021-11-27 20:59:29 +00:00
|
|
|
|
2022-02-23 00:15:16 +00:00
|
|
|
extern "x86-interrupt" fn floppy_disk_interrupt_handler(_stack_frame: InterruptStackFrame) {
|
|
|
|
println!("EXCEPTION: FLOPPY DISK");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_pit_frequency(freq: u32) {
|
|
|
|
let divisor: u16 = (1193180 / freq).try_into().unwrap();
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
outb(0x36, 0x43);
|
|
|
|
|
|
|
|
outb((divisor & 0xFF) as u8, 0x40);
|
|
|
|
outb((divisor >> 8) as u8, 0x40);
|
|
|
|
}
|
2021-11-16 06:09:27 +00:00
|
|
|
}
|