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>
|
2021-02-08 14:45:40 +00:00
|
|
|
#include <AK/WeakPtr.h>
|
2021-08-06 08:45:34 +00:00
|
|
|
#include <Kernel/Memory/AllocationStrategy.h>
|
|
|
|
#include <Kernel/Memory/PageDirectory.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-09-05 13:13:20 +00:00
|
|
|
static KResultOr<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; }
|
|
|
|
const PageDirectory& page_directory() const { return *m_page_directory; }
|
|
|
|
|
2021-07-14 22:18:00 +00:00
|
|
|
Region* add_region(NonnullOwnPtr<Region>);
|
2021-02-08 14:45:40 +00:00
|
|
|
|
|
|
|
size_t region_count() const { return m_regions.size(); }
|
|
|
|
|
2021-04-06 23:20:29 +00:00
|
|
|
RedBlackTree<FlatPtr, NonnullOwnPtr<Region>>& regions() { return m_regions; }
|
|
|
|
const RedBlackTree<FlatPtr, NonnullOwnPtr<Region>>& regions() const { return m_regions; }
|
2021-02-08 14:45:40 +00:00
|
|
|
|
|
|
|
void dump_regions();
|
|
|
|
|
2021-05-28 09:03:21 +00:00
|
|
|
KResult unmap_mmap_range(VirtualAddress, size_t);
|
|
|
|
|
2021-08-06 11:54:48 +00:00
|
|
|
Optional<VirtualRange> allocate_range(VirtualAddress, size_t, size_t alignment = PAGE_SIZE);
|
2021-02-08 14:45:40 +00:00
|
|
|
|
2021-08-06 11:54:48 +00:00
|
|
|
KResultOr<Region*> allocate_region_with_vmobject(VirtualRange const&, NonnullRefPtr<VMObject>, size_t offset_in_vmobject, StringView name, int prot, bool shared);
|
|
|
|
KResultOr<Region*> allocate_region(VirtualRange const&, 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-08-06 11:54:48 +00:00
|
|
|
KResultOr<Region*> try_allocate_split_region(Region const& source_region, VirtualRange const&, size_t offset_in_vmobject);
|
|
|
|
KResultOr<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
|
|
|
|
2021-08-06 11:54:48 +00:00
|
|
|
Vector<Region*> 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
|
|
|
|
2021-02-08 21:11:10 +00:00
|
|
|
size_t amount_clean_inode() const;
|
|
|
|
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;
|
|
|
|
|
2021-02-08 14:45:40 +00:00
|
|
|
private:
|
2021-08-07 19:32:30 +00:00
|
|
|
explicit AddressSpace(NonnullRefPtr<PageDirectory>);
|
2021-02-08 14:45:40 +00:00
|
|
|
|
2021-08-21 23:37:17 +00:00
|
|
|
mutable RecursiveSpinlock m_lock;
|
2021-02-08 14:45:40 +00:00
|
|
|
|
|
|
|
RefPtr<PageDirectory> m_page_directory;
|
|
|
|
|
2021-04-06 23:20:29 +00:00
|
|
|
RedBlackTree<FlatPtr, NonnullOwnPtr<Region>> m_regions;
|
2021-02-08 14:45:40 +00:00
|
|
|
|
|
|
|
struct RegionLookupCache {
|
2021-08-06 11:54:48 +00:00
|
|
|
Optional<VirtualRange> range;
|
2021-02-08 14:45:40 +00:00
|
|
|
WeakPtr<Region> region;
|
|
|
|
};
|
|
|
|
RegionLookupCache m_region_lookup_cache;
|
|
|
|
|
|
|
|
bool m_enforces_syscall_regions { false };
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|