From abaf770dd191227f043155edfa867faa43b5fb18 Mon Sep 17 00:00:00 2001 From: Erin Date: Tue, 31 Oct 2017 22:11:51 -0500 Subject: [PATCH] x86::memory: return a MemoryController from init(), wire up stackalloc --- src/arch/x86_64/memory/mod.rs | 35 +++++++++++++++++++++++++++++++++-- src/lib.rs | 2 +- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/arch/x86_64/memory/mod.rs b/src/arch/x86_64/memory/mod.rs index d761900..c58b178 100644 --- a/src/arch/x86_64/memory/mod.rs +++ b/src/arch/x86_64/memory/mod.rs @@ -11,7 +11,8 @@ use arch::x86_64; use ::alloca; pub use self::area_frame_allocator::AreaFrameAllocator; -use self::paging::Page; +use self::paging::{Page, ActivePageTable}; + pub use self::stack_allocator::{Stack, StackAllocator}; /// The physical size of each frame. @@ -82,7 +83,23 @@ pub trait FrameAllocator { } -pub fn init(boot_info: &BootInformation) { +pub struct MemoryController { + active_table: ActivePageTable, + frame_allocator: AreaFrameAllocator, + stack_allocator: StackAllocator, +} + +impl MemoryController { + /// Allocates and returns a stack. + /// + /// Note: `size` is given in pages. + pub fn alloc_stack(&mut self, size: usize) -> Option { + self.stack_allocator.alloc_stack(&mut self.active_table, &mut self.frame_allocator, size) + } +} + + +pub fn init(boot_info: &BootInformation) -> MemoryController { assert_first_call!("memory::init() can only be called once!"); let memory_map_tag = boot_info.memory_map_tag() @@ -130,4 +147,18 @@ pub fn init(boot_info: &BootInformation) { alloca::heap_init(HEAP_START, HEAP_SIZE); } info!("kheap: initialized"); + + let stack_allocator = { + let alloc_start = heap_end_page + 1; + let alloc_end = alloc_start + 100; + let alloc_range = Page::range_inclusive(alloc_start, alloc_end); + + StackAllocator::new(alloc_range) + }; + + MemoryController { + active_table: active_table, + frame_allocator: frame_allocator, + stack_allocator: stack_allocator, + } } diff --git a/src/lib.rs b/src/lib.rs index 3e75c7a..7b6fca7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -58,7 +58,7 @@ pub extern fn kernel_main(multiboot_info_pointer: usize) { let boot_info = unsafe {multiboot2::load(multiboot_info_pointer)}; - memory::init(boot_info); + let mut mem_ctrl = memory::init(boot_info); info!("* memory::init(): success! *"); info!("kheap: smoke test (boxing): {:?}", Box::new("hello world"));