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