SoftMMU.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #pragma once
  27. #include "ValueWithShadow.h"
  28. #include <AK/HashMap.h>
  29. #include <AK/NonnullOwnPtrVector.h>
  30. #include <AK/OwnPtr.h>
  31. #include <AK/Types.h>
  32. #include <LibX86/Instruction.h>
  33. namespace UserspaceEmulator {
  34. class SharedBufferRegion;
  35. class SoftMMU {
  36. public:
  37. class Region {
  38. public:
  39. virtual ~Region() { }
  40. u32 base() const { return m_base; }
  41. u32 size() const { return m_size; }
  42. u32 end() const { return m_base + m_size; }
  43. bool contains(u32 address) const { return address >= base() && address < end(); }
  44. virtual void write8(u32 offset, ValueWithShadow<u8>) = 0;
  45. virtual void write16(u32 offset, ValueWithShadow<u16>) = 0;
  46. virtual void write32(u32 offset, ValueWithShadow<u32>) = 0;
  47. virtual void write64(u32 offset, ValueWithShadow<u64>) = 0;
  48. virtual ValueWithShadow<u8> read8(u32 offset) = 0;
  49. virtual ValueWithShadow<u16> read16(u32 offset) = 0;
  50. virtual ValueWithShadow<u32> read32(u32 offset) = 0;
  51. virtual ValueWithShadow<u64> read64(u32 offset) = 0;
  52. virtual u8* cacheable_ptr([[maybe_unused]] u32 offset) { return nullptr; }
  53. virtual bool is_shared_buffer() const { return false; }
  54. virtual bool is_mmap() const { return false; }
  55. bool is_stack() const { return m_stack; }
  56. void set_stack(bool b) { m_stack = b; }
  57. bool is_text() const { return m_text; }
  58. void set_text(bool b) { m_text = b; }
  59. protected:
  60. Region(u32 base, u32 size)
  61. : m_base(base)
  62. , m_size(size)
  63. {
  64. }
  65. private:
  66. u32 m_base { 0 };
  67. u32 m_size { 0 };
  68. bool m_stack { false };
  69. bool m_text { false };
  70. };
  71. ValueWithShadow<u8> read8(X86::LogicalAddress);
  72. ValueWithShadow<u16> read16(X86::LogicalAddress);
  73. ValueWithShadow<u32> read32(X86::LogicalAddress);
  74. ValueWithShadow<u64> read64(X86::LogicalAddress);
  75. void write8(X86::LogicalAddress, ValueWithShadow<u8>);
  76. void write16(X86::LogicalAddress, ValueWithShadow<u16>);
  77. void write32(X86::LogicalAddress, ValueWithShadow<u32>);
  78. void write64(X86::LogicalAddress, ValueWithShadow<u64>);
  79. Region* find_region(X86::LogicalAddress);
  80. void add_region(NonnullOwnPtr<Region>);
  81. void remove_region(Region&);
  82. void set_tls_region(NonnullOwnPtr<Region>);
  83. void copy_to_vm(FlatPtr destination, const void* source, size_t);
  84. void copy_from_vm(void* destination, const FlatPtr source, size_t);
  85. ByteBuffer copy_buffer_from_vm(const FlatPtr source, size_t);
  86. SharedBufferRegion* shbuf_region(int shbuf_id);
  87. template<typename Callback>
  88. void for_each_region(Callback callback)
  89. {
  90. if (m_tls_region) {
  91. if (callback(*m_tls_region) == IterationDecision::Break)
  92. return;
  93. }
  94. for (auto& region : m_regions) {
  95. if (callback(region) == IterationDecision::Break)
  96. return;
  97. }
  98. }
  99. private:
  100. OwnPtr<Region> m_tls_region;
  101. NonnullOwnPtrVector<Region> m_regions;
  102. HashMap<int, Region*> m_shbuf_regions;
  103. };
  104. }