From 7002d2feab970aa740820d9c3a42891c6f53bb21 Mon Sep 17 00:00:00 2001 From: August Date: Thu, 6 Nov 2025 01:34:24 -0500 Subject: [PATCH] Investigate talc dealloc panic deadlock --- Cargo.lock | 25 ++++++++++++------------- src/kernel/arch/x86_64/paging.rs | 24 +++++++++++++++++++++--- src/kernel/main.rs | 2 ++ src/kernel/panic.rs | 2 +- 4 files changed, 36 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6afea4f..bd6e914 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -45,9 +45,9 @@ checksum = "1e4b40c7323adcfc0a41c4b88143ed58346ff65a288fc144329c5c45e05d70c6" [[package]] name = "bitflags" -version = "2.9.4" +version = "2.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2261d10cca569e4643e526d8dc2e62e433cc8aba21ab764233731f8d369bf394" +checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3" [[package]] name = "build_const" @@ -177,11 +177,10 @@ dependencies = [ [[package]] name = "lock_api" -version = "0.4.13" +version = "0.4.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "96936507f153605bddfcda068dd804796c84324ed2510809e5b2a624c81da765" +checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965" dependencies = [ - "autocfg", "scopeguard", ] @@ -255,18 +254,18 @@ checksum = "f84267b20a16ea918e43c6a88433c2d54fa145c92a811b5b047ccbe153674483" [[package]] name = "proc-macro2" -version = "1.0.101" +version = "1.0.103" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89ae43fd86e4158d6db51ad8e2b80f313af9cc74f5c0e03ccb87de09998732de" +checksum = "5ee95bc4ef87b8d5ba32e8b7714ccc834865276eab0aed5c9958d00ec45f49e8" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.40" +version = "1.0.41" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" +checksum = "ce25767e7b499d1b604768e7cde645d14cc8584231ea6b295e9c9eb22c02e1d1" dependencies = [ "proc-macro2", ] @@ -330,9 +329,9 @@ checksum = "5cdeee85371b1ec1f4b305c91787271a39f56b66e673bdbd73b7742150de5b0e" [[package]] name = "syn" -version = "2.0.106" +version = "2.0.109" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ede7c438028d4436d71104916910f5bb611972c5cfd7f89b8300a8186e6fada6" +checksum = "2f17c7e013e88258aa9543dcbe81aca68a667a9ac37cd69c9fbc07858bfe0e2f" dependencies = [ "proc-macro2", "quote", @@ -361,9 +360,9 @@ dependencies = [ [[package]] name = "unicode-ident" -version = "1.0.19" +version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f63a545481291138910575129486daeaf8ac54aee4387fe7906919f7830c7d9d" +checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" [[package]] name = "volatile" diff --git a/src/kernel/arch/x86_64/paging.rs b/src/kernel/arch/x86_64/paging.rs index 1c49e82..90c8607 100644 --- a/src/kernel/arch/x86_64/paging.rs +++ b/src/kernel/arch/x86_64/paging.rs @@ -5,13 +5,20 @@ use crate::constants::KERNEL_BUILD_PROFILE; use crate::{LOGGER, LogLevel, format, log_info, log_trace, memory::HHDM_RESPONSE}; +use crate::memory::alloc::boxed::Box; use free_list::{PAGE_SIZE, PageLayout}; +use lazy_static::lazy_static; +use spin::Mutex; use x86_64::{ PhysAddr, VirtAddr, registers::control::*, structures::paging::{PageTable, PageTableFlags}, }; +lazy_static! { + pub static ref KERNEL_PML4: Mutex>> = Mutex::new(None); +} + pub fn switch_ptable() { let allocated_region = crate::memory::FREELIST .lock() @@ -20,9 +27,20 @@ pub fn switch_ptable() { log_info!("Got region for new PML4: 0x{:x}", allocated_region.start()); let pml4_start_vaddr = allocated_region.start() + HHDM_RESPONSE.offset() as usize; let pml4_ptr = pml4_start_vaddr as *mut PageTable; - let mut pml4 = unsafe { crate::memory::alloc::boxed::Box::from_raw(pml4_ptr) }; - *pml4 = PageTable::new(); - log_info!("Initialized page table at 0x{:p}", pml4); + + /* + Dropping this PML4 causes a deadlock. Here is why: + - The allocator, Talc, did not allocate the memory we built the PML4 in. + - When this function exits, the allocator is *locked*. + - The allocator is told to drop this PML4, and panics, because it was never "allocated" to begin with. + - The allocator panics. + - The panic function tries to call the logging code. + - The logging code tries to acquire lock on the allocator, but it will never get it, since it is locked + until the function finishes exiting. Which it won't, because the allocator panicked. + */ + //let mut pml4 = unsafe { crate::memory::alloc::boxed::Box::from_raw(pml4_ptr) }; + //*pml4 = PageTable::new(); + //log_info!("Initialized page table at 0x{:p}", pml4); } pub fn get_mappings() { diff --git a/src/kernel/main.rs b/src/kernel/main.rs index 32a7cf7..4075981 100644 --- a/src/kernel/main.rs +++ b/src/kernel/main.rs @@ -4,6 +4,7 @@ #![no_std] #![no_main] #![feature(abi_x86_interrupt)] +#![feature(breakpoint)] mod arch; mod boot; @@ -160,6 +161,7 @@ unsafe extern "C" fn main() -> ! { log_info!("Virtualization provider: {:?}", virt_supported()); arch::x86_64::paging::switch_ptable(); + log_info!("Made it this far"); panic!("Finished boot, but cannot start init because processes not implemented!"); } diff --git a/src/kernel/panic.rs b/src/kernel/panic.rs index 4c773dc..dfa9f2d 100644 --- a/src/kernel/panic.rs +++ b/src/kernel/panic.rs @@ -3,8 +3,8 @@ use core::panic::*; -use crate::format; use crate::{LOGGER, LogLevel}; +use crate::format; #[panic_handler] pub fn panic(info: &PanicInfo) -> ! {