SharedBufferRegion.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. }
  42. SharedBufferRegion::~SharedBufferRegion()
  43. {
  44. }
  45. u8 SharedBufferRegion::read8(FlatPtr offset)
  46. {
  47. ASSERT(offset < size());
  48. return *reinterpret_cast<const u8*>(m_data + offset);
  49. }
  50. u16 SharedBufferRegion::read16(u32 offset)
  51. {
  52. ASSERT(offset + 1 < size());
  53. return *reinterpret_cast<const u16*>(m_data + offset);
  54. }
  55. u32 SharedBufferRegion::read32(u32 offset)
  56. {
  57. ASSERT(offset + 3 < size());
  58. return *reinterpret_cast<const u32*>(m_data + offset);
  59. }
  60. void SharedBufferRegion::write8(u32 offset, u8 value)
  61. {
  62. ASSERT(offset < size());
  63. *reinterpret_cast<u8*>(m_data + offset) = value;
  64. }
  65. void SharedBufferRegion::write16(u32 offset, u16 value)
  66. {
  67. ASSERT(offset + 1 < size());
  68. *reinterpret_cast<u16*>(m_data + offset) = value;
  69. }
  70. void SharedBufferRegion::write32(u32 offset, u32 value)
  71. {
  72. ASSERT(offset + 3 < size());
  73. *reinterpret_cast<u32*>(m_data + offset) = value;
  74. }
  75. int SharedBufferRegion::allow_all()
  76. {
  77. return syscall(SC_shbuf_allow_all, m_shbuf_id);
  78. }
  79. int SharedBufferRegion::allow_pid(pid_t pid)
  80. {
  81. return syscall(SC_shbuf_allow_pid, m_shbuf_id, pid);
  82. }
  83. int SharedBufferRegion::seal()
  84. {
  85. return syscall(SC_shbuf_seal, m_shbuf_id);
  86. }
  87. int SharedBufferRegion::release()
  88. {
  89. return syscall(SC_shbuf_release, m_shbuf_id);
  90. }
  91. int SharedBufferRegion::set_volatile(bool is_volatile)
  92. {
  93. return syscall(SC_shbuf_set_volatile, m_shbuf_id, is_volatile);
  94. }
  95. }