Refactor the source structure in aster_frame::mm

This commit is contained in:
Zhang Junyang
2024-06-02 11:00:34 +00:00
committed by Tate, Hongliang Tian
parent e8595b95fe
commit 7095b37e7e
38 changed files with 177 additions and 129 deletions

View File

@ -13,7 +13,7 @@ use crate::{
dma::Daddr,
page_prop::{CachePolicy, PageProperty, PrivilegedPageFlags as PrivFlags},
page_table::PageTableError,
Frame, Paddr, PageFlags, PageTable, VmAllocOptions, VmIo, PAGE_SIZE,
Frame, FrameAllocOptions, Paddr, PageFlags, PageTable, VmIo, PAGE_SIZE,
},
};
@ -51,7 +51,7 @@ pub enum ContextTableError {
impl RootTable {
pub fn new() -> Self {
Self {
root_frame: VmAllocOptions::new(1).alloc_single().unwrap(),
root_frame: FrameAllocOptions::new(1).alloc_single().unwrap(),
context_tables: BTreeMap::new(),
}
}
@ -240,7 +240,7 @@ pub struct ContextTable {
impl ContextTable {
fn new() -> Self {
Self {
entries_frame: VmAllocOptions::new(1).alloc_single().unwrap(),
entries_frame: FrameAllocOptions::new(1).alloc_single().unwrap(),
page_tables: BTreeMap::new(),
}
}

View File

@ -51,6 +51,14 @@ use tdx_guest::init_tdx;
pub use self::{cpu::CpuLocal, error::Error, prelude::Result};
/// Initialize the framework.
///
/// This function represents the first phase booting up the system. It makes
/// all functionalities of the framework available after the call.
///
/// TODO: We need to refactor this function to make it more modular and
/// make inter-initialization-dependencies more clear and reduce usages of
/// boot stage only global variables.
pub fn init() {
arch::before_all_init();
logger::init();

View File

@ -190,11 +190,11 @@ mod test {
use alloc::vec;
use super::*;
use crate::mm::VmAllocOptions;
use crate::mm::FrameAllocOptions;
#[ktest]
fn map_with_coherent_device() {
let vm_segment = VmAllocOptions::new(1)
let vm_segment = FrameAllocOptions::new(1)
.is_contiguous(true)
.alloc_contiguous()
.unwrap();
@ -204,7 +204,7 @@ mod test {
#[ktest]
fn map_with_incoherent_device() {
let vm_segment = VmAllocOptions::new(1)
let vm_segment = FrameAllocOptions::new(1)
.is_contiguous(true)
.alloc_contiguous()
.unwrap();
@ -217,7 +217,7 @@ mod test {
#[ktest]
fn duplicate_map() {
let vm_segment_parent = VmAllocOptions::new(2)
let vm_segment_parent = FrameAllocOptions::new(2)
.is_contiguous(true)
.alloc_contiguous()
.unwrap();
@ -229,7 +229,7 @@ mod test {
#[ktest]
fn read_and_write() {
let vm_segment = VmAllocOptions::new(2)
let vm_segment = FrameAllocOptions::new(2)
.is_contiguous(true)
.alloc_contiguous()
.unwrap();
@ -244,7 +244,7 @@ mod test {
#[ktest]
fn reader_and_wirter() {
let vm_segment = VmAllocOptions::new(2)
let vm_segment = FrameAllocOptions::new(2)
.is_contiguous(true)
.alloc_contiguous()
.unwrap();

View File

@ -294,11 +294,11 @@ mod test {
use alloc::vec;
use super::*;
use crate::mm::VmAllocOptions;
use crate::mm::FrameAllocOptions;
#[ktest]
fn streaming_map() {
let vm_segment = VmAllocOptions::new(1)
let vm_segment = FrameAllocOptions::new(1)
.is_contiguous(true)
.alloc_contiguous()
.unwrap();
@ -309,7 +309,7 @@ mod test {
#[ktest]
fn duplicate_map() {
let vm_segment_parent = VmAllocOptions::new(2)
let vm_segment_parent = FrameAllocOptions::new(2)
.is_contiguous(true)
.alloc_contiguous()
.unwrap();
@ -322,7 +322,7 @@ mod test {
#[ktest]
fn read_and_write() {
let vm_segment = VmAllocOptions::new(2)
let vm_segment = FrameAllocOptions::new(2)
.is_contiguous(true)
.alloc_contiguous()
.unwrap();
@ -338,7 +338,7 @@ mod test {
#[ktest]
fn reader_and_wirter() {
let vm_segment = VmAllocOptions::new(2)
let vm_segment = FrameAllocOptions::new(2)
.is_contiguous(true)
.alloc_contiguous()
.unwrap();

View File

@ -9,15 +9,15 @@ use crate::{
/// A collection of base page frames (regular physical memory pages).
///
/// For the most parts, `VmFrameVec` is like `Vec<Frame>`. But the
/// For the most parts, `FrameVec` is like `Vec<Frame>`. But the
/// implementation may or may not be based on `Vec`. Having a dedicated
/// type to represent a series of page frames is convenient because,
/// more often than not, one needs to operate on a batch of frames rather
/// a single frame.
#[derive(Debug, Clone)]
pub struct VmFrameVec(pub(crate) Vec<Frame>);
pub struct FrameVec(pub(crate) Vec<Frame>);
impl VmFrameVec {
impl FrameVec {
pub fn get(&self, index: usize) -> Option<&Frame> {
self.0.get(index)
}
@ -47,7 +47,7 @@ impl VmFrameVec {
}
/// Append some frames.
pub fn append(&mut self, more: &mut VmFrameVec) -> Result<()> {
pub fn append(&mut self, more: &mut FrameVec) -> Result<()> {
self.0.append(&mut more.0);
Ok(())
}
@ -89,7 +89,7 @@ impl VmFrameVec {
}
}
impl IntoIterator for VmFrameVec {
impl IntoIterator for FrameVec {
type Item = Frame;
type IntoIter = alloc::vec::IntoIter<Self::Item>;
@ -99,7 +99,7 @@ impl IntoIterator for VmFrameVec {
}
}
impl VmIo for VmFrameVec {
impl VmIo for FrameVec {
fn read_bytes(&self, offset: usize, buf: &mut [u8]) -> Result<()> {
// Do bound check with potential integer overflow in mind
let max_offset = offset.checked_add(buf.len()).ok_or(Error::Overflow)?;
@ -143,12 +143,12 @@ impl VmIo for VmFrameVec {
/// An iterator for frames.
pub struct FrameVecIter<'a> {
frames: &'a VmFrameVec,
frames: &'a FrameVec,
current: usize,
}
impl<'a> FrameVecIter<'a> {
pub fn new(frames: &'a VmFrameVec) -> Self {
pub fn new(frames: &'a FrameVec) -> Self {
Self { frames, current: 0 }
}
}

View File

@ -1,8 +1,23 @@
// SPDX-License-Identifier: MPL-2.0
//! Untyped physical memory management.
//!
//! A frame is a special page (defined in [`super::page`]) that is _untyped_
//! memory. It is used to store data irrelevant to the integrity of the kernel.
//! All pages mapped to the virtual address space of the users are backed by
//! frames. Frames, with all the properties of pages, can additionally be safely
//! read and written by the kernel or the user.
pub mod frame_vec;
pub mod options;
pub mod segment;
use core::mem::ManuallyDrop;
use super::{
pub use frame_vec::{FrameVec, FrameVecIter};
pub use segment::Segment;
use super::page::{
allocator,
meta::{FrameMeta, MetaSlot, PageMeta, PageUsage},
Page,

View File

@ -1,7 +1,7 @@
// SPDX-License-Identifier: MPL-2.0
use super::{page::allocator, Frame, Segment, VmFrameVec};
use crate::{prelude::*, Error};
use super::{Frame, FrameVec, Segment};
use crate::{mm::page::allocator, prelude::*, Error};
/// Options for allocating physical memory pages (or frames).
///
@ -10,13 +10,13 @@ use crate::{prelude::*, Error};
/// may store Rust objects or affect Rust memory safety, e.g.,
/// the code and data segments of the OS kernel, the stack and heap
/// allocated for the OS kernel.
pub struct VmAllocOptions {
pub struct FrameAllocOptions {
nframes: usize,
is_contiguous: bool,
uninit: bool,
}
impl VmAllocOptions {
impl FrameAllocOptions {
/// Creates new options for allocating the specified number of frames.
pub fn new(nframes: usize) -> Self {
Self {
@ -46,7 +46,7 @@ impl VmAllocOptions {
}
/// Allocate a collection of page frames according to the given options.
pub fn alloc(&self) -> Result<VmFrameVec> {
pub fn alloc(&self) -> Result<FrameVec> {
let frames = if self.is_contiguous {
allocator::alloc(self.nframes).ok_or(Error::NoMemory)?
} else {
@ -54,7 +54,7 @@ impl VmAllocOptions {
for _ in 0..self.nframes {
frame_list.push(allocator::alloc_single().ok_or(Error::NoMemory)?);
}
VmFrameVec(frame_list)
FrameVec(frame_list)
};
if !self.uninit {
for frame in frames.iter() {
@ -102,9 +102,9 @@ impl VmAllocOptions {
fn test_alloc_dealloc() {
// Here we allocate and deallocate frames in random orders to test the allocator.
// We expect the test to fail if the underlying implementation panics.
let single_options = VmAllocOptions::new(1);
let multi_options = VmAllocOptions::new(10);
let mut contiguous_options = VmAllocOptions::new(10);
let single_options = FrameAllocOptions::new(1);
let multi_options = FrameAllocOptions::new(10);
let mut contiguous_options = FrameAllocOptions::new(10);
contiguous_options.is_contiguous(true);
let mut remember_vec = Vec::new();
for i in 0..10 {

View File

@ -2,19 +2,22 @@
use core::ops::Range;
use super::{
allocator,
meta::{PageMeta, PageUsage, SegmentHeadMeta},
Frame, Page,
};
use super::Frame;
use crate::{
mm::{HasPaddr, Paddr, VmIo, VmReader, VmWriter, PAGE_SIZE},
mm::{
page::{
allocator,
meta::{PageMeta, PageUsage, SegmentHeadMeta},
Page,
},
HasPaddr, Paddr, VmIo, VmReader, VmWriter, PAGE_SIZE,
},
Error, Result,
};
/// A handle to a contiguous range of page frames (physical memory pages).
///
/// The biggest difference between `Segment` and `VmFrameVec` is that
/// The biggest difference between `Segment` and `FrameVec` is that
/// the page frames must be contiguous for `Segment`.
///
/// A cloned `Segment` refers to the same page frames as the original.
@ -24,7 +27,7 @@ use crate::{
/// #Example
///
/// ```rust
/// let vm_segment = VmAllocOptions::new(2)
/// let vm_segment = FrameAllocOptions::new(2)
/// .is_contiguous(true)
/// .alloc_contiguous()?;
/// vm_segment.write_bytes(0, buf)?;

View File

@ -9,7 +9,7 @@ use pod::Pod;
use crate::prelude::*;
/// A trait that enables reading/writing data from/to a VM object,
/// e.g., `VmSpace`, `VmFrameVec`, and `Frame`.
/// e.g., `VmSpace`, `FrameVec`, and `Frame`.
///
/// # Concurrency
///

View File

@ -9,11 +9,11 @@ pub type Vaddr = usize;
pub type Paddr = usize;
pub(crate) mod dma;
pub mod frame;
pub(crate) mod heap_allocator;
mod io;
pub(crate) mod kspace;
mod offset;
mod options;
pub(crate) mod page;
pub(crate) mod page_prop;
pub(crate) mod page_table;
@ -26,9 +26,8 @@ use spin::Once;
pub use self::{
dma::{Daddr, DmaCoherent, DmaDirection, DmaStream, DmaStreamSlice, HasDaddr},
frame::{options::FrameAllocOptions, Frame, FrameVec, FrameVecIter, Segment},
io::{VmIo, VmReader, VmWriter},
options::VmAllocOptions,
page::{Frame, FrameVecIter, Segment, VmFrameVec},
page_prop::{CachePolicy, PageFlags, PageProperty},
space::{VmMapOptions, VmSpace},
};

View File

@ -1,5 +1,10 @@
// SPDX-License-Identifier: MPL-2.0
//! The physical page memory allocator.
//!
//! TODO: Decouple it with the frame allocator in [`crate::mm::frame::options`] by
//! allocating pages rather untyped memory from this module.
use alloc::vec::Vec;
use align_ext::AlignExt;
@ -7,12 +12,16 @@ use buddy_system_allocator::FrameAllocator;
use log::info;
use spin::Once;
use super::{meta::FrameMeta, Frame, Page, Segment, VmFrameVec};
use crate::{boot::memory_region::MemoryRegionType, mm::PAGE_SIZE, sync::SpinLock};
use super::{meta::FrameMeta, Page};
use crate::{
boot::memory_region::MemoryRegionType,
mm::{Frame, FrameVec, Segment, PAGE_SIZE},
sync::SpinLock,
};
pub(in crate::mm) static FRAME_ALLOCATOR: Once<SpinLock<FrameAllocator>> = Once::new();
pub(crate) fn alloc(nframes: usize) -> Option<VmFrameVec> {
pub(crate) fn alloc(nframes: usize) -> Option<FrameVec> {
FRAME_ALLOCATOR
.get()
.unwrap()
@ -27,7 +36,7 @@ pub(crate) fn alloc(nframes: usize) -> Option<VmFrameVec> {
};
vector.push(frame);
}
VmFrameVec(vector)
FrameVec(vector)
})
}

View File

@ -82,7 +82,7 @@ pub enum PageUsage {
}
#[repr(C)]
pub(super) struct MetaSlot {
pub(in crate::mm) struct MetaSlot {
/// The metadata of the page.
///
/// The implementation may cast a `*const MetaSlot` to a `*const PageMeta`.
@ -139,7 +139,7 @@ impl Sealed for FrameMeta {}
#[repr(C)]
pub struct SegmentHeadMeta {
/// Length of the segment in bytes.
pub(super) seg_len: u64,
pub(in crate::mm) seg_len: u64,
}
impl Sealed for SegmentHeadMeta {}

View File

@ -1,33 +1,31 @@
// SPDX-License-Identifier: MPL-2.0
//! Managing pages or frames.
//! Physical memory page management.
//!
//! A page is an aligned, contiguous range of bytes in physical memory. The sizes
//! of base pages and huge pages are architecture-dependent. A page can be mapped
//! to a virtual address using the page table.
//!
//! A frame is a special page that is _untyped_ memory. It is used to store data
//! irrelevant to the integrity of the kernel. All pages mapped to the virtual
//! address space of the users are backed by frames.
//! Pages can be accessed through page handles, namely, [`Page`]. A page handle
//! is a reference-counted handle to a page. When all handles to a page are dropped,
//! the page is released and can be reused.
//!
//! Pages can have dedicated metadata, which is implemented in the [`meta`] module.
//! The reference count and usage of a page are stored in the metadata as well, leaving
//! the handle only a pointer to the metadata.
pub(crate) mod allocator;
mod frame;
pub(in crate::mm) mod meta;
mod segment;
mod vm_frame_vec;
use core::{
marker::PhantomData,
sync::atomic::{AtomicU32, AtomicUsize, Ordering},
};
pub use frame::Frame;
use meta::{mapping, MetaSlot, PageMeta};
pub use segment::Segment;
pub use vm_frame_vec::{FrameVecIter, VmFrameVec};
use super::PAGE_SIZE;
use crate::mm::{paddr_to_vaddr, Paddr, PagingConsts, Vaddr};
use crate::mm::{Paddr, PagingConsts, Vaddr};
static MAX_PADDR: AtomicUsize = AtomicUsize::new(0);
@ -35,8 +33,8 @@ static MAX_PADDR: AtomicUsize = AtomicUsize::new(0);
/// whose metadata is represented by `M`.
#[derive(Debug)]
pub struct Page<M: PageMeta> {
ptr: *const MetaSlot,
_marker: PhantomData<M>,
pub(super) ptr: *const MetaSlot,
pub(super) _marker: PhantomData<M>,
}
unsafe impl<M: PageMeta> Send for Page<M> {}

View File

@ -101,10 +101,10 @@ fn test_boot_pt() {
use super::page_walk;
use crate::{
arch::mm::{PageTableEntry, PagingConsts},
mm::{CachePolicy, PageFlags, VmAllocOptions},
mm::{CachePolicy, FrameAllocOptions, PageFlags},
};
let root_frame = VmAllocOptions::new(1).alloc_single().unwrap();
let root_frame = FrameAllocOptions::new(1).alloc_single().unwrap();
let root_paddr = root_frame.start_paddr();
let mut boot_pt = BootPageTable::<PageTableEntry, PagingConsts> {

View File

@ -122,7 +122,7 @@ where
// Create a guard array that only hold the root node lock.
let guards = core::array::from_fn(|i| {
if i == 0 {
Some(pt.root.copy_handle().lock())
Some(pt.root.clone_shallow().lock())
} else {
None
}
@ -313,7 +313,7 @@ where
// Drop the lock on the guard level.
self.guards[C::NR_LEVELS - self.guard_level] = None;
// Re-walk the page table to retreive the locks.
self.guards[0] = Some(self.pt.root.copy_handle().lock());
self.guards[0] = Some(self.pt.root.clone_shallow().lock());
self.level = C::NR_LEVELS;
let cur_pte = self.read_cur_pte();
let cur_child_is_pt = cur_pte.is_present() && !cur_pte.is_last(self.level);

View File

@ -135,7 +135,7 @@ impl PageTable<KernelMode> {
/// Then, one can use a user page table to call [`fork_copy_on_write`], creating
/// other child page tables.
pub(crate) fn create_user_page_table(&self) -> PageTable<UserMode> {
let root_frame = self.root.copy_handle().lock();
let root_frame = self.root.clone_shallow().lock();
const NR_PTES_PER_NODE: usize = nr_subpage_per_huge::<PagingConsts>();
let new_root_frame =
unsafe { root_frame.make_copy(0..0, NR_PTES_PER_NODE / 2..NR_PTES_PER_NODE) };
@ -157,7 +157,7 @@ impl PageTable<KernelMode> {
debug_assert!(start < NR_PTES_PER_NODE);
let end = root_index.end;
debug_assert!(end <= NR_PTES_PER_NODE);
let mut root_frame = self.root.copy_handle().lock();
let mut root_frame = self.root.clone_shallow().lock();
for i in start..end {
if !root_frame.read_pte(i).is_present() {
let frame = PageTableNode::alloc(PagingConsts::NR_LEVELS - 1);
@ -254,7 +254,7 @@ where
/// This is only useful for IOMMU page tables. Think twice before using it in other cases.
pub(crate) unsafe fn shallow_copy(&self) -> Self {
PageTable {
root: self.root.copy_handle(),
root: self.root.clone_shallow(),
_phantom: PhantomData,
}
}

View File

@ -89,7 +89,7 @@ where
}
/// Create a copy of the handle.
pub(super) fn copy_handle(&self) -> Self {
pub(super) fn clone_shallow(&self) -> Self {
self.inc_ref();
Self {
raw: self.raw,
@ -321,7 +321,7 @@ where
for i in deep {
match self.child(i, /*meaningless*/ true) {
Child::PageTable(pt) => {
let guard = pt.copy_handle().lock();
let guard = pt.clone_shallow().lock();
let new_child = guard.make_copy(0..nr_subpage_per_huge::<C>(), 0..0);
new_frame.set_child_pt(i, new_child.into_raw(), /*meaningless*/ true);
}
@ -339,7 +339,7 @@ where
debug_assert_eq!(self.level(), C::NR_LEVELS);
match self.child(i, /*meaningless*/ true) {
Child::PageTable(pt) => {
new_frame.set_child_pt(i, pt.copy_handle(), /*meaningless*/ true);
new_frame.set_child_pt(i, pt.clone_shallow(), /*meaningless*/ true);
}
Child::None => {}
Child::Frame(_) | Child::Untracked(_) => {

View File

@ -6,7 +6,7 @@ use super::*;
use crate::mm::{
kspace::LINEAR_MAPPING_BASE_VADDR,
page_prop::{CachePolicy, PageFlags},
VmAllocOptions,
FrameAllocOptions,
};
const PAGE_SIZE: usize = 4096;
@ -17,7 +17,7 @@ fn test_range_check() {
let good_va = 0..PAGE_SIZE;
let bad_va = 0..PAGE_SIZE + 1;
let bad_va2 = LINEAR_MAPPING_BASE_VADDR..LINEAR_MAPPING_BASE_VADDR + PAGE_SIZE;
let to = VmAllocOptions::new(1).alloc().unwrap();
let to = FrameAllocOptions::new(1).alloc().unwrap();
assert!(pt.cursor_mut(&good_va).is_ok());
assert!(pt.cursor_mut(&bad_va).is_err());
assert!(pt.cursor_mut(&bad_va2).is_err());
@ -31,7 +31,7 @@ fn test_tracked_map_unmap() {
let pt = PageTable::<UserMode>::empty();
let from = PAGE_SIZE..PAGE_SIZE * 2;
let frame = VmAllocOptions::new(1).alloc_single().unwrap();
let frame = FrameAllocOptions::new(1).alloc_single().unwrap();
let start_paddr = frame.start_paddr();
let prop = PageProperty::new(PageFlags::RW, CachePolicy::Writeback);
unsafe { pt.cursor_mut(&from).unwrap().map(frame.clone(), prop) };
@ -75,7 +75,7 @@ fn test_untracked_map_unmap() {
fn test_user_copy_on_write() {
let pt = PageTable::<UserMode>::empty();
let from = PAGE_SIZE..PAGE_SIZE * 2;
let frame = VmAllocOptions::new(1).alloc_single().unwrap();
let frame = FrameAllocOptions::new(1).alloc_single().unwrap();
let start_paddr = frame.start_paddr();
let prop = PageProperty::new(PageFlags::RW, CachePolicy::Writeback);
unsafe { pt.cursor_mut(&from).unwrap().map(frame.clone(), prop) };
@ -131,7 +131,7 @@ fn test_base_protect_query() {
let from_ppn = 1..1000;
let from = PAGE_SIZE * from_ppn.start..PAGE_SIZE * from_ppn.end;
let to = VmAllocOptions::new(999).alloc().unwrap();
let to = FrameAllocOptions::new(999).alloc().unwrap();
let prop = PageProperty::new(PageFlags::RW, CachePolicy::Writeback);
unsafe {
let mut cursor = pt.cursor_mut(&from).unwrap();

View File

@ -6,7 +6,7 @@ use super::{
is_page_aligned,
kspace::KERNEL_PAGE_TABLE,
page_table::{PageTable, PageTableMode, UserMode},
CachePolicy, PageFlags, PageProperty, PagingConstsTrait, PrivilegedPageFlags, VmFrameVec,
CachePolicy, FrameVec, PageFlags, PageProperty, PagingConstsTrait, PrivilegedPageFlags,
PAGE_SIZE,
};
use crate::{
@ -65,7 +65,7 @@ impl VmSpace {
/// The ownership of the frames will be transferred to the `VmSpace`.
///
/// For more information, see `VmMapOptions`.
pub fn map(&self, frames: VmFrameVec, options: &VmMapOptions) -> Result<Vaddr> {
pub fn map(&self, frames: FrameVec, options: &VmMapOptions) -> Result<Vaddr> {
if options.addr.is_none() {
return Err(Error::InvalidArgs);
}

View File

@ -12,7 +12,7 @@ use super::{
pub(crate) use crate::arch::task::{context_switch, TaskContext};
use crate::{
cpu::CpuSet,
mm::{kspace::KERNEL_PAGE_TABLE, PageFlags, Segment, VmAllocOptions, PAGE_SIZE},
mm::{kspace::KERNEL_PAGE_TABLE, FrameAllocOptions, PageFlags, Segment, PAGE_SIZE},
prelude::*,
sync::{SpinLock, SpinLockGuard},
user::UserSpace,
@ -42,7 +42,7 @@ pub struct KernelStack {
impl KernelStack {
pub fn new() -> Result<Self> {
Ok(Self {
segment: VmAllocOptions::new(KERNEL_STACK_SIZE / PAGE_SIZE).alloc_contiguous()?,
segment: FrameAllocOptions::new(KERNEL_STACK_SIZE / PAGE_SIZE).alloc_contiguous()?,
has_guard_page: false,
})
}
@ -51,7 +51,7 @@ impl KernelStack {
/// An additional page is allocated and be regarded as a guard page, which should not be accessed.
pub fn new_with_guard_page() -> Result<Self> {
let stack_segment =
VmAllocOptions::new(KERNEL_STACK_SIZE / PAGE_SIZE + 1).alloc_contiguous()?;
FrameAllocOptions::new(KERNEL_STACK_SIZE / PAGE_SIZE + 1).alloc_contiguous()?;
// FIXME: modifying the the linear mapping is bad.
let page_table = KERNEL_PAGE_TABLE.get().unwrap();
let guard_page_vaddr = {