Skip to content

Commit

Permalink
fix(core_local): put scheduler in RefCell
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Kröning <[email protected]>
  • Loading branch information
mkroening committed Sep 14, 2023
1 parent dbab4db commit fad2688
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 112 deletions.
17 changes: 9 additions & 8 deletions src/arch/aarch64/kernel/core_local.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use alloc::boxed::Box;
use alloc::vec::Vec;
use core::arch::asm;
use core::cell::{Cell, RefCell, RefMut};
use core::cell::{RefCell, RefMut};
use core::ptr;
use core::sync::atomic::Ordering;

Expand All @@ -20,7 +20,7 @@ pub(crate) struct CoreLocal {
/// ID of the current Core.
core_id: CoreId,
/// Scheduler of the current Core.
scheduler: Cell<*mut PerCoreScheduler>,
scheduler: RefCell<Option<PerCoreScheduler>>,
/// Interface to the interrupt counters
irq_statistics: &'static IrqStatistics,
/// Queue of async tasks
Expand All @@ -44,7 +44,7 @@ impl CoreLocal {
let this = Self {
this: ptr::null_mut(),
core_id,
scheduler: Cell::new(ptr::null_mut()),
scheduler: RefCell::new(None),
irq_statistics,
async_tasks: RefCell::new(Vec::new()),
#[cfg(feature = "smp")]
Expand Down Expand Up @@ -91,17 +91,18 @@ pub(crate) fn core_id() -> CoreId {
}
}

#[inline]
pub(crate) fn core_scheduler() -> &'static mut PerCoreScheduler {
unsafe { &mut *CoreLocal::get().scheduler.get() }
pub(crate) fn core_scheduler() -> RefMut<'static, PerCoreScheduler> {
RefMut::map(CoreLocal::get().scheduler.borrow_mut(), |scheduler| {
scheduler.as_mut().unwrap()
})
}

pub(crate) fn async_tasks() -> RefMut<'static, Vec<AsyncTask>> {
CoreLocal::get().async_tasks.borrow_mut()
}

pub(crate) fn set_core_scheduler(scheduler: *mut PerCoreScheduler) {
CoreLocal::get().scheduler.set(scheduler);
pub(crate) fn set_core_scheduler(scheduler: PerCoreScheduler) {
*CoreLocal::get().scheduler.borrow_mut() = Some(scheduler);
}

pub(crate) fn increment_irq_counter(irq_no: u8) {
Expand Down
4 changes: 3 additions & 1 deletion src/arch/x86_64/kernel/apic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,11 @@ extern "x86-interrupt" fn spurious_interrupt_handler(stack_frame: interrupts::Ex

#[cfg(feature = "smp")]
extern "x86-interrupt" fn wakeup_handler(_stack_frame: interrupts::ExceptionStackFrame) {
use crate::scheduler::PerCoreSchedulerExt;

debug!("Received Wakeup Interrupt");
increment_irq_counter(WAKEUP_INTERRUPT_NUMBER);
let core_scheduler = core_scheduler();
let mut core_scheduler = core_scheduler();
core_scheduler.check_input();
eoi();
if core_scheduler.is_scheduling() {
Expand Down
14 changes: 8 additions & 6 deletions src/arch/x86_64/kernel/core_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub(crate) struct CoreLocal {
/// Sequential ID of this CPU Core.
core_id: CoreId,
/// Scheduler for this CPU Core.
scheduler: Cell<*mut PerCoreScheduler>,
scheduler: RefCell<Option<PerCoreScheduler>>,
/// Task State Segment (TSS) allocated for this CPU Core.
pub tss: Cell<*mut TaskStateSegment>,
/// start address of the kernel stack
Expand Down Expand Up @@ -54,7 +54,7 @@ impl CoreLocal {
let this = Self {
this: ptr::null_mut(),
core_id,
scheduler: Cell::new(ptr::null_mut()),
scheduler: RefCell::new(None),
tss: Cell::new(ptr::null_mut()),
kernel_stack: Cell::new(0),
irq_statistics,
Expand Down Expand Up @@ -101,16 +101,18 @@ pub(crate) fn core_id() -> CoreId {
}
}

pub(crate) fn core_scheduler() -> &'static mut PerCoreScheduler {
unsafe { &mut *CoreLocal::get().scheduler.get() }
pub(crate) fn core_scheduler() -> RefMut<'static, PerCoreScheduler> {
RefMut::map(CoreLocal::get().scheduler.borrow_mut(), |scheduler| {
scheduler.as_mut().unwrap()
})
}

pub(crate) fn async_tasks() -> RefMut<'static, Vec<AsyncTask>> {
CoreLocal::get().async_tasks.borrow_mut()
}

pub(crate) fn set_core_scheduler(scheduler: *mut PerCoreScheduler) {
CoreLocal::get().scheduler.set(scheduler);
pub(crate) fn set_core_scheduler(scheduler: PerCoreScheduler) {
*CoreLocal::get().scheduler.borrow_mut() = Some(scheduler);
}

pub(crate) fn increment_irq_counter(irq_no: u8) {
Expand Down
1 change: 1 addition & 0 deletions src/arch/x86_64/kernel/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::arch::x86_64::mm::{PhysAddr, VirtAddr};
use crate::config::*;
use crate::kernel;
use crate::scheduler::task::{Task, TaskFrame};
use crate::scheduler::PerCoreSchedulerExt;

#[repr(C, packed)]
struct State {
Expand Down
2 changes: 2 additions & 0 deletions src/drivers/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ pub(crate) fn network_irqhandler(_state: &State) -> bool {

#[cfg(target_arch = "x86_64")]
pub(crate) extern "x86-interrupt" fn network_irqhandler(_stack_frame: ExceptionStackFrame) {
use crate::scheduler::PerCoreSchedulerExt;

debug!("Receive network interrupt");
apic::eoi();
let _ = _irqhandler();
Expand Down
7 changes: 3 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ use mm::allocator::LockedAllocator;
pub(crate) use crate::arch::*;
pub(crate) use crate::config::*;
use crate::kernel::is_uhyve_with_pci;
use crate::scheduler::{PerCoreScheduler, PerCoreSchedulerExt};
pub use crate::syscalls::*;

#[macro_use]
Expand Down Expand Up @@ -342,9 +343,8 @@ fn boot_processor_main() -> ! {
// Start the initd task.
scheduler::PerCoreScheduler::spawn(initd, 0, scheduler::task::NORMAL_PRIO, 0, USER_STACK_SIZE);

let core_scheduler = core_scheduler();
// Run the scheduler loop.
core_scheduler.run();
PerCoreScheduler::run();
}

/// Entry Point of HermitCore for an Application Processor
Expand All @@ -358,9 +358,8 @@ fn application_processor_main() -> ! {
synch_all_cores();
crate::executor::init();

let core_scheduler = core_scheduler();
// Run the scheduler loop.
core_scheduler.run();
PerCoreScheduler::run();
}

#[cfg(target_os = "none")]
Expand Down
Loading

0 comments on commit fad2688

Please sign in to comment.