page_table.h 4.4 KB

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