Verified Commit e02f110f authored by Jeremy Soller's avatar Jeremy Soller
Browse files

Cleanup arch code, use asm instead of llvm_asm

parent 134a27a5
pub use self::paging::paging_create;
mod paging;
pub use self::paging::paging_create;
mod paging;
#![no_std]
#![feature(asm)]
#![feature(lang_items)]
#![feature(llvm_asm)]
#![cfg_attr(
target_os = "uefi",
no_main,
......
......@@ -14,7 +14,7 @@ pub extern "C" fn rust_begin_unwind(info: &PanicInfo) -> ! {
unsafe {
println!("BOOTLOADER PANIC:\n{}", info);
loop {
llvm_asm!("hlt" : : : : "intel", "volatile");
asm!("hlt");
}
}
}
......@@ -32,7 +32,7 @@ pub extern fn rust_oom(_layout: Layout) -> ! {
pub extern "C" fn _Unwind_Resume() -> ! {
loop {
unsafe {
llvm_asm!("hlt" : : : : "intel", "volatile");
asm!("hlt");
}
}
}
......@@ -16,10 +16,6 @@ use super::super::{
memory_map::memory_map,
};
use self::paging::paging;
mod paging;
static PHYS_OFFSET: u64 = 0xFFFF800000000000;
#[no_mangle]
......@@ -44,8 +40,14 @@ unsafe extern "C" fn kernel_entry(
// Disable interrupts
asm!("msr daifset, #2");
// Enable paging
paging();
// Disable MMU
asm!(
"mrs x0, sctlr_el1",
"bic x0, x0, 1",
"msr sctlr_el1, x0",
"isb",
lateout("x0") _
);
//TODO: Set stack
......
pub unsafe fn paging() {
// Disable MMU
asm!(
"mrs x0, sctlr_el1",
"bic x0, x0, 1",
"msr sctlr_el1, x0",
"isb",
lateout("x0") _
);
}
use core::{mem, ptr};
use std::vec::Vec;
use uefi::status::Result;
use x86::{
controlregs::{self, Cr0, Cr4},
msr,
};
use crate::{
KernelArgs,
......@@ -16,10 +20,6 @@ use super::super::{
memory_map::memory_map,
};
use self::paging::paging_enter;
mod paging;
static PHYS_OFFSET: u64 = 0xFFFF800000000000;
unsafe extern "C" fn kernel_entry(
......@@ -37,13 +37,32 @@ unsafe extern "C" fn kernel_entry(
}
// Disable interrupts
llvm_asm!("cli" : : : "memory" : "intel", "volatile");
// Enable paging
paging_enter(page_phys as u64);
asm!("cli");
// Enable OSXSAVE, FXSAVE/FXRSTOR, Page Global, Page Address Extension, and Page Size Extension
let mut cr4 = controlregs::cr4();
cr4 |= Cr4::CR4_ENABLE_OS_XSAVE
| Cr4::CR4_ENABLE_SSE
| Cr4::CR4_ENABLE_GLOBAL_PAGES
| Cr4::CR4_ENABLE_PAE
| Cr4::CR4_ENABLE_PSE;
controlregs::cr4_write(cr4);
// Enable Long mode and NX bit
let mut efer = msr::rdmsr(msr::IA32_EFER);
efer |= 1 << 11 | 1 << 8;
msr::wrmsr(msr::IA32_EFER, efer);
// Set new page map
controlregs::cr3_write(page_phys as u64);
// Enable paging, write protect kernel, protected mode
let mut cr0 = controlregs::cr0();
cr0 |= Cr0::CR0_ENABLE_PAGING | Cr0::CR0_WRITE_PROTECT | Cr0::CR0_PROTECTED_MODE;
controlregs::cr0_write(cr0);
// Set stack
llvm_asm!("mov rsp, $0" : : "r"(stack) : "memory" : "intel", "volatile");
asm!("mov rsp, {}", in(reg) stack);
// Call kernel entry
let entry_fn: extern "sysv64" fn(*const KernelArgs) -> ! = mem::transmute(func);
......
use x86::{
controlregs::{self, Cr0, Cr4},
msr,
};
pub unsafe fn paging_enter(page_phys: u64) {
// Enable OSXSAVE, FXSAVE/FXRSTOR, Page Global, Page Address Extension, and Page Size Extension
let mut cr4 = controlregs::cr4();
cr4 |= Cr4::CR4_ENABLE_OS_XSAVE
| Cr4::CR4_ENABLE_SSE
| Cr4::CR4_ENABLE_GLOBAL_PAGES
| Cr4::CR4_ENABLE_PAE
| Cr4::CR4_ENABLE_PSE;
controlregs::cr4_write(cr4);
// Enable Long mode and NX bit
let mut efer = msr::rdmsr(msr::IA32_EFER);
efer |= 1 << 11 | 1 << 8;
msr::wrmsr(msr::IA32_EFER, efer);
// Set new page map
controlregs::cr3_write(page_phys);
// Enable paging, write protect kernel, protected mode
let mut cr0 = controlregs::cr0();
cr0 |= Cr0::CR0_ENABLE_PAGING | Cr0::CR0_WRITE_PROTECT | Cr0::CR0_PROTECTED_MODE;
controlregs::cr0_write(cr0);
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment