diff --git a/src/context/list.rs b/src/context/list.rs index 7d2973a9135a5c348dd990c689957ae6f5d33e4f..efca76da2409fa11934723124bb3e5fa2a5a2199 100644 --- a/src/context/list.rs +++ b/src/context/list.rs @@ -50,10 +50,23 @@ impl ContextList { self.map.range(range) } + pub(crate) fn insert_context_raw(&mut self, id: ContextId) -> Result<&Arc<RwLock<Context>>> { + 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.")) + } + /// Create a new context. pub fn new_context(&mut self) -> Result<&Arc<RwLock<Context>>> { + // Zero is not a valid context ID, therefore add 1. + // + // FIXME: Ensure the number of CPUs can't switch between new_context calls. + let min = crate::cpu_count() + 1; + + self.next_id = core::cmp::max(self.next_id, min); + if self.next_id >= super::CONTEXT_MAX_CONTEXTS { - self.next_id = 1; + self.next_id = min; } while self.map.contains_key(&ContextId::from(self.next_id)) { @@ -67,9 +80,7 @@ impl ContextList { let id = ContextId::from(self.next_id); self.next_id += 1; - 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.")) + self.insert_context_raw(id) } /// Spawn a context from a function. diff --git a/src/context/mod.rs b/src/context/mod.rs index c3296d4f0560c0f12df1d0cd366b12e47c375c2b..7f5fc849deec38477f80b63445d1453d62fe5b6e 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -65,8 +65,10 @@ pub use self::arch::empty_cr3; pub fn init() { let mut contexts = contexts_mut(); - let context_lock = contexts.new_context().expect("could not initialize first context"); + let id = ContextId::from(crate::cpu_id() + 1); + let context_lock = contexts.insert_context_raw(id).expect("could not initialize first context"); let mut context = context_lock.write(); + context.sched_affinity = Some(crate::cpu_id()); self::arch::EMPTY_CR3.call_once(|| unsafe { RmmA::table(TableKind::User) }); diff --git a/src/lib.rs b/src/lib.rs index 5b033b9dc9adff055c4e40ddb6e9b4f13e575869..4b47c1cee573ddbc9daa886bf90c2d85ba496bd1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -48,11 +48,8 @@ #![feature(allocator_api)] #![feature(arbitrary_self_types)] #![feature(array_chunks)] -#![feature(asm_const, asm_sym)] // TODO: Relax requirements of most asm invocations -#![feature(bool_to_option)] +#![feature(asm_const)] // TODO: Relax requirements of most asm invocations #![feature(concat_idents)] -#![feature(const_btree_new)] -#![feature(const_ptr_offset_from)] #![feature(core_intrinsics)] #![feature(integer_atomics)] #![feature(lang_items)]