mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-16 22:37:49 +00:00
103 lines
2.5 KiB
Rust
103 lines
2.5 KiB
Rust
// Copyright 2016 Philipp Oppermann. See the README.md
|
|
// file at the top-level directory of this distribution.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
// option. This file may not be copied, modified, or distributed
|
|
// except according to those terms.
|
|
|
|
#![feature(lang_items)]
|
|
#![feature(const_fn, unique)]
|
|
#![feature(alloc)]
|
|
#![feature(asm)]
|
|
#![feature(naked_functions)]
|
|
#![feature(abi_x86_interrupt)]
|
|
#![feature(const_unique_new)]
|
|
#![no_std]
|
|
|
|
extern crate rlibc;
|
|
extern crate volatile;
|
|
extern crate spin;
|
|
extern crate multiboot2;
|
|
#[macro_use]
|
|
extern crate bitflags;
|
|
extern crate x86_64;
|
|
#[macro_use]
|
|
extern crate once;
|
|
extern crate bit_field;
|
|
#[macro_use]
|
|
extern crate lazy_static;
|
|
|
|
extern crate hole_list_allocator as allocator;
|
|
#[macro_use]
|
|
extern crate alloc;
|
|
|
|
#[macro_use]
|
|
mod vga_buffer;
|
|
mod memory;
|
|
|
|
mod interrupts;
|
|
|
|
#[no_mangle]
|
|
pub extern "C" fn rust_main(multiboot_information_address: usize) {
|
|
// ATTENTION: we have a very small stack and no guard page
|
|
vga_buffer::clear_screen();
|
|
println!("Hello World{}", "!");
|
|
|
|
let boot_info = unsafe { multiboot2::load(multiboot_information_address) };
|
|
enable_nxe_bit();
|
|
enable_write_protect_bit();
|
|
|
|
// set up guard page and map the heap pages
|
|
let mut memory_controller = memory::init(boot_info);
|
|
|
|
// initialize our IDT
|
|
interrupts::init(&mut memory_controller);
|
|
|
|
fn stack_overflow() {
|
|
stack_overflow(); // for each recursion, the return address is pushed
|
|
}
|
|
|
|
// trigger a stack overflow
|
|
stack_overflow();
|
|
|
|
println!("It did not crash!");
|
|
loop {}
|
|
}
|
|
|
|
fn enable_nxe_bit() {
|
|
use x86_64::registers::msr::{IA32_EFER, rdmsr, wrmsr};
|
|
|
|
let nxe_bit = 1 << 11;
|
|
unsafe {
|
|
let efer = rdmsr(IA32_EFER);
|
|
wrmsr(IA32_EFER, efer | nxe_bit);
|
|
}
|
|
}
|
|
|
|
fn enable_write_protect_bit() {
|
|
use x86_64::registers::control_regs::{cr0, cr0_write, Cr0};
|
|
|
|
unsafe { cr0_write(cr0() | Cr0::WRITE_PROTECT) };
|
|
}
|
|
|
|
#[cfg(not(test))]
|
|
#[lang = "eh_personality"]
|
|
extern "C" fn eh_personality() {}
|
|
|
|
#[cfg(not(test))]
|
|
#[lang = "panic_fmt"]
|
|
#[no_mangle]
|
|
pub extern "C" fn panic_fmt(fmt: core::fmt::Arguments, file: &'static str, line: u32) -> ! {
|
|
println!("\n\nPANIC in {} at line {}:", file, line);
|
|
println!(" {}", fmt);
|
|
loop {}
|
|
}
|
|
|
|
#[allow(non_snake_case)]
|
|
#[no_mangle]
|
|
pub extern "C" fn _Unwind_Resume() -> ! {
|
|
loop {}
|
|
}
|