Skip to content

Commit

Permalink
Integrate MMIO into the memory manager
Browse files Browse the repository at this point in the history
  • Loading branch information
momo5502 committed Nov 18, 2024
1 parent 49a8435 commit 1a23e82
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
27 changes: 26 additions & 1 deletion src/emulator/memory_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,24 @@ bool memory_manager::protect_memory(const uint64_t address, const size_t size, c
return true;
}

bool memory_manager::allocate_mmio(uint64_t address, size_t size, mmio_read_callback read_cb,
mmio_write_callback write_cb)
{
if (this->overlaps_reserved_region(address, size))
{
return false;
}

this->map_mmio(address, size, std::move(read_cb), std::move(write_cb));

const auto entry = this->reserved_regions_.try_emplace(address, size).first;
entry->second.committed_regions[address] = committed_region{
.length = size, .pemissions = memory_permission::read_write, .is_mmio = true,
};

return true;
}

bool memory_manager::allocate_memory(const uint64_t address, const size_t size, const memory_permission permissions,
const bool reserve_only)
{
Expand All @@ -211,7 +229,9 @@ bool memory_manager::allocate_memory(const uint64_t address, const size_t size,
if (!reserve_only)
{
this->map_memory(address, size, permissions);
entry->second.committed_regions[address] = committed_region{size, permissions};
entry->second.committed_regions[address] = committed_region{
.length = size, .pemissions = memory_permission::read_write
};
}

return true;
Expand Down Expand Up @@ -303,6 +323,11 @@ bool memory_manager::decommit_memory(const uint64_t address, const size_t size)
break;
}

if (i->second.is_mmio)
{
throw std::runtime_error("Not allowed to decommit MMIO!");
}

const auto sub_region_end = i->first + i->second.length;
if (i->first >= address && sub_region_end <= end)
{
Expand Down
3 changes: 2 additions & 1 deletion src/emulator/memory_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class memory_manager
{
size_t length{};
memory_permission pemissions{};
bool is_mmio{false};
};

using committed_region_map = std::map<uint64_t, committed_region>;
Expand Down Expand Up @@ -82,6 +83,7 @@ class memory_manager
bool protect_memory(uint64_t address, size_t size, memory_permission permissions,
memory_permission* old_permissions = nullptr);

bool allocate_mmio(uint64_t address, size_t size, mmio_read_callback read_cb, mmio_write_callback write_cb);
bool allocate_memory(uint64_t address, size_t size, memory_permission permissions,
bool reserve_only = false);

Expand Down Expand Up @@ -118,7 +120,6 @@ class memory_manager

virtual void apply_memory_protection(uint64_t address, size_t size, memory_permission permissions) = 0;


protected:
void serialize_memory_state(utils::buffer_serializer& buffer, bool is_snapshot) const;
void deserialize_memory_state(utils::buffer_deserializer& buffer, bool is_snapshot);
Expand Down

0 comments on commit 1a23e82

Please sign in to comment.