2017-04-11 17:02:21 +04:00
|
|
|
#![feature(lang_items)]
|
2017-04-12 21:16:04 +04:00
|
|
|
#![feature(const_fn)]
|
2017-11-19 15:41:20 +04:00
|
|
|
#![feature(alloc)]
|
2017-11-19 15:54:47 +04:00
|
|
|
#![feature(const_unique_new, const_atomic_usize_new)]
|
2017-04-12 21:20:15 +04:00
|
|
|
#![feature(unique)]
|
2017-11-19 15:50:16 +04:00
|
|
|
#![feature(allocator_api)]
|
2017-11-19 16:13:18 +04:00
|
|
|
#![feature(global_allocator)]
|
2017-04-18 17:14:27 +04:00
|
|
|
#![feature(abi_x86_interrupt)]
|
2017-04-11 17:02:21 +04:00
|
|
|
#![no_std]
|
|
|
|
|
2017-11-19 15:41:20 +04:00
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
extern crate alloc;
|
|
|
|
|
2017-04-11 20:25:51 +04:00
|
|
|
extern crate rlibc;
|
2017-04-12 21:24:25 +04:00
|
|
|
extern crate volatile;
|
2017-04-12 21:32:59 +04:00
|
|
|
extern crate spin;
|
2017-04-13 19:51:09 +04:00
|
|
|
extern crate multiboot2;
|
2017-04-13 20:27:39 +04:00
|
|
|
#[macro_use]
|
|
|
|
extern crate bitflags;
|
2017-04-13 21:40:20 +04:00
|
|
|
extern crate x86_64;
|
2017-11-19 16:19:17 +04:00
|
|
|
#[macro_use]
|
|
|
|
extern crate once;
|
2017-11-19 17:16:33 +04:00
|
|
|
extern crate linked_list_allocator;
|
2017-04-12 21:35:08 +04:00
|
|
|
#[macro_use]
|
2017-04-18 17:15:44 +04:00
|
|
|
extern crate lazy_static;
|
|
|
|
#[macro_use]
|
2017-04-12 21:16:04 +04:00
|
|
|
mod vga_buffer;
|
2017-04-13 19:59:12 +04:00
|
|
|
mod memory;
|
2017-04-18 17:09:03 +04:00
|
|
|
mod interrupts;
|
2017-04-12 21:16:04 +04:00
|
|
|
|
2017-04-11 17:02:21 +04:00
|
|
|
#[no_mangle]
|
2017-11-19 16:16:33 +04:00
|
|
|
pub extern "C" fn rust_main(multiboot_information_address: usize) {
|
|
|
|
// ATTENTION: we have a very small stack and no guard page
|
2017-04-12 21:35:08 +04:00
|
|
|
vga_buffer::clear_screen();
|
|
|
|
println!("Hello World{}", "!");
|
2017-04-11 21:30:19 +04:00
|
|
|
|
2017-11-19 16:16:33 +04:00
|
|
|
let boot_info = unsafe {
|
|
|
|
multiboot2::load(multiboot_information_address)
|
|
|
|
};
|
|
|
|
enable_nxe_bit();
|
|
|
|
enable_write_protect_bit();
|
2017-04-13 19:57:33 +04:00
|
|
|
|
2017-11-19 16:16:33 +04:00
|
|
|
// set up guard page and map the heap pages
|
2017-04-18 20:20:15 +04:00
|
|
|
let mut memory_controller = memory::init(boot_info);
|
2017-04-18 14:22:36 +04:00
|
|
|
|
2017-11-19 17:16:33 +04:00
|
|
|
unsafe {
|
|
|
|
HEAP_ALLOCATOR.lock().init(HEAP_START, HEAP_START + HEAP_SIZE);
|
|
|
|
}
|
|
|
|
|
2017-04-18 17:17:43 +04:00
|
|
|
// initialize our IDT
|
2017-04-18 20:20:15 +04:00
|
|
|
interrupts::init(&mut memory_controller);
|
2017-04-18 17:17:43 +04:00
|
|
|
|
2017-11-19 17:16:33 +04:00
|
|
|
for i in 0..10000 {
|
|
|
|
format!("Some String");
|
|
|
|
}
|
2017-04-13 20:05:32 +04:00
|
|
|
|
2017-04-18 17:17:43 +04:00
|
|
|
// invoke a breakpoint exception
|
|
|
|
x86_64::instructions::interrupts::int3();
|
2017-04-18 14:21:47 +04:00
|
|
|
|
2017-04-18 20:13:05 +04:00
|
|
|
fn stack_overflow() {
|
|
|
|
stack_overflow(); // for each recursion, the return address is pushed
|
|
|
|
}
|
|
|
|
|
|
|
|
// trigger a stack overflow
|
|
|
|
stack_overflow();
|
|
|
|
|
2017-04-18 20:10:23 +04:00
|
|
|
|
2017-04-18 17:17:43 +04:00
|
|
|
println!("It did not crash!");
|
2017-04-18 14:21:47 +04:00
|
|
|
loop {}
|
2017-04-11 20:25:51 +04:00
|
|
|
}
|
2017-04-11 17:02:21 +04:00
|
|
|
|
2017-04-18 14:32:59 +04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-18 14:33:53 +04:00
|
|
|
fn enable_write_protect_bit() {
|
|
|
|
use x86_64::registers::control_regs::{cr0, cr0_write, Cr0};
|
|
|
|
|
|
|
|
unsafe { cr0_write(cr0() | Cr0::WRITE_PROTECT) };
|
|
|
|
}
|
|
|
|
|
2017-04-11 17:02:21 +04:00
|
|
|
#[lang = "eh_personality"] extern fn eh_personality() {}
|
2017-04-13 19:53:34 +04:00
|
|
|
|
|
|
|
#[lang = "panic_fmt"]
|
|
|
|
#[no_mangle]
|
|
|
|
pub extern fn panic_fmt(fmt: core::fmt::Arguments, file: &'static str, line: u32) -> ! {
|
|
|
|
println!("\n\nPANIC in {} at line {}:", file, line);
|
|
|
|
println!(" {}", fmt);
|
|
|
|
loop{}
|
|
|
|
}
|
2017-11-19 16:13:18 +04:00
|
|
|
|
2017-11-19 17:16:33 +04:00
|
|
|
use linked_list_allocator::LockedHeap;
|
2017-11-19 16:13:18 +04:00
|
|
|
|
|
|
|
pub const HEAP_START: usize = 0o_000_001_000_000_0000;
|
|
|
|
pub const HEAP_SIZE: usize = 100 * 1024; // 100 KiB
|
|
|
|
|
|
|
|
#[global_allocator]
|
2017-11-19 17:16:33 +04:00
|
|
|
static HEAP_ALLOCATOR: LockedHeap = LockedHeap::empty();
|