Skip to content

Commit

Permalink
Merge pull request #912 from mkroening/core-scheduler
Browse files Browse the repository at this point in the history
Make `core_scheduler` sound
  • Loading branch information
mkroening authored Sep 19, 2023
2 parents d14a777 + 8cbf7f6 commit 1872cc0
Show file tree
Hide file tree
Showing 13 changed files with 243 additions and 185 deletions.
27 changes: 19 additions & 8 deletions src/arch/aarch64/kernel/core_local.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
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;

#[cfg(feature = "smp")]
use hermit_sync::InterruptTicketMutex;

use super::interrupts::{IrqStatistics, IRQ_COUNTERS};
use super::CPU_ONLINE;
use crate::executor::task::AsyncTask;
#[cfg(feature = "smp")]
use crate::scheduler::SchedulerInput;
use crate::scheduler::{CoreId, PerCoreScheduler};

pub(crate) struct CoreLocal {
this: *const Self,
/// 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
async_tasks: RefCell<Vec<AsyncTask>>,
/// Queues to handle incoming requests from the other cores
#[cfg(feature = "smp")]
pub scheduler_input: InterruptTicketMutex<SchedulerInput>,
}

impl CoreLocal {
Expand All @@ -36,9 +44,11 @@ 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")]
scheduler_input: InterruptTicketMutex::new(SchedulerInput::new()),
};
let this = if core_id == 0 {
take_static::take_static! {
Expand Down Expand Up @@ -81,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
23 changes: 17 additions & 6 deletions src/arch/x86_64/kernel/core_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ use core::cell::{Cell, RefCell, RefMut};
use core::ptr;
use core::sync::atomic::Ordering;

#[cfg(feature = "smp")]
use hermit_sync::InterruptTicketMutex;
use x86_64::registers::model_specific::GsBase;
use x86_64::structures::tss::TaskStateSegment;
use x86_64::VirtAddr;

use super::interrupts::{IrqStatistics, IRQ_COUNTERS};
use super::CPU_ONLINE;
use crate::executor::task::AsyncTask;
#[cfg(feature = "smp")]
use crate::scheduler::SchedulerInput;
use crate::scheduler::{CoreId, PerCoreScheduler};

#[repr(C)]
Expand All @@ -20,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 All @@ -29,6 +33,9 @@ pub(crate) struct CoreLocal {
irq_statistics: &'static IrqStatistics,
/// Queue of async tasks
async_tasks: RefCell<Vec<AsyncTask>>,
/// Queues to handle incoming requests from the other cores
#[cfg(feature = "smp")]
pub scheduler_input: InterruptTicketMutex<SchedulerInput>,
}

impl CoreLocal {
Expand All @@ -47,11 +54,13 @@ 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,
async_tasks: RefCell::new(Vec::new()),
#[cfg(feature = "smp")]
scheduler_input: InterruptTicketMutex::new(SchedulerInput::new()),
};
let this = if core_id == 0 {
take_static::take_static! {
Expand Down Expand Up @@ -92,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
1 change: 1 addition & 0 deletions src/executor/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use crate::drivers::net::NetworkDriver;
use crate::drivers::pci::get_network_driver;
use crate::executor::device::HermitNet;
use crate::executor::{spawn, TaskNotify};
use crate::scheduler::PerCoreSchedulerExt;

pub(crate) enum NetworkState<'a> {
Missing,
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 1872cc0

Please sign in to comment.