diff --git a/Userland/DevTools/UserspaceEmulator/SoftCPU.h b/Userland/DevTools/UserspaceEmulator/SoftCPU.h index fed286f4604b2f..e139a3a8108068 100644 --- a/Userland/DevTools/UserspaceEmulator/SoftCPU.h +++ b/Userland/DevTools/UserspaceEmulator/SoftCPU.h @@ -6,6 +6,8 @@ #pragma once +#include "AK/Debug.h" +#include "Emulator.h" #include "Region.h" #include "SoftFPU.h" #include "ValueWithShadow.h" @@ -367,18 +369,12 @@ class SoftCPU final template ValueWithShadow read_memory(X86::LogicalAddress address) { - if constexpr (sizeof(T) == 1) - return read_memory8(address); - if constexpr (sizeof(T) == 2) - return read_memory16(address); - if constexpr (sizeof(T) == 4) - return read_memory32(address); - if constexpr (sizeof(T) == 8) - return read_memory64(address); - if constexpr (sizeof(T) == 16) - return read_memory128(address); - if constexpr (sizeof(T) == 32) - return read_memory256(address); + auto value = m_emulator.mmu().read(address); + if constexpr (AK::HasFormatter) + outln_if(MEMORY_DEBUG, "\033[36;1mread_memory: @{:#04x}:{:p} -> {:#064x} ({:hex-dump})\033[0m", address.selector(), address.offset(), value.value(), value.shadow().span()); + else + outln_if(MEMORY_DEBUG, "\033[36;1mread_memory: @{:#04x}:{:p} -> ??? ({:hex-dump})\033[0m", address.selector(), address.offset(), value.shadow().span()); + return value; } void write_memory8(X86::LogicalAddress, ValueWithShadow); diff --git a/Userland/DevTools/UserspaceEmulator/SoftMMU.cpp b/Userland/DevTools/UserspaceEmulator/SoftMMU.cpp index bf9e75d5537107..b11e968a15dea4 100644 --- a/Userland/DevTools/UserspaceEmulator/SoftMMU.cpp +++ b/Userland/DevTools/UserspaceEmulator/SoftMMU.cpp @@ -376,4 +376,9 @@ bool SoftMMU::fast_fill_memory32(X86::LogicalAddress address, size_t count, Valu return true; } +void SoftMMU::dump_backtrace() +{ + m_emulator.dump_backtrace(); +} + } diff --git a/Userland/DevTools/UserspaceEmulator/SoftMMU.h b/Userland/DevTools/UserspaceEmulator/SoftMMU.h index c46e1c8e8630bc..24f1a27b41ed41 100644 --- a/Userland/DevTools/UserspaceEmulator/SoftMMU.h +++ b/Userland/DevTools/UserspaceEmulator/SoftMMU.h @@ -7,6 +7,7 @@ #pragma once #include "Region.h" +#include "Report.h" #include "ValueWithShadow.h" #include #include @@ -29,6 +30,39 @@ class SoftMMU { ValueWithShadow read128(X86::LogicalAddress); ValueWithShadow read256(X86::LogicalAddress); + void dump_backtrace(); + + template + ValueWithShadow read(X86::LogicalAddress address) requires(IsTriviallyConstructible) + { + 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 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(&data[0]), + shadow, + }; + } + void write8(X86::LogicalAddress, ValueWithShadow); void write16(X86::LogicalAddress, ValueWithShadow); void write32(X86::LogicalAddress, ValueWithShadow);