MmapRegion.cpp 5.9 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 "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. bool MmapRegion::is_malloc_block() const
  62. {
  63. // FIXME: This is obviously incomplete!
  64. // We should somehow know which mmap regions are malloc blocks.
  65. return !m_file_backed;
  66. }
  67. ValueWithShadow<u8> MmapRegion::read8(FlatPtr offset)
  68. {
  69. if (!is_readable()) {
  70. warn() << "8-bit read from unreadable MmapRegion @ " << (const void*)(base() + offset);
  71. Emulator::the().dump_backtrace();
  72. TODO();
  73. }
  74. if (is_malloc_block()) {
  75. if (auto* tracer = Emulator::the().malloc_tracer())
  76. tracer->audit_read(base() + offset, 1);
  77. }
  78. ASSERT(offset < size());
  79. return { *reinterpret_cast<const u8*>(m_data + offset), *reinterpret_cast<const u8*>(m_shadow_data + offset) };
  80. }
  81. ValueWithShadow<u16> MmapRegion::read16(u32 offset)
  82. {
  83. if (!is_readable()) {
  84. warn() << "16-bit from unreadable MmapRegion @ " << (const void*)(base() + offset);
  85. Emulator::the().dump_backtrace();
  86. TODO();
  87. }
  88. if (is_malloc_block()) {
  89. if (auto* tracer = Emulator::the().malloc_tracer())
  90. tracer->audit_read(base() + offset, 2);
  91. }
  92. ASSERT(offset + 1 < size());
  93. return { *reinterpret_cast<const u16*>(m_data + offset), *reinterpret_cast<const u16*>(m_shadow_data + offset) };
  94. }
  95. ValueWithShadow<u32> MmapRegion::read32(u32 offset)
  96. {
  97. if (!is_readable()) {
  98. warn() << "32-bit read from unreadable MmapRegion @ " << (const void*)(base() + offset);
  99. Emulator::the().dump_backtrace();
  100. TODO();
  101. }
  102. if (is_malloc_block()) {
  103. if (auto* tracer = Emulator::the().malloc_tracer())
  104. tracer->audit_read(base() + offset, 4);
  105. }
  106. ASSERT(offset + 3 < size());
  107. return { *reinterpret_cast<const u32*>(m_data + offset), *reinterpret_cast<const u32*>(m_shadow_data + offset) };
  108. }
  109. void MmapRegion::write8(u32 offset, ValueWithShadow<u8> value)
  110. {
  111. if (!is_writable()) {
  112. warn() << "8-bit write to unreadable MmapRegion @ " << (const void*)(base() + offset);
  113. Emulator::the().dump_backtrace();
  114. TODO();
  115. }
  116. if (is_malloc_block()) {
  117. if (auto* tracer = Emulator::the().malloc_tracer())
  118. tracer->audit_write(base() + offset, 1);
  119. }
  120. ASSERT(offset < size());
  121. *reinterpret_cast<u8*>(m_data + offset) = value.value();
  122. *reinterpret_cast<u8*>(m_shadow_data + offset) = value.shadow();
  123. }
  124. void MmapRegion::write16(u32 offset, ValueWithShadow<u16> value)
  125. {
  126. if (!is_writable()) {
  127. warn() << "16-bit write to unreadable MmapRegion @ " << (const void*)(base() + offset);
  128. Emulator::the().dump_backtrace();
  129. TODO();
  130. }
  131. if (is_malloc_block()) {
  132. if (auto* tracer = Emulator::the().malloc_tracer())
  133. tracer->audit_write(base() + offset, 2);
  134. }
  135. ASSERT(offset + 1 < size());
  136. *reinterpret_cast<u16*>(m_data + offset) = value.value();
  137. *reinterpret_cast<u16*>(m_shadow_data + offset) = value.shadow();
  138. }
  139. void MmapRegion::write32(u32 offset, ValueWithShadow<u32> value)
  140. {
  141. if (!is_writable()) {
  142. warn() << "32-bit write to unreadable MmapRegion @ " << (const void*)(base() + offset);
  143. Emulator::the().dump_backtrace();
  144. TODO();
  145. }
  146. if (is_malloc_block()) {
  147. if (auto* tracer = Emulator::the().malloc_tracer())
  148. tracer->audit_write(base() + offset, 4);
  149. }
  150. ASSERT(offset + 3 < size());
  151. ASSERT(m_data != m_shadow_data);
  152. *reinterpret_cast<u32*>(m_data + offset) = value.value();
  153. *reinterpret_cast<u32*>(m_shadow_data + offset) = value.shadow();
  154. }
  155. }