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

277 lines
7.4 KiB
Rust
Raw Normal View History

2022-08-05 11:22:23 +00:00
/*
2022-08-12 13:40:23 +00:00
* Copyright (c) 2022, able <abl3theabove@gmail.com>
*
* SPDX-License-Identifier: MPL-2.0
*/
2022-08-05 11:22:23 +00:00
2022-07-29 17:48:45 +00:00
use core::panic::PanicInfo;
2022-08-16 13:35:34 +00:00
use crate::{
arch::gdt,
devices::pci::{PciDevice, PCI_DEVICES},
println,
rhai_shell::KEYBUFF,
};
2022-02-23 00:15:16 +00:00
use cpuio::outb;
2021-11-16 06:09:27 +00:00
use pic8259::ChainedPics;
use qrcode::QrCode;
2022-08-16 13:35:34 +00:00
use seq_macro::seq;
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
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-08-16 13:35:34 +00:00
/// Disk offset
Disk = 46,
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(|| {
2022-08-01 07:16:19 +00:00
reset_pit_for_cpu();
let mut idt = InterruptDescriptorTable::new();
2022-08-16 13:35:34 +00:00
seq!(N in 32..=255 {
idt[N].set_handler_fn(undefined_handler_~N);
});
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
idt[InterruptIndex::Timer.as_usize()].set_handler_fn(timer_interrupt_handler);
idt[InterruptIndex::Keyboard.as_usize()].set_handler_fn(keyboard_interrupt_handler);
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-08-16 13:35:34 +00:00
seq!(N in 32..=255 {
extern "x86-interrupt" fn undefined_handler_~N(stack_frame: InterruptStackFrame) {
error!("INT {}: {:?}", N, stack_frame);
unsafe {
PICS.lock()
.notify_end_of_interrupt(N);
}
}
});
2022-07-29 16:51:54 +00:00
extern "x86-interrupt" fn software_int_handler(stack_frame: InterruptStackFrame) {
2022-07-31 08:03:59 +00:00
trace!("EXCEPTION: SOFTWARE INT\n{:#?}", stack_frame);
2022-08-16 13:35:34 +00:00
unsafe {
PICS.lock().notify_end_of_interrupt(54);
}
2022-07-29 16:51:54 +00:00
}
2021-11-16 06:09:27 +00:00
extern "x86-interrupt" fn breakpoint_handler(stack_frame: InterruptStackFrame) {
2022-07-31 08:03:59 +00:00
trace!("EXCEPTION: BREAKPOINT\n{:#?}", stack_frame);
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 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 11:21:50 +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-08-07 03:56:08 +00:00
/* SAFETY
* DO NOT TOUCH
* The `#[naked]` macro removes various error/bounds checks that
* the Rust compiler would normally add.
* *Early return* and *enabling interrupts* in this function are
* undefined behavior.
* As long as nothing in this function does something that would
* normally trigger an error, this function is relatively safe.
*/
2021-11-16 06:09:27 +00:00
extern "x86-interrupt" fn timer_interrupt_handler(_stack_frame: InterruptStackFrame) {
crate::kmain::tick();
2021-11-16 06:09:27 +00:00
unsafe {
PICS.lock()
.notify_end_of_interrupt(InterruptIndex::Timer.as_u8());
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 11:21:50 +00:00
// trace!("{key:?}");
match key {
DecodedKey::Unicode(chr) => match chr {
2022-08-01 07:16:19 +00:00
'\n' => {
KEYBUFF.lock().push('\n');
}
// Backspace
'\u{8}' => {
2022-07-31 06:54:01 +00:00
// TODO: Fix this and apply to new term
2022-07-31 10:22:39 +00:00
KEYBUFF.lock().push(8.into());
2022-08-03 07:09:34 +00:00
// trace!("8");
2022-08-01 07:16:19 +00:00
// print!("\u{8}");
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);
2022-08-01 07:16:19 +00:00
// trace!("{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 => {
2022-08-01 07:16:19 +00:00
// warn!("ArrowKeys are unsupported currently");
2022-07-29 11:13:26 +00:00
}
_kc => {
2022-08-01 07:16:19 +00:00
// trace!("Unprintable key: {kc:?}"),
}
2022-07-29 11:13:26 +00:00
};
}
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-08-07 03:43:23 +00:00
/// https://wiki.osdev.org/Pit
///
const PIT_MAX_FREQ: u32 = 1193182;
2022-08-07 03:32:22 +00:00
2022-04-25 18:39:39 +00:00
pub fn set_pit_frequency(pit: u16, freq: u32) {
2022-08-07 03:43:23 +00:00
// Dividing the maximum frequency by the desired frequency
// gives roughly what the maximum value for the timer
// counter should be to run at the desired frequency.
2022-08-07 03:32:22 +00:00
let ret = (PIT_MAX_FREQ / freq).try_into();
2022-07-29 11:13:26 +00:00
2022-08-07 03:43:23 +00:00
// Type-bounded counter maximum.
2022-07-29 11:13:26 +00:00
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-08-01 07:16:19 +00:00
set_pit_1(1000);
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) -> ! {
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
println!("\n{}", st);
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();
println!("{}", image);
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
}