SoftMMU.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include "Region.h"
  8. #include "ValueWithShadow.h"
  9. #include <AK/HashMap.h>
  10. #include <AK/NonnullOwnPtrVector.h>
  11. #include <AK/OwnPtr.h>
  12. #include <AK/Types.h>
  13. #include <LibX86/Instruction.h>
  14. namespace UserspaceEmulator {
  15. class Emulator;
  16. class SoftMMU {
  17. public:
  18. explicit SoftMMU(Emulator&);
  19. ValueWithShadow<u8> read8(X86::LogicalAddress);
  20. ValueWithShadow<u16> read16(X86::LogicalAddress);
  21. ValueWithShadow<u32> read32(X86::LogicalAddress);
  22. ValueWithShadow<u64> read64(X86::LogicalAddress);
  23. ValueWithShadow<u128> read128(X86::LogicalAddress);
  24. ValueWithShadow<u256> read256(X86::LogicalAddress);
  25. void write8(X86::LogicalAddress, ValueWithShadow<u8>);
  26. void write16(X86::LogicalAddress, ValueWithShadow<u16>);
  27. void write32(X86::LogicalAddress, ValueWithShadow<u32>);
  28. void write64(X86::LogicalAddress, ValueWithShadow<u64>);
  29. void write128(X86::LogicalAddress, ValueWithShadow<u128>);
  30. void write256(X86::LogicalAddress, ValueWithShadow<u256>);
  31. ALWAYS_INLINE Region* find_region(X86::LogicalAddress address)
  32. {
  33. if (address.selector() == 0x2b)
  34. return m_tls_region.ptr();
  35. size_t page_index = address.offset() / PAGE_SIZE;
  36. return m_page_to_region_map[page_index];
  37. }
  38. void add_region(NonnullOwnPtr<Region>);
  39. void remove_region(Region&);
  40. void ensure_split_at(X86::LogicalAddress);
  41. void set_tls_region(NonnullOwnPtr<Region>);
  42. bool fast_fill_memory8(X86::LogicalAddress, size_t size, ValueWithShadow<u8>);
  43. bool fast_fill_memory32(X86::LogicalAddress, size_t size, ValueWithShadow<u32>);
  44. void copy_to_vm(FlatPtr destination, const void* source, size_t);
  45. void copy_from_vm(void* destination, const FlatPtr source, size_t);
  46. ByteBuffer copy_buffer_from_vm(const FlatPtr source, size_t);
  47. template<typename Callback>
  48. void for_each_region(Callback callback)
  49. {
  50. if (m_tls_region) {
  51. if (callback(*m_tls_region) == IterationDecision::Break)
  52. return;
  53. }
  54. for (auto& region : m_regions) {
  55. if (callback(region) == IterationDecision::Break)
  56. return;
  57. }
  58. }
  59. template<typename Type, typename Callback>
  60. void for_each_region_of_type(Callback callback)
  61. {
  62. return for_each_region([callback](auto& region) {
  63. if (!is<Type>(region))
  64. return IterationDecision::Continue;
  65. return callback(static_cast<Type&>(region));
  66. });
  67. }
  68. template<typename Callback>
  69. void for_regions_in(X86::LogicalAddress address, size_t size, Callback callback)
  70. {
  71. VERIFY(size > 0);
  72. X86::LogicalAddress address_end = address;
  73. address_end.set_offset(address_end.offset() + size);
  74. ensure_split_at(address);
  75. ensure_split_at(address_end);
  76. size_t first_page = address.offset() / PAGE_SIZE;
  77. size_t last_page = (address_end.offset() - 1) / PAGE_SIZE;
  78. Region* last_reported = nullptr;
  79. for (size_t page = first_page; page <= last_page; ++page) {
  80. Region* current_region = m_page_to_region_map[page];
  81. if (page != first_page && current_region == last_reported)
  82. continue;
  83. if (callback(current_region) == IterationDecision::Break)
  84. return;
  85. last_reported = current_region;
  86. }
  87. }
  88. private:
  89. Emulator& m_emulator;
  90. Region* m_page_to_region_map[786432] = { nullptr };
  91. OwnPtr<Region> m_tls_region;
  92. NonnullOwnPtrVector<Region> m_regions;
  93. };
  94. }