From 427b7d06b5ab6d2b06784a9d283eaf836a04c27e Mon Sep 17 00:00:00 2001 From: gdkchan Date: Sat, 20 Jan 2024 11:11:28 -0300 Subject: Implement a new JIT for Arm devices (#6057) * Implement a new JIT for Arm devices * Auto-format * Make a lot of Assembler members read-only * More read-only * Fix more warnings * ObjectDisposedException.ThrowIf * New JIT cache for platforms that enforce W^X, currently unused * Remove unused using * Fix assert * Pass memory manager type around * Safe memory manager mode support + other improvements * Actual safe memory manager mode masking support * PR feedback --- src/ARMeilleure/Translation/IntervalTree.cs | 2 +- src/ARMeilleure/Translation/Translator.cs | 2 +- src/ARMeilleure/Translation/TranslatorStubs.cs | 29 +++++++++++++------------- 3 files changed, 17 insertions(+), 16 deletions(-) (limited to 'src/ARMeilleure/Translation') diff --git a/src/ARMeilleure/Translation/IntervalTree.cs b/src/ARMeilleure/Translation/IntervalTree.cs index da29d6a6..a5f9b5d5 100644 --- a/src/ARMeilleure/Translation/IntervalTree.cs +++ b/src/ARMeilleure/Translation/IntervalTree.cs @@ -8,7 +8,7 @@ namespace ARMeilleure.Translation /// /// Key /// Value - class IntervalTree where TK : IComparable + public class IntervalTree where TK : IComparable { private const int ArrayGrowthSize = 32; diff --git a/src/ARMeilleure/Translation/Translator.cs b/src/ARMeilleure/Translation/Translator.cs index 48c1a575..014b1203 100644 --- a/src/ARMeilleure/Translation/Translator.cs +++ b/src/ARMeilleure/Translation/Translator.cs @@ -73,7 +73,7 @@ namespace ARMeilleure.Translation CountTable = new EntryTable(); Functions = new TranslatorCache(); FunctionTable = new AddressTable(for64Bits ? _levels64Bit : _levels32Bit); - Stubs = new TranslatorStubs(this); + Stubs = new TranslatorStubs(FunctionTable); FunctionTable.Fill = (ulong)Stubs.SlowDispatchStub; } diff --git a/src/ARMeilleure/Translation/TranslatorStubs.cs b/src/ARMeilleure/Translation/TranslatorStubs.cs index bbe48c16..d80823a8 100644 --- a/src/ARMeilleure/Translation/TranslatorStubs.cs +++ b/src/ARMeilleure/Translation/TranslatorStubs.cs @@ -1,3 +1,4 @@ +using ARMeilleure.Common; using ARMeilleure.Instructions; using ARMeilleure.IntermediateRepresentation; using ARMeilleure.State; @@ -14,11 +15,11 @@ namespace ARMeilleure.Translation /// class TranslatorStubs : IDisposable { - private static readonly Lazy _slowDispatchStub = new(GenerateSlowDispatchStub, isThreadSafe: true); + private readonly Lazy _slowDispatchStub; private bool _disposed; - private readonly Translator _translator; + private readonly AddressTable _functionTable; private readonly Lazy _dispatchStub; private readonly Lazy _dispatchLoop; private readonly Lazy _contextWrapper; @@ -83,13 +84,14 @@ namespace ARMeilleure.Translation /// Initializes a new instance of the class with the specified /// instance. /// - /// instance to use + /// Function table used to store pointers to the functions that the guest code will call /// is null - public TranslatorStubs(Translator translator) + public TranslatorStubs(AddressTable functionTable) { - ArgumentNullException.ThrowIfNull(translator); + ArgumentNullException.ThrowIfNull(functionTable); - _translator = translator; + _functionTable = functionTable; + _slowDispatchStub = new(GenerateSlowDispatchStub, isThreadSafe: true); _dispatchStub = new(GenerateDispatchStub, isThreadSafe: true); _dispatchLoop = new(GenerateDispatchLoop, isThreadSafe: true); _contextWrapper = new(GenerateContextWrapper, isThreadSafe: true); @@ -151,15 +153,15 @@ namespace ARMeilleure.Translation context.Add(nativeContext, Const((ulong)NativeContext.GetDispatchAddressOffset()))); // Check if guest address is within range of the AddressTable. - Operand masked = context.BitwiseAnd(guestAddress, Const(~_translator.FunctionTable.Mask)); + Operand masked = context.BitwiseAnd(guestAddress, Const(~_functionTable.Mask)); context.BranchIfTrue(lblFallback, masked); Operand index = default; - Operand page = Const((long)_translator.FunctionTable.Base); + Operand page = Const((long)_functionTable.Base); - for (int i = 0; i < _translator.FunctionTable.Levels.Length; i++) + for (int i = 0; i < _functionTable.Levels.Length; i++) { - ref var level = ref _translator.FunctionTable.Levels[i]; + ref var level = ref _functionTable.Levels[i]; // level.Mask is not used directly because it is more often bigger than 32-bits, so it will not // be encoded as an immediate on x86's bitwise and operation. @@ -167,7 +169,7 @@ namespace ARMeilleure.Translation index = context.BitwiseAnd(context.ShiftRightUI(guestAddress, Const(level.Index)), mask); - if (i < _translator.FunctionTable.Levels.Length - 1) + if (i < _functionTable.Levels.Length - 1) { page = context.Load(OperandType.I64, context.Add(page, context.ShiftLeft(index, Const(3)))); context.BranchIfFalse(lblFallback, page); @@ -196,7 +198,7 @@ namespace ARMeilleure.Translation /// Generates a . /// /// Generated - private static IntPtr GenerateSlowDispatchStub() + private IntPtr GenerateSlowDispatchStub() { var context = new EmitterContext(); @@ -205,8 +207,7 @@ namespace ARMeilleure.Translation Operand guestAddress = context.Load(OperandType.I64, context.Add(nativeContext, Const((ulong)NativeContext.GetDispatchAddressOffset()))); - MethodInfo getFuncAddress = typeof(NativeInterface).GetMethod(nameof(NativeInterface.GetFunctionAddress)); - Operand hostAddress = context.Call(getFuncAddress, guestAddress); + Operand hostAddress = context.Call(typeof(NativeInterface).GetMethod(nameof(NativeInterface.GetFunctionAddress)), guestAddress); context.Tailcall(hostAddress, nativeContext); var cfg = context.GetControlFlowGraph(); -- cgit v1.2.3