PageDirectory.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/HashMap.h>
  8. #include <AK/IntrusiveRedBlackTree.h>
  9. #include <AK/RefCounted.h>
  10. #include <AK/RefPtr.h>
  11. #include <Kernel/Forward.h>
  12. #include <Kernel/Memory/PhysicalPage.h>
  13. #include <Kernel/Memory/VirtualRangeAllocator.h>
  14. namespace Kernel::Memory {
  15. class PageDirectory : public RefCounted<PageDirectory> {
  16. friend class MemoryManager;
  17. public:
  18. static ErrorOr<NonnullRefPtr<PageDirectory>> try_create_for_userspace(VirtualRangeAllocator const* parent_range_allocator = nullptr);
  19. static NonnullRefPtr<PageDirectory> must_create_kernel_page_directory();
  20. static RefPtr<PageDirectory> find_by_cr3(FlatPtr);
  21. ~PageDirectory();
  22. void allocate_kernel_directory();
  23. FlatPtr cr3() const
  24. {
  25. #if ARCH(X86_64)
  26. return m_pml4t->paddr().get();
  27. #else
  28. return m_directory_table->paddr().get();
  29. #endif
  30. }
  31. VirtualRangeAllocator& range_allocator() { return m_range_allocator; }
  32. VirtualRangeAllocator const& range_allocator() const { return m_range_allocator; }
  33. AddressSpace* address_space() { return m_space; }
  34. const AddressSpace* address_space() const { return m_space; }
  35. void set_space(Badge<AddressSpace>, AddressSpace& space) { m_space = &space; }
  36. RecursiveSpinlock& get_lock() { return m_lock; }
  37. // This has to be public to let the global singleton access the member pointer
  38. IntrusiveRedBlackTreeNode<FlatPtr, PageDirectory, RawPtr<PageDirectory>> m_tree_node;
  39. private:
  40. PageDirectory();
  41. AddressSpace* m_space { nullptr };
  42. VirtualRangeAllocator m_range_allocator;
  43. #if ARCH(X86_64)
  44. RefPtr<PhysicalPage> m_pml4t;
  45. #endif
  46. RefPtr<PhysicalPage> m_directory_table;
  47. #if ARCH(X86_64)
  48. RefPtr<PhysicalPage> m_directory_pages[512];
  49. #else
  50. RefPtr<PhysicalPage> m_directory_pages[4];
  51. #endif
  52. HashMap<FlatPtr, NonnullRefPtr<PhysicalPage>> m_page_tables;
  53. RecursiveSpinlock m_lock;
  54. };
  55. }