/* * Copyright (c) 2020-2021, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include "SoftMMU.h" #include namespace UserspaceEmulator { class MallocRegionMetadata; class MallocTracer; class MmapRegion final : public Region { public: static NonnullOwnPtr create_anonymous(u32 base, u32 size, u32 prot, String name); static NonnullOwnPtr create_file_backed(u32 base, u32 size, u32 prot, int flags, int fd, off_t offset, String name); virtual ~MmapRegion() override; virtual ValueWithShadow read8(u32 offset) override; virtual ValueWithShadow read16(u32 offset) override; virtual ValueWithShadow read32(u32 offset) override; virtual ValueWithShadow read64(u32 offset) override; virtual ValueWithShadow read128(u32 offset) override; virtual ValueWithShadow read256(u32 offset) override; virtual void write8(u32 offset, ValueWithShadow) override; virtual void write16(u32 offset, ValueWithShadow) override; virtual void write32(u32 offset, ValueWithShadow) override; virtual void write64(u32 offset, ValueWithShadow) override; virtual void write128(u32 offset, ValueWithShadow) override; virtual void write256(u32 offset, ValueWithShadow) override; virtual u8* data() override { return m_data; } virtual u8* shadow_data() override { return m_shadow_data; } bool is_malloc_block() const { return m_malloc; } void set_malloc(bool b) { m_malloc = b; } NonnullOwnPtr split_at(VirtualAddress); int prot() const { return (is_readable() ? PROT_READ : 0) | (is_writable() ? PROT_WRITE : 0) | (is_executable() ? PROT_EXEC : 0); } void set_prot(int prot); MallocRegionMetadata* malloc_metadata() { return m_malloc_metadata; } void set_malloc_metadata(Badge, NonnullOwnPtr metadata) { m_malloc_metadata = move(metadata); } const String& name() const { return m_name; } void set_name(String name) { m_name = move(name); } private: MmapRegion(u32 base, u32 size, int prot, u8* data, u8* shadow_data); u8* m_data { nullptr }; u8* m_shadow_data { nullptr }; bool m_file_backed { false }; bool m_malloc { false }; OwnPtr m_malloc_metadata; String m_name; }; template<> inline bool Region::fast_is() const { return m_mmap; } }