From 6a537ceef19b30ece416e4b159a55df85a8cc51e Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 25 Jul 2021 18:37:11 +0200 Subject: [PATCH] Kernel: Remove ContiguousVMObject, let AnonymousVMObject do the job We don't need an entirely separate VMObject subclass to influence the location of the physical pages. Instead, we simply allocate enough physically contiguous memory first, and then pass it to the AnonymousVMObject constructor that takes a span of physical pages. --- Kernel/Bus/USB/UHCIController.cpp | 6 ++--- Kernel/Bus/USB/UHCIController.h | 2 +- Kernel/Bus/USB/USBTransfer.cpp | 4 +-- Kernel/Bus/USB/USBTransfer.h | 4 +-- Kernel/CMakeLists.txt | 1 - Kernel/VM/AnonymousVMObject.cpp | 8 ++++++ Kernel/VM/AnonymousVMObject.h | 1 + Kernel/VM/ContiguousVMObject.cpp | 44 ------------------------------- Kernel/VM/ContiguousVMObject.h | 34 ------------------------ Kernel/VM/MemoryManager.cpp | 3 +-- 10 files changed, 18 insertions(+), 89 deletions(-) delete mode 100644 Kernel/VM/ContiguousVMObject.cpp delete mode 100644 Kernel/VM/ContiguousVMObject.h diff --git a/Kernel/Bus/USB/UHCIController.cpp b/Kernel/Bus/USB/UHCIController.cpp index 86dac5e9c6..a0564103cd 100644 --- a/Kernel/Bus/USB/UHCIController.cpp +++ b/Kernel/Bus/USB/UHCIController.cpp @@ -289,7 +289,7 @@ void UHCIController::reset() } // Let's allocate the physical page for the Frame List (which is 4KiB aligned) - auto framelist_vmobj = ContiguousVMObject::try_create_with_size(PAGE_SIZE); + auto framelist_vmobj = AnonymousVMObject::try_create_physically_contiguous_with_size(PAGE_SIZE); m_framelist = MemoryManager::the().allocate_kernel_region_with_vmobject(*framelist_vmobj, PAGE_SIZE, "UHCI Framelist", Region::Access::Write); dbgln("UHCI: Allocated framelist at physical address {}", m_framelist->physical_page(0)->paddr()); dbgln("UHCI: Framelist is at virtual address {}", m_framelist->vaddr()); @@ -311,7 +311,7 @@ UNMAP_AFTER_INIT void UHCIController::create_structures() { // Let's allocate memory for both the QH and TD pools // First the QH pool and all of the Interrupt QH's - auto qh_pool_vmobject = ContiguousVMObject::try_create_with_size(2 * PAGE_SIZE); + auto qh_pool_vmobject = AnonymousVMObject::try_create_physically_contiguous_with_size(2 * PAGE_SIZE); m_qh_pool = MemoryManager::the().allocate_kernel_region_with_vmobject(*qh_pool_vmobject, 2 * PAGE_SIZE, "UHCI Queue Head Pool", Region::Access::Write); memset(m_qh_pool->vaddr().as_ptr(), 0, 2 * PAGE_SIZE); // Zero out both pages @@ -331,7 +331,7 @@ UNMAP_AFTER_INIT void UHCIController::create_structures() m_dummy_qh = allocate_queue_head(); // Now the Transfer Descriptor pool - auto td_pool_vmobject = ContiguousVMObject::try_create_with_size(2 * PAGE_SIZE); + auto td_pool_vmobject = AnonymousVMObject::try_create_physically_contiguous_with_size(2 * PAGE_SIZE); m_td_pool = MemoryManager::the().allocate_kernel_region_with_vmobject(*td_pool_vmobject, 2 * PAGE_SIZE, "UHCI Transfer Descriptor Pool", Region::Access::Write); memset(m_td_pool->vaddr().as_ptr(), 0, 2 * PAGE_SIZE); diff --git a/Kernel/Bus/USB/UHCIController.h b/Kernel/Bus/USB/UHCIController.h index a25da43357..82056ded9c 100644 --- a/Kernel/Bus/USB/UHCIController.h +++ b/Kernel/Bus/USB/UHCIController.h @@ -17,7 +17,7 @@ #include #include #include -#include +#include namespace Kernel::USB { diff --git a/Kernel/Bus/USB/USBTransfer.cpp b/Kernel/Bus/USB/USBTransfer.cpp index 34f4b64333..f0504010f5 100644 --- a/Kernel/Bus/USB/USBTransfer.cpp +++ b/Kernel/Bus/USB/USBTransfer.cpp @@ -11,14 +11,14 @@ namespace Kernel::USB { RefPtr Transfer::try_create(Pipe& pipe, u16 len) { - auto vmobject = ContiguousVMObject::try_create_with_size(PAGE_SIZE); + auto vmobject = AnonymousVMObject::try_create_physically_contiguous_with_size(PAGE_SIZE); if (!vmobject) return nullptr; return AK::try_create(pipe, len, *vmobject); } -Transfer::Transfer(Pipe& pipe, u16 len, ContiguousVMObject& vmobject) +Transfer::Transfer(Pipe& pipe, u16 len, AnonymousVMObject& vmobject) : m_pipe(pipe) , m_transfer_data_size(len) { diff --git a/Kernel/Bus/USB/USBTransfer.h b/Kernel/Bus/USB/USBTransfer.h index b54d3a73b3..5eca252aa8 100644 --- a/Kernel/Bus/USB/USBTransfer.h +++ b/Kernel/Bus/USB/USBTransfer.h @@ -10,7 +10,7 @@ #include #include #include -#include +#include #include #include @@ -23,7 +23,7 @@ public: public: Transfer() = delete; - Transfer(Pipe& pipe, u16 len, ContiguousVMObject&); + Transfer(Pipe& pipe, u16 len, AnonymousVMObject&); ~Transfer(); void set_setup_packet(const USBRequestData& request); diff --git a/Kernel/CMakeLists.txt b/Kernel/CMakeLists.txt index 2efc36336a..9f4a4e96a8 100644 --- a/Kernel/CMakeLists.txt +++ b/Kernel/CMakeLists.txt @@ -255,7 +255,6 @@ set(KERNEL_SOURCES VirtIO/VirtIOQueue.cpp VirtIO/VirtIORNG.cpp VM/AnonymousVMObject.cpp - VM/ContiguousVMObject.cpp VM/InodeVMObject.cpp VM/MemoryManager.cpp VM/PageDirectory.cpp diff --git a/Kernel/VM/AnonymousVMObject.cpp b/Kernel/VM/AnonymousVMObject.cpp index 023ffbe23c..1f7356d6eb 100644 --- a/Kernel/VM/AnonymousVMObject.cpp +++ b/Kernel/VM/AnonymousVMObject.cpp @@ -70,6 +70,14 @@ RefPtr AnonymousVMObject::try_create_with_size(size_t size, A return adopt_ref_if_nonnull(new (nothrow) AnonymousVMObject(size, commit)); } +RefPtr AnonymousVMObject::try_create_physically_contiguous_with_size(size_t size) +{ + auto contiguous_physical_pages = MM.allocate_contiguous_supervisor_physical_pages(size); + if (contiguous_physical_pages.is_empty()) + return {}; + return adopt_ref_if_nonnull(new (nothrow) AnonymousVMObject(contiguous_physical_pages.span())); +} + RefPtr AnonymousVMObject::try_create_purgeable_with_size(size_t size, AllocationStrategy commit) { if (commit == AllocationStrategy::Reserve || commit == AllocationStrategy::AllocateNow) { diff --git a/Kernel/VM/AnonymousVMObject.h b/Kernel/VM/AnonymousVMObject.h index f238c52e2d..6388918218 100644 --- a/Kernel/VM/AnonymousVMObject.h +++ b/Kernel/VM/AnonymousVMObject.h @@ -38,6 +38,7 @@ public: static RefPtr try_create_for_physical_range(PhysicalAddress paddr, size_t size); static RefPtr try_create_with_physical_pages(Span>); static RefPtr try_create_purgeable_with_size(size_t, AllocationStrategy); + static RefPtr try_create_physically_contiguous_with_size(size_t); virtual RefPtr try_clone() override; [[nodiscard]] NonnullRefPtr allocate_committed_page(Badge); diff --git a/Kernel/VM/ContiguousVMObject.cpp b/Kernel/VM/ContiguousVMObject.cpp deleted file mode 100644 index a946a3658e..0000000000 --- a/Kernel/VM/ContiguousVMObject.cpp +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (c) 2020, Liav A. - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#include -#include -#include - -namespace Kernel { - -RefPtr ContiguousVMObject::try_create_with_size(size_t size) -{ - auto contiguous_physical_pages = MM.allocate_contiguous_supervisor_physical_pages(size); - if (contiguous_physical_pages.is_empty()) - return {}; - return adopt_ref_if_nonnull(new (nothrow) ContiguousVMObject(size, contiguous_physical_pages)); -} - -ContiguousVMObject::ContiguousVMObject(size_t size, NonnullRefPtrVector& contiguous_physical_pages) - : VMObject(size) -{ - for (size_t i = 0; i < page_count(); i++) { - physical_pages()[i] = contiguous_physical_pages[i]; - dbgln_if(CONTIGUOUS_VMOBJECT_DEBUG, "Contiguous page[{}]: {}", i, physical_pages()[i]->paddr()); - } -} - -ContiguousVMObject::ContiguousVMObject(ContiguousVMObject const& other) - : VMObject(other) -{ -} - -ContiguousVMObject::~ContiguousVMObject() -{ -} - -RefPtr ContiguousVMObject::try_clone() -{ - VERIFY_NOT_REACHED(); -} - -} diff --git a/Kernel/VM/ContiguousVMObject.h b/Kernel/VM/ContiguousVMObject.h deleted file mode 100644 index 76700f1c4b..0000000000 --- a/Kernel/VM/ContiguousVMObject.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2020, Liav A. - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#pragma once - -#include -#include -#include - -namespace Kernel { -class ContiguousVMObject final : public VMObject { -public: - virtual ~ContiguousVMObject() override; - - static RefPtr try_create_with_size(size_t); - -private: - explicit ContiguousVMObject(size_t, NonnullRefPtrVector&); - explicit ContiguousVMObject(ContiguousVMObject const&); - - virtual StringView class_name() const override { return "ContiguousVMObject"sv; } - virtual RefPtr try_clone() override; - - ContiguousVMObject& operator=(ContiguousVMObject const&) = delete; - ContiguousVMObject& operator=(ContiguousVMObject&&) = delete; - ContiguousVMObject(ContiguousVMObject&&) = delete; - - virtual bool is_contiguous() const override { return true; } -}; - -} diff --git a/Kernel/VM/MemoryManager.cpp b/Kernel/VM/MemoryManager.cpp index 182e5dc923..37690cd3a4 100644 --- a/Kernel/VM/MemoryManager.cpp +++ b/Kernel/VM/MemoryManager.cpp @@ -17,7 +17,6 @@ #include #include #include -#include #include #include #include @@ -705,7 +704,7 @@ OwnPtr MemoryManager::allocate_contiguous_kernel_region(size_t size, Str auto range = kernel_page_directory().range_allocator().allocate_anywhere(size); if (!range.has_value()) return {}; - auto vmobject = ContiguousVMObject::try_create_with_size(size); + auto vmobject = AnonymousVMObject::try_create_physically_contiguous_with_size(size); if (!vmobject) { kernel_page_directory().range_allocator().deallocate(range.value()); return {};