diff --git a/Kernel/VM/MemoryManager.cpp b/Kernel/VM/MemoryManager.cpp index 585310f81c0..9e8a47a8433 100644 --- a/Kernel/VM/MemoryManager.cpp +++ b/Kernel/VM/MemoryManager.cpp @@ -310,17 +310,14 @@ PageFaultResponse MemoryManager::handle_page_fault(const PageFault& fault) OwnPtr MemoryManager::allocate_kernel_region(size_t size, const StringView& name, u8 access, bool user_accessible, bool should_commit, bool cacheable) { - InterruptDisabler disabler; ASSERT(!(size % PAGE_SIZE)); auto range = kernel_page_directory().range_allocator().allocate_anywhere(size); - ASSERT(range.is_valid()); + if (!range.is_valid()) + return nullptr; auto vmobject = AnonymousVMObject::create_with_size(size); - OwnPtr region; - if (user_accessible) - region = Region::create_user_accessible(range, vmobject, 0, name, access, cacheable); - else - region = Region::create_kernel_only(range, vmobject, 0, name, access, cacheable); - region->map(kernel_page_directory()); + auto region = allocate_kernel_region_with_vmobject(range, vmobject, name, access, user_accessible, cacheable); + if (!region) + return nullptr; if (should_commit) region->commit(); return region; @@ -328,20 +325,14 @@ OwnPtr MemoryManager::allocate_kernel_region(size_t size, const StringVi OwnPtr MemoryManager::allocate_kernel_region(PhysicalAddress paddr, size_t size, const StringView& name, u8 access, bool user_accessible, bool cacheable) { - InterruptDisabler disabler; ASSERT(!(size % PAGE_SIZE)); auto range = kernel_page_directory().range_allocator().allocate_anywhere(size); - ASSERT(range.is_valid()); + if (!range.is_valid()) + return nullptr; auto vmobject = AnonymousVMObject::create_for_physical_range(paddr, size); if (!vmobject) return nullptr; - OwnPtr region; - if (user_accessible) - region = Region::create_user_accessible(range, vmobject.release_nonnull(), 0, name, access, cacheable); - else - region = Region::create_kernel_only(range, vmobject.release_nonnull(), 0, name, access, cacheable); - region->map(kernel_page_directory()); - return region; + return allocate_kernel_region_with_vmobject(range, *vmobject, name, access, user_accessible, cacheable); } OwnPtr MemoryManager::allocate_user_accessible_kernel_region(size_t size, const StringView& name, u8 access, bool cacheable) @@ -349,21 +340,28 @@ OwnPtr MemoryManager::allocate_user_accessible_kernel_region(size_t size return allocate_kernel_region(size, name, access, true, true, cacheable); } -OwnPtr MemoryManager::allocate_kernel_region_with_vmobject(VMObject& vmobject, size_t size, const StringView& name, u8 access, bool user_accessible, bool cacheable) +OwnPtr MemoryManager::allocate_kernel_region_with_vmobject(const Range& range, VMObject& vmobject, const StringView& name, u8 access, bool user_accessible, bool cacheable) { InterruptDisabler disabler; - ASSERT(!(size % PAGE_SIZE)); - auto range = kernel_page_directory().range_allocator().allocate_anywhere(size); - ASSERT(range.is_valid()); OwnPtr region; if (user_accessible) region = Region::create_user_accessible(range, vmobject, 0, name, access, cacheable); else region = Region::create_kernel_only(range, vmobject, 0, name, access, cacheable); - region->map(kernel_page_directory()); + if (region) + region->map(kernel_page_directory()); return region; } +OwnPtr MemoryManager::allocate_kernel_region_with_vmobject(VMObject& vmobject, size_t size, const StringView& name, u8 access, bool user_accessible, bool cacheable) +{ + ASSERT(!(size % PAGE_SIZE)); + auto range = kernel_page_directory().range_allocator().allocate_anywhere(size); + if (!range.is_valid()) + return nullptr; + return allocate_kernel_region_with_vmobject(range, vmobject, name, access, user_accessible, cacheable); +} + void MemoryManager::deallocate_user_physical_page(PhysicalPage&& page) { for (auto& region : m_user_physical_regions) { diff --git a/Kernel/VM/MemoryManager.h b/Kernel/VM/MemoryManager.h index 1c58d3aae03..0b7ca24f26f 100644 --- a/Kernel/VM/MemoryManager.h +++ b/Kernel/VM/MemoryManager.h @@ -106,6 +106,7 @@ public: OwnPtr allocate_kernel_region(size_t, const StringView& name, u8 access, bool user_accessible = false, bool should_commit = true, bool cacheable = true); OwnPtr allocate_kernel_region(PhysicalAddress, size_t, const StringView& name, u8 access, bool user_accessible = false, bool cacheable = false); OwnPtr allocate_kernel_region_with_vmobject(VMObject&, size_t, const StringView& name, u8 access, bool user_accessible = false, bool cacheable = false); + OwnPtr allocate_kernel_region_with_vmobject(const Range&, VMObject&, const StringView& name, u8 access, bool user_accessible = false, bool cacheable = false); OwnPtr allocate_user_accessible_kernel_region(size_t, const StringView& name, u8 access, bool cacheable = false); unsigned user_physical_pages() const { return m_user_physical_pages; }