MemArenaAndroid.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright 2008 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "Common/MemArena.h"
  4. #include <cerrno>
  5. #include <cstddef>
  6. #include <cstdlib>
  7. #include <cstring>
  8. #include <set>
  9. #include <string>
  10. #include <fmt/format.h>
  11. #include <dlfcn.h>
  12. #include <fcntl.h>
  13. #include <linux/ashmem.h>
  14. #include <sys/ioctl.h>
  15. #include <sys/mman.h>
  16. #include <unistd.h>
  17. #include "Common/Assert.h"
  18. #include "Common/CommonFuncs.h"
  19. #include "Common/CommonTypes.h"
  20. #include "Common/Logging/Log.h"
  21. #include "Common/MsgHandler.h"
  22. #include "Common/StringUtil.h"
  23. namespace Common
  24. {
  25. #define ASHMEM_DEVICE "/dev/ashmem"
  26. static int AshmemCreateFileMapping(const char* name, size_t size)
  27. {
  28. // ASharedMemory path - works on API >= 26 and falls through on API < 26:
  29. // We can't call ASharedMemory_create the normal way without increasing the
  30. // minimum version requirement to API 26, so we use dlopen/dlsym instead
  31. static void* libandroid = dlopen("libandroid.so", RTLD_LAZY | RTLD_LOCAL);
  32. static auto shared_memory_create =
  33. reinterpret_cast<int (*)(const char*, size_t)>(dlsym(libandroid, "ASharedMemory_create"));
  34. if (shared_memory_create)
  35. return shared_memory_create(name, size);
  36. // /dev/ashmem path - works on API < 29:
  37. int fd, ret;
  38. fd = open(ASHMEM_DEVICE, O_RDWR);
  39. if (fd < 0)
  40. return fd;
  41. // We don't really care if we can't set the name, it is optional
  42. ioctl(fd, ASHMEM_SET_NAME, name);
  43. ret = ioctl(fd, ASHMEM_SET_SIZE, size);
  44. if (ret < 0)
  45. {
  46. close(fd);
  47. NOTICE_LOG_FMT(MEMMAP, "Ashmem returned error: {:#010x}", ret);
  48. return ret;
  49. }
  50. return fd;
  51. }
  52. MemArena::MemArena() = default;
  53. MemArena::~MemArena() = default;
  54. void MemArena::GrabSHMSegment(size_t size, std::string_view base_name)
  55. {
  56. const std::string name = fmt::format("{}.{}", base_name, getpid());
  57. m_shm_fd = AshmemCreateFileMapping(name.c_str(), size);
  58. if (m_shm_fd < 0)
  59. NOTICE_LOG_FMT(MEMMAP, "Ashmem allocation failed");
  60. }
  61. void MemArena::ReleaseSHMSegment()
  62. {
  63. close(m_shm_fd);
  64. }
  65. void* MemArena::CreateView(s64 offset, size_t size)
  66. {
  67. void* retval = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, m_shm_fd, offset);
  68. if (retval == MAP_FAILED)
  69. {
  70. NOTICE_LOG_FMT(MEMMAP, "mmap failed");
  71. return nullptr;
  72. }
  73. else
  74. {
  75. return retval;
  76. }
  77. }
  78. void MemArena::ReleaseView(void* view, size_t size)
  79. {
  80. munmap(view, size);
  81. }
  82. u8* MemArena::ReserveMemoryRegion(size_t memory_size)
  83. {
  84. // Android 4.3 changed how mmap works.
  85. // if we map it private and then munmap it, we can't use the base returned.
  86. // This may be due to changes in them to support a full SELinux implementation.
  87. const int flags = MAP_ANON | MAP_SHARED;
  88. void* base = mmap(nullptr, memory_size, PROT_NONE, flags, -1, 0);
  89. if (base == MAP_FAILED)
  90. {
  91. PanicAlertFmt("Failed to map enough memory space: {}", LastStrerrorString());
  92. return nullptr;
  93. }
  94. m_reserved_region = base;
  95. m_reserved_region_size = memory_size;
  96. return static_cast<u8*>(base);
  97. }
  98. void MemArena::ReleaseMemoryRegion()
  99. {
  100. if (m_reserved_region)
  101. {
  102. munmap(m_reserved_region, m_reserved_region_size);
  103. m_reserved_region = nullptr;
  104. }
  105. }
  106. void* MemArena::MapInMemoryRegion(s64 offset, size_t size, void* base)
  107. {
  108. void* retval = mmap(base, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, m_shm_fd, offset);
  109. if (retval == MAP_FAILED)
  110. {
  111. NOTICE_LOG_FMT(MEMMAP, "mmap failed");
  112. return nullptr;
  113. }
  114. else
  115. {
  116. return retval;
  117. }
  118. }
  119. void MemArena::UnmapFromMemoryRegion(void* view, size_t size)
  120. {
  121. void* retval = mmap(view, size, PROT_NONE, MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
  122. if (retval == MAP_FAILED)
  123. NOTICE_LOG_FMT(MEMMAP, "mmap failed");
  124. }
  125. LazyMemoryRegion::LazyMemoryRegion() = default;
  126. LazyMemoryRegion::~LazyMemoryRegion()
  127. {
  128. Release();
  129. }
  130. void* LazyMemoryRegion::Create(size_t size)
  131. {
  132. ASSERT(!m_memory);
  133. if (size == 0)
  134. return nullptr;
  135. void* memory = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  136. if (memory == MAP_FAILED)
  137. {
  138. NOTICE_LOG_FMT(MEMMAP, "Memory allocation of {} bytes failed.", size);
  139. return nullptr;
  140. }
  141. m_memory = memory;
  142. m_size = size;
  143. return memory;
  144. }
  145. void LazyMemoryRegion::Clear()
  146. {
  147. ASSERT(m_memory);
  148. void* new_memory = mmap(m_memory, m_size, PROT_READ | PROT_WRITE,
  149. MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
  150. ASSERT(new_memory == m_memory);
  151. }
  152. void LazyMemoryRegion::Release()
  153. {
  154. if (m_memory)
  155. {
  156. munmap(m_memory, m_size);
  157. m_memory = nullptr;
  158. m_size = 0;
  159. }
  160. }
  161. } // namespace Common