diff --git a/src/arch/x86_64/paging/mapper.rs b/src/arch/x86_64/paging/mapper.rs index d164e56..76e0013 100644 --- a/src/arch/x86_64/paging/mapper.rs +++ b/src/arch/x86_64/paging/mapper.rs @@ -94,7 +94,7 @@ impl Mapper { self.map_to(page, frame, flags) } - pub fn unmap(&mut self, page: Page) + pub fn unmap(&mut self, page: Page) -> Frame { use x86_64::instructions::tlb; use x86_64::VirtualAddress; @@ -110,5 +110,6 @@ impl Mapper { p1[page.p1_index()].set_unused(); tlb::flush(VirtualAddress(page.start_address())); // TODO free p(1,2,3) table if empty + frame } } \ No newline at end of file diff --git a/src/arch/x86_64/paging/mod.rs b/src/arch/x86_64/paging/mod.rs index 5bd8bc8..773583d 100644 --- a/src/arch/x86_64/paging/mod.rs +++ b/src/arch/x86_64/paging/mod.rs @@ -185,6 +185,7 @@ impl InactivePageTable { impl Drop for InactivePageTable { fn drop(&mut self) { - warn!("PageTable dropped: {:?}", self); + info!("PageTable dropping: {:?}", self); + dealloc_frame(self.p4_frame.clone()); } } \ No newline at end of file diff --git a/src/arch/x86_64/paging/temporary_page.rs b/src/arch/x86_64/paging/temporary_page.rs index d1931fd..d3406f8 100644 --- a/src/arch/x86_64/paging/temporary_page.rs +++ b/src/arch/x86_64/paging/temporary_page.rs @@ -25,7 +25,7 @@ impl TemporaryPage { } /// Unmaps the temporary page in the active table. - pub fn unmap(&self, active_table: &mut ActivePageTable) { + pub fn unmap(&self, active_table: &mut ActivePageTable) -> Frame { active_table.unmap(self.page) } diff --git a/src/memory/memory_set.rs b/src/memory/memory_set.rs index 6e9a1c7..a47e80c 100644 --- a/src/memory/memory_set.rs +++ b/src/memory/memory_set.rs @@ -67,7 +67,7 @@ impl MemoryArea { Some(phys_start) => { for page in Page::range_of(self.start_addr, self.end_addr) { let frame = Frame::of_addr(phys_start.get() + page.start_address() - self.start_addr); - pt.map_to(page, frame.clone(), self.flags.0); + pt.map_to(page, frame, self.flags.0); } } None => { @@ -79,7 +79,10 @@ impl MemoryArea { } fn unmap(&self, pt: &mut Mapper) { for page in Page::range_of(self.start_addr, self.end_addr) { - pt.unmap(page); + let frame = pt.unmap(page); + if self.phys_start_addr.is_none() { + dealloc_frame(frame); + } } } } diff --git a/src/memory/mod.rs b/src/memory/mod.rs index 70c0026..f756907 100644 --- a/src/memory/mod.rs +++ b/src/memory/mod.rs @@ -22,8 +22,14 @@ lazy_static! { static STACK_ALLOCATOR: Mutex> = Mutex::new(None); pub fn alloc_frame() -> Frame { - FRAME_ALLOCATOR.lock() - .allocate_frame().expect("no more frame") + let frame = FRAME_ALLOCATOR.lock().allocate_frame().expect("no more frame"); + trace!("alloc: {:?}", frame); + frame +} + +pub fn dealloc_frame(frame: Frame) { + trace!("dealloc: {:?}", frame); + FRAME_ALLOCATOR.lock().deallocate_frame(frame); } fn alloc_stack(size_in_pages: usize) -> Stack {