MmapRegion.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. MmapRegion::MmapRegion(u32 base, u32 size, int prot)
  31. : Region(base, size)
  32. , m_prot(prot)
  33. {
  34. m_data = (u8*)calloc(1, size);
  35. }
  36. MmapRegion::~MmapRegion()
  37. {
  38. free(m_data);
  39. }
  40. u8 MmapRegion::read8(FlatPtr offset)
  41. {
  42. if (!is_readable()) {
  43. warn() << "8-bit read from unreadable MmapRegion @ " << (const void*)(base() + offset);
  44. Emulator::the().dump_backtrace();
  45. TODO();
  46. }
  47. ASSERT(offset < size());
  48. return *reinterpret_cast<const u8*>(m_data + offset);
  49. }
  50. u16 MmapRegion::read16(u32 offset)
  51. {
  52. if (!is_readable()) {
  53. warn() << "16-bit from unreadable MmapRegion @ " << (const void*)(base() + offset);
  54. Emulator::the().dump_backtrace();
  55. TODO();
  56. }
  57. ASSERT(offset + 1 < size());
  58. return *reinterpret_cast<const u16*>(m_data + offset);
  59. }
  60. u32 MmapRegion::read32(u32 offset)
  61. {
  62. if (!is_readable()) {
  63. warn() << "32-bit read from unreadable MmapRegion @ " << (const void*)(base() + offset);
  64. Emulator::the().dump_backtrace();
  65. TODO();
  66. }
  67. ASSERT(offset + 3 < size());
  68. return *reinterpret_cast<const u32*>(m_data + offset);
  69. }
  70. void MmapRegion::write8(u32 offset, u8 value)
  71. {
  72. if (!is_writable()) {
  73. warn() << "8-bit write to unreadable MmapRegion @ " << (const void*)(base() + offset);
  74. Emulator::the().dump_backtrace();
  75. TODO();
  76. }
  77. ASSERT(offset < size());
  78. *reinterpret_cast<u8*>(m_data + offset) = value;
  79. }
  80. void MmapRegion::write16(u32 offset, u16 value)
  81. {
  82. if (!is_writable()) {
  83. warn() << "16-bit write to unreadable MmapRegion @ " << (const void*)(base() + offset);
  84. Emulator::the().dump_backtrace();
  85. TODO();
  86. }
  87. ASSERT(offset + 1 < size());
  88. *reinterpret_cast<u16*>(m_data + offset) = value;
  89. }
  90. void MmapRegion::write32(u32 offset, u32 value)
  91. {
  92. if (!is_writable()) {
  93. warn() << "32-bit write to unreadable MmapRegion @ " << (const void*)(base() + offset);
  94. Emulator::the().dump_backtrace();
  95. TODO();
  96. }
  97. ASSERT(offset + 3 < size());
  98. *reinterpret_cast<u32*>(m_data + offset) = value;
  99. }
  100. }