reorganised memory. starting on threads

This commit is contained in:
2025-03-03 15:50:58 +00:00
parent 2d3bc56962
commit a0f5ce8797
8 changed files with 113 additions and 59 deletions
@@ -1 +0,0 @@
@@ -1,2 +1,2 @@
pub mod allocator;
mod foundry_kalloc;
pub mod heap_alloc;
pub mod stack_alloc;
@@ -0,0 +1,59 @@
use x86_64::structures::paging::{mapper, FrameAllocator, Mapper, Page, Size4KiB};
use x86_64::VirtAddr;
fn reserve_stack_memory(size_in_pages: u64) -> Page {
use core::sync::atomic::{AtomicU64, Ordering};
static STACK_ALLOC_NEXT: AtomicU64 = AtomicU64::new(0x_5555_5555_0000);
let start_addr = VirtAddr::new(STACK_ALLOC_NEXT.fetch_add(
size_in_pages * Page::<Size4KiB>::SIZE,
Ordering::Relaxed,
));
Page::from_start_address(start_addr)
.expect("`STACK_ALLOC_NEXT` not page aligned")
}
pub unsafe fn alloc_stack(
size_in_pages: u64,
mapper: &mut impl Mapper<Size4KiB>,
frame_allocator: &mut impl FrameAllocator<Size4KiB>,
) -> Result<StackBounds, mapper::MapToError<Size4KiB>> {
use x86_64::structures::paging::PageTableFlags as Flags;
let guard_page = reserve_stack_memory(size_in_pages + 1);
let stack_start = guard_page + 1;
let stack_end = stack_start + size_in_pages;
for page in Page::range(stack_start, stack_end) {
let frame = frame_allocator
.allocate_frame()
.ok_or(mapper::MapToError::FrameAllocationFailed)?;
let flags = Flags::PRESENT | Flags::WRITABLE;
mapper.map_to(page, frame, flags, frame_allocator)?.flush();
}
Ok(StackBounds {
start: stack_start.start_address(),
end: stack_end.start_address(),
})
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct StackBounds {
start: VirtAddr,
end: VirtAddr,
}
impl StackBounds {
pub fn new(start: VirtAddr, end: VirtAddr) -> Self {
Self { start, end }
}
pub fn start(&self) -> VirtAddr {
self.start
}
pub fn end(&self) -> VirtAddr {
self.end
}
}
@@ -1,4 +1,5 @@
use x86_64::VirtAddr;
use crate::arch::x86_64::memory::allocation::stack_alloc::StackBounds;
mod switch;
@@ -9,25 +10,7 @@ pub struct Thread {
stack_bounds: Option<StackBounds>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct StackBounds {
start: VirtAddr,
end: VirtAddr,
}
impl StackBounds {
pub fn new(start: VirtAddr, end: VirtAddr) -> Self {
Self { start, end }
}
pub fn start(&self) -> VirtAddr {
self.start
}
pub fn end(&self) -> VirtAddr {
self.end
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct ThreadId(u64);
@@ -0,0 +1,13 @@
.intel_syntax noprefix
switch_thread:
pushfq
mov rax, rsp
mov rsp, rdi
mov rdi, rax
call add_paused_thread
popfq
ret
+1 -1
View File
@@ -16,7 +16,7 @@ use crate::{
arch::x86_64::memory::{init_frame_allocator, init_page_table},
prelude::*,
};
use arch::x86_64::memory::allocation::allocator::init_heap;
use arch::x86_64::memory::allocation::heap_alloc::init_heap;
use arch::x86_64::memory::memory_map;
use core::arch::asm;
use limine::BaseRevision;