MmapRegion.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. dbgln("name is not empty");
  44. name = String::format("%s (Emulated)", name.characters());
  45. region->m_name = name;
  46. }
  47. region->m_data = (u8*)mmap_with_name(nullptr, size, prot, flags, fd, offset, name.is_empty() ? nullptr : name.characters());
  48. ASSERT(region->m_data != MAP_FAILED);
  49. return region;
  50. }
  51. MmapRegion::MmapRegion(u32 base, u32 size, int prot)
  52. : Region(base, size)
  53. {
  54. set_prot(prot);
  55. m_shadow_data = (u8*)malloc(size);
  56. memset(m_shadow_data, 1, size);
  57. }
  58. MmapRegion::~MmapRegion()
  59. {
  60. free(m_shadow_data);
  61. if (m_file_backed)
  62. munmap(m_data, size());
  63. else
  64. free(m_data);
  65. }
  66. ValueWithShadow<u8> MmapRegion::read8(FlatPtr offset)
  67. {
  68. if (!is_readable()) {
  69. reportln("8-bit read from unreadable MmapRegion @ {:p}", base() + offset);
  70. emulator().dump_backtrace();
  71. TODO();
  72. }
  73. if (is_malloc_block()) {
  74. if (auto* tracer = emulator().malloc_tracer())
  75. tracer->audit_read(*this, base() + offset, 1);
  76. }
  77. ASSERT(offset < size());
  78. return { *reinterpret_cast<const u8*>(m_data + offset), *reinterpret_cast<const u8*>(m_shadow_data + offset) };
  79. }
  80. ValueWithShadow<u16> MmapRegion::read16(u32 offset)
  81. {
  82. if (!is_readable()) {
  83. reportln("16-bit read from unreadable MmapRegion @ {:p}", base() + offset);
  84. emulator().dump_backtrace();
  85. TODO();
  86. }
  87. if (is_malloc_block()) {
  88. if (auto* tracer = emulator().malloc_tracer())
  89. tracer->audit_read(*this, base() + offset, 2);
  90. }
  91. ASSERT(offset + 1 < size());
  92. return { *reinterpret_cast<const u16*>(m_data + offset), *reinterpret_cast<const u16*>(m_shadow_data + offset) };
  93. }
  94. ValueWithShadow<u32> MmapRegion::read32(u32 offset)
  95. {
  96. if (!is_readable()) {
  97. reportln("32-bit read from unreadable MmapRegion @ {:p}", base() + offset);
  98. emulator().dump_backtrace();
  99. TODO();
  100. }
  101. if (is_malloc_block()) {
  102. if (auto* tracer = emulator().malloc_tracer())
  103. tracer->audit_read(*this, base() + offset, 4);
  104. }
  105. ASSERT(offset + 3 < size());
  106. return { *reinterpret_cast<const u32*>(m_data + offset), *reinterpret_cast<const u32*>(m_shadow_data + offset) };
  107. }
  108. ValueWithShadow<u64> MmapRegion::read64(u32 offset)
  109. {
  110. if (!is_readable()) {
  111. reportln("64-bit read from unreadable MmapRegion @ {:p}", base() + offset);
  112. emulator().dump_backtrace();
  113. TODO();
  114. }
  115. if (is_malloc_block()) {
  116. if (auto* tracer = emulator().malloc_tracer())
  117. tracer->audit_read(*this, base() + offset, 8);
  118. }
  119. ASSERT(offset + 7 < size());
  120. return { *reinterpret_cast<const u64*>(m_data + offset), *reinterpret_cast<const u64*>(m_shadow_data + offset) };
  121. }
  122. void MmapRegion::write8(u32 offset, ValueWithShadow<u8> value)
  123. {
  124. if (!is_writable()) {
  125. reportln("8-bit write from unwritable MmapRegion @ {:p}", base() + offset);
  126. emulator().dump_backtrace();
  127. TODO();
  128. }
  129. if (is_malloc_block()) {
  130. if (auto* tracer = emulator().malloc_tracer())
  131. tracer->audit_write(*this, base() + offset, 1);
  132. }
  133. ASSERT(offset < size());
  134. *reinterpret_cast<u8*>(m_data + offset) = value.value();
  135. *reinterpret_cast<u8*>(m_shadow_data + offset) = value.shadow();
  136. }
  137. void MmapRegion::write16(u32 offset, ValueWithShadow<u16> value)
  138. {
  139. if (!is_writable()) {
  140. reportln("16-bit write from unwritable 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_write(*this, base() + offset, 2);
  147. }
  148. ASSERT(offset + 1 < size());
  149. *reinterpret_cast<u16*>(m_data + offset) = value.value();
  150. *reinterpret_cast<u16*>(m_shadow_data + offset) = value.shadow();
  151. }
  152. void MmapRegion::write32(u32 offset, ValueWithShadow<u32> value)
  153. {
  154. if (!is_writable()) {
  155. reportln("32-bit write from unwritable MmapRegion @ {:p}", base() + offset);
  156. emulator().dump_backtrace();
  157. TODO();
  158. }
  159. if (is_malloc_block()) {
  160. if (auto* tracer = emulator().malloc_tracer())
  161. tracer->audit_write(*this, base() + offset, 4);
  162. }
  163. ASSERT(offset + 3 < size());
  164. ASSERT(m_data != m_shadow_data);
  165. *reinterpret_cast<u32*>(m_data + offset) = value.value();
  166. *reinterpret_cast<u32*>(m_shadow_data + offset) = value.shadow();
  167. }
  168. void MmapRegion::write64(u32 offset, ValueWithShadow<u64> value)
  169. {
  170. if (!is_writable()) {
  171. reportln("64-bit write from unwritable MmapRegion @ {:p}", base() + offset);
  172. emulator().dump_backtrace();
  173. TODO();
  174. }
  175. if (is_malloc_block()) {
  176. if (auto* tracer = emulator().malloc_tracer())
  177. tracer->audit_write(*this, base() + offset, 8);
  178. }
  179. ASSERT(offset + 7 < size());
  180. ASSERT(m_data != m_shadow_data);
  181. *reinterpret_cast<u64*>(m_data + offset) = value.value();
  182. *reinterpret_cast<u64*>(m_shadow_data + offset) = value.shadow();
  183. }
  184. void MmapRegion::set_prot(int prot)
  185. {
  186. set_readable(prot & PROT_READ);
  187. set_writable(prot & PROT_WRITE);
  188. set_executable(prot & PROT_EXEC);
  189. if (m_file_backed) {
  190. mprotect(m_data, size(), prot);
  191. }
  192. }
  193. }