From b3a1c5194a3c8a27fefcc61075a740dc1eb762df Mon Sep 17 00:00:00 2001 From: wescande Date: Sun, 15 Apr 2018 21:00:53 +0200 Subject: [PATCH] add some --- kernel-rs/Makefile | 2 +- kernel-rs/src/arch/x86/gdt.rs | 16 ++++- kernel-rs/src/arch/x86/idt.rs | 2 + kernel-rs/src/arch/x86/interrupt/exception.rs | 1 + kernel-rs/src/arch/x86/mod.rs | 2 +- kernel-rs/src/io/mod.rs | 2 + kernel-rs/src/lib.rs | 15 ++++- kernel-rs/src/memory/mod.rs | 62 +++++++++++-------- kernel-rs/src/memory/stack_allocator.rs | 18 +++--- kernel-rs/x86 | 2 +- 10 files changed, 80 insertions(+), 42 deletions(-) diff --git a/kernel-rs/Makefile b/kernel-rs/Makefile index ebafd3f1..5f339951 100644 --- a/kernel-rs/Makefile +++ b/kernel-rs/Makefile @@ -15,7 +15,7 @@ build/arch/$(ARCH)/%.o: src/arch/$(ARCH)/%.asm Makefile ## COMPILE RUST (xargo) rust_os := target/$(TARGET)/debug/lib$(OS).a $(rust_os): $(TARGET).json Makefile - @RUST_TARGET_PATH="$(shell pwd)" xargo build --target $(TARGET) + @TERM=xterm RUST_TARGET_PATH="$(shell pwd)" xargo build --target $(TARGET) ## LINKAGE kernel := build/$(OS) diff --git a/kernel-rs/src/arch/x86/gdt.rs b/kernel-rs/src/arch/x86/gdt.rs index cea7e214..a5cb7ab7 100644 --- a/kernel-rs/src/arch/x86/gdt.rs +++ b/kernel-rs/src/arch/x86/gdt.rs @@ -2,14 +2,25 @@ use x86::structures::gdt; use x86::structures::tss; use x86::instructions::segmentation::set_cs; use x86::instructions::tables::load_tss; +use arch::x86::paging::ActivePageTable; use spin::Once; +// use io; static GDT: Once = Once::new(); static TSS: Once = Once::new(); -pub fn init() { +pub fn init(mut active_table: &mut ActivePageTable) { + // let tss = tss::TaskStateSegment::new(); let tss = TSS.call_once(|| { - let tss = tss::TaskStateSegment::new(); + let mut tss = tss::TaskStateSegment::new(); +match ::memory::allocate_stack(&mut active_table) { + Some(stack) => {tss.esp0 = stack.top; tss.ss = 0x18}, + // Some(stack) => {tss.esp = stack.top; tss.ebp = stack.bottom }, + _ => panic!("There is no stack available for tss"), + }; + // tss.esp = tss.esp0; + // tss.ebp = tss.esp; + // println!("tss on {:#x}", tss.esp0);flush!(); tss }); @@ -33,6 +44,7 @@ pub fn init() { println!("gdt 3 lower: {:#x}", gdt.table[3] >> 32 as u32); flush!(); + // io::halt(); gdt.load(); unsafe { // reload code segment register diff --git a/kernel-rs/src/arch/x86/idt.rs b/kernel-rs/src/arch/x86/idt.rs index 4a511522..7407f2c0 100644 --- a/kernel-rs/src/arch/x86/idt.rs +++ b/kernel-rs/src/arch/x86/idt.rs @@ -15,10 +15,12 @@ lazy_static! { idt.invalid_opcode.set_handler_fn(exception::invalid_opcode); idt.device_not_available.set_handler_fn(exception::device_not_available); idt.double_fault.set_handler_fn(exception::double_fault); + idt.double_fault.set_gate_type(GateType::TaskGate32); idt.segment_not_present.set_handler_fn(exception::segment_not_present); idt.stack_segment_fault.set_handler_fn(exception::stack_segment); idt.general_protection_fault.set_handler_fn(exception::general_protection); idt.page_fault.set_handler_fn(exception::page_fault); + idt.page_fault.set_gate_type(GateType::TaskGate32); idt.x87_floating_point.set_handler_fn(exception::x87_fpu); idt.alignment_check.set_handler_fn(exception::alignment_check); idt.machine_check.set_handler_fn(exception::machine_check); diff --git a/kernel-rs/src/arch/x86/interrupt/exception.rs b/kernel-rs/src/arch/x86/interrupt/exception.rs index b8994281..1eead757 100644 --- a/kernel-rs/src/arch/x86/interrupt/exception.rs +++ b/kernel-rs/src/arch/x86/interrupt/exception.rs @@ -1,6 +1,7 @@ // https://wiki.osdev.org/Exceptions use arch::x86::pti; +use io; macro_rules! exception { ($name:ident, $func:block) => { diff --git a/kernel-rs/src/arch/x86/mod.rs b/kernel-rs/src/arch/x86/mod.rs index 2110ffae..fb606873 100644 --- a/kernel-rs/src/arch/x86/mod.rs +++ b/kernel-rs/src/arch/x86/mod.rs @@ -40,7 +40,7 @@ pub unsafe extern "C" fn x86_rust_start(multiboot_info_addr: usize) { idt::init(); // fill and load gdt - gdt::init(); + gdt::init(&mut active_table); // set up heap ::allocator::init(&mut active_table); diff --git a/kernel-rs/src/io/mod.rs b/kernel-rs/src/io/mod.rs index 8c4c4543..2bfb053a 100644 --- a/kernel-rs/src/io/mod.rs +++ b/kernel-rs/src/io/mod.rs @@ -29,10 +29,12 @@ pub trait Io { } } +#[inline(always)] pub fn cli() { unsafe { asm!("cli" : : : : "volatile") }; } +#[inline(always)] pub fn halt() -> ! { cli(); loop { diff --git a/kernel-rs/src/lib.rs b/kernel-rs/src/lib.rs index 9fe8e091..143320a0 100644 --- a/kernel-rs/src/lib.rs +++ b/kernel-rs/src/lib.rs @@ -44,6 +44,8 @@ pub mod memory; /// arch specific entry points pub mod arch; +// use core::mem; +// use x86::structures::idt::*; /// kernel entry point. arch module is responsible for /// calling this once the core has loaded pub fn kmain() -> ! { @@ -51,15 +53,22 @@ pub fn kmain() -> ! { memory::init_noncore(); // x86::instructions::interrupts::int3(); + // println!("size of idt entry: {}", mem::size_of::>()); + // println!("size of i32 {}", mem::size_of::()); + // flush!(); + // unsafe {asm!("hlt");} + // let x = 0; + // let y = 5 /x; + // println!("x {} y {}", x, y); // fn stack_overflow() { // stack_overflow(); // } // stack_overflow(); - // unsafe { - // *(0xdead as *mut u32) = 42; - // }; + unsafe { + *(0xdead as *mut u32) = 42; + }; // vga is *not* cpu specific vga::init(); diff --git a/kernel-rs/src/memory/mod.rs b/kernel-rs/src/memory/mod.rs index 314b2dbc..d27633d8 100644 --- a/kernel-rs/src/memory/mod.rs +++ b/kernel-rs/src/memory/mod.rs @@ -4,12 +4,14 @@ mod stack_allocator; use multiboot2; use x86::structures::paging::*; +use arch::x86::paging::ActivePageTable; use x86::*; -use spin::Mutex; +// use spin::Mutex; use self::bump::BumpFrameAllocator; use self::recycle::RecycleAllocator; -use self::stack_allocator::StackAllocator; +use self::stack_allocator::{Stack,StackAllocator}; + pub trait FrameAllocator { fn allocate_frames(&mut self, size: usize) -> Option; @@ -21,7 +23,7 @@ pub struct MemoryControler { stack_allocator: StackAllocator, } -static MEMORY_CONTROLER: Mutex> = Mutex::new(None); +static mut MEMORY_CONTROLER: Option = None; pub fn init(boot_info: &multiboot2::BootInformation) { let elf_sections_tag = boot_info.elf_sections_tag().unwrap(); @@ -47,7 +49,7 @@ pub fn init(boot_info: &multiboot2::BootInformation) { boot_info.start_address(), boot_info.end_address(), memory_map_tag.memory_areas(), - ); + ); let frame_allocator = RecycleAllocator::new(bump_allocator); @@ -61,42 +63,52 @@ pub fn init(boot_info: &multiboot2::BootInformation) { StackAllocator::new(stack_alloc_range) }; - *MEMORY_CONTROLER.lock() = Some(MemoryControler { - frame_allocator, - stack_allocator, - }); + unsafe { + MEMORY_CONTROLER = Some(MemoryControler { + frame_allocator, + stack_allocator, + }); + } } pub fn allocate_frames(count: usize) -> Option { - if let Some(ref mut controler) = *MEMORY_CONTROLER.lock() { - controler.frame_allocator.allocate_frames(count) - } else { - panic!("frame allocator not initialized!"); + unsafe { + if let Some(ref mut controler) = MEMORY_CONTROLER { + controler.frame_allocator.allocate_frames(count) + } else { + panic!("frame allocator not initialized!"); + } } } pub fn deallocate_frames(frame: PhysFrame, count: usize) { - if let Some(ref mut controler) = *MEMORY_CONTROLER.lock() { - controler.frame_allocator.deallocate_frames(frame, count) - } else { - panic!("frame allocator not initialized!"); + unsafe { + if let Some(ref mut controler) = MEMORY_CONTROLER { + controler.frame_allocator.deallocate_frames(frame, count) + } else { + panic!("frame allocator not initialized!"); + } } } -pub fn allocate_stack() { - if let Some(ref mut controler) = *MEMORY_CONTROLER.lock() { - controler.stack_allocator.allocate_stack() - } else { - panic!("frame allocator not initialized!"); +pub fn allocate_stack(mut active_table: &mut ActivePageTable) -> Option { + unsafe { + if let Some(ref mut controler) = MEMORY_CONTROLER { + controler.stack_allocator.allocate_stack(&mut active_table, &mut controler.frame_allocator, 5) + } else { + panic!("frame allocator not initialized!"); + } } } /// Init memory module after core /// Must be called once, and only once, pub fn init_noncore() { - if let Some(ref mut controler) = *MEMORY_CONTROLER.lock() { - controler.frame_allocator.set_core(true); - } else { - panic!("frame allocator not initialized"); + unsafe { + if let Some(ref mut controler) = MEMORY_CONTROLER { + controler.frame_allocator.set_core(true); + } else { + panic!("frame allocator not initialized"); + } } } diff --git a/kernel-rs/src/memory/stack_allocator.rs b/kernel-rs/src/memory/stack_allocator.rs index ddf17a06..f9aa0ed0 100644 --- a/kernel-rs/src/memory/stack_allocator.rs +++ b/kernel-rs/src/memory/stack_allocator.rs @@ -1,25 +1,25 @@ use x86::structures::paging::*; -use memory::paging::ActivePageTable; +use arch::x86::paging::ActivePageTable; use memory::*; use core::ops::Range; #[derive(Debug)] pub struct Stack { - top: usize, - bottom: usize, + pub top: u32, + pub bottom: u32, } impl Stack { - fn new(top: usize, bottom: usize) -> Stack { + fn new(top: u32, bottom: u32) -> Stack { assert!(top > bottom); Stack { top, bottom } } - pub fn top(&self) -> usize { + pub fn top(&self) -> u32 { self.top } - pub fn bottom(&self) -> usize { + pub fn bottom(&self) -> u32 { self.bottom } } @@ -65,14 +65,14 @@ impl StackAllocator { // map stack pages to physical frames for page in range { - active_table.map(page, PageTableFlags::WRITABLE, frame_allocator); + active_table.map(page, PageTableFlags::WRITABLE); } // create a new stack let top_of_stack = end.start_address().as_u32() + PAGE_SIZE as u32; Some(Stack::new( - top_of_stack as usize, - start.start_address().as_u32() as usize, + top_of_stack, + start.start_address().as_u32(), )) } _ => None, /* not enough pages */ diff --git a/kernel-rs/x86 b/kernel-rs/x86 index 2a8416d0..c6636434 160000 --- a/kernel-rs/x86 +++ b/kernel-rs/x86 @@ -1 +1 @@ -Subproject commit 2a8416d0af403786d082695d13a0f74cd2b7f216 +Subproject commit c6636434cb64916c303c17c243c0e93830882248