From 26c86f8242031e44365ebe67260fca7b88442b92 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Wed, 17 Aug 2016 19:06:33 -0600 Subject: [PATCH] Set page table of AP to BSP, to allow mapping of heap to be shared --- arch/x86_64/src/paging/mod.rs | 31 +++++++++++++++++++++++++++++++ arch/x86_64/src/start.rs | 14 ++++++++------ 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/arch/x86_64/src/paging/mod.rs b/arch/x86_64/src/paging/mod.rs index a616db3..d57c0d6 100644 --- a/arch/x86_64/src/paging/mod.rs +++ b/arch/x86_64/src/paging/mod.rs @@ -81,6 +81,37 @@ pub unsafe fn init(stack_start: usize, stack_end: usize) -> ActivePageTable { active_table } +/// Initialize paging for AP +pub unsafe fn init_ap(stack_start: usize, stack_end: usize, bsp_page_table: usize) -> ActivePageTable { + let mut active_table = ActivePageTable::new(); + + let mut temporary_page = TemporaryPage::new(Page::containing_address(VirtualAddress::new(0x80000000))); + + let mut new_table = { + let frame = Frame::containing_address(PhysicalAddress::new(bsp_page_table)); + InactivePageTable { + p4_frame: frame + } + }; + + active_table.with(&mut new_table, &mut temporary_page, |mapper| { + let mut remap = |start: usize, end: usize, flags: EntryFlags| { + let start_frame = Frame::containing_address(PhysicalAddress::new(start)); + let end_frame = Frame::containing_address(PhysicalAddress::new(end - 1)); + for frame in Frame::range_inclusive(start_frame, end_frame) { + mapper.identity_map(frame, flags); + } + }; + + // Remap stack writable, no execute + remap(stack_start, stack_end, PRESENT | WRITABLE | NO_EXECUTE); + }); + + active_table.switch(new_table); + + active_table +} + pub struct ActivePageTable { mapper: Mapper, } diff --git a/arch/x86_64/src/start.rs b/arch/x86_64/src/start.rs index 6ab23a7..3526136 100644 --- a/arch/x86_64/src/start.rs +++ b/arch/x86_64/src/start.rs @@ -3,7 +3,8 @@ /// It must create the IDT with the correct entries, those entries are /// defined in other files inside of the `arch` module -use core::sync::atomic::{AtomicBool, ATOMIC_BOOL_INIT, Ordering}; +use core::sync::atomic::{AtomicBool, ATOMIC_BOOL_INIT, AtomicUsize, ATOMIC_USIZE_INIT, Ordering}; +use x86::controlregs; use acpi; use allocator::{HEAP_START, HEAP_SIZE}; @@ -19,6 +20,7 @@ static BSS_TEST_ZERO: usize = 0; static BSS_TEST_NONZERO: usize = 0xFFFFFFFFFFFFFFFF; static BSP_READY: AtomicBool = ATOMIC_BOOL_INIT; +static BSP_PAGE_TABLE: AtomicUsize = ATOMIC_USIZE_INIT; extern { /// Kernel main function @@ -68,7 +70,9 @@ pub unsafe extern fn kstart() -> ! { // Initialize paging let mut active_table = paging::init(stack_start, stack_end); - // Read ACPI tables + BSP_PAGE_TABLE.store(controlregs::cr3() as usize, Ordering::SeqCst); + + // Read ACPI tables, starts APs acpi::init(&mut active_table); // Map heap @@ -95,10 +99,8 @@ pub unsafe extern fn kstart_ap(stack_start: usize, stack_end: usize) -> ! { idt::init_ap(); // Initialize paging - //let mut active_table = - paging::init(stack_start, stack_end); - - // TODO: Use heap from master, ensuring consistency + //let mut active_table = + paging::init_ap(stack_start, stack_end, BSP_PAGE_TABLE.load(Ordering::SeqCst)); } while ! BSP_READY.load(Ordering::SeqCst) {