Skip to content

Commit

Permalink
修复mmap未延迟分配内存的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
MemoryShore committed May 27, 2024
1 parent 6cf1947 commit 33e9f0b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
2 changes: 1 addition & 1 deletion kernel/src/arch/x86_64/interrupt/trap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions kernel/src/mm/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,11 @@ impl<Arch: MemoryManagementArch, F: FrameAllocator> PageMapper<Arch, F> {
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) {
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/mm/syscall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ impl Syscall {
prot_flags,
map_flags,
true,
true,
false,
)?;
return Ok(start_page.virt_address().data());
}
Expand Down
30 changes: 14 additions & 16 deletions kernel/src/mm/ucontext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1116,15 +1116,15 @@ impl LockedVMA {
let before: Option<Arc<LockedVMA>> = guard.region.before(&region).map(|virt_region| {
let mut vma: VMA = unsafe { guard.clone() };
vma.region = virt_region;

vma.mapped = false;
let vma: Arc<LockedVMA> = LockedVMA::new(vma);
vma
});

let after: Option<Arc<LockedVMA>> = guard.region.after(&region).map(|virt_region| {
let mut vma: VMA = unsafe { guard.clone() };
vma.region = virt_region;

vma.mapped = false;
let vma: Arc<LockedVMA> = LockedVMA::new(vma);
vma
});
Expand All @@ -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;
}
}
}

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 33e9f0b

Please sign in to comment.