refactor current implementations with vmar and vmo

This commit is contained in:
Jianfeng Jiang
2022-12-07 19:22:37 +08:00
parent 52531c6cb6
commit 174fdc07ea
88 changed files with 1766 additions and 1362 deletions

View File

@ -6,6 +6,7 @@ use core::mem::MaybeUninit;
use crate::debug;
use crate::trap::{CalleeRegs, CallerRegs, SyscallFrame, TrapFrame};
use crate::x86_64_util::rdfsbase;
use pod::Pod;
/// Defines a CPU-local variable.
@ -156,7 +157,7 @@ impl From<TrapFrame> for CpuContext {
rip: trap.rip,
rflag: trap.rflags,
},
fs_base: 0,
fs_base: rdfsbase(),
fp_regs: FpRegs::default(),
trap_information: TrapInformation {
cr2: trap.cr2,

View File

@ -1,5 +1,6 @@
use super::{page_table::PageTable, *};
use crate::prelude::*;
use crate::vm::VmIo;
use crate::{
config::PAGE_SIZE,
mm::address::is_aligned,

View File

@ -7,5 +7,6 @@ pub(crate) use alloc::sync::Arc;
pub(crate) use alloc::vec::Vec;
pub(crate) use core::any::Any;
pub use crate::debug;
pub(crate) use crate::util::AlignExt;
pub use crate::vm::{Paddr, Vaddr};

View File

@ -38,7 +38,7 @@ __trap_entry:
push r12
push rbp
push rbx
mov rdi, 0
# mov rdi, 0
push rdi
save
# save cr2

View File

@ -245,7 +245,7 @@ impl VmAllocOptions {
}
}
#[derive(Debug, Clone)]
#[derive(Debug)]
/// A handle to a page frame.
///
/// An instance of `VmFrame` is a handle to a page frame (a physical memory
@ -260,6 +260,14 @@ pub struct VmFrame {
pub(crate) physical_frame: Arc<PhysFrame>,
}
impl Clone for VmFrame {
fn clone(&self) -> Self {
Self {
physical_frame: self.physical_frame.clone(),
}
}
}
impl VmFrame {
/// Creates a new VmFrame.
///

View File

@ -22,15 +22,17 @@ use super::VmIo;
/// A newly-created `VmSpace` is not backed by any physical memory pages.
/// To provide memory pages for a `VmSpace`, one can allocate and map
/// physical memory (`VmFrames`) to the `VmSpace`.
#[derive(Debug, Clone)]
pub struct VmSpace {
memory_set: Mutex<MemorySet>,
memory_set: Arc<Mutex<MemorySet>>,
}
impl VmSpace {
/// Creates a new VM address space.
pub fn new() -> Self {
Self {
memory_set: Mutex::new(MemorySet::new()),
memory_set: Arc::new(Mutex::new(MemorySet::new())),
}
}
/// Activate the page table, load root physical address to cr3
@ -55,6 +57,7 @@ impl VmSpace {
if options.addr.is_none() {
return Err(Error::InvalidArgs);
}
// debug!("map to vm space: 0x{:x}", options.addr.unwrap());
self.memory_set.lock().map(MapArea::new(
VirtAddr(options.addr.unwrap()),
frames.len() * PAGE_SIZE,
@ -108,15 +111,6 @@ impl Default for VmSpace {
}
}
impl Clone for VmSpace {
fn clone(&self) -> Self {
let memory_set = self.memory_set.lock().clone();
VmSpace {
memory_set: Mutex::new(memory_set),
}
}
}
impl VmIo for VmSpace {
fn read_bytes(&self, vaddr: usize, buf: &mut [u8]) -> Result<()> {
self.memory_set.lock().read_bytes(vaddr, buf)
@ -133,8 +127,12 @@ impl VmIo for VmSpace {
pub struct VmMapOptions {
/// start virtual address
addr: Option<Vaddr>,
/// map align
align: usize,
/// permission
perm: VmPerm,
/// can overwrite
can_overwrite: bool,
}
impl VmMapOptions {
@ -142,7 +140,9 @@ impl VmMapOptions {
pub fn new() -> Self {
Self {
addr: None,
align: PAGE_SIZE,
perm: VmPerm::empty(),
can_overwrite: false,
}
}
@ -153,7 +153,8 @@ impl VmMapOptions {
///
/// The default value of this option is the page size.
pub fn align(&mut self, align: usize) -> &mut Self {
todo!()
self.align = align;
self
}
/// Sets the permissions of the mapping, which affects whether
@ -182,7 +183,8 @@ impl VmMapOptions {
///
/// The default value of this option is `false`.
pub fn can_overwrite(&mut self, can_overwrite: bool) -> &mut Self {
todo!()
self.can_overwrite = can_overwrite;
self
}
}