Kernel: Simplify the way PhysicalRegions are constructed

Instead of creating a PhysicalRegion and then expanding it over and
over as we traverse the memory map on boot, we now compute the final
size of the contiguous physical range up front, and *then* create a
PhysicalRegion object.
This commit is contained in:
Andreas Kling 2021-07-13 18:22:49 +02:00
parent 479df315d2
commit 5171249540
Notes: sideshowbarker 2024-07-18 09:07:23 +09:00
3 changed files with 17 additions and 18 deletions

View File

@ -174,8 +174,6 @@ bool MemoryManager::is_allowed_to_mmap_to_userspace(PhysicalAddress start_addres
UNMAP_AFTER_INIT void MemoryManager::parse_memory_map()
{
PhysicalRegion* physical_region { nullptr };
// Register used memory regions that we know of.
m_used_memory_ranges.ensure_capacity(4);
m_used_memory_ranges.append(UsedMemoryRange { UsedMemoryRangeType::LowMemory, PhysicalAddress(0x00000000), PhysicalAddress(1 * MiB) });
@ -193,6 +191,13 @@ UNMAP_AFTER_INIT void MemoryManager::parse_memory_map()
auto* mmap_begin = reinterpret_cast<multiboot_memory_map_t*>(low_physical_to_virtual(multiboot_info_ptr->mmap_addr));
auto* mmap_end = reinterpret_cast<multiboot_memory_map_t*>(low_physical_to_virtual(multiboot_info_ptr->mmap_addr) + multiboot_info_ptr->mmap_length);
struct ContiguousPhysicalRange {
PhysicalAddress lower;
PhysicalAddress upper;
};
Vector<ContiguousPhysicalRange> contiguous_physical_ranges;
for (auto* mmap = mmap_begin; mmap < mmap_end; mmap++) {
dmesgln("MM: Multiboot mmap: address={:p}, length={}, type={}", mmap->addr, mmap->len, mmap->type);
@ -255,16 +260,21 @@ UNMAP_AFTER_INIT void MemoryManager::parse_memory_map()
if (should_skip)
continue;
// Assign page to user physical physical_region.
if (!physical_region || physical_region->upper().offset(PAGE_SIZE) != addr) {
m_user_physical_regions.append(PhysicalRegion::try_create(addr, addr).release_nonnull());
physical_region = &m_user_physical_regions.last();
if (contiguous_physical_ranges.is_empty() || contiguous_physical_ranges.last().upper.offset(PAGE_SIZE) != addr) {
contiguous_physical_ranges.append(ContiguousPhysicalRange {
.lower = addr,
.upper = addr,
});
} else {
physical_region->expand(physical_region->lower(), addr);
contiguous_physical_ranges.last().upper = addr;
}
}
}
for (auto& range : contiguous_physical_ranges) {
m_user_physical_regions.append(PhysicalRegion::try_create(range.lower, range.upper).release_nonnull());
}
// Append statically-allocated super physical physical_region.
m_super_physical_regions.append(PhysicalRegion::try_create(
PhysicalAddress(virtual_to_low_physical(FlatPtr(super_pages))),

View File

@ -36,14 +36,6 @@ PhysicalRegion::PhysicalRegion(PhysicalAddress lower, PhysicalAddress upper)
{
}
void PhysicalRegion::expand(PhysicalAddress lower, PhysicalAddress upper)
{
VERIFY(!m_pages);
m_lower = lower;
m_upper = upper;
}
void PhysicalRegion::initialize_zones()
{
size_t remaining_pages = m_pages;
@ -65,9 +57,7 @@ void PhysicalRegion::initialize_zones()
unsigned PhysicalRegion::finalize_capacity()
{
VERIFY(!m_pages);
m_pages = (m_upper.get() - m_lower.get()) / PAGE_SIZE;
return size();
}

View File

@ -25,7 +25,6 @@ public:
~PhysicalRegion();
void expand(PhysicalAddress lower, PhysicalAddress upper);
unsigned finalize_capacity();
void initialize_zones();