2020-06-16 18:28:02 +00:00
|
|
|
using ARMeilleure.CodeGen;
|
|
|
|
using ARMeilleure.CodeGen.Unwinding;
|
2020-11-18 18:35:54 +00:00
|
|
|
using ARMeilleure.CodeGen.X86;
|
2020-06-16 18:28:02 +00:00
|
|
|
using ARMeilleure.Memory;
|
2020-12-16 20:07:42 +00:00
|
|
|
using ARMeilleure.Translation.Cache;
|
2020-08-30 16:51:53 +00:00
|
|
|
using Ryujinx.Common.Configuration;
|
2020-06-16 18:28:02 +00:00
|
|
|
using Ryujinx.Common.Logging;
|
|
|
|
using System;
|
|
|
|
using System.Buffers.Binary;
|
|
|
|
using System.Collections.Concurrent;
|
2020-12-24 02:58:36 +00:00
|
|
|
using System.Collections.Generic;
|
2020-06-16 18:28:02 +00:00
|
|
|
using System.Diagnostics;
|
|
|
|
using System.IO;
|
|
|
|
using System.IO.Compression;
|
|
|
|
using System.Runtime.InteropServices;
|
2020-12-17 19:32:09 +00:00
|
|
|
using System.Security.Cryptography;
|
2020-06-16 18:28:02 +00:00
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
namespace ARMeilleure.Translation.PTC
|
|
|
|
{
|
|
|
|
public static class Ptc
|
|
|
|
{
|
|
|
|
private const string HeaderMagic = "PTChd";
|
2020-07-17 13:57:49 +00:00
|
|
|
|
2021-01-04 22:45:54 +00:00
|
|
|
private const int InternalVersion = 1817; //! To be incremented manually for each change to the ARMeilleure project.
|
2020-06-16 18:28:02 +00:00
|
|
|
|
|
|
|
private const string ActualDir = "0";
|
|
|
|
private const string BackupDir = "1";
|
|
|
|
|
|
|
|
private const string TitleIdTextDefault = "0000000000000000";
|
|
|
|
private const string DisplayVersionDefault = "0";
|
|
|
|
|
|
|
|
internal const int PageTablePointerIndex = -1; // Must be a negative value.
|
|
|
|
internal const int JumpPointerIndex = -2; // Must be a negative value.
|
|
|
|
internal const int DynamicPointerIndex = -3; // Must be a negative value.
|
|
|
|
|
2020-12-17 19:32:09 +00:00
|
|
|
private const byte FillingByte = 0x00;
|
2020-06-16 18:28:02 +00:00
|
|
|
private const CompressionLevel SaveCompressionLevel = CompressionLevel.Fastest;
|
|
|
|
|
|
|
|
private static readonly MemoryStream _infosStream;
|
|
|
|
private static readonly MemoryStream _codesStream;
|
|
|
|
private static readonly MemoryStream _relocsStream;
|
|
|
|
private static readonly MemoryStream _unwindInfosStream;
|
|
|
|
|
|
|
|
private static readonly BinaryWriter _infosWriter;
|
|
|
|
|
|
|
|
private static readonly ManualResetEvent _waitEvent;
|
|
|
|
|
|
|
|
private static readonly AutoResetEvent _loggerEvent;
|
|
|
|
|
|
|
|
private static readonly object _lock;
|
|
|
|
|
|
|
|
private static bool _disposed;
|
|
|
|
|
|
|
|
private static volatile int _translateCount;
|
|
|
|
|
|
|
|
internal static PtcJumpTable PtcJumpTable { get; private set; }
|
|
|
|
|
|
|
|
internal static string TitleIdText { get; private set; }
|
|
|
|
internal static string DisplayVersion { get; private set; }
|
|
|
|
|
|
|
|
internal static string CachePathActual { get; private set; }
|
|
|
|
internal static string CachePathBackup { get; private set; }
|
|
|
|
|
|
|
|
internal static PtcState State { get; private set; }
|
|
|
|
|
|
|
|
static Ptc()
|
|
|
|
{
|
|
|
|
_infosStream = new MemoryStream();
|
|
|
|
_codesStream = new MemoryStream();
|
|
|
|
_relocsStream = new MemoryStream();
|
|
|
|
_unwindInfosStream = new MemoryStream();
|
|
|
|
|
|
|
|
_infosWriter = new BinaryWriter(_infosStream, EncodingCache.UTF8NoBOM, true);
|
|
|
|
|
|
|
|
_waitEvent = new ManualResetEvent(true);
|
|
|
|
|
|
|
|
_loggerEvent = new AutoResetEvent(false);
|
|
|
|
|
|
|
|
_lock = new object();
|
|
|
|
|
|
|
|
_disposed = false;
|
|
|
|
|
|
|
|
PtcJumpTable = new PtcJumpTable();
|
|
|
|
|
|
|
|
TitleIdText = TitleIdTextDefault;
|
|
|
|
DisplayVersion = DisplayVersionDefault;
|
|
|
|
|
|
|
|
CachePathActual = string.Empty;
|
|
|
|
CachePathBackup = string.Empty;
|
|
|
|
|
|
|
|
Disable();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Initialize(string titleIdText, string displayVersion, bool enabled)
|
|
|
|
{
|
|
|
|
Wait();
|
|
|
|
ClearMemoryStreams();
|
|
|
|
PtcJumpTable.Clear();
|
|
|
|
|
|
|
|
PtcProfiler.Stop();
|
|
|
|
PtcProfiler.Wait();
|
|
|
|
PtcProfiler.ClearEntries();
|
|
|
|
|
|
|
|
if (String.IsNullOrEmpty(titleIdText) || titleIdText == TitleIdTextDefault)
|
|
|
|
{
|
|
|
|
TitleIdText = TitleIdTextDefault;
|
|
|
|
DisplayVersion = DisplayVersionDefault;
|
|
|
|
|
|
|
|
CachePathActual = string.Empty;
|
|
|
|
CachePathBackup = string.Empty;
|
|
|
|
|
|
|
|
Disable();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-03 23:32:53 +00:00
|
|
|
Logger.Info?.Print(LogClass.Ptc, $"Initializing Profiled Persistent Translation Cache (enabled: {enabled}).");
|
2020-06-16 18:28:02 +00:00
|
|
|
|
|
|
|
TitleIdText = titleIdText;
|
|
|
|
DisplayVersion = !String.IsNullOrEmpty(displayVersion) ? displayVersion : DisplayVersionDefault;
|
|
|
|
|
|
|
|
if (enabled)
|
|
|
|
{
|
2020-08-30 16:51:53 +00:00
|
|
|
string workPathActual = Path.Combine(AppDataManager.GamesDirPath, TitleIdText, "cache", "cpu", ActualDir);
|
|
|
|
string workPathBackup = Path.Combine(AppDataManager.GamesDirPath, TitleIdText, "cache", "cpu", BackupDir);
|
2020-06-16 18:28:02 +00:00
|
|
|
|
|
|
|
if (!Directory.Exists(workPathActual))
|
|
|
|
{
|
|
|
|
Directory.CreateDirectory(workPathActual);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Directory.Exists(workPathBackup))
|
|
|
|
{
|
|
|
|
Directory.CreateDirectory(workPathBackup);
|
|
|
|
}
|
|
|
|
|
|
|
|
CachePathActual = Path.Combine(workPathActual, DisplayVersion);
|
|
|
|
CachePathBackup = Path.Combine(workPathBackup, DisplayVersion);
|
|
|
|
|
|
|
|
Enable();
|
|
|
|
|
|
|
|
PreLoad();
|
|
|
|
PtcProfiler.PreLoad();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CachePathActual = string.Empty;
|
|
|
|
CachePathBackup = string.Empty;
|
|
|
|
|
|
|
|
Disable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static void ClearMemoryStreams()
|
|
|
|
{
|
|
|
|
_infosStream.SetLength(0L);
|
|
|
|
_codesStream.SetLength(0L);
|
|
|
|
_relocsStream.SetLength(0L);
|
|
|
|
_unwindInfosStream.SetLength(0L);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void PreLoad()
|
|
|
|
{
|
|
|
|
string fileNameActual = String.Concat(CachePathActual, ".cache");
|
|
|
|
string fileNameBackup = String.Concat(CachePathBackup, ".cache");
|
|
|
|
|
|
|
|
FileInfo fileInfoActual = new FileInfo(fileNameActual);
|
|
|
|
FileInfo fileInfoBackup = new FileInfo(fileNameBackup);
|
|
|
|
|
|
|
|
if (fileInfoActual.Exists && fileInfoActual.Length != 0L)
|
|
|
|
{
|
2020-12-17 19:32:09 +00:00
|
|
|
if (!Load(fileNameActual, false))
|
2020-06-16 18:28:02 +00:00
|
|
|
{
|
|
|
|
if (fileInfoBackup.Exists && fileInfoBackup.Length != 0L)
|
|
|
|
{
|
2020-12-17 19:32:09 +00:00
|
|
|
Load(fileNameBackup, true);
|
2020-06-16 18:28:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (fileInfoBackup.Exists && fileInfoBackup.Length != 0L)
|
|
|
|
{
|
2020-12-17 19:32:09 +00:00
|
|
|
Load(fileNameBackup, true);
|
2020-06-16 18:28:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 19:32:09 +00:00
|
|
|
private static bool Load(string fileName, bool isBackup)
|
2020-06-16 18:28:02 +00:00
|
|
|
{
|
|
|
|
using (FileStream compressedStream = new FileStream(fileName, FileMode.Open))
|
|
|
|
using (DeflateStream deflateStream = new DeflateStream(compressedStream, CompressionMode.Decompress, true))
|
|
|
|
using (MemoryStream stream = new MemoryStream())
|
2020-12-17 19:32:09 +00:00
|
|
|
using (MD5 md5 = MD5.Create())
|
2020-06-16 18:28:02 +00:00
|
|
|
{
|
|
|
|
int hashSize = md5.HashSize / 8;
|
|
|
|
|
2020-11-20 01:51:59 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
deflateStream.CopyTo(stream);
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
InvalidateCompressedStream(compressedStream);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2020-06-16 18:28:02 +00:00
|
|
|
|
|
|
|
stream.Seek(0L, SeekOrigin.Begin);
|
|
|
|
|
|
|
|
byte[] currentHash = new byte[hashSize];
|
|
|
|
stream.Read(currentHash, 0, hashSize);
|
|
|
|
|
|
|
|
byte[] expectedHash = md5.ComputeHash(stream);
|
|
|
|
|
|
|
|
if (!CompareHash(currentHash, expectedHash))
|
|
|
|
{
|
|
|
|
InvalidateCompressedStream(compressedStream);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream.Seek((long)hashSize, SeekOrigin.Begin);
|
|
|
|
|
|
|
|
Header header = ReadHeader(stream);
|
|
|
|
|
|
|
|
if (header.Magic != HeaderMagic)
|
|
|
|
{
|
|
|
|
InvalidateCompressedStream(compressedStream);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (header.CacheFileVersion != InternalVersion)
|
|
|
|
{
|
|
|
|
InvalidateCompressedStream(compressedStream);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (header.FeatureInfo != GetFeatureInfo())
|
|
|
|
{
|
|
|
|
InvalidateCompressedStream(compressedStream);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-12-17 19:32:09 +00:00
|
|
|
if (header.OSPlatform != GetOSPlatform())
|
|
|
|
{
|
|
|
|
InvalidateCompressedStream(compressedStream);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-16 18:28:02 +00:00
|
|
|
if (header.InfosLen % InfoEntry.Stride != 0)
|
|
|
|
{
|
|
|
|
InvalidateCompressedStream(compressedStream);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
byte[] infosBuf = new byte[header.InfosLen];
|
|
|
|
byte[] codesBuf = new byte[header.CodesLen];
|
|
|
|
byte[] relocsBuf = new byte[header.RelocsLen];
|
|
|
|
byte[] unwindInfosBuf = new byte[header.UnwindInfosLen];
|
|
|
|
|
|
|
|
stream.Read(infosBuf, 0, header.InfosLen);
|
|
|
|
stream.Read(codesBuf, 0, header.CodesLen);
|
|
|
|
stream.Read(relocsBuf, 0, header.RelocsLen);
|
|
|
|
stream.Read(unwindInfosBuf, 0, header.UnwindInfosLen);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2020-12-17 19:32:09 +00:00
|
|
|
PtcJumpTable = PtcJumpTable.Deserialize(stream);
|
2020-06-16 18:28:02 +00:00
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
PtcJumpTable = new PtcJumpTable();
|
|
|
|
|
|
|
|
InvalidateCompressedStream(compressedStream);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_infosStream.Write(infosBuf, 0, header.InfosLen);
|
|
|
|
_codesStream.Write(codesBuf, 0, header.CodesLen);
|
|
|
|
_relocsStream.Write(relocsBuf, 0, header.RelocsLen);
|
|
|
|
_unwindInfosStream.Write(unwindInfosBuf, 0, header.UnwindInfosLen);
|
|
|
|
}
|
2020-12-17 19:32:09 +00:00
|
|
|
|
|
|
|
long fileSize = new FileInfo(fileName).Length;
|
|
|
|
|
|
|
|
Logger.Info?.Print(LogClass.Ptc, $"{(isBackup ? "Loaded Backup Translation Cache" : "Loaded Translation Cache")} (size: {fileSize} bytes, translated functions: {GetInfosEntriesCount()}).");
|
|
|
|
|
|
|
|
return true;
|
2020-06-16 18:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static bool CompareHash(ReadOnlySpan<byte> currentHash, ReadOnlySpan<byte> expectedHash)
|
|
|
|
{
|
|
|
|
return currentHash.SequenceEqual(expectedHash);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Header ReadHeader(MemoryStream stream)
|
|
|
|
{
|
|
|
|
using (BinaryReader headerReader = new BinaryReader(stream, EncodingCache.UTF8NoBOM, true))
|
|
|
|
{
|
|
|
|
Header header = new Header();
|
|
|
|
|
|
|
|
header.Magic = headerReader.ReadString();
|
|
|
|
|
2020-12-17 19:32:09 +00:00
|
|
|
header.CacheFileVersion = headerReader.ReadUInt32();
|
2020-06-16 18:28:02 +00:00
|
|
|
header.FeatureInfo = headerReader.ReadUInt64();
|
2020-12-17 19:32:09 +00:00
|
|
|
header.OSPlatform = headerReader.ReadUInt32();
|
2020-06-16 18:28:02 +00:00
|
|
|
|
|
|
|
header.InfosLen = headerReader.ReadInt32();
|
|
|
|
header.CodesLen = headerReader.ReadInt32();
|
|
|
|
header.RelocsLen = headerReader.ReadInt32();
|
|
|
|
header.UnwindInfosLen = headerReader.ReadInt32();
|
|
|
|
|
|
|
|
return header;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void InvalidateCompressedStream(FileStream compressedStream)
|
|
|
|
{
|
|
|
|
compressedStream.SetLength(0L);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void PreSave(object state)
|
|
|
|
{
|
|
|
|
_waitEvent.Reset();
|
|
|
|
|
|
|
|
string fileNameActual = String.Concat(CachePathActual, ".cache");
|
|
|
|
string fileNameBackup = String.Concat(CachePathBackup, ".cache");
|
|
|
|
|
|
|
|
FileInfo fileInfoActual = new FileInfo(fileNameActual);
|
|
|
|
|
|
|
|
if (fileInfoActual.Exists && fileInfoActual.Length != 0L)
|
|
|
|
{
|
|
|
|
File.Copy(fileNameActual, fileNameBackup, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
Save(fileNameActual);
|
|
|
|
|
|
|
|
_waitEvent.Set();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void Save(string fileName)
|
|
|
|
{
|
|
|
|
using (MemoryStream stream = new MemoryStream())
|
2020-12-17 19:32:09 +00:00
|
|
|
using (MD5 md5 = MD5.Create())
|
2020-06-16 18:28:02 +00:00
|
|
|
{
|
|
|
|
int hashSize = md5.HashSize / 8;
|
|
|
|
|
|
|
|
stream.Seek((long)hashSize, SeekOrigin.Begin);
|
|
|
|
|
|
|
|
WriteHeader(stream);
|
|
|
|
|
|
|
|
_infosStream.WriteTo(stream);
|
|
|
|
_codesStream.WriteTo(stream);
|
|
|
|
_relocsStream.WriteTo(stream);
|
|
|
|
_unwindInfosStream.WriteTo(stream);
|
|
|
|
|
2020-12-17 19:32:09 +00:00
|
|
|
PtcJumpTable.Serialize(stream, PtcJumpTable);
|
2020-06-16 18:28:02 +00:00
|
|
|
|
|
|
|
stream.Seek((long)hashSize, SeekOrigin.Begin);
|
|
|
|
byte[] hash = md5.ComputeHash(stream);
|
|
|
|
|
|
|
|
stream.Seek(0L, SeekOrigin.Begin);
|
|
|
|
stream.Write(hash, 0, hashSize);
|
|
|
|
|
|
|
|
using (FileStream compressedStream = new FileStream(fileName, FileMode.OpenOrCreate))
|
|
|
|
using (DeflateStream deflateStream = new DeflateStream(compressedStream, SaveCompressionLevel, true))
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
stream.WriteTo(deflateStream);
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
compressedStream.Position = 0L;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (compressedStream.Position < compressedStream.Length)
|
|
|
|
{
|
|
|
|
compressedStream.SetLength(compressedStream.Position);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-17 19:32:09 +00:00
|
|
|
|
|
|
|
long fileSize = new FileInfo(fileName).Length;
|
|
|
|
|
|
|
|
Logger.Info?.Print(LogClass.Ptc, $"Saved Translation Cache (size: {fileSize} bytes, translated functions: {GetInfosEntriesCount()}).");
|
2020-06-16 18:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void WriteHeader(MemoryStream stream)
|
|
|
|
{
|
|
|
|
using (BinaryWriter headerWriter = new BinaryWriter(stream, EncodingCache.UTF8NoBOM, true))
|
|
|
|
{
|
|
|
|
headerWriter.Write((string)HeaderMagic); // Header.Magic
|
|
|
|
|
2020-12-17 19:32:09 +00:00
|
|
|
headerWriter.Write((uint)InternalVersion); // Header.CacheFileVersion
|
2020-06-16 18:28:02 +00:00
|
|
|
headerWriter.Write((ulong)GetFeatureInfo()); // Header.FeatureInfo
|
2020-12-17 19:32:09 +00:00
|
|
|
headerWriter.Write((uint)GetOSPlatform()); // Header.OSPlatform
|
2020-06-16 18:28:02 +00:00
|
|
|
|
|
|
|
headerWriter.Write((int)_infosStream.Length); // Header.InfosLen
|
|
|
|
headerWriter.Write((int)_codesStream.Length); // Header.CodesLen
|
|
|
|
headerWriter.Write((int)_relocsStream.Length); // Header.RelocsLen
|
|
|
|
headerWriter.Write((int)_unwindInfosStream.Length); // Header.UnwindInfosLen
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 19:32:09 +00:00
|
|
|
internal static void LoadTranslations(ConcurrentDictionary<ulong, TranslatedFunction> funcs, IMemoryManager memory, JumpTable jumpTable)
|
2020-06-16 18:28:02 +00:00
|
|
|
{
|
|
|
|
if ((int)_infosStream.Length == 0 ||
|
|
|
|
(int)_codesStream.Length == 0 ||
|
|
|
|
(int)_relocsStream.Length == 0 ||
|
|
|
|
(int)_unwindInfosStream.Length == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Debug.Assert(funcs.Count == 0);
|
|
|
|
|
|
|
|
_infosStream.Seek(0L, SeekOrigin.Begin);
|
|
|
|
_codesStream.Seek(0L, SeekOrigin.Begin);
|
|
|
|
_relocsStream.Seek(0L, SeekOrigin.Begin);
|
|
|
|
_unwindInfosStream.Seek(0L, SeekOrigin.Begin);
|
|
|
|
|
|
|
|
using (BinaryReader infosReader = new BinaryReader(_infosStream, EncodingCache.UTF8NoBOM, true))
|
|
|
|
using (BinaryReader codesReader = new BinaryReader(_codesStream, EncodingCache.UTF8NoBOM, true))
|
|
|
|
using (BinaryReader relocsReader = new BinaryReader(_relocsStream, EncodingCache.UTF8NoBOM, true))
|
|
|
|
using (BinaryReader unwindInfosReader = new BinaryReader(_unwindInfosStream, EncodingCache.UTF8NoBOM, true))
|
|
|
|
{
|
2020-12-17 19:32:09 +00:00
|
|
|
for (int i = 0; i < GetInfosEntriesCount(); i++)
|
2020-06-16 18:28:02 +00:00
|
|
|
{
|
|
|
|
InfoEntry infoEntry = ReadInfo(infosReader);
|
|
|
|
|
2020-12-17 19:32:09 +00:00
|
|
|
if (infoEntry.Stubbed)
|
2020-06-16 18:28:02 +00:00
|
|
|
{
|
2020-12-17 19:32:09 +00:00
|
|
|
SkipCode(infoEntry.CodeLen);
|
|
|
|
SkipReloc(infoEntry.RelocEntriesCount);
|
|
|
|
SkipUnwindInfo(unwindInfosReader);
|
2020-06-16 18:28:02 +00:00
|
|
|
}
|
2020-12-17 19:32:09 +00:00
|
|
|
else if (infoEntry.HighCq || !PtcProfiler.ProfiledFuncs.TryGetValue(infoEntry.Address, out var value) || !value.highCq)
|
|
|
|
{
|
|
|
|
byte[] code = ReadCode(codesReader, infoEntry.CodeLen);
|
|
|
|
|
|
|
|
if (infoEntry.RelocEntriesCount != 0)
|
|
|
|
{
|
|
|
|
RelocEntry[] relocEntries = GetRelocEntries(relocsReader, infoEntry.RelocEntriesCount);
|
2020-06-16 18:28:02 +00:00
|
|
|
|
2020-12-17 19:32:09 +00:00
|
|
|
PatchCode(code, relocEntries, memory.PageTablePointer, jumpTable);
|
|
|
|
}
|
2020-06-16 18:28:02 +00:00
|
|
|
|
2020-12-17 19:32:09 +00:00
|
|
|
UnwindInfo unwindInfo = ReadUnwindInfo(unwindInfosReader);
|
2020-06-16 18:28:02 +00:00
|
|
|
|
2020-12-17 19:32:09 +00:00
|
|
|
TranslatedFunction func = FastTranslate(code, infoEntry.GuestSize, unwindInfo, infoEntry.HighCq);
|
|
|
|
|
|
|
|
bool isAddressUnique = funcs.TryAdd(infoEntry.Address, func);
|
|
|
|
|
|
|
|
Debug.Assert(isAddressUnique, $"The address 0x{infoEntry.Address:X16} is not unique.");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
infoEntry.Stubbed = true;
|
|
|
|
UpdateInfo(infoEntry);
|
|
|
|
|
|
|
|
StubCode(infoEntry.CodeLen);
|
|
|
|
StubReloc(infoEntry.RelocEntriesCount);
|
|
|
|
StubUnwindInfo(unwindInfosReader);
|
|
|
|
}
|
2020-06-16 18:28:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_infosStream.Position < _infosStream.Length ||
|
|
|
|
_codesStream.Position < _codesStream.Length ||
|
|
|
|
_relocsStream.Position < _relocsStream.Length ||
|
|
|
|
_unwindInfosStream.Position < _unwindInfosStream.Length)
|
|
|
|
{
|
|
|
|
throw new Exception("Could not reach the end of one or more memory streams.");
|
|
|
|
}
|
|
|
|
|
|
|
|
jumpTable.Initialize(PtcJumpTable, funcs);
|
|
|
|
|
|
|
|
PtcJumpTable.WriteJumpTable(jumpTable, funcs);
|
|
|
|
PtcJumpTable.WriteDynamicTable(jumpTable);
|
2020-12-17 19:32:09 +00:00
|
|
|
|
|
|
|
Logger.Info?.Print(LogClass.Ptc, $"{funcs.Count} translated functions loaded");
|
|
|
|
}
|
|
|
|
|
|
|
|
private static int GetInfosEntriesCount()
|
|
|
|
{
|
|
|
|
return (int)_infosStream.Length / InfoEntry.Stride;
|
2020-06-16 18:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static InfoEntry ReadInfo(BinaryReader infosReader)
|
|
|
|
{
|
|
|
|
InfoEntry infoEntry = new InfoEntry();
|
|
|
|
|
2020-12-16 20:07:42 +00:00
|
|
|
infoEntry.Address = infosReader.ReadUInt64();
|
|
|
|
infoEntry.GuestSize = infosReader.ReadUInt64();
|
2020-06-16 18:28:02 +00:00
|
|
|
infoEntry.HighCq = infosReader.ReadBoolean();
|
2020-12-17 19:32:09 +00:00
|
|
|
infoEntry.Stubbed = infosReader.ReadBoolean();
|
2020-06-16 18:28:02 +00:00
|
|
|
infoEntry.CodeLen = infosReader.ReadInt32();
|
|
|
|
infoEntry.RelocEntriesCount = infosReader.ReadInt32();
|
|
|
|
|
|
|
|
return infoEntry;
|
|
|
|
}
|
|
|
|
|
2020-12-17 19:32:09 +00:00
|
|
|
private static void SkipCode(int codeLen)
|
|
|
|
{
|
|
|
|
_codesStream.Seek(codeLen, SeekOrigin.Current);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void SkipReloc(int relocEntriesCount)
|
|
|
|
{
|
|
|
|
_relocsStream.Seek(relocEntriesCount * RelocEntry.Stride, SeekOrigin.Current);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void SkipUnwindInfo(BinaryReader unwindInfosReader)
|
|
|
|
{
|
|
|
|
int pushEntriesLength = unwindInfosReader.ReadInt32();
|
|
|
|
|
|
|
|
_unwindInfosStream.Seek(pushEntriesLength * UnwindPushEntry.Stride + UnwindInfo.Stride, SeekOrigin.Current);
|
|
|
|
}
|
|
|
|
|
2020-06-16 18:28:02 +00:00
|
|
|
private static byte[] ReadCode(BinaryReader codesReader, int codeLen)
|
|
|
|
{
|
|
|
|
byte[] codeBuf = new byte[codeLen];
|
|
|
|
|
|
|
|
codesReader.Read(codeBuf, 0, codeLen);
|
|
|
|
|
|
|
|
return codeBuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static RelocEntry[] GetRelocEntries(BinaryReader relocsReader, int relocEntriesCount)
|
|
|
|
{
|
|
|
|
RelocEntry[] relocEntries = new RelocEntry[relocEntriesCount];
|
|
|
|
|
|
|
|
for (int i = 0; i < relocEntriesCount; i++)
|
|
|
|
{
|
|
|
|
int position = relocsReader.ReadInt32();
|
|
|
|
int index = relocsReader.ReadInt32();
|
|
|
|
|
|
|
|
relocEntries[i] = new RelocEntry(position, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
return relocEntries;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void PatchCode(Span<byte> code, RelocEntry[] relocEntries, IntPtr pageTablePointer, JumpTable jumpTable)
|
|
|
|
{
|
|
|
|
foreach (RelocEntry relocEntry in relocEntries)
|
|
|
|
{
|
|
|
|
ulong imm;
|
|
|
|
|
|
|
|
if (relocEntry.Index == PageTablePointerIndex)
|
|
|
|
{
|
|
|
|
imm = (ulong)pageTablePointer.ToInt64();
|
|
|
|
}
|
|
|
|
else if (relocEntry.Index == JumpPointerIndex)
|
|
|
|
{
|
|
|
|
imm = (ulong)jumpTable.JumpPointer.ToInt64();
|
|
|
|
}
|
|
|
|
else if (relocEntry.Index == DynamicPointerIndex)
|
|
|
|
{
|
|
|
|
imm = (ulong)jumpTable.DynamicPointer.ToInt64();
|
|
|
|
}
|
|
|
|
else if (Delegates.TryGetDelegateFuncPtrByIndex(relocEntry.Index, out IntPtr funcPtr))
|
|
|
|
{
|
|
|
|
imm = (ulong)funcPtr.ToInt64();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new Exception($"Unexpected reloc entry {relocEntry}.");
|
|
|
|
}
|
|
|
|
|
|
|
|
BinaryPrimitives.WriteUInt64LittleEndian(code.Slice(relocEntry.Position, 8), imm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static UnwindInfo ReadUnwindInfo(BinaryReader unwindInfosReader)
|
|
|
|
{
|
|
|
|
int pushEntriesLength = unwindInfosReader.ReadInt32();
|
|
|
|
|
|
|
|
UnwindPushEntry[] pushEntries = new UnwindPushEntry[pushEntriesLength];
|
|
|
|
|
|
|
|
for (int i = 0; i < pushEntriesLength; i++)
|
|
|
|
{
|
|
|
|
int pseudoOp = unwindInfosReader.ReadInt32();
|
|
|
|
int prologOffset = unwindInfosReader.ReadInt32();
|
|
|
|
int regIndex = unwindInfosReader.ReadInt32();
|
|
|
|
int stackOffsetOrAllocSize = unwindInfosReader.ReadInt32();
|
|
|
|
|
|
|
|
pushEntries[i] = new UnwindPushEntry((UnwindPseudoOp)pseudoOp, prologOffset, regIndex, stackOffsetOrAllocSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
int prologueSize = unwindInfosReader.ReadInt32();
|
|
|
|
|
|
|
|
return new UnwindInfo(pushEntries, prologueSize);
|
|
|
|
}
|
|
|
|
|
2020-12-16 20:07:42 +00:00
|
|
|
private static TranslatedFunction FastTranslate(byte[] code, ulong guestSize, UnwindInfo unwindInfo, bool highCq)
|
2020-06-16 18:28:02 +00:00
|
|
|
{
|
|
|
|
CompiledFunction cFunc = new CompiledFunction(code, unwindInfo);
|
|
|
|
|
|
|
|
IntPtr codePtr = JitCache.Map(cFunc);
|
|
|
|
|
|
|
|
GuestFunction gFunc = Marshal.GetDelegateForFunctionPointer<GuestFunction>(codePtr);
|
|
|
|
|
2020-12-16 20:07:42 +00:00
|
|
|
TranslatedFunction tFunc = new TranslatedFunction(gFunc, guestSize, highCq);
|
2020-06-16 18:28:02 +00:00
|
|
|
|
|
|
|
return tFunc;
|
|
|
|
}
|
|
|
|
|
2020-12-17 19:32:09 +00:00
|
|
|
private static void UpdateInfo(InfoEntry infoEntry)
|
|
|
|
{
|
|
|
|
_infosStream.Seek(-InfoEntry.Stride, SeekOrigin.Current);
|
|
|
|
|
|
|
|
// WriteInfo.
|
|
|
|
_infosWriter.Write((ulong)infoEntry.Address);
|
|
|
|
_infosWriter.Write((ulong)infoEntry.GuestSize);
|
|
|
|
_infosWriter.Write((bool)infoEntry.HighCq);
|
|
|
|
_infosWriter.Write((bool)infoEntry.Stubbed);
|
|
|
|
_infosWriter.Write((int)infoEntry.CodeLen);
|
|
|
|
_infosWriter.Write((int)infoEntry.RelocEntriesCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void StubCode(int codeLen)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < codeLen; i++)
|
|
|
|
{
|
|
|
|
_codesStream.WriteByte(FillingByte);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void StubReloc(int relocEntriesCount)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < relocEntriesCount * RelocEntry.Stride; i++)
|
|
|
|
{
|
|
|
|
_relocsStream.WriteByte(FillingByte);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void StubUnwindInfo(BinaryReader unwindInfosReader)
|
|
|
|
{
|
|
|
|
int pushEntriesLength = unwindInfosReader.ReadInt32();
|
|
|
|
|
|
|
|
for (int i = 0; i < pushEntriesLength * UnwindPushEntry.Stride + UnwindInfo.Stride; i++)
|
|
|
|
{
|
|
|
|
_unwindInfosStream.WriteByte(FillingByte);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-16 18:28:02 +00:00
|
|
|
internal static void MakeAndSaveTranslations(ConcurrentDictionary<ulong, TranslatedFunction> funcs, IMemoryManager memory, JumpTable jumpTable)
|
|
|
|
{
|
2020-12-17 19:32:09 +00:00
|
|
|
var profiledFuncsToTranslate = PtcProfiler.GetProfiledFuncsToTranslate(funcs);
|
|
|
|
|
|
|
|
if (profiledFuncsToTranslate.Count == 0)
|
2020-06-16 18:28:02 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_translateCount = 0;
|
|
|
|
|
2020-12-17 19:32:09 +00:00
|
|
|
ThreadPool.QueueUserWorkItem(TranslationLogger, profiledFuncsToTranslate.Count);
|
2020-06-16 18:28:02 +00:00
|
|
|
|
2020-12-24 02:58:36 +00:00
|
|
|
void TranslateFuncs()
|
2020-06-16 18:28:02 +00:00
|
|
|
{
|
2020-12-24 02:58:36 +00:00
|
|
|
while (profiledFuncsToTranslate.TryDequeue(out var item))
|
|
|
|
{
|
|
|
|
ulong address = item.address;
|
2020-06-16 18:28:02 +00:00
|
|
|
|
2020-12-24 02:58:36 +00:00
|
|
|
Debug.Assert(PtcProfiler.IsAddressInStaticCodeRange(address));
|
2020-06-16 18:28:02 +00:00
|
|
|
|
2020-12-24 02:58:36 +00:00
|
|
|
TranslatedFunction func = Translator.Translate(memory, jumpTable, address, item.mode, item.highCq);
|
2020-06-16 18:28:02 +00:00
|
|
|
|
2020-12-24 02:58:36 +00:00
|
|
|
bool isAddressUnique = funcs.TryAdd(address, func);
|
2020-06-16 18:28:02 +00:00
|
|
|
|
2020-12-24 02:58:36 +00:00
|
|
|
Debug.Assert(isAddressUnique, $"The address 0x{address:X16} is not unique.");
|
2020-06-16 18:28:02 +00:00
|
|
|
|
2020-12-24 03:44:39 +00:00
|
|
|
if (func.HighCq)
|
|
|
|
{
|
|
|
|
jumpTable.RegisterFunction(address, func);
|
|
|
|
}
|
|
|
|
|
2020-12-24 02:58:36 +00:00
|
|
|
Interlocked.Increment(ref _translateCount);
|
|
|
|
|
|
|
|
if (State != PtcState.Enabled)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2020-06-16 18:28:02 +00:00
|
|
|
}
|
2020-12-24 02:58:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int maxDegreeOfParallelism = (Environment.ProcessorCount * 3) / 4;
|
2020-06-16 18:28:02 +00:00
|
|
|
|
2020-12-24 02:58:36 +00:00
|
|
|
List<Thread> threads = new List<Thread>();
|
2020-12-17 19:32:09 +00:00
|
|
|
|
2020-12-24 02:58:36 +00:00
|
|
|
for (int i = 0; i < maxDegreeOfParallelism; i++)
|
|
|
|
{
|
|
|
|
Thread thread = new Thread(TranslateFuncs);
|
|
|
|
thread.IsBackground = true;
|
|
|
|
|
|
|
|
threads.Add(thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
threads.ForEach((thread) => thread.Start());
|
|
|
|
threads.ForEach((thread) => thread.Join());
|
|
|
|
|
|
|
|
threads.Clear();
|
2020-06-16 18:28:02 +00:00
|
|
|
|
|
|
|
_loggerEvent.Set();
|
|
|
|
|
2020-12-24 02:58:36 +00:00
|
|
|
Translator.ResetPools();
|
|
|
|
|
2020-12-17 19:32:09 +00:00
|
|
|
PtcJumpTable.Initialize(jumpTable);
|
2020-06-16 18:28:02 +00:00
|
|
|
|
2020-12-17 19:32:09 +00:00
|
|
|
PtcJumpTable.ReadJumpTable(jumpTable);
|
|
|
|
PtcJumpTable.ReadDynamicTable(jumpTable);
|
2020-06-16 18:28:02 +00:00
|
|
|
|
2020-12-17 19:32:09 +00:00
|
|
|
ThreadPool.QueueUserWorkItem(PreSave);
|
2020-06-16 18:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void TranslationLogger(object state)
|
|
|
|
{
|
|
|
|
const int refreshRate = 1; // Seconds.
|
|
|
|
|
2020-12-17 19:32:09 +00:00
|
|
|
int profiledFuncsToTranslateCount = (int)state;
|
2020-06-16 18:28:02 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2020-12-17 19:32:09 +00:00
|
|
|
Logger.Info?.Print(LogClass.Ptc, $"{_translateCount} of {profiledFuncsToTranslateCount} functions translated");
|
2020-06-16 18:28:02 +00:00
|
|
|
}
|
|
|
|
while (!_loggerEvent.WaitOne(refreshRate * 1000));
|
|
|
|
|
2020-12-17 19:32:09 +00:00
|
|
|
Logger.Info?.Print(LogClass.Ptc, $"{_translateCount} of {profiledFuncsToTranslateCount} functions translated");
|
2020-06-16 18:28:02 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 20:07:42 +00:00
|
|
|
internal static void WriteInfoCodeReloc(ulong address, ulong guestSize, bool highCq, PtcInfo ptcInfo)
|
2020-06-16 18:28:02 +00:00
|
|
|
{
|
|
|
|
lock (_lock)
|
|
|
|
{
|
|
|
|
// WriteInfo.
|
2020-12-16 20:07:42 +00:00
|
|
|
_infosWriter.Write((ulong)address); // InfoEntry.Address
|
|
|
|
_infosWriter.Write((ulong)guestSize); // InfoEntry.GuestSize
|
2020-06-16 18:28:02 +00:00
|
|
|
_infosWriter.Write((bool)highCq); // InfoEntry.HighCq
|
2020-12-17 19:32:09 +00:00
|
|
|
_infosWriter.Write((bool)false); // InfoEntry.Stubbed
|
2020-06-16 18:28:02 +00:00
|
|
|
_infosWriter.Write((int)ptcInfo.CodeStream.Length); // InfoEntry.CodeLen
|
|
|
|
_infosWriter.Write((int)ptcInfo.RelocEntriesCount); // InfoEntry.RelocEntriesCount
|
|
|
|
|
|
|
|
// WriteCode.
|
|
|
|
ptcInfo.CodeStream.WriteTo(_codesStream);
|
|
|
|
|
|
|
|
// WriteReloc.
|
|
|
|
ptcInfo.RelocStream.WriteTo(_relocsStream);
|
|
|
|
|
|
|
|
// WriteUnwindInfo.
|
|
|
|
ptcInfo.UnwindInfoStream.WriteTo(_unwindInfosStream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static ulong GetFeatureInfo()
|
|
|
|
{
|
2020-11-18 18:35:54 +00:00
|
|
|
return (ulong)HardwareCapabilities.FeatureInfoEdx << 32 | (uint)HardwareCapabilities.FeatureInfoEcx;
|
2020-06-16 18:28:02 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 19:32:09 +00:00
|
|
|
private static uint GetOSPlatform()
|
|
|
|
{
|
|
|
|
uint osPlatform = 0u;
|
|
|
|
|
|
|
|
osPlatform |= (RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD) ? 1u : 0u) << 0;
|
|
|
|
osPlatform |= (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? 1u : 0u) << 1;
|
|
|
|
osPlatform |= (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 1u : 0u) << 2;
|
|
|
|
osPlatform |= (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 1u : 0u) << 3;
|
|
|
|
|
|
|
|
return osPlatform;
|
|
|
|
}
|
|
|
|
|
2020-06-16 18:28:02 +00:00
|
|
|
private struct Header
|
|
|
|
{
|
|
|
|
public string Magic;
|
|
|
|
|
2020-12-17 19:32:09 +00:00
|
|
|
public uint CacheFileVersion;
|
2020-06-16 18:28:02 +00:00
|
|
|
public ulong FeatureInfo;
|
2020-12-17 19:32:09 +00:00
|
|
|
public uint OSPlatform;
|
2020-06-16 18:28:02 +00:00
|
|
|
|
|
|
|
public int InfosLen;
|
|
|
|
public int CodesLen;
|
|
|
|
public int RelocsLen;
|
|
|
|
public int UnwindInfosLen;
|
|
|
|
}
|
|
|
|
|
|
|
|
private struct InfoEntry
|
|
|
|
{
|
2020-12-17 19:32:09 +00:00
|
|
|
public const int Stride = 26; // Bytes.
|
2020-06-16 18:28:02 +00:00
|
|
|
|
2020-12-16 20:07:42 +00:00
|
|
|
public ulong Address;
|
|
|
|
public ulong GuestSize;
|
2020-06-16 18:28:02 +00:00
|
|
|
public bool HighCq;
|
2020-12-17 19:32:09 +00:00
|
|
|
public bool Stubbed;
|
2020-06-16 18:28:02 +00:00
|
|
|
public int CodeLen;
|
|
|
|
public int RelocEntriesCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void Enable()
|
|
|
|
{
|
|
|
|
State = PtcState.Enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Continue()
|
|
|
|
{
|
|
|
|
if (State == PtcState.Enabled)
|
|
|
|
{
|
|
|
|
State = PtcState.Continuing;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Close()
|
|
|
|
{
|
|
|
|
if (State == PtcState.Enabled ||
|
|
|
|
State == PtcState.Continuing)
|
|
|
|
{
|
|
|
|
State = PtcState.Closing;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static void Disable()
|
|
|
|
{
|
|
|
|
State = PtcState.Disabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void Wait()
|
|
|
|
{
|
|
|
|
_waitEvent.WaitOne();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Dispose()
|
|
|
|
{
|
|
|
|
if (!_disposed)
|
|
|
|
{
|
|
|
|
_disposed = true;
|
|
|
|
|
|
|
|
Wait();
|
|
|
|
_waitEvent.Dispose();
|
|
|
|
|
|
|
|
_infosWriter.Dispose();
|
|
|
|
|
|
|
|
_infosStream.Dispose();
|
|
|
|
_codesStream.Dispose();
|
|
|
|
_relocsStream.Dispose();
|
|
|
|
_unwindInfosStream.Dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-24 03:44:39 +00:00
|
|
|
}
|