Make tracked the metadata and kernel mappings

This commit is contained in:
Zhang Junyang
2024-06-17 03:26:39 +00:00
committed by Tate, Hongliang Tian
parent cab348349e
commit d520360512
3 changed files with 16 additions and 17 deletions

View File

@ -48,7 +48,7 @@ use spin::Once;
use super::{
nr_subpage_per_huge,
page::{
meta::{mapping, KernelMeta},
meta::{mapping, KernelMeta, MetaPageMeta},
Page,
},
page_prop::{CachePolicy, PageFlags, PageProperty, PrivilegedPageFlags},
@ -128,7 +128,7 @@ pub(crate) fn init_boot_page_table() {
///
/// This function should be called before:
/// - any initializer that modifies the kernel page table.
pub fn init_kernel_page_table(meta_pages: Vec<Range<Paddr>>) {
pub fn init_kernel_page_table(meta_pages: Vec<Page<MetaPageMeta>>) {
info!("Initializing the kernel page table");
let regions = crate::boot::memory_regions();
@ -168,10 +168,10 @@ pub fn init_kernel_page_table(meta_pages: Vec<Range<Paddr>>) {
priv_flags: PrivilegedPageFlags::GLOBAL,
};
let mut cursor = kpt.cursor_mut(&from).unwrap();
for frame in meta_pages {
for meta_page in meta_pages {
// SAFETY: we are doing the metadata mappings for the kernel.
unsafe {
cursor.map_pa(&frame, prop);
cursor.map(meta_page.into(), prop);
}
}
}
@ -212,10 +212,9 @@ pub fn init_kernel_page_table(meta_pages: Vec<Range<Paddr>>) {
let mut cursor = kpt.cursor_mut(&from).unwrap();
for frame_paddr in to.step_by(PAGE_SIZE) {
let page = Page::<KernelMeta>::from_unused(frame_paddr);
let paddr = page.into_raw();
// SAFETY: we are doing mappings for the kernel.
unsafe {
cursor.map_pa(&(paddr..paddr + PAGE_SIZE), prop);
cursor.map(page.into(), prop);
}
}
}

View File

@ -39,7 +39,6 @@ use alloc::vec::Vec;
use core::{
marker::PhantomData,
mem::{size_of, ManuallyDrop},
ops::Range,
panic,
sync::atomic::{AtomicU32, AtomicU8, Ordering},
};
@ -225,7 +224,7 @@ impl PageMeta for KernelMeta {
/// Initializes the metadata of all physical pages.
///
/// The function returns a list of `Page`s containing the metadata.
pub(crate) fn init() -> Vec<Range<Paddr>> {
pub(crate) fn init() -> Vec<Page<MetaPageMeta>> {
let max_paddr = {
let regions = crate::boot::memory_regions();
regions.iter().map(|r| r.base() + r.len()).max().unwrap()
@ -259,10 +258,7 @@ pub(crate) fn init() -> Vec<Range<Paddr>> {
// Now the metadata pages are mapped, we can initialize the metadata.
meta_pages
.into_iter()
.map(|paddr| {
let pa = Page::<MetaPageMeta>::from_unused(paddr).into_raw();
pa..pa + PAGE_SIZE
})
.map(Page::<MetaPageMeta>::from_unused)
.collect()
}

View File

@ -56,7 +56,7 @@ use align_ext::AlignExt;
use super::{
page_size, pte_index, Child, KernelMode, PageTable, PageTableEntryTrait, PageTableError,
PageTableMode, PageTableNode, PagingConstsTrait, PagingLevel,
PageTableMode, PageTableNode, PagingConstsTrait, PagingLevel, UserMode,
};
use crate::mm::{page::DynPage, Paddr, PageProperty, Vaddr};
@ -270,15 +270,19 @@ where
/// Tells if the current virtual range must contain untracked mappings.
///
/// _Tracked mappings_ means that the mapped physical addresses (in PTEs) points to pages
/// tracked by the metadata system. _Tracked mappings_ must be created with page handles.
/// While _untracked mappings_ solely maps to plain physical addresses.
///
/// In the kernel mode, this is aligned with the definition in [`crate::mm::kspace`].
/// Only linear mappings in the kernel are considered as untracked mappings.
/// Only linear mappings in the kernel should be considered as untracked mappings.
///
/// All mappings in the user mode are tracked. And all mappings in the IOMMU
/// page table are untracked.
fn in_tracked_range(&self) -> bool {
TypeId::of::<M>() != TypeId::of::<crate::arch::iommu::DeviceMode>()
&& (TypeId::of::<M>() != TypeId::of::<KernelMode>()
|| crate::mm::kspace::VMALLOC_VADDR_RANGE.contains(&self.va))
TypeId::of::<M>() == TypeId::of::<UserMode>()
|| TypeId::of::<M>() == TypeId::of::<KernelMode>()
&& !crate::mm::kspace::LINEAR_MAPPING_VADDR_RANGE.contains(&self.va)
}
}