MmapRegion.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 <sys/mman.h>
  29. namespace UserspaceEmulator {
  30. NonnullOwnPtr<MmapRegion> MmapRegion::create_anonymous(u32 base, u32 size, u32 prot)
  31. {
  32. auto region = adopt_own(*new MmapRegion(base, size, prot));
  33. region->m_file_backed = false;
  34. region->m_data = (u8*)calloc(1, size);
  35. return region;
  36. }
  37. NonnullOwnPtr<MmapRegion> MmapRegion::create_file_backed(u32 base, u32 size, u32 prot, int flags, int fd, off_t offset)
  38. {
  39. auto region = adopt_own(*new MmapRegion(base, size, prot));
  40. region->m_file_backed = true;
  41. region->m_data = (u8*)mmap(nullptr, size, prot, flags, fd, offset);
  42. ASSERT(region->m_data != MAP_FAILED);
  43. return region;
  44. }
  45. MmapRegion::MmapRegion(u32 base, u32 size, int prot)
  46. : Region(base, size)
  47. , m_prot(prot)
  48. {
  49. m_shadow_data = (u8*)calloc(1, size);
  50. }
  51. MmapRegion::~MmapRegion()
  52. {
  53. free(m_shadow_data);
  54. if (m_file_backed)
  55. munmap(m_data, size());
  56. else
  57. free(m_data);
  58. }
  59. bool MmapRegion::is_malloc_block() const
  60. {
  61. // FIXME: This is obviously incomplete!
  62. // We should somehow know which mmap regions are malloc blocks.
  63. return !m_file_backed;
  64. }
  65. ValueWithShadow<u8> MmapRegion::read8(FlatPtr offset)
  66. {
  67. if (!is_readable()) {
  68. warn() << "8-bit read from unreadable MmapRegion @ " << (const void*)(base() + offset);
  69. Emulator::the().dump_backtrace();
  70. TODO();
  71. }
  72. if (is_malloc_block()) {
  73. if (auto* tracer = Emulator::the().malloc_tracer())
  74. tracer->audit_read(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. warn() << "16-bit from unreadable MmapRegion @ " << (const void*)(base() + offset);
  83. Emulator::the().dump_backtrace();
  84. TODO();
  85. }
  86. if (is_malloc_block()) {
  87. if (auto* tracer = Emulator::the().malloc_tracer())
  88. tracer->audit_read(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. warn() << "32-bit read from unreadable MmapRegion @ " << (const void*)(base() + offset);
  97. Emulator::the().dump_backtrace();
  98. TODO();
  99. }
  100. if (is_malloc_block()) {
  101. if (auto* tracer = Emulator::the().malloc_tracer())
  102. tracer->audit_read(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. void MmapRegion::write8(u32 offset, ValueWithShadow<u8> value)
  108. {
  109. if (!is_writable()) {
  110. warn() << "8-bit write to unreadable MmapRegion @ " << (const void*)(base() + offset);
  111. Emulator::the().dump_backtrace();
  112. TODO();
  113. }
  114. if (is_malloc_block()) {
  115. if (auto* tracer = Emulator::the().malloc_tracer())
  116. tracer->audit_write(base() + offset, 1);
  117. }
  118. ASSERT(offset < size());
  119. *reinterpret_cast<u8*>(m_data + offset) = value.value();
  120. *reinterpret_cast<u8*>(m_shadow_data + offset) = value.shadow();
  121. }
  122. void MmapRegion::write16(u32 offset, ValueWithShadow<u16> value)
  123. {
  124. if (!is_writable()) {
  125. warn() << "16-bit write to unreadable MmapRegion @ " << (const void*)(base() + offset);
  126. Emulator::the().dump_backtrace();
  127. TODO();
  128. }
  129. if (is_malloc_block()) {
  130. if (auto* tracer = Emulator::the().malloc_tracer())
  131. tracer->audit_write(base() + offset, 2);
  132. }
  133. ASSERT(offset + 1 < size());
  134. *reinterpret_cast<u16*>(m_data + offset) = value.value();
  135. *reinterpret_cast<u16*>(m_shadow_data + offset) = value.shadow();
  136. }
  137. void MmapRegion::write32(u32 offset, ValueWithShadow<u32> value)
  138. {
  139. if (!is_writable()) {
  140. warn() << "32-bit write to unreadable MmapRegion @ " << (const void*)(base() + offset);
  141. Emulator::the().dump_backtrace();
  142. TODO();
  143. }
  144. if (is_malloc_block()) {
  145. if (auto* tracer = Emulator::the().malloc_tracer())
  146. tracer->audit_write(base() + offset, 4);
  147. }
  148. ASSERT(offset + 3 < size());
  149. ASSERT(m_data != m_shadow_data);
  150. *reinterpret_cast<u32*>(m_data + offset) = value.value();
  151. *reinterpret_cast<u32*>(m_shadow_data + offset) = value.shadow();
  152. }
  153. }