MemArena.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // Copyright 2008 Dolphin Emulator Project
  2. // Licensed under GPLv2+
  3. // Refer to the license.txt file included.
  4. #include <cstddef>
  5. #include <cstdlib>
  6. #include <set>
  7. #include <string>
  8. #include "Common/CommonTypes.h"
  9. #include "Common/MemArena.h"
  10. #include "Common/StringUtil.h"
  11. #ifdef _WIN32
  12. #include <windows.h>
  13. #else
  14. #include <cerrno>
  15. #include <cstring>
  16. #include <fcntl.h>
  17. #include <unistd.h>
  18. #include <sys/mman.h>
  19. #ifdef ANDROID
  20. #include <sys/ioctl.h>
  21. #include <linux/ashmem.h>
  22. #endif
  23. #endif
  24. #ifdef ANDROID
  25. #define ASHMEM_DEVICE "/dev/ashmem"
  26. static int AshmemCreateFileMapping(const char *name, size_t size)
  27. {
  28. int fd, ret;
  29. fd = open(ASHMEM_DEVICE, O_RDWR);
  30. if (fd < 0)
  31. return fd;
  32. // We don't really care if we can't set the name, it is optional
  33. ioctl(fd, ASHMEM_SET_NAME, name);
  34. ret = ioctl(fd, ASHMEM_SET_SIZE, size);
  35. if (ret < 0)
  36. {
  37. close(fd);
  38. NOTICE_LOG(MEMMAP, "Ashmem returned error: 0x%08x", ret);
  39. return ret;
  40. }
  41. return fd;
  42. }
  43. #endif
  44. void MemArena::GrabSHMSegment(size_t size)
  45. {
  46. #ifdef _WIN32
  47. hMemoryMapping = CreateFileMapping(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, 0, (DWORD)(size), nullptr);
  48. #elif defined(ANDROID)
  49. fd = AshmemCreateFileMapping("Dolphin-emu", size);
  50. if (fd < 0)
  51. {
  52. NOTICE_LOG(MEMMAP, "Ashmem allocation failed");
  53. return;
  54. }
  55. #else
  56. for (int i = 0; i < 10000; i++)
  57. {
  58. std::string file_name = StringFromFormat("/dolphinmem.%d", i);
  59. fd = shm_open(file_name.c_str(), O_RDWR | O_CREAT | O_EXCL, 0600);
  60. if (fd != -1)
  61. {
  62. shm_unlink(file_name.c_str());
  63. break;
  64. }
  65. else if (errno != EEXIST)
  66. {
  67. ERROR_LOG(MEMMAP, "shm_open failed: %s", strerror(errno));
  68. return;
  69. }
  70. }
  71. if (ftruncate(fd, size) < 0)
  72. ERROR_LOG(MEMMAP, "Failed to allocate low memory space");
  73. #endif
  74. }
  75. void MemArena::ReleaseSHMSegment()
  76. {
  77. #ifdef _WIN32
  78. CloseHandle(hMemoryMapping);
  79. hMemoryMapping = 0;
  80. #else
  81. close(fd);
  82. #endif
  83. }
  84. void *MemArena::CreateView(s64 offset, size_t size, void *base)
  85. {
  86. #ifdef _WIN32
  87. return MapViewOfFileEx(hMemoryMapping, FILE_MAP_ALL_ACCESS, 0, (DWORD)((u64)offset), size, base);
  88. #else
  89. void *retval = mmap(
  90. base, size,
  91. PROT_READ | PROT_WRITE,
  92. MAP_SHARED | ((base == nullptr) ? 0 : MAP_FIXED),
  93. fd, offset);
  94. if (retval == MAP_FAILED)
  95. {
  96. NOTICE_LOG(MEMMAP, "mmap failed");
  97. return nullptr;
  98. }
  99. else
  100. {
  101. return retval;
  102. }
  103. #endif
  104. }
  105. void MemArena::ReleaseView(void* view, size_t size)
  106. {
  107. #ifdef _WIN32
  108. UnmapViewOfFile(view);
  109. #else
  110. munmap(view, size);
  111. #endif
  112. }
  113. u8* MemArena::FindMemoryBase()
  114. {
  115. #if _ARCH_64
  116. #ifdef _WIN32
  117. // 64 bit
  118. u8* base = (u8*)VirtualAlloc(0, 0x400000000, MEM_RESERVE, PAGE_READWRITE);
  119. VirtualFree(base, 0, MEM_RELEASE);
  120. return base;
  121. #else
  122. // Very precarious - mmap cannot return an error when trying to map already used pages.
  123. // This makes the Windows approach above unusable on Linux, so we will simply pray...
  124. return reinterpret_cast<u8*>(0x2300000000ULL);
  125. #endif
  126. #else // 32 bit
  127. #ifdef ANDROID
  128. // Android 4.3 changed how mmap works.
  129. // if we map it private and then munmap it, we can't use the base returned.
  130. // This may be due to changes in them support a full SELinux implementation.
  131. const int flags = MAP_ANON | MAP_SHARED;
  132. #else
  133. const int flags = MAP_ANON | MAP_PRIVATE;
  134. #endif
  135. const u32 MemSize = 0x31000000;
  136. void* base = mmap(0, MemSize, PROT_NONE, flags, -1, 0);
  137. if (base == MAP_FAILED)
  138. {
  139. PanicAlert("Failed to map 1 GB of memory space: %s", strerror(errno));
  140. return 0;
  141. }
  142. munmap(base, MemSize);
  143. return static_cast<u8*>(base);
  144. #endif
  145. }
  146. // yeah, this could also be done in like two bitwise ops...
  147. #define SKIP(a_flags, b_flags) \
  148. if (!(a_flags & MV_WII_ONLY) && (b_flags & MV_WII_ONLY)) \
  149. continue; \
  150. if (!(a_flags & MV_FAKE_VMEM) && (b_flags & MV_FAKE_VMEM)) \
  151. continue; \
  152. static bool Memory_TryBase(u8 *base, MemoryView *views, int num_views, u32 flags, MemArena *arena)
  153. {
  154. // OK, we know where to find free space. Now grab it!
  155. // We just mimic the popular BAT setup.
  156. int i;
  157. for (i = 0; i < num_views; i++)
  158. {
  159. MemoryView* view = &views[i];
  160. void* view_base;
  161. bool use_sw_mirror;
  162. SKIP(flags, view->flags);
  163. #if _ARCH_64
  164. // On 64-bit, we map the same file position multiple times, so we
  165. // don't need the software fallback for the mirrors.
  166. view_base = base + view->virtual_address;
  167. use_sw_mirror = false;
  168. #else
  169. // On 32-bit, we don't have the actual address space to store all
  170. // the mirrors, so we just map the fallbacks somewhere in our address
  171. // space and use the software fallbacks for mirroring.
  172. view_base = base + (view->virtual_address & 0x3FFFFFFF);
  173. use_sw_mirror = true;
  174. #endif
  175. if (use_sw_mirror && (view->flags & MV_MIRROR_PREVIOUS))
  176. {
  177. view->view_ptr = views[i - 1].view_ptr;
  178. }
  179. else
  180. {
  181. view->mapped_ptr = arena->CreateView(view->shm_position, view->size, view_base);
  182. view->view_ptr = view->mapped_ptr;
  183. }
  184. if (!view->view_ptr)
  185. {
  186. // Argh! ERROR! Free what we grabbed so far so we can try again.
  187. MemoryMap_Shutdown(views, i+1, flags, arena);
  188. return false;
  189. }
  190. if (view->out_ptr)
  191. *(view->out_ptr) = (u8*) view->view_ptr;
  192. }
  193. return true;
  194. }
  195. static u32 MemoryMap_InitializeViews(MemoryView *views, int num_views, u32 flags)
  196. {
  197. u32 shm_position = 0;
  198. u32 last_position = 0;
  199. for (int i = 0; i < num_views; i++)
  200. {
  201. // Zero all the pointers to be sure.
  202. views[i].mapped_ptr = nullptr;
  203. SKIP(flags, views[i].flags);
  204. if (views[i].flags & MV_MIRROR_PREVIOUS)
  205. shm_position = last_position;
  206. views[i].shm_position = shm_position;
  207. last_position = shm_position;
  208. shm_position += views[i].size;
  209. }
  210. return shm_position;
  211. }
  212. u8 *MemoryMap_Setup(MemoryView *views, int num_views, u32 flags, MemArena *arena)
  213. {
  214. u32 total_mem = MemoryMap_InitializeViews(views, num_views, flags);
  215. arena->GrabSHMSegment(total_mem);
  216. // Now, create views in high memory where there's plenty of space.
  217. u8 *base = MemArena::FindMemoryBase();
  218. // This really shouldn't fail - in 64-bit, there will always be enough
  219. // address space.
  220. if (!Memory_TryBase(base, views, num_views, flags, arena))
  221. {
  222. PanicAlert("MemoryMap_Setup: Failed finding a memory base.");
  223. exit(0);
  224. return nullptr;
  225. }
  226. return base;
  227. }
  228. void MemoryMap_Shutdown(MemoryView *views, int num_views, u32 flags, MemArena *arena)
  229. {
  230. std::set<void*> freeset;
  231. for (int i = 0; i < num_views; i++)
  232. {
  233. MemoryView* view = &views[i];
  234. if (view->mapped_ptr && !freeset.count(view->mapped_ptr))
  235. {
  236. arena->ReleaseView(view->mapped_ptr, view->size);
  237. freeset.insert(view->mapped_ptr);
  238. view->mapped_ptr = nullptr;
  239. }
  240. }
  241. }