Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x86_64 virtio:get irqnum support for x86_64 virtio dev #97

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion modules/axhal/src/arch/x86_64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use x86_64::instructions::interrupts;
pub use self::context::{ExtendedState, FxsaveArea, TaskContext, TrapFrame};
pub use self::gdt::GdtStruct;
pub use self::idt::IdtStruct;
pub use self::trap::{irq_to_vector, vector_to_irq};
pub use self::trap::{IRQ_VECTOR_START, IRQ_VECTOR_END};
pub use x86_64::structures::tss::TaskStateSegment;

/// Allows the current CPU to respond to interrupts.
Expand Down
12 changes: 2 additions & 10 deletions modules/axhal/src/arch/x86_64/trap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use super::context::TrapFrame;

core::arch::global_asm!(include_str!("trap.S"));

const IRQ_VECTOR_START: u8 = 0x20;
const IRQ_VECTOR_END: u8 = 0xff;
pub const IRQ_VECTOR_START: u8 = 0x20;
pub const IRQ_VECTOR_END: u8 = 0xff;

#[no_mangle]
fn x86_trap_handler(tf: &mut TrapFrame) {
Expand Down Expand Up @@ -45,11 +45,3 @@ fn x86_trap_handler(tf: &mut TrapFrame) {
}
}

/// map external IRQ to vector
pub fn irq_to_vector(irq: u8) -> usize {
(irq + IRQ_VECTOR_START) as usize
}
/// map vector to external IRQ
pub fn vector_to_irq(vector: usize) -> u8 {
vector as u8 - IRQ_VECTOR_START
}
2 changes: 1 addition & 1 deletion modules/axhal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#[macro_use]
extern crate log;

pub mod platform;
mod platform;

pub mod arch;
pub mod cpu;
Expand Down
44 changes: 12 additions & 32 deletions modules/axhal/src/platform/pc_x86/acpi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@ extern crate alloc;

use alloc::boxed::Box;
use alloc::format;
use core::alloc::{AllocError, Layout};
use core::ptr::NonNull;

use acpi::{AcpiTables, PhysicalMapping};
use aml::pci_routing::{IrqDescriptor, PciRoutingTable, Pin};
use aml::{AmlContext, AmlName, DebugVerbosity};

use crate::mem::phys_to_virt;
use axalloc::global_allocator;
use lazy_init::LazyInit;
use memory_addr::PhysAddr;

use crate::arch::irq_to_vector;
#[cfg(feature = "irq")]
use crate::platform::irq::irq_to_vector;

#[derive(Clone)]
struct LocalAcpiHandler;
Expand Down Expand Up @@ -115,7 +114,7 @@ impl aml::Handler for LocalAmlHandler {
ACPI.get_pci_config_regions_addr(segment, bus, device, function)
.unwrap()
};
let vaddr = phys_to_virt(PhysAddr::from(paddr as usize)).as_usize() as *mut u8;
let vaddr = phys_to_virt(PhysAddr::from(paddr as usize)).as_mut_ptr();
let address = unsafe { vaddr.add(offset as usize) };
unsafe { address.read_volatile() }
}
Expand Down Expand Up @@ -153,7 +152,7 @@ impl aml::Handler for LocalAmlHandler {
ACPI.get_pci_config_regions_addr(segment, bus, device, function)
.unwrap()
};
let vaddr = phys_to_virt(PhysAddr::from(paddr as usize)).as_usize() as *mut u8;
let vaddr = phys_to_virt(PhysAddr::from(paddr as usize)).as_mut_ptr();
let address = unsafe { vaddr.add(offset as usize) };
unsafe { address.write_volatile(value) }
}
Expand Down Expand Up @@ -195,37 +194,14 @@ impl aml::Handler for LocalAmlHandler {
}
}

#[derive(Clone, Debug)]
struct LocalAllocator;

