Extract x86 specific irq code

This commit is contained in:
Yuke Peng 2023-04-01 22:41:46 -07:00 committed by Tate, Hongliang Tian
parent b3f4075bec
commit cb1a117681
6 changed files with 38 additions and 38 deletions

View File

@ -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) => {

View File

@ -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<RecycleAllocator> =
Mutex::new(RecycleAllocator::with_start_max(32, 256));
pub(crate) static IRQ_LIST: Once<Vec<IrqLine>> = Once::new();
pub(crate) fn init() {
let mut list: Vec<IrqLine> = 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();

View File

@ -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() {

View File

@ -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);

View File

@ -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<RecycleAllocator> = Mutex::new(RecycleAllocator::with_start_max(32,256));
}
pub fn allocate_irq() -> Result<IrqAllocateHandle> {
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<IrqLine> = {
let mut list: Vec<IrqLine> = 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<RecycleAllocator> = Mutex::new(RecycleAllocator::new());
}
static ID_ALLOCATOR: Mutex<RecycleAllocator> = Mutex::new(RecycleAllocator::new());
pub struct CallbackElement {
function: Box<dyn Fn(&TrapFrame) + Send + Sync + 'static>,
@ -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<Vec<CallbackElement>>,
pub(crate) irq_num: u8,
pub(crate) callback_list: Mutex<Vec<CallbackElement>>,
}
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

View File

@ -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(),