From 218f700b222e8170a85152b4f4b857a87c5dc2b3 Mon Sep 17 00:00:00 2001 From: elfein Date: Mon, 22 Nov 2021 06:58:35 -0800 Subject: [PATCH] Paging works :3 --- .cargo/config.toml | 2 +- ableos/.cargo/config.toml | 2 +- ableos/src/arch/x86_64/memory.rs | 31 +++++++++++++++++++++++++++++++ ableos/src/kmain.rs | 8 +++++--- ableos/src/lib.rs | 1 + 5 files changed, 39 insertions(+), 5 deletions(-) diff --git a/.cargo/config.toml b/.cargo/config.toml index 22b6ca6..6cea5ca 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,2 +1,2 @@ [alias] -repbuild = "run --manifest-path ./repbuild/Cargo.toml --" +repbuild = "run --manifest-path ./repbuild/Cargo.toml --" \ No newline at end of file diff --git a/ableos/.cargo/config.toml b/ableos/.cargo/config.toml index 75f0784..58aed6d 100644 --- a/ableos/.cargo/config.toml +++ b/ableos/.cargo/config.toml @@ -3,7 +3,7 @@ target = "./json_targets/x86_64-ableos.json" [unstable] build-std-features = ["compiler-builtins-mem"] -build-std = ["core", "compiler_builtins"] +build-std = ["core", "compiler_builtins", "alloc"] [target.'cfg(target_arch = "x86_64")'] diff --git a/ableos/src/arch/x86_64/memory.rs b/ableos/src/arch/x86_64/memory.rs index 28f0495..9c42a18 100644 --- a/ableos/src/arch/x86_64/memory.rs +++ b/ableos/src/arch/x86_64/memory.rs @@ -1,3 +1,4 @@ +use bootloader::bootinfo::{MemoryMap, MemoryRegionType}; use x86_64::{ structures::paging::{ FrameAllocator, Mapper, OffsetPageTable, Page, PageTable, PhysFrame, Size4KiB, @@ -79,3 +80,33 @@ unsafe impl FrameAllocator for EmptyFrameAllocator { None } } + +pub struct BootInfoFrameAllocator { + memory_map: &'static MemoryMap, + next: usize, +} + +impl BootInfoFrameAllocator { + pub unsafe fn init(memory_map: &'static MemoryMap) -> Self { + Self { + memory_map, + next: 0, + } + } + + fn usable_frames(&self) -> impl Iterator { + let regions = self.memory_map.iter(); + let usable_regions = regions.filter(|r| r.region_type == MemoryRegionType::Usable); + let addr_range = usable_regions.map(|r| r.range.start_addr()..r.range.end_addr()); + let frame_address = addr_range.flat_map(|r| r.step_by(4096)); + frame_address.map(|addr| PhysFrame::containing_address(PhysAddr::new(addr))) + } +} + +unsafe impl FrameAllocator for BootInfoFrameAllocator { + fn allocate_frame(&mut self) -> Option> { + let frame = self.usable_frames().nth(self.next); + self.next += 1; + frame + } +} diff --git a/ableos/src/kmain.rs b/ableos/src/kmain.rs index 0dd8eeb..ebe1db6 100644 --- a/ableos/src/kmain.rs +++ b/ableos/src/kmain.rs @@ -57,13 +57,15 @@ pub fn kernel_main(boot_info: &'static BootInfo) -> ! { let phys_mem_offset = VirtAddr::new(boot_info.physical_memory_offset); let mut mapper = unsafe { memory::init(phys_mem_offset) }; - let mut frame_allocator = memory::EmptyFrameAllocator; + let mut frame_allocator = unsafe { + memory::BootInfoFrameAllocator::init(&boot_info.memory_map) + }; - let page = Page::containing_address(VirtAddr::new(0)); + let page = Page::containing_address(VirtAddr::new(0xdeadbeaf000)); memory::create_example_mapping(page, &mut mapper, &mut frame_allocator); let page_ptr: *mut u64 = page.start_address().as_mut_ptr(); - unsafe { page_ptr.offset(400).write_volatile(0xf021_f077_f065_f04e) }; + unsafe { page_ptr.offset(400).write_volatile(0xf021_f077_f065_804e) }; // stack_overflow(); // crate::arch::shutdown(); diff --git a/ableos/src/lib.rs b/ableos/src/lib.rs index 6ca5156..2b08f55 100644 --- a/ableos/src/lib.rs +++ b/ableos/src/lib.rs @@ -45,3 +45,4 @@ pub const RELEASE_TYPE: &str = "debug"; #[cfg(not(debug_assertions))] /// A constant to check if the kernel is in release mode pub const RELEASE_TYPE: &str = "release"; +extern crate alloc;