mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-16 22:37:49 +00:00
Init allocator to make allocations work
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
#![feature(no_std, lang_items, asm)]
|
||||
#![feature(core_str_ext, const_fn, range_inclusive)]
|
||||
#![feature(unique, core_intrinsics, alloc)]
|
||||
#![feature(box_syntax)]
|
||||
#![no_std]
|
||||
|
||||
extern crate rlibc;
|
||||
@@ -54,6 +55,7 @@ pub extern fn rust_main(multiboot_address: usize) {
|
||||
print!("line {}", 2);
|
||||
|
||||
Box::new(42);
|
||||
box [42; 25000000];
|
||||
|
||||
loop{}
|
||||
}
|
||||
|
||||
@@ -4,18 +4,20 @@ use core::iter::range_inclusive;
|
||||
use rlibc::memcpy;
|
||||
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;
|
||||
|
||||
struct Allocator<'a, T> where T: 'a {
|
||||
struct Allocator {
|
||||
heap_top: usize,
|
||||
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 {
|
||||
println!("allocate {} bytes (align {})", size, align); //loop{}
|
||||
let start_address = align_up(self.heap_top, align);
|
||||
let end_address = start_address + size;
|
||||
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 {
|
||||
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;
|
||||
}
|
||||
@@ -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]
|
||||
pub extern fn __rust_allocate(size: usize, align: usize) -> *mut u8 {
|
||||
ALLOCATOR.lock().as_mut().expect("no allocator").allocate(size, align)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use multiboot2::Multiboot;
|
||||
use self::paging::Page;
|
||||
use self::frame_allocator::{FrameAllocator, DynamicFrameStack};
|
||||
|
||||
mod alloc;
|
||||
mod paging;
|
||||
@@ -26,12 +27,12 @@ pub fn init(multiboot: &Multiboot) {
|
||||
identity_map_kernel_sections(multiboot, lock.mapper(&mut bump_pointer));
|
||||
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(
|
||||
|area| area.base_addr + area.length).max().unwrap();
|
||||
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 self::frame_allocator::{FrameAllocator, DynamicFrameStack};
|
||||
|
||||
|
||||
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)]
|
||||
|
||||
@@ -70,6 +70,7 @@ pub fn unmap<A>(lock: &mut Lock, page: Page, allocator: &mut A) where A: FrameAl
|
||||
|
||||
|
||||
/// A mapped or unmapped page
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct Page {
|
||||
pub number: usize, // TOOD make private
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user