| 1 | // Copyright 2008 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #pragma once |
| 5 | |
| 6 | #include <cstddef> |
| 7 | #include <string_view> |
| 8 | #include <vector> |
| 9 | |
| 10 | #ifdef __APPLE__ |
| 11 | #include <mach/mach.h> |
| 12 | #endif |
| 13 | |
| 14 | #include "Common/CommonTypes.h" |
| 15 | #include "Common/DynamicLibrary.h" |
| 16 | |
| 17 | namespace Common |
| 18 | { |
| 19 | #ifdef _WIN32 |
| 20 | struct WindowsMemoryRegion; |
| 21 | |
| 22 | struct WindowsMemoryFunctions |
| 23 | { |
| 24 | Common::DynamicLibrary m_kernel32_handle; |
| 25 | Common::DynamicLibrary m_api_ms_win_core_memory_l1_1_6_handle; |
| 26 | void* m_address_UnmapViewOfFileEx = nullptr; |
| 27 | void* m_address_VirtualAlloc2 = nullptr; |
| 28 | void* m_address_MapViewOfFile3 = nullptr; |
| 29 | }; |
| 30 | #endif |
| 31 | |
| 32 | // This class lets you create a block of anonymous RAM, and then arbitrarily map views into it. |
| 33 | // Multiple views can mirror the same section of the block, which makes it very convenient for |
| 34 | // emulating memory mirrors. |
| 35 | class MemArena final |
| 36 | { |
| 37 | public: |
| 38 | MemArena(); |
| 39 | ~MemArena(); |
| 40 | MemArena(const MemArena&) = delete; |
| 41 | MemArena(MemArena&&) = delete; |
| 42 | MemArena& operator=(const MemArena&) = delete; |
| 43 | MemArena& operator=(MemArena&&) = delete; |
| 44 | |
| 45 | /// |
| 46 | /// Allocate the singular memory segment handled by this MemArena. This will be the actual |
| 47 | /// 'physical' available memory for this arena. After allocation, it can be interacted with using |
| 48 | /// CreateView() and ReleaseView(). Used to make a mappable region for emulated memory. |
| 49 | /// |
| 50 | /// @param size The amount of bytes that should be allocated in this region. |
| 51 | /// @param base_name A base name for the shared memory region, if applicable for this platform. |
| 52 | /// Will be extended with the process ID. |
| 53 | /// |
| 54 | void GrabSHMSegment(size_t size, std::string_view base_name); |
| 55 | |
| 56 | /// |
| 57 | /// Release the memory segment previously allocated with GrabSHMSegment(). |
| 58 | /// Should not be called before all views have been released. |
| 59 | /// |
| 60 | void ReleaseSHMSegment(); |
| 61 | |
| 62 | /// |
| 63 | /// Map a memory region in the memory segment previously allocated with GrabSHMSegment(). |
| 64 | /// |
| 65 | /// @param offset Offset within the memory segment to map at. |
| 66 | /// @param size Size of the region to map. |
| 67 | /// |
| 68 | /// @return Pointer to the memory region, or nullptr on failure. |
| 69 | /// |
| 70 | void* CreateView(s64 offset, size_t size); |
| 71 | |
| 72 | /// |
| 73 | /// Unmap a memory region previously mapped with CreateView(). |
| 74 | /// Should not be called on a view that is still mapped into the virtual memory region. |
| 75 | /// |
| 76 | /// @param view Pointer returned by CreateView(). |
| 77 | /// @param size Size passed to the corresponding CreateView() call. |
| 78 | /// |
| 79 | void ReleaseView(void* view, size_t size); |
| 80 | |
| 81 | /// |
| 82 | /// Reserve the singular 'virtual' memory region handled by this MemArena. This is used to create |
| 83 | /// our 'fastmem' memory area for the emulated game code to access directly. |
| 84 | /// |
| 85 | /// @param memory_size Size in bytes of the memory region to reserve. |
| 86 | /// |
| 87 | /// @return Pointer to the memory region, or nullptr on failure. |
| 88 | /// |
| 89 | u8* ReserveMemoryRegion(size_t memory_size); |
| 90 | |
| 91 | /// |
| 92 | /// Release the memory region previously reserved with ReserveMemoryRegion(). |
| 93 | /// Should not be called while any memory region is still mapped. |
| 94 | /// |
| 95 | void ReleaseMemoryRegion(); |
| 96 | |
| 97 | /// |
| 98 | /// Map a section from the memory segment previously allocated with GrabSHMSegment() |
| 99 | /// into the region previously reserved with ReserveMemoryRegion(). |
| 100 | /// |
| 101 | /// @param offset Offset within the memory segment previous allocated by GrabSHMSegment() to map |
| 102 | /// from. |
| 103 | /// @param size Size of the region to map. |
| 104 | /// @param base Address within the memory region from ReserveMemoryRegion() where to map it. |
| 105 | /// @param writeable Whether the region should be both readable and writeable, or just readable. |
| 106 | /// |
| 107 | /// @return The address we actually ended up mapping, which should be the given 'base'. |
| 108 | /// |
| 109 | void* MapInMemoryRegion(s64 offset, size_t size, void* base, bool writeable); |
| 110 | |
| 111 | /// |
| 112 | /// Changes whether a section mapped by MapInMemoryRegion is writeable. |
| 113 | /// |
| 114 | /// @param view The address returned by MapInMemoryRegion. |
| 115 | /// @param size The size passed to MapInMemoryRegion. |
| 116 | /// @param writeable Whether the region should be both readable and writeable, or just readable. |
| 117 | /// |
| 118 | /// @return Whether the operation succeeded. |
| 119 | /// |
| 120 | bool ChangeMappingProtection(void* view, size_t size, bool writeable); |
| 121 | |
| 122 | /// |
| 123 | /// Unmap a memory region previously mapped with MapInMemoryRegion(). |
| 124 | /// |
| 125 | /// @param view Pointer returned by MapInMemoryRegion(). |
| 126 | /// @param size Size passed to the corresponding MapInMemoryRegion() call. |
| 127 | /// |
| 128 | void UnmapFromMemoryRegion(void* view, size_t size); |
| 129 | |
| 130 | /// |
| 131 | /// Return the system's page size or required page alignment, whichever is larger. |
| 132 | /// |
| 133 | size_t GetPageSize() const; |
| 134 | |
| 135 | private: |
| 136 | #ifdef _WIN32 |
| 137 | WindowsMemoryRegion* EnsureSplitRegionForMapping(void* address, size_t size); |
| 138 | bool JoinRegionsAfterUnmap(void* address, size_t size); |
| 139 | |
| 140 | std::vector<WindowsMemoryRegion> m_regions; |
| 141 | void* m_reserved_region = nullptr; |
| 142 | void* m_memory_handle = nullptr; |
| 143 | WindowsMemoryFunctions m_memory_functions; |
| 144 | #elif defined(__APPLE__) |
| 145 | vm_address_t m_shm_address = 0; |
| 146 | vm_size_t m_shm_size = 0; |
| 147 | mem_entry_name_port_t m_shm_entry = MACH_PORT_NULL; |
| 148 | |
| 149 | vm_address_t m_region_address = 0; |
| 150 | vm_size_t m_region_size = 0; |
| 151 | #else |
| 152 | int m_shm_fd = 0; |
| 153 | void* m_reserved_region = nullptr; |
| 154 | std::size_t m_reserved_region_size = 0; |
| 155 | #endif |
| 156 | }; |
| 157 | |
| 158 | // This class represents a single fixed-size memory region where the individual memory pages are |
| 159 | // only actually allocated on first access. The memory will be zero on first access. |
| 160 | class LazyMemoryRegion final |
| 161 | { |
| 162 | public: |
| 163 | LazyMemoryRegion(); |
| 164 | ~LazyMemoryRegion(); |
| 165 | LazyMemoryRegion(const LazyMemoryRegion&) = delete; |
| 166 | LazyMemoryRegion(LazyMemoryRegion&&) = delete; |
| 167 | LazyMemoryRegion& operator=(const LazyMemoryRegion&) = delete; |
| 168 | LazyMemoryRegion& operator=(LazyMemoryRegion&&) = delete; |
| 169 | |
| 170 | /// |
| 171 | /// Reserve a memory region. |
| 172 | /// |
| 173 | /// @param size The size of the region. |
| 174 | /// |
| 175 | /// @return The address the region was mapped at. Returns nullptr on failure. |
| 176 | /// |
| 177 | void* Create(size_t size); |
| 178 | |
| 179 | /// |
| 180 | /// Reset the memory region back to zero, throwing away any mapped pages. |
| 181 | /// This can only be called after a successful call to Create(). |
| 182 | /// |
| 183 | void Clear(); |
| 184 | |
| 185 | /// |
| 186 | /// Release the memory previously reserved with Create(). After this call the pointer that was |
| 187 | /// returned by Create() will become invalid. |
| 188 | /// |
| 189 | void Release(); |
| 190 | |
| 191 | /// |
| 192 | /// Ensure that the memory page at the given byte offset from the start of the memory region is |
| 193 | /// writable. We use this on Windows as a workaround to only actually commit pages as they are |
| 194 | /// written to. On other OSes this does nothing. |
| 195 | /// |
| 196 | /// @param offset The offset into the memory region that should be made writable if it isn't. |
| 197 | /// |
| 198 | void EnsureMemoryPageWritable(size_t offset) |
| 199 | { |
| 200 | #ifdef _WIN32 |
| 201 | const size_t block_index = offset / BLOCK_SIZE; |
| 202 | if (m_writable_block_handles[block_index] == nullptr) |
| 203 | MakeMemoryBlockWritable(block_index); |
| 204 | #endif |
| 205 | } |
| 206 | |
| 207 | void EnsureMemoryPagesWritable(size_t offset, size_t size) |
| 208 | { |
| 209 | #ifdef _WIN32 |
| 210 | for (const auto end_offset = offset + size; offset < end_offset; offset += BLOCK_SIZE) |
| 211 | EnsureMemoryPageWritable(offset); |
| 212 | #endif |
| 213 | } |
| 214 | |
| 215 | private: |
| 216 | void* m_memory = nullptr; |
| 217 | size_t m_size = 0; |
| 218 | |
| 219 | #ifdef _WIN32 |
| 220 | void* m_zero_block = nullptr; |
| 221 | constexpr static size_t BLOCK_SIZE = 8 * 1024 * 1024; // size of allocated memory blocks |
| 222 | WindowsMemoryFunctions m_memory_functions; |
| 223 | std::vector<void*> m_writable_block_handles; |
| 224 | |
| 225 | void MakeMemoryBlockWritable(size_t offset); |
| 226 | #endif |
| 227 | }; |
| 228 | |
| 229 | } // namespace Common |