SharedBufferRegion.cpp 4.0 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 "SharedBufferRegion.h"
  27. #include "Emulator.h"
  28. #include <Kernel/API/Syscall.h>
  29. #include <serenity.h>
  30. #include <string.h>
  31. #include <sys/mman.h>
  32. namespace UserspaceEmulator {
  33. NonnullOwnPtr<SharedBufferRegion> SharedBufferRegion::create_with_shbuf_id(u32 base, u32 size, int shbuf_id, u8* host_data)
  34. {
  35. return adopt_own(*new SharedBufferRegion(base, size, shbuf_id, host_data));
  36. }
  37. SharedBufferRegion::SharedBufferRegion(u32 base, u32 size, int shbuf_id, u8* host_data)
  38. : Region(base, size)
  39. , m_data(host_data)
  40. , m_shbuf_id(shbuf_id)
  41. {
  42. m_shadow_data = (u8*)malloc(size);
  43. memset(m_shadow_data, 1, size);
  44. }
  45. SharedBufferRegion::~SharedBufferRegion()
  46. {
  47. free(m_shadow_data);
  48. }
  49. ValueWithShadow<u8> SharedBufferRegion::read8(FlatPtr offset)
  50. {
  51. ASSERT(offset < size());
  52. return { *reinterpret_cast<const u8*>(m_data + offset), *reinterpret_cast<const u8*>(m_shadow_data + offset) };
  53. }
  54. ValueWithShadow<u16> SharedBufferRegion::read16(u32 offset)
  55. {
  56. ASSERT(offset + 1 < size());
  57. return { *reinterpret_cast<const u16*>(m_data + offset), *reinterpret_cast<const u16*>(m_shadow_data + offset) };
  58. }
  59. ValueWithShadow<u32> SharedBufferRegion::read32(u32 offset)
  60. {
  61. ASSERT(offset + 3 < size());
  62. return { *reinterpret_cast<const u32*>(m_data + offset), *reinterpret_cast<const u32*>(m_shadow_data + offset) };
  63. }
  64. ValueWithShadow<u64> SharedBufferRegion::read64(u32 offset)
  65. {
  66. ASSERT(offset + 7 < size());
  67. return { *reinterpret_cast<const u64*>(m_data + offset), *reinterpret_cast<const u64*>(m_shadow_data + offset) };
  68. }
  69. void SharedBufferRegion::write8(u32 offset, ValueWithShadow<u8> value)
  70. {
  71. ASSERT(offset < size());
  72. *reinterpret_cast<u8*>(m_data + offset) = value.value();
  73. *reinterpret_cast<u8*>(m_shadow_data + offset) = value.shadow();
  74. }
  75. void SharedBufferRegion::write16(u32 offset, ValueWithShadow<u16> value)
  76. {
  77. ASSERT(offset + 1 < size());
  78. *reinterpret_cast<u16*>(m_data + offset) = value.value();
  79. *reinterpret_cast<u16*>(m_shadow_data + offset) = value.shadow();
  80. }
  81. void SharedBufferRegion::write32(u32 offset, ValueWithShadow<u32> value)
  82. {
  83. ASSERT(offset + 3 < size());
  84. *reinterpret_cast<u32*>(m_data + offset) = value.value();
  85. *reinterpret_cast<u32*>(m_shadow_data + offset) = value.shadow();
  86. }
  87. void SharedBufferRegion::write64(u32 offset, ValueWithShadow<u64> value)
  88. {
  89. ASSERT(offset + 7 < size());
  90. *reinterpret_cast<u64*>(m_data + offset) = value.value();
  91. *reinterpret_cast<u64*>(m_shadow_data + offset) = value.shadow();
  92. }
  93. int SharedBufferRegion::allow_pid(pid_t pid)
  94. {
  95. return syscall(SC_shbuf_allow_pid, m_shbuf_id, pid);
  96. }
  97. int SharedBufferRegion::release()
  98. {
  99. return syscall(SC_shbuf_release, m_shbuf_id);
  100. }
  101. }