|
|
@ -1,5 +1,4 @@
|
|
|
|
//! memory set, area
|
|
|
|
//! Memory management structures
|
|
|
|
//! and the inactive page table
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use alloc::{boxed::Box, string::String, vec::Vec};
|
|
|
|
use alloc::{boxed::Box, string::String, vec::Vec};
|
|
|
|
use core::fmt::{Debug, Error, Formatter};
|
|
|
|
use core::fmt::{Debug, Error, Formatter};
|
|
|
@ -13,8 +12,7 @@ use self::handler::MemoryHandler;
|
|
|
|
|
|
|
|
|
|
|
|
pub mod handler;
|
|
|
|
pub mod handler;
|
|
|
|
|
|
|
|
|
|
|
|
/// a continuous memory space when the same attribute
|
|
|
|
/// A continuous memory space when the same attribute
|
|
|
|
/// like `vma_struct` in ucore
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct MemoryArea {
|
|
|
|
pub struct MemoryArea {
|
|
|
|
start_addr: VirtAddr,
|
|
|
|
start_addr: VirtAddr,
|
|
|
@ -25,31 +23,7 @@ pub struct MemoryArea {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl MemoryArea {
|
|
|
|
impl MemoryArea {
|
|
|
|
/*
|
|
|
|
/// Test whether a virtual address is in the memory area
|
|
|
|
** @brief get slice of the content in the memory area
|
|
|
|
|
|
|
|
** @retval &[u8] the slice of the content in the memory area
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
pub unsafe fn as_slice(&self) -> &[u8] {
|
|
|
|
|
|
|
|
::core::slice::from_raw_parts(
|
|
|
|
|
|
|
|
self.start_addr as *const u8,
|
|
|
|
|
|
|
|
self.end_addr - self.start_addr,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
** @brief get mutable slice of the content in the memory area
|
|
|
|
|
|
|
|
** @retval &mut[u8] the mutable slice of the content in the memory area
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
pub unsafe fn as_slice_mut(&self) -> &mut [u8] {
|
|
|
|
|
|
|
|
::core::slice::from_raw_parts_mut(
|
|
|
|
|
|
|
|
self.start_addr as *mut u8,
|
|
|
|
|
|
|
|
self.end_addr - self.start_addr,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
** @brief test whether a virtual address is in the memory area
|
|
|
|
|
|
|
|
** @param addr: VirtAddr the virtual address to test
|
|
|
|
|
|
|
|
** @retval bool whether the virtual address is in the memory area
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
pub fn contains(&self, addr: VirtAddr) -> bool {
|
|
|
|
pub fn contains(&self, addr: VirtAddr) -> bool {
|
|
|
|
addr >= self.start_addr && addr < self.end_addr
|
|
|
|
addr >= self.start_addr && addr < self.end_addr
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -75,20 +49,6 @@ impl MemoryArea {
|
|
|
|
self.check_read_array(ptr, count)
|
|
|
|
self.check_read_array(ptr, count)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Check the null-end C string is within the readable memory, and is valid.
|
|
|
|
|
|
|
|
/// If so, clone it to a String.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// Unsafe: the page table must be active.
|
|
|
|
|
|
|
|
pub unsafe fn check_and_clone_cstr(&self, ptr: *const u8) -> Option<String> {
|
|
|
|
|
|
|
|
if ptr as usize >= self.end_addr {
|
|
|
|
|
|
|
|
return None;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
let max_len = self.end_addr - ptr as usize;
|
|
|
|
|
|
|
|
(0..max_len)
|
|
|
|
|
|
|
|
.find(|&i| ptr.offset(i as isize).read() == 0)
|
|
|
|
|
|
|
|
.and_then(|len| core::str::from_utf8(core::slice::from_raw_parts(ptr, len)).ok())
|
|
|
|
|
|
|
|
.map(|s| String::from(s))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Test whether this area is (page) overlap with area [`start_addr`, `end_addr`]
|
|
|
|
/// Test whether this area is (page) overlap with area [`start_addr`, `end_addr`]
|
|
|
|
pub fn is_overlap_with(&self, start_addr: VirtAddr, end_addr: VirtAddr) -> bool {
|
|
|
|
pub fn is_overlap_with(&self, start_addr: VirtAddr, end_addr: VirtAddr) -> bool {
|
|
|
|
let p0 = Page::of_addr(self.start_addr);
|
|
|
|
let p0 = Page::of_addr(self.start_addr);
|
|
|
@ -121,42 +81,22 @@ pub struct MemoryAttr {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl MemoryAttr {
|
|
|
|
impl MemoryAttr {
|
|
|
|
/*
|
|
|
|
|
|
|
|
** @brief set the memory attribute's user bit
|
|
|
|
|
|
|
|
** @retval MemoryAttr the memory attribute itself
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
pub fn user(mut self) -> Self {
|
|
|
|
pub fn user(mut self) -> Self {
|
|
|
|
self.user = true;
|
|
|
|
self.user = true;
|
|
|
|
self
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
|
|
|
|
** @brief set the memory attribute's readonly bit
|
|
|
|
|
|
|
|
** @retval MemoryAttr the memory attribute itself
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
pub fn readonly(mut self) -> Self {
|
|
|
|
pub fn readonly(mut self) -> Self {
|
|
|
|
self.readonly = true;
|
|
|
|
self.readonly = true;
|
|
|
|
self
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
|
|
|
|
** @brief unset the memory attribute's readonly bit
|
|
|
|
|
|
|
|
** @retval MemoryAttr the memory attribute itself
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
pub fn writable(mut self) -> Self {
|
|
|
|
pub fn writable(mut self) -> Self {
|
|
|
|
self.readonly = false;
|
|
|
|
self.readonly = false;
|
|
|
|
self
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
|
|
|
|
** @brief set the memory attribute's execute bit
|
|
|
|
|
|
|
|
** @retval MemoryAttr the memory attribute itself
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
pub fn execute(mut self) -> Self {
|
|
|
|
pub fn execute(mut self) -> Self {
|
|
|
|
self.execute = true;
|
|
|
|
self.execute = true;
|
|
|
|
self
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
|
|
|
|
** @brief set the MMIO type
|
|
|
|
|
|
|
|
** @retval MemoryAttr the memory attribute itself
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
pub fn mmio(mut self, value: u8) -> Self {
|
|
|
|
pub fn mmio(mut self, value: u8) -> Self {
|
|
|
|
self.mmio = value;
|
|
|
|
self.mmio = value;
|
|
|
|
self
|
|
|
|
self
|
|
|
@ -172,26 +112,24 @@ impl MemoryAttr {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// set of memory space with multiple memory area with associated page table and stack space
|
|
|
|
/// A set of memory space with multiple memory areas with associated page table
|
|
|
|
/// like `mm_struct` in ucore
|
|
|
|
|
|
|
|
/// NOTE: Don't remove align(64), or you will fail to run MIPS.
|
|
|
|
/// NOTE: Don't remove align(64), or you will fail to run MIPS.
|
|
|
|
#[repr(align(64))]
|
|
|
|
/// Temporary solution for rv64
|
|
|
|
pub struct MemorySet<T: InactivePageTable> {
|
|
|
|
#[cfg_attr(not(target_arch = "riscv64"), repr(align(64)))]
|
|
|
|
|
|
|
|
pub struct MemorySet<T: PageTableExt> {
|
|
|
|
areas: Vec<MemoryArea>,
|
|
|
|
areas: Vec<MemoryArea>,
|
|
|
|
page_table: T,
|
|
|
|
page_table: T,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl<T: InactivePageTable> MemorySet<T> {
|
|
|
|
impl<T: PageTableExt> MemorySet<T> {
|
|
|
|
/*
|
|
|
|
/// Create a new `MemorySet`
|
|
|
|
** @brief create a memory set
|
|
|
|
|
|
|
|
** @retval MemorySet<T> the memory set created
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
MemorySet {
|
|
|
|
MemorySet {
|
|
|
|
areas: Vec::new(),
|
|
|
|
areas: Vec::new(),
|
|
|
|
page_table: T::new(),
|
|
|
|
page_table: T::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a new `MemorySet` for kernel remap
|
|
|
|
pub fn new_bare() -> Self {
|
|
|
|
pub fn new_bare() -> Self {
|
|
|
|
MemorySet {
|
|
|
|
MemorySet {
|
|
|
|
areas: Vec::new(),
|
|
|
|
areas: Vec::new(),
|
|
|
@ -236,35 +174,6 @@ impl<T: InactivePageTable> MemorySet<T> {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(VMError::InvalidPtr)
|
|
|
|
Err(VMError::InvalidPtr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Check the null-end C string pointer array
|
|
|
|
|
|
|
|
/// Used for getting argv & envp
|
|
|
|
|
|
|
|
pub unsafe fn check_and_clone_cstr_array(
|
|
|
|
|
|
|
|
&self,
|
|
|
|
|
|
|
|
mut argv: *const *const u8,
|
|
|
|
|
|
|
|
) -> VMResult<Vec<String>> {
|
|
|
|
|
|
|
|
let mut args = Vec::new();
|
|
|
|
|
|
|
|
loop {
|
|
|
|
|
|
|
|
let cstr = *self.check_read_ptr(argv)?;
|
|
|
|
|
|
|
|
if cstr.is_null() {
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
let arg = self.check_and_clone_cstr(cstr)?;
|
|
|
|
|
|
|
|
args.push(arg);
|
|
|
|
|
|
|
|
argv = argv.add(1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(args)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check the null-end C string is within the readable memory, and is valid.
|
|
|
|
|
|
|
|
/// If so, clone it to a String.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// Unsafe: the page table must be active.
|
|
|
|
|
|
|
|
pub unsafe fn check_and_clone_cstr(&self, ptr: *const u8) -> VMResult<String> {
|
|
|
|
|
|
|
|
self.areas
|
|
|
|
|
|
|
|
.iter()
|
|
|
|
|
|
|
|
.filter_map(|area| area.check_and_clone_cstr(ptr))
|
|
|
|
|
|
|
|
.next()
|
|
|
|
|
|
|
|
.ok_or(VMError::InvalidPtr)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Find a free area with hint address `addr_hint` and length `len`.
|
|
|
|
/// Find a free area with hint address `addr_hint` and length `len`.
|
|
|
|
/// Return the start address of found free area.
|
|
|
|
/// Return the start address of found free area.
|
|
|
|
/// Used for mmap.
|
|
|
|
/// Used for mmap.
|
|
|
@ -284,11 +193,7 @@ impl<T: InactivePageTable> MemorySet<T> {
|
|
|
|
.find(|area| area.is_overlap_with(start_addr, end_addr))
|
|
|
|
.find(|area| area.is_overlap_with(start_addr, end_addr))
|
|
|
|
.is_none()
|
|
|
|
.is_none()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
/// Add an area to this set
|
|
|
|
** @brief add the memory area to the memory set
|
|
|
|
|
|
|
|
** @param area: MemoryArea the memory area to add
|
|
|
|
|
|
|
|
** @retval none
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
pub fn push(
|
|
|
|
pub fn push(
|
|
|
|
&mut self,
|
|
|
|
&mut self,
|
|
|
|
start_addr: VirtAddr,
|
|
|
|
start_addr: VirtAddr,
|
|
|
@ -309,7 +214,7 @@ impl<T: InactivePageTable> MemorySet<T> {
|
|
|
|
handler: Box::new(handler),
|
|
|
|
handler: Box::new(handler),
|
|
|
|
name,
|
|
|
|
name,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
self.page_table.edit(|pt| area.map(pt));
|
|
|
|
area.map(&mut self.page_table);
|
|
|
|
// keep order by start address
|
|
|
|
// keep order by start address
|
|
|
|
let idx = self
|
|
|
|
let idx = self
|
|
|
|
.areas
|
|
|
|
.areas
|
|
|
@ -321,28 +226,21 @@ impl<T: InactivePageTable> MemorySet<T> {
|
|
|
|
self.areas.insert(idx, area);
|
|
|
|
self.areas.insert(idx, area);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/// Remove the area `[start_addr, end_addr)` from `MemorySet`
|
|
|
|
** @brief remove the memory area from the memory set
|
|
|
|
|
|
|
|
** @param area: MemoryArea the memory area to remove
|
|
|
|
|
|
|
|
** @retval none
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
pub fn pop(&mut self, start_addr: VirtAddr, end_addr: VirtAddr) {
|
|
|
|
pub fn pop(&mut self, start_addr: VirtAddr, end_addr: VirtAddr) {
|
|
|
|
assert!(start_addr <= end_addr, "invalid memory area");
|
|
|
|
assert!(start_addr <= end_addr, "invalid memory area");
|
|
|
|
for i in 0..self.areas.len() {
|
|
|
|
for i in 0..self.areas.len() {
|
|
|
|
if self.areas[i].start_addr == start_addr && self.areas[i].end_addr == end_addr {
|
|
|
|
if self.areas[i].start_addr == start_addr && self.areas[i].end_addr == end_addr {
|
|
|
|
let area = self.areas.remove(i);
|
|
|
|
let area = self.areas.remove(i);
|
|
|
|
self.page_table.edit(|pt| area.unmap(pt));
|
|
|
|
area.unmap(&mut self.page_table);
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
panic!("no memory area found");
|
|
|
|
panic!("no memory area found");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/// Remove the area `[start_addr, end_addr)` from `MemorySet`
|
|
|
|
** @brief remove the memory area from the memory set and split existed ones when necessary
|
|
|
|
/// and split existed ones when necessary.
|
|
|
|
** @param area: MemoryArea the memory area to remove
|
|
|
|
|
|
|
|
** @retval none
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
pub fn pop_with_split(&mut self, start_addr: VirtAddr, end_addr: VirtAddr) {
|
|
|
|
pub fn pop_with_split(&mut self, start_addr: VirtAddr, end_addr: VirtAddr) {
|
|
|
|
assert!(start_addr <= end_addr, "invalid memory area");
|
|
|
|
assert!(start_addr <= end_addr, "invalid memory area");
|
|
|
|
let mut i = 0;
|
|
|
|
let mut i = 0;
|
|
|
@ -351,7 +249,7 @@ impl<T: InactivePageTable> MemorySet<T> {
|
|
|
|
if self.areas[i].start_addr >= start_addr && self.areas[i].end_addr <= end_addr {
|
|
|
|
if self.areas[i].start_addr >= start_addr && self.areas[i].end_addr <= end_addr {
|
|
|
|
// subset
|
|
|
|
// subset
|
|
|
|
let area = self.areas.remove(i);
|
|
|
|
let area = self.areas.remove(i);
|
|
|
|
self.page_table.edit(|pt| area.unmap(pt));
|
|
|
|
area.unmap(&mut self.page_table);
|
|
|
|
i -= 1;
|
|
|
|
i -= 1;
|
|
|
|
} else if self.areas[i].start_addr >= start_addr
|
|
|
|
} else if self.areas[i].start_addr >= start_addr
|
|
|
|
&& self.areas[i].start_addr < end_addr
|
|
|
|
&& self.areas[i].start_addr < end_addr
|
|
|
@ -365,7 +263,7 @@ impl<T: InactivePageTable> MemorySet<T> {
|
|
|
|
handler: area.handler.box_clone(),
|
|
|
|
handler: area.handler.box_clone(),
|
|
|
|
name: area.name,
|
|
|
|
name: area.name,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
self.page_table.edit(|pt| dead_area.unmap(pt));
|
|
|
|
dead_area.unmap(&mut self.page_table);
|
|
|
|
let new_area = MemoryArea {
|
|
|
|
let new_area = MemoryArea {
|
|
|
|
start_addr: end_addr,
|
|
|
|
start_addr: end_addr,
|
|
|
|
end_addr: area.end_addr,
|
|
|
|
end_addr: area.end_addr,
|
|
|
@ -379,13 +277,13 @@ impl<T: InactivePageTable> MemorySet<T> {
|
|
|
|
// postfix
|
|
|
|
// postfix
|
|
|
|
let area = self.areas.remove(i);
|
|
|
|
let area = self.areas.remove(i);
|
|
|
|
let dead_area = MemoryArea {
|
|
|
|
let dead_area = MemoryArea {
|
|
|
|
start_addr: start_addr,
|
|
|
|
start_addr,
|
|
|
|
end_addr: area.end_addr,
|
|
|
|
end_addr: area.end_addr,
|
|
|
|
attr: area.attr,
|
|
|
|
attr: area.attr,
|
|
|
|
handler: area.handler.box_clone(),
|
|
|
|
handler: area.handler.box_clone(),
|
|
|
|
name: area.name,
|
|
|
|
name: area.name,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
self.page_table.edit(|pt| dead_area.unmap(pt));
|
|
|
|
dead_area.unmap(&mut self.page_table);
|
|
|
|
let new_area = MemoryArea {
|
|
|
|
let new_area = MemoryArea {
|
|
|
|
start_addr: area.start_addr,
|
|
|
|
start_addr: area.start_addr,
|
|
|
|
end_addr: start_addr,
|
|
|
|
end_addr: start_addr,
|
|
|
@ -398,13 +296,13 @@ impl<T: InactivePageTable> MemorySet<T> {
|
|
|
|
// superset
|
|
|
|
// superset
|
|
|
|
let area = self.areas.remove(i);
|
|
|
|
let area = self.areas.remove(i);
|
|
|
|
let dead_area = MemoryArea {
|
|
|
|
let dead_area = MemoryArea {
|
|
|
|
start_addr: start_addr,
|
|
|
|
start_addr,
|
|
|
|
end_addr: end_addr,
|
|
|
|
end_addr,
|
|
|
|
attr: area.attr,
|
|
|
|
attr: area.attr,
|
|
|
|
handler: area.handler.box_clone(),
|
|
|
|
handler: area.handler.box_clone(),
|
|
|
|
name: area.name,
|
|
|
|
name: area.name,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
self.page_table.edit(|pt| dead_area.unmap(pt));
|
|
|
|
dead_area.unmap(&mut self.page_table);
|
|
|
|
let new_area_left = MemoryArea {
|
|
|
|
let new_area_left = MemoryArea {
|
|
|
|
start_addr: area.start_addr,
|
|
|
|
start_addr: area.start_addr,
|
|
|
|
end_addr: start_addr,
|
|
|
|
end_addr: start_addr,
|
|
|
@ -428,74 +326,50 @@ impl<T: InactivePageTable> MemorySet<T> {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/// Get iterator of areas
|
|
|
|
** @brief get iterator of the memory area
|
|
|
|
|
|
|
|
** @retval impl Iterator<Item=&MemoryArea>
|
|
|
|
|
|
|
|
** the memory area iterator
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
pub fn iter(&self) -> impl Iterator<Item = &MemoryArea> {
|
|
|
|
pub fn iter(&self) -> impl Iterator<Item = &MemoryArea> {
|
|
|
|
self.areas.iter()
|
|
|
|
self.areas.iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn edit(&mut self, f: impl FnOnce(&mut T::Active)) {
|
|
|
|
|
|
|
|
self.page_table.edit(f);
|
|
|
|
/// Execute function `f` with the associated page table
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
** @brief execute function with the associated page table
|
|
|
|
|
|
|
|
** @param f: impl FnOnce() the function to be executed
|
|
|
|
|
|
|
|
** @retval none
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
pub unsafe fn with(&self, f: impl FnOnce()) {
|
|
|
|
pub unsafe fn with(&self, f: impl FnOnce()) {
|
|
|
|
self.page_table.with(f);
|
|
|
|
self.page_table.with(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
/// Activate the associated page table
|
|
|
|
** @brief activate the associated page table
|
|
|
|
|
|
|
|
** @retval none
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
pub unsafe fn activate(&self) {
|
|
|
|
pub unsafe fn activate(&self) {
|
|
|
|
self.page_table.activate();
|
|
|
|
self.page_table.activate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
|
|
|
|
** @brief get the token of the associated page table
|
|
|
|
/// Get the token of the associated page table
|
|
|
|
** @retval usize the token of the inactive page table
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
pub fn token(&self) -> usize {
|
|
|
|
pub fn token(&self) -> usize {
|
|
|
|
self.page_table.token()
|
|
|
|
self.page_table.token()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
|
|
|
|
** @brief clear the memory set
|
|
|
|
/// Clear and unmap all areas
|
|
|
|
** @retval none
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
pub fn clear(&mut self) {
|
|
|
|
pub fn clear(&mut self) {
|
|
|
|
let Self {
|
|
|
|
let Self {
|
|
|
|
ref mut page_table,
|
|
|
|
ref mut page_table,
|
|
|
|
ref mut areas,
|
|
|
|
ref mut areas,
|
|
|
|
..
|
|
|
|
..
|
|
|
|
} = self;
|
|
|
|
} = self;
|
|
|
|
page_table.edit(|pt| {
|
|
|
|
|
|
|
|
for area in areas.iter() {
|
|
|
|
for area in areas.iter() {
|
|
|
|
area.unmap(pt);
|
|
|
|
area.unmap(page_table);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
areas.clear();
|
|
|
|
areas.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Get physical address of the page of given virtual `addr`
|
|
|
|
/// Get physical address of the page of given virtual `addr`
|
|
|
|
pub fn translate(&mut self, addr: VirtAddr) -> Option<PhysAddr> {
|
|
|
|
pub fn translate(&mut self, addr: VirtAddr) -> Option<PhysAddr> {
|
|
|
|
self.page_table.edit(|pt| {
|
|
|
|
self.page_table.get_entry(addr).and_then(|entry| {
|
|
|
|
pt.get_entry(addr).and_then(|entry| {
|
|
|
|
|
|
|
|
if entry.user() {
|
|
|
|
if entry.user() {
|
|
|
|
Some(entry.target())
|
|
|
|
Some(entry.target())
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/// Get the reference of inner page table
|
|
|
|
** @brief get the mutable reference for the inactive page table
|
|
|
|
|
|
|
|
** @retval: &mut T the mutable reference of the inactive page table
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
pub fn get_page_table_mut(&mut self) -> &mut T {
|
|
|
|
pub fn get_page_table_mut(&mut self) -> &mut T {
|
|
|
|
&mut self.page_table
|
|
|
|
&mut self.page_table
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -503,32 +377,28 @@ impl<T: InactivePageTable> MemorySet<T> {
|
|
|
|
pub fn handle_page_fault(&mut self, addr: VirtAddr) -> bool {
|
|
|
|
pub fn handle_page_fault(&mut self, addr: VirtAddr) -> bool {
|
|
|
|
let area = self.areas.iter().find(|area| area.contains(addr));
|
|
|
|
let area = self.areas.iter().find(|area| area.contains(addr));
|
|
|
|
match area {
|
|
|
|
match area {
|
|
|
|
Some(area) => self
|
|
|
|
Some(area) => area.handler.handle_page_fault(&mut self.page_table, addr),
|
|
|
|
.page_table
|
|
|
|
|
|
|
|
.edit(|pt| area.handler.handle_page_fault(pt, addr)),
|
|
|
|
|
|
|
|
None => false,
|
|
|
|
None => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn clone(&mut self) -> Self {
|
|
|
|
pub fn clone(&mut self) -> Self {
|
|
|
|
let new_page_table = T::new();
|
|
|
|
let mut new_page_table = T::new();
|
|
|
|
let Self {
|
|
|
|
let Self {
|
|
|
|
ref mut page_table,
|
|
|
|
ref mut page_table,
|
|
|
|
ref areas,
|
|
|
|
ref areas,
|
|
|
|
..
|
|
|
|
..
|
|
|
|
} = self;
|
|
|
|
} = self;
|
|
|
|
page_table.edit(|pt| {
|
|
|
|
|
|
|
|
for area in areas.iter() {
|
|
|
|
for area in areas.iter() {
|
|
|
|
for page in Page::range_of(area.start_addr, area.end_addr) {
|
|
|
|
for page in Page::range_of(area.start_addr, area.end_addr) {
|
|
|
|
area.handler.clone_map(
|
|
|
|
area.handler.clone_map(
|
|
|
|
pt,
|
|
|
|
&mut new_page_table,
|
|
|
|
&|f| unsafe { new_page_table.with(f) },
|
|
|
|
page_table,
|
|
|
|
page.start_address(),
|
|
|
|
page.start_address(),
|
|
|
|
&area.attr,
|
|
|
|
&area.attr,
|
|
|
|
);
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
MemorySet {
|
|
|
|
MemorySet {
|
|
|
|
areas: areas.clone(),
|
|
|
|
areas: areas.clone(),
|
|
|
|
page_table: new_page_table,
|
|
|
|
page_table: new_page_table,
|
|
|
@ -536,13 +406,13 @@ impl<T: InactivePageTable> MemorySet<T> {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl<T: InactivePageTable> Drop for MemorySet<T> {
|
|
|
|
impl<T: PageTableExt> Drop for MemorySet<T> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.clear();
|
|
|
|
self.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl<T: InactivePageTable> Debug for MemorySet<T> {
|
|
|
|
impl<T: PageTableExt> Debug for MemorySet<T> {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
|
|
|
|
f.debug_list().entries(self.areas.iter()).finish()
|
|
|
|
f.debug_list().entries(self.areas.iter()).finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|