minor changes & work on threading

This commit is contained in:
2025-03-04 00:53:06 +00:00
parent 0a5269eeb0
commit 48dcde0c02
8 changed files with 62 additions and 54 deletions
+10 -5
View File
@@ -7,12 +7,17 @@
> - [ ] write a script to run tests
> - [ ] a release should only be published if all tests pass
> [!todo] Memory
> - [ ] make sure page tables are correct. we're still getting the annoying page fault errors for the APIC
> - [ ] page allocator
> - [ ] e
> [!todo] Allocator
> - [ ] learn about several allocator designs and decide on the optimal one
> - [ ] implementation
> - [ ] implement an allocate method
> - [ ] implement a deallocate method
> - [ ] set the global allocator
> - [x] learn about several allocator designs and decide on the optimal one
> - [x] implementation
> - [x] implement an allocate method
> - [x] implement a deallocate method
> - [x] set the global allocator
> - [ ] testing
> - [ ] write unit tests
> - [ ] many allocations
@@ -19,6 +19,10 @@ pub unsafe fn alloc_stack(
) -> Result<StackBounds, mapper::MapToError<Size4KiB>> { unsafe {
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;
+4 -45
View File
@@ -58,24 +58,18 @@ pub struct FoundryOSFrameAllocator {
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 {
pub unsafe fn init(memory_map: &'static MemoryMapResponse) { unsafe {
FRAME_ALLOCATOR.call_once(|| Mutex::new(Self {
memory_map,
next: 0,
}
}
}));
}}
pub fn count_usable_frames(&self) -> u32 {
self.usable_frames().count() as u32
@@ -89,11 +83,8 @@ impl FoundryOSFrameAllocator {
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())
}
}
@@ -115,35 +106,3 @@ unsafe impl FrameAllocator<Size4KiB> for FoundryOSFrameAllocator {
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()))
// }
+1
View File
@@ -1,2 +1,3 @@
pub mod async_io;
pub mod threading;
mod taskrunner;
@@ -0,0 +1,12 @@
use alloc::boxed::Box;
pub struct Task {
pid: usize,
task: Box<dyn Fn() + Send + 'static>,
}
impl Task {
pub fn new() {}
pub fn run() {}
}
+3 -2
View File
@@ -13,7 +13,7 @@
extern crate alloc;
use crate::{
arch::x86_64::memory::{init_frame_allocator, init_page_table},
arch::x86_64::memory::init_page_table,
prelude::*,
};
use arch::x86_64::memory::allocation::heap_alloc::init_heap;
@@ -21,6 +21,7 @@ use arch::x86_64::memory::memory_map;
use core::arch::asm;
use limine::BaseRevision;
use x86_64::VirtAddr;
use crate::arch::x86_64::memory::FoundryOSFrameAllocator;
pub mod arch;
pub mod resources;
@@ -89,7 +90,7 @@ pub fn boot() -> Result<(), &'static str> {
println_log!("[Success]");
print_log!(" Setting Up Page Table... ");
init_frame_allocator(memory_map);
unsafe { FoundryOSFrameAllocator::init(memory_map) };
println_log!("[Success]");
print_log!(" Initialising Heap... ");
+1 -1
View File
@@ -8,7 +8,7 @@ pub mod window;
pub trait Application {
type Output;
async fn run(&mut self, args: Vec<String>) -> Result<Self::Output, Error>;
fn run(&mut self, args: Vec<String>) -> impl Future<Output = Result<Self::Output, Error>> + Send;
}
#[derive(Debug)]
+26
View File
@@ -47,6 +47,32 @@ pub fn include_font(item: TokenStream) -> TokenStream {
struct Font([[u8; 16]; 512]);
struct FontData {
width: u8,
height: u8,
length: u16,
data: Vec<Glyph>,
unicode_table: [[u8; 2]; 512],
}
struct Glyph {
bytes: Vec<u8>,
}
enum FontBuilder {
Psf1,
Psf2,
}
impl FontBuilder {
const PSF1_MAGIC: u16 = 0x3604;
const PSF2_MAGIC: u32 = 0x72b54a86;
pub fn load() -> Option<Self> {
None
}
}
impl Font {
const MAGIC: u16 = 0x3604;