unsafe impl core::alloc::Allocator for LocalAllocator {
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
match layout.size() {
0 => Ok(NonNull::slice_from_raw_parts(layout.dangling(), 0)),
size => {
let raw_ptr = global_allocator()
.alloc(layout.size(), layout.align())
.unwrap() as *mut u8;
let ptr = NonNull::new(raw_ptr).ok_or(AllocError)?;
Ok(NonNull::slice_from_raw_parts(ptr, size))
}
}
}

unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
if layout.size() != 0 {
global_allocator().dealloc(ptr.as_ptr() as usize, layout.size(), layout.align())
}
}
}

struct Acpi {
rsdp: AcpiTables<LocalAcpiHandler>,
aml_context: AmlContext,
}

/// irq model used in ACPI
pub enum X86IrqModel {
#[allow(dead_code)]
enum X86IrqModel {
equation314 marked this conversation as resolved.
Show resolved Hide resolved
/// PIC model
PIC,
/// APIC model
Expand All @@ -243,9 +219,9 @@ impl Acpi {
fn init(&mut self) -> bool {
let dsdt = self.rsdp.dsdt.as_ref().unwrap();
let paddr = PhysAddr::from(dsdt.address);
let vaddr = phys_to_virt(paddr).as_ptr();
let vaddr = phys_to_virt(paddr).as_mut_ptr();
let slice =
unsafe { core::slice::from_raw_parts_mut(vaddr as *mut u8, dsdt.length as usize) };
unsafe { core::slice::from_raw_parts_mut(vaddr, dsdt.length as usize) };
if self.aml_context.parse_table(slice).is_err() {
return false;
}
Expand Down Expand Up @@ -343,12 +319,16 @@ pub(crate) fn init() {
}

/// Get PCI IRQ and map it to vector used in OS.
/// Temporarily allow unused here because irq support for virtio hasn't ready yet.
#[allow(dead_code)]
#[cfg(feature = "irq")]
pub fn get_pci_irq_vector(bus: u8, device: u8, function: u8) -> Option<usize> {
unsafe { ACPI.get_pci_irq_desc(bus, device, function) }
.map(|irq_desc| irq_to_vector(irq_desc.irq as u8))
}

/// Get PCIe ECAM space physical address.
#[allow(dead_code)]
pub fn get_ecam_address() -> Option<u64> {
lklake marked this conversation as resolved.
Show resolved Hide resolved
unsafe { ACPI.get_ecam_address() }
}
15 changes: 13 additions & 2 deletions modules/axhal/src/platform/pc_x86/apic.rs
equation314 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,24 @@ use x86_64::instructions::port::Port;
use self::vectors::*;
use crate::mem::phys_to_virt;

#[cfg(feature = "irq")]
use crate::arch::vector_to_irq;
#[cfg(feature = "irq")]
use crate::platform::pc_x86::current_cpu_id;
#[cfg(feature = "irq")]
use x2apic::ioapic::{IrqFlags, IrqMode};

#[cfg(feature = "irq")]
use crate::arch::{IRQ_VECTOR_START};
/// map external IRQ to vector
#[cfg(feature = "irq")]
pub fn irq_to_vector(irq: u8) -> usize {
(irq + IRQ_VECTOR_START) as usize
}
/// map vector to external IRQ
#[cfg(feature = "irq")]
pub fn vector_to_irq(vector: usize) -> u8 {
vector as u8 - IRQ_VECTOR_START
}

pub(super) mod vectors {
pub const APIC_TIMER_VECTOR: u8 = 0xf0;
pub const APIC_SPURIOUS_VECTOR: u8 = 0xf1;
Expand Down
4 changes: 2 additions & 2 deletions modules/axhal/src/platform/pc_x86/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod dtables;
mod uart16550;

/// acpi module
lklake marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(feature = "alloc")]
#[cfg(feature = "irq")]
pub mod acpi;
/// mem module
pub mod mem;
Expand Down Expand Up @@ -67,7 +67,7 @@ unsafe extern "C" fn rust_entry_secondary(magic: usize) {
pub fn platform_init() {
self::apic::init_primary();
self::time::init_primary();
#[cfg(feature = "alloc")]
#[cfg(feature = "irq")]
self::acpi::init();
}

Expand Down