Move asm to boot dir. Continue to refactor...

master
WangRunji 7 years ago
parent 53bb54330c
commit c436b9afbe

@ -4,11 +4,12 @@ iso := build/os-$(arch).iso
target ?= $(arch)-blog_os target ?= $(arch)-blog_os
rust_os := target/$(target)/debug/libblog_os.a rust_os := target/$(target)/debug/libblog_os.a
linker_script := src/arch/$(arch)/linker.ld boot_src := src/arch/$(arch)/boot
grub_cfg := src/arch/$(arch)/grub.cfg linker_script := $(boot_src)/linker.ld
assembly_source_files := $(wildcard src/arch/$(arch)/*.asm) grub_cfg := $(boot_src)/grub.cfg
assembly_object_files := $(patsubst src/arch/$(arch)/%.asm, \ assembly_source_files := $(wildcard $(boot_src)/*.asm)
build/arch/$(arch)/%.o, $(assembly_source_files)) assembly_object_files := $(patsubst $(boot_src)/%.asm, \
build/arch/$(arch)/boot/%.o, $(assembly_source_files))
ifeq ($(shell uname), Linux) ifeq ($(shell uname), Linux)
prefix := prefix :=
@ -45,7 +46,7 @@ kernel:
@RUST_TARGET_PATH=$(shell pwd) xargo build --target $(target) @RUST_TARGET_PATH=$(shell pwd) xargo build --target $(target)
# compile assembly files # compile assembly files
build/arch/$(arch)/%.o: src/arch/$(arch)/%.asm build/arch/$(arch)/boot/%.o: $(boot_src)/%.asm
@mkdir -p $(shell dirname $@) @mkdir -p $(shell dirname $@)
@nasm -felf64 $< -o $@ @nasm -felf64 $< -o $@
@ -66,7 +67,7 @@ docker_iso:
@docker run --rm $(docker_args) $(docker_image):$(tag) make iso @docker run --rm $(docker_args) $(docker_image):$(tag) make iso
docker_run: docker_iso docker_run: docker_iso
@qemu-system-x86_64 -cdrom $(iso) -s @qemu-system-$(arch) -cdrom $(iso) -s
docker_interactive: docker_interactive:
@docker run -it --rm $(docker_args) $(docker_image):$(tag) @docker run -it --rm $(docker_args) $(docker_image):$(tag)

@ -0,0 +1,26 @@
// Enable 'No-Execute' bit in page entry
fn enable_nxe_bit() {
use x86_64::registers::msr::{IA32_EFER, rdmsr, wrmsr};
let nxe_bit = 1 << 11;
// The EFER register is only allowed in kernel mode
// But we are in kernel mode. So it's safe.
unsafe {
let efer = rdmsr(IA32_EFER);
wrmsr(IA32_EFER, efer | nxe_bit);
}
}
// Enable write protection in kernel mode
fn enable_write_protect_bit() {
use x86_64::registers::control_regs::{cr0, cr0_write, Cr0};
// The CR0 register is only allowed in kernel mode
// But we are in kernel mode. So it's safe.
unsafe { cr0_write(cr0() | Cr0::WRITE_PROTECT) };
}
pub fn init() {
enable_nxe_bit();
enable_write_protect_bit();
}

@ -0,0 +1,15 @@
// Rust language features implementions
use core;
#[lang = "eh_personality"]
extern fn eh_personality() {
}
#[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{}
}

@ -26,22 +26,27 @@ extern crate linked_list_allocator;
#[macro_use] #[macro_use]
extern crate lazy_static; extern crate lazy_static;
extern crate bit_field; extern crate bit_field;
#[macro_use]
#[macro_use] // println!
mod vga_buffer; mod vga_buffer;
mod memory; mod memory;
mod interrupts; mod interrupts;
mod lang;
#[allow(dead_code)]
#[cfg(target_arch = "x86_64")]
#[path = "arch/x86_64/mod.rs"]
mod arch;
// The entry point of Rust kernel
#[no_mangle] #[no_mangle]
pub extern "C" fn rust_main(multiboot_information_address: usize) { pub extern "C" fn rust_main(multiboot_information_address: usize) {
// ATTENTION: we have a very small stack and no guard page // ATTENTION: we have a very small stack and no guard page
vga_buffer::clear_screen(); vga_buffer::clear_screen();
println!("Hello World{}", "!"); println!("Hello World{}", "!");
let boot_info = unsafe { let boot_info = unsafe { multiboot2::load(multiboot_information_address) };
multiboot2::load(multiboot_information_address) arch::init();
};
enable_nxe_bit();
enable_write_protect_bit();
// set up guard page and map the heap pages // set up guard page and map the heap pages
let mut memory_controller = memory::init(boot_info); let mut memory_controller = memory::init(boot_info);
@ -72,32 +77,6 @@ pub extern "C" fn rust_main(multiboot_information_address: usize) {
loop {} 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) };
}
#[lang = "eh_personality"] extern fn eh_personality() {}
#[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{}
}
use linked_list_allocator::LockedHeap; use linked_list_allocator::LockedHeap;
pub const HEAP_START: usize = 0o_000_001_000_000_0000; pub const HEAP_START: usize = 0o_000_001_000_000_0000;

Loading…
Cancel
Save