MmapRegion.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "MmapRegion.h"
  7. #include "Emulator.h"
  8. #include <string.h>
  9. #include <sys/mman.h>
  10. namespace UserspaceEmulator {
  11. static void* mmap_initialized(size_t bytes, char initial_value, const char* name)
  12. {
  13. auto* ptr = mmap_with_name(nullptr, bytes, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0, name);
  14. VERIFY(ptr != MAP_FAILED);
  15. memset(ptr, initial_value, bytes);
  16. return ptr;
  17. }
  18. static void free_pages(void* ptr, size_t bytes)
  19. {
  20. int rc = munmap(ptr, bytes);
  21. VERIFY(rc == 0);
  22. }
  23. NonnullOwnPtr<MmapRegion> MmapRegion::create_anonymous(u32 base, u32 size, u32 prot, String name)
  24. {
  25. auto data = (u8*)mmap_initialized(size, 0, nullptr);
  26. auto shadow_data = (u8*)mmap_initialized(size, 1, "MmapRegion ShadowData");
  27. auto region = adopt_own(*new MmapRegion(base, size, prot, data, shadow_data));
  28. region->m_name = move(name);
  29. return region;
  30. }
  31. NonnullOwnPtr<MmapRegion> MmapRegion::create_file_backed(u32 base, u32 size, u32 prot, int flags, int fd, off_t offset, String name)
  32. {
  33. // Since we put the memory to an arbitrary location, do not pass MAP_FIXED to the Kernel.
  34. auto real_flags = flags & ~MAP_FIXED;
  35. auto data = (u8*)mmap_with_name(nullptr, size, prot, real_flags, fd, offset, name.is_empty() ? nullptr : name.characters());
  36. VERIFY(data != MAP_FAILED);
  37. auto shadow_data = (u8*)mmap_initialized(size, 1, "MmapRegion ShadowData");
  38. auto region = adopt_own(*new MmapRegion(base, size, prot, data, shadow_data));
  39. region->m_file_backed = true;
  40. region->m_name = move(name);
  41. return region;
  42. }
  43. MmapRegion::MmapRegion(u32 base, u32 size, int prot, u8* data, u8* shadow_data)
  44. : Region(base, size, true)
  45. , m_data(data)
  46. , m_shadow_data(shadow_data)
  47. {
  48. set_prot(prot);
  49. }
  50. MmapRegion::~MmapRegion()
  51. {
  52. free_pages(m_data, size());
  53. free_pages(m_shadow_data, size());
  54. }
  55. ValueWithShadow<u8> MmapRegion::read8(FlatPtr offset)
  56. {
  57. if (!is_readable()) {
  58. reportln("8-bit read from unreadable MmapRegion @ {:p}", base() + offset);
  59. emulator().dump_backtrace();
  60. TODO();
  61. }
  62. if (is_malloc_block()) {
  63. if (auto* tracer = emulator().malloc_tracer())
  64. tracer->audit_read(*this, base() + offset, 1);
  65. }
  66. VERIFY(offset < size());
  67. return { *reinterpret_cast<const u8*>(m_data + offset), *reinterpret_cast<const u8*>(m_shadow_data + offset) };
  68. }
  69. ValueWithShadow<u16> MmapRegion::read16(u32 offset)
  70. {
  71. if (!is_readable()) {
  72. reportln("16-bit read from unreadable MmapRegion @ {:p}", base() + offset);
  73. emulator().dump_backtrace();
  74. TODO();
  75. }
  76. if (is_malloc_block()) {
  77. if (auto* tracer = emulator().malloc_tracer())
  78. tracer->audit_read(*this, base() + offset, 2);
  79. }
  80. VERIFY(offset + 1 < size());
  81. return { *reinterpret_cast<const u16*>(m_data + offset), *reinterpret_cast<const u16*>(m_shadow_data + offset) };
  82. }
  83. ValueWithShadow<u32> MmapRegion::read32(u32 offset)
  84. {
  85. if (!is_readable()) {
  86. reportln("32-bit read from unreadable MmapRegion @ {:p}", base() + offset);
  87. emulator().dump_backtrace();
  88. TODO();
  89. }
  90. if (is_malloc_block()) {
  91. if (auto* tracer = emulator().malloc_tracer())
  92. tracer->audit_read(*this, base() + offset, 4);
  93. }
  94. VERIFY(offset + 3 < size());
  95. return { *reinterpret_cast<const u32*>(m_data + offset), *reinterpret_cast<const u32*>(m_shadow_data + offset) };
  96. }
  97. ValueWithShadow<u64> MmapRegion::read64(u32 offset)
  98. {
  99. if (!is_readable()) {
  100. reportln("64-bit read from unreadable MmapRegion @ {:p}", base() + offset);
  101. emulator().dump_backtrace();
  102. TODO();
  103. }
  104. if (is_malloc_block()) {
  105. if (auto* tracer = emulator().malloc_tracer())
  106. tracer->audit_read(*this, base() + offset, 8);
  107. }
  108. VERIFY(offset + 7 < size());
  109. return { *reinterpret_cast<const u64*>(m_data + offset), *reinterpret_cast<const u64*>(m_shadow_data + offset) };
  110. }
  111. ValueWithShadow<u128> MmapRegion::read128(u32 offset)
  112. {
  113. if (!is_readable()) {
  114. reportln("128-bit read from unreadable MmapRegion @ {:p}", base() + offset);
  115. emulator().dump_backtrace();
  116. TODO();
  117. }
  118. if (is_malloc_block()) {
  119. if (auto* tracer = emulator().malloc_tracer())
  120. tracer->audit_read(*this, base() + offset, 16);
  121. }
  122. VERIFY(offset + 15 < size());
  123. return { *reinterpret_cast<const u128*>(m_data + offset), *reinterpret_cast<const u128*>(m_shadow_data + offset) };
  124. }
  125. ValueWithShadow<u256> MmapRegion::read256(u32 offset)
  126. {
  127. if (!is_readable()) {
  128. reportln("256-bit read from unreadable MmapRegion @ {:p}", base() + offset);
  129. emulator().dump_backtrace();
  130. TODO();
  131. }
  132. if (is_malloc_block()) {
  133. if (auto* tracer = emulator().malloc_tracer())
  134. tracer->audit_read(*this, base() + offset, 32);
  135. }
  136. VERIFY(offset + 31 < size());
  137. return { *reinterpret_cast<const u256*>(m_data + offset), *reinterpret_cast<const u256*>(m_shadow_data + offset) };
  138. }
  139. void MmapRegion::write8(u32 offset, ValueWithShadow<u8> value)
  140. {
  141. if (!is_writable()) {
  142. reportln("8-bit write from unwritable MmapRegion @ {:p}", base() + offset);
  143. emulator().dump_backtrace();
  144. TODO();
  145. }
  146. if (is_malloc_block()) {
  147. if (auto* tracer = emulator().malloc_tracer())
  148. tracer->audit_write(*this, base() + offset, 1);
  149. }
  150. VERIFY(offset < size());
  151. *reinterpret_cast<u8*>(m_data + offset) = value.value();
  152. *reinterpret_cast<u8*>(m_shadow_data + offset) = value.shadow();
  153. }
  154. void MmapRegion::write16(u32 offset, ValueWithShadow<u16> value)
  155. {
  156. if (!is_writable()) {
  157. reportln("16-bit write from unwritable MmapRegion @ {:p}", base() + offset);
  158. emulator().dump_backtrace();
  159. TODO();
  160. }
  161. if (is_malloc_block()) {
  162. if (auto* tracer = emulator().malloc_tracer())
  163. tracer->audit_write(*this, base() + offset, 2);
  164. }
  165. VERIFY(offset + 1 < size());
  166. *reinterpret_cast<u16*>(m_data + offset) = value.value();
  167. *reinterpret_cast<u16*>(m_shadow_data + offset) = value.shadow();
  168. }
  169. void MmapRegion::write32(u32 offset, ValueWithShadow<u32> value)
  170. {
  171. if (!is_writable()) {
  172. reportln("32-bit write from unwritable MmapRegion @ {:p}", base() + offset);
  173. emulator().dump_backtrace();
  174. TODO();
  175. }
  176. if (is_malloc_block()) {
  177. if (auto* tracer = emulator().malloc_tracer())
  178. tracer->audit_write(*this, base() + offset, 4);
  179. }
  180. VERIFY(offset + 3 < size());
  181. VERIFY(m_data != m_shadow_data);
  182. *reinterpret_cast<u32*>(m_data + offset) = value.value();
  183. *reinterpret_cast<u32*>(m_shadow_data + offset) = value.shadow();
  184. }
  185. void MmapRegion::write64(u32 offset, ValueWithShadow<u64> value)
  186. {
  187. if (!is_writable()) {
  188. reportln("64-bit write from unwritable MmapRegion @ {:p}", base() + offset);
  189. emulator().dump_backtrace();
  190. TODO();
  191. }
  192. if (is_malloc_block()) {
  193. if (auto* tracer = emulator().malloc_tracer())
  194. tracer->audit_write(*this, base() + offset, 8);
  195. }
  196. VERIFY(offset + 7 < size());
  197. VERIFY(m_data != m_shadow_data);
  198. *reinterpret_cast<u64*>(m_data + offset) = value.value();
  199. *reinterpret_cast<u64*>(m_shadow_data + offset) = value.shadow();
  200. }
  201. void MmapRegion::write128(u32 offset, ValueWithShadow<u128> value)
  202. {
  203. if (!is_writable()) {
  204. reportln("128-bit write from unwritable MmapRegion @ {:p}", base() + offset);
  205. emulator().dump_backtrace();
  206. TODO();
  207. }
  208. if (is_malloc_block()) {
  209. if (auto* tracer = emulator().malloc_tracer())
  210. tracer->audit_write(*this, base() + offset, 16);
  211. }
  212. VERIFY(offset + 15 < size());
  213. VERIFY(m_data != m_shadow_data);
  214. *reinterpret_cast<u128*>(m_data + offset) = value.value();
  215. *reinterpret_cast<u128*>(m_shadow_data + offset) = value.shadow();
  216. }
  217. void MmapRegion::write256(u32 offset, ValueWithShadow<u256> value)
  218. {
  219. if (!is_writable()) {
  220. reportln("256-bit write from unwritable MmapRegion @ {:p}", base() + offset);
  221. emulator().dump_backtrace();
  222. TODO();
  223. }
  224. if (is_malloc_block()) {
  225. if (auto* tracer = emulator().malloc_tracer())
  226. tracer->audit_write(*this, base() + offset, 32);
  227. }
  228. VERIFY(offset + 31 < size());
  229. VERIFY(m_data != m_shadow_data);
  230. *reinterpret_cast<u256*>(m_data + offset) = value.value();
  231. *reinterpret_cast<u256*>(m_shadow_data + offset) = value.shadow();
  232. }
  233. NonnullOwnPtr<MmapRegion> MmapRegion::split_at(VirtualAddress offset)
  234. {
  235. VERIFY(!m_malloc);
  236. VERIFY(!m_malloc_metadata);
  237. Range new_range = range();
  238. Range other_range = new_range.split_at(offset);
  239. auto other_region = adopt_own(*new MmapRegion(other_range.base().get(), other_range.size(), prot(), data() + new_range.size(), shadow_data() + new_range.size()));
  240. other_region->m_file_backed = m_file_backed;
  241. other_region->m_name = m_name;
  242. set_range(new_range);
  243. return other_region;
  244. }
  245. void MmapRegion::set_prot(int prot)
  246. {
  247. set_readable(prot & PROT_READ);
  248. set_writable(prot & PROT_WRITE);
  249. set_executable(prot & PROT_EXEC);
  250. if (m_file_backed) {
  251. if (mprotect(m_data, size(), prot & ~PROT_EXEC) < 0) {
  252. perror("MmapRegion::set_prot: mprotect");
  253. exit(1);
  254. }
  255. }
  256. }
  257. }