mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-08 21:06:48 +00:00
Extract x86 specific irq code
This commit is contained in:
parent
b3f4075bec
commit
cb1a117681
@ -127,6 +127,7 @@ impl UserContextApiInternal for UserContext {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// As Osdev Wiki defines(https://wiki.osdev.org/Exceptions):
|
/// As Osdev Wiki defines(https://wiki.osdev.org/Exceptions):
|
||||||
/// CPU exceptions are classified as:
|
/// CPU exceptions are classified as:
|
||||||
///
|
///
|
||||||
@ -154,12 +155,6 @@ pub struct CpuException {
|
|||||||
pub typ: CpuExceptionType,
|
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
|
/// Copy from: https://veykril.github.io/tlborm/decl-macros/building-blocks/counting.html#slice-length
|
||||||
macro_rules! replace_expr {
|
macro_rules! replace_expr {
|
||||||
($_t:tt $sub:expr) => {
|
($_t:tt $sub:expr) => {
|
||||||
|
@ -1,5 +1,24 @@
|
|||||||
/// move interrupts instructions ..
|
use alloc::vec::Vec;
|
||||||
/// irq 32,256
|
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() {
|
pub(crate) fn enable_interrupts() {
|
||||||
x86_64::instructions::interrupts::enable();
|
x86_64::instructions::interrupts::enable();
|
||||||
|
@ -17,6 +17,7 @@ pub(crate) fn before_all_init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn after_all_init() {
|
pub(crate) fn after_all_init() {
|
||||||
|
irq::init();
|
||||||
device::serial::callback_init();
|
device::serial::callback_init();
|
||||||
kernel::acpi::init();
|
kernel::acpi::init();
|
||||||
if kernel::xapic::has_apic() {
|
if kernel::xapic::has_apic() {
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
use crate::cpu::CpuException;
|
use crate::{arch::irq::IRQ_LIST, cpu::CpuException};
|
||||||
|
|
||||||
use super::irq::IRQ_LIST;
|
|
||||||
use trapframe::TrapFrame;
|
use trapframe::TrapFrame;
|
||||||
|
|
||||||
/// Only from kernel
|
/// 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) {
|
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();
|
let callback_functions = irq_line.callback_list();
|
||||||
for callback_function in callback_functions.iter() {
|
for callback_function in callback_functions.iter() {
|
||||||
callback_function.call(trap_frame);
|
callback_function.call(trap_frame);
|
||||||
|
@ -1,16 +1,11 @@
|
|||||||
|
use crate::arch::irq::{IRQ_LIST, NOT_USING_IRQ};
|
||||||
use crate::{prelude::*, Error};
|
use crate::{prelude::*, Error};
|
||||||
|
|
||||||
use crate::util::recycle_allocator::RecycleAllocator;
|
use crate::util::recycle_allocator::RecycleAllocator;
|
||||||
use core::fmt::Debug;
|
use core::fmt::Debug;
|
||||||
use lazy_static::lazy_static;
|
|
||||||
use spin::{Mutex, MutexGuard};
|
use spin::{Mutex, MutexGuard};
|
||||||
use trapframe::TrapFrame;
|
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> {
|
pub fn allocate_irq() -> Result<IrqAllocateHandle> {
|
||||||
let irq_num = NOT_USING_IRQ.lock().alloc();
|
let irq_num = NOT_USING_IRQ.lock().alloc();
|
||||||
if irq_num == usize::MAX {
|
if irq_num == usize::MAX {
|
||||||
@ -77,22 +72,7 @@ impl Drop for IrqAllocateHandle {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
static ID_ALLOCATOR: Mutex<RecycleAllocator> = Mutex::new(RecycleAllocator::new());
|
||||||
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());
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct CallbackElement {
|
pub struct CallbackElement {
|
||||||
function: Box<dyn Fn(&TrapFrame) + Send + Sync + 'static>,
|
function: Box<dyn Fn(&TrapFrame) + Send + Sync + 'static>,
|
||||||
@ -116,8 +96,8 @@ impl Debug for CallbackElement {
|
|||||||
/// An interrupt request (IRQ) line.
|
/// An interrupt request (IRQ) line.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(crate) struct IrqLine {
|
pub(crate) struct IrqLine {
|
||||||
irq_num: u8,
|
pub(crate) irq_num: u8,
|
||||||
callback_list: Mutex<Vec<CallbackElement>>,
|
pub(crate) callback_list: Mutex<Vec<CallbackElement>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IrqLine {
|
impl IrqLine {
|
||||||
@ -128,7 +108,7 @@ impl IrqLine {
|
|||||||
/// This function is marked unsafe as manipulating interrupt lines is
|
/// This function is marked unsafe as manipulating interrupt lines is
|
||||||
/// considered a dangerous operation.
|
/// considered a dangerous operation.
|
||||||
pub unsafe fn acquire(irq_num: u8) -> Arc<&'static Self> {
|
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.
|
/// Get the IRQ number.
|
||||||
@ -176,6 +156,8 @@ pub struct IrqCallbackHandle {
|
|||||||
impl Drop for IrqCallbackHandle {
|
impl Drop for IrqCallbackHandle {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
let mut a = IRQ_LIST
|
let mut a = IRQ_LIST
|
||||||
|
.get()
|
||||||
|
.unwrap()
|
||||||
.get(self.irq_num as usize)
|
.get(self.irq_num as usize)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.callback_list
|
.callback_list
|
||||||
|
@ -9,7 +9,7 @@ pub struct RecycleAllocator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl RecycleAllocator {
|
impl RecycleAllocator {
|
||||||
pub fn new() -> Self {
|
pub const fn new() -> Self {
|
||||||
RecycleAllocator {
|
RecycleAllocator {
|
||||||
current: 0,
|
current: 0,
|
||||||
recycled: Vec::new(),
|
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 {
|
RecycleAllocator {
|
||||||
current: start,
|
current: start,
|
||||||
recycled: Vec::new(),
|
recycled: Vec::new(),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user