2020-12-16 20:07:42 +00:00
|
|
|
using ARMeilleure.Translation.Cache;
|
2020-06-16 18:28:02 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
namespace ARMeilleure.Translation.PTC
|
|
|
|
{
|
|
|
|
[Serializable]
|
|
|
|
class PtcJumpTable
|
|
|
|
{
|
2020-12-16 20:07:42 +00:00
|
|
|
[Serializable]
|
|
|
|
private struct TableEntry<TAddress>
|
|
|
|
{
|
|
|
|
public int EntryIndex;
|
|
|
|
public long GuestAddress;
|
|
|
|
public TAddress HostAddress;
|
2020-06-16 18:28:02 +00:00
|
|
|
|
2020-12-16 20:07:42 +00:00
|
|
|
public TableEntry(int entryIndex, long guestAddress, TAddress hostAddress)
|
|
|
|
{
|
|
|
|
EntryIndex = entryIndex;
|
|
|
|
GuestAddress = guestAddress;
|
|
|
|
HostAddress = hostAddress;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private enum DirectHostAddress
|
|
|
|
{
|
|
|
|
CallStub,
|
|
|
|
TailCallStub,
|
|
|
|
Host
|
|
|
|
}
|
|
|
|
|
|
|
|
private enum IndirectHostAddress
|
|
|
|
{
|
|
|
|
CallStub,
|
|
|
|
TailCallStub
|
|
|
|
}
|
|
|
|
|
|
|
|
private readonly List<TableEntry<DirectHostAddress>> _jumpTable;
|
|
|
|
private readonly List<TableEntry<IndirectHostAddress>> _dynamicTable;
|
2020-06-16 18:28:02 +00:00
|
|
|
|
2020-12-16 20:07:42 +00:00
|
|
|
private readonly List<ulong> _targets;
|
|
|
|
private readonly Dictionary<ulong, List<int>> _dependants;
|
|
|
|
private readonly Dictionary<ulong, List<int>> _owners;
|
2020-06-16 18:28:02 +00:00
|
|
|
|
|
|
|
public List<ulong> Targets => _targets;
|
2020-12-16 20:07:42 +00:00
|
|
|
public Dictionary<ulong, List<int>> Dependants => _dependants;
|
|
|
|
public Dictionary<ulong, List<int>> Owners => _owners;
|
2020-06-16 18:28:02 +00:00
|
|
|
|
|
|
|
public PtcJumpTable()
|
|
|
|
{
|
2020-12-16 20:07:42 +00:00
|
|
|
_jumpTable = new List<TableEntry<DirectHostAddress>>();
|
|
|
|
_dynamicTable = new List<TableEntry<IndirectHostAddress>>();
|
2020-06-16 18:28:02 +00:00
|
|
|
|
2020-12-16 20:07:42 +00:00
|
|
|
_targets = new List<ulong>();
|
|
|
|
_dependants = new Dictionary<ulong, List<int>>();
|
|
|
|
_owners = new Dictionary<ulong, List<int>>();
|
2020-06-16 18:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Initialize(JumpTable jumpTable)
|
|
|
|
{
|
|
|
|
_targets.Clear();
|
|
|
|
|
|
|
|
foreach (ulong guestAddress in jumpTable.Targets.Keys)
|
|
|
|
{
|
|
|
|
_targets.Add(guestAddress);
|
|
|
|
}
|
|
|
|
|
|
|
|
_dependants.Clear();
|
|
|
|
|
|
|
|
foreach (var item in jumpTable.Dependants)
|
|
|
|
{
|
2020-12-16 20:07:42 +00:00
|
|
|
_dependants.Add(item.Key, new List<int>(item.Value));
|
|
|
|
}
|
|
|
|
|
|
|
|
_owners.Clear();
|
|
|
|
|
|
|
|
foreach (var item in jumpTable.Owners)
|
|
|
|
{
|
|
|
|
_owners.Add(item.Key, new List<int>(item.Value));
|
2020-06-16 18:28:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Clear()
|
|
|
|
{
|
|
|
|
_jumpTable.Clear();
|
|
|
|
_dynamicTable.Clear();
|
|
|
|
|
|
|
|
_targets.Clear();
|
|
|
|
_dependants.Clear();
|
2020-12-16 20:07:42 +00:00
|
|
|
_owners.Clear();
|
2020-06-16 18:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void WriteJumpTable(JumpTable jumpTable, ConcurrentDictionary<ulong, TranslatedFunction> funcs)
|
|
|
|
{
|
2020-12-16 20:07:42 +00:00
|
|
|
// Writes internal state to jump table in-memory, after PTC was loaded.
|
2020-06-16 18:28:02 +00:00
|
|
|
|
|
|
|
foreach (var item in _jumpTable)
|
|
|
|
{
|
2020-12-16 20:07:42 +00:00
|
|
|
long guestAddress = item.GuestAddress;
|
|
|
|
DirectHostAddress directHostAddress = item.HostAddress;
|
2020-06-16 18:28:02 +00:00
|
|
|
|
|
|
|
long hostAddress;
|
|
|
|
|
|
|
|
if (directHostAddress == DirectHostAddress.CallStub)
|
|
|
|
{
|
|
|
|
hostAddress = DirectCallStubs.DirectCallStub(false).ToInt64();
|
|
|
|
}
|
|
|
|
else if (directHostAddress == DirectHostAddress.TailCallStub)
|
|
|
|
{
|
|
|
|
hostAddress = DirectCallStubs.DirectCallStub(true).ToInt64();
|
|
|
|
}
|
|
|
|
else if (directHostAddress == DirectHostAddress.Host)
|
|
|
|
{
|
|
|
|
if (funcs.TryGetValue((ulong)guestAddress, out TranslatedFunction func))
|
|
|
|
{
|
|
|
|
hostAddress = func.FuncPtr.ToInt64();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new KeyNotFoundException($"({nameof(guestAddress)} = 0x{(ulong)guestAddress:X16})");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException(nameof(directHostAddress));
|
|
|
|
}
|
|
|
|
|
2020-12-16 20:07:42 +00:00
|
|
|
int entry = item.EntryIndex;
|
|
|
|
|
|
|
|
jumpTable.Table.SetEntry(entry);
|
|
|
|
jumpTable.ExpandIfNeededJumpTable(entry);
|
|
|
|
|
2020-06-16 18:28:02 +00:00
|
|
|
IntPtr addr = jumpTable.GetEntryAddressJumpTable(entry);
|
|
|
|
|
|
|
|
Marshal.WriteInt64(addr, 0, guestAddress);
|
|
|
|
Marshal.WriteInt64(addr, 8, hostAddress);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void WriteDynamicTable(JumpTable jumpTable)
|
|
|
|
{
|
2020-12-16 20:07:42 +00:00
|
|
|
// Writes internal state to jump table in-memory, after PTC was loaded.
|
|
|
|
|
2020-06-16 18:28:02 +00:00
|
|
|
if (JumpTable.DynamicTableElems > 1)
|
|
|
|
{
|
|
|
|
throw new NotSupportedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (var item in _dynamicTable)
|
|
|
|
{
|
2020-12-16 20:07:42 +00:00
|
|
|
long guestAddress = item.GuestAddress;
|
|
|
|
IndirectHostAddress indirectHostAddress = item.HostAddress;
|
2020-06-16 18:28:02 +00:00
|
|
|
|
|
|
|
long hostAddress;
|
|
|
|
|
|
|
|
if (indirectHostAddress == IndirectHostAddress.CallStub)
|
|
|
|
{
|
|
|
|
hostAddress = DirectCallStubs.IndirectCallStub(false).ToInt64();
|
|
|
|
}
|
|
|
|
else if (indirectHostAddress == IndirectHostAddress.TailCallStub)
|
|
|
|
{
|
|
|
|
hostAddress = DirectCallStubs.IndirectCallStub(true).ToInt64();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException(nameof(indirectHostAddress));
|
|
|
|
}
|
|
|
|
|
2020-12-16 20:07:42 +00:00
|
|
|
int entry = item.EntryIndex;
|
|
|
|
|
|
|
|
jumpTable.DynTable.SetEntry(entry);
|
|
|
|
jumpTable.ExpandIfNeededDynamicTable(entry);
|
|
|
|
|
2020-06-16 18:28:02 +00:00
|
|
|
IntPtr addr = jumpTable.GetEntryAddressDynamicTable(entry);
|
|
|
|
|
|
|
|
Marshal.WriteInt64(addr, 0, guestAddress);
|
|
|
|
Marshal.WriteInt64(addr, 8, hostAddress);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void ReadJumpTable(JumpTable jumpTable)
|
|
|
|
{
|
2020-12-16 20:07:42 +00:00
|
|
|
// Reads in-memory jump table state and store internally for PTC serialization.
|
|
|
|
|
2020-06-16 18:28:02 +00:00
|
|
|
_jumpTable.Clear();
|
|
|
|
|
2020-12-16 20:07:42 +00:00
|
|
|
IEnumerable<int> entries = jumpTable.Table.GetEntries();
|
|
|
|
|
|
|
|
foreach (int entry in entries)
|
2020-06-16 18:28:02 +00:00
|
|
|
{
|
|
|
|
IntPtr addr = jumpTable.GetEntryAddressJumpTable(entry);
|
|
|
|
|
|
|
|
long guestAddress = Marshal.ReadInt64(addr, 0);
|
2020-12-16 20:07:42 +00:00
|
|
|
long hostAddress = Marshal.ReadInt64(addr, 8);
|
2020-06-16 18:28:02 +00:00
|
|
|
|
|
|
|
DirectHostAddress directHostAddress;
|
|
|
|
|
|
|
|
if (hostAddress == DirectCallStubs.DirectCallStub(false).ToInt64())
|
|
|
|
{
|
|
|
|
directHostAddress = DirectHostAddress.CallStub;
|
|
|
|
}
|
|
|
|
else if (hostAddress == DirectCallStubs.DirectCallStub(true).ToInt64())
|
|
|
|
{
|
|
|
|
directHostAddress = DirectHostAddress.TailCallStub;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
directHostAddress = DirectHostAddress.Host;
|
|
|
|
}
|
|
|
|
|
2020-12-16 20:07:42 +00:00
|
|
|
_jumpTable.Add(new TableEntry<DirectHostAddress>(entry, guestAddress, directHostAddress));
|
2020-06-16 18:28:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void ReadDynamicTable(JumpTable jumpTable)
|
|
|
|
{
|
2020-12-16 20:07:42 +00:00
|
|
|
// Reads in-memory jump table state and store internally for PTC serialization.
|
|
|
|
|
2020-06-16 18:28:02 +00:00
|
|
|
if (JumpTable.DynamicTableElems > 1)
|
|
|
|
{
|
|
|
|
throw new NotSupportedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
_dynamicTable.Clear();
|
|
|
|
|
2020-12-16 20:07:42 +00:00
|
|
|
IEnumerable<int> entries = jumpTable.DynTable.GetEntries();
|
|
|
|
|
|
|
|
foreach (int entry in entries)
|
2020-06-16 18:28:02 +00:00
|
|
|
{
|
|
|
|
IntPtr addr = jumpTable.GetEntryAddressDynamicTable(entry);
|
|
|
|
|
|
|
|
long guestAddress = Marshal.ReadInt64(addr, 0);
|
2020-12-16 20:07:42 +00:00
|
|
|
long hostAddress = Marshal.ReadInt64(addr, 8);
|
2020-06-16 18:28:02 +00:00
|
|
|
|
|
|
|
IndirectHostAddress indirectHostAddress;
|
|
|
|
|
|
|
|
if (hostAddress == DirectCallStubs.IndirectCallStub(false).ToInt64())
|
|
|
|
{
|
|
|
|
indirectHostAddress = IndirectHostAddress.CallStub;
|
|
|
|
}
|
|
|
|
else if (hostAddress == DirectCallStubs.IndirectCallStub(true).ToInt64())
|
|
|
|
{
|
|
|
|
indirectHostAddress = IndirectHostAddress.TailCallStub;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException($"({nameof(hostAddress)} = 0x{hostAddress:X16})");
|
|
|
|
}
|
|
|
|
|
2020-12-16 20:07:42 +00:00
|
|
|
_dynamicTable.Add(new TableEntry<IndirectHostAddress>(entry, guestAddress, indirectHostAddress));
|
2020-06-16 18:28:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|