From 33e9f0b34f9dc35a47757137a29605e51052a26e Mon Sep 17 00:00:00 2001 From: MemoryShore <1353318529@qq.com> Date: Mon, 27 May 2024 23:57:51 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dmmap=E6=9C=AA=E5=BB=B6?= =?UTF-8?q?=E8=BF=9F=E5=88=86=E9=85=8D=E5=86=85=E5=AD=98=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/src/arch/x86_64/interrupt/trap.rs | 2 +- kernel/src/mm/page.rs | 5 ++++ kernel/src/mm/syscall.rs | 2 +- kernel/src/mm/ucontext.rs | 30 +++++++++++------------- 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/kernel/src/arch/x86_64/interrupt/trap.rs b/kernel/src/arch/x86_64/interrupt/trap.rs index ecc03f8d3..9cd31fa9b 100644 --- a/kernel/src/arch/x86_64/interrupt/trap.rs +++ b/kernel/src/arch/x86_64/interrupt/trap.rs @@ -401,7 +401,7 @@ unsafe extern "C" fn do_page_fault(regs: &'static TrapFrame, error_code: u64) { // panic!("Page Fault"); CurrentIrqArch::interrupt_disable(); let address = x86::controlregs::cr2(); - // crate::info!( + // log::info!( // "fault address: {:#x}, error_code: {:#b}, pid: {}\n", // address, // error_code, diff --git a/kernel/src/mm/page.rs b/kernel/src/mm/page.rs index eb75c3eec..117708111 100644 --- a/kernel/src/mm/page.rs +++ b/kernel/src/mm/page.rs @@ -861,6 +861,11 @@ impl PageMapper { let phys: PhysAddr = self.frame_allocator.allocate_one()?; compiler_fence(Ordering::SeqCst); + unsafe { + let vaddr = MMArch::phys_2_virt(phys).unwrap(); + MMArch::write_bytes(vaddr, 0, MMArch::PAGE_SIZE); + } + let mut page_manager_guard: SpinLockGuard<'static, PageManager> = page_manager_lock_irqsave(); if !page_manager_guard.contains(&phys) { diff --git a/kernel/src/mm/syscall.rs b/kernel/src/mm/syscall.rs index 16bca4d33..beac5cc23 100644 --- a/kernel/src/mm/syscall.rs +++ b/kernel/src/mm/syscall.rs @@ -329,7 +329,7 @@ impl Syscall { prot_flags, map_flags, true, - true, + false, )?; return Ok(start_page.virt_address().data()); } diff --git a/kernel/src/mm/ucontext.rs b/kernel/src/mm/ucontext.rs index 1919ddeef..f5d484bc4 100644 --- a/kernel/src/mm/ucontext.rs +++ b/kernel/src/mm/ucontext.rs @@ -1116,7 +1116,7 @@ impl LockedVMA { let before: Option> = guard.region.before(®ion).map(|virt_region| { let mut vma: VMA = unsafe { guard.clone() }; vma.region = virt_region; - + vma.mapped = false; let vma: Arc = LockedVMA::new(vma); vma }); @@ -1124,7 +1124,7 @@ impl LockedVMA { let after: Option> = guard.region.after(®ion).map(|virt_region| { let mut vma: VMA = unsafe { guard.clone() }; vma.region = virt_region; - + vma.mapped = false; let vma: Arc = LockedVMA::new(vma); vma }); @@ -1134,20 +1134,24 @@ impl LockedVMA { if let Some(before) = before.clone() { let virt_iter = before.lock().region.iter_pages(); for frame in virt_iter { - let paddr = utable.translate(frame.virt_address()).unwrap().0; - let page = page_manager_guard.get_mut(&paddr); - page.insert_vma(before.clone()); - page.remove_vma(self); + if let Some((paddr, _)) = utable.translate(frame.virt_address()) { + let page = page_manager_guard.get_mut(&paddr); + page.insert_vma(before.clone()); + page.remove_vma(self); + before.lock().mapped = true; + } } } if let Some(after) = after.clone() { let virt_iter = after.lock().region.iter_pages(); for frame in virt_iter { - let paddr = utable.translate(frame.virt_address()).unwrap().0; - let page = page_manager_guard.get_mut(&paddr); - page.insert_vma(after.clone()); - page.remove_vma(self); + if let Some((paddr, _)) = utable.translate(frame.virt_address()) { + let page = page_manager_guard.get_mut(&paddr); + page.insert_vma(after.clone()); + page.remove_vma(self); + after.lock().mapped = true; + } } } @@ -1490,12 +1494,6 @@ impl VMA { // 将VMA加入到anon_vma let page = page_manager_guard.get_mut(&paddr); page.insert_vma(r.clone()); - - // 清空内存 - unsafe { - let vaddr = MMArch::phys_2_virt(paddr).unwrap(); - MMArch::write_bytes(vaddr, 0, MMArch::PAGE_SIZE); - } } // debug!("VMA::zeroed: done"); return Ok(r);