PageDirectory.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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/RefCounted.h>
  9. #include <AK/RefPtr.h>
  10. #include <Kernel/Forward.h>
  11. #include <Kernel/Memory/PhysicalPage.h>
  12. #include <Kernel/Memory/VirtualRangeAllocator.h>
  13. namespace Kernel::Memory {
  14. class PageDirectory : public RefCounted<PageDirectory> {
  15. friend class MemoryManager;
  16. public:
  17. static RefPtr<PageDirectory> try_create_for_userspace(VirtualRangeAllocator const* parent_range_allocator = nullptr);
  18. static NonnullRefPtr<PageDirectory> must_create_kernel_page_directory();
  19. static RefPtr<PageDirectory> find_by_cr3(FlatPtr);
  20. ~PageDirectory();
  21. void allocate_kernel_directory();
  22. FlatPtr cr3() const
  23. {
  24. #if ARCH(X86_64)
  25. return m_pml4t->paddr().get();
  26. #else
  27. return m_directory_table->paddr().get();
  28. #endif
  29. }
  30. VirtualRangeAllocator& range_allocator() { return m_range_allocator; }
  31. VirtualRangeAllocator const& range_allocator() const { return m_range_allocator; }
  32. AddressSpace* address_space() { return m_space; }
  33. const AddressSpace* address_space() const { return m_space; }
  34. void set_space(Badge<AddressSpace>, AddressSpace& space) { m_space = &space; }
  35. RecursiveSpinlock& get_lock() { return m_lock; }
  36. private:
  37. PageDirectory();
  38. AddressSpace* m_space { nullptr };
  39. VirtualRangeAllocator m_range_allocator;
  40. #if ARCH(X86_64)
  41. RefPtr<PhysicalPage> m_pml4t;
  42. #endif
  43. RefPtr<PhysicalPage> m_directory_table;
  44. #if ARCH(X86_64)
  45. RefPtr<PhysicalPage> m_directory_pages[512];
  46. #else
  47. RefPtr<PhysicalPage> m_directory_pages[4];
  48. #endif
  49. HashMap<FlatPtr, NonnullRefPtr<PhysicalPage>> m_page_tables;
  50. RecursiveSpinlock m_lock;
  51. };
  52. }