1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 04:07:45 +00:00

UserspaceEmulator: Add a SoftMMU::read<T> function

...and implement SoftCPU::read_memory<T> with it.
This allows the MMU to read a typed object (using 1-byte reads), which
is significantly nicer to use than reading the struct fields manually.
This commit is contained in:
Ali Mohammad Pur 2022-02-27 23:59:40 +03:30 committed by Andreas Kling
parent 70b53b44b2
commit baf7038919
3 changed files with 47 additions and 12 deletions

View file

@ -7,6 +7,7 @@
#pragma once
#include "Region.h"
#include "Report.h"
#include "ValueWithShadow.h"
#include <AK/HashMap.h>
#include <AK/NonnullOwnPtrVector.h>
@ -29,6 +30,39 @@ public:
ValueWithShadow<u128> read128(X86::LogicalAddress);
ValueWithShadow<u256> read256(X86::LogicalAddress);
void dump_backtrace();
template<typename T>
ValueWithShadow<T> read(X86::LogicalAddress address) requires(IsTriviallyConstructible<T>)
{
auto* region = find_region(address);
if (!region) {
reportln("SoftMMU::read256: No region for @ {:p}", address.offset());
dump_backtrace();
TODO();
}
if (!region->is_readable()) {
reportln("SoftMMU::read256: Non-readable region @ {:p}", address.offset());
dump_backtrace();
TODO();
}
alignas(alignof(T)) u8 data[sizeof(T)];
Array<u8, sizeof(T)> shadow;
for (auto i = 0u; i < sizeof(T); ++i) {
auto result = region->read8(address.offset() - region->base() + i);
data[i] = result.value();
shadow[i] = result.shadow()[0];
}
return {
*bit_cast<T*>(&data[0]),
shadow,
};
}
void write8(X86::LogicalAddress, ValueWithShadow<u8>);
void write16(X86::LogicalAddress, ValueWithShadow<u16>);
void write32(X86::LogicalAddress, ValueWithShadow<u32>);