2020-06-16 19:28:02 +01:00
|
|
|
using ARMeilleure.CodeGen;
|
|
|
|
using ARMeilleure.CodeGen.Unwinding;
|
2020-11-18 18:35:54 +00:00
|
|
|
using ARMeilleure.CodeGen.X86;
|
2021-04-09 17:49:15 +01:00
|
|
|
using ARMeilleure.Common;
|
2020-06-16 19:28:02 +01:00
|
|
|
using ARMeilleure.Memory;
|
2020-12-16 20:07:42 +00:00
|
|
|
using ARMeilleure.Translation.Cache;
|
2021-02-22 02:23:48 +00:00
|
|
|
using Ryujinx.Common;
|
2020-08-30 17:51:53 +01:00
|
|
|
using Ryujinx.Common.Configuration;
|
2020-06-16 19:28:02 +01: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 19:28:02 +01:00
|
|
|
using System.Diagnostics;
|
|
|
|
using System.IO;
|
|
|
|
using System.IO.Compression;
|
2021-02-22 02:23:48 +00:00
|
|
|
using System.Runtime;
|
|
|
|
using System.Runtime.CompilerServices;
|
2020-06-16 19:28:02 +01:00
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
using System.Threading;
|
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
using static ARMeilleure.Translation.PTC.PtcFormatter;
|
|
|
|
|
2020-06-16 19:28:02 +01:00
|
|
|
namespace ARMeilleure.Translation.PTC
|
|
|
|
{
|
|
|
|
public static class Ptc
|
|
|
|
{
|
2021-04-13 02:24:36 +01:00
|
|
|
private const string OuterHeaderMagicString = "PTCohd\0\0";
|
|
|
|
private const string InnerHeaderMagicString = "PTCihd\0\0";
|
2020-07-17 14:57:49 +01:00
|
|
|
|
2021-04-10 12:11:39 +01:00
|
|
|
private const uint InternalVersion = 2190; //! To be incremented manually for each change to the ARMeilleure project.
|
2020-06-16 19:28:02 +01: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.
|
2021-04-09 17:55:24 +01:00
|
|
|
internal const int CountTableIndex = -4; // Must be a negative value.
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2020-12-17 19:32:09 +00:00
|
|
|
private const byte FillingByte = 0x00;
|
2020-06-16 19:28:02 +01:00
|
|
|
private const CompressionLevel SaveCompressionLevel = CompressionLevel.Fastest;
|
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
// Carriers.
|
2021-02-22 02:23:48 +00:00
|
|
|
private static MemoryStream _infosStream;
|
2021-04-13 02:24:36 +01:00
|
|
|
private static List<byte[]> _codesList;
|
2021-02-22 02:23:48 +00:00
|
|
|
private static MemoryStream _relocsStream;
|
|
|
|
private static MemoryStream _unwindInfosStream;
|
|
|
|
|
|
|
|
private static BinaryWriter _infosWriter;
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
private static readonly ulong _outerHeaderMagic;
|
|
|
|
private static readonly ulong _innerHeaderMagic;
|
2020-06-16 19:28:02 +01:00
|
|
|
|
|
|
|
private static readonly ManualResetEvent _waitEvent;
|
|
|
|
|
|
|
|
private static readonly object _lock;
|
|
|
|
|
|
|
|
private static bool _disposed;
|
|
|
|
|
|
|
|
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; }
|
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
// Progress reporting helpers.
|
2021-03-22 18:40:07 +00:00
|
|
|
private static volatile int _translateCount;
|
|
|
|
private static volatile int _translateTotalCount;
|
|
|
|
public static event Action<PtcLoadingState, int, int> PtcStateChanged;
|
2021-03-03 00:39:36 +00:00
|
|
|
|
2020-06-16 19:28:02 +01:00
|
|
|
static Ptc()
|
|
|
|
{
|
2021-04-13 02:24:36 +01:00
|
|
|
InitializeCarriers();
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
_outerHeaderMagic = BinaryPrimitives.ReadUInt64LittleEndian(EncodingCache.UTF8NoBOM.GetBytes(OuterHeaderMagicString).AsSpan());
|
|
|
|
_innerHeaderMagic = BinaryPrimitives.ReadUInt64LittleEndian(EncodingCache.UTF8NoBOM.GetBytes(InnerHeaderMagicString).AsSpan());
|
2020-06-16 19:28:02 +01:00
|
|
|
|
|
|
|
_waitEvent = new ManualResetEvent(true);
|
|
|
|
|
|
|
|
_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();
|
|
|
|
|
|
|
|
PtcProfiler.Wait();
|
|
|
|
PtcProfiler.ClearEntries();
|
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
Logger.Info?.Print(LogClass.Ptc, $"Initializing Profiled Persistent Translation Cache (enabled: {enabled}).");
|
|
|
|
|
|
|
|
if (!enabled || string.IsNullOrEmpty(titleIdText) || titleIdText == TitleIdTextDefault)
|
2020-06-16 19:28:02 +01:00
|
|
|
{
|
|
|
|
TitleIdText = TitleIdTextDefault;
|
|
|
|
DisplayVersion = DisplayVersionDefault;
|
|
|
|
|
|
|
|
CachePathActual = string.Empty;
|
|
|
|
CachePathBackup = string.Empty;
|
|
|
|
|
|
|
|
Disable();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
TitleIdText = titleIdText;
|
2021-02-22 02:23:48 +00:00
|
|
|
DisplayVersion = !string.IsNullOrEmpty(displayVersion) ? displayVersion : DisplayVersionDefault;
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
string workPathActual = Path.Combine(AppDataManager.GamesDirPath, TitleIdText, "cache", "cpu", ActualDir);
|
|
|
|
string workPathBackup = Path.Combine(AppDataManager.GamesDirPath, TitleIdText, "cache", "cpu", BackupDir);
|
|
|
|
|
|
|
|
if (!Directory.Exists(workPathActual))
|
2020-06-16 19:28:02 +01:00
|
|
|
{
|
2021-02-22 02:23:48 +00:00
|
|
|
Directory.CreateDirectory(workPathActual);
|
|
|
|
}
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
if (!Directory.Exists(workPathBackup))
|
|
|
|
{
|
|
|
|
Directory.CreateDirectory(workPathBackup);
|
|
|
|
}
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
CachePathActual = Path.Combine(workPathActual, DisplayVersion);
|
|
|
|
CachePathBackup = Path.Combine(workPathBackup, DisplayVersion);
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
PreLoad();
|
|
|
|
PtcProfiler.PreLoad();
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
Enable();
|
|
|
|
}
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
private static void InitializeCarriers()
|
2021-02-22 02:23:48 +00:00
|
|
|
{
|
|
|
|
_infosStream = new MemoryStream();
|
2021-04-13 02:24:36 +01:00
|
|
|
_codesList = new List<byte[]>();
|
2021-02-22 02:23:48 +00:00
|
|
|
_relocsStream = new MemoryStream();
|
|
|
|
_unwindInfosStream = new MemoryStream();
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
_infosWriter = new BinaryWriter(_infosStream, EncodingCache.UTF8NoBOM, true);
|
2020-06-16 19:28:02 +01:00
|
|
|
}
|
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
private static void DisposeCarriers()
|
2020-06-16 19:28:02 +01:00
|
|
|
{
|
2021-02-22 02:23:48 +00:00
|
|
|
_infosWriter.Dispose();
|
|
|
|
|
|
|
|
_infosStream.Dispose();
|
2021-04-13 02:24:36 +01:00
|
|
|
_codesList.Clear();
|
2021-02-22 02:23:48 +00:00
|
|
|
_relocsStream.Dispose();
|
|
|
|
_unwindInfosStream.Dispose();
|
|
|
|
}
|
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
private static bool AreCarriersEmpty()
|
2021-02-22 02:23:48 +00:00
|
|
|
{
|
2021-04-13 02:24:36 +01:00
|
|
|
return _infosStream.Length == 0L && _codesList.Count == 0 && _relocsStream.Length == 0L && _unwindInfosStream.Length == 0L;
|
2021-02-22 02:23:48 +00:00
|
|
|
}
|
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
private static void ResetCarriersIfNeeded()
|
2021-02-22 02:23:48 +00:00
|
|
|
{
|
2021-04-13 02:24:36 +01:00
|
|
|
if (AreCarriersEmpty())
|
2021-02-22 02:23:48 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
DisposeCarriers();
|
2021-02-22 02:23:48 +00:00
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
InitializeCarriers();
|
2020-06-16 19:28:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void PreLoad()
|
|
|
|
{
|
2021-02-22 02:23:48 +00:00
|
|
|
string fileNameActual = string.Concat(CachePathActual, ".cache");
|
|
|
|
string fileNameBackup = string.Concat(CachePathBackup, ".cache");
|
2020-06-16 19:28:02 +01:00
|
|
|
|
|
|
|
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 19:28:02 +01:00
|
|
|
{
|
|
|
|
if (fileInfoBackup.Exists && fileInfoBackup.Length != 0L)
|
|
|
|
{
|
2020-12-17 19:32:09 +00:00
|
|
|
Load(fileNameBackup, true);
|
2020-06-16 19:28:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (fileInfoBackup.Exists && fileInfoBackup.Length != 0L)
|
|
|
|
{
|
2020-12-17 19:32:09 +00:00
|
|
|
Load(fileNameBackup, true);
|
2020-06-16 19:28:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
private static unsafe bool Load(string fileName, bool isBackup)
|
2020-06-16 19:28:02 +01:00
|
|
|
{
|
2021-02-22 02:23:48 +00:00
|
|
|
using (FileStream compressedStream = new(fileName, FileMode.Open))
|
|
|
|
using (DeflateStream deflateStream = new(compressedStream, CompressionMode.Decompress, true))
|
2020-06-16 19:28:02 +01:00
|
|
|
{
|
2021-04-13 02:24:36 +01:00
|
|
|
OuterHeader outerHeader = DeserializeStructure<OuterHeader>(compressedStream);
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
if (!outerHeader.IsHeaderValid())
|
|
|
|
{
|
|
|
|
InvalidateCompressedStream(compressedStream);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (outerHeader.Magic != _outerHeaderMagic)
|
|
|
|
{
|
|
|
|
InvalidateCompressedStream(compressedStream);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (outerHeader.CacheFileVersion != InternalVersion)
|
|
|
|
{
|
|
|
|
InvalidateCompressedStream(compressedStream);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2021-02-22 02:23:48 +00:00
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
if (outerHeader.Endianness != GetEndianness())
|
2020-11-20 01:51:59 +00:00
|
|
|
{
|
|
|
|
InvalidateCompressedStream(compressedStream);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
if (outerHeader.FeatureInfo != GetFeatureInfo())
|
|
|
|
{
|
|
|
|
InvalidateCompressedStream(compressedStream);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (outerHeader.OSPlatform != GetOSPlatform())
|
|
|
|
{
|
|
|
|
InvalidateCompressedStream(compressedStream);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
IntPtr intPtr = IntPtr.Zero;
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
try
|
2020-06-16 19:28:02 +01:00
|
|
|
{
|
2021-04-13 02:24:36 +01:00
|
|
|
intPtr = Marshal.AllocHGlobal(new IntPtr(outerHeader.UncompressedStreamSize));
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
using (UnmanagedMemoryStream stream = new((byte*)intPtr.ToPointer(), outerHeader.UncompressedStreamSize, outerHeader.UncompressedStreamSize, FileAccess.ReadWrite))
|
2021-02-22 02:23:48 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
deflateStream.CopyTo(stream);
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
InvalidateCompressedStream(compressedStream);
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
Debug.Assert(stream.Position == stream.Length);
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
stream.Seek(0L, SeekOrigin.Begin);
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
InnerHeader innerHeader = DeserializeStructure<InnerHeader>(stream);
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
if (!innerHeader.IsHeaderValid())
|
2021-02-22 02:23:48 +00:00
|
|
|
{
|
|
|
|
InvalidateCompressedStream(compressedStream);
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
if (innerHeader.Magic != _innerHeaderMagic)
|
2021-02-22 02:23:48 +00:00
|
|
|
{
|
|
|
|
InvalidateCompressedStream(compressedStream);
|
2020-12-17 19:32:09 +00:00
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-12-17 19:32:09 +00:00
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
ReadOnlySpan<byte> infosBytes = new(stream.PositionPointer, innerHeader.InfosLength);
|
|
|
|
stream.Seek(innerHeader.InfosLength, SeekOrigin.Current);
|
|
|
|
|
|
|
|
Hash128 infosHash = XXHash128.ComputeHash(infosBytes);
|
|
|
|
|
|
|
|
if (innerHeader.InfosHash != infosHash)
|
2021-02-22 02:23:48 +00:00
|
|
|
{
|
|
|
|
InvalidateCompressedStream(compressedStream);
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
ReadOnlySpan<byte> codesBytes = (int)innerHeader.CodesLength > 0 ? new(stream.PositionPointer, (int)innerHeader.CodesLength) : ReadOnlySpan<byte>.Empty;
|
|
|
|
stream.Seek(innerHeader.CodesLength, SeekOrigin.Current);
|
|
|
|
|
|
|
|
Hash128 codesHash = XXHash128.ComputeHash(codesBytes);
|
|
|
|
|
|
|
|
if (innerHeader.CodesHash != codesHash)
|
2021-02-22 02:23:48 +00:00
|
|
|
{
|
|
|
|
InvalidateCompressedStream(compressedStream);
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
ReadOnlySpan<byte> relocsBytes = new(stream.PositionPointer, innerHeader.RelocsLength);
|
|
|
|
stream.Seek(innerHeader.RelocsLength, SeekOrigin.Current);
|
|
|
|
|
|
|
|
Hash128 relocsHash = XXHash128.ComputeHash(relocsBytes);
|
|
|
|
|
|
|
|
if (innerHeader.RelocsHash != relocsHash)
|
2021-02-22 02:23:48 +00:00
|
|
|
{
|
|
|
|
InvalidateCompressedStream(compressedStream);
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
ReadOnlySpan<byte> unwindInfosBytes = new(stream.PositionPointer, innerHeader.UnwindInfosLength);
|
|
|
|
stream.Seek(innerHeader.UnwindInfosLength, SeekOrigin.Current);
|
|
|
|
|
|
|
|
Hash128 unwindInfosHash = XXHash128.ComputeHash(unwindInfosBytes);
|
|
|
|
|
|
|
|
if (innerHeader.UnwindInfosHash != unwindInfosHash)
|
2021-02-22 02:23:48 +00:00
|
|
|
{
|
|
|
|
InvalidateCompressedStream(compressedStream);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
ReadOnlySpan<byte> ptcJumpTableBytes = new(stream.PositionPointer, innerHeader.PtcJumpTableLength);
|
|
|
|
stream.Seek(innerHeader.PtcJumpTableLength, SeekOrigin.Current);
|
|
|
|
|
|
|
|
Debug.Assert(stream.Position == stream.Length);
|
|
|
|
|
|
|
|
Hash128 ptcJumpTableHash = XXHash128.ComputeHash(ptcJumpTableBytes);
|
|
|
|
|
|
|
|
if (innerHeader.PtcJumpTableHash != ptcJumpTableHash)
|
2021-02-22 02:23:48 +00:00
|
|
|
{
|
|
|
|
InvalidateCompressedStream(compressedStream);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
stream.Seek((long)Unsafe.SizeOf<InnerHeader>(), SeekOrigin.Begin);
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
_infosStream.Write(infosBytes);
|
|
|
|
stream.Seek(innerHeader.InfosLength, SeekOrigin.Current);
|
2021-02-22 02:23:48 +00:00
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
_codesList.ReadFrom(stream);
|
2021-02-22 02:23:48 +00:00
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
_relocsStream.Write(relocsBytes);
|
|
|
|
stream.Seek(innerHeader.RelocsLength, SeekOrigin.Current);
|
2021-02-22 02:23:48 +00:00
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
_unwindInfosStream.Write(unwindInfosBytes);
|
|
|
|
stream.Seek(innerHeader.UnwindInfosLength, SeekOrigin.Current);
|
2021-02-22 02:23:48 +00:00
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
PtcJumpTable = PtcJumpTable.Deserialize(stream);
|
2021-02-22 02:23:48 +00:00
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
Debug.Assert(stream.Position == stream.Length);
|
2021-02-22 02:23:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
if (intPtr != IntPtr.Zero)
|
|
|
|
{
|
|
|
|
Marshal.FreeHGlobal(intPtr);
|
|
|
|
}
|
|
|
|
}
|
2020-06-16 19:28:02 +01:00
|
|
|
}
|
2020-12-17 19:32:09 +00:00
|
|
|
|
|
|
|
long fileSize = new FileInfo(fileName).Length;
|
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
Logger.Info?.Print(LogClass.Ptc, $"{(isBackup ? "Loaded Backup Translation Cache" : "Loaded Translation Cache")} (size: {fileSize} bytes, translated functions: {GetEntriesCount()}).");
|
2020-12-17 19:32:09 +00:00
|
|
|
|
|
|
|
return true;
|
2020-06-16 19:28:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void InvalidateCompressedStream(FileStream compressedStream)
|
|
|
|
{
|
|
|
|
compressedStream.SetLength(0L);
|
|
|
|
}
|
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
private static void PreSave()
|
2020-06-16 19:28:02 +01:00
|
|
|
{
|
|
|
|
_waitEvent.Reset();
|
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
string fileNameActual = string.Concat(CachePathActual, ".cache");
|
|
|
|
string fileNameBackup = string.Concat(CachePathBackup, ".cache");
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
FileInfo fileInfoActual = new FileInfo(fileNameActual);
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
if (fileInfoActual.Exists && fileInfoActual.Length != 0L)
|
|
|
|
{
|
|
|
|
File.Copy(fileNameActual, fileNameBackup, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
Save(fileNameActual);
|
2020-06-16 19:28:02 +01:00
|
|
|
}
|
2021-02-22 02:23:48 +00:00
|
|
|
finally
|
|
|
|
{
|
2021-04-13 02:24:36 +01:00
|
|
|
ResetCarriersIfNeeded();
|
2021-02-22 02:23:48 +00:00
|
|
|
PtcJumpTable.ClearIfNeeded();
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
|
|
|
|
}
|
2020-06-16 19:28:02 +01:00
|
|
|
|
|
|
|
_waitEvent.Set();
|
|
|
|
}
|
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
private static unsafe void Save(string fileName)
|
2020-06-16 19:28:02 +01:00
|
|
|
{
|
2021-01-12 18:04:02 +00:00
|
|
|
int translatedFuncsCount;
|
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
InnerHeader innerHeader = new InnerHeader();
|
|
|
|
|
|
|
|
innerHeader.Magic = _innerHeaderMagic;
|
2021-02-22 02:23:48 +00:00
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
innerHeader.InfosLength = (int)_infosStream.Length;
|
|
|
|
innerHeader.CodesLength = _codesList.Length();
|
|
|
|
innerHeader.RelocsLength = (int)_relocsStream.Length;
|
|
|
|
innerHeader.UnwindInfosLength = (int)_unwindInfosStream.Length;
|
|
|
|
innerHeader.PtcJumpTableLength = PtcJumpTable.GetSerializeSize(PtcJumpTable);
|
2021-02-22 02:23:48 +00:00
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
OuterHeader outerHeader = new OuterHeader();
|
2021-02-22 02:23:48 +00:00
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
outerHeader.Magic = _outerHeaderMagic;
|
|
|
|
|
|
|
|
outerHeader.CacheFileVersion = InternalVersion;
|
|
|
|
outerHeader.Endianness = GetEndianness();
|
|
|
|
outerHeader.FeatureInfo = GetFeatureInfo();
|
|
|
|
outerHeader.OSPlatform = GetOSPlatform();
|
|
|
|
|
|
|
|
outerHeader.UncompressedStreamSize =
|
|
|
|
(long)Unsafe.SizeOf<InnerHeader>() +
|
|
|
|
innerHeader.InfosLength +
|
|
|
|
innerHeader.CodesLength +
|
|
|
|
innerHeader.RelocsLength +
|
|
|
|
innerHeader.UnwindInfosLength +
|
|
|
|
innerHeader.PtcJumpTableLength;
|
|
|
|
|
|
|
|
outerHeader.SetHeaderHash();
|
2021-02-22 02:23:48 +00:00
|
|
|
|
|
|
|
IntPtr intPtr = IntPtr.Zero;
|
|
|
|
|
|
|
|
try
|
2020-06-16 19:28:02 +01:00
|
|
|
{
|
2021-04-13 02:24:36 +01:00
|
|
|
intPtr = Marshal.AllocHGlobal(new IntPtr(outerHeader.UncompressedStreamSize));
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
using (UnmanagedMemoryStream stream = new((byte*)intPtr.ToPointer(), outerHeader.UncompressedStreamSize, outerHeader.UncompressedStreamSize, FileAccess.ReadWrite))
|
2021-02-22 02:23:48 +00:00
|
|
|
{
|
2021-04-13 02:24:36 +01:00
|
|
|
stream.Seek((long)Unsafe.SizeOf<InnerHeader>(), SeekOrigin.Begin);
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
ReadOnlySpan<byte> infosBytes = new(stream.PositionPointer, innerHeader.InfosLength);
|
2021-02-22 02:23:48 +00:00
|
|
|
_infosStream.WriteTo(stream);
|
2021-04-13 02:24:36 +01:00
|
|
|
|
|
|
|
ReadOnlySpan<byte> codesBytes = (int)innerHeader.CodesLength > 0 ? new(stream.PositionPointer, (int)innerHeader.CodesLength) : ReadOnlySpan<byte>.Empty;
|
|
|
|
_codesList.WriteTo(stream);
|
|
|
|
|
|
|
|
ReadOnlySpan<byte> relocsBytes = new(stream.PositionPointer, innerHeader.RelocsLength);
|
2021-02-22 02:23:48 +00:00
|
|
|
_relocsStream.WriteTo(stream);
|
2021-04-13 02:24:36 +01:00
|
|
|
|
|
|
|
ReadOnlySpan<byte> unwindInfosBytes = new(stream.PositionPointer, innerHeader.UnwindInfosLength);
|
2021-02-22 02:23:48 +00:00
|
|
|
_unwindInfosStream.WriteTo(stream);
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
ReadOnlySpan<byte> ptcJumpTableBytes = new(stream.PositionPointer, innerHeader.PtcJumpTableLength);
|
2021-02-22 02:23:48 +00:00
|
|
|
PtcJumpTable.Serialize(stream, PtcJumpTable);
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
Debug.Assert(stream.Position == stream.Length);
|
|
|
|
|
|
|
|
innerHeader.InfosHash = XXHash128.ComputeHash(infosBytes);
|
|
|
|
innerHeader.CodesHash = XXHash128.ComputeHash(codesBytes);
|
|
|
|
innerHeader.RelocsHash = XXHash128.ComputeHash(relocsBytes);
|
|
|
|
innerHeader.UnwindInfosHash = XXHash128.ComputeHash(unwindInfosBytes);
|
|
|
|
innerHeader.PtcJumpTableHash = XXHash128.ComputeHash(ptcJumpTableBytes);
|
|
|
|
|
|
|
|
innerHeader.SetHeaderHash();
|
2021-01-12 18:04:02 +00:00
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
stream.Seek(0L, SeekOrigin.Begin);
|
2021-04-13 02:24:36 +01:00
|
|
|
SerializeStructure(stream, innerHeader);
|
2021-01-12 18:04:02 +00:00
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
translatedFuncsCount = GetEntriesCount();
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
ResetCarriersIfNeeded();
|
2021-02-22 02:23:48 +00:00
|
|
|
PtcJumpTable.ClearIfNeeded();
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
using (FileStream compressedStream = new(fileName, FileMode.OpenOrCreate))
|
|
|
|
using (DeflateStream deflateStream = new(compressedStream, SaveCompressionLevel, true))
|
2020-06-16 19:28:02 +01:00
|
|
|
{
|
2021-02-22 02:23:48 +00:00
|
|
|
try
|
|
|
|
{
|
2021-04-13 02:24:36 +01:00
|
|
|
SerializeStructure(compressedStream, outerHeader);
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
stream.Seek(0L, SeekOrigin.Begin);
|
|
|
|
stream.CopyTo(deflateStream);
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
compressedStream.Position = 0L;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (compressedStream.Position < compressedStream.Length)
|
|
|
|
{
|
|
|
|
compressedStream.SetLength(compressedStream.Position);
|
|
|
|
}
|
2020-06-16 19:28:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-22 02:23:48 +00:00
|
|
|
finally
|
|
|
|
{
|
|
|
|
if (intPtr != IntPtr.Zero)
|
|
|
|
{
|
|
|
|
Marshal.FreeHGlobal(intPtr);
|
|
|
|
}
|
|
|
|
}
|
2020-12-17 19:32:09 +00:00
|
|
|
|
|
|
|
long fileSize = new FileInfo(fileName).Length;
|
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
if (fileSize != 0L)
|
|
|
|
{
|
|
|
|
Logger.Info?.Print(LogClass.Ptc, $"Saved Translation Cache (size: {fileSize} bytes, translated functions: {translatedFuncsCount}).");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-10 11:54:24 +01:00
|
|
|
internal static void LoadTranslations(ConcurrentDictionary<ulong, TranslatedFunction> funcs, IMemoryManager memory,
|
|
|
|
JumpTable jumpTable, EntryTable<uint> countTable)
|
2020-06-16 19:28:02 +01:00
|
|
|
{
|
2021-04-13 02:24:36 +01:00
|
|
|
if (AreCarriersEmpty())
|
2020-06-16 19:28:02 +01:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_infosStream.Seek(0L, SeekOrigin.Begin);
|
|
|
|
_relocsStream.Seek(0L, SeekOrigin.Begin);
|
|
|
|
_unwindInfosStream.Seek(0L, SeekOrigin.Begin);
|
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
using (BinaryReader infosReader = new(_infosStream, EncodingCache.UTF8NoBOM, true))
|
|
|
|
using (BinaryReader relocsReader = new(_relocsStream, EncodingCache.UTF8NoBOM, true))
|
|
|
|
using (BinaryReader unwindInfosReader = new(_unwindInfosStream, EncodingCache.UTF8NoBOM, true))
|
2020-06-16 19:28:02 +01:00
|
|
|
{
|
2021-04-13 02:24:36 +01:00
|
|
|
for (int index = 0; index < GetEntriesCount(); index++)
|
2020-06-16 19:28:02 +01:00
|
|
|
{
|
|
|
|
InfoEntry infoEntry = ReadInfo(infosReader);
|
|
|
|
|
2020-12-17 19:32:09 +00:00
|
|
|
if (infoEntry.Stubbed)
|
2020-06-16 19:28:02 +01:00
|
|
|
{
|
2021-04-13 02:24:36 +01:00
|
|
|
SkipCode(index, infoEntry.CodeLength);
|
2020-12-17 19:32:09 +00:00
|
|
|
SkipReloc(infoEntry.RelocEntriesCount);
|
|
|
|
SkipUnwindInfo(unwindInfosReader);
|
2020-06-16 19:28:02 +01:00
|
|
|
}
|
2021-02-22 02:23:48 +00:00
|
|
|
else if (infoEntry.HighCq || !PtcProfiler.ProfiledFuncs.TryGetValue(infoEntry.Address, out var value) || !value.HighCq)
|
2020-12-17 19:32:09 +00:00
|
|
|
{
|
2021-04-13 02:24:36 +01:00
|
|
|
byte[] code = ReadCode(index, infoEntry.CodeLength);
|
2020-12-17 19:32:09 +00:00
|
|
|
|
2021-04-10 11:54:24 +01:00
|
|
|
Counter<uint> callCounter = null;
|
2021-04-09 17:55:24 +01:00
|
|
|
|
2020-12-17 19:32:09 +00:00
|
|
|
if (infoEntry.RelocEntriesCount != 0)
|
|
|
|
{
|
|
|
|
RelocEntry[] relocEntries = GetRelocEntries(relocsReader, infoEntry.RelocEntriesCount);
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-04-09 17:55:24 +01:00
|
|
|
if (!PatchCode(code, relocEntries, memory.PageTablePointer, jumpTable, countTable, out callCounter))
|
|
|
|
{
|
|
|
|
SkipUnwindInfo(unwindInfosReader);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2020-12-17 19:32:09 +00:00
|
|
|
}
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2020-12-17 19:32:09 +00:00
|
|
|
UnwindInfo unwindInfo = ReadUnwindInfo(unwindInfosReader);
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-04-09 17:55:24 +01:00
|
|
|
TranslatedFunction func = FastTranslate(code, callCounter, infoEntry.GuestSize, unwindInfo, infoEntry.HighCq);
|
2020-12-17 19:32:09 +00:00
|
|
|
|
|
|
|
bool isAddressUnique = funcs.TryAdd(infoEntry.Address, func);
|
|
|
|
|
|
|
|
Debug.Assert(isAddressUnique, $"The address 0x{infoEntry.Address:X16} is not unique.");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
infoEntry.Stubbed = true;
|
2021-04-13 02:24:36 +01:00
|
|
|
infoEntry.CodeLength = 0;
|
2020-12-17 19:32:09 +00:00
|
|
|
UpdateInfo(infoEntry);
|
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
StubCode(index);
|
2020-12-17 19:32:09 +00:00
|
|
|
StubReloc(infoEntry.RelocEntriesCount);
|
|
|
|
StubUnwindInfo(unwindInfosReader);
|
|
|
|
}
|
2020-06-16 19:28:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_infosStream.Position < _infosStream.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");
|
|
|
|
}
|
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
private static int GetEntriesCount()
|
2020-12-17 19:32:09 +00:00
|
|
|
{
|
2021-04-13 02:24:36 +01:00
|
|
|
return _codesList.Count;
|
2020-06-16 19:28:02 +01: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 19:28:02 +01:00
|
|
|
infoEntry.HighCq = infosReader.ReadBoolean();
|
2020-12-17 19:32:09 +00:00
|
|
|
infoEntry.Stubbed = infosReader.ReadBoolean();
|
2021-04-13 02:24:36 +01:00
|
|
|
infoEntry.CodeLength = infosReader.ReadInt32();
|
2020-06-16 19:28:02 +01:00
|
|
|
infoEntry.RelocEntriesCount = infosReader.ReadInt32();
|
|
|
|
|
|
|
|
return infoEntry;
|
|
|
|
}
|
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
[Conditional("DEBUG")]
|
|
|
|
private static void SkipCode(int index, int codeLength)
|
2020-12-17 19:32:09 +00:00
|
|
|
{
|
2021-04-13 02:24:36 +01:00
|
|
|
Debug.Assert(_codesList[index].Length == 0);
|
|
|
|
Debug.Assert(codeLength == 0);
|
2020-12-17 19:32:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
private static byte[] ReadCode(int index, int codeLength)
|
2020-06-16 19:28:02 +01:00
|
|
|
{
|
2021-04-13 02:24:36 +01:00
|
|
|
Debug.Assert(_codesList[index].Length == codeLength);
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
return _codesList[index];
|
2020-06-16 19:28:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-04-10 11:54:24 +01:00
|
|
|
private static bool PatchCode(Span<byte> code, RelocEntry[] relocEntries, IntPtr pageTablePointer,
|
|
|
|
JumpTable jumpTable, EntryTable<uint> countTable, out Counter<uint> callCounter)
|
2020-06-16 19:28:02 +01:00
|
|
|
{
|
2021-04-09 17:55:24 +01:00
|
|
|
callCounter = null;
|
|
|
|
|
2020-06-16 19:28:02 +01:00
|
|
|
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();
|
|
|
|
}
|
2021-04-09 17:55:24 +01:00
|
|
|
else if (relocEntry.Index == CountTableIndex)
|
|
|
|
{
|
|
|
|
// If we could not allocate an entry on the count table we dip.
|
2021-04-10 11:54:24 +01:00
|
|
|
if (!Counter<uint>.TryCreate(countTable, out Counter<uint> counter))
|
2021-04-09 17:55:24 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe { imm = (ulong)Unsafe.AsPointer(ref counter.Value); }
|
|
|
|
}
|
2020-06-16 19:28:02 +01:00
|
|
|
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);
|
|
|
|
}
|
2021-04-09 17:55:24 +01:00
|
|
|
|
|
|
|
return true;
|
2020-06-16 19:28:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-04-10 11:54:24 +01:00
|
|
|
private static TranslatedFunction FastTranslate(byte[] code, Counter<uint> callCounter, ulong guestSize,
|
|
|
|
UnwindInfo unwindInfo, bool highCq)
|
2020-06-16 19:28:02 +01:00
|
|
|
{
|
2021-04-13 02:24:36 +01:00
|
|
|
CompiledFunction cFunc = new CompiledFunction(code, unwindInfo);
|
2020-06-16 19:28:02 +01:00
|
|
|
|
|
|
|
IntPtr codePtr = JitCache.Map(cFunc);
|
|
|
|
|
|
|
|
GuestFunction gFunc = Marshal.GetDelegateForFunctionPointer<GuestFunction>(codePtr);
|
|
|
|
|
2021-04-09 17:55:24 +01:00
|
|
|
TranslatedFunction tFunc = new TranslatedFunction(gFunc, callCounter, guestSize, highCq);
|
2020-06-16 19:28:02 +01: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);
|
2021-04-13 02:24:36 +01:00
|
|
|
_infosWriter.Write((int)infoEntry.CodeLength);
|
2020-12-17 19:32:09 +00:00
|
|
|
_infosWriter.Write((int)infoEntry.RelocEntriesCount);
|
|
|
|
}
|
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
private static void StubCode(int index)
|
2020-12-17 19:32:09 +00:00
|
|
|
{
|
2021-04-13 02:24:36 +01:00
|
|
|
_codesList[index] = Array.Empty<byte>();
|
2020-12-17 19:32:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-10 11:54:24 +01:00
|
|
|
internal static void MakeAndSaveTranslations(ConcurrentDictionary<ulong, TranslatedFunction> funcs, IMemoryManager memory,
|
|
|
|
JumpTable jumpTable, EntryTable<uint> countTable)
|
2020-06-16 19:28:02 +01:00
|
|
|
{
|
2020-12-17 19:32:09 +00:00
|
|
|
var profiledFuncsToTranslate = PtcProfiler.GetProfiledFuncsToTranslate(funcs);
|
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
_translateCount = 0;
|
|
|
|
_translateTotalCount = profiledFuncsToTranslate.Count;
|
|
|
|
|
|
|
|
int degreeOfParallelism = new DegreeOfParallelism(4d, 75d, 12.5d).GetDegreeOfParallelism(0, 32);
|
|
|
|
|
|
|
|
if (_translateTotalCount == 0 || degreeOfParallelism == 0)
|
2020-06-16 19:28:02 +01:00
|
|
|
{
|
2021-04-13 02:24:36 +01:00
|
|
|
ResetCarriersIfNeeded();
|
2021-02-22 02:23:48 +00:00
|
|
|
PtcJumpTable.ClearIfNeeded();
|
|
|
|
|
|
|
|
GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
|
|
|
|
|
2020-06-16 19:28:02 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
Logger.Info?.Print(LogClass.Ptc, $"{_translateCount} of {_translateTotalCount} functions translated | Thread count: {degreeOfParallelism}");
|
2021-03-22 18:40:07 +00:00
|
|
|
|
|
|
|
PtcStateChanged?.Invoke(PtcLoadingState.Start, _translateCount, _translateTotalCount);
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-03-22 18:40:07 +00:00
|
|
|
using AutoResetEvent progressReportEvent = new AutoResetEvent(false);
|
|
|
|
|
|
|
|
Thread progressReportThread = new Thread(ReportProgress)
|
|
|
|
{
|
|
|
|
Name = "Ptc.ProgressReporter",
|
|
|
|
Priority = ThreadPriority.Lowest,
|
|
|
|
IsBackground = true
|
|
|
|
};
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-03-22 18:40:07 +00:00
|
|
|
progressReportThread.Start(progressReportEvent);
|
2021-03-03 00:39:36 +00:00
|
|
|
|
2020-12-24 02:58:36 +00:00
|
|
|
void TranslateFuncs()
|
2020-06-16 19:28:02 +01:00
|
|
|
{
|
2020-12-24 02:58:36 +00:00
|
|
|
while (profiledFuncsToTranslate.TryDequeue(out var item))
|
|
|
|
{
|
|
|
|
ulong address = item.address;
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2020-12-24 02:58:36 +00:00
|
|
|
Debug.Assert(PtcProfiler.IsAddressInStaticCodeRange(address));
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-04-09 17:49:15 +01:00
|
|
|
TranslatedFunction func = Translator.Translate(memory, jumpTable, countTable, address, item.mode, item.highCq);
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2020-12-24 02:58:36 +00:00
|
|
|
bool isAddressUnique = funcs.TryAdd(address, func);
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2020-12-24 02:58:36 +00:00
|
|
|
Debug.Assert(isAddressUnique, $"The address 0x{address:X16} is not unique.");
|
2020-06-16 19:28:02 +01: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 19:28:02 +01:00
|
|
|
}
|
2021-02-22 02:23:48 +00:00
|
|
|
|
|
|
|
Translator.DisposePools();
|
2020-12-24 02:58:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
List<Thread> threads = new List<Thread>();
|
2020-12-17 19:32:09 +00:00
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
for (int i = 0; i < degreeOfParallelism; i++)
|
2020-12-24 02:58:36 +00:00
|
|
|
{
|
|
|
|
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 19:28:02 +01:00
|
|
|
|
2021-03-22 18:40:07 +00:00
|
|
|
progressReportEvent.Set();
|
|
|
|
progressReportThread.Join();
|
|
|
|
|
|
|
|
PtcStateChanged?.Invoke(PtcLoadingState.Loaded, _translateCount, _translateTotalCount);
|
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
Logger.Info?.Print(LogClass.Ptc, $"{_translateCount} of {_translateTotalCount} functions translated | Thread count: {degreeOfParallelism}");
|
2021-01-12 18:04:02 +00:00
|
|
|
|
2020-12-17 19:32:09 +00:00
|
|
|
PtcJumpTable.Initialize(jumpTable);
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2020-12-17 19:32:09 +00:00
|
|
|
PtcJumpTable.ReadJumpTable(jumpTable);
|
|
|
|
PtcJumpTable.ReadDynamicTable(jumpTable);
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
Thread preSaveThread = new Thread(PreSave);
|
|
|
|
preSaveThread.IsBackground = true;
|
|
|
|
preSaveThread.Start();
|
2020-06-16 19:28:02 +01:00
|
|
|
}
|
|
|
|
|
2021-03-22 18:40:07 +00:00
|
|
|
private static void ReportProgress(object state)
|
2020-06-16 19:28:02 +01:00
|
|
|
{
|
2021-04-13 02:24:36 +01:00
|
|
|
const int refreshRate = 50; // ms.
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-03-22 18:40:07 +00:00
|
|
|
AutoResetEvent endEvent = (AutoResetEvent)state;
|
|
|
|
|
|
|
|
int count = 0;
|
2020-06-16 19:28:02 +01:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2021-03-22 18:40:07 +00:00
|
|
|
int newCount = _translateCount;
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-03-22 18:40:07 +00:00
|
|
|
if (count != newCount)
|
|
|
|
{
|
|
|
|
PtcStateChanged?.Invoke(PtcLoadingState.Loading, newCount, _translateTotalCount);
|
|
|
|
count = newCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (!endEvent.WaitOne(refreshRate));
|
2020-06-16 19:28:02 +01:00
|
|
|
}
|
|
|
|
|
2021-01-12 18:04:02 +00:00
|
|
|
internal static void WriteInfoCodeRelocUnwindInfo(ulong address, ulong guestSize, bool highCq, PtcInfo ptcInfo)
|
2020-06-16 19:28:02 +01: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 19:28:02 +01:00
|
|
|
_infosWriter.Write((bool)highCq); // InfoEntry.HighCq
|
2020-12-17 19:32:09 +00:00
|
|
|
_infosWriter.Write((bool)false); // InfoEntry.Stubbed
|
2021-04-13 02:24:36 +01:00
|
|
|
_infosWriter.Write((int)ptcInfo.Code.Length); // InfoEntry.CodeLength
|
2020-06-16 19:28:02 +01:00
|
|
|
_infosWriter.Write((int)ptcInfo.RelocEntriesCount); // InfoEntry.RelocEntriesCount
|
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
WriteCode(ptcInfo.Code.AsSpan());
|
2020-06-16 19:28:02 +01:00
|
|
|
|
|
|
|
// WriteReloc.
|
|
|
|
ptcInfo.RelocStream.WriteTo(_relocsStream);
|
|
|
|
|
|
|
|
// WriteUnwindInfo.
|
|
|
|
ptcInfo.UnwindInfoStream.WriteTo(_unwindInfosStream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
private static void WriteCode(ReadOnlySpan<byte> code)
|
|
|
|
{
|
|
|
|
_codesList.Add(code.ToArray());
|
|
|
|
}
|
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
private static bool GetEndianness()
|
|
|
|
{
|
|
|
|
return BitConverter.IsLittleEndian;
|
|
|
|
}
|
|
|
|
|
2020-06-16 19:28:02 +01:00
|
|
|
private static ulong GetFeatureInfo()
|
|
|
|
{
|
2020-11-18 18:35:54 +00:00
|
|
|
return (ulong)HardwareCapabilities.FeatureInfoEdx << 32 | (uint)HardwareCapabilities.FeatureInfoEcx;
|
2020-06-16 19:28:02 +01: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;
|
|
|
|
}
|
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
[StructLayout(LayoutKind.Sequential, Pack = 1/*, Size = 49*/)]
|
|
|
|
private struct OuterHeader
|
2020-06-16 19:28:02 +01:00
|
|
|
{
|
2021-02-22 02:23:48 +00:00
|
|
|
public ulong Magic;
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2020-12-17 19:32:09 +00:00
|
|
|
public uint CacheFileVersion;
|
2021-04-13 02:24:36 +01:00
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
public bool Endianness;
|
2020-06-16 19:28:02 +01:00
|
|
|
public ulong FeatureInfo;
|
2020-12-17 19:32:09 +00:00
|
|
|
public uint OSPlatform;
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
public long UncompressedStreamSize;
|
|
|
|
|
|
|
|
public Hash128 HeaderHash;
|
|
|
|
|
|
|
|
public void SetHeaderHash()
|
|
|
|
{
|
|
|
|
Span<OuterHeader> spanHeader = MemoryMarshal.CreateSpan(ref this, 1);
|
|
|
|
|
|
|
|
HeaderHash = XXHash128.ComputeHash(MemoryMarshal.AsBytes(spanHeader).Slice(0, Unsafe.SizeOf<OuterHeader>() - Unsafe.SizeOf<Hash128>()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool IsHeaderValid()
|
|
|
|
{
|
|
|
|
Span<OuterHeader> spanHeader = MemoryMarshal.CreateSpan(ref this, 1);
|
|
|
|
|
|
|
|
return XXHash128.ComputeHash(MemoryMarshal.AsBytes(spanHeader).Slice(0, Unsafe.SizeOf<OuterHeader>() - Unsafe.SizeOf<Hash128>())) == HeaderHash;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[StructLayout(LayoutKind.Sequential, Pack = 1/*, Size = 128*/)]
|
|
|
|
private struct InnerHeader
|
|
|
|
{
|
|
|
|
public ulong Magic;
|
|
|
|
|
|
|
|
public int InfosLength;
|
|
|
|
public long CodesLength;
|
|
|
|
public int RelocsLength;
|
|
|
|
public int UnwindInfosLength;
|
|
|
|
public int PtcJumpTableLength;
|
|
|
|
|
|
|
|
public Hash128 InfosHash;
|
|
|
|
public Hash128 CodesHash;
|
|
|
|
public Hash128 RelocsHash;
|
|
|
|
public Hash128 UnwindInfosHash;
|
|
|
|
public Hash128 PtcJumpTableHash;
|
|
|
|
|
|
|
|
public Hash128 HeaderHash;
|
|
|
|
|
|
|
|
public void SetHeaderHash()
|
|
|
|
{
|
|
|
|
Span<InnerHeader> spanHeader = MemoryMarshal.CreateSpan(ref this, 1);
|
|
|
|
|
|
|
|
HeaderHash = XXHash128.ComputeHash(MemoryMarshal.AsBytes(spanHeader).Slice(0, Unsafe.SizeOf<InnerHeader>() - Unsafe.SizeOf<Hash128>()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool IsHeaderValid()
|
|
|
|
{
|
|
|
|
Span<InnerHeader> spanHeader = MemoryMarshal.CreateSpan(ref this, 1);
|
|
|
|
|
|
|
|
return XXHash128.ComputeHash(MemoryMarshal.AsBytes(spanHeader).Slice(0, Unsafe.SizeOf<InnerHeader>() - Unsafe.SizeOf<Hash128>())) == HeaderHash;
|
|
|
|
}
|
2020-06-16 19:28:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private struct InfoEntry
|
|
|
|
{
|
2020-12-17 19:32:09 +00:00
|
|
|
public const int Stride = 26; // Bytes.
|
2020-06-16 19:28:02 +01:00
|
|
|
|
2020-12-16 20:07:42 +00:00
|
|
|
public ulong Address;
|
|
|
|
public ulong GuestSize;
|
2020-06-16 19:28:02 +01:00
|
|
|
public bool HighCq;
|
2020-12-17 19:32:09 +00:00
|
|
|
public bool Stubbed;
|
2021-04-13 02:24:36 +01:00
|
|
|
public int CodeLength;
|
2020-06-16 19:28:02 +01:00
|
|
|
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();
|
|
|
|
|
2021-04-13 02:24:36 +01:00
|
|
|
DisposeCarriers();
|
2020-06-16 19:28:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-24 03:44:39 +00:00
|
|
|
}
|