mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-06 04:55:44 +00:00
Replay high-memory faults over the whole access and reconcile aio restores on unmap
This commit is contained in:
1 parent
dc3578b8f6
commit
3ac630f006
2 files changed
+386
-31
No files matched your search
@@ -13,15 +13,15 @@ writes to code in them invalidate translated blocks.
|
||||
Builds on StonkGuy's anti-cheat emulation from https://github.com/StonkGuy/FEX (eac-2611, MIT).
|
||||
---
|
||||
Source/Tools/LinuxEmulation/CMakeLists.txt | 1 +
|
||||
.../LinuxSyscalls/GuestHighMemory.cpp | 2085 +++++++++++++++++
|
||||
.../LinuxSyscalls/GuestHighMemory.h | 62 +
|
||||
.../LinuxSyscalls/GuestHighMemory.cpp | 2264 +++++++++++++++++
|
||||
.../LinuxSyscalls/GuestHighMemory.h | 61 +
|
||||
.../LinuxSyscalls/SignalDelegator.cpp | 5 +
|
||||
.../LinuxEmulation/LinuxSyscalls/Syscalls.cpp | 52 +-
|
||||
.../LinuxSyscalls/Syscalls/Memory.cpp | 30 +
|
||||
.../LinuxSyscalls/SyscallsSMCTracking.cpp | 61 +-
|
||||
.../LinuxSyscalls/ThreadManager.cpp | 2 +
|
||||
.../LinuxSyscalls/ThreadManager.h | 19 +
|
||||
9 files changed, 2287 insertions(+), 30 deletions(-)
|
||||
.../LinuxSyscalls/ThreadManager.h | 27 +
|
||||
9 files changed, 2473 insertions(+), 30 deletions(-)
|
||||
create mode 100644 Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.cpp
|
||||
create mode 100644 Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.h
|
||||
|
||||
@@ -39,10 +39,10 @@ index 3f02104..acf4c4c 100644
|
||||
LinuxSyscalls/SyscallsVMATracking.cpp
|
||||
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.cpp b/Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.cpp
|
||||
new file mode 100644
|
||||
index 0000000..15361da
|
||||
index 0000000..a6b9358
|
||||
--- /dev/null
|
||||
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.cpp
|
||||
@@ -0,0 +1,2083 @@
|
||||
@@ -0,0 +1,2264 @@
|
||||
+#include "LinuxSyscalls/GuestHighMemory.h"
|
||||
+#include "LinuxSyscalls/Syscalls.h"
|
||||
+#include "LinuxSyscalls/SignalDelegator.h"
|
||||
@@ -100,6 +100,16 @@ index 0000000..15361da
|
||||
+ uint64_t Size;
|
||||
+ };
|
||||
+
|
||||
+ constexpr uint32_t MaxAccessPieces = 2;
|
||||
+
|
||||
+ struct AccessPiece {
|
||||
+ uint64_t Guest;
|
||||
+ uint64_t Host;
|
||||
+ uint64_t Size;
|
||||
+ uint8_t Prot;
|
||||
+ bool Code;
|
||||
+ };
|
||||
+
|
||||
+ Region Regions[MaxRegions];
|
||||
+ std::atomic<uint32_t> Count {0};
|
||||
+ HostRange Retired[MaxRegions];
|
||||
@@ -155,10 +165,6 @@ index 0000000..15361da
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ bool Lookup(uint64_t Address, RegionView* Out) {
|
||||
+ return ReadConsistent([&] { return FindLocked(Address, Out); });
|
||||
+ }
|
||||
+
|
||||
+ bool SpanLocked(uint64_t Address, uint64_t Length, uint64_t* Host) {
|
||||
+ RegionView View;
|
||||
+ if (!FindLocked(Address, &View)) {
|
||||
@@ -182,6 +188,22 @@ index 0000000..15361da
|
||||
+ return true;
|
||||
+ }
|
||||
+
|
||||
+ uint32_t PlanAccessLocked(uint64_t Address, uint64_t Size, AccessPiece* Pieces) {
|
||||
+ uint32_t Parts = 0;
|
||||
+ uint64_t Done = 0;
|
||||
+ while (Done < Size) {
|
||||
+ RegionView View;
|
||||
+ if (Parts == MaxAccessPieces || !FindLocked(Address + Done, &View)) {
|
||||
+ return 0;
|
||||
+ }
|
||||
+ const auto Offset = Address + Done - View.Guest;
|
||||
+ const auto Length = std::min(Size - Done, View.Size - Offset);
|
||||
+ Pieces[Parts++] = {Address + Done, View.Host + Offset, Length, View.Prot, View.Code};
|
||||
+ Done += Length;
|
||||
+ }
|
||||
+ return Parts;
|
||||
+ }
|
||||
+
|
||||
+ template<typename Fn>
|
||||
+ uint64_t ApplyToRegionsLocked(uint64_t Address, uint64_t Length, uint64_t WrapError, Fn&& Apply) {
|
||||
+ if (Address & (FEXCore::Utils::FEX_PAGE_SIZE - 1)) {
|
||||
@@ -594,6 +616,129 @@ index 0000000..15361da
|
||||
+ return true;
|
||||
+ }
|
||||
+
|
||||
+ int64_t SignedField(uint32_t Insn, uint32_t Shift, uint32_t Bits) {
|
||||
+ return static_cast<int64_t>(static_cast<uint64_t>(Insn >> Shift) << (64 - Bits)) >> (64 - Bits);
|
||||
+ }
|
||||
+
|
||||
+ bool DecodeAccess(uint32_t Insn, void* UContext, uint64_t* Address, uint32_t* Size, bool* Plain) {
|
||||
+ const auto Base = ArchHelpers::Context::GetArmReg(UContext, RnField(Insn));
|
||||
+ const auto SizeBits = Insn >> 30;
|
||||
+ const bool Vector = (Insn >> 26) & 1;
|
||||
+ const auto Scale = Vector && ((Insn >> 23) & 1) && SizeBits == 0 ? 4U : SizeBits;
|
||||
+ *Address = Base;
|
||||
+ *Plain = false;
|
||||
+ if ((Insn & 0x3f000000) == 0x08000000) {
|
||||
+ const bool Pair = (Insn & (1U << 21)) && !(Insn & (1U << 23));
|
||||
+ *Size = Pair ? 8U << (SizeBits & 1) : 1U << SizeBits;
|
||||
+ return true;
|
||||
+ }
|
||||
+ if ((Insn & 0x3f200c00) == 0x19000000) {
|
||||
+ *Address = Base + SignedField(Insn, 12, 9);
|
||||
+ *Size = 1U << SizeBits;
|
||||
+ return true;
|
||||
+ }
|
||||
+ if ((Insn & 0x3b200c00) == 0x38200000) {
|
||||
+ *Size = 1U << SizeBits;
|
||||
+ return !Vector;
|
||||
+ }
|
||||
+ *Plain = true;
|
||||
+ if ((Insn & 0x3b000000) == 0x39000000) {
|
||||
+ *Address = Base + (static_cast<uint64_t>((Insn >> 10) & 0xfff) << Scale);
|
||||
+ *Size = 1U << Scale;
|
||||
+ return true;
|
||||
+ }
|
||||
+ if ((Insn & 0x3b200000) == 0x38000000) {
|
||||
+ *Address = ((Insn >> 10) & 3) == 1 ? Base : Base + SignedField(Insn, 12, 9);
|
||||
+ *Size = 1U << Scale;
|
||||
+ return true;
|
||||
+ }
|
||||
+ if ((Insn & 0x3b200c00) == 0x38200800) {
|
||||
+ const auto Option = (Insn >> 13) & 7;
|
||||
+ if ((Option & 2) == 0) {
|
||||
+ return false;
|
||||
+ }
|
||||
+ auto Offset = ReadReg(UContext, RsField(Insn));
|
||||
+ if ((Option & 1) == 0) {
|
||||
+ Offset = (Option & 4) ? static_cast<uint64_t>(static_cast<int64_t>(static_cast<int32_t>(Offset))) : static_cast<uint32_t>(Offset);
|
||||
+ }
|
||||
+ *Address = Base + (Offset << (((Insn >> 12) & 1) ? Scale : 0));
|
||||
+ *Size = 1U << Scale;
|
||||
+ return true;
|
||||
+ }
|
||||
+ if ((Insn & 0x3a000000) == 0x28000000) {
|
||||
+ if (SizeBits == 3) {
|
||||
+ return false;
|
||||
+ }
|
||||
+ const auto Element = Vector ? 4U << SizeBits : (SizeBits == 2 ? 8U : 4U);
|
||||
+ *Address = ((Insn >> 23) & 3) == 1 ? Base : Base + SignedField(Insn, 15, 7) * static_cast<int64_t>(Element);
|
||||
+ *Size = 2 * Element;
|
||||
+ return true;
|
||||
+ }
|
||||
+ if ((Insn & 0xbfbf0000) == 0x0c000000 || (Insn & 0xbfa00000) == 0x0c800000) {
|
||||
+ uint32_t Registers = 0;
|
||||
+ switch ((Insn >> 12) & 0xf) {
|
||||
+ case 0x0:
|
||||
+ case 0x2: Registers = 4; break;
|
||||
+ case 0x4:
|
||||
+ case 0x6: Registers = 3; break;
|
||||
+ case 0x7: Registers = 1; break;
|
||||
+ case 0x8:
|
||||
+ case 0xa: Registers = 2; break;
|
||||
+ default: return false;
|
||||
+ }
|
||||
+ *Size = Registers * ((Insn & (1U << 30)) ? 16U : 8U);
|
||||
+ return true;
|
||||
+ }
|
||||
+ if ((Insn & 0xbf9f0000) == 0x0d000000 || (Insn & 0xbf800000) == 0x0d800000) {
|
||||
+ const auto Opcode = (Insn >> 13) & 7;
|
||||
+ const auto Elements = (((Opcode & 1) << 1) | ((Insn >> 21) & 1)) + 1;
|
||||
+ const auto Bits = (Insn >> 10) & 3;
|
||||
+ uint32_t Element = 0;
|
||||
+ switch (Opcode >> 1) {
|
||||
+ case 0: Element = 1; break;
|
||||
+ case 1: Element = (Bits & 1) ? 0 : 2; break;
|
||||
+ case 2: Element = Bits == 0 ? 4 : (Bits == 1 && !((Insn >> 12) & 1) ? 8 : 0); break;
|
||||
+ default: Element = 1U << Bits; break;
|
||||
+ }
|
||||
+ *Size = Elements * Element;
|
||||
+ return Element != 0;
|
||||
+ }
|
||||
+ return false;
|
||||
+ }
|
||||
+
|
||||
+ bool HostContiguous(const AccessPiece* Pieces, uint32_t Parts) {
|
||||
+ for (uint32_t i = 1; i < Parts; ++i) {
|
||||
+ if (Pieces[i - 1].Host + Pieces[i - 1].Size != Pieces[i].Host) {
|
||||
+ return false;
|
||||
+ }
|
||||
+ }
|
||||
+ return true;
|
||||
+ }
|
||||
+
|
||||
+ bool PiecesReachable(const AccessPiece* Pieces, uint32_t Parts, bool Write) {
|
||||
+ const uint8_t Need = Write ? PROT_WRITE : PROT_READ | PROT_WRITE;
|
||||
+ for (uint32_t i = 0; i < Parts; ++i) {
|
||||
+ if ((Pieces[i].Prot & Need) == 0) {
|
||||
+ return false;
|
||||
+ }
|
||||
+ }
|
||||
+ return true;
|
||||
+ }
|
||||
+
|
||||
+ template<typename PieceType>
|
||||
+ void CopyPieces(uint8_t* Bounce, const PieceType* Pieces, uint32_t Parts, bool Gather) {
|
||||
+ uint64_t Offset = 0;
|
||||
+ for (uint32_t i = 0; i < Parts; ++i) {
|
||||
+ auto* Host = reinterpret_cast<uint8_t*>(Pieces[i].Host);
|
||||
+ if (Gather) {
|
||||
+ memcpy(Bounce + Offset, Host, Pieces[i].Size);
|
||||
+ } else {
|
||||
+ memcpy(Host, Bounce + Offset, Pieces[i].Size);
|
||||
+ }
|
||||
+ Offset += Pieces[i].Size;
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ int PickScratch(uint32_t Insn) {
|
||||
+ constexpr int Candidates[] = {16, 17, 9, 10, 11, 12, 13, 14, 15, 8, 7, 6, 5, 4, 3, 2};
|
||||
+ for (auto Reg : Candidates) {
|
||||
@@ -1804,6 +1949,7 @@ index 0000000..15361da
|
||||
+ BeginWrite();
|
||||
+ const int Result = UnmapLocked(Address, Size);
|
||||
+ EndWrite();
|
||||
+ ReconcilePending();
|
||||
+ return Result;
|
||||
+}
|
||||
+
|
||||
@@ -2009,6 +2155,7 @@ index 0000000..15361da
|
||||
+ ArchHelpers::Context::SetPc(UContext, Trap.ResumePC - 4);
|
||||
+ memcpy(&ArchHelpers::Context::GetUContext(UContext)->uc_sigmask, &Trap.SavedMask, sizeof(Trap.SavedMask));
|
||||
+ SigInfo->si_addr = reinterpret_cast<void*>(FaultAddress - Trap.Delta);
|
||||
+ Trap.Parts = 0;
|
||||
+ Trap.Active = false;
|
||||
+ return false;
|
||||
+ }
|
||||
@@ -2017,26 +2164,41 @@ index 0000000..15361da
|
||||
+ return false;
|
||||
+ }
|
||||
+
|
||||
+ RegionView View;
|
||||
+ if (!Lookup(FaultAddress, &View)) {
|
||||
+ return false;
|
||||
+ }
|
||||
+
|
||||
+ const auto Insn = *reinterpret_cast<const uint32_t*>(PC);
|
||||
+ if (!IsLoadStore(Insn) || IsLiteralLoad(Insn) || RnField(Insn) == 31) {
|
||||
+ return false;
|
||||
+ }
|
||||
+
|
||||
+ if (View.Code && IsWrite(Insn)) {
|
||||
+ const auto FaultBase = FEXCore::AlignDown(FaultAddress, FEXCore::Utils::FEX_PAGE_SIZE);
|
||||
+ uint64_t Address {};
|
||||
+ uint32_t Size {};
|
||||
+ bool Plain {};
|
||||
+ if (!DecodeAccess(Insn, UContext, &Address, &Size, &Plain)) {
|
||||
+ return false;
|
||||
+ }
|
||||
+ AccessPiece Pieces[MaxAccessPieces] {};
|
||||
+ const auto Parts = ReadConsistent([&] { return PlanAccessLocked(Address, Size, Pieces); });
|
||||
+ if (Parts == 0) {
|
||||
+ return false;
|
||||
+ }
|
||||
+
|
||||
+ if (IsWrite(Insn) && std::any_of(Pieces, Pieces + Parts, [](const AccessPiece& Piece) { return Piece.Code; })) {
|
||||
+ {
|
||||
+ auto lk = FEXCore::MaskSignalsAndLockMutex<std::shared_lock>(FEX::HLE::_SyscallHandler->VMATracking.Mutex);
|
||||
+ auto& VMATracking = FEX::HLE::_SyscallHandler->VMATracking;
|
||||
+ const auto Entry = VMATracking.FindVMAEntry(FaultAddress);
|
||||
+ if (Entry != VMATracking.VMAs.end()) {
|
||||
+ FEX::HLE::SyscallHandler::InvalidateWrittenCodePage(Thread, FaultBase, Entry->first, Entry->second);
|
||||
+ } else {
|
||||
+ FEX::HLE::_SyscallHandler->TM.InvalidateGuestCodeRange(Thread, FaultBase, FEXCore::Utils::FEX_PAGE_SIZE);
|
||||
+ for (uint32_t i = 0; i < Parts; ++i) {
|
||||
+ if (!Pieces[i].Code) {
|
||||
+ continue;
|
||||
+ }
|
||||
+ const auto Last = Pieces[i].Guest + Pieces[i].Size - 1;
|
||||
+ for (auto FaultBase = FEXCore::AlignDown(Pieces[i].Guest, FEXCore::Utils::FEX_PAGE_SIZE); FaultBase <= Last;
|
||||
+ FaultBase += FEXCore::Utils::FEX_PAGE_SIZE) {
|
||||
+ const auto Entry = VMATracking.FindVMAEntry(FaultBase);
|
||||
+ if (Entry != VMATracking.VMAs.end()) {
|
||||
+ FEX::HLE::SyscallHandler::InvalidateWrittenCodePage(Thread, FaultBase, Entry->first, Entry->second);
|
||||
+ } else {
|
||||
+ FEX::HLE::_SyscallHandler->TM.InvalidateGuestCodeRange(Thread, FaultBase, FEXCore::Utils::FEX_PAGE_SIZE);
|
||||
+ }
|
||||
+ }
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
@@ -2054,16 +2216,19 @@ index 0000000..15361da
|
||||
+ }
|
||||
+
|
||||
+ if (IsExclusive(Insn)) {
|
||||
+ const auto Address = ArchHelpers::Context::GetArmReg(UContext, RnField(Insn));
|
||||
+ const auto Size = IsExclusivePair(Insn) ? 2 * ExclusiveElementSize(Insn) : ExclusiveElementSize(Insn);
|
||||
+ if (Address < View.Guest || Address - View.Guest > View.Size - Size || !ExclusiveReachable(Insn, View.Prot) ||
|
||||
+ !EmulateExclusive(Trap.Monitor, Insn, View.Host + (Address - View.Guest), UContext)) {
|
||||
+ if (Parts != 1 || !ExclusiveReachable(Insn, Pieces[0].Prot) ||
|
||||
+ !EmulateExclusive(Trap.Monitor, Insn, Pieces[0].Host, UContext)) {
|
||||
+ return false;
|
||||
+ }
|
||||
+ ArchHelpers::Context::SetPc(UContext, PC + 4);
|
||||
+ return true;
|
||||
+ }
|
||||
+
|
||||
+ const bool Bounced = !HostContiguous(Pieces, Parts);
|
||||
+ if (Bounced && (!Plain || !PiecesReachable(Pieces, Parts, IsWrite(Insn)))) {
|
||||
+ return false;
|
||||
+ }
|
||||
+
|
||||
+ const int Scratch = PickScratch(Insn);
|
||||
+ if (Scratch < 0) {
|
||||
+ return false;
|
||||
@@ -2087,14 +2252,26 @@ index 0000000..15361da
|
||||
+ Trap.Base = Base;
|
||||
+ Trap.SavedScratch = ArchHelpers::Context::GetArmReg(UContext, Scratch);
|
||||
+ Trap.BaseBefore = ArchHelpers::Context::GetArmReg(UContext, Base);
|
||||
+ Trap.Delta = View.Host - View.Guest;
|
||||
+ Trap.Delta = Pieces[0].Host - Address;
|
||||
+ Trap.Parts = 0;
|
||||
+ if (Bounced) {
|
||||
+ Trap.BounceOffset = Address & 15;
|
||||
+ Trap.Store = IsWrite(Insn);
|
||||
+ Trap.Parts = Parts;
|
||||
+ for (uint32_t i = 0; i < Parts; ++i) {
|
||||
+ Trap.Pieces[i].Host = Pieces[i].Host;
|
||||
+ Trap.Pieces[i].Size = Pieces[i].Size;
|
||||
+ }
|
||||
+ auto* Window = Trap.Bounce + Trap.BounceOffset;
|
||||
+ CopyPieces(Window, Trap.Pieces, Parts, true);
|
||||
+ Trap.Delta = reinterpret_cast<uint64_t>(Window) - Address;
|
||||
+ }
|
||||
+ Trap.ResumePC = PC + 4;
|
||||
+
|
||||
+ auto* Mask = &ArchHelpers::Context::GetUContext(UContext)->uc_sigmask;
|
||||
+ memcpy(&Trap.SavedMask, Mask, sizeof(Trap.SavedMask));
|
||||
+ const uint64_t Blocked = ~SynchronousSignalMask();
|
||||
+ memcpy(Mask, &Blocked, sizeof(Blocked));
|
||||
+
|
||||
+ ArchHelpers::Context::SetArmReg(UContext, Scratch, Trap.BaseBefore + Trap.Delta);
|
||||
+ ArchHelpers::Context::SetPc(UContext, reinterpret_cast<uint64_t>(Code));
|
||||
+ Trap.Active = true;
|
||||
@@ -2112,6 +2289,10 @@ index 0000000..15361da
|
||||
+ if (ScratchAfter != Trap.BaseBefore + Trap.Delta) {
|
||||
+ ArchHelpers::Context::SetArmReg(UContext, Trap.Base, ScratchAfter - Trap.Delta);
|
||||
+ }
|
||||
+ if (Trap.Parts != 0 && Trap.Store) {
|
||||
+ CopyPieces(Trap.Bounce + Trap.BounceOffset, Trap.Pieces, Trap.Parts, false);
|
||||
+ }
|
||||
+ Trap.Parts = 0;
|
||||
+ ArchHelpers::Context::SetArmReg(UContext, Trap.Scratch, Trap.SavedScratch);
|
||||
+ memcpy(&ArchHelpers::Context::GetUContext(UContext)->uc_sigmask, &Trap.SavedMask, sizeof(Trap.SavedMask));
|
||||
+ ArchHelpers::Context::SetPc(UContext, Trap.ResumePC);
|
||||
@@ -2527,7 +2708,7 @@ diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/ThreadManager.h b/Source/
|
||||
index 657de6c..26040c6 100644
|
||||
--- a/Source/Tools/LinuxEmulation/LinuxSyscalls/ThreadManager.h
|
||||
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/ThreadManager.h
|
||||
@@ -103,6 +103,25 @@ struct ThreadStateObject : public FEXCore::Allocator::FEXAllocOperators {
|
||||
@@ -103,6 +103,33 @@ struct ThreadStateObject : public FEXCore::Allocator::FEXAllocOperators {
|
||||
bool Enabled;
|
||||
} SyscallUserDispatch {};
|
||||
|
||||
@@ -2541,6 +2722,14 @@ index 657de6c..26040c6 100644
|
||||
+ uint32_t Scratch;
|
||||
+ uint32_t Base;
|
||||
+ bool Active;
|
||||
+ bool Store;
|
||||
+ uint32_t Parts;
|
||||
+ uint32_t BounceOffset;
|
||||
+ struct {
|
||||
+ uint64_t Host;
|
||||
+ uint64_t Size;
|
||||
+ } Pieces[2];
|
||||
+ alignas(16) uint8_t Bounce[96];
|
||||
+ struct {
|
||||
+ uint64_t Host;
|
||||
+ uint64_t Low;
|
||||
|
||||
@@ -1168,6 +1168,7 @@ int main() {
|
||||
Strand(&Vanishing);
|
||||
Show("io_submit_vanished_pending", PendingRestores.size() - PendingBefore);
|
||||
Unmap(Sealing, Page);
|
||||
Show("io_submit_vanished_unmapped", PendingRestores.size() - PendingBefore);
|
||||
Reconcile();
|
||||
Show("io_submit_vanished_freed", PendingRestores.size() - PendingBefore);
|
||||
Map(Sealing, Page, PROT_READ | PROT_WRITE, Anonymous, -1, 0);
|
||||
@@ -1522,6 +1523,99 @@ int main() {
|
||||
FinishResult(1, &ProcfsSubmit);
|
||||
Show("io_submit_procfs_forced", ForcedWrites);
|
||||
Show("io_submit_procfs_restored", SealingBlock->Buffer == reinterpret_cast<uint64_t>(Scatter));
|
||||
|
||||
uint64_t AccessRegs[32] {};
|
||||
AccessRegs[0] = 0x10000;
|
||||
AccessRegs[2] = 0xfffffffeULL;
|
||||
auto Decode = [&](const char* Name, uint32_t Insn) {
|
||||
uint64_t Address {};
|
||||
uint32_t Size {};
|
||||
bool Plain {};
|
||||
const bool Known = DecodeAccess(Insn, AccessRegs, &Address, &Size, &Plain);
|
||||
std::printf("decode_%s_known %x\n", Name, Known);
|
||||
std::printf("decode_%s_offset %llx\n", Name, static_cast<unsigned long long>(Address - AccessRegs[0]));
|
||||
std::printf("decode_%s_size %x\n", Name, Size);
|
||||
std::printf("decode_%s_plain %x\n", Name, Plain);
|
||||
};
|
||||
Decode("str_x_imm", 0xf9000401);
|
||||
Decode("ldr_x_pre", 0xf85fcc01);
|
||||
Decode("ldr_x_post", 0xf8408401);
|
||||
Decode("stur_x", 0xf81fc001);
|
||||
Decode("ldr_w_lsl", 0xb8627801);
|
||||
Decode("ldr_w_sxtw", 0xb862c801);
|
||||
Decode("ldr_w_uxtw", 0xb8624801);
|
||||
Decode("ldr_w_reserved", 0xb8620801);
|
||||
Decode("stp_x", 0xa9010c01);
|
||||
Decode("ldp_w_negative", 0x297f0c01);
|
||||
Decode("ldp_q_post", 0xacc10c01);
|
||||
Decode("str_q", 0x3d800401);
|
||||
Decode("ld1_four", 0x4c402001);
|
||||
Decode("st1_post", 0x4c9f7c01);
|
||||
Decode("ld1_lane", 0x0d409001);
|
||||
Decode("ld4r", 0x4d60e801);
|
||||
Decode("ldadd", 0xf8210002);
|
||||
Decode("ldar", 0xc8dffc01);
|
||||
Decode("casp", 0x48227c04);
|
||||
Decode("ldxp", 0xc87f0c01);
|
||||
Decode("ldapur", 0x995fc001);
|
||||
Decode("ldraa", 0xf8200400);
|
||||
|
||||
auto* Backings = static_cast<uint8_t*>(mmap(nullptr, 6 * Page, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0));
|
||||
const uint64_t Backed = reinterpret_cast<uint64_t>(Backings);
|
||||
const uint64_t SeparateGuest = 0x6f0000000000ULL;
|
||||
const uint64_t Adjacent = 0x6f0000100000ULL;
|
||||
AddEntry(SeparateGuest, Page, Backed, false, PROT_READ | PROT_WRITE);
|
||||
AddEntry(SeparateGuest + Page, Page, Backed + 2 * Page, false, PROT_READ | PROT_WRITE);
|
||||
AddEntry(Adjacent, Page, Backed + 4 * Page, false, PROT_READ | PROT_WRITE);
|
||||
AddEntry(Adjacent + Page, Page, Backed + 5 * Page, false, PROT_READ | PROT_WRITE);
|
||||
std::memset(Backings + Page, 0xee, Page);
|
||||
uint64_t StoreRegs[32] {};
|
||||
StoreRegs[0] = SeparateGuest + Page - 4;
|
||||
StoreRegs[1] = 0x1122334455667788ULL;
|
||||
uint64_t SplitAddress {};
|
||||
uint32_t SplitSize {};
|
||||
bool SplitPlain {};
|
||||
DecodeAccess(0xf9000001, StoreRegs, &SplitAddress, &SplitSize, &SplitPlain);
|
||||
AccessPiece SplitPieces[MaxAccessPieces] {};
|
||||
const auto SplitParts = PlanAccessLocked(SplitAddress, SplitSize, SplitPieces);
|
||||
Show("split_access_parts", SplitParts);
|
||||
Show("split_access_first", SplitPieces[0].Host - Backed);
|
||||
Show("split_access_second", SplitPieces[1].Host - Backed);
|
||||
Show("split_access_sizes", SplitPieces[0].Size * 0x10 + SplitPieces[1].Size);
|
||||
Show("split_access_guests", (SplitPieces[0].Guest == SeparateGuest + Page - 4) * 0x10 + (SplitPieces[1].Guest == SeparateGuest + Page));
|
||||
Show("split_access_contiguous", HostContiguous(SplitPieces, SplitParts));
|
||||
Show("split_access_store_reachable", PiecesReachable(SplitPieces, SplitParts, true));
|
||||
alignas(16) uint8_t Window[96] {};
|
||||
const auto WindowOffset = SplitAddress & 15;
|
||||
CopyPieces(Window + WindowOffset, SplitPieces, SplitParts, true);
|
||||
std::memcpy(Window + WindowOffset, &StoreRegs[1], sizeof(StoreRegs[1]));
|
||||
CopyPieces(Window + WindowOffset, SplitPieces, SplitParts, false);
|
||||
uint32_t SplitWords[3] {};
|
||||
std::memcpy(&SplitWords[0], Backings + Page - 4, 4);
|
||||
std::memcpy(&SplitWords[1], Backings + 2 * Page, 4);
|
||||
std::memcpy(&SplitWords[2], Backings + Page, 4);
|
||||
Show("split_store_low", SplitWords[0]);
|
||||
Show("split_store_high", SplitWords[1]);
|
||||
Show("split_store_neighbour", SplitWords[2]);
|
||||
std::memset(Window, 0, sizeof(Window));
|
||||
CopyPieces(Window + WindowOffset, SplitPieces, SplitParts, true);
|
||||
uint64_t SplitLoaded {};
|
||||
std::memcpy(&SplitLoaded, Window + WindowOffset, sizeof(SplitLoaded));
|
||||
Show("split_load_value", SplitLoaded);
|
||||
SplitPieces[1].Prot = PROT_READ;
|
||||
Show("split_readonly_store", PiecesReachable(SplitPieces, SplitParts, true));
|
||||
Show("split_readonly_load", PiecesReachable(SplitPieces, SplitParts, false));
|
||||
SplitPieces[1].Prot = PROT_NONE;
|
||||
Show("split_none_load", PiecesReachable(SplitPieces, SplitParts, false));
|
||||
AccessPiece EdgePieces[MaxAccessPieces] {};
|
||||
Show("split_unmapped_parts", PlanAccessLocked(SeparateGuest + 2 * Page - 4, 8, EdgePieces));
|
||||
Show("split_inside_parts", PlanAccessLocked(SeparateGuest + 16, 64, EdgePieces));
|
||||
Show("split_inside_host", EdgePieces[0].Host - Backed);
|
||||
AccessPiece JoinedPieces[MaxAccessPieces] {};
|
||||
const auto JoinedParts = PlanAccessLocked(Adjacent + Page - 4, 8, JoinedPieces);
|
||||
Show("joined_access_parts", JoinedParts);
|
||||
Show("joined_access_contiguous", HostContiguous(JoinedPieces, JoinedParts));
|
||||
Show("joined_access_delta", JoinedPieces[0].Host - (Adjacent + Page - 4) == Backed + 4 * Page - Adjacent);
|
||||
return 0;
|
||||
}
|
||||
"""
|
||||
@@ -1869,6 +1963,12 @@ class GuestHighMemoryTest(unittest.TestCase):
|
||||
self.assertEqual(self.value("io_submit_full_kept"), 1)
|
||||
self.assertEqual(self.value("io_submit_full_drained"), 0)
|
||||
|
||||
def test_unmapping_reconciles_pending_aio_restores(self):
|
||||
self.assertEqual(self.value("io_submit_vanished_pending"), 1)
|
||||
self.assertEqual(self.value("io_submit_vanished_unmapped"), 0)
|
||||
lines = added_lines(PATCH.read_text(), SOURCE)
|
||||
self.assertIn("ReconcilePending();", block(lines, "uint64_t Unmap("))
|
||||
|
||||
def test_aio_completions_report_guest_control_blocks(self):
|
||||
self.assertEqual(self.value("io_getevents_result"), 0)
|
||||
self.assertEqual(self.value("io_getevents_object"), 1536)
|
||||
@@ -2136,11 +2236,77 @@ class GuestHighMemoryTest(unittest.TestCase):
|
||||
self.assertEqual(self.value("exclusive_refused"), 11)
|
||||
self.assertEqual(self.value("others_allowed"), 11)
|
||||
text = PATCH.read_text()
|
||||
self.assertRegex(text, r"\n\+ if \(IsExclusive\(Insn\)\) \{\n(?:\+.*\n){3}\+ !EmulateExclusive\(")
|
||||
self.assertRegex(text, r"\n\+ if \(IsExclusive\(Insn\)\) \{\n\+ if \(Parts != 1 \|\| !ExclusiveReachable\(Insn, Pieces\[0\]\.Prot\) \|\|\n\+ !EmulateExclusive\(")
|
||||
|
||||
def test_high_code_writes_invalidate_every_alias(self):
|
||||
text = PATCH.read_text()
|
||||
self.assertRegex(text, r"\n\+ FEX::HLE::SyscallHandler::InvalidateWrittenCodePage\(Thread, FaultBase, Entry->first, Entry->second\);")
|
||||
self.assertRegex(text, r"\n\+ FEX::HLE::SyscallHandler::InvalidateWrittenCodePage\(Thread, FaultBase, Entry->first, Entry->second\);")
|
||||
fault = block(added_lines(text, SOURCE), "bool HandleFault(")
|
||||
self.assertIn("for (auto FaultBase = FEXCore::AlignDown(Pieces[i].Guest, FEXCore::Utils::FEX_PAGE_SIZE); FaultBase <= Last;", fault)
|
||||
|
||||
def access(self, name):
|
||||
if not self.value(f"decode_{name}_known"):
|
||||
return None
|
||||
offset = self.value(f"decode_{name}_offset")
|
||||
if offset >= 1 << 63:
|
||||
offset -= 1 << 64
|
||||
return offset, self.value(f"decode_{name}_size"), self.value(f"decode_{name}_plain")
|
||||
|
||||
def test_fault_accesses_decode_their_whole_range(self):
|
||||
self.assertEqual(self.access("str_x_imm"), (8, 8, 1))
|
||||
self.assertEqual(self.access("ldr_x_pre"), (-4, 8, 1))
|
||||
self.assertEqual(self.access("ldr_x_post"), (0, 8, 1))
|
||||
self.assertEqual(self.access("stur_x"), (-4, 8, 1))
|
||||
self.assertEqual(self.access("ldr_w_lsl"), (0xfffffffe << 2, 4, 1))
|
||||
self.assertEqual(self.access("ldr_w_sxtw"), (-2, 4, 1))
|
||||
self.assertEqual(self.access("ldr_w_uxtw"), (0xfffffffe, 4, 1))
|
||||
self.assertIsNone(self.access("ldr_w_reserved"))
|
||||
self.assertEqual(self.access("stp_x"), (16, 16, 1))
|
||||
self.assertEqual(self.access("ldp_w_negative"), (-8, 8, 1))
|
||||
self.assertEqual(self.access("ldp_q_post"), (0, 32, 1))
|
||||
self.assertEqual(self.access("str_q"), (16, 16, 1))
|
||||
self.assertEqual(self.access("ld1_four"), (0, 64, 1))
|
||||
self.assertEqual(self.access("st1_post"), (0, 16, 1))
|
||||
self.assertEqual(self.access("ld1_lane"), (0, 4, 1))
|
||||
self.assertEqual(self.access("ld4r"), (0, 16, 1))
|
||||
self.assertEqual(self.access("ldadd"), (0, 8, 0))
|
||||
self.assertEqual(self.access("ldar"), (0, 8, 0))
|
||||
self.assertEqual(self.access("casp"), (0, 16, 0))
|
||||
self.assertEqual(self.access("ldxp"), (0, 16, 0))
|
||||
self.assertEqual(self.access("ldapur"), (-4, 4, 0))
|
||||
self.assertIsNone(self.access("ldraa"))
|
||||
|
||||
def test_accesses_across_separate_backings_reach_each_backing(self):
|
||||
self.assertEqual(self.value("split_access_parts"), 2)
|
||||
self.assertEqual(self.value("split_access_first"), 4096 - 4)
|
||||
self.assertEqual(self.value("split_access_second"), 2 * 4096)
|
||||
self.assertEqual(self.value("split_access_sizes"), 0x44)
|
||||
self.assertEqual(self.value("split_access_guests"), 0x11)
|
||||
self.assertEqual(self.value("split_access_contiguous"), 0)
|
||||
self.assertEqual(self.value("split_access_store_reachable"), 1)
|
||||
self.assertEqual(self.value("split_store_low"), 0x55667788)
|
||||
self.assertEqual(self.value("split_store_high"), 0x11223344)
|
||||
self.assertEqual(self.value("split_store_neighbour"), 0xeeeeeeee)
|
||||
self.assertEqual(self.value("split_load_value"), 0x1122334455667788)
|
||||
self.assertEqual(self.value("split_readonly_store"), 0)
|
||||
self.assertEqual(self.value("split_readonly_load"), 1)
|
||||
self.assertEqual(self.value("split_none_load"), 0)
|
||||
self.assertEqual(self.value("split_unmapped_parts"), 0)
|
||||
self.assertEqual(self.value("split_inside_parts"), 1)
|
||||
self.assertEqual(self.value("split_inside_host"), 16)
|
||||
self.assertEqual(self.value("joined_access_parts"), 2)
|
||||
self.assertEqual(self.value("joined_access_contiguous"), 1)
|
||||
self.assertEqual(self.value("joined_access_delta"), 1)
|
||||
|
||||
def test_fault_replay_translates_the_whole_access(self):
|
||||
lines = added_lines(PATCH.read_text(), SOURCE)
|
||||
fault = block(lines, "bool HandleFault(")
|
||||
self.assertNotIn("View.Host - View.Guest", fault)
|
||||
self.assertIn("const auto Parts = ReadConsistent([&] { return PlanAccessLocked(Address, Size, Pieces); });", fault)
|
||||
self.assertIn("if (Bounced && (!Plain || !PiecesReachable(Pieces, Parts, IsWrite(Insn)))) {", fault)
|
||||
self.assertIn("CopyPieces(Window, Trap.Pieces, Parts, true);", fault)
|
||||
self.assertIn("Trap.Delta = reinterpret_cast<uint64_t>(Window) - Address;", fault)
|
||||
self.assertIn("CopyPieces(Trap.Bounce + Trap.BounceOffset, Trap.Pieces, Trap.Parts, false);", block(lines, "bool HandleTrap("))
|
||||
|
||||
def test_madvise_applies_to_each_overlapping_region(self):
|
||||
self.assertEqual(self.value("split_apply_result"), 0)
|
||||
|
||||
Reference in new issue
Block a user