redox/kernel/context/list.rs

92 lines
2.8 KiB
Rust
Raw Normal View History

use alloc::arc::Arc;
2016-09-23 00:14:45 +02:00
use alloc::boxed::Box;
2016-09-16 18:44:52 +02:00
use collections::BTreeMap;
use core::mem;
use core::sync::atomic::Ordering;
use spin::RwLock;
use arch;
use syscall::error::{Result, Error, EAGAIN};
use super::context::{Context, ContextId};
2016-09-16 18:44:52 +02:00
/// Context list type
pub struct ContextList {
map: BTreeMap<ContextId, Arc<RwLock<Context>>>,
2016-09-16 18:44:52 +02:00
next_id: usize
}
impl ContextList {
/// Create a new context list.
pub fn new() -> Self {
ContextList {
map: BTreeMap::new(),
next_id: 1
}
}
/// Get the nth context.
pub fn get(&self, id: ContextId) -> Option<&Arc<RwLock<Context>>> {
2016-09-16 18:44:52 +02:00
self.map.get(&id)
}
/// Get the current context.
pub fn current(&self) -> Option<&Arc<RwLock<Context>>> {
2016-09-16 18:44:52 +02:00
self.map.get(&super::CONTEXT_ID.load(Ordering::SeqCst))
}
pub fn iter(&self) -> ::collections::btree_map::Iter<ContextId, Arc<RwLock<Context>>> {
2016-09-16 18:44:52 +02:00
self.map.iter()
}
/// Create a new context.
pub fn new_context(&mut self) -> Result<&Arc<RwLock<Context>>> {
2016-09-16 18:44:52 +02:00
if self.next_id >= super::CONTEXT_MAX_CONTEXTS {
self.next_id = 1;
}
while self.map.contains_key(&ContextId::from(self.next_id)) {
2016-09-16 18:44:52 +02:00
self.next_id += 1;
}
if self.next_id >= super::CONTEXT_MAX_CONTEXTS {
return Err(Error::new(EAGAIN));
2016-09-16 18:44:52 +02:00
}
let id = ContextId::from(self.next_id);
2016-09-16 18:44:52 +02:00
self.next_id += 1;
assert!(self.map.insert(id, Arc::new(RwLock::new(Context::new(id)))).is_none());
2016-09-16 18:44:52 +02:00
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<&Arc<RwLock<Context>>> {
2016-09-16 18:44:52 +02:00
let context_lock = self.new_context()?;
{
let mut context = context_lock.write();
2016-09-23 00:14:45 +02:00
let mut fx = unsafe { Box::from_raw(::alloc::heap::allocate(512, 16) as *mut [u8; 512]) };
for b in fx.iter_mut() {
*b = 0;
}
2016-09-17 17:18:35 +02:00
let mut stack = vec![0; 65536].into_boxed_slice();
2016-09-16 18:44:52 +02:00
let offset = stack.len() - mem::size_of::<usize>();
unsafe {
let offset = stack.len() - mem::size_of::<usize>();
let func_ptr = stack.as_mut_ptr().offset(offset as isize);
*(func_ptr as *mut usize) = func as usize;
}
context.arch.set_page_table(unsafe { arch::paging::ActivePageTable::new().address() });
2016-09-23 00:14:45 +02:00
context.arch.set_fx(fx.as_ptr() as usize);
2016-09-16 18:44:52 +02:00
context.arch.set_stack(stack.as_ptr() as usize + offset);
2016-09-23 00:14:45 +02:00
context.kfx = Some(fx);
2016-09-16 18:44:52 +02:00
context.kstack = Some(stack);
}
Ok(context_lock)
}
2016-09-17 02:50:47 +02:00
pub fn remove(&mut self, id: ContextId) -> Option<Arc<RwLock<Context>>> {
2016-09-17 02:50:47 +02:00
self.map.remove(&id)
}
2016-09-16 18:44:52 +02:00
}