From c01e440d014253fd4cae9f642c949720a54baf4b Mon Sep 17 00:00:00 2001 From: Tianhao Wang Date: Thu, 1 Feb 2024 15:10:11 +0100 Subject: basic interrupt/PIC support --- src/arch/x86_64/interrupt/mod.rs | 23 ++++++++++++++++++ src/arch/x86_64/interrupt/pic_8259.rs | 46 +++++++++++++++++++++++++++++++++++ src/arch/x86_64/interrupt/pit.rs | 1 + src/arch/x86_64/mod.rs | 4 +-- src/arch/x86_64/pic.rs | 1 - src/arch/x86_64/pit.rs | 1 - src/arch/x86_64/plugbox.rs | 1 - 7 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 src/arch/x86_64/interrupt/mod.rs create mode 100644 src/arch/x86_64/interrupt/pic_8259.rs create mode 100644 src/arch/x86_64/interrupt/pit.rs delete mode 100644 src/arch/x86_64/pic.rs delete mode 100644 src/arch/x86_64/pit.rs delete mode 100644 src/arch/x86_64/plugbox.rs (limited to 'src/arch') diff --git a/src/arch/x86_64/interrupt/mod.rs b/src/arch/x86_64/interrupt/mod.rs new file mode 100644 index 0000000..3a617e8 --- /dev/null +++ b/src/arch/x86_64/interrupt/mod.rs @@ -0,0 +1,23 @@ +pub mod pic_8259; +pub mod pit; +use crate::io::*; +use core::arch::asm; + +#[no_mangle] +extern "C" fn guardian(slot: u16) { + println!("interrupt received {:x}", slot); +} + +#[inline(always)] +pub fn interrupt_enable() { + unsafe { + asm!("sti"); + } +} + +#[inline(always)] +pub fn interrupt_disable() { + unsafe { + asm!("cli"); + } +} diff --git a/src/arch/x86_64/interrupt/pic_8259.rs b/src/arch/x86_64/interrupt/pic_8259.rs new file mode 100644 index 0000000..43a102c --- /dev/null +++ b/src/arch/x86_64/interrupt/pic_8259.rs @@ -0,0 +1,46 @@ +// For now, the PIC is stateless, i.e. we don'e need a struct for it. +// Perhaps I need a Mutex handle later... +use crate::arch::x86_64::io_port::*; + +const IMR1: u16 = 0x21; +const IMR2: u16 = 0xa1; +const CTRL1: u16 = 0x20; +const CTRL2: u16 = 0xa0; + +pub struct PicDeviceInt; +impl PicDeviceInt { + pub const TIMER: u8 = 0; + pub const KEYBOARD: u8 = 1; +} + +// 8-bit registers IMR1 and IMR2 registers hold interrupt masking bit 0~7 and +// 8~15; if an interrupt is masked(set 1) on the respective bit, it's disabled +pub fn allow(interrupt: u8) { + if interrupt < 8 { + let old = inb(IMR1); + outb(IMR1, old & !(1 << interrupt)); + } else { + let old = inb(IMR2); + outb(IMR2, old & !(1 << (interrupt - 8))); + } +} + +pub fn forbid(interrupt: u8) { + if interrupt < 8 { + let old = inb(IMR1); + outb(IMR1, old | (1 << interrupt)); + } else { + let old = inb(IMR2); + outb(IMR2, old | (1 << (interrupt - 8))); + } +} + +pub fn is_masked(interrupt: u8) -> bool { + if interrupt < 8 { + let val = inb(IMR1); + return val & (interrupt) != 0; + } else { + let val = inb(IMR2); + return val & (interrupt - 8) != 0; + } +} diff --git a/src/arch/x86_64/interrupt/pit.rs b/src/arch/x86_64/interrupt/pit.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/arch/x86_64/interrupt/pit.rs @@ -0,0 +1 @@ + diff --git a/src/arch/x86_64/mod.rs b/src/arch/x86_64/mod.rs index a7f9e89..9429c8f 100644 --- a/src/arch/x86_64/mod.rs +++ b/src/arch/x86_64/mod.rs @@ -1,6 +1,4 @@ pub mod cpu; +pub mod interrupt; pub mod io_port; pub mod misc; -pub mod pic; -pub mod pit; -pub mod plugbox; diff --git a/src/arch/x86_64/pic.rs b/src/arch/x86_64/pic.rs deleted file mode 100644 index 8b13789..0000000 --- a/src/arch/x86_64/pic.rs +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/arch/x86_64/pit.rs b/src/arch/x86_64/pit.rs deleted file mode 100644 index 8b13789..0000000 --- a/src/arch/x86_64/pit.rs +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/arch/x86_64/plugbox.rs b/src/arch/x86_64/plugbox.rs deleted file mode 100644 index 8b13789..0000000 --- a/src/arch/x86_64/plugbox.rs +++ /dev/null @@ -1 +0,0 @@ - -- cgit v1.2.3-70-g09d2