ableos/ableos/src/arch/x86_64/interrupts.rs

293 lines
7.5 KiB
Rust
Raw Normal View History

2022-07-29 17:48:45 +00:00
use core::panic::PanicInfo;
2021-12-28 08:56:29 +00:00
use crate::{
arch::{drivers::vga::WRITER, gdt},
2022-07-29 14:46:09 +00:00
image::mono_bitmap::bruh,
kernel_state::KERNEL_STATE,
2022-02-19 15:07:33 +00:00
print, println,
rhai_shell::KEYBUFF,
2022-07-29 14:46:09 +00:00
VgaBuffer, SCREEN_BUFFER,
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-07-29 14:46:09 +00:00
use qrcode::{
render::{string, unicode},
QrCode,
};
use spin::Lazy;
2022-07-29 14:46:09 +00:00
use vga::colors::Color16;
2021-11-16 06:09:27 +00:00
use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame};
2022-04-11 22:23:11 +00:00
2022-07-31 06:54:01 +00:00
use super::sloop;
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-07-31 06:54:01 +00:00
/// Mouse offset
Mouse = 44,
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
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
idt[InterruptIndex::Timer.as_usize()].set_handler_fn(timer_interrupt_handler);
idt[InterruptIndex::Keyboard.as_usize()].set_handler_fn(keyboard_interrupt_handler);
2022-07-31 06:54:01 +00:00
idt[InterruptIndex::Mouse.as_usize()].set_handler_fn(crate::hardware::mouse_interrupt_handler);
2022-02-23 00:15:16 +00:00
2022-07-29 16:51:54 +00:00
// run `a + b + l + e + o + s print;` in ablescript and its 54 thats why this seemingly arbitrary number was chosen
idt[54].set_handler_fn(software_int_handler);
2022-07-31 06:54:01 +00:00
idt
});
2022-04-11 18:53:33 +00:00
2022-07-29 16:51:54 +00:00
extern "x86-interrupt" fn software_int_handler(stack_frame: InterruptStackFrame) {
println!("EXCEPTION: SOFTWARE INT\n{:#?}", stack_frame);
}
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-07-29 16:51:54 +00:00
// NOTE(able): ignore this always is 0
_error_code: u64,
2021-11-16 06:09:27 +00:00
) -> ! {
2022-07-29 17:48:45 +00:00
bsod(BSODSource::DoubleFault(&stack_frame));
2022-07-31 06:54:01 +00:00
panic!("EXCEPTION: DOUBLE FAULT\n{:#?}", 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-07-29 11:13:26 +00:00
// print!(".");
2022-05-20 15:11:32 +00:00
asm!(
2022-07-31 06:54:01 +00:00
2022-05-20 15:11:32 +00:00
// 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}",
2022-07-31 06:54:01 +00:00
tick = sym crate::kmain::tick,
2022-05-20 15:11:32 +00:00
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) {
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;
static KEYBOARD: Lazy<Mutex<Keyboard<Us104Key, ScancodeSet1>>> =
Lazy::new(|| Mutex::new(Keyboard::new(Us104Key, ScancodeSet1, HandleControl::Ignore)));
2021-11-16 06:09:27 +00:00
let mut keyboard = KEYBOARD.lock();
if let Ok(Some(key)) = keyboard
.add_byte(unsafe { Port::new(0x60).read() })
.map(|x| x.and_then(|ev| keyboard.process_keyevent(ev)))
{
2022-07-31 06:54:01 +00:00
trace!("{key:?}");
match key {
DecodedKey::Unicode(chr) => match chr {
// Backspace
'\u{8}' => {
2022-07-31 06:54:01 +00:00
// TODO: Fix this and apply to new term
WRITER.lock().backspace();
KEYBUFF.lock().push(8.into());
2021-11-16 06:09:27 +00:00
}
2022-07-31 06:54:01 +00:00
// '^' => KERNEL_STATE.lock().shutdown(),
chr => {
KEYBUFF.lock().push(chr);
print!("{chr}");
}
},
2022-07-29 11:13:26 +00:00
DecodedKey::RawKey(key) => {
use KeyCode::*;
match KeyCode::from(key) {
AltLeft | AltRight => (),
ArrowDown | ArrowRight | ArrowLeft | ArrowUp => {
warn!("ArrowKeys are unsupported currently");
}
kc => print!("{kc:?}"),
};
}
2021-11-16 06:09:27 +00:00
}
}
2021-11-16 06:09:27 +00:00
unsafe {
PICS.lock()
.notify_end_of_interrupt(InterruptIndex::Keyboard.as_u8());
}
}
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-07-29 11:13:26 +00:00
let ret = (1193180 / freq).try_into();
let divisor: u16 = match ret {
Ok(div) => div,
Err(err) => {
error!("{}", err);
warn!("Defaulting to 1000 on PIT{}", pit);
1000
}
};
2022-02-23 00:15:16 +00:00
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() {
2022-07-29 11:13:26 +00:00
set_pit_1(50);
2022-04-25 18:39:39 +00:00
set_pit_2(1000);
set_pit_3(1000);
}
2022-07-29 17:48:45 +00:00
pub fn bsod(src: BSODSource) -> ! {
2022-07-31 06:54:01 +00:00
trace!("{src:?}");
2022-07-29 14:46:09 +00:00
let mut mode = SCREEN_BUFFER.lock();
mode.force_redraw();
2022-07-29 17:48:45 +00:00
/*
2022-07-29 14:46:09 +00:00
for y in 0..480 {
for x in 0..640 {
mode.set_pixel(x, y, 0x0000ff00);
}
}
2022-07-29 17:48:45 +00:00
*/
2022-07-29 14:46:09 +00:00
let mut x = 1;
2022-07-29 16:51:54 +00:00
let mut y = 0;
2022-07-29 14:46:09 +00:00
2022-07-29 17:48:45 +00:00
let src1 = match src {
2022-07-31 06:54:01 +00:00
BSODSource::DoubleFault(_) => "DoubleFault".to_string(),
BSODSource::Panic(panic_info) => {
let strr = format!("PANIC: {}", panic_info);
strr
}
2022-07-29 17:48:45 +00:00
};
2022-07-29 16:51:54 +00:00
let st = format!(
2022-07-31 06:54:01 +00:00
"We fucked up ඞ : \n{}\nThe following qr code will link you to the wiki which hopefully solves your problems",
2022-07-29 17:48:45 +00:00
src1
2022-07-29 16:51:54 +00:00
);
2022-07-29 14:46:09 +00:00
2022-07-29 16:51:54 +00:00
for current in st.chars() {
2022-07-31 06:54:01 +00:00
if current == '\n' || x == 40 {
2022-07-29 16:51:54 +00:00
y += 1;
x = 1;
} else {
mode.draw_char(
(x * 14).try_into().unwrap(),
(y * 22).try_into().unwrap(),
current,
0xffff0000,
);
}
x += 1;
}
let mut x = 1;
2022-07-31 06:54:01 +00:00
let mut y = 34;
2022-07-29 16:51:54 +00:00
// let sf = format!("https://git.ablecorp.us/able/ableos/wiki/Double-Faults");
let sd = match src {
2022-07-29 17:48:45 +00:00
BSODSource::DoubleFault(_) => "https://git.ablecorp.us/able/ableos/wiki/Double-Faults",
BSODSource::Panic(_) => {
2022-07-29 18:29:54 +00:00
trace!("panic");
2022-07-31 06:54:01 +00:00
"https://git.ablecorp.us/able/ableos/wiki/Panic"
2022-07-29 17:48:45 +00:00
}
2022-07-29 16:51:54 +00:00
};
let code = QrCode::new(sd).unwrap();
2022-07-29 14:46:09 +00:00
let image = code
.render::<char>()
.quiet_zone(false)
.module_dimensions(2, 1)
.build();
for current in image.chars() {
if current == '\n' {
y += 1;
2022-07-29 16:51:54 +00:00
x = 0;
2022-07-29 14:46:09 +00:00
} else {
2022-07-29 17:48:45 +00:00
if current == '█' {
mode.draw_filled_rect(x * 6, y * 7, (x * 6) + 6, (y * 7) + 7, 0xffffff00);
2022-07-29 14:46:09 +00:00
}
}
x += 1;
}
mode.copy_to_buffer();
2022-07-29 17:48:45 +00:00
2022-07-31 06:54:01 +00:00
sloop();
2022-07-29 14:46:09 +00:00
}
2022-07-29 16:51:54 +00:00
#[derive(Debug)]
2022-07-29 17:48:45 +00:00
pub enum BSODSource<'a> {
DoubleFault(&'a InterruptStackFrame),
Panic(&'a PanicInfo<'a>),
2022-07-29 16:51:54 +00:00
}