Merge unmap and unmap_and_free by using free on exit flag in mappings

This commit is contained in:
pjht 2024-08-17 15:25:01 -05:00
parent e805a2de0c
commit 3ef75dcfbc
Signed by: pjht
GPG Key ID: 7B5F6AFBEC7EE78E
2 changed files with 15 additions and 20 deletions

View File

@ -339,7 +339,7 @@ extern "C" fn syscall_handler() {
(ptr::with_exposed_provenance_mut::<u8>(usize(regs.rsi))) (ptr::with_exposed_provenance_mut::<u8>(usize(regs.rsi)))
.copy_from(page.start_address().as_mut_ptr::<u8>(), len); .copy_from(page.start_address().as_mut_ptr::<u8>(), len);
}); });
space.unmap_and_free(page, buffer_num_pages).unwrap(); space.unmap(page, buffer_num_pages).unwrap();
retval = 0; retval = 0;
} else { } else {
retval = 1; retval = 1;
@ -648,7 +648,7 @@ extern "C" fn syscall_handler() {
break 'call22; break 'call22;
}; };
retval = if regs.rcx == 0 { retval = if regs.rcx == 0 {
u64::from(ACTIVE_SPACE.lock().unmap_and_free(page, usize(regs.rsi)).is_err()) u64::from(ACTIVE_SPACE.lock().unmap(page, usize(regs.rsi)).is_err())
} else { } else {
TASKING.address_spaces_mut(|x| { TASKING.address_spaces_mut(|x| {
#[warn( #[warn(
@ -656,7 +656,7 @@ extern "C" fn syscall_handler() {
reason = "FIXME: The current address space should be usize::MAX as that is an invalid index, instead of 0." reason = "FIXME: The current address space should be usize::MAX as that is an invalid index, instead of 0."
)] )]
if let Some(space) = x.get_mut(usize(regs.rcx - 1)) { if let Some(space) = x.get_mut(usize(regs.rcx - 1)) {
u64::from(space.unmap_and_free(page, usize(regs.rsi)).is_err()) u64::from(space.unmap(page, usize(regs.rsi)).is_err())
} else { } else {
1 1
} }

View File

@ -643,18 +643,6 @@ impl AddressSpace {
} }
pub fn unmap(&mut self, page: Page, num_pages: usize) -> Result<(), PagingError> { pub fn unmap(&mut self, page: Page, num_pages: usize) -> Result<(), PagingError> {
self.check_request_valid(page, num_pages)?;
#[expect(
clippy::arithmetic_side_effects,
reason = "check_request_valid guarentees this won't overflow"
)]
for page in (PageRange { start: page, end: page + u64(num_pages) }) {
self.mapper.unmap(page)?.1.flush();
}
Ok(())
}
pub fn unmap_and_free(&mut self, page: Page, num_pages: usize) -> Result<(), PagingError> {
self.check_request_valid(page, num_pages)?; self.check_request_valid(page, num_pages)?;
#[expect( #[expect(
clippy::arithmetic_side_effects, clippy::arithmetic_side_effects,
@ -662,12 +650,19 @@ impl AddressSpace {
)] )]
for page in (PageRange { start: page, end: page + u64(num_pages) }) { for page in (PageRange { start: page, end: page + u64(num_pages) }) {
unsafe { unsafe {
let TranslateResult::Mapped { frame: _, offset: 0, flags } =
self.mapper.translate(page.start_address())
else {
continue;
};
let (frame, flush) = self.mapper.unmap(page)?; let (frame, flush) = self.mapper.unmap(page)?;
PHYSICAL_MEMORY.lock().deallocate_frame(frame); if flags.contains(PageTableFlags::BIT_9) {
PHYSICAL_MEMORY.lock().deallocate_frame(frame);
self.record_dealloc(4096);
}
flush.flush(); flush.flush();
} }
} }
self.record_dealloc(num_pages * 4096);
Ok(()) Ok(())
} }
@ -689,10 +684,10 @@ impl AddressSpace {
let TranslateResult::Mapped { frame: MappedFrame::Size4KiB(frame), offset: 0, flags } = let TranslateResult::Mapped { frame: MappedFrame::Size4KiB(frame), offset: 0, flags } =
self.mapper.translate(src_page.start_address()) self.mapper.translate(src_page.start_address())
else { else {
continue; return Err(PagingError::PageNotMapped);
}; };
unsafe { dest.map_to(dest_page, frame, 1, flags)? }; unsafe { dest.map_to(dest_page, frame, 1, flags)? };
self.unmap(src_page, 1)?; self.mapper.unmap(src_page)?.1.flush();
if flags.contains(PageTableFlags::BIT_9) { if flags.contains(PageTableFlags::BIT_9) {
self.record_dealloc(4096); self.record_dealloc(4096);
dest.record_alloc(4096); dest.record_alloc(4096);
@ -856,6 +851,6 @@ unsafe impl Allocator for ASpaceMutex {
The kernel doesn't use huge pages and the mapping must be valid to be returned from allocate, so unmap cannot fail. The kernel doesn't use huge pages and the mapping must be valid to be returned from allocate, so unmap cannot fail.
" "
)] )]
self.0.lock().unmap_and_free(start_page, length).unwrap(); self.0.lock().unmap(start_page, length).unwrap();
} }
} }