MmapRegion.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "MmapRegion.h"
  27. #include "Emulator.h"
  28. #include <string.h>
  29. #include <sys/mman.h>
  30. namespace UserspaceEmulator {
  31. NonnullOwnPtr<MmapRegion> MmapRegion::create_anonymous(u32 base, u32 size, u32 prot)
  32. {
  33. auto region = adopt_own(*new MmapRegion(base, size, prot));
  34. region->m_file_backed = false;
  35. region->m_data = (u8*)calloc(1, size);
  36. return region;
  37. }
  38. NonnullOwnPtr<MmapRegion> MmapRegion::create_file_backed(u32 base, u32 size, u32 prot, int flags, int fd, off_t offset, String name)
  39. {
  40. auto region = adopt_own(*new MmapRegion(base, size, prot));
  41. region->m_file_backed = true;
  42. if (!name.is_empty()) {
  43. name = String::formatted("{} (Emulated)", name);
  44. region->m_name = name;
  45. }
  46. region->m_data = (u8*)mmap_with_name(nullptr, size, prot, flags, fd, offset, name.is_empty() ? nullptr : name.characters());
  47. ASSERT(region->m_data != MAP_FAILED);
  48. return region;
  49. }
  50. MmapRegion::MmapRegion(u32 base, u32 size, int prot)
  51. : Region(base, size)
  52. {
  53. set_prot(prot);
  54. m_shadow_data = (u8*)malloc(size);
  55. memset(m_shadow_data, 1, size);
  56. }
  57. MmapRegion::~MmapRegion()
  58. {
  59. free(m_shadow_data);
  60. if (m_file_backed)
  61. munmap(m_data, size());
  62. else
  63. free(m_data);
  64. }
  65. ValueWithShadow<u8> MmapRegion::read8(FlatPtr offset)
  66. {
  67. if (!is_readable()) {
  68. reportln("8-bit read from unreadable MmapRegion @ {:p}", base() + offset);
  69. emulator().dump_backtrace();
  70. TODO();
  71. }
  72. if (is_malloc_block()) {
  73. if (auto* tracer = emulator().malloc_tracer())
  74. tracer->audit_read(*this, base() + offset, 1);
  75. }
  76. ASSERT(offset < size());
  77. return { *reinterpret_cast<const u8*>(m_data + offset), *reinterpret_cast<const u8*>(m_shadow_data + offset) };
  78. }
  79. ValueWithShadow<u16> MmapRegion::read16(u32 offset)
  80. {
  81. if (!is_readable()) {
  82. reportln("16-bit read from unreadable MmapRegion @ {:p}", base() + offset);
  83. emulator().dump_backtrace();
  84. TODO();
  85. }
  86. if (is_malloc_block()) {
  87. if (auto* tracer = emulator().malloc_tracer())
  88. tracer->audit_read(*this, base() + offset, 2);
  89. }
  90. ASSERT(offset + 1 < size());
  91. return { *reinterpret_cast<const u16*>(m_data + offset), *reinterpret_cast<const u16*>(m_shadow_data + offset) };
  92. }
  93. ValueWithShadow<u32> MmapRegion::read32(u32 offset)
  94. {
  95. if (!is_readable()) {
  96. reportln("32-bit read from unreadable MmapRegion @ {:p}", base() + offset);
  97. emulator().dump_backtrace();
  98. TODO();
  99. }
  100. if (is_malloc_block()) {
  101. if (auto* tracer = emulator().malloc_tracer())
  102. tracer->audit_read(*this, base() + offset, 4);
  103. }
  104. ASSERT(offset + 3 < size());
  105. return { *reinterpret_cast<const u32*>(m_data + offset), *reinterpret_cast<const u32*>(m_shadow_data + offset) };
  106. }
  107. ValueWithShadow<u64> MmapRegion::read64(u32 offset)
  108. {
  109. if (!is_readable()) {
  110. reportln("64-bit read from unreadable MmapRegion @ {:p}", base() + offset);
  111. emulator().dump_backtrace();
  112. TODO();
  113. }
  114. if (is_malloc_block()) {
  115. if (auto* tracer = emulator().malloc_tracer())
  116. tracer->audit_read(*this, base() + offset, 8);
  117. }
  118. ASSERT(offset + 7 < size());
  119. return { *reinterpret_cast<const u64*>(m_data + offset), *reinterpret_cast<const u64*>(m_shadow_data + offset) };
  120. }
  121. void MmapRegion::write8(u32 offset, ValueWithShadow<u8> value)
  122. {
  123. if (!is_writable()) {
  124. reportln("8-bit write from unwritable MmapRegion @ {:p}", base() + offset);
  125. emulator().dump_backtrace();
  126. TODO();
  127. }
  128. if (is_malloc_block()) {
  129. if (auto* tracer = emulator().malloc_tracer())
  130. tracer->audit_write(*this, base() + offset, 1);
  131. }
  132. ASSERT(offset < size());
  133. *reinterpret_cast<u8*>(m_data + offset) = value.value();
  134. *reinterpret_cast<u8*>(m_shadow_data + offset) = value.shadow();
  135. }
  136. void MmapRegion::write16(u32 offset, ValueWithShadow<u16> value)
  137. {
  138. if (!is_writable()) {
  139. reportln("16-bit write from unwritable MmapRegion @ {:p}", base() + offset);
  140. emulator().dump_backtrace();
  141. TODO();
  142. }
  143. if (is_malloc_block()) {
  144. if (auto* tracer = emulator().malloc_tracer())
  145. tracer->audit_write(*this, base() + offset, 2);
  146. }
  147. ASSERT(offset + 1 < size());
  148. *reinterpret_cast<u16*>(m_data + offset) = value.value();
  149. *reinterpret_cast<u16*>(m_shadow_data + offset) = value.shadow();
  150. }
  151. void MmapRegion::write32(u32 offset, ValueWithShadow<u32> value)
  152. {
  153. if (!is_writable()) {
  154. reportln("32-bit write from unwritable MmapRegion @ {:p}", base() + offset);
  155. emulator().dump_backtrace();
  156. TODO();
  157. }
  158. if (is_malloc_block()) {
  159. if (auto* tracer = emulator().malloc_tracer())
  160. tracer->audit_write(*this, base() + offset, 4);
  161. }
  162. ASSERT(offset + 3 < size());
  163. ASSERT(m_data != m_shadow_data);
  164. *reinterpret_cast<u32*>(m_data + offset) = value.value();
  165. *reinterpret_cast<u32*>(m_shadow_data + offset) = value.shadow();
  166. }
  167. void MmapRegion::write64(u32 offset, ValueWithShadow<u64> value)
  168. {
  169. if (!is_writable()) {
  170. reportln("64-bit write from unwritable MmapRegion @ {:p}", base() + offset);
  171. emulator().dump_backtrace();
  172. TODO();
  173. }
  174. if (is_malloc_block()) {
  175. if (auto* tracer = emulator().malloc_tracer())
  176. tracer->audit_write(*this, base() + offset, 8);
  177. }
  178. ASSERT(offset + 7 < size());
  179. ASSERT(m_data != m_shadow_data);
  180. *reinterpret_cast<u64*>(m_data + offset) = value.value();
  181. *reinterpret_cast<u64*>(m_shadow_data + offset) = value.shadow();
  182. }
  183. void MmapRegion::set_prot(int prot)
  184. {
  185. set_readable(prot & PROT_READ);
  186. set_writable(prot & PROT_WRITE);
  187. set_executable(prot & PROT_EXEC);
  188. if (m_file_backed) {
  189. mprotect(m_data, size(), prot);
  190. }
  191. }
  192. }