WIP: allocator

This commit is contained in:
mii443
2025-07-02 19:06:04 +09:00
parent 3c965ad857
commit 00add2d035
5 changed files with 78 additions and 2 deletions

View File

@ -35,6 +35,15 @@ dependencies = [
"spin 0.9.8",
]
[[package]]
name = "linked_list_allocator"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "549ce1740e46b291953c4340adcd74c59bcf4308f4cac050fd33ba91b7168f4a"
dependencies = [
"spinning_top",
]
[[package]]
name = "lock_api"
version = "0.4.13"
@ -54,6 +63,7 @@ name = "nel_os_kernel"
version = "0.1.0"
dependencies = [
"lazy_static",
"linked_list_allocator",
"nel_os_common",
"spin 0.10.0",
"uart_16550",
@ -96,6 +106,15 @@ dependencies = [
"lock_api",
]
[[package]]
name = "spinning_top"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b9eb1a2f4c41445a3a0ff9abc5221c5fcd28e1f13cd7c0397706f9ac938ddb0"
dependencies = [
"lock_api",
]
[[package]]
name = "uart_16550"
version = "0.3.2"

View File

@ -9,3 +9,4 @@ spin = "0.10.0"
uart_16550 = "0.3.2"
x86_64 = "0.15.2"
nel_os_common = { path = "../nel_os_common" }
linked_list_allocator = "0.9.1"

View File

@ -0,0 +1,47 @@
use linked_list_allocator::LockedHeap;
use x86_64::{
structures::paging::{
mapper::MapToError, FrameAllocator, Mapper, Page, PageTableFlags, Size4KiB,
},
VirtAddr,
};
use crate::{
constant::{HEAP_SIZE, HEAP_START},
info,
};
#[global_allocator]
static ALLOCATOR: LockedHeap = LockedHeap::empty();
pub fn init_heap(
mapper: &mut impl Mapper<Size4KiB>,
frame_allocator: &mut impl FrameAllocator<Size4KiB>,
) -> Result<(), MapToError<Size4KiB>> {
info!(
"Initializing heap: 0x{:x} - 0x{:x}",
HEAP_START,
HEAP_START + HEAP_SIZE - 1
);
let page_range = {
let heap_start = VirtAddr::new(HEAP_START as u64);
let heap_end = heap_start + HEAP_SIZE as u64 - 1u64;
let heap_start_page = Page::containing_address(heap_start);
let heap_end_page = Page::containing_address(heap_end);
Page::range_inclusive(heap_start_page, heap_end_page)
};
for page in page_range {
let frame = frame_allocator
.allocate_frame()
.ok_or(MapToError::FrameAllocationFailed)?;
let flags = PageTableFlags::PRESENT | PageTableFlags::WRITABLE;
unsafe { mapper.map_to(page, frame, flags, frame_allocator)?.flush() };
}
unsafe {
ALLOCATOR.lock().init(HEAP_START, HEAP_SIZE);
}
Ok(())
}

View File

@ -9,6 +9,8 @@ pub const PKG_VERSION: &str = env!("CARGO_PKG_VERSION");
pub const PKG_NAME: &str = env!("CARGO_PKG_NAME");
pub const KERNEL_STACK_SIZE: usize = 1024 * 1024;
pub const HEAP_START: usize = 0x4444_4444_0000;
pub const HEAP_SIZE: usize = 128 * 1024;
pub const PAGE_SIZE: usize = 4096;
pub const MAX_MEMORY: usize = 16 * 1024 * 1024 * 1024;

View File

@ -1,6 +1,7 @@
#![no_std]
#![no_main]
pub mod allocator;
pub mod constant;
pub mod logging;
pub mod memory;
@ -11,11 +12,12 @@ use core::arch::asm;
use core::panic::PanicInfo;
use core::ptr::addr_of;
use x86_64::VirtAddr;
use x86_64::{structures::paging::OffsetPageTable, VirtAddr};
use crate::{
constant::{BANNER, KERNEL_STACK_SIZE, PKG_VERSION},
memory::BitmapMemoryTable,
paging::get_active_level_4_table,
};
#[repr(C, align(16))]
@ -80,7 +82,7 @@ pub extern "sysv64" fn main(usable_memory: &nel_os_common::memory::UsableMemory)
}
info!("Usable memory: {}MiB", count / 1024 / 1024);
let bitmap_table = BitmapMemoryTable::init(usable_memory);
let mut bitmap_table = BitmapMemoryTable::init(usable_memory);
info!(
"Memory bitmap initialized: {} -> {}",
bitmap_table.start, bitmap_table.end
@ -95,5 +97,10 @@ pub extern "sysv64" fn main(usable_memory: &nel_os_common::memory::UsableMemory)
info!("Usable memory in bitmap: {}MiB", usable_frame * 4 / 1024);
let level_4_table = get_active_level_4_table();
let mut mapper = unsafe { OffsetPageTable::new(level_4_table, VirtAddr::new(0x0)) };
allocator::init_heap(&mut mapper, &mut bitmap_table).unwrap();
hlt_loop();
}