Let struct Stack own the stack, panic on Drop. Add a little process mod.

master
WangRunji 7 years ago
parent 0539f8673e
commit 5d857c38eb

@ -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; let page_table = unsafe{ *(0xFFFF_FFFF_FFFF_FFF8 as *const u32) } & 0xFFFF_F000;
for i in 1 .. acpi.cpu_num { for i in 1 .. acpi.cpu_num {
let apic_id = acpi.cpu_acpi_ids[i as usize]; 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 { *args = EntryArgs {
kstack: mc.alloc_stack(7).unwrap().top() as u64, kstack: kstack_top,
page_table: page_table, page_table: page_table,
stack: 0x8000, // just enough stack to get us to entry64mp stack: 0x8000, // just enough stack to get us to entry64mp
}; };

@ -41,6 +41,7 @@ mod util;
#[macro_use] // test! #[macro_use] // test!
mod test_util; mod test_util;
mod consts; mod consts;
mod process;
#[allow(dead_code)] #[allow(dead_code)]
#[cfg(target_arch = "x86_64")] #[cfg(target_arch = "x86_64")]
@ -76,9 +77,11 @@ pub extern "C" fn rust_main(multiboot_information_address: usize) -> ! {
// memory_controller.print_page_table(); // memory_controller.print_page_table();
arch::smp::start_other_cores(&acpi, &mut memory_controller); arch::smp::start_other_cores(&acpi, &mut memory_controller);
process::init(memory_controller.kernel_stack.take().unwrap());
unsafe{ arch::interrupt::enable(); } unsafe{ arch::interrupt::enable(); }
unsafe{ int!(120); } // to user // unsafe{ int!(120); } // to user
loop{} loop{}
@ -95,7 +98,7 @@ pub extern "C" fn other_main() -> ! {
let cpu_id = arch::driver::apic::lapic_id(); let cpu_id = arch::driver::apic::lapic_id();
println!("Hello world! from CPU {}!", arch::driver::apic::lapic_id()); println!("Hello world! from CPU {}!", arch::driver::apic::lapic_id());
unsafe{ arch::smp::notify_started(cpu_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 {} loop {}
} }

@ -46,7 +46,7 @@ pub fn init(boot_info: &BootInformation) -> MemoryController {
boot_info_start, boot_info_end, boot_info_start, boot_info_end,
memory_map_tag.memory_areas()); 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 self::paging::Page;
use consts::{KERNEL_HEAP_OFFSET, KERNEL_HEAP_SIZE}; use consts::{KERNEL_HEAP_OFFSET, KERNEL_HEAP_SIZE};
@ -67,14 +67,15 @@ pub fn init(boot_info: &BootInformation) -> MemoryController {
}; };
MemoryController { MemoryController {
active_table: active_table, kernel_stack: Some(kernel_stack),
frame_allocator: frame_allocator, active_table,
stack_allocator: stack_allocator, frame_allocator,
stack_allocator,
} }
} }
pub fn remap_the_kernel<A>(allocator: &mut A, boot_info: &BootInformation) pub fn remap_the_kernel<A>(allocator: &mut A, boot_info: &BootInformation)
-> ActivePageTable -> (ActivePageTable, Stack)
where A: FrameAllocator where A: FrameAllocator
{ {
let mut temporary_page = TemporaryPage::new(Page::containing_address(0xcafebabe), allocator); let mut temporary_page = TemporaryPage::new(Page::containing_address(0xcafebabe), allocator);
@ -137,12 +138,14 @@ pub fn remap_the_kernel<A>(allocator: &mut A, boot_info: &BootInformation)
let stack_bottom = PhysicalAddress(stack_bottom as u64).to_kernel_virtual(); let stack_bottom = PhysicalAddress(stack_bottom as u64).to_kernel_virtual();
let stack_bottom_page = Page::containing_address(stack_bottom); let stack_bottom_page = Page::containing_address(stack_bottom);
active_table.unmap(stack_bottom_page, allocator); 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()); println!("guard page at {:#x}", stack_bottom_page.start_address());
active_table (active_table, kernel_stack)
} }
pub struct MemoryController { pub struct MemoryController {
pub kernel_stack: Option<Stack>,
active_table: paging::ActivePageTable, active_table: paging::ActivePageTable,
frame_allocator: AreaFrameAllocator, frame_allocator: AreaFrameAllocator,
stack_allocator: stack_allocator::StackAllocator, stack_allocator: stack_allocator::StackAllocator,
@ -150,7 +153,8 @@ pub struct MemoryController {
impl MemoryController { impl MemoryController {
pub fn alloc_stack(&mut self, size_in_pages: usize) -> Option<Stack> { pub fn alloc_stack(&mut self, size_in_pages: usize) -> Option<Stack> {
let &mut MemoryController { ref mut active_table, let &mut MemoryController { ref mut kernel_stack,
ref mut active_table,
ref mut frame_allocator, ref mut frame_allocator,
ref mut stack_allocator } = self; ref mut stack_allocator } = self;
stack_allocator.alloc_stack(active_table, frame_allocator, stack_allocator.alloc_stack(active_table, frame_allocator,

@ -61,7 +61,7 @@ pub struct Stack {
} }
impl Stack { impl Stack {
fn new(top: usize, bottom: usize) -> Stack { pub(super) fn new(top: usize, bottom: usize) -> Stack {
assert!(top > bottom); assert!(top > bottom);
Stack { Stack {
top: top, top: top,
@ -77,3 +77,9 @@ impl Stack {
self.bottom self.bottom
} }
} }
impl Drop for Stack {
fn drop(&mut self) {
panic!("stack leak: {:#x?}", self);
}
}

@ -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<PageTable>,
status: Status,
context: Context,
}
#[derive(Debug)]
struct Context {
}
#[derive(Debug)]
enum Status {
Uninit, Ready, Running, Sleeping(usize), Exited
}
struct Processor {
procs: BTreeMap<Pid, Box<Process>>,
}
type Pid = usize;
impl Processor {
fn new(kernel_stack: Stack) -> Self {
let mut processor = Processor {
procs: BTreeMap::<Pid, Box<Process>>::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) {
}
}
Loading…
Cancel
Save