From a33b984b08126cfbf270141eafc9377ed27afe75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Thu, 24 Aug 2023 17:11:45 +0200 Subject: [PATCH] [WIP] feat(core_local): statically allocate first CoreLocal MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Kröning --- Cargo.lock | 12 ++++++++++++ Cargo.toml | 1 + src/arch/x86_64/kernel/core_local.rs | 24 +++++++++++++++++++++--- src/arch/x86_64/kernel/mod.rs | 1 + 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2af63d1b97..2db7aafa69 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -92,6 +92,10 @@ version = "1.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" +[[package]] +name = "call-once" +version = "0.1.0" + [[package]] name = "cc" version = "1.0.82" @@ -347,6 +351,7 @@ dependencies = [ "shell-words", "smallvec", "smoltcp", + "take-static", "talc", "time", "tock-registers", @@ -833,6 +838,13 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "take-static" +version = "0.1.0" +dependencies = [ + "call-once", +] + [[package]] name = "talc" version = "2.2.2" diff --git a/Cargo.toml b/Cargo.toml index 12dc3910a9..27b0105b67 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -88,6 +88,7 @@ qemu-exit = "3.0" rand_chacha = { version = "0.3", default-features = false } shell-words = { version = "1.1", default-features = false } smallvec = { version = "1", features = ["const_new"] } +take-static = { path = "../../exclusive_cell" } talc = { version = "2" } time = { version = "0.3", default-features = false } zerocopy = "0.6" diff --git a/src/arch/x86_64/kernel/core_local.rs b/src/arch/x86_64/kernel/core_local.rs index 5aec27b29d..1c6e9e993c 100644 --- a/src/arch/x86_64/kernel/core_local.rs +++ b/src/arch/x86_64/kernel/core_local.rs @@ -37,8 +37,12 @@ impl CoreLocal { let core_id = CPU_ONLINE.load(Ordering::Relaxed); - let irq_statistics = &*Box::leak(Box::new(IrqStatistics::new())); - IRQ_COUNTERS.lock().insert(core_id, irq_statistics); + let irq_statistics = if core_id == 0 { + static FIRST_IRQ_STATISTICS: IrqStatistics = IrqStatistics::new(); + &FIRST_IRQ_STATISTICS + } else { + &*Box::leak(Box::new(IrqStatistics::new())) + }; let this = Self { this: ptr::null_mut(), @@ -49,7 +53,15 @@ impl CoreLocal { irq_statistics, async_tasks: RefCell::new(Vec::new()), }; - let this = Box::leak(Box::new(this)); + let this = if core_id == 0 { + take_static::take_static! { + static FIRST_CORE_LOCAL: TakeStatic> = TakeStatic::new(None); + } + FIRST_CORE_LOCAL.take().unwrap().insert(this) + } else { + this.add_irq_counter(); + Box::leak(Box::new(this)) + }; this.this = &*this; GsBase::write(VirtAddr::from_ptr(this)); @@ -64,6 +76,12 @@ impl CoreLocal { &*raw } } + + pub fn add_irq_counter(&self) { + IRQ_COUNTERS + .lock() + .insert(self.core_id, self.irq_statistics); + } } pub(crate) fn core_id() -> CoreId { diff --git a/src/arch/x86_64/kernel/mod.rs b/src/arch/x86_64/kernel/mod.rs index c9c0e7900d..a8afad3ec3 100644 --- a/src/arch/x86_64/kernel/mod.rs +++ b/src/arch/x86_64/kernel/mod.rs @@ -213,6 +213,7 @@ pub fn boot_processor_init() { crate::mm::init(); crate::mm::print_information(); + CoreLocal::get().add_irq_counter(); env::init(); gdt::add_current_core(); interrupts::load_idt();