Create TSS and GDT modules and use a double fault stack

This commit is contained in:
Philipp Oppermann
2016-11-28 10:13:27 +01:00
parent b4bc47d5d9
commit 5f8de6e871
8 changed files with 320 additions and 17 deletions

View File

@@ -9,15 +9,17 @@
pub use self::area_frame_allocator::AreaFrameAllocator;
pub use self::paging::remap_the_kernel;
pub use self::stack_allocator::{StackAllocator, StackPointer};
use self::paging::PhysicalAddress;
use multiboot2::BootInformation;
mod area_frame_allocator;
mod paging;
mod stack_allocator;
pub const PAGE_SIZE: usize = 4096;
pub fn init(boot_info: &BootInformation) {
pub fn init(boot_info: &BootInformation) -> MemoryController {
assert_has_not_been_called!("memory::init must be called only once");
let memory_map_tag = boot_info.memory_map_tag().expect("Memory map tag required");
@@ -58,6 +60,35 @@ pub fn init(boot_info: &BootInformation) {
for page in Page::range_inclusive(heap_start_page, heap_end_page) {
active_table.map(page, paging::WRITABLE, &mut frame_allocator);
}
let stack_allocator = {
let stack_alloc_start_page = heap_end_page + 1;
let stack_alloc_end_page = stack_alloc_start_page + 100;
let stack_alloc_page_range = Page::range_inclusive(stack_alloc_start_page,
stack_alloc_end_page);
stack_allocator::new_stack_allocator(stack_alloc_page_range)
};
MemoryController {
active_table: active_table,
frame_allocator: frame_allocator,
stack_allocator: stack_allocator,
}
}
pub struct MemoryController {
active_table: paging::ActivePageTable,
frame_allocator: AreaFrameAllocator,
stack_allocator: StackAllocator,
}
impl MemoryController {
pub fn alloc_stack(&mut self, size_in_pages: usize) -> Result<StackPointer, ()> {
let &mut MemoryController { ref mut active_table,
ref mut frame_allocator,
ref mut stack_allocator } = self;
stack_allocator.alloc_stack(active_table, frame_allocator, size_in_pages)
}
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]

View File

@@ -11,7 +11,7 @@ pub use self::entry::*;
use memory::{PAGE_SIZE, Frame, FrameAllocator};
use self::temporary_page::TemporaryPage;
pub use self::mapper::Mapper;
use core::ops::{Deref, DerefMut};
use core::ops::{Deref, DerefMut, Add};
use multiboot2::BootInformation;
mod entry;
@@ -37,7 +37,7 @@ impl Page {
Page { number: address / PAGE_SIZE }
}
fn start_address(&self) -> usize {
pub fn start_address(&self) -> usize {
self.number * PAGE_SIZE
}
@@ -62,6 +62,14 @@ impl Page {
}
}
impl Add<usize> for Page {
type Output = Page;
fn add(self, rhs: usize) -> Page {
Page { number: self.number + rhs }
}
}
pub struct PageIter {
start: Page,
end: Page,

View File

@@ -0,0 +1,51 @@
use memory::paging::{self, Page, PageIter, ActivePageTable};
use memory::{PAGE_SIZE, FrameAllocator};
use core::nonzero::NonZero;
pub fn new_stack_allocator(page_range: PageIter) -> StackAllocator {
StackAllocator { range: page_range }
}
pub struct StackAllocator {
range: PageIter,
}
impl StackAllocator {
pub fn alloc_stack<FA: FrameAllocator>(&mut self,
active_table: &mut ActivePageTable,
frame_allocator: &mut FA,
size_in_pages: usize)
-> Result<StackPointer, ()> {
if size_in_pages == 0 {
return Err(());
}
let _guard_page = self.range.next().ok_or(())?;
let stack_start = self.range.next().ok_or(())?;
let stack_end = if size_in_pages == 1 {
stack_start
} else {
self.range.nth(size_in_pages - 1).ok_or(())?
};
for page in Page::range_inclusive(stack_start, stack_end) {
active_table.map(page, paging::WRITABLE, frame_allocator);
}
let top_of_stack = stack_end.start_address() + PAGE_SIZE;
StackPointer::new(top_of_stack).ok_or(())
}
}
#[derive(Debug)]
pub struct StackPointer(NonZero<usize>);
impl StackPointer {
fn new(ptr: usize) -> Option<StackPointer> {
match ptr {
0 => None,
ptr => Some(StackPointer(unsafe { NonZero::new(ptr) })),
}
}
}