SimpleRegion.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 "SimpleRegion.h"
  27. #include <string.h>
  28. namespace UserspaceEmulator {
  29. SimpleRegion::SimpleRegion(u32 base, u32 size)
  30. : Region(base, size)
  31. {
  32. m_data = (u8*)calloc(1, size);
  33. m_shadow_data = (u8*)malloc(size);
  34. memset(m_shadow_data, 1, size);
  35. }
  36. SimpleRegion::~SimpleRegion()
  37. {
  38. free(m_shadow_data);
  39. free(m_data);
  40. }
  41. ValueWithShadow<u8> SimpleRegion::read8(FlatPtr offset)
  42. {
  43. ASSERT(offset < size());
  44. return { *reinterpret_cast<const u8*>(m_data + offset), *reinterpret_cast<const u8*>(m_shadow_data + offset) };
  45. }
  46. ValueWithShadow<u16> SimpleRegion::read16(u32 offset)
  47. {
  48. ASSERT(offset + 1 < size());
  49. return { *reinterpret_cast<const u16*>(m_data + offset), *reinterpret_cast<const u16*>(m_shadow_data + offset) };
  50. }
  51. ValueWithShadow<u32> SimpleRegion::read32(u32 offset)
  52. {
  53. ASSERT(offset + 3 < size());
  54. return { *reinterpret_cast<const u32*>(m_data + offset), *reinterpret_cast<const u32*>(m_shadow_data + offset) };
  55. }
  56. ValueWithShadow<u64> SimpleRegion::read64(u32 offset)
  57. {
  58. ASSERT(offset + 7 < size());
  59. return { *reinterpret_cast<const u64*>(m_data + offset), *reinterpret_cast<const u64*>(m_shadow_data + offset) };
  60. }
  61. void SimpleRegion::write8(u32 offset, ValueWithShadow<u8> value)
  62. {
  63. ASSERT(offset < size());
  64. *reinterpret_cast<u8*>(m_data + offset) = value.value();
  65. *reinterpret_cast<u8*>(m_shadow_data + offset) = value.shadow();
  66. }
  67. void SimpleRegion::write16(u32 offset, ValueWithShadow<u16> value)
  68. {
  69. ASSERT(offset + 1 < size());
  70. *reinterpret_cast<u16*>(m_data + offset) = value.value();
  71. *reinterpret_cast<u16*>(m_shadow_data + offset) = value.shadow();
  72. }
  73. void SimpleRegion::write32(u32 offset, ValueWithShadow<u32> value)
  74. {
  75. ASSERT(offset + 3 < size());
  76. *reinterpret_cast<u32*>(m_data + offset) = value.value();
  77. *reinterpret_cast<u32*>(m_shadow_data + offset) = value.shadow();
  78. }
  79. void SimpleRegion::write64(u32 offset, ValueWithShadow<u64> value)
  80. {
  81. ASSERT(offset + 7 < size());
  82. *reinterpret_cast<u64*>(m_data + offset) = value.value();
  83. *reinterpret_cast<u64*>(m_shadow_data + offset) = value.shadow();
  84. }
  85. u8* SimpleRegion::cacheable_ptr(u32 offset)
  86. {
  87. return m_data + offset;
  88. }
  89. }