SoftMMU.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 "SharedBufferRegion.h"
  28. #include <AK/ByteBuffer.h>
  29. namespace UserspaceEmulator {
  30. SoftMMU::Region* SoftMMU::find_region(X86::LogicalAddress address)
  31. {
  32. if (address.selector() == 0x28)
  33. return m_tls_region.ptr();
  34. for (auto& region : m_regions) {
  35. if (region.contains(address.offset()))
  36. return &region;
  37. }
  38. return nullptr;
  39. }
  40. void SoftMMU::add_region(NonnullOwnPtr<Region> region)
  41. {
  42. ASSERT(!find_region({ 0x20, region->base() }));
  43. // FIXME: More sanity checks pls
  44. if (region->is_shared_buffer())
  45. m_shbuf_regions.set(static_cast<SharedBufferRegion*>(region.ptr())->shbuf_id(), region.ptr());
  46. m_regions.append(move(region));
  47. }
  48. void SoftMMU::remove_region(Region& region)
  49. {
  50. if (region.is_shared_buffer())
  51. m_shbuf_regions.remove(static_cast<SharedBufferRegion&>(region).shbuf_id());
  52. m_regions.remove_first_matching([&](auto& entry) { return entry.ptr() == &region; });
  53. }
  54. void SoftMMU::set_tls_region(NonnullOwnPtr<Region> region)
  55. {
  56. ASSERT(!m_tls_region);
  57. m_tls_region = move(region);
  58. }
  59. ValueWithShadow<u8> SoftMMU::read8(X86::LogicalAddress address)
  60. {
  61. auto* region = find_region(address);
  62. if (!region) {
  63. warnln("SoftMMU::read8: No region for @ {:p}", address.offset());
  64. TODO();
  65. }
  66. return region->read8(address.offset() - region->base());
  67. }
  68. ValueWithShadow<u16> SoftMMU::read16(X86::LogicalAddress address)
  69. {
  70. auto* region = find_region(address);
  71. if (!region) {
  72. warnln("SoftMMU::read16: No region for @ {:p}", address.offset());
  73. TODO();
  74. }
  75. return region->read16(address.offset() - region->base());
  76. }
  77. ValueWithShadow<u32> SoftMMU::read32(X86::LogicalAddress address)
  78. {
  79. auto* region = find_region(address);
  80. if (!region) {
  81. warnln("SoftMMU::read32: No region for @ {:p}", address.offset());
  82. TODO();
  83. }
  84. return region->read32(address.offset() - region->base());
  85. }
  86. ValueWithShadow<u64> SoftMMU::read64(X86::LogicalAddress address)
  87. {
  88. auto* region = find_region(address);
  89. if (!region) {
  90. warnln("SoftMMU::read64: No region for @ {:p}", address.offset());
  91. TODO();
  92. }
  93. return region->read64(address.offset() - region->base());
  94. }
  95. void SoftMMU::write8(X86::LogicalAddress address, ValueWithShadow<u8> value)
  96. {
  97. auto* region = find_region(address);
  98. if (!region) {
  99. warnln("SoftMMU::write8: No region for @ {:p}", address.offset());
  100. TODO();
  101. }
  102. region->write8(address.offset() - region->base(), value);
  103. }
  104. void SoftMMU::write16(X86::LogicalAddress address, ValueWithShadow<u16> value)
  105. {
  106. auto* region = find_region(address);
  107. if (!region) {
  108. warnln("SoftMMU::write16: No region for @ {:p}", address.offset());
  109. TODO();
  110. }
  111. region->write16(address.offset() - region->base(), value);
  112. }
  113. void SoftMMU::write32(X86::LogicalAddress address, ValueWithShadow<u32> value)
  114. {
  115. auto* region = find_region(address);
  116. if (!region) {
  117. warnln("SoftMMU::write32: No region for @ {:p}", address.offset());
  118. TODO();
  119. }
  120. region->write32(address.offset() - region->base(), value);
  121. }
  122. void SoftMMU::write64(X86::LogicalAddress address, ValueWithShadow<u64> value)
  123. {
  124. auto* region = find_region(address);
  125. if (!region) {
  126. warnln("SoftMMU::write64: No region for @ {:p}", address.offset());
  127. TODO();
  128. }
  129. region->write64(address.offset() - region->base(), value);
  130. }
  131. void SoftMMU::copy_to_vm(FlatPtr destination, const void* source, size_t size)
  132. {
  133. // FIXME: We should have a way to preserve the shadow data here as well.
  134. for (size_t i = 0; i < size; ++i)
  135. write8({ 0x20, destination + i }, shadow_wrap_as_initialized(((const u8*)source)[i]));
  136. }
  137. void SoftMMU::copy_from_vm(void* destination, const FlatPtr source, size_t size)
  138. {
  139. // FIXME: We should have a way to preserve the shadow data here as well.
  140. for (size_t i = 0; i < size; ++i)
  141. ((u8*)destination)[i] = read8({ 0x20, source + i }).value();
  142. }
  143. ByteBuffer SoftMMU::copy_buffer_from_vm(const FlatPtr source, size_t size)
  144. {
  145. auto buffer = ByteBuffer::create_uninitialized(size);
  146. copy_from_vm(buffer.data(), source, size);
  147. return buffer;
  148. }
  149. SharedBufferRegion* SoftMMU::shbuf_region(int shbuf_id)
  150. {
  151. return (SharedBufferRegion*)m_shbuf_regions.get(shbuf_id).value_or(nullptr);
  152. }
  153. }