Skip to content

Commit

Permalink
Merge branch 'scheduler'
Browse files Browse the repository at this point in the history
  • Loading branch information
joergroedel committed Nov 20, 2023
2 parents d1fb87d + a8db262 commit 03d5d24
Show file tree
Hide file tree
Showing 11 changed files with 863 additions and 129 deletions.
5 changes: 3 additions & 2 deletions src/cpu/idt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use super::vc::handle_vc_exception;
use super::{X86GeneralRegs, X86InterruptFrame};
use crate::address::{Address, VirtAddr};
use crate::cpu::extable::handle_exception_table;
use crate::debug::gdbstub::svsm_gdbstub::handle_bp_exception;
use crate::debug::gdbstub::svsm_gdbstub::handle_debug_exception;
use crate::types::SVSM_CS;
use core::arch::{asm, global_asm};
use core::mem;
Expand Down Expand Up @@ -206,6 +206,7 @@ fn generic_idt_handler(ctx: &mut X86ExceptionContext) {
.is_err()
&& !handle_exception_table(ctx)
{
handle_debug_exception(ctx, ctx.vector);
panic!(
"Unhandled Page-Fault at RIP {:#018x} CR2: {:#018x} error code: {:#018x}",
rip, cr2, err
Expand All @@ -214,7 +215,7 @@ fn generic_idt_handler(ctx: &mut X86ExceptionContext) {
} else if ctx.vector == VC_VECTOR {
handle_vc_exception(ctx);
} else if ctx.vector == BP_VECTOR {
handle_bp_exception(ctx);
handle_debug_exception(ctx, ctx.vector);
} else {
let err = ctx.error_code;
let vec = ctx.vector;
Expand Down
27 changes: 18 additions & 9 deletions src/cpu/percpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::mm::{
use crate::sev::ghcb::GHCB;
use crate::sev::utils::RMPFlags;
use crate::sev::vmsa::{allocate_new_vmsa, VMSASegment, VMSA};
use crate::task::RunQueue;
use crate::types::{PAGE_SHIFT, PAGE_SHIFT_2M, PAGE_SIZE, PAGE_SIZE_2M, SVSM_TR_FLAGS, SVSM_TSS};
use alloc::sync::Arc;
use alloc::vec::Vec;
Expand Down Expand Up @@ -191,19 +192,16 @@ pub struct PerCpu {
pub vrange_4k: VirtualRange,
/// Address allocator for per-cpu 2m temporary mappings
pub vrange_2m: VirtualRange,
}

impl Default for PerCpu {
fn default() -> Self {
Self::new()
}
/// Task list that has been assigned for scheduling on this CPU
runqueue: RWLock<RunQueue>,
}

impl PerCpu {
pub fn new() -> Self {
fn new(apic_id: u32) -> Self {
PerCpu {
online: AtomicBool::new(false),
apic_id: 0,
apic_id,
pgtbl: SpinLock::<PageTableRef>::new(PageTableRef::unset()),
ghcb: ptr::null_mut(),
init_stack: None,
Expand All @@ -215,15 +213,15 @@ impl PerCpu {
vm_range: VMR::new(SVSM_PERCPU_BASE, SVSM_PERCPU_END, PTEntryFlags::GLOBAL),
vrange_4k: VirtualRange::new(),
vrange_2m: VirtualRange::new(),
runqueue: RWLock::new(RunQueue::new(apic_id)),
}
}

pub fn alloc(apic_id: u32) -> Result<*mut PerCpu, SvsmError> {
let vaddr = allocate_zeroed_page()?;
unsafe {
let percpu = vaddr.as_mut_ptr::<PerCpu>();
(*percpu) = PerCpu::new();
(*percpu).apic_id = apic_id;
(*percpu) = PerCpu::new(apic_id);
PERCPU_AREAS.push(PerCpuInfo::new(apic_id, vaddr));
Ok(percpu)
}
Expand Down Expand Up @@ -566,6 +564,17 @@ impl PerCpu {
pub fn handle_pf(&self, vaddr: VirtAddr, write: bool) -> Result<(), SvsmError> {
self.vm_range.handle_page_fault(vaddr, write)
}

/// Allocate any candidate unallocated tasks from the global task list to our
/// CPU runqueue.
pub fn allocate_tasks(&mut self) {
self.runqueue.lock_write().allocate();
}

/// Access the PerCpu runqueue protected with a lock
pub fn runqueue(&self) -> &RWLock<RunQueue> {
&self.runqueue
}
}

unsafe impl Sync for PerCpu {}
Expand Down
18 changes: 14 additions & 4 deletions src/cpu/smp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
extern crate alloc;

use crate::acpi::tables::ACPICPUInfo;
use crate::cpu::percpu::{this_cpu_mut, PerCpu};
use crate::cpu::percpu::{this_cpu, this_cpu_mut, PerCpu};
use crate::cpu::vmsa::init_svsm_vmsa;
use crate::requests::request_loop;
use crate::task::{create_task, TASK_FLAG_SHARE_PT};

fn start_cpu(apic_id: u32) {
unsafe {
Expand Down Expand Up @@ -51,7 +52,7 @@ pub fn start_secondary_cpus(cpus: &[ACPICPUInfo]) {
start_cpu(c.apic_id);
count += 1;
}
log::info!("Brough {} AP(s) online", count);
log::info!("Brought {} AP(s) online", count);
}

#[no_mangle]
Expand All @@ -66,8 +67,17 @@ fn start_ap() {
// Set CPU online so that BSP can proceed
this_cpu_mut().set_online();

// Loop for now
request_loop();
// Create the task making sure the task only runs on this new AP
create_task(
ap_request_loop,
TASK_FLAG_SHARE_PT,
Some(this_cpu().get_apic_id()),
)
.expect("Failed to create AP initial task");
}

#[no_mangle]
pub extern "C" fn ap_request_loop() {
request_loop();
panic!("Returned from request_loop!");
}
Loading

0 comments on commit 03d5d24

Please sign in to comment.