MmapRegion.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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, String::formatted("(UE) {}", name).characters());
  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 : String::formatted("(UE) {}", 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 { m_data[offset], 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. u16 value, shadow;
  82. ByteReader::load(m_data + offset, value);
  83. ByteReader::load(m_shadow_data + offset, shadow);
  84. return { value, shadow };
  85. }
  86. ValueWithShadow<u32> MmapRegion::read32(u32 offset)
  87. {
  88. if (!is_readable()) {
  89. reportln("32-bit read from unreadable MmapRegion @ {:p}", base() + offset);
  90. emulator().dump_backtrace();
  91. TODO();
  92. }
  93. if (is_malloc_block()) {
  94. if (auto* tracer = emulator().malloc_tracer())
  95. tracer->audit_read(*this, base() + offset, 4);
  96. }
  97. VERIFY(offset + 3 < size());
  98. u32 value, shadow;
  99. ByteReader::load(m_data + offset, value);
  100. ByteReader::load(m_shadow_data + offset, shadow);
  101. return { value, shadow };
  102. }
  103. ValueWithShadow<u64> MmapRegion::read64(u32 offset)
  104. {
  105. if (!is_readable()) {
  106. reportln("64-bit read from unreadable MmapRegion @ {:p}", base() + offset);
  107. emulator().dump_backtrace();
  108. TODO();
  109. }
  110. if (is_malloc_block()) {
  111. if (auto* tracer = emulator().malloc_tracer())
  112. tracer->audit_read(*this, base() + offset, 8);
  113. }
  114. VERIFY(offset + 7 < size());
  115. u64 value, shadow;
  116. ByteReader::load(m_data + offset, value);
  117. ByteReader::load(m_shadow_data + offset, shadow);
  118. return { value, shadow };
  119. }
  120. ValueWithShadow<u128> MmapRegion::read128(u32 offset)
  121. {
  122. if (!is_readable()) {
  123. reportln("128-bit read from unreadable MmapRegion @ {:p}", base() + offset);
  124. emulator().dump_backtrace();
  125. TODO();
  126. }
  127. if (is_malloc_block()) {
  128. if (auto* tracer = emulator().malloc_tracer())
  129. tracer->audit_read(*this, base() + offset, 16);
  130. }
  131. VERIFY(offset + 15 < size());
  132. u128 value, shadow;
  133. ByteReader::load(m_data + offset, value);
  134. ByteReader::load(m_shadow_data + offset, shadow);
  135. return { value, shadow };
  136. }
  137. ValueWithShadow<u256> MmapRegion::read256(u32 offset)
  138. {
  139. if (!is_readable()) {
  140. reportln("256-bit read from unreadable MmapRegion @ {:p}", base() + offset);
  141. emulator().dump_backtrace();
  142. TODO();
  143. }
  144. if (is_malloc_block()) {
  145. if (auto* tracer = emulator().malloc_tracer())
  146. tracer->audit_read(*this, base() + offset, 32);
  147. }
  148. VERIFY(offset + 31 < size());
  149. u256 value, shadow;
  150. ByteReader::load(m_data + offset, value);
  151. ByteReader::load(m_shadow_data + offset, shadow);
  152. return { value, shadow };
  153. }
  154. void MmapRegion::write8(u32 offset, ValueWithShadow<u8> value)
  155. {
  156. if (!is_writable()) {
  157. reportln("8-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, 1);
  164. }
  165. VERIFY(offset < size());
  166. m_data[offset] = value.value();
  167. m_shadow_data[offset] = value.shadow();
  168. }
  169. void MmapRegion::write16(u32 offset, ValueWithShadow<u16> value)
  170. {
  171. if (!is_writable()) {
  172. reportln("16-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, 2);
  179. }
  180. VERIFY(offset + 1 < size());
  181. ByteReader::store(m_data + offset, value.value());
  182. ByteReader::store(m_shadow_data + offset, value.shadow());
  183. }
  184. void MmapRegion::write32(u32 offset, ValueWithShadow<u32> value)
  185. {
  186. if (!is_writable()) {
  187. reportln("32-bit write from unwritable MmapRegion @ {:p}", base() + offset);
  188. emulator().dump_backtrace();
  189. TODO();
  190. }
  191. if (is_malloc_block()) {
  192. if (auto* tracer = emulator().malloc_tracer())
  193. tracer->audit_write(*this, base() + offset, 4);
  194. }
  195. VERIFY(offset + 3 < size());
  196. VERIFY(m_data != m_shadow_data);
  197. ByteReader::store(m_data + offset, value.value());
  198. ByteReader::store(m_shadow_data + offset, value.shadow());
  199. }
  200. void MmapRegion::write64(u32 offset, ValueWithShadow<u64> value)
  201. {
  202. if (!is_writable()) {
  203. reportln("64-bit write from unwritable MmapRegion @ {:p}", base() + offset);
  204. emulator().dump_backtrace();
  205. TODO();
  206. }
  207. if (is_malloc_block()) {
  208. if (auto* tracer = emulator().malloc_tracer())
  209. tracer->audit_write(*this, base() + offset, 8);
  210. }
  211. VERIFY(offset + 7 < size());
  212. VERIFY(m_data != m_shadow_data);
  213. ByteReader::store(m_data + offset, value.value());
  214. ByteReader::store(m_shadow_data + offset, value.shadow());
  215. }
  216. void MmapRegion::write128(u32 offset, ValueWithShadow<u128> value)
  217. {
  218. if (!is_writable()) {
  219. reportln("128-bit write from unwritable MmapRegion @ {:p}", base() + offset);
  220. emulator().dump_backtrace();
  221. TODO();
  222. }
  223. if (is_malloc_block()) {
  224. if (auto* tracer = emulator().malloc_tracer())
  225. tracer->audit_write(*this, base() + offset, 16);
  226. }
  227. VERIFY(offset + 15 < size());
  228. VERIFY(m_data != m_shadow_data);
  229. ByteReader::store(m_data + offset, value.value());
  230. ByteReader::store(m_shadow_data + offset, value.shadow());
  231. }
  232. void MmapRegion::write256(u32 offset, ValueWithShadow<u256> value)
  233. {
  234. if (!is_writable()) {
  235. reportln("256-bit write from unwritable MmapRegion @ {:p}", base() + offset);
  236. emulator().dump_backtrace();
  237. TODO();
  238. }
  239. if (is_malloc_block()) {
  240. if (auto* tracer = emulator().malloc_tracer())
  241. tracer->audit_write(*this, base() + offset, 32);
  242. }
  243. VERIFY(offset + 31 < size());
  244. VERIFY(m_data != m_shadow_data);
  245. ByteReader::store(m_data + offset, value.value());
  246. ByteReader::store(m_shadow_data + offset, value.shadow());
  247. }
  248. NonnullOwnPtr<MmapRegion> MmapRegion::split_at(VirtualAddress offset)
  249. {
  250. VERIFY(!m_malloc);
  251. VERIFY(!m_malloc_metadata);
  252. Range new_range = range();
  253. Range other_range = new_range.split_at(offset);
  254. auto other_region = adopt_own(*new MmapRegion(other_range.base().get(), other_range.size(), prot(), data() + new_range.size(), shadow_data() + new_range.size()));
  255. other_region->m_file_backed = m_file_backed;
  256. other_region->m_name = m_name;
  257. set_range(new_range);
  258. return other_region;
  259. }
  260. void MmapRegion::set_prot(int prot)
  261. {
  262. set_readable(prot & PROT_READ);
  263. set_writable(prot & PROT_WRITE);
  264. set_executable(prot & PROT_EXEC);
  265. if (m_file_backed) {
  266. if (mprotect(m_data, size(), prot & ~PROT_EXEC) < 0) {
  267. perror("MmapRegion::set_prot: mprotect");
  268. exit(1);
  269. }
  270. }
  271. }
  272. void MmapRegion::set_name(String name)
  273. {
  274. m_name = move(name);
  275. set_mmap_name(range().base().as_ptr(), range().size(), String::formatted("(UE) {}", m_name).characters());
  276. }
  277. }