Update post and code

This commit is contained in:
Philipp Oppermann
2016-12-20 16:50:10 +01:00
parent 038fd097b6
commit ef786e1fe8
7 changed files with 360 additions and 64 deletions

View File

@@ -8,7 +8,7 @@
// except according to those terms.
use spin::Once;
use memory::StackPointer;
use memory::MemoryController;
mod idt;
mod tss;
@@ -95,7 +95,10 @@ static IDT: Once<idt::Idt> = Once::new();
static TSS: Once<tss::TaskStateSegment> = Once::new();
static GDT: Once<gdt::Gdt> = Once::new();
pub fn init(double_fault_stack: StackPointer) {
pub fn init(memory_controller: &mut MemoryController) {
let double_fault_stack = memory_controller.alloc_stack(1)
.expect("could not allocate double fault stack");
let mut double_fault_ist_index = 0;
let tss = TSS.call_once(|| {

View File

@@ -1,4 +1,4 @@
use memory::StackPointer;
use memory::{Stack, StackPointer};
#[derive(Debug)]
#[repr(C, packed)]
@@ -16,7 +16,7 @@ impl TaskStateSegment {
pub fn new() -> TaskStateSegment {
TaskStateSegment {
privilege_stacks: PrivilegeStackTable([None, None, None]),
interrupt_stacks: InterruptStackTable::new(),
interrupt_stacks: InterruptStackTable([None, None, None, None, None, None, None]),
iomap_base: 0,
reserved_0: 0,
reserved_1: 0,
@@ -33,18 +33,14 @@ pub struct PrivilegeStackTable([Option<StackPointer>; 3]);
pub struct InterruptStackTable([Option<StackPointer>; 7]);
impl InterruptStackTable {
pub fn new() -> InterruptStackTable {
InterruptStackTable([None, None, None, None, None, None, None])
}
pub fn insert_stack(&mut self, stack_pointer: StackPointer) -> Result<u8, StackPointer> {
pub fn insert_stack(&mut self, stack: Stack) -> Result<u8, Stack> {
// TSS index starts at 1
for (entry, i) in self.0.iter_mut().zip(1..) {
if entry.is_none() {
*entry = Some(stack_pointer);
*entry = Some(stack.top());
return Ok(i);
}
}
Err(stack_pointer)
Err(stack)
}
}

View File

@@ -55,11 +55,8 @@ pub extern "C" fn rust_main(multiboot_information_address: usize) {
// set up guard page and map the heap pages
let mut memory_controller = memory::init(boot_info);
// initialize our IDT
let double_fault_stack = memory_controller.alloc_stack(1)
.expect("could not allocate double fault stack");
interrupts::init(double_fault_stack);
interrupts::init(&mut memory_controller);
unsafe { int!(3) };

View File

@@ -9,7 +9,7 @@
pub use self::area_frame_allocator::AreaFrameAllocator;
pub use self::paging::remap_the_kernel;
pub use self::stack_allocator::{StackAllocator, StackPointer};
pub use self::stack_allocator::{Stack, StackPointer};
use self::paging::PhysicalAddress;
use multiboot2::BootInformation;
@@ -62,11 +62,10 @@ pub fn init(boot_info: &BootInformation) -> MemoryController {
}
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)
let stack_alloc_start = heap_end_page + 1;
let stack_alloc_end = stack_alloc_start + 100;
let stack_alloc_range = Page::range_inclusive(stack_alloc_start, stack_alloc_end);
stack_allocator::new_stack_allocator(stack_alloc_range)
};
MemoryController {
@@ -79,11 +78,11 @@ pub fn init(boot_info: &BootInformation) -> MemoryController {
pub struct MemoryController {
active_table: paging::ActivePageTable,
frame_allocator: AreaFrameAllocator,
stack_allocator: StackAllocator,
stack_allocator: stack_allocator::StackAllocator,
}
impl MemoryController {
pub fn alloc_stack(&mut self, size_in_pages: usize) -> Result<StackPointer, ()> {
pub fn alloc_stack(&mut self, size_in_pages: usize) -> Option<Stack> {
let &mut MemoryController { ref mut active_table,
ref mut frame_allocator,
ref mut stack_allocator } = self;

View File

@@ -70,6 +70,7 @@ impl Add<usize> for Page {
}
}
#[derive(Debug, Clone)]
pub struct PageIter {
start: Page,
end: Page,

View File

@@ -15,37 +15,73 @@ impl StackAllocator {
active_table: &mut ActivePageTable,
frame_allocator: &mut FA,
size_in_pages: usize)
-> Result<StackPointer, ()> {
-> Option<Stack> {
if size_in_pages == 0 {
return Err(());
return None;
}
let _guard_page = self.range.next().ok_or(())?;
let mut range = self.range.clone();
let stack_start = self.range.next().ok_or(())?;
// try to allocate the stack pages and a guard page
let guard_page = range.next();
let stack_start = range.next();
let stack_end = if size_in_pages == 1 {
stack_start
} else {
self.range.nth(size_in_pages - 1).ok_or(())?
range.nth(size_in_pages - 2)
};
for page in Page::range_inclusive(stack_start, stack_end) {
active_table.map(page, paging::WRITABLE, frame_allocator);
}
match (guard_page, stack_start, stack_end) {
(Some(_), Some(start), Some(end)) => {
// success! write back updated range
self.range = range;
let top_of_stack = stack_end.start_address() + PAGE_SIZE;
StackPointer::new(top_of_stack).ok_or(())
// map stack pages to physical frames
for page in Page::range_inclusive(start, end) {
active_table.map(page, paging::WRITABLE, frame_allocator);
}
// create a new stack
let top_of_stack = end.start_address() + PAGE_SIZE;
Some(Stack::new(top_of_stack, start.start_address()))
}
_ => None, /* not enough pages */
}
}
}
#[derive(Debug)]
pub struct Stack {
top: StackPointer,
bottom: StackPointer,
}
impl Stack {
fn new(top: usize, bottom: usize) -> Stack {
assert!(top > bottom);
Stack {
top: StackPointer::new(top),
bottom: StackPointer::new(bottom),
}
}
pub fn top(&self) -> StackPointer {
self.top
}
}
#[derive(Debug, Clone, Copy)]
pub struct StackPointer(NonZero<usize>);
impl StackPointer {
fn new(ptr: usize) -> Option<StackPointer> {
match ptr {
0 => None,
ptr => Some(StackPointer(unsafe { NonZero::new(ptr) })),
}
fn new(ptr: usize) -> StackPointer {
assert!(ptr != 0);
StackPointer(unsafe { NonZero::new(ptr) })
}
}
impl Into<usize> for StackPointer {
fn into(self) -> usize {
*self.0
}
}