MmapRegion.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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)
  39. {
  40. auto region = adopt_own(*new MmapRegion(base, size, prot));
  41. region->m_file_backed = true;
  42. region->m_data = (u8*)mmap(nullptr, size, prot, flags, fd, offset);
  43. ASSERT(region->m_data != MAP_FAILED);
  44. return region;
  45. }
  46. MmapRegion::MmapRegion(u32 base, u32 size, int prot)
  47. : Region(base, size)
  48. , m_prot(prot)
  49. {
  50. m_shadow_data = (u8*)malloc(size);
  51. memset(m_shadow_data, 1, size);
  52. }
  53. MmapRegion::~MmapRegion()
  54. {
  55. free(m_shadow_data);
  56. if (m_file_backed)
  57. munmap(m_data, size());
  58. else
  59. free(m_data);
  60. }
  61. ValueWithShadow<u8> MmapRegion::read8(FlatPtr offset)
  62. {
  63. if (!is_readable()) {
  64. warnln("8-bit read from unreadable MmapRegion @ {:p}", base() + offset);
  65. Emulator::the().dump_backtrace();
  66. TODO();
  67. }
  68. if (is_malloc_block()) {
  69. if (auto* tracer = Emulator::the().malloc_tracer())
  70. tracer->audit_read(base() + offset, 1);
  71. }
  72. ASSERT(offset < size());
  73. return { *reinterpret_cast<const u8*>(m_data + offset), *reinterpret_cast<const u8*>(m_shadow_data + offset) };
  74. }
  75. ValueWithShadow<u16> MmapRegion::read16(u32 offset)
  76. {
  77. if (!is_readable()) {
  78. warnln("16-bit read from unreadable MmapRegion @ {:p}", base() + offset);
  79. Emulator::the().dump_backtrace();
  80. TODO();
  81. }
  82. if (is_malloc_block()) {
  83. if (auto* tracer = Emulator::the().malloc_tracer())
  84. tracer->audit_read(base() + offset, 2);
  85. }
  86. ASSERT(offset + 1 < size());
  87. return { *reinterpret_cast<const u16*>(m_data + offset), *reinterpret_cast<const u16*>(m_shadow_data + offset) };
  88. }
  89. ValueWithShadow<u32> MmapRegion::read32(u32 offset)
  90. {
  91. if (!is_readable()) {
  92. warnln("32-bit read from unreadable MmapRegion @ {:p}", base() + offset);
  93. Emulator::the().dump_backtrace();
  94. TODO();
  95. }
  96. if (is_malloc_block()) {
  97. if (auto* tracer = Emulator::the().malloc_tracer())
  98. tracer->audit_read(base() + offset, 4);
  99. }
  100. ASSERT(offset + 3 < size());
  101. return { *reinterpret_cast<const u32*>(m_data + offset), *reinterpret_cast<const u32*>(m_shadow_data + offset) };
  102. }
  103. ValueWithShadow<u64> MmapRegion::read64(u32 offset)
  104. {
  105. if (!is_readable()) {
  106. warnln("64-bit read from unreadable MmapRegion @ {:p}", base() + offset);
  107. Emulator::the().dump_backtrace();
  108. TODO();
  109. }
  110. if (is_malloc_block()) {
  111. if (auto* tracer = Emulator::the().malloc_tracer())
  112. tracer->audit_read(base() + offset, 8);
  113. }
  114. ASSERT(offset + 7 < size());
  115. return { *reinterpret_cast<const u64*>(m_data + offset), *reinterpret_cast<const u64*>(m_shadow_data + offset) };
  116. }
  117. void MmapRegion::write8(u32 offset, ValueWithShadow<u8> value)
  118. {
  119. if (!is_writable()) {
  120. warnln("8-bit write from unwritable MmapRegion @ {:p}", base() + offset);
  121. Emulator::the().dump_backtrace();
  122. TODO();
  123. }
  124. if (is_malloc_block()) {
  125. if (auto* tracer = Emulator::the().malloc_tracer())
  126. tracer->audit_write(base() + offset, 1);
  127. }
  128. ASSERT(offset < size());
  129. *reinterpret_cast<u8*>(m_data + offset) = value.value();
  130. *reinterpret_cast<u8*>(m_shadow_data + offset) = value.shadow();
  131. }
  132. void MmapRegion::write16(u32 offset, ValueWithShadow<u16> value)
  133. {
  134. if (!is_writable()) {
  135. warnln("16-bit write from unwritable MmapRegion @ {:p}", base() + offset);
  136. Emulator::the().dump_backtrace();
  137. TODO();
  138. }
  139. if (is_malloc_block()) {
  140. if (auto* tracer = Emulator::the().malloc_tracer())
  141. tracer->audit_write(base() + offset, 2);
  142. }
  143. ASSERT(offset + 1 < size());
  144. *reinterpret_cast<u16*>(m_data + offset) = value.value();
  145. *reinterpret_cast<u16*>(m_shadow_data + offset) = value.shadow();
  146. }
  147. void MmapRegion::write32(u32 offset, ValueWithShadow<u32> value)
  148. {
  149. if (!is_writable()) {
  150. warnln("32-bit write from unwritable MmapRegion @ {:p}", base() + offset);
  151. Emulator::the().dump_backtrace();
  152. TODO();
  153. }
  154. if (is_malloc_block()) {
  155. if (auto* tracer = Emulator::the().malloc_tracer())
  156. tracer->audit_write(base() + offset, 4);
  157. }
  158. ASSERT(offset + 3 < size());
  159. ASSERT(m_data != m_shadow_data);
  160. *reinterpret_cast<u32*>(m_data + offset) = value.value();
  161. *reinterpret_cast<u32*>(m_shadow_data + offset) = value.shadow();
  162. }
  163. void MmapRegion::write64(u32 offset, ValueWithShadow<u64> value)
  164. {
  165. if (!is_writable()) {
  166. warnln("64-bit write from unwritable MmapRegion @ {:p}", base() + offset);
  167. Emulator::the().dump_backtrace();
  168. TODO();
  169. }
  170. if (is_malloc_block()) {
  171. if (auto* tracer = Emulator::the().malloc_tracer())
  172. tracer->audit_write(base() + offset, 8);
  173. }
  174. ASSERT(offset + 7 < size());
  175. ASSERT(m_data != m_shadow_data);
  176. *reinterpret_cast<u64*>(m_data + offset) = value.value();
  177. *reinterpret_cast<u64*>(m_shadow_data + offset) = value.shadow();
  178. }
  179. }