/* * Copyright (c) 2020, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include "Region.h" #include "ValueWithShadow.h" #include #include #include #include #include namespace UserspaceEmulator { class Emulator; class SoftMMU { public: explicit SoftMMU(Emulator&); ValueWithShadow read8(X86::LogicalAddress); ValueWithShadow read16(X86::LogicalAddress); ValueWithShadow read32(X86::LogicalAddress); ValueWithShadow read64(X86::LogicalAddress); ValueWithShadow read128(X86::LogicalAddress); ValueWithShadow read256(X86::LogicalAddress); void write8(X86::LogicalAddress, ValueWithShadow); void write16(X86::LogicalAddress, ValueWithShadow); void write32(X86::LogicalAddress, ValueWithShadow); void write64(X86::LogicalAddress, ValueWithShadow); void write128(X86::LogicalAddress, ValueWithShadow); void write256(X86::LogicalAddress, ValueWithShadow); ALWAYS_INLINE Region* find_region(X86::LogicalAddress address) { if (address.selector() == 0x2b) return m_tls_region.ptr(); size_t page_index = address.offset() / PAGE_SIZE; return m_page_to_region_map[page_index]; } void add_region(NonnullOwnPtr); void remove_region(Region&); void ensure_split_at(X86::LogicalAddress); void set_tls_region(NonnullOwnPtr); bool fast_fill_memory8(X86::LogicalAddress, size_t size, ValueWithShadow); bool fast_fill_memory32(X86::LogicalAddress, size_t size, ValueWithShadow); void copy_to_vm(FlatPtr destination, const void* source, size_t); void copy_from_vm(void* destination, const FlatPtr source, size_t); ByteBuffer copy_buffer_from_vm(const FlatPtr source, size_t); template void for_each_region(Callback callback) { if (m_tls_region) { if (callback(*m_tls_region) == IterationDecision::Break) return; } for (auto& region : m_regions) { if (callback(region) == IterationDecision::Break) return; } } template void for_regions_in(X86::LogicalAddress address, size_t size, Callback callback) { VERIFY(size > 0); X86::LogicalAddress address_end = address; address_end.set_offset(address_end.offset() + size); ensure_split_at(address); ensure_split_at(address_end); size_t first_page = address.offset() / PAGE_SIZE; size_t last_page = (address_end.offset() - 1) / PAGE_SIZE; Region* last_reported = nullptr; for (size_t page = first_page; page <= last_page; ++page) { Region* current_region = m_page_to_region_map[page]; if (page != first_page && current_region == last_reported) continue; if (callback(current_region) == IterationDecision::Break) return; last_reported = current_region; } } private: Emulator& m_emulator; Region* m_page_to_region_map[786432] = { nullptr }; OwnPtr m_tls_region; NonnullOwnPtrVector m_regions; }; }