#![allow(dead_code)]
use alloc::collections::BTreeMap;
use core::mem::size_of;
use log::warn;
use ostd_pod::Pod;
use super::second_stage::{DeviceMode, PageTableEntry, PagingConsts};
use crate::{
bus::pci::PciDeviceLocation,
mm::{
dma::Daddr,
page_prop::{CachePolicy, PageProperty, PrivilegedPageFlags as PrivFlags},
page_table::{PageTableError, PageTableItem},
Frame, FrameAllocOptions, Paddr, PageFlags, PageTable, VmIo, PAGE_SIZE,
},
};
#[derive(Pod, Clone, Copy)]
#[repr(C)]
pub struct RootEntry(u128);
impl RootEntry {
pub const fn is_present(&self) -> bool {
(self.0 & 0b1) != 0
}
pub const fn addr(&self) -> u64 {
(self.0 & 0xFFFF_FFFF_FFFF_F000) as u64
}
}
pub struct RootTable {
root_frame: Frame,
context_tables: BTreeMap<Paddr, ContextTable>,
}
#[derive(Debug)]
pub enum ContextTableError {
InvalidDeviceId,
ModificationError(PageTableError),
}
impl RootTable {
pub fn root_paddr(&self) -> Paddr {
self.root_frame.start_paddr()
}
pub(super) fn new() -> Self {
Self {
root_frame: FrameAllocOptions::new(1).alloc_single().unwrap(),
context_tables: BTreeMap::new(),
}
}
pub(super) unsafe fn map(
&mut self,
device: PciDeviceLocation,
daddr: Daddr,
paddr: Paddr,
) -> Result<(), ContextTableError> {
if device.device >= 32 || device.function >= 8 {
return Err(ContextTableError::InvalidDeviceId);
}
self.get_or_create_context_table(device)
.map(device, daddr, paddr)?;
Ok(())
}
pub(super) fn unmap(
&mut self,
device: PciDeviceLocation,
daddr: Daddr,
) -> Result<(), ContextTableError> {
if device.device >= 32 || device.function >= 8 {
return Err(ContextTableError::InvalidDeviceId);
}
self.get_or_create_context_table(device)
.unmap(device, daddr)?;
Ok(())
}
pub(super) fn specify_device_page_table(
&mut self,
device_id: PciDeviceLocation,
page_table: PageTable<DeviceMode, PageTableEntry, PagingConsts>,
) {
let context_table = self.get_or_create_context_table(device_id);
let bus_entry = context_table
.entries_frame
.read_val::<ContextEntry>(
(device_id.device as usize * 8 + device_id.function as usize)
* size_of::<ContextEntry>(),
)
.unwrap();
if bus_entry.is_present() {
warn!("IOMMU: Overwriting the existing device page table");
}
let address = unsafe { page_table.root_paddr() };
context_table.page_tables.insert(address, page_table);
let entry = ContextEntry(address as u128 | 1 | 0x1_0000_0000_0000_0000);
context_table
.entries_frame
.write_val::<ContextEntry>(
(device_id.device as usize * 8 + device_id.function as usize)
* size_of::<ContextEntry>(),
&entry,
)
.unwrap();
context_table.page_tables.get_mut(&address).unwrap();
}
fn get_or_create_context_table(&mut self, device_id: PciDeviceLocation) -> &mut ContextTable {
let bus_entry = self
.root_frame
.read_val::<RootEntry>(device_id.bus as usize * size_of::<RootEntry>())
.unwrap();
if !bus_entry.is_present() {
let table = ContextTable::new();
let address = table.paddr();
self.context_tables.insert(address, table);
let entry = RootEntry(address as u128 | 1);
self.root_frame
.write_val::<RootEntry>(device_id.bus as usize * size_of::<RootEntry>(), &entry)
.unwrap();
self.context_tables.get_mut(&address).unwrap()
} else {
self.context_tables
.get_mut(&(bus_entry.addr() as usize))
.unwrap()
}
}
}
#[derive(Pod, Clone, Copy)]
#[repr(C)]
pub struct ContextEntry(u128);
impl ContextEntry {
pub const fn domain_identifier(&self) -> u64 {
((self.0 & 0xFF_FF00_0000_0000_0000_0000) >> 72) as u64
}
pub const fn address_width(&self) -> AddressWidth {
let value = ((self.0 & 0x7_0000_0000_0000_0000) >> 64) as u64;
match value {
1 => AddressWidth::Level3PageTable,
2 => AddressWidth::Level4PageTable,
3 => AddressWidth::Level5PageTable,
_ => AddressWidth::Reserved,
}
}
pub const fn second_stage_pointer(&self) -> u64 {
(self.0 & 0xFFFF_FFFF_FFFF_F000) as u64
}
pub const fn translation_type(&self) -> u64 {
((self.0 & 0b1100) >> 2) as u64
}
pub const fn need_fault_process(&self) -> bool {
(self.0 & 0b10) == 0
}
pub const fn is_present(&self) -> bool {
(self.0 & 0b1) != 0
}
}
#[derive(Debug)]
pub enum AddressWidth {
Reserved,
Level3PageTable,
Level4PageTable,
Level5PageTable,
}
pub struct ContextTable {
entries_frame: Frame,
page_tables: BTreeMap<Paddr, PageTable<DeviceMode, PageTableEntry, PagingConsts>>,
}
impl ContextTable {
fn new() -> Self {
Self {
entries_frame: FrameAllocOptions::new(1).alloc_single().unwrap(),
page_tables: BTreeMap::new(),
}
}
fn paddr(&self) -> Paddr {
self.entries_frame.start_paddr()
}
fn get_or_create_page_table(
&mut self,
device: PciDeviceLocation,
) -> &mut PageTable<DeviceMode, PageTableEntry, PagingConsts> {
let bus_entry = self
.entries_frame
.read_val::<ContextEntry>(
(device.device as usize * 8 + device.function as usize) * size_of::<ContextEntry>(),
)
.unwrap();
if !bus_entry.is_present() {
let table = PageTable::<DeviceMode, PageTableEntry, PagingConsts>::empty();
let address = unsafe { table.root_paddr() };
self.page_tables.insert(address, table);
let entry = ContextEntry(address as u128 | 3 | 0x1_0000_0000_0000_0000);
self.entries_frame
.write_val::<ContextEntry>(
(device.device as usize * 8 + device.function as usize)
* size_of::<ContextEntry>(),
&entry,
)
.unwrap();
self.page_tables.get_mut(&address).unwrap()
} else {
self.page_tables
.get_mut(&(bus_entry.second_stage_pointer() as usize))
.unwrap()
}
}
unsafe fn map(
&mut self,
device: PciDeviceLocation,
daddr: Daddr,
paddr: Paddr,
) -> Result<(), ContextTableError> {
if device.device >= 32 || device.function >= 8 {
return Err(ContextTableError::InvalidDeviceId);
}
self.get_or_create_page_table(device)
.map(
&(daddr..daddr + PAGE_SIZE),
&(paddr..paddr + PAGE_SIZE),
PageProperty {
flags: PageFlags::RW,
cache: CachePolicy::Uncacheable,
priv_flags: PrivFlags::empty(),
},
)
.unwrap();
Ok(())
}
fn unmap(&mut self, device: PciDeviceLocation, daddr: Daddr) -> Result<(), ContextTableError> {
if device.device >= 32 || device.function >= 8 {
return Err(ContextTableError::InvalidDeviceId);
}
let pt = self.get_or_create_page_table(device);
let mut cursor = pt.cursor_mut(&(daddr..daddr + PAGE_SIZE)).unwrap();
unsafe {
let result = cursor.take_next(PAGE_SIZE);
debug_assert!(matches!(result, PageTableItem::MappedUntracked { .. }));
}
Ok(())
}
}