Initialize contexts, add getpid

This commit is contained in:
Jeremy Soller 2016-08-20 14:32:45 -06:00
parent 3b8f396229
commit 5b9c821ff5
4 changed files with 37 additions and 8 deletions

View file

@ -37,7 +37,7 @@ impl ContextList {
self.map.get(&CONTEXT_ID.load(Ordering::SeqCst)) self.map.get(&CONTEXT_ID.load(Ordering::SeqCst))
} }
pub fn new_context(&mut self) -> Result<(usize, &RwLock<Context>)> { pub fn new_context(&mut self) -> Result<&RwLock<Context>> {
if self.next_id >= CONTEXT_MAX_CONTEXTS { if self.next_id >= CONTEXT_MAX_CONTEXTS {
self.next_id = 1; self.next_id = 1;
} }
@ -52,8 +52,8 @@ impl ContextList {
let id = self.next_id; let id = self.next_id;
self.next_id += 1; self.next_id += 1;
assert!(self.map.insert(id, RwLock::new(Context::new())).is_none()); assert!(self.map.insert(id, RwLock::new(Context::new(id))).is_none());
Ok((id, self.map.get(&id).expect("failed to insert new context"))) Ok(self.map.get(&id).expect("failed to insert new context"))
} }
} }
@ -63,6 +63,13 @@ static CONTEXTS: Once<RwLock<ContextList>> = Once::new();
#[thread_local] #[thread_local]
static CONTEXT_ID: AtomicUsize = ATOMIC_USIZE_INIT; static CONTEXT_ID: AtomicUsize = ATOMIC_USIZE_INIT;
pub fn init() {
let mut contexts = contexts_mut();
let context_lock = contexts.new_context().expect("could not initialize first context");
let context = context_lock.read();
CONTEXT_ID.store(context.id, Ordering::SeqCst);
}
/// Initialize contexts, called if needed /// Initialize contexts, called if needed
fn init_contexts() -> RwLock<ContextList> { fn init_contexts() -> RwLock<ContextList> {
RwLock::new(ContextList::new()) RwLock::new(ContextList::new())
@ -81,13 +88,16 @@ pub fn contexts_mut() -> RwLockWriteGuard<'static, ContextList> {
/// A context, which identifies either a process or a thread /// A context, which identifies either a process or a thread
#[derive(Debug)] #[derive(Debug)]
pub struct Context { pub struct Context {
/// The ID of this context
pub id: usize,
/// The open files in the scheme /// The open files in the scheme
pub files: Vec<Option<file::File>> pub files: Vec<Option<file::File>>
} }
impl Context { impl Context {
pub fn new() -> Context { pub fn new(id: usize) -> Context {
Context { Context {
id: id,
files: Vec::new() files: Vec::new()
} }
} }

View file

@ -112,7 +112,9 @@ pub mod tests;
#[no_mangle] #[no_mangle]
pub extern fn kmain() { pub extern fn kmain() {
print!("{}", format!("BSP\n")); context::init();
print!("{}", format!("BSP: {:?}\n", syscall::getpid()));
loop { loop {
unsafe { interrupt::enable_and_halt(); } unsafe { interrupt::enable_and_halt(); }
@ -121,7 +123,9 @@ pub extern fn kmain() {
#[no_mangle] #[no_mangle]
pub extern fn kmain_ap(id: usize) { pub extern fn kmain_ap(id: usize) {
print!("{}", format!("AP {}\n", id)); context::init();
print!("{}", format!("AP {}: {:?}\n", id, syscall::getpid()));
loop { loop {
unsafe { interrupt::enable_and_halt() } unsafe { interrupt::enable_and_halt() }

View file

@ -28,6 +28,8 @@ pub enum Call {
Close = 6, Close = 6,
/// Execute syscall /// Execute syscall
Exec = 11, Exec = 11,
/// Get process ID
GetPid = 20,
} }
/// Convert numbers to calls /// Convert numbers to calls
@ -41,6 +43,7 @@ impl Call {
5 => Ok(Call::Open), 5 => Ok(Call::Open),
6 => Ok(Call::Close), 6 => Ok(Call::Close),
11 => Ok(Call::Exec), 11 => Ok(Call::Exec),
20 => Ok(Call::GetPid),
_ => Err(Error::NoCall) _ => Err(Error::NoCall)
} }
} }
@ -90,7 +93,8 @@ pub fn handle(a: usize, b: usize, c: usize, d: usize, e: usize, _f: usize) -> Re
Call::Write => write(b, convert_slice(c as *const u8, d)?), Call::Write => write(b, convert_slice(c as *const u8, d)?),
Call::Open => open(convert_slice(b as *const u8, c)?, d), Call::Open => open(convert_slice(b as *const u8, c)?, d),
Call::Close => close(b), Call::Close => close(b),
Call::Exec => exec(convert_slice(b as *const u8, c)?, convert_slice(d as *const [usize; 2], e)?) Call::Exec => exec(convert_slice(b as *const u8, c)?, convert_slice(d as *const [usize; 2], e)?),
Call::GetPid => getpid(),
} }
} }

View file

@ -2,7 +2,9 @@
use arch::interrupt::halt; use arch::interrupt::halt;
use super::{convert_slice, Result}; use context;
use super::{convert_slice, Error, Result};
pub fn exit(status: usize) -> ! { pub fn exit(status: usize) -> ! {
println!("Exit {}", status); println!("Exit {}", status);
@ -19,3 +21,12 @@ pub fn exec(path: &[u8], args: &[[usize; 2]]) -> Result<usize> {
println!(""); println!("");
Ok(0) Ok(0)
} }
pub fn getpid() -> Result<usize> {
if let Some(context_lock) = context::contexts().current() {
let context = context_lock.read();
Ok(context.id)
} else {
Err(Error::NoProcess)
}
}