From 5d857c38eb574847c3b3a5cc7f22756d0db0531f Mon Sep 17 00:00:00 2001 From: WangRunji Date: Thu, 26 Apr 2018 21:53:20 +0800 Subject: [PATCH] Let struct Stack own the stack, panic on Drop. Add a little process mod. --- src/arch/x86_64/smp.rs | 6 +++- src/lib.rs | 7 ++-- src/memory/mod.rs | 18 ++++++---- src/memory/stack_allocator.rs | 8 ++++- src/process.rs | 64 +++++++++++++++++++++++++++++++++++ 5 files changed, 92 insertions(+), 11 deletions(-) create mode 100644 src/process.rs diff --git a/src/arch/x86_64/smp.rs b/src/arch/x86_64/smp.rs index 3bc2af0..3af5555 100644 --- a/src/arch/x86_64/smp.rs +++ b/src/arch/x86_64/smp.rs @@ -19,8 +19,12 @@ pub fn start_other_cores(acpi: &ACPI_Result, mc: &mut MemoryController) { let page_table = unsafe{ *(0xFFFF_FFFF_FFFF_FFF8 as *const u32) } & 0xFFFF_F000; for i in 1 .. acpi.cpu_num { let apic_id = acpi.cpu_acpi_ids[i as usize]; + let kstack = mc.alloc_stack(7).unwrap(); + let kstack_top = kstack.top() as u64; + use core::mem::forget; + forget(kstack); // TODO pass this kstack to new AP *args = EntryArgs { - kstack: mc.alloc_stack(7).unwrap().top() as u64, + kstack: kstack_top, page_table: page_table, stack: 0x8000, // just enough stack to get us to entry64mp }; diff --git a/src/lib.rs b/src/lib.rs index c8801c3..1b997cc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -41,6 +41,7 @@ mod util; #[macro_use] // test! mod test_util; mod consts; +mod process; #[allow(dead_code)] #[cfg(target_arch = "x86_64")] @@ -76,9 +77,11 @@ pub extern "C" fn rust_main(multiboot_information_address: usize) -> ! { // memory_controller.print_page_table(); arch::smp::start_other_cores(&acpi, &mut memory_controller); + process::init(memory_controller.kernel_stack.take().unwrap()); + unsafe{ arch::interrupt::enable(); } - unsafe{ int!(120); } // to user +// unsafe{ int!(120); } // to user loop{} @@ -95,7 +98,7 @@ pub extern "C" fn other_main() -> ! { let cpu_id = arch::driver::apic::lapic_id(); println!("Hello world! from CPU {}!", arch::driver::apic::lapic_id()); unsafe{ arch::smp::notify_started(cpu_id); } - unsafe{ let a = *(0xdeadbeaf as *const u8); } // Page fault +// unsafe{ let a = *(0xdeadbeaf as *const u8); } // Page fault loop {} } diff --git a/src/memory/mod.rs b/src/memory/mod.rs index d039ddb..29a6b39 100644 --- a/src/memory/mod.rs +++ b/src/memory/mod.rs @@ -46,7 +46,7 @@ pub fn init(boot_info: &BootInformation) -> MemoryController { boot_info_start, boot_info_end, memory_map_tag.memory_areas()); - let mut active_table = remap_the_kernel(&mut frame_allocator, boot_info); + let (mut active_table, kernel_stack) = remap_the_kernel(&mut frame_allocator, boot_info); use self::paging::Page; use consts::{KERNEL_HEAP_OFFSET, KERNEL_HEAP_SIZE}; @@ -67,14 +67,15 @@ pub fn init(boot_info: &BootInformation) -> MemoryController { }; MemoryController { - active_table: active_table, - frame_allocator: frame_allocator, - stack_allocator: stack_allocator, + kernel_stack: Some(kernel_stack), + active_table, + frame_allocator, + stack_allocator, } } pub fn remap_the_kernel(allocator: &mut A, boot_info: &BootInformation) - -> ActivePageTable + -> (ActivePageTable, Stack) where A: FrameAllocator { let mut temporary_page = TemporaryPage::new(Page::containing_address(0xcafebabe), allocator); @@ -137,12 +138,14 @@ pub fn remap_the_kernel(allocator: &mut A, boot_info: &BootInformation) let stack_bottom = PhysicalAddress(stack_bottom as u64).to_kernel_virtual(); let stack_bottom_page = Page::containing_address(stack_bottom); active_table.unmap(stack_bottom_page, allocator); + let kernel_stack = Stack::new(stack_bottom + 8 * PAGE_SIZE, stack_bottom + 1 * PAGE_SIZE); println!("guard page at {:#x}", stack_bottom_page.start_address()); - active_table + (active_table, kernel_stack) } pub struct MemoryController { + pub kernel_stack: Option, active_table: paging::ActivePageTable, frame_allocator: AreaFrameAllocator, stack_allocator: stack_allocator::StackAllocator, @@ -150,7 +153,8 @@ pub struct MemoryController { impl MemoryController { pub fn alloc_stack(&mut self, size_in_pages: usize) -> Option { - let &mut MemoryController { ref mut active_table, + let &mut MemoryController { ref mut kernel_stack, + ref mut active_table, ref mut frame_allocator, ref mut stack_allocator } = self; stack_allocator.alloc_stack(active_table, frame_allocator, diff --git a/src/memory/stack_allocator.rs b/src/memory/stack_allocator.rs index 7249874..83a2f37 100644 --- a/src/memory/stack_allocator.rs +++ b/src/memory/stack_allocator.rs @@ -61,7 +61,7 @@ pub struct Stack { } impl Stack { - fn new(top: usize, bottom: usize) -> Stack { + pub(super) fn new(top: usize, bottom: usize) -> Stack { assert!(top > bottom); Stack { top: top, @@ -77,3 +77,9 @@ impl Stack { self.bottom } } + +impl Drop for Stack { + fn drop(&mut self) { + panic!("stack leak: {:#x?}", self); + } +} \ No newline at end of file diff --git a/src/process.rs b/src/process.rs new file mode 100644 index 0000000..9706526 --- /dev/null +++ b/src/process.rs @@ -0,0 +1,64 @@ +use alloc::{boxed::Box, string::String, btree_map::BTreeMap}; +use memory::Stack; + +pub fn init(kstack: Stack) { + let processor = Box::new(Processor::new(kstack)); + Box::into_raw(processor); +} + +#[derive(Debug)] +pub struct Process { + pid: Pid, + name: String, + kstack: Stack, +// page_table: Box, + status: Status, + context: Context, +} + +#[derive(Debug)] +struct Context { + +} + +#[derive(Debug)] +enum Status { + Uninit, Ready, Running, Sleeping(usize), Exited +} + +struct Processor { + procs: BTreeMap>, +} + +type Pid = usize; + +impl Processor { + fn new(kernel_stack: Stack) -> Self { + let mut processor = Processor { + procs: BTreeMap::>::new(), + }; + let initproc = Box::new(Process{ + pid: 0, + name: String::from("initproc"), + kstack: kernel_stack, + status: Status::Running, + context: Context{}, + }); + processor.procs.insert(0, initproc); + processor + } + fn alloc_pid(&self) -> Pid { + let mut next: Pid = 0; + for &i in self.procs.keys() { + if i != next { + return next; + } else { + next = i + 1; + } + } + return next; + } + fn schedule(&self) { + + } +} \ No newline at end of file