Init allocator to make allocations work

This commit is contained in:
Philipp Oppermann
2015-10-10 00:45:45 +02:00
parent d790fc20e5
commit a4835b6778
4 changed files with 33 additions and 9 deletions

View File

@@ -15,6 +15,7 @@
#![feature(no_std, lang_items, asm)] #![feature(no_std, lang_items, asm)]
#![feature(core_str_ext, const_fn, range_inclusive)] #![feature(core_str_ext, const_fn, range_inclusive)]
#![feature(unique, core_intrinsics, alloc)] #![feature(unique, core_intrinsics, alloc)]
#![feature(box_syntax)]
#![no_std] #![no_std]
extern crate rlibc; extern crate rlibc;
@@ -54,6 +55,7 @@ pub extern fn rust_main(multiboot_address: usize) {
print!("line {}", 2); print!("line {}", 2);
Box::new(42); Box::new(42);
box [42; 25000000];
loop{} loop{}
} }

View File

@@ -4,18 +4,20 @@ use core::iter::range_inclusive;
use rlibc::memcpy; use rlibc::memcpy;
use spin::Mutex; use spin::Mutex;
static ALLOCATOR: Mutex<Option<Allocator<'static, DynamicFrameStack>>> = Mutex::new(None); static ALLOCATOR: Mutex<Option<Allocator>> = Mutex::new(None);
const HEAD_BOTTOM: usize = 0o_001_000_000_000_0000; const HEAD_BOTTOM: usize = 0o_001_000_000_000_0000;
struct Allocator<'a, T> where T: 'a { struct Allocator {
heap_top: usize, heap_top: usize,
last_mapped_page: Page, last_mapped_page: Page,
mapper: Mapper<'a, T>, lock: paging::Lock,
frame_stack: DynamicFrameStack,
} }
impl<'a, T> Allocator<'a, T> where T: FrameAllocator { impl Allocator {
pub fn allocate(&mut self, size: usize, align: usize) -> *mut u8 { pub fn allocate(&mut self, size: usize, align: usize) -> *mut u8 {
println!("allocate {} bytes (align {})", size, align); //loop{}
let start_address = align_up(self.heap_top, align); let start_address = align_up(self.heap_top, align);
let end_address = start_address + size; let end_address = start_address + size;
let end_page = Page::containing_address(end_address - 1).number; let end_page = Page::containing_address(end_address - 1).number;
@@ -23,7 +25,8 @@ impl<'a, T> Allocator<'a, T> where T: FrameAllocator {
if end_page > last_mapped_page { if end_page > last_mapped_page {
for page in range_inclusive(last_mapped_page + 1, end_page).map(|n| Page{number: n}) { for page in range_inclusive(last_mapped_page + 1, end_page).map(|n| Page{number: n}) {
self.mapper.map(page, true, false) let mut mapper = self.lock.mapper(&mut self.frame_stack);
mapper.map(page, true, false)
} }
self.last_mapped_page.number = end_page; self.last_mapped_page.number = end_page;
} }
@@ -52,6 +55,21 @@ fn align_up(addr: usize, align: usize) -> usize {
} }
} }
pub fn init(mut lock: paging::Lock, mut frame_stack: DynamicFrameStack) {
let last_mapped_page = Page::containing_address(HEAD_BOTTOM);
{
let mut mapper = lock.mapper(&mut frame_stack);
mapper.map(last_mapped_page, true, false);
}
*ALLOCATOR.lock() = Some(Allocator {
heap_top: HEAD_BOTTOM,
last_mapped_page: last_mapped_page,
lock: lock,
frame_stack: frame_stack,
})
}
#[no_mangle] #[no_mangle]
pub extern fn __rust_allocate(size: usize, align: usize) -> *mut u8 { pub extern fn __rust_allocate(size: usize, align: usize) -> *mut u8 {
ALLOCATOR.lock().as_mut().expect("no allocator").allocate(size, align) ALLOCATOR.lock().as_mut().expect("no allocator").allocate(size, align)

View File

@@ -1,5 +1,6 @@
use multiboot2::Multiboot; use multiboot2::Multiboot;
use self::paging::Page; use self::paging::Page;
use self::frame_allocator::{FrameAllocator, DynamicFrameStack};
mod alloc; mod alloc;
mod paging; mod paging;
@@ -26,12 +27,12 @@ pub fn init(multiboot: &Multiboot) {
identity_map_kernel_sections(multiboot, lock.mapper(&mut bump_pointer)); identity_map_kernel_sections(multiboot, lock.mapper(&mut bump_pointer));
lock.activate_current_table(); lock.activate_current_table();
init_core_map(multiboot, &mut lock, bump_pointer); let frame_stack = init_core_map(multiboot, &mut lock, bump_pointer);
let maximal_memory = multiboot.memory_area_tag().unwrap().areas().map( let maximal_memory = multiboot.memory_area_tag().unwrap().areas().map(
|area| area.base_addr + area.length).max().unwrap(); |area| area.base_addr + area.length).max().unwrap();
println!("maximal_memory: 0x{:x}", maximal_memory); println!("maximal_memory: 0x{:x}", maximal_memory);
alloc::init(lock, frame_stack);
} }
@@ -72,9 +73,10 @@ fn identity_map_kernel_sections<T>(multiboot: &Multiboot, mut mapper: paging::Ma
} }
} }
fn init_core_map(multiboot: &Multiboot, lock: &mut paging::Lock, mut bump_pointer: BumpPointer) { fn init_core_map(multiboot: &Multiboot, lock: &mut paging::Lock,
mut bump_pointer: BumpPointer) -> DynamicFrameStack
{
use core::iter::range_inclusive; use core::iter::range_inclusive;
use self::frame_allocator::{FrameAllocator, DynamicFrameStack};
const CORE_MAP_PAGE: Page = Page{number: 0o_001_000_000}; const CORE_MAP_PAGE: Page = Page{number: 0o_001_000_000};
@@ -95,6 +97,7 @@ fn init_core_map(multiboot: &Multiboot, lock: &mut paging::Lock, mut bump_pointe
} }
} }
} }
frame_stack
} }
#[derive(Debug)] #[derive(Debug)]

View File

@@ -70,6 +70,7 @@ pub fn unmap<A>(lock: &mut Lock, page: Page, allocator: &mut A) where A: FrameAl
/// A mapped or unmapped page /// A mapped or unmapped page
#[derive(Clone, Copy)]
pub struct Page { pub struct Page {
pub number: usize, // TOOD make private pub number: usize, // TOOD make private
} }