refactor mega-commit.

- reorganised the entire project so that the entire kernel is a single codebase rather than a kernel and a libk.
This commit is contained in:
2025-03-03 02:49:56 +00:00
parent 53d325749d
commit 3966e697da
57 changed files with 331 additions and 997 deletions
@@ -0,0 +1,249 @@
use core::alloc::{GlobalAlloc, Layout};
use core::ptr;
use spin::{Mutex, MutexGuard};
use x86_64::structures::paging::{Size4KiB, mapper::MapToError};
/// We are currently using a linked list heap allocator which uses our underlying page allocator.
#[global_allocator]
/// This is now Rust's global allocator, so we can use stuff requiring heap allocations.
static ALLOCATOR: Locked<FoundryAllocator> = Locked::new(FoundryAllocator::new());
pub const HEAP_START: usize = 0x4444_4444_0000;
pub const HEAP_SIZE: usize = 1024 * 1024 * 1024;
/// Sets up the heap using the backing page frame allocator.
pub unsafe fn init_heap() -> Result<(), MapToError<Size4KiB>> {
unsafe {
// code to allocate frames is now done in the page fault interrupt handler!
ALLOCATOR.lock().init(HEAP_START, HEAP_SIZE);
Ok(())
}
}
pub struct Locked<T> {
inner: Mutex<T>,
}
impl<T> Locked<T> {
pub const fn new(inner: T) -> Self {
Locked {
inner: Mutex::new(inner),
}
}
pub fn lock(&self) -> MutexGuard<T> {
self.inner.lock()
}
}
const BLOCK_SIZES: &[usize] = &[8, 16, 32, 64, 128, 256, 512, 1024, 2048];
struct ListNode {
next: Option<&'static mut ListNode>,
}
pub struct FoundryAllocator {
list_heads: [Option<&'static mut ListNode>; BLOCK_SIZES.len()],
fallback: Locked<FoundryFallbackAllocator>,
}
impl Default for FoundryAllocator {
fn default() -> Self {
Self::new()
}
}
impl FoundryAllocator {
pub const fn new() -> Self {
const EMPTY: Option<&'static mut ListNode> = None;
Self {
list_heads: [EMPTY; BLOCK_SIZES.len()],
fallback: Locked::new(FoundryFallbackAllocator::new()),
}
}
pub unsafe fn init(&mut self, heap_start: usize, heap_size: usize) {
unsafe {
self.fallback.lock().init(heap_start, heap_size);
}
}
unsafe fn fallback_alloc(&mut self, layout: Layout) -> *mut u8 {
unsafe { self.fallback.alloc(layout) }
}
fn block_size(&self, layout: &Layout) -> Option<usize> {
let required_block_size = layout.size().max(layout.align());
BLOCK_SIZES.iter().position(|&s| s >= required_block_size)
}
}
unsafe impl GlobalAlloc for Locked<FoundryAllocator> {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let mut allocator = self.lock();
match allocator.block_size(&layout) {
Some(index) => {
match allocator.list_heads[index].take() {
Some(node) => {
allocator.list_heads[index] = node.next.take();
node as *mut ListNode as *mut u8
}
None => {
// no block exists in list => allocate new block
let block_size = BLOCK_SIZES[index];
// only works if all block sizes are a power of 2
let block_align = block_size;
let layout = Layout::from_size_align(block_size, block_align).unwrap();
unsafe { allocator.fallback_alloc(layout) }
}
}
}
None => unsafe { allocator.fallback_alloc(layout) },
}
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
let mut allocator = self.lock();
match allocator.block_size(&layout) {
Some(idx) => {
let new_node = ListNode {
next: allocator.list_heads[idx].take(),
};
let new_ptr = ptr as *mut ListNode;
unsafe { new_ptr.write(new_node) };
allocator.list_heads[idx] = Some(unsafe { &mut *new_ptr });
}
None => {
unsafe { allocator.fallback.dealloc(ptr, layout) };
}
}
}
}
struct FallbackListNode {
size: usize,
next: Option<&'static mut FallbackListNode>,
}
impl FallbackListNode {
const fn new(size: usize) -> Self {
Self { size, next: None }
}
fn start_addr(&self) -> usize {
self as *const Self as usize
}
fn end_addr(&self) -> usize {
self.start_addr() + self.size
}
}
pub struct FoundryFallbackAllocator {
head: FallbackListNode,
}
impl FoundryFallbackAllocator {
pub const fn new() -> Self {
Self {
head: FallbackListNode::new(0),
}
}
pub unsafe fn init(&mut self, heap_start: usize, heap_size: usize) {
unsafe { self.add_region(heap_start, heap_size) };
}
unsafe fn add_region(&mut self, addr: usize, size: usize) {
unsafe {
let mut node = FallbackListNode::new(size);
node.next = self.head.next.take();
let node_ptr = addr as *mut FallbackListNode;
node_ptr.write(node);
self.head.next = Some(&mut *node_ptr);
}
}
fn find_region(
&mut self,
size: usize,
align: usize,
) -> Option<(&'static mut FallbackListNode, usize)> {
let mut current = &mut self.head;
// look for a large enough memory region in linked list
while let Some(ref mut region) = current.next {
if let Ok(alloc_start) = Self::alloc_from_region(&region, size, align) {
// region suitable for allocation -> remove node from list
let next = region.next.take();
let ret = Some((current.next.take().unwrap(), alloc_start));
current.next = next;
return ret;
} else {
// region not suitable -> continue with next region
current = current.next.as_mut().unwrap();
}
}
None
}
const fn align_up(addr: usize, align: usize) -> usize {
(addr + align - 1) & !(align - 1)
}
fn size_align(layout: Layout) -> (usize, usize) {
let layout = layout
.align_to(align_of::<ListNode>())
.expect("adjusting alignment failed")
.pad_to_align();
let size = layout.size().max(size_of::<ListNode>());
(size, layout.align())
}
fn alloc_from_region(
region: &FallbackListNode,
size: usize,
align: usize,
) -> Result<usize, ()> {
let alloc_start = Self::align_up(region.start_addr(), align);
let alloc_end = alloc_start.checked_add(size).ok_or(())?;
if alloc_end > region.end_addr() {
return Err(());
}
let excess_size = region.end_addr() - alloc_end;
if excess_size > 0 && excess_size < size_of::<ListNode>() {
return Err(());
}
Ok(alloc_start)
}
}
unsafe impl GlobalAlloc for Locked<FoundryFallbackAllocator> {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let mut allocator = self.lock();
// perform layout adjustments
let (size, align) = FoundryFallbackAllocator::size_align(layout);
if let Some((region, alloc_start)) = allocator.find_region(size, align) {
let alloc_end = alloc_start.checked_add(size).expect("overflow");
let excess_size = region.end_addr() - alloc_end;
if excess_size > 0 {
unsafe { allocator.add_region(alloc_end, excess_size) };
}
alloc_start as *mut u8
} else {
ptr::null_mut()
}
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
let mut allocator = self.lock();
// perform layout adjustments
let (size, _) = FoundryFallbackAllocator::size_align(layout);
unsafe { allocator.add_region(ptr as usize, size) }
}
}
@@ -0,0 +1 @@
@@ -0,0 +1,2 @@
pub mod allocator;
mod foundry_kalloc;
+146
View File
@@ -0,0 +1,146 @@
// use lib_alloc::allocator::FoundryAllocator;
use limine::{memory_map::EntryType, response::MemoryMapResponse};
use spin::{Mutex, Once};
use x86_64::{
PhysAddr, VirtAddr,
registers::control::Cr3,
structures::paging::{FrameAllocator, OffsetPageTable, PageTable, PhysFrame, Size4KiB},
};
pub static FRAME_ALLOCATOR: Once<Mutex<FoundryOSFrameAllocator>> = Once::new();
pub static OFFSET_PAGE_TABLE: Once<Mutex<OffsetPageTable>> = Once::new();
/// Returns a mutable reference to the current level 4 page table.
///
/// # Safety
///
/// The caller must ensure that the level 4 page table is not modified
/// simultaneously. The caller must also ensure that the physical memory offset
/// is correct, to ensure that the correct virtual address is constructed.
unsafe fn active_l4_table(physical_memory_offset: VirtAddr) -> &'static mut PageTable {
let (level_4_frame, _) = Cr3::read();
let phys_addr = level_4_frame.start_address();
let virt = phys_addr.as_u64() + physical_memory_offset.as_u64();
unsafe { &mut *(virt as *mut PageTable) }
}
/// Initializes the `OffsetPageTable` for the current CPU architecture.
///
/// # Safety
///
/// This function must be called only once and should be called before any
/// memory operations are performed that rely on virtual memory management.
/// The provided `physical_memory_offset` must be accurate to ensure correct
/// translation of physical addresses.
///
/// # Parameters
///
/// - `physical_memory_offset`: The offset to convert physical addresses to
/// virtual addresses in the higher-half direct map.
///
/// # Returns
///
/// Returns an `OffsetPageTable` that allows for manipulation of the page
/// tables for the current CPU architecture.
pub fn init_page_table(physical_memory_offset: VirtAddr) {
unsafe {
let l4_table = active_l4_table(physical_memory_offset);
let offset_table = OffsetPageTable::new(l4_table, physical_memory_offset);
OFFSET_PAGE_TABLE.call_once(|| Mutex::new(offset_table));
}
}
pub struct FoundryOSFrameAllocator {
memory_map: &'static MemoryMapResponse,
next: usize,
}
pub fn init_frame_allocator(memory_map: &'static MemoryMapResponse) {
unsafe {
FRAME_ALLOCATOR.call_once(|| Mutex::new(FoundryOSFrameAllocator::init(memory_map)));
}
}
impl FoundryOSFrameAllocator {
/// Creates a new `FoundryOSFrameAllocator` from a memory map.
///
/// This function takes a reference to a `MemoryMapResponse` and initializes a
/// `FoundryOSFrameAllocator` with it. The `next` field is set to 0, indicating that
/// the first frame to be allocated is the first frame in the memory map.
pub const unsafe fn init(memory_map: &'static MemoryMapResponse) -> Self {
Self {
memory_map,
next: 0,
}
}
pub fn count_usable_frames(&self) -> u32 {
self.usable_frames().count() as u32
}
/// An iterator over all usable frames in the memory map.
///
/// Yields one `PhysFrame` for each available 4KiB frame in the memory map.
///
/// This function is used to allocate frames for the pagemap.
fn usable_frames(&self) -> impl Iterator<Item = PhysFrame> + use<> {
let regions = self.memory_map.entries().iter();
let usable_regions = regions.filter(|region| region.entry_type == EntryType::USABLE);
let addr_ranges = usable_regions.map(|region| region.base..region.base + region.length);
let frame_addresses = addr_ranges.flat_map(|r| r.step_by(4096));
frame_addresses.map(|addr| PhysFrame::from_start_address(PhysAddr::new(addr)).unwrap())
}
}
unsafe impl FrameAllocator<Size4KiB> for FoundryOSFrameAllocator {
/// Allocates a frame from the list of usable frames.
///
/// This function returns the next available `PhysFrame` from the memory map,
/// if one exists. Once a frame is allocated, the internal counter is incremented
/// to point to the next frame for future allocations.
///
/// # Returns
///
/// - `Some(PhysFrame)`: If a usable frame is available.
/// - `None`: If there are no more usable frames to allocate.
fn allocate_frame(&mut self) -> Option<PhysFrame> {
let frame = self.usable_frames().nth(self.next);
self.next += 1;
frame
}
}
// pub unsafe fn translate_addr(addr: VirtAddr, physical_memory_offset: VirtAddr) -> Option<PhysAddr> {
// translate_addr_inner(addr, physical_memory_offset)
// }
// fn translate_addr_inner(addr: VirtAddr, physical_memory_offset: VirtAddr) -> Option<PhysAddr> {
// let (l4_table_frame, _) = Cr3::read();
// let table_indexes = [
// addr.p4_index(),
// addr.p3_index(),
// addr.p2_index(),
// addr.p1_index(),
// ];
// let mut frame = l4_table_frame;
// for &i in &table_indexes {
// let virt = physical_memory_offset + frame.start_address().as_u64();
// let table_ptr: *const PageTable = virt.as_ptr();
// let table = unsafe { &*table_ptr };
// let entry = &table[i];
// frame = match entry.frame() {
// Ok(frame) => frame,
// Err(FrameError::FrameNotPresent) => return None,
// Err(FrameError::HugeFrame) => panic!("huge frames are not supported!"),
// };
// }
// Some(frame.start_address() + u64::from(addr.page_offset()))
// }
@@ -0,0 +1,55 @@
//! Sets up a memory map using Limine.
use limine::{
request::{HhdmRequest, KernelAddressRequest, MemoryMapRequest},
response::MemoryMapResponse,
};
use spin::Lazy;
#[used]
#[unsafe(link_section = ".requests")]
static MEMORY_MAP_REQUEST: MemoryMapRequest = MemoryMapRequest::new();
#[used]
#[unsafe(link_section = ".requests")]
static HIGHER_HALF_DIRECT_MAP_REQUEST: HhdmRequest = HhdmRequest::new();
#[used]
#[unsafe(link_section = ".requests")]
static KERNEL_ADDRESS_REQUEST: KernelAddressRequest = KernelAddressRequest::new();
/// ```rs
/// let virt_addr = phys_addr + offset;
/// let phys_addr = virt_addr - offset; // (given VA is in the HHDM). Do not use for executable code.
/// ```
pub static PHYSICAL_MEMORY_OFFSET: Lazy<u64> = Lazy::new(|| {
HIGHER_HALF_DIRECT_MAP_REQUEST
.get_response()
.unwrap()
.offset()
});
/// Converts virtual addresses in the kernel to a physical address like this:
/// ```rs
/// let phys_addr = virt_addr - virtual_base + physical_base;
/// ```
///
/// Returns (virtual_base, physical_base)
pub static _KERNEL_PHYSICAL_MEMORY_OFFSET: Lazy<(u64, u64)> = Lazy::new(|| {
let resp = KERNEL_ADDRESS_REQUEST.get_response().unwrap();
// These are base addresses, using Limine's built in page table.
(resp.virtual_base(), resp.physical_base())
});
/// Fetches the memory map from Limine.
///
/// # Panics
///
/// Panics if the memory map was not found in MEMORY_MAP_REQUEST.
pub fn get_memory_map() -> &'static MemoryMapResponse {
MEMORY_MAP_REQUEST.get_response().map_or_else(
|| unreachable!("Could not fetch memory map from Limine."),
|memory_map| memory_map,
)
}
+3
View File
@@ -0,0 +1,3 @@
pub mod allocation;
pub mod memory;
pub mod memory_map;