page_table.h 4.4 KB

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