Store context memory information

This commit is contained in:
Jeremy Soller 2016-09-11 21:04:34 -06:00
parent bed09d0518
commit bcd318d80b
5 changed files with 131 additions and 59 deletions

70
kernel/context/memory.rs Normal file
View file

@ -0,0 +1,70 @@
use arch::paging::{ActivePageTable, Page, PageIter, VirtualAddress};
use arch::paging::entry::EntryFlags;
#[derive(Debug)]
pub struct Memory {
start: VirtualAddress,
size: usize,
flags: EntryFlags
}
impl Memory {
pub fn new(start: VirtualAddress, size: usize, flags: EntryFlags) -> Self {
let mut memory = Memory {
start: start,
size: size,
flags: flags
};
memory.map(true);
memory
}
pub fn pages(&self) -> PageIter {
let start_page = Page::containing_address(self.start);
let end_page = Page::containing_address(VirtualAddress::new(self.start.get() + self.size - 1));
Page::range_inclusive(start_page, end_page)
}
pub fn map(&mut self, flush: bool) {
let mut active_table = unsafe { ActivePageTable::new() };
for page in self.pages() {
active_table.map(page, self.flags);
if flush {
active_table.flush(page);
}
}
}
pub fn unmap(&mut self, flush: bool) {
let mut active_table = unsafe { ActivePageTable::new() };
for page in self.pages() {
active_table.unmap(page);
if flush {
active_table.flush(page);
}
}
}
pub fn remap(&mut self, new_flags: EntryFlags, flush: bool) {
let mut active_table = unsafe { ActivePageTable::new() };
self.flags = new_flags;
for page in self.pages() {
active_table.remap(page, self.flags);
if flush {
active_table.flush(page);
}
}
}
}
impl Drop for Memory {
fn drop(&mut self) {
self.unmap(true);
}
}

View file

@ -13,6 +13,9 @@ use syscall::{Error, Result};
/// File operations
pub mod file;
/// Memory operations
pub mod memory;
/// Limit on number of contexts
pub const CONTEXT_MAX_CONTEXTS: usize = 65536;
@ -171,6 +174,10 @@ pub struct Context {
pub arch: ArchContext,
/// Kernel stack
pub kstack: Option<Box<[u8]>>,
/// Executable image
pub image: Vec<memory::Memory>,
/// User stack
pub stack: Option<memory::Memory>,
/// The open files in the scheme
pub files: Vec<Option<file::File>>
}
@ -184,6 +191,8 @@ impl Context {
blocked: true,
arch: ArchContext::new(),
kstack: None,
image: Vec::new(),
stack: None,
files: Vec::new()
}
}