Correctly position stack in higher half

This commit is contained in:
Jeremy Soller 2016-09-19 08:46:11 -06:00
parent 1331f4103f
commit 70a2faa0c7
4 changed files with 14 additions and 13 deletions

View file

@ -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) };

View file

@ -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);

View file

@ -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);

View file

@ -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: