page_table.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. /// Number of bits reserved for attribute tagging.
  25. /// This can be at most the guaranteed alignment of the pointers in the page table.
  26. static constexpr int ATTRIBUTE_BITS = 2;
  27. /**
  28. * Pair of host pointer and page type attribute.
  29. * This uses the lower bits of a given pointer to store the attribute tag.
  30. * Writing and reading the pointer attribute pair is guaranteed to be atomic for the same method
  31. * call. In other words, they are guaranteed to be synchronized at all times.
  32. */
  33. class PageInfo {
  34. public:
  35. /// Returns the page pointer
  36. [[nodiscard]] u8* Pointer() const noexcept {
  37. return ExtractPointer(raw.load(std::memory_order_relaxed));
  38. }
  39. /// Returns the page type attribute
  40. [[nodiscard]] PageType Type() const noexcept {
  41. return ExtractType(raw.load(std::memory_order_relaxed));
  42. }
  43. /// Returns the page pointer and attribute pair, extracted from the same atomic read
  44. [[nodiscard]] std::pair<u8*, PageType> PointerType() const noexcept {
  45. const uintptr_t non_atomic_raw = raw.load(std::memory_order_relaxed);
  46. return {ExtractPointer(non_atomic_raw), ExtractType(non_atomic_raw)};
  47. }
  48. /// Returns the raw representation of the page information.
  49. /// Use ExtractPointer and ExtractType to unpack the value.
  50. [[nodiscard]] uintptr_t Raw() const noexcept {
  51. return raw.load(std::memory_order_relaxed);
  52. }
  53. /// Write a page pointer and type pair atomically
  54. void Store(u8* pointer, PageType type) noexcept {
  55. raw.store(reinterpret_cast<uintptr_t>(pointer) | static_cast<uintptr_t>(type));
  56. }
  57. /// Unpack a pointer from a page info raw representation
  58. [[nodiscard]] static u8* ExtractPointer(uintptr_t raw) noexcept {
  59. return reinterpret_cast<u8*>(raw & (~uintptr_t{0} << ATTRIBUTE_BITS));
  60. }
  61. /// Unpack a page type from a page info raw representation
  62. [[nodiscard]] static PageType ExtractType(uintptr_t raw) noexcept {
  63. return static_cast<PageType>(raw & ((uintptr_t{1} << ATTRIBUTE_BITS) - 1));
  64. }
  65. private:
  66. std::atomic<uintptr_t> raw;
  67. };
  68. PageTable();
  69. ~PageTable() noexcept;
  70. PageTable(const PageTable&) = delete;
  71. PageTable& operator=(const PageTable&) = delete;
  72. PageTable(PageTable&&) noexcept = default;
  73. PageTable& operator=(PageTable&&) noexcept = default;
  74. /**
  75. * Resizes the page table to be able to accommodate enough pages within
  76. * a given address space.
  77. *
  78. * @param address_space_width_in_bits The address size width in bits.
  79. * @param page_size_in_bits The page size in bits.
  80. */
  81. void Resize(size_t address_space_width_in_bits, size_t page_size_in_bits);
  82. size_t GetAddressSpaceBits() const {
  83. return current_address_space_width_in_bits;
  84. }
  85. /**
  86. * Vector of memory pointers backing each page. An entry can only be non-null if the
  87. * corresponding attribute element is of type `Memory`.
  88. */
  89. VirtualBuffer<PageInfo> pointers;
  90. VirtualBuffer<u64> backing_addr;
  91. size_t current_address_space_width_in_bits;
  92. u8* fastmem_arena;
  93. };
  94. } // namespace Common