SoftMMU.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 "SoftMMU.h"
  27. #include "Emulator.h"
  28. #include "MmapRegion.h"
  29. #include "Report.h"
  30. #include "SharedBufferRegion.h"
  31. #include <AK/ByteBuffer.h>
  32. #include <AK/Memory.h>
  33. namespace UserspaceEmulator {
  34. SoftMMU::SoftMMU(Emulator& emulator)
  35. : m_emulator(emulator)
  36. {
  37. }
  38. void SoftMMU::add_region(NonnullOwnPtr<Region> region)
  39. {
  40. ASSERT(!find_region({ 0x20, region->base() }));
  41. // FIXME: More sanity checks pls
  42. if (region->is_shared_buffer())
  43. m_shbuf_regions.set(static_cast<SharedBufferRegion*>(region.ptr())->shbuf_id(), region.ptr());
  44. size_t first_page_in_region = region->base() / PAGE_SIZE;
  45. for (size_t i = 0; i < ceil_div(region->size(), PAGE_SIZE); ++i) {
  46. m_page_to_region_map[first_page_in_region + i] = region.ptr();
  47. }
  48. m_regions.append(move(region));
  49. }
  50. void SoftMMU::remove_region(Region& region)
  51. {
  52. size_t first_page_in_region = region.base() / PAGE_SIZE;
  53. for (size_t i = 0; i < ceil_div(region.size(), PAGE_SIZE); ++i) {
  54. m_page_to_region_map[first_page_in_region + i] = nullptr;
  55. }
  56. if (region.is_shared_buffer())
  57. m_shbuf_regions.remove(static_cast<SharedBufferRegion&>(region).shbuf_id());
  58. m_regions.remove_first_matching([&](auto& entry) { return entry.ptr() == &region; });
  59. }
  60. void SoftMMU::set_tls_region(NonnullOwnPtr<Region> region)
  61. {
  62. ASSERT(!m_tls_region);
  63. m_tls_region = move(region);
  64. }
  65. ValueWithShadow<u8> SoftMMU::read8(X86::LogicalAddress address)
  66. {
  67. auto* region = find_region(address);
  68. if (!region) {
  69. reportln("SoftMMU::read8: No region for @ {:p}", address.offset());
  70. m_emulator.dump_backtrace();
  71. TODO();
  72. }
  73. if (!region->is_readable()) {
  74. reportln("SoftMMU::read8: Non-readable region @ {:p}", address.offset());
  75. m_emulator.dump_backtrace();
  76. TODO();
  77. }
  78. return region->read8(address.offset() - region->base());
  79. }
  80. ValueWithShadow<u16> SoftMMU::read16(X86::LogicalAddress address)
  81. {
  82. auto* region = find_region(address);
  83. if (!region) {
  84. reportln("SoftMMU::read16: No region for @ {:p}", address.offset());
  85. m_emulator.dump_backtrace();
  86. TODO();
  87. }
  88. if (!region->is_readable()) {
  89. reportln("SoftMMU::read16: Non-readable region @ {:p}", address.offset());
  90. m_emulator.dump_backtrace();
  91. TODO();
  92. }
  93. return region->read16(address.offset() - region->base());
  94. }
  95. ValueWithShadow<u32> SoftMMU::read32(X86::LogicalAddress address)
  96. {
  97. auto* region = find_region(address);
  98. if (!region) {
  99. reportln("SoftMMU::read32: No region for @ {:p}", address.offset());
  100. m_emulator.dump_backtrace();
  101. TODO();
  102. }
  103. if (!region->is_readable()) {
  104. reportln("SoftMMU::read32: Non-readable region @ {:p}", address.offset());
  105. m_emulator.dump_backtrace();
  106. TODO();
  107. }
  108. return region->read32(address.offset() - region->base());
  109. }
  110. ValueWithShadow<u64> SoftMMU::read64(X86::LogicalAddress address)
  111. {
  112. auto* region = find_region(address);
  113. if (!region) {
  114. reportln("SoftMMU::read64: No region for @ {:p}", address.offset());
  115. m_emulator.dump_backtrace();
  116. TODO();
  117. }
  118. if (!region->is_readable()) {
  119. reportln("SoftMMU::read64: Non-readable region @ {:p}", address.offset());
  120. m_emulator.dump_backtrace();
  121. TODO();
  122. }
  123. return region->read64(address.offset() - region->base());
  124. }
  125. void SoftMMU::write8(X86::LogicalAddress address, ValueWithShadow<u8> value)
  126. {
  127. auto* region = find_region(address);
  128. if (!region) {
  129. reportln("SoftMMU::write8: No region for @ {:p}", address.offset());
  130. m_emulator.dump_backtrace();
  131. TODO();
  132. }
  133. if (!region->is_writable()) {
  134. reportln("SoftMMU::write8: Non-writable region @ {:p}", address.offset());
  135. m_emulator.dump_backtrace();
  136. TODO();
  137. }
  138. region->write8(address.offset() - region->base(), value);
  139. }
  140. void SoftMMU::write16(X86::LogicalAddress address, ValueWithShadow<u16> value)
  141. {
  142. auto* region = find_region(address);
  143. if (!region) {
  144. reportln("SoftMMU::write16: No region for @ {:p}", address.offset());
  145. m_emulator.dump_backtrace();
  146. TODO();
  147. }
  148. if (!region->is_writable()) {
  149. reportln("SoftMMU::write16: Non-writable region @ {:p}", address.offset());
  150. m_emulator.dump_backtrace();
  151. TODO();
  152. }
  153. region->write16(address.offset() - region->base(), value);
  154. }
  155. void SoftMMU::write32(X86::LogicalAddress address, ValueWithShadow<u32> value)
  156. {
  157. auto* region = find_region(address);
  158. if (!region) {
  159. reportln("SoftMMU::write32: No region for @ {:p}", address.offset());
  160. m_emulator.dump_backtrace();
  161. TODO();
  162. }
  163. if (!region->is_writable()) {
  164. reportln("SoftMMU::write32: Non-writable region @ {:p}", address.offset());
  165. m_emulator.dump_backtrace();
  166. TODO();
  167. }
  168. region->write32(address.offset() - region->base(), value);
  169. }
  170. void SoftMMU::write64(X86::LogicalAddress address, ValueWithShadow<u64> value)
  171. {
  172. auto* region = find_region(address);
  173. if (!region) {
  174. reportln("SoftMMU::write64: No region for @ {:p}", address.offset());
  175. m_emulator.dump_backtrace();
  176. TODO();
  177. }
  178. if (!region->is_writable()) {
  179. reportln("SoftMMU::write64: Non-writable region @ {:p}", address.offset());
  180. m_emulator.dump_backtrace();
  181. TODO();
  182. }
  183. region->write64(address.offset() - region->base(), value);
  184. }
  185. void SoftMMU::copy_to_vm(FlatPtr destination, const void* source, size_t size)
  186. {
  187. // FIXME: We should have a way to preserve the shadow data here as well.
  188. for (size_t i = 0; i < size; ++i)
  189. write8({ 0x20, destination + i }, shadow_wrap_as_initialized(((const u8*)source)[i]));
  190. }
  191. void SoftMMU::copy_from_vm(void* destination, const FlatPtr source, size_t size)
  192. {
  193. // FIXME: We should have a way to preserve the shadow data here as well.
  194. for (size_t i = 0; i < size; ++i)
  195. ((u8*)destination)[i] = read8({ 0x20, source + i }).value();
  196. }
  197. ByteBuffer SoftMMU::copy_buffer_from_vm(const FlatPtr source, size_t size)
  198. {
  199. auto buffer = ByteBuffer::create_uninitialized(size);
  200. copy_from_vm(buffer.data(), source, size);
  201. return buffer;
  202. }
  203. SharedBufferRegion* SoftMMU::shbuf_region(int shbuf_id)
  204. {
  205. return (SharedBufferRegion*)m_shbuf_regions.get(shbuf_id).value_or(nullptr);
  206. }
  207. bool SoftMMU::fast_fill_memory8(X86::LogicalAddress address, size_t size, ValueWithShadow<u8> value)
  208. {
  209. if (!size)
  210. return true;
  211. auto* region = find_region(address);
  212. if (!region)
  213. return false;
  214. if (!region->contains(address.offset() + size - 1))
  215. return false;
  216. if (region->is_mmap() && static_cast<const MmapRegion&>(*region).is_malloc_block()) {
  217. if (auto* tracer = m_emulator.malloc_tracer()) {
  218. // FIXME: Add a way to audit an entire range of memory instead of looping here!
  219. for (size_t i = 0; i < size; ++i) {
  220. tracer->audit_write(*region, address.offset() + (i * sizeof(u8)), sizeof(u8));
  221. }
  222. }
  223. }
  224. size_t offset_in_region = address.offset() - region->base();
  225. memset(region->data() + offset_in_region, value.value(), size);
  226. memset(region->shadow_data() + offset_in_region, value.shadow(), size);
  227. return true;
  228. }
  229. bool SoftMMU::fast_fill_memory32(X86::LogicalAddress address, size_t count, ValueWithShadow<u32> value)
  230. {
  231. if (!count)
  232. return true;
  233. auto* region = find_region(address);
  234. if (!region)
  235. return false;
  236. if (!region->contains(address.offset() + (count * sizeof(u32)) - 1))
  237. return false;
  238. if (region->is_mmap() && static_cast<const MmapRegion&>(*region).is_malloc_block()) {
  239. if (auto* tracer = m_emulator.malloc_tracer()) {
  240. // FIXME: Add a way to audit an entire range of memory instead of looping here!
  241. for (size_t i = 0; i < count; ++i) {
  242. tracer->audit_write(*region, address.offset() + (i * sizeof(u32)), sizeof(u32));
  243. }
  244. }
  245. }
  246. size_t offset_in_region = address.offset() - region->base();
  247. fast_u32_fill((u32*)(region->data() + offset_in_region), value.value(), count);
  248. fast_u32_fill((u32*)(region->shadow_data() + offset_in_region), value.shadow(), count);
  249. return true;
  250. }
  251. }