Grant to allow passing data to scheme handler

This commit is contained in:
Jeremy Soller 2016-09-20 14:50:04 -06:00
parent 2b915953c9
commit 941fc0b494
10 changed files with 205 additions and 19 deletions

View file

@ -5,7 +5,7 @@ use spin::Mutex;
use arch;
use super::file::File;
use super::memory::{Memory, SharedMemory};
use super::memory::{Grant, Memory, SharedMemory};
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Status {
@ -33,6 +33,8 @@ pub struct Context {
pub heap: Option<SharedMemory>,
/// User stack
pub stack: Option<Memory>,
/// User grants
pub grants: Arc<Mutex<Vec<Grant>>>,
/// The current working directory
pub cwd: Arc<Mutex<Vec<u8>>>,
/// The open files in the scheme
@ -51,6 +53,7 @@ impl Context {
image: Vec::new(),
heap: None,
stack: None,
grants: Arc::new(Mutex::new(Vec::new())),
cwd: Arc::new(Mutex::new(Vec::new())),
files: Arc::new(Mutex::new(Vec::new()))
}

View file

@ -1,3 +1,4 @@
use alloc::arc::Arc;
use collections::BTreeMap;
use core::mem;
use core::sync::atomic::Ordering;
@ -9,7 +10,7 @@ use super::context::Context;
/// Context list type
pub struct ContextList {
map: BTreeMap<usize, RwLock<Context>>,
map: BTreeMap<usize, Arc<RwLock<Context>>>,
next_id: usize
}
@ -23,21 +24,21 @@ impl ContextList {
}
/// Get the nth context.
pub fn get(&self, id: usize) -> Option<&RwLock<Context>> {
pub fn get(&self, id: usize) -> Option<&Arc<RwLock<Context>>> {
self.map.get(&id)
}
/// Get the current context.
pub fn current(&self) -> Option<&RwLock<Context>> {
pub fn current(&self) -> Option<&Arc<RwLock<Context>>> {
self.map.get(&super::CONTEXT_ID.load(Ordering::SeqCst))
}
pub fn iter(&self) -> ::collections::btree_map::Iter<usize, RwLock<Context>> {
pub fn iter(&self) -> ::collections::btree_map::Iter<usize, Arc<RwLock<Context>>> {
self.map.iter()
}
/// Create a new context.
pub fn new_context(&mut self) -> Result<&RwLock<Context>> {
pub fn new_context(&mut self) -> Result<&Arc<RwLock<Context>>> {
if self.next_id >= super::CONTEXT_MAX_CONTEXTS {
self.next_id = 1;
}
@ -53,13 +54,13 @@ impl ContextList {
let id = self.next_id;
self.next_id += 1;
assert!(self.map.insert(id, RwLock::new(Context::new(id))).is_none());
assert!(self.map.insert(id, Arc::new(RwLock::new(Context::new(id)))).is_none());
Ok(self.map.get(&id).expect("Failed to insert new context. ID is out of bounds."))
}
/// Spawn a context from a function.
pub fn spawn(&mut self, func: extern fn()) -> Result<&RwLock<Context>> {
pub fn spawn(&mut self, func: extern fn()) -> Result<&Arc<RwLock<Context>>> {
let context_lock = self.new_context()?;
{
let mut context = context_lock.write();
@ -77,7 +78,7 @@ impl ContextList {
Ok(context_lock)
}
pub fn remove(&mut self, id: usize) -> Option<RwLock<Context>> {
pub fn remove(&mut self, id: usize) -> Option<Arc<RwLock<Context>>> {
self.map.remove(&id)
}
}

View file

@ -1,4 +1,5 @@
use alloc::arc::{Arc, Weak};
use collections::VecDeque;
use spin::Mutex;
use arch::externs::memset;
@ -7,12 +8,66 @@ use arch::paging::entry::{self, EntryFlags};
use arch::paging::temporary_page::TemporaryPage;
#[derive(Debug)]
pub struct Memory {
pub struct Grant {
start: VirtualAddress,
size: usize,
flags: EntryFlags
}
impl Grant {
pub fn new(from: VirtualAddress, to: VirtualAddress, size: usize, flags: EntryFlags, new_table: &mut InactivePageTable, temporary_page: &mut TemporaryPage) -> Grant {
let mut active_table = unsafe { ActivePageTable::new() };
let mut frames = VecDeque::new();
let start_page = Page::containing_address(from);
let end_page = Page::containing_address(VirtualAddress::new(from.get() + size - 1));
for page in Page::range_inclusive(start_page, end_page) {
let frame = active_table.translate_page(page).expect("grant references unmapped memory");
frames.push_back(frame);
}
active_table.with(new_table, temporary_page, |mapper| {
let start_page = Page::containing_address(to);
let end_page = Page::containing_address(VirtualAddress::new(to.get() + size - 1));
for page in Page::range_inclusive(start_page, end_page) {
let frame = frames.pop_front().expect("grant did not find enough frames");
mapper.map_to(page, frame, flags);
}
});
Grant {
start: to,
size: size,
flags: flags
}
}
pub fn destroy(self, new_table: &mut InactivePageTable, temporary_page: &mut TemporaryPage) {
let mut active_table = unsafe { ActivePageTable::new() };
active_table.with(new_table, temporary_page, |mapper| {
let start_page = Page::containing_address(self.start);
let end_page = Page::containing_address(VirtualAddress::new(self.start.get() + self.size - 1));
for page in Page::range_inclusive(start_page, end_page) {
mapper.unmap_return(page);
}
});
}
pub fn start_address(&self) -> VirtualAddress {
self.start
}
pub fn size(&self) -> usize {
self.size
}
pub fn flags(&self) -> EntryFlags {
self.flags
}
}
#[derive(Clone, Debug)]
pub enum SharedMemory {
Owned(Arc<Mutex<Memory>>),
@ -42,6 +97,13 @@ impl SharedMemory {
}
}
#[derive(Debug)]
pub struct Memory {
start: VirtualAddress,
size: usize,
flags: EntryFlags
}
impl Memory {
pub fn new(start: VirtualAddress, size: usize, flags: EntryFlags, flush: bool, clear: bool) -> Self {
let mut memory = Memory {