From bea6a729fe3effa88f77f59c9fd6956b510b45a0 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Fri, 6 Apr 2018 17:43:58 +0200 Subject: [PATCH] gdt in place but triple faults because of spin::Once for now --- kernel-rs/Cargo.toml | 9 +++++---- kernel-rs/src/arch/x86/gdt.rs | 34 ++++++++++++++++++++++++++++++++++ kernel-rs/src/arch/x86/mod.rs | 13 +++++++------ kernel-rs/src/arch/x86/pti.rs | 3 +++ kernel-rs/src/lib.rs | 2 ++ kernel-rs/src/memory/mod.rs | 2 +- 6 files changed, 52 insertions(+), 11 deletions(-) create mode 100644 kernel-rs/src/arch/x86/gdt.rs diff --git a/kernel-rs/Cargo.toml b/kernel-rs/Cargo.toml index 217b03ac..d2b8779f 100644 --- a/kernel-rs/Cargo.toml +++ b/kernel-rs/Cargo.toml @@ -13,11 +13,12 @@ spin = "0.4" slab_allocator = "0.3.1" multiboot2 = { path = "multiboot2-elf64" } # added rsdp tag x86 = { path = "x86" } # forked for IA32 +raw-cpuid = { path = "rust-cpuid" } -[dependencies.raw-cpuid] -# need to use github/master because of features not yet on crates.io -git = "https://github.com/gz/rust-cpuid" -features = ["nightly"] +# [dependencies.raw-cpuid] +# # need to use github/master because of features not yet on crates.io +# git = "https://github.com/gz/rust-cpuid" +# features = ["nightly"] [dependencies.lazy_static] version = "1.0.0" diff --git a/kernel-rs/src/arch/x86/gdt.rs b/kernel-rs/src/arch/x86/gdt.rs new file mode 100644 index 00000000..0fed6425 --- /dev/null +++ b/kernel-rs/src/arch/x86/gdt.rs @@ -0,0 +1,34 @@ +use x86::structures::gdt; +use x86::structures::tss; +use x86::instructions::segmentation::set_cs; +use x86::instructions::tables::load_tss; +use spin::Once; + +static GDT: Once = Once::new(); +static TSS: Once = Once::new(); + +pub fn init() { + // let tss = tss::TaskStateSegment::new(); + let tss = TSS.call_once(|| { + let mut tss = tss::TaskStateSegment::new(); + tss + }); + + // let mut code_selector = gdt::SegmentSelector(0); + // let mut tss_selector = gdt::SegmentSelector(0); + + // let gdt = GDT.call_once(|| { + // let mut gdt = gdt::Gdt::new(); + // code_selector = gdt.add_entry(gdt::Descriptor::kernel_code_segment()); + // tss_selector = gdt.add_entry(gdt::Descriptor::tss_segment(&tss)); + // gdt + // }); + + // gdt.load(); + // unsafe { + // // reload code segment register + // set_cs(code_selector); + // // load TSS + // load_tss(tss_selector); + // } +} diff --git a/kernel-rs/src/arch/x86/mod.rs b/kernel-rs/src/arch/x86/mod.rs index f00125a2..74572557 100644 --- a/kernel-rs/src/arch/x86/mod.rs +++ b/kernel-rs/src/arch/x86/mod.rs @@ -6,6 +6,7 @@ pub mod interrupt; pub mod device; pub mod pti; +pub mod gdt; pub mod idt; use multiboot2; @@ -25,9 +26,6 @@ pub unsafe extern fn x86_rust_start(multiboot_info_addr: usize) { acpi::init().expect("ACPI failed"); } - // set up interrupts - idt::init(); - // set up physical allocator ::memory::init(&boot_info); @@ -37,12 +35,15 @@ pub unsafe extern fn x86_rust_start(multiboot_info_addr: usize) { // set up heap ::allocator::init(&mut active_table); + // set up memory segmentation + // gdt::init(); + + // set up interrupts + idt::init(); + // set up pic & apic device::init(&mut active_table); - // after core has loaded - ::memory::init_noncore(); - // primary CPU entry point ::kmain(); } diff --git a/kernel-rs/src/arch/x86/pti.rs b/kernel-rs/src/arch/x86/pti.rs index 7ed277cb..7942d611 100644 --- a/kernel-rs/src/arch/x86/pti.rs +++ b/kernel-rs/src/arch/x86/pti.rs @@ -12,6 +12,8 @@ pub static mut PTI_CONTEXT_STACK: usize = 0; #[inline(always)] unsafe fn switch_stack(old: usize, new: usize) { let old_esp: usize; + + // save the old esp asm!("" : "={esp}"(old_esp) : : : "intel", "volatile"); let offset_esp = old - old_esp; @@ -24,6 +26,7 @@ unsafe fn switch_stack(old: usize, new: usize) { offset_esp ); + // switch the esp with the new one asm!("" : : "{esp}"(new_esp) : : "intel", "volatile"); } diff --git a/kernel-rs/src/lib.rs b/kernel-rs/src/lib.rs index 509d3d71..8db6c338 100644 --- a/kernel-rs/src/lib.rs +++ b/kernel-rs/src/lib.rs @@ -47,6 +47,8 @@ pub mod arch; /// kernel entry point. arch module is responsible for calling this pub fn kmain() -> ! { + // core is loaded now + memory::init_noncore(); // x86::instructions::interrupts::int3(); diff --git a/kernel-rs/src/memory/mod.rs b/kernel-rs/src/memory/mod.rs index 01514299..a8c28ed5 100644 --- a/kernel-rs/src/memory/mod.rs +++ b/kernel-rs/src/memory/mod.rs @@ -72,7 +72,7 @@ pub fn deallocate_frames(frame: PhysFrame, count: usize) { /// Init memory module after core /// Must be called once, and only once, -pub unsafe fn init_noncore() { +pub fn init_noncore() { if let Some(ref mut controler) = *MEMORY_CONTROLER.lock() { controler.frame_allocator.set_core(true); } else {