mirror of
https://github.com/LearningOS/rust-based-os-comp2022.git
synced 2026-07-05 12:06:05 +08:00
add os[1-8]-ref for os refereces, add guide, add README
This commit is contained in:
246
os5-ref/src/mm/address.rs
Normal file
246
os5-ref/src/mm/address.rs
Normal file
@@ -0,0 +1,246 @@
|
||||
//! Implementation of physical and virtual address and page number.
|
||||
use super::PageTableEntry;
|
||||
use crate::config::{PAGE_SIZE, PAGE_SIZE_BITS};
|
||||
use core::fmt::{self, Debug, Formatter};
|
||||
|
||||
/// Definitions
|
||||
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
|
||||
pub struct PhysAddr(pub usize);
|
||||
|
||||
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
|
||||
pub struct VirtAddr(pub usize);
|
||||
|
||||
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
|
||||
pub struct PhysPageNum(pub usize);
|
||||
|
||||
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
|
||||
pub struct VirtPageNum(pub usize);
|
||||
|
||||
/// Debugging
|
||||
|
||||
impl Debug for VirtAddr {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
f.write_fmt(format_args!("VA:{:#x}", self.0))
|
||||
}
|
||||
}
|
||||
impl Debug for VirtPageNum {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
f.write_fmt(format_args!("VPN:{:#x}", self.0))
|
||||
}
|
||||
}
|
||||
impl Debug for PhysAddr {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
f.write_fmt(format_args!("PA:{:#x}", self.0))
|
||||
}
|
||||
}
|
||||
impl Debug for PhysPageNum {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
f.write_fmt(format_args!("PPN:{:#x}", self.0))
|
||||
}
|
||||
}
|
||||
|
||||
/// T: {PhysAddr, VirtAddr, PhysPageNum, VirtPageNum}
|
||||
/// T -> usize: T.0
|
||||
/// usize -> T: usize.into()
|
||||
|
||||
impl From<usize> for PhysAddr {
|
||||
fn from(v: usize) -> Self {
|
||||
Self(v)
|
||||
}
|
||||
}
|
||||
impl From<usize> for PhysPageNum {
|
||||
fn from(v: usize) -> Self {
|
||||
Self(v)
|
||||
}
|
||||
}
|
||||
impl From<usize> for VirtAddr {
|
||||
fn from(v: usize) -> Self {
|
||||
Self(v)
|
||||
}
|
||||
}
|
||||
impl From<usize> for VirtPageNum {
|
||||
fn from(v: usize) -> Self {
|
||||
Self(v)
|
||||
}
|
||||
}
|
||||
impl From<PhysAddr> for usize {
|
||||
fn from(v: PhysAddr) -> Self {
|
||||
v.0
|
||||
}
|
||||
}
|
||||
impl From<PhysPageNum> for usize {
|
||||
fn from(v: PhysPageNum) -> Self {
|
||||
v.0
|
||||
}
|
||||
}
|
||||
impl From<VirtAddr> for usize {
|
||||
fn from(v: VirtAddr) -> Self {
|
||||
v.0
|
||||
}
|
||||
}
|
||||
impl From<VirtPageNum> for usize {
|
||||
fn from(v: VirtPageNum) -> Self {
|
||||
v.0
|
||||
}
|
||||
}
|
||||
|
||||
impl VirtAddr {
|
||||
pub fn floor(&self) -> VirtPageNum {
|
||||
VirtPageNum(self.0 / PAGE_SIZE)
|
||||
}
|
||||
pub fn ceil(&self) -> VirtPageNum {
|
||||
VirtPageNum((self.0 - 1 + PAGE_SIZE) / PAGE_SIZE)
|
||||
}
|
||||
pub fn page_offset(&self) -> usize {
|
||||
self.0 & (PAGE_SIZE - 1)
|
||||
}
|
||||
pub fn aligned(&self) -> bool {
|
||||
self.page_offset() == 0
|
||||
}
|
||||
}
|
||||
impl From<VirtAddr> for VirtPageNum {
|
||||
fn from(v: VirtAddr) -> Self {
|
||||
assert_eq!(v.page_offset(), 0);
|
||||
v.floor()
|
||||
}
|
||||
}
|
||||
impl From<VirtPageNum> for VirtAddr {
|
||||
fn from(v: VirtPageNum) -> Self {
|
||||
Self(v.0 << PAGE_SIZE_BITS)
|
||||
}
|
||||
}
|
||||
impl PhysAddr {
|
||||
pub fn floor(&self) -> PhysPageNum {
|
||||
PhysPageNum(self.0 / PAGE_SIZE)
|
||||
}
|
||||
pub fn ceil(&self) -> PhysPageNum {
|
||||
PhysPageNum((self.0 - 1 + PAGE_SIZE) / PAGE_SIZE)
|
||||
}
|
||||
pub fn page_offset(&self) -> usize {
|
||||
self.0 & (PAGE_SIZE - 1)
|
||||
}
|
||||
pub fn aligned(&self) -> bool {
|
||||
self.page_offset() == 0
|
||||
}
|
||||
}
|
||||
impl From<PhysAddr> for PhysPageNum {
|
||||
fn from(v: PhysAddr) -> Self {
|
||||
assert_eq!(v.page_offset(), 0);
|
||||
v.floor()
|
||||
}
|
||||
}
|
||||
impl From<PhysPageNum> for PhysAddr {
|
||||
fn from(v: PhysPageNum) -> Self {
|
||||
Self(v.0 << PAGE_SIZE_BITS)
|
||||
}
|
||||
}
|
||||
|
||||
impl VirtPageNum {
|
||||
pub fn indexes(&self) -> [usize; 3] {
|
||||
let mut vpn = self.0;
|
||||
let mut idx = [0usize; 3];
|
||||
for i in (0..3).rev() {
|
||||
idx[i] = vpn & 511;
|
||||
vpn >>= 9;
|
||||
}
|
||||
idx
|
||||
}
|
||||
}
|
||||
|
||||
impl PhysAddr {
|
||||
pub fn get_mut<T>(&self) -> &'static mut T {
|
||||
unsafe { (self.0 as *mut T).as_mut().unwrap() }
|
||||
}
|
||||
}
|
||||
impl PhysPageNum {
|
||||
pub fn get_pte_array(&self) -> &'static mut [PageTableEntry] {
|
||||
let pa: PhysAddr = (*self).into();
|
||||
unsafe { core::slice::from_raw_parts_mut(pa.0 as *mut PageTableEntry, 512) }
|
||||
}
|
||||
pub fn get_bytes_array(&self) -> &'static mut [u8] {
|
||||
let pa: PhysAddr = (*self).into();
|
||||
unsafe { core::slice::from_raw_parts_mut(pa.0 as *mut u8, 4096) }
|
||||
}
|
||||
pub fn get_mut<T>(&self) -> &'static mut T {
|
||||
let pa: PhysAddr = (*self).into();
|
||||
pa.get_mut()
|
||||
}
|
||||
}
|
||||
|
||||
pub trait StepByOne {
|
||||
fn step(&mut self);
|
||||
}
|
||||
impl StepByOne for VirtPageNum {
|
||||
fn step(&mut self) {
|
||||
self.0 += 1;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
/// a simple range structure for type T
|
||||
pub struct SimpleRange<T>
|
||||
where
|
||||
T: StepByOne + Copy + PartialEq + PartialOrd + Debug,
|
||||
{
|
||||
l: T,
|
||||
r: T,
|
||||
}
|
||||
impl<T> SimpleRange<T>
|
||||
where
|
||||
T: StepByOne + Copy + PartialEq + PartialOrd + Debug,
|
||||
{
|
||||
pub fn new(start: T, end: T) -> Self {
|
||||
assert!(start <= end, "start {:?} > end {:?}!", start, end);
|
||||
Self { l: start, r: end }
|
||||
}
|
||||
pub fn get_start(&self) -> T {
|
||||
self.l
|
||||
}
|
||||
pub fn get_end(&self) -> T {
|
||||
self.r
|
||||
}
|
||||
}
|
||||
impl<T> IntoIterator for SimpleRange<T>
|
||||
where
|
||||
T: StepByOne + Copy + PartialEq + PartialOrd + Debug,
|
||||
{
|
||||
type Item = T;
|
||||
type IntoIter = SimpleRangeIterator<T>;
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
SimpleRangeIterator::new(self.l, self.r)
|
||||
}
|
||||
}
|
||||
/// iterator for the simple range structure
|
||||
pub struct SimpleRangeIterator<T>
|
||||
where
|
||||
T: StepByOne + Copy + PartialEq + PartialOrd + Debug,
|
||||
{
|
||||
current: T,
|
||||
end: T,
|
||||
}
|
||||
impl<T> SimpleRangeIterator<T>
|
||||
where
|
||||
T: StepByOne + Copy + PartialEq + PartialOrd + Debug,
|
||||
{
|
||||
pub fn new(l: T, r: T) -> Self {
|
||||
Self { current: l, end: r }
|
||||
}
|
||||
}
|
||||
impl<T> Iterator for SimpleRangeIterator<T>
|
||||
where
|
||||
T: StepByOne + Copy + PartialEq + PartialOrd + Debug,
|
||||
{
|
||||
type Item = T;
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.current == self.end {
|
||||
None
|
||||
} else {
|
||||
let t = self.current;
|
||||
self.current.step();
|
||||
Some(t)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// a simple range structure for virtual page number
|
||||
pub type VPNRange = SimpleRange<VirtPageNum>;
|
||||
136
os5-ref/src/mm/frame_allocator.rs
Normal file
136
os5-ref/src/mm/frame_allocator.rs
Normal file
@@ -0,0 +1,136 @@
|
||||
//! Implementation of [`FrameAllocator`] which
|
||||
//! controls all the frames in the operating system.
|
||||
|
||||
use super::{PhysAddr, PhysPageNum};
|
||||
use crate::config::MEMORY_END;
|
||||
use crate::sync::UPSafeCell;
|
||||
use alloc::vec::Vec;
|
||||
use core::fmt::{self, Debug, Formatter};
|
||||
use lazy_static::*;
|
||||
|
||||
/// manage a frame which has the same lifecycle as the tracker
|
||||
pub struct FrameTracker {
|
||||
pub ppn: PhysPageNum,
|
||||
}
|
||||
|
||||
impl FrameTracker {
|
||||
pub fn new(ppn: PhysPageNum) -> Self {
|
||||
// page cleaning
|
||||
let bytes_array = ppn.get_bytes_array();
|
||||
for i in bytes_array {
|
||||
*i = 0;
|
||||
}
|
||||
Self { ppn }
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for FrameTracker {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
f.write_fmt(format_args!("FrameTracker:PPN={:#x}", self.ppn.0))
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for FrameTracker {
|
||||
fn drop(&mut self) {
|
||||
frame_dealloc(self.ppn);
|
||||
}
|
||||
}
|
||||
|
||||
trait FrameAllocator {
|
||||
fn new() -> Self;
|
||||
fn alloc(&mut self) -> Option<PhysPageNum>;
|
||||
fn dealloc(&mut self, ppn: PhysPageNum);
|
||||
}
|
||||
|
||||
/// an implementation for frame allocator
|
||||
pub struct StackFrameAllocator {
|
||||
current: usize,
|
||||
end: usize,
|
||||
recycled: Vec<usize>,
|
||||
}
|
||||
|
||||
impl StackFrameAllocator {
|
||||
pub fn init(&mut self, l: PhysPageNum, r: PhysPageNum) {
|
||||
self.current = l.0;
|
||||
self.end = r.0;
|
||||
info!("last {} Physical Frames.", self.end - self.current);
|
||||
}
|
||||
}
|
||||
impl FrameAllocator for StackFrameAllocator {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
current: 0,
|
||||
end: 0,
|
||||
recycled: Vec::new(),
|
||||
}
|
||||
}
|
||||
fn alloc(&mut self) -> Option<PhysPageNum> {
|
||||
if let Some(ppn) = self.recycled.pop() {
|
||||
Some(ppn.into())
|
||||
} else if self.current == self.end {
|
||||
None
|
||||
} else {
|
||||
self.current += 1;
|
||||
Some((self.current - 1).into())
|
||||
}
|
||||
}
|
||||
fn dealloc(&mut self, ppn: PhysPageNum) {
|
||||
let ppn = ppn.0;
|
||||
// validity check
|
||||
if ppn >= self.current || self.recycled.iter().any(|v| *v == ppn) {
|
||||
panic!("Frame ppn={:#x} has not been allocated!", ppn);
|
||||
}
|
||||
// recycle
|
||||
self.recycled.push(ppn);
|
||||
}
|
||||
}
|
||||
|
||||
type FrameAllocatorImpl = StackFrameAllocator;
|
||||
|
||||
lazy_static! {
|
||||
/// frame allocator instance through lazy_static!
|
||||
pub static ref FRAME_ALLOCATOR: UPSafeCell<FrameAllocatorImpl> =
|
||||
unsafe { UPSafeCell::new(FrameAllocatorImpl::new()) };
|
||||
}
|
||||
|
||||
pub fn init_frame_allocator() {
|
||||
extern "C" {
|
||||
fn ekernel();
|
||||
}
|
||||
FRAME_ALLOCATOR.exclusive_access().init(
|
||||
PhysAddr::from(ekernel as usize).ceil(),
|
||||
PhysAddr::from(MEMORY_END).floor(),
|
||||
);
|
||||
}
|
||||
|
||||
/// initiate the frame allocator using `ekernel` and `MEMORY_END`
|
||||
pub fn frame_alloc() -> Option<FrameTracker> {
|
||||
FRAME_ALLOCATOR
|
||||
.exclusive_access()
|
||||
.alloc()
|
||||
.map(FrameTracker::new)
|
||||
}
|
||||
|
||||
/// deallocate a frame
|
||||
fn frame_dealloc(ppn: PhysPageNum) {
|
||||
FRAME_ALLOCATOR.exclusive_access().dealloc(ppn);
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
/// a simple test for frame allocator
|
||||
pub fn frame_allocator_test() {
|
||||
let mut v: Vec<FrameTracker> = Vec::new();
|
||||
for i in 0..5 {
|
||||
let frame = frame_alloc().unwrap();
|
||||
info!("{:?}", frame);
|
||||
v.push(frame);
|
||||
}
|
||||
v.clear();
|
||||
for i in 0..5 {
|
||||
let frame = frame_alloc().unwrap();
|
||||
info!("{:?}", frame);
|
||||
v.push(frame);
|
||||
}
|
||||
drop(v);
|
||||
info!("frame_allocator_test passed!");
|
||||
}
|
||||
51
os5-ref/src/mm/heap_allocator.rs
Normal file
51
os5-ref/src/mm/heap_allocator.rs
Normal file
@@ -0,0 +1,51 @@
|
||||
//! The global allocator
|
||||
|
||||
use crate::config::KERNEL_HEAP_SIZE;
|
||||
use buddy_system_allocator::LockedHeap;
|
||||
|
||||
#[global_allocator]
|
||||
/// heap allocator instance
|
||||
static HEAP_ALLOCATOR: LockedHeap = LockedHeap::empty();
|
||||
|
||||
#[alloc_error_handler]
|
||||
/// panic when heap allocation error occurs
|
||||
pub fn handle_alloc_error(layout: core::alloc::Layout) -> ! {
|
||||
panic!("Heap allocation error, layout = {:?}", layout);
|
||||
}
|
||||
|
||||
/// heap space ([u8; KERNEL_HEAP_SIZE])
|
||||
static mut HEAP_SPACE: [u8; KERNEL_HEAP_SIZE] = [0; KERNEL_HEAP_SIZE];
|
||||
|
||||
/// initiate heap allocator
|
||||
pub fn init_heap() {
|
||||
unsafe {
|
||||
HEAP_ALLOCATOR
|
||||
.lock()
|
||||
.init(HEAP_SPACE.as_ptr() as usize, KERNEL_HEAP_SIZE);
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
pub fn heap_test() {
|
||||
use alloc::boxed::Box;
|
||||
use alloc::vec::Vec;
|
||||
extern "C" {
|
||||
fn sbss();
|
||||
fn ebss();
|
||||
}
|
||||
let bss_range = sbss as usize..ebss as usize;
|
||||
let a = Box::new(5);
|
||||
assert_eq!(*a, 5);
|
||||
assert!(bss_range.contains(&(a.as_ref() as *const _ as usize)));
|
||||
drop(a);
|
||||
let mut v: Vec<usize> = Vec::new();
|
||||
for i in 0..500 {
|
||||
v.push(i);
|
||||
}
|
||||
for (i, vi) in v.iter().enumerate().take(500) {
|
||||
assert_eq!(*vi, i);
|
||||
}
|
||||
assert!(bss_range.contains(&(v.as_ptr() as usize)));
|
||||
drop(v);
|
||||
info!("heap_test passed!");
|
||||
}
|
||||
388
os5-ref/src/mm/memory_set.rs
Normal file
388
os5-ref/src/mm/memory_set.rs
Normal file
@@ -0,0 +1,388 @@
|
||||
//! Implementation of [`MapArea`] and [`MemorySet`].
|
||||
|
||||
use super::{frame_alloc, FrameTracker};
|
||||
use super::{PTEFlags, PageTable, PageTableEntry};
|
||||
use super::{PhysAddr, PhysPageNum, VirtAddr, VirtPageNum};
|
||||
use super::{StepByOne, VPNRange};
|
||||
use crate::config::{MEMORY_END, PAGE_SIZE, TRAMPOLINE, TRAP_CONTEXT, USER_STACK_SIZE};
|
||||
use crate::sync::UPSafeCell;
|
||||
use alloc::collections::BTreeMap;
|
||||
use alloc::sync::Arc;
|
||||
use alloc::vec::Vec;
|
||||
use lazy_static::*;
|
||||
use riscv::register::satp;
|
||||
|
||||
extern "C" {
|
||||
fn stext();
|
||||
fn etext();
|
||||
fn srodata();
|
||||
fn erodata();
|
||||
fn sdata();
|
||||
fn edata();
|
||||
fn sbss_with_stack();
|
||||
fn ebss();
|
||||
fn ekernel();
|
||||
fn strampoline();
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
/// a memory set instance through lazy_static! managing kernel space
|
||||
pub static ref KERNEL_SPACE: Arc<UPSafeCell<MemorySet>> =
|
||||
Arc::new(unsafe { UPSafeCell::new(MemorySet::new_kernel()) });
|
||||
}
|
||||
|
||||
/// memory set structure, controls virtual-memory space
|
||||
pub struct MemorySet {
|
||||
page_table: PageTable,
|
||||
areas: Vec<MapArea>,
|
||||
}
|
||||
|
||||
impl MemorySet {
|
||||
pub fn new_bare() -> Self {
|
||||
Self {
|
||||
page_table: PageTable::new(),
|
||||
areas: Vec::new(),
|
||||
}
|
||||
}
|
||||
pub fn token(&self) -> usize {
|
||||
self.page_table.token()
|
||||
}
|
||||
/// Assume that no conflicts.
|
||||
pub fn insert_framed_area(
|
||||
&mut self,
|
||||
start_va: VirtAddr,
|
||||
end_va: VirtAddr,
|
||||
permission: MapPermission,
|
||||
) {
|
||||
self.push(
|
||||
MapArea::new(start_va, end_va, MapType::Framed, permission),
|
||||
None,
|
||||
);
|
||||
}
|
||||
pub fn remove_area_with_start_vpn(&mut self, start_vpn: VirtPageNum) {
|
||||
if let Some((idx, area)) = self
|
||||
.areas
|
||||
.iter_mut()
|
||||
.enumerate()
|
||||
.find(|(_, area)| area.vpn_range.get_start() == start_vpn)
|
||||
{
|
||||
area.unmap(&mut self.page_table);
|
||||
self.areas.remove(idx);
|
||||
}
|
||||
}
|
||||
fn push(&mut self, mut map_area: MapArea, data: Option<&[u8]>) {
|
||||
map_area.map(&mut self.page_table);
|
||||
if let Some(data) = data {
|
||||
map_area.copy_data(&mut self.page_table, data);
|
||||
}
|
||||
self.areas.push(map_area);
|
||||
}
|
||||
/// Mention that trampoline is not collected by areas.
|
||||
fn map_trampoline(&mut self) {
|
||||
self.page_table.map(
|
||||
VirtAddr::from(TRAMPOLINE).into(),
|
||||
PhysAddr::from(strampoline as usize).into(),
|
||||
PTEFlags::R | PTEFlags::X,
|
||||
);
|
||||
}
|
||||
/// Without kernel stacks.
|
||||
pub fn new_kernel() -> Self {
|
||||
let mut memory_set = Self::new_bare();
|
||||
// map trampoline
|
||||
memory_set.map_trampoline();
|
||||
// map kernel sections
|
||||
info!(".text [{:#x}, {:#x})", stext as usize, etext as usize);
|
||||
info!(".rodata [{:#x}, {:#x})", srodata as usize, erodata as usize);
|
||||
info!(".data [{:#x}, {:#x})", sdata as usize, edata as usize);
|
||||
info!(
|
||||
".bss [{:#x}, {:#x})",
|
||||
sbss_with_stack as usize, ebss as usize
|
||||
);
|
||||
info!("mapping .text section");
|
||||
memory_set.push(
|
||||
MapArea::new(
|
||||
(stext as usize).into(),
|
||||
(etext as usize).into(),
|
||||
MapType::Identical,
|
||||
MapPermission::R | MapPermission::X,
|
||||
),
|
||||
None,
|
||||
);
|
||||
info!("mapping .rodata section");
|
||||
memory_set.push(
|
||||
MapArea::new(
|
||||
(srodata as usize).into(),
|
||||
(erodata as usize).into(),
|
||||
MapType::Identical,
|
||||
MapPermission::R,
|
||||
),
|
||||
None,
|
||||
);
|
||||
info!("mapping .data section");
|
||||
memory_set.push(
|
||||
MapArea::new(
|
||||
(sdata as usize).into(),
|
||||
(edata as usize).into(),
|
||||
MapType::Identical,
|
||||
MapPermission::R | MapPermission::W,
|
||||
),
|
||||
None,
|
||||
);
|
||||
info!("mapping .bss section");
|
||||
memory_set.push(
|
||||
MapArea::new(
|
||||
(sbss_with_stack as usize).into(),
|
||||
(ebss as usize).into(),
|
||||
MapType::Identical,
|
||||
MapPermission::R | MapPermission::W,
|
||||
),
|
||||
None,
|
||||
);
|
||||
info!("mapping physical memory");
|
||||
memory_set.push(
|
||||
MapArea::new(
|
||||
(ekernel as usize).into(),
|
||||
MEMORY_END.into(),
|
||||
MapType::Identical,
|
||||
MapPermission::R | MapPermission::W,
|
||||
),
|
||||
None,
|
||||
);
|
||||
memory_set
|
||||
}
|
||||
/// Include sections in elf and trampoline and TrapContext and user stack,
|
||||
/// also returns user_sp and entry point.
|
||||
pub fn from_elf(elf_data: &[u8]) -> (Self, usize, usize) {
|
||||
let mut memory_set = Self::new_bare();
|
||||
// map trampoline
|
||||
memory_set.map_trampoline();
|
||||
// map program headers of elf, with U flag
|
||||
let elf = xmas_elf::ElfFile::new(elf_data).unwrap();
|
||||
let elf_header = elf.header;
|
||||
let magic = elf_header.pt1.magic;
|
||||
assert_eq!(magic, [0x7f, 0x45, 0x4c, 0x46], "invalid elf!");
|
||||
let ph_count = elf_header.pt2.ph_count();
|
||||
let mut max_end_vpn = VirtPageNum(0);
|
||||
for i in 0..ph_count {
|
||||
let ph = elf.program_header(i).unwrap();
|
||||
if ph.get_type().unwrap() == xmas_elf::program::Type::Load {
|
||||
let start_va: VirtAddr = (ph.virtual_addr() as usize).into();
|
||||
let end_va: VirtAddr = ((ph.virtual_addr() + ph.mem_size()) as usize).into();
|
||||
let mut map_perm = MapPermission::U;
|
||||
let ph_flags = ph.flags();
|
||||
if ph_flags.is_read() {
|
||||
map_perm |= MapPermission::R;
|
||||
}
|
||||
if ph_flags.is_write() {
|
||||
map_perm |= MapPermission::W;
|
||||
}
|
||||
if ph_flags.is_execute() {
|
||||
map_perm |= MapPermission::X;
|
||||
}
|
||||
let map_area = MapArea::new(start_va, end_va, MapType::Framed, map_perm);
|
||||
max_end_vpn = map_area.vpn_range.get_end();
|
||||
memory_set.push(
|
||||
map_area,
|
||||
Some(&elf.input[ph.offset() as usize..(ph.offset() + ph.file_size()) as usize]),
|
||||
);
|
||||
}
|
||||
}
|
||||
// map user stack with U flags
|
||||
let max_end_va: VirtAddr = max_end_vpn.into();
|
||||
let mut user_stack_bottom: usize = max_end_va.into();
|
||||
// guard page
|
||||
user_stack_bottom += PAGE_SIZE;
|
||||
let user_stack_top = user_stack_bottom + USER_STACK_SIZE;
|
||||
memory_set.push(
|
||||
MapArea::new(
|
||||
user_stack_bottom.into(),
|
||||
user_stack_top.into(),
|
||||
MapType::Framed,
|
||||
MapPermission::R | MapPermission::W | MapPermission::U,
|
||||
),
|
||||
None,
|
||||
);
|
||||
// map TrapContext
|
||||
memory_set.push(
|
||||
MapArea::new(
|
||||
TRAP_CONTEXT.into(),
|
||||
TRAMPOLINE.into(),
|
||||
MapType::Framed,
|
||||
MapPermission::R | MapPermission::W,
|
||||
),
|
||||
None,
|
||||
);
|
||||
(
|
||||
memory_set,
|
||||
user_stack_top,
|
||||
elf.header.pt2.entry_point() as usize,
|
||||
)
|
||||
}
|
||||
/// Copy an identical user_space
|
||||
pub fn from_existed_user(user_space: &MemorySet) -> MemorySet {
|
||||
let mut memory_set = Self::new_bare();
|
||||
// map trampoline
|
||||
memory_set.map_trampoline();
|
||||
// copy data sections/trap_context/user_stack
|
||||
for area in user_space.areas.iter() {
|
||||
let new_area = MapArea::from_another(area);
|
||||
memory_set.push(new_area, None);
|
||||
// copy data from another space
|
||||
for vpn in area.vpn_range {
|
||||
let src_ppn = user_space.translate(vpn).unwrap().ppn();
|
||||
let dst_ppn = memory_set.translate(vpn).unwrap().ppn();
|
||||
dst_ppn
|
||||
.get_bytes_array()
|
||||
.copy_from_slice(src_ppn.get_bytes_array());
|
||||
}
|
||||
}
|
||||
memory_set
|
||||
}
|
||||
pub fn activate(&self) {
|
||||
let satp = self.page_table.token();
|
||||
unsafe {
|
||||
satp::write(satp);
|
||||
core::arch::asm!("sfence.vma");
|
||||
}
|
||||
}
|
||||
pub fn translate(&self, vpn: VirtPageNum) -> Option<PageTableEntry> {
|
||||
self.page_table.translate(vpn)
|
||||
}
|
||||
pub fn recycle_data_pages(&mut self) {
|
||||
//*self = Self::new_bare();
|
||||
self.areas.clear();
|
||||
}
|
||||
}
|
||||
|
||||
/// map area structure, controls a contiguous piece of virtual memory
|
||||
pub struct MapArea {
|
||||
vpn_range: VPNRange,
|
||||
data_frames: BTreeMap<VirtPageNum, FrameTracker>,
|
||||
map_type: MapType,
|
||||
map_perm: MapPermission,
|
||||
}
|
||||
|
||||
impl MapArea {
|
||||
pub fn new(
|
||||
start_va: VirtAddr,
|
||||
end_va: VirtAddr,
|
||||
map_type: MapType,
|
||||
map_perm: MapPermission,
|
||||
) -> Self {
|
||||
let start_vpn: VirtPageNum = start_va.floor();
|
||||
let end_vpn: VirtPageNum = end_va.ceil();
|
||||
Self {
|
||||
vpn_range: VPNRange::new(start_vpn, end_vpn),
|
||||
data_frames: BTreeMap::new(),
|
||||
map_type,
|
||||
map_perm,
|
||||
}
|
||||
}
|
||||
pub fn from_another(another: &MapArea) -> Self {
|
||||
Self {
|
||||
vpn_range: VPNRange::new(another.vpn_range.get_start(), another.vpn_range.get_end()),
|
||||
data_frames: BTreeMap::new(),
|
||||
map_type: another.map_type,
|
||||
map_perm: another.map_perm,
|
||||
}
|
||||
}
|
||||
pub fn map_one(&mut self, page_table: &mut PageTable, vpn: VirtPageNum) {
|
||||
let ppn: PhysPageNum;
|
||||
match self.map_type {
|
||||
MapType::Identical => {
|
||||
ppn = PhysPageNum(vpn.0);
|
||||
}
|
||||
MapType::Framed => {
|
||||
let frame = frame_alloc().unwrap();
|
||||
ppn = frame.ppn;
|
||||
self.data_frames.insert(vpn, frame);
|
||||
}
|
||||
}
|
||||
let pte_flags = PTEFlags::from_bits(self.map_perm.bits).unwrap();
|
||||
page_table.map(vpn, ppn, pte_flags);
|
||||
}
|
||||
|
||||
pub fn unmap_one(&mut self, page_table: &mut PageTable, vpn: VirtPageNum) {
|
||||
#[allow(clippy::single_match)]
|
||||
match self.map_type {
|
||||
MapType::Framed => {
|
||||
self.data_frames.remove(&vpn);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
page_table.unmap(vpn);
|
||||
}
|
||||
pub fn map(&mut self, page_table: &mut PageTable) {
|
||||
for vpn in self.vpn_range {
|
||||
self.map_one(page_table, vpn);
|
||||
}
|
||||
}
|
||||
pub fn unmap(&mut self, page_table: &mut PageTable) {
|
||||
for vpn in self.vpn_range {
|
||||
self.unmap_one(page_table, vpn);
|
||||
}
|
||||
}
|
||||
/// data: start-aligned but maybe with shorter length
|
||||
/// assume that all frames were cleared before
|
||||
pub fn copy_data(&mut self, page_table: &mut PageTable, data: &[u8]) {
|
||||
assert_eq!(self.map_type, MapType::Framed);
|
||||
let mut start: usize = 0;
|
||||
let mut current_vpn = self.vpn_range.get_start();
|
||||
let len = data.len();
|
||||
loop {
|
||||
let src = &data[start..len.min(start + PAGE_SIZE)];
|
||||
let dst = &mut page_table
|
||||
.translate(current_vpn)
|
||||
.unwrap()
|
||||
.ppn()
|
||||
.get_bytes_array()[..src.len()];
|
||||
dst.copy_from_slice(src);
|
||||
start += PAGE_SIZE;
|
||||
if start >= len {
|
||||
break;
|
||||
}
|
||||
current_vpn.step();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Debug)]
|
||||
/// map type for memory set: identical or framed
|
||||
pub enum MapType {
|
||||
Identical,
|
||||
Framed,
|
||||
}
|
||||
|
||||
bitflags! {
|
||||
/// map permission corresponding to that in pte: `R W X U`
|
||||
pub struct MapPermission: u8 {
|
||||
const R = 1 << 1;
|
||||
const W = 1 << 2;
|
||||
const X = 1 << 3;
|
||||
const U = 1 << 4;
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
pub fn remap_test() {
|
||||
let mut kernel_space = KERNEL_SPACE.exclusive_access();
|
||||
let mid_text: VirtAddr = ((stext as usize + etext as usize) / 2).into();
|
||||
let mid_rodata: VirtAddr = ((srodata as usize + erodata as usize) / 2).into();
|
||||
let mid_data: VirtAddr = ((sdata as usize + edata as usize) / 2).into();
|
||||
assert!(!kernel_space
|
||||
.page_table
|
||||
.translate(mid_text.floor())
|
||||
.unwrap()
|
||||
.writable());
|
||||
assert!(!kernel_space
|
||||
.page_table
|
||||
.translate(mid_rodata.floor())
|
||||
.unwrap()
|
||||
.writable());
|
||||
assert!(!kernel_space
|
||||
.page_table
|
||||
.translate(mid_data.floor())
|
||||
.unwrap()
|
||||
.executable());
|
||||
info!("remap_test passed!");
|
||||
}
|
||||
29
os5-ref/src/mm/mod.rs
Normal file
29
os5-ref/src/mm/mod.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
//! Memory management implementation
|
||||
//!
|
||||
//! SV39 page-based virtual-memory architecture for RV64 systems, and
|
||||
//! everything about memory management, like frame allocator, page table,
|
||||
//! map area and memory set, is implemented here.
|
||||
//!
|
||||
//! Every task or process has a memory_set to control its virtual memory.
|
||||
|
||||
|
||||
mod address;
|
||||
mod frame_allocator;
|
||||
mod heap_allocator;
|
||||
mod memory_set;
|
||||
mod page_table;
|
||||
|
||||
pub use address::{PhysAddr, PhysPageNum, VirtAddr, VirtPageNum};
|
||||
use address::{StepByOne, VPNRange};
|
||||
pub use frame_allocator::{frame_alloc, FrameTracker};
|
||||
pub use memory_set::remap_test;
|
||||
pub use memory_set::{MapPermission, MemorySet, KERNEL_SPACE};
|
||||
pub use page_table::{translated_byte_buffer, translated_refmut, translated_str, PageTableEntry};
|
||||
use page_table::{PTEFlags, PageTable};
|
||||
|
||||
/// initiate heap allocator, frame allocator and kernel space
|
||||
pub fn init() {
|
||||
heap_allocator::init_heap();
|
||||
frame_allocator::init_frame_allocator();
|
||||
KERNEL_SPACE.exclusive_access().activate();
|
||||
}
|
||||
198
os5-ref/src/mm/page_table.rs
Normal file
198
os5-ref/src/mm/page_table.rs
Normal file
@@ -0,0 +1,198 @@
|
||||
//! Implementation of [`PageTableEntry`] and [`PageTable`].
|
||||
|
||||
use super::{frame_alloc, FrameTracker, PhysAddr, PhysPageNum, StepByOne, VirtAddr, VirtPageNum};
|
||||
use alloc::string::String;
|
||||
use alloc::vec;
|
||||
use alloc::vec::Vec;
|
||||
use bitflags::*;
|
||||
|
||||
bitflags! {
|
||||
/// page table entry flags
|
||||
pub struct PTEFlags: u8 {
|
||||
const V = 1 << 0;
|
||||
const R = 1 << 1;
|
||||
const W = 1 << 2;
|
||||
const X = 1 << 3;
|
||||
const U = 1 << 4;
|
||||
const G = 1 << 5;
|
||||
const A = 1 << 6;
|
||||
const D = 1 << 7;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
#[repr(C)]
|
||||
/// page table entry structure
|
||||
pub struct PageTableEntry {
|
||||
pub bits: usize,
|
||||
}
|
||||
|
||||
impl PageTableEntry {
|
||||
pub fn new(ppn: PhysPageNum, flags: PTEFlags) -> Self {
|
||||
PageTableEntry {
|
||||
bits: ppn.0 << 10 | flags.bits as usize,
|
||||
}
|
||||
}
|
||||
pub fn empty() -> Self {
|
||||
PageTableEntry { bits: 0 }
|
||||
}
|
||||
pub fn ppn(&self) -> PhysPageNum {
|
||||
(self.bits >> 10 & ((1usize << 44) - 1)).into()
|
||||
}
|
||||
pub fn flags(&self) -> PTEFlags {
|
||||
PTEFlags::from_bits(self.bits as u8).unwrap()
|
||||
}
|
||||
pub fn is_valid(&self) -> bool {
|
||||
(self.flags() & PTEFlags::V) != PTEFlags::empty()
|
||||
}
|
||||
pub fn readable(&self) -> bool {
|
||||
(self.flags() & PTEFlags::R) != PTEFlags::empty()
|
||||
}
|
||||
pub fn writable(&self) -> bool {
|
||||
(self.flags() & PTEFlags::W) != PTEFlags::empty()
|
||||
}
|
||||
pub fn executable(&self) -> bool {
|
||||
(self.flags() & PTEFlags::X) != PTEFlags::empty()
|
||||
}
|
||||
}
|
||||
|
||||
/// page table structure
|
||||
pub struct PageTable {
|
||||
root_ppn: PhysPageNum,
|
||||
frames: Vec<FrameTracker>,
|
||||
}
|
||||
|
||||
/// Assume that it won't oom when creating/mapping.
|
||||
impl PageTable {
|
||||
pub fn new() -> Self {
|
||||
let frame = frame_alloc().unwrap();
|
||||
PageTable {
|
||||
root_ppn: frame.ppn,
|
||||
frames: vec![frame],
|
||||
}
|
||||
}
|
||||
/// Temporarily used to get arguments from user space.
|
||||
pub fn from_token(satp: usize) -> Self {
|
||||
Self {
|
||||
root_ppn: PhysPageNum::from(satp & ((1usize << 44) - 1)),
|
||||
frames: Vec::new(),
|
||||
}
|
||||
}
|
||||
fn find_pte_create(&mut self, vpn: VirtPageNum) -> Option<&mut PageTableEntry> {
|
||||
let mut idxs = vpn.indexes();
|
||||
let mut ppn = self.root_ppn;
|
||||
let mut result: Option<&mut PageTableEntry> = None;
|
||||
for (i, idx) in idxs.iter_mut().enumerate() {
|
||||
let pte = &mut ppn.get_pte_array()[*idx];
|
||||
if i == 2 {
|
||||
result = Some(pte);
|
||||
break;
|
||||
}
|
||||
if !pte.is_valid() {
|
||||
let frame = frame_alloc().unwrap();
|
||||
*pte = PageTableEntry::new(frame.ppn, PTEFlags::V);
|
||||
self.frames.push(frame);
|
||||
}
|
||||
ppn = pte.ppn();
|
||||
}
|
||||
result
|
||||
}
|
||||
fn find_pte(&self, vpn: VirtPageNum) -> Option<&PageTableEntry> {
|
||||
let idxs = vpn.indexes();
|
||||
let mut ppn = self.root_ppn;
|
||||
let mut result: Option<&PageTableEntry> = None;
|
||||
for (i, idx) in idxs.iter().enumerate() {
|
||||
let pte = &ppn.get_pte_array()[*idx];
|
||||
if i == 2 {
|
||||
result = Some(pte);
|
||||
break;
|
||||
}
|
||||
if !pte.is_valid() {
|
||||
return None;
|
||||
}
|
||||
ppn = pte.ppn();
|
||||
}
|
||||
result
|
||||
}
|
||||
#[allow(unused)]
|
||||
pub fn map(&mut self, vpn: VirtPageNum, ppn: PhysPageNum, flags: PTEFlags) {
|
||||
let pte = self.find_pte_create(vpn).unwrap();
|
||||
assert!(!pte.is_valid(), "vpn {:?} is mapped before mapping", vpn);
|
||||
*pte = PageTableEntry::new(ppn, flags | PTEFlags::V);
|
||||
}
|
||||
#[allow(unused)]
|
||||
pub fn unmap(&mut self, vpn: VirtPageNum) {
|
||||
let pte = self.find_pte_create(vpn).unwrap();
|
||||
assert!(pte.is_valid(), "vpn {:?} is invalid before unmapping", vpn);
|
||||
*pte = PageTableEntry::empty();
|
||||
}
|
||||
pub fn translate(&self, vpn: VirtPageNum) -> Option<PageTableEntry> {
|
||||
self.find_pte(vpn).copied()
|
||||
}
|
||||
pub fn translate_va(&self, va: VirtAddr) -> Option<PhysAddr> {
|
||||
self.find_pte(va.clone().floor()).map(|pte| {
|
||||
//println!("translate_va:va = {:?}", va);
|
||||
let aligned_pa: PhysAddr = pte.ppn().into();
|
||||
//println!("translate_va:pa_align = {:?}", aligned_pa);
|
||||
let offset = va.page_offset();
|
||||
let aligned_pa_usize: usize = aligned_pa.into();
|
||||
(aligned_pa_usize + offset).into()
|
||||
})
|
||||
}
|
||||
pub fn token(&self) -> usize {
|
||||
8usize << 60 | self.root_ppn.0
|
||||
}
|
||||
}
|
||||
|
||||
/// translate a pointer to a mutable u8 Vec through page table
|
||||
pub fn translated_byte_buffer(token: usize, ptr: *const u8, len: usize) -> Vec<&'static mut [u8]> {
|
||||
let page_table = PageTable::from_token(token);
|
||||
let mut start = ptr as usize;
|
||||
let end = start + len;
|
||||
let mut v = Vec::new();
|
||||
while start < end {
|
||||
let start_va = VirtAddr::from(start);
|
||||
let mut vpn = start_va.floor();
|
||||
let ppn = page_table.translate(vpn).unwrap().ppn();
|
||||
vpn.step();
|
||||
let mut end_va: VirtAddr = vpn.into();
|
||||
end_va = end_va.min(VirtAddr::from(end));
|
||||
if end_va.page_offset() == 0 {
|
||||
v.push(&mut ppn.get_bytes_array()[start_va.page_offset()..]);
|
||||
} else {
|
||||
v.push(&mut ppn.get_bytes_array()[start_va.page_offset()..end_va.page_offset()]);
|
||||
}
|
||||
start = end_va.into();
|
||||
}
|
||||
v
|
||||
}
|
||||
|
||||
pub fn translated_str(token: usize, ptr: *const u8) -> String {
|
||||
let page_table = PageTable::from_token(token);
|
||||
let mut string = String::new();
|
||||
let mut va = ptr as usize;
|
||||
loop {
|
||||
let ch: u8 = *(page_table
|
||||
.translate_va(VirtAddr::from(va))
|
||||
.unwrap()
|
||||
.get_mut());
|
||||
if ch == 0 {
|
||||
break;
|
||||
} else {
|
||||
string.push(ch as char);
|
||||
va += 1;
|
||||
}
|
||||
}
|
||||
string
|
||||
}
|
||||
|
||||
pub fn translated_refmut<T>(token: usize, ptr: *mut T) -> &'static mut T {
|
||||
//println!("into translated_refmut!");
|
||||
let page_table = PageTable::from_token(token);
|
||||
let va = ptr as usize;
|
||||
//println!("translated_refmut: before translate_va");
|
||||
page_table
|
||||
.translate_va(VirtAddr::from(va))
|
||||
.unwrap()
|
||||
.get_mut()
|
||||
}
|
||||
Reference in New Issue
Block a user