2022-04-02 19:12:05 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Error.h>
|
|
|
|
#include <AK/IntrusiveRedBlackTree.h>
|
2022-04-03 11:28:16 +00:00
|
|
|
#include <Kernel/Locking/Spinlock.h>
|
2022-04-02 19:12:05 +00:00
|
|
|
#include <Kernel/Memory/Region.h>
|
|
|
|
#include <Kernel/Memory/VirtualRange.h>
|
|
|
|
#include <Kernel/VirtualAddress.h>
|
|
|
|
|
|
|
|
namespace Kernel::Memory {
|
|
|
|
|
2022-04-03 16:46:27 +00:00
|
|
|
enum class RandomizeVirtualAddress {
|
|
|
|
No,
|
|
|
|
Yes,
|
|
|
|
};
|
|
|
|
|
2022-04-03 19:59:48 +00:00
|
|
|
// RegionTree represents a virtual address space.
|
|
|
|
// It is used by MemoryManager for kernel VM and by AddressSpace for user VM.
|
|
|
|
// Regions are stored in an intrusive data structure and there are no allocations when interacting with it.
|
2022-04-02 19:12:05 +00:00
|
|
|
class RegionTree {
|
|
|
|
AK_MAKE_NONCOPYABLE(RegionTree);
|
|
|
|
AK_MAKE_NONMOVABLE(RegionTree);
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit RegionTree(VirtualRange total_range)
|
|
|
|
: m_total_range(total_range)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~RegionTree();
|
|
|
|
|
|
|
|
auto& regions() { return m_regions; }
|
|
|
|
auto const& regions() const { return m_regions; }
|
|
|
|
|
|
|
|
VirtualRange total_range() const { return m_total_range; }
|
|
|
|
|
2022-04-03 11:28:16 +00:00
|
|
|
ErrorOr<NonnullOwnPtr<Region>> allocate_unbacked_anywhere(size_t size, size_t alignment = PAGE_SIZE);
|
|
|
|
|
2022-04-03 16:46:27 +00:00
|
|
|
ErrorOr<void> place_anywhere(Region&, RandomizeVirtualAddress, size_t size, size_t alignment = PAGE_SIZE);
|
2022-04-03 13:27:47 +00:00
|
|
|
ErrorOr<void> place_specifically(Region&, VirtualRange const&);
|
|
|
|
|
2022-04-03 15:12:39 +00:00
|
|
|
ErrorOr<NonnullOwnPtr<Memory::Region>> create_identity_mapped_region(PhysicalAddress, size_t);
|
|
|
|
|
2022-04-03 15:31:58 +00:00
|
|
|
void delete_all_regions_assuming_they_are_unmapped();
|
|
|
|
|
2022-04-04 23:07:23 +00:00
|
|
|
// FIXME: Access the region tree through a SpinlockProtected or similar.
|
|
|
|
RecursiveSpinlock& get_lock() const { return m_lock; }
|
|
|
|
|
2022-04-03 15:31:58 +00:00
|
|
|
private:
|
2022-04-03 19:56:16 +00:00
|
|
|
ErrorOr<VirtualRange> allocate_range_anywhere(size_t size, size_t alignment = PAGE_SIZE);
|
|
|
|
ErrorOr<VirtualRange> allocate_range_specific(VirtualAddress base, size_t size);
|
|
|
|
ErrorOr<VirtualRange> allocate_range_randomized(size_t size, size_t alignment = PAGE_SIZE);
|
2022-04-02 19:12:05 +00:00
|
|
|
|
2022-04-04 23:07:23 +00:00
|
|
|
RecursiveSpinlock mutable m_lock;
|
2022-04-03 11:28:16 +00:00
|
|
|
|
2022-04-02 19:12:05 +00:00
|
|
|
IntrusiveRedBlackTree<&Region::m_tree_node> m_regions;
|
|
|
|
VirtualRange const m_total_range;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|