From 70a2faa0c7a7a524f61a66f7d89dca53f59c8525 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Mon, 19 Sep 2016 08:46:11 -0600 Subject: [PATCH] Correctly position stack in higher half --- arch/x86_64/src/acpi/mod.rs | 4 ++-- arch/x86_64/src/paging/mod.rs | 14 +++++++------- arch/x86_64/src/start.rs | 6 +++--- bootloader/x86_64/startup-x86_64.asm | 3 ++- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/arch/x86_64/src/acpi/mod.rs b/arch/x86_64/src/acpi/mod.rs index eccd165..f44bd07 100644 --- a/arch/x86_64/src/acpi/mod.rs +++ b/arch/x86_64/src/acpi/mod.rs @@ -59,11 +59,11 @@ pub fn init_sdt(sdt: &'static Sdt, active_table: &mut ActivePageTable) { if ap_local_apic.flags & 1 == 1 { // Allocate a stack // TODO: Allocate contiguous - let stack_start = allocate_frame().expect("no more frames in acpi stack_start").start_address().get(); + let stack_start = allocate_frame().expect("no more frames in acpi stack_start").start_address().get() + ::KERNEL_OFFSET; for _i in 0..62 { allocate_frame().expect("no more frames in acpi stack"); } - let stack_end = allocate_frame().expect("no more frames in acpi stack_end").start_address().get() + 4096; + let stack_end = allocate_frame().expect("no more frames in acpi stack_end").start_address().get() + 4096 + ::KERNEL_OFFSET; let ap_ready = TRAMPOLINE as *mut u64; let ap_cpu_id = unsafe { ap_ready.offset(1) }; diff --git a/arch/x86_64/src/paging/mod.rs b/arch/x86_64/src/paging/mod.rs index 4c3da06..249b467 100644 --- a/arch/x86_64/src/paging/mod.rs +++ b/arch/x86_64/src/paging/mod.rs @@ -133,23 +133,23 @@ pub unsafe fn init(cpu_id: usize, stack_start: usize, stack_end: usize) -> (Acti } } - let mut remap = |start: usize, end: usize, flags: EntryFlags, offset: usize| { + let mut remap = |start: usize, end: usize, flags: EntryFlags| { if end > start { 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) { - let page = Page::containing_address(VirtualAddress::new(frame.start_address().get() + offset)); + let page = Page::containing_address(VirtualAddress::new(frame.start_address().get() + ::KERNEL_OFFSET)); mapper.map_to(page, frame, flags); } } }; // Remap stack writable, no execute - remap(stack_start, stack_end, PRESENT | NO_EXECUTE | WRITABLE, 0); + remap(stack_start - ::KERNEL_OFFSET, stack_end - ::KERNEL_OFFSET, PRESENT | NO_EXECUTE | WRITABLE); // Remap a section with `flags` let mut remap_section = |start: &u8, end: &u8, flags: EntryFlags| { - remap(start as *const _ as usize - ::KERNEL_OFFSET, end as *const _ as usize - ::KERNEL_OFFSET, flags, ::KERNEL_OFFSET); + remap(start as *const _ as usize - ::KERNEL_OFFSET, end as *const _ as usize - ::KERNEL_OFFSET, flags); }; // Remap text read-only remap_section(& __text_start, & __text_end, PRESENT); @@ -211,19 +211,19 @@ pub unsafe fn init_ap(cpu_id: usize, stack_start: usize, stack_end: usize, kerne } } - let mut remap = |start: usize, end: usize, flags: EntryFlags, offset: usize| { + let mut remap = |start: usize, end: usize, flags: EntryFlags| { if end > start { 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) { - let page = Page::containing_address(VirtualAddress::new(frame.start_address().get() + offset)); + let page = Page::containing_address(VirtualAddress::new(frame.start_address().get() + ::KERNEL_OFFSET)); mapper.map_to(page, frame, flags); } } }; // Remap stack writable, no execute - remap(stack_start, stack_end, PRESENT | NO_EXECUTE | WRITABLE, 0); + remap(stack_start - ::KERNEL_OFFSET, stack_end - ::KERNEL_OFFSET, PRESENT | NO_EXECUTE | WRITABLE); }); active_table.switch(new_table); diff --git a/arch/x86_64/src/start.rs b/arch/x86_64/src/start.rs index c941a88..8774064 100644 --- a/arch/x86_64/src/start.rs +++ b/arch/x86_64/src/start.rs @@ -68,8 +68,8 @@ pub unsafe extern fn kstart() -> ! { memory::init(0, &__end as *const u8 as usize - ::KERNEL_OFFSET); // TODO: allocate a stack - let stack_start = 0x00080000; - let stack_end = 0x0009F000; + let stack_start = 0x00080000 + ::KERNEL_OFFSET; + let stack_end = 0x0009F000 + ::KERNEL_OFFSET; // Initialize paging let (mut active_table, tcb_offset) = paging::init(0, stack_start, stack_end); @@ -148,7 +148,7 @@ pub unsafe extern fn kstart_ap(cpu_id: usize, page_table: usize, stack_start: us let kernel_table = KERNEL_TABLE.load(Ordering::SeqCst); // Initialize paging - let (mut active_table, tcb_offset) = paging::init_ap(cpu_id, stack_start, stack_end, kernel_table); + let (active_table, tcb_offset) = paging::init_ap(cpu_id, stack_start, stack_end, kernel_table); // Set up GDT for AP gdt::init(tcb_offset, stack_end); diff --git a/bootloader/x86_64/startup-x86_64.asm b/bootloader/x86_64/startup-x86_64.asm index 9cb683b..48d7980 100644 --- a/bootloader/x86_64/startup-x86_64.asm +++ b/bootloader/x86_64/startup-x86_64.asm @@ -122,10 +122,11 @@ long_mode: mov gs, rax mov ss, rax - mov rsp, 0x0009F000 + mov rsp, 0xFFFFFF000009F000 ;rust init mov rax, [kernel_base + 0x18] + xchg bx, bx jmp rax long_mode_ap: