From 4cd76ea9e3257180cabf4afa9a9ab9f2ccd689f2 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Sat, 9 Sep 2023 12:31:04 -0600 Subject: [PATCH] arch/x86: sync LogicalCpuId changes --- src/arch/x86/gdt.rs | 4 +++- src/arch/x86/idt.rs | 12 ++++++------ src/arch/x86/rmm.rs | 8 +++++--- src/arch/x86/start.rs | 15 +++++++++------ src/debugger.rs | 2 +- 5 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/arch/x86/gdt.rs b/src/arch/x86/gdt.rs index fb73e23a79..d5c9304691 100644 --- a/src/arch/x86/gdt.rs +++ b/src/arch/x86/gdt.rs @@ -4,6 +4,8 @@ use core::convert::TryInto; use core::mem; use core::ptr::addr_of_mut; +use crate::LogicalCpuId; + use x86::bits32::task::TaskStateSegment; use x86::Ring; use x86::dtables::{self, DescriptorTablePointer}; @@ -124,7 +126,7 @@ pub unsafe fn init() { } /// Initialize GDT and configure percpu. -pub unsafe fn init_paging(stack_offset: usize, cpu_id: usize) { +pub unsafe fn init_paging(stack_offset: usize, cpu_id: LogicalCpuId) { let pcr_frame = crate::memory::allocate_frames(1).expect("failed to allocate PCR frame"); let pcr = &mut *(RmmA::phys_to_virt(pcr_frame.start_address()).data() as *mut ProcessorControlRegion); diff --git a/src/arch/x86/idt.rs b/src/arch/x86/idt.rs index b97897043f..beb0a99ec0 100644 --- a/src/arch/x86/idt.rs +++ b/src/arch/x86/idt.rs @@ -8,7 +8,7 @@ use alloc::collections::BTreeMap; use x86::segmentation::Descriptor as X86IdtEntry; use x86::dtables::{self, DescriptorTablePointer}; -use crate::interrupt::*; +use crate::{interrupt::*, LogicalCpuId}; use crate::ipi::IpiKind; use spin::RwLock; @@ -68,10 +68,10 @@ impl Idt { static mut INIT_BSP_IDT: Idt = Idt::new(); // TODO: VecMap? -pub static IDTS: RwLock>> = RwLock::new(None); +pub static IDTS: RwLock>> = RwLock::new(None); #[inline] -pub fn is_reserved(cpu_id: usize, index: u8) -> bool { +pub fn is_reserved(cpu_id: LogicalCpuId, index: u8) -> bool { let byte_index = index / 32; let bit = index % 32; @@ -79,7 +79,7 @@ pub fn is_reserved(cpu_id: usize, index: u8) -> bool { } #[inline] -pub fn set_reserved(cpu_id: usize, index: u8, reserved: bool) { +pub fn set_reserved(cpu_id: LogicalCpuId, index: u8, reserved: bool) { let byte_index = index / 32; let bit = index % 32; @@ -97,7 +97,7 @@ pub fn allocate_interrupt() -> Option { None } -pub fn available_irqs_iter(cpu_id: usize) -> impl Iterator + 'static { +pub fn available_irqs_iter(cpu_id: LogicalCpuId) -> impl Iterator + 'static { (32..=254).filter(move |&index| !is_reserved(cpu_id, index)) } @@ -126,7 +126,7 @@ const fn new_idt_reservations() -> [AtomicU32; 8] { } /// Initialize the IDT for a -pub unsafe fn init_paging_post_heap(is_bsp: bool, cpu_id: usize) { +pub unsafe fn init_paging_post_heap(is_bsp: bool, cpu_id: LogicalCpuId) { let mut idts_guard = IDTS.write(); let idts_btree = idts_guard.get_or_insert_with(BTreeMap::new); diff --git a/src/arch/x86/rmm.rs b/src/arch/x86/rmm.rs index 92f8394cf9..d4c1d7e3ba 100644 --- a/src/arch/x86/rmm.rs +++ b/src/arch/x86/rmm.rs @@ -24,6 +24,8 @@ use rmm::{ }; use spin::Mutex; +use crate::LogicalCpuId; + use super::CurrentRmmArch as RmmA; extern "C" { @@ -297,14 +299,14 @@ impl KernelMapper { prev_count > 0 } - pub unsafe fn lock_for_manual_mapper(current_processor: usize, mapper: crate::paging::PageMapper) -> Self { - let ro = Self::lock_inner(current_processor); + pub unsafe fn lock_for_manual_mapper(current_processor: LogicalCpuId, mapper: crate::paging::PageMapper) -> Self { + let ro = Self::lock_inner(current_processor.get() as usize); Self { mapper, ro, } } - pub fn lock_manually(current_processor: usize) -> Self { + pub fn lock_manually(current_processor: LogicalCpuId) -> Self { unsafe { Self::lock_for_manual_mapper(current_processor, PageMapper::current(TableKind::Kernel, FRAME_ALLOCATOR)) } } pub fn lock() -> Self { diff --git a/src/arch/x86/start.rs b/src/arch/x86/start.rs index 8f29c8380a..c3dff61551 100644 --- a/src/arch/x86/start.rs +++ b/src/arch/x86/start.rs @@ -4,9 +4,9 @@ /// defined in other files inside of the `arch` module use core::slice; -use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; +use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering, AtomicU32}; -use crate::allocator; +use crate::{allocator, LogicalCpuId}; #[cfg(feature = "acpi")] use crate::acpi; use crate::arch::pti; @@ -28,7 +28,10 @@ static DATA_TEST_NONZERO: usize = usize::max_value(); pub static KERNEL_BASE: AtomicUsize = AtomicUsize::new(0); pub static KERNEL_SIZE: AtomicUsize = AtomicUsize::new(0); -pub static CPU_COUNT: AtomicUsize = AtomicUsize::new(0); + +// TODO: This probably shouldn't be an atomic. Only the BSP starts APs. +pub static CPU_COUNT: AtomicU32 = AtomicU32::new(0); + pub static AP_READY: AtomicBool = AtomicBool::new(false); static BSP_READY: AtomicBool = AtomicBool::new(false); @@ -128,7 +131,7 @@ pub unsafe extern fn kstart(args_ptr: *const KernelArgs) -> ! { paging::init(); // Set up GDT after paging with TLS - gdt::init_paging(args.stack_base as usize + args.stack_size as usize, 0); + gdt::init_paging(args.stack_base as usize + args.stack_size as usize, LogicalCpuId::BSP); // Set up IDT idt::init_paging_bsp(); @@ -148,7 +151,7 @@ pub unsafe extern fn kstart(args_ptr: *const KernelArgs) -> ! { #[cfg(feature = "graphical_debug")] graphical_debug::init_heap(); - idt::init_paging_post_heap(true, 0); + idt::init_paging_post_heap(true, LogicalCpuId::BSP); // Activate memory logging log::init(); @@ -202,7 +205,7 @@ pub struct KernelArgsAp { pub unsafe extern fn kstart_ap(args_ptr: *const KernelArgsAp) -> ! { let cpu_id = { let args = &*args_ptr; - let cpu_id = args.cpu_id as usize; + let cpu_id = LogicalCpuId::new(args.cpu_id as u32); let bsp_table = args.page_table as usize; let _stack_start = args.stack_start as usize; let stack_end = args.stack_end as usize; diff --git a/src/debugger.rs b/src/debugger.rs index 3476785275..0ba89fff78 100644 --- a/src/debugger.rs +++ b/src/debugger.rs @@ -110,7 +110,7 @@ pub unsafe fn debugger(target_id: Option) { for (id, context_lock) in crate::context::contexts().iter() { if target_id.map_or(false, |target_id| *id != target_id) { continue; } let context = context_lock.read(); - println!("{}: {}", (*id).into(), context.name); + println!("{}: {}", (*id).get(), context.name); // Switch to context page table to ensure syscall debug and stack dump will work if let Some(ref space) = context.addr_space {