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-02-23 00:15:16 +00:00
|
|
|
use cpuio::outb;
|
2021-11-16 06:09:27 +00:00
|
|
|
use pic8259::ChainedPics;
|
2022-04-11 20:51:54 +00:00
|
|
|
use spin::Lazy;
|
2021-11-16 06:09:27 +00:00
|
|
|
use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame};
|
2022-04-11 22:23:11 +00:00
|
|
|
|
2021-11-16 06:09:27 +00:00
|
|
|
pub const PIC_1_OFFSET: u8 = 32;
|
|
|
|
pub const PIC_2_OFFSET: u8 = PIC_1_OFFSET + 8;
|
2022-04-11 22:23:11 +00:00
|
|
|
|
2021-11-16 06:09:27 +00:00
|
|
|
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
|
|
|
}
|
2022-04-11 22:23:11 +00:00
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
2022-02-23 00:15:16 +00:00
|
|
|
|
2022-04-11 20:51:54 +00:00
|
|
|
static IDT: Lazy<InterruptDescriptorTable> = Lazy::new(|| {
|
|
|
|
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);
|
|
|
|
}
|
2022-02-23 00:15:16 +00:00
|
|
|
|
2022-04-25 18:39:39 +00:00
|
|
|
reset_pit_for_cpu();
|
2022-02-23 00:15:16 +00:00
|
|
|
|
2022-04-11 20:51:54 +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
|
|
|
|
2022-04-11 20:51:54 +00:00
|
|
|
idt[6].set_handler_fn(floppy_disk_interrupt_handler);
|
2022-02-23 00:15:16 +00:00
|
|
|
|
2022-04-11 20:51:54 +00:00
|
|
|
idt
|
|
|
|
});
|
2022-04-11 18:53:33 +00:00
|
|
|
|
2021-11-16 06:09:27 +00:00
|
|
|
extern "x86-interrupt" fn breakpoint_handler(stack_frame: InterruptStackFrame) {
|
|
|
|
println!("EXCEPTION: BREAKPOINT\n{:#?}", stack_frame);
|
|
|
|
}
|
2022-04-11 22:23:11 +00:00
|
|
|
|
2021-11-16 06:09:27 +00:00
|
|
|
extern "x86-interrupt" fn double_fault_handler(
|
|
|
|
stack_frame: InterruptStackFrame,
|
2022-04-11 18:53:33 +00:00
|
|
|
error_code: u64,
|
2021-11-16 06:09:27 +00:00
|
|
|
) -> ! {
|
2022-04-11 18:53:33 +00:00
|
|
|
panic!(
|
|
|
|
"EXCEPTION: Error code{}\nDOUBLE FAULT\n{:#?}",
|
|
|
|
error_code, stack_frame
|
|
|
|
);
|
2021-11-16 06:09:27 +00:00
|
|
|
}
|
2022-04-11 22:23:11 +00:00
|
|
|
|
2022-05-20 15:11:32 +00:00
|
|
|
#[naked]
|
2021-11-16 06:09:27 +00:00
|
|
|
extern "x86-interrupt" fn timer_interrupt_handler(_stack_frame: InterruptStackFrame) {
|
2022-05-20 15:11:32 +00:00
|
|
|
use super::task_switcher;
|
|
|
|
|
2021-11-16 06:09:27 +00:00
|
|
|
unsafe {
|
2022-05-20 15:11:32 +00:00
|
|
|
asm!(
|
|
|
|
// Kernel tick
|
|
|
|
"call {tick}",
|
|
|
|
|
|
|
|
// Push task's state onto stack
|
|
|
|
// and save task pointer into scheduler
|
|
|
|
task_switcher::save_tasks_state!(),
|
|
|
|
"mov rdi, rsp",
|
|
|
|
"call {save}",
|
|
|
|
|
|
|
|
// Switch to next task (interrupt'll be returned there)
|
|
|
|
"jmp {switch_to_next}",
|
|
|
|
|
|
|
|
tick = sym kernel::tick,
|
|
|
|
save = sym task_switcher::save_and_enqueue,
|
|
|
|
switch_to_next = sym task_switcher::switch_to_next,
|
|
|
|
options(noreturn),
|
|
|
|
);
|
2021-11-16 06:09:27 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-11 22:23:11 +00:00
|
|
|
|
2021-11-16 06:09:27 +00:00
|
|
|
extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: InterruptStackFrame) {
|
2022-06-14 15:27:30 +00:00
|
|
|
use pc_keyboard::{
|
|
|
|
layouts::Us104Key, DecodedKey, HandleControl, KeyCode, Keyboard, ScancodeSet1,
|
2021-11-16 06:09:27 +00:00
|
|
|
};
|
|
|
|
use spin::Mutex;
|
|
|
|
use x86_64::instructions::port::Port;
|
2022-04-11 20:51:54 +00:00
|
|
|
|
2022-06-14 15:27:30 +00:00
|
|
|
static KEYBOARD: Lazy<Mutex<Keyboard<Us104Key, ScancodeSet1>>> =
|
|
|
|
Lazy::new(|| Mutex::new(Keyboard::new(Us104Key, ScancodeSet1, HandleControl::Ignore)));
|
2022-04-11 20:51:54 +00:00
|
|
|
|
2021-11-16 06:09:27 +00:00
|
|
|
let mut keyboard = KEYBOARD.lock();
|
2022-06-14 15:27:30 +00:00
|
|
|
if let Ok(Some(key)) = keyboard
|
|
|
|
.add_byte(unsafe { Port::new(0x60).read() })
|
|
|
|
.map(|x| x.and_then(|ev| keyboard.process_keyevent(ev)))
|
|
|
|
{
|
|
|
|
match key {
|
|
|
|
DecodedKey::Unicode(chr) => match chr {
|
|
|
|
// Backspace
|
|
|
|
'\u{8}' => {
|
|
|
|
WRITER.lock().backspace();
|
|
|
|
KEYBUFF.lock().push(8.into());
|
2021-11-16 06:09:27 +00:00
|
|
|
}
|
2022-06-14 15:27:30 +00:00
|
|
|
'^' => KERNEL_STATE.lock().shutdown(),
|
|
|
|
chr => {
|
|
|
|
KEYBUFF.lock().push(chr);
|
|
|
|
print!("{chr}");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
DecodedKey::RawKey(key) => match KeyCode::from(key) {
|
|
|
|
KeyCode::AltLeft | KeyCode::AltRight => (),
|
|
|
|
kc => print!("{kc:?}"),
|
|
|
|
},
|
2021-11-16 06:09:27 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-14 15:27:30 +00:00
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2022-04-11 22:23:11 +00:00
|
|
|
pub fn init_idt() {
|
|
|
|
IDT.load();
|
|
|
|
}
|
|
|
|
|
2022-04-25 18:39:39 +00:00
|
|
|
pub fn set_pit_frequency(pit: u16, freq: u32) {
|
2022-02-23 00:15:16 +00:00
|
|
|
let divisor: u16 = (1193180 / freq).try_into().unwrap();
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
outb(0x36, 0x43);
|
|
|
|
|
2022-04-25 18:39:39 +00:00
|
|
|
outb((divisor & 0xFF) as u8, 0x39 + pit);
|
|
|
|
outb((divisor >> 8) as u8, 0x40 + pit);
|
2022-02-23 00:15:16 +00:00
|
|
|
}
|
2021-11-16 06:09:27 +00:00
|
|
|
}
|
2022-04-25 18:39:39 +00:00
|
|
|
pub fn set_pit_1(freq: u32) {
|
|
|
|
set_pit_frequency(1, freq);
|
|
|
|
}
|
|
|
|
pub fn set_pit_2(freq: u32) {
|
|
|
|
set_pit_frequency(2, freq);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_pit_3(freq: u32) {
|
|
|
|
set_pit_frequency(3, freq);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn reset_pit_for_cpu() {
|
|
|
|
set_pit_1(1000);
|
|
|
|
set_pit_2(1000);
|
|
|
|
set_pit_3(1000);
|
|
|
|
}
|