Kernel: Fix bugs in memory regions

MemoryRegion::initialize() required size to be page aligned, this is not
necessary.

MemoryBackedRegion::copy_data_to_region() required user-write
permissions for underlying data. This did not matter as memory backed
regions dont support COW memory, but it could lead to bugs later on.
This commit is contained in:
Bananymous 2024-09-15 23:18:05 +03:00
parent 1a24d1839f
commit 54732edff4
2 changed files with 13 additions and 3 deletions

View File

@ -94,9 +94,19 @@ namespace Kernel
vaddr_t page_offset = write_vaddr % PAGE_SIZE;
size_t bytes = BAN::Math::min<size_t>(buffer_size - written, PAGE_SIZE - page_offset);
TRY(allocate_page_containing(write_vaddr, true));
paddr_t paddr = m_page_table.physical_address_of(write_vaddr & PAGE_ADDR_MASK);
if (paddr == 0)
{
if (!TRY(allocate_page_containing(write_vaddr, false)))
{
dwarnln("Could not allocate page for data copying");
return BAN::Error::from_errno(EFAULT);
}
paddr = m_page_table.physical_address_of(write_vaddr & PAGE_ADDR_MASK);
ASSERT(paddr);
}
PageTable::with_fast_page(m_page_table.physical_address_of(write_vaddr & PAGE_ADDR_MASK), [&] {
PageTable::with_fast_page(paddr, [&] {
memcpy(PageTable::fast_page_as_ptr(page_offset), (void*)(buffer + written), bytes);
});

View File

@ -23,7 +23,7 @@ namespace Kernel
m_vaddr = m_page_table.reserve_free_contiguous_pages(needed_pages, address_range.start);
if (m_vaddr == 0)
return BAN::Error::from_errno(ENOMEM);
if (m_vaddr + needed_pages * PAGE_SIZE > address_range.end)
if (m_vaddr + m_size > address_range.end)
return BAN::Error::from_errno(ENOMEM);
return {};
}