page_table.h 4.5 KB

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