From 48dcde0c02ffb7650f9904feda7df06881e2b4c0 Mon Sep 17 00:00:00 2001 From: zxq5 Date: Tue, 4 Mar 2025 00:53:06 +0000 Subject: [PATCH] minor changes & work on threading --- docs/Planning & To-Dos.md | 15 ++++-- .../x86_64/memory/allocation/stack_alloc.rs | 4 ++ kernel/src/arch/x86_64/memory/mod.rs | 51 ++----------------- kernel/src/arch/x86_64/processing/mod.rs | 1 + .../arch/x86_64/processing/taskrunner/mod.rs | 12 +++++ kernel/src/lib.rs | 5 +- kernel/src/std/application.rs | 2 +- libm/src/lib.rs | 26 ++++++++++ 8 files changed, 62 insertions(+), 54 deletions(-) create mode 100644 kernel/src/arch/x86_64/processing/taskrunner/mod.rs diff --git a/docs/Planning & To-Dos.md b/docs/Planning & To-Dos.md index ab30a64..c3ea405 100644 --- a/docs/Planning & To-Dos.md +++ b/docs/Planning & To-Dos.md @@ -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 diff --git a/kernel/src/arch/x86_64/memory/allocation/stack_alloc.rs b/kernel/src/arch/x86_64/memory/allocation/stack_alloc.rs index 7be4742..d6a7c3b 100644 --- a/kernel/src/arch/x86_64/memory/allocation/stack_alloc.rs +++ b/kernel/src/arch/x86_64/memory/allocation/stack_alloc.rs @@ -19,6 +19,10 @@ pub unsafe fn alloc_stack( ) -> Result> { 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; diff --git a/kernel/src/arch/x86_64/memory/mod.rs b/kernel/src/arch/x86_64/memory/mod.rs index 93b9bd1..c7d2d7e 100644 --- a/kernel/src/arch/x86_64/memory/mod.rs +++ b/kernel/src/arch/x86_64/memory/mod.rs @@ -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 + 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()) } } @@ -114,36 +105,4 @@ unsafe impl FrameAllocator for FoundryOSFrameAllocator { self.next += 1; frame } -} - -// pub unsafe fn translate_addr(addr: VirtAddr, physical_memory_offset: VirtAddr) -> Option { -// translate_addr_inner(addr, physical_memory_offset) -// } - -// fn translate_addr_inner(addr: VirtAddr, physical_memory_offset: VirtAddr) -> Option { -// 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())) -// } +} \ No newline at end of file diff --git a/kernel/src/arch/x86_64/processing/mod.rs b/kernel/src/arch/x86_64/processing/mod.rs index 02b661e..01f827c 100644 --- a/kernel/src/arch/x86_64/processing/mod.rs +++ b/kernel/src/arch/x86_64/processing/mod.rs @@ -1,2 +1,3 @@ pub mod async_io; pub mod threading; +mod taskrunner; diff --git a/kernel/src/arch/x86_64/processing/taskrunner/mod.rs b/kernel/src/arch/x86_64/processing/taskrunner/mod.rs new file mode 100644 index 0000000..0198236 --- /dev/null +++ b/kernel/src/arch/x86_64/processing/taskrunner/mod.rs @@ -0,0 +1,12 @@ +use alloc::boxed::Box; + +pub struct Task { + pid: usize, + task: Box, +} + +impl Task { + pub fn new() {} + + pub fn run() {} +} \ No newline at end of file diff --git a/kernel/src/lib.rs b/kernel/src/lib.rs index f5e8e32..f9f4b7d 100644 --- a/kernel/src/lib.rs +++ b/kernel/src/lib.rs @@ -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... "); diff --git a/kernel/src/std/application.rs b/kernel/src/std/application.rs index bc5ab7e..7131253 100644 --- a/kernel/src/std/application.rs +++ b/kernel/src/std/application.rs @@ -8,7 +8,7 @@ pub mod window; pub trait Application { type Output; - async fn run(&mut self, args: Vec) -> Result; + fn run(&mut self, args: Vec) -> impl Future> + Send; } #[derive(Debug)] diff --git a/libm/src/lib.rs b/libm/src/lib.rs index 376fb0b..9458793 100644 --- a/libm/src/lib.rs +++ b/libm/src/lib.rs @@ -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, + unicode_table: [[u8; 2]; 512], +} + +struct Glyph { + bytes: Vec, +} + +enum FontBuilder { + Psf1, + Psf2, +} + +impl FontBuilder { + const PSF1_MAGIC: u16 = 0x3604; + const PSF2_MAGIC: u32 = 0x72b54a86; + + pub fn load() -> Option { + None + } +} + impl Font { const MAGIC: u16 = 0x3604;