diff --git a/src/framework/jinux-frame/src/arch/x86/cpu.rs b/src/framework/jinux-frame/src/arch/x86/cpu.rs index d2a1b2d4..2c1a21f8 100644 --- a/src/framework/jinux-frame/src/arch/x86/cpu.rs +++ b/src/framework/jinux-frame/src/arch/x86/cpu.rs @@ -127,6 +127,7 @@ impl UserContextApiInternal for UserContext { } } } + /// As Osdev Wiki defines(https://wiki.osdev.org/Exceptions): /// CPU exceptions are classified as: /// @@ -154,12 +155,6 @@ pub struct CpuException { pub typ: CpuExceptionType, } -// impl const PartialEq for CpuException{ -// fn eq(&self, other: &Self) -> bool { -// self.number == other.number -// } -// } - /// Copy from: https://veykril.github.io/tlborm/decl-macros/building-blocks/counting.html#slice-length macro_rules! replace_expr { ($_t:tt $sub:expr) => { diff --git a/src/framework/jinux-frame/src/arch/x86/irq.rs b/src/framework/jinux-frame/src/arch/x86/irq.rs index 6c213606..555f9e69 100644 --- a/src/framework/jinux-frame/src/arch/x86/irq.rs +++ b/src/framework/jinux-frame/src/arch/x86/irq.rs @@ -1,5 +1,24 @@ -/// move interrupts instructions .. -/// irq 32,256 +use alloc::vec::Vec; +use spin::{Mutex, Once}; + +use crate::{trap::IrqLine, util::recycle_allocator::RecycleAllocator}; + +/// The IRQ numbers which are not using +pub(crate) static NOT_USING_IRQ: Mutex = + Mutex::new(RecycleAllocator::with_start_max(32, 256)); + +pub(crate) static IRQ_LIST: Once> = Once::new(); + +pub(crate) fn init() { + let mut list: Vec = Vec::new(); + for i in 0..256 { + list.push(IrqLine { + irq_num: i as u8, + callback_list: Mutex::new(Vec::new()), + }); + } + IRQ_LIST.call_once(|| list); +} pub(crate) fn enable_interrupts() { x86_64::instructions::interrupts::enable(); diff --git a/src/framework/jinux-frame/src/arch/x86/mod.rs b/src/framework/jinux-frame/src/arch/x86/mod.rs index a81f7cc2..dce3cc7f 100644 --- a/src/framework/jinux-frame/src/arch/x86/mod.rs +++ b/src/framework/jinux-frame/src/arch/x86/mod.rs @@ -17,6 +17,7 @@ pub(crate) fn before_all_init() { } pub(crate) fn after_all_init() { + irq::init(); device::serial::callback_init(); kernel::acpi::init(); if kernel::xapic::has_apic() { diff --git a/src/framework/jinux-frame/src/trap/handler.rs b/src/framework/jinux-frame/src/trap/handler.rs index 505958d3..4c26f402 100644 --- a/src/framework/jinux-frame/src/trap/handler.rs +++ b/src/framework/jinux-frame/src/trap/handler.rs @@ -1,6 +1,5 @@ -use crate::cpu::CpuException; +use crate::{arch::irq::IRQ_LIST, cpu::CpuException}; -use super::irq::IRQ_LIST; use trapframe::TrapFrame; /// Only from kernel @@ -13,7 +12,11 @@ extern "sysv64" fn trap_handler(f: &mut TrapFrame) { } pub(crate) fn call_irq_callback_functions(trap_frame: &TrapFrame) { - let irq_line = IRQ_LIST.get(trap_frame.trap_num as usize).unwrap(); + let irq_line = IRQ_LIST + .get() + .unwrap() + .get(trap_frame.trap_num as usize) + .unwrap(); let callback_functions = irq_line.callback_list(); for callback_function in callback_functions.iter() { callback_function.call(trap_frame); diff --git a/src/framework/jinux-frame/src/trap/irq.rs b/src/framework/jinux-frame/src/trap/irq.rs index af0a9f81..70dd659f 100644 --- a/src/framework/jinux-frame/src/trap/irq.rs +++ b/src/framework/jinux-frame/src/trap/irq.rs @@ -1,16 +1,11 @@ +use crate::arch::irq::{IRQ_LIST, NOT_USING_IRQ}; use crate::{prelude::*, Error}; use crate::util::recycle_allocator::RecycleAllocator; use core::fmt::Debug; -use lazy_static::lazy_static; use spin::{Mutex, MutexGuard}; use trapframe::TrapFrame; -lazy_static! { - /// The IRQ numbers which are not using - static ref NOT_USING_IRQ: Mutex = Mutex::new(RecycleAllocator::with_start_max(32,256)); -} - pub fn allocate_irq() -> Result { let irq_num = NOT_USING_IRQ.lock().alloc(); if irq_num == usize::MAX { @@ -77,22 +72,7 @@ impl Drop for IrqAllocateHandle { } } -lazy_static! { - pub(crate) static ref IRQ_LIST: Vec = { - let mut list: Vec = Vec::new(); - for i in 0..256 { - list.push(IrqLine { - irq_num: i as u8, - callback_list: Mutex::new(Vec::new()), - }); - } - list - }; -} - -lazy_static! { - static ref ID_ALLOCATOR: Mutex = Mutex::new(RecycleAllocator::new()); -} +static ID_ALLOCATOR: Mutex = Mutex::new(RecycleAllocator::new()); pub struct CallbackElement { function: Box, @@ -116,8 +96,8 @@ impl Debug for CallbackElement { /// An interrupt request (IRQ) line. #[derive(Debug)] pub(crate) struct IrqLine { - irq_num: u8, - callback_list: Mutex>, + pub(crate) irq_num: u8, + pub(crate) callback_list: Mutex>, } impl IrqLine { @@ -128,7 +108,7 @@ impl IrqLine { /// This function is marked unsafe as manipulating interrupt lines is /// considered a dangerous operation. pub unsafe fn acquire(irq_num: u8) -> Arc<&'static Self> { - Arc::new(IRQ_LIST.get(irq_num as usize).unwrap()) + Arc::new(IRQ_LIST.get().unwrap().get(irq_num as usize).unwrap()) } /// Get the IRQ number. @@ -176,6 +156,8 @@ pub struct IrqCallbackHandle { impl Drop for IrqCallbackHandle { fn drop(&mut self) { let mut a = IRQ_LIST + .get() + .unwrap() .get(self.irq_num as usize) .unwrap() .callback_list diff --git a/src/framework/jinux-frame/src/util/recycle_allocator.rs b/src/framework/jinux-frame/src/util/recycle_allocator.rs index 37077472..798b147a 100644 --- a/src/framework/jinux-frame/src/util/recycle_allocator.rs +++ b/src/framework/jinux-frame/src/util/recycle_allocator.rs @@ -9,7 +9,7 @@ pub struct RecycleAllocator { } impl RecycleAllocator { - pub fn new() -> Self { + pub const fn new() -> Self { RecycleAllocator { current: 0, recycled: Vec::new(), @@ -18,7 +18,7 @@ impl RecycleAllocator { } } - pub fn with_start_max(start: usize, max: usize) -> Self { + pub const fn with_start_max(start: usize, max: usize) -> Self { RecycleAllocator { current: start, recycled: Vec::new(),