AddressSpace.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Leon Albrecht <leon2002.la@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/RedBlackTree.h>
  9. #include <AK/Vector.h>
  10. #include <AK/WeakPtr.h>
  11. #include <Kernel/Memory/AllocationStrategy.h>
  12. #include <Kernel/Memory/PageDirectory.h>
  13. #include <Kernel/UnixTypes.h>
  14. namespace Kernel::Memory {
  15. class AddressSpace {
  16. public:
  17. static OwnPtr<AddressSpace> try_create(AddressSpace const* parent);
  18. ~AddressSpace();
  19. PageDirectory& page_directory() { return *m_page_directory; }
  20. const PageDirectory& page_directory() const { return *m_page_directory; }
  21. Region* add_region(NonnullOwnPtr<Region>);
  22. size_t region_count() const { return m_regions.size(); }
  23. RedBlackTree<FlatPtr, NonnullOwnPtr<Region>>& regions() { return m_regions; }
  24. const RedBlackTree<FlatPtr, NonnullOwnPtr<Region>>& regions() const { return m_regions; }
  25. void dump_regions();
  26. KResult unmap_mmap_range(VirtualAddress, size_t);
  27. Optional<VirtualRange> allocate_range(VirtualAddress, size_t, size_t alignment = PAGE_SIZE);
  28. KResultOr<Region*> allocate_region_with_vmobject(VirtualRange const&, NonnullRefPtr<VMObject>, size_t offset_in_vmobject, StringView name, int prot, bool shared);
  29. KResultOr<Region*> allocate_region(VirtualRange const&, StringView name, int prot = PROT_READ | PROT_WRITE, AllocationStrategy strategy = AllocationStrategy::Reserve);
  30. void deallocate_region(Region& region);
  31. NonnullOwnPtr<Region> take_region(Region& region);
  32. KResultOr<Region*> try_allocate_split_region(Region const& source_region, VirtualRange const&, size_t offset_in_vmobject);
  33. KResultOr<Vector<Region*, 2>> try_split_region_around_range(Region const& source_region, VirtualRange const&);
  34. Region* find_region_from_range(VirtualRange const&);
  35. Region* find_region_containing(VirtualRange const&);
  36. Vector<Region*> find_regions_intersecting(VirtualRange const&);
  37. bool enforces_syscall_regions() const { return m_enforces_syscall_regions; }
  38. void set_enforces_syscall_regions(bool b) { m_enforces_syscall_regions = b; }
  39. void remove_all_regions(Badge<Process>);
  40. RecursiveSpinlock& get_lock() const { return m_lock; }
  41. size_t amount_clean_inode() const;
  42. size_t amount_dirty_private() const;
  43. size_t amount_virtual() const;
  44. size_t amount_resident() const;
  45. size_t amount_shared() const;
  46. size_t amount_purgeable_volatile() const;
  47. size_t amount_purgeable_nonvolatile() const;
  48. private:
  49. explicit AddressSpace(NonnullRefPtr<PageDirectory>);
  50. mutable RecursiveSpinlock m_lock;
  51. RefPtr<PageDirectory> m_page_directory;
  52. RedBlackTree<FlatPtr, NonnullOwnPtr<Region>> m_regions;
  53. struct RegionLookupCache {
  54. Optional<VirtualRange> range;
  55. WeakPtr<Region> region;
  56. };
  57. RegionLookupCache m_region_lookup_cache;
  58. bool m_enforces_syscall_regions { false };
  59. };
  60. }