page_table.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // SPDX-FileCopyrightText: Copyright 2019 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <atomic>
  5. #include "common/common_types.h"
  6. #include "common/typed_address.h"
  7. #include "common/virtual_buffer.h"
  8. namespace Common {
  9. enum class PageType : u8 {
  10. /// Page is unmapped and should cause an access error.
  11. Unmapped,
  12. /// Page is mapped to regular memory. This is the only type you can get pointers to.
  13. Memory,
  14. /// Page is mapped to regular memory, but inaccessible from CPU fastmem and must use
  15. /// the callbacks.
  16. DebugMemory,
  17. /// Page is mapped to regular memory, but also needs to check for rasterizer cache flushing and
  18. /// invalidation
  19. RasterizerCachedMemory,
  20. };
  21. /**
  22. * A (reasonably) fast way of allowing switchable and remappable process address spaces. It loosely
  23. * mimics the way a real CPU page table works.
  24. */
  25. struct PageTable {
  26. struct TraversalEntry {
  27. u64 phys_addr{};
  28. std::size_t block_size{};
  29. };
  30. struct TraversalContext {
  31. u64 next_page{};
  32. u64 next_offset{};
  33. };
  34. /// Number of bits reserved for attribute tagging.
  35. /// This can be at most the guaranteed alignment of the pointers in the page table.
  36. static constexpr int ATTRIBUTE_BITS = 2;
  37. /**
  38. * Pair of host pointer and page type attribute.
  39. * This uses the lower bits of a given pointer to store the attribute tag.
  40. * Writing and reading the pointer attribute pair is guaranteed to be atomic for the same method
  41. * call. In other words, they are guaranteed to be synchronized at all times.
  42. */
  43. class PageInfo {
  44. public:
  45. /// Returns the page pointer
  46. [[nodiscard]] uintptr_t Pointer() const noexcept {
  47. return ExtractPointer(raw.load(std::memory_order_relaxed));
  48. }
  49. /// Returns the page type attribute
  50. [[nodiscard]] PageType Type() const noexcept {
  51. return ExtractType(raw.load(std::memory_order_relaxed));
  52. }
  53. /// Returns the page pointer and attribute pair, extracted from the same atomic read
  54. [[nodiscard]] std::pair<uintptr_t, PageType> PointerType() const noexcept {
  55. const uintptr_t non_atomic_raw = raw.load(std::memory_order_relaxed);
  56. return {ExtractPointer(non_atomic_raw), ExtractType(non_atomic_raw)};
  57. }
  58. /// Returns the raw representation of the page information.
  59. /// Use ExtractPointer and ExtractType to unpack the value.
  60. [[nodiscard]] uintptr_t Raw() const noexcept {
  61. return raw.load(std::memory_order_relaxed);
  62. }
  63. /// Write a page pointer and type pair atomically
  64. void Store(uintptr_t pointer, PageType type) noexcept {
  65. raw.store(pointer | static_cast<uintptr_t>(type));
  66. }
  67. /// Unpack a pointer from a page info raw representation
  68. [[nodiscard]] static uintptr_t ExtractPointer(uintptr_t raw) noexcept {
  69. return raw & (~uintptr_t{0} << ATTRIBUTE_BITS);
  70. }
  71. /// Unpack a page type from a page info raw representation
  72. [[nodiscard]] static PageType ExtractType(uintptr_t raw) noexcept {
  73. return static_cast<PageType>(raw & ((uintptr_t{1} << ATTRIBUTE_BITS) - 1));
  74. }
  75. private:
  76. std::atomic<uintptr_t> raw;
  77. };
  78. PageTable();
  79. ~PageTable() noexcept;
  80. PageTable(const PageTable&) = delete;
  81. PageTable& operator=(const PageTable&) = delete;
  82. PageTable(PageTable&&) noexcept = default;
  83. PageTable& operator=(PageTable&&) noexcept = default;
  84. bool BeginTraversal(TraversalEntry* out_entry, TraversalContext* out_context,
  85. Common::ProcessAddress address) const;
  86. bool ContinueTraversal(TraversalEntry* out_entry, TraversalContext* context) const;
  87. /**
  88. * Resizes the page table to be able to accommodate enough pages within
  89. * a given address space.
  90. *
  91. * @param address_space_width_in_bits The address size width in bits.
  92. * @param page_size_in_bits The page size in bits.
  93. */
  94. void Resize(std::size_t address_space_width_in_bits, std::size_t page_size_in_bits);
  95. std::size_t GetAddressSpaceBits() const {
  96. return current_address_space_width_in_bits;
  97. }
  98. bool GetPhysicalAddress(Common::PhysicalAddress* out_phys_addr,
  99. Common::ProcessAddress virt_addr) const {
  100. if (virt_addr > (1ULL << this->GetAddressSpaceBits())) {
  101. return false;
  102. }
  103. *out_phys_addr = backing_addr[virt_addr / page_size] + GetInteger(virt_addr);
  104. return true;
  105. }
  106. /**
  107. * Vector of memory pointers backing each page. An entry can only be non-null if the
  108. * corresponding attribute element is of type `Memory`.
  109. */
  110. VirtualBuffer<PageInfo> pointers;
  111. VirtualBuffer<u64> blocks;
  112. VirtualBuffer<u64> backing_addr;
  113. std::size_t current_address_space_width_in_bits{};
  114. u8* fastmem_arena{};
  115. std::size_t page_size{};
  116. };
  117. } // namespace Common