memory.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <cstddef>
  6. #include <string>
  7. #include <tuple>
  8. #include <vector>
  9. #include <boost/icl/interval_map.hpp>
  10. #include "common/common_types.h"
  11. #include "core/memory_hook.h"
  12. namespace Kernel {
  13. class Process;
  14. }
  15. namespace Memory {
  16. /**
  17. * Page size used by the ARM architecture. This is the smallest granularity with which memory can
  18. * be mapped.
  19. */
  20. constexpr std::size_t PAGE_BITS = 12;
  21. constexpr u64 PAGE_SIZE = 1ULL << PAGE_BITS;
  22. constexpr u64 PAGE_MASK = PAGE_SIZE - 1;
  23. enum class PageType : u8 {
  24. /// Page is unmapped and should cause an access error.
  25. Unmapped,
  26. /// Page is mapped to regular memory. This is the only type you can get pointers to.
  27. Memory,
  28. /// Page is mapped to regular memory, but also needs to check for rasterizer cache flushing and
  29. /// invalidation
  30. RasterizerCachedMemory,
  31. /// Page is mapped to a I/O region. Writing and reading to this page is handled by functions.
  32. Special,
  33. };
  34. struct SpecialRegion {
  35. enum class Type {
  36. DebugHook,
  37. IODevice,
  38. } type;
  39. MemoryHookPointer handler;
  40. bool operator<(const SpecialRegion& other) const {
  41. return std::tie(type, handler) < std::tie(other.type, other.handler);
  42. }
  43. bool operator==(const SpecialRegion& other) const {
  44. return std::tie(type, handler) == std::tie(other.type, other.handler);
  45. }
  46. };
  47. /**
  48. * A (reasonably) fast way of allowing switchable and remappable process address spaces. It loosely
  49. * mimics the way a real CPU page table works.
  50. */
  51. struct PageTable {
  52. explicit PageTable();
  53. explicit PageTable(std::size_t address_space_width_in_bits);
  54. ~PageTable();
  55. /**
  56. * Resizes the page table to be able to accomodate enough pages within
  57. * a given address space.
  58. *
  59. * @param address_space_width_in_bits The address size width in bits.
  60. */
  61. void Resize(std::size_t address_space_width_in_bits);
  62. /**
  63. * Vector of memory pointers backing each page. An entry can only be non-null if the
  64. * corresponding entry in the `attributes` vector is of type `Memory`.
  65. */
  66. std::vector<u8*> pointers;
  67. /**
  68. * Contains MMIO handlers that back memory regions whose entries in the `attribute` vector is
  69. * of type `Special`.
  70. */
  71. boost::icl::interval_map<VAddr, std::set<SpecialRegion>> special_regions;
  72. /**
  73. * Vector of fine grained page attributes. If it is set to any value other than `Memory`, then
  74. * the corresponding entry in `pointers` MUST be set to null.
  75. */
  76. std::vector<PageType> attributes;
  77. };
  78. /// Virtual user-space memory regions
  79. enum : VAddr {
  80. /// Read-only page containing kernel and system configuration values.
  81. CONFIG_MEMORY_VADDR = 0x1FF80000,
  82. CONFIG_MEMORY_SIZE = 0x00001000,
  83. CONFIG_MEMORY_VADDR_END = CONFIG_MEMORY_VADDR + CONFIG_MEMORY_SIZE,
  84. /// Usually read-only page containing mostly values read from hardware.
  85. SHARED_PAGE_VADDR = 0x1FF81000,
  86. SHARED_PAGE_SIZE = 0x00001000,
  87. SHARED_PAGE_VADDR_END = SHARED_PAGE_VADDR + SHARED_PAGE_SIZE,
  88. /// TLS (Thread-Local Storage) related.
  89. TLS_ENTRY_SIZE = 0x200,
  90. /// Application stack
  91. DEFAULT_STACK_SIZE = 0x100000,
  92. /// Kernel Virtual Address Range
  93. KERNEL_REGION_VADDR = 0xFFFFFF8000000000,
  94. KERNEL_REGION_SIZE = 0x7FFFE00000,
  95. KERNEL_REGION_END = KERNEL_REGION_VADDR + KERNEL_REGION_SIZE,
  96. };
  97. /// Currently active page table
  98. void SetCurrentPageTable(PageTable* page_table);
  99. PageTable* GetCurrentPageTable();
  100. /// Determines if the given VAddr is valid for the specified process.
  101. bool IsValidVirtualAddress(const Kernel::Process& process, VAddr vaddr);
  102. bool IsValidVirtualAddress(VAddr vaddr);
  103. /// Determines if the given VAddr is a kernel address
  104. bool IsKernelVirtualAddress(VAddr vaddr);
  105. u8 Read8(VAddr addr);
  106. u16 Read16(VAddr addr);
  107. u32 Read32(VAddr addr);
  108. u64 Read64(VAddr addr);
  109. void Write8(VAddr addr, u8 data);
  110. void Write16(VAddr addr, u16 data);
  111. void Write32(VAddr addr, u32 data);
  112. void Write64(VAddr addr, u64 data);
  113. void ReadBlock(const Kernel::Process& process, VAddr src_addr, void* dest_buffer, std::size_t size);
  114. void ReadBlock(VAddr src_addr, void* dest_buffer, std::size_t size);
  115. void WriteBlock(const Kernel::Process& process, VAddr dest_addr, const void* src_buffer,
  116. std::size_t size);
  117. void WriteBlock(VAddr dest_addr, const void* src_buffer, std::size_t size);
  118. void ZeroBlock(const Kernel::Process& process, VAddr dest_addr, std::size_t size);
  119. void CopyBlock(VAddr dest_addr, VAddr src_addr, std::size_t size);
  120. u8* GetPointer(VAddr vaddr);
  121. std::string ReadCString(VAddr vaddr, std::size_t max_length);
  122. enum class FlushMode {
  123. /// Write back modified surfaces to RAM
  124. Flush,
  125. /// Remove region from the cache
  126. Invalidate,
  127. /// Write back modified surfaces to RAM, and also remove them from the cache
  128. FlushAndInvalidate,
  129. };
  130. /**
  131. * Mark each page touching the region as cached.
  132. */
  133. void RasterizerMarkRegionCached(VAddr vaddr, u64 size, bool cached);
  134. /**
  135. * Flushes and invalidates any externally cached rasterizer resources touching the given virtual
  136. * address region.
  137. */
  138. void RasterizerFlushVirtualRegion(VAddr start, u64 size, FlushMode mode);
  139. } // namespace Memory