2021-02-08 14:45:40 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2021-02-24 15:44:00 +00:00
|
|
|
* Copyright (c) 2021, Leon Albrecht <leon2002.la@gmail.com>
|
2021-02-08 14:45:40 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-02-08 14:45:40 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-04-06 23:20:29 +00:00
|
|
|
#include <AK/RedBlackTree.h>
|
2021-02-24 15:44:00 +00:00
|
|
|
#include <AK/Vector.h>
|
2022-08-19 18:53:40 +00:00
|
|
|
#include <Kernel/Library/LockWeakPtr.h>
|
2021-08-06 08:45:34 +00:00
|
|
|
#include <Kernel/Memory/AllocationStrategy.h>
|
|
|
|
#include <Kernel/Memory/PageDirectory.h>
|
2022-04-01 23:28:01 +00:00
|
|
|
#include <Kernel/Memory/Region.h>
|
2022-04-02 19:12:05 +00:00
|
|
|
#include <Kernel/Memory/RegionTree.h>
|
2021-02-08 14:45:40 +00:00
|
|
|
#include <Kernel/UnixTypes.h>
|
|
|
|
|
2021-08-06 11:49:36 +00:00
|
|
|
namespace Kernel::Memory {
|
2021-02-08 14:45:40 +00:00
|
|
|
|
2021-08-06 11:57:39 +00:00
|
|
|
class AddressSpace {
|
2021-02-08 14:45:40 +00:00
|
|
|
public:
|
2021-11-07 23:51:39 +00:00
|
|
|
static ErrorOr<NonnullOwnPtr<AddressSpace>> try_create(AddressSpace const* parent);
|
2021-08-06 11:57:39 +00:00
|
|
|
~AddressSpace();
|
2021-02-08 14:45:40 +00:00
|
|
|
|
|
|
|
PageDirectory& page_directory() { return *m_page_directory; }
|
2022-04-01 17:58:27 +00:00
|
|
|
PageDirectory const& page_directory() const { return *m_page_directory; }
|
2021-02-08 14:45:40 +00:00
|
|
|
|
2022-04-02 19:12:05 +00:00
|
|
|
auto& regions() { return m_region_tree.regions(); }
|
|
|
|
auto const& regions() const { return m_region_tree.regions(); }
|
2021-02-08 14:45:40 +00:00
|
|
|
|
|
|
|
void dump_regions();
|
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<void> unmap_mmap_range(VirtualAddress, size_t);
|
2021-05-28 09:03:21 +00:00
|
|
|
|
2022-08-19 18:53:40 +00:00
|
|
|
ErrorOr<Region*> allocate_region_with_vmobject(VirtualRange requested_range, NonnullLockRefPtr<VMObject>, size_t offset_in_vmobject, StringView name, int prot, bool shared);
|
|
|
|
ErrorOr<Region*> allocate_region_with_vmobject(RandomizeVirtualAddress, VirtualAddress requested_address, size_t requested_size, size_t requested_alignment, NonnullLockRefPtr<VMObject>, size_t offset_in_vmobject, StringView name, int prot, bool shared);
|
2022-04-03 16:46:27 +00:00
|
|
|
ErrorOr<Region*> allocate_region(RandomizeVirtualAddress, VirtualAddress requested_address, size_t requested_size, size_t requested_alignment, StringView name, int prot = PROT_READ | PROT_WRITE, AllocationStrategy strategy = AllocationStrategy::Reserve);
|
2021-07-17 13:07:02 +00:00
|
|
|
void deallocate_region(Region& region);
|
|
|
|
NonnullOwnPtr<Region> take_region(Region& region);
|
2021-02-08 14:45:40 +00:00
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<Region*> try_allocate_split_region(Region const& source_region, VirtualRange const&, size_t offset_in_vmobject);
|
|
|
|
ErrorOr<Vector<Region*, 2>> try_split_region_around_range(Region const& source_region, VirtualRange const&);
|
2021-02-08 14:45:40 +00:00
|
|
|
|
2021-08-06 11:54:48 +00:00
|
|
|
Region* find_region_from_range(VirtualRange const&);
|
|
|
|
Region* find_region_containing(VirtualRange const&);
|
2021-02-08 14:45:40 +00:00
|
|
|
|
2022-06-19 13:27:35 +00:00
|
|
|
ErrorOr<Vector<Region*, 4>> find_regions_intersecting(VirtualRange const&);
|
2021-02-24 15:44:00 +00:00
|
|
|
|
2021-02-08 14:45:40 +00:00
|
|
|
bool enforces_syscall_regions() const { return m_enforces_syscall_regions; }
|
|
|
|
void set_enforces_syscall_regions(bool b) { m_enforces_syscall_regions = b; }
|
|
|
|
|
|
|
|
void remove_all_regions(Badge<Process>);
|
|
|
|
|
2021-08-21 23:37:17 +00:00
|
|
|
RecursiveSpinlock& get_lock() const { return m_lock; }
|
2021-02-08 14:45:40 +00:00
|
|
|
|
2022-01-25 13:02:37 +00:00
|
|
|
ErrorOr<size_t> amount_clean_inode() const;
|
2021-02-08 21:11:10 +00:00
|
|
|
size_t amount_dirty_private() const;
|
|
|
|
size_t amount_virtual() const;
|
|
|
|
size_t amount_resident() const;
|
|
|
|
size_t amount_shared() const;
|
|
|
|
size_t amount_purgeable_volatile() const;
|
|
|
|
size_t amount_purgeable_nonvolatile() const;
|
|
|
|
|
2022-04-02 19:12:05 +00:00
|
|
|
auto& region_tree() { return m_region_tree; }
|
2022-04-02 18:01:29 +00:00
|
|
|
|
2021-02-08 14:45:40 +00:00
|
|
|
private:
|
2022-08-19 18:53:40 +00:00
|
|
|
AddressSpace(NonnullLockRefPtr<PageDirectory>, VirtualRange total_range);
|
2021-02-08 14:45:40 +00:00
|
|
|
|
2022-08-18 19:46:28 +00:00
|
|
|
mutable RecursiveSpinlock m_lock { LockRank::None };
|
2021-02-08 14:45:40 +00:00
|
|
|
|
2022-08-19 18:53:40 +00:00
|
|
|
LockRefPtr<PageDirectory> m_page_directory;
|
2021-02-08 14:45:40 +00:00
|
|
|
|
2022-04-02 19:12:05 +00:00
|
|
|
RegionTree m_region_tree;
|
2021-02-08 14:45:40 +00:00
|
|
|
|
|
|
|
bool m_enforces_syscall_regions { false };
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|