feat: Prepare for PIC/APIC initialization

This commit is contained in:
ParrotXray 2025-10-13 20:47:42 +08:00
parent 10433a4fb7
commit bcf97c9e24
4 changed files with 16 additions and 81 deletions

View File

@ -187,8 +187,6 @@ pub fn _kernel_init(boot_info: &'static mut BootInfo) -> ! {
_post_init();
_test_memory_management(&mut mapper, &mut frame_allocator);
kprintln!();
kprintln!("========================================");
kprintln!(" Kernel Initialization Complete! ");
@ -207,67 +205,4 @@ pub fn kernel_emergency_cleanup() {
log_error!("Emergency cleanup triggered");
// 在 panic 前調用,做最後的清理工作
// 比如刷新緩衝區、保存日誌等
}
fn _test_memory_management(
mapper: &mut OffsetPageTable,
frame_allocator: &mut frame::BootInfoFrameAllocator
) {
kprintln!();
log_info!("Testing Memory Management System...");
// Physical memory allocation
log_debug!("Test 1: Physical frame allocation");
if let Some(frame) = pmm::allocate_frame() {
log_debug!(" Allocated frame at: {:#x}", frame.start_address().as_u64());
pmm::deallocate_frame(frame);
log_debug!(" Deallocated frame");
}
// Virtual memory allocation
log_debug!("Test 2: Virtual memory allocation (kmalloc)");
if let Some(vaddr) = malloc::kmalloc(8192, mapper, frame_allocator) {
log_debug!(" Allocated 8KB at: {:#x}", vaddr.as_u64());
// Test Write
unsafe {
let ptr = vaddr.as_mut_ptr::<u64>();
*ptr = 0xDEADBEEF;
log_debug!(" Written test value: {:#x}", *ptr);
}
malloc::kfree(vaddr, 8192, mapper, frame_allocator);
log_debug!(" Freed memory");
}
// Page table mapping
log_debug!("Test 3: Page table mapping");
let test_vaddr = VirtAddr::new(0x5000_0000_0000);
let test_page = Page::containing_address(test_vaddr);
if let Some(test_frame) = pmm::allocate_frame() {
if paging::PageTableManager::map_page(
test_page,
test_frame,
paging::kernel_data(),
mapper,
frame_allocator
).is_ok() {
log_debug!(" Mapped page {:#x} to frame {:#x}",
test_vaddr.as_u64(), test_frame.start_address().as_u64());
// Testing Address Translation
if let Some(phys) = paging::PageTableManager::translate_addr(test_vaddr, mapper) {
log_debug!(" Translation check: {:#x} -> {:#x}", test_vaddr.as_u64(), phys.as_u64());
}
// Unmap
if paging::PageTableManager::unmap_page(test_page, mapper).is_ok() {
log_debug!(" Unmapped page");
}
}
pmm::deallocate_frame(test_frame);
}
log_info!("Memory tests completed!");
}

View File

@ -17,14 +17,17 @@ pub fn _kernel_main() -> ! {
);
kprintln!();
vma::print_info();
vmm::get_vmm_stats().print();
kprintln!();
log_debug!("CR0: 0x{:016x}", cpu::cpu_r_cr0());
log_debug!("CR2: 0x{:016x}", cpu::cpu_r_cr2());
log_debug!("CR3: 0x{:016x}", cpu::cpu_r_cr3());
log_debug!("CR4: 0x{:016x}", cpu::cpu_r_cr4());
vma::print_info();
let vmm_stats = vmm::get_vmm_stats();
vmm_stats.print();
// kprintln!();
// kprintln!("=== Logger Level Demonstration ===");

View File

@ -141,32 +141,31 @@ pub fn get_region_name(addr: VirtAddr) -> &'static str {
/// Print hhk address space layout
pub fn print_info() {
log_info!("Higher Half Kernel Memory Layout:");
log_info!(" User Space: {:#018x} - {:#018x}",
log_info!("User Space: {:#018x} - {:#018x}",
0x0u64,
0x0000_7FFF_FFFF_FFFFu64
);
log_info!(" Physical Map: {:#018x} - {:#018x}",
log_info!("Physical Map: {:#018x} - {:#018x}",
PHYS_MEM_OFFSET,
HIGHER_HALF_BASE - 1
);
log_info!(" Kernel Base: {:#018x}", HIGHER_HALF_BASE);
log_info!(" Heap: {:#018x} - {:#018x} ({} KiB)",
log_info!("Kernel Base: {:#018x}", HIGHER_HALF_BASE);
log_info!("Heap: {:#018x} - {:#018x} ({} KiB)",
HEAP_START.as_u64(),
HEAP_START.as_u64() + HEAP_SIZE as u64,
HEAP_SIZE / 1024
);
log_info!(" Dynamic: {:#018x} - {:#018x} ({} MiB)",
log_info!("Dynamic: {:#018x} - {:#018x} ({} MiB)",
KERNEL_DYNAMIC_START.as_u64(),
KERNEL_DYNAMIC_END.as_u64(),
KERNEL_DYNAMIC_SIZE / (1024 * 1024)
);
log_info!(" Kernel Stack: {:#018x} - {:#018x} ({} MiB)",
log_info!("Kernel Stack: {:#018x} - {:#018x} ({} MiB)",
KERNEL_STACK_START.as_u64(),
KERNEL_STACK_END.as_u64(),
KERNEL_STACK_SIZE / (1024 * 1024)
);
log_info!(" Device Mapping: {:#018x} - {:#018x} ({} MiB)",
log_info!("Device Mapping: {:#018x} - {:#018x} ({} MiB)",
DEVICE_MAPPING_START.as_u64(),
DEVICE_MAPPING_END.as_u64(),
DEVICE_MAPPING_SIZE / (1024 * 1024)

View File

@ -21,7 +21,6 @@ struct MemoryBlock {
page_count: usize,
}
#[derive(Debug, Clone, Copy)]
pub struct VmmStats {
pub next_vaddr: VirtAddr,
@ -35,18 +34,17 @@ pub struct VmmStats {
impl VmmStats {
pub fn print(&self) {
log_info!("Virtual Memory Statistics:");
log_info!(" Allocated: {} pages ({} KiB) in {} blocks",
log_info!("Allocated: {} pages ({} KiB) in {} blocks",
self.allocated_pages,
self.allocated_pages * 4,
self.allocated_blocks_count
);
log_info!(" Free: {} pages ({} KiB) in {} blocks",
log_info!("Free: {} pages ({} KiB) in {} blocks",
self.free_pages,
self.free_pages * 4,
self.free_blocks_count
);
log_info!(" New Usage: {} pages ({} KiB)",
log_info!("New Usage: {} pages ({} KiB)",
self.used_from_new,
self.used_from_new * 4
);